• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavformat/nutdec.c

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

Generated on Sat Feb 16 2013 09:23:15 for ffmpeg by  doxygen 1.7.1