FFmpeg  4.3.7
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(const 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, length;
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  length = get_packetheader(nut, bc, 1, MAIN_STARTCODE);
202  if (length == (uint64_t)-1)
203  return AVERROR_INVALIDDATA;
204  end = length + avio_tell(bc);
205 
206  nut->version = ffio_read_varlen(bc);
207  if (nut->version < NUT_MIN_VERSION ||
208  nut->version > NUT_MAX_VERSION) {
209  av_log(s, AV_LOG_ERROR, "Version %d not supported.\n",
210  nut->version);
211  return AVERROR(ENOSYS);
212  }
213  if (nut->version > 3)
214  nut->minor_version = ffio_read_varlen(bc);
215 
216  GET_V(stream_count, tmp > 0 && tmp <= NUT_MAX_STREAMS);
217 
218  nut->max_distance = ffio_read_varlen(bc);
219  if (nut->max_distance > 65536) {
220  av_log(s, AV_LOG_DEBUG, "max_distance %d\n", nut->max_distance);
221  nut->max_distance = 65536;
222  }
223 
224  GET_V(nut->time_base_count, tmp > 0 && tmp < INT_MAX / sizeof(AVRational) && tmp < length/2);
225  nut->time_base = av_malloc_array(nut->time_base_count, sizeof(AVRational));
226  if (!nut->time_base)
227  return AVERROR(ENOMEM);
228 
229  for (i = 0; i < nut->time_base_count; i++) {
230  GET_V(nut->time_base[i].num, tmp > 0 && tmp < (1ULL << 31));
231  GET_V(nut->time_base[i].den, tmp > 0 && tmp < (1ULL << 31));
232  if (av_gcd(nut->time_base[i].num, nut->time_base[i].den) != 1) {
233  av_log(s, AV_LOG_ERROR, "invalid time base %d/%d\n",
234  nut->time_base[i].num,
235  nut->time_base[i].den);
236  ret = AVERROR_INVALIDDATA;
237  goto fail;
238  }
239  }
240  tmp_pts = 0;
241  tmp_mul = 1;
242  tmp_stream = 0;
243  tmp_head_idx = 0;
244  for (i = 0; i < 256;) {
245  int tmp_flags = ffio_read_varlen(bc);
246  int tmp_fields = ffio_read_varlen(bc);
247  if (tmp_fields < 0) {
248  av_log(s, AV_LOG_ERROR, "fields %d is invalid\n", tmp_fields);
249  ret = AVERROR_INVALIDDATA;
250  goto fail;
251  }
252 
253  if (tmp_fields > 0)
254  tmp_pts = get_s(bc);
255  if (tmp_fields > 1)
256  tmp_mul = ffio_read_varlen(bc);
257  if (tmp_fields > 2)
258  tmp_stream = ffio_read_varlen(bc);
259  if (tmp_fields > 3)
260  tmp_size = ffio_read_varlen(bc);
261  else
262  tmp_size = 0;
263  if (tmp_fields > 4)
264  tmp_res = ffio_read_varlen(bc);
265  else
266  tmp_res = 0;
267  if (tmp_fields > 5)
268  count = ffio_read_varlen(bc);
269  else
270  count = tmp_mul - (unsigned)tmp_size;
271  if (tmp_fields > 6)
272  get_s(bc);
273  if (tmp_fields > 7)
274  tmp_head_idx = ffio_read_varlen(bc);
275 
276  while (tmp_fields-- > 8) {
277  if (bc->eof_reached) {
278  av_log(s, AV_LOG_ERROR, "reached EOF while decoding main header\n");
279  ret = AVERROR_INVALIDDATA;
280  goto fail;
281  }
282  ffio_read_varlen(bc);
283  }
284 
285  if (count <= 0 || count > 256 - (i <= 'N') - i) {
286  av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i);
287  ret = AVERROR_INVALIDDATA;
288  goto fail;
289  }
290  if (tmp_stream >= stream_count) {
291  av_log(s, AV_LOG_ERROR, "illegal stream number %d >= %d\n",
292  tmp_stream, stream_count);
293  ret = AVERROR_INVALIDDATA;
294  goto fail;
295  }
296  if (tmp_size < 0 || tmp_size > INT_MAX - count) {
297  av_log(s, AV_LOG_ERROR, "illegal size\n");
298  ret = AVERROR_INVALIDDATA;
299  goto fail;
300  }
301 
302  for (j = 0; j < count; j++, i++) {
303  if (i == 'N') {
304  nut->frame_code[i].flags = FLAG_INVALID;
305  j--;
306  continue;
307  }
308  nut->frame_code[i].flags = tmp_flags;
309  nut->frame_code[i].pts_delta = tmp_pts;
310  nut->frame_code[i].stream_id = tmp_stream;
311  nut->frame_code[i].size_mul = tmp_mul;
312  nut->frame_code[i].size_lsb = tmp_size + j;
313  nut->frame_code[i].reserved_count = tmp_res;
314  nut->frame_code[i].header_idx = tmp_head_idx;
315  }
316  }
317  av_assert0(nut->frame_code['N'].flags == FLAG_INVALID);
318 
319  if (end > avio_tell(bc) + 4) {
320  int rem = 1024;
321  GET_V(nut->header_count, tmp < 128U);
322  nut->header_count++;
323  for (i = 1; i < nut->header_count; i++) {
324  uint8_t *hdr;
325  GET_V(nut->header_len[i], tmp > 0 && tmp < 256);
326  if (rem < nut->header_len[i]) {
327  av_log(s, AV_LOG_ERROR,
328  "invalid elision header %d : %d > %d\n",
329  i, nut->header_len[i], rem);
330  ret = AVERROR_INVALIDDATA;
331  goto fail;
332  }
333  rem -= nut->header_len[i];
334  hdr = av_malloc(nut->header_len[i]);
335  if (!hdr) {
336  ret = AVERROR(ENOMEM);
337  goto fail;
338  }
339  avio_read(bc, hdr, nut->header_len[i]);
340  nut->header[i] = hdr;
341  }
342  av_assert0(nut->header_len[0] == 0);
343  }
344 
345  // flags had been effectively introduced in version 4
346  if (nut->version > 3 && end > avio_tell(bc) + 4) {
347  nut->flags = ffio_read_varlen(bc);
348  }
349 
350  if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
351  av_log(s, AV_LOG_ERROR, "main header checksum mismatch\n");
352  ret = AVERROR_INVALIDDATA;
353  goto fail;
354  }
355 
356  nut->stream = av_calloc(stream_count, sizeof(StreamContext));
357  if (!nut->stream) {
358  ret = AVERROR(ENOMEM);
359  goto fail;
360  }
361  for (i = 0; i < stream_count; i++) {
362  if (!avformat_new_stream(s, NULL)) {
363  ret = AVERROR(ENOMEM);
364  goto fail;
365  }
366  }
367 
368  return 0;
369 fail:
370  av_freep(&nut->time_base);
371  for (i = 1; i < nut->header_count; i++) {
372  av_freep(&nut->header[i]);
373  }
374  nut->header_count = 0;
375  return ret;
376 }
377 
379 {
380  AVFormatContext *s = nut->avf;
381  AVIOContext *bc = s->pb;
382  StreamContext *stc;
383  int class, stream_id, ret;
384  uint64_t tmp, end;
385  AVStream *st = NULL;
386 
387  end = get_packetheader(nut, bc, 1, STREAM_STARTCODE);
388  end += avio_tell(bc);
389 
390  GET_V(stream_id, tmp < s->nb_streams && !nut->stream[tmp].time_base);
391  stc = &nut->stream[stream_id];
392  st = s->streams[stream_id];
393  if (!st)
394  return AVERROR(ENOMEM);
395 
396  class = ffio_read_varlen(bc);
397  tmp = get_fourcc(bc);
398  st->codecpar->codec_tag = tmp;
399  switch (class) {
400  case 0:
402  st->codecpar->codec_id = av_codec_get_id((const AVCodecTag * const []) {
406  0
407  },
408  tmp);
409  break;
410  case 1:
412  st->codecpar->codec_id = av_codec_get_id((const AVCodecTag * const []) {
416  0
417  },
418  tmp);
419  break;
420  case 2:
423  break;
424  case 3:
427  break;
428  default:
429  av_log(s, AV_LOG_ERROR, "unknown stream class (%d)\n", class);
430  return AVERROR(ENOSYS);
431  }
432  if (class < 3 && st->codecpar->codec_id == AV_CODEC_ID_NONE)
433  av_log(s, AV_LOG_ERROR,
434  "Unknown codec tag '0x%04x' for stream number %d\n",
435  (unsigned int) tmp, stream_id);
436 
437  GET_V(stc->time_base_id, tmp < nut->time_base_count);
438  GET_V(stc->msb_pts_shift, tmp < 16);
440  GET_V(stc->decode_delay, tmp < 1000); // sanity limit, raise this if Moore's law is true
441  st->codecpar->video_delay = stc->decode_delay;
442  ffio_read_varlen(bc); // stream flags
443 
444  GET_V(st->codecpar->extradata_size, tmp < (1 << 30));
445  if (st->codecpar->extradata_size) {
446  ret = ff_get_extradata(s, st->codecpar, bc,
447  st->codecpar->extradata_size);
448  if (ret < 0)
449  return ret;
450  }
451 
452  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
453  GET_V(st->codecpar->width, tmp > 0);
454  GET_V(st->codecpar->height, tmp > 0);
457  if ((!st->sample_aspect_ratio.num) != (!st->sample_aspect_ratio.den)) {
458  av_log(s, AV_LOG_ERROR, "invalid aspect ratio %d/%d\n",
460  ret = AVERROR_INVALIDDATA;
461  goto fail;
462  }
463  ffio_read_varlen(bc); /* csp type */
464  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
465  GET_V(st->codecpar->sample_rate, tmp > 0);
466  ffio_read_varlen(bc); // samplerate_den
467  GET_V(st->codecpar->channels, tmp > 0);
468  }
469  if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
470  av_log(s, AV_LOG_ERROR,
471  "stream header %d checksum mismatch\n", stream_id);
472  ret = AVERROR_INVALIDDATA;
473  goto fail;
474  }
475  stc->time_base = &nut->time_base[stc->time_base_id];
476  avpriv_set_pts_info(s->streams[stream_id], 63, stc->time_base->num,
477  stc->time_base->den);
478  return 0;
479 fail:
480  if (st && st->codecpar) {
481  av_freep(&st->codecpar->extradata);
482  st->codecpar->extradata_size = 0;
483  }
484  return ret;
485 }
486 
488  int stream_id)
489 {
490  int flag = 0, i;
491 
492  for (i = 0; ff_nut_dispositions[i].flag; ++i)
493  if (!strcmp(ff_nut_dispositions[i].str, value))
494  flag = ff_nut_dispositions[i].flag;
495  if (!flag)
496  av_log(avf, AV_LOG_INFO, "unknown disposition type '%s'\n", value);
497  for (i = 0; i < avf->nb_streams; ++i)
498  if (stream_id == i || stream_id == -1)
499  avf->streams[i]->disposition |= flag;
500 }
501 
503 {
504  AVFormatContext *s = nut->avf;
505  AVIOContext *bc = s->pb;
506  uint64_t tmp, chapter_start, chapter_len;
507  unsigned int stream_id_plus1, count;
508  int chapter_id, i, ret = 0;
509  int64_t value, end;
510  char name[256], str_value[1024], type_str[256];
511  const char *type;
512  int *event_flags = NULL;
513  AVChapter *chapter = NULL;
514  AVStream *st = NULL;
515  AVDictionary **metadata = NULL;
516  int metadata_flag = 0;
517 
518  end = get_packetheader(nut, bc, 1, INFO_STARTCODE);
519  end += avio_tell(bc);
520 
521  GET_V(stream_id_plus1, tmp <= s->nb_streams);
522  chapter_id = get_s(bc);
523  chapter_start = ffio_read_varlen(bc);
524  chapter_len = ffio_read_varlen(bc);
525  count = ffio_read_varlen(bc);
526 
527  if (chapter_id && !stream_id_plus1) {
528  int64_t start = chapter_start / nut->time_base_count;
529  chapter = avpriv_new_chapter(s, chapter_id,
530  nut->time_base[chapter_start %
531  nut->time_base_count],
532  start, start + chapter_len, NULL);
533  if (!chapter) {
534  av_log(s, AV_LOG_ERROR, "Could not create chapter.\n");
535  return AVERROR(ENOMEM);
536  }
537  metadata = &chapter->metadata;
538  } else if (stream_id_plus1) {
539  st = s->streams[stream_id_plus1 - 1];
540  metadata = &st->metadata;
541  event_flags = &st->event_flags;
542  metadata_flag = AVSTREAM_EVENT_FLAG_METADATA_UPDATED;
543  } else {
544  metadata = &s->metadata;
545  event_flags = &s->event_flags;
546  metadata_flag = AVFMT_EVENT_FLAG_METADATA_UPDATED;
547  }
548 
549  for (i = 0; i < count; i++) {
550  ret = get_str(bc, name, sizeof(name));
551  if (ret < 0) {
552  av_log(s, AV_LOG_ERROR, "get_str failed while decoding info header\n");
553  return ret;
554  }
555  value = get_s(bc);
556  str_value[0] = 0;
557 
558  if (value == -1) {
559  type = "UTF-8";
560  ret = get_str(bc, str_value, sizeof(str_value));
561  } else if (value == -2) {
562  ret = get_str(bc, type_str, sizeof(type_str));
563  if (ret < 0) {
564  av_log(s, AV_LOG_ERROR, "get_str failed while decoding info header\n");
565  return ret;
566  }
567  type = type_str;
568  ret = get_str(bc, str_value, sizeof(str_value));
569  } else if (value == -3) {
570  type = "s";
571  value = get_s(bc);
572  } else if (value == -4) {
573  type = "t";
574  value = ffio_read_varlen(bc);
575  } else if (value < -4) {
576  type = "r";
577  get_s(bc);
578  } else {
579  type = "v";
580  }
581 
582  if (ret < 0) {
583  av_log(s, AV_LOG_ERROR, "get_str failed while decoding info header\n");
584  return ret;
585  }
586 
587  if (stream_id_plus1 > s->nb_streams) {
589  "invalid stream id %d for info packet\n",
590  stream_id_plus1);
591  continue;
592  }
593 
594  if (!strcmp(type, "UTF-8")) {
595  if (chapter_id == 0 && !strcmp(name, "Disposition")) {
596  set_disposition_bits(s, str_value, stream_id_plus1 - 1);
597  continue;
598  }
599 
600  if (stream_id_plus1 && !strcmp(name, "r_frame_rate")) {
601  sscanf(str_value, "%d/%d", &st->r_frame_rate.num, &st->r_frame_rate.den);
602  if (st->r_frame_rate.num >= 1000LL*st->r_frame_rate.den ||
603  st->r_frame_rate.num < 0 || st->r_frame_rate.den < 0)
604  st->r_frame_rate.num = st->r_frame_rate.den = 0;
605  continue;
606  }
607 
608  if (metadata && av_strcasecmp(name, "Uses") &&
609  av_strcasecmp(name, "Depends") && av_strcasecmp(name, "Replaces")) {
610  if (event_flags)
611  *event_flags |= metadata_flag;
612  av_dict_set(metadata, name, str_value, 0);
613  }
614  }
615  }
616 
617  if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
618  av_log(s, AV_LOG_ERROR, "info header checksum mismatch\n");
619  return AVERROR_INVALIDDATA;
620  }
621 fail:
622  return FFMIN(ret, 0);
623 }
624 
625 static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr)
626 {
627  AVFormatContext *s = nut->avf;
628  AVIOContext *bc = s->pb;
629  int64_t end;
630  uint64_t tmp;
631  int ret;
632 
633  nut->last_syncpoint_pos = avio_tell(bc) - 8;
634 
635  end = get_packetheader(nut, bc, 1, SYNCPOINT_STARTCODE);
636  end += avio_tell(bc);
637 
638  tmp = ffio_read_varlen(bc);
639  *back_ptr = nut->last_syncpoint_pos - 16 * ffio_read_varlen(bc);
640  if (*back_ptr < 0)
641  return AVERROR_INVALIDDATA;
642 
643  ff_nut_reset_ts(nut, nut->time_base[tmp % nut->time_base_count],
644  tmp / nut->time_base_count);
645 
646  if (nut->flags & NUT_BROADCAST) {
647  tmp = ffio_read_varlen(bc);
648  av_log(s, AV_LOG_VERBOSE, "Syncpoint wallclock %"PRId64"\n",
649  av_rescale_q(tmp / nut->time_base_count,
650  nut->time_base[tmp % nut->time_base_count],
651  AV_TIME_BASE_Q));
652  }
653 
654  if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
655  av_log(s, AV_LOG_ERROR, "sync point checksum mismatch\n");
656  return AVERROR_INVALIDDATA;
657  }
658 
659  *ts = tmp / nut->time_base_count *
660  av_q2d(nut->time_base[tmp % nut->time_base_count]) * AV_TIME_BASE;
661 
662  if ((ret = ff_nut_add_sp(nut, nut->last_syncpoint_pos, *back_ptr, *ts)) < 0)
663  return ret;
664 
665  return 0;
666 }
667 
668 //FIXME calculate exactly, this is just a good approximation.
669 static int64_t find_duration(NUTContext *nut, int64_t filesize)
670 {
671  AVFormatContext *s = nut->avf;
672  int64_t duration = 0;
673 
674  ff_find_last_ts(s, -1, &duration, NULL, nut_read_timestamp);
675 
676  if(duration > 0)
678  return duration;
679 }
680 
682 {
683  AVFormatContext *s = nut->avf;
684  AVIOContext *bc = s->pb;
685  uint64_t tmp, end;
686  int i, j, syncpoint_count;
687  int64_t filesize = avio_size(bc);
688  int64_t *syncpoints = NULL;
689  uint64_t max_pts;
690  int8_t *has_keyframe = NULL;
691  int ret = AVERROR_INVALIDDATA;
692 
693  if(filesize <= 0)
694  return -1;
695 
696  avio_seek(bc, filesize - 12, SEEK_SET);
697  avio_seek(bc, filesize - avio_rb64(bc), SEEK_SET);
698  if (avio_rb64(bc) != INDEX_STARTCODE) {
699  av_log(s, AV_LOG_WARNING, "no index at the end\n");
700 
701  if(s->duration<=0)
702  s->duration = find_duration(nut, filesize);
703  return ret;
704  }
705 
706  end = get_packetheader(nut, bc, 1, INDEX_STARTCODE);
707  end += avio_tell(bc);
708 
709  max_pts = ffio_read_varlen(bc);
710  s->duration = av_rescale_q(max_pts / nut->time_base_count,
711  nut->time_base[max_pts % nut->time_base_count],
714 
715  GET_V(syncpoint_count, tmp < INT_MAX / 8 && tmp > 0);
716  syncpoints = av_malloc_array(syncpoint_count, sizeof(int64_t));
717  has_keyframe = av_malloc_array(syncpoint_count + 1, sizeof(int8_t));
718  if (!syncpoints || !has_keyframe) {
719  ret = AVERROR(ENOMEM);
720  goto fail;
721  }
722  for (i = 0; i < syncpoint_count; i++) {
723  syncpoints[i] = ffio_read_varlen(bc);
724  if (syncpoints[i] <= 0)
725  goto fail;
726  if (i)
727  syncpoints[i] += syncpoints[i - 1];
728  }
729 
730  for (i = 0; i < s->nb_streams; i++) {
731  int64_t last_pts = -1;
732  for (j = 0; j < syncpoint_count;) {
733  uint64_t x = ffio_read_varlen(bc);
734  int type = x & 1;
735  int n = j;
736  x >>= 1;
737  if (type) {
738  int flag = x & 1;
739  x >>= 1;
740  if (n + x >= syncpoint_count + 1) {
741  av_log(s, AV_LOG_ERROR, "index overflow A %d + %"PRIu64" >= %d\n", n, x, syncpoint_count + 1);
742  goto fail;
743  }
744  while (x--)
745  has_keyframe[n++] = flag;
746  has_keyframe[n++] = !flag;
747  } else {
748  if (x <= 1) {
749  av_log(s, AV_LOG_ERROR, "index: x %"PRIu64" is invalid\n", x);
750  goto fail;
751  }
752  while (x != 1) {
753  if (n >= syncpoint_count + 1) {
754  av_log(s, AV_LOG_ERROR, "index overflow B\n");
755  goto fail;
756  }
757  has_keyframe[n++] = x & 1;
758  x >>= 1;
759  }
760  }
761  if (has_keyframe[0]) {
762  av_log(s, AV_LOG_ERROR, "keyframe before first syncpoint in index\n");
763  goto fail;
764  }
765  av_assert0(n <= syncpoint_count + 1);
766  for (; j < n && j < syncpoint_count; j++) {
767  if (has_keyframe[j]) {
768  uint64_t B, A = ffio_read_varlen(bc);
769  if (!A) {
770  A = ffio_read_varlen(bc);
771  B = ffio_read_varlen(bc);
772  // eor_pts[j][i] = last_pts + A + B
773  } else
774  B = 0;
775  av_add_index_entry(s->streams[i], 16 * syncpoints[j - 1],
776  last_pts + A, 0, 0, AVINDEX_KEYFRAME);
777  last_pts += A + B;
778  }
779  }
780  }
781  }
782 
783  if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
784  av_log(s, AV_LOG_ERROR, "index checksum mismatch\n");
785  goto fail;
786  }
787  ret = 0;
788 
789 fail:
790  av_free(syncpoints);
791  av_free(has_keyframe);
792  return ret;
793 }
794 
796 {
797  NUTContext *nut = s->priv_data;
798  int i;
799 
800  av_freep(&nut->time_base);
801  av_freep(&nut->stream);
802  ff_nut_free_sp(nut);
803  for (i = 1; i < nut->header_count; i++)
804  av_freep(&nut->header[i]);
805 
806  return 0;
807 }
808 
810 {
811  NUTContext *nut = s->priv_data;
812  AVIOContext *bc = s->pb;
813  int64_t pos;
814  int initialized_stream_count, ret;
815 
816  nut->avf = s;
817 
818  /* main header */
819  pos = 0;
820  ret = 0;
821  do {
822  if (ret == AVERROR(ENOMEM))
823  return ret;
824 
825  pos = find_startcode(bc, MAIN_STARTCODE, pos) + 1;
826  if (pos < 0 + 1) {
827  av_log(s, AV_LOG_ERROR, "No main startcode found.\n");
828  goto fail;
829  }
830  } while ((ret = decode_main_header(nut)) < 0);
831 
832  /* stream headers */
833  pos = 0;
834  for (initialized_stream_count = 0; initialized_stream_count < s->nb_streams;) {
835  pos = find_startcode(bc, STREAM_STARTCODE, pos) + 1;
836  if (pos < 0 + 1) {
837  av_log(s, AV_LOG_ERROR, "Not all stream headers found.\n");
838  goto fail;
839  }
840  if (decode_stream_header(nut) >= 0)
841  initialized_stream_count++;
842  }
843 
844  /* info headers */
845  pos = 0;
846  for (;;) {
847  uint64_t startcode = find_any_startcode(bc, pos);
848  pos = avio_tell(bc);
849 
850  if (startcode == 0) {
851  av_log(s, AV_LOG_ERROR, "EOF before video frames\n");
852  goto fail;
853  } else if (startcode == SYNCPOINT_STARTCODE) {
854  nut->next_startcode = startcode;
855  break;
856  } else if (startcode != INFO_STARTCODE) {
857  continue;
858  }
859 
860  decode_info_header(nut);
861  }
862 
863  s->internal->data_offset = pos - 8;
864 
865  if (bc->seekable & AVIO_SEEKABLE_NORMAL) {
866  int64_t orig_pos = avio_tell(bc);
868  avio_seek(bc, orig_pos, SEEK_SET);
869  }
871 
873 
874  return 0;
875 
876 fail:
877  nut_read_close(s);
878 
879  return AVERROR_INVALIDDATA;
880 }
881 
882 static int read_sm_data(AVFormatContext *s, AVIOContext *bc, AVPacket *pkt, int is_meta, int64_t maxpos)
883 {
884  int count = ffio_read_varlen(bc);
885  int skip_start = 0;
886  int skip_end = 0;
887  int channels = 0;
888  int64_t channel_layout = 0;
889  int sample_rate = 0;
890  int width = 0;
891  int height = 0;
892  int i, ret;
893 
894  for (i=0; i<count; i++) {
895  uint8_t name[256], str_value[256], type_str[256];
896  int value;
897  if (avio_tell(bc) >= maxpos)
898  return AVERROR_INVALIDDATA;
899  ret = get_str(bc, name, sizeof(name));
900  if (ret < 0) {
901  av_log(s, AV_LOG_ERROR, "get_str failed while reading sm data\n");
902  return ret;
903  }
904  value = get_s(bc);
905 
906  if (value == -1) {
907  ret = get_str(bc, str_value, sizeof(str_value));
908  if (ret < 0) {
909  av_log(s, AV_LOG_ERROR, "get_str failed while reading sm data\n");
910  return ret;
911  }
912  av_log(s, AV_LOG_WARNING, "Unknown string %s / %s\n", name, str_value);
913  } else if (value == -2) {
914  uint8_t *dst = NULL;
915  int64_t v64, value_len;
916 
917  ret = get_str(bc, type_str, sizeof(type_str));
918  if (ret < 0) {
919  av_log(s, AV_LOG_ERROR, "get_str failed while reading sm data\n");
920  return ret;
921  }
922  value_len = ffio_read_varlen(bc);
923  if (value_len < 0 || value_len >= maxpos - avio_tell(bc))
924  return AVERROR_INVALIDDATA;
925  if (!strcmp(name, "Palette")) {
926  dst = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, value_len);
927  } else if (!strcmp(name, "Extradata")) {
928  dst = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, value_len);
929  } else if (sscanf(name, "CodecSpecificSide%"SCNd64"", &v64) == 1) {
931  if(!dst)
932  return AVERROR(ENOMEM);
933  AV_WB64(dst, v64);
934  dst += 8;
935  } else if (!strcmp(name, "ChannelLayout") && value_len == 8) {
936  channel_layout = avio_rl64(bc);
937  continue;
938  } else {
939  av_log(s, AV_LOG_WARNING, "Unknown data %s / %s\n", name, type_str);
940  avio_skip(bc, value_len);
941  continue;
942  }
943  if(!dst)
944  return AVERROR(ENOMEM);
945  avio_read(bc, dst, value_len);
946  } else if (value == -3) {
947  value = get_s(bc);
948  } else if (value == -4) {
949  value = ffio_read_varlen(bc);
950  } else if (value < -4) {
951  get_s(bc);
952  } else {
953  if (!strcmp(name, "SkipStart")) {
954  skip_start = value;
955  } else if (!strcmp(name, "SkipEnd")) {
956  skip_end = value;
957  } else if (!strcmp(name, "Channels")) {
958  channels = value;
959  } else if (!strcmp(name, "SampleRate")) {
960  sample_rate = value;
961  } else if (!strcmp(name, "Width")) {
962  width = value;
963  } else if (!strcmp(name, "Height")) {
964  height = value;
965  } else {
966  av_log(s, AV_LOG_WARNING, "Unknown integer %s\n", name);
967  }
968  }
969  }
970 
971  if (channels || channel_layout || sample_rate || width || height) {
973  if (!dst)
974  return AVERROR(ENOMEM);
975  bytestream_put_le32(&dst,
977  AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT*(!!channel_layout) +
978  AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE*(!!sample_rate) +
979  AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS*(!!(width|height))
980  );
981  if (channels)
982  bytestream_put_le32(&dst, channels);
983  if (channel_layout)
984  bytestream_put_le64(&dst, channel_layout);
985  if (sample_rate)
986  bytestream_put_le32(&dst, sample_rate);
987  if (width || height){
988  bytestream_put_le32(&dst, width);
989  bytestream_put_le32(&dst, height);
990  }
991  }
992 
993  if (skip_start || skip_end) {
995  if (!dst)
996  return AVERROR(ENOMEM);
997  AV_WL32(dst, skip_start);
998  AV_WL32(dst+4, skip_end);
999  }
1000 
1001  if (avio_tell(bc) >= maxpos)
1002  return AVERROR_INVALIDDATA;
1003 
1004  return 0;
1005 }
1006 
1007 static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id,
1008  uint8_t *header_idx, int frame_code)
1009 {
1010  AVFormatContext *s = nut->avf;
1011  AVIOContext *bc = s->pb;
1012  StreamContext *stc;
1013  int size, flags, size_mul, pts_delta, i, reserved_count, ret;
1014  uint64_t tmp;
1015 
1016  if (!(nut->flags & NUT_PIPE) &&
1017  avio_tell(bc) > nut->last_syncpoint_pos + nut->max_distance) {
1018  av_log(s, AV_LOG_ERROR,
1019  "Last frame must have been damaged %"PRId64" > %"PRId64" + %d\n",
1020  avio_tell(bc), nut->last_syncpoint_pos, nut->max_distance);
1021  return AVERROR_INVALIDDATA;
1022  }
1023 
1024  flags = nut->frame_code[frame_code].flags;
1025  size_mul = nut->frame_code[frame_code].size_mul;
1026  size = nut->frame_code[frame_code].size_lsb;
1027  *stream_id = nut->frame_code[frame_code].stream_id;
1028  pts_delta = nut->frame_code[frame_code].pts_delta;
1029  reserved_count = nut->frame_code[frame_code].reserved_count;
1030  *header_idx = nut->frame_code[frame_code].header_idx;
1031 
1032  if (flags & FLAG_INVALID)
1033  return AVERROR_INVALIDDATA;
1034  if (flags & FLAG_CODED)
1035  flags ^= ffio_read_varlen(bc);
1036  if (flags & FLAG_STREAM_ID) {
1037  GET_V(*stream_id, tmp < s->nb_streams);
1038  }
1039  stc = &nut->stream[*stream_id];
1040  if (flags & FLAG_CODED_PTS) {
1041  int64_t coded_pts = ffio_read_varlen(bc);
1042  // FIXME check last_pts validity?
1043  if (coded_pts < (1LL << stc->msb_pts_shift)) {
1044  *pts = ff_lsb2full(stc, coded_pts);
1045  } else
1046  *pts = coded_pts - (1LL << stc->msb_pts_shift);
1047  } else
1048  *pts = stc->last_pts + pts_delta;
1049  if (flags & FLAG_SIZE_MSB)
1050  size += size_mul * ffio_read_varlen(bc);
1051  if (flags & FLAG_MATCH_TIME)
1052  get_s(bc);
1053  if (flags & FLAG_HEADER_IDX)
1054  *header_idx = ffio_read_varlen(bc);
1055  if (flags & FLAG_RESERVED)
1056  reserved_count = ffio_read_varlen(bc);
1057  for (i = 0; i < reserved_count; i++) {
1058  if (bc->eof_reached) {
1059  av_log(s, AV_LOG_ERROR, "reached EOF while decoding frame header\n");
1060  return AVERROR_INVALIDDATA;
1061  }
1062  ffio_read_varlen(bc);
1063  }
1064 
1065  if (*header_idx >= (unsigned)nut->header_count) {
1066  av_log(s, AV_LOG_ERROR, "header_idx invalid\n");
1067  return AVERROR_INVALIDDATA;
1068  }
1069  if (size > 4096)
1070  *header_idx = 0;
1071  size -= nut->header_len[*header_idx];
1072 
1073  if (flags & FLAG_CHECKSUM) {
1074  avio_rb32(bc); // FIXME check this
1075  } else if (!(nut->flags & NUT_PIPE) &&
1076  size > 2 * nut->max_distance ||
1077  FFABS(stc->last_pts - *pts) > stc->max_pts_distance) {
1078  av_log(s, AV_LOG_ERROR, "frame size > 2max_distance and no checksum\n");
1079  return AVERROR_INVALIDDATA;
1080  }
1081 
1082  stc->last_pts = *pts;
1083  stc->last_flags = flags;
1084 
1085  return size;
1086 fail:
1087  return ret;
1088 }
1089 
1090 static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code)
1091 {
1092  AVFormatContext *s = nut->avf;
1093  AVIOContext *bc = s->pb;
1094  int size, stream_id, discard, ret;
1095  int64_t pts, last_IP_pts;
1096  StreamContext *stc;
1097  uint8_t header_idx;
1098 
1099  size = decode_frame_header(nut, &pts, &stream_id, &header_idx, frame_code);
1100  if (size < 0)
1101  return size;
1102 
1103  stc = &nut->stream[stream_id];
1104 
1105  if (stc->last_flags & FLAG_KEY)
1106  stc->skip_until_key_frame = 0;
1107 
1108  discard = s->streams[stream_id]->discard;
1109  last_IP_pts = s->streams[stream_id]->last_IP_pts;
1110  if ((discard >= AVDISCARD_NONKEY && !(stc->last_flags & FLAG_KEY)) ||
1111  (discard >= AVDISCARD_BIDIR && last_IP_pts != AV_NOPTS_VALUE &&
1112  last_IP_pts > pts) ||
1113  discard >= AVDISCARD_ALL ||
1114  stc->skip_until_key_frame) {
1115  avio_skip(bc, size);
1116  return 1;
1117  }
1118 
1119  ret = av_new_packet(pkt, size + nut->header_len[header_idx]);
1120  if (ret < 0)
1121  return ret;
1122  if (nut->header[header_idx])
1123  memcpy(pkt->data, nut->header[header_idx], nut->header_len[header_idx]);
1124  pkt->pos = avio_tell(bc); // FIXME
1125  if (stc->last_flags & FLAG_SM_DATA) {
1126  int sm_size;
1127  if (read_sm_data(s, bc, pkt, 0, pkt->pos + size) < 0) {
1128  ret = AVERROR_INVALIDDATA;
1129  goto fail;
1130  }
1131  if (read_sm_data(s, bc, pkt, 1, pkt->pos + size) < 0) {
1132  ret = AVERROR_INVALIDDATA;
1133  goto fail;
1134  }
1135  sm_size = avio_tell(bc) - pkt->pos;
1136  size -= sm_size;
1137  pkt->size -= sm_size;
1138  }
1139 
1140  ret = avio_read(bc, pkt->data + nut->header_len[header_idx], size);
1141  if (ret != size) {
1142  if (ret < 0)
1143  goto fail;
1144  }
1145  av_shrink_packet(pkt, nut->header_len[header_idx] + ret);
1146 
1147  pkt->stream_index = stream_id;
1148  if (stc->last_flags & FLAG_KEY)
1149  pkt->flags |= AV_PKT_FLAG_KEY;
1150  pkt->pts = pts;
1151 
1152  return 0;
1153 fail:
1154  av_packet_unref(pkt);
1155  return ret;
1156 }
1157 
1159 {
1160  NUTContext *nut = s->priv_data;
1161  AVIOContext *bc = s->pb;
1162  int i, frame_code = 0, ret, skip;
1163  int64_t ts, back_ptr;
1164 
1165  for (;;) {
1166  int64_t pos = avio_tell(bc);
1167  uint64_t tmp = nut->next_startcode;
1168  nut->next_startcode = 0;
1169 
1170  if (tmp) {
1171  pos -= 8;
1172  } else {
1173  frame_code = avio_r8(bc);
1174  if (avio_feof(bc))
1175  return AVERROR_EOF;
1176  if (frame_code == 'N') {
1177  tmp = frame_code;
1178  for (i = 1; i < 8; i++)
1179  tmp = (tmp << 8) + avio_r8(bc);
1180  }
1181  }
1182  switch (tmp) {
1183  case MAIN_STARTCODE:
1184  case STREAM_STARTCODE:
1185  case INDEX_STARTCODE:
1186  skip = get_packetheader(nut, bc, 0, tmp);
1187  avio_skip(bc, skip);
1188  break;
1189  case INFO_STARTCODE:
1190  if (decode_info_header(nut) < 0)
1191  goto resync;
1192  break;
1193  case SYNCPOINT_STARTCODE:
1194  if (decode_syncpoint(nut, &ts, &back_ptr) < 0)
1195  goto resync;
1196  frame_code = avio_r8(bc);
1197  case 0:
1198  ret = decode_frame(nut, pkt, frame_code);
1199  if (ret == 0)
1200  return 0;
1201  else if (ret == 1) // OK but discard packet
1202  break;
1203  default:
1204 resync:
1205  av_log(s, AV_LOG_DEBUG, "syncing from %"PRId64"\n", pos);
1206  tmp = find_any_startcode(bc, FFMAX(nut->last_syncpoint_pos, nut->last_resync_pos) + 1);
1207  nut->last_resync_pos = avio_tell(bc);
1208  if (tmp == 0)
1209  return AVERROR_INVALIDDATA;
1210  av_log(s, AV_LOG_DEBUG, "sync\n");
1211  nut->next_startcode = tmp;
1212  }
1213  }
1214 }
1215 
1216 static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index,
1217  int64_t *pos_arg, int64_t pos_limit)
1218 {
1219  NUTContext *nut = s->priv_data;
1220  AVIOContext *bc = s->pb;
1221  int64_t pos, pts, back_ptr;
1222  av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%"PRId64",%"PRId64")\n",
1223  stream_index, *pos_arg, pos_limit);
1224 
1225  pos = *pos_arg;
1226  do {
1227  pos = find_startcode(bc, SYNCPOINT_STARTCODE, pos) + 1;
1228  if (pos < 1) {
1229  av_log(s, AV_LOG_ERROR, "read_timestamp failed.\n");
1230  return AV_NOPTS_VALUE;
1231  }
1232  } while (decode_syncpoint(nut, &pts, &back_ptr) < 0);
1233  *pos_arg = pos - 1;
1234  av_assert0(nut->last_syncpoint_pos == *pos_arg);
1235 
1236  av_log(s, AV_LOG_DEBUG, "return %"PRId64" %"PRId64"\n", pts, back_ptr);
1237  if (stream_index == -2)
1238  return back_ptr;
1239  av_assert0(stream_index == -1);
1240  return pts;
1241 }
1242 
1243 static int read_seek(AVFormatContext *s, int stream_index,
1244  int64_t pts, int flags)
1245 {
1246  NUTContext *nut = s->priv_data;
1247  AVStream *st = s->streams[stream_index];
1248  Syncpoint dummy = { .ts = pts * av_q2d(st->time_base) * AV_TIME_BASE };
1249  Syncpoint nopts_sp = { .ts = AV_NOPTS_VALUE, .back_ptr = AV_NOPTS_VALUE };
1250  Syncpoint *sp, *next_node[2] = { &nopts_sp, &nopts_sp };
1251  int64_t pos, pos2, ts;
1252  int i;
1253 
1254  if (nut->flags & NUT_PIPE) {
1255  return AVERROR(ENOSYS);
1256  }
1257 
1258  if (st->index_entries) {
1259  int index = av_index_search_timestamp(st, pts, flags);
1260  if (index < 0)
1261  index = av_index_search_timestamp(st, pts, flags ^ AVSEEK_FLAG_BACKWARD);
1262  if (index < 0)
1263  return -1;
1264 
1265  pos2 = st->index_entries[index].pos;
1266  ts = st->index_entries[index].timestamp;
1267  } else {
1269  (void **) next_node);
1270  av_log(s, AV_LOG_DEBUG, "%"PRIu64"-%"PRIu64" %"PRId64"-%"PRId64"\n",
1271  next_node[0]->pos, next_node[1]->pos, next_node[0]->ts,
1272  next_node[1]->ts);
1273  pos = ff_gen_search(s, -1, dummy.ts, next_node[0]->pos,
1274  next_node[1]->pos, next_node[1]->pos,
1275  next_node[0]->ts, next_node[1]->ts,
1277  if (pos < 0)
1278  return pos;
1279 
1280  if (!(flags & AVSEEK_FLAG_BACKWARD)) {
1281  dummy.pos = pos + 16;
1282  next_node[1] = &nopts_sp;
1284  (void **) next_node);
1285  pos2 = ff_gen_search(s, -2, dummy.pos, next_node[0]->pos,
1286  next_node[1]->pos, next_node[1]->pos,
1287  next_node[0]->back_ptr, next_node[1]->back_ptr,
1288  flags, &ts, nut_read_timestamp);
1289  if (pos2 >= 0)
1290  pos = pos2;
1291  // FIXME dir but I think it does not matter
1292  }
1293  dummy.pos = pos;
1294  sp = av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pos_cmp,
1295  NULL);
1296 
1297  av_assert0(sp);
1298  pos2 = sp->back_ptr - 15;
1299  }
1300  av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos2);
1301  pos = find_startcode(s->pb, SYNCPOINT_STARTCODE, pos2);
1302  avio_seek(s->pb, pos, SEEK_SET);
1303  nut->last_syncpoint_pos = pos;
1304  av_log(s, AV_LOG_DEBUG, "SP: %"PRId64"\n", pos);
1305  if (pos2 > pos || pos2 + 15 < pos)
1306  av_log(s, AV_LOG_ERROR, "no syncpoint at backptr pos\n");
1307  for (i = 0; i < s->nb_streams; i++)
1308  nut->stream[i].skip_until_key_frame = 1;
1309 
1310  nut->last_resync_pos = 0;
1311 
1312  return 0;
1313 }
1314 
1316  .name = "nut",
1317  .long_name = NULL_IF_CONFIG_SMALL("NUT"),
1318  .flags = AVFMT_SEEK_TO_PTS,
1319  .priv_data_size = sizeof(NUTContext),
1320  .read_probe = nut_probe,
1324  .read_seek = read_seek,
1325  .extensions = "nut",
1326  .codec_tag = ff_nut_codec_tags,
1327 };
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2512
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:907
discard all frames except keyframes
Definition: avcodec.h:235
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:334
#define AVSTREAM_EVENT_FLAG_METADATA_UPDATED
The call resulted in updated metadata.
Definition: avformat.h:990
static struct @314 state
int size
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:2052
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:3165
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:1738
#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: packet.h:375
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:103
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:4948
const AVCodecTag ff_nut_audio_extra_tags[]
Definition: nut.c:209
int64_t pos
Definition: avformat.h:805
int event_flags
Flags for the user to detect events happening on the stream.
Definition: avformat.h:989
int64_t data_offset
offset of the first packet
Definition: internal.h:80
static int get_str(AVIOContext *bc, char *string, unsigned int maxlen)
Definition: nutdec.c:41
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:938
int num
Numerator.
Definition: rational.h:59
int size
Definition: packet.h:356
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:241
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:1105
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1804
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:1666
static void set_disposition_bits(AVFormatContext *avf, char *value, int stream_id)
Definition: nutdec.c:487
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:329
discard all
Definition: avcodec.h:236
static AVPacket pkt
Definition: nut.h:91
int ff_nut_sp_pos_cmp(const void *a, const void *b)
Definition: nut.c:271
uint8_t stream_id
Definition: nut.h:67
AVDictionary * metadata
Definition: avformat.h:1312
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:245
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:4654
Format I/O context.
Definition: avformat.h:1351
static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id, uint8_t *header_idx, int frame_code)
Definition: nutdec.c:1007
#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:1216
#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:282
#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: codec_par.h:126
uint16_t flags
Definition: nut.h:66
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:778
#define AV_RB32
Definition: intreadwrite.h:130
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:92
#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:4526
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:1419
int64_t duration
Definition: movenc.c:63
const AVMetadataConv ff_nut_metadata_conv[]
Definition: nut.c:332
#define height
uint8_t * data
Definition: packet.h:355
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 decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code)
Definition: nutdec.c:1090
int ff_nut_sp_pts_cmp(const void *a, const void *b)
Definition: nut.c:277
#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:899
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
channels
Definition: aptx.h:33
#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:625
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: packet.h:388
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:88
#define AVINDEX_KEYFRAME
Definition: avformat.h:812
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
#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:1591
void ff_nut_free_sp(NUTContext *nut)
Definition: nut.c:314
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette...
Definition: packet.h:46
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:2169
#define NUT_BROADCAST
Definition: nut.h:113
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:747
discard all bidirectional frames
Definition: avcodec.h:233
An AV_PKT_DATA_PARAM_CHANGE side data packet is laid out as follows:
Definition: packet.h:72
#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:806
#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:188
const uint8_t * code
Definition: spdifenc.c:413
int video_delay
Video only.
Definition: codec_par.h:155
unsigned int pos
Definition: spdifenc.c:412
#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: codec_par.h:56
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:1158
const AVCodecTag ff_nut_audio_tags[]
Definition: nut.c:219
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:378
#define av_be2ne64(x)
Definition: bswap.h:94
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:506
#define FFMAX(a, b)
Definition: common.h:94
#define fail()
Definition: checkasm.h:123
Definition: nut.h:44
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:361
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:78
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:616
static int nut_read_close(AVFormatContext *s)
Definition: nutdec.c:795
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
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1407
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:604
const char * name
Definition: qsvenc.c:46
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:253
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
#define width
static uint64_t find_any_startcode(AVIOContext *bc, int64_t pos)
Definition: nutdec.c:109
uint16_t size_lsb
Definition: nut.h:69
static int nut_probe(const AVProbeData *p)
Definition: nutdec.c:152
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:578
int16_t pts_delta
Definition: nut.h:70
static int find_and_decode_index(NUTContext *nut)
Definition: nutdec.c:681
int64_t ff_lsb2full(StreamContext *stream, int64_t lsb)
Definition: nut.c:264
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:1667
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define s(width, name)
Definition: cbs_vp9.c:257
struct AVTreeNode * syncpoints
Definition: nut.h:108
AVDictionary * metadata
Definition: avformat.h:940
int dummy
Definition: motion.c:64
static int nut_read_header(AVFormatContext *s)
Definition: nutdec.c:809
#define INDEX_STARTCODE
Definition: nut.h:32
uint16_t size_mul
Definition: nut.h:68
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:545
static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr)
Definition: nutdec.c:625
Stream structure.
Definition: avformat.h:876
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: packet.h:55
static int read_sm_data(AVFormatContext *s, AVIOContext *bc, AVPacket *pkt, int is_meta, int64_t maxpos)
Definition: nutdec.c:882
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:1393
static int resync(AVFormatContext *s)
Definition: flvdec.c:976
int max_pts_distance
Definition: nut.h:82
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:605
Definition: nut.h:54
Data found in BlockAdditional element of matroska container.
Definition: packet.h:191
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:2290
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: packet.h:156
byte swapping routines
cl_device_type type
unsigned long ffio_get_checksum(AVIOContext *s)
Definition: aviobuf.c:596
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:493
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:441
static int read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
Definition: nutdec.c:1243
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:2252
#define INFO_STARTCODE
Definition: nut.h:33
static int64_t pts
#define flags(name, subs,...)
Definition: cbs_av1.c:565
int version
Definition: nut.h:116
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
Duration accurately estimated from PTSes.
Definition: avformat.h:1330
int skip_until_key_frame
Definition: nut.h:77
static int64_t find_duration(NUTContext *nut, int64_t filesize)
Definition: nutdec.c:669
int sample_rate
Audio only.
Definition: codec_par.h:170
const Dispositions ff_nut_dispositions[]
Definition: nut.c:322
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:731
uint64_t next_startcode
Definition: nut.h:99
#define flag(name)
Definition: cbs_av1.c:557
static int decode_info_header(NUTContext *nut)
Definition: nutdec.c:502
FrameCode frame_code[256]
Definition: nut.h:96
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:929
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:3346
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:283
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 was unable to read due to error or eof
Definition: avio.h:239
int len
AVInputFormat ff_nut_demuxer
Definition: nutdec.c:1315
static int64_t get_s(AVIOContext *bc)
Definition: nutdec.c:65
void * priv_data
Format private data.
Definition: avformat.h:1379
int time_base_id
Definition: nut.h:79
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
int channels
Audio only.
Definition: codec_par.h:166
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1466
int64_t last_IP_pts
Definition: avformat.h:1080
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:650
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1023
#define av_malloc_array(a, b)
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:356
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:332
int stream_index
Definition: packet.h:357
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:905
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:931
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:1000
const AVCodecTag *const ff_nut_codec_tags[]
Definition: nut.c:248
This structure stores compressed data.
Definition: packet.h:332
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:755
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: packet.h:348
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