]> git.sesse.net Git - ffmpeg/blob - libavformat/img2dec.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / img2dec.c
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/avstring.h"
24 #include "libavutil/log.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/parseutils.h"
28 #include "avformat.h"
29 #include "internal.h"
30 #if HAVE_GLOB
31 #include <glob.h>
32
33 /* Locally define as 0 (bitwise-OR no-op) any missing glob options that
34    are non-posix glibc/bsd extensions. */
35 #ifndef GLOB_NOMAGIC
36 #define GLOB_NOMAGIC 0
37 #endif
38 #ifndef GLOB_BRACE
39 #define GLOB_BRACE 0
40 #endif
41
42 #endif /* HAVE_GLOB */
43
44 typedef struct {
45     const AVClass *class;  /**< Class for private options. */
46     int img_first;
47     int img_last;
48     int img_number;
49     int img_count;
50     int is_pipe;
51     int split_planes;       /**< use independent file for each Y, U, V plane */
52     char path[1024];
53     char *pixel_format;     /**< Set by a private option. */
54     char *video_size;       /**< Set by a private option. */
55     char *framerate;        /**< Set by a private option. */
56     int loop;
57     int use_glob;
58 #if HAVE_GLOB
59     glob_t globstate;
60 #endif
61 } VideoDemuxData;
62
63 static const int sizes[][2] = {
64     { 640, 480 },
65     { 720, 480 },
66     { 720, 576 },
67     { 352, 288 },
68     { 352, 240 },
69     { 160, 128 },
70     { 512, 384 },
71     { 640, 352 },
72     { 640, 240 },
73 };
74
75 static int infer_size(int *width_ptr, int *height_ptr, int size)
76 {
77     int i;
78
79     for(i=0;i<FF_ARRAY_ELEMS(sizes);i++) {
80         if ((sizes[i][0] * sizes[i][1]) == size) {
81             *width_ptr = sizes[i][0];
82             *height_ptr = sizes[i][1];
83             return 0;
84         }
85     }
86     return -1;
87 }
88
89 static int is_glob(const char *path)
90 {
91 #if HAVE_GLOB
92     size_t span = 0;
93     const char *p = path;
94
95     while (p = strchr(p, '%')) {
96         if (*(++p) == '%') {
97             ++p;
98             continue;
99         }
100         if (span = strspn(p, "*?[]{}"))
101             break;
102     }
103     /* Did we hit a glob char or get to the end? */
104     return span != 0;
105 #else
106     return 0;
107 #endif
108 }
109
110 /* return -1 if no image found */
111 static int find_image_range(int *pfirst_index, int *plast_index,
112                             const char *path)
113 {
114     char buf[1024];
115     int range, last_index, range1, first_index;
116
117     /* find the first image */
118     for(first_index = 0; first_index < 5; first_index++) {
119         if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0){
120             *pfirst_index =
121             *plast_index = 1;
122             if (avio_check(buf, AVIO_FLAG_READ) > 0)
123                 return 0;
124             return -1;
125         }
126         if (avio_check(buf, AVIO_FLAG_READ) > 0)
127             break;
128     }
129     if (first_index == 5)
130         goto fail;
131
132     /* find the last image */
133     last_index = first_index;
134     for(;;) {
135         range = 0;
136         for(;;) {
137             if (!range)
138                 range1 = 1;
139             else
140                 range1 = 2 * range;
141             if (av_get_frame_filename(buf, sizeof(buf), path,
142                                       last_index + range1) < 0)
143                 goto fail;
144             if (avio_check(buf, AVIO_FLAG_READ) <= 0)
145                 break;
146             range = range1;
147             /* just in case... */
148             if (range >= (1 << 30))
149                 goto fail;
150         }
151         /* we are sure than image last_index + range exists */
152         if (!range)
153             break;
154         last_index += range;
155     }
156     *pfirst_index = first_index;
157     *plast_index = last_index;
158     return 0;
159  fail:
160     return -1;
161 }
162
163
164 static int read_probe(AVProbeData *p)
165 {
166     if (p->filename && ff_guess_image2_codec(p->filename)) {
167         if (av_filename_number_test(p->filename))
168             return AVPROBE_SCORE_MAX;
169         else if (is_glob(p->filename))
170             return AVPROBE_SCORE_MAX;
171         else
172             return AVPROBE_SCORE_MAX/2;
173     }
174     return 0;
175 }
176
177 static int read_header(AVFormatContext *s1)
178 {
179     VideoDemuxData *s = s1->priv_data;
180     int first_index, last_index, ret = 0;
181     int width = 0, height = 0;
182     AVStream *st;
183     enum PixelFormat pix_fmt = PIX_FMT_NONE;
184     AVRational framerate;
185
186     s1->ctx_flags |= AVFMTCTX_NOHEADER;
187
188     st = avformat_new_stream(s1, NULL);
189     if (!st) {
190         return AVERROR(ENOMEM);
191     }
192
193     if (s->pixel_format && (pix_fmt = av_get_pix_fmt(s->pixel_format)) == PIX_FMT_NONE) {
194         av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n", s->pixel_format);
195         return AVERROR(EINVAL);
196     }
197     if (s->video_size && (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
198         av_log(s, AV_LOG_ERROR, "Could not parse video size: %s.\n", s->video_size);
199         return ret;
200     }
201     if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
202         av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s->framerate);
203         return ret;
204     }
205
206     av_strlcpy(s->path, s1->filename, sizeof(s->path));
207     s->img_number = 0;
208     s->img_count = 0;
209
210     /* find format */
211     if (s1->iformat->flags & AVFMT_NOFILE)
212         s->is_pipe = 0;
213     else{
214         s->is_pipe = 1;
215         st->need_parsing = AVSTREAM_PARSE_FULL;
216     }
217
218     avpriv_set_pts_info(st, 60, framerate.den, framerate.num);
219
220     if (width && height) {
221         st->codec->width  = width;
222         st->codec->height = height;
223     }
224
225     if (!s->is_pipe) {
226         s->use_glob = is_glob(s->path);
227         if (s->use_glob) {
228 #if HAVE_GLOB
229             char *p = s->path, *q, *dup;
230             int gerr;
231
232             dup = q = av_strdup(p);
233             while (*q) {
234                 /* Do we have room for the next char and a \ insertion? */
235                 if ((p - s->path) >= (sizeof(s->path) - 2))
236                   break;
237                 if (*q == '%' && strspn(q + 1, "%*?[]{}"))
238                     ++q;
239                 else if (strspn(q, "\\*?[]{}"))
240                     *p++ = '\\';
241                 *p++ = *q++;
242             }
243             *p = 0;
244             av_free(dup);
245
246             gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
247             if (gerr != 0) {
248                 return AVERROR(ENOENT);
249             }
250             first_index = 0;
251             last_index = s->globstate.gl_pathc - 1;
252 #endif
253         } else {
254         if (find_image_range(&first_index, &last_index, s->path) < 0)
255             return AVERROR(ENOENT);
256         }
257         s->img_first = first_index;
258         s->img_last = last_index;
259         s->img_number = first_index;
260         /* compute duration */
261         st->start_time = 0;
262         st->duration = last_index - first_index + 1;
263     }
264
265     if(s1->video_codec_id){
266         st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
267         st->codec->codec_id = s1->video_codec_id;
268     }else if(s1->audio_codec_id){
269         st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
270         st->codec->codec_id = s1->audio_codec_id;
271     }else{
272         const char *str= strrchr(s->path, '.');
273         s->split_planes = str && !av_strcasecmp(str + 1, "y");
274         st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
275         st->codec->codec_id = ff_guess_image2_codec(s->path);
276         if (st->codec->codec_id == CODEC_ID_LJPEG)
277             st->codec->codec_id = CODEC_ID_MJPEG;
278     }
279     if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO && pix_fmt != PIX_FMT_NONE)
280         st->codec->pix_fmt = pix_fmt;
281
282     return 0;
283 }
284
285 static int read_packet(AVFormatContext *s1, AVPacket *pkt)
286 {
287     VideoDemuxData *s = s1->priv_data;
288     char filename_bytes[1024];
289     char *filename = filename_bytes;
290     int i;
291     int size[3]={0}, ret[3]={0};
292     AVIOContext *f[3];
293     AVCodecContext *codec= s1->streams[0]->codec;
294
295     if (!s->is_pipe) {
296         /* loop over input */
297         if (s->loop && s->img_number > s->img_last) {
298             s->img_number = s->img_first;
299         }
300         if (s->img_number > s->img_last)
301             return AVERROR_EOF;
302         if (s->use_glob) {
303 #if HAVE_GLOB
304             filename = s->globstate.gl_pathv[s->img_number];
305 #endif
306         } else {
307         if (av_get_frame_filename(filename_bytes, sizeof(filename_bytes),
308                                   s->path, s->img_number)<0 && s->img_number > 1)
309             return AVERROR(EIO);
310         }
311         for(i=0; i<3; i++){
312             if (avio_open2(&f[i], filename, AVIO_FLAG_READ,
313                            &s1->interrupt_callback, NULL) < 0) {
314                 if(i==1)
315                     break;
316                 av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",filename);
317                 return AVERROR(EIO);
318             }
319             size[i]= avio_size(f[i]);
320
321             if(!s->split_planes)
322                 break;
323             filename[ strlen(filename) - 1 ]= 'U' + i;
324         }
325
326         if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width)
327             infer_size(&codec->width, &codec->height, size[0]);
328     } else {
329         f[0] = s1->pb;
330         if (url_feof(f[0]))
331             return AVERROR(EIO);
332         size[0]= 4096;
333     }
334
335     av_new_packet(pkt, size[0] + size[1] + size[2]);
336     pkt->stream_index = 0;
337     pkt->flags |= AV_PKT_FLAG_KEY;
338
339     pkt->size= 0;
340     for(i=0; i<3; i++){
341         if(size[i]){
342             ret[i]= avio_read(f[i], pkt->data + pkt->size, size[i]);
343             if (!s->is_pipe)
344                 avio_close(f[i]);
345             if(ret[i]>0)
346                 pkt->size += ret[i];
347         }
348     }
349
350     if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
351         av_free_packet(pkt);
352         return AVERROR(EIO); /* signal EOF */
353     } else {
354         s->img_count++;
355         s->img_number++;
356         return 0;
357     }
358 }
359
360 static int read_close(struct AVFormatContext* s1)
361 {
362     VideoDemuxData *s = s1->priv_data;
363 #if HAVE_GLOB
364     if (s->use_glob) {
365         globfree(&s->globstate);
366     }
367 #endif
368     return 0;
369 }
370
371 #define OFFSET(x) offsetof(VideoDemuxData, x)
372 #define DEC AV_OPT_FLAG_DECODING_PARAM
373 static const AVOption options[] = {
374     { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
375     { "video_size",   "", OFFSET(video_size),   AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
376     { "framerate",    "", OFFSET(framerate),    AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
377     { "loop",         "", OFFSET(loop),         AV_OPT_TYPE_INT,    {.dbl = 0},    0, 1, DEC },
378     { NULL },
379 };
380
381 #if CONFIG_IMAGE2_DEMUXER
382 static const AVClass img2_class = {
383     .class_name = "image2 demuxer",
384     .item_name  = av_default_item_name,
385     .option     = options,
386     .version    = LIBAVUTIL_VERSION_INT,
387 };
388 AVInputFormat ff_image2_demuxer = {
389     .name           = "image2",
390     .long_name      = NULL_IF_CONFIG_SMALL("image2 sequence"),
391     .priv_data_size = sizeof(VideoDemuxData),
392     .read_probe     = read_probe,
393     .read_header    = read_header,
394     .read_packet    = read_packet,
395     .read_close     = read_close,
396     .flags          = AVFMT_NOFILE,
397     .priv_class     = &img2_class,
398 };
399 #endif
400 #if CONFIG_IMAGE2PIPE_DEMUXER
401 static const AVClass img2pipe_class = {
402     .class_name = "image2pipe demuxer",
403     .item_name  = av_default_item_name,
404     .option     = options,
405     .version    = LIBAVUTIL_VERSION_INT,
406 };
407 AVInputFormat ff_image2pipe_demuxer = {
408     .name           = "image2pipe",
409     .long_name      = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
410     .priv_data_size = sizeof(VideoDemuxData),
411     .read_header    = read_header,
412     .read_packet    = read_packet,
413     .priv_class     = &img2pipe_class,
414 };
415 #endif