65#define STEREO 0x1000002
66#define JOINT_STEREO 0x1000003
67#define MC_COOK 0x2000000
69#define SUBBAND_SIZE 20
70#define MAX_SUBPACKETS 5
72#define QUANT_VLC_BITS 9
73#define COUPLING_VLC_BITS 6
115 int *subband_coef_index,
int *subband_coef_sign,
122 float *decode_buffer,
123 float *mlt_buffer1,
float *mlt_buffer2);
126 cook_gains *gains_ptr,
float *previous_buffer);
129 int gain_index,
int gain_index_next);
179 static const float exp2_tab[2] = {1,
M_SQRT2};
180 float exp2_val =
powf(2, -63);
181 float root_val =
powf(2, -32);
182 for (
i = -63;
i < 64;
i++) {
196 for (
i = 0;
i < 31;
i++)
202 const void *syms,
int symbol_size,
int offset,
208 for (
int i = 0;
i < 16;
i++)
209 for (
unsigned count = num + counts[
i]; num < count; num++)
213 syms, symbol_size, symbol_size,
222 for (
i = 0;
i < 13;
i++) {
228 for (
i = 0;
i < 7;
i++) {
229 int sym_size = 1 + (
i == 3);
253 const float scale = 1.0 / 32768.0;
260 for (j = 0; j < mlt_size; j++)
275 for (
i = 0;
i < 5;
i++)
281#define DECODE_BYTES_PAD1(bytes) (3 - ((bytes) + 3) % 4)
282#define DECODE_BYTES_PAD2(bytes) ((bytes) % 4 + DECODE_BYTES_PAD1(2 * (bytes)))
306 static const uint32_t
tab[4] = {
313 uint32_t *obuf = (uint32_t *)
out;
320 off = (intptr_t) inbuffer & 3;
321 buf = (
const uint32_t *) (inbuffer - off);
324 for (
i = 0;
i < bytes / 4;
i++)
325 obuf[
i] =
c ^ buf[
i];
344 for (
i = 0;
i < 13;
i++)
346 for (
i = 0;
i < 7;
i++)
374 gaininfo[
i++] = gain;
387 int *quant_index_table)
391 quant_index_table[0] =
get_bits(&q->
gb, 6) - 6;
393 for (
i = 1;
i < p->total_subbands;
i++) {
395 if (
i >= p->js_subband_start * 2) {
396 vlc_index -= p->js_subband_start;
407 quant_index_table[
i] = quant_index_table[
i - 1] + j;
408 if (quant_index_table[
i] > 63 || quant_index_table[
i] < -63) {
410 "Invalid quantizer %d at position %d, outside [-63, 63] range\n",
411 quant_index_table[
i],
i);
431 int exp_index2[102] = { 0 };
432 int exp_index1[102] = { 0 };
434 int tmp_categorize_array[128 * 2] = { 0 };
435 int tmp_categorize_array1_idx = p->numvector_size;
436 int tmp_categorize_array2_idx = p->numvector_size;
447 for (
i = 32;
i > 0;
i =
i / 2) {
450 for (j = p->total_subbands; j > 0; j--) {
461 for (
i = 0;
i < p->total_subbands;
i++) {
464 exp_index1[
i] = exp_idx;
465 exp_index2[
i] = exp_idx;
467 tmpbias1 = tmpbias2 = num_bits;
469 for (j = 1; j < p->numvector_size; j++) {
470 if (tmpbias1 + tmpbias2 > 2 *
bits_left) {
473 for (
i = 0;
i < p->total_subbands;
i++) {
474 if (exp_index1[
i] < 7) {
475 v = (-2 * exp_index1[
i]) - quant_index_table[
i] +
bias;
484 tmp_categorize_array[tmp_categorize_array1_idx++] =
index;
491 for (
i = 0;
i < p->total_subbands;
i++) {
492 if (exp_index2[
i] > 0) {
493 v = (-2 * exp_index2[
i]) - quant_index_table[
i] +
bias;
502 tmp_categorize_array[--tmp_categorize_array2_idx] =
index;
509 for (
i = 0;
i < p->total_subbands;
i++)
512 for (
i = 0;
i < p->numvector_size - 1;
i++)
513 category_index[
i] = tmp_categorize_array[tmp_categorize_array2_idx++];
530 int idx = category_index[
i];
547 int *subband_coef_index,
int *subband_coef_sign,
554 if (subband_coef_index[
i]) {
556 if (subband_coef_sign[
i])
576 int *subband_coef_index,
int *subband_coef_sign)
579 int vlc, vd,
tmp, result;
589 for (j = vd - 1; j >= 0; j--) {
594 for (j = 0; j < vd; j++) {
595 if (subband_coef_index[
i * vd + j]) {
600 subband_coef_sign[
i * vd + j] = 0;
603 subband_coef_sign[
i * vd + j] = 0;
620 int *quant_index_table,
float *mlt_buffer)
631 for (band = 0; band < p->total_subbands; band++) {
636 for (j = 0; j < p->total_subbands; j++)
641 memset(subband_coef_index, 0,
sizeof(subband_coef_index));
642 memset(subband_coef_sign, 0,
sizeof(subband_coef_sign));
645 subband_coef_index, subband_coef_sign,
657 int category_index[128] = { 0 };
659 int quant_index_table[102];
667 for (
i=0;
i<p->total_subbands;
i++) {
686 int gain_index,
int gain_index_next)
690 fc1 =
pow2tab[gain_index + 63];
692 if (gain_index == gain_index_next) {
696 fc2 = q->
gain_table[15 + (gain_index_next - gain_index)];
713 cook_gains *gains_ptr,
float *previous_buffer)
741 cook_gains *gains_ptr,
float *previous_buffer)
750 q->
imlt_window(q, buffer1, gains_ptr, previous_buffer);
753 for (
i = 0;
i < 8;
i++)
754 if (gains_ptr->
now[
i] || gains_ptr->
now[
i + 1])
756 gains_ptr->
now[
i], gains_ptr->
now[
i + 1]);
759 memcpy(previous_buffer, buffer0,
774 int start =
cplband[p->js_subband_start];
775 int end =
cplband[p->subbands - 1];
776 int length = end - start + 1;
782 for (
i = 0;
i < length;
i++)
784 p->channel_coupling.table,
787 for (
i = 0;
i < length;
i++) {
789 if (v == (1<<p->js_vlc_bits)-1) {
793 decouple_tab[start +
i] = v;
813 float *decode_buffer,
814 float *mlt_buffer1,
float *mlt_buffer2)
818 tmp_idx = ((p->js_subband_start + subband) *
SUBBAND_SIZE) + j;
819 mlt_buffer1[
SUBBAND_SIZE * subband + j] = f1 * decode_buffer[tmp_idx];
820 mlt_buffer2[
SUBBAND_SIZE * subband + j] = f2 * decode_buffer[tmp_idx];
832 float *mlt_buffer_left,
float *mlt_buffer_right)
839 const float *cplscale;
844 memset(mlt_buffer_left, 0, 1024 *
sizeof(*mlt_buffer_left));
845 memset(mlt_buffer_right, 0, 1024 *
sizeof(*mlt_buffer_right));
851 for (
i = 0;
i < p->js_subband_start;
i++) {
853 mlt_buffer_left[
i * 20 + j] = decode_buffer[
i * 40 + j];
854 mlt_buffer_right[
i * 20 + j] = decode_buffer[
i * 40 + 20 + j];
860 idx = (1 << p->js_vlc_bits) - 1;
863 idx -= decouple_tab[cpl_tmp];
864 cplscale = q->
cplscales[p->js_vlc_bits - 2];
865 f1 = cplscale[decouple_tab[cpl_tmp] + 1];
867 q->
decouple(q, p,
i, f1, f2, decode_buffer,
868 mlt_buffer_left, mlt_buffer_right);
869 idx = (1 << p->js_vlc_bits) - 1;
884 const uint8_t *inbuffer,
890 p->bits_per_subpacket / 8);
892 p->bits_per_subpacket);
924 cook_gains *gains_ptr,
float *previous_buffer,
927 imlt_gain(q, decode_buffer, gains_ptr, previous_buffer);
942 const uint8_t *inbuffer,
float **outbuffer)
944 int sub_packet_size = p->size;
950 if (p->joint_stereo) {
957 if (p->num_channels == 2) {
965 p->mono_previous_buffer1,
966 outbuffer ? outbuffer[p->ch_idx] :
NULL);
968 if (p->num_channels == 2) {
971 p->mono_previous_buffer2,
972 outbuffer ? outbuffer[p->ch_idx + 1] :
NULL);
975 p->mono_previous_buffer2,
976 outbuffer ? outbuffer[p->ch_idx + 1] :
NULL);
984 int *got_frame_ptr,
AVPacket *avpkt)
986 const uint8_t *buf = avpkt->
data;
987 int buf_size = avpkt->
size;
989 float **samples =
NULL;
994 if (buf_size < avctx->block_align)
1002 samples = (
float **)
frame->extended_data;
1013 "frame subpacket size total > avctx->block_align!\n");
1024 "subpacket[%i] size %i js %i %i block_align %i\n",
1051#define PRINT(a, b) ff_dlog(q->avctx, " %s = %d\n", a, b);
1081 unsigned int channel_mask = 0;
1082 int samples_per_frame = 0;
1083 int total_channels = 0;
1120 samples_per_frame = bytestream2_get_be16(&gb);
1122 bytestream2_get_be32(&gb);
const FFCodec ff_cook_decoder
static av_cold void close(AVCodecParserContext *s)
Libavcodec external API header.
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
#define i(width, name, range_min, range_max)
Public libavutil channel layout APIs header.
#define FF_CODEC_DECODE_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...
static int decode_bytes(const uint8_t *inbuffer, uint8_t *out, int bytes)
Cook indata decoding, every 32 bits are XORed with 0x37c511f2.
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 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...
static int decode_envelope(COOKContext *q, COOKSubpacket *p, int *quant_index_table)
Create the quant index table needed for the envelope.
static void saturate_output_float(COOKContext *q, float *out)
Saturate the output signal and interleave.
static int joint_decode(COOKContext *q, COOKSubpacket *p, float *mlt_buffer_left, float *mlt_buffer_right)
function for decoding joint stereo data
static void dump_cook_context(COOKContext *q)
static void categorize(COOKContext *q, COOKSubpacket *p, const int *quant_index_table, int *category, int *category_index)
Calculate the category and category_index vector.
static void interpolate_float(COOKContext *q, float *buffer, int gain_index, int gain_index_next)
the actual requantization of the timedomain samples
static int decouple_info(COOKContext *q, COOKSubpacket *p, int *decouple_tab)
function for getting the jointstereo coupling information
static av_cold void init_pow2table(void)
#define COUPLING_VLC_BITS
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.
static void imlt_window_float(COOKContext *q, float *inbuffer, cook_gains *gains_ptr, float *previous_buffer)
Apply transform window, overlap buffers.
static float rootpow2tab[127]
static av_cold void init_cplscales_table(COOKContext *q)
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.
static int decode_subpacket(COOKContext *q, COOKSubpacket *p, const uint8_t *inbuffer, float **outbuffer)
Cook subpacket decoding.
#define DECODE_BYTES_PAD1(bytes)
static void expand_category(COOKContext *q, int *category, int *category_index)
Expand the category vector.
static int mono_decode(COOKContext *q, COOKSubpacket *p, float *mlt_buffer)
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.
static av_cold void init_gain_table(COOKContext *q)
static void decode_gain_info(GetBitContext *gb, int *gaininfo)
Fill the gain array for the timedomain quantization.
static av_cold int init_cook_mlt(COOKContext *q)
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.
static void decode_vectors(COOKContext *q, COOKSubpacket *p, int *category, int *quant_index_table, float *mlt_buffer)
Fill the mlt_buffer with mlt coefficients.
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,...
static av_cold int cook_decode_init(AVCodecContext *avctx)
Cook initialization.
static av_cold int init_cook_vlc_tables(COOKContext *q)
static float pow2tab[127]
static av_cold int cook_decode_close(AVCodecContext *avctx)
static int cook_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Cook AKA RealAudio G2 compatible decoder data.
static const int kmax_tab[7]
static const uint8_t *const ccpl_huffsyms[5]
static const void *const cvh_huffsyms[7]
static const int expbits_tab[8]
static const float quant_centroid_tab[7][14]
static const uint8_t ccpl_huffcounts[5][16]
static const uint8_t cvh_huffcounts[7][16]
static const int vd_tab[7]
static const int vpr_tab[7]
static const uint8_t envelope_quant_index_huffcounts[13][16]
#define MAX_COOK_VLC_ENTRIES
static const float dither_tab[9]
static const uint8_t envelope_quant_index_huffsyms[13][24]
static const float *const cplscales[5]
static const int cplband[51]
static const int invradix_tab[7]
static const int vhvlcsize_tab[7]
static const uint16_t fc[]
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
int(* init)(AVBSFContext *ctx)
bitstream reader API header.
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
static int get_bits_left(GetBitContext *gb)
static unsigned int get_bits1(GetBitContext *s)
static int get_bits_count(const GetBitContext *s)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
@ AV_SAMPLE_FMT_FLTP
float, planar
static void scale(int *out, const int *in, const int w, const int h, const int shift)
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
av_cold void ff_audiodsp_init(AudioDSPContext *c)
static int ff_thread_once(char *control, void(*routine)(void))
#define FFSWAP(type, a, b)
Memory handling functions.
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
void ff_sine_window_init(float *window, int n)
Generate a sine window.
#define FF_ARRAY_ELEMS(a)
int nb_channels
Number of channels in this layout.
main external API structure.
AVChannelLayout ch_layout
Audio channel layout.
enum AVSampleFormat sample_fmt
audio sample format
int64_t bit_rate
the average bitrate
int sample_rate
samples per second
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
This structure describes decoded (raw) audio or video data.
Context structure for the Lagged Fibonacci PRNG.
This structure stores compressed data.
void(* vector_clipf)(float *dst, const float *src, int len, float min, float max)
void(* imlt_window)(struct cook *q, float *buffer1, cook_gains *gains_ptr, float *previous_buffer)
VLC envelope_quant_index[13]
float decode_buffer_0[1060]
void(* scalar_dequant)(struct cook *q, int index, int quant_index, int *subband_coef_index, int *subband_coef_sign, float *mlt_p)
uint8_t * decoded_bytes_buffer
void(* decouple)(struct cook *q, COOKSubpacket *p, int subband, float f1, float f2, float *decode_buffer, float *mlt_buffer1, float *mlt_buffer2)
void(* interpolate)(struct cook *q, float *buffer, int gain_index, int gain_index_next)
float decode_buffer_1[1024]
float mono_mdct_output[2048]
COOKSubpacket subpacket[MAX_SUBPACKETS]
const float * cplscales[5]
float decode_buffer_2[1024]
void(* saturate_output)(struct cook *q, float *out)
float mono_previous_buffer1[1024]
unsigned int channel_mask
float mono_previous_buffer2[1024]
#define av_malloc_array(a, b)
#define avpriv_request_sample(...)
static const struct twinvq_data tab
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
@ AV_TX_FULL_IMDCT
Performs a full inverse MDCT rather than leaving out samples that can be derived through symmetry.
@ AV_TX_FLOAT_MDCT
Standard MDCT with a sample data type of float, double or int32_t, respectively.
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
static int get_unary(GetBitContext *gb, int stop, int len)
Get unary code of limited length.
int ff_vlc_init_from_lengths(VLC *vlc, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags, void *logctx)
Build VLC decoding tables suitable for use with get_vlc2()
void ff_vlc_free(VLC *vlc)
static int bias(int x, int c)