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 #include "config_components.h"
24
25 #define _DEFAULT_SOURCE
27 #include <sys/stat.h>
47
48 #if HAVE_GLOB
49 /* Locally define as 0 (bitwise-OR no-op) any missing glob options that
50 are non-posix glibc/bsd extensions. */
51 #ifndef GLOB_NOMAGIC
52 #define GLOB_NOMAGIC 0
53 #endif
54 #ifndef GLOB_BRACE
55 #define GLOB_BRACE 0
56 #endif
57
58 #endif /* HAVE_GLOB */
59
61 { 640, 480 },
62 { 720, 480 },
63 { 720, 576 },
64 { 352, 288 },
65 { 352, 240 },
66 { 160, 128 },
67 { 512, 384 },
68 { 640, 352 },
69 { 640, 240 },
70 };
71
73 {
75
80 return 0;
81 }
82 }
83
84 return -1;
85 }
86
88 {
89 #if HAVE_GLOB
90 size_t span = 0;
91 const char *p = path;
92
93 while (p = strchr(p, '%')) {
94 if (*(++p) == '%') {
95 ++p;
96 continue;
97 }
98 if (span = strspn(p, "*?[]{}"))
99 break;
100 }
101 /* Did we hit a glob char or get to the end? */
102 return span != 0;
103 #else
104 return 0;
105 #endif
106 }
107
108 /**
109 * Get index range of image files matched by path.
110 *
111 * @param pfirst_index pointer to index updated with the first number in the range
112 * @param plast_index pointer to index updated with the last number in the range
113 * @param path path which has to be matched by the image files in the range
114 * @param start_index minimum accepted value for the first index in the range
115 * @return -1 if no image file could be found
116 */
118 const char *path, int start_index, int start_index_range)
119 {
120 char buf[1024];
121 int range, last_index, range1, first_index;
122
123 /* find the first image */
124 for (first_index = start_index; first_index < start_index + start_index_range; first_index++) {
126 *pfirst_index =
127 *plast_index = 1;
129 return 0;
130 return -1;
131 }
133 break;
134 }
135 if (first_index == start_index + start_index_range)
137
138 /* find the last image */
139 last_index = first_index;
140 for (;;) {
142 for (;;) {
144 range1 = 1;
145 else
148 last_index + range1) < 0)
151 break;
153 /* just in case... */
154 if (
range >= (1 << 30))
156 }
157 /* we are sure than image last_index + range exists */
159 break;
161 }
162 *pfirst_index = first_index;
163 *plast_index = last_index;
164 return 0;
165
167 return -1;
168 }
169
171 {
180 return 0;
182 return 5;
183 else
185 }
186 return 0;
187 }
188
190 {
192 int first_index = 1, last_index = 1;
195
197
199 if (!st) {
201 }
202
203 if (
s->pixel_format &&
208 }
209
213
214 /* find format */
217 else {
220 }
221
222 if (
s->ts_from_file == 2) {
223 #if !HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
224 av_log(s1,
AV_LOG_ERROR,
"POSIX.1-2008 not supported, nanosecond file timestamps unavailable\n");
226 #endif
228 }
else if (
s->ts_from_file)
230 else {
233 }
234
235 if (
s->width &&
s->height) {
238 }
239
244 } else
246 }
247
251 #if HAVE_GLOB
252 char *p =
s->path, *q, *dup;
253 int gerr;
254 #endif
255
257 "use pattern_type 'glob' instead\n");
258 #if HAVE_GLOB
260 while (*q) {
261 /* Do we have room for the next char and a \ insertion? */
262 if ((p -
s->path) >= (
sizeof(
s->path) - 2))
263 break;
264 if (*q == '%' && strspn(q + 1, "%*?[]{}"))
265 ++q;
266 else if (strspn(q, "\\*?[]{}"))
267 *p++ = '\\';
268 *p++ = *q++;
269 }
270 *p = 0;
272
273 gerr = glob(
s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC,
NULL, &
s->globstate);
274 if (gerr != 0) {
276 }
277 first_index = 0;
278 last_index =
s->globstate.gl_pathc - 1;
279 #endif
280 }
281 }
284 s->start_number,
s->start_number_range) < 0) {
286 "Could find no file with path '%s' and index in the range %d-%d\n",
287 s->path,
s->start_number,
s->start_number +
s->start_number_range - 1);
289 }
290 }
else if (
s->pattern_type ==
PT_GLOB) {
291 #if HAVE_GLOB
292 int gerr;
293 gerr = glob(
s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC,
NULL, &
s->globstate);
294 if (gerr != 0) {
296 }
297 first_index = 0;
298 last_index =
s->globstate.gl_pathc - 1;
300 #else
302 "Pattern type 'glob' was selected but globbing "
303 "is not supported by this libavformat build\n");
305 #endif
308 "Unknown value '%d' for pattern_type option\n",
s->pattern_type);
310 }
311 s->img_first = first_index;
312 s->img_last = last_index;
313 s->img_number = first_index;
314 /* compute duration */
315 if (!
s->ts_from_file) {
317 st->
duration = last_index - first_index + 1;
318 }
319 }
320
330 } else {
331 const char *str = strrchr(
s->path,
'.');
335 int probe_buffer_size = 2048;
338 void *fmt_iter =
NULL;
340
341 if (!probe_buffer)
343
344 probe_buffer_size =
avio_read(s1->
pb, probe_buffer, probe_buffer_size);
345 if (probe_buffer_size < 0) {
347 return probe_buffer_size;
348 }
350
351 pd.
buf = probe_buffer;
354
361 continue;
364 break;
365 }
366 }
370 } else
372 }
379 }
383
384 return 0;
385 }
386
387 /**
388 * Add this frame's source path and basename to packet's sidedata
389 * as a dictionary, so it can be used by filters like 'drawtext'.
390 */
393 char *packed_metadata =
NULL;
394 size_t metadata_len;
396
397 av_dict_set(&d,
"lavf.image2dec.source_path", filename, 0);
399
402 if (!packed_metadata)
405 packed_metadata, metadata_len);
409 }
410 return 0;
411 }
412
414 {
416 char filename_bytes[1024];
417 char *filename = filename_bytes;
419 int size[3] = { 0 },
ret[3] = { 0 };
422
424 /* loop over input */
425 if (
s->loop &&
s->img_number >
s->img_last) {
426 s->img_number =
s->img_first;
427 }
428 if (
s->img_number >
s->img_last)
431 av_strlcpy(filename_bytes,
s->path,
sizeof(filename_bytes));
432 }
else if (
s->use_glob) {
433 #if HAVE_GLOB
434 filename =
s->globstate.gl_pathv[
s->img_number];
435 #endif
436 } else {
439 s->img_number) < 0 &&
s->img_number > 1)
441 }
442 for (
i = 0;
i < 3;
i++) {
444 !strcmp(filename_bytes,
s->path) &&
450 break;
452 filename);
454 }
456
457 if (!
s->split_planes)
458 break;
459 filename[strlen(filename) - 1] =
'U' +
i;
460 }
461
467 int score = 0;
468
477
481 }
482
485 } else {
491 if (
s->frame_size > 0) {
492 size[0] =
s->frame_size;
495 } else {
497 }
498 }
499
501 if (res < 0) {
503 }
506 if (
s->ts_from_file) {
507 struct stat img_stat;
508 av_assert0(!
s->is_pipe);
// The ts_from_file option is not supported by piped input demuxers
509 if (stat(filename, &img_stat)) {
512 }
514 #if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
515 if (
s->ts_from_file == 2)
516 pkt->
pts = 1000000000*
pkt->
pts + img_stat.st_mtim.tv_nsec;
517 #endif
519 }
else if (!
s->is_pipe) {
521 }
522
525
526 /*
527 * export_path_metadata must be explicitly enabled via
528 * command line options for path metadata to be exported
529 * as packet side_data.
530 */
531 if (!
s->is_pipe &&
s->export_path_metadata == 1) {
533 if (res < 0)
535 }
536
538 for (
i = 0;
i < 3;
i++) {
545 }
546 }
547 if (!
s->is_pipe &&
f[
i] != s1->
pb)
551 }
552 }
553
554 if (
ret[0] <= 0 ||
ret[1] < 0 ||
ret[2] < 0) {
557 }
else if (
ret[1] < 0) {
559 }
else if (
ret[2] < 0) {
561 } else {
563 }
565 } else {
570 return 0;
571 }
572
575 for (
i = 0;
i < 3;
i++) {
578 }
579 }
580 return res;
581 }
582
584 {
585 #if HAVE_GLOB
588 globfree(&
s->globstate);
589 }
590 #endif
591 return 0;
592 }
593
595 {
598
602 return -1;
604 return 0;
605 }
606
608 return -1;
611 return 0;
612 }
613
614 #define OFFSET(x) offsetof(VideoDemuxData, x)
615 #define DEC AV_OPT_FLAG_DECODING_PARAM
616 #define COMMON_OPTIONS \
617 { "framerate", "set the video framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC }, \
618 { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, \
619 { "video_size", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC }, \
620 { "loop", "force loop over input file sequence", OFFSET(loop), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, DEC }, \
621 { NULL },
622
623 #if CONFIG_IMAGE2_DEMUXER
630 {
"start_number",
"set first number in the sequence",
OFFSET(start_number),
AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX,
DEC },
631 {
"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 },
632 {
"ts_from_file",
"set frame timestamp from file's one",
OFFSET(ts_from_file),
AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2,
DEC, .unit =
"ts_type" },
635 {
"ns",
"nano second precision", 0,
AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 2,
DEC, .unit =
"ts_type" },
636 {
"export_path_metadata",
"enable metadata containing input path information",
OFFSET(export_path_metadata),
AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1,
DEC }, \
637 COMMON_OPTIONS
638 };
639
640 static const AVClass img2_class = {
645 };
650 .p.priv_class = &img2_class,
657 };
658 #endif
659
663 };
669 };
670
671 #if CONFIG_IMAGE2PIPE_DEMUXER
673 .
p.
name =
"image2pipe",
679 };
680 #endif
681
683 {
684 const uint8_t *
b = p->
buf;
685 int ihsize;
686
688 return 0;
689
691 if (ihsize < 12 || ihsize > 255)
692 return 0;
693
696 }
698 }
699
701 {
702 const uint8_t *
b = p->
buf;
703
708 return 0;
709 }
710
712 {
713 const uint8_t *
b = p->
buf;
714
715 if (
AV_RB64(
b) == 0x444453207c000000
719 return 0;
720 }
721
723 {
724 const uint8_t *
b = p->
buf;
727
729 return 0;
732 if (
w <= 0 ||
h <= 0)
733 return 0;
734
737 return 0;
738 }
739
741 {
742 const uint8_t *
b = p->
buf;
743
746 return 0;
747 }
748
750 {
751 const uint8_t *
b = p->
buf;
752
753 if (
AV_RB64(
b) == 0x0000000c6a502020 ||
756 return 0;
757 }
758
760 {
761 const uint8_t *
b = p->
buf;
763
766 return 0;
767
772 continue;
776 return 0;
786 return 0;
788 break;
792 return 0;
794 break;
797 return 0;
799 break;
802 got_header = 1;
803 /* fallthrough */
806 got_header = 1;
807 /* fallthrough */
822 case DQT:
/* fallthrough */
825 break;
826 default:
829 return 0;
830 }
831 }
832
838 }
839
841 {
842 const uint8_t *
b = p->
buf;
843
846 return 0;
847 }
848
850 {
851 const uint8_t *
b = p->
buf;
852
853 /* ISOBMFF-based container */
854 /* 0x4a584c20 == "JXL " */
857 /* Raw codestreams all start with 0xff0a */
859 return 0;
860 #if CONFIG_IMAGE_JPEGXL_PIPE_DEMUXER
863 #endif
864 return 0;
865 }
866
868 {
869 const uint8_t *
b = p->
buf;
870
879 return 0;
881 while (++b < p->buf + 128)
884
886 }
887
889 {
890 const uint8_t *
b = p->
buf;
891
893 && (
AV_RB64(
b + 520) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
897 if ( (
AV_RB64(
b + 8) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
901 return 0;
902 }
903
905 {
906 const uint8_t *
b = p->
buf;
907
910 return 0;
911 }
912
914 {
915 const uint8_t *
b = p->
buf;
916
917 if (
AV_RB64(
b) == 0x89504e470d0a1a0a)
919 return 0;
920 }
921
923 {
924 const uint8_t *
b = p->
buf;
926 uint16_t color_mode;
927
930 } else {
931 return 0;
932 }
933
934 if ((
b[4] == 0) && (
b[5] == 1)) {
/* version 1 is PSD, version 2 is PSB */
936 } else {
937 return 0;
938 }
939
942
944 if ((color_mode <= 9) && (color_mode != 5) && (color_mode != 6))
946
948 }
949
951 {
952 const uint8_t *
b = p->
buf;
953
956 (
b[3] & ~3) == 0 &&
b[3] &&
959 return 0;
960 }
961
963 {
964 const uint8_t *
b = p->
buf;
965
968 return 0;
969 }
970
972 {
973 const uint8_t *
b = p->
buf;
978 return 0;
979 if (!memcmp(
b,
"<svg", 4))
981 if (memcmp(p->
buf,
"<?xml", 5) && memcmp(
b,
"<!--", 4))
982 return 0;
986 break;
989 return 0;
990 if (!memcmp(
b,
"<svg", 4))
992 }
993 return 0;
994 }
995
997 {
998 const uint8_t *
b = p->
buf;
999
1003 return 0;
1004 }
1005
1007 {
1008 const uint8_t *
b = p->
buf;
1009
1013 return 0;
1014 }
1015
1017 {
1018 const uint8_t *
b = p->
buf;
1019
1020 return b[0] ==
'P' &&
b[1] == magic +
'0';
1021 }
1022
1024 {
1025 const uint8_t *
b = p->
buf;
1026
1027 while (
b[2] ==
'\r')
1029 if (
b[2] ==
'\n' && (
b[3] ==
'#' || (
b[3] >=
'0' &&
b[3] <=
'9')))
1031 return 0;
1032 }
1033
1035 {
1037 }
1038
1040 {
1043 }
1044
1046 {
1049 }
1050
1052 {
1054 }
1055
1057 {
1060 }
1061
1063 {
1066 }
1067
1069 {
1070 const uint8_t *
b = p->
buf;
1071 if (!memcmp(
b,
"PG ML ", 6))
1073 return 0;
1074 }
1075
1077 {
1079 }
1080
1082 {
1084 }
1085
1087 {
1088 if (!memcmp(p->
buf,
"#?RADIANCE\n", 11))
1090 return 0;
1091 }
1092
1094 {
1095 if (!memcmp(p->
buf,
"/* XBM X10 format */", 20))
1097
1098 if (!memcmp(p->
buf,
"#define", 7))
1100 return 0;
1101 }
1102
1104 {
1105 const uint8_t *
b = p->
buf;
1106
1107 if (
AV_RB64(
b) == 0x2f2a2058504d202a && *(
b+8) ==
'/')
1109 return 0;
1110 }
1111
1113 {
1114 const uint8_t *
b = p->
buf;
1115 unsigned width, bpp, bpad, lsize;
1116
1124 ||
AV_RB32(
b + 28) > 1
// byteorder
1129 ||
AV_RB32(
b + 68) > 256)
// colours
1130 return 0;
1131
1137 return 0;
1138
1140 }
1141
1143 {
1144 /* check magick */
1146 return 0;
1147
1148 /* width or height contains zero? */
1150 return 0;
1151
1153 }
1154
1156 {
1157 if (!memcmp(p->
buf,
"PCD_OPA", 7))
1159
1160 if (p->
buf_size < 0x807 || memcmp(p->
buf + 0x800,
"PCD_IPI", 7))
1161 return 0;
1162
1164 }
1165
1167 {
1168 if (memcmp(p->
buf,
"qoif", 4))
1169 return 0;
1170
1172 return 0;
1173
1174 if (p->
buf[12] != 3 && p->
buf[12] != 4)
1175 return 0;
1176
1178 return 0;
1179
1181 }
1182
1184 {
1185 const uint8_t *
b = p->
buf;
1199 }
1200 return 0;
1201 }
1202
1204 {
1205 const uint8_t *
b = p->
buf;
1210 return 0;
1211 }
1212
1213 #define IMAGEAUTO_DEMUXER_0(imgname, codecid)
1214 #define IMAGEAUTO_DEMUXER_1(imgname, codecid)\
1215 const FFInputFormat ff_image_ ## imgname ## _pipe_demuxer = {\
1216 .p.name = AV_STRINGIFY(imgname) "_pipe",\
1217 .p.long_name = NULL_IF_CONFIG_SMALL("piped " AV_STRINGIFY(imgname) " sequence"),\
1218 .p.priv_class = &imagepipe_class,\
1219 .p.flags = AVFMT_GENERIC_INDEX,\
1220 .priv_data_size = sizeof(VideoDemuxData),\
1221 .read_probe = imgname ## _probe,\
1222 .read_header = ff_img_read_header,\
1223 .read_packet = ff_img_read_packet,\
1224 .raw_codec_id = codecid,\
1225 };
1226
1227 #define IMAGEAUTO_DEMUXER_2(imgname, codecid, enabled) \
1228 IMAGEAUTO_DEMUXER_ ## enabled(imgname, codecid)
1229 #define IMAGEAUTO_DEMUXER_3(imgname, codecid, config) \
1230 IMAGEAUTO_DEMUXER_2(imgname, codecid, config)
1231 #define IMAGEAUTO_DEMUXER_EXT(imgname, codecid, uppercase_name) \
1232 IMAGEAUTO_DEMUXER_3(imgname, AV_CODEC_ID_ ## codecid, \
1233 CONFIG_IMAGE_ ## uppercase_name ## _PIPE_DEMUXER)
1234 #define IMAGEAUTO_DEMUXER(imgname, codecid) \
1235 IMAGEAUTO_DEMUXER_EXT(imgname, codecid, codecid)
1236