3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 * Copyright (c) 2004 Michael Niedermayer
6 * This file is part of Libav.
8 * Libav 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 * Libav 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 Libav; 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"
31 typedef struct VideoDemuxData {
32 const AVClass *class; /**< Class for private options. */
39 char *pixel_format; /**< Set by a private option. */
40 char *video_size; /**< Set by a private option. */
41 char *framerate; /**< Set by a private option. */
46 static const int sizes[][2] = {
58 static int infer_size(int *width_ptr, int *height_ptr, int size)
62 for (i = 0; i < FF_ARRAY_ELEMS(sizes); i++) {
63 if ((sizes[i][0] * sizes[i][1]) == size) {
64 *width_ptr = sizes[i][0];
65 *height_ptr = sizes[i][1];
73 /* return -1 if no image found */
74 static int find_image_range(int *pfirst_index, int *plast_index,
75 const char *path, int max_start)
78 int range, last_index, range1, first_index;
80 /* find the first image */
81 for (first_index = 0; first_index < max_start; first_index++) {
82 if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0) {
85 if (avio_check(buf, AVIO_FLAG_READ) > 0)
89 if (avio_check(buf, AVIO_FLAG_READ) > 0)
95 /* find the last image */
96 last_index = first_index;
104 if (av_get_frame_filename(buf, sizeof(buf), path,
105 last_index + range1) < 0)
107 if (avio_check(buf, AVIO_FLAG_READ) <= 0)
110 /* just in case... */
111 if (range >= (1 << 30))
114 /* we are sure than image last_index + range exists */
119 *pfirst_index = first_index;
120 *plast_index = last_index;
127 static int img_read_probe(AVProbeData *p)
129 if (p->filename && ff_guess_image2_codec(p->filename)) {
130 if (av_filename_number_test(p->filename))
131 return AVPROBE_SCORE_MAX;
133 return AVPROBE_SCORE_EXTENSION;
138 static int img_read_header(AVFormatContext *s1)
140 VideoDemuxData *s = s1->priv_data;
141 int first_index, last_index, ret = 0;
142 int width = 0, height = 0;
144 enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
145 AVRational framerate;
147 s1->ctx_flags |= AVFMTCTX_NOHEADER;
149 st = avformat_new_stream(s1, NULL);
151 return AVERROR(ENOMEM);
154 if (s->pixel_format &&
155 (pix_fmt = av_get_pix_fmt(s->pixel_format)) == AV_PIX_FMT_NONE) {
156 av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n",
158 return AVERROR(EINVAL);
161 (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
162 av_log(s, AV_LOG_ERROR,
163 "Could not parse video size: %s.\n", s->video_size);
166 if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
167 av_log(s, AV_LOG_ERROR,
168 "Could not parse framerate: %s.\n", s->framerate);
172 av_strlcpy(s->path, s1->filename, sizeof(s->path));
177 if (s1->iformat->flags & AVFMT_NOFILE)
181 st->need_parsing = AVSTREAM_PARSE_FULL;
184 avpriv_set_pts_info(st, 60, framerate.den, framerate.num);
186 if (width && height) {
187 st->codecpar->width = width;
188 st->codecpar->height = height;
192 if (find_image_range(&first_index, &last_index, s->path,
193 FFMAX(s->start_number, 5)) < 0)
194 return AVERROR(ENOENT);
195 s->img_first = first_index;
196 s->img_last = last_index;
197 s->img_number = s->start_number != 1 ? s->start_number : first_index;
198 /* compute duration */
200 st->duration = last_index - first_index + 1;
203 if (s1->video_codec_id) {
204 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
205 st->codecpar->codec_id = s1->video_codec_id;
206 } else if (s1->audio_codec_id) {
207 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
208 st->codecpar->codec_id = s1->audio_codec_id;
210 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
211 st->codecpar->codec_id = ff_guess_image2_codec(s->path);
213 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
214 pix_fmt != AV_PIX_FMT_NONE)
215 st->codecpar->format = pix_fmt;
220 static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
222 VideoDemuxData *s = s1->priv_data;
225 int size[3] = { 0 }, ret[3] = { 0 };
226 AVIOContext *f[3] = { NULL };
227 AVCodecParameters *par = s1->streams[0]->codecpar;
230 /* loop over input */
231 if (s->loop && s->img_number > s->img_last) {
232 s->img_number = s->img_first;
234 if (s->img_number > s->img_last)
236 if (av_get_frame_filename(filename, sizeof(filename),
238 s->img_number) < 0 && s->img_number > 1)
240 for (i = 0; i < 3; i++) {
241 if (s1->io_open(s1, &f[i], filename, AVIO_FLAG_READ, NULL) < 0) {
244 av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",
248 size[i] = avio_size(f[i]);
250 if (par->codec_id != AV_CODEC_ID_RAWVIDEO)
252 filename[strlen(filename) - 1] = 'U' + i;
255 if (par->codec_id == AV_CODEC_ID_RAWVIDEO && !par->width)
256 infer_size(&par->width, &par->height, size[0]);
259 if (f[0]->eof_reached)
264 res = av_new_packet(pkt, size[0] + size[1] + size[2]);
267 pkt->stream_index = 0;
268 pkt->flags |= AV_PKT_FLAG_KEY;
271 for (i = 0; i < 3; i++) {
273 ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
275 ff_format_io_close(s1, &f[i]);
281 if (ret[0] <= 0 || ret[1] < 0 || ret[2] < 0) {
282 av_packet_unref(pkt);
283 return AVERROR(EIO); /* signal EOF */
291 #define OFFSET(x) offsetof(VideoDemuxData, x)
292 #define DEC AV_OPT_FLAG_DECODING_PARAM
293 static const AVOption options[] = {
294 { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, DEC },
295 { "video_size", "", OFFSET(video_size), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, DEC },
296 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = "25" }, 0, 0, DEC },
297 { "loop", "", OFFSET(loop), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, DEC },
298 { "start_number", "first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, INT_MAX, DEC },
302 #if CONFIG_IMAGE2_DEMUXER
303 static const AVClass img2_class = {
304 .class_name = "image2 demuxer",
305 .item_name = av_default_item_name,
307 .version = LIBAVUTIL_VERSION_INT,
309 AVInputFormat ff_image2_demuxer = {
311 .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
312 .priv_data_size = sizeof(VideoDemuxData),
313 .read_probe = img_read_probe,
314 .read_header = img_read_header,
315 .read_packet = img_read_packet,
316 .flags = AVFMT_NOFILE,
317 .priv_class = &img2_class,
320 #if CONFIG_IMAGE2PIPE_DEMUXER
321 static const AVClass img2pipe_class = {
322 .class_name = "image2pipe demuxer",
323 .item_name = av_default_item_name,
325 .version = LIBAVUTIL_VERSION_INT,
327 AVInputFormat ff_image2pipe_demuxer = {
328 .name = "image2pipe",
329 .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
330 .priv_data_size = sizeof(VideoDemuxData),
331 .read_header = img_read_header,
332 .read_packet = img_read_packet,
333 .priv_class = &img2pipe_class,