Statistics
| Branch: | Revision:

ffmpeg / libavformat / img2.c @ 0bb240ac

History | View | Annotate | Download (13.5 KB)

1
/*
2
 * Image format
3
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4
 * Copyright (c) 2004 Michael Niedermayer
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22

    
23
#include "libavutil/intreadwrite.h"
24
#include "libavutil/avstring.h"
25
#include "avformat.h"
26
#include "avio_internal.h"
27
#include <strings.h>
28

    
29
typedef struct {
30
    int img_first;
31
    int img_last;
32
    int img_number;
33
    int img_count;
34
    int is_pipe;
35
    int split_planes;  /**< use independent file for each Y, U, V plane */
36
    char path[1024];
37
} VideoData;
38

    
39
typedef struct {
40
    enum CodecID id;
41
    const char *str;
42
} IdStrMap;
43

    
44
static const IdStrMap img_tags[] = {
45
    { CODEC_ID_MJPEG     , "jpeg"},
46
    { CODEC_ID_MJPEG     , "jpg"},
47
    { CODEC_ID_LJPEG     , "ljpg"},
48
    { CODEC_ID_PNG       , "png"},
49
    { CODEC_ID_PNG       , "mng"},
50
    { CODEC_ID_PPM       , "ppm"},
51
    { CODEC_ID_PPM       , "pnm"},
52
    { CODEC_ID_PGM       , "pgm"},
53
    { CODEC_ID_PGMYUV    , "pgmyuv"},
54
    { CODEC_ID_PBM       , "pbm"},
55
    { CODEC_ID_PAM       , "pam"},
56
    { CODEC_ID_MPEG1VIDEO, "mpg1-img"},
57
    { CODEC_ID_MPEG2VIDEO, "mpg2-img"},
58
    { CODEC_ID_MPEG4     , "mpg4-img"},
59
    { CODEC_ID_FFV1      , "ffv1-img"},
60
    { CODEC_ID_RAWVIDEO  , "y"},
61
    { CODEC_ID_BMP       , "bmp"},
62
    { CODEC_ID_GIF       , "gif"},
63
    { CODEC_ID_TARGA     , "tga"},
64
    { CODEC_ID_TIFF      , "tiff"},
65
    { CODEC_ID_TIFF      , "tif"},
66
    { CODEC_ID_SGI       , "sgi"},
67
    { CODEC_ID_PTX       , "ptx"},
68
    { CODEC_ID_PCX       , "pcx"},
69
    { CODEC_ID_SUNRAST   , "sun"},
70
    { CODEC_ID_SUNRAST   , "ras"},
71
    { CODEC_ID_SUNRAST   , "rs"},
72
    { CODEC_ID_SUNRAST   , "im1"},
73
    { CODEC_ID_SUNRAST   , "im8"},
74
    { CODEC_ID_SUNRAST   , "im24"},
75
    { CODEC_ID_SUNRAST   , "sunras"},
76
    { CODEC_ID_JPEG2000  , "jp2"},
77
    { CODEC_ID_DPX       , "dpx"},
78
    { CODEC_ID_PICTOR    , "pic"},
79
    { CODEC_ID_NONE      , NULL}
80
};
81

    
82
static const int sizes[][2] = {
83
    { 640, 480 },
84
    { 720, 480 },
85
    { 720, 576 },
86
    { 352, 288 },
87
    { 352, 240 },
88
    { 160, 128 },
89
    { 512, 384 },
90
    { 640, 352 },
91
    { 640, 240 },
92
};
93

    
94
static int infer_size(int *width_ptr, int *height_ptr, int size)
95
{
96
    int i;
97

    
98
    for(i=0;i<FF_ARRAY_ELEMS(sizes);i++) {
99
        if ((sizes[i][0] * sizes[i][1]) == size) {
100
            *width_ptr = sizes[i][0];
101
            *height_ptr = sizes[i][1];
102
            return 0;
103
        }
104
    }
105
    return -1;
106
}
107
static enum CodecID av_str2id(const IdStrMap *tags, const char *str)
108
{
109
    str= strrchr(str, '.');
110
    if(!str) return CODEC_ID_NONE;
111
    str++;
112

    
113
    while (tags->id) {
114
        if (!strcasecmp(str, tags->str))
115
            return tags->id;
116

    
117
        tags++;
118
    }
119
    return CODEC_ID_NONE;
120
}
121

    
122
/* return -1 if no image found */
123
static int find_image_range(int *pfirst_index, int *plast_index,
124
                            const char *path)
