From faec300a71cdb64e1bd27d393de51d2e3d1f5992 Mon Sep 17 00:00:00 2001 From: Loren Merritt Date: Sun, 1 Oct 2006 02:41:22 +0000 Subject: [PATCH] support interlace. uses MBAFF syntax, but is not adaptive yet. git-svn-id: svn://svn.videolan.org/x264/trunk@570 df754926-b1dd-0310-bc7b-ec298dee348c --- common/cabac.c | 2 +- common/cabac.h | 3 +- common/common.c | 2 + common/common.h | 20 +++-- common/dct.c | 14 +++ common/dct.h | 18 +--- common/frame.c | 156 ++++++++++++++++++++++----------- common/frame.h | 9 +- common/macroblock.c | 200 ++++++++++++++++++++++++++----------------- common/macroblock.h | 21 +++-- common/mc.c | 11 +-- encoder/analyse.c | 26 +++--- encoder/cabac.c | 101 +++++++++++++--------- encoder/cavlc.c | 54 ++++++------ encoder/encoder.c | 79 ++++++++++++++--- encoder/macroblock.c | 164 +++++++++++++++++++++++++++-------- encoder/rdo.c | 17 ++-- encoder/set.c | 25 ++++-- x264.c | 2 + x264.h | 2 + 20 files changed, 620 insertions(+), 306 deletions(-) diff --git a/common/cabac.c b/common/cabac.c index f6957fdd..ad796032 100644 --- a/common/cabac.c +++ b/common/cabac.c @@ -829,7 +829,7 @@ void x264_cabac_context_init( x264_cabac_t *cb, int i_slice_type, int i_qp, int cabac_context_init = &x264_cabac_context_init_PB[i_model]; } - for( i = 0; i < 436; i++ ) + for( i = 0; i < 460; i++ ) { cb->state[i] = x264_clip3( (((*cabac_context_init)[i][0] * i_qp) >> 4) + (*cabac_context_init)[i][1], 1, 126 ); } diff --git a/common/cabac.h b/common/cabac.h index 2934d8f9..a8dadf4e 100644 --- a/common/cabac.h +++ b/common/cabac.h @@ -27,8 +27,7 @@ typedef struct { /* context */ - /* #436-459 are for interlacing, so are omitted for now */ - uint8_t state[436]; + uint8_t state[460]; /* state */ int i_low; diff --git a/common/common.c b/common/common.c index ca1b6120..1b9768e2 100644 --- a/common/common.c +++ b/common/common.c @@ -292,6 +292,8 @@ int x264_param_parse( x264_param_t *p, const char *name, const char *value ) p->b_cabac = atobool(value); OPT("cabac-idc") p->i_cabac_init_idc = atoi(value); + OPT("interlaced") + p->b_interlaced = atobool(value); OPT("cqm") { if( strstr( value, "flat" ) ) diff --git a/common/common.h b/common/common.h index 8ee5b975..f0fb0406 100644 --- a/common/common.h +++ b/common/common.h @@ -31,6 +31,7 @@ #endif #include #include +#include #ifdef _MSC_VER #define snprintf _snprintf @@ -193,6 +194,7 @@ typedef struct int i_frame_num; + int b_mbaff; int b_field_pic; int b_bottom_field; @@ -397,6 +399,8 @@ struct x264_t int b_trellis; int b_noise_reduction; + int b_interlaced; + /* Allowed qpel MV range to stay within the picture + emulated edge pixels */ int mv_min[2]; int mv_max[2]; @@ -416,6 +420,8 @@ struct x264_t int i_mb_type_left; int i_mb_type_topleft; int i_mb_type_topright; + int i_mb_prev_xy; + int i_mb_top_xy; /* mb table */ int8_t *type; /* mb type */ @@ -427,7 +433,7 @@ struct x264_t int16_t (*mv[2])[2]; /* mb mv. set to 0 for intra mb */ int16_t (*mvd[2])[2]; /* mb mv difference with predict. set to 0 if intra. cabac only */ int8_t *ref[2]; /* mb ref. set to -1 if non used (intra or Lx only) */ - int16_t (*mvr[2][16])[2]; /* 16x16 mv for each possible ref */ + int16_t (*mvr[2][32])[2]; /* 16x16 mv for each possible ref */ int8_t *skipbp; /* block pattern for SKIP or DIRECT (sub)mbs. B-frames + cabac only */ int8_t *mb_transform_size; /* transform_size_8x8_flag of each mb */ @@ -458,7 +464,8 @@ struct x264_t uint8_t *p_fdec[3]; /* pointer over mb of the references */ - uint8_t *p_fref[2][16][4+2]; /* last: lN, lH, lV, lHV, cU, cV */ + int i_fref[2]; + uint8_t *p_fref[2][32][4+2]; /* last: lN, lH, lV, lHV, cU, cV */ uint16_t *p_integral[2][16]; /* fref stride */ @@ -490,6 +497,7 @@ struct x264_t /* number of neighbors (top and left) that used 8x8 dct */ int i_neighbour_transform_size; int b_transform_8x8_allowed; + int i_neighbour_interlaced; } cache; /* */ @@ -502,8 +510,8 @@ struct x264_t int b_direct_auto_write; /* analyse direct modes, to use and/or save */ /* B_direct and weighted prediction */ - int dist_scale_factor[16][16]; - int bipred_weight[16][16]; + int dist_scale_factor[16][2]; + int bipred_weight[32][4]; /* maps fref1[0]'s ref indices into the current list0 */ int map_col_to_list0_buf[2]; // for negative indices int map_col_to_list0[16]; @@ -532,7 +540,7 @@ struct x264_t int i_mb_count_skip; int i_mb_count_8x8dct[2]; int i_mb_count_size[7]; - int i_mb_count_ref[16]; + int i_mb_count_ref[32]; /* Estimated (SATD) cost as Intra/Predicted frame */ /* XXX: both omit the cost of MBs coded as P_SKIP */ int i_intra_cost; @@ -558,7 +566,7 @@ struct x264_t int64_t i_mb_count[5][19]; int64_t i_mb_count_8x8dct[2]; int64_t i_mb_count_size[2][7]; - int64_t i_mb_count_ref[2][16]; + int64_t i_mb_count_ref[2][32]; /* */ int i_direct_score[2]; int i_direct_frames[2]; diff --git a/common/dct.c b/common/dct.c index 99f95fe0..a9f855ea 100644 --- a/common/dct.c +++ b/common/dct.c @@ -29,6 +29,8 @@ # include "ppc/dct.h" #endif +int x264_dct4_weight2_zigzag[2][16]; +int x264_dct8_weight2_zigzag[2][64]; static inline int clip_uint8( int a ) { @@ -441,3 +443,15 @@ void x264_dct_init( int cpu, x264_dct_function_t *dctf ) #endif } +void x264_dct_init_weights( void ) +{ + int i, j; + for( j=0; j<2; j++ ) + { + for( i=0; i<16; i++ ) + x264_dct4_weight2_zigzag[j][i] = x264_dct4_weight2_tab[ x264_zigzag_scan4[j][i] ]; + for( i=0; i<64; i++ ) + x264_dct8_weight2_zigzag[j][i] = x264_dct8_weight2_tab[ x264_zigzag_scan8[j][i] ]; + } +} + diff --git a/common/dct.h b/common/dct.h index e8673832..ff643042 100644 --- a/common/dct.h +++ b/common/dct.h @@ -54,10 +54,6 @@ static const int x264_dct4_weight2_tab[16] = { W(0), W(1), W(0), W(1), W(1), W(2), W(1), W(2) }; -static const int x264_dct4_weight2_zigzag[16] = { - W(0), W(1), W(1), W(0), W(2), W(0), W(1), W(1), - W(1), W(1), W(2), W(0), W(2), W(1), W(1), W(2) -}; #undef W #define W(i) (i==0 ? FIX8(1.00000) :\ @@ -77,18 +73,11 @@ static const int x264_dct8_weight2_tab[64] = { W(4), W(5), W(2), W(5), W(4), W(5), W(2), W(5), W(3), W(1), W(5), W(1), W(3), W(1), W(5), W(1) }; -static const int x264_dct8_weight2_zigzag[64] = { - W(0), W(3), W(3), W(4), W(1), W(4), W(3), W(5), - W(5), W(3), W(0), W(1), W(2), W(1), W(0), W(3), - W(3), W(5), W(5), W(3), W(3), W(4), W(1), W(4), - W(1), W(4), W(1), W(4), W(3), W(5), W(5), W(3), - W(3), W(5), W(5), W(3), W(1), W(2), W(1), W(0), - W(1), W(2), W(1), W(5), W(5), W(3), W(3), W(5), - W(5), W(1), W(4), W(1), W(4), W(1), W(3), W(5), - W(5), W(3), W(1), W(2), W(1), W(5), W(5), W(1) -}; #undef W +extern int x264_dct4_weight2_zigzag[2][16]; // [2] = {frame, field} +extern int x264_dct8_weight2_zigzag[2][64]; + typedef struct { // pix1 stride = FENC_STRIDE @@ -118,5 +107,6 @@ typedef struct } x264_dct_function_t; void x264_dct_init( int cpu, x264_dct_function_t *dctf ); +void x264_dct_init_weights( void ); #endif diff --git a/common/frame.c b/common/frame.c index 8afbc9a4..362c6515 100644 --- a/common/frame.c +++ b/common/frame.c @@ -26,6 +26,9 @@ #include "common.h" +#define PADH 32 +#define PADV 32 + x264_frame_t *x264_frame_new( x264_t *h ) { x264_frame_t *frame = x264_malloc( sizeof(x264_frame_t) ); @@ -34,14 +37,17 @@ x264_frame_t *x264_frame_new( x264_t *h ) int i_mb_count = h->mb.i_mb_count; int i_stride; int i_lines; + int i_padv = PADV << h->param.b_interlaced; if( !frame ) return NULL; memset( frame, 0, sizeof(x264_frame_t) ); /* allocate frame data (+64 for extra data for me) */ - i_stride = ( ( h->param.i_width + 15 )&0xfffff0 )+ 64; - i_lines = ( ( h->param.i_height + 15 )&0xfffff0 ); + i_stride = ( ( h->param.i_width + 15 ) & -16 )+ 2*PADH; + i_lines = ( ( h->param.i_height + 15 ) & -16 ); + if( h->param.b_interlaced ) + i_lines = ( i_lines + 31 ) & -32; frame->i_plane = 3; for( i = 0; i < 3; i++ ) @@ -58,9 +64,10 @@ x264_frame_t *x264_frame_new( x264_t *h ) frame->i_stride[i] = i_stride / i_divw; frame->i_lines[i] = i_lines / i_divh; CHECKED_MALLOC( frame->buffer[i], - frame->i_stride[i] * ( frame->i_lines[i] + 64 / i_divh ) ); + frame->i_stride[i] * ( frame->i_lines[i] + 2*i_padv / i_divh ) ); + frame->plane[i] = ((uint8_t*)frame->buffer[i]) + - frame->i_stride[i] * 32 / i_divh + 32 / i_divw; + frame->i_stride[i] * i_padv / i_divh + PADH / i_divw; } frame->i_stride[3] = 0; frame->i_lines[3] = 0; @@ -71,29 +78,29 @@ x264_frame_t *x264_frame_new( x264_t *h ) for( i = 0; i < 3; i++ ) { CHECKED_MALLOC( frame->buffer[4+i], - frame->i_stride[0] * ( frame->i_lines[0] + 64 ) ); + frame->i_stride[0] * ( frame->i_lines[0] + 2*i_padv ) ); frame->filtered[i+1] = ((uint8_t*)frame->buffer[4+i]) + - frame->i_stride[0] * 32 + 32; + frame->i_stride[0] * i_padv + PADH; } if( h->frames.b_have_lowres ) { - frame->i_stride_lowres = frame->i_stride[0]/2 + 32; + frame->i_stride_lowres = frame->i_stride[0]/2 + PADH; frame->i_lines_lowres = frame->i_lines[0]/2; for( i = 0; i < 4; i++ ) { - CHECKED_MALLOC( frame->buffer[7+i], - frame->i_stride_lowres * ( frame->i_lines[0]/2 + 64 ) ); - frame->lowres[i] = ((uint8_t*)frame->buffer[7+i]) + - frame->i_stride_lowres * 32 + 32; + CHECKED_MALLOC( frame->buffer_lowres[i], + frame->i_stride_lowres * ( frame->i_lines[0]/2 + 2*i_padv ) ); + frame->lowres[i] = ((uint8_t*)frame->buffer_lowres[i]) + + frame->i_stride_lowres * i_padv + PADH; } } if( h->param.analyse.i_me_method == X264_ME_ESA ) { - CHECKED_MALLOC( frame->buffer[11], - 2 * frame->i_stride[0] * (frame->i_lines[0] + 64) * sizeof(uint16_t) ); - frame->integral = (uint16_t*)frame->buffer[11] + frame->i_stride[0] * 32 + 32; + CHECKED_MALLOC( frame->buffer[7], + 2 * frame->i_stride[0] * (frame->i_lines[0] + 2*i_padv) * sizeof(uint16_t) ); + frame->integral = (uint16_t*)frame->buffer[7] + frame->i_stride[0] * i_padv + PADH; } frame->i_poc = -1; @@ -133,8 +140,10 @@ fail: void x264_frame_delete( x264_frame_t *frame ) { int i, j; - for( i = 0; i < 12; i++ ) + for( i = 0; i < 8; i++ ) x264_free( frame->buffer[i] ); + for( i = 0; i < 4; i++ ) + x264_free( frame->buffer_lowres[i] ); for( i = 0; i < X264_BFRAME_MAX+2; i++ ) for( j = 0; j < X264_BFRAME_MAX+2; j++ ) x264_free( frame->i_row_satds[i][j] ); @@ -189,52 +198,77 @@ void x264_frame_copy_picture( x264_t *h, x264_frame_t *dst, x264_picture_t *src -static void plane_expand_border( uint8_t *pix, int i_stride, int i_height, int i_pad ) +static void plane_expand_border( uint8_t *pix, int i_stride, int i_width, int i_height, int i_padh, int i_padv ) { #define PPIXEL(x, y) ( pix + (x) + (y)*i_stride ) - const int i_width = i_stride - 2*i_pad; int y; - for( y = 0; y < i_height; y++ ) { /* left band */ - memset( PPIXEL(-i_pad, y), PPIXEL(0, y)[0], i_pad ); + memset( PPIXEL(-i_padh, y), PPIXEL(0, y)[0], i_padh ); /* right band */ - memset( PPIXEL(i_width, y), PPIXEL(i_width-1, y)[0], i_pad ); + memset( PPIXEL(i_width, y), PPIXEL(i_width-1, y)[0], i_padh ); } /* upper band */ - for( y = 0; y < i_pad; y++ ) - memcpy( PPIXEL(-i_pad, -y-1), PPIXEL(-i_pad, 0), i_stride ); + for( y = 0; y < i_padv; y++ ) + memcpy( PPIXEL(-i_padh, -y-1), PPIXEL(-i_padh, 0), i_width+2*i_padh ); /* lower band */ - for( y = 0; y < i_pad; y++ ) - memcpy( PPIXEL(-i_pad, i_height+y), PPIXEL(-i_pad, i_height-1), i_stride ); + for( y = 0; y < i_padv; y++ ) + memcpy( PPIXEL(-i_padh, i_height+y), PPIXEL(-i_padh, i_height-1), i_width+2*i_padh ); #undef PPIXEL } -void x264_frame_expand_border( x264_frame_t *frame ) +void x264_frame_expand_border( x264_t *h, x264_frame_t *frame ) { int i; for( i = 0; i < frame->i_plane; i++ ) { - int i_pad = i ? 16 : 32; - plane_expand_border( frame->plane[i], frame->i_stride[i], frame->i_lines[i], i_pad ); + int stride = frame->i_stride[i]; + int width = 16*h->sps->i_mb_width >> !!i; + int height = 16*h->sps->i_mb_height >> !!i; + int padh = PADH >> !!i; + int padv = PADV >> !!i; + if( h->param.b_interlaced ) + { + plane_expand_border( frame->plane[i], stride*2, width, height>>1, padh, padv ); + plane_expand_border( frame->plane[i]+stride, stride*2, width, height>>1, padh, padv ); + } + else + { + plane_expand_border( frame->plane[i], stride, width, height, padh, padv ); + } } } -void x264_frame_expand_border_filtered( x264_frame_t *frame ) +void x264_frame_expand_border_filtered( x264_t *h, x264_frame_t *frame ) { /* during filtering, 8 extra pixels were filtered on each edge. we want to expand border from the last filtered pixel */ + int stride = frame->i_stride[0]; + int width = 16*h->sps->i_mb_width; + int height = 16*h->sps->i_mb_height; + int padh = PADH - 8; + int padv = PADV - 8; int i; for( i = 1; i < 4; i++ ) - plane_expand_border( frame->filtered[i] - 8*frame->i_stride[0] - 8, frame->i_stride[0], frame->i_lines[0]+2*8, 24 ); + { + if( h->param.b_interlaced ) + { + plane_expand_border( frame->filtered[i] - 16*stride - 8, stride*2, width+16, (height>>1)+16, padh, padv ); + plane_expand_border( frame->filtered[i] - 15*stride - 8, stride*2, width+16, (height>>1)+16, padh, padv ); + } + else + { + plane_expand_border( frame->filtered[i] - 8*stride - 8, stride, width+16, height+16, padh, padv ); + } + } } void x264_frame_expand_border_lowres( x264_frame_t *frame ) { int i; for( i = 0; i < 4; i++ ) - plane_expand_border( frame->lowres[i], frame->i_stride_lowres, frame->i_lines_lowres, 32 ); + plane_expand_border( frame->lowres[i], frame->i_stride_lowres, frame->i_stride_lowres - 2*PADH, frame->i_lines_lowres, PADH, PADV ); } void x264_frame_expand_border_mod16( x264_t *h, x264_frame_t *frame ) @@ -257,6 +291,7 @@ void x264_frame_expand_border_mod16( x264_t *h, x264_frame_t *frame ) } if( i_pady ) { + //FIXME interlace? or just let it pad using the wrong field for( y = i_height; y < i_height + i_pady; y++ ) memcpy( &frame->plane[i][y*frame->i_stride[i]], &frame->plane[i][(i_height-1)*frame->i_stride[i]], @@ -500,9 +535,14 @@ void x264_frame_deblocking_filter( x264_t *h, int i_slice_type ) { const int s8x8 = 2 * h->mb.i_mb_stride; const int s4x4 = 4 * h->mb.i_mb_stride; - + const int b_interlaced = h->param.b_interlaced; + const int mvy_limit = 4 >> b_interlaced; int mb_y, mb_x; + int i_stride2[3] = { h->fdec->i_stride[0] << b_interlaced, + h->fdec->i_stride[1] << b_interlaced, + h->fdec->i_stride[2] << b_interlaced }; + for( mb_y = 0, mb_x = 0; mb_y < h->sps->i_mb_height; ) { const int mb_xy = mb_y * h->mb.i_mb_stride + mb_x; @@ -512,6 +552,16 @@ void x264_frame_deblocking_filter( x264_t *h, int i_slice_type ) const int i_edge_end = (h->mb.type[mb_xy] == P_SKIP) ? 1 : 4; int i_edge, i_dir; + int i_pix_y[3] = { 16*mb_y*h->fdec->i_stride[0] + 16*mb_x, + 8*mb_y*h->fdec->i_stride[1] + 8*mb_x, + 8*mb_y*h->fdec->i_stride[2] + 8*mb_x }; + if( b_interlaced && (mb_y&1) ) + { + i_pix_y[0] -= 15*h->fdec->i_stride[0]; + i_pix_y[1] -= 7*h->fdec->i_stride[1]; + i_pix_y[2] -= 7*h->fdec->i_stride[2]; + } + /* cavlc + 8x8 transform stores nnz per 16 coeffs for the purpose of * entropy coding, but per 64 coeffs for the purpose of deblocking */ if( !h->param.b_cabac && b_8x8_transform ) @@ -527,7 +577,7 @@ void x264_frame_deblocking_filter( x264_t *h, int i_slice_type ) * i_dir == 1 -> horizontal edge */ for( i_dir = 0; i_dir < 2; i_dir++ ) { - int i_start = (i_dir ? mb_y : mb_x) ? 0 : 1; + int i_start = (i_dir ? (mb_y <= b_interlaced) : (mb_x == 0)); int i_qp, i_qpn; for( i_edge = i_start; i_edge < i_edge_end; i_edge++ ) @@ -542,10 +592,17 @@ void x264_frame_deblocking_filter( x264_t *h, int i_slice_type ) mbn_8x8 = i_edge > 0 ? mb_8x8 : ( i_dir == 0 ? mb_8x8 - 2 : mb_8x8 - 2 * s8x8 ); mbn_4x4 = i_edge > 0 ? mb_4x4 : ( i_dir == 0 ? mb_4x4 - 4 : mb_4x4 - 4 * s4x4 ); + if( b_interlaced && i_edge == 0 && i_dir == 1 ) + { + mbn_xy -= h->mb.i_mb_stride; + mbn_8x8 -= 2 * s8x8; + mbn_4x4 -= 4 * s4x4; + } + /* *** Get bS for each 4px for the current edge *** */ if( IS_INTRA( h->mb.type[mb_xy] ) || IS_INTRA( h->mb.type[mbn_xy] ) ) { - bS[0] = bS[1] = bS[2] = bS[3] = ( i_edge == 0 ? 4 : 3 ); + bS[0] = bS[1] = bS[2] = bS[3] = ( i_edge == 0 && !(b_interlaced && i_dir) ? 4 : 3 ); } else { @@ -581,7 +638,7 @@ void x264_frame_deblocking_filter( x264_t *h, int i_slice_type ) { if( h->mb.ref[l][i8p] != h->mb.ref[l][i8q] || abs( h->mb.mv[l][i4p][0] - h->mb.mv[l][i4q][0] ) >= 4 || - abs( h->mb.mv[l][i4p][1] - h->mb.mv[l][i4q][1] ) >= 4 ) + abs( h->mb.mv[l][i4p][1] - h->mb.mv[l][i4q][1] ) >= mvy_limit ) { bS[i] = 1; break; @@ -599,51 +656,54 @@ void x264_frame_deblocking_filter( x264_t *h, int i_slice_type ) if( i_dir == 0 ) { /* vertical edge */ - deblock_edge( h, &h->fdec->plane[0][16*mb_y * h->fdec->i_stride[0] + 16*mb_x + 4*i_edge], - h->fdec->i_stride[0], bS, (i_qp+i_qpn+1) >> 1, 0, + deblock_edge( h, &h->fdec->plane[0][i_pix_y[0] + 4*i_edge], + i_stride2[0], bS, (i_qp+i_qpn+1) >> 1, 0, h->loopf.deblock_h_luma, h->loopf.deblock_h_luma_intra ); if( !(i_edge & 1) ) { /* U/V planes */ int i_qpc = ( i_chroma_qp_table[x264_clip3( i_qp + h->pps->i_chroma_qp_index_offset, 0, 51 )] + i_chroma_qp_table[x264_clip3( i_qpn + h->pps->i_chroma_qp_index_offset, 0, 51 )] + 1 ) >> 1; - deblock_edge( h, &h->fdec->plane[1][8*(mb_y*h->fdec->i_stride[1]+mb_x)+2*i_edge], - h->fdec->i_stride[1], bS, i_qpc, 1, + deblock_edge( h, &h->fdec->plane[1][i_pix_y[1] + 2*i_edge], + i_stride2[1], bS, i_qpc, 1, h->loopf.deblock_h_chroma, h->loopf.deblock_h_chroma_intra ); - deblock_edge( h, &h->fdec->plane[2][8*(mb_y*h->fdec->i_stride[2]+mb_x)+2*i_edge], - h->fdec->i_stride[2], bS, i_qpc, 1, + deblock_edge( h, &h->fdec->plane[2][i_pix_y[2] + 2*i_edge], + i_stride2[2], bS, i_qpc, 1, h->loopf.deblock_h_chroma, h->loopf.deblock_h_chroma_intra ); } } else { /* horizontal edge */ - deblock_edge( h, &h->fdec->plane[0][(16*mb_y + 4*i_edge) * h->fdec->i_stride[0] + 16*mb_x], - h->fdec->i_stride[0], bS, (i_qp+i_qpn+1) >> 1, 0, + deblock_edge( h, &h->fdec->plane[0][i_pix_y[0] + 4*i_edge*i_stride2[0]], + i_stride2[0], bS, (i_qp+i_qpn+1) >> 1, 0, h->loopf.deblock_v_luma, h->loopf.deblock_v_luma_intra ); /* U/V planes */ if( !(i_edge & 1) ) { int i_qpc = ( i_chroma_qp_table[x264_clip3( i_qp + h->pps->i_chroma_qp_index_offset, 0, 51 )] + i_chroma_qp_table[x264_clip3( i_qpn + h->pps->i_chroma_qp_index_offset, 0, 51 )] + 1 ) >> 1; - deblock_edge( h, &h->fdec->plane[1][8*(mb_y*h->fdec->i_stride[1]+mb_x)+2*i_edge*h->fdec->i_stride[1]], - h->fdec->i_stride[1], bS, i_qpc, 1, + deblock_edge( h, &h->fdec->plane[1][i_pix_y[1] + 2*i_edge*i_stride2[1]], + i_stride2[1], bS, i_qpc, 1, h->loopf.deblock_v_chroma, h->loopf.deblock_v_chroma_intra ); - deblock_edge( h, &h->fdec->plane[2][8*(mb_y*h->fdec->i_stride[2]+mb_x)+2*i_edge*h->fdec->i_stride[2]], - h->fdec->i_stride[2], bS, i_qpc, 1, + deblock_edge( h, &h->fdec->plane[2][i_pix_y[2] + 2*i_edge*i_stride2[2]], + i_stride2[2], bS, i_qpc, 1, h->loopf.deblock_v_chroma, h->loopf.deblock_v_chroma_intra ); } } } } - /* newt mb */ - mb_x++; + /* next mb */ + if( !b_interlaced || (mb_y&1) ) + mb_x++; if( mb_x >= h->sps->i_mb_width ) { mb_x = 0; mb_y++; } + else + mb_y ^= b_interlaced; } } diff --git a/common/frame.h b/common/frame.h index 2eedff72..ce802b8b 100644 --- a/common/frame.h +++ b/common/frame.h @@ -49,7 +49,8 @@ typedef struct /* for unrestricted mv we allocate more data than needed * allocated data are stored in buffer */ - void *buffer[12]; + void *buffer[8]; + void *buffer_lowres[4]; /* motion data */ int8_t *mb_type; @@ -90,14 +91,14 @@ void x264_frame_delete( x264_frame_t *frame ); void x264_frame_copy_picture( x264_t *h, x264_frame_t *dst, x264_picture_t *src ); -void x264_frame_expand_border( x264_frame_t *frame ); -void x264_frame_expand_border_filtered( x264_frame_t *frame ); +void x264_frame_expand_border( x264_t *h, x264_frame_t *frame ); +void x264_frame_expand_border_filtered( x264_t *h, x264_frame_t *frame ); void x264_frame_expand_border_lowres( x264_frame_t *frame ); void x264_frame_expand_border_mod16( x264_t *h, x264_frame_t *frame ); void x264_frame_deblocking_filter( x264_t *h, int i_slice_type ); -void x264_frame_filter( int cpu, x264_frame_t *frame ); +void x264_frame_filter( int cpu, x264_frame_t *frame, int b_interlaced ); void x264_frame_init_lowres( int cpu, x264_frame_t *frame ); void x264_deblock_init( int cpu, x264_deblock_function_t *pf ); diff --git a/common/macroblock.c b/common/macroblock.c index 9bb854a3..4a24ff2c 100644 --- a/common/macroblock.c +++ b/common/macroblock.c @@ -504,7 +504,7 @@ void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int mvc[8][2 } if( h->mb.i_neighbour & MB_TOP ) { - int i_mb_t = h->mb.i_mb_xy - h->mb.i_mb_stride; + int i_mb_t = h->mb.i_mb_top_xy; if( !IS_SKIP( h->mb.type[i_mb_t] ) ) SET_MVP( mvr[i_mb_t] ); @@ -516,7 +516,8 @@ void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int mvc[8][2 #undef SET_MVP /* temporal predictors */ - if( h->fref0[0]->i_ref[0] > 0 ) + /* FIXME temporal scaling w/ interlace */ + if( h->fref0[0]->i_ref[0] > 0 && !h->sh.b_mbaff ) { x264_frame_t *l0 = h->fref0[0]; int ref_col_cur, ref_col_prev = -1; @@ -555,12 +556,16 @@ static inline void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int hei const int i8 = x264_scan8[0]+x+8*y; const int i_ref = h->mb.cache.ref[0][i8]; const int mvx = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ); - const int mvy = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ); + int mvy = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ); h->mc.mc_luma( h->mb.pic.p_fref[0][i_ref], h->mb.pic.i_stride[0], &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height ); + // chroma is offset if MCing from a field of opposite parity + if( h->mb.b_interlaced & i_ref ) + mvy += (h->mb.i_mb_y & 1)*4 - 2; + h->mc.mc_chroma( &h->mb.pic.p_fref[0][i_ref][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1], &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, mvx, mvy, 2*width, 2*height ); @@ -574,12 +579,15 @@ static inline void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int hei const int i8 = x264_scan8[0]+x+8*y; const int i_ref = h->mb.cache.ref[1][i8]; const int mvx = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ); - const int mvy = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ); + int mvy = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ); h->mc.mc_luma( h->mb.pic.p_fref[1][i_ref], h->mb.pic.i_stride[0], &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height ); + if( h->mb.b_interlaced & i_ref ) + mvy += (h->mb.i_mb_y & 1)*4 - 2; + h->mc.mc_chroma( &h->mb.pic.p_fref[1][i_ref][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1], &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, mvx, mvy, 2*width, 2*height ); @@ -595,7 +603,7 @@ static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int he const int i_ref1 = h->mb.cache.ref[1][i8]; const int mvx1 = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] ); - const int mvy1 = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ); + int mvy1 = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] ); DECLARE_ALIGNED( uint8_t, tmp[16*16], 16 ); int i_mode = x264_size2pixel[height][width]; @@ -604,6 +612,9 @@ static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int he h->mc.mc_luma( h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0], tmp, 16, mvx1 + 4*4*x, mvy1 + 4*4*y, 4*width, 4*height ); + if( h->mb.b_interlaced & i_ref1 ) + mvy1 += (h->mb.i_mb_y & 1)*4 - 2; + if( h->param.analyse.b_weighted_bipred ) { const int i_ref0 = h->mb.cache.ref[0][i8]; @@ -823,6 +834,8 @@ int x264_macroblock_cache_init( x264_t *h ) h->mb.i_b8_stride = h->sps->i_mb_width * 2; h->mb.i_b4_stride = h->sps->i_mb_width * 4; + h->mb.b_interlaced = h->param.b_interlaced; + CHECKED_MALLOC( h->mb.qp, i_mb_count * sizeof(int8_t) ); CHECKED_MALLOC( h->mb.cbp, i_mb_count * sizeof(int16_t) ); CHECKED_MALLOC( h->mb.skipbp, i_mb_count * sizeof(int8_t) ); @@ -843,8 +856,8 @@ int x264_macroblock_cache_init( x264_t *h ) for( i=0; i<2; i++ ) { - int i_refs = (i ? 1 : h->param.i_frame_reference) + h->param.b_bframe_pyramid; - for( j=0; j < i_refs && j < 16; j++ ) + int i_refs = X264_MIN(16, (i ? 1 : h->param.i_frame_reference) + h->param.b_bframe_pyramid) << h->param.b_interlaced; + for( j=0; j < i_refs; j++ ) CHECKED_MALLOC( h->mb.mvr[i][j], 2 * i_mb_count * sizeof(int16_t) ); } @@ -917,80 +930,33 @@ void x264_macroblock_slice_init( x264_t *h ) void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y ) { - const int i_mb_4x4 = 4*(i_mb_y * h->mb.i_b4_stride + i_mb_x); - const int i_mb_8x8 = 2*(i_mb_y * h->mb.i_b8_stride + i_mb_x); - int i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x; - int i_top_xy = i_mb_xy - h->mb.i_mb_stride; + int i_mb_4x4 = 4*(i_mb_y * h->mb.i_b4_stride + i_mb_x); + int i_mb_8x8 = 2*(i_mb_y * h->mb.i_b8_stride + i_mb_x); + int i_top_y = i_mb_y - (1 << h->mb.b_interlaced); + int i_top_xy = i_top_y * h->mb.i_mb_stride + i_mb_x; + int i_top_4x4 = (4*i_top_y+3) * h->mb.i_b4_stride + 4*i_mb_x; + int i_top_8x8 = (2*i_top_y+1) * h->mb.i_b8_stride + 2*i_mb_x; int i_left_xy = -1; int i_top_type = -1; /* gcc warn */ int i_left_type= -1; int i; + assert( h->mb.i_b8_stride == 2*h->mb.i_mb_stride ); + assert( h->mb.i_b4_stride == 4*h->mb.i_mb_stride ); + /* init index */ h->mb.i_mb_x = i_mb_x; h->mb.i_mb_y = i_mb_y; h->mb.i_mb_xy = i_mb_xy; h->mb.i_b8_xy = i_mb_8x8; h->mb.i_b4_xy = i_mb_4x4; + h->mb.i_mb_top_xy = i_top_xy; h->mb.i_neighbour = 0; - /* fdec: fenc: - * yyyyyyy - * yYYYY YYYY - * yYYYY YYYY - * yYYYY YYYY - * yYYYY YYYY - * uuu vvv UUVV - * uUU vVV UUVV - * uUU vVV - */ - h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf; - h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE; - h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8; - h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE; - h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE; - h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16; - - /* load picture pointers */ - for( i = 0; i < 3; i++ ) - { - const int w = (i == 0 ? 16 : 8); - const int i_stride = h->fdec->i_stride[i]; - const uint8_t *plane_fdec = &h->fdec->plane[i][ w * (i_mb_x + i_mb_y * i_stride) ]; - int j; - - h->mb.pic.i_stride[i] = i_stride; - - h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE, - &h->fenc->plane[i][ w * (i_mb_x + i_mb_y * i_stride) ], i_stride, w ); - memcpy( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], &plane_fdec[-1-i_stride], w*3/2+1 ); - for( j = 0; j < w; j++ ) - h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride]; - - for( j = 0; j < h->i_ref0; j++ ) - { - h->mb.pic.p_fref[0][j][i==0 ? 0:i+3] = &h->fref0[j]->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )]; - h->mb.pic.p_fref[0][j][i+1] = &h->fref0[j]->filtered[i+1][ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )]; - } - for( j = 0; j < h->i_ref1; j++ ) - { - h->mb.pic.p_fref[1][j][i==0 ? 0:i+3] = &h->fref1[j]->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )]; - h->mb.pic.p_fref[1][j][i+1] = &h->fref1[j]->filtered[i+1][ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )]; - } - } - - if( h->fdec->integral ) - { - for( i = 0; i < h->i_ref0; i++ ) - h->mb.pic.p_integral[0][i] = &h->fref0[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )]; - for( i = 0; i < h->i_ref1; i++ ) - h->mb.pic.p_integral[1][i] = &h->fref1[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )]; - } - /* load cache */ - if( i_mb_xy >= h->sh.i_first_mb + h->mb.i_mb_stride ) + if( i_top_xy >= h->sh.i_first_mb ) { h->mb.i_mb_type_top = i_top_type= h->mb.type[i_top_xy]; @@ -1105,6 +1071,78 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y ) + ( i_top_type >= 0 && h->mb.mb_transform_size[i_top_xy] ); } + if( h->sh.b_mbaff ) + { + h->mb.pic.i_fref[0] = h->i_ref0 << h->mb.b_interlaced; + h->mb.pic.i_fref[1] = h->i_ref1 << h->mb.b_interlaced; + h->mb.cache.i_neighbour_interlaced = + !!(h->mb.i_neighbour & MB_LEFT) + + !!(h->mb.i_neighbour & MB_TOP); + } + + /* fdec: fenc: + * yyyyyyy + * yYYYY YYYY + * yYYYY YYYY + * yYYYY YYYY + * yYYYY YYYY + * uuu vvv UUVV + * uUU vVV UUVV + * uUU vVV + */ + h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf; + h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE; + h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8; + h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE; + h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE; + h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16; + + /* load picture pointers */ + for( i = 0; i < 3; i++ ) + { + const int w = (i == 0 ? 16 : 8); + const int i_stride = h->fdec->i_stride[i]; + const int i_stride2 = i_stride << h->mb.b_interlaced; + const int i_pix_offset = h->mb.b_interlaced + ? w * (i_mb_x + (i_mb_y&~1) * i_stride) + (i_mb_y&1) * i_stride + : w * (i_mb_x + i_mb_y * i_stride); + int ref_pix_offset[2] = { i_pix_offset, i_pix_offset }; + const uint8_t *plane_fdec = &h->fdec->plane[i][i_pix_offset]; + x264_frame_t **fref[2] = { h->fref0, h->fref1 }; + int j, k, l; + + if( h->mb.b_interlaced ) + ref_pix_offset[1] += (1-2*(i_mb_y&1)) * i_stride; + + h->mb.pic.i_stride[i] = i_stride2; + + h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE, + &h->fenc->plane[i][i_pix_offset], i_stride2, w ); + memcpy( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], &plane_fdec[-1-i_stride2], w*3/2+1 ); + for( j = 0; j < w; j++ ) + h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2]; + + for( l=0; l<2; l++ ) + { + for( j=0; jmb.pic.i_fref[l]; j++ ) + { + h->mb.pic.p_fref[l][j][i==0 ? 0:i+3] = &fref[l][j >> h->mb.b_interlaced]->plane[i][ref_pix_offset[j&1]]; + if( i == 0 ) + for( k = 1; k < 4; k++ ) + h->mb.pic.p_fref[l][j][k] = &fref[l][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]]; + } + } + } + + if( h->fdec->integral ) + { + assert( !h->mb.b_interlaced ); + for( i = 0; i < h->mb.pic.i_fref[0]; i++ ) + h->mb.pic.p_integral[0][i] = &h->fref0[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )]; + for( i = 0; i < h->mb.pic.i_fref[1]; i++ ) + h->mb.pic.p_integral[1][i] = &h->fref1[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )]; + } + /* load ref/mv/mvd */ if( h->sh.i_type != SLICE_TYPE_I ) { @@ -1124,8 +1162,8 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y ) if( h->mb.i_neighbour & MB_TOPLEFT ) { const int i8 = x264_scan8[0] - 1 - 1*8; - const int ir = i_mb_8x8 - s8x8 - 1; - const int iv = i_mb_4x4 - s4x4 - 1; + const int ir = i_top_8x8 - 1; + const int iv = i_top_4x4 - 1; h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir]; h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0]; h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1]; @@ -1141,9 +1179,8 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y ) if( h->mb.i_neighbour & MB_TOP ) { const int i8 = x264_scan8[0] - 8; - const int ir = i_mb_8x8 - s8x8; - const int iv = i_mb_4x4 - s4x4; - + const int ir = i_top_8x8; + const int iv = i_top_4x4; h->mb.cache.ref[i_list][i8+0] = h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0]; h->mb.cache.ref[i_list][i8+2] = @@ -1169,9 +1206,8 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y ) if( h->mb.i_neighbour & MB_TOPRIGHT ) { const int i8 = x264_scan8[0] + 4 - 1*8; - const int ir = i_mb_8x8 - s8x8 + 2; - const int iv = i_mb_4x4 - s4x4 + 4; - + const int ir = i_top_8x8 + 2; + const int iv = i_top_4x4 + 4; h->mb.cache.ref[i_list][i8] = h->mb.ref[i_list][ir]; h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0]; h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1]; @@ -1189,7 +1225,6 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y ) const int i8 = x264_scan8[0] - 1; const int ir = i_mb_8x8 - 1; const int iv = i_mb_4x4 - 1; - h->mb.cache.ref[i_list][i8+0*8] = h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8]; h->mb.cache.ref[i_list][i8+2*8] = @@ -1217,7 +1252,7 @@ void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y ) if( i_top_type >= 0 ) { const int i8 = x264_scan8[0] - 8; - const int iv = i_mb_4x4 - s4x4; + const int iv = i_top_4x4; for( i = 0; i < 4; i++ ) { h->mb.cache.mvd[i_list][i8+i][0] = h->mb.mvd[i_list][iv + i][0]; @@ -1311,9 +1346,13 @@ void x264_macroblock_cache_save( x264_t *h ) for( i = 0; i < 3; i++ ) { int w = i ? 8 : 16; + int i_stride = h->fdec->i_stride[i]; + int i_stride2 = i_stride << h->mb.b_interlaced; + int i_pix_offset = h->mb.b_interlaced + ? w * (h->mb.i_mb_x + (h->mb.i_mb_y&~1) * i_stride) + (h->mb.i_mb_y&1) * i_stride + : w * (h->mb.i_mb_x + h->mb.i_mb_y * i_stride); h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( - &h->fdec->plane[i][ w * (h->mb.i_mb_x + h->mb.i_mb_y * h->fdec->i_stride[i]) ], - h->fdec->i_stride[i], + &h->fdec->plane[i][i_pix_offset], i_stride2, h->mb.pic.p_fdec[i], FDEC_STRIDE, w ); } @@ -1325,6 +1364,7 @@ void x264_macroblock_cache_save( x264_t *h ) h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp; h->mb.i_last_qp = h->mb.i_qp; + h->mb.i_mb_prev_xy = h->mb.i_mb_xy; /* save intra4x4 */ if( i_mb_type == I_4x4 ) @@ -1502,4 +1542,10 @@ void x264_macroblock_bipred_init( x264_t *h ) h->mb.bipred_weight[i_ref0][i_ref1] = 32; } } + if( h->sh.b_mbaff ) + { + for( i_ref0 = 2*h->i_ref0-1; i_ref0 >= 0; i_ref0-- ) + for( i_ref1 = 2*h->i_ref1-1; i_ref1 >= 0; i_ref1-- ) + h->mb.bipred_weight[i_ref0][i_ref1] = h->mb.bipred_weight[i_ref0>>1][i_ref1>>1]; + } } diff --git a/common/macroblock.h b/common/macroblock.h index b08f0e9d..ff9dd826 100644 --- a/common/macroblock.h +++ b/common/macroblock.h @@ -182,17 +182,26 @@ static const int x264_mb_partition_pixel_table[17] = }; /* zigzags are transposed with respect to the tables in the standard */ -static const int x264_zigzag_scan4[16] = -{ +static const int x264_zigzag_scan4[2][16] = +{{ // frame 0, 4, 1, 2, 5, 8, 12, 9, 6, 3, 7, 10, 13, 14, 11, 15 -}; -static const int x264_zigzag_scan8[64] = -{ +}, +{ // field + 0, 1, 4, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 +}}; +static const int x264_zigzag_scan8[2][64] = +{{ 0, 8, 1, 2, 9, 16, 24, 17, 10, 3, 4, 11, 18, 25, 32, 40, 33, 26, 19, 12, 5, 6, 13, 20, 27, 34, 41, 48, 56, 49, 42, 35, 28, 21, 14, 7, 15, 22, 29, 36, 43, 50, 57, 58, 51, 44, 37, 30, 23, 31, 38, 45, 52, 59, 60, 53, 46, 39, 47, 54, 61, 62, 55, 63 -}; +}, +{ + 0, 1, 2, 8, 9, 3, 4, 10, 16, 11, 5, 6, 7, 12, 17, 24, + 18, 13, 14, 15, 19, 25, 32, 26, 20, 21, 22, 23, 27, 33, 40, 34, + 28, 29, 30, 31, 35, 41, 48, 42, 36, 37, 38, 39, 43, 49, 50, 44, + 45, 46, 47, 51, 56, 57, 52, 53, 54, 55, 58, 59, 60, 61, 62, 63 +}}; static const uint8_t block_idx_x[16] = { diff --git a/common/mc.c b/common/mc.c index 68802690..e915fb77 100644 --- a/common/mc.c +++ b/common/mc.c @@ -372,10 +372,11 @@ extern void x264_center_filter_mmxext( uint8_t *dst1, int i_dst1_stride, uint8_t *src, int i_src_stride, int i_width, int i_height ); -void x264_frame_filter( int cpu, x264_frame_t *frame ) +void x264_frame_filter( int cpu, x264_frame_t *frame, int b_interlaced ) { const int x_inc = 16, y_inc = 16; - const int stride = frame->i_stride[0]; + const int stride = frame->i_stride[0] << b_interlaced; + const int height = frame->i_lines[0] >> b_interlaced; int x, y; pf_mc_t int_h = mc_hh; @@ -387,16 +388,16 @@ void x264_frame_filter( int cpu, x264_frame_t *frame ) { x264_horizontal_filter_mmxext(frame->filtered[1] - 8 * stride - 8, stride, frame->plane[0] - 8 * stride - 8, stride, - stride - 48, frame->i_lines[0] + 16); + stride - 48, height + 16); x264_center_filter_mmxext(frame->filtered[2] - 8 * stride - 8, stride, frame->filtered[3] - 8 * stride - 8, stride, frame->plane[0] - 8 * stride - 8, stride, - stride - 48, frame->i_lines[0] + 16); + stride - 48, height + 16); } else #endif { - for( y = -8; y < frame->i_lines[0]+8; y += y_inc ) + for( y = -8; y < height + 8; y += y_inc ) { uint8_t *p_in = frame->plane[0] + y * stride - 8; uint8_t *p_h = frame->filtered[1] + y * stride - 8; diff --git a/encoder/analyse.c b/encoder/analyse.c index 3e4715a9..8d1c6c0f 100644 --- a/encoder/analyse.c +++ b/encoder/analyse.c @@ -230,10 +230,12 @@ static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp ) h->mb.mv_max_spel[0] = 4*( h->mb.mv_max_fpel[0] + 16 ); if( h->mb.i_mb_x == 0) { - h->mb.mv_min[1] = 4*( -16*h->mb.i_mb_y - 24 ); - h->mb.mv_max[1] = 4*( 16*( h->sps->i_mb_height - h->mb.i_mb_y - 1 ) + 24 ); - h->mb.mv_min_fpel[1] = CLIP_FMV( -16*h->mb.i_mb_y - 8 ); - h->mb.mv_max_fpel[1] = CLIP_FMV( 16*( h->sps->i_mb_height - h->mb.i_mb_y - 1 ) + 8 ); + int mb_y = h->mb.i_mb_y >> h->sh.b_mbaff; + int mb_height = h->sps->i_mb_height >> h->sh.b_mbaff; + h->mb.mv_min[1] = 4*( -16*mb_y - 24 ); + h->mb.mv_max[1] = 4*( 16*( mb_height - mb_y - 1 ) + 24 ); + h->mb.mv_min_fpel[1] = CLIP_FMV( -16*mb_y - 8 ); + h->mb.mv_max_fpel[1] = CLIP_FMV( 16*( mb_height - mb_y - 1 ) + 8 ); h->mb.mv_min_spel[1] = 4*( h->mb.mv_min_fpel[1] - 16 ); h->mb.mv_max_spel[1] = 4*( h->mb.mv_max_fpel[1] + 16 ); } @@ -868,7 +870,7 @@ static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a ) int i_ref; int mvc[7][2], i_mvc; int i_halfpel_thresh = INT_MAX; - int *p_halfpel_thresh = h->i_ref0>1 ? &i_halfpel_thresh : NULL; + int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL; /* 16x16 Search on all ref frame */ m.i_pixel = PIXEL_16x16; @@ -876,7 +878,7 @@ static void x264_mb_analyse_inter_p16x16( x264_t *h, x264_mb_analysis_t *a ) LOAD_FENC( &m, h->mb.pic.p_fenc, 0, 0 ); a->l0.me16x16.cost = INT_MAX; - for( i_ref = 0; i_ref < h->i_ref0; i_ref++ ) + for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ ) { const int i_ref_cost = REF_COST( 0, i_ref ); i_halfpel_thresh -= i_ref_cost; @@ -939,9 +941,9 @@ static void x264_mb_analyse_inter_p8x8_mixed_ref( x264_t *h, x264_mb_analysis_t int i_ref; uint8_t **p_fenc = h->mb.pic.p_fenc; int i_halfpel_thresh = INT_MAX; - int *p_halfpel_thresh = /*h->i_ref0>1 ? &i_halfpel_thresh : */NULL; + int *p_halfpel_thresh = /*h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : */NULL; int i; - int i_maxref = h->i_ref0-1; + int i_maxref = h->mb.pic.i_fref[0]-1; h->mb.i_partition = D_8x8; @@ -1349,7 +1351,7 @@ static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a ) int i_ref; int mvc[8][2], i_mvc; int i_halfpel_thresh = INT_MAX; - int *p_halfpel_thresh = h->i_ref0>1 ? &i_halfpel_thresh : NULL; + int *p_halfpel_thresh = h->mb.pic.i_fref[0]>1 ? &i_halfpel_thresh : NULL; /* 16x16 Search on all ref frame */ m.i_pixel = PIXEL_16x16; @@ -1358,7 +1360,7 @@ static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a ) /* ME for List 0 */ a->l0.me16x16.cost = INT_MAX; - for( i_ref = 0; i_ref < h->i_ref0; i_ref++ ) + for( i_ref = 0; i_ref < h->mb.pic.i_fref[0]; i_ref++ ) { /* search with ref */ LOAD_HPELS( &m, h->mb.pic.p_fref[0][i_ref], 0, i_ref, 0, 0 ); @@ -1384,9 +1386,9 @@ static void x264_mb_analyse_inter_b16x16( x264_t *h, x264_mb_analysis_t *a ) /* ME for list 1 */ i_halfpel_thresh = INT_MAX; - p_halfpel_thresh = h->i_ref1>1 ? &i_halfpel_thresh : NULL; + p_halfpel_thresh = h->mb.pic.i_fref[1]>1 ? &i_halfpel_thresh : NULL; a->l1.me16x16.cost = INT_MAX; - for( i_ref = 0; i_ref < h->i_ref1; i_ref++ ) + for( i_ref = 0; i_ref < h->mb.pic.i_fref[1]; i_ref++ ) { /* search with ref */ LOAD_HPELS( &m, h->mb.pic.p_fref[1][i_ref], 1, i_ref, 0, 0 ); diff --git a/encoder/cabac.c b/encoder/cabac.c index 38c03eec..b0964bc6 100644 --- a/encoder/cabac.c +++ b/encoder/cabac.c @@ -83,6 +83,12 @@ static void x264_cabac_mb_type( x264_t *h, x264_cabac_t *cb ) { const int i_mb_type = h->mb.i_type; + if( h->sh.b_mbaff && + (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) ) + { + x264_cabac_encode_decision( cb, 70 + h->mb.cache.i_neighbour_interlaced, h->mb.b_interlaced ); + } + if( h->sh.i_type == SLICE_TYPE_I ) { int ctx = 0; @@ -250,7 +256,7 @@ static void x264_cabac_mb_intra_chroma_pred_mode( x264_t *h, x264_cabac_t *cb ) { ctx++; } - if( (h->mb.i_neighbour & MB_TOP) && h->mb.chroma_pred_mode[h->mb.i_mb_xy - h->mb.i_mb_stride] != 0 ) + if( (h->mb.i_neighbour & MB_TOP) && h->mb.chroma_pred_mode[h->mb.i_mb_top_xy] != 0 ) { ctx++; } @@ -286,7 +292,7 @@ static void x264_cabac_mb_cbp_luma( x264_t *h, x264_cabac_t *cb ) if( y > 0 ) i_mbb_xy = h->mb.i_mb_xy; else if( h->mb.i_neighbour & MB_TOP ) - i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride; + i_mbb_xy = h->mb.i_mb_top_xy; /* No need to test for PCM and SKIP */ @@ -326,7 +332,7 @@ static void x264_cabac_mb_cbp_chroma( x264_t *h, x264_cabac_t *cb ) if( h->mb.i_neighbour & MB_TOP ) { - cbp_b = (h->mb.cbp[h->mb.i_mb_xy - h->mb.i_mb_stride] >> 4)&0x3; + cbp_b = (h->mb.cbp[h->mb.i_mb_top_xy] >> 4)&0x3; } ctx = 0; @@ -350,12 +356,12 @@ static void x264_cabac_mb_cbp_chroma( x264_t *h, x264_cabac_t *cb ) /* TODO check it with != qp per mb */ static void x264_cabac_mb_qp_delta( x264_t *h, x264_cabac_t *cb ) { - int i_mbn_xy = h->mb.i_mb_xy - 1; + int i_mbn_xy = h->mb.i_mb_prev_xy; int i_dqp = h->mb.i_qp - h->mb.i_last_qp; int ctx; /* No need to test for PCM / SKIP */ - if( i_mbn_xy >= h->sh.i_first_mb && h->mb.i_last_dqp != 0 && + if( h->mb.i_neighbour && h->mb.i_last_dqp != 0 && ( h->mb.type[i_mbn_xy] == I_16x16 || (h->mb.cbp[i_mbn_xy]&0x3f) ) ) ctx = 1; else @@ -596,13 +602,13 @@ static int x264_cabac_mb_cbf_ctxidxinc( x264_t *h, int i_cat, int i_idx ) if( h->mb.i_neighbour & MB_LEFT ) { i_mba_xy = h->mb.i_mb_xy - 1; - if( h->mb.type[i_mba_xy] == I_16x16 ) + if( h->mb.i_mb_type_left == I_16x16 ) i_nza = h->mb.cbp[i_mba_xy] & 0x100; } if( h->mb.i_neighbour & MB_TOP ) { - i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride; - if( h->mb.type[i_mbb_xy] == I_16x16 ) + i_mbb_xy = h->mb.i_mb_top_xy; + if( h->mb.i_mb_type_top == I_16x16 ) i_nzb = h->mb.cbp[i_mbb_xy] & 0x100; } } @@ -616,7 +622,7 @@ static int x264_cabac_mb_cbf_ctxidxinc( x264_t *h, int i_cat, int i_idx ) if( i_idx & ~5 ) // block_idx_y > 0 i_mbb_xy = h->mb.i_mb_xy; else if( h->mb.i_neighbour & MB_TOP ) - i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride; + i_mbb_xy = h->mb.i_mb_top_xy; /* no need to test for skip/pcm */ if( i_mba_xy >= 0 ) @@ -634,7 +640,7 @@ static int x264_cabac_mb_cbf_ctxidxinc( x264_t *h, int i_cat, int i_idx ) } if( h->mb.i_neighbour & MB_TOP ) { - i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride; + i_mbb_xy = h->mb.i_mb_top_xy; i_nzb = h->mb.cbp[i_mbb_xy] & (0x200 << i_idx); } } @@ -648,7 +654,7 @@ static int x264_cabac_mb_cbf_ctxidxinc( x264_t *h, int i_cat, int i_idx ) if( i_idx & 2 ) i_mbb_xy = h->mb.i_mb_xy; else if( h->mb.i_neighbour & MB_TOP ) - i_mbb_xy = h->mb.i_mb_xy - h->mb.i_mb_stride; + i_mbb_xy = h->mb.i_mb_top_xy; /* no need to test skip/pcm */ if( i_mba_xy >= 0 ) @@ -674,26 +680,41 @@ static int x264_cabac_mb_cbf_ctxidxinc( x264_t *h, int i_cat, int i_idx ) } -static const int significant_coeff_flag_offset[6] = { 105, 120, 134, 149, 152, 402 }; -static const int last_coeff_flag_offset[6] = { 166, 181, 195, 210, 213, 417 }; -static const int coeff_abs_level_m1_offset[6] = { 227, 237, 247, 257, 266, 426 }; -static const int significant_coeff_flag_offset_8x8[63] = { +static const int significant_coeff_flag_offset[2][6] = { + { 105, 120, 134, 149, 152, 402 }, + { 277, 292, 306, 321, 324, 436 } +}; +static const int last_coeff_flag_offset[2][6] = { + { 166, 181, 195, 210, 213, 417 }, + { 338, 353, 367, 382, 385, 451 } +}; +static const int coeff_abs_level_m1_offset[6] = + { 227, 237, 247, 257, 266, 426 }; +static const int significant_coeff_flag_offset_8x8[2][63] = +{{ 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5, 4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9,10, 9, 8, 7, 7, 6,11,12,13,11, 6, 7, 8, 9,14,10, 9, 8, 6,11, 12,13,11, 6, 9,14,10, 9,11,12,13,11,14,10,12 -}; +},{ + 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5, + 6, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,11,12,11, + 9, 9,10,10, 8,11,12,11, 9, 9,10,10, 8,13,13, 9, + 9,10,10, 8,13,13, 9, 9,10,10,14,14,14,14,14 +}}; static const int last_coeff_flag_offset_8x8[63] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8 }; +static const int identity[16] = + { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; static void block_residual_write_cabac( x264_t *h, x264_cabac_t *cb, int i_ctxBlockCat, int i_idx, int *l, int i_count ) { - const int i_ctx_sig = significant_coeff_flag_offset[i_ctxBlockCat]; - const int i_ctx_last = last_coeff_flag_offset[i_ctxBlockCat]; + const int i_ctx_sig = significant_coeff_flag_offset[h->mb.b_interlaced][i_ctxBlockCat]; + const int i_ctx_last = last_coeff_flag_offset[h->mb.b_interlaced][i_ctxBlockCat]; const int i_ctx_level = coeff_abs_level_m1_offset[i_ctxBlockCat]; int i_coeff_abs_m1[64]; @@ -707,6 +728,9 @@ static void block_residual_write_cabac( x264_t *h, x264_cabac_t *cb, int i_ctxBl int i; + const int *significant_coeff_flag_offset; + const int *last_coeff_flag_offset; + /* i_ctxBlockCat: 0-> DC 16x16 i_idx = 0 * 1-> AC 16x16 i_idx = luma4x4idx * 2-> Luma4x4 i_idx = luma4x4idx @@ -735,23 +759,18 @@ static void block_residual_write_cabac( x264_t *h, x264_cabac_t *cb, int i_ctxBl return; } + significant_coeff_flag_offset = (i_ctxBlockCat == DCT_LUMA_8x8) + ? significant_coeff_flag_offset_8x8[h->mb.b_interlaced] + : identity; + last_coeff_flag_offset = (i_ctxBlockCat == DCT_LUMA_8x8) + ? last_coeff_flag_offset_8x8 : identity; + i_sigmap_size = X264_MIN( i_last+1, i_count-1 ); for( i = 0; i < i_sigmap_size; i++ ) { - int i_sig_ctxIdxInc; - int i_last_ctxIdxInc; - - if( i_ctxBlockCat == DCT_LUMA_8x8 ) - { - i_sig_ctxIdxInc = significant_coeff_flag_offset_8x8[i]; - i_last_ctxIdxInc = last_coeff_flag_offset_8x8[i]; - } - else - i_sig_ctxIdxInc = i_last_ctxIdxInc = i; - - x264_cabac_encode_decision( cb, i_ctx_sig + i_sig_ctxIdxInc, l[i] != 0 ); + x264_cabac_encode_decision( cb, i_ctx_sig + significant_coeff_flag_offset[i], l[i] != 0 ); if( l[i] != 0 ) - x264_cabac_encode_decision( cb, i_ctx_last + i_last_ctxIdxInc, i == i_last ); + x264_cabac_encode_decision( cb, i_ctx_last + last_coeff_flag_offset[i], i == i_last ); } for( i = i_coeff - 1; i >= 0; i-- ) @@ -854,7 +873,7 @@ void x264_macroblock_write_cabac( x264_t *h, x264_cabac_t *cb ) { if( h->mb.i_partition == D_16x16 ) { - if( h->sh.i_num_ref_idx_l0_active > 1 ) + if( h->mb.pic.i_fref[0] > 1 ) { x264_cabac_mb_ref( h, cb, 0, 0 ); } @@ -862,7 +881,7 @@ void x264_macroblock_write_cabac( x264_t *h, x264_cabac_t *cb ) } else if( h->mb.i_partition == D_16x8 ) { - if( h->sh.i_num_ref_idx_l0_active > 1 ) + if( h->mb.pic.i_fref[0] > 1 ) { x264_cabac_mb_ref( h, cb, 0, 0 ); x264_cabac_mb_ref( h, cb, 0, 8 ); @@ -872,7 +891,7 @@ void x264_macroblock_write_cabac( x264_t *h, x264_cabac_t *cb ) } else if( h->mb.i_partition == D_8x16 ) { - if( h->sh.i_num_ref_idx_l0_active > 1 ) + if( h->mb.pic.i_fref[0] > 1 ) { x264_cabac_mb_ref( h, cb, 0, 0 ); x264_cabac_mb_ref( h, cb, 0, 4 ); @@ -890,7 +909,7 @@ void x264_macroblock_write_cabac( x264_t *h, x264_cabac_t *cb ) x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[3] ); /* ref 0 */ - if( h->sh.i_num_ref_idx_l0_active > 1 ) + if( h->mb.pic.i_fref[0] > 1 ) { x264_cabac_mb_ref( h, cb, 0, 0 ); x264_cabac_mb_ref( h, cb, 0, 4 ); @@ -912,7 +931,7 @@ void x264_macroblock_write_cabac( x264_t *h, x264_cabac_t *cb ) /* ref */ for( i_list = 0; i_list < 2; i_list++ ) { - if( ( i_list ? h->sh.i_num_ref_idx_l1_active : h->sh.i_num_ref_idx_l0_active ) == 1 ) + if( ( i_list ? h->mb.pic.i_fref[1] : h->mb.pic.i_fref[0] ) == 1 ) continue; for( i = 0; i < 4; i++ ) if( x264_mb_partition_listX_table[i_list][ h->mb.i_sub_partition[i] ] ) @@ -938,7 +957,7 @@ void x264_macroblock_write_cabac( x264_t *h, x264_cabac_t *cb ) for( i_list = 0; i_list < 2; i_list++ ) { - const int i_ref_max = i_list == 0 ? h->sh.i_num_ref_idx_l0_active : h->sh.i_num_ref_idx_l1_active; + const int i_ref_max = i_list == 0 ? h->mb.pic.i_fref[0] : h->mb.pic.i_fref[1]; if( i_ref_max > 1 ) { @@ -1056,13 +1075,13 @@ void x264_partition_size_cabac( x264_t *h, x264_cabac_t *cb, int i8, int i_pixel if( i_mb_type == P_8x8 ) { x264_cabac_mb_sub_p_partition( cb, h->mb.i_sub_partition[i8] ); - if( h->sh.i_num_ref_idx_l0_active > 1 ) + if( h->mb.pic.i_fref[0] > 1 ) x264_cabac_mb_ref( h, cb, 0, 4*i8 ); x264_cabac_mb8x8_mvd( h, cb, 0, i8 ); } else if( i_mb_type == P_L0 ) { - if( h->sh.i_num_ref_idx_l0_active > 1 ) + if( h->mb.pic.i_fref[0] > 1 ) x264_cabac_mb_ref( h, cb, 0, 4*i8 ); if( h->mb.i_partition == D_16x8 ) x264_cabac_mb_mvd( h, cb, 0, 4*i8, 4, 2 ); @@ -1073,10 +1092,10 @@ void x264_partition_size_cabac( x264_t *h, x264_cabac_t *cb, int i8, int i_pixel { x264_cabac_mb_sub_b_partition( cb, h->mb.i_sub_partition[i8] ); - if( h->sh.i_num_ref_idx_l0_active > 1 + if( h->mb.pic.i_fref[0] > 1 && x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i8] ] ) x264_cabac_mb_ref( h, cb, 0, 4*i8 ); - if( h->sh.i_num_ref_idx_l1_active > 1 + if( h->mb.pic.i_fref[1] > 1 && x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i8] ] ) x264_cabac_mb_ref( h, cb, 1, 4*i8 ); diff --git a/encoder/cavlc.c b/encoder/cavlc.c index b6ee9306..0589f9ca 100644 --- a/encoder/cavlc.c +++ b/encoder/cavlc.c @@ -363,6 +363,12 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s ) return; } + if( h->sh.b_mbaff + && (!(h->mb.i_mb_y & 1) || IS_SKIP(h->mb.type[h->mb.i_mb_xy - h->mb.i_mb_stride])) ) + { + bs_write1( s, h->mb.b_interlaced ); + } + /* Write: - type - prediction @@ -446,9 +452,9 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s ) { bs_write_ue( s, 0 ); - if( h->sh.i_num_ref_idx_l0_active > 1 ) + if( h->mb.pic.i_fref[0] > 1 ) { - bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] ); + bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] ); } x264_mb_predict_mv( h, 0, 0, 4, mvp ); bs_write_se( s, h->mb.cache.mv[0][x264_scan8[0]][0] - mvp[0] ); @@ -457,10 +463,10 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s ) else if( h->mb.i_partition == D_16x8 ) { bs_write_ue( s, 1 ); - if( h->sh.i_num_ref_idx_l0_active > 1 ) + if( h->mb.pic.i_fref[0] > 1 ) { - bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] ); - bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[8]] ); + bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] ); + bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] ); } x264_mb_predict_mv( h, 0, 0, 4, mvp ); @@ -474,10 +480,10 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s ) else if( h->mb.i_partition == D_8x16 ) { bs_write_ue( s, 2 ); - if( h->sh.i_num_ref_idx_l0_active > 1 ) + if( h->mb.pic.i_fref[0] > 1 ) { - bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] ); - bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[4]] ); + bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] ); + bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] ); } x264_mb_predict_mv( h, 0, 0, 2, mvp ); @@ -510,12 +516,12 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s ) bs_write_ue( s, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i] ] ); } /* ref0 */ - if( h->sh.i_num_ref_idx_l0_active > 1 && b_sub_ref0 ) + if( h->mb.pic.i_fref[0] > 1 && b_sub_ref0 ) { - bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[0]] ); - bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[4]] ); - bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[8]] ); - bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[12]] ); + bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[0]] ); + bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4]] ); + bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[8]] ); + bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[12]] ); } for( i = 0; i < 4; i++ ) @@ -535,14 +541,14 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s ) { if( x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i] ] ) { - bs_write_te( s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[i*4]] ); + bs_write_te( s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[i*4]] ); } } for( i = 0; i < 4; i++ ) { if( x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i] ] ) { - bs_write_te( s, h->sh.i_num_ref_idx_l1_active - 1, h->mb.cache.ref[1][x264_scan8[i*4]] ); + bs_write_te( s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[i*4]] ); } } /* mvd */ @@ -572,7 +578,7 @@ void x264_macroblock_write_cavlc( x264_t *h, bs_t *s ) for( i_list = 0; i_list < 2; i_list++ ) { - const int i_ref_max = i_list == 0 ? h->sh.i_num_ref_idx_l0_active : h->sh.i_num_ref_idx_l1_active; + const int i_ref_max = i_list == 0 ? h->mb.pic.i_fref[0] : h->mb.pic.i_fref[1]; if( i_ref_max > 1 ) { @@ -720,14 +726,14 @@ int x264_partition_size_cavlc( x264_t *h, int i8, int i_pixel ) if( i_mb_type == P_8x8 ) { bs_write_ue( &s, sub_mb_type_p_to_golomb[ h->mb.i_sub_partition[i8] ] ); - if( h->sh.i_num_ref_idx_l0_active > 1 ) - bs_write_te( &s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[4*i8]] ); + if( h->mb.pic.i_fref[0] > 1 ) + bs_write_te( &s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4*i8]] ); cavlc_mb8x8_mvd( h, &s, 0, i8 ); } else if( i_mb_type == P_L0 ) { - if( h->sh.i_num_ref_idx_l0_active > 1 ) - bs_write_te( &s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[4*i8]] ); + if( h->mb.pic.i_fref[0] > 1 ) + bs_write_te( &s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4*i8]] ); if( h->mb.i_partition == D_16x8 ) cavlc_mb_mvd( h, &s, 0, 4*i8, 4 ); else //8x16 @@ -737,12 +743,12 @@ int x264_partition_size_cavlc( x264_t *h, int i8, int i_pixel ) { bs_write_ue( &s, sub_mb_type_b_to_golomb[ h->mb.i_sub_partition[i8] ] ); - if( h->sh.i_num_ref_idx_l0_active > 1 + if( h->mb.pic.i_fref[0] > 1 && x264_mb_partition_listX_table[0][ h->mb.i_sub_partition[i8] ] ) - bs_write_te( &s, h->sh.i_num_ref_idx_l0_active - 1, h->mb.cache.ref[0][x264_scan8[4*i8]] ); - if( h->sh.i_num_ref_idx_l1_active > 1 + bs_write_te( &s, h->mb.pic.i_fref[0] - 1, h->mb.cache.ref[0][x264_scan8[4*i8]] ); + if( h->mb.pic.i_fref[1] > 1 && x264_mb_partition_listX_table[1][ h->mb.i_sub_partition[i8] ] ) - bs_write_te( &s, h->sh.i_num_ref_idx_l1_active - 1, h->mb.cache.ref[1][x264_scan8[4*i8]] ); + bs_write_te( &s, h->mb.pic.i_fref[1] - 1, h->mb.cache.ref[1][x264_scan8[4*i8]] ); cavlc_mb8x8_mvd( h, &s, 0, i8 ); cavlc_mb8x8_mvd( h, &s, 1, i8 ); diff --git a/encoder/encoder.c b/encoder/encoder.c index c5a3db0f..7f18c231 100644 --- a/encoder/encoder.c +++ b/encoder/encoder.c @@ -117,8 +117,9 @@ static void x264_slice_header_init( x264_t *h, x264_slice_header_t *sh, sh->i_frame_num = i_frame; - sh->b_field_pic = 0; /* Not field support for now */ - sh->b_bottom_field = 1; /* not yet used */ + sh->b_mbaff = h->param.b_interlaced; + sh->b_field_pic = 0; /* no field support for now */ + sh->b_bottom_field = 0; /* not yet used */ sh->i_idr_pic_id = i_idr_pic_id; @@ -188,11 +189,25 @@ static void x264_slice_header_write( bs_t *s, x264_slice_header_t *sh, int i_nal { int i; - bs_write_ue( s, sh->i_first_mb ); + if( sh->b_mbaff ) + { + assert( sh->i_first_mb % (2*sh->sps->i_mb_width) == 0 ); + bs_write_ue( s, sh->i_first_mb >> 1 ); + } + else + bs_write_ue( s, sh->i_first_mb ); + bs_write_ue( s, sh->i_type + 5 ); /* same type things */ bs_write_ue( s, sh->i_pps_id ); bs_write( s, sh->sps->i_log2_max_frame_num, sh->i_frame_num ); + if( !sh->sps->b_frame_mbs_only ) + { + bs_write1( s, sh->b_field_pic ); + if ( sh->b_field_pic ) + bs_write1( s, sh->b_bottom_field ); + } + if( sh->i_idr_pic_id >= 0 ) /* NAL IDR */ { bs_write_ue( s, sh->i_idr_pic_id ); @@ -334,7 +349,7 @@ static int x264_validate_parameters( x264_t *h ) if( h->param.i_threads == 0 ) h->param.i_threads = x264_cpu_num_processors(); h->param.i_threads = x264_clip3( h->param.i_threads, 1, X264_SLICE_MAX ); - h->param.i_threads = X264_MIN( h->param.i_threads, (h->param.i_height + 15) / 16 ); + h->param.i_threads = X264_MIN( h->param.i_threads, (h->param.i_height + 15) >> (4 + h->param.b_interlaced) ); #ifndef HAVE_PTHREAD if( h->param.i_threads > 1 ) { @@ -343,6 +358,20 @@ static int x264_validate_parameters( x264_t *h ) } #endif + if( h->param.b_interlaced ) + { + if( h->param.analyse.i_me_method == X264_ME_ESA ) + { + x264_log( h, X264_LOG_WARNING, "interlace + me=esa is not implemented\n" ); + h->param.analyse.i_me_method = X264_ME_UMH; + } + if( h->param.analyse.i_direct_mv_pred > X264_DIRECT_PRED_SPATIAL ) + { + x264_log( h, X264_LOG_WARNING, "interlace + direct=temporal is not implemented\n" ); + h->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL; + } + } + if( h->param.rc.i_rc_method < 0 || h->param.rc.i_rc_method > 2 ) { x264_log( h, X264_LOG_ERROR, "invalid RC method\n" ); @@ -458,6 +487,7 @@ static int x264_validate_parameters( x264_t *h ) #define BOOLIFY(x) h->param.x = !!h->param.x BOOLIFY( b_cabac ); BOOLIFY( b_deblocking_filter ); + BOOLIFY( b_interlaced ); BOOLIFY( analyse.b_transform_8x8 ); BOOLIFY( analyse.b_bidir_me ); BOOLIFY( analyse.b_chroma_me ); @@ -616,6 +646,7 @@ x264_t *x264_encoder_open ( x264_param_t *param ) x264_csp_init( h->param.cpu, h->param.i_csp, &h->csp ); x264_quant_init( h, h->param.cpu, &h->quantf ); x264_deblock_init( h->param.cpu, &h->loopf ); + x264_dct_init_weights(); mbcmp_init( h ); @@ -866,6 +897,8 @@ static inline void x264_reference_build_list( x264_t *h, int i_poc, int i_slice_ h->i_ref0 = X264_MIN( h->i_ref0, h->frames.i_max_ref0 ); h->i_ref0 = X264_MIN( h->i_ref0, h->param.i_frame_reference ); // if reconfig() has lowered the limit h->i_ref0 = X264_MIN( h->i_ref0, 16 - h->i_ref1 ); + h->mb.pic.i_fref[0] = h->i_ref0; + h->mb.pic.i_fref[1] = h->i_ref1; } static inline void x264_fdec_deblock( x264_t *h ) @@ -886,17 +919,20 @@ static inline void x264_reference_update( x264_t *h ) x264_fdec_deblock( h ); /* expand border */ - x264_frame_expand_border( h->fdec ); + x264_frame_expand_border( h, h->fdec ); /* create filtered images */ - x264_frame_filter( h->param.cpu, h->fdec ); + x264_frame_filter( h->param.cpu, h->fdec, h->sh.b_mbaff ); /* expand border of filtered images */ - x264_frame_expand_border_filtered( h->fdec ); + x264_frame_expand_border_filtered( h, h->fdec ); /* move lowres copy of the image to the ref frame */ for( i = 0; i < 4; i++) + { XCHG( uint8_t*, h->fdec->lowres[i], h->fenc->lowres[i] ); + XCHG( uint8_t*, h->fdec->buffer_lowres[i], h->fenc->buffer_lowres[i] ); + } /* adaptive B decision needs a pointer, since it can't use the ref lists */ if( h->sh.i_type != SLICE_TYPE_B ) @@ -988,7 +1024,7 @@ static int x264_slice_write( x264_t *h ) h->mb.i_last_qp = h->sh.i_qp; h->mb.i_last_dqp = 0; - for( mb_xy = h->sh.i_first_mb, i_skip = 0; mb_xy < h->sh.i_last_mb; mb_xy++ ) + for( mb_xy = h->sh.i_first_mb, i_skip = 0; mb_xy < h->sh.i_last_mb; ) { const int i_mb_y = mb_xy / h->sps->i_mb_width; const int i_mb_x = mb_xy % h->sps->i_mb_width; @@ -1014,7 +1050,7 @@ static int x264_slice_write( x264_t *h ) TIMER_START( i_mtime_write ); if( h->param.b_cabac ) { - if( mb_xy > h->sh.i_first_mb ) + if( mb_xy > h->sh.i_first_mb && !(h->sh.b_mbaff && (i_mb_y&1)) ) x264_cabac_encode_terminal( &h->cabac, 0 ); if( IS_SKIP( h->mb.i_type ) ) @@ -1077,6 +1113,18 @@ static int x264_slice_write( x264_t *h ) if( h->mb.b_variable_qp ) x264_ratecontrol_mb(h, bs_pos(&h->out.bs) - mb_spos); + + if( h->sh.b_mbaff ) + { + if( (i_mb_y&1) && i_mb_x == h->sps->i_mb_width - 1 ) + mb_xy++; + else if( i_mb_y&1 ) + mb_xy += 1 - h->sps->i_mb_width; + else + mb_xy += h->sps->i_mb_width; + } + else + mb_xy++; } if( h->param.b_cabac ) @@ -1136,6 +1184,8 @@ static inline int x264_slices_write( x264_t *h ) for( i = 0; i < h->param.i_threads; i++ ) { x264_t *t = h->thread[i]; + int mb_height = h->sps->i_mb_height >> h->sh.b_mbaff; + int mb_width = h->sps->i_mb_width << h->sh.b_mbaff; if( i > 0 ) { memcpy( t, h, sizeof(x264_t) ); @@ -1143,8 +1193,8 @@ static inline int x264_slices_write( x264_t *h ) bs_init( &t->out.bs, t->out.p_bitstream, i_bs_size ); t->i_thread_num = i; } - t->sh.i_first_mb = (i * h->sps->i_mb_height / h->param.i_threads) * h->sps->i_mb_width; - t->sh.i_last_mb = ((i+1) * h->sps->i_mb_height / h->param.i_threads) * h->sps->i_mb_width; + t->sh.i_first_mb = (i * mb_height / h->param.i_threads) * mb_width; + t->sh.i_last_mb = ((i+1) * mb_height / h->param.i_threads) * mb_width; t->out.i_nal = i_nal + i; } x264_ratecontrol_threads_start( h ); @@ -1233,7 +1283,8 @@ int x264_encoder_encode( x264_t *h, x264_frame_copy_picture( h, fenc, pic_in ); - if( h->param.i_width % 16 || h->param.i_height % 16 ) + if( h->param.i_width != 16 * h->sps->i_mb_width || + h->param.i_height != 16 * h->sps->i_mb_height ) x264_frame_expand_border_mod16( h, fenc ); fenc->i_frame = h->frames.i_input++; @@ -1573,7 +1624,7 @@ do_encode: { for( i = 0; i < 7; i++ ) h->stat.i_mb_count_size[h->sh.i_type][i] += h->stat.frame.i_mb_count_size[i]; - for( i = 0; i < 16; i++ ) + for( i = 0; i < 32; i++ ) h->stat.i_mb_count_ref[h->sh.i_type][i] += h->stat.frame.i_mb_count_ref[i]; } if( i_slice_type == SLICE_TYPE_B ) @@ -1808,7 +1859,7 @@ void x264_encoder_close ( x264_t *h ) char *p = buf; int64_t i_den = 0; int i_max = 0; - for( i = 0; i < h->frames.i_max_ref0; i++ ) + for( i = 0; i < h->frames.i_max_ref0 << h->param.b_interlaced; i++ ) if( h->stat.i_mb_count_ref[i_slice][i] ) { i_den += h->stat.i_mb_count_ref[i_slice][i]; diff --git a/encoder/macroblock.c b/encoder/macroblock.c index 0dab7011..63fc64c4 100644 --- a/encoder/macroblock.c +++ b/encoder/macroblock.c @@ -52,8 +52,30 @@ static const int def_quant4_mf[6][4][4] = ****************************************************************************/ #define ZIG(i,y,x) level[i] = dct[x][y]; -static inline void scan_zigzag_8x8full( int level[64], int16_t dct[8][8] ) + +static inline void scan_zigzag_8x8full( int level[64], int16_t dct[8][8], int b_interlaced ) { + if( b_interlaced ) + { + ZIG( 0,0,0) ZIG( 1,1,0) ZIG( 2,2,0) ZIG( 3,0,1) + ZIG( 4,1,1) ZIG( 5,3,0) ZIG( 6,4,0) ZIG( 7,2,1) + ZIG( 8,0,2) ZIG( 9,3,1) ZIG(10,5,0) ZIG(11,6,0) + ZIG(12,7,0) ZIG(13,4,1) ZIG(14,1,2) ZIG(15,0,3) + ZIG(16,2,2) ZIG(17,5,1) ZIG(18,6,1) ZIG(19,7,1) + ZIG(20,3,2) ZIG(21,1,3) ZIG(22,0,4) ZIG(23,2,3) + ZIG(24,4,2) ZIG(25,5,2) ZIG(26,6,2) ZIG(27,7,2) + ZIG(28,3,3) ZIG(29,1,4) ZIG(30,0,5) ZIG(31,2,4) + ZIG(32,4,3) ZIG(33,5,3) ZIG(34,6,3) ZIG(35,7,3) + ZIG(36,3,4) ZIG(37,1,5) ZIG(38,0,6) ZIG(39,2,5) + ZIG(40,4,4) ZIG(41,5,4) ZIG(42,6,4) ZIG(43,7,4) + ZIG(44,3,5) ZIG(45,1,6) ZIG(46,2,6) ZIG(47,4,5) + ZIG(48,5,5) ZIG(49,6,5) ZIG(50,7,5) ZIG(51,3,6) + ZIG(52,0,7) ZIG(53,1,7) ZIG(54,4,6) ZIG(55,5,6) + ZIG(56,6,6) ZIG(57,7,6) ZIG(58,2,7) ZIG(59,3,7) + ZIG(60,4,7) ZIG(61,5,7) ZIG(62,6,7) ZIG(63,7,7) + } + else + { ZIG( 0,0,0) ZIG( 1,0,1) ZIG( 2,1,0) ZIG( 3,2,0) ZIG( 4,1,1) ZIG( 5,0,2) ZIG( 6,0,3) ZIG( 7,1,2) ZIG( 8,2,1) ZIG( 9,3,0) ZIG(10,4,0) ZIG(11,3,1) @@ -70,21 +92,45 @@ static inline void scan_zigzag_8x8full( int level[64], int16_t dct[8][8] ) ZIG(52,4,6) ZIG(53,3,7) ZIG(54,4,7) ZIG(55,5,6) ZIG(56,6,5) ZIG(57,7,4) ZIG(58,7,5) ZIG(59,6,6) ZIG(60,5,7) ZIG(61,6,7) ZIG(62,7,6) ZIG(63,7,7) + } } -static inline void scan_zigzag_4x4full( int level[16], int16_t dct[4][4] ) + +static inline void scan_zigzag_4x4full( int level[16], int16_t dct[4][4], int b_interlaced ) { + if( b_interlaced ) + { + ZIG( 0,0,0) ZIG( 1,1,0) ZIG( 2,0,1) ZIG( 3,2,0) + ZIG( 4,3,0) ZIG( 5,1,1) ZIG( 6,2,1) ZIG( 7,3,1) + ZIG( 8,0,2) ZIG( 9,1,2) ZIG(10,2,2) ZIG(11,3,2) + ZIG(12,0,3) ZIG(13,1,3) ZIG(14,2,3) ZIG(15,3,3) + } + else + { ZIG( 0,0,0) ZIG( 1,0,1) ZIG( 2,1,0) ZIG( 3,2,0) ZIG( 4,1,1) ZIG( 5,0,2) ZIG( 6,0,3) ZIG( 7,1,2) ZIG( 8,2,1) ZIG( 9,3,0) ZIG(10,3,1) ZIG(11,2,2) ZIG(12,1,3) ZIG(13,2,3) ZIG(14,3,2) ZIG(15,3,3) + } } -static inline void scan_zigzag_4x4( int level[15], int16_t dct[4][4] ) + +static inline void scan_zigzag_4x4( int level[15], int16_t dct[4][4], int b_interlaced ) { + if( b_interlaced ) + { + ZIG( 0,1,0) ZIG( 1,0,1) ZIG( 2,2,0) + ZIG( 3,3,0) ZIG( 4,1,1) ZIG( 5,2,1) ZIG( 6,3,1) + ZIG( 7,0,2) ZIG( 8,1,2) ZIG( 9,2,2) ZIG(10,3,2) + ZIG(11,0,3) ZIG(12,1,3) ZIG(13,2,3) ZIG(14,3,3) + } + else + { ZIG( 0,0,1) ZIG( 1,1,0) ZIG( 2,2,0) ZIG( 3,1,1) ZIG( 4,0,2) ZIG( 5,0,3) ZIG( 6,1,2) ZIG( 7,2,1) ZIG( 8,3,0) ZIG( 9,3,1) ZIG(10,2,2) ZIG(11,1,3) ZIG(12,2,3) ZIG(13,3,2) ZIG(14,3,3) + } } + static inline void scan_zigzag_2x2_dc( int level[4], int16_t dct[2][2] ) { ZIG(0,0,0) @@ -100,19 +146,41 @@ static inline void scan_zigzag_2x2_dc( int level[4], int16_t dct[2][2] ) level[i] = p_src[oe] - p_dst[od];\ p_dst[od] = p_src[oe];\ } -static inline void sub_zigzag_4x4full( int level[16], const uint8_t *p_src, uint8_t *p_dst ) + +static inline void sub_zigzag_4x4full( int level[16], const uint8_t *p_src, uint8_t *p_dst, int b_interlaced ) { + if( b_interlaced ) + { + ZIG( 0,0,0) ZIG( 1,1,0) ZIG( 2,0,1) ZIG( 3,2,0) + ZIG( 4,3,0) ZIG( 5,1,1) ZIG( 6,2,1) ZIG( 7,3,1) + ZIG( 8,0,2) ZIG( 9,1,2) ZIG(10,2,2) ZIG(11,3,2) + ZIG(12,0,3) ZIG(13,1,3) ZIG(14,2,3) ZIG(15,3,3) + } + else + { ZIG( 0,0,0) ZIG( 1,0,1) ZIG( 2,1,0) ZIG( 3,2,0) ZIG( 4,1,1) ZIG( 5,0,2) ZIG( 6,0,3) ZIG( 7,1,2) ZIG( 8,2,1) ZIG( 9,3,0) ZIG(10,3,1) ZIG(11,2,2) ZIG(12,1,3) ZIG(13,2,3) ZIG(14,3,2) ZIG(15,3,3) + } } -static inline void sub_zigzag_4x4( int level[15], const uint8_t *p_src, uint8_t *p_dst ) + +static inline void sub_zigzag_4x4( int level[15], const uint8_t *p_src, uint8_t *p_dst, int b_interlaced ) { + if( b_interlaced ) + { + ZIG( 0,1,0) ZIG( 1,0,1) ZIG( 2,2,0) + ZIG( 3,3,0) ZIG( 4,1,1) ZIG( 5,2,1) ZIG( 6,3,1) + ZIG( 7,0,2) ZIG( 8,1,2) ZIG( 9,2,2) ZIG(10,3,2) + ZIG(11,0,3) ZIG(12,1,3) ZIG(13,2,3) ZIG(14,3,3) + } + else + { ZIG( 0,0,1) ZIG( 1,1,0) ZIG( 2,2,0) ZIG( 3,1,1) ZIG( 4,0,2) ZIG( 5,0,3) ZIG( 6,1,2) ZIG( 7,2,1) ZIG( 8,3,0) ZIG( 9,3,1) ZIG(10,2,2) ZIG(11,1,3) ZIG(12,2,3) ZIG(13,3,2) ZIG(14,3,3) + } } #undef ZIG @@ -199,7 +267,7 @@ void x264_mb_encode_i4x4( x264_t *h, int idx, int i_qscale ) if( h->mb.b_lossless ) { - sub_zigzag_4x4full( h->dct.block[idx].luma4x4, p_src, p_dst ); + sub_zigzag_4x4full( h->dct.block[idx].luma4x4, p_src, p_dst, h->mb.b_interlaced ); return; } @@ -210,7 +278,7 @@ void x264_mb_encode_i4x4( x264_t *h, int idx, int i_qscale ) else quant_4x4( h, dct4x4, h->quant4_mf[CQM_4IY], i_qscale, 1 ); - scan_zigzag_4x4full( h->dct.block[idx].luma4x4, dct4x4 ); + scan_zigzag_4x4full( h->dct.block[idx].luma4x4, dct4x4, h->mb.b_interlaced ); h->quantf.dequant_4x4( dct4x4, h->dequant4_mf[CQM_4IY], i_qscale ); /* output samples to fdec */ @@ -232,7 +300,7 @@ void x264_mb_encode_i8x8( x264_t *h, int idx, int i_qscale ) else quant_8x8( h, dct8x8, h->quant8_mf[CQM_8IY], i_qscale, 1 ); - scan_zigzag_8x8full( h->dct.luma8x8[idx], dct8x8 ); + scan_zigzag_8x8full( h->dct.luma8x8[idx], dct8x8, h->mb.b_interlaced ); h->quantf.dequant_8x8( dct8x8, h->dequant8_mf[CQM_8IY], i_qscale ); h->dctf.add8x8_idct8( p_dst, dct8x8 ); } @@ -252,11 +320,11 @@ static void x264_mb_encode_i16x16( x264_t *h, int i_qscale ) { int oe = block_idx_x[i]*4 + block_idx_y[i]*4*FENC_STRIDE; int od = block_idx_x[i]*4 + block_idx_y[i]*4*FDEC_STRIDE; - sub_zigzag_4x4( h->dct.block[i].residual_ac, p_src+oe, p_dst+od ); + sub_zigzag_4x4( h->dct.block[i].residual_ac, p_src+oe, p_dst+od, h->mb.b_interlaced ); dct4x4[0][block_idx_x[i]][block_idx_y[i]] = p_src[oe] - p_dst[od]; p_dst[od] = p_src[oe]; } - scan_zigzag_4x4full( h->dct.luma16x16_dc, dct4x4[0] ); + scan_zigzag_4x4full( h->dct.luma16x16_dc, dct4x4[0], h->mb.b_interlaced ); return; } @@ -272,13 +340,13 @@ static void x264_mb_encode_i16x16( x264_t *h, int i_qscale ) else quant_4x4( h, dct4x4[1+i], h->quant4_mf[CQM_4IY], i_qscale, 1 ); - scan_zigzag_4x4( h->dct.block[i].residual_ac, dct4x4[1+i] ); + scan_zigzag_4x4( h->dct.block[i].residual_ac, dct4x4[1+i], h->mb.b_interlaced ); h->quantf.dequant_4x4( dct4x4[1+i], h->dequant4_mf[CQM_4IY], i_qscale ); } h->dctf.dct4x4dc( dct4x4[0] ); quant_4x4_dc( h, dct4x4[0], h->quant4_mf[CQM_4IY], i_qscale ); - scan_zigzag_4x4full( h->dct.luma16x16_dc, dct4x4[0] ); + scan_zigzag_4x4full( h->dct.luma16x16_dc, dct4x4[0], h->mb.b_interlaced ); /* output samples to fdec */ h->dctf.idct4x4dc( dct4x4[0] ); @@ -314,7 +382,7 @@ static void x264_mb_encode_8x8_chroma( x264_t *h, int b_inter, int i_qscale ) { int oe = block_idx_x[i]*4 + block_idx_y[i]*4*FENC_STRIDE; int od = block_idx_x[i]*4 + block_idx_y[i]*4*FDEC_STRIDE; - sub_zigzag_4x4( h->dct.block[16+i+ch*4].residual_ac, p_src+oe, p_dst+od ); + sub_zigzag_4x4( h->dct.block[16+i+ch*4].residual_ac, p_src+oe, p_dst+od, h->mb.b_interlaced ); h->dct.chroma_dc[ch][i] = p_src[oe] - p_dst[od]; p_dst[od] = p_src[oe]; } @@ -330,7 +398,7 @@ static void x264_mb_encode_8x8_chroma( x264_t *h, int b_inter, int i_qscale ) /* no trellis; it doesn't seem to help chroma noticeably */ quant_4x4( h, dct4x4[i], h->quant4_mf[CQM_4IC + b_inter], i_qscale, !b_inter ); - scan_zigzag_4x4( h->dct.block[16+i+ch*4].residual_ac, dct4x4[i] ); + scan_zigzag_4x4( h->dct.block[16+i+ch*4].residual_ac, dct4x4[i], h->mb.b_interlaced ); if( b_decimate ) { @@ -417,8 +485,26 @@ void x264_macroblock_encode( x264_t *h ) int i_cbp_dc = 0; int i_qp = h->mb.i_qp; int b_decimate = h->sh.i_type == SLICE_TYPE_B || h->param.analyse.b_dct_decimate; + int b_force_no_skip = 0; int i; + if( h->sh.b_mbaff + && h->mb.i_mb_xy == h->sh.i_first_mb + h->mb.i_mb_stride + && IS_SKIP(h->mb.type[h->sh.i_first_mb]) ) + { + /* The first skip is predicted to be a frame mb pair. + * We don't yet support the aff part of mbaff, so force it to non-skip + * so that we can pick the aff flag. */ + b_force_no_skip = 1; + if( IS_SKIP(h->mb.i_type) ) + { + if( h->mb.i_type == P_SKIP ) + h->mb.i_type = P_L0; + else if( h->mb.i_type == B_SKIP ) + h->mb.i_type = B_DIRECT; + } + } + if( h->mb.i_type == P_SKIP ) { /* A bit special */ @@ -489,7 +575,8 @@ void x264_macroblock_encode( x264_t *h ) int y = 4*block_idx_y[i4x4]; sub_zigzag_4x4full( h->dct.block[i4x4].luma4x4, h->mb.pic.p_fenc[0]+x+y*FENC_STRIDE, - h->mb.pic.p_fdec[0]+x+y*FDEC_STRIDE ); + h->mb.pic.p_fdec[0]+x+y*FDEC_STRIDE, + h->mb.b_interlaced ); } } else if( h->mb.b_transform_8x8 ) @@ -508,7 +595,7 @@ void x264_macroblock_encode( x264_t *h ) else quant_8x8( h, dct8x8[idx], h->quant8_mf[CQM_8PY], i_qp, 0 ); - scan_zigzag_8x8full( h->dct.luma8x8[idx], dct8x8[idx] ); + scan_zigzag_8x8full( h->dct.luma8x8[idx], dct8x8[idx], h->mb.b_interlaced ); if( b_decimate ) { @@ -560,7 +647,7 @@ void x264_macroblock_encode( x264_t *h ) else quant_4x4( h, dct4x4[idx], h->quant4_mf[CQM_4PY], i_qp, 0 ); - scan_zigzag_4x4full( h->dct.block[idx].luma4x4, dct4x4[idx] ); + scan_zigzag_4x4full( h->dct.block[idx].luma4x4, dct4x4[idx], h->mb.b_interlaced ); if( b_decimate ) i_decimate_8x8 += x264_mb_decimate_score( h->dct.block[idx].luma4x4, 16 ); @@ -669,25 +756,28 @@ void x264_macroblock_encode( x264_t *h ) /* Check for P_SKIP * XXX: in the me perhaps we should take x264_mb_predict_mv_pskip into account * (if multiple mv give same result)*/ - if( h->mb.i_type == P_L0 && h->mb.i_partition == D_16x16 && - h->mb.i_cbp_luma == 0x00 && h->mb.i_cbp_chroma== 0x00 && - h->mb.cache.ref[0][x264_scan8[0]] == 0 ) + if( !b_force_no_skip ) { - int mvp[2]; - - x264_mb_predict_mv_pskip( h, mvp ); - if( h->mb.cache.mv[0][x264_scan8[0]][0] == mvp[0] && - h->mb.cache.mv[0][x264_scan8[0]][1] == mvp[1] ) + if( h->mb.i_type == P_L0 && h->mb.i_partition == D_16x16 && + h->mb.i_cbp_luma == 0x00 && h->mb.i_cbp_chroma== 0x00 && + h->mb.cache.ref[0][x264_scan8[0]] == 0 ) { - h->mb.i_type = P_SKIP; + int mvp[2]; + + x264_mb_predict_mv_pskip( h, mvp ); + if( h->mb.cache.mv[0][x264_scan8[0]][0] == mvp[0] && + h->mb.cache.mv[0][x264_scan8[0]][1] == mvp[1] ) + { + h->mb.i_type = P_SKIP; + } } - } - /* Check for B_SKIP */ - if( h->mb.i_type == B_DIRECT && - h->mb.i_cbp_luma == 0x00 && h->mb.i_cbp_chroma== 0x00 ) - { - h->mb.i_type = B_SKIP; + /* Check for B_SKIP */ + if( h->mb.i_type == B_DIRECT && + h->mb.i_cbp_luma == 0x00 && h->mb.i_cbp_chroma== 0x00 ) + { + h->mb.i_type = B_SKIP; + } } } @@ -734,7 +824,7 @@ int x264_macroblock_probe_skip( x264_t *h, int b_bidir ) const int idx = i8x8 * 4 + i4x4; quant_4x4( h, dct4x4[idx], (int(*)[4][4])def_quant4_mf, i_qp, 0 ); - scan_zigzag_4x4full( dctscan, dct4x4[idx] ); + scan_zigzag_4x4full( dctscan, dct4x4[idx], h->mb.b_interlaced ); i_decimate_mb += x264_mb_decimate_score( dctscan, 16 ); @@ -780,7 +870,7 @@ int x264_macroblock_probe_skip( x264_t *h, int b_bidir ) for( i4x4 = 0, i_decimate_mb = 0; i4x4 < 4; i4x4++ ) { quant_4x4( h, dct4x4[i4x4], (int(*)[4][4])def_quant4_mf, i_qp, 0 ); - scan_zigzag_4x4( dctscan, dct4x4[i4x4] ); + scan_zigzag_4x4( dctscan, dct4x4[i4x4], h->mb.b_interlaced ); i_decimate_mb += x264_mb_decimate_score( dctscan, 15 ); if( i_decimate_mb >= 7 ) @@ -872,7 +962,7 @@ void x264_macroblock_encode_p8x8( x264_t *h, int i8 ) int16_t dct8x8[8][8]; h->dctf.sub8x8_dct8( dct8x8, p_fenc, p_fdec ); quant_8x8( h, dct8x8, h->quant8_mf[CQM_8PY], i_qp, 0 ); - scan_zigzag_8x8full( h->dct.luma8x8[i8], dct8x8 ); + scan_zigzag_8x8full( h->dct.luma8x8[i8], dct8x8, h->mb.b_interlaced ); if( b_decimate ) nnz8x8 = 4 <= x264_mb_decimate_score( h->dct.luma8x8[i8], 64 ); @@ -895,7 +985,7 @@ void x264_macroblock_encode_p8x8( x264_t *h, int i8 ) quant_4x4( h, dct4x4[2], h->quant4_mf[CQM_4PY], i_qp, 0 ); quant_4x4( h, dct4x4[3], h->quant4_mf[CQM_4PY], i_qp, 0 ); for( i4 = 0; i4 < 4; i4++ ) - scan_zigzag_4x4full( h->dct.block[i8*4+i4].luma4x4, dct4x4[i4] ); + scan_zigzag_4x4full( h->dct.block[i8*4+i4].luma4x4, dct4x4[i4], h->mb.b_interlaced ); if( b_decimate ) { @@ -925,7 +1015,7 @@ void x264_macroblock_encode_p8x8( x264_t *h, int i8 ) h->dctf.sub4x4_dct( dct4x4, p_fenc, p_fdec ); quant_4x4( h, dct4x4, h->quant4_mf[CQM_4PC], i_qp, 0 ); - scan_zigzag_4x4( h->dct.block[16+i8+ch*4].residual_ac, dct4x4 ); + scan_zigzag_4x4( h->dct.block[16+i8+ch*4].residual_ac, dct4x4, h->mb.b_interlaced ); if( array_non_zero( (int*)dct4x4, sizeof(dct4x4)/sizeof(int) ) ) { h->quantf.dequant_4x4( dct4x4, h->dequant4_mf[CQM_4PC], i_qp ); diff --git a/encoder/rdo.c b/encoder/rdo.c index b45c1c7b..a108d643 100644 --- a/encoder/rdo.c +++ b/encoder/rdo.c @@ -268,6 +268,7 @@ static void quant_trellis_cabac( x264_t *h, int16_t *dct, trellis_node_t *bnode; uint8_t cabac_state_sig[64]; uint8_t cabac_state_last[64]; + const int b_interlaced = h->mb.b_interlaced; const int f = 1 << (i_qbits-1); // no deadzone int i_last_nnz = -1; int i, j; @@ -315,18 +316,18 @@ static void quant_trellis_cabac( x264_t *h, int16_t *dct, if( i_coefs == 64 ) { - const uint8_t *ctx_sig = &h->cabac.state[ significant_coeff_flag_offset[i_ctxBlockCat] ]; - const uint8_t *ctx_last = &h->cabac.state[ last_coeff_flag_offset[i_ctxBlockCat] ]; + const uint8_t *ctx_sig = &h->cabac.state[ significant_coeff_flag_offset[b_interlaced][i_ctxBlockCat] ]; + const uint8_t *ctx_last = &h->cabac.state[ last_coeff_flag_offset[b_interlaced][i_ctxBlockCat] ]; for( i = 0; i < 63; i++ ) { - cabac_state_sig[i] = ctx_sig[ significant_coeff_flag_offset_8x8[i] ]; + cabac_state_sig[i] = ctx_sig[ significant_coeff_flag_offset_8x8[b_interlaced][i] ]; cabac_state_last[i] = ctx_last[ last_coeff_flag_offset_8x8[i] ]; } } else { - memcpy( cabac_state_sig, &h->cabac.state[ significant_coeff_flag_offset[i_ctxBlockCat] ], 15 ); - memcpy( cabac_state_last, &h->cabac.state[ last_coeff_flag_offset[i_ctxBlockCat] ], 15 ); + memcpy( cabac_state_sig, &h->cabac.state[ significant_coeff_flag_offset[b_interlaced][i_ctxBlockCat] ], 15 ); + memcpy( cabac_state_last, &h->cabac.state[ last_coeff_flag_offset[b_interlaced][i_ctxBlockCat] ], 15 ); } memcpy( nodes_cur[0].cabac_state, &h->cabac.state[ coeff_abs_level_m1_offset[i_ctxBlockCat] ], 10 ); @@ -463,7 +464,8 @@ void x264_quant_4x4_trellis( x264_t *h, int16_t dct[4][4], int i_quant_cat, quant_trellis_cabac( h, (int16_t*)dct, (int*)h->quant4_mf[i_quant_cat][i_mf], h->unquant4_mf[i_quant_cat][i_qp], - x264_dct4_weight2_zigzag, x264_zigzag_scan4, + x264_dct4_weight2_zigzag[h->mb.b_interlaced], + x264_zigzag_scan4[h->mb.b_interlaced], i_ctxBlockCat, 15+i_qbits, i_lambda2, b_ac, 16 ); } @@ -479,7 +481,8 @@ void x264_quant_8x8_trellis( x264_t *h, int16_t dct[8][8], int i_quant_cat, quant_trellis_cabac( h, (int16_t*)dct, (int*)h->quant8_mf[i_quant_cat][i_mf], h->unquant8_mf[i_quant_cat][i_qp], - x264_dct8_weight2_zigzag, x264_zigzag_scan8, + x264_dct8_weight2_zigzag[h->mb.b_interlaced], + x264_zigzag_scan8[h->mb.b_interlaced], DCT_LUMA_8x8, 16+i_qbits, i_lambda2, 0, 64 ); } diff --git a/encoder/set.c b/encoder/set.c index 5f9ea1a2..a6b3efd7 100644 --- a/encoder/set.c +++ b/encoder/set.c @@ -41,7 +41,7 @@ static void transpose( uint8_t *buf, int w ) static void scaling_list_write( bs_t *s, x264_pps_t *pps, int idx ) { const int len = idx<4 ? 16 : 64; - const int *zigzag = idx<4 ? x264_zigzag_scan4 : x264_zigzag_scan8; + const int *zigzag = idx<4 ? x264_zigzag_scan4[0] : x264_zigzag_scan8[0]; const uint8_t *list = pps->scaling_list[idx]; const uint8_t *def_list = (idx==CQM_4IC) ? pps->scaling_list[CQM_4IY] : (idx==CQM_4PC) ? pps->scaling_list[CQM_4PY] @@ -127,11 +127,13 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param ) sps->b_gaps_in_frame_num_value_allowed = 0; sps->i_mb_width = ( param->i_width + 15 ) / 16; - sps->i_mb_height= ( param->i_height + 15 )/ 16; - sps->b_frame_mbs_only = 1; - sps->b_mb_adaptive_frame_field = 0; + sps->i_mb_height= ( param->i_height + 15 ) / 16; + if( param->b_interlaced ) + sps->i_mb_height = ( sps->i_mb_height + 1 ) & ~1; + sps->b_frame_mbs_only = ! param->b_interlaced; + sps->b_mb_adaptive_frame_field = param->b_interlaced; sps->b_direct8x8_inference = 0; - if( sps->b_frame_mbs_only == 0 || + if( ! sps->b_frame_mbs_only || !(param->analyse.inter & X264_ANALYSE_PSUB8x8) ) { sps->b_direct8x8_inference = 1; @@ -139,8 +141,8 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param ) sps->crop.i_left = 0; sps->crop.i_top = 0; - sps->crop.i_right = (- param->i_width) & 15; - sps->crop.i_bottom = (- param->i_height) & 15; + sps->crop.i_right = sps->i_mb_width*16 - param->i_width; + sps->crop.i_bottom = (sps->i_mb_height*16 - param->i_height) >> param->b_interlaced; sps->b_crop = sps->crop.i_left || sps->crop.i_top || sps->crop.i_right || sps->crop.i_bottom; @@ -258,7 +260,14 @@ void x264_sps_write( bs_t *s, x264_sps_t *sps ) bs_write_ue( s, sps->i_num_ref_frames ); bs_write( s, 1, sps->b_gaps_in_frame_num_value_allowed ); bs_write_ue( s, sps->i_mb_width - 1 ); - bs_write_ue( s, sps->i_mb_height - 1); + if (sps->b_frame_mbs_only) + { + bs_write_ue( s, sps->i_mb_height - 1); + } + else // interlaced + { + bs_write_ue( s, sps->i_mb_height/2 - 1); + } bs_write( s, 1, sps->b_frame_mbs_only ); if( !sps->b_frame_mbs_only ) { diff --git a/x264.c b/x264.c index 4d073500..67fc060a 100644 --- a/x264.c +++ b/x264.c @@ -165,6 +165,7 @@ static void Help( x264_param_t *defaults, int b_longhelp ) H1( " --nf Disable loop filter\n" ); H0( " -f, --filter Loop filter AlphaC0 and Beta parameters [%d:%d]\n", defaults->i_deblocking_filter_alphac0, defaults->i_deblocking_filter_beta ); + H0( " --interlaced Enable pure-interlaced mode\n" ); H0( "\n" ); H0( "Ratecontrol:\n" ); H0( "\n" ); @@ -354,6 +355,7 @@ static int Parse( int argc, char **argv, { "scenecut",required_argument, NULL, 0 }, { "nf", no_argument, NULL, 0 }, { "filter", required_argument, NULL, 'f' }, + { "interlaced", no_argument, NULL, 0 }, { "no-cabac",no_argument, NULL, 0 }, { "qp", required_argument, NULL, 'q' }, { "qpmin", required_argument, NULL, 0 }, diff --git a/x264.h b/x264.h index fbcf9318..82e56921 100644 --- a/x264.h +++ b/x264.h @@ -177,6 +177,8 @@ typedef struct int b_cabac; int i_cabac_init_idc; + int b_interlaced; + int i_cqm_preset; char *psz_cqm_file; /* JM format */ uint8_t cqm_4iy[16]; /* used only if i_cqm_preset == X264_CQM_CUSTOM */ -- 2.47.3