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