]> git.sesse.net Git - ffmpeg/blob - libavformat/img2dec.c
Merge commit 'dad5fd59f3d6a8311365314cfcde0ebcd15c2b01'
[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 #define _DEFAULT_SOURCE
24 #define _BSD_SOURCE
25 #include <sys/stat.h>
26 #include "libavutil/avstring.h"
27 #include "libavutil/log.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/pixdesc.h"
30 #include "libavutil/parseutils.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavcodec/gif.h"
33 #include "avformat.h"
34 #include "avio_internal.h"
35 #include "internal.h"
36 #include "img2.h"
37 #include "libavcodec/mjpeg.h"
38 #include "libavcodec/xwd.h"
39 #include "subtitles.h"
40
41 #if HAVE_GLOB
42 /* Locally define as 0 (bitwise-OR no-op) any missing glob options that
43    are non-posix glibc/bsd extensions. */
44 #ifndef GLOB_NOMAGIC
45 #define GLOB_NOMAGIC 0
46 #endif
47 #ifndef GLOB_BRACE
48 #define GLOB_BRACE 0
49 #endif
50
51 #endif /* HAVE_GLOB */
52
53 static const int sizes[][2] = {
54     { 640, 480 },
55     { 720, 480 },
56     { 720, 576 },
57     { 352, 288 },
58     { 352, 240 },
59     { 160, 128 },
60     { 512, 384 },
61     { 640, 352 },
62     { 640, 240 },
63 };
64
65 static int infer_size(int *width_ptr, int *height_ptr, int size)
66 {
67     int i;
68
69     for (i = 0; i < FF_ARRAY_ELEMS(sizes); i++) {
70         if ((sizes[i][0] * sizes[i][1]) == size) {
71             *width_ptr  = sizes[i][0];
72             *height_ptr = sizes[i][1];
73             return 0;
74         }
75     }
76
77     return -1;
78 }
79
80 static int is_glob(const char *path)
81 {
82 #if HAVE_GLOB
83     size_t span = 0;
84     const char *p = path;
85
86     while (p = strchr(p, '%')) {
87         if (*(++p) == '%') {
88             ++p;
89             continue;
90         }
91         if (span = strspn(p, "*?[]{}"))
92             break;
93     }
94     /* Did we hit a glob char or get to the end? */
95     return span != 0;
96 #else
97     return 0;
98 #endif
99 }
100
101 /**
102  * Get index range of image files matched by path.
103  *
104  * @param pfirst_index pointer to index updated with the first number in the range
105  * @param plast_index  pointer to index updated with the last number in the range
106  * @param path         path which has to be matched by the image files in the range
107  * @param start_index  minimum accepted value for the first index in the range
108  * @return -1 if no image file could be found
109  */
110 static int find_image_range(AVIOContext *pb, int *pfirst_index, int *plast_index,
111                             const char *path, int start_index, int start_index_range)
112 {
113     char buf[1024];
114     int range, last_index, range1, first_index;
115
116     /* find the first image */
117     for (first_index = start_index; first_index < start_index + start_index_range; first_index++) {
118         if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0) {
119             *pfirst_index =
120             *plast_index  = 1;
121             if (pb || avio_check(buf, AVIO_FLAG_READ) > 0)
122                 return 0;
123             return -1;
124         }
125         if (avio_check(buf, AVIO_FLAG_READ) > 0)
126             break;
127     }
128     if (first_index == start_index + start_index_range)
129         goto fail;
130
131     /* find the last image */
132     last_index = first_index;
133     for (;;) {
134         range = 0;
135         for (;;) {
136             if (!range)
137                 range1 = 1;
138             else
139                 range1 = 2 * range;
140             if (av_get_frame_filename(buf, sizeof(buf), path,
141                                       last_index + range1) < 0)
142                 goto fail;
143             if (avio_check(buf, AVIO_FLAG_READ) <= 0)
144                 break;
145             range = range1;
146             /* just in case... */
147             if (range >= (1 << 30))
148                 goto fail;
149         }
150         /* we are sure than image last_index + range exists */
151         if (!range)
152             break;
153         last_index += range;
154     }
155     *pfirst_index = first_index;
156     *plast_index  = last_index;
157     return 0;
158
159 fail:
160     return -1;
161 }
162
163 static int img_read_probe(AVProbeData *p)
164 {
165     if (p->filename && ff_guess_image2_codec(p->filename)) {
166         if (av_filename_number_test(p->filename))
167             return AVPROBE_SCORE_MAX;
168         else if (is_glob(p->filename))
169             return AVPROBE_SCORE_MAX;
170         else if (p->filename[strcspn(p->filename, "*?{")]) // probably PT_GLOB
171             return AVPROBE_SCORE_EXTENSION + 2; // score chosen to be a tad above the image pipes
172         else if (p->buf_size == 0)
173             return 0;
174         else if (av_match_ext(p->filename, "raw") || av_match_ext(p->filename, "gif"))
175             return 5;
176         else
177             return AVPROBE_SCORE_EXTENSION;
178     }
179     return 0;
180 }
181
182 int ff_img_read_header(AVFormatContext *s1)
183 {
184     VideoDemuxData *s = s1->priv_data;
185     int first_index = 1, last_index = 1;
186     AVStream *st;
187     enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
188
189     s1->ctx_flags |= AVFMTCTX_NOHEADER;
190
191     st = avformat_new_stream(s1, NULL);
192     if (!st) {
193         return AVERROR(ENOMEM);
194     }
195
196     if (s->pixel_format &&
197         (pix_fmt = av_get_pix_fmt(s->pixel_format)) == AV_PIX_FMT_NONE) {
198         av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n",
199                s->pixel_format);
200         return AVERROR(EINVAL);
201     }
202
203     av_strlcpy(s->path, s1->url, sizeof(s->path));
204     s->img_number = 0;
205     s->img_count  = 0;
206
207     /* find format */
208     if (s1->iformat->flags & AVFMT_NOFILE)
209         s->is_pipe = 0;
210     else {
211         s->is_pipe       = 1;
212         st->need_parsing = AVSTREAM_PARSE_FULL;
213     }
214
215     if (s->ts_from_file == 2) {
216 #if !HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
217         av_log(s1, AV_LOG_ERROR, "POSIX.1-2008 not supported, nanosecond file timestamps unavailable\n");
218         return AVERROR(ENOSYS);
219 #endif
220         avpriv_set_pts_info(st, 64, 1, 1000000000);
221     } else if (s->ts_from_file)
222         avpriv_set_pts_info(st, 64, 1, 1);
223     else
224         avpriv_set_pts_info(st, 64, s->framerate.den, s->framerate.num);
225
226     if (s->width && s->height) {
227         st->codecpar->width  = s->width;
228         st->codecpar->height = s->height;
229     }
230
231     if (!s->is_pipe) {
232         if (s->pattern_type == PT_DEFAULT) {
233             if (s1->pb) {
234                 s->pattern_type = PT_NONE;
235             } else
236                 s->pattern_type = PT_GLOB_SEQUENCE;
237         }
238
239         if (s->pattern_type == PT_GLOB_SEQUENCE) {
240         s->use_glob = is_glob(s->path);
241         if (s->use_glob) {
242 #if HAVE_GLOB
243             char *p = s->path, *q, *dup;
244             int gerr;
245 #endif
246
247             av_log(s1, AV_LOG_WARNING, "Pattern type 'glob_sequence' is deprecated: "
248                    "use pattern_type 'glob' instead\n");
249 #if HAVE_GLOB
250             dup = q = av_strdup(p);
251             while (*q) {
252                 /* Do we have room for the next char and a \ insertion? */
253                 if ((p - s->path) >= (sizeof(s->path) - 2))
254                   break;
255                 if (*q == '%' && strspn(q + 1, "%*?[]{}"))
256                     ++q;
257                 else if (strspn(q, "\\*?[]{}"))
258                     *p++ = '\\';
259                 *p++ = *q++;
260             }
261             *p = 0;
262             av_free(dup);
263
264             gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
265             if (gerr != 0) {
266                 return AVERROR(ENOENT);
267             }
268             first_index = 0;
269             last_index = s->globstate.gl_pathc - 1;
270 #endif
271         }
272         }
273         if ((s->pattern_type == PT_GLOB_SEQUENCE && !s->use_glob) || s->pattern_type == PT_SEQUENCE) {
274             if (find_image_range(s1->pb, &first_index, &last_index, s->path,
275                                  s->start_number, s->start_number_range) < 0) {
276                 av_log(s1, AV_LOG_ERROR,
277                        "Could find no file with path '%s' and index in the range %d-%d\n",
278                        s->path, s->start_number, s->start_number + s->start_number_range - 1);
279                 return AVERROR(ENOENT);
280             }
281         } else if (s->pattern_type == PT_GLOB) {
282 #if HAVE_GLOB
283             int gerr;
284             gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
285             if (gerr != 0) {
286                 return AVERROR(ENOENT);
287             }
288             first_index = 0;
289             last_index = s->globstate.gl_pathc - 1;
290             s->use_glob = 1;
291 #else
292             av_log(s1, AV_LOG_ERROR,
293                    "Pattern type 'glob' was selected but globbing "
294                    "is not supported by this libavformat build\n");
295             return AVERROR(ENOSYS);
296 #endif
297         } else if (s->pattern_type != PT_GLOB_SEQUENCE && s->pattern_type != PT_NONE) {
298             av_log(s1, AV_LOG_ERROR,
299                    "Unknown value '%d' for pattern_type option\n", s->pattern_type);
300             return AVERROR(EINVAL);
301         }
302         s->img_first  = first_index;
303         s->img_last   = last_index;
304         s->img_number = first_index;
305         /* compute duration */
306         if (!s->ts_from_file) {
307             st->start_time = 0;
308             st->duration   = last_index - first_index + 1;
309         }
310     }
311
312     if (s1->video_codec_id) {
313         st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
314         st->codecpar->codec_id   = s1->video_codec_id;
315     } else if (s1->audio_codec_id) {
316         st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
317         st->codecpar->codec_id   = s1->audio_codec_id;
318     } else if (s1->iformat->raw_codec_id) {
319         st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
320         st->codecpar->codec_id   = s1->iformat->raw_codec_id;
321     } else {
322         const char *str = strrchr(s->path, '.');
323         s->split_planes       = str && !av_strcasecmp(str + 1, "y");
324         st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
325         if (s1->pb) {
326             int probe_buffer_size = 2048;
327             uint8_t *probe_buffer = av_realloc(NULL, probe_buffer_size + AVPROBE_PADDING_SIZE);
328             const AVInputFormat *fmt = NULL;
329             void *fmt_iter = NULL;
330             AVProbeData pd = { 0 };
331
332             if (!probe_buffer)
333                 return AVERROR(ENOMEM);
334
335             probe_buffer_size = avio_read(s1->pb, probe_buffer, probe_buffer_size);
336             if (probe_buffer_size < 0) {
337                 av_free(probe_buffer);
338                 return probe_buffer_size;
339             }
340             memset(probe_buffer + probe_buffer_size, 0, AVPROBE_PADDING_SIZE);
341
342             pd.buf = probe_buffer;
343             pd.buf_size = probe_buffer_size;
344             pd.filename = s1->url;
345
346             while ((fmt = av_demuxer_iterate(&fmt_iter))) {
347                 if (fmt->read_header != ff_img_read_header ||
348                     !fmt->read_probe ||
349                     (fmt->flags & AVFMT_NOFILE) ||
350                     !fmt->raw_codec_id)
351                     continue;
352                 if (fmt->read_probe(&pd) > 0) {
353                     st->codecpar->codec_id = fmt->raw_codec_id;
354                     break;
355                 }
356             }
357             if (s1->flags & AVFMT_FLAG_CUSTOM_IO) {
358                 avio_seek(s1->pb, 0, SEEK_SET);
359             } else
360                 ffio_rewind_with_probe_data(s1->pb, &probe_buffer, probe_buffer_size);
361         }
362         if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
363             st->codecpar->codec_id = ff_guess_image2_codec(s->path);
364         if (st->codecpar->codec_id == AV_CODEC_ID_LJPEG)
365             st->codecpar->codec_id = AV_CODEC_ID_MJPEG;
366         if (st->codecpar->codec_id == AV_CODEC_ID_ALIAS_PIX) // we cannot distingiush this from BRENDER_PIX
367             st->codecpar->codec_id = AV_CODEC_ID_NONE;
368     }
369     if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
370         pix_fmt != AV_PIX_FMT_NONE)
371         st->codecpar->format = pix_fmt;
372
373     return 0;
374 }
375
376 int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt)
377 {
378     VideoDemuxData *s = s1->priv_data;
379     char filename_bytes[1024];
380     char *filename = filename_bytes;
381     int i, res;
382     int size[3]           = { 0 }, ret[3] = { 0 };
383     AVIOContext *f[3]     = { NULL };
384     AVCodecParameters *par = s1->streams[0]->codecpar;
385
386     if (!s->is_pipe) {
387         /* loop over input */
388         if (s->loop && s->img_number > s->img_last) {
389             s->img_number = s->img_first;
390         }
391         if (s->img_number > s->img_last)
392             return AVERROR_EOF;
393         if (s->pattern_type == PT_NONE) {
394             av_strlcpy(filename_bytes, s->path, sizeof(filename_bytes));
395         } else if (s->use_glob) {
396 #if HAVE_GLOB
397             filename = s->globstate.gl_pathv[s->img_number];
398 #endif
399         } else {
400         if (av_get_frame_filename(filename_bytes, sizeof(filename_bytes),
401                                   s->path,
402                                   s->img_number) < 0 && s->img_number > 1)
403             return AVERROR(EIO);
404         }
405         for (i = 0; i < 3; i++) {
406             if (s1->pb &&
407                 !strcmp(filename_bytes, s->path) &&
408                 !s->loop &&
409                 !s->split_planes) {
410                 f[i] = s1->pb;
411             } else if (s1->io_open(s1, &f[i], filename, AVIO_FLAG_READ, NULL) < 0) {
412                 if (i >= 1)
413                     break;
414                 av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",
415                        filename);
416                 return AVERROR(EIO);
417             }
418             size[i] = avio_size(f[i]);
419
420             if (!s->split_planes)
421                 break;
422             filename[strlen(filename) - 1] = 'U' + i;
423         }
424
425         if (par->codec_id == AV_CODEC_ID_NONE) {
426             AVProbeData pd = { 0 };
427             AVInputFormat *ifmt;
428             uint8_t header[PROBE_BUF_MIN + AVPROBE_PADDING_SIZE];
429             int ret;
430             int score = 0;
431
432             ret = avio_read(f[0], header, PROBE_BUF_MIN);
433             if (ret < 0)
434                 return ret;
435             memset(header + ret, 0, sizeof(header) - ret);
436             avio_skip(f[0], -ret);
437             pd.buf = header;
438             pd.buf_size = ret;
439             pd.filename = filename;
440
441             ifmt = av_probe_input_format3(&pd, 1, &score);
442             if (ifmt && ifmt->read_packet == ff_img_read_packet && ifmt->raw_codec_id)
443                 par->codec_id = ifmt->raw_codec_id;
444         }
445
446         if (par->codec_id == AV_CODEC_ID_RAWVIDEO && !par->width)
447             infer_size(&par->width, &par->height, size[0]);
448     } else {
449         f[0] = s1->pb;
450         if (avio_feof(f[0]) && s->loop && s->is_pipe)
451             avio_seek(f[0], 0, SEEK_SET);
452         if (avio_feof(f[0]))
453             return AVERROR_EOF;
454         if (s->frame_size > 0) {
455             size[0] = s->frame_size;
456         } else if (!s1->streams[0]->parser) {
457             size[0] = avio_size(s1->pb);
458         } else {
459             size[0] = 4096;
460         }
461     }
462
463     res = av_new_packet(pkt, size[0] + size[1] + size[2]);
464     if (res < 0) {
465         goto fail;
466     }
467     pkt->stream_index = 0;
468     pkt->flags       |= AV_PKT_FLAG_KEY;
469     if (s->ts_from_file) {
470         struct stat img_stat;
471         if (stat(filename, &img_stat)) {
472             res = AVERROR(EIO);
473             goto fail;
474         }
475         pkt->pts = (int64_t)img_stat.st_mtime;
476 #if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
477         if (s->ts_from_file == 2)
478             pkt->pts = 1000000000*pkt->pts + img_stat.st_mtim.tv_nsec;
479 #endif
480         av_add_index_entry(s1->streams[0], s->img_number, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
481     } else if (!s->is_pipe) {
482         pkt->pts      = s->pts;
483     }
484
485     if (s->is_pipe)
486         pkt->pos = avio_tell(f[0]);
487
488     pkt->size = 0;
489     for (i = 0; i < 3; i++) {
490         if (f[i]) {
491             ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
492             if (s->loop && s->is_pipe && ret[i] == AVERROR_EOF) {
493                 if (avio_seek(f[i], 0, SEEK_SET) >= 0) {
494                     pkt->pos = 0;
495                     ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
496                 }
497             }
498             if (!s->is_pipe && f[i] != s1->pb)
499                 ff_format_io_close(s1, &f[i]);
500             if (ret[i] > 0)
501                 pkt->size += ret[i];
502         }
503     }
504
505     if (ret[0] <= 0 || ret[1] < 0 || ret[2] < 0) {
506         av_packet_unref(pkt);
507         if (ret[0] < 0) {
508             res = ret[0];
509         } else if (ret[1] < 0) {
510             res = ret[1];
511         } else if (ret[2] < 0) {
512             res = ret[2];
513         } else {
514             res = AVERROR_EOF;
515         }
516         goto fail;
517     } else {
518         s->img_count++;
519         s->img_number++;
520         s->pts++;
521         return 0;
522     }
523
524 fail:
525     if (!s->is_pipe) {
526         for (i = 0; i < 3; i++) {
527             if (f[i] != s1->pb)
528                 ff_format_io_close(s1, &f[i]);
529         }
530     }
531     return res;
532 }
533
534 static int img_read_close(struct AVFormatContext* s1)
535 {
536 #if HAVE_GLOB
537     VideoDemuxData *s = s1->priv_data;
538     if (s->use_glob) {
539         globfree(&s->globstate);
540     }
541 #endif
542     return 0;
543 }
544
545 static int img_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
546 {
547     VideoDemuxData *s1 = s->priv_data;
548     AVStream *st = s->streams[0];
549
550     if (s1->ts_from_file) {
551         int index = av_index_search_timestamp(st, timestamp, flags);
552         if(index < 0)
553             return -1;
554         s1->img_number = st->index_entries[index].pos;
555         return 0;
556     }
557
558     if (timestamp < 0 || !s1->loop && timestamp > s1->img_last - s1->img_first)
559         return -1;
560     s1->img_number = timestamp%(s1->img_last - s1->img_first + 1) + s1->img_first;
561     s1->pts = timestamp;
562     return 0;
563 }
564
565 #define OFFSET(x) offsetof(VideoDemuxData, x)
566 #define DEC AV_OPT_FLAG_DECODING_PARAM
567 #define COMMON_OPTIONS \
568     { "framerate",    "set the video framerate", OFFSET(framerate),    AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC }, \
569     { "pixel_format", "set video pixel format",  OFFSET(pixel_format), AV_OPT_TYPE_STRING,     {.str = NULL}, 0, 0,       DEC }, \
570     { "video_size",   "set video size",          OFFSET(width),        AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0,       DEC }, \
571     { "loop",         "force loop over input file sequence", OFFSET(loop), AV_OPT_TYPE_BOOL,   {.i64 = 0   }, 0, 1,       DEC }, \
572     { NULL },
573
574 #if CONFIG_IMAGE2_DEMUXER
575 const AVOption ff_img_options[] = {
576     { "pattern_type", "set pattern type",                    OFFSET(pattern_type), AV_OPT_TYPE_INT,    {.i64=PT_DEFAULT}, 0,       INT_MAX, DEC, "pattern_type"},
577     { "glob_sequence","select glob/sequence pattern type",   0, AV_OPT_TYPE_CONST,  {.i64=PT_GLOB_SEQUENCE}, INT_MIN, INT_MAX, DEC, "pattern_type" },
578     { "glob",         "select glob pattern type",            0, AV_OPT_TYPE_CONST,  {.i64=PT_GLOB         }, INT_MIN, INT_MAX, DEC, "pattern_type" },
579     { "sequence",     "select sequence pattern type",        0, AV_OPT_TYPE_CONST,  {.i64=PT_SEQUENCE     }, INT_MIN, INT_MAX, DEC, "pattern_type" },
580     { "none",         "disable pattern matching",            0, AV_OPT_TYPE_CONST,  {.i64=PT_NONE         }, INT_MIN, INT_MAX, DEC, "pattern_type" },
581     { "start_number", "set first number in the sequence",    OFFSET(start_number), AV_OPT_TYPE_INT,    {.i64 = 0   }, INT_MIN, INT_MAX, DEC },
582     { "start_number_range", "set range for looking at the first sequence number", OFFSET(start_number_range), AV_OPT_TYPE_INT, {.i64 = 5}, 1, INT_MAX, DEC },
583     { "ts_from_file", "set frame timestamp from file's one", OFFSET(ts_from_file), AV_OPT_TYPE_INT,    {.i64 = 0   }, 0, 2,       DEC, "ts_type" },
584     { "none", "none",                   0, AV_OPT_TYPE_CONST,    {.i64 = 0   }, 0, 2,       DEC, "ts_type" },
585     { "sec",  "second precision",       0, AV_OPT_TYPE_CONST,    {.i64 = 1   }, 0, 2,       DEC, "ts_type" },
586     { "ns",   "nano second precision",  0, AV_OPT_TYPE_CONST,    {.i64 = 2   }, 0, 2,       DEC, "ts_type" },
587     COMMON_OPTIONS
588 };
589
590 static const AVClass img2_class = {
591     .class_name = "image2 demuxer",
592     .item_name  = av_default_item_name,
593     .option     = ff_img_options,
594     .version    = LIBAVUTIL_VERSION_INT,
595 };
596 AVInputFormat ff_image2_demuxer = {
597     .name           = "image2",
598     .long_name      = NULL_IF_CONFIG_SMALL("image2 sequence"),
599     .priv_data_size = sizeof(VideoDemuxData),
600     .read_probe     = img_read_probe,
601     .read_header    = ff_img_read_header,
602     .read_packet    = ff_img_read_packet,
603     .read_close     = img_read_close,
604     .read_seek      = img_read_seek,
605     .flags          = AVFMT_NOFILE,
606     .priv_class     = &img2_class,
607 };
608 #endif
609
610 const AVOption ff_img2pipe_options[] = {
611     { "frame_size", "force frame size in bytes", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
612     COMMON_OPTIONS
613 };
614
615 #if CONFIG_IMAGE2PIPE_DEMUXER
616 static const AVClass img2pipe_class = {
617     .class_name = "image2pipe demuxer",
618     .item_name  = av_default_item_name,
619     .option     = ff_img2pipe_options,
620     .version    = LIBAVUTIL_VERSION_INT,
621 };
622 AVInputFormat ff_image2pipe_demuxer = {
623     .name           = "image2pipe",
624     .long_name      = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
625     .priv_data_size = sizeof(VideoDemuxData),
626     .read_header    = ff_img_read_header,
627     .read_packet    = ff_img_read_packet,
628     .priv_class     = &img2pipe_class,
629 };
630 #endif
631
632 static int bmp_probe(AVProbeData *p)
633 {
634     const uint8_t *b = p->buf;
635     int ihsize;
636
637     if (AV_RB16(b) != 0x424d)
638         return 0;
639
640     ihsize = AV_RL32(b+14);
641     if (ihsize < 12 || ihsize > 255)
642         return 0;
643
644     if (!AV_RN32(b + 6)) {
645         return AVPROBE_SCORE_EXTENSION + 1;
646     }
647     return AVPROBE_SCORE_EXTENSION / 4;
648 }
649
650 static int dds_probe(AVProbeData *p)
651 {
652     const uint8_t *b = p->buf;
653
654     if (   AV_RB64(b) == 0x444453207c000000
655         && AV_RL32(b +  8)
656         && AV_RL32(b + 12))
657         return AVPROBE_SCORE_MAX - 1;
658     return 0;
659 }
660
661 static int dpx_probe(AVProbeData *p)
662 {
663     const uint8_t *b = p->buf;
664     int w, h;
665     int is_big = (AV_RN32(b) == AV_RN32("SDPX"));
666
667     if (p->buf_size < 0x304+8)
668         return 0;
669     w = is_big ? AV_RB32(p->buf + 0x304) : AV_RL32(p->buf + 0x304);
670     h = is_big ? AV_RB32(p->buf + 0x308) : AV_RL32(p->buf + 0x308);
671     if (w <= 0 || h <= 0)
672         return 0;
673
674     if (is_big || AV_RN32(b) == AV_RN32("XPDS"))
675         return AVPROBE_SCORE_EXTENSION + 1;
676     return 0;
677 }
678
679 static int exr_probe(AVProbeData *p)
680 {
681     const uint8_t *b = p->buf;
682
683     if (AV_RL32(b) == 20000630)
684         return AVPROBE_SCORE_EXTENSION + 1;
685     return 0;
686 }
687
688 static int j2k_probe(AVProbeData *p)
689 {
690     const uint8_t *b = p->buf;
691
692     if (AV_RB64(b) == 0x0000000c6a502020 ||
693         AV_RB32(b) == 0xff4fff51)
694         return AVPROBE_SCORE_EXTENSION + 1;
695     return 0;
696 }
697
698 static int jpeg_probe(AVProbeData *p)
699 {
700     const uint8_t *b = p->buf;
701     int i, state = SOI;
702
703     if (AV_RB16(b) != 0xFFD8 ||
704         AV_RB32(b) == 0xFFD8FFF7)
705     return 0;
706
707     b += 2;
708     for (i = 0; i < p->buf_size - 3; i++) {
709         int c;
710         if (b[i] != 0xFF)
711             continue;
712         c = b[i + 1];
713         switch (c) {
714         case SOI:
715             return 0;
716         case SOF0:
717         case SOF1:
718         case SOF2:
719         case SOF3:
720         case SOF5:
721         case SOF6:
722         case SOF7:
723             i += AV_RB16(&b[i + 2]) + 1;
724             if (state != SOI)
725                 return 0;
726             state = SOF0;
727             break;
728         case SOS:
729             i += AV_RB16(&b[i + 2]) + 1;
730             if (state != SOF0 && state != SOS)
731                 return 0;
732             state = SOS;
733             break;
734         case EOI:
735             if (state != SOS)
736                 return 0;
737             state = EOI;
738             break;
739         case DQT:
740         case APP0:
741         case APP1:
742         case APP2:
743         case APP3:
744         case APP4:
745         case APP5:
746         case APP6:
747         case APP7:
748         case APP8:
749         case APP9:
750         case APP10:
751         case APP11:
752         case APP12:
753         case APP13:
754         case APP14:
755         case APP15:
756         case COM:
757             i += AV_RB16(&b[i + 2]) + 1;
758             break;
759         default:
760             if (  (c > TEM && c < SOF0)
761                 || c == JPG)
762                 return 0;
763         }
764     }
765
766     if (state == EOI)
767         return AVPROBE_SCORE_EXTENSION + 1;
768     if (state == SOS)
769         return AVPROBE_SCORE_EXTENSION / 2;
770     return AVPROBE_SCORE_EXTENSION / 8;
771 }
772
773 static int jpegls_probe(AVProbeData *p)
774 {
775     const uint8_t *b = p->buf;
776
777     if (AV_RB32(b) == 0xffd8fff7)
778          return AVPROBE_SCORE_EXTENSION + 1;
779     return 0;
780 }
781
782 static int pcx_probe(AVProbeData *p)
783 {
784     const uint8_t *b = p->buf;
785
786     if (   p->buf_size < 128
787         || b[0] != 10
788         || b[1] > 5
789         || b[2] > 1
790         || av_popcount(b[3]) != 1 || b[3] > 8
791         || AV_RL16(&b[4]) > AV_RL16(&b[8])
792         || AV_RL16(&b[6]) > AV_RL16(&b[10])
793         || b[64])
794         return 0;
795     b += 73;
796     while (++b < p->buf + 128)
797         if (*b)
798             return AVPROBE_SCORE_EXTENSION / 4;
799
800     return AVPROBE_SCORE_EXTENSION + 1;
801 }
802
803 static int qdraw_probe(AVProbeData *p)
804 {
805     const uint8_t *b = p->buf;
806
807     if (   p->buf_size >= 528
808         && (AV_RB64(b + 520) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
809         && AV_RB16(b + 520)
810         && AV_RB16(b + 518))
811         return AVPROBE_SCORE_MAX * 3 / 4;
812     if (   (AV_RB64(b + 8) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
813         && AV_RB16(b + 8)
814         && AV_RB16(b + 6))
815         return AVPROBE_SCORE_EXTENSION / 4;
816     return 0;
817 }
818
819 static int pictor_probe(AVProbeData *p)
820 {
821     const uint8_t *b = p->buf;
822
823     if (AV_RL16(b) == 0x1234)
824         return AVPROBE_SCORE_EXTENSION / 4;
825     return 0;
826 }
827
828 static int png_probe(AVProbeData *p)
829 {
830     const uint8_t *b = p->buf;
831
832     if (AV_RB64(b) == 0x89504e470d0a1a0a)
833         return AVPROBE_SCORE_MAX - 1;
834     return 0;
835 }
836
837 static int psd_probe(AVProbeData *p)
838 {
839     const uint8_t *b = p->buf;
840     int ret = 0;
841     uint16_t color_mode;
842
843     if (AV_RL32(b) == MKTAG('8','B','P','S')) {
844         ret += 1;
845     } else {
846         return 0;
847     }
848
849     if ((b[4] == 0) && (b[5] == 1)) {/* version 1 is PSD, version 2 is PSB */
850         ret += 1;
851     } else {
852         return 0;
853     }
854
855     if ((AV_RL32(b+6) == 0) && (AV_RL16(b+10) == 0))/* reserved must be 0 */
856         ret += 1;
857
858     color_mode = AV_RB16(b+24);
859     if ((color_mode <= 9) && (color_mode != 5) && (color_mode != 6))
860         ret += 1;
861
862     return AVPROBE_SCORE_EXTENSION + ret;
863 }
864
865 static int sgi_probe(AVProbeData *p)
866 {
867     const uint8_t *b = p->buf;
868
869     if (AV_RB16(b) == 474 &&
870         (b[2] & ~1) == 0 &&
871         (b[3] & ~3) == 0 && b[3] &&
872         (AV_RB16(b + 4) & ~7) == 0 && AV_RB16(b + 4))
873         return AVPROBE_SCORE_EXTENSION + 1;
874     return 0;
875 }
876
877 static int sunrast_probe(AVProbeData *p)
878 {
879     const uint8_t *b = p->buf;
880
881     if (AV_RB32(b) == 0x59a66a95)
882         return AVPROBE_SCORE_EXTENSION + 1;
883     return 0;
884 }
885
886 static int svg_probe(AVProbeData *p)
887 {
888     const uint8_t *b = p->buf;
889     const uint8_t *end = p->buf + p->buf_size;
890
891     if (memcmp(p->buf, "<?xml", 5))
892         return 0;
893     while (b < end) {
894         int inc = ff_subtitles_next_line(b);
895         if (!inc)
896             break;
897         b += inc;
898         if (b >= end - 4)
899             return 0;
900         if (!memcmp(b, "<svg", 4))
901             return AVPROBE_SCORE_EXTENSION + 1;
902     }
903     return 0;
904 }
905
906 static int tiff_probe(AVProbeData *p)
907 {
908     const uint8_t *b = p->buf;
909
910     if (AV_RB32(b) == 0x49492a00 ||
911         AV_RB32(b) == 0x4D4D002a)
912         return AVPROBE_SCORE_EXTENSION + 1;
913     return 0;
914 }
915
916 static int webp_probe(AVProbeData *p)
917 {
918     const uint8_t *b = p->buf;
919
920     if (AV_RB32(b)     == 0x52494646 &&
921         AV_RB32(b + 8) == 0x57454250)
922         return AVPROBE_SCORE_MAX - 1;
923     return 0;
924 }
925
926 static int pnm_magic_check(const AVProbeData *p, int magic)
927 {
928     const uint8_t *b = p->buf;
929
930     return b[0] == 'P' && b[1] == magic + '0';
931 }
932
933 static inline int pnm_probe(const AVProbeData *p)
934 {
935     const uint8_t *b = p->buf;
936
937     while (b[2] == '\r')
938         b++;
939     if (b[2] == '\n' && (b[3] == '#' || (b[3] >= '0' && b[3] <= '9')))
940         return AVPROBE_SCORE_EXTENSION + 2;
941     return 0;
942 }
943
944 static int pbm_probe(AVProbeData *p)
945 {
946     return pnm_magic_check(p, 1) || pnm_magic_check(p, 4) ? pnm_probe(p) : 0;
947 }
948
949 static inline int pgmx_probe(AVProbeData *p)
950 {
951     return pnm_magic_check(p, 2) || pnm_magic_check(p, 5) ? pnm_probe(p) : 0;
952 }
953
954 static int pgm_probe(AVProbeData *p)
955 {
956     int ret = pgmx_probe(p);
957     return ret && !av_match_ext(p->filename, "pgmyuv") ? ret : 0;
958 }
959
960 static int pgmyuv_probe(AVProbeData *p) // custom FFmpeg format recognized by file extension
961 {
962     int ret = pgmx_probe(p);
963     return ret && av_match_ext(p->filename, "pgmyuv") ? ret : 0;
964 }
965
966 static int ppm_probe(AVProbeData *p)
967 {
968     return pnm_magic_check(p, 3) || pnm_magic_check(p, 6) ? pnm_probe(p) : 0;
969 }
970
971 static int pam_probe(AVProbeData *p)
972 {
973     return pnm_magic_check(p, 7) ? pnm_probe(p) : 0;
974 }
975
976 static int xpm_probe(AVProbeData *p)
977 {
978     const uint8_t *b = p->buf;
979
980     if (AV_RB64(b) == 0x2f2a2058504d202a && *(b+8) == '/')
981         return AVPROBE_SCORE_MAX - 1;
982     return 0;
983 }
984
985 static int xwd_probe(AVProbeData *p)
986 {
987     const uint8_t *b = p->buf;
988     unsigned width, bpp, bpad, lsize;
989
990     if (   p->buf_size < XWD_HEADER_SIZE
991         || AV_RB32(b     ) < XWD_HEADER_SIZE                          // header size
992         || AV_RB32(b +  4) != XWD_VERSION                             // version
993         || AV_RB32(b +  8) != XWD_Z_PIXMAP                            // format
994         || AV_RB32(b + 12) > 32 || !AV_RB32(b + 12)                   // depth
995         || AV_RB32(b + 16) == 0                                       // width
996         || AV_RB32(b + 20) == 0                                       // height
997         || AV_RB32(b + 28) > 1                                        // byteorder
998         || AV_RB32(b + 32) & ~56 || av_popcount(AV_RB32(b + 32)) != 1 // bitmap unit
999         || AV_RB32(b + 36) > 1                                        // bitorder
1000         || AV_RB32(b + 40) & ~56 || av_popcount(AV_RB32(b + 40)) != 1 // padding
1001         || AV_RB32(b + 44) > 32 || !AV_RB32(b + 44)                   // bpp
1002         || AV_RB32(b + 68) > 256)                                     // colours
1003         return 0;
1004
1005     width = AV_RB32(b + 16);
1006     bpad  = AV_RB32(b + 40);
1007     bpp   = AV_RB32(b + 44);
1008     lsize = AV_RB32(b + 48);
1009     if (lsize < FFALIGN(width * bpp, bpad) >> 3)
1010         return 0;
1011
1012     return AVPROBE_SCORE_MAX / 2 + 1;
1013 }
1014
1015 static int gif_probe(AVProbeData *p)
1016 {
1017     /* check magick */
1018     if (memcmp(p->buf, gif87a_sig, 6) && memcmp(p->buf, gif89a_sig, 6))
1019         return 0;
1020
1021     /* width or height contains zero? */
1022     if (!AV_RL16(&p->buf[6]) || !AV_RL16(&p->buf[8]))
1023         return 0;
1024
1025     return AVPROBE_SCORE_MAX - 1;
1026 }
1027
1028 #define IMAGEAUTO_DEMUXER(imgname, codecid)\
1029 static const AVClass imgname ## _class = {\
1030     .class_name = AV_STRINGIFY(imgname) " demuxer",\
1031     .item_name  = av_default_item_name,\
1032     .option     = ff_img2pipe_options,\
1033     .version    = LIBAVUTIL_VERSION_INT,\
1034 };\
1035 AVInputFormat ff_image_ ## imgname ## _pipe_demuxer = {\
1036     .name           = AV_STRINGIFY(imgname) "_pipe",\
1037     .long_name      = NULL_IF_CONFIG_SMALL("piped " AV_STRINGIFY(imgname) " sequence"),\
1038     .priv_data_size = sizeof(VideoDemuxData),\
1039     .read_probe     = imgname ## _probe,\
1040     .read_header    = ff_img_read_header,\
1041     .read_packet    = ff_img_read_packet,\
1042     .priv_class     = & imgname ## _class,\
1043     .flags          = AVFMT_GENERIC_INDEX, \
1044     .raw_codec_id   = codecid,\
1045 };
1046
1047 IMAGEAUTO_DEMUXER(bmp,     AV_CODEC_ID_BMP)
1048 IMAGEAUTO_DEMUXER(dds,     AV_CODEC_ID_DDS)
1049 IMAGEAUTO_DEMUXER(dpx,     AV_CODEC_ID_DPX)
1050 IMAGEAUTO_DEMUXER(exr,     AV_CODEC_ID_EXR)
1051 IMAGEAUTO_DEMUXER(gif,     AV_CODEC_ID_GIF)
1052 IMAGEAUTO_DEMUXER(j2k,     AV_CODEC_ID_JPEG2000)
1053 IMAGEAUTO_DEMUXER(jpeg,    AV_CODEC_ID_MJPEG)
1054 IMAGEAUTO_DEMUXER(jpegls,  AV_CODEC_ID_JPEGLS)
1055 IMAGEAUTO_DEMUXER(pam,     AV_CODEC_ID_PAM)
1056 IMAGEAUTO_DEMUXER(pbm,     AV_CODEC_ID_PBM)
1057 IMAGEAUTO_DEMUXER(pcx,     AV_CODEC_ID_PCX)
1058 IMAGEAUTO_DEMUXER(pgm,     AV_CODEC_ID_PGM)
1059 IMAGEAUTO_DEMUXER(pgmyuv,  AV_CODEC_ID_PGMYUV)
1060 IMAGEAUTO_DEMUXER(pictor,  AV_CODEC_ID_PICTOR)
1061 IMAGEAUTO_DEMUXER(png,     AV_CODEC_ID_PNG)
1062 IMAGEAUTO_DEMUXER(ppm,     AV_CODEC_ID_PPM)
1063 IMAGEAUTO_DEMUXER(psd,     AV_CODEC_ID_PSD)
1064 IMAGEAUTO_DEMUXER(qdraw,   AV_CODEC_ID_QDRAW)
1065 IMAGEAUTO_DEMUXER(sgi,     AV_CODEC_ID_SGI)
1066 IMAGEAUTO_DEMUXER(sunrast, AV_CODEC_ID_SUNRAST)
1067 IMAGEAUTO_DEMUXER(svg,     AV_CODEC_ID_SVG)
1068 IMAGEAUTO_DEMUXER(tiff,    AV_CODEC_ID_TIFF)
1069 IMAGEAUTO_DEMUXER(webp,    AV_CODEC_ID_WEBP)
1070 IMAGEAUTO_DEMUXER(xpm,     AV_CODEC_ID_XPM)
1071 IMAGEAUTO_DEMUXER(xwd,     AV_CODEC_ID_XWD)