From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id 9D9B64C1B2 for ; Thu, 6 Feb 2025 00:28:42 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 64F1E68B9C9; Thu, 6 Feb 2025 02:28:38 +0200 (EET) Received: from mx.sdf.org (mx.sdf.org [205.166.94.24]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E2FA4680B20 for ; Thu, 6 Feb 2025 02:28:30 +0200 (EET) Received: from b23210acc22a312b5f9efa84d03776bb ([1.145.216.185]) (authenticated (0 bits)) by mx.sdf.org (8.18.1/8.14.3) with ESMTPSA id 5160SMIX009036 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits) verified NO) for ; Thu, 6 Feb 2025 00:28:28 GMT Date: Thu, 6 Feb 2025 11:28:21 +1100 From: Peter Ross To: ffmpeg-devel@ffmpeg.org Message-ID: <1a5fd88cf35db508e3ccc45037c62a0d9dd40c17.1738801619.git.pross@xvid.org> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCHv2] avcodec/g728dec: G.728 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="===============0773918283420838285==" Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: --===============0773918283420838285== Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="ayEhmrFOD2aIcTeb" Content-Disposition: inline --ayEhmrFOD2aIcTeb Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable --- suggestions incorporated. libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/codec_desc.c | 7 ++ libavcodec/codec_id.h | 1 + libavcodec/g728data.h | 70 +++++++++++++ libavcodec/g728dec.c | 220 ++++++++++++++++++++++++++++++++++++++++ libavcodec/utils.c | 1 + 7 files changed, 301 insertions(+) create mode 100644 libavcodec/g728data.h create mode 100644 libavcodec/g728dec.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 95bd5488b6..1028046e89 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -395,6 +395,7 @@ OBJS-$(CONFIG_G723_1_DECODER) +=3D g723_1dec.o= g723_1.o \ acelp_vectors.o celp_filters.o c= elp_math.o OBJS-$(CONFIG_G723_1_ENCODER) +=3D g723_1enc.o g723_1.o \ acelp_vectors.o celp_filters.o c= elp_math.o +OBJS-$(CONFIG_G728_DECODER) +=3D g728dec.o OBJS-$(CONFIG_G729_DECODER) +=3D g729dec.o lsp.o celp_math.o ce= lp_filters.o acelp_filters.o acelp_pitch_delay.o acelp_vectors.o g729postfi= lter.o OBJS-$(CONFIG_GDV_DECODER) +=3D gdv.o OBJS-$(CONFIG_GEM_DECODER) +=3D gemdec.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 4e1b1c9b45..111eadf7ce 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -475,6 +475,7 @@ extern const FFCodec ff_flac_decoder; extern const FFCodec ff_ftr_decoder; extern const FFCodec ff_g723_1_encoder; extern const FFCodec ff_g723_1_decoder; +extern const FFCodec ff_g728_decoder; extern const FFCodec ff_g729_decoder; extern const FFCodec ff_gsm_decoder; extern const FFCodec ff_gsm_ms_decoder; diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c index b734d07ded..d8bd30d0e1 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -3466,6 +3466,13 @@ static const AVCodecDescriptor codec_descriptors[] = =3D { .long_name =3D NULL_IF_CONFIG_SMALL("LC3 (Low Complexity Communica= tion Codec)"), .props =3D AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY, }, + { + .id =3D AV_CODEC_ID_G728, + .type =3D AVMEDIA_TYPE_AUDIO, + .name =3D "g728", + .long_name =3D NULL_IF_CONFIG_SMALL("G.728"), + .props =3D AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY, + }, =20 /* subtitle codecs */ { diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h index 97b70c5bf5..ee02fe4261 100644 --- a/libavcodec/codec_id.h +++ b/libavcodec/codec_id.h @@ -552,6 +552,7 @@ enum AVCodecID { AV_CODEC_ID_OSQ, AV_CODEC_ID_QOA, AV_CODEC_ID_LC3, + AV_CODEC_ID_G728, =20 /* subtitle codecs */ AV_CODEC_ID_FIRST_SUBTITLE =3D 0x17000, ///< A dummy ID point= ing at the start of subtitle codecs. diff --git a/libavcodec/g728data.h b/libavcodec/g728data.h new file mode 100644 index 0000000000..a2ddf5682d --- /dev/null +++ b/libavcodec/g728data.h @@ -0,0 +1,70 @@ +/* + * G.728 decoder + * + * 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 + */ + +#ifndef AVCODEC_G728DATA_H +#define AVCODEC_G728DATA_H + +#include +#include "libavutil/macros.h" + +#define IDIM 5 /* Vector dimension (excitation block size) */ +#define LPC 50 /* Synthesis filter order */ +#define LPCLG 10 /* Log-gain predictor order */ +#define NFRSZ 20 /* Frame size (adaptation cycle size in samples */ +#define NONR 35 /* Number of non-recursive window samples for synthesis fi= lter */ +#define NONRLG 20 /* Number of non-recursive window samples for log-gain p= redictor */ +#define NUPDATE 4 /* Predictor update period (in terms of vectors) */ + +#define NSBSZ (LPC + NONR + NFRSZ) +#define NSBGSZ (LPCLG + NONRLG + NUPDATE) + +// Hybrid window for the synthesis filter +static const uint16_t g728_wnr[NSBSZ] =3D { + 1565, 3127, 4681, 6225, 7755, 9266, 10757, 12223, 13661, 15068, + 16441, 17776, 19071, 20322, 21526, 22682, 23786, 24835, 25828, 26761, + 27634, 28444, 29188, 29866, 30476, 31016, 31486, 31884, 32208, 32460, + 32637, 32739, 32767, 32721, 32599, 32403, 32171, 31940, 31711, 31484, + 31259, 31034, 30812, 30591, 30372, 30154, 29938, 29724, 29511, 29299, + 29089, 28881, 28674, 28468, 28264, 28062, 27861, 27661, 27463, 27266, + 27071, 26877, 26684, 26493, 26303, 26114, 25927, 25742, 25557, 25374, + 25192, 25012, 24832, 24654, 24478, 24302, 24128, 23955, 23784, 23613, + 23444, 23276, 23109, 22943, 22779, 22616, 22454, 22293, 22133, 21974, + 21817, 21661, 21505, 21351, 21198, 21046, 20896, 20746, 20597, 20450, + 20303, 20157, 20013, 19870, 19727 +}; + +// Hybrid window for the log-gain predictor +static const uint16_t g728_wnrg[NSBGSZ] =3D { + 3026, 6025, 8973, 11845, 14615, 17261, 19759, 22088, 24228, 26162, + 27872, 29344, 30565, 31525, 32216, 32631, 32767, 32625, 32203, 31506, + 30540, 29461, 28420, 27416, 26448, 25514, 24613, 23743, 22905, 22096, + 21315, 20562, 19836, 19135 +}; + +// Values for bandwidth broadcasting +static const uint16_t g728_facv[LPC] =3D { + 16192, 16002, 15815, 15629, 15446, 15265, 15086, 14910, 14735, 14562, + 14391, 14223, 14056, 13891, 13729, 13568, 13409, 13252, 13096, 12943, + 12791, 12641, 12493, 12347, 12202, 12059, 11918, 11778, 11640, 11504, + 11369, 11236, 11104, 10974, 10845, 10718, 10593, 10468, 10346, 10225, + 10105, 9986, 9869, 9754, 9639, 9526, 9415, 9304, 9195, 9088 +}; + +#endif /* AVCODEC_G728DATA_H */ diff --git a/libavcodec/g728dec.c b/libavcodec/g728dec.c new file mode 100644 index 0000000000..f932a50f6c --- /dev/null +++ b/libavcodec/g728dec.c @@ -0,0 +1,220 @@ +/* + * G.728 decoder + * 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 "avcodec.h" +#include "celp_filters.h" +#include "codec_internal.h" +#include "decode.h" +#include "get_bits.h" +#include "g728data.h" +#include "lpc_functions.h" +#include "ra288.h" +#include "libavutil/float_dsp.h" +#include "libavutil/mem.h" +#include "libavutil/mem_internal.h" +#include "libavutil/opt.h" +#include "libavutil/thread.h" + +#define MAX_BACKWARD_FILTER_ORDER LPC +#define MAX_BACKWARD_FILTER_LEN NFRSZ +#define MAX_BACKWARD_FILTER_NONREC NONR +#define ATTEN 0.75f +#include "g728_template.c" + +#define LPCW 10 /* Perceptual weighting filter order */ +#define GOFF 32.0f /* Log-gain offset value */ + +static float g728_gq_db[8]; +static float g728_y_db[128]; +static float g728_wnr_r[FFALIGN(NSBSZ,16)]; +static float g728_wnrg_r[FFALIGN(NSBGSZ, 16)]; +static float g728_facv_f[FFALIGN(LPC, 16)]; + +static av_cold void g728_init_static_data(void) +{ + for(int i =3D 0; i < FF_ARRAY_ELEMS(amptable); i++) + g728_gq_db[i] =3D 10.0f*log10f(amptable[i] * amptable[i]); + + for (int i =3D 0; i < FF_ARRAY_ELEMS(codetable); i++) { + float cby[IDIM]; + for (int j =3D 0; j < IDIM; j++) + cby[j] =3D codetable[i][j] * (1.0f/(1<<11)); + g728_y_db[i] =3D 10.0f*log10f(avpriv_scalarproduct_float_c(cby, cb= y, IDIM) / IDIM); + } + + for (int i =3D 0; i < NSBSZ; i++) + g728_wnr_r[i] =3D g728_wnr[NSBSZ - 1 - i] * (1.0f/(1<<15)); + for (int i =3D 0; i < NSBGSZ; i++) + g728_wnrg_r[i] =3D g728_wnrg[NSBGSZ - 1 - i] * (1.0f/(1<<15)); + for (int i =3D 0; i < LPC; i++) + g728_facv_f[i] =3D g728_facv[i] * (1.0f/(1<<14)); +} + +typedef struct { + AVFloatDSPContext *fdsp; + int valid; + float a[LPC]; + DECLARE_ALIGNED(32, float, sb)[NSBSZ]; + DECLARE_ALIGNED(32, float, sbg)[NSBGSZ]; + DECLARE_ALIGNED(32, float, gp)[FFALIGN(LPCLG, 16)]; + DECLARE_ALIGNED(32, float, atmp)[FFALIGN(LPC, 16)]; + float rexp[LPC + 1]; + float rexpg[LPCLG + 1]; + float r[LPC + 1]; + float alpha; +} G728Context; + +static av_cold int g728_decode_init(AVCodecContext *avctx) +{ + static AVOnce init_static_once =3D AV_ONCE_INIT; + G728Context *s =3D avctx->priv_data; + + s->fdsp =3D avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXA= CT); + if (!s->fdsp) + return AVERROR(ENOMEM); + + s->gp[0] =3D -1.0f; + for (int i =3D 0; i < NUPDATE; i++) + s->sbg[NSBGSZ - 1 -i] =3D -GOFF; + + avctx->sample_fmt =3D AV_SAMPLE_FMT_FLT; + + av_channel_layout_uninit(&avctx->ch_layout); + avctx->ch_layout =3D (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO; + + ff_thread_once(&init_static_once, g728_init_static_data); + return 0; +} + +static av_cold int g728_decode_close(AVCodecContext *avctx) +{ + G728Context *s =3D avctx->priv_data; + av_freep(&s->fdsp); + return 0; +} + +static int hybrid_window(AVFloatDSPContext *fdsp, + int order, int n, int non_rec, float *out, + const float *hist, float *out2, const float *wind= ow) +{ + do_hybrid_window(fdsp->vector_fmul, order, n, non_rec, out, hist, out2= , window); + return out[order] !=3D 0.0f; +} + +static void decode_frame(G728Context *s, GetBitContext *gb, float *dst) +{ + float *gstate =3D s->sbg + NSBGSZ - 2; + + for (int idx =3D 0; idx < NUPDATE; idx++) { + DECLARE_ALIGNED(32, float, et)[IDIM]; + float *statelpc =3D s->sb + NSBSZ - NFRSZ + idx*IDIM; + float gain, gain_db; + int is, ig; + + gain_db =3D 0.0f; + for (int i =3D 0; i < LPCLG; i++) + gain_db -=3D s->gp[i] * gstate[-i]; + gain_db =3D av_clipf(gain_db, -GOFF, 28.0f); + + is =3D get_bits(gb, 7); // shape index + ig =3D get_bits(gb, 3); // gain index + + gain =3D powf(10.0f, (gain_db + GOFF) * .05f) * amptable[ig] * (1.= 0f/(1<<11)); + for (int i =3D 0; i < IDIM; i++) + et[i] =3D codetable[is][i] * gain; + + ff_celp_lp_synthesis_filterf(statelpc, s->a, et, IDIM, LPC); + + for (int i =3D 0; i < IDIM; i++) { + statelpc[i] =3D av_clipf(statelpc[i], -4095.0f, 4095.0f); + dst[idx*IDIM + i] =3D statelpc[i] * (1.0f/(1<<12)); + } + + gstate++; + *gstate =3D FFMAX(-GOFF, g728_gq_db[ig] + g728_y_db[is] + gain_db); + + if (idx =3D=3D 0) { + DECLARE_ALIGNED(32, float, gptmp)[FFALIGN(LPCLG, 16)]; + if (s->valid && (s->valid =3D !compute_lpc_coefs(s->r + 1, LPC= W, LPC, s->atmp, 0, 0, 1, &s->alpha))) { + s->fdsp->vector_fmul(s->atmp, s->atmp, g728_facv_f, FFALIG= N(LPC, 16)); + } + if (hybrid_window(s->fdsp, LPCLG, NUPDATE, NONRLG, s->r, s->sb= g, s->rexpg, g728_wnrg_r) && + !compute_lpc_coefs(s->r, 0, LPCLG, gptmp, 0, 0, 1, &s-= >alpha)) { + s->fdsp->vector_fmul(s->gp, gptmp, gain_bw_tab, FFALIGN(LP= CLG, 16)); + } + memmove(s->sbg, s->sbg + NUPDATE, sizeof(float)*(LPCLG + NONRL= G)); + gstate =3D s->sbg + NSBGSZ - 1 - NUPDATE; + } else if (idx =3D=3D 1) { + if (s->valid) + memcpy(s->a, s->atmp, sizeof(float)*LPC); + } + } + + s->valid =3D 0; + if (hybrid_window(s->fdsp, LPC, NFRSZ, NONR, s->r, s->sb, s->rexp, g72= 8_wnr_r)) { + s->valid =3D !compute_lpc_coefs(s->r, 0, LPCW, s->atmp, 0, 0, 1, &= s->alpha); + } + + memmove(s->sb, s->sb + NFRSZ, sizeof(float)*(LPC + NONR)); +} + +static int g728_decode_frame(AVCodecContext *avctx, AVFrame *frame, + int *got_frame_ptr, AVPacket *avpkt) +{ + G728Context *s =3D avctx->priv_data; + GetBitContext gb; + int ret; + int nb_frames =3D avpkt->size / 5; + + if (!nb_frames) + return AVERROR_INVALIDDATA; + + if ((ret =3D init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0) + return ret; + +#define SAMPLES_PER_FRAME 20 + + frame->nb_samples =3D nb_frames * SAMPLES_PER_FRAME; + if ((ret =3D ff_get_buffer(avctx, frame, 0)) < 0) + return ret; + + for (int i =3D 0; i < nb_frames; i++) + decode_frame(s, &gb, (float *)frame->data[0] + i * 20); + + *got_frame_ptr =3D 1; + + return nb_frames * 5; +} + +const FFCodec ff_g728_decoder =3D { + .p.name =3D "g728", + CODEC_LONG_NAME("G.728)"), + .p.type =3D AVMEDIA_TYPE_AUDIO, + .p.id =3D AV_CODEC_ID_G728, + .priv_data_size =3D sizeof(G728Context), + .init =3D g728_decode_init, + .close =3D g728_decode_close, + FF_CODEC_DECODE_CB(g728_decode_frame), + .p.capabilities =3D AV_CODEC_CAP_CHANNEL_CONF | + AV_CODEC_CAP_DR1, + .p.sample_fmts =3D (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT, + AV_SAMPLE_FMT_NONE }, +}; diff --git a/libavcodec/utils.c b/libavcodec/utils.c index dd846b4ae9..58b29c4c9d 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -553,6 +553,7 @@ int av_get_bits_per_sample(enum AVCodecID codec_id) case AV_CODEC_ID_DFPWM: return 1; case AV_CODEC_ID_ADPCM_SBPRO_2: + case AV_CODEC_ID_G728: return 2; case AV_CODEC_ID_ADPCM_SBPRO_3: return 3; --=20 2.47.2 -- Peter (A907 E02F A6E5 0CD2 34CD 20D2 6760 79C5 AC40 DD6B) --ayEhmrFOD2aIcTeb Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iF0EABECAB0WIQSpB+AvpuUM0jTNINJnYHnFrEDdawUCZ6QCIQAKCRBnYHnFrEDd a7GIAKCYQIeqFUrviCcgfMINsaZlQVIXWQCfT2wCuHEXz6GYkNR8MkGX3AYnjvk= =SVp7 -----END PGP SIGNATURE----- --ayEhmrFOD2aIcTeb-- --===============0773918283420838285== 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". --===============0773918283420838285==--