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