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] avcodec/vima: support IMA4 (ADPCM QT) variant (PR #20253)
@ 2025-08-16  8:59 Manuel Lauss via ffmpeg-devel
  0 siblings, 0 replies; only message in thread
From: Manuel Lauss via ffmpeg-devel @ 2025-08-16  8:59 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Manuel Lauss

PR #20253 opened by Manuel Lauss (mlauss2)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20253
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20253.patch

Some versions of LucasArts SANM Audio chunks have a special "IMA4" tag in their VIMA chunks, to indicate that
a different coding scheme, in this case IMA ADPCM QT, is used.
- adpcm: export the ff_adpcm_ima_qt_expand_nibble() function
- vima: recognize the IMA4 tag


From c90ac9a8f94cbae9085c0a429f0e8ac95b687a25 Mon Sep 17 00:00:00 2001
From: Manuel Lauss <manuel.lauss@gmail.com>
Date: Sat, 16 Aug 2025 10:24:34 +0200
Subject: [PATCH 1/2] avcodec/adpcm: export ff_adpcm_ima_qt_expand_nibble

For use in the LucasArts VIMA decoder, where it is used as a subvariant.
---
 libavcodec/adpcm.c | 18 +++++++++---------
 libavcodec/adpcm.h |  1 +
 2 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/libavcodec/adpcm.c b/libavcodec/adpcm.c
index 83ef7c780a..4c15be6207 100644
--- a/libavcodec/adpcm.c
+++ b/libavcodec/adpcm.c
@@ -518,7 +518,7 @@ static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitC
     return (int16_t)c->predictor;
 }
 
-static inline int adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble)
+int16_t ff_adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble)
 {
     int step_index;
     int predictor;
@@ -1335,8 +1335,8 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame,
 
             for (int m = 0; m < 64; m += 2) {
                 int byte = bytestream2_get_byteu(&gb);
-                samples[m    ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F);
-                samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4  );
+                samples[m    ] = ff_adpcm_ima_qt_expand_nibble(cs, byte & 0x0F);
+                samples[m + 1] = ff_adpcm_ima_qt_expand_nibble(cs, byte >> 4  );
             }
         }
         ) /* End of CASE */
@@ -1689,16 +1689,16 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame,
     CASE(ADPCM_IMA_SSI,
         for (int n = nb_samples >> (1 - st); n > 0; n--) {
             int v = bytestream2_get_byteu(&gb);
-            *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0],  v >> 4  );
-            *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0x0F);
+            *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[0],  v >> 4  );
+            *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[st], v & 0x0F);
         }
         ) /* End of CASE */
     CASE(ADPCM_IMA_APM,
         for (int n = nb_samples / 2; n > 0; n--) {
             for (int channel = 0; channel < channels; channel++) {
                 int v = bytestream2_get_byteu(&gb);
-                *samples++  = adpcm_ima_qt_expand_nibble(&c->status[channel], v >> 4  );
-                samples[st] = adpcm_ima_qt_expand_nibble(&c->status[channel], v & 0x0F);
+                *samples++  = ff_adpcm_ima_qt_expand_nibble(&c->status[channel], v >> 4  );
+                samples[st] = ff_adpcm_ima_qt_expand_nibble(&c->status[channel], v & 0x0F);
             }
             samples += channels;
         }
@@ -2154,8 +2154,8 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame,
         for (int n = nb_samples >> (1 - st); n > 0; n--) {
             int v = bytestream2_get_byteu(&gb);
 
-            *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4 );
-            *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf);
+            *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4 );
+            *samples++ = ff_adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf);
         }
         ) /* End of CASE */
     CASE(ADPCM_CT,
diff --git a/libavcodec/adpcm.h b/libavcodec/adpcm.h
index 0ffc3da1d0..ff70e62078 100644
--- a/libavcodec/adpcm.h
+++ b/libavcodec/adpcm.h
@@ -44,5 +44,6 @@ typedef struct ADPCMChannelStatus {
 } ADPCMChannelStatus;
 
 int16_t ff_adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int shift, int flag);
+int16_t ff_adpcm_ima_qt_expand_nibble(ADPCMChannelStatus *c, int nibble);
 
 #endif /* AVCODEC_ADPCM_H */
-- 
2.49.1


From f0c79aec9d07be81a71e2c18c4be9d3f98478411 Mon Sep 17 00:00:00 2001
From: Manuel Lauss <manuel.lauss@gmail.com>
Date: Sat, 16 Aug 2025 10:44:06 +0200
Subject: [PATCH 2/2] avcodec/vima: IMA4 subtag support

