FFmpeg
Data Structures | Macros | Functions | Variables
cook.c File Reference
#include "libavutil/channel_layout.h"
#include "libavutil/lfg.h"
#include "libavutil/mem_internal.h"
#include "libavutil/thread.h"
#include "audiodsp.h"
#include "avcodec.h"
#include "get_bits.h"
#include "bytestream.h"
#include "codec_internal.h"
#include "fft.h"
#include "internal.h"
#include "sinewin.h"
#include "unary.h"
#include "cookdata.h"

Go to the source code of this file.

Data Structures

struct  cook_gains
 
struct  COOKSubpacket
 
struct  cook
 

Macros

#define MONO   0x1000001
 
#define STEREO   0x1000002
 
#define JOINT_STEREO   0x1000003
 
#define MC_COOK   0x2000000
 
#define SUBBAND_SIZE   20
 
#define MAX_SUBPACKETS   5
 
#define QUANT_VLC_BITS   9
 
#define COUPLING_VLC_BITS   6
 
#define DECODE_BYTES_PAD1(bytes)   (3 - ((bytes) + 3) % 4)
 
#define DECODE_BYTES_PAD2(bytes)   ((bytes) % 4 + DECODE_BYTES_PAD1(2 * (bytes)))
 
#define PRINT(a, b)   ff_dlog(q->avctx, " %s = %d\n", a, b);
 

Functions

static av_cold void init_pow2table (void)
 
static av_cold void init_gain_table (COOKContext *q)
 
static av_cold int build_vlc (VLC *vlc, int nb_bits, const uint8_t counts[16], const void *syms, int symbol_size, int offset, void *logctx)
 
static av_cold int init_cook_vlc_tables (COOKContext *q)
 
static av_cold int init_cook_mlt (COOKContext *q)
 
static av_cold void init_cplscales_table (COOKContext *q)
 
static int decode_bytes (const uint8_t *inbuffer, uint8_t *out, int bytes)
 Cook indata decoding, every 32 bits are XORed with 0x37c511f2. More...
 
static av_cold int cook_decode_close (AVCodecContext *avctx)
 
static void decode_gain_info (GetBitContext *gb, int *gaininfo)
 Fill the gain array for the timedomain quantization. More...
 
static int decode_envelope (COOKContext *q, COOKSubpacket *p, int *quant_index_table)
 Create the quant index table needed for the envelope. More...
 
static void categorize (COOKContext *q, COOKSubpacket *p, const int *quant_index_table, int *category, int *category_index)
 Calculate the category and category_index vector. More...
 
static void expand_category (COOKContext *q, int *category, int *category_index)
 Expand the category vector. More...
 
static void scalar_dequant_float (COOKContext *q, int index, int quant_index, int *subband_coef_index, int *subband_coef_sign, float *mlt_p)
 The real requantization of the mltcoefs. More...
 
static int unpack_SQVH (COOKContext *q, COOKSubpacket *p, int category, int *subband_coef_index, int *subband_coef_sign)
 Unpack the subband_coef_index and subband_coef_sign vectors. More...
 
static void decode_vectors (COOKContext *q, COOKSubpacket *p, int *category, int *quant_index_table, float *mlt_buffer)
 Fill the mlt_buffer with mlt coefficients. More...
 
static int mono_decode (COOKContext *q, COOKSubpacket *p, float *mlt_buffer)
 
static void interpolate_float (COOKContext *q, float *buffer, int gain_index, int gain_index_next)
 the actual requantization of the timedomain samples More...
 
static void imlt_window_float (COOKContext *q, float *inbuffer, cook_gains *gains_ptr, float *previous_buffer)
 Apply transform window, overlap buffers. More...
 
static void imlt_gain (COOKContext *q, float *inbuffer, cook_gains *gains_ptr, float *previous_buffer)
 The modulated lapped transform, this takes transform coefficients and transforms them into timedomain samples. More...
 
static int decouple_info (COOKContext *q, COOKSubpacket *p, int *decouple_tab)
 function for getting the jointstereo coupling information More...
 
