📄 macroblock.c
字号:
/*****************************************************************************
* macroblock.c: h264 encoder library
*****************************************************************************
* Copyright (C) 2003 Laurent Aimar
* $Id: macroblock.c,v 1.1 2004/06/03 19:27:06 fenrir Exp $
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*****************************************************************************/
#include <stdio.h>
#include <string.h>
#include "common.h"
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} }
};
int x264_mb_predict_intra4x4_mode( x264_t *h, int idx )
{
const int ma = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 1];
const int mb = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 8];
const int m = X264_MIN( x264_mb_pred_mode4x4_fix(ma),
x264_mb_pred_mode4x4_fix(mb) );
if( m < 0 )
return I_PRED_4x4_DC;
return m;
}
int x264_mb_predict_non_zero_code( x264_t *h, int idx )
{
const int za = h->mb.cache.non_zero_count[x264_scan8[idx] - 1];
const int zb = h->mb.cache.non_zero_count[x264_scan8[idx] - 8];
int i_ret = za + zb;
if( i_ret < 0x80 )
{
i_ret = ( i_ret + 1 ) >> 1;
}
return i_ret & 0x7f;
}
int x264_mb_transform_8x8_allowed( x264_t *h )
{
if( IS_SKIP( h->mb.i_type ) )
return 0;
if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
{
int i;
for( i = 0; i < 4; i++ )
if( !IS_SUB8x8(h->mb.i_sub_partition[i])
|| ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 && !h->sps->b_direct8x8_inference ) )
{
return 0;
}
}
if( h->mb.i_type == B_DIRECT && !h->sps->b_direct8x8_inference )
return 0;
return 1;
}
void x264_mb_predict_mv( x264_t *h, int i_list, int idx, int i_width, int mvp[2] )
{
const int i8 = x264_scan8[idx];
const int i_ref= h->mb.cache.ref[i_list][i8];
int i_refa = h->mb.cache.ref[i_list][i8 - 1];
int16_t *mv_a = h->mb.cache.mv[i_list][i8 - 1];
int i_refb = h->mb.cache.ref[i_list][i8 - 8];
int16_t *mv_b = h->mb.cache.mv[i_list][i8 - 8];
int i_refc = h->mb.cache.ref[i_list][i8 - 8 + i_width ];
int16_t *mv_c = h->mb.cache.mv[i_list][i8 - 8 + i_width];
int i_count;
if( (idx&0x03) == 3 || ( i_width == 2 && (idx&0x3) == 2 )|| i_refc == -2 )
{
i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1];
mv_c = h->mb.cache.mv[i_list][i8 - 8 - 1];
}
if( h->mb.i_partition == D_16x8 )
{
if( idx == 0 && i_refb == i_ref )
{
mvp[0] = mv_b[0];
mvp[1] = mv_b[1];
return;
}
else if( idx != 0 && i_refa == i_ref )
{
mvp[0] = mv_a[0];
mvp[1] = mv_a[1];
return;
}
}
else if( h->mb.i_partition == D_8x16 )
{
if( idx == 0 && i_refa == i_ref )
{
mvp[0] = mv_a[0];
mvp[1] = mv_a[1];
return;
}
else if( idx != 0 && i_refc == i_ref )
{
mvp[0] = mv_c[0];
mvp[1] = mv_c[1];
return;
}
}
i_count = 0;
if( i_refa == i_ref ) i_count++;
if( i_refb == i_ref ) i_count++;
if( i_refc == i_ref ) i_count++;
if( i_count > 1 )
{
mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
}
else if( i_count == 1 )
{
if( i_refa == i_ref )
{
mvp[0] = mv_a[0];
mvp[1] = mv_a[1];
}
else if( i_refb == i_ref )
{
mvp[0] = mv_b[0];
mvp[1] = mv_b[1];
}
else
{
mvp[0] = mv_c[0];
mvp[1] = mv_c[1];
}
}
else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
{
mvp[0] = mv_a[0];
mvp[1] = mv_a[1];
}
else
{
mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
}
}
void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int mvp[2] )
{
int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
int16_t *mv_a = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
int16_t *mv_b = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
int16_t *mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
int i_count;
if( i_refc == -2 )
{
i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
}
i_count = 0;
if( i_refa == i_ref ) i_count++;
if( i_refb == i_ref ) i_count++;
if( i_refc == i_ref ) i_count++;
if( i_count > 1 )
{
mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
}
else if( i_count == 1 )
{
if( i_refa == i_ref )
{
mvp[0] = mv_a[0];
mvp[1] = mv_a[1];
}
else if( i_refb == i_ref )
{
mvp[0] = mv_b[0];
mvp[1] = mv_b[1];
}
else
{
mvp[0] = mv_c[0];
mvp[1] = mv_c[1];
}
}
else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
{
mvp[0] = mv_a[0];
mvp[1] = mv_a[1];
}
else
{
mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
}
}
void x264_mb_predict_mv_pskip( x264_t *h, int mv[2] )
{
int i_refa = h->mb.cache.ref[0][X264_SCAN8_0 - 1];
int i_refb = h->mb.cache.ref[0][X264_SCAN8_0 - 8];
int16_t *mv_a = h->mb.cache.mv[0][X264_SCAN8_0 - 1];
int16_t *mv_b = h->mb.cache.mv[0][X264_SCAN8_0 - 8];
if( i_refa == -2 || i_refb == -2 ||
( i_refa == 0 && mv_a[0] == 0 && mv_a[1] == 0 ) ||
( i_refb == 0 && mv_b[0] == 0 && mv_b[1] == 0 ) )
{
mv[0] = mv[1] = 0;
}
else
{
x264_mb_predict_mv_16x16( h, 0, 0, mv );
}
}
static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
{
int i_mb_4x4 = 16 * h->mb.i_mb_stride * h->mb.i_mb_y + 4 * h->mb.i_mb_x;
int i_mb_8x8 = 4 * h->mb.i_mb_stride * h->mb.i_mb_y + 2 * h->mb.i_mb_x;
int i8, i4;
int b8x8;
const int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
if( IS_INTRA( type_col ) )
{
x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, 0, 0 );
x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, 0, 0 );
return 1;
}
b8x8 = h->sps->b_direct8x8_inference ||
(type_col != P_8x8 && type_col != B_SKIP && type_col != B_DIRECT && type_col != B_8x8);
for( i8 = 0; i8 < 4; i8++ )
{
const int x8 = i8%2;
const int y8 = i8/2;
const int i_part_8x8 = i_mb_8x8 + x8 + y8 * h->mb.i_b8_stride;
const int i_ref = h->mb.map_col_to_list0[ h->fref1[0]->ref[0][ i_part_8x8 ] ];
if( i_ref >= 0 )
{
const int dist_scale_factor = h->mb.dist_scale_factor[i_ref][0];
x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
if( b8x8 )
{
const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + 3*x8 + 3*y8 * h->mb.i_b4_stride];
int mv_l0[2];
mv_l0[0] = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
mv_l0[1] = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, mv_l0[0], mv_l0[1] );
x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1] );
}
else
{
for( i4 = 0; i4 < 4; i4++ )
{
const int x4 = i4%2 + 2*x8;
const int y4 = i4/2 + 2*y8;
const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + x4 + y4 * h->mb.i_b4_stride ];
int mv_l0[2];
mv_l0[0] = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
mv_l0[1] = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, mv_l0[0], mv_l0[1] );
x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1] );
}
}
}
else
{
/* the colocated ref isn't in the current list0 */
/* FIXME: we might still be able to use direct_8x8 on some partitions */
/* FIXME: with B-pyramid + extensive ref list reordering
* (not currently used), we would also have to check
* l1mv1 like in spatial mode */
return 0;
}
}
return 1;
}
static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
{
int ref[2];
int mv[2][2];
int i_list;
int i8, i4;
int b8x8;
const int8_t *l1ref0 = &h->fref1[0]->ref[0][ h->mb.i_b8_xy ];
const int8_t *l1ref1 = &h->fref1[0]->ref[1][ h->mb.i_b8_xy ];
const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[0][ h->mb.i_b4_xy ];
const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[1][ h->mb.i_b4_xy ];
const int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
for( i_list=0; i_list<2; i_list++ )
{
int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
if( i_refc == -2 )
i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
ref[i_list] = i_refa;
if( ref[i_list] < 0 || ( i_refb < ref[i_list] && i_refb >= 0 ))
ref[i_list] = i_refb;
if( ref[i_list] < 0 || ( i_refc < ref[i_list] && i_refc >= 0 ))
ref[i_list] = i_refc;
if( ref[i_list] < 0 )
ref[i_list] = -1;
}
if( ref[0] < 0 && ref[1] < 0 )
{
ref[0] =
ref[1] = 0;
mv[0][0] =
mv[0][1] =
mv[1][0] =
mv[1][1] = 0;
}
else
{
for( i_list=0; i_list<2; i_list++ )
{
if( ref[i_list] >= 0 )
x264_mb_predict_mv_16x16( h, i_list, ref[i_list], mv[i_list] );
else
mv[i_list][0] = mv[i_list][1] = 0;
}
}
x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, ref[0] );
x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, ref[1] );
x264_macroblock_cache_mv( h, 0, 0, 4, 4, 0, mv[0][0], mv[0][1] );
x264_macroblock_cache_mv( h, 0, 0, 4, 4, 1, mv[1][0], mv[1][1] );
if( IS_INTRA( type_col ) )
return 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -