FFmpeg  4.4.9
nutdec.c
Go to the documentation of this file.
1 /*
2  * "NUT" Container Format demuxer
3  * Copyright (c) 2004-2006 Michael Niedermayer
4  * Copyright (c) 2003 Alex Beregszaszi
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "libavutil/avstring.h"
24 #include "libavutil/avassert.h"
25 #include "libavutil/bswap.h"
26 #include "libavutil/dict.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/mathematics.h"
29 #include "libavutil/tree.h"
30 #include "libavcodec/bytestream.h"
31 #include "avio_internal.h"
32 #include "isom.h"
33 #include "nut.h"
34 #include "riff.h"
35 
36 static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index,
37  int64_t *pos_arg, int64_t pos_limit);
38 
39 static int get_str(AVIOContext *bc, char *string, unsigned int maxlen)
40 {
41  unsigned int len = ffio_read_varlen(bc);
42 
43  if (len && maxlen)
44  avio_read(bc, string, FFMIN(len, maxlen));
45  while (len > maxlen) {
46  avio_r8(bc);
47  len--;
48  if (bc->eof_reached)
49  len = maxlen;
50  }
51 
52  if (maxlen)
53  string[FFMIN(len, maxlen - 1)] = 0;
54 
55  if (bc->eof_reached)
56  return AVERROR_EOF;
57  if (maxlen == len)
58  return -1;
59  else
60  return 0;
61 }
62 
64 {
65  int64_t v = ffio_read_varlen(bc) + 1;
66 
67  if (v & 1)
68  return -(v >> 1);
69  else
70  return (v >> 1);
71 }
72 
73 static uint64_t get_fourcc(AVIOContext *bc)
74 {
75  unsigned int len = ffio_read_varlen(bc);
76 
77  if (len == 2)
78  return avio_rl16(bc);
79  else if (len == 4)
80  return avio_rl32(bc);
81  else {
82  av_log(NULL, AV_LOG_ERROR, "Unsupported fourcc length %d\n", len);
83  return -1;
84  }
85 }
86 
88  int calculate_checksum, uint64_t startcode)
89 {
90  int64_t size;
91 
92  startcode = av_be2ne64(startcode);
93  startcode = ff_crc04C11DB7_update(0, (uint8_t*) &startcode, 8);
94 
96  size = ffio_read_varlen(bc);
97  if (size > 4096)
98  avio_rb32(bc);
99  if (ffio_get_checksum(bc) && size > 4096)
100  return -1;
101 
102  ffio_init_checksum(bc, calculate_checksum ? ff_crc04C11DB7_update : NULL, 0);
103 
104  return size;
105 }
106 
108 {
109  uint64_t state = 0;
110 
111  if (pos >= 0)
112  /* Note, this may fail if the stream is not seekable, but that should
113  * not matter, as in this case we simply start where we currently are */
114  avio_seek(bc, pos, SEEK_SET);
115  while (!avio_feof(bc)) {
116  state = (state << 8) | avio_r8(bc);
117  if ((state >> 56) != 'N')
118  continue;
119  switch (state) {
120  case MAIN_STARTCODE:
121  case STREAM_STARTCODE:
122  case SYNCPOINT_STARTCODE:
123  case INFO_STARTCODE:
124  case INDEX_STARTCODE:
125  return state;
126  }
127  }
128 
129  return 0;
130 }
131 
132 /**
133  * Find the given startcode.
134  * @param code the startcode
135  * @param pos the start position of the search, or -1 if the current position
136  * @return the position of the startcode or -1 if not found
137  */
139 {
140  for (;;) {
141  uint64_t startcode = find_any_startcode(bc, pos);
142  if (startcode == code)
143  return avio_tell(bc) - 8;
144  else if (startcode == 0)
145  return -1;
146  pos = -1;
147  }
148 }
149 
150 static int nut_probe(const AVProbeData *p)
151 {
152  int i;
153 
154  for (i = 0; i < p->buf_size-8; i++) {
155  if (AV_RB32(p->buf+i) != MAIN_STARTCODE>>32)
156  continue;
157  if (AV_RB32(p->buf+i+4) == (MAIN_STARTCODE & 0xFFFFFFFF))
158  return AVPROBE_SCORE_MAX;
159  }
160  return 0;
161 }
162 
163 #define GET_V(dst, check) \
164  do { \
165  tmp = ffio_read_varlen(bc); \
166  if (!(check)) { \
167  av_log(s, AV_LOG_ERROR, "Error " #dst " is (%"PRId64")\n", tmp); \
168  ret = AVERROR_INVALIDDATA; \
169  goto fail; \
170  } \
171  dst = tmp; \
172  } while (0)
173 
175 {
176  pos -= avio_tell(bc);
177  if (pos < 0) {
178  avio_seek(bc, pos, SEEK_CUR);
179  return AVERROR_INVALIDDATA;
180  } else {
181  while (pos--) {
182  if (bc->eof_reached)
183  return AVERROR_INVALIDDATA;
184  avio_r8(bc);
185  }
186  return 0;
187  }
188 }
189 
191 {
192  AVFormatContext *s = nut->avf;
193  AVIOContext *bc = s->pb;
194  uint64_t tmp, end, length;
195  unsigned int stream_count;
196  int i, j, count, ret;
197  int tmp_stream, tmp_mul, tmp_pts, tmp_size, tmp_res, tmp_head_idx;
198 
199  length = get_packetheader(nut, bc, 1, MAIN_STARTCODE);
200  if (length == (uint64_t)-1)
201  return AVERROR_INVALIDDATA;
202  end = length + avio_tell(bc);
203 
204  nut->version = ffio_read_varlen(bc);
205  if (nut->version < NUT_MIN_VERSION ||
206  nut->version > NUT_MAX_VERSION) {
207  av_log(s, AV_LOG_ERROR, "Version %d not supported.\n",
208  nut->version);
209  return AVERROR(ENOSYS);
210  }
211  if (nut->version > 3)
212  nut->minor_version = ffio_read_varlen(bc);
213 
214  GET_V(stream_count, tmp > 0 && tmp <= s->max_streams);
215 
216  nut->max_distance = ffio_read_varlen(bc);
217  if (nut->max_distance > 65536) {
218  av_log(s, AV_LOG_DEBUG, "max_distance %d\n", nut->max_distance);
219  nut->max_distance = 65536;
220  }
221 
222  GET_V(nut->time_base_count, tmp > 0 && tmp < INT_MAX / sizeof(AVRational) && tmp < length/2);
223  nut->time_base = av_malloc_array(nut->time_base_count, sizeof(AVRational));
224  if (!nut->time_base)
225  return AVERROR(ENOMEM);
226 
227  for (i = 0; i < nut->time_base_count; i++) {
228  GET_V(nut->time_base[i].num, tmp > 0 && tmp < (1ULL << 31));
229  GET_V(nut->time_base[i].den, tmp > 0 && tmp < (1ULL << 31));
230  if (av_gcd(nut->time_base[i].num, nut->time_base[i].den) != 1) {
231  av_log(s, AV_LOG_ERROR, "invalid time base %d/%d\n",
232  nut->time_base[i].num,
233  nut->time_base[i].den);
234  ret = AVERROR_INVALIDDATA;
235  goto fail;
236  }
237  }
238  tmp_pts = 0;
239  tmp_mul = 1;
240  tmp_stream = 0;
241  tmp_head_idx = 0;
242  for (i = 0; i < 256;) {
243  int tmp_flags = ffio_read_varlen(bc);
244  int tmp_fields = ffio_read_varlen(bc);
245  if (tmp_fields < 0) {
246  av_log(s, AV_LOG_ERROR, "fields %d is invalid\n", tmp_fields);
247  ret = AVERROR_INVALIDDATA;
248  goto fail;
249  }
250 
251  if (tmp_fields > 0)
252  tmp_pts = get_s(bc);
253  if (tmp_fields > 1)
254  tmp_mul = ffio_read_varlen(bc);
255  if (tmp_fields > 2)
256  tmp_stream = ffio_read_varlen(bc);
257  if (tmp_fields > 3)
258  tmp_size = ffio_read_varlen(bc);
259  else
260  tmp_size = 0;
261  if (tmp_fields > 4)
262  tmp_res = ffio_read_varlen(bc);
263  else
264  tmp_res = 0;
265  if (tmp_fields > 5)
266  count = ffio_read_varlen(bc);
267  else
268  count = tmp_mul - (unsigned)tmp_size;
269  if (tmp_fields > 6)
270  get_s(bc);
271  if (tmp_fields > 7)
272  tmp_head_idx = ffio_read_varlen(bc);
273 
274  while (tmp_fields-- > 8) {
275  if (bc->eof_reached) {
276  av_log(s, AV_LOG_ERROR, "reached EOF while decoding main header\n");
277  ret = AVERROR_INVALIDDATA;
278  goto fail;
279  }
280  ffio_read_varlen(bc);
281  }
282 
283  if (count <= 0 || count > 256 - (i <= 'N') - i) {
284  av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i);
285  ret = AVERROR_INVALIDDATA;
286  goto fail;
287  }
288  if (tmp_stream >= stream_count) {
289  av_log(s, AV_LOG_ERROR, "illegal stream number %d >= %d\n",
290  tmp_stream, stream_count);
291  ret = AVERROR_INVALIDDATA;
292  goto fail;
293  }
294  if (tmp_size < 0 || tmp_size > INT_MAX - count) {
295  av_log(s, AV_LOG_ERROR, "illegal size\n");
296  ret = AVERROR_INVALIDDATA;
297  goto fail;
298  }
299 
300  for (j = 0; j < count; j++, i++) {
301  if (i == 'N') {
302  nut->frame_code[i].flags = FLAG_INVALID;
303  j--;
304  continue;
305  }
306  nut->frame_code[i].flags = tmp_flags;
307  nut->frame_code[i].pts_delta = tmp_pts;
308  nut->frame_code[i].stream_id = tmp_stream;
309  nut->frame_code[i].size_mul = tmp_mul;
310  nut->frame_code[i].size_lsb = tmp_size + j;
311  nut->frame_code[i].reserved_count = tmp_res;
312  nut->frame_code[i].header_idx = tmp_head_idx;
313  }
314  }
315  av_assert0(nut->frame_code['N'].flags == FLAG_INVALID);
316 
317  if (end > avio_tell(bc) + 4) {
318  int rem = 1024;
319  GET_V(nut->header_count, tmp < 128U);
320  nut->header_count++;
321  for (i = 1; i < nut->header_count; i++) {
322  uint8_t *hdr;
323  GET_V(nut->header_len[i], tmp > 0 && tmp < 256);
324  if (rem < nut->header_len[i]) {
326  "invalid elision header %d : %d > %d\n",
327  i, nut->header_len[i], rem);
328  ret = AVERROR_INVALIDDATA;
329  goto fail;
330  }
331  rem -= nut->header_len[i];
332  hdr = av_malloc(nut->header_len[i]);
333  if (!hdr) {
334  ret = AVERROR(ENOMEM);
335  goto fail;
336  }
337  avio_read(bc, hdr, nut->header_len[i]);
338  nut->header[i] = hdr;
339  }
340  av_assert0(nut->header_len[0] == 0);
341  }
342 
343  // flags had been effectively introduced in version 4
344  if (nut->version > 3 && end > avio_tell(bc) + 4) {
345  nut->flags = ffio_read_varlen(bc);
346  }
347 
348  if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
349  av_log(s, AV_LOG_ERROR, "main header checksum mismatch\n");
350  ret = AVERROR_INVALIDDATA;
351  goto fail;
352  }
353 
354  nut->stream = av_calloc(stream_count, sizeof(StreamContext));
355  if (!nut->stream) {
356  ret = AVERROR(ENOMEM);
357  goto fail;
358  }
359  for (i = 0; i < stream_count; i++) {
360  if (!avformat_new_stream(s, NULL)) {
361  ret = AVERROR(ENOMEM);
362  goto fail;
363  }
364  }
365 
366  return 0;
367 fail:
368  av_freep(&nut->time_base);
369  for (i = 1; i < nut->header_count; i++) {
370  av_freep(&nut->header[i]);
371  }
372  nut->header_count = 0;
373  return ret;
374 }
375 
377 {
378  AVFormatContext *s = nut->avf;
379  AVIOContext *bc = s->pb;
380  StreamContext *stc;
381  int class, stream_id, ret;
382  uint64_t tmp, end;
383  AVStream *st = NULL;
384 
385  end = get_packetheader(nut, bc, 1, STREAM_STARTCODE);
386  end += avio_tell(bc);
387 
388  GET_V(stream_id, tmp < s->nb_streams && !nut->stream[tmp].time_base);
389  stc = &nut->stream[stream_id];
390  st = s->streams[stream_id];
391  if (!st)
392  return AVERROR(ENOMEM);
393 
394  class = ffio_read_varlen(bc);
395  tmp = get_fourcc(bc);
396  st->codecpar->codec_tag = tmp;
397  switch (class) {
398  case 0:
400  st->codecpar->codec_id = av_codec_get_id((const AVCodecTag * const []) {
404  0
405  },
406  tmp);
407  break;
408  case 1:
410  st->codecpar->codec_id = av_codec_get_id((const AVCodecTag * const []) {
414  0
415  },
416  tmp);
417  break;
418  case 2:
421  break;
422  case 3:
425  break;
426  default:
427  av_log(s, AV_LOG_ERROR, "unknown stream class (%d)\n", class);
428  return AVERROR(ENOSYS);
429  }
430  if (class < 3 && st->codecpar->codec_id == AV_CODEC_ID_NONE)
432  "Unknown codec tag '0x%04x' for stream number %d\n",
433  (unsigned int) tmp, stream_id);
434 
435  GET_V(stc->time_base_id, tmp < nut->time_base_count);
436  GET_V(stc->msb_pts_shift, tmp < 16);
438  GET_V(stc->decode_delay, tmp < 1000); // sanity limit, raise this if Moore's law is true
439  st->codecpar->video_delay = stc->decode_delay;
440  ffio_read_varlen(bc); // stream flags
441 
442  GET_V(st->codecpar->extradata_size, tmp < (1 << 30));
443  if (st->codecpar->extradata_size) {
444  ret = ff_get_extradata(s, st->codecpar, bc,
445  st->codecpar->extradata_size);
446  if (ret < 0)
447  return ret;
448  }
449 
450  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
451  GET_V(st->codecpar->width, tmp > 0);
452  GET_V(st->codecpar->height, tmp > 0);
455  if ((!st->sample_aspect_ratio.num) != (!st->sample_aspect_ratio.den)) {
456  av_log(s, AV_LOG_ERROR, "invalid aspect ratio %d/%d\n",
458  ret = AVERROR_INVALIDDATA;
459  goto fail;
460  }
461  ffio_read_varlen(bc); /* csp type */
462  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
463  GET_V(st->codecpar->sample_rate, tmp > 0);
464  ffio_read_varlen(bc); // samplerate_den
465  GET_V(st->codecpar->channels, tmp > 0);
466  }
467  if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
469  "stream header %d checksum mismatch\n", stream_id);
470  ret = AVERROR_INVALIDDATA;
471  goto fail;
472  }
473  stc->time_base = &nut->time_base[stc->time_base_id];
474  avpriv_set_pts_info(s->streams[stream_id], 63, stc->time_base->num,
475  stc->time_base->den);
476  return 0;
477 fail:
478  if (st && st->codecpar) {
479  av_freep(&st->codecpar->extradata);
480  st->codecpar->extradata_size = 0;
481  }
482  return ret;
483 }
484 
486  int stream_id)
487 {
488  int flag = 0, i;
489 
490  for (i = 0; ff_nut_dispositions[i].flag; ++i)
491  if (!strcmp(ff_nut_dispositions[i].str, value))
493  if (!flag)
494  av_log(avf, AV_LOG_INFO, "unknown disposition type '%s'\n", value);
495  for (i = 0; i < avf->nb_streams; ++i)
496  if (stream_id == i || stream_id == -1)
497  avf->streams[i]->disposition |= flag;
498 }
499 
501 {
502  AVFormatContext *s = nut->avf;
503  AVIOContext *bc = s->pb;
504  uint64_t tmp, chapter_start, chapter_len;
505  unsigned int stream_id_plus1, count;
506  int i, ret = 0;
507  int64_t chapter_id, value, end;
508  char name[256], str_value[1024], type_str[256];
509  const char *type;
510  int *event_flags = NULL;
511  AVChapter *chapter = NULL;
512  AVStream *st = NULL;
513  AVDictionary **metadata = NULL;
514  int metadata_flag = 0;
515 
516  end = get_packetheader(nut, bc, 1, INFO_STARTCODE);
517  end += avio_tell(bc);
518 
519  GET_V(stream_id_plus1, tmp <= s->nb_streams);
520  chapter_id = get_s(bc);
521  chapter_start = ffio_read_varlen(bc);
522  chapter_len = ffio_read_varlen(bc);
523  count = ffio_read_varlen(bc);
524 
525  if (chapter_id && !stream_id_plus1) {
526  int64_t start = chapter_start / nut->time_base_count;
527  chapter = avpriv_new_chapter(s, chapter_id,
528  nut->time_base[chapter_start %
529  nut->time_base_count],
530  start, start + chapter_len, NULL);
531  if (!chapter) {
532  av_log(s, AV_LOG_ERROR, "Could not create chapter.\n");
533  return AVERROR(ENOMEM);
534  }
535  metadata = &chapter->metadata;
536  } else if (stream_id_plus1) {
537  st = s->streams[stream_id_plus1 - 1];
538  metadata = &st->metadata;
539  event_flags = &st->event_flags;
540  metadata_flag = AVSTREAM_EVENT_FLAG_METADATA_UPDATED;
541  } else {
542  metadata = &s->metadata;
543  event_flags = &s->event_flags;
544  metadata_flag = AVFMT_EVENT_FLAG_METADATA_UPDATED;
545  }
546 
547  for (i = 0; i < count; i++) {
548  ret = get_str(bc, name, sizeof(name));
549  if (ret < 0) {
550  av_log(s, AV_LOG_ERROR, "get_str failed while decoding info header\n");
551  return ret;
552  }
553  value = get_s(bc);
554  str_value[0] = 0;
555 
556  if (value == -1) {
557  type = "UTF-8";
558  ret = get_str(bc, str_value, sizeof(str_value));
559  } else if (value == -2) {
560  ret = get_str(bc, type_str, sizeof(type_str));
561  if (ret < 0) {
562  av_log(s, AV_LOG_ERROR, "get_str failed while decoding info header\n");
563  return ret;
564  }
565  type = type_str;
566  ret = get_str(bc, str_value, sizeof(str_value));
567  } else if (value == -3) {
568  type = "s";
569  value = get_s(bc);
570  } else if (value == -4) {
571  type = "t";
572  value = ffio_read_varlen(bc);
573  } else if (value < -4) {
574  type = "r";
575  get_s(bc);
576  } else {
577  type = "v";
578  }
579 
580  if (ret < 0) {
581  av_log(s, AV_LOG_ERROR, "get_str failed while decoding info header\n");
582  return ret;
583  }
584 
585  if (stream_id_plus1 > s->nb_streams) {
587  "invalid stream id %d for info packet\n",
588  stream_id_plus1);
589  continue;
590  }
591 
592  if (!strcmp(type, "UTF-8")) {
593  if (chapter_id == 0 && !strcmp(name, "Disposition")) {
594  set_disposition_bits(s, str_value, stream_id_plus1 - 1);
595  continue;
596  }
597 
598  if (stream_id_plus1 && !strcmp(name, "r_frame_rate")) {
599  sscanf(str_value, "%d/%d", &st->r_frame_rate.num, &st->r_frame_rate.den);
600  if (st->r_frame_rate.num >= 1000LL*st->r_frame_rate.den ||
601  st->r_frame_rate.num < 0 || st->r_frame_rate.den < 0)
602  st->r_frame_rate.num = st->r_frame_rate.den = 0;
603  continue;
604  }
605 
606  if (metadata && av_strcasecmp(name, "Uses") &&
607  av_strcasecmp(name, "Depends") && av_strcasecmp(name, "Replaces")) {
608  if (event_flags)
609  *event_flags |= metadata_flag;
610  av_dict_set(metadata, name, str_value, 0);
611  }
612  }
613  }
614 
615  if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
616  av_log(s, AV_LOG_ERROR, "info header checksum mismatch\n");
617  return AVERROR_INVALIDDATA;
618  }
619 fail:
620  return FFMIN(ret, 0);
621 }
622 
623 static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr)
624 {
625  AVFormatContext *s = nut->avf;
626  AVIOContext *bc = s->pb;
627  int64_t end;
628  uint64_t tmp;
629  int ret;
630 
631  nut->last_syncpoint_pos = avio_tell(bc) - 8;
632 
633  end = get_packetheader(nut, bc, 1, SYNCPOINT_STARTCODE);
634  end += avio_tell(bc);
635 
636  tmp = ffio_read_varlen(bc);
637  *back_ptr = nut->last_syncpoint_pos - 16 * ffio_read_varlen(bc);
638  if (*back_ptr < 0)
639  return AVERROR_INVALIDDATA;
640 
641  ff_nut_reset_ts(nut, nut->time_base[tmp % nut->time_base_count],
642  tmp / nut->time_base_count);
643 
644  if (nut->flags & NUT_BROADCAST) {
645  tmp = ffio_read_varlen(bc);
646  av_log(s, AV_LOG_VERBOSE, "Syncpoint wallclock %"PRId64"\n",
648  nut->time_base[tmp % nut->time_base_count],
649  AV_TIME_BASE_Q));
650  }
651 
652  if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
653  av_log(s, AV_LOG_ERROR, "sync point checksum mismatch\n");
654  return AVERROR_INVALIDDATA;
655  }
656 
657  *ts = tmp / nut->time_base_count *
659 
660  if ((ret = ff_nut_add_sp(nut, nut->last_syncpoint_pos, *back_ptr, *ts)) < 0)
661  return ret;
662 
663  return 0;
664 }
665 
666 //FIXME calculate exactly, this is just a good approximation.
667 static int64_t find_duration(NUTContext *nut, int64_t filesize)
668 {
669  AVFormatContext *s = nut->avf;
670  int64_t duration = 0;
671 
673 
674  if(duration > 0)
675  s->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
676  return duration;
677 }
678 
680 {
681  AVFormatContext *s = nut->avf;
682  AVIOContext *bc = s->pb;
683  uint64_t tmp, end;
684  int i, j, syncpoint_count;
685  int64_t filesize = avio_size(bc);
686  int64_t *syncpoints = NULL;
687  uint64_t max_pts;
688  int8_t *has_keyframe = NULL;
689  int ret = AVERROR_INVALIDDATA;
690 
691  if(filesize <= 0)
692  return -1;
693 
694  avio_seek(bc, filesize - 12, SEEK_SET);
695  avio_seek(bc, filesize - avio_rb64(bc), SEEK_SET);
696  if (avio_rb64(bc) != INDEX_STARTCODE) {
697  av_log(s, AV_LOG_WARNING, "no index at the end\n");
698 
699  if(s->duration<=0)
700  s->duration = find_duration(nut, filesize);
701  return ret;
702  }
703 
704  end = get_packetheader(nut, bc, 1, INDEX_STARTCODE);
705  end += avio_tell(bc);
706 
707  max_pts = ffio_read_varlen(bc);
708  s->duration = av_rescale_q(max_pts / nut->time_base_count,
709  nut->time_base[max_pts % nut->time_base_count],
711  s->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
712 
713  GET_V(syncpoint_count, tmp < INT_MAX / 8 && tmp > 0);
714  syncpoints = av_malloc_array(syncpoint_count, sizeof(int64_t));
715  has_keyframe = av_malloc_array(syncpoint_count + 1, sizeof(int8_t));
716  if (!syncpoints || !has_keyframe) {
717  ret = AVERROR(ENOMEM);
718  goto fail;
719  }
720  for (i = 0; i < syncpoint_count; i++) {
721  syncpoints[i] = ffio_read_varlen(bc);
722  if (syncpoints[i] <= 0)
723  goto fail;
724  if (i)
725  syncpoints[i] += syncpoints[i - 1];
726  }
727 
728  for (i = 0; i < s->nb_streams; i++) {
729  int64_t last_pts = -1;
730  for (j = 0; j < syncpoint_count;) {
731  uint64_t x = ffio_read_varlen(bc);
732  int type = x & 1;
733  int n = j;
734  x >>= 1;
735  if (type) {
736  int flag = x & 1;
737  x >>= 1;
738  if (n + x >= syncpoint_count + 1) {
739  av_log(s, AV_LOG_ERROR, "index overflow A %d + %"PRIu64" >= %d\n", n, x, syncpoint_count + 1);
740  goto fail;
741  }
742  while (x--)
743  has_keyframe[n++] = flag;
744  has_keyframe[n++] = !flag;
745  } else {
746  if (x <= 1) {
747  av_log(s, AV_LOG_ERROR, "index: x %"PRIu64" is invalid\n", x);
748  goto fail;
749  }
750  while (x != 1) {
751  if (n >= syncpoint_count + 1) {
752  av_log(s, AV_LOG_ERROR, "index overflow B\n");
753  goto fail;
754  }
755  has_keyframe[n++] = x & 1;
756  x >>= 1;
757  }
758  }
759  if (has_keyframe[0]) {
760  av_log(s, AV_LOG_ERROR, "keyframe before first syncpoint in index\n");
761  goto fail;
762  }
763  av_assert0(n <= syncpoint_count + 1);
764  for (; j < n && j < syncpoint_count; j++) {
765  if (has_keyframe[j]) {
766  uint64_t B, A = ffio_read_varlen(bc);
767  if (!A) {
768  A = ffio_read_varlen(bc);
769  B = ffio_read_varlen(bc);
770  // eor_pts[j][i] = last_pts + A + B
771  } else
772  B = 0;
773  av_add_index_entry(s->streams[i], 16 * syncpoints[j - 1],
774  last_pts + A, 0, 0, AVINDEX_KEYFRAME);
775  last_pts += A + B;
776  }
777  }
778  }
779  }
780 
781  if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
782  av_log(s, AV_LOG_ERROR, "index checksum mismatch\n");
783  goto fail;
784  }
785  ret = 0;
786 
787 fail:
788  av_free(syncpoints);
789  av_free(has_keyframe);
790  return ret;
791 }
792 
794 {
795  NUTContext *nut = s->priv_data;
796  int i;
797 
798  av_freep(&nut->time_base);
799  av_freep(&nut->stream);
800  ff_nut_free_sp(nut);
801  for (i = 1; i < nut->header_count; i++)
802  av_freep(&nut->header[i]);
803 
804  return 0;
805 }
806 
808 {
809  NUTContext *nut = s->priv_data;
810  AVIOContext *bc = s->pb;
811  int64_t pos;
812  int initialized_stream_count, ret;
813 
814  nut->avf = s;
815 
816  /* main header */
817  pos = 0;
818  ret = 0;
819  do {
820  if (ret == AVERROR(ENOMEM))
821  return ret;
822 
823  pos = find_startcode(bc, MAIN_STARTCODE, pos) + 1;
824  if (pos < 0 + 1) {
825  av_log(s, AV_LOG_ERROR, "No main startcode found.\n");
826  goto fail;
827  }
828  } while ((ret = decode_main_header(nut)) < 0);
829 
830  /* stream headers */
831  pos = 0;
832  for (initialized_stream_count = 0; initialized_stream_count < s->nb_streams;) {
834  if (pos < 0 + 1) {
835  av_log(s, AV_LOG_ERROR, "Not all stream headers found.\n");
836  goto fail;
837  }
838  if (decode_stream_header(nut) >= 0)
839  initialized_stream_count++;
840  }
841 
842  /* info headers */
843  pos = 0;
844  for (;;) {
845  uint64_t startcode = find_any_startcode(bc, pos);
846  pos = avio_tell(bc);
847 
848  if (startcode == 0) {
849  av_log(s, AV_LOG_ERROR, "EOF before video frames\n");
850  goto fail;
851  } else if (startcode == SYNCPOINT_STARTCODE) {
852  nut->next_startcode = startcode;
853  break;
854  } else if (startcode != INFO_STARTCODE) {
855  continue;
856  }
857 
858  decode_info_header(nut);
859  }
860 
861  s->internal->data_offset = pos - 8;
862 
863  if (bc->seekable & AVIO_SEEKABLE_NORMAL) {
864  int64_t orig_pos = avio_tell(bc);
866  avio_seek(bc, orig_pos, SEEK_SET);
867  }
869 
871 
872  return 0;
873 
874 fail:
875  nut_read_close(s);
876 
877  return AVERROR_INVALIDDATA;
878 }
879 
880 static int read_sm_data(AVFormatContext *s, AVIOContext *bc, AVPacket *pkt, int is_meta, int64_t maxpos)
881 {
882  int count = ffio_read_varlen(bc);
883  int skip_start = 0;
884  int skip_end = 0;
885  int channels = 0;
886  int64_t channel_layout = 0;
887  int sample_rate = 0;
888  int width = 0;
889  int height = 0;
890  int i, ret;
891 
892  for (i=0; i<count; i++) {
893  uint8_t name[256], str_value[256], type_str[256];
894  int value;
895  if (avio_tell(bc) >= maxpos)
896  return AVERROR_INVALIDDATA;
897  ret = get_str(bc, name, sizeof(name));
898  if (ret < 0) {
899  av_log(s, AV_LOG_ERROR, "get_str failed while reading sm data\n");
900  return ret;
901  }
902  value = get_s(bc);
903 
904  if (value == -1) {
905  ret = get_str(bc, str_value, sizeof(str_value));
906  if (ret < 0) {
907  av_log(s, AV_LOG_ERROR, "get_str failed while reading sm data\n");
908  return ret;
909  }
910  av_log(s, AV_LOG_WARNING, "Unknown string %s / %s\n", name, str_value);
911  } else if (value == -2) {
912  uint8_t *dst = NULL;
913  int64_t v64, value_len;
914 
915  ret = get_str(bc, type_str, sizeof(type_str));
916  if (ret < 0) {
917  av_log(s, AV_LOG_ERROR, "get_str failed while reading sm data\n");
918  return ret;
919  }
920  value_len = ffio_read_varlen(bc);
921  if (value_len < 0 || value_len >= maxpos - avio_tell(bc))
922  return AVERROR_INVALIDDATA;
923  if (!strcmp(name, "Palette")) {
925  } else if (!strcmp(name, "Extradata")) {
927  } else if (sscanf(name, "CodecSpecificSide%"SCNd64"", &v64) == 1) {
929  if(!dst)
930  return AVERROR(ENOMEM);
931  AV_WB64(dst, v64);
932  dst += 8;
933  } else if (!strcmp(name, "ChannelLayout") && value_len == 8) {
934  channel_layout = avio_rl64(bc);
935  continue;
936  } else {
937  av_log(s, AV_LOG_WARNING, "Unknown data %s / %s\n", name, type_str);
938  avio_skip(bc, value_len);
939  continue;
940  }
941  if(!dst)
942  return AVERROR(ENOMEM);
943  avio_read(bc, dst, value_len);
944  if (HAVE_BIGENDIAN && !strcmp(name, "Palette"))
945  for (int j = 0; j + 4 <= value_len; j += 4)
946  AV_WN32(dst + j, AV_RL32(dst + j));
947  } else if (value == -3) {
948  value = get_s(bc);
949  } else if (value == -4) {
950  value = ffio_read_varlen(bc);
951  } else if (value < -4) {
952  get_s(bc);
953  } else {
954  if (!strcmp(name, "SkipStart")) {
955  skip_start = value;
956  } else if (!strcmp(name, "SkipEnd")) {
957  skip_end = value;
958  } else if (!strcmp(name, "Channels")) {
959  channels = value;
960  } else if (!strcmp(name, "SampleRate")) {
961  sample_rate = value;
962  } else if (!strcmp(name, "Width")) {
963  width = value;
964  } else if (!strcmp(name, "Height")) {
965  height = value;
966  } else {
967  av_log(s, AV_LOG_WARNING, "Unknown integer %s\n", name);
968  }
969  }
970  }
971 
972  if (channels || channel_layout || sample_rate || width || height) {
974  if (!dst)
975  return AVERROR(ENOMEM);
976  bytestream_put_le32(&dst,
978  AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT*(!!channel_layout) +
981  );
982  if (channels)
983  bytestream_put_le32(&dst, channels);
984  if (channel_layout)
985  bytestream_put_le64(&dst, channel_layout);
986  if (sample_rate)
987  bytestream_put_le32(&dst, sample_rate);
988  if (width || height){
989  bytestream_put_le32(&dst, width);
990  bytestream_put_le32(&dst, height);
991  }
992  }
993 
994  if (skip_start || skip_end) {
996  if (!dst)
997  return AVERROR(ENOMEM);
998  AV_WL32(dst, skip_start);
999  AV_WL32(dst+4, skip_end);
1000  }
1001 
1002  if (avio_tell(bc) >= maxpos)
1003  return AVERROR_INVALIDDATA;
1004 
1005  return 0;
1006 }
1007 
1008 static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id,
1009  uint8_t *header_idx, int frame_code)
1010 {
1011  AVFormatContext *s = nut->avf;
1012  AVIOContext *bc = s->pb;
1013  StreamContext *stc;
1014  int size, flags, size_mul, pts_delta, i, reserved_count, ret;
1015  uint64_t tmp;
1016 
1017  if (!(nut->flags & NUT_PIPE) &&
1018  avio_tell(bc) > nut->last_syncpoint_pos + nut->max_distance) {
1020  "Last frame must have been damaged %"PRId64" > %"PRId64" + %d\n",
1021  avio_tell(bc), nut->last_syncpoint_pos, nut->max_distance);
1022  return AVERROR_INVALIDDATA;
1023  }
1024 
1025  flags = nut->frame_code[frame_code].flags;
1026  size_mul = nut->frame_code[frame_code].size_mul;
1027  size = nut->frame_code[frame_code].size_lsb;
1028  *stream_id = nut->frame_code[frame_code].stream_id;
1029  pts_delta = nut->frame_code[frame_code].pts_delta;
1030  reserved_count = nut->frame_code[frame_code].reserved_count;
1031  *header_idx = nut->frame_code[frame_code].header_idx;
1032 
1033  if (flags & FLAG_INVALID)
1034  return AVERROR_INVALIDDATA;
1035  if (flags & FLAG_CODED)
1036  flags ^= ffio_read_varlen(bc);
1037  if (flags & FLAG_STREAM_ID) {
1038  GET_V(*stream_id, tmp < s->nb_streams);
1039  }
1040  stc = &nut->stream[*stream_id];
1041  if (flags & FLAG_CODED_PTS) {
1042  int64_t coded_pts = ffio_read_varlen(bc);
1043  // FIXME check last_pts validity?
1044  if (coded_pts < (1LL << stc->msb_pts_shift)) {
1045  *pts = ff_lsb2full(stc, coded_pts);
1046  } else
1047  *pts = coded_pts - (1LL << stc->msb_pts_shift);
1048  } else
1049  *pts = stc->last_pts + pts_delta;
1050  if (flags & FLAG_SIZE_MSB)
1051  size += size_mul * ffio_read_varlen(bc);
1052  if (flags & FLAG_MATCH_TIME)
1053  get_s(bc);
1054  if (flags & FLAG_HEADER_IDX)
1055  *header_idx = ffio_read_varlen(bc);
1056  if (flags & FLAG_RESERVED)
1057  reserved_count = ffio_read_varlen(bc);
1058  for (i = 0; i < reserved_count; i++) {
1059  if (bc->eof_reached) {
1060  av_log(s, AV_LOG_ERROR, "reached EOF while decoding frame header\n");
1061  return AVERROR_INVALIDDATA;
1062  }
1063  ffio_read_varlen(bc);
1064  }
1065 
1066  if (*header_idx >= (unsigned)nut->header_count) {
1067  av_log(s, AV_LOG_ERROR, "header_idx invalid\n");
1068  return AVERROR_INVALIDDATA;
1069  }
1070  if (size > 4096)
1071  *header_idx = 0;
1072  size -= nut->header_len[*header_idx];
1073 
1074  if (flags & FLAG_CHECKSUM) {
1075  avio_rb32(bc); // FIXME check this
1076  } else if (!(nut->flags & NUT_PIPE) &&
1077  size > 2 * nut->max_distance ||
1078  FFABS(stc->last_pts - *pts) > stc->max_pts_distance) {
1079  av_log(s, AV_LOG_ERROR, "frame size > 2max_distance and no checksum\n");
1080  return AVERROR_INVALIDDATA;
1081  }
1082 
1083  stc->last_pts = *pts;
1084  stc->last_flags = flags;
1085 
1086  return size;
1087 fail:
1088  return ret;
1089 }
1090 
1091 static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code)
1092 {
1093  AVFormatContext *s = nut->avf;
1094  AVIOContext *bc = s->pb;
1095  int size, stream_id, discard, ret;
1096  int64_t pts, last_IP_pts;
1097  StreamContext *stc;
1098  uint8_t header_idx;
1099 
1100  size = decode_frame_header(nut, &pts, &stream_id, &header_idx, frame_code);
1101  if (size < 0)
1102  return size;
1103 
1104  stc = &nut->stream[stream_id];
1105 
1106  if (stc->last_flags & FLAG_KEY)
1107  stc->skip_until_key_frame = 0;
1108 
1109  discard = s->streams[stream_id]->discard;
1110  last_IP_pts = s->streams[stream_id]->last_IP_pts;
1111  if ((discard >= AVDISCARD_NONKEY && !(stc->last_flags & FLAG_KEY)) ||
1112  (discard >= AVDISCARD_BIDIR && last_IP_pts != AV_NOPTS_VALUE &&
1113  last_IP_pts > pts) ||
1114  discard >= AVDISCARD_ALL ||
1115  stc->skip_until_key_frame) {
1116  avio_skip(bc, size);
1117  return 1;
1118  }
1119 
1120  ret = av_new_packet(pkt, size + nut->header_len[header_idx]);
1121  if (ret < 0)
1122  return ret;
1123  if (nut->header[header_idx])
1124  memcpy(pkt->data, nut->header[header_idx], nut->header_len[header_idx]);
1125  pkt->pos = avio_tell(bc); // FIXME
1126  if (stc->last_flags & FLAG_SM_DATA) {
1127  int sm_size;
1128  if (read_sm_data(s, bc, pkt, 0, pkt->pos + size) < 0) {
1129  ret = AVERROR_INVALIDDATA;
1130  goto fail;
1131  }
1132  if (read_sm_data(s, bc, pkt, 1, pkt->pos + size) < 0) {
1133  ret = AVERROR_INVALIDDATA;
1134  goto fail;
1135  }
1136  sm_size = avio_tell(bc) - pkt->pos;
1137  size -= sm_size;
1138  pkt->size -= sm_size;
1139  }
1140 
1141  ret = avio_read(bc, pkt->data + nut->header_len[header_idx], size);
1142  if (ret != size) {
1143  if (ret < 0)
1144  goto fail;
1145  }
1146  av_shrink_packet(pkt, nut->header_len[header_idx] + ret);
1147 
1148  pkt->stream_index = stream_id;
1149  if (stc->last_flags & FLAG_KEY)
1151  pkt->pts = pts;
1152 
1153  return 0;
1154 fail:
1156  return ret;
1157 }
1158 
1160 {
1161  NUTContext *nut = s->priv_data;
1162  AVIOContext *bc = s->pb;
1163  int i, frame_code = 0, ret, skip;
1164  int64_t ts, back_ptr;
1165 
1166  for (;;) {
1167  int64_t pos = avio_tell(bc);
1168  uint64_t tmp = nut->next_startcode;
1169  nut->next_startcode = 0;
1170 
1171  if (tmp) {
1172  pos -= 8;
1173  } else {
1174  frame_code = avio_r8(bc);
1175  if (avio_feof(bc))
1176  return AVERROR_EOF;
1177  if (frame_code == 'N') {
1178  tmp = frame_code;
1179  for (i = 1; i < 8; i++)
1180  tmp = (tmp << 8) + avio_r8(bc);
1181  }
1182  }
1183  switch (tmp) {
1184  case MAIN_STARTCODE:
1185  case STREAM_STARTCODE:
1186  case INDEX_STARTCODE:
1187  skip = get_packetheader(nut, bc, 0, tmp);
1188  avio_skip(bc, skip);
1189  break;
1190  case INFO_STARTCODE:
1191  if (decode_info_header(nut) < 0)
1192  goto resync;
1193  break;
1194  case SYNCPOINT_STARTCODE:
1195  if (decode_syncpoint(nut, &ts, &back_ptr) < 0)
1196  goto resync;
1197  frame_code = avio_r8(bc);
1198  case 0:
1199  ret = decode_frame(nut, pkt, frame_code);
1200  if (ret == 0)
1201  return 0;
1202  else if (ret == 1) // OK but discard packet
1203  break;
1204  default:
1205 resync:
1206  av_log(s, AV_LOG_DEBUG, "syncing from %"PRId64"\n", pos);
1208  nut->last_resync_pos = avio_tell(bc);
1209  if (tmp == 0)
1210  return AVERROR_INVALIDDATA;
1211  av_log(s, AV_LOG_DEBUG, "sync\n");
1212  nut->next_startcode = tmp;
1213  }
1214  }
1215 }
1216 
1217 static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index,
1218  int64_t *pos_arg, int64_t pos_limit)
1219 {
1220  NUTContext *nut = s->priv_data;
1221  AVIOContext *bc = s->pb;
1222  int64_t pos, pts, back_ptr;
1223  av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%"PRId64",%"PRId64")\n",
1224  stream_index, *pos_arg, pos_limit);
1225 
1226  pos = *pos_arg;
1227  do {
1229  if (pos < 1) {
1230  av_log(s, AV_LOG_ERROR, "read_timestamp failed.\n");
1231  return AV_NOPTS_VALUE;
1232  }
1233  } while (decode_syncpoint(nut, &pts, &back_ptr) < 0);
1234  *pos_arg = pos - 1;
1235  av_assert0(nut->last_syncpoint_pos == *pos_arg);
1236 
1237  av_log(s, AV_LOG_DEBUG, "return %"PRId64" %"PRId64"\n", pts, back_ptr);
1238  if (stream_index == -2)
1239  return back_ptr;
1240  av_assert0(stream_index == -1);
1241  return pts;
1242 }
1243 
1244 static int read_seek(AVFormatContext *s, int stream_index,
1245  int64_t pts, int flags)
1246 {
1247  NUTContext *nut = s->priv_data;
1248  AVStream *st = s->streams[stream_index];
1249  Syncpoint dummy = { .ts = pts * av_q2d(st->time_base) * AV_TIME_BASE };
1250  Syncpoint nopts_sp = { .ts = AV_NOPTS_VALUE, .back_ptr = AV_NOPTS_VALUE };
1251  Syncpoint *sp, *next_node[2] = { &nopts_sp, &nopts_sp };
1252  int64_t pos, pos2, ts;
1253  int i;
1254 
1255  if (nut->flags & NUT_PIPE) {
1256  return AVERROR(ENOSYS);
1257  }
1258 
1259  if (st->index_entries) {
1261  if (index < 0)
1263  if (index < 0)
1264  return -1;
1265 
1266  pos2 = st->index_entries[index].pos;
1267  ts = st->index_entries[index].timestamp;
1268  } else {
1270  (void **) next_node);
1271  av_log(s, AV_LOG_DEBUG, "%"PRIu64"-%"PRIu64" %"PRId64"-%"PRId64"\n",
1272  next_node[0]->pos, next_node[1]->pos, next_node[0]->ts,
1273  next_node[1]->ts);
1274  pos = ff_gen_search(s, -1, dummy.ts, next_node[0]->pos,
1275  next_node[1]->pos, next_node[1]->pos,
1276  next_node[0]->ts, next_node[1]->ts,
1278  if (pos < 0)
1279  return pos;
1280 
1281  if (!(flags & AVSEEK_FLAG_BACKWARD)) {
1282  dummy.pos = pos + 16;
1283  next_node[1] = &nopts_sp;
1285  (void **) next_node);
1286  pos2 = ff_gen_search(s, -2, dummy.pos, next_node[0]->pos,
1287  next_node[1]->pos, next_node[1]->pos,
1288  next_node[0]->back_ptr, next_node[1]->back_ptr,
1289  flags, &ts, nut_read_timestamp);
1290  if (pos2 >= 0)
1291  pos = pos2;
1292  // FIXME dir but I think it does not matter
1293  }
1294  dummy.pos = pos;
1296  NULL);
1297 
1298  av_assert0(sp);
1299  pos2 = sp->back_ptr - 15;
1300  }
1301  av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos2);
1302  pos = find_startcode(s->pb, SYNCPOINT_STARTCODE, pos2);
1303  avio_seek(s->pb, pos, SEEK_SET);
1304  nut->last_syncpoint_pos = pos;
1305  av_log(s, AV_LOG_DEBUG, "SP: %"PRId64"\n", pos);
1306  if (pos2 > pos || pos2 + 15 < pos)
1307  av_log(s, AV_LOG_ERROR, "no syncpoint at backptr pos\n");
1308  for (i = 0; i < s->nb_streams; i++)
1309  nut->stream[i].skip_until_key_frame = 1;
1310 
1311  nut->last_resync_pos = 0;
1312 
1313  return 0;
1314 }
1315 
1317  .name = "nut",
1318  .long_name = NULL_IF_CONFIG_SMALL("NUT"),
1319  .flags = AVFMT_SEEK_TO_PTS,
1320  .priv_data_size = sizeof(NUTContext),
1321  .read_probe = nut_probe,
1325  .read_seek = read_seek,
1326  .extensions = "nut",
1327  .codec_tag = ff_nut_codec_tags,
1328 };
channels
Definition: aptx.h:33
#define A(x)
Definition: vp56_arith.h:28
uint8_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
Definition: avformat.h:1562
#define AVINDEX_KEYFRAME
Definition: avformat.h:811
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
@ AVFMT_DURATION_FROM_PTS
Duration accurately estimated from PTSes.
Definition: avformat.h:1211
#define AVSTREAM_EVENT_FLAG_METADATA_UPDATED
Definition: avformat.h:999
#define AVFMT_SEEK_TO_PTS
Seeking is based on PTS.
Definition: avformat.h:484
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2415
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:260
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:918
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:349
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:371
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:750
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:344
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:645
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:766
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:797
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:636
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:774
void ffio_init_checksum(AVIOContext *s, unsigned long(*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum)
Definition: aviobuf.c:624
unsigned long ffio_get_checksum(AVIOContext *s)
Definition: aviobuf.c:616
uint64_t ffio_read_varlen(AVIOContext *bc)
Definition: aviobuf.c:926
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:598
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, buffer_size_t size)
Definition: avpacket.c:343
#define AV_RB32
Definition: intreadwrite.h:130
#define AV_RL32
Definition: intreadwrite.h:146
byte swapping routines
#define av_be2ne64(x)
Definition: bswap.h:94
#define flag(name)
Definition: cbs_av1.c:564
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
static struct @322 state
#define fail()
Definition: checkasm.h:133
#define FFMIN(a, b)
Definition: common.h:105
#define FFMAX(a, b)
Definition: common.h:103
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define HAVE_BIGENDIAN
Definition: config.h:202
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
Public dictionary API.
double value
Definition: eval.c:100
sample_rate
static int nb_streams
Definition: ffprobe.c:283
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:555
static int64_t last_pts
@ AV_CODEC_ID_NONE
Definition: codec_id.h:47
@ AVDISCARD_ALL
discard all
Definition: avcodec.h:236
@ AVDISCARD_NONKEY
discard all frames except keyframes
Definition: avcodec.h:235
@ AVDISCARD_BIDIR
discard all bidirectional frames
Definition: avcodec.h:233
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:634
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:114
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
@ AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT
Definition: packet.h:432
@ AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT
Definition: packet.h:433
@ AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE
Definition: packet.h:434
@ AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS
Definition: packet.h:435
@ AV_PKT_DATA_SKIP_SAMPLES
Recommmends skipping the specified number of samples.
Definition: packet.h:156
@ AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL
Data found in BlockAdditional element of matroska container.
Definition: packet.h:191
@ AV_PKT_DATA_PALETTE
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette.
Definition: packet.h:46
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: packet.h:55
@ AV_PKT_DATA_PARAM_CHANGE
An AV_PKT_DATA_PARAM_CHANGE side data packet is laid out as follows:
Definition: packet.h:72
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4508
enum AVCodecID av_codec_get_id(const struct AVCodecTag *const *tags, unsigned int tag)
Get the AVCodecID for the given codec tag tag.
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:2013
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:2130
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
int64_t av_gcd(int64_t a, int64_t b)
Compute the greatest common divisor of two integer operands.
Definition: mathematics.c:37
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:245
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition: avutil.h:203
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:215
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
int index
Definition: gxfenc.c:89
#define B
Definition: huffyuvdsp.h:32
cl_device_type type
int i
Definition: input.c:407
#define AV_WN32(p, v)
Definition: intreadwrite.h:376
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
#define AV_WB64(p, v)
Definition: intreadwrite.h:433
const AVCodecTag ff_codec_movvideo_tags[]
Definition: isom_tags.c:29
static int resync(AVFormatContext *s)
Definition: flvdec.c:975
int ff_find_last_ts(AVFormatContext *s, int stream_index, int64_t *ts, int64_t *pos, int64_t(*read_timestamp)(struct AVFormatContext *, int, int64_t *, int64_t))
Definition: utils.c:2213
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:4944
AVChapter * avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
Add a new chapter.
Definition: utils.c:4638
int64_t ff_gen_search(AVFormatContext *s, int stream_index, int64_t target_ts, int64_t pos_min, int64_t pos_max, int64_t pos_limit, int64_t ts_min, int64_t ts_max, int flags, int64_t *ts_ret, int64_t(*read_timestamp)(struct AVFormatContext *, int, int64_t *, int64_t))
Perform a binary search using read_timestamp().
Definition: utils.c:2251
int ff_get_extradata(AVFormatContext *s, AVCodecParameters *par, AVIOContext *pb, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0 and f...
Definition: utils.c:3332
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:3131
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:59
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
int dummy
Definition: motion.c:64
const AVCodecTag ff_nut_audio_tags[]
Definition: nut.c:221
const AVCodecTag ff_nut_video_tags[]
Definition: nut.c:43
void ff_nut_reset_ts(NUTContext *nut, AVRational time_base, int64_t val)
Definition: nut.c:255
int ff_nut_sp_pos_cmp(const void *a, const void *b)
Definition: nut.c:273
int64_t ff_lsb2full(StreamContext *stream, int64_t lsb)
Definition: nut.c:266
int ff_nut_add_sp(NUTContext *nut, int64_t pos, int64_t back_ptr, int64_t ts)
Definition: nut.c:285
int ff_nut_sp_pts_cmp(const void *a, const void *b)
Definition: nut.c:279
const AVCodecTag ff_nut_data_tags[]
Definition: nut.c:38
const AVCodecTag *const ff_nut_codec_tags[]
Definition: nut.c:250
void ff_nut_free_sp(NUTContext *nut)
Definition: nut.c:316
const AVCodecTag ff_nut_audio_extra_tags[]
Definition: nut.c:211
const Dispositions ff_nut_dispositions[]
Definition: nut.c:324
const AVCodecTag ff_nut_subtitle_tags[]
Definition: nut.c:28
const AVMetadataConv ff_nut_metadata_conv[]
Definition: nut.c:334
#define SYNCPOINT_STARTCODE
Definition: nut.h:31
@ FLAG_CHECKSUM
Definition: nut.h:49
@ FLAG_HEADER_IDX
Definition: nut.h:52
@ FLAG_KEY
Definition: nut.h:44
@ FLAG_SM_DATA
Definition: nut.h:51
@ FLAG_MATCH_TIME
Definition: nut.h:53
@ FLAG_CODED_PTS
Definition: nut.h:46
@ FLAG_SIZE_MSB
Definition: nut.h:48
@ FLAG_INVALID
Definition: nut.h:55
@ FLAG_CODED
Definition: nut.h:54
@ FLAG_RESERVED
Definition: nut.h:50
@ FLAG_STREAM_ID
Definition: nut.h:47
#define NUT_MIN_VERSION
Definition: nut.h:41
#define NUT_BROADCAST
Definition: nut.h:113
#define STREAM_STARTCODE
Definition: nut.h:30
#define NUT_MAX_VERSION
Definition: nut.h:39
#define MAIN_STARTCODE
Definition: nut.h:29
#define NUT_PIPE
Definition: nut.h:114
#define INDEX_STARTCODE
Definition: nut.h:32
#define INFO_STARTCODE
Definition: nut.h:33
static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: nutdec.c:1159
static uint64_t find_any_startcode(AVIOContext *bc, int64_t pos)
Definition: nutdec.c:107
static uint64_t get_fourcc(AVIOContext *bc)
Definition: nutdec.c:73
static int get_str(AVIOContext *bc, char *string, unsigned int maxlen)
Definition: nutdec.c:39
static int decode_main_header(NUTContext *nut)
Definition: nutdec.c:190
static void set_disposition_bits(AVFormatContext *avf, char *value, int stream_id)
Definition: nutdec.c:485
static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index, int64_t *pos_arg, int64_t pos_limit)
Definition: nutdec.c:1217
static int decode_info_header(NUTContext *nut)
Definition: nutdec.c:500
static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr)
Definition: nutdec.c:623
AVInputFormat ff_nut_demuxer
Definition: nutdec.c:1316
static int64_t find_duration(NUTContext *nut, int64_t filesize)
Definition: nutdec.c:667
static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code)
Definition: nutdec.c:1091
static int get_packetheader(NUTContext *nut, AVIOContext *bc, int calculate_checksum, uint64_t startcode)
Definition: nutdec.c:87
static int nut_probe(const AVProbeData *p)
Definition: nutdec.c:150
static int skip_reserved(AVIOContext *bc, int64_t pos)
Definition: nutdec.c:174
static int find_and_decode_index(NUTContext *nut)
Definition: nutdec.c:679
static int read_sm_data(AVFormatContext *s, AVIOContext *bc, AVPacket *pkt, int is_meta, int64_t maxpos)
Definition: nutdec.c:880
static int64_t get_s(AVIOContext *bc)
Definition: nutdec.c:63
static int nut_read_close(AVFormatContext *s)
Definition: nutdec.c:793
static int read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
Definition: nutdec.c:1244
#define GET_V(dst, check)
Definition: nutdec.c:163
static int64_t find_startcode(AVIOContext *bc, uint64_t code, int64_t pos)
Find the given startcode.
Definition: nutdec.c:138
static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id, uint8_t *header_idx, int frame_code)
Definition: nutdec.c:1008
static int decode_stream_header(NUTContext *nut)
Definition: nutdec.c:376
static int nut_read_header(AVFormatContext *s)
Definition: nutdec.c:807
const char * name
Definition: qsvenc.c:46
#define sp
Definition: regdef.h:63
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:33
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:508
internal header for RIFF based (de)muxers do NOT include this in end user applications
const uint8_t * code
Definition: spdifenc.c:413
unsigned int pos
Definition: spdifenc.c:412
AVDictionary * metadata
Definition: avformat.h:1193
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:78
int channels
Audio only.
Definition: codec_par.h:166
int width
Video only.
Definition: codec_par.h:126
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
int video_delay
Video only.
Definition: codec_par.h:155
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int sample_rate
Audio only.
Definition: codec_par.h:170
Format I/O context.
Definition: avformat.h:1232
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1288
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1300
Bytestream IO Context.
Definition: avio.h:161
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:260
int eof_reached
true if was unable to read due to error or eof
Definition: avio.h:239
int64_t pos
Definition: avformat.h:804
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition: avformat.h:805
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
This structure stores compressed data.
Definition: packet.h:346
int stream_index
Definition: packet.h:371
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
int size
Definition: packet.h:370
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
uint8_t * data
Definition: packet.h:369
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:389
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:444
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int num
Numerator.
Definition: rational.h:59
int den
Denominator.
Definition: rational.h:60
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:935
AVDictionary * metadata
Definition: avformat.h:937
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:902
int event_flags
Flags indicating events happening on the stream, a combination of AVSTREAM_EVENT_FLAG_*.
Definition: avformat.h:992
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:1090
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:1015
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:926
int flag
Definition: nut.h:130
uint8_t header_idx
Definition: nut.h:72
uint16_t flags
Definition: nut.h:66
int stream_id
Definition: nut.h:67
int16_t pts_delta
Definition: nut.h:70
uint16_t size_mul
Definition: nut.h:68
uint16_t size_lsb
Definition: nut.h:69
uint8_t reserved_count
Definition: nut.h:71
Definition: nut.h:91
const uint8_t * header[128]
Definition: nut.h:98
int flags
Definition: nut.h:115
AVRational * time_base
Definition: nut.h:107
StreamContext * stream
Definition: nut.h:100
int header_count
Definition: nut.h:106
AVFormatContext * avf
Definition: nut.h:93
struct AVTreeNode * syncpoints
Definition: nut.h:108
int minor_version
Definition: nut.h:117
uint64_t next_startcode
Definition: nut.h:99
int version
Definition: nut.h:116
FrameCode frame_code[256]
Definition: nut.h:96
int64_t last_resync_pos
Definition: nut.h:105
unsigned int max_distance
Definition: nut.h:102
uint8_t header_len[128]
Definition: nut.h:97
int64_t last_syncpoint_pos
Definition: nut.h:104
unsigned int time_base_count
Definition: nut.h:103
int time_base_id
Definition: nut.h:79
AVRational time_base
Definition: signature.h:103
int max_pts_distance
Definition: nut.h:82
int skip_until_key_frame
Definition: nut.h:77
int decode_delay
Definition: nut.h:83
int msb_pts_shift
Definition: nut.h:81
int last_flags
Definition: nut.h:76
int64_t last_pts
Definition: nut.h:78
Definition: nut.h:58
int64_t ts
Definition: nut.h:62
#define av_free(p)
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_malloc(s)
#define av_log(a,...)
static uint8_t tmp[11]
Definition: aes_ctr.c:27
int64_t duration
Definition: movenc.c:64
AVPacket * pkt
Definition: movenc.c:59
#define height
#define width
static int64_t pts
void * av_tree_find(const AVTreeNode *t, void *key, int(*cmp)(const void *key, const void *b), void *next[2])
Definition: tree.c:39
A tree container.
int size
int len