125
{
126
    char buf[1024];
127
    int range, last_index, range1, first_index;
128

    
129
    /* find the first image */
130
    for(first_index = 0; first_index < 5; first_index++) {
131
        if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0){
132
            *pfirst_index =
133
            *plast_index = 1;
134
            if(url_exist(buf))
135
                return 0;
136
            return -1;
137
        }
138
        if (url_exist(buf))
139
            break;
140
    }
141
    if (first_index == 5)
142
        goto fail;
143

    
144
    /* find the last image */
145
    last_index = first_index;
146
    for(;;) {
147
        range = 0;
148
        for(;;) {
149
            if (!range)
150
                range1 = 1;
151
            else
152
                range1 = 2 * range;
153
            if (av_get_frame_filename(buf, sizeof(buf), path,
154
                                      last_index + range1) < 0)
155
                goto fail;
156
            if (!url_exist(buf))
157
                break;
158
            range = range1;
159
            /* just in case... */
160
            if (range >= (1 << 30))
161
                goto fail;
162
        }
163
        /* we are sure than image last_index + range exists */
164
        if (!range)
165
            break;
166
        last_index += range;
167
    }
168
    *pfirst_index = first_index;
169
    *plast_index = last_index;
170
    return 0;
171
 fail:
172
    return -1;
173
}
174

    
175

    
176
static int read_probe(AVProbeData *p)
177
{
178
    if (p->filename && av_str2id(img_tags, p->filename)) {
179
        if (av_filename_number_test(p->filename))
180
            return AVPROBE_SCORE_MAX;
181
        else
182
            return AVPROBE_SCORE_MAX/2;
183
    }
184
    return 0;
185
}
186

    
187
enum CodecID av_guess_image2_codec(const char *filename){
188
    return av_str2id(img_tags, filename);
189
}
190

    
191
static int read_header(AVFormatContext *s1, AVFormatParameters *ap)
192
{
193
    VideoData *s = s1->priv_data;
194
    int first_index, last_index;
195
    AVStream *st;
196

    
197
    s1->ctx_flags |= AVFMTCTX_NOHEADER;
198

    
199
    st = av_new_stream(s1, 0);
200
    if (!st) {
201
        return AVERROR(ENOMEM);
202
    }
203

    
204
    av_strlcpy(s->path, s1->filename, sizeof(s->path));
205
    s->img_number = 0;
206
    s->img_count = 0;
207

    
208
    /* find format */
209
    if (s1->iformat->flags & AVFMT_NOFILE)
210
        s->is_pipe = 0;
211
    else{
212
        s->is_pipe = 1;
213
        st->need_parsing = AVSTREAM_PARSE_FULL;
214
    }
215

    
216
    if (!ap->time_base.num) {
217
        av_set_pts_info(st, 60, 1, 25);
218
    } else {
219
        av_set_pts_info(st, 60, ap->time_base.num, ap->time_base.den);
220
    }
221

    
222
    if(ap->width && ap->height){
223
        st->codec->width = ap->width;
224
        st->codec->height= ap->height;
225
    }
226

    
227
    if (!s->is_pipe) {
228
        if (find_image_range(&first_index, &last_index, s->path) < 0)
229
            return AVERROR(ENOENT);
230
        s->img_first = first_index;
231
        s->img_last = last_index;
232
        s->img_number = first_index;
233
        /* compute duration */
234
        st->start_time = 0;
235
        st->duration = last_index - first_index + 1;
236
    }
237

    
238
    if(s1->video_codec_id){
239
        st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
240
        st->codec->codec_id = s1->video_codec_id;
241
    }else if(s1->audio_codec_id){
242
        st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
243
        st->codec->codec_id = s1->audio_codec_id;
244
    }else{
245
        const char *str= strrchr(s->path, '.');
246
        s->split_planes = str && !strcasecmp(str + 1, "y");
247
        st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
248
        st->codec->codec_id = av_str2id(img_tags, s->path);
249
    }
250
    if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO && ap->pix_fmt != PIX_FMT_NONE)
251
        st->codec->pix_fmt = ap->pix_fmt;
