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