Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(326)

Side by Side Diff: source/libvpx/vp8/encoder/firstpass.c

Issue 7671004: Update libvpx snapshot to v0.9.7-p1 (Cayuga). (Closed) Base URL: svn://chrome-svn/chrome/trunk/deps/third_party/libvpx/
Patch Set: '' Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "math.h" 11 #include "math.h"
12 #include "limits.h" 12 #include "limits.h"
13 #include "block.h" 13 #include "block.h"
14 #include "onyx_int.h" 14 #include "onyx_int.h"
15 #include "variance.h" 15 #include "variance.h"
16 #include "encodeintra.h" 16 #include "encodeintra.h"
17 #include "vp8/common/setupintrarecon.h" 17 #include "vp8/common/setupintrarecon.h"
18 #include "mcomp.h" 18 #include "mcomp.h"
19 #include "firstpass.h"
19 #include "vpx_scale/vpxscale.h" 20 #include "vpx_scale/vpxscale.h"
20 #include "encodemb.h" 21 #include "encodemb.h"
21 #include "vp8/common/extend.h" 22 #include "vp8/common/extend.h"
22 #include "vp8/common/systemdependent.h" 23 #include "vp8/common/systemdependent.h"
23 #include "vpx_scale/yv12extend.h" 24 #include "vpx_scale/yv12extend.h"
24 #include "vpx_mem/vpx_mem.h" 25 #include "vpx_mem/vpx_mem.h"
25 #include "vp8/common/swapyv12buffer.h" 26 #include "vp8/common/swapyv12buffer.h"
26 #include <stdio.h> 27 #include <stdio.h>
27 #include "rdopt.h" 28 #include "rdopt.h"
28 #include "vp8/common/quant_common.h" 29 #include "vp8/common/quant_common.h"
29 #include "encodemv.h" 30 #include "encodemv.h"
30 31
31 //#define OUTPUT_FPF 1 32 //#define OUTPUT_FPF 1
32 33
33 #if CONFIG_RUNTIME_CPU_DETECT 34 #if CONFIG_RUNTIME_CPU_DETECT
34 #define IF_RTCD(x) (x) 35 #define IF_RTCD(x) (x)
35 #else 36 #else
36 #define IF_RTCD(x) NULL 37 #define IF_RTCD(x) NULL
37 #endif 38 #endif
38 39
39 extern void vp8_build_block_offsets(MACROBLOCK *x); 40 extern void vp8_build_block_offsets(MACROBLOCK *x);
40 extern void vp8_setup_block_ptrs(MACROBLOCK *x); 41 extern void vp8_setup_block_ptrs(MACROBLOCK *x);
41 extern void vp8cx_frame_init_quantizer(VP8_COMP *cpi); 42 extern void vp8cx_frame_init_quantizer(VP8_COMP *cpi);
42 extern void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, MV *mv) ; 43 extern void vp8_set_mbmode_and_mvs(MACROBLOCK *x, MB_PREDICTION_MODE mb, int_mv *mv);
43 extern void vp8_alloc_compressor_data(VP8_COMP *cpi); 44 extern void vp8_alloc_compressor_data(VP8_COMP *cpi);
44 45
45 //#define GFQ_ADJUSTMENT (40 + ((15*Q)/10)) 46 //#define GFQ_ADJUSTMENT (40 + ((15*Q)/10))
46 //#define GFQ_ADJUSTMENT (80 + ((15*Q)/10)) 47 //#define GFQ_ADJUSTMENT (80 + ((15*Q)/10))
47 #define GFQ_ADJUSTMENT vp8_gf_boost_qadjustment[Q] 48 #define GFQ_ADJUSTMENT vp8_gf_boost_qadjustment[Q]
48 extern int vp8_kf_boost_qadjustment[QINDEX_RANGE]; 49 extern int vp8_kf_boost_qadjustment[QINDEX_RANGE];
49 50
50 extern const int vp8_gf_boost_qadjustment[QINDEX_RANGE]; 51 extern const int vp8_gf_boost_qadjustment[QINDEX_RANGE];
51 52
52 #define IIFACTOR 1.4 53 #define IIFACTOR 1.5
53 #define IIKFACTOR1 1.40 54 #define IIKFACTOR1 1.40
54 #define IIKFACTOR2 1.5 55 #define IIKFACTOR2 1.5
55 #define RMAX 14.0 56 #define RMAX 14.0
56 #define GF_RMAX 48.0 57 #define GF_RMAX 48.0
57 58
58 #define KF_MB_INTRA_MIN 300 59 #define KF_MB_INTRA_MIN 300
59 #define GF_MB_INTRA_MIN 200 60 #define GF_MB_INTRA_MIN 200
60 61
61 #define DOUBLE_DIVIDE_CHECK(X) ((X)<0?(X)-.000001:(X)+.000001) 62 #define DOUBLE_DIVIDE_CHECK(X) ((X)<0?(X)-.000001:(X)+.000001)
62 63
63 #define POW1 (double)cpi->oxcf.two_pass_vbrbias/100.0 64 #define POW1 (double)cpi->oxcf.two_pass_vbrbias/100.0
64 #define POW2 (double)cpi->oxcf.two_pass_vbrbias/100.0 65 #define POW2 (double)cpi->oxcf.two_pass_vbrbias/100.0
65 66
67 #define NEW_BOOST 1
68
66 static int vscale_lookup[7] = {0, 1, 1, 2, 2, 3, 3}; 69 static int vscale_lookup[7] = {0, 1, 1, 2, 2, 3, 3};
67 static int hscale_lookup[7] = {0, 0, 1, 1, 2, 2, 3}; 70 static int hscale_lookup[7] = {0, 0, 1, 1, 2, 2, 3};
68 71
69 72
70 static const int cq_level[QINDEX_RANGE] = 73 static const int cq_level[QINDEX_RANGE] =
71 { 74 {
72 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9, 75 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
73 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20, 76 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
74 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31, 77 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
75 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43, 78 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
76 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56, 79 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
77 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70, 80 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
78 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85, 81 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
79 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100 82 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
80 }; 83 };
81 84
82 static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame); 85 static void find_next_key_frame(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame);
83 86
84 static int encode_intra(VP8_COMP *cpi, MACROBLOCK *x, int use_dc_pred)
85 {
86
87 int i;
88 int intra_pred_var = 0;
89 (void) cpi;
90
91 if (use_dc_pred)
92 {
93 x->e_mbd.mode_info_context->mbmi.mode = DC_PRED;
94 x->e_mbd.mode_info_context->mbmi.uv_mode = DC_PRED;
95 x->e_mbd.mode_info_context->mbmi.ref_frame = INTRA_FRAME;
96
97 vp8_encode_intra16x16mby(IF_RTCD(&cpi->rtcd), x);
98 }
99 else
100 {
101 for (i = 0; i < 16; i++)
102 {
103 BLOCKD *b = &x->e_mbd.block[i];
104 BLOCK *be = &x->block[i];
105
106 vp8_encode_intra4x4block(IF_RTCD(&cpi->rtcd), x, be, b, B_DC_PRED);
107 }
108 }
109
110 intra_pred_var = VARIANCE_INVOKE(&cpi->rtcd.variance, getmbss)(x->src_diff);
111
112 return intra_pred_var;
113 }
114
115 // Resets the first pass file to the given position using a relative seek from t he current position 87 // Resets the first pass file to the given position using a relative seek from t he current position
116 static void reset_fpf_position(VP8_COMP *cpi, FIRSTPASS_STATS *Position) 88 static void reset_fpf_position(VP8_COMP *cpi, FIRSTPASS_STATS *Position)
117 { 89 {
118 cpi->stats_in = Position; 90 cpi->twopass.stats_in = Position;
119 } 91 }
120 92
121 static int lookup_next_frame_stats(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame) 93 static int lookup_next_frame_stats(VP8_COMP *cpi, FIRSTPASS_STATS *next_frame)
122 { 94 {
123 if (cpi->stats_in >= cpi->stats_in_end) 95 if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
124 return EOF; 96 return EOF;
125 97
126 *next_frame = *cpi->stats_in; 98 *next_frame = *cpi->twopass.stats_in;
127 return 1; 99 return 1;
128 } 100 }
129 101
102 // Read frame stats at an offset from the current position
103 static int read_frame_stats( VP8_COMP *cpi,
104 FIRSTPASS_STATS *frame_stats,
105 int offset )
106 {
107 FIRSTPASS_STATS * fps_ptr = cpi->twopass.stats_in;
108
109 // Check legality of offset
110 if ( offset >= 0 )
111 {
112 if ( &fps_ptr[offset] >= cpi->twopass.stats_in_end )
113 return EOF;
114 }
115 else if ( offset < 0 )
116 {
117 if ( &fps_ptr[offset] < cpi->twopass.stats_in_start )
118 return EOF;
119 }
120
121 *frame_stats = fps_ptr[offset];
122 return 1;
123 }
124
125 static int input_stats(VP8_COMP *cpi, FIRSTPASS_STATS *fps)
126 {
127 if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
128 return EOF;
129
130 *fps = *cpi->twopass.stats_in;
131 cpi->twopass.stats_in =
132 (void*)((char *)cpi->twopass.stats_in + sizeof(FIRSTPASS_STATS));
133 return 1;
134 }
135
136 static void output_stats(const VP8_COMP *cpi,
137 struct vpx_codec_pkt_list *pktlist,
138 FIRSTPASS_STATS *stats)
139 {
140 struct vpx_codec_cx_pkt pkt;
141 pkt.kind = VPX_CODEC_STATS_PKT;
142 pkt.data.twopass_stats.buf = stats;
143 pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
144 vpx_codec_pkt_list_add(pktlist, &pkt);
145
146 // TEMP debug code
147 #if OUTPUT_FPF
148
149 {
150 FILE *fpfile;
151 fpfile = fopen("firstpass.stt", "a");
152
153 fprintf(fpfile, "%12.0f %12.0f %12.0f %12.4f %12.4f %12.4f %12.4f"
154 " %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f"
155 " %12.0f %12.4f\n",
156 stats->frame,
157 stats->intra_error,
158 stats->coded_error,
159 stats->ssim_weighted_pred_err,
160 stats->pcnt_inter,
161 stats->pcnt_motion,
162 stats->pcnt_second_ref,
163 stats->pcnt_neutral,
164 stats->MVr,
165 stats->mvr_abs,
166 stats->MVc,
167 stats->mvc_abs,
168 stats->MVrv,
169 stats->MVcv,
170 stats->mv_in_out_count,
171 stats->count,
172 stats->duration);
173 fclose(fpfile);
174 }
175 #endif
176 }
177
178 static void zero_stats(FIRSTPASS_STATS *section)
179 {
180 section->frame = 0.0;
181 section->intra_error = 0.0;
182 section->coded_error = 0.0;
183 section->ssim_weighted_pred_err = 0.0;
184 section->pcnt_inter = 0.0;
185 section->pcnt_motion = 0.0;
186 section->pcnt_second_ref = 0.0;
187 section->pcnt_neutral = 0.0;
188 section->MVr = 0.0;
189 section->mvr_abs = 0.0;
190 section->MVc = 0.0;
191 section->mvc_abs = 0.0;
192 section->MVrv = 0.0;
193 section->MVcv = 0.0;
194 section->mv_in_out_count = 0.0;
195 section->count = 0.0;
196 section->duration = 1.0;
197 }
198
199 static void accumulate_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame)
200 {
201 section->frame += frame->frame;
202 section->intra_error += frame->intra_error;
203 section->coded_error += frame->coded_error;
204 section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err;
205 section->pcnt_inter += frame->pcnt_inter;
206 section->pcnt_motion += frame->pcnt_motion;
207 section->pcnt_second_ref += frame->pcnt_second_ref;
208 section->pcnt_neutral += frame->pcnt_neutral;
209 section->MVr += frame->MVr;
210 section->mvr_abs += frame->mvr_abs;
211 section->MVc += frame->MVc;
212 section->mvc_abs += frame->mvc_abs;
213 section->MVrv += frame->MVrv;
214 section->MVcv += frame->MVcv;
215 section->mv_in_out_count += frame->mv_in_out_count;
216 section->count += frame->count;
217 section->duration += frame->duration;
218 }
219
220 static void avg_stats(FIRSTPASS_STATS *section)
221 {
222 if (section->count < 1.0)
223 return;
224
225 section->intra_error /= section->count;
226 section->coded_error /= section->count;
227 section->ssim_weighted_pred_err /= section->count;
228 section->pcnt_inter /= section->count;
229 section->pcnt_second_ref /= section->count;
230 section->pcnt_neutral /= section->count;
231 section->pcnt_motion /= section->count;
232 section->MVr /= section->count;
233 section->mvr_abs /= section->count;
234 section->MVc /= section->count;
235 section->mvc_abs /= section->count;
236 section->MVrv /= section->count;
237 section->MVcv /= section->count;
238 section->mv_in_out_count /= section->count;
239 section->duration /= section->count;
240 }
241
130 // Calculate a modified Error used in distributing bits between easier and harde r frames 242 // Calculate a modified Error used in distributing bits between easier and harde r frames
131 static double calculate_modified_err(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) 243 static double calculate_modified_err(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
132 { 244 {
133 double av_err = cpi->total_stats->ssim_weighted_pred_err; 245 double av_err = cpi->twopass.total_stats->ssim_weighted_pred_err;
134 double this_err = this_frame->ssim_weighted_pred_err; 246 double this_err = this_frame->ssim_weighted_pred_err;
135 double modified_err; 247 double modified_err;
136 248
137 //double relative_next_iiratio; 249 //double relative_next_iiratio;
138 //double next_iiratio; 250 //double next_iiratio;
139 //double sum_iiratio; 251 //double sum_iiratio;
140 //int i; 252 //int i;
141 253
142 //FIRSTPASS_STATS next_frame; 254 //FIRSTPASS_STATS next_frame;
143 //FIRSTPASS_STATS *start_pos; 255 //FIRSTPASS_STATS *start_pos;
144 256
145 /*start_pos = cpi->stats_in; 257 /*start_pos = cpi->twopass.stats_in;
146 sum_iiratio = 0.0; 258 sum_iiratio = 0.0;
147 i = 0; 259 i = 0;
148 while ( (i < 1) && input_stats(cpi,&next_frame) != EOF ) 260 while ( (i < 1) && input_stats(cpi,&next_frame) != EOF )
149 { 261 {
150 262
151 next_iiratio = next_frame.intra_error / DOUBLE_DIVIDE_CHECK(next_frame.c oded_error); 263 next_iiratio = next_frame.intra_error / DOUBLE_DIVIDE_CHECK(next_frame.c oded_error);
152 next_iiratio = ( next_iiratio < 1.0 ) ? 1.0 : (next_iiratio > 20.0) ? 20 .0 : next_iiratio; 264 next_iiratio = ( next_iiratio < 1.0 ) ? 1.0 : (next_iiratio > 20.0) ? 20 .0 : next_iiratio;
153 sum_iiratio += next_iiratio; 265 sum_iiratio += next_iiratio;
154 i++; 266 i++;
155 } 267 }
156 if ( i > 0 ) 268 if ( i > 0 )
157 { 269 {
158 relative_next_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK(cpi->avg_iirat io * (double)i); 270 relative_next_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK(cpi->twopass.a vg_iiratio * (double)i);
159 } 271 }
160 else 272 else
161 { 273 {
162 relative_next_iiratio = 1.0; 274 relative_next_iiratio = 1.0;
163 } 275 }
164 reset_fpf_position(cpi, start_pos);*/ 276 reset_fpf_position(cpi, start_pos);*/
165 277
166 if (this_err > av_err) 278 if (this_err > av_err)
167 modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), PO W1); 279 modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), PO W1);
168 else 280 else
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 max_bits = (int)(max_bits * buffer_fullness_ratio); 374 max_bits = (int)(max_bits * buffer_fullness_ratio);
263 375
264 if (max_bits < min_max_bits) 376 if (max_bits < min_max_bits)
265 max_bits = min_max_bits; // Lowest value we will set ... w hich should allow the buffer to refil. 377 max_bits = min_max_bits; // Lowest value we will set ... w hich should allow the buffer to refil.
266 } 378 }
267 } 379 }
268 // VBR 380 // VBR
269 else 381 else
270 { 382 {
271 // For VBR base this on the bits and frames left plus the two_pass_vbrma x_section rate passed in by the user 383 // For VBR base this on the bits and frames left plus the two_pass_vbrma x_section rate passed in by the user
272 max_bits = (int)(((double)cpi->bits_left / (cpi->total_stats->count - (d ouble)cpi->common.current_video_frame)) * ((double)cpi->oxcf.two_pass_vbrmax_sec tion / 100.0)); 384 max_bits = (int)(((double)cpi->twopass.bits_left / (cpi->twopass.total_s tats->count - (double)cpi->common.current_video_frame)) * ((double)cpi->oxcf.two _pass_vbrmax_section / 100.0));
273 } 385 }
274 386
275 // Trap case where we are out of bits 387 // Trap case where we are out of bits
276 if (max_bits < 0) 388 if (max_bits < 0)
277 max_bits = 0; 389 max_bits = 0;
278 390
279 return max_bits; 391 return max_bits;
280 } 392 }
281 393
282
283 static void output_stats(const VP8_COMP *cpi,
284 struct vpx_codec_pkt_list *pktlist,
285 FIRSTPASS_STATS *stats)
286 {
287 struct vpx_codec_cx_pkt pkt;
288 pkt.kind = VPX_CODEC_STATS_PKT;
289 pkt.data.twopass_stats.buf = stats;
290 pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
291 vpx_codec_pkt_list_add(pktlist, &pkt);
292
293 // TEMP debug code
294 #if OUTPUT_FPF
295
296 {
297 FILE *fpfile;
298 fpfile = fopen("firstpass.stt", "a");
299
300 fprintf(fpfile, "%12.0f %12.0f %12.0f %12.4f %12.4f %12.4f %12.4f"
301 " %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f"
302 " %12.0f %12.4f\n",
303 stats->frame,
304 stats->intra_error,
305 stats->coded_error,
306 stats->ssim_weighted_pred_err,
307 stats->pcnt_inter,
308 stats->pcnt_motion,
309 stats->pcnt_second_ref,
310 stats->pcnt_neutral,
311 stats->MVr,
312 stats->mvr_abs,
313 stats->MVc,
314 stats->mvc_abs,
315 stats->MVrv,
316 stats->MVcv,
317 stats->mv_in_out_count,
318 stats->count,
319 stats->duration);
320 fclose(fpfile);
321 }
322 #endif
323 }
324
325 static int input_stats(VP8_COMP *cpi, FIRSTPASS_STATS *fps)
326 {
327 if (cpi->stats_in >= cpi->stats_in_end)
328 return EOF;
329
330 *fps = *cpi->stats_in;
331 cpi->stats_in = (void*)((char *)cpi->stats_in + sizeof(FIRSTPASS_STATS));
332 return 1;
333 }
334
335 static void zero_stats(FIRSTPASS_STATS *section)
336 {
337 section->frame = 0.0;
338 section->intra_error = 0.0;
339 section->coded_error = 0.0;
340 section->ssim_weighted_pred_err = 0.0;
341 section->pcnt_inter = 0.0;
342 section->pcnt_motion = 0.0;
343 section->pcnt_second_ref = 0.0;
344 section->pcnt_neutral = 0.0;
345 section->MVr = 0.0;
346 section->mvr_abs = 0.0;
347 section->MVc = 0.0;
348 section->mvc_abs = 0.0;
349 section->MVrv = 0.0;
350 section->MVcv = 0.0;
351 section->mv_in_out_count = 0.0;
352 section->count = 0.0;
353 section->duration = 1.0;
354 }
355 static void accumulate_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame)
356 {
357 section->frame += frame->frame;
358 section->intra_error += frame->intra_error;
359 section->coded_error += frame->coded_error;
360 section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err;
361 section->pcnt_inter += frame->pcnt_inter;
362 section->pcnt_motion += frame->pcnt_motion;
363 section->pcnt_second_ref += frame->pcnt_second_ref;
364 section->pcnt_neutral += frame->pcnt_neutral;
365 section->MVr += frame->MVr;
366 section->mvr_abs += frame->mvr_abs;
367 section->MVc += frame->MVc;
368 section->mvc_abs += frame->mvc_abs;
369 section->MVrv += frame->MVrv;
370 section->MVcv += frame->MVcv;
371 section->mv_in_out_count += frame->mv_in_out_count;
372 section->count += frame->count;
373 section->duration += frame->duration;
374 }
375 static void avg_stats(FIRSTPASS_STATS *section)
376 {
377 if (section->count < 1.0)
378 return;
379
380 section->intra_error /= section->count;
381 section->coded_error /= section->count;
382 section->ssim_weighted_pred_err /= section->count;
383 section->pcnt_inter /= section->count;
384 section->pcnt_second_ref /= section->count;
385 section->pcnt_neutral /= section->count;
386 section->pcnt_motion /= section->count;
387 section->MVr /= section->count;
388 section->mvr_abs /= section->count;
389 section->MVc /= section->count;
390 section->mvc_abs /= section->count;
391 section->MVrv /= section->count;
392 section->MVcv /= section->count;
393 section->mv_in_out_count /= section->count;
394 section->duration /= section->count;
395 }
396
397 void vp8_init_first_pass(VP8_COMP *cpi) 394 void vp8_init_first_pass(VP8_COMP *cpi)
398 { 395 {
399 zero_stats(cpi->total_stats); 396 zero_stats(cpi->twopass.total_stats);
400 } 397 }
401 398
402 void vp8_end_first_pass(VP8_COMP *cpi) 399 void vp8_end_first_pass(VP8_COMP *cpi)
403 { 400 {
404 output_stats(cpi, cpi->output_pkt_list, cpi->total_stats); 401 output_stats(cpi, cpi->output_pkt_list, cpi->twopass.total_stats);
405 } 402 }
406 403
407 static void zz_motion_search( VP8_COMP *cpi, MACROBLOCK * x, YV12_BUFFER_CONFIG * recon_buffer, int * best_motion_err, int recon_yoffset ) 404 static void zz_motion_search( VP8_COMP *cpi, MACROBLOCK * x, YV12_BUFFER_CONFIG * recon_buffer, int * best_motion_err, int recon_yoffset )
408 { 405 {
409 MACROBLOCKD * const xd = & x->e_mbd; 406 MACROBLOCKD * const xd = & x->e_mbd;
410 BLOCK *b = &x->block[0]; 407 BLOCK *b = &x->block[0];
411 BLOCKD *d = &x->e_mbd.block[0]; 408 BLOCKD *d = &x->e_mbd.block[0];
412 409
413 unsigned char *src_ptr = (*(b->base_src) + b->src); 410 unsigned char *src_ptr = (*(b->base_src) + b->src);
414 int src_stride = b->src_stride; 411 int src_stride = b->src_stride;
415 unsigned char *ref_ptr; 412 unsigned char *ref_ptr;
416 int ref_stride=d->pre_stride; 413 int ref_stride=d->pre_stride;
417 414
418 // Set up pointers for this macro block recon buffer 415 // Set up pointers for this macro block recon buffer
419 xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset; 416 xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
420 417
421 ref_ptr = (unsigned char *)(*(d->base_pre) + d->pre ); 418 ref_ptr = (unsigned char *)(*(d->base_pre) + d->pre );
422 419
423 VARIANCE_INVOKE(IF_RTCD(&cpi->rtcd.variance), mse16x16) ( src_ptr, src_strid e, ref_ptr, ref_stride, (unsigned int *)(best_motion_err)); 420 VARIANCE_INVOKE(IF_RTCD(&cpi->rtcd.variance), mse16x16) ( src_ptr, src_strid e, ref_ptr, ref_stride, (unsigned int *)(best_motion_err));
424 } 421 }
425 422
426 static void first_pass_motion_search(VP8_COMP *cpi, MACROBLOCK *x, MV *ref_mv, M V *best_mv, YV12_BUFFER_CONFIG *recon_buffer, int *best_motion_err, int recon_yo ffset ) 423 static void first_pass_motion_search(VP8_COMP *cpi, MACROBLOCK *x,
424 int_mv *ref_mv, MV *best_mv,
425 YV12_BUFFER_CONFIG *recon_buffer,
426 int *best_motion_err, int recon_yoffset )
427 { 427 {
428 MACROBLOCKD *const xd = & x->e_mbd; 428 MACROBLOCKD *const xd = & x->e_mbd;
429 BLOCK *b = &x->block[0]; 429 BLOCK *b = &x->block[0];
430 BLOCKD *d = &x->e_mbd.block[0]; 430 BLOCKD *d = &x->e_mbd.block[0];
431 int num00; 431 int num00;
432 432
433 MV tmp_mv = {0, 0}; 433 int_mv tmp_mv;
434 int_mv ref_mv_full;
434 435
435 int tmp_err; 436 int tmp_err;
436 int step_param = 3; //3; // D ont search over full range for first pass 437 int step_param = 3; //3; // D ont search over full range for first pass
437 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; //3; 438 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; //3;
438 int n; 439 int n;
439 vp8_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16]; 440 vp8_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
440 int new_mv_mode_penalty = 256; 441 int new_mv_mode_penalty = 256;
441 442
442 // override the default variance function to use MSE 443 // override the default variance function to use MSE
443 v_fn_ptr.vf = VARIANCE_INVOKE(IF_RTCD(&cpi->rtcd.variance), mse16x16); 444 v_fn_ptr.vf = VARIANCE_INVOKE(IF_RTCD(&cpi->rtcd.variance), mse16x16);
444 445
445 // Set up pointers for this macro block recon buffer 446 // Set up pointers for this macro block recon buffer
446 xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset; 447 xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
447 448
448 // Initial step/diamond search centred on best mv 449 // Initial step/diamond search centred on best mv
449 tmp_err = cpi->diamond_search_sad(x, b, d, ref_mv, &tmp_mv, step_param, x->e rrorperbit, &num00, &v_fn_ptr, x->mvsadcost, x->mvcost, ref_mv); 450 tmp_mv.as_int = 0;
451 ref_mv_full.as_mv.col = ref_mv->as_mv.col>>3;
452 ref_mv_full.as_mv.row = ref_mv->as_mv.row>>3;
453 tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv, step_param ,
454 x->sadperbit16, &num00, &v_fn_ptr,
455 x->mvcost, ref_mv);
450 if ( tmp_err < INT_MAX-new_mv_mode_penalty ) 456 if ( tmp_err < INT_MAX-new_mv_mode_penalty )
451 tmp_err += new_mv_mode_penalty; 457 tmp_err += new_mv_mode_penalty;
452 458
453 if (tmp_err < *best_motion_err) 459 if (tmp_err < *best_motion_err)
454 { 460 {
455 *best_motion_err = tmp_err; 461 *best_motion_err = tmp_err;
456 best_mv->row = tmp_mv.row; 462 best_mv->row = tmp_mv.as_mv.row;
457 best_mv->col = tmp_mv.col; 463 best_mv->col = tmp_mv.as_mv.col;
458 } 464 }
459 465
460 // Further step/diamond searches as necessary 466 // Further step/diamond searches as necessary
461 n = num00; 467 n = num00;
462 num00 = 0; 468 num00 = 0;
463 469
464 while (n < further_steps) 470 while (n < further_steps)
465 { 471 {
466 n++; 472 n++;
467 473
468 if (num00) 474 if (num00)
469 num00--; 475 num00--;
470 else 476 else
471 { 477 {
472 tmp_err = cpi->diamond_search_sad(x, b, d, ref_mv, &tmp_mv, step_par am + n, x->errorperbit, &num00, &v_fn_ptr, x->mvsadcost, x->mvcost, ref_mv); 478 tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv,
479 step_param + n, x->sadperbit16,
480 &num00, &v_fn_ptr, x->mvcost,
481 ref_mv);
473 if ( tmp_err < INT_MAX-new_mv_mode_penalty ) 482 if ( tmp_err < INT_MAX-new_mv_mode_penalty )
474 tmp_err += new_mv_mode_penalty; 483 tmp_err += new_mv_mode_penalty;
475 484
476 if (tmp_err < *best_motion_err) 485 if (tmp_err < *best_motion_err)
477 { 486 {
478 *best_motion_err = tmp_err; 487 *best_motion_err = tmp_err;
479 best_mv->row = tmp_mv.row; 488 best_mv->row = tmp_mv.as_mv.row;
480 best_mv->col = tmp_mv.col; 489 best_mv->col = tmp_mv.as_mv.col;
481 } 490 }
482 } 491 }
483 } 492 }
484 } 493 }
485 494
486 void vp8_first_pass(VP8_COMP *cpi) 495 void vp8_first_pass(VP8_COMP *cpi)
487 { 496 {
488 int mb_row, mb_col; 497 int mb_row, mb_col;
489 MACROBLOCK *const x = & cpi->mb; 498 MACROBLOCK *const x = & cpi->mb;
490 VP8_COMMON *const cm = & cpi->common; 499 VP8_COMMON *const cm = & cpi->common;
491 MACROBLOCKD *const xd = & x->e_mbd; 500 MACROBLOCKD *const xd = & x->e_mbd;
492 501
493 int col_blocks = 4 * cm->mb_cols;
494 int recon_yoffset, recon_uvoffset; 502 int recon_yoffset, recon_uvoffset;
495 YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx]; 503 YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx];
496 YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx]; 504 YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
497 YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx]; 505 YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx];
498 int recon_y_stride = lst_yv12->y_stride; 506 int recon_y_stride = lst_yv12->y_stride;
499 int recon_uv_stride = lst_yv12->uv_stride; 507 int recon_uv_stride = lst_yv12->uv_stride;
500 long long intra_error = 0; 508 int64_t intra_error = 0;
501 long long coded_error = 0; 509 int64_t coded_error = 0;
502 510
503 int sum_mvr = 0, sum_mvc = 0; 511 int sum_mvr = 0, sum_mvc = 0;
504 int sum_mvr_abs = 0, sum_mvc_abs = 0; 512 int sum_mvr_abs = 0, sum_mvc_abs = 0;
505 int sum_mvrs = 0, sum_mvcs = 0; 513 int sum_mvrs = 0, sum_mvcs = 0;
506 int mvcount = 0; 514 int mvcount = 0;
507 int intercount = 0; 515 int intercount = 0;
508 int second_ref_count = 0; 516 int second_ref_count = 0;
509 int intrapenalty = 256; 517 int intrapenalty = 256;
510 int neutral_count = 0; 518 int neutral_count = 0;
511 519
512 int sum_in_vectors = 0; 520 int sum_in_vectors = 0;
513 521
514 MV zero_ref_mv = {0, 0}; 522 int_mv zero_ref_mv;
523
524 zero_ref_mv.as_int = 0;
515 525
516 vp8_clear_system_state(); //__asm emms; 526 vp8_clear_system_state(); //__asm emms;
517 527
518 x->src = * cpi->Source; 528 x->src = * cpi->Source;
519 xd->pre = *lst_yv12; 529 xd->pre = *lst_yv12;
520 xd->dst = *new_yv12; 530 xd->dst = *new_yv12;
521 531
522 x->partition_info = x->pi; 532 x->partition_info = x->pi;
523 533
524 xd->mode_info_context = cm->mi; 534 xd->mode_info_context = cm->mi;
525 535
526 vp8_build_block_offsets(x); 536 vp8_build_block_offsets(x);
527 537
528 vp8_setup_block_dptrs(&x->e_mbd); 538 vp8_setup_block_dptrs(&x->e_mbd);
529 539
530 vp8_setup_block_ptrs(x); 540 vp8_setup_block_ptrs(x);
531 541
532 // set up frame new frame for intra coded blocks 542 // set up frame new frame for intra coded blocks
533 vp8_setup_intra_recon(new_yv12); 543 vp8_setup_intra_recon(new_yv12);
534 vp8cx_frame_init_quantizer(cpi); 544 vp8cx_frame_init_quantizer(cpi);
535 545
536 // Initialise the MV cost table to the defaults 546 // Initialise the MV cost table to the defaults
537 //if( cm->current_video_frame == 0) 547 //if( cm->current_video_frame == 0)
538 //if ( 0 ) 548 //if ( 0 )
539 { 549 {
540 int flag[2] = {1, 1}; 550 int flag[2] = {1, 1};
541 vp8_initialize_rd_consts(cpi, vp8_dc_quant(cm->base_qindex, cm->y1dc_del ta_q)); 551 vp8_initialize_rd_consts(cpi, vp8_dc_quant(cm->base_qindex, cm->y1dc_del ta_q));
542 vpx_memcpy(cm->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_con text)); 552 vpx_memcpy(cm->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_con text));
543 vp8_build_component_cost_table(cpi->mb.mvcost, cpi->mb.mvsadcost, (const MV_CONTEXT *) cm->fc.mvc, flag); 553 vp8_build_component_cost_table(cpi->mb.mvcost, (const MV_CONTEXT *) cm-> fc.mvc, flag);
544 } 554 }
545 555
546 // for each macroblock row in image 556 // for each macroblock row in image
547 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) 557 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
548 { 558 {
549 int_mv best_ref_mv; 559 int_mv best_ref_mv;
550 560
551 best_ref_mv.as_int = 0; 561 best_ref_mv.as_int = 0;
552 562
553 // reset above block coeffs 563 // reset above block coeffs
554 xd->up_available = (mb_row != 0); 564 xd->up_available = (mb_row != 0);
555 recon_yoffset = (mb_row * recon_y_stride * 16); 565 recon_yoffset = (mb_row * recon_y_stride * 16);
556 recon_uvoffset = (mb_row * recon_uv_stride * 8); 566 recon_uvoffset = (mb_row * recon_uv_stride * 8);
557 567
558 // Set up limit values for motion vectors to prevent them extending outs ide the UMV borders 568 // Set up limit values for motion vectors to prevent them extending outs ide the UMV borders
559 x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16)); 569 x->mv_row_min = -((mb_row * 16) + (VP8BORDERINPIXELS - 16));
560 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16); 570 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) + (VP8BORDERINPIXELS - 16);
561 571
562 572
563 // for each macroblock col in image 573 // for each macroblock col in image
564 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) 574 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
565 { 575 {
566 int this_error; 576 int this_error;
567 int zz_to_best_ratio;
568 int gf_motion_error = INT_MAX; 577 int gf_motion_error = INT_MAX;
569 int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row); 578 int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
570 579
571 xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset; 580 xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset;
572 xd->dst.u_buffer = new_yv12->u_buffer + recon_uvoffset; 581 xd->dst.u_buffer = new_yv12->u_buffer + recon_uvoffset;
573 xd->dst.v_buffer = new_yv12->v_buffer + recon_uvoffset; 582 xd->dst.v_buffer = new_yv12->v_buffer + recon_uvoffset;
574 xd->left_available = (mb_col != 0); 583 xd->left_available = (mb_col != 0);
575 584
585 //Copy current mb to a buffer
586 RECON_INVOKE(&xd->rtcd->recon, copy16x16)(x->src.y_buffer, x->src.y_ stride, x->thismb, 16);
587
576 // do intra 16x16 prediction 588 // do intra 16x16 prediction
577 this_error = encode_intra(cpi, x, use_dc_pred); 589 this_error = vp8_encode_intra(cpi, x, use_dc_pred);
578 590
579 // "intrapenalty" below deals with situations where the intra and in ter error scores are very low (eg a plain black frame) 591 // "intrapenalty" below deals with situations where the intra and in ter error scores are very low (eg a plain black frame)
580 // We do not have special cases in first pass for 0,0 and nearest et c so all inter modes carry an overhead cost estimate fot the mv. 592 // We do not have special cases in first pass for 0,0 and nearest et c so all inter modes carry an overhead cost estimate fot the mv.
581 // When the error score is very low this causes us to pick all or lo ts of INTRA modes and throw lots of key frames. 593 // When the error score is very low this causes us to pick all or lo ts of INTRA modes and throw lots of key frames.
582 // This penalty adds a cost matching that of a 0,0 mv to the intra c ase. 594 // This penalty adds a cost matching that of a 0,0 mv to the intra c ase.
583 this_error += intrapenalty; 595 this_error += intrapenalty;
584 596
585 // Cumulative intra error total 597 // Cumulative intra error total
586 intra_error += (long long)this_error; 598 intra_error += (int64_t)this_error;
587 599
588 // Set up limit values for motion vectors to prevent them extending outside the UMV borders 600 // Set up limit values for motion vectors to prevent them extending outside the UMV borders
589 x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16)); 601 x->mv_col_min = -((mb_col * 16) + (VP8BORDERINPIXELS - 16));
590 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXE LS - 16); 602 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + (VP8BORDERINPIXE LS - 16);
591 603
592 // Other than for the first frame do a motion search 604 // Other than for the first frame do a motion search
593 if (cm->current_video_frame > 0) 605 if (cm->current_video_frame > 0)
594 { 606 {
595 BLOCK *b = &x->block[0];
596 BLOCKD *d = &x->e_mbd.block[0]; 607 BLOCKD *d = &x->e_mbd.block[0];
597 MV tmp_mv = {0, 0}; 608 MV tmp_mv = {0, 0};
598 int tmp_err; 609 int tmp_err;
599 int motion_error = INT_MAX; 610 int motion_error = INT_MAX;
600 611
601 // Simple 0,0 motion with no mv overhead 612 // Simple 0,0 motion with no mv overhead
602 zz_motion_search( cpi, x, lst_yv12, &motion_error, recon_yoffset ); 613 zz_motion_search( cpi, x, lst_yv12, &motion_error, recon_yoffset );
603 d->bmi.mv.as_mv.row = 0; 614 d->bmi.mv.as_mv.row = 0;
604 d->bmi.mv.as_mv.col = 0; 615 d->bmi.mv.as_mv.col = 0;
605 616
606 // Test last reference frame using the previous best mv as the 617 // Test last reference frame using the previous best mv as the
607 // starting point (best reference) for the search 618 // starting point (best reference) for the search
608 first_pass_motion_search(cpi, x, &best_ref_mv.as_mv, 619 first_pass_motion_search(cpi, x, &best_ref_mv,
609 &d->bmi.mv.as_mv, lst_yv12, 620 &d->bmi.mv.as_mv, lst_yv12,
610 &motion_error, recon_yoffset); 621 &motion_error, recon_yoffset);
611 622
612 // If the current best reference mv is not centred on 0,0 then d o a 0,0 based search as well 623 // If the current best reference mv is not centred on 0,0 then d o a 0,0 based search as well
613 if (best_ref_mv.as_int) 624 if (best_ref_mv.as_int)
614 { 625 {
615 tmp_err = INT_MAX; 626 tmp_err = INT_MAX;
616 first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv, 627 first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv,
617 lst_yv12, &tmp_err, recon_yoffset); 628 lst_yv12, &tmp_err, recon_yoffset);
618 629
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 if( (((this_error-intrapenalty) * 9) <= 673 if( (((this_error-intrapenalty) * 9) <=
663 (motion_error*10)) && 674 (motion_error*10)) &&
664 (this_error < (2*intrapenalty)) ) 675 (this_error < (2*intrapenalty)) )
665 { 676 {
666 neutral_count++; 677 neutral_count++;
667 } 678 }
668 679
669 d->bmi.mv.as_mv.row <<= 3; 680 d->bmi.mv.as_mv.row <<= 3;
670 d->bmi.mv.as_mv.col <<= 3; 681 d->bmi.mv.as_mv.col <<= 3;
671 this_error = motion_error; 682 this_error = motion_error;
672 vp8_set_mbmode_and_mvs(x, NEWMV, &d->bmi.mv.as_mv); 683 vp8_set_mbmode_and_mvs(x, NEWMV, &d->bmi.mv);
673 vp8_encode_inter16x16y(IF_RTCD(&cpi->rtcd), x); 684 vp8_encode_inter16x16y(IF_RTCD(&cpi->rtcd), x);
674 sum_mvr += d->bmi.mv.as_mv.row; 685 sum_mvr += d->bmi.mv.as_mv.row;
675 sum_mvr_abs += abs(d->bmi.mv.as_mv.row); 686 sum_mvr_abs += abs(d->bmi.mv.as_mv.row);
676 sum_mvc += d->bmi.mv.as_mv.col; 687 sum_mvc += d->bmi.mv.as_mv.col;
677 sum_mvc_abs += abs(d->bmi.mv.as_mv.col); 688 sum_mvc_abs += abs(d->bmi.mv.as_mv.col);
678 sum_mvrs += d->bmi.mv.as_mv.row * d->bmi.mv.as_mv.row; 689 sum_mvrs += d->bmi.mv.as_mv.row * d->bmi.mv.as_mv.row;
679 sum_mvcs += d->bmi.mv.as_mv.col * d->bmi.mv.as_mv.col; 690 sum_mvcs += d->bmi.mv.as_mv.col * d->bmi.mv.as_mv.col;
680 intercount++; 691 intercount++;
681 692
682 best_ref_mv.as_int = d->bmi.mv.as_int; 693 best_ref_mv.as_int = d->bmi.mv.as_int;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
714 { 725 {
715 if (d->bmi.mv.as_mv.col > 0) 726 if (d->bmi.mv.as_mv.col > 0)
716 sum_in_vectors++; 727 sum_in_vectors++;
717 else if (d->bmi.mv.as_mv.col < 0) 728 else if (d->bmi.mv.as_mv.col < 0)
718 sum_in_vectors--; 729 sum_in_vectors--;
719 } 730 }
720 } 731 }
721 } 732 }
722 } 733 }
723 734
724 coded_error += (long long)this_error; 735 coded_error += (int64_t)this_error;
725 736
726 // adjust to the next column of macroblocks 737 // adjust to the next column of macroblocks
727 x->src.y_buffer += 16; 738 x->src.y_buffer += 16;
728 x->src.u_buffer += 8; 739 x->src.u_buffer += 8;
729 x->src.v_buffer += 8; 740 x->src.v_buffer += 8;
730 741
731 recon_yoffset += 16; 742 recon_yoffset += 16;
732 recon_uvoffset += 8; 743 recon_uvoffset += 8;
733 } 744 }
734 745
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
782 fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount; 793 fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount;
783 fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount) ) / (double)mvcount; 794 fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount) ) / (double)mvcount;
784 fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount) ) / (double)mvcount; 795 fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount) ) / (double)mvcount;
785 fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2) ; 796 fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2) ;
786 797
787 fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs; 798 fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs;
788 } 799 }
789 800
790 // TODO: handle the case when duration is set to 0, or something less 801 // TODO: handle the case when duration is set to 0, or something less
791 // than the full time between subsequent cpi->source_time_stamp s . 802 // than the full time between subsequent cpi->source_time_stamp s .
792 fps.duration = cpi->source_end_time_stamp - cpi->source_time_stamp; 803 fps.duration = cpi->source->ts_end
804 - cpi->source->ts_start;
793 805
794 // don't want to do output stats with a stack variable! 806 // don't want to do output stats with a stack variable!
795 memcpy(cpi->this_frame_stats, 807 memcpy(cpi->twopass.this_frame_stats,
796 &fps, 808 &fps,
797 sizeof(FIRSTPASS_STATS)); 809 sizeof(FIRSTPASS_STATS));
798 output_stats(cpi, cpi->output_pkt_list, cpi->this_frame_stats); 810 output_stats(cpi, cpi->output_pkt_list, cpi->twopass.this_frame_stats);
799 accumulate_stats(cpi->total_stats, &fps); 811 accumulate_stats(cpi->twopass.total_stats, &fps);
800 } 812 }
801 813
802 // Copy the previous Last Frame into the GF buffer if specific conditions fo r doing so are met 814 // Copy the previous Last Frame into the GF buffer if specific conditions fo r doing so are met
803 if ((cm->current_video_frame > 0) && 815 if ((cm->current_video_frame > 0) &&
804 (cpi->this_frame_stats->pcnt_inter > 0.20) && 816 (cpi->twopass.this_frame_stats->pcnt_inter > 0.20) &&
805 ((cpi->this_frame_stats->intra_error / cpi->this_frame_stats->coded_erro r) > 2.0)) 817 ((cpi->twopass.this_frame_stats->intra_error / cpi->twopass.this_frame_s tats->coded_error) > 2.0))
806 { 818 {
807 vp8_yv12_copy_frame_ptr(lst_yv12, gld_yv12); 819 vp8_yv12_copy_frame_ptr(lst_yv12, gld_yv12);
808 } 820 }
809 821
810 // swap frame pointers so last frame refers to the frame we just compressed 822 // swap frame pointers so last frame refers to the frame we just compressed
811 vp8_swap_yv12_buffer(lst_yv12, new_yv12); 823 vp8_swap_yv12_buffer(lst_yv12, new_yv12);
812 vp8_yv12_extend_frame_borders(lst_yv12); 824 vp8_yv12_extend_frame_borders(lst_yv12);
813 825
814 // Special case for the first frame. Copy into the GF buffer as a second ref erence. 826 // Special case for the first frame. Copy into the GF buffer as a second ref erence.
815 if (cm->current_video_frame == 0) 827 if (cm->current_video_frame == 0)
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
849 double err_per_mb = section_err / num_mbs; 861 double err_per_mb = section_err / num_mbs;
850 double correction_factor; 862 double correction_factor;
851 double corr_high; 863 double corr_high;
852 double speed_correction = 1.0; 864 double speed_correction = 1.0;
853 double rolling_ratio; 865 double rolling_ratio;
854 866
855 double pow_highq = 0.90; 867 double pow_highq = 0.90;
856 double pow_lowq = 0.40; 868 double pow_lowq = 0.40;
857 869
858 if (section_target_bandwitdh <= 0) 870 if (section_target_bandwitdh <= 0)
859 return cpi->maxq_max_limit; // Highest value allowed 871 return cpi->twopass.maxq_max_limit; // Highest value allowed
860 872
861 target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) ? (512 * se ction_target_bandwitdh) / num_mbs : 512 * (section_target_bandwitdh / num_mbs); 873 target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20)) ? (512 * se ction_target_bandwitdh) / num_mbs : 512 * (section_target_bandwitdh / num_mbs);
862 874
863 // Calculate a corrective factor based on a rolling ratio of bits spent vs t arget bits 875 // Calculate a corrective factor based on a rolling ratio of bits spent vs t arget bits
864 if ((cpi->rolling_target_bits > 0.0) && (cpi->active_worst_quality < cpi->wo rst_quality)) 876 if ((cpi->rolling_target_bits > 0.0) && (cpi->active_worst_quality < cpi->wo rst_quality))
865 { 877 {
866 //double adjustment_rate = 0.985 + (0.00005 * cpi->active_worst_quality) ;
867 double adjustment_rate = 0.99;
868
869 rolling_ratio = (double)cpi->rolling_actual_bits / (double)cpi->rolling_ target_bits; 878 rolling_ratio = (double)cpi->rolling_actual_bits / (double)cpi->rolling_ target_bits;
870 879
871 //if ( cpi->est_max_qcorrection_factor > rolling_ratio ) 880 //if ( cpi->twopass.est_max_qcorrection_factor > rolling_ratio )
872 if (rolling_ratio < 0.95) 881 if (rolling_ratio < 0.95)
873 //cpi->est_max_qcorrection_factor *= adjustment_rate; 882 //cpi->twopass.est_max_qcorrection_factor *= adjustment_rate;
874 cpi->est_max_qcorrection_factor -= 0.005; 883 cpi->twopass.est_max_qcorrection_factor -= 0.005;
875 //else if ( cpi->est_max_qcorrection_factor < rolling_ratio ) 884 //else if ( cpi->twopass.est_max_qcorrection_factor < rolling_ratio )
876 else if (rolling_ratio > 1.05) 885 else if (rolling_ratio > 1.05)
877 cpi->est_max_qcorrection_factor += 0.005; 886 cpi->twopass.est_max_qcorrection_factor += 0.005;
878 887
879 //cpi->est_max_qcorrection_factor /= adjustment_rate; 888 //cpi->twopass.est_max_qcorrection_factor /= adjustment_rate;
880 889
881 cpi->est_max_qcorrection_factor = (cpi->est_max_qcorrection_factor < 0.1 ) ? 0.1 : (cpi->est_max_qcorrection_factor > 10.0) ? 10.0 : cpi->est_max_qcorrec tion_factor; 890 cpi->twopass.est_max_qcorrection_factor =
891 (cpi->twopass.est_max_qcorrection_factor < 0.1)
892 ? 0.1
893 : (cpi->twopass.est_max_qcorrection_factor > 10.0)
894 ? 10.0 : cpi->twopass.est_max_qcorrection_factor;
882 } 895 }
883 896
884 // Corrections for higher compression speed settings (reduced compression ex pected) 897 // Corrections for higher compression speed settings (reduced compression ex pected)
885 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) 898 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
886 { 899 {
887 if (cpi->oxcf.cpu_used <= 5) 900 if (cpi->oxcf.cpu_used <= 5)
888 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04); 901 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
889 else 902 else
890 speed_correction = 1.25; 903 speed_correction = 1.25;
891 } 904 }
892 905
893 // Correction factor used for Q values >= 20 906 // Correction factor used for Q values >= 20
894 corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq); 907 corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq);
895 corr_high = (corr_high < 0.05) 908 corr_high = (corr_high < 0.05)
896 ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high; 909 ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high;
897 910
898 // Try and pick a max Q that will be high enough to encode the 911 // Try and pick a max Q that will be high enough to encode the
899 // content at the given rate. 912 // content at the given rate.
900 for (Q = cpi->maxq_min_limit; Q < cpi->maxq_max_limit; Q++) 913 for (Q = cpi->twopass.maxq_min_limit; Q < cpi->twopass.maxq_max_limit; Q++)
901 { 914 {
902 int bits_per_mb_at_this_q; 915 int bits_per_mb_at_this_q;
903 916
904 if (Q < 50) 917 if (Q < 50)
905 { 918 {
906 correction_factor = pow(err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01)); 919 correction_factor = pow(err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01));
907 correction_factor = (correction_factor < 0.05) ? 0.05 : (correction_ factor > 5.0) ? 5.0 : correction_factor; 920 correction_factor = (correction_factor < 0.05) ? 0.05 : (correction_ factor > 5.0) ? 5.0 : correction_factor;
908 } 921 }
909 else 922 else
910 correction_factor = corr_high; 923 correction_factor = corr_high;
911 924
912 bits_per_mb_at_this_q = (int)(.5 + correction_factor * speed_correction * cpi->est_max_qcorrection_factor * cpi->section_max_qfactor * (double)vp8_bits_ per_mb[INTER_FRAME][Q] / 1.0); 925 bits_per_mb_at_this_q = (int)(.5 + correction_factor
913 //bits_per_mb_at_this_q = (int)(.5 + correction_factor * speed_correctio n * cpi->est_max_qcorrection_factor * (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0); 926 * speed_correction * cpi->twopass.est_max_qcorrection_factor
927 * cpi->twopass.section_max_qfactor
928 * (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1.0);
914 929
915 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) 930 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
916 break; 931 break;
917 } 932 }
918 933
919 // Restriction on active max q for constrained quality mode. 934 // Restriction on active max q for constrained quality mode.
920 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) && 935 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
921 (Q < cpi->cq_target_quality) ) 936 (Q < cpi->cq_target_quality) )
922 //(Q < cpi->oxcf.cq_level;) ) 937 //(Q < cpi->oxcf.cq_level;) )
923 { 938 {
924 Q = cpi->cq_target_quality; 939 Q = cpi->cq_target_quality;
925 //Q = cpi->oxcf.cq_level; 940 //Q = cpi->oxcf.cq_level;
926 } 941 }
927 942
928 // Adjust maxq_min_limit and maxq_max_limit limits based on 943 // Adjust maxq_min_limit and maxq_max_limit limits based on
929 // averaga q observed in clip for non kf/gf.arf frames 944 // averaga q observed in clip for non kf/gf.arf frames
930 // Give average a chance to settle though. 945 // Give average a chance to settle though.
931 if ( (cpi->ni_frames > 946 if ( (cpi->ni_frames >
932 ((unsigned int)cpi->total_stats->count >> 8)) && 947 ((unsigned int)cpi->twopass.total_stats->count >> 8)) &&
933 (cpi->ni_frames > 150) ) 948 (cpi->ni_frames > 150) )
934 { 949 {
935 cpi->maxq_max_limit = ((cpi->ni_av_qi + 32) < cpi->worst_quality) 950 cpi->twopass.maxq_max_limit = ((cpi->ni_av_qi + 32) < cpi->worst_quality )
936 ? (cpi->ni_av_qi + 32) : cpi->worst_quality; 951 ? (cpi->ni_av_qi + 32) : cpi->worst_quality;
937 cpi->maxq_min_limit = ((cpi->ni_av_qi - 32) > cpi->best_quality) 952 cpi->twopass.maxq_min_limit = ((cpi->ni_av_qi - 32) > cpi->best_quality)
938 ? (cpi->ni_av_qi - 32) : cpi->best_quality; 953 ? (cpi->ni_av_qi - 32) : cpi->best_quality;
939 } 954 }
940 955
941 return Q; 956 return Q;
942 } 957 }
943 static int estimate_q(VP8_COMP *cpi, double section_err, int section_target_band witdh) 958 static int estimate_q(VP8_COMP *cpi, double section_err, int section_target_band witdh)
944 { 959 {
945 int Q; 960 int Q;
946 int num_mbs = cpi->common.MBs; 961 int num_mbs = cpi->common.MBs;
947 int target_norm_bits_per_mb; 962 int target_norm_bits_per_mb;
(...skipping 26 matching lines...) Expand all
974 int bits_per_mb_at_this_q; 989 int bits_per_mb_at_this_q;
975 990
976 if (Q < 50) 991 if (Q < 50)
977 { 992 {
978 correction_factor = pow(err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01)); 993 correction_factor = pow(err_per_mb / BASE_ERRPERMB, (pow_lowq + Q * 0.01));
979 correction_factor = (correction_factor < 0.05) ? 0.05 : (correction_ factor > 5.0) ? 5.0 : correction_factor; 994 correction_factor = (correction_factor < 0.05) ? 0.05 : (correction_ factor > 5.0) ? 5.0 : correction_factor;
980 } 995 }
981 else 996 else
982 correction_factor = corr_high; 997 correction_factor = corr_high;
983 998
984 bits_per_mb_at_this_q = (int)(.5 + correction_factor * speed_correction * cpi->est_max_qcorrection_factor * (double)vp8_bits_per_mb[INTER_FRAME][Q] / 1. 0); 999 bits_per_mb_at_this_q = (int)(.5 + correction_factor * speed_correction * cpi->twopass.est_max_qcorrection_factor * (double)vp8_bits_per_mb[INTER_FRAME] [Q] / 1.0);
985 1000
986 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb) 1001 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
987 break; 1002 break;
988 } 1003 }
989 1004
990 return Q; 1005 return Q;
991 } 1006 }
992 1007
993 // Estimate a worst case Q for a KF group 1008 // Estimate a worst case Q for a KF group
994 static int estimate_kf_group_q(VP8_COMP *cpi, double section_err, int section_ta rget_bandwitdh, double group_iiratio) 1009 static int estimate_kf_group_q(VP8_COMP *cpi, double section_err, int section_ta rget_bandwitdh, double group_iiratio)
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
1111 // Corrections for higher compression speed settings 1126 // Corrections for higher compression speed settings
1112 // (reduced compression expected) 1127 // (reduced compression expected)
1113 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1)) 1128 if ((cpi->compressor_speed == 3) || (cpi->compressor_speed == 1))
1114 { 1129 {
1115 if (cpi->oxcf.cpu_used <= 5) 1130 if (cpi->oxcf.cpu_used <= 5)
1116 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04); 1131 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1117 else 1132 else
1118 speed_correction = 1.25; 1133 speed_correction = 1.25;
1119 } 1134 }
1120 // II ratio correction factor for clip as a whole 1135 // II ratio correction factor for clip as a whole
1121 clip_iiratio = cpi->total_stats->intra_error / 1136 clip_iiratio = cpi->twopass.total_stats->intra_error /
1122 DOUBLE_DIVIDE_CHECK(cpi->total_stats->coded_error); 1137 DOUBLE_DIVIDE_CHECK(cpi->twopass.total_stats->coded_error);
1123 clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025); 1138 clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025);
1124 if (clip_iifactor < 0.80) 1139 if (clip_iifactor < 0.80)
1125 clip_iifactor = 0.80; 1140 clip_iifactor = 0.80;
1126 1141
1127 // Correction factor used for Q values >= 20 1142 // Correction factor used for Q values >= 20
1128 corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq); 1143 corr_high = pow(err_per_mb / BASE_ERRPERMB, pow_highq);
1129 corr_high = (corr_high < 0.05) ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high; 1144 corr_high = (corr_high < 0.05) ? 0.05 : (corr_high > 5.0) ? 5.0 : corr_high;
1130 1145
1131 // Try and pick a Q that can encode the content at the given rate. 1146 // Try and pick a Q that can encode the content at the given rate.
1132 for (Q = 0; Q < MAXQ; Q++) 1147 for (Q = 0; Q < MAXQ; Q++)
(...skipping 27 matching lines...) Expand all
1160 1175
1161 extern void vp8_new_frame_rate(VP8_COMP *cpi, double framerate); 1176 extern void vp8_new_frame_rate(VP8_COMP *cpi, double framerate);
1162 1177
1163 void vp8_init_second_pass(VP8_COMP *cpi) 1178 void vp8_init_second_pass(VP8_COMP *cpi)
1164 { 1179 {
1165 FIRSTPASS_STATS this_frame; 1180 FIRSTPASS_STATS this_frame;
1166 FIRSTPASS_STATS *start_pos; 1181 FIRSTPASS_STATS *start_pos;
1167 1182
1168 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * cpi->oxcf.t wo_pass_vbrmin_section / 100); 1183 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * cpi->oxcf.t wo_pass_vbrmin_section / 100);
1169 1184
1170 zero_stats(cpi->total_stats); 1185 zero_stats(cpi->twopass.total_stats);
1171 1186
1172 if (!cpi->stats_in_end) 1187 if (!cpi->twopass.stats_in_end)
1173 return; 1188 return;
1174 1189
1175 *cpi->total_stats = *cpi->stats_in_end; 1190 *cpi->twopass.total_stats = *cpi->twopass.stats_in_end;
1176 1191
1177 cpi->total_error_left = cpi->total_stats->ssim_weighted_pred_err; 1192 cpi->twopass.total_error_left = cpi->twopass.total_stats->ssim_weighted_pred _err;
1178 cpi->total_intra_error_left = cpi->total_stats->intra_error; 1193 cpi->twopass.total_intra_error_left = cpi->twopass.total_stats->intra_error;
1179 cpi->total_coded_error_left = cpi->total_stats->coded_error; 1194 cpi->twopass.total_coded_error_left = cpi->twopass.total_stats->coded_error;
1180 cpi->start_tot_err_left = cpi->total_error_left; 1195 cpi->twopass.start_tot_err_left = cpi->twopass.total_error_left;
1181 1196
1182 //cpi->bits_left = (long long)(cpi->total_stats->count * cpi->oxcf.target_ba ndwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate)); 1197 //cpi->twopass.bits_left = (int64_t)(cpi->twopass.total_stats->count * cpi-> oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
1183 //cpi->bits_left -= (long long)(cpi->total_stats->count * two_pass_min_rate / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate)); 1198 //cpi->twopass.bits_left -= (int64_t)(cpi->twopass.total_stats->count * two_ pass_min_rate / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
1184 1199
1185 // each frame can have a different duration, as the frame rate in the source 1200 // each frame can have a different duration, as the frame rate in the source
1186 // isn't guaranteed to be constant. The frame rate prior to the first fram e 1201 // isn't guaranteed to be constant. The frame rate prior to the first fram e
1187 // encoded in the second pass is a guess. However the sum duration is not. 1202 // encoded in the second pass is a guess. However the sum duration is not.
1188 // Its calculated based on the actual durations of all frames from the first 1203 // Its calculated based on the actual durations of all frames from the first
1189 // pass. 1204 // pass.
1190 vp8_new_frame_rate(cpi, 10000000.0 * cpi->total_stats->count / cpi->total_st ats->duration); 1205 vp8_new_frame_rate(cpi, 10000000.0 * cpi->twopass.total_stats->count / cpi-> twopass.total_stats->duration);
1191 1206
1192 cpi->output_frame_rate = cpi->oxcf.frame_rate; 1207 cpi->output_frame_rate = cpi->oxcf.frame_rate;
1193 cpi->bits_left = (long long)(cpi->total_stats->duration * cpi->oxcf.target_b andwidth / 10000000.0) ; 1208 cpi->twopass.bits_left = (int64_t)(cpi->twopass.total_stats->duration * cpi- >oxcf.target_bandwidth / 10000000.0) ;
1194 cpi->bits_left -= (long long)(cpi->total_stats->duration * two_pass_min_rate / 10000000.0); 1209 cpi->twopass.bits_left -= (int64_t)(cpi->twopass.total_stats->duration * two _pass_min_rate / 10000000.0);
1195 cpi->clip_bits_total = cpi->bits_left; 1210 cpi->twopass.clip_bits_total = cpi->twopass.bits_left;
1196 1211
1197 // Calculate a minimum intra value to be used in determining the IIratio 1212 // Calculate a minimum intra value to be used in determining the IIratio
1198 // scores used in the second pass. We have this minimum to make sure 1213 // scores used in the second pass. We have this minimum to make sure
1199 // that clips that are static but "low complexity" in the intra domain 1214 // that clips that are static but "low complexity" in the intra domain
1200 // are still boosted appropriately for KF/GF/ARF 1215 // are still boosted appropriately for KF/GF/ARF
1201 cpi->kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs; 1216 cpi->twopass.kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
1202 cpi->gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs; 1217 cpi->twopass.gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
1203 1218
1204 avg_stats(cpi->total_stats); 1219 avg_stats(cpi->twopass.total_stats);
1205 1220
1206 // Scan the first pass file and calculate an average Intra / Inter error sco re ratio for the sequence 1221 // Scan the first pass file and calculate an average Intra / Inter error sco re ratio for the sequence
1207 { 1222 {
1208 double sum_iiratio = 0.0; 1223 double sum_iiratio = 0.0;
1209 double IIRatio; 1224 double IIRatio;
1210 1225
1211 start_pos = cpi->stats_in; // Note starting "file" positio n 1226 start_pos = cpi->twopass.stats_in; // Note starting "file" position
1212 1227
1213 while (input_stats(cpi, &this_frame) != EOF) 1228 while (input_stats(cpi, &this_frame) != EOF)
1214 { 1229 {
1215 IIRatio = this_frame.intra_error / DOUBLE_DIVIDE_CHECK(this_frame.co ded_error); 1230 IIRatio = this_frame.intra_error / DOUBLE_DIVIDE_CHECK(this_frame.co ded_error);
1216 IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio; 1231 IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio;
1217 sum_iiratio += IIRatio; 1232 sum_iiratio += IIRatio;
1218 } 1233 }
1219 1234
1220 cpi->avg_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK((double)cpi->total_ stats->count); 1235 cpi->twopass.avg_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK((double)cpi ->twopass.total_stats->count);
1221 1236
1222 // Reset file position 1237 // Reset file position
1223 reset_fpf_position(cpi, start_pos); 1238 reset_fpf_position(cpi, start_pos);
1224 } 1239 }
1225 1240
1226 // Scan the first pass file and calculate a modified total error based upon the bias/power function 1241 // Scan the first pass file and calculate a modified total error based upon the bias/power function
1227 // used to allocate bits 1242 // used to allocate bits
1228 { 1243 {
1229 start_pos = cpi->stats_in; // Note starting "file" positio n 1244 start_pos = cpi->twopass.stats_in; // Note starting "file" position
1230 1245
1231 cpi->modified_error_total = 0.0; 1246 cpi->twopass.modified_error_total = 0.0;
1232 cpi->modified_error_used = 0.0; 1247 cpi->twopass.modified_error_used = 0.0;
1233 1248
1234 while (input_stats(cpi, &this_frame) != EOF) 1249 while (input_stats(cpi, &this_frame) != EOF)
1235 { 1250 {
1236 cpi->modified_error_total += calculate_modified_err(cpi, &this_frame ); 1251 cpi->twopass.modified_error_total += calculate_modified_err(cpi, &th is_frame);
1237 } 1252 }
1238 cpi->modified_error_left = cpi->modified_error_total; 1253 cpi->twopass.modified_error_left = cpi->twopass.modified_error_total;
1239 1254
1240 reset_fpf_position(cpi, start_pos); // Reset file position 1255 reset_fpf_position(cpi, start_pos); // Reset file position
1241 1256
1242 } 1257 }
1243
1244 // Calculate the clip target modified bits per error
1245 // The observed bpe starts as the same number.
1246 cpi->clip_bpe = cpi->bits_left /
1247 DOUBLE_DIVIDE_CHECK(cpi->modified_error_total);
1248 cpi->observed_bpe = cpi->clip_bpe;
1249 } 1258 }
1250 1259
1251 void vp8_end_second_pass(VP8_COMP *cpi) 1260 void vp8_end_second_pass(VP8_COMP *cpi)
1252 { 1261 {
1253 } 1262 }
1254 1263
1255 // This function gives and estimate of how badly we believe 1264 // This function gives and estimate of how badly we believe
1256 // the prediction quality is decaying from frame to frame. 1265 // the prediction quality is decaying from frame to frame.
1257 static double get_prediction_decay_rate(VP8_COMP *cpi, FIRSTPASS_STATS *next_fra me) 1266 static double get_prediction_decay_rate(VP8_COMP *cpi, FIRSTPASS_STATS *next_fra me)
1258 { 1267 {
1259 double prediction_decay_rate; 1268 double prediction_decay_rate;
1260 double motion_decay; 1269 double motion_decay;
1261 double motion_pct = next_frame->pcnt_motion; 1270 double motion_pct = next_frame->pcnt_motion;
1262 1271
1263
1264 // Initial basis is the % mbs inter coded 1272 // Initial basis is the % mbs inter coded
1265 prediction_decay_rate = next_frame->pcnt_inter; 1273 prediction_decay_rate = next_frame->pcnt_inter;
1266 1274
1267 // High % motion -> somewhat higher decay rate 1275 // High % motion -> somewhat higher decay rate
1268 motion_decay = (1.0 - (motion_pct / 20.0)); 1276 motion_decay = (1.0 - (motion_pct / 20.0));
1269 if (motion_decay < prediction_decay_rate) 1277 if (motion_decay < prediction_decay_rate)
1270 prediction_decay_rate = motion_decay; 1278 prediction_decay_rate = motion_decay;
1271 1279
1272 // Adjustment to decay rate based on speed of motion 1280 // Adjustment to decay rate based on speed of motion
1273 { 1281 {
(...skipping 28 matching lines...) Expand all
1302 BOOL trans_to_still = FALSE; 1310 BOOL trans_to_still = FALSE;
1303 1311
1304 // Break clause to detect very still sections after motion 1312 // Break clause to detect very still sections after motion
1305 // For example a static image after a fade or other transition 1313 // For example a static image after a fade or other transition
1306 // instead of a clean scene cut. 1314 // instead of a clean scene cut.
1307 if ( (frame_interval > MIN_GF_INTERVAL) && 1315 if ( (frame_interval > MIN_GF_INTERVAL) &&
1308 (loop_decay_rate >= 0.999) && 1316 (loop_decay_rate >= 0.999) &&
1309 (decay_accumulator < 0.9) ) 1317 (decay_accumulator < 0.9) )
1310 { 1318 {
1311 int j; 1319 int j;
1312 FIRSTPASS_STATS * position = cpi->stats_in; 1320 FIRSTPASS_STATS * position = cpi->twopass.stats_in;
1313 FIRSTPASS_STATS tmp_next_frame; 1321 FIRSTPASS_STATS tmp_next_frame;
1314 double decay_rate; 1322 double decay_rate;
1315 1323
1316 // Look ahead a few frames to see if static condition 1324 // Look ahead a few frames to see if static condition
1317 // persists... 1325 // persists...
1318 for ( j = 0; j < still_interval; j++ ) 1326 for ( j = 0; j < still_interval; j++ )
1319 { 1327 {
1320 if (EOF == input_stats(cpi, &tmp_next_frame)) 1328 if (EOF == input_stats(cpi, &tmp_next_frame))
1321 break; 1329 break;
1322 1330
1323 decay_rate = get_prediction_decay_rate(cpi, &tmp_next_frame); 1331 decay_rate = get_prediction_decay_rate(cpi, &tmp_next_frame);
1324 if ( decay_rate < 0.999 ) 1332 if ( decay_rate < 0.999 )
1325 break; 1333 break;
1326 } 1334 }
1327 // Reset file position 1335 // Reset file position
1328 reset_fpf_position(cpi, position); 1336 reset_fpf_position(cpi, position);
1329 1337
1330 // Only if it does do we signal a transition to still 1338 // Only if it does do we signal a transition to still
1331 if ( j == still_interval ) 1339 if ( j == still_interval )
1332 trans_to_still = TRUE; 1340 trans_to_still = TRUE;
1333 } 1341 }
1334 1342
1335 return trans_to_still; 1343 return trans_to_still;
1336 } 1344 }
1337 1345
1346 // This function detects a flash through the high relative pcnt_second_ref
1347 // score in the frame following a flash frame. The offset passed in should
1348 // reflect this
1349 static BOOL detect_flash( VP8_COMP *cpi, int offset )
1350 {
1351 FIRSTPASS_STATS next_frame;
1352
1353 BOOL flash_detected = FALSE;
1354
1355 // Read the frame data.
1356 // The return is FALSE (no flash detected) if not a valid frame
1357 if ( read_frame_stats(cpi, &next_frame, offset) != EOF )
1358 {
1359 // What we are looking for here is a situation where there is a
1360 // brief break in prediction (such as a flash) but subsequent frames
1361 // are reasonably well predicted by an earlier (pre flash) frame.
1362 // The recovery after a flash is indicated by a high pcnt_second_ref
1363 // comapred to pcnt_inter.
1364 if ( (next_frame.pcnt_second_ref > next_frame.pcnt_inter) &&
1365 (next_frame.pcnt_second_ref >= 0.5 ) )
1366 {
1367 flash_detected = TRUE;
1368
1369 /*if (1)
1370 {
1371 FILE *f = fopen("flash.stt", "a");
1372 fprintf(f, "%8.0f %6.2f %6.2f\n",
1373 next_frame.frame,
1374 next_frame.pcnt_inter,
1375 next_frame.pcnt_second_ref);
1376 fclose(f);
1377 }*/
1378 }
1379 }
1380
1381 return flash_detected;
1382 }
1383
1384 // Update the motion related elements to the GF arf boost calculation
1385 static void accumulate_frame_motion_stats(
1386 VP8_COMP *cpi,
1387 FIRSTPASS_STATS * this_frame,
1388 double * this_frame_mv_in_out,
1389 double * mv_in_out_accumulator,
1390 double * abs_mv_in_out_accumulator,
1391 double * mv_ratio_accumulator )
1392 {
1393 //double this_frame_mv_in_out;
1394 double this_frame_mvr_ratio;
1395 double this_frame_mvc_ratio;
1396 double motion_pct;
1397
1398 // Accumulate motion stats.
1399 motion_pct = this_frame->pcnt_motion;
1400
1401 // Accumulate Motion In/Out of frame stats
1402 *this_frame_mv_in_out = this_frame->mv_in_out_count * motion_pct;
1403 *mv_in_out_accumulator += this_frame->mv_in_out_count * motion_pct;
1404 *abs_mv_in_out_accumulator +=
1405 fabs(this_frame->mv_in_out_count * motion_pct);
1406
1407 // Accumulate a measure of how uniform (or conversely how random)
1408 // the motion field is. (A ratio of absmv / mv)
1409 if (motion_pct > 0.05)
1410 {
1411 this_frame_mvr_ratio = fabs(this_frame->mvr_abs) /
1412 DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVr));
1413
1414 this_frame_mvc_ratio = fabs(this_frame->mvc_abs) /
1415 DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVc));
1416
1417 *mv_ratio_accumulator +=
1418 (this_frame_mvr_ratio < this_frame->mvr_abs)
1419 ? (this_frame_mvr_ratio * motion_pct)
1420 : this_frame->mvr_abs * motion_pct;
1421
1422 *mv_ratio_accumulator +=
1423 (this_frame_mvc_ratio < this_frame->mvc_abs)
1424 ? (this_frame_mvc_ratio * motion_pct)
1425 : this_frame->mvc_abs * motion_pct;
1426
1427 }
1428 }
1429
1430 // Calculate a baseline boost number for the current frame.
1431 static double calc_frame_boost(
1432 VP8_COMP *cpi,
1433 FIRSTPASS_STATS * this_frame,
1434 double this_frame_mv_in_out )
1435 {
1436 double frame_boost;
1437
1438 // Underlying boost factor is based on inter intra error ratio
1439 if (this_frame->intra_error > cpi->twopass.gf_intra_err_min)
1440 frame_boost = (IIFACTOR * this_frame->intra_error /
1441 DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
1442 else
1443 frame_boost = (IIFACTOR * cpi->twopass.gf_intra_err_min /
1444 DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
1445
1446 // Increase boost for frames where new data coming into frame
1447 // (eg zoom out). Slightly reduce boost if there is a net balance
1448 // of motion out of the frame (zoom in).
1449 // The range for this_frame_mv_in_out is -1.0 to +1.0
1450 if (this_frame_mv_in_out > 0.0)
1451 frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
1452 // In extreme case boost is halved
1453 else
1454 frame_boost += frame_boost * (this_frame_mv_in_out / 2.0);
1455
1456 // Clip to maximum
1457 if (frame_boost > GF_RMAX)
1458 frame_boost = GF_RMAX;
1459
1460 return frame_boost;
1461 }
1462
1463 #if NEW_BOOST
1464 static int calc_arf_boost(
1465 VP8_COMP *cpi,
1466 int offset,
1467 int f_frames,
1468 int b_frames,
1469 int *f_boost,
1470 int *b_boost )
1471 {
1472 FIRSTPASS_STATS this_frame;
1473
1474 int i;
1475 double boost_score = 0.0;
1476 double fwd_boost_score = 0.0;
1477 double mv_ratio_accumulator = 0.0;
1478 double decay_accumulator = 1.0;
1479 double this_frame_mv_in_out = 0.0;
1480 double mv_in_out_accumulator = 0.0;
1481 double abs_mv_in_out_accumulator = 0.0;
1482 double r;
1483 BOOL flash_detected = FALSE;
1484
1485 // Search forward from the proposed arf/next gf position
1486 for ( i = 0; i < f_frames; i++ )
1487 {
1488 if ( read_frame_stats(cpi, &this_frame, (i+offset)) == EOF )
1489 break;
1490
1491 // Update the motion related elements to the boost calculation
1492 accumulate_frame_motion_stats( cpi, &this_frame,
1493 &this_frame_mv_in_out, &mv_in_out_accumulator,
1494 &abs_mv_in_out_accumulator, &mv_ratio_accumulator );
1495
1496 // Calculate the baseline boost number for this frame
1497 r = calc_frame_boost( cpi, &this_frame, this_frame_mv_in_out );
1498
1499 // We want to discount the the flash frame itself and the recovery
1500 // frame that follows as both will have poor scores.
1501 flash_detected = detect_flash(cpi, (i+offset)) ||
1502 detect_flash(cpi, (i+offset+1));
1503
1504 // Cumulative effect of prediction quality decay
1505 if ( !flash_detected )
1506 {
1507 decay_accumulator =
1508 decay_accumulator *
1509 get_prediction_decay_rate(cpi, &this_frame);
1510 decay_accumulator =
1511 decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1512 }
1513 boost_score += (decay_accumulator * r);
1514
1515 // Break out conditions.
1516 if ( (!flash_detected) &&
1517 ((mv_ratio_accumulator > 100.0) ||
1518 (abs_mv_in_out_accumulator > 3.0) ||
1519 (mv_in_out_accumulator < -2.0) ) )
1520 {
1521 break;
1522 }
1523 }
1524
1525 *f_boost = (int)(boost_score * 100.0) >> 4;
1526
1527 // Reset for backward looking loop
1528 boost_score = 0.0;
1529 mv_ratio_accumulator = 0.0;
1530 decay_accumulator = 1.0;
1531 this_frame_mv_in_out = 0.0;
1532 mv_in_out_accumulator = 0.0;
1533 abs_mv_in_out_accumulator = 0.0;
1534
1535 // Search forward from the proposed arf/next gf position
1536 for ( i = -1; i >= -b_frames; i-- )
1537 {
1538 if ( read_frame_stats(cpi, &this_frame, (i+offset)) == EOF )
1539 break;
1540
1541 // Update the motion related elements to the boost calculation
1542 accumulate_frame_motion_stats( cpi, &this_frame,
1543 &this_frame_mv_in_out, &mv_in_out_accumulator,
1544 &abs_mv_in_out_accumulator, &mv_ratio_accumulator );
1545
1546 // Calculate the baseline boost number for this frame
1547 r = calc_frame_boost( cpi, &this_frame, this_frame_mv_in_out );
1548
1549 // We want to discount the the flash frame itself and the recovery
1550 // frame that follows as both will have poor scores.
1551 flash_detected = detect_flash(cpi, (i+offset)) ||
1552 detect_flash(cpi, (i+offset+1));
1553
1554 // Cumulative effect of prediction quality decay
1555 if ( !flash_detected )
1556 {
1557 decay_accumulator =
1558 decay_accumulator *
1559 get_prediction_decay_rate(cpi, &this_frame);
1560 decay_accumulator =
1561 decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1562 }
1563
1564 boost_score += (decay_accumulator * r);
1565
1566 // Break out conditions.
1567 if ( (!flash_detected) &&
1568 ((mv_ratio_accumulator > 100.0) ||
1569 (abs_mv_in_out_accumulator > 3.0) ||
1570 (mv_in_out_accumulator < -2.0) ) )
1571 {
1572 break;
1573 }
1574 }
1575 *b_boost = (int)(boost_score * 100.0) >> 4;
1576
1577 return (*f_boost + *b_boost);
1578 }
1579 #endif
1580
1338 // Analyse and define a gf/arf group . 1581 // Analyse and define a gf/arf group .
1339 static void define_gf_group(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) 1582 static void define_gf_group(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
1340 { 1583 {
1341 FIRSTPASS_STATS next_frame; 1584 FIRSTPASS_STATS next_frame;
1342 FIRSTPASS_STATS *start_pos; 1585 FIRSTPASS_STATS *start_pos;
1343 int i; 1586 int i;
1344 int y_width = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_width; 1587 double r;
1345 int y_height = cpi->common.yv12_fb[cpi->common.lst_fb_idx].y_height;
1346 int image_size = y_width * y_height;
1347 double boost_score = 0.0; 1588 double boost_score = 0.0;
1348 double old_boost_score = 0.0; 1589 double old_boost_score = 0.0;
1349 double gf_group_err = 0.0; 1590 double gf_group_err = 0.0;
1350 double gf_first_frame_err = 0.0; 1591 double gf_first_frame_err = 0.0;
1351 double mod_frame_err = 0.0; 1592 double mod_frame_err = 0.0;
1352 1593
1353 double mv_accumulator_rabs = 0.0;
1354 double mv_accumulator_cabs = 0.0;
1355 double mv_ratio_accumulator = 0.0; 1594 double mv_ratio_accumulator = 0.0;
1356 double decay_accumulator = 1.0; 1595 double decay_accumulator = 1.0;
1357 1596
1358 double boost_factor = IIFACTOR;
1359 double loop_decay_rate = 1.00; // Starting decay rate 1597 double loop_decay_rate = 1.00; // Starting decay rate
1360 1598
1361 double this_frame_mv_in_out = 0.0; 1599 double this_frame_mv_in_out = 0.0;
1362 double mv_in_out_accumulator = 0.0; 1600 double mv_in_out_accumulator = 0.0;
1363 double abs_mv_in_out_accumulator = 0.0; 1601 double abs_mv_in_out_accumulator = 0.0;
1364 double mod_err_per_mb_accumulator = 0.0; 1602 double mod_err_per_mb_accumulator = 0.0;
1365 1603
1366 int max_bits = frame_max_bits(cpi); // Max for a single frame 1604 int max_bits = frame_max_bits(cpi); // Max for a single frame
1367 1605
1368 unsigned int allow_alt_ref = 1606 unsigned int allow_alt_ref =
1369 cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames; 1607 cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames;
1370 1608
1371 cpi->gf_group_bits = 0; 1609 int alt_boost = 0;
1372 cpi->gf_decay_rate = 0; 1610 int f_boost = 0;
1611 int b_boost = 0;
1612 BOOL flash_detected;
1613
1614 cpi->twopass.gf_group_bits = 0;
1615 cpi->twopass.gf_decay_rate = 0;
1373 1616
1374 vp8_clear_system_state(); //__asm emms; 1617 vp8_clear_system_state(); //__asm emms;
1375 1618
1376 start_pos = cpi->stats_in; 1619 start_pos = cpi->twopass.stats_in;
1377 1620
1378 vpx_memset(&next_frame, 0, sizeof(next_frame)); // assure clean 1621 vpx_memset(&next_frame, 0, sizeof(next_frame)); // assure clean
1379 1622
1380 // Preload the stats for the next frame. 1623 // Load stats for the current frame.
1381 mod_frame_err = calculate_modified_err(cpi, this_frame); 1624 mod_frame_err = calculate_modified_err(cpi, this_frame);
1382 1625
1383 // Note the error of the frame at the start of the group (this will be 1626 // Note the error of the frame at the start of the group (this will be
1384 // the GF frame error if we code a normal gf 1627 // the GF frame error if we code a normal gf
1385 gf_first_frame_err = mod_frame_err; 1628 gf_first_frame_err = mod_frame_err;
1386 1629
1387 // Special treatment if the current frame is a key frame (which is also 1630 // Special treatment if the current frame is a key frame (which is also
1388 // a gf). If it is then its error score (and hence bit allocation) need 1631 // a gf). If it is then its error score (and hence bit allocation) need
1389 // to be subtracted out from the calculation for the GF group 1632 // to be subtracted out from the calculation for the GF group
1390 if (cpi->common.frame_type == KEY_FRAME) 1633 if (cpi->common.frame_type == KEY_FRAME)
1391 gf_group_err -= gf_first_frame_err; 1634 gf_group_err -= gf_first_frame_err;
1392 1635
1393 // Scan forward to try and work out how many frames the next gf group 1636 // Scan forward to try and work out how many frames the next gf group
1394 // should contain and what level of boost is appropriate for the GF 1637 // should contain and what level of boost is appropriate for the GF
1395 // or ARF that will be coded with the group 1638 // or ARF that will be coded with the group
1396 i = 0; 1639 i = 0;
1397 1640
1398 while (((i < cpi->static_scene_max_gf_interval) || 1641 while (((i < cpi->twopass.static_scene_max_gf_interval) ||
1399 ((cpi->frames_to_key - i) < MIN_GF_INTERVAL)) && 1642 ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL)) &&
1400 (i < cpi->frames_to_key)) 1643 (i < cpi->twopass.frames_to_key))
1401 { 1644 {
1402 double r;
1403 double this_frame_mvr_ratio;
1404 double this_frame_mvc_ratio;
1405 double motion_decay;
1406 //double motion_pct = next_frame.pcnt_motion;
1407 double motion_pct;
1408
1409 i++; // Increment the loop counter 1645 i++; // Increment the loop counter
1410 1646
1411 // Accumulate error score of frames in this gf group 1647 // Accumulate error score of frames in this gf group
1412 mod_frame_err = calculate_modified_err(cpi, this_frame); 1648 mod_frame_err = calculate_modified_err(cpi, this_frame);
1413 1649
1414 gf_group_err += mod_frame_err; 1650 gf_group_err += mod_frame_err;
1415 1651
1416 mod_err_per_mb_accumulator += 1652 mod_err_per_mb_accumulator +=
1417 mod_frame_err / DOUBLE_DIVIDE_CHECK((double)cpi->common.MBs); 1653 mod_frame_err / DOUBLE_DIVIDE_CHECK((double)cpi->common.MBs);
1418 1654
1419 if (EOF == input_stats(cpi, &next_frame)) 1655 if (EOF == input_stats(cpi, &next_frame))
1420 break; 1656 break;
1421 1657
1422 // Accumulate motion stats. 1658 // Test for the case where there is a brief flash but the prediction
1423 motion_pct = next_frame.pcnt_motion; 1659 // quality back to an earlier frame is then restored.
1424 mv_accumulator_rabs += fabs(next_frame.mvr_abs * motion_pct); 1660 flash_detected = detect_flash(cpi, 0);
1425 mv_accumulator_cabs += fabs(next_frame.mvc_abs * motion_pct);
1426 1661
1427 //Accumulate Motion In/Out of frame stats 1662 // Update the motion related elements to the boost calculation
1428 this_frame_mv_in_out = 1663 accumulate_frame_motion_stats( cpi, &next_frame,
1429 next_frame.mv_in_out_count * motion_pct; 1664 &this_frame_mv_in_out, &mv_in_out_accumulator,
1430 mv_in_out_accumulator += 1665 &abs_mv_in_out_accumulator, &mv_ratio_accumulator );
1431 next_frame.mv_in_out_count * motion_pct;
1432 abs_mv_in_out_accumulator +=
1433 fabs(next_frame.mv_in_out_count * motion_pct);
1434 1666
1435 // If there is a significant amount of motion 1667 // Calculate a baseline boost number for this frame
1436 if (motion_pct > 0.05) 1668 r = calc_frame_boost( cpi, &next_frame, this_frame_mv_in_out );
1669
1670 // Cumulative effect of prediction quality decay
1671 if ( !flash_detected )
1437 { 1672 {
1438 this_frame_mvr_ratio = fabs(next_frame.mvr_abs) / 1673 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
1439 DOUBLE_DIVIDE_CHECK(fabs(next_frame.MVr)); 1674 decay_accumulator = decay_accumulator * loop_decay_rate;
1440 1675 decay_accumulator =
1441 this_frame_mvc_ratio = fabs(next_frame.mvc_abs) / 1676 decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1442 DOUBLE_DIVIDE_CHECK(fabs(next_frame.MVc));
1443
1444 mv_ratio_accumulator +=
1445 (this_frame_mvr_ratio < next_frame.mvr_abs)
1446 ? (this_frame_mvr_ratio * motion_pct)
1447 : next_frame.mvr_abs * motion_pct;
1448
1449 mv_ratio_accumulator +=
1450 (this_frame_mvc_ratio < next_frame.mvc_abs)
1451 ? (this_frame_mvc_ratio * motion_pct)
1452 : next_frame.mvc_abs * motion_pct;
1453 } 1677 }
1454 else
1455 {
1456 mv_ratio_accumulator += 0.0;
1457 this_frame_mvr_ratio = 1.0;
1458 this_frame_mvc_ratio = 1.0;
1459 }
1460
1461 // Underlying boost factor is based on inter intra error ratio
1462 r = ( boost_factor *
1463 ( next_frame.intra_error /
1464 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)));
1465
1466 if (next_frame.intra_error > cpi->gf_intra_err_min)
1467 r = (IIKFACTOR2 * next_frame.intra_error /
1468 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
1469 else
1470 r = (IIKFACTOR2 * cpi->gf_intra_err_min /
1471 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
1472
1473 // Increase boost for frames where new data coming into frame
1474 // (eg zoom out). Slightly reduce boost if there is a net balance
1475 // of motion out of the frame (zoom in).
1476 // The range for this_frame_mv_in_out is -1.0 to +1.0
1477 if (this_frame_mv_in_out > 0.0)
1478 r += r * (this_frame_mv_in_out * 2.0);
1479 // In extreme case boost is halved
1480 else
1481 r += r * (this_frame_mv_in_out / 2.0);
1482
1483 if (r > GF_RMAX)
1484 r = GF_RMAX;
1485
1486 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
1487
1488 // Cumulative effect of decay
1489 decay_accumulator = decay_accumulator * loop_decay_rate;
1490 decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1491
1492 boost_score += (decay_accumulator * r); 1678 boost_score += (decay_accumulator * r);
1493 1679
1494 // Break clause to detect very still sections after motion 1680 // Break clause to detect very still sections after motion
1495 // For example a staic image after a fade or other transition. 1681 // For example a staic image after a fade or other transition.
1496 if ( detect_transition_to_still( cpi, i, 5, 1682 if ( detect_transition_to_still( cpi, i, 5,
1497 loop_decay_rate, decay_accumulator ) ) 1683 loop_decay_rate,
1684 decay_accumulator ) )
1498 { 1685 {
1499 allow_alt_ref = FALSE; 1686 allow_alt_ref = FALSE;
1500 boost_score = old_boost_score; 1687 boost_score = old_boost_score;
1501 break; 1688 break;
1502 } 1689 }
1503 1690
1504 // Break out conditions. 1691 // Break out conditions.
1505 if ( /* i>4 || */ 1692 if (
1506 // Break at cpi->max_gf_interval unless almost totally static 1693 // Break at cpi->max_gf_interval unless almost totally static
1507 (i >= cpi->max_gf_interval && (decay_accumulator < 0.995)) || 1694 (i >= cpi->max_gf_interval && (decay_accumulator < 0.995)) ||
1508 ( 1695 (
1509 // Dont break out with a very short interval 1696 // Dont break out with a very short interval
1510 (i > MIN_GF_INTERVAL) && 1697 (i > MIN_GF_INTERVAL) &&
1511 // Dont break out very close to a key frame 1698 // Dont break out very close to a key frame
1512 ((cpi->frames_to_key - i) >= MIN_GF_INTERVAL) && 1699 ((cpi->twopass.frames_to_key - i) >= MIN_GF_INTERVAL) &&
1513 ((boost_score > 20.0) || (next_frame.pcnt_inter < 0.75)) && 1700 ((boost_score > 20.0) || (next_frame.pcnt_inter < 0.75)) &&
1701 (!flash_detected) &&
1514 ((mv_ratio_accumulator > 100.0) || 1702 ((mv_ratio_accumulator > 100.0) ||
1515 (abs_mv_in_out_accumulator > 3.0) || 1703 (abs_mv_in_out_accumulator > 3.0) ||
1516 (mv_in_out_accumulator < -2.0) || 1704 (mv_in_out_accumulator < -2.0) ||
1517 ((boost_score - old_boost_score) < 2.0)) 1705 ((boost_score - old_boost_score) < 2.0))
1518 ) ) 1706 ) )
1519 { 1707 {
1520 boost_score = old_boost_score; 1708 boost_score = old_boost_score;
1521 break; 1709 break;
1522 } 1710 }
1523 1711
1524 vpx_memcpy(this_frame, &next_frame, sizeof(*this_frame)); 1712 vpx_memcpy(this_frame, &next_frame, sizeof(*this_frame));
1525 1713
1526 old_boost_score = boost_score; 1714 old_boost_score = boost_score;
1527 } 1715 }
1528 1716
1529 cpi->gf_decay_rate = 1717 cpi->twopass.gf_decay_rate =
1530 (i > 0) ? (int)(100.0 * (1.0 - decay_accumulator)) / i : 0; 1718 (i > 0) ? (int)(100.0 * (1.0 - decay_accumulator)) / i : 0;
1531 1719
1532 // When using CBR apply additional buffer related upper limits 1720 // When using CBR apply additional buffer related upper limits
1533 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) 1721 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
1534 { 1722 {
1535 double max_boost; 1723 double max_boost;
1536 1724
1537 // For cbr apply buffer related limits 1725 // For cbr apply buffer related limits
1538 if (cpi->drop_frames_allowed) 1726 if (cpi->drop_frames_allowed)
1539 { 1727 {
(...skipping 11 matching lines...) Expand all
1551 } 1739 }
1552 else 1740 else
1553 { 1741 {
1554 max_boost = 0.0; 1742 max_boost = 0.0;
1555 } 1743 }
1556 1744
1557 if (boost_score > max_boost) 1745 if (boost_score > max_boost)
1558 boost_score = max_boost; 1746 boost_score = max_boost;
1559 } 1747 }
1560 1748
1749 // Dont allow conventional gf too near the next kf
1750 if ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL)
1751 {
1752 while (i < cpi->twopass.frames_to_key)
1753 {
1754 i++;
1755
1756 if (EOF == input_stats(cpi, this_frame))
1757 break;
1758
1759 if (i < cpi->twopass.frames_to_key)
1760 {
1761 mod_frame_err = calculate_modified_err(cpi, this_frame);
1762 gf_group_err += mod_frame_err;
1763 }
1764 }
1765 }
1766
1561 cpi->gfu_boost = (int)(boost_score * 100.0) >> 4; 1767 cpi->gfu_boost = (int)(boost_score * 100.0) >> 4;
1562 1768
1769 #if NEW_BOOST
1770 // Alterrnative boost calculation for alt ref
1771 alt_boost = calc_arf_boost( cpi, 0, (i-1), (i-1), &f_boost, &b_boost );
1772 #endif
1773
1563 // Should we use the alternate refernce frame 1774 // Should we use the alternate refernce frame
1564 if (allow_alt_ref && 1775 if (allow_alt_ref &&
1565 (i >= MIN_GF_INTERVAL) && 1776 (i >= MIN_GF_INTERVAL) &&
1566 // dont use ARF very near next kf 1777 // dont use ARF very near next kf
1567 (i <= (cpi->frames_to_key - MIN_GF_INTERVAL)) && 1778 (i <= (cpi->twopass.frames_to_key - MIN_GF_INTERVAL)) &&
1568 (((next_frame.pcnt_inter > 0.75) && 1779 #if NEW_BOOST
1569 ((mv_in_out_accumulator / (double)i > -0.2) || (mv_in_out_accumulator > -2.0)) && 1780 ((next_frame.pcnt_inter > 0.75) ||
1570 //(cpi->gfu_boost>150) && 1781 (next_frame.pcnt_second_ref > 0.5)) &&
1571 (cpi->gfu_boost > 100) && 1782 ((mv_in_out_accumulator / (double)i > -0.2) ||
1572 //(cpi->gfu_boost>AF_THRESH2) && 1783 (mv_in_out_accumulator > -2.0)) &&
1573 //((cpi->gfu_boost/i)>AF_THRESH) && 1784 (b_boost > 100) &&
1574 //(decay_accumulator > 0.5) && 1785 (f_boost > 100) )
1575 (cpi->gf_decay_rate <= (ARF_DECAY_THRESH + (cpi->gfu_boost / 200))) 1786 #else
1576 ) 1787 (next_frame.pcnt_inter > 0.75) &&
1577 ) 1788 ((mv_in_out_accumulator / (double)i > -0.2) ||
1578 ) 1789 (mv_in_out_accumulator > -2.0)) &&
1790 (cpi->gfu_boost > 100) &&
1791 (cpi->twopass.gf_decay_rate <=
1792 (ARF_DECAY_THRESH + (cpi->gfu_boost / 200))) )
1793 #endif
1579 { 1794 {
1580 int Boost; 1795 int Boost;
1581 int allocation_chunks; 1796 int allocation_chunks;
1582 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.f ixed_q; 1797 int Q = (cpi->oxcf.fixed_q < 0)
1798 ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
1583 int tmp_q; 1799 int tmp_q;
1584 int arf_frame_bits = 0; 1800 int arf_frame_bits = 0;
1585 int group_bits; 1801 int group_bits;
1586 1802
1803 #if NEW_BOOST
1804 cpi->gfu_boost = alt_boost;
1805 #endif
1806
1587 // Estimate the bits to be allocated to the group as a whole 1807 // Estimate the bits to be allocated to the group as a whole
1588 if ((cpi->kf_group_bits > 0) && (cpi->kf_group_error_left > 0)) 1808 if ((cpi->twopass.kf_group_bits > 0) &&
1589 group_bits = (int)((double)cpi->kf_group_bits * (gf_group_err / (dou ble)cpi->kf_group_error_left)); 1809 (cpi->twopass.kf_group_error_left > 0))
1810 {
1811 group_bits = (int)((double)cpi->twopass.kf_group_bits *
1812 (gf_group_err / (double)cpi->twopass.kf_group_error_left));
1813 }
1590 else 1814 else
1591 group_bits = 0; 1815 group_bits = 0;
1592 1816
1593 // Boost for arf frame 1817 // Boost for arf frame
1818 #if NEW_BOOST
1819 Boost = (alt_boost * GFQ_ADJUSTMENT) / 100;
1820 #else
1594 Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100); 1821 Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
1822 #endif
1595 Boost += (i * 50); 1823 Boost += (i * 50);
1824
1825 // Set max and minimum boost and hence minimum allocation
1826 if (Boost > ((cpi->baseline_gf_interval + 1) * 200))
1827 Boost = ((cpi->baseline_gf_interval + 1) * 200);
1828 else if (Boost < 125)
1829 Boost = 125;
1830
1596 allocation_chunks = (i * 100) + Boost; 1831 allocation_chunks = (i * 100) + Boost;
1597 1832
1598 // Normalize Altboost and allocations chunck down to prevent overflow 1833 // Normalize Altboost and allocations chunck down to prevent overflow
1599 while (Boost > 1000) 1834 while (Boost > 1000)
1600 { 1835 {
1601 Boost /= 2; 1836 Boost /= 2;
1602 allocation_chunks /= 2; 1837 allocation_chunks /= 2;
1603 } 1838 }
1604 1839
1605 // Calculate the number of bits to be spent on the arf based on the boos t number 1840 // Calculate the number of bits to be spent on the arf based on the
1606 arf_frame_bits = (int)((double)Boost * (group_bits / (double)allocation_ chunks)); 1841 // boost number
1842 arf_frame_bits = (int)((double)Boost * (group_bits /
1843 (double)allocation_chunks));
1607 1844
1608 // Estimate if there are enough bits available to make worthwhile use of an arf. 1845 // Estimate if there are enough bits available to make worthwhile use
1846 // of an arf.
1609 tmp_q = estimate_q(cpi, mod_frame_err, (int)arf_frame_bits); 1847 tmp_q = estimate_q(cpi, mod_frame_err, (int)arf_frame_bits);
1610 1848
1611 // Only use an arf if it is likely we will be able to code it at a lower Q than the surrounding frames. 1849 // Only use an arf if it is likely we will be able to code
1850 // it at a lower Q than the surrounding frames.
1612 if (tmp_q < cpi->worst_quality) 1851 if (tmp_q < cpi->worst_quality)
1613 { 1852 {
1614 int half_gf_int; 1853 int half_gf_int;
1615 int frames_after_arf; 1854 int frames_after_arf;
1616 int frames_bwd = cpi->oxcf.arnr_max_frames - 1; 1855 int frames_bwd = cpi->oxcf.arnr_max_frames - 1;
1617 int frames_fwd = cpi->oxcf.arnr_max_frames - 1; 1856 int frames_fwd = cpi->oxcf.arnr_max_frames - 1;
1618 1857
1619 cpi->source_alt_ref_pending = TRUE; 1858 cpi->source_alt_ref_pending = TRUE;
1620 1859
1621 // For alt ref frames the error score for the end frame of the group (the alt ref frame) should not contribute to the group total and hence 1860 // For alt ref frames the error score for the end frame of the
1622 // the number of bit allocated to the group. Rather it forms part of the next group (it is the GF at the start of the next group) 1861 // group (the alt ref frame) should not contribute to the group
1623 gf_group_err -= mod_frame_err; 1862 // total and hence the number of bit allocated to the group.
1863 // Rather it forms part of the next group (it is the GF at the
1864 // start of the next group)
1865 // gf_group_err -= mod_frame_err;
1624 1866
1625 // Set the interval till the next gf or arf. For ARFs this is the nu mber of frames to be coded before the future frame that is coded as an ARF. 1867 // For alt ref frames alt ref frame is technically part of the
1868 // GF frame for the next group but we always base the error
1869 // calculation and bit allocation on the current group of frames.
1870
1871 // Set the interval till the next gf or arf.
1872 // For ARFs this is the number of frames to be coded before the
1873 // future frame that is coded as an ARF.
1626 // The future frame itself is part of the next group 1874 // The future frame itself is part of the next group
1627 cpi->baseline_gf_interval = i - 1; 1875 cpi->baseline_gf_interval = i;
1628 1876
1629 // Define the arnr filter width for this group of frames: 1877 // Define the arnr filter width for this group of frames:
1630 // We only filter frames that lie within a distance of half 1878 // We only filter frames that lie within a distance of half
1631 // the GF interval from the ARF frame. We also have to trap 1879 // the GF interval from the ARF frame. We also have to trap
1632 // cases where the filter extends beyond the end of clip. 1880 // cases where the filter extends beyond the end of clip.
1633 // Note: this_frame->frame has been updated in the loop 1881 // Note: this_frame->frame has been updated in the loop
1634 // so it now points at the ARF frame. 1882 // so it now points at the ARF frame.
1635 half_gf_int = cpi->baseline_gf_interval >> 1; 1883 half_gf_int = cpi->baseline_gf_interval >> 1;
1636 frames_after_arf = cpi->total_stats->count - this_frame->frame - 1; 1884 frames_after_arf = cpi->twopass.total_stats->count -
1885 this_frame->frame - 1;
1637 1886
1638 switch (cpi->oxcf.arnr_type) 1887 switch (cpi->oxcf.arnr_type)
1639 { 1888 {
1640 case 1: // Backward filter 1889 case 1: // Backward filter
1641 frames_fwd = 0; 1890 frames_fwd = 0;
1642 if (frames_bwd > half_gf_int) 1891 if (frames_bwd > half_gf_int)
1643 frames_bwd = half_gf_int; 1892 frames_bwd = half_gf_int;
1644 break; 1893 break;
1645 1894
1646 case 2: // Forward filter 1895 case 2: // Forward filter
(...skipping 28 matching lines...) Expand all
1675 cpi->source_alt_ref_pending = FALSE; 1924 cpi->source_alt_ref_pending = FALSE;
1676 cpi->baseline_gf_interval = i; 1925 cpi->baseline_gf_interval = i;
1677 } 1926 }
1678 } 1927 }
1679 else 1928 else
1680 { 1929 {
1681 cpi->source_alt_ref_pending = FALSE; 1930 cpi->source_alt_ref_pending = FALSE;
1682 cpi->baseline_gf_interval = i; 1931 cpi->baseline_gf_interval = i;
1683 } 1932 }
1684 1933
1685 // Conventional GF 1934 // Now decide how many bits should be allocated to the GF group as a
1686 if (!cpi->source_alt_ref_pending) 1935 // proportion of those remaining in the kf group.
1936 // The final key frame group in the clip is treated as a special case
1937 // where cpi->twopass.kf_group_bits is tied to cpi->twopass.bits_left.
1938 // This is also important for short clips where there may only be one
1939 // key frame.
1940 if (cpi->twopass.frames_to_key >= (int)(cpi->twopass.total_stats->count -
1941 cpi->common.current_video_frame))
1687 { 1942 {
1688 // Dont allow conventional gf too near the next kf 1943 cpi->twopass.kf_group_bits =
1689 if ((cpi->frames_to_key - cpi->baseline_gf_interval) < MIN_GF_INTERVAL) 1944 (cpi->twopass.bits_left > 0) ? cpi->twopass.bits_left : 0;
1690 {
1691 while (cpi->baseline_gf_interval < cpi->frames_to_key)
1692 {
1693 if (EOF == input_stats(cpi, this_frame))
1694 break;
1695
1696 cpi->baseline_gf_interval++;
1697
1698 if (cpi->baseline_gf_interval < cpi->frames_to_key)
1699 gf_group_err += calculate_modified_err(cpi, this_frame);
1700 }
1701 }
1702 }
1703
1704 // Now decide how many bits should be allocated to the GF group as a propor tion of those remaining in the kf group.
1705 // The final key frame group in the clip is treated as a special case where cpi->kf_group_bits is tied to cpi->bits_left.
1706 // This is also important for short clips where there may only be one key fr ame.
1707 if (cpi->frames_to_key >= (int)(cpi->total_stats->count - cpi->common.curren t_video_frame))
1708 {
1709 cpi->kf_group_bits = (cpi->bits_left > 0) ? cpi->bits_left : 0;
1710 } 1945 }
1711 1946
1712 // Calculate the bits to be allocated to the group as a whole 1947 // Calculate the bits to be allocated to the group as a whole
1713 if ((cpi->kf_group_bits > 0) && (cpi->kf_group_error_left > 0)) 1948 if ((cpi->twopass.kf_group_bits > 0) &&
1714 cpi->gf_group_bits = (int)((double)cpi->kf_group_bits * (gf_group_err / (double)cpi->kf_group_error_left)); 1949 (cpi->twopass.kf_group_error_left > 0))
1950 {
1951 cpi->twopass.gf_group_bits =
1952 (int)((double)cpi->twopass.kf_group_bits *
1953 (gf_group_err / (double)cpi->twopass.kf_group_error_left));
1954 }
1715 else 1955 else
1716 cpi->gf_group_bits = 0; 1956 cpi->twopass.gf_group_bits = 0;
1717 1957
1718 cpi->gf_group_bits = (cpi->gf_group_bits < 0) ? 0 : (cpi->gf_group_bits > cp i->kf_group_bits) ? cpi->kf_group_bits : cpi->gf_group_bits; 1958 cpi->twopass.gf_group_bits =
1959 (cpi->twopass.gf_group_bits < 0)
1960 ? 0
1961 : (cpi->twopass.gf_group_bits > cpi->twopass.kf_group_bits)
1962 ? cpi->twopass.kf_group_bits : cpi->twopass.gf_group_bits;
1719 1963
1720 // Clip cpi->gf_group_bits based on user supplied data rate variability limi t (cpi->oxcf.two_pass_vbrmax_section) 1964 // Clip cpi->twopass.gf_group_bits based on user supplied data rate
1721 if (cpi->gf_group_bits > max_bits * cpi->baseline_gf_interval) 1965 // variability limit (cpi->oxcf.two_pass_vbrmax_section)
1722 cpi->gf_group_bits = max_bits * cpi->baseline_gf_interval; 1966 if (cpi->twopass.gf_group_bits > max_bits * cpi->baseline_gf_interval)
1967 cpi->twopass.gf_group_bits = max_bits * cpi->baseline_gf_interval;
1723 1968
1724 // Reset the file position 1969 // Reset the file position
1725 reset_fpf_position(cpi, start_pos); 1970 reset_fpf_position(cpi, start_pos);
1726 1971
1727 // Update the record of error used so far (only done once per gf group) 1972 // Update the record of error used so far (only done once per gf group)
1728 cpi->modified_error_used += gf_group_err; 1973 cpi->twopass.modified_error_used += gf_group_err;
1729 1974
1730 // Assign bits to the arf or gf. 1975 // Assign bits to the arf or gf.
1731 { 1976 for (i = 0; i <= (cpi->source_alt_ref_pending && cpi->common.frame_type != K EY_FRAME); i++) {
1732 int Boost; 1977 int Boost;
1733 int frames_in_section;
1734 int allocation_chunks; 1978 int allocation_chunks;
1735 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.f ixed_q; 1979 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.f ixed_q;
1980 int gf_bits;
1736 1981
1737 // For ARF frames 1982 // For ARF frames
1738 if (cpi->source_alt_ref_pending) 1983 if (cpi->source_alt_ref_pending && i == 0)
1739 { 1984 {
1985 #if NEW_BOOST
1986 Boost = (alt_boost * GFQ_ADJUSTMENT) / 100;
1987 #else
1740 Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100); 1988 Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
1741 //Boost += (cpi->baseline_gf_interval * 25); 1989 #endif
1742 Boost += (cpi->baseline_gf_interval * 50); 1990 Boost += (cpi->baseline_gf_interval * 50);
1743 1991
1744 // Set max and minimum boost and hence minimum allocation 1992 // Set max and minimum boost and hence minimum allocation
1745 if (Boost > ((cpi->baseline_gf_interval + 1) * 200)) 1993 if (Boost > ((cpi->baseline_gf_interval + 1) * 200))
1746 Boost = ((cpi->baseline_gf_interval + 1) * 200); 1994 Boost = ((cpi->baseline_gf_interval + 1) * 200);
1747 else if (Boost < 125) 1995 else if (Boost < 125)
1748 Boost = 125; 1996 Boost = 125;
1749 1997
1750 frames_in_section = cpi->baseline_gf_interval + 1; 1998 allocation_chunks =
1751 allocation_chunks = (frames_in_section * 100) + Boost; 1999 ((cpi->baseline_gf_interval + 1) * 100) + Boost;
1752 } 2000 }
1753 // Else for standard golden frames 2001 // Else for standard golden frames
1754 else 2002 else
1755 { 2003 {
1756 // boost based on inter / intra ratio of subsequent frames 2004 // boost based on inter / intra ratio of subsequent frames
1757 Boost = (cpi->gfu_boost * GFQ_ADJUSTMENT) / 100; 2005 Boost = (cpi->gfu_boost * GFQ_ADJUSTMENT) / 100;
1758 2006
1759 // Set max and minimum boost and hence minimum allocation 2007 // Set max and minimum boost and hence minimum allocation
1760 if (Boost > (cpi->baseline_gf_interval * 150)) 2008 if (Boost > (cpi->baseline_gf_interval * 150))
1761 Boost = (cpi->baseline_gf_interval * 150); 2009 Boost = (cpi->baseline_gf_interval * 150);
1762 else if (Boost < 125) 2010 else if (Boost < 125)
1763 Boost = 125; 2011 Boost = 125;
1764 2012
1765 frames_in_section = cpi->baseline_gf_interval; 2013 allocation_chunks =
1766 allocation_chunks = (frames_in_section * 100) + (Boost - 100); 2014 (cpi->baseline_gf_interval * 100) + (Boost - 100);
1767 } 2015 }
1768 2016
1769 // Normalize Altboost and allocations chunck down to prevent overflow 2017 // Normalize Altboost and allocations chunck down to prevent overflow
1770 while (Boost > 1000) 2018 while (Boost > 1000)
1771 { 2019 {
1772 Boost /= 2; 2020 Boost /= 2;
1773 allocation_chunks /= 2; 2021 allocation_chunks /= 2;
1774 } 2022 }
1775 2023
1776 // Calculate the number of bits to be spent on the gf or arf based on th e boost number 2024 // Calculate the number of bits to be spent on the gf or arf based on
1777 cpi->gf_bits = (int)((double)Boost * (cpi->gf_group_bits / (double)alloc ation_chunks)); 2025 // the boost number
2026 gf_bits = (int)((double)Boost *
2027 (cpi->twopass.gf_group_bits /
2028 (double)allocation_chunks));
1778 2029
1779 // If the frame that is to be boosted is simpler than the average for 2030 // If the frame that is to be boosted is simpler than the average for
1780 // the gf/arf group then use an alternative calculation 2031 // the gf/arf group then use an alternative calculation
1781 // based on the error score of the frame itself 2032 // based on the error score of the frame itself
1782 if (mod_frame_err < gf_group_err / (double)cpi->baseline_gf_interval) 2033 if (mod_frame_err < gf_group_err / (double)cpi->baseline_gf_interval)
1783 { 2034 {
1784 double alt_gf_grp_bits; 2035 double alt_gf_grp_bits;
1785 int alt_gf_bits; 2036 int alt_gf_bits;
1786 2037
1787 alt_gf_grp_bits = 2038 alt_gf_grp_bits =
1788 (double)cpi->kf_group_bits * 2039 (double)cpi->twopass.kf_group_bits *
1789 (mod_frame_err * (double)cpi->baseline_gf_interval) / 2040 (mod_frame_err * (double)cpi->baseline_gf_interval) /
1790 DOUBLE_DIVIDE_CHECK((double)cpi->kf_group_error_left); 2041 DOUBLE_DIVIDE_CHECK((double)cpi->twopass.kf_group_error_left);
1791 2042
1792 alt_gf_bits = (int)((double)Boost * (alt_gf_grp_bits / 2043 alt_gf_bits = (int)((double)Boost * (alt_gf_grp_bits /
1793 (double)allocation_chunks)); 2044 (double)allocation_chunks));
1794 2045
1795 if (cpi->gf_bits > alt_gf_bits) 2046 if (gf_bits > alt_gf_bits)
1796 { 2047 {
1797 cpi->gf_bits = alt_gf_bits; 2048 gf_bits = alt_gf_bits;
1798 } 2049 }
1799 } 2050 }
1800 // Else if it is harder than other frames in the group make sure it at 2051 // Else if it is harder than other frames in the group make sure it at
1801 // least receives an allocation in keeping with its relative error 2052 // least receives an allocation in keeping with its relative error
1802 // score, otherwise it may be worse off than an "un-boosted" frame 2053 // score, otherwise it may be worse off than an "un-boosted" frame
1803 else 2054 else
1804 { 2055 {
1805 int alt_gf_bits = 2056 int alt_gf_bits =
1806 (int)((double)cpi->kf_group_bits * 2057 (int)((double)cpi->twopass.kf_group_bits *
1807 mod_frame_err / 2058 mod_frame_err /
1808 DOUBLE_DIVIDE_CHECK((double)cpi->kf_group_error_left)); 2059 DOUBLE_DIVIDE_CHECK((double)cpi->twopass.kf_group_error_le ft));
1809 2060
1810 if (alt_gf_bits > cpi->gf_bits) 2061 if (alt_gf_bits > gf_bits)
1811 { 2062 {
1812 cpi->gf_bits = alt_gf_bits; 2063 gf_bits = alt_gf_bits;
1813 } 2064 }
1814 } 2065 }
1815 2066
1816 // Apply an additional limit for CBR 2067 // Apply an additional limit for CBR
1817 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) 2068 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
1818 { 2069 {
1819 if (cpi->gf_bits > (cpi->buffer_level >> 1)) 2070 if (cpi->twopass.gf_bits > (cpi->buffer_level >> 1))
1820 cpi->gf_bits = cpi->buffer_level >> 1; 2071 cpi->twopass.gf_bits = cpi->buffer_level >> 1;
1821 } 2072 }
1822 2073
1823 // Dont allow a negative value for gf_bits 2074 // Dont allow a negative value for gf_bits
1824 if (cpi->gf_bits < 0) 2075 if (gf_bits < 0)
1825 cpi->gf_bits = 0; 2076 gf_bits = 0;
1826 2077
2078 gf_bits += cpi->min_frame_bandwidth; // Add in minim um for a frame
2079
2080 if (i == 0)
2081 {
2082 cpi->twopass.gf_bits = gf_bits;
2083 }
2084 if (i == 1 || (!cpi->source_alt_ref_pending && (cpi->common.frame_type ! = KEY_FRAME)))
2085 {
2086 cpi->per_frame_bandwidth = gf_bits; // Per frame bit target for this frame
2087 }
2088 }
2089
2090 {
1827 // Adjust KF group bits and error remainin 2091 // Adjust KF group bits and error remainin
1828 cpi->kf_group_error_left -= gf_group_err; 2092 cpi->twopass.kf_group_error_left -= gf_group_err;
1829 cpi->kf_group_bits -= cpi->gf_group_bits; 2093 cpi->twopass.kf_group_bits -= cpi->twopass.gf_group_bits;
1830 2094
1831 if (cpi->kf_group_bits < 0) 2095 if (cpi->twopass.kf_group_bits < 0)
1832 cpi->kf_group_bits = 0; 2096 cpi->twopass.kf_group_bits = 0;
1833 2097
1834 // Note the error score left in the remaining frames of the group. 2098 // Note the error score left in the remaining frames of the group.
1835 // For normal GFs we want to remove the error score for the first frame of the group (except in Key frame case where this has already happened) 2099 // For normal GFs we want to remove the error score for the first frame of the group (except in Key frame case where this has already happened)
1836 if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME) 2100 if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME)
1837 cpi->gf_group_error_left = gf_group_err - gf_first_frame_err; 2101 cpi->twopass.gf_group_error_left = gf_group_err - gf_first_frame_err ;
1838 else 2102 else
1839 cpi->gf_group_error_left = gf_group_err; 2103 cpi->twopass.gf_group_error_left = gf_group_err;
1840 2104
1841 cpi->gf_group_bits -= cpi->gf_bits; 2105 cpi->twopass.gf_group_bits -= cpi->twopass.gf_bits - cpi->min_frame_band width;
1842 2106
1843 if (cpi->gf_group_bits < 0) 2107 if (cpi->twopass.gf_group_bits < 0)
1844 cpi->gf_group_bits = 0; 2108 cpi->twopass.gf_group_bits = 0;
1845 2109
1846 // Set aside some bits for a mid gf sequence boost
1847 if ((cpi->gfu_boost > 150) && (cpi->baseline_gf_interval > 5))
1848 { 2110 {
1849 int pct_extra = (cpi->gfu_boost - 100) / 50; 2111 #if NEW_BOOST
1850 pct_extra = (pct_extra > 10) ? 10 : pct_extra; 2112 int boost = (cpi->source_alt_ref_pending)
2113 ? b_boost : cpi->gfu_boost;
2114 #else
2115 int boost = cpi->gfu_boost;
2116 #endif
2117 // Set aside some bits for a mid gf sequence boost
2118 if ((boost > 150) && (cpi->baseline_gf_interval > 5))
2119 {
2120 int pct_extra = (boost - 100) / 50;
2121 pct_extra = (pct_extra > 10) ? 10 : pct_extra;
1851 2122
1852 cpi->mid_gf_extra_bits = (cpi->gf_group_bits * pct_extra) / 100; 2123 cpi->twopass.mid_gf_extra_bits =
1853 cpi->gf_group_bits -= cpi->mid_gf_extra_bits; 2124 (cpi->twopass.gf_group_bits * pct_extra) / 100;
2125 cpi->twopass.gf_group_bits -= cpi->twopass.mid_gf_extra_bits;
2126 }
2127 else
2128 cpi->twopass.mid_gf_extra_bits = 0;
1854 } 2129 }
1855 else
1856 cpi->mid_gf_extra_bits = 0;
1857
1858 cpi->gf_bits += cpi->min_frame_bandwidth; // Add in minimum for a frame
1859 }
1860
1861 if (!cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)) // Normal GF and not a KF
1862 {
1863 cpi->per_frame_bandwidth = cpi->gf_bits; // Per frame bit target for this frame
1864 } 2130 }
1865 2131
1866 // Adjustment to estimate_max_q based on a measure of complexity of the sect ion 2132 // Adjustment to estimate_max_q based on a measure of complexity of the sect ion
1867 if (cpi->common.frame_type != KEY_FRAME) 2133 if (cpi->common.frame_type != KEY_FRAME)
1868 { 2134 {
1869 FIRSTPASS_STATS sectionstats; 2135 FIRSTPASS_STATS sectionstats;
1870 double Ratio; 2136 double Ratio;
1871 2137
1872 zero_stats(&sectionstats); 2138 zero_stats(&sectionstats);
1873 reset_fpf_position(cpi, start_pos); 2139 reset_fpf_position(cpi, start_pos);
1874 2140
1875 for (i = 0 ; i < cpi->baseline_gf_interval ; i++) 2141 for (i = 0 ; i < cpi->baseline_gf_interval ; i++)
1876 { 2142 {
1877 input_stats(cpi, &next_frame); 2143 input_stats(cpi, &next_frame);
1878 accumulate_stats(&sectionstats, &next_frame); 2144 accumulate_stats(&sectionstats, &next_frame);
1879 } 2145 }
1880 2146
1881 avg_stats(&sectionstats); 2147 avg_stats(&sectionstats);
1882 2148
1883 cpi->section_intra_rating = 2149 cpi->twopass.section_intra_rating =
1884 sectionstats.intra_error / 2150 sectionstats.intra_error /
1885 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error); 2151 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
1886 2152
1887 Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.code d_error); 2153 Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.code d_error);
1888 //if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) ) 2154 //if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) )
1889 //{ 2155 //{
1890 cpi->section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025); 2156 cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
1891 2157
1892 if (cpi->section_max_qfactor < 0.80) 2158 if (cpi->twopass.section_max_qfactor < 0.80)
1893 cpi->section_max_qfactor = 0.80; 2159 cpi->twopass.section_max_qfactor = 0.80;
1894 2160
1895 //} 2161 //}
1896 //else 2162 //else
1897 // cpi->section_max_qfactor = 1.0; 2163 // cpi->twopass.section_max_qfactor = 1.0;
1898 2164
1899 reset_fpf_position(cpi, start_pos); 2165 reset_fpf_position(cpi, start_pos);
1900 } 2166 }
1901 } 2167 }
1902 2168
1903 // Allocate bits to a normal frame that is neither a gf an arf or a key frame. 2169 // Allocate bits to a normal frame that is neither a gf an arf or a key frame.
1904 static void assign_std_frame_bits(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame) 2170 static void assign_std_frame_bits(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
1905 { 2171 {
1906 int target_frame_size; // gf_group_error_left 2172 int target_frame_size; // gf_group_error_left
1907 2173
1908 double modified_err; 2174 double modified_err;
1909 double err_fraction; // What portion of the remaining GF group error is used by this frame 2175 double err_fraction; // What portion of the remaining GF group error is used by this frame
1910 2176
1911 int max_bits = frame_max_bits(cpi); // Max for a single frame 2177 int max_bits = frame_max_bits(cpi); // Max for a single frame
1912 2178
1913 // The final few frames have special treatment
1914 if (cpi->frames_till_gf_update_due >= (int)(cpi->total_stats->count - cpi->c ommon.current_video_frame))
1915 {
1916 cpi->gf_group_bits = (cpi->bits_left > 0) ? cpi->bits_left : 0;;
1917 }
1918
1919 // Calculate modified prediction error used in bit allocation 2179 // Calculate modified prediction error used in bit allocation
1920 modified_err = calculate_modified_err(cpi, this_frame); 2180 modified_err = calculate_modified_err(cpi, this_frame);
1921 2181
1922 if (cpi->gf_group_error_left > 0) 2182 if (cpi->twopass.gf_group_error_left > 0)
1923 err_fraction = modified_err / cpi->gf_group_error_left; // What portion of the remaining GF group error is used by this fra me 2183 err_fraction = modified_err / cpi->twopass.gf_group_error_left; // What portion of the remaining GF group error is used by this frame
1924 else 2184 else
1925 err_fraction = 0.0; 2185 err_fraction = 0.0;
1926 2186
1927 target_frame_size = (int)((double)cpi->gf_group_bits * err_fraction); // How many of those bits available for allocation should we give i t? 2187 target_frame_size = (int)((double)cpi->twopass.gf_group_bits * err_fraction) ; // How many of those bits available for allocation should w e give it?
1928 2188
1929 // Clip to target size to 0 - max_bits (or cpi->gf_group_bits) at the top en d. 2189 // Clip to target size to 0 - max_bits (or cpi->twopass.gf_group_bits) at th e top end.
1930 if (target_frame_size < 0) 2190 if (target_frame_size < 0)
1931 target_frame_size = 0; 2191 target_frame_size = 0;
1932 else 2192 else
1933 { 2193 {
1934 if (target_frame_size > max_bits) 2194 if (target_frame_size > max_bits)
1935 target_frame_size = max_bits; 2195 target_frame_size = max_bits;
1936 2196
1937 if (target_frame_size > cpi->gf_group_bits) 2197 if (target_frame_size > cpi->twopass.gf_group_bits)
1938 target_frame_size = cpi->gf_group_bits; 2198 target_frame_size = cpi->twopass.gf_group_bits;
1939 } 2199 }
1940 2200
1941 cpi->gf_group_error_left -= modified_err; // Adjust error remaining 2201 cpi->twopass.gf_group_error_left -= modified_err; // Adjust error remaining
1942 cpi->gf_group_bits -= target_frame_size; // Adjust bits remaining 2202 cpi->twopass.gf_group_bits -= target_frame_size; // Adjust bits remaining
1943 2203
1944 if (cpi->gf_group_bits < 0) 2204 if (cpi->twopass.gf_group_bits < 0)
1945 cpi->gf_group_bits = 0; 2205 cpi->twopass.gf_group_bits = 0;
1946 2206
1947 target_frame_size += cpi->min_frame_bandwidth; // Add in the minimum number of bits that is set aside for every fra me. 2207 target_frame_size += cpi->min_frame_bandwidth; // Add in the minimum number of bits that is set aside for every fra me.
1948 2208
1949 // Special case for the frame that lies half way between two gfs 2209 // Special case for the frame that lies half way between two gfs
1950 if (cpi->common.frames_since_golden == cpi->baseline_gf_interval / 2) 2210 if (cpi->common.frames_since_golden == cpi->baseline_gf_interval / 2)
1951 target_frame_size += cpi->mid_gf_extra_bits; 2211 target_frame_size += cpi->twopass.mid_gf_extra_bits;
1952 2212
1953 cpi->per_frame_bandwidth = target_frame_size; // Per frame bit target for this frame 2213 cpi->per_frame_bandwidth = target_frame_size; // Per frame bit target for this frame
1954 } 2214 }
1955 2215
1956 void vp8_second_pass(VP8_COMP *cpi) 2216 void vp8_second_pass(VP8_COMP *cpi)
1957 { 2217 {
1958 int tmp_q; 2218 int tmp_q;
1959 int frames_left = (int)(cpi->total_stats->count - cpi->common.current_video_ frame); 2219 int frames_left = (int)(cpi->twopass.total_stats->count - cpi->common.curren t_video_frame);
1960 2220
1961 FIRSTPASS_STATS this_frame; 2221 FIRSTPASS_STATS this_frame;
1962 FIRSTPASS_STATS this_frame_copy; 2222 FIRSTPASS_STATS this_frame_copy;
1963 2223
1964 VP8_COMMON *cm = &cpi->common;
1965
1966 double this_frame_error; 2224 double this_frame_error;
1967 double this_frame_intra_error; 2225 double this_frame_intra_error;
1968 double this_frame_coded_error; 2226 double this_frame_coded_error;
1969 2227
1970 FIRSTPASS_STATS *start_pos; 2228 FIRSTPASS_STATS *start_pos;
1971 2229
1972 if (!cpi->stats_in) 2230 if (!cpi->twopass.stats_in)
1973 { 2231 {
1974 return ; 2232 return ;
1975 } 2233 }
1976 2234
1977 vp8_clear_system_state(); 2235 vp8_clear_system_state();
1978 2236
1979 if (EOF == input_stats(cpi, &this_frame)) 2237 if (EOF == input_stats(cpi, &this_frame))
1980 return; 2238 return;
1981 2239
1982 this_frame_error = this_frame.ssim_weighted_pred_err; 2240 this_frame_error = this_frame.ssim_weighted_pred_err;
1983 this_frame_intra_error = this_frame.intra_error; 2241 this_frame_intra_error = this_frame.intra_error;
1984 this_frame_coded_error = this_frame.coded_error; 2242 this_frame_coded_error = this_frame.coded_error;
1985 2243
1986 // Store information regarding level of motion etc for use mode decisions. 2244 start_pos = cpi->twopass.stats_in;
1987 cpi->motion_speed = (int)(fabs(this_frame.MVr) + fabs(this_frame.MVc));
1988 cpi->motion_var = (int)(fabs(this_frame.MVrv) + fabs(this_frame.MVcv));
1989 cpi->inter_lvl = (int)(this_frame.pcnt_inter * 100);
1990 cpi->intra_lvl = (int)((1.0 - this_frame.pcnt_inter) * 100);
1991 cpi->motion_lvl = (int)(this_frame.pcnt_motion * 100);
1992
1993 start_pos = cpi->stats_in;
1994 2245
1995 // keyframe and section processing ! 2246 // keyframe and section processing !
1996 if (cpi->frames_to_key == 0) 2247 if (cpi->twopass.frames_to_key == 0)
1997 { 2248 {
1998 // Define next KF group and assign bits to it 2249 // Define next KF group and assign bits to it
1999 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame)); 2250 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2000 find_next_key_frame(cpi, &this_frame_copy); 2251 find_next_key_frame(cpi, &this_frame_copy);
2001 2252
2002 // Special case: Error error_resilient_mode mode does not make much sens e for two pass but with its current meaning but this code is designed to stop 2253 // Special case: Error error_resilient_mode mode does not make much sens e for two pass but with its current meaning but this code is designed to stop
2003 // outlandish behaviour if someone does set it when using two pass. It e ffectively disables GF groups. 2254 // outlandish behaviour if someone does set it when using two pass. It e ffectively disables GF groups.
2004 // This is temporary code till we decide what should really happen in th is case. 2255 // This is temporary code till we decide what should really happen in th is case.
2005 if (cpi->oxcf.error_resilient_mode) 2256 if (cpi->oxcf.error_resilient_mode)
2006 { 2257 {
2007 cpi->gf_group_bits = cpi->kf_group_bits; 2258 cpi->twopass.gf_group_bits = cpi->twopass.kf_group_bits;
2008 cpi->gf_group_error_left = cpi->kf_group_error_left; 2259 cpi->twopass.gf_group_error_left = cpi->twopass.kf_group_error_left;
2009 cpi->baseline_gf_interval = cpi->frames_to_key; 2260 cpi->baseline_gf_interval = cpi->twopass.frames_to_key;
2010 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; 2261 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2011 cpi->source_alt_ref_pending = FALSE; 2262 cpi->source_alt_ref_pending = FALSE;
2012 } 2263 }
2013 2264
2014 } 2265 }
2015 2266
2016 // Is this a GF / ARF (Note that a KF is always also a GF) 2267 // Is this a GF / ARF (Note that a KF is always also a GF)
2017 if (cpi->frames_till_gf_update_due == 0) 2268 if (cpi->frames_till_gf_update_due == 0)
2018 { 2269 {
2019 // Update monitor of the bits per error observed so far.
2020 // Done once per gf group based on what has gone before
2021 // so do nothing if this is the first frame.
2022 if (cpi->common.current_video_frame > 0)
2023 {
2024 cpi->observed_bpe =
2025 (double)(cpi->clip_bits_total - cpi->bits_left) /
2026 cpi->modified_error_used;
2027 }
2028
2029 // Define next gf group and assign bits to it 2270 // Define next gf group and assign bits to it
2030 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame)); 2271 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2031 define_gf_group(cpi, &this_frame_copy); 2272 define_gf_group(cpi, &this_frame_copy);
2032 2273
2033 // If we are going to code an altref frame at the end of the group and t he current frame is not a key frame.... 2274 // If we are going to code an altref frame at the end of the group and t he current frame is not a key frame....
2034 // If the previous group used an arf this frame has already benefited fr om that arf boost and it should not be given extra bits 2275 // If the previous group used an arf this frame has already benefited fr om that arf boost and it should not be given extra bits
2035 // If the previous group was NOT coded using arf we may want to apply so me boost to this GF as well 2276 // If the previous group was NOT coded using arf we may want to apply so me boost to this GF as well
2036 if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME) ) 2277 if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME) )
2037 { 2278 {
2038 // Assign a standard frames worth of bits from those allocated to th e GF group 2279 // Assign a standard frames worth of bits from those allocated to th e GF group
2280 int bak = cpi->per_frame_bandwidth;
2039 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame)); 2281 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2040 assign_std_frame_bits(cpi, &this_frame_copy); 2282 assign_std_frame_bits(cpi, &this_frame_copy);
2041 2283 cpi->per_frame_bandwidth = bak;
2042 // If appropriate (we are switching into ARF active but it was not p reviously active) apply a boost for the gf at the start of the group.
2043 //if ( !cpi->source_alt_ref_active && (cpi->gfu_boost > 150) )
2044 if (FALSE)
2045 {
2046 int extra_bits;
2047 int pct_extra = (cpi->gfu_boost - 100) / 50;
2048
2049 pct_extra = (pct_extra > 20) ? 20 : pct_extra;
2050
2051 extra_bits = (cpi->gf_group_bits * pct_extra) / 100;
2052 cpi->gf_group_bits -= extra_bits;
2053 cpi->per_frame_bandwidth += extra_bits;
2054 }
2055 } 2284 }
2056 } 2285 }
2057 2286
2058 // Otherwise this is an ordinary frame 2287 // Otherwise this is an ordinary frame
2059 else 2288 else
2060 { 2289 {
2061 // Special case: Error error_resilient_mode mode does not make much sens e for two pass but with its current meaning but this code is designed to stop 2290 // Special case: Error error_resilient_mode mode does not make much sens e for two pass but with its current meaning but this code is designed to stop
2062 // outlandish behaviour if someone does set it when using two pass. It e ffectively disables GF groups. 2291 // outlandish behaviour if someone does set it when using two pass. It e ffectively disables GF groups.
2063 // This is temporary code till we decide what should really happen in th is case. 2292 // This is temporary code till we decide what should really happen in th is case.
2064 if (cpi->oxcf.error_resilient_mode) 2293 if (cpi->oxcf.error_resilient_mode)
2065 { 2294 {
2066 cpi->frames_till_gf_update_due = cpi->frames_to_key; 2295 cpi->frames_till_gf_update_due = cpi->twopass.frames_to_key;
2067 2296
2068 if (cpi->common.frame_type != KEY_FRAME) 2297 if (cpi->common.frame_type != KEY_FRAME)
2069 { 2298 {
2070 // Assign bits from those allocated to the GF group 2299 // Assign bits from those allocated to the GF group
2071 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame)); 2300 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2072 assign_std_frame_bits(cpi, &this_frame_copy); 2301 assign_std_frame_bits(cpi, &this_frame_copy);
2073 } 2302 }
2074 } 2303 }
2075 else 2304 else
2076 { 2305 {
2077 // Assign bits from those allocated to the GF group 2306 // Assign bits from those allocated to the GF group
2078 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame)); 2307 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2079 assign_std_frame_bits(cpi, &this_frame_copy); 2308 assign_std_frame_bits(cpi, &this_frame_copy);
2080 } 2309 }
2081 } 2310 }
2082 2311
2083 // Keep a globally available copy of this and the next frame's iiratio. 2312 // Keep a globally available copy of this and the next frame's iiratio.
2084 cpi->this_iiratio = this_frame_intra_error / 2313 cpi->twopass.this_iiratio = this_frame_intra_error /
2085 DOUBLE_DIVIDE_CHECK(this_frame_coded_error); 2314 DOUBLE_DIVIDE_CHECK(this_frame_coded_error);
2086 { 2315 {
2087 FIRSTPASS_STATS next_frame; 2316 FIRSTPASS_STATS next_frame;
2088 if ( lookup_next_frame_stats(cpi, &next_frame) != EOF ) 2317 if ( lookup_next_frame_stats(cpi, &next_frame) != EOF )
2089 { 2318 {
2090 cpi->next_iiratio = next_frame.intra_error / 2319 cpi->twopass.next_iiratio = next_frame.intra_error /
2091 DOUBLE_DIVIDE_CHECK(next_frame.coded_error); 2320 DOUBLE_DIVIDE_CHECK(next_frame.coded_error);
2092 } 2321 }
2093 } 2322 }
2094 2323
2095 // Set nominal per second bandwidth for this frame 2324 // Set nominal per second bandwidth for this frame
2096 cpi->target_bandwidth = cpi->per_frame_bandwidth * cpi->output_frame_rate; 2325 cpi->target_bandwidth = cpi->per_frame_bandwidth * cpi->output_frame_rate;
2097 if (cpi->target_bandwidth < 0) 2326 if (cpi->target_bandwidth < 0)
2098 cpi->target_bandwidth = 0; 2327 cpi->target_bandwidth = 0;
2099 2328
2100 if (cpi->common.current_video_frame == 0) 2329 if (cpi->common.current_video_frame == 0)
2101 { 2330 {
2102 cpi->est_max_qcorrection_factor = 1.0; 2331 cpi->twopass.est_max_qcorrection_factor = 1.0;
2103 2332
2104 // Experimental code to try and set a cq_level in constrained 2333 // Experimental code to try and set a cq_level in constrained
2105 // quality mode. 2334 // quality mode.
2106 if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY ) 2335 if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY )
2107 { 2336 {
2108 int est_cq; 2337 int est_cq;
2109 2338
2110 est_cq = 2339 est_cq =
2111 estimate_cq( cpi, 2340 estimate_cq( cpi,
2112 (cpi->total_coded_error_left / frames_left), 2341 (cpi->twopass.total_coded_error_left / frames_left) ,
2113 (int)(cpi->bits_left / frames_left)); 2342 (int)(cpi->twopass.bits_left / frames_left));
2114 2343
2115 cpi->cq_target_quality = cpi->oxcf.cq_level; 2344 cpi->cq_target_quality = cpi->oxcf.cq_level;
2116 if ( est_cq > cpi->cq_target_quality ) 2345 if ( est_cq > cpi->cq_target_quality )
2117 cpi->cq_target_quality = est_cq; 2346 cpi->cq_target_quality = est_cq;
2118 } 2347 }
2119 2348
2120 // guess at maxq needed in 2nd pass 2349 // guess at maxq needed in 2nd pass
2121 cpi->maxq_max_limit = cpi->worst_quality; 2350 cpi->twopass.maxq_max_limit = cpi->worst_quality;
2122 cpi->maxq_min_limit = cpi->best_quality; 2351 cpi->twopass.maxq_min_limit = cpi->best_quality;
2123 tmp_q = estimate_max_q( cpi, 2352 tmp_q = estimate_max_q( cpi,
2124 (cpi->total_coded_error_left / frames_left), 2353 (cpi->twopass.total_coded_error_left / frames_le ft),
2125 (int)(cpi->bits_left / frames_left)); 2354 (int)(cpi->twopass.bits_left / frames_left));
2126 2355
2127 // Limit the maxq value returned subsequently. 2356 // Limit the maxq value returned subsequently.
2128 // This increases the risk of overspend or underspend if the initial 2357 // This increases the risk of overspend or underspend if the initial
2129 // estimate for the clip is bad, but helps prevent excessive 2358 // estimate for the clip is bad, but helps prevent excessive
2130 // variation in Q, especially near the end of a clip 2359 // variation in Q, especially near the end of a clip
2131 // where for example a small overspend may cause Q to crash 2360 // where for example a small overspend may cause Q to crash
2132 cpi->maxq_max_limit = ((tmp_q + 32) < cpi->worst_quality) 2361 cpi->twopass.maxq_max_limit = ((tmp_q + 32) < cpi->worst_quality)
2133 ? (tmp_q + 32) : cpi->worst_quality; 2362 ? (tmp_q + 32) : cpi->worst_quality;
2134 cpi->maxq_min_limit = ((tmp_q - 32) > cpi->best_quality) 2363 cpi->twopass.maxq_min_limit = ((tmp_q - 32) > cpi->best_quality)
2135 ? (tmp_q - 32) : cpi->best_quality; 2364 ? (tmp_q - 32) : cpi->best_quality;
2136 2365
2137 cpi->active_worst_quality = tmp_q; 2366 cpi->active_worst_quality = tmp_q;
2138 cpi->ni_av_qi = tmp_q; 2367 cpi->ni_av_qi = tmp_q;
2139 } 2368 }
2140 2369
2141 // The last few frames of a clip almost always have to few or too many 2370 // The last few frames of a clip almost always have to few or too many
2142 // bits and for the sake of over exact rate control we dont want to make 2371 // bits and for the sake of over exact rate control we dont want to make
2143 // radical adjustments to the allowed quantizer range just to use up a 2372 // radical adjustments to the allowed quantizer range just to use up a
2144 // few surplus bits or get beneath the target rate. 2373 // few surplus bits or get beneath the target rate.
2145 else if ( (cpi->common.current_video_frame < 2374 else if ( (cpi->common.current_video_frame <
2146 (((unsigned int)cpi->total_stats->count * 255)>>8)) && 2375 (((unsigned int)cpi->twopass.total_stats->count * 255)>>8)) &&
2147 ((cpi->common.current_video_frame + cpi->baseline_gf_interval) < 2376 ((cpi->common.current_video_frame + cpi->baseline_gf_interval) <
2148 (unsigned int)cpi->total_stats->count) ) 2377 (unsigned int)cpi->twopass.total_stats->count) )
2149 { 2378 {
2150 if (frames_left < 1) 2379 if (frames_left < 1)
2151 frames_left = 1; 2380 frames_left = 1;
2152 2381
2153 tmp_q = estimate_max_q(cpi, (cpi->total_coded_error_left / frames_left), (int)(cpi->bits_left / frames_left)); 2382 tmp_q = estimate_max_q(cpi, (cpi->twopass.total_coded_error_left / frame s_left), (int)(cpi->twopass.bits_left / frames_left));
2154 2383
2155 // Move active_worst_quality but in a damped way 2384 // Move active_worst_quality but in a damped way
2156 if (tmp_q > cpi->active_worst_quality) 2385 if (tmp_q > cpi->active_worst_quality)
2157 cpi->active_worst_quality ++; 2386 cpi->active_worst_quality ++;
2158 else if (tmp_q < cpi->active_worst_quality) 2387 else if (tmp_q < cpi->active_worst_quality)
2159 cpi->active_worst_quality --; 2388 cpi->active_worst_quality --;
2160 2389
2161 cpi->active_worst_quality = ((cpi->active_worst_quality * 3) + tmp_q + 2 ) / 4; 2390 cpi->active_worst_quality = ((cpi->active_worst_quality * 3) + tmp_q + 2 ) / 4;
2162 } 2391 }
2163 2392
2164 cpi->frames_to_key --; 2393 cpi->twopass.frames_to_key --;
2165 cpi->total_error_left -= this_frame_error; 2394 cpi->twopass.total_error_left -= this_frame_error;
2166 cpi->total_intra_error_left -= this_frame_intra_error; 2395 cpi->twopass.total_intra_error_left -= this_frame_intra_error;
2167 cpi->total_coded_error_left -= this_frame_coded_error; 2396 cpi->twopass.total_coded_error_left -= this_frame_coded_error;
2168 } 2397 }
2169 2398
2170 2399
2171 static BOOL test_candidate_kf(VP8_COMP *cpi, FIRSTPASS_STATS *last_frame, FIRST PASS_STATS *this_frame, FIRSTPASS_STATS *next_frame) 2400 static BOOL test_candidate_kf(VP8_COMP *cpi, FIRSTPASS_STATS *last_frame, FIRST PASS_STATS *this_frame, FIRSTPASS_STATS *next_frame)
2172 { 2401 {
2173 BOOL is_viable_kf = FALSE; 2402 BOOL is_viable_kf = FALSE;
2174 2403
2175 // Does the frame satisfy the primary criteria of a key frame 2404 // Does the frame satisfy the primary criteria of a key frame
2176 // If so, then examine how well it predicts subsequent frames 2405 // If so, then examine how well it predicts subsequent frames
2177 if ((this_frame->pcnt_second_ref < 0.10) && 2406 if ((this_frame->pcnt_second_ref < 0.10) &&
(...skipping 16 matching lines...) Expand all
2194 FIRSTPASS_STATS local_next_frame; 2423 FIRSTPASS_STATS local_next_frame;
2195 2424
2196 double boost_score = 0.0; 2425 double boost_score = 0.0;
2197 double old_boost_score = 0.0; 2426 double old_boost_score = 0.0;
2198 double decay_accumulator = 1.0; 2427 double decay_accumulator = 1.0;
2199 double next_iiratio; 2428 double next_iiratio;
2200 2429
2201 vpx_memcpy(&local_next_frame, next_frame, sizeof(*next_frame)); 2430 vpx_memcpy(&local_next_frame, next_frame, sizeof(*next_frame));
2202 2431
2203 // Note the starting file position so we can reset to it 2432 // Note the starting file position so we can reset to it
2204 start_pos = cpi->stats_in; 2433 start_pos = cpi->twopass.stats_in;
2205 2434
2206 // Examine how well the key frame predicts subsequent frames 2435 // Examine how well the key frame predicts subsequent frames
2207 for (i = 0 ; i < 16; i++) 2436 for (i = 0 ; i < 16; i++)
2208 { 2437 {
2209 next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / DOUBLE_D IVIDE_CHECK(local_next_frame.coded_error)) ; 2438 next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / DOUBLE_D IVIDE_CHECK(local_next_frame.coded_error)) ;
2210 2439
2211 if (next_iiratio > RMAX) 2440 if (next_iiratio > RMAX)
2212 next_iiratio = RMAX; 2441 next_iiratio = RMAX;
2213 2442
2214 // Cumulative effect of decay in prediction quality 2443 // Cumulative effect of decay in prediction quality
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
2266 2495
2267 double decay_accumulator = 1.0; 2496 double decay_accumulator = 1.0;
2268 double boost_score = 0; 2497 double boost_score = 0;
2269 double old_boost_score = 0.0; 2498 double old_boost_score = 0.0;
2270 double loop_decay_rate; 2499 double loop_decay_rate;
2271 2500
2272 double kf_mod_err = 0.0; 2501 double kf_mod_err = 0.0;
2273 double kf_group_err = 0.0; 2502 double kf_group_err = 0.0;
2274 double kf_group_intra_err = 0.0; 2503 double kf_group_intra_err = 0.0;
2275 double kf_group_coded_err = 0.0; 2504 double kf_group_coded_err = 0.0;
2276 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth * cpi->oxcf.t wo_pass_vbrmin_section / 100);
2277 double recent_loop_decay[8] = {1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0}; 2505 double recent_loop_decay[8] = {1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0};
2278 2506
2279 vpx_memset(&next_frame, 0, sizeof(next_frame)); // assure clean 2507 vpx_memset(&next_frame, 0, sizeof(next_frame)); // assure clean
2280 2508
2281 vp8_clear_system_state(); //__asm emms; 2509 vp8_clear_system_state(); //__asm emms;
2282 start_position = cpi->stats_in; 2510 start_position = cpi->twopass.stats_in;
2283 2511
2284 cpi->common.frame_type = KEY_FRAME; 2512 cpi->common.frame_type = KEY_FRAME;
2285 2513
2286 // is this a forced key frame by interval 2514 // is this a forced key frame by interval
2287 cpi->this_key_frame_forced = cpi->next_key_frame_forced; 2515 cpi->this_key_frame_forced = cpi->next_key_frame_forced;
2288 2516
2289 // Clear the alt ref active flag as this can never be active on a key frame 2517 // Clear the alt ref active flag as this can never be active on a key frame
2290 cpi->source_alt_ref_active = FALSE; 2518 cpi->source_alt_ref_active = FALSE;
2291 2519
2292 // Kf is always a gf so clear frames till next gf counter 2520 // Kf is always a gf so clear frames till next gf counter
2293 cpi->frames_till_gf_update_due = 0; 2521 cpi->frames_till_gf_update_due = 0;
2294 2522
2295 cpi->frames_to_key = 1; 2523 cpi->twopass.frames_to_key = 1;
2296 2524
2297 // Take a copy of the initial frame details 2525 // Take a copy of the initial frame details
2298 vpx_memcpy(&first_frame, this_frame, sizeof(*this_frame)); 2526 vpx_memcpy(&first_frame, this_frame, sizeof(*this_frame));
2299 2527
2300 cpi->kf_group_bits = 0; // Total bits avaialable to kf group 2528 cpi->twopass.kf_group_bits = 0; // Total bits avaialable to kf group
2301 cpi->kf_group_error_left = 0; // Group modified error score. 2529 cpi->twopass.kf_group_error_left = 0; // Group modified error score.
2302 2530
2303 kf_mod_err = calculate_modified_err(cpi, this_frame); 2531 kf_mod_err = calculate_modified_err(cpi, this_frame);
2304 2532
2305 // find the next keyframe 2533 // find the next keyframe
2306 i = 0; 2534 i = 0;
2307 while (cpi->stats_in < cpi->stats_in_end) 2535 while (cpi->twopass.stats_in < cpi->twopass.stats_in_end)
2308 { 2536 {
2309 // Accumulate kf group error 2537 // Accumulate kf group error
2310 kf_group_err += calculate_modified_err(cpi, this_frame); 2538 kf_group_err += calculate_modified_err(cpi, this_frame);
2311 2539
2312 // These figures keep intra and coded error counts for all frames includ ing key frames in the group. 2540 // These figures keep intra and coded error counts for all frames includ ing key frames in the group.
2313 // The effect of the key frame itself can be subtracted out using the fi rst_frame data collected above 2541 // The effect of the key frame itself can be subtracted out using the fi rst_frame data collected above
2314 kf_group_intra_err += this_frame->intra_error; 2542 kf_group_intra_err += this_frame->intra_error;
2315 kf_group_coded_err += this_frame->coded_error; 2543 kf_group_coded_err += this_frame->coded_error;
2316 2544
2317 // load a the next frame's stats 2545 // load a the next frame's stats
(...skipping 26 matching lines...) Expand all
2344 if ( detect_transition_to_still( cpi, i, 2572 if ( detect_transition_to_still( cpi, i,
2345 (cpi->key_frame_frequency-i), 2573 (cpi->key_frame_frequency-i),
2346 loop_decay_rate, 2574 loop_decay_rate,
2347 decay_accumulator ) ) 2575 decay_accumulator ) )
2348 { 2576 {
2349 break; 2577 break;
2350 } 2578 }
2351 2579
2352 2580
2353 // Step on to the next frame 2581 // Step on to the next frame
2354 cpi->frames_to_key ++; 2582 cpi->twopass.frames_to_key ++;
2355 2583
2356 // If we don't have a real key frame within the next two 2584 // If we don't have a real key frame within the next two
2357 // forcekeyframeevery intervals then break out of the loop. 2585 // forcekeyframeevery intervals then break out of the loop.
2358 if (cpi->frames_to_key >= 2 *(int)cpi->key_frame_frequency) 2586 if (cpi->twopass.frames_to_key >= 2 *(int)cpi->key_frame_frequency)
2359 break; 2587 break;
2360 } else 2588 } else
2361 cpi->frames_to_key ++; 2589 cpi->twopass.frames_to_key ++;
2362 2590
2363 i++; 2591 i++;
2364 } 2592 }
2365 2593
2366 // If there is a max kf interval set by the user we must obey it. 2594 // If there is a max kf interval set by the user we must obey it.
2367 // We already breakout of the loop above at 2x max. 2595 // We already breakout of the loop above at 2x max.
2368 // This code centers the extra kf if the actual natural 2596 // This code centers the extra kf if the actual natural
2369 // interval is between 1x and 2x 2597 // interval is between 1x and 2x
2370 if (cpi->oxcf.auto_key 2598 if (cpi->oxcf.auto_key
2371 && cpi->frames_to_key > (int)cpi->key_frame_frequency ) 2599 && cpi->twopass.frames_to_key > (int)cpi->key_frame_frequency )
2372 { 2600 {
2373 FIRSTPASS_STATS *current_pos = cpi->stats_in; 2601 FIRSTPASS_STATS *current_pos = cpi->twopass.stats_in;
2374 FIRSTPASS_STATS tmp_frame; 2602 FIRSTPASS_STATS tmp_frame;
2375 2603
2376 cpi->frames_to_key /= 2; 2604 cpi->twopass.frames_to_key /= 2;
2377 2605
2378 // Copy first frame details 2606 // Copy first frame details
2379 vpx_memcpy(&tmp_frame, &first_frame, sizeof(first_frame)); 2607 vpx_memcpy(&tmp_frame, &first_frame, sizeof(first_frame));
2380 2608
2381 // Reset to the start of the group 2609 // Reset to the start of the group
2382 reset_fpf_position(cpi, start_position); 2610 reset_fpf_position(cpi, start_position);
2383 2611
2384 kf_group_err = 0; 2612 kf_group_err = 0;
2385 kf_group_intra_err = 0; 2613 kf_group_intra_err = 0;
2386 kf_group_coded_err = 0; 2614 kf_group_coded_err = 0;
2387 2615
2388 // Rescan to get the correct error data for the forced kf group 2616 // Rescan to get the correct error data for the forced kf group
2389 for( i = 0; i < cpi->frames_to_key; i++ ) 2617 for( i = 0; i < cpi->twopass.frames_to_key; i++ )
2390 { 2618 {
2391 // Accumulate kf group errors 2619 // Accumulate kf group errors
2392 kf_group_err += calculate_modified_err(cpi, &tmp_frame); 2620 kf_group_err += calculate_modified_err(cpi, &tmp_frame);
2393 kf_group_intra_err += tmp_frame.intra_error; 2621 kf_group_intra_err += tmp_frame.intra_error;
2394 kf_group_coded_err += tmp_frame.coded_error; 2622 kf_group_coded_err += tmp_frame.coded_error;
2395 2623
2396 // Load a the next frame's stats 2624 // Load a the next frame's stats
2397 input_stats(cpi, &tmp_frame); 2625 input_stats(cpi, &tmp_frame);
2398 } 2626 }
2399 2627
2400 // Reset to the start of the group 2628 // Reset to the start of the group
2401 reset_fpf_position(cpi, current_pos); 2629 reset_fpf_position(cpi, current_pos);
2402 2630
2403 cpi->next_key_frame_forced = TRUE; 2631 cpi->next_key_frame_forced = TRUE;
2404 } 2632 }
2405 else 2633 else
2406 cpi->next_key_frame_forced = FALSE; 2634 cpi->next_key_frame_forced = FALSE;
2407 2635
2408 // Special case for the last frame of the file 2636 // Special case for the last frame of the file
2409 if (cpi->stats_in >= cpi->stats_in_end) 2637 if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
2410 { 2638 {
2411 // Accumulate kf group error 2639 // Accumulate kf group error
2412 kf_group_err += calculate_modified_err(cpi, this_frame); 2640 kf_group_err += calculate_modified_err(cpi, this_frame);
2413 2641
2414 // These figures keep intra and coded error counts for all frames includ ing key frames in the group. 2642 // These figures keep intra and coded error counts for all frames includ ing key frames in the group.
2415 // The effect of the key frame itself can be subtracted out using the fi rst_frame data collected above 2643 // The effect of the key frame itself can be subtracted out using the fi rst_frame data collected above
2416 kf_group_intra_err += this_frame->intra_error; 2644 kf_group_intra_err += this_frame->intra_error;
2417 kf_group_coded_err += this_frame->coded_error; 2645 kf_group_coded_err += this_frame->coded_error;
2418 } 2646 }
2419 2647
2420 // Calculate the number of bits that should be assigned to the kf group. 2648 // Calculate the number of bits that should be assigned to the kf group.
2421 if ((cpi->bits_left > 0) && (cpi->modified_error_left > 0.0)) 2649 if ((cpi->twopass.bits_left > 0) && (cpi->twopass.modified_error_left > 0.0) )
2422 { 2650 {
2423 // Max for a single normal frame (not key frame) 2651 // Max for a single normal frame (not key frame)
2424 int max_bits = frame_max_bits(cpi); 2652 int max_bits = frame_max_bits(cpi);
2425 2653
2426 // Maximum bits for the kf group 2654 // Maximum bits for the kf group
2427 long long max_grp_bits; 2655 int64_t max_grp_bits;
2428 2656
2429 // Default allocation based on bits left and relative 2657 // Default allocation based on bits left and relative
2430 // complexity of the section 2658 // complexity of the section
2431 cpi->kf_group_bits = (long long)( cpi->bits_left * 2659 cpi->twopass.kf_group_bits = (int64_t)( cpi->twopass.bits_left *
2432 ( kf_group_err / 2660 ( kf_group_err /
2433 cpi->modified_error_left )); 2661 cpi->twopass.modified_error_left ));
2434 2662
2435 // Clip based on maximum per frame rate defined by the user. 2663 // Clip based on maximum per frame rate defined by the user.
2436 max_grp_bits = (long long)max_bits * (long long)cpi->frames_to_key; 2664 max_grp_bits = (int64_t)max_bits * (int64_t)cpi->twopass.frames_to_key;
2437 if (cpi->kf_group_bits > max_grp_bits) 2665 if (cpi->twopass.kf_group_bits > max_grp_bits)
2438 cpi->kf_group_bits = max_grp_bits; 2666 cpi->twopass.kf_group_bits = max_grp_bits;
2439 2667
2440 // Additional special case for CBR if buffer is getting full. 2668 // Additional special case for CBR if buffer is getting full.
2441 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) 2669 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2442 { 2670 {
2443 int opt_buffer_lvl = cpi->oxcf.optimal_buffer_level; 2671 int opt_buffer_lvl = cpi->oxcf.optimal_buffer_level;
2444 int buffer_lvl = cpi->buffer_level; 2672 int buffer_lvl = cpi->buffer_level;
2445 2673
2446 // If the buffer is near or above the optimal and this kf group is 2674 // If the buffer is near or above the optimal and this kf group is
2447 // not being allocated much then increase the allocation a bit. 2675 // not being allocated much then increase the allocation a bit.
2448 if (buffer_lvl >= opt_buffer_lvl) 2676 if (buffer_lvl >= opt_buffer_lvl)
2449 { 2677 {
2450 int high_water_mark = (opt_buffer_lvl + 2678 int high_water_mark = (opt_buffer_lvl +
2451 cpi->oxcf.maximum_buffer_size) >> 1; 2679 cpi->oxcf.maximum_buffer_size) >> 1;
2452 2680
2453 long long av_group_bits; 2681 int64_t av_group_bits;
2454 2682
2455 // Av bits per frame * number of frames 2683 // Av bits per frame * number of frames
2456 av_group_bits = (long long)cpi->av_per_frame_bandwidth * 2684 av_group_bits = (int64_t)cpi->av_per_frame_bandwidth *
2457 (long long)cpi->frames_to_key; 2685 (int64_t)cpi->twopass.frames_to_key;
2458 2686
2459 // We are at or above the maximum. 2687 // We are at or above the maximum.
2460 if (cpi->buffer_level >= high_water_mark) 2688 if (cpi->buffer_level >= high_water_mark)
2461 { 2689 {
2462 long long min_group_bits; 2690 int64_t min_group_bits;
2463 2691
2464 min_group_bits = av_group_bits + 2692 min_group_bits = av_group_bits +
2465 (long long)(buffer_lvl - 2693 (int64_t)(buffer_lvl -
2466 high_water_mark); 2694 high_water_mark);
2467 2695
2468 if (cpi->kf_group_bits < min_group_bits) 2696 if (cpi->twopass.kf_group_bits < min_group_bits)
2469 cpi->kf_group_bits = min_group_bits; 2697 cpi->twopass.kf_group_bits = min_group_bits;
2470 } 2698 }
2471 // We are above optimal but below the maximum 2699 // We are above optimal but below the maximum
2472 else if (cpi->kf_group_bits < av_group_bits) 2700 else if (cpi->twopass.kf_group_bits < av_group_bits)
2473 { 2701 {
2474 long long bits_below_av = av_group_bits - 2702 int64_t bits_below_av = av_group_bits -
2475 cpi->kf_group_bits; 2703 cpi->twopass.kf_group_bits;
2476 2704
2477 cpi->kf_group_bits += 2705 cpi->twopass.kf_group_bits +=
2478 (long long)((double)bits_below_av * 2706 (int64_t)((double)bits_below_av *
2479 (double)(buffer_lvl - opt_buffer_lvl) / 2707 (double)(buffer_lvl - opt_buffer_lvl) /
2480 (double)(high_water_mark - opt_buffer_lvl)); 2708 (double)(high_water_mark - opt_buffer_lvl));
2481 } 2709 }
2482 } 2710 }
2483 } 2711 }
2484 } 2712 }
2485 else 2713 else
2486 cpi->kf_group_bits = 0; 2714 cpi->twopass.kf_group_bits = 0;
2487 2715
2488 // Reset the first pass file position 2716 // Reset the first pass file position
2489 reset_fpf_position(cpi, start_position); 2717 reset_fpf_position(cpi, start_position);
2490 2718
2491 // determine how big to make this keyframe based on how well the subsequent frames use inter blocks 2719 // determine how big to make this keyframe based on how well the subsequent frames use inter blocks
2492 decay_accumulator = 1.0; 2720 decay_accumulator = 1.0;
2493 boost_score = 0.0; 2721 boost_score = 0.0;
2494 loop_decay_rate = 1.00; // Starting decay rate 2722 loop_decay_rate = 1.00; // Starting decay rate
2495 2723
2496 for (i = 0 ; i < cpi->frames_to_key ; i++) 2724 for (i = 0 ; i < cpi->twopass.frames_to_key ; i++)
2497 { 2725 {
2498 double r; 2726 double r;
2499 double motion_decay;
2500 double motion_pct;
2501 2727
2502 if (EOF == input_stats(cpi, &next_frame)) 2728 if (EOF == input_stats(cpi, &next_frame))
2503 break; 2729 break;
2504 2730
2505 if (next_frame.intra_error > cpi->kf_intra_err_min) 2731 if (next_frame.intra_error > cpi->twopass.kf_intra_err_min)
2506 r = (IIKFACTOR2 * next_frame.intra_error / 2732 r = (IIKFACTOR2 * next_frame.intra_error /
2507 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); 2733 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2508 else 2734 else
2509 r = (IIKFACTOR2 * cpi->kf_intra_err_min / 2735 r = (IIKFACTOR2 * cpi->twopass.kf_intra_err_min /
2510 DOUBLE_DIVIDE_CHECK(next_frame.coded_error)); 2736 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2511 2737
2512 if (r > RMAX) 2738 if (r > RMAX)
2513 r = RMAX; 2739 r = RMAX;
2514 2740
2515 // How fast is prediction quality decaying 2741 // How fast is prediction quality decaying
2516 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame); 2742 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
2517 2743
2518 decay_accumulator = decay_accumulator * loop_decay_rate; 2744 decay_accumulator = decay_accumulator * loop_decay_rate;
2519 decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator; 2745 decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
(...skipping 10 matching lines...) Expand all
2530 } 2756 }
2531 2757
2532 if (1) 2758 if (1)
2533 { 2759 {
2534 FIRSTPASS_STATS sectionstats; 2760 FIRSTPASS_STATS sectionstats;
2535 double Ratio; 2761 double Ratio;
2536 2762
2537 zero_stats(&sectionstats); 2763 zero_stats(&sectionstats);
2538 reset_fpf_position(cpi, start_position); 2764 reset_fpf_position(cpi, start_position);
2539 2765
2540 for (i = 0 ; i < cpi->frames_to_key ; i++) 2766 for (i = 0 ; i < cpi->twopass.frames_to_key ; i++)
2541 { 2767 {
2542 input_stats(cpi, &next_frame); 2768 input_stats(cpi, &next_frame);
2543 accumulate_stats(&sectionstats, &next_frame); 2769 accumulate_stats(&sectionstats, &next_frame);
2544 } 2770 }
2545 2771
2546 avg_stats(&sectionstats); 2772 avg_stats(&sectionstats);
2547 2773
2548 cpi->section_intra_rating = sectionstats.intra_error / DOUBLE_DIVIDE_CH ECK(sectionstats.coded_error); 2774 cpi->twopass.section_intra_rating =
2775 sectionstats.intra_error
2776 / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error);
2549 2777
2550 Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.code d_error); 2778 Ratio = sectionstats.intra_error / DOUBLE_DIVIDE_CHECK(sectionstats.code d_error);
2551 // if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) ) 2779 // if( (Ratio > 11) ) //&& (sectionstats.pcnt_second_ref < .20) )
2552 //{ 2780 //{
2553 cpi->section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025); 2781 cpi->twopass.section_max_qfactor = 1.0 - ((Ratio - 10.0) * 0.025);
2554 2782
2555 if (cpi->section_max_qfactor < 0.80) 2783 if (cpi->twopass.section_max_qfactor < 0.80)
2556 cpi->section_max_qfactor = 0.80; 2784 cpi->twopass.section_max_qfactor = 0.80;
2557 2785
2558 //} 2786 //}
2559 //else 2787 //else
2560 // cpi->section_max_qfactor = 1.0; 2788 // cpi->twopass.section_max_qfactor = 1.0;
2561 } 2789 }
2562 2790
2563 // When using CBR apply additional buffer fullness related upper limits 2791 // When using CBR apply additional buffer fullness related upper limits
2564 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) 2792 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2565 { 2793 {
2566 double max_boost; 2794 double max_boost;
2567 2795
2568 if (cpi->drop_frames_allowed) 2796 if (cpi->drop_frames_allowed)
2569 { 2797 {
2570 int df_buffer_level = cpi->oxcf.drop_frames_water_mark * (cpi->oxcf. optimal_buffer_level / 100); 2798 int df_buffer_level = cpi->oxcf.drop_frames_water_mark * (cpi->oxcf. optimal_buffer_level / 100);
(...skipping 17 matching lines...) Expand all
2588 } 2816 }
2589 2817
2590 // Reset the first pass file position 2818 // Reset the first pass file position
2591 reset_fpf_position(cpi, start_position); 2819 reset_fpf_position(cpi, start_position);
2592 2820
2593 // Work out how many bits to allocate for the key frame itself 2821 // Work out how many bits to allocate for the key frame itself
2594 if (1) 2822 if (1)
2595 { 2823 {
2596 int kf_boost = boost_score; 2824 int kf_boost = boost_score;
2597 int allocation_chunks; 2825 int allocation_chunks;
2598 int Counter = cpi->frames_to_key; 2826 int Counter = cpi->twopass.frames_to_key;
2599 int alt_kf_bits; 2827 int alt_kf_bits;
2600 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_i dx]; 2828 YV12_BUFFER_CONFIG *lst_yv12 = &cpi->common.yv12_fb[cpi->common.lst_fb_i dx];
2601 // Min boost based on kf interval 2829 // Min boost based on kf interval
2602 #if 0 2830 #if 0
2603 2831
2604 while ((kf_boost < 48) && (Counter > 0)) 2832 while ((kf_boost < 48) && (Counter > 0))
2605 { 2833 {
2606 Counter -= 2; 2834 Counter -= 2;
2607 kf_boost ++; 2835 kf_boost ++;
2608 } 2836 }
(...skipping 19 matching lines...) Expand all
2628 //kf_boost = kf_boost * vp8_kf_boost_qadjustment[cpi->ni_av_qi] / 100; 2856 //kf_boost = kf_boost * vp8_kf_boost_qadjustment[cpi->ni_av_qi] / 100;
2629 2857
2630 if (kf_boost < 250) // Min KF boost 2858 if (kf_boost < 250) // Min KF boost
2631 kf_boost = 250; 2859 kf_boost = 250;
2632 2860
2633 // We do three calculations for kf size. 2861 // We do three calculations for kf size.
2634 // The first is based on the error score for the whole kf group. 2862 // The first is based on the error score for the whole kf group.
2635 // The second (optionaly) on the key frames own error if this is smaller than the average for the group. 2863 // The second (optionaly) on the key frames own error if this is smaller than the average for the group.
2636 // The final one insures that the frame receives at least the allocation it would have received based on its own error score vs the error score remainin g 2864 // The final one insures that the frame receives at least the allocation it would have received based on its own error score vs the error score remainin g
2637 2865
2638 allocation_chunks = ((cpi->frames_to_key - 1) * 100) + kf_boost; // cpi->frames_to_key-1 because key frame itself is taken care of by kf_boost 2866 allocation_chunks = ((cpi->twopass.frames_to_key - 1) * 100) + kf_boost; // cpi->twopass.frames_to_key-1 because key frame itself is taken car e of by kf_boost
2639 2867
2640 // Normalize Altboost and allocations chunck down to prevent overflow 2868 // Normalize Altboost and allocations chunck down to prevent overflow
2641 while (kf_boost > 1000) 2869 while (kf_boost > 1000)
2642 { 2870 {
2643 kf_boost /= 2; 2871 kf_boost /= 2;
2644 allocation_chunks /= 2; 2872 allocation_chunks /= 2;
2645 } 2873 }
2646 2874
2647 cpi->kf_group_bits = (cpi->kf_group_bits < 0) ? 0 : cpi->kf_group_bits; 2875 cpi->twopass.kf_group_bits = (cpi->twopass.kf_group_bits < 0) ? 0 : cpi- >twopass.kf_group_bits;
2648 2876
2649 // Calculate the number of bits to be spent on the key frame 2877 // Calculate the number of bits to be spent on the key frame
2650 cpi->kf_bits = (int)((double)kf_boost * ((double)cpi->kf_group_bits / ( double)allocation_chunks)); 2878 cpi->twopass.kf_bits = (int)((double)kf_boost * ((double)cpi->twopass.k f_group_bits / (double)allocation_chunks));
2651 2879
2652 // Apply an additional limit for CBR 2880 // Apply an additional limit for CBR
2653 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) 2881 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2654 { 2882 {
2655 if (cpi->kf_bits > ((3 * cpi->buffer_level) >> 2)) 2883 if (cpi->twopass.kf_bits > ((3 * cpi->buffer_level) >> 2))
2656 cpi->kf_bits = (3 * cpi->buffer_level) >> 2; 2884 cpi->twopass.kf_bits = (3 * cpi->buffer_level) >> 2;
2657 } 2885 }
2658 2886
2659 // If the key frame is actually easier than the average for the 2887 // If the key frame is actually easier than the average for the
2660 // kf group (which does sometimes happen... eg a blank intro frame) 2888 // kf group (which does sometimes happen... eg a blank intro frame)
2661 // Then use an alternate calculation based on the kf error score 2889 // Then use an alternate calculation based on the kf error score
2662 // which should give a smaller key frame. 2890 // which should give a smaller key frame.
2663 if (kf_mod_err < kf_group_err / cpi->frames_to_key) 2891 if (kf_mod_err < kf_group_err / cpi->twopass.frames_to_key)
2664 { 2892 {
2665 double alt_kf_grp_bits = 2893 double alt_kf_grp_bits =
2666 ((double)cpi->bits_left * 2894 ((double)cpi->twopass.bits_left *
2667 (kf_mod_err * (double)cpi->frames_to_key) / 2895 (kf_mod_err * (double)cpi->twopass.frames_to_key) /
2668 DOUBLE_DIVIDE_CHECK(cpi->modified_error_left)); 2896 DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left));
2669 2897
2670 alt_kf_bits = (int)((double)kf_boost * 2898 alt_kf_bits = (int)((double)kf_boost *
2671 (alt_kf_grp_bits / (double)allocation_chunks)); 2899 (alt_kf_grp_bits / (double)allocation_chunks));
2672 2900
2673 if (cpi->kf_bits > alt_kf_bits) 2901 if (cpi->twopass.kf_bits > alt_kf_bits)
2674 { 2902 {
2675 cpi->kf_bits = alt_kf_bits; 2903 cpi->twopass.kf_bits = alt_kf_bits;
2676 } 2904 }
2677 } 2905 }
2678 // Else if it is much harder than other frames in the group make sure 2906 // Else if it is much harder than other frames in the group make sure
2679 // it at least receives an allocation in keeping with its relative 2907 // it at least receives an allocation in keeping with its relative
2680 // error score 2908 // error score
2681 else 2909 else
2682 { 2910 {
2683 alt_kf_bits = 2911 alt_kf_bits =
2684 (int)((double)cpi->bits_left * 2912 (int)((double)cpi->twopass.bits_left *
2685 (kf_mod_err / 2913 (kf_mod_err /
2686 DOUBLE_DIVIDE_CHECK(cpi->modified_error_left))); 2914 DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left)));
2687 2915
2688 if (alt_kf_bits > cpi->kf_bits) 2916 if (alt_kf_bits > cpi->twopass.kf_bits)
2689 { 2917 {
2690 cpi->kf_bits = alt_kf_bits; 2918 cpi->twopass.kf_bits = alt_kf_bits;
2691 } 2919 }
2692 } 2920 }
2693 2921
2694 cpi->kf_group_bits -= cpi->kf_bits; 2922 cpi->twopass.kf_group_bits -= cpi->twopass.kf_bits;
2695 cpi->kf_bits += cpi->min_frame_bandwidth; // Add in the minimum frame allowance 2923 cpi->twopass.kf_bits += cpi->min_frame_bandwidth; // Add in the minimum frame allowance
2696 2924
2697 cpi->per_frame_bandwidth = cpi->kf_bits; // Peer frame bit target for this frame 2925 cpi->per_frame_bandwidth = cpi->twopass.kf_bits; // Peer frame bit target for this frame
2698 cpi->target_bandwidth = cpi->kf_bits * cpi->output_frame_rate; // Convert to a per second bitrate 2926 cpi->target_bandwidth = cpi->twopass.kf_bits * cpi->output_frame_rate; // Convert to a per second bitrate
2699 } 2927 }
2700 2928
2701 // Note the total error score of the kf group minus the key frame itself 2929 // Note the total error score of the kf group minus the key frame itself
2702 cpi->kf_group_error_left = (int)(kf_group_err - kf_mod_err); 2930 cpi->twopass.kf_group_error_left = (int)(kf_group_err - kf_mod_err);
2703 2931
2704 // Adjust the count of total modified error left. 2932 // Adjust the count of total modified error left.
2705 // The count of bits left is adjusted elsewhere based on real coded frame si zes 2933 // The count of bits left is adjusted elsewhere based on real coded frame si zes
2706 cpi->modified_error_left -= kf_group_err; 2934 cpi->twopass.modified_error_left -= kf_group_err;
2707 2935
2708 if (cpi->oxcf.allow_spatial_resampling) 2936 if (cpi->oxcf.allow_spatial_resampling)
2709 { 2937 {
2710 int resample_trigger = FALSE; 2938 int resample_trigger = FALSE;
2711 int last_kf_resampled = FALSE; 2939 int last_kf_resampled = FALSE;
2712 int kf_q; 2940 int kf_q;
2713 int scale_val = 0; 2941 int scale_val = 0;
2714 int hr, hs, vr, vs; 2942 int hr, hs, vr, vs;
2715 int new_width = cpi->oxcf.Width; 2943 int new_width = cpi->oxcf.Width;
2716 int new_height = cpi->oxcf.Height; 2944 int new_height = cpi->oxcf.Height;
2717 2945
2718 int projected_buffer_level = cpi->buffer_level; 2946 int projected_buffer_level = cpi->buffer_level;
2719 int tmp_q; 2947 int tmp_q;
2720 2948
2721 double projected_bits_perframe; 2949 double projected_bits_perframe;
2722 double group_iiratio = (kf_group_intra_err - first_frame.intra_error) / (kf_group_coded_err - first_frame.coded_error); 2950 double group_iiratio = (kf_group_intra_err - first_frame.intra_error) / (kf_group_coded_err - first_frame.coded_error);
2723 double err_per_frame = kf_group_err / cpi->frames_to_key; 2951 double err_per_frame = kf_group_err / cpi->twopass.frames_to_key;
2724 double bits_per_frame; 2952 double bits_per_frame;
2725 double av_bits_per_frame; 2953 double av_bits_per_frame;
2726 double effective_size_ratio; 2954 double effective_size_ratio;
2727 2955
2728 if ((cpi->common.Width != cpi->oxcf.Width) || (cpi->common.Height != cpi ->oxcf.Height)) 2956 if ((cpi->common.Width != cpi->oxcf.Width) || (cpi->common.Height != cpi ->oxcf.Height))
2729 last_kf_resampled = TRUE; 2957 last_kf_resampled = TRUE;
2730 2958
2731 // Set back to unscaled by defaults 2959 // Set back to unscaled by defaults
2732 cpi->common.horiz_scale = NORMAL; 2960 cpi->common.horiz_scale = NORMAL;
2733 cpi->common.vert_scale = NORMAL; 2961 cpi->common.vert_scale = NORMAL;
2734 2962
2735 // Calculate Average bits per frame. 2963 // Calculate Average bits per frame.
2736 //av_bits_per_frame = cpi->bits_left/(double)(cpi->total_stats->count - cpi->common.current_video_frame); 2964 //av_bits_per_frame = cpi->twopass.bits_left/(double)(cpi->twopass.total _stats->count - cpi->common.current_video_frame);
2737 av_bits_per_frame = cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((do uble)cpi->oxcf.frame_rate); 2965 av_bits_per_frame = cpi->oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((do uble)cpi->oxcf.frame_rate);
2738 //if ( av_bits_per_frame < 0.0 ) 2966 //if ( av_bits_per_frame < 0.0 )
2739 // av_bits_per_frame = 0.0 2967 // av_bits_per_frame = 0.0
2740 2968
2741 // CBR... Use the clip average as the target for deciding resample 2969 // CBR... Use the clip average as the target for deciding resample
2742 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) 2970 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2743 { 2971 {
2744 bits_per_frame = av_bits_per_frame; 2972 bits_per_frame = av_bits_per_frame;
2745 } 2973 }
2746 2974
2747 // In VBR we want to avoid downsampling in easy section unless we are un der extreme pressure 2975 // In VBR we want to avoid downsampling in easy section unless we are un der extreme pressure
2748 // So use the larger of target bitrate for this sectoion or average bitr ate for sequence 2976 // So use the larger of target bitrate for this sectoion or average bitr ate for sequence
2749 else 2977 else
2750 { 2978 {
2751 bits_per_frame = cpi->kf_group_bits / cpi->frames_to_key; // Thi s accounts for how hard the section is... 2979 bits_per_frame = cpi->twopass.kf_group_bits / cpi->twopass.frames_to _key; // This accounts for how hard the section is...
2752 2980
2753 if (bits_per_frame < av_bits_per_frame) // Dont turn to resampling in easy sections just because they have been assigned a smal l number of bits 2981 if (bits_per_frame < av_bits_per_frame) // Dont turn to resampling in easy sections just because they have been assigned a smal l number of bits
2754 bits_per_frame = av_bits_per_frame; 2982 bits_per_frame = av_bits_per_frame;
2755 } 2983 }
2756 2984
2757 // bits_per_frame should comply with our minimum 2985 // bits_per_frame should comply with our minimum
2758 if (bits_per_frame < (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vb rmin_section / 100)) 2986 if (bits_per_frame < (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vb rmin_section / 100))
2759 bits_per_frame = (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vb rmin_section / 100); 2987 bits_per_frame = (cpi->oxcf.target_bandwidth * cpi->oxcf.two_pass_vb rmin_section / 100);
2760 2988
2761 // Work out if spatial resampling is necessary 2989 // Work out if spatial resampling is necessary
2762 kf_q = estimate_kf_group_q(cpi, err_per_frame, bits_per_frame, group_iir atio); 2990 kf_q = estimate_kf_group_q(cpi, err_per_frame, bits_per_frame, group_iir atio);
2763 2991
2764 // If we project a required Q higher than the maximum allowed Q then mak e a guess at the actual size of frames in this section 2992 // If we project a required Q higher than the maximum allowed Q then mak e a guess at the actual size of frames in this section
2765 projected_bits_perframe = bits_per_frame; 2993 projected_bits_perframe = bits_per_frame;
2766 tmp_q = kf_q; 2994 tmp_q = kf_q;
2767 2995
2768 while (tmp_q > cpi->worst_quality) 2996 while (tmp_q > cpi->worst_quality)
2769 { 2997 {
2770 projected_bits_perframe *= 1.04; 2998 projected_bits_perframe *= 1.04;
2771 tmp_q--; 2999 tmp_q--;
2772 } 3000 }
2773 3001
2774 // Guess at buffer level at the end of the section 3002 // Guess at buffer level at the end of the section
2775 projected_buffer_level = cpi->buffer_level - (int)((projected_bits_perfr ame - av_bits_per_frame) * cpi->frames_to_key); 3003 projected_buffer_level = cpi->buffer_level - (int)((projected_bits_perfr ame - av_bits_per_frame) * cpi->twopass.frames_to_key);
2776 3004
2777 if (0) 3005 if (0)
2778 { 3006 {
2779 FILE *f = fopen("Subsamle.stt", "a"); 3007 FILE *f = fopen("Subsamle.stt", "a");
2780 fprintf(f, " %8d %8d %8d %8d %12.0f %8d %8d %8d\n", cpi->common.cur rent_video_frame, kf_q, cpi->common.horiz_scale, cpi->common.vert_scale, kf_gro up_err / cpi->frames_to_key, (int)(cpi->kf_group_bits / cpi->frames_to_key), new _height, new_width); 3008 fprintf(f, " %8d %8d %8d %8d %12.0f %8d %8d %8d\n", cpi->common.cur rent_video_frame, kf_q, cpi->common.horiz_scale, cpi->common.vert_scale, kf_gro up_err / cpi->twopass.frames_to_key, (int)(cpi->twopass.kf_group_bits / cpi->two pass.frames_to_key), new_height, new_width);
2781 fclose(f); 3009 fclose(f);
2782 } 3010 }
2783 3011
2784 // The trigger for spatial resampling depends on the various parameters such as whether we are streaming (CBR) or VBR. 3012 // The trigger for spatial resampling depends on the various parameters such as whether we are streaming (CBR) or VBR.
2785 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) 3013 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
2786 { 3014 {
2787 // Trigger resample if we are projected to fall below down sample le vel or 3015 // Trigger resample if we are projected to fall below down sample le vel or
2788 // resampled last time and are projected to remain below the up samp le level 3016 // resampled last time and are projected to remain below the up samp le level
2789 if ((projected_buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100)) || 3017 if ((projected_buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100)) ||
2790 (last_kf_resampled && (projected_buffer_level < (cpi->oxcf.resam ple_up_water_mark * cpi->oxcf.optimal_buffer_level / 100)))) 3018 (last_kf_resampled && (projected_buffer_level < (cpi->oxcf.resam ple_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))))
2791 //( ((cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))) && 3019 //( ((cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))) &&
2792 // ((projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))) )) 3020 // ((projected_buffer_level < (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))) ))
2793 resample_trigger = TRUE; 3021 resample_trigger = TRUE;
2794 else 3022 else
2795 resample_trigger = FALSE; 3023 resample_trigger = FALSE;
2796 } 3024 }
2797 else 3025 else
2798 { 3026 {
2799 long long clip_bits = (long long)(cpi->total_stats->count * cpi->oxc f.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate)); 3027 int64_t clip_bits = (int64_t)(cpi->twopass.total_stats->count * cpi- >oxcf.target_bandwidth / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.frame_rate));
2800 long long over_spend = cpi->oxcf.starting_buffer_level - cpi->buffer _level; 3028 int64_t over_spend = cpi->oxcf.starting_buffer_level - cpi->buffer_l evel;
2801 long long over_spend2 = cpi->oxcf.starting_buffer_level - projected_ buffer_level;
2802 3029
2803 if ((last_kf_resampled && (kf_q > cpi->worst_quality)) || // If triggered last time the threshold for triggering again is reduced 3030 if ((last_kf_resampled && (kf_q > cpi->worst_quality)) || // If triggered last time the threshold for triggering again is reduced
2804 ((kf_q > cpi->worst_quality) && // Projected Q higher than allowed and ... 3031 ((kf_q > cpi->worst_quality) && // Projected Q higher than allowed and ...
2805 (over_spend > clip_bits / 20))) // ... Overspend > 5% of total bits 3032 (over_spend > clip_bits / 20))) // ... Overspend > 5% of total bits
2806 resample_trigger = TRUE; 3033 resample_trigger = TRUE;
2807 else 3034 else
2808 resample_trigger = FALSE; 3035 resample_trigger = FALSE;
2809 3036
2810 } 3037 }
2811 3038
(...skipping 16 matching lines...) Expand all
2828 // effective_sizeratio attempts to provide a crude correction fo r this 3055 // effective_sizeratio attempts to provide a crude correction fo r this
2829 effective_size_ratio = (double)(new_width * new_height) / (doubl e)(cpi->oxcf.Width * cpi->oxcf.Height); 3056 effective_size_ratio = (double)(new_width * new_height) / (doubl e)(cpi->oxcf.Width * cpi->oxcf.Height);
2830 effective_size_ratio = (1.0 + (3.0 * effective_size_ratio)) / 4. 0; 3057 effective_size_ratio = (1.0 + (3.0 * effective_size_ratio)) / 4. 0;
2831 3058
2832 // Now try again and see what Q we get with the smaller image si ze 3059 // Now try again and see what Q we get with the smaller image si ze
2833 kf_q = estimate_kf_group_q(cpi, err_per_frame * effective_size_r atio, bits_per_frame, group_iiratio); 3060 kf_q = estimate_kf_group_q(cpi, err_per_frame * effective_size_r atio, bits_per_frame, group_iiratio);
2834 3061
2835 if (0) 3062 if (0)
2836 { 3063 {
2837 FILE *f = fopen("Subsamle.stt", "a"); 3064 FILE *f = fopen("Subsamle.stt", "a");
2838 fprintf(f, "******** %8d %8d %8d %12.0f %8d %8d %8d\n", kf_ q, cpi->common.horiz_scale, cpi->common.vert_scale, kf_group_err / cpi->frames_ to_key, (int)(cpi->kf_group_bits / cpi->frames_to_key), new_height, new_width); 3065 fprintf(f, "******** %8d %8d %8d %12.0f %8d %8d %8d\n", kf_ q, cpi->common.horiz_scale, cpi->common.vert_scale, kf_group_err / cpi->twopass .frames_to_key, (int)(cpi->twopass.kf_group_bits / cpi->twopass.frames_to_key), new_height, new_width);
2839 fclose(f); 3066 fclose(f);
2840 } 3067 }
2841 } 3068 }
2842 } 3069 }
2843 3070
2844 if ((cpi->common.Width != new_width) || (cpi->common.Height != new_heigh t)) 3071 if ((cpi->common.Width != new_width) || (cpi->common.Height != new_heigh t))
2845 { 3072 {
2846 cpi->common.Width = new_width; 3073 cpi->common.Width = new_width;
2847 cpi->common.Height = new_height; 3074 cpi->common.Height = new_height;
2848 vp8_alloc_compressor_data(cpi); 3075 vp8_alloc_compressor_data(cpi);
2849 } 3076 }
2850 } 3077 }
2851 } 3078 }
OLDNEW
« no previous file with comments | « source/libvpx/vp8/encoder/ethreading.c ('k') | source/libvpx/vp8/encoder/generic/csystemdependent.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698