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