FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Macros | Enumerations | Functions | Variables
twinvq.c File Reference
#include "libavutil/channel_layout.h"
#include "libavutil/float_dsp.h"
#include "avcodec.h"
#include "get_bits.h"
#include "fft.h"
#include "internal.h"
#include "lsp.h"
#include "sinewin.h"
#include <math.h>
#include <stdint.h>
#include "twinvq_data.h"

Go to the source code of this file.

Data Structures

struct  FrameMode
 Parameters and tables that are different for each frame type. More...
 
struct  ModeTab
 Parameters and tables that are different for every combination of bitrate/sample rate. More...
 
struct  TwinContext
 

Macros

#define PPC_SHAPE_CB_SIZE   64
 
#define PPC_SHAPE_LEN_MAX   60
 
#define SUB_AMP_MAX   4500.0
 
#define MULAW_MU   100.0
 
#define GAIN_BITS   8
 
#define AMP_MAX   13000.0
 
#define SUB_GAIN_BITS   5
 
#define WINDOW_TYPE_BITS   4
 
#define PGAIN_MU   200
 
#define LSP_COEFS_MAX   20
 
#define LSP_SPLIT_MAX   4
 
#define CHANNELS_MAX   2
 
#define SUBBLOCKS_MAX   16
 
#define BARK_N_COEF_MAX   4
 

Enumerations

enum  FrameType {
  ACTIVE_FRAME, SID_FRAME, UNTRANSMITTED_FRAME, INTRA_FRAME = 0,
  INTER_FRAME, SKIP_FRAME, FT_SHORT = 0, FT_MEDIUM,
  FT_LONG, FT_PPC
}
 

Functions

static void memset_float (float *buf, float val, int size)
 
static float eval_lpc_spectrum (const float *lsp, float cos_val, int order)
 Evaluate a single LPC amplitude spectrum envelope coefficient from the line spectrum pairs.
 
static void eval_lpcenv (TwinContext *tctx, const float *cos_vals, float *lpc)
 Evaluate the LPC amplitude spectrum envelope from the line spectrum pairs.
 
static void interpolate (float *out, float v1, float v2, int size)
 
static float get_cos (int idx, int part, const float *cos_tab, int size)
 
static void eval_lpcenv_or_interp (TwinContext *tctx, enum FrameType ftype, float *out, const float *in, int size, int step, int part)
 Evaluate the LPC amplitude spectrum envelope from the line spectrum pairs.
 
static void eval_lpcenv_2parts (TwinContext *tctx, enum FrameType ftype, const float *buf, float *lpc, int size, int step)
 
static void dequant (TwinContext *tctx, GetBitContext *gb, float *out, enum FrameType ftype, const int16_t *cb0, const int16_t *cb1, int cb_len)
 Inverse quantization.
 
static float mulawinv (float y, float clip, float mu)
 
static int very_broken_op (int a, int b)
 Evaluate a*b/400 rounded to the nearest integer.
 
static void add_peak (int period, int width, const float *shape, float ppc_gain, float *speech, int len)
 Sum to data a periodic peak of a given period, width and shape.
 
static void decode_ppc (TwinContext *tctx, int period_coef, const float *shape, float ppc_gain, float *speech)
 
static void dec_gain (TwinContext *tctx, GetBitContext *gb, enum FrameType ftype, float *out)
 
static void rearrange_lsp (int order, float *lsp, float min_dist)
 Rearrange the LSP coefficients so that they have a minimum distance of min_dist.
 
static void decode_lsp (TwinContext *tctx, int lpc_idx1, uint8_t *lpc_idx2, int lpc_hist_idx, float *lsp, float *hist)
 
static void dec_lpc_spectrum_inv (TwinContext *tctx, float *lsp, enum FrameType ftype, float *lpc)
 
static void imdct_and_window (TwinContext *tctx, enum FrameType ftype, int wtype, float *in, float *prev, int ch)
 
static void imdct_output (TwinContext *tctx, enum FrameType ftype, int wtype, float **out)
 
static void dec_bark_env (TwinContext *tctx, const uint8_t *in, int use_hist, int ch, float *out, float gain, enum FrameType ftype)
 
static void read_and_decode_spectrum (TwinContext *tctx, GetBitContext *gb, float *out, enum FrameType ftype)
 
