00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <math.h>
00023
00024 #include "libavutil/common.h"
00025 #include "avcodec.h"
00026 #include "celp_filters.h"
00027 #include "internal.h"
00028 #include "libavutil/lfg.h"
00029
00030 typedef struct CNGContext {
00031 AVFrame avframe;
00032 float *refl_coef, *target_refl_coef;
00033 float *lpc_coef;
00034 int order;
00035 int energy, target_energy;
00036 int inited;
00037 float *filter_out;
00038 float *excitation;
00039 AVLFG lfg;
00040 } CNGContext;
00041
00042 static av_cold int cng_decode_close(AVCodecContext *avctx)
00043 {
00044 CNGContext *p = avctx->priv_data;
00045 av_free(p->refl_coef);
00046 av_free(p->target_refl_coef);
00047 av_free(p->lpc_coef);
00048 av_free(p->filter_out);
00049 av_free(p->excitation);
00050 return 0;
00051 }
00052
00053 static av_cold int cng_decode_init(AVCodecContext *avctx)
00054 {
00055 CNGContext *p = avctx->priv_data;
00056
00057 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00058 avctx->channels = 1;
00059 avctx->sample_rate = 8000;
00060
00061 avcodec_get_frame_defaults(&p->avframe);
00062 avctx->coded_frame = &p->avframe;
00063 p->order = 12;
00064 avctx->frame_size = 640;
00065 p->refl_coef = av_mallocz(p->order * sizeof(*p->refl_coef));
00066 p->target_refl_coef = av_mallocz(p->order * sizeof(*p->target_refl_coef));
00067 p->lpc_coef = av_mallocz(p->order * sizeof(*p->lpc_coef));
00068 p->filter_out = av_mallocz((avctx->frame_size + p->order) *
00069 sizeof(*p->filter_out));
00070 p->excitation = av_mallocz(avctx->frame_size * sizeof(*p->excitation));
00071 if (!p->refl_coef || !p->target_refl_coef || !p->lpc_coef ||
00072 !p->filter_out || !p->excitation) {
00073 cng_decode_close(avctx);
00074 return AVERROR(ENOMEM);
00075 }
00076
00077 av_lfg_init(&p->lfg, 0);
00078
00079 return 0;
00080 }
00081
00082 static void make_lpc_coefs(float *lpc, const float *refl, int order)
00083 {
00084 float buf[100];
00085 float *next, *cur;
00086 int m, i;
00087 next = buf;
00088 cur = lpc;
00089 for (m = 0; m < order; m++) {
00090 next[m] = refl[m];
00091 for (i = 0; i < m; i++)
00092 next[i] = cur[i] + refl[m] * cur[m - i - 1];
00093 FFSWAP(float*, next, cur);
00094 }
00095 if (cur != lpc)
00096 memcpy(lpc, cur, sizeof(*lpc) * order);
00097 }
00098
00099 static void cng_decode_flush(AVCodecContext *avctx)
00100 {
00101 CNGContext *p = avctx->priv_data;
00102 p->inited = 0;
00103 }
00104
00105 static int cng_decode_frame(AVCodecContext *avctx, void *data,
00106 int *got_frame_ptr, AVPacket *avpkt)
00107 {
00108
00109 CNGContext *p = avctx->priv_data;
00110 int buf_size = avpkt->size;
00111 int ret, i;
00112 int16_t *buf_out;
00113 float e = 1.0;
00114 float scaling;
00115
00116 if (avpkt->size) {
00117 int dbov = -avpkt->data[0];
00118 p->target_energy = 1081109975 * pow(10, dbov / 10.0) * 0.75;
00119 memset(p->target_refl_coef, 0, p->order * sizeof(*p->target_refl_coef));
00120 for (i = 0; i < FFMIN(avpkt->size - 1, p->order); i++) {
00121 p->target_refl_coef[i] = (avpkt->data[1 + i] - 127) / 128.0;
00122 }
00123 }
00124
00125 if (p->inited) {
00126 p->energy = p->energy / 2 + p->target_energy / 2;
00127 for (i = 0; i < p->order; i++)
00128 p->refl_coef[i] = 0.6 *p->refl_coef[i] + 0.4 * p->target_refl_coef[i];
00129 } else {
00130 p->energy = p->target_energy;
00131 memcpy(p->refl_coef, p->target_refl_coef, p->order * sizeof(*p->refl_coef));
00132 p->inited = 1;
00133 }
00134 make_lpc_coefs(p->lpc_coef, p->refl_coef, p->order);
00135
00136 for (i = 0; i < p->order; i++)
00137 e *= 1.0 - p->refl_coef[i]*p->refl_coef[i];
00138
00139 scaling = sqrt(e * p->energy / 1081109975);
00140 for (i = 0; i < avctx->frame_size; i++) {
00141 int r = (av_lfg_get(&p->lfg) & 0xffff) - 0x8000;
00142 p->excitation[i] = scaling * r;
00143 }
00144 ff_celp_lp_synthesis_filterf(p->filter_out + p->order, p->lpc_coef,
00145 p->excitation, avctx->frame_size, p->order);
00146
00147 p->avframe.nb_samples = avctx->frame_size;
00148 if ((ret = ff_get_buffer(avctx, &p->avframe)) < 0) {
00149 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00150 return ret;
00151 }
00152 buf_out = (int16_t *)p->avframe.data[0];
00153 for (i = 0; i < avctx->frame_size; i++)
00154 buf_out[i] = p->filter_out[i + p->order];
00155 memcpy(p->filter_out, p->filter_out + avctx->frame_size,
00156 p->order * sizeof(*p->filter_out));
00157
00158 *got_frame_ptr = 1;
00159 *(AVFrame *)data = p->avframe;
00160
00161 return buf_size;
00162 }
00163
00164 AVCodec ff_comfortnoise_decoder = {
00165 .name = "comfortnoise",
00166 .type = AVMEDIA_TYPE_AUDIO,
00167 .id = AV_CODEC_ID_COMFORT_NOISE,
00168 .priv_data_size = sizeof(CNGContext),
00169 .init = cng_decode_init,
00170 .decode = cng_decode_frame,
00171 .flush = cng_decode_flush,
00172 .close = cng_decode_close,
00173 .long_name = NULL_IF_CONFIG_SMALL("RFC 3389 comfort noise generator"),
00174 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
00175 AV_SAMPLE_FMT_NONE },
00176 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
00177 };