macroblock.c
上传用户:hjq518
上传日期:2021-12-09
资源大小:5084k
文件大小:29k
源码类别:

Audio

开发平台:

Visual C++

  1. /*****************************************************************************
  2.  * macroblock.c: h264 encoder library
  3.  *****************************************************************************
  4.  * Copyright (C) 2003 Laurent Aimar
  5.  * $Id: macroblock.c,v 1.1 2004/06/03 19:27:08 fenrir Exp $
  6.  *
  7.  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  22.  *****************************************************************************/
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include "common/common.h"
  26. #include "macroblock.h"
  27. /* def_quant4_mf only for probe_skip; actual encoding uses matrices from set.c */
  28. /* FIXME this seems to make better decisions with cqm=jvt, but could screw up
  29.  * with general custom matrices. */
  30. static const int def_quant4_mf[6][4][4] =
  31. {
  32.     { { 13107, 8066, 13107, 8066 }, { 8066, 5243, 8066, 5243 },
  33.       { 13107, 8066, 13107, 8066 }, { 8066, 5243, 8066, 5243 } },
  34.     { { 11916, 7490, 11916, 7490 }, { 7490, 4660, 7490, 4660 },
  35.       { 11916, 7490, 11916, 7490 }, { 7490, 4660, 7490, 4660 } },
  36.     { { 10082, 6554, 10082, 6554 }, { 6554, 4194, 6554, 4194 },
  37.       { 10082, 6554, 10082, 6554 }, { 6554, 4194, 6554, 4194 } },
  38.     { {  9362, 5825,  9362, 5825 }, { 5825, 3647, 5825, 3647 },
  39.       {  9362, 5825,  9362, 5825 }, { 5825, 3647, 5825, 3647 } },
  40.     { {  8192, 5243,  8192, 5243 }, { 5243, 3355, 5243, 3355 },
  41.       {  8192, 5243,  8192, 5243 }, { 5243, 3355, 5243, 3355 } },
  42.     { {  7282, 4559,  7282, 4559 }, { 4559, 2893, 4559, 2893 },
  43.       {  7282, 4559,  7282, 4559 }, { 4559, 2893, 4559, 2893 } }
  44. };
  45. /****************************************************************************
  46.  * Scan and Quant functions
  47.  ****************************************************************************/
  48. //static const int scan_zigzag_x[16]={0, 1, 0, 0, 1, 2, 3, 2, 1, 0, 1, 2, 3, 3, 2, 3};
  49. //static const int scan_zigzag_y[16]={0, 0, 1, 2, 1, 0, 0, 1, 2, 3, 3, 2, 1, 2, 3, 3};
  50. #define ZIG(i,y,x) level[i] = dct[y][x];
  51. static inline void scan_zigzag_8x8full( int level[64], int16_t dct[8][8] )
  52. {
  53.     ZIG( 0,0,0) ZIG( 1,0,1) ZIG( 2,1,0) ZIG( 3,2,0)
  54.     ZIG( 4,1,1) ZIG( 5,0,2) ZIG( 6,0,3) ZIG( 7,1,2)
  55.     ZIG( 8,2,1) ZIG( 9,3,0) ZIG(10,4,0) ZIG(11,3,1)
  56.     ZIG(12,2,2) ZIG(13,1,3) ZIG(14,0,4) ZIG(15,0,5)
  57.     ZIG(16,1,4) ZIG(17,2,3) ZIG(18,3,2) ZIG(19,4,1)
  58.     ZIG(20,5,0) ZIG(21,6,0) ZIG(22,5,1) ZIG(23,4,2)
  59.     ZIG(24,3,3) ZIG(25,2,4) ZIG(26,1,5) ZIG(27,0,6)
  60.     ZIG(28,0,7) ZIG(29,1,6) ZIG(30,2,5) ZIG(31,3,4)
  61.     ZIG(32,4,3) ZIG(33,5,2) ZIG(34,6,1) ZIG(35,7,0)
  62.     ZIG(36,7,1) ZIG(37,6,2) ZIG(38,5,3) ZIG(39,4,4)
  63.     ZIG(40,3,5) ZIG(41,2,6) ZIG(42,1,7) ZIG(43,2,7)
  64.     ZIG(44,3,6) ZIG(45,4,5) ZIG(46,5,4) ZIG(47,6,3)
  65.     ZIG(48,7,2) ZIG(49,7,3) ZIG(50,6,4) ZIG(51,5,5)
  66.     ZIG(52,4,6) ZIG(53,3,7) ZIG(54,4,7) ZIG(55,5,6)
  67.     ZIG(56,6,5) ZIG(57,7,4) ZIG(58,7,5) ZIG(59,6,6)
  68.     ZIG(60,5,7) ZIG(61,6,7) ZIG(62,7,6) ZIG(63,7,7)
  69. }
  70. static inline void scan_zigzag_4x4full( int level[16], int16_t dct[4][4] )
  71. {
  72.     ZIG( 0,0,0) ZIG( 1,0,1) ZIG( 2,1,0) ZIG( 3,2,0)
  73.     ZIG( 4,1,1) ZIG( 5,0,2) ZIG( 6,0,3) ZIG( 7,1,2)
  74.     ZIG( 8,2,1) ZIG( 9,3,0) ZIG(10,3,1) ZIG(11,2,2)
  75.     ZIG(12,1,3) ZIG(13,2,3) ZIG(14,3,2) ZIG(15,3,3)
  76. }
  77. static inline void scan_zigzag_4x4( int level[15], int16_t dct[4][4] )
  78. {
  79.                 ZIG( 0,0,1) ZIG( 1,1,0) ZIG( 2,2,0)
  80.     ZIG( 3,1,1) ZIG( 4,0,2) ZIG( 5,0,3) ZIG( 6,1,2)
  81.     ZIG( 7,2,1) ZIG( 8,3,0) ZIG( 9,3,1) ZIG(10,2,2)
  82.     ZIG(11,1,3) ZIG(12,2,3) ZIG(13,3,2) ZIG(14,3,3)
  83. }
  84. static inline void scan_zigzag_2x2_dc( int level[4], int16_t dct[2][2] )
  85. {
  86.     ZIG(0,0,0)
  87.     ZIG(1,0,1)
  88.     ZIG(2,1,0)
  89.     ZIG(3,1,1)
  90. }
  91. #undef ZIG
  92. #define ZIG(i,y,x) {
  93.     int oe = x+y*FENC_STRIDE;
  94.     int od = x+y*FDEC_STRIDE;
  95.     level[i] = p_src[oe] - p_dst[od];
  96.     p_dst[od] = p_src[oe];
  97. }
  98. static inline void sub_zigzag_4x4full( int level[16], const uint8_t *p_src, uint8_t *p_dst )
  99. {
  100.     ZIG( 0,0,0) ZIG( 1,0,1) ZIG( 2,1,0) ZIG( 3,2,0)
  101.     ZIG( 4,1,1) ZIG( 5,0,2) ZIG( 6,0,3) ZIG( 7,1,2)
  102.     ZIG( 8,2,1) ZIG( 9,3,0) ZIG(10,3,1) ZIG(11,2,2)
  103.     ZIG(12,1,3) ZIG(13,2,3) ZIG(14,3,2) ZIG(15,3,3)
  104. }
  105. static inline void sub_zigzag_4x4( int level[15], const uint8_t *p_src, uint8_t *p_dst )
  106. {
  107.                 ZIG( 0,0,1) ZIG( 1,1,0) ZIG( 2,2,0)
  108.     ZIG( 3,1,1) ZIG( 4,0,2) ZIG( 5,0,3) ZIG( 6,1,2)
  109.     ZIG( 7,2,1) ZIG( 8,3,0) ZIG( 9,3,1) ZIG(10,2,2)
  110.     ZIG(11,1,3) ZIG(12,2,3) ZIG(13,3,2) ZIG(14,3,3)
  111. }
  112. #undef ZIG
  113. static void quant_8x8( x264_t *h, int16_t dct[8][8], int quant_mf[6][8][8], int i_qscale, int b_intra )
  114. {
  115.     const int i_qbits = 16 + i_qscale / 6;
  116.     const int i_mf = i_qscale % 6;
  117.     const int f = ( 1 << i_qbits ) / ( b_intra ? 3 : 6 );
  118.     h->quantf.quant_8x8_core( dct, quant_mf[i_mf], i_qbits, f );
  119. }
  120. static void quant_4x4( x264_t *h, int16_t dct[4][4], int quant_mf[6][4][4], int i_qscale, int b_intra )
  121. {
  122.     const int i_qbits = 15 + i_qscale / 6;
  123.     const int i_mf = i_qscale % 6;
  124.     const int f = ( 1 << i_qbits ) / ( b_intra ? 3 : 6 );
  125.     h->quantf.quant_4x4_core( dct, quant_mf[i_mf], i_qbits, f );
  126. }
  127. static void quant_4x4_dc( x264_t *h, int16_t dct[4][4], int quant_mf[6][4][4], int i_qscale )
  128. {
  129.     const int i_qbits = 16 + i_qscale / 6;
  130.     const int i_mf = i_qscale % 6;
  131.     const int f = ( 1 << i_qbits ) / 3;
  132.     h->quantf.quant_4x4_dc_core( dct, quant_mf[i_mf][0][0], i_qbits, f );
  133. }
  134. static void quant_2x2_dc( x264_t *h, int16_t dct[2][2], int quant_mf[6][4][4], int i_qscale, int b_intra )
  135. {
  136.     const int i_qbits = 16 + i_qscale / 6;
  137.     const int i_mf = i_qscale % 6;
  138.     const int f = ( 1 << i_qbits ) / ( b_intra ? 3 : 6 );
  139.     h->quantf.quant_2x2_dc_core( dct, quant_mf[i_mf][0][0], i_qbits, f );
  140. }
  141. /* (ref: JVT-B118)
  142.  * x264_mb_decimate_score: given dct coeffs it returns a score to see if we could empty this dct coeffs
  143.  * to 0 (low score means set it to null)
  144.  * Used in inter macroblock (luma and chroma)
  145.  *  luma: for a 8x8 block: if score < 4 -> null
  146.  *        for the complete mb: if score < 6 -> null
  147.  *  chroma: for the complete mb: if score < 7 -> null
  148.  */
  149. static int x264_mb_decimate_score( int *dct, int i_max )
  150. {
  151.     static const int i_ds_table4[16] = {
  152.         3,2,2,1,1,1,0,0,0,0,0,0,0,0,0,0 };
  153.     static const int i_ds_table8[64] = {
  154.         3,3,3,3,2,2,2,2,2,2,2,2,1,1,1,1,
  155.         1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,
  156.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  157.         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  158.     const int *ds_table = (i_max == 64) ? i_ds_table8 : i_ds_table4;
  159.     int i_score = 0;
  160.     int idx = i_max - 1;
  161.     while( idx >= 0 && dct[idx] == 0 )
  162.         idx--;
  163.     while( idx >= 0 )
  164.     {
  165.         int i_run;
  166.         if( abs( dct[idx--] ) > 1 )
  167.             return 9;
  168.         i_run = 0;
  169.         while( idx >= 0 && dct[idx] == 0 )
  170.         {
  171.             idx--;
  172.             i_run++;
  173.         }
  174.         i_score += ds_table[i_run];
  175.     }
  176.     return i_score;
  177. }
  178. void x264_mb_encode_i4x4( x264_t *h, int idx, int i_qscale )
  179. {
  180.     int x = 4 * block_idx_x[idx];
  181.     int y = 4 * block_idx_y[idx];
  182.     uint8_t *p_src = &h->mb.pic.p_fenc[0][x+y*FENC_STRIDE];
  183.     uint8_t *p_dst = &h->mb.pic.p_fdec[0][x+y*FDEC_STRIDE];
  184.     int16_t dct4x4[4][4];
  185.     if( h->mb.b_lossless )
  186.     {
  187.         sub_zigzag_4x4full( h->dct.block[idx].luma4x4, p_src, p_dst );
  188.         return;
  189.     }
  190.     h->dctf.sub4x4_dct( dct4x4, p_src, FENC_STRIDE, p_dst, FDEC_STRIDE );
  191.     if( h->mb.b_trellis )
  192.         x264_quant_4x4_trellis( h, dct4x4, CQM_4IY, i_qscale, DCT_LUMA_4x4, 1 );
  193.     else
  194.         quant_4x4( h, dct4x4, h->quant4_mf[CQM_4IY], i_qscale, 1 );
  195.     scan_zigzag_4x4full( h->dct.block[idx].luma4x4, dct4x4 );
  196.     h->quantf.dequant_4x4( dct4x4, h->dequant4_mf[CQM_4IY], i_qscale );
  197.     /* output samples to fdec */
  198.     h->dctf.add4x4_idct( p_dst, FDEC_STRIDE, dct4x4 );
  199. }
  200. void x264_mb_encode_i8x8( x264_t *h, int idx, int i_qscale )
  201. {
  202.     int x = 8 * (idx&1);
  203.     int y = 8 * (idx>>1);
  204.     uint8_t *p_src = &h->mb.pic.p_fenc[0][x+y*FENC_STRIDE];
  205.     uint8_t *p_dst = &h->mb.pic.p_fdec[0][x+y*FDEC_STRIDE];
  206.     int16_t dct8x8[8][8];
  207.     h->dctf.sub8x8_dct8( dct8x8, p_src, FENC_STRIDE, p_dst, FDEC_STRIDE );
  208.     if( h->mb.b_trellis )
  209.         x264_quant_8x8_trellis( h, dct8x8, CQM_8IY, i_qscale, 1 );
  210.     else 
  211.         quant_8x8( h, dct8x8, h->quant8_mf[CQM_8IY], i_qscale, 1 );
  212.     scan_zigzag_8x8full( h->dct.luma8x8[idx], dct8x8 );
  213.     h->quantf.dequant_8x8( dct8x8, h->dequant8_mf[CQM_8IY], i_qscale );
  214.     h->dctf.add8x8_idct8( p_dst, FDEC_STRIDE, dct8x8 );
  215. }
  216. static void x264_mb_encode_i16x16( x264_t *h, int i_qscale )
  217. {
  218.     uint8_t  *p_src = h->mb.pic.p_fenc[0];
  219.     uint8_t  *p_dst = h->mb.pic.p_fdec[0];
  220.     int16_t dct4x4[16+1][4][4];
  221.     int i;
  222.     if( h->mb.b_lossless )
  223.     {
  224.         for( i = 0; i < 16; i++ )
  225.         {
  226.             int oe = block_idx_x[i]*4 + block_idx_y[i]*4*FENC_STRIDE;
  227.             int od = block_idx_x[i]*4 + block_idx_y[i]*4*FDEC_STRIDE;
  228.             sub_zigzag_4x4( h->dct.block[i].residual_ac, p_src+oe, p_dst+od );
  229.             dct4x4[0][block_idx_y[i]][block_idx_x[i]] = p_src[oe] - p_dst[od];
  230.             p_dst[od] = p_src[oe];
  231.         }
  232.         scan_zigzag_4x4full( h->dct.luma16x16_dc, dct4x4[0] );
  233.         return;
  234.     }
  235.     h->dctf.sub16x16_dct( &dct4x4[1], p_src, FENC_STRIDE, p_dst, FDEC_STRIDE );
  236.     for( i = 0; i < 16; i++ )
  237.     {
  238.         /* copy dc coeff */
  239.         dct4x4[0][block_idx_y[i]][block_idx_x[i]] = dct4x4[1+i][0][0];
  240.         /* quant/scan/dequant */
  241.         if( h->mb.b_trellis )
  242.             x264_quant_4x4_trellis( h, dct4x4[1+i], CQM_4IY, i_qscale, DCT_LUMA_AC, 1 );
  243.         else
  244.             quant_4x4( h, dct4x4[1+i], h->quant4_mf[CQM_4IY], i_qscale, 1 );
  245.         scan_zigzag_4x4( h->dct.block[i].residual_ac, dct4x4[1+i] );
  246.         h->quantf.dequant_4x4( dct4x4[1+i], h->dequant4_mf[CQM_4IY], i_qscale );
  247.     }
  248.     h->dctf.dct4x4dc( dct4x4[0] );
  249.     quant_4x4_dc( h, dct4x4[0], h->quant4_mf[CQM_4IY], i_qscale );
  250.     scan_zigzag_4x4full( h->dct.luma16x16_dc, dct4x4[0] );
  251.     /* output samples to fdec */
  252.     h->dctf.idct4x4dc( dct4x4[0] );
  253.     x264_mb_dequant_4x4_dc( dct4x4[0], h->dequant4_mf[CQM_4IY], i_qscale );  /* XXX not inversed */
  254.     /* calculate dct coeffs */
  255.     for( i = 0; i < 16; i++ )
  256.     {
  257.         /* copy dc coeff */
  258.         dct4x4[1+i][0][0] = dct4x4[0][block_idx_y[i]][block_idx_x[i]];
  259.     }
  260.     /* put pixels to fdec */
  261.     h->dctf.add16x16_idct( p_dst, FDEC_STRIDE, &dct4x4[1] );
  262. }
  263. static void x264_mb_encode_8x8_chroma( x264_t *h, int b_inter, int i_qscale )
  264. {
  265.     int i, ch;
  266.     for( ch = 0; ch < 2; ch++ )
  267.     {
  268.         uint8_t  *p_src = h->mb.pic.p_fenc[1+ch];
  269.         uint8_t  *p_dst = h->mb.pic.p_fdec[1+ch];
  270.         int i_decimate_score = 0;
  271.         int16_t dct2x2[2][2];
  272.         int16_t dct4x4[4][4][4];
  273.         if( h->mb.b_lossless )
  274.         {
  275.             for( i = 0; i < 4; i++ )
  276.             {
  277.                 int oe = block_idx_x[i]*4 + block_idx_y[i]*4*FENC_STRIDE;
  278.                 int od = block_idx_x[i]*4 + block_idx_y[i]*4*FDEC_STRIDE;
  279.                 sub_zigzag_4x4( h->dct.block[16+i+ch*4].residual_ac, p_src+oe, p_dst+od );
  280.                 h->dct.chroma_dc[ch][i] = p_src[oe] - p_dst[od];
  281.                 p_dst[od] = p_src[oe];
  282.             }
  283.             continue;
  284.         }
  285.             
  286.         h->dctf.sub8x8_dct( dct4x4, p_src, FENC_STRIDE, p_dst, FDEC_STRIDE );
  287.         /* calculate dct coeffs */
  288.         for( i = 0; i < 4; i++ )
  289.         {
  290.             /* copy dc coeff */
  291.             dct2x2[block_idx_y[i]][block_idx_x[i]] = dct4x4[i][0][0];
  292.             /* no trellis; it doesn't seem to help chroma noticeably */
  293.             quant_4x4( h, dct4x4[i], h->quant4_mf[CQM_4IC + b_inter], i_qscale, !b_inter );
  294.             scan_zigzag_4x4( h->dct.block[16+i+ch*4].residual_ac, dct4x4[i] );
  295.             if( b_inter )
  296.             {
  297.                 i_decimate_score += x264_mb_decimate_score( h->dct.block[16+i+ch*4].residual_ac, 15 );
  298.             }
  299.         }
  300.         h->dctf.dct2x2dc( dct2x2 );
  301.         quant_2x2_dc( h, dct2x2, h->quant4_mf[CQM_4IC + b_inter], i_qscale, !b_inter );
  302.         scan_zigzag_2x2_dc( h->dct.chroma_dc[ch], dct2x2 );
  303.         /* output samples to fdec */
  304.         h->dctf.idct2x2dc( dct2x2 );
  305.         x264_mb_dequant_2x2_dc( dct2x2, h->dequant4_mf[CQM_4IC + b_inter], i_qscale );  /* XXX not inversed */
  306.         if( b_inter && i_decimate_score < 7 )
  307.         {
  308.             /* Near null chroma 8x8 block so make it null (bits saving) */
  309.             memset( dct4x4, 0, sizeof( dct4x4 ) );
  310.             memset( &h->dct.block[16+ch*4], 0, 4 * sizeof( *h->dct.block ) );
  311.         }
  312.         else
  313.         {
  314.             for( i = 0; i < 4; i++ )
  315.                 h->quantf.dequant_4x4( dct4x4[i], h->dequant4_mf[CQM_4IC + b_inter], i_qscale );
  316.         }
  317.         /* calculate dct coeffs */
  318.         for( i = 0; i < 4; i++ )
  319.         {
  320.             /* copy dc coeff */
  321.             dct4x4[i][0][0] = dct2x2[0][i];
  322.         }
  323.         h->dctf.add8x8_idct( p_dst, FDEC_STRIDE, dct4x4 );
  324.     }
  325. }
  326. static void x264_macroblock_encode_skip( x264_t *h )
  327. {
  328.     int i;
  329.     h->mb.i_cbp_luma = 0x00;
  330.     h->mb.i_cbp_chroma = 0x00;
  331.     for( i = 0; i < 16+8; i++ )
  332.     {
  333.         h->mb.cache.non_zero_count[x264_scan8[i]] = 0;
  334.     }
  335.     /* store cbp */
  336.     h->mb.cbp[h->mb.i_mb_xy] = 0;
  337. }
  338. /*****************************************************************************
  339.  * x264_macroblock_encode_pskip:
  340.  *  Encode an already marked skip block
  341.  *****************************************************************************/
  342. void x264_macroblock_encode_pskip( x264_t *h )
  343. {
  344.     const int mvx = x264_clip3( h->mb.cache.mv[0][x264_scan8[0]][0],
  345.                                 h->mb.mv_min[0], h->mb.mv_max[0] );
  346.     const int mvy = x264_clip3( h->mb.cache.mv[0][x264_scan8[0]][1],
  347.                                 h->mb.mv_min[1], h->mb.mv_max[1] );
  348.     /* Motion compensation XXX probably unneeded */
  349.     h->mc.mc_luma( h->mb.pic.p_fref[0][0], h->mb.pic.i_stride[0],
  350.                    h->mb.pic.p_fdec[0],    FDEC_STRIDE,
  351.                    mvx, mvy, 16, 16 );
  352.     /* Chroma MC */
  353.     h->mc.mc_chroma( h->mb.pic.p_fref[0][0][4], h->mb.pic.i_stride[1],
  354.                      h->mb.pic.p_fdec[1],       FDEC_STRIDE,
  355.                      mvx, mvy, 8, 8 );
  356.     h->mc.mc_chroma( h->mb.pic.p_fref[0][0][5], h->mb.pic.i_stride[2],
  357.                      h->mb.pic.p_fdec[2],       FDEC_STRIDE,
  358.                      mvx, mvy, 8, 8 );
  359.     x264_macroblock_encode_skip( h );
  360. }
  361. /*****************************************************************************
  362.  * x264_macroblock_encode:
  363.  *****************************************************************************/
  364. void x264_macroblock_encode( x264_t *h )
  365. {
  366.     int i_cbp_dc = 0;
  367.     int i_qp = h->mb.i_qp;
  368.     int i;
  369.     if( h->mb.i_type == P_SKIP )
  370.     {
  371.         /* A bit special */
  372.         x264_macroblock_encode_pskip( h );
  373.         return;
  374.     }
  375.     if( h->mb.i_type == B_SKIP )
  376.     {
  377.         /* XXX motion compensation is probably unneeded */
  378.         x264_mb_mc( h );
  379.         x264_macroblock_encode_skip( h );
  380.         return;
  381.     }
  382.     if( h->mb.i_type == I_16x16 )
  383.     {
  384.         const int i_mode = h->mb.i_intra16x16_pred_mode;
  385.         h->mb.b_transform_8x8 = 0;
  386.         /* do the right prediction */
  387.         h->predict_16x16[i_mode]( h->mb.pic.p_fdec[0] );
  388.         /* encode the 16x16 macroblock */
  389.         x264_mb_encode_i16x16( h, i_qp );
  390.     }
  391.     else if( h->mb.i_type == I_8x8 )
  392.     {
  393.         h->mb.b_transform_8x8 = 1;
  394.         for( i = 0; i < 4; i++ )
  395.         {
  396.             uint8_t  *p_dst = &h->mb.pic.p_fdec[0][8 * (i&1) + 8 * (i>>1) * FDEC_STRIDE];
  397.             int      i_mode = h->mb.cache.intra4x4_pred_mode[x264_scan8[4*i]];
  398.             h->predict_8x8[i_mode]( p_dst, h->mb.i_neighbour8[i] );
  399.             x264_mb_encode_i8x8( h, i, i_qp );
  400.         }
  401.     }
  402.     else if( h->mb.i_type == I_4x4 )
  403.     {
  404.         h->mb.b_transform_8x8 = 0;
  405.         for( i = 0; i < 16; i++ )
  406.         {
  407.             uint8_t  *p_dst = &h->mb.pic.p_fdec[0][4 * block_idx_x[i] + 4 * block_idx_y[i] * FDEC_STRIDE];
  408.             int      i_mode = h->mb.cache.intra4x4_pred_mode[x264_scan8[i]];
  409.             if( (h->mb.i_neighbour4[i] & (MB_TOPRIGHT|MB_TOP)) == MB_TOP )
  410.                 /* emulate missing topright samples */
  411.                 *(uint32_t*) &p_dst[4-FDEC_STRIDE] = p_dst[3-FDEC_STRIDE] * 0x01010101U;
  412.             h->predict_4x4[i_mode]( p_dst );
  413.             x264_mb_encode_i4x4( h, i, i_qp );
  414.         }
  415.     }
  416.     else    /* Inter MB */
  417.     {
  418.         int i8x8, i4x4, idx;
  419.         int i_decimate_mb = 0;
  420.         /* Motion compensation */
  421.         x264_mb_mc( h );
  422.         if( h->mb.b_lossless )
  423.         {
  424.             for( i4x4 = 0; i4x4 < 16; i4x4++ )
  425.             {
  426.                 int x = 4*block_idx_x[i4x4];
  427.                 int y = 4*block_idx_y[i4x4];
  428.                 sub_zigzag_4x4full( h->dct.block[i4x4].luma4x4,
  429.                                     h->mb.pic.p_fenc[0]+x+y*FENC_STRIDE,
  430.                                     h->mb.pic.p_fdec[0]+x+y*FDEC_STRIDE );
  431.             }
  432.         }
  433.         else if( h->mb.b_transform_8x8 )
  434.         {
  435.             int16_t dct8x8[4][8][8];
  436.             int nnz8x8[4] = {1,1,1,1};
  437.             h->dctf.sub16x16_dct8( dct8x8,
  438.                                    h->mb.pic.p_fenc[0], FENC_STRIDE,
  439.                                    h->mb.pic.p_fdec[0], FDEC_STRIDE );
  440.             for( idx = 0; idx < 4; idx++ )
  441.             {
  442.                 if( h->mb.b_noise_reduction )
  443.                     x264_denoise_dct( h, (int16_t*)dct8x8[idx] );
  444.                 if( h->mb.b_trellis )
  445.                     x264_quant_8x8_trellis( h, dct8x8[idx], CQM_8PY, i_qp, 0 );
  446.                 else
  447.                     quant_8x8( h, dct8x8[idx], h->quant8_mf[CQM_8PY], i_qp, 0 );
  448.                 scan_zigzag_8x8full( h->dct.luma8x8[idx], dct8x8[idx] );
  449.                 if( !h->mb.b_trellis )
  450.                 {
  451.                     int i_decimate_8x8 = x264_mb_decimate_score( h->dct.luma8x8[idx], 64 );
  452.                     i_decimate_mb += i_decimate_8x8;
  453.                     if( i_decimate_8x8 < 4 )
  454.                     {
  455.                         memset( h->dct.luma8x8[idx], 0, sizeof( h->dct.luma8x8[idx] ) );
  456.                         memset( dct8x8[idx], 0, sizeof( dct8x8[idx] ) );
  457.                         nnz8x8[idx] = 0;
  458.                     }
  459.                 }
  460.             }
  461.             if( i_decimate_mb < 6 && !h->mb.b_trellis )
  462.                 memset( h->dct.luma8x8, 0, sizeof( h->dct.luma8x8 ) );
  463.             else
  464.             {
  465.                 for( idx = 0; idx < 4; idx++ )
  466.                     if( nnz8x8[idx] )
  467.                     {
  468.                         h->quantf.dequant_8x8( dct8x8[idx], h->dequant8_mf[CQM_8PY], i_qp );
  469.                         h->dctf.add8x8_idct8( &h->mb.pic.p_fdec[0][(idx&1)*8 + (idx>>1)*8*FDEC_STRIDE], FDEC_STRIDE, dct8x8[idx] );
  470.                     }
  471.             }
  472.         }
  473.         else
  474.         {
  475.             int16_t dct4x4[16][4][4];
  476.             int nnz8x8[4] = {1,1,1,1};
  477.             h->dctf.sub16x16_dct( dct4x4,
  478.                                   h->mb.pic.p_fenc[0], FENC_STRIDE,
  479.                                   h->mb.pic.p_fdec[0], FDEC_STRIDE );
  480.             for( i8x8 = 0; i8x8 < 4; i8x8++ )
  481.             {
  482.                 int i_decimate_8x8;
  483.                 /* encode one 4x4 block */
  484.                 i_decimate_8x8 = 0;
  485.                 for( i4x4 = 0; i4x4 < 4; i4x4++ )
  486.                 {
  487.                     idx = i8x8 * 4 + i4x4;
  488.                     if( h->mb.b_noise_reduction )
  489.                         x264_denoise_dct( h, (int16_t*)dct4x4[idx] );
  490.                     if( h->mb.b_trellis )
  491.                         x264_quant_4x4_trellis( h, dct4x4[idx], CQM_4PY, i_qp, DCT_LUMA_4x4, 0 );
  492.                     else
  493.                         quant_4x4( h, dct4x4[idx], h->quant4_mf[CQM_4PY], i_qp, 0 );
  494.                     scan_zigzag_4x4full( h->dct.block[idx].luma4x4, dct4x4[idx] );
  495.                     i_decimate_8x8 += x264_mb_decimate_score( h->dct.block[idx].luma4x4, 16 );
  496.                 }
  497.                 /* decimate this 8x8 block */
  498.                 i_decimate_mb += i_decimate_8x8;
  499.                 if( i_decimate_8x8 < 4 )
  500.                 {
  501.                     memset( &dct4x4[i8x8*4], 0, 4 * sizeof( *dct4x4 ) );
  502.                     memset( &h->dct.block[i8x8*4], 0, 4 * sizeof( *h->dct.block ) );
  503.                     nnz8x8[i8x8] = 0;
  504.                 }
  505.             }
  506.             if( i_decimate_mb < 6 )
  507.                 memset( h->dct.block, 0, 16 * sizeof( *h->dct.block ) );
  508.             else
  509.             {
  510.                 for( i8x8 = 0; i8x8 < 4; i8x8++ )
  511.                     if( nnz8x8[i8x8] )
  512.                     {
  513.                         for( i = 0; i < 4; i++ )
  514.                             h->quantf.dequant_4x4( dct4x4[i8x8*4+i], h->dequant4_mf[CQM_4PY], i_qp );
  515.                         h->dctf.add8x8_idct( &h->mb.pic.p_fdec[0][(i8x8&1)*8 + (i8x8>>1)*8*FDEC_STRIDE], FDEC_STRIDE, &dct4x4[i8x8*4] );
  516.                     }
  517.             }
  518.         }
  519.     }
  520.     /* encode chroma */
  521.     i_qp = i_chroma_qp_table[x264_clip3( i_qp + h->pps->i_chroma_qp_index_offset, 0, 51 )];
  522.     if( IS_INTRA( h->mb.i_type ) )
  523.     {
  524.         const int i_mode = h->mb.i_chroma_pred_mode;
  525.         h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[1] );
  526.         h->predict_8x8c[i_mode]( h->mb.pic.p_fdec[2] );
  527.     }
  528.     /* encode the 8x8 blocks */
  529.     x264_mb_encode_8x8_chroma( h, !IS_INTRA( h->mb.i_type ), i_qp );
  530.     /* Calculate the Luma/Chroma patern and non_zero_count */
  531.     h->mb.i_cbp_luma = 0x00;
  532.     if( h->mb.i_type == I_16x16 )
  533.     {
  534.         for( i = 0; i < 16; i++ )
  535.         {
  536.             const int nz = array_non_zero_count( h->dct.block[i].residual_ac, 15 );
  537.             h->mb.cache.non_zero_count[x264_scan8[i]] = nz;
  538.             if( nz > 0 )
  539.                 h->mb.i_cbp_luma = 0x0f;
  540.         }
  541.     }
  542.     else if( h->mb.b_transform_8x8 )
  543.     {
  544.         /* coded_block_flag is enough for CABAC.
  545.          * the full non_zero_count is done only in CAVLC. */
  546.         for( i = 0; i < 4; i++ )
  547.         {
  548.             const int nz = array_non_zero( h->dct.luma8x8[i], 64 );
  549.             int j;
  550.             for( j = 0; j < 4; j++ )
  551.                 h->mb.cache.non_zero_count[x264_scan8[4*i+j]] = nz;
  552.             if( nz > 0 )
  553.                 h->mb.i_cbp_luma |= 1 << i;
  554.         }
  555.     }
  556.     else
  557.     {
  558.         for( i = 0; i < 16; i++ )
  559.         {
  560.             const int nz = array_non_zero_count( h->dct.block[i].luma4x4, 16 );
  561.             h->mb.cache.non_zero_count[x264_scan8[i]] = nz;
  562.             if( nz > 0 )
  563.                 h->mb.i_cbp_luma |= 1 << (i/4);
  564.         }
  565.     }
  566.     /* Calculate the chroma patern */
  567.     h->mb.i_cbp_chroma = 0x00;
  568.     for( i = 0; i < 8; i++ )
  569.     {
  570.         const int nz = array_non_zero_count( h->dct.block[16+i].residual_ac, 15 );
  571.         h->mb.cache.non_zero_count[x264_scan8[16+i]] = nz;
  572.         if( nz > 0 )
  573.         {
  574.             h->mb.i_cbp_chroma = 0x02;    /* dc+ac (we can't do only ac) */
  575.         }
  576.     }
  577.     if( h->mb.i_cbp_chroma == 0x00 && array_non_zero( h->dct.chroma_dc[0], 8 ) )
  578.     {
  579.         h->mb.i_cbp_chroma = 0x01;    /* dc only */
  580.     }
  581.     if( h->param.b_cabac )
  582.     {
  583.         i_cbp_dc = ( h->mb.i_type == I_16x16 && array_non_zero( h->dct.luma16x16_dc, 16 ) )
  584.                  | array_non_zero( h->dct.chroma_dc[0], 4 ) << 1
  585.                  | array_non_zero( h->dct.chroma_dc[1], 4 ) << 2;
  586.     }
  587.     /* store cbp */
  588.     h->mb.cbp[h->mb.i_mb_xy] = (i_cbp_dc << 8) | (h->mb.i_cbp_chroma << 4) | h->mb.i_cbp_luma;
  589.     /* Check for P_SKIP
  590.      * XXX: in the me perhaps we should take x264_mb_predict_mv_pskip into account
  591.      *      (if multiple mv give same result)*/
  592.     if( h->mb.i_type == P_L0 && h->mb.i_partition == D_16x16 &&
  593.         h->mb.i_cbp_luma == 0x00 && h->mb.i_cbp_chroma== 0x00 &&
  594.         h->mb.cache.ref[0][x264_scan8[0]] == 0 )
  595.     {
  596.         int mvp[2];
  597.         x264_mb_predict_mv_pskip( h, mvp );
  598.         if( h->mb.cache.mv[0][x264_scan8[0]][0] == mvp[0] &&
  599.             h->mb.cache.mv[0][x264_scan8[0]][1] == mvp[1] )
  600.         {
  601.             h->mb.i_type = P_SKIP;
  602.         }
  603.     }
  604.     /* Check for B_SKIP */
  605.     if( h->mb.i_type == B_DIRECT &&
  606.         h->mb.i_cbp_luma == 0x00 && h->mb.i_cbp_chroma== 0x00 )
  607.     {
  608.         h->mb.i_type = B_SKIP;
  609.     }
  610. }
  611. /*****************************************************************************
  612.  * x264_macroblock_probe_skip:
  613.  *  Check if the current MB could be encoded as a [PB]_SKIP (it supposes you use
  614.  *  the previous QP
  615.  *****************************************************************************/
  616. int x264_macroblock_probe_skip( x264_t *h, int b_bidir )
  617. {
  618.     DECLARE_ALIGNED( int16_t, dct4x4[16][4][4], 16 );
  619.     DECLARE_ALIGNED( int16_t, dct2x2[2][2], 16 );
  620.     DECLARE_ALIGNED( int,     dctscan[16], 16 );
  621.     int i_qp = h->mb.i_qp;
  622.     int mvp[2];
  623.     int ch;
  624.     int i8x8, i4x4;
  625.     int i_decimate_mb;
  626.     if( !b_bidir )
  627.     {
  628.         /* Get the MV */
  629.         x264_mb_predict_mv_pskip( h, mvp );
  630.         mvp[0] = x264_clip3( mvp[0], h->mb.mv_min[0], h->mb.mv_max[0] );
  631.         mvp[1] = x264_clip3( mvp[1], h->mb.mv_min[1], h->mb.mv_max[1] );
  632.         /* Motion compensation */
  633.         h->mc.mc_luma( h->mb.pic.p_fref[0][0], h->mb.pic.i_stride[0],
  634.                        h->mb.pic.p_fdec[0],    FDEC_STRIDE,
  635.                        mvp[0], mvp[1], 16, 16 );
  636.     }
  637.     /* get luma diff */
  638.     h->dctf.sub16x16_dct( dct4x4, h->mb.pic.p_fenc[0], FENC_STRIDE,
  639.                                   h->mb.pic.p_fdec[0], FDEC_STRIDE );
  640.     for( i8x8 = 0, i_decimate_mb = 0; i8x8 < 4; i8x8++ )
  641.     {
  642.         /* encode one 4x4 block */
  643.         for( i4x4 = 0; i4x4 < 4; i4x4++ )
  644.         {
  645.             const int idx = i8x8 * 4 + i4x4;
  646.             quant_4x4( h, dct4x4[idx], (int(*)[4][4])def_quant4_mf, i_qp, 0 );
  647.             scan_zigzag_4x4full( dctscan, dct4x4[idx] );
  648.             i_decimate_mb += x264_mb_decimate_score( dctscan, 16 );
  649.             if( i_decimate_mb >= 6 )
  650.             {
  651.                 /* not as P_SKIP */
  652.                 return 0;
  653.             }
  654.         }
  655.     }
  656.     /* encode chroma */
  657.     i_qp = i_chroma_qp_table[x264_clip3( i_qp + h->pps->i_chroma_qp_index_offset, 0, 51 )];
  658.     for( ch = 0; ch < 2; ch++ )
  659.     {
  660.         uint8_t  *p_src = h->mb.pic.p_fenc[1+ch];
  661.         uint8_t  *p_dst = h->mb.pic.p_fdec[1+ch];
  662.         if( !b_bidir )
  663.         {
  664.             h->mc.mc_chroma( h->mb.pic.p_fref[0][0][4+ch], h->mb.pic.i_stride[1+ch],
  665.                              h->mb.pic.p_fdec[1+ch],       FDEC_STRIDE,
  666.                              mvp[0], mvp[1], 8, 8 );
  667.         }
  668.         h->dctf.sub8x8_dct( dct4x4, p_src, FENC_STRIDE, p_dst, FDEC_STRIDE );
  669.         /* calculate dct DC */
  670.         dct2x2[0][0] = dct4x4[0][0][0];
  671.         dct2x2[0][1] = dct4x4[1][0][0];
  672.         dct2x2[1][0] = dct4x4[2][0][0];
  673.         dct2x2[1][1] = dct4x4[3][0][0];
  674.         h->dctf.dct2x2dc( dct2x2 );
  675.         quant_2x2_dc( h, dct2x2, (int(*)[4][4])def_quant4_mf, i_qp, 0 );
  676.         if( dct2x2[0][0] || dct2x2[0][1] || dct2x2[1][0] || dct2x2[1][1]  )
  677.         {
  678.             /* can't be */
  679.             return 0;
  680.         }
  681.         /* calculate dct coeffs */
  682.         for( i4x4 = 0, i_decimate_mb = 0; i4x4 < 4; i4x4++ )
  683.         {
  684.             quant_4x4( h, dct4x4[i4x4], (int(*)[4][4])def_quant4_mf, i_qp, 0 );
  685.             scan_zigzag_4x4( dctscan, dct4x4[i4x4] );
  686.             i_decimate_mb += x264_mb_decimate_score( dctscan, 15 );
  687.             if( i_decimate_mb >= 7 )
  688.             {
  689.                 return 0;
  690.             }
  691.         }
  692.     }
  693.     return 1;
  694. }
  695. /****************************************************************************
  696.  * DCT-domain noise reduction / adaptive deadzone
  697.  * from libavcodec
  698.  ****************************************************************************/
  699. void x264_noise_reduction_update( x264_t *h )
  700. {
  701.     int cat, i;
  702.     for( cat = 0; cat < 2; cat++ )
  703.     {
  704.         int size = cat ? 64 : 16;
  705.         const int *weight = cat ? x264_dct8_weight2_tab : x264_dct4_weight2_tab;
  706.         if( h->nr_count[cat] > (cat ? (1<<16) : (1<<18)) )
  707.         {
  708.             for( i = 0; i < size; i++ )
  709.                 h->nr_residual_sum[cat][i] >>= 1;
  710.             h->nr_count[cat] >>= 1;
  711.         }
  712.         for( i = 0; i < size; i++ )
  713.             h->nr_offset[cat][i] =
  714.                 ((uint64_t)h->param.analyse.i_noise_reduction * h->nr_count[cat]
  715.                  + h->nr_residual_sum[cat][i]/2)
  716.               / ((uint64_t)h->nr_residual_sum[cat][i] * weight[i]/256 + 1);
  717.     }
  718. }
  719. void x264_denoise_dct( x264_t *h, int16_t *dct )
  720. {
  721.     const int cat = h->mb.b_transform_8x8;
  722.     int i;
  723.     h->nr_count[cat]++;
  724.     for( i = (cat ? 63 : 15); i >= 1; i-- )
  725.     {
  726.         int level = dct[i];
  727.         if( level )
  728.         {
  729.             if( level > 0 )
  730.             {
  731.                 h->nr_residual_sum[cat][i] += level;
  732.                 level -= h->nr_offset[cat][i];
  733.                 if( level < 0 )
  734.                     level = 0;
  735.             }
  736.             else
  737.             {
  738.                 h->nr_residual_sum[cat][i] -= level;
  739.                 level += h->nr_offset[cat][i];
  740.                 if( level > 0 )
  741.                     level = 0;
  742.             }
  743.             dct[i] = level;
  744.         }
  745.     }
  746. }