FFmpeg
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"
32 #include "libavutil/mem_internal.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 
45 static float g728_gq_db[8];
46 static float g728_y_db[128];
47 static DECLARE_ALIGNED(32, float, g728_wnr_r)[FFALIGN(NSBSZ,16)];
48 static DECLARE_ALIGNED(32, float, g728_wnrg_r)[FFALIGN(NSBGSZ, 16)];
49 static DECLARE_ALIGNED(32, float, g728_facv_f)[FFALIGN(LPC, 16)];
50 
51 static av_cold void g728_init_static_data(void)
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 
71 typedef struct {
73  int valid;
74  float a[LPC];
75  DECLARE_ALIGNED(32, float, sb)[NSBSZ];
76  DECLARE_ALIGNED(32, float, sbg)[NSBGSZ];
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;
83 } G728Context;
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 
124 static 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 };
g728_wnrg
static const uint16_t g728_wnrg[NSBGSZ]
Definition: g728data.h:54
G728Context::fdsp
AVFloatDSPContext * fdsp
Definition: g728dec.c:72
r
const char * r
Definition: vf_curves.c:127
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
g728_wnr_r
static float g728_wnr_r[FFALIGN(NSBSZ, 16)]
Definition: g728dec.c:47
mem_internal.h
out
FILE * out
Definition: movenc.c:55
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1024
is
The official guide to swscale for confused that is
Definition: swscale.txt:28
thread.h
codetable
static const int16_t codetable[128][5]
Definition: ra288.h:34
g728_decode_frame
static int g728_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: g728dec.c:181
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:427
AVPacket::data
uint8_t * data
Definition: packet.h:558
FFCodec
Definition: codec_internal.h:127
g728_decode_close
static av_cold int g728_decode_close(AVCodecContext *avctx)
Definition: g728dec.c:109
SAMPLES_PER_FRAME
#define SAMPLES_PER_FRAME
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
NFRSZ
#define NFRSZ
Definition: g728data.h:30
ff_celp_lp_synthesis_filterf
void ff_celp_lp_synthesis_filterf(float *out, const float *filter_coeffs, const float *in, int buffer_length, int filter_length)
LP synthesis filter.
Definition: celp_filters.c:85
NONRLG
#define NONRLG
Definition: g728data.h:32
ff_g728_decoder
const FFCodec ff_g728_decoder
Definition: g728dec.c:209
close
static av_cold void close(AVCodecParserContext *s)
Definition: apv_parser.c:135
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:333
window
static SDL_Window * window
Definition: ffplay.c:361
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1039
log10f
#define log10f(x)
Definition: libm.h:416
GetBitContext
Definition: get_bits.h:109
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:488
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:205
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:539
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:346
s
#define s(width, name)
Definition: cbs_vp9.c:198
G728Context::alpha
float alpha
Definition: g728dec.c:82
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:201
NUPDATE
#define NUPDATE
Definition: g728data.h:33
decode.h
get_bits.h
NONR
#define NONR
Definition: g728data.h:31
LPCLG
#define LPCLG
Definition: g728data.h:29
g728data.h
gain_bw_tab
static const float gain_bw_tab[FFALIGN(10, 16)]
gain bandwidth broadening table
Definition: ra288.h:144
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:331
g728_template.c
if
if(ret)
Definition: filter_design.txt:179
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:203
LPCW
#define LPCW
Definition: g728dec.c:42
GOFF
#define GOFF
Definition: g728dec.c:43
celp_filters.h
av_clipf
av_clipf
Definition: af_crystalizer.c:122
AV_CODEC_ID_G728
@ AV_CODEC_ID_G728
Definition: codec_id.h:558
AVOnce
#define AVOnce
Definition: thread.h:202
float_dsp.h
g728_facv_f
static float g728_facv_f[FFALIGN(LPC, 16)]
Definition: g728dec.c:49
ff_scalarproduct_float_c
float ff_scalarproduct_float_c(const float *v1, const float *v2, int len)
Return the scalar product of two vectors of floats.
Definition: float_scalarproduct.c:24
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:91
f
f
Definition: af_crystalizer.c:122
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1720
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:559
powf
#define powf(x, y)
Definition: libm.h:52
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:319
codec_internal.h
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem_internal.h:104
AVFloatDSPContext::vector_fmul
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
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1031
g728_wnr
static const uint16_t g728_wnr[NSBSZ]
Definition: g728data.h:39
AVFloatDSPContext
Definition: float_dsp.h:24
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
do_hybrid_window
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.
Definition: g728_template.c:40
g728_y_db
static float g728_y_db[128]
Definition: g728dec.c:46
g728_facv
static const uint16_t g728_facv[LPC]
Definition: g728data.h:62
g728_decode_init
static av_cold int g728_decode_init(AVCodecContext *avctx)
Definition: g728dec.c:85
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
compute_lpc_coefs
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.
Definition: lpc_functions.h:54
NSBGSZ
#define NSBGSZ
Definition: g728data.h:36
IDIM
#define IDIM
Definition: g728data.h:27
hybrid_window
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
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
G728Context
Definition: g728dec.c:71
decode_frame
static void decode_frame(G728Context *s, GetBitContext *gb, float *dst)
Definition: g728dec.c:124
LPC
#define LPC
Definition: g728data.h:28
g728_gq_db
static float g728_gq_db[8]
Definition: g728dec.c:45
NSBSZ
#define NSBSZ
Definition: g728data.h:35
avcodec.h
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:265
AVCodecContext
main external API structure.
Definition: avcodec.h:431
lpc_functions.h
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:442
G728Context::valid
int valid
Definition: g728dec.c:73
mem.h
AV_CODEC_FLAG_BITEXACT
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:322
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:394
amptable
static const float amptable[8]
Definition: ra288.h:29
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVPacket
This structure stores compressed data.
Definition: packet.h:535
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:458
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
avpriv_float_dsp_alloc
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:135
g728_init_static_data
static av_cold void g728_init_static_data(void)
Definition: g728dec.c:51
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
g728_wnrg_r
static float g728_wnrg_r[FFALIGN(NSBGSZ, 16)]
Definition: g728dec.c:48
ra288.h
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:60