252

    
253
    return 0;
254
}
255

    
256
static int read_packet(AVFormatContext *s1, AVPacket *pkt)
257
{
258
    VideoData *s = s1->priv_data;
259
    char filename[1024];
260
    int i;
261
    int size[3]={0}, ret[3]={0};
262
    AVIOContext *f[3];
263
    AVCodecContext *codec= s1->streams[0]->codec;
264

    
265
    if (!s->is_pipe) {
266
        /* loop over input */
267
        if (s1->loop_input && s->img_number > s->img_last) {
268
            s->img_number = s->img_first;
269
        }
270
        if (s->img_number > s->img_last)
271
            return AVERROR_EOF;
272
        if (av_get_frame_filename(filename, sizeof(filename),
273
                                  s->path, s->img_number)<0 && s->img_number > 1)
274
            return AVERROR(EIO);
275
        for(i=0; i<3; i++){
276
            if (avio_open(&f[i], filename, URL_RDONLY) < 0) {
277
                if(i==1)
278
                    break;
279
                av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",filename);
280
                return AVERROR(EIO);
281
            }
282
            size[i]= avio_size(f[i]);
283

    
284
            if(!s->split_planes)
285
                break;
286
            filename[ strlen(filename) - 1 ]= 'U' + i;
287
        }
288

    
289
        if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width)
290
            infer_size(&codec->width, &codec->height, size[0]);
291
    } else {
292
        f[0] = s1->pb;
293
        if (url_feof(f[0]))
294
            return AVERROR(EIO);
295
        size[0]= 4096;
296
    }
297

    
298
    av_new_packet(pkt, size[0] + size[1] + size[2]);
299
    pkt->stream_index = 0;
300
    pkt->flags |= AV_PKT_FLAG_KEY;
301

    
302
    pkt->size= 0;
303
    for(i=0; i<3; i++){
304
        if(size[i]){
305
            ret[i]= avio_read(f[i], pkt->data + pkt->size, size[i]);
306
            if (!s->is_pipe)
307
                avio_close(f[i]);
308
            if(ret[i]>0)
309
                pkt->size += ret[i];
310
        }
311
    }
312

    
313
    if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
314
        av_free_packet(pkt);
315
        return AVERROR(EIO); /* signal EOF */
316
    } else {
317
        s->img_count++;
318
        s->img_number++;
319
        return 0;
320
    }
