]> granicus.if.org Git - libvpx/blob - vp9/encoder/vp9_encoder.c
be29201e056ce4545f618c06032248940a0bb709
[libvpx] / vp9 / encoder / vp9_encoder.c
1 /*
2  * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10
11 #include <math.h>
12 #include <stdio.h>
13 #include <limits.h>
14
15 #include "./vpx_config.h"
16 #include "./vpx_scale_rtcd.h"
17 #include "vpx/internal/vpx_psnr.h"
18 #include "vpx_ports/vpx_timer.h"
19
20 #include "vp9/common/vp9_alloccommon.h"
21 #include "vp9/common/vp9_filter.h"
22 #include "vp9/common/vp9_idct.h"
23 #if CONFIG_VP9_POSTPROC
24 #include "vp9/common/vp9_postproc.h"
25 #endif
26 #include "vp9/common/vp9_reconinter.h"
27 #include "vp9/common/vp9_systemdependent.h"
28 #include "vp9/common/vp9_tile_common.h"
29
30 #include "vp9/encoder/vp9_aq_complexity.h"
31 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
32 #include "vp9/encoder/vp9_aq_variance.h"
33 #include "vp9/encoder/vp9_bitstream.h"
34 #include "vp9/encoder/vp9_context_tree.h"
35 #include "vp9/encoder/vp9_encodeframe.h"
36 #include "vp9/encoder/vp9_encodemv.h"
37 #include "vp9/encoder/vp9_firstpass.h"
38 #include "vp9/encoder/vp9_mbgraph.h"
39 #include "vp9/encoder/vp9_encoder.h"
40 #include "vp9/encoder/vp9_picklpf.h"
41 #include "vp9/encoder/vp9_ratectrl.h"
42 #include "vp9/encoder/vp9_rdopt.h"
43 #include "vp9/encoder/vp9_segmentation.h"
44 #include "vp9/encoder/vp9_speed_features.h"
45 #if CONFIG_INTERNAL_STATS
46 #include "vp9/encoder/vp9_ssim.h"
47 #endif
48 #include "vp9/encoder/vp9_temporal_filter.h"
49 #include "vp9/encoder/vp9_resize.h"
50 #include "vp9/encoder/vp9_svc_layercontext.h"
51
52 void vp9_coef_tree_initialize();
53
54 #define DEFAULT_INTERP_FILTER SWITCHABLE
55
56 #define SHARP_FILTER_QTHRESH 0          /* Q threshold for 8-tap sharp filter */
57
58 #define ALTREF_HIGH_PRECISION_MV 1      // Whether to use high precision mv
59                                          //  for altref computation.
60 #define HIGH_PRECISION_MV_QTHRESH 200   // Q threshold for high precision
61                                          // mv. Choose a very high value for
62                                          // now so that HIGH_PRECISION is always
63                                          // chosen.
64
65 // #define OUTPUT_YUV_REC
66
67 #ifdef OUTPUT_YUV_DENOISED
68 FILE *yuv_denoised_file;
69 #endif
70 #ifdef OUTPUT_YUV_SRC
71 FILE *yuv_file;
72 #endif
73 #ifdef OUTPUT_YUV_REC
74 FILE *yuv_rec_file;
75 #endif
76
77 #if 0
78 FILE *framepsnr;
79 FILE *kf_list;
80 FILE *keyfile;
81 #endif
82
83 static INLINE void Scale2Ratio(VPX_SCALING mode, int *hr, int *hs) {
84   switch (mode) {
85     case NORMAL:
86       *hr = 1;
87       *hs = 1;
88       break;
89     case FOURFIVE:
90       *hr = 4;
91       *hs = 5;
92       break;
93     case THREEFIVE:
94       *hr = 3;
95       *hs = 5;
96     break;
97     case ONETWO:
98       *hr = 1;
99       *hs = 2;
100     break;
101     default:
102       *hr = 1;
103       *hs = 1;
104        assert(0);
105       break;
106   }
107 }
108
109 static void set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
110   MACROBLOCK *const mb = &cpi->mb;
111   cpi->common.allow_high_precision_mv = allow_high_precision_mv;
112   if (cpi->common.allow_high_precision_mv) {
113     mb->mvcost = mb->nmvcost_hp;
114     mb->mvsadcost = mb->nmvsadcost_hp;
115   } else {
116     mb->mvcost = mb->nmvcost;
117     mb->mvsadcost = mb->nmvsadcost;
118   }
119 }
120
121 static void setup_frame(VP9_COMP *cpi) {
122   VP9_COMMON *const cm = &cpi->common;
123   // Set up entropy context depending on frame type. The decoder mandates
124   // the use of the default context, index 0, for keyframes and inter
125   // frames where the error_resilient_mode or intra_only flag is set. For
126   // other inter-frames the encoder currently uses only two contexts;
127   // context 1 for ALTREF frames and context 0 for the others.
128   if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
129     vp9_setup_past_independence(cm);
130   } else {
131     if (!cpi->use_svc)
132       cm->frame_context_idx = cpi->refresh_alt_ref_frame;
133   }
134
135   if (cm->frame_type == KEY_FRAME) {
136     cpi->refresh_golden_frame = 1;
137     cpi->refresh_alt_ref_frame = 1;
138   } else {
139     cm->fc = cm->frame_contexts[cm->frame_context_idx];
140   }
141 }
142
143 void vp9_initialize_enc() {
144   static int init_done = 0;
145
146   if (!init_done) {
147     vp9_init_neighbors();
148     vp9_coef_tree_initialize();
149     vp9_tokenize_initialize();
150     vp9_init_me_luts();
151     vp9_rc_init_minq_luts();
152     vp9_entropy_mv_init();
153     vp9_entropy_mode_init();
154     vp9_temporal_filter_init();
155     init_done = 1;
156   }
157 }
158
159 static void dealloc_compressor_data(VP9_COMP *cpi) {
160   VP9_COMMON *const cm = &cpi->common;
161   int i;
162
163   // Delete sementation map
164   vpx_free(cpi->segmentation_map);
165   cpi->segmentation_map = NULL;
166   vpx_free(cm->last_frame_seg_map);
167   cm->last_frame_seg_map = NULL;
168   vpx_free(cpi->coding_context.last_frame_seg_map_copy);
169   cpi->coding_context.last_frame_seg_map_copy = NULL;
170
171   vpx_free(cpi->complexity_map);
172   cpi->complexity_map = NULL;
173
174   vp9_cyclic_refresh_free(cpi->cyclic_refresh);
175   cpi->cyclic_refresh = NULL;
176
177   vp9_free_frame_buffers(cm);
178   vp9_free_context_buffers(cm);
179
180   vp9_free_frame_buffer(&cpi->last_frame_uf);
181   vp9_free_frame_buffer(&cpi->scaled_source);
182   vp9_free_frame_buffer(&cpi->scaled_last_source);
183   vp9_free_frame_buffer(&cpi->alt_ref_buffer);
184   vp9_lookahead_destroy(cpi->lookahead);
185
186   vpx_free(cpi->tok);
187   cpi->tok = 0;
188
189   vp9_free_pc_tree(cpi);
190
191   for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
192     LAYER_CONTEXT *const lc = &cpi->svc.layer_context[i];
193     vpx_free(lc->rc_twopass_stats_in.buf);
194     lc->rc_twopass_stats_in.buf = NULL;
195     lc->rc_twopass_stats_in.sz = 0;
196   }
197 }
198
199 static void save_coding_context(VP9_COMP *cpi) {
200   CODING_CONTEXT *const cc = &cpi->coding_context;
201   VP9_COMMON *cm = &cpi->common;
202
203   // Stores a snapshot of key state variables which can subsequently be
204   // restored with a call to vp9_restore_coding_context. These functions are
205   // intended for use in a re-code loop in vp9_compress_frame where the
206   // quantizer value is adjusted between loop iterations.
207   vp9_copy(cc->nmvjointcost,  cpi->mb.nmvjointcost);
208   vp9_copy(cc->nmvcosts,  cpi->mb.nmvcosts);
209   vp9_copy(cc->nmvcosts_hp,  cpi->mb.nmvcosts_hp);
210
211   vp9_copy(cc->segment_pred_probs, cm->seg.pred_probs);
212
213   vpx_memcpy(cpi->coding_context.last_frame_seg_map_copy,
214              cm->last_frame_seg_map, (cm->mi_rows * cm->mi_cols));
215
216   vp9_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
217   vp9_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
218
219   cc->fc = cm->fc;
220 }
221
222 static void restore_coding_context(VP9_COMP *cpi) {
223   CODING_CONTEXT *const cc = &cpi->coding_context;
224   VP9_COMMON *cm = &cpi->common;
225
226   // Restore key state variables to the snapshot state stored in the
227   // previous call to vp9_save_coding_context.
228   vp9_copy(cpi->mb.nmvjointcost, cc->nmvjointcost);
229   vp9_copy(cpi->mb.nmvcosts, cc->nmvcosts);
230   vp9_copy(cpi->mb.nmvcosts_hp, cc->nmvcosts_hp);
231
232   vp9_copy(cm->seg.pred_probs, cc->segment_pred_probs);
233
234   vpx_memcpy(cm->last_frame_seg_map,
235              cpi->coding_context.last_frame_seg_map_copy,
236              (cm->mi_rows * cm->mi_cols));
237
238   vp9_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
239   vp9_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
240
241   cm->fc = cc->fc;
242 }
243
244 static void configure_static_seg_features(VP9_COMP *cpi) {
245   VP9_COMMON *const cm = &cpi->common;
246   const RATE_CONTROL *const rc = &cpi->rc;
247   struct segmentation *const seg = &cm->seg;
248
249   int high_q = (int)(rc->avg_q > 48.0);
250   int qi_delta;
251
252   // Disable and clear down for KF
253   if (cm->frame_type == KEY_FRAME) {
254     // Clear down the global segmentation map
255     vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
256     seg->update_map = 0;
257     seg->update_data = 0;
258     cpi->static_mb_pct = 0;
259
260     // Disable segmentation
261     vp9_disable_segmentation(seg);
262
263     // Clear down the segment features.
264     vp9_clearall_segfeatures(seg);
265   } else if (cpi->refresh_alt_ref_frame) {
266     // If this is an alt ref frame
267     // Clear down the global segmentation map
268     vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
269     seg->update_map = 0;
270     seg->update_data = 0;
271     cpi->static_mb_pct = 0;
272
273     // Disable segmentation and individual segment features by default
274     vp9_disable_segmentation(seg);
275     vp9_clearall_segfeatures(seg);
276
277     // Scan frames from current to arf frame.
278     // This function re-enables segmentation if appropriate.
279     vp9_update_mbgraph_stats(cpi);
280
281     // If segmentation was enabled set those features needed for the
282     // arf itself.
283     if (seg->enabled) {
284       seg->update_map = 1;
285       seg->update_data = 1;
286
287       qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875);
288       vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
289       vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
290
291       vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
292       vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
293
294       // Where relevant assume segment data is delta data
295       seg->abs_delta = SEGMENT_DELTADATA;
296     }
297   } else if (seg->enabled) {
298     // All other frames if segmentation has been enabled
299
300     // First normal frame in a valid gf or alt ref group
301     if (rc->frames_since_golden == 0) {
302       // Set up segment features for normal frames in an arf group
303       if (rc->source_alt_ref_active) {
304         seg->update_map = 0;
305         seg->update_data = 1;
306         seg->abs_delta = SEGMENT_DELTADATA;
307
308         qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125);
309         vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
310         vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
311
312         vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
313         vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
314
315         // Segment coding disabled for compred testing
316         if (high_q || (cpi->static_mb_pct == 100)) {
317           vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
318           vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
319           vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
320         }
321       } else {
322         // Disable segmentation and clear down features if alt ref
323         // is not active for this group
324
325         vp9_disable_segmentation(seg);
326
327         vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
328
329         seg->update_map = 0;
330         seg->update_data = 0;
331
332         vp9_clearall_segfeatures(seg);
333       }
334     } else if (rc->is_src_frame_alt_ref) {
335       // Special case where we are coding over the top of a previous
336       // alt ref frame.
337       // Segment coding disabled for compred testing
338
339       // Enable ref frame features for segment 0 as well
340       vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
341       vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
342
343       // All mbs should use ALTREF_FRAME
344       vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
345       vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
346       vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
347       vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
348
349       // Skip all MBs if high Q (0,0 mv and skip coeffs)
350       if (high_q) {
351         vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
352         vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
353       }
354       // Enable data update
355       seg->update_data = 1;
356     } else {
357       // All other frames.
358
359       // No updates.. leave things as they are.
360       seg->update_map = 0;
361       seg->update_data = 0;
362     }
363   }
364 }
365
366 static void update_reference_segmentation_map(VP9_COMP *cpi) {
367   VP9_COMMON *const cm = &cpi->common;
368   MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
369   uint8_t *cache_ptr = cm->last_frame_seg_map;
370   int row, col;
371
372   for (row = 0; row < cm->mi_rows; row++) {
373     MODE_INFO **mi_8x8 = mi_8x8_ptr;
374     uint8_t *cache = cache_ptr;
375     for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
376       cache[0] = mi_8x8[0]->mbmi.segment_id;
377     mi_8x8_ptr += cm->mi_stride;
378     cache_ptr += cm->mi_cols;
379   }
380 }
381
382
383 static void set_speed_features(VP9_COMP *cpi) {
384 #if CONFIG_INTERNAL_STATS
385   int i;
386   for (i = 0; i < MAX_MODES; ++i)
387     cpi->mode_chosen_counts[i] = 0;
388 #endif
389
390   vp9_set_speed_features(cpi);
391
392   // Set rd thresholds based on mode and speed setting
393   vp9_set_rd_speed_thresholds(cpi);
394   vp9_set_rd_speed_thresholds_sub8x8(cpi);
395 }
396
397 static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
398   VP9_COMMON *cm = &cpi->common;
399   const VP9EncoderConfig *oxcf = &cpi->oxcf;
400
401   cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height,
402                                       cm->subsampling_x, cm->subsampling_y,
403                                       oxcf->lag_in_frames);
404   if (!cpi->lookahead)
405     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
406                        "Failed to allocate lag buffers");
407
408   if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
409                                oxcf->width, oxcf->height,
410                                cm->subsampling_x, cm->subsampling_y,
411                                VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
412     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
413                        "Failed to allocate altref buffer");
414 }
415
416 static void alloc_ref_frame_buffers(VP9_COMP *cpi) {
417   VP9_COMMON *const cm = &cpi->common;
418   if (vp9_alloc_frame_buffers(cm, cm->width, cm->height))
419     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
420                        "Failed to allocate frame buffers");
421 }
422
423 static void alloc_util_frame_buffers(VP9_COMP *cpi) {
424   VP9_COMMON *const cm = &cpi->common;
425   if (vp9_realloc_frame_buffer(&cpi->last_frame_uf,
426                                cm->width, cm->height,
427                                cm->subsampling_x, cm->subsampling_y,
428                                VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
429     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
430                        "Failed to allocate last frame buffer");
431
432   if (vp9_realloc_frame_buffer(&cpi->scaled_source,
433                                cm->width, cm->height,
434                                cm->subsampling_x, cm->subsampling_y,
435                                VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
436     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
437                        "Failed to allocate scaled source buffer");
438
439   if (vp9_realloc_frame_buffer(&cpi->scaled_last_source,
440                                cm->width, cm->height,
441                                cm->subsampling_x, cm->subsampling_y,
442                                VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL))
443     vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
444                        "Failed to allocate scaled last source buffer");
445 }
446
447 void vp9_alloc_compressor_data(VP9_COMP *cpi) {
448   VP9_COMMON *cm = &cpi->common;
449
450   vp9_alloc_context_buffers(cm, cm->width, cm->height);
451
452   vpx_free(cpi->tok);
453
454   {
455     unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
456     CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
457   }
458
459   vp9_setup_pc_tree(&cpi->common, cpi);
460 }
461
462 static void update_frame_size(VP9_COMP *cpi) {
463   VP9_COMMON *const cm = &cpi->common;
464   MACROBLOCKD *const xd = &cpi->mb.e_mbd;
465   vp9_update_frame_size(cm);
466   init_macroblockd(cm, xd);
467 }
468
469 void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
470   cpi->oxcf.framerate = framerate < 0.1 ? 30 : framerate;
471   vp9_rc_update_framerate(cpi);
472 }
473
474 int64_t vp9_rescale(int64_t val, int64_t num, int denom) {
475   int64_t llnum = num;
476   int64_t llden = denom;
477   int64_t llval = val;
478
479   return (llval * llnum / llden);
480 }
481
482 static void set_tile_limits(VP9_COMP *cpi) {
483   VP9_COMMON *const cm = &cpi->common;
484
485   int min_log2_tile_cols, max_log2_tile_cols;
486   vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
487
488   cm->log2_tile_cols = clamp(cpi->oxcf.tile_columns,
489                              min_log2_tile_cols, max_log2_tile_cols);
490   cm->log2_tile_rows = cpi->oxcf.tile_rows;
491 }
492
493 static void init_buffer_indices(VP9_COMP *cpi) {
494   cpi->lst_fb_idx = 0;
495   cpi->gld_fb_idx = 1;
496   cpi->alt_fb_idx = 2;
497 }
498
499 static void init_config(struct VP9_COMP *cpi, VP9EncoderConfig *oxcf) {
500   VP9_COMMON *const cm = &cpi->common;
501
502   cpi->oxcf = *oxcf;
503
504   cm->profile = oxcf->profile;
505   cm->bit_depth = oxcf->bit_depth;
506
507   cm->width = oxcf->width;
508   cm->height = oxcf->height;
509   vp9_alloc_compressor_data(cpi);
510
511   // Spatial scalability.
512   cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
513   // Temporal scalability.
514   cpi->svc.number_temporal_layers = oxcf->ts_number_layers;
515
516   if ((cpi->svc.number_temporal_layers > 1 &&
517       cpi->oxcf.rc_mode == VPX_CBR) ||
518       (cpi->svc.number_spatial_layers > 1 &&
519       cpi->oxcf.mode == TWO_PASS_SECOND_BEST)) {
520     vp9_init_layer_context(cpi);
521   }
522
523   // change includes all joint functionality
524   vp9_change_config(cpi, oxcf);
525
526   cpi->static_mb_pct = 0;
527   cpi->ref_frame_flags = 0;
528
529   init_buffer_indices(cpi);
530
531   set_tile_limits(cpi);
532 }
533
534 static int get_pass(MODE mode) {
535   switch (mode) {
536     case REALTIME:
537     case ONE_PASS_GOOD:
538     case ONE_PASS_BEST:
539       return 0;
540
541     case TWO_PASS_FIRST:
542       return 1;
543
544     case TWO_PASS_SECOND_GOOD:
545     case TWO_PASS_SECOND_BEST:
546       return 2;
547   }
548   return -1;
549 }
550
551 void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
552   VP9_COMMON *const cm = &cpi->common;
553   RATE_CONTROL *const rc = &cpi->rc;
554
555   if (cm->profile != oxcf->profile)
556     cm->profile = oxcf->profile;
557   cm->bit_depth = oxcf->bit_depth;
558
559   if (cm->profile <= PROFILE_1)
560     assert(cm->bit_depth == BITS_8);
561   else
562     assert(cm->bit_depth > BITS_8);
563
564   cpi->oxcf = *oxcf;
565   cpi->pass = get_pass(cpi->oxcf.mode);
566
567   rc->baseline_gf_interval = DEFAULT_GF_INTERVAL;
568
569   cpi->refresh_golden_frame = 0;
570   cpi->refresh_last_frame = 1;
571   cm->refresh_frame_context = 1;
572   cm->reset_frame_context = 0;
573
574   vp9_reset_segment_features(&cm->seg);
575   set_high_precision_mv(cpi, 0);
576
577   {
578     int i;
579
580     for (i = 0; i < MAX_SEGMENTS; i++)
581       cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
582   }
583   cpi->encode_breakout = cpi->oxcf.encode_breakout;
584
585   // local file playback mode == really big buffer
586   if (cpi->oxcf.rc_mode == VPX_VBR) {
587     cpi->oxcf.starting_buffer_level_ms = 60000;
588     cpi->oxcf.optimal_buffer_level_ms = 60000;
589     cpi->oxcf.maximum_buffer_size_ms = 240000;
590   }
591
592   rc->starting_buffer_level = vp9_rescale(cpi->oxcf.starting_buffer_level_ms,
593                                           cpi->oxcf.target_bandwidth, 1000);
594
595   // Set or reset optimal and maximum buffer levels.
596   if (cpi->oxcf.optimal_buffer_level_ms == 0)
597     rc->optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
598   else
599     rc->optimal_buffer_level = vp9_rescale(cpi->oxcf.optimal_buffer_level_ms,
600                                            cpi->oxcf.target_bandwidth, 1000);
601
602   if (cpi->oxcf.maximum_buffer_size_ms == 0)
603     rc->maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
604   else
605     rc->maximum_buffer_size = vp9_rescale(cpi->oxcf.maximum_buffer_size_ms,
606                                           cpi->oxcf.target_bandwidth, 1000);
607   // Under a configuration change, where maximum_buffer_size may change,
608   // keep buffer level clipped to the maximum allowed buffer size.
609   rc->bits_off_target = MIN(rc->bits_off_target, rc->maximum_buffer_size);
610   rc->buffer_level = MIN(rc->buffer_level, rc->maximum_buffer_size);
611
612   // Set up frame rate and related parameters rate control values.
613   vp9_new_framerate(cpi, cpi->oxcf.framerate);
614
615   // Set absolute upper and lower quality limits
616   rc->worst_quality = cpi->oxcf.worst_allowed_q;
617   rc->best_quality = cpi->oxcf.best_allowed_q;
618
619   cm->interp_filter = DEFAULT_INTERP_FILTER;
620
621   cm->display_width = cpi->oxcf.width;
622   cm->display_height = cpi->oxcf.height;
623
624   if (cpi->initial_width) {
625     // Increasing the size of the frame beyond the first seen frame, or some
626     // otherwise signaled maximum size, is not supported.
627     // TODO(jkoleszar): exit gracefully.
628     assert(cm->width <= cpi->initial_width);
629     assert(cm->height <= cpi->initial_height);
630   }
631   update_frame_size(cpi);
632
633   if ((cpi->svc.number_temporal_layers > 1 &&
634       cpi->oxcf.rc_mode == VPX_CBR) ||
635       (cpi->svc.number_spatial_layers > 1 && cpi->pass == 2)) {
636     vp9_update_layer_context_change_config(cpi,
637                                            (int)cpi->oxcf.target_bandwidth);
638   }
639
640   cpi->alt_ref_source = NULL;
641   rc->is_src_frame_alt_ref = 0;
642
643 #if 0
644   // Experimental RD Code
645   cpi->frame_distortion = 0;
646   cpi->last_frame_distortion = 0;
647 #endif
648
649   set_tile_limits(cpi);
650
651   cpi->ext_refresh_frame_flags_pending = 0;
652   cpi->ext_refresh_frame_context_pending = 0;
653
654 #if CONFIG_DENOISING
655   vp9_denoiser_alloc(&(cpi->denoiser), cm->width, cm->height,
656                      // TODO(tkopp) An unrelated bug causes
657                      // cm->subsampling_{x,y} to be uninitialized at this point
658                      // in execution. For now we assume YUV-420, which is x/y
659                      // subsampling of 1.
660                      1, 1,
661                      // cm->subsampling_x, cm->subsampling_y,
662                      VP9_ENC_BORDER_IN_PIXELS);
663 #endif
664 }
665
666 #ifndef M_LOG2_E
667 #define M_LOG2_E 0.693147180559945309417
668 #endif
669 #define log2f(x) (log (x) / (float) M_LOG2_E)
670
671 static void cal_nmvjointsadcost(int *mvjointsadcost) {
672   mvjointsadcost[0] = 600;
673   mvjointsadcost[1] = 300;
674   mvjointsadcost[2] = 300;
675   mvjointsadcost[3] = 300;
676 }
677
678 static void cal_nmvsadcosts(int *mvsadcost[2]) {
679   int i = 1;
680
681   mvsadcost[0][0] = 0;
682   mvsadcost[1][0] = 0;
683
684   do {
685     double z = 256 * (2 * (log2f(8 * i) + .6));
686     mvsadcost[0][i] = (int)z;
687     mvsadcost[1][i] = (int)z;
688     mvsadcost[0][-i] = (int)z;
689     mvsadcost[1][-i] = (int)z;
690   } while (++i <= MV_MAX);
691 }
692
693 static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
694   int i = 1;
695
696   mvsadcost[0][0] = 0;
697   mvsadcost[1][0] = 0;
698
699   do {
700     double z = 256 * (2 * (log2f(8 * i) + .6));
701     mvsadcost[0][i] = (int)z;
702     mvsadcost[1][i] = (int)z;
703     mvsadcost[0][-i] = (int)z;
704     mvsadcost[1][-i] = (int)z;
705   } while (++i <= MV_MAX);
706 }
707
708
709 VP9_COMP *vp9_create_compressor(VP9EncoderConfig *oxcf) {
710   unsigned int i, j;
711   VP9_COMP *const cpi = vpx_memalign(32, sizeof(VP9_COMP));
712   VP9_COMMON *const cm = cpi != NULL ? &cpi->common : NULL;
713
714   if (!cm)
715     return NULL;
716
717   vp9_zero(*cpi);
718
719   if (setjmp(cm->error.jmp)) {
720     cm->error.setjmp = 0;
721     vp9_remove_compressor(cpi);
722     return 0;
723   }
724
725   cm->error.setjmp = 1;
726
727   vp9_rtcd();
728
729   cpi->use_svc = 0;
730
731   init_config(cpi, oxcf);
732   vp9_rc_init(&cpi->oxcf, cpi->pass, &cpi->rc);
733
734   cm->current_video_frame = 0;
735
736   // Set reference frame sign bias for ALTREF frame to 1 (for now)
737   cm->ref_frame_sign_bias[ALTREF_FRAME] = 1;
738
739   cpi->gold_is_last = 0;
740   cpi->alt_is_last = 0;
741   cpi->gold_is_alt = 0;
742
743   // Create the encoder segmentation map and set all entries to 0
744   CHECK_MEM_ERROR(cm, cpi->segmentation_map,
745                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
746
747   // Create a complexity map used for rd adjustment
748   CHECK_MEM_ERROR(cm, cpi->complexity_map,
749                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
750
751   // Create a map used for cyclic background refresh.
752   CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
753                   vp9_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
754
755   // And a place holder structure is the coding context
756   // for use if we want to save and restore it
757   CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy,
758                   vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
759
760   for (i = 0; i < (sizeof(cpi->mbgraph_stats) /
761                    sizeof(cpi->mbgraph_stats[0])); i++) {
762     CHECK_MEM_ERROR(cm, cpi->mbgraph_stats[i].mb_stats,
763                     vpx_calloc(cm->MBs *
764                                sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
765   }
766
767   cpi->refresh_alt_ref_frame = 0;
768
769   // Note that at the moment multi_arf will not work with svc.
770   // For the current check in all the execution paths are defaulted to 0
771   // pending further tuning and testing. The code is left in place here
772   // as a place holder in regard to the required paths.
773   if (cpi->pass == 2) {
774     if (cpi->use_svc) {
775       cpi->multi_arf_allowed = 0;
776       cpi->multi_arf_enabled = 0;
777     } else {
778       // Disable by default for now.
779       cpi->multi_arf_allowed = 0;
780       cpi->multi_arf_enabled = 0;
781     }
782   } else {
783     cpi->multi_arf_allowed = 0;
784     cpi->multi_arf_enabled = 0;
785   }
786
787   cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
788 #if CONFIG_INTERNAL_STATS
789   cpi->b_calculate_ssimg = 0;
790
791   cpi->count = 0;
792   cpi->bytes = 0;
793
794   if (cpi->b_calculate_psnr) {
795     cpi->total_y = 0.0;
796     cpi->total_u = 0.0;
797     cpi->total_v = 0.0;
798     cpi->total = 0.0;
799     cpi->total_sq_error = 0;
800     cpi->total_samples = 0;
801
802     cpi->totalp_y = 0.0;
803     cpi->totalp_u = 0.0;
804     cpi->totalp_v = 0.0;
805     cpi->totalp = 0.0;
806     cpi->totalp_sq_error = 0;
807     cpi->totalp_samples = 0;
808
809     cpi->tot_recode_hits = 0;
810     cpi->summed_quality = 0;
811     cpi->summed_weights = 0;
812     cpi->summedp_quality = 0;
813     cpi->summedp_weights = 0;
814   }
815
816   if (cpi->b_calculate_ssimg) {
817     cpi->total_ssimg_y = 0;
818     cpi->total_ssimg_u = 0;
819     cpi->total_ssimg_v = 0;
820     cpi->total_ssimg_all = 0;
821   }
822
823 #endif
824
825   cpi->first_time_stamp_ever = INT64_MAX;
826
827   cal_nmvjointsadcost(cpi->mb.nmvjointsadcost);
828   cpi->mb.nmvcost[0] = &cpi->mb.nmvcosts[0][MV_MAX];
829   cpi->mb.nmvcost[1] = &cpi->mb.nmvcosts[1][MV_MAX];
830   cpi->mb.nmvsadcost[0] = &cpi->mb.nmvsadcosts[0][MV_MAX];
831   cpi->mb.nmvsadcost[1] = &cpi->mb.nmvsadcosts[1][MV_MAX];
832   cal_nmvsadcosts(cpi->mb.nmvsadcost);
833
834   cpi->mb.nmvcost_hp[0] = &cpi->mb.nmvcosts_hp[0][MV_MAX];
835   cpi->mb.nmvcost_hp[1] = &cpi->mb.nmvcosts_hp[1][MV_MAX];
836   cpi->mb.nmvsadcost_hp[0] = &cpi->mb.nmvsadcosts_hp[0][MV_MAX];
837   cpi->mb.nmvsadcost_hp[1] = &cpi->mb.nmvsadcosts_hp[1][MV_MAX];
838   cal_nmvsadcosts_hp(cpi->mb.nmvsadcost_hp);
839
840 #ifdef OUTPUT_YUV_DENOISED
841   yuv_denoised_file = fopen("denoised.yuv", "ab");
842 #endif
843 #ifdef OUTPUT_YUV_SRC
844   yuv_file = fopen("bd.yuv", "ab");
845 #endif
846 #ifdef OUTPUT_YUV_REC
847   yuv_rec_file = fopen("rec.yuv", "wb");
848 #endif
849
850 #if 0
851   framepsnr = fopen("framepsnr.stt", "a");
852   kf_list = fopen("kf_list.stt", "w");
853 #endif
854
855   cpi->output_pkt_list = oxcf->output_pkt_list;
856
857   cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
858
859   if (cpi->pass == 1) {
860     vp9_init_first_pass(cpi);
861   } else if (cpi->pass == 2) {
862     const size_t packet_sz = sizeof(FIRSTPASS_STATS);
863     const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
864
865     if (cpi->svc.number_spatial_layers > 1
866         && cpi->svc.number_temporal_layers == 1) {
867       FIRSTPASS_STATS *const stats = oxcf->two_pass_stats_in.buf;
868       FIRSTPASS_STATS *stats_copy[VPX_SS_MAX_LAYERS] = {0};
869       int i;
870
871       for (i = 0; i < oxcf->ss_number_layers; ++i) {
872         FIRSTPASS_STATS *const last_packet_for_layer =
873             &stats[packets - oxcf->ss_number_layers + i];
874         const int layer_id = (int)last_packet_for_layer->spatial_layer_id;
875         const int packets_in_layer = (int)last_packet_for_layer->count + 1;
876         if (layer_id >= 0 && layer_id < oxcf->ss_number_layers) {
877           LAYER_CONTEXT *const lc = &cpi->svc.layer_context[layer_id];
878
879           vpx_free(lc->rc_twopass_stats_in.buf);
880
881           lc->rc_twopass_stats_in.sz = packets_in_layer * packet_sz;
882           CHECK_MEM_ERROR(cm, lc->rc_twopass_stats_in.buf,
883                           vpx_malloc(lc->rc_twopass_stats_in.sz));
884           lc->twopass.stats_in_start = lc->rc_twopass_stats_in.buf;
885           lc->twopass.stats_in = lc->twopass.stats_in_start;
886           lc->twopass.stats_in_end = lc->twopass.stats_in_start
887                                      + packets_in_layer - 1;
888           stats_copy[layer_id] = lc->rc_twopass_stats_in.buf;
889         }
890       }
891
892       for (i = 0; i < packets; ++i) {
893         const int layer_id = (int)stats[i].spatial_layer_id;
894         if (layer_id >= 0 && layer_id < oxcf->ss_number_layers
895             && stats_copy[layer_id] != NULL) {
896           *stats_copy[layer_id] = stats[i];
897           ++stats_copy[layer_id];
898         }
899       }
900
901       vp9_init_second_pass_spatial_svc(cpi);
902     } else {
903       cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
904       cpi->twopass.stats_in = cpi->twopass.stats_in_start;
905       cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
906
907       vp9_init_second_pass(cpi);
908     }
909   }
910
911   set_speed_features(cpi);
912
913   // Default rd threshold factors for mode selection
914   for (i = 0; i < BLOCK_SIZES; ++i) {
915     for (j = 0; j < MAX_MODES; ++j)
916       cpi->rd.thresh_freq_fact[i][j] = 32;
917   }
918
919 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF)\
920     cpi->fn_ptr[BT].sdf            = SDF; \
921     cpi->fn_ptr[BT].sdaf           = SDAF; \
922     cpi->fn_ptr[BT].vf             = VF; \
923     cpi->fn_ptr[BT].svf            = SVF; \
924     cpi->fn_ptr[BT].svaf           = SVAF; \
925     cpi->fn_ptr[BT].sdx3f          = SDX3F; \
926     cpi->fn_ptr[BT].sdx8f          = SDX8F; \
927     cpi->fn_ptr[BT].sdx4df         = SDX4DF;
928
929   BFP(BLOCK_32X16, vp9_sad32x16, vp9_sad32x16_avg,
930       vp9_variance32x16, vp9_sub_pixel_variance32x16,
931       vp9_sub_pixel_avg_variance32x16, NULL, NULL, vp9_sad32x16x4d)
932
933   BFP(BLOCK_16X32, vp9_sad16x32, vp9_sad16x32_avg,
934       vp9_variance16x32, vp9_sub_pixel_variance16x32,
935       vp9_sub_pixel_avg_variance16x32, NULL, NULL, vp9_sad16x32x4d)
936
937   BFP(BLOCK_64X32, vp9_sad64x32, vp9_sad64x32_avg,
938       vp9_variance64x32, vp9_sub_pixel_variance64x32,
939       vp9_sub_pixel_avg_variance64x32, NULL, NULL, vp9_sad64x32x4d)
940
941   BFP(BLOCK_32X64, vp9_sad32x64, vp9_sad32x64_avg,
942       vp9_variance32x64, vp9_sub_pixel_variance32x64,
943       vp9_sub_pixel_avg_variance32x64, NULL, NULL, vp9_sad32x64x4d)
944
945   BFP(BLOCK_32X32, vp9_sad32x32, vp9_sad32x32_avg,
946       vp9_variance32x32, vp9_sub_pixel_variance32x32,
947       vp9_sub_pixel_avg_variance32x32, vp9_sad32x32x3, vp9_sad32x32x8,
948       vp9_sad32x32x4d)
949
950   BFP(BLOCK_64X64, vp9_sad64x64, vp9_sad64x64_avg,
951       vp9_variance64x64, vp9_sub_pixel_variance64x64,
952       vp9_sub_pixel_avg_variance64x64, vp9_sad64x64x3, vp9_sad64x64x8,
953       vp9_sad64x64x4d)
954
955   BFP(BLOCK_16X16, vp9_sad16x16, vp9_sad16x16_avg,
956       vp9_variance16x16, vp9_sub_pixel_variance16x16,
957       vp9_sub_pixel_avg_variance16x16, vp9_sad16x16x3, vp9_sad16x16x8,
958       vp9_sad16x16x4d)
959
960   BFP(BLOCK_16X8, vp9_sad16x8, vp9_sad16x8_avg,
961       vp9_variance16x8, vp9_sub_pixel_variance16x8,
962       vp9_sub_pixel_avg_variance16x8,
963       vp9_sad16x8x3, vp9_sad16x8x8, vp9_sad16x8x4d)
964
965   BFP(BLOCK_8X16, vp9_sad8x16, vp9_sad8x16_avg,
966       vp9_variance8x16, vp9_sub_pixel_variance8x16,
967       vp9_sub_pixel_avg_variance8x16,
968       vp9_sad8x16x3, vp9_sad8x16x8, vp9_sad8x16x4d)
969
970   BFP(BLOCK_8X8, vp9_sad8x8, vp9_sad8x8_avg,
971       vp9_variance8x8, vp9_sub_pixel_variance8x8,
972       vp9_sub_pixel_avg_variance8x8,
973       vp9_sad8x8x3, vp9_sad8x8x8, vp9_sad8x8x4d)
974
975   BFP(BLOCK_8X4, vp9_sad8x4, vp9_sad8x4_avg,
976       vp9_variance8x4, vp9_sub_pixel_variance8x4,
977       vp9_sub_pixel_avg_variance8x4, NULL, vp9_sad8x4x8, vp9_sad8x4x4d)
978
979   BFP(BLOCK_4X8, vp9_sad4x8, vp9_sad4x8_avg,
980       vp9_variance4x8, vp9_sub_pixel_variance4x8,
981       vp9_sub_pixel_avg_variance4x8, NULL, vp9_sad4x8x8, vp9_sad4x8x4d)
982
983   BFP(BLOCK_4X4, vp9_sad4x4, vp9_sad4x4_avg,
984       vp9_variance4x4, vp9_sub_pixel_variance4x4,
985       vp9_sub_pixel_avg_variance4x4,
986       vp9_sad4x4x3, vp9_sad4x4x8, vp9_sad4x4x4d)
987
988   cpi->full_search_sad = vp9_full_search_sad;
989   cpi->diamond_search_sad = vp9_diamond_search_sad;
990   cpi->refining_search_sad = vp9_refining_search_sad;
991
992   /* vp9_init_quantizer() is first called here. Add check in
993    * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
994    * called later when needed. This will avoid unnecessary calls of
995    * vp9_init_quantizer() for every frame.
996    */
997   vp9_init_quantizer(cpi);
998
999   vp9_loop_filter_init(cm);
1000
1001   cm->error.setjmp = 0;
1002
1003   return cpi;
1004 }
1005
1006 void vp9_remove_compressor(VP9_COMP *cpi) {
1007   unsigned int i;
1008
1009   if (!cpi)
1010     return;
1011
1012   if (cpi && (cpi->common.current_video_frame > 0)) {
1013 #if CONFIG_INTERNAL_STATS
1014
1015     vp9_clear_system_state();
1016
1017     // printf("\n8x8-4x4:%d-%d\n", cpi->t8x8_count, cpi->t4x4_count);
1018     if (cpi->pass != 1) {
1019       FILE *f = fopen("opsnr.stt", "a");
1020       double time_encoded = (cpi->last_end_time_stamp_seen
1021                              - cpi->first_time_stamp_ever) / 10000000.000;
1022       double total_encode_time = (cpi->time_receive_data +
1023                                   cpi->time_compress_data)   / 1000.000;
1024       double dr = (double)cpi->bytes * (double) 8 / (double)1000
1025                   / time_encoded;
1026
1027       if (cpi->b_calculate_psnr) {
1028         const double total_psnr =
1029             vpx_sse_to_psnr((double)cpi->total_samples, 255.0,
1030                             (double)cpi->total_sq_error);
1031         const double totalp_psnr =
1032             vpx_sse_to_psnr((double)cpi->totalp_samples, 255.0,
1033                             (double)cpi->totalp_sq_error);
1034         const double total_ssim = 100 * pow(cpi->summed_quality /
1035                                                 cpi->summed_weights, 8.0);
1036         const double totalp_ssim = 100 * pow(cpi->summedp_quality /
1037                                                 cpi->summedp_weights, 8.0);
1038
1039         fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
1040                 "VPXSSIM\tVPSSIMP\t  Time(ms)\n");
1041         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",
1042                 dr, cpi->total / cpi->count, total_psnr,
1043                 cpi->totalp / cpi->count, totalp_psnr, total_ssim, totalp_ssim,
1044                 total_encode_time);
1045       }
1046
1047       if (cpi->b_calculate_ssimg) {
1048         fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t  Time(ms)\n");
1049         fprintf(f, "%7.2f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
1050                 cpi->total_ssimg_y / cpi->count,
1051                 cpi->total_ssimg_u / cpi->count,
1052                 cpi->total_ssimg_v / cpi->count,
1053                 cpi->total_ssimg_all / cpi->count, total_encode_time);
1054       }
1055
1056       fclose(f);
1057     }
1058
1059 #endif
1060
1061 #if 0
1062     {
1063       printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
1064       printf("\n_frames recive_data encod_mb_row compress_frame  Total\n");
1065       printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
1066              cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
1067              cpi->time_compress_data / 1000,
1068              (cpi->time_receive_data + cpi->time_compress_data) / 1000);
1069     }
1070 #endif
1071   }
1072
1073 #if CONFIG_DENOISING
1074   vp9_denoiser_free(&(cpi->denoiser));
1075 #endif
1076
1077   dealloc_compressor_data(cpi);
1078   vpx_free(cpi->tok);
1079
1080   for (i = 0; i < sizeof(cpi->mbgraph_stats) /
1081                   sizeof(cpi->mbgraph_stats[0]); ++i) {
1082     vpx_free(cpi->mbgraph_stats[i].mb_stats);
1083   }
1084
1085   vp9_remove_common(&cpi->common);
1086   vpx_free(cpi);
1087
1088 #ifdef OUTPUT_YUV_DENOISED
1089   fclose(yuv_denoised_file);
1090 #endif
1091 #ifdef OUTPUT_YUV_SRC
1092   fclose(yuv_file);
1093 #endif
1094 #ifdef OUTPUT_YUV_REC
1095   fclose(yuv_rec_file);
1096 #endif
1097
1098 #if 0
1099
1100   if (keyfile)
1101     fclose(keyfile);
1102
1103   if (framepsnr)
1104     fclose(framepsnr);
1105
1106   if (kf_list)
1107     fclose(kf_list);
1108
1109 #endif
1110 }
1111 static int64_t get_sse(const uint8_t *a, int a_stride,
1112                        const uint8_t *b, int b_stride,
1113                        int width, int height) {
1114   const int dw = width % 16;
1115   const int dh = height % 16;
1116   int64_t total_sse = 0;
1117   unsigned int sse = 0;
1118   int sum = 0;
1119   int x, y;
1120
1121   if (dw > 0) {
1122     variance(&a[width - dw], a_stride, &b[width - dw], b_stride,
1123              dw, height, &sse, &sum);
1124     total_sse += sse;
1125   }
1126
1127   if (dh > 0) {
1128     variance(&a[(height - dh) * a_stride], a_stride,
1129              &b[(height - dh) * b_stride], b_stride,
1130              width - dw, dh, &sse, &sum);
1131     total_sse += sse;
1132   }
1133
1134   for (y = 0; y < height / 16; ++y) {
1135     const uint8_t *pa = a;
1136     const uint8_t *pb = b;
1137     for (x = 0; x < width / 16; ++x) {
1138       vp9_mse16x16(pa, a_stride, pb, b_stride, &sse);
1139       total_sse += sse;
1140
1141       pa += 16;
1142       pb += 16;
1143     }
1144
1145     a += 16 * a_stride;
1146     b += 16 * b_stride;
1147   }
1148
1149   return total_sse;
1150 }
1151
1152 typedef struct {
1153   double psnr[4];       // total/y/u/v
1154   uint64_t sse[4];      // total/y/u/v
1155   uint32_t samples[4];  // total/y/u/v
1156 } PSNR_STATS;
1157
1158 static void calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b,
1159                       PSNR_STATS *psnr) {
1160   const int widths[3]        = {a->y_width,  a->uv_width,  a->uv_width };
1161   const int heights[3]       = {a->y_height, a->uv_height, a->uv_height};
1162   const uint8_t *a_planes[3] = {a->y_buffer, a->u_buffer,  a->v_buffer };
1163   const int a_strides[3]     = {a->y_stride, a->uv_stride, a->uv_stride};
1164   const uint8_t *b_planes[3] = {b->y_buffer, b->u_buffer,  b->v_buffer };
1165   const int b_strides[3]     = {b->y_stride, b->uv_stride, b->uv_stride};
1166   int i;
1167   uint64_t total_sse = 0;
1168   uint32_t total_samples = 0;
1169
1170   for (i = 0; i < 3; ++i) {
1171     const int w = widths[i];
1172     const int h = heights[i];
1173     const uint32_t samples = w * h;
1174     const uint64_t sse = get_sse(a_planes[i], a_strides[i],
1175                                  b_planes[i], b_strides[i],
1176                                  w, h);
1177     psnr->sse[1 + i] = sse;
1178     psnr->samples[1 + i] = samples;
1179     psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, 255.0, (double)sse);
1180
1181     total_sse += sse;
1182     total_samples += samples;
1183   }
1184
1185   psnr->sse[0] = total_sse;
1186   psnr->samples[0] = total_samples;
1187   psnr->psnr[0] = vpx_sse_to_psnr((double)total_samples, 255.0,
1188                                   (double)total_sse);
1189 }
1190
1191 static void generate_psnr_packet(VP9_COMP *cpi) {
1192   struct vpx_codec_cx_pkt pkt;
1193   int i;
1194   PSNR_STATS psnr;
1195   calc_psnr(cpi->Source, cpi->common.frame_to_show, &psnr);
1196   for (i = 0; i < 4; ++i) {
1197     pkt.data.psnr.samples[i] = psnr.samples[i];
1198     pkt.data.psnr.sse[i] = psnr.sse[i];
1199     pkt.data.psnr.psnr[i] = psnr.psnr[i];
1200   }
1201   pkt.kind = VPX_CODEC_PSNR_PKT;
1202   vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
1203 }
1204
1205 int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags) {
1206   if (ref_frame_flags > 7)
1207     return -1;
1208
1209   cpi->ref_frame_flags = ref_frame_flags;
1210   return 0;
1211 }
1212
1213 void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags) {
1214   cpi->ext_refresh_golden_frame = (ref_frame_flags & VP9_GOLD_FLAG) != 0;
1215   cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & VP9_ALT_FLAG) != 0;
1216   cpi->ext_refresh_last_frame = (ref_frame_flags & VP9_LAST_FLAG) != 0;
1217   cpi->ext_refresh_frame_flags_pending = 1;
1218 }
1219
1220 static YV12_BUFFER_CONFIG *get_vp9_ref_frame_buffer(VP9_COMP *cpi,
1221                                 VP9_REFFRAME ref_frame_flag) {
1222   MV_REFERENCE_FRAME ref_frame = NONE;
1223   if (ref_frame_flag == VP9_LAST_FLAG)
1224     ref_frame = LAST_FRAME;
1225   else if (ref_frame_flag == VP9_GOLD_FLAG)
1226     ref_frame = GOLDEN_FRAME;
1227   else if (ref_frame_flag == VP9_ALT_FLAG)
1228     ref_frame = ALTREF_FRAME;
1229
1230   return ref_frame == NONE ? NULL : get_ref_frame_buffer(cpi, ref_frame);
1231 }
1232
1233 int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
1234                            YV12_BUFFER_CONFIG *sd) {
1235   YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
1236   if (cfg) {
1237     vp8_yv12_copy_frame(cfg, sd);
1238     return 0;
1239   } else {
1240     return -1;
1241   }
1242 }
1243
1244 int vp9_get_reference_enc(VP9_COMP *cpi, int index, YV12_BUFFER_CONFIG **fb) {
1245   VP9_COMMON *cm = &cpi->common;
1246
1247   if (index < 0 || index >= REF_FRAMES)
1248     return -1;
1249
1250   *fb = &cm->frame_bufs[cm->ref_frame_map[index]].buf;
1251   return 0;
1252 }
1253
1254 int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
1255                           YV12_BUFFER_CONFIG *sd) {
1256   YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
1257   if (cfg) {
1258     vp8_yv12_copy_frame(sd, cfg);
1259     return 0;
1260   } else {
1261     return -1;
1262   }
1263 }
1264
1265 int vp9_update_entropy(VP9_COMP * cpi, int update) {
1266   cpi->ext_refresh_frame_context = update;
1267   cpi->ext_refresh_frame_context_pending = 1;
1268   return 0;
1269 }
1270
1271
1272 #if defined(OUTPUT_YUV_SRC) || defined(OUTPUT_YUV_DENOISED)
1273 void vp9_write_yuv_frame(YV12_BUFFER_CONFIG *s, FILE *f) {
1274   uint8_t *src = s->y_buffer;
1275   int h = s->y_height;
1276
1277   do {
1278     fwrite(src, s->y_width, 1, f);
1279     src += s->y_stride;
1280   } while (--h);
1281
1282   src = s->u_buffer;
1283   h = s->uv_height;
1284
1285   do {
1286     fwrite(src, s->uv_width, 1, f);
1287     src += s->uv_stride;
1288   } while (--h);
1289
1290   src = s->v_buffer;
1291   h = s->uv_height;
1292
1293   do {
1294     fwrite(src, s->uv_width, 1, f);
1295     src += s->uv_stride;
1296   } while (--h);
1297 }
1298 #endif
1299
1300 #ifdef OUTPUT_YUV_REC
1301 void vp9_write_yuv_rec_frame(VP9_COMMON *cm) {
1302   YV12_BUFFER_CONFIG *s = cm->frame_to_show;
1303   uint8_t *src = s->y_buffer;
1304   int h = cm->height;
1305
1306   do {
1307     fwrite(src, s->y_width, 1,  yuv_rec_file);
1308     src += s->y_stride;
1309   } while (--h);
1310
1311   src = s->u_buffer;
1312   h = s->uv_height;
1313
1314   do {
1315     fwrite(src, s->uv_width, 1,  yuv_rec_file);
1316     src += s->uv_stride;
1317   } while (--h);
1318
1319   src = s->v_buffer;
1320   h = s->uv_height;
1321
1322   do {
1323     fwrite(src, s->uv_width, 1, yuv_rec_file);
1324     src += s->uv_stride;
1325   } while (--h);
1326
1327 #if CONFIG_ALPHA
1328   if (s->alpha_buffer) {
1329     src = s->alpha_buffer;
1330     h = s->alpha_height;
1331     do {
1332       fwrite(src, s->alpha_width, 1,  yuv_rec_file);
1333       src += s->alpha_stride;
1334     } while (--h);
1335   }
1336 #endif
1337
1338   fflush(yuv_rec_file);
1339 }
1340 #endif
1341
1342 static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
1343                                                 YV12_BUFFER_CONFIG *dst) {
1344   // TODO(dkovalev): replace YV12_BUFFER_CONFIG with vpx_image_t
1345   int i;
1346   const uint8_t *const srcs[4] = {src->y_buffer, src->u_buffer, src->v_buffer,
1347                                   src->alpha_buffer};
1348   const int src_strides[4] = {src->y_stride, src->uv_stride, src->uv_stride,
1349                               src->alpha_stride};
1350   const int src_widths[4] = {src->y_crop_width, src->uv_crop_width,
1351                              src->uv_crop_width, src->y_crop_width};
1352   const int src_heights[4] = {src->y_crop_height, src->uv_crop_height,
1353                               src->uv_crop_height, src->y_crop_height};
1354   uint8_t *const dsts[4] = {dst->y_buffer, dst->u_buffer, dst->v_buffer,
1355                             dst->alpha_buffer};
1356   const int dst_strides[4] = {dst->y_stride, dst->uv_stride, dst->uv_stride,
1357                               dst->alpha_stride};
1358   const int dst_widths[4] = {dst->y_crop_width, dst->uv_crop_width,
1359                              dst->uv_crop_width, dst->y_crop_width};
1360   const int dst_heights[4] = {dst->y_crop_height, dst->uv_crop_height,
1361                               dst->uv_crop_height, dst->y_crop_height};
1362
1363   for (i = 0; i < MAX_MB_PLANE; ++i)
1364     vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
1365                      dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
1366
1367   // TODO(hkuang): Call C version explicitly
1368   // as neon version only expand border size 32.
1369   vp8_yv12_extend_frame_borders_c(dst);
1370 }
1371
1372 static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
1373                                    YV12_BUFFER_CONFIG *dst) {
1374   const int src_w = src->y_crop_width;
1375   const int src_h = src->y_crop_height;
1376   const int dst_w = dst->y_crop_width;
1377   const int dst_h = dst->y_crop_height;
1378   const uint8_t *const srcs[4] = {src->y_buffer, src->u_buffer, src->v_buffer,
1379                                   src->alpha_buffer};
1380   const int src_strides[4] = {src->y_stride, src->uv_stride, src->uv_stride,
1381                               src->alpha_stride};
1382   uint8_t *const dsts[4] = {dst->y_buffer, dst->u_buffer, dst->v_buffer,
1383                             dst->alpha_buffer};
1384   const int dst_strides[4] = {dst->y_stride, dst->uv_stride, dst->uv_stride,
1385                               dst->alpha_stride};
1386   const InterpKernel *const kernel = vp9_get_interp_kernel(EIGHTTAP);
1387   int x, y, i;
1388
1389   for (y = 0; y < dst_h; y += 16) {
1390     for (x = 0; x < dst_w; x += 16) {
1391       for (i = 0; i < MAX_MB_PLANE; ++i) {
1392         const int factor = (i == 0 || i == 3 ? 1 : 2);
1393         const int x_q4 = x * (16 / factor) * src_w / dst_w;
1394         const int y_q4 = y * (16 / factor) * src_h / dst_h;
1395         const int src_stride = src_strides[i];
1396         const int dst_stride = dst_strides[i];
1397         const uint8_t *src_ptr = srcs[i] + (y / factor) * src_h / dst_h *
1398                                      src_stride + (x / factor) * src_w / dst_w;
1399         uint8_t *dst_ptr = dsts[i] + (y / factor) * dst_stride + (x / factor);
1400
1401         vp9_convolve8(src_ptr, src_stride, dst_ptr, dst_stride,
1402                       kernel[x_q4 & 0xf], 16 * src_w / dst_w,
1403                       kernel[y_q4 & 0xf], 16 * src_h / dst_h,
1404                       16 / factor, 16 / factor);
1405       }
1406     }
1407   }
1408
1409   // TODO(hkuang): Call C version explicitly
1410   // as neon version only expand border size 32.
1411   vp8_yv12_extend_frame_borders_c(dst);
1412 }
1413
1414 #define WRITE_RECON_BUFFER 0
1415 #if WRITE_RECON_BUFFER
1416 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame) {
1417   FILE *yframe;
1418   int i;
1419   char filename[255];
1420
1421   snprintf(filename, sizeof(filename), "cx\\y%04d.raw", this_frame);
1422   yframe = fopen(filename, "wb");
1423
1424   for (i = 0; i < frame->y_height; i++)
1425     fwrite(frame->y_buffer + i * frame->y_stride,
1426            frame->y_width, 1, yframe);
1427
1428   fclose(yframe);
1429   snprintf(filename, sizeof(filename), "cx\\u%04d.raw", this_frame);
1430   yframe = fopen(filename, "wb");
1431
1432   for (i = 0; i < frame->uv_height; i++)
1433     fwrite(frame->u_buffer + i * frame->uv_stride,
1434            frame->uv_width, 1, yframe);
1435
1436   fclose(yframe);
1437   snprintf(filename, sizeof(filename), "cx\\v%04d.raw", this_frame);
1438   yframe = fopen(filename, "wb");
1439
1440   for (i = 0; i < frame->uv_height; i++)
1441     fwrite(frame->v_buffer + i * frame->uv_stride,
1442            frame->uv_width, 1, yframe);
1443
1444   fclose(yframe);
1445 }
1446 #endif
1447
1448 // Function to test for conditions that indicate we should loop
1449 // back and recode a frame.
1450 static int recode_loop_test(const VP9_COMP *cpi,
1451                             int high_limit, int low_limit,
1452                             int q, int maxq, int minq) {
1453   const VP9_COMMON *const cm = &cpi->common;
1454   const RATE_CONTROL *const rc = &cpi->rc;
1455   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1456   int force_recode = 0;
1457
1458   // Special case trap if maximum allowed frame size exceeded.
1459   if (rc->projected_frame_size > rc->max_frame_bandwidth) {
1460     force_recode = 1;
1461
1462   // Is frame recode allowed.
1463   // Yes if either recode mode 1 is selected or mode 2 is selected
1464   // and the frame is a key frame, golden frame or alt_ref_frame
1465   } else if ((cpi->sf.recode_loop == ALLOW_RECODE) ||
1466              ((cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF) &&
1467               (cm->frame_type == KEY_FRAME ||
1468                cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
1469     // General over and under shoot tests
1470     if ((rc->projected_frame_size > high_limit && q < maxq) ||
1471         (rc->projected_frame_size < low_limit && q > minq)) {
1472       force_recode = 1;
1473     } else if (cpi->oxcf.rc_mode == VPX_CQ) {
1474       // Deal with frame undershoot and whether or not we are
1475       // below the automatically set cq level.
1476       if (q > oxcf->cq_level &&
1477           rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
1478         force_recode = 1;
1479       }
1480     }
1481   }
1482   return force_recode;
1483 }
1484
1485 void vp9_update_reference_frames(VP9_COMP *cpi) {
1486   VP9_COMMON * const cm = &cpi->common;
1487
1488   // At this point the new frame has been encoded.
1489   // If any buffer copy / swapping is signaled it should be done here.
1490   if (cm->frame_type == KEY_FRAME) {
1491     ref_cnt_fb(cm->frame_bufs,
1492                &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
1493     ref_cnt_fb(cm->frame_bufs,
1494                &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
1495   } else if (!cpi->multi_arf_allowed && cpi->refresh_golden_frame &&
1496              cpi->rc.is_src_frame_alt_ref && !cpi->use_svc) {
1497     /* Preserve the previously existing golden frame and update the frame in
1498      * the alt ref slot instead. This is highly specific to the current use of
1499      * alt-ref as a forward reference, and this needs to be generalized as
1500      * other uses are implemented (like RTC/temporal scaling)
1501      *
1502      * The update to the buffer in the alt ref slot was signaled in
1503      * vp9_pack_bitstream(), now swap the buffer pointers so that it's treated
1504      * as the golden frame next time.
1505      */
1506     int tmp;
1507
1508     ref_cnt_fb(cm->frame_bufs,
1509                &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
1510
1511     tmp = cpi->alt_fb_idx;
1512     cpi->alt_fb_idx = cpi->gld_fb_idx;
1513     cpi->gld_fb_idx = tmp;
1514   } else { /* For non key/golden frames */
1515     if (cpi->refresh_alt_ref_frame) {
1516       int arf_idx = cpi->alt_fb_idx;
1517       if ((cpi->pass == 2) && cpi->multi_arf_enabled) {
1518         const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
1519         arf_idx = gf_group->arf_update_idx[gf_group->index];
1520       }
1521
1522       ref_cnt_fb(cm->frame_bufs,
1523                  &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
1524     }
1525
1526     if (cpi->refresh_golden_frame) {
1527       ref_cnt_fb(cm->frame_bufs,
1528                  &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
1529     }
1530   }
1531
1532   if (cpi->refresh_last_frame) {
1533     ref_cnt_fb(cm->frame_bufs,
1534                &cm->ref_frame_map[cpi->lst_fb_idx], cm->new_fb_idx);
1535   }
1536 #if CONFIG_DENOISING
1537   vp9_denoiser_update_frame_info(&cpi->denoiser,
1538                                 *cpi->Source,
1539                                 cpi->common.frame_type,
1540                                 cpi->refresh_alt_ref_frame,
1541                                 cpi->refresh_golden_frame,
1542                                 cpi->refresh_last_frame);
1543 #endif
1544 }
1545
1546 static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) {
1547   MACROBLOCKD *xd = &cpi->mb.e_mbd;
1548   struct loopfilter *lf = &cm->lf;
1549   if (xd->lossless) {
1550       lf->filter_level = 0;
1551   } else {
1552     struct vpx_usec_timer timer;
1553
1554     vp9_clear_system_state();
1555
1556     vpx_usec_timer_start(&timer);
1557
1558     vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.lpf_pick);
1559
1560     vpx_usec_timer_mark(&timer);
1561     cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
1562   }
1563
1564   if (lf->filter_level > 0) {
1565     vp9_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
1566   }
1567
1568   vp9_extend_frame_inner_borders(cm->frame_to_show);
1569 }
1570
1571 void vp9_scale_references(VP9_COMP *cpi) {
1572   VP9_COMMON *cm = &cpi->common;
1573   MV_REFERENCE_FRAME ref_frame;
1574   const VP9_REFFRAME ref_mask[3] = {VP9_LAST_FLAG, VP9_GOLD_FLAG, VP9_ALT_FLAG};
1575
1576   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
1577     const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)];
1578     const YV12_BUFFER_CONFIG *const ref = &cm->frame_bufs[idx].buf;
1579
1580     // Need to convert from VP9_REFFRAME to index into ref_mask (subtract 1).
1581     if ((cpi->ref_frame_flags & ref_mask[ref_frame - 1]) &&
1582         (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height)) {
1583       const int new_fb = get_free_fb(cm);
1584       vp9_realloc_frame_buffer(&cm->frame_bufs[new_fb].buf,
1585                                cm->width, cm->height,
1586                                cm->subsampling_x, cm->subsampling_y,
1587                                VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL);
1588       scale_and_extend_frame(ref, &cm->frame_bufs[new_fb].buf);
1589       cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
1590     } else {
1591       cpi->scaled_ref_idx[ref_frame - 1] = idx;
1592       cm->frame_bufs[idx].ref_count++;
1593     }
1594   }
1595 }
1596
1597 static void release_scaled_references(VP9_COMP *cpi) {
1598   VP9_COMMON *cm = &cpi->common;
1599   int i;
1600
1601   for (i = 0; i < 3; i++)
1602     cm->frame_bufs[cpi->scaled_ref_idx[i]].ref_count--;
1603 }
1604
1605 static void full_to_model_count(unsigned int *model_count,
1606                                 unsigned int *full_count) {
1607   int n;
1608   model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
1609   model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
1610   model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
1611   for (n = THREE_TOKEN; n < EOB_TOKEN; ++n)
1612     model_count[TWO_TOKEN] += full_count[n];
1613   model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
1614 }
1615
1616 static void full_to_model_counts(vp9_coeff_count_model *model_count,
1617                                  vp9_coeff_count *full_count) {
1618   int i, j, k, l;
1619
1620   for (i = 0; i < PLANE_TYPES; ++i)
1621     for (j = 0; j < REF_TYPES; ++j)
1622       for (k = 0; k < COEF_BANDS; ++k)
1623         for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
1624           full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
1625 }
1626
1627 #if 0 && CONFIG_INTERNAL_STATS
1628 static void output_frame_level_debug_stats(VP9_COMP *cpi) {
1629   VP9_COMMON *const cm = &cpi->common;
1630   FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
1631   int recon_err;
1632
1633   vp9_clear_system_state();
1634
1635   recon_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
1636
1637   if (cpi->twopass.total_left_stats.coded_error != 0.0)
1638     fprintf(f, "%10u %10d %10d %10d %10d"
1639         "%10"PRId64" %10"PRId64" %10"PRId64" %10"PRId64" %10d "
1640         "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
1641         "%6d %6d %5d %5d %5d "
1642         "%10"PRId64" %10.3lf"
1643         "%10lf %8u %10d %10d %10d\n",
1644         cpi->common.current_video_frame, cpi->rc.this_frame_target,
1645         cpi->rc.projected_frame_size,
1646         cpi->rc.projected_frame_size / cpi->common.MBs,
1647         (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
1648         cpi->rc.vbr_bits_off_target,
1649         cpi->rc.total_target_vs_actual,
1650         (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
1651         cpi->rc.total_actual_bits, cm->base_qindex,
1652         vp9_convert_qindex_to_q(cm->base_qindex),
1653         (double)vp9_dc_quant(cm->base_qindex, 0) / 4.0,
1654         cpi->rc.avg_q,
1655         vp9_convert_qindex_to_q(cpi->rc.ni_av_qi),
1656         vp9_convert_qindex_to_q(cpi->oxcf.cq_level),
1657         cpi->refresh_last_frame, cpi->refresh_golden_frame,
1658         cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
1659         cpi->twopass.bits_left,
1660         cpi->twopass.total_left_stats.coded_error,
1661         cpi->twopass.bits_left /
1662             (1 + cpi->twopass.total_left_stats.coded_error),
1663         cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
1664         cpi->twopass.kf_zeromotion_pct);
1665
1666   fclose(f);
1667
1668   if (0) {
1669     FILE *const fmodes = fopen("Modes.stt", "a");
1670     int i;
1671
1672     fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
1673             cm->frame_type, cpi->refresh_golden_frame,
1674             cpi->refresh_alt_ref_frame);
1675
1676     for (i = 0; i < MAX_MODES; ++i)
1677       fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
1678
1679     fprintf(fmodes, "\n");
1680
1681     fclose(fmodes);
1682   }
1683 }
1684 #endif
1685
1686 static void encode_without_recode_loop(VP9_COMP *cpi,
1687                                        int q) {
1688   VP9_COMMON *const cm = &cpi->common;
1689   vp9_clear_system_state();
1690   vp9_set_quantizer(cm, q);
1691   setup_frame(cpi);
1692   // Variance adaptive and in frame q adjustment experiments are mutually
1693   // exclusive.
1694   if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
1695     vp9_vaq_frame_setup(cpi);
1696   } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
1697     vp9_setup_in_frame_q_adj(cpi);
1698   } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
1699     vp9_cyclic_refresh_setup(cpi);
1700   }
1701   // transform / motion compensation build reconstruction frame
1702   vp9_encode_frame(cpi);
1703
1704   // Update the skip mb flag probabilities based on the distribution
1705   // seen in the last encoder iteration.
1706   // update_base_skip_probs(cpi);
1707   vp9_clear_system_state();
1708 }
1709
1710 static void encode_with_recode_loop(VP9_COMP *cpi,
1711                                     size_t *size,
1712                                     uint8_t *dest,
1713                                     int q,
1714                                     int bottom_index,
1715                                     int top_index) {
1716   VP9_COMMON *const cm = &cpi->common;
1717   RATE_CONTROL *const rc = &cpi->rc;
1718   int loop_count = 0;
1719   int loop = 0;
1720   int overshoot_seen = 0;
1721   int undershoot_seen = 0;
1722   int q_low = bottom_index, q_high = top_index;
1723   int frame_over_shoot_limit;
1724   int frame_under_shoot_limit;
1725
1726   // Decide frame size bounds
1727   vp9_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
1728                                    &frame_under_shoot_limit,
1729                                    &frame_over_shoot_limit);
1730
1731   do {
1732     vp9_clear_system_state();
1733
1734     vp9_set_quantizer(cm, q);
1735
1736     if (loop_count == 0)
1737       setup_frame(cpi);
1738
1739     // Variance adaptive and in frame q adjustment experiments are mutually
1740     // exclusive.
1741     if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
1742       vp9_vaq_frame_setup(cpi);
1743     } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
1744       vp9_setup_in_frame_q_adj(cpi);
1745     }
1746
1747     // transform / motion compensation build reconstruction frame
1748     vp9_encode_frame(cpi);
1749
1750     // Update the skip mb flag probabilities based on the distribution
1751     // seen in the last encoder iteration.
1752     // update_base_skip_probs(cpi);
1753
1754     vp9_clear_system_state();
1755
1756     // Dummy pack of the bitstream using up to date stats to get an
1757     // accurate estimate of output frame size to determine if we need
1758     // to recode.
1759     if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
1760       save_coding_context(cpi);
1761       cpi->dummy_packing = 1;
1762       if (!cpi->sf.use_nonrd_pick_mode)
1763         vp9_pack_bitstream(cpi, dest, size);
1764
1765       rc->projected_frame_size = (int)(*size) << 3;
1766       restore_coding_context(cpi);
1767
1768       if (frame_over_shoot_limit == 0)
1769         frame_over_shoot_limit = 1;
1770     }
1771
1772     if (cpi->oxcf.rc_mode == VPX_Q) {
1773       loop = 0;
1774     } else {
1775       if ((cm->frame_type == KEY_FRAME) &&
1776            rc->this_key_frame_forced &&
1777            (rc->projected_frame_size < rc->max_frame_bandwidth)) {
1778         int last_q = q;
1779         int kf_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
1780
1781         int high_err_target = cpi->ambient_err;
1782         int low_err_target = cpi->ambient_err >> 1;
1783
1784         // Prevent possible divide by zero error below for perfect KF
1785         kf_err += !kf_err;
1786
1787         // The key frame is not good enough or we can afford
1788         // to make it better without undue risk of popping.
1789         if ((kf_err > high_err_target &&
1790              rc->projected_frame_size <= frame_over_shoot_limit) ||
1791             (kf_err > low_err_target &&
1792              rc->projected_frame_size <= frame_under_shoot_limit)) {
1793           // Lower q_high
1794           q_high = q > q_low ? q - 1 : q_low;
1795
1796           // Adjust Q
1797           q = (q * high_err_target) / kf_err;
1798           q = MIN(q, (q_high + q_low) >> 1);
1799         } else if (kf_err < low_err_target &&
1800                    rc->projected_frame_size >= frame_under_shoot_limit) {
1801           // The key frame is much better than the previous frame
1802           // Raise q_low
1803           q_low = q < q_high ? q + 1 : q_high;
1804
1805           // Adjust Q
1806           q = (q * low_err_target) / kf_err;
1807           q = MIN(q, (q_high + q_low + 1) >> 1);
1808         }
1809
1810         // Clamp Q to upper and lower limits:
1811         q = clamp(q, q_low, q_high);
1812
1813         loop = q != last_q;
1814       } else if (recode_loop_test(
1815           cpi, frame_over_shoot_limit, frame_under_shoot_limit,
1816           q, MAX(q_high, top_index), bottom_index)) {
1817         // Is the projected frame size out of range and are we allowed
1818         // to attempt to recode.
1819         int last_q = q;
1820         int retries = 0;
1821
1822         // Frame size out of permitted range:
1823         // Update correction factor & compute new Q to try...
1824
1825         // Frame is too large
1826         if (rc->projected_frame_size > rc->this_frame_target) {
1827           // Special case if the projected size is > the max allowed.
1828           if (rc->projected_frame_size >= rc->max_frame_bandwidth)
1829             q_high = rc->worst_quality;
1830
1831           // Raise Qlow as to at least the current value
1832           q_low = q < q_high ? q + 1 : q_high;
1833
1834           if (undershoot_seen || loop_count > 1) {
1835             // Update rate_correction_factor unless
1836             vp9_rc_update_rate_correction_factors(cpi, 1);
1837
1838             q = (q_high + q_low + 1) / 2;
1839           } else {
1840             // Update rate_correction_factor unless
1841             vp9_rc_update_rate_correction_factors(cpi, 0);
1842
1843             q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
1844                                    bottom_index, MAX(q_high, top_index));
1845
1846             while (q < q_low && retries < 10) {
1847               vp9_rc_update_rate_correction_factors(cpi, 0);
1848               q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
1849                                      bottom_index, MAX(q_high, top_index));
1850               retries++;
1851             }
1852           }
1853
1854           overshoot_seen = 1;
1855         } else {
1856           // Frame is too small
1857           q_high = q > q_low ? q - 1 : q_low;
1858
1859           if (overshoot_seen || loop_count > 1) {
1860             vp9_rc_update_rate_correction_factors(cpi, 1);
1861             q = (q_high + q_low) / 2;
1862           } else {
1863             vp9_rc_update_rate_correction_factors(cpi, 0);
1864             q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
1865                                    bottom_index, top_index);
1866             // Special case reset for qlow for constrained quality.
1867             // This should only trigger where there is very substantial
1868             // undershoot on a frame and the auto cq level is above
1869             // the user passsed in value.
1870             if (cpi->oxcf.rc_mode == VPX_CQ &&
1871                 q < q_low) {
1872               q_low = q;
1873             }
1874
1875             while (q > q_high && retries < 10) {
1876               vp9_rc_update_rate_correction_factors(cpi, 0);
1877               q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
1878                                      bottom_index, top_index);
1879               retries++;
1880             }
1881           }
1882
1883           undershoot_seen = 1;
1884         }
1885
1886         // Clamp Q to upper and lower limits:
1887         q = clamp(q, q_low, q_high);
1888
1889         loop = q != last_q;
1890       } else {
1891         loop = 0;
1892       }
1893     }
1894
1895     // Special case for overlay frame.
1896     if (rc->is_src_frame_alt_ref &&
1897         rc->projected_frame_size < rc->max_frame_bandwidth)
1898       loop = 0;
1899
1900     if (loop) {
1901       loop_count++;
1902
1903 #if CONFIG_INTERNAL_STATS
1904       cpi->tot_recode_hits++;
1905 #endif
1906     }
1907   } while (loop);
1908 }
1909
1910 static void get_ref_frame_flags(VP9_COMP *cpi) {
1911   if (cpi->refresh_last_frame & cpi->refresh_golden_frame)
1912     cpi->gold_is_last = 1;
1913   else if (cpi->refresh_last_frame ^ cpi->refresh_golden_frame)
1914     cpi->gold_is_last = 0;
1915
1916   if (cpi->refresh_last_frame & cpi->refresh_alt_ref_frame)
1917     cpi->alt_is_last = 1;
1918   else if (cpi->refresh_last_frame ^ cpi->refresh_alt_ref_frame)
1919     cpi->alt_is_last = 0;
1920
1921   if (cpi->refresh_alt_ref_frame & cpi->refresh_golden_frame)
1922     cpi->gold_is_alt = 1;
1923   else if (cpi->refresh_alt_ref_frame ^ cpi->refresh_golden_frame)
1924     cpi->gold_is_alt = 0;
1925
1926   cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
1927
1928   if (cpi->gold_is_last)
1929     cpi->ref_frame_flags &= ~VP9_GOLD_FLAG;
1930
1931   if (cpi->rc.frames_till_gf_update_due == INT_MAX)
1932     cpi->ref_frame_flags &= ~VP9_GOLD_FLAG;
1933
1934   if (cpi->alt_is_last)
1935     cpi->ref_frame_flags &= ~VP9_ALT_FLAG;
1936
1937   if (cpi->gold_is_alt)
1938     cpi->ref_frame_flags &= ~VP9_ALT_FLAG;
1939 }
1940
1941 static void set_ext_overrides(VP9_COMP *cpi) {
1942   // Overrides the defaults with the externally supplied values with
1943   // vp9_update_reference() and vp9_update_entropy() calls
1944   // Note: The overrides are valid only for the next frame passed
1945   // to encode_frame_to_data_rate() function
1946   if (cpi->ext_refresh_frame_context_pending) {
1947     cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
1948     cpi->ext_refresh_frame_context_pending = 0;
1949   }
1950   if (cpi->ext_refresh_frame_flags_pending) {
1951     cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
1952     cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
1953     cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
1954     cpi->ext_refresh_frame_flags_pending = 0;
1955   }
1956 }
1957
1958 YV12_BUFFER_CONFIG *vp9_scale_if_required(VP9_COMMON *cm,
1959                                           YV12_BUFFER_CONFIG *unscaled,
1960                                           YV12_BUFFER_CONFIG *scaled) {
1961   if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
1962       cm->mi_rows * MI_SIZE != unscaled->y_height) {
1963     scale_and_extend_frame_nonnormative(unscaled, scaled);
1964     return scaled;
1965   } else {
1966     return unscaled;
1967   }
1968 }
1969
1970 static void encode_frame_to_data_rate(VP9_COMP *cpi,
1971                                       size_t *size,
1972                                       uint8_t *dest,
1973                                       unsigned int *frame_flags) {
1974   VP9_COMMON *const cm = &cpi->common;
1975   TX_SIZE t;
1976   int q;
1977   int top_index;
1978   int bottom_index;
1979
1980   const SPEED_FEATURES *const sf = &cpi->sf;
1981   const unsigned int max_mv_def = MIN(cm->width, cm->height);
1982   struct segmentation *const seg = &cm->seg;
1983   set_ext_overrides(cpi);
1984
1985   cpi->Source = vp9_scale_if_required(cm, cpi->un_scaled_source,
1986                                       &cpi->scaled_source);
1987
1988   if (cpi->unscaled_last_source != NULL)
1989     cpi->Last_Source = vp9_scale_if_required(cm, cpi->unscaled_last_source,
1990                                              &cpi->scaled_last_source);
1991
1992   vp9_scale_references(cpi);
1993
1994   vp9_clear_system_state();
1995
1996   // Enable or disable mode based tweaking of the zbin.
1997   // For 2 pass only used where GF/ARF prediction quality
1998   // is above a threshold.
1999   cpi->zbin_mode_boost = 0;
2000   cpi->zbin_mode_boost_enabled = 0;
2001
2002   // Current default encoder behavior for the altref sign bias.
2003   cm->ref_frame_sign_bias[ALTREF_FRAME] = cpi->rc.source_alt_ref_active;
2004
2005   // Set default state for segment based loop filter update flags.
2006   cm->lf.mode_ref_delta_update = 0;
2007
2008   // Initialize cpi->mv_step_param to default based on max resolution.
2009   cpi->mv_step_param = vp9_init_search_range(sf, max_mv_def);
2010   // Initialize cpi->max_mv_magnitude and cpi->mv_step_param if appropriate.
2011   if (sf->mv.auto_mv_step_size) {
2012     if (frame_is_intra_only(cm)) {
2013       // Initialize max_mv_magnitude for use in the first INTER frame
2014       // after a key/intra-only frame.
2015       cpi->max_mv_magnitude = max_mv_def;
2016     } else {
2017       if (cm->show_frame)
2018         // Allow mv_steps to correspond to twice the max mv magnitude found
2019         // in the previous frame, capped by the default max_mv_magnitude based
2020         // on resolution.
2021         cpi->mv_step_param = vp9_init_search_range(sf, MIN(max_mv_def, 2 *
2022                                  cpi->max_mv_magnitude));
2023       cpi->max_mv_magnitude = 0;
2024     }
2025   }
2026
2027   // Set various flags etc to special state if it is a key frame.
2028   if (frame_is_intra_only(cm)) {
2029     // Reset the loop filter deltas and segmentation map.
2030     vp9_reset_segment_features(&cm->seg);
2031
2032     // If segmentation is enabled force a map update for key frames.
2033     if (seg->enabled) {
2034       seg->update_map = 1;
2035       seg->update_data = 1;
2036     }
2037
2038     // The alternate reference frame cannot be active for a key frame.
2039     cpi->rc.source_alt_ref_active = 0;
2040
2041     cm->error_resilient_mode = (cpi->oxcf.error_resilient_mode != 0);
2042     cm->frame_parallel_decoding_mode =
2043       (cpi->oxcf.frame_parallel_decoding_mode != 0);
2044
2045     // By default, encoder assumes decoder can use prev_mi.
2046     cm->coding_use_prev_mi = 1;
2047     if (cm->error_resilient_mode) {
2048       cm->coding_use_prev_mi = 0;
2049       cm->frame_parallel_decoding_mode = 1;
2050       cm->reset_frame_context = 0;
2051       cm->refresh_frame_context = 0;
2052     } else if (cm->intra_only) {
2053       // Only reset the current context.
2054       cm->reset_frame_context = 2;
2055     }
2056   }
2057
2058   // Configure experimental use of segmentation for enhanced coding of
2059   // static regions if indicated.
2060   // Only allowed in second pass of two pass (as requires lagged coding)
2061   // and if the relevant speed feature flag is set.
2062   if (cpi->pass == 2 && cpi->sf.static_segmentation)
2063     configure_static_seg_features(cpi);
2064
2065   // For 1 pass CBR, check if we are dropping this frame.
2066   // Never drop on key frame.
2067   if (cpi->pass == 0 &&
2068       cpi->oxcf.rc_mode == VPX_CBR &&
2069       cm->frame_type != KEY_FRAME) {
2070     if (vp9_rc_drop_frame(cpi)) {
2071       vp9_rc_postencode_update_drop_frame(cpi);
2072       ++cm->current_video_frame;
2073       return;
2074     }
2075   }
2076
2077   vp9_clear_system_state();
2078
2079 #if CONFIG_VP9_POSTPROC
2080   if (cpi->oxcf.noise_sensitivity > 0) {
2081     int l = 0;
2082     switch (cpi->oxcf.noise_sensitivity) {
2083       case 1:
2084         l = 20;
2085         break;
2086       case 2:
2087         l = 40;
2088         break;
2089       case 3:
2090         l = 60;
2091         break;
2092       case 4:
2093       case 5:
2094         l = 100;
2095         break;
2096       case 6:
2097         l = 150;
2098         break;
2099     }
2100     vp9_denoise(cpi->Source, cpi->Source, l);
2101   }
2102 #endif
2103
2104 #ifdef OUTPUT_YUV_DENOISED
2105   vp9_write_yuv_frame(&cpi->denoiser.running_avg_y[INTRA_FRAME],
2106                       yuv_denoised_file);
2107 #endif
2108 #ifdef OUTPUT_YUV_SRC
2109   vp9_write_yuv_frame(cpi->Source, yuv_file);
2110 #endif
2111
2112   set_speed_features(cpi);
2113
2114   // Decide q and q bounds.
2115   q = vp9_rc_pick_q_and_bounds(cpi, &bottom_index, &top_index);
2116
2117   if (!frame_is_intra_only(cm)) {
2118     cm->interp_filter = DEFAULT_INTERP_FILTER;
2119     /* TODO: Decide this more intelligently */
2120     set_high_precision_mv(cpi, q < HIGH_PRECISION_MV_QTHRESH);
2121   }
2122
2123   if (cpi->sf.recode_loop == DISALLOW_RECODE) {
2124     encode_without_recode_loop(cpi, q);
2125   } else {
2126     encode_with_recode_loop(cpi, size, dest, q, bottom_index, top_index);
2127   }
2128
2129   // Special case code to reduce pulsing when key frames are forced at a
2130   // fixed interval. Note the reconstruction error if it is the frame before
2131   // the force key frame
2132   if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
2133     cpi->ambient_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
2134   }
2135
2136   // If the encoder forced a KEY_FRAME decision
2137   if (cm->frame_type == KEY_FRAME)
2138     cpi->refresh_last_frame = 1;
2139
2140   cm->frame_to_show = get_frame_new_buffer(cm);
2141
2142 #if WRITE_RECON_BUFFER
2143   if (cm->show_frame)
2144     write_cx_frame_to_file(cm->frame_to_show,
2145                            cm->current_video_frame);
2146   else
2147     write_cx_frame_to_file(cm->frame_to_show,
2148                            cm->current_video_frame + 1000);
2149 #endif
2150
2151   // Pick the loop filter level for the frame.
2152   loopfilter_frame(cpi, cm);
2153
2154 #if WRITE_RECON_BUFFER
2155   if (cm->show_frame)
2156     write_cx_frame_to_file(cm->frame_to_show,
2157                            cm->current_video_frame + 2000);
2158   else
2159     write_cx_frame_to_file(cm->frame_to_show,
2160                            cm->current_video_frame + 3000);
2161 #endif
2162
2163   // build the bitstream
2164   cpi->dummy_packing = 0;
2165   vp9_pack_bitstream(cpi, dest, size);
2166
2167   if (cm->seg.update_map)
2168     update_reference_segmentation_map(cpi);
2169
2170   release_scaled_references(cpi);
2171   vp9_update_reference_frames(cpi);
2172
2173   for (t = TX_4X4; t <= TX_32X32; t++)
2174     full_to_model_counts(cm->counts.coef[t], cpi->coef_counts[t]);
2175
2176   if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode)
2177     vp9_adapt_coef_probs(cm);
2178
2179   if (!frame_is_intra_only(cm)) {
2180     if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
2181       vp9_adapt_mode_probs(cm);
2182       vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
2183     }
2184   }
2185
2186   if (cpi->refresh_golden_frame == 1)
2187     cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
2188   else
2189     cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
2190
2191   if (cpi->refresh_alt_ref_frame == 1)
2192     cpi->frame_flags |= FRAMEFLAGS_ALTREF;
2193   else
2194     cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
2195
2196   get_ref_frame_flags(cpi);
2197
2198   cm->last_frame_type = cm->frame_type;
2199   vp9_rc_postencode_update(cpi, *size);
2200
2201 #if 0
2202   output_frame_level_debug_stats(cpi);
2203 #endif
2204
2205   if (cm->frame_type == KEY_FRAME) {
2206     // Tell the caller that the frame was coded as a key frame
2207     *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
2208   } else {
2209     *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
2210   }
2211
2212   // Clear the one shot update flags for segmentation map and mode/ref loop
2213   // filter deltas.
2214   cm->seg.update_map = 0;
2215   cm->seg.update_data = 0;
2216   cm->lf.mode_ref_delta_update = 0;
2217
2218   // keep track of the last coded dimensions
2219   cm->last_width = cm->width;
2220   cm->last_height = cm->height;
2221
2222   // reset to normal state now that we are done.
2223   if (!cm->show_existing_frame)
2224     cm->last_show_frame = cm->show_frame;
2225
2226   if (cm->show_frame) {
2227     vp9_swap_mi_and_prev_mi(cm);
2228
2229     // Don't increment frame counters if this was an altref buffer
2230     // update not a real frame
2231     ++cm->current_video_frame;
2232     if (cpi->use_svc)
2233       vp9_inc_frame_in_layer(&cpi->svc);
2234   }
2235 }
2236
2237 static void SvcEncode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
2238                       unsigned int *frame_flags) {
2239   vp9_rc_get_svc_params(cpi);
2240   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
2241 }
2242
2243 static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
2244                         unsigned int *frame_flags) {
2245   if (cpi->oxcf.rc_mode == VPX_CBR) {
2246     vp9_rc_get_one_pass_cbr_params(cpi);
2247   } else {
2248     vp9_rc_get_one_pass_vbr_params(cpi);
2249   }
2250   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
2251 }
2252
2253 static void Pass2Encode(VP9_COMP *cpi, size_t *size,
2254                         uint8_t *dest, unsigned int *frame_flags) {
2255   cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
2256
2257   vp9_rc_get_second_pass_params(cpi);
2258   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
2259
2260   vp9_twopass_postencode_update(cpi);
2261 }
2262
2263 static void init_motion_estimation(VP9_COMP *cpi) {
2264   int y_stride = cpi->scaled_source.y_stride;
2265
2266   if (cpi->sf.mv.search_method == NSTEP) {
2267     vp9_init3smotion_compensation(&cpi->ss_cfg, y_stride);
2268   } else if (cpi->sf.mv.search_method == DIAMOND) {
2269     vp9_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
2270   }
2271 }
2272
2273 static void check_initial_width(VP9_COMP *cpi, int subsampling_x,
2274                                 int subsampling_y) {
2275   VP9_COMMON *const cm = &cpi->common;
2276
2277   if (!cpi->initial_width) {
2278     cm->subsampling_x = subsampling_x;
2279     cm->subsampling_y = subsampling_y;
2280
2281     alloc_raw_frame_buffers(cpi);
2282     alloc_ref_frame_buffers(cpi);
2283     alloc_util_frame_buffers(cpi);
2284
2285     init_motion_estimation(cpi);
2286
2287     cpi->initial_width = cm->width;
2288     cpi->initial_height = cm->height;
2289   }
2290 }
2291
2292
2293 int vp9_receive_raw_frame(VP9_COMP *cpi, unsigned int frame_flags,
2294                           YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
2295                           int64_t end_time) {
2296   VP9_COMMON *cm = &cpi->common;
2297   struct vpx_usec_timer timer;
2298   int res = 0;
2299   const int subsampling_x = sd->uv_width  < sd->y_width;
2300   const int subsampling_y = sd->uv_height < sd->y_height;
2301
2302   check_initial_width(cpi, subsampling_x, subsampling_y);
2303
2304   vpx_usec_timer_start(&timer);
2305   if (vp9_lookahead_push(cpi->lookahead,
2306                          sd, time_stamp, end_time, frame_flags))
2307     res = -1;
2308   vpx_usec_timer_mark(&timer);
2309   cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
2310
2311   if (cm->profile == PROFILE_0 && (subsampling_x != 1 || subsampling_y != 1)) {
2312     vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
2313                        "Non-4:2:0 color space requires profile >= 1");
2314     res = -1;
2315   }
2316
2317   return res;
2318 }
2319
2320
2321 static int frame_is_reference(const VP9_COMP *cpi) {
2322   const VP9_COMMON *cm = &cpi->common;
2323
2324   return cm->frame_type == KEY_FRAME ||
2325          cpi->refresh_last_frame ||
2326          cpi->refresh_golden_frame ||
2327          cpi->refresh_alt_ref_frame ||
2328          cm->refresh_frame_context ||
2329          cm->lf.mode_ref_delta_update ||
2330          cm->seg.update_map ||
2331          cm->seg.update_data;
2332 }
2333
2334 void adjust_frame_rate(VP9_COMP *cpi) {
2335   int64_t this_duration;
2336   int step = 0;
2337
2338   if (cpi->source->ts_start == cpi->first_time_stamp_ever) {
2339     this_duration = cpi->source->ts_end - cpi->source->ts_start;
2340     step = 1;
2341   } else {
2342     int64_t last_duration = cpi->last_end_time_stamp_seen
2343         - cpi->last_time_stamp_seen;
2344
2345     this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
2346
2347     // do a step update if the duration changes by 10%
2348     if (last_duration)
2349       step = (int)((this_duration - last_duration) * 10 / last_duration);
2350   }
2351
2352   if (this_duration) {
2353     if (step) {
2354       vp9_new_framerate(cpi, 10000000.0 / this_duration);
2355     } else {
2356       // Average this frame's rate into the last second's average
2357       // frame rate. If we haven't seen 1 second yet, then average
2358       // over the whole interval seen.
2359       const double interval = MIN((double)(cpi->source->ts_end
2360                                    - cpi->first_time_stamp_ever), 10000000.0);
2361       double avg_duration = 10000000.0 / cpi->oxcf.framerate;
2362       avg_duration *= (interval - avg_duration + this_duration);
2363       avg_duration /= interval;
2364
2365       vp9_new_framerate(cpi, 10000000.0 / avg_duration);
2366     }
2367   }
2368   cpi->last_time_stamp_seen = cpi->source->ts_start;
2369   cpi->last_end_time_stamp_seen = cpi->source->ts_end;
2370 }
2371
2372 // Returns 0 if this is not an alt ref else the offset of the source frame
2373 // used as the arf midpoint.
2374 static int get_arf_src_index(VP9_COMP *cpi) {
2375   RATE_CONTROL *const rc = &cpi->rc;
2376   int arf_src_index = 0;
2377   if (is_altref_enabled(&cpi->oxcf)) {
2378     if (cpi->pass == 2) {
2379       const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2380       if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
2381         arf_src_index = gf_group->arf_src_offset[gf_group->index];
2382       }
2383     } else if (rc->source_alt_ref_pending) {
2384       arf_src_index = rc->frames_till_gf_update_due;
2385     }
2386   }
2387   return arf_src_index;
2388 }
2389
2390 static void is_src_altref(VP9_COMP *cpi) {
2391   RATE_CONTROL *const rc = &cpi->rc;
2392
2393   if (cpi->pass == 2) {
2394     const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2395     rc->is_src_frame_alt_ref =
2396       (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
2397   } else {
2398     rc->is_src_frame_alt_ref = cpi->alt_ref_source &&
2399                                (cpi->source == cpi->alt_ref_source);
2400   }
2401
2402   if (rc->is_src_frame_alt_ref) {
2403     // Current frame is an ARF overlay frame.
2404     cpi->alt_ref_source = NULL;
2405
2406     // Don't refresh the last buffer for an ARF overlay frame. It will
2407     // become the GF so preserve last as an alternative prediction option.
2408     cpi->refresh_last_frame = 0;
2409   }
2410 }
2411
2412 int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
2413                             size_t *size, uint8_t *dest,
2414                             int64_t *time_stamp, int64_t *time_end, int flush) {
2415   VP9_COMMON *const cm = &cpi->common;
2416   MACROBLOCKD *const xd = &cpi->mb.e_mbd;
2417   RATE_CONTROL *const rc = &cpi->rc;
2418   struct vpx_usec_timer  cmptimer;
2419   YV12_BUFFER_CONFIG *force_src_buffer = NULL;
2420   MV_REFERENCE_FRAME ref_frame;
2421   int arf_src_index;
2422
2423   if (!cpi)
2424     return -1;
2425
2426   if (cpi->svc.number_spatial_layers > 1 && cpi->pass == 2) {
2427     vp9_restore_layer_context(cpi);
2428   }
2429
2430   vpx_usec_timer_start(&cmptimer);
2431
2432   cpi->source = NULL;
2433   cpi->last_source = NULL;
2434
2435   set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
2436
2437   // Normal defaults
2438   cm->reset_frame_context = 0;
2439   cm->refresh_frame_context = 1;
2440   cpi->refresh_last_frame = 1;
2441   cpi->refresh_golden_frame = 0;
2442   cpi->refresh_alt_ref_frame = 0;
2443
2444   // Should we encode an arf frame.
2445   arf_src_index = get_arf_src_index(cpi);
2446   if (arf_src_index) {
2447     assert(arf_src_index <= rc->frames_to_key);
2448
2449     if ((cpi->source = vp9_lookahead_peek(cpi->lookahead, arf_src_index))) {
2450       cpi->alt_ref_source = cpi->source;
2451
2452       if (cpi->oxcf.arnr_max_frames > 0) {
2453         // Produce the filtered ARF frame.
2454         // TODO(agrange) merge these two functions.
2455         vp9_configure_arnr_filter(cpi, arf_src_index, rc->gfu_boost);
2456         vp9_temporal_filter_prepare(cpi, arf_src_index);
2457         vp9_extend_frame_borders(&cpi->alt_ref_buffer);
2458         force_src_buffer = &cpi->alt_ref_buffer;
2459       }
2460
2461       cm->show_frame = 0;
2462       cpi->refresh_alt_ref_frame = 1;
2463       cpi->refresh_golden_frame = 0;
2464       cpi->refresh_last_frame = 0;
2465       rc->is_src_frame_alt_ref = 0;
2466       rc->source_alt_ref_pending = 0;
2467     } else {
2468       rc->source_alt_ref_pending = 0;
2469     }
2470   }
2471
2472   if (!cpi->source) {
2473     // Get last frame source.
2474     if (cm->current_video_frame > 0) {
2475       if ((cpi->last_source = vp9_lookahead_peek(cpi->lookahead, -1)) == NULL)
2476         return -1;
2477     }
2478
2479     // Read in the source frame.
2480     if ((cpi->source = vp9_lookahead_pop(cpi->lookahead, flush))) {
2481       cm->show_frame = 1;
2482       cm->intra_only = 0;
2483
2484       // Check to see if the frame to be encoded is an overlay for a previous
2485       // arf frame and if so configure it as such.
2486       is_src_altref(cpi);
2487     }
2488   }
2489
2490   if (cpi->source) {
2491     cpi->un_scaled_source = cpi->Source = force_src_buffer ? force_src_buffer
2492                                                            : &cpi->source->img;
2493
2494     if (cpi->last_source != NULL) {
2495       cpi->unscaled_last_source = &cpi->last_source->img;
2496     } else {
2497       cpi->unscaled_last_source = NULL;
2498     }
2499
2500     *time_stamp = cpi->source->ts_start;
2501     *time_end = cpi->source->ts_end;
2502     *frame_flags = cpi->source->flags;
2503   } else {
2504     *size = 0;
2505     if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done) {
2506       vp9_end_first_pass(cpi);    /* get last stats packet */
2507       cpi->twopass.first_pass_done = 1;
2508     }
2509     return -1;
2510   }
2511
2512   if (cpi->source->ts_start < cpi->first_time_stamp_ever) {
2513     cpi->first_time_stamp_ever = cpi->source->ts_start;
2514     cpi->last_end_time_stamp_seen = cpi->source->ts_start;
2515   }
2516
2517   // adjust frame rates based on timestamps given
2518   if (cm->show_frame) {
2519     adjust_frame_rate(cpi);
2520   }
2521
2522   if (cpi->svc.number_temporal_layers > 1 &&
2523       cpi->oxcf.rc_mode == VPX_CBR) {
2524     vp9_update_temporal_layer_framerate(cpi);
2525     vp9_restore_layer_context(cpi);
2526   }
2527
2528   // start with a 0 size frame
2529   *size = 0;
2530
2531   // Clear down mmx registers
2532   vp9_clear_system_state();
2533
2534   /* find a free buffer for the new frame, releasing the reference previously
2535    * held.
2536    */
2537   cm->frame_bufs[cm->new_fb_idx].ref_count--;
2538   cm->new_fb_idx = get_free_fb(cm);
2539
2540   if (!cpi->use_svc && cpi->multi_arf_allowed) {
2541     if (cm->frame_type == KEY_FRAME) {
2542       init_buffer_indices(cpi);
2543     } else if (cpi->pass == 2) {
2544       const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2545       cpi->alt_fb_idx = gf_group->arf_ref_idx[gf_group->index];
2546     }
2547   }
2548
2549   cpi->frame_flags = *frame_flags;
2550
2551   if (cpi->pass == 2 &&
2552       cm->current_video_frame == 0 &&
2553       cpi->oxcf.allow_spatial_resampling &&
2554       cpi->oxcf.rc_mode == VPX_VBR) {
2555     // Internal scaling is triggered on the first frame.
2556     vp9_set_size_literal(cpi, cpi->oxcf.scaled_frame_width,
2557                          cpi->oxcf.scaled_frame_height);
2558   }
2559
2560   // Reset the frame pointers to the current frame size
2561   vp9_realloc_frame_buffer(get_frame_new_buffer(cm),
2562                            cm->width, cm->height,
2563                            cm->subsampling_x, cm->subsampling_y,
2564                            VP9_ENC_BORDER_IN_PIXELS, NULL, NULL, NULL);
2565
2566   alloc_util_frame_buffers(cpi);
2567   init_motion_estimation(cpi);
2568
2569   for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2570     const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)];
2571     YV12_BUFFER_CONFIG *const buf = &cm->frame_bufs[idx].buf;
2572     RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - 1];
2573     ref_buf->buf = buf;
2574     ref_buf->idx = idx;
2575     vp9_setup_scale_factors_for_frame(&ref_buf->sf,
2576                                       buf->y_crop_width, buf->y_crop_height,
2577                                       cm->width, cm->height);
2578
2579     if (vp9_is_scaled(&ref_buf->sf))
2580       vp9_extend_frame_borders(buf);
2581   }
2582
2583   set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
2584
2585   if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
2586     vp9_vaq_init();
2587   }
2588
2589   if (cpi->pass == 1 &&
2590       (!cpi->use_svc || cpi->svc.number_temporal_layers == 1)) {
2591     const int lossless = is_lossless_requested(&cpi->oxcf);
2592     cpi->mb.fwd_txm4x4 = lossless ? vp9_fwht4x4 : vp9_fdct4x4;
2593     cpi->mb.itxm_add = lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
2594     vp9_first_pass(cpi);
2595   } else if (cpi->pass == 2 &&
2596       (!cpi->use_svc || cpi->svc.number_temporal_layers == 1)) {
2597     Pass2Encode(cpi, size, dest, frame_flags);
2598   } else if (cpi->use_svc) {
2599     SvcEncode(cpi, size, dest, frame_flags);
2600   } else {
2601     // One pass encode
2602     Pass0Encode(cpi, size, dest, frame_flags);
2603   }
2604
2605   if (cm->refresh_frame_context)
2606     cm->frame_contexts[cm->frame_context_idx] = cm->fc;
2607
2608   // Frame was dropped, release scaled references.
2609   if (*size == 0) {
2610     release_scaled_references(cpi);
2611   }
2612
2613   if (*size > 0) {
2614     cpi->droppable = !frame_is_reference(cpi);
2615   }
2616
2617   // Save layer specific state.
2618   if ((cpi->svc.number_temporal_layers > 1 &&
2619       cpi->oxcf.rc_mode == VPX_CBR) ||
2620       (cpi->svc.number_spatial_layers > 1 && cpi->pass == 2)) {
2621     vp9_save_layer_context(cpi);
2622   }
2623
2624   vpx_usec_timer_mark(&cmptimer);
2625   cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
2626
2627   if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame)
2628     generate_psnr_packet(cpi);
2629
2630 #if CONFIG_INTERNAL_STATS
2631
2632   if (cpi->pass != 1) {
2633     cpi->bytes += (int)(*size);
2634
2635     if (cm->show_frame) {
2636       cpi->count++;
2637
2638       if (cpi->b_calculate_psnr) {
2639         YV12_BUFFER_CONFIG *orig = cpi->Source;
2640         YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
2641         YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
2642         PSNR_STATS psnr;
2643         calc_psnr(orig, recon, &psnr);
2644
2645         cpi->total += psnr.psnr[0];
2646         cpi->total_y += psnr.psnr[1];
2647         cpi->total_u += psnr.psnr[2];
2648         cpi->total_v += psnr.psnr[3];
2649         cpi->total_sq_error += psnr.sse[0];
2650         cpi->total_samples += psnr.samples[0];
2651
2652         {
2653           PSNR_STATS psnr2;
2654           double frame_ssim2 = 0, weight = 0;
2655 #if CONFIG_VP9_POSTPROC
2656           vp9_deblock(cm->frame_to_show, &cm->post_proc_buffer,
2657                       cm->lf.filter_level * 10 / 6);
2658 #endif
2659           vp9_clear_system_state();
2660
2661           calc_psnr(orig, pp, &psnr2);
2662
2663           cpi->totalp += psnr2.psnr[0];
2664           cpi->totalp_y += psnr2.psnr[1];
2665           cpi->totalp_u += psnr2.psnr[2];
2666           cpi->totalp_v += psnr2.psnr[3];
2667           cpi->totalp_sq_error += psnr2.sse[0];
2668           cpi->totalp_samples += psnr2.samples[0];
2669
2670           frame_ssim2 = vp9_calc_ssim(orig, recon, 1, &weight);
2671
2672           cpi->summed_quality += frame_ssim2 * weight;
2673           cpi->summed_weights += weight;
2674
2675           frame_ssim2 = vp9_calc_ssim(orig, &cm->post_proc_buffer, 1, &weight);
2676
2677           cpi->summedp_quality += frame_ssim2 * weight;
2678           cpi->summedp_weights += weight;
2679 #if 0
2680           {
2681             FILE *f = fopen("q_used.stt", "a");
2682             fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
2683                     cpi->common.current_video_frame, y2, u2, v2,
2684                     frame_psnr2, frame_ssim2);
2685             fclose(f);
2686           }
2687 #endif
2688         }
2689       }
2690
2691       if (cpi->b_calculate_ssimg) {
2692         double y, u, v, frame_all;
2693         frame_all = vp9_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u, &v);
2694         cpi->total_ssimg_y += y;
2695         cpi->total_ssimg_u += u;
2696         cpi->total_ssimg_v += v;
2697         cpi->total_ssimg_all += frame_all;
2698       }
2699     }
2700   }
2701
2702 #endif
2703   return 0;
2704 }
2705
2706 int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
2707                               vp9_ppflags_t *flags) {
2708   VP9_COMMON *cm = &cpi->common;
2709 #if !CONFIG_VP9_POSTPROC
2710   (void)flags;
2711 #endif
2712
2713   if (!cm->show_frame) {
2714     return -1;
2715   } else {
2716     int ret;
2717 #if CONFIG_VP9_POSTPROC
2718     ret = vp9_post_proc_frame(cm, dest, flags);
2719 #else
2720     if (cm->frame_to_show) {
2721       *dest = *cm->frame_to_show;
2722       dest->y_width = cm->width;
2723       dest->y_height = cm->height;
2724       dest->uv_width = cm->width >> cm->subsampling_x;
2725       dest->uv_height = cm->height >> cm->subsampling_y;
2726       ret = 0;
2727     } else {
2728       ret = -1;
2729     }
2730 #endif  // !CONFIG_VP9_POSTPROC
2731     vp9_clear_system_state();
2732     return ret;
2733   }
2734 }
2735
2736 int vp9_set_active_map(VP9_COMP *cpi, unsigned char *map, int rows, int cols) {
2737   if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
2738     const int mi_rows = cpi->common.mi_rows;
2739     const int mi_cols = cpi->common.mi_cols;
2740     if (map) {
2741       int r, c;
2742       for (r = 0; r < mi_rows; r++) {
2743         for (c = 0; c < mi_cols; c++) {
2744           cpi->segmentation_map[r * mi_cols + c] =
2745               !map[(r >> 1) * cols + (c >> 1)];
2746         }
2747       }
2748       vp9_enable_segfeature(&cpi->common.seg, 1, SEG_LVL_SKIP);
2749       vp9_enable_segmentation(&cpi->common.seg);
2750     } else {
2751       vp9_disable_segmentation(&cpi->common.seg);
2752     }
2753     return 0;
2754   } else {
2755     return -1;
2756   }
2757 }
2758
2759 int vp9_set_internal_size(VP9_COMP *cpi,
2760                           VPX_SCALING horiz_mode, VPX_SCALING vert_mode) {
2761   VP9_COMMON *cm = &cpi->common;
2762   int hr = 0, hs = 0, vr = 0, vs = 0;
2763
2764   if (horiz_mode > ONETWO || vert_mode > ONETWO)
2765     return -1;
2766
2767   Scale2Ratio(horiz_mode, &hr, &hs);
2768   Scale2Ratio(vert_mode, &vr, &vs);
2769
2770   // always go to the next whole number
2771   cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
2772   cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
2773
2774   assert(cm->width <= cpi->initial_width);
2775   assert(cm->height <= cpi->initial_height);
2776   update_frame_size(cpi);
2777   return 0;
2778 }
2779
2780 int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
2781                          unsigned int height) {
2782   VP9_COMMON *cm = &cpi->common;
2783
2784   check_initial_width(cpi, 1, 1);
2785
2786   if (width) {
2787     cm->width = width;
2788     if (cm->width * 5 < cpi->initial_width) {
2789       cm->width = cpi->initial_width / 5 + 1;
2790       printf("Warning: Desired width too small, changed to %d\n", cm->width);
2791     }
2792     if (cm->width > cpi->initial_width) {
2793       cm->width = cpi->initial_width;
2794       printf("Warning: Desired width too large, changed to %d\n", cm->width);
2795     }
2796   }
2797
2798   if (height) {
2799     cm->height = height;
2800     if (cm->height * 5 < cpi->initial_height) {
2801       cm->height = cpi->initial_height / 5 + 1;
2802       printf("Warning: Desired height too small, changed to %d\n", cm->height);
2803     }
2804     if (cm->height > cpi->initial_height) {
2805       cm->height = cpi->initial_height;
2806       printf("Warning: Desired height too large, changed to %d\n", cm->height);
2807     }
2808   }
2809
2810   assert(cm->width <= cpi->initial_width);
2811   assert(cm->height <= cpi->initial_height);
2812   update_frame_size(cpi);
2813   return 0;
2814 }
2815
2816 void vp9_set_svc(VP9_COMP *cpi, int use_svc) {
2817   cpi->use_svc = use_svc;
2818   return;
2819 }
2820
2821 int vp9_get_y_sse(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b) {
2822   assert(a->y_crop_width == b->y_crop_width);
2823   assert(a->y_crop_height == b->y_crop_height);
2824
2825   return (int)get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
2826                       a->y_crop_width, a->y_crop_height);
2827 }
2828
2829
2830 int vp9_get_quantizer(VP9_COMP *cpi) {
2831   return cpi->common.base_qindex;
2832 }