Statistics
| Branch: | Revision:

ffmpeg / libavformat / img2.c @ 17929c04

History | View | Annotate | Download (10.7 KB)

1 03cfe134 Michael Niedermayer
/*
2
 * Image format
3
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
4
 * Copyright (c) 2004 Michael Niedermayer
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20
#include "avformat.h"
21
22 b339fb00 Michael Niedermayer
/* XXX: this is a hack */
23
extern int loop_input;
24
25 03cfe134 Michael Niedermayer
typedef struct {
26
    int img_first;
27
    int img_last;
28
    int img_number;
29
    int img_count;
30
    int is_pipe;
31
    char path[1024];
32
} VideoData;
33
34
typedef struct {
35
    enum CodecID id;
36
    const char *str;
37
} IdStrMap;
38
39
static const IdStrMap img_tags[] = {
40
    { CODEC_ID_MJPEG     , "jpeg"},
41
    { CODEC_ID_MJPEG     , "jpg"},
42
    { CODEC_ID_LJPEG     , "ljpg"},
43 4eff7cf4 Michael Niedermayer
    { CODEC_ID_PNG       , "png"},
44 5b6d5596 Michael Niedermayer
    { CODEC_ID_PPM       , "ppm"},
45
    { CODEC_ID_PGM       , "pgm"},
46
    { CODEC_ID_PGMYUV    , "pgmyuv"},
47
    { CODEC_ID_PBM       , "pbm"},
48
    { CODEC_ID_PAM       , "pam"},
49 03cfe134 Michael Niedermayer
    { CODEC_ID_MPEG1VIDEO, "mpg1-img"},
50
    { CODEC_ID_MPEG2VIDEO, "mpg2-img"},
51
    { CODEC_ID_MPEG4     , "mpg4-img"},
52
    { CODEC_ID_FFV1      , "ffv1-img"},
53 55cf1959 Michael Niedermayer
    { CODEC_ID_RAWVIDEO  , "y"},
54 03cfe134 Michael Niedermayer
    {0, NULL}
55
};
56
57 55cf1959 Michael Niedermayer
static int sizes[][2] = {
58
    { 640, 480 },
59
    { 720, 480 },
60
    { 720, 576 },
61
    { 352, 288 },
62
    { 352, 240 },
63
    { 160, 128 },
64
    { 512, 384 },
65
    { 640, 352 },
66
    { 640, 240 },
67
};
68
69
static int infer_size(int *width_ptr, int *height_ptr, int size)
70
{
71
    int i;
72
73
    for(i=0;i<sizeof(sizes)/sizeof(sizes[0]);i++) {
74
        if ((sizes[i][0] * sizes[i][1]) == size) {
75
            *width_ptr = sizes[i][0];
76
            *height_ptr = sizes[i][1];
77
            return 0;
78
        }
79
    }
80
    return -1;
81
}
82 03cfe134 Michael Niedermayer
static enum CodecID av_str2id(const IdStrMap *tags, const char *str)
83
{
84 3ed02129 Michael Niedermayer
    str= strrchr(str, '.');
85
    if(!str) return CODEC_ID_NONE;
86
    str++;
87 03cfe134 Michael Niedermayer
88
    while (tags->id) {
89
        int i;
90
        for(i=0; toupper(tags->str[i]) == toupper(str[i]); i++){
91
            if(tags->str[i]==0 && str[i]==0)
92
                return tags->id;
93
        }
94
95
        tags++;
96
    }
97
    return CODEC_ID_NONE;
98
}
99
100
/* return -1 if no image found */
101
static int find_image_range(int *pfirst_index, int *plast_index, 
102
                            const char *path)
