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

📄 macroblock.c

📁 H.264编码器
💻 C
📖 第 1 页 / 共 5 页
字号:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "stdint.h"
#include "common.h"
#include "vlc.h"
#include "macroblock.h"

static const uint8_t intra4x4_cbp_to_golomb[48]=
{
  3, 29, 30, 17, 31, 18, 37,  8, 32, 38, 19,  9, 20, 10, 11,  2,
 16, 33, 34, 21, 35, 22, 39,  4, 36, 40, 23,  5, 24,  6,  7,  1,
 41, 42, 43, 25, 44, 26, 46, 12, 45, 47, 27, 13, 28, 14, 15,  0
};
static const uint8_t inter_cbp_to_golomb[48]=
{
  0,  2,  3,  7,  4,  8, 17, 13,  5, 18,  9, 14, 10, 15, 16, 11,
  1, 32, 33, 36, 34, 37, 44, 40, 35, 45, 38, 41, 39, 42, 43, 19,
  6, 24, 25, 20, 26, 21, 46, 28, 27, 47, 22, 29, 23, 30, 31, 12
};

static const uint8_t block_idx_x[16] =
{
    0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
};
static const uint8_t block_idx_y[16] =
{
    0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
};
static const uint8_t block_idx_xy[4][4] =
{
    { 0, 2, 8,  10},
    { 1, 3, 9,  11},
    { 4, 6, 12, 14},
    { 5, 7, 13, 15}
};

static const int quant_mf[6][4][4] =
{
    {  { 13107, 8066, 13107, 8066}, {  8066, 5243,  8066, 5243},
       { 13107, 8066, 13107, 8066}, {  8066, 5243,  8066, 5243}  },
    {  { 11916, 7490, 11916, 7490}, {  7490, 4660,  7490, 4660},
       { 11916, 7490, 11916, 7490}, {  7490, 4660,  7490, 4660}  },
    {  { 10082, 6554, 10082, 6554}, {  6554, 4194,  6554, 4194},
       { 10082, 6554, 10082, 6554}, {  6554, 4194,  6554, 4194}  },
    {  {  9362, 5825,  9362, 5825}, {  5825, 3647,  5825, 3647},
       {  9362, 5825,  9362, 5825}, {  5825, 3647,  5825, 3647}  },
    {  {  8192, 5243,  8192, 5243}, {  5243, 3355,  5243, 3355},
       {  8192, 5243,  8192, 5243}, {  5243, 3355,  5243, 3355}  },
    {  {  7282, 4559,  7282, 4559}, {  4559, 2893,  4559, 2893},
       {  7282, 4559,  7282, 4559}, {  4559, 2893,  4559, 2893}  }
};

static const int i_chroma_qp_table[52] =
{
     0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
    10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
    20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
    29, 30, 31, 32, 32, 33, 34, 34, 35, 35,
    36, 36, 37, 37, 37, 38, 38, 38, 39, 39,
    39, 39
};

static const int dequant_mf[6][4][4] =
{
    { {10, 13, 10, 13}, {13, 16, 13, 16}, {10, 13, 10, 13}, {13, 16, 13, 16} },
    { {11, 14, 11, 14}, {14, 18, 14, 18}, {11, 14, 11, 14}, {14, 18, 14, 18} },
    { {13, 16, 13, 16}, {16, 20, 16, 20}, {13, 16, 13, 16}, {16, 20, 16, 20} },
    { {14, 18, 14, 18}, {18, 23, 18, 23}, {14, 18, 14, 18}, {18, 23, 18, 23} },
    { {16, 20, 16, 20}, {20, 25, 20, 25}, {16, 20, 16, 20}, {20, 25, 20, 25} },
    { {18, 23, 18, 23}, {23, 29, 23, 29}, {18, 23, 18, 23}, {23, 29, 23, 29} }
};

#if 0
static const int i_chroma_qp_table[52] =
{
     0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
    10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
    20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
    29, 30, 31, 32, 32, 33, 34, 34, 35, 35,
    36, 36, 37, 37, 37, 38, 38, 38, 39, 39,
    39, 39
};
#endif

