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_free(p->samples32);
41  av_free(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(avctx->frame_size * sizeof(*p->samples32));
60  p->ref_coef = av_malloc(p->order * sizeof(*p->ref_coef));
61  if (!p->samples32 || !p->ref_coef) {
62  cng_encode_close(avctx);
63  return AVERROR(ENOMEM);
64  }
65 
66  return 0;
67 }
68 
69 static int cng_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
70  const AVFrame *frame, int *got_packet_ptr)
71 {
72  CNGContext *p = avctx->priv_data;
73  int ret, i;
74  double energy = 0;
75  int qdbov;
76  int16_t *samples = (int16_t*) frame->data[0];
77 
78  if ((ret = ff_alloc_packet(avpkt, 1 + p->order))) {
79  av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
80  return ret;
81  }
82 
83  for (i = 0; i < frame->nb_samples; i++) {
84  p->samples32[i] = samples[i];
85  energy += samples[i] * samples[i];
86  }
87  energy /= frame->nb_samples;
88  if (energy > 0) {
89  double dbov = 10 * log10(energy / 1081109975);
90  qdbov = av_clip(-floor(dbov), 0, 127);
91  } else {
92  qdbov = 127;
93  }
94  ret = ff_lpc_calc_ref_coefs(&p->lpc, p->samples32, p->order, p->ref_coef);
95  avpkt->data[0] = qdbov;
96  for (i = 0; i < p->order; i++)
97  avpkt->data[1 + i] = p->ref_coef[i] * 127 + 127;
98 
99  *got_packet_ptr = 1;
100  avpkt->size = 1 + p->order;
101 
102  return 0;
103 }
104 
106  .name = "comfortnoise",
107  .type = AVMEDIA_TYPE_AUDIO,
109  .priv_data_size = sizeof(CNGContext),
111  .encode2 = cng_encode_frame,
113  .long_name = NULL_IF_CONFIG_SMALL("RFC 3389 comfort noise generator"),
114  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
116 };
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
Definition: lpc.h:50
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
signed 16 bits
Definition: samplefmt.h:52
initialize output if(nPeaks >3)%at least 3 peaks in spectrum for trying to find f0 nf0peaks
#define av_cold
Definition: attributes.h:78
struct CNGContext CNGContext
uint8_t * data
int ff_lpc_calc_ref_coefs(LPCContext *s, const int32_t *samples, int order, double *ref)
Definition: lpc.c:152
frame
Definition: stft.m:14
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
external API header
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
Definition: lpc.c:284
ret
Definition: avfilter.c:821
int32_t
AVCodec ff_comfortnoise_encoder
Definition: cngenc.c:105
int ff_alloc_packet(AVPacket *avpkt, int size)
int32_t * samples32
Definition: cngenc.c:32
static av_cold int cng_encode_close(AVCodecContext *avctx)
Definition: cngenc.c:36
int frame_size
Number of samples per channel in an audio frame.
LPCContext lpc
Definition: cngenc.c:30
main external API structure.
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
int order
Definition: cngdec.c:33
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
Levinson-Durbin recursion.
Definition: lpc.h:45
double * ref_coef
Definition: cngenc.c:33
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
synthesis window for stochastic i
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order, enum FFLPCType lpc_type)
Initialize LPCContext.
Definition: lpc.c:258
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
static int cng_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: cngenc.c:69
common internal api header.
common internal and external API header
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
int channels
number of audio channels
Filter the word “frame” indicates either a video frame or a group of audio samples
static av_cold int cng_encode_init(AVCodecContext *avctx)
Definition: cngenc.c:45
int energy
Definition: cngdec.c:34
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127