Adds another safeguard to ensure AC3 pass-thru uses a 48khz sample rate.
[HandBrake.git] / contrib / patch-x264-vbv-2pass.patch
blobbd39a1bd0ea33d347a207015258eb237f2a9c37d
1 Index: encoder/ratecontrol.c
2 ===================================================================
3 --- encoder/ratecontrol.c (revision 736)
4 +++ encoder/ratecontrol.c (working copy)
5 @@ -43,6 +43,7 @@
6 int p_tex_bits;
7 int misc_bits;
8 uint64_t expected_bits;
9 + double expected_vbv;
10 float new_qscale;
11 int new_qp;
12 int i_count;
13 @@ -1149,7 +1150,7 @@
14 return;
16 rct->buffer_fill_final += rct->buffer_rate - bits;
17 - if( rct->buffer_fill_final < 0 && !rct->b_2pass )
18 + if( rct->buffer_fill_final < 0 )
19 x264_log( h, X264_LOG_WARNING, "VBV underflow (%.0f bits)\n", rct->buffer_fill_final );
20 rct->buffer_fill_final = x264_clip3f( rct->buffer_fill_final, 0, rct->buffer_size );
22 @@ -1325,6 +1326,22 @@
23 double w = x264_clip3f( time*100, 0.0, 1.0 );
24 q *= pow( (double)total_bits / rcc->expected_bits_sum, w );
26 + if( rcc->b_vbv )
27 + {
28 + double expected_size = qscale2bits(&rce, q);
29 + double expected_vbv = rcc->buffer_fill + rcc->buffer_rate - expected_size;
30 + if( (expected_vbv < rcc->buffer_size*.4) && (expected_vbv < rce.expected_vbv) )
31 + {
32 + double qmax = (expected_vbv < rcc->buffer_size*.15) ? lmax : q*1.5;
33 + double size_constraint = 1 + rce.expected_vbv/rcc->buffer_size;
34 + while( (expected_vbv < rce.expected_vbv/size_constraint) && (q < qmax) )
35 + {
36 + q *= 1.05;
37 + expected_size = qscale2bits(&rce, q);
38 + expected_vbv = rcc->buffer_fill + rcc->buffer_rate - expected_size;
39 + }
40 + }
41 + }
42 q = x264_clip3f( q, lmin, lmax );
44 else /* 1pass ABR */
45 @@ -1455,6 +1472,134 @@
46 /* the rest of the variables are either constant or thread-local */
49 +FILE *fh_vbv;
51 +static int find_underflow( x264_t *h, double *fills, int *t0, int *t1, int over )
53 + /* find an interval ending on an overflow or underflow (depending on whether
54 + * we're adding or removing bits), and starting on the earliest frame that
55 + * can influence the buffer fill of that end frame. */
56 + x264_ratecontrol_t *rcc = h->rc;
57 + const double buffer_min = (over ? .1 : .1) * rcc->buffer_size;
58 + const double buffer_max = .9 * rcc->buffer_size;
59 + double fill = fills[*t0-1];
60 + double parity = over ? 1. : -1.;
61 + int i, start=-1, end=-1;
62 + for(i=*t0; i<rcc->num_entries; i++)
63 + {
64 + fill += (rcc->buffer_rate - qscale2bits(&rcc->entry[i], rcc->entry[i].new_qscale)) * parity;
65 + fill = x264_clip3f(fill, 0, rcc->buffer_size);
66 + fills[i] = fill;
67 + if(fill <= buffer_min || i == 0)
68 + {
69 + if(end >= 0)
70 + break;
71 + start = i;
72 + }
73 + else if(fill >= buffer_max && start >= 0)
74 + end = i;
75 + }
76 + *t0 = start;
77 + *t1 = end;
78 + return start>=0 && end>=0;
81 +static void fix_underflow( x264_t *h, int t0, int t1, double adjustment, double qscale_min, double qscale_max)
83 + x264_ratecontrol_t *rcc = h->rc;
84 + double qscale_new;
85 + int i;
86 + if(t0 > 0)
87 + t0++;
88 + for(i=t0; i<=t1; i++) {
89 + qscale_new = rcc->entry[i].new_qscale *= adjustment;
90 + rcc->entry[i].new_qscale = x264_clip3f(qscale_new, qscale_min, qscale_max);
91 + }
94 +static double count_expected_bits( x264_t *h )
96 + x264_ratecontrol_t *rcc = h->rc;
97 + double expected_bits = 0;
98 + int i;
99 + for(i=0; i<rcc->num_entries; i++)
101 + ratecontrol_entry_t *rce = &rcc->entry[i];
102 + rce->expected_bits = expected_bits;
103 + expected_bits += qscale2bits(rce, rce->new_qscale);
105 + return expected_bits;
108 +static void debug_dump_vbv( x264_t *h )
110 + x264_ratecontrol_t *rcc = h->rc;
111 + double fill = rcc->buffer_size * h->param.rc.f_vbv_buffer_init;
112 + int i;
113 + for(i=0; i<rcc->num_entries; i++)
115 + fill += rcc->buffer_rate - qscale2bits(&rcc->entry[i], rcc->entry[i].new_qscale);
116 + fill = x264_clip3f(fill, rcc->buffer_size*-.5, rcc->buffer_size);
117 + fprintf(fh_vbv, "%d %.0f\n", i, fill);
121 +static void vbv_pass2( x264_t *h )
123 + /* foreach interval of buffer_full .. underflow
124 + * uniformly increase the qp of all frames in the interval until either
125 + * buffer is full at some intermediate frame
126 + * or the last frame in the interval no longer underflows
127 + * recompute intervals and repeat
128 + * then do the converse to put bits back into overflow areas until target size is met */
130 + x264_ratecontrol_t *rcc = h->rc;
131 + double *fills = x264_malloc((rcc->num_entries+1)*sizeof(double));
132 + double all_available_bits = h->param.rc.i_bitrate * 1000. * rcc->num_entries / rcc->fps;
133 + double expected_bits = 0;
134 + double adjustment;
135 + double prev_bits = 0;
136 + int i, t0, t1;
137 + double qscale_min = qp2qscale(h->param.rc.i_qp_min);
138 + double qscale_max = qp2qscale(h->param.rc.i_qp_max);
140 + fills++;
142 + //adjust overall stream size
143 + do {
144 + prev_bits = expected_bits;
146 + if (expected_bits == 0) { //first iteration
147 + expected_bits = count_expected_bits(h);
149 + adjustment = X264_MAX(expected_bits / all_available_bits, 0.999);
150 + fills[-1] = rcc->buffer_size * h->param.rc.f_vbv_buffer_init;
151 + t0 = 0;
152 + //fix overflows
153 + while(find_underflow(h, fills, &t0, &t1, 1))
155 + fix_underflow(h, t0, t1, adjustment, qscale_min, qscale_max);
156 + t0 = t1;
159 + fills[-1] = rcc->buffer_size * (1. - h->param.rc.f_vbv_buffer_init);
160 + t0 = 0;
161 + //fix underflows - should be done after overflow, as we'd better undersize target than underflowing VBV
162 + while(find_underflow(h, fills, &t0, &t1, 0))
164 + fix_underflow(h, t0, t1, 1.001, qscale_min, qscale_max);
167 + expected_bits = count_expected_bits(h);
168 + } while(expected_bits < .995*all_available_bits && expected_bits >= prev_bits+1);
170 + //store expected vbv filling values for tracking when encoding
171 + for(i=0; i<rcc->num_entries; i++)
172 + rcc->entry[i].expected_vbv = rcc->buffer_size - fills[i];
174 + x264_free(fills-1);
177 static int init_pass2( x264_t *h )
179 x264_ratecontrol_t *rcc = h->rc;
180 @@ -1543,7 +1688,6 @@
181 rcc->last_non_b_pict_type = -1;
182 rcc->last_accum_p_norm = 1;
183 rcc->accum_p_norm = 0;
184 - rcc->buffer_fill = rcc->buffer_size * h->param.rc.f_vbv_buffer_init;
186 /* find qscale */
187 for(i=0; i<rcc->num_entries; i++){
188 @@ -1580,18 +1724,11 @@
189 /* find expected bits */
190 for(i=0; i<rcc->num_entries; i++){
191 ratecontrol_entry_t *rce = &rcc->entry[i];
192 - double bits;
193 rce->new_qscale = clip_qscale(h, rce->pict_type, blurred_qscale[i]);
194 assert(rce->new_qscale >= 0);
195 - bits = qscale2bits(rce, rce->new_qscale);
197 - rce->expected_bits = expected_bits;
198 - expected_bits += bits;
199 - update_vbv(h, bits);
200 - rcc->buffer_fill = rcc->buffer_fill_final;
201 + expected_bits += qscale2bits(rce, rce->new_qscale);
204 -//printf("expected:%llu available:%llu factor:%lf avgQ:%lf\n", (uint64_t)expected_bits, all_available_bits, rate_factor);
205 if(expected_bits > all_available_bits) rate_factor -= step;
208 @@ -1599,6 +1736,10 @@
209 if(filter_size > 1)
210 x264_free(blurred_qscale);
212 + if(rcc->b_vbv)
213 + vbv_pass2(h);
214 + expected_bits = count_expected_bits(h);
216 if(fabs(expected_bits/all_available_bits - 1.0) > 0.01)
218 double avgq = 0;
219 @@ -1606,7 +1747,8 @@
220 avgq += rcc->entry[i].new_qscale;
221 avgq = qscale2qp(avgq / rcc->num_entries);
223 - x264_log(h, X264_LOG_WARNING, "Error: 2pass curve failed to converge\n");
224 + if ((expected_bits > all_available_bits) || (!rcc->b_vbv))
225 + x264_log(h, X264_LOG_WARNING, "Error: 2pass curve failed to converge\n");
226 x264_log(h, X264_LOG_WARNING, "target: %.2f kbit/s, expected: %.2f kbit/s, avg QP: %.4f\n",
227 (float)h->param.rc.i_bitrate,
228 expected_bits * rcc->fps / (rcc->num_entries * 1000.),
229 @@ -1625,7 +1767,7 @@
230 else
231 x264_log(h, X264_LOG_WARNING, "try increasing target bitrate\n");
233 - else
234 + else if(!(rcc->b_2pass && rcc->b_vbv))
235 x264_log(h, X264_LOG_WARNING, "internal error\n");