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