/****************************************************************************
 * Scan and Quant functions
 ****************************************************************************/
static const int scan_zigzag_x[16]={0, 1, 0, 0, 1, 2, 3, 2, 1, 0, 1, 2, 3, 3, 2, 3};
static const int scan_zigzag_y[16]={0, 0, 1, 2, 1, 0, 0, 1, 2, 3, 3, 2, 1, 2, 3, 3};

static/* inline */ void scan_zigzag_4x4full( int level[16], int16_t dct[4][4] )
{
    int i;

    for( i = 0; i < 16; i++ )
    {
        level[i] = dct[scan_zigzag_y[i]][scan_zigzag_x[i]];
    }
}
static/* inline */ void scan_zigzag_4x4( int level[15], int16_t dct[4][4] )
{
    int i;

    for( i = 1; i < 16; i++ )
    {
        level[i - 1] = dct[scan_zigzag_y[i]][scan_zigzag_x[i]];
    }
}

static/* inline */ void scan_zigzag_2x2_dc( int level[4], int16_t dct[2][2] )
{
    level[0] = dct[0][0];
    level[1] = dct[0][1];
    level[2] = dct[1][0];
    level[3] = dct[1][1];
}


static void quant_4x4( int16_t dct[4][4], int i_qscale, int b_intra )
{
    const int i_qbits = 15 + i_qscale / 6;
    const int i_mf = i_qscale % 6;
    const int f = ( 1 << i_qbits ) / ( b_intra ? 3 : 6 );
	int x,y;

    
    for( y = 0; y < 4; y++ )
	{
        for( x = 0; x < 4; x++ )
        {
            if( dct[y][x] > 0 )
            {
                dct[y][x] =( f + dct[y][x]  * quant_mf[i_mf][y][x] ) >> i_qbits;
            }
            else
			{
                dct[y][x] = - ( ( f - dct[y][x]  * quant_mf[i_mf][y][x] ) >> i_qbits );
			}
        }
	}
}
static void quant_4x4_dc( int16_t dct[4][4], int i_qscale )
{
    const int i_qbits = 15 + i_qscale / 6;
    const int f2 = ( 2 << i_qbits ) / 3;
    const int i_qmf = quant_mf[i_qscale%6][0][0];
    int x,y;

    for( y = 0; y < 4; y++ )
    {
        for( x = 0; x < 4; x++ )
        {
            if( dct[y][x] > 0 )
            {
                dct[y][x] =( f2 + dct[y][x]  * i_qmf) >> ( 1 + i_qbits );
            }
            else
            {
                dct[y][x] = - ( ( f2 - dct[y][x]  * i_qmf ) >> (1 + i_qbits ) );
            }
        }
    }
}
static void quant_2x2_dc( int16_t dct[2][2], int i_qscale, int b_intra )
{
    int const i_qbits = 15 + i_qscale / 6;
    const int f2 = ( 2 << i_qbits ) / ( b_intra ? 3 : 6 );
    const int i_qmf = quant_mf[i_qscale%6][0][0];

    int x,y;
    for( y = 0; y < 2; y++ )
    {
        for( x = 0; x < 2; x++ )
        {
            if( dct[y][x] > 0 )
            {
                dct[y][x] =( f2 + dct[y][x]  * i_qmf) >> ( 1 + i_qbits );
            }
            else
            {
                dct[y][x] = - ( ( f2 - dct[y][x]  * i_qmf ) >> (1 + i_qbits ) );
            }
        }
    }
}

static/* inline */ int array_non_zero_count( int *v, int i_count )
{
    int i;
    int i_nz;
//	if(i_count!=4&&i_count!=15&&i_count!=16)
//	printf("i_count: %d   ",  i_count);

    for( i = 0, i_nz = 0; i < i_count; i++ )
    {
        if( v[i] )
        {
            i_nz++;
        }
    }
    return i_nz;
}

