From f08c1554f30839c7d42a9c5e677619f2c8b11c11 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <tjop...@acc.umu.se>
Date: Thu, 22 Nov 2018 22:28:30 +0100
Subject: [PATCH 2/3] Add CRYO APC muxer

---
 Changelog                |   1 +
 doc/general.texi         |   2 +-
 libavformat/Makefile     |   1 +
 libavformat/allformats.c |   1 +
 libavformat/apcenc.c     | 119 +++++++++++++++++++++++++++++++++++++++++++++++
 libavformat/version.h    |   4 +-
 6 files changed, 125 insertions(+), 3 deletions(-)
 create mode 100644 libavformat/apcenc.c

diff --git a/Changelog b/Changelog
index 1f53ff46da..f678feed65 100644
--- a/Changelog
+++ b/Changelog
@@ -10,6 +10,7 @@ version <next>:
 - truehd_core bitstream filter
 - dhav demuxer
 - PCM-DVD encoder
+- CRYO APC muxer
 
 
 version 4.1:
diff --git a/doc/general.texi b/doc/general.texi
index d8832407bb..72da4019cb 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -400,7 +400,7 @@ library:
 @item CRC testing format        @tab X @tab
 @item Creative Voice            @tab X @tab X
     @tab Created for the Sound Blaster Pro.
-@item CRYO APC                  @tab   @tab X
+@item CRYO APC                  @tab X @tab X
     @tab Audio format used in some games by CRYO Interactive Entertainment.
 @item D-Cinema audio            @tab X @tab X
 @item Deluxe Paint Animation    @tab   @tab X
diff --git a/libavformat/Makefile b/libavformat/Makefile
index d0d621de07..1ac8c20cca 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -91,6 +91,7 @@ OBJS-$(CONFIG_AMRNB_DEMUXER)             += amr.o
 OBJS-$(CONFIG_AMRWB_DEMUXER)             += amr.o
 OBJS-$(CONFIG_ANM_DEMUXER)               += anm.o
 OBJS-$(CONFIG_APC_DEMUXER)               += apc.o
+OBJS-$(CONFIG_APC_MUXER)                 += apcenc.o
 OBJS-$(CONFIG_APE_DEMUXER)               += ape.o apetag.o img2.o
 OBJS-$(CONFIG_APNG_DEMUXER)              += apngdec.o
 OBJS-$(CONFIG_APNG_MUXER)                += apngenc.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 5fb5bf17c6..3bc1be430a 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -52,6 +52,7 @@ extern AVInputFormat  ff_amrnb_demuxer;
 extern AVInputFormat  ff_amrwb_demuxer;
 extern AVInputFormat  ff_anm_demuxer;
 extern AVInputFormat  ff_apc_demuxer;
+extern AVOutputFormat ff_apc_muxer;
 extern AVInputFormat  ff_ape_demuxer;
 extern AVInputFormat  ff_apng_demuxer;
 extern AVOutputFormat ff_apng_muxer;
diff --git a/libavformat/apcenc.c b/libavformat/apcenc.c
new file mode 100644
index 0000000000..dba1b3b90c
--- /dev/null
+++ b/libavformat/apcenc.c
@@ -0,0 +1,119 @@
+/*
+ * CRYO APC audio format muxer
+ * Copyright (c) 2018 Tomas Härdin <tjop...@acc.umu.se>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg 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.
+ *
+ * FFmpeg 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "avio_internal.h"
+#include "internal.h"
+
+#define APC_HEADER_SIZE (8*4)
+
+static int apc_write_header(AVFormatContext *s)
+{
+    AVIOContext *pb = s->pb;
+    AVCodecParameters *par;
+    AVStream *st;
+
+    if (s->nb_streams != 1) {
+        av_log(s, AV_LOG_ERROR, "Must have exactly one stream\n");
+        return AVERROR(EINVAL);
+    }
+
+    st = s->streams[0];
+    par = st->codecpar;
+
+    if (par->channels <= 0 || par->channels > 2) {
+        av_log(s, AV_LOG_ERROR, "Must be mono or stereo\n");
+        return AVERROR(EINVAL);
+    }
+
+    if (par->extradata_size != 0 && par->extradata_size != 8) {
+        av_log(s, AV_LOG_ERROR,
+            "Must have exactly 0 or 8 bytes of extradata, got %i\n",
+            par->extradata_size);
+        return AVERROR(EINVAL);
+    }
+
+    ffio_wfourcc(pb, "CRYO");
+    ffio_wfourcc(pb, "_APC");
+    ffio_wfourcc(pb, "1.20");
+    avio_wl32(pb, 0); //number or samples
+    avio_wl32(pb, par->sample_rate);
+
+    //write extradata if we have it (remuxing)
+    //else write dummy values and wait for AV_PKT_DATA_NEW_EXTRADATA (encoding)
+    if (par->extradata_size) {
+        avio_write(pb, par->extradata, par->extradata_size);
+    } else {
+        avio_wl64(pb, 0);
+    }
+
+    avio_wl32(pb, par->channels-1);
+    avpriv_set_pts_info(st, 64, 1, par->sample_rate);
+    return 0;
+}
+
+static int apc_write_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    int extradata_size = 0;
+    uint8_t *extradata = av_packet_get_side_data(
+            pkt, AV_PKT_DATA_NEW_EXTRADATA, &extradata_size);
+
+    if (extradata_size == 8) {
+        //we got predictors from encoder
+        //try to seek back end write them
+        int64_t pos = avio_tell(s->pb);
+        if (avio_seek(s->pb, 20, SEEK_SET) > 0) {
+            avio_write(s->pb, extradata, extradata_size);
+            avio_seek(s->pb, pos, SEEK_SET);
+        }
+    }
+
+    avio_write(s->pb, pkt->data, pkt->size);
+    return 0;
+}
+
+static int apc_write_trailer(AVFormatContext *s)
+{
+    int64_t file_size = avio_tell(s->pb);
+
+    //write length, if we're able to seek back
+    if (avio_seek(s->pb, 12, SEEK_SET) > 0) {
+        if (file_size - APC_HEADER_SIZE >
+                UINT32_MAX * s->streams[0]->codecpar->channels / 2) {
+            av_log(s, AV_LOG_ERROR, "File too large\n");
+            return AVERROR(EINVAL);
+        }
+
+        avio_wl32(s->pb, (file_size - APC_HEADER_SIZE) *
+                            2 / s->streams[0]->codecpar->channels);
+    }
+    return 0;
+}
+
+AVOutputFormat ff_apc_muxer = {
+    .name           = "apc",
+    .long_name      = NULL_IF_CONFIG_SMALL("CRYO APC"),
+    .extensions     = "apc",
+    .audio_codec    = AV_CODEC_ID_ADPCM_IMA_APC,
+    .write_header   = apc_write_header,
+    .write_packet   = apc_write_packet,
+    .write_trailer  = apc_write_trailer,
+};
+
diff --git a/libavformat/version.h b/libavformat/version.h
index 30f2a84ddb..391ebed462 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -32,8 +32,8 @@
 // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium)
 // Also please add any ticket numbers that you believe might be affected here
 #define LIBAVFORMAT_VERSION_MAJOR  58
-#define LIBAVFORMAT_VERSION_MINOR  23
-#define LIBAVFORMAT_VERSION_MICRO 102
+#define LIBAVFORMAT_VERSION_MINOR  24
+#define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \
-- 
2.11.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel

Reply via email to