FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cngenc.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 0 41 0.0%
Branches: 0 16 0.0%

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 "avcodec.h"
26 #include "encode.h"
27 #include "internal.h"
28 #include "lpc.h"
29
30 typedef struct CNGContext {
31 LPCContext lpc;
32 int order;
33 int32_t *samples32;
34 double *ref_coef;
35 } CNGContext;
36
37 static av_cold int cng_encode_close(AVCodecContext *avctx)
38 {
39 CNGContext *p = avctx->priv_data;
40 ff_lpc_end(&p->lpc);
41 av_freep(&p->samples32);
42 av_freep(&p->ref_coef);
43 return 0;
44 }
45
46 static av_cold int cng_encode_init(AVCodecContext *avctx)
47 {
48 CNGContext *p = avctx->priv_data;
49 int ret;
50
51 if (avctx->channels != 1) {
52 av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
53 return AVERROR(EINVAL);
54 }
55
56 avctx->frame_size = 640;
57 p->order = 10;
58 if ((ret = ff_lpc_init(&p->lpc, avctx->frame_size, p->order, FF_LPC_TYPE_LEVINSON)) < 0)
59 return ret;
60 p->samples32 = av_malloc_array(avctx->frame_size, sizeof(*p->samples32));
61 p->ref_coef = av_malloc_array(p->order, sizeof(*p->ref_coef));
62 if (!p->samples32 || !p->ref_coef)
63 return AVERROR(ENOMEM);
64
65 return 0;
66 }
67
68 static int cng_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
69 const AVFrame *frame, int *got_packet_ptr)
70 {
71 CNGContext *p = avctx->priv_data;
72 int ret, i;
73 double energy = 0;
74 int qdbov;
75 int16_t *samples = (int16_t*) frame->data[0];
76
77 if ((ret = ff_get_encode_buffer(avctx, avpkt, 1 + p->order, 0))) {
78 av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
79 return ret;
80 }
81
82 for (i = 0; i < frame->nb_samples; i++) {
83 p->samples32[i] = samples[i];
84 energy += samples[i] * samples[i];
85 }
86 energy /= frame->nb_samples;
87 if (energy > 0) {
88 double dbov = 10 * log10(energy / 1081109975);
89 qdbov = av_clip_uintp2(-floor(dbov), 7);
90 } else {
91 qdbov = 127;
92 }
93 ff_lpc_calc_ref_coefs(&p->lpc, p->samples32, p->order, p->ref_coef);
94 avpkt->data[0] = qdbov;
95 for (i = 0; i < p->order; i++)
96 avpkt->data[1 + i] = p->ref_coef[i] * 127 + 127;
97
98 *got_packet_ptr = 1;
99 av_assert1(avpkt->size == 1 + p->order);
100
101 return 0;
102 }
103
104 const AVCodec ff_comfortnoise_encoder = {
105 .name = "comfortnoise",
106 .long_name = NULL_IF_CONFIG_SMALL("RFC 3389 comfort noise generator"),
107 .type = AVMEDIA_TYPE_AUDIO,
108 .id = AV_CODEC_ID_COMFORT_NOISE,
109 .capabilities = AV_CODEC_CAP_DR1,
110 .priv_data_size = sizeof(CNGContext),
111 .init = cng_encode_init,
112 .encode2 = cng_encode_frame,
113 .close = cng_encode_close,
114 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
115 AV_SAMPLE_FMT_NONE },
116 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
117 };
118