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