avformat: Remove non-compiling and/or silly commented-out printf/av_log statements
[libav.git] / libavformat / nutenc.c
CommitLineData
70ea1e69
MN
1/*
2 * nut muxer
3 * Copyright (c) 2004-2007 Michael Niedermayer
4 *
2912e87a 5 * This file is part of Libav.
70ea1e69 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
70ea1e69
MN
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
70ea1e69
MN
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
70ea1e69
MN
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
6a5d31ac 22#include "libavutil/intreadwrite.h"
0ebcdf5c 23#include "libavutil/mathematics.h"
245976da 24#include "libavutil/tree.h"
d2d67e42 25#include "libavutil/dict.h"
245976da 26#include "libavcodec/mpegaudiodata.h"
70ea1e69 27#include "nut.h"
80b39e1c 28#include "internal.h"
4c4427a7 29#include "avio_internal.h"
a712d725
MN
30
31static int find_expected_header(AVCodecContext *c, int size, int key_frame, uint8_t out[64]){
32 int sample_rate= c->sample_rate;
33
34 if(size>4096)
35 return 0;
36
37 AV_WB24(out, 1);
38
36ef5369 39 if(c->codec_id == AV_CODEC_ID_MPEG4){
a712d725
MN
40 if(key_frame){
41 return 3;
42 }else{
43 out[3]= 0xB6;
44 return 4;
45 }
36ef5369 46 }else if(c->codec_id == AV_CODEC_ID_MPEG1VIDEO || c->codec_id == AV_CODEC_ID_MPEG2VIDEO){
a712d725 47 return 3;
36ef5369 48 }else if(c->codec_id == AV_CODEC_ID_H264){
a712d725 49 return 3;
36ef5369 50 }else if(c->codec_id == AV_CODEC_ID_MP3 || c->codec_id == AV_CODEC_ID_MP2){
a712d725 51 int lsf, mpeg25, sample_rate_index, bitrate_index, frame_size;
36ef5369 52 int layer= c->codec_id == AV_CODEC_ID_MP3 ? 3 : 2;
a712d725
MN
53 unsigned int header= 0xFFF00000;
54
55 lsf = sample_rate < (24000+32000)/2;
56 mpeg25 = sample_rate < (12000+16000)/2;
57 sample_rate <<= lsf + mpeg25;
58 if (sample_rate < (32000 + 44100)/2) sample_rate_index=2;
59 else if(sample_rate < (44100 + 48000)/2) sample_rate_index=0;
60 else sample_rate_index=1;
61
82ab61f9 62 sample_rate= avpriv_mpa_freq_tab[sample_rate_index] >> (lsf + mpeg25);
a712d725
MN
63
64 for(bitrate_index=2; bitrate_index<30; bitrate_index++){
82ab61f9 65 frame_size = avpriv_mpa_bitrate_tab[lsf][layer-1][bitrate_index>>1];
a712d725
MN
66 frame_size = (frame_size * 144000) / (sample_rate << lsf) + (bitrate_index&1);
67
68 if(frame_size == size)
69 break;
70 }
71
72 header |= (!lsf)<<19;
73 header |= (4-layer)<<17;
74 header |= 1<<16; //no crc
75 AV_WB32(out, header);
76 if(size <= 0)
7ce68923 77 return 2; //we guess there is no crc, if there is one the user clearly does not care about overhead
a712d725
MN
78 if(bitrate_index == 30)
79 return -1; //something is wrong ...
80
81 header |= (bitrate_index>>1)<<12;
82 header |= sample_rate_index<<10;
83 header |= (bitrate_index&1)<<9;
84
85 return 2; //FIXME actually put the needed ones in build_elision_headers()
7ce68923
DB
86 return 3; //we guess that the private bit is not set
87//FIXME the above assumptions should be checked, if these turn out false too often something should be done
a712d725
MN
88 }
89 return 0;
90}
91
92static int find_header_idx(AVFormatContext *s, AVCodecContext *c, int size, int frame_type){
93 NUTContext *nut = s->priv_data;
94 uint8_t out[64];
95 int i;
96 int len= find_expected_header(c, size, frame_type, out);
97
a712d725
MN
98 for(i=1; i<nut->header_count; i++){
99 if( len == nut->header_len[i]
100 && !memcmp(out, nut->header[i], len)){
a712d725
MN
101 return i;
102 }
103 }
a712d725
MN
104 return 0;
105}
106
107static void build_elision_headers(AVFormatContext *s){
108 NUTContext *nut = s->priv_data;
109 int i;
110 //FIXME this is lame
111 //FIXME write a 2pass mode to find the maximal headers
1bf6e565 112 static const uint8_t headers[][5]={
a712d725
MN
113 {3, 0x00, 0x00, 0x01},
114 {4, 0x00, 0x00, 0x01, 0xB6},
115 {2, 0xFF, 0xFA}, //mp3+crc
116 {2, 0xFF, 0xFB}, //mp3
117 {2, 0xFF, 0xFC}, //mp2+crc
118 {2, 0xFF, 0xFD}, //mp2
119 };
120
121 nut->header_count= 7;
122 for(i=1; i<nut->header_count; i++){
123 nut->header_len[i]= headers[i-1][0];
124 nut->header [i]= &headers[i-1][1];
125 }
126}
70ea1e69 127
70ea1e69
MN
128static void build_frame_code(AVFormatContext *s){
129 NUTContext *nut = s->priv_data;
130 int key_frame, index, pred, stream_id;
131 int start=1;
132 int end= 254;
133 int keyframe_0_esc= s->nb_streams > 2;
134 int pred_table[10];
0fcfa8eb
MN
135 FrameCode *ft;
136
137 ft= &nut->frame_code[start];
138 ft->flags= FLAG_CODED;
139 ft->size_mul=1;
140 ft->pts_delta=1;
141 start++;
70ea1e69
MN
142
143 if(keyframe_0_esc){
144 /* keyframe = 0 escape */
145 FrameCode *ft= &nut->frame_code[start];
146 ft->flags= FLAG_STREAM_ID | FLAG_SIZE_MSB | FLAG_CODED_PTS;
147 ft->size_mul=1;
148 start++;
149 }
150
151 for(stream_id= 0; stream_id<s->nb_streams; stream_id++){
152 int start2= start + (end-start)*stream_id / s->nb_streams;
153 int end2 = start + (end-start)*(stream_id+1) / s->nb_streams;
154 AVCodecContext *codec = s->streams[stream_id]->codec;
72415b2a 155 int is_audio= codec->codec_type == AVMEDIA_TYPE_AUDIO;
70ea1e69
MN
156 int intra_only= /*codec->intra_only || */is_audio;
157 int pred_count;
158
159 for(key_frame=0; key_frame<2; key_frame++){
160 if(intra_only && keyframe_0_esc && key_frame==0)
161 continue;
162
163 {
164 FrameCode *ft= &nut->frame_code[start2];
165 ft->flags= FLAG_KEY*key_frame;
166 ft->flags|= FLAG_SIZE_MSB | FLAG_CODED_PTS;
167 ft->stream_id= stream_id;
168 ft->size_mul=1;
a712d725
MN
169 if(is_audio)
170 ft->header_idx= find_header_idx(s, codec, -1, key_frame);
70ea1e69
MN
171 start2++;
172 }
173 }
174
175 key_frame= intra_only;
70ea1e69
MN
176 if(is_audio){
177 int frame_bytes= codec->frame_size*(int64_t)codec->bit_rate / (8*codec->sample_rate);
178 int pts;
179 for(pts=0; pts<2; pts++){
180 for(pred=0; pred<2; pred++){
181 FrameCode *ft= &nut->frame_code[start2];
182 ft->flags= FLAG_KEY*key_frame;
183 ft->stream_id= stream_id;
184 ft->size_mul=frame_bytes + 2;
185 ft->size_lsb=frame_bytes + pred;
186 ft->pts_delta=pts;
a712d725 187 ft->header_idx= find_header_idx(s, codec, frame_bytes + pred, key_frame);
70ea1e69
MN
188 start2++;
189 }
190 }
191 }else{
192 FrameCode *ft= &nut->frame_code[start2];
193 ft->flags= FLAG_KEY | FLAG_SIZE_MSB;
194 ft->stream_id= stream_id;
195 ft->size_mul=1;
196 ft->pts_delta=1;
197 start2++;
198 }
70ea1e69
MN
199
200 if(codec->has_b_frames){
201 pred_count=5;
202 pred_table[0]=-2;
203 pred_table[1]=-1;
204 pred_table[2]=1;
205 pred_table[3]=3;
206 pred_table[4]=4;
36ef5369 207 }else if(codec->codec_id == AV_CODEC_ID_VORBIS){
70ea1e69
MN
208 pred_count=3;
209 pred_table[0]=2;
210 pred_table[1]=9;
211 pred_table[2]=16;
212 }else{
213 pred_count=1;
214 pred_table[0]=1;
215 }
216
217 for(pred=0; pred<pred_count; pred++){
218 int start3= start2 + (end2-start2)*pred / pred_count;
219 int end3 = start2 + (end2-start2)*(pred+1) / pred_count;
220
221 for(index=start3; index<end3; index++){
222 FrameCode *ft= &nut->frame_code[index];
223 ft->flags= FLAG_KEY*key_frame;
224 ft->flags|= FLAG_SIZE_MSB;
225 ft->stream_id= stream_id;
226//FIXME use single byte size and pred from last
227 ft->size_mul= end3-start3;
228 ft->size_lsb= index - start3;
229 ft->pts_delta= pred_table[pred];
a712d725
MN
230 if(is_audio)
231 ft->header_idx= find_header_idx(s, codec, -1, key_frame);
70ea1e69
MN
232 }
233 }
234 }
235 memmove(&nut->frame_code['N'+1], &nut->frame_code['N'], sizeof(FrameCode)*(255-'N'));
236 nut->frame_code[ 0].flags=
237 nut->frame_code[255].flags=
238 nut->frame_code['N'].flags= FLAG_INVALID;
239}
240
073f8b10 241static void put_tt(NUTContext *nut, AVRational *time_base, AVIOContext *bc, uint64_t val){
491216c6 242 val *= nut->time_base_count;
073f8b10 243 val += time_base - nut->time_base;
f8280ff4 244 ff_put_v(bc, val);
491216c6
MN
245}
246
70ea1e69 247/**
49bd8e4b 248 * Store a string as vb.
70ea1e69 249 */
ae628ec1 250static void put_str(AVIOContext *bc, const char *string){
70ea1e69
MN
251 int len= strlen(string);
252
f8280ff4 253 ff_put_v(bc, len);
77eb5504 254 avio_write(bc, string, len);
70ea1e69
MN
255}
256
ae628ec1 257static void put_s(AVIOContext *bc, int64_t val){
f8280ff4 258 ff_put_v(bc, 2*FFABS(val) - (val>0));
70ea1e69
MN
259}
260
261#ifdef TRACE
bc66827f
DB
262static inline void ff_put_v_trace(AVIOContext *bc, uint64_t v, const char *file,
263 const char *func, int line)
264{
f8280ff4 265 av_log(NULL, AV_LOG_DEBUG, "ff_put_v %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
70ea1e69 266
f8280ff4 267 ff_put_v(bc, v);
70ea1e69
MN
268}
269
bc66827f
DB
270static inline void put_s_trace(AVIOContext *bc, int64_t v, const char *file,
271 const char *func, int line)
272{
7a26da44 273 av_log(NULL, AV_LOG_DEBUG, "put_s %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
70ea1e69
MN
274
275 put_s(bc, v);
276}
f8280ff4 277#define ff_put_v(bc, v) ff_put_v_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
70ea1e69
MN
278#define put_s(bc, v) put_s_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
279#endif
280
34f8cc38 281//FIXME remove calculate_checksum
ae628ec1 282static void put_packet(NUTContext *nut, AVIOContext *bc, AVIOContext *dyn_bc, int calculate_checksum, uint64_t startcode){
8d9a3f7e 283 uint8_t *dyn_buf=NULL;
6dc7d80d 284 int dyn_size= avio_close_dyn_buf(dyn_bc, &dyn_buf);
6c666941
MN
285 int forw_ptr= dyn_size + 4*calculate_checksum;
286
287 if(forw_ptr > 4096)
4c4427a7 288 ffio_init_checksum(bc, ff_crc04C11DB7_update, 0);
77eb5504 289 avio_wb64(bc, startcode);
f8280ff4 290 ff_put_v(bc, forw_ptr);
6c666941 291 if(forw_ptr > 4096)
b64030f2 292 avio_wl32(bc, ffio_get_checksum(bc));
70ea1e69
MN
293
294 if(calculate_checksum)
4c4427a7 295 ffio_init_checksum(bc, ff_crc04C11DB7_update, 0);
77eb5504 296 avio_write(bc, dyn_buf, dyn_size);
70ea1e69 297 if(calculate_checksum)
b64030f2 298 avio_wl32(bc, ffio_get_checksum(bc));
70ea1e69 299
8d9a3f7e 300 av_free(dyn_buf);
70ea1e69
MN
301}
302
ae628ec1 303static void write_mainheader(NUTContext *nut, AVIOContext *bc){
a712d725
MN
304 int i, j, tmp_pts, tmp_flags, tmp_stream, tmp_mul, tmp_size, tmp_fields, tmp_head_idx;
305 int64_t tmp_match;
70ea1e69 306
f8280ff4
MC
307 ff_put_v(bc, 3); /* version */
308 ff_put_v(bc, nut->avf->nb_streams);
309 ff_put_v(bc, nut->max_distance);
310 ff_put_v(bc, nut->time_base_count);
70ea1e69
MN
311
312 for(i=0; i<nut->time_base_count; i++){
f8280ff4
MC
313 ff_put_v(bc, nut->time_base[i].num);
314 ff_put_v(bc, nut->time_base[i].den);
70ea1e69
MN
315 }
316
70ea1e69
MN
317 tmp_pts=0;
318 tmp_mul=1;
319 tmp_stream=0;
a712d725
MN
320 tmp_match= 1-(1LL<<62);
321 tmp_head_idx= 0;
70ea1e69
MN
322 for(i=0; i<256;){
323 tmp_fields=0;
324 tmp_size=0;
325// tmp_res=0;
326 if(tmp_pts != nut->frame_code[i].pts_delta) tmp_fields=1;
327 if(tmp_mul != nut->frame_code[i].size_mul ) tmp_fields=2;
328 if(tmp_stream != nut->frame_code[i].stream_id) tmp_fields=3;
329 if(tmp_size != nut->frame_code[i].size_lsb ) tmp_fields=4;
330// if(tmp_res != nut->frame_code[i].res ) tmp_fields=5;
a712d725 331 if(tmp_head_idx!=nut->frame_code[i].header_idx)tmp_fields=8;
70ea1e69
MN
332
333 tmp_pts = nut->frame_code[i].pts_delta;
334 tmp_flags = nut->frame_code[i].flags;
335 tmp_stream= nut->frame_code[i].stream_id;
336 tmp_mul = nut->frame_code[i].size_mul;
337 tmp_size = nut->frame_code[i].size_lsb;
338// tmp_res = nut->frame_code[i].res;
a712d725 339 tmp_head_idx= nut->frame_code[i].header_idx;
70ea1e69
MN
340
341 for(j=0; i<256; j++,i++){
342 if(i == 'N'){
343 j--;
344 continue;
345 }
346 if(nut->frame_code[i].pts_delta != tmp_pts ) break;
347 if(nut->frame_code[i].flags != tmp_flags ) break;
348 if(nut->frame_code[i].stream_id != tmp_stream) break;
349 if(nut->frame_code[i].size_mul != tmp_mul ) break;
350 if(nut->frame_code[i].size_lsb != tmp_size+j) break;
351// if(nut->frame_code[i].res != tmp_res ) break;
a712d725 352 if(nut->frame_code[i].header_idx!= tmp_head_idx) break;
70ea1e69
MN
353 }
354 if(j != tmp_mul - tmp_size) tmp_fields=6;
355
f8280ff4
MC
356 ff_put_v(bc, tmp_flags);
357 ff_put_v(bc, tmp_fields);
70ea1e69 358 if(tmp_fields>0) put_s(bc, tmp_pts);
f8280ff4
MC
359 if(tmp_fields>1) ff_put_v(bc, tmp_mul);
360 if(tmp_fields>2) ff_put_v(bc, tmp_stream);
361 if(tmp_fields>3) ff_put_v(bc, tmp_size);
362 if(tmp_fields>4) ff_put_v(bc, 0 /*tmp_res*/);
363 if(tmp_fields>5) ff_put_v(bc, j);
364 if(tmp_fields>6) ff_put_v(bc, tmp_match);
365 if(tmp_fields>7) ff_put_v(bc, tmp_head_idx);
a712d725 366 }
f8280ff4 367 ff_put_v(bc, nut->header_count-1);
a712d725 368 for(i=1; i<nut->header_count; i++){
f8280ff4 369 ff_put_v(bc, nut->header_len[i]);
77eb5504 370 avio_write(bc, nut->header[i], nut->header_len[i]);
70ea1e69 371 }
97d5eb11 372}
70ea1e69 373
ae628ec1 374static int write_streamheader(AVFormatContext *avctx, AVIOContext *bc, AVStream *st, int i){
da9e6c42 375 NUTContext *nut = avctx->priv_data;
59729451 376 AVCodecContext *codec = st->codec;
f8280ff4 377 ff_put_v(bc, i);
0809b51e 378 switch(codec->codec_type){
f8280ff4
MC
379 case AVMEDIA_TYPE_VIDEO: ff_put_v(bc, 0); break;
380 case AVMEDIA_TYPE_AUDIO: ff_put_v(bc, 1); break;
381 case AVMEDIA_TYPE_SUBTITLE: ff_put_v(bc, 2); break;
382 default : ff_put_v(bc, 3); break;
0809b51e 383 }
f8280ff4 384 ff_put_v(bc, 4);
0809b51e 385 if (codec->codec_tag){
77eb5504 386 avio_wl32(bc, codec->codec_tag);
fa586439
SS
387 } else {
388 av_log(avctx, AV_LOG_ERROR, "No codec tag defined for stream %d\n", i);
ffd5f098 389 return AVERROR(EINVAL);
fa586439 390 }
0809b51e 391
f8280ff4
MC
392 ff_put_v(bc, nut->stream[i].time_base - nut->time_base);
393 ff_put_v(bc, nut->stream[i].msb_pts_shift);
394 ff_put_v(bc, nut->stream[i].max_pts_distance);
395 ff_put_v(bc, codec->has_b_frames);
77eb5504 396 avio_w8(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */
0809b51e 397
f8280ff4 398 ff_put_v(bc, codec->extradata_size);
77eb5504 399 avio_write(bc, codec->extradata, codec->extradata_size);
0809b51e
MN
400
401 switch(codec->codec_type){
72415b2a 402 case AVMEDIA_TYPE_AUDIO:
f8280ff4
MC
403 ff_put_v(bc, codec->sample_rate);
404 ff_put_v(bc, 1);
405 ff_put_v(bc, codec->channels);
0809b51e 406 break;
72415b2a 407 case AVMEDIA_TYPE_VIDEO:
f8280ff4
MC
408 ff_put_v(bc, codec->width);
409 ff_put_v(bc, codec->height);
0a5790d1 410
59729451 411 if(st->sample_aspect_ratio.num<=0 || st->sample_aspect_ratio.den<=0){
f8280ff4
MC
412 ff_put_v(bc, 0);
413 ff_put_v(bc, 0);
0a5790d1 414 }else{
f8280ff4
MC
415 ff_put_v(bc, st->sample_aspect_ratio.num);
416 ff_put_v(bc, st->sample_aspect_ratio.den);
0a5790d1 417 }
f8280ff4 418 ff_put_v(bc, 0); /* csp type -- unknown */
0809b51e
MN
419 break;
420 default:
421 break;
422 }
423 return 0;
424}
425
ae628ec1 426static int add_info(AVIOContext *bc, const char *type, const char *value){
ead1939f
MN
427 put_str(bc, type);
428 put_s(bc, -1);
429 put_str(bc, value);
430 return 1;
431}
432
ae628ec1 433static int write_globalinfo(NUTContext *nut, AVIOContext *bc){
ead1939f 434 AVFormatContext *s= nut->avf;
d2d67e42 435 AVDictionaryEntry *t = NULL;
ae628ec1 436 AVIOContext *dyn_bc;
ead1939f
MN
437 uint8_t *dyn_buf=NULL;
438 int count=0, dyn_size;
b92c5452 439 int ret = avio_open_dyn_buf(&dyn_bc);
899681cd
BA
440 if(ret < 0)
441 return ret;
ead1939f 442
d2d67e42 443 while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX)))
43382b5f 444 count += add_info(dyn_bc, t->key, t->value);
ead1939f 445
f8280ff4
MC
446 ff_put_v(bc, 0); //stream_if_plus1
447 ff_put_v(bc, 0); //chapter_id
448 ff_put_v(bc, 0); //timestamp_start
449 ff_put_v(bc, 0); //length
ead1939f 450
f8280ff4 451 ff_put_v(bc, count);
ead1939f 452
6dc7d80d 453 dyn_size= avio_close_dyn_buf(dyn_bc, &dyn_buf);
77eb5504 454 avio_write(bc, dyn_buf, dyn_size);
ead1939f 455 av_free(dyn_buf);
899681cd 456 return 0;
ead1939f
MN
457}
458
ae628ec1 459static int write_streaminfo(NUTContext *nut, AVIOContext *bc, int stream_id){
90c2295b
ES
460 AVFormatContext *s= nut->avf;
461 AVStream* st = s->streams[stream_id];
ae628ec1 462 AVIOContext *dyn_bc;
90c2295b
ES
463 uint8_t *dyn_buf=NULL;
464 int count=0, dyn_size, i;
b92c5452 465 int ret = avio_open_dyn_buf(&dyn_bc);
90c2295b
ES
466 if(ret < 0)
467 return ret;
468
469 for (i=0; ff_nut_dispositions[i].flag; ++i) {
470 if (st->disposition & ff_nut_dispositions[i].flag)
471 count += add_info(dyn_bc, "Disposition", ff_nut_dispositions[i].str);
472 }
6dc7d80d 473 dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
90c2295b
ES
474
475 if (count) {
f8280ff4
MC
476 ff_put_v(bc, stream_id + 1); //stream_id_plus1
477 ff_put_v(bc, 0); //chapter_id
478 ff_put_v(bc, 0); //timestamp_start
479 ff_put_v(bc, 0); //length
90c2295b 480
f8280ff4 481 ff_put_v(bc, count);
90c2295b 482
77eb5504 483 avio_write(bc, dyn_buf, dyn_size);
90c2295b
ES
484 }
485
486 av_free(dyn_buf);
487 return count;
488}
489
073f8b10
AK
490static int write_chapter(NUTContext *nut, AVIOContext *bc, int id)
491{
492 AVIOContext *dyn_bc;
493 uint8_t *dyn_buf = NULL;
d2d67e42 494 AVDictionaryEntry *t = NULL;
073f8b10
AK
495 AVChapter *ch = nut->avf->chapters[id];
496 int ret, dyn_size, count = 0;
497
b92c5452 498 ret = avio_open_dyn_buf(&dyn_bc);
073f8b10
AK
499 if (ret < 0)
500 return ret;
501
502 ff_put_v(bc, 0); // stream_id_plus1
503 put_s(bc, id + 1); // chapter_id
504 put_tt(nut, nut->chapter[id].time_base, bc, ch->start); // chapter_start
505 ff_put_v(bc, ch->end - ch->start); // chapter_len
506
d2d67e42 507 while ((t = av_dict_get(ch->metadata, "", t, AV_DICT_IGNORE_SUFFIX)))
073f8b10
AK
508 count += add_info(dyn_bc, t->key, t->value);
509
510 ff_put_v(bc, count);
511
6dc7d80d 512 dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
073f8b10
AK
513 avio_write(bc, dyn_buf, dyn_size);
514 av_freep(&dyn_buf);
515 return 0;
516}
517
ae628ec1 518static int write_headers(AVFormatContext *avctx, AVIOContext *bc){
da9e6c42 519 NUTContext *nut = avctx->priv_data;
ae628ec1 520 AVIOContext *dyn_bc;
899681cd 521 int i, ret;
7e506494 522
03700d39
AK
523 ff_metadata_conv_ctx(avctx, ff_nut_metadata_conv, NULL);
524
b92c5452 525 ret = avio_open_dyn_buf(&dyn_bc);
899681cd
BA
526 if(ret < 0)
527 return ret;
528 write_mainheader(nut, dyn_bc);
529 put_packet(nut, bc, dyn_bc, 1, MAIN_STARTCODE);
7e506494
MN
530
531 for (i=0; i < nut->avf->nb_streams; i++){
b92c5452 532 ret = avio_open_dyn_buf(&dyn_bc);
899681cd
BA
533 if(ret < 0)
534 return ret;
fc0c4124
SS
535 if ((ret = write_streamheader(avctx, dyn_bc, nut->avf->streams[i], i)) < 0)
536 return ret;
899681cd 537 put_packet(nut, bc, dyn_bc, 1, STREAM_STARTCODE);
7e506494 538 }
ead1939f 539
b92c5452 540 ret = avio_open_dyn_buf(&dyn_bc);
899681cd
BA
541 if(ret < 0)
542 return ret;
543 write_globalinfo(nut, dyn_bc);
544 put_packet(nut, bc, dyn_bc, 1, INFO_STARTCODE);
14b3f996 545
90c2295b 546 for (i = 0; i < nut->avf->nb_streams; i++) {
b92c5452 547 ret = avio_open_dyn_buf(&dyn_bc);
90c2295b
ES
548 if(ret < 0)
549 return ret;
550 ret = write_streaminfo(nut, dyn_bc, i);
551 if (ret < 0)
552 return ret;
553 if (ret > 0)
554 put_packet(nut, bc, dyn_bc, 1, INFO_STARTCODE);
555 else {
556 uint8_t* buf;
6dc7d80d 557 avio_close_dyn_buf(dyn_bc, &buf);
90c2295b
ES
558 av_free(buf);
559 }
560 }
561
073f8b10 562 for (i = 0; i < nut->avf->nb_chapters; i++) {
b92c5452 563 ret = avio_open_dyn_buf(&dyn_bc);
073f8b10
AK
564 if (ret < 0)
565 return ret;
566 ret = write_chapter(nut, dyn_bc, i);
567 if (ret < 0) {
568 uint8_t *buf;
6dc7d80d 569 avio_close_dyn_buf(dyn_bc, &buf);
073f8b10
AK
570 av_freep(&buf);
571 return ret;
572 }
573 put_packet(nut, bc, dyn_bc, 1, INFO_STARTCODE);
574 }
575
14b3f996
MN
576 nut->last_syncpoint_pos= INT_MIN;
577 nut->header_count++;
899681cd 578 return 0;
7e506494
MN
579}
580
5ee5dc4e 581static int nut_write_header(AVFormatContext *s){
97d5eb11 582 NUTContext *nut = s->priv_data;
ae628ec1 583 AVIOContext *bc = s->pb;
fc0c4124 584 int i, j, ret;
97d5eb11
MN
585
586 nut->avf= s;
587
588 nut->stream = av_mallocz(sizeof(StreamContext)*s->nb_streams);
25588d3b
RB
589 if (s->nb_chapters)
590 nut->chapter = av_mallocz(sizeof(ChapterContext)*s->nb_chapters);
073f8b10
AK
591 nut->time_base= av_mallocz(sizeof(AVRational )*(s->nb_streams +
592 s->nb_chapters));
76432655
RB
593 if (!nut->stream || (s->nb_chapters && !nut->chapter) || !nut->time_base) {
594 av_freep(&nut->stream);
595 av_freep(&nut->chapter);
596 av_freep(&nut->time_base);
597 return AVERROR(ENOMEM);
598 }
97d5eb11
MN
599
600 for(i=0; i<s->nb_streams; i++){
601 AVStream *st= s->streams[i];
602 int ssize;
603 AVRational time_base;
604 ff_parse_specific_params(st->codec, &time_base.den, &ssize, &time_base.num);
605
c3f9ebf7 606 avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
97d5eb11
MN
607
608 for(j=0; j<nut->time_base_count; j++){
609 if(!memcmp(&time_base, &nut->time_base[j], sizeof(AVRational))){
610 break;
611 }
612 }
613 nut->time_base[j]= time_base;
614 nut->stream[i].time_base= &nut->time_base[j];
615 if(j==nut->time_base_count)
616 nut->time_base_count++;
617
324d22b2 618 if(INT64_C(1000) * time_base.num >= time_base.den)
97d5eb11
MN
619 nut->stream[i].msb_pts_shift = 7;
620 else
621 nut->stream[i].msb_pts_shift = 14;
324d22b2 622 nut->stream[i].max_pts_distance= FFMAX(time_base.den, time_base.num) / time_base.num;
97d5eb11
MN
623 }
624
073f8b10
AK
625 for (i = 0; i < s->nb_chapters; i++) {
626 AVChapter *ch = s->chapters[i];
627
628 for (j = 0; j < nut->time_base_count; j++) {
629 if (!memcmp(&ch->time_base, &nut->time_base[j], sizeof(AVRational)))
630 break;
631 }
632
633 nut->time_base[j] = ch->time_base;
634 nut->chapter[i].time_base = &nut->time_base[j];
635 if(j == nut->time_base_count)
636 nut->time_base_count++;
637 }
638
82badd27 639 nut->max_distance = MAX_DISTANCE;
a712d725 640 build_elision_headers(s);
97d5eb11
MN
641 build_frame_code(s);
642 assert(nut->frame_code['N'].flags == FLAG_INVALID);
643
77eb5504
AK
644 avio_write(bc, ID_STRING, strlen(ID_STRING));
645 avio_w8(bc, 0);
97d5eb11 646
fc0c4124
SS
647 if ((ret = write_headers(s, bc)) < 0)
648 return ret;
dcc9bde0 649
b7f2fdde 650 avio_flush(bc);
70ea1e69 651
14b3f996 652 //FIXME index
70ea1e69
MN
653
654 return 0;
655}
656
e1249ba9
MN
657static int get_needed_flags(NUTContext *nut, StreamContext *nus, FrameCode *fc, AVPacket *pkt){
658 int flags= 0;
659
cc947f04 660 if(pkt->flags & AV_PKT_FLAG_KEY ) flags |= FLAG_KEY;
e1249ba9
MN
661 if(pkt->stream_index != fc->stream_id ) flags |= FLAG_STREAM_ID;
662 if(pkt->size / fc->size_mul ) flags |= FLAG_SIZE_MSB;
663 if(pkt->pts - nus->last_pts != fc->pts_delta) flags |= FLAG_CODED_PTS;
664 if(pkt->size > 2*nut->max_distance ) flags |= FLAG_CHECKSUM;
665 if(FFABS(pkt->pts - nus->last_pts)
666 > nus->max_pts_distance) flags |= FLAG_CHECKSUM;
a712d725
MN
667 if( pkt->size < nut->header_len[fc->header_idx]
668 || (pkt->size > 4096 && fc->header_idx)
669 || memcmp(pkt->data, nut->header[fc->header_idx], nut->header_len[fc->header_idx]))
670 flags |= FLAG_HEADER_IDX;
e1249ba9 671
d2062ff6 672 return flags | (fc->flags & FLAG_CODED);
e1249ba9
MN
673}
674
a712d725
MN
675static int find_best_header_idx(NUTContext *nut, AVPacket *pkt){
676 int i;
677 int best_i = 0;
678 int best_len= 0;
679
680 if(pkt->size > 4096)
681 return 0;
682
683 for(i=1; i<nut->header_count; i++){
684 if( pkt->size >= nut->header_len[i]
685 && nut->header_len[i] > best_len
686 && !memcmp(pkt->data, nut->header[i], nut->header_len[i])){
687 best_i= i;
688 best_len= nut->header_len[i];
689 }
690 }
691 return best_i;
692}
693
5ee5dc4e 694static int nut_write_packet(AVFormatContext *s, AVPacket *pkt){
e1249ba9
MN
695 NUTContext *nut = s->priv_data;
696 StreamContext *nus= &nut->stream[pkt->stream_index];
ae628ec1 697 AVIOContext *bc = s->pb, *dyn_bc;
e1249ba9
MN
698 FrameCode *fc;
699 int64_t coded_pts;
a712d725 700 int best_length, frame_code, flags, needed_flags, i, header_idx, best_header_idx;
cc947f04 701 int key_frame = !!(pkt->flags & AV_PKT_FLAG_KEY);
e1249ba9 702 int store_sp=0;
899681cd 703 int ret;
e1249ba9 704
f53dd468
MN
705 if(pkt->pts < 0)
706 return -1;
707
a2704c97 708 if(1LL<<(20+3*nut->header_count) <= avio_tell(bc))
da9e6c42 709 write_headers(s, bc);
14b3f996 710
02c986a8 711 if(key_frame && !(nus->last_flags & FLAG_KEY))
e1249ba9
MN
712 store_sp= 1;
713
a2704c97 714 if(pkt->size + 30/*FIXME check*/ + avio_tell(bc) >= nut->last_syncpoint_pos + nut->max_distance)
e1249ba9
MN
715 store_sp= 1;
716
93951943 717//FIXME: Ensure store_sp is 1 in the first place.
e1249ba9
MN
718
719 if(store_sp){
52afa376 720 Syncpoint *sp, dummy= {.pos= INT64_MAX};
7c04134f 721
e1249ba9 722 ff_nut_reset_ts(nut, *nus->time_base, pkt->dts);
7c04134f
MN
723 for(i=0; i<s->nb_streams; i++){
724 AVStream *st= s->streams[i];
4ddcc983
OS
725 int64_t dts_tb = av_rescale_rnd(pkt->dts,
726 nus->time_base->num * (int64_t)nut->stream[i].time_base->den,
727 nus->time_base->den * (int64_t)nut->stream[i].time_base->num,
728 AV_ROUND_DOWN);
729 int index= av_index_search_timestamp(st, dts_tb, AVSEEK_FLAG_BACKWARD);
175e23e8 730 if(index>=0) dummy.pos= FFMIN(dummy.pos, st->index_entries[index].pos);
7c04134f 731 }
175e23e8
MN
732 if(dummy.pos == INT64_MAX)
733 dummy.pos= 0;
f990f6e3
VS
734 sp= av_tree_find(nut->syncpoints, &dummy, (void *) ff_nut_sp_pos_cmp,
735 NULL);
e1249ba9 736
a2704c97 737 nut->last_syncpoint_pos= avio_tell(bc);
b92c5452 738 ret = avio_open_dyn_buf(&dyn_bc);
899681cd
BA
739 if(ret < 0)
740 return ret;
073f8b10 741 put_tt(nut, nus->time_base, dyn_bc, pkt->dts);
f8280ff4 742 ff_put_v(dyn_bc, sp ? (nut->last_syncpoint_pos - sp->pos)>>4 : 0);
899681cd 743 put_packet(nut, bc, dyn_bc, 1, SYNCPOINT_STARTCODE);
7c04134f
MN
744
745 ff_nut_add_sp(nut, nut->last_syncpoint_pos, 0/*unused*/, pkt->dts);
e1249ba9
MN
746 }
747 assert(nus->last_pts != AV_NOPTS_VALUE);
748
749 coded_pts = pkt->pts & ((1<<nus->msb_pts_shift)-1);
750 if(ff_lsb2full(nus, coded_pts) != pkt->pts)
751 coded_pts= pkt->pts + (1<<nus->msb_pts_shift);
752
a712d725
MN
753 best_header_idx= find_best_header_idx(nut, pkt);
754
e1249ba9
MN
755 best_length=INT_MAX;
756 frame_code= -1;
757 for(i=0; i<256; i++){
758 int length= 0;
759 FrameCode *fc= &nut->frame_code[i];
760 int flags= fc->flags;
761
762 if(flags & FLAG_INVALID)
763 continue;
764 needed_flags= get_needed_flags(nut, nus, fc, pkt);
765
766 if(flags & FLAG_CODED){
767 length++;
63dd9d0e 768 flags = needed_flags;
e1249ba9
MN
769 }
770
771 if((flags & needed_flags) != needed_flags)
772 continue;
773
774 if((flags ^ needed_flags) & FLAG_KEY)
775 continue;
776
777 if(flags & FLAG_STREAM_ID)
f8280ff4 778 length+= ff_get_v_length(pkt->stream_index);
e1249ba9
MN
779
780 if(pkt->size % fc->size_mul != fc->size_lsb)
781 continue;
782 if(flags & FLAG_SIZE_MSB)
f8280ff4 783 length += ff_get_v_length(pkt->size / fc->size_mul);
e1249ba9
MN
784
785 if(flags & FLAG_CHECKSUM)
786 length+=4;
787
788 if(flags & FLAG_CODED_PTS)
f8280ff4 789 length += ff_get_v_length(coded_pts);
e1249ba9 790
a712d725
MN
791 if( (flags & FLAG_CODED)
792 && nut->header_len[best_header_idx] > nut->header_len[fc->header_idx]+1){
793 flags |= FLAG_HEADER_IDX;
794 }
795
796 if(flags & FLAG_HEADER_IDX){
797 length += 1 - nut->header_len[best_header_idx];
798 }else{
799 length -= nut->header_len[fc->header_idx];
800 }
801
e1249ba9
MN
802 length*=4;
803 length+= !(flags & FLAG_CODED_PTS);
804 length+= !(flags & FLAG_CHECKSUM);
805
806 if(length < best_length){
807 best_length= length;
808 frame_code=i;
809 }
810 }
811 assert(frame_code != -1);
812 fc= &nut->frame_code[frame_code];
813 flags= fc->flags;
814 needed_flags= get_needed_flags(nut, nus, fc, pkt);
a712d725 815 header_idx= fc->header_idx;
e1249ba9 816
4c4427a7 817 ffio_init_checksum(bc, ff_crc04C11DB7_update, 0);
77eb5504 818 avio_w8(bc, frame_code);
e1249ba9 819 if(flags & FLAG_CODED){
f8280ff4 820 ff_put_v(bc, (flags^needed_flags) & ~(FLAG_CODED));
e1249ba9
MN
821 flags = needed_flags;
822 }
f8280ff4
MC
823 if(flags & FLAG_STREAM_ID) ff_put_v(bc, pkt->stream_index);
824 if(flags & FLAG_CODED_PTS) ff_put_v(bc, coded_pts);
825 if(flags & FLAG_SIZE_MSB) ff_put_v(bc, pkt->size / fc->size_mul);
826 if(flags & FLAG_HEADER_IDX) ff_put_v(bc, header_idx= best_header_idx);
e1249ba9 827
b64030f2
AK
828 if(flags & FLAG_CHECKSUM) avio_wl32(bc, ffio_get_checksum(bc));
829 else ffio_get_checksum(bc);
e1249ba9 830
77eb5504 831 avio_write(bc, pkt->data + nut->header_len[header_idx], pkt->size - nut->header_len[header_idx]);
e1249ba9 832 nus->last_flags= flags;
e942293d 833 nus->last_pts= pkt->pts;
7c04134f
MN
834
835 //FIXME just store one per syncpoint
836 if(flags & FLAG_KEY)
837 av_add_index_entry(
838 s->streams[pkt->stream_index],
839 nut->last_syncpoint_pos,
840 pkt->pts,
841 0,
842 0,
843 AVINDEX_KEYFRAME);
844
70ea1e69
MN
845 return 0;
846}
847
5ee5dc4e 848static int nut_write_trailer(AVFormatContext *s){
bd533e6c 849 NUTContext *nut= s->priv_data;
ae628ec1 850 AVIOContext *bc= s->pb;
bd533e6c 851
14b3f996 852 while(nut->header_count<3)
da9e6c42 853 write_headers(s, bc);
3b4bb19e 854
4b83fc0f 855 ff_nut_free_sp(nut);
27dbc47c 856 av_freep(&nut->stream);
2cface71 857 av_freep(&nut->chapter);
27dbc47c 858 av_freep(&nut->time_base);
bd533e6c
MN
859
860 return 0;
861}
862
c6610a21 863AVOutputFormat ff_nut_muxer = {
dfc2c4d9 864 .name = "nut",
6774247a 865 .long_name = NULL_IF_CONFIG_SMALL("NUT"),
dfc2c4d9
AK
866 .mime_type = "video/x-nut",
867 .extensions = "nut",
868 .priv_data_size = sizeof(NUTContext),
36ef5369
AK
869 .audio_codec = CONFIG_LIBVORBIS ? AV_CODEC_ID_VORBIS :
870 CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_MP2,
871 .video_codec = AV_CODEC_ID_MPEG4,
5ee5dc4e
LB
872 .write_header = nut_write_header,
873 .write_packet = nut_write_packet,
874 .write_trailer = nut_write_trailer,
20234a4b
MS
875 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
876 .codec_tag = (const AVCodecTag * const []){
877 ff_codec_bmp_tags, ff_nut_video_tags, ff_codec_wav_tags,
878 ff_nut_subtitle_tags, 0
879 },
70ea1e69 880};