FFmpeg
Functions | Variables
ra144enc.c File Reference
#include <float.h>
#include "libavutil/channel_layout.h"
#include "avcodec.h"
#include "audio_frame_queue.h"
#include "celp_filters.h"
#include "codec_internal.h"
#include "encode.h"
#include "mathops.h"
#include "put_bits.h"
#include "ra144.h"

Go to the source code of this file.

Functions

static av_cold int ra144_encode_close (AVCodecContext *avctx)
 
static av_cold int ra144_encode_init (AVCodecContext *avctx)
 
static int quantize (int value, const int16_t *table, unsigned int size)
 Quantize a value by searching a sorted table for the element with the nearest value. More...
 
static void orthogonalize (float *v, const float *u)
 Orthogonalize a vector to another vector. More...
 
static void get_match_score (float *work, const float *coefs, float *vect, const float *ortho1, const float *ortho2, const float *data, float *score, float *gain)
 Calculate match score and gain of an LPC-filtered vector with respect to input data, possibly orthogonalizing it to up to two other vectors. More...
 
static void create_adapt_vect (float *vect, const int16_t *cb, int lag)
 Create a vector from the adaptive codebook at a given lag value. More...
 
static int adaptive_cb_search (const int16_t *adapt_cb, float *work, const float *coefs, float *data)
 Search the adaptive codebook for the best entry and gain and remove its contribution from input data. More...
 
static void find_best_vect (float *work, const float *coefs, const int8_t cb[][BLOCKSIZE], const float *ortho1, const float *ortho2, float *data, int *idx, float *gain)
 Find the best vector of a fixed codebook by applying an LPC filter to codebook entries, possibly orthogonalizing them to up to two other vectors and matching the results with input data. More...
 
static void fixed_cb_search (float *work, const float *coefs, float *data, int cba_idx, int *cb1_idx, int *cb2_idx)
 Search the two fixed codebooks for the best entry and gain. More...
 
static void ra144_encode_subblock (RA144Context *ractx, const int16_t *sblock_data, const int16_t *lpc_coefs, unsigned int rms, PutBitContext *pb)
 Encode a subblock of the current frame. More...
 
