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