00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "libavutil/avstring.h"
00024 #include "libavutil/avassert.h"
00025 #include "libavutil/bswap.h"
00026 #include "libavutil/dict.h"
00027 #include "libavutil/mathematics.h"
00028 #include "libavutil/tree.h"
00029 #include "avio_internal.h"
00030 #include "nut.h"
00031
00032 #define NUT_MAX_STREAMS 256
00033
00034 static int get_str(AVIOContext *bc, char *string, unsigned int maxlen)
00035 {
00036 unsigned int len = ffio_read_varlen(bc);
00037
00038 if (len && maxlen)
00039 avio_read(bc, string, FFMIN(len, maxlen));
00040 while (len > maxlen) {
00041 avio_r8(bc);
00042 len--;
00043 }
00044
00045 if (maxlen)
00046 string[FFMIN(len, maxlen - 1)] = 0;
00047
00048 if (maxlen == len)
00049 return -1;
00050 else
00051 return 0;
00052 }
00053
00054 static int64_t get_s(AVIOContext *bc)
00055 {
00056 int64_t v = ffio_read_varlen(bc) + 1;
00057
00058 if (v & 1)
00059 return -(v >> 1);
00060 else
00061 return (v >> 1);
00062 }
00063
00064 static uint64_t get_fourcc(AVIOContext *bc)
00065 {
00066 unsigned int len = ffio_read_varlen(bc);
00067
00068 if (len == 2)
00069 return avio_rl16(bc);
00070 else if (len == 4)
00071 return avio_rl32(bc);
00072 else
00073 return -1;
00074 }
00075
00076 #ifdef TRACE
00077 static inline uint64_t get_v_trace(AVIOContext *bc, char *file,
00078 char *func, int line)
00079 {
00080 uint64_t v = ffio_read_varlen(bc);
00081
00082 av_log(NULL, AV_LOG_DEBUG, "get_v %5"PRId64" / %"PRIX64" in %s %s:%d\n",
00083 v, v, file, func, line);
00084 return v;
00085 }
00086
00087 static inline int64_t get_s_trace(AVIOContext *bc, char *file,
00088 char *func, int line)
00089 {
00090 int64_t v = get_s(bc);
00091
00092 av_log(NULL, AV_LOG_DEBUG, "get_s %5"PRId64" / %"PRIX64" in %s %s:%d\n",
00093 v, v, file, func, line);
00094 return v;
00095 }
00096
00097 static inline uint64_t get_vb_trace(AVIOContext *bc, char *file,
00098 char *func, int line)
00099 {
00100 uint64_t v = get_vb(bc);
00101
00102 av_log(NULL, AV_LOG_DEBUG, "get_vb %5"PRId64" / %"PRIX64" in %s %s:%d\n",
00103 v, v, file, func, line);
00104 return v;
00105 }
00106 #define ffio_read_varlen(bc) get_v_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00107 #define get_s(bc) get_s_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00108 #define get_vb(bc) get_vb_trace(bc, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00109 #endif
00110
00111 static int get_packetheader(NUTContext *nut, AVIOContext *bc,
00112 int calculate_checksum, uint64_t startcode)
00113 {
00114 int64_t size;
00115
00116
00117 startcode = av_be2ne64(startcode);
00118 startcode = ff_crc04C11DB7_update(0, (uint8_t*) &startcode, 8);
00119
00120 ffio_init_checksum(bc, ff_crc04C11DB7_update, startcode);
00121 size = ffio_read_varlen(bc);
00122 if (size > 4096)
00123 avio_rb32(bc);
00124 if (ffio_get_checksum(bc) && size > 4096)
00125 return -1;
00126
00127 ffio_init_checksum(bc, calculate_checksum ? ff_crc04C11DB7_update : NULL, 0);
00128
00129 return size;
00130 }
00131
00132 static uint64_t find_any_startcode(AVIOContext *bc, int64_t pos)
00133 {
00134 uint64_t state = 0;
00135
00136 if (pos >= 0)
00137
00138
00139 avio_seek(bc, pos, SEEK_SET);
00140 while (!url_feof(bc)) {
00141 state = (state << 8) | avio_r8(bc);
00142 if ((state >> 56) != 'N')
00143 continue;
00144 switch (state) {
00145 case MAIN_STARTCODE:
00146 case STREAM_STARTCODE:
00147 case SYNCPOINT_STARTCODE:
00148 case INFO_STARTCODE:
00149 case INDEX_STARTCODE:
00150 return state;
00151 }
00152 }
00153
00154 return 0;
00155 }
00156
00163 static int64_t find_startcode(AVIOContext *bc, uint64_t code, int64_t pos)
00164 {
00165 for (;;) {
00166 uint64_t startcode = find_any_startcode(bc, pos);
00167 if (startcode == code)
00168 return avio_tell(bc) - 8;
00169 else if (startcode == 0)
00170 return -1;
00171 pos = -1;
00172 }
00173 }
00174
00175 static int nut_probe(AVProbeData *p)
00176 {
00177 int i;
00178 uint64_t code = 0;
00179
00180 for (i = 0; i < p->buf_size; i++) {
00181 code = (code << 8) | p->buf[i];
00182 if (code == MAIN_STARTCODE)
00183 return AVPROBE_SCORE_MAX;
00184 }
00185 return 0;
00186 }
00187
00188 #define GET_V(dst, check) \
00189 tmp = ffio_read_varlen(bc); \
00190 if (!(check)) { \
00191 av_log(s, AV_LOG_ERROR, "Error " #dst " is (%"PRId64")\n", tmp); \
00192 return -1; \
00193 } \
00194 dst = tmp;
00195
00196 static int skip_reserved(AVIOContext *bc, int64_t pos)
00197 {
00198 pos -= avio_tell(bc);
00199 if (pos < 0) {
00200 avio_seek(bc, pos, SEEK_CUR);
00201 return -1;
00202 } else {
00203 while (pos--)
00204 avio_r8(bc);
00205 return 0;
00206 }
00207 }
00208
00209 static int decode_main_header(NUTContext *nut)
00210 {
00211 AVFormatContext *s = nut->avf;
00212 AVIOContext *bc = s->pb;
00213 uint64_t tmp, end;
00214 unsigned int stream_count;
00215 int i, j, count;
00216 int tmp_stream, tmp_mul, tmp_pts, tmp_size, tmp_res, tmp_head_idx;
00217
00218 end = get_packetheader(nut, bc, 1, MAIN_STARTCODE);
00219 end += avio_tell(bc);
00220
00221 GET_V(tmp, tmp >= 2 && tmp <= 3)
00222 GET_V(stream_count, tmp > 0 && tmp <= NUT_MAX_STREAMS)
00223
00224 nut->max_distance = ffio_read_varlen(bc);
00225 if (nut->max_distance > 65536) {
00226 av_log(s, AV_LOG_DEBUG, "max_distance %d\n", nut->max_distance);
00227 nut->max_distance = 65536;
00228 }
00229
00230 GET_V(nut->time_base_count, tmp > 0 && tmp < INT_MAX / sizeof(AVRational))
00231 nut->time_base = av_malloc(nut->time_base_count * sizeof(AVRational));
00232
00233 for (i = 0; i < nut->time_base_count; i++) {
00234 GET_V(nut->time_base[i].num, tmp > 0 && tmp < (1ULL << 31))
00235 GET_V(nut->time_base[i].den, tmp > 0 && tmp < (1ULL << 31))
00236 if (av_gcd(nut->time_base[i].num, nut->time_base[i].den) != 1) {
00237 av_log(s, AV_LOG_ERROR, "time base invalid\n");
00238 return AVERROR_INVALIDDATA;
00239 }
00240 }
00241 tmp_pts = 0;
00242 tmp_mul = 1;
00243 tmp_stream = 0;
00244 tmp_head_idx = 0;
00245 for (i = 0; i < 256;) {
00246 int tmp_flags = ffio_read_varlen(bc);
00247 int tmp_fields = ffio_read_varlen(bc);
00248
00249 if (tmp_fields > 0)
00250 tmp_pts = get_s(bc);
00251 if (tmp_fields > 1)
00252 tmp_mul = ffio_read_varlen(bc);
00253 if (tmp_fields > 2)
00254 tmp_stream = ffio_read_varlen(bc);
00255 if (tmp_fields > 3)
00256 tmp_size = ffio_read_varlen(bc);
00257 else
00258 tmp_size = 0;
00259 if (tmp_fields > 4)
00260 tmp_res = ffio_read_varlen(bc);
00261 else
00262 tmp_res = 0;
00263 if (tmp_fields > 5)
00264 count = ffio_read_varlen(bc);
00265 else
00266 count = tmp_mul - tmp_size;
00267 if (tmp_fields > 6)
00268 get_s(bc);
00269 if (tmp_fields > 7)
00270 tmp_head_idx = ffio_read_varlen(bc);
00271
00272 while (tmp_fields-- > 8)
00273 ffio_read_varlen(bc);
00274
00275 if (count == 0 || i + count > 256) {
00276 av_log(s, AV_LOG_ERROR, "illegal count %d at %d\n", count, i);
00277 return AVERROR_INVALIDDATA;
00278 }
00279 if (tmp_stream >= stream_count) {
00280 av_log(s, AV_LOG_ERROR, "illegal stream number\n");
00281 return AVERROR_INVALIDDATA;
00282 }
00283
00284 for (j = 0; j < count; j++, i++) {
00285 if (i == 'N') {
00286 nut->frame_code[i].flags = FLAG_INVALID;
00287 j--;
00288 continue;
00289 }
00290 nut->frame_code[i].flags = tmp_flags;
00291 nut->frame_code[i].pts_delta = tmp_pts;
00292 nut->frame_code[i].stream_id = tmp_stream;
00293 nut->frame_code[i].size_mul = tmp_mul;
00294 nut->frame_code[i].size_lsb = tmp_size + j;
00295 nut->frame_code[i].reserved_count = tmp_res;
00296 nut->frame_code[i].header_idx = tmp_head_idx;
00297 }
00298 }
00299 av_assert0(nut->frame_code['N'].flags == FLAG_INVALID);
00300
00301 if (end > avio_tell(bc) + 4) {
00302 int rem = 1024;
00303 GET_V(nut->header_count, tmp < 128U)
00304 nut->header_count++;
00305 for (i = 1; i < nut->header_count; i++) {
00306 uint8_t *hdr;
00307 GET_V(nut->header_len[i], tmp > 0 && tmp < 256);
00308 rem -= nut->header_len[i];
00309 if (rem < 0) {
00310 av_log(s, AV_LOG_ERROR, "invalid elision header\n");
00311 return AVERROR_INVALIDDATA;
00312 }
00313 hdr = av_malloc(nut->header_len[i]);
00314 if (!hdr)
00315 return AVERROR(ENOMEM);
00316 avio_read(bc, hdr, nut->header_len[i]);
00317 nut->header[i] = hdr;
00318 }
00319 av_assert0(nut->header_len[0] == 0);
00320 }
00321
00322 if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
00323 av_log(s, AV_LOG_ERROR, "main header checksum mismatch\n");
00324 return AVERROR_INVALIDDATA;
00325 }
00326
00327 nut->stream = av_mallocz(sizeof(StreamContext) * stream_count);
00328 for (i = 0; i < stream_count; i++)
00329 avformat_new_stream(s, NULL);
00330
00331 return 0;
00332 }
00333
00334 static int decode_stream_header(NUTContext *nut)
00335 {
00336 AVFormatContext *s = nut->avf;
00337 AVIOContext *bc = s->pb;
00338 StreamContext *stc;
00339 int class, stream_id;
00340 uint64_t tmp, end;
00341 AVStream *st;
00342
00343 end = get_packetheader(nut, bc, 1, STREAM_STARTCODE);
00344 end += avio_tell(bc);
00345
00346 GET_V(stream_id, tmp < s->nb_streams && !nut->stream[tmp].time_base);
00347 stc = &nut->stream[stream_id];
00348 st = s->streams[stream_id];
00349 if (!st)
00350 return AVERROR(ENOMEM);
00351
00352 class = ffio_read_varlen(bc);
00353 tmp = get_fourcc(bc);
00354 st->codec->codec_tag = tmp;
00355 switch (class) {
00356 case 0:
00357 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00358 st->codec->codec_id = av_codec_get_id((const AVCodecTag * const []) {
00359 ff_codec_bmp_tags,
00360 ff_nut_video_tags,
00361 0
00362 },
00363 tmp);
00364 break;
00365 case 1:
00366 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00367 st->codec->codec_id = ff_codec_get_id(ff_codec_wav_tags, tmp);
00368 break;
00369 case 2:
00370 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
00371 st->codec->codec_id = ff_codec_get_id(ff_nut_subtitle_tags, tmp);
00372 break;
00373 case 3:
00374 st->codec->codec_type = AVMEDIA_TYPE_DATA;
00375 break;
00376 default:
00377 av_log(s, AV_LOG_ERROR, "unknown stream class (%d)\n", class);
00378 return -1;
00379 }
00380 if (class < 3 && st->codec->codec_id == CODEC_ID_NONE)
00381 av_log(s, AV_LOG_ERROR,
00382 "Unknown codec tag '0x%04x' for stream number %d\n",
00383 (unsigned int) tmp, stream_id);
00384
00385 GET_V(stc->time_base_id, tmp < nut->time_base_count);
00386 GET_V(stc->msb_pts_shift, tmp < 16);
00387 stc->max_pts_distance = ffio_read_varlen(bc);
00388 GET_V(stc->decode_delay, tmp < 1000);
00389 st->codec->has_b_frames = stc->decode_delay;
00390 ffio_read_varlen(bc);
00391
00392 GET_V(st->codec->extradata_size, tmp < (1 << 30));
00393 if (st->codec->extradata_size) {
00394 st->codec->extradata = av_mallocz(st->codec->extradata_size +
00395 FF_INPUT_BUFFER_PADDING_SIZE);
00396 avio_read(bc, st->codec->extradata, st->codec->extradata_size);
00397 }
00398
00399 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00400 GET_V(st->codec->width, tmp > 0)
00401 GET_V(st->codec->height, tmp > 0)
00402 st->sample_aspect_ratio.num = ffio_read_varlen(bc);
00403 st->sample_aspect_ratio.den = ffio_read_varlen(bc);
00404 if ((!st->sample_aspect_ratio.num) != (!st->sample_aspect_ratio.den)) {
00405 av_log(s, AV_LOG_ERROR, "invalid aspect ratio %d/%d\n",
00406 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
00407 return -1;
00408 }
00409 ffio_read_varlen(bc);
00410 } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
00411 GET_V(st->codec->sample_rate, tmp > 0)
00412 ffio_read_varlen(bc);
00413 GET_V(st->codec->channels, tmp > 0)
00414 }
00415 if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
00416 av_log(s, AV_LOG_ERROR,
00417 "stream header %d checksum mismatch\n", stream_id);
00418 return -1;
00419 }
00420 stc->time_base = &nut->time_base[stc->time_base_id];
00421 avpriv_set_pts_info(s->streams[stream_id], 63, stc->time_base->num,
00422 stc->time_base->den);
00423 return 0;
00424 }
00425
00426 static void set_disposition_bits(AVFormatContext *avf, char *value,
00427 int stream_id)
00428 {
00429 int flag = 0, i;
00430
00431 for (i = 0; ff_nut_dispositions[i].flag; ++i)
00432 if (!strcmp(ff_nut_dispositions[i].str, value))
00433 flag = ff_nut_dispositions[i].flag;
00434 if (!flag)
00435 av_log(avf, AV_LOG_INFO, "unknown disposition type '%s'\n", value);
00436 for (i = 0; i < avf->nb_streams; ++i)
00437 if (stream_id == i || stream_id == -1)
00438 avf->streams[i]->disposition |= flag;
00439 }
00440
00441 static int decode_info_header(NUTContext *nut)
00442 {
00443 AVFormatContext *s = nut->avf;
00444 AVIOContext *bc = s->pb;
00445 uint64_t tmp, chapter_start, chapter_len;
00446 unsigned int stream_id_plus1, count;
00447 int chapter_id, i;
00448 int64_t value, end;
00449 char name[256], str_value[1024], type_str[256];
00450 const char *type;
00451 AVChapter *chapter = NULL;
00452 AVStream *st = NULL;
00453 AVDictionary **metadata = NULL;
00454
00455 end = get_packetheader(nut, bc, 1, INFO_STARTCODE);
00456 end += avio_tell(bc);
00457
00458 GET_V(stream_id_plus1, tmp <= s->nb_streams)
00459 chapter_id = get_s(bc);
00460 chapter_start = ffio_read_varlen(bc);
00461 chapter_len = ffio_read_varlen(bc);
00462 count = ffio_read_varlen(bc);
00463
00464 if (chapter_id && !stream_id_plus1) {
00465 int64_t start = chapter_start / nut->time_base_count;
00466 chapter = avpriv_new_chapter(s, chapter_id,
00467 nut->time_base[chapter_start %
00468 nut->time_base_count],
00469 start, start + chapter_len, NULL);
00470 metadata = &chapter->metadata;
00471 } else if (stream_id_plus1) {
00472 st = s->streams[stream_id_plus1 - 1];
00473 metadata = &st->metadata;
00474 } else
00475 metadata = &s->metadata;
00476
00477 for (i = 0; i < count; i++) {
00478 get_str(bc, name, sizeof(name));
00479 value = get_s(bc);
00480 if (value == -1) {
00481 type = "UTF-8";
00482 get_str(bc, str_value, sizeof(str_value));
00483 } else if (value == -2) {
00484 get_str(bc, type_str, sizeof(type_str));
00485 type = type_str;
00486 get_str(bc, str_value, sizeof(str_value));
00487 } else if (value == -3) {
00488 type = "s";
00489 value = get_s(bc);
00490 } else if (value == -4) {
00491 type = "t";
00492 value = ffio_read_varlen(bc);
00493 } else if (value < -4) {
00494 type = "r";
00495 get_s(bc);
00496 } else {
00497 type = "v";
00498 }
00499
00500 if (stream_id_plus1 > s->nb_streams) {
00501 av_log(s, AV_LOG_ERROR, "invalid stream id for info packet\n");
00502 continue;
00503 }
00504
00505 if (!strcmp(type, "UTF-8")) {
00506 if (chapter_id == 0 && !strcmp(name, "Disposition")) {
00507 set_disposition_bits(s, str_value, stream_id_plus1 - 1);
00508 continue;
00509 }
00510 if (metadata && av_strcasecmp(name, "Uses") &&
00511 av_strcasecmp(name, "Depends") && av_strcasecmp(name, "Replaces"))
00512 av_dict_set(metadata, name, str_value, 0);
00513 }
00514 }
00515
00516 if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
00517 av_log(s, AV_LOG_ERROR, "info header checksum mismatch\n");
00518 return -1;
00519 }
00520 return 0;
00521 }
00522
00523 static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr)
00524 {
00525 AVFormatContext *s = nut->avf;
00526 AVIOContext *bc = s->pb;
00527 int64_t end, tmp;
00528
00529 nut->last_syncpoint_pos = avio_tell(bc) - 8;
00530
00531 end = get_packetheader(nut, bc, 1, SYNCPOINT_STARTCODE);
00532 end += avio_tell(bc);
00533
00534 tmp = ffio_read_varlen(bc);
00535 *back_ptr = nut->last_syncpoint_pos - 16 * ffio_read_varlen(bc);
00536 if (*back_ptr < 0)
00537 return -1;
00538
00539 ff_nut_reset_ts(nut, nut->time_base[tmp % nut->time_base_count],
00540 tmp / nut->time_base_count);
00541
00542 if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
00543 av_log(s, AV_LOG_ERROR, "sync point checksum mismatch\n");
00544 return -1;
00545 }
00546
00547 *ts = tmp / s->nb_streams *
00548 av_q2d(nut->time_base[tmp % s->nb_streams]) * AV_TIME_BASE;
00549 ff_nut_add_sp(nut, nut->last_syncpoint_pos, *back_ptr, *ts);
00550
00551 return 0;
00552 }
00553
00554 static int find_and_decode_index(NUTContext *nut)
00555 {
00556 AVFormatContext *s = nut->avf;
00557 AVIOContext *bc = s->pb;
00558 uint64_t tmp, end;
00559 int i, j, syncpoint_count;
00560 int64_t filesize = avio_size(bc);
00561 int64_t *syncpoints;
00562 int8_t *has_keyframe;
00563 int ret = -1;
00564
00565 avio_seek(bc, filesize - 12, SEEK_SET);
00566 avio_seek(bc, filesize - avio_rb64(bc), SEEK_SET);
00567 if (avio_rb64(bc) != INDEX_STARTCODE) {
00568 av_log(s, AV_LOG_ERROR, "no index at the end\n");
00569 return -1;
00570 }
00571
00572 end = get_packetheader(nut, bc, 1, INDEX_STARTCODE);
00573 end += avio_tell(bc);
00574
00575 ffio_read_varlen(bc);
00576 GET_V(syncpoint_count, tmp < INT_MAX / 8 && tmp > 0)
00577 syncpoints = av_malloc(sizeof(int64_t) * syncpoint_count);
00578 has_keyframe = av_malloc(sizeof(int8_t) * (syncpoint_count + 1));
00579 for (i = 0; i < syncpoint_count; i++) {
00580 syncpoints[i] = ffio_read_varlen(bc);
00581 if (syncpoints[i] <= 0)
00582 goto fail;
00583 if (i)
00584 syncpoints[i] += syncpoints[i - 1];
00585 }
00586
00587 for (i = 0; i < s->nb_streams; i++) {
00588 int64_t last_pts = -1;
00589 for (j = 0; j < syncpoint_count;) {
00590 uint64_t x = ffio_read_varlen(bc);
00591 int type = x & 1;
00592 int n = j;
00593 x >>= 1;
00594 if (type) {
00595 int flag = x & 1;
00596 x >>= 1;
00597 if (n + x >= syncpoint_count + 1) {
00598 av_log(s, AV_LOG_ERROR, "index overflow A\n");
00599 goto fail;
00600 }
00601 while (x--)
00602 has_keyframe[n++] = flag;
00603 has_keyframe[n++] = !flag;
00604 } else {
00605 while (x != 1) {
00606 if (n >= syncpoint_count + 1) {
00607 av_log(s, AV_LOG_ERROR, "index overflow B\n");
00608 goto fail;
00609 }
00610 has_keyframe[n++] = x & 1;
00611 x >>= 1;
00612 }
00613 }
00614 if (has_keyframe[0]) {
00615 av_log(s, AV_LOG_ERROR, "keyframe before first syncpoint in index\n");
00616 goto fail;
00617 }
00618 av_assert0(n <= syncpoint_count + 1);
00619 for (; j < n && j < syncpoint_count; j++) {
00620 if (has_keyframe[j]) {
00621 uint64_t B, A = ffio_read_varlen(bc);
00622 if (!A) {
00623 A = ffio_read_varlen(bc);
00624 B = ffio_read_varlen(bc);
00625
00626 } else
00627 B = 0;
00628 av_add_index_entry(s->streams[i], 16 * syncpoints[j - 1],
00629 last_pts + A, 0, 0, AVINDEX_KEYFRAME);
00630 last_pts += A + B;
00631 }
00632 }
00633 }
00634 }
00635
00636 if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
00637 av_log(s, AV_LOG_ERROR, "index checksum mismatch\n");
00638 goto fail;
00639 }
00640 ret = 0;
00641
00642 fail:
00643 av_free(syncpoints);
00644 av_free(has_keyframe);
00645 return ret;
00646 }
00647
00648 static int nut_read_header(AVFormatContext *s)
00649 {
00650 NUTContext *nut = s->priv_data;
00651 AVIOContext *bc = s->pb;
00652 int64_t pos;
00653 int initialized_stream_count;
00654
00655 nut->avf = s;
00656
00657
00658 pos = 0;
00659 do {
00660 pos = find_startcode(bc, MAIN_STARTCODE, pos) + 1;
00661 if (pos < 0 + 1) {
00662 av_log(s, AV_LOG_ERROR, "No main startcode found.\n");
00663 return AVERROR_INVALIDDATA;
00664 }
00665 } while (decode_main_header(nut) < 0);
00666
00667
00668 pos = 0;
00669 for (initialized_stream_count = 0; initialized_stream_count < s->nb_streams;) {
00670 pos = find_startcode(bc, STREAM_STARTCODE, pos) + 1;
00671 if (pos < 0 + 1) {
00672 av_log(s, AV_LOG_ERROR, "Not all stream headers found.\n");
00673 return AVERROR_INVALIDDATA;
00674 }
00675 if (decode_stream_header(nut) >= 0)
00676 initialized_stream_count++;
00677 }
00678
00679
00680 pos = 0;
00681 for (;;) {
00682 uint64_t startcode = find_any_startcode(bc, pos);
00683 pos = avio_tell(bc);
00684
00685 if (startcode == 0) {
00686 av_log(s, AV_LOG_ERROR, "EOF before video frames\n");
00687 return AVERROR_INVALIDDATA;
00688 } else if (startcode == SYNCPOINT_STARTCODE) {
00689 nut->next_startcode = startcode;
00690 break;
00691 } else if (startcode != INFO_STARTCODE) {
00692 continue;
00693 }
00694
00695 decode_info_header(nut);
00696 }
00697
00698 s->data_offset = pos - 8;
00699
00700 if (bc->seekable) {
00701 int64_t orig_pos = avio_tell(bc);
00702 find_and_decode_index(nut);
00703 avio_seek(bc, orig_pos, SEEK_SET);
00704 }
00705 av_assert0(nut->next_startcode == SYNCPOINT_STARTCODE);
00706
00707 ff_metadata_conv_ctx(s, NULL, ff_nut_metadata_conv);
00708
00709 return 0;
00710 }
00711
00712 static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id,
00713 uint8_t *header_idx, int frame_code)
00714 {
00715 AVFormatContext *s = nut->avf;
00716 AVIOContext *bc = s->pb;
00717 StreamContext *stc;
00718 int size, flags, size_mul, pts_delta, i, reserved_count;
00719 uint64_t tmp;
00720
00721 if (avio_tell(bc) > nut->last_syncpoint_pos + nut->max_distance) {
00722 av_log(s, AV_LOG_ERROR,
00723 "Last frame must have been damaged %"PRId64" > %"PRId64" + %d\n",
00724 avio_tell(bc), nut->last_syncpoint_pos, nut->max_distance);
00725 return AVERROR_INVALIDDATA;
00726 }
00727
00728 flags = nut->frame_code[frame_code].flags;
00729 size_mul = nut->frame_code[frame_code].size_mul;
00730 size = nut->frame_code[frame_code].size_lsb;
00731 *stream_id = nut->frame_code[frame_code].stream_id;
00732 pts_delta = nut->frame_code[frame_code].pts_delta;
00733 reserved_count = nut->frame_code[frame_code].reserved_count;
00734 *header_idx = nut->frame_code[frame_code].header_idx;
00735
00736 if (flags & FLAG_INVALID)
00737 return AVERROR_INVALIDDATA;
00738 if (flags & FLAG_CODED)
00739 flags ^= ffio_read_varlen(bc);
00740 if (flags & FLAG_STREAM_ID) {
00741 GET_V(*stream_id, tmp < s->nb_streams)
00742 }
00743 stc = &nut->stream[*stream_id];
00744 if (flags & FLAG_CODED_PTS) {
00745 int coded_pts = ffio_read_varlen(bc);
00746
00747 if (coded_pts < (1 << stc->msb_pts_shift)) {
00748 *pts = ff_lsb2full(stc, coded_pts);
00749 } else
00750 *pts = coded_pts - (1 << stc->msb_pts_shift);
00751 } else
00752 *pts = stc->last_pts + pts_delta;
00753 if (flags & FLAG_SIZE_MSB)
00754 size += size_mul * ffio_read_varlen(bc);
00755 if (flags & FLAG_MATCH_TIME)
00756 get_s(bc);
00757 if (flags & FLAG_HEADER_IDX)
00758 *header_idx = ffio_read_varlen(bc);
00759 if (flags & FLAG_RESERVED)
00760 reserved_count = ffio_read_varlen(bc);
00761 for (i = 0; i < reserved_count; i++)
00762 ffio_read_varlen(bc);
00763
00764 if (*header_idx >= (unsigned)nut->header_count) {
00765 av_log(s, AV_LOG_ERROR, "header_idx invalid\n");
00766 return AVERROR_INVALIDDATA;
00767 }
00768 if (size > 4096)
00769 *header_idx = 0;
00770 size -= nut->header_len[*header_idx];
00771
00772 if (flags & FLAG_CHECKSUM) {
00773 avio_rb32(bc);
00774 } else if (size > 2 * nut->max_distance || FFABS(stc->last_pts - *pts) >
00775 stc->max_pts_distance) {
00776 av_log(s, AV_LOG_ERROR, "frame size > 2max_distance and no checksum\n");
00777 return AVERROR_INVALIDDATA;
00778 }
00779
00780 stc->last_pts = *pts;
00781 stc->last_flags = flags;
00782
00783 return size;
00784 }
00785
00786 static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code)
00787 {
00788 AVFormatContext *s = nut->avf;
00789 AVIOContext *bc = s->pb;
00790 int size, stream_id, discard;
00791 int64_t pts, last_IP_pts;
00792 StreamContext *stc;
00793 uint8_t header_idx;
00794
00795 size = decode_frame_header(nut, &pts, &stream_id, &header_idx, frame_code);
00796 if (size < 0)
00797 return size;
00798
00799 stc = &nut->stream[stream_id];
00800
00801 if (stc->last_flags & FLAG_KEY)
00802 stc->skip_until_key_frame = 0;
00803
00804 discard = s->streams[stream_id]->discard;
00805 last_IP_pts = s->streams[stream_id]->last_IP_pts;
00806 if ((discard >= AVDISCARD_NONKEY && !(stc->last_flags & FLAG_KEY)) ||
00807 (discard >= AVDISCARD_BIDIR && last_IP_pts != AV_NOPTS_VALUE &&
00808 last_IP_pts > pts) ||
00809 discard >= AVDISCARD_ALL ||
00810 stc->skip_until_key_frame) {
00811 avio_skip(bc, size);
00812 return 1;
00813 }
00814
00815 av_new_packet(pkt, size + nut->header_len[header_idx]);
00816 memcpy(pkt->data, nut->header[header_idx], nut->header_len[header_idx]);
00817 pkt->pos = avio_tell(bc);
00818 avio_read(bc, pkt->data + nut->header_len[header_idx], size);
00819
00820 pkt->stream_index = stream_id;
00821 if (stc->last_flags & FLAG_KEY)
00822 pkt->flags |= AV_PKT_FLAG_KEY;
00823 pkt->pts = pts;
00824
00825 return 0;
00826 }
00827
00828 static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
00829 {
00830 NUTContext *nut = s->priv_data;
00831 AVIOContext *bc = s->pb;
00832 int i, frame_code = 0, ret, skip;
00833 int64_t ts, back_ptr;
00834
00835 for (;;) {
00836 int64_t pos = avio_tell(bc);
00837 uint64_t tmp = nut->next_startcode;
00838 nut->next_startcode = 0;
00839
00840 if (tmp) {
00841 pos -= 8;
00842 } else {
00843 frame_code = avio_r8(bc);
00844 if (url_feof(bc))
00845 return -1;
00846 if (frame_code == 'N') {
00847 tmp = frame_code;
00848 for (i = 1; i < 8; i++)
00849 tmp = (tmp << 8) + avio_r8(bc);
00850 }
00851 }
00852 switch (tmp) {
00853 case MAIN_STARTCODE:
00854 case STREAM_STARTCODE:
00855 case INDEX_STARTCODE:
00856 skip = get_packetheader(nut, bc, 0, tmp);
00857 avio_skip(bc, skip);
00858 break;
00859 case INFO_STARTCODE:
00860 if (decode_info_header(nut) < 0)
00861 goto resync;
00862 break;
00863 case SYNCPOINT_STARTCODE:
00864 if (decode_syncpoint(nut, &ts, &back_ptr) < 0)
00865 goto resync;
00866 frame_code = avio_r8(bc);
00867 case 0:
00868 ret = decode_frame(nut, pkt, frame_code);
00869 if (ret == 0)
00870 return 0;
00871 else if (ret == 1)
00872 break;
00873 default:
00874 resync:
00875 av_log(s, AV_LOG_DEBUG, "syncing from %"PRId64"\n", pos);
00876 tmp = find_any_startcode(bc, nut->last_syncpoint_pos + 1);
00877 if (tmp == 0)
00878 return AVERROR_INVALIDDATA;
00879 av_log(s, AV_LOG_DEBUG, "sync\n");
00880 nut->next_startcode = tmp;
00881 }
00882 }
00883 }
00884
00885 static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index,
00886 int64_t *pos_arg, int64_t pos_limit)
00887 {
00888 NUTContext *nut = s->priv_data;
00889 AVIOContext *bc = s->pb;
00890 int64_t pos, pts, back_ptr;
00891 av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%"PRId64",%"PRId64")\n",
00892 stream_index, *pos_arg, pos_limit);
00893
00894 pos = *pos_arg;
00895 do {
00896 pos = find_startcode(bc, SYNCPOINT_STARTCODE, pos) + 1;
00897 if (pos < 1) {
00898 av_assert0(nut->next_startcode == 0);
00899 av_log(s, AV_LOG_ERROR, "read_timestamp failed.\n");
00900 return AV_NOPTS_VALUE;
00901 }
00902 } while (decode_syncpoint(nut, &pts, &back_ptr) < 0);
00903 *pos_arg = pos - 1;
00904 av_assert0(nut->last_syncpoint_pos == *pos_arg);
00905
00906 av_log(s, AV_LOG_DEBUG, "return %"PRId64" %"PRId64"\n", pts, back_ptr);
00907 if (stream_index == -2)
00908 return back_ptr;
00909 av_assert0(stream_index == -1);
00910 return pts;
00911 }
00912
00913 static int read_seek(AVFormatContext *s, int stream_index,
00914 int64_t pts, int flags)
00915 {
00916 NUTContext *nut = s->priv_data;
00917 AVStream *st = s->streams[stream_index];
00918 Syncpoint dummy = { .ts = pts * av_q2d(st->time_base) * AV_TIME_BASE };
00919 Syncpoint nopts_sp = { .ts = AV_NOPTS_VALUE, .back_ptr = AV_NOPTS_VALUE };
00920 Syncpoint *sp, *next_node[2] = { &nopts_sp, &nopts_sp };
00921 int64_t pos, pos2, ts;
00922 int i;
00923
00924 if (st->index_entries) {
00925 int index = av_index_search_timestamp(st, pts, flags);
00926 if (index < 0)
00927 return -1;
00928
00929 pos2 = st->index_entries[index].pos;
00930 ts = st->index_entries[index].timestamp;
00931 } else {
00932 av_tree_find(nut->syncpoints, &dummy, (void *) ff_nut_sp_pts_cmp,
00933 (void **) next_node);
00934 av_log(s, AV_LOG_DEBUG, "%"PRIu64"-%"PRIu64" %"PRId64"-%"PRId64"\n",
00935 next_node[0]->pos, next_node[1]->pos, next_node[0]->ts,
00936 next_node[1]->ts);
00937 pos = ff_gen_search(s, -1, dummy.ts, next_node[0]->pos,
00938 next_node[1]->pos, next_node[1]->pos,
00939 next_node[0]->ts, next_node[1]->ts,
00940 AVSEEK_FLAG_BACKWARD, &ts, nut_read_timestamp);
00941
00942 if (!(flags & AVSEEK_FLAG_BACKWARD)) {
00943 dummy.pos = pos + 16;
00944 next_node[1] = &nopts_sp;
00945 av_tree_find(nut->syncpoints, &dummy, (void *) ff_nut_sp_pos_cmp,
00946 (void **) next_node);
00947 pos2 = ff_gen_search(s, -2, dummy.pos, next_node[0]->pos,
00948 next_node[1]->pos, next_node[1]->pos,
00949 next_node[0]->back_ptr, next_node[1]->back_ptr,
00950 flags, &ts, nut_read_timestamp);
00951 if (pos2 >= 0)
00952 pos = pos2;
00953
00954 }
00955 dummy.pos = pos;
00956 sp = av_tree_find(nut->syncpoints, &dummy, (void *) ff_nut_sp_pos_cmp,
00957 NULL);
00958
00959 av_assert0(sp);
00960 pos2 = sp->back_ptr - 15;
00961 }
00962 av_log(NULL, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos2);
00963 pos = find_startcode(s->pb, SYNCPOINT_STARTCODE, pos2);
00964 avio_seek(s->pb, pos, SEEK_SET);
00965 av_log(NULL, AV_LOG_DEBUG, "SP: %"PRId64"\n", pos);
00966 if (pos2 > pos || pos2 + 15 < pos)
00967 av_log(NULL, AV_LOG_ERROR, "no syncpoint at backptr pos\n");
00968 for (i = 0; i < s->nb_streams; i++)
00969 nut->stream[i].skip_until_key_frame = 1;
00970
00971 return 0;
00972 }
00973
00974 static int nut_read_close(AVFormatContext *s)
00975 {
00976 NUTContext *nut = s->priv_data;
00977 int i;
00978
00979 av_freep(&nut->time_base);
00980 av_freep(&nut->stream);
00981 ff_nut_free_sp(nut);
00982 for (i = 1; i < nut->header_count; i++)
00983 av_freep(&nut->header[i]);
00984
00985 return 0;
00986 }
00987
00988 AVInputFormat ff_nut_demuxer = {
00989 .name = "nut",
00990 .long_name = NULL_IF_CONFIG_SMALL("NUT format"),
00991 .flags = AVFMT_SEEK_TO_PTS,
00992 .priv_data_size = sizeof(NUTContext),
00993 .read_probe = nut_probe,
00994 .read_header = nut_read_header,
00995 .read_packet = nut_read_packet,
00996 .read_close = nut_read_close,
00997 .read_seek = read_seek,
00998 .extensions = "nut",
00999 .codec_tag = (const AVCodecTag * const []) {
01000 ff_codec_bmp_tags, ff_nut_video_tags, ff_codec_wav_tags,
01001 ff_nut_subtitle_tags, 0
01002 },
01003 };