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  AVOpenCallback open_func = s1->open_cb;
381 
382  if (!open_func)
383  open_func = ffio_open2_wrapper;
384 
385  if (!s->is_pipe) {
386  /* loop over input */
387  if (s->loop && s->img_number > s->img_last) {
388  s->img_number = s->img_first;
389  }
390  if (s->img_number > s->img_last)
391  return AVERROR_EOF;
392  if (s->pattern_type == PT_NONE) {
393  av_strlcpy(filename_bytes, s->path, sizeof(filename_bytes));
394  } else if (s->use_glob) {
395 #if HAVE_GLOB
396  filename = s->globstate.gl_pathv[s->img_number];
397 #endif
398  } else {
399  if (av_get_frame_filename(filename_bytes, sizeof(filename_bytes),
400  s->path,
401  s->img_number) < 0 && s->img_number > 1)
402  return AVERROR(EIO);
403  }
404  for (i = 0; i < 3; i++) {
405  if (s1->pb &&
406  !strcmp(filename_bytes, s->path) &&
407  !s->loop &&
408  !s->split_planes) {
409  f[i] = s1->pb;
410  } else if (open_func(s1, &f[i], filename, AVIO_FLAG_READ,
411  &s1->interrupt_callback, NULL) < 0) {
412  if (i >= 1)
413  break;
414  av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",
415  filename);
416  return AVERROR(EIO);
417  }
418  size[i] = avio_size(f[i]);
419 
420  if (!s->split_planes)
421  break;
422  filename[strlen(filename) - 1] = 'U' + i;
423  }
424 
425  if (codec->codec_id == AV_CODEC_ID_NONE) {
426  AVProbeData pd = { 0 };
427  AVInputFormat *ifmt;
429  int ret;
430  int score = 0;
431 
432  ret = avio_read(f[0], header, PROBE_BUF_MIN);
433  if (ret < 0)
434  return ret;
435  memset(header + ret, 0, sizeof(header) - ret);
436  avio_skip(f[0], -ret);
437  pd.buf = header;
438  pd.buf_size = ret;
439  pd.filename = filename;
440 
441  ifmt = av_probe_input_format3(&pd, 1, &score);
442  if (ifmt && ifmt->read_packet == ff_img_read_packet && ifmt->raw_codec_id)
443  codec->codec_id = ifmt->raw_codec_id;
444  }
445 
446  if (codec->codec_id == AV_CODEC_ID_RAWVIDEO && !codec->width)
447  infer_size(&codec->width, &codec->height, size[0]);
448  } else {
449  f[0] = s1->pb;
450  if (avio_feof(f[0]) && s->loop && s->is_pipe)
451  avio_seek(f[0], 0, SEEK_SET);
452  if (avio_feof(f[0]))
453  return AVERROR_EOF;
454  if (s->frame_size > 0) {
455  size[0] = s->frame_size;
456  } else if (!s1->streams[0]->parser) {
457  size[0] = avio_size(s1->pb);
458  } else {
459  size[0] = 4096;
460  }
461  }
462 
463  res = av_new_packet(pkt, size[0] + size[1] + size[2]);
464  if (res < 0) {
465  goto fail;
466  }
467  pkt->stream_index = 0;
468  pkt->flags |= AV_PKT_FLAG_KEY;
469  if (s->ts_from_file) {
470  struct stat img_stat;
471  if (stat(filename, &img_stat)) {
472  res = AVERROR(EIO);
473  goto fail;
474  }
475  pkt->pts = (int64_t)img_stat.st_mtime;
476 #if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
477  if (s->ts_from_file == 2)
478  pkt->pts = 1000000000*pkt->pts + img_stat.st_mtim.tv_nsec;
479 #endif
480  av_add_index_entry(s1->streams[0], s->img_number, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
481  } else if (!s->is_pipe) {
482  pkt->pts = s->pts;
483  }
484 
485  if (s->is_pipe)
486  pkt->pos = avio_tell(f[0]);
487 
488  pkt->size = 0;
489  for (i = 0; i < 3; i++) {
490  if (f[i]) {
491  ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
492  if (s->loop && s->is_pipe && ret[i] == AVERROR_EOF) {
493  if (avio_seek(f[i], 0, SEEK_SET) >= 0) {
494  pkt->pos = 0;
495  ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
496  }
497  }
498  if (!s->is_pipe && f[i] != s1->pb)
499  avio_closep(&f[i]);
500  if (ret[i] > 0)
501  pkt->size += ret[i];
502  }
503  }
504 
505  if (ret[0] <= 0 || ret[1] < 0 || ret[2] < 0) {
506  av_packet_unref(pkt);
507  if (ret[0] < 0) {
508  res = ret[0];
509  } else if (ret[1] < 0) {
510  res = ret[1];
511  } else if (ret[2] < 0) {
512  res = ret[2];
513  } else {
514  res = AVERROR_EOF;
515  }
516  goto fail;
517  } else {
518  s->img_count++;
519  s->img_number++;
520  s->pts++;
521  return 0;
522  }
523 
524 fail:
525  if (!s->is_pipe) {
526  for (i = 0; i < 3; i++) {
527  if (f[i] != s1->pb)
528  avio_closep(&f[i]);
529  }
530  }
531  return res;
532 }
533 
534 static int img_read_close(struct AVFormatContext* s1)
535 {
536 #if HAVE_GLOB
537  VideoDemuxData *s = s1->priv_data;
538  if (s->use_glob) {
539  globfree(&s->globstate);
540  }
541 #endif
542  return 0;
543 }
544 
545 static int img_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
546 {
548  AVStream *st = s->streams[0];
549 
550  if (s1->ts_from_file) {
551  int index = av_index_search_timestamp(st, timestamp, flags);
552  if(index < 0)
553  return -1;
554  s1->img_number = st->index_entries[index].pos;
555  return 0;
556  }
557 
558  if (timestamp < 0 || !s1->loop && timestamp > s1->img_last - s1->img_first)
559  return -1;
560  s1->img_number = timestamp%(s1->img_last - s1->img_first + 1) + s1->img_first;
561  s1->pts = timestamp;
562  return 0;
563 }
564 
565 #define OFFSET(x) offsetof(VideoDemuxData, x)
566 #define DEC AV_OPT_FLAG_DECODING_PARAM
568  { "framerate", "set the video framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, DEC },
569  { "loop", "force loop over input file sequence", OFFSET(loop), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, DEC },
570 
571  { "pattern_type", "set pattern type", OFFSET(pattern_type), AV_OPT_TYPE_INT, {.i64=PT_DEFAULT}, 0, INT_MAX, DEC, "pattern_type"},
572  { "glob_sequence","select glob/sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB_SEQUENCE}, INT_MIN, INT_MAX, DEC, "pattern_type" },
573  { "glob", "select glob pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB }, INT_MIN, INT_MAX, DEC, "pattern_type" },
574  { "sequence", "select sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_SEQUENCE }, INT_MIN, INT_MAX, DEC, "pattern_type" },
575  { "none", "disable pattern matching", 0, AV_OPT_TYPE_CONST, {.i64=PT_NONE }, INT_MIN, INT_MAX, DEC, "pattern_type" },
576 
577  { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
578  { "start_number", "set first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, DEC },
579  { "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 },
580  { "video_size", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
581  { "frame_size", "force frame size in bytes", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
582  { "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" },
583  { "none", "none", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 2, DEC, "ts_type" },
584  { "sec", "second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 2, DEC, "ts_type" },
585  { "ns", "nano second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 2, DEC, "ts_type" },
586  { NULL },
587 };
588 
589 #if CONFIG_IMAGE2_DEMUXER
590 static const AVClass img2_class = {
591  .class_name = "image2 demuxer",
592  .item_name = av_default_item_name,
593  .option = ff_img_options,
594  .version = LIBAVUTIL_VERSION_INT,
595 };
596 AVInputFormat ff_image2_demuxer = {
597  .name = "image2",
598  .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
599  .priv_data_size = sizeof(VideoDemuxData),
605  .flags = AVFMT_NOFILE,
606  .priv_class = &img2_class,
607 };
608 #endif
609 #if CONFIG_IMAGE2PIPE_DEMUXER
610 static const AVClass img2pipe_class = {
611  .class_name = "image2pipe demuxer",
612  .item_name = av_default_item_name,
613  .option = ff_img_options,
614  .version = LIBAVUTIL_VERSION_INT,
615 };
616 AVInputFormat ff_image2pipe_demuxer = {
617  .name = "image2pipe",
618  .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
619  .priv_data_size = sizeof(VideoDemuxData),
622  .priv_class = &img2pipe_class,
623 };
624 #endif
625 
626 static int bmp_probe(AVProbeData *p)
627 {
628  const uint8_t *b = p->buf;
629  int ihsize;
630 
631  if (AV_RB16(b) != 0x424d)
632  return 0;
633 
634  ihsize = AV_RL32(b+14);
635  if (ihsize < 12 || ihsize > 255)
636  return 0;
637 
638  if (!AV_RN32(b + 6)) {
639  return AVPROBE_SCORE_EXTENSION + 1;
640  }
641  return AVPROBE_SCORE_EXTENSION / 4;
642 }
643 
644 static int dds_probe(AVProbeData *p)
645 {
646  const uint8_t *b = p->buf;
647 
648  if ( AV_RB64(b) == 0x444453207c000000
649  && AV_RL32(b + 8)
650  && AV_RL32(b + 12))
651  return AVPROBE_SCORE_MAX - 1;
652  return 0;
653 }
654 
655 static int dpx_probe(AVProbeData *p)
656 {
657  const uint8_t *b = p->buf;
658  int w, h;
659  int is_big = (AV_RN32(b) == AV_RN32("SDPX"));
660 
661  if (p->buf_size < 0x304+8)
662  return 0;
663  w = is_big ? AV_RB32(p->buf + 0x304) : AV_RL32(p->buf + 0x304);
664  h = is_big ? AV_RB32(p->buf + 0x308) : AV_RL32(p->buf + 0x308);
665  if (w <= 0 || h <= 0)
666  return 0;
667 
668  if (is_big || AV_RN32(b) == AV_RN32("XPDS"))
669  return AVPROBE_SCORE_EXTENSION + 1;
670  return 0;
671 }
672 
673 static int exr_probe(AVProbeData *p)
674 {
675  const uint8_t *b = p->buf;
676 
677  if (AV_RL32(b) == 20000630)
678  return AVPROBE_SCORE_EXTENSION + 1;
679  return 0;
680 }
681 
682 static int j2k_probe(AVProbeData *p)
683 {
684  const uint8_t *b = p->buf;
685 
686  if (AV_RB64(b) == 0x0000000c6a502020 ||
687  AV_RB32(b) == 0xff4fff51)
688  return AVPROBE_SCORE_EXTENSION + 1;
689  return 0;
690 }
691 
692 static int jpeg_probe(AVProbeData *p)
693 {
694  const uint8_t *b = p->buf;
695  int i, state = 0xD8;
696 
697  if (AV_RB16(b) != 0xFFD8 ||
698  AV_RB32(b) == 0xFFD8FFF7)
699  return 0;
700 
701  b += 2;
702  for (i = 0; i < p->buf_size - 3; i++) {
703  int c;
704  if (b[i] != 0xFF)
705  continue;
706  c = b[i + 1];
707  switch (c) {
708  case 0xD8:
709  return 0;
710  case 0xC0:
711  case 0xC1:
712  case 0xC2:
713  case 0xC3:
714  case 0xC5:
715  case 0xC6:
716  case 0xC7:
717  if (state != 0xD8)
718  return 0;
719  state = 0xC0;
720  break;
721  case 0xDA:
722  if (state != 0xC0)
723  return 0;
724  state = 0xDA;
725  break;
726  case 0xD9:
727  if (state != 0xDA)
728  return 0;
729  state = 0xD9;
730  break;
731  case 0xE0:
732  case 0xE1:
733  case 0xE2:
734  case 0xE3:
735  case 0xE4:
736  case 0xE5:
737  case 0xE6:
738  case 0xE7:
739  case 0xE8:
740  case 0xE9:
741  case 0xEA:
742  case 0xEB:
743  case 0xEC:
744  case 0xED:
745  case 0xEE:
746  case 0xEF:
747  i += AV_RB16(&b[i + 2]) + 1;
748  break;
749  default:
750  if ( (c >= 0x02 && c <= 0xBF)
751  || c == 0xC8)
752  return 0;
753  }
754  }
755 
756  if (state == 0xD9)
757  return AVPROBE_SCORE_EXTENSION + 1;
758  return AVPROBE_SCORE_EXTENSION / 8;
759 }
760 
761 static int jpegls_probe(AVProbeData *p)
762 {
763  const uint8_t *b = p->buf;
764 
765  if (AV_RB32(b) == 0xffd8fff7)
766  return AVPROBE_SCORE_EXTENSION + 1;
767  return 0;
768 }
769 
770 static int qdraw_probe(AVProbeData *p)
771 {
772  const uint8_t *b = p->buf;
773 
774  if ( p->buf_size >= 528
775  && (AV_RB64(b + 520) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
776  && AV_RB16(b + 520)
777  && AV_RB16(b + 518))
778  return AVPROBE_SCORE_MAX * 3 / 4;
779  if ( (AV_RB64(b + 8) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
780  && AV_RB16(b + 8)
781  && AV_RB16(b + 6))
782  return AVPROBE_SCORE_EXTENSION / 4;
783  return 0;
784 }
785 
786 static int pictor_probe(AVProbeData *p)
787 {
788  const uint8_t *b = p->buf;
789 
790  if (AV_RL16(b) == 0x1234)
791  return AVPROBE_SCORE_EXTENSION / 4;
792  return 0;
793 }
794 
795 static int png_probe(AVProbeData *p)
796 {
797  const uint8_t *b = p->buf;
798 
799  if (AV_RB64(b) == 0x89504e470d0a1a0a)
800  return AVPROBE_SCORE_MAX - 1;
801  return 0;
802 }
803 
804 static int sgi_probe(AVProbeData *p)
805 {
806  const uint8_t *b = p->buf;
807 
808  if (AV_RB16(b) == 474 &&
809  (b[2] & ~1) == 0 &&
810  (b[3] & ~3) == 0 && b[3] &&
811  (AV_RB16(b + 4) & ~7) == 0 && AV_RB16(b + 4))
812  return AVPROBE_SCORE_EXTENSION + 1;
813  return 0;
814 }
815 
817 {
818  const uint8_t *b = p->buf;
819 
820  if (AV_RB32(b) == 0x59a66a95)
821  return AVPROBE_SCORE_EXTENSION + 1;
822  return 0;
823 }
824 
825 static int tiff_probe(AVProbeData *p)
826 {
827  const uint8_t *b = p->buf;
828 
829  if (AV_RB32(b) == 0x49492a00 ||
830  AV_RB32(b) == 0x4D4D002a)
831  return AVPROBE_SCORE_EXTENSION + 1;
832  return 0;
833 }
834 
835 static int webp_probe(AVProbeData *p)
836 {
837  const uint8_t *b = p->buf;
838 
839  if (AV_RB32(b) == 0x52494646 &&
840  AV_RB32(b + 8) == 0x57454250)
841  return AVPROBE_SCORE_MAX - 1;
842  return 0;
843 }
844 
845 #define IMAGEAUTO_DEMUXER(imgname, codecid)\
846 static const AVClass imgname ## _class = {\
847  .class_name = AV_STRINGIFY(imgname) " demuxer",\
848  .item_name = av_default_item_name,\
849  .option = ff_img_options,\
850  .version = LIBAVUTIL_VERSION_INT,\
851 };\
852 AVInputFormat ff_image_ ## imgname ## _pipe_demuxer = {\
853  .name = AV_STRINGIFY(imgname) "_pipe",\
854  .long_name = NULL_IF_CONFIG_SMALL("piped " AV_STRINGIFY(imgname) " sequence"),\
855  .priv_data_size = sizeof(VideoDemuxData),\
856  .read_probe = imgname ## _probe,\
857  .read_header = ff_img_read_header,\
858  .read_packet = ff_img_read_packet,\
859  .priv_class = & imgname ## _class,\
860  .flags = AVFMT_GENERIC_INDEX, \
861  .raw_codec_id = codecid,\
862 };
863 
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:545
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:287
static int bmp_probe(AVProbeData *p)
Definition: img2dec.c:626
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1566
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:1776
#define OFFSET(x)
Definition: img2dec.c:565
static int qdraw_probe(AVProbeData *p)
Definition: img2dec.c:770
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:4147
int64_t pos
Definition: avformat.h:817
char path[1024]
Definition: img2.h:50
static int j2k_probe(AVProbeData *p)
Definition: img2dec.c:682
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:534
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:795
int(* open_cb)(struct AVFormatContext *s, AVIOContext **p, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Called to open further IO contexts when needed for demuxing.
Definition: avformat.h:1839
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:845
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:644
#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:786
enum AVStreamParseType need_parsing
Definition: avformat.h:1069
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:3803
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:835
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:567
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:673
#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:1875
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:79
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:692
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
int ffio_open2_wrapper(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Definition: aviobuf.c:956
Definition: img2.h:35
static int sgi_probe(AVProbeData *p)
Definition: img2dec.c:804
#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:3972
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:566
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:265
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:816
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:655
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:472
static int tiff_probe(AVProbeData *p)
Definition: img2dec.c:825
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)
int(* AVOpenCallback)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Definition: avformat.h:1291
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:761
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
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
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
Definition: aviobuf.c:981
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