cook_fix_all_new.c

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

C
1,811
字号
-
-/**
- * Fill the mlt_buffer with mlt coefficients.
- *
- * @param q                 pointer to the COOKContext
- * @param category          pointer to the category array
- * @param quant_index_table pointer to the array
- * @param mlt_buffer        pointer to mlt coefficients
- */
-
-
-static void decode_vectors(COOKContext* q, int* category,
-                           int *quant_index_table, float* mlt_buffer){
-    /* A zero in this table means that the subband coefficient is
-       random noise coded. */
-    int subband_coef_index[SUBBAND_SIZE];
-    /* A zero in this table means that the subband coefficient is a
-       positive multiplicator. */
-    int subband_coef_sign[SUBBAND_SIZE];
-    int band, j;
-    int index=0;
 
-    for(band=0 ; band<q->total_subbands ; band++){
-        index = category[band];
-        if(category[band] < 7){
-            if(unpack_SQVH(q, category[band], subband_coef_index, subband_coef_sign)){
-                index=7;
-                for(j=0 ; j<q->total_subbands ; j++) category[band+j]=7;
-            }
-        }
-        if(index==7) {
-            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]);
-    }
-
-    if(q->total_subbands*SUBBAND_SIZE >= q->samples_per_channel){
-        return;
-    } /* FIXME: should this be removed, or moved into loop above? */
-}
-
-
-/**
- * function for decoding mono data
- *
- * @param q                 pointer to the COOKContext
- * @param mlt_buffer1       pointer to left channel mlt coefficients
- * @param mlt_buffer2       pointer to right channel mlt coefficients
- */
-
-static void mono_decode(COOKContext *q, float* mlt_buffer) {
-
-    int category_index[128];
-    int quant_index_table[102];
-    int category[128];
-
-    memset(&category, 0, 128*sizeof(int));
-    memset(&category_index, 0, 128*sizeof(int));
-
-    decode_envelope(q, quant_index_table);
-    q->num_vectors = get_bits(&q->gb,q->log2_numvector_size);
-    categorize(q, quant_index_table, category, category_index);
-    expand_category(q, category, category_index);
-    decode_vectors(q, category, quant_index_table, mlt_buffer);
-}
-
-
-/**
- * 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
- * and buffer management.
- *
- * @param q                 pointer to the COOKContext
- * @param inbuffer          pointer to the mltcoefficients
- * @param gains_ptr         current and previous gains
- * @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)
-{
-    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;
-    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];
-    }
-
-    /* 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]);
-    }
-
-    /* Save away the current to be previous block. */
-    memcpy(previous_buffer, buffer0, sizeof(float)*q->samples_per_channel);
-}
-
-
-/**
- * function for getting the jointstereo coupling information
- *
- * @param q                 pointer to the COOKContext
- * @param decouple_tab      decoupling array
- *
- */
-
-static void decouple_info(COOKContext *q, int* decouple_tab){
-    int length, i;
-
-    if(get_bits1(&q->gb)) {
-        if(cplband[q->js_subband_start] > cplband[q->subbands-1]) return;
-
-        length = cplband[q->subbands-1] - cplband[q->js_subband_start] + 1;
-        for (i=0 ; i<length ; i++) {
-            decouple_tab[cplband[q->js_subband_start] + i] = get_vlc2(&q->gb, q->ccpl.table, q->ccpl.bits, 2);
-        }
-        return;
-    }
-
-    if(cplband[q->js_subband_start] > cplband[q->subbands-1]) return;
-
-    length = cplband[q->subbands-1] - cplband[q->js_subband_start] + 1;
-    for (i=0 ; i<length ; i++) {
-       decouple_tab[cplband[q->js_subband_start] + i] = get_bits(&q->gb, q->js_vlc_bits);
-    }
-    return;
-}
-
-
-/**
- * function for decoding joint stereo data
- *
- * @param q                 pointer to the COOKContext
- * @param mlt_buffer1       pointer to left channel mlt coefficients
- * @param mlt_buffer2       pointer to right channel mlt coefficients
- */
-
-static void joint_decode(COOKContext *q, float* mlt_buffer1,
-                         float* 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;
-
-    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));
-    decouple_info(q, decouple_tab);
-    mono_decode(q, decode_buffer);
-
-    /* The two channels are stored interleaved in decode_buffer. */
-    for (i=0 ; i<q->js_subband_start ; i++) {
-        for (j=0 ; j<SUBBAND_SIZE ; j++) {
-            mlt_buffer1[i*20+j] = decode_buffer[i*40+j];
-            mlt_buffer2[i*20+j] = decode_buffer[i*40+20+j];
-        }
-    }
-
-    /* When we reach js_subband_start (the higher frequencies)
-       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];
-        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];
-        }
-        idx = (1 << q->js_vlc_bits) - 1;
-    }
-}
-
-/**
- * First part of subpacket decoding:
- *  decode raw stream bytes and read gain info.
- *
- * @param q                 pointer to the COOKContext
- * @param inbuffer          pointer to raw stream data
- * @param gain_ptr          array of current/prev gain pointers
- */
-
-static inline void
-decode_bytes_and_gain(COOKContext *q, uint8_t *inbuffer,
-                      cook_gains *gains_ptr)
-{
-    int offset;
-
-    offset = decode_bytes(inbuffer, q->decoded_bytes_buffer,
-                          q->bits_per_subpacket/8);
-    init_get_bits(&q->gb, q->decoded_bytes_buffer + offset,
-                  q->bits_per_subpacket);
-    decode_gain_info(&q->gb, gains_ptr->now);
-
-    /* Swap current and previous gains */
-    FFSWAP(int *, gains_ptr->now, gains_ptr->previous);
-}
-
-/**
- * Final part of subpacket decoding:
- *  Apply modulated lapped transform, gain compensation,
- *  clip and convert to integer.
- *
- * @param q                 pointer to the COOKContext
- * @param decode_buffer     pointer to the mlt coefficients
- * @param gain_ptr          array of current/prev gain pointers
- * @param previous_buffer   pointer to the previous buffer to be used for overlapping
- * @param out               pointer to the output buffer
- * @param chan              0: left or single channel, 1: right channel
- */
-
-static inline void
-mlt_compensate_output(COOKContext *q, float *decode_buffer,
-                      cook_gains *gains, float *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);
-    }
-}
-
-
-/**
- * Cook subpacket decoding. This function returns one decoded subpacket,
- * usually 1024 samples per channel.
- *
- * @param q                 pointer to the COOKContext
- * @param inbuffer          pointer to the inbuffer
- * @param sub_packet_size   subpacket size
- * @param outbuffer         pointer to the outbuffer
- */
-
-
-static int decode_subpacket(COOKContext *q, uint8_t *inbuffer,
-                            int sub_packet_size, int16_t *outbuffer) {
-    /* packet dump */
-//    for (i=0 ; i<sub_packet_size ; i++) {
-//        av_log(NULL, AV_LOG_ERROR, "%02x", inbuffer[i]);
-//    }
-//    av_log(NULL, AV_LOG_ERROR, "\n");
-
-    decode_bytes_and_gain(q, inbuffer, &q->gains1);
-
-    if (q->joint_stereo) {
-        joint_decode(q, q->decode_buffer_1, q->decode_buffer_2);
-    } else {
-        mono_decode(q, q->decode_buffer_1);
-
-        if (q->nb_channels == 2) {
-            decode_bytes_and_gain(q, inbuffer + sub_packet_size/2, &q->gains2);
-            mono_decode(q, q->decode_buffer_2);
-        }
-    }
-
-    mlt_compensate_output(q, q->decode_buffer_1, &q->gains1,
-                          q->mono_previous_buffer1, outbuffer, 0);
-
-    if (q->nb_channels == 2) {
-        if (q->joint_stereo) {
-            mlt_compensate_output(q, q->decode_buffer_2, &q->gains1,
-                                  q->mono_previous_buffer2, outbuffer, 1);
-        } else {
-            mlt_compensate_output(q, q->decode_buffer_2, &q->gains2,
-                                  q->mono_previous_buffer2, outbuffer, 1);
-        }
-    }
-    return q->samples_per_frame * sizeof(int16_t);
-}
-
-
-/**
- * Cook frame decoding
- *
- * @param avctx     pointer to the AVCodecContext
- */
-
-static int cook_decode_frame(AVCodecContext *avctx,
-            void *data, int *data_size,
-            uint8_t *buf, int buf_size) {
-    COOKContext *q = avctx->priv_data;
-
-    if (buf_size < avctx->block_align)
-        return buf_size;
-
-    *data_size = decode_subpacket(q, buf, avctx->block_align, data);
-
-    /* Discard the first two frames: no valid audio. */
-    if (avctx->frame_number < 2) *data_size = 0;
-
-    return avctx->block_align;
-}
-
-#ifdef COOKDEBUG
-static void dump_cook_context(COOKContext *q)
-{
-    //int i=0;

⌨️ 快捷键说明

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