103
{
104
    char buf[1024];
105
    int range, last_index, range1, first_index;
106
107
    /* find the first image */
108
    for(first_index = 0; first_index < 5; first_index++) {
109 61fb3183 Michael Niedermayer
        if (get_frame_filename(buf, sizeof(buf), path, first_index) < 0){
110
            *pfirst_index = 
111
            *plast_index = 1;
112
            return 0;
113
        }
114 03cfe134 Michael Niedermayer
        if (url_exist(buf))
115
            break;
116
    }
117
    if (first_index == 5)
118
        goto fail;
119
    
120
    /* find the last image */
121
    last_index = first_index;
122
    for(;;) {
123
        range = 0;
124
        for(;;) {
125
            if (!range)
126
                range1 = 1;
127
            else
128
                range1 = 2 * range;
129
            if (get_frame_filename(buf, sizeof(buf), path, 
130
                                   last_index + range1) < 0)
131
                goto fail;
132
            if (!url_exist(buf))
133
                break;
134
            range = range1;
135
            /* just in case... */
136
            if (range >= (1 << 30))
137
                goto fail;
138
        }
139
        /* we are sure than image last_index + range exists */
140
        if (!range)
141
            break;
142
        last_index += range;
143
    }
144
    *pfirst_index = first_index;
145
    *plast_index = last_index;
146
    return 0;
147
 fail:
148
    return -1;
149
}
150
151
152
static int image_probe(AVProbeData *p)
153
{
154
    if (filename_number_test(p->filename) >= 0 && av_str2id(img_tags, p->filename))
155
        return AVPROBE_SCORE_MAX;
156
    else
157
        return 0;
158
}
159
160 5b6d5596 Michael Niedermayer
enum CodecID av_guess_image2_codec(const char *filename){
161
    return av_str2id(img_tags, filename);
162
}
163
164 03cfe134 Michael Niedermayer
static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
165
{
166
    VideoData *s = s1->priv_data;
167
    int first_index, last_index;
168
    AVStream *st;
169
170
    s1->ctx_flags |= AVFMTCTX_NOHEADER;
171
172
    st = av_new_stream(s1, 0);
173
    if (!st) {
174
        return -ENOMEM;
175
    }
176
177 568e18b1 Michael Niedermayer
    pstrcpy(s->path, sizeof(s->path), s1->filename);
178 03cfe134 Michael Niedermayer
    s->img_number = 0;
179
    s->img_count = 0;
180
    
181
    /* find format */
182
    if (s1->iformat->flags & AVFMT_NOFILE)
183
        s->is_pipe = 0;
184 146210ca Michael Niedermayer
    else{
185 03cfe134 Michael Niedermayer
        s->is_pipe = 1;
186 146210ca Michael Niedermayer
        st->need_parsing= 1;
187
    }
188 03cfe134 Michael Niedermayer
        
189
    if (!ap || !ap->frame_rate) {
190
        st->codec.frame_rate      = 25;
191
        st->codec.frame_rate_base = 1;
192
    } else {
193
        st->codec.frame_rate      = ap->frame_rate;
194
        st->codec.frame_rate_base = ap->frame_rate_base;
195
    }
196
    
197 55cf1959 Michael Niedermayer
    if(ap && ap->width && ap->height){
198
        st->codec.width = ap->width;
199
        st->codec.height= ap->height;
200
    }
201
    
202 03cfe134 Michael Niedermayer
    if (!s->is_pipe) {
203
        if (find_image_range(&first_index, &last_index, s->path) < 0)
204 ae895a40 Michael Niedermayer
            return AVERROR_IO;
205 03cfe134 Michael Niedermayer
        s->img_first = first_index;
206
        s->img_last = last_index;
207
        s->img_number = first_index;
208
        /* compute duration */
209
        st->start_time = 0;
210
        st->duration = ((int64_t)AV_TIME_BASE * 
211
                        (last_index - first_index + 1) * 
212
                        st->codec.frame_rate_base) / st->codec.frame_rate;
213
    }
214
    
215 5b6d5596 Michael Niedermayer
    if(ap->video_codec_id){
216
        st->codec.codec_type = CODEC_TYPE_VIDEO;
217
        st->codec.codec_id = ap->video_codec_id;
218
    }else if(ap->audio_codec_id){
219
        st->codec.codec_type = CODEC_TYPE_AUDIO;
220
        st->codec.codec_id = ap->audio_codec_id;
221
    }else{
222
        st->codec.codec_type = CODEC_TYPE_VIDEO;
223
        st->codec.codec_id = av_str2id(img_tags, s->path);
224
    }
225 03cfe134 Michael Niedermayer
226
    return 0;
227
}
228
229
static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
230
{
231
    VideoData *s = s1->priv_data;
232
    char filename[1024];
233 55cf1959 Michael Niedermayer
    int i;
234
    int size[3]={0}, ret[3]={0};
235
    ByteIOContext f1[3], *f[3]= {&f1[0], &f1[1], &f1[2]};
236
    AVCodecContext *codec= &s1->streams[0]->codec;
237 03cfe134 Michael Niedermayer
238
    if (!s->is_pipe) {
239
        /* loop over input */
240 b339fb00 Michael Niedermayer
        if (loop_input && s->img_number > s->img_last) {
241 03cfe134 Michael Niedermayer
            s->img_number = s->img_first;
242 b339fb00 Michael Niedermayer
        }
243 03cfe134 Michael Niedermayer
        if (get_frame_filename(filename, sizeof(filename),
244 61fb3183 Michael Niedermayer
                               s->path, s->img_number)<0 && s->img_number > 1)
245 03cfe134 Michael Niedermayer
            return AVERROR_IO;
246 55cf1959 Michael Niedermayer
        for(i=0; i<3; i++){
247
            if (url_fopen(f[i], filename, URL_RDONLY) < 0)
248
                return AVERROR_IO;
249
            size[i]= url_filesize(url_fileno(f[i]));
250
            
251
            if(codec->codec_id != CODEC_ID_RAWVIDEO)
252
                break;
253
            filename[ strlen(filename) - 1 ]= 'U' + i;
254
        }
255
        
256
        if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width)
257
            infer_size(&codec->width, &codec->height, size[0]);
258 03cfe134 Michael Niedermayer
    } else {
259 55cf1959 Michael Niedermayer
        f[0] = &s1->pb;
260
        if (url_feof(f[0]))
261 03cfe134 Michael Niedermayer
            return AVERROR_IO;
262 55cf1959 Michael Niedermayer
        size[0]= 4096;
263 03cfe134 Michael Niedermayer
    }
