FFmpeg  4.3
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_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  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_packet2(avctx, avpkt, 1 + p->order, 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_uintp2(-floor(dbov), 7);
91  } else {
92  qdbov = 127;
93  }
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  av_assert1(avpkt->size == 1 + p->order);
101 
102  return 0;
103 }
104 
106  .name = "comfortnoise",
107  .long_name = NULL_IF_CONFIG_SMALL("RFC 3389 comfort noise generator"),
108  .type = AVMEDIA_TYPE_AUDIO,
110  .priv_data_size = sizeof(CNGContext),
112  .encode2 = cng_encode_frame,
113  .close = cng_encode_close,
114  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
116 };
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1206
AVCodec
AVCodec.
Definition: codec.h:190
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
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
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:716
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:355
cng_encode_frame
static int cng_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: cngenc.c:69
samples
FFmpeg Automated Testing Environment ************************************Introduction Using FATE from your FFmpeg source directory Submitting the results to the FFmpeg result aggregation server Uploading new samples to the fate suite FATE makefile targets and variables Makefile targets Makefile variables Examples Introduction **************FATE is an extended regression suite on the client side and a means for results aggregation and presentation on the server side The first part of this document explains how you can use FATE from your FFmpeg source directory to test your ffmpeg binary The second part describes how you can run FATE to submit the results to FFmpeg’s FATE server In any way you can have a look at the publicly viewable FATE results by visiting this as it can be seen if some test on some platform broke with their recent contribution This usually happens on the platforms the developers could not test on The second part of this document describes how you can run FATE to submit your results to FFmpeg’s FATE server If you want to submit your results be sure to check that your combination of OS and compiler is not already listed on the above mentioned website In the third part you can find a comprehensive listing of FATE makefile targets and variables Using FATE from your FFmpeg source directory **********************************************If you want to run FATE on your machine you need to have the samples in place You can get the samples via the build target fate rsync Use this command from the top level source this will cause FATE to fail NOTE To use a custom wrapper to run the pass ‘ target exec’ to ‘configure’ or set the TARGET_EXEC Make variable Submitting the results to the FFmpeg result aggregation server ****************************************************************To submit your results to the server you should run fate through the shell script ‘tests fate sh’ from the FFmpeg sources This script needs to be invoked with a configuration file as its first argument tests fate sh path to fate_config A configuration file template with comments describing the individual configuration variables can be found at ‘doc fate_config sh template’ Create a configuration that suits your based on the configuration template The ‘slot’ configuration variable can be any string that is not yet but it is suggested that you name it adhering to the following pattern ‘ARCH OS COMPILER COMPILER VERSION’ The configuration file itself will be sourced in a shell therefore all shell features may be used This enables you to setup the environment as you need it for your build For your first test runs the ‘fate_recv’ variable should be empty or commented out This will run everything as normal except that it will omit the submission of the results to the server The following files should be present in $workdir as specified in the configuration it may help to try out the ‘ssh’ command with one or more ‘ v’ options You should get detailed output concerning your SSH configuration and the authentication process The only thing left is to automate the execution of the fate sh script and the synchronisation of the samples directory Uploading new samples to the fate suite *****************************************If you need a sample uploaded send a mail to samples request This is for developers who have an account on the fate suite server If you upload new samples
Definition: fate.txt:139
lpc.h
LPCContext
Definition: lpc.h:52
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_cold
#define av_cold
Definition: attributes.h:90
CNGContext::lpc
LPCContext lpc
Definition: cngenc.c:30
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
CNGContext::samples32
int32_t * samples32
Definition: cngenc.c:32
int32_t
int32_t
Definition: audio_convert.c:194
if
if(ret)
Definition: filter_design.txt:179
CNGContext
Definition: cngdec.c:32
ff_comfortnoise_encoder
AVCodec ff_comfortnoise_encoder
Definition: cngenc.c:105
AVPacket::size
int size
Definition: packet.h:356
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:186
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
AV_CODEC_ID_COMFORT_NOISE
@ AV_CODEC_ID_COMFORT_NOISE
Definition: codec_id.h:471
ff_lpc_calc_ref_coefs
int ff_lpc_calc_ref_coefs(LPCContext *s, const int32_t *samples, int order, double *ref)
Definition: lpc.c:159
ff_lpc_end
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
Definition: lpc.c:322
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:1187
cng_encode_init
static av_cold int cng_encode_init(AVCodecContext *avctx)
Definition: cngenc.c:45
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
common.h
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:197
avcodec.h
CNGContext::order
int order
Definition: cngdec.c:35
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVCodecContext
main external API structure.
Definition: avcodec.h:526
cng_encode_close
static av_cold int cng_encode_close(AVCodecContext *avctx)
Definition: cngenc.c:36
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVPacket
This structure stores compressed data.
Definition: packet.h:332
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:553
CNGContext::ref_coef
double * ref_coef
Definition: cngenc.c:33
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
ff_alloc_packet2
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:32
FF_LPC_TYPE_LEVINSON
@ FF_LPC_TYPE_LEVINSON
Levinson-Durbin recursion.
Definition: lpc.h:47
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:300