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

📄 mpegaudiodec.c

📁 tcpmp播放器的flv插件
💻 C
📖 第 1 页 / 共 5 页
字号:
    offset = *synth_buf_offset;    synth_buf = synth_buf_ptr + offset;    for(j=0;j<32;j++) {        v = tmp[j];#if FRAC_BITS <= 15        /* NOTE: can cause a loss in precision if very high amplitude           sound */        if (v > 32767)            v = 32767;        else if (v < -32768)            v = -32768;#endif        synth_buf[j] = v;    }    /* copy to avoid wrap */    memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));    samples2 = samples + 31 * incr;    w = window;    w2 = window + 31;    sum = *dither_state;    p = synth_buf + 16;    SUM8(sum, +=, w, p);    p = synth_buf + 48;    SUM8(sum, -=, w + 32, p);    *samples = round_sample(&sum);    samples += incr;    w++;    /* we calculate two samples at the same time to avoid one memory       access per two sample */    for(j=1;j<16;j++) {        sum2 = 0;        p = synth_buf + 16 + j;        SUM8P2(sum, +=, sum2, -=, w, w2, p);        p = synth_buf + 48 - j;        SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p);        *samples = round_sample(&sum);        samples += incr;        sum += sum2;        *samples2 = round_sample(&sum);        samples2 -= incr;        w++;        w2--;    }        p = synth_buf + 32;    SUM8(sum, -=, w + 32, p);    *samples = round_sample(&sum);    *dither_state= sum;    offset = (offset - 32) & 511;    *synth_buf_offset = offset;}#define C3 FIXHR(0.86602540378443864676/2)/* 0.5 / cos(pi*(2*i+1)/36) */static const int icos36[9] = {    FIXR(0.50190991877167369479),    FIXR(0.51763809020504152469), //0    FIXR(0.55168895948124587824),    FIXR(0.61038729438072803416),    FIXR(0.70710678118654752439), //1    FIXR(0.87172339781054900991),    FIXR(1.18310079157624925896),    FIXR(1.93185165257813657349), //2    FIXR(5.73685662283492756461),};/* 12 points IMDCT. We compute it "by hand" by factorizing obvious   cases. */static void imdct12(int *out, int *in){    int in0, in1, in2, in3, in4, in5, t1, t2;    in0= in[0*3];    in1= in[1*3] + in[0*3];    in2= in[2*3] + in[1*3];    in3= in[3*3] + in[2*3];    in4= in[4*3] + in[3*3];    in5= in[5*3] + in[4*3];    in5 += in3;    in3 += in1;    in2= MULH(2*in2, C3);    in3= MULH(2*in3, C3);        t1 = in0 - in4;    t2 = MULL(in1 - in5, icos36[4]);    out[ 7]=     out[10]= t1 + t2;    out[ 1]=    out[ 4]= t1 - t2;    in0 += in4>>1;    in4 = in0 + in2;    in1 += in5>>1;    in5 = MULL(in1 + in3, icos36[1]);        out[ 8]=     out[ 9]= in4 + in5;    out[ 2]=    out[ 3]= in4 - in5;        in0 -= in2;    in1 = MULL(in1 - in3, icos36[7]);    out[ 0]=    out[ 5]= in0 - in1;    out[ 6]=    out[11]= in0 + in1;    }/* cos(pi*i/18) */#define C1 FIXHR(0.98480775301220805936/2)#define C2 FIXHR(0.93969262078590838405/2)#define C3 FIXHR(0.86602540378443864676/2)#define C4 FIXHR(0.76604444311897803520/2)#define C5 FIXHR(0.64278760968653932632/2)#define C6 FIXHR(0.5/2)#define C7 FIXHR(0.34202014332566873304/2)#define C8 FIXHR(0.17364817766693034885/2)/* using Lee like decomposition followed by hand coded 9 points DCT */static void imdct36(int *out, int *buf, int *in, int *win){    int i, j, t0, t1, t2, t3, s0, s1, s2, s3;    int tmp[18], *tmp1, *in1;    for(i=17;i>=1;i--)        in[i] += in[i-1];    for(i=17;i>=3;i-=2)        in[i] += in[i-2];    for(j=0;j<2;j++) {        tmp1 = tmp + j;        in1 = in + j;#if 0//more accurate but slower        int64_t t0, t1, t2, t3;        t2 = in1[2*4] + in1[2*8] - in1[2*2];                t3 = (in1[2*0] + (int64_t)(in1[2*6]>>1))<<32;        t1 = in1[2*0] - in1[2*6];        tmp1[ 6] = t1 - (t2>>1);        tmp1[16] = t1 + t2;        t0 = MUL64(2*(in1[2*2] + in1[2*4]),    C2);        t1 = MUL64(   in1[2*4] - in1[2*8] , -2*C8);        t2 = MUL64(2*(in1[2*2] + in1[2*8]),   -C4);                tmp1[10] = (t3 - t0 - t2) >> 32;        tmp1[ 2] = (t3 + t0 + t1) >> 32;        tmp1[14] = (t3 + t2 - t1) >> 32;                tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);        t2 = MUL64(2*(in1[2*1] + in1[2*5]),    C1);        t3 = MUL64(   in1[2*5] - in1[2*7] , -2*C7);        t0 = MUL64(2*in1[2*3], C3);        t1 = MUL64(2*(in1[2*1] + in1[2*7]),   -C5);        tmp1[ 0] = (t2 + t3 + t0) >> 32;        tmp1[12] = (t2 + t1 - t0) >> 32;        tmp1[ 8] = (t3 - t1 - t0) >> 32;#else        t2 = in1[2*4] + in1[2*8] - in1[2*2];                t3 = in1[2*0] + (in1[2*6]>>1);        t1 = in1[2*0] - in1[2*6];        tmp1[ 6] = t1 - (t2>>1);        tmp1[16] = t1 + t2;        t0 = MULH(2*(in1[2*2] + in1[2*4]),    C2);        t1 = MULH(   in1[2*4] - in1[2*8] , -2*C8);        t2 = MULH(2*(in1[2*2] + in1[2*8]),   -C4);                tmp1[10] = t3 - t0 - t2;        tmp1[ 2] = t3 + t0 + t1;        tmp1[14] = t3 + t2 - t1;                tmp1[ 4] = MULH(2*(in1[2*5] + in1[2*7] - in1[2*1]), -C3);        t2 = MULH(2*(in1[2*1] + in1[2*5]),    C1);        t3 = MULH(   in1[2*5] - in1[2*7] , -2*C7);        t0 = MULH(2*in1[2*3], C3);        t1 = MULH(2*(in1[2*1] + in1[2*7]),   -C5);        tmp1[ 0] = t2 + t3 + t0;        tmp1[12] = t2 + t1 - t0;        tmp1[ 8] = t3 - t1 - t0;#endif    }    i = 0;    for(j=0;j<4;j++) {        t0 = tmp[i];        t1 = tmp[i + 2];        s0 = t1 + t0;        s2 = t1 - t0;        t2 = tmp[i + 1];        t3 = tmp[i + 3];        s1 = MULL(t3 + t2, icos36[j]);        s3 = MULL(t3 - t2, icos36[8 - j]);                t0 = s0 + s1;        t1 = s0 - s1;        out[(9 + j)*SBLIMIT] =  MULH(t1, win[9 + j]) + buf[9 + j];        out[(8 - j)*SBLIMIT] =  MULH(t1, win[8 - j]) + buf[8 - j];        buf[9 + j] = MULH(t0, win[18 + 9 + j]);        buf[8 - j] = MULH(t0, win[18 + 8 - j]);                t0 = s2 + s3;        t1 = s2 - s3;        out[(9 + 8 - j)*SBLIMIT] =  MULH(t1, win[9 + 8 - j]) + buf[9 + 8 - j];        out[(        j)*SBLIMIT] =  MULH(t1, win[        j]) + buf[        j];        buf[9 + 8 - j] = MULH(t0, win[18 + 9 + 8 - j]);        buf[      + j] = MULH(t0, win[18         + j]);        i += 4;    }    s0 = tmp[16];    s1 = MULL(tmp[17], icos36[4]);    t0 = s0 + s1;    t1 = s0 - s1;    out[(9 + 4)*SBLIMIT] =  MULH(t1, win[9 + 4]) + buf[9 + 4];    out[(8 - 4)*SBLIMIT] =  MULH(t1, win[8 - 4]) + buf[8 - 4];    buf[9 + 4] = MULH(t0, win[18 + 9 + 4]);    buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);}/* header decoding. MUST check the header before because no   consistency check is done there. Return 1 if free format found and   that the frame size must be computed externally */static int decode_header(MPADecodeContext *s, uint32_t header){    int sample_rate, frame_size, mpeg25, padding;    int sample_rate_index, bitrate_index;    if (header & (1<<20)) {        s->lsf = (header & (1<<19)) ? 0 : 1;        mpeg25 = 0;    } else {        s->lsf = 1;        mpeg25 = 1;    }        s->layer = 4 - ((header >> 17) & 3);    /* extract frequency */    sample_rate_index = (header >> 10) & 3;    sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);    sample_rate_index += 3 * (s->lsf + mpeg25);    s->sample_rate_index = sample_rate_index;    s->error_protection = ((header >> 16) & 1) ^ 1;    s->sample_rate = sample_rate;    bitrate_index = (header >> 12) & 0xf;    padding = (header >> 9) & 1;    //extension = (header >> 8) & 1;    s->mode = (header >> 6) & 3;    s->mode_ext = (header >> 4) & 3;    //copyright = (header >> 3) & 1;    //original = (header >> 2) & 1;    //emphasis = header & 3;    if (s->mode == MPA_MONO)        s->nb_channels = 1;    else        s->nb_channels = 2;        if (bitrate_index != 0) {        frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];        s->bit_rate = frame_size * 1000;        switch(s->layer) {        case 1:            frame_size = (frame_size * 12000) / sample_rate;            frame_size = (frame_size + padding) * 4;            break;        case 2:            frame_size = (frame_size * 144000) / sample_rate;            frame_size += padding;            break;        default:        case 3:            frame_size = (frame_size * 144000) / (sample_rate << s->lsf);            frame_size += padding;            break;        }        s->frame_size = frame_size;    } else {        /* if no frame size computed, signal it */        if (!s->free_format_frame_size)            return 1;        /* free format: compute bitrate and real frame size from the           frame size we extracted by reading the bitstream */        s->frame_size = s->free_format_frame_size;        switch(s->layer) {        case 1:            s->frame_size += padding  * 4;            s->bit_rate = (s->frame_size * sample_rate) / 48000;            break;        case 2:            s->frame_size += padding;            s->bit_rate = (s->frame_size * sample_rate) / 144000;            break;        default:        case 3:            s->frame_size += padding;            s->bit_rate = (s->frame_size * (sample_rate << s->lsf)) / 144000;            break;        }    }    #if defined(DEBUG)    printf("layer%d, %d Hz, %d kbits/s, ",           s->layer, s->sample_rate, s->bit_rate);    if (s->nb_channels == 2) {        if (s->layer == 3) {            if (s->mode_ext & MODE_EXT_MS_STEREO)                printf("ms-");            if (s->mode_ext & MODE_EXT_I_STEREO)                printf("i-");        }        printf("stereo");    } else {        printf("mono");    }    printf("\n");#endif    return 0;}/* useful helper to get mpeg audio stream infos. Return -1 if error in   header, otherwise the coded frame size in bytes */int mpa_decode_header(AVCodecContext *avctx, uint32_t head){    MPADecodeContext s1, *s = &s1;    memset( s, 0, sizeof(MPADecodeContext) );    if (ff_mpa_check_header(head) != 0)        return -1;    if (decode_header(s, head) != 0) {        return -1;    }    switch(s->layer) {    case 1:        avctx->frame_size = 384;        break;    case 2:        avctx->frame_size = 1152;        break;    default:    case 3:        if (s->lsf)            avctx->frame_size = 576;        else            avctx->frame_size = 1152;        break;    }    avctx->sample_rate = s->sample_rate;    avctx->channels = s->nb_channels;    avctx->bit_rate = s->bit_rate;    avctx->sub_id = s->layer;    return s->frame_size;}/* return the number of decoded frames */static int mp_decode_layer1(MPADecodeContext *s){    int bound, i, v, n, ch, j, mant;    uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT];    uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT];    if (s->mode == MPA_JSTEREO)         bound = (s->mode_ext + 1) * 4;    else        bound = SBLIMIT;    /* allocation bits */    for(i=0;i<bound;i++) {        for(ch=0;ch<s->nb_channels;ch++) {            allocation[ch][i] = get_bits(&s->gb, 4);        }    }    for(i=bound;i<SBLIMIT;i++) {        allocation[0][i] = get_bits(&s->gb, 4);    }    /* scale factors */    for(i=0;i<bound;i++) {        for(ch=0;ch<s->nb_channels;ch++) {            if (allocation[ch][i])                scale_factors[ch][i] = get_bits(&s->gb, 6);        }    }    for(i=bound;i<SBLIMIT;i++) {        if (allocation[0][i]) {            scale_factors[0][i] = get_bits(&s->gb, 6);            scale_factors[1][i] = get_bits(&s->gb, 6);        }    }        /* compute samples */    for(j=0;j<12;j++) {        for(i=0;i<bound;i++) {            for(ch=0;ch<s->nb_channels;ch++) {                n = allocation[ch][i];                if (n) {                    mant = get_bits(&s->gb, n + 1);                    v = l1_unscale(n, mant, scale_factors[ch][i]);                } else {                    v = 0;                }                s->sb_samples[ch][j][i] = v;            }        }        for(i=bound;i<SBLIMIT;i++) {            n = allocation[0][i];            if (n) {                mant = get_bits(&s->gb, n + 1);                v = l1_unscale(n, mant, scale_factors[0][i]);                s->sb_samples[0][j][i] = v;                v = l1_unscale(n, mant, scale_factors[1][i]);                s->sb_samples[1][j][i] = v;            } else {                s->sb_samples[0][j][i] = 0;                s->sb_samples[1][j][i] = 0;            }        }    }    return 12;}/* bitrate is in kb/s */int l2_select_table(int bitrate, int nb_channels, int freq, int lsf){    int ch_bitrate, table;

⌨️ 快捷键说明

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