static void decouple_float (COOKContext *q, COOKSubpacket *p, int subband, float f1, float f2, float *decode_buffer, float *mlt_buffer1, float *mlt_buffer2)
 function decouples a pair of signals from a single signal via multiplication. More...
 
static int joint_decode (COOKContext *q, COOKSubpacket *p, float *mlt_buffer_left, float *mlt_buffer_right)
 function for decoding joint stereo data More...
 
static void decode_bytes_and_gain (COOKContext *q, COOKSubpacket *p, const uint8_t *inbuffer, cook_gains *gains_ptr)
 First part of subpacket decoding: decode raw stream bytes and read gain info. More...
 
static void saturate_output_float (COOKContext *q, float *out)
 Saturate the output signal and interleave. More...
 
static void mlt_compensate_output (COOKContext *q, float *decode_buffer, cook_gains *gains_ptr, float *previous_buffer, float *out)
 Final part of subpacket decoding: Apply modulated lapped transform, gain compensation, clip and convert to integer. More...
 
static int decode_subpacket (COOKContext *q, COOKSubpacket *p, const uint8_t *inbuffer, float **outbuffer)
 Cook subpacket decoding. More...
 
static int cook_decode_frame (AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
 
static void dump_cook_context (COOKContext *q)
 
static av_cold int cook_decode_init (AVCodecContext *avctx)
 Cook initialization. More...
 

Variables

static float pow2tab [127]
 
static float rootpow2tab [127]
 
const FFCodec ff_cook_decoder
 

Detailed Description

Cook compatible decoder. Bastardization of the G.722.1 standard. This decoder handles RealNetworks, RealAudio G2 data. Cook is identified by the codec name cook in RM files.

To use this decoder, a calling application must supply the extradata bytes provided from the RM container; 8+ bytes for mono streams and 16+ for stereo streams (maybe more).

Codec technicalities (all this assume a buffer length of 1024): Cook works with several different techniques to achieve its compression. In the timedomain the buffer is divided into 8 pieces and quantized. If two neighboring pieces have different quantization index a smooth quantization curve is used to get a smooth overlap between the different pieces. To get to the transformdomain Cook uses a modulated lapped transform. The transform domain has 50 subbands with 20 elements each. This means only a maximum of 50*20=1000 coefficients are used out of the 1024 available.

Definition in file cook.c.

Macro Definition Documentation

◆ MONO

#define MONO   0x1000001

Definition at line 63 of file cook.c.

◆ STEREO

#define STEREO   0x1000002

Definition at line 64 of file cook.c.

◆ JOINT_STEREO

#define JOINT_STEREO   0x1000003

Definition at line 65 of file cook.c.

◆ MC_COOK

#define MC_COOK   0x2000000

Definition at line 66 of file cook.c.

◆ SUBBAND_SIZE

#define SUBBAND_SIZE   20

Definition at line 68 of file cook.c.

◆ MAX_SUBPACKETS

#define MAX_SUBPACKETS   5

Definition at line 69 of file cook.c.

◆ QUANT_VLC_BITS

#define QUANT_VLC_BITS   9

Definition at line 71 of file cook.c.

◆ COUPLING_VLC_BITS

#define COUPLING_VLC_BITS   6

Definition at line 72 of file cook.c.

◆ DECODE_BYTES_PAD1

#define DECODE_BYTES_PAD1 (   bytes)    (3 - ((bytes) + 3) % 4)

Definition at line 279 of file cook.c.

◆ DECODE_BYTES_PAD2

#define DECODE_BYTES_PAD2 (   bytes)    ((bytes) % 4 + DECODE_BYTES_PAD1(2 * (bytes)))

Definition at line 280 of file cook.c.

◆ PRINT

#define PRINT (   a,
  b 
)    ff_dlog(q->avctx, " %s = %d\n", a, b);

Function Documentation

◆ init_pow2table()

static av_cold void init_pow2table ( void  )
static

Definition at line 173 of file cook.c.

Referenced by cook_decode_init().

