* [FFmpeg-devel] [PATCH] RFC: avcodec/vima: add IMA4 support (PR #20243)
@ 2025-08-15 9:53 Manuel Lauss
0 siblings, 0 replies; only message in thread
From: Manuel Lauss @ 2025-08-15 9:53 UTC (permalink / raw)
To: ffmpeg-devel
PR #20243 opened by Manuel Lauss (mlauss2)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20243
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20243.patch
Add support to the LucasArts VIMA decoder for the IMA4 variant.
This is a fix for Trac Ticket #5406 (https://trac.ffmpeg.org/ticket/5406)
RFC because: the implementation I made here works for the first chunk, but all subsequent chunks have popping noises in them. Maybe it's too hot here at the moment, but I cannot find the reason why, especially since I've derived this from an already flawlessly working implementation I made earlier. Am I missing something in ffmpeg?
2 sample files are at https://samples.ffmpeg.org/ffmpeg-bugs/trac/ticket5406/
Thanks!
From 62f3796e78cfde3ef0d1505b97e1b4528f8f2943 Mon Sep 17 00:00:00 2001
From: Manuel Lauss <manuel.lauss@gmail.com>
Date: Fri, 15 Aug 2025 11:40:55 +0200
Subject: [PATCH] avcodec/vima: add IMA4 support
ADPCM based on 4-bit input nibbles.
---
libavcodec/vima.c | 100 ++++++++++++++++++++++++++++++----------------
1 file changed, 66 insertions(+), 34 deletions(-)
diff --git a/libavcodec/vima.c b/libavcodec/vima.c
index 56cc1b7a85..63b5690a89 100644
--- a/libavcodec/vima.c
+++ b/libavcodec/vima.c
@@ -85,6 +85,10 @@ static const int8_t *const step_index_tables[] = {
index_table4, index_table5, index_table6
};
+static const int8_t ima4_table[] = {
+ -1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8
+};
+
static av_cold void predict_table_init(void)
{
for (int start_pos = 0; start_pos < 64; start_pos++) {
@@ -159,49 +163,77 @@ 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];
+ int dat, nib, adpcm_step, diff, step_index;
- for (sample = 0; sample < samples; sample++) {
- int lookup_size, lookup, highbit, lowbits;
+ adpcm_step = 7;
+ skip_bits_long(&gb, 32); /* skip the 'IMA4' tag */
+ dat = (int16_t)get_sbits(&gb, 16);
+ step_index = get_bits(&gb, 8);
+ for (int i = 0; i < samples; i++) {
+ nib = get_bits(&gb, 4);
+ diff = adpcm_step >> 3;
+ if (nib & 4)
+ diff += adpcm_step;
+ if (nib & 2)
+ diff += (adpcm_step >> 1);
+ if (nib & 1)
+ diff += (adpcm_step >> 2);
+ if (nib & 8)
+ diff = -diff;
+ dat = av_clip_int16(dat + diff);
+ for (int j = 0; j < channels; j++)
+ *dest++ = dat;
- 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;
+ step_index = av_clip(step_index + ima4_table[nib], 0, 88);
+ if (step_index > 0)
+ adpcm_step = ff_adpcm_step_table[step_index];
+ }
+ } 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;
- if (lookup & highbit)
- lookup ^= highbit;
- else
- highbit = 0;
+ for (sample = 0; sample < samples; sample++) {
+ int lookup_size, lookup, highbit, lowbits;
- if (lookup == lowbits) {
- output = get_sbits(&gb, 16);
- } else {
- int predict_index, diff;
+ 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;
- 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 & highbit)
+ lookup ^= highbit;
+ else
+ highbit = 0;
- output = av_clip_int16(output + diff);
+ if (lookup == lowbits) {
+ output = get_sbits(&gb, 16);
+ } else {
+ int predict_index, 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];
}
}
-
*got_frame_ptr = 1;
return pkt->size;
--
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-15 9:53 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-08-15 9:53 [FFmpeg-devel] [PATCH] RFC: avcodec/vima: add IMA4 support (PR #20243) Manuel Lauss
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