blob: b4540711687147af8da5846bb1e5b6edaae1f747
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 | #include "libavcodec/mjpeg.h" |
37 | |
38 | #if HAVE_GLOB |
39 | /* Locally define as 0 (bitwise-OR no-op) any missing glob options that |
40 | are non-posix glibc/bsd extensions. */ |
41 | #ifndef GLOB_NOMAGIC |
42 | #define GLOB_NOMAGIC 0 |
43 | #endif |
44 | #ifndef GLOB_BRACE |
45 | #define GLOB_BRACE 0 |
46 | #endif |
47 | |
48 | #endif /* HAVE_GLOB */ |
49 | |
50 | static const int sizes[][2] = { |
51 | { 640, 480 }, |
52 | { 720, 480 }, |
53 | { 720, 576 }, |
54 | { 352, 288 }, |
55 | { 352, 240 }, |
56 | { 160, 128 }, |
57 | { 512, 384 }, |
58 | { 640, 352 }, |
59 | { 640, 240 }, |
60 | }; |
61 | |
62 | static int infer_size(int *width_ptr, int *height_ptr, int size) |
63 | { |
64 | int i; |
65 | |
66 | for (i = 0; i < FF_ARRAY_ELEMS(sizes); i++) { |
67 | if ((sizes[i][0] * sizes[i][1]) == size) { |
68 | *width_ptr = sizes[i][0]; |
69 | *height_ptr = sizes[i][1]; |
70 | return 0; |
71 | } |
72 | } |
73 | |
74 | return -1; |
75 | } |
76 | |
77 | static int is_glob(const char *path) |
78 | { |
79 | #if HAVE_GLOB |
80 | size_t span = 0; |
81 | const char *p = path; |
82 | |
83 | while (p = strchr(p, '%')) { |
84 | if (*(++p) == '%') { |
85 | ++p; |
86 | continue; |
87 | } |
88 | if (span = strspn(p, "*?[]{}")) |
89 | break; |
90 | } |
91 | /* Did we hit a glob char or get to the end? */ |
92 | return span != 0; |
93 | #else |
94 | return 0; |
95 | #endif |
96 | } |
97 | |
98 | /** |
99 | * Get index range of image files matched by path. |
100 | * |
101 | * @param pfirst_index pointer to index updated with the first number in the range |
102 | * @param plast_index pointer to index updated with the last number in the range |
103 | * @param path path which has to be matched by the image files in the range |
104 | * @param start_index minimum accepted value for the first index in the range |
105 | * @return -1 if no image file could be found |
106 | */ |
107 | static int find_image_range(AVIOContext *pb, int *pfirst_index, int *plast_index, |
108 | const char *path, int start_index, int start_index_range) |
109 | { |
110 | char buf[1024]; |
111 | int range, last_index, range1, first_index; |
112 | |
113 | /* find the first image */ |
114 | for (first_index = start_index; first_index < start_index + start_index_range; first_index++) { |
115 | if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0) { |
116 | *pfirst_index = |
117 | *plast_index = 1; |
118 | if (pb || avio_check(buf, AVIO_FLAG_READ) > 0) |
119 | return 0; |
120 | return -1; |
121 | } |
122 | if (avio_check(buf, AVIO_FLAG_READ) > 0) |
123 | break; |
124 | } |
125 | if (first_index == start_index + start_index_range) |
126 | goto fail; |
127 | |
128 | /* find the last image */ |
129 | last_index = first_index; |
130 | for (;;) { |
131 | range = 0; |
132 | for (;;) { |
133 | if (!range) |
134 | range1 = 1; |
135 | else |
136 | range1 = 2 * range; |
137 | if (av_get_frame_filename(buf, sizeof(buf), path, |
138 | last_index + range1) < 0) |
139 | goto fail; |
140 | if (avio_check(buf, AVIO_FLAG_READ) <= 0) |
141 | break; |
142 | range = range1; |
143 | /* just in case... */ |
144 | if (range >= (1 << 30)) |
145 | goto fail; |
146 | } |
147 | /* we are sure than image last_index + range exists */ |
148 | if (!range) |
149 | break; |
150 | last_index += range; |
151 | } |
152 | *pfirst_index = first_index; |
153 | *plast_index = last_index; |
154 | return 0; |
155 | |
156 | fail: |
157 | return -1; |
158 | } |
159 | |
160 | static int img_read_probe(AVProbeData *p) |
161 | { |
162 | if (p->filename && ff_guess_image2_codec(p->filename)) { |
163 | if (av_filename_number_test(p->filename)) |
164 | return AVPROBE_SCORE_MAX; |
165 | else if (is_glob(p->filename)) |
166 | return AVPROBE_SCORE_MAX; |
167 | else if (p->filename[strcspn(p->filename, "*?{")]) // probably PT_GLOB |
168 | return AVPROBE_SCORE_EXTENSION + 2; // score chosen to be a tad above the image pipes |
169 | else if (p->buf_size == 0) |
170 | return 0; |
171 | else if (av_match_ext(p->filename, "raw") || av_match_ext(p->filename, "gif")) |
172 | return 5; |
173 | else |
174 | return AVPROBE_SCORE_EXTENSION; |
175 | } |
176 | return 0; |
177 | } |
178 | |
179 | int ff_img_read_header(AVFormatContext *s1) |
180 | { |
181 | VideoDemuxData *s = s1->priv_data; |
182 | int first_index = 1, last_index = 1; |
183 | AVStream *st; |
184 | enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE; |
185 | |
186 | s1->ctx_flags |= AVFMTCTX_NOHEADER; |
187 | |
188 | st = avformat_new_stream(s1, NULL); |
189 | if (!st) { |
190 | return AVERROR(ENOMEM); |
191 | } |
192 | |
193 | if (s->pixel_format && |
194 | (pix_fmt = av_get_pix_fmt(s->pixel_format)) == AV_PIX_FMT_NONE) { |
195 | av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n", |
196 | s->pixel_format); |
197 | return AVERROR(EINVAL); |
198 | } |
199 | |
200 | av_strlcpy(s->path, s1->filename, sizeof(s->path)); |
201 | s->img_number = 0; |
202 | s->img_count = 0; |
203 | |
204 | /* find format */ |
205 | if (s1->iformat->flags & AVFMT_NOFILE) |
206 | s->is_pipe = 0; |
207 | else { |
208 | s->is_pipe = 1; |
209 | st->need_parsing = AVSTREAM_PARSE_FULL; |
210 | } |
211 | |
212 | if (s->ts_from_file == 2) { |
213 | #if !HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC |
214 | av_log(s1, AV_LOG_ERROR, "POSIX.1-2008 not supported, nanosecond file timestamps unavailable\n"); |
215 | return AVERROR(ENOSYS); |
216 | #endif |
217 | avpriv_set_pts_info(st, 64, 1, 1000000000); |
218 | } else if (s->ts_from_file) |
219 | avpriv_set_pts_info(st, 64, 1, 1); |
220 | else |
221 | avpriv_set_pts_info(st, 64, s->framerate.den, s->framerate.num); |
222 | |
223 | if (s->width && s->height) { |
224 | st->codecpar->width = s->width; |
225 | st->codecpar->height = s->height; |
226 | } |
227 | |
228 | if (!s->is_pipe) { |
229 | if (s->pattern_type == PT_DEFAULT) { |
230 | if (s1->pb) { |
231 | s->pattern_type = PT_NONE; |
232 | } else |
233 | s->pattern_type = PT_GLOB_SEQUENCE; |
234 | } |
235 | |
236 | if (s->pattern_type == PT_GLOB_SEQUENCE) { |
237 | s->use_glob = is_glob(s->path); |
238 | if (s->use_glob) { |
239 | #if HAVE_GLOB |
240 | char *p = s->path, *q, *dup; |
241 | int gerr; |
242 | #endif |
243 | |
244 | av_log(s1, AV_LOG_WARNING, "Pattern type 'glob_sequence' is deprecated: " |
245 | "use pattern_type 'glob' instead\n"); |
246 | #if HAVE_GLOB |
247 | dup = q = av_strdup(p); |
248 | while (*q) { |
249 | /* Do we have room for the next char and a \ insertion? */ |
250 | if ((p - s->path) >= (sizeof(s->path) - 2)) |
251 | break; |
252 | if (*q == '%' && strspn(q + 1, "%*?[]{}")) |
253 | ++q; |
254 | else if (strspn(q, "\\*?[]{}")) |
255 | *p++ = '\\'; |
256 | *p++ = *q++; |
257 | } |
258 | *p = 0; |
259 | av_free(dup); |
260 | |
261 | gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate); |
262 | if (gerr != 0) { |
263 | return AVERROR(ENOENT); |
264 | } |
265 | first_index = 0; |
266 | last_index = s->globstate.gl_pathc - 1; |
267 | #endif |
268 | } |
269 | } |
270 | if ((s->pattern_type == PT_GLOB_SEQUENCE && !s->use_glob) || s->pattern_type == PT_SEQUENCE) { |
271 | if (find_image_range(s1->pb, &first_index, &last_index, s->path, |
272 | s->start_number, s->start_number_range) < 0) { |
273 | av_log(s1, AV_LOG_ERROR, |
274 | "Could find no file with path '%s' and index in the range %d-%d\n", |
275 | s->path, s->start_number, s->start_number + s->start_number_range - 1); |
276 | return AVERROR(ENOENT); |
277 | } |
278 | } else if (s->pattern_type == PT_GLOB) { |
279 | #if HAVE_GLOB |
280 | int gerr; |
281 | gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate); |
282 | if (gerr != 0) { |
283 | return AVERROR(ENOENT); |
284 | } |
285 | first_index = 0; |
286 | last_index = s->globstate.gl_pathc - 1; |
287 | s->use_glob = 1; |
288 | #else |
289 | av_log(s1, AV_LOG_ERROR, |
290 | "Pattern type 'glob' was selected but globbing " |
291 | "is not supported by this libavformat build\n"); |
292 | return AVERROR(ENOSYS); |
293 | #endif |
294 | } else if (s->pattern_type != PT_GLOB_SEQUENCE && s->pattern_type != PT_NONE) { |
295 | av_log(s1, AV_LOG_ERROR, |
296 | "Unknown value '%d' for pattern_type option\n", s->pattern_type); |
297 | return AVERROR(EINVAL); |
298 | } |
299 | s->img_first = first_index; |
300 | s->img_last = last_index; |
301 | s->img_number = first_index; |
302 | /* compute duration */ |
303 | if (!s->ts_from_file) { |
304 | st->start_time = 0; |
305 | st->duration = last_index - first_index + 1; |
306 | } |
307 | } |
308 | |
309 | if (s1->video_codec_id) { |
310 | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; |
311 | st->codecpar->codec_id = s1->video_codec_id; |
312 | } else if (s1->audio_codec_id) { |
313 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; |
314 | st->codecpar->codec_id = s1->audio_codec_id; |
315 | } else if (s1->iformat->raw_codec_id) { |
316 | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; |
317 | st->codecpar->codec_id = s1->iformat->raw_codec_id; |
318 | } else { |
319 | const char *str = strrchr(s->path, '.'); |
320 | s->split_planes = str && !av_strcasecmp(str + 1, "y"); |
321 | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; |
322 | if (s1->pb) { |
323 | int probe_buffer_size = 2048; |
324 | uint8_t *probe_buffer = av_realloc(NULL, probe_buffer_size + AVPROBE_PADDING_SIZE); |
325 | AVInputFormat *fmt = NULL; |
326 | AVProbeData pd = { 0 }; |
327 | |
328 | if (!probe_buffer) |
329 | return AVERROR(ENOMEM); |
330 | |
331 | probe_buffer_size = avio_read(s1->pb, probe_buffer, probe_buffer_size); |
332 | if (probe_buffer_size < 0) { |
333 | av_free(probe_buffer); |
334 | return probe_buffer_size; |
335 | } |
336 | memset(probe_buffer + probe_buffer_size, 0, AVPROBE_PADDING_SIZE); |
337 | |
338 | pd.buf = probe_buffer; |
339 | pd.buf_size = probe_buffer_size; |
340 | pd.filename = s1->filename; |
341 | |
342 | while ((fmt = av_iformat_next(fmt))) { |
343 | if (fmt->read_header != ff_img_read_header || |
344 | !fmt->read_probe || |
345 | (fmt->flags & AVFMT_NOFILE) || |
346 | !fmt->raw_codec_id) |
347 | continue; |
348 | if (fmt->read_probe(&pd) > 0) { |
349 | st->codecpar->codec_id = fmt->raw_codec_id; |
350 | break; |
351 | } |
352 | } |
353 | if (s1->flags & AVFMT_FLAG_CUSTOM_IO) { |
354 | avio_seek(s1->pb, 0, SEEK_SET); |
355 | } else |
356 | ffio_rewind_with_probe_data(s1->pb, &probe_buffer, probe_buffer_size); |
357 | } |
358 | if (st->codecpar->codec_id == AV_CODEC_ID_NONE) |
359 | st->codecpar->codec_id = ff_guess_image2_codec(s->path); |
360 | if (st->codecpar->codec_id == AV_CODEC_ID_LJPEG) |
361 | st->codecpar->codec_id = AV_CODEC_ID_MJPEG; |
362 | if (st->codecpar->codec_id == AV_CODEC_ID_ALIAS_PIX) // we cannot distingiush this from BRENDER_PIX |
363 | st->codecpar->codec_id = AV_CODEC_ID_NONE; |
364 | } |
365 | if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && |
366 | pix_fmt != AV_PIX_FMT_NONE) |
367 | st->codecpar->format = pix_fmt; |
368 | |
369 | return 0; |
370 | } |
371 | |
372 | int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt) |
373 | { |
374 | VideoDemuxData *s = s1->priv_data; |
375 | char filename_bytes[1024]; |
376 | char *filename = filename_bytes; |
377 | int i, res; |
378 | int size[3] = { 0 }, ret[3] = { 0 }; |
379 | AVIOContext *f[3] = { NULL }; |
380 | AVCodecParameters *par = s1->streams[0]->codecpar; |
381 | |
382 | if (!s->is_pipe) { |
383 | /* loop over input */ |
384 | if (s->loop && s->img_number > s->img_last) { |
385 | s->img_number = s->img_first; |
386 | } |
387 | if (s->img_number > s->img_last) |
388 | return AVERROR_EOF; |
389 | if (s->pattern_type == PT_NONE) { |
390 | av_strlcpy(filename_bytes, s->path, sizeof(filename_bytes)); |
391 | } else if (s->use_glob) { |
392 | #if HAVE_GLOB |
393 | filename = s->globstate.gl_pathv[s->img_number]; |
394 | #endif |
395 | } else { |
396 | if (av_get_frame_filename(filename_bytes, sizeof(filename_bytes), |
397 | s->path, |
398 | s->img_number) < 0 && s->img_number > 1) |
399 | return AVERROR(EIO); |
400 | } |
401 | for (i = 0; i < 3; i++) { |
402 | if (s1->pb && |
403 | !strcmp(filename_bytes, s->path) && |
404 | !s->loop && |
405 | !s->split_planes) { |
406 | f[i] = s1->pb; |
407 | } else if (s1->io_open(s1, &f[i], filename, AVIO_FLAG_READ, NULL) < 0) { |
408 | if (i >= 1) |
409 | break; |
410 | av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n", |
411 | filename); |
412 | return AVERROR(EIO); |
413 | } |
414 | size[i] = avio_size(f[i]); |
415 | |
416 | if (!s->split_planes) |
417 | break; |
418 | filename[strlen(filename) - 1] = 'U' + i; |
419 | } |
420 | |
421 | if (par->codec_id == AV_CODEC_ID_NONE) { |
422 | AVProbeData pd = { 0 }; |
423 | AVInputFormat *ifmt; |
424 | uint8_t header[PROBE_BUF_MIN + AVPROBE_PADDING_SIZE]; |
425 | int ret; |
426 | int score = 0; |
427 | |
428 | ret = avio_read(f[0], header, PROBE_BUF_MIN); |
429 | if (ret < 0) |
430 | return ret; |
431 | memset(header + ret, 0, sizeof(header) - ret); |
432 | avio_skip(f[0], -ret); |
433 | pd.buf = header; |
434 | pd.buf_size = ret; |
435 | pd.filename = filename; |
436 | |
437 | ifmt = av_probe_input_format3(&pd, 1, &score); |
438 | if (ifmt && ifmt->read_packet == ff_img_read_packet && ifmt->raw_codec_id) |
439 | par->codec_id = ifmt->raw_codec_id; |
440 | } |
441 | |
442 | if (par->codec_id == AV_CODEC_ID_RAWVIDEO && !par->width) |
443 | infer_size(&par->width, &par->height, size[0]); |
444 | } else { |
445 | f[0] = s1->pb; |
446 | if (avio_feof(f[0]) && s->loop && s->is_pipe) |
447 | avio_seek(f[0], 0, SEEK_SET); |
448 | if (avio_feof(f[0])) |
449 | return AVERROR_EOF; |
450 | if (s->frame_size > 0) { |
451 | size[0] = s->frame_size; |
452 | } else if (!s1->streams[0]->parser) { |
453 | size[0] = avio_size(s1->pb); |
454 | } else { |
455 | size[0] = 4096; |
456 | } |
457 | } |
458 | |
459 | res = av_new_packet(pkt, size[0] + size[1] + size[2]); |
460 | if (res < 0) { |
461 | goto fail; |
462 | } |
463 | pkt->stream_index = 0; |
464 | pkt->flags |= AV_PKT_FLAG_KEY; |
465 | if (s->ts_from_file) { |
466 | struct stat img_stat; |
467 | if (stat(filename, &img_stat)) { |
468 | res = AVERROR(EIO); |
469 | goto fail; |
470 | } |
471 | pkt->pts = (int64_t)img_stat.st_mtime; |
472 | #if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC |
473 | if (s->ts_from_file == 2) |
474 | pkt->pts = 1000000000*pkt->pts + img_stat.st_mtim.tv_nsec; |
475 | #endif |
476 | av_add_index_entry(s1->streams[0], s->img_number, pkt->pts, 0, 0, AVINDEX_KEYFRAME); |
477 | } else if (!s->is_pipe) { |
478 | pkt->pts = s->pts; |
479 | } |
480 | |
481 | if (s->is_pipe) |
482 | pkt->pos = avio_tell(f[0]); |
483 | |
484 | pkt->size = 0; |
485 | for (i = 0; i < 3; i++) { |
486 | if (f[i]) { |
487 | ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]); |
488 | if (s->loop && s->is_pipe && ret[i] == AVERROR_EOF) { |
489 | if (avio_seek(f[i], 0, SEEK_SET) >= 0) { |
490 | pkt->pos = 0; |
491 | ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]); |
492 | } |
493 | } |
494 | if (!s->is_pipe && f[i] != s1->pb) |
495 | ff_format_io_close(s1, &f[i]); |
496 | if (ret[i] > 0) |
497 | pkt->size += ret[i]; |
498 | } |
499 | } |
500 | |
501 | if (ret[0] <= 0 || ret[1] < 0 || ret[2] < 0) { |
502 | av_packet_unref(pkt); |
503 | if (ret[0] < 0) { |
504 | res = ret[0]; |
505 | } else if (ret[1] < 0) { |
506 | res = ret[1]; |
507 | } else if (ret[2] < 0) { |
508 | res = ret[2]; |
509 | } else { |
510 | res = AVERROR_EOF; |
511 | } |
512 | goto fail; |
513 | } else { |
514 | s->img_count++; |
515 | s->img_number++; |
516 | s->pts++; |
517 | return 0; |
518 | } |
519 | |
520 | fail: |
521 | if (!s->is_pipe) { |
522 | for (i = 0; i < 3; i++) { |
523 | if (f[i] != s1->pb) |
524 | ff_format_io_close(s1, &f[i]); |
525 | } |
526 | } |
527 | return res; |
528 | } |
529 | |
530 | static int img_read_close(struct AVFormatContext* s1) |
531 | { |
532 | #if HAVE_GLOB |
533 | VideoDemuxData *s = s1->priv_data; |
534 | if (s->use_glob) { |
535 | globfree(&s->globstate); |
536 | } |
537 | #endif |
538 | return 0; |
539 | } |
540 | |
541 | static int img_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) |
542 | { |
543 | VideoDemuxData *s1 = s->priv_data; |
544 | AVStream *st = s->streams[0]; |
545 | |
546 | if (s1->ts_from_file) { |
547 | int index = av_index_search_timestamp(st, timestamp, flags); |
548 | if(index < 0) |
549 | return -1; |
550 | s1->img_number = st->index_entries[index].pos; |
551 | return 0; |
552 | } |
553 | |
554 | if (timestamp < 0 || !s1->loop && timestamp > s1->img_last - s1->img_first) |
555 | return -1; |
556 | s1->img_number = timestamp%(s1->img_last - s1->img_first + 1) + s1->img_first; |
557 | s1->pts = timestamp; |
558 | return 0; |
559 | } |
560 | |
561 | #define OFFSET(x) offsetof(VideoDemuxData, x) |
562 | #define DEC AV_OPT_FLAG_DECODING_PARAM |
563 | const AVOption ff_img_options[] = { |
564 | { "framerate", "set the video framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC }, |
565 | { "loop", "force loop over input file sequence", OFFSET(loop), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, DEC }, |
566 | |
567 | { "pattern_type", "set pattern type", OFFSET(pattern_type), AV_OPT_TYPE_INT, {.i64=PT_DEFAULT}, 0, INT_MAX, DEC, "pattern_type"}, |
568 | { "glob_sequence","select glob/sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB_SEQUENCE}, INT_MIN, INT_MAX, DEC, "pattern_type" }, |
569 | { "glob", "select glob pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB }, INT_MIN, INT_MAX, DEC, "pattern_type" }, |
570 | { "sequence", "select sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_SEQUENCE }, INT_MIN, INT_MAX, DEC, "pattern_type" }, |
571 | { "none", "disable pattern matching", 0, AV_OPT_TYPE_CONST, {.i64=PT_NONE }, INT_MIN, INT_MAX, DEC, "pattern_type" }, |
572 | |
573 | { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, |
574 | { "start_number", "set first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, DEC }, |
575 | { "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 }, |
576 | { "video_size", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC }, |
577 | { "frame_size", "force frame size in bytes", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC }, |
578 | { "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" }, |
579 | { "none", "none", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 2, DEC, "ts_type" }, |
580 | { "sec", "second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 2, DEC, "ts_type" }, |
581 | { "ns", "nano second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 2, DEC, "ts_type" }, |
582 | { NULL }, |
583 | }; |
584 | |
585 | #if CONFIG_IMAGE2_DEMUXER |
586 | static const AVClass img2_class = { |
587 | .class_name = "image2 demuxer", |
588 | .item_name = av_default_item_name, |
589 | .option = ff_img_options, |
590 | .version = LIBAVUTIL_VERSION_INT, |
591 | }; |
592 | AVInputFormat ff_image2_demuxer = { |
593 | .name = "image2", |
594 | .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"), |
595 | .priv_data_size = sizeof(VideoDemuxData), |
596 | .read_probe = img_read_probe, |
597 | .read_header = ff_img_read_header, |
598 | .read_packet = ff_img_read_packet, |
599 | .read_close = img_read_close, |
600 | .read_seek = img_read_seek, |
601 | .flags = AVFMT_NOFILE, |
602 | .priv_class = &img2_class, |
603 | }; |
604 | #endif |
605 | #if CONFIG_IMAGE2PIPE_DEMUXER |
606 | static const AVClass img2pipe_class = { |
607 | .class_name = "image2pipe demuxer", |
608 | .item_name = av_default_item_name, |
609 | .option = ff_img_options, |
610 | .version = LIBAVUTIL_VERSION_INT, |
611 | }; |
612 | AVInputFormat ff_image2pipe_demuxer = { |
613 | .name = "image2pipe", |
614 | .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"), |
615 | .priv_data_size = sizeof(VideoDemuxData), |
616 | .read_header = ff_img_read_header, |
617 | .read_packet = ff_img_read_packet, |
618 | .priv_class = &img2pipe_class, |
619 | }; |
620 | #endif |
621 | |
622 | static int bmp_probe(AVProbeData *p) |
623 | { |
624 | const uint8_t *b = p->buf; |
625 | int ihsize; |
626 | |
627 | if (AV_RB16(b) != 0x424d) |
628 | return 0; |
629 | |
630 | ihsize = AV_RL32(b+14); |
631 | if (ihsize < 12 || ihsize > 255) |
632 | return 0; |
633 | |
634 | if (!AV_RN32(b + 6)) { |
635 | return AVPROBE_SCORE_EXTENSION + 1; |
636 | } |
637 | return AVPROBE_SCORE_EXTENSION / 4; |
638 | } |
639 | |
640 | static int dds_probe(AVProbeData *p) |
641 | { |
642 | const uint8_t *b = p->buf; |
643 | |
644 | if ( AV_RB64(b) == 0x444453207c000000 |
645 | && AV_RL32(b + 8) |
646 | && AV_RL32(b + 12)) |
647 | return AVPROBE_SCORE_MAX - 1; |
648 | return 0; |
649 | } |
650 | |
651 | static int dpx_probe(AVProbeData *p) |
652 | { |
653 | const uint8_t *b = p->buf; |
654 | int w, h; |
655 | int is_big = (AV_RN32(b) == AV_RN32("SDPX")); |
656 | |
657 | if (p->buf_size < 0x304+8) |
658 | return 0; |
659 | w = is_big ? AV_RB32(p->buf + 0x304) : AV_RL32(p->buf + 0x304); |
660 | h = is_big ? AV_RB32(p->buf + 0x308) : AV_RL32(p->buf + 0x308); |
661 | if (w <= 0 || h <= 0) |
662 | return 0; |
663 | |
664 | if (is_big || AV_RN32(b) == AV_RN32("XPDS")) |
665 | return AVPROBE_SCORE_EXTENSION + 1; |
666 | return 0; |
667 | } |
668 | |
669 | static int exr_probe(AVProbeData *p) |
670 | { |
671 | const uint8_t *b = p->buf; |
672 | |
673 | if (AV_RL32(b) == 20000630) |
674 | return AVPROBE_SCORE_EXTENSION + 1; |
675 | return 0; |
676 | } |
677 | |
678 | static int j2k_probe(AVProbeData *p) |
679 | { |
680 | const uint8_t *b = p->buf; |
681 | |
682 | if (AV_RB64(b) == 0x0000000c6a502020 || |
683 | AV_RB32(b) == 0xff4fff51) |
684 | return AVPROBE_SCORE_EXTENSION + 1; |
685 | return 0; |
686 | } |
687 | |
688 | static int jpeg_probe(AVProbeData *p) |
689 | { |
690 | const uint8_t *b = p->buf; |
691 | int i, state = SOI; |
692 | |
693 | if (AV_RB16(b) != 0xFFD8 || |
694 | AV_RB32(b) == 0xFFD8FFF7) |
695 | return 0; |
696 | |
697 | b += 2; |
698 | for (i = 0; i < p->buf_size - 3; i++) { |
699 | int c; |
700 | if (b[i] != 0xFF) |
701 | continue; |
702 | c = b[i + 1]; |
703 | switch (c) { |
704 | case SOI: |
705 | return 0; |
706 | case SOF0: |
707 | case SOF1: |
708 | case SOF2: |
709 | case SOF3: |
710 | case SOF5: |
711 | case SOF6: |
712 | case SOF7: |
713 | i += AV_RB16(&b[i + 2]) + 1; |
714 | if (state != SOI) |
715 | return 0; |
716 | state = SOF0; |
717 | break; |
718 | case SOS: |
719 | i += AV_RB16(&b[i + 2]) + 1; |
720 | if (state != SOF0 && state != SOS) |
721 | return 0; |
722 | state = SOS; |
723 | break; |
724 | case EOI: |
725 | if (state != SOS) |
726 | return 0; |
727 | state = EOI; |
728 | break; |
729 | case DQT: |
730 | case APP0: |
731 | case APP1: |
732 | case APP2: |
733 | case APP3: |
734 | case APP4: |
735 | case APP5: |
736 | case APP6: |
737 | case APP7: |
738 | case APP8: |
739 | case APP9: |
740 | case APP10: |
741 | case APP11: |
742 | case APP12: |
743 | case APP13: |
744 | case APP14: |
745 | case APP15: |
746 | case COM: |
747 | i += AV_RB16(&b[i + 2]) + 1; |
748 | break; |
749 | default: |
750 | if ( (c > TEM && c < SOF0) |
751 | || c == JPG) |
752 | return 0; |
753 | } |
754 | } |
755 | |
756 | if (state == EOI) |
757 | return AVPROBE_SCORE_EXTENSION + 1; |
758 | if (state == SOS) |
759 | return AVPROBE_SCORE_EXTENSION / 2; |
760 | return AVPROBE_SCORE_EXTENSION / 8; |
761 | } |
762 | |
763 | static int jpegls_probe(AVProbeData *p) |
764 | { |
765 | const uint8_t *b = p->buf; |
766 | |
767 | if (AV_RB32(b) == 0xffd8fff7) |
768 | return AVPROBE_SCORE_EXTENSION + 1; |
769 | return 0; |
770 | } |
771 | |
772 | static int pcx_probe(AVProbeData *p) |
773 | { |
774 | const uint8_t *b = p->buf; |
775 | |
776 | if ( p->buf_size < 128 |
777 | || b[0] != 10 |
778 | || b[1] > 5 |
779 | || b[2] > 1 |
780 | || av_popcount(b[3]) != 1 || b[3] > 8 |
781 | || AV_RL16(&b[4]) > AV_RL16(&b[8]) |
782 | || AV_RL16(&b[6]) > AV_RL16(&b[10]) |
783 | || b[64]) |
784 | return 0; |
785 | b += 73; |
786 | while (++b < p->buf + 128) |
787 | if (*b) |
788 | return AVPROBE_SCORE_EXTENSION / 4; |
789 | |
790 | return AVPROBE_SCORE_EXTENSION + 1; |
791 | } |
792 | |
793 | static int qdraw_probe(AVProbeData *p) |
794 | { |
795 | const uint8_t *b = p->buf; |
796 | |
797 | if ( p->buf_size >= 528 |
798 | && (AV_RB64(b + 520) & 0xFFFFFFFFFFFF) == 0x001102ff0c00 |
799 | && AV_RB16(b + 520) |
800 | && AV_RB16(b + 518)) |
801 | return AVPROBE_SCORE_MAX * 3 / 4; |
802 | if ( (AV_RB64(b + 8) & 0xFFFFFFFFFFFF) == 0x001102ff0c00 |
803 | && AV_RB16(b + 8) |
804 | && AV_RB16(b + 6)) |
805 | return AVPROBE_SCORE_EXTENSION / 4; |
806 | return 0; |
807 | } |
808 | |
809 | static int pictor_probe(AVProbeData *p) |
810 | { |
811 | const uint8_t *b = p->buf; |
812 | |
813 | if (AV_RL16(b) == 0x1234) |
814 | return AVPROBE_SCORE_EXTENSION / 4; |
815 | return 0; |
816 | } |
817 | |
818 | static int png_probe(AVProbeData *p) |
819 | { |
820 | const uint8_t *b = p->buf; |
821 | |
822 | if (AV_RB64(b) == 0x89504e470d0a1a0a) |
823 | return AVPROBE_SCORE_MAX - 1; |
824 | return 0; |
825 | } |
826 | |
827 | static int psd_probe(AVProbeData *p) |
828 | { |
829 | const uint8_t *b = p->buf; |
830 | int ret = 0; |
831 | uint16_t color_mode; |
832 | |
833 | if (AV_RL32(b) == MKTAG('8','B','P','S')) { |
834 | ret += 1; |
835 | } else { |
836 | return 0; |
837 | } |
838 | |
839 | if ((b[4] == 0) && (b[5] == 1)) {/* version 1 is PSD, version 2 is PSB */ |
840 | ret += 1; |
841 | } else { |
842 | return 0; |
843 | } |
844 | |
845 | if ((AV_RL32(b+6) == 0) && (AV_RL16(b+10) == 0))/* reserved must be 0 */ |
846 | ret += 1; |
847 | |
848 | color_mode = AV_RB16(b+24); |
849 | if ((color_mode <= 9) && (color_mode != 5) && (color_mode != 6)) |
850 | ret += 1; |
851 | |
852 | return AVPROBE_SCORE_EXTENSION + ret; |
853 | } |
854 | |
855 | static int sgi_probe(AVProbeData *p) |
856 | { |
857 | const uint8_t *b = p->buf; |
858 | |
859 | if (AV_RB16(b) == 474 && |
860 | (b[2] & ~1) == 0 && |
861 | (b[3] & ~3) == 0 && b[3] && |
862 | (AV_RB16(b + 4) & ~7) == 0 && AV_RB16(b + 4)) |
863 | return AVPROBE_SCORE_EXTENSION + 1; |
864 | return 0; |
865 | } |
866 | |
867 | static int sunrast_probe(AVProbeData *p) |
868 | { |
869 | const uint8_t *b = p->buf; |
870 | |
871 | if (AV_RB32(b) == 0x59a66a95) |
872 | return AVPROBE_SCORE_EXTENSION + 1; |
873 | return 0; |
874 | } |
875 | |
876 | static int tiff_probe(AVProbeData *p) |
877 | { |
878 | const uint8_t *b = p->buf; |
879 | |
880 | if (AV_RB32(b) == 0x49492a00 || |
881 | AV_RB32(b) == 0x4D4D002a) |
882 | return AVPROBE_SCORE_EXTENSION + 1; |
883 | return 0; |
884 | } |
885 | |
886 | static int webp_probe(AVProbeData *p) |
887 | { |
888 | const uint8_t *b = p->buf; |
889 | |
890 | if (AV_RB32(b) == 0x52494646 && |
891 | AV_RB32(b + 8) == 0x57454250) |
892 | return AVPROBE_SCORE_MAX - 1; |
893 | return 0; |
894 | } |
895 | |
896 | static int pnm_magic_check(const AVProbeData *p, int magic) |
897 | { |
898 | const uint8_t *b = p->buf; |
899 | |
900 | return b[0] == 'P' && b[1] == magic + '0'; |
901 | } |
902 | |
903 | static inline int pnm_probe(const AVProbeData *p) |
904 | { |
905 | const uint8_t *b = p->buf; |
906 | |
907 | while (b[2] == '\r') |
908 | b++; |
909 | if (b[2] == '\n' && (b[3] == '#' || (b[3] >= '0' && b[3] <= '9'))) |
910 | return AVPROBE_SCORE_EXTENSION + 2; |
911 | return 0; |
912 | } |
913 | |
914 | static int pbm_probe(AVProbeData *p) |
915 | { |
916 | return pnm_magic_check(p, 1) || pnm_magic_check(p, 4) ? pnm_probe(p) : 0; |
917 | } |
918 | |
919 | static inline int pgmx_probe(AVProbeData *p) |
920 | { |
921 | return pnm_magic_check(p, 2) || pnm_magic_check(p, 5) ? pnm_probe(p) : 0; |
922 | } |
923 | |
924 | static int pgm_probe(AVProbeData *p) |
925 | { |
926 | int ret = pgmx_probe(p); |
927 | return ret && !av_match_ext(p->filename, "pgmyuv") ? ret : 0; |
928 | } |
929 | |
930 | static int pgmyuv_probe(AVProbeData *p) // custom FFmpeg format recognized by file extension |
931 | { |
932 | int ret = pgmx_probe(p); |
933 | return ret && av_match_ext(p->filename, "pgmyuv") ? ret : 0; |
934 | } |
935 | |
936 | static int ppm_probe(AVProbeData *p) |
937 | { |
938 | return pnm_magic_check(p, 3) || pnm_magic_check(p, 6) ? pnm_probe(p) : 0; |
939 | } |
940 | |
941 | static int pam_probe(AVProbeData *p) |
942 | { |
943 | return pnm_magic_check(p, 7) ? pnm_probe(p) : 0; |
944 | } |
945 | |
946 | static int xpm_probe(AVProbeData *p) |
947 | { |
948 | const uint8_t *b = p->buf; |
949 | |
950 | if (AV_RB64(b) == 0x2f2a2058504d202a && *(b+8) == '/') |
951 | return AVPROBE_SCORE_MAX - 1; |
952 | return 0; |
953 | } |
954 | |
955 | #define IMAGEAUTO_DEMUXER(imgname, codecid)\ |
956 | static const AVClass imgname ## _class = {\ |
957 | .class_name = AV_STRINGIFY(imgname) " demuxer",\ |
958 | .item_name = av_default_item_name,\ |
959 | .option = ff_img_options,\ |
960 | .version = LIBAVUTIL_VERSION_INT,\ |
961 | };\ |
962 | AVInputFormat ff_image_ ## imgname ## _pipe_demuxer = {\ |
963 | .name = AV_STRINGIFY(imgname) "_pipe",\ |
964 | .long_name = NULL_IF_CONFIG_SMALL("piped " AV_STRINGIFY(imgname) " sequence"),\ |
965 | .priv_data_size = sizeof(VideoDemuxData),\ |
966 | .read_probe = imgname ## _probe,\ |
967 | .read_header = ff_img_read_header,\ |
968 | .read_packet = ff_img_read_packet,\ |
969 | .priv_class = & imgname ## _class,\ |
970 | .flags = AVFMT_GENERIC_INDEX, \ |
971 | .raw_codec_id = codecid,\ |
972 | }; |
973 | |
974 | IMAGEAUTO_DEMUXER(bmp, AV_CODEC_ID_BMP) |
975 | IMAGEAUTO_DEMUXER(dds, AV_CODEC_ID_DDS) |
976 | IMAGEAUTO_DEMUXER(dpx, AV_CODEC_ID_DPX) |
977 | IMAGEAUTO_DEMUXER(exr, AV_CODEC_ID_EXR) |
978 | IMAGEAUTO_DEMUXER(j2k, AV_CODEC_ID_JPEG2000) |
979 | IMAGEAUTO_DEMUXER(jpeg, AV_CODEC_ID_MJPEG) |
980 | IMAGEAUTO_DEMUXER(jpegls, AV_CODEC_ID_JPEGLS) |
981 | IMAGEAUTO_DEMUXER(pam, AV_CODEC_ID_PAM) |
982 | IMAGEAUTO_DEMUXER(pbm, AV_CODEC_ID_PBM) |
983 | IMAGEAUTO_DEMUXER(pcx, AV_CODEC_ID_PCX) |
984 | IMAGEAUTO_DEMUXER(pgm, AV_CODEC_ID_PGM) |
985 | IMAGEAUTO_DEMUXER(pgmyuv, AV_CODEC_ID_PGMYUV) |
986 | IMAGEAUTO_DEMUXER(pictor, AV_CODEC_ID_PICTOR) |
987 | IMAGEAUTO_DEMUXER(png, AV_CODEC_ID_PNG) |
988 | IMAGEAUTO_DEMUXER(ppm, AV_CODEC_ID_PPM) |
989 | IMAGEAUTO_DEMUXER(psd, AV_CODEC_ID_PSD) |
990 | IMAGEAUTO_DEMUXER(qdraw, AV_CODEC_ID_QDRAW) |
991 | IMAGEAUTO_DEMUXER(sgi, AV_CODEC_ID_SGI) |
992 | IMAGEAUTO_DEMUXER(sunrast, AV_CODEC_ID_SUNRAST) |
993 | IMAGEAUTO_DEMUXER(tiff, AV_CODEC_ID_TIFF) |
994 | IMAGEAUTO_DEMUXER(webp, AV_CODEC_ID_WEBP) |
995 | IMAGEAUTO_DEMUXER(xpm, AV_CODEC_ID_XPM) |
996 |