Libav
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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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/bswap.h"
25 #include "libavutil/dict.h"
26 #include "libavutil/mathematics.h"
27 #include "libavutil/tree.h"
28 #include "avio_internal.h"
29 #include "nut.h"
30 #include "riff.h"
31 
32 #undef NDEBUG
33 #include <assert.h>
34 
35 #define NUT_MAX_STREAMS 256 /* arbitrary sanity check value */
36 
37 static int get_str(AVIOContext *bc, char *string, unsigned int maxlen)
38 {
39  unsigned int len = ffio_read_varlen(bc);
40 
41  if (len && maxlen)
42  avio_read(bc, string, FFMIN(len, maxlen));
43  while (len > maxlen) {
44  avio_r8(bc);
45  len--;
46  }
47 
48  if (maxlen)
49  string[FFMIN(len, maxlen - 1)] = 0;
50 
51  if (maxlen == len)
52  return -1;
53  else
54  return 0;
55 }
56 
57 static int64_t get_s(AVIOContext *bc)
58 {
59  int64_t v = ffio_read_varlen(bc) + 1;
60 
61  if (v & 1)
62  return -(v >> 1);
63  else
64  return (v >> 1);
65 }
66 
67 static uint64_t get_fourcc(AVIOContext *bc)
68 {
69  unsigned int len = ffio_read_varlen(bc);
70 
71  if (len == 2)
72  return avio_rl16(bc);
73  else if (len == 4)
74  return avio_rl32(bc);
75  else
76  return -1;
77 }
78 
79 #ifdef TRACE
80 static inline uint64_t get_v_trace(AVIOContext *bc, const char *file,
81  const char *func, int line)
82 {
83  uint64_t v = ffio_read_varlen(bc);
84 
85  av_log(NULL, AV_LOG_DEBUG, "get_v %5"PRId64" / %"PRIX64" in %s %s:%d\n",
86  v, v, file, func, line);
87  return v;
88 }
89 
90 static inline int64_t get_s_trace(AVIOContext *bc, const char *file,
91  const char *func, int line)
92 {
93  int64_t v = get_s(bc);
94 
95  av_log(NULL, AV_LOG_DEBUG, "get_s %5"PRId64" / %"PRIX64" in %s %s:%d\n",
96  v, v, file, func, line);
97  return v;
98 }
99 
100 #define ffio_read_varlen(bc) get_v_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
101 #define get_s(bc) get_s_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
102 #endif
103 
105  int calculate_checksum, uint64_t startcode)
106 {
107  int64_t size;
108 // start = avio_tell(bc) - 8;
109 
110  startcode = av_be2ne64(startcode);
111  startcode = ff_crc04C11DB7_update(0, (uint8_t*) &startcode, 8);
112 
114  size = ffio_read_varlen(bc);
115  if (size > 4096)
116  avio_rb32(bc);
117  if (ffio_get_checksum(bc) && size > 4096)
118  return -1;
119 
120  ffio_init_checksum(bc, calculate_checksum ? ff_crc04C11DB7_update : NULL, 0);
121 
122  return size;
123 }
124 
125 static uint64_t find_any_startcode(AVIOContext *bc, int64_t pos)
126 {
127  uint64_t state = 0;
128 
129  if (pos >= 0)
130  /* Note, this may fail if the stream is not seekable, but that should
131  * not matter, as in this case we simply start where we currently are */
132  avio_seek(bc, pos, SEEK_SET);
133  while (!bc->eof_reached) {
134  state = (state << 8) | avio_r8(bc);
135  if ((state >> 56) != 'N')
136  continue;
137  switch (state) {
138  case MAIN_STARTCODE:
139  case STREAM_STARTCODE:
140  case SYNCPOINT_STARTCODE:
141  case INFO_STARTCODE:
142  case INDEX_STARTCODE:
143  return state;
144  }
145  }
146 
147  return 0;
148 }
149 
156 static int64_t find_startcode(AVIOContext *bc, uint64_t code, int64_t pos)
157 {
158  for (;;) {
159  uint64_t startcode = find_any_startcode(bc, pos);
160  if (startcode == code)
161  return avio_tell(bc) - 8;
162  else if (startcode == 0)
163  return -1;
164  pos = -1;
165  }
166 }
167 
168 static int nut_probe(AVProbeData *p)
169 {
170  int i;
171  uint64_t code = 0;
172 
173  for (i = 0; i < p->buf_size; i++) {
174  code = (code << 8) | p->buf[i];
175  if (code == MAIN_STARTCODE)
176  return AVPROBE_SCORE_MAX;
177  }
178  return 0;
179 }
180 
181 #define GET_V(dst, check) \
182  do { \
183  tmp = ffio_read_varlen(bc); \
184  if (!(check)) { \
185  av_log(s, AV_LOG_ERROR, "Error " #dst " is (%"PRId64")\n", tmp); \
186  return AVERROR_INVALIDDATA; \
187  } \
188  dst = tmp; \
189  } while (0)
190 
191 static int skip_reserved(AVIOContext *bc, int64_t pos)
192 {
193  pos -= avio_tell(bc);
194  if (pos < 0) {
195  avio_seek(bc, pos, SEEK_CUR);
196  return AVERROR_INVALIDDATA;
197  } else {
198  while (pos--)
199  avio_r8(bc);
200  return 0;
201  }
202 }
203 
205 {
206  AVFormatContext *s = nut->avf;
207  AVIOContext *bc = s->pb;
208  uint64_t tmp, end;
209  unsigned int stream_count;
210  int i, j, count;
211  int tmp_stream, tmp_mul, tmp_pts, tmp_size, tmp_res, tmp_head_idx;
212 
213  end = get_packetheader(nut, bc, 1, MAIN_STARTCODE);
214  end += avio_tell(bc);
215 
216  nut->version = ffio_read_varlen(bc);
217  if (nut->version < NUT_MIN_VERSION &&
218  nut->version > NUT_MAX_VERSION) {
219  av_log(s, AV_LOG_ERROR, "Version %d not supported.\n",
220  nut->version);
221  return AVERROR(ENOSYS);
222  }
223 
224  GET_V(stream_count, tmp > 0 && tmp <= NUT_MAX_STREAMS);
225 
226  nut->max_distance = ffio_read_varlen(bc);
227  if (nut->max_distance > 65536) {
228  av_log(s, AV_LOG_DEBUG, "max_distance %d\n", nut->max_distance);
229  nut->max_distance = 65536;
230  }
231 
232  GET_V(nut->time_base_count, tmp > 0 && tmp < INT_MAX / sizeof(AVRational));
233  nut->time_base = av_malloc(nut->time_base_count * sizeof(AVRational));
234 
235  for (i = 0; i < nut->time_base_count; i++) {
236  GET_V(nut->time_base[i].num, tmp > 0 && tmp < (1ULL << 31));
237  GET_V(nut->time_base[i].den, tmp > 0 && tmp < (1ULL << 31));
238  if (av_gcd(nut->time_base[i].num, nut->time_base[i].den) != 1) {
239  av_log(s, AV_LOG_ERROR, "time base invalid\n");
240  return AVERROR_INVALIDDATA;
241  }
242  }
243  tmp_pts = 0;
244  tmp_mul = 1;
245  tmp_stream = 0;
246  tmp_head_idx = 0;
247  for (i = 0; i < 256;) {
248  int tmp_flags = ffio_read_varlen(bc);
249  int tmp_fields = ffio_read_varlen(bc);
250 
251  if (tmp_fields > 0)
252  tmp_pts = get_s(bc);
253  if (tmp_fields > 1)
254  tmp_mul = ffio_read_varlen(bc);
255  if (tmp_fields > 2)
256  tmp_stream = ffio_read_varlen(bc);
257  if (tmp_fields > 3)
258  tmp_size = ffio_read_varlen(bc);
259  else
260  tmp_size = 0;
261  if (tmp_fields > 4)
262  tmp_res = ffio_read_varlen(bc);
263  else
264  tmp_res = 0;
265  if (tmp_fields > 5)
266  count = ffio_read_varlen(bc);
267  else
268  count = tmp_mul - tmp_size;
269  if (tmp_fields > 6)
270  get_s(bc);
271  if (tmp_fields > 7)
272  tmp_head_idx = ffio_read_varlen(bc);
273 
274  while (tmp_fields-- > 8)
275  ffio_read_varlen(bc);
276 
277  if (count == 0 || i + count > 256) {
278  av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i);
279  return AVERROR_INVALIDDATA;
280  }
281  if (tmp_stream >= stream_count) {
282  av_log(s, AV_LOG_ERROR, "illegal stream number\n");
283  return AVERROR_INVALIDDATA;
284  }
285 
286  for (j = 0; j < count; j++, i++) {
287  if (i == 'N') {
288  nut->frame_code[i].flags = FLAG_INVALID;
289  j--;
290  continue;
291  }
292  nut->frame_code[i].flags = tmp_flags;
293  nut->frame_code[i].pts_delta = tmp_pts;
294  nut->frame_code[i].stream_id = tmp_stream;
295  nut->frame_code[i].size_mul = tmp_mul;
296  nut->frame_code[i].size_lsb = tmp_size + j;
297  nut->frame_code[i].reserved_count = tmp_res;
298  nut->frame_code[i].header_idx = tmp_head_idx;
299  }
300  }
301  assert(nut->frame_code['N'].flags == FLAG_INVALID);
302 
303  if (end > avio_tell(bc) + 4) {
304  int rem = 1024;
305  GET_V(nut->header_count, tmp < 128U);
306  nut->header_count++;
307  for (i = 1; i < nut->header_count; i++) {
308  uint8_t *hdr;
309  GET_V(nut->header_len[i], tmp > 0 && tmp < 256);
310  rem -= nut->header_len[i];
311  if (rem < 0) {
312  av_log(s, AV_LOG_ERROR, "invalid elision header\n");
313  return AVERROR_INVALIDDATA;
314  }
315  hdr = av_malloc(nut->header_len[i]);
316  if (!hdr)
317  return AVERROR(ENOMEM);
318  avio_read(bc, hdr, nut->header_len[i]);
319  nut->header[i] = hdr;
320  }
321  assert(nut->header_len[0] == 0);
322  }
323 
324  // flags had been effectively introduced in version 4
325  if (nut->version > NUT_STABLE_VERSION) {
326  nut->flags = ffio_read_varlen(bc);
327  }
328 
329  if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
330  av_log(s, AV_LOG_ERROR, "main header checksum mismatch\n");
331  return AVERROR_INVALIDDATA;
332  }
333 
334  nut->stream = av_mallocz(sizeof(StreamContext) * stream_count);
335  for (i = 0; i < stream_count; i++)
337 
338  return 0;
339 }
340 
342 {
343  AVFormatContext *s = nut->avf;
344  AVIOContext *bc = s->pb;
345  StreamContext *stc;
346  int class, stream_id;
347  uint64_t tmp, end;
348  AVStream *st;
349 
350  end = get_packetheader(nut, bc, 1, STREAM_STARTCODE);
351  end += avio_tell(bc);
352 
353  GET_V(stream_id, tmp < s->nb_streams && !nut->stream[tmp].time_base);
354  stc = &nut->stream[stream_id];
355  st = s->streams[stream_id];
356  if (!st)
357  return AVERROR(ENOMEM);
358 
359  class = ffio_read_varlen(bc);
360  tmp = get_fourcc(bc);
361  st->codec->codec_tag = tmp;
362  switch (class) {
363  case 0:
365  st->codec->codec_id = av_codec_get_id((const AVCodecTag * const []) {
368  0
369  },
370  tmp);
371  break;
372  case 1:
374  st->codec->codec_id = av_codec_get_id((const AVCodecTag * const []) {
377  0
378  },
379  tmp);
380  break;
381  case 2:
384  break;
385  case 3:
388  break;
389  default:
390  av_log(s, AV_LOG_ERROR, "unknown stream class (%d)\n", class);
391  return AVERROR(ENOSYS);
392  }
393  if (class < 3 && st->codec->codec_id == AV_CODEC_ID_NONE)
394  av_log(s, AV_LOG_ERROR,
395  "Unknown codec tag '0x%04x' for stream number %d\n",
396  (unsigned int) tmp, stream_id);
397 
398  GET_V(stc->time_base_id, tmp < nut->time_base_count);
399  GET_V(stc->msb_pts_shift, tmp < 16);
401  GET_V(stc->decode_delay, tmp < 1000); // sanity limit, raise this if Moore's law is true
402  st->codec->has_b_frames = stc->decode_delay;
403  ffio_read_varlen(bc); // stream flags
404 
405  GET_V(st->codec->extradata_size, tmp < (1 << 30));
406  if (st->codec->extradata_size) {
409  avio_read(bc, st->codec->extradata, st->codec->extradata_size);
410  }
411 
412  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
413  GET_V(st->codec->width, tmp > 0);
414  GET_V(st->codec->height, tmp > 0);
417  if ((!st->sample_aspect_ratio.num) != (!st->sample_aspect_ratio.den)) {
418  av_log(s, AV_LOG_ERROR, "invalid aspect ratio %d/%d\n",
420  return AVERROR_INVALIDDATA;
421  }
422  ffio_read_varlen(bc); /* csp type */
423  } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
424  GET_V(st->codec->sample_rate, tmp > 0);
425  ffio_read_varlen(bc); // samplerate_den
426  GET_V(st->codec->channels, tmp > 0);
427  }
428  if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
429  av_log(s, AV_LOG_ERROR,
430  "stream header %d checksum mismatch\n", stream_id);
431  return AVERROR_INVALIDDATA;
432  }
433  stc->time_base = &nut->time_base[stc->time_base_id];
434  avpriv_set_pts_info(s->streams[stream_id], 63, stc->time_base->num,
435  stc->time_base->den);
436  return 0;
437 }
438 
439 static void set_disposition_bits(AVFormatContext *avf, char *value,
440  int stream_id)
441 {
442  int flag = 0, i;
443 
444  for (i = 0; ff_nut_dispositions[i].flag; ++i)
445  if (!strcmp(ff_nut_dispositions[i].str, value))
446  flag = ff_nut_dispositions[i].flag;
447  if (!flag)
448  av_log(avf, AV_LOG_INFO, "unknown disposition type '%s'\n", value);
449  for (i = 0; i < avf->nb_streams; ++i)
450  if (stream_id == i || stream_id == -1)
451  avf->streams[i]->disposition |= flag;
452 }
453 
455 {
456  AVFormatContext *s = nut->avf;
457  AVIOContext *bc = s->pb;
458  uint64_t tmp, chapter_start, chapter_len;
459  unsigned int stream_id_plus1, count;
460  int chapter_id, i;
461  int64_t value, end;
462  char name[256], str_value[1024], type_str[256];
463  const char *type;
464  int *event_flags;
465  AVChapter *chapter = NULL;
466  AVStream *st = NULL;
467  AVDictionary **metadata = NULL;
468  int metadata_flag = 0;
469 
470  end = get_packetheader(nut, bc, 1, INFO_STARTCODE);
471  end += avio_tell(bc);
472 
473  GET_V(stream_id_plus1, tmp <= s->nb_streams);
474  chapter_id = get_s(bc);
475  chapter_start = ffio_read_varlen(bc);
476  chapter_len = ffio_read_varlen(bc);
477  count = ffio_read_varlen(bc);
478 
479  if (chapter_id && !stream_id_plus1) {
480  int64_t start = chapter_start / nut->time_base_count;
481  chapter = avpriv_new_chapter(s, chapter_id,
482  nut->time_base[chapter_start %
483  nut->time_base_count],
484  start, start + chapter_len, NULL);
485  metadata = &chapter->metadata;
486  } else if (stream_id_plus1) {
487  st = s->streams[stream_id_plus1 - 1];
488  metadata = &st->metadata;
489  event_flags = &st->event_flags;
490  metadata_flag = AVSTREAM_EVENT_FLAG_METADATA_UPDATED;
491  } else {
492  metadata = &s->metadata;
493  event_flags = &s->event_flags;
494  metadata_flag = AVFMT_EVENT_FLAG_METADATA_UPDATED;
495  }
496 
497  for (i = 0; i < count; i++) {
498  get_str(bc, name, sizeof(name));
499  value = get_s(bc);
500  if (value == -1) {
501  type = "UTF-8";
502  get_str(bc, str_value, sizeof(str_value));
503  } else if (value == -2) {
504  get_str(bc, type_str, sizeof(type_str));
505  type = type_str;
506  get_str(bc, str_value, sizeof(str_value));
507  } else if (value == -3) {
508  type = "s";
509  value = get_s(bc);
510  } else if (value == -4) {
511  type = "t";
512  value = ffio_read_varlen(bc);
513  } else if (value < -4) {
514  type = "r";
515  get_s(bc);
516  } else {
517  type = "v";
518  }
519 
520  if (stream_id_plus1 > s->nb_streams) {
521  av_log(s, AV_LOG_ERROR, "invalid stream id for info packet\n");
522  continue;
523  }
524 
525  if (!strcmp(type, "UTF-8")) {
526  if (chapter_id == 0 && !strcmp(name, "Disposition")) {
527  set_disposition_bits(s, str_value, stream_id_plus1 - 1);
528  continue;
529  }
530  if (metadata && av_strcasecmp(name, "Uses") &&
531  av_strcasecmp(name, "Depends") && av_strcasecmp(name, "Replaces")) {
532  *event_flags |= metadata_flag;
533  av_dict_set(metadata, name, str_value, 0);
534  }
535  }
536  }
537 
538  if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
539  av_log(s, AV_LOG_ERROR, "info header checksum mismatch\n");
540  return AVERROR_INVALIDDATA;
541  }
542  return 0;
543 }
544 
545 static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr)
546 {
547  AVFormatContext *s = nut->avf;
548  AVIOContext *bc = s->pb;
549  int64_t end, tmp;
550  int ret;
551 
552  nut->last_syncpoint_pos = avio_tell(bc) - 8;
553 
554  end = get_packetheader(nut, bc, 1, SYNCPOINT_STARTCODE);
555  end += avio_tell(bc);
556 
557  tmp = ffio_read_varlen(bc);
558  *back_ptr = nut->last_syncpoint_pos - 16 * ffio_read_varlen(bc);
559  if (*back_ptr < 0)
560  return -1;
561 
562  ff_nut_reset_ts(nut, nut->time_base[tmp % nut->time_base_count],
563  tmp / nut->time_base_count);
564 
565  if (nut->flags & NUT_BROADCAST) {
566  tmp = ffio_read_varlen(bc);
567  av_log(s, AV_LOG_VERBOSE, "Syncpoint wallclock %"PRId64"\n",
568  av_rescale_q(tmp / nut->time_base_count,
569  nut->time_base[tmp % nut->time_base_count],
570  AV_TIME_BASE_Q));
571  }
572 
573  if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
574  av_log(s, AV_LOG_ERROR, "sync point checksum mismatch\n");
575  return AVERROR_INVALIDDATA;
576  }
577 
578  *ts = tmp / s->nb_streams *
579  av_q2d(nut->time_base[tmp % s->nb_streams]) * AV_TIME_BASE;
580 
581  if ((ret = ff_nut_add_sp(nut, nut->last_syncpoint_pos, *back_ptr, *ts)) < 0)
582  return ret;
583 
584  return 0;
585 }
586 
588 {
589  AVFormatContext *s = nut->avf;
590  AVIOContext *bc = s->pb;
591  uint64_t tmp, end;
592  int i, j, syncpoint_count;
593  int64_t filesize = avio_size(bc);
594  int64_t *syncpoints;
595  int8_t *has_keyframe;
596  int ret = AVERROR_INVALIDDATA;
597 
598  avio_seek(bc, filesize - 12, SEEK_SET);
599  avio_seek(bc, filesize - avio_rb64(bc), SEEK_SET);
600  if (avio_rb64(bc) != INDEX_STARTCODE) {
601  av_log(s, AV_LOG_ERROR, "no index at the end\n");
602  return ret;
603  }
604 
605  end = get_packetheader(nut, bc, 1, INDEX_STARTCODE);
606  end += avio_tell(bc);
607 
608  ffio_read_varlen(bc); // max_pts
609  GET_V(syncpoint_count, tmp < INT_MAX / 8 && tmp > 0);
610  syncpoints = av_malloc(sizeof(int64_t) * syncpoint_count);
611  has_keyframe = av_malloc(sizeof(int8_t) * (syncpoint_count + 1));
612  for (i = 0; i < syncpoint_count; i++) {
613  syncpoints[i] = ffio_read_varlen(bc);
614  if (syncpoints[i] <= 0)
615  goto fail;
616  if (i)
617  syncpoints[i] += syncpoints[i - 1];
618  }
619 
620  for (i = 0; i < s->nb_streams; i++) {
621  int64_t last_pts = -1;
622  for (j = 0; j < syncpoint_count;) {
623  uint64_t x = ffio_read_varlen(bc);
624  int type = x & 1;
625  int n = j;
626  x >>= 1;
627  if (type) {
628  int flag = x & 1;
629  x >>= 1;
630  if (n + x >= syncpoint_count + 1) {
631  av_log(s, AV_LOG_ERROR, "index overflow A\n");
632  goto fail;
633  }
634  while (x--)
635  has_keyframe[n++] = flag;
636  has_keyframe[n++] = !flag;
637  } else {
638  while (x != 1) {
639  if (n >= syncpoint_count + 1) {
640  av_log(s, AV_LOG_ERROR, "index overflow B\n");
641  goto fail;
642  }
643  has_keyframe[n++] = x & 1;
644  x >>= 1;
645  }
646  }
647  if (has_keyframe[0]) {
648  av_log(s, AV_LOG_ERROR, "keyframe before first syncpoint in index\n");
649  goto fail;
650  }
651  assert(n <= syncpoint_count + 1);
652  for (; j < n && j < syncpoint_count; j++) {
653  if (has_keyframe[j]) {
654  uint64_t B, A = ffio_read_varlen(bc);
655  if (!A) {
656  A = ffio_read_varlen(bc);
657  B = ffio_read_varlen(bc);
658  // eor_pts[j][i] = last_pts + A + B
659  } else
660  B = 0;
661  av_add_index_entry(s->streams[i], 16 * syncpoints[j - 1],
662  last_pts + A, 0, 0, AVINDEX_KEYFRAME);
663  last_pts += A + B;
664  }
665  }
666  }
667  }
668 
669  if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
670  av_log(s, AV_LOG_ERROR, "index checksum mismatch\n");
671  goto fail;
672  }
673  ret = 0;
674 
675 fail:
676  av_free(syncpoints);
677  av_free(has_keyframe);
678  return ret;
679 }
680 
682 {
683  NUTContext *nut = s->priv_data;
684  AVIOContext *bc = s->pb;
685  int64_t pos;
686  int initialized_stream_count;
687 
688  nut->avf = s;
689 
690  /* main header */
691  pos = 0;
692  do {
693  pos = find_startcode(bc, MAIN_STARTCODE, pos) + 1;
694  if (pos < 0 + 1) {
695  av_log(s, AV_LOG_ERROR, "No main startcode found.\n");
696  return AVERROR_INVALIDDATA;
697  }
698  } while (decode_main_header(nut) < 0);
699 
700  /* stream headers */
701  pos = 0;
702  for (initialized_stream_count = 0; initialized_stream_count < s->nb_streams;) {
703  pos = find_startcode(bc, STREAM_STARTCODE, pos) + 1;
704  if (pos < 0 + 1) {
705  av_log(s, AV_LOG_ERROR, "Not all stream headers found.\n");
706  return AVERROR_INVALIDDATA;
707  }
708  if (decode_stream_header(nut) >= 0)
709  initialized_stream_count++;
710  }
711 
712  /* info headers */
713  pos = 0;
714  for (;;) {
715  uint64_t startcode = find_any_startcode(bc, pos);
716  pos = avio_tell(bc);
717 
718  if (startcode == 0) {
719  av_log(s, AV_LOG_ERROR, "EOF before video frames\n");
720  return AVERROR_INVALIDDATA;
721  } else if (startcode == SYNCPOINT_STARTCODE) {
722  nut->next_startcode = startcode;
723  break;
724  } else if (startcode != INFO_STARTCODE) {
725  continue;
726  }
727 
728  decode_info_header(nut);
729  }
730 
731  s->data_offset = pos - 8;
732 
733  if (bc->seekable) {
734  int64_t orig_pos = avio_tell(bc);
736  avio_seek(bc, orig_pos, SEEK_SET);
737  }
738  assert(nut->next_startcode == SYNCPOINT_STARTCODE);
739 
741 
742  return 0;
743 }
744 
745 static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id,
746  uint8_t *header_idx, int frame_code)
747 {
748  AVFormatContext *s = nut->avf;
749  AVIOContext *bc = s->pb;
750  StreamContext *stc;
751  int size, flags, size_mul, pts_delta, i, reserved_count;
752  uint64_t tmp;
753 
754  if (!(nut->flags & NUT_PIPE) &&
755  avio_tell(bc) > nut->last_syncpoint_pos + nut->max_distance) {
756  av_log(s, AV_LOG_ERROR,
757  "Last frame must have been damaged %"PRId64" > %"PRId64" + %d\n",
758  avio_tell(bc), nut->last_syncpoint_pos, nut->max_distance);
759  return AVERROR_INVALIDDATA;
760  }
761 
762  flags = nut->frame_code[frame_code].flags;
763  size_mul = nut->frame_code[frame_code].size_mul;
764  size = nut->frame_code[frame_code].size_lsb;
765  *stream_id = nut->frame_code[frame_code].stream_id;
766  pts_delta = nut->frame_code[frame_code].pts_delta;
767  reserved_count = nut->frame_code[frame_code].reserved_count;
768  *header_idx = nut->frame_code[frame_code].header_idx;
769 
770  if (flags & FLAG_INVALID)
771  return AVERROR_INVALIDDATA;
772  if (flags & FLAG_CODED)
773  flags ^= ffio_read_varlen(bc);
774  if (flags & FLAG_STREAM_ID) {
775  GET_V(*stream_id, tmp < s->nb_streams);
776  }
777  stc = &nut->stream[*stream_id];
778  if (flags & FLAG_CODED_PTS) {
779  int coded_pts = ffio_read_varlen(bc);
780  // FIXME check last_pts validity?
781  if (coded_pts < (1 << stc->msb_pts_shift)) {
782  *pts = ff_lsb2full(stc, coded_pts);
783  } else
784  *pts = coded_pts - (1 << stc->msb_pts_shift);
785  } else
786  *pts = stc->last_pts + pts_delta;
787  if (flags & FLAG_SIZE_MSB)
788  size += size_mul * ffio_read_varlen(bc);
789  if (flags & FLAG_MATCH_TIME)
790  get_s(bc);
791  if (flags & FLAG_HEADER_IDX)
792  *header_idx = ffio_read_varlen(bc);
793  if (flags & FLAG_RESERVED)
794  reserved_count = ffio_read_varlen(bc);
795  for (i = 0; i < reserved_count; i++)
796  ffio_read_varlen(bc);
797 
798  if (*header_idx >= (unsigned)nut->header_count) {
799  av_log(s, AV_LOG_ERROR, "header_idx invalid\n");
800  return AVERROR_INVALIDDATA;
801  }
802  if (size > 4096)
803  *header_idx = 0;
804  size -= nut->header_len[*header_idx];
805 
806  if (flags & FLAG_CHECKSUM) {
807  avio_rb32(bc); // FIXME check this
808  } else if (!(nut->flags & NUT_PIPE) &&
809  size > 2 * nut->max_distance ||
810  FFABS(stc->last_pts - *pts) > stc->max_pts_distance) {
811  av_log(s, AV_LOG_ERROR, "frame size > 2max_distance and no checksum\n");
812  return AVERROR_INVALIDDATA;
813  }
814 
815  stc->last_pts = *pts;
816  stc->last_flags = flags;
817 
818  return size;
819 }
820 
821 static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code)
822 {
823  AVFormatContext *s = nut->avf;
824  AVIOContext *bc = s->pb;
825  int size, stream_id, discard, ret;
826  int64_t pts, last_IP_pts;
827  StreamContext *stc;
828  uint8_t header_idx;
829 
830  size = decode_frame_header(nut, &pts, &stream_id, &header_idx, frame_code);
831  if (size < 0)
832  return size;
833 
834  stc = &nut->stream[stream_id];
835 
836  if (stc->last_flags & FLAG_KEY)
837  stc->skip_until_key_frame = 0;
838 
839  discard = s->streams[stream_id]->discard;
840  last_IP_pts = s->streams[stream_id]->last_IP_pts;
841  if ((discard >= AVDISCARD_NONKEY && !(stc->last_flags & FLAG_KEY)) ||
842  (discard >= AVDISCARD_BIDIR && last_IP_pts != AV_NOPTS_VALUE &&
843  last_IP_pts > pts) ||
844  discard >= AVDISCARD_ALL ||
845  stc->skip_until_key_frame) {
846  avio_skip(bc, size);
847  return 1;
848  }
849 
850  ret = av_new_packet(pkt, size + nut->header_len[header_idx]);
851  if (ret < 0)
852  return ret;
853  memcpy(pkt->data, nut->header[header_idx], nut->header_len[header_idx]);
854  pkt->pos = avio_tell(bc); // FIXME
855  avio_read(bc, pkt->data + nut->header_len[header_idx], size);
856 
857  pkt->stream_index = stream_id;
858  if (stc->last_flags & FLAG_KEY)
859  pkt->flags |= AV_PKT_FLAG_KEY;
860  pkt->pts = pts;
861 
862  return 0;
863 }
864 
866 {
867  NUTContext *nut = s->priv_data;
868  AVIOContext *bc = s->pb;
869  int i, frame_code = 0, ret, skip;
870  int64_t ts, back_ptr;
871 
872  for (;;) {
873  int64_t pos = avio_tell(bc);
874  uint64_t tmp = nut->next_startcode;
875  nut->next_startcode = 0;
876 
877  if (tmp) {
878  pos -= 8;
879  } else {
880  frame_code = avio_r8(bc);
881  if (bc->eof_reached)
882  return AVERROR_EOF;
883  if (frame_code == 'N') {
884  tmp = frame_code;
885  for (i = 1; i < 8; i++)
886  tmp = (tmp << 8) + avio_r8(bc);
887  }
888  }
889  switch (tmp) {
890  case MAIN_STARTCODE:
891  case STREAM_STARTCODE:
892  case INDEX_STARTCODE:
893  skip = get_packetheader(nut, bc, 0, tmp);
894  avio_skip(bc, skip);
895  break;
896  case INFO_STARTCODE:
897  if (decode_info_header(nut) < 0)
898  goto resync;
899  break;
900  case SYNCPOINT_STARTCODE:
901  if (decode_syncpoint(nut, &ts, &back_ptr) < 0)
902  goto resync;
903  frame_code = avio_r8(bc);
904  case 0:
905  ret = decode_frame(nut, pkt, frame_code);
906  if (ret == 0)
907  return 0;
908  else if (ret == 1) // OK but discard packet
909  break;
910  default:
911 resync:
912  av_log(s, AV_LOG_DEBUG, "syncing from %"PRId64"\n", pos);
913  tmp = find_any_startcode(bc, nut->last_syncpoint_pos + 1);
914  if (tmp == 0)
915  return AVERROR_INVALIDDATA;
916  av_log(s, AV_LOG_DEBUG, "sync\n");
917  nut->next_startcode = tmp;
918  }
919  }
920 }
921 
922 static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index,
923  int64_t *pos_arg, int64_t pos_limit)
924 {
925  NUTContext *nut = s->priv_data;
926  AVIOContext *bc = s->pb;
927  int64_t pos, pts, back_ptr;
928  av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%"PRId64",%"PRId64")\n",
929  stream_index, *pos_arg, pos_limit);
930 
931  pos = *pos_arg;
932  do {
933  pos = find_startcode(bc, SYNCPOINT_STARTCODE, pos) + 1;
934  if (pos < 1) {
935  assert(nut->next_startcode == 0);
936  av_log(s, AV_LOG_ERROR, "read_timestamp failed.\n");
937  return AV_NOPTS_VALUE;
938  }
939  } while (decode_syncpoint(nut, &pts, &back_ptr) < 0);
940  *pos_arg = pos - 1;
941  assert(nut->last_syncpoint_pos == *pos_arg);
942 
943  av_log(s, AV_LOG_DEBUG, "return %"PRId64" %"PRId64"\n", pts, back_ptr);
944  if (stream_index == -1)
945  return pts;
946  else if (stream_index == -2)
947  return back_ptr;
948 
949  return AV_NOPTS_VALUE;
950 }
951 
952 static int read_seek(AVFormatContext *s, int stream_index,
953  int64_t pts, int flags)
954 {
955  NUTContext *nut = s->priv_data;
956  AVStream *st = s->streams[stream_index];
957  Syncpoint dummy = { .ts = pts * av_q2d(st->time_base) * AV_TIME_BASE };
958  Syncpoint nopts_sp = { .ts = AV_NOPTS_VALUE, .back_ptr = AV_NOPTS_VALUE };
959  Syncpoint *sp, *next_node[2] = { &nopts_sp, &nopts_sp };
960  int64_t pos, pos2, ts;
961  int i;
962 
963  if (nut->flags & NUT_PIPE) {
964  return AVERROR(ENOSYS);
965  }
966 
967  if (st->index_entries) {
968  int index = av_index_search_timestamp(st, pts, flags);
969  if (index < 0)
970  return -1;
971 
972  pos2 = st->index_entries[index].pos;
973  ts = st->index_entries[index].timestamp;
974  } else {
975  av_tree_find(nut->syncpoints, &dummy, (void *) ff_nut_sp_pts_cmp,
976  (void **) next_node);
977  av_log(s, AV_LOG_DEBUG, "%"PRIu64"-%"PRIu64" %"PRId64"-%"PRId64"\n",
978  next_node[0]->pos, next_node[1]->pos, next_node[0]->ts,
979  next_node[1]->ts);
980  pos = ff_gen_search(s, -1, dummy.ts, next_node[0]->pos,
981  next_node[1]->pos, next_node[1]->pos,
982  next_node[0]->ts, next_node[1]->ts,
984 
985  if (!(flags & AVSEEK_FLAG_BACKWARD)) {
986  dummy.pos = pos + 16;
987  next_node[1] = &nopts_sp;
988  av_tree_find(nut->syncpoints, &dummy, (void *) ff_nut_sp_pos_cmp,
989  (void **) next_node);
990  pos2 = ff_gen_search(s, -2, dummy.pos, next_node[0]->pos,
991  next_node[1]->pos, next_node[1]->pos,
992  next_node[0]->back_ptr, next_node[1]->back_ptr,
993  flags, &ts, nut_read_timestamp);
994  if (pos2 >= 0)
995  pos = pos2;
996  // FIXME dir but I think it does not matter
997  }
998  dummy.pos = pos;
999  sp = av_tree_find(nut->syncpoints, &dummy, (void *) ff_nut_sp_pos_cmp,
1000  NULL);
1001 
1002  assert(sp);
1003  pos2 = sp->back_ptr - 15;
1004  }
1005  av_log(NULL, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos2);
1006  pos = find_startcode(s->pb, SYNCPOINT_STARTCODE, pos2);
1007  avio_seek(s->pb, pos, SEEK_SET);
1008  av_log(NULL, AV_LOG_DEBUG, "SP: %"PRId64"\n", pos);
1009  if (pos2 > pos || pos2 + 15 < pos)
1010  av_log(NULL, AV_LOG_ERROR, "no syncpoint at backptr pos\n");
1011  for (i = 0; i < s->nb_streams; i++)
1012  nut->stream[i].skip_until_key_frame = 1;
1013 
1014  return 0;
1015 }
1016 
1018 {
1019  NUTContext *nut = s->priv_data;
1020  int i;
1021 
1022  av_freep(&nut->time_base);
1023  av_freep(&nut->stream);
1024  ff_nut_free_sp(nut);
1025  for (i = 1; i < nut->header_count; i++)
1026  av_freep(&nut->header[i]);
1027 
1028  return 0;
1029 }
1030 
1032  .name = "nut",
1033  .long_name = NULL_IF_CONFIG_SMALL("NUT"),
1034  .priv_data_size = sizeof(NUTContext),
1035  .read_probe = nut_probe,
1039  .read_seek = read_seek,
1040  .extensions = "nut",
1041  .codec_tag = ff_nut_codec_tags,
1042 };
#define NUT_STABLE_VERSION
Definition: nut.h:40
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:1609
uint8_t header_len[128]
Definition: nut.h:95
uint64_t ffio_read_varlen(AVIOContext *bc)
Definition: aviobuf.c:668
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
discard all frames except keyframes
Definition: avcodec.h:567
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
#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:241
#define AVSTREAM_EVENT_FLAG_METADATA_UPDATED
The call resulted in updated metadata.
Definition: avformat.h:819
int size
int64_t last_syncpoint_pos
Definition: nut.h:102
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:1181
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:1943
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:998
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:2829
int64_t pos
Definition: avformat.h:660
int event_flags
Flags for the user to detect events happening on the stream.
Definition: avformat.h:818
static int get_str(AVIOContext *bc, char *string, unsigned int maxlen)
Definition: nutdec.c:37
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:769
int num
numerator
Definition: rational.h:44
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:878
Definition: nut.h:57
#define NUT_MAX_STREAMS
Definition: nutdec.c:35
int64_t ts
Definition: nut.h:61
int event_flags
Flags for the user to detect events happening on the file.
Definition: avformat.h:1200
static void set_disposition_bits(AVFormatContext *avf, char *value, int stream_id)
Definition: nutdec.c:439
int64_t data_offset
offset of the first packet
Definition: avformat.h:1220
discard all
Definition: avcodec.h:568
Definition: nut.h:89
uint8_t stream_id
Definition: nut.h:66
AVDictionary * metadata
Definition: avformat.h:909
static int decode_main_header(NUTContext *nut)
Definition: nutdec.c:204
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
const uint8_t * header[128]
Definition: nut.h:96
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:2601
Format I/O context.
Definition: avformat.h:922
static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id, uint8_t *header_idx, int frame_code)
Definition: nutdec.c:745
if set, reserved_count is coded in the frame header
Definition: nut.h:50
static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index, int64_t *pos_arg, int64_t pos_limit)
Definition: nutdec.c:922
Public dictionary API.
void * av_tree_find(const AVTreeNode *t, void *key, int(*cmp)(void *key, const void *b), void *next[2])
Definition: tree.c:41
uint8_t
AVRational * time_base
Definition: nut.h:104
Opaque data information usually continuous.
Definition: avutil.h:189
int decode_delay
Definition: nut.h:82
uint16_t flags
Definition: nut.h:65
static int nut_probe(AVProbeData *p)
Definition: nutdec.c:168
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.
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:595
#define NUT_MAX_VERSION
Definition: nut.h:39
if set, coded_pts is in the frame header
Definition: nut.h:46
#define STREAM_STARTCODE
Definition: nut.h:30
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1164
const char * name
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2521
#define NUT_PIPE
Definition: nut.h:107
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
If set, match_time_delta is coded in the frame header.
Definition: nut.h:52
const AVMetadataConv ff_nut_metadata_conv[]
Definition: nut.c:237
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
uint8_t * data
Definition: avcodec.h:973
int last_flags
Definition: nut.h:75
static int flags
Definition: log.c:44
static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code)
Definition: nutdec.c:821
#define AVERROR_EOF
End of file.
Definition: error.h:51
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:139
const AVCodecTag ff_nut_data_tags[]
Definition: nut.c:36
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:660
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
#define B
Definition: huffyuv.h:49
int ff_nut_sp_pos_cmp(const Syncpoint *a, const Syncpoint *b)
Definition: nut.c:182
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:452
AVFormatContext * avf
Definition: nut.h:91
int64_t last_pts
Definition: nut.h:77
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1019
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:129
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:81
#define AVINDEX_KEYFRAME
Definition: avformat.h:662
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1130
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1339
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
void ff_nut_free_sp(NUTContext *nut)
Definition: nut.c:221
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:1222
#define NUT_BROADCAST
Definition: nut.h:106
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:564
discard all bidirectional frames
Definition: avcodec.h:566
#define AVERROR(e)
Definition: error.h:43
uint64_t pos
Definition: nut.h:58
int64_t timestamp
Definition: avformat.h:661
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
Definition: graph2dot.c:49
int64_t av_gcd(int64_t a, int64_t b)
Return the greatest common divisor of a and b.
Definition: mathematics.c:53
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: nutdec.c:865
const AVCodecTag ff_nut_audio_tags[]
Definition: nut.c:131
int header_count
Definition: nut.h:103
AVRational * time_base
Definition: nut.h:79
#define NUT_MIN_VERSION
Definition: nut.h:41
static int decode_stream_header(NUTContext *nut)
Definition: nutdec.c:341
#define av_be2ne64(x)
Definition: bswap.h:96
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:353
if set, frame is keyframe
Definition: nut.h:44
int ff_nut_sp_pts_cmp(const Syncpoint *a, const Syncpoint *b)
Definition: nut.c:187
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:443
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
static int nut_read_close(AVFormatContext *s)
Definition: nutdec.c:1017
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:398
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:397
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:531
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:978
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:431
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:117
void ff_nut_reset_ts(NUTContext *nut, AVRational time_base, int64_t val)
Definition: nut.c:164
int flags
Definition: nut.h:108
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:234
#define FFMIN(a, b)
Definition: common.h:57
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:29
int av_strcasecmp(const char *a, const char *b)
Definition: avstring.c:156
uint8_t header_idx
Definition: nut.h:71
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int width
picture width / height.
Definition: avcodec.h:1224
static uint64_t find_any_startcode(AVIOContext *bc, int64_t pos)
Definition: nutdec.c:125
uint16_t size_lsb
Definition: nut.h:68
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition: aviobuf.c:411
int16_t pts_delta
Definition: nut.h:69
static int find_and_decode_index(NUTContext *nut)
Definition: nutdec.c:587
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
int64_t ff_lsb2full(StreamContext *stream, int64_t lsb)
Definition: nut.c:175
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
if set, frame_code is invalid
Definition: nut.h:54
static uint64_t get_fourcc(AVIOContext *bc)
Definition: nutdec.c:67
static int get_packetheader(NUTContext *nut, AVIOContext *bc, int calculate_checksum, uint64_t startcode)
Definition: nutdec.c:104
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
The call resulted in updated metadata.
Definition: avformat.h:1201
#define FFABS(a)
Definition: common.h:52
struct AVTreeNode * syncpoints
Definition: nut.h:105
if set, data_size_msb is at frame header, otherwise data_size_msb is 0
Definition: nut.h:48
AVDictionary * metadata
Definition: avformat.h:771
static int nut_read_header(AVFormatContext *s)
Definition: nutdec.c:681
if set, the frame header contains a checksum
Definition: nut.h:49
#define INDEX_STARTCODE
Definition: nut.h:32
uint16_t size_mul
Definition: nut.h:67
Definition: vf_drawbox.c:37
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:544
if(ac->has_optimized_func)
static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr)
Definition: nutdec.c:545
Stream structure.
Definition: avformat.h:699
int msb_pts_shift
Definition: nut.h:80
NULL
Definition: eval.c:55
#define AV_LOG_INFO
Standard information.
Definition: log.h:134
enum AVMediaType codec_type
Definition: avcodec.h:1058
const AVCodecTag ff_nut_subtitle_tags[]
Definition: nut.c:28
enum AVCodecID codec_id
Definition: avcodec.h:1067
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:240
int sample_rate
samples per second
Definition: avcodec.h:1791
AVIOContext * pb
I/O context.
Definition: avformat.h:964
int max_pts_distance
Definition: nut.h:81
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1082
if set, coded_flags are stored in the frame header
Definition: nut.h:53
int extradata_size
Definition: avcodec.h:1165
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
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:68
#define GET_V(dst, check)
Definition: nutdec.c:181
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:1295
int index
Definition: gxfenc.c:72
rational number numerator/denominator
Definition: rational.h:43
byte swapping routines
unsigned long ffio_get_checksum(AVIOContext *s)
Definition: aviobuf.c:423
StreamContext * stream
Definition: nut.h:98
static int skip_reserved(AVIOContext *bc, int64_t pos)
Definition: nutdec.c:191
static int64_t find_startcode(AVIOContext *bc, uint64_t code, int64_t pos)
Find the given startcode.
Definition: nutdec.c:156
This structure contains the data a format has to probe a file.
Definition: avformat.h:395
static int read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
Definition: nutdec.c:952
#define INFO_STARTCODE
Definition: nut.h:33
static uint32_t state
Definition: trasher.c:27
int version
Definition: nut.h:109
int skip_until_key_frame
Definition: nut.h:76
const Dispositions ff_nut_dispositions[]
Definition: nut.c:227
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:404
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:548
uint64_t next_startcode
stores the next startcode if it has already been parsed but the stream is not seekable ...
Definition: nut.h:97
static int decode_info_header(NUTContext *nut)
Definition: nutdec.c:454
FrameCode frame_code[256]
Definition: nut.h:94
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:760
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:192
int den
denominator
Definition: rational.h:45
#define SYNCPOINT_STARTCODE
Definition: nut.h:31
int flag
Definition: nut.h:121
int eof_reached
true if eof reached
Definition: avio.h:96
If set, header_idx is coded in the frame header.
Definition: nut.h:51
int len
AVInputFormat ff_nut_demuxer
Definition: nutdec.c:1031
int channels
number of audio channels
Definition: avcodec.h:1792
static int64_t get_s(AVIOContext *bc)
Definition: nutdec.c:57
void * priv_data
Format private data.
Definition: avformat.h:950
int time_base_id
Definition: nut.h:78
int64_t last_IP_pts
Definition: avformat.h:852
if set, stream_id is coded in the frame header
Definition: nut.h:47
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
int stream_index
Definition: avcodec.h:975
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:741
uint64_t back_ptr
Definition: nut.h:59
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:762
const AVCodecTag *const ff_nut_codec_tags[]
Definition: nut.c:159
This structure stores compressed data.
Definition: avcodec.h:950
unsigned int time_base_count
Definition: nut.h:101
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228
uint8_t reserved_count
Definition: nut.h:70
unsigned int max_distance
Definition: nut.h:100
Definition: vf_drawbox.c:37