#include "isom.h"
#include "matroska.h"
#include "avc.h"
+#include "flacenc.h"
+#include "libavutil/intreadwrite.h"
#include "libavutil/md5.h"
#include "libavcodec/xiph.h"
#include "libavcodec/mpeg4audio.h"
return 0;
}
-#define FLAC_STREAMINFO_SIZE 34
-
-static int put_flac_codecpriv(AVFormatContext *s, ByteIOContext *pb, AVCodecContext *codec)
-{
- // if the extradata_size is greater than FLAC_STREAMINFO_SIZE,
- // assume that it's in Matroska format already
- if (codec->extradata_size < FLAC_STREAMINFO_SIZE) {
- av_log(s, AV_LOG_ERROR, "Invalid FLAC extradata\n");
- return -1;
- } else if (codec->extradata_size == FLAC_STREAMINFO_SIZE) {
- // only the streaminfo packet
- put_buffer(pb, "fLaC", 4);
- put_byte(pb, 0x80);
- put_be24(pb, FLAC_STREAMINFO_SIZE);
- } else if(memcmp("fLaC", codec->extradata, 4)) {
- av_log(s, AV_LOG_ERROR, "Invalid FLAC extradata\n");
- return -1;
- }
- put_buffer(pb, codec->extradata, codec->extradata_size);
- return 0;
-}
-
static void get_aac_sample_rates(AVFormatContext *s, AVCodecContext *codec, int *sample_rate, int *output_sample_rate)
{
int sri;
if (codec->codec_id == CODEC_ID_VORBIS || codec->codec_id == CODEC_ID_THEORA)
ret = put_xiph_codecpriv(s, dyn_cp, codec);
else if (codec->codec_id == CODEC_ID_FLAC)
- ret = put_flac_codecpriv(s, dyn_cp, codec);
+ ret = ff_flac_write_header(dyn_cp, codec);
else if (codec->codec_id == CODEC_ID_H264)
ret = ff_isom_write_avcc(dyn_cp, codec->extradata, codec->extradata_size);
else if (codec->extradata_size)
} else if (codec->codec_type == CODEC_TYPE_VIDEO) {
if (qt_id) {
if (!codec->codec_tag)
- codec->codec_tag = codec_get_tag(codec_movvideo_tags, codec->codec_id);
+ codec->codec_tag = ff_codec_get_tag(codec_movvideo_tags, codec->codec_id);
if (codec->extradata_size)
put_buffer(dyn_cp, codec->extradata, codec->extradata_size);
} else {
if (!codec->codec_tag)
- codec->codec_tag = codec_get_tag(codec_bmp_tags, codec->codec_id);
+ codec->codec_tag = ff_codec_get_tag(ff_codec_bmp_tags, codec->codec_id);
if (!codec->codec_tag) {
av_log(s, AV_LOG_ERROR, "No bmp codec ID found.");
ret = -1;
}
- put_bmp_header(dyn_cp, codec, codec_bmp_tags, 0);
+ ff_put_bmp_header(dyn_cp, codec, ff_codec_bmp_tags, 0);
}
} else if (codec->codec_type == CODEC_TYPE_AUDIO) {
if (!codec->codec_tag)
- codec->codec_tag = codec_get_tag(codec_wav_tags, codec->codec_id);
+ codec->codec_tag = ff_codec_get_tag(ff_codec_wav_tags, codec->codec_id);
if (!codec->codec_tag) {
av_log(s, AV_LOG_ERROR, "No wav codec ID found.");
ret = -1;
}
- put_wav_header(dyn_cp, codec);
+ ff_put_wav_header(dyn_cp, codec);
}
codecpriv_size = url_close_dyn_buf(dyn_cp, &codecpriv);
int bit_depth = av_get_bits_per_sample(codec->codec_id);
int sample_rate = codec->sample_rate;
int output_sample_rate = 0;
+ AVMetadataTag *tag;
if (!bit_depth)
bit_depth = av_get_bits_per_sample_format(codec->sample_fmt);
put_ebml_uint (pb, MATROSKA_ID_TRACKNUMBER , i + 1);
put_ebml_uint (pb, MATROSKA_ID_TRACKUID , i + 1);
put_ebml_uint (pb, MATROSKA_ID_TRACKFLAGLACING , 0); // no lacing (yet)
+ put_ebml_float(pb, MATROSKA_ID_TRACKTIMECODESCALE, 1.0);
- if (st->language[0])
- put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, st->language);
- else
- put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, "und");
+ if ((tag = av_metadata_get(st->metadata, "description", NULL, 0)))
+ put_ebml_string(pb, MATROSKA_ID_TRACKNAME, tag->value);
+ tag = av_metadata_get(st->metadata, "language", NULL, 0);
+ put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, tag ? tag->value:"und");
- put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGDEFAULT, !!(st->disposition & AV_DISPOSITION_DEFAULT));
+ if (st->disposition)
+ put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGDEFAULT, !!(st->disposition & AV_DISPOSITION_DEFAULT));
// look for a codec ID string specific to mkv to use,
// if none are found, use AVI codes
put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_VIDEO);
if (!native_id &&
- codec_get_tag(codec_movvideo_tags, codec->codec_id) &&
- (!codec_get_tag(codec_bmp_tags, codec->codec_id)
+ ff_codec_get_tag(codec_movvideo_tags, codec->codec_id) &&
+ (!ff_codec_get_tag(ff_codec_bmp_tags, codec->codec_id)
|| codec->codec_id == CODEC_ID_SVQ1
|| codec->codec_id == CODEC_ID_SVQ3
|| codec->codec_id == CODEC_ID_CINEPAK))
return 0;
}
+static int mkv_write_chapters(AVFormatContext *s)
+{
+ MatroskaMuxContext *mkv = s->priv_data;
+ ByteIOContext *pb = s->pb;
+ ebml_master chapters, editionentry;
+ AVRational scale = {1, 1E9};
+ int i, ret;
+
+ if (!s->nb_chapters)
+ return 0;
+
+ ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_CHAPTERS, url_ftell(pb));
+ if (ret < 0) return ret;
+
+ chapters = start_ebml_master(pb, MATROSKA_ID_CHAPTERS , 0);
+ editionentry = start_ebml_master(pb, MATROSKA_ID_EDITIONENTRY, 0);
+ put_ebml_uint(pb, MATROSKA_ID_EDITIONFLAGDEFAULT, 1);
+ put_ebml_uint(pb, MATROSKA_ID_EDITIONFLAGHIDDEN , 0);
+ for (i = 0; i < s->nb_chapters; i++) {
+ ebml_master chapteratom, chapterdisplay;
+ AVChapter *c = s->chapters[i];
+ AVMetadataTag *t = NULL;
+
+ chapteratom = start_ebml_master(pb, MATROSKA_ID_CHAPTERATOM, 0);
+ put_ebml_uint(pb, MATROSKA_ID_CHAPTERUID, c->id);
+ put_ebml_uint(pb, MATROSKA_ID_CHAPTERTIMESTART,
+ av_rescale_q(c->start, c->time_base, scale));
+ put_ebml_uint(pb, MATROSKA_ID_CHAPTERTIMEEND,
+ av_rescale_q(c->end, c->time_base, scale));
+ put_ebml_uint(pb, MATROSKA_ID_CHAPTERFLAGHIDDEN , 0);
+ put_ebml_uint(pb, MATROSKA_ID_CHAPTERFLAGENABLED, 1);
+ if ((t = av_metadata_get(c->metadata, "title", NULL, 0))) {
+ chapterdisplay = start_ebml_master(pb, MATROSKA_ID_CHAPTERDISPLAY, 0);
+ put_ebml_string(pb, MATROSKA_ID_CHAPSTRING, t->value);
+ put_ebml_string(pb, MATROSKA_ID_CHAPLANG , "und");
+ end_ebml_master(pb, chapterdisplay);
+ }
+ end_ebml_master(pb, chapteratom);
+ }
+ end_ebml_master(pb, editionentry);
+ end_ebml_master(pb, chapters);
+ return 0;
+}
+
static int mkv_write_header(AVFormatContext *s)
{
MatroskaMuxContext *mkv = s->priv_data;
ByteIOContext *pb = s->pb;
ebml_master ebml_header, segment_info;
+ AVMetadataTag *tag;
int ret;
mkv->md5_ctx = av_mallocz(av_md5_size);
segment_info = start_ebml_master(pb, MATROSKA_ID_INFO, 0);
put_ebml_uint(pb, MATROSKA_ID_TIMECODESCALE, 1000000);
- if (strlen(s->title))
- put_ebml_string(pb, MATROSKA_ID_TITLE, s->title);
+ if ((tag = av_metadata_get(s->metadata, "title", NULL, 0)))
+ put_ebml_string(pb, MATROSKA_ID_TITLE, tag->value);
if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
put_ebml_string(pb, MATROSKA_ID_MUXINGAPP , LIBAVFORMAT_IDENT);
put_ebml_string(pb, MATROSKA_ID_WRITINGAPP, LIBAVFORMAT_IDENT);
ret = mkv_write_tracks(s);
if (ret < 0) return ret;
+ ret = mkv_write_chapters(s);
+ if (ret < 0) return ret;
+
ret = mkv_add_seekhead_entry(mkv->cluster_seekhead, MATROSKA_ID_CLUSTER, url_ftell(pb));
if (ret < 0) return ret;
if (mkv->cues == NULL)
return AVERROR(ENOMEM);
+ put_flush_packet(pb);
return 0;
}
-static int mkv_blockgroup_size(AVPacket *pkt)
+static int mkv_blockgroup_size(int pkt_size)
{
- int size = pkt->size + 4;
+ int size = pkt_size + 4;
size += ebml_num_size(size);
size += 2; // EBML ID for block and block duration
size += 8; // max size of block duration
return size;
}
+static int ass_get_duration(const uint8_t *p)
+{
+ int sh, sm, ss, sc, eh, em, es, ec;
+ uint64_t start, end;
+
+ if (sscanf(p, "%*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d",
+ &sh, &sm, &ss, &sc, &eh, &em, &es, &ec) != 8)
+ return 0;
+ start = 3600000*sh + 60000*sm + 1000*ss + 10*sc;
+ end = 3600000*eh + 60000*em + 1000*es + 10*ec;
+ return end - start;
+}
+
+static int mkv_write_ass_blocks(AVFormatContext *s, AVPacket *pkt)
+{
+ MatroskaMuxContext *mkv = s->priv_data;
+ ByteIOContext *pb = s->pb;
+ int i, layer = 0, max_duration = 0, size, line_size, data_size = pkt->size;
+ uint8_t *start, *end, *data = pkt->data;
+ ebml_master blockgroup;
+ char buffer[2048];
+
+ while (data_size) {
+ int duration = ass_get_duration(data);
+ max_duration = FFMAX(duration, max_duration);
+ end = memchr(data, '\n', data_size);
+ size = line_size = end ? end-data+1 : data_size;
+ size -= end ? (end[-1]=='\r')+1 : 0;
+ start = data;
+ for (i=0; i<3; i++, start++)
+ if (!(start = memchr(start, ',', size-(start-data))))
+ return max_duration;
+ size -= start - data;
+ sscanf(data, "Dialogue: %d,", &layer);
+ i = snprintf(buffer, sizeof(buffer), "%"PRId64",%d,",
+ s->streams[pkt->stream_index]->nb_frames++, layer);
+ size = FFMIN(i+size, sizeof(buffer));
+ memcpy(buffer+i, start, size-i);
+
+ av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
+ "pts %" PRId64 ", duration %d\n",
+ url_ftell(pb), size, pkt->pts, duration);
+ blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, mkv_blockgroup_size(size));
+ put_ebml_id(pb, MATROSKA_ID_BLOCK);
+ put_ebml_num(pb, size+4, 0);
+ put_byte(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
+ put_be16(pb, pkt->pts - mkv->cluster_pts);
+ put_byte(pb, 0);
+ put_buffer(pb, buffer, size);
+ put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, duration);
+ end_ebml_master(pb, blockgroup);
+
+ data += line_size;
+ data_size -= line_size;
+ }
+
+ return max_duration;
+}
+
static void mkv_write_block(AVFormatContext *s, unsigned int blockid, AVPacket *pkt, int flags)
{
MatroskaMuxContext *mkv = s->priv_data;
ByteIOContext *pb = s->pb;
+ AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
+ uint8_t *data = NULL;
+ int size = pkt->size;
av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
"pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",
url_ftell(pb), pkt->size, pkt->pts, pkt->dts, pkt->duration, flags);
+ if (codec->codec_id == CODEC_ID_H264 && codec->extradata_size > 0 &&
+ (AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1))
+ ff_avc_parse_nal_units_buf(pkt->data, &data, &size);
+ else
+ data = pkt->data;
put_ebml_id(pb, blockid);
- put_ebml_num(pb, pkt->size+4, 0);
+ put_ebml_num(pb, size+4, 0);
put_byte(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
put_be16(pb, pkt->pts - mkv->cluster_pts);
put_byte(pb, flags);
- put_buffer(pb, pkt->data, pkt->size);
+ put_buffer(pb, data, size);
+ if (data != pkt->data)
+ av_free(data);
}
static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
av_md5_update(mkv->md5_ctx, pkt->data, FFMIN(200, pkt->size));
}
- if (codec->codec_id == CODEC_ID_H264 &&
- codec->extradata_size > 0 && AV_RB32(codec->extradata) == 0x00000001) {
- /* from x264 or from bytestream h264 */
- /* nal reformating needed */
- int ret = ff_avc_parse_nal_units(pkt->data, &pkt->data, &pkt->size);
- if (ret < 0)
- return ret;
- assert(pkt->size);
- }
-
if (codec->codec_type != CODEC_TYPE_SUBTITLE) {
mkv_write_block(s, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe << 7);
+ } else if (codec->codec_id == CODEC_ID_SSA) {
+ duration = mkv_write_ass_blocks(s, pkt);
} else {
- ebml_master blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, mkv_blockgroup_size(pkt));
+ ebml_master blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, mkv_blockgroup_size(pkt->size));
duration = pkt->convergence_duration;
mkv_write_block(s, MATROSKA_ID_BLOCK, pkt, 0);
- put_ebml_uint(pb, MATROSKA_ID_DURATION, duration);
+ put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, duration);
end_ebml_master(pb, blockgroup);
}
end_ebml_master(pb, mkv->segment);
av_free(mkv->md5_ctx);
+ put_flush_packet(pb);
return 0;
}
mkv_write_header,
mkv_write_packet,
mkv_write_trailer,
- .codec_tag = (const AVCodecTag* const []){codec_bmp_tags, codec_wav_tags, 0},
+ .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
+ .codec_tag = (const AVCodecTag* const []){ff_codec_bmp_tags, ff_codec_wav_tags, 0},
.subtitle_codec = CODEC_ID_TEXT,
};
mkv_write_header,
mkv_write_packet,
mkv_write_trailer,
- .codec_tag = (const AVCodecTag* const []){codec_wav_tags, 0},
+ .flags = AVFMT_GLOBALHEADER,
+ .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
};