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 "libavcodec/xwd.h"
38 #include "subtitles.h"
41 /* Locally define as 0 (bitwise-OR no-op) any missing glob options that
42 are non-posix glibc/bsd extensions. */
44 #define GLOB_NOMAGIC 0
50 #endif /* HAVE_GLOB */
52 static const int sizes[][2] = {
64 static int infer_size(int *width_ptr, int *height_ptr, int size)
68 for (i = 0; i < FF_ARRAY_ELEMS(sizes); i++) {
69 if ((sizes[i][0] * sizes[i][1]) == size) {
70 *width_ptr = sizes[i][0];
71 *height_ptr = sizes[i][1];
79 static int is_glob(const char *path)
85 while (p = strchr(p, '%')) {
90 if (span = strspn(p, "*?[]{}"))
93 /* Did we hit a glob char or get to the end? */
101 * Get index range of image files matched by path.
103 * @param pfirst_index pointer to index updated with the first number in the range
104 * @param plast_index pointer to index updated with the last number in the range
105 * @param path path which has to be matched by the image files in the range
106 * @param start_index minimum accepted value for the first index in the range
107 * @return -1 if no image file could be found
109 static int find_image_range(AVIOContext *pb, int *pfirst_index, int *plast_index,
110 const char *path, int start_index, int start_index_range)
113 int range, last_index, range1, first_index;
115 /* find the first image */
116 for (first_index = start_index; first_index < start_index + start_index_range; first_index++) {
117 if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0) {
120 if (pb || avio_check(buf, AVIO_FLAG_READ) > 0)
124 if (avio_check(buf, AVIO_FLAG_READ) > 0)
127 if (first_index == start_index + start_index_range)
130 /* find the last image */
131 last_index = first_index;
139 if (av_get_frame_filename(buf, sizeof(buf), path,
140 last_index + range1) < 0)
142 if (avio_check(buf, AVIO_FLAG_READ) <= 0)
145 /* just in case... */
146 if (range >= (1 << 30))
149 /* we are sure than image last_index + range exists */
154 *pfirst_index = first_index;
155 *plast_index = last_index;
162 static int img_read_probe(AVProbeData *p)
164 if (p->filename && ff_guess_image2_codec(p->filename)) {
165 if (av_filename_number_test(p->filename))
166 return AVPROBE_SCORE_MAX;
167 else if (is_glob(p->filename))
168 return AVPROBE_SCORE_MAX;
169 else if (p->filename[strcspn(p->filename, "*?{")]) // probably PT_GLOB
170 return AVPROBE_SCORE_EXTENSION + 2; // score chosen to be a tad above the image pipes
171 else if (p->buf_size == 0)
173 else if (av_match_ext(p->filename, "raw") || av_match_ext(p->filename, "gif"))
176 return AVPROBE_SCORE_EXTENSION;
181 int ff_img_read_header(AVFormatContext *s1)
183 VideoDemuxData *s = s1->priv_data;
184 int first_index = 1, last_index = 1;
186 enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
188 s1->ctx_flags |= AVFMTCTX_NOHEADER;
190 st = avformat_new_stream(s1, NULL);
192 return AVERROR(ENOMEM);
195 if (s->pixel_format &&
196 (pix_fmt = av_get_pix_fmt(s->pixel_format)) == AV_PIX_FMT_NONE) {
197 av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n",
199 return AVERROR(EINVAL);
202 av_strlcpy(s->path, s1->url, sizeof(s->path));
207 if (s1->iformat->flags & AVFMT_NOFILE)
211 st->need_parsing = AVSTREAM_PARSE_FULL;
214 if (s->ts_from_file == 2) {
215 #if !HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
216 av_log(s1, AV_LOG_ERROR, "POSIX.1-2008 not supported, nanosecond file timestamps unavailable\n");
217 return AVERROR(ENOSYS);
219 avpriv_set_pts_info(st, 64, 1, 1000000000);
220 } else if (s->ts_from_file)
221 avpriv_set_pts_info(st, 64, 1, 1);
223 avpriv_set_pts_info(st, 64, s->framerate.den, s->framerate.num);
225 if (s->width && s->height) {
226 st->codecpar->width = s->width;
227 st->codecpar->height = s->height;
231 if (s->pattern_type == PT_DEFAULT) {
233 s->pattern_type = PT_NONE;
235 s->pattern_type = PT_GLOB_SEQUENCE;
238 if (s->pattern_type == PT_GLOB_SEQUENCE) {
239 s->use_glob = is_glob(s->path);
242 char *p = s->path, *q, *dup;
246 av_log(s1, AV_LOG_WARNING, "Pattern type 'glob_sequence' is deprecated: "
247 "use pattern_type 'glob' instead\n");
249 dup = q = av_strdup(p);
251 /* Do we have room for the next char and a \ insertion? */
252 if ((p - s->path) >= (sizeof(s->path) - 2))
254 if (*q == '%' && strspn(q + 1, "%*?[]{}"))
256 else if (strspn(q, "\\*?[]{}"))
263 gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
265 return AVERROR(ENOENT);
268 last_index = s->globstate.gl_pathc - 1;
272 if ((s->pattern_type == PT_GLOB_SEQUENCE && !s->use_glob) || s->pattern_type == PT_SEQUENCE) {
273 if (find_image_range(s1->pb, &first_index, &last_index, s->path,
274 s->start_number, s->start_number_range) < 0) {
275 av_log(s1, AV_LOG_ERROR,
276 "Could find no file with path '%s' and index in the range %d-%d\n",
277 s->path, s->start_number, s->start_number + s->start_number_range - 1);
278 return AVERROR(ENOENT);
280 } else if (s->pattern_type == PT_GLOB) {
283 gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
285 return AVERROR(ENOENT);
288 last_index = s->globstate.gl_pathc - 1;
291 av_log(s1, AV_LOG_ERROR,
292 "Pattern type 'glob' was selected but globbing "
293 "is not supported by this libavformat build\n");
294 return AVERROR(ENOSYS);
296 } else if (s->pattern_type != PT_GLOB_SEQUENCE && s->pattern_type != PT_NONE) {
297 av_log(s1, AV_LOG_ERROR,
298 "Unknown value '%d' for pattern_type option\n", s->pattern_type);
299 return AVERROR(EINVAL);
301 s->img_first = first_index;
302 s->img_last = last_index;
303 s->img_number = first_index;
304 /* compute duration */
305 if (!s->ts_from_file) {
307 st->duration = last_index - first_index + 1;
311 if (s1->video_codec_id) {
312 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
313 st->codecpar->codec_id = s1->video_codec_id;
314 } else if (s1->audio_codec_id) {
315 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
316 st->codecpar->codec_id = s1->audio_codec_id;
317 } else if (s1->iformat->raw_codec_id) {
318 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
319 st->codecpar->codec_id = s1->iformat->raw_codec_id;
321 const char *str = strrchr(s->path, '.');
322 s->split_planes = str && !av_strcasecmp(str + 1, "y");
323 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
325 int probe_buffer_size = 2048;
326 uint8_t *probe_buffer = av_realloc(NULL, probe_buffer_size + AVPROBE_PADDING_SIZE);
327 const AVInputFormat *fmt = NULL;
328 void *fmt_iter = NULL;
329 AVProbeData pd = { 0 };
332 return AVERROR(ENOMEM);
334 probe_buffer_size = avio_read(s1->pb, probe_buffer, probe_buffer_size);
335 if (probe_buffer_size < 0) {
336 av_free(probe_buffer);
337 return probe_buffer_size;
339 memset(probe_buffer + probe_buffer_size, 0, AVPROBE_PADDING_SIZE);
341 pd.buf = probe_buffer;
342 pd.buf_size = probe_buffer_size;
343 pd.filename = s1->url;
345 while ((fmt = av_demuxer_iterate(&fmt_iter))) {
346 if (fmt->read_header != ff_img_read_header ||
348 (fmt->flags & AVFMT_NOFILE) ||
351 if (fmt->read_probe(&pd) > 0) {
352 st->codecpar->codec_id = fmt->raw_codec_id;
356 if (s1->flags & AVFMT_FLAG_CUSTOM_IO) {
357 avio_seek(s1->pb, 0, SEEK_SET);
359 ffio_rewind_with_probe_data(s1->pb, &probe_buffer, probe_buffer_size);
361 if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
362 st->codecpar->codec_id = ff_guess_image2_codec(s->path);
363 if (st->codecpar->codec_id == AV_CODEC_ID_LJPEG)
364 st->codecpar->codec_id = AV_CODEC_ID_MJPEG;
365 if (st->codecpar->codec_id == AV_CODEC_ID_ALIAS_PIX) // we cannot distingiush this from BRENDER_PIX
366 st->codecpar->codec_id = AV_CODEC_ID_NONE;
368 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
369 pix_fmt != AV_PIX_FMT_NONE)
370 st->codecpar->format = pix_fmt;
375 int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt)
377 VideoDemuxData *s = s1->priv_data;
378 char filename_bytes[1024];
379 char *filename = filename_bytes;
381 int size[3] = { 0 }, ret[3] = { 0 };
382 AVIOContext *f[3] = { NULL };
383 AVCodecParameters *par = s1->streams[0]->codecpar;
386 /* loop over input */
387 if (s->loop && s->img_number > s->img_last) {
388 s->img_number = s->img_first;
390 if (s->img_number > s->img_last)
392 if (s->pattern_type == PT_NONE) {
393 av_strlcpy(filename_bytes, s->path, sizeof(filename_bytes));
394 } else if (s->use_glob) {
396 filename = s->globstate.gl_pathv[s->img_number];
399 if (av_get_frame_filename(filename_bytes, sizeof(filename_bytes),
401 s->img_number) < 0 && s->img_number > 1)
404 for (i = 0; i < 3; i++) {
406 !strcmp(filename_bytes, s->path) &&
410 } else if (s1->io_open(s1, &f[i], filename, AVIO_FLAG_READ, NULL) < 0) {
413 av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",
417 size[i] = avio_size(f[i]);
419 if (!s->split_planes)
421 filename[strlen(filename) - 1] = 'U' + i;
424 if (par->codec_id == AV_CODEC_ID_NONE) {
425 AVProbeData pd = { 0 };
427 uint8_t header[PROBE_BUF_MIN + AVPROBE_PADDING_SIZE];
431 ret = avio_read(f[0], header, PROBE_BUF_MIN);
434 memset(header + ret, 0, sizeof(header) - ret);
435 avio_skip(f[0], -ret);
438 pd.filename = filename;
440 ifmt = av_probe_input_format3(&pd, 1, &score);
441 if (ifmt && ifmt->read_packet == ff_img_read_packet && ifmt->raw_codec_id)
442 par->codec_id = ifmt->raw_codec_id;
445 if (par->codec_id == AV_CODEC_ID_RAWVIDEO && !par->width)
446 infer_size(&par->width, &par->height, size[0]);
449 if (avio_feof(f[0]) && s->loop && s->is_pipe)
450 avio_seek(f[0], 0, SEEK_SET);
453 if (s->frame_size > 0) {
454 size[0] = s->frame_size;
455 } else if (!s1->streams[0]->parser) {
456 size[0] = avio_size(s1->pb);
462 res = av_new_packet(pkt, size[0] + size[1] + size[2]);
466 pkt->stream_index = 0;
467 pkt->flags |= AV_PKT_FLAG_KEY;
468 if (s->ts_from_file) {
469 struct stat img_stat;
470 if (stat(filename, &img_stat)) {
474 pkt->pts = (int64_t)img_stat.st_mtime;
475 #if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
476 if (s->ts_from_file == 2)
477 pkt->pts = 1000000000*pkt->pts + img_stat.st_mtim.tv_nsec;
479 av_add_index_entry(s1->streams[0], s->img_number, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
480 } else if (!s->is_pipe) {
485 pkt->pos = avio_tell(f[0]);
488 for (i = 0; i < 3; i++) {
490 ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
491 if (s->loop && s->is_pipe && ret[i] == AVERROR_EOF) {
492 if (avio_seek(f[i], 0, SEEK_SET) >= 0) {
494 ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
497 if (!s->is_pipe && f[i] != s1->pb)
498 ff_format_io_close(s1, &f[i]);
504 if (ret[0] <= 0 || ret[1] < 0 || ret[2] < 0) {
505 av_packet_unref(pkt);
508 } else if (ret[1] < 0) {
510 } else if (ret[2] < 0) {
525 for (i = 0; i < 3; i++) {
527 ff_format_io_close(s1, &f[i]);
533 static int img_read_close(struct AVFormatContext* s1)
536 VideoDemuxData *s = s1->priv_data;
538 globfree(&s->globstate);
544 static int img_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
546 VideoDemuxData *s1 = s->priv_data;
547 AVStream *st = s->streams[0];
549 if (s1->ts_from_file) {
550 int index = av_index_search_timestamp(st, timestamp, flags);
553 s1->img_number = st->index_entries[index].pos;
557 if (timestamp < 0 || !s1->loop && timestamp > s1->img_last - s1->img_first)
559 s1->img_number = timestamp%(s1->img_last - s1->img_first + 1) + s1->img_first;
564 #define OFFSET(x) offsetof(VideoDemuxData, x)
565 #define DEC AV_OPT_FLAG_DECODING_PARAM
566 const AVOption ff_img_options[] = {
567 { "framerate", "set the video framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC },
568 { "loop", "force loop over input file sequence", OFFSET(loop), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, DEC },
570 { "pattern_type", "set pattern type", OFFSET(pattern_type), AV_OPT_TYPE_INT, {.i64=PT_DEFAULT}, 0, INT_MAX, DEC, "pattern_type"},
571 { "glob_sequence","select glob/sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB_SEQUENCE}, INT_MIN, INT_MAX, DEC, "pattern_type" },
572 { "glob", "select glob pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB }, INT_MIN, INT_MAX, DEC, "pattern_type" },
573 { "sequence", "select sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_SEQUENCE }, INT_MIN, INT_MAX, DEC, "pattern_type" },
574 { "none", "disable pattern matching", 0, AV_OPT_TYPE_CONST, {.i64=PT_NONE }, INT_MIN, INT_MAX, DEC, "pattern_type" },
576 { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
577 { "start_number", "set first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, DEC },
578 { "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 },
579 { "video_size", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
580 { "frame_size", "force frame size in bytes", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
581 { "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" },
582 { "none", "none", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 2, DEC, "ts_type" },
583 { "sec", "second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 2, DEC, "ts_type" },
584 { "ns", "nano second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 2, DEC, "ts_type" },
588 #if CONFIG_IMAGE2_DEMUXER
589 static const AVClass img2_class = {
590 .class_name = "image2 demuxer",
591 .item_name = av_default_item_name,
592 .option = ff_img_options,
593 .version = LIBAVUTIL_VERSION_INT,
595 AVInputFormat ff_image2_demuxer = {
597 .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
598 .priv_data_size = sizeof(VideoDemuxData),
599 .read_probe = img_read_probe,
600 .read_header = ff_img_read_header,
601 .read_packet = ff_img_read_packet,
602 .read_close = img_read_close,
603 .read_seek = img_read_seek,
604 .flags = AVFMT_NOFILE,
605 .priv_class = &img2_class,
608 #if CONFIG_IMAGE2PIPE_DEMUXER
609 static const AVClass img2pipe_class = {
610 .class_name = "image2pipe demuxer",
611 .item_name = av_default_item_name,
612 .option = ff_img_options,
613 .version = LIBAVUTIL_VERSION_INT,
615 AVInputFormat ff_image2pipe_demuxer = {
616 .name = "image2pipe",
617 .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
618 .priv_data_size = sizeof(VideoDemuxData),
619 .read_header = ff_img_read_header,
620 .read_packet = ff_img_read_packet,
621 .priv_class = &img2pipe_class,
625 static int bmp_probe(AVProbeData *p)
627 const uint8_t *b = p->buf;
630 if (AV_RB16(b) != 0x424d)
633 ihsize = AV_RL32(b+14);
634 if (ihsize < 12 || ihsize > 255)
637 if (!AV_RN32(b + 6)) {
638 return AVPROBE_SCORE_EXTENSION + 1;
640 return AVPROBE_SCORE_EXTENSION / 4;
643 static int dds_probe(AVProbeData *p)
645 const uint8_t *b = p->buf;
647 if ( AV_RB64(b) == 0x444453207c000000
650 return AVPROBE_SCORE_MAX - 1;
654 static int dpx_probe(AVProbeData *p)
656 const uint8_t *b = p->buf;
658 int is_big = (AV_RN32(b) == AV_RN32("SDPX"));
660 if (p->buf_size < 0x304+8)
662 w = is_big ? AV_RB32(p->buf + 0x304) : AV_RL32(p->buf + 0x304);
663 h = is_big ? AV_RB32(p->buf + 0x308) : AV_RL32(p->buf + 0x308);
664 if (w <= 0 || h <= 0)
667 if (is_big || AV_RN32(b) == AV_RN32("XPDS"))
668 return AVPROBE_SCORE_EXTENSION + 1;
672 static int exr_probe(AVProbeData *p)
674 const uint8_t *b = p->buf;
676 if (AV_RL32(b) == 20000630)
677 return AVPROBE_SCORE_EXTENSION + 1;
681 static int j2k_probe(AVProbeData *p)
683 const uint8_t *b = p->buf;
685 if (AV_RB64(b) == 0x0000000c6a502020 ||
686 AV_RB32(b) == 0xff4fff51)
687 return AVPROBE_SCORE_EXTENSION + 1;
691 static int jpeg_probe(AVProbeData *p)
693 const uint8_t *b = p->buf;
696 if (AV_RB16(b) != 0xFFD8 ||
697 AV_RB32(b) == 0xFFD8FFF7)
701 for (i = 0; i < p->buf_size - 3; i++) {
716 i += AV_RB16(&b[i + 2]) + 1;
722 i += AV_RB16(&b[i + 2]) + 1;
723 if (state != SOF0 && state != SOS)
750 i += AV_RB16(&b[i + 2]) + 1;
753 if ( (c > TEM && c < SOF0)
760 return AVPROBE_SCORE_EXTENSION + 1;
762 return AVPROBE_SCORE_EXTENSION / 2;
763 return AVPROBE_SCORE_EXTENSION / 8;
766 static int jpegls_probe(AVProbeData *p)
768 const uint8_t *b = p->buf;
770 if (AV_RB32(b) == 0xffd8fff7)
771 return AVPROBE_SCORE_EXTENSION + 1;
775 static int pcx_probe(AVProbeData *p)
777 const uint8_t *b = p->buf;
779 if ( p->buf_size < 128
783 || av_popcount(b[3]) != 1 || b[3] > 8
784 || AV_RL16(&b[4]) > AV_RL16(&b[8])
785 || AV_RL16(&b[6]) > AV_RL16(&b[10])
789 while (++b < p->buf + 128)
791 return AVPROBE_SCORE_EXTENSION / 4;
793 return AVPROBE_SCORE_EXTENSION + 1;
796 static int qdraw_probe(AVProbeData *p)
798 const uint8_t *b = p->buf;
800 if ( p->buf_size >= 528
801 && (AV_RB64(b + 520) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
804 return AVPROBE_SCORE_MAX * 3 / 4;
805 if ( (AV_RB64(b + 8) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
808 return AVPROBE_SCORE_EXTENSION / 4;
812 static int pictor_probe(AVProbeData *p)
814 const uint8_t *b = p->buf;
816 if (AV_RL16(b) == 0x1234)
817 return AVPROBE_SCORE_EXTENSION / 4;
821 static int png_probe(AVProbeData *p)
823 const uint8_t *b = p->buf;
825 if (AV_RB64(b) == 0x89504e470d0a1a0a)
826 return AVPROBE_SCORE_MAX - 1;
830 static int psd_probe(AVProbeData *p)
832 const uint8_t *b = p->buf;
836 if (AV_RL32(b) == MKTAG('8','B','P','S')) {
842 if ((b[4] == 0) && (b[5] == 1)) {/* version 1 is PSD, version 2 is PSB */
848 if ((AV_RL32(b+6) == 0) && (AV_RL16(b+10) == 0))/* reserved must be 0 */
851 color_mode = AV_RB16(b+24);
852 if ((color_mode <= 9) && (color_mode != 5) && (color_mode != 6))
855 return AVPROBE_SCORE_EXTENSION + ret;
858 static int sgi_probe(AVProbeData *p)
860 const uint8_t *b = p->buf;
862 if (AV_RB16(b) == 474 &&
864 (b[3] & ~3) == 0 && b[3] &&
865 (AV_RB16(b + 4) & ~7) == 0 && AV_RB16(b + 4))
866 return AVPROBE_SCORE_EXTENSION + 1;
870 static int sunrast_probe(AVProbeData *p)
872 const uint8_t *b = p->buf;
874 if (AV_RB32(b) == 0x59a66a95)
875 return AVPROBE_SCORE_EXTENSION + 1;
879 static int svg_probe(AVProbeData *p)
881 const uint8_t *b = p->buf;
882 const uint8_t *end = p->buf + p->buf_size;
884 if (memcmp(p->buf, "<?xml", 5))
887 int inc = ff_subtitles_next_line(b);
893 if (!memcmp(b, "<svg", 4))
894 return AVPROBE_SCORE_EXTENSION + 1;
899 static int tiff_probe(AVProbeData *p)
901 const uint8_t *b = p->buf;
903 if (AV_RB32(b) == 0x49492a00 ||
904 AV_RB32(b) == 0x4D4D002a)
905 return AVPROBE_SCORE_EXTENSION + 1;
909 static int webp_probe(AVProbeData *p)
911 const uint8_t *b = p->buf;
913 if (AV_RB32(b) == 0x52494646 &&
914 AV_RB32(b + 8) == 0x57454250)
915 return AVPROBE_SCORE_MAX - 1;
919 static int pnm_magic_check(const AVProbeData *p, int magic)
921 const uint8_t *b = p->buf;
923 return b[0] == 'P' && b[1] == magic + '0';
926 static inline int pnm_probe(const AVProbeData *p)
928 const uint8_t *b = p->buf;
932 if (b[2] == '\n' && (b[3] == '#' || (b[3] >= '0' && b[3] <= '9')))
933 return AVPROBE_SCORE_EXTENSION + 2;
937 static int pbm_probe(AVProbeData *p)
939 return pnm_magic_check(p, 1) || pnm_magic_check(p, 4) ? pnm_probe(p) : 0;
942 static inline int pgmx_probe(AVProbeData *p)
944 return pnm_magic_check(p, 2) || pnm_magic_check(p, 5) ? pnm_probe(p) : 0;
947 static int pgm_probe(AVProbeData *p)
949 int ret = pgmx_probe(p);
950 return ret && !av_match_ext(p->filename, "pgmyuv") ? ret : 0;
953 static int pgmyuv_probe(AVProbeData *p) // custom FFmpeg format recognized by file extension
955 int ret = pgmx_probe(p);
956 return ret && av_match_ext(p->filename, "pgmyuv") ? ret : 0;
959 static int ppm_probe(AVProbeData *p)
961 return pnm_magic_check(p, 3) || pnm_magic_check(p, 6) ? pnm_probe(p) : 0;
964 static int pam_probe(AVProbeData *p)
966 return pnm_magic_check(p, 7) ? pnm_probe(p) : 0;
969 static int xpm_probe(AVProbeData *p)
971 const uint8_t *b = p->buf;
973 if (AV_RB64(b) == 0x2f2a2058504d202a && *(b+8) == '/')
974 return AVPROBE_SCORE_MAX - 1;
978 static int xwd_probe(AVProbeData *p)
980 const uint8_t *b = p->buf;
981 unsigned width, bpp, bpad, lsize;
983 if ( p->buf_size < XWD_HEADER_SIZE
984 || AV_RB32(b ) < XWD_HEADER_SIZE // header size
985 || AV_RB32(b + 4) != XWD_VERSION // version
986 || AV_RB32(b + 8) != XWD_Z_PIXMAP // format
987 || AV_RB32(b + 12) > 32 || !AV_RB32(b + 12) // depth
988 || AV_RB32(b + 16) == 0 // width
989 || AV_RB32(b + 20) == 0 // height
990 || AV_RB32(b + 28) > 1 // byteorder
991 || AV_RB32(b + 32) & ~56 || av_popcount(AV_RB32(b + 32)) != 1 // bitmap unit
992 || AV_RB32(b + 36) > 1 // bitorder
993 || AV_RB32(b + 40) & ~56 || av_popcount(AV_RB32(b + 40)) != 1 // padding
994 || AV_RB32(b + 44) > 32 || !AV_RB32(b + 44) // bpp
995 || AV_RB32(b + 68) > 256) // colours
998 width = AV_RB32(b + 16);
999 bpad = AV_RB32(b + 40);
1000 bpp = AV_RB32(b + 44);
1001 lsize = AV_RB32(b + 48);
1002 if (lsize < FFALIGN(width * bpp, bpad) >> 3)
1005 return AVPROBE_SCORE_MAX / 2 + 1;
1008 #define IMAGEAUTO_DEMUXER(imgname, codecid)\
1009 static const AVClass imgname ## _class = {\
1010 .class_name = AV_STRINGIFY(imgname) " demuxer",\
1011 .item_name = av_default_item_name,\
1012 .option = ff_img_options,\
1013 .version = LIBAVUTIL_VERSION_INT,\
1015 AVInputFormat ff_image_ ## imgname ## _pipe_demuxer = {\
1016 .name = AV_STRINGIFY(imgname) "_pipe",\
1017 .long_name = NULL_IF_CONFIG_SMALL("piped " AV_STRINGIFY(imgname) " sequence"),\
1018 .priv_data_size = sizeof(VideoDemuxData),\
1019 .read_probe = imgname ## _probe,\
1020 .read_header = ff_img_read_header,\
1021 .read_packet = ff_img_read_packet,\
1022 .priv_class = & imgname ## _class,\
1023 .flags = AVFMT_GENERIC_INDEX, \
1024 .raw_codec_id = codecid,\
1027 IMAGEAUTO_DEMUXER(bmp, AV_CODEC_ID_BMP)
1028 IMAGEAUTO_DEMUXER(dds, AV_CODEC_ID_DDS)
1029 IMAGEAUTO_DEMUXER(dpx, AV_CODEC_ID_DPX)
1030 IMAGEAUTO_DEMUXER(exr, AV_CODEC_ID_EXR)
1031 IMAGEAUTO_DEMUXER(j2k, AV_CODEC_ID_JPEG2000)
1032 IMAGEAUTO_DEMUXER(jpeg, AV_CODEC_ID_MJPEG)
1033 IMAGEAUTO_DEMUXER(jpegls, AV_CODEC_ID_JPEGLS)
1034 IMAGEAUTO_DEMUXER(pam, AV_CODEC_ID_PAM)
1035 IMAGEAUTO_DEMUXER(pbm, AV_CODEC_ID_PBM)
1036 IMAGEAUTO_DEMUXER(pcx, AV_CODEC_ID_PCX)
1037 IMAGEAUTO_DEMUXER(pgm, AV_CODEC_ID_PGM)
1038 IMAGEAUTO_DEMUXER(pgmyuv, AV_CODEC_ID_PGMYUV)
1039 IMAGEAUTO_DEMUXER(pictor, AV_CODEC_ID_PICTOR)
1040 IMAGEAUTO_DEMUXER(png, AV_CODEC_ID_PNG)
1041 IMAGEAUTO_DEMUXER(ppm, AV_CODEC_ID_PPM)
1042 IMAGEAUTO_DEMUXER(psd, AV_CODEC_ID_PSD)
1043 IMAGEAUTO_DEMUXER(qdraw, AV_CODEC_ID_QDRAW)
1044 IMAGEAUTO_DEMUXER(sgi, AV_CODEC_ID_SGI)
1045 IMAGEAUTO_DEMUXER(sunrast, AV_CODEC_ID_SUNRAST)
1046 IMAGEAUTO_DEMUXER(svg, AV_CODEC_ID_SVG)
1047 IMAGEAUTO_DEMUXER(tiff, AV_CODEC_ID_TIFF)
1048 IMAGEAUTO_DEMUXER(webp, AV_CODEC_ID_WEBP)
1049 IMAGEAUTO_DEMUXER(xpm, AV_CODEC_ID_XPM)
1050 IMAGEAUTO_DEMUXER(xwd, AV_CODEC_ID_XWD)