]> git.sesse.net Git - ffmpeg/blob - libavformat/img2dec.c
Merge commit '65db4899fa8790049bec3af16ecdb75dd81051fd'
[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(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 (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_GLOB_SEQUENCE) {
229         s->use_glob = is_glob(s->path);
230         if (s->use_glob) {
231 #if HAVE_GLOB
232             char *p = s->path, *q, *dup;
233             int gerr;
234 #endif
235
236             av_log(s1, AV_LOG_WARNING, "Pattern type 'glob_sequence' is deprecated: "
237                    "use pattern_type 'glob' instead\n");
238 #if HAVE_GLOB
239             dup = q = av_strdup(p);
240             while (*q) {
241                 /* Do we have room for the next char and a \ insertion? */
242                 if ((p - s->path) >= (sizeof(s->path) - 2))
243                   break;
244                 if (*q == '%' && strspn(q + 1, "%*?[]{}"))
245                     ++q;
246                 else if (strspn(q, "\\*?[]{}"))
247                     *p++ = '\\';
248                 *p++ = *q++;
249             }
250             *p = 0;
251             av_free(dup);
252
253             gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
254             if (gerr != 0) {
255                 return AVERROR(ENOENT);
256             }
257             first_index = 0;
258             last_index = s->globstate.gl_pathc - 1;
259 #endif
260         }
261         }
262         if ((s->pattern_type == PT_GLOB_SEQUENCE && !s->use_glob) || s->pattern_type == PT_SEQUENCE) {
263             if (find_image_range(&first_index, &last_index, s->path,
264                                  s->start_number, s->start_number_range) < 0) {
265                 av_log(s1, AV_LOG_ERROR,
266                        "Could find no file with path '%s' and index in the range %d-%d\n",
267                        s->path, s->start_number, s->start_number + s->start_number_range - 1);
268                 return AVERROR(ENOENT);
269             }
270         } else if (s->pattern_type == PT_GLOB) {
271 #if HAVE_GLOB
272             int gerr;
273             gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
274             if (gerr != 0) {
275                 return AVERROR(ENOENT);
276             }
277             first_index = 0;
278             last_index = s->globstate.gl_pathc - 1;
279             s->use_glob = 1;
280 #else
281             av_log(s1, AV_LOG_ERROR,
282                    "Pattern type 'glob' was selected but globbing "
283                    "is not supported by this libavformat build\n");
284             return AVERROR(ENOSYS);
285 #endif
286         } else if (s->pattern_type != PT_GLOB_SEQUENCE && s->pattern_type != PT_NONE) {
287             av_log(s1, AV_LOG_ERROR,
288                    "Unknown value '%d' for pattern_type option\n", s->pattern_type);
289             return AVERROR(EINVAL);
290         }
291         s->img_first  = first_index;
292         s->img_last   = last_index;
293         s->img_number = first_index;
294         /* compute duration */
295         if (!s->ts_from_file) {
296             st->start_time = 0;
297             st->duration   = last_index - first_index + 1;
298         }
299     }
300
301     if (s1->video_codec_id) {
302         st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
303         st->codec->codec_id   = s1->video_codec_id;
304     } else if (s1->audio_codec_id) {
305         st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
306         st->codec->codec_id   = s1->audio_codec_id;
307     } else if (s1->iformat->raw_codec_id) {
308         st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
309         st->codec->codec_id   = s1->iformat->raw_codec_id;
310     } else {
311         const char *str = strrchr(s->path, '.');
312         s->split_planes       = str && !av_strcasecmp(str + 1, "y");
313         st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
314         if (s1->pb) {
315             int probe_buffer_size = 2048;
316             uint8_t *probe_buffer = av_realloc(NULL, probe_buffer_size + AVPROBE_PADDING_SIZE);
317             AVInputFormat *fmt = NULL;
318             AVProbeData pd = { 0 };
319
320             if (!probe_buffer)
321                 return AVERROR(ENOMEM);
322
323             probe_buffer_size = avio_read(s1->pb, probe_buffer, probe_buffer_size);
324             if (probe_buffer_size < 0) {
325                 av_free(probe_buffer);
326                 return probe_buffer_size;
327             }
328             memset(probe_buffer + probe_buffer_size, 0, AVPROBE_PADDING_SIZE);
329
330             pd.buf = probe_buffer;
331             pd.buf_size = probe_buffer_size;
332             pd.filename = s1->filename;
333
334             while ((fmt = av_iformat_next(fmt))) {
335                 if (fmt->read_header != ff_img_read_header ||
336                     !fmt->read_probe ||
337                     (fmt->flags & AVFMT_NOFILE) ||
338                     !fmt->raw_codec_id)
339                     continue;
340                 if (fmt->read_probe(&pd) > 0) {
341                     st->codec->codec_id = fmt->raw_codec_id;
342                     break;
343                 }
344             }
345             if (s1->flags & AVFMT_FLAG_CUSTOM_IO) {
346                 avio_seek(s1->pb, 0, SEEK_SET);
347             } else
348                 ffio_rewind_with_probe_data(s1->pb, &probe_buffer, probe_buffer_size);
349         }
350         if (st->codec->codec_id == AV_CODEC_ID_NONE)
351             st->codec->codec_id = ff_guess_image2_codec(s->path);
352         if (st->codec->codec_id == AV_CODEC_ID_LJPEG)
353             st->codec->codec_id = AV_CODEC_ID_MJPEG;
354         if (st->codec->codec_id == AV_CODEC_ID_ALIAS_PIX) // we cannot distingiush this from BRENDER_PIX
355             st->codec->codec_id = AV_CODEC_ID_NONE;
356     }
357     if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
358         pix_fmt != AV_PIX_FMT_NONE)
359         st->codec->pix_fmt = pix_fmt;
360
361     return 0;
362 }
363
364 int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt)
365 {
366     VideoDemuxData *s = s1->priv_data;
367     char filename_bytes[1024];
368     char *filename = filename_bytes;
369     int i, res;
370     int size[3]           = { 0 }, ret[3] = { 0 };
371     AVIOContext *f[3]     = { NULL };
372     AVCodecContext *codec = s1->streams[0]->codec;
373
374     if (!s->is_pipe) {
375         /* loop over input */
376         if (s->loop && s->img_number > s->img_last) {
377             s->img_number = s->img_first;
378         }
379         if (s->img_number > s->img_last)
380             return AVERROR_EOF;
381         if (s->pattern_type == PT_NONE) {
382             av_strlcpy(filename_bytes, s->path, sizeof(filename_bytes));
383         } else if (s->use_glob) {
384 #if HAVE_GLOB
385             filename = s->globstate.gl_pathv[s->img_number];
386 #endif
387         } else {
388         if (av_get_frame_filename(filename_bytes, sizeof(filename_bytes),
389                                   s->path,
390                                   s->img_number) < 0 && s->img_number > 1)
391             return AVERROR(EIO);
392         }
393         for (i = 0; i < 3; i++) {
394             if (avio_open2(&f[i], filename, AVIO_FLAG_READ,
395                            &s1->interrupt_callback, NULL) < 0) {
396                 if (i >= 1)
397                     break;
398                 av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",
399                        filename);
400                 return AVERROR(EIO);
401             }
402             size[i] = avio_size(f[i]);
403
404             if (!s->split_planes)
405                 break;
406             filename[strlen(filename) - 1] = 'U' + i;
407         }
408
409         if (codec->codec_id == AV_CODEC_ID_NONE) {
410             AVProbeData pd = { 0 };
411             AVInputFormat *ifmt;
412             uint8_t header[PROBE_BUF_MIN + AVPROBE_PADDING_SIZE];
413             int ret;
414             int score = 0;
415
416             ret = avio_read(f[0], header, PROBE_BUF_MIN);
417             if (ret < 0)
418                 return ret;
419             memset(header + ret, 0, sizeof(header) - ret);
420             avio_skip(f[0], -ret);
421             pd.buf = header;
422             pd.buf_size = ret;
423             pd.filename = filename;
424
425             ifmt = av_probe_input_format3(&pd, 1, &score);
426             if (ifmt && ifmt->read_packet == ff_img_read_packet && ifmt->raw_codec_id)
427                 codec->codec_id = ifmt->raw_codec_id;
428         }
429
430         if (codec->codec_id == AV_CODEC_ID_RAWVIDEO && !codec->width)
431             infer_size(&codec->width, &codec->height, size[0]);
432     } else {
433         f[0] = s1->pb;
434         if (avio_feof(f[0]) && s->loop && s->is_pipe)
435             avio_seek(f[0], 0, SEEK_SET);
436         if (avio_feof(f[0]))
437             return AVERROR_EOF;
438         if (s->frame_size > 0) {
439             size[0] = s->frame_size;
440         } else if (!s1->streams[0]->parser) {
441             size[0] = avio_size(s1->pb);
442         } else {
443             size[0] = 4096;
444         }
445     }
446
447     res = av_new_packet(pkt, size[0] + size[1] + size[2]);
448     if (res < 0) {
449         goto fail;
450     }
451     pkt->stream_index = 0;
452     pkt->flags       |= AV_PKT_FLAG_KEY;
453     if (s->ts_from_file) {
454         struct stat img_stat;
455         if (stat(filename, &img_stat)) {
456             res = AVERROR(EIO);
457             goto fail;
458         }
459         pkt->pts = (int64_t)img_stat.st_mtime;
460 #if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
461         if (s->ts_from_file == 2)
462             pkt->pts = 1000000000*pkt->pts + img_stat.st_mtim.tv_nsec;
463 #endif
464         av_add_index_entry(s1->streams[0], s->img_number, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
465     } else if (!s->is_pipe) {
466         pkt->pts      = s->pts;
467     }
468
469     if (s->is_pipe)
470         pkt->pos = avio_tell(f[0]);
471
472     pkt->size = 0;
473     for (i = 0; i < 3; i++) {
474         if (f[i]) {
475             ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
476             if (s->loop && s->is_pipe && ret[i] == AVERROR_EOF) {
477                 if (avio_seek(f[i], 0, SEEK_SET) >= 0) {
478                     pkt->pos = 0;
479                     ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
480                 }
481             }
482             if (!s->is_pipe)
483                 avio_closep(&f[i]);
484             if (ret[i] > 0)
485                 pkt->size += ret[i];
486         }
487     }
488
489     if (ret[0] <= 0 || ret[1] < 0 || ret[2] < 0) {
490         av_free_packet(pkt);
491         if (ret[0] < 0) {
492             res = ret[0];
493         } else if (ret[1] < 0) {
494             res = ret[1];
495         } else if (ret[2] < 0) {
496             res = ret[2];
497         } else {
498             res = AVERROR_EOF;
499         }
500         goto fail;
501     } else {
502         s->img_count++;
503         s->img_number++;
504         s->pts++;
505         return 0;
506     }
507
508 fail:
509     if (!s->is_pipe) {
510         for (i = 0; i < 3; i++) {
511             avio_closep(&f[i]);
512         }
513     }
514     return res;
515 }
516
517 static int img_read_close(struct AVFormatContext* s1)
518 {
519 #if HAVE_GLOB
520     VideoDemuxData *s = s1->priv_data;
521     if (s->use_glob) {
522         globfree(&s->globstate);
523     }
524 #endif
525     return 0;
526 }
527
528 static int img_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
529 {
530     VideoDemuxData *s1 = s->priv_data;
531     AVStream *st = s->streams[0];
532
533     if (s1->ts_from_file) {
534         int index = av_index_search_timestamp(st, timestamp, flags);
535         if(index < 0)
536             return -1;
537         s1->img_number = st->index_entries[index].pos;
538         return 0;
539     }
540
541     if (timestamp < 0 || !s1->loop && timestamp > s1->img_last - s1->img_first)
542         return -1;
543     s1->img_number = timestamp%(s1->img_last - s1->img_first + 1) + s1->img_first;
544     s1->pts = timestamp;
545     return 0;
546 }
547
548 #define OFFSET(x) offsetof(VideoDemuxData, x)
549 #define DEC AV_OPT_FLAG_DECODING_PARAM
550 const AVOption ff_img_options[] = {
551     { "framerate",    "set the video framerate",             OFFSET(framerate),    AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0,   DEC },
552     { "loop",         "force loop over input file sequence", OFFSET(loop),         AV_OPT_TYPE_INT,    {.i64 = 0   }, 0, 1,       DEC },
553
554     { "pattern_type", "set pattern type",                    OFFSET(pattern_type), AV_OPT_TYPE_INT,    {.i64=PT_GLOB_SEQUENCE}, 0,       INT_MAX, DEC, "pattern_type"},
555     { "glob_sequence","select glob/sequence pattern type",   0, AV_OPT_TYPE_CONST,  {.i64=PT_GLOB_SEQUENCE}, INT_MIN, INT_MAX, DEC, "pattern_type" },
556     { "glob",         "select glob pattern type",            0, AV_OPT_TYPE_CONST,  {.i64=PT_GLOB         }, INT_MIN, INT_MAX, DEC, "pattern_type" },
557     { "sequence",     "select sequence pattern type",        0, AV_OPT_TYPE_CONST,  {.i64=PT_SEQUENCE     }, INT_MIN, INT_MAX, DEC, "pattern_type" },
558     { "none",         "disable pattern matching",            0, AV_OPT_TYPE_CONST,  {.i64=PT_NONE         }, INT_MIN, INT_MAX, DEC, "pattern_type" },
559
560     { "pixel_format", "set video pixel format",              OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0,       DEC },
561     { "start_number", "set first number in the sequence",    OFFSET(start_number), AV_OPT_TYPE_INT,    {.i64 = 0   }, INT_MIN, INT_MAX, DEC },
562     { "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 },
563     { "video_size",   "set video size",                      OFFSET(width),        AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0,   DEC },
564     { "frame_size",   "force frame size in bytes",           OFFSET(frame_size),   AV_OPT_TYPE_INT,    {.i64 = 0   }, 0, INT_MAX, DEC },
565     { "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" },
566     { "none", "none",                   0, AV_OPT_TYPE_CONST,    {.i64 = 0   }, 0, 2,       DEC, "ts_type" },
567     { "sec",  "second precision",       0, AV_OPT_TYPE_CONST,    {.i64 = 1   }, 0, 2,       DEC, "ts_type" },
568     { "ns",   "nano second precision",  0, AV_OPT_TYPE_CONST,    {.i64 = 2   }, 0, 2,       DEC, "ts_type" },
569     { NULL },
570 };
571
572 #if CONFIG_IMAGE2_DEMUXER
573 static const AVClass img2_class = {
574     .class_name = "image2 demuxer",
575     .item_name  = av_default_item_name,
576     .option     = ff_img_options,
577     .version    = LIBAVUTIL_VERSION_INT,
578 };
579 AVInputFormat ff_image2_demuxer = {
580     .name           = "image2",
581     .long_name      = NULL_IF_CONFIG_SMALL("image2 sequence"),
582     .priv_data_size = sizeof(VideoDemuxData),
583     .read_probe     = img_read_probe,
584     .read_header    = ff_img_read_header,
585     .read_packet    = ff_img_read_packet,
586     .read_close     = img_read_close,
587     .read_seek      = img_read_seek,
588     .flags          = AVFMT_NOFILE,
589     .priv_class     = &img2_class,
590 };
591 #endif
592 #if CONFIG_IMAGE2PIPE_DEMUXER
593 static const AVClass img2pipe_class = {
594     .class_name = "image2pipe demuxer",
595     .item_name  = av_default_item_name,
596     .option     = ff_img_options,
597     .version    = LIBAVUTIL_VERSION_INT,
598 };
599 AVInputFormat ff_image2pipe_demuxer = {
600     .name           = "image2pipe",
601     .long_name      = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
602     .priv_data_size = sizeof(VideoDemuxData),
603     .read_header    = ff_img_read_header,
604     .read_packet    = ff_img_read_packet,
605     .priv_class     = &img2pipe_class,
606 };
607 #endif
608
609 static int bmp_probe(AVProbeData *p)
610 {
611     const uint8_t *b = p->buf;
612     int ihsize;
613
614     if (AV_RB16(b) != 0x424d)
615         return 0;
616
617     ihsize = AV_RL32(b+14);
618     if (ihsize < 12 || ihsize > 255)
619         return 0;
620
621     if (!AV_RN32(b + 6)) {
622         return AVPROBE_SCORE_EXTENSION + 1;
623     }
624     return AVPROBE_SCORE_EXTENSION / 4;
625 }
626
627 static int dds_probe(AVProbeData *p)
628 {
629     const uint8_t *b = p->buf;
630
631     if (   AV_RB64(b) == 0x444453207c000000
632         && AV_RL32(b +  8)
633         && AV_RL32(b + 12))
634         return AVPROBE_SCORE_MAX - 1;
635     return 0;
636 }
637
638 static int dpx_probe(AVProbeData *p)
639 {
640     const uint8_t *b = p->buf;
641     int w, h;
642     int is_big = (AV_RN32(b) == AV_RN32("SDPX"));
643
644     if (p->buf_size < 0x304+8)
645         return 0;
646     w = is_big ? AV_RB32(p->buf + 0x304) : AV_RL32(p->buf + 0x304);
647     h = is_big ? AV_RB32(p->buf + 0x308) : AV_RL32(p->buf + 0x308);
648     if (w <= 0 || h <= 0)
649         return 0;
650
651     if (is_big || AV_RN32(b) == AV_RN32("XPDS"))
652         return AVPROBE_SCORE_EXTENSION + 1;
653     return 0;
654 }
655
656 static int exr_probe(AVProbeData *p)
657 {
658     const uint8_t *b = p->buf;
659
660     if (AV_RL32(b) == 20000630)
661         return AVPROBE_SCORE_EXTENSION + 1;
662     return 0;
663 }
664
665 static int j2k_probe(AVProbeData *p)
666 {
667     const uint8_t *b = p->buf;
668
669     if (AV_RB64(b) == 0x0000000c6a502020 ||
670         AV_RB32(b) == 0xff4fff51)
671         return AVPROBE_SCORE_EXTENSION + 1;
672     return 0;
673 }
674
675 static int jpeg_probe(AVProbeData *p)
676 {
677     const uint8_t *b = p->buf;
678     int i, state = 0xD8;
679
680     if (AV_RB16(b) != 0xFFD8 ||
681         AV_RB32(b) == 0xFFD8FFF7)
682     return 0;
683
684     b += 2;
685     for (i = 0; i < p->buf_size - 3; i++) {
686         int c;
687         if (b[i] != 0xFF)
688             continue;
689         c = b[i + 1];
690         switch (c) {
691         case 0xD8:
692             return 0;
693         case 0xC0:
694         case 0xC1:
695         case 0xC2:
696         case 0xC3:
697         case 0xC5:
698         case 0xC6:
699         case 0xC7:
700             if (state != 0xD8)
701                 return 0;
702             state = 0xC0;
703             break;
704         case 0xDA:
705             if (state != 0xC0)
706                 return 0;
707             state = 0xDA;
708             break;
709         case 0xD9:
710             if (state != 0xDA)
711                 return 0;
712             state = 0xD9;
713             break;
714         case 0xE0:
715         case 0xE1:
716         case 0xE2:
717         case 0xE3:
718         case 0xE4:
719         case 0xE5:
720         case 0xE6:
721         case 0xE7:
722         case 0xE8:
723         case 0xE9:
724         case 0xEA:
725         case 0xEB:
726         case 0xEC:
727         case 0xED:
728         case 0xEE:
729         case 0xEF:
730             i += AV_RB16(&b[i + 2]) + 1;
731             break;
732         default:
733             if (  (c >= 0x02 && c <= 0xBF)
734                 || c == 0xC8)
735                 return 0;
736         }
737     }
738
739     if (state == 0xD9)
740         return AVPROBE_SCORE_EXTENSION + 1;
741     return AVPROBE_SCORE_EXTENSION / 8;
742 }
743
744 static int jpegls_probe(AVProbeData *p)
745 {
746     const uint8_t *b = p->buf;
747
748     if (AV_RB32(b) == 0xffd8fff7)
749          return AVPROBE_SCORE_EXTENSION + 1;
750     return 0;
751 }
752
753 static int qdraw_probe(AVProbeData *p)
754 {
755     const uint8_t *b = p->buf;
756
757     if (   p->buf_size >= 528
758         && (AV_RB64(b + 520) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
759         && AV_RB16(b + 520)
760         && AV_RB16(b + 518))
761         return AVPROBE_SCORE_MAX * 3 / 4;
762     if (   (AV_RB64(b + 8) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
763         && AV_RB16(b + 8)
764         && AV_RB16(b + 6))
765         return AVPROBE_SCORE_EXTENSION / 4;
766     return 0;
767 }
768
769 static int pictor_probe(AVProbeData *p)
770 {
771     const uint8_t *b = p->buf;
772
773     if (AV_RL16(b) == 0x1234)
774         return AVPROBE_SCORE_EXTENSION / 4;
775     return 0;
776 }
777
778 static int png_probe(AVProbeData *p)
779 {
780     const uint8_t *b = p->buf;
781
782     if (AV_RB64(b) == 0x89504e470d0a1a0a)
783         return AVPROBE_SCORE_MAX - 1;
784     return 0;
785 }
786
787 static int sgi_probe(AVProbeData *p)
788 {
789     const uint8_t *b = p->buf;
790
791     if (AV_RB16(b) == 474 &&
792         (b[2] & ~1) == 0 &&
793         (b[3] & ~3) == 0 && b[3] &&
794         (AV_RB16(b + 4) & ~7) == 0 && AV_RB16(b + 4))
795         return AVPROBE_SCORE_EXTENSION + 1;
796     return 0;
797 }
798
799 static int sunrast_probe(AVProbeData *p)
800 {
801     const uint8_t *b = p->buf;
802
803     if (AV_RB32(b) == 0x59a66a95)
804         return AVPROBE_SCORE_EXTENSION + 1;
805     return 0;
806 }
807
808 static int tiff_probe(AVProbeData *p)
809 {
810     const uint8_t *b = p->buf;
811
812     if (AV_RB32(b) == 0x49492a00 ||
813         AV_RB32(b) == 0x4D4D002a)
814         return AVPROBE_SCORE_EXTENSION + 1;
815     return 0;
816 }
817
818 static int webp_probe(AVProbeData *p)
819 {
820     const uint8_t *b = p->buf;
821
822     if (AV_RB32(b)     == 0x52494646 &&
823         AV_RB32(b + 8) == 0x57454250)
824         return AVPROBE_SCORE_MAX - 1;
825     return 0;
826 }
827
828 #define IMAGEAUTO_DEMUXER(imgname, codecid)\
829 static const AVClass imgname ## _class = {\
830     .class_name = AV_STRINGIFY(imgname) " demuxer",\
831     .item_name  = av_default_item_name,\
832     .option     = ff_img_options,\
833     .version    = LIBAVUTIL_VERSION_INT,\
834 };\
835 AVInputFormat ff_image_ ## imgname ## _pipe_demuxer = {\
836     .name           = AV_STRINGIFY(imgname) "_pipe",\
837     .long_name      = NULL_IF_CONFIG_SMALL("piped " AV_STRINGIFY(imgname) " sequence"),\
838     .priv_data_size = sizeof(VideoDemuxData),\
839     .read_probe     = imgname ## _probe,\
840     .read_header    = ff_img_read_header,\
841     .read_packet    = ff_img_read_packet,\
842     .priv_class     = & imgname ## _class,\
843     .flags          = AVFMT_GENERIC_INDEX, \
844     .raw_codec_id   = codecid,\
845 };
846
847 IMAGEAUTO_DEMUXER(bmp,     AV_CODEC_ID_BMP)
848 IMAGEAUTO_DEMUXER(dds,     AV_CODEC_ID_DDS)
849 IMAGEAUTO_DEMUXER(dpx,     AV_CODEC_ID_DPX)
850 IMAGEAUTO_DEMUXER(exr,     AV_CODEC_ID_EXR)
851 IMAGEAUTO_DEMUXER(j2k,     AV_CODEC_ID_JPEG2000)
852 IMAGEAUTO_DEMUXER(jpeg,    AV_CODEC_ID_MJPEG)
853 IMAGEAUTO_DEMUXER(jpegls,  AV_CODEC_ID_JPEGLS)
854 IMAGEAUTO_DEMUXER(pictor,  AV_CODEC_ID_PICTOR)
855 IMAGEAUTO_DEMUXER(png,     AV_CODEC_ID_PNG)
856 IMAGEAUTO_DEMUXER(qdraw,   AV_CODEC_ID_QDRAW)
857 IMAGEAUTO_DEMUXER(sgi,     AV_CODEC_ID_SGI)
858 IMAGEAUTO_DEMUXER(sunrast, AV_CODEC_ID_SUNRAST)
859 IMAGEAUTO_DEMUXER(tiff,    AV_CODEC_ID_TIFF)
860 IMAGEAUTO_DEMUXER(webp,    AV_CODEC_ID_WEBP)