321
}
322

    
323
#if CONFIG_IMAGE2_MUXER || CONFIG_IMAGE2PIPE_MUXER
324
/******************************************************/
325
/* image output */
326

    
327
static int write_header(AVFormatContext *s)
328
{
329
    VideoData *img = s->priv_data;
330
    const char *str;
331

    
332
    img->img_number = 1;
333
    av_strlcpy(img->path, s->filename, sizeof(img->path));
334

    
335
    /* find format */
336
    if (s->oformat->flags & AVFMT_NOFILE)
337
        img->is_pipe = 0;
338
    else
339
        img->is_pipe = 1;
340

    
341
    str = strrchr(img->path, '.');
342
    img->split_planes = str && !strcasecmp(str + 1, "y");
343
    return 0;
344
}
345

    
346
static int write_packet(AVFormatContext *s, AVPacket *pkt)
347
{
348
    VideoData *img = s->priv_data;
349
    AVIOContext *pb[3];
350
    char filename[1024];
351
    AVCodecContext *codec= s->streams[ pkt->stream_index ]->codec;
352
    int i;
353

    
354
    if (!img->is_pipe) {
355
        if (av_get_frame_filename(filename, sizeof(filename),
356
                                  img->path, img->img_number) < 0 && img->img_number>1) {
357
            av_log(s, AV_LOG_ERROR,
358
                   "Could not get frame filename number %d from pattern '%s'\n",
359
                   img->img_number, img->path);
360
            return AVERROR(EIO);
361
        }
362
        for(i=0; i<3; i++){
363
            if (avio_open(&pb[i], filename, URL_WRONLY) < 0) {
364
                av_log(s, AV_LOG_ERROR, "Could not open file : %s\n",filename);
365
                return AVERROR(EIO);
366
            }
367

    
368
            if(!img->split_planes)
369
                break;
370
            filename[ strlen(filename) - 1 ]= 'U' + i;
371
        }
372
    } else {
373
        pb[0] = s->pb;
374
    }
375

    
376
    if(img->split_planes){
377
        int ysize = codec->width * codec->height;
378
        avio_write(pb[0], pkt->data        , ysize);
379
        avio_write(pb[1], pkt->data + ysize, (pkt->size - ysize)/2);
380
        avio_write(pb[2], pkt->data + ysize +(pkt->size - ysize)/2, (pkt->size - ysize)/2);
381
        avio_flush(pb[1]);
382
        avio_flush(pb[2]);
383
        avio_close(pb[1]);
384
        avio_close(pb[2]);
385
    }else{
386
        if(av_str2id(img_tags, s->filename) == CODEC_ID_JPEG2000){
387
            AVStream *st = s->streams[0];
388
            if(st->codec->extradata_size > 8 &&
389
               AV_RL32(st->codec->extradata+4) == MKTAG('j','p','2','h')){
390
                if(pkt->size < 8 || AV_RL32(pkt->data+4) != MKTAG('j','p','2','c'))
391
                    goto error;
392
                avio_wb32(pb[0], 12);
393
                ffio_wfourcc(pb[0], "jP  ");
394
                avio_wb32(pb[0], 0x0D0A870A); // signature
395
                avio_wb32(pb[0], 20);
396
                ffio_wfourcc(pb[0], "ftyp");
397
                ffio_wfourcc(pb[0], "jp2 ");
398
                avio_wb32(pb[0], 0);
399
                ffio_wfourcc(pb[0], "jp2 ");
400
                avio_write(pb[0], st->codec->extradata, st->codec->extradata_size);
401
            }else if(pkt->size < 8 ||
402
                     (!st->codec->extradata_size &&
403
                      AV_RL32(pkt->data+4) != MKTAG('j','P',' ',' '))){ // signature
404
            error:
405
                av_log(s, AV_LOG_ERROR, "malformated jpeg2000 codestream\n");
406
                return -1;
407
            }
408
        }
409
        avio_write(pb[0], pkt->data, pkt->size);
410
    }
411
    avio_flush(pb[0]);
412
    if (!img->is_pipe) {
413
        avio_close(pb[0]);
414
    }
415

    
416
    img->img_number++;
417
    return 0;
418
}
419

    
420
#endif /* CONFIG_IMAGE2_MUXER || CONFIG_IMAGE2PIPE_MUXER */
421

    
422
/* input */
423
#if CONFIG_IMAGE2_DEMUXER
424
AVInputFormat ff_image2_demuxer = {
425
    .name           = "image2",
426
    .long_name      = NULL_IF_CONFIG_SMALL("image2 sequence"),
427
    .priv_data_size = sizeof(VideoData),
428
    .read_probe     = read_probe,
429
    .read_header    = read_header,
430
    .read_packet    = read_packet,
431
    .flags          = AVFMT_NOFILE,
432
};
433
#endif
434
#if CONFIG_IMAGE2PIPE_DEMUXER
435
AVInputFormat ff_image2pipe_demuxer = {
436
    .name           = "image2pipe",
437
    .long_name      = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
438
    .priv_data_size = sizeof(VideoData),
439
    .read_header    = read_header,
440
    .read_packet    = read_packet,
441
};
442
#endif
443

    
444
/* output */
445
#if CONFIG_IMAGE2_MUXER
446
AVOutputFormat ff_image2_muxer = {
447
    .name           = "image2",
448
    .long_name      = NULL_IF_CONFIG_SMALL("image2 sequence"),
449
    .extensions     = "bmp,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
450
                      "ppm,sgi,tga,tif,tiff,jp2",
451
    .priv_data_size = sizeof(VideoData),
452
    .video_codec    = CODEC_ID_MJPEG,
453
    .write_header   = write_header,
454
    .write_packet   = write_packet,
455
    .flags          = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS | AVFMT_NOFILE
456
};
457
#endif
458
#if CONFIG_IMAGE2PIPE_MUXER
459
AVOutputFormat ff_image2pipe_muxer = {
460
    .name           = "image2pipe",
461
    .long_name      = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
462
    .priv_data_size = sizeof(VideoData),
463
    .video_codec    = CODEC_ID_MJPEG,
464
    .write_header   = write_header,
465
    .write_packet   = write_packet,
466
    .flags          = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS
467
};
468
#endif