FFmpeg
Loading...
Searching...
No Matches
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
24#include "libavutil/avstring.h"
25#include "libavutil/avassert.h"
26#include "libavutil/bswap.h"
27#include "libavutil/dict.h"
30#include "libavutil/mem.h"
31#include "libavutil/tree.h"
33#include "avio_internal.h"
34#include "demux.h"
35#include "isom.h"
36#include "nut.h"
37#include "riff.h"
38
39static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index,
40 int64_t *pos_arg, int64_t pos_limit);
41
42static int get_str(AVIOContext *bc, char *string, unsigned int maxlen)
43{
44 unsigned int len = ffio_read_varlen(bc);
45
46 if (len && maxlen)
47 avio_read(bc, string, FFMIN(len, maxlen));
48 while (len > maxlen) {
49 avio_r8(bc);
50 len--;
51 if (bc->eof_reached)
52 len = maxlen;
53 }
54
55 if (maxlen)
56 string[FFMIN(len, maxlen - 1)] = 0;
57
58 if (bc->eof_reached)
59 return AVERROR_EOF;
60 if (maxlen == len)
61 return -1;
62 else
63 return 0;
64}
65
67{
68 int64_t v = ffio_read_varlen(bc) + 1;
69
70 if (v & 1)
71 return -(v >> 1);
72 else
73 return (v >> 1);
74}
75
76static uint64_t get_fourcc(AVIOContext *bc)
77{
78 unsigned int len = ffio_read_varlen(bc);
79
80 if (len == 2)
81 return avio_rl16(bc);
82 else if (len == 4)
83 return avio_rl32(bc);
84 else {
85 av_log(NULL, AV_LOG_ERROR, "Unsupported fourcc length %d\n", len);
86 return -1;
87 }
88}
89
91 int calculate_checksum, uint64_t startcode)
92{
94
95 startcode = av_be2ne64(startcode);
96 startcode = ff_crc04C11DB7_update(0, (uint8_t*) &startcode, 8);
97
100 if (size > 4096)
101 avio_rb32(bc);
102 if (ffio_get_checksum(bc) && size > 4096)
103 return -1;
104
105 ffio_init_checksum(bc, calculate_checksum ? ff_crc04C11DB7_update : NULL, 0);
106
107 return size;
108}
109
111{
112 uint64_t state = 0;
113
114 if (pos >= 0)
115 /* Note, this may fail if the stream is not seekable, but that should
116 * not matter, as in this case we simply start where we currently are */
117 avio_seek(bc, pos, SEEK_SET);
118 while (!avio_feof(bc)) {
119 state = (state << 8) | avio_r8(bc);
120 if ((state >> 56) != 'N')
121 continue;
122 switch (state) {
123 case MAIN_STARTCODE:
124 case STREAM_STARTCODE:
126 case INFO_STARTCODE:
127 case INDEX_STARTCODE:
128 return state;
129 }
130 }
131
132 return 0;
133}
134
135/**
136 * Find the given startcode.
137 * @param code the startcode
138 * @param pos the start position of the search, or -1 if the current position
139 * @return the position of the startcode or -1 if not found
140 */
142{
143 for (;;) {
144 uint64_t startcode = find_any_startcode(bc, pos);
145 if (startcode == code)
146 return avio_tell(bc) - 8;
147 else if (startcode == 0)
148 return -1;
149 pos = -1;
150 }
151}
152
153static int nut_probe(const AVProbeData *p)
154{
155 int i;
156
157 for (i = 0; i < p->buf_size-8; i++) {
158 if (AV_RB32(p->buf+i) != MAIN_STARTCODE>>32)
159 continue;
160 if (AV_RB32(p->buf+i+4) == (MAIN_STARTCODE & 0xFFFFFFFF))
161 return AVPROBE_SCORE_MAX;
162 }
163 return 0;
164}
165
166#define GET_V(dst, check) \
167 do { \
168 tmp = ffio_read_varlen(bc); \
169 if (!(check)) { \
170 av_log(s, AV_LOG_ERROR, "Error " #dst " is (%"PRId64")\n", tmp); \
171 ret = AVERROR_INVALIDDATA; \
172 goto fail; \
173 } \
174 dst = tmp; \
175 } while (0)
176
178{
179 pos -= avio_tell(bc);
180 if (pos < 0) {
181 avio_seek(bc, pos, SEEK_CUR);
182 return AVERROR_INVALIDDATA;
183 } else {
184 while (pos--) {
185 if (bc->eof_reached)
186 return AVERROR_INVALIDDATA;
187 avio_r8(bc);
188 }
189 return 0;
190 }
191}
192
194{
195 AVFormatContext *s = nut->avf;
196 AVIOContext *bc = s->pb;
197 uint64_t tmp, end, length;
198 unsigned int stream_count;
199 int i, j, count, ret;
200 int tmp_stream, tmp_mul, tmp_pts, tmp_size, tmp_res, tmp_head_idx;
201
202 length = get_packetheader(nut, bc, 1, MAIN_STARTCODE);
203 if (length == (uint64_t)-1)
204 return AVERROR_INVALIDDATA;
205 end = length + avio_tell(bc);
206
207 nut->version = ffio_read_varlen(bc);
208 if (nut->version < NUT_MIN_VERSION ||
209 nut->version > NUT_MAX_VERSION) {
210 av_log(s, AV_LOG_ERROR, "Version %d not supported.\n",
211 nut->version);
212 return AVERROR(ENOSYS);
213 }
214 if (nut->version > 3)
216
217 GET_V(stream_count, tmp > 0 && tmp <= s->max_streams);
218
220 if (nut->max_distance > 65536) {
221 av_log(s, AV_LOG_DEBUG, "max_distance %d\n", nut->max_distance);
222 nut->max_distance = 65536;
223 }
224
225 GET_V(nut->time_base_count, tmp > 0 && tmp < INT_MAX / sizeof(AVRational) && tmp < length/2);
227 if (!nut->time_base)
228 return AVERROR(ENOMEM);
229
230 for (i = 0; i < nut->time_base_count; i++) {
231 GET_V(nut->time_base[i].num, tmp > 0 && tmp < (1ULL << 31));
232 GET_V(nut->time_base[i].den, tmp > 0 && tmp < (1ULL << 31));
233 if (av_gcd(nut->time_base[i].num, nut->time_base[i].den) != 1) {
234 av_log(s, AV_LOG_ERROR, "invalid time base %d/%d\n",
235 nut->time_base[i].num,
236 nut->time_base[i].den);
238 goto fail;
239 }
240 }
241 tmp_pts = 0;
242 tmp_mul = 1;
243 tmp_stream = 0;
244 tmp_head_idx = 0;
245 for (i = 0; i < 256;) {
246 int tmp_flags = ffio_read_varlen(bc);
247 int tmp_fields = ffio_read_varlen(bc);
248 if (tmp_fields < 0) {
249 av_log(s, AV_LOG_ERROR, "fields %d is invalid\n", tmp_fields);
251 goto fail;
252 }
253
254 if (tmp_fields > 0)
255 tmp_pts = get_s(bc);
256 if (tmp_fields > 1)
257 tmp_mul = ffio_read_varlen(bc);
258 if (tmp_fields > 2)
259 tmp_stream = ffio_read_varlen(bc);
260 if (tmp_fields > 3)
261 tmp_size = ffio_read_varlen(bc);
262 else
263 tmp_size = 0;
264 if (tmp_fields > 4)
265 tmp_res = ffio_read_varlen(bc);
266 else
267 tmp_res = 0;
268 if (tmp_fields > 5)
269 count = ffio_read_varlen(bc);
270 else
271 count = tmp_mul - (unsigned)tmp_size;
272 if (tmp_fields > 6)
273 get_s(bc);
274 if (tmp_fields > 7)
275 tmp_head_idx = ffio_read_varlen(bc);
276
277 while (tmp_fields-- > 8) {
278 if (bc->eof_reached) {
279 av_log(s, AV_LOG_ERROR, "reached EOF while decoding main header\n");
281 goto fail;
282 }
284 }
285
286 if (count <= 0 || count > 256 - (i <= 'N') - i) {
287 av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i);
289 goto fail;
290 }
291 if (tmp_stream >= stream_count) {
292 av_log(s, AV_LOG_ERROR, "illegal stream number %d >= %d\n",
293 tmp_stream, stream_count);
295 goto fail;
296 }
297 if (tmp_size < 0 || tmp_size > INT_MAX - count) {
298 av_log(s, AV_LOG_ERROR, "illegal size\n");
300 goto fail;
301 }
302
303 for (j = 0; j < count; j++, i++) {
304 if (i == 'N') {
306 j--;
307 continue;
308 }
309 nut->frame_code[i].flags = tmp_flags;
310 nut->frame_code[i].pts_delta = tmp_pts;
311 nut->frame_code[i].stream_id = tmp_stream;
312 nut->frame_code[i].size_mul = tmp_mul;
313 nut->frame_code[i].size_lsb = tmp_size + j;
314 nut->frame_code[i].reserved_count = tmp_res;
315 nut->frame_code[i].header_idx = tmp_head_idx;
316 }
317 }
319
320 if (end > avio_tell(bc) + 4) {
321 int rem = 1024;
322 GET_V(nut->header_count, tmp < 128U);
323 nut->header_count++;
324 for (i = 1; i < nut->header_count; i++) {
325 uint8_t *hdr;
326 GET_V(nut->header_len[i], tmp > 0 && tmp < 256);
327 if (rem < nut->header_len[i]) {
329 "invalid elision header %d : %d > %d\n",
330 i, nut->header_len[i], rem);
332 goto fail;
333 }
334 rem -= nut->header_len[i];
335 hdr = av_malloc(nut->header_len[i]);
336 if (!hdr) {
337 ret = AVERROR(ENOMEM);
338 goto fail;
339 }
340 avio_read(bc, hdr, nut->header_len[i]);
341 nut->header[i] = hdr;
342 }
343 av_assert0(nut->header_len[0] == 0);
344 }
345
346 // flags had been effectively introduced in version 4
347 if (nut->version > 3 && end > avio_tell(bc) + 4) {
348 nut->flags = ffio_read_varlen(bc);
349 }
350
351 if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
352 av_log(s, AV_LOG_ERROR, "main header checksum mismatch\n");
354 goto fail;
355 }
356
357 nut->stream = av_calloc(stream_count, sizeof(StreamContext));
358 if (!nut->stream) {
359 ret = AVERROR(ENOMEM);
360 goto fail;
361 }
362 for (i = 0; i < stream_count; i++) {
363 if (!avformat_new_stream(s, NULL)) {
364 ret = AVERROR(ENOMEM);
365 goto fail;
366 }
367 }
368
369 return 0;
370fail:
371 av_freep(&nut->time_base);
372 for (i = 1; i < nut->header_count; i++) {
373 av_freep(&nut->header[i]);
374 }
375 nut->header_count = 0;
376 return ret;
377}
378
380{
381 AVFormatContext *s = nut->avf;
382 AVIOContext *bc = s->pb;
383 StreamContext *stc;
384 int class, stream_id, ret;
385 uint64_t tmp, end;
386 AVStream *st = NULL;
387
388 end = get_packetheader(nut, bc, 1, STREAM_STARTCODE);
389 end += avio_tell(bc);
390
391 GET_V(stream_id, tmp < s->nb_streams && !nut->stream[tmp].time_base);
392 stc = &nut->stream[stream_id];
393 st = s->streams[stream_id];
394 if (!st)
395 return AVERROR(ENOMEM);
396
397 class = ffio_read_varlen(bc);
398 tmp = get_fourcc(bc);
399 st->codecpar->codec_tag = tmp;
400 switch (class) {
401 case 0:
403 st->codecpar->codec_id = av_codec_get_id((const AVCodecTag * const []) {
407 0
408 },
409 tmp);
410 break;
411 case 1:
413 st->codecpar->codec_id = av_codec_get_id((const AVCodecTag * const []) {
417 0
418 },
419 tmp);
420 break;
421 case 2:
424 break;
425 case 3:
428 break;
429 default:
430 av_log(s, AV_LOG_ERROR, "unknown stream class (%d)\n", class);
431 return AVERROR(ENOSYS);
432 }
433 if (class < 3 && st->codecpar->codec_id == AV_CODEC_ID_NONE)
435 "Unknown codec tag '0x%04x' for stream number %d\n",
436 (unsigned int) tmp, stream_id);
437
438 GET_V(stc->time_base_id, tmp < nut->time_base_count);
439 GET_V(stc->msb_pts_shift, tmp < 16);
441 GET_V(stc->decode_delay, tmp < 1000); // sanity limit, raise this if Moore's law is true
443 ffio_read_varlen(bc); // stream flags
444
445 GET_V(st->codecpar->extradata_size, tmp < (1 << 30));
446 if (st->codecpar->extradata_size) {
447 ret = ff_get_extradata(s, st->codecpar, bc,
449 if (ret < 0)
450 return ret;
451 }
452
454 GET_V(st->codecpar->width, tmp > 0);
455 GET_V(st->codecpar->height, tmp > 0);
458 if ((!st->sample_aspect_ratio.num) != (!st->sample_aspect_ratio.den)) {
459 av_log(s, AV_LOG_ERROR, "invalid aspect ratio %d/%d\n",
462 goto fail;
463 }
464 ffio_read_varlen(bc); /* csp type */
465 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
466 GET_V(st->codecpar->sample_rate, tmp > 0);
467 ffio_read_varlen(bc); // samplerate_den
469 }
470 if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
472 "stream header %d checksum mismatch\n", stream_id);
474 goto fail;
475 }
476 stc->time_base = &nut->time_base[stc->time_base_id];
477 avpriv_set_pts_info(s->streams[stream_id], 63, stc->time_base->num,
478 stc->time_base->den);
479 return 0;
480fail:
481 if (st && st->codecpar) {
483 st->codecpar->extradata_size = 0;
484 }
485 return ret;
486}
487
489 int stream_id)
490{
491 int flag = 0, i;
492
493 for (i = 0; ff_nut_dispositions[i].flag; ++i)
494 if (!strcmp(ff_nut_dispositions[i].str, value))
495 flag = ff_nut_dispositions[i].flag;
496 if (!flag)
497 av_log(avf, AV_LOG_INFO, "unknown disposition type '%s'\n", value);
498 for (i = 0; i < avf->nb_streams; ++i)
499 if (stream_id == i || stream_id == -1)
500 avf->streams[i]->disposition |= flag;
501}
502
504{
505 AVFormatContext *s = nut->avf;
506 AVIOContext *bc = s->pb;
507 uint64_t tmp, chapter_start, chapter_len;
508 unsigned int stream_id_plus1, count;
509 int i, ret = 0;
510 int64_t chapter_id, value, end;
511 char name[256], str_value[1024], type_str[256];
512 const char *type;
513 int *event_flags = NULL;
514 AVChapter *chapter = NULL;
515 AVStream *st = NULL;
517 int metadata_flag = 0;
518
519 end = get_packetheader(nut, bc, 1, INFO_STARTCODE);
520 end += avio_tell(bc);
521
522 GET_V(stream_id_plus1, tmp <= s->nb_streams);
523 chapter_id = get_s(bc);
524 chapter_start = ffio_read_varlen(bc);
525 chapter_len = ffio_read_varlen(bc);
526 count = ffio_read_varlen(bc);
527
528 if (chapter_id && !stream_id_plus1) {
529 int64_t start = chapter_start / nut->time_base_count;
530 chapter = avpriv_new_chapter(s, chapter_id,
531 nut->time_base[chapter_start %
532 nut->time_base_count],
533 start, start + chapter_len, NULL);
534 if (!chapter) {
535 av_log(s, AV_LOG_ERROR, "Could not create chapter.\n");
536 return AVERROR(ENOMEM);
537 }
538 metadata = &chapter->metadata;
539 } else if (stream_id_plus1) {
540 st = s->streams[stream_id_plus1 - 1];
541 metadata = &st->metadata;
542 event_flags = &st->event_flags;
544 } else {
545 metadata = &s->metadata;
546 event_flags = &s->event_flags;
547 metadata_flag = AVFMT_EVENT_FLAG_METADATA_UPDATED;
548 }
549
550 for (i = 0; i < count; i++) {
551 ret = get_str(bc, name, sizeof(name));
552 if (ret < 0) {
553 av_log(s, AV_LOG_ERROR, "get_str failed while decoding info header\n");
554 return ret;
555 }
556 value = get_s(bc);
557 str_value[0] = 0;
558
559 if (value == -1) {
560 type = "UTF-8";
561 ret = get_str(bc, str_value, sizeof(str_value));
562 } else if (value == -2) {
563 ret = get_str(bc, type_str, sizeof(type_str));
564 if (ret < 0) {
565 av_log(s, AV_LOG_ERROR, "get_str failed while decoding info header\n");
566 return ret;
567 }
568 type = type_str;
569 ret = get_str(bc, str_value, sizeof(str_value));
570 } else if (value == -3) {
571 type = "s";
572 value = get_s(bc);
573 } else if (value == -4) {
574 type = "t";
576 } else if (value < -4) {
577 type = "r";
578 get_s(bc);
579 } else {
580 type = "v";
581 }
582
583 if (ret < 0) {
584 av_log(s, AV_LOG_ERROR, "get_str failed while decoding info header\n");
585 return ret;
586 }
587
588 if (stream_id_plus1 > s->nb_streams) {
590 "invalid stream id %d for info packet\n",
591 stream_id_plus1);
592 continue;
593 }
594
595 if (!strcmp(type, "UTF-8")) {
596 if (chapter_id == 0 && !strcmp(name, "Disposition")) {
597 set_disposition_bits(s, str_value, stream_id_plus1 - 1);
598 continue;
599 }
600
601 if (stream_id_plus1 && !strcmp(name, "r_frame_rate")) {
602 sscanf(str_value, "%d/%d", &st->r_frame_rate.num, &st->r_frame_rate.den);
603 if (st->r_frame_rate.num >= 1000LL*st->r_frame_rate.den ||
604 st->r_frame_rate.num < 0 || st->r_frame_rate.den < 0)
605 st->r_frame_rate.num = st->r_frame_rate.den = 0;
606 continue;
607 }
608
609 if (metadata && av_strcasecmp(name, "Uses") &&
610 av_strcasecmp(name, "Depends") && av_strcasecmp(name, "Replaces")) {
611 if (event_flags)
612 *event_flags |= metadata_flag;
613 av_dict_set(metadata, name, str_value, 0);
614 }
615 }
616 }
617
618 if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
619 av_log(s, AV_LOG_ERROR, "info header checksum mismatch\n");
620 return AVERROR_INVALIDDATA;
621 }
622fail:
623 return FFMIN(ret, 0);
624}
625
626static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr)
627{
628 AVFormatContext *s = nut->avf;
629 AVIOContext *bc = s->pb;
630 int64_t end;
631 uint64_t tmp;
632 int ret;
633
634 nut->last_syncpoint_pos = avio_tell(bc) - 8;
635
636 end = get_packetheader(nut, bc, 1, SYNCPOINT_STARTCODE);
637 end += avio_tell(bc);
638
639 tmp = ffio_read_varlen(bc);
640 *back_ptr = nut->last_syncpoint_pos - 16 * ffio_read_varlen(bc);
641 if (*back_ptr < 0)
642 return AVERROR_INVALIDDATA;
643
645 tmp / nut->time_base_count);
646
647 if (nut->flags & NUT_BROADCAST) {
648 tmp = ffio_read_varlen(bc);
649 av_log(s, AV_LOG_VERBOSE, "Syncpoint wallclock %"PRId64"\n",
651 nut->time_base[tmp % nut->time_base_count],
653 }
654
655 if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
656 av_log(s, AV_LOG_ERROR, "sync point checksum mismatch\n");
657 return AVERROR_INVALIDDATA;
658 }
659
660 *ts = tmp / nut->time_base_count *
662
663 if ((ret = ff_nut_add_sp(nut, nut->last_syncpoint_pos, *back_ptr, *ts)) < 0)
664 return ret;
665
666 return 0;
667}
668
669//FIXME calculate exactly, this is just a good approximation.
671{
672 AVFormatContext *s = nut->avf;
673 int64_t duration = 0;
674
676
677 if(duration > 0)
678 s->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
679 return duration;
680}
681
683{
684 AVFormatContext *s = nut->avf;
685 AVIOContext *bc = s->pb;
686 uint64_t tmp, end;
687 int i, j, syncpoint_count;
689 int64_t *syncpoints = NULL;
690 uint64_t max_pts;
691 int8_t *has_keyframe = NULL;
692 int ret = AVERROR_INVALIDDATA;
693
694 if(filesize <= 0)
695 return -1;
696
697 avio_seek(bc, filesize - 12, SEEK_SET);
698 avio_seek(bc, filesize - avio_rb64(bc), SEEK_SET);
699 if (avio_rb64(bc) != INDEX_STARTCODE) {
700 av_log(s, AV_LOG_WARNING, "no index at the end\n");
701
702 if(s->duration<=0)
703 s->duration = find_duration(nut, filesize);
704 return ret;
705 }
706
707 end = get_packetheader(nut, bc, 1, INDEX_STARTCODE);
708 end += avio_tell(bc);
709
710 max_pts = ffio_read_varlen(bc);
711 s->duration = av_rescale_q(max_pts / nut->time_base_count,
712 nut->time_base[max_pts % nut->time_base_count],
714 s->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
715
716 GET_V(syncpoint_count, tmp < INT_MAX / 8 && tmp > 0);
717 syncpoints = av_malloc_array(syncpoint_count, sizeof(int64_t));
718 has_keyframe = av_malloc_array(syncpoint_count + 1, sizeof(int8_t));
719 if (!syncpoints || !has_keyframe) {
720 ret = AVERROR(ENOMEM);
721 goto fail;
722 }
723 for (i = 0; i < syncpoint_count; i++) {
724 syncpoints[i] = ffio_read_varlen(bc);
725 if (syncpoints[i] <= 0)
726 goto fail;
727 if (i)
728 syncpoints[i] += syncpoints[i - 1];
729 }
730
731 for (i = 0; i < s->nb_streams; i++) {
732 int64_t last_pts = -1;
733 for (j = 0; j < syncpoint_count;) {
734 uint64_t x = ffio_read_varlen(bc);
735 int type = x & 1;
736 int n = j;
737 x >>= 1;
738 if (type) {
739 int flag = x & 1;
740 x >>= 1;
741 if (n + x >= syncpoint_count + 1) {
742 av_log(s, AV_LOG_ERROR, "index overflow A %d + %"PRIu64" >= %d\n", n, x, syncpoint_count + 1);
743 goto fail;
744 }
745 while (x--)
746 has_keyframe[n++] = flag;
747 has_keyframe[n++] = !flag;
748 } else {
749 if (x <= 1) {
750 av_log(s, AV_LOG_ERROR, "index: x %"PRIu64" is invalid\n", x);
751 goto fail;
752 }
753 while (x != 1) {
754 if (n >= syncpoint_count + 1) {
755 av_log(s, AV_LOG_ERROR, "index overflow B\n");
756 goto fail;
757 }
758 has_keyframe[n++] = x & 1;
759 x >>= 1;
760 }
761 }
762 if (has_keyframe[0]) {
763 av_log(s, AV_LOG_ERROR, "keyframe before first syncpoint in index\n");
764 goto fail;
765 }
766 av_assert0(n <= syncpoint_count + 1);
767 for (; j < n && j < syncpoint_count; j++) {
768 if (has_keyframe[j]) {
769 uint64_t B, A = ffio_read_varlen(bc);
770 if (!A) {
771 A = ffio_read_varlen(bc);
772 B = ffio_read_varlen(bc);
773 // eor_pts[j][i] = last_pts + A + B
774 } else
775 B = 0;
776 av_add_index_entry(s->streams[i], 16 * syncpoints[j - 1],
777 last_pts + A, 0, 0, AVINDEX_KEYFRAME);
778 last_pts += A + B;
779 }
780 }
781 }
782 }
783
784 if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
785 av_log(s, AV_LOG_ERROR, "index checksum mismatch\n");
786 goto fail;
787 }
788 ret = 0;
789
790fail:
791 av_free(syncpoints);
792 av_free(has_keyframe);
793 return ret;
794}
795
797{
798 NUTContext *nut = s->priv_data;
799 int i;
800
801 av_freep(&nut->time_base);
802 av_freep(&nut->stream);
803 ff_nut_free_sp(nut);
804 for (i = 1; i < nut->header_count; i++)
805 av_freep(&nut->header[i]);
806
807 return 0;
808}
809
811{
812 NUTContext *nut = s->priv_data;
813 AVIOContext *bc = s->pb;
814 int64_t pos;
815 int initialized_stream_count, ret;
816
817 nut->avf = s;
818
819 /* main header */
820 pos = 0;
821 ret = 0;
822 do {
823 if (ret == AVERROR(ENOMEM))
824 return ret;
825
827 if (pos < 0 + 1) {
828 av_log(s, AV_LOG_ERROR, "No main startcode found.\n");
829 return AVERROR_INVALIDDATA;
830 }
831 } while ((ret = decode_main_header(nut)) < 0);
832
833 /* stream headers */
834 pos = 0;
835 for (initialized_stream_count = 0; initialized_stream_count < s->nb_streams;) {
837 if (pos < 0 + 1) {
838 av_log(s, AV_LOG_ERROR, "Not all stream headers found.\n");
839 return AVERROR_INVALIDDATA;
840 }
841 if (decode_stream_header(nut) >= 0)
842 initialized_stream_count++;
843 }
844
845 /* info headers */
846 pos = 0;
847 for (;;) {
848 uint64_t startcode = find_any_startcode(bc, pos);
849 pos = avio_tell(bc);
850
851 if (startcode == 0) {
852 av_log(s, AV_LOG_ERROR, "EOF before video frames\n");
853 return AVERROR_INVALIDDATA;
854 } else if (startcode == SYNCPOINT_STARTCODE) {
855 nut->next_startcode = startcode;
856 break;
857 } else if (startcode != INFO_STARTCODE) {
858 continue;
859 }
860
862 }
863
865
866 if (bc->seekable & AVIO_SEEKABLE_NORMAL) {
867 int64_t orig_pos = avio_tell(bc);
869 avio_seek(bc, orig_pos, SEEK_SET);
870 }
872
874
875 return 0;
876}
877
878static int read_sm_data(AVFormatContext *s, AVIOContext *bc, AVPacket *pkt, int is_meta, int64_t maxpos)
879{
880 int count = ffio_read_varlen(bc);
881 int skip_start = 0;
882 int skip_end = 0;
883 int sample_rate = 0;
884 int width = 0;
885 int height = 0;
886 int i, ret;
887
888 for (i=0; i<count; i++) {
889 uint8_t name[256], str_value[256], type_str[256];
890 int value;
891 if (avio_tell(bc) >= maxpos)
892 return AVERROR_INVALIDDATA;
893 ret = get_str(bc, name, sizeof(name));
894 if (ret < 0) {
895 av_log(s, AV_LOG_ERROR, "get_str failed while reading sm data\n");
896 return ret;
897 }
898 value = get_s(bc);
899
900 if (value == -1) {
901 ret = get_str(bc, str_value, sizeof(str_value));
902 if (ret < 0) {
903 av_log(s, AV_LOG_ERROR, "get_str failed while reading sm data\n");
904 return ret;
905 }
906 av_log(s, AV_LOG_WARNING, "Unknown string %s / %s\n", name, str_value);
907 } else if (value == -2) {
908 uint8_t *dst = NULL;
909 int64_t v64, value_len;
910
911 ret = get_str(bc, type_str, sizeof(type_str));
912 if (ret < 0) {
913 av_log(s, AV_LOG_ERROR, "get_str failed while reading sm data\n");
914 return ret;
915 }
916 value_len = ffio_read_varlen(bc);
917 if (value_len < 0 || value_len >= maxpos - avio_tell(bc))
918 return AVERROR_INVALIDDATA;
919 if (!strcmp(name, "Palette")) {
921 } else if (!strcmp(name, "Extradata")) {
923 } else if (sscanf(name, "CodecSpecificSide%"SCNd64"", &v64) == 1) {
925 if(!dst)
926 return AVERROR(ENOMEM);
927 AV_WB64(dst, v64);
928 dst += 8;
929 } else if (!strcmp(name, "ChannelLayout") && value_len == 8) {
930 // Ignored
931 continue;
932 } else {
933 av_log(s, AV_LOG_WARNING, "Unknown data %s / %s\n", name, type_str);
934 avio_skip(bc, value_len);
935 continue;
936 }
937 if(!dst)
938 return AVERROR(ENOMEM);
939 avio_read(bc, dst, value_len);
940 } else if (value == -3) {
941 value = get_s(bc);
942 } else if (value == -4) {
944 } else if (value < -4) {
945 get_s(bc);
946 } else {
947 if (!strcmp(name, "SkipStart")) {
948 skip_start = value;
949 } else if (!strcmp(name, "SkipEnd")) {
950 skip_end = value;
951 } else if (!strcmp(name, "Channels")) {
952 // Ignored
953 } else if (!strcmp(name, "SampleRate")) {
954 sample_rate = value;
955 } else if (!strcmp(name, "Width")) {
956 width = value;
957 } else if (!strcmp(name, "Height")) {
958 height = value;
959 } else {
960 av_log(s, AV_LOG_WARNING, "Unknown integer %s\n", name);
961 }
962 }
963 }
964
965 if (sample_rate || width || height) {
967 if (!dst)
968 return AVERROR(ENOMEM);
969 bytestream_put_le32(&dst,
972 );
973 if (sample_rate)
974 bytestream_put_le32(&dst, sample_rate);
975 if (width || height){
976 bytestream_put_le32(&dst, width);
977 bytestream_put_le32(&dst, height);
978 }
979 }
980
981 if (skip_start || skip_end) {
983 if (!dst)
984 return AVERROR(ENOMEM);
985 AV_WL32(dst, skip_start);
986 AV_WL32(dst+4, skip_end);
987 }
988
989 if (avio_tell(bc) >= maxpos)
990 return AVERROR_INVALIDDATA;
991
992 return 0;
993}
994
995static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id,
996 uint8_t *header_idx, int frame_code)
997{
998 AVFormatContext *s = nut->avf;
999 AVIOContext *bc = s->pb;
1000 StreamContext *stc;
1001 int size, flags, size_mul, pts_delta, i, reserved_count, ret;
1002 uint64_t tmp;
1003
1004 if (!(nut->flags & NUT_PIPE) &&
1005 avio_tell(bc) > nut->last_syncpoint_pos + nut->max_distance) {
1007 "Last frame must have been damaged %"PRId64" > %"PRId64" + %d\n",
1008 avio_tell(bc), nut->last_syncpoint_pos, nut->max_distance);
1009 return AVERROR_INVALIDDATA;
1010 }
1011
1012 flags = nut->frame_code[frame_code].flags;
1013 size_mul = nut->frame_code[frame_code].size_mul;
1014 size = nut->frame_code[frame_code].size_lsb;
1015 *stream_id = nut->frame_code[frame_code].stream_id;
1016 pts_delta = nut->frame_code[frame_code].pts_delta;
1017 reserved_count = nut->frame_code[frame_code].reserved_count;
1018 *header_idx = nut->frame_code[frame_code].header_idx;
1019
1020 if (flags & FLAG_INVALID)
1021 return AVERROR_INVALIDDATA;
1022 if (flags & FLAG_CODED)
1023 flags ^= ffio_read_varlen(bc);
1024 if (flags & FLAG_STREAM_ID) {
1025 GET_V(*stream_id, tmp < s->nb_streams);
1026 }
1027 stc = &nut->stream[*stream_id];
1028 if (flags & FLAG_CODED_PTS) {
1029 int64_t coded_pts = ffio_read_varlen(bc);
1030 // FIXME check last_pts validity?
1031 if (coded_pts < (1LL << stc->msb_pts_shift)) {
1032 *pts = ff_lsb2full(stc, coded_pts);
1033 } else
1034 *pts = coded_pts - (1LL << stc->msb_pts_shift);
1035 } else
1036 *pts = stc->last_pts + pts_delta;
1037 if (flags & FLAG_SIZE_MSB)
1038 size += size_mul * ffio_read_varlen(bc);
1039 if (flags & FLAG_MATCH_TIME)
1040 get_s(bc);
1041 if (flags & FLAG_HEADER_IDX)
1042 *header_idx = ffio_read_varlen(bc);
1043 if (flags & FLAG_RESERVED)
1044 reserved_count = ffio_read_varlen(bc);
1045 for (i = 0; i < reserved_count; i++) {
1046 if (bc->eof_reached) {
1047 av_log(s, AV_LOG_ERROR, "reached EOF while decoding frame header\n");
1048 return AVERROR_INVALIDDATA;
1049 }
1050 ffio_read_varlen(bc);
1051 }
1052
1053 if (*header_idx >= (unsigned)nut->header_count) {
1054 av_log(s, AV_LOG_ERROR, "header_idx invalid\n");
1055 return AVERROR_INVALIDDATA;
1056 }
1057 if (size > 4096)
1058 *header_idx = 0;
1059 size -= nut->header_len[*header_idx];
1060
1061 if (flags & FLAG_CHECKSUM) {
1062 avio_rb32(bc); // FIXME check this
1063 } else if (!(nut->flags & NUT_PIPE) &&
1064 size > 2 * nut->max_distance ||
1065 FFABS(stc->last_pts - *pts) > stc->max_pts_distance) {
1066 av_log(s, AV_LOG_ERROR, "frame size > 2max_distance and no checksum\n");
1067 return AVERROR_INVALIDDATA;
1068 }
1069
1070 stc->last_pts = *pts;
1071 stc->last_flags = flags;
1072
1073 return size;
1074fail:
1075 return ret;
1076}
1077
1078static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code)
1079{
1080 AVFormatContext *s = nut->avf;
1081 AVIOContext *bc = s->pb;
1082 int size, stream_id, discard, ret;
1083 int64_t pts, last_IP_pts;
1084 StreamContext *stc;
1085 uint8_t header_idx;
1086
1087 size = decode_frame_header(nut, &pts, &stream_id, &header_idx, frame_code);
1088 if (size < 0)
1089 return size;
1090
1091 stc = &nut->stream[stream_id];
1092
1093 if (stc->last_flags & FLAG_KEY)
1094 stc->skip_until_key_frame = 0;
1095
1096 discard = s->streams[stream_id]->discard;
1097 last_IP_pts = ffstream(s->streams[stream_id])->last_IP_pts;
1098 if ((discard >= AVDISCARD_NONKEY && !(stc->last_flags & FLAG_KEY)) ||
1099 (discard >= AVDISCARD_BIDIR && last_IP_pts != AV_NOPTS_VALUE &&
1100 last_IP_pts > pts) ||
1101 discard >= AVDISCARD_ALL ||
1102 stc->skip_until_key_frame) {
1103 avio_skip(bc, size);
1104 return 1;
1105 }
1106
1107 ret = av_new_packet(pkt, size + nut->header_len[header_idx]);
1108 if (ret < 0)
1109 return ret;
1110 if (nut->header[header_idx])
1111 memcpy(pkt->data, nut->header[header_idx], nut->header_len[header_idx]);
1112 pkt->pos = avio_tell(bc); // FIXME
1113 if (stc->last_flags & FLAG_SM_DATA) {
1114 int sm_size;
1115 if (read_sm_data(s, bc, pkt, 0, pkt->pos + size) < 0) {
1116 ret = AVERROR_INVALIDDATA;
1117 goto fail;
1118 }
1119 if (read_sm_data(s, bc, pkt, 1, pkt->pos + size) < 0) {
1120 ret = AVERROR_INVALIDDATA;
1121 goto fail;
1122 }
1123 sm_size = avio_tell(bc) - pkt->pos;
1124 size -= sm_size;
1125 }
1126
1127 ret = avio_read(bc, pkt->data + nut->header_len[header_idx], size);
1128 if (ret != size) {
1129 if (ret < 0)
1130 goto fail;
1131 }
1132 av_shrink_packet(pkt, nut->header_len[header_idx] + ret);
1133
1134 pkt->stream_index = stream_id;
1135 if (stc->last_flags & FLAG_KEY)
1136 pkt->flags |= AV_PKT_FLAG_KEY;
1137 pkt->pts = pts;
1138
1139 return 0;
1140fail:
1142 return ret;
1143}
1144
1146{
1147 NUTContext *nut = s->priv_data;
1148 AVIOContext *bc = s->pb;
1149 int i, frame_code = 0, ret, skip;
1150 int64_t ts, back_ptr;
1151
1152 for (;;) {
1153 int64_t pos = avio_tell(bc);
1154 uint64_t tmp = nut->next_startcode;
1155 nut->next_startcode = 0;
1156
1157 if (tmp) {
1158 pos -= 8;
1159 } else {
1160 frame_code = avio_r8(bc);
1161 if (avio_feof(bc))
1162 return AVERROR_EOF;
1163 if (frame_code == 'N') {
1164 tmp = frame_code;
1165 for (i = 1; i < 8; i++)
1166 tmp = (tmp << 8) + avio_r8(bc);
1167 }
1168 }
1169 switch (tmp) {
1170 case MAIN_STARTCODE:
1171 case STREAM_STARTCODE:
1172 case INDEX_STARTCODE:
1173 skip = get_packetheader(nut, bc, 0, tmp);
1174 avio_skip(bc, skip);
1175 break;
1176 case INFO_STARTCODE:
1177 if (decode_info_header(nut) < 0)
1178 goto resync;
1179 break;
1181 if (decode_syncpoint(nut, &ts, &back_ptr) < 0)
1182 goto resync;
1183 frame_code = avio_r8(bc);
1185 case 0:
1186 ret = decode_frame(nut, pkt, frame_code);
1187 if (ret == 0)
1188 return 0;
1189 else if (ret == 1) // OK but discard packet
1190 break;
1192 default:
1193resync:
1194 av_log(s, AV_LOG_DEBUG, "syncing from %"PRId64"\n", pos);
1196 nut->last_resync_pos = avio_tell(bc);
1197 if (tmp == 0)
1198 return AVERROR_INVALIDDATA;
1199 av_log(s, AV_LOG_DEBUG, "sync\n");
1200 nut->next_startcode = tmp;
1201 }
1202 }
1203}
1204
1206 int64_t *pos_arg, int64_t pos_limit)
1207{
1208 NUTContext *nut = s->priv_data;
1209 AVIOContext *bc = s->pb;
1210 int64_t pos, pts, back_ptr;
1211 av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%"PRId64",%"PRId64")\n",
1212 stream_index, *pos_arg, pos_limit);
1213
1214 pos = *pos_arg;
1215 do {
1217 if (pos < 1) {
1218 av_log(s, AV_LOG_ERROR, "read_timestamp failed.\n");
1219 return AV_NOPTS_VALUE;
1220 }
1221 } while (decode_syncpoint(nut, &pts, &back_ptr) < 0);
1222 *pos_arg = pos - 1;
1223 av_assert0(nut->last_syncpoint_pos == *pos_arg);
1224
1225 av_log(s, AV_LOG_DEBUG, "return %"PRId64" %"PRId64"\n", pts, back_ptr);
1226 if (stream_index == -2)
1227 return back_ptr;
1228 av_assert0(stream_index == -1);
1229 return pts;
1230}
1231
1232static int read_seek(AVFormatContext *s, int stream_index,
1233 int64_t pts, int flags)
1234{
1235 NUTContext *nut = s->priv_data;
1236 AVStream *st = s->streams[stream_index];
1237 FFStream *const sti = ffstream(st);
1238 Syncpoint dummy = { .ts = pts * av_q2d(st->time_base) * AV_TIME_BASE };
1239 Syncpoint nopts_sp = { .ts = AV_NOPTS_VALUE, .back_ptr = AV_NOPTS_VALUE };
1240 Syncpoint *sp, *next_node[2] = { &nopts_sp, &nopts_sp };
1241 int64_t pos, pos2, ts;
1242 int i;
1243
1244 if (nut->flags & NUT_PIPE) {
1245 return AVERROR(ENOSYS);
1246 }
1247
1248 if (sti->index_entries) {
1250 if (index < 0)
1252 if (index < 0)
1253 return -1;
1254
1255 pos2 = sti->index_entries[index].pos;
1256 ts = sti->index_entries[index].timestamp;
1257 } else {
1259 (void **) next_node);
1260 av_log(s, AV_LOG_DEBUG, "%"PRIu64"-%"PRIu64" %"PRId64"-%"PRId64"\n",
1261 next_node[0]->pos, next_node[1]->pos, next_node[0]->ts,
1262 next_node[1]->ts);
1263 pos = ff_gen_search(s, -1, dummy.ts, next_node[0]->pos,
1264 next_node[1]->pos, next_node[1]->pos,
1265 next_node[0]->ts, next_node[1]->ts,
1267 if (pos < 0)
1268 return pos;
1269
1270 if (!(flags & AVSEEK_FLAG_BACKWARD)) {
1271 dummy.pos = pos + 16;
1272 next_node[1] = &nopts_sp;
1274 (void **) next_node);
1275 pos2 = ff_gen_search(s, -2, dummy.pos, next_node[0]->pos,
1276 next_node[1]->pos, next_node[1]->pos,
1277 next_node[0]->back_ptr, next_node[1]->back_ptr,
1279 if (pos2 >= 0)
1280 pos = pos2;
1281 // FIXME dir but I think it does not matter
1282 }
1283 dummy.pos = pos;
1285 NULL);
1286
1287 av_assert0(sp);
1288 pos2 = sp->back_ptr - 15;
1289 }
1290 av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos2);
1292 avio_seek(s->pb, pos, SEEK_SET);
1293 nut->last_syncpoint_pos = pos;
1294 av_log(s, AV_LOG_DEBUG, "SP: %"PRId64"\n", pos);
1295 if (pos2 > pos || pos2 + 15 < pos)
1296 av_log(s, AV_LOG_ERROR, "no syncpoint at backptr pos\n");
1297 for (i = 0; i < s->nb_streams; i++)
1298 nut->stream[i].skip_until_key_frame = 1;
1299
1300 nut->last_resync_pos = 0;
1301
1302 return 0;
1303}
1304
1306 .p.name = "nut",
1307 .p.long_name = NULL_IF_CONFIG_SMALL("NUT"),
1308 .p.flags = AVFMT_SEEK_TO_PTS,
1309 .p.extensions = "nut",
1310 .p.codec_tag = ff_nut_codec_tags,
1311 .priv_data_size = sizeof(NUTContext),
1312 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
1318};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
#define class
Definition math.h:25
const FFInputFormat ff_nut_demuxer
Definition nutdec.c:1305
#define A(x)
Definition vpx_arith.h:28
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:856
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
Definition avformat.h:1795
#define AVINDEX_KEYFRAME
Definition avformat.h:630
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:485
@ AVFMT_DURATION_FROM_PTS
Duration accurately estimated from PTSes.
Definition avformat.h:1381
#define AVSTREAM_EVENT_FLAG_METADATA_UPDATED
Definition avformat.h:886
#define AVFMT_SEEK_TO_PTS
Seeking is based on PTS.
Definition avformat.h:522
#define AVSEEK_FLAG_BACKWARD
seek backward
Definition avformat.h:2711
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
uint64_t avio_rb64(AVIOContext *s)
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition aviobuf.c:326
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
unsigned int avio_rl16(AVIOContext *s)
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
unsigned int avio_rl32(AVIOContext *s)
unsigned int avio_rb32(AVIOContext *s)
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
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:594
unsigned long ffio_get_checksum(AVIOContext *s)
Definition aviobuf.c:586
uint64_t ffio_read_varlen(AVIOContext *bc)
Definition aviobuf.c:878
unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len)
Definition aviobuf.c:568
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
byte swapping routines
#define av_be2ne64(x)
Definition bswap.h:90
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
#define flag(name)
Definition cbs_h264.c:60
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static int64_t last_pts
AVChapter * avpriv_new_chapter(AVFormatContext *s, int64_t id, AVRational time_base, int64_t start, int64_t end, const char *title)
Add a new chapter.
Definition demux_utils.c:46
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition demux.h:35
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 seek.c:360
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 seek.c:398
int ff_get_extradata(void *logctx, AVCodecParameters *par, AVIOContext *pb, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0 and f...
static AVPacket * pkt
Public dictionary API.
double value
Definition eval.c:102
static struct @346255127015250356166251341105367306144006377143 state
static int64_t filesize(AVIOContext *pb)
Definition ffmpeg_mux.c:52
static int64_t duration
Definition ffplay.c:399
static int dummy
Definition ffplay.c:4664
static unsigned int nb_streams
Definition ffprobe.c:390
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
#define fail
Definition test.h:479
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AVDISCARD_ALL
discard all
Definition defs.h:246
@ AVDISCARD_NONKEY
discard all frames except keyframes
Definition defs.h:245
@ AVDISCARD_BIDIR
discard all bidirectional frames
Definition defs.h:243
@ AV_PKT_DATA_SKIP_SAMPLES
Recommends skipping the specified number of samples.
Definition packet.h:153
@ AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL
Data found in BlockAdditional element of matroska container.
Definition packet.h:188
@ AV_PKT_DATA_PALETTE
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette.
Definition packet.h:47
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition packet.h:56
@ AV_PKT_DATA_PARAM_CHANGE
An AV_PKT_DATA_PARAM_CHANGE side data packet is laid out as follows:
Definition packet.h:69
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:435
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition packet.c:231
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:657
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition packet.c:113
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
@ AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE
Definition packet.h:679
@ AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS
Definition packet.h:680
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
enum AVCodecID av_codec_get_id(const struct AVCodecTag *const *tags, unsigned int tag)
Get the AVCodecID for the given codec tag tag.
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 seek.c:122
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition seek.c:245
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:87
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition rational.h:104
int64_t av_gcd(int64_t a, int64_t b)
Compute the greatest common divisor of two integer operands.
Definition mathematics.c:37
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition avutil.h:202
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition avstring.c:208
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_TIME_BASE
Internal time base represented as integer.
Definition avutil.h:253
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition avutil.h:263
int index
Definition gxfenc.c:90
#define B
Definition huffyuv.h:42
#define AV_WL32(p, v)
#define AV_RB32(p)
#define AV_WB64(p, v)
const AVCodecTag ff_codec_movvideo_tags[]
Definition isom_tags.c:29
uint32_t type
Definition jpegmpfenc.c:80
static int resync(AVFormatContext *s)
Definition flvdec.c:1167
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:365
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition internal.h:130
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition utils.c:143
Macro definitions for various function/variable attributes.
#define av_fallthrough
Definition attributes.h:67
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:370
Memory handling functions.
void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition metadata.c:59
const AVCodecTag ff_nut_audio_tags[]
Definition nut.c:232
const AVCodecTag ff_nut_video_tags[]
Definition nut.c:44
void ff_nut_reset_ts(NUTContext *nut, AVRational time_base, int64_t val)
Definition nut.c:266
int ff_nut_sp_pos_cmp(const void *a, const void *b)
Definition nut.c:284
int64_t ff_lsb2full(StreamContext *stream, int64_t lsb)
Definition nut.c:277
int ff_nut_add_sp(NUTContext *nut, int64_t pos, int64_t back_ptr, int64_t ts)
Definition nut.c:296
int ff_nut_sp_pts_cmp(const void *a, const void *b)
Definition nut.c:290
const AVCodecTag ff_nut_data_tags[]
Definition nut.c:39
const AVCodecTag *const ff_nut_codec_tags[]
Definition nut.c:261
void ff_nut_free_sp(NUTContext *nut)
Definition nut.c:327
const AVCodecTag ff_nut_audio_extra_tags[]
Definition nut.c:222
const Dispositions ff_nut_dispositions[]
Definition nut.c:335
const AVCodecTag ff_nut_subtitle_tags[]
Definition nut.c:29
const AVMetadataConv ff_nut_metadata_conv[]
Definition nut.c:345
#define SYNCPOINT_STARTCODE
Definition nut.h:31
@ FLAG_CHECKSUM
Definition nut.h:49
@ FLAG_HEADER_IDX
Definition nut.h:52
@ FLAG_KEY
Definition nut.h:44
@ FLAG_SM_DATA
Definition nut.h:51
@ FLAG_MATCH_TIME
Definition nut.h:53
@ FLAG_CODED_PTS
Definition nut.h:46
@ FLAG_SIZE_MSB
Definition nut.h:48
@ FLAG_INVALID
Definition nut.h:55
@ FLAG_CODED
Definition nut.h:54
@ FLAG_RESERVED
Definition nut.h:50
@ FLAG_STREAM_ID
Definition nut.h:47
#define NUT_MIN_VERSION
Definition nut.h:41
#define NUT_BROADCAST
Definition nut.h:113
#define STREAM_STARTCODE
Definition nut.h:30
#define NUT_MAX_VERSION
Definition nut.h:39
#define MAIN_STARTCODE
Definition nut.h:29
#define NUT_PIPE
Definition nut.h:114
#define INDEX_STARTCODE
Definition nut.h:32
#define INFO_STARTCODE
Definition nut.h:33
static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition nutdec.c:1145
static uint64_t find_any_startcode(AVIOContext *bc, int64_t pos)
Definition nutdec.c:110
static uint64_t get_fourcc(AVIOContext *bc)
Definition nutdec.c:76
static int get_str(AVIOContext *bc, char *string, unsigned int maxlen)
Definition nutdec.c:42
static int decode_main_header(NUTContext *nut)
Definition nutdec.c:193
static void set_disposition_bits(AVFormatContext *avf, char *value, int stream_id)
Definition nutdec.c:488
static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index, int64_t *pos_arg, int64_t pos_limit)
Definition nutdec.c:1205
static int decode_info_header(NUTContext *nut)
Definition nutdec.c:503
static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr)
Definition nutdec.c:626
static int64_t find_duration(NUTContext *nut, int64_t filesize)
Definition nutdec.c:670
static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code)
Definition nutdec.c:1078
static int get_packetheader(NUTContext *nut, AVIOContext *bc, int calculate_checksum, uint64_t startcode)
Definition nutdec.c:90
static int nut_probe(const AVProbeData *p)
Definition nutdec.c:153
static int skip_reserved(AVIOContext *bc, int64_t pos)
Definition nutdec.c:177
static int find_and_decode_index(NUTContext *nut)
Definition nutdec.c:682
static int read_sm_data(AVFormatContext *s, AVIOContext *bc, AVPacket *pkt, int is_meta, int64_t maxpos)
Definition nutdec.c:878
static int64_t get_s(AVIOContext *bc)
Definition nutdec.c:66
static int nut_read_close(AVFormatContext *s)
Definition nutdec.c:796
static int read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
Definition nutdec.c:1232
#define GET_V(dst, check)
Definition nutdec.c:166
static int64_t find_startcode(AVIOContext *bc, uint64_t code, int64_t pos)
Find the given startcode.
Definition nutdec.c:141
static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id, uint8_t *header_idx, int frame_code)
Definition nutdec.c:995
static int decode_stream_header(NUTContext *nut)
Definition nutdec.c:379
static int nut_read_header(AVFormatContext *s)
Definition nutdec.c:810
#define av_malloc(s)
Definition ops_static.c:52
const char * name
Definition qsvenc.c:142
const AVCodecTag ff_codec_bmp_tags[]
Definition riff.c:36
const AVCodecTag ff_codec_wav_tags[]
Definition riff.c:525
internal header for RIFF based (de)muxers do NOT include this in end user applications
const uint8_t * code
Definition spdifenc.c:433
unsigned int pos
Definition spdifenc.c:431
int nb_channels
Number of channels in this layout.
AVDictionary * metadata
Definition avformat.h:1363
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
int height
The height of the video frame in pixels.
Definition codec_par.h:150
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int video_delay
Number of delayed frames.
Definition codec_par.h:200
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1400
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition avformat.h:1456
AVStream ** streams
A list of all streams in the file.
Definition avformat.h:1468
Bytestream IO Context.
Definition avio.h:160
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition avio.h:261
int eof_reached
true if was unable to read due to error or eof
Definition avio.h:238
int64_t pos
Definition avformat.h:623
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition avformat.h:624
This structure stores compressed data.
Definition packet.h:587
This structure contains the data a format has to probe a file.
Definition avformat.h:473
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition avformat.h:846
AVDictionary * metadata
Definition avformat.h:848
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:807
int event_flags
Flags indicating events happening on the stream, a combination of AVSTREAM_EVENT_FLAG_*.
Definition avformat.h:879
AVRational r_frame_rate
Real base framerate of the stream.
Definition avformat.h:902
int disposition
Stream disposition - a combination of AV_DISPOSITION_* flags.
Definition avformat.h:837
int64_t data_offset
offset of the first packet
Definition internal.h:89
int64_t last_IP_pts
Definition internal.h:312
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition internal.h:191
uint8_t header_idx
Definition nut.h:72
uint16_t flags
Definition nut.h:66
int stream_id
Definition nut.h:67
int16_t pts_delta
Definition nut.h:70
uint16_t size_mul
Definition nut.h:68
uint16_t size_lsb
Definition nut.h:69
uint8_t reserved_count
Definition nut.h:71
const uint8_t * header[128]
Definition nut.h:98
int flags
Definition nut.h:115
AVRational * time_base
Definition nut.h:107
StreamContext * stream
Definition nut.h:100
int header_count
Definition nut.h:106
AVFormatContext * avf
Definition nut.h:93
struct AVTreeNode * syncpoints
Definition nut.h:108
int minor_version
Definition nut.h:117
uint64_t next_startcode
Definition nut.h:99
int version
Definition nut.h:116
FrameCode frame_code[256]
Definition nut.h:96
int64_t last_resync_pos
Definition nut.h:105
unsigned int max_distance
Definition nut.h:102
uint8_t header_len[128]
Definition nut.h:97
int64_t last_syncpoint_pos
Definition nut.h:104
unsigned int time_base_count
Definition nut.h:103
int time_base_id
Definition nut.h:79
AVRational time_base
Definition signature.h:102
int max_pts_distance
Definition nut.h:82
int skip_until_key_frame
Definition nut.h:77
int decode_delay
Definition nut.h:83
int msb_pts_shift
Definition nut.h:81
int last_flags
Definition nut.h:76
int64_t last_pts
Definition nut.h:78
uint64_t back_ptr
Definition nut.h:60
#define av_free(p)
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
#define height
Definition dsp.h:99
#define width
Definition dsp.h:99
static int64_t pts
void * av_tree_find(const AVTreeNode *t, void *key, int(*cmp)(const void *key, const void *b), void *next[2])
Definition tree.c:39
A tree container.
int size
int len