3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 * Copyright (c) 2004 Michael Niedermayer
6 * This file is part of FFmpeg.
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.
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.
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
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"
33 /* Locally define as 0 (bitwise-OR no-op) any missing glob options that
34 are non-posix glibc/bsd extensions. */
36 #define GLOB_NOMAGIC 0
42 #endif /* HAVE_GLOB */
45 const AVClass *class; /**< Class for private options. */
51 int split_planes; /**< use independent file for each Y, U, V plane */
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. */
64 static const int sizes[][2] = {
76 static int infer_size(int *width_ptr, int *height_ptr, int size)
80 for(i=0;i<FF_ARRAY_ELEMS(sizes);i++) {
81 if ((sizes[i][0] * sizes[i][1]) == size) {
82 *width_ptr = sizes[i][0];
83 *height_ptr = sizes[i][1];
90 static int is_glob(const char *path)
96 while (p = strchr(p, '%')) {
101 if (span = strspn(p, "*?[]{}"))
104 /* Did we hit a glob char or get to the end? */
111 /* return -1 if no image found */
112 static int find_image_range(int *pfirst_index, int *plast_index,
113 const char *path, int max_start)
116 int range, last_index, range1, first_index;
118 /* find the first image */
119 for (first_index = max_start; first_index < max_start + 5; first_index++) {
120 if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0){
123 if (avio_check(buf, AVIO_FLAG_READ) > 0)
127 if (avio_check(buf, AVIO_FLAG_READ) > 0)
130 if (first_index == 5)
133 /* find the last image */
134 last_index = first_index;
142 if (av_get_frame_filename(buf, sizeof(buf), path,
143 last_index + range1) < 0)
145 if (avio_check(buf, AVIO_FLAG_READ) <= 0)
148 /* just in case... */
149 if (range >= (1 << 30))
152 /* we are sure than image last_index + range exists */
157 *pfirst_index = first_index;
158 *plast_index = last_index;
165 static int read_probe(AVProbeData *p)
167 if (p->filename && ff_guess_image2_codec(p->filename)) {
168 if (av_filename_number_test(p->filename))
169 return AVPROBE_SCORE_MAX;
170 else if (is_glob(p->filename))
171 return AVPROBE_SCORE_MAX;
173 return AVPROBE_SCORE_MAX/2;
178 static int read_header(AVFormatContext *s1)
180 VideoDemuxData *s = s1->priv_data;
181 int first_index, last_index, ret = 0;
182 int width = 0, height = 0;
184 enum PixelFormat pix_fmt = PIX_FMT_NONE;
185 AVRational framerate;
187 s1->ctx_flags |= AVFMTCTX_NOHEADER;
189 st = avformat_new_stream(s1, NULL);
191 return AVERROR(ENOMEM);
194 if (s->pixel_format && (pix_fmt = av_get_pix_fmt(s->pixel_format)) == PIX_FMT_NONE) {
195 av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n", s->pixel_format);
196 return AVERROR(EINVAL);
198 if (s->video_size && (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
199 av_log(s, AV_LOG_ERROR, "Could not parse video size: %s.\n", s->video_size);
202 if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
203 av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s->framerate);
207 av_strlcpy(s->path, s1->filename, sizeof(s->path));
212 if (s1->iformat->flags & AVFMT_NOFILE)
216 st->need_parsing = AVSTREAM_PARSE_FULL;
219 avpriv_set_pts_info(st, 60, framerate.den, framerate.num);
221 if (width && height) {
222 st->codec->width = width;
223 st->codec->height = height;
227 s->use_glob = is_glob(s->path);
230 char *p = s->path, *q, *dup;
233 dup = q = av_strdup(p);
235 /* Do we have room for the next char and a \ insertion? */
236 if ((p - s->path) >= (sizeof(s->path) - 2))
238 if (*q == '%' && strspn(q + 1, "%*?[]{}"))
240 else if (strspn(q, "\\*?[]{}"))
247 gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
249 return AVERROR(ENOENT);
252 last_index = s->globstate.gl_pathc - 1;
255 if (find_image_range(&first_index, &last_index, s->path,
256 s->start_number - 1) < 0)
257 return AVERROR(ENOENT);
259 s->img_first = first_index;
260 s->img_last = last_index;
261 s->img_number = first_index;
262 /* compute duration */
264 st->duration = last_index - first_index + 1;
267 if(s1->video_codec_id){
268 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
269 st->codec->codec_id = s1->video_codec_id;
270 }else if(s1->audio_codec_id){
271 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
272 st->codec->codec_id = s1->audio_codec_id;
274 const char *str= strrchr(s->path, '.');
275 s->split_planes = str && !av_strcasecmp(str + 1, "y");
276 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
277 st->codec->codec_id = ff_guess_image2_codec(s->path);
278 if (st->codec->codec_id == CODEC_ID_LJPEG)
279 st->codec->codec_id = CODEC_ID_MJPEG;
281 if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO && pix_fmt != PIX_FMT_NONE)
282 st->codec->pix_fmt = pix_fmt;
287 static int read_packet(AVFormatContext *s1, AVPacket *pkt)
289 VideoDemuxData *s = s1->priv_data;
290 char filename_bytes[1024];
291 char *filename = filename_bytes;
293 int size[3]={0}, ret[3]={0};
295 AVCodecContext *codec= s1->streams[0]->codec;
298 /* loop over input */
299 if (s->loop && s->img_number > s->img_last) {
300 s->img_number = s->img_first;
302 if (s->img_number > s->img_last)
306 filename = s->globstate.gl_pathv[s->img_number];
309 if (av_get_frame_filename(filename_bytes, sizeof(filename_bytes),
310 s->path, s->img_number)<0 && s->img_number > 1)
314 if (avio_open2(&f[i], filename, AVIO_FLAG_READ,
315 &s1->interrupt_callback, NULL) < 0) {
318 av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",filename);
321 size[i]= avio_size(f[i]);
325 filename[ strlen(filename) - 1 ]= 'U' + i;
328 if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width)
329 infer_size(&codec->width, &codec->height, size[0]);
337 av_new_packet(pkt, size[0] + size[1] + size[2]);
338 pkt->stream_index = 0;
339 pkt->flags |= AV_PKT_FLAG_KEY;
344 ret[i]= avio_read(f[i], pkt->data + pkt->size, size[i]);
352 if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
354 return AVERROR(EIO); /* signal EOF */
362 static int read_close(struct AVFormatContext* s1)
364 VideoDemuxData *s = s1->priv_data;
367 globfree(&s->globstate);
373 #define OFFSET(x) offsetof(VideoDemuxData, x)
374 #define DEC AV_OPT_FLAG_DECODING_PARAM
375 static const AVOption options[] = {
376 { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
377 { "video_size", "", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
378 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
379 { "loop", "", OFFSET(loop), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, DEC },
380 { "start_number", "first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, {.dbl = 1}, 1, INT_MAX, DEC },
384 #if CONFIG_IMAGE2_DEMUXER
385 static const AVClass img2_class = {
386 .class_name = "image2 demuxer",
387 .item_name = av_default_item_name,
389 .version = LIBAVUTIL_VERSION_INT,
391 AVInputFormat ff_image2_demuxer = {
393 .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
394 .priv_data_size = sizeof(VideoDemuxData),
395 .read_probe = read_probe,
396 .read_header = read_header,
397 .read_packet = read_packet,
398 .read_close = read_close,
399 .flags = AVFMT_NOFILE,
400 .priv_class = &img2_class,
403 #if CONFIG_IMAGE2PIPE_DEMUXER
404 static const AVClass img2pipe_class = {
405 .class_name = "image2pipe demuxer",
406 .item_name = av_default_item_name,
408 .version = LIBAVUTIL_VERSION_INT,
410 AVInputFormat ff_image2pipe_demuxer = {
411 .name = "image2pipe",
412 .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
413 .priv_data_size = sizeof(VideoDemuxData),
414 .read_header = read_header,
415 .read_packet = read_packet,
416 .priv_class = &img2pipe_class,