Support decoding of the IMA4 ADPCM QT scheme used in some LucasArts
SANM files.
---
 libavcodec/vima.c | 81 ++++++++++++++++++++++++++++-------------------
 1 file changed, 48 insertions(+), 33 deletions(-)

diff --git a/libavcodec/vima.c b/libavcodec/vima.c
index 56cc1b7a85..bde7033c2c 100644
--- a/libavcodec/vima.c
+++ b/libavcodec/vima.c
@@ -28,6 +28,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/thread.h"
 
+#include "adpcm.h"
 #include "adpcm_data.h"
 #include "avcodec.h"
 #include "codec_internal.h"
@@ -159,46 +160,60 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
 
-    for (chan = 0; chan < channels; chan++) {
-        uint16_t *dest = (uint16_t *)frame->data[0] + chan;
-        int step_index = channel_hint[chan];
-        int output = pcm_data[chan];
-        int sample;
+    if (show_bits_long(&gb, 32) == MKBETAG('I','M','A','4')) {
+        int16_t *dest = (int16_t *)frame->data[0];
+        ADPCMChannelStatus cs;
 
-        for (sample = 0; sample < samples; sample++) {
-            int lookup_size, lookup, highbit, lowbits;
+        skip_bits_long(&gb, 32);        /* skip the 'IMA4' tag */
+        cs.predictor = (int16_t)get_xbits_le(&gb, 16);
+        cs.step_index = av_clip(get_bits(&gb, 8), 0, 88);
+        for (int i = 0; i < samples; i++) {
+            ff_adpcm_ima_qt_expand_nibble(&cs, get_bits(&gb, 4));
+            for (int j = 0; j < channels; j++)
+                *dest++ = cs.predictor;
+        }
+    } else {
+        for (chan = 0; chan < channels; chan++) {
+            uint16_t *dest = (uint16_t *)frame->data[0] + chan;
+            int step_index = channel_hint[chan];
+            int output = pcm_data[chan];
+            int sample;
 
-            step_index  = av_clip(step_index, 0, 88);
-            lookup_size = size_table[step_index];
-            lookup      = get_bits(&gb, lookup_size);
-            highbit     = 1 << (lookup_size - 1);
-            lowbits     = highbit - 1;
+            for (sample = 0; sample < samples; sample++) {
+                int lookup_size, lookup, highbit, lowbits;
 
-            if (lookup & highbit)
-                lookup ^= highbit;
-            else
-                highbit = 0;
+                step_index  = av_clip(step_index, 0, 88);
+                lookup_size = size_table[step_index];
+                lookup      = get_bits(&gb, lookup_size);
+                highbit     = 1 << (lookup_size - 1);
+                lowbits     = highbit - 1;
 
-            if (lookup == lowbits) {
-                output = get_sbits(&gb, 16);
-            } else {
-                int predict_index, diff;
+                if (lookup & highbit)
+                    lookup ^= highbit;
+                else
+                    highbit = 0;
 
-                predict_index = (lookup << (7 - lookup_size)) | (step_index << 6);
-                predict_index = av_clip(predict_index, 0, 5785);
-                diff          = predict_table[predict_index];
-                if (lookup)
-                    diff += ff_adpcm_step_table[step_index] >> (lookup_size - 1);
-                if (highbit)
-                    diff = -diff;
+                if (lookup == lowbits) {
+                    output = get_sbits(&gb, 16);
+                } else {
+                    int predict_index, diff;
 
-                output = av_clip_int16(output + diff);
+                    predict_index = (lookup << (7 - lookup_size)) | (step_index << 6);
+                    predict_index = av_clip(predict_index, 0, 5785);
+                    diff          = predict_table[predict_index];
+                    if (lookup)
+                        diff += ff_adpcm_step_table[step_index] >> (lookup_size - 1);
+                    if (highbit)
+                        diff = -diff;
+
+                    output = av_clip_int16(output + diff);
+                }
+
+                *dest = output;
+                dest += channels;
+
+                step_index += step_index_tables[lookup_size - 2][lookup];
             }
-
-            *dest = output;
-            dest += channels;
-
-            step_index += step_index_tables[lookup_size - 2][lookup];
         }
     }
 
-- 
2.49.1

_______________________________________________
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] only message in thread

only message in thread, other threads:[~2025-08-16  8:59 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-08-16  8:59 [FFmpeg-devel] [PATCH] avcodec/vima: support IMA4 (ADPCM QT) variant (PR #20253) Manuel Lauss via ffmpeg-devel

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