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

📄 mpegaudio.c

📁 Trolltech公司发布的图形界面操作系统。可在qt-embedded-2.3.10平台上编译为嵌入式图形界面操作系统。
💻 C
📖 第 1 页 / 共 2 页
字号:
#if 0            printf("%2d:%d in=%x %x %d\n",                    j, i, vmax, scale_factor_table[index], index);#endif            /* store the scale factor */            assert(index >=0 && index <= 63);            sf[i] = index;        }        /* compute the transmission factor : look if the scale factors           are close enough to each other */        d1 = scale_diff_table[sf[0] - sf[1] + 64];        d2 = scale_diff_table[sf[1] - sf[2] + 64];                /* handle the 25 cases */        switch(d1 * 5 + d2) {        case 0*5+0:        case 0*5+4:        case 3*5+4:        case 4*5+0:        case 4*5+4:            code = 0;            break;        case 0*5+1:        case 0*5+2:        case 4*5+1:        case 4*5+2:            code = 3;            sf[2] = sf[1];            break;        case 0*5+3:        case 4*5+3:            code = 3;            sf[1] = sf[2];            break;        case 1*5+0:        case 1*5+4:        case 2*5+4:            code = 1;            sf[1] = sf[0];            break;        case 1*5+1:        case 1*5+2:        case 2*5+0:        case 2*5+1:        case 2*5+2:            code = 2;            sf[1] = sf[2] = sf[0];            break;        case 2*5+3:        case 3*5+3:            code = 2;            sf[0] = sf[1] = sf[2];            break;        case 3*5+0:        case 3*5+1:        case 3*5+2:            code = 2;            sf[0] = sf[2] = sf[1];            break;        case 1*5+3:            code = 2;            if (sf[0] > sf[2])              sf[0] = sf[2];            sf[1] = sf[2] = sf[0];            break;        default:            av_abort();        }        #if 0        printf("%d: %2d %2d %2d %d %d -> %d\n", j,                sf[0], sf[1], sf[2], d1, d2, code);#endif        scale_code[j] = code;        sf += 3;    }}/* The most important function : psycho acoustic module. In this   encoder there is basically none, so this is the worst you can do,   but also this is the simpler. */static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT]){    int i;    for(i=0;i<s->sblimit;i++) {        smr[i] = (int)(fixed_smr[i] * 10);    }}#define SB_NOTALLOCATED  0#define SB_ALLOCATED     1#define SB_NOMORE        2/* Try to maximize the smr while using a number of bits inferior to   the frame size. I tried to make the code simpler, faster and   smaller than other encoders :-) */static void compute_bit_allocation(MpegAudioContext *s,                                    short smr1[MPA_MAX_CHANNELS][SBLIMIT],                                   unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],                                   int *padding){    int i, ch, b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size;    int incr;    short smr[MPA_MAX_CHANNELS][SBLIMIT];    unsigned char subband_status[MPA_MAX_CHANNELS][SBLIMIT];    const unsigned char *alloc;    memcpy(smr, smr1, s->nb_channels * sizeof(short) * SBLIMIT);    memset(subband_status, SB_NOTALLOCATED, s->nb_channels * SBLIMIT);    memset(bit_alloc, 0, s->nb_channels * SBLIMIT);        /* compute frame size and padding */    max_frame_size = s->frame_size;    s->frame_frac += s->frame_frac_incr;    if (s->frame_frac >= 65536) {        s->frame_frac -= 65536;        s->do_padding = 1;        max_frame_size += 8;    } else {        s->do_padding = 0;    }    /* compute the header + bit alloc size */    current_frame_size = 32;    alloc = s->alloc_table;    for(i=0;i<s->sblimit;i++) {        incr = alloc[0];        current_frame_size += incr * s->nb_channels;        alloc += 1 << incr;    }    for(;;) {        /* look for the subband with the largest signal to mask ratio */        max_sb = -1;        max_ch = -1;        max_smr = 0x80000000;        for(ch=0;ch<s->nb_channels;ch++) {            for(i=0;i<s->sblimit;i++) {                if (smr[ch][i] > max_smr && subband_status[ch][i] != SB_NOMORE) {                    max_smr = smr[ch][i];                    max_sb = i;                    max_ch = ch;                }            }        }#if 0        printf("current=%d max=%d max_sb=%d alloc=%d\n",                current_frame_size, max_frame_size, max_sb,               bit_alloc[max_sb]);#endif                if (max_sb < 0)            break;                /* find alloc table entry (XXX: not optimal, should use           pointer table) */        alloc = s->alloc_table;        for(i=0;i<max_sb;i++) {            alloc += 1 << alloc[0];        }        if (subband_status[max_ch][max_sb] == SB_NOTALLOCATED) {            /* nothing was coded for this band: add the necessary bits */            incr = 2 + nb_scale_factors[s->scale_code[max_ch][max_sb]] * 6;            incr += total_quant_bits[alloc[1]];        } else {            /* increments bit allocation */            b = bit_alloc[max_ch][max_sb];            incr = total_quant_bits[alloc[b + 1]] -                 total_quant_bits[alloc[b]];        }        if (current_frame_size + incr <= max_frame_size) {            /* can increase size */            b = ++bit_alloc[max_ch][max_sb];            current_frame_size += incr;            /* decrease smr by the resolution we added */            smr[max_ch][max_sb] = smr1[max_ch][max_sb] - quant_snr[alloc[b]];            /* max allocation size reached ? */            if (b == ((1 << alloc[0]) - 1))                subband_status[max_ch][max_sb] = SB_NOMORE;            else                subband_status[max_ch][max_sb] = SB_ALLOCATED;        } else {            /* cannot increase the size of this subband */            subband_status[max_ch][max_sb] = SB_NOMORE;        }    }    *padding = max_frame_size - current_frame_size;    assert(*padding >= 0);#if 0    for(i=0;i<s->sblimit;i++) {        printf("%d ", bit_alloc[i]);    }    printf("\n");#endif}/* * Output the mpeg audio layer 2 frame. Note how the code is small * compared to other encoders :-) */static void encode_frame(MpegAudioContext *s,                         unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT],                         int padding){    int i, j, k, l, bit_alloc_bits, b, ch;    unsigned char *sf;    int q[3];    PutBitContext *p = &s->pb;    /* header */    put_bits(p, 12, 0xfff);    put_bits(p, 1, 1 - s->lsf); /* 1 = mpeg1 ID, 0 = mpeg2 lsf ID */    put_bits(p, 2, 4-2);  /* layer 2 */    put_bits(p, 1, 1); /* no error protection */    put_bits(p, 4, s->bitrate_index);    put_bits(p, 2, s->freq_index);    put_bits(p, 1, s->do_padding); /* use padding */    put_bits(p, 1, 0);             /* private_bit */    put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO);    put_bits(p, 2, 0); /* mode_ext */    put_bits(p, 1, 0); /* no copyright */    put_bits(p, 1, 1); /* original */    put_bits(p, 2, 0); /* no emphasis */    /* bit allocation */    j = 0;    for(i=0;i<s->sblimit;i++) {        bit_alloc_bits = s->alloc_table[j];        for(ch=0;ch<s->nb_channels;ch++) {            put_bits(p, bit_alloc_bits, bit_alloc[ch][i]);        }        j += 1 << bit_alloc_bits;    }        /* scale codes */    for(i=0;i<s->sblimit;i++) {        for(ch=0;ch<s->nb_channels;ch++) {            if (bit_alloc[ch][i])                 put_bits(p, 2, s->scale_code[ch][i]);        }    }    /* scale factors */    for(i=0;i<s->sblimit;i++) {        for(ch=0;ch<s->nb_channels;ch++) {            if (bit_alloc[ch][i]) {                sf = &s->scale_factors[ch][i][0];                switch(s->scale_code[ch][i]) {                case 0:                    put_bits(p, 6, sf[0]);                    put_bits(p, 6, sf[1]);                    put_bits(p, 6, sf[2]);                    break;                case 3:                case 1:                    put_bits(p, 6, sf[0]);                    put_bits(p, 6, sf[2]);                    break;                case 2:                    put_bits(p, 6, sf[0]);                    break;                }            }        }    }        /* quantization & write sub band samples */    for(k=0;k<3;k++) {        for(l=0;l<12;l+=3) {            j = 0;            for(i=0;i<s->sblimit;i++) {                bit_alloc_bits = s->alloc_table[j];                for(ch=0;ch<s->nb_channels;ch++) {                    b = bit_alloc[ch][i];                    if (b) {                        int qindex, steps, m, sample, bits;                        /* we encode 3 sub band samples of the same sub band at a time */                        qindex = s->alloc_table[j+b];                        steps = quant_steps[qindex];                        for(m=0;m<3;m++) {                            sample = s->sb_samples[ch][k][l + m][i];                            /* divide by scale factor */#ifdef USE_FLOATS                            {                                float a;                                a = (float)sample * scale_factor_inv_table[s->scale_factors[ch][i][k]];                                q[m] = (int)((a + 1.0) * steps * 0.5);                            }#else                            {                                int q1, e, shift, mult;                                e = s->scale_factors[ch][i][k];                                shift = scale_factor_shift[e];                                mult = scale_factor_mult[e];                                                                /* normalize to P bits */                                if (shift < 0)                                    q1 = sample << (-shift);                                else                                    q1 = sample >> shift;                                q1 = (q1 * mult) >> P;                                q[m] = ((q1 + (1 << P)) * steps) >> (P + 1);                            }#endif                            if (q[m] >= steps)                                q[m] = steps - 1;                            assert(q[m] >= 0 && q[m] < steps);                        }                        bits = quant_bits[qindex];                        if (bits < 0) {                            /* group the 3 values to save bits */                            put_bits(p, -bits,                                      q[0] + steps * (q[1] + steps * q[2]));#if 0                            printf("%d: gr1 %d\n",                                    i, q[0] + steps * (q[1] + steps * q[2]));#endif                        } else {#if 0                            printf("%d: gr3 %d %d %d\n",                                    i, q[0], q[1], q[2]);#endif                                                           put_bits(p, bits, q[0]);                            put_bits(p, bits, q[1]);                            put_bits(p, bits, q[2]);                        }                    }                }                /* next subband in alloc table */                j += 1 << bit_alloc_bits;             }        }    }    /* padding */    for(i=0;i<padding;i++)        put_bits(p, 1, 0);    /* flush */    flush_put_bits(p);}static int MPA_encode_frame(AVCodecContext *avctx,			    unsigned char *frame, int buf_size, void *data){    MpegAudioContext *s = avctx->priv_data;    short *samples = data;    short smr[MPA_MAX_CHANNELS][SBLIMIT];    unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT];    int padding, i;    for(i=0;i<s->nb_channels;i++) {        filter(s, i, samples + i, s->nb_channels);    }    for(i=0;i<s->nb_channels;i++) {        compute_scale_factors(s->scale_code[i], s->scale_factors[i],                               s->sb_samples[i], s->sblimit);    }    for(i=0;i<s->nb_channels;i++) {        psycho_acoustic_model(s, smr[i]);    }    compute_bit_allocation(s, smr, bit_alloc, &padding);    init_put_bits(&s->pb, frame, MPA_MAX_CODED_FRAME_SIZE);    encode_frame(s, bit_alloc, padding);        s->nb_samples += MPA_FRAME_SIZE;    return pbBufPtr(&s->pb) - s->pb.buf;}static int MPA_encode_close(AVCodecContext *avctx){    av_freep(&avctx->coded_frame);    return 0;}AVCodec mp2_encoder = {    "mp2",    CODEC_TYPE_AUDIO,    CODEC_ID_MP2,    sizeof(MpegAudioContext),    MPA_encode_init,    MPA_encode_frame,    MPA_encode_close,    NULL,};#undef FIX

⌨️ 快捷键说明

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