2 * RFC 3389 comfort noise generator
3 * Copyright (c) 2012 Martin Storsjo
5 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/avassert.h"
25 #include "libavutil/common.h"
26 #include "libavutil/mem.h"
28 #include "codec_internal.h"
32 typedef struct CNGContext
{
39 static av_cold
int cng_encode_close(AVCodecContext
*avctx
)
41 CNGContext
*p
= avctx
->priv_data
;
43 av_freep(&p
->samples32
);
44 av_freep(&p
->ref_coef
);
48 static av_cold
int cng_encode_init(AVCodecContext
*avctx
)
50 CNGContext
*p
= avctx
->priv_data
;
53 avctx
->frame_size
= 640;
55 if ((ret
= ff_lpc_init(&p
->lpc
, avctx
->frame_size
, p
->order
, FF_LPC_TYPE_LEVINSON
)) < 0)
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
);
65 static int cng_encode_frame(AVCodecContext
*avctx
, AVPacket
*avpkt
,
66 const AVFrame
*frame
, int *got_packet_ptr
)
68 CNGContext
*p
= avctx
->priv_data
;
72 const int16_t *samples
= (const int16_t*) frame
->data
[0];
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");
79 for (i
= 0; i
< frame
->nb_samples
; i
++) {
80 p
->samples32
[i
] = samples
[i
];
81 energy
+= samples
[i
] * samples
[i
];
83 energy
/= frame
->nb_samples
;
85 double dbov
= 10 * log10(energy
/ 1081109975);
86 qdbov
= av_clip_uintp2(-floor(dbov
), 7);
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;
96 av_assert1(avpkt
->size
== 1 + p
->order
);
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
,