Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / vp9 / encoder / vp9_onyx_if.c
1 /*
2  * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
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
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include <math.h>
12 #include <stdio.h>
13 #include <limits.h>
14
15 #include "./vpx_config.h"
16 #include "./vpx_scale_rtcd.h"
17
18 #include "vp9/common/vp9_alloccommon.h"
19 #include "vp9/common/vp9_filter.h"
20 #include "vp9/common/vp9_idct.h"
21 #if CONFIG_VP9_POSTPROC
22 #include "vp9/common/vp9_postproc.h"
23 #endif
24 #include "vp9/common/vp9_reconinter.h"
25 #include "vp9/common/vp9_systemdependent.h"
26 #include "vp9/common/vp9_tile_common.h"
27
28 #include "vp9/encoder/vp9_encodemv.h"
29 #include "vp9/encoder/vp9_firstpass.h"
30 #include "vp9/encoder/vp9_mbgraph.h"
31 #include "vp9/encoder/vp9_onyx_int.h"
32 #include "vp9/encoder/vp9_picklpf.h"
33 #include "vp9/encoder/vp9_psnr.h"
34 #include "vp9/encoder/vp9_ratectrl.h"
35 #include "vp9/encoder/vp9_rdopt.h"
36 #include "vp9/encoder/vp9_segmentation.h"
37 #include "vp9/encoder/vp9_temporal_filter.h"
38 #include "vp9/encoder/vp9_vaq.h"
39 #include "vp9/encoder/vp9_resize.h"
40
41 #include "vpx_ports/vpx_timer.h"
42
43 void vp9_entropy_mode_init();
44 void vp9_coef_tree_initialize();
45
46 #define DEFAULT_INTERP_FILTER SWITCHABLE
47
48 #define SHARP_FILTER_QTHRESH 0          /* Q threshold for 8-tap sharp filter */
49
50 #define ALTREF_HIGH_PRECISION_MV 1      // Whether to use high precision mv
51                                          //  for altref computation.
52 #define HIGH_PRECISION_MV_QTHRESH 200   // Q threshold for high precision
53                                          // mv. Choose a very high value for
54                                          // now so that HIGH_PRECISION is always
55                                          // chosen.
56
57 // Masks for partially or completely disabling split mode
58 #define DISABLE_ALL_SPLIT         0x3F
59 #define DISABLE_ALL_INTER_SPLIT   0x1F
60 #define DISABLE_COMPOUND_SPLIT    0x18
61 #define LAST_AND_INTRA_SPLIT_ONLY 0x1E
62
63 // Max rate target for 1080P and below encodes under normal circumstances
64 // (1920 * 1080 / (16 * 16)) * MAX_MB_RATE bits per MB
65 #define MAX_MB_RATE 250
66 #define MAXRATE_1080P 2025000
67
68 #if CONFIG_INTERNAL_STATS
69 extern double vp9_calc_ssim(YV12_BUFFER_CONFIG *source,
70                             YV12_BUFFER_CONFIG *dest, int lumamask,
71                             double *weight);
72
73
74 extern double vp9_calc_ssimg(YV12_BUFFER_CONFIG *source,
75                              YV12_BUFFER_CONFIG *dest, double *ssim_y,
76                              double *ssim_u, double *ssim_v);
77
78
79 #endif
80
81 // #define OUTPUT_YUV_REC
82
83 #ifdef OUTPUT_YUV_SRC
84 FILE *yuv_file;
85 #endif
86 #ifdef OUTPUT_YUV_REC
87 FILE *yuv_rec_file;
88 #endif
89
90 #if 0
91 FILE *framepsnr;
92 FILE *kf_list;
93 FILE *keyfile;
94 #endif
95
96 void vp9_init_quantizer(VP9_COMP *cpi);
97
98 static const double in_frame_q_adj_ratio[MAX_SEGMENTS] =
99   {1.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
100
101 static INLINE void Scale2Ratio(int mode, int *hr, int *hs) {
102   switch (mode) {
103     case NORMAL:
104       *hr = 1;
105       *hs = 1;
106       break;
107     case FOURFIVE:
108       *hr = 4;
109       *hs = 5;
110       break;
111     case THREEFIVE:
112       *hr = 3;
113       *hs = 5;
114     break;
115     case ONETWO:
116       *hr = 1;
117       *hs = 2;
118     break;
119     default:
120       *hr = 1;
121       *hs = 1;
122        assert(0);
123       break;
124   }
125 }
126
127 static void set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
128   MACROBLOCK *const mb = &cpi->mb;
129   cpi->common.allow_high_precision_mv = allow_high_precision_mv;
130   if (cpi->common.allow_high_precision_mv) {
131     mb->mvcost = mb->nmvcost_hp;
132     mb->mvsadcost = mb->nmvsadcost_hp;
133   } else {
134     mb->mvcost = mb->nmvcost;
135     mb->mvsadcost = mb->nmvsadcost;
136   }
137 }
138
139 void vp9_initialize_enc() {
140   static int init_done = 0;
141
142   if (!init_done) {
143     vp9_initialize_common();
144     vp9_coef_tree_initialize();
145     vp9_tokenize_initialize();
146     vp9_init_quant_tables();
147     vp9_init_me_luts();
148     vp9_rc_init_minq_luts();
149     // init_base_skip_probs();
150     vp9_entropy_mv_init();
151     vp9_entropy_mode_init();
152     init_done = 1;
153   }
154 }
155
156 static void dealloc_compressor_data(VP9_COMP *cpi) {
157   // Delete sementation map
158   vpx_free(cpi->segmentation_map);
159   cpi->segmentation_map = 0;
160   vpx_free(cpi->common.last_frame_seg_map);
161   cpi->common.last_frame_seg_map = 0;
162   vpx_free(cpi->coding_context.last_frame_seg_map_copy);
163   cpi->coding_context.last_frame_seg_map_copy = 0;
164
165   vpx_free(cpi->complexity_map);
166   cpi->complexity_map = 0;
167   vpx_free(cpi->active_map);
168   cpi->active_map = 0;
169
170   vp9_free_frame_buffers(&cpi->common);
171
172   vp9_free_frame_buffer(&cpi->last_frame_uf);
173   vp9_free_frame_buffer(&cpi->scaled_source);
174   vp9_free_frame_buffer(&cpi->alt_ref_buffer);
175   vp9_lookahead_destroy(cpi->lookahead);
176
177   vpx_free(cpi->tok);
178   cpi->tok = 0;
179
180   // Activity mask based per mb zbin adjustments
181   vpx_free(cpi->mb_activity_map);
182   cpi->mb_activity_map = 0;
183   vpx_free(cpi->mb_norm_activity_map);
184   cpi->mb_norm_activity_map = 0;
185
186   vpx_free(cpi->above_context[0]);
187   cpi->above_context[0] = NULL;
188
189   vpx_free(cpi->above_seg_context);
190   cpi->above_seg_context = NULL;
191 }
192
193 // Computes a q delta (in "q index" terms) to get from a starting q value
194 // to a target value
195 // target q value
196 int vp9_compute_qdelta(const VP9_COMP *cpi, double qstart, double qtarget) {
197   int i;
198   int start_index = cpi->rc.worst_quality;
199   int target_index = cpi->rc.worst_quality;
200
201   // Convert the average q value to an index.
202   for (i = cpi->rc.best_quality; i < cpi->rc.worst_quality; i++) {
203     start_index = i;
204     if (vp9_convert_qindex_to_q(i) >= qstart)
205       break;
206   }
207
208   // Convert the q target to an index
209   for (i = cpi->rc.best_quality; i < cpi->rc.worst_quality; i++) {
210     target_index = i;
211     if (vp9_convert_qindex_to_q(i) >= qtarget)
212       break;
213   }
214
215   return target_index - start_index;
216 }
217
218 // Computes a q delta (in "q index" terms) to get from a starting q value
219 // to a value that should equate to thegiven rate ratio.
220
221 int vp9_compute_qdelta_by_rate(VP9_COMP *cpi,
222                                double base_q_index, double rate_target_ratio) {
223   int i;
224   int base_bits_per_mb;
225   int target_bits_per_mb;
226   int target_index = cpi->rc.worst_quality;
227
228   // Make SURE use of floating point in this function is safe.
229   vp9_clear_system_state();
230
231   // Look up the current projected bits per block for the base index
232   base_bits_per_mb = vp9_rc_bits_per_mb(cpi->common.frame_type,
233                                         base_q_index, 1.0);
234
235   // Find the target bits per mb based on the base value and given ratio.
236   target_bits_per_mb = rate_target_ratio * base_bits_per_mb;
237
238   // Convert the q target to an index
239   for (i = cpi->rc.best_quality; i < cpi->rc.worst_quality; i++) {
240     target_index = i;
241     if (vp9_rc_bits_per_mb(cpi->common.frame_type,
242                            i, 1.0) <= target_bits_per_mb )
243       break;
244   }
245
246   return target_index - base_q_index;
247 }
248
249 // This function sets up a set of segments with delta Q values around
250 // the baseline frame quantizer.
251 static void setup_in_frame_q_adj(VP9_COMP *cpi) {
252   VP9_COMMON *cm = &cpi->common;
253   struct segmentation *seg = &cm->seg;
254   // double q_ratio;
255   int segment;
256   int qindex_delta;
257
258   // Make SURE use of floating point in this function is safe.
259   vp9_clear_system_state();
260
261   if (cm->frame_type == KEY_FRAME ||
262       cpi->refresh_alt_ref_frame ||
263       (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref)) {
264     // Clear down the segment map
265     vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
266
267     // Clear down the complexity map used for rd
268     vpx_memset(cpi->complexity_map, 0, cm->mi_rows * cm->mi_cols);
269
270     vp9_enable_segmentation((VP9_PTR)cpi);
271     vp9_clearall_segfeatures(seg);
272
273     // Select delta coding method
274     seg->abs_delta = SEGMENT_DELTADATA;
275
276     // Segment 0 "Q" feature is disabled so it defaults to the baseline Q
277     vp9_disable_segfeature(seg, 0, SEG_LVL_ALT_Q);
278
279     // Use some of the segments for in frame Q adjustment
280     for (segment = 1; segment < 2; segment++) {
281       qindex_delta =
282         vp9_compute_qdelta_by_rate(cpi, cm->base_qindex,
283                                    in_frame_q_adj_ratio[segment]);
284       vp9_enable_segfeature(seg, segment, SEG_LVL_ALT_Q);
285       vp9_set_segdata(seg, segment, SEG_LVL_ALT_Q, qindex_delta);
286     }
287   }
288 }
289 static void configure_static_seg_features(VP9_COMP *cpi) {
290   VP9_COMMON *cm = &cpi->common;
291   struct segmentation *seg = &cm->seg;
292
293   int high_q = (int)(cpi->rc.avg_q > 48.0);
294   int qi_delta;
295
296   // Disable and clear down for KF
297   if (cm->frame_type == KEY_FRAME) {
298     // Clear down the global segmentation map
299     vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
300     seg->update_map = 0;
301     seg->update_data = 0;
302     cpi->static_mb_pct = 0;
303
304     // Disable segmentation
305     vp9_disable_segmentation((VP9_PTR)cpi);
306
307     // Clear down the segment features.
308     vp9_clearall_segfeatures(seg);
309   } else if (cpi->refresh_alt_ref_frame) {
310     // If this is an alt ref frame
311     // Clear down the global segmentation map
312     vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
313     seg->update_map = 0;
314     seg->update_data = 0;
315     cpi->static_mb_pct = 0;
316
317     // Disable segmentation and individual segment features by default
318     vp9_disable_segmentation((VP9_PTR)cpi);
319     vp9_clearall_segfeatures(seg);
320
321     // Scan frames from current to arf frame.
322     // This function re-enables segmentation if appropriate.
323     vp9_update_mbgraph_stats(cpi);
324
325     // If segmentation was enabled set those features needed for the
326     // arf itself.
327     if (seg->enabled) {
328       seg->update_map = 1;
329       seg->update_data = 1;
330
331       qi_delta = vp9_compute_qdelta(
332           cpi, cpi->rc.avg_q, (cpi->rc.avg_q * 0.875));
333       vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, (qi_delta - 2));
334       vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
335
336       vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
337       vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
338
339       // Where relevant assume segment data is delta data
340       seg->abs_delta = SEGMENT_DELTADATA;
341     }
342   } else if (seg->enabled) {
343     // All other frames if segmentation has been enabled
344
345     // First normal frame in a valid gf or alt ref group
346     if (cpi->rc.frames_since_golden == 0) {
347       // Set up segment features for normal frames in an arf group
348       if (cpi->rc.source_alt_ref_active) {
349         seg->update_map = 0;
350         seg->update_data = 1;
351         seg->abs_delta = SEGMENT_DELTADATA;
352
353         qi_delta = vp9_compute_qdelta(cpi, cpi->rc.avg_q,
354                                       (cpi->rc.avg_q * 1.125));
355         vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, (qi_delta + 2));
356         vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
357
358         vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
359         vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
360
361         // Segment coding disabled for compred testing
362         if (high_q || (cpi->static_mb_pct == 100)) {
363           vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
364           vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
365           vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
366         }
367       } else {
368         // Disable segmentation and clear down features if alt ref
369         // is not active for this group
370
371         vp9_disable_segmentation((VP9_PTR)cpi);
372
373         vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
374
375         seg->update_map = 0;
376         seg->update_data = 0;
377
378         vp9_clearall_segfeatures(seg);
379       }
380     } else if (cpi->rc.is_src_frame_alt_ref) {
381       // Special case where we are coding over the top of a previous
382       // alt ref frame.
383       // Segment coding disabled for compred testing
384
385       // Enable ref frame features for segment 0 as well
386       vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
387       vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
388
389       // All mbs should use ALTREF_FRAME
390       vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
391       vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
392       vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
393       vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
394
395       // Skip all MBs if high Q (0,0 mv and skip coeffs)
396       if (high_q) {
397         vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
398         vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
399       }
400       // Enable data update
401       seg->update_data = 1;
402     } else {
403       // All other frames.
404
405       // No updates.. leave things as they are.
406       seg->update_map = 0;
407       seg->update_data = 0;
408     }
409   }
410 }
411
412 // DEBUG: Print out the segment id of each MB in the current frame.
413 static void print_seg_map(VP9_COMP *cpi) {
414   VP9_COMMON *cm = &cpi->common;
415   int row, col;
416   int map_index = 0;
417   FILE *statsfile = fopen("segmap.stt", "a");
418
419   fprintf(statsfile, "%10d\n", cm->current_video_frame);
420
421   for (row = 0; row < cpi->common.mi_rows; row++) {
422     for (col = 0; col < cpi->common.mi_cols; col++) {
423       fprintf(statsfile, "%10d", cpi->segmentation_map[map_index]);
424       map_index++;
425     }
426     fprintf(statsfile, "\n");
427   }
428   fprintf(statsfile, "\n");
429
430   fclose(statsfile);
431 }
432
433 static void update_reference_segmentation_map(VP9_COMP *cpi) {
434   VP9_COMMON *const cm = &cpi->common;
435   int row, col;
436   MODE_INFO **mi_8x8, **mi_8x8_ptr = cm->mi_grid_visible;
437   uint8_t *cache_ptr = cm->last_frame_seg_map, *cache;
438
439   for (row = 0; row < cm->mi_rows; row++) {
440     mi_8x8 = mi_8x8_ptr;
441     cache = cache_ptr;
442     for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
443       cache[0] = mi_8x8[0]->mbmi.segment_id;
444     mi_8x8_ptr += cm->mode_info_stride;
445     cache_ptr += cm->mi_cols;
446   }
447 }
448 static int is_slowest_mode(int mode) {
449   return (mode == MODE_SECONDPASS_BEST || mode == MODE_BESTQUALITY);
450 }
451
452 static void set_rd_speed_thresholds(VP9_COMP *cpi) {
453   SPEED_FEATURES *sf = &cpi->sf;
454   int i;
455
456   // Set baseline threshold values
457   for (i = 0; i < MAX_MODES; ++i)
458     sf->thresh_mult[i] = is_slowest_mode(cpi->oxcf.mode) ? -500 : 0;
459
460   sf->thresh_mult[THR_NEARESTMV] = 0;
461   sf->thresh_mult[THR_NEARESTG] = 0;
462   sf->thresh_mult[THR_NEARESTA] = 0;
463
464   sf->thresh_mult[THR_DC] += 1000;
465
466   sf->thresh_mult[THR_NEWMV] += 1000;
467   sf->thresh_mult[THR_NEWA] += 1000;
468   sf->thresh_mult[THR_NEWG] += 1000;
469
470   sf->thresh_mult[THR_NEARMV] += 1000;
471   sf->thresh_mult[THR_NEARA] += 1000;
472   sf->thresh_mult[THR_COMP_NEARESTLA] += 1000;
473   sf->thresh_mult[THR_COMP_NEARESTGA] += 1000;
474
475   sf->thresh_mult[THR_TM] += 1000;
476
477   sf->thresh_mult[THR_COMP_NEARLA] += 1500;
478   sf->thresh_mult[THR_COMP_NEWLA] += 2000;
479   sf->thresh_mult[THR_NEARG] += 1000;
480   sf->thresh_mult[THR_COMP_NEARGA] += 1500;
481   sf->thresh_mult[THR_COMP_NEWGA] += 2000;
482
483   sf->thresh_mult[THR_ZEROMV] += 2000;
484   sf->thresh_mult[THR_ZEROG] += 2000;
485   sf->thresh_mult[THR_ZEROA] += 2000;
486   sf->thresh_mult[THR_COMP_ZEROLA] += 2500;
487   sf->thresh_mult[THR_COMP_ZEROGA] += 2500;
488
489   sf->thresh_mult[THR_H_PRED] += 2000;
490   sf->thresh_mult[THR_V_PRED] += 2000;
491   sf->thresh_mult[THR_D45_PRED ] += 2500;
492   sf->thresh_mult[THR_D135_PRED] += 2500;
493   sf->thresh_mult[THR_D117_PRED] += 2500;
494   sf->thresh_mult[THR_D153_PRED] += 2500;
495   sf->thresh_mult[THR_D207_PRED] += 2500;
496   sf->thresh_mult[THR_D63_PRED] += 2500;
497
498   /* disable frame modes if flags not set */
499   if (!(cpi->ref_frame_flags & VP9_LAST_FLAG)) {
500     sf->thresh_mult[THR_NEWMV    ] = INT_MAX;
501     sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
502     sf->thresh_mult[THR_ZEROMV   ] = INT_MAX;
503     sf->thresh_mult[THR_NEARMV   ] = INT_MAX;
504   }
505   if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG)) {
506     sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
507     sf->thresh_mult[THR_ZEROG    ] = INT_MAX;
508     sf->thresh_mult[THR_NEARG    ] = INT_MAX;
509     sf->thresh_mult[THR_NEWG     ] = INT_MAX;
510   }
511   if (!(cpi->ref_frame_flags & VP9_ALT_FLAG)) {
512     sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
513     sf->thresh_mult[THR_ZEROA    ] = INT_MAX;
514     sf->thresh_mult[THR_NEARA    ] = INT_MAX;
515     sf->thresh_mult[THR_NEWA     ] = INT_MAX;
516   }
517
518   if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
519       (VP9_LAST_FLAG | VP9_ALT_FLAG)) {
520     sf->thresh_mult[THR_COMP_ZEROLA   ] = INT_MAX;
521     sf->thresh_mult[THR_COMP_NEARESTLA] = INT_MAX;
522     sf->thresh_mult[THR_COMP_NEARLA   ] = INT_MAX;
523     sf->thresh_mult[THR_COMP_NEWLA    ] = INT_MAX;
524   }
525   if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
526       (VP9_GOLD_FLAG | VP9_ALT_FLAG)) {
527     sf->thresh_mult[THR_COMP_ZEROGA   ] = INT_MAX;
528     sf->thresh_mult[THR_COMP_NEARESTGA] = INT_MAX;
529     sf->thresh_mult[THR_COMP_NEARGA   ] = INT_MAX;
530     sf->thresh_mult[THR_COMP_NEWGA    ] = INT_MAX;
531   }
532 }
533
534 static void set_rd_speed_thresholds_sub8x8(VP9_COMP *cpi) {
535   SPEED_FEATURES *sf = &cpi->sf;
536   int i;
537
538   for (i = 0; i < MAX_REFS; ++i)
539     sf->thresh_mult_sub8x8[i] = is_slowest_mode(cpi->oxcf.mode)  ? -500 : 0;
540
541   sf->thresh_mult_sub8x8[THR_LAST] += 2500;
542   sf->thresh_mult_sub8x8[THR_GOLD] += 2500;
543   sf->thresh_mult_sub8x8[THR_ALTR] += 2500;
544   sf->thresh_mult_sub8x8[THR_INTRA] += 2500;
545   sf->thresh_mult_sub8x8[THR_COMP_LA] += 4500;
546   sf->thresh_mult_sub8x8[THR_COMP_GA] += 4500;
547
548   // Check for masked out split cases.
549   for (i = 0; i < MAX_REFS; i++) {
550     if (sf->disable_split_mask & (1 << i))
551       sf->thresh_mult_sub8x8[i] = INT_MAX;
552   }
553
554   // disable mode test if frame flag is not set
555   if (!(cpi->ref_frame_flags & VP9_LAST_FLAG))
556     sf->thresh_mult_sub8x8[THR_LAST] = INT_MAX;
557   if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG))
558     sf->thresh_mult_sub8x8[THR_GOLD] = INT_MAX;
559   if (!(cpi->ref_frame_flags & VP9_ALT_FLAG))
560     sf->thresh_mult_sub8x8[THR_ALTR] = INT_MAX;
561   if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
562       (VP9_LAST_FLAG | VP9_ALT_FLAG))
563     sf->thresh_mult_sub8x8[THR_COMP_LA] = INT_MAX;
564   if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
565       (VP9_GOLD_FLAG | VP9_ALT_FLAG))
566     sf->thresh_mult_sub8x8[THR_COMP_GA] = INT_MAX;
567 }
568
569 static void set_good_speed_feature(VP9_COMMON *cm,
570                                    SPEED_FEATURES *sf,
571                                    int speed) {
572   int i;
573   sf->adaptive_rd_thresh = 1;
574   sf->recode_loop = ((speed < 1) ? ALLOW_RECODE : ALLOW_RECODE_KFMAXBW);
575   if (speed == 1) {
576     sf->use_square_partition_only = !frame_is_intra_only(cm);
577     sf->less_rectangular_check  = 1;
578     sf->tx_size_search_method = frame_is_intra_only(cm)
579       ? USE_FULL_RD : USE_LARGESTALL;
580
581     if (MIN(cm->width, cm->height) >= 720)
582       sf->disable_split_mask = cm->show_frame ?
583         DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
584     else
585       sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
586
587     sf->use_rd_breakout = 1;
588     sf->adaptive_motion_search = 1;
589     sf->adaptive_pred_interp_filter = 1;
590     sf->auto_mv_step_size = 1;
591     sf->adaptive_rd_thresh = 2;
592     sf->recode_loop = ALLOW_RECODE_KFARFGF;
593     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
594     sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
595     sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
596   }
597   if (speed == 2) {
598     sf->use_square_partition_only = !frame_is_intra_only(cm);
599     sf->less_rectangular_check  = 1;
600     sf->tx_size_search_method = frame_is_intra_only(cm)
601       ? USE_FULL_RD : USE_LARGESTALL;
602
603     if (MIN(cm->width, cm->height) >= 720)
604       sf->disable_split_mask = cm->show_frame ?
605         DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
606     else
607       sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
608
609     sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
610                                  FLAG_SKIP_INTRA_BESTINTER |
611                                  FLAG_SKIP_COMP_BESTINTRA |
612                                  FLAG_SKIP_INTRA_LOWVAR;
613     sf->use_rd_breakout = 1;
614     sf->adaptive_motion_search = 1;
615     sf->adaptive_pred_interp_filter = 2;
616     sf->reference_masking = 1;
617     sf->auto_mv_step_size = 1;
618
619     sf->disable_filter_search_var_thresh = 50;
620     sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
621
622     sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
623     sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_LOW_MOTION;
624     sf->adjust_partitioning_from_last_frame = 1;
625     sf->last_partitioning_redo_frequency = 3;
626
627     sf->adaptive_rd_thresh = 2;
628     sf->recode_loop = ALLOW_RECODE_KFARFGF;
629     sf->use_lp32x32fdct = 1;
630     sf->mode_skip_start = 11;
631     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
632     sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
633     sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
634     sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
635   }
636   if (speed == 3) {
637     sf->use_square_partition_only = 1;
638     sf->tx_size_search_method = USE_LARGESTALL;
639
640     if (MIN(cm->width, cm->height) >= 720)
641       sf->disable_split_mask = DISABLE_ALL_SPLIT;
642     else
643       sf->disable_split_mask = DISABLE_ALL_INTER_SPLIT;
644
645     sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
646       FLAG_SKIP_INTRA_BESTINTER |
647       FLAG_SKIP_COMP_BESTINTRA |
648       FLAG_SKIP_INTRA_LOWVAR;
649
650     sf->use_rd_breakout = 1;
651     sf->adaptive_motion_search = 1;
652     sf->adaptive_pred_interp_filter = 2;
653     sf->reference_masking = 1;
654     sf->auto_mv_step_size = 1;
655
656     sf->disable_filter_search_var_thresh = 100;
657     sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
658
659     sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
660     sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL;
661     sf->adjust_partitioning_from_last_frame = 1;
662     sf->last_partitioning_redo_frequency = 3;
663
664     sf->use_uv_intra_rd_estimate = 1;
665     sf->skip_encode_sb = 1;
666     sf->use_lp32x32fdct = 1;
667     sf->subpel_iters_per_step = 1;
668     sf->use_fast_coef_updates = 2;
669
670     sf->adaptive_rd_thresh = 4;
671     sf->mode_skip_start = 6;
672   }
673   if (speed == 4) {
674     sf->use_square_partition_only = 1;
675     sf->tx_size_search_method = USE_LARGESTALL;
676     sf->disable_split_mask = DISABLE_ALL_SPLIT;
677
678     sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
679       FLAG_SKIP_INTRA_BESTINTER |
680       FLAG_SKIP_COMP_BESTINTRA |
681       FLAG_SKIP_COMP_REFMISMATCH |
682       FLAG_SKIP_INTRA_LOWVAR |
683       FLAG_EARLY_TERMINATE;
684
685     sf->use_rd_breakout = 1;
686     sf->adaptive_motion_search = 1;
687     sf->adaptive_pred_interp_filter = 2;
688     sf->reference_masking = 1;
689     sf->auto_mv_step_size = 1;
690
691     sf->disable_filter_search_var_thresh = 200;
692     sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
693
694     sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
695     sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL;
696     sf->adjust_partitioning_from_last_frame = 1;
697     sf->last_partitioning_redo_frequency = 3;
698
699     sf->use_uv_intra_rd_estimate = 1;
700     sf->skip_encode_sb = 1;
701     sf->use_lp32x32fdct = 1;
702     sf->subpel_iters_per_step = 1;
703     sf->use_fast_coef_updates = 2;
704
705     sf->adaptive_rd_thresh = 4;
706     sf->mode_skip_start = 6;
707   }
708   if (speed == 5) {
709     sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
710     sf->use_one_partition_size_always = 1;
711     sf->always_this_block_size = BLOCK_16X16;
712     sf->tx_size_search_method = frame_is_intra_only(cm) ?
713       USE_FULL_RD : USE_LARGESTALL;
714     sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
715                                  FLAG_SKIP_INTRA_BESTINTER |
716                                  FLAG_SKIP_COMP_BESTINTRA |
717                                  FLAG_SKIP_COMP_REFMISMATCH |
718                                  FLAG_SKIP_INTRA_LOWVAR |
719                                  FLAG_EARLY_TERMINATE;
720     sf->use_rd_breakout = 1;
721     sf->use_lp32x32fdct = 1;
722     sf->optimize_coefficients = 0;
723     sf->auto_mv_step_size = 1;
724     sf->reference_masking = 1;
725
726     sf->disable_split_mask = DISABLE_ALL_SPLIT;
727     sf->search_method = HEX;
728     sf->subpel_iters_per_step = 1;
729     sf->disable_split_var_thresh = 64;
730     sf->disable_filter_search_var_thresh = 500;
731     for (i = 0; i < TX_SIZES; i++) {
732       sf->intra_y_mode_mask[i] = INTRA_DC_ONLY;
733       sf->intra_uv_mode_mask[i] = INTRA_DC_ONLY;
734     }
735     sf->use_fast_coef_updates = 2;
736     sf->adaptive_rd_thresh = 4;
737     sf->mode_skip_start = 6;
738   }
739 }
740 static void set_rt_speed_feature(VP9_COMMON *cm,
741                                  SPEED_FEATURES *sf,
742                                  int speed) {
743   sf->static_segmentation = 0;
744   sf->adaptive_rd_thresh = 1;
745   sf->recode_loop = ((speed < 1) ? ALLOW_RECODE : ALLOW_RECODE_KFMAXBW);
746   sf->encode_breakout_thresh = 1;
747
748   if (speed == 1) {
749     sf->use_square_partition_only = !frame_is_intra_only(cm);
750     sf->less_rectangular_check = 1;
751     sf->tx_size_search_method =
752         frame_is_intra_only(cm) ? USE_FULL_RD : USE_LARGESTALL;
753
754     if (MIN(cm->width, cm->height) >= 720)
755       sf->disable_split_mask = cm->show_frame ?
756         DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
757     else
758       sf->disable_split_mask = DISABLE_COMPOUND_SPLIT;
759
760     sf->use_rd_breakout = 1;
761     sf->adaptive_motion_search = 1;
762     sf->adaptive_pred_interp_filter = 1;
763     sf->auto_mv_step_size = 1;
764     sf->adaptive_rd_thresh = 2;
765     sf->recode_loop = ALLOW_RECODE_KFARFGF;
766     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
767     sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
768     sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
769     sf->encode_breakout_thresh = 8;
770   }
771   if (speed >= 2) {
772     sf->use_square_partition_only = !frame_is_intra_only(cm);
773     sf->less_rectangular_check = 1;
774     sf->tx_size_search_method =
775         frame_is_intra_only(cm) ? USE_FULL_RD : USE_LARGESTALL;
776
777     if (MIN(cm->width, cm->height) >= 720)
778       sf->disable_split_mask = cm->show_frame ?
779         DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT;
780     else
781       sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY;
782
783     sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH
784         | FLAG_SKIP_INTRA_BESTINTER | FLAG_SKIP_COMP_BESTINTRA
785         | FLAG_SKIP_INTRA_LOWVAR;
786
787     sf->use_rd_breakout = 1;
788     sf->adaptive_motion_search = 1;
789     sf->adaptive_pred_interp_filter = 2;
790     sf->auto_mv_step_size = 1;
791     sf->reference_masking = 1;
792
793     sf->disable_filter_search_var_thresh = 50;
794     sf->comp_inter_joint_search_thresh = BLOCK_SIZES;
795
796     sf->auto_min_max_partition_size = RELAXED_NEIGHBORING_MIN_MAX;
797     sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_LOW_MOTION;
798     sf->adjust_partitioning_from_last_frame = 1;
799     sf->last_partitioning_redo_frequency = 3;
800
801     sf->adaptive_rd_thresh = 2;
802     sf->recode_loop = ALLOW_RECODE_KFARFGF;
803     sf->use_lp32x32fdct = 1;
804     sf->mode_skip_start = 11;
805     sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V;
806     sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V;
807     sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V;
808     sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V;
809     sf->encode_breakout_thresh = 200;
810   }
811   if (speed >= 3) {
812     sf->use_square_partition_only = 1;
813     sf->tx_size_search_method = USE_LARGESTALL;
814
815     if (MIN(cm->width, cm->height) >= 720)
816       sf->disable_split_mask = DISABLE_ALL_SPLIT;
817     else
818       sf->disable_split_mask = DISABLE_ALL_INTER_SPLIT;
819
820     sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH
821         | FLAG_SKIP_INTRA_BESTINTER | FLAG_SKIP_COMP_BESTINTRA
822         | FLAG_SKIP_INTRA_LOWVAR;
823
824     sf->disable_filter_search_var_thresh = 100;
825     sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL;
826     sf->use_uv_intra_rd_estimate = 1;
827     sf->skip_encode_sb = 1;
828     sf->subpel_iters_per_step = 1;
829     sf->use_fast_coef_updates = 2;
830     sf->adaptive_rd_thresh = 4;
831     sf->mode_skip_start = 6;
832     sf->encode_breakout_thresh = 400;
833   }
834   if (speed >= 4) {
835     sf->optimize_coefficients = 0;
836     sf->disable_split_mask = DISABLE_ALL_SPLIT;
837     sf->use_fast_lpf_pick = 2;
838     sf->encode_breakout_thresh = 700;
839   }
840   if (speed >= 5) {
841     int i;
842     sf->adaptive_rd_thresh = 5;
843     sf->auto_min_max_partition_size = frame_is_intra_only(cm) ?
844         RELAXED_NEIGHBORING_MIN_MAX : STRICT_NEIGHBORING_MIN_MAX;
845     sf->subpel_force_stop = 1;
846     for (i = 0; i < TX_SIZES; i++) {
847       sf->intra_y_mode_mask[i] = INTRA_DC_H_V;
848       sf->intra_uv_mode_mask[i] = INTRA_DC_ONLY;
849     }
850     sf->frame_parameter_update = 0;
851     sf->encode_breakout_thresh = 1000;
852   }
853   if (speed >= 6) {
854     sf->always_this_block_size = BLOCK_16X16;
855     sf->use_pick_mode = 1;
856     sf->encode_breakout_thresh = 1000;
857   }
858 }
859
860 void vp9_set_speed_features(VP9_COMP *cpi) {
861   SPEED_FEATURES *sf = &cpi->sf;
862   VP9_COMMON *cm = &cpi->common;
863   int speed = cpi->speed;
864   int i;
865
866   // Convert negative speed to positive
867   if (speed < 0)
868     speed = -speed;
869
870   for (i = 0; i < MAX_MODES; ++i)
871     cpi->mode_chosen_counts[i] = 0;
872
873   // best quality defaults
874   sf->frame_parameter_update = 1;
875   sf->search_method = NSTEP;
876   sf->recode_loop = ALLOW_RECODE;
877   sf->subpel_search_method = SUBPEL_TREE;
878   sf->subpel_iters_per_step = 2;
879   sf->subpel_force_stop = 0;
880   sf->optimize_coefficients = !cpi->oxcf.lossless;
881   sf->reduce_first_step_size = 0;
882   sf->auto_mv_step_size = 0;
883   sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
884   sf->comp_inter_joint_search_thresh = BLOCK_4X4;
885   sf->adaptive_rd_thresh = 0;
886   sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_OFF;
887   sf->tx_size_search_method = USE_FULL_RD;
888   sf->use_lp32x32fdct = 0;
889   sf->adaptive_motion_search = 0;
890   sf->adaptive_pred_interp_filter = 0;
891   sf->reference_masking = 0;
892   sf->use_one_partition_size_always = 0;
893   sf->less_rectangular_check = 0;
894   sf->use_square_partition_only = 0;
895   sf->auto_min_max_partition_size = NOT_IN_USE;
896   sf->max_partition_size = BLOCK_64X64;
897   sf->min_partition_size = BLOCK_4X4;
898   sf->adjust_partitioning_from_last_frame = 0;
899   sf->last_partitioning_redo_frequency = 4;
900   sf->disable_split_mask = 0;
901   sf->mode_search_skip_flags = 0;
902   sf->disable_split_var_thresh = 0;
903   sf->disable_filter_search_var_thresh = 0;
904   for (i = 0; i < TX_SIZES; i++) {
905     sf->intra_y_mode_mask[i] = ALL_INTRA_MODES;
906     sf->intra_uv_mode_mask[i] = ALL_INTRA_MODES;
907   }
908   sf->use_rd_breakout = 0;
909   sf->skip_encode_sb = 0;
910   sf->use_uv_intra_rd_estimate = 0;
911   sf->use_fast_lpf_pick = 0;
912   sf->use_fast_coef_updates = 0;
913   sf->mode_skip_start = MAX_MODES;  // Mode index at which mode skip mask set
914   sf->use_pick_mode = 0;
915   sf->encode_breakout_thresh = 0;
916
917   switch (cpi->oxcf.mode) {
918     case MODE_BESTQUALITY:
919     case MODE_SECONDPASS_BEST:  // This is the best quality mode.
920       cpi->diamond_search_sad = vp9_full_range_search;
921       break;
922     case MODE_FIRSTPASS:
923     case MODE_GOODQUALITY:
924     case MODE_SECONDPASS:
925       set_good_speed_feature(cm, sf, speed);
926       break;
927     case MODE_REALTIME:
928       set_rt_speed_feature(cm, sf, speed);
929       break;
930   }; /* switch */
931
932   // Set rd thresholds based on mode and speed setting
933   set_rd_speed_thresholds(cpi);
934   set_rd_speed_thresholds_sub8x8(cpi);
935
936   // Slow quant, dct and trellis not worthwhile for first pass
937   // so make sure they are always turned off.
938   if (cpi->pass == 1) {
939     sf->optimize_coefficients = 0;
940   }
941
942   // No recode for 1 pass.
943   if (cpi->pass == 0) {
944     sf->recode_loop = DISALLOW_RECODE;
945     sf->optimize_coefficients = 0;
946   }
947
948   cpi->mb.fwd_txm4x4 = vp9_fdct4x4;
949   if (cpi->oxcf.lossless || cpi->mb.e_mbd.lossless) {
950     cpi->mb.fwd_txm4x4 = vp9_fwht4x4;
951   }
952
953   if (cpi->sf.subpel_search_method == SUBPEL_TREE) {
954     cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree;
955     cpi->find_fractional_mv_step_comp = vp9_find_best_sub_pixel_comp_tree;
956   }
957
958   cpi->mb.optimize = cpi->sf.optimize_coefficients == 1 && cpi->pass != 1;
959
960   if (cpi->encode_breakout && cpi->oxcf.mode == MODE_REALTIME &&
961       sf->encode_breakout_thresh > cpi->encode_breakout)
962     cpi->encode_breakout = sf->encode_breakout_thresh;
963 }
964
965 static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
966   VP9_COMMON *cm = &cpi->common;
967
968   cpi->lookahead = vp9_lookahead_init(cpi->oxcf.width, cpi->oxcf.height,
969                                       cm->subsampling_x, cm->subsampling_y,
970                                       cpi->oxcf.lag_in_frames);
971   if (!cpi->lookahead)
972     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
973                        "Failed to allocate lag buffers");
974
975   if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
976                                cpi->oxcf.width, cpi->oxcf.height,
977                                cm->subsampling_x, cm->subsampling_y,
978                                VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
979     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
980                        "Failed to allocate altref buffer");
981 }
982
983 void vp9_alloc_compressor_data(VP9_COMP *cpi) {
984   VP9_COMMON *cm = &cpi->common;
985
986   if (vp9_alloc_frame_buffers(cm, cm->width, cm->height))
987     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
988                        "Failed to allocate frame buffers");
989
990   if (vp9_alloc_frame_buffer(&cpi->last_frame_uf,
991                              cm->width, cm->height,
992                              cm->subsampling_x, cm->subsampling_y,
993                              VP9_ENC_BORDER_IN_PIXELS))
994     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
995                        "Failed to allocate last frame buffer");
996
997   if (vp9_alloc_frame_buffer(&cpi->scaled_source,
998                              cm->width, cm->height,
999                              cm->subsampling_x, cm->subsampling_y,
1000                              VP9_ENC_BORDER_IN_PIXELS))
1001     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1002                        "Failed to allocate scaled source buffer");
1003
1004   vpx_free(cpi->tok);
1005
1006   {
1007     unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
1008
1009     CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
1010   }
1011
1012   vpx_free(cpi->mb_activity_map);
1013   CHECK_MEM_ERROR(cm, cpi->mb_activity_map,
1014                   vpx_calloc(sizeof(unsigned int),
1015                              cm->mb_rows * cm->mb_cols));
1016
1017   vpx_free(cpi->mb_norm_activity_map);
1018   CHECK_MEM_ERROR(cm, cpi->mb_norm_activity_map,
1019                   vpx_calloc(sizeof(unsigned int),
1020                              cm->mb_rows * cm->mb_cols));
1021
1022   // 2 contexts per 'mi unit', so that we have one context per 4x4 txfm
1023   // block where mi unit size is 8x8.
1024   vpx_free(cpi->above_context[0]);
1025   CHECK_MEM_ERROR(cm, cpi->above_context[0],
1026                   vpx_calloc(2 * mi_cols_aligned_to_sb(cm->mi_cols) *
1027                              MAX_MB_PLANE,
1028                              sizeof(*cpi->above_context[0])));
1029
1030   vpx_free(cpi->above_seg_context);
1031   CHECK_MEM_ERROR(cm, cpi->above_seg_context,
1032                   vpx_calloc(mi_cols_aligned_to_sb(cm->mi_cols),
1033                              sizeof(*cpi->above_seg_context)));
1034 }
1035
1036
1037 static void update_frame_size(VP9_COMP *cpi) {
1038   VP9_COMMON *cm = &cpi->common;
1039
1040   vp9_update_frame_size(cm);
1041
1042   // Update size of buffers local to this frame
1043   if (vp9_realloc_frame_buffer(&cpi->last_frame_uf,
1044                                cm->width, cm->height,
1045                                cm->subsampling_x, cm->subsampling_y,
1046                                VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
1047     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1048                        "Failed to reallocate last frame buffer");
1049
1050   if (vp9_realloc_frame_buffer(&cpi->scaled_source,
1051                                cm->width, cm->height,
1052                                cm->subsampling_x, cm->subsampling_y,
1053                                VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
1054     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1055                        "Failed to reallocate scaled source buffer");
1056
1057   {
1058     int y_stride = cpi->scaled_source.y_stride;
1059
1060     if (cpi->sf.search_method == NSTEP) {
1061       vp9_init3smotion_compensation(&cpi->mb, y_stride);
1062     } else if (cpi->sf.search_method == DIAMOND) {
1063       vp9_init_dsmotion_compensation(&cpi->mb, y_stride);
1064     }
1065   }
1066
1067   {
1068     int i;
1069     for (i = 1; i < MAX_MB_PLANE; ++i) {
1070       cpi->above_context[i] = cpi->above_context[0] +
1071                               i * sizeof(*cpi->above_context[0]) * 2 *
1072                               mi_cols_aligned_to_sb(cm->mi_cols);
1073     }
1074   }
1075 }
1076
1077
1078 // Table that converts 0-63 Q range values passed in outside to the Qindex
1079 // range used internally.
1080 static const int q_trans[] = {
1081   0,    4,   8,  12,  16,  20,  24,  28,
1082   32,   36,  40,  44,  48,  52,  56,  60,
1083   64,   68,  72,  76,  80,  84,  88,  92,
1084   96,  100, 104, 108, 112, 116, 120, 124,
1085   128, 132, 136, 140, 144, 148, 152, 156,
1086   160, 164, 168, 172, 176, 180, 184, 188,
1087   192, 196, 200, 204, 208, 212, 216, 220,
1088   224, 228, 232, 236, 240, 244, 249, 255,
1089 };
1090
1091 int vp9_reverse_trans(int x) {
1092   int i;
1093
1094   for (i = 0; i < 64; i++)
1095     if (q_trans[i] >= x)
1096       return i;
1097
1098   return 63;
1099 };
1100
1101 void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
1102   VP9_COMMON *const cm = &cpi->common;
1103   int64_t vbr_max_bits;
1104
1105   if (framerate < 0.1)
1106     framerate = 30;
1107
1108   cpi->oxcf.framerate = framerate;
1109   cpi->output_framerate = cpi->oxcf.framerate;
1110   cpi->rc.av_per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth
1111                                          / cpi->output_framerate);
1112   cpi->rc.min_frame_bandwidth = (int)(cpi->rc.av_per_frame_bandwidth *
1113                                       cpi->oxcf.two_pass_vbrmin_section / 100);
1114
1115
1116   cpi->rc.min_frame_bandwidth = MAX(cpi->rc.min_frame_bandwidth,
1117                                     FRAME_OVERHEAD_BITS);
1118
1119   // A maximum bitrate for a frame is defined.
1120   // The baseline for this aligns with HW implementations that
1121   // can support decode of 1080P content up to a bitrate of MAX_MB_RATE bits
1122   // per 16x16 MB (averaged over a frame). However this limit is extended if
1123   // a very high rate is given on the command line or the the rate cannnot
1124   // be acheived because of a user specificed max q (e.g. when the user
1125   // specifies lossless encode.
1126   //
1127   vbr_max_bits = ((int64_t)cpi->rc.av_per_frame_bandwidth *
1128                   (int64_t)cpi->oxcf.two_pass_vbrmax_section) / 100;
1129   cpi->rc.max_frame_bandwidth =
1130     MAX(MAX((cm->MBs * MAX_MB_RATE), MAXRATE_1080P), vbr_max_bits);
1131
1132   // Set Maximum gf/arf interval
1133   cpi->rc.max_gf_interval = 16;
1134
1135   // Extended interval for genuinely static scenes
1136   cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
1137
1138   // Special conditions when alt ref frame enabled in lagged compress mode
1139   if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames) {
1140     if (cpi->rc.max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1141       cpi->rc.max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1142
1143     if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1144       cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1145   }
1146
1147   if (cpi->rc.max_gf_interval > cpi->twopass.static_scene_max_gf_interval)
1148     cpi->rc.max_gf_interval = cpi->twopass.static_scene_max_gf_interval;
1149 }
1150
1151 static int64_t rescale(int val, int64_t num, int denom) {
1152   int64_t llnum = num;
1153   int64_t llden = denom;
1154   int64_t llval = val;
1155
1156   return (llval * llnum / llden);
1157 }
1158
1159 // Initialize layer context data from init_config().
1160 static void init_layer_context(VP9_COMP *const cpi) {
1161   const VP9_CONFIG *const oxcf = &cpi->oxcf;
1162   int temporal_layer = 0;
1163   cpi->svc.spatial_layer_id = 0;
1164   cpi->svc.temporal_layer_id = 0;
1165   for (temporal_layer = 0; temporal_layer < cpi->svc.number_temporal_layers;
1166       ++temporal_layer) {
1167     LAYER_CONTEXT *const lc = &cpi->svc.layer_context[temporal_layer];
1168     RATE_CONTROL *const lrc = &lc->rc;
1169     lrc->avg_frame_qindex[INTER_FRAME] = q_trans[oxcf->worst_allowed_q];
1170     lrc->last_q[INTER_FRAME] = q_trans[oxcf->worst_allowed_q];
1171     lrc->ni_av_qi = q_trans[oxcf->worst_allowed_q];
1172     lrc->total_actual_bits = 0;
1173     lrc->total_target_vs_actual = 0;
1174     lrc->ni_tot_qi = 0;
1175     lrc->tot_q = 0.0;
1176     lrc->avg_q = 0.0;
1177     lrc->ni_frames = 0;
1178     lrc->decimation_count = 0;
1179     lrc->decimation_factor = 0;
1180     lrc->rate_correction_factor = 1.0;
1181     lrc->key_frame_rate_correction_factor = 1.0;
1182     lc->target_bandwidth = oxcf->ts_target_bitrate[temporal_layer] *
1183         1000;
1184     lrc->buffer_level = rescale((int)(oxcf->starting_buffer_level),
1185                                lc->target_bandwidth, 1000);
1186     lrc->bits_off_target = lrc->buffer_level;
1187   }
1188 }
1189
1190 // Update the layer context from a change_config() call.
1191 static void update_layer_context_change_config(VP9_COMP *const cpi,
1192                                                const int target_bandwidth) {
1193   const VP9_CONFIG *const oxcf = &cpi->oxcf;
1194   const RATE_CONTROL *const rc = &cpi->rc;
1195   int temporal_layer = 0;
1196   float bitrate_alloc = 1.0;
1197   for (temporal_layer = 0; temporal_layer < cpi->svc.number_temporal_layers;
1198       ++temporal_layer) {
1199     LAYER_CONTEXT *const lc = &cpi->svc.layer_context[temporal_layer];
1200     RATE_CONTROL *const lrc = &lc->rc;
1201     lc->target_bandwidth = oxcf->ts_target_bitrate[temporal_layer] * 1000;
1202     bitrate_alloc = (float)lc->target_bandwidth / (float)target_bandwidth;
1203     // Update buffer-related quantities.
1204     lc->starting_buffer_level = oxcf->starting_buffer_level * bitrate_alloc;
1205     lc->optimal_buffer_level = oxcf->optimal_buffer_level * bitrate_alloc;
1206     lc->maximum_buffer_size = oxcf->maximum_buffer_size * bitrate_alloc;
1207     lrc->bits_off_target = MIN(lrc->bits_off_target, lc->maximum_buffer_size);
1208     lrc->buffer_level = MIN(lrc->buffer_level, lc->maximum_buffer_size);
1209     // Update framerate-related quantities.
1210     lc->framerate = oxcf->framerate / oxcf->ts_rate_decimator[temporal_layer];
1211     lrc->av_per_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
1212     lrc->max_frame_bandwidth = rc->max_frame_bandwidth;
1213     // Update qp-related quantities.
1214     lrc->worst_quality = rc->worst_quality;
1215     lrc->best_quality = rc->best_quality;
1216   }
1217 }
1218
1219 // Prior to encoding the frame, update framerate-related quantities
1220 // for the current layer.
1221 static void update_layer_framerate(VP9_COMP *const cpi) {
1222   int temporal_layer = cpi->svc.temporal_layer_id;
1223   const VP9_CONFIG *const oxcf = &cpi->oxcf;
1224   LAYER_CONTEXT *const lc = &cpi->svc.layer_context[temporal_layer];
1225   RATE_CONTROL *const lrc = &lc->rc;
1226   lc->framerate = oxcf->framerate / oxcf->ts_rate_decimator[temporal_layer];
1227   lrc->av_per_frame_bandwidth = (int)(lc->target_bandwidth / lc->framerate);
1228   lrc->max_frame_bandwidth = cpi->rc.max_frame_bandwidth;
1229   // Update the average layer frame size (non-cumulative per-frame-bw).
1230   if (temporal_layer == 0) {
1231     lc->avg_frame_size = lrc->av_per_frame_bandwidth;
1232   } else {
1233     double prev_layer_framerate = oxcf->framerate /
1234         oxcf->ts_rate_decimator[temporal_layer - 1];
1235     int prev_layer_target_bandwidth =
1236         oxcf->ts_target_bitrate[temporal_layer - 1] * 1000;
1237     lc->avg_frame_size =
1238         (int)(lc->target_bandwidth - prev_layer_target_bandwidth) /
1239         (lc->framerate - prev_layer_framerate);
1240   }
1241 }
1242
1243 // Prior to encoding the frame, set the layer context, for the current layer
1244 // to be encoded, to the cpi struct.
1245 static void restore_layer_context(VP9_COMP *const cpi) {
1246   int temporal_layer = cpi->svc.temporal_layer_id;
1247   LAYER_CONTEXT *lc = &cpi->svc.layer_context[temporal_layer];
1248   int frame_since_key = cpi->rc.frames_since_key;
1249   int frame_to_key = cpi->rc.frames_to_key;
1250   cpi->rc = lc->rc;
1251   cpi->oxcf.target_bandwidth = lc->target_bandwidth;
1252   cpi->oxcf.starting_buffer_level = lc->starting_buffer_level;
1253   cpi->oxcf.optimal_buffer_level = lc->optimal_buffer_level;
1254   cpi->oxcf.maximum_buffer_size = lc->maximum_buffer_size;
1255   cpi->output_framerate = lc->framerate;
1256   // Reset the frames_since_key and frames_to_key counters to their values
1257   // before the layer restore. Keep these defined for the stream (not layer).
1258   cpi->rc.frames_since_key = frame_since_key;
1259   cpi->rc.frames_to_key = frame_to_key;
1260 }
1261
1262 // Save the layer context after encoding the frame.
1263 static void save_layer_context(VP9_COMP *const cpi) {
1264   int temporal_layer = cpi->svc.temporal_layer_id;
1265   LAYER_CONTEXT *lc = &cpi->svc.layer_context[temporal_layer];
1266   lc->rc = cpi->rc;
1267   lc->target_bandwidth = cpi->oxcf.target_bandwidth;
1268   lc->starting_buffer_level = cpi->oxcf.starting_buffer_level;
1269   lc->optimal_buffer_level = cpi->oxcf.optimal_buffer_level;
1270   lc->maximum_buffer_size = cpi->oxcf.maximum_buffer_size;
1271   lc->framerate = cpi->output_framerate;
1272 }
1273
1274 static void set_tile_limits(VP9_COMP *cpi) {
1275   VP9_COMMON *const cm = &cpi->common;
1276
1277   int min_log2_tile_cols, max_log2_tile_cols;
1278   vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
1279
1280   cm->log2_tile_cols = clamp(cpi->oxcf.tile_columns,
1281                              min_log2_tile_cols, max_log2_tile_cols);
1282   cm->log2_tile_rows = cpi->oxcf.tile_rows;
1283 }
1284
1285 static void init_config(VP9_PTR ptr, VP9_CONFIG *oxcf) {
1286   VP9_COMP *cpi = (VP9_COMP *)(ptr);
1287   VP9_COMMON *const cm = &cpi->common;
1288   int i;
1289
1290   cpi->oxcf = *oxcf;
1291
1292   cm->version = oxcf->version;
1293
1294   cm->width = oxcf->width;
1295   cm->height = oxcf->height;
1296   cm->subsampling_x = 0;
1297   cm->subsampling_y = 0;
1298   vp9_alloc_compressor_data(cpi);
1299
1300   // Spatial scalability.
1301   cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
1302   // Temporal scalability.
1303   cpi->svc.number_temporal_layers = oxcf->ts_number_layers;
1304
1305   if (cpi->svc.number_temporal_layers > 1 &&
1306       cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
1307     init_layer_context(cpi);
1308   }
1309
1310   // change includes all joint functionality
1311   vp9_change_config(ptr, oxcf);
1312
1313   // Initialize active best and worst q and average q values.
1314   if (cpi->pass == 0 && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
1315     cpi->rc.avg_frame_qindex[0] = cpi->oxcf.worst_allowed_q;
1316     cpi->rc.avg_frame_qindex[1] = cpi->oxcf.worst_allowed_q;
1317     cpi->rc.avg_frame_qindex[2] = cpi->oxcf.worst_allowed_q;
1318   } else {
1319     cpi->rc.avg_frame_qindex[0] = (cpi->oxcf.worst_allowed_q +
1320                                    cpi->oxcf.best_allowed_q) / 2;
1321     cpi->rc.avg_frame_qindex[1] = (cpi->oxcf.worst_allowed_q +
1322                                    cpi->oxcf.best_allowed_q) / 2;
1323     cpi->rc.avg_frame_qindex[2] = (cpi->oxcf.worst_allowed_q +
1324                                    cpi->oxcf.best_allowed_q) / 2;
1325   }
1326   cpi->rc.last_q[0]                 = cpi->oxcf.best_allowed_q;
1327   cpi->rc.last_q[1]                 = cpi->oxcf.best_allowed_q;
1328   cpi->rc.last_q[2]                 = cpi->oxcf.best_allowed_q;
1329
1330   // Initialise the starting buffer levels
1331   cpi->rc.buffer_level              = cpi->oxcf.starting_buffer_level;
1332   cpi->rc.bits_off_target           = cpi->oxcf.starting_buffer_level;
1333
1334   cpi->rc.rolling_target_bits       = cpi->rc.av_per_frame_bandwidth;
1335   cpi->rc.rolling_actual_bits       = cpi->rc.av_per_frame_bandwidth;
1336   cpi->rc.long_rolling_target_bits  = cpi->rc.av_per_frame_bandwidth;
1337   cpi->rc.long_rolling_actual_bits  = cpi->rc.av_per_frame_bandwidth;
1338
1339   cpi->rc.total_actual_bits         = 0;
1340   cpi->rc.total_target_vs_actual    = 0;
1341
1342   cpi->static_mb_pct = 0;
1343
1344   cpi->lst_fb_idx = 0;
1345   cpi->gld_fb_idx = 1;
1346   cpi->alt_fb_idx = 2;
1347
1348   set_tile_limits(cpi);
1349
1350   cpi->fixed_divide[0] = 0;
1351   for (i = 1; i < 512; i++)
1352     cpi->fixed_divide[i] = 0x80000 / i;
1353 }
1354
1355 void vp9_change_config(VP9_PTR ptr, VP9_CONFIG *oxcf) {
1356   VP9_COMP *cpi = (VP9_COMP *)(ptr);
1357   VP9_COMMON *const cm = &cpi->common;
1358
1359   if (!cpi || !oxcf)
1360     return;
1361
1362   if (cm->version != oxcf->version) {
1363     cm->version = oxcf->version;
1364   }
1365
1366   cpi->oxcf = *oxcf;
1367
1368   switch (cpi->oxcf.mode) {
1369       // Real time and one pass deprecated in test code base
1370     case MODE_GOODQUALITY:
1371       cpi->pass = 0;
1372       cpi->oxcf.cpu_used = clamp(cpi->oxcf.cpu_used, -5, 5);
1373       break;
1374
1375     case MODE_FIRSTPASS:
1376       cpi->pass = 1;
1377       break;
1378
1379     case MODE_SECONDPASS:
1380       cpi->pass = 2;
1381       cpi->oxcf.cpu_used = clamp(cpi->oxcf.cpu_used, -5, 5);
1382       break;
1383
1384     case MODE_SECONDPASS_BEST:
1385       cpi->pass = 2;
1386       break;
1387
1388     case MODE_REALTIME:
1389       cpi->pass = 0;
1390       break;
1391   }
1392
1393   cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1394   cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1395   cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
1396
1397   cpi->oxcf.lossless = oxcf->lossless;
1398   cpi->mb.e_mbd.itxm_add = cpi->oxcf.lossless ? vp9_iwht4x4_add
1399                                               : vp9_idct4x4_add;
1400   cpi->rc.baseline_gf_interval = DEFAULT_GF_INTERVAL;
1401
1402   cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
1403
1404   cpi->refresh_golden_frame = 0;
1405   cpi->refresh_last_frame = 1;
1406   cm->refresh_frame_context = 1;
1407   cm->reset_frame_context = 0;
1408
1409   vp9_reset_segment_features(&cm->seg);
1410   set_high_precision_mv(cpi, 0);
1411
1412   {
1413     int i;
1414
1415     for (i = 0; i < MAX_SEGMENTS; i++)
1416       cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1417   }
1418   cpi->encode_breakout = cpi->oxcf.encode_breakout;
1419
1420   // local file playback mode == really big buffer
1421   if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK) {
1422     cpi->oxcf.starting_buffer_level   = 60000;
1423     cpi->oxcf.optimal_buffer_level    = 60000;
1424     cpi->oxcf.maximum_buffer_size     = 240000;
1425   }
1426
1427   // Convert target bandwidth from Kbit/s to Bit/s
1428   cpi->oxcf.target_bandwidth       *= 1000;
1429
1430   cpi->oxcf.starting_buffer_level = rescale(cpi->oxcf.starting_buffer_level,
1431                                             cpi->oxcf.target_bandwidth, 1000);
1432
1433   // Set or reset optimal and maximum buffer levels.
1434   if (cpi->oxcf.optimal_buffer_level == 0)
1435     cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1436   else
1437     cpi->oxcf.optimal_buffer_level = rescale(cpi->oxcf.optimal_buffer_level,
1438                                              cpi->oxcf.target_bandwidth, 1000);
1439
1440   if (cpi->oxcf.maximum_buffer_size == 0)
1441     cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
1442   else
1443     cpi->oxcf.maximum_buffer_size = rescale(cpi->oxcf.maximum_buffer_size,
1444                                             cpi->oxcf.target_bandwidth, 1000);
1445   // Under a configuration change, where maximum_buffer_size may change,
1446   // keep buffer level clipped to the maximum allowed buffer size.
1447   cpi->rc.bits_off_target = MIN(cpi->rc.bits_off_target,
1448                                 cpi->oxcf.maximum_buffer_size);
1449   cpi->rc.buffer_level = MIN(cpi->rc.buffer_level,
1450                              cpi->oxcf.maximum_buffer_size);
1451
1452   // Set up frame rate and related parameters rate control values.
1453   vp9_new_framerate(cpi, cpi->oxcf.framerate);
1454
1455   // Set absolute upper and lower quality limits
1456   cpi->rc.worst_quality = cpi->oxcf.worst_allowed_q;
1457   cpi->rc.best_quality = cpi->oxcf.best_allowed_q;
1458
1459   // active values should only be modified if out of new range
1460
1461   cpi->cq_target_quality = cpi->oxcf.cq_level;
1462
1463   cm->interp_filter = DEFAULT_INTERP_FILTER;
1464
1465   cm->display_width = cpi->oxcf.width;
1466   cm->display_height = cpi->oxcf.height;
1467
1468   // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
1469   cpi->oxcf.sharpness = MIN(7, cpi->oxcf.sharpness);
1470
1471   cpi->common.lf.sharpness_level = cpi->oxcf.sharpness;
1472
1473   if (cpi->initial_width) {
1474     // Increasing the size of the frame beyond the first seen frame, or some
1475     // otherwise signaled maximum size, is not supported.
1476     // TODO(jkoleszar): exit gracefully.
1477     assert(cm->width <= cpi->initial_width);
1478     assert(cm->height <= cpi->initial_height);
1479   }
1480   update_frame_size(cpi);
1481
1482   if (cpi->svc.number_temporal_layers > 1 &&
1483       cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
1484     update_layer_context_change_config(cpi, cpi->oxcf.target_bandwidth);
1485   }
1486
1487   cpi->speed = cpi->oxcf.cpu_used;
1488
1489   if (cpi->oxcf.lag_in_frames == 0) {
1490     // Force allow_lag to 0 if lag_in_frames is 0.
1491     cpi->oxcf.allow_lag = 0;
1492   } else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS) {
1493      // Limit on lag buffers as these are not currently dynamically allocated.
1494     cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
1495   }
1496
1497 #if CONFIG_MULTIPLE_ARF
1498   vp9_zero(cpi->alt_ref_source);
1499 #else
1500   cpi->alt_ref_source = NULL;
1501 #endif
1502   cpi->rc.is_src_frame_alt_ref = 0;
1503
1504 #if 0
1505   // Experimental RD Code
1506   cpi->frame_distortion = 0;
1507   cpi->last_frame_distortion = 0;
1508 #endif
1509
1510   set_tile_limits(cpi);
1511
1512   cpi->ext_refresh_frame_flags_pending = 0;
1513   cpi->ext_refresh_frame_context_pending = 0;
1514 }
1515
1516 #define M_LOG2_E 0.693147180559945309417
1517 #define log2f(x) (log (x) / (float) M_LOG2_E)
1518
1519 static void cal_nmvjointsadcost(int *mvjointsadcost) {
1520   mvjointsadcost[0] = 600;
1521   mvjointsadcost[1] = 300;
1522   mvjointsadcost[2] = 300;
1523   mvjointsadcost[0] = 300;
1524 }
1525
1526 static void cal_nmvsadcosts(int *mvsadcost[2]) {
1527   int i = 1;
1528
1529   mvsadcost[0][0] = 0;
1530   mvsadcost[1][0] = 0;
1531
1532   do {
1533     double z = 256 * (2 * (log2f(8 * i) + .6));
1534     mvsadcost[0][i] = (int)z;
1535     mvsadcost[1][i] = (int)z;
1536     mvsadcost[0][-i] = (int)z;
1537     mvsadcost[1][-i] = (int)z;
1538   } while (++i <= MV_MAX);
1539 }
1540
1541 static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
1542   int i = 1;
1543
1544   mvsadcost[0][0] = 0;
1545   mvsadcost[1][0] = 0;
1546
1547   do {
1548     double z = 256 * (2 * (log2f(8 * i) + .6));
1549     mvsadcost[0][i] = (int)z;
1550     mvsadcost[1][i] = (int)z;
1551     mvsadcost[0][-i] = (int)z;
1552     mvsadcost[1][-i] = (int)z;
1553   } while (++i <= MV_MAX);
1554 }
1555
1556 static void alloc_mode_context(VP9_COMMON *cm, int num_4x4_blk,
1557                                PICK_MODE_CONTEXT *ctx) {
1558   int num_pix = num_4x4_blk << 4;
1559   int i, k;
1560   ctx->num_4x4_blk = num_4x4_blk;
1561   CHECK_MEM_ERROR(cm, ctx->zcoeff_blk,
1562                   vpx_calloc(num_4x4_blk, sizeof(uint8_t)));
1563   for (i = 0; i < MAX_MB_PLANE; ++i) {
1564     for (k = 0; k < 3; ++k) {
1565       CHECK_MEM_ERROR(cm, ctx->coeff[i][k],
1566                       vpx_memalign(16, num_pix * sizeof(int16_t)));
1567       CHECK_MEM_ERROR(cm, ctx->qcoeff[i][k],
1568                       vpx_memalign(16, num_pix * sizeof(int16_t)));
1569       CHECK_MEM_ERROR(cm, ctx->dqcoeff[i][k],
1570                       vpx_memalign(16, num_pix * sizeof(int16_t)));
1571       CHECK_MEM_ERROR(cm, ctx->eobs[i][k],
1572                       vpx_memalign(16, num_pix * sizeof(uint16_t)));
1573       ctx->coeff_pbuf[i][k]   = ctx->coeff[i][k];
1574       ctx->qcoeff_pbuf[i][k]  = ctx->qcoeff[i][k];
1575       ctx->dqcoeff_pbuf[i][k] = ctx->dqcoeff[i][k];
1576       ctx->eobs_pbuf[i][k]    = ctx->eobs[i][k];
1577     }
1578   }
1579 }
1580
1581 static void free_mode_context(PICK_MODE_CONTEXT *ctx) {
1582   int i, k;
1583   vpx_free(ctx->zcoeff_blk);
1584   ctx->zcoeff_blk = 0;
1585   for (i = 0; i < MAX_MB_PLANE; ++i) {
1586     for (k = 0; k < 3; ++k) {
1587       vpx_free(ctx->coeff[i][k]);
1588       ctx->coeff[i][k] = 0;
1589       vpx_free(ctx->qcoeff[i][k]);
1590       ctx->qcoeff[i][k] = 0;
1591       vpx_free(ctx->dqcoeff[i][k]);
1592       ctx->dqcoeff[i][k] = 0;
1593       vpx_free(ctx->eobs[i][k]);
1594       ctx->eobs[i][k] = 0;
1595     }
1596   }
1597 }
1598
1599 static void init_pick_mode_context(VP9_COMP *cpi) {
1600   int i;
1601   VP9_COMMON *const cm = &cpi->common;
1602   MACROBLOCK *const x  = &cpi->mb;
1603
1604
1605   for (i = 0; i < BLOCK_SIZES; ++i) {
1606     const int num_4x4_w = num_4x4_blocks_wide_lookup[i];
1607     const int num_4x4_h = num_4x4_blocks_high_lookup[i];
1608     const int num_4x4_blk = MAX(4, num_4x4_w * num_4x4_h);
1609     if (i < BLOCK_16X16) {
1610       for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) {
1611         for (x->mb_index = 0; x->mb_index < 4; ++x->mb_index) {
1612           for (x->b_index = 0; x->b_index < 16 / num_4x4_blk; ++x->b_index) {
1613             PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1614             alloc_mode_context(cm, num_4x4_blk, ctx);
1615           }
1616         }
1617       }
1618     } else if (i < BLOCK_32X32) {
1619       for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) {
1620         for (x->mb_index = 0; x->mb_index < 64 / num_4x4_blk; ++x->mb_index) {
1621           PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1622           ctx->num_4x4_blk = num_4x4_blk;
1623           alloc_mode_context(cm, num_4x4_blk, ctx);
1624         }
1625       }
1626     } else if (i < BLOCK_64X64) {
1627       for (x->sb_index = 0; x->sb_index < 256 / num_4x4_blk; ++x->sb_index) {
1628         PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1629         ctx->num_4x4_blk = num_4x4_blk;
1630         alloc_mode_context(cm, num_4x4_blk, ctx);
1631       }
1632     } else {
1633       PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1634       ctx->num_4x4_blk = num_4x4_blk;
1635       alloc_mode_context(cm, num_4x4_blk, ctx);
1636     }
1637   }
1638 }
1639
1640 static void free_pick_mode_context(MACROBLOCK *x) {
1641   int i;
1642
1643   for (i = 0; i < BLOCK_SIZES; ++i) {
1644     const int num_4x4_w = num_4x4_blocks_wide_lookup[i];
1645     const int num_4x4_h = num_4x4_blocks_high_lookup[i];
1646     const int num_4x4_blk = MAX(4, num_4x4_w * num_4x4_h);
1647     if (i < BLOCK_16X16) {
1648       for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) {
1649         for (x->mb_index = 0; x->mb_index < 4; ++x->mb_index) {
1650           for (x->b_index = 0; x->b_index < 16 / num_4x4_blk; ++x->b_index) {
1651             PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1652             free_mode_context(ctx);
1653           }
1654         }
1655       }
1656     } else if (i < BLOCK_32X32) {
1657       for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) {
1658         for (x->mb_index = 0; x->mb_index < 64 / num_4x4_blk; ++x->mb_index) {
1659           PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1660           free_mode_context(ctx);
1661         }
1662       }
1663     } else if (i < BLOCK_64X64) {
1664       for (x->sb_index = 0; x->sb_index < 256 / num_4x4_blk; ++x->sb_index) {
1665         PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1666         free_mode_context(ctx);
1667       }
1668     } else {
1669       PICK_MODE_CONTEXT *ctx = get_block_context(x, i);
1670       free_mode_context(ctx);
1671     }
1672   }
1673 }
1674
1675 VP9_PTR vp9_create_compressor(VP9_CONFIG *oxcf) {
1676   int i, j;
1677   volatile union {
1678     VP9_COMP *cpi;
1679     VP9_PTR   ptr;
1680   } ctx;
1681
1682   VP9_COMP *cpi;
1683   VP9_COMMON *cm;
1684
1685   cpi = ctx.cpi = vpx_memalign(32, sizeof(VP9_COMP));
1686   // Check that the CPI instance is valid
1687   if (!cpi)
1688     return 0;
1689
1690   cm = &cpi->common;
1691
1692   vp9_zero(*cpi);
1693
1694   if (setjmp(cm->error.jmp)) {
1695     VP9_PTR ptr = ctx.ptr;
1696
1697     ctx.cpi->common.error.setjmp = 0;
1698     vp9_remove_compressor(&ptr);
1699     return 0;
1700   }
1701
1702   cm->error.setjmp = 1;
1703
1704   CHECK_MEM_ERROR(cm, cpi->mb.ss, vpx_calloc(sizeof(search_site),
1705                                              (MAX_MVSEARCH_STEPS * 8) + 1));
1706
1707   vp9_create_common(cm);
1708
1709   cpi->use_svc = 0;
1710
1711   init_config((VP9_PTR)cpi, oxcf);
1712
1713   init_pick_mode_context(cpi);
1714
1715   cm->current_video_frame   = 0;
1716
1717   // Set reference frame sign bias for ALTREF frame to 1 (for now)
1718   cm->ref_frame_sign_bias[ALTREF_FRAME] = 1;
1719
1720   cpi->rc.baseline_gf_interval = DEFAULT_GF_INTERVAL;
1721
1722   cpi->gold_is_last = 0;
1723   cpi->alt_is_last  = 0;
1724   cpi->gold_is_alt  = 0;
1725
1726   // Create the encoder segmentation map and set all entries to 0
1727   CHECK_MEM_ERROR(cm, cpi->segmentation_map,
1728                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1729
1730   // Create a complexity map used for rd adjustment
1731   CHECK_MEM_ERROR(cm, cpi->complexity_map,
1732                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1733
1734
1735   // And a place holder structure is the coding context
1736   // for use if we want to save and restore it
1737   CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy,
1738                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1739
1740   CHECK_MEM_ERROR(cm, cpi->active_map, vpx_calloc(cm->MBs, 1));
1741   vpx_memset(cpi->active_map, 1, cm->MBs);
1742   cpi->active_map_enabled = 0;
1743
1744   for (i = 0; i < (sizeof(cpi->mbgraph_stats) /
1745                    sizeof(cpi->mbgraph_stats[0])); i++) {
1746     CHECK_MEM_ERROR(cm, cpi->mbgraph_stats[i].mb_stats,
1747                     vpx_calloc(cm->MBs *
1748                                sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
1749   }
1750
1751   /*Initialize the feed-forward activity masking.*/
1752   cpi->activity_avg = 90 << 12;
1753   cpi->key_frame_frequency = cpi->oxcf.key_freq;
1754
1755   cpi->rc.frames_since_key = 8;  // Sensible default for first frame.
1756   cpi->rc.this_key_frame_forced = 0;
1757   cpi->rc.next_key_frame_forced = 0;
1758
1759   cpi->rc.source_alt_ref_pending = 0;
1760   cpi->rc.source_alt_ref_active = 0;
1761   cpi->refresh_alt_ref_frame = 0;
1762
1763 #if CONFIG_MULTIPLE_ARF
1764   // Turn multiple ARF usage on/off. This is a quick hack for the initial test
1765   // version. It should eventually be set via the codec API.
1766   cpi->multi_arf_enabled = 1;
1767
1768   if (cpi->multi_arf_enabled) {
1769     cpi->sequence_number = 0;
1770     cpi->frame_coding_order_period = 0;
1771     vp9_zero(cpi->frame_coding_order);
1772     vp9_zero(cpi->arf_buffer_idx);
1773   }
1774 #endif
1775
1776   cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1777 #if CONFIG_INTERNAL_STATS
1778   cpi->b_calculate_ssimg = 0;
1779
1780   cpi->count = 0;
1781   cpi->bytes = 0;
1782
1783   if (cpi->b_calculate_psnr) {
1784     cpi->total_y = 0.0;
1785     cpi->total_u = 0.0;
1786     cpi->total_v = 0.0;
1787     cpi->total = 0.0;
1788     cpi->total_sq_error = 0;
1789     cpi->total_samples = 0;
1790
1791     cpi->totalp_y = 0.0;
1792     cpi->totalp_u = 0.0;
1793     cpi->totalp_v = 0.0;
1794     cpi->totalp = 0.0;
1795     cpi->totalp_sq_error = 0;
1796     cpi->totalp_samples = 0;
1797
1798     cpi->tot_recode_hits = 0;
1799     cpi->summed_quality = 0;
1800     cpi->summed_weights = 0;
1801     cpi->summedp_quality = 0;
1802     cpi->summedp_weights = 0;
1803   }
1804
1805   if (cpi->b_calculate_ssimg) {
1806     cpi->total_ssimg_y = 0;
1807     cpi->total_ssimg_u = 0;
1808     cpi->total_ssimg_v = 0;
1809     cpi->total_ssimg_all = 0;
1810   }
1811
1812 #endif
1813
1814   cpi->first_time_stamp_ever = INT64_MAX;
1815
1816   cpi->rc.frames_till_gf_update_due      = 0;
1817
1818   cpi->rc.ni_av_qi                     = cpi->oxcf.worst_allowed_q;
1819   cpi->rc.ni_tot_qi                    = 0;
1820   cpi->rc.ni_frames                   = 0;
1821   cpi->rc.tot_q = 0.0;
1822   cpi->rc.avg_q = vp9_convert_qindex_to_q(cpi->oxcf.worst_allowed_q);
1823
1824   cpi->rc.rate_correction_factor         = 1.0;
1825   cpi->rc.key_frame_rate_correction_factor = 1.0;
1826   cpi->rc.gf_rate_correction_factor  = 1.0;
1827
1828   cal_nmvjointsadcost(cpi->mb.nmvjointsadcost);
1829   cpi->mb.nmvcost[0] = &cpi->mb.nmvcosts[0][MV_MAX];
1830   cpi->mb.nmvcost[1] = &cpi->mb.nmvcosts[1][MV_MAX];
1831   cpi->mb.nmvsadcost[0] = &cpi->mb.nmvsadcosts[0][MV_MAX];
1832   cpi->mb.nmvsadcost[1] = &cpi->mb.nmvsadcosts[1][MV_MAX];
1833   cal_nmvsadcosts(cpi->mb.nmvsadcost);
1834
1835   cpi->mb.nmvcost_hp[0] = &cpi->mb.nmvcosts_hp[0][MV_MAX];
1836   cpi->mb.nmvcost_hp[1] = &cpi->mb.nmvcosts_hp[1][MV_MAX];
1837   cpi->mb.nmvsadcost_hp[0] = &cpi->mb.nmvsadcosts_hp[0][MV_MAX];
1838   cpi->mb.nmvsadcost_hp[1] = &cpi->mb.nmvsadcosts_hp[1][MV_MAX];
1839   cal_nmvsadcosts_hp(cpi->mb.nmvsadcost_hp);
1840
1841 #ifdef OUTPUT_YUV_SRC
1842   yuv_file = fopen("bd.yuv", "ab");
1843 #endif
1844 #ifdef OUTPUT_YUV_REC
1845   yuv_rec_file = fopen("rec.yuv", "wb");
1846 #endif
1847
1848 #if 0
1849   framepsnr = fopen("framepsnr.stt", "a");
1850   kf_list = fopen("kf_list.stt", "w");
1851 #endif
1852
1853   cpi->output_pkt_list = oxcf->output_pkt_list;
1854
1855   cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
1856
1857   if (cpi->pass == 1) {
1858     vp9_init_first_pass(cpi);
1859   } else if (cpi->pass == 2) {
1860     size_t packet_sz = sizeof(FIRSTPASS_STATS);
1861     int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
1862
1863     cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
1864     cpi->twopass.stats_in = cpi->twopass.stats_in_start;
1865     cpi->twopass.stats_in_end = (void *)((char *)cpi->twopass.stats_in
1866                                          + (packets - 1) * packet_sz);
1867     vp9_init_second_pass(cpi);
1868   }
1869
1870   vp9_set_speed_features(cpi);
1871
1872   // Default rd threshold factors for mode selection
1873   for (i = 0; i < BLOCK_SIZES; ++i) {
1874     for (j = 0; j < MAX_MODES; ++j)
1875       cpi->rd_thresh_freq_fact[i][j] = 32;
1876     for (j = 0; j < MAX_REFS; ++j)
1877       cpi->rd_thresh_freq_sub8x8[i][j] = 32;
1878   }
1879
1880 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SVFHH, SVFHV, SVFHHV, \
1881             SDX3F, SDX8F, SDX4DF)\
1882     cpi->fn_ptr[BT].sdf            = SDF; \
1883     cpi->fn_ptr[BT].sdaf           = SDAF; \
1884     cpi->fn_ptr[BT].vf             = VF; \
1885     cpi->fn_ptr[BT].svf            = SVF; \
1886     cpi->fn_ptr[BT].svaf           = SVAF; \
1887     cpi->fn_ptr[BT].svf_halfpix_h  = SVFHH; \
1888     cpi->fn_ptr[BT].svf_halfpix_v  = SVFHV; \
1889     cpi->fn_ptr[BT].svf_halfpix_hv = SVFHHV; \
1890     cpi->fn_ptr[BT].sdx3f          = SDX3F; \
1891     cpi->fn_ptr[BT].sdx8f          = SDX8F; \
1892     cpi->fn_ptr[BT].sdx4df         = SDX4DF;
1893
1894   BFP(BLOCK_32X16, vp9_sad32x16, vp9_sad32x16_avg,
1895       vp9_variance32x16, vp9_sub_pixel_variance32x16,
1896       vp9_sub_pixel_avg_variance32x16, NULL, NULL,
1897       NULL, NULL, NULL,
1898       vp9_sad32x16x4d)
1899
1900   BFP(BLOCK_16X32, vp9_sad16x32, vp9_sad16x32_avg,
1901       vp9_variance16x32, vp9_sub_pixel_variance16x32,
1902       vp9_sub_pixel_avg_variance16x32, NULL, NULL,
1903       NULL, NULL, NULL,
1904       vp9_sad16x32x4d)
1905
1906   BFP(BLOCK_64X32, vp9_sad64x32, vp9_sad64x32_avg,
1907       vp9_variance64x32, vp9_sub_pixel_variance64x32,
1908       vp9_sub_pixel_avg_variance64x32, NULL, NULL,
1909       NULL, NULL, NULL,
1910       vp9_sad64x32x4d)
1911
1912   BFP(BLOCK_32X64, vp9_sad32x64, vp9_sad32x64_avg,
1913       vp9_variance32x64, vp9_sub_pixel_variance32x64,
1914       vp9_sub_pixel_avg_variance32x64, NULL, NULL,
1915       NULL, NULL, NULL,
1916       vp9_sad32x64x4d)
1917
1918   BFP(BLOCK_32X32, vp9_sad32x32, vp9_sad32x32_avg,
1919       vp9_variance32x32, vp9_sub_pixel_variance32x32,
1920       vp9_sub_pixel_avg_variance32x32, vp9_variance_halfpixvar32x32_h,
1921       vp9_variance_halfpixvar32x32_v,
1922       vp9_variance_halfpixvar32x32_hv, vp9_sad32x32x3, vp9_sad32x32x8,
1923       vp9_sad32x32x4d)
1924
1925   BFP(BLOCK_64X64, vp9_sad64x64, vp9_sad64x64_avg,
1926       vp9_variance64x64, vp9_sub_pixel_variance64x64,
1927       vp9_sub_pixel_avg_variance64x64, vp9_variance_halfpixvar64x64_h,
1928       vp9_variance_halfpixvar64x64_v,
1929       vp9_variance_halfpixvar64x64_hv, vp9_sad64x64x3, vp9_sad64x64x8,
1930       vp9_sad64x64x4d)
1931
1932   BFP(BLOCK_16X16, vp9_sad16x16, vp9_sad16x16_avg,
1933       vp9_variance16x16, vp9_sub_pixel_variance16x16,
1934       vp9_sub_pixel_avg_variance16x16, vp9_variance_halfpixvar16x16_h,
1935       vp9_variance_halfpixvar16x16_v,
1936       vp9_variance_halfpixvar16x16_hv, vp9_sad16x16x3, vp9_sad16x16x8,
1937       vp9_sad16x16x4d)
1938
1939   BFP(BLOCK_16X8, vp9_sad16x8, vp9_sad16x8_avg,
1940       vp9_variance16x8, vp9_sub_pixel_variance16x8,
1941       vp9_sub_pixel_avg_variance16x8, NULL, NULL, NULL,
1942       vp9_sad16x8x3, vp9_sad16x8x8, vp9_sad16x8x4d)
1943
1944   BFP(BLOCK_8X16, vp9_sad8x16, vp9_sad8x16_avg,
1945       vp9_variance8x16, vp9_sub_pixel_variance8x16,
1946       vp9_sub_pixel_avg_variance8x16, NULL, NULL, NULL,
1947       vp9_sad8x16x3, vp9_sad8x16x8, vp9_sad8x16x4d)
1948
1949   BFP(BLOCK_8X8, vp9_sad8x8, vp9_sad8x8_avg,
1950       vp9_variance8x8, vp9_sub_pixel_variance8x8,
1951       vp9_sub_pixel_avg_variance8x8, NULL, NULL, NULL,
1952       vp9_sad8x8x3, vp9_sad8x8x8, vp9_sad8x8x4d)
1953
1954   BFP(BLOCK_8X4, vp9_sad8x4, vp9_sad8x4_avg,
1955       vp9_variance8x4, vp9_sub_pixel_variance8x4,
1956       vp9_sub_pixel_avg_variance8x4, NULL, NULL,
1957       NULL, NULL, vp9_sad8x4x8,
1958       vp9_sad8x4x4d)
1959
1960   BFP(BLOCK_4X8, vp9_sad4x8, vp9_sad4x8_avg,
1961       vp9_variance4x8, vp9_sub_pixel_variance4x8,
1962       vp9_sub_pixel_avg_variance4x8, NULL, NULL,
1963       NULL, NULL, vp9_sad4x8x8,
1964       vp9_sad4x8x4d)
1965
1966   BFP(BLOCK_4X4, vp9_sad4x4, vp9_sad4x4_avg,
1967       vp9_variance4x4, vp9_sub_pixel_variance4x4,
1968       vp9_sub_pixel_avg_variance4x4, NULL, NULL, NULL,
1969       vp9_sad4x4x3, vp9_sad4x4x8, vp9_sad4x4x4d)
1970
1971   cpi->full_search_sad = vp9_full_search_sad;
1972   cpi->diamond_search_sad = vp9_diamond_search_sad;
1973   cpi->refining_search_sad = vp9_refining_search_sad;
1974
1975   /* vp9_init_quantizer() is first called here. Add check in
1976    * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
1977    * called later when needed. This will avoid unnecessary calls of
1978    * vp9_init_quantizer() for every frame.
1979    */
1980   vp9_init_quantizer(cpi);
1981
1982   vp9_loop_filter_init(cm);
1983
1984   cm->error.setjmp = 0;
1985
1986   vp9_zero(cpi->common.counts.uv_mode);
1987
1988 #ifdef MODE_TEST_HIT_STATS
1989   vp9_zero(cpi->mode_test_hits);
1990 #endif
1991
1992   return (VP9_PTR) cpi;
1993 }
1994
1995 void vp9_remove_compressor(VP9_PTR *ptr) {
1996   VP9_COMP *cpi = (VP9_COMP *)(*ptr);
1997   int i;
1998
1999   if (!cpi)
2000     return;
2001
2002   if (cpi && (cpi->common.current_video_frame > 0)) {
2003     if (cpi->pass == 2) {
2004       vp9_end_second_pass(cpi);
2005     }
2006
2007 #if CONFIG_INTERNAL_STATS
2008
2009     vp9_clear_system_state();
2010
2011     // printf("\n8x8-4x4:%d-%d\n", cpi->t8x8_count, cpi->t4x4_count);
2012     if (cpi->pass != 1) {
2013       FILE *f = fopen("opsnr.stt", "a");
2014       double time_encoded = (cpi->last_end_time_stamp_seen
2015                              - cpi->first_time_stamp_ever) / 10000000.000;
2016       double total_encode_time = (cpi->time_receive_data +
2017                                   cpi->time_compress_data)   / 1000.000;
2018       double dr = (double)cpi->bytes * (double) 8 / (double)1000
2019                   / time_encoded;
2020
2021       if (cpi->b_calculate_psnr) {
2022         const double total_psnr = vp9_mse2psnr(cpi->total_samples, 255.0,
2023                                                cpi->total_sq_error);
2024         const double totalp_psnr = vp9_mse2psnr(cpi->totalp_samples, 255.0,
2025                                                 cpi->totalp_sq_error);
2026         const double total_ssim = 100 * pow(cpi->summed_quality /
2027                                                 cpi->summed_weights, 8.0);
2028         const double totalp_ssim = 100 * pow(cpi->summedp_quality /
2029                                                 cpi->summedp_weights, 8.0);
2030
2031         fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
2032                 "VPXSSIM\tVPSSIMP\t  Time(ms)\n");
2033         fprintf(f, "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%8.0f\n",
2034                 dr, cpi->total / cpi->count, total_psnr,
2035                 cpi->totalp / cpi->count, totalp_psnr, total_ssim, totalp_ssim,
2036                 total_encode_time);
2037       }
2038
2039       if (cpi->b_calculate_ssimg) {
2040         fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t  Time(ms)\n");
2041         fprintf(f, "%7.2f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
2042                 cpi->total_ssimg_y / cpi->count,
2043                 cpi->total_ssimg_u / cpi->count,
2044                 cpi->total_ssimg_v / cpi->count,
2045                 cpi->total_ssimg_all / cpi->count, total_encode_time);
2046       }
2047
2048       fclose(f);
2049     }
2050
2051 #endif
2052
2053 #ifdef MODE_TEST_HIT_STATS
2054     if (cpi->pass != 1) {
2055       double norm_per_pixel_mode_tests = 0;
2056       double norm_counts[BLOCK_SIZES];
2057       int i;
2058       int sb64_per_frame;
2059       int norm_factors[BLOCK_SIZES] =
2060         {256, 128, 128, 64, 32, 32, 16, 8, 8, 4, 2, 2, 1};
2061       FILE *f = fopen("mode_hit_stats.stt", "a");
2062
2063       // On average, how many mode tests do we do
2064       for (i = 0; i < BLOCK_SIZES; ++i) {
2065         norm_counts[i] = (double)cpi->mode_test_hits[i] /
2066                          (double)norm_factors[i];
2067         norm_per_pixel_mode_tests += norm_counts[i];
2068       }
2069       // Convert to a number per 64x64 and per frame
2070       sb64_per_frame = ((cpi->common.height + 63) / 64) *
2071                        ((cpi->common.width + 63) / 64);
2072       norm_per_pixel_mode_tests =
2073         norm_per_pixel_mode_tests /
2074         (double)(cpi->common.current_video_frame * sb64_per_frame);
2075
2076       fprintf(f, "%6.4f\n", norm_per_pixel_mode_tests);
2077       fclose(f);
2078     }
2079 #endif
2080
2081 #if 0
2082     {
2083       printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2084       printf("\n_frames recive_data encod_mb_row compress_frame  Total\n");
2085       printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
2086              cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
2087              cpi->time_compress_data / 1000,
2088              (cpi->time_receive_data + cpi->time_compress_data) / 1000);
2089     }
2090 #endif
2091   }
2092
2093   free_pick_mode_context(&cpi->mb);
2094   dealloc_compressor_data(cpi);
2095   vpx_free(cpi->mb.ss);
2096   vpx_free(cpi->tok);
2097
2098   for (i = 0; i < sizeof(cpi->mbgraph_stats) /
2099                   sizeof(cpi->mbgraph_stats[0]); ++i) {
2100     vpx_free(cpi->mbgraph_stats[i].mb_stats);
2101   }
2102
2103   vp9_remove_common(&cpi->common);
2104   vpx_free(cpi);
2105   *ptr = 0;
2106
2107 #ifdef OUTPUT_YUV_SRC
2108   fclose(yuv_file);
2109 #endif
2110 #ifdef OUTPUT_YUV_REC
2111   fclose(yuv_rec_file);
2112 #endif
2113
2114 #if 0
2115
2116   if (keyfile)
2117     fclose(keyfile);
2118
2119   if (framepsnr)
2120     fclose(framepsnr);
2121
2122   if (kf_list)
2123     fclose(kf_list);
2124
2125 #endif
2126 }
2127
2128
2129 static uint64_t calc_plane_error(const uint8_t *orig, int orig_stride,
2130                                  const uint8_t *recon, int recon_stride,
2131                                  unsigned int cols, unsigned int rows) {
2132   unsigned int row, col;
2133   uint64_t total_sse = 0;
2134   int diff;
2135
2136   for (row = 0; row + 16 <= rows; row += 16) {
2137     for (col = 0; col + 16 <= cols; col += 16) {
2138       unsigned int sse;
2139
2140       vp9_mse16x16(orig + col, orig_stride, recon + col, recon_stride, &sse);
2141       total_sse += sse;
2142     }
2143
2144     /* Handle odd-sized width */
2145     if (col < cols) {
2146       unsigned int border_row, border_col;
2147       const uint8_t *border_orig = orig;
2148       const uint8_t *border_recon = recon;
2149
2150       for (border_row = 0; border_row < 16; border_row++) {
2151         for (border_col = col; border_col < cols; border_col++) {
2152           diff = border_orig[border_col] - border_recon[border_col];
2153           total_sse += diff * diff;
2154         }
2155
2156         border_orig += orig_stride;
2157         border_recon += recon_stride;
2158       }
2159     }
2160
2161     orig += orig_stride * 16;
2162     recon += recon_stride * 16;
2163   }
2164
2165   /* Handle odd-sized height */
2166   for (; row < rows; row++) {
2167     for (col = 0; col < cols; col++) {
2168       diff = orig[col] - recon[col];
2169       total_sse += diff * diff;
2170     }
2171
2172     orig += orig_stride;
2173     recon += recon_stride;
2174   }
2175
2176   return total_sse;
2177 }
2178
2179 typedef struct {
2180   double psnr[4];       // total/y/u/v
2181   uint64_t sse[4];      // total/y/u/v
2182   uint32_t samples[4];  // total/y/u/v
2183 } PSNR_STATS;
2184
2185 static void calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b,
2186                       PSNR_STATS *psnr) {
2187   const int widths[3]        = {a->y_width,  a->uv_width,  a->uv_width };
2188   const int heights[3]       = {a->y_height, a->uv_height, a->uv_height};
2189   const uint8_t *a_planes[3] = {a->y_buffer, a->u_buffer,  a->v_buffer };
2190   const int a_strides[3]     = {a->y_stride, a->uv_stride, a->uv_stride};
2191   const uint8_t *b_planes[3] = {b->y_buffer, b->u_buffer,  b->v_buffer };
2192   const int b_strides[3]     = {b->y_stride, b->uv_stride, b->uv_stride};
2193   int i;
2194   uint64_t total_sse = 0;
2195   uint32_t total_samples = 0;
2196
2197   for (i = 0; i < 3; ++i) {
2198     const int w = widths[i];
2199     const int h = heights[i];
2200     const uint32_t samples = w * h;
2201     const double sse = calc_plane_error(a_planes[i], a_strides[i],
2202                                         b_planes[i], b_strides[i],
2203                                         w, h);
2204     psnr->sse[1 + i] = sse;
2205     psnr->samples[1 + i] = samples;
2206     psnr->psnr[1 + i] = vp9_mse2psnr(samples, 255.0, sse);
2207
2208     total_sse += sse;
2209     total_samples += samples;
2210   }
2211
2212   psnr->sse[0] = total_sse;
2213   psnr->samples[0] = total_samples;
2214   psnr->psnr[0] = vp9_mse2psnr(total_samples, 255.0, total_sse);
2215 }
2216
2217 static void generate_psnr_packet(VP9_COMP *cpi) {
2218   struct vpx_codec_cx_pkt pkt;
2219   int i;
2220   PSNR_STATS psnr;
2221   calc_psnr(cpi->Source, cpi->common.frame_to_show, &psnr);
2222   for (i = 0; i < 4; ++i) {
2223     pkt.data.psnr.samples[i] = psnr.samples[i];
2224     pkt.data.psnr.sse[i] = psnr.sse[i];
2225     pkt.data.psnr.psnr[i] = psnr.psnr[i];
2226   }
2227   pkt.kind = VPX_CODEC_PSNR_PKT;
2228   vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2229 }
2230
2231 int vp9_use_as_reference(VP9_PTR ptr, int ref_frame_flags) {
2232   VP9_COMP *cpi = (VP9_COMP *)(ptr);
2233
2234   if (ref_frame_flags > 7)
2235     return -1;
2236
2237   cpi->ref_frame_flags = ref_frame_flags;
2238   return 0;
2239 }
2240
2241 int vp9_update_reference(VP9_PTR ptr, int ref_frame_flags) {
2242   VP9_COMP *cpi = (VP9_COMP *)(ptr);
2243
2244   if (ref_frame_flags > 7)
2245     return -1;
2246
2247   cpi->ext_refresh_golden_frame = 0;
2248   cpi->ext_refresh_alt_ref_frame = 0;
2249   cpi->ext_refresh_last_frame   = 0;
2250
2251   if (ref_frame_flags & VP9_LAST_FLAG)
2252     cpi->ext_refresh_last_frame = 1;
2253
2254   if (ref_frame_flags & VP9_GOLD_FLAG)
2255     cpi->ext_refresh_golden_frame = 1;
2256
2257   if (ref_frame_flags & VP9_ALT_FLAG)
2258     cpi->ext_refresh_alt_ref_frame = 1;
2259
2260   cpi->ext_refresh_frame_flags_pending = 1;
2261   return 0;
2262 }
2263
2264 static YV12_BUFFER_CONFIG *get_vp9_ref_frame_buffer(VP9_COMP *cpi,
2265                                 VP9_REFFRAME ref_frame_flag) {
2266   MV_REFERENCE_FRAME ref_frame = NONE;
2267   if (ref_frame_flag == VP9_LAST_FLAG)
2268     ref_frame = LAST_FRAME;
2269   else if (ref_frame_flag == VP9_GOLD_FLAG)
2270     ref_frame = GOLDEN_FRAME;
2271   else if (ref_frame_flag == VP9_ALT_FLAG)
2272     ref_frame = ALTREF_FRAME;
2273
2274   return ref_frame == NONE ? NULL : get_ref_frame_buffer(cpi, ref_frame);
2275 }
2276
2277 int vp9_copy_reference_enc(VP9_PTR ptr, VP9_REFFRAME ref_frame_flag,
2278                            YV12_BUFFER_CONFIG *sd) {
2279   VP9_COMP *const cpi = (VP9_COMP *)ptr;
2280   YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
2281   if (cfg) {
2282     vp8_yv12_copy_frame(cfg, sd);
2283     return 0;
2284   } else {
2285     return -1;
2286   }
2287 }
2288
2289 int vp9_get_reference_enc(VP9_PTR ptr, int index, YV12_BUFFER_CONFIG **fb) {
2290   VP9_COMP *cpi = (VP9_COMP *)ptr;
2291   VP9_COMMON *cm = &cpi->common;
2292
2293   if (index < 0 || index >= REF_FRAMES)
2294     return -1;
2295
2296   *fb = &cm->frame_bufs[cm->ref_frame_map[index]].buf;
2297   return 0;
2298 }
2299
2300 int vp9_set_reference_enc(VP9_PTR ptr, VP9_REFFRAME ref_frame_flag,
2301                           YV12_BUFFER_CONFIG *sd) {
2302   VP9_COMP *cpi = (VP9_COMP *)ptr;
2303   YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
2304   if (cfg) {
2305     vp8_yv12_copy_frame(sd, cfg);
2306     return 0;
2307   } else {
2308     return -1;
2309   }
2310 }
2311
2312 int vp9_update_entropy(VP9_PTR comp, int update) {
2313   ((VP9_COMP *)comp)->ext_refresh_frame_context = update;
2314   ((VP9_COMP *)comp)->ext_refresh_frame_context_pending = 1;
2315   return 0;
2316 }
2317
2318
2319 #ifdef OUTPUT_YUV_SRC
2320 void vp9_write_yuv_frame(YV12_BUFFER_CONFIG *s) {
2321   uint8_t *src = s->y_buffer;
2322   int h = s->y_height;
2323
2324   do {
2325     fwrite(src, s->y_width, 1,  yuv_file);
2326     src += s->y_stride;
2327   } while (--h);
2328
2329   src = s->u_buffer;
2330   h = s->uv_height;
2331
2332   do {
2333     fwrite(src, s->uv_width, 1,  yuv_file);
2334     src += s->uv_stride;
2335   } while (--h);
2336
2337   src = s->v_buffer;
2338   h = s->uv_height;
2339
2340   do {
2341     fwrite(src, s->uv_width, 1, yuv_file);
2342     src += s->uv_stride;
2343   } while (--h);
2344 }
2345 #endif
2346
2347 #ifdef OUTPUT_YUV_REC
2348 void vp9_write_yuv_rec_frame(VP9_COMMON *cm) {
2349   YV12_BUFFER_CONFIG *s = cm->frame_to_show;
2350   uint8_t *src = s->y_buffer;
2351   int h = cm->height;
2352
2353   do {
2354     fwrite(src, s->y_width, 1,  yuv_rec_file);
2355     src += s->y_stride;
2356   } while (--h);
2357
2358   src = s->u_buffer;
2359   h = s->uv_height;
2360
2361   do {
2362     fwrite(src, s->uv_width, 1,  yuv_rec_file);
2363     src += s->uv_stride;
2364   } while (--h);
2365
2366   src = s->v_buffer;
2367   h = s->uv_height;
2368
2369   do {
2370     fwrite(src, s->uv_width, 1, yuv_rec_file);
2371     src += s->uv_stride;
2372   } while (--h);
2373
2374 #if CONFIG_ALPHA
2375   if (s->alpha_buffer) {
2376     src = s->alpha_buffer;
2377     h = s->alpha_height;
2378     do {
2379       fwrite(src, s->alpha_width, 1,  yuv_rec_file);
2380       src += s->alpha_stride;
2381     } while (--h);
2382   }
2383 #endif
2384
2385   fflush(yuv_rec_file);
2386 }
2387 #endif
2388
2389 static void scale_and_extend_frame_nonnormative(YV12_BUFFER_CONFIG *src_fb,
2390                                                 YV12_BUFFER_CONFIG *dst_fb) {
2391   const int in_w = src_fb->y_crop_width;
2392   const int in_h = src_fb->y_crop_height;
2393   const int out_w = dst_fb->y_crop_width;
2394   const int out_h = dst_fb->y_crop_height;
2395   const int in_w_uv = src_fb->uv_crop_width;
2396   const int in_h_uv = src_fb->uv_crop_height;
2397   const int out_w_uv = dst_fb->uv_crop_width;
2398   const int out_h_uv = dst_fb->uv_crop_height;
2399   int i;
2400
2401   uint8_t *srcs[4] = {src_fb->y_buffer, src_fb->u_buffer, src_fb->v_buffer,
2402     src_fb->alpha_buffer};
2403   int src_strides[4] = {src_fb->y_stride, src_fb->uv_stride, src_fb->uv_stride,
2404     src_fb->alpha_stride};
2405
2406   uint8_t *dsts[4] = {dst_fb->y_buffer, dst_fb->u_buffer, dst_fb->v_buffer,
2407     dst_fb->alpha_buffer};
2408   int dst_strides[4] = {dst_fb->y_stride, dst_fb->uv_stride, dst_fb->uv_stride,
2409     dst_fb->alpha_stride};
2410
2411   for (i = 0; i < MAX_MB_PLANE; ++i) {
2412     if (i == 0 || i == 3) {
2413       // Y and alpha planes
2414       vp9_resize_plane(srcs[i], in_h, in_w, src_strides[i],
2415                        dsts[i], out_h, out_w, dst_strides[i]);
2416     } else {
2417       // Chroma planes
2418       vp9_resize_plane(srcs[i], in_h_uv, in_w_uv, src_strides[i],
2419                        dsts[i], out_h_uv, out_w_uv, dst_strides[i]);
2420     }
2421   }
2422   vp8_yv12_extend_frame_borders(dst_fb);
2423 }
2424
2425 static void scale_and_extend_frame(YV12_BUFFER_CONFIG *src_fb,
2426                                    YV12_BUFFER_CONFIG *dst_fb) {
2427   const int in_w = src_fb->y_crop_width;
2428   const int in_h = src_fb->y_crop_height;
2429   const int out_w = dst_fb->y_crop_width;
2430   const int out_h = dst_fb->y_crop_height;
2431   int x, y, i;
2432
2433   uint8_t *srcs[4] = {src_fb->y_buffer, src_fb->u_buffer, src_fb->v_buffer,
2434                       src_fb->alpha_buffer};
2435   int src_strides[4] = {src_fb->y_stride, src_fb->uv_stride, src_fb->uv_stride,
2436                         src_fb->alpha_stride};
2437
2438   uint8_t *dsts[4] = {dst_fb->y_buffer, dst_fb->u_buffer, dst_fb->v_buffer,
2439                       dst_fb->alpha_buffer};
2440   int dst_strides[4] = {dst_fb->y_stride, dst_fb->uv_stride, dst_fb->uv_stride,
2441                         dst_fb->alpha_stride};
2442
2443   for (y = 0; y < out_h; y += 16) {
2444     for (x = 0; x < out_w; x += 16) {
2445       for (i = 0; i < MAX_MB_PLANE; ++i) {
2446         const int factor = (i == 0 || i == 3 ? 1 : 2);
2447         const int x_q4 = x * (16 / factor) * in_w / out_w;
2448         const int y_q4 = y * (16 / factor) * in_h / out_h;
2449         const int src_stride = src_strides[i];
2450         const int dst_stride = dst_strides[i];
2451         uint8_t *src = srcs[i] + y / factor * in_h / out_h * src_stride +
2452                                  x / factor * in_w / out_w;
2453         uint8_t *dst = dsts[i] + y / factor * dst_stride + x / factor;
2454
2455         vp9_convolve8(src, src_stride, dst, dst_stride,
2456                       vp9_sub_pel_filters_8[x_q4 & 0xf], 16 * in_w / out_w,
2457                       vp9_sub_pel_filters_8[y_q4 & 0xf], 16 * in_h / out_h,
2458                       16 / factor, 16 / factor);
2459       }
2460     }
2461   }
2462
2463   vp8_yv12_extend_frame_borders(dst_fb);
2464 }
2465
2466 static int find_fp_qindex() {
2467   int i;
2468
2469   for (i = 0; i < QINDEX_RANGE; i++) {
2470     if (vp9_convert_qindex_to_q(i) >= 30.0) {
2471       break;
2472     }
2473   }
2474
2475   if (i == QINDEX_RANGE)
2476     i--;
2477
2478   return i;
2479 }
2480
2481 #define WRITE_RECON_BUFFER 0
2482 #if WRITE_RECON_BUFFER
2483 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame) {
2484   FILE *yframe;
2485   int i;
2486   char filename[255];
2487
2488   snprintf(filename, sizeof(filename), "cx\\y%04d.raw", this_frame);
2489   yframe = fopen(filename, "wb");
2490
2491   for (i = 0; i < frame->y_height; i++)
2492     fwrite(frame->y_buffer + i * frame->y_stride,
2493            frame->y_width, 1, yframe);
2494
2495   fclose(yframe);
2496   snprintf(filename, sizeof(filename), "cx\\u%04d.raw", this_frame);
2497   yframe = fopen(filename, "wb");
2498
2499   for (i = 0; i < frame->uv_height; i++)
2500     fwrite(frame->u_buffer + i * frame->uv_stride,
2501            frame->uv_width, 1, yframe);
2502
2503   fclose(yframe);
2504   snprintf(filename, sizeof(filename), "cx\\v%04d.raw", this_frame);
2505   yframe = fopen(filename, "wb");
2506
2507   for (i = 0; i < frame->uv_height; i++)
2508     fwrite(frame->v_buffer + i * frame->uv_stride,
2509            frame->uv_width, 1, yframe);
2510
2511   fclose(yframe);
2512 }
2513 #endif
2514
2515 static double compute_edge_pixel_proportion(YV12_BUFFER_CONFIG *frame) {
2516 #define EDGE_THRESH 128
2517   int i, j;
2518   int num_edge_pels = 0;
2519   int num_pels = (frame->y_height - 2) * (frame->y_width - 2);
2520   uint8_t *prev = frame->y_buffer + 1;
2521   uint8_t *curr = frame->y_buffer + 1 + frame->y_stride;
2522   uint8_t *next = frame->y_buffer + 1 + 2 * frame->y_stride;
2523   for (i = 1; i < frame->y_height - 1; i++) {
2524     for (j = 1; j < frame->y_width - 1; j++) {
2525       /* Sobel hor and ver gradients */
2526       int v = 2 * (curr[1] - curr[-1]) + (prev[1] - prev[-1]) +
2527               (next[1] - next[-1]);
2528       int h = 2 * (prev[0] - next[0]) + (prev[1] - next[1]) +
2529               (prev[-1] - next[-1]);
2530       h = (h < 0 ? -h : h);
2531       v = (v < 0 ? -v : v);
2532       if (h > EDGE_THRESH || v > EDGE_THRESH)
2533         num_edge_pels++;
2534       curr++;
2535       prev++;
2536       next++;
2537     }
2538     curr += frame->y_stride - frame->y_width + 2;
2539     prev += frame->y_stride - frame->y_width + 2;
2540     next += frame->y_stride - frame->y_width + 2;
2541   }
2542   return (double)num_edge_pels / num_pels;
2543 }
2544
2545 // Function to test for conditions that indicate we should loop
2546 // back and recode a frame.
2547 static int recode_loop_test(const VP9_COMP *cpi,
2548                             int high_limit, int low_limit,
2549                             int q, int maxq, int minq) {
2550   const VP9_COMMON *const cm = &cpi->common;
2551   const RATE_CONTROL *const rc = &cpi->rc;
2552   int force_recode = 0;
2553
2554   // Special case trap if maximum allowed frame size exceeded.
2555   if (rc->projected_frame_size > rc->max_frame_bandwidth) {
2556     force_recode = 1;
2557
2558   // Is frame recode allowed.
2559   // Yes if either recode mode 1 is selected or mode 2 is selected
2560   // and the frame is a key frame, golden frame or alt_ref_frame
2561   } else if ((cpi->sf.recode_loop == ALLOW_RECODE) ||
2562              ((cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF) &&
2563               (cm->frame_type == KEY_FRAME ||
2564                cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
2565     // General over and under shoot tests
2566     if ((rc->projected_frame_size > high_limit && q < maxq) ||
2567         (rc->projected_frame_size < low_limit && q > minq)) {
2568       force_recode = 1;
2569     } else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
2570       // Deal with frame undershoot and whether or not we are
2571       // below the automatically set cq level.
2572       if (q > cpi->cq_target_quality &&
2573           rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
2574         force_recode = 1;
2575       }
2576     }
2577   }
2578   return force_recode;
2579 }
2580
2581 static void update_reference_frames(VP9_COMP * const cpi) {
2582   VP9_COMMON * const cm = &cpi->common;
2583
2584   // At this point the new frame has been encoded.
2585   // If any buffer copy / swapping is signaled it should be done here.
2586   if (cm->frame_type == KEY_FRAME) {
2587     ref_cnt_fb(cm->frame_bufs,
2588                &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
2589     ref_cnt_fb(cm->frame_bufs,
2590                &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
2591   }
2592 #if CONFIG_MULTIPLE_ARF
2593   else if (!cpi->multi_arf_enabled && cpi->refresh_golden_frame &&
2594       !cpi->refresh_alt_ref_frame) {
2595 #else
2596   else if (cpi->refresh_golden_frame && !cpi->refresh_alt_ref_frame &&
2597            !cpi->use_svc) {
2598 #endif
2599     /* Preserve the previously existing golden frame and update the frame in
2600      * the alt ref slot instead. This is highly specific to the current use of
2601      * alt-ref as a forward reference, and this needs to be generalized as
2602      * other uses are implemented (like RTC/temporal scaling)
2603      *
2604      * The update to the buffer in the alt ref slot was signaled in
2605      * vp9_pack_bitstream(), now swap the buffer pointers so that it's treated
2606      * as the golden frame next time.
2607      */
2608     int tmp;
2609
2610     ref_cnt_fb(cm->frame_bufs,
2611                &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
2612
2613     tmp = cpi->alt_fb_idx;
2614     cpi->alt_fb_idx = cpi->gld_fb_idx;
2615     cpi->gld_fb_idx = tmp;
2616   }  else { /* For non key/golden frames */
2617     if (cpi->refresh_alt_ref_frame) {
2618       int arf_idx = cpi->alt_fb_idx;
2619 #if CONFIG_MULTIPLE_ARF
2620       if (cpi->multi_arf_enabled) {
2621         arf_idx = cpi->arf_buffer_idx[cpi->sequence_number + 1];
2622       }
2623 #endif
2624       ref_cnt_fb(cm->frame_bufs,
2625                  &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
2626     }
2627
2628     if (cpi->refresh_golden_frame) {
2629       ref_cnt_fb(cm->frame_bufs,
2630                  &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
2631     }
2632   }
2633
2634   if (cpi->refresh_last_frame) {
2635     ref_cnt_fb(cm->frame_bufs,
2636                &cm->ref_frame_map[cpi->lst_fb_idx], cm->new_fb_idx);
2637   }
2638 }
2639
2640 static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) {
2641   MACROBLOCKD *xd = &cpi->mb.e_mbd;
2642   struct loopfilter *lf = &cm->lf;
2643   if (xd->lossless) {
2644       lf->filter_level = 0;
2645   } else {
2646     struct vpx_usec_timer timer;
2647
2648     vp9_clear_system_state();
2649
2650     vpx_usec_timer_start(&timer);
2651
2652     vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.use_fast_lpf_pick);
2653
2654     vpx_usec_timer_mark(&timer);
2655     cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
2656   }
2657
2658   if (lf->filter_level > 0) {
2659     vp9_set_alt_lf_level(cpi, lf->filter_level);
2660     vp9_loop_filter_frame(cm, xd, lf->filter_level, 0, 0);
2661   }
2662
2663   vp9_extend_frame_inner_borders(cm->frame_to_show,
2664                                  cm->subsampling_x, cm->subsampling_y);
2665 }
2666
2667 static void scale_references(VP9_COMP *cpi) {
2668   VP9_COMMON *cm = &cpi->common;
2669   MV_REFERENCE_FRAME ref_frame;
2670
2671   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2672     const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)];
2673     YV12_BUFFER_CONFIG *const ref = &cm->frame_bufs[idx].buf;
2674
2675     if (ref->y_crop_width != cm->width ||
2676         ref->y_crop_height != cm->height) {
2677       const int new_fb = get_free_fb(cm);
2678       vp9_realloc_frame_buffer(&cm->frame_bufs[new_fb].buf,
2679                                cm->width, cm->height,
2680                                cm->subsampling_x, cm->subsampling_y,
2681                                VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL);
2682       scale_and_extend_frame(ref, &cm->frame_bufs[new_fb].buf);
2683       cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
2684     } else {
2685       cpi->scaled_ref_idx[ref_frame - 1] = idx;
2686       cm->frame_bufs[idx].ref_count++;
2687     }
2688   }
2689 }
2690
2691 static void release_scaled_references(VP9_COMP *cpi) {
2692   VP9_COMMON *cm = &cpi->common;
2693   int i;
2694
2695   for (i = 0; i < 3; i++)
2696     cm->frame_bufs[cpi->scaled_ref_idx[i]].ref_count--;
2697 }
2698
2699 static void full_to_model_count(unsigned int *model_count,
2700                                 unsigned int *full_count) {
2701   int n;
2702   model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
2703   model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
2704   model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
2705   for (n = THREE_TOKEN; n < EOB_TOKEN; ++n)
2706     model_count[TWO_TOKEN] += full_count[n];
2707   model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
2708 }
2709
2710 static void full_to_model_counts(vp9_coeff_count_model *model_count,
2711                                  vp9_coeff_count *full_count) {
2712   int i, j, k, l;
2713
2714   for (i = 0; i < PLANE_TYPES; ++i)
2715     for (j = 0; j < REF_TYPES; ++j)
2716       for (k = 0; k < COEF_BANDS; ++k)
2717         for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
2718           full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
2719 }
2720
2721 #if 0 && CONFIG_INTERNAL_STATS
2722 static void output_frame_level_debug_stats(VP9_COMP *cpi) {
2723   VP9_COMMON *const cm = &cpi->common;
2724   FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
2725   int recon_err;
2726
2727   vp9_clear_system_state();  // __asm emms;
2728
2729   recon_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm));
2730
2731   if (cpi->twopass.total_left_stats.coded_error != 0.0)
2732     fprintf(f, "%10u %10d %10d %10d %10d %10d "
2733         "%10"PRId64" %10"PRId64" %10d "
2734         "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
2735         "%6d %6d %5d %5d %5d "
2736         "%10"PRId64" %10.3lf"
2737         "%10lf %8u %10d %10d %10d\n",
2738         cpi->common.current_video_frame, cpi->rc.this_frame_target,
2739         cpi->rc.projected_frame_size,
2740         cpi->rc.projected_frame_size / cpi->common.MBs,
2741         (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
2742         cpi->rc.total_target_vs_actual,
2743         (cpi->oxcf.starting_buffer_level - cpi->rc.bits_off_target),
2744         cpi->rc.total_actual_bits, cm->base_qindex,
2745         vp9_convert_qindex_to_q(cm->base_qindex),
2746         (double)vp9_dc_quant(cm->base_qindex, 0) / 4.0,
2747         cpi->rc.avg_q,
2748         vp9_convert_qindex_to_q(cpi->rc.ni_av_qi),
2749         vp9_convert_qindex_to_q(cpi->cq_target_quality),
2750         cpi->refresh_last_frame, cpi->refresh_golden_frame,
2751         cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
2752         cpi->twopass.bits_left,
2753         cpi->twopass.total_left_stats.coded_error,
2754         cpi->twopass.bits_left /
2755             (1 + cpi->twopass.total_left_stats.coded_error),
2756         cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
2757         cpi->twopass.kf_zeromotion_pct);
2758
2759   fclose(f);
2760
2761   if (0) {
2762     FILE *const fmodes = fopen("Modes.stt", "a");
2763     int i;
2764
2765     fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
2766             cm->frame_type, cpi->refresh_golden_frame,
2767             cpi->refresh_alt_ref_frame);
2768
2769     for (i = 0; i < MAX_MODES; ++i)
2770       fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
2771     for (i = 0; i < MAX_REFS; ++i)
2772       fprintf(fmodes, "%5d ", cpi->sub8x8_mode_chosen_counts[i]);
2773
2774     fprintf(fmodes, "\n");
2775
2776     fclose(fmodes);
2777   }
2778 }
2779 #endif
2780
2781 static void encode_without_recode_loop(VP9_COMP *cpi,
2782                                        size_t *size,
2783                                        uint8_t *dest,
2784                                        int q) {
2785   VP9_COMMON *const cm = &cpi->common;
2786   vp9_clear_system_state();  // __asm emms;
2787   vp9_set_quantizer(cpi, q);
2788
2789   // Set up entropy context depending on frame type. The decoder mandates
2790   // the use of the default context, index 0, for keyframes and inter
2791   // frames where the error_resilient_mode or intra_only flag is set. For
2792   // other inter-frames the encoder currently uses only two contexts;
2793   // context 1 for ALTREF frames and context 0 for the others.
2794   if (cm->frame_type == KEY_FRAME) {
2795     vp9_setup_key_frame(cpi);
2796   } else {
2797     if (!cm->intra_only && !cm->error_resilient_mode) {
2798       cpi->common.frame_context_idx = cpi->refresh_alt_ref_frame;
2799     }
2800     vp9_setup_inter_frame(cpi);
2801   }
2802   // Variance adaptive and in frame q adjustment experiments are mutually
2803   // exclusive.
2804   if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
2805     vp9_vaq_frame_setup(cpi);
2806   } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
2807     setup_in_frame_q_adj(cpi);
2808   }
2809   // transform / motion compensation build reconstruction frame
2810   vp9_encode_frame(cpi);
2811
2812   // Update the skip mb flag probabilities based on the distribution
2813   // seen in the last encoder iteration.
2814   // update_base_skip_probs(cpi);
2815   vp9_clear_system_state();  // __asm emms;
2816 }
2817
2818 static void encode_with_recode_loop(VP9_COMP *cpi,
2819                                     size_t *size,
2820                                     uint8_t *dest,
2821                                     int q,
2822                                     int bottom_index,
2823                                     int top_index) {
2824   VP9_COMMON *const cm = &cpi->common;
2825   int loop_count = 0;
2826   int loop = 0;
2827   int overshoot_seen = 0;
2828   int undershoot_seen = 0;
2829   int q_low = bottom_index, q_high = top_index;
2830   int frame_over_shoot_limit;
2831   int frame_under_shoot_limit;
2832
2833   // Decide frame size bounds
2834   vp9_rc_compute_frame_size_bounds(cpi, cpi->rc.this_frame_target,
2835                                    &frame_under_shoot_limit,
2836                                    &frame_over_shoot_limit);
2837
2838   do {
2839     vp9_clear_system_state();  // __asm emms;
2840
2841     vp9_set_quantizer(cpi, q);
2842
2843     if (loop_count == 0) {
2844       // Set up entropy context depending on frame type. The decoder mandates
2845       // the use of the default context, index 0, for keyframes and inter
2846       // frames where the error_resilient_mode or intra_only flag is set. For
2847       // other inter-frames the encoder currently uses only two contexts;
2848       // context 1 for ALTREF frames and context 0 for the others.
2849       if (cm->frame_type == KEY_FRAME) {
2850         vp9_setup_key_frame(cpi);
2851       } else {
2852         if (!cm->intra_only && !cm->error_resilient_mode) {
2853           cpi->common.frame_context_idx = cpi->refresh_alt_ref_frame;
2854         }
2855         vp9_setup_inter_frame(cpi);
2856       }
2857     }
2858
2859     // Variance adaptive and in frame q adjustment experiments are mutually
2860     // exclusive.
2861     if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
2862       vp9_vaq_frame_setup(cpi);
2863     } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
2864       setup_in_frame_q_adj(cpi);
2865     }
2866
2867     // transform / motion compensation build reconstruction frame
2868     vp9_encode_frame(cpi);
2869
2870     // Update the skip mb flag probabilities based on the distribution
2871     // seen in the last encoder iteration.
2872     // update_base_skip_probs(cpi);
2873
2874     vp9_clear_system_state();  // __asm emms;
2875
2876     // Dummy pack of the bitstream using up to date stats to get an
2877     // accurate estimate of output frame size to determine if we need
2878     // to recode.
2879     if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
2880       vp9_save_coding_context(cpi);
2881       cpi->dummy_packing = 1;
2882       if (!cpi->sf.use_pick_mode)
2883         vp9_pack_bitstream(cpi, dest, size);
2884
2885       cpi->rc.projected_frame_size = (*size) << 3;
2886       vp9_restore_coding_context(cpi);
2887
2888       if (frame_over_shoot_limit == 0)
2889         frame_over_shoot_limit = 1;
2890     }
2891
2892     if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) {
2893       loop = 0;
2894     } else {
2895       if ((cm->frame_type == KEY_FRAME) &&
2896            cpi->rc.this_key_frame_forced &&
2897            (cpi->rc.projected_frame_size < cpi->rc.max_frame_bandwidth)) {
2898         int last_q = q;
2899         int kf_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm));
2900
2901         int high_err_target = cpi->ambient_err;
2902         int low_err_target = cpi->ambient_err >> 1;
2903
2904         // Prevent possible divide by zero error below for perfect KF
2905         kf_err += !kf_err;
2906
2907         // The key frame is not good enough or we can afford
2908         // to make it better without undue risk of popping.
2909         if ((kf_err > high_err_target &&
2910              cpi->rc.projected_frame_size <= frame_over_shoot_limit) ||
2911             (kf_err > low_err_target &&
2912              cpi->rc.projected_frame_size <= frame_under_shoot_limit)) {
2913           // Lower q_high
2914           q_high = q > q_low ? q - 1 : q_low;
2915
2916           // Adjust Q
2917           q = (q * high_err_target) / kf_err;
2918           q = MIN(q, (q_high + q_low) >> 1);
2919         } else if (kf_err < low_err_target &&
2920                    cpi->rc.projected_frame_size >= frame_under_shoot_limit) {
2921           // The key frame is much better than the previous frame
2922           // Raise q_low
2923           q_low = q < q_high ? q + 1 : q_high;
2924
2925           // Adjust Q
2926           q = (q * low_err_target) / kf_err;
2927           q = MIN(q, (q_high + q_low + 1) >> 1);
2928         }
2929
2930         // Clamp Q to upper and lower limits:
2931         q = clamp(q, q_low, q_high);
2932
2933         loop = q != last_q;
2934       } else if (recode_loop_test(
2935           cpi, frame_over_shoot_limit, frame_under_shoot_limit,
2936           q, MAX(q_high, top_index), bottom_index)) {
2937         // Is the projected frame size out of range and are we allowed
2938         // to attempt to recode.
2939         int last_q = q;
2940         int retries = 0;
2941
2942         // Frame size out of permitted range:
2943         // Update correction factor & compute new Q to try...
2944
2945         // Frame is too large
2946         if (cpi->rc.projected_frame_size > cpi->rc.this_frame_target) {
2947           // Special case if the projected size is > the max allowed.
2948           if (cpi->rc.projected_frame_size >= cpi->rc.max_frame_bandwidth)
2949             q_high = cpi->rc.worst_quality;
2950
2951           // Raise Qlow as to at least the current value
2952           q_low = q < q_high ? q + 1 : q_high;
2953
2954           if (undershoot_seen || loop_count > 1) {
2955             // Update rate_correction_factor unless
2956             vp9_rc_update_rate_correction_factors(cpi, 1);
2957
2958             q = (q_high + q_low + 1) / 2;
2959           } else {
2960             // Update rate_correction_factor unless
2961             vp9_rc_update_rate_correction_factors(cpi, 0);
2962
2963             q = vp9_rc_regulate_q(cpi, cpi->rc.this_frame_target,
2964                                    bottom_index, MAX(q_high, top_index));
2965
2966             while (q < q_low && retries < 10) {
2967               vp9_rc_update_rate_correction_factors(cpi, 0);
2968               q = vp9_rc_regulate_q(cpi, cpi->rc.this_frame_target,
2969                                      bottom_index, MAX(q_high, top_index));
2970               retries++;
2971             }
2972           }
2973
2974           overshoot_seen = 1;
2975         } else {
2976           // Frame is too small
2977           q_high = q > q_low ? q - 1 : q_low;
2978
2979           if (overshoot_seen || loop_count > 1) {
2980             vp9_rc_update_rate_correction_factors(cpi, 1);
2981             q = (q_high + q_low) / 2;
2982           } else {
2983             vp9_rc_update_rate_correction_factors(cpi, 0);
2984             q = vp9_rc_regulate_q(cpi, cpi->rc.this_frame_target,
2985                                    bottom_index, top_index);
2986             // Special case reset for qlow for constrained quality.
2987             // This should only trigger where there is very substantial
2988             // undershoot on a frame and the auto cq level is above
2989             // the user passsed in value.
2990             if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY &&
2991                 q < q_low) {
2992               q_low = q;
2993             }
2994
2995             while (q > q_high && retries < 10) {
2996               vp9_rc_update_rate_correction_factors(cpi, 0);
2997               q = vp9_rc_regulate_q(cpi, cpi->rc.this_frame_target,
2998                                      bottom_index, top_index);
2999               retries++;
3000             }
3001           }
3002
3003           undershoot_seen = 1;
3004         }
3005
3006         // Clamp Q to upper and lower limits:
3007         q = clamp(q, q_low, q_high);
3008
3009         loop = q != last_q;
3010       } else {
3011         loop = 0;
3012       }
3013     }
3014
3015     // Special case for overlay frame.
3016     if (cpi->rc.is_src_frame_alt_ref &&
3017         (cpi->rc.projected_frame_size < cpi->rc.max_frame_bandwidth))
3018       loop = 0;
3019
3020     if (loop) {
3021       loop_count++;
3022
3023 #if CONFIG_INTERNAL_STATS
3024       cpi->tot_recode_hits++;
3025 #endif
3026     }
3027   } while (loop);
3028 }
3029
3030 static void get_ref_frame_flags(VP9_COMP *cpi) {
3031   if (cpi->refresh_last_frame & cpi->refresh_golden_frame)
3032     cpi->gold_is_last = 1;
3033   else if (cpi->refresh_last_frame ^ cpi->refresh_golden_frame)
3034     cpi->gold_is_last = 0;
3035
3036   if (cpi->refresh_last_frame & cpi->refresh_alt_ref_frame)
3037     cpi->alt_is_last = 1;
3038   else if (cpi->refresh_last_frame ^ cpi->refresh_alt_ref_frame)
3039     cpi->alt_is_last = 0;
3040
3041   if (cpi->refresh_alt_ref_frame & cpi->refresh_golden_frame)
3042     cpi->gold_is_alt = 1;
3043   else if (cpi->refresh_alt_ref_frame ^ cpi->refresh_golden_frame)
3044     cpi->gold_is_alt = 0;
3045
3046   cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
3047
3048   if (cpi->gold_is_last)
3049     cpi->ref_frame_flags &= ~VP9_GOLD_FLAG;
3050
3051   if (cpi->alt_is_last)
3052     cpi->ref_frame_flags &= ~VP9_ALT_FLAG;
3053
3054   if (cpi->gold_is_alt)
3055     cpi->ref_frame_flags &= ~VP9_ALT_FLAG;
3056 }
3057
3058 static void set_ext_overrides(VP9_COMP *cpi) {
3059   // Overrides the defaults with the externally supplied values with
3060   // vp9_update_reference() and vp9_update_entropy() calls
3061   // Note: The overrides are valid only for the next frame passed
3062   // to encode_frame_to_data_rate() function
3063   if (cpi->ext_refresh_frame_context_pending) {
3064     cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
3065     cpi->ext_refresh_frame_context_pending = 0;
3066   }
3067   if (cpi->ext_refresh_frame_flags_pending) {
3068     cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
3069     cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
3070     cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
3071     cpi->ext_refresh_frame_flags_pending = 0;
3072   }
3073 }
3074
3075 static void encode_frame_to_data_rate(VP9_COMP *cpi,
3076                                       size_t *size,
3077                                       uint8_t *dest,
3078                                       unsigned int *frame_flags) {
3079   VP9_COMMON *const cm = &cpi->common;
3080   TX_SIZE t;
3081   int q;
3082   int top_index;
3083   int bottom_index;
3084
3085   SPEED_FEATURES *const sf = &cpi->sf;
3086   unsigned int max_mv_def = MIN(cm->width, cm->height);
3087   struct segmentation *const seg = &cm->seg;
3088
3089   set_ext_overrides(cpi);
3090
3091   /* Scale the source buffer, if required. */
3092   if (cm->mi_cols * MI_SIZE != cpi->un_scaled_source->y_width ||
3093       cm->mi_rows * MI_SIZE != cpi->un_scaled_source->y_height) {
3094     scale_and_extend_frame_nonnormative(cpi->un_scaled_source,
3095                                         &cpi->scaled_source);
3096     cpi->Source = &cpi->scaled_source;
3097   } else {
3098     cpi->Source = cpi->un_scaled_source;
3099   }
3100   scale_references(cpi);
3101
3102   vp9_clear_system_state();
3103
3104   // Enable or disable mode based tweaking of the zbin.
3105   // For 2 pass only used where GF/ARF prediction quality
3106   // is above a threshold.
3107   cpi->zbin_mode_boost = 0;
3108   cpi->zbin_mode_boost_enabled = 0;
3109
3110   // Current default encoder behavior for the altref sign bias.
3111   cm->ref_frame_sign_bias[ALTREF_FRAME] = cpi->rc.source_alt_ref_active;
3112
3113   // Set default state for segment based loop filter update flags.
3114   cm->lf.mode_ref_delta_update = 0;
3115
3116   // Initialize cpi->mv_step_param to default based on max resolution.
3117   cpi->mv_step_param = vp9_init_search_range(cpi, max_mv_def);
3118   // Initialize cpi->max_mv_magnitude and cpi->mv_step_param if appropriate.
3119   if (sf->auto_mv_step_size) {
3120     if (frame_is_intra_only(cm)) {
3121       // Initialize max_mv_magnitude for use in the first INTER frame
3122       // after a key/intra-only frame.
3123       cpi->max_mv_magnitude = max_mv_def;
3124     } else {
3125       if (cm->show_frame)
3126         // Allow mv_steps to correspond to twice the max mv magnitude found
3127         // in the previous frame, capped by the default max_mv_magnitude based
3128         // on resolution.
3129         cpi->mv_step_param = vp9_init_search_range(cpi, MIN(max_mv_def, 2 *
3130                                  cpi->max_mv_magnitude));
3131       cpi->max_mv_magnitude = 0;
3132     }
3133   }
3134
3135   // Set various flags etc to special state if it is a key frame.
3136   if (frame_is_intra_only(cm)) {
3137     vp9_setup_key_frame(cpi);
3138     // Reset the loop filter deltas and segmentation map.
3139     vp9_reset_segment_features(&cm->seg);
3140
3141     // If segmentation is enabled force a map update for key frames.
3142     if (seg->enabled) {
3143       seg->update_map = 1;
3144       seg->update_data = 1;
3145     }
3146
3147     // The alternate reference frame cannot be active for a key frame.
3148     cpi->rc.source_alt_ref_active = 0;
3149
3150     cm->error_resilient_mode = (cpi->oxcf.error_resilient_mode != 0);
3151     cm->frame_parallel_decoding_mode =
3152       (cpi->oxcf.frame_parallel_decoding_mode != 0);
3153     if (cm->error_resilient_mode) {
3154       cm->frame_parallel_decoding_mode = 1;
3155       cm->reset_frame_context = 0;
3156       cm->refresh_frame_context = 0;
3157     } else if (cm->intra_only) {
3158       // Only reset the current context.
3159       cm->reset_frame_context = 2;
3160     }
3161   }
3162
3163   // Configure experimental use of segmentation for enhanced coding of
3164   // static regions if indicated.
3165   // Only allowed in second pass of two pass (as requires lagged coding)
3166   // and if the relevant speed feature flag is set.
3167   if (cpi->pass == 2 && cpi->sf.static_segmentation)
3168     configure_static_seg_features(cpi);
3169
3170   // For 1 pass CBR, check if we are dropping this frame.
3171   // Never drop on key frame.
3172   if (cpi->pass == 0 &&
3173       cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER &&
3174       cm->frame_type != KEY_FRAME) {
3175     if (vp9_rc_drop_frame(cpi)) {
3176       vp9_rc_postencode_update_drop_frame(cpi);
3177       ++cm->current_video_frame;
3178       return;
3179     }
3180   }
3181
3182   vp9_clear_system_state();
3183
3184   vp9_zero(cpi->rd_tx_select_threshes);
3185
3186 #if CONFIG_VP9_POSTPROC
3187   if (cpi->oxcf.noise_sensitivity > 0) {
3188     int l = 0;
3189     switch (cpi->oxcf.noise_sensitivity) {
3190       case 1:
3191         l = 20;
3192         break;
3193       case 2:
3194         l = 40;
3195         break;
3196       case 3:
3197         l = 60;
3198         break;
3199       case 4:
3200       case 5:
3201         l = 100;
3202         break;
3203       case 6:
3204         l = 150;
3205         break;
3206     }
3207     vp9_denoise(cpi->Source, cpi->Source, l);
3208   }
3209 #endif
3210
3211 #ifdef OUTPUT_YUV_SRC
3212   vp9_write_yuv_frame(cpi->Source);
3213 #endif
3214
3215   // Decide q and q bounds.
3216   q = vp9_rc_pick_q_and_bounds(cpi, &bottom_index, &top_index);
3217
3218   if (!frame_is_intra_only(cm)) {
3219     cm->interp_filter = DEFAULT_INTERP_FILTER;
3220     /* TODO: Decide this more intelligently */
3221     set_high_precision_mv(cpi, (q < HIGH_PRECISION_MV_QTHRESH));
3222   }
3223
3224   if (cpi->sf.recode_loop == DISALLOW_RECODE) {
3225     encode_without_recode_loop(cpi, size, dest, q);
3226   } else {
3227     encode_with_recode_loop(cpi, size, dest, q, bottom_index, top_index);
3228   }
3229
3230   // Special case code to reduce pulsing when key frames are forced at a
3231   // fixed interval. Note the reconstruction error if it is the frame before
3232   // the force key frame
3233   if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
3234     cpi->ambient_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm));
3235   }
3236
3237   // If the encoder forced a KEY_FRAME decision
3238   if (cm->frame_type == KEY_FRAME)
3239     cpi->refresh_last_frame = 1;
3240
3241   cm->frame_to_show = get_frame_new_buffer(cm);
3242
3243 #if WRITE_RECON_BUFFER
3244   if (cm->show_frame)
3245     write_cx_frame_to_file(cm->frame_to_show,
3246                            cm->current_video_frame);
3247   else
3248     write_cx_frame_to_file(cm->frame_to_show,
3249                            cm->current_video_frame + 1000);
3250 #endif
3251
3252   // Pick the loop filter level for the frame.
3253   loopfilter_frame(cpi, cm);
3254
3255 #if WRITE_RECON_BUFFER
3256   if (cm->show_frame)
3257     write_cx_frame_to_file(cm->frame_to_show,
3258                            cm->current_video_frame + 2000);
3259   else
3260     write_cx_frame_to_file(cm->frame_to_show,
3261                            cm->current_video_frame + 3000);
3262 #endif
3263
3264   // build the bitstream
3265   cpi->dummy_packing = 0;
3266   vp9_pack_bitstream(cpi, dest, size);
3267
3268   if (cm->seg.update_map)
3269     update_reference_segmentation_map(cpi);
3270
3271   release_scaled_references(cpi);
3272   update_reference_frames(cpi);
3273
3274   for (t = TX_4X4; t <= TX_32X32; t++)
3275     full_to_model_counts(cm->counts.coef[t], cpi->coef_counts[t]);
3276
3277   if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode)
3278     vp9_adapt_coef_probs(cm);
3279
3280   if (!frame_is_intra_only(cm)) {
3281     if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
3282       vp9_adapt_mode_probs(cm);
3283       vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
3284     }
3285   }
3286
3287 #if 0
3288   output_frame_level_debug_stats(cpi);
3289 #endif
3290   if (cpi->refresh_golden_frame == 1)
3291     cm->frame_flags |= FRAMEFLAGS_GOLDEN;
3292   else
3293     cm->frame_flags &= ~FRAMEFLAGS_GOLDEN;
3294
3295   if (cpi->refresh_alt_ref_frame == 1)
3296     cm->frame_flags |= FRAMEFLAGS_ALTREF;
3297   else
3298     cm->frame_flags &= ~FRAMEFLAGS_ALTREF;
3299
3300   get_ref_frame_flags(cpi);
3301
3302   vp9_rc_postencode_update(cpi, *size);
3303
3304   if (cm->frame_type == KEY_FRAME) {
3305     // Tell the caller that the frame was coded as a key frame
3306     *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY;
3307
3308 #if CONFIG_MULTIPLE_ARF
3309     // Reset the sequence number.
3310     if (cpi->multi_arf_enabled) {
3311       cpi->sequence_number = 0;
3312       cpi->frame_coding_order_period = cpi->new_frame_coding_order_period;
3313       cpi->new_frame_coding_order_period = -1;
3314     }
3315 #endif
3316   } else {
3317     *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY;
3318
3319 #if CONFIG_MULTIPLE_ARF
3320     /* Increment position in the coded frame sequence. */
3321     if (cpi->multi_arf_enabled) {
3322       ++cpi->sequence_number;
3323       if (cpi->sequence_number >= cpi->frame_coding_order_period) {
3324         cpi->sequence_number = 0;
3325         cpi->frame_coding_order_period = cpi->new_frame_coding_order_period;
3326         cpi->new_frame_coding_order_period = -1;
3327       }
3328       cpi->this_frame_weight = cpi->arf_weight[cpi->sequence_number];
3329       assert(cpi->this_frame_weight >= 0);
3330     }
3331 #endif
3332   }
3333
3334   // Clear the one shot update flags for segmentation map and mode/ref loop
3335   // filter deltas.
3336   cm->seg.update_map = 0;
3337   cm->seg.update_data = 0;
3338   cm->lf.mode_ref_delta_update = 0;
3339
3340   // keep track of the last coded dimensions
3341   cm->last_width = cm->width;
3342   cm->last_height = cm->height;
3343
3344   // reset to normal state now that we are done.
3345   if (!cm->show_existing_frame)
3346     cm->last_show_frame = cm->show_frame;
3347
3348   if (cm->show_frame) {
3349     // current mip will be the prev_mip for the next frame
3350     MODE_INFO *temp = cm->prev_mip;
3351     MODE_INFO **temp2 = cm->prev_mi_grid_base;
3352     cm->prev_mip = cm->mip;
3353     cm->mip = temp;
3354     cm->prev_mi_grid_base = cm->mi_grid_base;
3355     cm->mi_grid_base = temp2;
3356
3357     // update the upper left visible macroblock ptrs
3358     cm->mi = cm->mip + cm->mode_info_stride + 1;
3359     cm->mi_grid_visible = cm->mi_grid_base + cm->mode_info_stride + 1;
3360
3361     cpi->mb.e_mbd.mi_8x8 = cm->mi_grid_visible;
3362     cpi->mb.e_mbd.mi_8x8[0] = cm->mi;
3363
3364     // Don't increment frame counters if this was an altref buffer
3365     // update not a real frame
3366     ++cm->current_video_frame;
3367   }
3368
3369   // restore prev_mi
3370   cm->prev_mi = cm->prev_mip + cm->mode_info_stride + 1;
3371   cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mode_info_stride + 1;
3372 }
3373
3374 static void SvcEncode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
3375                       unsigned int *frame_flags) {
3376   vp9_rc_get_svc_params(cpi);
3377   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
3378 }
3379
3380 static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
3381                         unsigned int *frame_flags) {
3382   if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
3383     vp9_rc_get_one_pass_cbr_params(cpi);
3384   } else {
3385     vp9_rc_get_one_pass_vbr_params(cpi);
3386   }
3387   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
3388 }
3389
3390 static void Pass1Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
3391                         unsigned int *frame_flags) {
3392   (void) size;
3393   (void) dest;
3394   (void) frame_flags;
3395
3396   vp9_rc_get_first_pass_params(cpi);
3397   vp9_set_quantizer(cpi, find_fp_qindex());
3398   vp9_first_pass(cpi);
3399 }
3400
3401 static void Pass2Encode(VP9_COMP *cpi, size_t *size,
3402                         uint8_t *dest, unsigned int *frame_flags) {
3403   cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
3404
3405   vp9_rc_get_second_pass_params(cpi);
3406   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
3407
3408   vp9_twopass_postencode_update(cpi, *size);
3409 }
3410
3411 static void check_initial_width(VP9_COMP *cpi, int subsampling_x,
3412                                 int subsampling_y) {
3413   VP9_COMMON *const cm = &cpi->common;
3414   if (!cpi->initial_width) {
3415     cm->subsampling_x = subsampling_x;
3416     cm->subsampling_y = subsampling_y;
3417     alloc_raw_frame_buffers(cpi);
3418     cpi->initial_width = cm->width;
3419     cpi->initial_height = cm->height;
3420   }
3421 }
3422
3423
3424 int vp9_receive_raw_frame(VP9_PTR ptr, unsigned int frame_flags,
3425                           YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
3426                           int64_t end_time) {
3427   VP9_COMP              *cpi = (VP9_COMP *) ptr;
3428   VP9_COMMON             *cm = &cpi->common;
3429   struct vpx_usec_timer  timer;
3430   int                    res = 0;
3431   const int    subsampling_x = sd->uv_width  < sd->y_width;
3432   const int    subsampling_y = sd->uv_height < sd->y_height;
3433
3434   check_initial_width(cpi, subsampling_x, subsampling_y);
3435   vpx_usec_timer_start(&timer);
3436   if (vp9_lookahead_push(cpi->lookahead, sd, time_stamp, end_time, frame_flags,
3437                          cpi->active_map_enabled ? cpi->active_map : NULL))
3438     res = -1;
3439   vpx_usec_timer_mark(&timer);
3440   cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
3441
3442   if (cm->version == 0 && (subsampling_x != 1 || subsampling_y != 1)) {
3443     vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
3444                        "Non-4:2:0 color space requires profile >= 1");
3445     res = -1;
3446   }
3447
3448   return res;
3449 }
3450
3451
3452 static int frame_is_reference(const VP9_COMP *cpi) {
3453   const VP9_COMMON *cm = &cpi->common;
3454
3455   return cm->frame_type == KEY_FRAME ||
3456          cpi->refresh_last_frame ||
3457          cpi->refresh_golden_frame ||
3458          cpi->refresh_alt_ref_frame ||
3459          cm->refresh_frame_context ||
3460          cm->lf.mode_ref_delta_update ||
3461          cm->seg.update_map ||
3462          cm->seg.update_data;
3463 }
3464
3465 #if CONFIG_MULTIPLE_ARF
3466 int is_next_frame_arf(VP9_COMP *cpi) {
3467   // Negative entry in frame_coding_order indicates an ARF at this position.
3468   return cpi->frame_coding_order[cpi->sequence_number + 1] < 0 ? 1 : 0;
3469 }
3470 #endif
3471
3472 void adjust_frame_rate(VP9_COMP *cpi) {
3473   int64_t this_duration;
3474   int step = 0;
3475
3476   if (cpi->source->ts_start == cpi->first_time_stamp_ever) {
3477     this_duration = cpi->source->ts_end - cpi->source->ts_start;
3478     step = 1;
3479   } else {
3480     int64_t last_duration = cpi->last_end_time_stamp_seen
3481         - cpi->last_time_stamp_seen;
3482
3483     this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
3484
3485     // do a step update if the duration changes by 10%
3486     if (last_duration)
3487       step = (int)((this_duration - last_duration) * 10 / last_duration);
3488   }
3489
3490   if (this_duration) {
3491     if (step) {
3492       vp9_new_framerate(cpi, 10000000.0 / this_duration);
3493     } else {
3494       // Average this frame's rate into the last second's average
3495       // frame rate. If we haven't seen 1 second yet, then average
3496       // over the whole interval seen.
3497       const double interval = MIN((double)(cpi->source->ts_end
3498                                    - cpi->first_time_stamp_ever), 10000000.0);
3499       double avg_duration = 10000000.0 / cpi->oxcf.framerate;
3500       avg_duration *= (interval - avg_duration + this_duration);
3501       avg_duration /= interval;
3502
3503       vp9_new_framerate(cpi, 10000000.0 / avg_duration);
3504     }
3505   }
3506   cpi->last_time_stamp_seen = cpi->source->ts_start;
3507   cpi->last_end_time_stamp_seen = cpi->source->ts_end;
3508 }
3509
3510 int vp9_get_compressed_data(VP9_PTR ptr, unsigned int *frame_flags,
3511                             size_t *size, uint8_t *dest,
3512                             int64_t *time_stamp, int64_t *time_end, int flush) {
3513   VP9_COMP *cpi = (VP9_COMP *) ptr;
3514   VP9_COMMON *cm = &cpi->common;
3515   MACROBLOCKD *xd = &cpi->mb.e_mbd;
3516   struct vpx_usec_timer  cmptimer;
3517   YV12_BUFFER_CONFIG *force_src_buffer = NULL;
3518   MV_REFERENCE_FRAME ref_frame;
3519
3520   if (!cpi)
3521     return -1;
3522
3523   vpx_usec_timer_start(&cmptimer);
3524
3525   cpi->source = NULL;
3526
3527   set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
3528
3529   // Normal defaults
3530   cm->reset_frame_context = 0;
3531   cm->refresh_frame_context = 1;
3532   cpi->refresh_last_frame = 1;
3533   cpi->refresh_golden_frame = 0;
3534   cpi->refresh_alt_ref_frame = 0;
3535
3536   // Should we code an alternate reference frame.
3537   if (cpi->oxcf.play_alternate && cpi->rc.source_alt_ref_pending) {
3538     int frames_to_arf;
3539
3540 #if CONFIG_MULTIPLE_ARF
3541     assert(!cpi->multi_arf_enabled ||
3542            cpi->frame_coding_order[cpi->sequence_number] < 0);
3543
3544     if (cpi->multi_arf_enabled && (cpi->pass == 2))
3545       frames_to_arf = (-cpi->frame_coding_order[cpi->sequence_number])
3546           - cpi->next_frame_in_order;
3547     else
3548 #endif
3549       frames_to_arf = cpi->rc.frames_till_gf_update_due;
3550
3551     assert(frames_to_arf <= cpi->rc.frames_to_key);
3552
3553     if ((cpi->source = vp9_lookahead_peek(cpi->lookahead, frames_to_arf))) {
3554 #if CONFIG_MULTIPLE_ARF
3555       cpi->alt_ref_source[cpi->arf_buffered] = cpi->source;
3556 #else
3557       cpi->alt_ref_source = cpi->source;
3558 #endif
3559
3560       if (cpi->oxcf.arnr_max_frames > 0) {
3561         // Produce the filtered ARF frame.
3562         // TODO(agrange) merge these two functions.
3563         vp9_configure_arnr_filter(cpi, frames_to_arf, cpi->rc.gfu_boost);
3564         vp9_temporal_filter_prepare(cpi, frames_to_arf);
3565         vp9_extend_frame_borders(&cpi->alt_ref_buffer,
3566                                  cm->subsampling_x, cm->subsampling_y);
3567         force_src_buffer = &cpi->alt_ref_buffer;
3568       }
3569
3570       cm->show_frame = 0;
3571       cpi->refresh_alt_ref_frame = 1;
3572       cpi->refresh_golden_frame = 0;
3573       cpi->refresh_last_frame = 0;
3574       cpi->rc.is_src_frame_alt_ref = 0;
3575
3576 #if CONFIG_MULTIPLE_ARF
3577       if (!cpi->multi_arf_enabled)
3578 #endif
3579         cpi->rc.source_alt_ref_pending = 0;
3580     } else {
3581       cpi->rc.source_alt_ref_pending = 0;
3582     }
3583   }
3584
3585   if (!cpi->source) {
3586 #if CONFIG_MULTIPLE_ARF
3587     int i;
3588 #endif
3589     if ((cpi->source = vp9_lookahead_pop(cpi->lookahead, flush))) {
3590       cm->show_frame = 1;
3591       cm->intra_only = 0;
3592
3593 #if CONFIG_MULTIPLE_ARF
3594       // Is this frame the ARF overlay.
3595       cpi->rc.is_src_frame_alt_ref = 0;
3596       for (i = 0; i < cpi->arf_buffered; ++i) {
3597         if (cpi->source == cpi->alt_ref_source[i]) {
3598           cpi->rc.is_src_frame_alt_ref = 1;
3599           cpi->refresh_golden_frame = 1;
3600           break;
3601         }
3602       }
3603 #else
3604       cpi->rc.is_src_frame_alt_ref = cpi->alt_ref_source
3605           && (cpi->source == cpi->alt_ref_source);
3606 #endif
3607       if (cpi->rc.is_src_frame_alt_ref) {
3608         // Current frame is an ARF overlay frame.
3609 #if CONFIG_MULTIPLE_ARF
3610         cpi->alt_ref_source[i] = NULL;
3611 #else
3612         cpi->alt_ref_source = NULL;
3613 #endif
3614         // Don't refresh the last buffer for an ARF overlay frame. It will
3615         // become the GF so preserve last as an alternative prediction option.
3616         cpi->refresh_last_frame = 0;
3617       }
3618 #if CONFIG_MULTIPLE_ARF
3619       ++cpi->next_frame_in_order;
3620 #endif
3621     }
3622   }
3623
3624   if (cpi->source) {
3625     cpi->un_scaled_source = cpi->Source = force_src_buffer ? force_src_buffer
3626                                                            : &cpi->source->img;
3627     *time_stamp = cpi->source->ts_start;
3628     *time_end = cpi->source->ts_end;
3629     *frame_flags = cpi->source->flags;
3630
3631 #if CONFIG_MULTIPLE_ARF
3632     if ((cm->frame_type != KEY_FRAME) && (cpi->pass == 2))
3633       cpi->rc.source_alt_ref_pending = is_next_frame_arf(cpi);
3634 #endif
3635   } else {
3636     *size = 0;
3637     if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done) {
3638       vp9_end_first_pass(cpi);    /* get last stats packet */
3639       cpi->twopass.first_pass_done = 1;
3640     }
3641     return -1;
3642   }
3643
3644   if (cpi->source->ts_start < cpi->first_time_stamp_ever) {
3645     cpi->first_time_stamp_ever = cpi->source->ts_start;
3646     cpi->last_end_time_stamp_seen = cpi->source->ts_start;
3647   }
3648
3649   // adjust frame rates based on timestamps given
3650   if (cm->show_frame) {
3651     adjust_frame_rate(cpi);
3652   }
3653
3654   if (cpi->svc.number_temporal_layers > 1 &&
3655       cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
3656     update_layer_framerate(cpi);
3657     restore_layer_context(cpi);
3658   }
3659
3660   // start with a 0 size frame
3661   *size = 0;
3662
3663   // Clear down mmx registers
3664   vp9_clear_system_state();  // __asm emms;
3665
3666   /* find a free buffer for the new frame, releasing the reference previously
3667    * held.
3668    */
3669   cm->frame_bufs[cm->new_fb_idx].ref_count--;
3670   cm->new_fb_idx = get_free_fb(cm);
3671
3672 #if CONFIG_MULTIPLE_ARF
3673   /* Set up the correct ARF frame. */
3674   if (cpi->refresh_alt_ref_frame) {
3675     ++cpi->arf_buffered;
3676   }
3677   if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) &&
3678       (cpi->pass == 2)) {
3679     cpi->alt_fb_idx = cpi->arf_buffer_idx[cpi->sequence_number];
3680   }
3681 #endif
3682
3683   cm->frame_flags = *frame_flags;
3684
3685   // Reset the frame pointers to the current frame size
3686   vp9_realloc_frame_buffer(get_frame_new_buffer(cm),
3687                            cm->width, cm->height,
3688                            cm->subsampling_x, cm->subsampling_y,
3689                            VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL);
3690
3691   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3692     const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)];
3693     YV12_BUFFER_CONFIG *const buf = &cm->frame_bufs[idx].buf;
3694     RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - 1];
3695     ref_buf->buf = buf;
3696     ref_buf->idx = idx;
3697     vp9_setup_scale_factors_for_frame(&ref_buf->sf,
3698                                       buf->y_crop_width, buf->y_crop_height,
3699                                       cm->width, cm->height);
3700
3701     if (vp9_is_scaled(&ref_buf->sf))
3702       vp9_extend_frame_borders(buf, cm->subsampling_x, cm->subsampling_y);
3703   }
3704
3705   set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3706   xd->interp_kernel = vp9_get_interp_kernel(
3707       DEFAULT_INTERP_FILTER == SWITCHABLE ? EIGHTTAP : DEFAULT_INTERP_FILTER);
3708
3709   if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
3710     vp9_vaq_init();
3711   }
3712
3713   if (cpi->use_svc) {
3714     SvcEncode(cpi, size, dest, frame_flags);
3715   } else if (cpi->pass == 1) {
3716     Pass1Encode(cpi, size, dest, frame_flags);
3717   } else if (cpi->pass == 2) {
3718     Pass2Encode(cpi, size, dest, frame_flags);
3719   } else {
3720     // One pass encode
3721     Pass0Encode(cpi, size, dest, frame_flags);
3722   }
3723
3724   if (cm->refresh_frame_context)
3725     cm->frame_contexts[cm->frame_context_idx] = cm->fc;
3726
3727   // Frame was dropped, release scaled references.
3728   if (*size == 0) {
3729     release_scaled_references(cpi);
3730   }
3731
3732   if (*size > 0) {
3733     cpi->droppable = !frame_is_reference(cpi);
3734   }
3735
3736   // Save layer specific state.
3737   if (cpi->svc.number_temporal_layers > 1 &&
3738       cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
3739     save_layer_context(cpi);
3740   }
3741
3742   vpx_usec_timer_mark(&cmptimer);
3743   cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
3744
3745   if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame)
3746     generate_psnr_packet(cpi);
3747
3748 #if CONFIG_INTERNAL_STATS
3749
3750   if (cpi->pass != 1) {
3751     cpi->bytes += *size;
3752
3753     if (cm->show_frame) {
3754       cpi->count++;
3755
3756       if (cpi->b_calculate_psnr) {
3757         YV12_BUFFER_CONFIG *orig = cpi->Source;
3758         YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
3759         YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
3760         PSNR_STATS psnr;
3761         calc_psnr(orig, recon, &psnr);
3762
3763         cpi->total += psnr.psnr[0];
3764         cpi->total_y += psnr.psnr[1];
3765         cpi->total_u += psnr.psnr[2];
3766         cpi->total_v += psnr.psnr[3];
3767         cpi->total_sq_error += psnr.sse[0];
3768         cpi->total_samples += psnr.samples[0];
3769
3770         {
3771           PSNR_STATS psnr2;
3772           double frame_ssim2 = 0, weight = 0;
3773 #if CONFIG_VP9_POSTPROC
3774           vp9_deblock(cm->frame_to_show, &cm->post_proc_buffer,
3775                       cm->lf.filter_level * 10 / 6);
3776 #endif
3777           vp9_clear_system_state();
3778
3779           calc_psnr(orig, pp, &psnr2);
3780
3781           cpi->totalp += psnr2.psnr[0];
3782           cpi->totalp_y += psnr2.psnr[1];
3783           cpi->totalp_u += psnr2.psnr[2];
3784           cpi->totalp_v += psnr2.psnr[3];
3785           cpi->totalp_sq_error += psnr2.sse[0];
3786           cpi->totalp_samples += psnr2.samples[0];
3787
3788           frame_ssim2 = vp9_calc_ssim(orig, recon, 1, &weight);
3789
3790           cpi->summed_quality += frame_ssim2 * weight;
3791           cpi->summed_weights += weight;
3792
3793           frame_ssim2 = vp9_calc_ssim(orig, &cm->post_proc_buffer, 1, &weight);
3794
3795           cpi->summedp_quality += frame_ssim2 * weight;
3796           cpi->summedp_weights += weight;
3797 #if 0
3798           {
3799             FILE *f = fopen("q_used.stt", "a");
3800             fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
3801                     cpi->common.current_video_frame, y2, u2, v2,
3802                     frame_psnr2, frame_ssim2);
3803             fclose(f);
3804           }
3805 #endif
3806         }
3807       }
3808
3809       if (cpi->b_calculate_ssimg) {
3810         double y, u, v, frame_all;
3811         frame_all = vp9_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u, &v);
3812         cpi->total_ssimg_y += y;
3813         cpi->total_ssimg_u += u;
3814         cpi->total_ssimg_v += v;
3815         cpi->total_ssimg_all += frame_all;
3816       }
3817     }
3818   }
3819
3820 #endif
3821   return 0;
3822 }
3823
3824 int vp9_get_preview_raw_frame(VP9_PTR comp, YV12_BUFFER_CONFIG *dest,
3825                               vp9_ppflags_t *flags) {
3826   VP9_COMP *cpi = (VP9_COMP *) comp;
3827
3828   if (!cpi->common.show_frame) {
3829     return -1;
3830   } else {
3831     int ret;
3832 #if CONFIG_VP9_POSTPROC
3833     ret = vp9_post_proc_frame(&cpi->common, dest, flags);
3834 #else
3835
3836     if (cpi->common.frame_to_show) {
3837       *dest = *cpi->common.frame_to_show;
3838       dest->y_width = cpi->common.width;
3839       dest->y_height = cpi->common.height;
3840       dest->uv_width = cpi->common.width >> cpi->common.subsampling_x;
3841       dest->uv_height = cpi->common.height >> cpi->common.subsampling_y;
3842       ret = 0;
3843     } else {
3844       ret = -1;
3845     }
3846
3847 #endif  // !CONFIG_VP9_POSTPROC
3848     vp9_clear_system_state();
3849     return ret;
3850   }
3851 }
3852
3853 int vp9_set_roimap(VP9_PTR comp, unsigned char *map, unsigned int rows,
3854                    unsigned int cols, int delta_q[MAX_SEGMENTS],
3855                    int delta_lf[MAX_SEGMENTS],
3856                    unsigned int threshold[MAX_SEGMENTS]) {
3857   VP9_COMP *cpi = (VP9_COMP *) comp;
3858   signed char feature_data[SEG_LVL_MAX][MAX_SEGMENTS];
3859   struct segmentation *seg = &cpi->common.seg;
3860   int i;
3861
3862   if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols)
3863     return -1;
3864
3865   if (!map) {
3866     vp9_disable_segmentation((VP9_PTR)cpi);
3867     return 0;
3868   }
3869
3870   // Set the segmentation Map
3871   vp9_set_segmentation_map((VP9_PTR)cpi, map);
3872
3873   // Activate segmentation.
3874   vp9_enable_segmentation((VP9_PTR)cpi);
3875
3876   // Set up the quant, LF and breakout threshold segment data
3877   for (i = 0; i < MAX_SEGMENTS; i++) {
3878     feature_data[SEG_LVL_ALT_Q][i] = delta_q[i];
3879     feature_data[SEG_LVL_ALT_LF][i] = delta_lf[i];
3880     cpi->segment_encode_breakout[i] = threshold[i];
3881   }
3882
3883   // Enable the loop and quant changes in the feature mask
3884   for (i = 0; i < MAX_SEGMENTS; i++) {
3885     if (delta_q[i])
3886       vp9_enable_segfeature(seg, i, SEG_LVL_ALT_Q);
3887     else
3888       vp9_disable_segfeature(seg, i, SEG_LVL_ALT_Q);
3889
3890     if (delta_lf[i])
3891       vp9_enable_segfeature(seg, i, SEG_LVL_ALT_LF);
3892     else
3893       vp9_disable_segfeature(seg, i, SEG_LVL_ALT_LF);
3894   }
3895
3896   // Initialize the feature data structure
3897   // SEGMENT_DELTADATA    0, SEGMENT_ABSDATA      1
3898   vp9_set_segment_data((VP9_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
3899
3900   return 0;
3901 }
3902
3903 int vp9_set_active_map(VP9_PTR comp, unsigned char *map,
3904                        unsigned int rows, unsigned int cols) {
3905   VP9_COMP *cpi = (VP9_COMP *) comp;
3906
3907   if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
3908     if (map) {
3909       vpx_memcpy(cpi->active_map, map, rows * cols);
3910       cpi->active_map_enabled = 1;
3911     } else {
3912       cpi->active_map_enabled = 0;
3913     }
3914
3915     return 0;
3916   } else {
3917     // cpi->active_map_enabled = 0;
3918     return -1;
3919   }
3920 }
3921
3922 int vp9_set_internal_size(VP9_PTR comp,
3923                           VPX_SCALING horiz_mode, VPX_SCALING vert_mode) {
3924   VP9_COMP *cpi = (VP9_COMP *) comp;
3925   VP9_COMMON *cm = &cpi->common;
3926   int hr = 0, hs = 0, vr = 0, vs = 0;
3927
3928   if (horiz_mode > ONETWO || vert_mode > ONETWO)
3929     return -1;
3930
3931   Scale2Ratio(horiz_mode, &hr, &hs);
3932   Scale2Ratio(vert_mode, &vr, &vs);
3933
3934   // always go to the next whole number
3935   cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
3936   cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
3937
3938   assert(cm->width <= cpi->initial_width);
3939   assert(cm->height <= cpi->initial_height);
3940   update_frame_size(cpi);
3941   return 0;
3942 }
3943
3944 int vp9_set_size_literal(VP9_PTR comp, unsigned int width,
3945                          unsigned int height) {
3946   VP9_COMP *cpi = (VP9_COMP *)comp;
3947   VP9_COMMON *cm = &cpi->common;
3948
3949   check_initial_width(cpi, 1, 1);
3950
3951   if (width) {
3952     cm->width = width;
3953     if (cm->width * 5 < cpi->initial_width) {
3954       cm->width = cpi->initial_width / 5 + 1;
3955       printf("Warning: Desired width too small, changed to %d\n", cm->width);
3956     }
3957     if (cm->width > cpi->initial_width) {
3958       cm->width = cpi->initial_width;
3959       printf("Warning: Desired width too large, changed to %d\n", cm->width);
3960     }
3961   }
3962
3963   if (height) {
3964     cm->height = height;
3965     if (cm->height * 5 < cpi->initial_height) {
3966       cm->height = cpi->initial_height / 5 + 1;
3967       printf("Warning: Desired height too small, changed to %d\n", cm->height);
3968     }
3969     if (cm->height > cpi->initial_height) {
3970       cm->height = cpi->initial_height;
3971       printf("Warning: Desired height too large, changed to %d\n", cm->height);
3972     }
3973   }
3974
3975   assert(cm->width <= cpi->initial_width);
3976   assert(cm->height <= cpi->initial_height);
3977   update_frame_size(cpi);
3978   return 0;
3979 }
3980
3981 void vp9_set_svc(VP9_PTR comp, int use_svc) {
3982   VP9_COMP *cpi = (VP9_COMP *)comp;
3983   cpi->use_svc = use_svc;
3984   return;
3985 }
3986
3987 int vp9_calc_ss_err(const YV12_BUFFER_CONFIG *source,
3988                     const YV12_BUFFER_CONFIG *reference) {
3989   int i, j;
3990   int total = 0;
3991
3992   const uint8_t *src = source->y_buffer;
3993   const uint8_t *ref = reference->y_buffer;
3994
3995   // Loop through the Y plane raw and reconstruction data summing
3996   // (square differences)
3997   for (i = 0; i < source->y_height; i += 16) {
3998     for (j = 0; j < source->y_width; j += 16) {
3999       unsigned int sse;
4000       total += vp9_mse16x16(src + j, source->y_stride,
4001                             ref + j, reference->y_stride, &sse);
4002     }
4003
4004     src += 16 * source->y_stride;
4005     ref += 16 * reference->y_stride;
4006   }
4007
4008   return total;
4009 }
4010
4011
4012 int vp9_get_quantizer(VP9_PTR c) {
4013   return ((VP9_COMP *)c)->common.base_qindex;
4014 }