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