* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+#include "avc.h"
#include "avformat.h"
+#include "avlanguage.h"
+#include "flacenc.h"
#include "internal.h"
-#include "riff.h"
#include "isom.h"
#include "matroska.h"
-#include "avc.h"
-#include "flacenc.h"
-#include "avlanguage.h"
-#include "libavutil/samplefmt.h"
-#include "libavutil/sha.h"
-#include "libavutil/intreadwrite.h"
+#include "riff.h"
+
+#include "libavutil/avstring.h"
+#include "libavutil/dict.h"
#include "libavutil/intfloat.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/lfg.h"
#include "libavutil/mathematics.h"
+#include "libavutil/opt.h"
#include "libavutil/random_seed.h"
-#include "libavutil/lfg.h"
-#include "libavutil/dict.h"
-#include "libavutil/avstring.h"
+#include "libavutil/samplefmt.h"
+#include "libavutil/sha.h"
+
#include "libavcodec/xiph.h"
#include "libavcodec/mpeg4audio.h"
#define MODE_WEBM 0x02
typedef struct MatroskaMuxContext {
+ const AVClass *class;
int mode;
AVIOContext *dyn_bc;
ebml_master segment;
AVPacket cur_audio_pkt;
int have_attachments;
+
+ int reserve_cues_space;
+ int64_t cues_pos;
} MatroskaMuxContext;
put_ebml_uint(pb, MATROSKA_ID_VIDEOSTEREOMODE, st_mode);
}
+ if ((tag = av_dict_get(st->metadata, "alpha_mode", NULL, 0)) ||
+ (tag = av_dict_get( s->metadata, "alpha_mode", NULL, 0)) ||
+ (codec->pix_fmt == AV_PIX_FMT_YUVA420P)) {
+ put_ebml_uint(pb, MATROSKA_ID_VIDEOALPHAMODE, 1);
+ }
+
if (st->sample_aspect_ratio.num) {
int64_t d_width = av_rescale(codec->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den);
if (d_width > INT_MAX) {
break;
default:
av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
- break;
+ return AVERROR(EINVAL);
}
ret = mkv_write_codecprivate(s, pb, codec, native_id, qt_id);
if (ret < 0) return ret;
if (mkv->cues == NULL)
return AVERROR(ENOMEM);
+ if (pb->seekable && mkv->reserve_cues_space) {
+ mkv->cues_pos = avio_tell(pb);
+ put_ebml_void(pb, mkv->reserve_cues_space);
+ }
+
av_init_packet(&mkv->cur_audio_pkt);
mkv->cur_audio_pkt.size = 0;
mkv->cluster_pos = -1;
{
MatroskaMuxContext *mkv = s->priv_data;
AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
- uint8_t *data = NULL;
- int offset = 0, size = pkt->size;
+ uint8_t *data = NULL, *side_data = NULL;
+ int offset = 0, size = pkt->size, side_data_size = 0;
int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
+ uint64_t additional_id = 0;
+ ebml_master block_group, block_additions, block_more;
av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
"pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",
offset = 8;
}
+ side_data = av_packet_get_side_data(pkt,
+ AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL,
+ &side_data_size);
+ if (side_data) {
+ additional_id = AV_RB64(side_data);
+ side_data += 8;
+ side_data_size -= 8;
+ }
+
+ if (side_data_size && additional_id == 1) {
+ block_group = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, 0);
+ blockid = MATROSKA_ID_BLOCK;
+ }
+
put_ebml_id(pb, blockid);
put_ebml_num(pb, size+4, 0);
avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
avio_write(pb, data + offset, size);
if (data != pkt->data)
av_free(data);
+
+ if (side_data_size && additional_id == 1) {
+ block_additions = start_ebml_master(pb, MATROSKA_ID_BLOCKADDITIONS, 0);
+ block_more = start_ebml_master(pb, MATROSKA_ID_BLOCKMORE, 0);
+ put_ebml_uint(pb, MATROSKA_ID_BLOCKADDID, 1);
+ put_ebml_id(pb, MATROSKA_ID_BLOCKADDITIONAL);
+ put_ebml_num(pb, side_data_size, 0);
+ avio_write(pb, side_data, side_data_size);
+ end_ebml_master(pb, block_more);
+ end_ebml_master(pb, block_additions);
+ end_ebml_master(pb, block_group);
+ }
}
static int srt_get_duration(uint8_t **buf)
if (pb->seekable) {
if (mkv->cues->num_entries) {
- cuespos = mkv_write_cues(pb, mkv->cues, mkv->tracks, s->nb_streams);
+ if (mkv->reserve_cues_space) {
+ int64_t cues_end;
+
+ currentpos = avio_tell(pb);
+ avio_seek(pb, mkv->cues_pos, SEEK_SET);
+
+ cuespos = mkv_write_cues(pb, mkv->cues, mkv->tracks, s->nb_streams);
+ cues_end = avio_tell(pb);
+ if (cues_end > cuespos + mkv->reserve_cues_space) {
+ av_log(s, AV_LOG_ERROR, "Insufficient space reserved for cues: %d "
+ "(needed: %"PRId64").\n", mkv->reserve_cues_space,
+ cues_end - cuespos);
+ return AVERROR(EINVAL);
+ }
+
+ if (cues_end < cuespos + mkv->reserve_cues_space)
+ put_ebml_void(pb, mkv->reserve_cues_space - (cues_end - cuespos));
+
+ avio_seek(pb, currentpos, SEEK_SET);
+ } else {
+ cuespos = mkv_write_cues(pb, mkv->cues, mkv->tracks, s->nb_streams);
+ }
ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_CUES, cuespos);
if (ret < 0) return ret;
{ AV_CODEC_ID_NONE, 0xFFFFFFFF }
};
+#define OFFSET(x) offsetof(MatroskaMuxContext, x)
+#define FLAGS AV_OPT_FLAG_ENCODING_PARAM
+static const AVOption options[] = {
+ { "reserve_index_space", "Reserve a given amount of space (in bytes) at the beginning "
+ "of the file for the index (cues).", OFFSET(reserve_cues_space), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
+ { NULL },
+};
+
#if CONFIG_MATROSKA_MUXER
+static const AVClass matroska_class = {
+ .class_name = "matroska muxer",
+ .item_name = av_default_item_name,
+ .option = options,
+ .version = LIBAVUTIL_VERSION_INT,
+};
+
AVOutputFormat ff_matroska_muxer = {
.name = "matroska",
.long_name = NULL_IF_CONFIG_SMALL("Matroska"),
.subtitle_codec = AV_CODEC_ID_ASS,
#endif
.query_codec = mkv_query_codec,
+ .priv_class = &matroska_class,
};
#endif
#if CONFIG_WEBM_MUXER
+static const AVClass webm_class = {
+ .class_name = "webm muxer",
+ .item_name = av_default_item_name,
+ .option = options,
+ .version = LIBAVUTIL_VERSION_INT,
+};
+
AVOutputFormat ff_webm_muxer = {
.name = "webm",
.long_name = NULL_IF_CONFIG_SMALL("WebM"),
.write_trailer = mkv_write_trailer,
.flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |
AVFMT_TS_NONSTRICT,
+ .priv_class = &webm_class,
};
#endif
#if CONFIG_MATROSKA_AUDIO_MUXER
+static const AVClass mka_class = {
+ .class_name = "matroska audio muxer",
+ .item_name = av_default_item_name,
+ .option = options,
+ .version = LIBAVUTIL_VERSION_INT,
+};
AVOutputFormat ff_matroska_audio_muxer = {
.name = "matroska",
.long_name = NULL_IF_CONFIG_SMALL("Matroska"),
.codec_tag = (const AVCodecTag* const []){
ff_codec_wav_tags, additional_audio_tags, 0
},
+ .priv_class = &mka_class,
};
#endif