]> git.sesse.net Git - ffmpeg/blob - libavformat/img2dec.c
avformat: add windows.h to SChannel SSP TLS code
[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     AVOpenCallback open_func = s1->open_cb;
381
382     if (!open_func)
383         open_func = ffio_open2_wrapper;
384
385     if (!s->is_pipe) {
386         /* loop over input */
387         if (s->loop && s->img_number > s->img_last) {
388             s->img_number = s->img_first;
389         }
390         if (s->img_number > s->img_last)
391             return AVERROR_EOF;
392         if (s->pattern_type == PT_NONE) {
393             av_strlcpy(filename_bytes, s->path, sizeof(filename_bytes));
394         } else if (s->use_glob) {
395 #if HAVE_GLOB
396             filename = s->globstate.gl_pathv[s->img_number];
397 #endif
398         } else {
399         if (av_get_frame_filename(filename_bytes, sizeof(filename_bytes),
400                                   s->path,
401                                   s->img_number) < 0 && s->img_number > 1)
402             return AVERROR(EIO);
403         }
404         for (i = 0; i < 3; i++) {
405             if (s1->pb &&
406                 !strcmp(filename_bytes, s->path) &&
407                 !s->loop &&
408                 !s->split_planes) {
409                 f[i] = s1->pb;
410             } else if (open_func(s1, &f[i], filename, AVIO_FLAG_READ,
411                            &s1->interrupt_callback, 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 (codec->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                 codec->codec_id = ifmt->raw_codec_id;
444         }
445
446         if (codec->codec_id == AV_CODEC_ID_RAWVIDEO && !codec->width)
447             infer_size(&codec->width, &codec->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                 avio_closep(&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                 avio_closep(&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 const AVOption ff_img_options[] = {
568     { "framerate",    "set the video framerate",             OFFSET(framerate),    AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0,   DEC },
569     { "loop",         "force loop over input file sequence", OFFSET(loop),         AV_OPT_TYPE_BOOL,   {.i64 = 0   }, 0, 1,       DEC },
570
571     { "pattern_type", "set pattern type",                    OFFSET(pattern_type), AV_OPT_TYPE_INT,    {.i64=PT_DEFAULT}, 0,       INT_MAX, DEC, "pattern_type"},
572     { "glob_sequence","select glob/sequence pattern type",   0, AV_OPT_TYPE_CONST,  {.i64=PT_GLOB_SEQUENCE}, INT_MIN, INT_MAX, DEC, "pattern_type" },
573     { "glob",         "select glob pattern type",            0, AV_OPT_TYPE_CONST,  {.i64=PT_GLOB         }, INT_MIN, INT_MAX, DEC, "pattern_type" },
574     { "sequence",     "select sequence pattern type",        0, AV_OPT_TYPE_CONST,  {.i64=PT_SEQUENCE     }, INT_MIN, INT_MAX, DEC, "pattern_type" },
575     { "none",         "disable pattern matching",            0, AV_OPT_TYPE_CONST,  {.i64=PT_NONE         }, INT_MIN, INT_MAX, DEC, "pattern_type" },
576
577     { "pixel_format", "set video pixel format",              OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0,       DEC },
578     { "start_number", "set first number in the sequence",    OFFSET(start_number), AV_OPT_TYPE_INT,    {.i64 = 0   }, INT_MIN, INT_MAX, DEC },
579     { "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 },
580     { "video_size",   "set video size",                      OFFSET(width),        AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0,   DEC },
581     { "frame_size",   "force frame size in bytes",           OFFSET(frame_size),   AV_OPT_TYPE_INT,    {.i64 = 0   }, 0, INT_MAX, DEC },
582     { "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" },
583     { "none", "none",                   0, AV_OPT_TYPE_CONST,    {.i64 = 0   }, 0, 2,       DEC, "ts_type" },
584     { "sec",  "second precision",       0, AV_OPT_TYPE_CONST,    {.i64 = 1   }, 0, 2,       DEC, "ts_type" },
585     { "ns",   "nano second precision",  0, AV_OPT_TYPE_CONST,    {.i64 = 2   }, 0, 2,       DEC, "ts_type" },
586     { NULL },
587 };
588
589 #if CONFIG_IMAGE2_DEMUXER
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 #if CONFIG_IMAGE2PIPE_DEMUXER
610 static const AVClass img2pipe_class = {
611     .class_name = "image2pipe demuxer",
612     .item_name  = av_default_item_name,
613     .option     = ff_img_options,
614     .version    = LIBAVUTIL_VERSION_INT,
615 };
616 AVInputFormat ff_image2pipe_demuxer = {
617     .name           = "image2pipe",
618     .long_name      = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
619     .priv_data_size = sizeof(VideoDemuxData),
620     .read_header    = ff_img_read_header,
621     .read_packet    = ff_img_read_packet,
622     .priv_class     = &img2pipe_class,
623 };
624 #endif
625
626 static int bmp_probe(AVProbeData *p)
627 {
628     const uint8_t *b = p->buf;
629     int ihsize;
630
631     if (AV_RB16(b) != 0x424d)
632         return 0;
633
634     ihsize = AV_RL32(b+14);
635     if (ihsize < 12 || ihsize > 255)
636         return 0;
637
638     if (!AV_RN32(b + 6)) {
639         return AVPROBE_SCORE_EXTENSION + 1;
640     }
641     return AVPROBE_SCORE_EXTENSION / 4;
642 }
643
644 static int dds_probe(AVProbeData *p)
645 {
646     const uint8_t *b = p->buf;
647
648     if (   AV_RB64(b) == 0x444453207c000000
649         && AV_RL32(b +  8)
650         && AV_RL32(b + 12))
651         return AVPROBE_SCORE_MAX - 1;
652     return 0;
653 }
654
655 static int dpx_probe(AVProbeData *p)
656 {
657     const uint8_t *b = p->buf;
658     int w, h;
659     int is_big = (AV_RN32(b) == AV_RN32("SDPX"));
660
661     if (p->buf_size < 0x304+8)
662         return 0;
663     w = is_big ? AV_RB32(p->buf + 0x304) : AV_RL32(p->buf + 0x304);
664     h = is_big ? AV_RB32(p->buf + 0x308) : AV_RL32(p->buf + 0x308);
665     if (w <= 0 || h <= 0)
666         return 0;
667
668     if (is_big || AV_RN32(b) == AV_RN32("XPDS"))
669         return AVPROBE_SCORE_EXTENSION + 1;
670     return 0;
671 }
672
673 static int exr_probe(AVProbeData *p)
674 {
675     const uint8_t *b = p->buf;
676
677     if (AV_RL32(b) == 20000630)
678         return AVPROBE_SCORE_EXTENSION + 1;
679     return 0;
680 }
681
682 static int j2k_probe(AVProbeData *p)
683 {
684     const uint8_t *b = p->buf;
685
686     if (AV_RB64(b) == 0x0000000c6a502020 ||
687         AV_RB32(b) == 0xff4fff51)
688         return AVPROBE_SCORE_EXTENSION + 1;
689     return 0;
690 }
691
692 static int jpeg_probe(AVProbeData *p)
693 {
694     const uint8_t *b = p->buf;
695     int i, state = 0xD8;
696
697     if (AV_RB16(b) != 0xFFD8 ||
698         AV_RB32(b) == 0xFFD8FFF7)
699     return 0;
700
701     b += 2;
702     for (i = 0; i < p->buf_size - 3; i++) {
703         int c;
704         if (b[i] != 0xFF)
705             continue;
706         c = b[i + 1];
707         switch (c) {
708         case 0xD8:
709             return 0;
710         case 0xC0:
711         case 0xC1:
712         case 0xC2:
713         case 0xC3:
714         case 0xC5:
715         case 0xC6:
716         case 0xC7:
717             if (state != 0xD8)
718                 return 0;
719             state = 0xC0;
720             break;
721         case 0xDA:
722             if (state != 0xC0)
723                 return 0;
724             state = 0xDA;
725             break;
726         case 0xD9:
727             if (state != 0xDA)
728                 return 0;
729             state = 0xD9;
730             break;
731         case 0xE0:
732         case 0xE1:
733         case 0xE2:
734         case 0xE3:
735         case 0xE4:
736         case 0xE5:
737         case 0xE6:
738         case 0xE7:
739         case 0xE8:
740         case 0xE9:
741         case 0xEA:
742         case 0xEB:
743         case 0xEC:
744         case 0xED:
745         case 0xEE:
746         case 0xEF:
747             i += AV_RB16(&b[i + 2]) + 1;
748             break;
749         default:
750             if (  (c >= 0x02 && c <= 0xBF)
751                 || c == 0xC8)
752                 return 0;
753         }
754     }
755
756     if (state == 0xD9)
757         return AVPROBE_SCORE_EXTENSION + 1;
758     return AVPROBE_SCORE_EXTENSION / 8;
759 }
760
761 static int jpegls_probe(AVProbeData *p)
762 {
763     const uint8_t *b = p->buf;
764
765     if (AV_RB32(b) == 0xffd8fff7)
766          return AVPROBE_SCORE_EXTENSION + 1;
767     return 0;
768 }
769
770 static int qdraw_probe(AVProbeData *p)
771 {
772     const uint8_t *b = p->buf;
773
774     if (   p->buf_size >= 528
775         && (AV_RB64(b + 520) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
776         && AV_RB16(b + 520)
777         && AV_RB16(b + 518))
778         return AVPROBE_SCORE_MAX * 3 / 4;
779     if (   (AV_RB64(b + 8) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
780         && AV_RB16(b + 8)
781         && AV_RB16(b + 6))
782         return AVPROBE_SCORE_EXTENSION / 4;
783     return 0;
784 }
785
786 static int pictor_probe(AVProbeData *p)
787 {
788     const uint8_t *b = p->buf;
789
790     if (AV_RL16(b) == 0x1234)
791         return AVPROBE_SCORE_EXTENSION / 4;
792     return 0;
793 }
794
795 static int png_probe(AVProbeData *p)
796 {
797     const uint8_t *b = p->buf;
798
799     if (AV_RB64(b) == 0x89504e470d0a1a0a)
800         return AVPROBE_SCORE_MAX - 1;
801     return 0;
802 }
803
804 static int sgi_probe(AVProbeData *p)
805 {
806     const uint8_t *b = p->buf;
807
808     if (AV_RB16(b) == 474 &&
809         (b[2] & ~1) == 0 &&
810         (b[3] & ~3) == 0 && b[3] &&
811         (AV_RB16(b + 4) & ~7) == 0 && AV_RB16(b + 4))
812         return AVPROBE_SCORE_EXTENSION + 1;
813     return 0;
814 }
815
816 static int sunrast_probe(AVProbeData *p)
817 {
818     const uint8_t *b = p->buf;
819
820     if (AV_RB32(b) == 0x59a66a95)
821         return AVPROBE_SCORE_EXTENSION + 1;
822     return 0;
823 }
824
825 static int tiff_probe(AVProbeData *p)
826 {
827     const uint8_t *b = p->buf;
828
829     if (AV_RB32(b) == 0x49492a00 ||
830         AV_RB32(b) == 0x4D4D002a)
831         return AVPROBE_SCORE_EXTENSION + 1;
832     return 0;
833 }
834
835 static int webp_probe(AVProbeData *p)
836 {
837     const uint8_t *b = p->buf;
838
839     if (AV_RB32(b)     == 0x52494646 &&
840         AV_RB32(b + 8) == 0x57454250)
841         return AVPROBE_SCORE_MAX - 1;
842     return 0;
843 }
844
845 #define IMAGEAUTO_DEMUXER(imgname, codecid)\
846 static const AVClass imgname ## _class = {\
847     .class_name = AV_STRINGIFY(imgname) " demuxer",\
848     .item_name  = av_default_item_name,\
849     .option     = ff_img_options,\
850     .version    = LIBAVUTIL_VERSION_INT,\
851 };\
852 AVInputFormat ff_image_ ## imgname ## _pipe_demuxer = {\
853     .name           = AV_STRINGIFY(imgname) "_pipe",\
854     .long_name      = NULL_IF_CONFIG_SMALL("piped " AV_STRINGIFY(imgname) " sequence"),\
855     .priv_data_size = sizeof(VideoDemuxData),\
856     .read_probe     = imgname ## _probe,\
857     .read_header    = ff_img_read_header,\
858     .read_packet    = ff_img_read_packet,\
859     .priv_class     = & imgname ## _class,\
860     .flags          = AVFMT_GENERIC_INDEX, \
861     .raw_codec_id   = codecid,\
862 };
863
864 IMAGEAUTO_DEMUXER(bmp,     AV_CODEC_ID_BMP)
865 IMAGEAUTO_DEMUXER(dds,     AV_CODEC_ID_DDS)
866 IMAGEAUTO_DEMUXER(dpx,     AV_CODEC_ID_DPX)
867 IMAGEAUTO_DEMUXER(exr,     AV_CODEC_ID_EXR)
868 IMAGEAUTO_DEMUXER(j2k,     AV_CODEC_ID_JPEG2000)
869 IMAGEAUTO_DEMUXER(jpeg,    AV_CODEC_ID_MJPEG)
870 IMAGEAUTO_DEMUXER(jpegls,  AV_CODEC_ID_JPEGLS)
871 IMAGEAUTO_DEMUXER(pictor,  AV_CODEC_ID_PICTOR)
872 IMAGEAUTO_DEMUXER(png,     AV_CODEC_ID_PNG)
873 IMAGEAUTO_DEMUXER(qdraw,   AV_CODEC_ID_QDRAW)
874 IMAGEAUTO_DEMUXER(sgi,     AV_CODEC_ID_SGI)
875 IMAGEAUTO_DEMUXER(sunrast, AV_CODEC_ID_SUNRAST)
876 IMAGEAUTO_DEMUXER(tiff,    AV_CODEC_ID_TIFF)
877 IMAGEAUTO_DEMUXER(webp,    AV_CODEC_ID_WEBP)