FFmpeg  4.4.6
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 "internal.h"
30 #include "libavutil/lfg.h"
31 
32 typedef struct CNGContext {
34  float *lpc_coef;
35  int order;
37  int inited;
38  float *filter_out;
39  float *excitation;
41 } CNGContext;
42 
44 {
45  CNGContext *p = avctx->priv_data;
46  av_freep(&p->refl_coef);
48  av_freep(&p->lpc_coef);
49  av_freep(&p->filter_out);
50  av_freep(&p->excitation);
51  return 0;
52 }
53 
55 {
56  CNGContext *p = avctx->priv_data;
57 
59  avctx->channels = 1;
60  avctx->sample_rate = 8000;
61 
62  p->order = 12;
63  avctx->frame_size = 640;
64  p->refl_coef = av_mallocz_array(p->order, sizeof(*p->refl_coef));
66  p->lpc_coef = av_mallocz_array(p->order, sizeof(*p->lpc_coef));
68  sizeof(*p->filter_out));
69  p->excitation = av_mallocz_array(avctx->frame_size, sizeof(*p->excitation));
70  if (!p->refl_coef || !p->target_refl_coef || !p->lpc_coef ||
71  !p->filter_out || !p->excitation) {
72  return AVERROR(ENOMEM);
73  }
74 
75  av_lfg_init(&p->lfg, 0);
76 
77  return 0;
78 }
79 
80 static void make_lpc_coefs(float *lpc, const float *refl, int order)
81 {
82  float buf[100];
83  float *next, *cur;
84  int m, i;
85  next = buf;
86  cur = lpc;
87  for (m = 0; m < order; m++) {
88  next[m] = refl[m];
89  for (i = 0; i < m; i++)
90  next[i] = cur[i] + refl[m] * cur[m - i - 1];
91  FFSWAP(float*, next, cur);
92  }
93  if (cur != lpc)
94  memcpy(lpc, cur, sizeof(*lpc) * order);
95 }
96 
97 static void cng_decode_flush(AVCodecContext *avctx)
98 {
99  CNGContext *p = avctx->priv_data;
100  p->inited = 0;
101 }
102 
103 static int cng_decode_frame(AVCodecContext *avctx, void *data,
104  int *got_frame_ptr, AVPacket *avpkt)
105 {
106  AVFrame *frame = data;
107  CNGContext *p = avctx->priv_data;
108  int buf_size = avpkt->size;
109  int ret, i;
110  int16_t *buf_out;
111  float e = 1.0;
112  float scaling;
113 
114  if (avpkt->size) {
115  int dbov = -avpkt->data[0];
116  p->target_energy = 1081109975 * ff_exp10(dbov / 10.0) * 0.75;
117  memset(p->target_refl_coef, 0, p->order * sizeof(*p->target_refl_coef));
118  for (i = 0; i < FFMIN(avpkt->size - 1, p->order); i++) {
119  p->target_refl_coef[i] = (avpkt->data[1 + i] - 127) / 128.0;
120  }
121  }
122 
123  if (avctx->internal->skip_samples > 10 * avctx->frame_size) {
124  avctx->internal->skip_samples = 0;
125  return AVERROR_INVALIDDATA;
126  }
127 
128  if (p->inited) {
129  p->energy = p->energy / 2 + p->target_energy / 2;
130  for (i = 0; i < p->order; i++)
131  p->refl_coef[i] = 0.6 *p->refl_coef[i] + 0.4 * p->target_refl_coef[i];
132  } else {
133  p->energy = p->target_energy;
134  memcpy(p->refl_coef, p->target_refl_coef, p->order * sizeof(*p->refl_coef));
135  p->inited = 1;
136  }
138 
139  for (i = 0; i < p->order; i++)
140  e *= 1.0 - p->refl_coef[i]*p->refl_coef[i];
141 
142  scaling = sqrt(e * p->energy / 1081109975);
143  for (i = 0; i < avctx->frame_size; i++) {
144  int r = (av_lfg_get(&p->lfg) & 0xffff) - 0x8000;
145  p->excitation[i] = scaling * r;
146  }
148  p->excitation, avctx->frame_size, p->order);
149 
150  frame->nb_samples = avctx->frame_size;
151  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
152  return ret;
153  buf_out = (int16_t *)frame->data[0];
154  for (i = 0; i < avctx->frame_size; i++)
155  buf_out[i] = av_clip_int16(p->filter_out[i + p->order]);
156  memcpy(p->filter_out, p->filter_out + avctx->frame_size,
157  p->order * sizeof(*p->filter_out));
158 
159  *got_frame_ptr = 1;
160 
161  return buf_size;
162 }
163 
165  .name = "comfortnoise",
166  .long_name = NULL_IF_CONFIG_SMALL("RFC 3389 comfort noise generator"),
167  .type = AVMEDIA_TYPE_AUDIO,
169  .priv_data_size = sizeof(CNGContext),
173  .close = cng_decode_close,
174  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
177  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
179 };
static void flush(AVCodecContext *avctx)
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:925
#define av_cold
Definition: attributes.h:88
Libavcodec external API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
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:84
AVCodec ff_comfortnoise_decoder
Definition: cngdec.c:164
static av_cold int cng_decode_close(AVCodecContext *avctx)
Definition: cngdec.c:43
static int cng_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: cngdec.c:103
static av_cold int cng_decode_init(AVCodecContext *avctx)
Definition: cngdec.c:54
static void cng_decode_flush(AVCodecContext *avctx)
Definition: cngdec.c:97
static void make_lpc_coefs(float *lpc, const float *refl, int order)
Definition: cngdec.c:80
common internal and external API header
#define FFSWAP(type, a, b)
Definition: common.h:108
#define FFMIN(a, b)
Definition: common.h:105
#define av_clip_int16
Definition: common.h:137
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1900
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
static AVFrame * frame
internal math functions header
static av_always_inline double ff_exp10(double x)
Compute 10^x for floating point values.
Definition: ffmath.h:42
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:104
@ AV_CODEC_ID_COMFORT_NOISE
Definition: codec_id.h:485
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
Definition: mem.c:190
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
for(j=16;j >0;--j)
int i
Definition: input.c:407
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:53
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:41
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:49
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
const char data[16]
Definition: mxf.c:142
main external API structure.
Definition: avcodec.h:536
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1204
int sample_rate
samples per second
Definition: avcodec.h:1196
int channels
number of audio channels
Definition: avcodec.h:1197
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1216
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:571
void * priv_data
Definition: avcodec.h:563
int skip_samples
Number of audio samples to skip at the start of the next decoded frame.
Definition: internal.h:175
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:384
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
float * lpc_coef
Definition: cngdec.c:34
int energy
Definition: cngdec.c:36
float * target_refl_coef
Definition: cngdec.c:33
float * excitation
Definition: cngdec.c:39
int target_energy
Definition: cngdec.c:36
int inited
Definition: cngdec.c:37
float * filter_out
Definition: cngdec.c:38
int order
Definition: cngdec.c:35
float * refl_coef
Definition: cngdec.c:33
AVLFG lfg
Definition: cngdec.c:40
#define av_freep(p)
const char * r
Definition: vf_curves.c:116