FFmpeg  4.4.5
cngenc.c
Go to the documentation of this file.
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 "internal.h"
27 #include "lpc.h"
28 
29 typedef struct CNGContext {
31  int order;
33  double *ref_coef;
34 } CNGContext;
35 
37 {
38  CNGContext *p = avctx->priv_data;
39  ff_lpc_end(&p->lpc);
40  av_freep(&p->samples32);
41  av_freep(&p->ref_coef);
42  return 0;
43 }
44 
46 {
47  CNGContext *p = avctx->priv_data;
48  int ret;
49 
50  if (avctx->channels != 1) {
51  av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
52  return AVERROR(EINVAL);
53  }
54 
55  avctx->frame_size = 640;
56  p->order = 10;
57  if ((ret = ff_lpc_init(&p->lpc, avctx->frame_size, p->order, FF_LPC_TYPE_LEVINSON)) < 0)
58  return ret;
59  p->samples32 = av_malloc_array(avctx->frame_size, sizeof(*p->samples32));
60  p->ref_coef = av_malloc_array(p->order, sizeof(*p->ref_coef));
61  if (!p->samples32 || !p->ref_coef)
62  return AVERROR(ENOMEM);
63 
64  return 0;
65 }
66 
67 static int cng_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
68  const AVFrame *frame, int *got_packet_ptr)
69 {
70  CNGContext *p = avctx->priv_data;
71  int ret, i;
72  double energy = 0;
73  int qdbov;
74  int16_t *samples = (int16_t*) frame->data[0];
75 
76  if ((ret = ff_alloc_packet2(avctx, avpkt, 1 + p->order, 1 + p->order))) {
77  av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
78  return ret;
79  }
80 
81  for (i = 0; i < frame->nb_samples; i++) {
82  p->samples32[i] = samples[i];
83  energy += samples[i] * samples[i];
84  }
85  energy /= frame->nb_samples;
86  if (energy > 0) {
87  double dbov = 10 * log10(energy / 1081109975);
88  qdbov = av_clip_uintp2(-floor(dbov), 7);
89  } else {
90  qdbov = 127;
91  }
93  avpkt->data[0] = qdbov;
94  for (i = 0; i < p->order; i++)
95  avpkt->data[1 + i] = p->ref_coef[i] * 127 + 127;
96 
97  *got_packet_ptr = 1;
98  av_assert1(avpkt->size == 1 + p->order);
99 
100  return 0;
101 }
102 
104  .name = "comfortnoise",
105  .long_name = NULL_IF_CONFIG_SMALL("RFC 3389 comfort noise generator"),
106  .type = AVMEDIA_TYPE_AUDIO,
108  .priv_data_size = sizeof(CNGContext),
110  .encode2 = cng_encode_frame,
111  .close = cng_encode_close,
112  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
114  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
115 };
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:925
#define av_cold
Definition: attributes.h:88
int32_t
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
Libavcodec external API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
AVCodec ff_comfortnoise_encoder
Definition: cngenc.c:103
static av_cold int cng_encode_init(AVCodecContext *avctx)
Definition: cngenc.c:45
static int cng_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: cngenc.c:67
static av_cold int cng_encode_close(AVCodecContext *avctx)
Definition: cngenc.c:36
common internal and external API header
#define av_clip_uintp2
Definition: common.h:146
static __device__ float floor(float a)
Definition: cuda_runtime.h:173
static AVFrame * frame
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:33
@ AV_CODEC_ID_COMFORT_NOISE
Definition: codec_id.h:485
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
int i
Definition: input.c:407
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:49
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
Definition: lpc.c:325
int ff_lpc_calc_ref_coefs(LPCContext *s, const int32_t *samples, int order, double *ref)
Definition: lpc.c:160
av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order, enum FFLPCType lpc_type)
Initialize LPCContext.
Definition: lpc.c:303
@ FF_LPC_TYPE_LEVINSON
Levinson-Durbin recursion.
Definition: lpc.h:47
main external API structure.
Definition: avcodec.h:536
int channels
number of audio channels
Definition: avcodec.h:1197
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1216
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:384
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
LPCContext lpc
Definition: cngenc.c:30
int32_t * samples32
Definition: cngenc.c:32
double * ref_coef
Definition: cngenc.c:33
int order
Definition: cngdec.c:35
Definition: lpc.h:52
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
if(ret< 0)
Definition: vf_mcdeint.c:282