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"
32 #include "libavcodec/gif.h"
34 #include "avio_internal.h"
37 #include "libavcodec/mjpeg.h"
38 #include "libavcodec/xwd.h"
39 #include "subtitles.h"
42 /* Locally define as 0 (bitwise-OR no-op) any missing glob options that
43 are non-posix glibc/bsd extensions. */
45 #define GLOB_NOMAGIC 0
51 #endif /* HAVE_GLOB */
53 static const int sizes[][2] = {
65 static int infer_size(int *width_ptr, int *height_ptr, int size)
69 for (i = 0; i < FF_ARRAY_ELEMS(sizes); i++) {
70 if ((sizes[i][0] * sizes[i][1]) == size) {
71 *width_ptr = sizes[i][0];
72 *height_ptr = sizes[i][1];
80 static int is_glob(const char *path)
86 while (p = strchr(p, '%')) {
91 if (span = strspn(p, "*?[]{}"))
94 /* Did we hit a glob char or get to the end? */
102 * Get index range of image files matched by path.
104 * @param pfirst_index pointer to index updated with the first number in the range
105 * @param plast_index pointer to index updated with the last number in the range
106 * @param path path which has to be matched by the image files in the range
107 * @param start_index minimum accepted value for the first index in the range
108 * @return -1 if no image file could be found
110 static int find_image_range(AVIOContext *pb, int *pfirst_index, int *plast_index,
111 const char *path, int start_index, int start_index_range)
114 int range, last_index, range1, first_index;
116 /* find the first image */
117 for (first_index = start_index; first_index < start_index + start_index_range; first_index++) {
118 if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0) {
121 if (pb || avio_check(buf, AVIO_FLAG_READ) > 0)
125 if (avio_check(buf, AVIO_FLAG_READ) > 0)
128 if (first_index == start_index + start_index_range)
131 /* find the last image */
132 last_index = first_index;
140 if (av_get_frame_filename(buf, sizeof(buf), path,
141 last_index + range1) < 0)
143 if (avio_check(buf, AVIO_FLAG_READ) <= 0)
146 /* just in case... */
147 if (range >= (1 << 30))
150 /* we are sure than image last_index + range exists */
155 *pfirst_index = first_index;
156 *plast_index = last_index;
163 static int img_read_probe(const AVProbeData *p)
165 if (p->filename && ff_guess_image2_codec(p->filename)) {
166 if (av_filename_number_test(p->filename))
167 return AVPROBE_SCORE_MAX;
168 else if (is_glob(p->filename))
169 return AVPROBE_SCORE_MAX;
170 else if (p->filename[strcspn(p->filename, "*?{")]) // probably PT_GLOB
171 return AVPROBE_SCORE_EXTENSION + 2; // score chosen to be a tad above the image pipes
172 else if (p->buf_size == 0)
174 else if (av_match_ext(p->filename, "raw") || av_match_ext(p->filename, "gif"))
177 return AVPROBE_SCORE_EXTENSION;
182 int ff_img_read_header(AVFormatContext *s1)
184 VideoDemuxData *s = s1->priv_data;
185 int first_index = 1, last_index = 1;
187 enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
189 s1->ctx_flags |= AVFMTCTX_NOHEADER;
191 st = avformat_new_stream(s1, NULL);
193 return AVERROR(ENOMEM);
196 if (s->pixel_format &&
197 (pix_fmt = av_get_pix_fmt(s->pixel_format)) == AV_PIX_FMT_NONE) {
198 av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n",
200 return AVERROR(EINVAL);
203 av_strlcpy(s->path, s1->url, sizeof(s->path));
208 if (s1->iformat->flags & AVFMT_NOFILE)
212 st->need_parsing = AVSTREAM_PARSE_FULL;
215 if (s->ts_from_file == 2) {
216 #if !HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
217 av_log(s1, AV_LOG_ERROR, "POSIX.1-2008 not supported, nanosecond file timestamps unavailable\n");
218 return AVERROR(ENOSYS);
220 avpriv_set_pts_info(st, 64, 1, 1000000000);
221 } else if (s->ts_from_file)
222 avpriv_set_pts_info(st, 64, 1, 1);
224 avpriv_set_pts_info(st, 64, s->framerate.den, s->framerate.num);
226 if (s->width && s->height) {
227 st->codecpar->width = s->width;
228 st->codecpar->height = s->height;
232 if (s->pattern_type == PT_DEFAULT) {
234 s->pattern_type = PT_NONE;
236 s->pattern_type = PT_GLOB_SEQUENCE;
239 if (s->pattern_type == PT_GLOB_SEQUENCE) {
240 s->use_glob = is_glob(s->path);
243 char *p = s->path, *q, *dup;
247 av_log(s1, AV_LOG_WARNING, "Pattern type 'glob_sequence' is deprecated: "
248 "use pattern_type 'glob' instead\n");
250 dup = q = av_strdup(p);
252 /* Do we have room for the next char and a \ insertion? */
253 if ((p - s->path) >= (sizeof(s->path) - 2))
255 if (*q == '%' && strspn(q + 1, "%*?[]{}"))
257 else if (strspn(q, "\\*?[]{}"))
264 gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
266 return AVERROR(ENOENT);
269 last_index = s->globstate.gl_pathc - 1;
273 if ((s->pattern_type == PT_GLOB_SEQUENCE && !s->use_glob) || s->pattern_type == PT_SEQUENCE) {
274 if (find_image_range(s1->pb, &first_index, &last_index, s->path,
275 s->start_number, s->start_number_range) < 0) {
276 av_log(s1, AV_LOG_ERROR,
277 "Could find no file with path '%s' and index in the range %d-%d\n",
278 s->path, s->start_number, s->start_number + s->start_number_range - 1);
279 return AVERROR(ENOENT);
281 } else if (s->pattern_type == PT_GLOB) {
284 gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
286 return AVERROR(ENOENT);
289 last_index = s->globstate.gl_pathc - 1;
292 av_log(s1, AV_LOG_ERROR,
293 "Pattern type 'glob' was selected but globbing "
294 "is not supported by this libavformat build\n");
295 return AVERROR(ENOSYS);
297 } else if (s->pattern_type != PT_GLOB_SEQUENCE && s->pattern_type != PT_NONE) {
298 av_log(s1, AV_LOG_ERROR,
299 "Unknown value '%d' for pattern_type option\n", s->pattern_type);
300 return AVERROR(EINVAL);
302 s->img_first = first_index;
303 s->img_last = last_index;
304 s->img_number = first_index;
305 /* compute duration */
306 if (!s->ts_from_file) {
308 st->duration = last_index - first_index + 1;
312 if (s1->video_codec_id) {
313 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
314 st->codecpar->codec_id = s1->video_codec_id;
315 } else if (s1->audio_codec_id) {
316 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
317 st->codecpar->codec_id = s1->audio_codec_id;
318 } else if (s1->iformat->raw_codec_id) {
319 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
320 st->codecpar->codec_id = s1->iformat->raw_codec_id;
322 const char *str = strrchr(s->path, '.');
323 s->split_planes = str && !av_strcasecmp(str + 1, "y");
324 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
326 int probe_buffer_size = 2048;
327 uint8_t *probe_buffer = av_realloc(NULL, probe_buffer_size + AVPROBE_PADDING_SIZE);
328 const AVInputFormat *fmt = NULL;
329 void *fmt_iter = NULL;
330 AVProbeData pd = { 0 };
333 return AVERROR(ENOMEM);
335 probe_buffer_size = avio_read(s1->pb, probe_buffer, probe_buffer_size);
336 if (probe_buffer_size < 0) {
337 av_free(probe_buffer);
338 return probe_buffer_size;
340 memset(probe_buffer + probe_buffer_size, 0, AVPROBE_PADDING_SIZE);
342 pd.buf = probe_buffer;
343 pd.buf_size = probe_buffer_size;
344 pd.filename = s1->url;
346 while ((fmt = av_demuxer_iterate(&fmt_iter))) {
347 if (fmt->read_header != ff_img_read_header ||
349 (fmt->flags & AVFMT_NOFILE) ||
352 if (fmt->read_probe(&pd) > 0) {
353 st->codecpar->codec_id = fmt->raw_codec_id;
357 if (s1->flags & AVFMT_FLAG_CUSTOM_IO) {
358 avio_seek(s1->pb, 0, SEEK_SET);
359 av_freep(&probe_buffer);
361 ffio_rewind_with_probe_data(s1->pb, &probe_buffer, probe_buffer_size);
363 if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
364 st->codecpar->codec_id = ff_guess_image2_codec(s->path);
365 if (st->codecpar->codec_id == AV_CODEC_ID_LJPEG)
366 st->codecpar->codec_id = AV_CODEC_ID_MJPEG;
367 if (st->codecpar->codec_id == AV_CODEC_ID_ALIAS_PIX) // we cannot distingiush this from BRENDER_PIX
368 st->codecpar->codec_id = AV_CODEC_ID_NONE;
370 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
371 pix_fmt != AV_PIX_FMT_NONE)
372 st->codecpar->format = pix_fmt;
377 int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt)
379 VideoDemuxData *s = s1->priv_data;
380 char filename_bytes[1024];
381 char *filename = filename_bytes;
383 int size[3] = { 0 }, ret[3] = { 0 };
384 AVIOContext *f[3] = { NULL };
385 AVCodecParameters *par = s1->streams[0]->codecpar;
388 /* loop over input */
389 if (s->loop && s->img_number > s->img_last) {
390 s->img_number = s->img_first;
392 if (s->img_number > s->img_last)
394 if (s->pattern_type == PT_NONE) {
395 av_strlcpy(filename_bytes, s->path, sizeof(filename_bytes));
396 } else if (s->use_glob) {
398 filename = s->globstate.gl_pathv[s->img_number];
401 if (av_get_frame_filename(filename_bytes, sizeof(filename_bytes),
403 s->img_number) < 0 && s->img_number > 1)
406 for (i = 0; i < 3; i++) {
408 !strcmp(filename_bytes, s->path) &&
412 } else if (s1->io_open(s1, &f[i], filename, AVIO_FLAG_READ, NULL) < 0) {
415 av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",
419 size[i] = avio_size(f[i]);
421 if (!s->split_planes)
423 filename[strlen(filename) - 1] = 'U' + i;
426 if (par->codec_id == AV_CODEC_ID_NONE) {
427 AVProbeData pd = { 0 };
428 const AVInputFormat *ifmt;
429 uint8_t header[PROBE_BUF_MIN + AVPROBE_PADDING_SIZE];
433 ret = avio_read(f[0], header, PROBE_BUF_MIN);
436 memset(header + ret, 0, sizeof(header) - ret);
437 avio_skip(f[0], -ret);
440 pd.filename = filename;
442 ifmt = av_probe_input_format3(&pd, 1, &score);
443 if (ifmt && ifmt->read_packet == ff_img_read_packet && ifmt->raw_codec_id)
444 par->codec_id = ifmt->raw_codec_id;
447 if (par->codec_id == AV_CODEC_ID_RAWVIDEO && !par->width)
448 infer_size(&par->width, &par->height, size[0]);
451 if (avio_feof(f[0]) && s->loop && s->is_pipe)
452 avio_seek(f[0], 0, SEEK_SET);
455 if (s->frame_size > 0) {
456 size[0] = s->frame_size;
457 } else if (!s1->streams[0]->parser) {
458 size[0] = avio_size(s1->pb);
464 res = av_new_packet(pkt, size[0] + size[1] + size[2]);
468 pkt->stream_index = 0;
469 pkt->flags |= AV_PKT_FLAG_KEY;
470 if (s->ts_from_file) {
471 struct stat img_stat;
472 if (stat(filename, &img_stat)) {
476 pkt->pts = (int64_t)img_stat.st_mtime;
477 #if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
478 if (s->ts_from_file == 2)
479 pkt->pts = 1000000000*pkt->pts + img_stat.st_mtim.tv_nsec;
481 av_add_index_entry(s1->streams[0], s->img_number, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
482 } else if (!s->is_pipe) {
487 pkt->pos = avio_tell(f[0]);
490 for (i = 0; i < 3; i++) {
492 ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
493 if (s->loop && s->is_pipe && ret[i] == AVERROR_EOF) {
494 if (avio_seek(f[i], 0, SEEK_SET) >= 0) {
496 ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
499 if (!s->is_pipe && f[i] != s1->pb)
500 ff_format_io_close(s1, &f[i]);
506 if (ret[0] <= 0 || ret[1] < 0 || ret[2] < 0) {
507 av_packet_unref(pkt);
510 } else if (ret[1] < 0) {
512 } else if (ret[2] < 0) {
527 for (i = 0; i < 3; i++) {
529 ff_format_io_close(s1, &f[i]);
535 static int img_read_close(struct AVFormatContext* s1)
538 VideoDemuxData *s = s1->priv_data;
540 globfree(&s->globstate);
546 static int img_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
548 VideoDemuxData *s1 = s->priv_data;
549 AVStream *st = s->streams[0];
551 if (s1->ts_from_file) {
552 int index = av_index_search_timestamp(st, timestamp, flags);
555 s1->img_number = st->index_entries[index].pos;
559 if (timestamp < 0 || !s1->loop && timestamp > s1->img_last - s1->img_first)
561 s1->img_number = timestamp%(s1->img_last - s1->img_first + 1) + s1->img_first;
566 #define OFFSET(x) offsetof(VideoDemuxData, x)
567 #define DEC AV_OPT_FLAG_DECODING_PARAM
568 #define COMMON_OPTIONS \
569 { "framerate", "set the video framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC }, \
570 { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, \
571 { "video_size", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC }, \
572 { "loop", "force loop over input file sequence", OFFSET(loop), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, DEC }, \
575 #if CONFIG_IMAGE2_DEMUXER
576 const AVOption ff_img_options[] = {
577 { "pattern_type", "set pattern type", OFFSET(pattern_type), AV_OPT_TYPE_INT, {.i64=PT_DEFAULT}, 0, INT_MAX, DEC, "pattern_type"},
578 { "glob_sequence","select glob/sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB_SEQUENCE}, INT_MIN, INT_MAX, DEC, "pattern_type" },
579 { "glob", "select glob pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB }, INT_MIN, INT_MAX, DEC, "pattern_type" },
580 { "sequence", "select sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_SEQUENCE }, INT_MIN, INT_MAX, DEC, "pattern_type" },
581 { "none", "disable pattern matching", 0, AV_OPT_TYPE_CONST, {.i64=PT_NONE }, INT_MIN, INT_MAX, DEC, "pattern_type" },
582 { "start_number", "set first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, DEC },
583 { "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 },
584 { "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" },
585 { "none", "none", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 2, DEC, "ts_type" },
586 { "sec", "second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 2, DEC, "ts_type" },
587 { "ns", "nano second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 2, DEC, "ts_type" },
591 static const AVClass img2_class = {
592 .class_name = "image2 demuxer",
593 .item_name = av_default_item_name,
594 .option = ff_img_options,
595 .version = LIBAVUTIL_VERSION_INT,
597 AVInputFormat ff_image2_demuxer = {
599 .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
600 .priv_data_size = sizeof(VideoDemuxData),
601 .read_probe = img_read_probe,
602 .read_header = ff_img_read_header,
603 .read_packet = ff_img_read_packet,
604 .read_close = img_read_close,
605 .read_seek = img_read_seek,
606 .flags = AVFMT_NOFILE,
607 .priv_class = &img2_class,
611 const AVOption ff_img2pipe_options[] = {
612 { "frame_size", "force frame size in bytes", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
616 #if CONFIG_IMAGE2PIPE_DEMUXER
617 static const AVClass img2pipe_class = {
618 .class_name = "image2pipe demuxer",
619 .item_name = av_default_item_name,
620 .option = ff_img2pipe_options,
621 .version = LIBAVUTIL_VERSION_INT,
623 AVInputFormat ff_image2pipe_demuxer = {
624 .name = "image2pipe",
625 .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
626 .priv_data_size = sizeof(VideoDemuxData),
627 .read_header = ff_img_read_header,
628 .read_packet = ff_img_read_packet,
629 .priv_class = &img2pipe_class,
633 static int bmp_probe(const AVProbeData *p)
635 const uint8_t *b = p->buf;
638 if (AV_RB16(b) != 0x424d)
641 ihsize = AV_RL32(b+14);
642 if (ihsize < 12 || ihsize > 255)
645 if (!AV_RN32(b + 6)) {
646 return AVPROBE_SCORE_EXTENSION + 1;
648 return AVPROBE_SCORE_EXTENSION / 4;
651 static int dds_probe(const AVProbeData *p)
653 const uint8_t *b = p->buf;
655 if ( AV_RB64(b) == 0x444453207c000000
658 return AVPROBE_SCORE_MAX - 1;
662 static int dpx_probe(const AVProbeData *p)
664 const uint8_t *b = p->buf;
666 int is_big = (AV_RN32(b) == AV_RN32("SDPX"));
668 if (p->buf_size < 0x304+8)
670 w = is_big ? AV_RB32(p->buf + 0x304) : AV_RL32(p->buf + 0x304);
671 h = is_big ? AV_RB32(p->buf + 0x308) : AV_RL32(p->buf + 0x308);
672 if (w <= 0 || h <= 0)
675 if (is_big || AV_RN32(b) == AV_RN32("XPDS"))
676 return AVPROBE_SCORE_EXTENSION + 1;
680 static int exr_probe(const AVProbeData *p)
682 const uint8_t *b = p->buf;
684 if (AV_RL32(b) == 20000630)
685 return AVPROBE_SCORE_EXTENSION + 1;
689 static int j2k_probe(const AVProbeData *p)
691 const uint8_t *b = p->buf;
693 if (AV_RB64(b) == 0x0000000c6a502020 ||
694 AV_RB32(b) == 0xff4fff51)
695 return AVPROBE_SCORE_EXTENSION + 1;
699 static int jpeg_probe(const AVProbeData *p)
701 const uint8_t *b = p->buf;
704 if (AV_RB16(b) != 0xFFD8 ||
705 AV_RB32(b) == 0xFFD8FFF7)
709 for (i = 0; i < p->buf_size - 3; i++) {
724 i += AV_RB16(&b[i + 2]) + 1;
730 i += AV_RB16(&b[i + 2]) + 1;
731 if (state != SOF0 && state != SOS)
758 i += AV_RB16(&b[i + 2]) + 1;
761 if ( (c > TEM && c < SOF0)
768 return AVPROBE_SCORE_EXTENSION + 1;
770 return AVPROBE_SCORE_EXTENSION / 2;
771 return AVPROBE_SCORE_EXTENSION / 8;
774 static int jpegls_probe(const AVProbeData *p)
776 const uint8_t *b = p->buf;
778 if (AV_RB32(b) == 0xffd8fff7)
779 return AVPROBE_SCORE_EXTENSION + 1;
783 static int pcx_probe(const AVProbeData *p)
785 const uint8_t *b = p->buf;
787 if ( p->buf_size < 128
791 || av_popcount(b[3]) != 1 || b[3] > 8
792 || AV_RL16(&b[4]) > AV_RL16(&b[8])
793 || AV_RL16(&b[6]) > AV_RL16(&b[10])
797 while (++b < p->buf + 128)
799 return AVPROBE_SCORE_EXTENSION / 4;
801 return AVPROBE_SCORE_EXTENSION + 1;
804 static int qdraw_probe(const AVProbeData *p)
806 const uint8_t *b = p->buf;
808 if ( p->buf_size >= 528
809 && (AV_RB64(b + 520) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
812 return AVPROBE_SCORE_MAX * 3 / 4;
813 if ( (AV_RB64(b + 8) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
816 return AVPROBE_SCORE_EXTENSION / 4;
820 static int pictor_probe(const AVProbeData *p)
822 const uint8_t *b = p->buf;
824 if (AV_RL16(b) == 0x1234)
825 return AVPROBE_SCORE_EXTENSION / 4;
829 static int png_probe(const AVProbeData *p)
831 const uint8_t *b = p->buf;
833 if (AV_RB64(b) == 0x89504e470d0a1a0a)
834 return AVPROBE_SCORE_MAX - 1;
838 static int psd_probe(const AVProbeData *p)
840 const uint8_t *b = p->buf;
844 if (AV_RL32(b) == MKTAG('8','B','P','S')) {
850 if ((b[4] == 0) && (b[5] == 1)) {/* version 1 is PSD, version 2 is PSB */
856 if ((AV_RL32(b+6) == 0) && (AV_RL16(b+10) == 0))/* reserved must be 0 */
859 color_mode = AV_RB16(b+24);
860 if ((color_mode <= 9) && (color_mode != 5) && (color_mode != 6))
863 return AVPROBE_SCORE_EXTENSION + ret;
866 static int sgi_probe(const AVProbeData *p)
868 const uint8_t *b = p->buf;
870 if (AV_RB16(b) == 474 &&
872 (b[3] & ~3) == 0 && b[3] &&
873 (AV_RB16(b + 4) & ~7) == 0 && AV_RB16(b + 4))
874 return AVPROBE_SCORE_EXTENSION + 1;
878 static int sunrast_probe(const AVProbeData *p)
880 const uint8_t *b = p->buf;
882 if (AV_RB32(b) == 0x59a66a95)
883 return AVPROBE_SCORE_EXTENSION + 1;
887 static int svg_probe(const AVProbeData *p)
889 const uint8_t *b = p->buf;
890 const uint8_t *end = p->buf + p->buf_size;
892 if (memcmp(p->buf, "<?xml", 5))
895 int inc = ff_subtitles_next_line(b);
901 if (!memcmp(b, "<svg", 4))
902 return AVPROBE_SCORE_EXTENSION + 1;
907 static int tiff_probe(const AVProbeData *p)
909 const uint8_t *b = p->buf;
911 if (AV_RB32(b) == 0x49492a00 ||
912 AV_RB32(b) == 0x4D4D002a)
913 return AVPROBE_SCORE_EXTENSION + 1;
917 static int webp_probe(const AVProbeData *p)
919 const uint8_t *b = p->buf;
921 if (AV_RB32(b) == 0x52494646 &&
922 AV_RB32(b + 8) == 0x57454250)
923 return AVPROBE_SCORE_MAX - 1;
927 static int pnm_magic_check(const AVProbeData *p, int magic)
929 const uint8_t *b = p->buf;
931 return b[0] == 'P' && b[1] == magic + '0';
934 static inline int pnm_probe(const AVProbeData *p)
936 const uint8_t *b = p->buf;
940 if (b[2] == '\n' && (b[3] == '#' || (b[3] >= '0' && b[3] <= '9')))
941 return AVPROBE_SCORE_EXTENSION + 2;
945 static int pbm_probe(const AVProbeData *p)
947 return pnm_magic_check(p, 1) || pnm_magic_check(p, 4) ? pnm_probe(p) : 0;
950 static inline int pgmx_probe(const AVProbeData *p)
952 return pnm_magic_check(p, 2) || pnm_magic_check(p, 5) ? pnm_probe(p) : 0;
955 static int pgm_probe(const AVProbeData *p)
957 int ret = pgmx_probe(p);
958 return ret && !av_match_ext(p->filename, "pgmyuv") ? ret : 0;
961 static int pgmyuv_probe(const AVProbeData *p) // custom FFmpeg format recognized by file extension
963 int ret = pgmx_probe(p);
964 return ret && av_match_ext(p->filename, "pgmyuv") ? ret : 0;
967 static int ppm_probe(const AVProbeData *p)
969 return pnm_magic_check(p, 3) || pnm_magic_check(p, 6) ? pnm_probe(p) : 0;
972 static int pam_probe(const AVProbeData *p)
974 return pnm_magic_check(p, 7) ? pnm_probe(p) : 0;
977 static int xpm_probe(const AVProbeData *p)
979 const uint8_t *b = p->buf;
981 if (AV_RB64(b) == 0x2f2a2058504d202a && *(b+8) == '/')
982 return AVPROBE_SCORE_MAX - 1;
986 static int xwd_probe(const AVProbeData *p)
988 const uint8_t *b = p->buf;
989 unsigned width, bpp, bpad, lsize;
991 if ( p->buf_size < XWD_HEADER_SIZE
992 || AV_RB32(b ) < XWD_HEADER_SIZE // header size
993 || AV_RB32(b + 4) != XWD_VERSION // version
994 || AV_RB32(b + 8) != XWD_Z_PIXMAP // format
995 || AV_RB32(b + 12) > 32 || !AV_RB32(b + 12) // depth
996 || AV_RB32(b + 16) == 0 // width
997 || AV_RB32(b + 20) == 0 // height
998 || AV_RB32(b + 28) > 1 // byteorder
999 || AV_RB32(b + 32) & ~56 || av_popcount(AV_RB32(b + 32)) != 1 // bitmap unit
1000 || AV_RB32(b + 36) > 1 // bitorder
1001 || AV_RB32(b + 40) & ~56 || av_popcount(AV_RB32(b + 40)) != 1 // padding
1002 || AV_RB32(b + 44) > 32 || !AV_RB32(b + 44) // bpp
1003 || AV_RB32(b + 68) > 256) // colours
1006 width = AV_RB32(b + 16);
1007 bpad = AV_RB32(b + 40);
1008 bpp = AV_RB32(b + 44);
1009 lsize = AV_RB32(b + 48);
1010 if (lsize < FFALIGN(width * bpp, bpad) >> 3)
1013 return AVPROBE_SCORE_MAX / 2 + 1;
1016 static int gif_probe(const AVProbeData *p)
1019 if (memcmp(p->buf, gif87a_sig, 6) && memcmp(p->buf, gif89a_sig, 6))
1022 /* width or height contains zero? */
1023 if (!AV_RL16(&p->buf[6]) || !AV_RL16(&p->buf[8]))
1026 return AVPROBE_SCORE_MAX - 1;
1029 #define IMAGEAUTO_DEMUXER(imgname, codecid)\
1030 static const AVClass imgname ## _class = {\
1031 .class_name = AV_STRINGIFY(imgname) " demuxer",\
1032 .item_name = av_default_item_name,\
1033 .option = ff_img2pipe_options,\
1034 .version = LIBAVUTIL_VERSION_INT,\
1036 AVInputFormat ff_image_ ## imgname ## _pipe_demuxer = {\
1037 .name = AV_STRINGIFY(imgname) "_pipe",\
1038 .long_name = NULL_IF_CONFIG_SMALL("piped " AV_STRINGIFY(imgname) " sequence"),\
1039 .priv_data_size = sizeof(VideoDemuxData),\
1040 .read_probe = imgname ## _probe,\
1041 .read_header = ff_img_read_header,\
1042 .read_packet = ff_img_read_packet,\
1043 .priv_class = & imgname ## _class,\
1044 .flags = AVFMT_GENERIC_INDEX, \
1045 .raw_codec_id = codecid,\
1048 IMAGEAUTO_DEMUXER(bmp, AV_CODEC_ID_BMP)
1049 IMAGEAUTO_DEMUXER(dds, AV_CODEC_ID_DDS)
1050 IMAGEAUTO_DEMUXER(dpx, AV_CODEC_ID_DPX)
1051 IMAGEAUTO_DEMUXER(exr, AV_CODEC_ID_EXR)
1052 IMAGEAUTO_DEMUXER(gif, AV_CODEC_ID_GIF)
1053 IMAGEAUTO_DEMUXER(j2k, AV_CODEC_ID_JPEG2000)
1054 IMAGEAUTO_DEMUXER(jpeg, AV_CODEC_ID_MJPEG)
1055 IMAGEAUTO_DEMUXER(jpegls, AV_CODEC_ID_JPEGLS)
1056 IMAGEAUTO_DEMUXER(pam, AV_CODEC_ID_PAM)
1057 IMAGEAUTO_DEMUXER(pbm, AV_CODEC_ID_PBM)
1058 IMAGEAUTO_DEMUXER(pcx, AV_CODEC_ID_PCX)
1059 IMAGEAUTO_DEMUXER(pgm, AV_CODEC_ID_PGM)
1060 IMAGEAUTO_DEMUXER(pgmyuv, AV_CODEC_ID_PGMYUV)
1061 IMAGEAUTO_DEMUXER(pictor, AV_CODEC_ID_PICTOR)
1062 IMAGEAUTO_DEMUXER(png, AV_CODEC_ID_PNG)
1063 IMAGEAUTO_DEMUXER(ppm, AV_CODEC_ID_PPM)
1064 IMAGEAUTO_DEMUXER(psd, AV_CODEC_ID_PSD)
1065 IMAGEAUTO_DEMUXER(qdraw, AV_CODEC_ID_QDRAW)
1066 IMAGEAUTO_DEMUXER(sgi, AV_CODEC_ID_SGI)
1067 IMAGEAUTO_DEMUXER(sunrast, AV_CODEC_ID_SUNRAST)
1068 IMAGEAUTO_DEMUXER(svg, AV_CODEC_ID_SVG)
1069 IMAGEAUTO_DEMUXER(tiff, AV_CODEC_ID_TIFF)
1070 IMAGEAUTO_DEMUXER(webp, AV_CODEC_ID_WEBP)
1071 IMAGEAUTO_DEMUXER(xpm, AV_CODEC_ID_XPM)
1072 IMAGEAUTO_DEMUXER(xwd, AV_CODEC_ID_XWD)