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