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 3EF6B4C5B0 for ; Wed, 25 Jun 2025 08:03:19 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id 8862F68CB8E; Wed, 25 Jun 2025 11:03:14 +0300 (EEST) Received: from mx.sdf.org (mx.sdf.org [205.166.94.24]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTPS id 4117268CA41 for ; Wed, 25 Jun 2025 11:03:08 +0300 (EEST) Received: from c85480c446b5888e93dabaf6a239f4b4 ([1.136.108.120]) (authenticated (0 bits)) by mx.sdf.org (8.18.1/8.14.3) with ESMTPSA id 55P82vD6023525 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits) verified NO) for ; Wed, 25 Jun 2025 08:03:03 GMT Date: Wed, 25 Jun 2025 18:02:56 +1000 From: Peter Ross To: ffmpeg-devel@ffmpeg.org Message-ID: <7bb76ae91079c70434e8069230d5e7a4d11a152e.1750838520.git.pross@xvid.org> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/2] avcodec/adpcm: Sanyo LD-ADPCM decoder 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: , Reply-To: FFmpeg development discussions and patches Content-Type: multipart/mixed; boundary="===============0151888419994349499==" Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: --===============0151888419994349499== Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="K8UE77p76k/tXsPt" Content-Disposition: inline --K8UE77p76k/tXsPt Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable --- adpcm_le.c was added because the decoder needs little-endian get_bits.h Samples: https://pross.sdf.org/sandpit/sanyo-mono-3bit-8000.wav https://pross.sdf.org/sandpit/sanyo-mono-4bit-8000.wav https://pross.sdf.org/sandpit/sanyo-mono-5bit-8000.wav libavcodec/Makefile | 1 + libavcodec/adpcm.c | 21 ++++ libavcodec/adpcm.h | 2 + libavcodec/adpcm_le.c | 205 ++++++++++++++++++++++++++++++++++++++++ libavcodec/allcodecs.c | 1 + libavcodec/codec_desc.c | 7 ++ libavcodec/codec_id.h | 1 + libavformat/riff.c | 1 + 8 files changed, 239 insertions(+) create mode 100644 libavcodec/adpcm_le.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index dca6c100c0..45cf80e5dd 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -990,6 +990,7 @@ OBJS-$(CONFIG_ADPCM_MS_DECODER) +=3D adpcm.o = adpcm_data.o OBJS-$(CONFIG_ADPCM_MS_ENCODER) +=3D adpcmenc.o adpcm_data.o OBJS-$(CONFIG_ADPCM_MTAF_DECODER) +=3D adpcm.o adpcm_data.o OBJS-$(CONFIG_ADPCM_PSX_DECODER) +=3D adpcm.o adpcm_data.o +OBJS-$(CONFIG_ADPCM_SANYO_DECODER) +=3D adpcm.o adpcm_data.o adpcm_= le.o OBJS-$(CONFIG_ADPCM_SBPRO_2_DECODER) +=3D adpcm.o adpcm_data.o OBJS-$(CONFIG_ADPCM_SBPRO_3_DECODER) +=3D adpcm.o adpcm_data.o OBJS-$(CONFIG_ADPCM_SBPRO_4_DECODER) +=3D adpcm.o adpcm_data.o diff --git a/libavcodec/adpcm.c b/libavcodec/adpcm.c index 622cf54b40..d0f5780dce 100644 --- a/libavcodec/adpcm.c +++ b/libavcodec/adpcm.c @@ -260,6 +260,9 @@ static av_cold int adpcm_decode_init(AVCodecContext * a= vctx) case AV_CODEC_ID_ADPCM_IMA_AMV: max_channels =3D 1; break; + case AV_CODEC_ID_ADPCM_SANYO: + max_channels =3D 2; + break; case AV_CODEC_ID_ADPCM_AFC: case AV_CODEC_ID_ADPCM_EA_R1: case AV_CODEC_ID_ADPCM_EA_R2: @@ -307,6 +310,10 @@ static av_cold int adpcm_decode_init(AVCodecContext * = avctx) avctx->block_align !=3D 17 * avctx->ch_layout.nb_channels) return AVERROR_INVALIDDATA; break; + case AV_CODEC_ID_ADPCM_SANYO: + if (avctx->bits_per_coded_sample < 3 || avctx->bits_per_coded_samp= le > 5) + return AVERROR_INVALIDDATA; + break; case AV_CODEC_ID_ADPCM_IMA_XBOX: if (avctx->bits_per_coded_sample !=3D 4) return AVERROR_INVALIDDATA; @@ -338,6 +345,7 @@ static av_cold int adpcm_decode_init(AVCodecContext * a= vctx) case AV_CODEC_ID_ADPCM_AFC: case AV_CODEC_ID_ADPCM_DTK: case AV_CODEC_ID_ADPCM_PSX: + case AV_CODEC_ID_ADPCM_SANYO: case AV_CODEC_ID_ADPCM_MTAF: case AV_CODEC_ID_ADPCM_ARGO: case AV_CODEC_ID_ADPCM_IMA_MOFLEX: @@ -1072,6 +1080,11 @@ static int get_nb_samples(AVCodecContext *avctx, Get= ByteContext *gb, case AV_CODEC_ID_ADPCM_ZORK: nb_samples =3D buf_size / ch; break; + case AV_CODEC_ID_ADPCM_SANYO: + if (!avctx->extradata || avctx->extradata_size !=3D 2) + return AVERROR_INVALIDDATA; + nb_samples =3D AV_RL16(avctx->extradata); + break; } =20 /* validate coded sample count */ @@ -2265,6 +2278,13 @@ static int adpcm_decode_frame(AVCodecContext *avctx,= AVFrame *frame, } } ) /* End of CASE */ + CASE(ADPCM_SANYO, + for (int ch =3D 0; ch < channels; ch++) { + c->status[ch].predictor =3D (int16_t)bytestream2_get_le16(&gb); + c->status[ch].step =3D (int16_t)bytestream2_get_le16(&gb); + } + bytestream2_skip(&gb, ff_adpcm_sanyo_decode(c->status, gb.buffer, = bytestream2_get_bytes_left(&gb), avctx->bits_per_coded_sample, nb_samples, = channels, samples_p)); + ) /* End of CASE */ CASE(ADPCM_ARGO, /* * The format of each block: @@ -2448,6 +2468,7 @@ ADPCM_DECODER(ADPCM_IMA_XBOX, sample_fmts_s16p, ad= pcm_ima_xbox, "ADPCM IMA ADPCM_DECODER(ADPCM_MS, sample_fmts_both, adpcm_ms, "ADP= CM Microsoft") ADPCM_DECODER(ADPCM_MTAF, sample_fmts_s16p, adpcm_mtaf, "ADP= CM MTAF") ADPCM_DECODER(ADPCM_PSX, sample_fmts_s16p, adpcm_psx, "ADP= CM Playstation") +ADPCM_DECODER(ADPCM_SANYO, sample_fmts_s16p, adpcm_sanyo, "ADP= CM Sanyo") ADPCM_DECODER(ADPCM_SBPRO_2, sample_fmts_s16, adpcm_sbpro_2, "ADP= CM Sound Blaster Pro 2-bit") ADPCM_DECODER(ADPCM_SBPRO_3, sample_fmts_s16, adpcm_sbpro_3, "ADP= CM Sound Blaster Pro 2.6-bit") ADPCM_DECODER(ADPCM_SBPRO_4, sample_fmts_s16, adpcm_sbpro_4, "ADP= CM Sound Blaster Pro 4-bit") diff --git a/libavcodec/adpcm.h b/libavcodec/adpcm.h index 0ffc3da1d0..b1c54ed275 100644 --- a/libavcodec/adpcm.h +++ b/libavcodec/adpcm.h @@ -45,4 +45,6 @@ typedef struct ADPCMChannelStatus { =20 int16_t ff_adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, in= t shift, int flag); =20 +int ff_adpcm_sanyo_decode(ADPCMChannelStatus *cs, const uint8_t *data, int= data_size, int bits_per_coded_sample, int nb_samples, int channels, int16_= t **samples_p); + #endif /* AVCODEC_ADPCM_H */ diff --git a/libavcodec/adpcm_le.c b/libavcodec/adpcm_le.c new file mode 100644 index 0000000000..df8eb3dd90 --- /dev/null +++ b/libavcodec/adpcm_le.c @@ -0,0 +1,205 @@ +/* + * Copyright (c) 2025 Peter Ross + * + * 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-130= 1 USA + */ + +#include "adpcm.h" +#define BITSTREAM_READER_LE +#include "get_bits.h" + +static int adpcm_sanyo_expand3(ADPCMChannelStatus *c, int bits) +{ + int sign, delta, add; + + sign =3D bits & 4; + if (sign) + delta =3D 4 - (bits & 3); + else + delta =3D bits; + + switch (delta) { + case 0: + add =3D 0; + c->step =3D (3 * c->step) >> 2; + break; + case 1: + add =3D c->step; + c->step =3D (4 * c->step - (c->step >> 1)) >> 2; + break; + case 2: + add =3D 2 * c->step; + c->step =3D ((c->step >> 1) + add) >> 1; + break; + case 3: + add =3D 4 * c->step - (c->step >> 1); + c->step =3D 2 * c->step; + break; + case 4: + add =3D (11 * c->step) >> 1; + c->step =3D 3 * c->step; + break; + } + + if (sign) + add =3D -add; + + c->predictor =3D av_clip_int16(c->predictor + add); + c->step =3D av_clip(c->step, 1, 7281); + return c->predictor; +} + +static int adpcm_sanyo_expand4(ADPCMChannelStatus *c, int bits) +{ + int sign, delta, add; + + sign =3D bits & 8; + if (sign) + delta =3D 8 - (bits & 7); + else + delta =3D bits; + + switch (delta) { + case 0: + add =3D 0; + c->step =3D (3 * c->step) >> 2; + break; + case 1: + add =3D c->step; + c->step =3D (3 * c->step) >> 2; + break; + case 2: + add =3D 2 * c->step; + break; + case 3: + add =3D 3 * c->step; + break; + case 4: + add =3D 4 * c->step; + break; + case 5: + add =3D (11 * c->step) >> 1; + c->step +=3D c->step >> 2; + break; + case 6: + add =3D (15 * c->step) >> 1; + c->step =3D 2 * c->step; + break; + case 7: + if (sign) + add =3D (19 * c->step) >> 1; + else + add =3D (21 * c->step) >> 1; + c->step =3D (c->step >> 1) + 2 * c->step; + break; + case 8: + add =3D (25 * c->step) >> 1; + c->step =3D 5 * c->step; + break; + } + + if (sign) + add =3D -add; + + c->predictor =3D av_clip_int16(c->predictor + add); + c->step =3D av_clip(c->step, 1, 2621); + return c->predictor; +} + +static int adpcm_sanyo_expand5(ADPCMChannelStatus *c, int bits) +{ + int sign, delta, add; + + sign =3D bits & 0x10; + if (sign) + delta =3D 16 - (bits & 0xF); + else + delta =3D bits; + + add =3D delta * c->step; + switch (delta) { + case 0: + c->step +=3D (c->step >> 2) - (c->step >> 1); + break; + case 1: + case 2: + case 3: + c->step +=3D (c->step >> 3) - (c->step >> 2); + break; + case 4: + case 5: + c->step +=3D (c->step >> 4) - (c->step >> 3); + break; + case 6: + break; + case 7: + c->step +=3D c->step >> 3; + break; + case 8: + c->step +=3D c->step >> 2; + break; + case 9: + c->step +=3D c->step >> 1; + break; + case 10: + c->step =3D 2 * c->step - (c->step >> 3); + break; + case 11: + c->step =3D 2 * c->step + (c->step >> 3); + break; + case 12: + c->step =3D 2 * c->step + (c->step >> 1) - (c->step >> 3); + break; + case 13: + c->step =3D 3 * c->step - (c->step >> 2); + break; + case 14: + c->step *=3D 3; + break; + case 15: + case 16: + c->step =3D (7 * c->step) >> 1; + break; + } + + if (sign) + add =3D -add; + + c->predictor =3D av_clip_int16(c->predictor + add); + c->step =3D av_clip(c->step, 1, 1024); + return c->predictor; +} + +int ff_adpcm_sanyo_decode(ADPCMChannelStatus *cs, const uint8_t *data, int= data_size, int bits_per_coded_sample, int nb_samples, int channels, int16_= t **samples_p) +{ + int (*expand)(ADPCMChannelStatus *c, int bits); + GetBitContext gb; + + switch(bits_per_coded_sample) { + case 3: expand =3D adpcm_sanyo_expand3; break; + case 4: expand =3D adpcm_sanyo_expand4; break; + case 5: expand =3D adpcm_sanyo_expand5; break; + } + + init_get_bits8(&gb, data, data_size); + for (int i =3D 0; i < nb_samples; i++) + for (int ch =3D 0; ch < channels; ch++) + samples_p[ch][i] =3D expand(&cs[ch], get_bits(&gb, bits_per_co= ded_sample)); + + align_get_bits(&gb); + return get_bits_count(&gb) / 8; +} diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 4b11ad3a75..082cd22e6a 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -694,6 +694,7 @@ extern const FFCodec ff_adpcm_ms_encoder; extern const FFCodec ff_adpcm_ms_decoder; extern const FFCodec ff_adpcm_mtaf_decoder; extern const FFCodec ff_adpcm_psx_decoder; +extern const FFCodec ff_adpcm_sanyo_decoder; extern const FFCodec ff_adpcm_sbpro_2_decoder; extern const FFCodec ff_adpcm_sbpro_3_decoder; extern const FFCodec ff_adpcm_sbpro_4_decoder; diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c index 5b6aaab88e..dae2296689 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -2619,6 +2619,13 @@ static const AVCodecDescriptor codec_descriptors[] = =3D { .long_name =3D NULL_IF_CONFIG_SMALL("ADPCM IMA Xbox"), .props =3D AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY, }, + { + .id =3D AV_CODEC_ID_ADPCM_SANYO, + .type =3D AVMEDIA_TYPE_AUDIO, + .name =3D "adpcm_sanyo", + .long_name =3D NULL_IF_CONFIG_SMALL("ADPCM Sanyo"), + .props =3D AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY, + }, =20 /* AMR */ { diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h index 09dff29886..d00d3fe121 100644 --- a/libavcodec/codec_id.h +++ b/libavcodec/codec_id.h @@ -425,6 +425,7 @@ enum AVCodecID { AV_CODEC_ID_ADPCM_IMA_ACORN, AV_CODEC_ID_ADPCM_XMD, AV_CODEC_ID_ADPCM_IMA_XBOX, + AV_CODEC_ID_ADPCM_SANYO, =20 /* AMR */ AV_CODEC_ID_AMR_NB =3D 0x12000, diff --git a/libavformat/riff.c b/libavformat/riff.c index 151563e9f2..3c12c4e6c3 100644 --- a/libavformat/riff.c +++ b/libavformat/riff.c @@ -570,6 +570,7 @@ const AVCodecTag ff_codec_wav_tags[] =3D { { AV_CODEC_ID_G729, 0x0083 }, { AV_CODEC_ID_AAC, 0x00ff }, { AV_CODEC_ID_G723_1, 0x0111 }, + { AV_CODEC_ID_ADPCM_SANYO, 0x0125 }, { AV_CODEC_ID_SIPR, 0x0130 }, { AV_CODEC_ID_ACELP_KELVIN, 0x0135 }, { AV_CODEC_ID_WMAV1, 0x0160 }, --=20 2.47.2 -- Peter (A907 E02F A6E5 0CD2 34CD 20D2 6760 79C5 AC40 DD6B) --K8UE77p76k/tXsPt Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iF0EABECAB0WIQSpB+AvpuUM0jTNINJnYHnFrEDdawUCaFutKQAKCRBnYHnFrEDd a/SSAKCrN1hGW35bd88f8rg9S/dCR6AOSwCfY90RNjhvQXlZkGI64v0ht/5ah3g= =Jt/D -----END PGP SIGNATURE----- --K8UE77p76k/tXsPt-- --===============0151888419994349499== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ 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". --===============0151888419994349499==--