OSDN Git Service

Set an minimum subtitle display time of three seconds *or* until the next subtitle...
[handbrake-jp/handbrake-jp-git.git] / contrib / patch-x264-aq.patch
1 Index: common/common.c\r
2 ===================================================================\r
3 --- common/common.c     (revision 669)\r
4 +++ common/common.c     (working copy)\r
5 @@ -123,6 +123,9 @@\r
6      param->analyse.i_chroma_qp_offset = 0;
7      param->analyse.b_fast_pskip = 1;
8      param->analyse.b_dct_decimate = 1;
9 +    param->analyse.b_aq = 0;
10 +    param->analyse.f_aq_strength = 0.0;
11 +    param->analyse.f_aq_sensitivity = 15;
12      param->analyse.i_luma_deadzone[0] = 21;
13      param->analyse.i_luma_deadzone[1] = 11;
14      param->analyse.b_psnr = 1;
15 @@ -455,6 +458,13 @@\r
16          p->analyse.b_fast_pskip = atobool(value);
17      OPT("dct-decimate")
18          p->analyse.b_dct_decimate = atobool(value);
19 +    OPT("aq-strength")
20 +    {
21 +        p->analyse.f_aq_strength = atof(value);
22 +        p->analyse.b_aq = (p->analyse.f_aq_strength > 0.0);
23 +    }
24 +    OPT("aq-sensitivity")
25 +        p->analyse.f_aq_sensitivity = atof(value);
26      OPT("deadzone-inter")
27          p->analyse.i_luma_deadzone[0] = atoi(value);
28      OPT("deadzone-intra")
29 @@ -939,6 +949,9 @@\r
30              s += sprintf( s, " zones" );
31      }
32  
33 +    if( p->analyse.b_aq )
34 +        s += sprintf( s, " aq=1:%.1f:%.1f", p->analyse.f_aq_strength, p->analyse.f_aq_sensitivity );
35 +
36      return buf;
37  }
38  
39 Index: common/pixel.c\r
40 ===================================================================\r
41 --- common/pixel.c      (revision 669)\r
42 +++ common/pixel.c      (working copy)\r
43 @@ -213,6 +213,14 @@\r
44  PIXEL_SATD_C( x264_pixel_satd_4x8,   4, 8 )
45  PIXEL_SATD_C( x264_pixel_satd_4x4,   4, 4 )
46  
47 +static int x264_pixel_count_8x8( uint8_t *pix, int i_pix, uint32_t threshold )
48 +{
49 +    int x, y, sum = 0;
50 +    for( y=0; y<8; y++, pix += i_pix )
51 +        for( x=0; x<8; x++ )
52 +            sum += pix[x] > (uint8_t)threshold;
53 +    return sum;
54 +}
55  
56  /****************************************************************************
57   * pixel_sa8d_WxH: sum of 8x8 Hadamard transformed differences
58 @@ -470,6 +478,8 @@\r
59      pixf->ads[PIXEL_16x8] = pixel_ads2;
60      pixf->ads[PIXEL_8x8] = pixel_ads1;
61  
62 +    pixf->count_8x8 = x264_pixel_count_8x8;
63 +
64  #ifdef HAVE_MMX
65      if( cpu&X264_CPU_MMX )
66      {
67 Index: common/pixel.h\r
68 ===================================================================\r
69 --- common/pixel.h      (revision 669)\r
70 +++ common/pixel.h      (working copy)\r
71 @@ -90,6 +90,8 @@\r
72      void (*ads[7])( int enc_dc[4], uint16_t *sums, int delta,
73                      uint16_t *res, int width );
74  
75 +    int (*count_8x8)( uint8_t *pix, int i_pix, uint32_t threshold );
76 +
77      /* calculate satd of V, H, and DC modes.
78       * may be NULL, in which case just use pred+satd instead. */
79      void (*intra_satd_x3_16x16)( uint8_t *fenc, uint8_t *fdec, int res[3] );
80 Index: encoder/analyse.c\r
81 ===================================================================\r
82 --- encoder/analyse.c   (revision 669)\r
83 +++ encoder/analyse.c   (working copy)\r
84 @@ -29,6 +29,7 @@\r
85  #endif
86  
87  #include "common/common.h"
88 +#include "common/cpu.h"
89  #include "macroblock.h"
90  #include "me.h"
91  #include "ratecontrol.h"
92 @@ -2029,8 +2030,68 @@\r
93      }
94  }
95  
96 +static int x264_sum_dctq( int16_t dct[8][8] )
97 +{
98 +    int i, t = 0;
99 +    int16_t *p = &dct[0][0];
100 +    for( i=1; i<64; i++ )
101 +        t += abs(p[i]) * x264_dct8_weight_tab[i];
102 +    return t;
103 +}
104  
105  /*****************************************************************************
106 + * x264_adaptive_quant:
107 + * check if mb is "flat", i.e. has most energy in low frequency components, and
108 + * adjust qp down if it is
109 + *****************************************************************************/
110 +void x264_adaptive_quant( x264_t *h, x264_mb_analysis_t *a )
111 +{
112 +    DECLARE_ALIGNED( static uint8_t, zero[FDEC_STRIDE*8], 16 );
113 +    DECLARE_ALIGNED( int16_t, dct[8][8], 16 );
114 +    float fc;
115 +    int total = 0;
116 +    int qp = h->mb.i_qp, qp_adj;
117 +    int i;
118 +
119 +    if( qp <= 10 ) /* AQ is probably not needed at such low QP */
120 +        return;
121 +
122 +    if( h->pixf.sad[PIXEL_16x16](h->mb.pic.p_fenc[0], FENC_STRIDE, zero, 16) > 64*16*16 )
123 +    {   /* light places */
124 +        if( h->pixf.count_8x8(h->mb.pic.p_fenc[1], FENC_STRIDE, 0x81818181) < 40 )
125 +            /* not enough "blue" pixels */
126 +            return;
127 +
128 +        if( h->pixf.count_8x8(h->mb.pic.p_fenc[2], FENC_STRIDE, 0x87878787) > 24 )
129 +            /* too many "red" pixels */
130 +            return;
131 +    }
132 +
133 +    for( i=0; i<4; i++ )
134 +    {
135 +        h->dctf.sub8x8_dct8( dct, h->mb.pic.p_fenc[0] + (i&1)*8 + (i>>1)*FENC_STRIDE, zero );
136 +        total += x264_sum_dctq( dct );
137 +    }
138 +
139 +    if( total == 0 ) /* no AC coefficients, nothing to do */
140 +        return;
141 +
142 +    x264_cpu_restore( h->param.cpu );
143 +
144 +    fc = expf(-5e-13 * total * total);
145 +
146 +    /* the function is chosen such that it stays close to 0 in almost all
147 +      * range of 0..1, and rapidly goes up to 1 near 1.0 */
148 +    qp_adj = (int)(qp * h->param.analyse.f_aq_strength / pow(2 - fc, h->param.analyse.f_aq_sensitivity));
149 +
150 +    /* don't adjust by more than this amount */
151 +    qp_adj = X264_MIN(qp_adj, qp/2);
152 +
153 +    h->mb.i_qp = a->i_qp = qp - qp_adj;
154 +    h->mb.i_chroma_qp = i_chroma_qp_table[x264_clip3( h->mb.i_qp + h->pps->i_chroma_qp_index_offset, 0, 51 )];
155 +}
156 +
157 +/*****************************************************************************
158   * x264_macroblock_analyse:
159   *****************************************************************************/
160  void x264_macroblock_analyse( x264_t *h )
161 @@ -2038,9 +2099,14 @@\r
162      x264_mb_analysis_t analysis;
163      int i_cost = COST_MAX;
164      int i;
165 +       
166 +    h->mb.i_qp = x264_ratecontrol_qp( h );
167  
168 +    if( h->param.analyse.b_aq )
169 +        x264_adaptive_quant( h, &analysis );
170 +
171      /* init analysis */
172 -    x264_mb_analyse_init( h, &analysis, x264_ratecontrol_qp( h ) );
173 +    x264_mb_analyse_init( h, &analysis, h->mb.i_qp );
174  
175      /*--------------------------- Do the analysis ---------------------------*/
176      if( h->sh.i_type == SLICE_TYPE_I )
177 Index: encoder/encoder.c\r
178 ===================================================================\r
179 --- encoder/encoder.c   (revision 669)\r
180 +++ encoder/encoder.c   (working copy)\r
181 @@ -477,6 +477,8 @@\r
182      if( !h->param.b_cabac )
183          h->param.analyse.i_trellis = 0;
184      h->param.analyse.i_trellis = x264_clip3( h->param.analyse.i_trellis, 0, 2 );
185 +    if( h->param.analyse.b_aq && h->param.analyse.f_aq_strength <= 0 )
186 +        h->param.analyse.b_aq = 0;
187      h->param.analyse.i_noise_reduction = x264_clip3( h->param.analyse.i_noise_reduction, 0, 1<<16 );
188  
189      {
190 Index: x264.c\r
191 ===================================================================\r
192 --- x264.c      (revision 669)\r
193 +++ x264.c      (working copy)\r
194 @@ -243,6 +243,12 @@\r
195          "                                  - 2: enabled on all mode decisions\n", defaults->analyse.i_trellis );
196      H0( "      --no-fast-pskip         Disables early SKIP detection on P-frames\n" );
197      H0( "      --no-dct-decimate       Disables coefficient thresholding on P-frames\n" );
198 +    H0( "      --aq-strength <float>   Amount to adjust QP per MB [%.1f]\n"
199 +        "                                  0.0: no AQ\n"
200 +        "                                  1.1: strong AQ\n", defaults->analyse.f_aq_strength );
201 +    H0( "      --aq-sensitivity <float> \"Flatness\" threshold to trigger AQ [%.1f]\n"
202 +        "                                    5: applies to almost all blocks\n"
203 +        "                                   22: only flat blocks\n", defaults->analyse.f_aq_sensitivity );
204      H0( "      --nr <integer>          Noise reduction [%d]\n", defaults->analyse.i_noise_reduction );
205      H1( "\n" );
206      H1( "      --deadzone-inter <int>  Set the size of the inter luma quantization deadzone [%d]\n", defaults->analyse.i_luma_deadzone[0] );
207 @@ -406,6 +412,8 @@\r
208              { "trellis", required_argument, NULL, 't' },
209              { "no-fast-pskip", no_argument, NULL, 0 },
210              { "no-dct-decimate", no_argument, NULL, 0 },
211 +            { "aq-strength", required_argument, NULL, 0 },
212 +            { "aq-sensitivity", required_argument, NULL, 0 },
213              { "deadzone-inter", required_argument, NULL, '0' },
214              { "deadzone-intra", required_argument, NULL, '0' },
215              { "level",   required_argument, NULL, 0 },
216 Index: x264.h\r
217 ===================================================================\r
218 --- x264.h      (revision 669)\r
219 +++ x264.h      (working copy)\r
220 @@ -227,6 +227,9 @@\r
221          int          i_trellis;  /* trellis RD quantization */
222          int          b_fast_pskip; /* early SKIP detection on P-frames */
223          int          b_dct_decimate; /* transform coefficient thresholding on P-frames */
224 +        int          b_aq; /* psy adaptive QP */
225 +        float        f_aq_strength;
226 +        float        f_aq_sensitivity;
227          int          i_noise_reduction; /* adaptive pseudo-deadzone */
228  
229          /* the deadzone size that will be used in luma quantization */