From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.ffmpeg.org (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id 528B748474 for ; Sat, 16 Aug 2025 08:59:44 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id A197E68D384; Sat, 16 Aug 2025 11:59:38 +0300 (EEST) Received: from 1e8b7847f7d1 (code.ffmpeg.org [188.245.149.3]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTPS id 4193268C746 for ; Sat, 16 Aug 2025 11:59:36 +0300 (EEST) MIME-Version: 1.0 To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] =?utf-8?q?=5BPATCH=5D_avcodec/vima=3A_support_IMA?= =?utf-8?q?4_=28ADPCM_QT=29_variant_=28PR_=2320253=29?= X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Manuel Lauss via ffmpeg-devel Reply-To: FFmpeg development discussions and patches Cc: Manuel Lauss Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Message-Id: <20250816085938.A197E68D384@ffbox0-bg.ffmpeg.org> Date: Sat, 16 Aug 2025 11:59:38 +0300 (EEST) Archived-At: List-Archive: List-Post: 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 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 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".