LCOV - code coverage report
Current view: top level - libavcodec - cngdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 81 0.0 %
Date: 2017-12-10 21:22:29 Functions: 0 5 0.0 %

          Line data    Source code
       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 {
      33             :     float *refl_coef, *target_refl_coef;
      34             :     float *lpc_coef;
      35             :     int order;
      36             :     int energy, target_energy;
      37             :     int inited;
      38             :     float *filter_out;
      39             :     float *excitation;
      40             :     AVLFG lfg;
      41             : } CNGContext;
      42             : 
      43           0 : static av_cold int cng_decode_close(AVCodecContext *avctx)
      44             : {
      45           0 :     CNGContext *p = avctx->priv_data;
      46           0 :     av_freep(&p->refl_coef);
      47           0 :     av_freep(&p->target_refl_coef);
      48           0 :     av_freep(&p->lpc_coef);
      49           0 :     av_freep(&p->filter_out);
      50           0 :     av_freep(&p->excitation);
      51           0 :     return 0;
      52             : }
      53             : 
      54           0 : static av_cold int cng_decode_init(AVCodecContext *avctx)
      55             : {
      56           0 :     CNGContext *p = avctx->priv_data;
      57             : 
      58           0 :     avctx->sample_fmt  = AV_SAMPLE_FMT_S16;
      59           0 :     avctx->channels    = 1;
      60           0 :     avctx->sample_rate = 8000;
      61             : 
      62           0 :     p->order            = 12;
      63           0 :     avctx->frame_size   = 640;
      64           0 :     p->refl_coef        = av_mallocz_array(p->order, sizeof(*p->refl_coef));
      65           0 :     p->target_refl_coef = av_mallocz_array(p->order, sizeof(*p->target_refl_coef));
      66           0 :     p->lpc_coef         = av_mallocz_array(p->order, sizeof(*p->lpc_coef));
      67           0 :     p->filter_out       = av_mallocz_array(avctx->frame_size + p->order,
      68             :                                      sizeof(*p->filter_out));
      69           0 :     p->excitation       = av_mallocz_array(avctx->frame_size, sizeof(*p->excitation));
      70           0 :     if (!p->refl_coef || !p->target_refl_coef || !p->lpc_coef ||
      71           0 :         !p->filter_out || !p->excitation) {
      72           0 :         cng_decode_close(avctx);
      73           0 :         return AVERROR(ENOMEM);
      74             :     }
      75             : 
      76           0 :     av_lfg_init(&p->lfg, 0);
      77             : 
      78           0 :     return 0;
      79             : }
      80             : 
      81           0 : static void make_lpc_coefs(float *lpc, const float *refl, int order)
      82             : {
      83             :     float buf[100];
      84             :     float *next, *cur;
      85             :     int m, i;
      86           0 :     next = buf;
      87           0 :     cur  = lpc;
      88           0 :     for (m = 0; m < order; m++) {
      89           0 :         next[m] = refl[m];
      90           0 :         for (i = 0; i < m; i++)
      91           0 :             next[i] = cur[i] + refl[m] * cur[m - i - 1];
      92           0 :         FFSWAP(float*, next, cur);
      93             :     }
      94           0 :     if (cur != lpc)
      95           0 :         memcpy(lpc, cur, sizeof(*lpc) * order);
      96           0 : }
      97             : 
      98           0 : static void cng_decode_flush(AVCodecContext *avctx)
      99             : {
     100           0 :     CNGContext *p = avctx->priv_data;
     101           0 :     p->inited = 0;
     102           0 : }
     103             : 
     104           0 : static int cng_decode_frame(AVCodecContext *avctx, void *data,
     105             :                             int *got_frame_ptr, AVPacket *avpkt)
     106             : {
     107           0 :     AVFrame *frame = data;
     108           0 :     CNGContext *p = avctx->priv_data;
     109           0 :     int buf_size  = avpkt->size;
     110             :     int ret, i;
     111             :     int16_t *buf_out;
     112           0 :     float e = 1.0;
     113             :     float scaling;
     114             : 
     115           0 :     if (avpkt->size) {
     116           0 :         int dbov = -avpkt->data[0];
     117           0 :         p->target_energy = 1081109975 * ff_exp10(dbov / 10.0) * 0.75;
     118           0 :         memset(p->target_refl_coef, 0, p->order * sizeof(*p->target_refl_coef));
     119           0 :         for (i = 0; i < FFMIN(avpkt->size - 1, p->order); i++) {
     120           0 :             p->target_refl_coef[i] = (avpkt->data[1 + i] - 127) / 128.0;
     121             :         }
     122             :     }
     123             : 
     124           0 :     if (avctx->internal->skip_samples > 10 * avctx->frame_size) {
     125           0 :         avctx->internal->skip_samples = 0;
     126           0 :         return AVERROR_INVALIDDATA;
     127             :     }
     128             : 
     129           0 :     if (p->inited) {
     130           0 :         p->energy = p->energy / 2 + p->target_energy / 2;
     131           0 :         for (i = 0; i < p->order; i++)
     132           0 :             p->refl_coef[i] = 0.6 *p->refl_coef[i] + 0.4 * p->target_refl_coef[i];
     133             :     } else {
     134           0 :         p->energy = p->target_energy;
     135           0 :         memcpy(p->refl_coef, p->target_refl_coef, p->order * sizeof(*p->refl_coef));
     136           0 :         p->inited = 1;
     137             :     }
     138           0 :     make_lpc_coefs(p->lpc_coef, p->refl_coef, p->order);
     139             : 
     140           0 :     for (i = 0; i < p->order; i++)
     141           0 :         e *= 1.0 - p->refl_coef[i]*p->refl_coef[i];
     142             : 
     143           0 :     scaling = sqrt(e * p->energy / 1081109975);
     144           0 :     for (i = 0; i < avctx->frame_size; i++) {
     145           0 :         int r = (av_lfg_get(&p->lfg) & 0xffff) - 0x8000;
     146           0 :         p->excitation[i] = scaling * r;
     147             :     }
     148           0 :     ff_celp_lp_synthesis_filterf(p->filter_out + p->order, p->lpc_coef,
     149           0 :                                  p->excitation, avctx->frame_size, p->order);
     150             : 
     151           0 :     frame->nb_samples = avctx->frame_size;
     152           0 :     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
     153           0 :         return ret;
     154           0 :     buf_out = (int16_t *)frame->data[0];
     155           0 :     for (i = 0; i < avctx->frame_size; i++)
     156           0 :         buf_out[i] = av_clip_int16(p->filter_out[i + p->order]);
     157           0 :     memcpy(p->filter_out, p->filter_out + avctx->frame_size,
     158           0 :            p->order * sizeof(*p->filter_out));
     159             : 
     160           0 :     *got_frame_ptr = 1;
     161             : 
     162           0 :     return buf_size;
     163             : }
     164             : 
     165             : AVCodec ff_comfortnoise_decoder = {
     166             :     .name           = "comfortnoise",
     167             :     .long_name      = NULL_IF_CONFIG_SMALL("RFC 3389 comfort noise generator"),
     168             :     .type           = AVMEDIA_TYPE_AUDIO,
     169             :     .id             = AV_CODEC_ID_COMFORT_NOISE,
     170             :     .priv_data_size = sizeof(CNGContext),
     171             :     .init           = cng_decode_init,
     172             :     .decode         = cng_decode_frame,
     173             :     .flush          = cng_decode_flush,
     174             :     .close          = cng_decode_close,
     175             :     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
     176             :                                                      AV_SAMPLE_FMT_NONE },
     177             :     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
     178             : };

Generated by: LCOV version 1.13