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