◆ init_gain_table()

static av_cold void init_gain_table ( COOKContext *  q)
static

Definition at line 190 of file cook.c.

Referenced by cook_decode_init().

◆ build_vlc()

static av_cold int build_vlc ( VLC vlc,
int  nb_bits,
const uint8_t  counts[16],
const void *  syms,
int  symbol_size,
int  offset,
void *  logctx 
)
static

Definition at line 199 of file cook.c.

Referenced by init_cook_vlc_tables().

◆ init_cook_vlc_tables()

static av_cold int init_cook_vlc_tables ( COOKContext *  q)
static

Definition at line 215 of file cook.c.

Referenced by cook_decode_init().

◆ init_cook_mlt()

static av_cold int init_cook_mlt ( COOKContext *  q)
static

Definition at line 247 of file cook.c.

Referenced by cook_decode_init().

◆ init_cplscales_table()

static av_cold void init_cplscales_table ( COOKContext *  q)
static

Definition at line 270 of file cook.c.

Referenced by cook_decode_init().

◆ decode_bytes()

static int decode_bytes ( const uint8_t *  inbuffer,
uint8_t *  out,
int  bytes 
)
inlinestatic

Cook indata decoding, every 32 bits are XORed with 0x37c511f2.

Why? No idea, some checksum/error detection method maybe.

Out buffer size: extra bytes are needed to cope with padding/misalignment. Subpackets passed to the decoder can contain two, consecutive half-subpackets, of identical but arbitrary size. 1234 1234 1234 1234 extraA extraB Case 1: AAAA BBBB 0 0 Case 2: AAAA ABBB BB– 3 3 Case 3: AAAA AABB BBBB 2 2 Case 4: AAAA AAAB BBBB BB– 1 5

Nice way to waste CPU cycles.

Parameters
inbufferpointer to byte array of indata
outpointer to byte array of outdata
bytesnumber of bytes

Definition at line 302 of file cook.c.

Referenced by decode_bytes_and_gain().

◆ cook_decode_close()

static av_cold int cook_decode_close ( AVCodecContext avctx)
static

Definition at line 328 of file cook.c.

◆ decode_gain_info()

static void decode_gain_info ( GetBitContext gb,
int gaininfo 
)
static

Fill the gain array for the timedomain quantization.

Parameters
gbpointer to the GetBitContext
gaininfoarray[9] of gain indexes

Definition at line 360 of file cook.c.

Referenced by decode_bytes_and_gain().

◆ decode_envelope()

static int decode_envelope ( COOKContext *  q,
COOKSubpacket p,
int quant_index_table 
)
static

Create the quant index table needed for the envelope.

Parameters
qpointer to the COOKContext
quant_index_tablepointer to the array

Definition at line 384 of file cook.c.

Referenced by mono_decode().

◆ categorize()

static void categorize ( COOKContext *  q,
COOKSubpacket p,
const int quant_index_table,
int category,
int category_index 
)
static

Calculate the category and category_index vector.

Parameters
qpointer to the COOKContext
quant_index_tablepointer to the array
categorypointer to the category array
category_indexpointer to the category_index array

Definition at line 425 of file cook.c.

Referenced by mono_decode().

◆ expand_category()

static void expand_category ( COOKContext *  q,
int category,
int category_index 
)
inlinestatic

Expand the category vector.

Parameters
qpointer to the COOKContext
categorypointer to the category array
category_indexpointer to the category_index array

Definition at line 522 of file cook.c.

Referenced by mono_decode().

◆ scalar_dequant_float()

static void scalar_dequant_float ( COOKContext *  q,
int  index,
int  quant_index,
int subband_coef_index,
int subband_coef_sign,
float mlt_p 
)
static

The real requantization of the mltcoefs.

Parameters
qpointer to the COOKContext
indexindex
quant_indexquantisation index
subband_coef_indexarray of indexes to quant_centroid_tab
subband_coef_signsigns of coefficients
mlt_ppointer into the mlt buffer

Definition at line 544 of file cook.c.

