FFmpeg
Loading...
Searching...
No Matches
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/mem.h"
27#include "avcodec.h"
28#include "celp_filters.h"
29#include "codec_internal.h"
30#include "decode.h"
31#include "internal.h"
32#include "libavutil/lfg.h"
33
44
46{
47 CNGContext *p = avctx->priv_data;
48 av_freep(&p->refl_coef);
49 av_freep(&p->target_refl_coef);
50 av_freep(&p->lpc_coef);
51 av_freep(&p->filter_out);
52 av_freep(&p->excitation);
53 return 0;
54}
55
57{
58 CNGContext *p = avctx->priv_data;
59
63 avctx->sample_rate = 8000;
64
65 p->order = 12;
66 avctx->frame_size = 640;
67 p->refl_coef = av_calloc(p->order, sizeof(*p->refl_coef));
68 p->target_refl_coef = av_calloc(p->order, sizeof(*p->target_refl_coef));
69 p->lpc_coef = av_calloc(p->order, sizeof(*p->lpc_coef));
70 p->filter_out = av_calloc(avctx->frame_size + p->order,
71 sizeof(*p->filter_out));
72 p->excitation = av_calloc(avctx->frame_size, sizeof(*p->excitation));
73 if (!p->refl_coef || !p->target_refl_coef || !p->lpc_coef ||
74 !p->filter_out || !p->excitation) {
75 return AVERROR(ENOMEM);
76 }
77
78 av_lfg_init(&p->lfg, 0);
79
80 return 0;
81}
82
83static void make_lpc_coefs(float *lpc, const float *refl, int order)
84{
85 float buf[100];
86 float *next, *cur;
87 int m, i;
88 next = buf;
89 cur = lpc;
90 for (m = 0; m < order; m++) {
91 next[m] = refl[m];
92 for (i = 0; i < m; i++)
93 next[i] = cur[i] + refl[m] * cur[m - i - 1];
94 FFSWAP(float*, next, cur);
95 }
96 if (cur != lpc)
97 memcpy(lpc, cur, sizeof(*lpc) * order);
98}
99
101{
102 CNGContext *p = avctx->priv_data;
103 p->inited = 0;
104}
105
107 int *got_frame_ptr, AVPacket *avpkt)
108{
109 CNGContext *p = avctx->priv_data;
110 int buf_size = avpkt->size;
111 int ret, i;
112 int16_t *buf_out;
113 float e = 1.0;
114 float scaling;
115
116 if (avpkt->size) {
117 int dbov = -avpkt->data[0];
118 p->target_energy = 1081109975 * ff_exp10(dbov / 10.0) * 0.75;
119 memset(p->target_refl_coef, 0, p->order * sizeof(*p->target_refl_coef));
120 for (i = 0; i < FFMIN(avpkt->size - 1, p->order); i++) {
121 p->target_refl_coef[i] = (avpkt->data[1 + i] - 127) / 128.0;
122 }
123 }
124
125 if (avctx->internal->skip_samples > 10 * avctx->frame_size) {
126 avctx->internal->skip_samples = 0;
127 return AVERROR_INVALIDDATA;
128 }
129
130 if (p->inited) {
131 p->energy = p->energy / 2 + p->target_energy / 2;
132 for (i = 0; i < p->order; i++)
133 p->refl_coef[i] = 0.6 *p->refl_coef[i] + 0.4 * p->target_refl_coef[i];
134 } else {
135 p->energy = p->target_energy;
136 memcpy(p->refl_coef, p->target_refl_coef, p->order * sizeof(*p->refl_coef));
137 p->inited = 1;
138 }
139 make_lpc_coefs(p->lpc_coef, p->refl_coef, p->order);
140
141 for (i = 0; i < p->order; i++)
142 e *= 1.0 - p->refl_coef[i]*p->refl_coef[i];
143
144 scaling = sqrt(e * p->energy / 1081109975);
145 for (i = 0; i < avctx->frame_size; i++) {
146 int r = (av_lfg_get(&p->lfg) & 0xffff) - 0x8000;
147 p->excitation[i] = scaling * r;
148 }
149 ff_celp_lp_synthesis_filterf(p->filter_out + p->order, p->lpc_coef,
150 p->excitation, avctx->frame_size, p->order);
151
152 frame->nb_samples = avctx->frame_size;
153 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
154 return ret;
155 buf_out = (int16_t *)frame->data[0];
156 for (i = 0; i < avctx->frame_size; i++)
157 buf_out[i] = av_clip_int16(p->filter_out[i + p->order]);
158 memcpy(p->filter_out, p->filter_out + avctx->frame_size,
159 p->order * sizeof(*p->filter_out));
160
161 *got_frame_ptr = 1;
162
163 return buf_size;
164}
165
167 .p.name = "comfortnoise",
168 CODEC_LONG_NAME("RFC 3389 comfort noise generator"),
169 .p.type = AVMEDIA_TYPE_AUDIO,
171 .priv_data_size = sizeof(CNGContext),
174 .flush = cng_decode_flush,
175 .close = cng_decode_close,
177 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
178};
const FFCodec ff_comfortnoise_decoder
Definition cngdec.c:166
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
void ff_celp_lp_synthesis_filterf(float *out, const float *filter_coeffs, const float *in, int buffer_length, int filter_length)
LP synthesis filter.
static av_cold void cng_decode_flush(AVCodecContext *avctx)
Definition cngdec.c:100
static av_cold int cng_decode_close(AVCodecContext *avctx)
Definition cngdec.c:45
static av_cold int cng_decode_init(AVCodecContext *avctx)
Definition cngdec.c:56
static void make_lpc_coefs(float *lpc, const float *refl, int order)
Definition cngdec.c:83
static int cng_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition cngdec.c:106
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
common internal and external API header
#define av_clip_int16
Definition common.h:115
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
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: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_COMFORT_NOISE
Definition codec_id.h:514
#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_S16
signed 16 bits
Definition samplefmt.h:58
#define r
Definition input.c:42
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
common internal api header.
#define av_cold
Definition attributes.h:117
#define FFSWAP(type, a, b)
Definition macros.h:52
#define FFMIN(a, b)
Definition macros.h:49
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
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 frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1068
struct AVCodecInternal * internal
Private context used for internal data.
Definition avcodec.h:478
void * priv_data
Definition avcodec.h:470
int skip_samples
Number of audio samples to skip at the start of the next decoded frame.
Definition internal.h:125
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
Context structure for the Lagged Fibonacci PRNG.
Definition lfg.h:33
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
float * lpc_coef
Definition cngdec.c:36
int energy
Definition cngdec.c:38
float * target_refl_coef
Definition cngdec.c:35
float * excitation
Definition cngdec.c:41
int target_energy
Definition cngdec.c:38
int inited
Definition cngdec.c:39
float * filter_out
Definition cngdec.c:40
int order
Definition cngdec.c:37
float * refl_coef
Definition cngdec.c:35
AVLFG lfg
Definition cngdec.c:42
#define av_freep(p)