move syncpoint cache related stuff to common file
[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
22#include "nut.h"
23
24#define TRACE
25
26static void build_frame_code(AVFormatContext *s){
27 NUTContext *nut = s->priv_data;
28 int key_frame, index, pred, stream_id;
29 int start=1;
30 int end= 254;
31 int keyframe_0_esc= s->nb_streams > 2;
32 int pred_table[10];
0fcfa8eb
MN
33 FrameCode *ft;
34
35 ft= &nut->frame_code[start];
36 ft->flags= FLAG_CODED;
37 ft->size_mul=1;
38 ft->pts_delta=1;
39 start++;
70ea1e69
MN
40
41 if(keyframe_0_esc){
42 /* keyframe = 0 escape */
43 FrameCode *ft= &nut->frame_code[start];
44 ft->flags= FLAG_STREAM_ID | FLAG_SIZE_MSB | FLAG_CODED_PTS;
45 ft->size_mul=1;
46 start++;
47 }
48
49 for(stream_id= 0; stream_id<s->nb_streams; stream_id++){
50 int start2= start + (end-start)*stream_id / s->nb_streams;
51 int end2 = start + (end-start)*(stream_id+1) / s->nb_streams;
52 AVCodecContext *codec = s->streams[stream_id]->codec;
53 int is_audio= codec->codec_type == CODEC_TYPE_AUDIO;
54 int intra_only= /*codec->intra_only || */is_audio;
55 int pred_count;
56
57 for(key_frame=0; key_frame<2; key_frame++){
58 if(intra_only && keyframe_0_esc && key_frame==0)
59 continue;
60
61 {
62 FrameCode *ft= &nut->frame_code[start2];
63 ft->flags= FLAG_KEY*key_frame;
64 ft->flags|= FLAG_SIZE_MSB | FLAG_CODED_PTS;
65 ft->stream_id= stream_id;
66 ft->size_mul=1;
67 start2++;
68 }
69 }
70
71 key_frame= intra_only;
72#if 1
73 if(is_audio){
74 int frame_bytes= codec->frame_size*(int64_t)codec->bit_rate / (8*codec->sample_rate);
75 int pts;
76 for(pts=0; pts<2; pts++){
77 for(pred=0; pred<2; pred++){
78 FrameCode *ft= &nut->frame_code[start2];
79 ft->flags= FLAG_KEY*key_frame;
80 ft->stream_id= stream_id;
81 ft->size_mul=frame_bytes + 2;
82 ft->size_lsb=frame_bytes + pred;
83 ft->pts_delta=pts;
84 start2++;
85 }
86 }
87 }else{
88 FrameCode *ft= &nut->frame_code[start2];
89 ft->flags= FLAG_KEY | FLAG_SIZE_MSB;
90 ft->stream_id= stream_id;
91 ft->size_mul=1;
92 ft->pts_delta=1;
93 start2++;
94 }
95#endif
96
97 if(codec->has_b_frames){
98 pred_count=5;
99 pred_table[0]=-2;
100 pred_table[1]=-1;
101 pred_table[2]=1;
102 pred_table[3]=3;
103 pred_table[4]=4;
104 }else if(codec->codec_id == CODEC_ID_VORBIS){
105 pred_count=3;
106 pred_table[0]=2;
107 pred_table[1]=9;
108 pred_table[2]=16;
109 }else{
110 pred_count=1;
111 pred_table[0]=1;
112 }
113
114 for(pred=0; pred<pred_count; pred++){
115 int start3= start2 + (end2-start2)*pred / pred_count;
116 int end3 = start2 + (end2-start2)*(pred+1) / pred_count;
117
118 for(index=start3; index<end3; index++){
119 FrameCode *ft= &nut->frame_code[index];
120 ft->flags= FLAG_KEY*key_frame;
121 ft->flags|= FLAG_SIZE_MSB;
122 ft->stream_id= stream_id;
123//FIXME use single byte size and pred from last
124 ft->size_mul= end3-start3;
125 ft->size_lsb= index - start3;
126 ft->pts_delta= pred_table[pred];
127 }
128 }
129 }
130 memmove(&nut->frame_code['N'+1], &nut->frame_code['N'], sizeof(FrameCode)*(255-'N'));
131 nut->frame_code[ 0].flags=
132 nut->frame_code[255].flags=
133 nut->frame_code['N'].flags= FLAG_INVALID;
134}
135
136/**
137 * Gets the length in bytes which is needed to store val as v.
138 */
139static int get_length(uint64_t val){
140 int i=1;
141
142 while(val>>=7)
143 i++;
144
145 return i;
146}
147
148static void put_v(ByteIOContext *bc, uint64_t val){
149 int i= get_length(val);
150
151 while(--i>0)
152 put_byte(bc, 128 | (val>>(7*i)));
153
154 put_byte(bc, val&127);
155}
156
491216c6
MN
157static void put_t(NUTContext *nut, StreamContext *nus, ByteIOContext *bc, uint64_t val){
158 val *= nut->time_base_count;
159 val += nus->time_base - nut->time_base;
160 put_v(bc, val);
161}
162
70ea1e69
MN
163/**
164 * stores a string as vb.
165 */
166static void put_str(ByteIOContext *bc, const char *string){
167 int len= strlen(string);
168
169 put_v(bc, len);
170 put_buffer(bc, string, len);
171}
172
173static void put_s(ByteIOContext *bc, int64_t val){
174 put_v(bc, 2*FFABS(val) - (val>0));
175}
176
177#ifdef TRACE
178static inline void put_v_trace(ByteIOContext *bc, uint64_t v, char *file, char *func, int line){
7a26da44 179 av_log(NULL, AV_LOG_DEBUG, "put_v %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
70ea1e69
MN
180
181 put_v(bc, v);
182}
183
184static inline void put_s_trace(ByteIOContext *bc, int64_t v, char *file, char *func, int line){
7a26da44 185 av_log(NULL, AV_LOG_DEBUG, "put_s %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
70ea1e69
MN
186
187 put_s(bc, v);
188}
189#define put_v(bc, v) put_v_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
190#define put_s(bc, v) put_s_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
191#endif
192
34f8cc38 193//FIXME remove calculate_checksum
f0a9978c 194static void put_packet(NUTContext *nut, ByteIOContext *bc, ByteIOContext *dyn_bc, int calculate_checksum, uint64_t startcode){
8d9a3f7e
MN
195 uint8_t *dyn_buf=NULL;
196 int dyn_size= url_close_dyn_buf(dyn_bc, &dyn_buf);
6c666941
MN
197 int forw_ptr= dyn_size + 4*calculate_checksum;
198
199 if(forw_ptr > 4096)
f0a9978c
MN
200 init_checksum(bc, av_crc04C11DB7_update, 0);
201 put_be64(bc, startcode);
6c666941
MN
202 put_v(bc, forw_ptr);
203 if(forw_ptr > 4096)
204 put_le32(bc, get_checksum(bc));
70ea1e69
MN
205
206 if(calculate_checksum)
207 init_checksum(bc, av_crc04C11DB7_update, 0);
8d9a3f7e 208 put_buffer(bc, dyn_buf, dyn_size);
70ea1e69 209 if(calculate_checksum)
8d9a3f7e 210 put_le32(bc, get_checksum(bc));
70ea1e69 211
8d9a3f7e 212 av_free(dyn_buf);
70ea1e69
MN
213}
214
97d5eb11 215static void write_mainheader(NUTContext *nut, ByteIOContext *bc){
70ea1e69
MN
216 int i, j, tmp_pts, tmp_flags, tmp_stream, tmp_mul, tmp_size, tmp_fields;
217
70ea1e69 218 put_v(bc, 2); /* version */
97d5eb11 219 put_v(bc, nut->avf->nb_streams);
70ea1e69
MN
220 put_v(bc, MAX_DISTANCE);
221 put_v(bc, nut->time_base_count);
222
223 for(i=0; i<nut->time_base_count; i++){
224 put_v(bc, nut->time_base[i].num);
225 put_v(bc, nut->time_base[i].den);
226 }
227
70ea1e69
MN
228 tmp_pts=0;
229 tmp_mul=1;
230 tmp_stream=0;
231 for(i=0; i<256;){
232 tmp_fields=0;
233 tmp_size=0;
234// tmp_res=0;
235 if(tmp_pts != nut->frame_code[i].pts_delta) tmp_fields=1;
236 if(tmp_mul != nut->frame_code[i].size_mul ) tmp_fields=2;
237 if(tmp_stream != nut->frame_code[i].stream_id) tmp_fields=3;
238 if(tmp_size != nut->frame_code[i].size_lsb ) tmp_fields=4;
239// if(tmp_res != nut->frame_code[i].res ) tmp_fields=5;
240
241 tmp_pts = nut->frame_code[i].pts_delta;
242 tmp_flags = nut->frame_code[i].flags;
243 tmp_stream= nut->frame_code[i].stream_id;
244 tmp_mul = nut->frame_code[i].size_mul;
245 tmp_size = nut->frame_code[i].size_lsb;
246// tmp_res = nut->frame_code[i].res;
247
248 for(j=0; i<256; j++,i++){
249 if(i == 'N'){
250 j--;
251 continue;
252 }
253 if(nut->frame_code[i].pts_delta != tmp_pts ) break;
254 if(nut->frame_code[i].flags != tmp_flags ) break;
255 if(nut->frame_code[i].stream_id != tmp_stream) break;
256 if(nut->frame_code[i].size_mul != tmp_mul ) break;
257 if(nut->frame_code[i].size_lsb != tmp_size+j) break;
258// if(nut->frame_code[i].res != tmp_res ) break;
259 }
260 if(j != tmp_mul - tmp_size) tmp_fields=6;
261
262 put_v(bc, tmp_flags);
263 put_v(bc, tmp_fields);
264 if(tmp_fields>0) put_s(bc, tmp_pts);
265 if(tmp_fields>1) put_v(bc, tmp_mul);
266 if(tmp_fields>2) put_v(bc, tmp_stream);
267 if(tmp_fields>3) put_v(bc, tmp_size);
268 if(tmp_fields>4) put_v(bc, 0 /*tmp_res*/);
269 if(tmp_fields>5) put_v(bc, j);
270 }
97d5eb11 271}
70ea1e69 272
0809b51e
MN
273static int write_streamheader(NUTContext *nut, ByteIOContext *bc, AVCodecContext *codec, int i){
274 put_v(bc, i);
275 switch(codec->codec_type){
276 case CODEC_TYPE_VIDEO: put_v(bc, 0); break;
277 case CODEC_TYPE_AUDIO: put_v(bc, 1); break;
278// case CODEC_TYPE_TEXT : put_v(bc, 2); break;
279 default : put_v(bc, 3); break;
280 }
281 put_v(bc, 4);
282 if (codec->codec_tag){
283 put_le32(bc, codec->codec_tag);
284 }else
285 return -1;
286
6e6cf116 287 put_v(bc, nut->stream[i].time_base - nut->time_base);
0809b51e
MN
288 put_v(bc, nut->stream[i].msb_pts_shift);
289 put_v(bc, nut->stream[i].max_pts_distance);
290 put_v(bc, codec->has_b_frames);
291 put_byte(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */
292
293 put_v(bc, codec->extradata_size);
294 put_buffer(bc, codec->extradata, codec->extradata_size);
295
296 switch(codec->codec_type){
297 case CODEC_TYPE_AUDIO:
298 put_v(bc, codec->sample_rate);
299 put_v(bc, 1);
300 put_v(bc, codec->channels);
301 break;
302 case CODEC_TYPE_VIDEO:
303 put_v(bc, codec->width);
304 put_v(bc, codec->height);
305 put_v(bc, codec->sample_aspect_ratio.num);
306 put_v(bc, codec->sample_aspect_ratio.den);
307 put_v(bc, 0); /* csp type -- unknown */
308 break;
309 default:
310 break;
311 }
312 return 0;
313}
314
7e506494
MN
315static void write_headers(NUTContext *nut, ByteIOContext *bc){
316 ByteIOContext dyn_bc;
317 int i;
318
7e506494
MN
319 url_open_dyn_buf(&dyn_bc);
320 write_mainheader(nut, &dyn_bc);
f0a9978c 321 put_packet(nut, bc, &dyn_bc, 1, MAIN_STARTCODE);
7e506494
MN
322
323 for (i=0; i < nut->avf->nb_streams; i++){
324 AVCodecContext *codec = nut->avf->streams[i]->codec;
325
7e506494
MN
326 url_open_dyn_buf(&dyn_bc);
327 write_streamheader(nut, &dyn_bc, codec, i);
f0a9978c 328 put_packet(nut, bc, &dyn_bc, 1, STREAM_STARTCODE);
7e506494
MN
329 }
330}
331
97d5eb11
MN
332static int write_header(AVFormatContext *s){
333 NUTContext *nut = s->priv_data;
7e506494 334 ByteIOContext *bc = &s->pb;
97d5eb11
MN
335 int i, j;
336
337 nut->avf= s;
338
339 nut->stream = av_mallocz(sizeof(StreamContext)*s->nb_streams);
340 nut->time_base= av_mallocz(sizeof(AVRational )*s->nb_streams);
341
342 for(i=0; i<s->nb_streams; i++){
343 AVStream *st= s->streams[i];
344 int ssize;
345 AVRational time_base;
346 ff_parse_specific_params(st->codec, &time_base.den, &ssize, &time_base.num);
347
348 av_set_pts_info(st, 64, time_base.num, time_base.den);
349
350 for(j=0; j<nut->time_base_count; j++){
351 if(!memcmp(&time_base, &nut->time_base[j], sizeof(AVRational))){
352 break;
353 }
354 }
355 nut->time_base[j]= time_base;
356 nut->stream[i].time_base= &nut->time_base[j];
357 if(j==nut->time_base_count)
358 nut->time_base_count++;
359
360 if(av_q2d(time_base) >= 0.001)
361 nut->stream[i].msb_pts_shift = 7;
362 else
363 nut->stream[i].msb_pts_shift = 14;
364 nut->stream[i].max_pts_distance= FFMAX(1/av_q2d(time_base), 1);
365 }
366
367 build_frame_code(s);
368 assert(nut->frame_code['N'].flags == FLAG_INVALID);
369
370 put_buffer(bc, ID_STRING, strlen(ID_STRING));
371 put_byte(bc, 0);
372
7e506494 373 write_headers(nut, bc);
dcc9bde0 374
70ea1e69
MN
375 put_flush_packet(bc);
376
6c666941 377 //FIXME info header, header repeation, ...
70ea1e69
MN
378
379 return 0;
380}
381
e1249ba9
MN
382static int get_needed_flags(NUTContext *nut, StreamContext *nus, FrameCode *fc, AVPacket *pkt){
383 int flags= 0;
384
385 if(pkt->flags & PKT_FLAG_KEY ) flags |= FLAG_KEY;
386 if(pkt->stream_index != fc->stream_id ) flags |= FLAG_STREAM_ID;
387 if(pkt->size / fc->size_mul ) flags |= FLAG_SIZE_MSB;
388 if(pkt->pts - nus->last_pts != fc->pts_delta) flags |= FLAG_CODED_PTS;
389 if(pkt->size > 2*nut->max_distance ) flags |= FLAG_CHECKSUM;
390 if(FFABS(pkt->pts - nus->last_pts)
391 > nus->max_pts_distance) flags |= FLAG_CHECKSUM;
392
393 return flags;
394}
395
70ea1e69 396static int write_packet(AVFormatContext *s, AVPacket *pkt){
e1249ba9
MN
397 NUTContext *nut = s->priv_data;
398 StreamContext *nus= &nut->stream[pkt->stream_index];
399 ByteIOContext *bc = &s->pb, dyn_bc;
400 FrameCode *fc;
401 int64_t coded_pts;
402 int best_length, frame_code, flags, needed_flags, i;
403 int key_frame = !!(pkt->flags & PKT_FLAG_KEY);
404 int store_sp=0;
405
406 if(key_frame && !!(nus->last_flags & FLAG_KEY))
407 store_sp= 1;
408
409 if(pkt->size + 30/*FIXME check*/ + url_ftell(bc) >= nut->last_syncpoint_pos + nut->max_distance)
410 store_sp= 1;
411
412//FIXME ensure store_sp is 1 for the first thing
413
414 if(store_sp){
415 ff_nut_reset_ts(nut, *nus->time_base, pkt->dts);
416
417 nut->last_syncpoint_pos= url_ftell(bc);
e1249ba9
MN
418 url_open_dyn_buf(&dyn_bc);
419 put_t(nut, nus, &dyn_bc, pkt->dts);
420 put_v(&dyn_bc, 0); //FIXME back_ptr_div16
f0a9978c 421 put_packet(nut, bc, &dyn_bc, 1, SYNCPOINT_STARTCODE);
e1249ba9
MN
422 }
423 assert(nus->last_pts != AV_NOPTS_VALUE);
424
425 coded_pts = pkt->pts & ((1<<nus->msb_pts_shift)-1);
426 if(ff_lsb2full(nus, coded_pts) != pkt->pts)
427 coded_pts= pkt->pts + (1<<nus->msb_pts_shift);
428
429 best_length=INT_MAX;
430 frame_code= -1;
431 for(i=0; i<256; i++){
432 int length= 0;
433 FrameCode *fc= &nut->frame_code[i];
434 int flags= fc->flags;
435
436 if(flags & FLAG_INVALID)
437 continue;
438 needed_flags= get_needed_flags(nut, nus, fc, pkt);
439
440 if(flags & FLAG_CODED){
441 length++;
442 flags &= ~needed_flags;
443 flags |= needed_flags;
444 }
445
446 if((flags & needed_flags) != needed_flags)
447 continue;
448
449 if((flags ^ needed_flags) & FLAG_KEY)
450 continue;
451
452 if(flags & FLAG_STREAM_ID)
453 length+= get_length(pkt->stream_index);
454
455 if(pkt->size % fc->size_mul != fc->size_lsb)
456 continue;
457 if(flags & FLAG_SIZE_MSB)
458 length += get_length(pkt->size / fc->size_mul);
459
460 if(flags & FLAG_CHECKSUM)
461 length+=4;
462
463 if(flags & FLAG_CODED_PTS)
464 length += get_length(coded_pts);
465
466 length*=4;
467 length+= !(flags & FLAG_CODED_PTS);
468 length+= !(flags & FLAG_CHECKSUM);
469
470 if(length < best_length){
471 best_length= length;
472 frame_code=i;
473 }
474 }
475 assert(frame_code != -1);
476 fc= &nut->frame_code[frame_code];
477 flags= fc->flags;
478 needed_flags= get_needed_flags(nut, nus, fc, pkt);
479
480 init_checksum(bc, av_crc04C11DB7_update, 0);
481 put_byte(bc, frame_code);
482 if(flags & FLAG_CODED){
483 put_v(bc, (flags^needed_flags) & ~(FLAG_CODED));
484 flags = needed_flags;
485 }
486 if(flags & FLAG_STREAM_ID) put_v(bc, pkt->stream_index);
487 if(flags & FLAG_CODED_PTS) put_v(bc, coded_pts);
488 if(flags & FLAG_SIZE_MSB) put_v(bc, pkt->size / fc->size_mul);
489
490 if(flags & FLAG_CHECKSUM) put_le32(bc, get_checksum(bc));
491 else get_checksum(bc);
492
493 put_buffer(bc, pkt->data, pkt->size);
494 nus->last_flags= flags;
70ea1e69
MN
495 return 0;
496}
497
bd533e6c
MN
498static int write_trailer(AVFormatContext *s){
499 NUTContext *nut= s->priv_data;
500 ByteIOContext *bc= &s->pb;
501
502 write_headers(nut, bc);
503 put_flush_packet(bc);
504
505 return 0;
506}
507
70ea1e69
MN
508AVOutputFormat nut_muxer = {
509 "nut",
510 "nut format",
511 "video/x-nut",
512 "nut",
513 sizeof(NUTContext),
514#ifdef CONFIG_LIBVORBIS
515 CODEC_ID_VORBIS,
516#elif defined(CONFIG_LIBMP3LAME)
517 CODEC_ID_MP3,
518#else
519 CODEC_ID_MP2, /* AC3 needs liba52 decoder */
520#endif
521 CODEC_ID_MPEG4,
522 write_header,
523 write_packet,
bd533e6c 524 write_trailer,
70ea1e69 525 .flags = AVFMT_GLOBALHEADER,
dcc9bde0 526 .codec_tag= (const AVCodecTag*[]){codec_bmp_tags, codec_wav_tags, 0},
70ea1e69 527};