📄 cook_fix_all.c
字号:
diff -urN svn/libavcodec/cook.c fixpoint/libavcodec/cook.c
--- svn/libavcodec/cook.c 2007-03-20 19:31:39.000000000 +0100
+++ fixpoint/libavcodec/cook.c 2007-03-21 21:12:24.000000000 +0100
@@ -43,7 +43,9 @@
* available.
*/
-#include <math.h>
+/* Enable the following define for a fixed point implementation */
+//#define COOK_FIXPOINT
+
#include <stddef.h>
#include <stdio.h>
@@ -54,7 +56,12 @@
#include "bytestream.h"
#include "random.h"
-#include "cookdata.h"
+/* Include cook fixed/floating point types and data */
+#ifdef COOK_FIXPOINT
+# include "cookdata_fixpoint.h"
+#else
+# include "cookdata_float.h"
+#endif
/* the different Cook versions */
#define MONO 0x1000001
@@ -90,11 +97,6 @@
/* states */
AVRandomState random_state;
- /* transform data */
- MDCTContext mdct_ctx;
- DECLARE_ALIGNED_16(FFTSample, mdct_tmp[1024]); /* temporary storage for imlt */
- float* mlt_window;
-
/* gain buffers */
cook_gains gains1;
cook_gains gains2;
@@ -109,22 +111,27 @@
VLC sqvh[7]; //scalar quantization
VLC ccpl; //channel coupling
- /* generatable tables and related variables */
- int gain_size_factor;
- float gain_table[23];
- float pow2tab[127];
- float rootpow2tab[127];
+ /* Variables for fixed/float arithmetic routines */
+ realvars_t math;
/* data buffers */
-
uint8_t* decoded_bytes_buffer;
- DECLARE_ALIGNED_16(float,mono_mdct_output[2048]);
- float mono_previous_buffer1[1024];
- float mono_previous_buffer2[1024];
- float decode_buffer_1[1024];
- float decode_buffer_2[1024];
+ DECLARE_ALIGNED_16(REAL_T ,mono_mdct_output[2048]);
+ REAL_T mono_previous_buffer1[1024];
+ REAL_T mono_previous_buffer2[1024];
+ REAL_T decode_buffer_1[1024];
+ REAL_T decode_buffer_2[1024];
} COOKContext;
+
+/* Include fixed/floating point code for COOK decoder */
+#ifdef COOK_FIXPOINT
+# include "cook_fixpoint.h"
+#else
+# include "cook_float.h"
+#endif
+
+
/* debug functions */
#ifdef COOKDEBUG
@@ -159,37 +166,6 @@
/*************** init functions ***************/
-/* table generator */
-static void init_pow2table(COOKContext *q){
- int i;
- q->pow2tab[63] = 1.0;
- for (i=1 ; i<64 ; i++){
- q->pow2tab[63+i]=(float)((uint64_t)1<<i);
- q->pow2tab[63-i]=1.0/(float)((uint64_t)1<<i);
- }
-}
-
-/* table generator */
-static void init_rootpow2table(COOKContext *q){
- int i;
- q->rootpow2tab[63] = 1.0;
- for (i=1 ; i<64 ; i++){
- q->rootpow2tab[63+i]=sqrt((float)((uint64_t)1<<i));
- q->rootpow2tab[63-i]=sqrt(1.0/(float)((uint64_t)1<<i));
- }
-}
-
-/* table generator */
-static void init_gain_table(COOKContext *q) {
- int i;
- q->gain_size_factor = q->samples_per_channel/8;
- for (i=0 ; i<23 ; i++) {
- q->gain_table[i] = pow((double)q->pow2tab[i+52] ,
- (1.0/(double)q->gain_size_factor));
- }
-}
-
-
static int init_cook_vlc_tables(COOKContext *q) {
int i, result;
@@ -217,30 +193,6 @@
return result;
}
-static int init_cook_mlt(COOKContext *q) {
- int j;
- float alpha;
- int mlt_size = q->samples_per_channel;
-
- if ((q->mlt_window = av_malloc(sizeof(float)*mlt_size)) == 0)
- return -1;
-
- /* Initialize the MLT window: simple sine window. */
- alpha = M_PI / (2.0 * (float)mlt_size);
- for(j=0 ; j<mlt_size ; j++)
- q->mlt_window[j] = sin((j + 0.5) * alpha) * sqrt(2.0 / q->samples_per_channel);
-
- /* Initialize the MDCT. */
- if (ff_mdct_init(&q->mdct_ctx, av_log2(mlt_size)+1, 1)) {
- av_free(q->mlt_window);
- return -1;
- }
- av_log(NULL,AV_LOG_DEBUG,"MDCT initialized, order = %d.\n",
- av_log2(mlt_size)+1);
-
- return 0;
-}
-
/*************** init functions end ***********/
/**
@@ -298,11 +250,10 @@
av_log(avctx,AV_LOG_DEBUG, "Deallocating memory.\n");
/* Free allocated memory buffers. */
- av_free(q->mlt_window);
av_free(q->decoded_bytes_buffer);
- /* Free the transform. */
- ff_mdct_end(&q->mdct_ctx);
+ /* Free fixed/floating point resources. */
+ free_cook_math(q);
/* Free the VLC tables. */
for (i=0 ; i<13 ; i++) {
@@ -517,35 +468,6 @@
}
/**
- * The real requantization of the mltcoefs
- *
- * @param q pointer to the COOKContext
- * @param index index
- * @param quant_index quantisation index
- * @param subband_coef_index array of indexes to quant_centroid_tab
- * @param subband_coef_sign signs of coefficients
- * @param mlt_p pointer into the mlt buffer
- */
-
-static void scalar_dequant(COOKContext *q, int index, int quant_index,
- int* subband_coef_index, int* subband_coef_sign,
- float* mlt_p){
- int i;
- float f1;
-
- for(i=0 ; i<SUBBAND_SIZE ; i++) {
- if (subband_coef_index[i]) {
- f1 = quant_centroid_tab[index][subband_coef_index[i]];
- if (subband_coef_sign[i]) f1 = -f1;
- } else {
- /* noise coding if subband_coef_index[i] == 0 */
- f1 = dither_tab[index];
- if (av_random(&q->random_state) < 0x80000000) f1 = -f1;
- }
- mlt_p[i] = f1 * q->rootpow2tab[quant_index+63];
- }
-}
-/**
* Unpack the subband_coef_index and subband_coef_sign vectors.
*
* @param q pointer to the COOKContext
@@ -604,7 +526,7 @@
static void decode_vectors(COOKContext* q, int* category,
- int *quant_index_table, float* mlt_buffer){
+ int *quant_index_table, REAL_T *mlt_buffer){
/* A zero in this table means that the subband coefficient is
random noise coded. */
int subband_coef_index[SUBBAND_SIZE];
@@ -626,9 +548,9 @@
memset(subband_coef_index, 0, sizeof(subband_coef_index));
memset(subband_coef_sign, 0, sizeof(subband_coef_sign));
}
- scalar_dequant(q, index, quant_index_table[band],
- subband_coef_index, subband_coef_sign,
- &mlt_buffer[band * 20]);
+ scalar_dequant_math(q, index, quant_index_table[band],
+ subband_coef_index, subband_coef_sign,
+ &mlt_buffer[band * 20]);
}
if(q->total_subbands*SUBBAND_SIZE >= q->samples_per_channel){
@@ -645,7 +567,7 @@
* @param mlt_buffer2 pointer to right channel mlt coefficients
*/
-static void mono_decode(COOKContext *q, float* mlt_buffer) {
+static void mono_decode(COOKContext *q, REAL_T *mlt_buffer) {
int category_index[128];
int quant_index_table[102];
@@ -663,37 +585,6 @@
/**
- * the actual requantization of the timedomain samples
- *
- * @param q pointer to the COOKContext
- * @param buffer pointer to the timedomain buffer
- * @param gain_index index for the block multiplier
- * @param gain_index_next index for the next block multiplier
- */
-
-static void interpolate(COOKContext *q, float* buffer,
- int gain_index, int gain_index_next){
- int i;
- float fc1, fc2;
- fc1 = q->pow2tab[gain_index+63];
-
- if(gain_index == gain_index_next){ //static gain
- for(i=0 ; i<q->gain_size_factor ; i++){
- buffer[i]*=fc1;
- }
- return;
- } else { //smooth gain
- fc2 = q->gain_table[11 + (gain_index_next-gain_index)];
- for(i=0 ; i<q->gain_size_factor ; i++){
- buffer[i]*=fc1;
- fc1*=fc2;
- }
- return;
- }
-}
-
-
-/**
* The modulated lapped transform, this takes transform coefficients
* and transforms them into timedomain samples.
* Apply transform window, overlap buffers, apply gain profile
@@ -705,35 +596,21 @@
* @param previous_buffer pointer to the previous buffer to be used for overlapping
*/
-static void imlt_gain(COOKContext *q, float *inbuffer,
- cook_gains *gains_ptr, float* previous_buffer)
+static void imlt_gain(COOKContext *q, REAL_T *inbuffer,
+ cook_gains *gains_ptr, REAL_T *previous_buffer)
{
- const float fc = q->pow2tab[gains_ptr->previous[0] + 63];
- float *buffer0 = q->mono_mdct_output;
- float *buffer1 = q->mono_mdct_output + q->samples_per_channel;
+ REAL_T *buffer0 = q->mono_mdct_output;
+ REAL_T *buffer1 = q->mono_mdct_output + q->samples_per_channel;
+ int gain_size_factor = q->samples_per_channel/8;
int i;
- /* Inverse modified discrete cosine transform */
- q->mdct_ctx.fft.imdct_calc(&q->mdct_ctx, q->mono_mdct_output,
- inbuffer, q->mdct_tmp);
-
- /* The weird thing here, is that the two halves of the time domain
- * buffer are swapped. Also, the newest data, that we save away for
- * next frame, has the wrong sign. Hence the subtraction below.
- * Almost sounds like a complex conjugate/reverse data/FFT effect.
- */
-
- /* Apply window and overlap */
- for(i = 0; i < q->samples_per_channel; i++){
- buffer1[i] = buffer1[i] * fc * q->mlt_window[i] -
- previous_buffer[i] * q->mlt_window[q->samples_per_channel - 1 - i];
- }
+ imlt_math(q, inbuffer, gains_ptr->previous[0], previous_buffer);
/* Apply gain profile */
for (i = 0; i < 8; i++) {
if (gains_ptr->now[i] || gains_ptr->now[i + 1])
- interpolate(q, &buffer1[q->gain_size_factor * i],
- gains_ptr->now[i], gains_ptr->now[i + 1]);
+ interpolate_math(q, &buffer1[gain_size_factor * i],
+ gains_ptr->now[i], gains_ptr->now[i + 1]);
}
/* Save away the current to be previous block. */
@@ -780,21 +657,19 @@
* @param mlt_buffer2 pointer to right channel mlt coefficients
*/
-static void joint_decode(COOKContext *q, float* mlt_buffer1,
- float* mlt_buffer2) {
+static void joint_decode(COOKContext *q, REAL_T *mlt_buffer1,
+ REAL_T *mlt_buffer2) {
int i,j;
int decouple_tab[SUBBAND_SIZE];
- float decode_buffer[1060];
- int idx, cpl_tmp,tmp_idx;
- float f1,f2;
- float* cplscale;
+ REAL_T decode_buffer[1060];
+ int idx;
memset(decouple_tab, 0, sizeof(decouple_tab));
memset(decode_buffer, 0, sizeof(decode_buffer));
/* Make sure the buffers are zeroed out. */
- memset(mlt_buffer1,0, 1024*sizeof(float));
- memset(mlt_buffer2,0, 1024*sizeof(float));
+ memset(mlt_buffer1,0, 1024*sizeof(REAL_T));
+ memset(mlt_buffer2,0, 1024*sizeof(REAL_T));
decouple_info(q, decouple_tab);
mono_decode(q, decode_buffer);
@@ -810,17 +685,13 @@
the coefficients are stored in a coupling scheme. */
idx = (1 << q->js_vlc_bits) - 1;
for (i=q->js_subband_start ; i<q->subbands ; i++) {
- cpl_tmp = cplband[i];
- idx -=decouple_tab[cpl_tmp];
- cplscale = (float*)cplscales[q->js_vlc_bits-2]; //choose decoupler table
- f1 = cplscale[decouple_tab[cpl_tmp]];
- f2 = cplscale[idx-1];
+ int i1 = decouple_tab[cplband[i]];
+ int i2 = idx - i1 - 1;
for (j=0 ; j<SUBBAND_SIZE ; j++) {
- tmp_idx = ((q->js_subband_start + i)*20)+j;
- mlt_buffer1[20*i + j] = f1 * decode_buffer[tmp_idx];
- mlt_buffer2[20*i + j] = f2 * decode_buffer[tmp_idx];
+ REAL_T x = decode_buffer[((q->js_subband_start + i)*20)+j];
+ mlt_buffer1[20*i+j] = cplscale_math(x, q->js_vlc_bits, i1);
+ mlt_buffer2[20*i+j] = cplscale_math(x, q->js_vlc_bits, i2);
}
- idx = (1 << q->js_vlc_bits) - 1;
}
}
@@ -863,21 +734,12 @@
*/
static inline void
-mlt_compensate_output(COOKContext *q, float *decode_buffer,
- cook_gains *gains, float *previous_buffer,
+mlt_compensate_output(COOKContext *q, REAL_T *decode_buffer,
+ cook_gains *gains, REAL_T *previous_buffer,
int16_t *out, int chan)
{
- float *output = q->mono_mdct_output + q->samples_per_channel;
- int j;
-
imlt_gain(q, decode_buffer, gains, previous_buffer);
-
- /* Clip and convert floats to 16 bits.
- */
- for (j = 0; j < q->samples_per_channel; j++) {
- out[chan + q->nb_channels * j] =
- av_clip(lrintf(output[j]), -32768, 32767);
- }
+ output_math(q, out, chan);
}
@@ -1072,10 +934,6 @@
q->numvector_size = (1 << q->log2_numvector_size);
/* Generate tables */
- init_rootpow2table(q);
- init_pow2table(q);
- init_gain_table(q);
-
if (init_cook_vlc_tables(q) != 0)
return -1;
@@ -1105,8 +963,8 @@
q->gains2.now = q->gain_3;
q->gains2.previous = q->gain_4;
- /* Initialize transform. */
- if ( init_cook_mlt(q) != 0 )
+ /* Initialize fixed/floating point routines. */
+ if (init_cook_math(q) != 0)
return -1;
/* Try to catch some obviously faulty streams, othervise it might be exploitable */
diff -urN svn/libavcodec/cookdata_fixpoint.h fixpoint/libavcodec/cookdata_fixpoint.h
--- svn/libavcodec/cookdata_fixpoint.h 1970-01-01 01:00:00.000000000 +0100
+++ fixpoint/libavcodec/cookdata_fixpoint.h 2007-03-21 13:02:32.000000000 +0100
@@ -0,0 +1,82 @@
+/*
+ * COOK compatible decoder fixed point data types and constants
+ * Copyright (c) 2007 Ian Braithwaite
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -