2 * "NUT" Container Format demuxer
3 * Copyright (c) 2004-2006 Michael Niedermayer
4 * Copyright (c) 2003 Alex Beregszaszi
6 * This file is part of Libav.
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/avstring.h"
24 #include "libavutil/bswap.h"
25 #include "libavutil/dict.h"
26 #include "libavutil/mathematics.h"
27 #include "libavutil/tree.h"
28 #include "avio_internal.h"
35 #define NUT_MAX_STREAMS 256 /* arbitrary sanity check value */
37 static int get_str(AVIOContext *bc, char *string, unsigned int maxlen)
39 unsigned int len = ffio_read_varlen(bc);
42 avio_read(bc, string, FFMIN(len, maxlen));
43 while (len > maxlen) {
49 string[FFMIN(len, maxlen - 1)] = 0;
57 static int64_t get_s(AVIOContext *bc)
59 int64_t v = ffio_read_varlen(bc) + 1;
67 static uint64_t get_fourcc(AVIOContext *bc)
69 unsigned int len = ffio_read_varlen(bc);
79 static int get_packetheader(NUTContext *nut, AVIOContext *bc,
80 int calculate_checksum, uint64_t startcode)
84 startcode = av_be2ne64(startcode);
85 startcode = ff_crc04C11DB7_update(0, (uint8_t*) &startcode, 8);
87 ffio_init_checksum(bc, ff_crc04C11DB7_update, startcode);
88 size = ffio_read_varlen(bc);
91 if (ffio_get_checksum(bc) && size > 4096)
94 ffio_init_checksum(bc, calculate_checksum ? ff_crc04C11DB7_update : NULL, 0);
99 static uint64_t find_any_startcode(AVIOContext *bc, int64_t pos)
104 /* Note, this may fail if the stream is not seekable, but that should
105 * not matter, as in this case we simply start where we currently are */
106 avio_seek(bc, pos, SEEK_SET);
107 while (!bc->eof_reached) {
108 state = (state << 8) | avio_r8(bc);
109 if ((state >> 56) != 'N')
113 case STREAM_STARTCODE:
114 case SYNCPOINT_STARTCODE:
116 case INDEX_STARTCODE:
125 * Find the given startcode.
126 * @param code the startcode
127 * @param pos the start position of the search, or -1 if the current position
128 * @return the position of the startcode or -1 if not found
130 static int64_t find_startcode(AVIOContext *bc, uint64_t code, int64_t pos)
133 uint64_t startcode = find_any_startcode(bc, pos);
134 if (startcode == code)
135 return avio_tell(bc) - 8;
136 else if (startcode == 0)
142 static int nut_probe(AVProbeData *p)
147 for (i = 0; i < p->buf_size; i++) {
148 code = (code << 8) | p->buf[i];
149 if (code == MAIN_STARTCODE)
150 return AVPROBE_SCORE_MAX;
155 #define GET_V(dst, check) \
157 tmp = ffio_read_varlen(bc); \
159 av_log(s, AV_LOG_ERROR, "Error " #dst " is (%"PRId64")\n", tmp); \
160 return AVERROR_INVALIDDATA; \
165 static int skip_reserved(AVIOContext *bc, int64_t pos)
167 pos -= avio_tell(bc);
169 avio_seek(bc, pos, SEEK_CUR);
170 return AVERROR_INVALIDDATA;
178 static int decode_main_header(NUTContext *nut)
180 AVFormatContext *s = nut->avf;
181 AVIOContext *bc = s->pb;
183 unsigned int stream_count;
185 int tmp_stream, tmp_mul, tmp_pts, tmp_size, tmp_res, tmp_head_idx;
187 end = get_packetheader(nut, bc, 1, MAIN_STARTCODE);
188 end += avio_tell(bc);
190 nut->version = ffio_read_varlen(bc);
191 if (nut->version < NUT_MIN_VERSION &&
192 nut->version > NUT_MAX_VERSION) {
193 av_log(s, AV_LOG_ERROR, "Version %d not supported.\n",
195 return AVERROR(ENOSYS);
198 GET_V(stream_count, tmp > 0 && tmp <= NUT_MAX_STREAMS);
200 nut->max_distance = ffio_read_varlen(bc);
201 if (nut->max_distance > 65536) {
202 av_log(s, AV_LOG_DEBUG, "max_distance %d\n", nut->max_distance);
203 nut->max_distance = 65536;
206 GET_V(nut->time_base_count, tmp > 0 && tmp < INT_MAX / sizeof(AVRational));
207 nut->time_base = av_malloc(nut->time_base_count * sizeof(AVRational));
209 return AVERROR(ENOMEM);
211 for (i = 0; i < nut->time_base_count; i++) {
212 GET_V(nut->time_base[i].num, tmp > 0 && tmp < (1ULL << 31));
213 GET_V(nut->time_base[i].den, tmp > 0 && tmp < (1ULL << 31));
214 if (av_gcd(nut->time_base[i].num, nut->time_base[i].den) != 1) {
215 av_log(s, AV_LOG_ERROR, "invalid time base %d/%d\n",
216 nut->time_base[i].num,
217 nut->time_base[i].den);
218 return AVERROR_INVALIDDATA;
225 for (i = 0; i < 256;) {
226 int tmp_flags = ffio_read_varlen(bc);
227 int tmp_fields = ffio_read_varlen(bc);
232 tmp_mul = ffio_read_varlen(bc);
234 tmp_stream = ffio_read_varlen(bc);
236 tmp_size = ffio_read_varlen(bc);
240 tmp_res = ffio_read_varlen(bc);
244 count = ffio_read_varlen(bc);
246 count = tmp_mul - tmp_size;
250 tmp_head_idx = ffio_read_varlen(bc);
252 while (tmp_fields-- > 8)
253 ffio_read_varlen(bc);
255 if (count == 0 || i + count > 256) {
256 av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i);
257 return AVERROR_INVALIDDATA;
259 if (tmp_stream >= stream_count) {
260 av_log(s, AV_LOG_ERROR, "illegal stream number %d >= %d\n",
261 tmp_stream, stream_count);
262 return AVERROR_INVALIDDATA;
265 for (j = 0; j < count; j++, i++) {
267 nut->frame_code[i].flags = FLAG_INVALID;
271 nut->frame_code[i].flags = tmp_flags;
272 nut->frame_code[i].pts_delta = tmp_pts;
273 nut->frame_code[i].stream_id = tmp_stream;
274 nut->frame_code[i].size_mul = tmp_mul;
275 nut->frame_code[i].size_lsb = tmp_size + j;
276 nut->frame_code[i].reserved_count = tmp_res;
277 nut->frame_code[i].header_idx = tmp_head_idx;
280 assert(nut->frame_code['N'].flags == FLAG_INVALID);
282 if (end > avio_tell(bc) + 4) {
284 GET_V(nut->header_count, tmp < 128U);
286 for (i = 1; i < nut->header_count; i++) {
288 GET_V(nut->header_len[i], tmp > 0 && tmp < 256);
289 if (rem < nut->header_len[i]) {
290 av_log(s, AV_LOG_ERROR,
291 "invalid elision header %d : %d > %d\n",
292 i, nut->header_len[i], rem);
293 return AVERROR_INVALIDDATA;
295 rem -= nut->header_len[i];
296 hdr = av_malloc(nut->header_len[i]);
298 return AVERROR(ENOMEM);
299 avio_read(bc, hdr, nut->header_len[i]);
300 nut->header[i] = hdr;
302 assert(nut->header_len[0] == 0);
305 // flags had been effectively introduced in version 4
306 if (nut->version > NUT_STABLE_VERSION) {
307 nut->flags = ffio_read_varlen(bc);
310 if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
311 av_log(s, AV_LOG_ERROR, "main header checksum mismatch\n");
312 return AVERROR_INVALIDDATA;
315 nut->stream = av_mallocz(sizeof(StreamContext) * stream_count);
317 return AVERROR(ENOMEM);
318 for (i = 0; i < stream_count; i++)
319 avformat_new_stream(s, NULL);
324 static int decode_stream_header(NUTContext *nut)
326 AVFormatContext *s = nut->avf;
327 AVIOContext *bc = s->pb;
329 int class, stream_id;
333 end = get_packetheader(nut, bc, 1, STREAM_STARTCODE);
334 end += avio_tell(bc);
336 GET_V(stream_id, tmp < s->nb_streams && !nut->stream[tmp].time_base);
337 stc = &nut->stream[stream_id];
338 st = s->streams[stream_id];
340 return AVERROR(ENOMEM);
342 class = ffio_read_varlen(bc);
343 tmp = get_fourcc(bc);
344 st->codecpar->codec_tag = tmp;
347 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
348 st->codecpar->codec_id = av_codec_get_id((const AVCodecTag * const []) {
356 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
357 st->codecpar->codec_id = av_codec_get_id((const AVCodecTag * const []) {
365 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
366 st->codecpar->codec_id = ff_codec_get_id(ff_nut_subtitle_tags, tmp);
369 st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
370 st->codecpar->codec_id = ff_codec_get_id(ff_nut_data_tags, tmp);
373 av_log(s, AV_LOG_ERROR, "unknown stream class (%d)\n", class);
374 return AVERROR(ENOSYS);
376 if (class < 3 && st->codecpar->codec_id == AV_CODEC_ID_NONE)
377 av_log(s, AV_LOG_ERROR,
378 "Unknown codec tag '0x%04x' for stream number %d\n",
379 (unsigned int) tmp, stream_id);
381 GET_V(stc->time_base_id, tmp < nut->time_base_count);
382 GET_V(stc->msb_pts_shift, tmp < 16);
383 stc->max_pts_distance = ffio_read_varlen(bc);
384 GET_V(stc->decode_delay, tmp < 1000); // sanity limit, raise this if Moore's law is true
385 ffio_read_varlen(bc); // stream flags
387 GET_V(st->codecpar->extradata_size, tmp < (1 << 30));
388 if (st->codecpar->extradata_size) {
389 st->codecpar->extradata = av_mallocz(st->codecpar->extradata_size +
390 AV_INPUT_BUFFER_PADDING_SIZE);
391 if (!st->codecpar->extradata)
392 return AVERROR(ENOMEM);
393 avio_read(bc, st->codecpar->extradata, st->codecpar->extradata_size);
396 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
397 GET_V(st->codecpar->width, tmp > 0);
398 GET_V(st->codecpar->height, tmp > 0);
399 st->sample_aspect_ratio.num = ffio_read_varlen(bc);
400 st->sample_aspect_ratio.den = ffio_read_varlen(bc);
401 if ((!st->sample_aspect_ratio.num) != (!st->sample_aspect_ratio.den)) {
402 av_log(s, AV_LOG_ERROR, "invalid aspect ratio %d/%d\n",
403 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
404 return AVERROR_INVALIDDATA;
406 ffio_read_varlen(bc); /* csp type */
407 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
408 GET_V(st->codecpar->sample_rate, tmp > 0);
409 ffio_read_varlen(bc); // samplerate_den
410 GET_V(st->codecpar->channels, tmp > 0);
412 if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
413 av_log(s, AV_LOG_ERROR,
414 "stream header %d checksum mismatch\n", stream_id);
415 return AVERROR_INVALIDDATA;
417 stc->time_base = &nut->time_base[stc->time_base_id];
418 avpriv_set_pts_info(s->streams[stream_id], 63, stc->time_base->num,
419 stc->time_base->den);
423 static void set_disposition_bits(AVFormatContext *avf, char *value,
428 for (i = 0; ff_nut_dispositions[i].flag; ++i)
429 if (!strcmp(ff_nut_dispositions[i].str, value))
430 flag = ff_nut_dispositions[i].flag;
432 av_log(avf, AV_LOG_INFO, "unknown disposition type '%s'\n", value);
433 for (i = 0; i < avf->nb_streams; ++i)
434 if (stream_id == i || stream_id == -1)
435 avf->streams[i]->disposition |= flag;
438 static int decode_info_header(NUTContext *nut)
440 AVFormatContext *s = nut->avf;
441 AVIOContext *bc = s->pb;
442 uint64_t tmp, chapter_start, chapter_len;
443 unsigned int stream_id_plus1, count;
446 char name[256], str_value[1024], type_str[256];
448 int *event_flags = NULL;
449 AVChapter *chapter = NULL;
451 AVDictionary **metadata = NULL;
452 int metadata_flag = 0;
454 end = get_packetheader(nut, bc, 1, INFO_STARTCODE);
455 end += avio_tell(bc);
457 GET_V(stream_id_plus1, tmp <= s->nb_streams);
458 chapter_id = get_s(bc);
459 chapter_start = ffio_read_varlen(bc);
460 chapter_len = ffio_read_varlen(bc);
461 count = ffio_read_varlen(bc);
463 if (chapter_id && !stream_id_plus1) {
464 int64_t start = chapter_start / nut->time_base_count;
465 chapter = avpriv_new_chapter(s, chapter_id,
466 nut->time_base[chapter_start %
467 nut->time_base_count],
468 start, start + chapter_len, NULL);
470 av_log(s, AV_LOG_ERROR, "Could not create chapter.\n");
471 return AVERROR(ENOMEM);
473 metadata = &chapter->metadata;
474 } else if (stream_id_plus1) {
475 st = s->streams[stream_id_plus1 - 1];
476 metadata = &st->metadata;
477 event_flags = &st->event_flags;
478 metadata_flag = AVSTREAM_EVENT_FLAG_METADATA_UPDATED;
480 metadata = &s->metadata;
481 event_flags = &s->event_flags;
482 metadata_flag = AVFMT_EVENT_FLAG_METADATA_UPDATED;
485 for (i = 0; i < count; i++) {
486 get_str(bc, name, sizeof(name));
490 get_str(bc, str_value, sizeof(str_value));
491 } else if (value == -2) {
492 get_str(bc, type_str, sizeof(type_str));
494 get_str(bc, str_value, sizeof(str_value));
495 } else if (value == -3) {
498 } else if (value == -4) {
500 value = ffio_read_varlen(bc);
501 } else if (value < -4) {
508 if (stream_id_plus1 > s->nb_streams) {
509 av_log(s, AV_LOG_WARNING,
510 "invalid stream id %d for info packet\n",
515 if (!strcmp(type, "UTF-8")) {
516 if (chapter_id == 0 && !strcmp(name, "Disposition")) {
517 set_disposition_bits(s, str_value, stream_id_plus1 - 1);
520 if (metadata && av_strcasecmp(name, "Uses") &&
521 av_strcasecmp(name, "Depends") && av_strcasecmp(name, "Replaces")) {
523 *event_flags |= metadata_flag;
524 av_dict_set(metadata, name, str_value, 0);
529 if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
530 av_log(s, AV_LOG_ERROR, "info header checksum mismatch\n");
531 return AVERROR_INVALIDDATA;
536 static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr)
538 AVFormatContext *s = nut->avf;
539 AVIOContext *bc = s->pb;
543 nut->last_syncpoint_pos = avio_tell(bc) - 8;
545 end = get_packetheader(nut, bc, 1, SYNCPOINT_STARTCODE);
546 end += avio_tell(bc);
548 tmp = ffio_read_varlen(bc);
549 *back_ptr = nut->last_syncpoint_pos - 16 * ffio_read_varlen(bc);
553 ff_nut_reset_ts(nut, nut->time_base[tmp % nut->time_base_count],
554 tmp / nut->time_base_count);
556 if (nut->flags & NUT_BROADCAST) {
557 tmp = ffio_read_varlen(bc);
558 av_log(s, AV_LOG_VERBOSE, "Syncpoint wallclock %"PRId64"\n",
559 av_rescale_q(tmp / nut->time_base_count,
560 nut->time_base[tmp % nut->time_base_count],
564 if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
565 av_log(s, AV_LOG_ERROR, "sync point checksum mismatch\n");
566 return AVERROR_INVALIDDATA;
569 *ts = tmp / s->nb_streams *
570 av_q2d(nut->time_base[tmp % s->nb_streams]) * AV_TIME_BASE;
572 if ((ret = ff_nut_add_sp(nut, nut->last_syncpoint_pos, *back_ptr, *ts)) < 0)
578 static int find_and_decode_index(NUTContext *nut)
580 AVFormatContext *s = nut->avf;
581 AVIOContext *bc = s->pb;
583 int i, j, syncpoint_count;
584 int64_t filesize = avio_size(bc);
586 int8_t *has_keyframe;
587 int ret = AVERROR_INVALIDDATA;
589 avio_seek(bc, filesize - 12, SEEK_SET);
590 avio_seek(bc, filesize - avio_rb64(bc), SEEK_SET);
591 if (avio_rb64(bc) != INDEX_STARTCODE) {
592 av_log(s, AV_LOG_WARNING, "no index at the end\n");
596 end = get_packetheader(nut, bc, 1, INDEX_STARTCODE);
597 end += avio_tell(bc);
599 ffio_read_varlen(bc); // max_pts
600 GET_V(syncpoint_count, tmp < INT_MAX / 8 && tmp > 0);
601 syncpoints = av_malloc(sizeof(int64_t) * syncpoint_count);
602 has_keyframe = av_malloc(sizeof(int8_t) * (syncpoint_count + 1));
603 if (!syncpoints || !has_keyframe) {
604 ret = AVERROR(ENOMEM);
607 for (i = 0; i < syncpoint_count; i++) {
608 syncpoints[i] = ffio_read_varlen(bc);
609 if (syncpoints[i] <= 0)
612 syncpoints[i] += syncpoints[i - 1];
615 for (i = 0; i < s->nb_streams; i++) {
616 int64_t last_pts = -1;
617 for (j = 0; j < syncpoint_count;) {
618 uint64_t x = ffio_read_varlen(bc);
625 if (n + x >= syncpoint_count + 1) {
626 av_log(s, AV_LOG_ERROR, "index overflow A\n");
630 has_keyframe[n++] = flag;
631 has_keyframe[n++] = !flag;
634 if (n >= syncpoint_count + 1) {
635 av_log(s, AV_LOG_ERROR, "index overflow B\n");
638 has_keyframe[n++] = x & 1;
642 if (has_keyframe[0]) {
643 av_log(s, AV_LOG_ERROR, "keyframe before first syncpoint in index\n");
646 assert(n <= syncpoint_count + 1);
647 for (; j < n && j < syncpoint_count; j++) {
648 if (has_keyframe[j]) {
649 uint64_t B, A = ffio_read_varlen(bc);
651 A = ffio_read_varlen(bc);
652 B = ffio_read_varlen(bc);
653 // eor_pts[j][i] = last_pts + A + B
656 av_add_index_entry(s->streams[i], 16 * syncpoints[j - 1],
657 last_pts + A, 0, 0, AVINDEX_KEYFRAME);
664 if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
665 av_log(s, AV_LOG_ERROR, "index checksum mismatch\n");
672 av_free(has_keyframe);
676 static int nut_read_close(AVFormatContext *s)
678 NUTContext *nut = s->priv_data;
681 av_freep(&nut->time_base);
682 av_freep(&nut->stream);
684 for (i = 1; i < nut->header_count; i++)
685 av_freep(&nut->header[i]);
690 static int nut_read_header(AVFormatContext *s)
692 NUTContext *nut = s->priv_data;
693 AVIOContext *bc = s->pb;
695 int initialized_stream_count;
702 pos = find_startcode(bc, MAIN_STARTCODE, pos) + 1;
704 av_log(s, AV_LOG_ERROR, "No main startcode found.\n");
707 } while (decode_main_header(nut) < 0);
711 for (initialized_stream_count = 0; initialized_stream_count < s->nb_streams;) {
712 pos = find_startcode(bc, STREAM_STARTCODE, pos) + 1;
714 av_log(s, AV_LOG_ERROR, "Not all stream headers found.\n");
717 if (decode_stream_header(nut) >= 0)
718 initialized_stream_count++;
724 uint64_t startcode = find_any_startcode(bc, pos);
727 if (startcode == 0) {
728 av_log(s, AV_LOG_ERROR, "EOF before video frames\n");
730 } else if (startcode == SYNCPOINT_STARTCODE) {
731 nut->next_startcode = startcode;
733 } else if (startcode != INFO_STARTCODE) {
737 decode_info_header(nut);
740 s->internal->data_offset = pos - 8;
742 if (bc->seekable & AVIO_SEEKABLE_NORMAL) {
743 int64_t orig_pos = avio_tell(bc);
744 find_and_decode_index(nut);
745 avio_seek(bc, orig_pos, SEEK_SET);
747 assert(nut->next_startcode == SYNCPOINT_STARTCODE);
749 ff_metadata_conv_ctx(s, NULL, ff_nut_metadata_conv);
756 return AVERROR_INVALIDDATA;
759 static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id,
760 uint8_t *header_idx, int frame_code)
762 AVFormatContext *s = nut->avf;
763 AVIOContext *bc = s->pb;
765 int size, flags, size_mul, pts_delta, i, reserved_count;
768 if (!(nut->flags & NUT_PIPE) &&
769 avio_tell(bc) > nut->last_syncpoint_pos + nut->max_distance) {
770 av_log(s, AV_LOG_ERROR,
771 "Last frame must have been damaged %"PRId64" > %"PRId64" + %d\n",
772 avio_tell(bc), nut->last_syncpoint_pos, nut->max_distance);
773 return AVERROR_INVALIDDATA;
776 flags = nut->frame_code[frame_code].flags;
777 size_mul = nut->frame_code[frame_code].size_mul;
778 size = nut->frame_code[frame_code].size_lsb;
779 *stream_id = nut->frame_code[frame_code].stream_id;
780 pts_delta = nut->frame_code[frame_code].pts_delta;
781 reserved_count = nut->frame_code[frame_code].reserved_count;
782 *header_idx = nut->frame_code[frame_code].header_idx;
784 if (flags & FLAG_INVALID)
785 return AVERROR_INVALIDDATA;
786 if (flags & FLAG_CODED)
787 flags ^= ffio_read_varlen(bc);
788 if (flags & FLAG_STREAM_ID) {
789 GET_V(*stream_id, tmp < s->nb_streams);
791 stc = &nut->stream[*stream_id];
792 if (flags & FLAG_CODED_PTS) {
793 int coded_pts = ffio_read_varlen(bc);
794 // FIXME check last_pts validity?
795 if (coded_pts < (1 << stc->msb_pts_shift)) {
796 *pts = ff_lsb2full(stc, coded_pts);
798 *pts = coded_pts - (1 << stc->msb_pts_shift);
800 *pts = stc->last_pts + pts_delta;
801 if (flags & FLAG_SIZE_MSB)
802 size += size_mul * ffio_read_varlen(bc);
803 if (flags & FLAG_MATCH_TIME)
805 if (flags & FLAG_HEADER_IDX)
806 *header_idx = ffio_read_varlen(bc);
807 if (flags & FLAG_RESERVED)
808 reserved_count = ffio_read_varlen(bc);
809 for (i = 0; i < reserved_count; i++)
810 ffio_read_varlen(bc);
812 if (*header_idx >= (unsigned)nut->header_count) {
813 av_log(s, AV_LOG_ERROR, "header_idx invalid\n");
814 return AVERROR_INVALIDDATA;
818 size -= nut->header_len[*header_idx];
820 if (flags & FLAG_CHECKSUM) {
821 avio_rb32(bc); // FIXME check this
822 } else if (!(nut->flags & NUT_PIPE) &&
823 size > 2 * nut->max_distance ||
824 FFABS(stc->last_pts - *pts) > stc->max_pts_distance) {
825 av_log(s, AV_LOG_ERROR, "frame size > 2max_distance and no checksum\n");
826 return AVERROR_INVALIDDATA;
829 stc->last_pts = *pts;
830 stc->last_flags = flags;
835 static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code)
837 AVFormatContext *s = nut->avf;
838 AVIOContext *bc = s->pb;
839 int size, stream_id, discard, ret;
840 int64_t pts, last_IP_pts;
844 size = decode_frame_header(nut, &pts, &stream_id, &header_idx, frame_code);
848 stc = &nut->stream[stream_id];
850 if (stc->last_flags & FLAG_KEY)
851 stc->skip_until_key_frame = 0;
853 discard = s->streams[stream_id]->discard;
854 last_IP_pts = s->streams[stream_id]->last_IP_pts;
855 if ((discard >= AVDISCARD_NONKEY && !(stc->last_flags & FLAG_KEY)) ||
856 (discard >= AVDISCARD_BIDIR && last_IP_pts != AV_NOPTS_VALUE &&
857 last_IP_pts > pts) ||
858 discard >= AVDISCARD_ALL ||
859 stc->skip_until_key_frame) {
864 ret = av_new_packet(pkt, size + nut->header_len[header_idx]);
867 if (nut->header[header_idx])
868 memcpy(pkt->data, nut->header[header_idx], nut->header_len[header_idx]);
869 pkt->pos = avio_tell(bc); // FIXME
870 avio_read(bc, pkt->data + nut->header_len[header_idx], size);
872 pkt->stream_index = stream_id;
873 if (stc->last_flags & FLAG_KEY)
874 pkt->flags |= AV_PKT_FLAG_KEY;
880 static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
882 NUTContext *nut = s->priv_data;
883 AVIOContext *bc = s->pb;
884 int i, frame_code = 0, ret, skip;
885 int64_t ts, back_ptr;
888 int64_t pos = avio_tell(bc);
889 uint64_t tmp = nut->next_startcode;
890 nut->next_startcode = 0;
895 frame_code = avio_r8(bc);
898 if (frame_code == 'N') {
900 for (i = 1; i < 8; i++)
901 tmp = (tmp << 8) + avio_r8(bc);
906 case STREAM_STARTCODE:
907 case INDEX_STARTCODE:
908 skip = get_packetheader(nut, bc, 0, tmp);
912 if (decode_info_header(nut) < 0)
915 case SYNCPOINT_STARTCODE:
916 if (decode_syncpoint(nut, &ts, &back_ptr) < 0)
918 frame_code = avio_r8(bc);
920 ret = decode_frame(nut, pkt, frame_code);
923 else if (ret == 1) // OK but discard packet
927 av_log(s, AV_LOG_DEBUG, "syncing from %"PRId64"\n", pos);
928 tmp = find_any_startcode(bc, nut->last_syncpoint_pos + 1);
930 return AVERROR_INVALIDDATA;
931 av_log(s, AV_LOG_DEBUG, "sync\n");
932 nut->next_startcode = tmp;
937 static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index,
938 int64_t *pos_arg, int64_t pos_limit)
940 NUTContext *nut = s->priv_data;
941 AVIOContext *bc = s->pb;
942 int64_t pos, pts, back_ptr;
943 av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%"PRId64",%"PRId64")\n",
944 stream_index, *pos_arg, pos_limit);
948 pos = find_startcode(bc, SYNCPOINT_STARTCODE, pos) + 1;
950 assert(nut->next_startcode == 0);
951 av_log(s, AV_LOG_ERROR, "read_timestamp failed.\n");
952 return AV_NOPTS_VALUE;
954 } while (decode_syncpoint(nut, &pts, &back_ptr) < 0);
956 assert(nut->last_syncpoint_pos == *pos_arg);
958 av_log(s, AV_LOG_DEBUG, "return %"PRId64" %"PRId64"\n", pts, back_ptr);
959 if (stream_index == -1)
961 else if (stream_index == -2)
964 return AV_NOPTS_VALUE;
967 static int read_seek(AVFormatContext *s, int stream_index,
968 int64_t pts, int flags)
970 NUTContext *nut = s->priv_data;
971 AVStream *st = s->streams[stream_index];
972 Syncpoint dummy = { .ts = pts * av_q2d(st->time_base) * AV_TIME_BASE };
973 Syncpoint nopts_sp = { .ts = AV_NOPTS_VALUE, .back_ptr = AV_NOPTS_VALUE };
974 Syncpoint *sp, *next_node[2] = { &nopts_sp, &nopts_sp };
975 int64_t pos, pos2, ts;
978 if (nut->flags & NUT_PIPE) {
979 return AVERROR(ENOSYS);
982 if (st->index_entries) {
983 int index = av_index_search_timestamp(st, pts, flags);
987 pos2 = st->index_entries[index].pos;
988 ts = st->index_entries[index].timestamp;
990 av_tree_find(nut->syncpoints, &dummy,
991 (int (*)(void *, const void *)) ff_nut_sp_pts_cmp,
992 (void **) next_node);
993 av_log(s, AV_LOG_DEBUG, "%"PRIu64"-%"PRIu64" %"PRId64"-%"PRId64"\n",
994 next_node[0]->pos, next_node[1]->pos, next_node[0]->ts,
996 pos = ff_gen_search(s, -1, dummy.ts, next_node[0]->pos,
997 next_node[1]->pos, next_node[1]->pos,
998 next_node[0]->ts, next_node[1]->ts,
999 AVSEEK_FLAG_BACKWARD, &ts, nut_read_timestamp);
1001 if (!(flags & AVSEEK_FLAG_BACKWARD)) {
1002 dummy.pos = pos + 16;
1003 next_node[1] = &nopts_sp;
1004 av_tree_find(nut->syncpoints, &dummy,
1005 (int (*)(void *, const void *)) ff_nut_sp_pos_cmp,
1006 (void **) next_node);
1007 pos2 = ff_gen_search(s, -2, dummy.pos, next_node[0]->pos,
1008 next_node[1]->pos, next_node[1]->pos,
1009 next_node[0]->back_ptr, next_node[1]->back_ptr,
1010 flags, &ts, nut_read_timestamp);
1013 // FIXME dir but I think it does not matter
1016 sp = av_tree_find(nut->syncpoints, &dummy,
1017 (int (*)(void *, const void *)) ff_nut_sp_pos_cmp,
1021 pos2 = sp->back_ptr - 15;
1023 av_log(NULL, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos2);
1024 pos = find_startcode(s->pb, SYNCPOINT_STARTCODE, pos2);
1025 avio_seek(s->pb, pos, SEEK_SET);
1026 av_log(NULL, AV_LOG_DEBUG, "SP: %"PRId64"\n", pos);
1027 if (pos2 > pos || pos2 + 15 < pos)
1028 av_log(NULL, AV_LOG_ERROR, "no syncpoint at backptr pos\n");
1029 for (i = 0; i < s->nb_streams; i++)
1030 nut->stream[i].skip_until_key_frame = 1;
1035 AVInputFormat ff_nut_demuxer = {
1037 .long_name = NULL_IF_CONFIG_SMALL("NUT"),
1038 .priv_data_size = sizeof(NUTContext),
1039 .read_probe = nut_probe,
1040 .read_header = nut_read_header,
1041 .read_packet = nut_read_packet,
1042 .read_close = nut_read_close,
1043 .read_seek = read_seek,
1044 .extensions = "nut",
1045 .codec_tag = ff_nut_codec_tags,