Referenced by cook_decode_init().

◆ unpack_SQVH()

static int unpack_SQVH ( COOKContext *  q,
COOKSubpacket p,
int  category,
int subband_coef_index,
int subband_coef_sign 
)
static

Unpack the subband_coef_index and subband_coef_sign vectors.

Parameters
qpointer to the COOKContext
categorypointer to the category array
subband_coef_indexarray of indexes to quant_centroid_tab
subband_coef_signsigns of coefficients

Definition at line 573 of file cook.c.

Referenced by decode_vectors().

◆ decode_vectors()

static void decode_vectors ( COOKContext *  q,
COOKSubpacket p,
int category,
int quant_index_table,
float mlt_buffer 
)
static

Fill the mlt_buffer with mlt coefficients.

Parameters
qpointer to the COOKContext
categorypointer to the category array
quant_index_tablepointer to the array
mlt_bufferpointer to mlt coefficients

Definition at line 617 of file cook.c.

Referenced by mono_decode().

◆ mono_decode()

static int mono_decode ( COOKContext *  q,
COOKSubpacket p,
float mlt_buffer 
)
static

Definition at line 653 of file cook.c.

Referenced by decode_subpacket(), and joint_decode().

◆ interpolate_float()

static void interpolate_float ( COOKContext *  q,
float buffer,
int  gain_index,
int  gain_index_next 
)
static

the actual requantization of the timedomain samples

Parameters
qpointer to the COOKContext
bufferpointer to the timedomain buffer
gain_indexindex for the block multiplier
gain_index_nextindex for the next block multiplier

Definition at line 683 of file cook.c.

Referenced by cook_decode_init().

◆ imlt_window_float()

static void imlt_window_float ( COOKContext *  q,
float inbuffer,
cook_gains gains_ptr,
float previous_buffer 
)
static

Apply transform window, overlap buffers.

Parameters
qpointer to the COOKContext
inbufferpointer to the mltcoefficients
gains_ptrcurrent and previous gains
previous_bufferpointer to the previous buffer to be used for overlapping

Definition at line 710 of file cook.c.

Referenced by cook_decode_init().

◆ imlt_gain()

static void imlt_gain ( COOKContext *  q,
float inbuffer,
cook_gains gains_ptr,
float previous_buffer 
)
static

The modulated lapped transform, this takes transform coefficients and transforms them into timedomain samples.

Apply transform window, overlap buffers, apply gain profile and buffer management.

Parameters
qpointer to the COOKContext
inbufferpointer to the mltcoefficients
gains_ptrcurrent and previous gains
previous_bufferpointer to the previous buffer to be used for overlapping

Definition at line 738 of file cook.c.

Referenced by mlt_compensate_output().

◆ decouple_info()

static int decouple_info ( COOKContext *  q,
COOKSubpacket p,
int decouple_tab 
)
static

function for getting the jointstereo coupling information

Parameters
qpointer to the COOKContext
decouple_tabdecoupling array

Definition at line 768 of file cook.c.

Referenced by joint_decode().

◆ decouple_float()

static void decouple_float ( COOKContext *  q,
COOKSubpacket p,
int  subband,
float  f1,
float  f2,
float decode_buffer,
float mlt_buffer1,
float mlt_buffer2 
)
static

function decouples a pair of signals from a single signal via multiplication.

Parameters
qpointer to the COOKContext
subbandindex of the current subband
f1multiplier for channel 1 extraction
f2multiplier for channel 2 extraction
decode_bufferinput buffer
mlt_buffer1pointer to left channel mlt coefficients
mlt_buffer2pointer to right channel mlt coefficients

Definition at line 807 of file cook.c.

Referenced by cook_decode_init().

◆ joint_decode()

static int joint_decode ( COOKContext *  q,
COOKSubpacket p,
float mlt_buffer_left,
float mlt_buffer_right 
)
static

function for decoding joint stereo data

Parameters
qpointer to the COOKContext
mlt_buffer1pointer to left channel mlt coefficients
mlt_buffer2pointer to right channel mlt coefficients

