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