void x264_mb_partition_mvd( x264_macroblock_t *mb, int i_list, int i_part, int i_sub, int mvd[2])
{
    int mvp[2];

    int x,  y;
    int w,  h;
    int dx, dy;

    x264_mb_partition_getxy( mb, i_part, i_sub, &x, &y );
    x264_mb_partition_size ( mb, i_part, i_sub, &w, &h );
    x264_mb_predict_mv(  mb, i_list, i_part, i_sub, mvp );

    mvd[0] = mb->partition[x][y].mv[i_list][0] - mvp[0];
    mvd[1] = mb->partition[x][y].mv[i_list][1] - mvp[1];

    for( dx = 0; dx < w; dx++ )
    {
        for( dy = 0; dy < h; dy++ )
        {
            mb->partition[x+dx][y+dy].mvd[i_list][0] = mvd[0];
            mb->partition[x+dx][y+dy].mvd[i_list][1] = mvd[1];
        }
    }
}

/* (ref: JVT-B118)
 * x264_mb_decimate_score: given dct coeffs it returns a score to see if we could empty this dct coeffs
 * to 0 (low score means set it to null)
 * Used in inter macroblock (luma and chroma)
 *  luma: for a 8x8 block: if score < 4 -> null
 *        for the complete mb: if score < 6 -> null
 *  chroma: for the complete mb: if score < 7 -> null
 */
