From 01656fd47694d76e30cac356632b98482263428d Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Tue, 28 May 2013 10:09:21 +0200 Subject: [PATCH 1/3] 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) From 88de0c7901ee2bd6021cf32def87ce98ce63155c Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Tue, 28 May 2013 17:43:22 +0200 Subject: [PATCH 2/3] apetag: add support for writing APE tags This will be useful in the WavPack muxer. --- libavformat/apetag.c | 56 ++++++++++++++++++++++++++++++++++++++++++++ libavformat/apetag.h | 5 ++++ 2 files changed, 61 insertions(+) diff --git a/libavformat/apetag.c b/libavformat/apetag.c index 0d2cb973fb..68f2df6c56 100644 --- a/libavformat/apetag.c +++ b/libavformat/apetag.c @@ -23,12 +23,14 @@ #include "libavutil/intreadwrite.h" #include "libavutil/dict.h" #include "avformat.h" +#include "avio_internal.h" #include "apetag.h" #include "internal.h" #define APE_TAG_VERSION 2000 #define APE_TAG_FOOTER_BYTES 32 #define APE_TAG_FLAG_CONTAINS_HEADER (1 << 31) +#define APE_TAG_FLAG_CONTAINS_FOOTER (1 << 30) #define APE_TAG_FLAG_IS_HEADER (1 << 29) #define APE_TAG_FLAG_IS_BINARY (1 << 1) @@ -169,3 +171,57 @@ int64_t ff_ape_parse_tag(AVFormatContext *s) return tag_start; } + +int ff_ape_write_tag(AVFormatContext *s) +{ + AVDictionaryEntry *e = NULL; + int64_t start, end; + int size, count = 0; + + if (!s->pb->seekable) + return 0; + + start = avio_tell(s->pb); + + // header + avio_write(s->pb, "APETAGEX", 8); // id + avio_wl32 (s->pb, APE_TAG_VERSION); // version + avio_wl32(s->pb, 0); // reserve space for size + avio_wl32(s->pb, 0); // reserve space for tag count + + // flags + avio_wl32(s->pb, APE_TAG_FLAG_CONTAINS_HEADER | APE_TAG_FLAG_CONTAINS_FOOTER | + APE_TAG_FLAG_IS_HEADER); + ffio_fill(s->pb, 0, 8); // reserved + + while ((e = av_dict_get(s->metadata, "", e, AV_DICT_IGNORE_SUFFIX))) { + int val_len = strlen(e->value); + + avio_wl32(s->pb, val_len); // value length + avio_wl32(s->pb, 0); // item flags + avio_put_str(s->pb, e->key); // key + avio_write(s->pb, e->value, val_len); // value + count++; + } + + size = avio_tell(s->pb) - start; + + // footer + avio_write(s->pb, "APETAGEX", 8); // id + avio_wl32 (s->pb, APE_TAG_VERSION); // version + avio_wl32(s->pb, size); // size + avio_wl32(s->pb, count); // tag count + + // flags + avio_wl32(s->pb, APE_TAG_FLAG_CONTAINS_HEADER | APE_TAG_FLAG_CONTAINS_FOOTER); + ffio_fill(s->pb, 0, 8); // reserved + + // update values in the header + end = avio_tell(s->pb); + avio_seek(s->pb, start + 12, SEEK_SET); + avio_wl32(s->pb, size); + avio_wl32(s->pb, count); + avio_seek(s->pb, end, SEEK_SET); + + return 0; +} diff --git a/libavformat/apetag.h b/libavformat/apetag.h index 279972ff6e..36e3211fc8 100644 --- a/libavformat/apetag.h +++ b/libavformat/apetag.h @@ -32,4 +32,9 @@ */ int64_t ff_ape_parse_tag(AVFormatContext *s); +/** + * Write an APE tag into a file. + */ +int ff_ape_write_tag(AVFormatContext *s); + #endif /* AVFORMAT_APETAG_H */ From 2d2d6a4883479403798f4ed46941d5b365823570 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Tue, 28 May 2013 17:44:18 +0200 Subject: [PATCH 3/3] lavf: add a raw WavPack muxer. --- Changelog | 1 + libavformat/Makefile | 1 + libavformat/allformats.c | 2 +- libavformat/version.h | 4 +- libavformat/wvenc.c | 88 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 libavformat/wvenc.c diff --git a/Changelog b/Changelog index a20d62f864..5466ce1a9d 100644 --- a/Changelog +++ b/Changelog @@ -22,6 +22,7 @@ version 10: - Escape 130 video decoder - support for slice multithreading in libavfilter - VC-1 interlaced B-frame support +- support for WavPack muxing (raw and in Matroska) version 9: diff --git a/libavformat/Makefile b/libavformat/Makefile index 06c6ad8509..fddc647603 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -335,6 +335,7 @@ OBJS-$(CONFIG_WSVQA_DEMUXER) += westwood_vqa.o OBJS-$(CONFIG_WTV_DEMUXER) += wtv.o asfdec.o asf.o asfcrypt.o \ avlanguage.o mpegts.o isom.o OBJS-$(CONFIG_WV_DEMUXER) += wvdec.o wv.o apetag.o img2.o +OBJS-$(CONFIG_WV_MUXER) += wvenc.o wv.o apetag.o OBJS-$(CONFIG_XA_DEMUXER) += xa.o OBJS-$(CONFIG_XMV_DEMUXER) += xmv.o OBJS-$(CONFIG_XWMA_DEMUXER) += xwma.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 6dbf9fda04..142d647a2c 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -247,7 +247,7 @@ void av_register_all(void) REGISTER_DEMUXER (WSAUD, wsaud); REGISTER_DEMUXER (WSVQA, wsvqa); REGISTER_DEMUXER (WTV, wtv); - REGISTER_DEMUXER (WV, wv); + REGISTER_MUXDEMUX(WV, wv); REGISTER_DEMUXER (XA, xa); REGISTER_DEMUXER (XMV, xmv); REGISTER_DEMUXER (XWMA, xwma); diff --git a/libavformat/version.h b/libavformat/version.h index 4ebf0784e9..8e6c76f193 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -30,8 +30,8 @@ #include "libavutil/avutil.h" #define LIBAVFORMAT_VERSION_MAJOR 55 -#define LIBAVFORMAT_VERSION_MINOR 0 -#define LIBAVFORMAT_VERSION_MICRO 1 +#define LIBAVFORMAT_VERSION_MINOR 1 +#define LIBAVFORMAT_VERSION_MICRO 0 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ LIBAVFORMAT_VERSION_MINOR, \ diff --git a/libavformat/wvenc.c b/libavformat/wvenc.c new file mode 100644 index 0000000000..0ce08e884c --- /dev/null +++ b/libavformat/wvenc.c @@ -0,0 +1,88 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/attributes.h" + +#include "apetag.h" +#include "avformat.h" +#include "wv.h" + +typedef struct WvMuxContext { + int64_t samples; +} WvMuxContext; + +static av_cold int wv_write_header(AVFormatContext *ctx) +{ + if (ctx->nb_streams > 1 || + ctx->streams[0]->codec->codec_id != AV_CODEC_ID_WAVPACK) { + av_log(ctx, AV_LOG_ERROR, "This muxer only supports a single WavPack stream.\n"); + return AVERROR(EINVAL); + } + + return 0; +} + +static int wv_write_packet(AVFormatContext *ctx, AVPacket *pkt) +{ + WvMuxContext *s = ctx->priv_data; + WvHeader header; + int ret; + + if (pkt->size < WV_HEADER_SIZE || + (ret = ff_wv_parse_header(&header, pkt->data)) < 0) { + av_log(ctx, AV_LOG_ERROR, "Invalid WavPack packet.\n"); + return AVERROR(EINVAL); + } + s->samples += header.samples; + + avio_write(ctx->pb, pkt->data, pkt->size); + avio_flush(ctx->pb); + + return 0; +} + +static av_cold int wv_write_trailer(AVFormatContext *ctx) +{ + WvMuxContext *s = ctx->priv_data; + + /* update total number of samples in the first block */ + if (ctx->pb->seekable && s->samples && + s->samples < UINT32_MAX) { + int64_t pos = avio_tell(ctx->pb); + avio_seek(ctx->pb, 12, SEEK_SET); + avio_wl32(ctx->pb, s->samples); + avio_seek(ctx->pb, pos, SEEK_SET); + } + + ff_ape_write_tag(ctx); + return 0; +} + +AVOutputFormat ff_wv_muxer = { + .name = "wv", + .long_name = NULL_IF_CONFIG_SMALL("raw WavPack"), + .mime_type = "audio/x-wavpack", + .extensions = "wv", + .priv_data_size = sizeof(WvMuxContext), + .audio_codec = AV_CODEC_ID_WAVPACK, + .video_codec = AV_CODEC_ID_NONE, + .write_header = wv_write_header, + .write_packet = wv_write_packet, + .write_trailer = wv_write_trailer, + .flags = AVFMT_NOTIMESTAMPS, +};