static int ra144_encode_frame (AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
 

Variables

const FFCodec ff_ra_144_encoder
 

Detailed Description

Real Audio 1.0 (14.4K) encoder

Author
Francesco Lavra franc.nosp@m.esco.nosp@m.lavra.nosp@m.@int.nosp@m.erfre.nosp@m.e.it

Definition in file ra144enc.c.

Function Documentation

◆ ra144_encode_close()

static av_cold int ra144_encode_close ( AVCodecContext avctx)
static

Definition at line 40 of file ra144enc.c.

◆ ra144_encode_init()

static av_cold int ra144_encode_init ( AVCodecContext avctx)
static

Definition at line 49 of file ra144enc.c.

◆ quantize()

static int quantize ( int  value,
const int16_t *  table,
unsigned int  size 
)
static

Quantize a value by searching a sorted table for the element with the nearest value.

Parameters
valuevalue to quantize
tablearray containing the quantization table
sizesize of the quantization table
Returns
index of the quantization table corresponding to the element with the nearest value

Definition at line 83 of file ra144enc.c.

Referenced by ra144_encode_frame().

◆ orthogonalize()

static void orthogonalize ( float v,
const float u 
)
static

Orthogonalize a vector to another vector.

Parameters
vvector to orthogonalize
uvector against which orthogonalization is performed

Definition at line 108 of file ra144enc.c.

Referenced by fixed_cb_search(), and get_match_score().

◆ get_match_score()

static void get_match_score ( float work,
const float coefs,
float vect,
const float ortho1,
const float ortho2,
const float data,
float score,
float gain 
)
static

Calculate match score and gain of an LPC-filtered vector with respect to input data, possibly orthogonalizing it to up to two other vectors.

Parameters
workarray used to calculate the filtered vector
coefscoefficients of the LPC filter
vectoriginal vector
ortho1first vector against which orthogonalization is performed
ortho2second vector against which orthogonalization is performed
datainput data
scorepointer to variable where match score is returned
gainpointer to variable where gain is returned

Definition at line 136 of file ra144enc.c.

Referenced by adaptive_cb_search(), and find_best_vect().

◆ create_adapt_vect()

static void create_adapt_vect ( float vect,
const int16_t *  cb,
int  lag 
)
static

Create a vector from the adaptive codebook at a given lag value.

Parameters
vectarray where vector is stored
cbadaptive codebook
laglag value

Definition at line 169 of file ra144enc.c.

Referenced by adaptive_cb_search().

◆ adaptive_cb_search()

static int adaptive_cb_search ( const int16_t *  adapt_cb,
float work,
const float coefs,
float data 
)
static

Search the adaptive codebook for the best entry and gain and remove its contribution from input data.

Parameters
adapt_cbarray from which the adaptive codebook is extracted
workarray used to calculate LPC-filtered vectors
coefscoefficients of the LPC filter
datainput data
Returns
index of the best entry of the adaptive codebook

Re-calculate the filtered vector from the vector with maximum match score and remove its contribution from input data.

Definition at line 192 of file ra144enc.c.

Referenced by ra144_encode_subblock().

◆ find_best_vect()

static void find_best_vect ( float work,
const float coefs,
const int8_t  cb[][BLOCKSIZE],
const float ortho1,
const float ortho2,
float data,
int idx,
float gain 
)
static

Find the best vector of a fixed codebook by applying an LPC filter to codebook entries, possibly orthogonalizing them to up to two other vectors and matching the results with input data.

Parameters
workarray used to calculate the filtered vectors
coefscoefficients of the LPC filter
cbfixed codebook
ortho1first vector against which orthogonalization is performed
ortho2second vector against which orthogonalization is performed
datainput data
idxpointer to variable where the index of the best codebook entry is returned
gainpointer to variable where the gain of the best codebook entry is returned

Definition at line 240 of file ra144enc.c.

Referenced by fixed_cb_search().

◆ fixed_cb_search()

static void fixed_cb_search ( float work,
const float coefs,
float data,
int  cba_idx,
int cb1_idx,
int cb2_idx 
)
static

Search the two fixed codebooks for the best entry and gain.

Parameters
workarray used to calculate LPC-filtered vectors
coefscoefficients of the LPC filter
datainput data
cba_idxindex of the best entry of the adaptive codebook
cb1_idxpointer to variable where the index of the best entry of the first fixed codebook is returned
cb2_idxpointer to variable where the index of the best entry of the second fixed codebook is returned

The filtered vector from the adaptive codebook can be retrieved from work, because this function is called just after adaptive_cb_search().

Re-calculate the filtered vector from the vector with maximum match score and remove its contribution from input data.

Definition at line 275 of file ra144enc.c.

Referenced by ra144_encode_subblock().

◆ ra144_encode_subblock()

static void ra144_encode_subblock ( RA144Context ractx,
const int16_t *  sblock_data,
const int16_t *  lpc_coefs,
unsigned int  rms,
PutBitContext pb 
)
static

Encode a subblock of the current frame.

Parameters
ractxencoder context
sblock_datainput data of the subblock
lpc_coefscoefficients of the LPC filter
rmsRMS of the reflection coefficients
pbpointer to PutBitContext of the current frame

Calculate the zero-input response of the LPC filter and subtract it from input data.

Codebook search is performed without taking into account the contribution of the previous subblock, since it has been just subtracted from input data.

The filtered vector from the adaptive codebook can be retrieved from work, see implementation of adaptive_cb_search().

Definition at line 324 of file ra144enc.c.

Referenced by ra144_encode_frame().

◆ ra144_encode_frame()

static int ra144_encode_frame ( AVCodecContext avctx,
AVPacket avpkt,
const AVFrame frame,
int got_packet_ptr 
)
static

< reflection coefficients of the frame

< RMS of the reflection coefficients

Since the LPC coefficients are calculated on a frame centered over the fourth subframe, to encode a given frame, data from the next frame is needed. In each call to this function, the previous frame (whose data are saved in the encoder context) is encoded, and data from the current frame are saved in the encoder context to be used in the next function call.

TODO: apply perceptual weighting of the input speech through bandwidth expansion of the LPC filter.

The filter is unstable: use the coefficients of the previous frame.

Definition at line 424 of file ra144enc.c.

Variable Documentation

◆ ff_ra_144_encoder

const FFCodec ff_ra_144_encoder
Initial value:
= {
.p.name = "real_144",
CODEC_LONG_NAME("RealAudio 1.0 (14.4K)"),
.p.type = AVMEDIA_TYPE_AUDIO,
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
.priv_data_size = sizeof(RA144Context),
.p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
.p.supported_samplerates = (const int[]){ 8000, 0 },
.p.ch_layouts = (const AVChannelLayout[]){ AV_CHANNEL_LAYOUT_MONO, { 0 } },
}

Definition at line 538 of file ra144enc.c.

AV_CODEC_ID_RA_144
@ AV_CODEC_ID_RA_144
Definition: codec_id.h:427
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:315
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:365
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
RA144Context
Definition: ra144.h:40
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
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:307
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
ra144_encode_close
static av_cold int ra144_encode_close(AVCodecContext *avctx)
Definition: ra144enc.c:40
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
AV_CODEC_CAP_DELAY
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:76
ra144_encode_frame
static int ra144_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: ra144enc.c:424
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:382
AV_CODEC_CAP_SMALL_LAST_FRAME
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: codec.h:81
ra144_encode_init
static av_cold int ra144_encode_init(AVCodecContext *avctx)
Definition: ra144enc.c:49