static int x264_mb_decimate_score( int *dct, int i_max )
{
    static const int i_ds_table[16] = { 3, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

    int i_score = 0;
    int idx = i_max - 1;

    while( idx >= 0 && dct[idx] == 0 )
    {
        idx--;
    }

    while( idx >= 0 )
    {
        int i_run;

        if( abs( dct[idx--] ) > 1 )
        {
            return 9;
        }

        i_run = 0;
        while( idx >= 0 && dct[idx] == 0 )
        {
            idx--;
            i_run++;
        }
        i_score += i_ds_table[i_run];
    }

    return i_score;
}

void x264_mb_encode_i4x4( x264_t *h, x264_macroblock_t *mb, int idx, int i_qscale )
{
    x264_mb_context_t *ctx = mb->context;

    uint8_t *p_src = ctx->p_img[0] + 4 * block_idx_x[idx] + 4 * block_idx_y[idx] * ctx->i_img[0];
    int      i_src = ctx->i_img[0];
    uint8_t *p_dst = ctx->p_fdec[0] + 4 * block_idx_x[idx] + 4 * block_idx_y[idx] * ctx->i_fdec[0];
    int      i_dst = ctx->i_fdec[0];

    int16_t luma[4][4];
    int16_t dct4x4[4][4];

    /* we calculate diff */
    h->pixf.sub4x4( luma, p_src, i_src, p_dst, i_dst );

    /* calculate dct coeffs */
    h->dctf.dct4x4( dct4x4, luma );
    quant_4x4( dct4x4, i_qscale, 1 );

    scan_zigzag_4x4full( mb->block[idx].luma4x4, dct4x4 );

    /* output samples to fdec */
    x264_mb_dequant_4x4( dct4x4, i_qscale );
    h->dctf.idct4x4( luma, dct4x4 );

    /* put pixel to fdec */
    h->pixf.add4x4( p_dst, i_dst, luma );
}

static void x264_mb_encode_i16x16( x264_t *h, x264_macroblock_t *mb, int i_qscale )
{
    x264_mb_context_t *ctx = mb->context;

    uint8_t *p_src = ctx->p_img[0];
    int      i_src = ctx->i_img[0];
    uint8_t *p_dst = ctx->p_fdec[0];
    int      i_dst = ctx->i_fdec[0];

    int16_t luma[16][4][4];
    int16_t dct4x4[16+1][4][4];

    int i;

    /* calculate the diff */
    h->pixf.sub16x16( luma, p_src, i_src, p_dst, i_dst );

    /* calculate dct coeffs */
    for( i = 0; i < 16; i++ )
    {
        h->dctf.dct4x4( dct4x4[i+1], luma[i] );

        /* copy dc coeff */
        dct4x4[0][block_idx_y[i]][block_idx_x[i]] = dct4x4[1+i][0][0];

        quant_4x4( dct4x4[1+i], i_qscale, 1 );
        scan_zigzag_4x4( mb->block[i].residual_ac, dct4x4[1+i] );
    }

    h->dctf.dct4x4dc( dct4x4[0], dct4x4[0] );
    quant_4x4_dc( dct4x4[0], i_qscale );
    scan_zigzag_4x4full( mb->luma16x16_dc, dct4x4[0] );

    /* output samples to fdec */
    h->dctf.idct4x4dc( dct4x4[0], dct4x4[0] );
    x264_mb_dequant_4x4_dc( dct4x4[0], i_qscale );  /* XXX not inversed */

    /* calculate dct coeffs */
    for( i = 0; i < 16; i++ )
    {
        x264_mb_dequant_4x4( dct4x4[1+i], i_qscale );

        /* copy dc coeff */
        dct4x4[1+i][0][0] = dct4x4[0][block_idx_y[i]][block_idx_x[i]];

        h->dctf.idct4x4( luma[i], dct4x4[i+1] );
    }
    /* put pixels to fdec */
    h->pixf.add16x16( p_dst, i_dst, luma );
}

static void x264_mb_encode_8x8( x264_t *h, x264_macroblock_t *mb, int b_inter, int i_qscale )
{
    x264_mb_context_t *ctx = mb->context;

    uint8_t *p_src, *p_dst;
    int      i_src, i_dst;

    int i, ch;
    int i_decimate_score = 0;

    for( ch = 0; ch < 2; ch++ )
    {
        int16_t chroma[4][4][4];
        int16_t dct2x2[2][2];
        int16_t dct4x4[4][4][4];

        p_src = ctx->p_img[1+ch];
        i_src = ctx->i_img[1+ch];
        p_dst = ctx->p_fdec[1+ch];
        i_dst = ctx->i_fdec[1+ch];

        /* calculate the diff */
        h->pixf.sub8x8( chroma, p_src, i_src, p_dst, i_dst );

        /* calculate dct coeffs */
        for( i = 0; i < 4; i++ )
        {
            h->dctf.dct4x4( dct4x4[i], chroma[i] );

            /* copy dc coeff */
            dct2x2[block_idx_y[i]][block_idx_x[i]] = dct4x4[i][0][0];

            quant_4x4( dct4x4[i], i_qscale, b_inter ? 0 : 1 );
            scan_zigzag_4x4( mb->block[16+i+ch*4].residual_ac, dct4x4[i] );

            i_decimate_score += x264_mb_decimate_score( mb->block[16+i+ch*4].residual_ac, 15 );
        }

        h->dctf.dct2x2dc( dct2x2, dct2x2 );
        quant_2x2_dc( dct2x2, i_qscale, b_inter ? 0 : 1 );
        scan_zigzag_2x2_dc( mb->chroma_dc[ch], dct2x2 );

        if( i_decimate_score < 7 && b_inter )
        {
            /* Near null chroma 8x8 block so make it null (bits saving) */
            for( i = 0; i < 4; i++ )
            {
                int x, y;
                for( x = 0; x < 15; x++ )
                {
                    mb->block[16+i+ch*4].residual_ac[x] = 0;
                }
                for( x = 0; x < 4; x++ )
                {
                    for( y = 0; y < 4; y++ )
                    {
                        dct4x4[i][x][y] = 0;
                    }
                }
            }
        }

        /* output samples to fdec */
        h->dctf.idct2x2dc( dct2x2, dct2x2 );
        x264_mb_dequant_2x2_dc( dct2x2, i_qscale );  /* XXX not inversed */

        /* calculate dct coeffs */
        for( i = 0; i < 4; i++ )
        {
            x264_mb_dequant_4x4( dct4x4[i], i_qscale );

            /* copy dc coeff */
            dct4x4[i][0][0] = dct2x2[block_idx_y[i]][block_idx_x[i]];

            h->dctf.idct4x4( chroma[i], dct4x4[i] );
        }
        h->pixf.add8x8( p_dst, i_dst, chroma );
    }
}

/*****************************************************************************
 * x264_macroblock_encode:
 *****************************************************************************/
void x264_macroblock_encode( x264_t *h, x264_macroblock_t *mb )
{
    x264_mb_context_t *ctx = mb->context;
    int i;

⌨️ 快捷键说明

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