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