3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 * Copyright (c) 2004 Michael Niedermayer
6 * This file is part of FFmpeg.
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.
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.
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
23 #define _DEFAULT_SOURCE
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"
33 #include "avio_internal.h"
36 #include "libavcodec/mjpeg.h"
37 #include "subtitles.h"
40 /* Locally define as 0 (bitwise-OR no-op) any missing glob options that
41 are non-posix glibc/bsd extensions. */
43 #define GLOB_NOMAGIC 0
49 #endif /* HAVE_GLOB */
51 static const int sizes[][2] = {
63 static int infer_size(int *width_ptr, int *height_ptr, int size)
67 for (i = 0; i < FF_ARRAY_ELEMS(sizes); i++) {
68 if ((sizes[i][0] * sizes[i][1]) == size) {
69 *width_ptr = sizes[i][0];
70 *height_ptr = sizes[i][1];
78 static int is_glob(const char *path)
84 while (p = strchr(p, '%')) {
89 if (span = strspn(p, "*?[]{}"))
92 /* Did we hit a glob char or get to the end? */
100 * Get index range of image files matched by path.
102 * @param pfirst_index pointer to index updated with the first number in the range
103 * @param plast_index pointer to index updated with the last number in the range
104 * @param path path which has to be matched by the image files in the range
105 * @param start_index minimum accepted value for the first index in the range
106 * @return -1 if no image file could be found
108 static int find_image_range(AVIOContext *pb, int *pfirst_index, int *plast_index,
109 const char *path, int start_index, int start_index_range)
112 int range, last_index, range1, first_index;
114 /* find the first image */
115 for (first_index = start_index; first_index < start_index + start_index_range; first_index++) {
116 if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0) {
119 if (pb || avio_check(buf, AVIO_FLAG_READ) > 0)
123 if (avio_check(buf, AVIO_FLAG_READ) > 0)
126 if (first_index == start_index + start_index_range)
129 /* find the last image */
130 last_index = first_index;
138 if (av_get_frame_filename(buf, sizeof(buf), path,
139 last_index + range1) < 0)
141 if (avio_check(buf, AVIO_FLAG_READ) <= 0)
144 /* just in case... */
145 if (range >= (1 << 30))
148 /* we are sure than image last_index + range exists */
153 *pfirst_index = first_index;
154 *plast_index = last_index;
161 static int img_read_probe(AVProbeData *p)
163 if (p->filename && ff_guess_image2_codec(p->filename)) {
164 if (av_filename_number_test(p->filename))
165 return AVPROBE_SCORE_MAX;
166 else if (is_glob(p->filename))
167 return AVPROBE_SCORE_MAX;
168 else if (p->filename[strcspn(p->filename, "*?{")]) // probably PT_GLOB
169 return AVPROBE_SCORE_EXTENSION + 2; // score chosen to be a tad above the image pipes
170 else if (p->buf_size == 0)
172 else if (av_match_ext(p->filename, "raw") || av_match_ext(p->filename, "gif"))
175 return AVPROBE_SCORE_EXTENSION;
180 int ff_img_read_header(AVFormatContext *s1)
182 VideoDemuxData *s = s1->priv_data;
183 int first_index = 1, last_index = 1;
185 enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
187 s1->ctx_flags |= AVFMTCTX_NOHEADER;
189 st = avformat_new_stream(s1, NULL);
191 return AVERROR(ENOMEM);
194 if (s->pixel_format &&
195 (pix_fmt = av_get_pix_fmt(s->pixel_format)) == AV_PIX_FMT_NONE) {
196 av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n",
198 return AVERROR(EINVAL);
201 av_strlcpy(s->path, s1->filename, sizeof(s->path));
206 if (s1->iformat->flags & AVFMT_NOFILE)
210 st->need_parsing = AVSTREAM_PARSE_FULL;
213 if (s->ts_from_file == 2) {
214 #if !HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
215 av_log(s1, AV_LOG_ERROR, "POSIX.1-2008 not supported, nanosecond file timestamps unavailable\n");
216 return AVERROR(ENOSYS);
218 avpriv_set_pts_info(st, 64, 1, 1000000000);
219 } else if (s->ts_from_file)
220 avpriv_set_pts_info(st, 64, 1, 1);
222 avpriv_set_pts_info(st, 64, s->framerate.den, s->framerate.num);
224 if (s->width && s->height) {
225 st->codecpar->width = s->width;
226 st->codecpar->height = s->height;
230 if (s->pattern_type == PT_DEFAULT) {
232 s->pattern_type = PT_NONE;
234 s->pattern_type = PT_GLOB_SEQUENCE;
237 if (s->pattern_type == PT_GLOB_SEQUENCE) {
238 s->use_glob = is_glob(s->path);
241 char *p = s->path, *q, *dup;
245 av_log(s1, AV_LOG_WARNING, "Pattern type 'glob_sequence' is deprecated: "
246 "use pattern_type 'glob' instead\n");
248 dup = q = av_strdup(p);
250 /* Do we have room for the next char and a \ insertion? */
251 if ((p - s->path) >= (sizeof(s->path) - 2))
253 if (*q == '%' && strspn(q + 1, "%*?[]{}"))
255 else if (strspn(q, "\\*?[]{}"))
262 gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
264 return AVERROR(ENOENT);
267 last_index = s->globstate.gl_pathc - 1;
271 if ((s->pattern_type == PT_GLOB_SEQUENCE && !s->use_glob) || s->pattern_type == PT_SEQUENCE) {
272 if (find_image_range(s1->pb, &first_index, &last_index, s->path,
273 s->start_number, s->start_number_range) < 0) {
274 av_log(s1, AV_LOG_ERROR,
275 "Could find no file with path '%s' and index in the range %d-%d\n",
276 s->path, s->start_number, s->start_number + s->start_number_range - 1);
277 return AVERROR(ENOENT);
279 } else if (s->pattern_type == PT_GLOB) {
282 gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
284 return AVERROR(ENOENT);
287 last_index = s->globstate.gl_pathc - 1;
290 av_log(s1, AV_LOG_ERROR,
291 "Pattern type 'glob' was selected but globbing "
292 "is not supported by this libavformat build\n");
293 return AVERROR(ENOSYS);
295 } else if (s->pattern_type != PT_GLOB_SEQUENCE && s->pattern_type != PT_NONE) {
296 av_log(s1, AV_LOG_ERROR,
297 "Unknown value '%d' for pattern_type option\n", s->pattern_type);
298 return AVERROR(EINVAL);
300 s->img_first = first_index;
301 s->img_last = last_index;
302 s->img_number = first_index;
303 /* compute duration */
304 if (!s->ts_from_file) {
306 st->duration = last_index - first_index + 1;
310 if (s1->video_codec_id) {
311 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
312 st->codecpar->codec_id = s1->video_codec_id;
313 } else if (s1->audio_codec_id) {
314 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
315 st->codecpar->codec_id = s1->audio_codec_id;
316 } else if (s1->iformat->raw_codec_id) {
317 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
318 st->codecpar->codec_id = s1->iformat->raw_codec_id;
320 const char *str = strrchr(s->path, '.');
321 s->split_planes = str && !av_strcasecmp(str + 1, "y");
322 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
324 int probe_buffer_size = 2048;
325 uint8_t *probe_buffer = av_realloc(NULL, probe_buffer_size + AVPROBE_PADDING_SIZE);
326 AVInputFormat *fmt = NULL;
327 AVProbeData pd = { 0 };
330 return AVERROR(ENOMEM);
332 probe_buffer_size = avio_read(s1->pb, probe_buffer, probe_buffer_size);
333 if (probe_buffer_size < 0) {
334 av_free(probe_buffer);
335 return probe_buffer_size;
337 memset(probe_buffer + probe_buffer_size, 0, AVPROBE_PADDING_SIZE);
339 pd.buf = probe_buffer;
340 pd.buf_size = probe_buffer_size;
341 pd.filename = s1->filename;
343 while ((fmt = av_iformat_next(fmt))) {
344 if (fmt->read_header != ff_img_read_header ||
346 (fmt->flags & AVFMT_NOFILE) ||
349 if (fmt->read_probe(&pd) > 0) {
350 st->codecpar->codec_id = fmt->raw_codec_id;
354 if (s1->flags & AVFMT_FLAG_CUSTOM_IO) {
355 avio_seek(s1->pb, 0, SEEK_SET);
357 ffio_rewind_with_probe_data(s1->pb, &probe_buffer, probe_buffer_size);
359 if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
360 st->codecpar->codec_id = ff_guess_image2_codec(s->path);
361 if (st->codecpar->codec_id == AV_CODEC_ID_LJPEG)
362 st->codecpar->codec_id = AV_CODEC_ID_MJPEG;
363 if (st->codecpar->codec_id == AV_CODEC_ID_ALIAS_PIX) // we cannot distingiush this from BRENDER_PIX
364 st->codecpar->codec_id = AV_CODEC_ID_NONE;
366 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
367 pix_fmt != AV_PIX_FMT_NONE)
368 st->codecpar->format = pix_fmt;
373 int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt)
375 VideoDemuxData *s = s1->priv_data;
376 char filename_bytes[1024];
377 char *filename = filename_bytes;
379 int size[3] = { 0 }, ret[3] = { 0 };
380 AVIOContext *f[3] = { NULL };
381 AVCodecParameters *par = s1->streams[0]->codecpar;
384 /* loop over input */
385 if (s->loop && s->img_number > s->img_last) {
386 s->img_number = s->img_first;
388 if (s->img_number > s->img_last)
390 if (s->pattern_type == PT_NONE) {
391 av_strlcpy(filename_bytes, s->path, sizeof(filename_bytes));
392 } else if (s->use_glob) {
394 filename = s->globstate.gl_pathv[s->img_number];
397 if (av_get_frame_filename(filename_bytes, sizeof(filename_bytes),
399 s->img_number) < 0 && s->img_number > 1)
402 for (i = 0; i < 3; i++) {
404 !strcmp(filename_bytes, s->path) &&
408 } else if (s1->io_open(s1, &f[i], filename, AVIO_FLAG_READ, NULL) < 0) {
411 av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",
415 size[i] = avio_size(f[i]);
417 if (!s->split_planes)
419 filename[strlen(filename) - 1] = 'U' + i;
422 if (par->codec_id == AV_CODEC_ID_NONE) {
423 AVProbeData pd = { 0 };
425 uint8_t header[PROBE_BUF_MIN + AVPROBE_PADDING_SIZE];
429 ret = avio_read(f[0], header, PROBE_BUF_MIN);
432 memset(header + ret, 0, sizeof(header) - ret);
433 avio_skip(f[0], -ret);
436 pd.filename = filename;
438 ifmt = av_probe_input_format3(&pd, 1, &score);
439 if (ifmt && ifmt->read_packet == ff_img_read_packet && ifmt->raw_codec_id)
440 par->codec_id = ifmt->raw_codec_id;
443 if (par->codec_id == AV_CODEC_ID_RAWVIDEO && !par->width)
444 infer_size(&par->width, &par->height, size[0]);
447 if (avio_feof(f[0]) && s->loop && s->is_pipe)
448 avio_seek(f[0], 0, SEEK_SET);
451 if (s->frame_size > 0) {
452 size[0] = s->frame_size;
453 } else if (!s1->streams[0]->parser) {
454 size[0] = avio_size(s1->pb);
460 res = av_new_packet(pkt, size[0] + size[1] + size[2]);
464 pkt->stream_index = 0;
465 pkt->flags |= AV_PKT_FLAG_KEY;
466 if (s->ts_from_file) {
467 struct stat img_stat;
468 if (stat(filename, &img_stat)) {
472 pkt->pts = (int64_t)img_stat.st_mtime;
473 #if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
474 if (s->ts_from_file == 2)
475 pkt->pts = 1000000000*pkt->pts + img_stat.st_mtim.tv_nsec;
477 av_add_index_entry(s1->streams[0], s->img_number, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
478 } else if (!s->is_pipe) {
483 pkt->pos = avio_tell(f[0]);
486 for (i = 0; i < 3; i++) {
488 ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
489 if (s->loop && s->is_pipe && ret[i] == AVERROR_EOF) {
490 if (avio_seek(f[i], 0, SEEK_SET) >= 0) {
492 ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
495 if (!s->is_pipe && f[i] != s1->pb)
496 ff_format_io_close(s1, &f[i]);
502 if (ret[0] <= 0 || ret[1] < 0 || ret[2] < 0) {
503 av_packet_unref(pkt);
506 } else if (ret[1] < 0) {
508 } else if (ret[2] < 0) {
523 for (i = 0; i < 3; i++) {
525 ff_format_io_close(s1, &f[i]);
531 static int img_read_close(struct AVFormatContext* s1)
534 VideoDemuxData *s = s1->priv_data;
536 globfree(&s->globstate);
542 static int img_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
544 VideoDemuxData *s1 = s->priv_data;
545 AVStream *st = s->streams[0];
547 if (s1->ts_from_file) {
548 int index = av_index_search_timestamp(st, timestamp, flags);
551 s1->img_number = st->index_entries[index].pos;
555 if (timestamp < 0 || !s1->loop && timestamp > s1->img_last - s1->img_first)
557 s1->img_number = timestamp%(s1->img_last - s1->img_first + 1) + s1->img_first;
562 #define OFFSET(x) offsetof(VideoDemuxData, x)
563 #define DEC AV_OPT_FLAG_DECODING_PARAM
564 const AVOption ff_img_options[] = {
565 { "framerate", "set the video framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC },
566 { "loop", "force loop over input file sequence", OFFSET(loop), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, DEC },
568 { "pattern_type", "set pattern type", OFFSET(pattern_type), AV_OPT_TYPE_INT, {.i64=PT_DEFAULT}, 0, INT_MAX, DEC, "pattern_type"},
569 { "glob_sequence","select glob/sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB_SEQUENCE}, INT_MIN, INT_MAX, DEC, "pattern_type" },
570 { "glob", "select glob pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB }, INT_MIN, INT_MAX, DEC, "pattern_type" },
571 { "sequence", "select sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_SEQUENCE }, INT_MIN, INT_MAX, DEC, "pattern_type" },
572 { "none", "disable pattern matching", 0, AV_OPT_TYPE_CONST, {.i64=PT_NONE }, INT_MIN, INT_MAX, DEC, "pattern_type" },
574 { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
575 { "start_number", "set first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, DEC },
576 { "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 },
577 { "video_size", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
578 { "frame_size", "force frame size in bytes", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
579 { "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" },
580 { "none", "none", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 2, DEC, "ts_type" },
581 { "sec", "second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 2, DEC, "ts_type" },
582 { "ns", "nano second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 2, DEC, "ts_type" },
586 #if CONFIG_IMAGE2_DEMUXER
587 static const AVClass img2_class = {
588 .class_name = "image2 demuxer",
589 .item_name = av_default_item_name,
590 .option = ff_img_options,
591 .version = LIBAVUTIL_VERSION_INT,
593 AVInputFormat ff_image2_demuxer = {
595 .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
596 .priv_data_size = sizeof(VideoDemuxData),
597 .read_probe = img_read_probe,
598 .read_header = ff_img_read_header,
599 .read_packet = ff_img_read_packet,
600 .read_close = img_read_close,
601 .read_seek = img_read_seek,
602 .flags = AVFMT_NOFILE,
603 .priv_class = &img2_class,
606 #if CONFIG_IMAGE2PIPE_DEMUXER
607 static const AVClass img2pipe_class = {
608 .class_name = "image2pipe demuxer",
609 .item_name = av_default_item_name,
610 .option = ff_img_options,
611 .version = LIBAVUTIL_VERSION_INT,
613 AVInputFormat ff_image2pipe_demuxer = {
614 .name = "image2pipe",
615 .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
616 .priv_data_size = sizeof(VideoDemuxData),
617 .read_header = ff_img_read_header,
618 .read_packet = ff_img_read_packet,
619 .priv_class = &img2pipe_class,
623 static int bmp_probe(AVProbeData *p)
625 const uint8_t *b = p->buf;
628 if (AV_RB16(b) != 0x424d)
631 ihsize = AV_RL32(b+14);
632 if (ihsize < 12 || ihsize > 255)
635 if (!AV_RN32(b + 6)) {
636 return AVPROBE_SCORE_EXTENSION + 1;
638 return AVPROBE_SCORE_EXTENSION / 4;
641 static int dds_probe(AVProbeData *p)
643 const uint8_t *b = p->buf;
645 if ( AV_RB64(b) == 0x444453207c000000
648 return AVPROBE_SCORE_MAX - 1;
652 static int dpx_probe(AVProbeData *p)
654 const uint8_t *b = p->buf;
656 int is_big = (AV_RN32(b) == AV_RN32("SDPX"));
658 if (p->buf_size < 0x304+8)
660 w = is_big ? AV_RB32(p->buf + 0x304) : AV_RL32(p->buf + 0x304);
661 h = is_big ? AV_RB32(p->buf + 0x308) : AV_RL32(p->buf + 0x308);
662 if (w <= 0 || h <= 0)
665 if (is_big || AV_RN32(b) == AV_RN32("XPDS"))
666 return AVPROBE_SCORE_EXTENSION + 1;
670 static int exr_probe(AVProbeData *p)
672 const uint8_t *b = p->buf;
674 if (AV_RL32(b) == 20000630)
675 return AVPROBE_SCORE_EXTENSION + 1;
679 static int j2k_probe(AVProbeData *p)
681 const uint8_t *b = p->buf;
683 if (AV_RB64(b) == 0x0000000c6a502020 ||
684 AV_RB32(b) == 0xff4fff51)
685 return AVPROBE_SCORE_EXTENSION + 1;
689 static int jpeg_probe(AVProbeData *p)
691 const uint8_t *b = p->buf;
694 if (AV_RB16(b) != 0xFFD8 ||
695 AV_RB32(b) == 0xFFD8FFF7)
699 for (i = 0; i < p->buf_size - 3; i++) {
714 i += AV_RB16(&b[i + 2]) + 1;
720 i += AV_RB16(&b[i + 2]) + 1;
721 if (state != SOF0 && state != SOS)
748 i += AV_RB16(&b[i + 2]) + 1;
751 if ( (c > TEM && c < SOF0)
758 return AVPROBE_SCORE_EXTENSION + 1;
760 return AVPROBE_SCORE_EXTENSION / 2;
761 return AVPROBE_SCORE_EXTENSION / 8;
764 static int jpegls_probe(AVProbeData *p)
766 const uint8_t *b = p->buf;
768 if (AV_RB32(b) == 0xffd8fff7)
769 return AVPROBE_SCORE_EXTENSION + 1;
773 static int pcx_probe(AVProbeData *p)
775 const uint8_t *b = p->buf;
777 if ( p->buf_size < 128
781 || av_popcount(b[3]) != 1 || b[3] > 8
782 || AV_RL16(&b[4]) > AV_RL16(&b[8])
783 || AV_RL16(&b[6]) > AV_RL16(&b[10])
787 while (++b < p->buf + 128)
789 return AVPROBE_SCORE_EXTENSION / 4;
791 return AVPROBE_SCORE_EXTENSION + 1;
794 static int qdraw_probe(AVProbeData *p)
796 const uint8_t *b = p->buf;
798 if ( p->buf_size >= 528
799 && (AV_RB64(b + 520) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
802 return AVPROBE_SCORE_MAX * 3 / 4;
803 if ( (AV_RB64(b + 8) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
806 return AVPROBE_SCORE_EXTENSION / 4;
810 static int pictor_probe(AVProbeData *p)
812 const uint8_t *b = p->buf;
814 if (AV_RL16(b) == 0x1234)
815 return AVPROBE_SCORE_EXTENSION / 4;
819 static int png_probe(AVProbeData *p)
821 const uint8_t *b = p->buf;
823 if (AV_RB64(b) == 0x89504e470d0a1a0a)
824 return AVPROBE_SCORE_MAX - 1;
828 static int psd_probe(AVProbeData *p)
830 const uint8_t *b = p->buf;
834 if (AV_RL32(b) == MKTAG('8','B','P','S')) {
840 if ((b[4] == 0) && (b[5] == 1)) {/* version 1 is PSD, version 2 is PSB */
846 if ((AV_RL32(b+6) == 0) && (AV_RL16(b+10) == 0))/* reserved must be 0 */
849 color_mode = AV_RB16(b+24);
850 if ((color_mode <= 9) && (color_mode != 5) && (color_mode != 6))
853 return AVPROBE_SCORE_EXTENSION + ret;
856 static int sgi_probe(AVProbeData *p)
858 const uint8_t *b = p->buf;
860 if (AV_RB16(b) == 474 &&
862 (b[3] & ~3) == 0 && b[3] &&
863 (AV_RB16(b + 4) & ~7) == 0 && AV_RB16(b + 4))
864 return AVPROBE_SCORE_EXTENSION + 1;
868 static int sunrast_probe(AVProbeData *p)
870 const uint8_t *b = p->buf;
872 if (AV_RB32(b) == 0x59a66a95)
873 return AVPROBE_SCORE_EXTENSION + 1;
877 static int svg_probe(AVProbeData *p)
879 const uint8_t *b = p->buf;
880 const uint8_t *end = p->buf + p->buf_size;
881 if (memcmp(p->buf, "<?xml", 5))
884 b += ff_subtitles_next_line(b);
887 if (!memcmp(b, "<svg", 4))
888 return AVPROBE_SCORE_EXTENSION + 1;
893 static int tiff_probe(AVProbeData *p)
895 const uint8_t *b = p->buf;
897 if (AV_RB32(b) == 0x49492a00 ||
898 AV_RB32(b) == 0x4D4D002a)
899 return AVPROBE_SCORE_EXTENSION + 1;
903 static int webp_probe(AVProbeData *p)
905 const uint8_t *b = p->buf;
907 if (AV_RB32(b) == 0x52494646 &&
908 AV_RB32(b + 8) == 0x57454250)
909 return AVPROBE_SCORE_MAX - 1;
913 static int pnm_magic_check(const AVProbeData *p, int magic)
915 const uint8_t *b = p->buf;
917 return b[0] == 'P' && b[1] == magic + '0';
920 static inline int pnm_probe(const AVProbeData *p)
922 const uint8_t *b = p->buf;
926 if (b[2] == '\n' && (b[3] == '#' || (b[3] >= '0' && b[3] <= '9')))
927 return AVPROBE_SCORE_EXTENSION + 2;
931 static int pbm_probe(AVProbeData *p)
933 return pnm_magic_check(p, 1) || pnm_magic_check(p, 4) ? pnm_probe(p) : 0;
936 static inline int pgmx_probe(AVProbeData *p)
938 return pnm_magic_check(p, 2) || pnm_magic_check(p, 5) ? pnm_probe(p) : 0;
941 static int pgm_probe(AVProbeData *p)
943 int ret = pgmx_probe(p);
944 return ret && !av_match_ext(p->filename, "pgmyuv") ? ret : 0;
947 static int pgmyuv_probe(AVProbeData *p) // custom FFmpeg format recognized by file extension
949 int ret = pgmx_probe(p);
950 return ret && av_match_ext(p->filename, "pgmyuv") ? ret : 0;
953 static int ppm_probe(AVProbeData *p)
955 return pnm_magic_check(p, 3) || pnm_magic_check(p, 6) ? pnm_probe(p) : 0;
958 static int pam_probe(AVProbeData *p)
960 return pnm_magic_check(p, 7) ? pnm_probe(p) : 0;
963 static int xpm_probe(AVProbeData *p)
965 const uint8_t *b = p->buf;
967 if (AV_RB64(b) == 0x2f2a2058504d202a && *(b+8) == '/')
968 return AVPROBE_SCORE_MAX - 1;
972 #define IMAGEAUTO_DEMUXER(imgname, codecid)\
973 static const AVClass imgname ## _class = {\
974 .class_name = AV_STRINGIFY(imgname) " demuxer",\
975 .item_name = av_default_item_name,\
976 .option = ff_img_options,\
977 .version = LIBAVUTIL_VERSION_INT,\
979 AVInputFormat ff_image_ ## imgname ## _pipe_demuxer = {\
980 .name = AV_STRINGIFY(imgname) "_pipe",\
981 .long_name = NULL_IF_CONFIG_SMALL("piped " AV_STRINGIFY(imgname) " sequence"),\
982 .priv_data_size = sizeof(VideoDemuxData),\
983 .read_probe = imgname ## _probe,\
984 .read_header = ff_img_read_header,\
985 .read_packet = ff_img_read_packet,\
986 .priv_class = & imgname ## _class,\
987 .flags = AVFMT_GENERIC_INDEX, \
988 .raw_codec_id = codecid,\
991 IMAGEAUTO_DEMUXER(bmp, AV_CODEC_ID_BMP)
992 IMAGEAUTO_DEMUXER(dds, AV_CODEC_ID_DDS)
993 IMAGEAUTO_DEMUXER(dpx, AV_CODEC_ID_DPX)
994 IMAGEAUTO_DEMUXER(exr, AV_CODEC_ID_EXR)
995 IMAGEAUTO_DEMUXER(j2k, AV_CODEC_ID_JPEG2000)
996 IMAGEAUTO_DEMUXER(jpeg, AV_CODEC_ID_MJPEG)
997 IMAGEAUTO_DEMUXER(jpegls, AV_CODEC_ID_JPEGLS)
998 IMAGEAUTO_DEMUXER(pam, AV_CODEC_ID_PAM)
999 IMAGEAUTO_DEMUXER(pbm, AV_CODEC_ID_PBM)
1000 IMAGEAUTO_DEMUXER(pcx, AV_CODEC_ID_PCX)
1001 IMAGEAUTO_DEMUXER(pgm, AV_CODEC_ID_PGM)
1002 IMAGEAUTO_DEMUXER(pgmyuv, AV_CODEC_ID_PGMYUV)
1003 IMAGEAUTO_DEMUXER(pictor, AV_CODEC_ID_PICTOR)
1004 IMAGEAUTO_DEMUXER(png, AV_CODEC_ID_PNG)
1005 IMAGEAUTO_DEMUXER(ppm, AV_CODEC_ID_PPM)
1006 IMAGEAUTO_DEMUXER(psd, AV_CODEC_ID_PSD)
1007 IMAGEAUTO_DEMUXER(qdraw, AV_CODEC_ID_QDRAW)
1008 IMAGEAUTO_DEMUXER(sgi, AV_CODEC_ID_SGI)
1009 IMAGEAUTO_DEMUXER(sunrast, AV_CODEC_ID_SUNRAST)
1010 IMAGEAUTO_DEMUXER(svg, AV_CODEC_ID_SVG)
1011 IMAGEAUTO_DEMUXER(tiff, AV_CODEC_ID_TIFF)
1012 IMAGEAUTO_DEMUXER(webp, AV_CODEC_ID_WEBP)
1013 IMAGEAUTO_DEMUXER(xpm, AV_CODEC_ID_XPM)