FFmpeg
Loading...
Searching...
No Matches
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 "libavutil/mem.h"
27#include "avcodec.h"
28#include "codec_internal.h"
29#include "encode.h"
30#include "lpc.h"
31
32typedef struct CNGContext {
34 int order;
36 double *ref_coef;
38
40{
41 CNGContext *p = avctx->priv_data;
42 ff_lpc_end(&p->lpc);
43 av_freep(&p->samples32);
44 av_freep(&p->ref_coef);
45 return 0;
46}
47
49{
50 CNGContext *p = avctx->priv_data;
51 int ret;
52
53 avctx->frame_size = 640;
54 p->order = 10;
55 if ((ret = ff_lpc_init(&p->lpc, avctx->frame_size, p->order, FF_LPC_TYPE_LEVINSON)) < 0)
56 return ret;
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);
61
62 return 0;
63}
64
65static int cng_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
66 const AVFrame *frame, int *got_packet_ptr)
67{
68 CNGContext *p = avctx->priv_data;
69 int ret, i;
70 double energy = 0;
71 int qdbov;
72 const int16_t *samples = (const int16_t*) frame->data[0];
73
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");
76 return ret;
77 }
78
79 for (i = 0; i < frame->nb_samples; i++) {
80 p->samples32[i] = samples[i];
81 energy += samples[i] * samples[i];
82 }
83 energy /= frame->nb_samples;
84 if (energy > 0) {
85 double dbov = 10 * log10(energy / 1081109975);
86 qdbov = av_clip_uintp2(-floor(dbov), 7);
87 } else {
88 qdbov = 127;
89 }
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;
94
95 *got_packet_ptr = 1;
96 av_assert1(avpkt->size == 1 + p->order);
97
98 return 0;
99}
100
102 .p.name = "comfortnoise",
103 CODEC_LONG_NAME("RFC 3389 comfort noise generator"),
104 .p.type = AVMEDIA_TYPE_AUDIO,
107 .priv_data_size = sizeof(CNGContext),
110 .close = cng_encode_close,
113 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
114};
const FFCodec ff_comfortnoise_encoder
Definition cngenc.c:101
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static av_cold int cng_encode_init(AVCodecContext *avctx)
Definition cngenc.c:48
static int cng_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition cngenc.c:65
static av_cold int cng_encode_close(AVCodecContext *avctx)
Definition cngenc.c:39
#define CODEC_CH_LAYOUTS(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define CODEC_SAMPLEFMTS(...)
common internal and external API header
#define av_clip_uintp2
Definition common.h:124
static __device__ float floor(float a)
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition encode.c:106
#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:147
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_COMFORT_NOISE
Definition codec_id.h:514
#define AV_CHANNEL_LAYOUT_MONO
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
Definition lpc.c:367
int ff_lpc_calc_ref_coefs(LPCContext *s, const int32_t *samples, int order, double *ref)
Definition lpc.c:197
av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order, enum FFLPCType lpc_type)
Initialize LPCContext.
Definition lpc.c:342
#define av_cold
Definition attributes.h:117
@ FF_LPC_TYPE_LEVINSON
Levinson-Durbin recursion.
Definition lpc.h:46
Memory handling functions.
main external API structure.
Definition avcodec.h:443
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1068
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
LPCContext lpc
Definition cngenc.c:33
int32_t * samples32
Definition cngenc.c:35
double * ref_coef
Definition cngenc.c:36
int order
Definition cngdec.c:37
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)