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