blob: acb5498faf7a4f26ecd6e38443855acc12f283b3 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
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 <limits.h>
12#include <math.h>
13#include <stdio.h>
14
Yaowu Xuf883b422016-08-30 14:01:10 -070015#include "./aom_config.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070016
17#include "av1/common/alloccommon.h"
18#if CONFIG_CLPF
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +020019#include "aom/aom_image.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070020#include "av1/common/clpf.h"
Steinar Midtskogend06588a2016-05-06 13:48:20 +020021#include "av1/encoder/clpf_rdo.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070022#endif
23#if CONFIG_DERING
24#include "av1/common/dering.h"
25#endif // CONFIG_DERING
26#include "av1/common/filter.h"
27#include "av1/common/idct.h"
28#include "av1/common/reconinter.h"
29#include "av1/common/reconintra.h"
30#include "av1/common/tile_common.h"
31
32#include "av1/encoder/aq_complexity.h"
33#include "av1/encoder/aq_cyclicrefresh.h"
34#include "av1/encoder/aq_variance.h"
35#include "av1/encoder/bitstream.h"
36#if CONFIG_ANS
Alex Converse1ac1ae72016-09-17 15:11:16 -070037#include "aom_dsp/buf_ans.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070038#endif
39#include "av1/encoder/context_tree.h"
40#include "av1/encoder/encodeframe.h"
41#include "av1/encoder/encodemv.h"
42#include "av1/encoder/encoder.h"
43#include "av1/encoder/ethread.h"
44#include "av1/encoder/firstpass.h"
45#include "av1/encoder/mbgraph.h"
46#include "av1/encoder/picklpf.h"
47#if CONFIG_LOOP_RESTORATION
48#include "av1/encoder/pickrst.h"
49#endif // CONFIG_LOOP_RESTORATION
50#include "av1/encoder/ratectrl.h"
51#include "av1/encoder/rd.h"
52#include "av1/encoder/resize.h"
53#include "av1/encoder/segmentation.h"
54#include "av1/encoder/speed_features.h"
55#include "av1/encoder/temporal_filter.h"
56
Yaowu Xuf883b422016-08-30 14:01:10 -070057#include "./av1_rtcd.h"
58#include "./aom_dsp_rtcd.h"
59#include "./aom_scale_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070060#include "aom_dsp/psnr.h"
61#if CONFIG_INTERNAL_STATS
62#include "aom_dsp/ssim.h"
63#endif
Yaowu Xuf883b422016-08-30 14:01:10 -070064#include "aom_dsp/aom_dsp_common.h"
65#include "aom_dsp/aom_filter.h"
Jingning Han1aab8182016-06-03 11:09:06 -070066#include "aom_ports/aom_timer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070067#include "aom_ports/mem.h"
68#include "aom_ports/system_state.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070069#include "aom_scale/aom_scale.h"
Angie Chiang6062a8b2016-09-21 16:01:04 -070070#if CONFIG_BITSTREAM_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070071#include "aom_util/debug_util.h"
Angie Chiang6062a8b2016-09-21 16:01:04 -070072#endif // CONFIG_BITSTREAM_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070073
74#define AM_SEGMENT_ID_INACTIVE 7
75#define AM_SEGMENT_ID_ACTIVE 0
76
77#define SHARP_FILTER_QTHRESH 0 /* Q threshold for 8-tap sharp filter */
78
79#define ALTREF_HIGH_PRECISION_MV 1 // Whether to use high precision mv
80 // for altref computation.
81#define HIGH_PRECISION_MV_QTHRESH 200 // Q threshold for high precision
82 // mv. Choose a very high value for
83 // now so that HIGH_PRECISION is always
84 // chosen.
85// #define OUTPUT_YUV_REC
86#ifdef OUTPUT_YUV_DENOISED
87FILE *yuv_denoised_file = NULL;
88#endif
89#ifdef OUTPUT_YUV_SKINMAP
90FILE *yuv_skinmap_file = NULL;
91#endif
92#ifdef OUTPUT_YUV_REC
93FILE *yuv_rec_file;
94#define FILE_NAME_LEN 100
95#endif
96
97#if 0
98FILE *framepsnr;
99FILE *kf_list;
100FILE *keyfile;
101#endif
102
Yaowu Xuf883b422016-08-30 14:01:10 -0700103static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700104 switch (mode) {
105 case NORMAL:
106 *hr = 1;
107 *hs = 1;
108 break;
109 case FOURFIVE:
110 *hr = 4;
111 *hs = 5;
112 break;
113 case THREEFIVE:
114 *hr = 3;
115 *hs = 5;
116 break;
117 case ONETWO:
118 *hr = 1;
119 *hs = 2;
120 break;
121 default:
122 *hr = 1;
123 *hs = 1;
124 assert(0);
125 break;
126 }
127}
128
129// Mark all inactive blocks as active. Other segmentation features may be set
130// so memset cannot be used, instead only inactive blocks should be reset.
Yaowu Xuf883b422016-08-30 14:01:10 -0700131static void suppress_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700132 unsigned char *const seg_map = cpi->segmentation_map;
133 int i;
134 if (cpi->active_map.enabled || cpi->active_map.update)
135 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
136 if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
137 seg_map[i] = AM_SEGMENT_ID_ACTIVE;
138}
139
Yaowu Xuf883b422016-08-30 14:01:10 -0700140static void apply_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700141 struct segmentation *const seg = &cpi->common.seg;
142 unsigned char *const seg_map = cpi->segmentation_map;
143 const unsigned char *const active_map = cpi->active_map.map;
144 int i;
145
146 assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
147
148 if (frame_is_intra_only(&cpi->common)) {
149 cpi->active_map.enabled = 0;
150 cpi->active_map.update = 1;
151 }
152
153 if (cpi->active_map.update) {
154 if (cpi->active_map.enabled) {
155 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
156 if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
Yaowu Xuf883b422016-08-30 14:01:10 -0700157 av1_enable_segmentation(seg);
158 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
159 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700160 // Setting the data to -MAX_LOOP_FILTER will result in the computed loop
161 // filter level being zero regardless of the value of seg->abs_delta.
Yaowu Xuf883b422016-08-30 14:01:10 -0700162 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF,
163 -MAX_LOOP_FILTER);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700164 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700165 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
166 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700167 if (seg->enabled) {
168 seg->update_data = 1;
169 seg->update_map = 1;
170 }
171 }
172 cpi->active_map.update = 0;
173 }
174}
175
Yaowu Xuf883b422016-08-30 14:01:10 -0700176int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
177 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700178 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
179 unsigned char *const active_map_8x8 = cpi->active_map.map;
180 const int mi_rows = cpi->common.mi_rows;
181 const int mi_cols = cpi->common.mi_cols;
182 cpi->active_map.update = 1;
183 if (new_map_16x16) {
184 int r, c;
185 for (r = 0; r < mi_rows; ++r) {
186 for (c = 0; c < mi_cols; ++c) {
187 active_map_8x8[r * mi_cols + c] =
188 new_map_16x16[(r >> 1) * cols + (c >> 1)]
189 ? AM_SEGMENT_ID_ACTIVE
190 : AM_SEGMENT_ID_INACTIVE;
191 }
192 }
193 cpi->active_map.enabled = 1;
194 } else {
195 cpi->active_map.enabled = 0;
196 }
197 return 0;
198 } else {
199 return -1;
200 }
201}
202
Yaowu Xuf883b422016-08-30 14:01:10 -0700203int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
204 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700205 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
206 new_map_16x16) {
207 unsigned char *const seg_map_8x8 = cpi->segmentation_map;
208 const int mi_rows = cpi->common.mi_rows;
209 const int mi_cols = cpi->common.mi_cols;
210 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
211 if (cpi->active_map.enabled) {
212 int r, c;
213 for (r = 0; r < mi_rows; ++r) {
214 for (c = 0; c < mi_cols; ++c) {
215 // Cyclic refresh segments are considered active despite not having
216 // AM_SEGMENT_ID_ACTIVE
217 new_map_16x16[(r >> 1) * cols + (c >> 1)] |=
218 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
219 }
220 }
221 }
222 return 0;
223 } else {
224 return -1;
225 }
226}
227
Yaowu Xuf883b422016-08-30 14:01:10 -0700228void av1_set_high_precision_mv(AV1_COMP *cpi, int allow_high_precision_mv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700229 MACROBLOCK *const mb = &cpi->td.mb;
230 cpi->common.allow_high_precision_mv = allow_high_precision_mv;
231
232#if CONFIG_REF_MV
233 if (cpi->common.allow_high_precision_mv) {
234 int i;
235 for (i = 0; i < NMV_CONTEXTS; ++i) {
236 mb->mv_cost_stack[i] = mb->nmvcost_hp[i];
237 mb->mvsadcost = mb->nmvsadcost_hp;
238 }
239 } else {
240 int i;
241 for (i = 0; i < NMV_CONTEXTS; ++i) {
242 mb->mv_cost_stack[i] = mb->nmvcost[i];
243 mb->mvsadcost = mb->nmvsadcost;
244 }
245 }
246#else
247 if (cpi->common.allow_high_precision_mv) {
248 mb->mvcost = mb->nmvcost_hp;
249 mb->mvsadcost = mb->nmvcost_hp;
250 } else {
251 mb->mvcost = mb->nmvcost;
252 mb->mvsadcost = mb->nmvcost;
253 }
254#endif
255}
256
Yaowu Xuf883b422016-08-30 14:01:10 -0700257static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700258#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700259 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700260 return BLOCK_64X64;
261
Yaowu Xuf883b422016-08-30 14:01:10 -0700262 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700263 return BLOCK_128X128;
264
Yaowu Xuf883b422016-08-30 14:01:10 -0700265 assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700266
267 assert(IMPLIES(cpi->common.tile_cols > 1,
268 cpi->common.tile_width % MAX_MIB_SIZE == 0));
269 assert(IMPLIES(cpi->common.tile_rows > 1,
270 cpi->common.tile_height % MAX_MIB_SIZE == 0));
271
272 // TODO(any): Possibly could improve this with a heuristic.
273 return BLOCK_128X128;
274#else
275 (void)cpi;
276 return BLOCK_64X64;
277#endif // CONFIG_EXT_PARTITION
278}
279
Yaowu Xuf883b422016-08-30 14:01:10 -0700280static void setup_frame(AV1_COMP *cpi) {
281 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700282 // Set up entropy context depending on frame type. The decoder mandates
283 // the use of the default context, index 0, for keyframes and inter
284 // frames where the error_resilient_mode or intra_only flag is set. For
285 // other inter-frames the encoder currently uses only two contexts;
286 // context 1 for ALTREF frames and context 0 for the others.
287 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700288 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700289 } else {
290#if CONFIG_EXT_REFS
291 const GF_GROUP *gf_group = &cpi->twopass.gf_group;
292 if (gf_group->rf_level[gf_group->index] == GF_ARF_LOW)
293 cm->frame_context_idx = EXT_ARF_FRAME;
294 else if (cpi->refresh_alt_ref_frame)
295 cm->frame_context_idx = ARF_FRAME;
296#else
297 if (cpi->refresh_alt_ref_frame) cm->frame_context_idx = ARF_FRAME;
298#endif
299 else if (cpi->rc.is_src_frame_alt_ref)
300 cm->frame_context_idx = OVERLAY_FRAME;
301 else if (cpi->refresh_golden_frame)
302 cm->frame_context_idx = GLD_FRAME;
303#if CONFIG_EXT_REFS
304 else if (cpi->refresh_bwd_ref_frame)
305 cm->frame_context_idx = BRF_FRAME;
306#endif
307 else
308 cm->frame_context_idx = REGULAR_FRAME;
309 }
310
311 if (cm->frame_type == KEY_FRAME) {
312 cpi->refresh_golden_frame = 1;
313 cpi->refresh_alt_ref_frame = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700314 av1_zero(cpi->interp_filter_selected);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700315 } else {
316 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
Yaowu Xuf883b422016-08-30 14:01:10 -0700317 av1_zero(cpi->interp_filter_selected[0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700318 }
319
320 cpi->vaq_refresh = 0;
321
322 set_sb_size(cm, select_sb_size(cpi));
323}
324
Yaowu Xuf883b422016-08-30 14:01:10 -0700325static void av1_enc_setup_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700326 int i;
327 cm->mi = cm->mip + cm->mi_stride + 1;
328 memset(cm->mip, 0, cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mip));
329 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
330 // Clear top border row
331 memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
332 // Clear left border column
333 for (i = 1; i < cm->mi_rows + 1; ++i)
334 memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
335
336 cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
337 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
338
339 memset(cm->mi_grid_base, 0,
340 cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mi_grid_base));
341}
342
Yaowu Xuf883b422016-08-30 14:01:10 -0700343static int av1_enc_alloc_mi(AV1_COMMON *cm, int mi_size) {
344 cm->mip = aom_calloc(mi_size, sizeof(*cm->mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700345 if (!cm->mip) return 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700346 cm->prev_mip = aom_calloc(mi_size, sizeof(*cm->prev_mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700347 if (!cm->prev_mip) return 1;
348 cm->mi_alloc_size = mi_size;
349
Yaowu Xuf883b422016-08-30 14:01:10 -0700350 cm->mi_grid_base = (MODE_INFO **)aom_calloc(mi_size, sizeof(MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700351 if (!cm->mi_grid_base) return 1;
352 cm->prev_mi_grid_base =
Yaowu Xuf883b422016-08-30 14:01:10 -0700353 (MODE_INFO **)aom_calloc(mi_size, sizeof(MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700354 if (!cm->prev_mi_grid_base) return 1;
355
356 return 0;
357}
358
Yaowu Xuf883b422016-08-30 14:01:10 -0700359static void av1_enc_free_mi(AV1_COMMON *cm) {
360 aom_free(cm->mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700361 cm->mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700362 aom_free(cm->prev_mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700363 cm->prev_mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700364 aom_free(cm->mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700365 cm->mi_grid_base = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700366 aom_free(cm->prev_mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700367 cm->prev_mi_grid_base = NULL;
368}
369
Yaowu Xuf883b422016-08-30 14:01:10 -0700370static void av1_swap_mi_and_prev_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700371 // Current mip will be the prev_mip for the next frame.
372 MODE_INFO **temp_base = cm->prev_mi_grid_base;
373 MODE_INFO *temp = cm->prev_mip;
374 cm->prev_mip = cm->mip;
375 cm->mip = temp;
376
377 // Update the upper left visible macroblock ptrs.
378 cm->mi = cm->mip + cm->mi_stride + 1;
379 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
380
381 cm->prev_mi_grid_base = cm->mi_grid_base;
382 cm->mi_grid_base = temp_base;
383 cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
384 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
385}
386
Yaowu Xuf883b422016-08-30 14:01:10 -0700387void av1_initialize_enc(void) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700388 static volatile int init_done = 0;
389
390 if (!init_done) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700391 av1_rtcd();
392 aom_dsp_rtcd();
393 aom_scale_rtcd();
394 av1_init_intra_predictors();
395 av1_init_me_luts();
396 av1_rc_init_minq_luts();
397 av1_entropy_mv_init();
398 av1_encode_token_init();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700399#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700400 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700401#endif
402 init_done = 1;
403 }
404}
405
Yaowu Xuf883b422016-08-30 14:01:10 -0700406static void dealloc_compressor_data(AV1_COMP *cpi) {
407 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700408 int i;
409
Yaowu Xuf883b422016-08-30 14:01:10 -0700410 aom_free(cpi->mbmi_ext_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700411 cpi->mbmi_ext_base = NULL;
412
Yaowu Xuf883b422016-08-30 14:01:10 -0700413 aom_free(cpi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700414 cpi->tile_data = NULL;
415
416 // Delete sementation map
Yaowu Xuf883b422016-08-30 14:01:10 -0700417 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700418 cpi->segmentation_map = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700419 aom_free(cpi->coding_context.last_frame_seg_map_copy);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700420 cpi->coding_context.last_frame_seg_map_copy = NULL;
421
422#if CONFIG_REF_MV
423 for (i = 0; i < NMV_CONTEXTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700424 aom_free(cpi->nmv_costs[i][0]);
425 aom_free(cpi->nmv_costs[i][1]);
426 aom_free(cpi->nmv_costs_hp[i][0]);
427 aom_free(cpi->nmv_costs_hp[i][1]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700428 cpi->nmv_costs[i][0] = NULL;
429 cpi->nmv_costs[i][1] = NULL;
430 cpi->nmv_costs_hp[i][0] = NULL;
431 cpi->nmv_costs_hp[i][1] = NULL;
432 }
433#endif
434
Yaowu Xuf883b422016-08-30 14:01:10 -0700435 aom_free(cpi->nmvcosts[0]);
436 aom_free(cpi->nmvcosts[1]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700437 cpi->nmvcosts[0] = NULL;
438 cpi->nmvcosts[1] = NULL;
439
Yaowu Xuf883b422016-08-30 14:01:10 -0700440 aom_free(cpi->nmvcosts_hp[0]);
441 aom_free(cpi->nmvcosts_hp[1]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700442 cpi->nmvcosts_hp[0] = NULL;
443 cpi->nmvcosts_hp[1] = NULL;
444
Yaowu Xuf883b422016-08-30 14:01:10 -0700445 aom_free(cpi->nmvsadcosts[0]);
446 aom_free(cpi->nmvsadcosts[1]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700447 cpi->nmvsadcosts[0] = NULL;
448 cpi->nmvsadcosts[1] = NULL;
449
Yaowu Xuf883b422016-08-30 14:01:10 -0700450 aom_free(cpi->nmvsadcosts_hp[0]);
451 aom_free(cpi->nmvsadcosts_hp[1]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700452 cpi->nmvsadcosts_hp[0] = NULL;
453 cpi->nmvsadcosts_hp[1] = NULL;
454
Yaowu Xuf883b422016-08-30 14:01:10 -0700455 av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700456 cpi->cyclic_refresh = NULL;
457
Yaowu Xuf883b422016-08-30 14:01:10 -0700458 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700459 cpi->active_map.map = NULL;
460
461 // Free up-sampled reference buffers.
462 for (i = 0; i < (REF_FRAMES + 1); i++)
Yaowu Xuf883b422016-08-30 14:01:10 -0700463 aom_free_frame_buffer(&cpi->upsampled_ref_bufs[i].buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700464
Yaowu Xuf883b422016-08-30 14:01:10 -0700465 av1_free_ref_frame_buffers(cm->buffer_pool);
466 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700467
Yaowu Xuf883b422016-08-30 14:01:10 -0700468 aom_free_frame_buffer(&cpi->last_frame_uf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700469#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700470 aom_free_frame_buffer(&cpi->last_frame_db);
471 av1_free_restoration_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700472#endif // CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700473 aom_free_frame_buffer(&cpi->scaled_source);
474 aom_free_frame_buffer(&cpi->scaled_last_source);
475 aom_free_frame_buffer(&cpi->alt_ref_buffer);
476 av1_lookahead_destroy(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700477
Yaowu Xuf883b422016-08-30 14:01:10 -0700478 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700479 cpi->tile_tok[0][0] = 0;
480
Yaowu Xuf883b422016-08-30 14:01:10 -0700481 av1_free_pc_tree(&cpi->td);
482 av1_free_var_tree(&cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700483
484 if (cpi->common.allow_screen_content_tools)
Yaowu Xuf883b422016-08-30 14:01:10 -0700485 aom_free(cpi->td.mb.palette_buffer);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700486
487 if (cpi->source_diff_var != NULL) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700488 aom_free(cpi->source_diff_var);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700489 cpi->source_diff_var = NULL;
490 }
491#if CONFIG_ANS
Alex Converse1ac1ae72016-09-17 15:11:16 -0700492 aom_buf_ans_free(&cpi->buf_ans);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700493#endif // CONFIG_ANS
494}
495
Yaowu Xuf883b422016-08-30 14:01:10 -0700496static void save_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700497 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700498 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700499#if CONFIG_REF_MV
500 int i;
501#endif
502
503// Stores a snapshot of key state variables which can subsequently be
Yaowu Xuf883b422016-08-30 14:01:10 -0700504// restored with a call to av1_restore_coding_context. These functions are
505// intended for use in a re-code loop in av1_compress_frame where the
Yaowu Xuc27fc142016-08-22 16:08:15 -0700506// quantizer value is adjusted between loop iterations.
507#if CONFIG_REF_MV
508 for (i = 0; i < NMV_CONTEXTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700509 av1_copy(cc->nmv_vec_cost[i], cpi->td.mb.nmv_vec_cost[i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700510 memcpy(cc->nmv_costs[i][0], cpi->nmv_costs[i][0],
511 MV_VALS * sizeof(*cpi->nmv_costs[i][0]));
512 memcpy(cc->nmv_costs[i][1], cpi->nmv_costs[i][1],
513 MV_VALS * sizeof(*cpi->nmv_costs[i][1]));
514 memcpy(cc->nmv_costs_hp[i][0], cpi->nmv_costs_hp[i][0],
515 MV_VALS * sizeof(*cpi->nmv_costs_hp[i][0]));
516 memcpy(cc->nmv_costs_hp[i][1], cpi->nmv_costs_hp[i][1],
517 MV_VALS * sizeof(*cpi->nmv_costs_hp[i][1]));
518 }
519#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700520 av1_copy(cc->nmvjointcost, cpi->td.mb.nmvjointcost);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700521#endif
522
523 memcpy(cc->nmvcosts[0], cpi->nmvcosts[0],
524 MV_VALS * sizeof(*cpi->nmvcosts[0]));
525 memcpy(cc->nmvcosts[1], cpi->nmvcosts[1],
526 MV_VALS * sizeof(*cpi->nmvcosts[1]));
527 memcpy(cc->nmvcosts_hp[0], cpi->nmvcosts_hp[0],
528 MV_VALS * sizeof(*cpi->nmvcosts_hp[0]));
529 memcpy(cc->nmvcosts_hp[1], cpi->nmvcosts_hp[1],
530 MV_VALS * sizeof(*cpi->nmvcosts_hp[1]));
531
532 memcpy(cpi->coding_context.last_frame_seg_map_copy, cm->last_frame_seg_map,
533 (cm->mi_rows * cm->mi_cols));
534
Yaowu Xuf883b422016-08-30 14:01:10 -0700535 av1_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
536 av1_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700537
538 cc->fc = *cm->fc;
539}
540
Yaowu Xuf883b422016-08-30 14:01:10 -0700541static void restore_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700542 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700543 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700544#if CONFIG_REF_MV
545 int i;
546#endif
547
548// Restore key state variables to the snapshot state stored in the
Yaowu Xuf883b422016-08-30 14:01:10 -0700549// previous call to av1_save_coding_context.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700550#if CONFIG_REF_MV
551 for (i = 0; i < NMV_CONTEXTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700552 av1_copy(cpi->td.mb.nmv_vec_cost[i], cc->nmv_vec_cost[i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700553 memcpy(cpi->nmv_costs[i][0], cc->nmv_costs[i][0],
554 MV_VALS * sizeof(*cc->nmv_costs[i][0]));
555 memcpy(cpi->nmv_costs[i][1], cc->nmv_costs[i][1],
556 MV_VALS * sizeof(*cc->nmv_costs[i][1]));
557 memcpy(cpi->nmv_costs_hp[i][0], cc->nmv_costs_hp[i][0],
558 MV_VALS * sizeof(*cc->nmv_costs_hp[i][0]));
559 memcpy(cpi->nmv_costs_hp[i][1], cc->nmv_costs_hp[i][1],
560 MV_VALS * sizeof(*cc->nmv_costs_hp[i][1]));
561 }
562#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700563 av1_copy(cpi->td.mb.nmvjointcost, cc->nmvjointcost);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700564#endif
565
566 memcpy(cpi->nmvcosts[0], cc->nmvcosts[0], MV_VALS * sizeof(*cc->nmvcosts[0]));
567 memcpy(cpi->nmvcosts[1], cc->nmvcosts[1], MV_VALS * sizeof(*cc->nmvcosts[1]));
568 memcpy(cpi->nmvcosts_hp[0], cc->nmvcosts_hp[0],
569 MV_VALS * sizeof(*cc->nmvcosts_hp[0]));
570 memcpy(cpi->nmvcosts_hp[1], cc->nmvcosts_hp[1],
571 MV_VALS * sizeof(*cc->nmvcosts_hp[1]));
572
573 memcpy(cm->last_frame_seg_map, cpi->coding_context.last_frame_seg_map_copy,
574 (cm->mi_rows * cm->mi_cols));
575
Yaowu Xuf883b422016-08-30 14:01:10 -0700576 av1_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
577 av1_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700578
579 *cm->fc = cc->fc;
580}
581
Yaowu Xuf883b422016-08-30 14:01:10 -0700582static void configure_static_seg_features(AV1_COMP *cpi) {
583 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700584 const RATE_CONTROL *const rc = &cpi->rc;
585 struct segmentation *const seg = &cm->seg;
586
587 int high_q = (int)(rc->avg_q > 48.0);
588 int qi_delta;
589
590 // Disable and clear down for KF
591 if (cm->frame_type == KEY_FRAME) {
592 // Clear down the global segmentation map
593 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
594 seg->update_map = 0;
595 seg->update_data = 0;
596 cpi->static_mb_pct = 0;
597
598 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700599 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700600
601 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700602 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700603 } else if (cpi->refresh_alt_ref_frame) {
604 // If this is an alt ref frame
605 // Clear down the global segmentation map
606 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
607 seg->update_map = 0;
608 seg->update_data = 0;
609 cpi->static_mb_pct = 0;
610
611 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700612 av1_disable_segmentation(seg);
613 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700614
615 // Scan frames from current to arf frame.
616 // This function re-enables segmentation if appropriate.
Yaowu Xuf883b422016-08-30 14:01:10 -0700617 av1_update_mbgraph_stats(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700618
619 // If segmentation was enabled set those features needed for the
620 // arf itself.
621 if (seg->enabled) {
622 seg->update_map = 1;
623 seg->update_data = 1;
624
625 qi_delta =
Yaowu Xuf883b422016-08-30 14:01:10 -0700626 av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875, cm->bit_depth);
627 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
628 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700629
Yaowu Xuf883b422016-08-30 14:01:10 -0700630 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
631 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700632
633 // Where relevant assume segment data is delta data
634 seg->abs_delta = SEGMENT_DELTADATA;
635 }
636 } else if (seg->enabled) {
637 // All other frames if segmentation has been enabled
638
639 // First normal frame in a valid gf or alt ref group
640 if (rc->frames_since_golden == 0) {
641 // Set up segment features for normal frames in an arf group
642 if (rc->source_alt_ref_active) {
643 seg->update_map = 0;
644 seg->update_data = 1;
645 seg->abs_delta = SEGMENT_DELTADATA;
646
Yaowu Xuf883b422016-08-30 14:01:10 -0700647 qi_delta =
648 av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125, cm->bit_depth);
649 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
650 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700651
Yaowu Xuf883b422016-08-30 14:01:10 -0700652 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
653 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700654
655 // Segment coding disabled for compred testing
656 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700657 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
658 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
659 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700660 }
661 } else {
662 // Disable segmentation and clear down features if alt ref
663 // is not active for this group
664
Yaowu Xuf883b422016-08-30 14:01:10 -0700665 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700666
667 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
668
669 seg->update_map = 0;
670 seg->update_data = 0;
671
Yaowu Xuf883b422016-08-30 14:01:10 -0700672 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700673 }
674 } else if (rc->is_src_frame_alt_ref) {
675 // Special case where we are coding over the top of a previous
676 // alt ref frame.
677 // Segment coding disabled for compred testing
678
679 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -0700680 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
681 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700682
683 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -0700684 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
685 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
686 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
687 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700688
689 // Skip all MBs if high Q (0,0 mv and skip coeffs)
690 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700691 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
692 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700693 }
694 // Enable data update
695 seg->update_data = 1;
696 } else {
697 // All other frames.
698
699 // No updates.. leave things as they are.
700 seg->update_map = 0;
701 seg->update_data = 0;
702 }
703 }
704}
705
Yaowu Xuf883b422016-08-30 14:01:10 -0700706static void update_reference_segmentation_map(AV1_COMP *cpi) {
707 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700708 MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
709 uint8_t *cache_ptr = cm->last_frame_seg_map;
710 int row, col;
711
712 for (row = 0; row < cm->mi_rows; row++) {
713 MODE_INFO **mi_8x8 = mi_8x8_ptr;
714 uint8_t *cache = cache_ptr;
715 for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
716 cache[0] = mi_8x8[0]->mbmi.segment_id;
717 mi_8x8_ptr += cm->mi_stride;
718 cache_ptr += cm->mi_cols;
719 }
720}
721
Yaowu Xuf883b422016-08-30 14:01:10 -0700722static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
723 AV1_COMMON *cm = &cpi->common;
724 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700725
726 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700727 cpi->lookahead = av1_lookahead_init(oxcf->width, oxcf->height,
728 cm->subsampling_x, cm->subsampling_y,
729#if CONFIG_AOM_HIGHBITDEPTH
730 cm->use_highbitdepth,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700731#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700732 oxcf->lag_in_frames);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700733 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700734 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700735 "Failed to allocate lag buffers");
736
737 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Yaowu Xuf883b422016-08-30 14:01:10 -0700738 if (aom_realloc_frame_buffer(&cpi->alt_ref_buffer, oxcf->width, oxcf->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700739 cm->subsampling_x, cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -0700740#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700741 cm->use_highbitdepth,
742#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700743 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
744 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700745 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700746 "Failed to allocate altref buffer");
747}
748
Yaowu Xuf883b422016-08-30 14:01:10 -0700749static void alloc_util_frame_buffers(AV1_COMP *cpi) {
750 AV1_COMMON *const cm = &cpi->common;
751 if (aom_realloc_frame_buffer(&cpi->last_frame_uf, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700752 cm->subsampling_x, cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -0700753#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700754 cm->use_highbitdepth,
755#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700756 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
757 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700758 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700759 "Failed to allocate last frame buffer");
760
761#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700762 if (aom_realloc_frame_buffer(&cpi->last_frame_db, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700763 cm->subsampling_x, cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -0700764#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700765 cm->use_highbitdepth,
766#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700767 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
768 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700769 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700770 "Failed to allocate last frame deblocked buffer");
771#endif // CONFIG_LOOP_RESTORATION
772
Yaowu Xuf883b422016-08-30 14:01:10 -0700773 if (aom_realloc_frame_buffer(&cpi->scaled_source, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700774 cm->subsampling_x, cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -0700775#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700776 cm->use_highbitdepth,
777#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700778 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
779 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700780 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700781 "Failed to allocate scaled source buffer");
782
Yaowu Xuf883b422016-08-30 14:01:10 -0700783 if (aom_realloc_frame_buffer(&cpi->scaled_last_source, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700784 cm->subsampling_x, cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -0700785#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700786 cm->use_highbitdepth,
787#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700788 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
789 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700790 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700791 "Failed to allocate scaled last source buffer");
792}
793
Yaowu Xuf883b422016-08-30 14:01:10 -0700794static int alloc_context_buffers_ext(AV1_COMP *cpi) {
795 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700796 int mi_size = cm->mi_cols * cm->mi_rows;
797
Yaowu Xuf883b422016-08-30 14:01:10 -0700798 cpi->mbmi_ext_base = aom_calloc(mi_size, sizeof(*cpi->mbmi_ext_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700799 if (!cpi->mbmi_ext_base) return 1;
800
801 return 0;
802}
803
Yaowu Xuf883b422016-08-30 14:01:10 -0700804void av1_alloc_compressor_data(AV1_COMP *cpi) {
805 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700806
Yaowu Xuf883b422016-08-30 14:01:10 -0700807 av1_alloc_context_buffers(cm, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700808
809 alloc_context_buffers_ext(cpi);
810
Yaowu Xuf883b422016-08-30 14:01:10 -0700811 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700812
813 {
814 unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
815 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -0700816 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700817#if CONFIG_ANS
Alex Converse1ac1ae72016-09-17 15:11:16 -0700818 aom_buf_ans_alloc(&cpi->buf_ans, &cm->error, tokens);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700819#endif // CONFIG_ANS
820 }
821
Yaowu Xuf883b422016-08-30 14:01:10 -0700822 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700823}
824
Yaowu Xuf883b422016-08-30 14:01:10 -0700825void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700826 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Yaowu Xuf883b422016-08-30 14:01:10 -0700827 av1_rc_update_framerate(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700828}
829
Yaowu Xuf883b422016-08-30 14:01:10 -0700830static void set_tile_info(AV1_COMP *cpi) {
831 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700832
833#if CONFIG_EXT_TILE
834#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700835 if (cpi->oxcf.superblock_size != AOM_SUPERBLOCK_SIZE_64X64) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700836 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 32);
837 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 32);
838 cm->tile_width <<= MAX_MIB_SIZE_LOG2;
839 cm->tile_height <<= MAX_MIB_SIZE_LOG2;
840 } else {
841 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 64);
842 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 64);
843 cm->tile_width <<= MAX_MIB_SIZE_LOG2 - 1;
844 cm->tile_height <<= MAX_MIB_SIZE_LOG2 - 1;
845 }
846#else
847 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 64);
848 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 64);
849 cm->tile_width <<= MAX_MIB_SIZE_LOG2;
850 cm->tile_height <<= MAX_MIB_SIZE_LOG2;
851#endif // CONFIG_EXT_PARTITION
852
Yaowu Xuf883b422016-08-30 14:01:10 -0700853 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
854 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700855
856 assert(cm->tile_width >> MAX_MIB_SIZE <= 32);
857 assert(cm->tile_height >> MAX_MIB_SIZE <= 32);
858
859 // Get the number of tiles
860 cm->tile_cols = 1;
861 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
862
863 cm->tile_rows = 1;
864 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
865#else
866 int min_log2_tile_cols, max_log2_tile_cols;
Yaowu Xuf883b422016-08-30 14:01:10 -0700867 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700868
869 cm->log2_tile_cols =
870 clamp(cpi->oxcf.tile_columns, min_log2_tile_cols, max_log2_tile_cols);
871 cm->log2_tile_rows = cpi->oxcf.tile_rows;
872
873 cm->tile_cols = 1 << cm->log2_tile_cols;
874 cm->tile_rows = 1 << cm->log2_tile_rows;
875
876 cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
877 cm->tile_width >>= cm->log2_tile_cols;
878 cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
879 cm->tile_height >>= cm->log2_tile_rows;
880
881 // round to integer multiples of max superblock size
882 cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
883 cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
884#endif // CONFIG_EXT_TILE
885}
886
Yaowu Xuf883b422016-08-30 14:01:10 -0700887static void update_frame_size(AV1_COMP *cpi) {
888 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700889 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
890
Yaowu Xuf883b422016-08-30 14:01:10 -0700891 av1_set_mb_mi(cm, cm->width, cm->height);
892 av1_init_context_buffers(cm);
893 av1_init_macroblockd(cm, xd, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700894 memset(cpi->mbmi_ext_base, 0,
895 cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
896
897 set_tile_info(cpi);
898}
899
Yaowu Xuf883b422016-08-30 14:01:10 -0700900static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700901#if CONFIG_EXT_REFS
902 int fb_idx;
903 for (fb_idx = 0; fb_idx < LAST_REF_FRAMES; ++fb_idx)
904 cpi->lst_fb_idxes[fb_idx] = fb_idx;
905 cpi->gld_fb_idx = LAST_REF_FRAMES;
906 cpi->bwd_fb_idx = LAST_REF_FRAMES + 1;
907 cpi->alt_fb_idx = LAST_REF_FRAMES + 2;
908 for (fb_idx = 0; fb_idx < MAX_EXT_ARFS + 1; ++fb_idx)
909 cpi->arf_map[fb_idx] = LAST_REF_FRAMES + 2 + fb_idx;
910#else
911 cpi->lst_fb_idx = 0;
912 cpi->gld_fb_idx = 1;
913 cpi->alt_fb_idx = 2;
914#endif // CONFIG_EXT_REFS
915}
916
Yaowu Xuf883b422016-08-30 14:01:10 -0700917static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
918 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700919
920 cpi->oxcf = *oxcf;
921 cpi->framerate = oxcf->init_framerate;
922
923 cm->profile = oxcf->profile;
924 cm->bit_depth = oxcf->bit_depth;
Yaowu Xuf883b422016-08-30 14:01:10 -0700925#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700926 cm->use_highbitdepth = oxcf->use_highbitdepth;
927#endif
928 cm->color_space = oxcf->color_space;
929 cm->color_range = oxcf->color_range;
930
931 cm->width = oxcf->width;
932 cm->height = oxcf->height;
Yaowu Xuf883b422016-08-30 14:01:10 -0700933 av1_alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700934
935 // Single thread case: use counts in common.
936 cpi->td.counts = &cm->counts;
937
938 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -0700939 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700940
941 cpi->static_mb_pct = 0;
942 cpi->ref_frame_flags = 0;
943
944 init_buffer_indices(cpi);
945}
946
947static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -0700948 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700949 const int64_t bandwidth = oxcf->target_bandwidth;
950 const int64_t starting = oxcf->starting_buffer_level_ms;
951 const int64_t optimal = oxcf->optimal_buffer_level_ms;
952 const int64_t maximum = oxcf->maximum_buffer_size_ms;
953
954 rc->starting_buffer_level = starting * bandwidth / 1000;
955 rc->optimal_buffer_level =
956 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
957 rc->maximum_buffer_size =
958 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
959}
960
Yaowu Xuf883b422016-08-30 14:01:10 -0700961#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700962#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
963 cpi->fn_ptr[BT].sdf = SDF; \
964 cpi->fn_ptr[BT].sdaf = SDAF; \
965 cpi->fn_ptr[BT].vf = VF; \
966 cpi->fn_ptr[BT].svf = SVF; \
967 cpi->fn_ptr[BT].svaf = SVAF; \
968 cpi->fn_ptr[BT].sdx3f = SDX3F; \
969 cpi->fn_ptr[BT].sdx8f = SDX8F; \
970 cpi->fn_ptr[BT].sdx4df = SDX4DF;
971
972#define MAKE_BFP_SAD_WRAPPER(fnname) \
973 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
974 int source_stride, \
975 const uint8_t *ref_ptr, int ref_stride) { \
976 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
977 } \
978 static unsigned int fnname##_bits10( \
979 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
980 int ref_stride) { \
981 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
982 } \
983 static unsigned int fnname##_bits12( \
984 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
985 int ref_stride) { \
986 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
987 }
988
989#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
990 static unsigned int fnname##_bits8( \
991 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
992 int ref_stride, const uint8_t *second_pred) { \
993 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
994 } \
995 static unsigned int fnname##_bits10( \
996 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
997 int ref_stride, const uint8_t *second_pred) { \
998 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
999 2; \
1000 } \
1001 static unsigned int fnname##_bits12( \
1002 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1003 int ref_stride, const uint8_t *second_pred) { \
1004 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1005 4; \
1006 }
1007
1008#define MAKE_BFP_SAD3_WRAPPER(fnname) \
1009 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1010 const uint8_t *ref_ptr, int ref_stride, \
1011 unsigned int *sad_array) { \
1012 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1013 } \
1014 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1015 const uint8_t *ref_ptr, int ref_stride, \
1016 unsigned int *sad_array) { \
1017 int i; \
1018 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1019 for (i = 0; i < 3; i++) sad_array[i] >>= 2; \
1020 } \
1021 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1022 const uint8_t *ref_ptr, int ref_stride, \
1023 unsigned int *sad_array) { \
1024 int i; \
1025 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1026 for (i = 0; i < 3; i++) sad_array[i] >>= 4; \
1027 }
1028
1029#define MAKE_BFP_SAD8_WRAPPER(fnname) \
1030 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1031 const uint8_t *ref_ptr, int ref_stride, \
1032 unsigned int *sad_array) { \
1033 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1034 } \
1035 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1036 const uint8_t *ref_ptr, int ref_stride, \
1037 unsigned int *sad_array) { \
1038 int i; \
1039 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1040 for (i = 0; i < 8; i++) sad_array[i] >>= 2; \
1041 } \
1042 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1043 const uint8_t *ref_ptr, int ref_stride, \
1044 unsigned int *sad_array) { \
1045 int i; \
1046 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1047 for (i = 0; i < 8; i++) sad_array[i] >>= 4; \
1048 }
1049#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1050 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1051 const uint8_t *const ref_ptr[], int ref_stride, \
1052 unsigned int *sad_array) { \
1053 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1054 } \
1055 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1056 const uint8_t *const ref_ptr[], int ref_stride, \
1057 unsigned int *sad_array) { \
1058 int i; \
1059 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1060 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1061 } \
1062 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1063 const uint8_t *const ref_ptr[], int ref_stride, \
1064 unsigned int *sad_array) { \
1065 int i; \
1066 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1067 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1068 }
1069
1070#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001071MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1072MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
1073MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad128x128x3)
1074MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad128x128x8)
1075MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1076MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1077MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1078MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1079MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1080MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1081MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001082#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001083MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1084MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1085MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1086MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1087MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1088MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1089MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1090MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1091MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1092MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1093MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1094MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1095MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1096MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
1097MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad32x32x3)
1098MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad32x32x8)
1099MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1100MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1101MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
1102MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad64x64x3)
1103MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad64x64x8)
1104MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1105MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1106MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
1107MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad16x16x3)
1108MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad16x16x8)
1109MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1110MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1111MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
1112MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad16x8x3)
1113MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad16x8x8)
1114MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1115MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1116MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
1117MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad8x16x3)
1118MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x16x8)
1119MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1120MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1121MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
1122MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad8x8x3)
1123MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x8x8)
1124MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1125MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1126MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
1127MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x4x8)
1128MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1129MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1130MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
1131MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad4x8x8)
1132MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1133MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1134MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
1135MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad4x4x3)
1136MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad4x4x8)
1137MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001138
1139#if CONFIG_EXT_INTER
1140#define HIGHBD_MBFP(BT, MSDF, MVF, MSVF) \
1141 cpi->fn_ptr[BT].msdf = MSDF; \
1142 cpi->fn_ptr[BT].mvf = MVF; \
1143 cpi->fn_ptr[BT].msvf = MSVF;
1144
1145#define MAKE_MBFP_SAD_WRAPPER(fnname) \
1146 static unsigned int fnname##_bits8( \
1147 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1148 int ref_stride, const uint8_t *m, int m_stride) { \
1149 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, m, m_stride); \
1150 } \
1151 static unsigned int fnname##_bits10( \
1152 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1153 int ref_stride, const uint8_t *m, int m_stride) { \
1154 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, m, m_stride) >> \
1155 2; \
1156 } \
1157 static unsigned int fnname##_bits12( \
1158 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1159 int ref_stride, const uint8_t *m, int m_stride) { \
1160 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, m, m_stride) >> \
1161 4; \
1162 }
1163
1164#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001165MAKE_MBFP_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1166MAKE_MBFP_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1167MAKE_MBFP_SAD_WRAPPER(aom_highbd_masked_sad64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001168#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001169MAKE_MBFP_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1170MAKE_MBFP_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1171MAKE_MBFP_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1172MAKE_MBFP_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1173MAKE_MBFP_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1174MAKE_MBFP_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1175MAKE_MBFP_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1176MAKE_MBFP_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1177MAKE_MBFP_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1178MAKE_MBFP_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1179MAKE_MBFP_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1180MAKE_MBFP_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1181MAKE_MBFP_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001182#endif // CONFIG_EXT_INTER
1183
Yue Chencb60b182016-10-13 15:18:22 -07001184#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001185#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1186 cpi->fn_ptr[BT].osdf = OSDF; \
1187 cpi->fn_ptr[BT].ovf = OVF; \
1188 cpi->fn_ptr[BT].osvf = OSVF;
1189
1190#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1191 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1192 const int32_t *wsrc, \
1193 const int32_t *msk) { \
1194 return fnname(ref, ref_stride, wsrc, msk); \
1195 } \
1196 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1197 const int32_t *wsrc, \
1198 const int32_t *msk) { \
1199 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1200 } \
1201 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1202 const int32_t *wsrc, \
1203 const int32_t *msk) { \
1204 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1205 }
1206
1207#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001208MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1209MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1210MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001211#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001212MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1213MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1214MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1215MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1216MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1217MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1218MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1219MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1220MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1221MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1222MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1223MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1224MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Yue Chencb60b182016-10-13 15:18:22 -07001225#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001226
Yaowu Xuf883b422016-08-30 14:01:10 -07001227static void highbd_set_var_fns(AV1_COMP *const cpi) {
1228 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001229 if (cm->use_highbitdepth) {
1230 switch (cm->bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001231 case AOM_BITS_8:
1232 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1233 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1234 aom_highbd_8_sub_pixel_variance32x16,
1235 aom_highbd_8_sub_pixel_avg_variance32x16, NULL, NULL,
1236 aom_highbd_sad32x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001237
Yaowu Xuf883b422016-08-30 14:01:10 -07001238 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1239 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1240 aom_highbd_8_sub_pixel_variance16x32,
1241 aom_highbd_8_sub_pixel_avg_variance16x32, NULL, NULL,
1242 aom_highbd_sad16x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001243
Yaowu Xuf883b422016-08-30 14:01:10 -07001244 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1245 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1246 aom_highbd_8_sub_pixel_variance64x32,
1247 aom_highbd_8_sub_pixel_avg_variance64x32, NULL, NULL,
1248 aom_highbd_sad64x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001249
Yaowu Xuf883b422016-08-30 14:01:10 -07001250 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1251 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1252 aom_highbd_8_sub_pixel_variance32x64,
1253 aom_highbd_8_sub_pixel_avg_variance32x64, NULL, NULL,
1254 aom_highbd_sad32x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001255
Yaowu Xuf883b422016-08-30 14:01:10 -07001256 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1257 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1258 aom_highbd_8_sub_pixel_variance32x32,
1259 aom_highbd_8_sub_pixel_avg_variance32x32,
1260 aom_highbd_sad32x32x3_bits8, aom_highbd_sad32x32x8_bits8,
1261 aom_highbd_sad32x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001262
Yaowu Xuf883b422016-08-30 14:01:10 -07001263 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1264 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1265 aom_highbd_8_sub_pixel_variance64x64,
1266 aom_highbd_8_sub_pixel_avg_variance64x64,
1267 aom_highbd_sad64x64x3_bits8, aom_highbd_sad64x64x8_bits8,
1268 aom_highbd_sad64x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001269
Yaowu Xuf883b422016-08-30 14:01:10 -07001270 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1271 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1272 aom_highbd_8_sub_pixel_variance16x16,
1273 aom_highbd_8_sub_pixel_avg_variance16x16,
1274 aom_highbd_sad16x16x3_bits8, aom_highbd_sad16x16x8_bits8,
1275 aom_highbd_sad16x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001276
1277 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001278 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1279 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
1280 aom_highbd_8_sub_pixel_avg_variance16x8, aom_highbd_sad16x8x3_bits8,
1281 aom_highbd_sad16x8x8_bits8, aom_highbd_sad16x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001282
1283 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001284 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1285 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
1286 aom_highbd_8_sub_pixel_avg_variance8x16, aom_highbd_sad8x16x3_bits8,
1287 aom_highbd_sad8x16x8_bits8, aom_highbd_sad8x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001288
1289 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001290 BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
1291 aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
1292 aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits8,
1293 aom_highbd_sad8x8x8_bits8, aom_highbd_sad8x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001294
Yaowu Xuf883b422016-08-30 14:01:10 -07001295 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits8,
1296 aom_highbd_sad8x4_avg_bits8, aom_highbd_8_variance8x4,
1297 aom_highbd_8_sub_pixel_variance8x4,
1298 aom_highbd_8_sub_pixel_avg_variance8x4, NULL,
1299 aom_highbd_sad8x4x8_bits8, aom_highbd_sad8x4x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001300
Yaowu Xuf883b422016-08-30 14:01:10 -07001301 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits8,
1302 aom_highbd_sad4x8_avg_bits8, aom_highbd_8_variance4x8,
1303 aom_highbd_8_sub_pixel_variance4x8,
1304 aom_highbd_8_sub_pixel_avg_variance4x8, NULL,
1305 aom_highbd_sad4x8x8_bits8, aom_highbd_sad4x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001306
1307 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001308 BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
1309 aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
1310 aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits8,
1311 aom_highbd_sad4x4x8_bits8, aom_highbd_sad4x4x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001312
1313#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001314 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
1315 aom_highbd_sad128x128_avg_bits8,
1316 aom_highbd_8_variance128x128,
1317 aom_highbd_8_sub_pixel_variance128x128,
1318 aom_highbd_8_sub_pixel_avg_variance128x128,
1319 aom_highbd_sad128x128x3_bits8, aom_highbd_sad128x128x8_bits8,
1320 aom_highbd_sad128x128x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001321
Yaowu Xuf883b422016-08-30 14:01:10 -07001322 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1323 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1324 aom_highbd_8_sub_pixel_variance128x64,
1325 aom_highbd_8_sub_pixel_avg_variance128x64, NULL, NULL,
1326 aom_highbd_sad128x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001327
Yaowu Xuf883b422016-08-30 14:01:10 -07001328 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1329 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1330 aom_highbd_8_sub_pixel_variance64x128,
1331 aom_highbd_8_sub_pixel_avg_variance64x128, NULL, NULL,
1332 aom_highbd_sad64x128x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001333#endif // CONFIG_EXT_PARTITION
1334
1335#if CONFIG_EXT_INTER
1336#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001337 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1338 aom_highbd_masked_variance128x128,
1339 aom_highbd_masked_sub_pixel_variance128x128)
1340 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1341 aom_highbd_masked_variance128x64,
1342 aom_highbd_masked_sub_pixel_variance128x64)
1343 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1344 aom_highbd_masked_variance64x128,
1345 aom_highbd_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001346#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001347 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1348 aom_highbd_masked_variance64x64,
1349 aom_highbd_masked_sub_pixel_variance64x64)
1350 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1351 aom_highbd_masked_variance64x32,
1352 aom_highbd_masked_sub_pixel_variance64x32)
1353 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1354 aom_highbd_masked_variance32x64,
1355 aom_highbd_masked_sub_pixel_variance32x64)
1356 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1357 aom_highbd_masked_variance32x32,
1358 aom_highbd_masked_sub_pixel_variance32x32)
1359 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1360 aom_highbd_masked_variance32x16,
1361 aom_highbd_masked_sub_pixel_variance32x16)
1362 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1363 aom_highbd_masked_variance16x32,
1364 aom_highbd_masked_sub_pixel_variance16x32)
1365 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1366 aom_highbd_masked_variance16x16,
1367 aom_highbd_masked_sub_pixel_variance16x16)
1368 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1369 aom_highbd_masked_variance8x16,
1370 aom_highbd_masked_sub_pixel_variance8x16)
1371 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1372 aom_highbd_masked_variance16x8,
1373 aom_highbd_masked_sub_pixel_variance16x8)
1374 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1375 aom_highbd_masked_variance8x8,
1376 aom_highbd_masked_sub_pixel_variance8x8)
1377 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1378 aom_highbd_masked_variance4x8,
1379 aom_highbd_masked_sub_pixel_variance4x8)
1380 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1381 aom_highbd_masked_variance8x4,
1382 aom_highbd_masked_sub_pixel_variance8x4)
1383 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1384 aom_highbd_masked_variance4x4,
1385 aom_highbd_masked_sub_pixel_variance4x4)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001386#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07001387#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001388#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001389 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1390 aom_highbd_obmc_variance128x128,
1391 aom_highbd_obmc_sub_pixel_variance128x128)
1392 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1393 aom_highbd_obmc_variance128x64,
1394 aom_highbd_obmc_sub_pixel_variance128x64)
1395 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1396 aom_highbd_obmc_variance64x128,
1397 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001398#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001399 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
1400 aom_highbd_obmc_variance64x64,
1401 aom_highbd_obmc_sub_pixel_variance64x64)
1402 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
1403 aom_highbd_obmc_variance64x32,
1404 aom_highbd_obmc_sub_pixel_variance64x32)
1405 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
1406 aom_highbd_obmc_variance32x64,
1407 aom_highbd_obmc_sub_pixel_variance32x64)
1408 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
1409 aom_highbd_obmc_variance32x32,
1410 aom_highbd_obmc_sub_pixel_variance32x32)
1411 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
1412 aom_highbd_obmc_variance32x16,
1413 aom_highbd_obmc_sub_pixel_variance32x16)
1414 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
1415 aom_highbd_obmc_variance16x32,
1416 aom_highbd_obmc_sub_pixel_variance16x32)
1417 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
1418 aom_highbd_obmc_variance16x16,
1419 aom_highbd_obmc_sub_pixel_variance16x16)
1420 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
1421 aom_highbd_obmc_variance8x16,
1422 aom_highbd_obmc_sub_pixel_variance8x16)
1423 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
1424 aom_highbd_obmc_variance16x8,
1425 aom_highbd_obmc_sub_pixel_variance16x8)
1426 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
1427 aom_highbd_obmc_variance8x8,
1428 aom_highbd_obmc_sub_pixel_variance8x8)
1429 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
1430 aom_highbd_obmc_variance4x8,
1431 aom_highbd_obmc_sub_pixel_variance4x8)
1432 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
1433 aom_highbd_obmc_variance8x4,
1434 aom_highbd_obmc_sub_pixel_variance8x4)
1435 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
1436 aom_highbd_obmc_variance4x4,
1437 aom_highbd_obmc_sub_pixel_variance4x4)
Yue Chencb60b182016-10-13 15:18:22 -07001438#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001439 break;
1440
Yaowu Xuf883b422016-08-30 14:01:10 -07001441 case AOM_BITS_10:
1442 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
1443 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
1444 aom_highbd_10_sub_pixel_variance32x16,
1445 aom_highbd_10_sub_pixel_avg_variance32x16, NULL, NULL,
1446 aom_highbd_sad32x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001447
Yaowu Xuf883b422016-08-30 14:01:10 -07001448 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
1449 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
1450 aom_highbd_10_sub_pixel_variance16x32,
1451 aom_highbd_10_sub_pixel_avg_variance16x32, NULL, NULL,
1452 aom_highbd_sad16x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001453
Yaowu Xuf883b422016-08-30 14:01:10 -07001454 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
1455 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
1456 aom_highbd_10_sub_pixel_variance64x32,
1457 aom_highbd_10_sub_pixel_avg_variance64x32, NULL, NULL,
1458 aom_highbd_sad64x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001459
Yaowu Xuf883b422016-08-30 14:01:10 -07001460 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
1461 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
1462 aom_highbd_10_sub_pixel_variance32x64,
1463 aom_highbd_10_sub_pixel_avg_variance32x64, NULL, NULL,
1464 aom_highbd_sad32x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001465
Yaowu Xuf883b422016-08-30 14:01:10 -07001466 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
1467 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
1468 aom_highbd_10_sub_pixel_variance32x32,
1469 aom_highbd_10_sub_pixel_avg_variance32x32,
1470 aom_highbd_sad32x32x3_bits10, aom_highbd_sad32x32x8_bits10,
1471 aom_highbd_sad32x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001472
Yaowu Xuf883b422016-08-30 14:01:10 -07001473 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
1474 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
1475 aom_highbd_10_sub_pixel_variance64x64,
1476 aom_highbd_10_sub_pixel_avg_variance64x64,
1477 aom_highbd_sad64x64x3_bits10, aom_highbd_sad64x64x8_bits10,
1478 aom_highbd_sad64x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001479
Yaowu Xuf883b422016-08-30 14:01:10 -07001480 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
1481 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
1482 aom_highbd_10_sub_pixel_variance16x16,
1483 aom_highbd_10_sub_pixel_avg_variance16x16,
1484 aom_highbd_sad16x16x3_bits10, aom_highbd_sad16x16x8_bits10,
1485 aom_highbd_sad16x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001486
Yaowu Xuf883b422016-08-30 14:01:10 -07001487 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
1488 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
1489 aom_highbd_10_sub_pixel_variance16x8,
1490 aom_highbd_10_sub_pixel_avg_variance16x8,
1491 aom_highbd_sad16x8x3_bits10, aom_highbd_sad16x8x8_bits10,
1492 aom_highbd_sad16x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001493
Yaowu Xuf883b422016-08-30 14:01:10 -07001494 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
1495 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
1496 aom_highbd_10_sub_pixel_variance8x16,
1497 aom_highbd_10_sub_pixel_avg_variance8x16,
1498 aom_highbd_sad8x16x3_bits10, aom_highbd_sad8x16x8_bits10,
1499 aom_highbd_sad8x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001500
1501 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001502 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
1503 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
1504 aom_highbd_10_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits10,
1505 aom_highbd_sad8x8x8_bits10, aom_highbd_sad8x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001506
Yaowu Xuf883b422016-08-30 14:01:10 -07001507 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits10,
1508 aom_highbd_sad8x4_avg_bits10, aom_highbd_10_variance8x4,
1509 aom_highbd_10_sub_pixel_variance8x4,
1510 aom_highbd_10_sub_pixel_avg_variance8x4, NULL,
1511 aom_highbd_sad8x4x8_bits10, aom_highbd_sad8x4x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001512
Yaowu Xuf883b422016-08-30 14:01:10 -07001513 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits10,
1514 aom_highbd_sad4x8_avg_bits10, aom_highbd_10_variance4x8,
1515 aom_highbd_10_sub_pixel_variance4x8,
1516 aom_highbd_10_sub_pixel_avg_variance4x8, NULL,
1517 aom_highbd_sad4x8x8_bits10, aom_highbd_sad4x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001518
1519 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001520 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
1521 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
1522 aom_highbd_10_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits10,
1523 aom_highbd_sad4x4x8_bits10, aom_highbd_sad4x4x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001524
1525#if CONFIG_EXT_PARTITION
1526 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001527 BLOCK_128X128, aom_highbd_sad128x128_bits10,
1528 aom_highbd_sad128x128_avg_bits10, aom_highbd_10_variance128x128,
1529 aom_highbd_10_sub_pixel_variance128x128,
1530 aom_highbd_10_sub_pixel_avg_variance128x128,
1531 aom_highbd_sad128x128x3_bits10, aom_highbd_sad128x128x8_bits10,
1532 aom_highbd_sad128x128x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001533
Yaowu Xuf883b422016-08-30 14:01:10 -07001534 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
1535 aom_highbd_sad128x64_avg_bits10,
1536 aom_highbd_10_variance128x64,
1537 aom_highbd_10_sub_pixel_variance128x64,
1538 aom_highbd_10_sub_pixel_avg_variance128x64, NULL, NULL,
1539 aom_highbd_sad128x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001540
Yaowu Xuf883b422016-08-30 14:01:10 -07001541 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
1542 aom_highbd_sad64x128_avg_bits10,
1543 aom_highbd_10_variance64x128,
1544 aom_highbd_10_sub_pixel_variance64x128,
1545 aom_highbd_10_sub_pixel_avg_variance64x128, NULL, NULL,
1546 aom_highbd_sad64x128x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001547#endif // CONFIG_EXT_PARTITION
1548
1549#if CONFIG_EXT_INTER
1550#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001551 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
1552 aom_highbd_10_masked_variance128x128,
1553 aom_highbd_10_masked_sub_pixel_variance128x128)
1554 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
1555 aom_highbd_10_masked_variance128x64,
1556 aom_highbd_10_masked_sub_pixel_variance128x64)
1557 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
1558 aom_highbd_10_masked_variance64x128,
1559 aom_highbd_10_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001560#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001561 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
1562 aom_highbd_10_masked_variance64x64,
1563 aom_highbd_10_masked_sub_pixel_variance64x64)
1564 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
1565 aom_highbd_10_masked_variance64x32,
1566 aom_highbd_10_masked_sub_pixel_variance64x32)
1567 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
1568 aom_highbd_10_masked_variance32x64,
1569 aom_highbd_10_masked_sub_pixel_variance32x64)
1570 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
1571 aom_highbd_10_masked_variance32x32,
1572 aom_highbd_10_masked_sub_pixel_variance32x32)
1573 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
1574 aom_highbd_10_masked_variance32x16,
1575 aom_highbd_10_masked_sub_pixel_variance32x16)
1576 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
1577 aom_highbd_10_masked_variance16x32,
1578 aom_highbd_10_masked_sub_pixel_variance16x32)
1579 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
1580 aom_highbd_10_masked_variance16x16,
1581 aom_highbd_10_masked_sub_pixel_variance16x16)
1582 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
1583 aom_highbd_10_masked_variance8x16,
1584 aom_highbd_10_masked_sub_pixel_variance8x16)
1585 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
1586 aom_highbd_10_masked_variance16x8,
1587 aom_highbd_10_masked_sub_pixel_variance16x8)
1588 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
1589 aom_highbd_10_masked_variance8x8,
1590 aom_highbd_10_masked_sub_pixel_variance8x8)
1591 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
1592 aom_highbd_10_masked_variance4x8,
1593 aom_highbd_10_masked_sub_pixel_variance4x8)
1594 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
1595 aom_highbd_10_masked_variance8x4,
1596 aom_highbd_10_masked_sub_pixel_variance8x4)
1597 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
1598 aom_highbd_10_masked_variance4x4,
1599 aom_highbd_10_masked_sub_pixel_variance4x4)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001600#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07001601#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001602#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001603 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
1604 aom_highbd_10_obmc_variance128x128,
1605 aom_highbd_10_obmc_sub_pixel_variance128x128)
1606 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
1607 aom_highbd_10_obmc_variance128x64,
1608 aom_highbd_10_obmc_sub_pixel_variance128x64)
1609 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
1610 aom_highbd_10_obmc_variance64x128,
1611 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001612#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001613 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
1614 aom_highbd_10_obmc_variance64x64,
1615 aom_highbd_10_obmc_sub_pixel_variance64x64)
1616 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
1617 aom_highbd_10_obmc_variance64x32,
1618 aom_highbd_10_obmc_sub_pixel_variance64x32)
1619 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
1620 aom_highbd_10_obmc_variance32x64,
1621 aom_highbd_10_obmc_sub_pixel_variance32x64)
1622 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
1623 aom_highbd_10_obmc_variance32x32,
1624 aom_highbd_10_obmc_sub_pixel_variance32x32)
1625 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
1626 aom_highbd_10_obmc_variance32x16,
1627 aom_highbd_10_obmc_sub_pixel_variance32x16)
1628 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
1629 aom_highbd_10_obmc_variance16x32,
1630 aom_highbd_10_obmc_sub_pixel_variance16x32)
1631 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
1632 aom_highbd_10_obmc_variance16x16,
1633 aom_highbd_10_obmc_sub_pixel_variance16x16)
1634 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
1635 aom_highbd_10_obmc_variance8x16,
1636 aom_highbd_10_obmc_sub_pixel_variance8x16)
1637 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
1638 aom_highbd_10_obmc_variance16x8,
1639 aom_highbd_10_obmc_sub_pixel_variance16x8)
1640 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
1641 aom_highbd_10_obmc_variance8x8,
1642 aom_highbd_10_obmc_sub_pixel_variance8x8)
1643 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
1644 aom_highbd_10_obmc_variance4x8,
1645 aom_highbd_10_obmc_sub_pixel_variance4x8)
1646 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
1647 aom_highbd_10_obmc_variance8x4,
1648 aom_highbd_10_obmc_sub_pixel_variance8x4)
1649 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
1650 aom_highbd_10_obmc_variance4x4,
1651 aom_highbd_10_obmc_sub_pixel_variance4x4)
Yue Chencb60b182016-10-13 15:18:22 -07001652#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001653 break;
1654
Yaowu Xuf883b422016-08-30 14:01:10 -07001655 case AOM_BITS_12:
1656 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
1657 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
1658 aom_highbd_12_sub_pixel_variance32x16,
1659 aom_highbd_12_sub_pixel_avg_variance32x16, NULL, NULL,
1660 aom_highbd_sad32x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001661
Yaowu Xuf883b422016-08-30 14:01:10 -07001662 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
1663 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
1664 aom_highbd_12_sub_pixel_variance16x32,
1665 aom_highbd_12_sub_pixel_avg_variance16x32, NULL, NULL,
1666 aom_highbd_sad16x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001667
Yaowu Xuf883b422016-08-30 14:01:10 -07001668 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
1669 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
1670 aom_highbd_12_sub_pixel_variance64x32,
1671 aom_highbd_12_sub_pixel_avg_variance64x32, NULL, NULL,
1672 aom_highbd_sad64x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001673
Yaowu Xuf883b422016-08-30 14:01:10 -07001674 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
1675 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
1676 aom_highbd_12_sub_pixel_variance32x64,
1677 aom_highbd_12_sub_pixel_avg_variance32x64, NULL, NULL,
1678 aom_highbd_sad32x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001679
Yaowu Xuf883b422016-08-30 14:01:10 -07001680 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
1681 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
1682 aom_highbd_12_sub_pixel_variance32x32,
1683 aom_highbd_12_sub_pixel_avg_variance32x32,
1684 aom_highbd_sad32x32x3_bits12, aom_highbd_sad32x32x8_bits12,
1685 aom_highbd_sad32x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001686
Yaowu Xuf883b422016-08-30 14:01:10 -07001687 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
1688 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
1689 aom_highbd_12_sub_pixel_variance64x64,
1690 aom_highbd_12_sub_pixel_avg_variance64x64,
1691 aom_highbd_sad64x64x3_bits12, aom_highbd_sad64x64x8_bits12,
1692 aom_highbd_sad64x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001693
Yaowu Xuf883b422016-08-30 14:01:10 -07001694 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
1695 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
1696 aom_highbd_12_sub_pixel_variance16x16,
1697 aom_highbd_12_sub_pixel_avg_variance16x16,
1698 aom_highbd_sad16x16x3_bits12, aom_highbd_sad16x16x8_bits12,
1699 aom_highbd_sad16x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001700
Yaowu Xuf883b422016-08-30 14:01:10 -07001701 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
1702 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
1703 aom_highbd_12_sub_pixel_variance16x8,
1704 aom_highbd_12_sub_pixel_avg_variance16x8,
1705 aom_highbd_sad16x8x3_bits12, aom_highbd_sad16x8x8_bits12,
1706 aom_highbd_sad16x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001707
Yaowu Xuf883b422016-08-30 14:01:10 -07001708 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
1709 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
1710 aom_highbd_12_sub_pixel_variance8x16,
1711 aom_highbd_12_sub_pixel_avg_variance8x16,
1712 aom_highbd_sad8x16x3_bits12, aom_highbd_sad8x16x8_bits12,
1713 aom_highbd_sad8x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001714
1715 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001716 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
1717 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
1718 aom_highbd_12_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits12,
1719 aom_highbd_sad8x8x8_bits12, aom_highbd_sad8x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001720
Yaowu Xuf883b422016-08-30 14:01:10 -07001721 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits12,
1722 aom_highbd_sad8x4_avg_bits12, aom_highbd_12_variance8x4,
1723 aom_highbd_12_sub_pixel_variance8x4,
1724 aom_highbd_12_sub_pixel_avg_variance8x4, NULL,
1725 aom_highbd_sad8x4x8_bits12, aom_highbd_sad8x4x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001726
Yaowu Xuf883b422016-08-30 14:01:10 -07001727 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits12,
1728 aom_highbd_sad4x8_avg_bits12, aom_highbd_12_variance4x8,
1729 aom_highbd_12_sub_pixel_variance4x8,
1730 aom_highbd_12_sub_pixel_avg_variance4x8, NULL,
1731 aom_highbd_sad4x8x8_bits12, aom_highbd_sad4x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001732
1733 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001734 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
1735 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
1736 aom_highbd_12_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits12,
1737 aom_highbd_sad4x4x8_bits12, aom_highbd_sad4x4x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001738
1739#if CONFIG_EXT_PARTITION
1740 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001741 BLOCK_128X128, aom_highbd_sad128x128_bits12,
1742 aom_highbd_sad128x128_avg_bits12, aom_highbd_12_variance128x128,
1743 aom_highbd_12_sub_pixel_variance128x128,
1744 aom_highbd_12_sub_pixel_avg_variance128x128,
1745 aom_highbd_sad128x128x3_bits12, aom_highbd_sad128x128x8_bits12,
1746 aom_highbd_sad128x128x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001747
Yaowu Xuf883b422016-08-30 14:01:10 -07001748 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
1749 aom_highbd_sad128x64_avg_bits12,
1750 aom_highbd_12_variance128x64,
1751 aom_highbd_12_sub_pixel_variance128x64,
1752 aom_highbd_12_sub_pixel_avg_variance128x64, NULL, NULL,
1753 aom_highbd_sad128x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001754
Yaowu Xuf883b422016-08-30 14:01:10 -07001755 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
1756 aom_highbd_sad64x128_avg_bits12,
1757 aom_highbd_12_variance64x128,
1758 aom_highbd_12_sub_pixel_variance64x128,
1759 aom_highbd_12_sub_pixel_avg_variance64x128, NULL, NULL,
1760 aom_highbd_sad64x128x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001761#endif // CONFIG_EXT_PARTITION
1762
1763#if CONFIG_EXT_INTER
1764#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001765 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
1766 aom_highbd_12_masked_variance128x128,
1767 aom_highbd_12_masked_sub_pixel_variance128x128)
1768 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
1769 aom_highbd_12_masked_variance128x64,
1770 aom_highbd_12_masked_sub_pixel_variance128x64)
1771 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
1772 aom_highbd_12_masked_variance64x128,
1773 aom_highbd_12_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001774#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001775 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
1776 aom_highbd_12_masked_variance64x64,
1777 aom_highbd_12_masked_sub_pixel_variance64x64)
1778 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
1779 aom_highbd_12_masked_variance64x32,
1780 aom_highbd_12_masked_sub_pixel_variance64x32)
1781 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
1782 aom_highbd_12_masked_variance32x64,
1783 aom_highbd_12_masked_sub_pixel_variance32x64)
1784 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
1785 aom_highbd_12_masked_variance32x32,
1786 aom_highbd_12_masked_sub_pixel_variance32x32)
1787 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
1788 aom_highbd_12_masked_variance32x16,
1789 aom_highbd_12_masked_sub_pixel_variance32x16)
1790 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
1791 aom_highbd_12_masked_variance16x32,
1792 aom_highbd_12_masked_sub_pixel_variance16x32)
1793 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
1794 aom_highbd_12_masked_variance16x16,
1795 aom_highbd_12_masked_sub_pixel_variance16x16)
1796 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
1797 aom_highbd_12_masked_variance8x16,
1798 aom_highbd_12_masked_sub_pixel_variance8x16)
1799 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
1800 aom_highbd_12_masked_variance16x8,
1801 aom_highbd_12_masked_sub_pixel_variance16x8)
1802 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
1803 aom_highbd_12_masked_variance8x8,
1804 aom_highbd_12_masked_sub_pixel_variance8x8)
1805 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
1806 aom_highbd_12_masked_variance4x8,
1807 aom_highbd_12_masked_sub_pixel_variance4x8)
1808 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
1809 aom_highbd_12_masked_variance8x4,
1810 aom_highbd_12_masked_sub_pixel_variance8x4)
1811 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
1812 aom_highbd_12_masked_variance4x4,
1813 aom_highbd_12_masked_sub_pixel_variance4x4)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001814#endif // CONFIG_EXT_INTER
1815
Yue Chencb60b182016-10-13 15:18:22 -07001816#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001817#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001818 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
1819 aom_highbd_12_obmc_variance128x128,
1820 aom_highbd_12_obmc_sub_pixel_variance128x128)
1821 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
1822 aom_highbd_12_obmc_variance128x64,
1823 aom_highbd_12_obmc_sub_pixel_variance128x64)
1824 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
1825 aom_highbd_12_obmc_variance64x128,
1826 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001827#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001828 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
1829 aom_highbd_12_obmc_variance64x64,
1830 aom_highbd_12_obmc_sub_pixel_variance64x64)
1831 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
1832 aom_highbd_12_obmc_variance64x32,
1833 aom_highbd_12_obmc_sub_pixel_variance64x32)
1834 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
1835 aom_highbd_12_obmc_variance32x64,
1836 aom_highbd_12_obmc_sub_pixel_variance32x64)
1837 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
1838 aom_highbd_12_obmc_variance32x32,
1839 aom_highbd_12_obmc_sub_pixel_variance32x32)
1840 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
1841 aom_highbd_12_obmc_variance32x16,
1842 aom_highbd_12_obmc_sub_pixel_variance32x16)
1843 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
1844 aom_highbd_12_obmc_variance16x32,
1845 aom_highbd_12_obmc_sub_pixel_variance16x32)
1846 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
1847 aom_highbd_12_obmc_variance16x16,
1848 aom_highbd_12_obmc_sub_pixel_variance16x16)
1849 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
1850 aom_highbd_12_obmc_variance8x16,
1851 aom_highbd_12_obmc_sub_pixel_variance8x16)
1852 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
1853 aom_highbd_12_obmc_variance16x8,
1854 aom_highbd_12_obmc_sub_pixel_variance16x8)
1855 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
1856 aom_highbd_12_obmc_variance8x8,
1857 aom_highbd_12_obmc_sub_pixel_variance8x8)
1858 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
1859 aom_highbd_12_obmc_variance4x8,
1860 aom_highbd_12_obmc_sub_pixel_variance4x8)
1861 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
1862 aom_highbd_12_obmc_variance8x4,
1863 aom_highbd_12_obmc_sub_pixel_variance8x4)
1864 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
1865 aom_highbd_12_obmc_variance4x4,
1866 aom_highbd_12_obmc_sub_pixel_variance4x4)
Yue Chencb60b182016-10-13 15:18:22 -07001867#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001868 break;
1869
1870 default:
1871 assert(0 &&
Yaowu Xuf883b422016-08-30 14:01:10 -07001872 "cm->bit_depth should be AOM_BITS_8, "
1873 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07001874 }
1875 }
1876}
Yaowu Xuf883b422016-08-30 14:01:10 -07001877#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001878
Yaowu Xuf883b422016-08-30 14:01:10 -07001879static void realloc_segmentation_maps(AV1_COMP *cpi) {
1880 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001881
1882 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07001883 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001884 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Yaowu Xuf883b422016-08-30 14:01:10 -07001885 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001886
1887 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07001888 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001889 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
Yaowu Xuf883b422016-08-30 14:01:10 -07001890 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001891
1892 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07001893 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001894 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Yaowu Xuf883b422016-08-30 14:01:10 -07001895 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001896
1897 // And a place holder structure is the coding context
1898 // for use if we want to save and restore it
Yaowu Xuf883b422016-08-30 14:01:10 -07001899 aom_free(cpi->coding_context.last_frame_seg_map_copy);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001900 CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy,
Yaowu Xuf883b422016-08-30 14:01:10 -07001901 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001902}
1903
Yaowu Xuf883b422016-08-30 14:01:10 -07001904void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
1905 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001906 RATE_CONTROL *const rc = &cpi->rc;
1907
1908 if (cm->profile != oxcf->profile) cm->profile = oxcf->profile;
1909 cm->bit_depth = oxcf->bit_depth;
1910 cm->color_space = oxcf->color_space;
1911 cm->color_range = oxcf->color_range;
1912
1913 if (cm->profile <= PROFILE_1)
Yaowu Xuf883b422016-08-30 14:01:10 -07001914 assert(cm->bit_depth == AOM_BITS_8);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001915 else
Yaowu Xuf883b422016-08-30 14:01:10 -07001916 assert(cm->bit_depth > AOM_BITS_8);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001917
1918 cpi->oxcf = *oxcf;
Yaowu Xuf883b422016-08-30 14:01:10 -07001919#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001920 cpi->td.mb.e_mbd.bd = (int)cm->bit_depth;
Yaowu Xuf883b422016-08-30 14:01:10 -07001921#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001922#if CONFIG_GLOBAL_MOTION
1923 cpi->td.mb.e_mbd.global_motion = cm->global_motion;
1924#endif // CONFIG_GLOBAL_MOTION
1925
Yaowu Xuf883b422016-08-30 14:01:10 -07001926 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001927 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
1928 } else {
1929 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
1930 }
1931
1932 cpi->refresh_last_frame = 1;
1933 cpi->refresh_golden_frame = 0;
1934#if CONFIG_EXT_REFS
1935 cpi->refresh_bwd_ref_frame = 0;
1936#endif // CONFIG_EXT_REFS
1937
1938 cm->refresh_frame_context =
1939 (oxcf->error_resilient_mode || oxcf->frame_parallel_decoding_mode)
1940 ? REFRESH_FRAME_CONTEXT_FORWARD
1941 : REFRESH_FRAME_CONTEXT_BACKWARD;
1942 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
1943
Yaowu Xuf883b422016-08-30 14:01:10 -07001944 cm->allow_screen_content_tools = (cpi->oxcf.content == AOM_CONTENT_SCREEN);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001945 if (cm->allow_screen_content_tools) {
1946 MACROBLOCK *x = &cpi->td.mb;
1947 if (x->palette_buffer == 0) {
1948 CHECK_MEM_ERROR(cm, x->palette_buffer,
Yaowu Xuf883b422016-08-30 14:01:10 -07001949 aom_memalign(16, sizeof(*x->palette_buffer)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001950 }
1951 // Reallocate the pc_tree, as it's contents depends on
1952 // the state of cm->allow_screen_content_tools
Yaowu Xuf883b422016-08-30 14:01:10 -07001953 av1_free_pc_tree(&cpi->td);
1954 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001955 }
1956
Yaowu Xuf883b422016-08-30 14:01:10 -07001957 av1_reset_segment_features(cm);
1958 av1_set_high_precision_mv(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001959
1960 {
1961 int i;
1962
1963 for (i = 0; i < MAX_SEGMENTS; i++)
1964 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1965 }
1966 cpi->encode_breakout = cpi->oxcf.encode_breakout;
1967
1968 set_rc_buffer_sizes(rc, &cpi->oxcf);
1969
1970 // Under a configuration change, where maximum_buffer_size may change,
1971 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07001972 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
1973 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001974
1975 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07001976 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001977
1978 // Set absolute upper and lower quality limits
1979 rc->worst_quality = cpi->oxcf.worst_allowed_q;
1980 rc->best_quality = cpi->oxcf.best_allowed_q;
1981
1982 cm->interp_filter = cpi->sf.default_interp_filter;
1983
1984 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
1985 cm->render_width = cpi->oxcf.render_width;
1986 cm->render_height = cpi->oxcf.render_height;
1987 } else {
1988 cm->render_width = cpi->oxcf.width;
1989 cm->render_height = cpi->oxcf.height;
1990 }
1991 cm->width = cpi->oxcf.width;
1992 cm->height = cpi->oxcf.height;
1993
1994 if (cpi->initial_width) {
1995 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001996 av1_free_context_buffers(cm);
1997 av1_alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001998 realloc_segmentation_maps(cpi);
1999 cpi->initial_width = cpi->initial_height = 0;
2000 }
2001 }
2002 update_frame_size(cpi);
2003
2004 cpi->alt_ref_source = NULL;
2005 rc->is_src_frame_alt_ref = 0;
2006
2007#if CONFIG_EXT_REFS
2008 rc->is_bwd_ref_frame = 0;
2009 rc->is_last_bipred_frame = 0;
2010 rc->is_bipred_frame = 0;
2011#endif // CONFIG_EXT_REFS
2012
2013#if 0
2014 // Experimental RD Code
2015 cpi->frame_distortion = 0;
2016 cpi->last_frame_distortion = 0;
2017#endif
2018
2019 set_tile_info(cpi);
2020
2021 cpi->ext_refresh_frame_flags_pending = 0;
2022 cpi->ext_refresh_frame_context_pending = 0;
2023
Yaowu Xuf883b422016-08-30 14:01:10 -07002024#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002025 highbd_set_var_fns(cpi);
2026#endif
2027}
2028
2029#ifndef M_LOG2_E
2030#define M_LOG2_E 0.693147180559945309417
2031#endif
2032#define log2f(x) (log(x) / (float)M_LOG2_E)
2033
2034#if !CONFIG_REF_MV
2035static void cal_nmvjointsadcost(int *mvjointsadcost) {
2036 mvjointsadcost[0] = 600;
2037 mvjointsadcost[1] = 300;
2038 mvjointsadcost[2] = 300;
2039 mvjointsadcost[3] = 300;
2040}
2041#endif
2042
2043static void cal_nmvsadcosts(int *mvsadcost[2]) {
2044 int i = 1;
2045
2046 mvsadcost[0][0] = 0;
2047 mvsadcost[1][0] = 0;
2048
2049 do {
2050 double z = 256 * (2 * (log2f(8 * i) + .6));
2051 mvsadcost[0][i] = (int)z;
2052 mvsadcost[1][i] = (int)z;
2053 mvsadcost[0][-i] = (int)z;
2054 mvsadcost[1][-i] = (int)z;
2055 } while (++i <= MV_MAX);
2056}
2057
2058static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
2059 int i = 1;
2060
2061 mvsadcost[0][0] = 0;
2062 mvsadcost[1][0] = 0;
2063
2064 do {
2065 double z = 256 * (2 * (log2f(8 * i) + .6));
2066 mvsadcost[0][i] = (int)z;
2067 mvsadcost[1][i] = (int)z;
2068 mvsadcost[0][-i] = (int)z;
2069 mvsadcost[1][-i] = (int)z;
2070 } while (++i <= MV_MAX);
2071}
2072
Yaowu Xuf883b422016-08-30 14:01:10 -07002073static INLINE void init_upsampled_ref_frame_bufs(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002074 int i;
2075
2076 for (i = 0; i < (REF_FRAMES + 1); ++i) {
2077 cpi->upsampled_ref_bufs[i].ref_count = 0;
2078 cpi->upsampled_ref_idx[i] = INVALID_IDX;
2079 }
2080}
2081
Yaowu Xuf883b422016-08-30 14:01:10 -07002082AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
2083 BufferPool *const pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002084 unsigned int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07002085 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2086 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002087
2088 if (!cm) return NULL;
2089
Yaowu Xuf883b422016-08-30 14:01:10 -07002090 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002091
2092 if (setjmp(cm->error.jmp)) {
2093 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002094 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002095 return 0;
2096 }
2097
2098 cm->error.setjmp = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07002099 cm->alloc_mi = av1_enc_alloc_mi;
2100 cm->free_mi = av1_enc_free_mi;
2101 cm->setup_mi = av1_enc_setup_mi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002102
Yaowu Xuf883b422016-08-30 14:01:10 -07002103 CHECK_MEM_ERROR(cm, cm->fc, (FRAME_CONTEXT *)aom_calloc(1, sizeof(*cm->fc)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002104 CHECK_MEM_ERROR(
2105 cm, cm->frame_contexts,
Yaowu Xuf883b422016-08-30 14:01:10 -07002106 (FRAME_CONTEXT *)aom_calloc(FRAME_CONTEXTS, sizeof(*cm->frame_contexts)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002107
2108 cpi->resize_state = 0;
2109 cpi->resize_avg_qp = 0;
2110 cpi->resize_buffer_underflow = 0;
2111 cpi->common.buffer_pool = pool;
2112
2113 init_config(cpi, oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07002114 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002115
2116 cm->current_video_frame = 0;
2117 cpi->partition_search_skippable_frame = 0;
2118 cpi->tile_data = NULL;
2119 cpi->last_show_frame_buf_idx = INVALID_IDX;
2120
2121 realloc_segmentation_maps(cpi);
2122
2123#if CONFIG_REF_MV
2124 for (i = 0; i < NMV_CONTEXTS; ++i) {
2125 CHECK_MEM_ERROR(cm, cpi->nmv_costs[i][0],
Yaowu Xuf883b422016-08-30 14:01:10 -07002126 aom_calloc(MV_VALS, sizeof(*cpi->nmv_costs[i][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002127 CHECK_MEM_ERROR(cm, cpi->nmv_costs[i][1],
Yaowu Xuf883b422016-08-30 14:01:10 -07002128 aom_calloc(MV_VALS, sizeof(*cpi->nmv_costs[i][1])));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002129 CHECK_MEM_ERROR(cm, cpi->nmv_costs_hp[i][0],
Yaowu Xuf883b422016-08-30 14:01:10 -07002130 aom_calloc(MV_VALS, sizeof(*cpi->nmv_costs_hp[i][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002131 CHECK_MEM_ERROR(cm, cpi->nmv_costs_hp[i][1],
Yaowu Xuf883b422016-08-30 14:01:10 -07002132 aom_calloc(MV_VALS, sizeof(*cpi->nmv_costs_hp[i][1])));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002133 }
2134#endif
2135
2136 CHECK_MEM_ERROR(cm, cpi->nmvcosts[0],
Yaowu Xuf883b422016-08-30 14:01:10 -07002137 aom_calloc(MV_VALS, sizeof(*cpi->nmvcosts[0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002138 CHECK_MEM_ERROR(cm, cpi->nmvcosts[1],
Yaowu Xuf883b422016-08-30 14:01:10 -07002139 aom_calloc(MV_VALS, sizeof(*cpi->nmvcosts[1])));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002140 CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[0],
Yaowu Xuf883b422016-08-30 14:01:10 -07002141 aom_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002142 CHECK_MEM_ERROR(cm, cpi->nmvcosts_hp[1],
Yaowu Xuf883b422016-08-30 14:01:10 -07002143 aom_calloc(MV_VALS, sizeof(*cpi->nmvcosts_hp[1])));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002144 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[0],
Yaowu Xuf883b422016-08-30 14:01:10 -07002145 aom_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002146 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts[1],
Yaowu Xuf883b422016-08-30 14:01:10 -07002147 aom_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts[1])));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002148 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[0],
Yaowu Xuf883b422016-08-30 14:01:10 -07002149 aom_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002150 CHECK_MEM_ERROR(cm, cpi->nmvsadcosts_hp[1],
Yaowu Xuf883b422016-08-30 14:01:10 -07002151 aom_calloc(MV_VALS, sizeof(*cpi->nmvsadcosts_hp[1])));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002152
2153 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2154 i++) {
2155 CHECK_MEM_ERROR(
2156 cm, cpi->mbgraph_stats[i].mb_stats,
Yaowu Xuf883b422016-08-30 14:01:10 -07002157 aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002158 }
2159
2160#if CONFIG_FP_MB_STATS
2161 cpi->use_fp_mb_stats = 0;
2162 if (cpi->use_fp_mb_stats) {
2163 // a place holder used to store the first pass mb stats in the first pass
2164 CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
Yaowu Xuf883b422016-08-30 14:01:10 -07002165 aom_calloc(cm->MBs * sizeof(uint8_t), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002166 } else {
2167 cpi->twopass.frame_mb_stats_buf = NULL;
2168 }
2169#endif
2170
2171 cpi->refresh_alt_ref_frame = 0;
2172 cpi->multi_arf_last_grp_enabled = 0;
2173
2174 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2175#if CONFIG_INTERNAL_STATS
2176 cpi->b_calculate_blockiness = 1;
2177 cpi->b_calculate_consistency = 1;
2178 cpi->total_inconsistency = 0;
2179 cpi->psnr.worst = 100.0;
2180 cpi->worst_ssim = 100.0;
2181
2182 cpi->count = 0;
2183 cpi->bytes = 0;
2184
2185 if (cpi->b_calculate_psnr) {
2186 cpi->total_sq_error = 0;
2187 cpi->total_samples = 0;
2188 cpi->tot_recode_hits = 0;
2189 cpi->summed_quality = 0;
2190 cpi->summed_weights = 0;
2191 }
2192
2193 cpi->fastssim.worst = 100.0;
2194 cpi->psnrhvs.worst = 100.0;
2195
2196 if (cpi->b_calculate_blockiness) {
2197 cpi->total_blockiness = 0;
2198 cpi->worst_blockiness = 0.0;
2199 }
2200
2201 if (cpi->b_calculate_consistency) {
2202 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
Yaowu Xuf883b422016-08-30 14:01:10 -07002203 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07002204 cpi->common.mi_rows * cpi->common.mi_cols));
2205 cpi->worst_consistency = 100.0;
2206 }
2207#endif
2208
2209 cpi->first_time_stamp_ever = INT64_MAX;
2210
2211#if CONFIG_REF_MV
2212 for (i = 0; i < NMV_CONTEXTS; ++i) {
2213 cpi->td.mb.nmvcost[i][0] = &cpi->nmv_costs[i][0][MV_MAX];
2214 cpi->td.mb.nmvcost[i][1] = &cpi->nmv_costs[i][1][MV_MAX];
2215 cpi->td.mb.nmvcost_hp[i][0] = &cpi->nmv_costs_hp[i][0][MV_MAX];
2216 cpi->td.mb.nmvcost_hp[i][1] = &cpi->nmv_costs_hp[i][1][MV_MAX];
2217 }
2218#else
2219 cal_nmvjointsadcost(cpi->td.mb.nmvjointsadcost);
2220 cpi->td.mb.nmvcost[0] = &cpi->nmvcosts[0][MV_MAX];
2221 cpi->td.mb.nmvcost[1] = &cpi->nmvcosts[1][MV_MAX];
2222 cpi->td.mb.nmvcost_hp[0] = &cpi->nmvcosts_hp[0][MV_MAX];
2223 cpi->td.mb.nmvcost_hp[1] = &cpi->nmvcosts_hp[1][MV_MAX];
2224#endif
2225 cpi->td.mb.nmvsadcost[0] = &cpi->nmvsadcosts[0][MV_MAX];
2226 cpi->td.mb.nmvsadcost[1] = &cpi->nmvsadcosts[1][MV_MAX];
2227 cal_nmvsadcosts(cpi->td.mb.nmvsadcost);
2228
2229 cpi->td.mb.nmvsadcost_hp[0] = &cpi->nmvsadcosts_hp[0][MV_MAX];
2230 cpi->td.mb.nmvsadcost_hp[1] = &cpi->nmvsadcosts_hp[1][MV_MAX];
2231 cal_nmvsadcosts_hp(cpi->td.mb.nmvsadcost_hp);
2232
2233#ifdef OUTPUT_YUV_SKINMAP
2234 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
2235#endif
2236#ifdef OUTPUT_YUV_REC
2237 yuv_rec_file = fopen("rec.yuv", "wb");
2238#endif
2239
2240#if 0
2241 framepsnr = fopen("framepsnr.stt", "a");
2242 kf_list = fopen("kf_list.stt", "w");
2243#endif
2244
2245 cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
2246
2247 if (oxcf->pass == 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002248 av1_init_first_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002249 } else if (oxcf->pass == 2) {
2250 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2251 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2252
2253#if CONFIG_FP_MB_STATS
2254 if (cpi->use_fp_mb_stats) {
2255 const size_t psz = cpi->common.MBs * sizeof(uint8_t);
2256 const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
2257
2258 cpi->twopass.firstpass_mb_stats.mb_stats_start =
2259 oxcf->firstpass_mb_stats_in.buf;
2260 cpi->twopass.firstpass_mb_stats.mb_stats_end =
2261 cpi->twopass.firstpass_mb_stats.mb_stats_start +
2262 (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
2263 }
2264#endif
2265
2266 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2267 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2268 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2269
Yaowu Xuf883b422016-08-30 14:01:10 -07002270 av1_init_second_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002271 }
2272
2273 init_upsampled_ref_frame_bufs(cpi);
2274
Yaowu Xuf883b422016-08-30 14:01:10 -07002275 av1_set_speed_features_framesize_independent(cpi);
2276 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002277
2278 // Allocate memory to store variances for a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07002279 CHECK_MEM_ERROR(cm, cpi->source_diff_var, aom_calloc(cm->MBs, sizeof(diff)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002280 cpi->source_var_thresh = 0;
2281 cpi->frames_till_next_var_check = 0;
2282
2283#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
2284 cpi->fn_ptr[BT].sdf = SDF; \
2285 cpi->fn_ptr[BT].sdaf = SDAF; \
2286 cpi->fn_ptr[BT].vf = VF; \
2287 cpi->fn_ptr[BT].svf = SVF; \
2288 cpi->fn_ptr[BT].svaf = SVAF; \
2289 cpi->fn_ptr[BT].sdx3f = SDX3F; \
2290 cpi->fn_ptr[BT].sdx8f = SDX8F; \
2291 cpi->fn_ptr[BT].sdx4df = SDX4DF;
2292
2293#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002294 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
2295 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
2296 aom_sad128x128x3, aom_sad128x128x8, aom_sad128x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002297
Yaowu Xuf883b422016-08-30 14:01:10 -07002298 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
2299 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64, NULL,
2300 NULL, aom_sad128x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002301
Yaowu Xuf883b422016-08-30 14:01:10 -07002302 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
2303 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128, NULL,
2304 NULL, aom_sad64x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002305#endif // CONFIG_EXT_PARTITION
2306
Yaowu Xuf883b422016-08-30 14:01:10 -07002307 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
2308 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16, NULL, NULL,
2309 aom_sad32x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002310
Yaowu Xuf883b422016-08-30 14:01:10 -07002311 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
2312 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32, NULL, NULL,
2313 aom_sad16x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002314
Yaowu Xuf883b422016-08-30 14:01:10 -07002315 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
2316 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32, NULL, NULL,
2317 aom_sad64x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002318
Yaowu Xuf883b422016-08-30 14:01:10 -07002319 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
2320 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64, NULL, NULL,
2321 aom_sad32x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002322
Yaowu Xuf883b422016-08-30 14:01:10 -07002323 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
2324 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
2325 aom_sad32x32x3, aom_sad32x32x8, aom_sad32x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002326
Yaowu Xuf883b422016-08-30 14:01:10 -07002327 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
2328 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
2329 aom_sad64x64x3, aom_sad64x64x8, aom_sad64x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002330
Yaowu Xuf883b422016-08-30 14:01:10 -07002331 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
2332 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
2333 aom_sad16x16x3, aom_sad16x16x8, aom_sad16x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002334
Yaowu Xuf883b422016-08-30 14:01:10 -07002335 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
2336 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8, aom_sad16x8x3,
2337 aom_sad16x8x8, aom_sad16x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002338
Yaowu Xuf883b422016-08-30 14:01:10 -07002339 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
2340 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16, aom_sad8x16x3,
2341 aom_sad8x16x8, aom_sad8x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002342
Yaowu Xuf883b422016-08-30 14:01:10 -07002343 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
2344 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x3,
2345 aom_sad8x8x8, aom_sad8x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002346
Yaowu Xuf883b422016-08-30 14:01:10 -07002347 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
2348 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, NULL,
2349 aom_sad8x4x8, aom_sad8x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002350
Yaowu Xuf883b422016-08-30 14:01:10 -07002351 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
2352 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, NULL,
2353 aom_sad4x8x8, aom_sad4x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002354
Yaowu Xuf883b422016-08-30 14:01:10 -07002355 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
2356 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x3,
2357 aom_sad4x4x8, aom_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002358
Yue Chencb60b182016-10-13 15:18:22 -07002359#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07002360#define OBFP(BT, OSDF, OVF, OSVF) \
2361 cpi->fn_ptr[BT].osdf = OSDF; \
2362 cpi->fn_ptr[BT].ovf = OVF; \
2363 cpi->fn_ptr[BT].osvf = OSVF;
2364
2365#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002366 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
2367 aom_obmc_sub_pixel_variance128x128)
2368 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
2369 aom_obmc_sub_pixel_variance128x64)
2370 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
2371 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002372#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002373 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
2374 aom_obmc_sub_pixel_variance64x64)
2375 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
2376 aom_obmc_sub_pixel_variance64x32)
2377 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
2378 aom_obmc_sub_pixel_variance32x64)
2379 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
2380 aom_obmc_sub_pixel_variance32x32)
2381 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
2382 aom_obmc_sub_pixel_variance32x16)
2383 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
2384 aom_obmc_sub_pixel_variance16x32)
2385 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
2386 aom_obmc_sub_pixel_variance16x16)
2387 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
2388 aom_obmc_sub_pixel_variance16x8)
2389 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
2390 aom_obmc_sub_pixel_variance8x16)
2391 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
2392 aom_obmc_sub_pixel_variance8x8)
2393 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
2394 aom_obmc_sub_pixel_variance4x8)
2395 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
2396 aom_obmc_sub_pixel_variance8x4)
2397 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
2398 aom_obmc_sub_pixel_variance4x4)
Yue Chencb60b182016-10-13 15:18:22 -07002399#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07002400
2401#if CONFIG_EXT_INTER
2402#define MBFP(BT, MSDF, MVF, MSVF) \
2403 cpi->fn_ptr[BT].msdf = MSDF; \
2404 cpi->fn_ptr[BT].mvf = MVF; \
2405 cpi->fn_ptr[BT].msvf = MSVF;
2406
2407#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002408 MBFP(BLOCK_128X128, aom_masked_sad128x128, aom_masked_variance128x128,
2409 aom_masked_sub_pixel_variance128x128)
2410 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_variance128x64,
2411 aom_masked_sub_pixel_variance128x64)
2412 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_variance64x128,
2413 aom_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002414#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002415 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_variance64x64,
2416 aom_masked_sub_pixel_variance64x64)
2417 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_variance64x32,
2418 aom_masked_sub_pixel_variance64x32)
2419 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_variance32x64,
2420 aom_masked_sub_pixel_variance32x64)
2421 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_variance32x32,
2422 aom_masked_sub_pixel_variance32x32)
2423 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_variance32x16,
2424 aom_masked_sub_pixel_variance32x16)
2425 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_variance16x32,
2426 aom_masked_sub_pixel_variance16x32)
2427 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_variance16x16,
2428 aom_masked_sub_pixel_variance16x16)
2429 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_variance16x8,
2430 aom_masked_sub_pixel_variance16x8)
2431 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_variance8x16,
2432 aom_masked_sub_pixel_variance8x16)
2433 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_variance8x8,
2434 aom_masked_sub_pixel_variance8x8)
2435 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_variance4x8,
2436 aom_masked_sub_pixel_variance4x8)
2437 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_variance8x4,
2438 aom_masked_sub_pixel_variance8x4)
2439 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_variance4x4,
2440 aom_masked_sub_pixel_variance4x4)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002441#endif // CONFIG_EXT_INTER
2442
Yaowu Xuf883b422016-08-30 14:01:10 -07002443#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002444 highbd_set_var_fns(cpi);
2445#endif
2446
Yaowu Xuf883b422016-08-30 14:01:10 -07002447 /* av1_init_quantizer() is first called here. Add check in
2448 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07002449 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07002450 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002451 */
Yaowu Xuf883b422016-08-30 14:01:10 -07002452 av1_init_quantizer(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002453#if CONFIG_AOM_QM
2454 aom_qm_init(cm);
2455#endif
2456
Yaowu Xuf883b422016-08-30 14:01:10 -07002457 av1_loop_filter_init(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002458#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -07002459 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002460#endif // CONFIG_LOOP_RESTORATION
2461
2462 cm->error.setjmp = 0;
2463
2464 return cpi;
2465}
2466
2467#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
2468
2469#define SNPRINT2(H, T, V) \
2470 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
2471
Yaowu Xuf883b422016-08-30 14:01:10 -07002472void av1_remove_compressor(AV1_COMP *cpi) {
2473 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002474 unsigned int i;
2475 int t;
2476
2477 if (!cpi) return;
2478
2479 cm = &cpi->common;
2480 if (cm->current_video_frame > 0) {
2481#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07002482 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002483
2484 if (cpi->oxcf.pass != 1) {
2485 char headings[512] = { 0 };
2486 char results[512] = { 0 };
2487 FILE *f = fopen("opsnr.stt", "a");
2488 double time_encoded =
2489 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
2490 10000000.000;
2491 double total_encode_time =
2492 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
2493 const double dr =
2494 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
2495 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
2496 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
2497 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
2498
2499 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002500 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002501 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
2502 const double total_ssim =
2503 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2504 snprintf(headings, sizeof(headings),
2505 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07002506 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Yaowu Xuc27fc142016-08-22 16:08:15 -07002507 "WstPsnr\tWstSsim\tWstFast\tWstHVS");
2508 snprintf(results, sizeof(results),
2509 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2510 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2511 "%7.3f\t%7.3f\t%7.3f\t%7.3f",
2512 dr, cpi->psnr.stat[ALL] / cpi->count, total_psnr,
2513 cpi->psnr.stat[ALL] / cpi->count, total_psnr, total_ssim,
2514 total_ssim, cpi->fastssim.stat[ALL] / cpi->count,
2515 cpi->psnrhvs.stat[ALL] / cpi->count, cpi->psnr.worst,
2516 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst);
2517
2518 if (cpi->b_calculate_blockiness) {
2519 SNPRINT(headings, "\t Block\tWstBlck");
2520 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
2521 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
2522 }
2523
2524 if (cpi->b_calculate_consistency) {
2525 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07002526 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002527 (double)cpi->total_inconsistency);
2528
2529 SNPRINT(headings, "\tConsist\tWstCons");
2530 SNPRINT2(results, "\t%7.3f", consistency);
2531 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
2532 }
Sarah Parkerf97b7862016-08-25 17:42:57 -07002533 fprintf(f, "%s\t Time\tRcErr\tAbsErr\n", headings);
2534 fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002535 rate_err, fabs(rate_err));
2536 }
2537
2538 fclose(f);
2539 }
2540
2541#endif
2542
2543#if 0
2544 {
2545 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2546 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
2547 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
2548 cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
2549 cpi->time_compress_data / 1000,
2550 (cpi->time_receive_data + cpi->time_compress_data) / 1000);
2551 }
2552#endif
2553 }
2554
2555 for (t = 0; t < cpi->num_workers; ++t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002556 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002557 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
2558
2559 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07002560 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002561
2562 // Deallocate allocated thread data.
2563 if (t < cpi->num_workers - 1) {
2564 if (cpi->common.allow_screen_content_tools)
Yaowu Xuf883b422016-08-30 14:01:10 -07002565 aom_free(thread_data->td->mb.palette_buffer);
2566 aom_free(thread_data->td->counts);
2567 av1_free_pc_tree(thread_data->td);
2568 av1_free_var_tree(thread_data->td);
2569 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002570 }
2571 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002572 aom_free(cpi->tile_thr_data);
2573 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002574
Yaowu Xuf883b422016-08-30 14:01:10 -07002575 if (cpi->num_workers > 1) av1_loop_filter_dealloc(&cpi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002576
2577 dealloc_compressor_data(cpi);
2578
2579 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
2580 ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002581 aom_free(cpi->mbgraph_stats[i].mb_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002582 }
2583
2584#if CONFIG_FP_MB_STATS
2585 if (cpi->use_fp_mb_stats) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002586 aom_free(cpi->twopass.frame_mb_stats_buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002587 cpi->twopass.frame_mb_stats_buf = NULL;
2588 }
2589#endif
2590
Yaowu Xuf883b422016-08-30 14:01:10 -07002591 av1_remove_common(cm);
2592 av1_free_ref_frame_buffers(cm->buffer_pool);
2593 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002594
2595#ifdef OUTPUT_YUV_SKINMAP
2596 fclose(yuv_skinmap_file);
2597#endif
2598#ifdef OUTPUT_YUV_REC
2599 fclose(yuv_rec_file);
2600#endif
2601
2602#if 0
2603
2604 if (keyfile)
2605 fclose(keyfile);
2606
2607 if (framepsnr)
2608 fclose(framepsnr);
2609
2610 if (kf_list)
2611 fclose(kf_list);
2612
2613#endif
2614}
2615
Yaowu Xuf883b422016-08-30 14:01:10 -07002616static void generate_psnr_packet(AV1_COMP *cpi) {
2617 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002618 int i;
2619 PSNR_STATS psnr;
Yaowu Xuf883b422016-08-30 14:01:10 -07002620#if CONFIG_AOM_HIGHBITDEPTH
2621 aom_calc_highbd_psnr(cpi->Source, cpi->common.frame_to_show, &psnr,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002622 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
2623#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002624 aom_calc_psnr(cpi->Source, cpi->common.frame_to_show, &psnr);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002625#endif
2626
2627 for (i = 0; i < 4; ++i) {
2628 pkt.data.psnr.samples[i] = psnr.samples[i];
2629 pkt.data.psnr.sse[i] = psnr.sse[i];
2630 pkt.data.psnr.psnr[i] = psnr.psnr[i];
2631 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002632 pkt.kind = AOM_CODEC_PSNR_PKT;
2633 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002634}
2635
Yaowu Xuf883b422016-08-30 14:01:10 -07002636int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002637 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
2638
2639 cpi->ref_frame_flags = ref_frame_flags;
2640 return 0;
2641}
2642
Yaowu Xuf883b422016-08-30 14:01:10 -07002643void av1_update_reference(AV1_COMP *cpi, int ref_frame_flags) {
2644 cpi->ext_refresh_golden_frame = (ref_frame_flags & AOM_GOLD_FLAG) != 0;
2645 cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & AOM_ALT_FLAG) != 0;
2646 cpi->ext_refresh_last_frame = (ref_frame_flags & AOM_LAST_FLAG) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002647 cpi->ext_refresh_frame_flags_pending = 1;
2648}
2649
Yaowu Xuf883b422016-08-30 14:01:10 -07002650static YV12_BUFFER_CONFIG *get_av1_ref_frame_buffer(
2651 AV1_COMP *cpi, AOM_REFFRAME ref_frame_flag) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002652 MV_REFERENCE_FRAME ref_frame = NONE;
Yaowu Xuf883b422016-08-30 14:01:10 -07002653 if (ref_frame_flag == AOM_LAST_FLAG) ref_frame = LAST_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002654#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07002655 else if (ref_frame_flag == AOM_LAST2_FLAG)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002656 ref_frame = LAST2_FRAME;
Yaowu Xuf883b422016-08-30 14:01:10 -07002657 else if (ref_frame_flag == AOM_LAST3_FLAG)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002658 ref_frame = LAST3_FRAME;
2659#endif // CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07002660 else if (ref_frame_flag == AOM_GOLD_FLAG)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002661 ref_frame = GOLDEN_FRAME;
2662#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07002663 else if (ref_frame_flag == AOM_BWD_FLAG)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002664 ref_frame = BWDREF_FRAME;
2665#endif // CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07002666 else if (ref_frame_flag == AOM_ALT_FLAG)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002667 ref_frame = ALTREF_FRAME;
2668
2669 return ref_frame == NONE ? NULL : get_ref_frame_buffer(cpi, ref_frame);
2670}
2671
Yaowu Xuf883b422016-08-30 14:01:10 -07002672int av1_copy_reference_enc(AV1_COMP *cpi, AOM_REFFRAME ref_frame_flag,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002673 YV12_BUFFER_CONFIG *sd) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002674 YV12_BUFFER_CONFIG *cfg = get_av1_ref_frame_buffer(cpi, ref_frame_flag);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002675 if (cfg) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002676 aom_yv12_copy_frame(cfg, sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002677 return 0;
2678 } else {
2679 return -1;
2680 }
2681}
2682
Yaowu Xuf883b422016-08-30 14:01:10 -07002683int av1_set_reference_enc(AV1_COMP *cpi, AOM_REFFRAME ref_frame_flag,
2684 YV12_BUFFER_CONFIG *sd) {
2685 YV12_BUFFER_CONFIG *cfg = get_av1_ref_frame_buffer(cpi, ref_frame_flag);
2686 if (cfg) {
2687 aom_yv12_copy_frame(sd, cfg);
2688 return 0;
2689 } else {
2690 return -1;
2691 }
2692}
2693
2694int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002695 cpi->ext_refresh_frame_context = update;
2696 cpi->ext_refresh_frame_context_pending = 1;
2697 return 0;
2698}
2699
2700#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
2701// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
2702// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
2703// not denoise the UV channels at this time. If ever we implement UV channel
2704// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07002705void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002706 uint8_t *src = s->y_buffer;
2707 int h = s->y_height;
2708
2709 do {
2710 fwrite(src, s->y_width, 1, f);
2711 src += s->y_stride;
2712 } while (--h);
2713
2714 src = s->u_buffer;
2715 h = s->uv_height;
2716
2717 do {
2718 fwrite(src, s->uv_width, 1, f);
2719 src += s->uv_stride;
2720 } while (--h);
2721
2722 src = s->v_buffer;
2723 h = s->uv_height;
2724
2725 do {
2726 fwrite(src, s->uv_width, 1, f);
2727 src += s->uv_stride;
2728 } while (--h);
2729}
2730#endif
2731
2732#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07002733static void check_show_existing_frame(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002734 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Yaowu Xuf883b422016-08-30 14:01:10 -07002735 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002736 const FRAME_UPDATE_TYPE next_frame_update_type =
2737 gf_group->update_type[gf_group->index];
2738 const int which_arf = gf_group->arf_update_idx[gf_group->index];
Zoe Liu5fca7242016-10-10 17:18:57 -07002739
2740 if (cm->show_existing_frame == 1) {
2741 cm->show_existing_frame = 0;
2742 } else if (cpi->rc.is_last_bipred_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002743 // NOTE(zoeliu): If the current frame is a last bi-predictive frame, it is
2744 // needed next to show the BWDREF_FRAME, which is pointed by
2745 // the last_fb_idxes[0] after reference frame buffer update
2746 cpi->rc.is_last_bipred_frame = 0;
2747 cm->show_existing_frame = 1;
2748 cpi->existing_fb_idx_to_show = cpi->lst_fb_idxes[0];
2749 } else if (cpi->is_arf_filter_off[which_arf] &&
2750 (next_frame_update_type == OVERLAY_UPDATE ||
2751 next_frame_update_type == INTNL_OVERLAY_UPDATE)) {
2752 // Other parameters related to OVERLAY_UPDATE will be taken care of
Yaowu Xuf883b422016-08-30 14:01:10 -07002753 // in av1_rc_get_second_pass_params(cpi)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002754 cm->show_existing_frame = 1;
2755 cpi->rc.is_src_frame_alt_ref = 1;
2756 cpi->existing_fb_idx_to_show = cpi->alt_fb_idx;
2757 cpi->is_arf_filter_off[which_arf] = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002758 }
2759 cpi->rc.is_src_frame_ext_arf = 0;
2760}
2761#endif // CONFIG_EXT_REFS
2762
2763#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07002764void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002765 uint8_t *src = s->y_buffer;
2766 int h = cm->height;
2767
Yaowu Xuf883b422016-08-30 14:01:10 -07002768#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002769 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
2770 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
2771
2772 do {
2773 fwrite(src16, s->y_width, 2, yuv_rec_file);
2774 src16 += s->y_stride;
2775 } while (--h);
2776
2777 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
2778 h = s->uv_height;
2779
2780 do {
2781 fwrite(src16, s->uv_width, 2, yuv_rec_file);
2782 src16 += s->uv_stride;
2783 } while (--h);
2784
2785 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
2786 h = s->uv_height;
2787
2788 do {
2789 fwrite(src16, s->uv_width, 2, yuv_rec_file);
2790 src16 += s->uv_stride;
2791 } while (--h);
2792
2793 fflush(yuv_rec_file);
2794 return;
2795 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002796#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002797
2798 do {
2799 fwrite(src, s->y_width, 1, yuv_rec_file);
2800 src += s->y_stride;
2801 } while (--h);
2802
2803 src = s->u_buffer;
2804 h = s->uv_height;
2805
2806 do {
2807 fwrite(src, s->uv_width, 1, yuv_rec_file);
2808 src += s->uv_stride;
2809 } while (--h);
2810
2811 src = s->v_buffer;
2812 h = s->uv_height;
2813
2814 do {
2815 fwrite(src, s->uv_width, 1, yuv_rec_file);
2816 src += s->uv_stride;
2817 } while (--h);
2818
2819 fflush(yuv_rec_file);
2820}
2821#endif // OUTPUT_YUV_REC
2822
Yaowu Xuf883b422016-08-30 14:01:10 -07002823#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002824static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
2825 YV12_BUFFER_CONFIG *dst,
2826 int bd) {
2827#else
2828static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
2829 YV12_BUFFER_CONFIG *dst) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002830#endif // CONFIG_AOM_HIGHBITDEPTH
2831 // TODO(dkovalev): replace YV12_BUFFER_CONFIG with aom_image_t
Yaowu Xuc27fc142016-08-22 16:08:15 -07002832 int i;
2833 const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
2834 src->v_buffer };
2835 const int src_strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
2836 const int src_widths[3] = { src->y_crop_width, src->uv_crop_width,
2837 src->uv_crop_width };
2838 const int src_heights[3] = { src->y_crop_height, src->uv_crop_height,
2839 src->uv_crop_height };
2840 uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
2841 const int dst_strides[3] = { dst->y_stride, dst->uv_stride, dst->uv_stride };
2842 const int dst_widths[3] = { dst->y_crop_width, dst->uv_crop_width,
2843 dst->uv_crop_width };
2844 const int dst_heights[3] = { dst->y_crop_height, dst->uv_crop_height,
2845 dst->uv_crop_height };
2846
2847 for (i = 0; i < MAX_MB_PLANE; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002848#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002849 if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002850 av1_highbd_resize_plane(srcs[i], src_heights[i], src_widths[i],
2851 src_strides[i], dsts[i], dst_heights[i],
2852 dst_widths[i], dst_strides[i], bd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002853 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002854 av1_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
2855 dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002856 }
2857#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002858 av1_resize_plane(srcs[i], src_heights[i], src_widths[i], src_strides[i],
2859 dsts[i], dst_heights[i], dst_widths[i], dst_strides[i]);
2860#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002861 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002862 aom_extend_frame_borders(dst);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002863}
2864
Yaowu Xuf883b422016-08-30 14:01:10 -07002865#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002866static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
2867 YV12_BUFFER_CONFIG *dst, int planes,
2868 int bd) {
2869#else
2870static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
2871 YV12_BUFFER_CONFIG *dst, int planes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002872#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002873 const int src_w = src->y_crop_width;
2874 const int src_h = src->y_crop_height;
2875 const int dst_w = dst->y_crop_width;
2876 const int dst_h = dst->y_crop_height;
2877 const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
2878 src->v_buffer };
2879 const int src_strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
2880 uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
2881 const int dst_strides[3] = { dst->y_stride, dst->uv_stride, dst->uv_stride };
2882 const InterpFilterParams interp_filter_params =
Yaowu Xuf883b422016-08-30 14:01:10 -07002883 av1_get_interp_filter_params(EIGHTTAP_REGULAR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002884 const int16_t *kernel = interp_filter_params.filter_ptr;
2885 const int taps = interp_filter_params.taps;
2886 int x, y, i;
2887
2888 for (y = 0; y < dst_h; y += 16) {
2889 for (x = 0; x < dst_w; x += 16) {
2890 for (i = 0; i < planes; ++i) {
2891 const int factor = (i == 0 || i == 3 ? 1 : 2);
2892 const int x_q4 = x * (16 / factor) * src_w / dst_w;
2893 const int y_q4 = y * (16 / factor) * src_h / dst_h;
2894 const int src_stride = src_strides[i];
2895 const int dst_stride = dst_strides[i];
2896 const uint8_t *src_ptr = srcs[i] +
2897 (y / factor) * src_h / dst_h * src_stride +
2898 (x / factor) * src_w / dst_w;
2899 uint8_t *dst_ptr = dsts[i] + (y / factor) * dst_stride + (x / factor);
2900
Yaowu Xuf883b422016-08-30 14:01:10 -07002901#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002902 if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002903 aom_highbd_convolve8(src_ptr, src_stride, dst_ptr, dst_stride,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002904 &kernel[(x_q4 & 0xf) * taps], 16 * src_w / dst_w,
2905 &kernel[(y_q4 & 0xf) * taps], 16 * src_h / dst_h,
2906 16 / factor, 16 / factor, bd);
2907 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002908 aom_scaled_2d(src_ptr, src_stride, dst_ptr, dst_stride,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002909 &kernel[(x_q4 & 0xf) * taps], 16 * src_w / dst_w,
2910 &kernel[(y_q4 & 0xf) * taps], 16 * src_h / dst_h,
2911 16 / factor, 16 / factor);
2912 }
2913#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002914 aom_scaled_2d(src_ptr, src_stride, dst_ptr, dst_stride,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002915 &kernel[(x_q4 & 0xf) * taps], 16 * src_w / dst_w,
2916 &kernel[(y_q4 & 0xf) * taps], 16 * src_h / dst_h,
2917 16 / factor, 16 / factor);
Yaowu Xuf883b422016-08-30 14:01:10 -07002918#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002919 }
2920 }
2921 }
2922
2923 if (planes == 1)
Yaowu Xuf883b422016-08-30 14:01:10 -07002924 aom_extend_frame_borders_y(dst);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002925 else
Yaowu Xuf883b422016-08-30 14:01:10 -07002926 aom_extend_frame_borders(dst);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002927}
2928
Yaowu Xuf883b422016-08-30 14:01:10 -07002929static int scale_down(AV1_COMP *cpi, int q) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002930 RATE_CONTROL *const rc = &cpi->rc;
2931 GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2932 int scale = 0;
2933 assert(frame_is_kf_gf_arf(cpi));
2934
2935 if (rc->frame_size_selector == UNSCALED &&
2936 q >= rc->rf_level_maxq[gf_group->rf_level[gf_group->index]]) {
2937 const int max_size_thresh =
2938 (int)(rate_thresh_mult[SCALE_STEP1] *
Yaowu Xuf883b422016-08-30 14:01:10 -07002939 AOMMAX(rc->this_frame_target, rc->avg_frame_bandwidth));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002940 scale = rc->projected_frame_size > max_size_thresh ? 1 : 0;
2941 }
2942 return scale;
2943}
2944
2945// Function to test for conditions that indicate we should loop
2946// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07002947static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
2948 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002949 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07002950 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002951 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
2952 int force_recode = 0;
2953
2954 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
2955 (cpi->sf.recode_loop == ALLOW_RECODE) ||
2956 (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
2957 if (frame_is_kfgfarf && (oxcf->resize_mode == RESIZE_DYNAMIC) &&
2958 scale_down(cpi, q)) {
2959 // Code this group at a lower resolution.
2960 cpi->resize_pending = 1;
2961 return 1;
2962 }
2963
2964 // TODO(agrange) high_limit could be greater than the scale-down threshold.
2965 if ((rc->projected_frame_size > high_limit && q < maxq) ||
2966 (rc->projected_frame_size < low_limit && q > minq)) {
2967 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07002968 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002969 // Deal with frame undershoot and whether or not we are
2970 // below the automatically set cq level.
2971 if (q > oxcf->cq_level &&
2972 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
2973 force_recode = 1;
2974 }
2975 }
2976 }
2977 return force_recode;
2978}
2979
2980static INLINE int get_free_upsampled_ref_buf(EncRefCntBuffer *ubufs) {
2981 int i;
2982
2983 for (i = 0; i < (REF_FRAMES + 1); i++) {
2984 if (!ubufs[i].ref_count) {
2985 return i;
2986 }
2987 }
2988 return INVALID_IDX;
2989}
2990
2991// Up-sample 1 reference frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07002992static INLINE int upsample_ref_frame(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002993 const YV12_BUFFER_CONFIG *const ref) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002994 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002995 EncRefCntBuffer *ubufs = cpi->upsampled_ref_bufs;
2996 int new_uidx = get_free_upsampled_ref_buf(ubufs);
2997
2998 if (new_uidx == INVALID_IDX) {
2999 return INVALID_IDX;
3000 } else {
3001 YV12_BUFFER_CONFIG *upsampled_ref = &ubufs[new_uidx].buf;
3002
3003 // Can allocate buffer for Y plane only.
3004 if (upsampled_ref->buffer_alloc_sz < (ref->buffer_alloc_sz << 6))
Yaowu Xuf883b422016-08-30 14:01:10 -07003005 if (aom_realloc_frame_buffer(upsampled_ref, (cm->width << 3),
Yaowu Xuc27fc142016-08-22 16:08:15 -07003006 (cm->height << 3), cm->subsampling_x,
3007 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07003008#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003009 cm->use_highbitdepth,
3010#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003011 (AOM_BORDER_IN_PIXELS << 3),
Yaowu Xuc27fc142016-08-22 16:08:15 -07003012 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003013 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003014 "Failed to allocate up-sampled frame buffer");
3015
3016// Currently, only Y plane is up-sampled, U, V are not used.
Yaowu Xuf883b422016-08-30 14:01:10 -07003017#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003018 scale_and_extend_frame(ref, upsampled_ref, 1, (int)cm->bit_depth);
3019#else
3020 scale_and_extend_frame(ref, upsampled_ref, 1);
3021#endif
3022 return new_uidx;
3023 }
3024}
3025
3026#define DUMP_REF_FRAME_IMAGES 0
3027
3028#if DUMP_REF_FRAME_IMAGES == 1
Yaowu Xuf883b422016-08-30 14:01:10 -07003029static int dump_one_image(AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003030 const YV12_BUFFER_CONFIG *const ref_buf,
3031 char *file_name) {
3032 int h;
3033 FILE *f_ref = NULL;
3034
3035 if (ref_buf == NULL) {
3036 printf("Frame data buffer is NULL.\n");
Yaowu Xuf883b422016-08-30 14:01:10 -07003037 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003038 }
3039
3040 if ((f_ref = fopen(file_name, "wb")) == NULL) {
3041 printf("Unable to open file %s to write.\n", file_name);
Yaowu Xuf883b422016-08-30 14:01:10 -07003042 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003043 }
3044
3045 // --- Y ---
3046 for (h = 0; h < cm->height; ++h) {
3047 fwrite(&ref_buf->y_buffer[h * ref_buf->y_stride], 1, cm->width, f_ref);
3048 }
3049 // --- U ---
3050 for (h = 0; h < (cm->height >> 1); ++h) {
3051 fwrite(&ref_buf->u_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3052 f_ref);
3053 }
3054 // --- V ---
3055 for (h = 0; h < (cm->height >> 1); ++h) {
3056 fwrite(&ref_buf->v_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3057 f_ref);
3058 }
3059
3060 fclose(f_ref);
3061
Yaowu Xuf883b422016-08-30 14:01:10 -07003062 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003063}
3064
Yaowu Xuf883b422016-08-30 14:01:10 -07003065static void dump_ref_frame_images(AV1_COMP *cpi) {
3066 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003067 MV_REFERENCE_FRAME ref_frame;
3068
3069 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3070 char file_name[256] = "";
3071 snprintf(file_name, sizeof(file_name), "/tmp/enc_F%d_ref_%d.yuv",
3072 cm->current_video_frame, ref_frame);
3073 dump_one_image(cm, get_ref_frame_buffer(cpi, ref_frame), file_name);
3074 }
3075}
3076#endif // DUMP_REF_FRAME_IMAGES == 1
3077
3078#if CONFIG_EXT_REFS
3079// This function is used to shift the virtual indices of last reference frames
3080// as follows:
3081// LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3082// when the LAST_FRAME is updated.
Yaowu Xuf883b422016-08-30 14:01:10 -07003083static INLINE void shift_last_ref_frames(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003084 int ref_frame;
3085 for (ref_frame = LAST_REF_FRAMES - 1; ref_frame > 0; --ref_frame) {
3086 cpi->lst_fb_idxes[ref_frame] = cpi->lst_fb_idxes[ref_frame - 1];
3087
3088 // [0] is allocated to the current coded frame. The statistics for the
Zoe Liuf0e46692016-10-12 12:31:43 -07003089 // reference frames start at [LAST_FRAME], i.e. [1].
Yaowu Xuc27fc142016-08-22 16:08:15 -07003090 if (!cpi->rc.is_src_frame_alt_ref) {
Zoe Liuf0e46692016-10-12 12:31:43 -07003091 memcpy(cpi->interp_filter_selected[ref_frame + LAST_FRAME],
3092 cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME],
3093 sizeof(cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003094 }
3095 }
3096}
Zoe Liuf0e46692016-10-12 12:31:43 -07003097#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003098
Yaowu Xuf883b422016-08-30 14:01:10 -07003099void av1_update_reference_frames(AV1_COMP *cpi) {
3100 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003101 BufferPool *const pool = cm->buffer_pool;
3102 const int use_upsampled_ref = cpi->sf.use_upsampled_references;
3103 int new_uidx = 0;
3104
3105 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3106 // for the purpose to verify no mismatch between encoder and decoder.
3107 if (cm->show_frame) cpi->last_show_frame_buf_idx = cm->new_fb_idx;
3108
3109 if (use_upsampled_ref) {
3110#if CONFIG_EXT_REFS
3111 if (cm->show_existing_frame) {
3112 new_uidx = cpi->upsampled_ref_idx[cpi->existing_fb_idx_to_show];
3113 // TODO(zoeliu): Once following is confirmed, remove it.
3114 assert(cpi->upsampled_ref_bufs[new_uidx].ref_count > 0);
3115 } else {
3116#endif // CONFIG_EXT_REFS
3117 // Up-sample the current encoded frame.
3118 RefCntBuffer *bufs = pool->frame_bufs;
3119 const YV12_BUFFER_CONFIG *const ref = &bufs[cm->new_fb_idx].buf;
3120
3121 new_uidx = upsample_ref_frame(cpi, ref);
3122#if CONFIG_EXT_REFS
3123 assert(new_uidx != INVALID_IDX);
3124 }
3125#endif // CONFIG_EXT_REFS
3126 }
3127 // At this point the new frame has been encoded.
3128 // If any buffer copy / swapping is signaled it should be done here.
3129 if (cm->frame_type == KEY_FRAME) {
3130 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3131 cm->new_fb_idx);
3132#if CONFIG_EXT_REFS
3133 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->bwd_fb_idx],
3134 cm->new_fb_idx);
3135#endif // CONFIG_EXT_REFS
3136 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3137 cm->new_fb_idx);
3138
3139 if (use_upsampled_ref) {
3140 uref_cnt_fb(cpi->upsampled_ref_bufs,
3141 &cpi->upsampled_ref_idx[cpi->gld_fb_idx], new_uidx);
3142#if CONFIG_EXT_REFS
3143 uref_cnt_fb(cpi->upsampled_ref_bufs,
3144 &cpi->upsampled_ref_idx[cpi->bwd_fb_idx], new_uidx);
3145#endif // CONFIG_EXT_REFS
3146 uref_cnt_fb(cpi->upsampled_ref_bufs,
3147 &cpi->upsampled_ref_idx[cpi->alt_fb_idx], new_uidx);
3148 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003149 } else if (av1_preserve_existing_gf(cpi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003150 // We have decided to preserve the previously existing golden frame as our
3151 // new ARF frame. However, in the short term in function
Yaowu Xuf883b422016-08-30 14:01:10 -07003152 // av1_bitstream.c::get_refresh_mask() we left it in the GF slot and, if
Yaowu Xuc27fc142016-08-22 16:08:15 -07003153 // we're updating the GF with the current decoded frame, we save it to the
3154 // ARF slot instead.
3155 // We now have to update the ARF with the current frame and swap gld_fb_idx
3156 // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
3157 // slot and, if we're updating the GF, the current frame becomes the new GF.
3158 int tmp;
3159
3160 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3161 cm->new_fb_idx);
3162 if (use_upsampled_ref)
3163 uref_cnt_fb(cpi->upsampled_ref_bufs,
3164 &cpi->upsampled_ref_idx[cpi->alt_fb_idx], new_uidx);
3165
3166 tmp = cpi->alt_fb_idx;
3167 cpi->alt_fb_idx = cpi->gld_fb_idx;
3168 cpi->gld_fb_idx = tmp;
3169
3170#if CONFIG_EXT_REFS
3171 // We need to modify the mapping accordingly
3172 cpi->arf_map[0] = cpi->alt_fb_idx;
3173#endif
3174// TODO(zoeliu): Do we need to copy cpi->interp_filter_selected[0] over to
3175// cpi->interp_filter_selected[GOLDEN_FRAME]?
3176#if CONFIG_EXT_REFS
3177 } else if (cpi->rc.is_last_bipred_frame) {
3178 // Refresh the LAST_FRAME with the BWDREF_FRAME and retire the LAST3_FRAME
3179 // by updating the virtual indices. Note that the frame BWDREF_FRAME points
3180 // to now should be retired, and it should not be used before refreshed.
3181 int tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
3182
3183 shift_last_ref_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003184 cpi->lst_fb_idxes[0] = cpi->bwd_fb_idx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003185 cpi->bwd_fb_idx = tmp;
Zoe Liuf0e46692016-10-12 12:31:43 -07003186
3187 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3188 cpi->interp_filter_selected[BWDREF_FRAME],
3189 sizeof(cpi->interp_filter_selected[BWDREF_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003190 } else if (cpi->rc.is_src_frame_ext_arf && cm->show_existing_frame) {
3191 // Deal with the special case for showing existing internal ALTREF_FRAME
3192 // Refresh the LAST_FRAME with the ALTREF_FRAME and retire the LAST3_FRAME
3193 // by updating the virtual indices.
3194 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3195 int which_arf = gf_group->arf_ref_idx[gf_group->index];
3196 int tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
3197
3198 shift_last_ref_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003199 cpi->lst_fb_idxes[0] = cpi->alt_fb_idx;
Zoe Liuf0e46692016-10-12 12:31:43 -07003200 cpi->alt_fb_idx = tmp;
3201
3202 // We need to modify the mapping accordingly
3203 cpi->arf_map[which_arf] = cpi->alt_fb_idx;
3204
Yaowu Xuc27fc142016-08-22 16:08:15 -07003205 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3206 cpi->interp_filter_selected[ALTREF_FRAME + which_arf],
3207 sizeof(cpi->interp_filter_selected[ALTREF_FRAME + which_arf]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003208#endif // CONFIG_EXT_REFS
3209 } else { /* For non key/golden frames */
3210 if (cpi->refresh_alt_ref_frame) {
3211 int arf_idx = cpi->alt_fb_idx;
3212 int which_arf = 0;
3213#if CONFIG_EXT_REFS
3214 if (cpi->oxcf.pass == 2) {
3215 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3216 which_arf = gf_group->arf_update_idx[gf_group->index];
3217 arf_idx = cpi->arf_map[which_arf];
3218 }
3219#else
3220 if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
3221 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3222 arf_idx = gf_group->arf_update_idx[gf_group->index];
3223 }
3224#endif // CONFIG_EXT_REFS
3225 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
3226 if (use_upsampled_ref)
3227 uref_cnt_fb(cpi->upsampled_ref_bufs, &cpi->upsampled_ref_idx[arf_idx],
3228 new_uidx);
3229
3230 memcpy(cpi->interp_filter_selected[ALTREF_FRAME + which_arf],
3231 cpi->interp_filter_selected[0],
3232 sizeof(cpi->interp_filter_selected[0]));
3233 }
3234
3235 if (cpi->refresh_golden_frame) {
3236 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3237 cm->new_fb_idx);
3238 if (use_upsampled_ref)
3239 uref_cnt_fb(cpi->upsampled_ref_bufs,
3240 &cpi->upsampled_ref_idx[cpi->gld_fb_idx], new_uidx);
3241
Zoe Liuf0e46692016-10-12 12:31:43 -07003242#if !CONFIG_EXT_REFS
3243 if (!cpi->rc.is_src_frame_alt_ref)
3244#endif // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003245 memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
3246 cpi->interp_filter_selected[0],
3247 sizeof(cpi->interp_filter_selected[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003248 }
3249
3250#if CONFIG_EXT_REFS
3251 if (cpi->refresh_bwd_ref_frame) {
3252 if (cpi->rc.is_bwd_ref_frame && cpi->num_extra_arfs) {
3253 // We have swapped the virtual indices to allow bwd_ref_frame to use
3254 // ALT0 as reference frame. We need to swap them back.
3255 // NOTE: The ALT_REFs' are indexed reversely, and ALT0 refers to the
3256 // farthest ALT_REF from the first frame in the gf group.
3257 int tmp = cpi->arf_map[0];
3258 cpi->arf_map[0] = cpi->alt_fb_idx;
3259 cpi->alt_fb_idx = cpi->bwd_fb_idx;
3260 cpi->bwd_fb_idx = tmp;
3261 }
Zoe Liuf0e46692016-10-12 12:31:43 -07003262
Yaowu Xuc27fc142016-08-22 16:08:15 -07003263 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->bwd_fb_idx],
3264 cm->new_fb_idx);
3265 if (use_upsampled_ref)
3266 uref_cnt_fb(cpi->upsampled_ref_bufs,
3267 &cpi->upsampled_ref_idx[cpi->bwd_fb_idx], new_uidx);
3268
3269 memcpy(cpi->interp_filter_selected[BWDREF_FRAME],
3270 cpi->interp_filter_selected[0],
3271 sizeof(cpi->interp_filter_selected[0]));
3272 }
3273#endif // CONFIG_EXT_REFS
3274 }
3275
3276 if (cpi->refresh_last_frame) {
3277#if CONFIG_EXT_REFS
3278 // NOTE(zoeliu): We have two layers of mapping (1) from the per-frame
3279 // reference to the reference frame buffer virtual index; and then (2) from
3280 // the virtual index to the reference frame buffer physical index:
3281 //
3282 // LAST_FRAME, ..., LAST3_FRAME, ..., ALTREF_FRAME
3283 // | | |
3284 // v v v
3285 // lst_fb_idxes[0], ..., lst_fb_idxes[2], ..., alt_fb_idx
3286 // | | |
3287 // v v v
3288 // ref_frame_map[], ..., ref_frame_map[], ..., ref_frame_map[]
3289 //
3290 // When refresh_last_frame is set, it is intended to retire LAST3_FRAME,
3291 // have the other 2 LAST reference frames shifted as follows:
3292 // LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3293 // , and then have LAST_FRAME refreshed by the newly coded frame.
3294 //
3295 // To fulfill it, the decoder will be notified to execute following 2 steps:
3296 //
3297 // (a) To change ref_frame_map[] and have the virtual index of LAST3_FRAME
3298 // to point to the newly coded frame, i.e.
3299 // ref_frame_map[lst_fb_idexes[2]] => new_fb_idx;
3300 //
3301 // (b) To change the 1st layer mapping to have LAST_FRAME mapped to the
3302 // original virtual index of LAST3_FRAME and have the other mappings
3303 // shifted as follows:
3304 // LAST_FRAME, LAST2_FRAME, LAST3_FRAME
3305 // | | |
3306 // v v v
3307 // lst_fb_idxes[2], lst_fb_idxes[0], lst_fb_idxes[1]
3308 int ref_frame;
Zoe Liu5fca7242016-10-10 17:18:57 -07003309
Yaowu Xuc27fc142016-08-22 16:08:15 -07003310 if (cpi->rc.is_bwd_ref_frame && cpi->num_extra_arfs) {
3311 // We have swapped the virtual indices to use ALT0 as BWD_REF
3312 // and we need to swap them back.
3313 int tmp = cpi->arf_map[0];
3314 cpi->arf_map[0] = cpi->alt_fb_idx;
3315 cpi->alt_fb_idx = cpi->bwd_fb_idx;
3316 cpi->bwd_fb_idx = tmp;
3317 }
Zoe Liu5fca7242016-10-10 17:18:57 -07003318
Yaowu Xuc27fc142016-08-22 16:08:15 -07003319 if (cm->frame_type == KEY_FRAME) {
3320 for (ref_frame = 0; ref_frame < LAST_REF_FRAMES; ++ref_frame) {
3321 ref_cnt_fb(pool->frame_bufs,
3322 &cm->ref_frame_map[cpi->lst_fb_idxes[ref_frame]],
3323 cm->new_fb_idx);
3324
3325 if (use_upsampled_ref)
3326 uref_cnt_fb(cpi->upsampled_ref_bufs,
3327 &cpi->upsampled_ref_idx[cpi->lst_fb_idxes[ref_frame]],
3328 new_uidx);
3329 }
3330 } else {
3331 int tmp;
3332
3333 ref_cnt_fb(pool->frame_bufs,
3334 &cm->ref_frame_map[cpi->lst_fb_idxes[LAST_REF_FRAMES - 1]],
3335 cm->new_fb_idx);
3336
3337 if (use_upsampled_ref)
3338 uref_cnt_fb(
3339 cpi->upsampled_ref_bufs,
3340 &cpi->upsampled_ref_idx[cpi->lst_fb_idxes[LAST_REF_FRAMES - 1]],
3341 new_uidx);
3342
3343 tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
3344
3345 shift_last_ref_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003346 cpi->lst_fb_idxes[0] = tmp;
3347
Zoe Liuf0e46692016-10-12 12:31:43 -07003348 assert(cm->show_existing_frame == 0);
3349 // NOTE: Currently only LF_UPDATE and INTNL_OVERLAY_UPDATE frames are to
3350 // refresh the LAST_FRAME.
3351 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3352 cpi->interp_filter_selected[0],
3353 sizeof(cpi->interp_filter_selected[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003354 }
3355#else
3356 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->lst_fb_idx],
3357 cm->new_fb_idx);
3358 if (use_upsampled_ref)
3359 uref_cnt_fb(cpi->upsampled_ref_bufs,
3360 &cpi->upsampled_ref_idx[cpi->lst_fb_idx], new_uidx);
3361 if (!cpi->rc.is_src_frame_alt_ref) {
3362 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3363 cpi->interp_filter_selected[0],
3364 sizeof(cpi->interp_filter_selected[0]));
3365 }
3366#endif // CONFIG_EXT_REFS
3367 }
3368
3369#if DUMP_REF_FRAME_IMAGES == 1
3370 // Dump out all reference frame images.
3371 dump_ref_frame_images(cpi);
3372#endif // DUMP_REF_FRAME_IMAGES
3373}
3374
Yaowu Xuf883b422016-08-30 14:01:10 -07003375static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003376 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
3377 struct loopfilter *lf = &cm->lf;
3378 if (is_lossless_requested(&cpi->oxcf)) {
3379 lf->filter_level = 0;
3380 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07003381 struct aom_usec_timer timer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003382
Yaowu Xuf883b422016-08-30 14:01:10 -07003383 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003384
Yaowu Xuf883b422016-08-30 14:01:10 -07003385 aom_usec_timer_start(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003386
3387#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -07003388 av1_pick_filter_restoration(cpi->Source, cpi, cpi->sf.lpf_pick);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003389#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003390 av1_pick_filter_level(cpi->Source, cpi, cpi->sf.lpf_pick);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003391#endif // CONFIG_LOOP_RESTORATION
3392
Yaowu Xuf883b422016-08-30 14:01:10 -07003393 aom_usec_timer_mark(&timer);
3394 cpi->time_pick_lpf += aom_usec_timer_elapsed(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003395 }
3396
3397 if (lf->filter_level > 0) {
3398#if CONFIG_VAR_TX || CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07003399 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003400#else
3401 if (cpi->num_workers > 1)
Yaowu Xuf883b422016-08-30 14:01:10 -07003402 av1_loop_filter_frame_mt(cm->frame_to_show, cm, xd->plane,
3403 lf->filter_level, 0, 0, cpi->workers,
3404 cpi->num_workers, &cpi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003405 else
Yaowu Xuf883b422016-08-30 14:01:10 -07003406 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003407#endif
3408 }
Steinar Midtskogend06588a2016-05-06 13:48:20 +02003409#if CONFIG_CLPF
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02003410 cm->clpf_strength_y = cm->clpf_strength_u = cm->clpf_strength_v = 0;
Yaowu Xud71be782016-10-14 08:47:03 -07003411 cm->clpf_size = CLPF_64X64;
3412
3413 // Allocate buffer to hold the status of all filter blocks:
3414 // 1 = On, 0 = off, -1 = implicitly off
3415 {
3416 int size;
3417 cm->clpf_stride = ((cm->frame_to_show->y_crop_width + MIN_FB_SIZE - 1) &
3418 ~(MIN_FB_SIZE - 1)) >>
3419 MIN_FB_SIZE_LOG2;
3420 size = cm->clpf_stride *
3421 ((cm->frame_to_show->y_crop_height + MIN_FB_SIZE - 1) &
3422 ~(MIN_FB_SIZE - 1)) >>
3423 MIN_FB_SIZE_LOG2;
3424 CHECK_MEM_ERROR(cm, cm->clpf_blocks, aom_malloc(size));
3425 memset(cm->clpf_blocks, CLPF_NOFLAG, size);
3426 }
3427
Steinar Midtskogend06588a2016-05-06 13:48:20 +02003428 if (!is_lossless_requested(&cpi->oxcf)) {
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02003429 const YV12_BUFFER_CONFIG *const frame = cm->frame_to_show;
3430
Steinar Midtskogen499deb92016-09-02 10:56:54 +02003431 // Find the best strength and block size for the entire frame
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02003432 int fb_size_log2, strength_y, strength_u, strength_v;
3433 av1_clpf_test_frame(frame, cpi->Source, cm, &strength_y, &fb_size_log2,
3434 AOM_PLANE_Y);
Steinar Midtskogen2e40cc42016-09-21 12:39:13 +02003435 av1_clpf_test_frame(frame, cpi->Source, cm, &strength_u, 0, AOM_PLANE_U);
3436 av1_clpf_test_frame(frame, cpi->Source, cm, &strength_v, 0, AOM_PLANE_V);
Steinar Midtskogend06588a2016-05-06 13:48:20 +02003437
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02003438 if (strength_y) {
Steinar Midtskogen499deb92016-09-02 10:56:54 +02003439 // Apply the filter using the chosen strength
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02003440 cm->clpf_strength_y = strength_y - (strength_y == 4);
Steinar Midtskogen499deb92016-09-02 10:56:54 +02003441 cm->clpf_size =
Yaowu Xud71be782016-10-14 08:47:03 -07003442 fb_size_log2 ? fb_size_log2 - MAX_FB_SIZE_LOG2 + 3 : CLPF_NOSIZE;
3443 av1_clpf_frame(frame, cpi->Source, cm, cm->clpf_size != CLPF_NOSIZE,
3444 strength_y, 4 + cm->clpf_size, AOM_PLANE_Y,
3445 av1_clpf_decision);
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02003446 }
3447 if (strength_u) {
3448 cm->clpf_strength_u = strength_u - (strength_u == 4);
Yaowu Xud71be782016-10-14 08:47:03 -07003449 av1_clpf_frame(frame, NULL, cm, 0, strength_u, 4, AOM_PLANE_U, NULL);
Steinar Midtskogenecf9a0c2016-09-13 16:37:13 +02003450 }
3451 if (strength_v) {
3452 cm->clpf_strength_v = strength_v - (strength_v == 4);
Yaowu Xud71be782016-10-14 08:47:03 -07003453 av1_clpf_frame(frame, NULL, cm, 0, strength_v, 4, AOM_PLANE_V, NULL);
Steinar Midtskogend06588a2016-05-06 13:48:20 +02003454 }
3455 }
3456#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003457#if CONFIG_DERING
3458 if (is_lossless_requested(&cpi->oxcf)) {
3459 cm->dering_level = 0;
3460 } else {
3461 cm->dering_level =
Yaowu Xuf883b422016-08-30 14:01:10 -07003462 av1_dering_search(cm->frame_to_show, cpi->Source, cm, xd);
3463 av1_dering_frame(cm->frame_to_show, cm, xd, cm->dering_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003464 }
3465#endif // CONFIG_DERING
Yaowu Xuc27fc142016-08-22 16:08:15 -07003466#if CONFIG_LOOP_RESTORATION
3467 if (cm->rst_info.restoration_type != RESTORE_NONE) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003468 av1_loop_restoration_init(&cm->rst_internal, &cm->rst_info,
3469 cm->frame_type == KEY_FRAME, cm->width,
3470 cm->height);
3471 av1_loop_restoration_rows(cm->frame_to_show, cm, 0, cm->mi_rows, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003472 }
3473#endif // CONFIG_LOOP_RESTORATION
3474
Yaowu Xuf883b422016-08-30 14:01:10 -07003475 aom_extend_frame_inner_borders(cm->frame_to_show);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003476}
3477
Yaowu Xuf883b422016-08-30 14:01:10 -07003478static INLINE void alloc_frame_mvs(AV1_COMMON *const cm, int buffer_idx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003479 RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
3480 if (new_fb_ptr->mvs == NULL || new_fb_ptr->mi_rows < cm->mi_rows ||
3481 new_fb_ptr->mi_cols < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003482 aom_free(new_fb_ptr->mvs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003483 CHECK_MEM_ERROR(cm, new_fb_ptr->mvs,
Yaowu Xuf883b422016-08-30 14:01:10 -07003484 (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003485 sizeof(*new_fb_ptr->mvs)));
3486 new_fb_ptr->mi_rows = cm->mi_rows;
3487 new_fb_ptr->mi_cols = cm->mi_cols;
3488 }
3489}
3490
Yaowu Xuf883b422016-08-30 14:01:10 -07003491void av1_scale_references(AV1_COMP *cpi) {
3492 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003493 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07003494 const AOM_REFFRAME ref_mask[INTER_REFS_PER_FRAME] = {
3495 AOM_LAST_FLAG,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003496#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07003497 AOM_LAST2_FLAG,
3498 AOM_LAST3_FLAG,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003499#endif // CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07003500 AOM_GOLD_FLAG,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003501#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07003502 AOM_BWD_FLAG,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003503#endif // CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07003504 AOM_ALT_FLAG
Yaowu Xuc27fc142016-08-22 16:08:15 -07003505 };
3506
3507 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003508 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Yaowu Xuc27fc142016-08-22 16:08:15 -07003509 if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
3510 BufferPool *const pool = cm->buffer_pool;
3511 const YV12_BUFFER_CONFIG *const ref =
3512 get_ref_frame_buffer(cpi, ref_frame);
3513
3514 if (ref == NULL) {
3515 cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3516 continue;
3517 }
3518
Yaowu Xuf883b422016-08-30 14:01:10 -07003519#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003520 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3521 RefCntBuffer *new_fb_ptr = NULL;
3522 int force_scaling = 0;
3523 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3524 if (new_fb == INVALID_IDX) {
3525 new_fb = get_free_fb(cm);
3526 force_scaling = 1;
3527 }
3528 if (new_fb == INVALID_IDX) return;
3529 new_fb_ptr = &pool->frame_bufs[new_fb];
3530 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3531 new_fb_ptr->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003532 if (aom_realloc_frame_buffer(
3533 &new_fb_ptr->buf, cm->width, cm->height, cm->subsampling_x,
3534 cm->subsampling_y, cm->use_highbitdepth, AOM_BORDER_IN_PIXELS,
3535 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003536 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003537 "Failed to allocate frame buffer");
3538 scale_and_extend_frame(ref, &new_fb_ptr->buf, MAX_MB_PLANE,
3539 (int)cm->bit_depth);
3540 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3541 alloc_frame_mvs(cm, new_fb);
3542 }
3543#else
3544 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3545 RefCntBuffer *new_fb_ptr = NULL;
3546 int force_scaling = 0;
3547 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3548 if (new_fb == INVALID_IDX) {
3549 new_fb = get_free_fb(cm);
3550 force_scaling = 1;
3551 }
3552 if (new_fb == INVALID_IDX) return;
3553 new_fb_ptr = &pool->frame_bufs[new_fb];
3554 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3555 new_fb_ptr->buf.y_crop_height != cm->height) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003556 if (aom_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003557 cm->subsampling_x, cm->subsampling_y,
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003558 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
3559 NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003560 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003561 "Failed to allocate frame buffer");
3562 scale_and_extend_frame(ref, &new_fb_ptr->buf, MAX_MB_PLANE);
3563 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3564 alloc_frame_mvs(cm, new_fb);
3565 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003566#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003567
3568 if (cpi->sf.use_upsampled_references &&
3569 (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3570 new_fb_ptr->buf.y_crop_height != cm->height)) {
3571 const int map_idx = get_ref_frame_map_idx(cpi, ref_frame);
3572 EncRefCntBuffer *ubuf =
3573 &cpi->upsampled_ref_bufs[cpi->upsampled_ref_idx[map_idx]];
3574
Yaowu Xuf883b422016-08-30 14:01:10 -07003575 if (aom_realloc_frame_buffer(&ubuf->buf, (cm->width << 3),
Yaowu Xuc27fc142016-08-22 16:08:15 -07003576 (cm->height << 3), cm->subsampling_x,
3577 cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07003578#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003579 cm->use_highbitdepth,
3580#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003581 (AOM_BORDER_IN_PIXELS << 3),
Yaowu Xuc27fc142016-08-22 16:08:15 -07003582 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003583 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003584 "Failed to allocate up-sampled frame buffer");
Yaowu Xuf883b422016-08-30 14:01:10 -07003585#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003586 scale_and_extend_frame(&new_fb_ptr->buf, &ubuf->buf, 1,
3587 (int)cm->bit_depth);
3588#else
3589 scale_and_extend_frame(&new_fb_ptr->buf, &ubuf->buf, 1);
3590#endif
3591 }
3592 } else {
3593 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3594 RefCntBuffer *const buf = &pool->frame_bufs[buf_idx];
3595 buf->buf.y_crop_width = ref->y_crop_width;
3596 buf->buf.y_crop_height = ref->y_crop_height;
3597 cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
3598 ++buf->ref_count;
3599 }
3600 } else {
3601 if (cpi->oxcf.pass != 0) cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3602 }
3603 }
3604}
3605
Yaowu Xuf883b422016-08-30 14:01:10 -07003606static void release_scaled_references(AV1_COMP *cpi) {
3607 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003608 int i;
3609 if (cpi->oxcf.pass == 0) {
3610 // Only release scaled references under certain conditions:
3611 // if reference will be updated, or if scaled reference has same resolution.
3612 int refresh[INTER_REFS_PER_FRAME];
3613 refresh[0] = (cpi->refresh_last_frame) ? 1 : 0;
3614#if CONFIG_EXT_REFS
3615 refresh[1] = refresh[2] = 0;
3616 refresh[3] = (cpi->refresh_golden_frame) ? 1 : 0;
3617 refresh[4] = (cpi->refresh_bwd_ref_frame) ? 1 : 0;
3618 refresh[5] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
3619#else
3620 refresh[1] = (cpi->refresh_golden_frame) ? 1 : 0;
3621 refresh[2] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
3622#endif // CONFIG_EXT_REFS
3623 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3624 const int idx = cpi->scaled_ref_idx[i - 1];
3625 RefCntBuffer *const buf =
3626 idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
3627 const YV12_BUFFER_CONFIG *const ref = get_ref_frame_buffer(cpi, i);
3628 if (buf != NULL &&
3629 (refresh[i - 1] || (buf->buf.y_crop_width == ref->y_crop_width &&
3630 buf->buf.y_crop_height == ref->y_crop_height))) {
3631 --buf->ref_count;
3632 cpi->scaled_ref_idx[i - 1] = INVALID_IDX;
3633 }
3634 }
3635 } else {
3636 for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i) {
3637 const int idx = cpi->scaled_ref_idx[i];
3638 RefCntBuffer *const buf =
3639 idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
3640 if (buf != NULL) {
3641 --buf->ref_count;
3642 cpi->scaled_ref_idx[i] = INVALID_IDX;
3643 }
3644 }
3645 }
3646}
3647
3648static void full_to_model_count(unsigned int *model_count,
3649 unsigned int *full_count) {
3650 int n;
3651 model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
3652 model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
3653 model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
3654 for (n = THREE_TOKEN; n < EOB_TOKEN; ++n)
3655 model_count[TWO_TOKEN] += full_count[n];
3656 model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
3657}
3658
Yaowu Xuf883b422016-08-30 14:01:10 -07003659void av1_full_to_model_counts(av1_coeff_count_model *model_count,
3660 av1_coeff_count *full_count) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003661 int i, j, k, l;
3662
3663 for (i = 0; i < PLANE_TYPES; ++i)
3664 for (j = 0; j < REF_TYPES; ++j)
3665 for (k = 0; k < COEF_BANDS; ++k)
3666 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
3667 full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
3668}
3669
3670#if 0 && CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003671static void output_frame_level_debug_stats(AV1_COMP *cpi) {
3672 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003673 FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
3674 int64_t recon_err;
3675
Yaowu Xuf883b422016-08-30 14:01:10 -07003676 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003677
Yaowu Xuf883b422016-08-30 14:01:10 -07003678 recon_err = aom_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003679
3680 if (cpi->twopass.total_left_stats.coded_error != 0.0)
3681 fprintf(f, "%10u %dx%d %10d %10d %d %d %10d %10d %10d %10d"
3682 "%10"PRId64" %10"PRId64" %5d %5d %10"PRId64" "
3683 "%10"PRId64" %10"PRId64" %10d "
3684 "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
3685 "%6d %6d %5d %5d %5d "
3686 "%10"PRId64" %10.3lf"
3687 "%10lf %8u %10"PRId64" %10d %10d %10d\n",
3688 cpi->common.current_video_frame,
3689 cm->width, cm->height,
3690 cpi->td.rd_counts.m_search_count,
3691 cpi->td.rd_counts.ex_search_count,
3692 cpi->rc.source_alt_ref_pending,
3693 cpi->rc.source_alt_ref_active,
3694 cpi->rc.this_frame_target,
3695 cpi->rc.projected_frame_size,
3696 cpi->rc.projected_frame_size / cpi->common.MBs,
3697 (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
3698 cpi->rc.vbr_bits_off_target,
3699 cpi->rc.vbr_bits_off_target_fast,
3700 cpi->twopass.extend_minq,
3701 cpi->twopass.extend_minq_fast,
3702 cpi->rc.total_target_vs_actual,
3703 (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
3704 cpi->rc.total_actual_bits, cm->base_qindex,
Yaowu Xuf883b422016-08-30 14:01:10 -07003705 av1_convert_qindex_to_q(cm->base_qindex, cm->bit_depth),
3706 (double)av1_dc_quant(cm->base_qindex, 0, cm->bit_depth) / 4.0,
3707 av1_convert_qindex_to_q(cpi->twopass.active_worst_quality,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003708 cm->bit_depth),
3709 cpi->rc.avg_q,
Yaowu Xuf883b422016-08-30 14:01:10 -07003710 av1_convert_qindex_to_q(cpi->oxcf.cq_level, cm->bit_depth),
Yaowu Xuc27fc142016-08-22 16:08:15 -07003711 cpi->refresh_last_frame, cpi->refresh_golden_frame,
3712 cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
3713 cpi->twopass.bits_left,
3714 cpi->twopass.total_left_stats.coded_error,
3715 cpi->twopass.bits_left /
3716 (1 + cpi->twopass.total_left_stats.coded_error),
3717 cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
3718 cpi->twopass.kf_zeromotion_pct,
3719 cpi->twopass.fr_content_type);
3720
3721 fclose(f);
3722
3723 if (0) {
3724 FILE *const fmodes = fopen("Modes.stt", "a");
3725 int i;
3726
3727 fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
3728 cm->frame_type, cpi->refresh_golden_frame,
3729 cpi->refresh_alt_ref_frame);
3730
3731 for (i = 0; i < MAX_MODES; ++i)
3732 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
3733
3734 fprintf(fmodes, "\n");
3735
3736 fclose(fmodes);
3737 }
3738}
3739#endif
3740
Yaowu Xuf883b422016-08-30 14:01:10 -07003741static void set_mv_search_params(AV1_COMP *cpi) {
3742 const AV1_COMMON *const cm = &cpi->common;
3743 const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003744
3745 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003746 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003747
3748 if (cpi->sf.mv.auto_mv_step_size) {
3749 if (frame_is_intra_only(cm)) {
3750 // Initialize max_mv_magnitude for use in the first INTER frame
3751 // after a key/intra-only frame.
3752 cpi->max_mv_magnitude = max_mv_def;
3753 } else {
3754 if (cm->show_frame) {
3755 // Allow mv_steps to correspond to twice the max mv magnitude found
3756 // in the previous frame, capped by the default max_mv_magnitude based
3757 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003758 cpi->mv_step_param = av1_init_search_range(
3759 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003760 }
3761 cpi->max_mv_magnitude = 0;
3762 }
3763 }
3764}
3765
Yaowu Xuf883b422016-08-30 14:01:10 -07003766static void set_size_independent_vars(AV1_COMP *cpi) {
3767 av1_set_speed_features_framesize_independent(cpi);
3768 av1_set_rd_speed_thresholds(cpi);
3769 av1_set_rd_speed_thresholds_sub8x8(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003770 cpi->common.interp_filter = cpi->sf.default_interp_filter;
3771}
3772
Yaowu Xuf883b422016-08-30 14:01:10 -07003773static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003774 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003775 AV1_COMMON *const cm = &cpi->common;
3776 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003777
3778 // Setup variables that depend on the dimensions of the frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003779 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003780
3781 // Decide q and q bounds.
Yaowu Xuf883b422016-08-30 14:01:10 -07003782 *q = av1_rc_pick_q_and_bounds(cpi, bottom_index, top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003783
3784 if (!frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003785 av1_set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003786 }
3787
3788 // Configure experimental use of segmentation for enhanced coding of
3789 // static regions if indicated.
3790 // Only allowed in the second pass of a two pass encode, as it requires
3791 // lagged coding, and if the relevant speed feature flag is set.
3792 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
3793 configure_static_seg_features(cpi);
3794}
3795
Yaowu Xuf883b422016-08-30 14:01:10 -07003796static void init_motion_estimation(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003797 int y_stride = cpi->scaled_source.y_stride;
3798
3799 if (cpi->sf.mv.search_method == NSTEP) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003800 av1_init3smotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003801 } else if (cpi->sf.mv.search_method == DIAMOND) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003802 av1_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003803 }
3804}
3805
Yaowu Xuf883b422016-08-30 14:01:10 -07003806static void set_frame_size(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003807 int ref_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07003808 AV1_COMMON *const cm = &cpi->common;
3809 AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003810 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
3811
Yaowu Xuf883b422016-08-30 14:01:10 -07003812 if (oxcf->pass == 2 && oxcf->rc_mode == AOM_VBR &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07003813 ((oxcf->resize_mode == RESIZE_FIXED && cm->current_video_frame == 0) ||
3814 (oxcf->resize_mode == RESIZE_DYNAMIC && cpi->resize_pending))) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003815 av1_calculate_coded_size(cpi, &oxcf->scaled_frame_width,
3816 &oxcf->scaled_frame_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003817
3818 // There has been a change in frame size.
Yaowu Xuf883b422016-08-30 14:01:10 -07003819 av1_set_size_literal(cpi, oxcf->scaled_frame_width,
3820 oxcf->scaled_frame_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003821 }
3822
Yaowu Xuf883b422016-08-30 14:01:10 -07003823 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07003824 oxcf->resize_mode == RESIZE_DYNAMIC) {
3825 if (cpi->resize_pending == 1) {
3826 oxcf->scaled_frame_width =
3827 (cm->width * cpi->resize_scale_num) / cpi->resize_scale_den;
3828 oxcf->scaled_frame_height =
3829 (cm->height * cpi->resize_scale_num) / cpi->resize_scale_den;
3830 } else if (cpi->resize_pending == -1) {
3831 // Go back up to original size.
3832 oxcf->scaled_frame_width = oxcf->width;
3833 oxcf->scaled_frame_height = oxcf->height;
3834 }
3835 if (cpi->resize_pending != 0) {
3836 // There has been a change in frame size.
Yaowu Xuf883b422016-08-30 14:01:10 -07003837 av1_set_size_literal(cpi, oxcf->scaled_frame_width,
3838 oxcf->scaled_frame_height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003839
3840 // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
3841 set_mv_search_params(cpi);
3842 }
3843 }
3844
3845 if (oxcf->pass == 2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003846 av1_set_target_rate(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003847 }
3848
3849 alloc_frame_mvs(cm, cm->new_fb_idx);
3850
3851 // Reset the frame pointers to the current frame size.
Yaowu Xuf883b422016-08-30 14:01:10 -07003852 if (aom_realloc_frame_buffer(get_frame_new_buffer(cm), cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003853 cm->subsampling_x, cm->subsampling_y,
Yaowu Xuf883b422016-08-30 14:01:10 -07003854#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003855 cm->use_highbitdepth,
3856#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003857 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
3858 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003859 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003860 "Failed to allocate frame buffer");
3861
3862 alloc_util_frame_buffers(cpi);
3863 init_motion_estimation(cpi);
3864
3865 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3866 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - LAST_FRAME];
3867 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3868
3869 ref_buf->idx = buf_idx;
3870
3871 if (buf_idx != INVALID_IDX) {
3872 YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
3873 ref_buf->buf = buf;
Yaowu Xuf883b422016-08-30 14:01:10 -07003874#if CONFIG_AOM_HIGHBITDEPTH
3875 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003876 &ref_buf->sf, buf->y_crop_width, buf->y_crop_height, cm->width,
3877 cm->height, (buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0);
3878#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003879 av1_setup_scale_factors_for_frame(&ref_buf->sf, buf->y_crop_width,
3880 buf->y_crop_height, cm->width,
3881 cm->height);
3882#endif // CONFIG_AOM_HIGHBITDEPTH
3883 if (av1_is_scaled(&ref_buf->sf)) aom_extend_frame_borders(buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003884 } else {
3885 ref_buf->buf = NULL;
3886 }
3887 }
3888
3889 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3890}
3891
Yaowu Xuf883b422016-08-30 14:01:10 -07003892static void reset_use_upsampled_references(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003893 MV_REFERENCE_FRAME ref_frame;
3894
3895 // reset up-sampled reference buffer structure.
3896 init_upsampled_ref_frame_bufs(cpi);
3897
3898 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3899 const YV12_BUFFER_CONFIG *const ref = get_ref_frame_buffer(cpi, ref_frame);
3900 int new_uidx = upsample_ref_frame(cpi, ref);
3901
3902 // Update the up-sampled reference index.
3903 cpi->upsampled_ref_idx[get_ref_frame_map_idx(cpi, ref_frame)] = new_uidx;
3904 cpi->upsampled_ref_bufs[new_uidx].ref_count++;
3905 }
3906}
3907
Yaowu Xuf883b422016-08-30 14:01:10 -07003908static void encode_without_recode_loop(AV1_COMP *cpi) {
3909 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003910 int q = 0, bottom_index = 0, top_index = 0; // Dummy variables.
3911 const int use_upsampled_ref = cpi->sf.use_upsampled_references;
3912
Yaowu Xuf883b422016-08-30 14:01:10 -07003913 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003914
3915 set_frame_size(cpi);
3916
3917 // For 1 pass CBR under dynamic resize mode: use faster scaling for source.
3918 // Only for 2x2 scaling for now.
Yaowu Xuf883b422016-08-30 14:01:10 -07003919 if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == AOM_CBR &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07003920 cpi->oxcf.resize_mode == RESIZE_DYNAMIC &&
3921 cpi->un_scaled_source->y_width == (cm->width << 1) &&
3922 cpi->un_scaled_source->y_height == (cm->height << 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003923 cpi->Source = av1_scale_if_required_fast(cm, cpi->un_scaled_source,
3924 &cpi->scaled_source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003925 if (cpi->unscaled_last_source != NULL)
Yaowu Xuf883b422016-08-30 14:01:10 -07003926 cpi->Last_Source = av1_scale_if_required_fast(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003927 cm, cpi->unscaled_last_source, &cpi->scaled_last_source);
3928 } else {
3929 cpi->Source =
Yaowu Xuf883b422016-08-30 14:01:10 -07003930 av1_scale_if_required(cm, cpi->un_scaled_source, &cpi->scaled_source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003931 if (cpi->unscaled_last_source != NULL)
Yaowu Xuf883b422016-08-30 14:01:10 -07003932 cpi->Last_Source = av1_scale_if_required(cm, cpi->unscaled_last_source,
3933 &cpi->scaled_last_source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003934 }
3935
3936 if (frame_is_intra_only(cm) == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003937 av1_scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003938 }
3939
3940 set_size_independent_vars(cpi);
3941 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
3942
3943 // cpi->sf.use_upsampled_references can be different from frame to frame.
3944 // Every time when cpi->sf.use_upsampled_references is changed from 0 to 1.
3945 // The reference frames for this frame have to be up-sampled before encoding.
3946 if (!use_upsampled_ref && cpi->sf.use_upsampled_references)
3947 reset_use_upsampled_references(cpi);
3948
Yaowu Xuf883b422016-08-30 14:01:10 -07003949 av1_set_quantizer(cm, q);
3950 av1_set_variance_partition_thresholds(cpi, q);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003951
3952 setup_frame(cpi);
3953
3954#if CONFIG_ENTROPY
3955 cm->do_subframe_update = cm->tile_cols == 1 && cm->tile_rows == 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003956 av1_copy(cm->starting_coef_probs, cm->fc->coef_probs);
3957 av1_copy(cpi->subframe_stats.enc_starting_coef_probs, cm->fc->coef_probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003958 cm->coef_probs_update_idx = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003959 av1_copy(cpi->subframe_stats.coef_probs_buf[0], cm->fc->coef_probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003960#endif // CONFIG_ENTROPY
3961
3962 suppress_active_map(cpi);
3963 // Variance adaptive and in frame q adjustment experiments are mutually
3964 // exclusive.
3965 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003966 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003967 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003968 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003969 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003970 av1_cyclic_refresh_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003971 }
3972 apply_active_map(cpi);
3973
3974 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07003975 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003976
3977 // Update some stats from cyclic refresh, and check if we should not update
3978 // golden reference, for 1 pass CBR.
3979 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->frame_type != KEY_FRAME &&
Yaowu Xuf883b422016-08-30 14:01:10 -07003980 (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == AOM_CBR))
3981 av1_cyclic_refresh_check_golden_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003982
3983 // Update the skip mb flag probabilities based on the distribution
3984 // seen in the last encoder iteration.
3985 // update_base_skip_probs(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07003986 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003987}
3988
Yaowu Xuf883b422016-08-30 14:01:10 -07003989static void encode_with_recode_loop(AV1_COMP *cpi, size_t *size,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003990 uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003991 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003992 RATE_CONTROL *const rc = &cpi->rc;
3993 int bottom_index, top_index;
3994 int loop_count = 0;
3995 int loop_at_this_size = 0;
3996 int loop = 0;
3997 int overshoot_seen = 0;
3998 int undershoot_seen = 0;
3999 int frame_over_shoot_limit;
4000 int frame_under_shoot_limit;
4001 int q = 0, q_low = 0, q_high = 0;
4002 const int use_upsampled_ref = cpi->sf.use_upsampled_references;
4003
4004 set_size_independent_vars(cpi);
4005
4006 // cpi->sf.use_upsampled_references can be different from frame to frame.
4007 // Every time when cpi->sf.use_upsampled_references is changed from 0 to 1.
4008 // The reference frames for this frame have to be up-sampled before encoding.
4009 if (!use_upsampled_ref && cpi->sf.use_upsampled_references)
4010 reset_use_upsampled_references(cpi);
4011
4012 do {
Yaowu Xuf883b422016-08-30 14:01:10 -07004013 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004014
4015 set_frame_size(cpi);
4016
4017 if (loop_count == 0 || cpi->resize_pending != 0) {
4018 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4019
4020 // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
4021 set_mv_search_params(cpi);
4022
4023 // Reset the loop state for new frame size.
4024 overshoot_seen = 0;
4025 undershoot_seen = 0;
4026
4027 // Reconfiguration for change in frame size has concluded.
4028 cpi->resize_pending = 0;
4029
4030 q_low = bottom_index;
4031 q_high = top_index;
4032
4033 loop_at_this_size = 0;
4034 }
4035
4036 // Decide frame size bounds first time through.
4037 if (loop_count == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004038 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4039 &frame_under_shoot_limit,
4040 &frame_over_shoot_limit);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004041 }
4042
4043 cpi->Source =
Yaowu Xuf883b422016-08-30 14:01:10 -07004044 av1_scale_if_required(cm, cpi->un_scaled_source, &cpi->scaled_source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004045
4046 if (cpi->unscaled_last_source != NULL)
Yaowu Xuf883b422016-08-30 14:01:10 -07004047 cpi->Last_Source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4048 &cpi->scaled_last_source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004049
4050 if (frame_is_intra_only(cm) == 0) {
4051 if (loop_count > 0) {
4052 release_scaled_references(cpi);
4053 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004054 av1_scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004055 }
4056
Yaowu Xuf883b422016-08-30 14:01:10 -07004057 av1_set_quantizer(cm, q);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004058
4059 if (loop_count == 0) setup_frame(cpi);
4060
4061#if CONFIG_ENTROPY
4062 // Base q-index may have changed, so we need to assign proper default coef
4063 // probs before every iteration.
4064 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
4065 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07004066 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004067 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
4068 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
4069 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
4070 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
4071 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4072 }
4073 }
4074#endif // CONFIG_ENTROPY
4075
4076#if CONFIG_ENTROPY
4077 cm->do_subframe_update = cm->tile_cols == 1 && cm->tile_rows == 1;
4078 if (loop_count == 0 || frame_is_intra_only(cm) ||
4079 cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004080 av1_copy(cm->starting_coef_probs, cm->fc->coef_probs);
4081 av1_copy(cpi->subframe_stats.enc_starting_coef_probs, cm->fc->coef_probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004082 } else {
4083 if (cm->do_subframe_update) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004084 av1_copy(cm->fc->coef_probs,
4085 cpi->subframe_stats.enc_starting_coef_probs);
4086 av1_copy(cm->starting_coef_probs,
4087 cpi->subframe_stats.enc_starting_coef_probs);
4088 av1_zero(cpi->subframe_stats.coef_counts_buf);
4089 av1_zero(cpi->subframe_stats.eob_counts_buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004090 }
4091 }
4092 cm->coef_probs_update_idx = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004093 av1_copy(cpi->subframe_stats.coef_probs_buf[0], cm->fc->coef_probs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004094#endif // CONFIG_ENTROPY
4095
4096 // Variance adaptive and in frame q adjustment experiments are mutually
4097 // exclusive.
4098 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004099 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004100 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004101 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004102 }
4103
4104 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07004105 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004106
4107 // Update the skip mb flag probabilities based on the distribution
4108 // seen in the last encoder iteration.
4109 // update_base_skip_probs(cpi);
4110
Yaowu Xuf883b422016-08-30 14:01:10 -07004111 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004112
4113 // Dummy pack of the bitstream using up to date stats to get an
4114 // accurate estimate of output frame size to determine if we need
4115 // to recode.
4116 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
4117 save_coding_context(cpi);
4118
Yaowu Xuf883b422016-08-30 14:01:10 -07004119 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004120
4121 rc->projected_frame_size = (int)(*size) << 3;
4122 restore_coding_context(cpi);
4123
4124 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4125 }
4126
Yaowu Xuf883b422016-08-30 14:01:10 -07004127 if (cpi->oxcf.rc_mode == AOM_Q) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004128 loop = 0;
4129 } else {
4130 if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
4131 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
4132 int last_q = q;
4133 int64_t kf_err;
4134
4135 int64_t high_err_target = cpi->ambient_err;
4136 int64_t low_err_target = cpi->ambient_err >> 1;
4137
Yaowu Xuf883b422016-08-30 14:01:10 -07004138#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004139 if (cm->use_highbitdepth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004140 kf_err = aom_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004141 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004142 kf_err = aom_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004143 }
4144#else
Yaowu Xuf883b422016-08-30 14:01:10 -07004145 kf_err = aom_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
4146#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004147
4148 // Prevent possible divide by zero error below for perfect KF
4149 kf_err += !kf_err;
4150
4151 // The key frame is not good enough or we can afford
4152 // to make it better without undue risk of popping.
4153 if ((kf_err > high_err_target &&
4154 rc->projected_frame_size <= frame_over_shoot_limit) ||
4155 (kf_err > low_err_target &&
4156 rc->projected_frame_size <= frame_under_shoot_limit)) {
4157 // Lower q_high
4158 q_high = q > q_low ? q - 1 : q_low;
4159
4160 // Adjust Q
4161 q = (int)((q * high_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004162 q = AOMMIN(q, (q_high + q_low) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004163 } else if (kf_err < low_err_target &&
4164 rc->projected_frame_size >= frame_under_shoot_limit) {
4165 // The key frame is much better than the previous frame
4166 // Raise q_low
4167 q_low = q < q_high ? q + 1 : q_high;
4168
4169 // Adjust Q
4170 q = (int)((q * low_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004171 q = AOMMIN(q, (q_high + q_low + 1) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004172 }
4173
4174 // Clamp Q to upper and lower limits:
4175 q = clamp(q, q_low, q_high);
4176
4177 loop = q != last_q;
4178 } else if (recode_loop_test(cpi, frame_over_shoot_limit,
4179 frame_under_shoot_limit, q,
Yaowu Xuf883b422016-08-30 14:01:10 -07004180 AOMMAX(q_high, top_index), bottom_index)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004181 // Is the projected frame size out of range and are we allowed
4182 // to attempt to recode.
4183 int last_q = q;
4184 int retries = 0;
4185
4186 if (cpi->resize_pending == 1) {
4187 // Change in frame size so go back around the recode loop.
4188 cpi->rc.frame_size_selector =
4189 SCALE_STEP1 - cpi->rc.frame_size_selector;
4190 cpi->rc.next_frame_size_selector = cpi->rc.frame_size_selector;
4191
4192#if CONFIG_INTERNAL_STATS
4193 ++cpi->tot_recode_hits;
4194#endif
4195 ++loop_count;
4196 loop = 1;
4197 continue;
4198 }
4199
4200 // Frame size out of permitted range:
4201 // Update correction factor & compute new Q to try...
4202
4203 // Frame is too large
4204 if (rc->projected_frame_size > rc->this_frame_target) {
4205 // Special case if the projected size is > the max allowed.
4206 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
4207 q_high = rc->worst_quality;
4208
4209 // Raise Qlow as to at least the current value
4210 q_low = q < q_high ? q + 1 : q_high;
4211
4212 if (undershoot_seen || loop_at_this_size > 1) {
4213 // Update rate_correction_factor unless
Yaowu Xuf883b422016-08-30 14:01:10 -07004214 av1_rc_update_rate_correction_factors(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004215
4216 q = (q_high + q_low + 1) / 2;
4217 } else {
4218 // Update rate_correction_factor unless
Yaowu Xuf883b422016-08-30 14:01:10 -07004219 av1_rc_update_rate_correction_factors(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004220
Yaowu Xuf883b422016-08-30 14:01:10 -07004221 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4222 AOMMAX(q_high, top_index));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004223
4224 while (q < q_low && retries < 10) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004225 av1_rc_update_rate_correction_factors(cpi);
4226 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4227 AOMMAX(q_high, top_index));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004228 retries++;
4229 }
4230 }
4231
4232 overshoot_seen = 1;
4233 } else {
4234 // Frame is too small
4235 q_high = q > q_low ? q - 1 : q_low;
4236
4237 if (overshoot_seen || loop_at_this_size > 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004238 av1_rc_update_rate_correction_factors(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004239 q = (q_high + q_low) / 2;
4240 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004241 av1_rc_update_rate_correction_factors(cpi);
4242 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4243 top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004244 // Special case reset for qlow for constrained quality.
4245 // This should only trigger where there is very substantial
4246 // undershoot on a frame and the auto cq level is above
4247 // the user passsed in value.
Yaowu Xuf883b422016-08-30 14:01:10 -07004248 if (cpi->oxcf.rc_mode == AOM_CQ && q < q_low) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004249 q_low = q;
4250 }
4251
4252 while (q > q_high && retries < 10) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004253 av1_rc_update_rate_correction_factors(cpi);
4254 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4255 top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004256 retries++;
4257 }
4258 }
4259
4260 undershoot_seen = 1;
4261 }
4262
4263 // Clamp Q to upper and lower limits:
4264 q = clamp(q, q_low, q_high);
4265
4266 loop = (q != last_q);
4267 } else {
4268 loop = 0;
4269 }
4270 }
4271
4272 // Special case for overlay frame.
4273 if (rc->is_src_frame_alt_ref &&
4274 rc->projected_frame_size < rc->max_frame_bandwidth)
4275 loop = 0;
4276
4277 if (loop) {
4278 ++loop_count;
4279 ++loop_at_this_size;
4280
4281#if CONFIG_INTERNAL_STATS
4282 ++cpi->tot_recode_hits;
4283#endif
4284 }
4285 } while (loop);
4286}
4287
Yaowu Xuf883b422016-08-30 14:01:10 -07004288static int get_ref_frame_flags(const AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004289 const int *const map = cpi->common.ref_frame_map;
4290
4291#if CONFIG_EXT_REFS
4292 const int last2_is_last =
4293 map[cpi->lst_fb_idxes[1]] == map[cpi->lst_fb_idxes[0]];
4294 const int last3_is_last =
4295 map[cpi->lst_fb_idxes[2]] == map[cpi->lst_fb_idxes[0]];
4296 const int gld_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[0]];
4297 const int bwd_is_last = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[0]];
4298 const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idxes[0]];
4299
4300 const int last3_is_last2 =
4301 map[cpi->lst_fb_idxes[2]] == map[cpi->lst_fb_idxes[1]];
4302 const int gld_is_last2 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[1]];
4303 const int bwd_is_last2 = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[1]];
4304
4305 const int gld_is_last3 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[2]];
4306 const int bwd_is_last3 = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[2]];
4307
4308 const int bwd_is_gld = map[cpi->bwd_fb_idx] == map[cpi->gld_fb_idx];
4309
4310 const int last2_is_alt = map[cpi->lst_fb_idxes[1]] == map[cpi->alt_fb_idx];
4311 const int last3_is_alt = map[cpi->lst_fb_idxes[2]] == map[cpi->alt_fb_idx];
4312 const int gld_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
4313 const int bwd_is_alt = map[cpi->bwd_fb_idx] == map[cpi->alt_fb_idx];
4314#else
4315 const int gld_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idx];
4316 const int gld_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
4317 const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idx];
4318#endif // CONFIG_EXT_REFS
4319
Yaowu Xuf883b422016-08-30 14:01:10 -07004320 int flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004321
4322#if CONFIG_EXT_REFS
4323 // Disable the use of BWDREF_FRAME for non-bipredictive frames.
4324 if (!(cpi->rc.is_bipred_frame || cpi->rc.is_last_bipred_frame ||
4325 (cpi->rc.is_bwd_ref_frame && cpi->num_extra_arfs)))
Yaowu Xuf883b422016-08-30 14:01:10 -07004326 flags &= ~AOM_BWD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004327#endif // CONFIG_EXT_REFS
4328
Yaowu Xuf883b422016-08-30 14:01:10 -07004329 if (gld_is_last || gld_is_alt) flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004330
Yaowu Xuf883b422016-08-30 14:01:10 -07004331 if (cpi->rc.frames_till_gf_update_due == INT_MAX) flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004332
Yaowu Xuf883b422016-08-30 14:01:10 -07004333 if (alt_is_last) flags &= ~AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004334
4335#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07004336 if (last2_is_last || last2_is_alt) flags &= ~AOM_LAST2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004337
Yaowu Xuf883b422016-08-30 14:01:10 -07004338 if (last3_is_last || last3_is_last2 || last3_is_alt) flags &= ~AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004339
Yaowu Xuf883b422016-08-30 14:01:10 -07004340 if (gld_is_last2 || gld_is_last3) flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004341
4342 if ((bwd_is_last || bwd_is_last2 || bwd_is_last3 || bwd_is_gld ||
4343 bwd_is_alt) &&
Yaowu Xuf883b422016-08-30 14:01:10 -07004344 (flags & AOM_BWD_FLAG))
4345 flags &= ~AOM_BWD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004346#endif // CONFIG_EXT_REFS
4347
4348 return flags;
4349}
4350
Yaowu Xuf883b422016-08-30 14:01:10 -07004351static void set_ext_overrides(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004352 // Overrides the defaults with the externally supplied values with
Yaowu Xuf883b422016-08-30 14:01:10 -07004353 // av1_update_reference() and av1_update_entropy() calls
Yaowu Xuc27fc142016-08-22 16:08:15 -07004354 // Note: The overrides are valid only for the next frame passed
4355 // to encode_frame_to_data_rate() function
4356 if (cpi->ext_refresh_frame_context_pending) {
4357 cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
4358 cpi->ext_refresh_frame_context_pending = 0;
4359 }
4360 if (cpi->ext_refresh_frame_flags_pending) {
4361 cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
4362 cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
4363 cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
4364 cpi->ext_refresh_frame_flags_pending = 0;
4365 }
4366}
4367
Yaowu Xuf883b422016-08-30 14:01:10 -07004368YV12_BUFFER_CONFIG *av1_scale_if_required_fast(AV1_COMMON *cm,
4369 YV12_BUFFER_CONFIG *unscaled,
4370 YV12_BUFFER_CONFIG *scaled) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004371 if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
4372 cm->mi_rows * MI_SIZE != unscaled->y_height) {
4373 // For 2x2 scaling down.
Yaowu Xuf883b422016-08-30 14:01:10 -07004374 aom_scale_frame(unscaled, scaled, unscaled->y_buffer, 9, 2, 1, 2, 1, 0);
4375 aom_extend_frame_borders(scaled);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004376 return scaled;
4377 } else {
4378 return unscaled;
4379 }
4380}
4381
Yaowu Xuf883b422016-08-30 14:01:10 -07004382YV12_BUFFER_CONFIG *av1_scale_if_required(AV1_COMMON *cm,
4383 YV12_BUFFER_CONFIG *unscaled,
4384 YV12_BUFFER_CONFIG *scaled) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004385 if (cm->mi_cols * MI_SIZE != unscaled->y_width ||
4386 cm->mi_rows * MI_SIZE != unscaled->y_height) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004387#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004388 scale_and_extend_frame_nonnormative(unscaled, scaled, (int)cm->bit_depth);
4389#else
4390 scale_and_extend_frame_nonnormative(unscaled, scaled);
Yaowu Xuf883b422016-08-30 14:01:10 -07004391#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004392 return scaled;
4393 } else {
4394 return unscaled;
4395 }
4396}
4397
Yaowu Xuf883b422016-08-30 14:01:10 -07004398static void set_arf_sign_bias(AV1_COMP *cpi) {
4399 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004400 int arf_sign_bias;
4401#if CONFIG_EXT_REFS
4402 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4403 // The arf_sign_bias will be one for internal ARFs'
4404 arf_sign_bias = cpi->rc.source_alt_ref_active &&
4405 (!cpi->refresh_alt_ref_frame ||
4406 (gf_group->rf_level[gf_group->index] == GF_ARF_LOW));
4407#else
4408 if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
4409 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4410 arf_sign_bias = cpi->rc.source_alt_ref_active &&
4411 (!cpi->refresh_alt_ref_frame ||
4412 (gf_group->rf_level[gf_group->index] == GF_ARF_LOW));
4413 } else {
4414 arf_sign_bias =
4415 (cpi->rc.source_alt_ref_active && !cpi->refresh_alt_ref_frame);
4416 }
4417#endif
4418 cm->ref_frame_sign_bias[ALTREF_FRAME] = arf_sign_bias;
4419#if CONFIG_EXT_REFS
4420 cm->ref_frame_sign_bias[BWDREF_FRAME] = cm->ref_frame_sign_bias[ALTREF_FRAME];
4421#endif // CONFIG_EXT_REFS
4422}
4423
Yaowu Xuf883b422016-08-30 14:01:10 -07004424static int setup_interp_filter_search_mask(AV1_COMP *cpi) {
James Zern7b9407a2016-05-18 23:48:05 -07004425 InterpFilter ifilter;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004426 int ref_total[TOTAL_REFS_PER_FRAME] = { 0 };
4427 MV_REFERENCE_FRAME ref;
4428 int mask = 0;
4429 int arf_idx = ALTREF_FRAME;
4430
4431#if CONFIG_EXT_REFS
4432 // Get which arf used as ALTREF_FRAME
4433 if (cpi->oxcf.pass == 2)
4434 arf_idx += cpi->twopass.gf_group.arf_ref_idx[cpi->twopass.gf_group.index];
4435#endif
4436
4437 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
4438 return mask;
4439
4440#if CONFIG_EXT_REFS
4441 for (ref = LAST_FRAME; ref < ALTREF_FRAME; ++ref)
4442 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter)
4443 ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
4444
4445 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter)
4446 ref_total[ref] += cpi->interp_filter_selected[arf_idx][ifilter];
4447#else
4448 for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
4449 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter)
4450 ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
4451#endif
4452
4453 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter) {
4454 if ((ref_total[LAST_FRAME] &&
4455 cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
4456#if CONFIG_EXT_REFS
4457 (ref_total[LAST2_FRAME] == 0 ||
4458 cpi->interp_filter_selected[LAST2_FRAME][ifilter] * 50 <
4459 ref_total[LAST2_FRAME]) &&
4460 (ref_total[LAST3_FRAME] == 0 ||
4461 cpi->interp_filter_selected[LAST3_FRAME][ifilter] * 50 <
4462 ref_total[LAST3_FRAME]) &&
4463#endif // CONFIG_EXT_REFS
4464 (ref_total[GOLDEN_FRAME] == 0 ||
4465 cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50 <
4466 ref_total[GOLDEN_FRAME]) &&
4467#if CONFIG_EXT_REFS
4468 (ref_total[BWDREF_FRAME] == 0 ||
4469 cpi->interp_filter_selected[BWDREF_FRAME][ifilter] * 50 <
4470 ref_total[BWDREF_FRAME]) &&
4471#endif // CONFIG_EXT_REFS
4472 (ref_total[ALTREF_FRAME] == 0 ||
4473 cpi->interp_filter_selected[arf_idx][ifilter] * 50 <
4474 ref_total[ALTREF_FRAME]))
4475 mask |= 1 << ifilter;
4476 }
4477 return mask;
4478}
4479
4480#define DUMP_RECON_FRAMES 0
4481
4482#if DUMP_RECON_FRAMES == 1
4483// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07004484static void dump_filtered_recon_frames(AV1_COMP *cpi) {
4485 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004486 const YV12_BUFFER_CONFIG *recon_buf = cm->frame_to_show;
4487 int h;
4488 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
4489 FILE *f_recon = NULL;
4490
4491 if (recon_buf == NULL || !cm->show_frame) {
4492 printf("Frame %d is not ready or no show to dump.\n",
4493 cm->current_video_frame);
4494 return;
4495 }
4496
4497 if (cm->current_video_frame == 0) {
4498 if ((f_recon = fopen(file_name, "wb")) == NULL) {
4499 printf("Unable to open file %s to write.\n", file_name);
4500 return;
4501 }
4502 } else {
4503 if ((f_recon = fopen(file_name, "ab")) == NULL) {
4504 printf("Unable to open file %s to append.\n", file_name);
4505 return;
4506 }
4507 }
4508 printf(
4509 "\nFrame=%5d, encode_update_type[%5d]=%1d, show_existing_frame=%d, "
4510 "y_stride=%4d, uv_stride=%4d, width=%4d, height=%4d\n",
4511 cm->current_video_frame, cpi->twopass.gf_group.index,
4512 cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index],
4513 cm->show_existing_frame, recon_buf->y_stride, recon_buf->uv_stride,
4514 cm->width, cm->height);
4515
4516 // --- Y ---
4517 for (h = 0; h < cm->height; ++h) {
4518 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
4519 f_recon);
4520 }
4521 // --- U ---
4522 for (h = 0; h < (cm->height >> 1); ++h) {
4523 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4524 f_recon);
4525 }
4526 // --- V ---
4527 for (h = 0; h < (cm->height >> 1); ++h) {
4528 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4529 f_recon);
4530 }
4531
4532 fclose(f_recon);
4533}
4534#endif // DUMP_RECON_FRAMES
4535
Yaowu Xuf883b422016-08-30 14:01:10 -07004536static void encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004537 uint8_t *dest,
4538 unsigned int *frame_flags) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004539 AV1_COMMON *const cm = &cpi->common;
4540 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004541 struct segmentation *const seg = &cm->seg;
4542 TX_SIZE t;
4543 set_ext_overrides(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004544 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004545
4546 // Set the arf sign bias for this frame.
4547 set_arf_sign_bias(cpi);
4548
4549#if CONFIG_EXT_REFS
4550 // NOTE:
4551 // (1) Move the setup of the ref_frame_flags upfront as it would be
4552 // determined by the current frame properties;
4553 // (2) The setup of the ref_frame_flags applies to both show_existing_frame's
4554 // and the other cases.
4555 if (cm->current_video_frame > 0)
4556 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
4557
4558 if (cm->show_existing_frame) {
4559 // NOTE(zoeliu): In BIDIR_PRED, the existing frame to show is the current
4560 // BWDREF_FRAME in the reference frame buffer.
4561 cm->frame_type = INTER_FRAME;
4562 cm->show_frame = 1;
4563 cpi->frame_flags = *frame_flags;
4564
4565 // In the case of show_existing frame, we will not send fresh flag
4566 // to decoder. Any change in the reference frame buffer can be done by
4567 // switching the virtual indices.
4568
4569 cpi->refresh_last_frame = 0;
4570 cpi->refresh_golden_frame = 0;
4571 cpi->refresh_bwd_ref_frame = 0;
4572 cpi->refresh_alt_ref_frame = 0;
4573
4574 cpi->rc.is_bwd_ref_frame = 0;
4575 cpi->rc.is_last_bipred_frame = 0;
4576 cpi->rc.is_bipred_frame = 0;
4577
4578 // Build the bitstream
Yaowu Xuf883b422016-08-30 14:01:10 -07004579 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004580
4581 // Set up frame to show to get ready for stats collection.
4582 cm->frame_to_show = get_frame_new_buffer(cm);
4583
4584#if DUMP_RECON_FRAMES == 1
4585 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
4586 dump_filtered_recon_frames(cpi);
4587#endif // DUMP_RECON_FRAMES
4588
4589 // Update the LAST_FRAME in the reference frame buffer.
Yaowu Xuf883b422016-08-30 14:01:10 -07004590 av1_update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004591
4592 // Update frame flags
4593 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
4594 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
4595 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
4596
4597 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
4598
4599 // Update the frame type
4600 cm->last_frame_type = cm->frame_type;
4601
4602#if CONFIG_EXT_REFS
4603 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
4604 // to do post-encoding update accordingly.
4605 if (cpi->rc.is_src_frame_alt_ref) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004606 av1_set_target_rate(cpi);
4607 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004608 }
4609#endif
4610
4611 cm->last_width = cm->width;
4612 cm->last_height = cm->height;
4613
4614 ++cm->current_video_frame;
4615
4616 return;
4617 }
4618#endif // CONFIG_EXT_REFS
4619
4620 // Set default state for segment based loop filter update flags.
4621 cm->lf.mode_ref_delta_update = 0;
4622
4623 if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
4624 cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
4625
4626 // Set various flags etc to special state if it is a key frame.
4627 if (frame_is_intra_only(cm)) {
4628 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07004629 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004630
4631 // If segmentation is enabled force a map update for key frames.
4632 if (seg->enabled) {
4633 seg->update_map = 1;
4634 seg->update_data = 1;
4635 }
4636
4637 // The alternate reference frame cannot be active for a key frame.
4638 cpi->rc.source_alt_ref_active = 0;
4639
4640 cm->error_resilient_mode = oxcf->error_resilient_mode;
4641
4642 // By default, encoder assumes decoder can use prev_mi.
4643 if (cm->error_resilient_mode) {
4644 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
4645 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
4646 } else if (cm->intra_only) {
4647 // Only reset the current context.
4648 cm->reset_frame_context = RESET_FRAME_CONTEXT_CURRENT;
4649 }
4650 }
4651
4652 // For 1 pass CBR, check if we are dropping this frame.
4653 // Never drop on key frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07004654 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07004655 cm->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004656 if (av1_rc_drop_frame(cpi)) {
4657 av1_rc_postencode_update_drop_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004658 ++cm->current_video_frame;
4659 return;
4660 }
4661 }
4662
Yaowu Xuf883b422016-08-30 14:01:10 -07004663 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004664
4665#if CONFIG_INTERNAL_STATS
4666 memset(cpi->mode_chosen_counts, 0,
4667 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
4668#endif
4669
4670 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
4671 encode_without_recode_loop(cpi);
4672 } else {
4673 encode_with_recode_loop(cpi, size, dest);
4674 }
4675
4676#ifdef OUTPUT_YUV_SKINMAP
4677 if (cpi->common.current_video_frame > 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004678 av1_compute_skin_map(cpi, yuv_skinmap_file);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004679 }
4680#endif // OUTPUT_YUV_SKINMAP
4681
4682 // Special case code to reduce pulsing when key frames are forced at a
4683 // fixed interval. Note the reconstruction error if it is the frame before
4684 // the force key frame
4685 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004686#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004687 if (cm->use_highbitdepth) {
4688 cpi->ambient_err =
Yaowu Xuf883b422016-08-30 14:01:10 -07004689 aom_highbd_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004690 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004691 cpi->ambient_err = aom_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004692 }
4693#else
Yaowu Xuf883b422016-08-30 14:01:10 -07004694 cpi->ambient_err = aom_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
4695#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004696 }
4697
4698 // If the encoder forced a KEY_FRAME decision
4699 if (cm->frame_type == KEY_FRAME) {
4700 cpi->refresh_last_frame = 1;
4701 }
4702
4703 cm->frame_to_show = get_frame_new_buffer(cm);
4704 cm->frame_to_show->color_space = cm->color_space;
4705 cm->frame_to_show->color_range = cm->color_range;
4706 cm->frame_to_show->render_width = cm->render_width;
4707 cm->frame_to_show->render_height = cm->render_height;
4708
4709#if CONFIG_EXT_REFS
4710// TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
4711// off.
4712#endif // CONFIG_EXT_REFS
4713
4714 // Pick the loop filter level for the frame.
4715 loopfilter_frame(cpi, cm);
4716
4717 // Build the bitstream
Yaowu Xuf883b422016-08-30 14:01:10 -07004718 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004719
4720#if DUMP_RECON_FRAMES == 1
4721 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
4722 if (cm->show_frame) dump_filtered_recon_frames(cpi);
4723#endif // DUMP_RECON_FRAMES
4724
Steinar Midtskogend06588a2016-05-06 13:48:20 +02004725#if CONFIG_CLPF
4726 aom_free(cm->clpf_blocks);
Steinar Midtskogenf4d41e62016-08-25 12:22:24 +02004727 cm->clpf_blocks = 0;
Steinar Midtskogend06588a2016-05-06 13:48:20 +02004728#endif
4729
Yaowu Xuc27fc142016-08-22 16:08:15 -07004730 if (cm->seg.update_map) update_reference_segmentation_map(cpi);
4731
4732 if (frame_is_intra_only(cm) == 0) {
4733 release_scaled_references(cpi);
4734 }
4735
Yaowu Xuf883b422016-08-30 14:01:10 -07004736 av1_update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004737
4738 for (t = TX_4X4; t <= TX_32X32; t++)
Yaowu Xuf883b422016-08-30 14:01:10 -07004739 av1_full_to_model_counts(cpi->td.counts->coef[t],
4740 cpi->td.rd_counts.coef_counts[t]);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004741
4742 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
4743#if CONFIG_ENTROPY
4744 cm->partial_prob_update = 0;
4745#endif // CONFIG_ENTROPY
Yaowu Xuf883b422016-08-30 14:01:10 -07004746 av1_adapt_coef_probs(cm);
4747 av1_adapt_intra_frame_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004748 }
4749
4750 if (!frame_is_intra_only(cm)) {
4751 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004752 av1_adapt_inter_frame_probs(cm);
4753 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004754 }
4755 }
4756
4757 if (cpi->refresh_golden_frame == 1)
4758 cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
4759 else
4760 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
4761
4762 if (cpi->refresh_alt_ref_frame == 1)
4763 cpi->frame_flags |= FRAMEFLAGS_ALTREF;
4764 else
4765 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
4766
4767#if CONFIG_EXT_REFS
4768 if (cpi->refresh_bwd_ref_frame == 1)
4769 cpi->frame_flags |= FRAMEFLAGS_BWDREF;
4770 else
4771 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
4772#endif // CONFIG_EXT_REFS
4773
4774#if !CONFIG_EXT_REFS
4775 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
4776#endif // !CONFIG_EXT_REFS
4777
4778#if CONFIG_EXT_REFS
4779 cm->last3_frame_type = cm->last2_frame_type;
4780 cm->last2_frame_type = cm->last_frame_type;
4781#endif // CONFIG_EXT_REFS
4782 cm->last_frame_type = cm->frame_type;
4783
Yaowu Xuf883b422016-08-30 14:01:10 -07004784 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004785
4786#if 0
4787 output_frame_level_debug_stats(cpi);
4788#endif
4789
4790 if (cm->frame_type == KEY_FRAME) {
4791 // Tell the caller that the frame was coded as a key frame
4792 *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
4793 } else {
4794 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
4795 }
4796
4797 // Clear the one shot update flags for segmentation map and mode/ref loop
4798 // filter deltas.
4799 cm->seg.update_map = 0;
4800 cm->seg.update_data = 0;
4801 cm->lf.mode_ref_delta_update = 0;
4802
4803 // keep track of the last coded dimensions
4804 cm->last_width = cm->width;
4805 cm->last_height = cm->height;
4806
4807 // reset to normal state now that we are done.
4808 if (!cm->show_existing_frame) cm->last_show_frame = cm->show_frame;
4809
4810 if (cm->show_frame) {
4811#if CONFIG_EXT_REFS
4812// TODO(zoeliu): We may only swamp mi and prev_mi for those frames that are
4813// being used as reference.
4814#endif // CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07004815 av1_swap_mi_and_prev_mi(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004816 // Don't increment frame counters if this was an altref buffer
4817 // update not a real frame
4818 ++cm->current_video_frame;
4819 }
4820
4821#if CONFIG_EXT_REFS
4822 // NOTE: Shall not refer to any frame not used as reference.
4823 if (cm->is_reference_frame)
4824#endif // CONFIG_EXT_REFS
4825 cm->prev_frame = cm->cur_frame;
4826}
4827
Yaowu Xuf883b422016-08-30 14:01:10 -07004828static void Pass0Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004829 unsigned int *frame_flags) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004830 if (cpi->oxcf.rc_mode == AOM_CBR) {
4831 av1_rc_get_one_pass_cbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004832 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004833 av1_rc_get_one_pass_vbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004834 }
4835 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4836}
4837
Yaowu Xuf883b422016-08-30 14:01:10 -07004838static void Pass2Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004839 unsigned int *frame_flags) {
4840 cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
4841
4842 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
4843
4844#if CONFIG_EXT_REFS
4845 // Do not do post-encoding update for those frames that do not have a spot in
4846 // a gf group, but note that an OVERLAY frame always has a spot in a gf group,
4847 // even when show_existing_frame is used.
4848 if (!cpi->common.show_existing_frame || cpi->rc.is_src_frame_alt_ref) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004849 av1_twopass_postencode_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004850 }
4851 check_show_existing_frame(cpi);
4852#else
Yaowu Xuf883b422016-08-30 14:01:10 -07004853 av1_twopass_postencode_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004854#endif // CONFIG_EXT_REFS
4855}
4856
Yaowu Xuf883b422016-08-30 14:01:10 -07004857static void init_ref_frame_bufs(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004858 int i;
4859 BufferPool *const pool = cm->buffer_pool;
4860 cm->new_fb_idx = INVALID_IDX;
4861 for (i = 0; i < REF_FRAMES; ++i) {
4862 cm->ref_frame_map[i] = INVALID_IDX;
4863 pool->frame_bufs[i].ref_count = 0;
4864 }
4865}
4866
Yaowu Xuf883b422016-08-30 14:01:10 -07004867static void check_initial_width(AV1_COMP *cpi,
4868#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004869 int use_highbitdepth,
4870#endif
4871 int subsampling_x, int subsampling_y) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004872 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004873
4874 if (!cpi->initial_width ||
Yaowu Xuf883b422016-08-30 14:01:10 -07004875#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004876 cm->use_highbitdepth != use_highbitdepth ||
4877#endif
4878 cm->subsampling_x != subsampling_x ||
4879 cm->subsampling_y != subsampling_y) {
4880 cm->subsampling_x = subsampling_x;
4881 cm->subsampling_y = subsampling_y;
Yaowu Xuf883b422016-08-30 14:01:10 -07004882#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004883 cm->use_highbitdepth = use_highbitdepth;
4884#endif
4885
4886 alloc_raw_frame_buffers(cpi);
4887 init_ref_frame_bufs(cm);
4888 alloc_util_frame_buffers(cpi);
4889
4890 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
4891
4892 cpi->initial_width = cm->width;
4893 cpi->initial_height = cm->height;
4894 cpi->initial_mbs = cm->MBs;
4895 }
4896}
4897
Yaowu Xuf883b422016-08-30 14:01:10 -07004898int av1_receive_raw_frame(AV1_COMP *cpi, unsigned int frame_flags,
4899 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
4900 int64_t end_time) {
4901 AV1_COMMON *const cm = &cpi->common;
4902 struct aom_usec_timer timer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004903 int res = 0;
4904 const int subsampling_x = sd->subsampling_x;
4905 const int subsampling_y = sd->subsampling_y;
Yaowu Xuf883b422016-08-30 14:01:10 -07004906#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004907 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
4908#endif
4909
Yaowu Xuf883b422016-08-30 14:01:10 -07004910#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004911 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
4912#else
4913 check_initial_width(cpi, subsampling_x, subsampling_y);
Yaowu Xuf883b422016-08-30 14:01:10 -07004914#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004915
Yaowu Xuf883b422016-08-30 14:01:10 -07004916 aom_usec_timer_start(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004917
Yaowu Xuf883b422016-08-30 14:01:10 -07004918 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
4919#if CONFIG_AOM_HIGHBITDEPTH
4920 use_highbitdepth,
4921#endif // CONFIG_AOM_HIGHBITDEPTH
4922 frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07004923 res = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -07004924 aom_usec_timer_mark(&timer);
4925 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004926
4927 if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
4928 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004929 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004930 "Non-4:2:0 color format requires profile 1 or 3");
4931 res = -1;
4932 }
4933 if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
4934 (subsampling_x == 1 && subsampling_y == 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004935 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004936 "4:2:0 color format requires profile 0 or 2");
4937 res = -1;
4938 }
4939
4940 return res;
4941}
4942
Yaowu Xuf883b422016-08-30 14:01:10 -07004943static int frame_is_reference(const AV1_COMP *cpi) {
4944 const AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004945
4946 return cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
4947 cpi->refresh_golden_frame ||
4948#if CONFIG_EXT_REFS
4949 cpi->refresh_bwd_ref_frame ||
4950#endif // CONFIG_EXT_REFS
4951 cpi->refresh_alt_ref_frame || !cm->error_resilient_mode ||
4952 cm->lf.mode_ref_delta_update || cm->seg.update_map ||
4953 cm->seg.update_data;
4954}
4955
Yaowu Xuf883b422016-08-30 14:01:10 -07004956static void adjust_frame_rate(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004957 const struct lookahead_entry *source) {
4958 int64_t this_duration;
4959 int step = 0;
4960
4961 if (source->ts_start == cpi->first_time_stamp_ever) {
4962 this_duration = source->ts_end - source->ts_start;
4963 step = 1;
4964 } else {
4965 int64_t last_duration =
4966 cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
4967
4968 this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
4969
4970 // do a step update if the duration changes by 10%
4971 if (last_duration)
4972 step = (int)((this_duration - last_duration) * 10 / last_duration);
4973 }
4974
4975 if (this_duration) {
4976 if (step) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004977 av1_new_framerate(cpi, 10000000.0 / this_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004978 } else {
4979 // Average this frame's rate into the last second's average
4980 // frame rate. If we haven't seen 1 second yet, then average
4981 // over the whole interval seen.
Yaowu Xuf883b422016-08-30 14:01:10 -07004982 const double interval = AOMMIN(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004983 (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
4984 double avg_duration = 10000000.0 / cpi->framerate;
4985 avg_duration *= (interval - avg_duration + this_duration);
4986 avg_duration /= interval;
4987
Yaowu Xuf883b422016-08-30 14:01:10 -07004988 av1_new_framerate(cpi, 10000000.0 / avg_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004989 }
4990 }
4991 cpi->last_time_stamp_seen = source->ts_start;
4992 cpi->last_end_time_stamp_seen = source->ts_end;
4993}
4994
4995// Returns 0 if this is not an alt ref else the offset of the source frame
4996// used as the arf midpoint.
Yaowu Xuf883b422016-08-30 14:01:10 -07004997static int get_arf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004998 RATE_CONTROL *const rc = &cpi->rc;
4999 int arf_src_index = 0;
5000 if (is_altref_enabled(cpi)) {
5001 if (cpi->oxcf.pass == 2) {
5002 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5003 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
5004 arf_src_index = gf_group->arf_src_offset[gf_group->index];
5005 }
5006 } else if (rc->source_alt_ref_pending) {
5007 arf_src_index = rc->frames_till_gf_update_due;
5008 }
5009 }
5010 return arf_src_index;
5011}
5012
5013#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07005014static int get_brf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005015 int brf_src_index = 0;
5016 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5017
5018 // TODO(zoeliu): We need to add the check on the -bwd_ref command line setup
5019 // flag.
5020 if (gf_group->bidir_pred_enabled[gf_group->index]) {
5021 if (cpi->oxcf.pass == 2) {
5022 if (gf_group->update_type[gf_group->index] == BRF_UPDATE)
5023 brf_src_index = gf_group->brf_src_offset[gf_group->index];
5024 } else {
5025 // TODO(zoeliu): To re-visit the setup for this scenario
5026 brf_src_index = cpi->rc.bipred_group_interval - 1;
5027 }
5028 }
5029
5030 return brf_src_index;
5031}
5032#endif // CONFIG_EXT_REFS
5033
Yaowu Xuf883b422016-08-30 14:01:10 -07005034static void check_src_altref(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005035 const struct lookahead_entry *source) {
5036 RATE_CONTROL *const rc = &cpi->rc;
5037
5038 // If pass == 2, the parameters set here will be reset in
Yaowu Xuf883b422016-08-30 14:01:10 -07005039 // av1_rc_get_second_pass_params()
Yaowu Xuc27fc142016-08-22 16:08:15 -07005040
5041 if (cpi->oxcf.pass == 2) {
5042 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5043 rc->is_src_frame_alt_ref =
5044#if CONFIG_EXT_REFS
5045 (gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE) ||
5046#endif
5047 (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
5048 } else {
5049 rc->is_src_frame_alt_ref =
5050 cpi->alt_ref_source && (source == cpi->alt_ref_source);
5051 }
5052
5053 if (rc->is_src_frame_alt_ref) {
5054 // Current frame is an ARF overlay frame.
5055 cpi->alt_ref_source = NULL;
5056
5057 // Don't refresh the last buffer for an ARF overlay frame. It will
5058 // become the GF so preserve last as an alternative prediction option.
5059 cpi->refresh_last_frame = 0;
5060 }
5061}
5062
5063#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005064extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
5065 const unsigned char *img2, int img2_pitch,
5066 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005067
5068static void adjust_image_stat(double y, double u, double v, double all,
5069 ImageStat *s) {
5070 s->stat[Y] += y;
5071 s->stat[U] += u;
5072 s->stat[V] += v;
5073 s->stat[ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07005074 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005075}
5076
Yaowu Xuf883b422016-08-30 14:01:10 -07005077static void compute_internal_stats(AV1_COMP *cpi) {
5078 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005079 double samples = 0.0;
5080 uint32_t in_bit_depth = 8;
5081 uint32_t bit_depth = 8;
5082
Yaowu Xuf883b422016-08-30 14:01:10 -07005083#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005084 if (cm->use_highbitdepth) {
5085 in_bit_depth = cpi->oxcf.input_bit_depth;
5086 bit_depth = cm->bit_depth;
5087 }
5088#endif
5089 if (cm->show_frame) {
5090 const YV12_BUFFER_CONFIG *orig = cpi->Source;
5091 const YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5092 double y, u, v, frame_all;
5093
5094 cpi->count++;
5095 if (cpi->b_calculate_psnr) {
5096 PSNR_STATS psnr;
5097 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005098 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005099// TODO(yaowu): unify these two versions into one.
Yaowu Xuf883b422016-08-30 14:01:10 -07005100#if CONFIG_AOM_HIGHBITDEPTH
5101 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005102#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005103 aom_calc_psnr(orig, recon, &psnr);
5104#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005105
5106 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
5107 &cpi->psnr);
5108 cpi->total_sq_error += psnr.sse[0];
5109 cpi->total_samples += psnr.samples[0];
5110 samples = psnr.samples[0];
5111// TODO(yaowu): unify these two versions into one.
Yaowu Xuf883b422016-08-30 14:01:10 -07005112#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005113 if (cm->use_highbitdepth)
5114 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07005115 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005116 else
Yaowu Xuf883b422016-08-30 14:01:10 -07005117 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005118#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005119 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
5120#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005121
Yaowu Xuf883b422016-08-30 14:01:10 -07005122 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005123 cpi->summed_quality += frame_ssim2 * weight;
5124 cpi->summed_weights += weight;
5125
5126#if 0
5127 {
5128 FILE *f = fopen("q_used.stt", "a");
5129 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
5130 cpi->common.current_video_frame, y2, u2, v2,
5131 frame_psnr2, frame_ssim2);
5132 fclose(f);
5133 }
5134#endif
5135 }
5136 if (cpi->b_calculate_blockiness) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005137#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005138 if (!cm->use_highbitdepth)
5139#endif
5140 {
5141 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07005142 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
5143 recon->y_stride, orig->y_width, orig->y_height);
5144 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005145 cpi->total_blockiness += frame_blockiness;
5146 }
5147
5148 if (cpi->b_calculate_consistency) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005149#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005150 if (!cm->use_highbitdepth)
5151#endif
5152 {
Yaowu Xuf883b422016-08-30 14:01:10 -07005153 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005154 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
5155 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
5156
5157 const double peak = (double)((1 << in_bit_depth) - 1);
5158 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005159 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005160 if (consistency > 0.0)
5161 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005162 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005163 cpi->total_inconsistency += this_inconsistency;
5164 }
5165 }
5166 }
5167
5168 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07005169 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005170 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07005171 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005172 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
5173 }
5174}
5175#endif // CONFIG_INTERNAL_STATS
5176
Yaowu Xuf883b422016-08-30 14:01:10 -07005177int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
5178 size_t *size, uint8_t *dest, int64_t *time_stamp,
5179 int64_t *time_end, int flush) {
5180 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
5181 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005182 BufferPool *const pool = cm->buffer_pool;
5183 RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07005184 struct aom_usec_timer cmptimer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005185 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
5186 struct lookahead_entry *last_source = NULL;
5187 struct lookahead_entry *source = NULL;
5188 int arf_src_index;
5189#if CONFIG_EXT_REFS
5190 int brf_src_index;
5191#endif // CONFIG_EXT_REFS
5192 int i;
5193
5194#if CONFIG_BITSTREAM_DEBUG
5195 assert(cpi->oxcf.max_threads == 0 &&
5196 "bitstream debug tool does not support multithreading");
5197 bitstream_queue_record_write();
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07005198 bitstream_queue_set_frame_write(cm->current_video_frame * 2 + cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005199#endif
5200
Yaowu Xuf883b422016-08-30 14:01:10 -07005201 aom_usec_timer_start(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005202
Yaowu Xuf883b422016-08-30 14:01:10 -07005203 av1_set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005204
5205 // Is multi-arf enabled.
5206 // Note that at the moment multi_arf is only configured for 2 pass VBR
5207 if ((oxcf->pass == 2) && (cpi->oxcf.enable_auto_arf > 1))
5208 cpi->multi_arf_allowed = 1;
5209 else
5210 cpi->multi_arf_allowed = 0;
5211
5212 // Normal defaults
5213 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
5214 cm->refresh_frame_context =
5215 (oxcf->error_resilient_mode || oxcf->frame_parallel_decoding_mode)
5216 ? REFRESH_FRAME_CONTEXT_FORWARD
5217 : REFRESH_FRAME_CONTEXT_BACKWARD;
5218
5219 cpi->refresh_last_frame = 1;
5220 cpi->refresh_golden_frame = 0;
5221#if CONFIG_EXT_REFS
5222 cpi->refresh_bwd_ref_frame = 0;
5223#endif // CONFIG_EXT_REFS
5224 cpi->refresh_alt_ref_frame = 0;
5225
5226#if CONFIG_EXT_REFS
5227 if (oxcf->pass == 2 && cm->show_existing_frame) {
5228 // Manage the source buffer and flush out the source frame that has been
5229 // coded already; Also get prepared for PSNR calculation if needed.
Yaowu Xuf883b422016-08-30 14:01:10 -07005230 if ((source = av1_lookahead_pop(cpi->lookahead, flush)) == NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005231 *size = 0;
5232 return -1;
5233 }
5234 cpi->Source = &source->img;
5235 // TODO(zoeliu): To track down to determine whether it's needed to adjust
5236 // the frame rate.
5237 *time_stamp = source->ts_start;
5238 *time_end = source->ts_end;
5239
5240 // We need to adjust frame rate for an overlay frame
5241 if (cpi->rc.is_src_frame_alt_ref) {
5242 adjust_frame_rate(cpi, source);
5243 }
5244
5245 // Find a free buffer for the new frame, releasing the reference previously
5246 // held.
5247 if (cm->new_fb_idx != INVALID_IDX) {
5248 --pool->frame_bufs[cm->new_fb_idx].ref_count;
5249 }
5250 cm->new_fb_idx = get_free_fb(cm);
5251
5252 if (cm->new_fb_idx == INVALID_IDX) return -1;
5253
5254 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07005255 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005256
5257 // Start with a 0 size frame.
5258 *size = 0;
5259
5260 // We need to update the gf_group for show_existing overlay frame
5261 if (cpi->rc.is_src_frame_alt_ref) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005262 av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005263 }
5264
5265 Pass2Encode(cpi, size, dest, frame_flags);
5266
5267 if (cpi->b_calculate_psnr) generate_psnr_packet(cpi);
5268
5269#if CONFIG_INTERNAL_STATS
5270 compute_internal_stats(cpi);
5271 cpi->bytes += (int)(*size);
5272#endif // CONFIG_INTERNAL_STATS
5273
5274 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07005275 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005276
5277 cm->show_existing_frame = 0;
5278 return 0;
5279 }
5280#endif // CONFIG_EXT_REFS
5281
5282 // Should we encode an arf frame.
5283 arf_src_index = get_arf_src_index(cpi);
5284 if (arf_src_index) {
5285 for (i = 0; i <= arf_src_index; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005286 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005287 // Avoid creating an alt-ref if there's a forced keyframe pending.
5288 if (e == NULL) {
5289 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07005290 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005291 arf_src_index = 0;
5292 flush = 1;
5293 break;
5294 }
5295 }
5296 }
5297
5298 if (arf_src_index) {
5299 assert(arf_src_index <= rc->frames_to_key);
5300
Yaowu Xuf883b422016-08-30 14:01:10 -07005301 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005302 cpi->alt_ref_source = source;
5303
5304 if (oxcf->arnr_max_frames > 0) {
5305 // Produce the filtered ARF frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07005306 av1_temporal_filter(cpi, arf_src_index);
5307 aom_extend_frame_borders(&cpi->alt_ref_buffer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005308 force_src_buffer = &cpi->alt_ref_buffer;
5309 }
5310
5311 cm->show_frame = 0;
5312 cm->intra_only = 0;
5313 cpi->refresh_alt_ref_frame = 1;
5314 cpi->refresh_golden_frame = 0;
5315 cpi->refresh_last_frame = 0;
5316 rc->is_src_frame_alt_ref = 0;
5317 }
5318 rc->source_alt_ref_pending = 0;
5319 }
5320
5321#if CONFIG_EXT_REFS
5322 rc->is_bwd_ref_frame = 0;
5323 brf_src_index = get_brf_src_index(cpi);
5324 if (brf_src_index) {
5325 assert(brf_src_index <= rc->frames_to_key);
Yaowu Xuf883b422016-08-30 14:01:10 -07005326 if ((source = av1_lookahead_peek(cpi->lookahead, brf_src_index)) != NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005327 cm->show_frame = 0;
5328 cm->intra_only = 0;
5329
5330 cpi->refresh_bwd_ref_frame = 1;
5331 cpi->refresh_last_frame = 0;
5332 cpi->refresh_golden_frame = 0;
5333 cpi->refresh_alt_ref_frame = 0;
5334
5335 rc->is_bwd_ref_frame = 1;
5336 }
5337 }
5338#endif // CONFIG_EXT_REFS
5339
5340 if (!source) {
5341 // Get last frame source.
5342 if (cm->current_video_frame > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005343 if ((last_source = av1_lookahead_peek(cpi->lookahead, -1)) == NULL)
Yaowu Xuc27fc142016-08-22 16:08:15 -07005344 return -1;
5345 }
5346
5347 // Read in the source frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07005348 source = av1_lookahead_pop(cpi->lookahead, flush);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005349
5350 if (source != NULL) {
5351 cm->show_frame = 1;
5352 cm->intra_only = 0;
5353
5354 // Check to see if the frame should be encoded as an arf overlay.
5355 check_src_altref(cpi, source);
5356 }
5357 }
5358
5359 if (source) {
5360 cpi->un_scaled_source = cpi->Source =
5361 force_src_buffer ? force_src_buffer : &source->img;
5362
5363 cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
5364
5365 *time_stamp = source->ts_start;
5366 *time_end = source->ts_end;
Yaowu Xuf883b422016-08-30 14:01:10 -07005367 *frame_flags = (source->flags & AOM_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005368
5369 } else {
5370 *size = 0;
5371 if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005372 av1_end_first_pass(cpi); /* get last stats packet */
Yaowu Xuc27fc142016-08-22 16:08:15 -07005373 cpi->twopass.first_pass_done = 1;
5374 }
5375 return -1;
5376 }
5377
5378 if (source->ts_start < cpi->first_time_stamp_ever) {
5379 cpi->first_time_stamp_ever = source->ts_start;
5380 cpi->last_end_time_stamp_seen = source->ts_start;
5381 }
5382
5383 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07005384 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005385
5386 // adjust frame rates based on timestamps given
5387 if (cm->show_frame) adjust_frame_rate(cpi, source);
5388
5389 // Find a free buffer for the new frame, releasing the reference previously
5390 // held.
5391 if (cm->new_fb_idx != INVALID_IDX) {
5392 --pool->frame_bufs[cm->new_fb_idx].ref_count;
5393 }
5394 cm->new_fb_idx = get_free_fb(cm);
5395
5396 if (cm->new_fb_idx == INVALID_IDX) return -1;
5397
5398 cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
5399
5400#if CONFIG_EXT_REFS
5401 if (oxcf->pass == 2) {
5402 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5403 cpi->alt_fb_idx = cpi->arf_map[gf_group->arf_ref_idx[gf_group->index]];
5404 }
5405#else
5406 if (cpi->multi_arf_allowed) {
5407 if (cm->frame_type == KEY_FRAME) {
5408 init_buffer_indices(cpi);
5409 } else if (oxcf->pass == 2) {
5410 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5411 cpi->alt_fb_idx = gf_group->arf_ref_idx[gf_group->index];
5412 }
5413 }
5414#endif
5415 // Start with a 0 size frame.
5416 *size = 0;
5417
5418 cpi->frame_flags = *frame_flags;
5419
5420 if (oxcf->pass == 2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005421 av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005422 } else if (oxcf->pass == 1) {
5423 set_frame_size(cpi);
5424 }
5425
5426 if (cpi->oxcf.pass != 0 || frame_is_intra_only(cm) == 1) {
5427 for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i)
5428 cpi->scaled_ref_idx[i] = INVALID_IDX;
5429 }
5430
5431#if CONFIG_AOM_QM
5432 cm->using_qmatrix = cpi->oxcf.using_qm;
5433 cm->min_qmlevel = cpi->oxcf.qm_minlevel;
5434 cm->max_qmlevel = cpi->oxcf.qm_maxlevel;
5435#endif
5436
5437 if (oxcf->pass == 1) {
5438 cpi->td.mb.e_mbd.lossless[0] = is_lossless_requested(oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07005439 av1_first_pass(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005440 } else if (oxcf->pass == 2) {
5441 Pass2Encode(cpi, size, dest, frame_flags);
5442 } else {
5443 // One pass encode
5444 Pass0Encode(cpi, size, dest, frame_flags);
5445 }
5446
5447 if (!cm->error_resilient_mode)
5448 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
5449
5450 // No frame encoded, or frame was dropped, release scaled references.
5451 if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
5452 release_scaled_references(cpi);
5453 }
5454
5455 if (*size > 0) {
5456 cpi->droppable = !frame_is_reference(cpi);
5457 }
5458
Yaowu Xuf883b422016-08-30 14:01:10 -07005459 aom_usec_timer_mark(&cmptimer);
5460 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005461
5462 if (cpi->b_calculate_psnr && oxcf->pass != 1 && cm->show_frame)
5463 generate_psnr_packet(cpi);
5464
5465#if CONFIG_INTERNAL_STATS
5466 if (oxcf->pass != 1) {
5467 compute_internal_stats(cpi);
5468 cpi->bytes += (int)(*size);
5469 }
5470#endif // CONFIG_INTERNAL_STATS
5471
Yaowu Xuf883b422016-08-30 14:01:10 -07005472 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005473
5474 return 0;
5475}
5476
Yaowu Xuf883b422016-08-30 14:01:10 -07005477int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
5478 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005479 if (!cm->show_frame) {
5480 return -1;
5481 } else {
5482 int ret;
5483 if (cm->frame_to_show) {
5484 *dest = *cm->frame_to_show;
5485 dest->y_width = cm->width;
5486 dest->y_height = cm->height;
5487 dest->uv_width = cm->width >> cm->subsampling_x;
5488 dest->uv_height = cm->height >> cm->subsampling_y;
5489 ret = 0;
5490 } else {
5491 ret = -1;
5492 }
Yaowu Xuf883b422016-08-30 14:01:10 -07005493 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005494 return ret;
5495 }
5496}
5497
Yaowu Xuf883b422016-08-30 14:01:10 -07005498int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005499 if (cpi->last_show_frame_buf_idx == INVALID_IDX) return -1;
5500
5501 *frame =
5502 cpi->common.buffer_pool->frame_bufs[cpi->last_show_frame_buf_idx].buf;
5503 return 0;
5504}
5505
Yaowu Xuf883b422016-08-30 14:01:10 -07005506int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
5507 AOM_SCALING vert_mode) {
5508 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005509 int hr = 0, hs = 0, vr = 0, vs = 0;
5510
5511 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
5512
5513 Scale2Ratio(horiz_mode, &hr, &hs);
5514 Scale2Ratio(vert_mode, &vr, &vs);
5515
5516 // always go to the next whole number
5517 cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
5518 cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
5519 assert(cm->width <= cpi->initial_width);
5520 assert(cm->height <= cpi->initial_height);
5521
5522 update_frame_size(cpi);
5523
5524 return 0;
5525}
5526
Yaowu Xuf883b422016-08-30 14:01:10 -07005527int av1_set_size_literal(AV1_COMP *cpi, unsigned int width,
5528 unsigned int height) {
5529 AV1_COMMON *cm = &cpi->common;
5530#if CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005531 check_initial_width(cpi, cm->use_highbitdepth, 1, 1);
5532#else
5533 check_initial_width(cpi, 1, 1);
Yaowu Xuf883b422016-08-30 14:01:10 -07005534#endif // CONFIG_AOM_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005535
5536 if (width) {
5537 cm->width = width;
5538 if (cm->width > cpi->initial_width) {
5539 cm->width = cpi->initial_width;
5540 printf("Warning: Desired width too large, changed to %d\n", cm->width);
5541 }
5542 }
5543
5544 if (height) {
5545 cm->height = height;
5546 if (cm->height > cpi->initial_height) {
5547 cm->height = cpi->initial_height;
5548 printf("Warning: Desired height too large, changed to %d\n", cm->height);
5549 }
5550 }
5551 assert(cm->width <= cpi->initial_width);
5552 assert(cm->height <= cpi->initial_height);
5553
5554 update_frame_size(cpi);
5555
5556 return 0;
5557}
5558
Yaowu Xuf883b422016-08-30 14:01:10 -07005559int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005560
Yaowu Xuf883b422016-08-30 14:01:10 -07005561void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005562 if (flags &
Yaowu Xuf883b422016-08-30 14:01:10 -07005563 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF)) {
5564 int ref = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005565
Yaowu Xuf883b422016-08-30 14:01:10 -07005566 if (flags & AOM_EFLAG_NO_REF_LAST) {
5567 ref ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005568#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07005569 ref ^= AOM_LAST2_FLAG;
5570 ref ^= AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005571#endif // CONFIG_EXT_REFS
5572 }
5573
Yaowu Xuf883b422016-08-30 14:01:10 -07005574 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005575
Yaowu Xuf883b422016-08-30 14:01:10 -07005576 if (flags & AOM_EFLAG_NO_REF_ARF) ref ^= AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005577
Yaowu Xuf883b422016-08-30 14:01:10 -07005578 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005579 }
5580
5581 if (flags &
Yaowu Xuf883b422016-08-30 14:01:10 -07005582 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF |
5583 AOM_EFLAG_FORCE_GF | AOM_EFLAG_FORCE_ARF)) {
5584 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005585
Yaowu Xuf883b422016-08-30 14:01:10 -07005586 if (flags & AOM_EFLAG_NO_UPD_LAST) {
5587 upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005588#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07005589 upd ^= AOM_LAST2_FLAG;
5590 upd ^= AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005591#endif // CONFIG_EXT_REFS
5592 }
5593
Yaowu Xuf883b422016-08-30 14:01:10 -07005594 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005595
Yaowu Xuf883b422016-08-30 14:01:10 -07005596 if (flags & AOM_EFLAG_NO_UPD_ARF) upd ^= AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005597
Yaowu Xuf883b422016-08-30 14:01:10 -07005598 av1_update_reference(cpi, upd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005599 }
5600
Yaowu Xuf883b422016-08-30 14:01:10 -07005601 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
5602 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005603 }
5604}