Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: encoder/macroblock.c
- ===================================================================
- --- encoder/macroblock.c (revision 736)
- +++ encoder/macroblock.c (working copy)
- @@ -96,7 +96,10 @@
- h->dctf.sub4x4_dct( dct4x4, p_src, p_dst );
- if( h->mb.b_trellis )
- + {
- x264_quant_4x4_trellis( h, dct4x4, CQM_4IY, i_qscale, DCT_LUMA_4x4, 1 );
- + x264_quant_4x4_qns( h, dct4x4, i_qscale, idx, 1, DCT_LUMA_4x4 );
- + }
- else
- h->quantf.quant_4x4( dct4x4, h->quant4_mf[CQM_4IY][i_qscale], h->quant4_bias[CQM_4IY][i_qscale] );
- @@ -118,7 +121,10 @@
- h->dctf.sub8x8_dct8( dct8x8, p_src, p_dst );
- if( h->mb.b_trellis )
- + {
- x264_quant_8x8_trellis( h, dct8x8, CQM_8IY, i_qscale, 1 );
- + x264_quant_8x8_qns( h, dct8x8, i_qscale, idx, 1 );
- + }
- else
- h->quantf.quant_8x8( dct8x8, h->quant8_mf[CQM_8IY][i_qscale], h->quant8_bias[CQM_8IY][i_qscale] );
- @@ -158,7 +164,10 @@
- /* quant/scan/dequant */
- if( h->mb.b_trellis )
- + {
- x264_quant_4x4_trellis( h, dct4x4[1+i], CQM_4IY, i_qscale, DCT_LUMA_AC, 1 );
- + x264_quant_4x4_qns( h, dct4x4[1+i], i_qscale, i, 1, DCT_LUMA_AC );
- + }
- else
- h->quantf.quant_4x4( dct4x4[1+i], h->quant4_mf[CQM_4IY][i_qscale], h->quant4_bias[CQM_4IY][i_qscale] );
- @@ -425,7 +434,10 @@
- if( h->mb.b_noise_reduction )
- x264_denoise_dct( h, (int16_t*)dct8x8[idx] );
- if( h->mb.b_trellis )
- + {
- x264_quant_8x8_trellis( h, dct8x8[idx], CQM_8PY, i_qp, 0 );
- + x264_quant_8x8_qns( h, dct8x8[idx], i_qp, idx, 0 );
- + }
- else
- h->quantf.quant_8x8( dct8x8[idx], h->quant8_mf[CQM_8PY][i_qp], h->quant8_bias[CQM_8PY][i_qp] );
- @@ -477,7 +489,10 @@
- if( h->mb.b_noise_reduction )
- x264_denoise_dct( h, (int16_t*)dct4x4[idx] );
- if( h->mb.b_trellis )
- + {
- x264_quant_4x4_trellis( h, dct4x4[idx], CQM_4PY, i_qp, DCT_LUMA_4x4, 0 );
- + x264_quant_4x4_qns( h, dct4x4[idx], i_qp, idx, 0, DCT_LUMA_4x4 );
- + }
- else
- h->quantf.quant_4x4( dct4x4[idx], h->quant4_mf[CQM_4PY][i_qp], h->quant4_bias[CQM_4PY][i_qp] );
- Index: encoder/rdo.c
- ===================================================================
- --- encoder/rdo.c (revision 736)
- +++ encoder/rdo.c (working copy)
- @@ -503,7 +503,183 @@
- }
- }
- +inline int variance( uint8_t *pix, int x, int y, int start )
- +{
- + uint8_t *p_src = pix + (x + start) + FENC_STRIDE * (y + start);
- + int sum = 0; int ssd = 0; int i,j;
- + for(i = 0; i < 3; i++)
- + for(j = 0; j < 3; j++)
- + {
- + int cur = p_src[j+i*FENC_STRIDE];
- + sum += cur; ssd += cur*cur;
- + }
- + return (9*ssd - (sum * sum));
- +}
- +uint64_t weighted_error8( x264_t *h, int16_t dct[8][8], uint16_t inv_variance[8][8], int i_qp, int idx, int i_quant_cat)
- +{
- + uint8_t idct[8*FDEC_STRIDE];
- + int16_t ndct[8][8];
- + int i,j;
- + uint8_t *decpix = h->mb.pic.p_fdec[0]+(8*(idx&1))+FDEC_STRIDE*(((idx>>1)&1)<<3);
- + h->mc.copy[PIXEL_8x8]( idct, FDEC_STRIDE, decpix, FDEC_STRIDE, 8 );
- + memcpy( ndct, dct, sizeof(ndct) );
- + h->zigzagf.scan_8x8( h->dct.luma8x8[idx], dct );
- + h->quantf.dequant_8x8( ndct, h->dequant8_mf[CQM_8PY], i_qp );
- + h->dctf.add8x8_idct8( idct, ndct );
- + uint64_t error = 0;
- + uint8_t *pix = h->mb.pic.p_fenc[0]+(8*(idx&1))+FENC_STRIDE*(((idx>>1)&1)<<3);
- + for( i = 0; i < 8; i++ )
- + for( j = 0; j < 8; j++ )
- + {
- + int pix_error = idct[i+j*FDEC_STRIDE] - pix[i+j*FENC_STRIDE];
- + error += (pix_error*pix_error)*inv_variance[j][i];
- + }
- + x264_cabac_t cabac_tmp = h->cabac;
- + block_residual_write_cabac(h,&cabac_tmp,DCT_LUMA_8x8,idx,h->dct.luma8x8[idx],64);
- + error *= 38;
- + uint64_t bits = cabac_tmp.f8_bits_encoded;
- + bits *= lambda2_tab[i_quant_cat][i_qp];
- + error += (bits + 8) >> 4;
- + return error;
- +}
- +
- +uint64_t weighted_error4( x264_t *h, int16_t dct[4][4], uint16_t inv_variance[4][4], int i_qp, int idx, int i_quant_cat, int i_ctxBlockCat )
- +{
- + uint8_t idct[4*FDEC_STRIDE];
- + int16_t ndct[4][4];
- + int i,j;
- + uint8_t *decpix = h->mb.pic.p_fdec[0] + 4*block_idx_x[idx] + 4*FDEC_STRIDE*block_idx_y[idx];
- + h->mc.copy[PIXEL_4x4]( idct, FDEC_STRIDE, decpix, FDEC_STRIDE, 4 );
- + memcpy( ndct, dct, sizeof(ndct) );
- + h->zigzagf.scan_4x4( h->dct.block[idx].luma4x4, dct );
- + h->quantf.dequant_4x4( ndct, h->dequant4_mf[CQM_4PY], i_qp );
- + if( i_ctxBlockCat == DCT_LUMA_AC )
- + ndct[0][0] = dct[0][0];
- + h->dctf.add4x4_idct( idct, ndct );
- + uint64_t error = 0;
- + uint8_t *pix = h->mb.pic.p_fenc[0] + 4*block_idx_x[idx] + 4*FENC_STRIDE*block_idx_y[idx];
- + for( i = 0; i < 4; i++ )
- + for( j = 0; j < 4; j++ )
- + {
- + int pix_error = idct[i+j*FDEC_STRIDE] - pix[i+j*FENC_STRIDE];
- + error += ((pix_error*pix_error)*inv_variance[j][i]);
- + }
- + x264_cabac_t cabac_tmp = h->cabac;
- + block_residual_write_cabac(h,&cabac_tmp,i_ctxBlockCat,idx,h->dct.block[idx].luma4x4,16 - (i_ctxBlockCat == DCT_LUMA_AC));
- + error *= 38;
- + uint64_t bits = cabac_tmp.f8_bits_encoded;
- + bits *= lambda2_tab[i_quant_cat][i_qp];
- + error += (bits + 8) >> 4;
- + return error;
- +}
- +
- +void x264_quant_8x8_qns( x264_t *h, int16_t dct[8][8], int i_qp, int idx, int i_quant_cat )
- +{
- + int x,y,start;
- + uint16_t inv_variance[8][8];
- + int size = 8;
- + uint8_t *pix = h->mb.pic.p_fenc[0]+(8*(idx&1))+FENC_STRIDE*(((idx>>1)&1)<<3);
- + for(x = 0; x < size; x++)
- + for(y = 0; y < size; y++)
- + {
- + if(x == 0 || y == 0) start = 0;
- + else if(x == size - 1 || y == size - 1) start = -2;
- + else start = -1;
- + inv_variance[y][x] = 256 + 65536 / (variance(pix,x,y,start) + 180);
- + inv_variance[y][x] = 800 - inv_variance[y][x];
- + }
- + int i;
- + int runs = 0;
- + int changed[64];
- + for( ; runs < 1000; runs++)
- + {
- + int best_change_i = 0;
- + int64_t best_change = 0;
- + int best_change_amount = 0;
- + uint64_t orig_error = weighted_error8(h,dct,inv_variance,i_qp,idx,i_quant_cat);
- + for( i = 0; i < 64; i++ )
- + {
- + // if(changed[i]) continue;
- + int change = -1;
- + for(; change <= 1; change += 2)
- + {
- + dct[0][i] += change;
- + uint64_t new_error = weighted_error8(h,dct,inv_variance,i_qp,idx,i_quant_cat);
- + int64_t cur_change = orig_error - new_error;
- + if(cur_change > best_change)
- + {
- + dct[0][i] -= change;
- + best_change = cur_change;
- + best_change_i = i;
- + best_change_amount = change;
- + break;
- + }
- + dct[0][i] -= change;
- + }
- + }
- + if(best_change == 0) break;
- + else
- + {
- + changed[best_change_i] = 1;
- + dct[0][best_change_i] += best_change_amount;
- + }
- + }
- +}
- +
- +void x264_quant_4x4_qns( x264_t *h, int16_t dct[4][4], int i_qp, int idx, int i_quant_cat, int i_ctxBlockCat )
- +{
- + int x,y,start;
- + uint16_t inv_variance[4][4];
- + int size = 4;
- + uint8_t *pix = h->mb.pic.p_fenc[0] + 4*block_idx_x[idx] + 4*FENC_STRIDE*block_idx_y[idx];
- + for(x = 0; x < size; x++)
- + for(y = 0; y < size; y++)
- + {
- + if(x == 0 || y == 0) start = 0;
- + else if(x == size - 1 || y == size - 1) start = -2;
- + else start = -1;
- + inv_variance[y][x] = 256 + 65536 / (variance(pix,x,y,start) + 180);
- + inv_variance[y][x] = 800 - inv_variance[y][x];
- + }
- + int i;
- + int runs = 0;
- + int changed[16];
- + for( ; runs < 1000; runs++)
- + {
- + int best_change_i = 0;
- + int64_t best_change = 0;
- + int best_change_amount = 0;
- + uint64_t orig_error = weighted_error4(h,dct,inv_variance,i_qp,idx,i_quant_cat,i_ctxBlockCat);
- + for( i = (i_ctxBlockCat == DCT_LUMA_AC); i < 16; i++ )
- + {
- + //if(changed[i]) continue;
- + int change = -1;
- + for(; change <= 1; change += 2)
- + {
- + dct[0][i] += change;
- + uint64_t new_error = weighted_error4(h,dct,inv_variance,i_qp,idx,i_quant_cat,i_ctxBlockCat);
- + int64_t cur_change = orig_error - new_error;
- + if(cur_change > best_change)
- + {
- + dct[0][i] -= change;
- + best_change = cur_change;
- + best_change_i = i;
- + best_change_amount = change;
- + break;
- + }
- + dct[0][i] -= change;
- + }
- + }
- + if(best_change == 0) break;
- + else
- + {
- + changed[best_change_i] = 1;
- + dct[0][best_change_i] += best_change_amount;
- + }
- + }
- +}
- +
- void x264_quant_4x4_trellis( x264_t *h, int16_t dct[4][4], int i_quant_cat,
- int i_qp, int i_ctxBlockCat, int b_intra )
- {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement