FFmpeg
Loading...
Searching...
No Matches
g728dec.c
Go to the documentation of this file.
1/*
2 * G.728 decoder
3 * Copyright (c) 2025 Peter Ross
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "avcodec.h"
23#include "celp_filters.h"
24#include "codec_internal.h"
25#include "decode.h"
26#include "get_bits.h"
27#include "g728data.h"
28#include "lpc_functions.h"
29#include "ra288.h"
30#include "libavutil/float_dsp.h"
31#include "libavutil/mem.h"
33#include "libavutil/opt.h"
34#include "libavutil/thread.h"
35
36#define MAX_BACKWARD_FILTER_ORDER LPC
37#define MAX_BACKWARD_FILTER_LEN NFRSZ
38#define MAX_BACKWARD_FILTER_NONREC NONR
39#define ATTEN 0.75f
40#include "g728_template.c"
41
42#define LPCW 10 /* Perceptual weighting filter order */
43#define GOFF 32.0f /* Log-gain offset value */
44
45static float g728_gq_db[8];
46static float g728_y_db[128];
47static DECLARE_ALIGNED(32, float, g728_wnr_r)[FFALIGN(NSBSZ,16)];
48static DECLARE_ALIGNED(32, float, g728_wnrg_r)[FFALIGN(NSBGSZ, 16)];
49static DECLARE_ALIGNED(32, float, g728_facv_f)[FFALIGN(LPC, 16)];
50
52{
53 for(int i = 0; i < FF_ARRAY_ELEMS(amptable); i++)
54 g728_gq_db[i] = 10.0f*log10f(amptable[i] * amptable[i]);
55
56 for (int i = 0; i < FF_ARRAY_ELEMS(codetable); i++) {
57 float cby[IDIM];
58 for (int j = 0; j < IDIM; j++)
59 cby[j] = codetable[i][j] * (1.0f/(1<<11));
60 g728_y_db[i] = 10.0f*log10f(ff_scalarproduct_float_c(cby, cby, IDIM) / IDIM);
61 }
62
63 for (int i = 0; i < NSBSZ; i++)
64 g728_wnr_r[i] = g728_wnr[NSBSZ - 1 - i] * (1.0f/(1<<15));
65 for (int i = 0; i < NSBGSZ; i++)
66 g728_wnrg_r[i] = g728_wnrg[NSBGSZ - 1 - i] * (1.0f/(1<<15));
67 for (int i = 0; i < LPC; i++)
68 g728_facv_f[i] = g728_facv[i] * (1.0f/(1<<14));
69}
70
71typedef struct {
73 int valid;
74 float a[LPC];
75 DECLARE_ALIGNED(32, float, sb)[NSBSZ];
77 DECLARE_ALIGNED(32, float, gp)[FFALIGN(LPCLG, 16)];
78 DECLARE_ALIGNED(32, float, atmp)[FFALIGN(LPC, 16)];
79 float rexp[LPC + 1];
80 float rexpg[LPCLG + 1];
81 float r[LPC + 1];
82 float alpha;
84
86{
87 static AVOnce init_static_once = AV_ONCE_INIT;
88 G728Context *s = avctx->priv_data;
89
91 if (!s->fdsp)
92 return AVERROR(ENOMEM);
93
94 s->gp[0] = -1.0f;
95 for (int i = 0; i < NUPDATE; i++)
96 s->sbg[NSBGSZ - 1 -i] = -GOFF;
97
99 if (!avctx->sample_rate)
100 avctx->sample_rate = 8000;
101
104
105 ff_thread_once(&init_static_once, g728_init_static_data);
106 return 0;
107}
108
110{
111 G728Context *s = avctx->priv_data;
112 av_freep(&s->fdsp);
113 return 0;
114}
115
117 int order, int n, int non_rec, float *out,
118 const float *hist, float *out2, const float *window)
119{
120 do_hybrid_window(fdsp->vector_fmul, order, n, non_rec, out, hist, out2, window);
121 return out[order] != 0.0f;
122}
123
124static void decode_frame(G728Context *s, GetBitContext *gb, float *dst)
125{
126 float *gstate = s->sbg + NSBGSZ - 2;
127
128 for (int idx = 0; idx < NUPDATE; idx++) {
129 DECLARE_ALIGNED(32, float, et)[IDIM];
130 float *statelpc = s->sb + NSBSZ - NFRSZ + idx*IDIM;
131 float gain, gain_db;
132 int is, ig;
133
134 gain_db = 0.0f;
135 for (int i = 0; i < LPCLG; i++)
136 gain_db -= s->gp[i] * gstate[-i];
137 gain_db = av_clipf(gain_db, -GOFF, 28.0f);
138
139 is = get_bits(gb, 7); // shape index
140 ig = get_bits(gb, 3); // gain index
141
142 gain = powf(10.0f, (gain_db + GOFF) * .05f) * amptable[ig] * (1.0f/(1<<11));
143 for (int i = 0; i < IDIM; i++)
144 et[i] = codetable[is][i] * gain;
145
146 ff_celp_lp_synthesis_filterf(statelpc, s->a, et, IDIM, LPC);
147
148 for (int i = 0; i < IDIM; i++) {
149 statelpc[i] = av_clipf(statelpc[i], -4095.0f, 4095.0f);
150 dst[idx*IDIM + i] = statelpc[i] * (1.0f/(1<<12));
151 }
152
153 gstate++;
154 *gstate = FFMAX(-GOFF, g728_gq_db[ig] + g728_y_db[is] + gain_db);
155
156 if (idx == 0) {
157 DECLARE_ALIGNED(32, float, gptmp)[FFALIGN(LPCLG, 16)];
158 if (s->valid && (s->valid = !compute_lpc_coefs(s->r + 1, LPCW, LPC, s->atmp, 0, 0, 1, &s->alpha))) {
159 s->fdsp->vector_fmul(s->atmp, s->atmp, g728_facv_f, FFALIGN(LPC, 16));
160 }
161 if (hybrid_window(s->fdsp, LPCLG, NUPDATE, NONRLG, s->r, s->sbg, s->rexpg, g728_wnrg_r) &&
162 !compute_lpc_coefs(s->r, 0, LPCLG, gptmp, 0, 0, 1, &s->alpha)) {
163 s->fdsp->vector_fmul(s->gp, gptmp, gain_bw_tab, FFALIGN(LPCLG, 16));
164 }
165 memmove(s->sbg, s->sbg + NUPDATE, sizeof(float)*(LPCLG + NONRLG));
166 gstate = s->sbg + NSBGSZ - 1 - NUPDATE;
167 } else if (idx == 1) {
168 if (s->valid)
169 memcpy(s->a, s->atmp, sizeof(float)*LPC);
170 }
171 }
172
173 s->valid = 0;
174 if (hybrid_window(s->fdsp, LPC, NFRSZ, NONR, s->r, s->sb, s->rexp, g728_wnr_r)) {
175 s->valid = !compute_lpc_coefs(s->r, 0, LPCW, s->atmp, 0, 0, 1, &s->alpha);
176 }
177
178 memmove(s->sb, s->sb + NFRSZ, sizeof(float)*(LPC + NONR));
179}
180
182 int *got_frame_ptr, AVPacket *avpkt)
183{
184 G728Context *s = avctx->priv_data;
185 GetBitContext gb;
186 int ret;
187 int nb_frames = avpkt->size / 5;
188
189 if (!nb_frames)
190 return AVERROR_INVALIDDATA;
191
192 if ((ret = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
193 return ret;
194
195#define SAMPLES_PER_FRAME 20
196
197 frame->nb_samples = nb_frames * SAMPLES_PER_FRAME;
198 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
199 return ret;
200
201 for (int i = 0; i < nb_frames; i++)
202 decode_frame(s, &gb, (float *)frame->data[0] + i * 20);
203
204 *got_frame_ptr = 1;
205
206 return nb_frames * 5;
207}
208
210 .p.name = "g728",
211 CODEC_LONG_NAME("G.728)"),
212 .p.type = AVMEDIA_TYPE_AUDIO,
213 .p.id = AV_CODEC_ID_G728,
214 .priv_data_size = sizeof(G728Context),
218 .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
220};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
const FFCodec ff_g728_decoder
Definition g728dec.c:209
static FILE * out
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
#define SAMPLES_PER_FRAME
Definition atrac3.c:62
Libavcodec external API header.
#define is(width, name, range_min, range_max, subs,...)
Definition cbs_h264.c:78
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
void ff_celp_lp_synthesis_filterf(float *out, const float *filter_coeffs, const float *in, int buffer_length, int filter_length)
LP synthesis filter.
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define av_clipf
Definition common.h:145
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static SDL_Window * window
Definition ffplay.c:365
float ff_scalarproduct_float_c(const float *v1, const float *v2, int len)
Return the scalar product of two vectors of floats.
static void do_hybrid_window(void(*vector_fmul)(float *dst, const float *src0, const float *src1, int len), int order, int n, int non_rec, float *out, const float *hist, float *out2, const float *window)
Hybrid window filtering, see blocks 36 and 49 of the G.728 specification.
#define NSBGSZ
Definition g728data.h:36
static const uint16_t g728_wnr[NSBSZ]
Definition g728data.h:39
#define LPC
Definition g728data.h:28
static const uint16_t g728_wnrg[NSBGSZ]
Definition g728data.h:54
#define NFRSZ
Definition g728data.h:30
#define NONRLG
Definition g728data.h:32
#define NUPDATE
Definition g728data.h:33
#define IDIM
Definition g728data.h:27
#define LPCLG
Definition g728data.h:29
static const uint16_t g728_facv[LPC]
Definition g728data.h:62
#define NONR
Definition g728data.h:31
#define NSBSZ
Definition g728data.h:35
bitstream reader API header.
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition avcodec.h:322
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition codec.h:94
@ AV_CODEC_ID_G728
Definition codec_id.h:560
#define AV_CHANNEL_LAYOUT_MONO
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_FLT
float
Definition samplefmt.h:60
static int g728_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition g728dec.c:181
#define GOFF
Definition g728dec.c:43
static int hybrid_window(AVFloatDSPContext *fdsp, int order, int n, int non_rec, float *out, const float *hist, float *out2, const float *window)
Definition g728dec.c:116
static void decode_frame(G728Context *s, GetBitContext *gb, float *dst)
Definition g728dec.c:124
static av_cold int g728_decode_init(AVCodecContext *avctx)
Definition g728dec.c:85
static float g728_gq_db[8]
Definition g728dec.c:45
static float g728_facv_f[FFALIGN(LPC, 16)]
Definition g728dec.c:49
static float g728_y_db[128]
Definition g728dec.c:46
static av_cold int g728_decode_close(AVCodecContext *avctx)
Definition g728dec.c:109
static float g728_wnr_r[FFALIGN(NSBSZ, 16)]
Definition g728dec.c:47
static av_cold void g728_init_static_data(void)
Definition g728dec.c:51
static float g728_wnrg_r[FFALIGN(NSBGSZ, 16)]
Definition g728dec.c:48
#define LPCW
Definition g728dec.c:42
#define av_cold
Definition attributes.h:117
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition float_dsp.c:135
#define AVOnce
Definition thread.h:202
static int ff_thread_once(char *control, void(*routine)(void))
Definition thread.h:205
#define AV_ONCE_INIT
Definition thread.h:203
#define log10f(x)
Definition libm.h:416
#define powf(x, y)
Definition libm.h:52
static int compute_lpc_coefs(const LPC_TYPE *autoc, int i, int max_order, LPC_TYPE *lpc, int lpc_stride, int fail, int normalize, LPC_TYPE *err_ptr)
Levinson-Durbin recursion.
#define FFMAX(a, b)
Definition macros.h:47
#define FFALIGN(x, a)
Definition macros.h:78
Memory handling functions.
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
AVOptions.
static const float gain_bw_tab[FFALIGN(10, 16)]
gain bandwidth broadening table
Definition ra288.h:144
static const int16_t codetable[128][5]
Definition ra288.h:34
static const float amptable[8]
Definition ra288.h:29
#define FF_ARRAY_ELEMS(a)
An AVChannelLayout holds information about the channel layout of audio data.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
int sample_rate
samples per second
Definition avcodec.h:1040
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
void * priv_data
Definition avcodec.h:470
void(* vector_fmul)(float *dst, const float *src0, const float *src1, int len)
Calculate the entry wise product of two vectors of floats and store the result in a vector of floats.
Definition float_dsp.h:38
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
AVFloatDSPContext * fdsp
Definition g728dec.c:72
float r[LPC+1]
Definition g728dec.c:81
float rexpg[LPCLG+1]
Definition g728dec.c:80
float atmp[FFALIGN(LPC, 16)]
Definition g728dec.c:78
int valid
Definition g728dec.c:73
float rexp[LPC+1]
Definition g728dec.c:79
float a[LPC]
Definition g728dec.c:74
float alpha
Definition g728dec.c:82
float gp[FFALIGN(LPCLG, 16)]
Definition g728dec.c:77
float sb[NSBSZ]
Definition g728dec.c:75
float sbg[NSBGSZ]
Definition g728dec.c:76
#define av_freep(p)