cook_fix_all_new.c

来自「君正早期ucos系统(只有早期的才不没有打包成库),MPLAYER,文件系统,图」· C语言 代码 · 共 1,811 行 · 第 1/5 页

C
1,811
字号
-
-    /* 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 ***********/
-
-/**
- * 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/missalignment.
- * 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. */
-    ff_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;
-    int bitbias;
-
-    bitbias = get_bits_count(&q->gb);
-    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, tmpbias, bits_left, num_bits, index, v, i, j;
-    int exp_index2[102];
-    int exp_index1[102];
-
-    int tmp_categorize_array1[128];
-    int tmp_categorize_array1_idx=0;
-    int tmp_categorize_array2[128];
-    int tmp_categorize_array2_idx=0;
-    int category_index_size=0;
-
-    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_array1,0,128*sizeof(int));
-    memset(&tmp_categorize_array2,0,128*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 = (i - quant_index_table[index] + bias) / 2;
-            if (exp_idx<0){
-                exp_idx=0;
-            } else if(exp_idx >7) {
-                exp_idx=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 = (bias - quant_index_table[i]) / 2;
-        if (exp_idx<0) {
-            exp_idx=0;
-        } else if(exp_idx >7) {
-            exp_idx=7;
-        }
-        num_bits += expbits_tab[exp_idx];
-        exp_index1[i] = exp_idx;
-        exp_index2[i] = exp_idx;
-    }
-    tmpbias = bias = num_bits;
-
-    for (j = 1 ; j < q->numvector_size ; j++) {
-        if (tmpbias + bias > 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] - 32;
-                    if ( v >= max) {
-                        max = v;
-                        index = i;
-                    }
-                }
-            }
-            if(index==-1)break;
-            tmp_categorize_array1[tmp_categorize_array1_idx++] = index;
-            tmpbias -= expbits_tab[exp_index1[index]] -
-                       expbits_tab[exp_index1[index]+1];
-            ++exp_index1[index];
-        } else {  /* <--- */
-            int min = 999999;
-            index=-1;
-            for (i=0 ; i<q->total_subbands ; i++){
-                if(exp_index2[i] > 0){
-                    v = (-2*exp_index2[i])-quant_index_table[i];
-                    if ( v < min) {
-                        min = v;
-                        index = i;
-                    }
-                }
-            }
-            if(index == -1)break;
-            tmp_categorize_array2[tmp_categorize_array2_idx++] = index;
-            tmpbias -= expbits_tab[exp_index2[index]] -
-                       expbits_tab[exp_index2[index]-1];
-            --exp_index2[index];
-        }
-    }
-
-    for(i=0 ; i<q->total_subbands ; i++)
-        category[i] = exp_index2[i];
-
-    /* Concatenate the two arrays. */
-    for(i=tmp_categorize_array2_idx-1 ; i >= 0; i--)
-        category_index[category_index_size++] =  tmp_categorize_array2[i];
-
-    for(i=0;i<tmp_categorize_array1_idx;i++)
-        category_index[category_index_size++ ] =  tmp_categorize_array1[i];
-
-    /* FIXME: mc_sich_ra8_20.rm triggers this, not sure with what we
-       should fill the remaining bytes. */
-    for(i=category_index_size;i<q->numvector_size;i++)
-        category_index[i]=0;
-
-}
-
-
-/**
- * Expand the category vector.
- *
- * @param q                     pointer to the COOKContext
- * @param category              pointer to the category array
- * @param category_index        pointer to the category_index array
- */
-
-static void inline expand_category(COOKContext *q, int* category,
-                                   int* category_index){
-    int i;
-    for(i=0 ; i<q->num_vectors ; i++){
-        ++category[category_index[i]];
-    }
-}
-
-/**
- * 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
- * @param category              pointer to the category array
- * @param subband_coef_index    array of indexes to quant_centroid_tab
- * @param subband_coef_sign     signs of coefficients
- */
-
-static int unpack_SQVH(COOKContext *q, int category, int* subband_coef_index,
-                       int* subband_coef_sign) {
-    int i,j;
-    int vlc, vd ,tmp, result;
-    int ub;
-    int cb;
-
-    vd = vd_tab[category];
-    result = 0;
-    for(i=0 ; i<vpr_tab[category] ; i++){
-        ub = get_bits_count(&q->gb);
-        vlc = get_vlc2(&q->gb, q->sqvh[category].table, q->sqvh[category].bits, 3);
-        cb = get_bits_count(&q->gb);
-        if (q->bits_per_subpacket < get_bits_count(&q->gb)){
-            vlc = 0;
-            result = 1;
-        }
-        for(j=vd-1 ; j>=0 ; j--){
-            tmp = (vlc * invradix_tab[category])/0x100000;
-            subband_coef_index[vd*i+j] = vlc - tmp * (kmax_tab[category]+1);
-            vlc = tmp;
-        }
-        for(j=0 ; j<vd ; j++){
-            if (subband_coef_index[i*vd + j]) {
-                if(get_bits_count(&q->gb) < q->bits_per_subpacket){
-                    subband_coef_sign[i*vd+j] = get_bits1(&q->gb);
-                } else {
-                    result=1;
-                    subband_coef_sign[i*vd+j]=0;
-                }
-            } else {
-                subband_coef_sign[i*vd+j]=0;
-            }
-        }
-    }
-    return result;
-}
-

⌨️ 快捷键说明

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