00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/intreadwrite.h"
00023 #include "libavutil/tree.h"
00024 #include "libavcodec/mpegaudiodata.h"
00025 #include "nut.h"
00026
00027 static int find_expected_header(AVCodecContext *c, int size, int key_frame, uint8_t out[64]){
00028 int sample_rate= c->sample_rate;
00029
00030 if(size>4096)
00031 return 0;
00032
00033 AV_WB24(out, 1);
00034
00035 if(c->codec_id == CODEC_ID_MPEG4){
00036 if(key_frame){
00037 return 3;
00038 }else{
00039 out[3]= 0xB6;
00040 return 4;
00041 }
00042 }else if(c->codec_id == CODEC_ID_MPEG1VIDEO || c->codec_id == CODEC_ID_MPEG2VIDEO){
00043 return 3;
00044 }else if(c->codec_id == CODEC_ID_H264){
00045 return 3;
00046 }else if(c->codec_id == CODEC_ID_MP3 || c->codec_id == CODEC_ID_MP2){
00047 int lsf, mpeg25, sample_rate_index, bitrate_index, frame_size;
00048 int layer= c->codec_id == CODEC_ID_MP3 ? 3 : 2;
00049 unsigned int header= 0xFFF00000;
00050
00051 lsf = sample_rate < (24000+32000)/2;
00052 mpeg25 = sample_rate < (12000+16000)/2;
00053 sample_rate <<= lsf + mpeg25;
00054 if (sample_rate < (32000 + 44100)/2) sample_rate_index=2;
00055 else if(sample_rate < (44100 + 48000)/2) sample_rate_index=0;
00056 else sample_rate_index=1;
00057
00058 sample_rate= ff_mpa_freq_tab[sample_rate_index] >> (lsf + mpeg25);
00059
00060 for(bitrate_index=2; bitrate_index<30; bitrate_index++){
00061 frame_size = ff_mpa_bitrate_tab[lsf][layer-1][bitrate_index>>1];
00062 frame_size = (frame_size * 144000) / (sample_rate << lsf) + (bitrate_index&1);
00063
00064 if(frame_size == size)
00065 break;
00066 }
00067
00068 header |= (!lsf)<<19;
00069 header |= (4-layer)<<17;
00070 header |= 1<<16;
00071 AV_WB32(out, header);
00072 if(size <= 0)
00073 return 2;
00074 if(bitrate_index == 30)
00075 return -1;
00076
00077 header |= (bitrate_index>>1)<<12;
00078 header |= sample_rate_index<<10;
00079 header |= (bitrate_index&1)<<9;
00080
00081 return 2;
00082 return 3;
00083
00084 }
00085 return 0;
00086 }
00087
00088 static int find_header_idx(AVFormatContext *s, AVCodecContext *c, int size, int frame_type){
00089 NUTContext *nut = s->priv_data;
00090 uint8_t out[64];
00091 int i;
00092 int len= find_expected_header(c, size, frame_type, out);
00093
00094
00095
00096 for(i=1; i<nut->header_count; i++){
00097 if( len == nut->header_len[i]
00098 && !memcmp(out, nut->header[i], len)){
00099
00100 return i;
00101 }
00102 }
00103
00104 return 0;
00105 }
00106
00107 static void build_elision_headers(AVFormatContext *s){
00108 NUTContext *nut = s->priv_data;
00109 int i;
00110
00111
00112 static const uint8_t headers[][5]={
00113 {3, 0x00, 0x00, 0x01},
00114 {4, 0x00, 0x00, 0x01, 0xB6},
00115 {2, 0xFF, 0xFA},
00116 {2, 0xFF, 0xFB},
00117 {2, 0xFF, 0xFC},
00118 {2, 0xFF, 0xFD},
00119 };
00120
00121 nut->header_count= 7;
00122 for(i=1; i<nut->header_count; i++){
00123 nut->header_len[i]= headers[i-1][0];
00124 nut->header [i]= &headers[i-1][1];
00125 }
00126 }
00127
00128 static void build_frame_code(AVFormatContext *s){
00129 NUTContext *nut = s->priv_data;
00130 int key_frame, index, pred, stream_id;
00131 int start=1;
00132 int end= 254;
00133 int keyframe_0_esc= s->nb_streams > 2;
00134 int pred_table[10];
00135 FrameCode *ft;
00136
00137 ft= &nut->frame_code[start];
00138 ft->flags= FLAG_CODED;
00139 ft->size_mul=1;
00140 ft->pts_delta=1;
00141 start++;
00142
00143 if(keyframe_0_esc){
00144
00145 FrameCode *ft= &nut->frame_code[start];
00146 ft->flags= FLAG_STREAM_ID | FLAG_SIZE_MSB | FLAG_CODED_PTS;
00147 ft->size_mul=1;
00148 start++;
00149 }
00150
00151 for(stream_id= 0; stream_id<s->nb_streams; stream_id++){
00152 int start2= start + (end-start)*stream_id / s->nb_streams;
00153 int end2 = start + (end-start)*(stream_id+1) / s->nb_streams;
00154 AVCodecContext *codec = s->streams[stream_id]->codec;
00155 int is_audio= codec->codec_type == AVMEDIA_TYPE_AUDIO;
00156 int intra_only= is_audio;
00157 int pred_count;
00158
00159 for(key_frame=0; key_frame<2; key_frame++){
00160 if(intra_only && keyframe_0_esc && key_frame==0)
00161 continue;
00162
00163 {
00164 FrameCode *ft= &nut->frame_code[start2];
00165 ft->flags= FLAG_KEY*key_frame;
00166 ft->flags|= FLAG_SIZE_MSB | FLAG_CODED_PTS;
00167 ft->stream_id= stream_id;
00168 ft->size_mul=1;
00169 if(is_audio)
00170 ft->header_idx= find_header_idx(s, codec, -1, key_frame);
00171 start2++;
00172 }
00173 }
00174
00175 key_frame= intra_only;
00176 #if 1
00177 if(is_audio){
00178 int frame_bytes= codec->frame_size*(int64_t)codec->bit_rate / (8*codec->sample_rate);
00179 int pts;
00180 for(pts=0; pts<2; pts++){
00181 for(pred=0; pred<2; pred++){
00182 FrameCode *ft= &nut->frame_code[start2];
00183 ft->flags= FLAG_KEY*key_frame;
00184 ft->stream_id= stream_id;
00185 ft->size_mul=frame_bytes + 2;
00186 ft->size_lsb=frame_bytes + pred;
00187 ft->pts_delta=pts;
00188 ft->header_idx= find_header_idx(s, codec, frame_bytes + pred, key_frame);
00189 start2++;
00190 }
00191 }
00192 }else{
00193 FrameCode *ft= &nut->frame_code[start2];
00194 ft->flags= FLAG_KEY | FLAG_SIZE_MSB;
00195 ft->stream_id= stream_id;
00196 ft->size_mul=1;
00197 ft->pts_delta=1;
00198 start2++;
00199 }
00200 #endif
00201
00202 if(codec->has_b_frames){
00203 pred_count=5;
00204 pred_table[0]=-2;
00205 pred_table[1]=-1;
00206 pred_table[2]=1;
00207 pred_table[3]=3;
00208 pred_table[4]=4;
00209 }else if(codec->codec_id == CODEC_ID_VORBIS){
00210 pred_count=3;
00211 pred_table[0]=2;
00212 pred_table[1]=9;
00213 pred_table[2]=16;
00214 }else{
00215 pred_count=1;
00216 pred_table[0]=1;
00217 }
00218
00219 for(pred=0; pred<pred_count; pred++){
00220 int start3= start2 + (end2-start2)*pred / pred_count;
00221 int end3 = start2 + (end2-start2)*(pred+1) / pred_count;
00222
00223 for(index=start3; index<end3; index++){
00224 FrameCode *ft= &nut->frame_code[index];
00225 ft->flags= FLAG_KEY*key_frame;
00226 ft->flags|= FLAG_SIZE_MSB;
00227 ft->stream_id= stream_id;
00228
00229 ft->size_mul= end3-start3;
00230 ft->size_lsb= index - start3;
00231 ft->pts_delta= pred_table[pred];
00232 if(is_audio)
00233 ft->header_idx= find_header_idx(s, codec, -1, key_frame);
00234 }
00235 }
00236 }
00237 memmove(&nut->frame_code['N'+1], &nut->frame_code['N'], sizeof(FrameCode)*(255-'N'));
00238 nut->frame_code[ 0].flags=
00239 nut->frame_code[255].flags=
00240 nut->frame_code['N'].flags= FLAG_INVALID;
00241 }
00242
00246 static int get_length(uint64_t val){
00247 int i=1;
00248
00249 while(val>>=7)
00250 i++;
00251
00252 return i;
00253 }
00254
00255 static void put_v(ByteIOContext *bc, uint64_t val){
00256 int i= get_length(val);
00257
00258 while(--i>0)
00259 put_byte(bc, 128 | (val>>(7*i)));
00260
00261 put_byte(bc, val&127);
00262 }
00263
00264 static void put_tt(NUTContext *nut, StreamContext *nus, ByteIOContext *bc, uint64_t val){
00265 val *= nut->time_base_count;
00266 val += nus->time_base - nut->time_base;
00267 put_v(bc, val);
00268 }
00269
00273 static void put_str(ByteIOContext *bc, const char *string){
00274 int len= strlen(string);
00275
00276 put_v(bc, len);
00277 put_buffer(bc, string, len);
00278 }
00279
00280 static void put_s(ByteIOContext *bc, int64_t val){
00281 put_v(bc, 2*FFABS(val) - (val>0));
00282 }
00283
00284 #ifdef TRACE
00285 static inline void put_v_trace(ByteIOContext *bc, uint64_t v, char *file, char *func, int line){
00286 av_log(NULL, AV_LOG_DEBUG, "put_v %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
00287
00288 put_v(bc, v);
00289 }
00290
00291 static inline void put_s_trace(ByteIOContext *bc, int64_t v, char *file, char *func, int line){
00292 av_log(NULL, AV_LOG_DEBUG, "put_s %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
00293
00294 put_s(bc, v);
00295 }
00296 #define put_v(bc, v) put_v_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00297 #define put_s(bc, v) put_s_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00298 #endif
00299
00300
00301 static void put_packet(NUTContext *nut, ByteIOContext *bc, ByteIOContext *dyn_bc, int calculate_checksum, uint64_t startcode){
00302 uint8_t *dyn_buf=NULL;
00303 int dyn_size= url_close_dyn_buf(dyn_bc, &dyn_buf);
00304 int forw_ptr= dyn_size + 4*calculate_checksum;
00305
00306 if(forw_ptr > 4096)
00307 init_checksum(bc, ff_crc04C11DB7_update, 0);
00308 put_be64(bc, startcode);
00309 put_v(bc, forw_ptr);
00310 if(forw_ptr > 4096)
00311 put_le32(bc, get_checksum(bc));
00312
00313 if(calculate_checksum)
00314 init_checksum(bc, ff_crc04C11DB7_update, 0);
00315 put_buffer(bc, dyn_buf, dyn_size);
00316 if(calculate_checksum)
00317 put_le32(bc, get_checksum(bc));
00318
00319 av_free(dyn_buf);
00320 }
00321
00322 static void write_mainheader(NUTContext *nut, ByteIOContext *bc){
00323 int i, j, tmp_pts, tmp_flags, tmp_stream, tmp_mul, tmp_size, tmp_fields, tmp_head_idx;
00324 int64_t tmp_match;
00325
00326 put_v(bc, 3);
00327 put_v(bc, nut->avf->nb_streams);
00328 put_v(bc, nut->max_distance);
00329 put_v(bc, nut->time_base_count);
00330
00331 for(i=0; i<nut->time_base_count; i++){
00332 put_v(bc, nut->time_base[i].num);
00333 put_v(bc, nut->time_base[i].den);
00334 }
00335
00336 tmp_pts=0;
00337 tmp_mul=1;
00338 tmp_stream=0;
00339 tmp_match= 1-(1LL<<62);
00340 tmp_head_idx= 0;
00341 for(i=0; i<256;){
00342 tmp_fields=0;
00343 tmp_size=0;
00344
00345 if(tmp_pts != nut->frame_code[i].pts_delta) tmp_fields=1;
00346 if(tmp_mul != nut->frame_code[i].size_mul ) tmp_fields=2;
00347 if(tmp_stream != nut->frame_code[i].stream_id) tmp_fields=3;
00348 if(tmp_size != nut->frame_code[i].size_lsb ) tmp_fields=4;
00349
00350 if(tmp_head_idx!=nut->frame_code[i].header_idx)tmp_fields=8;
00351
00352 tmp_pts = nut->frame_code[i].pts_delta;
00353 tmp_flags = nut->frame_code[i].flags;
00354 tmp_stream= nut->frame_code[i].stream_id;
00355 tmp_mul = nut->frame_code[i].size_mul;
00356 tmp_size = nut->frame_code[i].size_lsb;
00357
00358 tmp_head_idx= nut->frame_code[i].header_idx;
00359
00360 for(j=0; i<256; j++,i++){
00361 if(i == 'N'){
00362 j--;
00363 continue;
00364 }
00365 if(nut->frame_code[i].pts_delta != tmp_pts ) break;
00366 if(nut->frame_code[i].flags != tmp_flags ) break;
00367 if(nut->frame_code[i].stream_id != tmp_stream) break;
00368 if(nut->frame_code[i].size_mul != tmp_mul ) break;
00369 if(nut->frame_code[i].size_lsb != tmp_size+j) break;
00370
00371 if(nut->frame_code[i].header_idx!= tmp_head_idx) break;
00372 }
00373 if(j != tmp_mul - tmp_size) tmp_fields=6;
00374
00375 put_v(bc, tmp_flags);
00376 put_v(bc, tmp_fields);
00377 if(tmp_fields>0) put_s(bc, tmp_pts);
00378 if(tmp_fields>1) put_v(bc, tmp_mul);
00379 if(tmp_fields>2) put_v(bc, tmp_stream);
00380 if(tmp_fields>3) put_v(bc, tmp_size);
00381 if(tmp_fields>4) put_v(bc, 0 );
00382 if(tmp_fields>5) put_v(bc, j);
00383 if(tmp_fields>6) put_v(bc, tmp_match);
00384 if(tmp_fields>7) put_v(bc, tmp_head_idx);
00385 }
00386 put_v(bc, nut->header_count-1);
00387 for(i=1; i<nut->header_count; i++){
00388 put_v(bc, nut->header_len[i]);
00389 put_buffer(bc, nut->header[i], nut->header_len[i]);
00390 }
00391 }
00392
00393 static int write_streamheader(NUTContext *nut, ByteIOContext *bc, AVStream *st, int i){
00394 AVCodecContext *codec = st->codec;
00395 put_v(bc, i);
00396 switch(codec->codec_type){
00397 case AVMEDIA_TYPE_VIDEO: put_v(bc, 0); break;
00398 case AVMEDIA_TYPE_AUDIO: put_v(bc, 1); break;
00399 case AVMEDIA_TYPE_SUBTITLE: put_v(bc, 2); break;
00400 default : put_v(bc, 3); break;
00401 }
00402 put_v(bc, 4);
00403 if (codec->codec_tag){
00404 put_le32(bc, codec->codec_tag);
00405 }else
00406 return -1;
00407
00408 put_v(bc, nut->stream[i].time_base - nut->time_base);
00409 put_v(bc, nut->stream[i].msb_pts_shift);
00410 put_v(bc, nut->stream[i].max_pts_distance);
00411 put_v(bc, codec->has_b_frames);
00412 put_byte(bc, 0);
00413
00414 put_v(bc, codec->extradata_size);
00415 put_buffer(bc, codec->extradata, codec->extradata_size);
00416
00417 switch(codec->codec_type){
00418 case AVMEDIA_TYPE_AUDIO:
00419 put_v(bc, codec->sample_rate);
00420 put_v(bc, 1);
00421 put_v(bc, codec->channels);
00422 break;
00423 case AVMEDIA_TYPE_VIDEO:
00424 put_v(bc, codec->width);
00425 put_v(bc, codec->height);
00426
00427 if(st->sample_aspect_ratio.num<=0 || st->sample_aspect_ratio.den<=0){
00428 put_v(bc, 0);
00429 put_v(bc, 0);
00430 }else{
00431 put_v(bc, st->sample_aspect_ratio.num);
00432 put_v(bc, st->sample_aspect_ratio.den);
00433 }
00434 put_v(bc, 0);
00435 break;
00436 default:
00437 break;
00438 }
00439 return 0;
00440 }
00441
00442 static int add_info(ByteIOContext *bc, const char *type, const char *value){
00443 put_str(bc, type);
00444 put_s(bc, -1);
00445 put_str(bc, value);
00446 return 1;
00447 }
00448
00449 static int write_globalinfo(NUTContext *nut, ByteIOContext *bc){
00450 AVFormatContext *s= nut->avf;
00451 AVMetadataTag *t = NULL;
00452 ByteIOContext *dyn_bc;
00453 uint8_t *dyn_buf=NULL;
00454 int count=0, dyn_size;
00455 int ret = url_open_dyn_buf(&dyn_bc);
00456 if(ret < 0)
00457 return ret;
00458
00459 while ((t = av_metadata_get(s->metadata, "", t, AV_METADATA_IGNORE_SUFFIX)))
00460 count += add_info(dyn_bc, t->key, t->value);
00461
00462 put_v(bc, 0);
00463 put_v(bc, 0);
00464 put_v(bc, 0);
00465 put_v(bc, 0);
00466
00467 put_v(bc, count);
00468
00469 dyn_size= url_close_dyn_buf(dyn_bc, &dyn_buf);
00470 put_buffer(bc, dyn_buf, dyn_size);
00471 av_free(dyn_buf);
00472 return 0;
00473 }
00474
00475 static int write_streaminfo(NUTContext *nut, ByteIOContext *bc, int stream_id){
00476 AVFormatContext *s= nut->avf;
00477 AVStream* st = s->streams[stream_id];
00478 ByteIOContext *dyn_bc;
00479 uint8_t *dyn_buf=NULL;
00480 int count=0, dyn_size, i;
00481 int ret = url_open_dyn_buf(&dyn_bc);
00482 if(ret < 0)
00483 return ret;
00484
00485 for (i=0; ff_nut_dispositions[i].flag; ++i) {
00486 if (st->disposition & ff_nut_dispositions[i].flag)
00487 count += add_info(dyn_bc, "Disposition", ff_nut_dispositions[i].str);
00488 }
00489 dyn_size = url_close_dyn_buf(dyn_bc, &dyn_buf);
00490
00491 if (count) {
00492 put_v(bc, stream_id + 1);
00493 put_v(bc, 0);
00494 put_v(bc, 0);
00495 put_v(bc, 0);
00496
00497 put_v(bc, count);
00498
00499 put_buffer(bc, dyn_buf, dyn_size);
00500 }
00501
00502 av_free(dyn_buf);
00503 return count;
00504 }
00505
00506 static int write_headers(NUTContext *nut, ByteIOContext *bc){
00507 ByteIOContext *dyn_bc;
00508 int i, ret;
00509
00510 ret = url_open_dyn_buf(&dyn_bc);
00511 if(ret < 0)
00512 return ret;
00513 write_mainheader(nut, dyn_bc);
00514 put_packet(nut, bc, dyn_bc, 1, MAIN_STARTCODE);
00515
00516 for (i=0; i < nut->avf->nb_streams; i++){
00517 ret = url_open_dyn_buf(&dyn_bc);
00518 if(ret < 0)
00519 return ret;
00520 write_streamheader(nut, dyn_bc, nut->avf->streams[i], i);
00521 put_packet(nut, bc, dyn_bc, 1, STREAM_STARTCODE);
00522 }
00523
00524 ret = url_open_dyn_buf(&dyn_bc);
00525 if(ret < 0)
00526 return ret;
00527 write_globalinfo(nut, dyn_bc);
00528 put_packet(nut, bc, dyn_bc, 1, INFO_STARTCODE);
00529
00530 for (i = 0; i < nut->avf->nb_streams; i++) {
00531 ret = url_open_dyn_buf(&dyn_bc);
00532 if(ret < 0)
00533 return ret;
00534 ret = write_streaminfo(nut, dyn_bc, i);
00535 if (ret < 0)
00536 return ret;
00537 if (ret > 0)
00538 put_packet(nut, bc, dyn_bc, 1, INFO_STARTCODE);
00539 else {
00540 uint8_t* buf;
00541 url_close_dyn_buf(dyn_bc, &buf);
00542 av_free(buf);
00543 }
00544 }
00545
00546 nut->last_syncpoint_pos= INT_MIN;
00547 nut->header_count++;
00548 return 0;
00549 }
00550
00551 static int write_header(AVFormatContext *s){
00552 NUTContext *nut = s->priv_data;
00553 ByteIOContext *bc = s->pb;
00554 int i, j;
00555
00556 nut->avf= s;
00557
00558 nut->stream = av_mallocz(sizeof(StreamContext)*s->nb_streams);
00559 nut->time_base= av_mallocz(sizeof(AVRational )*s->nb_streams);
00560
00561 for(i=0; i<s->nb_streams; i++){
00562 AVStream *st= s->streams[i];
00563 int ssize;
00564 AVRational time_base;
00565 ff_parse_specific_params(st->codec, &time_base.den, &ssize, &time_base.num);
00566
00567 av_set_pts_info(st, 64, time_base.num, time_base.den);
00568
00569 for(j=0; j<nut->time_base_count; j++){
00570 if(!memcmp(&time_base, &nut->time_base[j], sizeof(AVRational))){
00571 break;
00572 }
00573 }
00574 nut->time_base[j]= time_base;
00575 nut->stream[i].time_base= &nut->time_base[j];
00576 if(j==nut->time_base_count)
00577 nut->time_base_count++;
00578
00579 if(av_q2d(time_base) >= 0.001)
00580 nut->stream[i].msb_pts_shift = 7;
00581 else
00582 nut->stream[i].msb_pts_shift = 14;
00583 nut->stream[i].max_pts_distance= FFMAX(1/av_q2d(time_base), 1);
00584 }
00585
00586 nut->max_distance = MAX_DISTANCE;
00587 build_elision_headers(s);
00588 build_frame_code(s);
00589 assert(nut->frame_code['N'].flags == FLAG_INVALID);
00590
00591 put_buffer(bc, ID_STRING, strlen(ID_STRING));
00592 put_byte(bc, 0);
00593
00594 write_headers(nut, bc);
00595
00596 put_flush_packet(bc);
00597
00598
00599
00600 return 0;
00601 }
00602
00603 static int get_needed_flags(NUTContext *nut, StreamContext *nus, FrameCode *fc, AVPacket *pkt){
00604 int flags= 0;
00605
00606 if(pkt->flags & AV_PKT_FLAG_KEY ) flags |= FLAG_KEY;
00607 if(pkt->stream_index != fc->stream_id ) flags |= FLAG_STREAM_ID;
00608 if(pkt->size / fc->size_mul ) flags |= FLAG_SIZE_MSB;
00609 if(pkt->pts - nus->last_pts != fc->pts_delta) flags |= FLAG_CODED_PTS;
00610 if(pkt->size > 2*nut->max_distance ) flags |= FLAG_CHECKSUM;
00611 if(FFABS(pkt->pts - nus->last_pts)
00612 > nus->max_pts_distance) flags |= FLAG_CHECKSUM;
00613 if( pkt->size < nut->header_len[fc->header_idx]
00614 || (pkt->size > 4096 && fc->header_idx)
00615 || memcmp(pkt->data, nut->header[fc->header_idx], nut->header_len[fc->header_idx]))
00616 flags |= FLAG_HEADER_IDX;
00617
00618 return flags | (fc->flags & FLAG_CODED);
00619 }
00620
00621 static int find_best_header_idx(NUTContext *nut, AVPacket *pkt){
00622 int i;
00623 int best_i = 0;
00624 int best_len= 0;
00625
00626 if(pkt->size > 4096)
00627 return 0;
00628
00629 for(i=1; i<nut->header_count; i++){
00630 if( pkt->size >= nut->header_len[i]
00631 && nut->header_len[i] > best_len
00632 && !memcmp(pkt->data, nut->header[i], nut->header_len[i])){
00633 best_i= i;
00634 best_len= nut->header_len[i];
00635 }
00636 }
00637 return best_i;
00638 }
00639
00640 static int write_packet(AVFormatContext *s, AVPacket *pkt){
00641 NUTContext *nut = s->priv_data;
00642 StreamContext *nus= &nut->stream[pkt->stream_index];
00643 ByteIOContext *bc = s->pb, *dyn_bc;
00644 FrameCode *fc;
00645 int64_t coded_pts;
00646 int best_length, frame_code, flags, needed_flags, i, header_idx, best_header_idx;
00647 int key_frame = !!(pkt->flags & AV_PKT_FLAG_KEY);
00648 int store_sp=0;
00649 int ret;
00650
00651 if(pkt->pts < 0)
00652 return -1;
00653
00654 if(1LL<<(20+3*nut->header_count) <= url_ftell(bc))
00655 write_headers(nut, bc);
00656
00657 if(key_frame && !(nus->last_flags & FLAG_KEY))
00658 store_sp= 1;
00659
00660 if(pkt->size + 30 + url_ftell(bc) >= nut->last_syncpoint_pos + nut->max_distance)
00661 store_sp= 1;
00662
00663
00664
00665 if(store_sp){
00666 Syncpoint *sp, dummy= {.pos= INT64_MAX};
00667
00668 ff_nut_reset_ts(nut, *nus->time_base, pkt->dts);
00669 for(i=0; i<s->nb_streams; i++){
00670 AVStream *st= s->streams[i];
00671 int64_t dts_tb = av_rescale_rnd(pkt->dts,
00672 nus->time_base->num * (int64_t)nut->stream[i].time_base->den,
00673 nus->time_base->den * (int64_t)nut->stream[i].time_base->num,
00674 AV_ROUND_DOWN);
00675 int index= av_index_search_timestamp(st, dts_tb, AVSEEK_FLAG_BACKWARD);
00676 if(index>=0) dummy.pos= FFMIN(dummy.pos, st->index_entries[index].pos);
00677 }
00678 if(dummy.pos == INT64_MAX)
00679 dummy.pos= 0;
00680 sp= av_tree_find(nut->syncpoints, &dummy, (void *) ff_nut_sp_pos_cmp,
00681 NULL);
00682
00683 nut->last_syncpoint_pos= url_ftell(bc);
00684 ret = url_open_dyn_buf(&dyn_bc);
00685 if(ret < 0)
00686 return ret;
00687 put_tt(nut, nus, dyn_bc, pkt->dts);
00688 put_v(dyn_bc, sp ? (nut->last_syncpoint_pos - sp->pos)>>4 : 0);
00689 put_packet(nut, bc, dyn_bc, 1, SYNCPOINT_STARTCODE);
00690
00691 ff_nut_add_sp(nut, nut->last_syncpoint_pos, 0, pkt->dts);
00692 }
00693 assert(nus->last_pts != AV_NOPTS_VALUE);
00694
00695 coded_pts = pkt->pts & ((1<<nus->msb_pts_shift)-1);
00696 if(ff_lsb2full(nus, coded_pts) != pkt->pts)
00697 coded_pts= pkt->pts + (1<<nus->msb_pts_shift);
00698
00699 best_header_idx= find_best_header_idx(nut, pkt);
00700
00701 best_length=INT_MAX;
00702 frame_code= -1;
00703 for(i=0; i<256; i++){
00704 int length= 0;
00705 FrameCode *fc= &nut->frame_code[i];
00706 int flags= fc->flags;
00707
00708 if(flags & FLAG_INVALID)
00709 continue;
00710 needed_flags= get_needed_flags(nut, nus, fc, pkt);
00711
00712 if(flags & FLAG_CODED){
00713 length++;
00714 flags = needed_flags;
00715 }
00716
00717 if((flags & needed_flags) != needed_flags)
00718 continue;
00719
00720 if((flags ^ needed_flags) & FLAG_KEY)
00721 continue;
00722
00723 if(flags & FLAG_STREAM_ID)
00724 length+= get_length(pkt->stream_index);
00725
00726 if(pkt->size % fc->size_mul != fc->size_lsb)
00727 continue;
00728 if(flags & FLAG_SIZE_MSB)
00729 length += get_length(pkt->size / fc->size_mul);
00730
00731 if(flags & FLAG_CHECKSUM)
00732 length+=4;
00733
00734 if(flags & FLAG_CODED_PTS)
00735 length += get_length(coded_pts);
00736
00737 if( (flags & FLAG_CODED)
00738 && nut->header_len[best_header_idx] > nut->header_len[fc->header_idx]+1){
00739 flags |= FLAG_HEADER_IDX;
00740 }
00741
00742 if(flags & FLAG_HEADER_IDX){
00743 length += 1 - nut->header_len[best_header_idx];
00744 }else{
00745 length -= nut->header_len[fc->header_idx];
00746 }
00747
00748 length*=4;
00749 length+= !(flags & FLAG_CODED_PTS);
00750 length+= !(flags & FLAG_CHECKSUM);
00751
00752 if(length < best_length){
00753 best_length= length;
00754 frame_code=i;
00755 }
00756 }
00757 assert(frame_code != -1);
00758 fc= &nut->frame_code[frame_code];
00759 flags= fc->flags;
00760 needed_flags= get_needed_flags(nut, nus, fc, pkt);
00761 header_idx= fc->header_idx;
00762
00763 init_checksum(bc, ff_crc04C11DB7_update, 0);
00764 put_byte(bc, frame_code);
00765 if(flags & FLAG_CODED){
00766 put_v(bc, (flags^needed_flags) & ~(FLAG_CODED));
00767 flags = needed_flags;
00768 }
00769 if(flags & FLAG_STREAM_ID) put_v(bc, pkt->stream_index);
00770 if(flags & FLAG_CODED_PTS) put_v(bc, coded_pts);
00771 if(flags & FLAG_SIZE_MSB) put_v(bc, pkt->size / fc->size_mul);
00772 if(flags & FLAG_HEADER_IDX) put_v(bc, header_idx= best_header_idx);
00773
00774 if(flags & FLAG_CHECKSUM) put_le32(bc, get_checksum(bc));
00775 else get_checksum(bc);
00776
00777 put_buffer(bc, pkt->data + nut->header_len[header_idx], pkt->size - nut->header_len[header_idx]);
00778 nus->last_flags= flags;
00779 nus->last_pts= pkt->pts;
00780
00781
00782 if(flags & FLAG_KEY)
00783 av_add_index_entry(
00784 s->streams[pkt->stream_index],
00785 nut->last_syncpoint_pos,
00786 pkt->pts,
00787 0,
00788 0,
00789 AVINDEX_KEYFRAME);
00790
00791 return 0;
00792 }
00793
00794 static int write_trailer(AVFormatContext *s){
00795 NUTContext *nut= s->priv_data;
00796 ByteIOContext *bc= s->pb;
00797
00798 while(nut->header_count<3)
00799 write_headers(nut, bc);
00800 put_flush_packet(bc);
00801 ff_nut_free_sp(nut);
00802 av_freep(&nut->stream);
00803 av_freep(&nut->time_base);
00804
00805 return 0;
00806 }
00807
00808 AVOutputFormat nut_muxer = {
00809 "nut",
00810 NULL_IF_CONFIG_SMALL("NUT format"),
00811 "video/x-nut",
00812 "nut",
00813 sizeof(NUTContext),
00814 #if CONFIG_LIBVORBIS
00815 CODEC_ID_VORBIS,
00816 #elif CONFIG_LIBMP3LAME
00817 CODEC_ID_MP3,
00818 #else
00819 CODEC_ID_MP2,
00820 #endif
00821 CODEC_ID_MPEG4,
00822 write_header,
00823 write_packet,
00824 write_trailer,
00825 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
00826 .codec_tag= (const AVCodecTag* const []){ff_codec_bmp_tags, ff_codec_wav_tags, ff_nut_subtitle_tags, 0},
00827 .metadata_conv = ff_nut_metadata_conv,
00828 };