Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH 1/6] doc/general_contents.texi: Add missing ADPCM IMA APC entry
@ 2023-12-26 15:51 Tomas Härdin
  2023-12-26 15:52 ` [FFmpeg-devel] [PATCH 2/6] apc: Read duration from file Tomas Härdin
                   ` (4 more replies)
  0 siblings, 5 replies; 13+ messages in thread
From: Tomas Härdin @ 2023-12-26 15:51 UTC (permalink / raw)
  To: ffmpeg-devel

[-- Attachment #1: Type: text/plain, Size: 179 bytes --]

Rebased and addressed some comments from last time. Also discovered
some issues like the IMA APC codec not being listed. This is for the
Cryo game modding community.

/Tomas

[-- Attachment #2: 0001-doc-general_contents.texi-Add-missing-ADPCM-IMA-APC-.patch --]
[-- Type: text/x-patch, Size: 945 bytes --]

From 490754ea1580d4d02bba83150ca09ca8f960c6fd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <git@haerdin.se>
Date: Tue, 26 Dec 2023 15:03:51 +0100
Subject: [PATCH 1/6] doc/general_contents.texi: Add missing ADPCM IMA APC
 entry

---
 doc/general_contents.texi | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/doc/general_contents.texi b/doc/general_contents.texi
index 62ca4f7cb6..3328753acb 100644
--- a/doc/general_contents.texi
+++ b/doc/general_contents.texi
@@ -1169,6 +1169,8 @@ following image formats are supported:
 @item ADPCM IMA Acorn Replay @tab     @tab  X
 @item ADPCM IMA AMV          @tab  X  @tab  X
     @tab Used in AMV files
+@item ADPCM IMA APC          @tab     @tab  X
+    @tab Codec used in games by Cryo Interactive
 @item ADPCM IMA Cunning Developments  @tab     @tab  X
 @item ADPCM IMA Electronic Arts EACS  @tab     @tab  X
 @item ADPCM IMA Electronic Arts SEAD  @tab     @tab  X
-- 
2.39.2


[-- Attachment #3: Type: text/plain, Size: 251 bytes --]

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

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [FFmpeg-devel] [PATCH 2/6] apc: Read duration from file
  2023-12-26 15:51 [FFmpeg-devel] [PATCH 1/6] doc/general_contents.texi: Add missing ADPCM IMA APC entry Tomas Härdin
@ 2023-12-26 15:52 ` Tomas Härdin
  2023-12-26 15:52 ` [FFmpeg-devel] [PATCH 3/6] Add CRYO APC muxer Tomas Härdin
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 13+ messages in thread
From: Tomas Härdin @ 2023-12-26 15:52 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

[-- Attachment #1: Type: text/plain, Size: 1 bytes --]



[-- Attachment #2: 0002-apc-Read-duration-from-file.patch --]
[-- Type: text/x-patch, Size: 1129 bytes --]

From b3756e6258d18139fb2b67e3d9bc795bafd1b065 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <git@haerdin.se>
Date: Tue, 26 Dec 2023 14:31:59 +0100
Subject: [PATCH 2/6] apc: Read duration from file

---
 libavformat/apc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/libavformat/apc.c b/libavformat/apc.c
index b8b18c966c..7765c53088 100644
--- a/libavformat/apc.c
+++ b/libavformat/apc.c
@@ -24,6 +24,7 @@
 #include "libavutil/channel_layout.h"
 #include "avformat.h"
 #include "demux.h"
+#include "internal.h"
 
 static int apc_probe(const AVProbeData *p)
 {
@@ -51,8 +52,9 @@ static int apc_read_header(AVFormatContext *s)
     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_APC;
 
-    avio_rl32(pb); /* number of samples */
+    st->duration = avio_rl32(pb); /* number of samples */
     st->codecpar->sample_rate = avio_rl32(pb);
+    avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
 
     /* initial predictor values for adpcm decoder */
     if ((ret = ff_get_extradata(s, st->codecpar, pb, 2 * 4)) < 0)
-- 
2.39.2


[-- Attachment #3: Type: text/plain, Size: 251 bytes --]

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

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [FFmpeg-devel] [PATCH 3/6] Add CRYO APC muxer
  2023-12-26 15:51 [FFmpeg-devel] [PATCH 1/6] doc/general_contents.texi: Add missing ADPCM IMA APC entry Tomas Härdin
  2023-12-26 15:52 ` [FFmpeg-devel] [PATCH 2/6] apc: Read duration from file Tomas Härdin
@ 2023-12-26 15:52 ` Tomas Härdin
  2023-12-26 22:30   ` Michael Niedermayer
  2023-12-26 15:53 ` [FFmpeg-devel] [PATCH 4/6] Un-mark IMA APC as intra-only Tomas Härdin
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 13+ messages in thread
From: Tomas Härdin @ 2023-12-26 15:52 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

[-- Attachment #1: Type: text/plain, Size: 1 bytes --]



[-- Attachment #2: 0003-Add-CRYO-APC-muxer.patch --]
[-- Type: text/x-patch, Size: 7335 bytes --]

From 14f3dd40a49ebf5ea020465732511e9d79a2e14a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <git@haerdin.se>
Date: Tue, 26 Dec 2023 14:32:10 +0100
Subject: [PATCH 3/6] Add CRYO APC muxer

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

diff --git a/Changelog b/Changelog
index ca38546262..344bf4d1cf 100644
--- a/Changelog
+++ b/Changelog
@@ -3,6 +3,7 @@ releases are sorted from youngest to oldest.
 
 version <next>:
 - LEAD MCMP decoder
+- CRYO APC muxer
 
 version 6.1:
 - libaribcaption decoder
diff --git a/doc/general_contents.texi b/doc/general_contents.texi
index 3328753acb..a43736e3a8 100644
--- a/doc/general_contents.texi
+++ b/doc/general_contents.texi
@@ -448,7 +448,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 329055ccfd..d88a65d713 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -105,6 +105,7 @@ OBJS-$(CONFIG_AMV_MUXER)                 += amvenc.o
 OBJS-$(CONFIG_ANM_DEMUXER)               += anm.o
 OBJS-$(CONFIG_APAC_DEMUXER)              += apac.o rawdec.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_APM_DEMUXER)               += apm.o
 OBJS-$(CONFIG_APM_MUXER)                 += apm.o rawenc.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index d4b505a5a3..ab74c2028f 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -60,6 +60,7 @@ extern const FFOutputFormat ff_amv_muxer;
 extern const AVInputFormat  ff_anm_demuxer;
 extern const AVInputFormat  ff_apac_demuxer;
 extern const AVInputFormat  ff_apc_demuxer;
+extern const FFOutputFormat ff_apc_muxer;
 extern const AVInputFormat  ff_ape_demuxer;
 extern const AVInputFormat  ff_apm_demuxer;
 extern const FFOutputFormat ff_apm_muxer;
diff --git a/libavformat/apcenc.c b/libavformat/apcenc.c
new file mode 100644
index 0000000000..ba024ead50
--- /dev/null
+++ b/libavformat/apcenc.c
@@ -0,0 +1,125 @@
+/*
+ * CRYO APC audio format muxer
+ * Copyright (c) 2023 Tomas Härdin <git@haerdin.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"
+#include "mux.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->ch_layout.nb_channels <= 0 || par->ch_layout.nb_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->ch_layout.nb_channels - 1);
+    avpriv_set_pts_info(st, 64, 1, par->sample_rate);
+    return 0;
+}
+
+static int apc_write_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    size_t extradata_size = 0;
+    const 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), err;
+        if ((err = avio_seek(s->pb, 20, SEEK_SET)) >= 0) {
+            avio_write(s->pb, extradata, extradata_size);
+            avio_seek(s->pb, pos, SEEK_SET);
+        } else {
+            av_log(s, AV_LOG_ERROR, "Got predictors from encoder but couldn't seek back to write them\n");
+            // fail since we should always be able to do this within the avio cache
+            // unless the encoder gave us predictors way too late for some reason
+            return err;
+        }
+    }
+
+    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->ch_layout.nb_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->ch_layout.nb_channels);
+    }
+    return 0;
+}
+
+const FFOutputFormat ff_apc_muxer = {
+    .p.name         = "apc",
+    .p.long_name    = NULL_IF_CONFIG_SMALL("CRYO APC"),
+    .p.extensions   = "apc",
+    .p.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 2a28a3bf40..6a80f3ac4e 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -31,7 +31,7 @@
 
 #include "version_major.h"
 
-#define LIBAVFORMAT_VERSION_MINOR  17
+#define LIBAVFORMAT_VERSION_MINOR  18
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
-- 
2.39.2


[-- Attachment #3: Type: text/plain, Size: 251 bytes --]

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

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [FFmpeg-devel] [PATCH 4/6] Un-mark IMA APC as intra-only
  2023-12-26 15:51 [FFmpeg-devel] [PATCH 1/6] doc/general_contents.texi: Add missing ADPCM IMA APC entry Tomas Härdin
  2023-12-26 15:52 ` [FFmpeg-devel] [PATCH 2/6] apc: Read duration from file Tomas Härdin
  2023-12-26 15:52 ` [FFmpeg-devel] [PATCH 3/6] Add CRYO APC muxer Tomas Härdin
@ 2023-12-26 15:53 ` Tomas Härdin
  2023-12-26 15:53 ` [FFmpeg-devel] [PATCH 5/6] Add ADPCM IMA CRYO APC encoder Tomas Härdin
  2023-12-26 15:53 ` [FFmpeg-devel] [PATCH 6/6] Add myself as APC maintainer Tomas Härdin
  4 siblings, 0 replies; 13+ messages in thread
From: Tomas Härdin @ 2023-12-26 15:53 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

[-- Attachment #1: Type: text/plain, Size: 1 bytes --]



[-- Attachment #2: 0004-Un-mark-IMA-APC-as-intra-only.patch --]
[-- Type: text/x-patch, Size: 1615 bytes --]

From dd84044af61931988e43bd58c202fe4e7ca7069a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <git@haerdin.se>
Date: Tue, 26 Dec 2023 16:30:35 +0100
Subject: [PATCH 4/6] Un-mark IMA APC as intra-only

Packets must be decoded in order.
There is no way to seek and get a bitexact decode.
---
 libavcodec/codec_desc.c | 2 +-
 libavformat/apc.c       | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 432a9c9ea6..899c082c83 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -2431,7 +2431,7 @@ static const AVCodecDescriptor codec_descriptors[] = {
         .type      = AVMEDIA_TYPE_AUDIO,
         .name      = "adpcm_ima_apc",
         .long_name = NULL_IF_CONFIG_SMALL("ADPCM IMA CRYO APC"),
-        .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
+        .props     = AV_CODEC_PROP_LOSSY,
     },
     {
         .id        = AV_CODEC_ID_ADPCM_VIMA,
diff --git a/libavformat/apc.c b/libavformat/apc.c
index 7765c53088..d1891954a8 100644
--- a/libavformat/apc.c
+++ b/libavformat/apc.c
@@ -75,8 +75,13 @@ static int apc_read_header(AVFormatContext *s)
 
 static int apc_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
+    int first = avio_tell(s->pb) == 32;
     if (av_get_packet(s->pb, pkt, MAX_READ_SIZE) <= 0)
         return AVERROR(EIO);
+    // each IMA APC packet depends on the one before for bitexact decode
+    // extradata is used to initialize the decoder
+    if (first)
+        pkt->flags |= AV_PKT_FLAG_KEY;
     pkt->stream_index = 0;
     return 0;
 }
-- 
2.39.2


[-- Attachment #3: Type: text/plain, Size: 251 bytes --]

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

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [FFmpeg-devel] [PATCH 5/6] Add ADPCM IMA CRYO APC encoder
  2023-12-26 15:51 [FFmpeg-devel] [PATCH 1/6] doc/general_contents.texi: Add missing ADPCM IMA APC entry Tomas Härdin
                   ` (2 preceding siblings ...)
  2023-12-26 15:53 ` [FFmpeg-devel] [PATCH 4/6] Un-mark IMA APC as intra-only Tomas Härdin
@ 2023-12-26 15:53 ` Tomas Härdin
  2023-12-26 15:53 ` [FFmpeg-devel] [PATCH 6/6] Add myself as APC maintainer Tomas Härdin
  4 siblings, 0 replies; 13+ messages in thread
From: Tomas Härdin @ 2023-12-26 15:53 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

[-- Attachment #1: Type: text/plain, Size: 34 bytes --]

Hopefully I got the testing right

[-- Attachment #2: 0005-Add-ADPCM-IMA-CRYO-APC-encoder.patch --]
[-- Type: text/x-patch, Size: 7025 bytes --]

From 047ba1051d5e03b2a54d2aad514dd4501750bc71 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <git@haerdin.se>
Date: Tue, 26 Dec 2023 14:32:20 +0100
Subject: [PATCH 5/6] Add ADPCM IMA CRYO APC encoder

No trellis quantization yet
---
 Changelog                      |  1 +
 doc/general_contents.texi      |  2 +-
 libavcodec/adpcmenc.c          | 33 +++++++++++++++++++++++++++++++++
 libavcodec/allcodecs.c         |  1 +
 libavcodec/version.h           |  2 +-
 tests/fate/acodec.mak          |  2 ++
 tests/ref/acodec/adpcm-ima_apc |  4 ++++
 7 files changed, 43 insertions(+), 2 deletions(-)
 create mode 100644 tests/ref/acodec/adpcm-ima_apc

diff --git a/Changelog b/Changelog
index 344bf4d1cf..38ad342e09 100644
--- a/Changelog
+++ b/Changelog
@@ -4,6 +4,7 @@ releases are sorted from youngest to oldest.
 version <next>:
 - LEAD MCMP decoder
 - CRYO APC muxer
+- ADPCM IMA APC encoder
 
 version 6.1:
 - libaribcaption decoder
diff --git a/doc/general_contents.texi b/doc/general_contents.texi
index a43736e3a8..f02ea89b54 100644
--- a/doc/general_contents.texi
+++ b/doc/general_contents.texi
@@ -1169,7 +1169,7 @@ following image formats are supported:
 @item ADPCM IMA Acorn Replay @tab     @tab  X
 @item ADPCM IMA AMV          @tab  X  @tab  X
     @tab Used in AMV files
-@item ADPCM IMA APC          @tab     @tab  X
+@item ADPCM IMA APC          @tab  X  @tab  X
     @tab Codec used in games by Cryo Interactive
 @item ADPCM IMA Cunning Developments  @tab     @tab  X
 @item ADPCM IMA Electronic Arts EACS  @tab     @tab  X
diff --git a/libavcodec/adpcmenc.c b/libavcodec/adpcmenc.c
index 63afffc58f..47dbec41fa 100644
--- a/libavcodec/adpcmenc.c
+++ b/libavcodec/adpcmenc.c
@@ -74,6 +74,7 @@ typedef struct ADPCMEncodeContext {
     TrellisNode *node_buf;
     TrellisNode **nodep_buf;
     uint8_t *trellis_hash;
+    int extradata_updated;
 } ADPCMEncodeContext;
 
 #define FREEZE_INTERVAL 128
@@ -157,6 +158,15 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx)
             bytestream_put_le16(&extradata, ff_adpcm_AdaptCoeff2[i] * 4);
         }
         ) /* End of CASE */
+    CASE(ADPCM_IMA_APC,
+        if (avctx->trellis) {
+            av_log(avctx, AV_LOG_ERROR, "trellis encoding not implemented for CRYO APC\n");
+            return AVERROR_PATCHWELCOME;
+        }
+        //extradata will be output in adpcm_encode_frame()
+        avctx->frame_size  = s->block_size * 2 / channels;
+        avctx->block_align = s->block_size;
+        ) /* End of CASE */
     CASE(ADPCM_YAMAHA,
         avctx->frame_size  = s->block_size * 2 / channels;
         avctx->block_align = s->block_size;
@@ -622,6 +632,28 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
     dst = avpkt->data;
 
     switch(avctx->codec->id) {
+    CASE(ADPCM_IMA_APC,
+        //initialize predictors using initial samples
+        if (!c->extradata_updated) {
+            uint8_t *side_data = av_packet_new_side_data(
+                avpkt, AV_PKT_DATA_NEW_EXTRADATA, 8);
+
+            if (!side_data) {
+                return AVERROR(ENOMEM);
+            }
+
+            for (int ch = 0; ch < channels; ch++) {
+                c->status[ch].prev_sample = samples[ch];
+                bytestream_put_le32(&side_data, c->status[ch].prev_sample);
+            }
+            c->extradata_updated = 1;
+        }
+        for (int i = 0; i < frame->nb_samples*channels/2; i++) {
+            uint8_t l = adpcm_ima_compress_sample(&c->status[0],  samples[2*i+0]);
+            uint8_t r = adpcm_ima_compress_sample(&c->status[st], samples[2*i+1]);
+            *dst++ = (l<<4) | r;
+        }
+        ) /* End of CASE */
     CASE(ADPCM_IMA_WAV,
         int blocks = (frame->nb_samples - 1) / 8;
 
@@ -1028,6 +1060,7 @@ ADPCM_ENCODER(ADPCM_IMA_QT,  adpcm_ima_qt,  sample_fmts_p, 0,
 ADPCM_ENCODER(ADPCM_IMA_SSI, adpcm_ima_ssi, sample_fmts,   AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Simon & Schuster Interactive")
 ADPCM_ENCODER(ADPCM_IMA_WAV, adpcm_ima_wav, sample_fmts_p, 0,                             "ADPCM IMA WAV")
 ADPCM_ENCODER(ADPCM_IMA_WS,  adpcm_ima_ws,  sample_fmts,   AV_CODEC_CAP_SMALL_LAST_FRAME, "ADPCM IMA Westwood")
+ADPCM_ENCODER(ADPCM_IMA_APC, adpcm_ima_apc, sample_fmts,   0,                             "ADPCM IMA CRYO APC")
 ADPCM_ENCODER(ADPCM_MS,      adpcm_ms,      sample_fmts,   0,                             "ADPCM Microsoft")
 ADPCM_ENCODER(ADPCM_SWF,     adpcm_swf,     sample_fmts,   0,                             "ADPCM Shockwave Flash")
 ADPCM_ENCODER(ADPCM_YAMAHA,  adpcm_yamaha,  sample_fmts,   0,                             "ADPCM Yamaha")
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 2662adb754..33bdd72d27 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -668,6 +668,7 @@ extern const FFCodec ff_adpcm_ima_amv_encoder;
 extern const FFCodec ff_adpcm_ima_alp_decoder;
 extern const FFCodec ff_adpcm_ima_alp_encoder;
 extern const FFCodec ff_adpcm_ima_apc_decoder;
+extern const FFCodec ff_adpcm_ima_apc_encoder;
 extern const FFCodec ff_adpcm_ima_apm_decoder;
 extern const FFCodec ff_adpcm_ima_apm_encoder;
 extern const FFCodec ff_adpcm_ima_cunning_decoder;
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 2a08e42d7e..0ef6c991f3 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -29,7 +29,7 @@
 
 #include "version_major.h"
 
-#define LIBAVCODEC_VERSION_MINOR  33
+#define LIBAVCODEC_VERSION_MINOR  34
 #define LIBAVCODEC_VERSION_MICRO 100
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
diff --git a/tests/fate/acodec.mak b/tests/fate/acodec.mak
index 7b09e3bd63..e7e3ae5ab8 100644
--- a/tests/fate/acodec.mak
+++ b/tests/fate/acodec.mak
@@ -48,6 +48,7 @@ fate-acodec-pcm-f%be: FMT = au
 
 FATE_ACODEC_ADPCM_RESAMPLE-$(call ENCDEC, ADPCM_ADX,  ADX)      += adx
 FATE_ACODEC_ADPCM_RESAMPLE-$(call ENCDEC, ADPCM_ARGO, ARGO_ASF) += argo
+FATE_ACODEC_ADPCM-$(call ENCDEC, ADPCM_IMA_APC, APC)      += ima_apc
 FATE_ACODEC_ADPCM-$(call ENCDEC, ADPCM_IMA_APM, APM)      += ima_apm
 FATE_ACODEC_ADPCM-$(call ENCDEC, ADPCM_IMA_ALP, ALP)      += ima_alp
 FATE_ACODEC_ADPCM_RESAMPLE-$(call ENCDEC, ADPCM_IMA_QT,  AIFF)  += ima_qt
@@ -69,6 +70,7 @@ fate-acodec-adpcm-%: CODEC = adpcm_$(@:fate-acodec-adpcm-%=%)
 
 fate-acodec-adpcm-adx:     FMT = adx
 fate-acodec-adpcm-argo:    FMT = argo_asf
+fate-acodec-adpcm-ima_apc: FMT = apc
 fate-acodec-adpcm-ima_apm: FMT = apm
 fate-acodec-adpcm-ima_qt:  FMT = aiff
 fate-acodec-adpcm-ima_ssi: FMT = kvag
diff --git a/tests/ref/acodec/adpcm-ima_apc b/tests/ref/acodec/adpcm-ima_apc
new file mode 100644
index 0000000000..f168734c78
--- /dev/null
+++ b/tests/ref/acodec/adpcm-ima_apc
@@ -0,0 +1,4 @@
+45aca515c679bb0c315df766432d5630 *tests/data/fate/acodec-adpcm-ima_apc.apc
+265248 tests/data/fate/acodec-adpcm-ima_apc.apc
+03fc41cf61b7a160359147cd6363562a *tests/data/fate/acodec-adpcm-ima_apc.out.wav
+stddev:  904.04 PSNR: 37.21 MAXDIFF:34026 bytes:  1058400/  1060864
-- 
2.39.2


[-- Attachment #3: Type: text/plain, Size: 251 bytes --]

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

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [FFmpeg-devel] [PATCH 6/6] Add myself as APC maintainer
  2023-12-26 15:51 [FFmpeg-devel] [PATCH 1/6] doc/general_contents.texi: Add missing ADPCM IMA APC entry Tomas Härdin
                   ` (3 preceding siblings ...)
  2023-12-26 15:53 ` [FFmpeg-devel] [PATCH 5/6] Add ADPCM IMA CRYO APC encoder Tomas Härdin
@ 2023-12-26 15:53 ` Tomas Härdin
  4 siblings, 0 replies; 13+ messages in thread
From: Tomas Härdin @ 2023-12-26 15:53 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

[-- Attachment #1: Type: text/plain, Size: 1 bytes --]



[-- Attachment #2: 0006-Add-myself-as-APC-maintainer.patch --]
[-- Type: text/x-patch, Size: 845 bytes --]

From 616ab30780e72beb3f07ef0cb2f26ac1c8181524 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <git@haerdin.se>
Date: Tue, 26 Dec 2023 16:45:33 +0100
Subject: [PATCH 6/6] Add myself as APC maintainer

---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 3430e1722b..da111e72fb 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -391,6 +391,7 @@ Muxers/Demuxers:
   aiffenc.c                             Baptiste Coudurier, Matthieu Bouron
   alp.c                                 Zane van Iperen
   amvenc.c                              Zane van Iperen
+  apc*                                  Tomas Härdin
   apm.c                                 Zane van Iperen
   apngdec.c                             Benoit Fouet
   argo_asf.c                            Zane van Iperen
-- 
2.39.2


[-- Attachment #3: Type: text/plain, Size: 251 bytes --]

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

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [FFmpeg-devel] [PATCH 3/6] Add CRYO APC muxer
  2023-12-26 15:52 ` [FFmpeg-devel] [PATCH 3/6] Add CRYO APC muxer Tomas Härdin
@ 2023-12-26 22:30   ` Michael Niedermayer
  2023-12-28 10:31     ` Tomas Härdin
  2023-12-28 17:23     ` Tomas Härdin
  0 siblings, 2 replies; 13+ messages in thread
From: Michael Niedermayer @ 2023-12-26 22:30 UTC (permalink / raw)
  To: FFmpeg development discussions and patches


[-- Attachment #1.1: Type: text/plain, Size: 4074 bytes --]

Hi

On Tue, Dec 26, 2023 at 04:52:47PM +0100, Tomas Härdin wrote:
> 

[...]
> +
> +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->ch_layout.nb_channels <= 0 || par->ch_layout.nb_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

please add to the comment "updated in apc_write_trailer()"


> +    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->ch_layout.nb_channels - 1);
> +    avpriv_set_pts_info(st, 64, 1, par->sample_rate);
> +    return 0;
> +}
> +

> +static int apc_write_packet(AVFormatContext *s, AVPacket *pkt)
> +{
> +    size_t extradata_size = 0;

> +    const 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), err;
> +        if ((err = avio_seek(s->pb, 20, SEEK_SET)) >= 0) {
> +            avio_write(s->pb, extradata, extradata_size);
> +            avio_seek(s->pb, pos, SEEK_SET);
> +        } else {
> +            av_log(s, AV_LOG_ERROR, "Got predictors from encoder but couldn't seek back to write them\n");
> +            // fail since we should always be able to do this within the avio cache
> +            // unless the encoder gave us predictors way too late for some reason
> +            return err;
> +        }
> +    }

I think the encoder should buffer data or use 2 passes
if it needs future data. seeking back in the muxer is a bit odd.

if it becomes available always in teh first packet then maybe the
whole header could be written in the first packet

I would suggest, you add APC support to nut.
Thats a good test to ensure the packet and extradata is set at the right time
for a generic muxer



> +
> +    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->ch_layout.nb_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->ch_layout.nb_channels);

I think something like this is slightly cleaner:


int64_t number_of_samples = (file_size - APC_HEADER_SIZE) *
                            2 / s->streams[0]->codecpar->ch_layout.nb_channels;

if (number_of_samples > UINT32_MAX)
    ...
[...]

thx

--
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Avoid a single point of failure, be that a person or equipment.

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

[-- Attachment #2: Type: text/plain, Size: 251 bytes --]

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

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [FFmpeg-devel] [PATCH 3/6] Add CRYO APC muxer
  2023-12-26 22:30   ` Michael Niedermayer
@ 2023-12-28 10:31     ` Tomas Härdin
  2023-12-29 17:33       ` Michael Niedermayer
  2023-12-28 17:23     ` Tomas Härdin
  1 sibling, 1 reply; 13+ messages in thread
From: Tomas Härdin @ 2023-12-28 10:31 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

tis 2023-12-26 klockan 23:30 +0100 skrev Michael Niedermayer:
> Hi
> 
> On Tue, Dec 26, 2023 at 04:52:47PM +0100, Tomas Härdin wrote:
> > 
> 
> [...]
> > +
> > +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->ch_layout.nb_channels <= 0 || par-
> > >ch_layout.nb_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
> 
> please add to the comment "updated in apc_write_trailer()"

Sure


> > +static int apc_write_packet(AVFormatContext *s, AVPacket *pkt)
> > +{
> > +    size_t extradata_size = 0;
> 
> > +    const 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), err;
> > +        if ((err = avio_seek(s->pb, 20, SEEK_SET)) >= 0) {
> > +            avio_write(s->pb, extradata, extradata_size);
> > +            avio_seek(s->pb, pos, SEEK_SET);
> > +        } else {
> > +            av_log(s, AV_LOG_ERROR, "Got predictors from encoder
> > but couldn't seek back to write them\n");
> > +            // fail since we should always be able to do this
> > within the avio cache
> > +            // unless the encoder gave us predictors way too late
> > for some reason
> > +            return err;
> > +        }
> > +    }
> 
> I think the encoder should buffer data or use 2 passes
> if it needs future data. seeking back in the muxer is a bit odd.
> 
> if it becomes available always in teh first packet then maybe the
> whole header could be written in the first packet

Good idea. IIRC the issue was that extradata is either given
immediately (remuxing) or after a single-packet delay (encoding), hence
the check for *new* metadata

> I would suggest, you add APC support to nut.
> Thats a good test to ensure the packet and extradata is set at the
> right time
> for a generic muxer

How would I go about doing that?

> > +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-
> > >ch_layout.nb_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-
> > >ch_layout.nb_channels);
> 
> I think something like this is slightly cleaner:
> 
> 
> int64_t number_of_samples = (file_size - APC_HEADER_SIZE) *
>                             2 / s->streams[0]->codecpar-
> >ch_layout.nb_channels;

This will UB when file_size is a bit above INT64_MAX / 2. But yeah it
can probably be made prettier. I'll have a think about it

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

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [FFmpeg-devel] [PATCH 3/6] Add CRYO APC muxer
  2023-12-26 22:30   ` Michael Niedermayer
  2023-12-28 10:31     ` Tomas Härdin
@ 2023-12-28 17:23     ` Tomas Härdin
  2023-12-28 17:29       ` Tomas Härdin
  1 sibling, 1 reply; 13+ messages in thread
From: Tomas Härdin @ 2023-12-28 17:23 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

[-- Attachment #1: Type: text/plain, Size: 139 bytes --]

Updated patch attached. I reordered the patch series a bit also, will
post the entire series once this patch looks good enough

/Tomas

[-- Attachment #2: 0004-lavf-Add-CRYO-APC-muxer.patch --]
[-- Type: text/x-patch, Size: 7152 bytes --]

From ab94ae3520923ceb305055a0e568f3293e0e0c54 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <git@haerdin.se>
Date: Tue, 26 Dec 2023 14:32:10 +0100
Subject: [PATCH 4/6] lavf: Add CRYO APC muxer

---
 Changelog                 |   1 +
 doc/general_contents.texi |   2 +-
 libavformat/Makefile      |   1 +
 libavformat/allformats.c  |   1 +
 libavformat/apcenc.c      | 124 ++++++++++++++++++++++++++++++++++++++
 libavformat/version.h     |   2 +-
 6 files changed, 129 insertions(+), 2 deletions(-)
 create mode 100644 libavformat/apcenc.c

diff --git a/Changelog b/Changelog
index a638c03250..31c9e25be6 100644
--- a/Changelog
+++ b/Changelog
@@ -13,6 +13,7 @@ version <next>:
 - IAMF raw demuxer and muxer
 - D3D12VA hardware accelerated H264, HEVC, VP9, AV1, MPEG-2 and VC1 decoding
 - tiltandshift filter
+- CRYO APC muxer
 
 version 6.1:
 - libaribcaption decoder
diff --git a/doc/general_contents.texi b/doc/general_contents.texi
index ec7516dce8..df31edd060 100644
--- a/doc/general_contents.texi
+++ b/doc/general_contents.texi
@@ -464,7 +464,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 581e378d95..f605f4ae69 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -105,6 +105,7 @@ OBJS-$(CONFIG_AMV_MUXER)                 += amvenc.o
 OBJS-$(CONFIG_ANM_DEMUXER)               += anm.o
 OBJS-$(CONFIG_APAC_DEMUXER)              += apac.o rawdec.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_APM_DEMUXER)               += apm.o
 OBJS-$(CONFIG_APM_MUXER)                 += apm.o rawenc.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index ce6be5f04d..18a33d7448 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -60,6 +60,7 @@ extern const FFOutputFormat ff_amv_muxer;
 extern const AVInputFormat  ff_anm_demuxer;
 extern const AVInputFormat  ff_apac_demuxer;
 extern const AVInputFormat  ff_apc_demuxer;
+extern const FFOutputFormat ff_apc_muxer;
 extern const AVInputFormat  ff_ape_demuxer;
 extern const AVInputFormat  ff_apm_demuxer;
 extern const FFOutputFormat ff_apm_muxer;
diff --git a/libavformat/apcenc.c b/libavformat/apcenc.c
new file mode 100644
index 0000000000..175c6c6cdd
--- /dev/null
+++ b/libavformat/apcenc.c
@@ -0,0 +1,124 @@
+/*
+ * CRYO APC audio format muxer
+ * Copyright (c) 2023 Tomas Härdin <git@haerdin.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"
+#include "mux.h"
+
+#define APC_HEADER_SIZE (8*4)
+
+static int apc_write_header(AVFormatContext *s)
+{
+    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->ch_layout.nb_channels <= 0 || par->ch_layout.nb_channels > 2) {
+        av_log(s, AV_LOG_ERROR, "Must be mono or stereo\n");
+        return AVERROR(EINVAL);
+    }
+
+    avpriv_set_pts_info(st, 64, 1, par->sample_rate);
+    // delay writing the actual header until we get extradata from the encoder
+    return 0;
+}
+
+static int apc_write_header_delayed(AVFormatContext *s, AVPacket *pkt)
+{
+    AVIOContext *pb = s->pb;
+    AVCodecParameters *par = s->streams[0]->codecpar;
+    size_t new_extradata_size = 0, extradata_size;
+    const uint8_t *new_extradata = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, &new_extradata_size), *extradata;
+
+    // prefer updated extradata from the encoder
+    if (new_extradata) {
+        extradata_size = new_extradata_size;
+        extradata = new_extradata;
+    } else {
+        extradata_size = par->extradata_size;
+        extradata = par->extradata;
+    }
+
+    if (extradata_size != 8) {
+        av_log(s, AV_LOG_ERROR, "Must have exactly 8 bytes of extradata, got %zu\n", 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. updated in apc_write_trailer()
+    avio_wl32(pb, par->sample_rate);
+    avio_write(pb, extradata, extradata_size);
+    avio_wl32(pb, par->ch_layout.nb_channels - 1);
+
+    return 0;
+}
+
+static int apc_write_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    if (avio_tell(s->pb) == 0) {
+        // first packet - write header
+        int ret;
+        if ((ret = apc_write_header_delayed(s, pkt)) < 0)
+            return ret;
+    }
+
+    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) {
+        int64_t data_size = file_size -= APC_HEADER_SIZE;
+        int channels = s->streams[0]->codecpar->ch_layout.nb_channels;
+
+        if (data_size / channels > UINT32_MAX / 2) {
+            av_log(s, AV_LOG_ERROR, "File too large\n");
+            return AVERROR(EINVAL);
+        }
+
+        avio_wl32(s->pb, data_size * 2 / channels);
+    }
+    return 0;
+}
+
+const FFOutputFormat ff_apc_muxer = {
+    .p.name         = "apc",
+    .p.long_name    = NULL_IF_CONFIG_SMALL("CRYO APC"),
+    .p.extensions   = "apc",
+    .p.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 de9cc8e31d..683184d5da 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -31,7 +31,7 @@
 
 #include "version_major.h"
 
-#define LIBAVFORMAT_VERSION_MINOR  20
+#define LIBAVFORMAT_VERSION_MINOR  21
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
-- 
2.39.2


[-- Attachment #3: Type: text/plain, Size: 251 bytes --]

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

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [FFmpeg-devel] [PATCH 3/6] Add CRYO APC muxer
  2023-12-28 17:23     ` Tomas Härdin
@ 2023-12-28 17:29       ` Tomas Härdin
  0 siblings, 0 replies; 13+ messages in thread
From: Tomas Härdin @ 2023-12-28 17:29 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

tor 2023-12-28 klockan 18:23 +0100 skrev Tomas Härdin:
> +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) {
> +        int64_t data_size = file_size -= APC_HEADER_SIZE;

I realized a small bug right after posting: data_size can become
negative if the file is closed immediately after opening. Will fix this
when reposting the series

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

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [FFmpeg-devel] [PATCH 3/6] Add CRYO APC muxer
  2023-12-28 10:31     ` Tomas Härdin
@ 2023-12-29 17:33       ` Michael Niedermayer
  2023-12-30 17:01         ` Tomas Härdin
  0 siblings, 1 reply; 13+ messages in thread
From: Michael Niedermayer @ 2023-12-29 17:33 UTC (permalink / raw)
  To: FFmpeg development discussions and patches


[-- Attachment #1.1: Type: text/plain, Size: 3545 bytes --]

On Thu, Dec 28, 2023 at 11:31:05AM +0100, Tomas Härdin wrote:
> tis 2023-12-26 klockan 23:30 +0100 skrev Michael Niedermayer:
> > Hi
> > 
> > On Tue, Dec 26, 2023 at 04:52:47PM +0100, Tomas Härdin wrote:
> > > 
> > 
> > [...]
> > > +
> > > +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->ch_layout.nb_channels <= 0 || par-
> > > >ch_layout.nb_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
> > 
> > please add to the comment "updated in apc_write_trailer()"
> 
> Sure
> 
> 
> > > +static int apc_write_packet(AVFormatContext *s, AVPacket *pkt)
> > > +{
> > > +    size_t extradata_size = 0;
> > 
> > > +    const 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), err;
> > > +        if ((err = avio_seek(s->pb, 20, SEEK_SET)) >= 0) {
> > > +            avio_write(s->pb, extradata, extradata_size);
> > > +            avio_seek(s->pb, pos, SEEK_SET);
> > > +        } else {
> > > +            av_log(s, AV_LOG_ERROR, "Got predictors from encoder
> > > but couldn't seek back to write them\n");
> > > +            // fail since we should always be able to do this
> > > within the avio cache
> > > +            // unless the encoder gave us predictors way too late
> > > for some reason
> > > +            return err;
> > > +        }
> > > +    }
> > 
> > I think the encoder should buffer data or use 2 passes
> > if it needs future data. seeking back in the muxer is a bit odd.
> > 
> > if it becomes available always in teh first packet then maybe the
> > whole header could be written in the first packet
> 
> Good idea. IIRC the issue was that extradata is either given
> immediately (remuxing) or after a single-packet delay (encoding), hence
> the check for *new* metadata
> 
> > I would suggest, you add APC support to nut.
> > Thats a good test to ensure the packet and extradata is set at the
> > right time
> > for a generic muxer
> 
> How would I go about doing that?

nut mainly needs to be able to map the ffmpeg API CODEC_ID to some external id
so the codec id needs to be in one of the table nut scans for it

thx

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Its not that you shouldnt use gotos but rather that you should write
readable code and code with gotos often but not always is less readable

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

[-- Attachment #2: Type: text/plain, Size: 251 bytes --]

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

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [FFmpeg-devel] [PATCH 3/6] Add CRYO APC muxer
  2023-12-29 17:33       ` Michael Niedermayer
@ 2023-12-30 17:01         ` Tomas Härdin
  2023-12-31 21:36           ` Michael Niedermayer
  0 siblings, 1 reply; 13+ messages in thread
From: Tomas Härdin @ 2023-12-30 17:01 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

fre 2023-12-29 klockan 18:33 +0100 skrev Michael Niedermayer:
> On Thu, Dec 28, 2023 at 11:31:05AM +0100, Tomas Härdin wrote:
> > tis 2023-12-26 klockan 23:30 +0100 skrev Michael Niedermayer:
> > > Hi
> > > 
> > > On Tue, Dec 26, 2023 at 04:52:47PM +0100, Tomas Härdin wrote:
> > > > 
> > > 
> > > [...]
> > > > +
> > > > +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->ch_layout.nb_channels <= 0 || par-
> > > > > ch_layout.nb_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
> > > 
> > > please add to the comment "updated in apc_write_trailer()"
> > 
> > Sure
> > 
> > 
> > > > +static int apc_write_packet(AVFormatContext *s, AVPacket *pkt)
> > > > +{
> > > > +    size_t extradata_size = 0;
> > > 
> > > > +    const 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), err;
> > > > +        if ((err = avio_seek(s->pb, 20, SEEK_SET)) >= 0) {
> > > > +            avio_write(s->pb, extradata, extradata_size);
> > > > +            avio_seek(s->pb, pos, SEEK_SET);
> > > > +        } else {
> > > > +            av_log(s, AV_LOG_ERROR, "Got predictors from
> > > > encoder
> > > > but couldn't seek back to write them\n");
> > > > +            // fail since we should always be able to do this
> > > > within the avio cache
> > > > +            // unless the encoder gave us predictors way too
> > > > late
> > > > for some reason
> > > > +            return err;
> > > > +        }
> > > > +    }
> > > 
> > > I think the encoder should buffer data or use 2 passes
> > > if it needs future data. seeking back in the muxer is a bit odd.
> > > 
> > > if it becomes available always in teh first packet then maybe the
> > > whole header could be written in the first packet
> > 
> > Good idea. IIRC the issue was that extradata is either given
> > immediately (remuxing) or after a single-packet delay (encoding),
> > hence
> > the check for *new* metadata
> > 
> > > I would suggest, you add APC support to nut.
> > > Thats a good test to ensure the packet and extradata is set at
> > > the
> > > right time
> > > for a generic muxer
> > 
> > How would I go about doing that?
> 
> nut mainly needs to be able to map the ffmpeg API CODEC_ID to some
> external id
> so the codec id needs to be in one of the table nut scans for it

I doubt this codec has an official RIFF TwoCC. I see some other IMA
codecs in  riff.c though. I suspect this would require coordinating
with Microsoft, which I feel shouldn't hold up this patchset

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

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [FFmpeg-devel] [PATCH 3/6] Add CRYO APC muxer
  2023-12-30 17:01         ` Tomas Härdin
@ 2023-12-31 21:36           ` Michael Niedermayer
  0 siblings, 0 replies; 13+ messages in thread
From: Michael Niedermayer @ 2023-12-31 21:36 UTC (permalink / raw)
  To: FFmpeg development discussions and patches


[-- Attachment #1.1: Type: text/plain, Size: 4486 bytes --]

On Sat, Dec 30, 2023 at 06:01:25PM +0100, Tomas Härdin wrote:
> fre 2023-12-29 klockan 18:33 +0100 skrev Michael Niedermayer:
> > On Thu, Dec 28, 2023 at 11:31:05AM +0100, Tomas Härdin wrote:
> > > tis 2023-12-26 klockan 23:30 +0100 skrev Michael Niedermayer:
> > > > Hi
> > > > 
> > > > On Tue, Dec 26, 2023 at 04:52:47PM +0100, Tomas Härdin wrote:
> > > > > 
> > > > 
> > > > [...]
> > > > > +
> > > > > +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->ch_layout.nb_channels <= 0 || par-
> > > > > > ch_layout.nb_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
> > > > 
> > > > please add to the comment "updated in apc_write_trailer()"
> > > 
> > > Sure
> > > 
> > > 
> > > > > +static int apc_write_packet(AVFormatContext *s, AVPacket *pkt)
> > > > > +{
> > > > > +    size_t extradata_size = 0;
> > > > 
> > > > > +    const 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), err;
> > > > > +        if ((err = avio_seek(s->pb, 20, SEEK_SET)) >= 0) {
> > > > > +            avio_write(s->pb, extradata, extradata_size);
> > > > > +            avio_seek(s->pb, pos, SEEK_SET);
> > > > > +        } else {
> > > > > +            av_log(s, AV_LOG_ERROR, "Got predictors from
> > > > > encoder
> > > > > but couldn't seek back to write them\n");
> > > > > +            // fail since we should always be able to do this
> > > > > within the avio cache
> > > > > +            // unless the encoder gave us predictors way too
> > > > > late
> > > > > for some reason
> > > > > +            return err;
> > > > > +        }
> > > > > +    }
> > > > 
> > > > I think the encoder should buffer data or use 2 passes
> > > > if it needs future data. seeking back in the muxer is a bit odd.
> > > > 
> > > > if it becomes available always in teh first packet then maybe the
> > > > whole header could be written in the first packet
> > > 
> > > Good idea. IIRC the issue was that extradata is either given
> > > immediately (remuxing) or after a single-packet delay (encoding),
> > > hence
> > > the check for *new* metadata
> > > 
> > > > I would suggest, you add APC support to nut.
> > > > Thats a good test to ensure the packet and extradata is set at
> > > > the
> > > > right time
> > > > for a generic muxer
> > > 
> > > How would I go about doing that?
> > 
> > nut mainly needs to be able to map the ffmpeg API CODEC_ID to some
> > external id
> > so the codec id needs to be in one of the table nut scans for it
> 
> I doubt this codec has an official RIFF TwoCC. I see some other IMA
> codecs in  riff.c though. I suspect this would require coordinating
> with Microsoft, which I feel shouldn't hold up this patchset

you only have to add it to:
https://git.ffmpeg.org/gitweb/nut.git/blob/HEAD:/docs/nut4cc.txt
https://lists.mplayerhq.hu/mailman/listinfo/nut-devel
and please CC me in case this ML doesnt work (it seems a little silent)

thx

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Everything should be made as simple as possible, but not simpler.
-- Albert Einstein

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

[-- Attachment #2: Type: text/plain, Size: 251 bytes --]

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

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2023-12-31 21:37 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-26 15:51 [FFmpeg-devel] [PATCH 1/6] doc/general_contents.texi: Add missing ADPCM IMA APC entry Tomas Härdin
2023-12-26 15:52 ` [FFmpeg-devel] [PATCH 2/6] apc: Read duration from file Tomas Härdin
2023-12-26 15:52 ` [FFmpeg-devel] [PATCH 3/6] Add CRYO APC muxer Tomas Härdin
2023-12-26 22:30   ` Michael Niedermayer
2023-12-28 10:31     ` Tomas Härdin
2023-12-29 17:33       ` Michael Niedermayer
2023-12-30 17:01         ` Tomas Härdin
2023-12-31 21:36           ` Michael Niedermayer
2023-12-28 17:23     ` Tomas Härdin
2023-12-28 17:29       ` Tomas Härdin
2023-12-26 15:53 ` [FFmpeg-devel] [PATCH 4/6] Un-mark IMA APC as intra-only Tomas Härdin
2023-12-26 15:53 ` [FFmpeg-devel] [PATCH 5/6] Add ADPCM IMA CRYO APC encoder Tomas Härdin
2023-12-26 15:53 ` [FFmpeg-devel] [PATCH 6/6] Add myself as APC maintainer Tomas Härdin

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git