Definition at line 829 of file cook.c.

Referenced by decode_subpacket().

◆ decode_bytes_and_gain()

static void decode_bytes_and_gain ( COOKContext *  q,
COOKSubpacket p,
const uint8_t *  inbuffer,
cook_gains gains_ptr 
)
inlinestatic

First part of subpacket decoding: decode raw stream bytes and read gain info.

Parameters
qpointer to the COOKContext
inbufferpointer to raw stream data
gains_ptrarray of current/prev gain pointers

Definition at line 881 of file cook.c.

Referenced by decode_subpacket().

◆ saturate_output_float()

static void saturate_output_float ( COOKContext *  q,
float out 
)
static

Saturate the output signal and interleave.

Parameters
qpointer to the COOKContext
outpointer to the output vector

Definition at line 903 of file cook.c.

Referenced by cook_decode_init().

◆ mlt_compensate_output()

static void mlt_compensate_output ( COOKContext *  q,
float decode_buffer,
cook_gains gains_ptr,
float previous_buffer,
float out 
)
inlinestatic

Final part of subpacket decoding: Apply modulated lapped transform, gain compensation, clip and convert to integer.

Parameters
qpointer to the COOKContext
decode_bufferpointer to the mlt coefficients
gains_ptrarray of current/prev gain pointers
previous_bufferpointer to the previous buffer to be used for overlapping
outpointer to the output buffer

Definition at line 921 of file cook.c.

Referenced by decode_subpacket().

◆ decode_subpacket()

static int decode_subpacket ( COOKContext *  q,
COOKSubpacket p,
const uint8_t *  inbuffer,
float **  outbuffer 
)
static

Cook subpacket decoding.

This function returns one decoded subpacket, usually 1024 samples per channel.

Parameters
qpointer to the COOKContext
inbufferpointer to the inbuffer
outbufferpointer to the outbuffer

Definition at line 939 of file cook.c.

Referenced by cook_decode_frame().

◆ cook_decode_frame()

static int cook_decode_frame ( AVCodecContext avctx,
AVFrame frame,
int got_frame_ptr,
AVPacket avpkt 
)
static

Definition at line 981 of file cook.c.

◆ dump_cook_context()

static void dump_cook_context ( COOKContext *  q)
static

Definition at line 1046 of file cook.c.

Referenced by cook_decode_init().

◆ cook_decode_init()

static av_cold int cook_decode_init ( AVCodecContext avctx)
static

Cook initialization.

Parameters
avctxpointer to the AVCodecContext

Definition at line 1073 of file cook.c.

Variable Documentation

◆ pow2tab

float pow2tab[127]
static

Definition at line 167 of file cook.c.

Referenced by imlt_window_float(), init_gain_table(), init_pow2table(), and interpolate_float().

◆ rootpow2tab

float rootpow2tab[127]
static

Definition at line 168 of file cook.c.

Referenced by init_pow2table(), and scalar_dequant_float().

◆ ff_cook_decoder

const FFCodec ff_cook_decoder
Initial value:
= {
.p.name = "cook",
.p.long_name = NULL_IF_CONFIG_SMALL("Cook / Cooker / Gecko (RealAudio G2)"),
.p.type = AVMEDIA_TYPE_AUDIO,
.priv_data_size = sizeof(COOKContext),
.p.capabilities = AV_CODEC_CAP_DR1,
.p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
}

Definition at line 1299 of file cook.c.

AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
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:39
cook_decode_init
static av_cold int cook_decode_init(AVCodecContext *avctx)
Cook initialization.
Definition: cook.c:1073
cook_decode_close
static av_cold int cook_decode_close(AVCodecContext *avctx)
Definition: cook.c:328
init
static int init
Definition: av_tx.c:47
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:254
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
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
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:117
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: codec_internal.h:31
cook_decode_frame
static int cook_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: cook.c:981
AV_CODEC_ID_COOK
@ AV_CODEC_ID_COOK
Definition: codec_id.h:447