transrate.c

来自「VLC媒体播放程序」· C语言 代码 · 共 2,148 行 · 第 1/5 页

C
2,148
字号
{    if( s->i_bit_out != 8 ) bs_write(s, 0, s->i_bit_out);}/////---- begin ext mpeg codeconst uint8_t non_linear_mquant_table[32] ={    0, 1, 2, 3, 4, 5, 6, 7,    8,10,12,14,16,18,20,22,    24,28,32,36,40,44,48,52,    56,64,72,80,88,96,104,112};const uint8_t map_non_linear_mquant[113] ={    0,1,2,3,4,5,6,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,    16,17,17,17,18,18,18,18,19,19,19,19,20,20,20,20,21,21,21,21,22,22,    22,22,23,23,23,23,24,24,24,24,24,24,24,25,25,25,25,25,25,25,26,26,    26,26,26,26,26,26,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,29,    29,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,31,31,31,31,31};static int scale_quant( unsigned int q_scale_type, double quant ){    int iquant;    if (q_scale_type)    {        iquant = (int) floor(quant+0.5);        /* clip mquant to legal (linear) range */        if (iquant<1) iquant = 1;        if (iquant>112) iquant = 112;        iquant = non_linear_mquant_table[map_non_linear_mquant[iquant]];    }    else    {        /* clip mquant to legal (linear) range */        iquant = (int)floor(quant+0.5);        if (iquant<2) iquant = 2;        if (iquant>62) iquant = 62;        iquant = (iquant/2)*2; // Must be *even*    }    return iquant;}static int increment_quant( transrate_t *tr, int quant ){    if( tr->q_scale_type )    {        assert(quant >= 1 && quant <= 112 );        quant = map_non_linear_mquant[quant] + 1;        if( tr->picture_coding_type == P_TYPE )            quant += tr->level_p;        if( quant > 31) quant = 31;        quant = non_linear_mquant_table[quant];    }    else    {        assert(!(quant & 1));        quant += 2;        if( tr->picture_coding_type == P_TYPE )            quant += 2 * tr->level_p;        if (quant > 62) quant = 62;    }    return quant;}static inline int intmax( register int x, register int y ){    return x < y ? y : x;}static inline int intmin( register int x, register int y ){    return x < y ? x : y;}static int getNewQuant( transrate_t *tr, int curQuant){    bs_transrate_t *bs = &tr->bs;    double calc_quant, quant_to_use;    int mquant = 0;    switch ( tr->picture_coding_type )    {        case I_TYPE:        case P_TYPE:            mquant = increment_quant( tr, curQuant );            break;        case B_TYPE:            tr->quant_corr = (((bs->i_byte_in - (bs->p_r - 4 - bs->p_c)) / tr->fact_x) - (bs->i_byte_out + (bs->p_w - bs->p_ow))) / REACT_DELAY + B_HANDICAP;            calc_quant = curQuant * tr->current_fact_x;            quant_to_use = calc_quant - tr->quant_corr;            mquant = intmax(scale_quant( tr->q_scale_type, quant_to_use), increment_quant( tr, curQuant) );            break;        default:            assert(0);            break;    }    /*        LOGF("type: %s orig_quant: %3i calc_quant: %7.1f quant_corr: %7.1f using_quant: %3i\n",        (picture_coding_type == I_TYPE ? "I_TYPE" : (picture_coding_type == P_TYPE ? "P_TYPE" : "B_TYPE")),        (int)curQuant, (float)calc_quant, (float)quant_corr, (int)mquant);    */    assert(mquant >= curQuant);    return mquant;}static inline int isNotEmpty(RunLevel *blk){    return (blk->level);}#include "putvlc.h"static void putAC( bs_transrate_t *bs, int run, int signed_level, int vlcformat){    int level, len;    const VLCtable *ptab = NULL;    level = (signed_level<0) ? -signed_level : signed_level; /* abs(signed_level) */    assert(!(run<0 || run>63 || level==0 || level>2047));    len = 0;    if (run<2 && level<41)    {        if (vlcformat)  ptab = &dct_code_tab1a[run][level-1];        else ptab = &dct_code_tab1[run][level-1];        len = ptab->len;    }    else if (run<32 && level<6)    {        if (vlcformat) ptab = &dct_code_tab2a[run-2][level-1];        else ptab = &dct_code_tab2[run-2][level-1];        len = ptab->len;    }    if (len) /* a VLC code exists */    {        bs_write( bs, ptab->code, len);        bs_write( bs, signed_level<0, 1); /* sign */    }    else    {        bs_write( bs, 1l, 6); /* Escape */        bs_write( bs, run, 6); /* 6 bit code for run */        bs_write( bs, ((unsigned int)signed_level) & 0xFFF, 12);    }}static inline void putACfirst( bs_transrate_t *bs, int run, int val){    if (run==0 && (val==1 || val==-1)) bs_write( bs, 2|(val<0),2);    else putAC( bs, run,val,0);}static void putnonintrablk( bs_transrate_t *bs, RunLevel *blk){    assert(blk->level);    putACfirst( bs, blk->run, blk->level);    blk++;    while(blk->level)    {        putAC( bs, blk->run, blk->level, 0);        blk++;    }    bs_write( bs, 2,2);}#include "getvlc.h"static const int non_linear_quantizer_scale [] ={     0,  1,  2,  3,  4,  5,   6,   7,     8, 10, 12, 14, 16, 18,  20,  22,    24, 28, 32, 36, 40, 44,  48,  52,    56, 64, 72, 80, 88, 96, 104, 112};static inline int get_macroblock_modes( transrate_t *tr ){    bs_transrate_t *bs = &tr->bs;    int macroblock_modes;    const MBtab * tab;    switch( tr->picture_coding_type)    {        case I_TYPE:            tab = MB_I + UBITS (bs->i_bit_in_cache, 1);            bs_flush( bs, tab->len );            macroblock_modes = tab->modes;            if ((! ( tr->frame_pred_frame_dct)) && ( tr->picture_structure == FRAME_PICTURE))            {                macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;                bs_flush( bs, 1 );            }            return macroblock_modes;        case P_TYPE:            tab = MB_P + UBITS (bs->i_bit_in_cache, 5);            bs_flush( bs, tab->len );            macroblock_modes = tab->modes;            if (tr->picture_structure != FRAME_PICTURE)            {                if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)                {                    macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;                    bs_flush( bs, 2 );                }                return macroblock_modes;            }            else if (tr->frame_pred_frame_dct)            {                if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)                    macroblock_modes |= MC_FRAME;                return macroblock_modes;            }            else            {                if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)                {                    macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;                    bs_flush( bs, 2 );                }                if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))                {                    macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;                    bs_flush( bs, 1 );                }                return macroblock_modes;            }        case B_TYPE:            tab = MB_B + UBITS (bs->i_bit_in_cache, 6);            bs_flush( bs, tab->len );            macroblock_modes = tab->modes;            if( tr->picture_structure != FRAME_PICTURE)            {                if (! (macroblock_modes & MACROBLOCK_INTRA))                {                    macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;                    bs_flush( bs, 2 );                }                return macroblock_modes;            }            else if (tr->frame_pred_frame_dct)            {                /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */                macroblock_modes |= MC_FRAME;                return macroblock_modes;            }            else            {                if (macroblock_modes & MACROBLOCK_INTRA) goto intra;                macroblock_modes |= UBITS (bs->i_bit_in_cache, 2) * MOTION_TYPE_BASE;                bs_flush( bs, 2 );                if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN))                {                    intra:                    macroblock_modes |= UBITS (bs->i_bit_in_cache, 1) * DCT_TYPE_INTERLACED;                    bs_flush( bs, 1 );                }                return macroblock_modes;            }        default:            return 0;    }}static inline int get_quantizer_scale( transrate_t *tr ){    bs_transrate_t *bs = &tr->bs;    int quantizer_scale_code;    quantizer_scale_code = UBITS (bs->i_bit_in_cache, 5);    bs_flush( bs, 5 );    if( tr->q_scale_type )        return non_linear_quantizer_scale[quantizer_scale_code];    else        return quantizer_scale_code << 1;}static inline int get_motion_delta( bs_transrate_t *bs, const int f_code ){    int delta;    int sign;    const MVtab * tab;    if (bs->i_bit_in_cache & 0x80000000)    {        bs_copy( bs, 1 );        return 0;    }    else if (bs->i_bit_in_cache >= 0x0c000000)    {        tab = MV_4 + UBITS (bs->i_bit_in_cache, 4);        delta = (tab->delta << f_code) + 1;        bs_copy( bs, tab->len);        sign = SBITS (bs->i_bit_in_cache, 1);        bs_copy( bs, 1 );        if (f_code) delta += UBITS (bs->i_bit_in_cache, f_code);        bs_copy( bs, f_code);        return (delta ^ sign) - sign;    }    else    {        tab = MV_10 + UBITS (bs->i_bit_in_cache, 10);        delta = (tab->delta << f_code) + 1;        bs_copy( bs, tab->len);        sign = SBITS (bs->i_bit_in_cache, 1);        bs_copy( bs, 1);        if (f_code)        {            delta += UBITS (bs->i_bit_in_cache, f_code);            bs_copy( bs, f_code);        }        return (delta ^ sign) - sign;    }}static inline int get_dmv( bs_transrate_t *bs ){    const DMVtab * tab;    tab = DMV_2 + UBITS (bs->i_bit_in_cache, 2);    bs_copy( bs, tab->len);    return tab->dmv;}static inline int get_coded_block_pattern( bs_transrate_t *bs ){    const CBPtab * tab;    if (bs->i_bit_in_cache >= 0x20000000)    {        tab = CBP_7 + (UBITS (bs->i_bit_in_cache, 7) - 16);        bs_flush( bs, tab->len );        return tab->cbp;    }    else    {        tab = CBP_9 + UBITS (bs->i_bit_in_cache, 9);        bs_flush( bs, tab->len );        return tab->cbp;    }}static inline int get_luma_dc_dct_diff( bs_transrate_t *bs ){    const DCtab * tab;    int size;    int dc_diff;    if (bs->i_bit_in_cache < 0xf8000000)    {        tab = DC_lum_5 + UBITS (bs->i_bit_in_cache, 5);        size = tab->size;        if (size)        {            bs_copy( bs, tab->len);            //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);            dc_diff = UBITS (bs->i_bit_in_cache, size);            if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);            bs_copy( bs, size);            return dc_diff;        }        else        {            bs_copy( bs, 3);            return 0;        }    }    else    {        tab = DC_long + (UBITS (bs->i_bit_in_cache, 9) - 0x1e0);        size = tab->size;        bs_copy( bs, tab->len);        //dc_diff = UBITS (bs->i_bit_in_cache, size) - UBITS (SBITS (~bs->i_bit_in_cache, 1), size);        dc_diff = UBITS (bs->i_bit_in_cache, size);        if (!(dc_diff >> (size - 1))) dc_diff = (dc_diff + 1) - (1 << size);        bs_copy( bs, size);        return dc_diff;    }}static inline int get_chroma_dc_dct_diff( bs_transrate_t *bs ){    const DCtab * tab;    int size;    int dc_diff;    if (bs->i_bit_in_cache < 0xf8000000)    {        tab = DC_chrom_5 + UBITS (bs->i_bit_in_cache, 5);        size = tab->size;        if (size)

⌨️ 快捷键说明

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