From 01656fd47694d76e30cac356632b98482263428d Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Tue, 28 May 2013 10:09:21 +0200 Subject: [PATCH] matroskaenc: support muxing WavPack --- libavformat/Makefile | 2 +- libavformat/matroskaenc.c | 73 ++++++++++++++++++++++++++++++++++++++- tests/Makefile | 2 ++ tests/fate/wavpack.mak | 10 ++++++ 4 files changed, 85 insertions(+), 2 deletions(-) diff --git a/libavformat/Makefile b/libavformat/Makefile index 28836128ff..06c6ad8509 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -160,7 +160,7 @@ OBJS-$(CONFIG_MATROSKA_DEMUXER) += matroskadec.o matroska.o \ isom.o rmsipr.o OBJS-$(CONFIG_MATROSKA_MUXER) += matroskaenc.o matroska.o \ isom.o avc.o \ - flacenc_header.o avlanguage.o + flacenc_header.o avlanguage.o wv.o OBJS-$(CONFIG_MD5_MUXER) += md5enc.o OBJS-$(CONFIG_MJPEG_DEMUXER) += rawdec.o OBJS-$(CONFIG_MJPEG_MUXER) += rawenc.o diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c index c521ed3926..9f7b5f9b52 100644 --- a/libavformat/matroskaenc.c +++ b/libavformat/matroskaenc.c @@ -27,6 +27,7 @@ #include "isom.h" #include "matroska.h" #include "riff.h" +#include "wv.h" #include "libavutil/avstring.h" #include "libavutil/dict.h" @@ -450,6 +451,15 @@ static int put_xiph_codecpriv(AVFormatContext *s, AVIOContext *pb, AVCodecContex return 0; } +static int put_wv_codecpriv(AVIOContext *pb, AVCodecContext *codec) +{ + if (codec->extradata && codec->extradata_size == 2) + avio_write(pb, codec->extradata, 2); + else + avio_wl16(pb, 0x403); // fallback to the version mentioned in matroska specs + return 0; +} + static void get_aac_sample_rates(AVFormatContext *s, AVCodecContext *codec, int *sample_rate, int *output_sample_rate) { MPEG4AudioConfig mp4ac; @@ -479,6 +489,8 @@ static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecCo ret = put_xiph_codecpriv(s, dyn_cp, codec); else if (codec->codec_id == AV_CODEC_ID_FLAC) ret = ff_flac_write_header(dyn_cp, codec, 1); + else if (codec->codec_id == AV_CODEC_ID_WAVPACK) + ret = put_wv_codecpriv(dyn_cp, codec); else if (codec->codec_id == AV_CODEC_ID_H264) ret = ff_isom_write_avcc(dyn_cp, codec->extradata, codec->extradata_size); else if (codec->codec_id == AV_CODEC_ID_ALAC) { @@ -1054,6 +1066,59 @@ static int mkv_write_ass_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *p return max_duration; } +static int mkv_strip_wavpack(const uint8_t *src, uint8_t **pdst, int *size) +{ + uint8_t *dst; + int srclen = *size; + int offset = 0; + int ret; + + dst = av_malloc(srclen); + if (!dst) + return AVERROR(ENOMEM); + + while (srclen >= WV_HEADER_SIZE) { + WvHeader header; + + ret = ff_wv_parse_header(&header, src); + if (ret < 0) + goto fail; + src += WV_HEADER_SIZE; + srclen -= WV_HEADER_SIZE; + + if (srclen < header.blocksize) { + ret = AVERROR_INVALIDDATA; + goto fail; + } + + if (header.initial) { + AV_WL32(dst + offset, header.samples); + offset += 4; + } + AV_WL32(dst + offset, header.flags); + AV_WL32(dst + offset + 4, header.crc); + offset += 8; + + if (!(header.initial && header.final)) { + AV_WL32(dst + offset, header.blocksize); + offset += 4; + } + + memcpy(dst + offset, src, header.blocksize); + src += header.blocksize; + srclen -= header.blocksize; + offset += header.blocksize; + } + + *pdst = dst; + *size = offset; + + return 0; +fail: + av_freep(&dst); + return ret; +} + static void mkv_write_block(AVFormatContext *s, AVIOContext *pb, unsigned int blockid, AVPacket *pkt, int flags) { @@ -1069,7 +1134,13 @@ static void mkv_write_block(AVFormatContext *s, AVIOContext *pb, if (codec->codec_id == AV_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 + else if (codec->codec_id == AV_CODEC_ID_WAVPACK) { + int ret = mkv_strip_wavpack(pkt->data, &data, &size); + if (ret < 0) { + av_log(s, AV_LOG_ERROR, "Error stripping a WavPack packet.\n"); + return; + } + } else data = pkt->data; if (codec->codec_id == AV_CODEC_ID_PRORES) { diff --git a/tests/Makefile b/tests/Makefile index 0951bdec3e..e32320dc9c 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -42,6 +42,8 @@ ENCDEC2 = $(call ALLYES, $(firstword $(1))_ENCODER $(lastword $(1))_DECODER \ DEMDEC = $(call ALLYES, $(1)_DEMUXER $(2:%=%_DECODER)) ENCMUX = $(call ALLYES, $(1:%=%_ENCODER) $(2)_MUXER) +DEMMUX = $(call ALLYES, $(1)_DEMUXER $(2)_MUXER) + FILTERDEMDEC = $(call ALLYES, $(1)_FILTER $(2)_DEMUXER $(3)_DECODER) FILTERDEMDECMUX = $(call ALLYES, $(1)_FILTER $(2)_DEMUXER $(3)_DECODER $(4)_MUXER) FILTERDEMDECENCMUX = $(call ALLYES, $(1)_FILTER $(2)_DEMUXER $(3)_DECODER $(4)_ENCODER $(5)_MUXER) diff --git a/tests/fate/wavpack.mak b/tests/fate/wavpack.mak index 64bc7a8800..4ed3f2804f 100644 --- a/tests/fate/wavpack.mak +++ b/tests/fate/wavpack.mak @@ -88,5 +88,15 @@ FATE_WAVPACK-$(call DEMDEC, WV, WAVPACK) += $(FATE_WAVPACK) FATE_WAVPACK-$(call DEMDEC, MATROSKA, WAVPACK) += fate-wavpack-matroskamode fate-wavpack-matroskamode: CMD = md5 -i $(TARGET_SAMPLES)/wavpack/special/matroska_mode.mka -f s16le +FATE_WAVPACK-$(call DEMMUX, WV, MATROSKA) += fate-wavpack-matroska_mux-mono +fate-wavpack-matroska_mux-mono: CMD = md5 -i $(TARGET_SAMPLES)/wavpack/num_channels/mono_16bit_int.wv -c copy -flags +bitexact -f matroska +fate-wavpack-matroska_mux-mono: CMP = oneline +fate-wavpack-matroska_mux-mono: REF = f471da7c71cd01d5fb81efb1d740504f + +FATE_WAVPACK-$(call DEMMUX, WV, MATROSKA) += fate-wavpack-matroska_mux-61 +fate-wavpack-matroska_mux-61: CMD = md5 -i $(TARGET_SAMPLES)/wavpack/num_channels/eva_2.22_6.1_16bit-partial.wv -c copy -flags +bitexact -f matroska +fate-wavpack-matroska_mux-61: CMP = oneline +fate-wavpack-matroska_mux-61: REF = cbbd0ce7eabef52235bcdd2cb0690f24 + FATE_SAMPLES_AVCONV += $(FATE_WAVPACK-yes) fate-wavpack: $(FATE_WAVPACK-yes)