FFmpeg
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/avassert.h"
25 #include "libavutil/common.h"
26 #include "avcodec.h"
27 #include "codec_internal.h"
28 #include "encode.h"
29 #include "lpc.h"
30 
31 typedef struct CNGContext {
33  int order;
35  double *ref_coef;
36 } CNGContext;
37 
39 {
40  CNGContext *p = avctx->priv_data;
41  ff_lpc_end(&p->lpc);
42  av_freep(&p->samples32);
43  av_freep(&p->ref_coef);
44  return 0;
45 }
46 
48 {
49  CNGContext *p = avctx->priv_data;
50  int ret;
51 
52  avctx->frame_size = 640;
53  p->order = 10;
54  if ((ret = ff_lpc_init(&p->lpc, avctx->frame_size, p->order, FF_LPC_TYPE_LEVINSON)) < 0)
55  return ret;
56  p->samples32 = av_malloc_array(avctx->frame_size, sizeof(*p->samples32));
57  p->ref_coef = av_malloc_array(p->order, sizeof(*p->ref_coef));
58  if (!p->samples32 || !p->ref_coef)
59  return AVERROR(ENOMEM);
60 
61  return 0;
62 }
63 
64 static int cng_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
65  const AVFrame *frame, int *got_packet_ptr)
66 {
67  CNGContext *p = avctx->priv_data;
68  int ret, i;
69  double energy = 0;
70  int qdbov;
71  const int16_t *samples = (const int16_t*) frame->data[0];
72 
73  if ((ret = ff_get_encode_buffer(avctx, avpkt, 1 + p->order, 0))) {
74  av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
75  return ret;
76  }
77 
78  for (i = 0; i < frame->nb_samples; i++) {
79  p->samples32[i] = samples[i];
80  energy += samples[i] * samples[i];
81  }
82  energy /= frame->nb_samples;
83  if (energy > 0) {
84  double dbov = 10 * log10(energy / 1081109975);
85  qdbov = av_clip_uintp2(-floor(dbov), 7);
86  } else {
87  qdbov = 127;
88  }
90  avpkt->data[0] = qdbov;
91  for (i = 0; i < p->order; i++)
92  avpkt->data[1 + i] = p->ref_coef[i] * 127 + 127;
93 
94  *got_packet_ptr = 1;
95  av_assert1(avpkt->size == 1 + p->order);
96 
97  return 0;
98 }
99 
101  .p.name = "comfortnoise",
102  CODEC_LONG_NAME("RFC 3389 comfort noise generator"),
103  .p.type = AVMEDIA_TYPE_AUDIO,
106  .priv_data_size = sizeof(CNGContext),
109  .close = cng_encode_close,
110  .p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
112  .p.ch_layouts = (const AVChannelLayout[]){ AV_CHANNEL_LAYOUT_MONO, { 0 } },
113  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
114 };
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1077
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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. Frame references ownership and permissions
av_clip_uintp2
#define av_clip_uintp2
Definition: common.h:122
ff_lpc_init
av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order, enum FFLPCType lpc_type)
Initialize LPCContext.
Definition: lpc.c:340
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:344
AVPacket::data
uint8_t * data
Definition: packet.h:522
encode.h
cng_encode_frame
static int cng_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: cngenc.c:64
FFCodec
Definition: codec_internal.h:127
lpc.h
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:365
LPCContext
Definition: lpc.h:51
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:296
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
floor
static __device__ float floor(float a)
Definition: cuda_runtime.h:173
CNGContext::lpc
LPCContext lpc
Definition: cngenc.c:32
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
CNGContext::samples32
int32_t * samples32
Definition: cngenc.c:34
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:159
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
frame
static AVFrame * frame
Definition: demux_decode.c:54
if
if(ret)
Definition: filter_design.txt:179
CNGContext
Definition: cngdec.c:34
ff_lpc_end
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
Definition: lpc.c:365
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:365
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:523
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:303
codec_internal.h
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
AV_CODEC_ID_COMFORT_NOISE
@ AV_CODEC_ID_COMFORT_NOISE
Definition: codec_id.h:501
cng_encode_init
static av_cold int cng_encode_init(AVCodecContext *avctx)
Definition: cngenc.c:47
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:424
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
common.h
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
avcodec.h
CNGContext::order
int order
Definition: cngdec.c:37
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:445
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:105
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
ff_lpc_calc_ref_coefs
int ff_lpc_calc_ref_coefs(LPCContext *s, const int32_t *samples, int order, double *ref)
Definition: lpc.c:197
cng_encode_close
static av_cold int cng_encode_close(AVCodecContext *avctx)
Definition: cngenc.c:38
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:378
ff_comfortnoise_encoder
const FFCodec ff_comfortnoise_encoder
Definition: cngenc.c:100
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
CNGContext::ref_coef
double * ref_coef
Definition: cngenc.c:35
int32_t
int32_t
Definition: audioconvert.c:56
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
FF_LPC_TYPE_LEVINSON
@ FF_LPC_TYPE_LEVINSON
Levinson-Durbin recursion.
Definition: lpc.h:46