FFmpeg coverage


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