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