---
 Changelog                |    1 +
 libavformat/Makefile     |    1 +
 libavformat/allformats.c |    2 +-
 libavformat/apetag.c     |   56 +++++++++++++++++++++++++++++
 libavformat/apetag.h     |    5 +++
 libavformat/version.h    |    4 +--
 libavformat/wvenc.c      |   88 ++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 154 insertions(+), 3 deletions(-)
 create mode 100644 libavformat/wvenc.c

diff --git a/Changelog b/Changelog
index b8fbca2..24160a3 100644
--- a/Changelog
+++ b/Changelog
@@ -21,6 +21,7 @@ version 10:
 - Apple Intermediate Codec decoder
 - Escape 130 video decoder
 - support for slice multithreading in libavfilter
+- support for WavPack muxing (raw and in Matroska)
 
 
 version 9:
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 06c6ad8..fddc647 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 6dbf9fd..142d647 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/apetag.c b/libavformat/apetag.c
index 0d2cb97..68f2df6 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 279972f..36e3211 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 */
diff --git a/libavformat/version.h b/libavformat/version.h
index 4ebf078..8e6c76f 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 0000000..0ce08e8
--- /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,
+};
-- 
1.7.10.4

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to