]> git.sesse.net Git - ffmpeg/blob - libavformat/img2dec.c
lavf: move CodecMime from matroska.h to internal.h
[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 Libav.
7  *
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.
12  *
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.
17  *
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
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
31 typedef struct {
32     const AVClass *class;  /**< Class for private options. */
33     int img_first;
34     int img_last;
35     int img_number;
36     int img_count;
37     int is_pipe;
38     char path[1024];
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. */
42     int loop;
43 } VideoDemuxData;
44
45 static const int sizes[][2] = {
46     { 640, 480 },
47     { 720, 480 },
48     { 720, 576 },
49     { 352, 288 },
50     { 352, 240 },
51     { 160, 128 },
52     { 512, 384 },
53     { 640, 352 },
54     { 640, 240 },
55 };
56
57 static int infer_size(int *width_ptr, int *height_ptr, int size)
58 {
59     int i;
60
61     for(i=0;i<FF_ARRAY_ELEMS(sizes);i++) {
62         if ((sizes[i][0] * sizes[i][1]) == size) {
63             *width_ptr = sizes[i][0];
64             *height_ptr = sizes[i][1];
65             return 0;
66         }
67     }
68     return -1;
69 }
70
71 /* return -1 if no image found */
72 static int find_image_range(int *pfirst_index, int *plast_index,
73                             const char *path)
74 {
75     char buf[1024];
76     int range, last_index, range1, first_index;
77
78     /* find the first image */
79     for(first_index = 0; first_index < 5; first_index++) {
80         if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0){
81             *pfirst_index =
82             *plast_index = 1;
83             if (avio_check(buf, AVIO_FLAG_READ) > 0)
84                 return 0;
85             return -1;
86         }
87         if (avio_check(buf, AVIO_FLAG_READ) > 0)
88             break;
89     }
90     if (first_index == 5)
91         goto fail;
92
93     /* find the last image */
94     last_index = first_index;
95     for(;;) {
96         range = 0;
97         for(;;) {
98             if (!range)
99                 range1 = 1;
100             else
101                 range1 = 2 * range;
102             if (av_get_frame_filename(buf, sizeof(buf), path,
103                                       last_index + range1) < 0)
104                 goto fail;
105             if (avio_check(buf, AVIO_FLAG_READ) <= 0)
106                 break;
107             range = range1;
108             /* just in case... */
109             if (range >= (1 << 30))
110                 goto fail;
111         }
112         /* we are sure than image last_index + range exists */
113         if (!range)
114             break;
115         last_index += range;
116     }
117     *pfirst_index = first_index;
118     *plast_index = last_index;
119     return 0;
120  fail:
121     return -1;
122 }
123
124
125 static int read_probe(AVProbeData *p)
126 {
127     if (p->filename && ff_guess_image2_codec(p->filename)) {
128         if (av_filename_number_test(p->filename))
129             return AVPROBE_SCORE_MAX;
130         else
131             return AVPROBE_SCORE_MAX/2;
132     }
133     return 0;
134 }
135
136 static int read_header(AVFormatContext *s1)
137 {
138     VideoDemuxData *s = s1->priv_data;
139     int first_index, last_index, ret = 0;
140     int width = 0, height = 0;
141     AVStream *st;
142     enum PixelFormat pix_fmt = PIX_FMT_NONE;
143     AVRational framerate;
144
145     s1->ctx_flags |= AVFMTCTX_NOHEADER;
146
147     st = avformat_new_stream(s1, NULL);
148     if (!st) {
149         return AVERROR(ENOMEM);
150     }
151
152     if (s->pixel_format && (pix_fmt = av_get_pix_fmt(s->pixel_format)) == PIX_FMT_NONE) {
153         av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n", s->pixel_format);
154         return AVERROR(EINVAL);
155     }
156     if (s->video_size && (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
157         av_log(s, AV_LOG_ERROR, "Could not parse video size: %s.\n", s->video_size);
158         return ret;
159     }
160     if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
161         av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s->framerate);
162         return ret;
163     }
164
165     av_strlcpy(s->path, s1->filename, sizeof(s->path));
166     s->img_number = 0;
167     s->img_count = 0;
168
169     /* find format */
170     if (s1->iformat->flags & AVFMT_NOFILE)
171         s->is_pipe = 0;
172     else{
173         s->is_pipe = 1;
174         st->need_parsing = AVSTREAM_PARSE_FULL;
175     }
176
177     avpriv_set_pts_info(st, 60, framerate.den, framerate.num);
178
179     if (width && height) {
180         st->codec->width  = width;
181         st->codec->height = height;
182     }
183
184     if (!s->is_pipe) {
185         if (find_image_range(&first_index, &last_index, s->path) < 0)
186             return AVERROR(ENOENT);
187         s->img_first = first_index;
188         s->img_last = last_index;
189         s->img_number = first_index;
190         /* compute duration */
191         st->start_time = 0;
192         st->duration = last_index - first_index + 1;
193     }
194
195     if(s1->video_codec_id){
196         st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
197         st->codec->codec_id = s1->video_codec_id;
198     }else if(s1->audio_codec_id){
199         st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
200         st->codec->codec_id = s1->audio_codec_id;
201     }else{
202         st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
203         st->codec->codec_id = ff_guess_image2_codec(s->path);
204     }
205     if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO && pix_fmt != PIX_FMT_NONE)
206         st->codec->pix_fmt = pix_fmt;
207
208     return 0;
209 }
210
211 static int read_packet(AVFormatContext *s1, AVPacket *pkt)
212 {
213     VideoDemuxData *s = s1->priv_data;
214     char filename[1024];
215     int i;
216     int size[3]={0}, ret[3]={0};
217     AVIOContext *f[3];
218     AVCodecContext *codec= s1->streams[0]->codec;
219
220     if (!s->is_pipe) {
221         /* loop over input */
222         if (s->loop && s->img_number > s->img_last) {
223             s->img_number = s->img_first;
224         }
225         if (s->img_number > s->img_last)
226             return AVERROR_EOF;
227         if (av_get_frame_filename(filename, sizeof(filename),
228                                   s->path, s->img_number)<0 && s->img_number > 1)
229             return AVERROR(EIO);
230         for(i=0; i<3; i++){
231             if (avio_open2(&f[i], filename, AVIO_FLAG_READ,
232                            &s1->interrupt_callback, NULL) < 0) {
233                 if(i==1)
234                     break;
235                 av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",filename);
236                 return AVERROR(EIO);
237             }
238             size[i]= avio_size(f[i]);
239
240             if(codec->codec_id != CODEC_ID_RAWVIDEO)
241                 break;
242             filename[ strlen(filename) - 1 ]= 'U' + i;
243         }
244
245         if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width)
246             infer_size(&codec->width, &codec->height, size[0]);
247     } else {
248         f[0] = s1->pb;
249         if (f[0]->eof_reached)
250             return AVERROR(EIO);
251         size[0]= 4096;
252     }
253
254     av_new_packet(pkt, size[0] + size[1] + size[2]);
255     pkt->stream_index = 0;
256     pkt->flags |= AV_PKT_FLAG_KEY;
257
258     pkt->size= 0;
259     for(i=0; i<3; i++){
260         if(size[i]){
261             ret[i]= avio_read(f[i], pkt->data + pkt->size, size[i]);
262             if (!s->is_pipe)
263                 avio_close(f[i]);
264             if(ret[i]>0)
265                 pkt->size += ret[i];
266         }
267     }
268
269     if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
270         av_free_packet(pkt);
271         return AVERROR(EIO); /* signal EOF */
272     } else {
273         s->img_count++;
274         s->img_number++;
275         return 0;
276     }
277 }
278
279 #define OFFSET(x) offsetof(VideoDemuxData, x)
280 #define DEC AV_OPT_FLAG_DECODING_PARAM
281 static const AVOption options[] = {
282     { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
283     { "video_size",   "", OFFSET(video_size),   AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
284     { "framerate",    "", OFFSET(framerate),    AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
285     { "loop",         "", OFFSET(loop),         AV_OPT_TYPE_INT,    {.dbl = 0},    0, 1, DEC },
286     { NULL },
287 };
288
289 #if CONFIG_IMAGE2_DEMUXER
290 static const AVClass img2_class = {
291     .class_name = "image2 demuxer",
292     .item_name  = av_default_item_name,
293     .option     = options,
294     .version    = LIBAVUTIL_VERSION_INT,
295 };
296 AVInputFormat ff_image2_demuxer = {
297     .name           = "image2",
298     .long_name      = NULL_IF_CONFIG_SMALL("image2 sequence"),
299     .priv_data_size = sizeof(VideoDemuxData),
300     .read_probe     = read_probe,
301     .read_header    = read_header,
302     .read_packet    = read_packet,
303     .flags          = AVFMT_NOFILE,
304     .priv_class     = &img2_class,
305 };
306 #endif
307 #if CONFIG_IMAGE2PIPE_DEMUXER
308 static const AVClass img2pipe_class = {
309     .class_name = "image2pipe demuxer",
310     .item_name  = av_default_item_name,
311     .option     = options,
312     .version    = LIBAVUTIL_VERSION_INT,
313 };
314 AVInputFormat ff_image2pipe_demuxer = {
315     .name           = "image2pipe",
316     .long_name      = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
317     .priv_data_size = sizeof(VideoDemuxData),
318     .read_header    = read_header,
319     .read_packet    = read_packet,
320     .priv_class     = &img2pipe_class,
321 };
322 #endif