FFmpeg
cngdec.c
Go to the documentation of this file.
1 /*
2  * RFC 3389 comfort noise generator
3  * Copyright (c) 2012 Martin Storsjo
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 <math.h>
23 
24 #include "libavutil/common.h"
25 #include "libavutil/ffmath.h"
26 #include "libavutil/intreadwrite.h"
27 #include "avcodec.h"
28 #include "celp_filters.h"
29 #include "codec_internal.h"
30 #include "internal.h"
31 #include "libavutil/lfg.h"
32 
33 typedef struct CNGContext {
35  float *lpc_coef;
36  int order;
38  int inited;
39  float *filter_out;
40  float *excitation;
42 } CNGContext;
43 
45 {
46  CNGContext *p = avctx->priv_data;
47  av_freep(&p->refl_coef);
49  av_freep(&p->lpc_coef);
50  av_freep(&p->filter_out);
51  av_freep(&p->excitation);
52  return 0;
53 }
54 
56 {
57  CNGContext *p = avctx->priv_data;
58 
62  avctx->sample_rate = 8000;
63 
64  p->order = 12;
65  avctx->frame_size = 640;
66  p->refl_coef = av_calloc(p->order, sizeof(*p->refl_coef));
67  p->target_refl_coef = av_calloc(p->order, sizeof(*p->target_refl_coef));
68  p->lpc_coef = av_calloc(p->order, sizeof(*p->lpc_coef));
69  p->filter_out = av_calloc(avctx->frame_size + p->order,
70  sizeof(*p->filter_out));
71  p->excitation = av_calloc(avctx->frame_size, sizeof(*p->excitation));
72  if (!p->refl_coef || !p->target_refl_coef || !p->lpc_coef ||
73  !p->filter_out || !p->excitation) {
74  return AVERROR(ENOMEM);
75  }
76 
77  av_lfg_init(&p->lfg, 0);
78 
79  return 0;
80 }
81 
82 static void make_lpc_coefs(float *lpc, const float *refl, int order)
83 {
84  float buf[100];
85  float *next, *cur;
86  int m, i;
87  next = buf;
88  cur = lpc;
89  for (m = 0; m < order; m++) {
90  next[m] = refl[m];
91  for (i = 0; i < m; i++)
92  next[i] = cur[i] + refl[m] * cur[m - i - 1];
93  FFSWAP(float*, next, cur);
94  }
95  if (cur != lpc)
96  memcpy(lpc, cur, sizeof(*lpc) * order);
97 }
98 
99 static void cng_decode_flush(AVCodecContext *avctx)
100 {
101  CNGContext *p = avctx->priv_data;
102  p->inited = 0;
103 }
104 
106  int *got_frame_ptr, AVPacket *avpkt)
107 {
108  CNGContext *p = avctx->priv_data;
109  int buf_size = avpkt->size;
110  int ret, i;
111  int16_t *buf_out;
112  float e = 1.0;
113  float scaling;
114 
115  if (avpkt->size) {
116  int dbov = -avpkt->data[0];
117  p->target_energy = 1081109975 * ff_exp10(dbov / 10.0) * 0.75;
118  memset(p->target_refl_coef, 0, p->order * sizeof(*p->target_refl_coef));
119  for (i = 0; i < FFMIN(avpkt->size - 1, p->order); i++) {
120  p->target_refl_coef[i] = (avpkt->data[1 + i] - 127) / 128.0;
121  }
122  }
123 
124  if (avctx->internal->skip_samples > 10 * avctx->frame_size) {
125  avctx->internal->skip_samples = 0;
126  return AVERROR_INVALIDDATA;
127  }
128 
129  if (p->inited) {
130  p->energy = p->energy / 2 + p->target_energy / 2;
131  for (i = 0; i < p->order; i++)
132  p->refl_coef[i] = 0.6 *p->refl_coef[i] + 0.4 * p->target_refl_coef[i];
133  } else {
134  p->energy = p->target_energy;
135  memcpy(p->refl_coef, p->target_refl_coef, p->order * sizeof(*p->refl_coef));
136  p->inited = 1;
137  }
139 
140  for (i = 0; i < p->order; i++)
141  e *= 1.0 - p->refl_coef[i]*p->refl_coef[i];
142 
143  scaling = sqrt(e * p->energy / 1081109975);
144  for (i = 0; i < avctx->frame_size; i++) {
145  int r = (av_lfg_get(&p->lfg) & 0xffff) - 0x8000;
146  p->excitation[i] = scaling * r;
147  }
149  p->excitation, avctx->frame_size, p->order);
150 
151  frame->nb_samples = avctx->frame_size;
152  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
153  return ret;
154  buf_out = (int16_t *)frame->data[0];
155  for (i = 0; i < avctx->frame_size; i++)
156  buf_out[i] = av_clip_int16(p->filter_out[i + p->order]);
157  memcpy(p->filter_out, p->filter_out + avctx->frame_size,
158  p->order * sizeof(*p->filter_out));
159 
160  *got_frame_ptr = 1;
161 
162  return buf_size;
163 }
164 
166  .p.name = "comfortnoise",
167  .p.long_name = NULL_IF_CONFIG_SMALL("RFC 3389 comfort noise generator"),
168  .p.type = AVMEDIA_TYPE_AUDIO,
170  .priv_data_size = sizeof(CNGContext),
173  .flush = cng_decode_flush,
174  .close = cng_decode_close,
175  .p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
177  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
178  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
179 };
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1035
CNGContext::target_energy
int target_energy
Definition: cngdec.c:37
ff_exp10
static av_always_inline double ff_exp10(double x)
Compute 10^x for floating point values.
Definition: ffmath.h:42
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:41
r
const char * r
Definition: vf_curves.c:116
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
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1007
av_lfg_init
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
AVCodecInternal::skip_samples
int skip_samples
Number of audio samples to skip at the start of the next decoded frame.
Definition: internal.h:127
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:374
make_lpc_coefs
static void make_lpc_coefs(float *lpc, const float *refl, int order)
Definition: cngdec.c:82
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:353
FFCodec
Definition: codec_internal.h:118
CNGContext::lpc_coef
float * lpc_coef
Definition: cngdec.c:35
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
init
static int init
Definition: av_tx.c:47
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:122
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2065
CNGContext::inited
int inited
Definition: cngdec.c:38
av_cold
#define av_cold
Definition: attributes.h:90
CNGContext::excitation
float * excitation
Definition: cngdec.c:40
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:260
intreadwrite.h
av_lfg_get
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:53
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
lfg.h
cng_decode_frame
static int cng_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: cngdec.c:105
ff_comfortnoise_decoder
const FFCodec ff_comfortnoise_decoder
Definition: cngdec.c:165
av_clip_int16
#define av_clip_int16
Definition: common.h:110
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:433
CNGContext
Definition: cngdec.c:33
CNGContext::filter_out
float * filter_out
Definition: cngdec.c:39
celp_filters.h
cng_decode_close
static av_cold int cng_decode_close(AVCodecContext *avctx)
Definition: cngdec.c:44
cng_decode_flush
static void cng_decode_flush(AVCodecContext *avctx)
Definition: cngdec.c:99
CNGContext::refl_coef
float * refl_coef
Definition: cngdec.c:34
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:630
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:109
AVLFG
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1462
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:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:290
codec_internal.h
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1023
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
AV_CODEC_ID_COMFORT_NOISE
@ AV_CODEC_ID_COMFORT_NOISE
Definition: codec_id.h:489
CNGContext::energy
int energy
Definition: cngdec.c:37
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
CNGContext::lfg
AVLFG lfg
Definition: cngdec.c:41
common.h
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:211
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:272
avcodec.h
CNGContext::order
int order
Definition: cngdec.c:36
ret
ret
Definition: filter_design.txt:187
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
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:264
AVCodecContext
main external API structure.
Definition: avcodec.h:398
cng_decode_init
static av_cold int cng_decode_init(AVCodecContext *avctx)
Definition: cngdec.c:55
ffmath.h
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:425
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
CNGContext::target_refl_coef
float * target_refl_coef
Definition: cngdec.c:34