FFmpeg  4.1.11
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, 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  if (ff_get_extradata(s, st->codecpar, bc, st->codecpar->extradata_size) < 0)
447  return AVERROR(ENOMEM);
448  }
449 
450  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
451  GET_V(st->codecpar->width, tmp > 0);
452  GET_V(st->codecpar->height, tmp > 0);
455  if ((!st->sample_aspect_ratio.num) != (!st->sample_aspect_ratio.den)) {
456  av_log(s, AV_LOG_ERROR, "invalid aspect ratio %d/%d\n",
458  ret = AVERROR_INVALIDDATA;
459  goto fail;
460  }
461  ffio_read_varlen(bc); /* csp type */
462  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
463  GET_V(st->codecpar->sample_rate, tmp > 0);
464  ffio_read_varlen(bc); // samplerate_den
465  GET_V(st->codecpar->channels, tmp > 0);
466  }
467  if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
468  av_log(s, AV_LOG_ERROR,
469  "stream header %d checksum mismatch\n", stream_id);
470  ret = AVERROR_INVALIDDATA;
471  goto fail;
472  }
473  stc->time_base = &nut->time_base[stc->time_base_id];
474  avpriv_set_pts_info(s->streams[stream_id], 63, stc->time_base->num,
475  stc->time_base->den);
476  return 0;
477 fail:
478  if (st && st->codecpar) {
479  av_freep(&st->codecpar->extradata);
480  st->codecpar->extradata_size = 0;
481  }
482  return ret;
483 }
484 
486  int stream_id)
487 {
488  int flag = 0, i;
489 
490  for (i = 0; ff_nut_dispositions[i].flag; ++i)
491  if (!strcmp(ff_nut_dispositions[i].str, value))
492  flag = ff_nut_dispositions[i].flag;
493  if (!flag)
494  av_log(avf, AV_LOG_INFO, "unknown disposition type '%s'\n", value);
495  for (i = 0; i < avf->nb_streams; ++i)
496  if (stream_id == i || stream_id == -1)
497  avf->streams[i]->disposition |= flag;
498 }
499 
501 {
502  AVFormatContext *s = nut->avf;
503  AVIOContext *bc = s->pb;
504  uint64_t tmp, chapter_start, chapter_len;
505  unsigned int stream_id_plus1, count;
506  int chapter_id, i, ret = 0;
507  int64_t value, end;
508  char name[256], str_value[1024], type_str[256];
509  const char *type;
510  int *event_flags = NULL;
511  AVChapter *chapter = NULL;
512  AVStream *st = NULL;
513  AVDictionary **metadata = NULL;
514  int metadata_flag = 0;
515 
516  end = get_packetheader(nut, bc, 1, INFO_STARTCODE);
517  end += avio_tell(bc);
518 
519  GET_V(stream_id_plus1, tmp <= s->nb_streams);
520  chapter_id = get_s(bc);
521  chapter_start = ffio_read_varlen(bc);
522  chapter_len = ffio_read_varlen(bc);
523  count = ffio_read_varlen(bc);
524 
525  if (chapter_id && !stream_id_plus1) {
526  int64_t start = chapter_start / nut->time_base_count;
527  chapter = avpriv_new_chapter(s, chapter_id,
528  nut->time_base[chapter_start %
529  nut->time_base_count],
530  start, start + chapter_len, NULL);
531  if (!chapter) {
532  av_log(s, AV_LOG_ERROR, "Could not create chapter.\n");
533  return AVERROR(ENOMEM);
534  }
535  metadata = &chapter->metadata;
536  } else if (stream_id_plus1) {
537  st = s->streams[stream_id_plus1 - 1];
538  metadata = &st->metadata;
539  event_flags = &st->event_flags;
540  metadata_flag = AVSTREAM_EVENT_FLAG_METADATA_UPDATED;
541  } else {
542  metadata = &s->metadata;
543  event_flags = &s->event_flags;
544  metadata_flag = AVFMT_EVENT_FLAG_METADATA_UPDATED;
545  }
546 
547  for (i = 0; i < count; i++) {
548  ret = get_str(bc, name, sizeof(name));
549  if (ret < 0) {
550  av_log(s, AV_LOG_ERROR, "get_str failed while decoding info header\n");
551  return ret;
552  }
553  value = get_s(bc);
554  str_value[0] = 0;
555 
556  if (value == -1) {
557  type = "UTF-8";
558  ret = get_str(bc, str_value, sizeof(str_value));
559  } else if (value == -2) {
560  ret = get_str(bc, type_str, sizeof(type_str));
561  if (ret < 0) {
562  av_log(s, AV_LOG_ERROR, "get_str failed while decoding info header\n");
563  return ret;
564  }
565  type = type_str;
566  ret = get_str(bc, str_value, sizeof(str_value));
567  } else if (value == -3) {
568  type = "s";
569  value = get_s(bc);
570  } else if (value == -4) {
571  type = "t";
572  value = ffio_read_varlen(bc);
573  } else if (value < -4) {
574  type = "r";
575  get_s(bc);
576  } else {
577  type = "v";
578  }
579 
580  if (ret < 0) {
581  av_log(s, AV_LOG_ERROR, "get_str failed while decoding info header\n");
582  return ret;
583  }
584 
585  if (stream_id_plus1 > s->nb_streams) {
587  "invalid stream id %d for info packet\n",
588  stream_id_plus1);
589  continue;
590  }
591 
592  if (!strcmp(type, "UTF-8")) {
593  if (chapter_id == 0 && !strcmp(name, "Disposition")) {
594  set_disposition_bits(s, str_value, stream_id_plus1 - 1);
595  continue;
596  }
597 
598  if (stream_id_plus1 && !strcmp(name, "r_frame_rate")) {
599  sscanf(str_value, "%d/%d", &st->r_frame_rate.num, &st->r_frame_rate.den);
600  if (st->r_frame_rate.num >= 1000LL*st->r_frame_rate.den ||
601  st->r_frame_rate.num < 0 || st->r_frame_rate.num < 0)
602  st->r_frame_rate.num = st->r_frame_rate.den = 0;
603  continue;
604  }
605 
606  if (metadata && av_strcasecmp(name, "Uses") &&
607  av_strcasecmp(name, "Depends") && av_strcasecmp(name, "Replaces")) {
608  if (event_flags)
609  *event_flags |= metadata_flag;
610  av_dict_set(metadata, name, str_value, 0);
611  }
612  }
613  }
614 
615  if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
616  av_log(s, AV_LOG_ERROR, "info header checksum mismatch\n");
617  return AVERROR_INVALIDDATA;
618  }
619 fail:
620  return FFMIN(ret, 0);
621 }
622 
623 static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr)
624 {
625  AVFormatContext *s = nut->avf;
626  AVIOContext *bc = s->pb;
627  int64_t end;
628  uint64_t tmp;
629  int ret;
630 
631  nut->last_syncpoint_pos = avio_tell(bc) - 8;
632 
633  end = get_packetheader(nut, bc, 1, SYNCPOINT_STARTCODE);
634  end += avio_tell(bc);
635 
636  tmp = ffio_read_varlen(bc);
637  *back_ptr = nut->last_syncpoint_pos - 16 * ffio_read_varlen(bc);
638  if (*back_ptr < 0)
639  return AVERROR_INVALIDDATA;
640 
641  ff_nut_reset_ts(nut, nut->time_base[tmp % nut->time_base_count],
642  tmp / nut->time_base_count);
643 
644  if (nut->flags & NUT_BROADCAST) {
645  tmp = ffio_read_varlen(bc);
646  av_log(s, AV_LOG_VERBOSE, "Syncpoint wallclock %"PRId64"\n",
647  av_rescale_q(tmp / nut->time_base_count,
648  nut->time_base[tmp % nut->time_base_count],
649  AV_TIME_BASE_Q));
650  }
651 
652  if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
653  av_log(s, AV_LOG_ERROR, "sync point checksum mismatch\n");
654  return AVERROR_INVALIDDATA;
655  }
656 
657  *ts = tmp / nut->time_base_count *
658  av_q2d(nut->time_base[tmp % nut->time_base_count]) * AV_TIME_BASE;
659 
660  if ((ret = ff_nut_add_sp(nut, nut->last_syncpoint_pos, *back_ptr, *ts)) < 0)
661  return ret;
662 
663  return 0;
664 }
665 
666 //FIXME calculate exactly, this is just a good approximation.
667 static int64_t find_duration(NUTContext *nut, int64_t filesize)
668 {
669  AVFormatContext *s = nut->avf;
670  int64_t duration = 0;
671 
672  ff_find_last_ts(s, -1, &duration, NULL, nut_read_timestamp);
673 
674  if(duration > 0)
676  return duration;
677 }
678 
680 {
681  AVFormatContext *s = nut->avf;
682  AVIOContext *bc = s->pb;
683  uint64_t tmp, end;
684  int i, j, syncpoint_count;
685  int64_t filesize = avio_size(bc);
686  int64_t *syncpoints = NULL;
687  uint64_t max_pts;
688  int8_t *has_keyframe = NULL;
689  int ret = AVERROR_INVALIDDATA;
690 
691  if(filesize <= 0)
692  return -1;
693 
694  avio_seek(bc, filesize - 12, SEEK_SET);
695  avio_seek(bc, filesize - avio_rb64(bc), SEEK_SET);
696  if (avio_rb64(bc) != INDEX_STARTCODE) {
697  av_log(s, AV_LOG_WARNING, "no index at the end\n");
698 
699  if(s->duration<=0)
700  s->duration = find_duration(nut, filesize);
701  return ret;
702  }
703 
704  end = get_packetheader(nut, bc, 1, INDEX_STARTCODE);
705  end += avio_tell(bc);
706 
707  max_pts = ffio_read_varlen(bc);
708  s->duration = av_rescale_q(max_pts / nut->time_base_count,
709  nut->time_base[max_pts % nut->time_base_count],
712 
713  GET_V(syncpoint_count, tmp < INT_MAX / 8 && tmp > 0);
714  syncpoints = av_malloc_array(syncpoint_count, sizeof(int64_t));
715  has_keyframe = av_malloc_array(syncpoint_count + 1, sizeof(int8_t));
716  if (!syncpoints || !has_keyframe) {
717  ret = AVERROR(ENOMEM);
718  goto fail;
719  }
720  for (i = 0; i < syncpoint_count; i++) {
721  syncpoints[i] = ffio_read_varlen(bc);
722  if (syncpoints[i] <= 0)
723  goto fail;
724  if (i)
725  syncpoints[i] += syncpoints[i - 1];
726  }
727 
728  for (i = 0; i < s->nb_streams; i++) {
729  int64_t last_pts = -1;
730  for (j = 0; j < syncpoint_count;) {
731  uint64_t x = ffio_read_varlen(bc);
732  int type = x & 1;
733  int n = j;
734  x >>= 1;
735  if (type) {
736  int flag = x & 1;
737  x >>= 1;
738  if (n + x >= syncpoint_count + 1) {
739  av_log(s, AV_LOG_ERROR, "index overflow A %d + %"PRIu64" >= %d\n", n, x, syncpoint_count + 1);
740  goto fail;
741  }
742  while (x--)
743  has_keyframe[n++] = flag;
744  has_keyframe[n++] = !flag;
745  } else {
746  if (x <= 1) {
747  av_log(s, AV_LOG_ERROR, "index: x %"PRIu64" is invalid\n", x);
748  goto fail;
749  }
750  while (x != 1) {
751  if (n >= syncpoint_count + 1) {
752  av_log(s, AV_LOG_ERROR, "index overflow B\n");
753  goto fail;
754  }
755  has_keyframe[n++] = x & 1;
756  x >>= 1;
757  }
758  }
759  if (has_keyframe[0]) {
760  av_log(s, AV_LOG_ERROR, "keyframe before first syncpoint in index\n");
761  goto fail;
762  }
763  av_assert0(n <= syncpoint_count + 1);
764  for (; j < n && j < syncpoint_count; j++) {
765  if (has_keyframe[j]) {
766  uint64_t B, A = ffio_read_varlen(bc);
767  if (!A) {
768  A = ffio_read_varlen(bc);
769  B = ffio_read_varlen(bc);
770  // eor_pts[j][i] = last_pts + A + B
771  } else
772  B = 0;
773  av_add_index_entry(s->streams[i], 16 * syncpoints[j - 1],
774  last_pts + A, 0, 0, AVINDEX_KEYFRAME);
775  last_pts += A + B;
776  }
777  }
778  }
779  }
780 
781  if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
782  av_log(s, AV_LOG_ERROR, "index checksum mismatch\n");
783  goto fail;
784  }
785  ret = 0;
786 
787 fail:
788  av_free(syncpoints);
789  av_free(has_keyframe);
790  return ret;
791 }
792 
794 {
795  NUTContext *nut = s->priv_data;
796  int i;
797 
798  av_freep(&nut->time_base);
799  av_freep(&nut->stream);
800  ff_nut_free_sp(nut);
801  for (i = 1; i < nut->header_count; i++)
802  av_freep(&nut->header[i]);
803 
804  return 0;
805 }
806 
808 {
809  NUTContext *nut = s->priv_data;
810  AVIOContext *bc = s->pb;
811  int64_t pos;
812  int initialized_stream_count, ret;
813 
814  nut->avf = s;
815 
816  /* main header */
817  pos = 0;
818  ret = 0;
819  do {
820  if (ret == AVERROR(ENOMEM))
821  return ret;
822 
823  pos = find_startcode(bc, MAIN_STARTCODE, pos) + 1;
824  if (pos < 0 + 1) {
825  av_log(s, AV_LOG_ERROR, "No main startcode found.\n");
826  goto fail;
827  }
828  } while ((ret = decode_main_header(nut)) < 0);
829 
830  /* stream headers */
831  pos = 0;
832  for (initialized_stream_count = 0; initialized_stream_count < s->nb_streams;) {
833  pos = find_startcode(bc, STREAM_STARTCODE, pos) + 1;
834  if (pos < 0 + 1) {
835  av_log(s, AV_LOG_ERROR, "Not all stream headers found.\n");
836  goto fail;
837  }
838  if (decode_stream_header(nut) >= 0)
839  initialized_stream_count++;
840  }
841 
842  /* info headers */
843  pos = 0;
844  for (;;) {
845  uint64_t startcode = find_any_startcode(bc, pos);
846  pos = avio_tell(bc);
847 
848  if (startcode == 0) {
849  av_log(s, AV_LOG_ERROR, "EOF before video frames\n");
850  goto fail;
851  } else if (startcode == SYNCPOINT_STARTCODE) {
852  nut->next_startcode = startcode;
853  break;
854  } else if (startcode != INFO_STARTCODE) {
855  continue;
856  }
857 
858  decode_info_header(nut);
859  }
860 
861  s->internal->data_offset = pos - 8;
862 
863  if (bc->seekable & AVIO_SEEKABLE_NORMAL) {
864  int64_t orig_pos = avio_tell(bc);
866  avio_seek(bc, orig_pos, SEEK_SET);
867  }
869 
871 
872  return 0;
873 
874 fail:
875  nut_read_close(s);
876 
877  return AVERROR_INVALIDDATA;
878 }
879 
880 static int read_sm_data(AVFormatContext *s, AVIOContext *bc, AVPacket *pkt, int is_meta, int64_t maxpos)
881 {
882  int count = ffio_read_varlen(bc);
883  int skip_start = 0;
884  int skip_end = 0;
885  int channels = 0;
886  int64_t channel_layout = 0;
887  int sample_rate = 0;
888  int width = 0;
889  int height = 0;
890  int i, ret;
891 
892  for (i=0; i<count; i++) {
893  uint8_t name[256], str_value[256], type_str[256];
894  int value;
895  if (avio_tell(bc) >= maxpos)
896  return AVERROR_INVALIDDATA;
897  ret = get_str(bc, name, sizeof(name));
898  if (ret < 0) {
899  av_log(s, AV_LOG_ERROR, "get_str failed while reading sm data\n");
900  return ret;
901  }
902  value = get_s(bc);
903 
904  if (value == -1) {
905  ret = get_str(bc, str_value, sizeof(str_value));
906  if (ret < 0) {
907  av_log(s, AV_LOG_ERROR, "get_str failed while reading sm data\n");
908  return ret;
909  }
910  av_log(s, AV_LOG_WARNING, "Unknown string %s / %s\n", name, str_value);
911  } else if (value == -2) {
912  uint8_t *dst = NULL;
913  int64_t v64, value_len;
914 
915  ret = get_str(bc, type_str, sizeof(type_str));
916  if (ret < 0) {
917  av_log(s, AV_LOG_ERROR, "get_str failed while reading sm data\n");
918  return ret;
919  }
920  value_len = ffio_read_varlen(bc);
921  if (value_len < 0 || value_len >= maxpos - avio_tell(bc))
922  return AVERROR_INVALIDDATA;
923  if (!strcmp(name, "Palette")) {
924  dst = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, value_len);
925  } else if (!strcmp(name, "Extradata")) {
926  dst = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, value_len);
927  } else if (sscanf(name, "CodecSpecificSide%"SCNd64"", &v64) == 1) {
929  if(!dst)
930  return AVERROR(ENOMEM);
931  AV_WB64(dst, v64);
932  dst += 8;
933  } else if (!strcmp(name, "ChannelLayout") && value_len == 8) {
934  channel_layout = avio_rl64(bc);
935  continue;
936  } else {
937  av_log(s, AV_LOG_WARNING, "Unknown data %s / %s\n", name, type_str);
938  avio_skip(bc, value_len);
939  continue;
940  }
941  if(!dst)
942  return AVERROR(ENOMEM);
943  avio_read(bc, dst, value_len);
944  } else if (value == -3) {
945  value = get_s(bc);
946  } else if (value == -4) {
947  value = ffio_read_varlen(bc);
948  } else if (value < -4) {
949  get_s(bc);
950  } else {
951  if (!strcmp(name, "SkipStart")) {
952  skip_start = value;
953  } else if (!strcmp(name, "SkipEnd")) {
954  skip_end = value;
955  } else if (!strcmp(name, "Channels")) {
956  channels = value;
957  } else if (!strcmp(name, "SampleRate")) {
958  sample_rate = value;
959  } else if (!strcmp(name, "Width")) {
960  width = value;
961  } else if (!strcmp(name, "Height")) {
962  height = value;
963  } else {
964  av_log(s, AV_LOG_WARNING, "Unknown integer %s\n", name);
965  }
966  }
967  }
968 
969  if (channels || channel_layout || sample_rate || width || height) {
971  if (!dst)
972  return AVERROR(ENOMEM);
973  bytestream_put_le32(&dst,
975  AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT*(!!channel_layout) +
976  AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE*(!!sample_rate) +
977  AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS*(!!(width|height))
978  );
979  if (channels)
980  bytestream_put_le32(&dst, channels);
981  if (channel_layout)
982  bytestream_put_le64(&dst, channel_layout);
983  if (sample_rate)
984  bytestream_put_le32(&dst, sample_rate);
985  if (width || height){
986  bytestream_put_le32(&dst, width);
987  bytestream_put_le32(&dst, height);
988  }
989  }
990 
991  if (skip_start || skip_end) {
993  if (!dst)
994  return AVERROR(ENOMEM);
995  AV_WL32(dst, skip_start);
996  AV_WL32(dst+4, skip_end);
997  }
998 
999  if (avio_tell(bc) >= maxpos)
1000  return AVERROR_INVALIDDATA;
1001 
1002  return 0;
1003 }
1004 
1005 static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id,
1006  uint8_t *header_idx, int frame_code)
1007 {
1008  AVFormatContext *s = nut->avf;
1009  AVIOContext *bc = s->pb;
1010  StreamContext *stc;
1011  int size, flags, size_mul, pts_delta, i, reserved_count, ret;
1012  uint64_t tmp;
1013 
1014  if (!(nut->flags & NUT_PIPE) &&
1015  avio_tell(bc) > nut->last_syncpoint_pos + nut->max_distance) {
1016  av_log(s, AV_LOG_ERROR,
1017  "Last frame must have been damaged %"PRId64" > %"PRId64" + %d\n",
1018  avio_tell(bc), nut->last_syncpoint_pos, nut->max_distance);
1019  return AVERROR_INVALIDDATA;
1020  }
1021 
1022  flags = nut->frame_code[frame_code].flags;
1023  size_mul = nut->frame_code[frame_code].size_mul;
1024  size = nut->frame_code[frame_code].size_lsb;
1025  *stream_id = nut->frame_code[frame_code].stream_id;
1026  pts_delta = nut->frame_code[frame_code].pts_delta;
1027  reserved_count = nut->frame_code[frame_code].reserved_count;
1028  *header_idx = nut->frame_code[frame_code].header_idx;
1029 
1030  if (flags & FLAG_INVALID)
1031  return AVERROR_INVALIDDATA;
1032  if (flags & FLAG_CODED)
1033  flags ^= ffio_read_varlen(bc);
1034  if (flags & FLAG_STREAM_ID) {
1035  GET_V(*stream_id, tmp < s->nb_streams);
1036  }
1037  stc = &nut->stream[*stream_id];
1038  if (flags & FLAG_CODED_PTS) {
1039  int coded_pts = ffio_read_varlen(bc);
1040  // FIXME check last_pts validity?
1041  if (coded_pts < (1 << stc->msb_pts_shift)) {
1042  *pts = ff_lsb2full(stc, coded_pts);
1043  } else
1044  *pts = coded_pts - (1LL << stc->msb_pts_shift);
1045  } else
1046  *pts = stc->last_pts + pts_delta;
1047  if (flags & FLAG_SIZE_MSB)
1048  size += size_mul * ffio_read_varlen(bc);
1049  if (flags & FLAG_MATCH_TIME)
1050  get_s(bc);
1051  if (flags & FLAG_HEADER_IDX)
1052  *header_idx = ffio_read_varlen(bc);
1053  if (flags & FLAG_RESERVED)
1054  reserved_count = ffio_read_varlen(bc);
1055  for (i = 0; i < reserved_count; i++) {
1056  if (bc->eof_reached) {
1057  av_log(s, AV_LOG_ERROR, "reached EOF while decoding frame header\n");
1058  return AVERROR_INVALIDDATA;
1059  }
1060  ffio_read_varlen(bc);
1061  }
1062 
1063  if (*header_idx >= (unsigned)nut->header_count) {
1064  av_log(s, AV_LOG_ERROR, "header_idx invalid\n");
1065  return AVERROR_INVALIDDATA;
1066  }
1067  if (size > 4096)
1068  *header_idx = 0;
1069  size -= nut->header_len[*header_idx];
1070 
1071  if (flags & FLAG_CHECKSUM) {
1072  avio_rb32(bc); // FIXME check this
1073  } else if (!(nut->flags & NUT_PIPE) &&
1074  size > 2 * nut->max_distance ||
1075  FFABS(stc->last_pts - *pts) > stc->max_pts_distance) {
1076  av_log(s, AV_LOG_ERROR, "frame size > 2max_distance and no checksum\n");
1077  return AVERROR_INVALIDDATA;
1078  }
1079 
1080  stc->last_pts = *pts;
1081  stc->last_flags = flags;
1082 
1083  return size;
1084 fail:
1085  return ret;
1086 }
1087 
1088 static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code)
1089 {
1090  AVFormatContext *s = nut->avf;
1091  AVIOContext *bc = s->pb;
1092  int size, stream_id, discard, ret;
1093  int64_t pts, last_IP_pts;
1094  StreamContext *stc;
1095  uint8_t header_idx;
1096 
1097  size = decode_frame_header(nut, &pts, &stream_id, &header_idx, frame_code);
1098  if (size < 0)
1099  return size;
1100 
1101  stc = &nut->stream[stream_id];
1102 
1103  if (stc->last_flags & FLAG_KEY)
1104  stc->skip_until_key_frame = 0;
1105 
1106  discard = s->streams[stream_id]->discard;
1107  last_IP_pts = s->streams[stream_id]->last_IP_pts;
1108  if ((discard >= AVDISCARD_NONKEY && !(stc->last_flags & FLAG_KEY)) ||
1109  (discard >= AVDISCARD_BIDIR && last_IP_pts != AV_NOPTS_VALUE &&
1110  last_IP_pts > pts) ||
1111  discard >= AVDISCARD_ALL ||
1112  stc->skip_until_key_frame) {
1113  avio_skip(bc, size);
1114  return 1;
1115  }
1116 
1117  ret = av_new_packet(pkt, size + nut->header_len[header_idx]);
1118  if (ret < 0)
1119  return ret;
1120  if (nut->header[header_idx])
1121  memcpy(pkt->data, nut->header[header_idx], nut->header_len[header_idx]);
1122  pkt->pos = avio_tell(bc); // FIXME
1123  if (stc->last_flags & FLAG_SM_DATA) {
1124  int sm_size;
1125  if (read_sm_data(s, bc, pkt, 0, pkt->pos + size) < 0) {
1126  ret = AVERROR_INVALIDDATA;
1127  goto fail;
1128  }
1129  if (read_sm_data(s, bc, pkt, 1, pkt->pos + size) < 0) {
1130  ret = AVERROR_INVALIDDATA;
1131  goto fail;
1132  }
1133  sm_size = avio_tell(bc) - pkt->pos;
1134  size -= sm_size;
1135  pkt->size -= sm_size;
1136  }
1137 
1138  ret = avio_read(bc, pkt->data + nut->header_len[header_idx], size);
1139  if (ret != size) {
1140  if (ret < 0)
1141  goto fail;
1142  }
1143  av_shrink_packet(pkt, nut->header_len[header_idx] + ret);
1144 
1145  pkt->stream_index = stream_id;
1146  if (stc->last_flags & FLAG_KEY)
1147  pkt->flags |= AV_PKT_FLAG_KEY;
1148  pkt->pts = pts;
1149 
1150  return 0;
1151 fail:
1152  av_packet_unref(pkt);
1153  return ret;
1154 }
1155 
1157 {
1158  NUTContext *nut = s->priv_data;
1159  AVIOContext *bc = s->pb;
1160  int i, frame_code = 0, ret, skip;
1161  int64_t ts, back_ptr;
1162 
1163  for (;;) {
1164  int64_t pos = avio_tell(bc);
1165  uint64_t tmp = nut->next_startcode;
1166  nut->next_startcode = 0;
1167 
1168  if (tmp) {
1169  pos -= 8;
1170  } else {
1171  frame_code = avio_r8(bc);
1172  if (avio_feof(bc))
1173  return AVERROR_EOF;
1174  if (frame_code == 'N') {
1175  tmp = frame_code;
1176  for (i = 1; i < 8; i++)
1177  tmp = (tmp << 8) + avio_r8(bc);
1178  }
1179  }
1180  switch (tmp) {
1181  case MAIN_STARTCODE:
1182  case STREAM_STARTCODE:
1183  case INDEX_STARTCODE:
1184  skip = get_packetheader(nut, bc, 0, tmp);
1185  avio_skip(bc, skip);
1186  break;
1187  case INFO_STARTCODE:
1188  if (decode_info_header(nut) < 0)
1189  goto resync;
1190  break;
1191  case SYNCPOINT_STARTCODE:
1192  if (decode_syncpoint(nut, &ts, &back_ptr) < 0)
1193  goto resync;
1194  frame_code = avio_r8(bc);
1195  case 0:
1196  ret = decode_frame(nut, pkt, frame_code);
1197  if (ret == 0)
1198  return 0;
1199  else if (ret == 1) // OK but discard packet
1200  break;
1201  default:
1202 resync:
1203  av_log(s, AV_LOG_DEBUG, "syncing from %"PRId64"\n", pos);
1204  tmp = find_any_startcode(bc, FFMAX(nut->last_syncpoint_pos, nut->last_resync_pos) + 1);
1205  nut->last_resync_pos = avio_tell(bc);
1206  if (tmp == 0)
1207  return AVERROR_INVALIDDATA;
1208  av_log(s, AV_LOG_DEBUG, "sync\n");
1209  nut->next_startcode = tmp;
1210  }
1211  }
1212 }
1213 
1214 static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index,
1215  int64_t *pos_arg, int64_t pos_limit)
1216 {
1217  NUTContext *nut = s->priv_data;
1218  AVIOContext *bc = s->pb;
1219  int64_t pos, pts, back_ptr;
1220  av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%"PRId64",%"PRId64")\n",
1221  stream_index, *pos_arg, pos_limit);
1222 
1223  pos = *pos_arg;
1224  do {
1225  pos = find_startcode(bc, SYNCPOINT_STARTCODE, pos) + 1;
1226  if (pos < 1) {
1227  av_log(s, AV_LOG_ERROR, "read_timestamp failed.\n");
1228  return AV_NOPTS_VALUE;
1229  }
1230  } while (decode_syncpoint(nut, &pts, &back_ptr) < 0);
1231  *pos_arg = pos - 1;
1232  av_assert0(nut->last_syncpoint_pos == *pos_arg);
1233 
1234  av_log(s, AV_LOG_DEBUG, "return %"PRId64" %"PRId64"\n", pts, back_ptr);
1235  if (stream_index == -2)
1236  return back_ptr;
1237  av_assert0(stream_index == -1);
1238  return pts;
1239 }
1240 
1241 static int read_seek(AVFormatContext *s, int stream_index,
1242  int64_t pts, int flags)
1243 {
1244  NUTContext *nut = s->priv_data;
1245  AVStream *st = s->streams[stream_index];
1246  Syncpoint dummy = { .ts = pts * av_q2d(st->time_base) * AV_TIME_BASE };
1247  Syncpoint nopts_sp = { .ts = AV_NOPTS_VALUE, .back_ptr = AV_NOPTS_VALUE };
1248  Syncpoint *sp, *next_node[2] = { &nopts_sp, &nopts_sp };
1249  int64_t pos, pos2, ts;
1250  int i;
1251 
1252  if (nut->flags & NUT_PIPE) {
1253  return AVERROR(ENOSYS);
1254  }
1255 
1256  if (st->index_entries) {
1257  int index = av_index_search_timestamp(st, pts, flags);
1258  if (index < 0)
1259  index = av_index_search_timestamp(st, pts, flags ^ AVSEEK_FLAG_BACKWARD);
1260  if (index < 0)
1261  return -1;
1262 
1263  pos2 = st->index_entries[index].pos;
1264  ts = st->index_entries[index].timestamp;
1265  } else {
1267  (void **) next_node);
1268  av_log(s, AV_LOG_DEBUG, "%"PRIu64"-%"PRIu64" %"PRId64"-%"PRId64"\n",
1269  next_node[0]->pos, next_node[1]->pos, next_node[0]->ts,
1270  next_node[1]->ts);
1271  pos = ff_gen_search(s, -1, dummy.ts, next_node[0]->pos,
1272  next_node[1]->pos, next_node[1]->pos,
1273  next_node[0]->ts, next_node[1]->ts,
1275  if (pos < 0)
1276  return pos;
1277 
1278  if (!(flags & AVSEEK_FLAG_BACKWARD)) {
1279  dummy.pos = pos + 16;
1280  next_node[1] = &nopts_sp;
1282  (void **) next_node);
1283  pos2 = ff_gen_search(s, -2, dummy.pos, next_node[0]->pos,
1284  next_node[1]->pos, next_node[1]->pos,
1285  next_node[0]->back_ptr, next_node[1]->back_ptr,
1286  flags, &ts, nut_read_timestamp);
1287  if (pos2 >= 0)
1288  pos = pos2;
1289  // FIXME dir but I think it does not matter
1290  }
1291  dummy.pos = pos;
1292  sp = av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pos_cmp,
1293  NULL);
1294 
1295  av_assert0(sp);
1296  pos2 = sp->back_ptr - 15;
1297  }
1298  av_log(NULL, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos2);
1299  pos = find_startcode(s->pb, SYNCPOINT_STARTCODE, pos2);
1300  avio_seek(s->pb, pos, SEEK_SET);
1301  nut->last_syncpoint_pos = pos;
1302  av_log(NULL, AV_LOG_DEBUG, "SP: %"PRId64"\n", pos);
1303  if (pos2 > pos || pos2 + 15 < pos)
1304  av_log(NULL, AV_LOG_ERROR, "no syncpoint at backptr pos\n");
1305  for (i = 0; i < s->nb_streams; i++)
1306  nut->stream[i].skip_until_key_frame = 1;
1307 
1308  nut->last_resync_pos = 0;
1309 
1310  return 0;
1311 }
1312 
1314  .name = "nut",
1315  .long_name = NULL_IF_CONFIG_SMALL("NUT"),
1316  .flags = AVFMT_SEEK_TO_PTS,
1317  .priv_data_size = sizeof(NUTContext),
1318  .read_probe = nut_probe,
1322  .read_seek = read_seek,
1323  .extensions = "nut",
1324  .codec_tag = ff_nut_codec_tags,
1325 };
const char * name
Definition: avisynth_c.h:775
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2504
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:929
discard all frames except keyframes
Definition: avcodec.h:802
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:988
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:2048
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:3136
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: avcodec.h:1465
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:4899
const AVCodecTag ff_nut_audio_extra_tags[]
Definition: nut.c:203
int64_t pos
Definition: avformat.h:803
int event_flags
Flags for the user to detect events happening on the stream.
Definition: avformat.h:987
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:3900
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:936
int num
Numerator.
Definition: rational.h:59
int size
Definition: avcodec.h:1446
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:1103
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:485
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:803
static AVPacket pkt
Definition: nut.h:91
int ff_nut_sp_pos_cmp(const void *a, const void *b)
Definition: nut.c:263
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: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:4597
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:1005
#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:1214
#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:3966
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:800
#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:4469
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:324
#define height
uint8_t * data
Definition: avcodec.h:1445
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:1088
int ff_nut_sp_pts_cmp(const void *a, const void *b)
Definition: nut.c:269
#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:921
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:647
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:1477
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 i(width, name, range_min, range_max)
Definition: cbs_h2645.c:258
#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:306
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette...
Definition: avcodec.h:1158
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:2159
#define NUT_BROADCAST
Definition: nut.h:113
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:769
discard all bidirectional frames
Definition: avcodec.h:800
An AV_PKT_DATA_PARAM_CHANGE side data packet is laid out as follows:
Definition: avcodec.h:1184
#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:188
int video_delay
Video only.
Definition: avcodec.h:3995
#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:3896
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:1156
const AVCodecTag ff_nut_audio_tags[]
Definition: nut.c:213
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:481
#define FFMAX(a, b)
Definition: common.h:94
#define fail()
Definition: checkasm.h:117
Definition: nut.h:44
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1451
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3918
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:638
static int nut_read_close(AVFormatContext *s)
Definition: nutdec.c:793
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
static struct @303 state
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:626
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:245
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 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:600
int16_t pts_delta
Definition: nut.h:70
static int find_and_decode_index(NUTContext *nut)
Definition: nutdec.c:679
int64_t ff_lsb2full(StreamContext *stream, int64_t lsb)
Definition: nut.c:256
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
int n
Definition: avisynth_c.h:684
AVDictionary * metadata
Definition: avformat.h:938
int dummy
Definition: motion.c:64
static int nut_read_header(AVFormatContext *s)
Definition: nutdec.c:807
#define INDEX_STARTCODE
Definition: nut.h:32
uint16_t size_mul
Definition: nut.h:68
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:548
static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr)
Definition: nutdec.c:623
Stream structure.
Definition: avformat.h:874
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:1167
static int read_sm_data(AVFormatContext *s, AVIOContext *bc, AVPacket *pkt, int is_meta, int64_t maxpos)
Definition: nutdec.c:880
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:934
int max_pts_distance
Definition: nut.h:82
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:598
Definition: nut.h:54
Data found in BlockAdditional element of matroska container.
Definition: avcodec.h:1303
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:2280
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:1268
byte swapping routines
cl_device_type type
unsigned long ffio_get_checksum(AVIOContext *s)
Definition: aviobuf.c:618
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:1241
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:2242
#define INFO_STARTCODE
Definition: nut.h:33
static int64_t pts
#define flags(name, subs,...)
Definition: cbs_av1.c:610
int version
Definition: nut.h:116
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:667
int sample_rate
Audio only.
Definition: avcodec.h:4010
const Dispositions ff_nut_dispositions[]
Definition: nut.c:314
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:753
uint64_t next_startcode
Definition: nut.h:99
#define flag(name)
Definition: cbs_av1.c:602
static int decode_info_header(NUTContext *nut)
Definition: nutdec.c:500
FrameCode frame_code[256]
Definition: nut.h:96
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:927
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:3317
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:275
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:1313
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: avcodec.h:3914
int channels
Audio only.
Definition: avcodec.h:4006
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:1078
#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:1021
#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:3904
const char int length
Definition: avisynth_c.h:768
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:1447
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:903
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:929
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:998
const AVCodecTag *const ff_nut_codec_tags[]
Definition: nut.c:240
This structure stores compressed data.
Definition: avcodec.h:1422
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:777
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:1438
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