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
39#define NUT_MAX_STREAMS 256 /* arbitrary sanity check value */
40
41static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index,
42 int64_t *pos_arg, int64_t pos_limit);
43
44static int get_str(AVIOContext *bc, char *string, unsigned int maxlen)
45{
46 unsigned int len = ffio_read_varlen(bc);
47
48 if (len && maxlen)
49 avio_read(bc, string, FFMIN(len, maxlen));
50 while (len > maxlen) {
51 avio_r8(bc);
52 len--;
53 if (bc->eof_reached)
54 len = maxlen;
55 }
56
57 if (maxlen)
58 string[FFMIN(len, maxlen - 1)] = 0;
59
60 if (bc->eof_reached)
61 return AVERROR_EOF;
62 if (maxlen == len)
63 return -1;
64 else
65 return 0;
66}
67
69{
70 int64_t v = ffio_read_varlen(bc) + 1;
71
72 if (v & 1)
73 return -(v >> 1);
74 else
75 return (v >> 1);
76}
77
78static uint64_t get_fourcc(AVIOContext *bc)
79{
80 unsigned int len = ffio_read_varlen(bc);
81
82 if (len == 2)
83 return avio_rl16(bc);
84 else if (len == 4)
85 return avio_rl32(bc);
86 else {
87 av_log(NULL, AV_LOG_ERROR, "Unsupported fourcc length %d\n", len);
88 return -1;
89 }
90}
91
93 int calculate_checksum, uint64_t startcode)
94{
96
97 startcode = av_be2ne64(startcode);
98 startcode = ff_crc04C11DB7_update(0, (uint8_t*) &startcode, 8);
99
102 if (size > 4096)
103 avio_rb32(bc);
104 if (ffio_get_checksum(bc) && size > 4096)
105 return -1;
106
107 ffio_init_checksum(bc, calculate_checksum ? ff_crc04C11DB7_update : NULL, 0);
108
109 return size;
110}
111
113{
114 uint64_t state = 0;
115
116 if (pos >= 0)
117 /* Note, this may fail if the stream is not seekable, but that should
118 * not matter, as in this case we simply start where we currently are */
119 avio_seek(bc, pos, SEEK_SET);
120 while (!avio_feof(bc)) {
121 state = (state << 8) | avio_r8(bc);
122 if ((state >> 56) != 'N')
123 continue;
124 switch (state) {
125 case MAIN_STARTCODE:
126 case STREAM_STARTCODE:
128 case INFO_STARTCODE:
129 case INDEX_STARTCODE:
130 return state;
131 }
132 }
133
134 return 0;
135}
136
137/**
138 * Find the given startcode.
139 * @param code the startcode
140 * @param pos the start position of the search, or -1 if the current position
141 * @return the position of the startcode or -1 if not found
142 */
144{
145 for (;;) {
146 uint64_t startcode = find_any_startcode(bc, pos);
147 if (startcode == code)
148 return avio_tell(bc) - 8;
149 else if (startcode == 0)
150 return -1;
151 pos = -1;
152 }
153}
154
155static int nut_probe(const AVProbeData *p)
156{
157 int i;
158
159 for (i = 0; i < p->buf_size-8; i++) {
160 if (AV_RB32(p->buf+i) != MAIN_STARTCODE>>32)
161 continue;
162 if (AV_RB32(p->buf+i+4) == (MAIN_STARTCODE & 0xFFFFFFFF))
163 return AVPROBE_SCORE_MAX;
164 }
165 return 0;
166}
167
168#define GET_V(dst, check) \
169 do { \
170 tmp = ffio_read_varlen(bc); \
171 if (!(check)) { \
172 av_log(s, AV_LOG_ERROR, "Error " #dst " is (%"PRId64")\n", tmp); \
173 ret = AVERROR_INVALIDDATA; \
174 goto fail; \
175 } \
176 dst = tmp; \
177 } while (0)
178
180{
181 pos -= avio_tell(bc);
182 if (pos < 0) {
183 avio_seek(bc, pos, SEEK_CUR);
184 return AVERROR_INVALIDDATA;
185 } else {
186 while (pos--) {
187 if (bc->eof_reached)
188 return AVERROR_INVALIDDATA;
189 avio_r8(bc);
190 }
191 return 0;
192 }
193}
194
196{
197 AVFormatContext *s = nut->avf;
198 AVIOContext *bc = s->pb;
199 uint64_t tmp, end, length;
200 unsigned int stream_count;
201 int i, j, count, ret;
202 int tmp_stream, tmp_mul, tmp_pts, tmp_size, tmp_res, tmp_head_idx;
203
204 length = get_packetheader(nut, bc, 1, MAIN_STARTCODE);
205 if (length == (uint64_t)-1)
206 return AVERROR_INVALIDDATA;
207 end = length + avio_tell(bc);
208
209 nut->version = ffio_read_varlen(bc);
210 if (nut->version < NUT_MIN_VERSION ||
211 nut->version > NUT_MAX_VERSION) {
212 av_log(s, AV_LOG_ERROR, "Version %d not supported.\n",
213 nut->version);
214 return AVERROR(ENOSYS);
215 }
216 if (nut->version > 3)
218
219 GET_V(stream_count, tmp > 0 && tmp <= NUT_MAX_STREAMS);
220
222 if (nut->max_distance > 65536) {
223 av_log(s, AV_LOG_DEBUG, "max_distance %d\n", nut->max_distance);
224 nut->max_distance = 65536;
225 }
226
227 GET_V(nut->time_base_count, tmp > 0 && tmp < INT_MAX / sizeof(AVRational) && tmp < length/2);
229 if (!nut->time_base)
230 return AVERROR(ENOMEM);
231
232 for (i = 0; i < nut->time_base_count; i++) {
233 GET_V(nut->time_base[i].num, tmp > 0 && tmp < (1ULL << 31));
234 GET_V(nut->time_base[i].den, tmp > 0 && tmp < (1ULL << 31));
235 if (av_gcd(nut->time_base[i].num, nut->time_base[i].den) != 1) {
236 av_log(s, AV_LOG_ERROR, "invalid time base %d/%d\n",
237 nut->time_base[i].num,
238 nut->time_base[i].den);
240 goto fail;
241 }
242 }
243 tmp_pts = 0;
244 tmp_mul = 1;
245 tmp_stream = 0;
246 tmp_head_idx = 0;
247 for (i = 0; i < 256;) {
248 int tmp_flags = ffio_read_varlen(bc);
249 int tmp_fields = ffio_read_varlen(bc);
250 if (tmp_fields < 0) {
251 av_log(s, AV_LOG_ERROR, "fields %d is invalid\n", tmp_fields);
253 goto fail;
254 }
255
256 if (tmp_fields > 0)
257 tmp_pts = get_s(bc);
258 if (tmp_fields > 1)
259 tmp_mul = ffio_read_varlen(bc);
260 if (tmp_fields > 2)
261 tmp_stream = ffio_read_varlen(bc);
262 if (tmp_fields > 3)
263 tmp_size = ffio_read_varlen(bc);
264 else
265 tmp_size = 0;
266 if (tmp_fields > 4)
267 tmp_res = ffio_read_varlen(bc);
268 else
269 tmp_res = 0;
270 if (tmp_fields > 5)
271 count = ffio_read_varlen(bc);
272 else
273 count = tmp_mul - (unsigned)tmp_size;
274 if (tmp_fields > 6)
275 get_s(bc);
276 if (tmp_fields > 7)
277 tmp_head_idx = ffio_read_varlen(bc);
278
279 while (tmp_fields-- > 8) {
280 if (bc->eof_reached) {
281 av_log(s, AV_LOG_ERROR, "reached EOF while decoding main header\n");
283 goto fail;
284 }
286 }
287
288 if (count <= 0 || count > 256 - (i <= 'N') - i) {
289 av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i);
291 goto fail;
292 }
293 if (tmp_stream >= stream_count) {
294 av_log(s, AV_LOG_ERROR, "illegal stream number %d >= %d\n",
295 tmp_stream, stream_count);
297 goto fail;
298 }
299 if (tmp_size < 0 || tmp_size > INT_MAX - count) {
300 av_log(s, AV_LOG_ERROR, "illegal size\n");
302 goto fail;
303 }
304
305 for (j = 0; j < count; j++, i++) {
306 if (i == 'N') {
308 j--;
309 continue;
310 }
311 nut->frame_code[i].flags = tmp_flags;
312 nut->frame_code[i].pts_delta = tmp_pts;
313 nut->frame_code[i].stream_id = tmp_stream;
314 nut->frame_code[i].size_mul = tmp_mul;
315 nut->frame_code[i].size_lsb = tmp_size + j;
316 nut->frame_code[i].reserved_count = tmp_res;
317 nut->frame_code[i].header_idx = tmp_head_idx;
318 }
319 }
321
322 if (end > avio_tell(bc) + 4) {
323 int rem = 1024;
324 GET_V(nut->header_count, tmp < 128U);
325 nut->header_count++;
326 for (i = 1; i < nut->header_count; i++) {
327 uint8_t *hdr;
328 GET_V(nut->header_len[i], tmp > 0 && tmp < 256);
329 if (rem < nut->header_len[i]) {
331 "invalid elision header %d : %d > %d\n",
332 i, nut->header_len[i], rem);
334 goto fail;
335 }
336 rem -= nut->header_len[i];
337 hdr = av_malloc(nut->header_len[i]);
338 if (!hdr) {
339 ret = AVERROR(ENOMEM);
340 goto fail;
341 }
342 avio_read(bc, hdr, nut->header_len[i]);
343 nut->header[i] = hdr;
344 }
345 av_assert0(nut->header_len[0] == 0);
346 }
347
348 // flags had been effectively introduced in version 4
349 if (nut->version > 3 && end > avio_tell(bc) + 4) {
350 nut->flags = ffio_read_varlen(bc);
351 }
352
353 if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
354 av_log(s, AV_LOG_ERROR, "main header checksum mismatch\n");
356 goto fail;
357 }
358
359 nut->stream = av_calloc(stream_count, sizeof(StreamContext));
360 if (!nut->stream) {
361 ret = AVERROR(ENOMEM);
362 goto fail;
363 }
364 for (i = 0; i < stream_count; i++) {
365 if (!avformat_new_stream(s, NULL)) {
366 ret = AVERROR(ENOMEM);
367 goto fail;
368 }
369 }
370
371 return 0;
372fail:
373 av_freep(&nut->time_base);
374 for (i = 1; i < nut->header_count; i++) {
375 av_freep(&nut->header[i]);
376 }
377 nut->header_count = 0;
378 return ret;
379}
380
382{
383 AVFormatContext *s = nut->avf;
384 AVIOContext *bc = s->pb;
385 StreamContext *stc;
386 int class, stream_id, ret;
387 uint64_t tmp, end;
388 AVStream *st = NULL;
389
390 end = get_packetheader(nut, bc, 1, STREAM_STARTCODE);
391 end += avio_tell(bc);
392
393 GET_V(stream_id, tmp < s->nb_streams && !nut->stream[tmp].time_base);
394 stc = &nut->stream[stream_id];
395 st = s->streams[stream_id];
396 if (!st)
397 return AVERROR(ENOMEM);
398
399 class = ffio_read_varlen(bc);
400 tmp = get_fourcc(bc);
401 st->codecpar->codec_tag = tmp;
402 switch (class) {
403 case 0:
405 st->codecpar->codec_id = av_codec_get_id((const AVCodecTag * const []) {
409 0
410 },
411 tmp);
412 break;
413 case 1:
415 st->codecpar->codec_id = av_codec_get_id((const AVCodecTag * const []) {
419 0
420 },
421 tmp);
422 break;
423 case 2:
426 break;
427 case 3:
430 break;
431 default:
432 av_log(s, AV_LOG_ERROR, "unknown stream class (%d)\n", class);
433 return AVERROR(ENOSYS);
434 }
435 if (class < 3 && st->codecpar->codec_id == AV_CODEC_ID_NONE)
437 "Unknown codec tag '0x%04x' for stream number %d\n",
438 (unsigned int) tmp, stream_id);
439
440 GET_V(stc->time_base_id, tmp < nut->time_base_count);
441 GET_V(stc->msb_pts_shift, tmp < 16);
443 GET_V(stc->decode_delay, tmp < 1000); // sanity limit, raise this if Moore's law is true
445 ffio_read_varlen(bc); // stream flags
446
447 GET_V(st->codecpar->extradata_size, tmp < (1 << 30));
448 if (st->codecpar->extradata_size) {
449 ret = ff_get_extradata(s, st->codecpar, bc,
451 if (ret < 0)
452 return ret;
453 }
454
456 GET_V(st->codecpar->width, tmp > 0);
457 GET_V(st->codecpar->height, tmp > 0);
460 if ((!st->sample_aspect_ratio.num) != (!st->sample_aspect_ratio.den)) {
461 av_log(s, AV_LOG_ERROR, "invalid aspect ratio %d/%d\n",
464 goto fail;
465 }
466 ffio_read_varlen(bc); /* csp type */
467 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
468 GET_V(st->codecpar->sample_rate, tmp > 0);
469 ffio_read_varlen(bc); // samplerate_den
471 }
472 if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
474 "stream header %d checksum mismatch\n", stream_id);
476 goto fail;
477 }
478 stc->time_base = &nut->time_base[stc->time_base_id];
479 avpriv_set_pts_info(s->streams[stream_id], 63, stc->time_base->num,
480 stc->time_base->den);
481 return 0;
482fail:
483 if (st && st->codecpar) {
485 st->codecpar->extradata_size = 0;
486 }
487 return ret;
488}
489
491 int stream_id)
492{
493 int flag = 0, i;
494
495 for (i = 0; ff_nut_dispositions[i].flag; ++i)
496 if (!strcmp(ff_nut_dispositions[i].str, value))
497 flag = ff_nut_dispositions[i].flag;
498 if (!flag)
499 av_log(avf, AV_LOG_INFO, "unknown disposition type '%s'\n", value);
500 for (i = 0; i < avf->nb_streams; ++i)
501 if (stream_id == i || stream_id == -1)
502 avf->streams[i]->disposition |= flag;
503}
504
506{
507 AVFormatContext *s = nut->avf;
508 AVIOContext *bc = s->pb;
509 uint64_t tmp, chapter_start, chapter_len;
510 unsigned int stream_id_plus1, count;
511 int i, ret = 0;
512 int64_t chapter_id, value, end;
513 char name[256], str_value[1024], type_str[256];
514 const char *type;
515 int *event_flags = NULL;
516 AVChapter *chapter = NULL;
517 AVStream *st = NULL;
519 int metadata_flag = 0;
520
521 end = get_packetheader(nut, bc, 1, INFO_STARTCODE);
522 end += avio_tell(bc);
523
524 GET_V(stream_id_plus1, tmp <= s->nb_streams);
525 chapter_id = get_s(bc);
526 chapter_start = ffio_read_varlen(bc);
527 chapter_len = ffio_read_varlen(bc);
528 count = ffio_read_varlen(bc);
529
530 if (chapter_id && !stream_id_plus1) {
531 int64_t start = chapter_start / nut->time_base_count;
532 chapter = avpriv_new_chapter(s, chapter_id,
533 nut->time_base[chapter_start %
534 nut->time_base_count],
535 start, start + chapter_len, NULL);
536 if (!chapter) {
537 av_log(s, AV_LOG_ERROR, "Could not create chapter.\n");
538 return AVERROR(ENOMEM);
539 }
540 metadata = &chapter->metadata;
541 } else if (stream_id_plus1) {
542 st = s->streams[stream_id_plus1 - 1];
543 metadata = &st->metadata;
544 event_flags = &st->event_flags;
546 } else {
547 metadata = &s->metadata;
548 event_flags = &s->event_flags;
549 metadata_flag = AVFMT_EVENT_FLAG_METADATA_UPDATED;
550 }
551
552 for (i = 0; i < count; i++) {
553 ret = get_str(bc, name, sizeof(name));
554 if (ret < 0) {
555 av_log(s, AV_LOG_ERROR, "get_str failed while decoding info header\n");
556 return ret;
557 }
558 value = get_s(bc);
559 str_value[0] = 0;
560
561 if (value == -1) {
562 type = "UTF-8";
563 ret = get_str(bc, str_value, sizeof(str_value));
564 } else if (value == -2) {
565 ret = get_str(bc, type_str, sizeof(type_str));
566 if (ret < 0) {
567 av_log(s, AV_LOG_ERROR, "get_str failed while decoding info header\n");
568 return ret;
569 }
570 type = type_str;
571 ret = get_str(bc, str_value, sizeof(str_value));
572 } else if (value == -3) {
573 type = "s";
574 value = get_s(bc);
575 } else if (value == -4) {
576 type = "t";
578 } else if (value < -4) {
579 type = "r";
580 get_s(bc);
581 } else {
582 type = "v";
583 }
584
585 if (ret < 0) {
586 av_log(s, AV_LOG_ERROR, "get_str failed while decoding info header\n");
587 return ret;
588 }
589
590 if (stream_id_plus1 > s->nb_streams) {
592 "invalid stream id %d for info packet\n",
593 stream_id_plus1);
594 continue;
595 }
596
597 if (!strcmp(type, "UTF-8")) {
598 if (chapter_id == 0 && !strcmp(name, "Disposition")) {
599 set_disposition_bits(s, str_value, stream_id_plus1 - 1);
600 continue;
601 }
602
603 if (stream_id_plus1 && !strcmp(name, "r_frame_rate")) {
604 sscanf(str_value, "%d/%d", &st->r_frame_rate.num, &st->r_frame_rate.den);
605 if (st->r_frame_rate.num >= 1000LL*st->r_frame_rate.den ||
606 st->r_frame_rate.num < 0 || st->r_frame_rate.den < 0)
607 st->r_frame_rate.num = st->r_frame_rate.den = 0;
608 continue;
609 }
610
611 if (metadata && av_strcasecmp(name, "Uses") &&
612 av_strcasecmp(name, "Depends") && av_strcasecmp(name, "Replaces")) {
613 if (event_flags)
614 *event_flags |= metadata_flag;
615 av_dict_set(metadata, name, str_value, 0);
616 }
617 }
618 }
619
620 if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
621 av_log(s, AV_LOG_ERROR, "info header checksum mismatch\n");
622 return AVERROR_INVALIDDATA;
623 }
624fail:
625 return FFMIN(ret, 0);
626}
627
628static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr)
629{
630 AVFormatContext *s = nut->avf;
631 AVIOContext *bc = s->pb;
632 int64_t end;
633 uint64_t tmp;
634 int ret;
635
636 nut->last_syncpoint_pos = avio_tell(bc) - 8;
637
638 end = get_packetheader(nut, bc, 1, SYNCPOINT_STARTCODE);
639 end += avio_tell(bc);
640
641 tmp = ffio_read_varlen(bc);
642 *back_ptr = nut->last_syncpoint_pos - 16 * ffio_read_varlen(bc);
643 if (*back_ptr < 0)
644 return AVERROR_INVALIDDATA;
645
647 tmp / nut->time_base_count);
648
649 if (nut->flags & NUT_BROADCAST) {
650 tmp = ffio_read_varlen(bc);
651 av_log(s, AV_LOG_VERBOSE, "Syncpoint wallclock %"PRId64"\n",
653 nut->time_base[tmp % nut->time_base_count],
655 }
656
657 if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
658 av_log(s, AV_LOG_ERROR, "sync point checksum mismatch\n");
659 return AVERROR_INVALIDDATA;
660 }
661
662 *ts = tmp / nut->time_base_count *
664
665 if ((ret = ff_nut_add_sp(nut, nut->last_syncpoint_pos, *back_ptr, *ts)) < 0)
666 return ret;
667
668 return 0;
669}
670
671//FIXME calculate exactly, this is just a good approximation.
673{
674 AVFormatContext *s = nut->avf;
675 int64_t duration = 0;
676
678
679 if(duration > 0)
680 s->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
681 return duration;
682}
683
685{
686 AVFormatContext *s = nut->avf;
687 AVIOContext *bc = s->pb;
688 uint64_t tmp, end;
689 int i, j, syncpoint_count;
691 int64_t *syncpoints = NULL;
692 uint64_t max_pts;
693 int8_t *has_keyframe = NULL;
694 int ret = AVERROR_INVALIDDATA;
695
696 if(filesize <= 0)
697 return -1;
698
699 avio_seek(bc, filesize - 12, SEEK_SET);
700 avio_seek(bc, filesize - avio_rb64(bc), SEEK_SET);
701 if (avio_rb64(bc) != INDEX_STARTCODE) {
702 av_log(s, AV_LOG_WARNING, "no index at the end\n");
703
704 if(s->duration<=0)
705 s->duration = find_duration(nut, filesize);
706 return ret;
707 }
708
709 end = get_packetheader(nut, bc, 1, INDEX_STARTCODE);
710 end += avio_tell(bc);
711
712 max_pts = ffio_read_varlen(bc);
713 s->duration = av_rescale_q(max_pts / nut->time_base_count,
714 nut->time_base[max_pts % nut->time_base_count],
716 s->duration_estimation_method = AVFMT_DURATION_FROM_PTS;
717
718 GET_V(syncpoint_count, tmp < INT_MAX / 8 && tmp > 0);
719 syncpoints = av_malloc_array(syncpoint_count, sizeof(int64_t));
720 has_keyframe = av_malloc_array(syncpoint_count + 1, sizeof(int8_t));
721 if (!syncpoints || !has_keyframe) {
722 ret = AVERROR(ENOMEM);
723 goto fail;
724 }
725 for (i = 0; i < syncpoint_count; i++) {
726 syncpoints[i] = ffio_read_varlen(bc);
727 if (syncpoints[i] <= 0)
728 goto fail;
729 if (i)
730 syncpoints[i] += syncpoints[i - 1];
731 }
732
733 for (i = 0; i < s->nb_streams; i++) {
734 int64_t last_pts = -1;
735 for (j = 0; j < syncpoint_count;) {
736 uint64_t x = ffio_read_varlen(bc);
737 int type = x & 1;
738 int n = j;
739 x >>= 1;
740 if (type) {
741 int flag = x & 1;
742 x >>= 1;
743 if (n + x >= syncpoint_count + 1) {
744 av_log(s, AV_LOG_ERROR, "index overflow A %d + %"PRIu64" >= %d\n", n, x, syncpoint_count + 1);
745 goto fail;
746 }
747 while (x--)
748 has_keyframe[n++] = flag;
749 has_keyframe[n++] = !flag;
750 } else {
751 if (x <= 1) {
752 av_log(s, AV_LOG_ERROR, "index: x %"PRIu64" is invalid\n", x);
753 goto fail;
754 }
755 while (x != 1) {
756 if (n >= syncpoint_count + 1) {
757 av_log(s, AV_LOG_ERROR, "index overflow B\n");
758 goto fail;
759 }
760 has_keyframe[n++] = x & 1;
761 x >>= 1;
762 }
763 }
764 if (has_keyframe[0]) {
765 av_log(s, AV_LOG_ERROR, "keyframe before first syncpoint in index\n");
766 goto fail;
767 }
768 av_assert0(n <= syncpoint_count + 1);
769 for (; j < n && j < syncpoint_count; j++) {
770 if (has_keyframe[j]) {
771 uint64_t B, A = ffio_read_varlen(bc);
772 if (!A) {
773 A = ffio_read_varlen(bc);
774 B = ffio_read_varlen(bc);
775 // eor_pts[j][i] = last_pts + A + B
776 } else
777 B = 0;
778 av_add_index_entry(s->streams[i], 16 * syncpoints[j - 1],
779 last_pts + A, 0, 0, AVINDEX_KEYFRAME);
780 last_pts += A + B;
781 }
782 }
783 }
784 }
785
786 if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
787 av_log(s, AV_LOG_ERROR, "index checksum mismatch\n");
788 goto fail;
789 }
790 ret = 0;
791
792fail:
793 av_free(syncpoints);
794 av_free(has_keyframe);
795 return ret;
796}
797
799{
800 NUTContext *nut = s->priv_data;
801 int i;
802
803 av_freep(&nut->time_base);
804 av_freep(&nut->stream);
805 ff_nut_free_sp(nut);
806 for (i = 1; i < nut->header_count; i++)
807 av_freep(&nut->header[i]);
808
809 return 0;
810}
811
813{
814 NUTContext *nut = s->priv_data;
815 AVIOContext *bc = s->pb;
816 int64_t pos;
817 int initialized_stream_count, ret;
818
819 nut->avf = s;
820
821 /* main header */
822 pos = 0;
823 ret = 0;
824 do {
825 if (ret == AVERROR(ENOMEM))
826 return ret;
827
829 if (pos < 0 + 1) {
830 av_log(s, AV_LOG_ERROR, "No main startcode found.\n");
831 return AVERROR_INVALIDDATA;
832 }
833 } while ((ret = decode_main_header(nut)) < 0);
834
835 /* stream headers */
836 pos = 0;
837 for (initialized_stream_count = 0; initialized_stream_count < s->nb_streams;) {
839 if (pos < 0 + 1) {
840 av_log(s, AV_LOG_ERROR, "Not all stream headers found.\n");
841 return AVERROR_INVALIDDATA;
842 }
843 if (decode_stream_header(nut) >= 0)
844 initialized_stream_count++;
845 }
846
847 /* info headers */
848 pos = 0;
849 for (;;) {
850 uint64_t startcode = find_any_startcode(bc, pos);
851 pos = avio_tell(bc);
852
853 if (startcode == 0) {
854 av_log(s, AV_LOG_ERROR, "EOF before video frames\n");
855 return AVERROR_INVALIDDATA;
856 } else if (startcode == SYNCPOINT_STARTCODE) {
857 nut->next_startcode = startcode;
858 break;
859 } else if (startcode != INFO_STARTCODE) {
860 continue;
861 }
862
864 }
865
867
868 if (bc->seekable & AVIO_SEEKABLE_NORMAL) {
869 int64_t orig_pos = avio_tell(bc);
871 avio_seek(bc, orig_pos, SEEK_SET);
872 }
874
876
877 return 0;
878}
879
880static int read_sm_data(AVFormatContext *s, AVIOContext *bc, AVPacket *pkt, int is_meta, int64_t maxpos)
881{
882 int count = ffio_read_varlen(bc);
883 int skip_start = 0;
884 int skip_end = 0;
885 int sample_rate = 0;
886 int width = 0;
887 int height = 0;
888 int i, ret;
889
890 for (i=0; i<count; i++) {
891 uint8_t name[256], str_value[256], type_str[256];
892 int value;
893 if (avio_tell(bc) >= maxpos)
894 return AVERROR_INVALIDDATA;
895 ret = get_str(bc, name, sizeof(name));
896 if (ret < 0) {
897 av_log(s, AV_LOG_ERROR, "get_str failed while reading sm data\n");
898 return ret;
899 }
900 value = get_s(bc);
901
902 if (value == -1) {
903 ret = get_str(bc, str_value, sizeof(str_value));
904 if (ret < 0) {
905 av_log(s, AV_LOG_ERROR, "get_str failed while reading sm data\n");
906 return ret;
907 }
908 av_log(s, AV_LOG_WARNING, "Unknown string %s / %s\n", name, str_value);
909 } else if (value == -2) {
910 uint8_t *dst = NULL;
911 int64_t v64, value_len;
912
913 ret = get_str(bc, type_str, sizeof(type_str));
914 if (ret < 0) {
915 av_log(s, AV_LOG_ERROR, "get_str failed while reading sm data\n");
916 return ret;
917 }
918 value_len = ffio_read_varlen(bc);
919 if (value_len < 0 || value_len >= maxpos - avio_tell(bc))
920 return AVERROR_INVALIDDATA;
921 if (!strcmp(name, "Palette")) {
923 } else if (!strcmp(name, "Extradata")) {
925 } else if (sscanf(name, "CodecSpecificSide%"SCNd64"", &v64) == 1) {
927 if(!dst)
928 return AVERROR(ENOMEM);
929 AV_WB64(dst, v64);
930 dst += 8;
931 } else if (!strcmp(name, "ChannelLayout") && value_len == 8) {
932 // Ignored
933 continue;
934 } else {
935 av_log(s, AV_LOG_WARNING, "Unknown data %s / %s\n", name, type_str);
936 avio_skip(bc, value_len);
937 continue;
938 }
939 if(!dst)
940 return AVERROR(ENOMEM);
941 avio_read(bc, dst, value_len);
942 } else if (value == -3) {
943 value = get_s(bc);
944 } else if (value == -4) {
946 } else if (value < -4) {
947 get_s(bc);
948 } else {
949 if (!strcmp(name, "SkipStart")) {
950 skip_start = value;
951 } else if (!strcmp(name, "SkipEnd")) {
952 skip_end = value;
953 } else if (!strcmp(name, "Channels")) {
954 // Ignored
955 } else if (!strcmp(name, "SampleRate")) {
956 sample_rate = value;
957 } else if (!strcmp(name, "Width")) {
958 width = value;
959 } else if (!strcmp(name, "Height")) {
960 height = value;
961 } else {
962 av_log(s, AV_LOG_WARNING, "Unknown integer %s\n", name);
963 }
964 }
965 }
966
967 if (sample_rate || width || height) {
969 if (!dst)
970 return AVERROR(ENOMEM);
971 bytestream_put_le32(&dst,
974 );
975 if (sample_rate)
976 bytestream_put_le32(&dst, sample_rate);
977 if (width || height){
978 bytestream_put_le32(&dst, width);
979 bytestream_put_le32(&dst, height);
980 }
981 }
982
983 if (skip_start || skip_end) {
985 if (!dst)
986 return AVERROR(ENOMEM);
987 AV_WL32(dst, skip_start);
988 AV_WL32(dst+4, skip_end);
989 }
990
991 if (avio_tell(bc) >= maxpos)
992 return AVERROR_INVALIDDATA;
993
994 return 0;
995}
996
997static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id,
998 uint8_t *header_idx, int frame_code)
999{
1000 AVFormatContext *s = nut->avf;
1001 AVIOContext *bc = s->pb;
1002 StreamContext *stc;
1003 int size, flags, size_mul, pts_delta, i, reserved_count, ret;
1004 uint64_t tmp;
1005
1006 if (!(nut->flags & NUT_PIPE) &&
1007 avio_tell(bc) > nut->last_syncpoint_pos + nut->max_distance) {
1009 "Last frame must have been damaged %"PRId64" > %"PRId64" + %d\n",
1010 avio_tell(bc), nut->last_syncpoint_pos, nut->max_distance);
1011 return AVERROR_INVALIDDATA;
1012 }
1013
1014 flags = nut->frame_code[frame_code].flags;
1015 size_mul = nut->frame_code[frame_code].size_mul;
1016 size = nut->frame_code[frame_code].size_lsb;
1017 *stream_id = nut->frame_code[frame_code].stream_id;
1018 pts_delta = nut->frame_code[frame_code].pts_delta;
1019 reserved_count = nut->frame_code[frame_code].reserved_count;
1020 *header_idx = nut->frame_code[frame_code].header_idx;
1021
1022 if (flags & FLAG_INVALID)
1023 return AVERROR_INVALIDDATA;
1024 if (flags & FLAG_CODED)
1025 flags ^= ffio_read_varlen(bc);
1026 if (flags & FLAG_STREAM_ID) {
1027 GET_V(*stream_id, tmp < s->nb_streams);
1028 }
1029 stc = &nut->stream[*stream_id];
1030 if (flags & FLAG_CODED_PTS) {
1031 int64_t coded_pts = ffio_read_varlen(bc);
1032 // FIXME check last_pts validity?
1033 if (coded_pts < (1LL << stc->msb_pts_shift)) {
1034 *pts = ff_lsb2full(stc, coded_pts);
1035 } else
1036 *pts = coded_pts - (1LL << stc->msb_pts_shift);
1037 } else
1038 *pts = stc->last_pts + pts_delta;
1039 if (flags & FLAG_SIZE_MSB)
1040 size += size_mul * ffio_read_varlen(bc);
1041 if (flags & FLAG_MATCH_TIME)
1042 get_s(bc);
1043 if (flags & FLAG_HEADER_IDX)
1044 *header_idx = ffio_read_varlen(bc);
1045 if (flags & FLAG_RESERVED)
1046 reserved_count = ffio_read_varlen(bc);
1047 for (i = 0; i < reserved_count; i++) {
1048 if (bc->eof_reached) {
1049 av_log(s, AV_LOG_ERROR, "reached EOF while decoding frame header\n");
1050 return AVERROR_INVALIDDATA;
1051 }
1052 ffio_read_varlen(bc);
1053 }
1054
1055 if (*header_idx >= (unsigned)nut->header_count) {
1056 av_log(s, AV_LOG_ERROR, "header_idx invalid\n");
1057 return AVERROR_INVALIDDATA;
1058 }
1059 if (size > 4096)
1060 *header_idx = 0;
1061 size -= nut->header_len[*header_idx];
1062
1063 if (flags & FLAG_CHECKSUM) {
1064 avio_rb32(bc); // FIXME check this
1065 } else if (!(nut->flags & NUT_PIPE) &&
1066 size > 2 * nut->max_distance ||
1067 FFABS(stc->last_pts - *pts) > stc->max_pts_distance) {
1068 av_log(s, AV_LOG_ERROR, "frame size > 2max_distance and no checksum\n");
1069 return AVERROR_INVALIDDATA;
1070 }
1071
1072 stc->last_pts = *pts;
1073 stc->last_flags = flags;
1074
1075 return size;
1076fail:
1077 return ret;
1078}
1079
1080static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code)
1081{
1082 AVFormatContext *s = nut->avf;
1083 AVIOContext *bc = s->pb;
1084 int size, stream_id, discard, ret;
1085 int64_t pts, last_IP_pts;
1086 StreamContext *stc;
1087 uint8_t header_idx;
1088
1089 size = decode_frame_header(nut, &pts, &stream_id, &header_idx, frame_code);
1090 if (size < 0)
1091 return size;
1092
1093 stc = &nut->stream[stream_id];
1094
1095 if (stc->last_flags & FLAG_KEY)
1096 stc->skip_until_key_frame = 0;
1097
1098 discard = s->streams[stream_id]->discard;
1099 last_IP_pts = ffstream(s->streams[stream_id])->last_IP_pts;
1100 if ((discard >= AVDISCARD_NONKEY && !(stc->last_flags & FLAG_KEY)) ||
1101 (discard >= AVDISCARD_BIDIR && last_IP_pts != AV_NOPTS_VALUE &&
1102 last_IP_pts > pts) ||
1103 discard >= AVDISCARD_ALL ||
1104 stc->skip_until_key_frame) {
1105 avio_skip(bc, size);
1106 return 1;
1107 }
1108
1109 ret = av_new_packet(pkt, size + nut->header_len[header_idx]);
1110 if (ret < 0)
1111 return ret;
1112 if (nut->header[header_idx])
1113 memcpy(pkt->data, nut->header[header_idx], nut->header_len[header_idx]);
1114 pkt->pos = avio_tell(bc); // FIXME
1115 if (stc->last_flags & FLAG_SM_DATA) {
1116 int sm_size;
1117 if (read_sm_data(s, bc, pkt, 0, pkt->pos + size) < 0) {
1118 ret = AVERROR_INVALIDDATA;
1119 goto fail;
1120 }
1121 if (read_sm_data(s, bc, pkt, 1, pkt->pos + size) < 0) {
1122 ret = AVERROR_INVALIDDATA;
1123 goto fail;
1124 }
1125 sm_size = avio_tell(bc) - pkt->pos;
1126 size -= sm_size;
1127 }
1128
1129 ret = avio_read(bc, pkt->data + nut->header_len[header_idx], size);
1130 if (ret != size) {
1131 if (ret < 0)
1132 goto fail;
1133 }
1134 av_shrink_packet(pkt, nut->header_len[header_idx] + ret);
1135
1136 pkt->stream_index = stream_id;
1137 if (stc->last_flags & FLAG_KEY)
1138 pkt->flags |= AV_PKT_FLAG_KEY;
1139 pkt->pts = pts;
1140
1141 return 0;
1142fail:
1144 return ret;
1145}
1146
1148{
1149 NUTContext *nut = s->priv_data;
1150 AVIOContext *bc = s->pb;
1151 int i, frame_code = 0, ret, skip;
1152 int64_t ts, back_ptr;
1153
1154 for (;;) {
1155 int64_t pos = avio_tell(bc);
1156 uint64_t tmp = nut->next_startcode;
1157 nut->next_startcode = 0;
1158
1159 if (tmp) {
1160 pos -= 8;
1161 } else {
1162 frame_code = avio_r8(bc);
1163 if (avio_feof(bc))
1164 return AVERROR_EOF;
1165 if (frame_code == 'N') {
1166 tmp = frame_code;
1167 for (i = 1; i < 8; i++)
1168 tmp = (tmp << 8) + avio_r8(bc);
1169 }
1170 }
1171 switch (tmp) {
1172 case MAIN_STARTCODE:
1173 case STREAM_STARTCODE:
1174 case INDEX_STARTCODE:
1175 skip = get_packetheader(nut, bc, 0, tmp);
1176 avio_skip(bc, skip);
1177 break;
1178 case INFO_STARTCODE:
1179 if (decode_info_header(nut) < 0)
1180 goto resync;
1181 break;
1183 if (decode_syncpoint(nut, &ts, &back_ptr) < 0)
1184 goto resync;
1185 frame_code = avio_r8(bc);
1187 case 0:
1188 ret = decode_frame(nut, pkt, frame_code);
1189 if (ret == 0)
1190 return 0;
1191 else if (ret == 1) // OK but discard packet
1192 break;
1194 default:
1195resync:
1196 av_log(s, AV_LOG_DEBUG, "syncing from %"PRId64"\n", pos);
1198 nut->last_resync_pos = avio_tell(bc);
1199 if (tmp == 0)
1200 return AVERROR_INVALIDDATA;
1201 av_log(s, AV_LOG_DEBUG, "sync\n");
1202 nut->next_startcode = tmp;
1203 }
1204 }
1205}
1206
1208 int64_t *pos_arg, int64_t pos_limit)
1209{
1210 NUTContext *nut = s->priv_data;
1211 AVIOContext *bc = s->pb;
1212 int64_t pos, pts, back_ptr;
1213 av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%"PRId64",%"PRId64")\n",
1214 stream_index, *pos_arg, pos_limit);
1215
1216 pos = *pos_arg;
1217 do {
1219 if (pos < 1) {
1220 av_log(s, AV_LOG_ERROR, "read_timestamp failed.\n");
1221 return AV_NOPTS_VALUE;
1222 }
1223 } while (decode_syncpoint(nut, &pts, &back_ptr) < 0);
1224 *pos_arg = pos - 1;
1225 av_assert0(nut->last_syncpoint_pos == *pos_arg);
1226
1227 av_log(s, AV_LOG_DEBUG, "return %"PRId64" %"PRId64"\n", pts, back_ptr);
1228 if (stream_index == -2)
1229 return back_ptr;
1230 av_assert0(stream_index == -1);
1231 return pts;
1232}
1233
1234static int read_seek(AVFormatContext *s, int stream_index,
1235 int64_t pts, int flags)
1236{
1237 NUTContext *nut = s->priv_data;
1238 AVStream *st = s->streams[stream_index];
1239 FFStream *const sti = ffstream(st);
1240 Syncpoint dummy = { .ts = pts * av_q2d(st->time_base) * AV_TIME_BASE };
1241 Syncpoint nopts_sp = { .ts = AV_NOPTS_VALUE, .back_ptr = AV_NOPTS_VALUE };
1242 Syncpoint *sp, *next_node[2] = { &nopts_sp, &nopts_sp };
1243 int64_t pos, pos2, ts;
1244 int i;
1245
1246 if (nut->flags & NUT_PIPE) {
1247 return AVERROR(ENOSYS);
1248 }
1249
1250 if (sti->index_entries) {
1252 if (index < 0)
1254 if (index < 0)
1255 return -1;
1256
1257 pos2 = sti->index_entries[index].pos;
1258 ts = sti->index_entries[index].timestamp;
1259 } else {
1261 (void **) next_node);
1262 av_log(s, AV_LOG_DEBUG, "%"PRIu64"-%"PRIu64" %"PRId64"-%"PRId64"\n",
1263 next_node[0]->pos, next_node[1]->pos, next_node[0]->ts,
1264 next_node[1]->ts);
1265 pos = ff_gen_search(s, -1, dummy.ts, next_node[0]->pos,
1266 next_node[1]->pos, next_node[1]->pos,
1267 next_node[0]->ts, next_node[1]->ts,
1269 if (pos < 0)
1270 return pos;
1271
1272 if (!(flags & AVSEEK_FLAG_BACKWARD)) {
1273 dummy.pos = pos + 16;
1274 next_node[1] = &nopts_sp;
1276 (void **) next_node);
1277 pos2 = ff_gen_search(s, -2, dummy.pos, next_node[0]->pos,
1278 next_node[1]->pos, next_node[1]->pos,
1279 next_node[0]->back_ptr, next_node[1]->back_ptr,
1281 if (pos2 >= 0)
1282 pos = pos2;
1283 // FIXME dir but I think it does not matter
1284 }
1285 dummy.pos = pos;
1287 NULL);
1288
1289 av_assert0(sp);
1290 pos2 = sp->back_ptr - 15;
1291 }
1292 av_log(s, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos2);
1294 avio_seek(s->pb, pos, SEEK_SET);
1295 nut->last_syncpoint_pos = pos;
1296 av_log(s, AV_LOG_DEBUG, "SP: %"PRId64"\n", pos);
1297 if (pos2 > pos || pos2 + 15 < pos)
1298 av_log(s, AV_LOG_ERROR, "no syncpoint at backptr pos\n");
1299 for (i = 0; i < s->nb_streams; i++)
1300 nut->stream[i].skip_until_key_frame = 1;
1301
1302 nut->last_resync_pos = 0;
1303
1304 return 0;
1305}
1306
1308 .p.name = "nut",
1309 .p.long_name = NULL_IF_CONFIG_SMALL("NUT"),
1310 .p.flags = AVFMT_SEEK_TO_PTS,
1311 .p.extensions = "nut",
1312 .p.codec_tag = ff_nut_codec_tags,
1313 .priv_data_size = sizeof(NUTContext),
1314 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
1320};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
#define class
Definition math.h:25
const FFInputFormat ff_nut_demuxer
Definition nutdec.c:1307
#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:834
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
Definition avformat.h:1727
#define AVINDEX_KEYFRAME
Definition avformat.h:628
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
@ AVFMT_DURATION_FROM_PTS
Duration accurately estimated from PTSes.
Definition avformat.h:1314
#define AVSTREAM_EVENT_FLAG_METADATA_UPDATED
Definition avformat.h:884
#define AVFMT_SEEK_TO_PTS
Seeking is based on PTS.
Definition avformat.h:520
#define AVSEEK_FLAG_BACKWARD
seek backward
Definition avformat.h:2616
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)
Definition aviobuf.c:911
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)
Definition aviobuf.c:717
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)
Definition aviobuf.c:733
unsigned int avio_rb32(AVIOContext *s)
Definition aviobuf.c:764
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:919
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:51
static int64_t duration
Definition ffplay.c:330
static int dummy
Definition ffplay.c:3754
static unsigned int nb_streams
Definition ffprobe.c:352
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:232
@ AVDISCARD_NONKEY
discard all frames except keyframes
Definition defs.h:231
@ AVDISCARD_BIDIR
discard all bidirectional frames
Definition defs.h:229
@ 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:434
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:650
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:672
@ AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS
Definition packet.h:673
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:86
#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
cl_device_type type
#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
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:88
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:264
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:1147
static uint64_t find_any_startcode(AVIOContext *bc, int64_t pos)
Definition nutdec.c:112
static uint64_t get_fourcc(AVIOContext *bc)
Definition nutdec.c:78
#define NUT_MAX_STREAMS
Definition nutdec.c:39
static int get_str(AVIOContext *bc, char *string, unsigned int maxlen)
Definition nutdec.c:44
static int decode_main_header(NUTContext *nut)
Definition nutdec.c:195
static void set_disposition_bits(AVFormatContext *avf, char *value, int stream_id)
Definition nutdec.c:490
static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index, int64_t *pos_arg, int64_t pos_limit)
Definition nutdec.c:1207
static int decode_info_header(NUTContext *nut)
Definition nutdec.c:505
static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr)
Definition nutdec.c:628
static int64_t find_duration(NUTContext *nut, int64_t filesize)
Definition nutdec.c:672
static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code)
Definition nutdec.c:1080
static int get_packetheader(NUTContext *nut, AVIOContext *bc, int calculate_checksum, uint64_t startcode)
Definition nutdec.c:92
static int nut_probe(const AVProbeData *p)
Definition nutdec.c:155
static int skip_reserved(AVIOContext *bc, int64_t pos)
Definition nutdec.c:179
static int find_and_decode_index(NUTContext *nut)
Definition nutdec.c:684
static int read_sm_data(AVFormatContext *s, AVIOContext *bc, AVPacket *pkt, int is_meta, int64_t maxpos)
Definition nutdec.c:880
static int64_t get_s(AVIOContext *bc)
Definition nutdec.c:68
static int nut_read_close(AVFormatContext *s)
Definition nutdec.c:798
static int read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
Definition nutdec.c:1234
#define GET_V(dst, check)
Definition nutdec.c:168
static int64_t find_startcode(AVIOContext *bc, uint64_t code, int64_t pos)
Find the given startcode.
Definition nutdec.c:143
static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id, uint8_t *header_idx, int frame_code)
Definition nutdec.c:997
static int decode_stream_header(NUTContext *nut)
Definition nutdec.c:381
static int nut_read_header(AVFormatContext *s)
Definition nutdec.c:812
#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:1296
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:1333
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition avformat.h:1389
AVStream ** streams
A list of all streams in the file.
Definition avformat.h:1401
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:621
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition avformat.h:622
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
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:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition avformat.h:844
AVDictionary * metadata
Definition avformat.h:846
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:805
int event_flags
Flags indicating events happening on the stream, a combination of AVSTREAM_EVENT_FLAG_*.
Definition avformat.h:877
AVRational r_frame_rate
Real base framerate of the stream.
Definition avformat.h:900
int disposition
Stream disposition - a combination of AV_DISPOSITION_* flags.
Definition avformat.h:835
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
int16_t pts_delta
Definition nut.h:70
uint16_t size_mul
Definition nut.h:68
uint8_t stream_id
Definition nut.h:67
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:89
#define width
Definition dsp.h:89
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