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