2 * "NUT" Container Format demuxer
3 * Copyright (c) 2004-2006 Michael Niedermayer
4 * Copyright (c) 2003 Alex Beregszaszi
6 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavutil/avstring.h"
24 #include "libavutil/avassert.h"
25 #include "libavutil/bswap.h"
26 #include "libavutil/dict.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/mathematics.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/tree.h"
31 #include "libavcodec/bytestream.h"
32 #include "avio_internal.h"
38 #define NUT_MAX_STREAMS 256 /* arbitrary sanity check value */
40 static int64_t nut_read_timestamp(AVFormatContext
*s
, int stream_index
,
41 int64_t *pos_arg
, int64_t pos_limit
);
43 static int get_str(AVIOContext
*bc
, char *string
, unsigned int maxlen
)
45 unsigned int len
= ffio_read_varlen(bc
);
48 avio_read(bc
, string
, FFMIN(len
, maxlen
));
49 while (len
> maxlen
) {
57 string
[FFMIN(len
, maxlen
- 1)] = 0;
67 static int64_t get_s(AVIOContext
*bc
)
69 int64_t v
= ffio_read_varlen(bc
) + 1;
77 static uint64_t get_fourcc(AVIOContext
*bc
)
79 unsigned int len
= ffio_read_varlen(bc
);
86 av_log(NULL
, AV_LOG_ERROR
, "Unsupported fourcc length %d\n", len
);
91 static int get_packetheader(NUTContext
*nut
, AVIOContext
*bc
,
92 int calculate_checksum
, uint64_t startcode
)
96 startcode
= av_be2ne64(startcode
);
97 startcode
= ff_crc04C11DB7_update(0, (uint8_t*) &startcode
, 8);
99 ffio_init_checksum(bc
, ff_crc04C11DB7_update
, startcode
);
100 size
= ffio_read_varlen(bc
);
103 if (ffio_get_checksum(bc
) && size
> 4096)
106 ffio_init_checksum(bc
, calculate_checksum
? ff_crc04C11DB7_update
: NULL
, 0);
111 static uint64_t find_any_startcode(AVIOContext
*bc
, int64_t pos
)
116 /* Note, this may fail if the stream is not seekable, but that should
117 * not matter, as in this case we simply start where we currently are */
118 avio_seek(bc
, pos
, SEEK_SET
);
119 while (!avio_feof(bc
)) {
120 state
= (state
<< 8) | avio_r8(bc
);
121 if ((state
>> 56) != 'N')
125 case STREAM_STARTCODE
:
126 case SYNCPOINT_STARTCODE
:
128 case INDEX_STARTCODE
:
137 * Find the given startcode.
138 * @param code the startcode
139 * @param pos the start position of the search, or -1 if the current position
140 * @return the position of the startcode or -1 if not found
142 static int64_t find_startcode(AVIOContext
*bc
, uint64_t code
, int64_t pos
)
145 uint64_t startcode
= find_any_startcode(bc
, pos
);
146 if (startcode
== code
)
147 return avio_tell(bc
) - 8;
148 else if (startcode
== 0)
154 static int nut_probe(const AVProbeData
*p
)
158 for (i
= 0; i
< p
->buf_size
-8; i
++) {
159 if (AV_RB32(p
->buf
+i
) != MAIN_STARTCODE
>>32)
161 if (AV_RB32(p
->buf
+i
+4) == (MAIN_STARTCODE
& 0xFFFFFFFF))
162 return AVPROBE_SCORE_MAX
;
167 #define GET_V(dst, check) \
169 tmp = ffio_read_varlen(bc); \
171 av_log(s, AV_LOG_ERROR, "Error " #dst " is (%"PRId64")\n", tmp); \
172 ret = AVERROR_INVALIDDATA; \
178 static int skip_reserved(AVIOContext
*bc
, int64_t pos
)
180 pos
-= avio_tell(bc
);
182 avio_seek(bc
, pos
, SEEK_CUR
);
183 return AVERROR_INVALIDDATA
;
187 return AVERROR_INVALIDDATA
;
194 static int decode_main_header(NUTContext
*nut
)
196 AVFormatContext
*s
= nut
->avf
;
197 AVIOContext
*bc
= s
->pb
;
198 uint64_t tmp
, end
, length
;
199 unsigned int stream_count
;
200 int i
, j
, count
, ret
;
201 int tmp_stream
, tmp_mul
, tmp_pts
, tmp_size
, tmp_res
, tmp_head_idx
;
203 length
= get_packetheader(nut
, bc
, 1, MAIN_STARTCODE
);
204 if (length
== (uint64_t)-1)
205 return AVERROR_INVALIDDATA
;
206 end
= length
+ avio_tell(bc
);
208 nut
->version
= ffio_read_varlen(bc
);
209 if (nut
->version
< NUT_MIN_VERSION
||
210 nut
->version
> NUT_MAX_VERSION
) {
211 av_log(s
, AV_LOG_ERROR
, "Version %d not supported.\n",
213 return AVERROR(ENOSYS
);
215 if (nut
->version
> 3)
216 nut
->minor_version
= ffio_read_varlen(bc
);
218 GET_V(stream_count
, tmp
> 0 && tmp
<= NUT_MAX_STREAMS
);
220 nut
->max_distance
= ffio_read_varlen(bc
);
221 if (nut
->max_distance
> 65536) {
222 av_log(s
, AV_LOG_DEBUG
, "max_distance %d\n", nut
->max_distance
);
223 nut
->max_distance
= 65536;
226 GET_V(nut
->time_base_count
, tmp
> 0 && tmp
< INT_MAX
/ sizeof(AVRational
) && tmp
< length
/2);
227 nut
->time_base
= av_malloc_array(nut
->time_base_count
, sizeof(AVRational
));
229 return AVERROR(ENOMEM
);
231 for (i
= 0; i
< nut
->time_base_count
; i
++) {
232 GET_V(nut
->time_base
[i
].num
, tmp
> 0 && tmp
< (1ULL << 31));
233 GET_V(nut
->time_base
[i
].den
, tmp
> 0 && tmp
< (1ULL << 31));
234 if (av_gcd(nut
->time_base
[i
].num
, nut
->time_base
[i
].den
) != 1) {
235 av_log(s
, AV_LOG_ERROR
, "invalid time base %d/%d\n",
236 nut
->time_base
[i
].num
,
237 nut
->time_base
[i
].den
);
238 ret
= AVERROR_INVALIDDATA
;
246 for (i
= 0; i
< 256;) {
247 int tmp_flags
= ffio_read_varlen(bc
);
248 int tmp_fields
= ffio_read_varlen(bc
);
249 if (tmp_fields
< 0) {
250 av_log(s
, AV_LOG_ERROR
, "fields %d is invalid\n", tmp_fields
);
251 ret
= AVERROR_INVALIDDATA
;
258 tmp_mul
= ffio_read_varlen(bc
);
260 tmp_stream
= ffio_read_varlen(bc
);
262 tmp_size
= ffio_read_varlen(bc
);
266 tmp_res
= ffio_read_varlen(bc
);
270 count
= ffio_read_varlen(bc
);
272 count
= tmp_mul
- (unsigned)tmp_size
;
276 tmp_head_idx
= ffio_read_varlen(bc
);
278 while (tmp_fields
-- > 8) {
279 if (bc
->eof_reached
) {
280 av_log(s
, AV_LOG_ERROR
, "reached EOF while decoding main header\n");
281 ret
= AVERROR_INVALIDDATA
;
284 ffio_read_varlen(bc
);
287 if (count
<= 0 || count
> 256 - (i
<= 'N') - i
) {
288 av_log(s
, AV_LOG_ERROR
, "illegal count %d at %d\n", count
, i
);
289 ret
= AVERROR_INVALIDDATA
;
292 if (tmp_stream
>= stream_count
) {
293 av_log(s
, AV_LOG_ERROR
, "illegal stream number %d >= %d\n",
294 tmp_stream
, stream_count
);
295 ret
= AVERROR_INVALIDDATA
;
298 if (tmp_size
< 0 || tmp_size
> INT_MAX
- count
) {
299 av_log(s
, AV_LOG_ERROR
, "illegal size\n");
300 ret
= AVERROR_INVALIDDATA
;
304 for (j
= 0; j
< count
; j
++, i
++) {
306 nut
->frame_code
[i
].flags
= FLAG_INVALID
;
310 nut
->frame_code
[i
].flags
= tmp_flags
;
311 nut
->frame_code
[i
].pts_delta
= tmp_pts
;
312 nut
->frame_code
[i
].stream_id
= tmp_stream
;
313 nut
->frame_code
[i
].size_mul
= tmp_mul
;
314 nut
->frame_code
[i
].size_lsb
= tmp_size
+ j
;
315 nut
->frame_code
[i
].reserved_count
= tmp_res
;
316 nut
->frame_code
[i
].header_idx
= tmp_head_idx
;
319 av_assert0(nut
->frame_code
['N'].flags
== FLAG_INVALID
);
321 if (end
> avio_tell(bc
) + 4) {
323 GET_V(nut
->header_count
, tmp
< 128U);
325 for (i
= 1; i
< nut
->header_count
; i
++) {
327 GET_V(nut
->header_len
[i
], tmp
> 0 && tmp
< 256);
328 if (rem
< nut
->header_len
[i
]) {
329 av_log(s
, AV_LOG_ERROR
,
330 "invalid elision header %d : %d > %d\n",
331 i
, nut
->header_len
[i
], rem
);
332 ret
= AVERROR_INVALIDDATA
;
335 rem
-= nut
->header_len
[i
];
336 hdr
= av_malloc(nut
->header_len
[i
]);
338 ret
= AVERROR(ENOMEM
);
341 avio_read(bc
, hdr
, nut
->header_len
[i
]);
342 nut
->header
[i
] = hdr
;
344 av_assert0(nut
->header_len
[0] == 0);
347 // flags had been effectively introduced in version 4
348 if (nut
->version
> 3 && end
> avio_tell(bc
) + 4) {
349 nut
->flags
= ffio_read_varlen(bc
);
352 if (skip_reserved(bc
, end
) || ffio_get_checksum(bc
)) {
353 av_log(s
, AV_LOG_ERROR
, "main header checksum mismatch\n");
354 ret
= AVERROR_INVALIDDATA
;
358 nut
->stream
= av_calloc(stream_count
, sizeof(StreamContext
));
360 ret
= AVERROR(ENOMEM
);
363 for (i
= 0; i
< stream_count
; i
++) {
364 if (!avformat_new_stream(s
, NULL
)) {
365 ret
= AVERROR(ENOMEM
);
372 av_freep(&nut
->time_base
);
373 for (i
= 1; i
< nut
->header_count
; i
++) {
374 av_freep(&nut
->header
[i
]);
376 nut
->header_count
= 0;
380 static int decode_stream_header(NUTContext
*nut
)
382 AVFormatContext
*s
= nut
->avf
;
383 AVIOContext
*bc
= s
->pb
;
385 int class, stream_id
, ret
;
389 end
= get_packetheader(nut
, bc
, 1, STREAM_STARTCODE
);
390 end
+= avio_tell(bc
);
392 GET_V(stream_id
, tmp
< s
->nb_streams
&& !nut
->stream
[tmp
].time_base
);
393 stc
= &nut
->stream
[stream_id
];
394 st
= s
->streams
[stream_id
];
396 return AVERROR(ENOMEM
);
398 class = ffio_read_varlen(bc
);
399 tmp
= get_fourcc(bc
);
400 st
->codecpar
->codec_tag
= tmp
;
403 st
->codecpar
->codec_type
= AVMEDIA_TYPE_VIDEO
;
404 st
->codecpar
->codec_id
= av_codec_get_id((const AVCodecTag
* const []) {
407 ff_codec_movvideo_tags
,
413 st
->codecpar
->codec_type
= AVMEDIA_TYPE_AUDIO
;
414 st
->codecpar
->codec_id
= av_codec_get_id((const AVCodecTag
* const []) {
417 ff_nut_audio_extra_tags
,
423 st
->codecpar
->codec_type
= AVMEDIA_TYPE_SUBTITLE
;
424 st
->codecpar
->codec_id
= ff_codec_get_id(ff_nut_subtitle_tags
, tmp
);
427 st
->codecpar
->codec_type
= AVMEDIA_TYPE_DATA
;
428 st
->codecpar
->codec_id
= ff_codec_get_id(ff_nut_data_tags
, tmp
);
431 av_log(s
, AV_LOG_ERROR
, "unknown stream class (%d)\n", class);
432 return AVERROR(ENOSYS
);
434 if (class < 3 && st
->codecpar
->codec_id
== AV_CODEC_ID_NONE
)
435 av_log(s
, AV_LOG_ERROR
,
436 "Unknown codec tag '0x%04x' for stream number %d\n",
437 (unsigned int) tmp
, stream_id
);
439 GET_V(stc
->time_base_id
, tmp
< nut
->time_base_count
);
440 GET_V(stc
->msb_pts_shift
, tmp
< 16);
441 stc
->max_pts_distance
= ffio_read_varlen(bc
);
442 GET_V(stc
->decode_delay
, tmp
< 1000); // sanity limit, raise this if Moore's law is true
443 st
->codecpar
->video_delay
= stc
->decode_delay
;
444 ffio_read_varlen(bc
); // stream flags
446 GET_V(st
->codecpar
->extradata_size
, tmp
< (1 << 30));
447 if (st
->codecpar
->extradata_size
) {
448 ret
= ff_get_extradata(s
, st
->codecpar
, bc
,
449 st
->codecpar
->extradata_size
);
454 if (st
->codecpar
->codec_type
== AVMEDIA_TYPE_VIDEO
) {
455 GET_V(st
->codecpar
->width
, tmp
> 0);
456 GET_V(st
->codecpar
->height
, tmp
> 0);
457 st
->sample_aspect_ratio
.num
= ffio_read_varlen(bc
);
458 st
->sample_aspect_ratio
.den
= ffio_read_varlen(bc
);
459 if ((!st
->sample_aspect_ratio
.num
) != (!st
->sample_aspect_ratio
.den
)) {
460 av_log(s
, AV_LOG_ERROR
, "invalid aspect ratio %d/%d\n",
461 st
->sample_aspect_ratio
.num
, st
->sample_aspect_ratio
.den
);
462 ret
= AVERROR_INVALIDDATA
;
465 ffio_read_varlen(bc
); /* csp type */
466 } else if (st
->codecpar
->codec_type
== AVMEDIA_TYPE_AUDIO
) {
467 GET_V(st
->codecpar
->sample_rate
, tmp
> 0);
468 ffio_read_varlen(bc
); // samplerate_den
469 GET_V(st
->codecpar
->ch_layout
.nb_channels
, tmp
> 0);
471 if (skip_reserved(bc
, end
) || ffio_get_checksum(bc
)) {
472 av_log(s
, AV_LOG_ERROR
,
473 "stream header %d checksum mismatch\n", stream_id
);
474 ret
= AVERROR_INVALIDDATA
;
477 stc
->time_base
= &nut
->time_base
[stc
->time_base_id
];
478 avpriv_set_pts_info(s
->streams
[stream_id
], 63, stc
->time_base
->num
,
479 stc
->time_base
->den
);
482 if (st
&& st
->codecpar
) {
483 av_freep(&st
->codecpar
->extradata
);
484 st
->codecpar
->extradata_size
= 0;
489 static void set_disposition_bits(AVFormatContext
*avf
, char *value
,
494 for (i
= 0; ff_nut_dispositions
[i
].flag
; ++i
)
495 if (!strcmp(ff_nut_dispositions
[i
].str
, value
))
496 flag
= ff_nut_dispositions
[i
].flag
;
498 av_log(avf
, AV_LOG_INFO
, "unknown disposition type '%s'\n", value
);
499 for (i
= 0; i
< avf
->nb_streams
; ++i
)
500 if (stream_id
== i
|| stream_id
== -1)
501 avf
->streams
[i
]->disposition
|= flag
;
504 static int decode_info_header(NUTContext
*nut
)
506 AVFormatContext
*s
= nut
->avf
;
507 AVIOContext
*bc
= s
->pb
;
508 uint64_t tmp
, chapter_start
, chapter_len
;
509 unsigned int stream_id_plus1
, count
;
511 int64_t chapter_id
, value
, end
;
512 char name
[256], str_value
[1024], type_str
[256];
514 int *event_flags
= NULL
;
515 AVChapter
*chapter
= NULL
;
517 AVDictionary
**metadata
= NULL
;
518 int metadata_flag
= 0;
520 end
= get_packetheader(nut
, bc
, 1, INFO_STARTCODE
);
521 end
+= avio_tell(bc
);
523 GET_V(stream_id_plus1
, tmp
<= s
->nb_streams
);
524 chapter_id
= get_s(bc
);
525 chapter_start
= ffio_read_varlen(bc
);
526 chapter_len
= ffio_read_varlen(bc
);
527 count
= ffio_read_varlen(bc
);
529 if (chapter_id
&& !stream_id_plus1
) {
530 int64_t start
= chapter_start
/ nut
->time_base_count
;
531 chapter
= avpriv_new_chapter(s
, chapter_id
,
532 nut
->time_base
[chapter_start
%
533 nut
->time_base_count
],
534 start
, start
+ chapter_len
, NULL
);
536 av_log(s
, AV_LOG_ERROR
, "Could not create chapter.\n");
537 return AVERROR(ENOMEM
);
539 metadata
= &chapter
->metadata
;
540 } else if (stream_id_plus1
) {
541 st
= s
->streams
[stream_id_plus1
- 1];
542 metadata
= &st
->metadata
;
543 event_flags
= &st
->event_flags
;
544 metadata_flag
= AVSTREAM_EVENT_FLAG_METADATA_UPDATED
;
546 metadata
= &s
->metadata
;
547 event_flags
= &s
->event_flags
;
548 metadata_flag
= AVFMT_EVENT_FLAG_METADATA_UPDATED
;
551 for (i
= 0; i
< count
; i
++) {
552 ret
= get_str(bc
, name
, sizeof(name
));
554 av_log(s
, AV_LOG_ERROR
, "get_str failed while decoding info header\n");
562 ret
= get_str(bc
, str_value
, sizeof(str_value
));
563 } else if (value
== -2) {
564 ret
= get_str(bc
, type_str
, sizeof(type_str
));
566 av_log(s
, AV_LOG_ERROR
, "get_str failed while decoding info header\n");
570 ret
= get_str(bc
, str_value
, sizeof(str_value
));
571 } else if (value
== -3) {
574 } else if (value
== -4) {
576 value
= ffio_read_varlen(bc
);
577 } else if (value
< -4) {
585 av_log(s
, AV_LOG_ERROR
, "get_str failed while decoding info header\n");
589 if (stream_id_plus1
> s
->nb_streams
) {
590 av_log(s
, AV_LOG_WARNING
,
591 "invalid stream id %d for info packet\n",
596 if (!strcmp(type
, "UTF-8")) {
597 if (chapter_id
== 0 && !strcmp(name
, "Disposition")) {
598 set_disposition_bits(s
, str_value
, stream_id_plus1
- 1);
602 if (stream_id_plus1
&& !strcmp(name
, "r_frame_rate")) {
603 sscanf(str_value
, "%d/%d", &st
->r_frame_rate
.num
, &st
->r_frame_rate
.den
);
604 if (st
->r_frame_rate
.num
>= 1000LL*st
->r_frame_rate
.den
||
605 st
->r_frame_rate
.num
< 0 || st
->r_frame_rate
.den
< 0)
606 st
->r_frame_rate
.num
= st
->r_frame_rate
.den
= 0;
610 if (metadata
&& av_strcasecmp(name
, "Uses") &&
611 av_strcasecmp(name
, "Depends") && av_strcasecmp(name
, "Replaces")) {
613 *event_flags
|= metadata_flag
;
614 av_dict_set(metadata
, name
, str_value
, 0);
619 if (skip_reserved(bc
, end
) || ffio_get_checksum(bc
)) {
620 av_log(s
, AV_LOG_ERROR
, "info header checksum mismatch\n");
621 return AVERROR_INVALIDDATA
;
624 return FFMIN(ret
, 0);
627 static int decode_syncpoint(NUTContext
*nut
, int64_t *ts
, int64_t *back_ptr
)
629 AVFormatContext
*s
= nut
->avf
;
630 AVIOContext
*bc
= s
->pb
;
635 nut
->last_syncpoint_pos
= avio_tell(bc
) - 8;
637 end
= get_packetheader(nut
, bc
, 1, SYNCPOINT_STARTCODE
);
638 end
+= avio_tell(bc
);
640 tmp
= ffio_read_varlen(bc
);
641 *back_ptr
= nut
->last_syncpoint_pos
- 16 * ffio_read_varlen(bc
);
643 return AVERROR_INVALIDDATA
;
645 ff_nut_reset_ts(nut
, nut
->time_base
[tmp
% nut
->time_base_count
],
646 tmp
/ nut
->time_base_count
);
648 if (nut
->flags
& NUT_BROADCAST
) {
649 tmp
= ffio_read_varlen(bc
);
650 av_log(s
, AV_LOG_VERBOSE
, "Syncpoint wallclock %"PRId64
"\n",
651 av_rescale_q(tmp
/ nut
->time_base_count
,
652 nut
->time_base
[tmp
% nut
->time_base_count
],
656 if (skip_reserved(bc
, end
) || ffio_get_checksum(bc
)) {
657 av_log(s
, AV_LOG_ERROR
, "sync point checksum mismatch\n");
658 return AVERROR_INVALIDDATA
;
661 *ts
= tmp
/ nut
->time_base_count
*
662 av_q2d(nut
->time_base
[tmp
% nut
->time_base_count
]) * AV_TIME_BASE
;
664 if ((ret
= ff_nut_add_sp(nut
, nut
->last_syncpoint_pos
, *back_ptr
, *ts
)) < 0)
670 //FIXME calculate exactly, this is just a good approximation.
671 static int64_t find_duration(NUTContext
*nut
, int64_t filesize
)
673 AVFormatContext
*s
= nut
->avf
;
674 int64_t duration
= 0;
676 ff_find_last_ts(s
, -1, &duration
, NULL
, nut_read_timestamp
);
679 s
->duration_estimation_method
= AVFMT_DURATION_FROM_PTS
;
683 static int find_and_decode_index(NUTContext
*nut
)
685 AVFormatContext
*s
= nut
->avf
;
686 AVIOContext
*bc
= s
->pb
;
688 int i
, j
, syncpoint_count
;
689 int64_t filesize
= avio_size(bc
);
690 int64_t *syncpoints
= NULL
;
692 int8_t *has_keyframe
= NULL
;
693 int ret
= AVERROR_INVALIDDATA
;
698 avio_seek(bc
, filesize
- 12, SEEK_SET
);
699 avio_seek(bc
, filesize
- avio_rb64(bc
), SEEK_SET
);
700 if (avio_rb64(bc
) != INDEX_STARTCODE
) {
701 av_log(s
, AV_LOG_WARNING
, "no index at the end\n");
704 s
->duration
= find_duration(nut
, filesize
);
708 end
= get_packetheader(nut
, bc
, 1, INDEX_STARTCODE
);
709 end
+= avio_tell(bc
);
711 max_pts
= ffio_read_varlen(bc
);
712 s
->duration
= av_rescale_q(max_pts
/ nut
->time_base_count
,
713 nut
->time_base
[max_pts
% nut
->time_base_count
],
715 s
->duration_estimation_method
= AVFMT_DURATION_FROM_PTS
;
717 GET_V(syncpoint_count
, tmp
< INT_MAX
/ 8 && tmp
> 0);
718 syncpoints
= av_malloc_array(syncpoint_count
, sizeof(int64_t));
719 has_keyframe
= av_malloc_array(syncpoint_count
+ 1, sizeof(int8_t));
720 if (!syncpoints
|| !has_keyframe
) {
721 ret
= AVERROR(ENOMEM
);
724 for (i
= 0; i
< syncpoint_count
; i
++) {
725 syncpoints
[i
] = ffio_read_varlen(bc
);
726 if (syncpoints
[i
] <= 0)
729 syncpoints
[i
] += syncpoints
[i
- 1];
732 for (i
= 0; i
< s
->nb_streams
; i
++) {
733 int64_t last_pts
= -1;
734 for (j
= 0; j
< syncpoint_count
;) {
735 uint64_t x
= ffio_read_varlen(bc
);
742 if (n
+ x
>= syncpoint_count
+ 1) {
743 av_log(s
, AV_LOG_ERROR
, "index overflow A %d + %"PRIu64
" >= %d\n", n
, x
, syncpoint_count
+ 1);
747 has_keyframe
[n
++] = flag
;
748 has_keyframe
[n
++] = !flag
;
751 av_log(s
, AV_LOG_ERROR
, "index: x %"PRIu64
" is invalid\n", x
);
755 if (n
>= syncpoint_count
+ 1) {
756 av_log(s
, AV_LOG_ERROR
, "index overflow B\n");
759 has_keyframe
[n
++] = x
& 1;
763 if (has_keyframe
[0]) {
764 av_log(s
, AV_LOG_ERROR
, "keyframe before first syncpoint in index\n");
767 av_assert0(n
<= syncpoint_count
+ 1);
768 for (; j
< n
&& j
< syncpoint_count
; j
++) {
769 if (has_keyframe
[j
]) {
770 uint64_t B
, A
= ffio_read_varlen(bc
);
772 A
= ffio_read_varlen(bc
);
773 B
= ffio_read_varlen(bc
);
774 // eor_pts[j][i] = last_pts + A + B
777 av_add_index_entry(s
->streams
[i
], 16 * syncpoints
[j
- 1],
778 last_pts
+ A
, 0, 0, AVINDEX_KEYFRAME
);
785 if (skip_reserved(bc
, end
) || ffio_get_checksum(bc
)) {
786 av_log(s
, AV_LOG_ERROR
, "index checksum mismatch\n");
793 av_free(has_keyframe
);
797 static int nut_read_close(AVFormatContext
*s
)
799 NUTContext
*nut
= s
->priv_data
;
802 av_freep(&nut
->time_base
);
803 av_freep(&nut
->stream
);
805 for (i
= 1; i
< nut
->header_count
; i
++)
806 av_freep(&nut
->header
[i
]);
811 static int nut_read_header(AVFormatContext
*s
)
813 NUTContext
*nut
= s
->priv_data
;
814 AVIOContext
*bc
= s
->pb
;
816 int initialized_stream_count
, ret
;
824 if (ret
== AVERROR(ENOMEM
))
827 pos
= find_startcode(bc
, MAIN_STARTCODE
, pos
) + 1;
829 av_log(s
, AV_LOG_ERROR
, "No main startcode found.\n");
830 return AVERROR_INVALIDDATA
;
832 } while ((ret
= decode_main_header(nut
)) < 0);
836 for (initialized_stream_count
= 0; initialized_stream_count
< s
->nb_streams
;) {
837 pos
= find_startcode(bc
, STREAM_STARTCODE
, pos
) + 1;
839 av_log(s
, AV_LOG_ERROR
, "Not all stream headers found.\n");
840 return AVERROR_INVALIDDATA
;
842 if (decode_stream_header(nut
) >= 0)
843 initialized_stream_count
++;
849 uint64_t startcode
= find_any_startcode(bc
, pos
);
852 if (startcode
== 0) {
853 av_log(s
, AV_LOG_ERROR
, "EOF before video frames\n");
854 return AVERROR_INVALIDDATA
;
855 } else if (startcode
== SYNCPOINT_STARTCODE
) {
856 nut
->next_startcode
= startcode
;
858 } else if (startcode
!= INFO_STARTCODE
) {
862 decode_info_header(nut
);
865 ffformatcontext(s
)->data_offset
= pos
- 8;
867 if (bc
->seekable
& AVIO_SEEKABLE_NORMAL
) {
868 int64_t orig_pos
= avio_tell(bc
);
869 find_and_decode_index(nut
);
870 avio_seek(bc
, orig_pos
, SEEK_SET
);
872 av_assert0(nut
->next_startcode
== SYNCPOINT_STARTCODE
);
874 ff_metadata_conv_ctx(s
, NULL
, ff_nut_metadata_conv
);
879 static int read_sm_data(AVFormatContext
*s
, AVIOContext
*bc
, AVPacket
*pkt
, int is_meta
, int64_t maxpos
)
881 int count
= ffio_read_varlen(bc
);
889 for (i
=0; i
<count
; i
++) {
890 uint8_t name
[256], str_value
[256], type_str
[256];
892 if (avio_tell(bc
) >= maxpos
)
893 return AVERROR_INVALIDDATA
;
894 ret
= get_str(bc
, name
, sizeof(name
));
896 av_log(s
, AV_LOG_ERROR
, "get_str failed while reading sm data\n");
902 ret
= get_str(bc
, str_value
, sizeof(str_value
));
904 av_log(s
, AV_LOG_ERROR
, "get_str failed while reading sm data\n");
907 av_log(s
, AV_LOG_WARNING
, "Unknown string %s / %s\n", name
, str_value
);
908 } else if (value
== -2) {
910 int64_t v64
, value_len
;
912 ret
= get_str(bc
, type_str
, sizeof(type_str
));
914 av_log(s
, AV_LOG_ERROR
, "get_str failed while reading sm data\n");
917 value_len
= ffio_read_varlen(bc
);
918 if (value_len
< 0 || value_len
>= maxpos
- avio_tell(bc
))
919 return AVERROR_INVALIDDATA
;
920 if (!strcmp(name
, "Palette")) {
921 dst
= av_packet_new_side_data(pkt
, AV_PKT_DATA_PALETTE
, value_len
);
922 } else if (!strcmp(name
, "Extradata")) {
923 dst
= av_packet_new_side_data(pkt
, AV_PKT_DATA_NEW_EXTRADATA
, value_len
);
924 } else if (sscanf(name
, "CodecSpecificSide%"SCNd64
"", &v64
) == 1) {
925 dst
= av_packet_new_side_data(pkt
, AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL
, value_len
+ 8);
927 return AVERROR(ENOMEM
);
930 } else if (!strcmp(name
, "ChannelLayout") && value_len
== 8) {
934 av_log(s
, AV_LOG_WARNING
, "Unknown data %s / %s\n", name
, type_str
);
935 avio_skip(bc
, value_len
);
939 return AVERROR(ENOMEM
);
940 avio_read(bc
, dst
, value_len
);
941 } else if (value
== -3) {
943 } else if (value
== -4) {
944 value
= ffio_read_varlen(bc
);
945 } else if (value
< -4) {
948 if (!strcmp(name
, "SkipStart")) {
950 } else if (!strcmp(name
, "SkipEnd")) {
952 } else if (!strcmp(name
, "Channels")) {
954 } else if (!strcmp(name
, "SampleRate")) {
956 } else if (!strcmp(name
, "Width")) {
958 } else if (!strcmp(name
, "Height")) {
961 av_log(s
, AV_LOG_WARNING
, "Unknown integer %s\n", name
);
966 if (sample_rate
|| width
|| height
) {
967 uint8_t *dst
= av_packet_new_side_data(pkt
, AV_PKT_DATA_PARAM_CHANGE
, 16);
969 return AVERROR(ENOMEM
);
970 bytestream_put_le32(&dst
,
971 AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE
*(!!sample_rate
) +
972 AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS
*(!!(width
|height
))
975 bytestream_put_le32(&dst
, sample_rate
);
976 if (width
|| height
){
977 bytestream_put_le32(&dst
, width
);
978 bytestream_put_le32(&dst
, height
);
982 if (skip_start
|| skip_end
) {
983 uint8_t *dst
= av_packet_new_side_data(pkt
, AV_PKT_DATA_SKIP_SAMPLES
, 10);
985 return AVERROR(ENOMEM
);
986 AV_WL32(dst
, skip_start
);
987 AV_WL32(dst
+4, skip_end
);
990 if (avio_tell(bc
) >= maxpos
)
991 return AVERROR_INVALIDDATA
;
996 static int decode_frame_header(NUTContext
*nut
, int64_t *pts
, int *stream_id
,
997 uint8_t *header_idx
, int frame_code
)
999 AVFormatContext
*s
= nut
->avf
;
1000 AVIOContext
*bc
= s
->pb
;
1002 int size
, flags
, size_mul
, pts_delta
, i
, reserved_count
, ret
;
1005 if (!(nut
->flags
& NUT_PIPE
) &&
1006 avio_tell(bc
) > nut
->last_syncpoint_pos
+ nut
->max_distance
) {
1007 av_log(s
, AV_LOG_ERROR
,
1008 "Last frame must have been damaged %"PRId64
" > %"PRId64
" + %d\n",
1009 avio_tell(bc
), nut
->last_syncpoint_pos
, nut
->max_distance
);
1010 return AVERROR_INVALIDDATA
;
1013 flags
= nut
->frame_code
[frame_code
].flags
;
1014 size_mul
= nut
->frame_code
[frame_code
].size_mul
;
1015 size
= nut
->frame_code
[frame_code
].size_lsb
;
1016 *stream_id
= nut
->frame_code
[frame_code
].stream_id
;
1017 pts_delta
= nut
->frame_code
[frame_code
].pts_delta
;
1018 reserved_count
= nut
->frame_code
[frame_code
].reserved_count
;
1019 *header_idx
= nut
->frame_code
[frame_code
].header_idx
;
1021 if (flags
& FLAG_INVALID
)
1022 return AVERROR_INVALIDDATA
;
1023 if (flags
& FLAG_CODED
)
1024 flags
^= ffio_read_varlen(bc
);
1025 if (flags
& FLAG_STREAM_ID
) {
1026 GET_V(*stream_id
, tmp
< s
->nb_streams
);
1028 stc
= &nut
->stream
[*stream_id
];
1029 if (flags
& FLAG_CODED_PTS
) {
1030 int64_t coded_pts
= ffio_read_varlen(bc
);
1031 // FIXME check last_pts validity?
1032 if (coded_pts
< (1LL << stc
->msb_pts_shift
)) {
1033 *pts
= ff_lsb2full(stc
, coded_pts
);
1035 *pts
= coded_pts
- (1LL << stc
->msb_pts_shift
);
1037 *pts
= stc
->last_pts
+ pts_delta
;
1038 if (flags
& FLAG_SIZE_MSB
)
1039 size
+= size_mul
* ffio_read_varlen(bc
);
1040 if (flags
& FLAG_MATCH_TIME
)
1042 if (flags
& FLAG_HEADER_IDX
)
1043 *header_idx
= ffio_read_varlen(bc
);
1044 if (flags
& FLAG_RESERVED
)
1045 reserved_count
= ffio_read_varlen(bc
);
1046 for (i
= 0; i
< reserved_count
; i
++) {
1047 if (bc
->eof_reached
) {
1048 av_log(s
, AV_LOG_ERROR
, "reached EOF while decoding frame header\n");
1049 return AVERROR_INVALIDDATA
;
1051 ffio_read_varlen(bc
);
1054 if (*header_idx
>= (unsigned)nut
->header_count
) {
1055 av_log(s
, AV_LOG_ERROR
, "header_idx invalid\n");
1056 return AVERROR_INVALIDDATA
;
1060 size
-= nut
->header_len
[*header_idx
];
1062 if (flags
& FLAG_CHECKSUM
) {
1063 avio_rb32(bc
); // FIXME check this
1064 } else if (!(nut
->flags
& NUT_PIPE
) &&
1065 size
> 2 * nut
->max_distance
||
1066 FFABS(stc
->last_pts
- *pts
) > stc
->max_pts_distance
) {
1067 av_log(s
, AV_LOG_ERROR
, "frame size > 2max_distance and no checksum\n");
1068 return AVERROR_INVALIDDATA
;
1071 stc
->last_pts
= *pts
;
1072 stc
->last_flags
= flags
;
1079 static int decode_frame(NUTContext
*nut
, AVPacket
*pkt
, int frame_code
)
1081 AVFormatContext
*s
= nut
->avf
;
1082 AVIOContext
*bc
= s
->pb
;
1083 int size
, stream_id
, discard
, ret
;
1084 int64_t pts
, last_IP_pts
;
1088 size
= decode_frame_header(nut
, &pts
, &stream_id
, &header_idx
, frame_code
);
1092 stc
= &nut
->stream
[stream_id
];
1094 if (stc
->last_flags
& FLAG_KEY
)
1095 stc
->skip_until_key_frame
= 0;
1097 discard
= s
->streams
[stream_id
]->discard
;
1098 last_IP_pts
= ffstream(s
->streams
[stream_id
])->last_IP_pts
;
1099 if ((discard
>= AVDISCARD_NONKEY
&& !(stc
->last_flags
& FLAG_KEY
)) ||
1100 (discard
>= AVDISCARD_BIDIR
&& last_IP_pts
!= AV_NOPTS_VALUE
&&
1101 last_IP_pts
> pts
) ||
1102 discard
>= AVDISCARD_ALL
||
1103 stc
->skip_until_key_frame
) {
1104 avio_skip(bc
, size
);
1108 ret
= av_new_packet(pkt
, size
+ nut
->header_len
[header_idx
]);
1111 if (nut
->header
[header_idx
])
1112 memcpy(pkt
->data
, nut
->header
[header_idx
], nut
->header_len
[header_idx
]);
1113 pkt
->pos
= avio_tell(bc
); // FIXME
1114 if (stc
->last_flags
& FLAG_SM_DATA
) {
1116 if (read_sm_data(s
, bc
, pkt
, 0, pkt
->pos
+ size
) < 0) {
1117 ret
= AVERROR_INVALIDDATA
;
1120 if (read_sm_data(s
, bc
, pkt
, 1, pkt
->pos
+ size
) < 0) {
1121 ret
= AVERROR_INVALIDDATA
;
1124 sm_size
= avio_tell(bc
) - pkt
->pos
;
1128 ret
= avio_read(bc
, pkt
->data
+ nut
->header_len
[header_idx
], size
);
1133 av_shrink_packet(pkt
, nut
->header_len
[header_idx
] + ret
);
1135 pkt
->stream_index
= stream_id
;
1136 if (stc
->last_flags
& FLAG_KEY
)
1137 pkt
->flags
|= AV_PKT_FLAG_KEY
;
1142 av_packet_unref(pkt
);
1146 static int nut_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
1148 NUTContext
*nut
= s
->priv_data
;
1149 AVIOContext
*bc
= s
->pb
;
1150 int i
, frame_code
= 0, ret
, skip
;
1151 int64_t ts
, back_ptr
;
1154 int64_t pos
= avio_tell(bc
);
1155 uint64_t tmp
= nut
->next_startcode
;
1156 nut
->next_startcode
= 0;
1161 frame_code
= avio_r8(bc
);
1164 if (frame_code
== 'N') {
1166 for (i
= 1; i
< 8; i
++)
1167 tmp
= (tmp
<< 8) + avio_r8(bc
);
1171 case MAIN_STARTCODE
:
1172 case STREAM_STARTCODE
:
1173 case INDEX_STARTCODE
:
1174 skip
= get_packetheader(nut
, bc
, 0, tmp
);
1175 avio_skip(bc
, skip
);
1177 case INFO_STARTCODE
:
1178 if (decode_info_header(nut
) < 0)
1181 case SYNCPOINT_STARTCODE
:
1182 if (decode_syncpoint(nut
, &ts
, &back_ptr
) < 0)
1184 frame_code
= avio_r8(bc
);
1186 ret
= decode_frame(nut
, pkt
, frame_code
);
1189 else if (ret
== 1) // OK but discard packet
1193 av_log(s
, AV_LOG_DEBUG
, "syncing from %"PRId64
"\n", pos
);
1194 tmp
= find_any_startcode(bc
, FFMAX(nut
->last_syncpoint_pos
, nut
->last_resync_pos
) + 1);
1195 nut
->last_resync_pos
= avio_tell(bc
);
1197 return AVERROR_INVALIDDATA
;
1198 av_log(s
, AV_LOG_DEBUG
, "sync\n");
1199 nut
->next_startcode
= tmp
;
1204 static int64_t nut_read_timestamp(AVFormatContext
*s
, int stream_index
,
1205 int64_t *pos_arg
, int64_t pos_limit
)
1207 NUTContext
*nut
= s
->priv_data
;
1208 AVIOContext
*bc
= s
->pb
;
1209 int64_t pos
, pts
, back_ptr
;
1210 av_log(s
, AV_LOG_DEBUG
, "read_timestamp(X,%d,%"PRId64
",%"PRId64
")\n",
1211 stream_index
, *pos_arg
, pos_limit
);
1215 pos
= find_startcode(bc
, SYNCPOINT_STARTCODE
, pos
) + 1;
1217 av_log(s
, AV_LOG_ERROR
, "read_timestamp failed.\n");
1218 return AV_NOPTS_VALUE
;
1220 } while (decode_syncpoint(nut
, &pts
, &back_ptr
) < 0);
1222 av_assert0(nut
->last_syncpoint_pos
== *pos_arg
);
1224 av_log(s
, AV_LOG_DEBUG
, "return %"PRId64
" %"PRId64
"\n", pts
, back_ptr
);
1225 if (stream_index
== -2)
1227 av_assert0(stream_index
== -1);
1231 static int read_seek(AVFormatContext
*s
, int stream_index
,
1232 int64_t pts
, int flags
)
1234 NUTContext
*nut
= s
->priv_data
;
1235 AVStream
*st
= s
->streams
[stream_index
];
1236 FFStream
*const sti
= ffstream(st
);
1237 Syncpoint dummy
= { .ts
= pts
* av_q2d(st
->time_base
) * AV_TIME_BASE
};
1238 Syncpoint nopts_sp
= { .ts
= AV_NOPTS_VALUE
, .back_ptr
= AV_NOPTS_VALUE
};
1239 Syncpoint
*sp
, *next_node
[2] = { &nopts_sp
, &nopts_sp
};
1240 int64_t pos
, pos2
, ts
;
1243 if (nut
->flags
& NUT_PIPE
) {
1244 return AVERROR(ENOSYS
);
1247 if (sti
->index_entries
) {
1248 int index
= av_index_search_timestamp(st
, pts
, flags
);
1250 index
= av_index_search_timestamp(st
, pts
, flags
^ AVSEEK_FLAG_BACKWARD
);
1254 pos2
= sti
->index_entries
[index
].pos
;
1255 ts
= sti
->index_entries
[index
].timestamp
;
1257 av_tree_find(nut
->syncpoints
, &dummy
, ff_nut_sp_pts_cmp
,
1258 (void **) next_node
);
1259 av_log(s
, AV_LOG_DEBUG
, "%"PRIu64
"-%"PRIu64
" %"PRId64
"-%"PRId64
"\n",
1260 next_node
[0]->pos
, next_node
[1]->pos
, next_node
[0]->ts
,
1262 pos
= ff_gen_search(s
, -1, dummy
.ts
, next_node
[0]->pos
,
1263 next_node
[1]->pos
, next_node
[1]->pos
,
1264 next_node
[0]->ts
, next_node
[1]->ts
,
1265 AVSEEK_FLAG_BACKWARD
, &ts
, nut_read_timestamp
);
1269 if (!(flags
& AVSEEK_FLAG_BACKWARD
)) {
1270 dummy
.pos
= pos
+ 16;
1271 next_node
[1] = &nopts_sp
;
1272 av_tree_find(nut
->syncpoints
, &dummy
, ff_nut_sp_pos_cmp
,
1273 (void **) next_node
);
1274 pos2
= ff_gen_search(s
, -2, dummy
.pos
, next_node
[0]->pos
,
1275 next_node
[1]->pos
, next_node
[1]->pos
,
1276 next_node
[0]->back_ptr
, next_node
[1]->back_ptr
,
1277 flags
, &ts
, nut_read_timestamp
);
1280 // FIXME dir but I think it does not matter
1283 sp
= av_tree_find(nut
->syncpoints
, &dummy
, ff_nut_sp_pos_cmp
,
1287 pos2
= sp
->back_ptr
- 15;
1289 av_log(s
, AV_LOG_DEBUG
, "SEEKTO: %"PRId64
"\n", pos2
);
1290 pos
= find_startcode(s
->pb
, SYNCPOINT_STARTCODE
, pos2
);
1291 avio_seek(s
->pb
, pos
, SEEK_SET
);
1292 nut
->last_syncpoint_pos
= pos
;
1293 av_log(s
, AV_LOG_DEBUG
, "SP: %"PRId64
"\n", pos
);
1294 if (pos2
> pos
|| pos2
+ 15 < pos
)
1295 av_log(s
, AV_LOG_ERROR
, "no syncpoint at backptr pos\n");
1296 for (i
= 0; i
< s
->nb_streams
; i
++)
1297 nut
->stream
[i
].skip_until_key_frame
= 1;
1299 nut
->last_resync_pos
= 0;
1304 const FFInputFormat ff_nut_demuxer
= {
1306 .p
.long_name
= NULL_IF_CONFIG_SMALL("NUT"),
1307 .p
.flags
= AVFMT_SEEK_TO_PTS
,
1308 .p
.extensions
= "nut",
1309 .p
.codec_tag
= ff_nut_codec_tags
,
1310 .priv_data_size
= sizeof(NUTContext
),
1311 .flags_internal
= FF_INFMT_FLAG_INIT_CLEANUP
,
1312 .read_probe
= nut_probe
,
1313 .read_header
= nut_read_header
,
1314 .read_packet
= nut_read_packet
,
1315 .read_close
= nut_read_close
,
1316 .read_seek
= read_seek
,