3 * Copyright (c) 2004-2007 Michael Niedermayer
5 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/mathematics.h"
26 #include "libavutil/tree.h"
27 #include "libavutil/dict.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/time.h"
30 #include "libavutil/opt.h"
31 #include "libavcodec/bytestream.h"
32 #include "libavcodec/mpegaudiodata.h"
35 #include "avio_internal.h"
38 static int find_expected_header(AVCodecContext *c, int size, int key_frame,
41 int sample_rate = c->sample_rate;
48 if (c->codec_id == AV_CODEC_ID_MPEG4) {
55 } else if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
56 c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
58 } else if (c->codec_id == AV_CODEC_ID_H264) {
60 } else if (c->codec_id == AV_CODEC_ID_MP3 ||
61 c->codec_id == AV_CODEC_ID_MP2) {
62 int lsf, mpeg25, sample_rate_index, bitrate_index, frame_size;
63 int layer = c->codec_id == AV_CODEC_ID_MP3 ? 3 : 2;
64 unsigned int header = 0xFFF00000;
66 lsf = sample_rate < (24000 + 32000) / 2;
67 mpeg25 = sample_rate < (12000 + 16000) / 2;
68 sample_rate <<= lsf + mpeg25;
69 if (sample_rate < (32000 + 44100) / 2) sample_rate_index = 2;
70 else if (sample_rate < (44100 + 48000) / 2) sample_rate_index = 0;
71 else sample_rate_index = 1;
73 sample_rate = avpriv_mpa_freq_tab[sample_rate_index] >> (lsf + mpeg25);
75 for (bitrate_index = 2; bitrate_index < 30; bitrate_index++) {
77 avpriv_mpa_bitrate_tab[lsf][layer - 1][bitrate_index >> 1];
78 frame_size = (frame_size * 144000) / (sample_rate << lsf) +
81 if (frame_size == size)
85 header |= (!lsf) << 19;
86 header |= (4 - layer) << 17;
87 header |= 1 << 16; //no crc
90 return 2; //we guess there is no crc, if there is one the user clearly does not care about overhead
91 if (bitrate_index == 30)
92 return -1; //something is wrong ...
94 header |= (bitrate_index >> 1) << 12;
95 header |= sample_rate_index << 10;
96 header |= (bitrate_index & 1) << 9;
98 return 2; //FIXME actually put the needed ones in build_elision_headers()
99 //return 3; //we guess that the private bit is not set
100 //FIXME the above assumptions should be checked, if these turn out false too often something should be done
105 static int find_header_idx(AVFormatContext *s, AVCodecContext *c, int size, int frame_type)
107 NUTContext *nut = s->priv_data;
110 int len = find_expected_header(c, size, frame_type, out);
112 for (i = 1; i < nut->header_count; i++) {
113 if (len == nut->header_len[i] && !memcmp(out, nut->header[i], len)) {
121 static void build_elision_headers(AVFormatContext *s)
123 NUTContext *nut = s->priv_data;
126 //FIXME write a 2pass mode to find the maximal headers
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
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];
143 static void build_frame_code(AVFormatContext *s)
145 NUTContext *nut = s->priv_data;
146 int key_frame, index, pred, stream_id;
149 int keyframe_0_esc = s->nb_streams > 2;
153 ft = &nut->frame_code[start];
154 ft->flags = FLAG_CODED;
159 if (keyframe_0_esc) {
160 /* keyframe = 0 escape */
161 FrameCode *ft = &nut->frame_code[start];
162 ft->flags = FLAG_STREAM_ID | FLAG_SIZE_MSB | FLAG_CODED_PTS;
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;
170 AVCodecContext *codec = s->streams[stream_id]->codec;
171 int is_audio = codec->codec_type == AVMEDIA_TYPE_AUDIO;
172 int intra_only = /*codec->intra_only || */ is_audio;
176 if (codec->codec_type == AVMEDIA_TYPE_AUDIO) {
177 frame_size = av_get_audio_frame_duration(codec, 0);
178 if (codec->codec_id == AV_CODEC_ID_VORBIS && !frame_size)
181 AVRational f = av_div_q(codec->time_base, *nut->stream[stream_id].time_base);
182 if (f.den == 1 && f.num>0)
188 for (key_frame = 0; key_frame < 2; key_frame++) {
189 if (!intra_only || !keyframe_0_esc || key_frame != 0) {
190 FrameCode *ft = &nut->frame_code[start2];
191 ft->flags = FLAG_KEY * key_frame;
192 ft->flags |= FLAG_SIZE_MSB | FLAG_CODED_PTS;
193 ft->stream_id = stream_id;
196 ft->header_idx = find_header_idx(s, codec, -1, key_frame);
201 key_frame = intra_only;
204 int frame_bytes = codec->frame_size * (int64_t)codec->bit_rate /
205 (8 * codec->sample_rate);
207 for (pts = 0; pts < 2; pts++) {
208 for (pred = 0; pred < 2; pred++) {
209 FrameCode *ft = &nut->frame_code[start2];
210 ft->flags = FLAG_KEY * key_frame;
211 ft->stream_id = stream_id;
212 ft->size_mul = frame_bytes + 2;
213 ft->size_lsb = frame_bytes + pred;
214 ft->pts_delta = pts * frame_size;
215 ft->header_idx = find_header_idx(s, codec, frame_bytes + pred, key_frame);
220 FrameCode *ft = &nut->frame_code[start2];
221 ft->flags = FLAG_KEY | FLAG_SIZE_MSB;
222 ft->stream_id = stream_id;
224 ft->pts_delta = frame_size;
229 if (codec->has_b_frames) {
236 } else if (codec->codec_id == AV_CODEC_ID_VORBIS) {
246 for (pred = 0; pred < pred_count; pred++) {
247 int start3 = start2 + (end2 - start2) * pred / pred_count;
248 int end3 = start2 + (end2 - start2) * (pred + 1) / pred_count;
250 pred_table[pred] *= frame_size;
252 for (index = start3; index < end3; index++) {
253 FrameCode *ft = &nut->frame_code[index];
254 ft->flags = FLAG_KEY * key_frame;
255 ft->flags |= FLAG_SIZE_MSB;
256 ft->stream_id = stream_id;
257 //FIXME use single byte size and pred from last
258 ft->size_mul = end3 - start3;
259 ft->size_lsb = index - start3;
260 ft->pts_delta = pred_table[pred];
262 ft->header_idx = find_header_idx(s, codec, -1, key_frame);
266 memmove(&nut->frame_code['N' + 1], &nut->frame_code['N'], sizeof(FrameCode) * (255 - 'N'));
267 nut->frame_code[0].flags =
268 nut->frame_code[255].flags =
269 nut->frame_code['N'].flags = FLAG_INVALID;
272 static void put_tt(NUTContext *nut, AVRational *time_base, AVIOContext *bc, uint64_t val)
274 val *= nut->time_base_count;
275 val += time_base - nut->time_base;
279 * Store a string as vb.
281 static void put_str(AVIOContext *bc, const char *string)
283 size_t len = strlen(string);
286 avio_write(bc, string, len);
289 static void put_s(AVIOContext *bc, int64_t val)
291 ff_put_v(bc, 2 * FFABS(val) - (val > 0));
295 static inline void ff_put_v_trace(AVIOContext *bc, uint64_t v, const char *file,
296 const char *func, int line)
298 av_log(NULL, AV_LOG_DEBUG, "ff_put_v %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
303 static inline void put_s_trace(AVIOContext *bc, int64_t v, const char *file, const char *func, int line)
305 av_log(NULL, AV_LOG_DEBUG, "put_s %5"PRId64" / %"PRIX64" in %s %s:%d\n", v, v, file, func, line);
309 #define ff_put_v(bc, v) ff_put_v_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
310 #define put_s(bc, v) put_s_trace(bc, v, __FILE__, __PRETTY_FUNCTION__, __LINE__)
313 //FIXME remove calculate_checksum
314 static void put_packet(NUTContext *nut, AVIOContext *bc, AVIOContext *dyn_bc,
315 int calculate_checksum, uint64_t startcode)
317 uint8_t *dyn_buf = NULL;
318 int dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
319 int forw_ptr = dyn_size + 4 * calculate_checksum;
322 ffio_init_checksum(bc, ff_crc04C11DB7_update, 0);
323 avio_wb64(bc, startcode);
324 ff_put_v(bc, forw_ptr);
326 avio_wl32(bc, ffio_get_checksum(bc));
328 if (calculate_checksum)
329 ffio_init_checksum(bc, ff_crc04C11DB7_update, 0);
330 avio_write(bc, dyn_buf, dyn_size);
331 if (calculate_checksum)
332 avio_wl32(bc, ffio_get_checksum(bc));
337 static void write_mainheader(NUTContext *nut, AVIOContext *bc)
339 int i, j, tmp_pts, tmp_flags, tmp_stream, tmp_mul, tmp_size, tmp_fields,
343 ff_put_v(bc, nut->version);
344 if (nut->version > 3)
345 ff_put_v(bc, nut->minor_version = 1);
346 ff_put_v(bc, nut->avf->nb_streams);
347 ff_put_v(bc, nut->max_distance);
348 ff_put_v(bc, nut->time_base_count);
350 for (i = 0; i < nut->time_base_count; i++) {
351 ff_put_v(bc, nut->time_base[i].num);
352 ff_put_v(bc, nut->time_base[i].den);
358 tmp_match = 1 - (1LL << 62);
360 for (i = 0; i < 256; ) {
364 if (tmp_pts != nut->frame_code[i].pts_delta ) tmp_fields = 1;
365 if (tmp_mul != nut->frame_code[i].size_mul ) tmp_fields = 2;
366 if (tmp_stream != nut->frame_code[i].stream_id ) tmp_fields = 3;
367 if (tmp_size != nut->frame_code[i].size_lsb ) tmp_fields = 4;
368 // if (tmp_res != nut->frame_code[i].res ) tmp_fields=5;
369 if (tmp_head_idx != nut->frame_code[i].header_idx) tmp_fields = 8;
371 tmp_pts = nut->frame_code[i].pts_delta;
372 tmp_flags = nut->frame_code[i].flags;
373 tmp_stream = nut->frame_code[i].stream_id;
374 tmp_mul = nut->frame_code[i].size_mul;
375 tmp_size = nut->frame_code[i].size_lsb;
376 // tmp_res = nut->frame_code[i].res;
377 tmp_head_idx = nut->frame_code[i].header_idx;
379 for (j = 0; i < 256; j++, i++) {
384 if (nut->frame_code[i].pts_delta != tmp_pts ||
385 nut->frame_code[i].flags != tmp_flags ||
386 nut->frame_code[i].stream_id != tmp_stream ||
387 nut->frame_code[i].size_mul != tmp_mul ||
388 nut->frame_code[i].size_lsb != tmp_size + j ||
389 // nut->frame_code[i].res != tmp_res ||
390 nut->frame_code[i].header_idx != tmp_head_idx)
393 if (j != tmp_mul - tmp_size)
396 ff_put_v(bc, tmp_flags);
397 ff_put_v(bc, tmp_fields);
398 if (tmp_fields > 0) put_s(bc, tmp_pts);
399 if (tmp_fields > 1) ff_put_v(bc, tmp_mul);
400 if (tmp_fields > 2) ff_put_v(bc, tmp_stream);
401 if (tmp_fields > 3) ff_put_v(bc, tmp_size);
402 if (tmp_fields > 4) ff_put_v(bc, 0 /*tmp_res*/);
403 if (tmp_fields > 5) ff_put_v(bc, j);
404 if (tmp_fields > 6) ff_put_v(bc, tmp_match);
405 if (tmp_fields > 7) ff_put_v(bc, tmp_head_idx);
407 ff_put_v(bc, nut->header_count - 1);
408 for (i = 1; i < nut->header_count; i++) {
409 ff_put_v(bc, nut->header_len[i]);
410 avio_write(bc, nut->header[i], nut->header_len[i]);
412 // flags had been effectively introduced in version 4
413 if (nut->version > 3)
414 ff_put_v(bc, nut->flags);
417 static int write_streamheader(AVFormatContext *avctx, AVIOContext *bc,
420 NUTContext *nut = avctx->priv_data;
421 AVCodecContext *codec = st->codec;
424 switch (codec->codec_type) {
425 case AVMEDIA_TYPE_VIDEO: ff_put_v(bc, 0); break;
426 case AVMEDIA_TYPE_AUDIO: ff_put_v(bc, 1); break;
427 case AVMEDIA_TYPE_SUBTITLE: ff_put_v(bc, 2); break;
428 default: ff_put_v(bc, 3); break;
431 if (codec->codec_tag) {
432 avio_wl32(bc, codec->codec_tag);
434 av_log(avctx, AV_LOG_ERROR, "No codec tag defined for stream %d\n", i);
435 return AVERROR(EINVAL);
438 ff_put_v(bc, nut->stream[i].time_base - nut->time_base);
439 ff_put_v(bc, nut->stream[i].msb_pts_shift);
440 ff_put_v(bc, nut->stream[i].max_pts_distance);
441 ff_put_v(bc, codec->has_b_frames);
442 avio_w8(bc, 0); /* flags: 0x1 - fixed_fps, 0x2 - index_present */
444 ff_put_v(bc, codec->extradata_size);
445 avio_write(bc, codec->extradata, codec->extradata_size);
447 switch (codec->codec_type) {
448 case AVMEDIA_TYPE_AUDIO:
449 ff_put_v(bc, codec->sample_rate);
451 ff_put_v(bc, codec->channels);
453 case AVMEDIA_TYPE_VIDEO:
454 ff_put_v(bc, codec->width);
455 ff_put_v(bc, codec->height);
457 if (st->sample_aspect_ratio.num <= 0 ||
458 st->sample_aspect_ratio.den <= 0) {
462 ff_put_v(bc, st->sample_aspect_ratio.num);
463 ff_put_v(bc, st->sample_aspect_ratio.den);
465 ff_put_v(bc, 0); /* csp type -- unknown */
473 static int add_info(AVIOContext *bc, const char *type, const char *value)
481 static int write_globalinfo(NUTContext *nut, AVIOContext *bc)
483 AVFormatContext *s = nut->avf;
484 AVDictionaryEntry *t = NULL;
486 uint8_t *dyn_buf = NULL;
487 int count = 0, dyn_size;
488 int ret = avio_open_dyn_buf(&dyn_bc);
492 while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX)))
493 count += add_info(dyn_bc, t->key, t->value);
495 ff_put_v(bc, 0); //stream_if_plus1
496 ff_put_v(bc, 0); //chapter_id
497 ff_put_v(bc, 0); //timestamp_start
498 ff_put_v(bc, 0); //length
502 dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
503 avio_write(bc, dyn_buf, dyn_size);
508 static int write_streaminfo(NUTContext *nut, AVIOContext *bc, int stream_id) {
509 AVFormatContext *s= nut->avf;
510 AVStream* st = s->streams[stream_id];
511 AVDictionaryEntry *t = NULL;
513 uint8_t *dyn_buf=NULL;
514 int count=0, dyn_size, i;
515 int ret = avio_open_dyn_buf(&dyn_bc);
519 while ((t = av_dict_get(st->metadata, "", t, AV_DICT_IGNORE_SUFFIX)))
520 count += add_info(dyn_bc, t->key, t->value);
521 for (i=0; ff_nut_dispositions[i].flag; ++i) {
522 if (st->disposition & ff_nut_dispositions[i].flag)
523 count += add_info(dyn_bc, "Disposition", ff_nut_dispositions[i].str);
525 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
527 if (st->r_frame_rate.num>0 && st->r_frame_rate.den>0)
528 snprintf(buf, sizeof(buf), "%d/%d", st->r_frame_rate.num, st->r_frame_rate.den);
530 snprintf(buf, sizeof(buf), "%d/%d", st->codec->time_base.den, st->codec->time_base.num);
531 count += add_info(dyn_bc, "r_frame_rate", buf);
533 dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
536 ff_put_v(bc, stream_id + 1); //stream_id_plus1
537 ff_put_v(bc, 0); //chapter_id
538 ff_put_v(bc, 0); //timestamp_start
539 ff_put_v(bc, 0); //length
543 avio_write(bc, dyn_buf, dyn_size);
550 static int write_chapter(NUTContext *nut, AVIOContext *bc, int id)
553 uint8_t *dyn_buf = NULL;
554 AVDictionaryEntry *t = NULL;
555 AVChapter *ch = nut->avf->chapters[id];
556 int ret, dyn_size, count = 0;
558 ret = avio_open_dyn_buf(&dyn_bc);
562 ff_put_v(bc, 0); // stream_id_plus1
563 put_s(bc, id + 1); // chapter_id
564 put_tt(nut, nut->chapter[id].time_base, bc, ch->start); // chapter_start
565 ff_put_v(bc, ch->end - ch->start); // chapter_len
567 while ((t = av_dict_get(ch->metadata, "", t, AV_DICT_IGNORE_SUFFIX)))
568 count += add_info(dyn_bc, t->key, t->value);
572 dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
573 avio_write(bc, dyn_buf, dyn_size);
578 static int write_index(NUTContext *nut, AVIOContext *bc) {
580 Syncpoint dummy= { .pos= 0 };
581 Syncpoint *next_node[2] = { NULL };
582 int64_t startpos = avio_tell(bc);
583 int64_t payload_size;
585 put_tt(nut, nut->max_pts_tb, bc, nut->max_pts);
587 ff_put_v(bc, nut->sp_count);
589 for (i=0; i<nut->sp_count; i++) {
590 av_tree_find(nut->syncpoints, &dummy, (void *) ff_nut_sp_pos_cmp, (void**)next_node);
591 ff_put_v(bc, (next_node[1]->pos >> 4) - (dummy.pos>>4));
592 dummy.pos = next_node[1]->pos;
595 for (i=0; i<nut->avf->nb_streams; i++) {
596 StreamContext *nus= &nut->stream[i];
597 int64_t last_pts= -1;
599 for (j=0; j<nut->sp_count; j++) {
603 if (j && nus->keyframe_pts[j] == nus->keyframe_pts[j-1]) {
604 av_log(nut->avf, AV_LOG_WARNING, "Multiple keyframes with same PTS\n");
605 nus->keyframe_pts[j] = AV_NOPTS_VALUE;
608 flag = (nus->keyframe_pts[j] != AV_NOPTS_VALUE) ^ (j+1 == nut->sp_count);
609 for (; j<nut->sp_count && (nus->keyframe_pts[j] != AV_NOPTS_VALUE) == flag; j++)
612 ff_put_v(bc, 1 + 2*flag + 4*n);
613 for (k= j - n; k<=j && k<nut->sp_count; k++) {
614 if (nus->keyframe_pts[k] == AV_NOPTS_VALUE)
616 av_assert0(nus->keyframe_pts[k] > last_pts);
617 ff_put_v(bc, nus->keyframe_pts[k] - last_pts);
618 last_pts = nus->keyframe_pts[k];
623 payload_size = avio_tell(bc) - startpos + 8 + 4;
625 avio_wb64(bc, 8 + payload_size + av_log2(payload_size) / 7 + 1 + 4*(payload_size > 4096));
630 static int write_headers(AVFormatContext *avctx, AVIOContext *bc)
632 NUTContext *nut = avctx->priv_data;
636 ff_metadata_conv_ctx(avctx, ff_nut_metadata_conv, NULL);
638 ret = avio_open_dyn_buf(&dyn_bc);
641 write_mainheader(nut, dyn_bc);
642 put_packet(nut, bc, dyn_bc, 1, MAIN_STARTCODE);
644 for (i = 0; i < nut->avf->nb_streams; i++) {
645 ret = avio_open_dyn_buf(&dyn_bc);
648 ret = write_streamheader(avctx, dyn_bc, nut->avf->streams[i], i);
651 put_packet(nut, bc, dyn_bc, 1, STREAM_STARTCODE);
654 ret = avio_open_dyn_buf(&dyn_bc);
657 write_globalinfo(nut, dyn_bc);
658 put_packet(nut, bc, dyn_bc, 1, INFO_STARTCODE);
660 for (i = 0; i < nut->avf->nb_streams; i++) {
661 ret = avio_open_dyn_buf(&dyn_bc);
664 ret = write_streaminfo(nut, dyn_bc, i);
668 put_packet(nut, bc, dyn_bc, 1, INFO_STARTCODE);
670 ffio_free_dyn_buf(&dyn_bc);
673 for (i = 0; i < nut->avf->nb_chapters; i++) {
674 ret = avio_open_dyn_buf(&dyn_bc);
677 ret = write_chapter(nut, dyn_bc, i);
679 ffio_free_dyn_buf(&dyn_bc);
682 put_packet(nut, bc, dyn_bc, 1, INFO_STARTCODE);
685 nut->last_syncpoint_pos = INT_MIN;
690 static int nut_write_header(AVFormatContext *s)
692 NUTContext *nut = s->priv_data;
693 AVIOContext *bc = s->pb;
698 nut->version = FFMAX(NUT_STABLE_VERSION, 3 + !!nut->flags);
699 if (nut->version > 3 && s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
700 av_log(s, AV_LOG_ERROR,
701 "The additional syncpoint modes require version %d, "
702 "that is currently not finalized, "
703 "please set -f_strict experimental in order to enable it.\n",
705 return AVERROR_EXPERIMENTAL;
708 nut->stream = av_calloc(s->nb_streams, sizeof(*nut->stream ));
709 nut->chapter = av_calloc(s->nb_chapters, sizeof(*nut->chapter));
710 nut->time_base= av_calloc(s->nb_streams +
711 s->nb_chapters, sizeof(*nut->time_base));
712 if (!nut->stream || !nut->chapter || !nut->time_base) {
713 av_freep(&nut->stream);
714 av_freep(&nut->chapter);
715 av_freep(&nut->time_base);
716 return AVERROR(ENOMEM);
719 for (i = 0; i < s->nb_streams; i++) {
720 AVStream *st = s->streams[i];
722 AVRational time_base;
723 ff_parse_specific_params(st, &time_base.den, &ssize, &time_base.num);
725 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->codec->sample_rate) {
726 time_base = (AVRational) {1, st->codec->sample_rate};
728 time_base = ff_choose_timebase(s, st, 48000);
731 avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
733 for (j = 0; j < nut->time_base_count; j++)
734 if (!memcmp(&time_base, &nut->time_base[j], sizeof(AVRational))) {
737 nut->time_base[j] = time_base;
738 nut->stream[i].time_base = &nut->time_base[j];
739 if (j == nut->time_base_count)
740 nut->time_base_count++;
742 if (INT64_C(1000) * time_base.num >= time_base.den)
743 nut->stream[i].msb_pts_shift = 7;
745 nut->stream[i].msb_pts_shift = 14;
746 nut->stream[i].max_pts_distance =
747 FFMAX(time_base.den, time_base.num) / time_base.num;
750 for (i = 0; i < s->nb_chapters; i++) {
751 AVChapter *ch = s->chapters[i];
753 for (j = 0; j < nut->time_base_count; j++)
754 if (!memcmp(&ch->time_base, &nut->time_base[j], sizeof(AVRational)))
757 nut->time_base[j] = ch->time_base;
758 nut->chapter[i].time_base = &nut->time_base[j];
759 if (j == nut->time_base_count)
760 nut->time_base_count++;
763 nut->max_distance = MAX_DISTANCE;
764 build_elision_headers(s);
766 av_assert0(nut->frame_code['N'].flags == FLAG_INVALID);
768 avio_write(bc, ID_STRING, strlen(ID_STRING));
771 if ((ret = write_headers(s, bc)) < 0)
774 if (s->avoid_negative_ts < 0)
775 s->avoid_negative_ts = 1;
782 static int get_needed_flags(NUTContext *nut, StreamContext *nus, FrameCode *fc,
787 if (pkt->flags & AV_PKT_FLAG_KEY)
789 if (pkt->stream_index != fc->stream_id)
790 flags |= FLAG_STREAM_ID;
791 if (pkt->size / fc->size_mul)
792 flags |= FLAG_SIZE_MSB;
793 if (pkt->pts - nus->last_pts != fc->pts_delta)
794 flags |= FLAG_CODED_PTS;
795 if (pkt->side_data_elems && nut->version > 3)
796 flags |= FLAG_SM_DATA;
797 if (pkt->size > 2 * nut->max_distance)
798 flags |= FLAG_CHECKSUM;
799 if (FFABS(pkt->pts - nus->last_pts) > nus->max_pts_distance)
800 flags |= FLAG_CHECKSUM;
801 if (pkt->size < nut->header_len[fc->header_idx] ||
802 (pkt->size > 4096 && fc->header_idx) ||
803 memcmp(pkt->data, nut->header[fc->header_idx],
804 nut->header_len[fc->header_idx]))
805 flags |= FLAG_HEADER_IDX;
807 return flags | (fc->flags & FLAG_CODED);
810 static int find_best_header_idx(NUTContext *nut, AVPacket *pkt)
816 if (pkt->size > 4096)
819 for (i = 1; i < nut->header_count; i++)
820 if (pkt->size >= nut->header_len[i]
821 && nut->header_len[i] > best_len
822 && !memcmp(pkt->data, nut->header[i], nut->header_len[i])) {
824 best_len = nut->header_len[i];
829 static int write_sm_data(AVFormatContext *s, AVIOContext *bc, AVPacket *pkt, int is_meta)
831 int ret, i, dyn_size;
834 int sm_data_count = 0;
838 ret = avio_open_dyn_buf(&dyn_bc);
842 for (i = 0; i<pkt->side_data_elems; i++) {
843 const uint8_t *data = pkt->side_data[i].data;
844 int size = pkt->side_data[i].size;
845 const uint8_t *data_end = data + size;
848 if ( pkt->side_data[i].type == AV_PKT_DATA_METADATA_UPDATE
849 || pkt->side_data[i].type == AV_PKT_DATA_STRINGS_METADATA) {
850 if (!size || data[size-1]) {
851 ret = AVERROR(EINVAL);
854 while (data < data_end) {
855 const uint8_t *key = data;
856 const uint8_t *val = data + strlen(key) + 1;
858 if(val >= data_end) {
859 ret = AVERROR(EINVAL);
862 put_str(dyn_bc, key);
864 put_str(dyn_bc, val);
865 data = val + strlen(val) + 1;
870 switch (pkt->side_data[i].type) {
871 case AV_PKT_DATA_PALETTE:
872 case AV_PKT_DATA_NEW_EXTRADATA:
873 case AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL:
875 if (pkt->side_data[i].type == AV_PKT_DATA_PALETTE) {
876 put_str(dyn_bc, "Palette");
877 } else if(pkt->side_data[i].type == AV_PKT_DATA_NEW_EXTRADATA) {
878 put_str(dyn_bc, "Extradata");
879 } else if(pkt->side_data[i].type == AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL) {
880 snprintf(tmp, sizeof(tmp), "CodecSpecificSide%"PRId64"", AV_RB64(data));
881 put_str(dyn_bc, tmp);
883 snprintf(tmp, sizeof(tmp), "UserData%s-SD-%d",
884 (s->flags & AVFMT_FLAG_BITEXACT) ? "Lavf" : LIBAVFORMAT_IDENT,
885 pkt->side_data[i].type);
886 put_str(dyn_bc, tmp);
889 put_str(dyn_bc, "bin");
890 ff_put_v(dyn_bc, pkt->side_data[i].size);
891 avio_write(dyn_bc, data, pkt->side_data[i].size);
894 case AV_PKT_DATA_PARAM_CHANGE:
895 flags = bytestream_get_le32(&data);
896 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
897 put_str(dyn_bc, "Channels");
898 put_s(dyn_bc, bytestream_get_le32(&data));
901 if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT) {
902 put_str(dyn_bc, "ChannelLayout");
904 put_str(dyn_bc, "u64");
906 avio_write(dyn_bc, data, 8); data+=8;
909 if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
910 put_str(dyn_bc, "SampleRate");
911 put_s(dyn_bc, bytestream_get_le32(&data));
914 if (flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS) {
915 put_str(dyn_bc, "Width");
916 put_s(dyn_bc, bytestream_get_le32(&data));
917 put_str(dyn_bc, "Height");
918 put_s(dyn_bc, bytestream_get_le32(&data));
922 case AV_PKT_DATA_SKIP_SAMPLES:
924 put_str(dyn_bc, "SkipStart");
925 put_s(dyn_bc, (unsigned)AV_RL32(data));
928 if (AV_RL32(data+4)) {
929 put_str(dyn_bc, "SkipEnd");
930 put_s(dyn_bc, (unsigned)AV_RL32(data+4));
934 case AV_PKT_DATA_METADATA_UPDATE:
935 case AV_PKT_DATA_STRINGS_METADATA:
936 // belongs into meta, not side data
943 ff_put_v(bc, sm_data_count);
944 dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
945 avio_write(bc, dyn_buf, dyn_size);
951 static int nut_write_packet(AVFormatContext *s, AVPacket *pkt)
953 NUTContext *nut = s->priv_data;
954 StreamContext *nus = &nut->stream[pkt->stream_index];
955 AVIOContext *bc = s->pb, *dyn_bc, *sm_bc = NULL;
958 int best_length, frame_code, flags, needed_flags, i, header_idx;
960 int key_frame = !!(pkt->flags & AV_PKT_FLAG_KEY);
964 int data_size = pkt->size;
965 uint8_t *sm_buf = NULL;
968 av_log(s, AV_LOG_ERROR,
969 "Negative pts not supported stream %d, pts %"PRId64"\n",
970 pkt->stream_index, pkt->pts);
971 if (pkt->pts == AV_NOPTS_VALUE)
972 av_log(s, AV_LOG_ERROR, "Try to enable the genpts flag\n");
973 return AVERROR(EINVAL);
976 if (pkt->side_data_elems && nut->version > 3) {
977 ret = avio_open_dyn_buf(&sm_bc);
980 ret = write_sm_data(s, sm_bc, pkt, 0);
982 ret = write_sm_data(s, sm_bc, pkt, 1);
983 sm_size = avio_close_dyn_buf(sm_bc, &sm_buf);
986 data_size += sm_size;
989 if (1LL << (20 + 3 * nut->header_count) <= avio_tell(bc))
990 write_headers(s, bc);
992 if (key_frame && !(nus->last_flags & FLAG_KEY))
995 if (data_size + 30 /*FIXME check*/ + avio_tell(bc) >= nut->last_syncpoint_pos + nut->max_distance)
998 //FIXME: Ensure store_sp is 1 in the first place.
1001 (!(nut->flags & NUT_PIPE) || nut->last_syncpoint_pos == INT_MIN)) {
1002 int64_t sp_pos = INT64_MAX;
1004 ff_nut_reset_ts(nut, *nus->time_base, pkt->dts);
1005 for (i = 0; i < s->nb_streams; i++) {
1006 AVStream *st = s->streams[i];
1007 int64_t dts_tb = av_rescale_rnd(pkt->dts,
1008 nus->time_base->num * (int64_t)nut->stream[i].time_base->den,
1009 nus->time_base->den * (int64_t)nut->stream[i].time_base->num,
1011 int index = av_index_search_timestamp(st, dts_tb,
1012 AVSEEK_FLAG_BACKWARD);
1014 sp_pos = FFMIN(sp_pos, st->index_entries[index].pos);
1015 if (!nut->write_index && 2*index > st->nb_index_entries) {
1016 memmove(st->index_entries,
1017 st->index_entries + index,
1018 sizeof(*st->index_entries) * (st->nb_index_entries - index));
1019 st->nb_index_entries -= index;
1024 nut->last_syncpoint_pos = avio_tell(bc);
1025 ret = avio_open_dyn_buf(&dyn_bc);
1028 put_tt(nut, nus->time_base, dyn_bc, pkt->dts);
1029 ff_put_v(dyn_bc, sp_pos != INT64_MAX ? (nut->last_syncpoint_pos - sp_pos) >> 4 : 0);
1031 if (nut->flags & NUT_BROADCAST) {
1032 put_tt(nut, nus->time_base, dyn_bc,
1033 av_rescale_q(av_gettime(), AV_TIME_BASE_Q, *nus->time_base));
1035 put_packet(nut, bc, dyn_bc, 1, SYNCPOINT_STARTCODE);
1037 if (nut->write_index) {
1038 if ((ret = ff_nut_add_sp(nut, nut->last_syncpoint_pos, 0 /*unused*/, pkt->dts)) < 0)
1041 if ((1ll<<60) % nut->sp_count == 0)
1042 for (i=0; i<s->nb_streams; i++) {
1044 StreamContext *nus = &nut->stream[i];
1045 av_reallocp_array(&nus->keyframe_pts, 2*nut->sp_count, sizeof(*nus->keyframe_pts));
1046 if (!nus->keyframe_pts) {
1047 ret = AVERROR(ENOMEM);
1050 for (j=nut->sp_count == 1 ? 0 : nut->sp_count; j<2*nut->sp_count; j++)
1051 nus->keyframe_pts[j] = AV_NOPTS_VALUE;
1055 av_assert0(nus->last_pts != AV_NOPTS_VALUE);
1057 coded_pts = pkt->pts & ((1 << nus->msb_pts_shift) - 1);
1058 if (ff_lsb2full(nus, coded_pts) != pkt->pts)
1059 coded_pts = pkt->pts + (1 << nus->msb_pts_shift);
1061 best_header_idx = find_best_header_idx(nut, pkt);
1063 best_length = INT_MAX;
1065 for (i = 0; i < 256; i++) {
1067 FrameCode *fc = &nut->frame_code[i];
1068 int flags = fc->flags;
1070 if (flags & FLAG_INVALID)
1072 needed_flags = get_needed_flags(nut, nus, fc, pkt);
1074 if (flags & FLAG_CODED) {
1076 flags = needed_flags;
1079 if ((flags & needed_flags) != needed_flags)
1082 if ((flags ^ needed_flags) & FLAG_KEY)
1085 if (flags & FLAG_STREAM_ID)
1086 length += ff_get_v_length(pkt->stream_index);
1088 if (data_size % fc->size_mul != fc->size_lsb)
1090 if (flags & FLAG_SIZE_MSB)
1091 length += ff_get_v_length(data_size / fc->size_mul);
1093 if (flags & FLAG_CHECKSUM)
1096 if (flags & FLAG_CODED_PTS)
1097 length += ff_get_v_length(coded_pts);
1099 if ( (flags & FLAG_CODED)
1100 && nut->header_len[best_header_idx] > nut->header_len[fc->header_idx] + 1) {
1101 flags |= FLAG_HEADER_IDX;
1104 if (flags & FLAG_HEADER_IDX) {
1105 length += 1 - nut->header_len[best_header_idx];
1107 length -= nut->header_len[fc->header_idx];
1111 length += !(flags & FLAG_CODED_PTS);
1112 length += !(flags & FLAG_CHECKSUM);
1114 if (length < best_length) {
1115 best_length = length;
1119 av_assert0(frame_code != -1);
1121 fc = &nut->frame_code[frame_code];
1123 needed_flags = get_needed_flags(nut, nus, fc, pkt);
1124 header_idx = fc->header_idx;
1126 ffio_init_checksum(bc, ff_crc04C11DB7_update, 0);
1127 avio_w8(bc, frame_code);
1128 if (flags & FLAG_CODED) {
1129 ff_put_v(bc, (flags ^ needed_flags) & ~(FLAG_CODED));
1130 flags = needed_flags;
1132 if (flags & FLAG_STREAM_ID) ff_put_v(bc, pkt->stream_index);
1133 if (flags & FLAG_CODED_PTS) ff_put_v(bc, coded_pts);
1134 if (flags & FLAG_SIZE_MSB ) ff_put_v(bc, data_size / fc->size_mul);
1135 if (flags & FLAG_HEADER_IDX) ff_put_v(bc, header_idx = best_header_idx);
1137 if (flags & FLAG_CHECKSUM) avio_wl32(bc, ffio_get_checksum(bc));
1138 else ffio_get_checksum(bc);
1140 if (flags & FLAG_SM_DATA) {
1141 avio_write(bc, sm_buf, sm_size);
1143 avio_write(bc, pkt->data + nut->header_len[header_idx], pkt->size - nut->header_len[header_idx]);
1145 nus->last_flags = flags;
1146 nus->last_pts = pkt->pts;
1148 //FIXME just store one per syncpoint
1149 if (flags & FLAG_KEY && !(nut->flags & NUT_PIPE)) {
1151 s->streams[pkt->stream_index],
1152 nut->last_syncpoint_pos,
1157 if (nus->keyframe_pts && nus->keyframe_pts[nut->sp_count] == AV_NOPTS_VALUE)
1158 nus->keyframe_pts[nut->sp_count] = pkt->pts;
1161 if (!nut->max_pts_tb || av_compare_ts(nut->max_pts, *nut->max_pts_tb, pkt->pts, *nus->time_base) < 0) {
1162 nut->max_pts = pkt->pts;
1163 nut->max_pts_tb = nus->time_base;
1172 static int nut_write_trailer(AVFormatContext *s)
1174 NUTContext *nut = s->priv_data;
1175 AVIOContext *bc = s->pb, *dyn_bc;
1178 while (nut->header_count < 3)
1179 write_headers(s, bc);
1181 ret = avio_open_dyn_buf(&dyn_bc);
1182 if (ret >= 0 && nut->sp_count) {
1183 av_assert1(nut->write_index);
1184 write_index(nut, dyn_bc);
1185 put_packet(nut, bc, dyn_bc, 1, INDEX_STARTCODE);
1188 ff_nut_free_sp(nut);
1189 for (i=0; i<s->nb_streams; i++)
1190 av_freep(&nut->stream[i].keyframe_pts);
1192 av_freep(&nut->stream);
1193 av_freep(&nut->chapter);
1194 av_freep(&nut->time_base);
1199 #define OFFSET(x) offsetof(NUTContext, x)
1200 #define E AV_OPT_FLAG_ENCODING_PARAM
1201 static const AVOption options[] = {
1202 { "syncpoints", "NUT syncpoint behaviour", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, E, "syncpoints" },
1203 { "default", "", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, E, "syncpoints" },
1204 { "none", "Disable syncpoints, low overhead and unseekable", 0, AV_OPT_TYPE_CONST, {.i64 = NUT_PIPE}, INT_MIN, INT_MAX, E, "syncpoints" },
1205 { "timestamped", "Extend syncpoints with a wallclock timestamp", 0, AV_OPT_TYPE_CONST, {.i64 = NUT_BROADCAST}, INT_MIN, INT_MAX, E, "syncpoints" },
1206 { "write_index", "Write index", OFFSET(write_index), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E, },
1210 static const AVClass class = {
1211 .class_name = "nutenc",
1212 .item_name = av_default_item_name,
1214 .version = LIBAVUTIL_VERSION_INT,
1217 AVOutputFormat ff_nut_muxer = {
1219 .long_name = NULL_IF_CONFIG_SMALL("NUT"),
1220 .mime_type = "video/x-nut",
1221 .extensions = "nut",
1222 .priv_data_size = sizeof(NUTContext),
1223 .audio_codec = CONFIG_LIBVORBIS ? AV_CODEC_ID_VORBIS :
1224 CONFIG_LIBMP3LAME ? AV_CODEC_ID_MP3 : AV_CODEC_ID_MP2,
1225 .video_codec = AV_CODEC_ID_MPEG4,
1226 .write_header = nut_write_header,
1227 .write_packet = nut_write_packet,
1228 .write_trailer = nut_write_trailer,
1229 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
1230 .codec_tag = ff_nut_codec_tags,
1231 .priv_class = &class,