FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
img2dec.c
Go to the documentation of this file.
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 
37 #if HAVE_GLOB
38 /* Locally define as 0 (bitwise-OR no-op) any missing glob options that
39  are non-posix glibc/bsd extensions. */
40 #ifndef GLOB_NOMAGIC
41 #define GLOB_NOMAGIC 0
42 #endif
43 #ifndef GLOB_BRACE
44 #define GLOB_BRACE 0
45 #endif
46 
47 #endif /* HAVE_GLOB */
48 
49 static const int sizes[][2] = {
50  { 640, 480 },
51  { 720, 480 },
52  { 720, 576 },
53  { 352, 288 },
54  { 352, 240 },
55  { 160, 128 },
56  { 512, 384 },
57  { 640, 352 },
58  { 640, 240 },
59 };
60 
61 static int infer_size(int *width_ptr, int *height_ptr, int size)
62 {
63  int i;
64 
65  for (i = 0; i < FF_ARRAY_ELEMS(sizes); i++) {
66  if ((sizes[i][0] * sizes[i][1]) == size) {
67  *width_ptr = sizes[i][0];
68  *height_ptr = sizes[i][1];
69  return 0;
70  }
71  }
72 
73  return -1;
74 }
75 
76 static int is_glob(const char *path)
77 {
78 #if HAVE_GLOB
79  size_t span = 0;
80  const char *p = path;
81 
82  while (p = strchr(p, '%')) {
83  if (*(++p) == '%') {
84  ++p;
85  continue;
86  }
87  if (span = strspn(p, "*?[]{}"))
88  break;
89  }
90  /* Did we hit a glob char or get to the end? */
91  return span != 0;
92 #else
93  return 0;
94 #endif
95 }
96 
97 /**
98  * Get index range of image files matched by path.
99  *
100  * @param pfirst_index pointer to index updated with the first number in the range
101  * @param plast_index pointer to index updated with the last number in the range
102  * @param path path which has to be matched by the image files in the range
103  * @param start_index minimum accepted value for the first index in the range
104  * @return -1 if no image file could be found
105  */
106 static int find_image_range(AVIOContext *pb, int *pfirst_index, int *plast_index,
107  const char *path, int start_index, int start_index_range)
108 {
109  char buf[1024];
110  int range, last_index, range1, first_index;
111 
112  /* find the first image */
113  for (first_index = start_index; first_index < start_index + start_index_range; first_index++) {
114  if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0) {
115  *pfirst_index =
116  *plast_index = 1;
117  if (pb || avio_check(buf, AVIO_FLAG_READ) > 0)
118  return 0;
119  return -1;
120  }
121  if (avio_check(buf, AVIO_FLAG_READ) > 0)
122  break;
123  }
124  if (first_index == start_index + start_index_range)
125  goto fail;
126 
127  /* find the last image */
128  last_index = first_index;
129  for (;;) {
130  range = 0;
131  for (;;) {
132  if (!range)
133  range1 = 1;
134  else
135  range1 = 2 * range;
136  if (av_get_frame_filename(buf, sizeof(buf), path,
137  last_index + range1) < 0)
138  goto fail;
139  if (avio_check(buf, AVIO_FLAG_READ) <= 0)
140  break;
141  range = range1;
142  /* just in case... */
143  if (range >= (1 << 30))
144  goto fail;
145  }
146  /* we are sure than image last_index + range exists */
147  if (!range)
148  break;
149  last_index += range;
150  }
151  *pfirst_index = first_index;
152  *plast_index = last_index;
153  return 0;
154 
155 fail:
156  return -1;
157 }
158 
160 {
161  if (p->filename && ff_guess_image2_codec(p->filename)) {
163  return AVPROBE_SCORE_MAX;
164  else if (is_glob(p->filename))
165  return AVPROBE_SCORE_MAX;
166  else if (p->filename[strcspn(p->filename, "*?{")]) // probably PT_GLOB
167  return AVPROBE_SCORE_EXTENSION + 2; // score chosen to be a tad above the image pipes
168  else if (p->buf_size == 0)
169  return 0;
170  else if (av_match_ext(p->filename, "raw") || av_match_ext(p->filename, "gif"))
171  return 5;
172  else
174  }
175  return 0;
176 }
177 
179 {
180  VideoDemuxData *s = s1->priv_data;
181  int first_index = 1, last_index = 1;
182  AVStream *st;
184 
186 
187  st = avformat_new_stream(s1, NULL);
188  if (!st) {
189  return AVERROR(ENOMEM);
190  }
191 
192  if (s->pixel_format &&
193  (pix_fmt = av_get_pix_fmt(s->pixel_format)) == AV_PIX_FMT_NONE) {
194  av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n",
195  s->pixel_format);
196  return AVERROR(EINVAL);
197  }
198 
199  av_strlcpy(s->path, s1->filename, sizeof(s->path));
200  s->img_number = 0;
201  s->img_count = 0;
202 
203  /* find format */
204  if (s1->iformat->flags & AVFMT_NOFILE)
205  s->is_pipe = 0;
206  else {
207  s->is_pipe = 1;
209  }
210 
211  if (s->ts_from_file == 2) {
212 #if !HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
213  av_log(s1, AV_LOG_ERROR, "POSIX.1-2008 not supported, nanosecond file timestamps unavailable\n");
214  return AVERROR(ENOSYS);
215 #endif
216  avpriv_set_pts_info(st, 64, 1, 1000000000);
217  } else if (s->ts_from_file)
218  avpriv_set_pts_info(st, 64, 1, 1);
219  else
221 
222  if (s->width && s->height) {
223  st->codec->width = s->width;
224  st->codec->height = s->height;
225  }
226 
227  if (!s->is_pipe) {
228  if (s->pattern_type == PT_DEFAULT) {
229  if (s1->pb) {
230  s->pattern_type = PT_NONE;
231  } else
233  }
234 
235  if (s->pattern_type == PT_GLOB_SEQUENCE) {
236  s->use_glob = is_glob(s->path);
237  if (s->use_glob) {
238 #if HAVE_GLOB
239  char *p = s->path, *q, *dup;
240  int gerr;
241 #endif
242 
243  av_log(s1, AV_LOG_WARNING, "Pattern type 'glob_sequence' is deprecated: "
244  "use pattern_type 'glob' instead\n");
245 #if HAVE_GLOB
246  dup = q = av_strdup(p);
247  while (*q) {
248  /* Do we have room for the next char and a \ insertion? */
249  if ((p - s->path) >= (sizeof(s->path) - 2))
250  break;
251  if (*q == '%' && strspn(q + 1, "%*?[]{}"))
252  ++q;
253  else if (strspn(q, "\\*?[]{}"))
254  *p++ = '\\';
255  *p++ = *q++;
256  }
257  *p = 0;
258  av_free(dup);
259 
260  gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
261  if (gerr != 0) {
262  return AVERROR(ENOENT);
263  }
264  first_index = 0;
265  last_index = s->globstate.gl_pathc - 1;
266 #endif
267  }
268  }
269  if ((s->pattern_type == PT_GLOB_SEQUENCE && !s->use_glob) || s->pattern_type == PT_SEQUENCE) {
270  if (find_image_range(s1->pb, &first_index, &last_index, s->path,
271  s->start_number, s->start_number_range) < 0) {
272  av_log(s1, AV_LOG_ERROR,
273  "Could find no file with path '%s' and index in the range %d-%d\n",
274  s->path, s->start_number, s->start_number + s->start_number_range - 1);
275  return AVERROR(ENOENT);
276  }
277  } else if (s->pattern_type == PT_GLOB) {
278 #if HAVE_GLOB
279  int gerr;
280  gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
281  if (gerr != 0) {
282  return AVERROR(ENOENT);
283  }
284  first_index = 0;
285  last_index = s->globstate.gl_pathc - 1;
286  s->use_glob = 1;
287 #else
288  av_log(s1, AV_LOG_ERROR,
289  "Pattern type 'glob' was selected but globbing "
290  "is not supported by this libavformat build\n");
291  return AVERROR(ENOSYS);
292 #endif
293  } else if (s->pattern_type != PT_GLOB_SEQUENCE && s->pattern_type != PT_NONE) {
294  av_log(s1, AV_LOG_ERROR,
295  "Unknown value '%d' for pattern_type option\n", s->pattern_type);
296  return AVERROR(EINVAL);
297  }
298  s->img_first = first_index;
299  s->img_last = last_index;
300  s->img_number = first_index;
301  /* compute duration */
302  if (!s->ts_from_file) {
303  st->start_time = 0;
304  st->duration = last_index - first_index + 1;
305  }
306  }
307 
308  if (s1->video_codec_id) {
310  st->codec->codec_id = s1->video_codec_id;
311  } else if (s1->audio_codec_id) {
313  st->codec->codec_id = s1->audio_codec_id;
314  } else if (s1->iformat->raw_codec_id) {
316  st->codec->codec_id = s1->iformat->raw_codec_id;
317  } else {
318  const char *str = strrchr(s->path, '.');
319  s->split_planes = str && !av_strcasecmp(str + 1, "y");
321  if (s1->pb) {
322  int probe_buffer_size = 2048;
323  uint8_t *probe_buffer = av_realloc(NULL, probe_buffer_size + AVPROBE_PADDING_SIZE);
325  AVProbeData pd = { 0 };
326 
327  if (!probe_buffer)
328  return AVERROR(ENOMEM);
329 
330  probe_buffer_size = avio_read(s1->pb, probe_buffer, probe_buffer_size);
331  if (probe_buffer_size < 0) {
332  av_free(probe_buffer);
333  return probe_buffer_size;
334  }
335  memset(probe_buffer + probe_buffer_size, 0, AVPROBE_PADDING_SIZE);
336 
337  pd.buf = probe_buffer;
338  pd.buf_size = probe_buffer_size;
339  pd.filename = s1->filename;
340 
341  while ((fmt = av_iformat_next(fmt))) {
342  if (fmt->read_header != ff_img_read_header ||
343  !fmt->read_probe ||
344  (fmt->flags & AVFMT_NOFILE) ||
345  !fmt->raw_codec_id)
346  continue;
347  if (fmt->read_probe(&pd) > 0) {
348  st->codec->codec_id = fmt->raw_codec_id;
349  break;
350  }
351  }
352  if (s1->flags & AVFMT_FLAG_CUSTOM_IO) {
353  avio_seek(s1->pb, 0, SEEK_SET);
354  } else
355  ffio_rewind_with_probe_data(s1->pb, &probe_buffer, probe_buffer_size);
356  }
357  if (st->codec->codec_id == AV_CODEC_ID_NONE)
359  if (st->codec->codec_id == AV_CODEC_ID_LJPEG)
361  if (st->codec->codec_id == AV_CODEC_ID_ALIAS_PIX) // we cannot distingiush this from BRENDER_PIX
363  }
364  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
365  pix_fmt != AV_PIX_FMT_NONE)
366  st->codec->pix_fmt = pix_fmt;
367 
368  return 0;
369 }
370 
372 {
373  VideoDemuxData *s = s1->priv_data;
374  char filename_bytes[1024];
375  char *filename = filename_bytes;
376  int i, res;
377  int size[3] = { 0 }, ret[3] = { 0 };
378  AVIOContext *f[3] = { NULL };
379  AVCodecContext *codec = s1->streams[0]->codec;
380 
381  if (!s->is_pipe) {
382  /* loop over input */
383  if (s->loop && s->img_number > s->img_last) {
384  s->img_number = s->img_first;
385  }
386  if (s->img_number > s->img_last)
387  return AVERROR_EOF;
388  if (s->pattern_type == PT_NONE) {
389  av_strlcpy(filename_bytes, s->path, sizeof(filename_bytes));
390  } else if (s->use_glob) {
391 #if HAVE_GLOB
392  filename = s->globstate.gl_pathv[s->img_number];
393 #endif
394  } else {
395  if (av_get_frame_filename(filename_bytes, sizeof(filename_bytes),
396  s->path,
397  s->img_number) < 0 && s->img_number > 1)
398  return AVERROR(EIO);
399  }
400  for (i = 0; i < 3; i++) {
401  if (s1->pb &&
402  !strcmp(filename_bytes, s->path) &&
403  !s->loop &&
404  !s->split_planes) {
405  f[i] = s1->pb;
406  } else if (s1->io_open(s1, &f[i], filename, AVIO_FLAG_READ, NULL) < 0) {
407  if (i >= 1)
408  break;
409  av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",
410  filename);
411  return AVERROR(EIO);
412  }
413  size[i] = avio_size(f[i]);
414 
415  if (!s->split_planes)
416  break;
417  filename[strlen(filename) - 1] = 'U' + i;
418  }
419 
420  if (codec->codec_id == AV_CODEC_ID_NONE) {
421  AVProbeData pd = { 0 };
422  AVInputFormat *ifmt;
424  int ret;
425  int score = 0;
426 
427  ret = avio_read(f[0], header, PROBE_BUF_MIN);
428  if (ret < 0)
429  return ret;
430  memset(header + ret, 0, sizeof(header) - ret);
431  avio_skip(f[0], -ret);
432  pd.buf = header;
433  pd.buf_size = ret;
434  pd.filename = filename;
435 
436  ifmt = av_probe_input_format3(&pd, 1, &score);
437  if (ifmt && ifmt->read_packet == ff_img_read_packet && ifmt->raw_codec_id)
438  codec->codec_id = ifmt->raw_codec_id;
439  }
440 
441  if (codec->codec_id == AV_CODEC_ID_RAWVIDEO && !codec->width)
442  infer_size(&codec->width, &codec->height, size[0]);
443  } else {
444  f[0] = s1->pb;
445  if (avio_feof(f[0]) && s->loop && s->is_pipe)
446  avio_seek(f[0], 0, SEEK_SET);
447  if (avio_feof(f[0]))
448  return AVERROR_EOF;
449  if (s->frame_size > 0) {
450  size[0] = s->frame_size;
451  } else if (!s1->streams[0]->parser) {
452  size[0] = avio_size(s1->pb);
453  } else {
454  size[0] = 4096;
455  }
456  }
457 
458  res = av_new_packet(pkt, size[0] + size[1] + size[2]);
459  if (res < 0) {
460  goto fail;
461  }
462  pkt->stream_index = 0;
463  pkt->flags |= AV_PKT_FLAG_KEY;
464  if (s->ts_from_file) {
465  struct stat img_stat;
466  if (stat(filename, &img_stat)) {
467  res = AVERROR(EIO);
468  goto fail;
469  }
470  pkt->pts = (int64_t)img_stat.st_mtime;
471 #if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
472  if (s->ts_from_file == 2)
473  pkt->pts = 1000000000*pkt->pts + img_stat.st_mtim.tv_nsec;
474 #endif
475  av_add_index_entry(s1->streams[0], s->img_number, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
476  } else if (!s->is_pipe) {
477  pkt->pts = s->pts;
478  }
479 
480  if (s->is_pipe)
481  pkt->pos = avio_tell(f[0]);
482 
483  pkt->size = 0;
484  for (i = 0; i < 3; i++) {
485  if (f[i]) {
486  ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
487  if (s->loop && s->is_pipe && ret[i] == AVERROR_EOF) {
488  if (avio_seek(f[i], 0, SEEK_SET) >= 0) {
489  pkt->pos = 0;
490  ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
491  }
492  }
493  if (!s->is_pipe && f[i] != s1->pb)
494  ff_format_io_close(s1, &f[i]);
495  if (ret[i] > 0)
496  pkt->size += ret[i];
497  }
498  }
499 
500  if (ret[0] <= 0 || ret[1] < 0 || ret[2] < 0) {
501  av_packet_unref(pkt);
502  if (ret[0] < 0) {
503  res = ret[0];
504  } else if (ret[1] < 0) {
505  res = ret[1];
506  } else if (ret[2] < 0) {
507  res = ret[2];
508  } else {
509  res = AVERROR_EOF;
510  }
511  goto fail;
512  } else {
513  s->img_count++;
514  s->img_number++;
515  s->pts++;
516  return 0;
517  }
518 
519 fail:
520  if (!s->is_pipe) {
521  for (i = 0; i < 3; i++) {
522  if (f[i] != s1->pb)
523  ff_format_io_close(s1, &f[i]);
524  }
525  }
526  return res;
527 }
528 
529 static int img_read_close(struct AVFormatContext* s1)
530 {
531 #if HAVE_GLOB
532  VideoDemuxData *s = s1->priv_data;
533  if (s->use_glob) {
534  globfree(&s->globstate);
535  }
536 #endif
537  return 0;
538 }
539 
540 static int img_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
541 {
543  AVStream *st = s->streams[0];
544 
545  if (s1->ts_from_file) {
546  int index = av_index_search_timestamp(st, timestamp, flags);
547  if(index < 0)
548  return -1;
549  s1->img_number = st->index_entries[index].pos;
550  return 0;
551  }
552 
553  if (timestamp < 0 || !s1->loop && timestamp > s1->img_last - s1->img_first)
554  return -1;
555  s1->img_number = timestamp%(s1->img_last - s1->img_first + 1) + s1->img_first;
556  s1->pts = timestamp;
557  return 0;
558 }
559 
560 #define OFFSET(x) offsetof(VideoDemuxData, x)
561 #define DEC AV_OPT_FLAG_DECODING_PARAM
563  { "framerate", "set the video framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, DEC },
564  { "loop", "force loop over input file sequence", OFFSET(loop), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, DEC },
565 
566  { "pattern_type", "set pattern type", OFFSET(pattern_type), AV_OPT_TYPE_INT, {.i64=PT_DEFAULT}, 0, INT_MAX, DEC, "pattern_type"},
567  { "glob_sequence","select glob/sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB_SEQUENCE}, INT_MIN, INT_MAX, DEC, "pattern_type" },
568  { "glob", "select glob pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB }, INT_MIN, INT_MAX, DEC, "pattern_type" },
569  { "sequence", "select sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_SEQUENCE }, INT_MIN, INT_MAX, DEC, "pattern_type" },
570  { "none", "disable pattern matching", 0, AV_OPT_TYPE_CONST, {.i64=PT_NONE }, INT_MIN, INT_MAX, DEC, "pattern_type" },
571 
572  { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
573  { "start_number", "set first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, DEC },
574  { "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 },
575  { "video_size", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
576  { "frame_size", "force frame size in bytes", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
577  { "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" },
578  { "none", "none", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 2, DEC, "ts_type" },
579  { "sec", "second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 2, DEC, "ts_type" },
580  { "ns", "nano second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 2, DEC, "ts_type" },
581  { NULL },
582 };
583 
584 #if CONFIG_IMAGE2_DEMUXER
585 static const AVClass img2_class = {
586  .class_name = "image2 demuxer",
587  .item_name = av_default_item_name,
588  .option = ff_img_options,
589  .version = LIBAVUTIL_VERSION_INT,
590 };
591 AVInputFormat ff_image2_demuxer = {
592  .name = "image2",
593  .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
594  .priv_data_size = sizeof(VideoDemuxData),
600  .flags = AVFMT_NOFILE,
601  .priv_class = &img2_class,
602 };
603 #endif
604 #if CONFIG_IMAGE2PIPE_DEMUXER
605 static const AVClass img2pipe_class = {
606  .class_name = "image2pipe demuxer",
607  .item_name = av_default_item_name,
608  .option = ff_img_options,
609  .version = LIBAVUTIL_VERSION_INT,
610 };
611 AVInputFormat ff_image2pipe_demuxer = {
612  .name = "image2pipe",
613  .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
614  .priv_data_size = sizeof(VideoDemuxData),
617  .priv_class = &img2pipe_class,
618 };
619 #endif
620 
621 static int bmp_probe(AVProbeData *p)
622 {
623  const uint8_t *b = p->buf;
624  int ihsize;
625 
626  if (AV_RB16(b) != 0x424d)
627  return 0;
628 
629  ihsize = AV_RL32(b+14);
630  if (ihsize < 12 || ihsize > 255)
631  return 0;
632 
633  if (!AV_RN32(b + 6)) {
634  return AVPROBE_SCORE_EXTENSION + 1;
635  }
636  return AVPROBE_SCORE_EXTENSION / 4;
637 }
638 
639 static int dds_probe(AVProbeData *p)
640 {
641  const uint8_t *b = p->buf;
642 
643  if ( AV_RB64(b) == 0x444453207c000000
644  && AV_RL32(b + 8)
645  && AV_RL32(b + 12))
646  return AVPROBE_SCORE_MAX - 1;
647  return 0;
648 }
649 
650 static int dpx_probe(AVProbeData *p)
651 {
652  const uint8_t *b = p->buf;
653  int w, h;
654  int is_big = (AV_RN32(b) == AV_RN32("SDPX"));
655 
656  if (p->buf_size < 0x304+8)
657  return 0;
658  w = is_big ? AV_RB32(p->buf + 0x304) : AV_RL32(p->buf + 0x304);
659  h = is_big ? AV_RB32(p->buf + 0x308) : AV_RL32(p->buf + 0x308);
660  if (w <= 0 || h <= 0)
661  return 0;
662 
663  if (is_big || AV_RN32(b) == AV_RN32("XPDS"))
664  return AVPROBE_SCORE_EXTENSION + 1;
665  return 0;
666 }
667 
668 static int exr_probe(AVProbeData *p)
669 {
670  const uint8_t *b = p->buf;
671 
672  if (AV_RL32(b) == 20000630)
673  return AVPROBE_SCORE_EXTENSION + 1;
674  return 0;
675 }
676 
677 static int j2k_probe(AVProbeData *p)
678 {
679  const uint8_t *b = p->buf;
680 
681  if (AV_RB64(b) == 0x0000000c6a502020 ||
682  AV_RB32(b) == 0xff4fff51)
683  return AVPROBE_SCORE_EXTENSION + 1;
684  return 0;
685 }
686 
687 static int jpeg_probe(AVProbeData *p)
688 {
689  const uint8_t *b = p->buf;
690  int i, state = 0xD8;
691 
692  if (AV_RB16(b) != 0xFFD8 ||
693  AV_RB32(b) == 0xFFD8FFF7)
694  return 0;
695 
696  b += 2;
697  for (i = 0; i < p->buf_size - 3; i++) {
698  int c;
699  if (b[i] != 0xFF)
700  continue;
701  c = b[i + 1];
702  switch (c) {
703  case 0xD8:
704  return 0;
705  case 0xC0:
706  case 0xC1:
707  case 0xC2:
708  case 0xC3:
709  case 0xC5:
710  case 0xC6:
711  case 0xC7:
712  if (state != 0xD8)
713  return 0;
714  state = 0xC0;
715  break;
716  case 0xDA:
717  if (state != 0xC0)
718  return 0;
719  state = 0xDA;
720  break;
721  case 0xD9:
722  if (state != 0xDA)
723  return 0;
724  state = 0xD9;
725  break;
726  case 0xE0:
727  case 0xE1:
728  case 0xE2:
729  case 0xE3:
730  case 0xE4:
731  case 0xE5:
732  case 0xE6:
733  case 0xE7:
734  case 0xE8:
735  case 0xE9:
736  case 0xEA:
737  case 0xEB:
738  case 0xEC:
739  case 0xED:
740  case 0xEE:
741  case 0xEF:
742  i += AV_RB16(&b[i + 2]) + 1;
743  break;
744  default:
745  if ( (c >= 0x02 && c <= 0xBF)
746  || c == 0xC8)
747  return 0;
748  }
749  }
750 
751  if (state == 0xD9)
752  return AVPROBE_SCORE_EXTENSION + 1;
753  return AVPROBE_SCORE_EXTENSION / 8;
754 }
755 
756 static int jpegls_probe(AVProbeData *p)
757 {
758  const uint8_t *b = p->buf;
759 
760  if (AV_RB32(b) == 0xffd8fff7)
761  return AVPROBE_SCORE_EXTENSION + 1;
762  return 0;
763 }
764 
765 static int qdraw_probe(AVProbeData *p)
766 {
767  const uint8_t *b = p->buf;
768 
769  if ( p->buf_size >= 528
770  && (AV_RB64(b + 520) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
771  && AV_RB16(b + 520)
772  && AV_RB16(b + 518))
773  return AVPROBE_SCORE_MAX * 3 / 4;
774  if ( (AV_RB64(b + 8) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
775  && AV_RB16(b + 8)
776  && AV_RB16(b + 6))
777  return AVPROBE_SCORE_EXTENSION / 4;
778  return 0;
779 }
780 
781 static int pictor_probe(AVProbeData *p)
782 {
783  const uint8_t *b = p->buf;
784 
785  if (AV_RL16(b) == 0x1234)
786  return AVPROBE_SCORE_EXTENSION / 4;
787  return 0;
788 }
789 
790 static int png_probe(AVProbeData *p)
791 {
792  const uint8_t *b = p->buf;
793 
794  if (AV_RB64(b) == 0x89504e470d0a1a0a)
795  return AVPROBE_SCORE_MAX - 1;
796  return 0;
797 }
798 
799 static int sgi_probe(AVProbeData *p)
800 {
801  const uint8_t *b = p->buf;
802 
803  if (AV_RB16(b) == 474 &&
804  (b[2] & ~1) == 0 &&
805  (b[3] & ~3) == 0 && b[3] &&
806  (AV_RB16(b + 4) & ~7) == 0 && AV_RB16(b + 4))
807  return AVPROBE_SCORE_EXTENSION + 1;
808  return 0;
809 }
810 
812 {
813  const uint8_t *b = p->buf;
814 
815  if (AV_RB32(b) == 0x59a66a95)
816  return AVPROBE_SCORE_EXTENSION + 1;
817  return 0;
818 }
819 
820 static int tiff_probe(AVProbeData *p)
821 {
822  const uint8_t *b = p->buf;
823 
824  if (AV_RB32(b) == 0x49492a00 ||
825  AV_RB32(b) == 0x4D4D002a)
826  return AVPROBE_SCORE_EXTENSION + 1;
827  return 0;
828 }
829 
830 static int webp_probe(AVProbeData *p)
831 {
832  const uint8_t *b = p->buf;
833 
834  if (AV_RB32(b) == 0x52494646 &&
835  AV_RB32(b + 8) == 0x57454250)
836  return AVPROBE_SCORE_MAX - 1;
837  return 0;
838 }
839 
840 #define IMAGEAUTO_DEMUXER(imgname, codecid)\
841 static const AVClass imgname ## _class = {\
842  .class_name = AV_STRINGIFY(imgname) " demuxer",\
843  .item_name = av_default_item_name,\
844  .option = ff_img_options,\
845  .version = LIBAVUTIL_VERSION_INT,\
846 };\
847 AVInputFormat ff_image_ ## imgname ## _pipe_demuxer = {\
848  .name = AV_STRINGIFY(imgname) "_pipe",\
849  .long_name = NULL_IF_CONFIG_SMALL("piped " AV_STRINGIFY(imgname) " sequence"),\
850  .priv_data_size = sizeof(VideoDemuxData),\
851  .read_probe = imgname ## _probe,\
852  .read_header = ff_img_read_header,\
853  .read_packet = ff_img_read_packet,\
854  .priv_class = & imgname ## _class,\
855  .flags = AVFMT_GENERIC_INDEX, \
856  .raw_codec_id = codecid,\
857 };
858 
int img_number
Definition: img2.h:45
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
static enum AVPixelFormat pix_fmt
int height
Set by a private option.
Definition: img2.h:52
static int img_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: img2dec.c:540
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:287
static int bmp_probe(AVProbeData *p)
Definition: img2dec.c:621
AVOption.
Definition: opt.h:245
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition: utils.c:1778
#define OFFSET(x)
Definition: img2dec.c:560
static int qdraw_probe(AVProbeData *p)
Definition: img2dec.c:765
const char * fmt
Definition: avisynth_c.h:632
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
const char * filename
Definition: avformat.h:461
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1487
int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char **buf, int buf_size)
Rewind the AVIOContext using the specified buffer containing the first buf_size bytes of the file...
Definition: aviobuf.c:882
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4149
int64_t pos
Definition: avformat.h:817
char path[1024]
Definition: img2.h:50
static int j2k_probe(AVProbeData *p)
Definition: img2dec.c:677
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
static int img_read_close(struct AVFormatContext *s1)
Definition: img2dec.c:529
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:1468
const char * b
Definition: vf_curves.c:109
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:208
#define AVIO_FLAG_READ
read-only
Definition: avio.h:537
#define AVPROBE_PADDING_SIZE
extra allocated bytes at the end of the probe buffer
Definition: avformat.h:474
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:1080
int start_number_range
Definition: img2.h:61
Definition: img2.h:37
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1752
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:282
AVInputFormat * av_probe_input_format3(AVProbeData *pd, int is_opened, int *score_ret)
Guess the file format.
Definition: format.c:169
static AVPacket pkt
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:1363
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:87
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
static int png_probe(AVProbeData *p)
Definition: img2dec.c:790
int img_count
Definition: img2.h:47
char * pixel_format
Set by a private option.
Definition: img2.h:51
#define IMAGEAUTO_DEMUXER(imgname, codecid)
Definition: img2dec.c:840
Format I/O context.
Definition: avformat.h:1314
int avio_check(const char *url, int flags)
Return AVIO_FLAG_* access flags corresponding to the access permissions of the resource in url...
Definition: avio.c:492
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
uint8_t
static int dds_probe(AVProbeData *p)
Definition: img2dec.c:639
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1273
AVOptions.
int pattern_type
PatternType.
Definition: img2.h:55
int flags
Can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_SHOW_IDS, AVFMT_GENERIC_INDEX, AVFMT_TS_DISCONT, AVFMT_NOBINSEARCH, AVFMT_NOGENSEARCH, AVFMT_NO_BYTE_SEEK, AVFMT_SEEK_TO_PTS.
Definition: avformat.h:675
static int pictor_probe(AVProbeData *p)
Definition: img2dec.c:781
enum AVStreamParseType need_parsing
Definition: avformat.h:1069
void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
Definition: utils.c:4736
int ff_img_read_header(AVFormatContext *s1)
Definition: img2dec.c:178
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3805
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1382
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1425
uint8_t * data
Definition: avcodec.h:1467
int(* read_header)(struct AVFormatContext *)
Read the format header and initialize the AVFormatContext structure.
Definition: avformat.h:726
static int img_read_probe(AVProbeData *p)
Definition: img2dec.c:159
#define AVERROR_EOF
End of file.
Definition: error.h:55
static int webp_probe(AVProbeData *p)
Definition: img2dec.c:830
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
int av_match_ext(const char *filename, const char *extensions)
Return a positive value if the given filename has one of the given extensions, 0 otherwise.
Definition: format.c:81
const AVOption ff_img_options[]
Definition: img2dec.c:562
ptrdiff_t size
Definition: opengl_enc.c:101
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:442
static const uint8_t header[24]
Definition: sdr2.c:67
enum AVCodecID video_codec_id
Forced video codec_id.
Definition: avformat.h:1474
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:545
static int exr_probe(AVProbeData *p)
Definition: img2dec.c:668
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1499
static int is_glob(const char *path)
Definition: img2dec.c:76
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
#define AVINDEX_KEYFRAME
Definition: avformat.h:824
int64_t pts
Definition: img2.h:46
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:1877
int(* read_probe)(AVProbeData *)
Tell if a given file has a chance of being parsed as this format.
Definition: avformat.h:719
av_default_item_name
static const int sizes[][2]
Definition: img2dec.c:49
int loop
Definition: img2.h:54
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
int ts_from_file
Definition: img2.h:63
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:83
#define fail()
Definition: checkasm.h:80
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1473
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:896
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:463
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:462
static int find_image_range(AVIOContext *pb, int *pfirst_index, int *plast_index, const char *path, int start_index, int start_index_range)
Get index range of image files matched by path.
Definition: img2dec.c:106
char filename[1024]
input or output filename
Definition: avformat.h:1390
static int jpeg_probe(AVProbeData *p)
Definition: img2dec.c:687
enum AVCodecID audio_codec_id
Forced audio codec_id.
Definition: avformat.h:1480
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:213
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int is_pipe
Definition: img2.h:48
int width
picture width / height.
Definition: avcodec.h:1711
int width
Definition: img2.h:52
Definition: img2.h:35
static int sgi_probe(AVProbeData *p)
Definition: img2dec.c:799
#define AVFMT_FLAG_CUSTOM_IO
The caller has supplied a custom AVIOContext, don't avio_close() it.
Definition: avformat.h:1433
int start_number
Definition: img2.h:60
#define FF_ARRAY_ELEMS(a)
int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
Return in 'buf' the path with 'd' replaced by a number.
Definition: utils.c:3974
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:638
#define PROBE_BUF_MIN
size of probe buffer, for guessing file type from file contents
Definition: internal.h:31
Stream structure.
Definition: avformat.h:877
#define DEC
Definition: img2dec.c:561
int frame_size
Definition: img2.h:62
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
int frame_size
Definition: mxfenc.c:1821
enum AVMediaType codec_type
Definition: avcodec.h:1540
enum AVCodecID codec_id
Definition: avcodec.h:1549
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:267
AVIOContext * pb
I/O context.
Definition: avformat.h:1356
static int loop
Definition: ffplay.c:339
int split_planes
use independent file for each Y, U, V plane
Definition: img2.h:49
main external API structure.
Definition: avcodec.h:1532
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:545
int av_filename_number_test(const char *filename)
Check whether filename actually is a numbered sequence generator.
Definition: utils.c:269
int(* read_packet)(struct AVFormatContext *, AVPacket *pkt)
Read one packet and put it in 'pkt'.
Definition: avformat.h:737
void * buf
Definition: avisynth_c.h:553
Describe the class of an AVClass context structure.
Definition: log.h:67
int index
Definition: gxfenc.c:89
static int infer_size(int *width_ptr, int *height_ptr, int size)
Definition: img2dec.c:61
offset must point to AVRational
Definition: opt.h:235
#define s1
Definition: regdef.h:38
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:470
offset must point to two consecutive integers
Definition: opt.h:232
This structure contains the data a format has to probe a file.
Definition: avformat.h:460
#define AV_RN32(p)
Definition: intreadwrite.h:364
misc parsing utilities
int img_last
Definition: img2.h:44
static int sunrast_probe(AVProbeData *p)
Definition: img2dec.c:811
int raw_codec_id
Raw demuxers store their codec ID here.
Definition: avformat.h:707
static int flags
Definition: cpu.c:47
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:936
static int dpx_probe(AVProbeData *p)
Definition: img2dec.c:650
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:472
static int tiff_probe(AVProbeData *p)
Definition: img2dec.c:820
full parsing and repack
Definition: avformat.h:807
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_RB64
Definition: bytestream.h:87
Main libavformat public API header.
if(ret< 0)
Definition: vf_mcdeint.c:282
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:477
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:145
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:929
static double c[64]
int den
denominator
Definition: rational.h:45
struct AVInputFormat * iformat
The input container format.
Definition: avformat.h:1326
enum AVCodecID ff_guess_image2_codec(const char *filename)
Definition: img2.c:100
#define av_free(p)
AVRational framerate
Set by a private option.
Definition: img2.h:53
struct AVCodecParserContext * parser
Definition: avformat.h:1070
void * priv_data
Format private data.
Definition: avformat.h:1342
static int jpegls_probe(AVProbeData *p)
Definition: img2dec.c:756
int use_glob
Definition: img2.h:56
static struct @205 state
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:661
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:306
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:2090
int stream_index
Definition: avcodec.h:1469
int img_first
Definition: img2.h:43
int(* io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **options)
Definition: avformat.h:1872
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
This structure stores compressed data.
Definition: avcodec.h:1444
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1460
int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: img2dec.c:371
AVInputFormat * av_iformat_next(const AVInputFormat *f)
If f is NULL, returns the first registered input format, if f is non-NULL, returns the next registere...
Definition: format.c:45
static int width