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\n");
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 tmp= ff_get_v(bc);
00340 if(tmp > st->codec->sample_rate){
00341 av_log(s, AV_LOG_ERROR, "Bleh, libnut muxed this ;)\n");
00342 st->codec->sample_rate= tmp;
00343 }
00344 GET_V(st->codec->channels, tmp > 0)
00345 }
00346 if(skip_reserved(bc, end) || get_checksum(bc)){
00347 av_log(s, AV_LOG_ERROR, "stream header %d checksum mismatch\n", stream_id);
00348 return -1;
00349 }
00350 stc->time_base= &nut->time_base[stc->time_base_id];
00351 av_set_pts_info(s->streams[stream_id], 63, stc->time_base->num, stc->time_base->den);
00352 return 0;
00353 }
00354
00355 static int decode_info_header(NUTContext *nut){
00356 AVFormatContext *s= nut->avf;
00357 ByteIOContext *bc = &s->pb;
00358 uint64_t tmp;
00359 unsigned int stream_id_plus1, chapter_start, chapter_len, count;
00360 int chapter_id, i;
00361 int64_t value, end;
00362 char name[256], str_value[1024], type_str[256], *type= type_str;
00363
00364 end= get_packetheader(nut, bc, 1, INFO_STARTCODE);
00365 end += url_ftell(bc);
00366
00367 GET_V(stream_id_plus1, tmp <= s->nb_streams)
00368 chapter_id = get_s(bc);
00369 chapter_start= ff_get_v(bc);
00370 chapter_len = ff_get_v(bc);
00371 count = ff_get_v(bc);
00372 for(i=0; i<count; i++){
00373 get_str(bc, name, sizeof(name));
00374 value= get_s(bc);
00375 if(value == -1){
00376 type= "UTF-8";
00377 get_str(bc, str_value, sizeof(str_value));
00378 }else if(value == -2){
00379 get_str(bc, type, sizeof(type));
00380 get_str(bc, str_value, sizeof(str_value));
00381 }else if(value == -3){
00382 type= "s";
00383 value= get_s(bc);
00384 }else if(value == -4){
00385 type= "t";
00386 value= ff_get_v(bc);
00387 }else if(value < -4){
00388 type= "r";
00389 get_s(bc);
00390 }else{
00391 type= "v";
00392 }
00393
00394 if(chapter_id==0 && !strcmp(type, "UTF-8")){
00395 if (!strcmp(name, "Author"))
00396 av_strlcpy(s->author , str_value, sizeof(s->author));
00397 else if(!strcmp(name, "Title"))
00398 av_strlcpy(s->title , str_value, sizeof(s->title));
00399 else if(!strcmp(name, "Copyright"))
00400 av_strlcpy(s->copyright, str_value, sizeof(s->copyright));
00401 else if(!strcmp(name, "Description"))
00402 av_strlcpy(s->comment , str_value, sizeof(s->comment));
00403 }
00404 }
00405
00406 if(skip_reserved(bc, end) || get_checksum(bc)){
00407 av_log(s, AV_LOG_ERROR, "info header checksum mismatch\n");
00408 return -1;
00409 }
00410 return 0;
00411 }
00412
00413 static int decode_syncpoint(NUTContext *nut, int64_t *ts, int64_t *back_ptr){
00414 AVFormatContext *s= nut->avf;
00415 ByteIOContext *bc = &s->pb;
00416 int64_t end, tmp;
00417
00418 nut->last_syncpoint_pos= url_ftell(bc)-8;
00419
00420 end= get_packetheader(nut, bc, 1, SYNCPOINT_STARTCODE);
00421 end += url_ftell(bc);
00422
00423 tmp= ff_get_v(bc);
00424 *back_ptr= nut->last_syncpoint_pos - 16*ff_get_v(bc);
00425 if(*back_ptr < 0)
00426 return -1;
00427
00428 ff_nut_reset_ts(nut, nut->time_base[tmp % nut->time_base_count], tmp);
00429
00430 if(skip_reserved(bc, end) || get_checksum(bc)){
00431 av_log(s, AV_LOG_ERROR, "sync point checksum mismatch\n");
00432 return -1;
00433 }
00434
00435 *ts= tmp / s->nb_streams * av_q2d(nut->time_base[tmp % s->nb_streams])*AV_TIME_BASE;
00436 ff_nut_add_sp(nut, nut->last_syncpoint_pos, *back_ptr, *ts);
00437
00438 return 0;
00439 }
00440
00441 static int find_and_decode_index(NUTContext *nut){
00442 AVFormatContext *s= nut->avf;
00443 ByteIOContext *bc = &s->pb;
00444 uint64_t tmp, end;
00445 int i, j, syncpoint_count;
00446 int64_t filesize= url_fsize(bc);
00447 int64_t *syncpoints;
00448 int8_t *has_keyframe;
00449
00450 url_fseek(bc, filesize-12, SEEK_SET);
00451 url_fseek(bc, filesize-get_be64(bc), SEEK_SET);
00452 if(get_be64(bc) != INDEX_STARTCODE){
00453 av_log(s, AV_LOG_ERROR, "no index at the end\n");
00454 return -1;
00455 }
00456
00457 end= get_packetheader(nut, bc, 1, INDEX_STARTCODE);
00458 end += url_ftell(bc);
00459
00460 ff_get_v(bc);
00461 GET_V(syncpoint_count, tmp < INT_MAX/8 && tmp > 0)
00462 syncpoints= av_malloc(sizeof(int64_t)*syncpoint_count);
00463 has_keyframe= av_malloc(sizeof(int8_t)*(syncpoint_count+1));
00464 for(i=0; i<syncpoint_count; i++){
00465 GET_V(syncpoints[i], tmp>0)
00466 if(i)
00467 syncpoints[i] += syncpoints[i-1];
00468 }
00469
00470 for(i=0; i<s->nb_streams; i++){
00471 int64_t last_pts= -1;
00472 for(j=0; j<syncpoint_count;){
00473 uint64_t x= ff_get_v(bc);
00474 int type= x&1;
00475 int n= j;
00476 x>>=1;
00477 if(type){
00478 int flag= x&1;
00479 x>>=1;
00480 if(n+x >= syncpoint_count + 1){
00481 av_log(s, AV_LOG_ERROR, "index overflow A\n");
00482 return -1;
00483 }
00484 while(x--)
00485 has_keyframe[n++]= flag;
00486 has_keyframe[n++]= !flag;
00487 }else{
00488 while(x != 1){
00489 if(n>=syncpoint_count + 1){
00490 av_log(s, AV_LOG_ERROR, "index overflow B\n");
00491 return -1;
00492 }
00493 has_keyframe[n++]= x&1;
00494 x>>=1;
00495 }
00496 }
00497 if(has_keyframe[0]){
00498 av_log(s, AV_LOG_ERROR, "keyframe before first syncpoint in index\n");
00499 return -1;
00500 }
00501 assert(n<=syncpoint_count+1);
00502 for(; j<n; j++){
00503 if(has_keyframe[j]){
00504 uint64_t B, A= ff_get_v(bc);
00505 if(!A){
00506 A= ff_get_v(bc);
00507 B= ff_get_v(bc);
00508
00509 }else
00510 B= 0;
00511 av_add_index_entry(
00512 s->streams[i],
00513 16*syncpoints[j-1],
00514 last_pts + A,
00515 0,
00516 0,
00517 AVINDEX_KEYFRAME);
00518 last_pts += A + B;
00519 }
00520 }
00521 }
00522 }
00523
00524 if(skip_reserved(bc, end) || get_checksum(bc)){
00525 av_log(s, AV_LOG_ERROR, "index checksum mismatch\n");
00526 return -1;
00527 }
00528 return 0;
00529 }
00530
00531 static int nut_read_header(AVFormatContext *s, AVFormatParameters *ap)
00532 {
00533 NUTContext *nut = s->priv_data;
00534 ByteIOContext *bc = &s->pb;
00535 int64_t pos;
00536 int inited_stream_count;
00537
00538 nut->avf= s;
00539
00540
00541 pos=0;
00542 do{
00543 pos= find_startcode(bc, MAIN_STARTCODE, pos)+1;
00544 if (pos<0+1){
00545 av_log(s, AV_LOG_ERROR, "No main startcode found.\n");
00546 return -1;
00547 }
00548 }while(decode_main_header(nut) < 0);
00549
00550
00551 pos=0;
00552 for(inited_stream_count=0; inited_stream_count < s->nb_streams;){
00553 pos= find_startcode(bc, STREAM_STARTCODE, pos)+1;
00554 if (pos<0+1){
00555 av_log(s, AV_LOG_ERROR, "Not all stream headers found.\n");
00556 return -1;
00557 }
00558 if(decode_stream_header(nut) >= 0)
00559 inited_stream_count++;
00560 }
00561
00562
00563 pos=0;
00564 for(;;){
00565 uint64_t startcode= find_any_startcode(bc, pos);
00566 pos= url_ftell(bc);
00567
00568 if(startcode==0){
00569 av_log(s, AV_LOG_ERROR, "EOF before video frames\n");
00570 return -1;
00571 }else if(startcode == SYNCPOINT_STARTCODE){
00572 nut->next_startcode= startcode;
00573 break;
00574 }else if(startcode != INFO_STARTCODE){
00575 continue;
00576 }
00577
00578 decode_info_header(nut);
00579 }
00580
00581 s->data_offset= pos-8;
00582
00583 if(!url_is_streamed(bc)){
00584 int64_t orig_pos= url_ftell(bc);
00585 find_and_decode_index(nut);
00586 url_fseek(bc, orig_pos, SEEK_SET);
00587 }
00588 assert(nut->next_startcode == SYNCPOINT_STARTCODE);
00589
00590 return 0;
00591 }
00592
00593 static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id, int frame_code){
00594 AVFormatContext *s= nut->avf;
00595 ByteIOContext *bc = &s->pb;
00596 StreamContext *stc;
00597 int size, flags, size_mul, pts_delta, i, reserved_count;
00598 uint64_t tmp;
00599
00600 if(url_ftell(bc) > nut->last_syncpoint_pos + nut->max_distance){
00601 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);
00602 return -1;
00603 }
00604
00605 flags = nut->frame_code[frame_code].flags;
00606 size_mul = nut->frame_code[frame_code].size_mul;
00607 size = nut->frame_code[frame_code].size_lsb;
00608 *stream_id = nut->frame_code[frame_code].stream_id;
00609 pts_delta = nut->frame_code[frame_code].pts_delta;
00610 reserved_count = nut->frame_code[frame_code].reserved_count;
00611
00612 if(flags & FLAG_INVALID)
00613 return -1;
00614 if(flags & FLAG_CODED)
00615 flags ^= ff_get_v(bc);
00616 if(flags & FLAG_STREAM_ID){
00617 GET_V(*stream_id, tmp < s->nb_streams)
00618 }
00619 stc= &nut->stream[*stream_id];
00620 if(flags&FLAG_CODED_PTS){
00621 int coded_pts= ff_get_v(bc);
00622
00623 if(coded_pts < (1<<stc->msb_pts_shift)){
00624 *pts=ff_lsb2full(stc, coded_pts);
00625 }else
00626 *pts=coded_pts - (1<<stc->msb_pts_shift);
00627 }else
00628 *pts= stc->last_pts + pts_delta;
00629 if(flags&FLAG_SIZE_MSB){
00630 size += size_mul*ff_get_v(bc);
00631 }
00632 if(flags&FLAG_RESERVED)
00633 reserved_count= ff_get_v(bc);
00634 for(i=0; i<reserved_count; i++)
00635 ff_get_v(bc);
00636 if(flags&FLAG_CHECKSUM){
00637 get_be32(bc);
00638 }else if(size > 2*nut->max_distance || FFABS(stc->last_pts - *pts) > stc->max_pts_distance){
00639 av_log(s, AV_LOG_ERROR, "frame size > 2max_distance and no checksum\n");
00640 return -1;
00641 }
00642
00643 stc->last_pts= *pts;
00644 stc->last_flags= flags;
00645
00646 return size;
00647 }
00648
00649 static int decode_frame(NUTContext *nut, AVPacket *pkt, int frame_code){
00650 AVFormatContext *s= nut->avf;
00651 ByteIOContext *bc = &s->pb;
00652 int size, stream_id, discard;
00653 int64_t pts, last_IP_pts;
00654 StreamContext *stc;
00655
00656 size= decode_frame_header(nut, &pts, &stream_id, frame_code);
00657 if(size < 0)
00658 return -1;
00659
00660 stc= &nut->stream[stream_id];
00661
00662 if (stc->last_flags & FLAG_KEY)
00663 stc->skip_until_key_frame=0;
00664
00665 discard= s->streams[ stream_id ]->discard;
00666 last_IP_pts= s->streams[ stream_id ]->last_IP_pts;
00667 if( (discard >= AVDISCARD_NONKEY && !(stc->last_flags & FLAG_KEY))
00668 ||(discard >= AVDISCARD_BIDIR && last_IP_pts != AV_NOPTS_VALUE && last_IP_pts > pts)
00669 || discard >= AVDISCARD_ALL
00670 || stc->skip_until_key_frame){
00671 url_fskip(bc, size);
00672 return 1;
00673 }
00674
00675 av_get_packet(bc, pkt, size);
00676 pkt->stream_index = stream_id;
00677 if (stc->last_flags & FLAG_KEY)
00678 pkt->flags |= PKT_FLAG_KEY;
00679 pkt->pts = pts;
00680
00681 return 0;
00682 }
00683
00684 static int nut_read_packet(AVFormatContext *s, AVPacket *pkt)
00685 {
00686 NUTContext *nut = s->priv_data;
00687 ByteIOContext *bc = &s->pb;
00688 int i, frame_code=0, ret, skip;
00689 int64_t ts, back_ptr;
00690
00691 for(;;){
00692 int64_t pos= url_ftell(bc);
00693 uint64_t tmp= nut->next_startcode;
00694 nut->next_startcode=0;
00695
00696 if(tmp){
00697 pos-=8;
00698 }else{
00699 frame_code = get_byte(bc);
00700 if(url_feof(bc))
00701 return -1;
00702 if(frame_code == 'N'){
00703 tmp= frame_code;
00704 for(i=1; i<8; i++)
00705 tmp = (tmp<<8) + get_byte(bc);
00706 }
00707 }
00708 switch(tmp){
00709 case MAIN_STARTCODE:
00710 case STREAM_STARTCODE:
00711 case INDEX_STARTCODE:
00712 skip= get_packetheader(nut, bc, 0, tmp);
00713 url_fseek(bc, skip, SEEK_CUR);
00714 break;
00715 case INFO_STARTCODE:
00716 if(decode_info_header(nut)<0)
00717 goto resync;
00718 break;
00719 case SYNCPOINT_STARTCODE:
00720 if(decode_syncpoint(nut, &ts, &back_ptr)<0)
00721 goto resync;
00722 frame_code = get_byte(bc);
00723 case 0:
00724 ret= decode_frame(nut, pkt, frame_code);
00725 if(ret==0)
00726 return 0;
00727 else if(ret==1)
00728 break;
00729 default:
00730 resync:
00731 av_log(s, AV_LOG_DEBUG, "syncing from %"PRId64"\n", pos);
00732 tmp= find_any_startcode(bc, nut->last_syncpoint_pos+1);
00733 if(tmp==0)
00734 return -1;
00735 av_log(s, AV_LOG_DEBUG, "sync\n");
00736 nut->next_startcode= tmp;
00737 }
00738 }
00739 }
00740
00741 static int64_t nut_read_timestamp(AVFormatContext *s, int stream_index, int64_t *pos_arg, int64_t pos_limit){
00742 NUTContext *nut = s->priv_data;
00743 ByteIOContext *bc = &s->pb;
00744 int64_t pos, pts, back_ptr;
00745 av_log(s, AV_LOG_DEBUG, "read_timestamp(X,%d,%"PRId64",%"PRId64")\n", stream_index, *pos_arg, pos_limit);
00746
00747 pos= *pos_arg;
00748 do{
00749 pos= find_startcode(bc, SYNCPOINT_STARTCODE, pos)+1;
00750 if(pos < 1){
00751 assert(nut->next_startcode == 0);
00752 av_log(s, AV_LOG_ERROR, "read_timestamp failed.\n");
00753 return AV_NOPTS_VALUE;
00754 }
00755 }while(decode_syncpoint(nut, &pts, &back_ptr) < 0);
00756 *pos_arg = pos-1;
00757 assert(nut->last_syncpoint_pos == *pos_arg);
00758
00759 av_log(s, AV_LOG_DEBUG, "return %"PRId64" %"PRId64"\n", pts,back_ptr );
00760 if (stream_index == -1) return pts;
00761 else if(stream_index == -2) return back_ptr;
00762
00763 assert(0);
00764 }
00765
00766 static int read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags){
00767 NUTContext *nut = s->priv_data;
00768 AVStream *st= s->streams[stream_index];
00769 syncpoint_t dummy={.ts= pts*av_q2d(st->time_base)*AV_TIME_BASE};
00770 syncpoint_t nopts_sp= {.ts= AV_NOPTS_VALUE, .back_ptr= AV_NOPTS_VALUE};
00771 syncpoint_t *sp, *next_node[2]= {&nopts_sp, &nopts_sp};
00772 int64_t pos, pos2, ts;
00773 int i;
00774
00775 if(st->index_entries){
00776 int index= av_index_search_timestamp(st, pts, flags);
00777 if(index<0)
00778 return -1;
00779
00780 pos2= st->index_entries[index].pos;
00781 ts = st->index_entries[index].timestamp;
00782 }else{
00783 av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pts_cmp, next_node);
00784 av_log(s, AV_LOG_DEBUG, "%"PRIu64"-%"PRIu64" %"PRId64"-%"PRId64"\n", next_node[0]->pos, next_node[1]->pos,
00785 next_node[0]->ts , next_node[1]->ts);
00786 pos= av_gen_search(s, -1, dummy.ts, next_node[0]->pos, next_node[1]->pos, next_node[1]->pos,
00787 next_node[0]->ts , next_node[1]->ts, AVSEEK_FLAG_BACKWARD, &ts, nut_read_timestamp);
00788
00789 if(!(flags & AVSEEK_FLAG_BACKWARD)){
00790 dummy.pos= pos+16;
00791 next_node[1]= &nopts_sp;
00792 av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pos_cmp, next_node);
00793 pos2= av_gen_search(s, -2, dummy.pos, next_node[0]->pos , next_node[1]->pos, next_node[1]->pos,
00794 next_node[0]->back_ptr, next_node[1]->back_ptr, flags, &ts, nut_read_timestamp);
00795 if(pos2>=0)
00796 pos= pos2;
00797
00798 }
00799 dummy.pos= pos;
00800 sp= av_tree_find(nut->syncpoints, &dummy, ff_nut_sp_pos_cmp, NULL);
00801
00802 assert(sp);
00803 pos2= sp->back_ptr - 15;
00804 }
00805 av_log(NULL, AV_LOG_DEBUG, "SEEKTO: %"PRId64"\n", pos2);
00806 pos= find_startcode(&s->pb, SYNCPOINT_STARTCODE, pos2);
00807 url_fseek(&s->pb, pos, SEEK_SET);
00808 av_log(NULL, AV_LOG_DEBUG, "SP: %"PRId64"\n", pos);
00809 if(pos2 > pos || pos2 + 15 < pos){
00810 av_log(NULL, AV_LOG_ERROR, "no syncpoint at backptr pos\n");
00811 }
00812 for(i=0; i<s->nb_streams; i++)
00813 nut->stream[i].skip_until_key_frame=1;
00814
00815 return 0;
00816 }
00817
00818 static int nut_read_close(AVFormatContext *s)
00819 {
00820 NUTContext *nut = s->priv_data;
00821
00822 av_freep(&nut->time_base);
00823 av_freep(&nut->stream);
00824
00825 return 0;
00826 }
00827
00828 #ifdef CONFIG_NUT_DEMUXER
00829 AVInputFormat nut_demuxer = {
00830 "nut",
00831 "nut format",
00832 sizeof(NUTContext),
00833 nut_probe,
00834 nut_read_header,
00835 nut_read_packet,
00836 nut_read_close,
00837 read_seek,
00838 .extensions = "nut",
00839 };
00840 #endif