annotate ffmpeg/libavcodec/cngdec.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * RFC 3389 comfort noise generator
yading@10 3 * Copyright (c) 2012 Martin Storsjo
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 #include <math.h>
yading@10 23
yading@10 24 #include "libavutil/common.h"
yading@10 25 #include "avcodec.h"
yading@10 26 #include "celp_filters.h"
yading@10 27 #include "internal.h"
yading@10 28 #include "libavutil/lfg.h"
yading@10 29
yading@10 30 typedef struct CNGContext {
yading@10 31 float *refl_coef, *target_refl_coef;
yading@10 32 float *lpc_coef;
yading@10 33 int order;
yading@10 34 int energy, target_energy;
yading@10 35 int inited;
yading@10 36 float *filter_out;
yading@10 37 float *excitation;
yading@10 38 AVLFG lfg;
yading@10 39 } CNGContext;
yading@10 40
yading@10 41 static av_cold int cng_decode_close(AVCodecContext *avctx)
yading@10 42 {
yading@10 43 CNGContext *p = avctx->priv_data;
yading@10 44 av_free(p->refl_coef);
yading@10 45 av_free(p->target_refl_coef);
yading@10 46 av_free(p->lpc_coef);
yading@10 47 av_free(p->filter_out);
yading@10 48 av_free(p->excitation);
yading@10 49 return 0;
yading@10 50 }
yading@10 51
yading@10 52 static av_cold int cng_decode_init(AVCodecContext *avctx)
yading@10 53 {
yading@10 54 CNGContext *p = avctx->priv_data;
yading@10 55
yading@10 56 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
yading@10 57 avctx->channels = 1;
yading@10 58 avctx->sample_rate = 8000;
yading@10 59
yading@10 60 p->order = 12;
yading@10 61 avctx->frame_size = 640;
yading@10 62 p->refl_coef = av_mallocz(p->order * sizeof(*p->refl_coef));
yading@10 63 p->target_refl_coef = av_mallocz(p->order * sizeof(*p->target_refl_coef));
yading@10 64 p->lpc_coef = av_mallocz(p->order * sizeof(*p->lpc_coef));
yading@10 65 p->filter_out = av_mallocz((avctx->frame_size + p->order) *
yading@10 66 sizeof(*p->filter_out));
yading@10 67 p->excitation = av_mallocz(avctx->frame_size * sizeof(*p->excitation));
yading@10 68 if (!p->refl_coef || !p->target_refl_coef || !p->lpc_coef ||
yading@10 69 !p->filter_out || !p->excitation) {
yading@10 70 cng_decode_close(avctx);
yading@10 71 return AVERROR(ENOMEM);
yading@10 72 }
yading@10 73
yading@10 74 av_lfg_init(&p->lfg, 0);
yading@10 75
yading@10 76 return 0;
yading@10 77 }
yading@10 78
yading@10 79 static void make_lpc_coefs(float *lpc, const float *refl, int order)
yading@10 80 {
yading@10 81 float buf[100];
yading@10 82 float *next, *cur;
yading@10 83 int m, i;
yading@10 84 next = buf;
yading@10 85 cur = lpc;
yading@10 86 for (m = 0; m < order; m++) {
yading@10 87 next[m] = refl[m];
yading@10 88 for (i = 0; i < m; i++)
yading@10 89 next[i] = cur[i] + refl[m] * cur[m - i - 1];
yading@10 90 FFSWAP(float*, next, cur);
yading@10 91 }
yading@10 92 if (cur != lpc)
yading@10 93 memcpy(lpc, cur, sizeof(*lpc) * order);
yading@10 94 }
yading@10 95
yading@10 96 static void cng_decode_flush(AVCodecContext *avctx)
yading@10 97 {
yading@10 98 CNGContext *p = avctx->priv_data;
yading@10 99 p->inited = 0;
yading@10 100 }
yading@10 101
yading@10 102 static int cng_decode_frame(AVCodecContext *avctx, void *data,
yading@10 103 int *got_frame_ptr, AVPacket *avpkt)
yading@10 104 {
yading@10 105 AVFrame *frame = data;
yading@10 106 CNGContext *p = avctx->priv_data;
yading@10 107 int buf_size = avpkt->size;
yading@10 108 int ret, i;
yading@10 109 int16_t *buf_out;
yading@10 110 float e = 1.0;
yading@10 111 float scaling;
yading@10 112
yading@10 113 if (avpkt->size) {
yading@10 114 int dbov = -avpkt->data[0];
yading@10 115 p->target_energy = 1081109975 * pow(10, dbov / 10.0) * 0.75;
yading@10 116 memset(p->target_refl_coef, 0, p->order * sizeof(*p->target_refl_coef));
yading@10 117 for (i = 0; i < FFMIN(avpkt->size - 1, p->order); i++) {
yading@10 118 p->target_refl_coef[i] = (avpkt->data[1 + i] - 127) / 128.0;
yading@10 119 }
yading@10 120 }
yading@10 121
yading@10 122 if (p->inited) {
yading@10 123 p->energy = p->energy / 2 + p->target_energy / 2;
yading@10 124 for (i = 0; i < p->order; i++)
yading@10 125 p->refl_coef[i] = 0.6 *p->refl_coef[i] + 0.4 * p->target_refl_coef[i];
yading@10 126 } else {
yading@10 127 p->energy = p->target_energy;
yading@10 128 memcpy(p->refl_coef, p->target_refl_coef, p->order * sizeof(*p->refl_coef));
yading@10 129 p->inited = 1;
yading@10 130 }
yading@10 131 make_lpc_coefs(p->lpc_coef, p->refl_coef, p->order);
yading@10 132
yading@10 133 for (i = 0; i < p->order; i++)
yading@10 134 e *= 1.0 - p->refl_coef[i]*p->refl_coef[i];
yading@10 135
yading@10 136 scaling = sqrt(e * p->energy / 1081109975);
yading@10 137 for (i = 0; i < avctx->frame_size; i++) {
yading@10 138 int r = (av_lfg_get(&p->lfg) & 0xffff) - 0x8000;
yading@10 139 p->excitation[i] = scaling * r;
yading@10 140 }
yading@10 141 ff_celp_lp_synthesis_filterf(p->filter_out + p->order, p->lpc_coef,
yading@10 142 p->excitation, avctx->frame_size, p->order);
yading@10 143
yading@10 144 frame->nb_samples = avctx->frame_size;
yading@10 145 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
yading@10 146 return ret;
yading@10 147 buf_out = (int16_t *)frame->data[0];
yading@10 148 for (i = 0; i < avctx->frame_size; i++)
yading@10 149 buf_out[i] = p->filter_out[i + p->order];
yading@10 150 memcpy(p->filter_out, p->filter_out + avctx->frame_size,
yading@10 151 p->order * sizeof(*p->filter_out));
yading@10 152
yading@10 153 *got_frame_ptr = 1;
yading@10 154
yading@10 155 return buf_size;
yading@10 156 }
yading@10 157
yading@10 158 AVCodec ff_comfortnoise_decoder = {
yading@10 159 .name = "comfortnoise",
yading@10 160 .type = AVMEDIA_TYPE_AUDIO,
yading@10 161 .id = AV_CODEC_ID_COMFORT_NOISE,
yading@10 162 .priv_data_size = sizeof(CNGContext),
yading@10 163 .init = cng_decode_init,
yading@10 164 .decode = cng_decode_frame,
yading@10 165 .flush = cng_decode_flush,
yading@10 166 .close = cng_decode_close,
yading@10 167 .long_name = NULL_IF_CONFIG_SMALL("RFC 3389 comfort noise generator"),
yading@10 168 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
yading@10 169 AV_SAMPLE_FMT_NONE },
yading@10 170 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
yading@10 171 };