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