⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cook.c

📁 君正早期ucos系统(只有早期的才不没有打包成库),MPLAYER,文件系统,图片解码,浏览,电子书,录音,想学ucos,识货的人就下吧 russblock fmradio explore set
💻 C
📖 第 1 页 / 共 4 页
字号:
    void (* saturate_output) (struct cook *q, int chan, int16_t *out);    GetBitContext       gb;    /* stream data */    int                 nb_channels;    int                 joint_stereo;    int                 bit_rate;    int                 sample_rate;    int                 samples_per_channel;    int                 samples_per_frame;    int                 subbands;    int                 log2_numvector_size;    int                 numvector_size;                //1 << log2_numvector_size;    int                 js_subband_start;    int                 total_subbands;    int                 num_vectors;    int                 bits_per_subpacket;    int                 cookversion;    /* states */    AVRandomState       random_state;    /* transform data *///    MDCTContext         mdct_ctx;    CookMDCTContext     mdct_ctx;    DECLARE_ALIGNED_16(CookFFTSample, mdct_tmp[1024]);  /* temporary storage for imlt */    CookLevel *         mlt_window;    /* gain buffers */    cook_gains          gains1;    cook_gains          gains2;    int                 gain_1[9];    int                 gain_2[9];    int                 gain_3[9];    int                 gain_4[9];    /* VLC data */    int                 js_vlc_bits;    VLC                 envelope_quant_index[13];    VLC                 sqvh[7];          //scalar quantization    VLC                 ccpl;             //channel coupling    /* generatable tables and related variables */    int                 gain_size_factor;    CookLevel           gain_table[23];    float               pow2tab[127];    float               rootpow2tab[127];    /* data buffers */    uint8_t*            decoded_bytes_buffer;    DECLARE_ALIGNED_16(CookFFTSample,mono_mdct_output[2048]);    CookFFTSample               mono_previous_buffer1[1024];    CookFFTSample               mono_previous_buffer2[1024];    CookFFTSample               decode_buffer_1[1024];    CookFFTSample               decode_buffer_2[1024];    CookFFTSample              decode_buffer_0[1060]; /* static allocation for joint decode */    CookFFTSample *cplscales[5];} COOKContext;/* debug functions */#ifdef COOKDEBUGstatic void dump_float_table(float* table, int size, int delimiter) {    int i=0;    av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i);    for (i=0 ; i<size ; i++) {        av_log(NULL, AV_LOG_ERROR, "%5.1f, ", table[i]);        if ((i+1)%delimiter == 0) av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i+1);    }}static void dump_int_table(int* table, int size, int delimiter) {    int i=0;    av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i);    for (i=0 ; i<size ; i++) {        av_log(NULL, AV_LOG_ERROR, "%d, ", table[i]);        if ((i+1)%delimiter == 0) av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i+1);    }}static void dump_short_table(short* table, int size, int delimiter) {    int i=0;    av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i);    for (i=0 ; i<size ; i++) {        av_log(NULL, AV_LOG_ERROR, "%d, ", table[i]);        if ((i+1)%delimiter == 0) av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i+1);    }}#endif/*************** 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] = FIXEDSAMPLE(                             pow((double)q->pow2tab[i+52] ,                               (1.0/(double)q->gain_size_factor)));    }}static int init_cook_vlc_tables(COOKContext *q) {    int i, result;    result = 0;    for (i=0 ; i<13 ; i++) {        result |= init_vlc (&q->envelope_quant_index[i], 9, 24,            envelope_quant_index_huffbits[i], 1, 1,            envelope_quant_index_huffcodes[i], 2, 2, 0);    }    av_log(NULL,AV_LOG_DEBUG,"sqvh VLC init\n");    for (i=0 ; i<7 ; i++) {        result |= init_vlc (&q->sqvh[i], vhvlcsize_tab[i], vhsize_tab[i],            cvh_huffbits[i], 1, 1,            cvh_huffcodes[i], 2, 2, 0);    }    if (q->nb_channels==2 && q->joint_stereo==1){        result |= init_vlc (&q->ccpl, 6, (1<<q->js_vlc_bits)-1,            ccpl_huffbits[q->js_vlc_bits-2], 1, 1,            ccpl_huffcodes[q->js_vlc_bits-2], 2, 2, 0);        av_log(NULL,AV_LOG_DEBUG,"Joint-stereo VLC used.\n");    }    av_log(NULL,AV_LOG_DEBUG,"VLC tables initialized.\n");    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(CookLevel)*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] =                 COOKFIXED31(sin((j + 0.5) * alpha) * sqrt(2.0 / q->samples_per_channel));   }    /* Initialize the MDCT. */    if( Cook_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;}static CookFFTSample *maybe_reformat_buffer32 (COOKContext *q, CookFFTSample *ptr, int n){    if (1)        return ptr;}static void init_cplscales_table (COOKContext *q) {    int i;    for (i=0;i<5;i++)        q->cplscales[i] = maybe_reformat_buffer32 (q, cplscales[i], (1<<(i+2))-1);}/*************** init functions end ***********//** * 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. * * @param inbuffer  pointer to byte array of indata * @param out       pointer to byte array of outdata * @param bytes     number of bytes */#define DECODE_BYTES_PAD1(bytes) (3 - ((bytes)+3) % 4)#define DECODE_BYTES_PAD2(bytes) ((bytes) % 4 + DECODE_BYTES_PAD1(2 * (bytes)))static inline int decode_bytes(uint8_t* inbuffer, uint8_t* out, int bytes){    int i, off;    uint32_t c;    uint32_t* buf;    uint32_t* obuf = (uint32_t*) out;    /* FIXME: 64 bit platforms would be able to do 64 bits at a time.     * I'm too lazy though, should be something like     * for(i=0 ; i<bitamount/64 ; i++)     *     (int64_t)out[i] = 0x37c511f237c511f2^be2me_64(int64_t)in[i]);     * Buffer alignment needs to be checked. */    off = (int)((long)inbuffer & 3);    buf = (uint32_t*) (inbuffer - off);    c = be2me_32((0x37c511f2 >> (off*8)) | (0x37c511f2 << (32-(off*8))));    bytes += 3 + off;    for (i = 0; i < bytes/4; i++)        obuf[i] = c ^ buf[i];    return off;}/** * Cook uninit */static int cook_decode_close(AVCodecContext *avctx){    int i;    COOKContext *q = avctx->priv_data;    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. */    Cook_mdct_end(&q->mdct_ctx);    /* Free the VLC tables. */    for (i=0 ; i<13 ; i++) {        free_vlc(&q->envelope_quant_index[i]);    }    for (i=0 ; i<7 ; i++) {        free_vlc(&q->sqvh[i]);    }    if(q->nb_channels==2 && q->joint_stereo==1 ){        free_vlc(&q->ccpl);    }    av_log(NULL,AV_LOG_DEBUG,"Memory deallocated.\n");    return 0;}/** * Fill the gain array for the timedomain quantization. * * @param q                 pointer to the COOKContext * @param gaininfo[9]       array of gain indices */static void decode_gain_info(GetBitContext *gb, int *gaininfo){    int i, n;    while (get_bits1(gb)) {}    n = get_bits_count(gb) - 1;     //amount of elements*2 to update    i = 0;    while (n--) {        int index = get_bits(gb, 3);        int gain = get_bits1(gb) ? get_bits(gb, 4) - 7 : -1;        while (i <= index) gaininfo[i++] = gain;    }    while (i <= 8) gaininfo[i++] = 0;}/** * Create the quant index table needed for the envelope. * * @param q                 pointer to the COOKContext * @param quant_index_table pointer to the array */static void decode_envelope(COOKContext *q, int* quant_index_table) {    int i,j, vlc_index;    quant_index_table[0]= get_bits(&q->gb,6) - 6;       //This is used later in categorize    for (i=1 ; i < q->total_subbands ; i++){        vlc_index=i;        if (i >= q->js_subband_start * 2) {            vlc_index-=q->js_subband_start;        } else {            vlc_index/=2;            if(vlc_index < 1) vlc_index = 1;        }        if (vlc_index>13) vlc_index = 13;           //the VLC tables >13 are identical to No. 13        j = get_vlc2(&q->gb, q->envelope_quant_index[vlc_index-1].table,                     q->envelope_quant_index[vlc_index-1].bits,2);        quant_index_table[i] = quant_index_table[i-1] + j - 12;    //differential encoding    }}/** * Calculate the category and category_index vector. * * @param q                     pointer to the COOKContext * @param quant_index_table     pointer to the array * @param category              pointer to the category array * @param category_index        pointer to the category_index array */static void categorize(COOKContext *q, int* quant_index_table,                       int* category, int* category_index){    int exp_idx, bias, tmpbias1, tmpbias2, bits_left, num_bits, index, v, i, j;    int exp_index2[102];    int exp_index1[102];    int tmp_categorize_array[128*2];    int tmp_categorize_array1_idx=q->numvector_size;    int tmp_categorize_array2_idx=q->numvector_size;    bits_left =  q->bits_per_subpacket - get_bits_count(&q->gb);    if(bits_left > q->samples_per_channel) {        bits_left = q->samples_per_channel +                    ((bits_left - q->samples_per_channel)*5)/8;        //av_log(NULL, AV_LOG_ERROR, "bits_left = %d\n",bits_left);    }    memset(&exp_index1,0,102*sizeof(int));    memset(&exp_index2,0,102*sizeof(int));    memset(&tmp_categorize_array,0,128*2*sizeof(int));    bias=-32;    /* Estimate bias. */    for (i=32 ; i>0 ; i=i/2){        num_bits = 0;        index = 0;        for (j=q->total_subbands ; j>0 ; j--){            exp_idx = av_clip((i - quant_index_table[index] + bias) / 2, 0, 7);            index++;            num_bits+=expbits_tab[exp_idx];        }        if(num_bits >= bits_left - 32){            bias+=i;        }    }    /* Calculate total number of bits. */    num_bits=0;    for (i=0 ; i<q->total_subbands ; i++) {        exp_idx = av_clip((bias - quant_index_table[i]) / 2, 0, 7);        num_bits += expbits_tab[exp_idx];        exp_index1[i] = exp_idx;        exp_index2[i] = exp_idx;    }    tmpbias1 = tmpbias2 = num_bits;    for (j = 1 ; j < q->numvector_size ; j++) {        if (tmpbias1 + tmpbias2 > 2*bits_left) {  /* ---> */            int max = -999999;            index=-1;            for (i=0 ; i<q->total_subbands ; i++){                if (exp_index1[i] < 7) {                    v = (-2*exp_index1[i]) - quant_index_table[i] + bias;                    if ( v >= max) {                        max = v;                        index = i;                    }                }            }            if(index==-1)break;            tmp_categorize_array[tmp_categorize_array1_idx++] = index;            tmpbias1 -= expbits_tab[exp_index1[index]] -

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -