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