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 "./vp9_rtcd.h"
16 #include "./vpx_config.h"
17 #include "./vpx_dsp_rtcd.h"
18 #include "./vpx_scale_rtcd.h"
19 #include "vpx/internal/vpx_psnr.h"
20 #include "vpx_dsp/vpx_filter.h"
21 #if CONFIG_INTERNAL_STATS
22 #include "vpx_dsp/ssim.h"
23 #endif
24 #include "vpx_ports/mem.h"
25 #include "vpx_ports/system_state.h"
26 #include "vpx_ports/vpx_timer.h"
27
28 #include "vp9/common/vp9_alloccommon.h"
29 #include "vp9/common/vp9_filter.h"
30 #include "vp9/common/vp9_idct.h"
31 #if CONFIG_VP9_POSTPROC
32 #include "vp9/common/vp9_postproc.h"
33 #endif
34 #include "vp9/common/vp9_reconinter.h"
35 #include "vp9/common/vp9_reconintra.h"
36 #include "vp9/common/vp9_tile_common.h"
37
38 #include "vp9/encoder/vp9_aq_complexity.h"
39 #include "vp9/encoder/vp9_aq_cyclicrefresh.h"
40 #include "vp9/encoder/vp9_aq_variance.h"
41 #include "vp9/encoder/vp9_bitstream.h"
42 #include "vp9/encoder/vp9_context_tree.h"
43 #include "vp9/encoder/vp9_encodeframe.h"
44 #include "vp9/encoder/vp9_encodemv.h"
45 #include "vp9/encoder/vp9_encoder.h"
46 #include "vp9/encoder/vp9_ethread.h"
47 #include "vp9/encoder/vp9_firstpass.h"
48 #include "vp9/encoder/vp9_mbgraph.h"
49 #include "vp9/encoder/vp9_picklpf.h"
50 #include "vp9/encoder/vp9_ratectrl.h"
51 #include "vp9/encoder/vp9_rd.h"
52 #include "vp9/encoder/vp9_resize.h"
53 #include "vp9/encoder/vp9_segmentation.h"
54 #include "vp9/encoder/vp9_skin_detection.h"
55 #include "vp9/encoder/vp9_speed_features.h"
56 #include "vp9/encoder/vp9_svc_layercontext.h"
57 #include "vp9/encoder/vp9_temporal_filter.h"
58
59 #define AM_SEGMENT_ID_INACTIVE 7
60 #define AM_SEGMENT_ID_ACTIVE 0
61
62 #define SHARP_FILTER_QTHRESH 0 /* Q threshold for 8-tap sharp filter */
63
64 #define ALTREF_HIGH_PRECISION_MV 1 // Whether to use high precision mv
65 // for altref computation.
66 #define HIGH_PRECISION_MV_QTHRESH 200 // Q threshold for high precision
67 // mv. Choose a very high value for
68 // now so that HIGH_PRECISION is always
69 // chosen.
70 // #define OUTPUT_YUV_REC
71
72 #ifdef OUTPUT_YUV_DENOISED
73 FILE *yuv_denoised_file = NULL;
74 #endif
75 #ifdef OUTPUT_YUV_SKINMAP
76 FILE *yuv_skinmap_file = NULL;
77 #endif
78 #ifdef OUTPUT_YUV_REC
79 FILE *yuv_rec_file;
80 #endif
81
82 #if 0
83 FILE *framepsnr;
84 FILE *kf_list;
85 FILE *keyfile;
86 #endif
87
Scale2Ratio(VPX_SCALING mode,int * hr,int * hs)88 static INLINE void Scale2Ratio(VPX_SCALING mode, int *hr, int *hs) {
89 switch (mode) {
90 case NORMAL:
91 *hr = 1;
92 *hs = 1;
93 break;
94 case FOURFIVE:
95 *hr = 4;
96 *hs = 5;
97 break;
98 case THREEFIVE:
99 *hr = 3;
100 *hs = 5;
101 break;
102 case ONETWO:
103 *hr = 1;
104 *hs = 2;
105 break;
106 default:
107 *hr = 1;
108 *hs = 1;
109 assert(0);
110 break;
111 }
112 }
113
114 // Mark all inactive blocks as active. Other segmentation features may be set
115 // so memset cannot be used, instead only inactive blocks should be reset.
suppress_active_map(VP9_COMP * cpi)116 static void suppress_active_map(VP9_COMP *cpi) {
117 unsigned char *const seg_map = cpi->segmentation_map;
118 int i;
119 if (cpi->active_map.enabled || cpi->active_map.update)
120 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
121 if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
122 seg_map[i] = AM_SEGMENT_ID_ACTIVE;
123 }
124
apply_active_map(VP9_COMP * cpi)125 static void apply_active_map(VP9_COMP *cpi) {
126 struct segmentation *const seg = &cpi->common.seg;
127 unsigned char *const seg_map = cpi->segmentation_map;
128 const unsigned char *const active_map = cpi->active_map.map;
129 int i;
130
131 assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
132
133 if (frame_is_intra_only(&cpi->common)) {
134 cpi->active_map.enabled = 0;
135 cpi->active_map.update = 1;
136 }
137
138 if (cpi->active_map.update) {
139 if (cpi->active_map.enabled) {
140 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
141 if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
142 vp9_enable_segmentation(seg);
143 vp9_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
144 vp9_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
145 // Setting the data to -MAX_LOOP_FILTER will result in the computed loop
146 // filter level being zero regardless of the value of seg->abs_delta.
147 vp9_set_segdata(seg, AM_SEGMENT_ID_INACTIVE,
148 SEG_LVL_ALT_LF, -MAX_LOOP_FILTER);
149 } else {
150 vp9_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
151 vp9_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
152 if (seg->enabled) {
153 seg->update_data = 1;
154 seg->update_map = 1;
155 }
156 }
157 cpi->active_map.update = 0;
158 }
159 }
160
vp9_set_active_map(VP9_COMP * cpi,unsigned char * new_map_16x16,int rows,int cols)161 int vp9_set_active_map(VP9_COMP* cpi,
162 unsigned char* new_map_16x16,
163 int rows,
164 int cols) {
165 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
166 unsigned char *const active_map_8x8 = cpi->active_map.map;
167 const int mi_rows = cpi->common.mi_rows;
168 const int mi_cols = cpi->common.mi_cols;
169 cpi->active_map.update = 1;
170 if (new_map_16x16) {
171 int r, c;
172 for (r = 0; r < mi_rows; ++r) {
173 for (c = 0; c < mi_cols; ++c) {
174 active_map_8x8[r * mi_cols + c] =
175 new_map_16x16[(r >> 1) * cols + (c >> 1)]
176 ? AM_SEGMENT_ID_ACTIVE
177 : AM_SEGMENT_ID_INACTIVE;
178 }
179 }
180 cpi->active_map.enabled = 1;
181 } else {
182 cpi->active_map.enabled = 0;
183 }
184 return 0;
185 } else {
186 return -1;
187 }
188 }
189
vp9_get_active_map(VP9_COMP * cpi,unsigned char * new_map_16x16,int rows,int cols)190 int vp9_get_active_map(VP9_COMP* cpi,
191 unsigned char* new_map_16x16,
192 int rows,
193 int cols) {
194 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
195 new_map_16x16) {
196 unsigned char* const seg_map_8x8 = cpi->segmentation_map;
197 const int mi_rows = cpi->common.mi_rows;
198 const int mi_cols = cpi->common.mi_cols;
199 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
200 if (cpi->active_map.enabled) {
201 int r, c;
202 for (r = 0; r < mi_rows; ++r) {
203 for (c = 0; c < mi_cols; ++c) {
204 // Cyclic refresh segments are considered active despite not having
205 // AM_SEGMENT_ID_ACTIVE
206 new_map_16x16[(r >> 1) * cols + (c >> 1)] |=
207 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
208 }
209 }
210 }
211 return 0;
212 } else {
213 return -1;
214 }
215 }
216
vp9_set_high_precision_mv(VP9_COMP * cpi,int allow_high_precision_mv)217 void vp9_set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
218 MACROBLOCK *const mb = &cpi->td.mb;
219 cpi->common.allow_high_precision_mv = allow_high_precision_mv;
220 if (cpi->common.allow_high_precision_mv) {
221 mb->mvcost = mb->nmvcost_hp;
222 mb->mvsadcost = mb->nmvsadcost_hp;
223 } else {
224 mb->mvcost = mb->nmvcost;
225 mb->mvsadcost = mb->nmvsadcost;
226 }
227 }
228
setup_frame(VP9_COMP * cpi)229 static void setup_frame(VP9_COMP *cpi) {
230 VP9_COMMON *const cm = &cpi->common;
231 // Set up entropy context depending on frame type. The decoder mandates
232 // the use of the default context, index 0, for keyframes and inter
233 // frames where the error_resilient_mode or intra_only flag is set. For
234 // other inter-frames the encoder currently uses only two contexts;
235 // context 1 for ALTREF frames and context 0 for the others.
236 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
237 vp9_setup_past_independence(cm);
238 } else {
239 if (!cpi->use_svc)
240 cm->frame_context_idx = cpi->refresh_alt_ref_frame;
241 }
242
243 if (cm->frame_type == KEY_FRAME) {
244 if (!is_two_pass_svc(cpi))
245 cpi->refresh_golden_frame = 1;
246 cpi->refresh_alt_ref_frame = 1;
247 vp9_zero(cpi->interp_filter_selected);
248 } else {
249 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
250 vp9_zero(cpi->interp_filter_selected[0]);
251 }
252 }
253
vp9_enc_setup_mi(VP9_COMMON * cm)254 static void vp9_enc_setup_mi(VP9_COMMON *cm) {
255 int i;
256 cm->mi = cm->mip + cm->mi_stride + 1;
257 memset(cm->mip, 0, cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mip));
258 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
259 // Clear top border row
260 memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
261 // Clear left border column
262 for (i = 1; i < cm->mi_rows + 1; ++i)
263 memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
264
265 cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
266 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
267
268 memset(cm->mi_grid_base, 0,
269 cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mi_grid_base));
270 }
271
vp9_enc_alloc_mi(VP9_COMMON * cm,int mi_size)272 static int vp9_enc_alloc_mi(VP9_COMMON *cm, int mi_size) {
273 cm->mip = vpx_calloc(mi_size, sizeof(*cm->mip));
274 if (!cm->mip)
275 return 1;
276 cm->prev_mip = vpx_calloc(mi_size, sizeof(*cm->prev_mip));
277 if (!cm->prev_mip)
278 return 1;
279 cm->mi_alloc_size = mi_size;
280
281 cm->mi_grid_base = (MODE_INFO **)vpx_calloc(mi_size, sizeof(MODE_INFO*));
282 if (!cm->mi_grid_base)
283 return 1;
284 cm->prev_mi_grid_base = (MODE_INFO **)vpx_calloc(mi_size, sizeof(MODE_INFO*));
285 if (!cm->prev_mi_grid_base)
286 return 1;
287
288 return 0;
289 }
290
vp9_enc_free_mi(VP9_COMMON * cm)291 static void vp9_enc_free_mi(VP9_COMMON *cm) {
292 vpx_free(cm->mip);
293 cm->mip = NULL;
294 vpx_free(cm->prev_mip);
295 cm->prev_mip = NULL;
296 vpx_free(cm->mi_grid_base);
297 cm->mi_grid_base = NULL;
298 vpx_free(cm->prev_mi_grid_base);
299 cm->prev_mi_grid_base = NULL;
300 }
301
vp9_swap_mi_and_prev_mi(VP9_COMMON * cm)302 static void vp9_swap_mi_and_prev_mi(VP9_COMMON *cm) {
303 // Current mip will be the prev_mip for the next frame.
304 MODE_INFO **temp_base = cm->prev_mi_grid_base;
305 MODE_INFO *temp = cm->prev_mip;
306 cm->prev_mip = cm->mip;
307 cm->mip = temp;
308
309 // Update the upper left visible macroblock ptrs.
310 cm->mi = cm->mip + cm->mi_stride + 1;
311 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
312
313 cm->prev_mi_grid_base = cm->mi_grid_base;
314 cm->mi_grid_base = temp_base;
315 cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
316 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
317 }
318
vp9_initialize_enc(void)319 void vp9_initialize_enc(void) {
320 static volatile int init_done = 0;
321
322 if (!init_done) {
323 vp9_rtcd();
324 vpx_dsp_rtcd();
325 vpx_scale_rtcd();
326 vp9_init_intra_predictors();
327 vp9_init_me_luts();
328 vp9_rc_init_minq_luts();
329 vp9_entropy_mv_init();
330 vp9_temporal_filter_init();
331 init_done = 1;
332 }
333 }
334
dealloc_compressor_data(VP9_COMP * cpi)335 static void dealloc_compressor_data(VP9_COMP *cpi) {
336 VP9_COMMON *const cm = &cpi->common;
337 int i;
338
339 vpx_free(cpi->mbmi_ext_base);
340 cpi->mbmi_ext_base = NULL;
341
342 vpx_free(cpi->tile_data);
343 cpi->tile_data = NULL;
344
345 // Delete sementation map
346 vpx_free(cpi->segmentation_map);
347 cpi->segmentation_map = NULL;
348 vpx_free(cpi->coding_context.last_frame_seg_map_copy);
349 cpi->coding_context.last_frame_seg_map_copy = NULL;
350
351 vpx_free(cpi->nmvcosts[0]);
352 vpx_free(cpi->nmvcosts[1]);
353 cpi->nmvcosts[0] = NULL;
354 cpi->nmvcosts[1] = NULL;
355
356 vpx_free(cpi->nmvcosts_hp[0]);
357 vpx_free(cpi->nmvcosts_hp[1]);
358 cpi->nmvcosts_hp[0] = NULL;
359 cpi->nmvcosts_hp[1] = NULL;
360
361 vpx_free(cpi->nmvsadcosts[0]);
362 vpx_free(cpi->nmvsadcosts[1]);
363 cpi->nmvsadcosts[0] = NULL;
364 cpi->nmvsadcosts[1] = NULL;
365
366 vpx_free(cpi->nmvsadcosts_hp[0]);
367 vpx_free(cpi->nmvsadcosts_hp[1]);
368 cpi->nmvsadcosts_hp[0] = NULL;
369 cpi->nmvsadcosts_hp[1] = NULL;
370
371 vp9_cyclic_refresh_free(cpi->cyclic_refresh);
372 cpi->cyclic_refresh = NULL;
373
374 vpx_free(cpi->active_map.map);
375 cpi->active_map.map = NULL;
376
377 vp9_free_ref_frame_buffers(cm->buffer_pool);
378 #if CONFIG_VP9_POSTPROC
379 vp9_free_postproc_buffers(cm);
380 #endif
381 vp9_free_context_buffers(cm);
382
383 vpx_free_frame_buffer(&cpi->last_frame_uf);
384 vpx_free_frame_buffer(&cpi->scaled_source);
385 vpx_free_frame_buffer(&cpi->scaled_last_source);
386 vpx_free_frame_buffer(&cpi->alt_ref_buffer);
387 vp9_lookahead_destroy(cpi->lookahead);
388
389 vpx_free(cpi->tile_tok[0][0]);
390 cpi->tile_tok[0][0] = 0;
391
392 vp9_free_pc_tree(&cpi->td);
393
394 for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
395 LAYER_CONTEXT *const lc = &cpi->svc.layer_context[i];
396 vpx_free(lc->rc_twopass_stats_in.buf);
397 lc->rc_twopass_stats_in.buf = NULL;
398 lc->rc_twopass_stats_in.sz = 0;
399 }
400
401 if (cpi->source_diff_var != NULL) {
402 vpx_free(cpi->source_diff_var);
403 cpi->source_diff_var = NULL;
404 }
405
406 for (i = 0; i < MAX_LAG_BUFFERS; ++i) {
407 vpx_free_frame_buffer(&cpi->svc.scaled_frames[i]);
408 }
409 memset(&cpi->svc.scaled_frames[0], 0,
410 MAX_LAG_BUFFERS * sizeof(cpi->svc.scaled_frames[0]));
411
412 vpx_free_frame_buffer(&cpi->svc.empty_frame.img);
413 memset(&cpi->svc.empty_frame, 0, sizeof(cpi->svc.empty_frame));
414 }
415
save_coding_context(VP9_COMP * cpi)416 static void save_coding_context(VP9_COMP *cpi) {
417 CODING_CONTEXT *const cc = &cpi->coding_context;
418 VP9_COMMON *cm = &cpi->common;
419
420 // Stores a snapshot of key state variables which can subsequently be
421 // restored with a call to vp9_restore_coding_context. These functions are
422 // intended for use in a re-code loop in vp9_compress_frame where the
423 // quantizer value is adjusted between loop iterations.
424 vp9_copy(cc->nmvjointcost, cpi->td.mb.nmvjointcost);
425
426 memcpy(cc->nmvcosts[0], cpi->nmvcosts[0],
427 MV_VALS * sizeof(*cpi->nmvcosts[0]));
428 memcpy(cc->nmvcosts[1], cpi->nmvcosts[1],
429 MV_VALS * sizeof(*cpi->nmvcosts[1]));
430 memcpy(cc->nmvcosts_hp[0], cpi->nmvcosts_hp[0],
431 MV_VALS * sizeof(*cpi->nmvcosts_hp[0]));
432 memcpy(cc->nmvcosts_hp[1], cpi->nmvcosts_hp[1],
433 MV_VALS * sizeof(*cpi->nmvcosts_hp[1]));
434
435 vp9_copy(cc->segment_pred_probs, cm->seg.pred_probs);
436
437 memcpy(cpi->coding_context.last_frame_seg_map_copy,
438 cm->last_frame_seg_map, (cm->mi_rows * cm->mi_cols));
439
440 vp9_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
441 vp9_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
442
443 cc->fc = *cm->fc;
444 }
445
restore_coding_context(VP9_COMP * cpi)446 static void restore_coding_context(VP9_COMP *cpi) {
447 CODING_CONTEXT *const cc = &cpi->coding_context;
448 VP9_COMMON *cm = &cpi->common;
449
450 // Restore key state variables to the snapshot state stored in the
451 // previous call to vp9_save_coding_context.
452 vp9_copy(cpi->td.mb.nmvjointcost, cc->nmvjointcost);
453
454 memcpy(cpi->nmvcosts[0], cc->nmvcosts[0], MV_VALS * sizeof(*cc->nmvcosts[0]));
455 memcpy(cpi->nmvcosts[1], cc->nmvcosts[1], MV_VALS * sizeof(*cc->nmvcosts[1]));
456 memcpy(cpi->nmvcosts_hp[0], cc->nmvcosts_hp[0],
457 MV_VALS * sizeof(*cc->nmvcosts_hp[0]));
458 memcpy(cpi->nmvcosts_hp[1], cc->nmvcosts_hp[1],
459 MV_VALS * sizeof(*cc->nmvcosts_hp[1]));
460
461 vp9_copy(cm->seg.pred_probs, cc->segment_pred_probs);
462
463 memcpy(cm->last_frame_seg_map,
464 cpi->coding_context.last_frame_seg_map_copy,
465 (cm->mi_rows * cm->mi_cols));
466
467 vp9_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
468 vp9_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
469
470 *cm->fc = cc->fc;
471 }
472
configure_static_seg_features(VP9_COMP * cpi)473 static void configure_static_seg_features(VP9_COMP *cpi) {
474 VP9_COMMON *const cm = &cpi->common;
475 const RATE_CONTROL *const rc = &cpi->rc;
476 struct segmentation *const seg = &cm->seg;
477
478 int high_q = (int)(rc->avg_q > 48.0);
479 int qi_delta;
480
481 // Disable and clear down for KF
482 if (cm->frame_type == KEY_FRAME) {
483 // Clear down the global segmentation map
484 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
485 seg->update_map = 0;
486 seg->update_data = 0;
487 cpi->static_mb_pct = 0;
488
489 // Disable segmentation
490 vp9_disable_segmentation(seg);
491
492 // Clear down the segment features.
493 vp9_clearall_segfeatures(seg);
494 } else if (cpi->refresh_alt_ref_frame) {
495 // If this is an alt ref frame
496 // Clear down the global segmentation map
497 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
498 seg->update_map = 0;
499 seg->update_data = 0;
500 cpi->static_mb_pct = 0;
501
502 // Disable segmentation and individual segment features by default
503 vp9_disable_segmentation(seg);
504 vp9_clearall_segfeatures(seg);
505
506 // Scan frames from current to arf frame.
507 // This function re-enables segmentation if appropriate.
508 vp9_update_mbgraph_stats(cpi);
509
510 // If segmentation was enabled set those features needed for the
511 // arf itself.
512 if (seg->enabled) {
513 seg->update_map = 1;
514 seg->update_data = 1;
515
516 qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
517 cm->bit_depth);
518 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
519 vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
520
521 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
522 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
523
524 // Where relevant assume segment data is delta data
525 seg->abs_delta = SEGMENT_DELTADATA;
526 }
527 } else if (seg->enabled) {
528 // All other frames if segmentation has been enabled
529
530 // First normal frame in a valid gf or alt ref group
531 if (rc->frames_since_golden == 0) {
532 // Set up segment features for normal frames in an arf group
533 if (rc->source_alt_ref_active) {
534 seg->update_map = 0;
535 seg->update_data = 1;
536 seg->abs_delta = SEGMENT_DELTADATA;
537
538 qi_delta = vp9_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
539 cm->bit_depth);
540 vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
541 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
542
543 vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
544 vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
545
546 // Segment coding disabled for compred testing
547 if (high_q || (cpi->static_mb_pct == 100)) {
548 vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
549 vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
550 vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
551 }
552 } else {
553 // Disable segmentation and clear down features if alt ref
554 // is not active for this group
555
556 vp9_disable_segmentation(seg);
557
558 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
559
560 seg->update_map = 0;
561 seg->update_data = 0;
562
563 vp9_clearall_segfeatures(seg);
564 }
565 } else if (rc->is_src_frame_alt_ref) {
566 // Special case where we are coding over the top of a previous
567 // alt ref frame.
568 // Segment coding disabled for compred testing
569
570 // Enable ref frame features for segment 0 as well
571 vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
572 vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
573
574 // All mbs should use ALTREF_FRAME
575 vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
576 vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
577 vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
578 vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
579
580 // Skip all MBs if high Q (0,0 mv and skip coeffs)
581 if (high_q) {
582 vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP);
583 vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP);
584 }
585 // Enable data update
586 seg->update_data = 1;
587 } else {
588 // All other frames.
589
590 // No updates.. leave things as they are.
591 seg->update_map = 0;
592 seg->update_data = 0;
593 }
594 }
595 }
596
update_reference_segmentation_map(VP9_COMP * cpi)597 static void update_reference_segmentation_map(VP9_COMP *cpi) {
598 VP9_COMMON *const cm = &cpi->common;
599 MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
600 uint8_t *cache_ptr = cm->last_frame_seg_map;
601 int row, col;
602
603 for (row = 0; row < cm->mi_rows; row++) {
604 MODE_INFO **mi_8x8 = mi_8x8_ptr;
605 uint8_t *cache = cache_ptr;
606 for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
607 cache[0] = mi_8x8[0]->mbmi.segment_id;
608 mi_8x8_ptr += cm->mi_stride;
609 cache_ptr += cm->mi_cols;
610 }
611 }
612
alloc_raw_frame_buffers(VP9_COMP * cpi)613 static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
614 VP9_COMMON *cm = &cpi->common;
615 const VP9EncoderConfig *oxcf = &cpi->oxcf;
616
617 if (!cpi->lookahead)
618 cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height,
619 cm->subsampling_x, cm->subsampling_y,
620 #if CONFIG_VP9_HIGHBITDEPTH
621 cm->use_highbitdepth,
622 #endif
623 oxcf->lag_in_frames);
624 if (!cpi->lookahead)
625 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
626 "Failed to allocate lag buffers");
627
628 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
629 if (vpx_realloc_frame_buffer(&cpi->alt_ref_buffer,
630 oxcf->width, oxcf->height,
631 cm->subsampling_x, cm->subsampling_y,
632 #if CONFIG_VP9_HIGHBITDEPTH
633 cm->use_highbitdepth,
634 #endif
635 VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
636 NULL, NULL, NULL))
637 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
638 "Failed to allocate altref buffer");
639 }
640
alloc_util_frame_buffers(VP9_COMP * cpi)641 static void alloc_util_frame_buffers(VP9_COMP *cpi) {
642 VP9_COMMON *const cm = &cpi->common;
643 if (vpx_realloc_frame_buffer(&cpi->last_frame_uf,
644 cm->width, cm->height,
645 cm->subsampling_x, cm->subsampling_y,
646 #if CONFIG_VP9_HIGHBITDEPTH
647 cm->use_highbitdepth,
648 #endif
649 VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
650 NULL, NULL, NULL))
651 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
652 "Failed to allocate last frame buffer");
653
654 if (vpx_realloc_frame_buffer(&cpi->scaled_source,
655 cm->width, cm->height,
656 cm->subsampling_x, cm->subsampling_y,
657 #if CONFIG_VP9_HIGHBITDEPTH
658 cm->use_highbitdepth,
659 #endif
660 VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
661 NULL, NULL, NULL))
662 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
663 "Failed to allocate scaled source buffer");
664
665 if (vpx_realloc_frame_buffer(&cpi->scaled_last_source,
666 cm->width, cm->height,
667 cm->subsampling_x, cm->subsampling_y,
668 #if CONFIG_VP9_HIGHBITDEPTH
669 cm->use_highbitdepth,
670 #endif
671 VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
672 NULL, NULL, NULL))
673 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
674 "Failed to allocate scaled last source buffer");
675 }
676
677
alloc_context_buffers_ext(VP9_COMP * cpi)678 static int alloc_context_buffers_ext(VP9_COMP *cpi) {
679 VP9_COMMON *cm = &cpi->common;
680 int mi_size = cm->mi_cols * cm->mi_rows;
681
682 cpi->mbmi_ext_base = vpx_calloc(mi_size, sizeof(*cpi->mbmi_ext_base));
683 if (!cpi->mbmi_ext_base)
684 return 1;
685
686 return 0;
687 }
688
vp9_alloc_compressor_data(VP9_COMP * cpi)689 void vp9_alloc_compressor_data(VP9_COMP *cpi) {
690 VP9_COMMON *cm = &cpi->common;
691
692 vp9_alloc_context_buffers(cm, cm->width, cm->height);
693
694 alloc_context_buffers_ext(cpi);
695
696 vpx_free(cpi->tile_tok[0][0]);
697
698 {
699 unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
700 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
701 vpx_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
702 }
703
704 vp9_setup_pc_tree(&cpi->common, &cpi->td);
705 }
706
vp9_new_framerate(VP9_COMP * cpi,double framerate)707 void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
708 cpi->framerate = framerate < 0.1 ? 30 : framerate;
709 vp9_rc_update_framerate(cpi);
710 }
711
set_tile_limits(VP9_COMP * cpi)712 static void set_tile_limits(VP9_COMP *cpi) {
713 VP9_COMMON *const cm = &cpi->common;
714
715 int min_log2_tile_cols, max_log2_tile_cols;
716 vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
717
718 if (is_two_pass_svc(cpi) &&
719 (cpi->svc.encode_empty_frame_state == ENCODING ||
720 cpi->svc.number_spatial_layers > 1)) {
721 cm->log2_tile_cols = 0;
722 cm->log2_tile_rows = 0;
723 } else {
724 cm->log2_tile_cols = clamp(cpi->oxcf.tile_columns,
725 min_log2_tile_cols, max_log2_tile_cols);
726 cm->log2_tile_rows = cpi->oxcf.tile_rows;
727 }
728 }
729
update_frame_size(VP9_COMP * cpi)730 static void update_frame_size(VP9_COMP *cpi) {
731 VP9_COMMON *const cm = &cpi->common;
732 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
733
734 vp9_set_mb_mi(cm, cm->width, cm->height);
735 vp9_init_context_buffers(cm);
736 vp9_init_macroblockd(cm, xd, NULL);
737 cpi->td.mb.mbmi_ext_base = cpi->mbmi_ext_base;
738 memset(cpi->mbmi_ext_base, 0,
739 cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
740
741 set_tile_limits(cpi);
742
743 if (is_two_pass_svc(cpi)) {
744 if (vpx_realloc_frame_buffer(&cpi->alt_ref_buffer,
745 cm->width, cm->height,
746 cm->subsampling_x, cm->subsampling_y,
747 #if CONFIG_VP9_HIGHBITDEPTH
748 cm->use_highbitdepth,
749 #endif
750 VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
751 NULL, NULL, NULL))
752 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
753 "Failed to reallocate alt_ref_buffer");
754 }
755 }
756
init_buffer_indices(VP9_COMP * cpi)757 static void init_buffer_indices(VP9_COMP *cpi) {
758 cpi->lst_fb_idx = 0;
759 cpi->gld_fb_idx = 1;
760 cpi->alt_fb_idx = 2;
761 }
762
init_config(struct VP9_COMP * cpi,VP9EncoderConfig * oxcf)763 static void init_config(struct VP9_COMP *cpi, VP9EncoderConfig *oxcf) {
764 VP9_COMMON *const cm = &cpi->common;
765
766 cpi->oxcf = *oxcf;
767 cpi->framerate = oxcf->init_framerate;
768
769 cm->profile = oxcf->profile;
770 cm->bit_depth = oxcf->bit_depth;
771 #if CONFIG_VP9_HIGHBITDEPTH
772 cm->use_highbitdepth = oxcf->use_highbitdepth;
773 #endif
774 cm->color_space = oxcf->color_space;
775
776 cm->width = oxcf->width;
777 cm->height = oxcf->height;
778 vp9_alloc_compressor_data(cpi);
779
780 cpi->svc.temporal_layering_mode = oxcf->temporal_layering_mode;
781
782 // Single thread case: use counts in common.
783 cpi->td.counts = &cm->counts;
784
785 // Spatial scalability.
786 cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
787 // Temporal scalability.
788 cpi->svc.number_temporal_layers = oxcf->ts_number_layers;
789
790 if ((cpi->svc.number_temporal_layers > 1 && cpi->oxcf.rc_mode == VPX_CBR) ||
791 ((cpi->svc.number_temporal_layers > 1 ||
792 cpi->svc.number_spatial_layers > 1) &&
793 cpi->oxcf.pass != 1)) {
794 vp9_init_layer_context(cpi);
795 }
796
797 // change includes all joint functionality
798 vp9_change_config(cpi, oxcf);
799
800 cpi->static_mb_pct = 0;
801 cpi->ref_frame_flags = 0;
802
803 init_buffer_indices(cpi);
804 }
805
set_rc_buffer_sizes(RATE_CONTROL * rc,const VP9EncoderConfig * oxcf)806 static void set_rc_buffer_sizes(RATE_CONTROL *rc,
807 const VP9EncoderConfig *oxcf) {
808 const int64_t bandwidth = oxcf->target_bandwidth;
809 const int64_t starting = oxcf->starting_buffer_level_ms;
810 const int64_t optimal = oxcf->optimal_buffer_level_ms;
811 const int64_t maximum = oxcf->maximum_buffer_size_ms;
812
813 rc->starting_buffer_level = starting * bandwidth / 1000;
814 rc->optimal_buffer_level = (optimal == 0) ? bandwidth / 8
815 : optimal * bandwidth / 1000;
816 rc->maximum_buffer_size = (maximum == 0) ? bandwidth / 8
817 : maximum * bandwidth / 1000;
818 }
819
820 #if CONFIG_VP9_HIGHBITDEPTH
821 #define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
822 cpi->fn_ptr[BT].sdf = SDF; \
823 cpi->fn_ptr[BT].sdaf = SDAF; \
824 cpi->fn_ptr[BT].vf = VF; \
825 cpi->fn_ptr[BT].svf = SVF; \
826 cpi->fn_ptr[BT].svaf = SVAF; \
827 cpi->fn_ptr[BT].sdx3f = SDX3F; \
828 cpi->fn_ptr[BT].sdx8f = SDX8F; \
829 cpi->fn_ptr[BT].sdx4df = SDX4DF;
830
831 #define MAKE_BFP_SAD_WRAPPER(fnname) \
832 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
833 int source_stride, \
834 const uint8_t *ref_ptr, \
835 int ref_stride) { \
836 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
837 } \
838 static unsigned int fnname##_bits10(const uint8_t *src_ptr, \
839 int source_stride, \
840 const uint8_t *ref_ptr, \
841 int ref_stride) { \
842 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
843 } \
844 static unsigned int fnname##_bits12(const uint8_t *src_ptr, \
845 int source_stride, \
846 const uint8_t *ref_ptr, \
847 int ref_stride) { \
848 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
849 }
850
851 #define MAKE_BFP_SADAVG_WRAPPER(fnname) static unsigned int \
852 fnname##_bits8(const uint8_t *src_ptr, \
853 int source_stride, \
854 const uint8_t *ref_ptr, \
855 int ref_stride, \
856 const uint8_t *second_pred) { \
857 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
858 } \
859 static unsigned int fnname##_bits10(const uint8_t *src_ptr, \
860 int source_stride, \
861 const uint8_t *ref_ptr, \
862 int ref_stride, \
863 const uint8_t *second_pred) { \
864 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
865 second_pred) >> 2; \
866 } \
867 static unsigned int fnname##_bits12(const uint8_t *src_ptr, \
868 int source_stride, \
869 const uint8_t *ref_ptr, \
870 int ref_stride, \
871 const uint8_t *second_pred) { \
872 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
873 second_pred) >> 4; \
874 }
875
876 #define MAKE_BFP_SAD3_WRAPPER(fnname) \
877 static void fnname##_bits8(const uint8_t *src_ptr, \
878 int source_stride, \
879 const uint8_t *ref_ptr, \
880 int ref_stride, \
881 unsigned int *sad_array) { \
882 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
883 } \
884 static void fnname##_bits10(const uint8_t *src_ptr, \
885 int source_stride, \
886 const uint8_t *ref_ptr, \
887 int ref_stride, \
888 unsigned int *sad_array) { \
889 int i; \
890 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
891 for (i = 0; i < 3; i++) \
892 sad_array[i] >>= 2; \
893 } \
894 static void fnname##_bits12(const uint8_t *src_ptr, \
895 int source_stride, \
896 const uint8_t *ref_ptr, \
897 int ref_stride, \
898 unsigned int *sad_array) { \
899 int i; \
900 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
901 for (i = 0; i < 3; i++) \
902 sad_array[i] >>= 4; \
903 }
904
905 #define MAKE_BFP_SAD8_WRAPPER(fnname) \
906 static void fnname##_bits8(const uint8_t *src_ptr, \
907 int source_stride, \
908 const uint8_t *ref_ptr, \
909 int ref_stride, \
910 unsigned int *sad_array) { \
911 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
912 } \
913 static void fnname##_bits10(const uint8_t *src_ptr, \
914 int source_stride, \
915 const uint8_t *ref_ptr, \
916 int ref_stride, \
917 unsigned int *sad_array) { \
918 int i; \
919 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
920 for (i = 0; i < 8; i++) \
921 sad_array[i] >>= 2; \
922 } \
923 static void fnname##_bits12(const uint8_t *src_ptr, \
924 int source_stride, \
925 const uint8_t *ref_ptr, \
926 int ref_stride, \
927 unsigned int *sad_array) { \
928 int i; \
929 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
930 for (i = 0; i < 8; i++) \
931 sad_array[i] >>= 4; \
932 }
933 #define MAKE_BFP_SAD4D_WRAPPER(fnname) \
934 static void fnname##_bits8(const uint8_t *src_ptr, \
935 int source_stride, \
936 const uint8_t* const ref_ptr[], \
937 int ref_stride, \
938 unsigned int *sad_array) { \
939 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
940 } \
941 static void fnname##_bits10(const uint8_t *src_ptr, \
942 int source_stride, \
943 const uint8_t* const ref_ptr[], \
944 int ref_stride, \
945 unsigned int *sad_array) { \
946 int i; \
947 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
948 for (i = 0; i < 4; i++) \
949 sad_array[i] >>= 2; \
950 } \
951 static void fnname##_bits12(const uint8_t *src_ptr, \
952 int source_stride, \
953 const uint8_t* const ref_ptr[], \
954 int ref_stride, \
955 unsigned int *sad_array) { \
956 int i; \
957 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
958 for (i = 0; i < 4; i++) \
959 sad_array[i] >>= 4; \
960 }
961
962 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x16)
MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x16_avg)963 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x16_avg)
964 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x16x4d)
965 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x32)
966 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x32_avg)
967 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x32x4d)
968 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad64x32)
969 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad64x32_avg)
970 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad64x32x4d)
971 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x64)
972 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x64_avg)
973 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x64x4d)
974 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad32x32)
975 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad32x32_avg)
976 MAKE_BFP_SAD3_WRAPPER(vpx_highbd_sad32x32x3)
977 MAKE_BFP_SAD8_WRAPPER(vpx_highbd_sad32x32x8)
978 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad32x32x4d)
979 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad64x64)
980 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad64x64_avg)
981 MAKE_BFP_SAD3_WRAPPER(vpx_highbd_sad64x64x3)
982 MAKE_BFP_SAD8_WRAPPER(vpx_highbd_sad64x64x8)
983 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad64x64x4d)
984 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x16)
985 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x16_avg)
986 MAKE_BFP_SAD3_WRAPPER(vpx_highbd_sad16x16x3)
987 MAKE_BFP_SAD8_WRAPPER(vpx_highbd_sad16x16x8)
988 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x16x4d)
989 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad16x8)
990 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad16x8_avg)
991 MAKE_BFP_SAD3_WRAPPER(vpx_highbd_sad16x8x3)
992 MAKE_BFP_SAD8_WRAPPER(vpx_highbd_sad16x8x8)
993 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad16x8x4d)
994 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x16)
995 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x16_avg)
996 MAKE_BFP_SAD3_WRAPPER(vpx_highbd_sad8x16x3)
997 MAKE_BFP_SAD8_WRAPPER(vpx_highbd_sad8x16x8)
998 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x16x4d)
999 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x8)
1000 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x8_avg)
1001 MAKE_BFP_SAD3_WRAPPER(vpx_highbd_sad8x8x3)
1002 MAKE_BFP_SAD8_WRAPPER(vpx_highbd_sad8x8x8)
1003 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x8x4d)
1004 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad8x4)
1005 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad8x4_avg)
1006 MAKE_BFP_SAD8_WRAPPER(vpx_highbd_sad8x4x8)
1007 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad8x4x4d)
1008 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad4x8)
1009 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad4x8_avg)
1010 MAKE_BFP_SAD8_WRAPPER(vpx_highbd_sad4x8x8)
1011 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad4x8x4d)
1012 MAKE_BFP_SAD_WRAPPER(vpx_highbd_sad4x4)
1013 MAKE_BFP_SADAVG_WRAPPER(vpx_highbd_sad4x4_avg)
1014 MAKE_BFP_SAD3_WRAPPER(vpx_highbd_sad4x4x3)
1015 MAKE_BFP_SAD8_WRAPPER(vpx_highbd_sad4x4x8)
1016 MAKE_BFP_SAD4D_WRAPPER(vpx_highbd_sad4x4x4d)
1017
1018 static void highbd_set_var_fns(VP9_COMP *const cpi) {
1019 VP9_COMMON *const cm = &cpi->common;
1020 if (cm->use_highbitdepth) {
1021 switch (cm->bit_depth) {
1022 case VPX_BITS_8:
1023 HIGHBD_BFP(BLOCK_32X16,
1024 vpx_highbd_sad32x16_bits8,
1025 vpx_highbd_sad32x16_avg_bits8,
1026 vpx_highbd_8_variance32x16,
1027 vpx_highbd_8_sub_pixel_variance32x16,
1028 vpx_highbd_8_sub_pixel_avg_variance32x16,
1029 NULL,
1030 NULL,
1031 vpx_highbd_sad32x16x4d_bits8)
1032
1033 HIGHBD_BFP(BLOCK_16X32,
1034 vpx_highbd_sad16x32_bits8,
1035 vpx_highbd_sad16x32_avg_bits8,
1036 vpx_highbd_8_variance16x32,
1037 vpx_highbd_8_sub_pixel_variance16x32,
1038 vpx_highbd_8_sub_pixel_avg_variance16x32,
1039 NULL,
1040 NULL,
1041 vpx_highbd_sad16x32x4d_bits8)
1042
1043 HIGHBD_BFP(BLOCK_64X32,
1044 vpx_highbd_sad64x32_bits8,
1045 vpx_highbd_sad64x32_avg_bits8,
1046 vpx_highbd_8_variance64x32,
1047 vpx_highbd_8_sub_pixel_variance64x32,
1048 vpx_highbd_8_sub_pixel_avg_variance64x32,
1049 NULL,
1050 NULL,
1051 vpx_highbd_sad64x32x4d_bits8)
1052
1053 HIGHBD_BFP(BLOCK_32X64,
1054 vpx_highbd_sad32x64_bits8,
1055 vpx_highbd_sad32x64_avg_bits8,
1056 vpx_highbd_8_variance32x64,
1057 vpx_highbd_8_sub_pixel_variance32x64,
1058 vpx_highbd_8_sub_pixel_avg_variance32x64,
1059 NULL,
1060 NULL,
1061 vpx_highbd_sad32x64x4d_bits8)
1062
1063 HIGHBD_BFP(BLOCK_32X32,
1064 vpx_highbd_sad32x32_bits8,
1065 vpx_highbd_sad32x32_avg_bits8,
1066 vpx_highbd_8_variance32x32,
1067 vpx_highbd_8_sub_pixel_variance32x32,
1068 vpx_highbd_8_sub_pixel_avg_variance32x32,
1069 vpx_highbd_sad32x32x3_bits8,
1070 vpx_highbd_sad32x32x8_bits8,
1071 vpx_highbd_sad32x32x4d_bits8)
1072
1073 HIGHBD_BFP(BLOCK_64X64,
1074 vpx_highbd_sad64x64_bits8,
1075 vpx_highbd_sad64x64_avg_bits8,
1076 vpx_highbd_8_variance64x64,
1077 vpx_highbd_8_sub_pixel_variance64x64,
1078 vpx_highbd_8_sub_pixel_avg_variance64x64,
1079 vpx_highbd_sad64x64x3_bits8,
1080 vpx_highbd_sad64x64x8_bits8,
1081 vpx_highbd_sad64x64x4d_bits8)
1082
1083 HIGHBD_BFP(BLOCK_16X16,
1084 vpx_highbd_sad16x16_bits8,
1085 vpx_highbd_sad16x16_avg_bits8,
1086 vpx_highbd_8_variance16x16,
1087 vpx_highbd_8_sub_pixel_variance16x16,
1088 vpx_highbd_8_sub_pixel_avg_variance16x16,
1089 vpx_highbd_sad16x16x3_bits8,
1090 vpx_highbd_sad16x16x8_bits8,
1091 vpx_highbd_sad16x16x4d_bits8)
1092
1093 HIGHBD_BFP(BLOCK_16X8,
1094 vpx_highbd_sad16x8_bits8,
1095 vpx_highbd_sad16x8_avg_bits8,
1096 vpx_highbd_8_variance16x8,
1097 vpx_highbd_8_sub_pixel_variance16x8,
1098 vpx_highbd_8_sub_pixel_avg_variance16x8,
1099 vpx_highbd_sad16x8x3_bits8,
1100 vpx_highbd_sad16x8x8_bits8,
1101 vpx_highbd_sad16x8x4d_bits8)
1102
1103 HIGHBD_BFP(BLOCK_8X16,
1104 vpx_highbd_sad8x16_bits8,
1105 vpx_highbd_sad8x16_avg_bits8,
1106 vpx_highbd_8_variance8x16,
1107 vpx_highbd_8_sub_pixel_variance8x16,
1108 vpx_highbd_8_sub_pixel_avg_variance8x16,
1109 vpx_highbd_sad8x16x3_bits8,
1110 vpx_highbd_sad8x16x8_bits8,
1111 vpx_highbd_sad8x16x4d_bits8)
1112
1113 HIGHBD_BFP(BLOCK_8X8,
1114 vpx_highbd_sad8x8_bits8,
1115 vpx_highbd_sad8x8_avg_bits8,
1116 vpx_highbd_8_variance8x8,
1117 vpx_highbd_8_sub_pixel_variance8x8,
1118 vpx_highbd_8_sub_pixel_avg_variance8x8,
1119 vpx_highbd_sad8x8x3_bits8,
1120 vpx_highbd_sad8x8x8_bits8,
1121 vpx_highbd_sad8x8x4d_bits8)
1122
1123 HIGHBD_BFP(BLOCK_8X4,
1124 vpx_highbd_sad8x4_bits8,
1125 vpx_highbd_sad8x4_avg_bits8,
1126 vpx_highbd_8_variance8x4,
1127 vpx_highbd_8_sub_pixel_variance8x4,
1128 vpx_highbd_8_sub_pixel_avg_variance8x4,
1129 NULL,
1130 vpx_highbd_sad8x4x8_bits8,
1131 vpx_highbd_sad8x4x4d_bits8)
1132
1133 HIGHBD_BFP(BLOCK_4X8,
1134 vpx_highbd_sad4x8_bits8,
1135 vpx_highbd_sad4x8_avg_bits8,
1136 vpx_highbd_8_variance4x8,
1137 vpx_highbd_8_sub_pixel_variance4x8,
1138 vpx_highbd_8_sub_pixel_avg_variance4x8,
1139 NULL,
1140 vpx_highbd_sad4x8x8_bits8,
1141 vpx_highbd_sad4x8x4d_bits8)
1142
1143 HIGHBD_BFP(BLOCK_4X4,
1144 vpx_highbd_sad4x4_bits8,
1145 vpx_highbd_sad4x4_avg_bits8,
1146 vpx_highbd_8_variance4x4,
1147 vpx_highbd_8_sub_pixel_variance4x4,
1148 vpx_highbd_8_sub_pixel_avg_variance4x4,
1149 vpx_highbd_sad4x4x3_bits8,
1150 vpx_highbd_sad4x4x8_bits8,
1151 vpx_highbd_sad4x4x4d_bits8)
1152 break;
1153
1154 case VPX_BITS_10:
1155 HIGHBD_BFP(BLOCK_32X16,
1156 vpx_highbd_sad32x16_bits10,
1157 vpx_highbd_sad32x16_avg_bits10,
1158 vpx_highbd_10_variance32x16,
1159 vpx_highbd_10_sub_pixel_variance32x16,
1160 vpx_highbd_10_sub_pixel_avg_variance32x16,
1161 NULL,
1162 NULL,
1163 vpx_highbd_sad32x16x4d_bits10)
1164
1165 HIGHBD_BFP(BLOCK_16X32,
1166 vpx_highbd_sad16x32_bits10,
1167 vpx_highbd_sad16x32_avg_bits10,
1168 vpx_highbd_10_variance16x32,
1169 vpx_highbd_10_sub_pixel_variance16x32,
1170 vpx_highbd_10_sub_pixel_avg_variance16x32,
1171 NULL,
1172 NULL,
1173 vpx_highbd_sad16x32x4d_bits10)
1174
1175 HIGHBD_BFP(BLOCK_64X32,
1176 vpx_highbd_sad64x32_bits10,
1177 vpx_highbd_sad64x32_avg_bits10,
1178 vpx_highbd_10_variance64x32,
1179 vpx_highbd_10_sub_pixel_variance64x32,
1180 vpx_highbd_10_sub_pixel_avg_variance64x32,
1181 NULL,
1182 NULL,
1183 vpx_highbd_sad64x32x4d_bits10)
1184
1185 HIGHBD_BFP(BLOCK_32X64,
1186 vpx_highbd_sad32x64_bits10,
1187 vpx_highbd_sad32x64_avg_bits10,
1188 vpx_highbd_10_variance32x64,
1189 vpx_highbd_10_sub_pixel_variance32x64,
1190 vpx_highbd_10_sub_pixel_avg_variance32x64,
1191 NULL,
1192 NULL,
1193 vpx_highbd_sad32x64x4d_bits10)
1194
1195 HIGHBD_BFP(BLOCK_32X32,
1196 vpx_highbd_sad32x32_bits10,
1197 vpx_highbd_sad32x32_avg_bits10,
1198 vpx_highbd_10_variance32x32,
1199 vpx_highbd_10_sub_pixel_variance32x32,
1200 vpx_highbd_10_sub_pixel_avg_variance32x32,
1201 vpx_highbd_sad32x32x3_bits10,
1202 vpx_highbd_sad32x32x8_bits10,
1203 vpx_highbd_sad32x32x4d_bits10)
1204
1205 HIGHBD_BFP(BLOCK_64X64,
1206 vpx_highbd_sad64x64_bits10,
1207 vpx_highbd_sad64x64_avg_bits10,
1208 vpx_highbd_10_variance64x64,
1209 vpx_highbd_10_sub_pixel_variance64x64,
1210 vpx_highbd_10_sub_pixel_avg_variance64x64,
1211 vpx_highbd_sad64x64x3_bits10,
1212 vpx_highbd_sad64x64x8_bits10,
1213 vpx_highbd_sad64x64x4d_bits10)
1214
1215 HIGHBD_BFP(BLOCK_16X16,
1216 vpx_highbd_sad16x16_bits10,
1217 vpx_highbd_sad16x16_avg_bits10,
1218 vpx_highbd_10_variance16x16,
1219 vpx_highbd_10_sub_pixel_variance16x16,
1220 vpx_highbd_10_sub_pixel_avg_variance16x16,
1221 vpx_highbd_sad16x16x3_bits10,
1222 vpx_highbd_sad16x16x8_bits10,
1223 vpx_highbd_sad16x16x4d_bits10)
1224
1225 HIGHBD_BFP(BLOCK_16X8,
1226 vpx_highbd_sad16x8_bits10,
1227 vpx_highbd_sad16x8_avg_bits10,
1228 vpx_highbd_10_variance16x8,
1229 vpx_highbd_10_sub_pixel_variance16x8,
1230 vpx_highbd_10_sub_pixel_avg_variance16x8,
1231 vpx_highbd_sad16x8x3_bits10,
1232 vpx_highbd_sad16x8x8_bits10,
1233 vpx_highbd_sad16x8x4d_bits10)
1234
1235 HIGHBD_BFP(BLOCK_8X16,
1236 vpx_highbd_sad8x16_bits10,
1237 vpx_highbd_sad8x16_avg_bits10,
1238 vpx_highbd_10_variance8x16,
1239 vpx_highbd_10_sub_pixel_variance8x16,
1240 vpx_highbd_10_sub_pixel_avg_variance8x16,
1241 vpx_highbd_sad8x16x3_bits10,
1242 vpx_highbd_sad8x16x8_bits10,
1243 vpx_highbd_sad8x16x4d_bits10)
1244
1245 HIGHBD_BFP(BLOCK_8X8,
1246 vpx_highbd_sad8x8_bits10,
1247 vpx_highbd_sad8x8_avg_bits10,
1248 vpx_highbd_10_variance8x8,
1249 vpx_highbd_10_sub_pixel_variance8x8,
1250 vpx_highbd_10_sub_pixel_avg_variance8x8,
1251 vpx_highbd_sad8x8x3_bits10,
1252 vpx_highbd_sad8x8x8_bits10,
1253 vpx_highbd_sad8x8x4d_bits10)
1254
1255 HIGHBD_BFP(BLOCK_8X4,
1256 vpx_highbd_sad8x4_bits10,
1257 vpx_highbd_sad8x4_avg_bits10,
1258 vpx_highbd_10_variance8x4,
1259 vpx_highbd_10_sub_pixel_variance8x4,
1260 vpx_highbd_10_sub_pixel_avg_variance8x4,
1261 NULL,
1262 vpx_highbd_sad8x4x8_bits10,
1263 vpx_highbd_sad8x4x4d_bits10)
1264
1265 HIGHBD_BFP(BLOCK_4X8,
1266 vpx_highbd_sad4x8_bits10,
1267 vpx_highbd_sad4x8_avg_bits10,
1268 vpx_highbd_10_variance4x8,
1269 vpx_highbd_10_sub_pixel_variance4x8,
1270 vpx_highbd_10_sub_pixel_avg_variance4x8,
1271 NULL,
1272 vpx_highbd_sad4x8x8_bits10,
1273 vpx_highbd_sad4x8x4d_bits10)
1274
1275 HIGHBD_BFP(BLOCK_4X4,
1276 vpx_highbd_sad4x4_bits10,
1277 vpx_highbd_sad4x4_avg_bits10,
1278 vpx_highbd_10_variance4x4,
1279 vpx_highbd_10_sub_pixel_variance4x4,
1280 vpx_highbd_10_sub_pixel_avg_variance4x4,
1281 vpx_highbd_sad4x4x3_bits10,
1282 vpx_highbd_sad4x4x8_bits10,
1283 vpx_highbd_sad4x4x4d_bits10)
1284 break;
1285
1286 case VPX_BITS_12:
1287 HIGHBD_BFP(BLOCK_32X16,
1288 vpx_highbd_sad32x16_bits12,
1289 vpx_highbd_sad32x16_avg_bits12,
1290 vpx_highbd_12_variance32x16,
1291 vpx_highbd_12_sub_pixel_variance32x16,
1292 vpx_highbd_12_sub_pixel_avg_variance32x16,
1293 NULL,
1294 NULL,
1295 vpx_highbd_sad32x16x4d_bits12)
1296
1297 HIGHBD_BFP(BLOCK_16X32,
1298 vpx_highbd_sad16x32_bits12,
1299 vpx_highbd_sad16x32_avg_bits12,
1300 vpx_highbd_12_variance16x32,
1301 vpx_highbd_12_sub_pixel_variance16x32,
1302 vpx_highbd_12_sub_pixel_avg_variance16x32,
1303 NULL,
1304 NULL,
1305 vpx_highbd_sad16x32x4d_bits12)
1306
1307 HIGHBD_BFP(BLOCK_64X32,
1308 vpx_highbd_sad64x32_bits12,
1309 vpx_highbd_sad64x32_avg_bits12,
1310 vpx_highbd_12_variance64x32,
1311 vpx_highbd_12_sub_pixel_variance64x32,
1312 vpx_highbd_12_sub_pixel_avg_variance64x32,
1313 NULL,
1314 NULL,
1315 vpx_highbd_sad64x32x4d_bits12)
1316
1317 HIGHBD_BFP(BLOCK_32X64,
1318 vpx_highbd_sad32x64_bits12,
1319 vpx_highbd_sad32x64_avg_bits12,
1320 vpx_highbd_12_variance32x64,
1321 vpx_highbd_12_sub_pixel_variance32x64,
1322 vpx_highbd_12_sub_pixel_avg_variance32x64,
1323 NULL,
1324 NULL,
1325 vpx_highbd_sad32x64x4d_bits12)
1326
1327 HIGHBD_BFP(BLOCK_32X32,
1328 vpx_highbd_sad32x32_bits12,
1329 vpx_highbd_sad32x32_avg_bits12,
1330 vpx_highbd_12_variance32x32,
1331 vpx_highbd_12_sub_pixel_variance32x32,
1332 vpx_highbd_12_sub_pixel_avg_variance32x32,
1333 vpx_highbd_sad32x32x3_bits12,
1334 vpx_highbd_sad32x32x8_bits12,
1335 vpx_highbd_sad32x32x4d_bits12)
1336
1337 HIGHBD_BFP(BLOCK_64X64,
1338 vpx_highbd_sad64x64_bits12,
1339 vpx_highbd_sad64x64_avg_bits12,
1340 vpx_highbd_12_variance64x64,
1341 vpx_highbd_12_sub_pixel_variance64x64,
1342 vpx_highbd_12_sub_pixel_avg_variance64x64,
1343 vpx_highbd_sad64x64x3_bits12,
1344 vpx_highbd_sad64x64x8_bits12,
1345 vpx_highbd_sad64x64x4d_bits12)
1346
1347 HIGHBD_BFP(BLOCK_16X16,
1348 vpx_highbd_sad16x16_bits12,
1349 vpx_highbd_sad16x16_avg_bits12,
1350 vpx_highbd_12_variance16x16,
1351 vpx_highbd_12_sub_pixel_variance16x16,
1352 vpx_highbd_12_sub_pixel_avg_variance16x16,
1353 vpx_highbd_sad16x16x3_bits12,
1354 vpx_highbd_sad16x16x8_bits12,
1355 vpx_highbd_sad16x16x4d_bits12)
1356
1357 HIGHBD_BFP(BLOCK_16X8,
1358 vpx_highbd_sad16x8_bits12,
1359 vpx_highbd_sad16x8_avg_bits12,
1360 vpx_highbd_12_variance16x8,
1361 vpx_highbd_12_sub_pixel_variance16x8,
1362 vpx_highbd_12_sub_pixel_avg_variance16x8,
1363 vpx_highbd_sad16x8x3_bits12,
1364 vpx_highbd_sad16x8x8_bits12,
1365 vpx_highbd_sad16x8x4d_bits12)
1366
1367 HIGHBD_BFP(BLOCK_8X16,
1368 vpx_highbd_sad8x16_bits12,
1369 vpx_highbd_sad8x16_avg_bits12,
1370 vpx_highbd_12_variance8x16,
1371 vpx_highbd_12_sub_pixel_variance8x16,
1372 vpx_highbd_12_sub_pixel_avg_variance8x16,
1373 vpx_highbd_sad8x16x3_bits12,
1374 vpx_highbd_sad8x16x8_bits12,
1375 vpx_highbd_sad8x16x4d_bits12)
1376
1377 HIGHBD_BFP(BLOCK_8X8,
1378 vpx_highbd_sad8x8_bits12,
1379 vpx_highbd_sad8x8_avg_bits12,
1380 vpx_highbd_12_variance8x8,
1381 vpx_highbd_12_sub_pixel_variance8x8,
1382 vpx_highbd_12_sub_pixel_avg_variance8x8,
1383 vpx_highbd_sad8x8x3_bits12,
1384 vpx_highbd_sad8x8x8_bits12,
1385 vpx_highbd_sad8x8x4d_bits12)
1386
1387 HIGHBD_BFP(BLOCK_8X4,
1388 vpx_highbd_sad8x4_bits12,
1389 vpx_highbd_sad8x4_avg_bits12,
1390 vpx_highbd_12_variance8x4,
1391 vpx_highbd_12_sub_pixel_variance8x4,
1392 vpx_highbd_12_sub_pixel_avg_variance8x4,
1393 NULL,
1394 vpx_highbd_sad8x4x8_bits12,
1395 vpx_highbd_sad8x4x4d_bits12)
1396
1397 HIGHBD_BFP(BLOCK_4X8,
1398 vpx_highbd_sad4x8_bits12,
1399 vpx_highbd_sad4x8_avg_bits12,
1400 vpx_highbd_12_variance4x8,
1401 vpx_highbd_12_sub_pixel_variance4x8,
1402 vpx_highbd_12_sub_pixel_avg_variance4x8,
1403 NULL,
1404 vpx_highbd_sad4x8x8_bits12,
1405 vpx_highbd_sad4x8x4d_bits12)
1406
1407 HIGHBD_BFP(BLOCK_4X4,
1408 vpx_highbd_sad4x4_bits12,
1409 vpx_highbd_sad4x4_avg_bits12,
1410 vpx_highbd_12_variance4x4,
1411 vpx_highbd_12_sub_pixel_variance4x4,
1412 vpx_highbd_12_sub_pixel_avg_variance4x4,
1413 vpx_highbd_sad4x4x3_bits12,
1414 vpx_highbd_sad4x4x8_bits12,
1415 vpx_highbd_sad4x4x4d_bits12)
1416 break;
1417
1418 default:
1419 assert(0 && "cm->bit_depth should be VPX_BITS_8, "
1420 "VPX_BITS_10 or VPX_BITS_12");
1421 }
1422 }
1423 }
1424 #endif // CONFIG_VP9_HIGHBITDEPTH
1425
realloc_segmentation_maps(VP9_COMP * cpi)1426 static void realloc_segmentation_maps(VP9_COMP *cpi) {
1427 VP9_COMMON *const cm = &cpi->common;
1428
1429 // Create the encoder segmentation map and set all entries to 0
1430 vpx_free(cpi->segmentation_map);
1431 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
1432 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1433
1434 // Create a map used for cyclic background refresh.
1435 if (cpi->cyclic_refresh)
1436 vp9_cyclic_refresh_free(cpi->cyclic_refresh);
1437 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
1438 vp9_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
1439
1440 // Create a map used to mark inactive areas.
1441 vpx_free(cpi->active_map.map);
1442 CHECK_MEM_ERROR(cm, cpi->active_map.map,
1443 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1444
1445 // And a place holder structure is the coding context
1446 // for use if we want to save and restore it
1447 vpx_free(cpi->coding_context.last_frame_seg_map_copy);
1448 CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy,
1449 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1450 }
1451
vp9_change_config(struct VP9_COMP * cpi,const VP9EncoderConfig * oxcf)1452 void vp9_change_config(struct VP9_COMP *cpi, const VP9EncoderConfig *oxcf) {
1453 VP9_COMMON *const cm = &cpi->common;
1454 RATE_CONTROL *const rc = &cpi->rc;
1455
1456 if (cm->profile != oxcf->profile)
1457 cm->profile = oxcf->profile;
1458 cm->bit_depth = oxcf->bit_depth;
1459 cm->color_space = oxcf->color_space;
1460
1461 if (cm->profile <= PROFILE_1)
1462 assert(cm->bit_depth == VPX_BITS_8);
1463 else
1464 assert(cm->bit_depth > VPX_BITS_8);
1465
1466 cpi->oxcf = *oxcf;
1467 #if CONFIG_VP9_HIGHBITDEPTH
1468 cpi->td.mb.e_mbd.bd = (int)cm->bit_depth;
1469 #endif // CONFIG_VP9_HIGHBITDEPTH
1470
1471 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
1472
1473 cpi->refresh_golden_frame = 0;
1474 cpi->refresh_last_frame = 1;
1475 cm->refresh_frame_context = 1;
1476 cm->reset_frame_context = 0;
1477
1478 vp9_reset_segment_features(&cm->seg);
1479 vp9_set_high_precision_mv(cpi, 0);
1480
1481 {
1482 int i;
1483
1484 for (i = 0; i < MAX_SEGMENTS; i++)
1485 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1486 }
1487 cpi->encode_breakout = cpi->oxcf.encode_breakout;
1488
1489 set_rc_buffer_sizes(rc, &cpi->oxcf);
1490
1491 // Under a configuration change, where maximum_buffer_size may change,
1492 // keep buffer level clipped to the maximum allowed buffer size.
1493 rc->bits_off_target = MIN(rc->bits_off_target, rc->maximum_buffer_size);
1494 rc->buffer_level = MIN(rc->buffer_level, rc->maximum_buffer_size);
1495
1496 // Set up frame rate and related parameters rate control values.
1497 vp9_new_framerate(cpi, cpi->framerate);
1498
1499 // Set absolute upper and lower quality limits
1500 rc->worst_quality = cpi->oxcf.worst_allowed_q;
1501 rc->best_quality = cpi->oxcf.best_allowed_q;
1502
1503 cm->interp_filter = cpi->sf.default_interp_filter;
1504
1505 cm->display_width = cpi->oxcf.width;
1506 cm->display_height = cpi->oxcf.height;
1507 cm->width = cpi->oxcf.width;
1508 cm->height = cpi->oxcf.height;
1509
1510 if (cpi->initial_width) {
1511 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height) {
1512 vp9_free_context_buffers(cm);
1513 vp9_alloc_compressor_data(cpi);
1514 realloc_segmentation_maps(cpi);
1515 cpi->initial_width = cpi->initial_height = 0;
1516 }
1517 }
1518 update_frame_size(cpi);
1519
1520 if ((cpi->svc.number_temporal_layers > 1 &&
1521 cpi->oxcf.rc_mode == VPX_CBR) ||
1522 ((cpi->svc.number_temporal_layers > 1 ||
1523 cpi->svc.number_spatial_layers > 1) &&
1524 cpi->oxcf.pass != 1)) {
1525 vp9_update_layer_context_change_config(cpi,
1526 (int)cpi->oxcf.target_bandwidth);
1527 }
1528
1529 cpi->alt_ref_source = NULL;
1530 rc->is_src_frame_alt_ref = 0;
1531
1532 #if 0
1533 // Experimental RD Code
1534 cpi->frame_distortion = 0;
1535 cpi->last_frame_distortion = 0;
1536 #endif
1537
1538 set_tile_limits(cpi);
1539
1540 cpi->ext_refresh_frame_flags_pending = 0;
1541 cpi->ext_refresh_frame_context_pending = 0;
1542
1543 #if CONFIG_VP9_HIGHBITDEPTH
1544 highbd_set_var_fns(cpi);
1545 #endif
1546 }
1547
1548 #ifndef M_LOG2_E
1549 #define M_LOG2_E 0.693147180559945309417
1550 #endif
1551 #define log2f(x) (log (x) / (float) M_LOG2_E)
1552
cal_nmvjointsadcost(int * mvjointsadcost)1553 static void cal_nmvjointsadcost(int *mvjointsadcost) {
1554 mvjointsadcost[0] = 600;
1555 mvjointsadcost[1] = 300;
1556 mvjointsadcost[2] = 300;
1557 mvjointsadcost[3] = 300;
1558 }
1559
cal_nmvsadcosts(int * mvsadcost[2])1560 static void cal_nmvsadcosts(int *mvsadcost[2]) {
1561 int i = 1;
1562
1563 mvsadcost[0][0] = 0;
1564 mvsadcost[1][0] = 0;
1565
1566 do {
1567 double z = 256 * (2 * (log2f(8 * i) + .6));
1568 mvsadcost[0][i] = (int)z;
1569 mvsadcost[1][i] = (int)z;
1570 mvsadcost[0][-i] = (int)z;
1571 mvsadcost[1][-i] = (int)z;
1572 } while (++i <= MV_MAX);
1573 }
1574
cal_nmvsadcosts_hp(int * mvsadcost[2])1575 static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
1576 int i = 1;
1577
1578 mvsadcost[0][0] = 0;
1579 mvsadcost[1][0] = 0;
1580
1581 do {
1582 double z = 256 * (2 * (log2f(8 * i) + .6));
1583 mvsadcost[0][i] = (int)z;
1584 mvsadcost[1][i] = (int)z;
1585 mvsadcost[0][-i] = (int)z;
1586 mvsadcost[1][-i] = (int)z;
1587 } while (++i <= MV_MAX);
1588 }
1589
1590
vp9_create_compressor(VP9EncoderConfig * oxcf,BufferPool * const pool)1591 VP9_COMP *vp9_create_compressor(VP9EncoderConfig *oxcf,
1592 BufferPool *const pool) {
1593 unsigned int i;
1594 VP9_COMP *volatile const cpi = vpx_memalign(32, sizeof(VP9_COMP));
1595 VP9_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
1596
1597 if (!cm)
1598 return NULL;
1599
1600 vp9_zero(*cpi);
1601
1602 if (setjmp(cm->error.jmp)) {
1603 cm->error.setjmp = 0;
1604 vp9_remove_compressor(cpi);
1605 return 0;
1606 }
1607
1608 cm->error.setjmp = 1;
1609 cm->alloc_mi = vp9_enc_alloc_mi;
1610 cm->free_mi = vp9_enc_free_mi;
1611 cm->setup_mi = vp9_enc_setup_mi;
1612
1613 CHECK_MEM_ERROR(cm, cm->fc,
1614 (FRAME_CONTEXT *)vpx_calloc(1, sizeof(*cm->fc)));
1615 CHECK_MEM_ERROR(cm, cm->frame_contexts,
1616 (FRAME_CONTEXT *)vpx_calloc(FRAME_CONTEXTS,
1617 sizeof(*cm->frame_contexts)));
1618
1619 cpi->use_svc = 0;
1620 cpi->resize_state = 0;
1621 cpi->resize_avg_qp = 0;
1622 cpi->resize_buffer_underflow = 0;
1623 cpi->common.buffer_pool = pool;
1624
1625 cpi->rc.high_source_sad = 0;
1626
1627 init_config(cpi, oxcf);
1628 vp9_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
1629
1630 cm->current_video_frame = 0;
1631 cpi->partition_search_skippable_frame = 0;
1632 cpi->tile_data = NULL;
1633
1634 realloc_segmentation_maps(cpi);
1635
1636 CHECK_MEM_ERROR(cm, cpi->nmvcosts[0],
1637 vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[0])));
1638 CHECK_MEM_ERROR(cm, cpi->nmvcosts[1],
1639 vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts[1])));
1640 CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[0],
1641 vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[0])));
1642 CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[1],
1643 vpx_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[1])));
1644 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[0],
1645 vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[0])));
1646 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[1],
1647 vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[1])));
1648 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[0],
1649 vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[0])));
1650 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[1],
1651 vpx_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[1])));
1652
1653 for (i = 0; i < (sizeof(cpi->mbgraph_stats) /
1654 sizeof(cpi->mbgraph_stats[0])); i++) {
1655 CHECK_MEM_ERROR(cm, cpi->mbgraph_stats[i].mb_stats,
1656 vpx_calloc(cm->MBs *
1657 sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
1658 }
1659
1660 #if CONFIG_FP_MB_STATS
1661 cpi->use_fp_mb_stats = 0;
1662 if (cpi->use_fp_mb_stats) {
1663 // a place holder used to store the first pass mb stats in the first pass
1664 CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
1665 vpx_calloc(cm->MBs * sizeof(uint8_t), 1));
1666 } else {
1667 cpi->twopass.frame_mb_stats_buf = NULL;
1668 }
1669 #endif
1670
1671 cpi->refresh_alt_ref_frame = 0;
1672 cpi->multi_arf_last_grp_enabled = 0;
1673
1674 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1675 #if CONFIG_INTERNAL_STATS
1676 cpi->b_calculate_ssimg = 0;
1677 cpi->b_calculate_blockiness = 1;
1678 cpi->b_calculate_consistency = 1;
1679 cpi->total_inconsistency = 0;
1680 cpi->psnr.worst = 100.0;
1681 cpi->worst_ssim = 100.0;
1682
1683 cpi->count = 0;
1684 cpi->bytes = 0;
1685
1686 if (cpi->b_calculate_psnr) {
1687 cpi->total_sq_error = 0;
1688 cpi->total_samples = 0;
1689
1690 cpi->totalp_sq_error = 0;
1691 cpi->totalp_samples = 0;
1692
1693 cpi->tot_recode_hits = 0;
1694 cpi->summed_quality = 0;
1695 cpi->summed_weights = 0;
1696 cpi->summedp_quality = 0;
1697 cpi->summedp_weights = 0;
1698 }
1699
1700 if (cpi->b_calculate_ssimg) {
1701 cpi->ssimg.worst= 100.0;
1702 }
1703 cpi->fastssim.worst = 100.0;
1704
1705 cpi->psnrhvs.worst = 100.0;
1706
1707 if (cpi->b_calculate_blockiness) {
1708 cpi->total_blockiness = 0;
1709 cpi->worst_blockiness = 0.0;
1710 }
1711
1712 if (cpi->b_calculate_consistency) {
1713 cpi->ssim_vars = vpx_malloc(sizeof(*cpi->ssim_vars) *
1714 4 * cpi->common.mi_rows * cpi->common.mi_cols);
1715 cpi->worst_consistency = 100.0;
1716 }
1717
1718 #endif
1719
1720 cpi->first_time_stamp_ever = INT64_MAX;
1721
1722 cal_nmvjointsadcost(cpi->td.mb.nmvjointsadcost);
1723 cpi->td.mb.nmvcost[0] = &cpi->nmvcosts[0][MV_MAX];
1724 cpi->td.mb.nmvcost[1] = &cpi->nmvcosts[1][MV_MAX];
1725 cpi->td.mb.nmvsadcost[0] = &cpi->nmvsadcosts[0][MV_MAX];
1726 cpi->td.mb.nmvsadcost[1] = &cpi->nmvsadcosts[1][MV_MAX];
1727 cal_nmvsadcosts(cpi->td.mb.nmvsadcost);
1728
1729 cpi->td.mb.nmvcost_hp[0] = &cpi->nmvcosts_hp[0][MV_MAX];
1730 cpi->td.mb.nmvcost_hp[1] = &cpi->nmvcosts_hp[1][MV_MAX];
1731 cpi->td.mb.nmvsadcost_hp[0] = &cpi->nmvsadcosts_hp[0][MV_MAX];
1732 cpi->td.mb.nmvsadcost_hp[1] = &cpi->nmvsadcosts_hp[1][MV_MAX];
1733 cal_nmvsadcosts_hp(cpi->td.mb.nmvsadcost_hp);
1734
1735 #if CONFIG_VP9_TEMPORAL_DENOISING
1736 #ifdef OUTPUT_YUV_DENOISED
1737 yuv_denoised_file = fopen("denoised.yuv", "ab");
1738 #endif
1739 #endif
1740 #ifdef OUTPUT_YUV_SKINMAP
1741 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
1742 #endif
1743 #ifdef OUTPUT_YUV_REC
1744 yuv_rec_file = fopen("rec.yuv", "wb");
1745 #endif
1746
1747 #if 0
1748 framepsnr = fopen("framepsnr.stt", "a");
1749 kf_list = fopen("kf_list.stt", "w");
1750 #endif
1751
1752 cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
1753
1754 if (oxcf->pass == 1) {
1755 vp9_init_first_pass(cpi);
1756 } else if (oxcf->pass == 2) {
1757 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
1758 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
1759
1760 if (cpi->svc.number_spatial_layers > 1
1761 || cpi->svc.number_temporal_layers > 1) {
1762 FIRSTPASS_STATS *const stats = oxcf->two_pass_stats_in.buf;
1763 FIRSTPASS_STATS *stats_copy[VPX_SS_MAX_LAYERS] = {0};
1764 int i;
1765
1766 for (i = 0; i < oxcf->ss_number_layers; ++i) {
1767 FIRSTPASS_STATS *const last_packet_for_layer =
1768 &stats[packets - oxcf->ss_number_layers + i];
1769 const int layer_id = (int)last_packet_for_layer->spatial_layer_id;
1770 const int packets_in_layer = (int)last_packet_for_layer->count + 1;
1771 if (layer_id >= 0 && layer_id < oxcf->ss_number_layers) {
1772 LAYER_CONTEXT *const lc = &cpi->svc.layer_context[layer_id];
1773
1774 vpx_free(lc->rc_twopass_stats_in.buf);
1775
1776 lc->rc_twopass_stats_in.sz = packets_in_layer * packet_sz;
1777 CHECK_MEM_ERROR(cm, lc->rc_twopass_stats_in.buf,
1778 vpx_malloc(lc->rc_twopass_stats_in.sz));
1779 lc->twopass.stats_in_start = lc->rc_twopass_stats_in.buf;
1780 lc->twopass.stats_in = lc->twopass.stats_in_start;
1781 lc->twopass.stats_in_end = lc->twopass.stats_in_start
1782 + packets_in_layer - 1;
1783 stats_copy[layer_id] = lc->rc_twopass_stats_in.buf;
1784 }
1785 }
1786
1787 for (i = 0; i < packets; ++i) {
1788 const int layer_id = (int)stats[i].spatial_layer_id;
1789 if (layer_id >= 0 && layer_id < oxcf->ss_number_layers
1790 && stats_copy[layer_id] != NULL) {
1791 *stats_copy[layer_id] = stats[i];
1792 ++stats_copy[layer_id];
1793 }
1794 }
1795
1796 vp9_init_second_pass_spatial_svc(cpi);
1797 } else {
1798 #if CONFIG_FP_MB_STATS
1799 if (cpi->use_fp_mb_stats) {
1800 const size_t psz = cpi->common.MBs * sizeof(uint8_t);
1801 const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
1802
1803 cpi->twopass.firstpass_mb_stats.mb_stats_start =
1804 oxcf->firstpass_mb_stats_in.buf;
1805 cpi->twopass.firstpass_mb_stats.mb_stats_end =
1806 cpi->twopass.firstpass_mb_stats.mb_stats_start +
1807 (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
1808 }
1809 #endif
1810
1811 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
1812 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
1813 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
1814
1815 vp9_init_second_pass(cpi);
1816 }
1817 }
1818
1819 vp9_set_speed_features_framesize_independent(cpi);
1820 vp9_set_speed_features_framesize_dependent(cpi);
1821
1822 // Allocate memory to store variances for a frame.
1823 CHECK_MEM_ERROR(cm, cpi->source_diff_var,
1824 vpx_calloc(cm->MBs, sizeof(diff)));
1825 cpi->source_var_thresh = 0;
1826 cpi->frames_till_next_var_check = 0;
1827
1828 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF)\
1829 cpi->fn_ptr[BT].sdf = SDF; \
1830 cpi->fn_ptr[BT].sdaf = SDAF; \
1831 cpi->fn_ptr[BT].vf = VF; \
1832 cpi->fn_ptr[BT].svf = SVF; \
1833 cpi->fn_ptr[BT].svaf = SVAF; \
1834 cpi->fn_ptr[BT].sdx3f = SDX3F; \
1835 cpi->fn_ptr[BT].sdx8f = SDX8F; \
1836 cpi->fn_ptr[BT].sdx4df = SDX4DF;
1837
1838 BFP(BLOCK_32X16, vpx_sad32x16, vpx_sad32x16_avg,
1839 vpx_variance32x16, vpx_sub_pixel_variance32x16,
1840 vpx_sub_pixel_avg_variance32x16, NULL, NULL, vpx_sad32x16x4d)
1841
1842 BFP(BLOCK_16X32, vpx_sad16x32, vpx_sad16x32_avg,
1843 vpx_variance16x32, vpx_sub_pixel_variance16x32,
1844 vpx_sub_pixel_avg_variance16x32, NULL, NULL, vpx_sad16x32x4d)
1845
1846 BFP(BLOCK_64X32, vpx_sad64x32, vpx_sad64x32_avg,
1847 vpx_variance64x32, vpx_sub_pixel_variance64x32,
1848 vpx_sub_pixel_avg_variance64x32, NULL, NULL, vpx_sad64x32x4d)
1849
1850 BFP(BLOCK_32X64, vpx_sad32x64, vpx_sad32x64_avg,
1851 vpx_variance32x64, vpx_sub_pixel_variance32x64,
1852 vpx_sub_pixel_avg_variance32x64, NULL, NULL, vpx_sad32x64x4d)
1853
1854 BFP(BLOCK_32X32, vpx_sad32x32, vpx_sad32x32_avg,
1855 vpx_variance32x32, vpx_sub_pixel_variance32x32,
1856 vpx_sub_pixel_avg_variance32x32, vpx_sad32x32x3, vpx_sad32x32x8,
1857 vpx_sad32x32x4d)
1858
1859 BFP(BLOCK_64X64, vpx_sad64x64, vpx_sad64x64_avg,
1860 vpx_variance64x64, vpx_sub_pixel_variance64x64,
1861 vpx_sub_pixel_avg_variance64x64, vpx_sad64x64x3, vpx_sad64x64x8,
1862 vpx_sad64x64x4d)
1863
1864 BFP(BLOCK_16X16, vpx_sad16x16, vpx_sad16x16_avg,
1865 vpx_variance16x16, vpx_sub_pixel_variance16x16,
1866 vpx_sub_pixel_avg_variance16x16, vpx_sad16x16x3, vpx_sad16x16x8,
1867 vpx_sad16x16x4d)
1868
1869 BFP(BLOCK_16X8, vpx_sad16x8, vpx_sad16x8_avg,
1870 vpx_variance16x8, vpx_sub_pixel_variance16x8,
1871 vpx_sub_pixel_avg_variance16x8,
1872 vpx_sad16x8x3, vpx_sad16x8x8, vpx_sad16x8x4d)
1873
1874 BFP(BLOCK_8X16, vpx_sad8x16, vpx_sad8x16_avg,
1875 vpx_variance8x16, vpx_sub_pixel_variance8x16,
1876 vpx_sub_pixel_avg_variance8x16,
1877 vpx_sad8x16x3, vpx_sad8x16x8, vpx_sad8x16x4d)
1878
1879 BFP(BLOCK_8X8, vpx_sad8x8, vpx_sad8x8_avg,
1880 vpx_variance8x8, vpx_sub_pixel_variance8x8,
1881 vpx_sub_pixel_avg_variance8x8,
1882 vpx_sad8x8x3, vpx_sad8x8x8, vpx_sad8x8x4d)
1883
1884 BFP(BLOCK_8X4, vpx_sad8x4, vpx_sad8x4_avg,
1885 vpx_variance8x4, vpx_sub_pixel_variance8x4,
1886 vpx_sub_pixel_avg_variance8x4, NULL, vpx_sad8x4x8, vpx_sad8x4x4d)
1887
1888 BFP(BLOCK_4X8, vpx_sad4x8, vpx_sad4x8_avg,
1889 vpx_variance4x8, vpx_sub_pixel_variance4x8,
1890 vpx_sub_pixel_avg_variance4x8, NULL, vpx_sad4x8x8, vpx_sad4x8x4d)
1891
1892 BFP(BLOCK_4X4, vpx_sad4x4, vpx_sad4x4_avg,
1893 vpx_variance4x4, vpx_sub_pixel_variance4x4,
1894 vpx_sub_pixel_avg_variance4x4,
1895 vpx_sad4x4x3, vpx_sad4x4x8, vpx_sad4x4x4d)
1896
1897 #if CONFIG_VP9_HIGHBITDEPTH
1898 highbd_set_var_fns(cpi);
1899 #endif
1900
1901 /* vp9_init_quantizer() is first called here. Add check in
1902 * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
1903 * called later when needed. This will avoid unnecessary calls of
1904 * vp9_init_quantizer() for every frame.
1905 */
1906 vp9_init_quantizer(cpi);
1907
1908 vp9_loop_filter_init(cm);
1909
1910 cm->error.setjmp = 0;
1911
1912 return cpi;
1913 }
1914 #define SNPRINT(H, T) \
1915 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
1916
1917 #define SNPRINT2(H, T, V) \
1918 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
1919
vp9_remove_compressor(VP9_COMP * cpi)1920 void vp9_remove_compressor(VP9_COMP *cpi) {
1921 VP9_COMMON *const cm = &cpi->common;
1922 unsigned int i;
1923 int t;
1924
1925 if (!cpi)
1926 return;
1927
1928 if (cpi && (cm->current_video_frame > 0)) {
1929 #if CONFIG_INTERNAL_STATS
1930 vpx_clear_system_state();
1931
1932 if (cpi->oxcf.pass != 1) {
1933 char headings[512] = {0};
1934 char results[512] = {0};
1935 FILE *f = fopen("opsnr.stt", "a");
1936 double time_encoded = (cpi->last_end_time_stamp_seen
1937 - cpi->first_time_stamp_ever) / 10000000.000;
1938 double total_encode_time = (cpi->time_receive_data +
1939 cpi->time_compress_data) / 1000.000;
1940 const double dr =
1941 (double)cpi->bytes * (double) 8 / (double)1000 / time_encoded;
1942 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
1943
1944 if (cpi->b_calculate_psnr) {
1945 const double total_psnr =
1946 vpx_sse_to_psnr((double)cpi->total_samples, peak,
1947 (double)cpi->total_sq_error);
1948 const double totalp_psnr =
1949 vpx_sse_to_psnr((double)cpi->totalp_samples, peak,
1950 (double)cpi->totalp_sq_error);
1951 const double total_ssim = 100 * pow(cpi->summed_quality /
1952 cpi->summed_weights, 8.0);
1953 const double totalp_ssim = 100 * pow(cpi->summedp_quality /
1954 cpi->summedp_weights, 8.0);
1955
1956 snprintf(headings, sizeof(headings),
1957 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
1958 "VPXSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
1959 "WstPsnr\tWstSsim\tWstFast\tWstHVS");
1960 snprintf(results, sizeof(results),
1961 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
1962 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
1963 "%7.3f\t%7.3f\t%7.3f\t%7.3f",
1964 dr, cpi->psnr.stat[ALL] / cpi->count, total_psnr,
1965 cpi->psnrp.stat[ALL] / cpi->count, totalp_psnr,
1966 total_ssim, totalp_ssim,
1967 cpi->fastssim.stat[ALL] / cpi->count,
1968 cpi->psnrhvs.stat[ALL] / cpi->count,
1969 cpi->psnr.worst, cpi->worst_ssim, cpi->fastssim.worst,
1970 cpi->psnrhvs.worst);
1971
1972 if (cpi->b_calculate_blockiness) {
1973 SNPRINT(headings, "\t Block\tWstBlck");
1974 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
1975 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
1976 }
1977
1978 if (cpi->b_calculate_consistency) {
1979 double consistency =
1980 vpx_sse_to_psnr((double)cpi->totalp_samples, peak,
1981 (double)cpi->total_inconsistency);
1982
1983 SNPRINT(headings, "\tConsist\tWstCons");
1984 SNPRINT2(results, "\t%7.3f", consistency);
1985 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
1986 }
1987
1988 if (cpi->b_calculate_ssimg) {
1989 SNPRINT(headings, "\t SSIMG\tWtSSIMG");
1990 SNPRINT2(results, "\t%7.3f", cpi->ssimg.stat[ALL] / cpi->count);
1991 SNPRINT2(results, "\t%7.3f", cpi->ssimg.worst);
1992 }
1993
1994 fprintf(f, "%s\t Time\n", headings);
1995 fprintf(f, "%s\t%8.0f\n", results, total_encode_time);
1996 }
1997
1998 fclose(f);
1999 }
2000
2001 #endif
2002
2003 #if 0
2004 {
2005 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2006 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
2007 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
2008 cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
2009 cpi->time_compress_data / 1000,
2010 (cpi->time_receive_data + cpi->time_compress_data) / 1000);
2011 }
2012 #endif
2013 }
2014
2015 #if CONFIG_VP9_TEMPORAL_DENOISING
2016 vp9_denoiser_free(&(cpi->denoiser));
2017 #endif
2018
2019 for (t = 0; t < cpi->num_workers; ++t) {
2020 VPxWorker *const worker = &cpi->workers[t];
2021 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
2022
2023 // Deallocate allocated threads.
2024 vpx_get_worker_interface()->end(worker);
2025
2026 // Deallocate allocated thread data.
2027 if (t < cpi->num_workers - 1) {
2028 vpx_free(thread_data->td->counts);
2029 vp9_free_pc_tree(thread_data->td);
2030 vpx_free(thread_data->td);
2031 }
2032 }
2033 vpx_free(cpi->tile_thr_data);
2034 vpx_free(cpi->workers);
2035
2036 if (cpi->num_workers > 1)
2037 vp9_loop_filter_dealloc(&cpi->lf_row_sync);
2038
2039 dealloc_compressor_data(cpi);
2040
2041 for (i = 0; i < sizeof(cpi->mbgraph_stats) /
2042 sizeof(cpi->mbgraph_stats[0]); ++i) {
2043 vpx_free(cpi->mbgraph_stats[i].mb_stats);
2044 }
2045
2046 #if CONFIG_FP_MB_STATS
2047 if (cpi->use_fp_mb_stats) {
2048 vpx_free(cpi->twopass.frame_mb_stats_buf);
2049 cpi->twopass.frame_mb_stats_buf = NULL;
2050 }
2051 #endif
2052
2053 vp9_remove_common(cm);
2054 vp9_free_ref_frame_buffers(cm->buffer_pool);
2055 #if CONFIG_VP9_POSTPROC
2056 vp9_free_postproc_buffers(cm);
2057 #endif
2058 vpx_free(cpi);
2059
2060 #if CONFIG_VP9_TEMPORAL_DENOISING
2061 #ifdef OUTPUT_YUV_DENOISED
2062 fclose(yuv_denoised_file);
2063 #endif
2064 #endif
2065 #ifdef OUTPUT_YUV_SKINMAP
2066 fclose(yuv_skinmap_file);
2067 #endif
2068 #ifdef OUTPUT_YUV_REC
2069 fclose(yuv_rec_file);
2070 #endif
2071
2072 #if 0
2073
2074 if (keyfile)
2075 fclose(keyfile);
2076
2077 if (framepsnr)
2078 fclose(framepsnr);
2079
2080 if (kf_list)
2081 fclose(kf_list);
2082
2083 #endif
2084 }
2085
2086 /* TODO(yaowu): The block_variance calls the unoptimized versions of variance()
2087 * and highbd_8_variance(). It should not.
2088 */
encoder_variance(const uint8_t * a,int a_stride,const uint8_t * b,int b_stride,int w,int h,unsigned int * sse,int * sum)2089 static void encoder_variance(const uint8_t *a, int a_stride,
2090 const uint8_t *b, int b_stride,
2091 int w, int h, unsigned int *sse, int *sum) {
2092 int i, j;
2093
2094 *sum = 0;
2095 *sse = 0;
2096
2097 for (i = 0; i < h; i++) {
2098 for (j = 0; j < w; j++) {
2099 const int diff = a[j] - b[j];
2100 *sum += diff;
2101 *sse += diff * diff;
2102 }
2103
2104 a += a_stride;
2105 b += b_stride;
2106 }
2107 }
2108
2109 #if CONFIG_VP9_HIGHBITDEPTH
encoder_highbd_variance64(const uint8_t * a8,int a_stride,const uint8_t * b8,int b_stride,int w,int h,uint64_t * sse,uint64_t * sum)2110 static void encoder_highbd_variance64(const uint8_t *a8, int a_stride,
2111 const uint8_t *b8, int b_stride,
2112 int w, int h, uint64_t *sse,
2113 uint64_t *sum) {
2114 int i, j;
2115
2116 uint16_t *a = CONVERT_TO_SHORTPTR(a8);
2117 uint16_t *b = CONVERT_TO_SHORTPTR(b8);
2118 *sum = 0;
2119 *sse = 0;
2120
2121 for (i = 0; i < h; i++) {
2122 for (j = 0; j < w; j++) {
2123 const int diff = a[j] - b[j];
2124 *sum += diff;
2125 *sse += diff * diff;
2126 }
2127 a += a_stride;
2128 b += b_stride;
2129 }
2130 }
2131
encoder_highbd_8_variance(const uint8_t * a8,int a_stride,const uint8_t * b8,int b_stride,int w,int h,unsigned int * sse,int * sum)2132 static void encoder_highbd_8_variance(const uint8_t *a8, int a_stride,
2133 const uint8_t *b8, int b_stride,
2134 int w, int h,
2135 unsigned int *sse, int *sum) {
2136 uint64_t sse_long = 0;
2137 uint64_t sum_long = 0;
2138 encoder_highbd_variance64(a8, a_stride, b8, b_stride, w, h,
2139 &sse_long, &sum_long);
2140 *sse = (unsigned int)sse_long;
2141 *sum = (int)sum_long;
2142 }
2143 #endif // CONFIG_VP9_HIGHBITDEPTH
2144
get_sse(const uint8_t * a,int a_stride,const uint8_t * b,int b_stride,int width,int height)2145 static int64_t get_sse(const uint8_t *a, int a_stride,
2146 const uint8_t *b, int b_stride,
2147 int width, int height) {
2148 const int dw = width % 16;
2149 const int dh = height % 16;
2150 int64_t total_sse = 0;
2151 unsigned int sse = 0;
2152 int sum = 0;
2153 int x, y;
2154
2155 if (dw > 0) {
2156 encoder_variance(&a[width - dw], a_stride, &b[width - dw], b_stride,
2157 dw, height, &sse, &sum);
2158 total_sse += sse;
2159 }
2160
2161 if (dh > 0) {
2162 encoder_variance(&a[(height - dh) * a_stride], a_stride,
2163 &b[(height - dh) * b_stride], b_stride,
2164 width - dw, dh, &sse, &sum);
2165 total_sse += sse;
2166 }
2167
2168 for (y = 0; y < height / 16; ++y) {
2169 const uint8_t *pa = a;
2170 const uint8_t *pb = b;
2171 for (x = 0; x < width / 16; ++x) {
2172 vpx_mse16x16(pa, a_stride, pb, b_stride, &sse);
2173 total_sse += sse;
2174
2175 pa += 16;
2176 pb += 16;
2177 }
2178
2179 a += 16 * a_stride;
2180 b += 16 * b_stride;
2181 }
2182
2183 return total_sse;
2184 }
2185
2186 #if CONFIG_VP9_HIGHBITDEPTH
highbd_get_sse_shift(const uint8_t * a8,int a_stride,const uint8_t * b8,int b_stride,int width,int height,unsigned int input_shift)2187 static int64_t highbd_get_sse_shift(const uint8_t *a8, int a_stride,
2188 const uint8_t *b8, int b_stride,
2189 int width, int height,
2190 unsigned int input_shift) {
2191 const uint16_t *a = CONVERT_TO_SHORTPTR(a8);
2192 const uint16_t *b = CONVERT_TO_SHORTPTR(b8);
2193 int64_t total_sse = 0;
2194 int x, y;
2195 for (y = 0; y < height; ++y) {
2196 for (x = 0; x < width; ++x) {
2197 int64_t diff;
2198 diff = (a[x] >> input_shift) - (b[x] >> input_shift);
2199 total_sse += diff * diff;
2200 }
2201 a += a_stride;
2202 b += b_stride;
2203 }
2204 return total_sse;
2205 }
2206
highbd_get_sse(const uint8_t * a,int a_stride,const uint8_t * b,int b_stride,int width,int height)2207 static int64_t highbd_get_sse(const uint8_t *a, int a_stride,
2208 const uint8_t *b, int b_stride,
2209 int width, int height) {
2210 int64_t total_sse = 0;
2211 int x, y;
2212 const int dw = width % 16;
2213 const int dh = height % 16;
2214 unsigned int sse = 0;
2215 int sum = 0;
2216 if (dw > 0) {
2217 encoder_highbd_8_variance(&a[width - dw], a_stride,
2218 &b[width - dw], b_stride,
2219 dw, height, &sse, &sum);
2220 total_sse += sse;
2221 }
2222 if (dh > 0) {
2223 encoder_highbd_8_variance(&a[(height - dh) * a_stride], a_stride,
2224 &b[(height - dh) * b_stride], b_stride,
2225 width - dw, dh, &sse, &sum);
2226 total_sse += sse;
2227 }
2228 for (y = 0; y < height / 16; ++y) {
2229 const uint8_t *pa = a;
2230 const uint8_t *pb = b;
2231 for (x = 0; x < width / 16; ++x) {
2232 vpx_highbd_8_mse16x16(pa, a_stride, pb, b_stride, &sse);
2233 total_sse += sse;
2234 pa += 16;
2235 pb += 16;
2236 }
2237 a += 16 * a_stride;
2238 b += 16 * b_stride;
2239 }
2240 return total_sse;
2241 }
2242 #endif // CONFIG_VP9_HIGHBITDEPTH
2243
2244 typedef struct {
2245 double psnr[4]; // total/y/u/v
2246 uint64_t sse[4]; // total/y/u/v
2247 uint32_t samples[4]; // total/y/u/v
2248 } PSNR_STATS;
2249
calc_psnr(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b,PSNR_STATS * psnr)2250 static void calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b,
2251 PSNR_STATS *psnr) {
2252 static const double peak = 255.0;
2253 const int widths[3] = {
2254 a->y_crop_width, a->uv_crop_width, a->uv_crop_width};
2255 const int heights[3] = {
2256 a->y_crop_height, a->uv_crop_height, a->uv_crop_height};
2257 const uint8_t *a_planes[3] = {a->y_buffer, a->u_buffer, a->v_buffer};
2258 const int a_strides[3] = {a->y_stride, a->uv_stride, a->uv_stride};
2259 const uint8_t *b_planes[3] = {b->y_buffer, b->u_buffer, b->v_buffer};
2260 const int b_strides[3] = {b->y_stride, b->uv_stride, b->uv_stride};
2261 int i;
2262 uint64_t total_sse = 0;
2263 uint32_t total_samples = 0;
2264
2265 for (i = 0; i < 3; ++i) {
2266 const int w = widths[i];
2267 const int h = heights[i];
2268 const uint32_t samples = w * h;
2269 const uint64_t sse = get_sse(a_planes[i], a_strides[i],
2270 b_planes[i], b_strides[i],
2271 w, h);
2272 psnr->sse[1 + i] = sse;
2273 psnr->samples[1 + i] = samples;
2274 psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, peak, (double)sse);
2275
2276 total_sse += sse;
2277 total_samples += samples;
2278 }
2279
2280 psnr->sse[0] = total_sse;
2281 psnr->samples[0] = total_samples;
2282 psnr->psnr[0] = vpx_sse_to_psnr((double)total_samples, peak,
2283 (double)total_sse);
2284 }
2285
2286 #if CONFIG_VP9_HIGHBITDEPTH
calc_highbd_psnr(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b,PSNR_STATS * psnr,unsigned int bit_depth,unsigned int in_bit_depth)2287 static void calc_highbd_psnr(const YV12_BUFFER_CONFIG *a,
2288 const YV12_BUFFER_CONFIG *b,
2289 PSNR_STATS *psnr,
2290 unsigned int bit_depth,
2291 unsigned int in_bit_depth) {
2292 const int widths[3] =
2293 {a->y_crop_width, a->uv_crop_width, a->uv_crop_width };
2294 const int heights[3] =
2295 {a->y_crop_height, a->uv_crop_height, a->uv_crop_height};
2296 const uint8_t *a_planes[3] = {a->y_buffer, a->u_buffer, a->v_buffer };
2297 const int a_strides[3] = {a->y_stride, a->uv_stride, a->uv_stride};
2298 const uint8_t *b_planes[3] = {b->y_buffer, b->u_buffer, b->v_buffer };
2299 const int b_strides[3] = {b->y_stride, b->uv_stride, b->uv_stride};
2300 int i;
2301 uint64_t total_sse = 0;
2302 uint32_t total_samples = 0;
2303 const double peak = (double)((1 << in_bit_depth) - 1);
2304 const unsigned int input_shift = bit_depth - in_bit_depth;
2305
2306 for (i = 0; i < 3; ++i) {
2307 const int w = widths[i];
2308 const int h = heights[i];
2309 const uint32_t samples = w * h;
2310 uint64_t sse;
2311 if (a->flags & YV12_FLAG_HIGHBITDEPTH) {
2312 if (input_shift) {
2313 sse = highbd_get_sse_shift(a_planes[i], a_strides[i],
2314 b_planes[i], b_strides[i], w, h,
2315 input_shift);
2316 } else {
2317 sse = highbd_get_sse(a_planes[i], a_strides[i],
2318 b_planes[i], b_strides[i], w, h);
2319 }
2320 } else {
2321 sse = get_sse(a_planes[i], a_strides[i],
2322 b_planes[i], b_strides[i],
2323 w, h);
2324 }
2325 psnr->sse[1 + i] = sse;
2326 psnr->samples[1 + i] = samples;
2327 psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, peak, (double)sse);
2328
2329 total_sse += sse;
2330 total_samples += samples;
2331 }
2332
2333 psnr->sse[0] = total_sse;
2334 psnr->samples[0] = total_samples;
2335 psnr->psnr[0] = vpx_sse_to_psnr((double)total_samples, peak,
2336 (double)total_sse);
2337 }
2338 #endif // CONFIG_VP9_HIGHBITDEPTH
2339
generate_psnr_packet(VP9_COMP * cpi)2340 static void generate_psnr_packet(VP9_COMP *cpi) {
2341 struct vpx_codec_cx_pkt pkt;
2342 int i;
2343 PSNR_STATS psnr;
2344 #if CONFIG_VP9_HIGHBITDEPTH
2345 calc_highbd_psnr(cpi->Source, cpi->common.frame_to_show, &psnr,
2346 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
2347 #else
2348 calc_psnr(cpi->Source, cpi->common.frame_to_show, &psnr);
2349 #endif
2350
2351 for (i = 0; i < 4; ++i) {
2352 pkt.data.psnr.samples[i] = psnr.samples[i];
2353 pkt.data.psnr.sse[i] = psnr.sse[i];
2354 pkt.data.psnr.psnr[i] = psnr.psnr[i];
2355 }
2356 pkt.kind = VPX_CODEC_PSNR_PKT;
2357 if (cpi->use_svc)
2358 cpi->svc.layer_context[cpi->svc.spatial_layer_id *
2359 cpi->svc.number_temporal_layers].psnr_pkt = pkt.data.psnr;
2360 else
2361 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
2362 }
2363
vp9_use_as_reference(VP9_COMP * cpi,int ref_frame_flags)2364 int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags) {
2365 if (ref_frame_flags > 7)
2366 return -1;
2367
2368 cpi->ref_frame_flags = ref_frame_flags;
2369 return 0;
2370 }
2371
vp9_update_reference(VP9_COMP * cpi,int ref_frame_flags)2372 void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags) {
2373 cpi->ext_refresh_golden_frame = (ref_frame_flags & VP9_GOLD_FLAG) != 0;
2374 cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & VP9_ALT_FLAG) != 0;
2375 cpi->ext_refresh_last_frame = (ref_frame_flags & VP9_LAST_FLAG) != 0;
2376 cpi->ext_refresh_frame_flags_pending = 1;
2377 }
2378
get_vp9_ref_frame_buffer(VP9_COMP * cpi,VP9_REFFRAME ref_frame_flag)2379 static YV12_BUFFER_CONFIG *get_vp9_ref_frame_buffer(VP9_COMP *cpi,
2380 VP9_REFFRAME ref_frame_flag) {
2381 MV_REFERENCE_FRAME ref_frame = NONE;
2382 if (ref_frame_flag == VP9_LAST_FLAG)
2383 ref_frame = LAST_FRAME;
2384 else if (ref_frame_flag == VP9_GOLD_FLAG)
2385 ref_frame = GOLDEN_FRAME;
2386 else if (ref_frame_flag == VP9_ALT_FLAG)
2387 ref_frame = ALTREF_FRAME;
2388
2389 return ref_frame == NONE ? NULL : get_ref_frame_buffer(cpi, ref_frame);
2390 }
2391
vp9_copy_reference_enc(VP9_COMP * cpi,VP9_REFFRAME ref_frame_flag,YV12_BUFFER_CONFIG * sd)2392 int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
2393 YV12_BUFFER_CONFIG *sd) {
2394 YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
2395 if (cfg) {
2396 vp8_yv12_copy_frame(cfg, sd);
2397 return 0;
2398 } else {
2399 return -1;
2400 }
2401 }
2402
vp9_set_reference_enc(VP9_COMP * cpi,VP9_REFFRAME ref_frame_flag,YV12_BUFFER_CONFIG * sd)2403 int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
2404 YV12_BUFFER_CONFIG *sd) {
2405 YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
2406 if (cfg) {
2407 vp8_yv12_copy_frame(sd, cfg);
2408 return 0;
2409 } else {
2410 return -1;
2411 }
2412 }
2413
vp9_update_entropy(VP9_COMP * cpi,int update)2414 int vp9_update_entropy(VP9_COMP * cpi, int update) {
2415 cpi->ext_refresh_frame_context = update;
2416 cpi->ext_refresh_frame_context_pending = 1;
2417 return 0;
2418 }
2419
2420 #if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
2421 // The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
2422 // as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
2423 // not denoise the UV channels at this time. If ever we implement UV channel
2424 // denoising we will have to modify this.
vp9_write_yuv_frame_420(YV12_BUFFER_CONFIG * s,FILE * f)2425 void vp9_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
2426 uint8_t *src = s->y_buffer;
2427 int h = s->y_height;
2428
2429 do {
2430 fwrite(src, s->y_width, 1, f);
2431 src += s->y_stride;
2432 } while (--h);
2433
2434 src = s->u_buffer;
2435 h = s->uv_height;
2436
2437 do {
2438 fwrite(src, s->uv_width, 1, f);
2439 src += s->uv_stride;
2440 } while (--h);
2441
2442 src = s->v_buffer;
2443 h = s->uv_height;
2444
2445 do {
2446 fwrite(src, s->uv_width, 1, f);
2447 src += s->uv_stride;
2448 } while (--h);
2449 }
2450 #endif
2451
2452 #ifdef OUTPUT_YUV_REC
vp9_write_yuv_rec_frame(VP9_COMMON * cm)2453 void vp9_write_yuv_rec_frame(VP9_COMMON *cm) {
2454 YV12_BUFFER_CONFIG *s = cm->frame_to_show;
2455 uint8_t *src = s->y_buffer;
2456 int h = cm->height;
2457
2458 #if CONFIG_VP9_HIGHBITDEPTH
2459 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
2460 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
2461
2462 do {
2463 fwrite(src16, s->y_width, 2, yuv_rec_file);
2464 src16 += s->y_stride;
2465 } while (--h);
2466
2467 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
2468 h = s->uv_height;
2469
2470 do {
2471 fwrite(src16, s->uv_width, 2, yuv_rec_file);
2472 src16 += s->uv_stride;
2473 } while (--h);
2474
2475 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
2476 h = s->uv_height;
2477
2478 do {
2479 fwrite(src16, s->uv_width, 2, yuv_rec_file);
2480 src16 += s->uv_stride;
2481 } while (--h);
2482
2483 fflush(yuv_rec_file);
2484 return;
2485 }
2486 #endif // CONFIG_VP9_HIGHBITDEPTH
2487
2488 do {
2489 fwrite(src, s->y_width, 1, yuv_rec_file);
2490 src += s->y_stride;
2491 } while (--h);
2492
2493 src = s->u_buffer;
2494 h = s->uv_height;
2495
2496 do {
2497 fwrite(src, s->uv_width, 1, yuv_rec_file);
2498 src += s->uv_stride;
2499 } while (--h);
2500
2501 src = s->v_buffer;
2502 h = s->uv_height;
2503
2504 do {
2505 fwrite(src, s->uv_width, 1, yuv_rec_file);
2506 src += s->uv_stride;
2507 } while (--h);
2508
2509 fflush(yuv_rec_file);
2510 }
2511 #endif
2512
2513 #if CONFIG_VP9_HIGHBITDEPTH
scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG * src,YV12_BUFFER_CONFIG * dst,int bd)2514 static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
2515 YV12_BUFFER_CONFIG *dst,
2516 int bd) {
2517 #else
2518 static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
2519 YV12_BUFFER_CONFIG *dst) {
2520 #endif // CONFIG_VP9_HIGHBITDEPTH
2521 // TODO(dkovalev): replace YV12_BUFFER_CONFIG with vpx_image_t
2522 int i;
2523 const uint8_t *const srcs[3] = {src->y_buffer, src->u_buffer, src->v_buffer};
2524 const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
2525 const int src_widths[3] = {src->y_crop_width, src->uv_crop_width,
2526 src->uv_crop_width };
2527 const int src_heights[3] = {src->y_crop_height, src->uv_crop_height,
2528 src->uv_crop_height};
2529 uint8_t *const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
2530 const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
2531 const int dst_widths[3] = {dst->y_crop_width, dst->uv_crop_width,
2532 dst->uv_crop_width};
2533 const int dst_heights[3] = {dst->y_crop_height, dst->uv_crop_height,
2534 dst->uv_crop_height};
2535
2536 for (i = 0; i < MAX_MB_PLANE; ++i) {
2537 #if CONFIG_VP9_HIGHBITDEPTH
2538 if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
2539 vp9_highbd_resize_plane(srcs[i], src_heights[i], src_widths[i],
2540 src_strides[i], dsts[i], dst_heights[i],
2541 dst_widths[i], dst_strides[i], bd);
2542 } else {
2543 vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
2544 dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
2545 }
2546 #else
2547 vp9_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
2548 dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
2549 #endif // CONFIG_VP9_HIGHBITDEPTH
2550 }
2551 vpx_extend_frame_borders(dst);
2552 }
2553
2554 #if CONFIG_VP9_HIGHBITDEPTH
2555 static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
2556 YV12_BUFFER_CONFIG *dst, int bd) {
2557 #else
2558 static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
2559 YV12_BUFFER_CONFIG *dst) {
2560 #endif // CONFIG_VP9_HIGHBITDEPTH
2561 const int src_w = src->y_crop_width;
2562 const int src_h = src->y_crop_height;
2563 const int dst_w = dst->y_crop_width;
2564 const int dst_h = dst->y_crop_height;
2565 const uint8_t *const srcs[3] = {src->y_buffer, src->u_buffer, src->v_buffer};
2566 const int src_strides[3] = {src->y_stride, src->uv_stride, src->uv_stride};
2567 uint8_t *const dsts[3] = {dst->y_buffer, dst->u_buffer, dst->v_buffer};
2568 const int dst_strides[3] = {dst->y_stride, dst->uv_stride, dst->uv_stride};
2569 const InterpKernel *const kernel = vp9_filter_kernels[EIGHTTAP];
2570 int x, y, i;
2571
2572 for (y = 0; y < dst_h; y += 16) {
2573 for (x = 0; x < dst_w; x += 16) {
2574 for (i = 0; i < MAX_MB_PLANE; ++i) {
2575 const int factor = (i == 0 || i == 3 ? 1 : 2);
2576 const int x_q4 = x * (16 / factor) * src_w / dst_w;
2577 const int y_q4 = y * (16 / factor) * src_h / dst_h;
2578 const int src_stride = src_strides[i];
2579 const int dst_stride = dst_strides[i];
2580 const uint8_t *src_ptr = srcs[i] + (y / factor) * src_h / dst_h *
2581 src_stride + (x / factor) * src_w / dst_w;
2582 uint8_t *dst_ptr = dsts[i] + (y / factor) * dst_stride + (x / factor);
2583
2584 #if CONFIG_VP9_HIGHBITDEPTH
2585 if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
2586 vpx_highbd_convolve8(src_ptr, src_stride, dst_ptr, dst_stride,
2587 kernel[x_q4 & 0xf], 16 * src_w / dst_w,
2588 kernel[y_q4 & 0xf], 16 * src_h / dst_h,
2589 16 / factor, 16 / factor, bd);
2590 } else {
2591 vpx_scaled_2d(src_ptr, src_stride, dst_ptr, dst_stride,
2592 kernel[x_q4 & 0xf], 16 * src_w / dst_w,
2593 kernel[y_q4 & 0xf], 16 * src_h / dst_h,
2594 16 / factor, 16 / factor);
2595 }
2596 #else
2597 vpx_scaled_2d(src_ptr, src_stride, dst_ptr, dst_stride,
2598 kernel[x_q4 & 0xf], 16 * src_w / dst_w,
2599 kernel[y_q4 & 0xf], 16 * src_h / dst_h,
2600 16 / factor, 16 / factor);
2601 #endif // CONFIG_VP9_HIGHBITDEPTH
2602 }
2603 }
2604 }
2605
2606 vpx_extend_frame_borders(dst);
2607 }
2608
2609 static int scale_down(VP9_COMP *cpi, int q) {
2610 RATE_CONTROL *const rc = &cpi->rc;
2611 GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2612 int scale = 0;
2613 assert(frame_is_kf_gf_arf(cpi));
2614
2615 if (rc->frame_size_selector == UNSCALED &&
2616 q >= rc->rf_level_maxq[gf_group->rf_level[gf_group->index]]) {
2617 const int max_size_thresh = (int)(rate_thresh_mult[SCALE_STEP1]
2618 * MAX(rc->this_frame_target, rc->avg_frame_bandwidth));
2619 scale = rc->projected_frame_size > max_size_thresh ? 1 : 0;
2620 }
2621 return scale;
2622 }
2623
2624 // Function to test for conditions that indicate we should loop
2625 // back and recode a frame.
2626 static int recode_loop_test(VP9_COMP *cpi,
2627 int high_limit, int low_limit,
2628 int q, int maxq, int minq) {
2629 const RATE_CONTROL *const rc = &cpi->rc;
2630 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
2631 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
2632 int force_recode = 0;
2633
2634 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
2635 (cpi->sf.recode_loop == ALLOW_RECODE) ||
2636 (frame_is_kfgfarf &&
2637 (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
2638 if (frame_is_kfgfarf &&
2639 (oxcf->resize_mode == RESIZE_DYNAMIC) &&
2640 scale_down(cpi, q)) {
2641 // Code this group at a lower resolution.
2642 cpi->resize_pending = 1;
2643 return 1;
2644 }
2645
2646 // TODO(agrange) high_limit could be greater than the scale-down threshold.
2647 if ((rc->projected_frame_size > high_limit && q < maxq) ||
2648 (rc->projected_frame_size < low_limit && q > minq)) {
2649 force_recode = 1;
2650 } else if (cpi->oxcf.rc_mode == VPX_CQ) {
2651 // Deal with frame undershoot and whether or not we are
2652 // below the automatically set cq level.
2653 if (q > oxcf->cq_level &&
2654 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
2655 force_recode = 1;
2656 }
2657 }
2658 }
2659 return force_recode;
2660 }
2661
2662 void vp9_update_reference_frames(VP9_COMP *cpi) {
2663 VP9_COMMON * const cm = &cpi->common;
2664 BufferPool *const pool = cm->buffer_pool;
2665
2666 // At this point the new frame has been encoded.
2667 // If any buffer copy / swapping is signaled it should be done here.
2668 if (cm->frame_type == KEY_FRAME) {
2669 ref_cnt_fb(pool->frame_bufs,
2670 &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
2671 ref_cnt_fb(pool->frame_bufs,
2672 &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
2673 } else if (vp9_preserve_existing_gf(cpi)) {
2674 // We have decided to preserve the previously existing golden frame as our
2675 // new ARF frame. However, in the short term in function
2676 // vp9_bitstream.c::get_refresh_mask() we left it in the GF slot and, if
2677 // we're updating the GF with the current decoded frame, we save it to the
2678 // ARF slot instead.
2679 // We now have to update the ARF with the current frame and swap gld_fb_idx
2680 // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
2681 // slot and, if we're updating the GF, the current frame becomes the new GF.
2682 int tmp;
2683
2684 ref_cnt_fb(pool->frame_bufs,
2685 &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
2686
2687 tmp = cpi->alt_fb_idx;
2688 cpi->alt_fb_idx = cpi->gld_fb_idx;
2689 cpi->gld_fb_idx = tmp;
2690
2691 if (is_two_pass_svc(cpi)) {
2692 cpi->svc.layer_context[0].gold_ref_idx = cpi->gld_fb_idx;
2693 cpi->svc.layer_context[0].alt_ref_idx = cpi->alt_fb_idx;
2694 }
2695 } else { /* For non key/golden frames */
2696 if (cpi->refresh_alt_ref_frame) {
2697 int arf_idx = cpi->alt_fb_idx;
2698 if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
2699 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2700 arf_idx = gf_group->arf_update_idx[gf_group->index];
2701 }
2702
2703 ref_cnt_fb(pool->frame_bufs,
2704 &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
2705 memcpy(cpi->interp_filter_selected[ALTREF_FRAME],
2706 cpi->interp_filter_selected[0],
2707 sizeof(cpi->interp_filter_selected[0]));
2708 }
2709
2710 if (cpi->refresh_golden_frame) {
2711 ref_cnt_fb(pool->frame_bufs,
2712 &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
2713 if (!cpi->rc.is_src_frame_alt_ref)
2714 memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
2715 cpi->interp_filter_selected[0],
2716 sizeof(cpi->interp_filter_selected[0]));
2717 else
2718 memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
2719 cpi->interp_filter_selected[ALTREF_FRAME],
2720 sizeof(cpi->interp_filter_selected[ALTREF_FRAME]));
2721 }
2722 }
2723
2724 if (cpi->refresh_last_frame) {
2725 ref_cnt_fb(pool->frame_bufs,
2726 &cm->ref_frame_map[cpi->lst_fb_idx], cm->new_fb_idx);
2727 if (!cpi->rc.is_src_frame_alt_ref)
2728 memcpy(cpi->interp_filter_selected[LAST_FRAME],
2729 cpi->interp_filter_selected[0],
2730 sizeof(cpi->interp_filter_selected[0]));
2731 }
2732 #if CONFIG_VP9_TEMPORAL_DENOISING
2733 if (cpi->oxcf.noise_sensitivity > 0) {
2734 vp9_denoiser_update_frame_info(&cpi->denoiser,
2735 *cpi->Source,
2736 cpi->common.frame_type,
2737 cpi->refresh_alt_ref_frame,
2738 cpi->refresh_golden_frame,
2739 cpi->refresh_last_frame);
2740 }
2741 #endif
2742 }
2743
2744 static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) {
2745 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
2746 struct loopfilter *lf = &cm->lf;
2747 if (xd->lossless) {
2748 lf->filter_level = 0;
2749 } else {
2750 struct vpx_usec_timer timer;
2751
2752 vpx_clear_system_state();
2753
2754 vpx_usec_timer_start(&timer);
2755
2756 vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.lpf_pick);
2757
2758 vpx_usec_timer_mark(&timer);
2759 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
2760 }
2761
2762 if (lf->filter_level > 0) {
2763 if (cpi->num_workers > 1)
2764 vp9_loop_filter_frame_mt(cm->frame_to_show, cm, xd->plane,
2765 lf->filter_level, 0, 0,
2766 cpi->workers, cpi->num_workers,
2767 &cpi->lf_row_sync);
2768 else
2769 vp9_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
2770 }
2771
2772 vpx_extend_frame_inner_borders(cm->frame_to_show);
2773 }
2774
2775 static INLINE void alloc_frame_mvs(const VP9_COMMON *cm,
2776 int buffer_idx) {
2777 RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
2778 if (new_fb_ptr->mvs == NULL ||
2779 new_fb_ptr->mi_rows < cm->mi_rows ||
2780 new_fb_ptr->mi_cols < cm->mi_cols) {
2781 vpx_free(new_fb_ptr->mvs);
2782 new_fb_ptr->mvs =
2783 (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols,
2784 sizeof(*new_fb_ptr->mvs));
2785 new_fb_ptr->mi_rows = cm->mi_rows;
2786 new_fb_ptr->mi_cols = cm->mi_cols;
2787 }
2788 }
2789
2790 void vp9_scale_references(VP9_COMP *cpi) {
2791 VP9_COMMON *cm = &cpi->common;
2792 MV_REFERENCE_FRAME ref_frame;
2793 const VP9_REFFRAME ref_mask[3] = {VP9_LAST_FLAG, VP9_GOLD_FLAG, VP9_ALT_FLAG};
2794
2795 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
2796 // Need to convert from VP9_REFFRAME to index into ref_mask (subtract 1).
2797 if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
2798 BufferPool *const pool = cm->buffer_pool;
2799 const YV12_BUFFER_CONFIG *const ref = get_ref_frame_buffer(cpi,
2800 ref_frame);
2801
2802 if (ref == NULL) {
2803 cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
2804 continue;
2805 }
2806
2807 #if CONFIG_VP9_HIGHBITDEPTH
2808 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
2809 RefCntBuffer *new_fb_ptr = NULL;
2810 int force_scaling = 0;
2811 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
2812 if (new_fb == INVALID_IDX) {
2813 new_fb = get_free_fb(cm);
2814 force_scaling = 1;
2815 }
2816 if (new_fb == INVALID_IDX)
2817 return;
2818 new_fb_ptr = &pool->frame_bufs[new_fb];
2819 if (force_scaling ||
2820 new_fb_ptr->buf.y_crop_width != cm->width ||
2821 new_fb_ptr->buf.y_crop_height != cm->height) {
2822 vpx_realloc_frame_buffer(&new_fb_ptr->buf,
2823 cm->width, cm->height,
2824 cm->subsampling_x, cm->subsampling_y,
2825 cm->use_highbitdepth,
2826 VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
2827 NULL, NULL, NULL);
2828 scale_and_extend_frame(ref, &new_fb_ptr->buf, (int)cm->bit_depth);
2829 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
2830 alloc_frame_mvs(cm, new_fb);
2831 }
2832 #else
2833 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
2834 RefCntBuffer *new_fb_ptr = NULL;
2835 int force_scaling = 0;
2836 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
2837 if (new_fb == INVALID_IDX) {
2838 new_fb = get_free_fb(cm);
2839 force_scaling = 1;
2840 }
2841 if (new_fb == INVALID_IDX)
2842 return;
2843 new_fb_ptr = &pool->frame_bufs[new_fb];
2844 if (force_scaling ||
2845 new_fb_ptr->buf.y_crop_width != cm->width ||
2846 new_fb_ptr->buf.y_crop_height != cm->height) {
2847 vpx_realloc_frame_buffer(&new_fb_ptr->buf,
2848 cm->width, cm->height,
2849 cm->subsampling_x, cm->subsampling_y,
2850 VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
2851 NULL, NULL, NULL);
2852 scale_and_extend_frame(ref, &new_fb_ptr->buf);
2853 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
2854 alloc_frame_mvs(cm, new_fb);
2855 }
2856 #endif // CONFIG_VP9_HIGHBITDEPTH
2857 } else {
2858 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
2859 RefCntBuffer *const buf = &pool->frame_bufs[buf_idx];
2860 buf->buf.y_crop_width = ref->y_crop_width;
2861 buf->buf.y_crop_height = ref->y_crop_height;
2862 cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
2863 ++buf->ref_count;
2864 }
2865 } else {
2866 if (cpi->oxcf.pass != 0 || cpi->use_svc)
2867 cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
2868 }
2869 }
2870 }
2871
2872 static void release_scaled_references(VP9_COMP *cpi) {
2873 VP9_COMMON *cm = &cpi->common;
2874 int i;
2875 if (cpi->oxcf.pass == 0 && !cpi->use_svc) {
2876 // Only release scaled references under certain conditions:
2877 // if reference will be updated, or if scaled reference has same resolution.
2878 int refresh[3];
2879 refresh[0] = (cpi->refresh_last_frame) ? 1 : 0;
2880 refresh[1] = (cpi->refresh_golden_frame) ? 1 : 0;
2881 refresh[2] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
2882 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
2883 const int idx = cpi->scaled_ref_idx[i - 1];
2884 RefCntBuffer *const buf = idx != INVALID_IDX ?
2885 &cm->buffer_pool->frame_bufs[idx] : NULL;
2886 const YV12_BUFFER_CONFIG *const ref = get_ref_frame_buffer(cpi, i);
2887 if (buf != NULL &&
2888 (refresh[i - 1] ||
2889 (buf->buf.y_crop_width == ref->y_crop_width &&
2890 buf->buf.y_crop_height == ref->y_crop_height))) {
2891 --buf->ref_count;
2892 cpi->scaled_ref_idx[i -1] = INVALID_IDX;
2893 }
2894 }
2895 } else {
2896 for (i = 0; i < MAX_REF_FRAMES; ++i) {
2897 const int idx = cpi->scaled_ref_idx[i];
2898 RefCntBuffer *const buf = idx != INVALID_IDX ?
2899 &cm->buffer_pool->frame_bufs[idx] : NULL;
2900 if (buf != NULL) {
2901 --buf->ref_count;
2902 cpi->scaled_ref_idx[i] = INVALID_IDX;
2903 }
2904 }
2905 }
2906 }
2907
2908 static void full_to_model_count(unsigned int *model_count,
2909 unsigned int *full_count) {
2910 int n;
2911 model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
2912 model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
2913 model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
2914 for (n = THREE_TOKEN; n < EOB_TOKEN; ++n)
2915 model_count[TWO_TOKEN] += full_count[n];
2916 model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
2917 }
2918
2919 static void full_to_model_counts(vp9_coeff_count_model *model_count,
2920 vp9_coeff_count *full_count) {
2921 int i, j, k, l;
2922
2923 for (i = 0; i < PLANE_TYPES; ++i)
2924 for (j = 0; j < REF_TYPES; ++j)
2925 for (k = 0; k < COEF_BANDS; ++k)
2926 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
2927 full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
2928 }
2929
2930 #if 0 && CONFIG_INTERNAL_STATS
2931 static void output_frame_level_debug_stats(VP9_COMP *cpi) {
2932 VP9_COMMON *const cm = &cpi->common;
2933 FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
2934 int64_t recon_err;
2935
2936 vpx_clear_system_state();
2937
2938 recon_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
2939
2940 if (cpi->twopass.total_left_stats.coded_error != 0.0)
2941 fprintf(f, "%10u %dx%d %d %d %10d %10d %10d %10d"
2942 "%10"PRId64" %10"PRId64" %5d %5d %10"PRId64" "
2943 "%10"PRId64" %10"PRId64" %10d "
2944 "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
2945 "%6d %6d %5d %5d %5d "
2946 "%10"PRId64" %10.3lf"
2947 "%10lf %8u %10"PRId64" %10d %10d %10d\n",
2948 cpi->common.current_video_frame,
2949 cm->width, cm->height,
2950 cpi->rc.source_alt_ref_pending,
2951 cpi->rc.source_alt_ref_active,
2952 cpi->rc.this_frame_target,
2953 cpi->rc.projected_frame_size,
2954 cpi->rc.projected_frame_size / cpi->common.MBs,
2955 (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
2956 cpi->rc.vbr_bits_off_target,
2957 cpi->rc.vbr_bits_off_target_fast,
2958 cpi->twopass.extend_minq,
2959 cpi->twopass.extend_minq_fast,
2960 cpi->rc.total_target_vs_actual,
2961 (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
2962 cpi->rc.total_actual_bits, cm->base_qindex,
2963 vp9_convert_qindex_to_q(cm->base_qindex, cm->bit_depth),
2964 (double)vp9_dc_quant(cm->base_qindex, 0, cm->bit_depth) / 4.0,
2965 vp9_convert_qindex_to_q(cpi->twopass.active_worst_quality,
2966 cm->bit_depth),
2967 cpi->rc.avg_q,
2968 vp9_convert_qindex_to_q(cpi->oxcf.cq_level, cm->bit_depth),
2969 cpi->refresh_last_frame, cpi->refresh_golden_frame,
2970 cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
2971 cpi->twopass.bits_left,
2972 cpi->twopass.total_left_stats.coded_error,
2973 cpi->twopass.bits_left /
2974 (1 + cpi->twopass.total_left_stats.coded_error),
2975 cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
2976 cpi->twopass.kf_zeromotion_pct,
2977 cpi->twopass.fr_content_type);
2978
2979 fclose(f);
2980
2981 if (0) {
2982 FILE *const fmodes = fopen("Modes.stt", "a");
2983 int i;
2984
2985 fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
2986 cm->frame_type, cpi->refresh_golden_frame,
2987 cpi->refresh_alt_ref_frame);
2988
2989 for (i = 0; i < MAX_MODES; ++i)
2990 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
2991
2992 fprintf(fmodes, "\n");
2993
2994 fclose(fmodes);
2995 }
2996 }
2997 #endif
2998
2999 static void set_mv_search_params(VP9_COMP *cpi) {
3000 const VP9_COMMON *const cm = &cpi->common;
3001 const unsigned int max_mv_def = MIN(cm->width, cm->height);
3002
3003 // Default based on max resolution.
3004 cpi->mv_step_param = vp9_init_search_range(max_mv_def);
3005
3006 if (cpi->sf.mv.auto_mv_step_size) {
3007 if (frame_is_intra_only(cm)) {
3008 // Initialize max_mv_magnitude for use in the first INTER frame
3009 // after a key/intra-only frame.
3010 cpi->max_mv_magnitude = max_mv_def;
3011 } else {
3012 if (cm->show_frame) {
3013 // Allow mv_steps to correspond to twice the max mv magnitude found
3014 // in the previous frame, capped by the default max_mv_magnitude based
3015 // on resolution.
3016 cpi->mv_step_param =
3017 vp9_init_search_range(MIN(max_mv_def, 2 * cpi->max_mv_magnitude));
3018 }
3019 cpi->max_mv_magnitude = 0;
3020 }
3021 }
3022 }
3023
3024 static void set_size_independent_vars(VP9_COMP *cpi) {
3025 vp9_set_speed_features_framesize_independent(cpi);
3026 vp9_set_rd_speed_thresholds(cpi);
3027 vp9_set_rd_speed_thresholds_sub8x8(cpi);
3028 cpi->common.interp_filter = cpi->sf.default_interp_filter;
3029 }
3030
3031 static void set_size_dependent_vars(VP9_COMP *cpi, int *q,
3032 int *bottom_index, int *top_index) {
3033 VP9_COMMON *const cm = &cpi->common;
3034 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
3035
3036 // Setup variables that depend on the dimensions of the frame.
3037 vp9_set_speed_features_framesize_dependent(cpi);
3038
3039 // Decide q and q bounds.
3040 *q = vp9_rc_pick_q_and_bounds(cpi, bottom_index, top_index);
3041
3042 if (!frame_is_intra_only(cm)) {
3043 vp9_set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH);
3044 }
3045
3046 // Configure experimental use of segmentation for enhanced coding of
3047 // static regions if indicated.
3048 // Only allowed in the second pass of a two pass encode, as it requires
3049 // lagged coding, and if the relevant speed feature flag is set.
3050 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
3051 configure_static_seg_features(cpi);
3052
3053 #if CONFIG_VP9_POSTPROC
3054 if (oxcf->noise_sensitivity > 0) {
3055 int l = 0;
3056 switch (oxcf->noise_sensitivity) {
3057 case 1:
3058 l = 20;
3059 break;
3060 case 2:
3061 l = 40;
3062 break;
3063 case 3:
3064 l = 60;
3065 break;
3066 case 4:
3067 case 5:
3068 l = 100;
3069 break;
3070 case 6:
3071 l = 150;
3072 break;
3073 }
3074 vp9_denoise(cpi->Source, cpi->Source, l);
3075 }
3076 #endif // CONFIG_VP9_POSTPROC
3077 }
3078
3079 static void init_motion_estimation(VP9_COMP *cpi) {
3080 int y_stride = cpi->scaled_source.y_stride;
3081
3082 if (cpi->sf.mv.search_method == NSTEP) {
3083 vp9_init3smotion_compensation(&cpi->ss_cfg, y_stride);
3084 } else if (cpi->sf.mv.search_method == DIAMOND) {
3085 vp9_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
3086 }
3087 }
3088
3089 static void set_frame_size(VP9_COMP *cpi) {
3090 int ref_frame;
3091 VP9_COMMON *const cm = &cpi->common;
3092 VP9EncoderConfig *const oxcf = &cpi->oxcf;
3093 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
3094
3095 if (oxcf->pass == 2 &&
3096 oxcf->rc_mode == VPX_VBR &&
3097 ((oxcf->resize_mode == RESIZE_FIXED && cm->current_video_frame == 0) ||
3098 (oxcf->resize_mode == RESIZE_DYNAMIC && cpi->resize_pending))) {
3099 calculate_coded_size(
3100 cpi, &oxcf->scaled_frame_width, &oxcf->scaled_frame_height);
3101
3102 // There has been a change in frame size.
3103 vp9_set_size_literal(cpi, oxcf->scaled_frame_width,
3104 oxcf->scaled_frame_height);
3105 }
3106
3107 if (oxcf->pass == 0 &&
3108 oxcf->rc_mode == VPX_CBR &&
3109 !cpi->use_svc &&
3110 oxcf->resize_mode == RESIZE_DYNAMIC) {
3111 if (cpi->resize_pending == 1) {
3112 oxcf->scaled_frame_width =
3113 (cm->width * cpi->resize_scale_num) / cpi->resize_scale_den;
3114 oxcf->scaled_frame_height =
3115 (cm->height * cpi->resize_scale_num) /cpi->resize_scale_den;
3116 } else if (cpi->resize_pending == -1) {
3117 // Go back up to original size.
3118 oxcf->scaled_frame_width = oxcf->width;
3119 oxcf->scaled_frame_height = oxcf->height;
3120 }
3121 if (cpi->resize_pending != 0) {
3122 // There has been a change in frame size.
3123 vp9_set_size_literal(cpi,
3124 oxcf->scaled_frame_width,
3125 oxcf->scaled_frame_height);
3126
3127 // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
3128 set_mv_search_params(cpi);
3129 }
3130 }
3131
3132 if ((oxcf->pass == 2) &&
3133 (!cpi->use_svc ||
3134 (is_two_pass_svc(cpi) &&
3135 cpi->svc.encode_empty_frame_state != ENCODING))) {
3136 vp9_set_target_rate(cpi);
3137 }
3138
3139 alloc_frame_mvs(cm, cm->new_fb_idx);
3140
3141 // Reset the frame pointers to the current frame size.
3142 vpx_realloc_frame_buffer(get_frame_new_buffer(cm),
3143 cm->width, cm->height,
3144 cm->subsampling_x, cm->subsampling_y,
3145 #if CONFIG_VP9_HIGHBITDEPTH
3146 cm->use_highbitdepth,
3147 #endif
3148 VP9_ENC_BORDER_IN_PIXELS, cm->byte_alignment,
3149 NULL, NULL, NULL);
3150
3151 alloc_util_frame_buffers(cpi);
3152 init_motion_estimation(cpi);
3153
3154 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3155 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - 1];
3156 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3157
3158 ref_buf->idx = buf_idx;
3159
3160 if (buf_idx != INVALID_IDX) {
3161 YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
3162 ref_buf->buf = buf;
3163 #if CONFIG_VP9_HIGHBITDEPTH
3164 vp9_setup_scale_factors_for_frame(&ref_buf->sf,
3165 buf->y_crop_width, buf->y_crop_height,
3166 cm->width, cm->height,
3167 (buf->flags & YV12_FLAG_HIGHBITDEPTH) ?
3168 1 : 0);
3169 #else
3170 vp9_setup_scale_factors_for_frame(&ref_buf->sf,
3171 buf->y_crop_width, buf->y_crop_height,
3172 cm->width, cm->height);
3173 #endif // CONFIG_VP9_HIGHBITDEPTH
3174 if (vp9_is_scaled(&ref_buf->sf))
3175 vpx_extend_frame_borders(buf);
3176 } else {
3177 ref_buf->buf = NULL;
3178 }
3179 }
3180
3181 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3182 }
3183
3184 static void encode_without_recode_loop(VP9_COMP *cpi,
3185 size_t *size,
3186 uint8_t *dest) {
3187 VP9_COMMON *const cm = &cpi->common;
3188 int q = 0, bottom_index = 0, top_index = 0; // Dummy variables.
3189
3190 vpx_clear_system_state();
3191
3192 set_frame_size(cpi);
3193
3194 cpi->Source = vp9_scale_if_required(cm,
3195 cpi->un_scaled_source,
3196 &cpi->scaled_source);
3197 if (cpi->unscaled_last_source != NULL)
3198 cpi->Last_Source = vp9_scale_if_required(cm,
3199 cpi->unscaled_last_source,
3200 &cpi->scaled_last_source);
3201
3202 if (cpi->oxcf.pass == 0 &&
3203 cpi->oxcf.rc_mode == VPX_CBR &&
3204 cpi->resize_state == 0 &&
3205 cm->frame_type != KEY_FRAME &&
3206 cpi->oxcf.content == VP9E_CONTENT_SCREEN)
3207 vp9_avg_source_sad(cpi);
3208
3209 if (frame_is_intra_only(cm) == 0) {
3210 vp9_scale_references(cpi);
3211 }
3212
3213 set_size_independent_vars(cpi);
3214 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
3215
3216 vp9_set_quantizer(cm, q);
3217 vp9_set_variance_partition_thresholds(cpi, q);
3218
3219 setup_frame(cpi);
3220
3221 suppress_active_map(cpi);
3222 // Variance adaptive and in frame q adjustment experiments are mutually
3223 // exclusive.
3224 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
3225 vp9_vaq_frame_setup(cpi);
3226 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
3227 vp9_setup_in_frame_q_adj(cpi);
3228 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
3229 vp9_cyclic_refresh_setup(cpi);
3230 }
3231 apply_active_map(cpi);
3232
3233 // transform / motion compensation build reconstruction frame
3234 vp9_encode_frame(cpi);
3235
3236 // Check if we should drop this frame because of high overshoot.
3237 // Only for frames where high temporal-source sad is detected.
3238 if (cpi->oxcf.pass == 0 &&
3239 cpi->oxcf.rc_mode == VPX_CBR &&
3240 cpi->resize_state == 0 &&
3241 cm->frame_type != KEY_FRAME &&
3242 cpi->oxcf.content == VP9E_CONTENT_SCREEN &&
3243 cpi->rc.high_source_sad == 1) {
3244 int frame_size = 0;
3245 // Get an estimate of the encoded frame size.
3246 save_coding_context(cpi);
3247 vp9_pack_bitstream(cpi, dest, size);
3248 restore_coding_context(cpi);
3249 frame_size = (int)(*size) << 3;
3250 // Check if encoded frame will overshoot too much, and if so, set the q and
3251 // adjust some rate control parameters, and return to re-encode the frame.
3252 if (vp9_encodedframe_overshoot(cpi, frame_size, &q)) {
3253 vpx_clear_system_state();
3254 vp9_set_quantizer(cm, q);
3255 vp9_set_variance_partition_thresholds(cpi, q);
3256 suppress_active_map(cpi);
3257 // Turn-off cyclic refresh for re-encoded frame.
3258 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
3259 unsigned char *const seg_map = cpi->segmentation_map;
3260 memset(seg_map, 0, cm->mi_rows * cm->mi_cols);
3261 vp9_disable_segmentation(&cm->seg);
3262 }
3263 apply_active_map(cpi);
3264 vp9_encode_frame(cpi);
3265 }
3266 }
3267
3268 // Update some stats from cyclic refresh, and check if we should not update
3269 // golden reference, for non-SVC 1 pass CBR.
3270 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
3271 cm->frame_type != KEY_FRAME &&
3272 !cpi->use_svc &&
3273 (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR))
3274 vp9_cyclic_refresh_check_golden_update(cpi);
3275
3276 // Update the skip mb flag probabilities based on the distribution
3277 // seen in the last encoder iteration.
3278 // update_base_skip_probs(cpi);
3279 vpx_clear_system_state();
3280 }
3281
3282 static void encode_with_recode_loop(VP9_COMP *cpi,
3283 size_t *size,
3284 uint8_t *dest) {
3285 VP9_COMMON *const cm = &cpi->common;
3286 RATE_CONTROL *const rc = &cpi->rc;
3287 int bottom_index, top_index;
3288 int loop_count = 0;
3289 int loop_at_this_size = 0;
3290 int loop = 0;
3291 int overshoot_seen = 0;
3292 int undershoot_seen = 0;
3293 int frame_over_shoot_limit;
3294 int frame_under_shoot_limit;
3295 int q = 0, q_low = 0, q_high = 0;
3296
3297 set_size_independent_vars(cpi);
3298
3299 do {
3300 vpx_clear_system_state();
3301
3302 set_frame_size(cpi);
3303
3304 if (loop_count == 0 || cpi->resize_pending != 0) {
3305 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
3306
3307 // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
3308 set_mv_search_params(cpi);
3309
3310 // Reset the loop state for new frame size.
3311 overshoot_seen = 0;
3312 undershoot_seen = 0;
3313
3314 // Reconfiguration for change in frame size has concluded.
3315 cpi->resize_pending = 0;
3316
3317 q_low = bottom_index;
3318 q_high = top_index;
3319
3320 loop_at_this_size = 0;
3321 }
3322
3323 // Decide frame size bounds first time through.
3324 if (loop_count == 0) {
3325 vp9_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
3326 &frame_under_shoot_limit,
3327 &frame_over_shoot_limit);
3328 }
3329
3330 cpi->Source = vp9_scale_if_required(cm, cpi->un_scaled_source,
3331 &cpi->scaled_source);
3332
3333 if (cpi->unscaled_last_source != NULL)
3334 cpi->Last_Source = vp9_scale_if_required(cm, cpi->unscaled_last_source,
3335 &cpi->scaled_last_source);
3336
3337 if (frame_is_intra_only(cm) == 0) {
3338 if (loop_count > 0) {
3339 release_scaled_references(cpi);
3340 }
3341 vp9_scale_references(cpi);
3342 }
3343
3344 vp9_set_quantizer(cm, q);
3345
3346 if (loop_count == 0)
3347 setup_frame(cpi);
3348
3349 // Variance adaptive and in frame q adjustment experiments are mutually
3350 // exclusive.
3351 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
3352 vp9_vaq_frame_setup(cpi);
3353 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
3354 vp9_setup_in_frame_q_adj(cpi);
3355 }
3356
3357 // transform / motion compensation build reconstruction frame
3358 vp9_encode_frame(cpi);
3359
3360 // Update the skip mb flag probabilities based on the distribution
3361 // seen in the last encoder iteration.
3362 // update_base_skip_probs(cpi);
3363
3364 vpx_clear_system_state();
3365
3366 // Dummy pack of the bitstream using up to date stats to get an
3367 // accurate estimate of output frame size to determine if we need
3368 // to recode.
3369 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
3370 save_coding_context(cpi);
3371 if (!cpi->sf.use_nonrd_pick_mode)
3372 vp9_pack_bitstream(cpi, dest, size);
3373
3374 rc->projected_frame_size = (int)(*size) << 3;
3375 restore_coding_context(cpi);
3376
3377 if (frame_over_shoot_limit == 0)
3378 frame_over_shoot_limit = 1;
3379 }
3380
3381 if (cpi->oxcf.rc_mode == VPX_Q) {
3382 loop = 0;
3383 } else {
3384 if ((cm->frame_type == KEY_FRAME) &&
3385 rc->this_key_frame_forced &&
3386 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
3387 int last_q = q;
3388 int64_t kf_err;
3389
3390 int64_t high_err_target = cpi->ambient_err;
3391 int64_t low_err_target = cpi->ambient_err >> 1;
3392
3393 #if CONFIG_VP9_HIGHBITDEPTH
3394 if (cm->use_highbitdepth) {
3395 kf_err = vp9_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3396 } else {
3397 kf_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3398 }
3399 #else
3400 kf_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3401 #endif // CONFIG_VP9_HIGHBITDEPTH
3402
3403 // Prevent possible divide by zero error below for perfect KF
3404 kf_err += !kf_err;
3405
3406 // The key frame is not good enough or we can afford
3407 // to make it better without undue risk of popping.
3408 if ((kf_err > high_err_target &&
3409 rc->projected_frame_size <= frame_over_shoot_limit) ||
3410 (kf_err > low_err_target &&
3411 rc->projected_frame_size <= frame_under_shoot_limit)) {
3412 // Lower q_high
3413 q_high = q > q_low ? q - 1 : q_low;
3414
3415 // Adjust Q
3416 q = (int)((q * high_err_target) / kf_err);
3417 q = MIN(q, (q_high + q_low) >> 1);
3418 } else if (kf_err < low_err_target &&
3419 rc->projected_frame_size >= frame_under_shoot_limit) {
3420 // The key frame is much better than the previous frame
3421 // Raise q_low
3422 q_low = q < q_high ? q + 1 : q_high;
3423
3424 // Adjust Q
3425 q = (int)((q * low_err_target) / kf_err);
3426 q = MIN(q, (q_high + q_low + 1) >> 1);
3427 }
3428
3429 // Clamp Q to upper and lower limits:
3430 q = clamp(q, q_low, q_high);
3431
3432 loop = q != last_q;
3433 } else if (recode_loop_test(
3434 cpi, frame_over_shoot_limit, frame_under_shoot_limit,
3435 q, MAX(q_high, top_index), bottom_index)) {
3436 // Is the projected frame size out of range and are we allowed
3437 // to attempt to recode.
3438 int last_q = q;
3439 int retries = 0;
3440
3441 if (cpi->resize_pending == 1) {
3442 // Change in frame size so go back around the recode loop.
3443 cpi->rc.frame_size_selector =
3444 SCALE_STEP1 - cpi->rc.frame_size_selector;
3445 cpi->rc.next_frame_size_selector = cpi->rc.frame_size_selector;
3446
3447 #if CONFIG_INTERNAL_STATS
3448 ++cpi->tot_recode_hits;
3449 #endif
3450 ++loop_count;
3451 loop = 1;
3452 continue;
3453 }
3454
3455 // Frame size out of permitted range:
3456 // Update correction factor & compute new Q to try...
3457
3458 // Frame is too large
3459 if (rc->projected_frame_size > rc->this_frame_target) {
3460 // Special case if the projected size is > the max allowed.
3461 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
3462 q_high = rc->worst_quality;
3463
3464 // Raise Qlow as to at least the current value
3465 q_low = q < q_high ? q + 1 : q_high;
3466
3467 if (undershoot_seen || loop_at_this_size > 1) {
3468 // Update rate_correction_factor unless
3469 vp9_rc_update_rate_correction_factors(cpi);
3470
3471 q = (q_high + q_low + 1) / 2;
3472 } else {
3473 // Update rate_correction_factor unless
3474 vp9_rc_update_rate_correction_factors(cpi);
3475
3476 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
3477 bottom_index, MAX(q_high, top_index));
3478
3479 while (q < q_low && retries < 10) {
3480 vp9_rc_update_rate_correction_factors(cpi);
3481 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
3482 bottom_index, MAX(q_high, top_index));
3483 retries++;
3484 }
3485 }
3486
3487 overshoot_seen = 1;
3488 } else {
3489 // Frame is too small
3490 q_high = q > q_low ? q - 1 : q_low;
3491
3492 if (overshoot_seen || loop_at_this_size > 1) {
3493 vp9_rc_update_rate_correction_factors(cpi);
3494 q = (q_high + q_low) / 2;
3495 } else {
3496 vp9_rc_update_rate_correction_factors(cpi);
3497 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
3498 bottom_index, top_index);
3499 // Special case reset for qlow for constrained quality.
3500 // This should only trigger where there is very substantial
3501 // undershoot on a frame and the auto cq level is above
3502 // the user passsed in value.
3503 if (cpi->oxcf.rc_mode == VPX_CQ &&
3504 q < q_low) {
3505 q_low = q;
3506 }
3507
3508 while (q > q_high && retries < 10) {
3509 vp9_rc_update_rate_correction_factors(cpi);
3510 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
3511 bottom_index, top_index);
3512 retries++;
3513 }
3514 }
3515
3516 undershoot_seen = 1;
3517 }
3518
3519 // Clamp Q to upper and lower limits:
3520 q = clamp(q, q_low, q_high);
3521
3522 loop = (q != last_q);
3523 } else {
3524 loop = 0;
3525 }
3526 }
3527
3528 // Special case for overlay frame.
3529 if (rc->is_src_frame_alt_ref &&
3530 rc->projected_frame_size < rc->max_frame_bandwidth)
3531 loop = 0;
3532
3533 if (loop) {
3534 ++loop_count;
3535 ++loop_at_this_size;
3536
3537 #if CONFIG_INTERNAL_STATS
3538 ++cpi->tot_recode_hits;
3539 #endif
3540 }
3541 } while (loop);
3542 }
3543
3544 static int get_ref_frame_flags(const VP9_COMP *cpi) {
3545 const int *const map = cpi->common.ref_frame_map;
3546 const int gold_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idx];
3547 const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idx];
3548 const int gold_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
3549 int flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
3550
3551 if (gold_is_last)
3552 flags &= ~VP9_GOLD_FLAG;
3553
3554 if (cpi->rc.frames_till_gf_update_due == INT_MAX &&
3555 (cpi->svc.number_temporal_layers == 1 &&
3556 cpi->svc.number_spatial_layers == 1))
3557 flags &= ~VP9_GOLD_FLAG;
3558
3559 if (alt_is_last)
3560 flags &= ~VP9_ALT_FLAG;
3561
3562 if (gold_is_alt)
3563 flags &= ~VP9_ALT_FLAG;
3564
3565 return flags;
3566 }
3567
3568 static void set_ext_overrides(VP9_COMP *cpi) {
3569 // Overrides the defaults with the externally supplied values with
3570 // vp9_update_reference() and vp9_update_entropy() calls
3571 // Note: The overrides are valid only for the next frame passed
3572 // to encode_frame_to_data_rate() function
3573 if (cpi->ext_refresh_frame_context_pending) {
3574 cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
3575 cpi->ext_refresh_frame_context_pending = 0;
3576 }
3577 if (cpi->ext_refresh_frame_flags_pending) {
3578 cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
3579 cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
3580 cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
3581 cpi->ext_refresh_frame_flags_pending = 0;
3582 }
3583 }
3584
3585 YV12_BUFFER_CONFIG *vp9_scale_if_required(VP9_COMMON *cm,
3586 YV12_BUFFER_CONFIG *unscaled,
3587 YV12_BUFFER_CONFIG *scaled) {
3588 if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
3589 cm->mi_rows * MI_SIZE != unscaled->y_height) {
3590 #if CONFIG_VP9_HIGHBITDEPTH
3591 if (unscaled->y_width == (scaled->y_width << 1) &&
3592 unscaled->y_height == (scaled->y_height << 1))
3593 scale_and_extend_frame(unscaled, scaled, (int)cm->bit_depth);
3594 else
3595 scale_and_extend_frame_nonnormative(unscaled, scaled, (int)cm->bit_depth);
3596 #else
3597 // Use the faster normative (convolve8) scaling filter: for now only for
3598 // scaling factor of 2.
3599 if (unscaled->y_width == (scaled->y_width << 1) &&
3600 unscaled->y_height == (scaled->y_height << 1))
3601 scale_and_extend_frame(unscaled, scaled);
3602 else
3603 scale_and_extend_frame_nonnormative(unscaled, scaled);
3604 #endif // CONFIG_VP9_HIGHBITDEPTH
3605 return scaled;
3606 } else {
3607 return unscaled;
3608 }
3609 }
3610
3611 static void set_arf_sign_bias(VP9_COMP *cpi) {
3612 VP9_COMMON *const cm = &cpi->common;
3613 int arf_sign_bias;
3614
3615 if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
3616 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3617 arf_sign_bias = cpi->rc.source_alt_ref_active &&
3618 (!cpi->refresh_alt_ref_frame ||
3619 (gf_group->rf_level[gf_group->index] == GF_ARF_LOW));
3620 } else {
3621 arf_sign_bias =
3622 (cpi->rc.source_alt_ref_active && !cpi->refresh_alt_ref_frame);
3623 }
3624 cm->ref_frame_sign_bias[ALTREF_FRAME] = arf_sign_bias;
3625 }
3626
3627 static int setup_interp_filter_search_mask(VP9_COMP *cpi) {
3628 INTERP_FILTER ifilter;
3629 int ref_total[MAX_REF_FRAMES] = {0};
3630 MV_REFERENCE_FRAME ref;
3631 int mask = 0;
3632 if (cpi->common.last_frame_type == KEY_FRAME ||
3633 cpi->refresh_alt_ref_frame)
3634 return mask;
3635 for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
3636 for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter)
3637 ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
3638
3639 for (ifilter = EIGHTTAP; ifilter <= EIGHTTAP_SHARP; ++ifilter) {
3640 if ((ref_total[LAST_FRAME] &&
3641 cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
3642 (ref_total[GOLDEN_FRAME] == 0 ||
3643 cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50
3644 < ref_total[GOLDEN_FRAME]) &&
3645 (ref_total[ALTREF_FRAME] == 0 ||
3646 cpi->interp_filter_selected[ALTREF_FRAME][ifilter] * 50
3647 < ref_total[ALTREF_FRAME]))
3648 mask |= 1 << ifilter;
3649 }
3650 return mask;
3651 }
3652
3653 static void encode_frame_to_data_rate(VP9_COMP *cpi,
3654 size_t *size,
3655 uint8_t *dest,
3656 unsigned int *frame_flags) {
3657 VP9_COMMON *const cm = &cpi->common;
3658 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
3659 struct segmentation *const seg = &cm->seg;
3660 TX_SIZE t;
3661
3662 set_ext_overrides(cpi);
3663 vpx_clear_system_state();
3664
3665 // Set the arf sign bias for this frame.
3666 set_arf_sign_bias(cpi);
3667
3668 // Set default state for segment based loop filter update flags.
3669 cm->lf.mode_ref_delta_update = 0;
3670
3671 if (cpi->oxcf.pass == 2 &&
3672 cpi->sf.adaptive_interp_filter_search)
3673 cpi->sf.interp_filter_search_mask =
3674 setup_interp_filter_search_mask(cpi);
3675
3676 // Set various flags etc to special state if it is a key frame.
3677 if (frame_is_intra_only(cm)) {
3678 // Reset the loop filter deltas and segmentation map.
3679 vp9_reset_segment_features(&cm->seg);
3680
3681 // If segmentation is enabled force a map update for key frames.
3682 if (seg->enabled) {
3683 seg->update_map = 1;
3684 seg->update_data = 1;
3685 }
3686
3687 // The alternate reference frame cannot be active for a key frame.
3688 cpi->rc.source_alt_ref_active = 0;
3689
3690 cm->error_resilient_mode = oxcf->error_resilient_mode;
3691 cm->frame_parallel_decoding_mode = oxcf->frame_parallel_decoding_mode;
3692
3693 // By default, encoder assumes decoder can use prev_mi.
3694 if (cm->error_resilient_mode) {
3695 cm->frame_parallel_decoding_mode = 1;
3696 cm->reset_frame_context = 0;
3697 cm->refresh_frame_context = 0;
3698 } else if (cm->intra_only) {
3699 // Only reset the current context.
3700 cm->reset_frame_context = 2;
3701 }
3702 }
3703 if (is_two_pass_svc(cpi) && cm->error_resilient_mode == 0) {
3704 // Use context 0 for intra only empty frame, but the last frame context
3705 // for other empty frames.
3706 if (cpi->svc.encode_empty_frame_state == ENCODING) {
3707 if (cpi->svc.encode_intra_empty_frame != 0)
3708 cm->frame_context_idx = 0;
3709 else
3710 cm->frame_context_idx = FRAME_CONTEXTS - 1;
3711 } else {
3712 cm->frame_context_idx =
3713 cpi->svc.spatial_layer_id * cpi->svc.number_temporal_layers +
3714 cpi->svc.temporal_layer_id;
3715 }
3716
3717 cm->frame_parallel_decoding_mode = oxcf->frame_parallel_decoding_mode;
3718
3719 // The probs will be updated based on the frame type of its previous
3720 // frame if frame_parallel_decoding_mode is 0. The type may vary for
3721 // the frame after a key frame in base layer since we may drop enhancement
3722 // layers. So set frame_parallel_decoding_mode to 1 in this case.
3723 if (cm->frame_parallel_decoding_mode == 0) {
3724 if (cpi->svc.number_temporal_layers == 1) {
3725 if (cpi->svc.spatial_layer_id == 0 &&
3726 cpi->svc.layer_context[0].last_frame_type == KEY_FRAME)
3727 cm->frame_parallel_decoding_mode = 1;
3728 } else if (cpi->svc.spatial_layer_id == 0) {
3729 // Find the 2nd frame in temporal base layer and 1st frame in temporal
3730 // enhancement layers from the key frame.
3731 int i;
3732 for (i = 0; i < cpi->svc.number_temporal_layers; ++i) {
3733 if (cpi->svc.layer_context[0].frames_from_key_frame == 1 << i) {
3734 cm->frame_parallel_decoding_mode = 1;
3735 break;
3736 }
3737 }
3738 }
3739 }
3740 }
3741
3742 // For 1 pass CBR, check if we are dropping this frame.
3743 // Never drop on key frame.
3744 if (oxcf->pass == 0 &&
3745 oxcf->rc_mode == VPX_CBR &&
3746 cm->frame_type != KEY_FRAME) {
3747 if (vp9_rc_drop_frame(cpi)) {
3748 vp9_rc_postencode_update_drop_frame(cpi);
3749 ++cm->current_video_frame;
3750 return;
3751 }
3752 }
3753
3754 vpx_clear_system_state();
3755
3756 #if CONFIG_INTERNAL_STATS
3757 memset(cpi->mode_chosen_counts, 0,
3758 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
3759 #endif
3760
3761 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
3762 encode_without_recode_loop(cpi, size, dest);
3763 } else {
3764 encode_with_recode_loop(cpi, size, dest);
3765 }
3766
3767 #if CONFIG_VP9_TEMPORAL_DENOISING
3768 #ifdef OUTPUT_YUV_DENOISED
3769 if (oxcf->noise_sensitivity > 0) {
3770 vp9_write_yuv_frame_420(&cpi->denoiser.running_avg_y[INTRA_FRAME],
3771 yuv_denoised_file);
3772 }
3773 #endif
3774 #endif
3775 #ifdef OUTPUT_YUV_SKINMAP
3776 if (cpi->common.current_video_frame > 1) {
3777 vp9_compute_skin_map(cpi, yuv_skinmap_file);
3778 }
3779 #endif
3780
3781 // Special case code to reduce pulsing when key frames are forced at a
3782 // fixed interval. Note the reconstruction error if it is the frame before
3783 // the force key frame
3784 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
3785 #if CONFIG_VP9_HIGHBITDEPTH
3786 if (cm->use_highbitdepth) {
3787 cpi->ambient_err = vp9_highbd_get_y_sse(cpi->Source,
3788 get_frame_new_buffer(cm));
3789 } else {
3790 cpi->ambient_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3791 }
3792 #else
3793 cpi->ambient_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
3794 #endif // CONFIG_VP9_HIGHBITDEPTH
3795 }
3796
3797 // If the encoder forced a KEY_FRAME decision
3798 if (cm->frame_type == KEY_FRAME)
3799 cpi->refresh_last_frame = 1;
3800
3801 cm->frame_to_show = get_frame_new_buffer(cm);
3802
3803 // Pick the loop filter level for the frame.
3804 loopfilter_frame(cpi, cm);
3805
3806 // build the bitstream
3807 vp9_pack_bitstream(cpi, dest, size);
3808
3809 if (cm->seg.update_map)
3810 update_reference_segmentation_map(cpi);
3811
3812 if (frame_is_intra_only(cm) == 0) {
3813 release_scaled_references(cpi);
3814 }
3815 vp9_update_reference_frames(cpi);
3816
3817 for (t = TX_4X4; t <= TX_32X32; t++)
3818 full_to_model_counts(cpi->td.counts->coef[t],
3819 cpi->td.rd_counts.coef_counts[t]);
3820
3821 if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode)
3822 vp9_adapt_coef_probs(cm);
3823
3824 if (!frame_is_intra_only(cm)) {
3825 if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
3826 vp9_adapt_mode_probs(cm);
3827 vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
3828 }
3829 }
3830
3831 if (cpi->refresh_golden_frame == 1)
3832 cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
3833 else
3834 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
3835
3836 if (cpi->refresh_alt_ref_frame == 1)
3837 cpi->frame_flags |= FRAMEFLAGS_ALTREF;
3838 else
3839 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
3840
3841 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
3842
3843 cm->last_frame_type = cm->frame_type;
3844
3845 if (!(is_two_pass_svc(cpi) && cpi->svc.encode_empty_frame_state == ENCODING))
3846 vp9_rc_postencode_update(cpi, *size);
3847
3848 #if 0
3849 output_frame_level_debug_stats(cpi);
3850 #endif
3851
3852 if (cm->frame_type == KEY_FRAME) {
3853 // Tell the caller that the frame was coded as a key frame
3854 *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
3855 } else {
3856 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
3857 }
3858
3859 // Clear the one shot update flags for segmentation map and mode/ref loop
3860 // filter deltas.
3861 cm->seg.update_map = 0;
3862 cm->seg.update_data = 0;
3863 cm->lf.mode_ref_delta_update = 0;
3864
3865 // keep track of the last coded dimensions
3866 cm->last_width = cm->width;
3867 cm->last_height = cm->height;
3868
3869 // reset to normal state now that we are done.
3870 if (!cm->show_existing_frame)
3871 cm->last_show_frame = cm->show_frame;
3872
3873 if (cm->show_frame) {
3874 vp9_swap_mi_and_prev_mi(cm);
3875 // Don't increment frame counters if this was an altref buffer
3876 // update not a real frame
3877 ++cm->current_video_frame;
3878 if (cpi->use_svc)
3879 vp9_inc_frame_in_layer(cpi);
3880 }
3881 cm->prev_frame = cm->cur_frame;
3882
3883 if (cpi->use_svc)
3884 cpi->svc.layer_context[cpi->svc.spatial_layer_id *
3885 cpi->svc.number_temporal_layers +
3886 cpi->svc.temporal_layer_id].last_frame_type =
3887 cm->frame_type;
3888 }
3889
3890 static void SvcEncode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
3891 unsigned int *frame_flags) {
3892 vp9_rc_get_svc_params(cpi);
3893 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
3894 }
3895
3896 static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
3897 unsigned int *frame_flags) {
3898 if (cpi->oxcf.rc_mode == VPX_CBR) {
3899 vp9_rc_get_one_pass_cbr_params(cpi);
3900 } else {
3901 vp9_rc_get_one_pass_vbr_params(cpi);
3902 }
3903 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
3904 }
3905
3906 static void Pass2Encode(VP9_COMP *cpi, size_t *size,
3907 uint8_t *dest, unsigned int *frame_flags) {
3908 cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
3909 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
3910
3911 if (!(is_two_pass_svc(cpi) && cpi->svc.encode_empty_frame_state == ENCODING))
3912 vp9_twopass_postencode_update(cpi);
3913 }
3914
3915 static void init_ref_frame_bufs(VP9_COMMON *cm) {
3916 int i;
3917 BufferPool *const pool = cm->buffer_pool;
3918 cm->new_fb_idx = INVALID_IDX;
3919 for (i = 0; i < REF_FRAMES; ++i) {
3920 cm->ref_frame_map[i] = INVALID_IDX;
3921 pool->frame_bufs[i].ref_count = 0;
3922 }
3923 }
3924
3925 static void check_initial_width(VP9_COMP *cpi,
3926 #if CONFIG_VP9_HIGHBITDEPTH
3927 int use_highbitdepth,
3928 #endif
3929 int subsampling_x, int subsampling_y) {
3930 VP9_COMMON *const cm = &cpi->common;
3931
3932 if (!cpi->initial_width ||
3933 #if CONFIG_VP9_HIGHBITDEPTH
3934 cm->use_highbitdepth != use_highbitdepth ||
3935 #endif
3936 cm->subsampling_x != subsampling_x ||
3937 cm->subsampling_y != subsampling_y) {
3938 cm->subsampling_x = subsampling_x;
3939 cm->subsampling_y = subsampling_y;
3940 #if CONFIG_VP9_HIGHBITDEPTH
3941 cm->use_highbitdepth = use_highbitdepth;
3942 #endif
3943
3944 alloc_raw_frame_buffers(cpi);
3945 init_ref_frame_bufs(cm);
3946 alloc_util_frame_buffers(cpi);
3947
3948 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
3949
3950 cpi->initial_width = cm->width;
3951 cpi->initial_height = cm->height;
3952 cpi->initial_mbs = cm->MBs;
3953 }
3954 }
3955
3956 #if CONFIG_VP9_TEMPORAL_DENOISING
3957 static void setup_denoiser_buffer(VP9_COMP *cpi) {
3958 VP9_COMMON *const cm = &cpi->common;
3959 if (cpi->oxcf.noise_sensitivity > 0 &&
3960 !cpi->denoiser.frame_buffer_initialized) {
3961 vp9_denoiser_alloc(&(cpi->denoiser), cm->width, cm->height,
3962 cm->subsampling_x, cm->subsampling_y,
3963 #if CONFIG_VP9_HIGHBITDEPTH
3964 cm->use_highbitdepth,
3965 #endif
3966 VP9_ENC_BORDER_IN_PIXELS);
3967 }
3968 }
3969 #endif
3970
3971 int vp9_receive_raw_frame(VP9_COMP *cpi, unsigned int frame_flags,
3972 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
3973 int64_t end_time) {
3974 VP9_COMMON *cm = &cpi->common;
3975 struct vpx_usec_timer timer;
3976 int res = 0;
3977 const int subsampling_x = sd->subsampling_x;
3978 const int subsampling_y = sd->subsampling_y;
3979 #if CONFIG_VP9_HIGHBITDEPTH
3980 const int use_highbitdepth = sd->flags & YV12_FLAG_HIGHBITDEPTH;
3981 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
3982 #else
3983 check_initial_width(cpi, subsampling_x, subsampling_y);
3984 #endif // CONFIG_VP9_HIGHBITDEPTH
3985
3986 #if CONFIG_VP9_TEMPORAL_DENOISING
3987 setup_denoiser_buffer(cpi);
3988 #endif
3989 vpx_usec_timer_start(&timer);
3990
3991 if (vp9_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
3992 #if CONFIG_VP9_HIGHBITDEPTH
3993 use_highbitdepth,
3994 #endif // CONFIG_VP9_HIGHBITDEPTH
3995 frame_flags))
3996 res = -1;
3997 vpx_usec_timer_mark(&timer);
3998 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
3999
4000 if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
4001 (subsampling_x != 1 || subsampling_y != 1)) {
4002 vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
4003 "Non-4:2:0 color format requires profile 1 or 3");
4004 res = -1;
4005 }
4006 if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
4007 (subsampling_x == 1 && subsampling_y == 1)) {
4008 vpx_internal_error(&cm->error, VPX_CODEC_INVALID_PARAM,
4009 "4:2:0 color format requires profile 0 or 2");
4010 res = -1;
4011 }
4012
4013 return res;
4014 }
4015
4016
4017 static int frame_is_reference(const VP9_COMP *cpi) {
4018 const VP9_COMMON *cm = &cpi->common;
4019
4020 return cm->frame_type == KEY_FRAME ||
4021 cpi->refresh_last_frame ||
4022 cpi->refresh_golden_frame ||
4023 cpi->refresh_alt_ref_frame ||
4024 cm->refresh_frame_context ||
4025 cm->lf.mode_ref_delta_update ||
4026 cm->seg.update_map ||
4027 cm->seg.update_data;
4028 }
4029
4030 static void adjust_frame_rate(VP9_COMP *cpi,
4031 const struct lookahead_entry *source) {
4032 int64_t this_duration;
4033 int step = 0;
4034
4035 if (source->ts_start == cpi->first_time_stamp_ever) {
4036 this_duration = source->ts_end - source->ts_start;
4037 step = 1;
4038 } else {
4039 int64_t last_duration = cpi->last_end_time_stamp_seen
4040 - cpi->last_time_stamp_seen;
4041
4042 this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
4043
4044 // do a step update if the duration changes by 10%
4045 if (last_duration)
4046 step = (int)((this_duration - last_duration) * 10 / last_duration);
4047 }
4048
4049 if (this_duration) {
4050 if (step) {
4051 vp9_new_framerate(cpi, 10000000.0 / this_duration);
4052 } else {
4053 // Average this frame's rate into the last second's average
4054 // frame rate. If we haven't seen 1 second yet, then average
4055 // over the whole interval seen.
4056 const double interval = MIN((double)(source->ts_end
4057 - cpi->first_time_stamp_ever), 10000000.0);
4058 double avg_duration = 10000000.0 / cpi->framerate;
4059 avg_duration *= (interval - avg_duration + this_duration);
4060 avg_duration /= interval;
4061
4062 vp9_new_framerate(cpi, 10000000.0 / avg_duration);
4063 }
4064 }
4065 cpi->last_time_stamp_seen = source->ts_start;
4066 cpi->last_end_time_stamp_seen = source->ts_end;
4067 }
4068
4069 // Returns 0 if this is not an alt ref else the offset of the source frame
4070 // used as the arf midpoint.
4071 static int get_arf_src_index(VP9_COMP *cpi) {
4072 RATE_CONTROL *const rc = &cpi->rc;
4073 int arf_src_index = 0;
4074 if (is_altref_enabled(cpi)) {
4075 if (cpi->oxcf.pass == 2) {
4076 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4077 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
4078 arf_src_index = gf_group->arf_src_offset[gf_group->index];
4079 }
4080 } else if (rc->source_alt_ref_pending) {
4081 arf_src_index = rc->frames_till_gf_update_due;
4082 }
4083 }
4084 return arf_src_index;
4085 }
4086
4087 static void check_src_altref(VP9_COMP *cpi,
4088 const struct lookahead_entry *source) {
4089 RATE_CONTROL *const rc = &cpi->rc;
4090
4091 if (cpi->oxcf.pass == 2) {
4092 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4093 rc->is_src_frame_alt_ref =
4094 (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
4095 } else {
4096 rc->is_src_frame_alt_ref = cpi->alt_ref_source &&
4097 (source == cpi->alt_ref_source);
4098 }
4099
4100 if (rc->is_src_frame_alt_ref) {
4101 // Current frame is an ARF overlay frame.
4102 cpi->alt_ref_source = NULL;
4103
4104 // Don't refresh the last buffer for an ARF overlay frame. It will
4105 // become the GF so preserve last as an alternative prediction option.
4106 cpi->refresh_last_frame = 0;
4107 }
4108 }
4109
4110 #if CONFIG_INTERNAL_STATS
4111 extern double vp9_get_blockiness(const uint8_t *img1, int img1_pitch,
4112 const uint8_t *img2, int img2_pitch,
4113 int width, int height);
4114
4115 static void adjust_image_stat(double y, double u, double v, double all,
4116 ImageStat *s) {
4117 s->stat[Y] += y;
4118 s->stat[U] += u;
4119 s->stat[V] += v;
4120 s->stat[ALL] += all;
4121 s->worst = MIN(s->worst, all);
4122 }
4123 #endif // CONFIG_INTERNAL_STATS
4124
4125 int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
4126 size_t *size, uint8_t *dest,
4127 int64_t *time_stamp, int64_t *time_end, int flush) {
4128 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
4129 VP9_COMMON *const cm = &cpi->common;
4130 BufferPool *const pool = cm->buffer_pool;
4131 RATE_CONTROL *const rc = &cpi->rc;
4132 struct vpx_usec_timer cmptimer;
4133 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
4134 struct lookahead_entry *last_source = NULL;
4135 struct lookahead_entry *source = NULL;
4136 int arf_src_index;
4137 int i;
4138
4139 if (is_two_pass_svc(cpi)) {
4140 #if CONFIG_SPATIAL_SVC
4141 vp9_svc_start_frame(cpi);
4142 // Use a small empty frame instead of a real frame
4143 if (cpi->svc.encode_empty_frame_state == ENCODING)
4144 source = &cpi->svc.empty_frame;
4145 #endif
4146 if (oxcf->pass == 2)
4147 vp9_restore_layer_context(cpi);
4148 } else if (is_one_pass_cbr_svc(cpi)) {
4149 vp9_one_pass_cbr_svc_start_layer(cpi);
4150 }
4151
4152 vpx_usec_timer_start(&cmptimer);
4153
4154 vp9_set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
4155
4156 // Is multi-arf enabled.
4157 // Note that at the moment multi_arf is only configured for 2 pass VBR and
4158 // will not work properly with svc.
4159 if ((oxcf->pass == 2) && !cpi->use_svc &&
4160 (cpi->oxcf.enable_auto_arf > 1))
4161 cpi->multi_arf_allowed = 1;
4162 else
4163 cpi->multi_arf_allowed = 0;
4164
4165 // Normal defaults
4166 cm->reset_frame_context = 0;
4167 cm->refresh_frame_context = 1;
4168 if (!is_one_pass_cbr_svc(cpi)) {
4169 cpi->refresh_last_frame = 1;
4170 cpi->refresh_golden_frame = 0;
4171 cpi->refresh_alt_ref_frame = 0;
4172 }
4173
4174 // Should we encode an arf frame.
4175 arf_src_index = get_arf_src_index(cpi);
4176
4177 // Skip alt frame if we encode the empty frame
4178 if (is_two_pass_svc(cpi) && source != NULL)
4179 arf_src_index = 0;
4180
4181 if (arf_src_index) {
4182 assert(arf_src_index <= rc->frames_to_key);
4183
4184 if ((source = vp9_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
4185 cpi->alt_ref_source = source;
4186
4187 #if CONFIG_SPATIAL_SVC
4188 if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0) {
4189 int i;
4190 // Reference a hidden frame from a lower layer
4191 for (i = cpi->svc.spatial_layer_id - 1; i >= 0; --i) {
4192 if (oxcf->ss_enable_auto_arf[i]) {
4193 cpi->gld_fb_idx = cpi->svc.layer_context[i].alt_ref_idx;
4194 break;
4195 }
4196 }
4197 }
4198 cpi->svc.layer_context[cpi->svc.spatial_layer_id].has_alt_frame = 1;
4199 #endif
4200
4201 if (oxcf->arnr_max_frames > 0) {
4202 // Produce the filtered ARF frame.
4203 vp9_temporal_filter(cpi, arf_src_index);
4204 vpx_extend_frame_borders(&cpi->alt_ref_buffer);
4205 force_src_buffer = &cpi->alt_ref_buffer;
4206 }
4207
4208 cm->show_frame = 0;
4209 cm->intra_only = 0;
4210 cpi->refresh_alt_ref_frame = 1;
4211 cpi->refresh_golden_frame = 0;
4212 cpi->refresh_last_frame = 0;
4213 rc->is_src_frame_alt_ref = 0;
4214 rc->source_alt_ref_pending = 0;
4215 } else {
4216 rc->source_alt_ref_pending = 0;
4217 }
4218 }
4219
4220 if (!source) {
4221 // Get last frame source.
4222 if (cm->current_video_frame > 0) {
4223 if ((last_source = vp9_lookahead_peek(cpi->lookahead, -1)) == NULL)
4224 return -1;
4225 }
4226
4227 // Read in the source frame.
4228 if (cpi->use_svc)
4229 source = vp9_svc_lookahead_pop(cpi, cpi->lookahead, flush);
4230 else
4231 source = vp9_lookahead_pop(cpi->lookahead, flush);
4232
4233 if (source != NULL) {
4234 cm->show_frame = 1;
4235 cm->intra_only = 0;
4236 // if the flags indicate intra frame, but if the current picture is for
4237 // non-zero spatial layer, it should not be an intra picture.
4238 // TODO(Won Kap): this needs to change if per-layer intra frame is
4239 // allowed.
4240 if ((source->flags & VPX_EFLAG_FORCE_KF) && cpi->svc.spatial_layer_id) {
4241 source->flags &= ~(unsigned int)(VPX_EFLAG_FORCE_KF);
4242 }
4243
4244 // Check to see if the frame should be encoded as an arf overlay.
4245 check_src_altref(cpi, source);
4246 }
4247 }
4248
4249 if (source) {
4250 cpi->un_scaled_source = cpi->Source = force_src_buffer ? force_src_buffer
4251 : &source->img;
4252
4253 cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
4254
4255 *time_stamp = source->ts_start;
4256 *time_end = source->ts_end;
4257 *frame_flags = (source->flags & VPX_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
4258
4259 } else {
4260 *size = 0;
4261 if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
4262 vp9_end_first_pass(cpi); /* get last stats packet */
4263 cpi->twopass.first_pass_done = 1;
4264 }
4265 return -1;
4266 }
4267
4268 if (source->ts_start < cpi->first_time_stamp_ever) {
4269 cpi->first_time_stamp_ever = source->ts_start;
4270 cpi->last_end_time_stamp_seen = source->ts_start;
4271 }
4272
4273 // Clear down mmx registers
4274 vpx_clear_system_state();
4275
4276 // adjust frame rates based on timestamps given
4277 if (cm->show_frame) {
4278 adjust_frame_rate(cpi, source);
4279 }
4280
4281 if (is_one_pass_cbr_svc(cpi)) {
4282 vp9_update_temporal_layer_framerate(cpi);
4283 vp9_restore_layer_context(cpi);
4284 }
4285
4286 // Find a free buffer for the new frame, releasing the reference previously
4287 // held.
4288 if (cm->new_fb_idx != INVALID_IDX) {
4289 --pool->frame_bufs[cm->new_fb_idx].ref_count;
4290 }
4291 cm->new_fb_idx = get_free_fb(cm);
4292
4293 if (cm->new_fb_idx == INVALID_IDX)
4294 return -1;
4295
4296 cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
4297
4298 if (!cpi->use_svc && cpi->multi_arf_allowed) {
4299 if (cm->frame_type == KEY_FRAME) {
4300 init_buffer_indices(cpi);
4301 } else if (oxcf->pass == 2) {
4302 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4303 cpi->alt_fb_idx = gf_group->arf_ref_idx[gf_group->index];
4304 }
4305 }
4306
4307 // Start with a 0 size frame.
4308 *size = 0;
4309
4310 cpi->frame_flags = *frame_flags;
4311
4312 if ((oxcf->pass == 2) &&
4313 (!cpi->use_svc ||
4314 (is_two_pass_svc(cpi) &&
4315 cpi->svc.encode_empty_frame_state != ENCODING))) {
4316 vp9_rc_get_second_pass_params(cpi);
4317 } else if (oxcf->pass == 1) {
4318 set_frame_size(cpi);
4319 }
4320
4321 if (cpi->oxcf.pass != 0 ||
4322 cpi->use_svc ||
4323 frame_is_intra_only(cm) == 1) {
4324 for (i = 0; i < MAX_REF_FRAMES; ++i)
4325 cpi->scaled_ref_idx[i] = INVALID_IDX;
4326 }
4327
4328 if (oxcf->pass == 1 &&
4329 (!cpi->use_svc || is_two_pass_svc(cpi))) {
4330 const int lossless = is_lossless_requested(oxcf);
4331 #if CONFIG_VP9_HIGHBITDEPTH
4332 if (cpi->oxcf.use_highbitdepth)
4333 cpi->td.mb.fwd_txm4x4 = lossless ?
4334 vp9_highbd_fwht4x4 : vpx_highbd_fdct4x4;
4335 else
4336 cpi->td.mb.fwd_txm4x4 = lossless ? vp9_fwht4x4 : vpx_fdct4x4;
4337 cpi->td.mb.highbd_itxm_add = lossless ? vp9_highbd_iwht4x4_add :
4338 vp9_highbd_idct4x4_add;
4339 #else
4340 cpi->td.mb.fwd_txm4x4 = lossless ? vp9_fwht4x4 : vpx_fdct4x4;
4341 #endif // CONFIG_VP9_HIGHBITDEPTH
4342 cpi->td.mb.itxm_add = lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
4343 vp9_first_pass(cpi, source);
4344 } else if (oxcf->pass == 2 &&
4345 (!cpi->use_svc || is_two_pass_svc(cpi))) {
4346 Pass2Encode(cpi, size, dest, frame_flags);
4347 } else if (cpi->use_svc) {
4348 SvcEncode(cpi, size, dest, frame_flags);
4349 } else {
4350 // One pass encode
4351 Pass0Encode(cpi, size, dest, frame_flags);
4352 }
4353
4354 if (cm->refresh_frame_context)
4355 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4356
4357 // No frame encoded, or frame was dropped, release scaled references.
4358 if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
4359 release_scaled_references(cpi);
4360 }
4361
4362 if (*size > 0) {
4363 cpi->droppable = !frame_is_reference(cpi);
4364 }
4365
4366 // Save layer specific state.
4367 if (is_one_pass_cbr_svc(cpi) ||
4368 ((cpi->svc.number_temporal_layers > 1 ||
4369 cpi->svc.number_spatial_layers > 1) &&
4370 oxcf->pass == 2)) {
4371 vp9_save_layer_context(cpi);
4372 }
4373
4374 vpx_usec_timer_mark(&cmptimer);
4375 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
4376
4377 if (cpi->b_calculate_psnr && oxcf->pass != 1 && cm->show_frame)
4378 generate_psnr_packet(cpi);
4379
4380 #if CONFIG_INTERNAL_STATS
4381
4382 if (oxcf->pass != 1) {
4383 double samples = 0.0;
4384 cpi->bytes += (int)(*size);
4385
4386 if (cm->show_frame) {
4387 cpi->count++;
4388
4389 if (cpi->b_calculate_psnr) {
4390 YV12_BUFFER_CONFIG *orig = cpi->Source;
4391 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
4392 YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
4393 PSNR_STATS psnr;
4394 #if CONFIG_VP9_HIGHBITDEPTH
4395 calc_highbd_psnr(orig, recon, &psnr, cpi->td.mb.e_mbd.bd,
4396 cpi->oxcf.input_bit_depth);
4397 #else
4398 calc_psnr(orig, recon, &psnr);
4399 #endif // CONFIG_VP9_HIGHBITDEPTH
4400
4401 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3],
4402 psnr.psnr[0], &cpi->psnr);
4403 cpi->total_sq_error += psnr.sse[0];
4404 cpi->total_samples += psnr.samples[0];
4405 samples = psnr.samples[0];
4406
4407 {
4408 PSNR_STATS psnr2;
4409 double frame_ssim2 = 0, weight = 0;
4410 #if CONFIG_VP9_POSTPROC
4411 if (vpx_alloc_frame_buffer(&cm->post_proc_buffer,
4412 recon->y_crop_width, recon->y_crop_height,
4413 cm->subsampling_x, cm->subsampling_y,
4414 #if CONFIG_VP9_HIGHBITDEPTH
4415 cm->use_highbitdepth,
4416 #endif
4417 VP9_ENC_BORDER_IN_PIXELS,
4418 cm->byte_alignment) < 0) {
4419 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
4420 "Failed to allocate post processing buffer");
4421 }
4422
4423 vp9_deblock(cm->frame_to_show, &cm->post_proc_buffer,
4424 cm->lf.filter_level * 10 / 6);
4425 #endif
4426 vpx_clear_system_state();
4427
4428 #if CONFIG_VP9_HIGHBITDEPTH
4429 calc_highbd_psnr(orig, pp, &psnr2, cpi->td.mb.e_mbd.bd,
4430 cpi->oxcf.input_bit_depth);
4431 #else
4432 calc_psnr(orig, pp, &psnr2);
4433 #endif // CONFIG_VP9_HIGHBITDEPTH
4434
4435 cpi->totalp_sq_error += psnr2.sse[0];
4436 cpi->totalp_samples += psnr2.samples[0];
4437 adjust_image_stat(psnr2.psnr[1], psnr2.psnr[2], psnr2.psnr[3],
4438 psnr2.psnr[0], &cpi->psnrp);
4439
4440 #if CONFIG_VP9_HIGHBITDEPTH
4441 if (cm->use_highbitdepth) {
4442 frame_ssim2 = vpx_highbd_calc_ssim(orig, recon, &weight,
4443 (int)cm->bit_depth);
4444 } else {
4445 frame_ssim2 = vpx_calc_ssim(orig, recon, &weight);
4446 }
4447 #else
4448 frame_ssim2 = vpx_calc_ssim(orig, recon, &weight);
4449 #endif // CONFIG_VP9_HIGHBITDEPTH
4450
4451 cpi->worst_ssim= MIN(cpi->worst_ssim, frame_ssim2);
4452 cpi->summed_quality += frame_ssim2 * weight;
4453 cpi->summed_weights += weight;
4454
4455 #if CONFIG_VP9_HIGHBITDEPTH
4456 if (cm->use_highbitdepth) {
4457 frame_ssim2 = vpx_highbd_calc_ssim(
4458 orig, &cm->post_proc_buffer, &weight, (int)cm->bit_depth);
4459 } else {
4460 frame_ssim2 = vpx_calc_ssim(orig, &cm->post_proc_buffer, &weight);
4461 }
4462 #else
4463 frame_ssim2 = vpx_calc_ssim(orig, &cm->post_proc_buffer, &weight);
4464 #endif // CONFIG_VP9_HIGHBITDEPTH
4465
4466 cpi->summedp_quality += frame_ssim2 * weight;
4467 cpi->summedp_weights += weight;
4468 #if 0
4469 {
4470 FILE *f = fopen("q_used.stt", "a");
4471 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
4472 cpi->common.current_video_frame, y2, u2, v2,
4473 frame_psnr2, frame_ssim2);
4474 fclose(f);
4475 }
4476 #endif
4477 }
4478 }
4479 if (cpi->b_calculate_blockiness) {
4480 #if CONFIG_VP9_HIGHBITDEPTH
4481 if (!cm->use_highbitdepth)
4482 #endif
4483 {
4484 double frame_blockiness = vp9_get_blockiness(
4485 cpi->Source->y_buffer, cpi->Source->y_stride,
4486 cm->frame_to_show->y_buffer, cm->frame_to_show->y_stride,
4487 cpi->Source->y_width, cpi->Source->y_height);
4488 cpi->worst_blockiness = MAX(cpi->worst_blockiness, frame_blockiness);
4489 cpi->total_blockiness += frame_blockiness;
4490 }
4491 }
4492
4493 if (cpi->b_calculate_consistency) {
4494 #if CONFIG_VP9_HIGHBITDEPTH
4495 if (!cm->use_highbitdepth)
4496 #endif
4497 {
4498 double this_inconsistency = vpx_get_ssim_metrics(
4499 cpi->Source->y_buffer, cpi->Source->y_stride,
4500 cm->frame_to_show->y_buffer, cm->frame_to_show->y_stride,
4501 cpi->Source->y_width, cpi->Source->y_height, cpi->ssim_vars,
4502 &cpi->metrics, 1);
4503
4504 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
4505 double consistency = vpx_sse_to_psnr(samples, peak,
4506 (double)cpi->total_inconsistency);
4507 if (consistency > 0.0)
4508 cpi->worst_consistency = MIN(cpi->worst_consistency,
4509 consistency);
4510 cpi->total_inconsistency += this_inconsistency;
4511 }
4512 }
4513
4514 if (cpi->b_calculate_ssimg) {
4515 double y, u, v, frame_all;
4516 #if CONFIG_VP9_HIGHBITDEPTH
4517 if (cm->use_highbitdepth) {
4518 frame_all = vpx_highbd_calc_ssimg(cpi->Source, cm->frame_to_show, &y,
4519 &u, &v, (int)cm->bit_depth);
4520 } else {
4521 frame_all = vpx_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u,
4522 &v);
4523 }
4524 #else
4525 frame_all = vpx_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u, &v);
4526 #endif // CONFIG_VP9_HIGHBITDEPTH
4527 adjust_image_stat(y, u, v, frame_all, &cpi->ssimg);
4528 }
4529 #if CONFIG_VP9_HIGHBITDEPTH
4530 if (!cm->use_highbitdepth)
4531 #endif
4532 {
4533 double y, u, v, frame_all;
4534 frame_all = vpx_calc_fastssim(cpi->Source, cm->frame_to_show, &y, &u,
4535 &v);
4536 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
4537 /* TODO(JBB): add 10/12 bit support */
4538 }
4539 #if CONFIG_VP9_HIGHBITDEPTH
4540 if (!cm->use_highbitdepth)
4541 #endif
4542 {
4543 double y, u, v, frame_all;
4544 frame_all = vpx_psnrhvs(cpi->Source, cm->frame_to_show, &y, &u, &v);
4545 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
4546 }
4547 }
4548 }
4549
4550 #endif
4551
4552 if (is_two_pass_svc(cpi)) {
4553 if (cpi->svc.encode_empty_frame_state == ENCODING) {
4554 cpi->svc.encode_empty_frame_state = ENCODED;
4555 cpi->svc.encode_intra_empty_frame = 0;
4556 }
4557
4558 if (cm->show_frame) {
4559 ++cpi->svc.spatial_layer_to_encode;
4560 if (cpi->svc.spatial_layer_to_encode >= cpi->svc.number_spatial_layers)
4561 cpi->svc.spatial_layer_to_encode = 0;
4562
4563 // May need the empty frame after an visible frame.
4564 cpi->svc.encode_empty_frame_state = NEED_TO_ENCODE;
4565 }
4566 } else if (is_one_pass_cbr_svc(cpi)) {
4567 if (cm->show_frame) {
4568 ++cpi->svc.spatial_layer_to_encode;
4569 if (cpi->svc.spatial_layer_to_encode >= cpi->svc.number_spatial_layers)
4570 cpi->svc.spatial_layer_to_encode = 0;
4571 }
4572 }
4573 vpx_clear_system_state();
4574 return 0;
4575 }
4576
4577 int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
4578 vp9_ppflags_t *flags) {
4579 VP9_COMMON *cm = &cpi->common;
4580 #if !CONFIG_VP9_POSTPROC
4581 (void)flags;
4582 #endif
4583
4584 if (!cm->show_frame) {
4585 return -1;
4586 } else {
4587 int ret;
4588 #if CONFIG_VP9_POSTPROC
4589 ret = vp9_post_proc_frame(cm, dest, flags);
4590 #else
4591 if (cm->frame_to_show) {
4592 *dest = *cm->frame_to_show;
4593 dest->y_width = cm->width;
4594 dest->y_height = cm->height;
4595 dest->uv_width = cm->width >> cm->subsampling_x;
4596 dest->uv_height = cm->height >> cm->subsampling_y;
4597 ret = 0;
4598 } else {
4599 ret = -1;
4600 }
4601 #endif // !CONFIG_VP9_POSTPROC
4602 vpx_clear_system_state();
4603 return ret;
4604 }
4605 }
4606
4607 int vp9_set_internal_size(VP9_COMP *cpi,
4608 VPX_SCALING horiz_mode, VPX_SCALING vert_mode) {
4609 VP9_COMMON *cm = &cpi->common;
4610 int hr = 0, hs = 0, vr = 0, vs = 0;
4611
4612 if (horiz_mode > ONETWO || vert_mode > ONETWO)
4613 return -1;
4614
4615 Scale2Ratio(horiz_mode, &hr, &hs);
4616 Scale2Ratio(vert_mode, &vr, &vs);
4617
4618 // always go to the next whole number
4619 cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
4620 cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
4621 assert(cm->width <= cpi->initial_width);
4622 assert(cm->height <= cpi->initial_height);
4623
4624 update_frame_size(cpi);
4625
4626 return 0;
4627 }
4628
4629 int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
4630 unsigned int height) {
4631 VP9_COMMON *cm = &cpi->common;
4632 #if CONFIG_VP9_HIGHBITDEPTH
4633 check_initial_width(cpi, cm->use_highbitdepth, 1, 1);
4634 #else
4635 check_initial_width(cpi, 1, 1);
4636 #endif // CONFIG_VP9_HIGHBITDEPTH
4637
4638 #if CONFIG_VP9_TEMPORAL_DENOISING
4639 setup_denoiser_buffer(cpi);
4640 #endif
4641
4642 if (width) {
4643 cm->width = width;
4644 if (cm->width > cpi->initial_width) {
4645 cm->width = cpi->initial_width;
4646 printf("Warning: Desired width too large, changed to %d\n", cm->width);
4647 }
4648 }
4649
4650 if (height) {
4651 cm->height = height;
4652 if (cm->height > cpi->initial_height) {
4653 cm->height = cpi->initial_height;
4654 printf("Warning: Desired height too large, changed to %d\n", cm->height);
4655 }
4656 }
4657 assert(cm->width <= cpi->initial_width);
4658 assert(cm->height <= cpi->initial_height);
4659
4660 update_frame_size(cpi);
4661
4662 return 0;
4663 }
4664
4665 void vp9_set_svc(VP9_COMP *cpi, int use_svc) {
4666 cpi->use_svc = use_svc;
4667 return;
4668 }
4669
4670 int64_t vp9_get_y_sse(const YV12_BUFFER_CONFIG *a,
4671 const YV12_BUFFER_CONFIG *b) {
4672 assert(a->y_crop_width == b->y_crop_width);
4673 assert(a->y_crop_height == b->y_crop_height);
4674
4675 return get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
4676 a->y_crop_width, a->y_crop_height);
4677 }
4678
4679 #if CONFIG_VP9_HIGHBITDEPTH
4680 int64_t vp9_highbd_get_y_sse(const YV12_BUFFER_CONFIG *a,
4681 const YV12_BUFFER_CONFIG *b) {
4682 assert(a->y_crop_width == b->y_crop_width);
4683 assert(a->y_crop_height == b->y_crop_height);
4684 assert((a->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
4685 assert((b->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
4686
4687 return highbd_get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
4688 a->y_crop_width, a->y_crop_height);
4689 }
4690 #endif // CONFIG_VP9_HIGHBITDEPTH
4691
4692 int vp9_get_quantizer(VP9_COMP *cpi) {
4693 return cpi->common.base_qindex;
4694 }
4695
4696 void vp9_apply_encoding_flags(VP9_COMP *cpi, vpx_enc_frame_flags_t flags) {
4697 if (flags & (VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_GF |
4698 VP8_EFLAG_NO_REF_ARF)) {
4699 int ref = 7;
4700
4701 if (flags & VP8_EFLAG_NO_REF_LAST)
4702 ref ^= VP9_LAST_FLAG;
4703
4704 if (flags & VP8_EFLAG_NO_REF_GF)
4705 ref ^= VP9_GOLD_FLAG;
4706
4707 if (flags & VP8_EFLAG_NO_REF_ARF)
4708 ref ^= VP9_ALT_FLAG;
4709
4710 vp9_use_as_reference(cpi, ref);
4711 }
4712
4713 if (flags & (VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF |
4714 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_FORCE_GF |
4715 VP8_EFLAG_FORCE_ARF)) {
4716 int upd = 7;
4717
4718 if (flags & VP8_EFLAG_NO_UPD_LAST)
4719 upd ^= VP9_LAST_FLAG;
4720
4721 if (flags & VP8_EFLAG_NO_UPD_GF)
4722 upd ^= VP9_GOLD_FLAG;
4723
4724 if (flags & VP8_EFLAG_NO_UPD_ARF)
4725 upd ^= VP9_ALT_FLAG;
4726
4727 vp9_update_reference(cpi, upd);
4728 }
4729
4730 if (flags & VP8_EFLAG_NO_UPD_ENTROPY) {
4731 vp9_update_entropy(cpi, 0);
4732 }
4733 }
4734