static int twin_decode_frame (AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
 
static av_cold int init_mdct_win (TwinContext *tctx)
 Init IMDCT and windowing tables.
 
static void permutate_in_line (int16_t *tab, int num_vect, int num_blocks, int block_size, const uint8_t line_len[2], int length_div, enum FrameType ftype)
 Interpret the data as if it were a num_blocks x line_len[0] matrix and for each line do a cyclic permutation, i.e.
 
static void transpose_perm (int16_t *out, int16_t *in, int num_vect, const uint8_t line_len[2], int length_div)
 Interpret the input data as in the following table:
 
static void linear_perm (int16_t *out, int16_t *in, int n_blocks, int size)
 
static av_cold void construct_perm_table (TwinContext *tctx, enum FrameType ftype)
 
static av_cold void init_bitstream_params (TwinContext *tctx)
 
static av_cold int twin_decode_close (AVCodecContext *avctx)
 
static av_cold int twin_decode_init (AVCodecContext *avctx)
 

Variables

static const ModeTab mode_08_08
 
static const ModeTab mode_11_08
 
static const ModeTab mode_11_10
 
static const ModeTab mode_16_16
 
static const ModeTab mode_22_20
 
static const ModeTab mode_22_24
 
static const ModeTab mode_22_32
 
static const ModeTab mode_44_40
 
static const ModeTab mode_44_48
 
AVCodec ff_twinvq_decoder
 

Macro Definition Documentation

#define PPC_SHAPE_CB_SIZE   64

Definition at line 208 of file twinvq.c.

Referenced by read_and_decode_spectrum().

#define PPC_SHAPE_LEN_MAX   60

Definition at line 209 of file twinvq.c.

Referenced by read_and_decode_spectrum().

#define SUB_AMP_MAX   4500.0

Definition at line 210 of file twinvq.c.

Referenced by dec_gain().

#define MULAW_MU   100.0

Definition at line 211 of file twinvq.c.

Referenced by dec_gain().

#define GAIN_BITS   8

Definition at line 212 of file twinvq.c.

Referenced by dec_gain(), and init_bitstream_params().

#define AMP_MAX   13000.0

Definition at line 213 of file twinvq.c.

Referenced by dec_gain().

#define SUB_GAIN_BITS   5

Definition at line 214 of file twinvq.c.

Referenced by dec_gain(), and init_bitstream_params().

#define WINDOW_TYPE_BITS   4

Definition at line 215 of file twinvq.c.

Referenced by init_bitstream_params(), and twin_decode_frame().

#define PGAIN_MU   200

Definition at line 216 of file twinvq.c.

Referenced by read_and_decode_spectrum().

#define LSP_COEFS_MAX   20

Definition at line 217 of file twinvq.c.

Referenced by read_and_decode_spectrum().

#define LSP_SPLIT_MAX   4

Definition at line 218 of file twinvq.c.

Referenced by read_and_decode_spectrum().

#define CHANNELS_MAX   2

Definition at line 219 of file twinvq.c.

Referenced by read_and_decode_spectrum(), and twin_decode_init().

#define SUBBLOCKS_MAX   16

Definition at line 220 of file twinvq.c.

Referenced by read_and_decode_spectrum().

#define BARK_N_COEF_MAX   4

Definition at line 221 of file twinvq.c.

Referenced by read_and_decode_spectrum().

Enumeration Type Documentation

enum FrameType
Enumerator:
ACTIVE_FRAME 

Active speech.

SID_FRAME 

Silence Insertion Descriptor frame.

UNTRANSMITTED_FRAME 
INTRA_FRAME 
INTER_FRAME 
SKIP_FRAME 
FT_SHORT 

Short frame (divided in n sub-blocks)

FT_MEDIUM 

Medium frame (divided in m<n sub-blocks)

FT_LONG 

Long frame (single sub-block + PPC)

FT_PPC 

Periodic Peak Component (part of the long frame)

Definition at line 36 of file twinvq.c.

Function Documentation

static void memset_float ( float *  buf,
float  val,
int  size 
)
static
Note
not speed critical, hence not optimized

Definition at line 224 of file twinvq.c.

Referenced by dec_bark_env(), eval_lpcenv_2parts(), and twin_decode_init().

static float eval_lpc_spectrum ( const float *  lsp,
float  cos_val,
int  order 
)
static

Evaluate a single LPC amplitude spectrum envelope coefficient from the line spectrum pairs.

Parameters
lspa vector of the cosinus of the LSP values
cos_valcos(PI*i/N) where i is the index of the LPC amplitude
orderthe order of the LSP (and the size of the *lsp buffer). Must be a multiple of four.
Returns
the LPC value
Todo:
reuse code from Vorbis decoder: vorbis_floor0_decode

Definition at line 242 of file twinvq.c.

Referenced by eval_lpcenv(), and eval_lpcenv_or_interp().

static void eval_lpcenv ( TwinContext tctx,
const float *  cos_vals,
float *  lpc 
)
static

Evaluate the LPC amplitude spectrum envelope from the line spectrum pairs.

Definition at line 267 of file twinvq.c.

Referenced by dec_lpc_spectrum_inv().

static void interpolate ( float *  out,
float  v1,
float  v2,
int  size 
)
static

Definition at line 280 of file twinvq.c.

Referenced by eval_lpcenv_2parts(), and eval_lpcenv_or_interp().

static float get_cos ( int  idx,
int  part,
const float *  cos_tab,
int  size 
)
inlinestatic

Definition at line 291 of file twinvq.c.

Referenced by eval_lpcenv_or_interp().

static void eval_lpcenv_or_interp ( TwinContext tctx,
enum FrameType  ftype,
float *  out,
const float *  in,
int  size,
int  step,
int  part 
)
inlinestatic

Evaluate the LPC amplitude spectrum envelope from the line spectrum pairs.

Probably for speed reasons, the coefficients are evaluated as siiiibiiiisiiiibiiiisiiiibiiiisiiiibiiiis ... where s is an evaluated value, i is a value interpolated from the others and b might be either calculated or interpolated, depending on an unexplained condition.

Parameters
stepthe size of a block "siiiibiiii"
inthe cosinus of the LSP data
partis 0 for 0...PI (positive cossinus values) and 1 for PI...2PI (negative cossinus values)
sizethe size of the whole output

Definition at line 311 of file twinvq.c.

Referenced by eval_lpcenv_2parts().

static void eval_lpcenv_2parts ( TwinContext tctx,
enum FrameType  ftype,
const float *  buf,
float *  lpc,
int  size,
int  step 
)
static

Definition at line 345 of file twinvq.c.

Referenced by dec_lpc_spectrum_inv().

static void dequant ( TwinContext tctx,
GetBitContext gb,
float *  out,
enum FrameType  ftype,
const int16_t *  cb0,
const int16_t *  cb1,
int  cb_len 
)
static

Inverse quantization.

Read CB coefficients for cb1 and cb2 from the bitstream, sum the corresponding vectors and write the result to *out after permutation.

Definition at line 362 of file twinvq.c.

Referenced by read_and_decode_spectrum().

static float mulawinv ( float  y,
float  clip,
float  mu 
)
inlinestatic

Definition at line 406 of file twinvq.c.

Referenced by dec_gain(), and read_and_decode_spectrum().

static int very_broken_op ( int  a,
int  b 
)
static

Evaluate a*b/400 rounded to the nearest integer.

When, for example, a*b == 200 and the nearest integer is ill-defined, use a table to emulate the following broken float-based implementation used by the binary decoder:

static int very_broken_op(int a, int b)
{
static float test; // Ugh, force gcc to do the division first...
test = a/400.;
return b * test + 0.5;
}
Note
if this function is replaced by just ROUNDED_DIV(a*b,400.), the stddev between the original file (before encoding with Yamaha encoder) and the decoded output increases, which leads one to believe that the encoder expects exactly this broken calculation.

Definition at line 432 of file twinvq.c.

Referenced by add_peak().

static void add_peak ( int  period,
int  width,
const float *  shape,
float  ppc_gain,
float *  speech,
int  len 
)
static

Sum to data a periodic peak of a given period, width and shape.

Parameters
periodthe period of the peak divised by 400.0

Definition at line 453 of file twinvq.c.

Referenced by decode_ppc().

static void decode_ppc ( TwinContext tctx,
int  period_coef,
const float *  shape,
float  ppc_gain,
float *  speech 
)
static

Definition at line 477 of file twinvq.c.

Referenced by read_and_decode_spectrum().

static void dec_gain ( TwinContext tctx,
GetBitContext gb,
enum FrameType  ftype,
float *  out 
)
static

Definition at line 502 of file twinvq.c.

Referenced by read_and_decode_spectrum().

static void rearrange_lsp ( int  order,
float *  lsp,
float  min_dist 
)
static

Rearrange the LSP coefficients so that they have a minimum distance of min_dist.

This function does it exactly as described in section of 3.2.4 of the G.729 specification (but interestingly is different from what the reference decoder actually does).

Definition at line 538 of file twinvq.c.

Referenced by decode_lsp().

static void decode_lsp ( TwinContext tctx,
int  lpc_idx1,
uint8_t lpc_idx2,
int  lpc_hist_idx,
float *  lsp,
float *  hist 
)
static

Definition at line 551 of file twinvq.c.

Referenced by read_and_decode_spectrum().

static void dec_lpc_spectrum_inv ( TwinContext tctx,
float *  lsp,
enum FrameType  ftype,
float *  lpc 
)
static

Definition at line 590 of file twinvq.c.

Referenced by read_and_decode_spectrum().

static void imdct_and_window ( TwinContext tctx,
enum FrameType  ftype,
int  wtype,
float *  in,
float *  prev,
int  ch 
)
static

Definition at line 612 of file twinvq.c.

Referenced by imdct_output().

static void imdct_output ( TwinContext tctx,
enum FrameType  ftype,
int  wtype,
float **  out 
)
static

Definition at line 666 of file twinvq.c.

Referenced by twin_decode_frame().

static void dec_bark_env ( TwinContext tctx,
const uint8_t in,
int  use_hist,
int  ch,
float *  out,
float  gain,
enum FrameType  ftype 
)
static

Definition at line 697 of file twinvq.c.

Referenced by read_and_decode_spectrum().

static void read_and_decode_spectrum ( TwinContext tctx,
GetBitContext gb,
float *  out,
enum FrameType  ftype 
)
static

Definition at line 724 of file twinvq.c.

Referenced by twin_decode_frame().

static int twin_decode_frame ( AVCodecContext avctx,
void data,
int *  got_frame_ptr,
AVPacket avpkt 
)
static

Definition at line 809 of file twinvq.c.

static av_cold int init_mdct_win ( TwinContext tctx)
static

Init IMDCT and windowing tables.

Definition at line 871 of file twinvq.c.

Referenced by twin_decode_init().

static void permutate_in_line ( int16_t *  tab,
int  num_vect,
int  num_blocks,
int  block_size,
const uint8_t  line_len[2],
int  length_div,
enum FrameType  ftype 
)
static

Interpret the data as if it were a num_blocks x line_len[0] matrix and for each line do a cyclic permutation, i.e.

abcdefghijklm -> defghijklmabc where the amount to be shifted is evaluated depending on the column.

Definition at line 928 of file twinvq.c.

Referenced by construct_perm_table().

static void transpose_perm ( int16_t *  out,
int16_t *  in,
int  num_vect,
const uint8_t  line_len[2],
int  length_div 
)
static

Interpret the input data as in the following table:

*
* abcdefgh
* ijklmnop
* qrstuvw
* x123456
*
* 

and transpose it, giving the output aiqxbjr1cks2dlt3emu4fvn5gow6hp

Definition at line 969 of file twinvq.c.

Referenced by construct_perm_table().

static void linear_perm ( int16_t *  out,
int16_t *  in,
int  n_blocks,
int  size 
)
static

Definition at line 979 of file twinvq.c.

Referenced by construct_perm_table().

static av_cold void construct_perm_table ( TwinContext tctx,
enum FrameType  ftype 
)
static

Definition at line 988 of file twinvq.c.

Referenced by init_bitstream_params().

static av_cold void init_bitstream_params ( TwinContext tctx)
static

Definition at line 1014 of file twinvq.c.

Referenced by twin_decode_init().

static av_cold int twin_decode_close ( AVCodecContext avctx)
static

Definition at line 1083 of file twinvq.c.

Referenced by twin_decode_init().

static av_cold int twin_decode_init ( AVCodecContext avctx)
static

Definition at line 1102 of file twinvq.c.

Variable Documentation

const ModeTab mode_08_08
static
Initial value:
= {
{
{ 8, bark_tab_s08_64, 10, tab.fcb08s , 1, 5, tab.cb0808s0, tab.cb0808s1, 18},
{ 2, bark_tab_m08_256, 20, tab.fcb08m , 2, 5, tab.cb0808m0, tab.cb0808m1, 16},
},
512 , 12, tab.lsp08, 1, 5, 3, 3, tab.shape08 , 8, 28, 20, 6, 40
}

Definition at line 96 of file twinvq.c.

Referenced by twin_decode_init().

const ModeTab mode_11_08
static
Initial value:
= {
{
{ 8, bark_tab_s11_64, 10, tab.fcb11s , 1, 5, tab.cb1108s0, tab.cb1108s1, 29},
{ 2, bark_tab_m11_256, 20, tab.fcb11m , 2, 5, tab.cb1108m0, tab.cb1108m1, 24},
},
512 , 16, tab.lsp11, 1, 6, 4, 3, tab.shape11 , 9, 36, 30, 7, 90
}

Definition at line 105 of file twinvq.c.

Referenced by twin_decode_init().

const ModeTab mode_11_10
static
Initial value:
= {
{
{ 8, bark_tab_s11_64, 10, tab.fcb11s , 1, 5, tab.cb1110s0, tab.cb1110s1, 21},
{ 2, bark_tab_m11_256, 20, tab.fcb11m , 2, 5, tab.cb1110m0, tab.cb1110m1, 18},
},
512 , 16, tab.lsp11, 1, 6, 4, 3, tab.shape11 , 9, 36, 30, 7, 90
}

Definition at line 114 of file twinvq.c.

Referenced by twin_decode_init().

const ModeTab mode_16_16
static
Initial value:
= {
{
{ 8, bark_tab_s16_128, 10, tab.fcb16s , 1, 5, tab.cb1616s0, tab.cb1616s1, 16},
{ 2, bark_tab_m16_512, 20, tab.fcb16m , 2, 5, tab.cb1616m0, tab.cb1616m1, 15},
},
1024, 16, tab.lsp16, 1, 6, 4, 3, tab.shape16 , 9, 56, 60, 7, 180
}

Definition at line 123 of file twinvq.c.

Referenced by twin_decode_init().

const ModeTab mode_22_20
static
Initial value:
= {
{
},
1024, 16, tab.lsp22_1, 1, 6, 4, 3, tab.shape22_1, 9, 56, 36, 7, 144
}

Definition at line 132 of file twinvq.c.

Referenced by twin_decode_init().

const ModeTab mode_22_24
static
Initial value:
= {
{
},
1024, 16, tab.lsp22_1, 1, 6, 4, 3, tab.shape22_1, 9, 56, 36, 7, 144
}

Definition at line 141 of file twinvq.c.

Referenced by twin_decode_init().

const ModeTab mode_22_32
static
Initial value:
= {
{
},
512 , 16, tab.lsp22_2, 1, 6, 4, 4, tab.shape22_2, 9, 56, 36, 7, 72
}

Definition at line 150 of file twinvq.c.

Referenced by twin_decode_init().

const ModeTab mode_44_40
static
Initial value:
= {
{
{16, bark_tab_s44_128, 10, tab.fcb44s , 1, 6, tab.cb4440s0, tab.cb4440s1, 18},
{ 4, bark_tab_m44_512, 20, tab.fcb44m , 2, 6, tab.cb4440m0, tab.cb4440m1, 17},
},
2048, 20, tab.lsp44, 1, 6, 4, 4, tab.shape44 , 9, 84, 54, 7, 432
}

Definition at line 159 of file twinvq.c.

Referenced by twin_decode_init().

const ModeTab mode_44_48
static
Initial value:
= {
{
{16, bark_tab_s44_128, 10, tab.fcb44s , 1, 6, tab.cb4448s0, tab.cb4448s1, 15},
{ 4, bark_tab_m44_512, 20, tab.fcb44m , 2, 6, tab.cb4448m0, tab.cb4448m1, 14},
},
2048, 20, tab.lsp44, 1, 6, 4, 4, tab.shape44 , 9, 84, 54, 7, 432
}

Definition at line 168 of file twinvq.c.

Referenced by twin_decode_init().

AVCodec ff_twinvq_decoder
Initial value:
= {
.name = "twinvq",
.priv_data_size = sizeof(TwinContext),
.capabilities = CODEC_CAP_DR1,
.long_name = NULL_IF_CONFIG_SMALL("VQF TwinVQ"),
.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
}

Definition at line 1173 of file twinvq.c.