2012-10-22 21:02:41 +00:00
|
|
|
/*
|
|
|
|
* RFC 3389 comfort noise generator
|
|
|
|
* Copyright (c) 2012 Martin Storsjo
|
|
|
|
*
|
2012-10-30 13:40:22 +00:00
|
|
|
* This file is part of FFmpeg.
|
2012-10-22 21:02:41 +00:00
|
|
|
*
|
2012-10-30 13:40:22 +00:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2012-10-22 21:02:41 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2012-10-30 13:40:22 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2012-10-22 21:02:41 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-10-30 13:40:22 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2012-10-22 21:02:41 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "libavutil/common.h"
|
2016-03-15 01:28:56 +00:00
|
|
|
#include "libavutil/ffmath.h"
|
2017-08-17 21:40:45 +00:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2012-10-22 21:02:41 +00:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "celp_filters.h"
|
2022-03-16 17:18:28 +00:00
|
|
|
#include "codec_internal.h"
|
2022-08-24 19:28:16 +00:00
|
|
|
#include "decode.h"
|
2012-11-10 12:22:56 +00:00
|
|
|
#include "internal.h"
|
2012-10-22 21:02:41 +00:00
|
|
|
#include "libavutil/lfg.h"
|
|
|
|
|
|
|
|
typedef struct CNGContext {
|
|
|
|
float *refl_coef, *target_refl_coef;
|
|
|
|
float *lpc_coef;
|
|
|
|
int order;
|
|
|
|
int energy, target_energy;
|
2012-10-30 10:03:25 +00:00
|
|
|
int inited;
|
2012-10-22 21:02:41 +00:00
|
|
|
float *filter_out;
|
|
|
|
float *excitation;
|
|
|
|
AVLFG lfg;
|
|
|
|
} CNGContext;
|
|
|
|
|
|
|
|
static av_cold int cng_decode_close(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
CNGContext *p = avctx->priv_data;
|
2014-10-30 22:36:51 +00:00
|
|
|
av_freep(&p->refl_coef);
|
|
|
|
av_freep(&p->target_refl_coef);
|
|
|
|
av_freep(&p->lpc_coef);
|
|
|
|
av_freep(&p->filter_out);
|
|
|
|
av_freep(&p->excitation);
|
2012-10-22 21:02:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int cng_decode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
CNGContext *p = avctx->priv_data;
|
|
|
|
|
|
|
|
avctx->sample_fmt = AV_SAMPLE_FMT_S16;
|
2013-05-07 05:20:32 +00:00
|
|
|
av_channel_layout_uninit(&avctx->ch_layout);
|
|
|
|
avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
|
2012-10-22 21:02:41 +00:00
|
|
|
avctx->sample_rate = 8000;
|
|
|
|
|
|
|
|
p->order = 12;
|
|
|
|
avctx->frame_size = 640;
|
2021-09-14 19:31:53 +00:00
|
|
|
p->refl_coef = av_calloc(p->order, sizeof(*p->refl_coef));
|
|
|
|
p->target_refl_coef = av_calloc(p->order, sizeof(*p->target_refl_coef));
|
|
|
|
p->lpc_coef = av_calloc(p->order, sizeof(*p->lpc_coef));
|
|
|
|
p->filter_out = av_calloc(avctx->frame_size + p->order,
|
2012-10-22 21:02:41 +00:00
|
|
|
sizeof(*p->filter_out));
|
2021-09-14 19:31:53 +00:00
|
|
|
p->excitation = av_calloc(avctx->frame_size, sizeof(*p->excitation));
|
2012-10-22 21:02:41 +00:00
|
|
|
if (!p->refl_coef || !p->target_refl_coef || !p->lpc_coef ||
|
|
|
|
!p->filter_out || !p->excitation) {
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
av_lfg_init(&p->lfg, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void make_lpc_coefs(float *lpc, const float *refl, int order)
|
|
|
|
{
|
|
|
|
float buf[100];
|
|
|
|
float *next, *cur;
|
|
|
|
int m, i;
|
|
|
|
next = buf;
|
|
|
|
cur = lpc;
|
|
|
|
for (m = 0; m < order; m++) {
|
|
|
|
next[m] = refl[m];
|
|
|
|
for (i = 0; i < m; i++)
|
|
|
|
next[i] = cur[i] + refl[m] * cur[m - i - 1];
|
|
|
|
FFSWAP(float*, next, cur);
|
|
|
|
}
|
|
|
|
if (cur != lpc)
|
|
|
|
memcpy(lpc, cur, sizeof(*lpc) * order);
|
|
|
|
}
|
|
|
|
|
2012-10-30 10:03:25 +00:00
|
|
|
static void cng_decode_flush(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
CNGContext *p = avctx->priv_data;
|
|
|
|
p->inited = 0;
|
|
|
|
}
|
|
|
|
|
2022-03-30 19:33:24 +00:00
|
|
|
static int cng_decode_frame(AVCodecContext *avctx, AVFrame *frame,
|
2012-10-30 10:09:57 +00:00
|
|
|
int *got_frame_ptr, AVPacket *avpkt)
|
2012-10-22 21:02:41 +00:00
|
|
|
{
|
|
|
|
CNGContext *p = avctx->priv_data;
|
|
|
|
int buf_size = avpkt->size;
|
|
|
|
int ret, i;
|
|
|
|
int16_t *buf_out;
|
|
|
|
float e = 1.0;
|
|
|
|
float scaling;
|
|
|
|
|
|
|
|
if (avpkt->size) {
|
2012-10-30 09:56:54 +00:00
|
|
|
int dbov = -avpkt->data[0];
|
2015-12-23 18:20:15 +00:00
|
|
|
p->target_energy = 1081109975 * ff_exp10(dbov / 10.0) * 0.75;
|
2012-10-30 10:17:35 +00:00
|
|
|
memset(p->target_refl_coef, 0, p->order * sizeof(*p->target_refl_coef));
|
2012-10-22 21:02:41 +00:00
|
|
|
for (i = 0; i < FFMIN(avpkt->size - 1, p->order); i++) {
|
|
|
|
p->target_refl_coef[i] = (avpkt->data[1 + i] - 127) / 128.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 21:40:45 +00:00
|
|
|
if (avctx->internal->skip_samples > 10 * avctx->frame_size) {
|
|
|
|
avctx->internal->skip_samples = 0;
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2012-10-30 10:03:25 +00:00
|
|
|
if (p->inited) {
|
|
|
|
p->energy = p->energy / 2 + p->target_energy / 2;
|
|
|
|
for (i = 0; i < p->order; i++)
|
|
|
|
p->refl_coef[i] = 0.6 *p->refl_coef[i] + 0.4 * p->target_refl_coef[i];
|
|
|
|
} else {
|
|
|
|
p->energy = p->target_energy;
|
|
|
|
memcpy(p->refl_coef, p->target_refl_coef, p->order * sizeof(*p->refl_coef));
|
|
|
|
p->inited = 1;
|
|
|
|
}
|
2012-10-30 10:26:39 +00:00
|
|
|
make_lpc_coefs(p->lpc_coef, p->refl_coef, p->order);
|
2012-10-22 21:02:41 +00:00
|
|
|
|
|
|
|
for (i = 0; i < p->order; i++)
|
|
|
|
e *= 1.0 - p->refl_coef[i]*p->refl_coef[i];
|
|
|
|
|
|
|
|
scaling = sqrt(e * p->energy / 1081109975);
|
|
|
|
for (i = 0; i < avctx->frame_size; i++) {
|
|
|
|
int r = (av_lfg_get(&p->lfg) & 0xffff) - 0x8000;
|
|
|
|
p->excitation[i] = scaling * r;
|
|
|
|
}
|
|
|
|
ff_celp_lp_synthesis_filterf(p->filter_out + p->order, p->lpc_coef,
|
|
|
|
p->excitation, avctx->frame_size, p->order);
|
|
|
|
|
2012-12-23 22:37:34 +00:00
|
|
|
frame->nb_samples = avctx->frame_size;
|
lavc: factorize ff_{thread_,re,}get_buffer error messages.
Coccinelle profile used:
@@
expression r, ctx, f, loglevel, str, flags;
@@
-if ((r = ff_get_buffer(ctx, f, flags)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_get_buffer(ctx, f, flags)) < 0)
+ return r;
@@
expression r, ctx, f, loglevel, str;
@@
-if ((r = ff_reget_buffer(ctx, f)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_reget_buffer(ctx, f)) < 0)
+ return r;
@@
expression r, ctx, f, loglevel, str, flags;
@@
-if ((r = ff_thread_get_buffer(ctx, f, flags)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_thread_get_buffer(ctx, f, flags)) < 0)
+ return r;
...along with some manual patches for the remaining ones.
2013-03-12 07:41:53 +00:00
|
|
|
if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
|
2012-10-22 21:02:41 +00:00
|
|
|
return ret;
|
2012-12-23 22:37:34 +00:00
|
|
|
buf_out = (int16_t *)frame->data[0];
|
2012-10-22 21:02:41 +00:00
|
|
|
for (i = 0; i < avctx->frame_size; i++)
|
2017-11-02 17:34:09 +00:00
|
|
|
buf_out[i] = av_clip_int16(p->filter_out[i + p->order]);
|
2012-10-22 21:02:41 +00:00
|
|
|
memcpy(p->filter_out, p->filter_out + avctx->frame_size,
|
|
|
|
p->order * sizeof(*p->filter_out));
|
|
|
|
|
2012-12-23 22:37:34 +00:00
|
|
|
*got_frame_ptr = 1;
|
2012-10-22 21:02:41 +00:00
|
|
|
|
|
|
|
return buf_size;
|
|
|
|
}
|
|
|
|
|
2022-03-16 20:09:54 +00:00
|
|
|
const FFCodec ff_comfortnoise_decoder = {
|
|
|
|
.p.name = "comfortnoise",
|
2022-08-29 11:38:02 +00:00
|
|
|
CODEC_LONG_NAME("RFC 3389 comfort noise generator"),
|
2022-03-16 20:09:54 +00:00
|
|
|
.p.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.p.id = AV_CODEC_ID_COMFORT_NOISE,
|
2012-10-22 21:02:41 +00:00
|
|
|
.priv_data_size = sizeof(CNGContext),
|
|
|
|
.init = cng_decode_init,
|
2022-03-30 21:28:24 +00:00
|
|
|
FF_CODEC_DECODE_CB(cng_decode_frame),
|
2012-10-30 10:03:25 +00:00
|
|
|
.flush = cng_decode_flush,
|
2012-10-22 21:02:41 +00:00
|
|
|
.close = cng_decode_close,
|
2022-03-16 20:09:54 +00:00
|
|
|
.p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
|
2012-10-22 21:02:41 +00:00
|
|
|
AV_SAMPLE_FMT_NONE },
|
2022-03-16 20:09:54 +00:00
|
|
|
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
|
2022-07-09 22:05:45 +00:00
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
|
2012-10-22 21:02:41 +00:00
|
|
|
};
|