264
265 55cf1959 Michael Niedermayer
    av_new_packet(pkt, size[0] + size[1] + size[2]);
266 03cfe134 Michael Niedermayer
    pkt->stream_index = 0;
267
    pkt->flags |= PKT_FLAG_KEY;
268
269 55cf1959 Michael Niedermayer
    pkt->size= 0;
270
    for(i=0; i<3; i++){
271
        if(size[i]){
272
            ret[i]= get_buffer(f[i], pkt->data + pkt->size, size[i]);
273
            if (!s->is_pipe)
274
                url_fclose(f[i]);
275
            if(ret[i]>0)
276
                pkt->size += ret[i];
277
        }
278 03cfe134 Michael Niedermayer
    }
279
280 55cf1959 Michael Niedermayer
    if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
281 03cfe134 Michael Niedermayer
        av_free_packet(pkt);
282
        return AVERROR_IO; /* signal EOF */
283
    } else {
284
        s->img_count++;
285
        s->img_number++;
286
        return 0;
287
    }
288
}
289
290
static int img_read_close(AVFormatContext *s1)
291
{
292
    return 0;
293
}
294
295
/******************************************************/
296
/* image output */
297
298
static int img_write_header(AVFormatContext *s)
299
{
300
    VideoData *img = s->priv_data;
301
302
    img->img_number = 1;
303 568e18b1 Michael Niedermayer
    pstrcpy(img->path, sizeof(img->path), s->filename);
304 03cfe134 Michael Niedermayer
305
    /* find format */
306
    if (s->oformat->flags & AVFMT_NOFILE)
307
        img->is_pipe = 0;
308
    else
309
        img->is_pipe = 1;
310
        
311
    return 0;
312
}
313
314
static int img_write_packet(AVFormatContext *s, AVPacket *pkt)
315
{
316
    VideoData *img = s->priv_data;
317 55cf1959 Michael Niedermayer
    ByteIOContext pb1[3], *pb[3]= {&pb1[0], &pb1[1], &pb1[2]};
318 03cfe134 Michael Niedermayer
    char filename[1024];
319 55cf1959 Michael Niedermayer
    AVCodecContext *codec= &s->streams[ pkt->stream_index ]->codec;
320
    int i;
321 03cfe134 Michael Niedermayer
322
    if (!img->is_pipe) {
323
        if (get_frame_filename(filename, sizeof(filename), 
324 61fb3183 Michael Niedermayer
                               img->path, img->img_number) < 0 && img->img_number>1)
325 03cfe134 Michael Niedermayer
            return AVERROR_IO;
326 55cf1959 Michael Niedermayer
        for(i=0; i<3; i++){
327
            if (url_fopen(pb[i], filename, URL_WRONLY) < 0)
328
                return AVERROR_IO;
329
        
330
            if(codec->codec_id != CODEC_ID_RAWVIDEO)
331
                break;
332
            filename[ strlen(filename) - 1 ]= 'U' + i;
333
        }
334 03cfe134 Michael Niedermayer
    } else {
335 55cf1959 Michael Niedermayer
        pb[0] = &s->pb;
336 03cfe134 Michael Niedermayer
    }
337 55cf1959 Michael Niedermayer
    
338
    if(codec->codec_id == CODEC_ID_RAWVIDEO){
339
        int size = (codec->width * codec->height)>>2;
340
        put_buffer(pb[0], pkt->data         , 4*size);
341
        put_buffer(pb[1], pkt->data + 4*size,   size);
342
        put_buffer(pb[2], pkt->data + 5*size,   size);
343
        put_flush_packet(pb[1]);
344
        put_flush_packet(pb[2]);
345
        url_fclose(pb[1]);
346
        url_fclose(pb[2]);
347
    }else{
348
        put_buffer(pb[0], pkt->data, pkt->size);
349
    }
350
    put_flush_packet(pb[0]);
351 03cfe134 Michael Niedermayer
    if (!img->is_pipe) {
352 55cf1959 Michael Niedermayer
        url_fclose(pb[0]);
353 03cfe134 Michael Niedermayer
    }
354
355
    img->img_number++;
356
    return 0;
357
}
358
359
static int img_write_trailer(AVFormatContext *s)
360
{
361
    return 0;
362
}
363
364
/* input */
365
366
static AVInputFormat image2_iformat = {
367
    "image2",
368
    "image2 sequence",
369
    sizeof(VideoData),
370
    image_probe,
371
    img_read_header,
372
    img_read_packet,
373
    img_read_close,
374
    NULL,
375
    NULL,
376 61fb3183 Michael Niedermayer
    AVFMT_NOFILE,
377 03cfe134 Michael Niedermayer
};
378
379
static AVInputFormat image2pipe_iformat = {
380
    "image2pipe",
381
    "piped image2 sequence",
382
    sizeof(VideoData),
383
    NULL, /* no probe */
384
    img_read_header,
385
    img_read_packet,
386
    img_read_close,
387
    NULL,
388
};
389
390
391
/* output */
392
393
static AVOutputFormat image2_oformat = {
394
    "image2",
395
    "image2 sequence",
396
    "",
397
    "",
398
    sizeof(VideoData),
399
    CODEC_ID_NONE,
400
    CODEC_ID_MJPEG,
401
    img_write_header,
402
    img_write_packet,
403
    img_write_trailer,
404 61fb3183 Michael Niedermayer
    AVFMT_NOFILE,
405 03cfe134 Michael Niedermayer
};
406
407
static AVOutputFormat image2pipe_oformat = {
408
    "image2pipe",
409
    "piped image2 sequence",
410
    "",
411
    "",
412
    sizeof(VideoData),
413
    CODEC_ID_NONE,
414
    CODEC_ID_MJPEG,
415
    img_write_header,
416
    img_write_packet,
417
    img_write_trailer,
418
};
419
420
int img2_init(void)
421
{
422
    av_register_input_format(&image2_iformat);
423
    av_register_output_format(&image2_oformat);
424
425
    av_register_input_format(&image2pipe_iformat);
426
    av_register_output_format(&image2pipe_oformat);
427
    
428
    return 0;
429
}