blob: 99d3c5f8f82faa1a998f8b524dc3431172e1c1b4 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Urvang Joshi8a02d762016-07-28 15:51:12 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Urvang Joshi8a02d762016-07-28 15:51:12 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <limits.h>
13#include <math.h>
14#include <stdio.h>
15
Tom Finegan60e653d2018-05-22 11:34:58 -070016#include "config/aom_config.h"
Tom Finegan44702c82018-05-22 13:00:39 -070017#include "config/aom_dsp_rtcd.h"
18#include "config/aom_scale_rtcd.h"
Yaowu Xufa3721d2018-07-30 14:38:49 -070019#include "config/av1_rtcd.h"
20
21#include "aom_dsp/aom_dsp_common.h"
22#include "aom_dsp/aom_filter.h"
23#if CONFIG_DENOISE
24#include "aom_dsp/grain_table.h"
25#include "aom_dsp/noise_util.h"
26#include "aom_dsp/noise_model.h"
27#endif
28#include "aom_dsp/psnr.h"
29#if CONFIG_INTERNAL_STATS
30#include "aom_dsp/ssim.h"
31#endif
32#include "aom_ports/aom_timer.h"
33#include "aom_ports/mem.h"
34#include "aom_ports/system_state.h"
35#include "aom_scale/aom_scale.h"
36#if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
37#include "aom_util/debug_util.h"
38#endif // CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070039
40#include "av1/common/alloccommon.h"
Steinar Midtskogena9d41e82017-03-17 12:48:15 +010041#include "av1/common/cdef.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070042#include "av1/common/filter.h"
43#include "av1/common/idct.h"
44#include "av1/common/reconinter.h"
45#include "av1/common/reconintra.h"
Fergus Simpsond0565002017-03-27 16:51:52 -070046#include "av1/common/resize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070047#include "av1/common/tile_common.h"
48
Ravi Chaudharyc5e74692018-10-08 16:05:38 +053049#include "av1/encoder/av1_multi_thread.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070050#include "av1/encoder/aq_complexity.h"
51#include "av1/encoder/aq_cyclicrefresh.h"
52#include "av1/encoder/aq_variance.h"
53#include "av1/encoder/bitstream.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070054#include "av1/encoder/context_tree.h"
55#include "av1/encoder/encodeframe.h"
56#include "av1/encoder/encodemv.h"
57#include "av1/encoder/encoder.h"
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070058#include "av1/encoder/encodetxb.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070059#include "av1/encoder/ethread.h"
60#include "av1/encoder/firstpass.h"
Yaowu Xufa3721d2018-07-30 14:38:49 -070061#include "av1/encoder/grain_test_vectors.h"
RogerZhoucc5d35d2017-08-07 22:20:15 -070062#include "av1/encoder/hash_motion.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070063#include "av1/encoder/mbgraph.h"
64#include "av1/encoder/picklpf.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070065#include "av1/encoder/pickrst.h"
Debargha Mukherjee7166f222017-09-05 21:32:42 -070066#include "av1/encoder/random.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070067#include "av1/encoder/ratectrl.h"
68#include "av1/encoder/rd.h"
Debargha Mukherjeedf713102018-10-02 12:33:32 -070069#include "av1/encoder/rdopt.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070070#include "av1/encoder/segmentation.h"
71#include "av1/encoder/speed_features.h"
72#include "av1/encoder/temporal_filter.h"
Yue Chen7cae98f2018-08-24 10:43:16 -070073#include "av1/encoder/reconinter_enc.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070074
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000075#define DEFAULT_EXPLICIT_ORDER_HINT_BITS 7
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000076
Andrey Norkin795ba872018-03-06 13:24:14 -080077// av1 uses 10,000,000 ticks/second as time stamp
78#define TICKS_PER_SEC 10000000LL
Andrey Norkin795ba872018-03-06 13:24:14 -080079
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080080#if CONFIG_ENTROPY_STATS
81FRAME_COUNTS aggregate_fc;
82#endif // CONFIG_ENTROPY_STATS
83
Yaowu Xuc27fc142016-08-22 16:08:15 -070084#define AM_SEGMENT_ID_INACTIVE 7
85#define AM_SEGMENT_ID_ACTIVE 0
86
Johannb0ef6ff2018-02-08 14:32:21 -080087// Whether to use high precision mv for altref computation.
88#define ALTREF_HIGH_PRECISION_MV 1
89
90// Q threshold for high precision mv. Choose a very high value for now so that
91// HIGH_PRECISION is always chosen.
92#define HIGH_PRECISION_MV_QTHRESH 200
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -070093
Yaowu Xuc27fc142016-08-22 16:08:15 -070094// #define OUTPUT_YUV_REC
Yaowu Xuc27fc142016-08-22 16:08:15 -070095#ifdef OUTPUT_YUV_SKINMAP
96FILE *yuv_skinmap_file = NULL;
97#endif
98#ifdef OUTPUT_YUV_REC
99FILE *yuv_rec_file;
100#define FILE_NAME_LEN 100
101#endif
102
Debargha Mukherjeedf713102018-10-02 12:33:32 -0700103// Estimate if the source frame is screen content, based on the portion of
104// blocks that have no more than 4 (experimentally selected) luma colors.
105static int is_screen_content(const uint8_t *src, int use_hbd, int bd,
106 int stride, int width, int height) {
107 assert(src != NULL);
108 int counts = 0;
109 const int blk_w = 16;
110 const int blk_h = 16;
111 const int limit = 4;
112 for (int r = 0; r + blk_h <= height; r += blk_h) {
113 for (int c = 0; c + blk_w <= width; c += blk_w) {
114 int count_buf[1 << 12]; // Maximum (1 << 12) color levels.
115 const int n_colors =
116 use_hbd ? av1_count_colors_highbd(src + r * stride + c, stride, blk_w,
117 blk_h, bd, count_buf)
118 : av1_count_colors(src + r * stride + c, stride, blk_w, blk_h,
119 count_buf);
120 if (n_colors > 1 && n_colors <= limit) counts++;
121 }
122 }
123 // The threshold is 10%.
124 return counts * blk_h * blk_w * 10 > width * height;
125}
126
Yaowu Xuf883b422016-08-30 14:01:10 -0700127static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700128 switch (mode) {
129 case NORMAL:
130 *hr = 1;
131 *hs = 1;
132 break;
133 case FOURFIVE:
134 *hr = 4;
135 *hs = 5;
136 break;
137 case THREEFIVE:
138 *hr = 3;
139 *hs = 5;
140 break;
141 case ONETWO:
142 *hr = 1;
143 *hs = 2;
144 break;
145 default:
146 *hr = 1;
147 *hs = 1;
148 assert(0);
149 break;
150 }
151}
152
153// Mark all inactive blocks as active. Other segmentation features may be set
154// so memset cannot be used, instead only inactive blocks should be reset.
Yaowu Xuf883b422016-08-30 14:01:10 -0700155static void suppress_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700156 unsigned char *const seg_map = cpi->segmentation_map;
157 int i;
158 if (cpi->active_map.enabled || cpi->active_map.update)
159 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
160 if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
161 seg_map[i] = AM_SEGMENT_ID_ACTIVE;
162}
163
Yaowu Xuf883b422016-08-30 14:01:10 -0700164static void apply_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700165 struct segmentation *const seg = &cpi->common.seg;
166 unsigned char *const seg_map = cpi->segmentation_map;
167 const unsigned char *const active_map = cpi->active_map.map;
168 int i;
169
170 assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
171
172 if (frame_is_intra_only(&cpi->common)) {
173 cpi->active_map.enabled = 0;
174 cpi->active_map.update = 1;
175 }
176
177 if (cpi->active_map.update) {
178 if (cpi->active_map.enabled) {
179 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
180 if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
Yaowu Xuf883b422016-08-30 14:01:10 -0700181 av1_enable_segmentation(seg);
182 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700183 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
184 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
185 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
186 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
187
188 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H,
189 -MAX_LOOP_FILTER);
190 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V,
191 -MAX_LOOP_FILTER);
192 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U,
193 -MAX_LOOP_FILTER);
194 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V,
195 -MAX_LOOP_FILTER);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700196 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700197 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700198 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
199 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
200 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
201 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700202 if (seg->enabled) {
203 seg->update_data = 1;
204 seg->update_map = 1;
205 }
206 }
207 cpi->active_map.update = 0;
208 }
209}
210
Yaowu Xuf883b422016-08-30 14:01:10 -0700211int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
212 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700213 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
214 unsigned char *const active_map_8x8 = cpi->active_map.map;
215 const int mi_rows = cpi->common.mi_rows;
216 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700217 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
218 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700219 cpi->active_map.update = 1;
220 if (new_map_16x16) {
221 int r, c;
222 for (r = 0; r < mi_rows; ++r) {
223 for (c = 0; c < mi_cols; ++c) {
224 active_map_8x8[r * mi_cols + c] =
Jingning Han9d533022017-04-07 10:14:42 -0700225 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700226 ? AM_SEGMENT_ID_ACTIVE
227 : AM_SEGMENT_ID_INACTIVE;
228 }
229 }
230 cpi->active_map.enabled = 1;
231 } else {
232 cpi->active_map.enabled = 0;
233 }
234 return 0;
235 } else {
236 return -1;
237 }
238}
239
Yaowu Xuf883b422016-08-30 14:01:10 -0700240int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
241 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700242 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
243 new_map_16x16) {
244 unsigned char *const seg_map_8x8 = cpi->segmentation_map;
245 const int mi_rows = cpi->common.mi_rows;
246 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700247 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
248 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
249
Yaowu Xuc27fc142016-08-22 16:08:15 -0700250 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
251 if (cpi->active_map.enabled) {
252 int r, c;
253 for (r = 0; r < mi_rows; ++r) {
254 for (c = 0; c < mi_cols; ++c) {
255 // Cyclic refresh segments are considered active despite not having
256 // AM_SEGMENT_ID_ACTIVE
Jingning Han9d533022017-04-07 10:14:42 -0700257 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
Yaowu Xuc27fc142016-08-22 16:08:15 -0700258 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
259 }
260 }
261 }
262 return 0;
263 } else {
264 return -1;
265 }
266}
267
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800268// Compute the horizontal frequency component energy in a frame
269// by calucluating the 16x4 Horizontal DCT. This will be subsequently
270// used to decide the superresolution factors.
271void analyze_hor_freq(const AV1_COMP *cpi, double *energy) {
272 uint64_t freq_energy[16] = { 0 };
273 const YV12_BUFFER_CONFIG *buf = cpi->source;
274 const int bd = cpi->td.mb.e_mbd.bd;
275 const int width = buf->y_crop_width;
276 const int height = buf->y_crop_height;
277 int32_t coeff[16 * 4];
278 int n = 0;
279 if (buf->flags & YV12_FLAG_HIGHBITDEPTH) {
280 const int16_t *src16 = (const int16_t *)CONVERT_TO_SHORTPTR(buf->y_buffer);
281 for (int i = 0; i < height - 4; i += 4) {
282 for (int j = 0; j < width - 16; j += 16) {
283 av1_fwd_txfm2d_16x4(src16 + i * buf->y_stride + j, coeff, buf->y_stride,
284 H_DCT, bd);
285 for (int k = 4; k < 16; ++k) {
286 const int64_t en =
287 coeff[k] * coeff[k] + coeff[k + 16] * coeff[k + 16] +
288 coeff[k + 32] * coeff[k + 32] + coeff[k + 48] * coeff[k + 48];
289 freq_energy[k] += ROUND_POWER_OF_TWO(en, 2 * (bd - 8));
290 }
291 n++;
292 }
293 }
294 } else {
295 int16_t src16[16 * 4];
296 for (int i = 0; i < height - 4; i += 4) {
297 for (int j = 0; j < width - 16; j += 16) {
298 for (int ii = 0; ii < 4; ++ii)
299 for (int jj = 0; jj < 16; ++jj)
300 src16[ii * 16 + jj] =
301 buf->y_buffer[(i + ii) * buf->y_stride + (j + jj)];
302 av1_fwd_txfm2d_16x4(src16, coeff, buf->y_stride, H_DCT, bd);
303 for (int k = 4; k < 16; ++k) {
304 const int64_t en =
305 coeff[k] * coeff[k] + coeff[k + 16] * coeff[k + 16] +
306 coeff[k + 32] * coeff[k + 32] + coeff[k + 48] * coeff[k + 48];
307 freq_energy[k] += ROUND_POWER_OF_TWO(en, 2 * (bd - 8));
308 }
309 n++;
310 }
311 }
312 }
313 for (int k = 4; k < 16; ++k) energy[k] = (double)freq_energy[k] / n;
314}
315
Yaowu Xu45295c32018-03-29 12:06:10 -0700316static void set_high_precision_mv(AV1_COMP *cpi, int allow_high_precision_mv,
317 int cur_frame_force_integer_mv) {
James Zern01a9d702017-08-25 19:09:33 +0000318 MACROBLOCK *const mb = &cpi->td.mb;
Hui Su50361152018-03-02 11:01:42 -0800319 cpi->common.allow_high_precision_mv =
320 allow_high_precision_mv && cur_frame_force_integer_mv == 0;
Rupert Swarbricka84faf22017-12-11 13:56:40 +0000321 const int copy_hp =
322 cpi->common.allow_high_precision_mv && cur_frame_force_integer_mv == 0;
Jingning Hanf050fc12018-03-09 14:53:33 -0800323 int *(*src)[2] = copy_hp ? &mb->nmvcost_hp : &mb->nmvcost;
324 mb->mv_cost_stack = *src;
James Zern01a9d702017-08-25 19:09:33 +0000325}
326
Yaowu Xuf883b422016-08-30 14:01:10 -0700327static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
Urvang Joshie4530f82018-01-09 11:43:37 -0800328 const AV1_COMMON *const cm = &cpi->common;
329
Yaowu Xuf883b422016-08-30 14:01:10 -0700330 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700331 return BLOCK_64X64;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100332#if CONFIG_FILEOPTIONS
Urvang Joshie4530f82018-01-09 11:43:37 -0800333 if (cm->options && cm->options->ext_partition)
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100334#endif
335 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
336 return BLOCK_128X128;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700337
Yaowu Xuf883b422016-08-30 14:01:10 -0700338 assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700339
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100340// TODO(any): Possibly could improve this with a heuristic.
341#if CONFIG_FILEOPTIONS
Urvang Joshie4530f82018-01-09 11:43:37 -0800342 if (cm->options && !cm->options->ext_partition) return BLOCK_64X64;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100343#endif
Urvang Joshie4530f82018-01-09 11:43:37 -0800344
Urvang Joshiaab74432018-06-01 12:06:22 -0700345 // When superres / resize is on, 'cm->width / height' can change between
346 // calls, so we don't apply this heuristic there. Also, this heuristic gives
347 // compression gain for speed >= 2 only.
348 if (cpi->oxcf.superres_mode == SUPERRES_NONE &&
349 cpi->oxcf.resize_mode == RESIZE_NONE && cpi->oxcf.speed >= 2) {
Urvang Joshie4530f82018-01-09 11:43:37 -0800350 return (cm->width >= 480 && cm->height >= 360) ? BLOCK_128X128
351 : BLOCK_64X64;
352 }
353
Yaowu Xuc27fc142016-08-22 16:08:15 -0700354 return BLOCK_128X128;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700355}
356
Yaowu Xuf883b422016-08-30 14:01:10 -0700357static void setup_frame(AV1_COMP *cpi) {
358 AV1_COMMON *const cm = &cpi->common;
Johannb0ef6ff2018-02-08 14:32:21 -0800359 // Set up entropy context depending on frame type. The decoder mandates
360 // the use of the default context, index 0, for keyframes and inter
361 // frames where the error_resilient_mode or intra_only flag is set. For
362 // other inter-frames the encoder currently uses only two contexts;
363 // context 1 for ALTREF frames and context 0 for the others.
Soo-Chul Han85e8c792018-01-21 01:58:15 -0500364
Thomas Daede51020e12017-12-14 20:12:44 -0800365 cm->primary_ref_frame = PRIMARY_REF_NONE;
Sarah Parker50b6d6e2018-04-11 19:21:54 -0700366 if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
367 cm->force_primary_ref_none) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700368 av1_setup_past_independence(cm);
Thomas Daede51020e12017-12-14 20:12:44 -0800369 for (int i = 0; i < REF_FRAMES; i++) {
370 cm->fb_of_context_type[i] = -1;
371 }
372 cm->fb_of_context_type[REGULAR_FRAME] =
Sarah Parkerb9041612018-05-22 19:06:47 -0700373 cm->show_frame ? get_ref_frame_map_idx(cpi, GOLDEN_FRAME)
374 : get_ref_frame_map_idx(cpi, ALTREF_FRAME);
Sarah Parker167546f2018-04-24 12:14:45 -0700375 cm->frame_context_idx = REGULAR_FRAME;
Yunqing Wang19aefd12018-05-14 15:38:57 -0700376 } else {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700377 const GF_GROUP *gf_group = &cpi->twopass.gf_group;
Zoe Liue9b15e22017-07-19 15:53:01 -0700378 if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700379 cm->frame_context_idx = EXT_ARF_FRAME;
380 else if (cpi->refresh_alt_ref_frame)
381 cm->frame_context_idx = ARF_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700382 else if (cpi->rc.is_src_frame_alt_ref)
383 cm->frame_context_idx = OVERLAY_FRAME;
384 else if (cpi->refresh_golden_frame)
385 cm->frame_context_idx = GLD_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700386 else if (cpi->refresh_bwd_ref_frame)
387 cm->frame_context_idx = BRF_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700388 else
389 cm->frame_context_idx = REGULAR_FRAME;
Thomas Daede51020e12017-12-14 20:12:44 -0800390 int wanted_fb = cm->fb_of_context_type[cm->frame_context_idx];
391 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
392 int fb = get_ref_frame_map_idx(cpi, ref_frame);
393 if (fb == wanted_fb) {
394 cm->primary_ref_frame = ref_frame - LAST_FRAME;
395 }
396 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700397 }
398
Sarah Parkerb9041612018-05-22 19:06:47 -0700399 if (cm->frame_type == KEY_FRAME && cm->show_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700400 cpi->refresh_golden_frame = 1;
401 cpi->refresh_alt_ref_frame = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700402 av1_zero(cpi->interp_filter_selected);
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000403 set_sb_size(&cm->seq_params, select_sb_size(cpi));
Debargha Mukherjee778023d2017-09-26 17:50:27 -0700404 set_use_reference_buffer(cm, 0);
Tarek AMARAc9813852018-03-05 18:40:18 -0500405 } else if (frame_is_sframe(cm)) {
406 cpi->refresh_golden_frame = 1;
407 cpi->refresh_alt_ref_frame = 1;
408 av1_zero(cpi->interp_filter_selected);
409 set_sb_size(&cm->seq_params, select_sb_size(cpi));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700410 } else {
Thomas Daede51020e12017-12-14 20:12:44 -0800411 if (cm->primary_ref_frame == PRIMARY_REF_NONE ||
412 cm->frame_refs[cm->primary_ref_frame].idx < 0) {
David Barkercc615a82018-03-19 14:38:51 +0000413 av1_setup_past_independence(cm);
414 cm->seg.update_map = 1;
415 cm->seg.update_data = 1;
Thomas Daededa4d8b92017-06-05 15:44:14 -0700416 } else {
Thomas Daede51020e12017-12-14 20:12:44 -0800417 *cm->fc = cm->frame_contexts[cm->frame_refs[cm->primary_ref_frame].idx];
Thomas Daededa4d8b92017-06-05 15:44:14 -0700418 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700419 av1_zero(cpi->interp_filter_selected[0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700420 }
421
Yue Chend90d3432018-03-16 11:28:42 -0700422 cm->prev_frame = get_prev_frame(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700423 cpi->vaq_refresh = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700424}
425
Cheng Chen46f30c72017-09-07 11:13:33 -0700426static void enc_setup_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700427 int i;
Ravi Chaudhary75c4c5f2018-07-17 16:32:08 +0530428 int mi_rows_sb_aligned = calc_mi_size(cm->mi_rows);
Yunqing Wang19b9f722018-02-20 16:22:01 -0800429 cm->mi = cm->mip;
Ravi Chaudhary75c4c5f2018-07-17 16:32:08 +0530430 memset(cm->mip, 0, cm->mi_stride * mi_rows_sb_aligned * sizeof(*cm->mip));
Yunqing Wang19b9f722018-02-20 16:22:01 -0800431 cm->prev_mi = cm->prev_mip;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700432 // Clear top border row
433 memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
434 // Clear left border column
Ravi Chaudhary75c4c5f2018-07-17 16:32:08 +0530435 for (i = 0; i < mi_rows_sb_aligned; ++i)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700436 memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
Yunqing Wang19b9f722018-02-20 16:22:01 -0800437 cm->mi_grid_visible = cm->mi_grid_base;
438 cm->prev_mi_grid_visible = cm->prev_mi_grid_base;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700439
440 memset(cm->mi_grid_base, 0,
Ravi Chaudhary75c4c5f2018-07-17 16:32:08 +0530441 cm->mi_stride * mi_rows_sb_aligned * sizeof(*cm->mi_grid_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700442}
443
Cheng Chen46f30c72017-09-07 11:13:33 -0700444static int enc_alloc_mi(AV1_COMMON *cm, int mi_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700445 cm->mip = aom_calloc(mi_size, sizeof(*cm->mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700446 if (!cm->mip) return 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700447 cm->prev_mip = aom_calloc(mi_size, sizeof(*cm->prev_mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700448 if (!cm->prev_mip) return 1;
449 cm->mi_alloc_size = mi_size;
450
Yue Chen53b53f02018-03-29 14:31:23 -0700451 cm->mi_grid_base =
452 (MB_MODE_INFO **)aom_calloc(mi_size, sizeof(MB_MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700453 if (!cm->mi_grid_base) return 1;
454 cm->prev_mi_grid_base =
Yue Chen53b53f02018-03-29 14:31:23 -0700455 (MB_MODE_INFO **)aom_calloc(mi_size, sizeof(MB_MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700456 if (!cm->prev_mi_grid_base) return 1;
457
458 return 0;
459}
460
Cheng Chen46f30c72017-09-07 11:13:33 -0700461static void enc_free_mi(AV1_COMMON *cm) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700462 aom_free(cm->mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700463 cm->mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700464 aom_free(cm->prev_mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700465 cm->prev_mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700466 aom_free(cm->mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700467 cm->mi_grid_base = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700468 aom_free(cm->prev_mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700469 cm->prev_mi_grid_base = NULL;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700470 cm->mi_alloc_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700471}
472
Cheng Chen46f30c72017-09-07 11:13:33 -0700473static void swap_mi_and_prev_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700474 // Current mip will be the prev_mip for the next frame.
Yue Chen53b53f02018-03-29 14:31:23 -0700475 MB_MODE_INFO **temp_base = cm->prev_mi_grid_base;
476 MB_MODE_INFO *temp = cm->prev_mip;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700477 cm->prev_mip = cm->mip;
478 cm->mip = temp;
479
480 // Update the upper left visible macroblock ptrs.
Yunqing Wang19b9f722018-02-20 16:22:01 -0800481 cm->mi = cm->mip;
482 cm->prev_mi = cm->prev_mip;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700483
484 cm->prev_mi_grid_base = cm->mi_grid_base;
485 cm->mi_grid_base = temp_base;
Yunqing Wang19b9f722018-02-20 16:22:01 -0800486 cm->mi_grid_visible = cm->mi_grid_base;
487 cm->prev_mi_grid_visible = cm->prev_mi_grid_base;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700488}
489
Yaowu Xuf883b422016-08-30 14:01:10 -0700490void av1_initialize_enc(void) {
Wan-Teh Chang3cac4542018-06-29 10:21:39 -0700491 av1_rtcd();
492 aom_dsp_rtcd();
493 aom_scale_rtcd();
494 av1_init_intra_predictors();
495 av1_init_me_luts();
496 av1_rc_init_minq_luts();
497 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700498}
499
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700500static void dealloc_context_buffers_ext(AV1_COMP *cpi) {
501 if (cpi->mbmi_ext_base) {
502 aom_free(cpi->mbmi_ext_base);
503 cpi->mbmi_ext_base = NULL;
504 }
505}
506
507static void alloc_context_buffers_ext(AV1_COMP *cpi) {
508 AV1_COMMON *cm = &cpi->common;
509 int mi_size = cm->mi_cols * cm->mi_rows;
510
511 dealloc_context_buffers_ext(cpi);
512 CHECK_MEM_ERROR(cm, cpi->mbmi_ext_base,
513 aom_calloc(mi_size, sizeof(*cpi->mbmi_ext_base)));
514}
515
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800516static void update_film_grain_parameters(struct AV1_COMP *cpi,
517 const AV1EncoderConfig *oxcf) {
518 AV1_COMMON *const cm = &cpi->common;
519 cpi->oxcf = *oxcf;
520
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700521 if (cpi->film_grain_table) {
522 aom_film_grain_table_free(cpi->film_grain_table);
523 aom_free(cpi->film_grain_table);
524 cpi->film_grain_table = NULL;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700525 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700526
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800527 if (oxcf->film_grain_test_vector) {
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700528 cm->seq_params.film_grain_params_present = 1;
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800529 if (cm->frame_type == KEY_FRAME) {
530 memcpy(&cm->film_grain_params,
531 film_grain_test_vectors + oxcf->film_grain_test_vector - 1,
532 sizeof(cm->film_grain_params));
533
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700534 cm->film_grain_params.bit_depth = cm->seq_params.bit_depth;
535 if (cm->seq_params.color_range == AOM_CR_FULL_RANGE) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800536 cm->film_grain_params.clip_to_restricted_range = 0;
537 }
538 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700539 } else if (oxcf->film_grain_table_filename) {
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700540 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
541 memset(cpi->film_grain_table, 0, sizeof(aom_film_grain_table_t));
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700542
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700543 aom_film_grain_table_read(cpi->film_grain_table,
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700544 oxcf->film_grain_table_filename, &cm->error);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800545 } else {
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700546 cm->seq_params.film_grain_params_present = 0;
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800547 memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
548 }
549}
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800550
Yaowu Xuf883b422016-08-30 14:01:10 -0700551static void dealloc_compressor_data(AV1_COMP *cpi) {
552 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000553 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700554
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700555 dealloc_context_buffers_ext(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700556
Yaowu Xuf883b422016-08-30 14:01:10 -0700557 aom_free(cpi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700558 cpi->tile_data = NULL;
559
560 // Delete sementation map
Yaowu Xuf883b422016-08-30 14:01:10 -0700561 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700562 cpi->segmentation_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700563
Yaowu Xuf883b422016-08-30 14:01:10 -0700564 av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700565 cpi->cyclic_refresh = NULL;
566
Yaowu Xuf883b422016-08-30 14:01:10 -0700567 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700568 cpi->active_map.map = NULL;
569
Jingning Hand064cf02017-06-01 10:00:39 -0700570 aom_free(cpi->td.mb.above_pred_buf);
571 cpi->td.mb.above_pred_buf = NULL;
572
573 aom_free(cpi->td.mb.left_pred_buf);
574 cpi->td.mb.left_pred_buf = NULL;
575
576 aom_free(cpi->td.mb.wsrc_buf);
577 cpi->td.mb.wsrc_buf = NULL;
578
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530579#if CONFIG_COLLECT_INTER_MODE_RD_STATS
580 aom_free(cpi->td.mb.inter_modes_info);
581 cpi->td.mb.inter_modes_info = NULL;
582#endif
583
Ravi Chaudhary783d6a32018-08-28 18:21:02 +0530584 for (int i = 0; i < 2; i++)
585 for (int j = 0; j < 2; j++) {
586 aom_free(cpi->td.mb.hash_value_buffer[i][j]);
587 cpi->td.mb.hash_value_buffer[i][j] = NULL;
588 }
Jingning Hand064cf02017-06-01 10:00:39 -0700589 aom_free(cpi->td.mb.mask_buf);
590 cpi->td.mb.mask_buf = NULL;
Jingning Hand064cf02017-06-01 10:00:39 -0700591
Jingning Han6cc1fd32017-10-13 09:05:36 -0700592 aom_free(cm->tpl_mvs);
593 cm->tpl_mvs = NULL;
Jingning Han6cc1fd32017-10-13 09:05:36 -0700594
Yaowu Xuf883b422016-08-30 14:01:10 -0700595 av1_free_ref_frame_buffers(cm->buffer_pool);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700596 av1_free_txb_buf(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -0700597 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700598
Yaowu Xuf883b422016-08-30 14:01:10 -0700599 aom_free_frame_buffer(&cpi->last_frame_uf);
Yaowu Xuf883b422016-08-30 14:01:10 -0700600 av1_free_restoration_buffers(cm);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800601 aom_free_frame_buffer(&cpi->trial_frame_rst);
Yaowu Xuf883b422016-08-30 14:01:10 -0700602 aom_free_frame_buffer(&cpi->scaled_source);
603 aom_free_frame_buffer(&cpi->scaled_last_source);
604 aom_free_frame_buffer(&cpi->alt_ref_buffer);
605 av1_lookahead_destroy(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700606
Yaowu Xuf883b422016-08-30 14:01:10 -0700607 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700608 cpi->tile_tok[0][0] = 0;
609
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530610 aom_free(cpi->tplist[0][0]);
611 cpi->tplist[0][0] = NULL;
612
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000613 av1_free_pc_tree(&cpi->td, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700614
hui sud9a812b2017-07-06 14:34:37 -0700615 aom_free(cpi->td.mb.palette_buffer);
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700616
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700617 aom_free(cpi->td.mb.tmp_conv_dst);
618 for (int j = 0; j < 2; ++j) {
619 aom_free(cpi->td.mb.tmp_obmc_bufs[j]);
620 }
621
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700622#if CONFIG_DENOISE
623 if (cpi->denoise_and_model) {
624 aom_denoise_and_model_free(cpi->denoise_and_model);
625 cpi->denoise_and_model = NULL;
626 }
627#endif
628 if (cpi->film_grain_table) {
629 aom_film_grain_table_free(cpi->film_grain_table);
630 cpi->film_grain_table = NULL;
631 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700632}
633
Yaowu Xuf883b422016-08-30 14:01:10 -0700634static void save_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700635 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700636 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700637
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200638 // Stores a snapshot of key state variables which can subsequently be
639 // restored with a call to av1_restore_coding_context. These functions are
640 // intended for use in a re-code loop in av1_compress_frame where the
641 // quantizer value is adjusted between loop iterations.
Jingning Hanf050fc12018-03-09 14:53:33 -0800642 av1_copy(cc->nmv_vec_cost, cpi->td.mb.nmv_vec_cost);
643 av1_copy(cc->nmv_costs, cpi->nmv_costs);
644 av1_copy(cc->nmv_costs_hp, cpi->nmv_costs_hp);
James Zern01a9d702017-08-25 19:09:33 +0000645
Yaowu Xuc27fc142016-08-22 16:08:15 -0700646 cc->fc = *cm->fc;
647}
648
Yaowu Xuf883b422016-08-30 14:01:10 -0700649static void restore_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700650 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700651 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700652
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200653 // Restore key state variables to the snapshot state stored in the
654 // previous call to av1_save_coding_context.
Jingning Hanf050fc12018-03-09 14:53:33 -0800655 av1_copy(cpi->td.mb.nmv_vec_cost, cc->nmv_vec_cost);
656 av1_copy(cpi->nmv_costs, cc->nmv_costs);
657 av1_copy(cpi->nmv_costs_hp, cc->nmv_costs_hp);
James Zern01a9d702017-08-25 19:09:33 +0000658
Yaowu Xuc27fc142016-08-22 16:08:15 -0700659 *cm->fc = cc->fc;
660}
661
Yaowu Xuf883b422016-08-30 14:01:10 -0700662static void configure_static_seg_features(AV1_COMP *cpi) {
663 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700664 const RATE_CONTROL *const rc = &cpi->rc;
665 struct segmentation *const seg = &cm->seg;
666
667 int high_q = (int)(rc->avg_q > 48.0);
668 int qi_delta;
669
670 // Disable and clear down for KF
671 if (cm->frame_type == KEY_FRAME) {
672 // Clear down the global segmentation map
673 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
674 seg->update_map = 0;
675 seg->update_data = 0;
676 cpi->static_mb_pct = 0;
677
678 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700679 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700680
681 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700682 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700683 } else if (cpi->refresh_alt_ref_frame) {
684 // If this is an alt ref frame
685 // Clear down the global segmentation map
686 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
687 seg->update_map = 0;
688 seg->update_data = 0;
689 cpi->static_mb_pct = 0;
690
691 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700692 av1_disable_segmentation(seg);
693 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700694
695 // Scan frames from current to arf frame.
696 // This function re-enables segmentation if appropriate.
Yaowu Xuf883b422016-08-30 14:01:10 -0700697 av1_update_mbgraph_stats(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700698
699 // If segmentation was enabled set those features needed for the
700 // arf itself.
701 if (seg->enabled) {
702 seg->update_map = 1;
703 seg->update_data = 1;
704
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700705 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
706 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700707 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
Cheng Chend8184da2017-09-26 18:15:22 -0700708 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
709 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
710 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
711 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
712
713 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
714 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
715 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
716 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700717
Yaowu Xuf883b422016-08-30 14:01:10 -0700718 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700719 }
720 } else if (seg->enabled) {
721 // All other frames if segmentation has been enabled
722
723 // First normal frame in a valid gf or alt ref group
724 if (rc->frames_since_golden == 0) {
725 // Set up segment features for normal frames in an arf group
726 if (rc->source_alt_ref_active) {
727 seg->update_map = 0;
728 seg->update_data = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700729
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700730 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
731 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700732 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
733 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700734
Cheng Chend8184da2017-09-26 18:15:22 -0700735 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
736 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
737 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
738 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
739
740 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
741 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
742 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
743 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700744
745 // Segment coding disabled for compred testing
746 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700747 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
748 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
749 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700750 }
751 } else {
752 // Disable segmentation and clear down features if alt ref
753 // is not active for this group
754
Yaowu Xuf883b422016-08-30 14:01:10 -0700755 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700756
757 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
758
759 seg->update_map = 0;
760 seg->update_data = 0;
761
Yaowu Xuf883b422016-08-30 14:01:10 -0700762 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700763 }
764 } else if (rc->is_src_frame_alt_ref) {
765 // Special case where we are coding over the top of a previous
766 // alt ref frame.
767 // Segment coding disabled for compred testing
768
769 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -0700770 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
771 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700772
773 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -0700774 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
775 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
776 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
777 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700778
779 // Skip all MBs if high Q (0,0 mv and skip coeffs)
780 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700781 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
782 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700783 }
784 // Enable data update
785 seg->update_data = 1;
786 } else {
787 // All other frames.
788
789 // No updates.. leave things as they are.
790 seg->update_map = 0;
791 seg->update_data = 0;
792 }
793 }
794}
795
Yaowu Xuf883b422016-08-30 14:01:10 -0700796static void update_reference_segmentation_map(AV1_COMP *cpi) {
797 AV1_COMMON *const cm = &cpi->common;
Yushin Choa7f65922018-04-04 16:06:11 -0700798 MB_MODE_INFO **mi_4x4_ptr = cm->mi_grid_visible;
Soo-Chul Han934af352017-10-15 15:21:51 -0400799 uint8_t *cache_ptr = cm->current_frame_seg_map;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700800 int row, col;
801
802 for (row = 0; row < cm->mi_rows; row++) {
Yushin Choa7f65922018-04-04 16:06:11 -0700803 MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700804 uint8_t *cache = cache_ptr;
Yushin Choa7f65922018-04-04 16:06:11 -0700805 for (col = 0; col < cm->mi_cols; col++, mi_4x4++, cache++)
806 cache[0] = mi_4x4[0]->segment_id;
807 mi_4x4_ptr += cm->mi_stride;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700808 cache_ptr += cm->mi_cols;
809 }
810}
811
Yaowu Xuf883b422016-08-30 14:01:10 -0700812static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
813 AV1_COMMON *cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700814 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuf883b422016-08-30 14:01:10 -0700815 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700816
817 if (!cpi->lookahead)
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700818 cpi->lookahead =
819 av1_lookahead_init(oxcf->width, oxcf->height, seq_params->subsampling_x,
820 seq_params->subsampling_y,
821 seq_params->use_highbitdepth, oxcf->lag_in_frames);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700822 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700823 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700824 "Failed to allocate lag buffers");
825
826 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700827 if (aom_realloc_frame_buffer(
828 &cpi->alt_ref_buffer, oxcf->width, oxcf->height,
829 seq_params->subsampling_x, seq_params->subsampling_y,
830 seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS,
831 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700832 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700833 "Failed to allocate altref buffer");
834}
835
Yaowu Xuf883b422016-08-30 14:01:10 -0700836static void alloc_util_frame_buffers(AV1_COMP *cpi) {
837 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700838 const SequenceHeader *const seq_params = &cm->seq_params;
839 if (aom_realloc_frame_buffer(
840 &cpi->last_frame_uf, cm->width, cm->height, seq_params->subsampling_x,
841 seq_params->subsampling_y, seq_params->use_highbitdepth,
842 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700843 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700844 "Failed to allocate last frame buffer");
845
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700846 if (aom_realloc_frame_buffer(
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -0800847 &cpi->trial_frame_rst, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700848 cm->superres_upscaled_height, seq_params->subsampling_x,
849 seq_params->subsampling_y, seq_params->use_highbitdepth,
850 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800851 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800852 "Failed to allocate trial restored frame buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700853
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700854 if (aom_realloc_frame_buffer(
855 &cpi->scaled_source, cm->width, cm->height, seq_params->subsampling_x,
856 seq_params->subsampling_y, seq_params->use_highbitdepth,
857 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700858 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700859 "Failed to allocate scaled source buffer");
860
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700861 if (aom_realloc_frame_buffer(
862 &cpi->scaled_last_source, cm->width, cm->height,
863 seq_params->subsampling_x, seq_params->subsampling_y,
864 seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS,
865 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700866 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700867 "Failed to allocate scaled last source buffer");
868}
869
Cheng Chen46f30c72017-09-07 11:13:33 -0700870static void alloc_compressor_data(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700871 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000872 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700873
Yaowu Xuf883b422016-08-30 14:01:10 -0700874 av1_alloc_context_buffers(cm, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700875
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530876 int mi_rows_aligned_to_sb =
877 ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
878 int sb_rows = mi_rows_aligned_to_sb >> cm->seq_params.mib_size_log2;
879
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700880 av1_alloc_txb_buf(cpi);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700881
Yaowu Xuc27fc142016-08-22 16:08:15 -0700882 alloc_context_buffers_ext(cpi);
883
Yaowu Xuf883b422016-08-30 14:01:10 -0700884 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700885
886 {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000887 unsigned int tokens =
888 get_token_alloc(cm->mb_rows, cm->mb_cols, MAX_SB_SIZE_LOG2, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700889 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -0700890 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700891 }
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530892 aom_free(cpi->tplist[0][0]);
893
894 CHECK_MEM_ERROR(cm, cpi->tplist[0][0],
895 aom_calloc(sb_rows * MAX_TILE_ROWS * MAX_TILE_COLS,
896 sizeof(*cpi->tplist[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700897
Yaowu Xuf883b422016-08-30 14:01:10 -0700898 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700899}
900
Yaowu Xuf883b422016-08-30 14:01:10 -0700901void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700902 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700903 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700904}
905
Yunqing Wang75e20e82018-06-16 12:10:48 -0700906static void set_tile_info(AV1_COMP *cpi) {
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200907 AV1_COMMON *const cm = &cpi->common;
Dominic Symesf58f1112017-09-25 12:47:40 +0200908 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200909
910 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200911
912 // configure tile columns
Dominic Symes26ad0b22017-10-01 16:35:13 +0200913 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
Dominic Symesf58f1112017-09-25 12:47:40 +0200914 cm->uniform_tile_spacing_flag = 1;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200915 cm->log2_tile_cols = AOMMAX(cpi->oxcf.tile_columns, cm->min_log2_tile_cols);
916 cm->log2_tile_cols = AOMMIN(cm->log2_tile_cols, cm->max_log2_tile_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +0200917 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000918 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->seq_params.mib_size_log2);
919 int sb_cols = mi_cols >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200920 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200921 cm->uniform_tile_spacing_flag = 0;
922 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
923 cm->tile_col_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200924 size_sb = cpi->oxcf.tile_widths[j++];
925 if (j >= cpi->oxcf.tile_width_count) j = 0;
David Barker6cd5a822018-03-05 16:19:28 +0000926 start_sb += AOMMIN(size_sb, cm->max_tile_width_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200927 }
928 cm->tile_cols = i;
929 cm->tile_col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200930 }
931 av1_calculate_tile_cols(cm);
932
933 // configure tile rows
934 if (cm->uniform_tile_spacing_flag) {
935 cm->log2_tile_rows = AOMMAX(cpi->oxcf.tile_rows, cm->min_log2_tile_rows);
936 cm->log2_tile_rows = AOMMIN(cm->log2_tile_rows, cm->max_log2_tile_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +0200937 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000938 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
939 int sb_rows = mi_rows >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200940 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200941 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
942 cm->tile_row_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200943 size_sb = cpi->oxcf.tile_heights[j++];
944 if (j >= cpi->oxcf.tile_height_count) j = 0;
945 start_sb += AOMMIN(size_sb, cm->max_tile_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200946 }
947 cm->tile_rows = i;
948 cm->tile_row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200949 }
950 av1_calculate_tile_rows(cm);
951}
952
Yaowu Xuf883b422016-08-30 14:01:10 -0700953static void update_frame_size(AV1_COMP *cpi) {
954 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700955 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
956
Yaowu Xuf883b422016-08-30 14:01:10 -0700957 av1_set_mb_mi(cm, cm->width, cm->height);
958 av1_init_context_buffers(cm);
Luc Trudeau1e84af52017-11-25 15:00:28 -0500959 av1_init_macroblockd(cm, xd, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700960 memset(cpi->mbmi_ext_base, 0,
961 cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700962 set_tile_info(cpi);
963}
964
Yaowu Xuf883b422016-08-30 14:01:10 -0700965static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700966 int fb_idx;
Zoe Liu5989a722018-03-29 13:37:36 -0700967 for (fb_idx = 0; fb_idx < REF_FRAMES; ++fb_idx)
Urvang Joshi4d9f15f2018-11-05 15:26:22 -0800968 cpi->remapped_ref_idx[fb_idx] = fb_idx;
RogerZhou3b635242017-09-19 10:06:46 -0700969 cpi->rate_index = 0;
970 cpi->rate_size = 0;
971 cpi->cur_poc = -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700972}
973
Debargha Mukherjee57498692018-05-11 13:29:31 -0700974static INLINE int does_level_match(int width, int height, double fps,
975 int lvl_width, int lvl_height,
976 double lvl_fps, int lvl_dim_mult) {
977 const int64_t lvl_luma_pels = lvl_width * lvl_height;
978 const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
979 const int64_t luma_pels = width * height;
980 const double display_sample_rate = luma_pels * fps;
981 return luma_pels <= lvl_luma_pels &&
982 display_sample_rate <= lvl_display_sample_rate &&
983 width <= lvl_width * lvl_dim_mult &&
984 height <= lvl_height * lvl_dim_mult;
985}
986
Andrey Norkin26495512018-06-20 17:13:11 -0700987static void set_bitstream_level_tier(SequenceHeader *seq, AV1_COMMON *cm,
Andrey Norkinf481d982018-05-15 12:05:31 -0700988 const AV1EncoderConfig *oxcf) {
Debargha Mukherjee57498692018-05-11 13:29:31 -0700989 // TODO(any): This is a placeholder function that only addresses dimensions
990 // and max display sample rates.
991 // Need to add checks for max bit rate, max decoded luma sample rate, header
992 // rate, etc. that are not covered by this function.
Debargha Mukherjeeea675402018-05-10 16:10:41 -0700993 (void)oxcf;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700994 BitstreamLevel bl = { 9, 3 };
995 if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate, 512,
996 288, 30.0, 4)) {
997 bl.major = 2;
998 bl.minor = 0;
999 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1000 704, 396, 30.0, 4)) {
1001 bl.major = 2;
1002 bl.minor = 1;
1003 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1004 1088, 612, 30.0, 4)) {
1005 bl.major = 3;
1006 bl.minor = 0;
1007 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1008 1376, 774, 30.0, 4)) {
1009 bl.major = 3;
1010 bl.minor = 1;
1011 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1012 2048, 1152, 30.0, 3)) {
1013 bl.major = 4;
1014 bl.minor = 0;
1015 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1016 2048, 1152, 60.0, 3)) {
1017 bl.major = 4;
1018 bl.minor = 1;
1019 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1020 4096, 2176, 30.0, 2)) {
1021 bl.major = 5;
1022 bl.minor = 0;
1023 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1024 4096, 2176, 60.0, 2)) {
1025 bl.major = 5;
1026 bl.minor = 1;
1027 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1028 4096, 2176, 120.0, 2)) {
1029 bl.major = 5;
1030 bl.minor = 2;
1031 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1032 8192, 4352, 30.0, 2)) {
1033 bl.major = 6;
1034 bl.minor = 0;
1035 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1036 8192, 4352, 60.0, 2)) {
1037 bl.major = 6;
1038 bl.minor = 1;
1039 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1040 8192, 4352, 120.0, 2)) {
1041 bl.major = 6;
1042 bl.minor = 2;
1043 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1044 16384, 8704, 30.0, 2)) {
1045 bl.major = 7;
1046 bl.minor = 0;
1047 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1048 16384, 8704, 60.0, 2)) {
1049 bl.major = 7;
1050 bl.minor = 1;
1051 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1052 16384, 8704, 120.0, 2)) {
1053 bl.major = 7;
1054 bl.minor = 2;
1055 }
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001056 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Debargha Mukherjee57498692018-05-11 13:29:31 -07001057 seq->level[i] = bl;
Andrey Norkinf481d982018-05-15 12:05:31 -07001058 seq->tier[i] = 0; // setting main tier by default
Andrey Norkin26495512018-06-20 17:13:11 -07001059 // Set the maximum parameters for bitrate and buffer size for this profile,
1060 // level, and tier
1061 cm->op_params[i].bitrate = max_level_bitrate(
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001062 cm->seq_params.profile, major_minor_to_seq_level_idx(seq->level[i]),
1063 seq->tier[i]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001064 // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
1065 // check
Andrey Norkin26495512018-06-20 17:13:11 -07001066 if (cm->op_params[i].bitrate == 0)
1067 aom_internal_error(
1068 &cm->error, AOM_CODEC_UNSUP_BITSTREAM,
1069 "AV1 does not support this combination of profile, level, and tier.");
Andrey Norkinc7511de2018-06-22 12:31:06 -07001070 // Buffer size in bits/s is bitrate in bits/s * 1 s
Andrey Norkin26495512018-06-20 17:13:11 -07001071 cm->op_params[i].buffer_size = cm->op_params[i].bitrate;
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001072 }
1073}
1074
Andrey Norkin26495512018-06-20 17:13:11 -07001075static void init_seq_coding_tools(SequenceHeader *seq, AV1_COMMON *cm,
1076 const AV1EncoderConfig *oxcf) {
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001077 seq->still_picture = (oxcf->limit == 1);
1078 seq->reduced_still_picture_hdr = seq->still_picture;
Debargha Mukherjee9713ccb2018-04-08 19:09:17 -07001079 seq->reduced_still_picture_hdr &= !oxcf->full_still_picture_hdr;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001080 seq->force_screen_content_tools = 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001081 seq->force_integer_mv = 2;
David Turnerebf96f42018-11-14 16:57:57 +00001082 seq->order_hint_info.enable_order_hint = oxcf->enable_order_hint;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001083 seq->frame_id_numbers_present_flag = oxcf->large_scale_tile;
1084 if (seq->still_picture && seq->reduced_still_picture_hdr) {
David Turnerebf96f42018-11-14 16:57:57 +00001085 seq->order_hint_info.enable_order_hint = 0;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001086 seq->frame_id_numbers_present_flag = 0;
1087 seq->force_screen_content_tools = 2;
1088 seq->force_integer_mv = 2;
1089 }
David Turnerebf96f42018-11-14 16:57:57 +00001090 seq->order_hint_info.order_hint_bits_minus_1 =
1091 seq->order_hint_info.enable_order_hint
1092 ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1
1093 : -1;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001094
1095 seq->enable_dual_filter = oxcf->enable_dual_filter;
David Turnerebf96f42018-11-14 16:57:57 +00001096 seq->order_hint_info.enable_jnt_comp = oxcf->enable_jnt_comp;
1097 seq->order_hint_info.enable_jnt_comp &=
1098 seq->order_hint_info.enable_order_hint;
1099 seq->order_hint_info.enable_ref_frame_mvs = oxcf->enable_ref_frame_mvs;
1100 seq->order_hint_info.enable_ref_frame_mvs &=
1101 seq->order_hint_info.enable_order_hint;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001102 seq->enable_superres = oxcf->enable_superres;
1103 seq->enable_cdef = oxcf->enable_cdef;
1104 seq->enable_restoration = oxcf->enable_restoration;
Debargha Mukherjee37df9162018-03-25 12:48:24 -07001105 seq->enable_warped_motion = oxcf->enable_warped_motion;
Debargha Mukherjee97095fb2018-03-26 07:51:48 -07001106 seq->enable_interintra_compound = 1;
1107 seq->enable_masked_compound = 1;
Debargha Mukherjee365eae82018-03-26 19:19:55 -07001108 seq->enable_intra_edge_filter = 1;
1109 seq->enable_filter_intra = 1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001110
Andrey Norkin26495512018-06-20 17:13:11 -07001111 set_bitstream_level_tier(seq, cm, oxcf);
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001112
1113 if (seq->operating_points_cnt_minus_1 == 0) {
1114 seq->operating_point_idc[0] = 0;
1115 } else {
1116 // Set operating_point_idc[] such that for the i-th operating point the
1117 // first (operating_points_cnt-i) spatial layers and the first temporal
1118 // layer are decoded Note that highest quality operating point should come
1119 // first
1120 for (int i = 0; i < seq->operating_points_cnt_minus_1 + 1; i++)
1121 seq->operating_point_idc[i] =
1122 (~(~0u << (seq->operating_points_cnt_minus_1 + 1 - i)) << 8) | 1;
1123 }
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001124}
1125
Yaowu Xuf883b422016-08-30 14:01:10 -07001126static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
1127 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001128
1129 cpi->oxcf = *oxcf;
1130 cpi->framerate = oxcf->init_framerate;
1131
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001132 cm->seq_params.profile = oxcf->profile;
1133 cm->seq_params.bit_depth = oxcf->bit_depth;
1134 cm->seq_params.use_highbitdepth = oxcf->use_highbitdepth;
1135 cm->seq_params.color_primaries = oxcf->color_primaries;
1136 cm->seq_params.transfer_characteristics = oxcf->transfer_characteristics;
1137 cm->seq_params.matrix_coefficients = oxcf->matrix_coefficients;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -08001138 cm->seq_params.monochrome = oxcf->monochrome;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001139 cm->seq_params.chroma_sample_position = oxcf->chroma_sample_position;
1140 cm->seq_params.color_range = oxcf->color_range;
Andrey Norkin28e9ce22018-01-08 10:11:21 -08001141 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08001142 cm->timing_info.num_units_in_display_tick =
1143 oxcf->timing_info.num_units_in_display_tick;
1144 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
1145 cm->timing_info.equal_picture_interval =
1146 oxcf->timing_info.equal_picture_interval;
1147 cm->timing_info.num_ticks_per_picture =
1148 oxcf->timing_info.num_ticks_per_picture;
1149
Andrey Norkin26495512018-06-20 17:13:11 -07001150 cm->seq_params.display_model_info_present_flag =
1151 oxcf->display_model_info_present_flag;
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001152 cm->seq_params.decoder_model_info_present_flag =
1153 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08001154 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07001155 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08001156 cm->buffer_model.num_units_in_decoding_tick =
1157 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07001158 cm->buffer_removal_time_present = 1;
Andrey Norkin795ba872018-03-06 13:24:14 -08001159 set_aom_dec_model_info(&cm->buffer_model);
Andrey Norkin26495512018-06-20 17:13:11 -07001160 set_dec_model_op_parameters(&cm->op_params[0]);
1161 } else if (cm->timing_info_present &&
1162 cm->timing_info.equal_picture_interval &&
1163 !cm->seq_params.decoder_model_info_present_flag) {
1164 // set the decoder model parameters in resource availability mode
1165 set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001166 } else {
1167 cm->op_params[0].initial_display_delay =
1168 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08001169 }
Andrey Norkinc7511de2018-06-22 12:31:06 -07001170
Tom Fineganf8d6a162018-08-21 10:47:55 -07001171 if (cm->seq_params.monochrome) {
1172 cm->seq_params.subsampling_x = 1;
1173 cm->seq_params.subsampling_y = 1;
1174 } else if (cm->seq_params.color_primaries == AOM_CICP_CP_BT_709 &&
1175 cm->seq_params.transfer_characteristics == AOM_CICP_TC_SRGB &&
1176 cm->seq_params.matrix_coefficients == AOM_CICP_MC_IDENTITY) {
1177 cm->seq_params.subsampling_x = 0;
1178 cm->seq_params.subsampling_y = 0;
1179 } else {
1180 if (cm->seq_params.profile == 0) {
1181 cm->seq_params.subsampling_x = 1;
1182 cm->seq_params.subsampling_y = 1;
1183 } else if (cm->seq_params.profile == 1) {
1184 cm->seq_params.subsampling_x = 0;
1185 cm->seq_params.subsampling_y = 0;
1186 } else {
1187 if (cm->seq_params.bit_depth == AOM_BITS_12) {
1188 cm->seq_params.subsampling_x = oxcf->chroma_subsampling_x;
1189 cm->seq_params.subsampling_y = oxcf->chroma_subsampling_y;
1190 } else {
1191 cm->seq_params.subsampling_x = 1;
1192 cm->seq_params.subsampling_y = 0;
1193 }
1194 }
Tom Finegan02b2a842018-08-24 13:50:00 -07001195 }
1196
Yaowu Xuc27fc142016-08-22 16:08:15 -07001197 cm->width = oxcf->width;
1198 cm->height = oxcf->height;
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00001199 set_sb_size(&cm->seq_params,
1200 select_sb_size(cpi)); // set sb size before allocations
Cheng Chen46f30c72017-09-07 11:13:33 -07001201 alloc_compressor_data(cpi);
Yaowu Xuc7119a72018-03-29 09:59:37 -07001202
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08001203 update_film_grain_parameters(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001204
1205 // Single thread case: use counts in common.
Yue Chencc6a6ef2018-05-21 16:21:05 -07001206 cpi->td.counts = &cpi->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001207
1208 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -07001209 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001210
1211 cpi->static_mb_pct = 0;
1212 cpi->ref_frame_flags = 0;
1213
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001214 // Reset resize pending flags
1215 cpi->resize_pending_width = 0;
1216 cpi->resize_pending_height = 0;
1217
Yaowu Xuc27fc142016-08-22 16:08:15 -07001218 init_buffer_indices(cpi);
1219}
1220
1221static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -07001222 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001223 const int64_t bandwidth = oxcf->target_bandwidth;
1224 const int64_t starting = oxcf->starting_buffer_level_ms;
1225 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1226 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1227
1228 rc->starting_buffer_level = starting * bandwidth / 1000;
1229 rc->optimal_buffer_level =
1230 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1231 rc->maximum_buffer_size =
1232 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1233}
1234
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001235#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
1236 cpi->fn_ptr[BT].sdf = SDF; \
1237 cpi->fn_ptr[BT].sdaf = SDAF; \
1238 cpi->fn_ptr[BT].vf = VF; \
1239 cpi->fn_ptr[BT].svf = SVF; \
1240 cpi->fn_ptr[BT].svaf = SVAF; \
1241 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
1242 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001243 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001244
1245#define MAKE_BFP_SAD_WRAPPER(fnname) \
1246 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1247 int source_stride, \
1248 const uint8_t *ref_ptr, int ref_stride) { \
1249 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1250 } \
1251 static unsigned int fnname##_bits10( \
1252 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1253 int ref_stride) { \
1254 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1255 } \
1256 static unsigned int fnname##_bits12( \
1257 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1258 int ref_stride) { \
1259 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1260 }
1261
1262#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1263 static unsigned int fnname##_bits8( \
1264 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1265 int ref_stride, const uint8_t *second_pred) { \
1266 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1267 } \
1268 static unsigned int fnname##_bits10( \
1269 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1270 int ref_stride, const uint8_t *second_pred) { \
1271 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1272 2; \
1273 } \
1274 static unsigned int fnname##_bits12( \
1275 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1276 int ref_stride, const uint8_t *second_pred) { \
1277 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1278 4; \
1279 }
1280
Yaowu Xuc27fc142016-08-22 16:08:15 -07001281#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1282 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1283 const uint8_t *const ref_ptr[], int ref_stride, \
1284 unsigned int *sad_array) { \
1285 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1286 } \
1287 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1288 const uint8_t *const ref_ptr[], int ref_stride, \
1289 unsigned int *sad_array) { \
1290 int i; \
1291 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1292 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1293 } \
1294 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1295 const uint8_t *const ref_ptr[], int ref_stride, \
1296 unsigned int *sad_array) { \
1297 int i; \
1298 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1299 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1300 }
1301
Cheng Chenbf3d4962017-11-01 14:48:52 -07001302#define MAKE_BFP_JSADAVG_WRAPPER(fnname) \
1303 static unsigned int fnname##_bits8( \
1304 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1305 int ref_stride, const uint8_t *second_pred, \
1306 const JNT_COMP_PARAMS *jcp_param) { \
1307 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1308 jcp_param); \
1309 } \
1310 static unsigned int fnname##_bits10( \
1311 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1312 int ref_stride, const uint8_t *second_pred, \
1313 const JNT_COMP_PARAMS *jcp_param) { \
1314 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1315 jcp_param) >> \
1316 2; \
1317 } \
1318 static unsigned int fnname##_bits12( \
1319 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1320 int ref_stride, const uint8_t *second_pred, \
1321 const JNT_COMP_PARAMS *jcp_param) { \
1322 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1323 jcp_param) >> \
1324 4; \
1325 }
Cheng Chenbf3d4962017-11-01 14:48:52 -07001326
Yaowu Xuf883b422016-08-30 14:01:10 -07001327MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1328MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001329MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1330MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1331MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1332MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1333MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1334MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1335MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuf883b422016-08-30 14:01:10 -07001336MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1337MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1338MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1339MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1340MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1341MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1342MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1343MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1344MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1345MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1346MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1347MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1348MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1349MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001350MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1351MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1352MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001353MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1354MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1355MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001356MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1357MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1358MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001359MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1360MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1361MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001362MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1363MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1364MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001365MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1366MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1367MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001368MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1369MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1370MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001371MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1372MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1373MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001374MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001375
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001376MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1377MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1378MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1379MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1380MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1381MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1382MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1383MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1384MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1385MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1386MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1387MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001388MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1389MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1390MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1391MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1392MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1393MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001394
Cheng Chenbf3d4962017-11-01 14:48:52 -07001395MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad128x128_avg)
1396MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad128x64_avg)
1397MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x128_avg)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001398MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x16_avg)
1399MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x32_avg)
1400MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x32_avg)
1401MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x64_avg)
1402MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x32_avg)
1403MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x64_avg)
1404MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x16_avg)
1405MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x8_avg)
1406MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x16_avg)
1407MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x8_avg)
1408MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x4_avg)
1409MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad4x8_avg)
1410MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad4x4_avg)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001411MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad4x16_avg)
1412MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x4_avg)
1413MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x32_avg)
1414MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x8_avg)
1415MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x64_avg)
1416MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x16_avg)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001417
David Barker0f3c94e2017-05-16 15:21:50 +01001418#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001419 cpi->fn_ptr[BT].msdf = MCSDF; \
1420 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001421
David Barkerc155e012017-05-11 13:54:54 +01001422#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1423 static unsigned int fnname##_bits8( \
1424 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1425 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1426 int m_stride, int invert_mask) { \
1427 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1428 second_pred_ptr, m, m_stride, invert_mask); \
1429 } \
1430 static unsigned int fnname##_bits10( \
1431 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1432 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1433 int m_stride, int invert_mask) { \
1434 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1435 second_pred_ptr, m, m_stride, invert_mask) >> \
1436 2; \
1437 } \
1438 static unsigned int fnname##_bits12( \
1439 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1440 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1441 int m_stride, int invert_mask) { \
1442 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1443 second_pred_ptr, m, m_stride, invert_mask) >> \
1444 4; \
1445 }
1446
David Barkerf19f35f2017-05-22 16:33:22 +01001447MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1448MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1449MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001450MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1451MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1452MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1453MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1454MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1455MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1456MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1457MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1458MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1459MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1460MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1461MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1462MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001463MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1464MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1465MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1466MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001467MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1468MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001469
Yaowu Xuc27fc142016-08-22 16:08:15 -07001470#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1471 cpi->fn_ptr[BT].osdf = OSDF; \
1472 cpi->fn_ptr[BT].ovf = OVF; \
1473 cpi->fn_ptr[BT].osvf = OSVF;
1474
1475#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1476 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1477 const int32_t *wsrc, \
1478 const int32_t *msk) { \
1479 return fnname(ref, ref_stride, wsrc, msk); \
1480 } \
1481 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1482 const int32_t *wsrc, \
1483 const int32_t *msk) { \
1484 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1485 } \
1486 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1487 const int32_t *wsrc, \
1488 const int32_t *msk) { \
1489 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1490 }
1491
Yaowu Xuf883b422016-08-30 14:01:10 -07001492MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1493MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1494MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001495MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1496MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1497MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1498MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1499MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1500MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1501MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1502MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1503MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1504MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1505MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1506MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1507MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001508MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1509MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1510MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1511MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001512MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1513MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001514
Yaowu Xuf883b422016-08-30 14:01:10 -07001515static void highbd_set_var_fns(AV1_COMP *const cpi) {
1516 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001517 if (cm->seq_params.use_highbitdepth) {
1518 switch (cm->seq_params.bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001519 case AOM_BITS_8:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001520 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1521 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1522 aom_highbd_8_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001523 aom_highbd_8_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001524 aom_highbd_sad64x16x4d_bits8,
1525 aom_highbd_jnt_sad64x16_avg_bits8,
1526 aom_highbd_8_jnt_sub_pixel_avg_variance64x16)
1527
1528 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1529 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1530 aom_highbd_8_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001531 aom_highbd_8_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001532 aom_highbd_sad16x64x4d_bits8,
1533 aom_highbd_jnt_sad16x64_avg_bits8,
1534 aom_highbd_8_jnt_sub_pixel_avg_variance16x64)
1535
1536 HIGHBD_BFP(
1537 BLOCK_32X8, aom_highbd_sad32x8_bits8, aom_highbd_sad32x8_avg_bits8,
1538 aom_highbd_8_variance32x8, aom_highbd_8_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001539 aom_highbd_8_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001540 aom_highbd_sad32x8x4d_bits8, aom_highbd_jnt_sad32x8_avg_bits8,
1541 aom_highbd_8_jnt_sub_pixel_avg_variance32x8)
1542
1543 HIGHBD_BFP(
1544 BLOCK_8X32, aom_highbd_sad8x32_bits8, aom_highbd_sad8x32_avg_bits8,
1545 aom_highbd_8_variance8x32, aom_highbd_8_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001546 aom_highbd_8_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001547 aom_highbd_sad8x32x4d_bits8, aom_highbd_jnt_sad8x32_avg_bits8,
1548 aom_highbd_8_jnt_sub_pixel_avg_variance8x32)
1549
1550 HIGHBD_BFP(
1551 BLOCK_16X4, aom_highbd_sad16x4_bits8, aom_highbd_sad16x4_avg_bits8,
1552 aom_highbd_8_variance16x4, aom_highbd_8_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001553 aom_highbd_8_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001554 aom_highbd_sad16x4x4d_bits8, aom_highbd_jnt_sad16x4_avg_bits8,
1555 aom_highbd_8_jnt_sub_pixel_avg_variance16x4)
1556
1557 HIGHBD_BFP(
1558 BLOCK_4X16, aom_highbd_sad4x16_bits8, aom_highbd_sad4x16_avg_bits8,
1559 aom_highbd_8_variance4x16, aom_highbd_8_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001560 aom_highbd_8_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001561 aom_highbd_sad4x16x4d_bits8, aom_highbd_jnt_sad4x16_avg_bits8,
1562 aom_highbd_8_jnt_sub_pixel_avg_variance4x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001563
1564 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1565 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1566 aom_highbd_8_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001567 aom_highbd_8_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001568 aom_highbd_sad32x16x4d_bits8,
1569 aom_highbd_jnt_sad32x16_avg_bits8,
1570 aom_highbd_8_jnt_sub_pixel_avg_variance32x16)
1571
1572 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1573 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1574 aom_highbd_8_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001575 aom_highbd_8_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001576 aom_highbd_sad16x32x4d_bits8,
1577 aom_highbd_jnt_sad16x32_avg_bits8,
1578 aom_highbd_8_jnt_sub_pixel_avg_variance16x32)
1579
1580 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1581 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1582 aom_highbd_8_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001583 aom_highbd_8_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001584 aom_highbd_sad64x32x4d_bits8,
1585 aom_highbd_jnt_sad64x32_avg_bits8,
1586 aom_highbd_8_jnt_sub_pixel_avg_variance64x32)
1587
1588 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1589 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1590 aom_highbd_8_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001591 aom_highbd_8_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001592 aom_highbd_sad32x64x4d_bits8,
1593 aom_highbd_jnt_sad32x64_avg_bits8,
1594 aom_highbd_8_jnt_sub_pixel_avg_variance32x64)
1595
1596 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1597 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1598 aom_highbd_8_sub_pixel_variance32x32,
1599 aom_highbd_8_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001600 aom_highbd_sad32x32x4d_bits8,
1601 aom_highbd_jnt_sad32x32_avg_bits8,
1602 aom_highbd_8_jnt_sub_pixel_avg_variance32x32)
1603
1604 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1605 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1606 aom_highbd_8_sub_pixel_variance64x64,
1607 aom_highbd_8_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001608 aom_highbd_sad64x64x4d_bits8,
1609 aom_highbd_jnt_sad64x64_avg_bits8,
1610 aom_highbd_8_jnt_sub_pixel_avg_variance64x64)
1611
1612 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1613 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1614 aom_highbd_8_sub_pixel_variance16x16,
1615 aom_highbd_8_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001616 aom_highbd_sad16x16x4d_bits8,
1617 aom_highbd_jnt_sad16x16_avg_bits8,
1618 aom_highbd_8_jnt_sub_pixel_avg_variance16x16)
1619
1620 HIGHBD_BFP(
1621 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1622 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001623 aom_highbd_8_sub_pixel_avg_variance16x8,
1624 aom_highbd_sad16x8x4d_bits8, aom_highbd_jnt_sad16x8_avg_bits8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001625 aom_highbd_8_jnt_sub_pixel_avg_variance16x8)
1626
1627 HIGHBD_BFP(
1628 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1629 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001630 aom_highbd_8_sub_pixel_avg_variance8x16,
1631 aom_highbd_sad8x16x4d_bits8, aom_highbd_jnt_sad8x16_avg_bits8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001632 aom_highbd_8_jnt_sub_pixel_avg_variance8x16)
1633
1634 HIGHBD_BFP(BLOCK_8X8, aom_highbd_sad8x8_bits8,
1635 aom_highbd_sad8x8_avg_bits8, aom_highbd_8_variance8x8,
1636 aom_highbd_8_sub_pixel_variance8x8,
1637 aom_highbd_8_sub_pixel_avg_variance8x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001638 aom_highbd_sad8x8x4d_bits8, aom_highbd_jnt_sad8x8_avg_bits8,
1639 aom_highbd_8_jnt_sub_pixel_avg_variance8x8)
1640
1641 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits8,
1642 aom_highbd_sad8x4_avg_bits8, aom_highbd_8_variance8x4,
1643 aom_highbd_8_sub_pixel_variance8x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001644 aom_highbd_8_sub_pixel_avg_variance8x4,
1645 aom_highbd_sad8x4x4d_bits8, aom_highbd_jnt_sad8x4_avg_bits8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001646 aom_highbd_8_jnt_sub_pixel_avg_variance8x4)
1647
1648 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits8,
1649 aom_highbd_sad4x8_avg_bits8, aom_highbd_8_variance4x8,
1650 aom_highbd_8_sub_pixel_variance4x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001651 aom_highbd_8_sub_pixel_avg_variance4x8,
1652 aom_highbd_sad4x8x4d_bits8, aom_highbd_jnt_sad4x8_avg_bits8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001653 aom_highbd_8_jnt_sub_pixel_avg_variance4x8)
1654
1655 HIGHBD_BFP(BLOCK_4X4, aom_highbd_sad4x4_bits8,
1656 aom_highbd_sad4x4_avg_bits8, aom_highbd_8_variance4x4,
1657 aom_highbd_8_sub_pixel_variance4x4,
1658 aom_highbd_8_sub_pixel_avg_variance4x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001659 aom_highbd_sad4x4x4d_bits8, aom_highbd_jnt_sad4x4_avg_bits8,
1660 aom_highbd_8_jnt_sub_pixel_avg_variance4x4)
1661
Cheng Chenbf3d4962017-11-01 14:48:52 -07001662 HIGHBD_BFP(
1663 BLOCK_128X128, aom_highbd_sad128x128_bits8,
1664 aom_highbd_sad128x128_avg_bits8, aom_highbd_8_variance128x128,
1665 aom_highbd_8_sub_pixel_variance128x128,
1666 aom_highbd_8_sub_pixel_avg_variance128x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001667 aom_highbd_sad128x128x4d_bits8, aom_highbd_jnt_sad128x128_avg_bits8,
1668 aom_highbd_8_jnt_sub_pixel_avg_variance128x128)
1669
1670 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1671 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1672 aom_highbd_8_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001673 aom_highbd_8_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001674 aom_highbd_sad128x64x4d_bits8,
1675 aom_highbd_jnt_sad128x64_avg_bits8,
1676 aom_highbd_8_jnt_sub_pixel_avg_variance128x64)
1677
1678 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1679 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1680 aom_highbd_8_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001681 aom_highbd_8_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001682 aom_highbd_sad64x128x4d_bits8,
1683 aom_highbd_jnt_sad64x128_avg_bits8,
1684 aom_highbd_8_jnt_sub_pixel_avg_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001685
David Barkerf19f35f2017-05-22 16:33:22 +01001686 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1687 aom_highbd_8_masked_sub_pixel_variance128x128)
1688 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1689 aom_highbd_8_masked_sub_pixel_variance128x64)
1690 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1691 aom_highbd_8_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001692 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1693 aom_highbd_8_masked_sub_pixel_variance64x64)
1694 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1695 aom_highbd_8_masked_sub_pixel_variance64x32)
1696 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1697 aom_highbd_8_masked_sub_pixel_variance32x64)
1698 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1699 aom_highbd_8_masked_sub_pixel_variance32x32)
1700 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1701 aom_highbd_8_masked_sub_pixel_variance32x16)
1702 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1703 aom_highbd_8_masked_sub_pixel_variance16x32)
1704 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1705 aom_highbd_8_masked_sub_pixel_variance16x16)
1706 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1707 aom_highbd_8_masked_sub_pixel_variance8x16)
1708 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1709 aom_highbd_8_masked_sub_pixel_variance16x8)
1710 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1711 aom_highbd_8_masked_sub_pixel_variance8x8)
1712 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1713 aom_highbd_8_masked_sub_pixel_variance4x8)
1714 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1715 aom_highbd_8_masked_sub_pixel_variance8x4)
1716 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1717 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001718 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1719 aom_highbd_8_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001720 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1721 aom_highbd_8_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001722 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1723 aom_highbd_8_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001724 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
1725 aom_highbd_8_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001726 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
1727 aom_highbd_8_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001728 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
1729 aom_highbd_8_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07001730 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1731 aom_highbd_obmc_variance128x128,
1732 aom_highbd_obmc_sub_pixel_variance128x128)
1733 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1734 aom_highbd_obmc_variance128x64,
1735 aom_highbd_obmc_sub_pixel_variance128x64)
1736 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1737 aom_highbd_obmc_variance64x128,
1738 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001739 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
1740 aom_highbd_obmc_variance64x64,
1741 aom_highbd_obmc_sub_pixel_variance64x64)
1742 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
1743 aom_highbd_obmc_variance64x32,
1744 aom_highbd_obmc_sub_pixel_variance64x32)
1745 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
1746 aom_highbd_obmc_variance32x64,
1747 aom_highbd_obmc_sub_pixel_variance32x64)
1748 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
1749 aom_highbd_obmc_variance32x32,
1750 aom_highbd_obmc_sub_pixel_variance32x32)
1751 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
1752 aom_highbd_obmc_variance32x16,
1753 aom_highbd_obmc_sub_pixel_variance32x16)
1754 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
1755 aom_highbd_obmc_variance16x32,
1756 aom_highbd_obmc_sub_pixel_variance16x32)
1757 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
1758 aom_highbd_obmc_variance16x16,
1759 aom_highbd_obmc_sub_pixel_variance16x16)
1760 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
1761 aom_highbd_obmc_variance8x16,
1762 aom_highbd_obmc_sub_pixel_variance8x16)
1763 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
1764 aom_highbd_obmc_variance16x8,
1765 aom_highbd_obmc_sub_pixel_variance16x8)
1766 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
1767 aom_highbd_obmc_variance8x8,
1768 aom_highbd_obmc_sub_pixel_variance8x8)
1769 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
1770 aom_highbd_obmc_variance4x8,
1771 aom_highbd_obmc_sub_pixel_variance4x8)
1772 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
1773 aom_highbd_obmc_variance8x4,
1774 aom_highbd_obmc_sub_pixel_variance8x4)
1775 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
1776 aom_highbd_obmc_variance4x4,
1777 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001778 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
1779 aom_highbd_obmc_variance64x16,
1780 aom_highbd_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001781 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
1782 aom_highbd_obmc_variance16x64,
1783 aom_highbd_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001784 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
1785 aom_highbd_obmc_variance32x8,
1786 aom_highbd_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001787 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
1788 aom_highbd_obmc_variance8x32,
1789 aom_highbd_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001790 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
1791 aom_highbd_obmc_variance16x4,
1792 aom_highbd_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001793 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
1794 aom_highbd_obmc_variance4x16,
1795 aom_highbd_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001796 break;
1797
Yaowu Xuf883b422016-08-30 14:01:10 -07001798 case AOM_BITS_10:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001799 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
1800 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
1801 aom_highbd_10_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001802 aom_highbd_10_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001803 aom_highbd_sad64x16x4d_bits10,
1804 aom_highbd_jnt_sad64x16_avg_bits10,
1805 aom_highbd_10_jnt_sub_pixel_avg_variance64x16);
1806
1807 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
1808 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
1809 aom_highbd_10_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001810 aom_highbd_10_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001811 aom_highbd_sad16x64x4d_bits10,
1812 aom_highbd_jnt_sad16x64_avg_bits10,
1813 aom_highbd_10_jnt_sub_pixel_avg_variance16x64);
1814
1815 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
1816 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
1817 aom_highbd_10_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001818 aom_highbd_10_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001819 aom_highbd_sad32x8x4d_bits10,
1820 aom_highbd_jnt_sad32x8_avg_bits10,
1821 aom_highbd_10_jnt_sub_pixel_avg_variance32x8);
1822
1823 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
1824 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
1825 aom_highbd_10_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001826 aom_highbd_10_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001827 aom_highbd_sad8x32x4d_bits10,
1828 aom_highbd_jnt_sad8x32_avg_bits10,
1829 aom_highbd_10_jnt_sub_pixel_avg_variance8x32);
1830
1831 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
1832 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
1833 aom_highbd_10_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001834 aom_highbd_10_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001835 aom_highbd_sad16x4x4d_bits10,
1836 aom_highbd_jnt_sad16x4_avg_bits10,
1837 aom_highbd_10_jnt_sub_pixel_avg_variance16x4);
1838
1839 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
1840 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
1841 aom_highbd_10_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001842 aom_highbd_10_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001843 aom_highbd_sad4x16x4d_bits10,
1844 aom_highbd_jnt_sad4x16_avg_bits10,
1845 aom_highbd_10_jnt_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001846
1847 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
1848 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
1849 aom_highbd_10_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001850 aom_highbd_10_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001851 aom_highbd_sad32x16x4d_bits10,
1852 aom_highbd_jnt_sad32x16_avg_bits10,
1853 aom_highbd_10_jnt_sub_pixel_avg_variance32x16);
1854
1855 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
1856 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
1857 aom_highbd_10_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001858 aom_highbd_10_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001859 aom_highbd_sad16x32x4d_bits10,
1860 aom_highbd_jnt_sad16x32_avg_bits10,
1861 aom_highbd_10_jnt_sub_pixel_avg_variance16x32);
1862
1863 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
1864 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
1865 aom_highbd_10_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001866 aom_highbd_10_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001867 aom_highbd_sad64x32x4d_bits10,
1868 aom_highbd_jnt_sad64x32_avg_bits10,
1869 aom_highbd_10_jnt_sub_pixel_avg_variance64x32);
1870
1871 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
1872 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
1873 aom_highbd_10_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001874 aom_highbd_10_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001875 aom_highbd_sad32x64x4d_bits10,
1876 aom_highbd_jnt_sad32x64_avg_bits10,
1877 aom_highbd_10_jnt_sub_pixel_avg_variance32x64);
1878
1879 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
1880 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
1881 aom_highbd_10_sub_pixel_variance32x32,
1882 aom_highbd_10_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001883 aom_highbd_sad32x32x4d_bits10,
1884 aom_highbd_jnt_sad32x32_avg_bits10,
1885 aom_highbd_10_jnt_sub_pixel_avg_variance32x32);
1886
1887 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
1888 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
1889 aom_highbd_10_sub_pixel_variance64x64,
1890 aom_highbd_10_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001891 aom_highbd_sad64x64x4d_bits10,
1892 aom_highbd_jnt_sad64x64_avg_bits10,
1893 aom_highbd_10_jnt_sub_pixel_avg_variance64x64);
1894
1895 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
1896 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
1897 aom_highbd_10_sub_pixel_variance16x16,
1898 aom_highbd_10_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001899 aom_highbd_sad16x16x4d_bits10,
1900 aom_highbd_jnt_sad16x16_avg_bits10,
1901 aom_highbd_10_jnt_sub_pixel_avg_variance16x16);
1902
1903 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
1904 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
1905 aom_highbd_10_sub_pixel_variance16x8,
1906 aom_highbd_10_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001907 aom_highbd_sad16x8x4d_bits10,
1908 aom_highbd_jnt_sad16x8_avg_bits10,
1909 aom_highbd_10_jnt_sub_pixel_avg_variance16x8);
1910
1911 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
1912 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
1913 aom_highbd_10_sub_pixel_variance8x16,
1914 aom_highbd_10_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001915 aom_highbd_sad8x16x4d_bits10,
1916 aom_highbd_jnt_sad8x16_avg_bits10,
1917 aom_highbd_10_jnt_sub_pixel_avg_variance8x16);
1918
1919 HIGHBD_BFP(
1920 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
1921 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001922 aom_highbd_10_sub_pixel_avg_variance8x8,
1923 aom_highbd_sad8x8x4d_bits10, aom_highbd_jnt_sad8x8_avg_bits10,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001924 aom_highbd_10_jnt_sub_pixel_avg_variance8x8);
1925
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001926 HIGHBD_BFP(
1927 BLOCK_8X4, aom_highbd_sad8x4_bits10, aom_highbd_sad8x4_avg_bits10,
1928 aom_highbd_10_variance8x4, aom_highbd_10_sub_pixel_variance8x4,
1929 aom_highbd_10_sub_pixel_avg_variance8x4,
1930 aom_highbd_sad8x4x4d_bits10, aom_highbd_jnt_sad8x4_avg_bits10,
1931 aom_highbd_10_jnt_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001932
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001933 HIGHBD_BFP(
1934 BLOCK_4X8, aom_highbd_sad4x8_bits10, aom_highbd_sad4x8_avg_bits10,
1935 aom_highbd_10_variance4x8, aom_highbd_10_sub_pixel_variance4x8,
1936 aom_highbd_10_sub_pixel_avg_variance4x8,
1937 aom_highbd_sad4x8x4d_bits10, aom_highbd_jnt_sad4x8_avg_bits10,
1938 aom_highbd_10_jnt_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001939
1940 HIGHBD_BFP(
1941 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
1942 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001943 aom_highbd_10_sub_pixel_avg_variance4x4,
1944 aom_highbd_sad4x4x4d_bits10, aom_highbd_jnt_sad4x4_avg_bits10,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001945 aom_highbd_10_jnt_sub_pixel_avg_variance4x4);
1946
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001947 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits10,
1948 aom_highbd_sad128x128_avg_bits10,
1949 aom_highbd_10_variance128x128,
1950 aom_highbd_10_sub_pixel_variance128x128,
1951 aom_highbd_10_sub_pixel_avg_variance128x128,
1952 aom_highbd_sad128x128x4d_bits10,
1953 aom_highbd_jnt_sad128x128_avg_bits10,
1954 aom_highbd_10_jnt_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001955
1956 HIGHBD_BFP(
1957 BLOCK_128X64, aom_highbd_sad128x64_bits10,
1958 aom_highbd_sad128x64_avg_bits10, aom_highbd_10_variance128x64,
1959 aom_highbd_10_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001960 aom_highbd_10_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001961 aom_highbd_sad128x64x4d_bits10, aom_highbd_jnt_sad128x64_avg_bits10,
1962 aom_highbd_10_jnt_sub_pixel_avg_variance128x64);
1963
1964 HIGHBD_BFP(
1965 BLOCK_64X128, aom_highbd_sad64x128_bits10,
1966 aom_highbd_sad64x128_avg_bits10, aom_highbd_10_variance64x128,
1967 aom_highbd_10_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001968 aom_highbd_10_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001969 aom_highbd_sad64x128x4d_bits10, aom_highbd_jnt_sad64x128_avg_bits10,
1970 aom_highbd_10_jnt_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001971
David Barkerf19f35f2017-05-22 16:33:22 +01001972 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
1973 aom_highbd_10_masked_sub_pixel_variance128x128)
1974 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
1975 aom_highbd_10_masked_sub_pixel_variance128x64)
1976 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
1977 aom_highbd_10_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001978 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
1979 aom_highbd_10_masked_sub_pixel_variance64x64)
1980 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
1981 aom_highbd_10_masked_sub_pixel_variance64x32)
1982 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
1983 aom_highbd_10_masked_sub_pixel_variance32x64)
1984 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
1985 aom_highbd_10_masked_sub_pixel_variance32x32)
1986 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
1987 aom_highbd_10_masked_sub_pixel_variance32x16)
1988 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
1989 aom_highbd_10_masked_sub_pixel_variance16x32)
1990 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
1991 aom_highbd_10_masked_sub_pixel_variance16x16)
1992 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
1993 aom_highbd_10_masked_sub_pixel_variance8x16)
1994 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
1995 aom_highbd_10_masked_sub_pixel_variance16x8)
1996 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
1997 aom_highbd_10_masked_sub_pixel_variance8x8)
1998 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
1999 aom_highbd_10_masked_sub_pixel_variance4x8)
2000 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
2001 aom_highbd_10_masked_sub_pixel_variance8x4)
2002 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
2003 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002004 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
2005 aom_highbd_10_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002006 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
2007 aom_highbd_10_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002008 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
2009 aom_highbd_10_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002010 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
2011 aom_highbd_10_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002012 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
2013 aom_highbd_10_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002014 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
2015 aom_highbd_10_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002016 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
2017 aom_highbd_10_obmc_variance128x128,
2018 aom_highbd_10_obmc_sub_pixel_variance128x128)
2019 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
2020 aom_highbd_10_obmc_variance128x64,
2021 aom_highbd_10_obmc_sub_pixel_variance128x64)
2022 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
2023 aom_highbd_10_obmc_variance64x128,
2024 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002025 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
2026 aom_highbd_10_obmc_variance64x64,
2027 aom_highbd_10_obmc_sub_pixel_variance64x64)
2028 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
2029 aom_highbd_10_obmc_variance64x32,
2030 aom_highbd_10_obmc_sub_pixel_variance64x32)
2031 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
2032 aom_highbd_10_obmc_variance32x64,
2033 aom_highbd_10_obmc_sub_pixel_variance32x64)
2034 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
2035 aom_highbd_10_obmc_variance32x32,
2036 aom_highbd_10_obmc_sub_pixel_variance32x32)
2037 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
2038 aom_highbd_10_obmc_variance32x16,
2039 aom_highbd_10_obmc_sub_pixel_variance32x16)
2040 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
2041 aom_highbd_10_obmc_variance16x32,
2042 aom_highbd_10_obmc_sub_pixel_variance16x32)
2043 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
2044 aom_highbd_10_obmc_variance16x16,
2045 aom_highbd_10_obmc_sub_pixel_variance16x16)
2046 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
2047 aom_highbd_10_obmc_variance8x16,
2048 aom_highbd_10_obmc_sub_pixel_variance8x16)
2049 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
2050 aom_highbd_10_obmc_variance16x8,
2051 aom_highbd_10_obmc_sub_pixel_variance16x8)
2052 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
2053 aom_highbd_10_obmc_variance8x8,
2054 aom_highbd_10_obmc_sub_pixel_variance8x8)
2055 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
2056 aom_highbd_10_obmc_variance4x8,
2057 aom_highbd_10_obmc_sub_pixel_variance4x8)
2058 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
2059 aom_highbd_10_obmc_variance8x4,
2060 aom_highbd_10_obmc_sub_pixel_variance8x4)
2061 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
2062 aom_highbd_10_obmc_variance4x4,
2063 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002064
Rupert Swarbrick72678572017-08-02 12:05:26 +01002065 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
2066 aom_highbd_10_obmc_variance64x16,
2067 aom_highbd_10_obmc_sub_pixel_variance64x16)
2068
2069 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
2070 aom_highbd_10_obmc_variance16x64,
2071 aom_highbd_10_obmc_sub_pixel_variance16x64)
2072
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002073 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
2074 aom_highbd_10_obmc_variance32x8,
2075 aom_highbd_10_obmc_sub_pixel_variance32x8)
2076
2077 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
2078 aom_highbd_10_obmc_variance8x32,
2079 aom_highbd_10_obmc_sub_pixel_variance8x32)
2080
2081 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
2082 aom_highbd_10_obmc_variance16x4,
2083 aom_highbd_10_obmc_sub_pixel_variance16x4)
2084
2085 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
2086 aom_highbd_10_obmc_variance4x16,
2087 aom_highbd_10_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002088 break;
2089
Yaowu Xuf883b422016-08-30 14:01:10 -07002090 case AOM_BITS_12:
Cheng Chenbf3d4962017-11-01 14:48:52 -07002091 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
2092 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
2093 aom_highbd_12_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002094 aom_highbd_12_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002095 aom_highbd_sad64x16x4d_bits12,
2096 aom_highbd_jnt_sad64x16_avg_bits12,
2097 aom_highbd_12_jnt_sub_pixel_avg_variance64x16);
2098
2099 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
2100 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
2101 aom_highbd_12_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002102 aom_highbd_12_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002103 aom_highbd_sad16x64x4d_bits12,
2104 aom_highbd_jnt_sad16x64_avg_bits12,
2105 aom_highbd_12_jnt_sub_pixel_avg_variance16x64);
2106
2107 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
2108 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
2109 aom_highbd_12_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002110 aom_highbd_12_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002111 aom_highbd_sad32x8x4d_bits12,
2112 aom_highbd_jnt_sad32x8_avg_bits12,
2113 aom_highbd_12_jnt_sub_pixel_avg_variance32x8);
2114
2115 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
2116 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
2117 aom_highbd_12_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002118 aom_highbd_12_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002119 aom_highbd_sad8x32x4d_bits12,
2120 aom_highbd_jnt_sad8x32_avg_bits12,
2121 aom_highbd_12_jnt_sub_pixel_avg_variance8x32);
2122
2123 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
2124 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
2125 aom_highbd_12_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002126 aom_highbd_12_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002127 aom_highbd_sad16x4x4d_bits12,
2128 aom_highbd_jnt_sad16x4_avg_bits12,
2129 aom_highbd_12_jnt_sub_pixel_avg_variance16x4);
2130
2131 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
2132 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
2133 aom_highbd_12_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002134 aom_highbd_12_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002135 aom_highbd_sad4x16x4d_bits12,
2136 aom_highbd_jnt_sad4x16_avg_bits12,
2137 aom_highbd_12_jnt_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002138
2139 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
2140 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
2141 aom_highbd_12_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002142 aom_highbd_12_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002143 aom_highbd_sad32x16x4d_bits12,
2144 aom_highbd_jnt_sad32x16_avg_bits12,
2145 aom_highbd_12_jnt_sub_pixel_avg_variance32x16);
2146
2147 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
2148 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
2149 aom_highbd_12_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002150 aom_highbd_12_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002151 aom_highbd_sad16x32x4d_bits12,
2152 aom_highbd_jnt_sad16x32_avg_bits12,
2153 aom_highbd_12_jnt_sub_pixel_avg_variance16x32);
2154
2155 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
2156 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
2157 aom_highbd_12_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002158 aom_highbd_12_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002159 aom_highbd_sad64x32x4d_bits12,
2160 aom_highbd_jnt_sad64x32_avg_bits12,
2161 aom_highbd_12_jnt_sub_pixel_avg_variance64x32);
2162
2163 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
2164 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
2165 aom_highbd_12_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002166 aom_highbd_12_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002167 aom_highbd_sad32x64x4d_bits12,
2168 aom_highbd_jnt_sad32x64_avg_bits12,
2169 aom_highbd_12_jnt_sub_pixel_avg_variance32x64);
2170
2171 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
2172 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
2173 aom_highbd_12_sub_pixel_variance32x32,
2174 aom_highbd_12_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002175 aom_highbd_sad32x32x4d_bits12,
2176 aom_highbd_jnt_sad32x32_avg_bits12,
2177 aom_highbd_12_jnt_sub_pixel_avg_variance32x32);
2178
2179 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2180 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2181 aom_highbd_12_sub_pixel_variance64x64,
2182 aom_highbd_12_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002183 aom_highbd_sad64x64x4d_bits12,
2184 aom_highbd_jnt_sad64x64_avg_bits12,
2185 aom_highbd_12_jnt_sub_pixel_avg_variance64x64);
2186
2187 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2188 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2189 aom_highbd_12_sub_pixel_variance16x16,
2190 aom_highbd_12_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002191 aom_highbd_sad16x16x4d_bits12,
2192 aom_highbd_jnt_sad16x16_avg_bits12,
2193 aom_highbd_12_jnt_sub_pixel_avg_variance16x16);
2194
2195 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2196 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2197 aom_highbd_12_sub_pixel_variance16x8,
2198 aom_highbd_12_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002199 aom_highbd_sad16x8x4d_bits12,
2200 aom_highbd_jnt_sad16x8_avg_bits12,
2201 aom_highbd_12_jnt_sub_pixel_avg_variance16x8);
2202
2203 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2204 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2205 aom_highbd_12_sub_pixel_variance8x16,
2206 aom_highbd_12_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002207 aom_highbd_sad8x16x4d_bits12,
2208 aom_highbd_jnt_sad8x16_avg_bits12,
2209 aom_highbd_12_jnt_sub_pixel_avg_variance8x16);
2210
2211 HIGHBD_BFP(
2212 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2213 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002214 aom_highbd_12_sub_pixel_avg_variance8x8,
2215 aom_highbd_sad8x8x4d_bits12, aom_highbd_jnt_sad8x8_avg_bits12,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002216 aom_highbd_12_jnt_sub_pixel_avg_variance8x8);
2217
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002218 HIGHBD_BFP(
2219 BLOCK_8X4, aom_highbd_sad8x4_bits12, aom_highbd_sad8x4_avg_bits12,
2220 aom_highbd_12_variance8x4, aom_highbd_12_sub_pixel_variance8x4,
2221 aom_highbd_12_sub_pixel_avg_variance8x4,
2222 aom_highbd_sad8x4x4d_bits12, aom_highbd_jnt_sad8x4_avg_bits12,
2223 aom_highbd_12_jnt_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002224
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002225 HIGHBD_BFP(
2226 BLOCK_4X8, aom_highbd_sad4x8_bits12, aom_highbd_sad4x8_avg_bits12,
2227 aom_highbd_12_variance4x8, aom_highbd_12_sub_pixel_variance4x8,
2228 aom_highbd_12_sub_pixel_avg_variance4x8,
2229 aom_highbd_sad4x8x4d_bits12, aom_highbd_jnt_sad4x8_avg_bits12,
2230 aom_highbd_12_jnt_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002231
2232 HIGHBD_BFP(
2233 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2234 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002235 aom_highbd_12_sub_pixel_avg_variance4x4,
2236 aom_highbd_sad4x4x4d_bits12, aom_highbd_jnt_sad4x4_avg_bits12,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002237 aom_highbd_12_jnt_sub_pixel_avg_variance4x4);
2238
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002239 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits12,
2240 aom_highbd_sad128x128_avg_bits12,
2241 aom_highbd_12_variance128x128,
2242 aom_highbd_12_sub_pixel_variance128x128,
2243 aom_highbd_12_sub_pixel_avg_variance128x128,
2244 aom_highbd_sad128x128x4d_bits12,
2245 aom_highbd_jnt_sad128x128_avg_bits12,
2246 aom_highbd_12_jnt_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002247
2248 HIGHBD_BFP(
2249 BLOCK_128X64, aom_highbd_sad128x64_bits12,
2250 aom_highbd_sad128x64_avg_bits12, aom_highbd_12_variance128x64,
2251 aom_highbd_12_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002252 aom_highbd_12_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002253 aom_highbd_sad128x64x4d_bits12, aom_highbd_jnt_sad128x64_avg_bits12,
2254 aom_highbd_12_jnt_sub_pixel_avg_variance128x64);
2255
2256 HIGHBD_BFP(
2257 BLOCK_64X128, aom_highbd_sad64x128_bits12,
2258 aom_highbd_sad64x128_avg_bits12, aom_highbd_12_variance64x128,
2259 aom_highbd_12_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002260 aom_highbd_12_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002261 aom_highbd_sad64x128x4d_bits12, aom_highbd_jnt_sad64x128_avg_bits12,
2262 aom_highbd_12_jnt_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002263
David Barkerf19f35f2017-05-22 16:33:22 +01002264 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2265 aom_highbd_12_masked_sub_pixel_variance128x128)
2266 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2267 aom_highbd_12_masked_sub_pixel_variance128x64)
2268 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2269 aom_highbd_12_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002270 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2271 aom_highbd_12_masked_sub_pixel_variance64x64)
2272 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2273 aom_highbd_12_masked_sub_pixel_variance64x32)
2274 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2275 aom_highbd_12_masked_sub_pixel_variance32x64)
2276 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2277 aom_highbd_12_masked_sub_pixel_variance32x32)
2278 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2279 aom_highbd_12_masked_sub_pixel_variance32x16)
2280 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2281 aom_highbd_12_masked_sub_pixel_variance16x32)
2282 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2283 aom_highbd_12_masked_sub_pixel_variance16x16)
2284 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2285 aom_highbd_12_masked_sub_pixel_variance8x16)
2286 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2287 aom_highbd_12_masked_sub_pixel_variance16x8)
2288 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2289 aom_highbd_12_masked_sub_pixel_variance8x8)
2290 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2291 aom_highbd_12_masked_sub_pixel_variance4x8)
2292 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2293 aom_highbd_12_masked_sub_pixel_variance8x4)
2294 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2295 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002296 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2297 aom_highbd_12_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002298 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2299 aom_highbd_12_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002300 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2301 aom_highbd_12_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002302 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2303 aom_highbd_12_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002304 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2305 aom_highbd_12_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002306 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2307 aom_highbd_12_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002308 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2309 aom_highbd_12_obmc_variance128x128,
2310 aom_highbd_12_obmc_sub_pixel_variance128x128)
2311 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2312 aom_highbd_12_obmc_variance128x64,
2313 aom_highbd_12_obmc_sub_pixel_variance128x64)
2314 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2315 aom_highbd_12_obmc_variance64x128,
2316 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002317 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2318 aom_highbd_12_obmc_variance64x64,
2319 aom_highbd_12_obmc_sub_pixel_variance64x64)
2320 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2321 aom_highbd_12_obmc_variance64x32,
2322 aom_highbd_12_obmc_sub_pixel_variance64x32)
2323 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2324 aom_highbd_12_obmc_variance32x64,
2325 aom_highbd_12_obmc_sub_pixel_variance32x64)
2326 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2327 aom_highbd_12_obmc_variance32x32,
2328 aom_highbd_12_obmc_sub_pixel_variance32x32)
2329 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2330 aom_highbd_12_obmc_variance32x16,
2331 aom_highbd_12_obmc_sub_pixel_variance32x16)
2332 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2333 aom_highbd_12_obmc_variance16x32,
2334 aom_highbd_12_obmc_sub_pixel_variance16x32)
2335 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2336 aom_highbd_12_obmc_variance16x16,
2337 aom_highbd_12_obmc_sub_pixel_variance16x16)
2338 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2339 aom_highbd_12_obmc_variance8x16,
2340 aom_highbd_12_obmc_sub_pixel_variance8x16)
2341 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2342 aom_highbd_12_obmc_variance16x8,
2343 aom_highbd_12_obmc_sub_pixel_variance16x8)
2344 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2345 aom_highbd_12_obmc_variance8x8,
2346 aom_highbd_12_obmc_sub_pixel_variance8x8)
2347 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2348 aom_highbd_12_obmc_variance4x8,
2349 aom_highbd_12_obmc_sub_pixel_variance4x8)
2350 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2351 aom_highbd_12_obmc_variance8x4,
2352 aom_highbd_12_obmc_sub_pixel_variance8x4)
2353 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2354 aom_highbd_12_obmc_variance4x4,
2355 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002356 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2357 aom_highbd_12_obmc_variance64x16,
2358 aom_highbd_12_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002359 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2360 aom_highbd_12_obmc_variance16x64,
2361 aom_highbd_12_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002362 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2363 aom_highbd_12_obmc_variance32x8,
2364 aom_highbd_12_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002365 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2366 aom_highbd_12_obmc_variance8x32,
2367 aom_highbd_12_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002368 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2369 aom_highbd_12_obmc_variance16x4,
2370 aom_highbd_12_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002371 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2372 aom_highbd_12_obmc_variance4x16,
2373 aom_highbd_12_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002374 break;
2375
2376 default:
2377 assert(0 &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002378 "cm->seq_params.bit_depth should be AOM_BITS_8, "
Yaowu Xuf883b422016-08-30 14:01:10 -07002379 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002380 }
2381 }
2382}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002383
Yaowu Xuf883b422016-08-30 14:01:10 -07002384static void realloc_segmentation_maps(AV1_COMP *cpi) {
2385 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002386
2387 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07002388 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002389 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002390 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002391
2392 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002393 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002394 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
Yaowu Xuf883b422016-08-30 14:01:10 -07002395 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002396
2397 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002398 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002399 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002400 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002401}
2402
Yaowu Xuf883b422016-08-30 14:01:10 -07002403void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2404 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002405 SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002406 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002407 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002408 MACROBLOCK *const x = &cpi->td.mb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002409
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002410 if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
2411 seq_params->bit_depth = oxcf->bit_depth;
2412 seq_params->color_primaries = oxcf->color_primaries;
2413 seq_params->transfer_characteristics = oxcf->transfer_characteristics;
2414 seq_params->matrix_coefficients = oxcf->matrix_coefficients;
2415 seq_params->monochrome = oxcf->monochrome;
2416 seq_params->chroma_sample_position = oxcf->chroma_sample_position;
2417 seq_params->color_range = oxcf->color_range;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002418
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002419 assert(IMPLIES(seq_params->profile <= PROFILE_1,
2420 seq_params->bit_depth <= AOM_BITS_10));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002421
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002422 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08002423 cm->timing_info.num_units_in_display_tick =
2424 oxcf->timing_info.num_units_in_display_tick;
2425 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
2426 cm->timing_info.equal_picture_interval =
2427 oxcf->timing_info.equal_picture_interval;
2428 cm->timing_info.num_ticks_per_picture =
2429 oxcf->timing_info.num_ticks_per_picture;
2430
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002431 seq_params->display_model_info_present_flag =
Andrey Norkin26495512018-06-20 17:13:11 -07002432 oxcf->display_model_info_present_flag;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002433 seq_params->decoder_model_info_present_flag =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002434 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08002435 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002436 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08002437 cm->buffer_model.num_units_in_decoding_tick =
2438 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07002439 cm->buffer_removal_time_present = 1;
Andrey Norkin795ba872018-03-06 13:24:14 -08002440 set_aom_dec_model_info(&cm->buffer_model);
Andrey Norkin26495512018-06-20 17:13:11 -07002441 set_dec_model_op_parameters(&cm->op_params[0]);
2442 } else if (cm->timing_info_present &&
2443 cm->timing_info.equal_picture_interval &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002444 !seq_params->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002445 // set the decoder model parameters in resource availability mode
2446 set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07002447 } else {
2448 cm->op_params[0].initial_display_delay =
2449 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08002450 }
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002451
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002452 update_film_grain_parameters(cpi, oxcf);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002453
Yaowu Xuc27fc142016-08-22 16:08:15 -07002454 cpi->oxcf = *oxcf;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01002455 cpi->common.options = oxcf->cfg;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002456 x->e_mbd.bd = (int)seq_params->bit_depth;
hui sud9a812b2017-07-06 14:34:37 -07002457 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002458
Yaowu Xuf883b422016-08-30 14:01:10 -07002459 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002460 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2461 } else {
2462 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2463 }
2464
2465 cpi->refresh_last_frame = 1;
2466 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002467 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07002468 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002469
Debargha Mukherjee229fdc82018-03-10 07:45:33 -08002470 cm->refresh_frame_context = (oxcf->frame_parallel_decoding_mode)
2471 ? REFRESH_FRAME_CONTEXT_DISABLED
2472 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002473 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08002474 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002475
Alex Converse74ad0912017-07-18 10:22:58 -07002476 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002477 CHECK_MEM_ERROR(cm, x->palette_buffer,
2478 aom_memalign(16, sizeof(*x->palette_buffer)));
2479 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002480
2481 if (x->tmp_conv_dst == NULL) {
2482 CHECK_MEM_ERROR(
2483 cm, x->tmp_conv_dst,
2484 aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002485 x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002486 }
2487 for (int i = 0; i < 2; ++i) {
2488 if (x->tmp_obmc_bufs[i] == NULL) {
2489 CHECK_MEM_ERROR(cm, x->tmp_obmc_bufs[i],
wenyao.liu22d8ab32018-10-16 09:11:29 +08002490 aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002491 sizeof(*x->tmp_obmc_bufs[i])));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002492 x->e_mbd.tmp_obmc_bufs[i] = x->tmp_obmc_bufs[i];
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002493 }
2494 }
2495
Yaowu Xuf883b422016-08-30 14:01:10 -07002496 av1_reset_segment_features(cm);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002497 set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002498
Yaowu Xuc27fc142016-08-22 16:08:15 -07002499 set_rc_buffer_sizes(rc, &cpi->oxcf);
2500
2501 // Under a configuration change, where maximum_buffer_size may change,
2502 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002503 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2504 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002505
2506 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002507 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002508
2509 // Set absolute upper and lower quality limits
2510 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2511 rc->best_quality = cpi->oxcf.best_allowed_q;
2512
Urvang Joshib55cb5e2018-09-12 14:50:21 -07002513 cm->interp_filter = oxcf->large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE;
Yue Chen5380cb52018-02-23 15:33:21 -08002514 cm->switchable_motion_mode = 1;
2515
Yaowu Xuc27fc142016-08-22 16:08:15 -07002516 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2517 cm->render_width = cpi->oxcf.render_width;
2518 cm->render_height = cpi->oxcf.render_height;
2519 } else {
2520 cm->render_width = cpi->oxcf.width;
2521 cm->render_height = cpi->oxcf.height;
2522 }
2523 cm->width = cpi->oxcf.width;
2524 cm->height = cpi->oxcf.height;
2525
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002526 int sb_size = seq_params->sb_size;
Urvang Joshie4530f82018-01-09 11:43:37 -08002527 // Superblock size should not be updated after the first key frame.
2528 if (!cpi->seq_params_locked) {
2529 set_sb_size(&cm->seq_params, select_sb_size(cpi));
2530 }
Dominic Symes917d6c02017-10-11 18:00:52 +02002531
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002532 if (cpi->initial_width || sb_size != seq_params->sb_size) {
Dominic Symes917d6c02017-10-11 18:00:52 +02002533 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height ||
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002534 seq_params->sb_size != sb_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002535 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002536 av1_free_pc_tree(&cpi->td, num_planes);
Cheng Chen46f30c72017-09-07 11:13:33 -07002537 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002538 realloc_segmentation_maps(cpi);
2539 cpi->initial_width = cpi->initial_height = 0;
2540 }
2541 }
2542 update_frame_size(cpi);
2543
2544 cpi->alt_ref_source = NULL;
2545 rc->is_src_frame_alt_ref = 0;
2546
Yaowu Xuc27fc142016-08-22 16:08:15 -07002547 rc->is_bwd_ref_frame = 0;
2548 rc->is_last_bipred_frame = 0;
2549 rc->is_bipred_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002550
Yaowu Xuc27fc142016-08-22 16:08:15 -07002551 set_tile_info(cpi);
2552
2553 cpi->ext_refresh_frame_flags_pending = 0;
2554 cpi->ext_refresh_frame_context_pending = 0;
2555
Yaowu Xuc27fc142016-08-22 16:08:15 -07002556 highbd_set_var_fns(cpi);
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00002557
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07002558 // Init sequence level coding tools
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002559 // This should not be called after the first key frame.
2560 if (!cpi->seq_params_locked) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002561 seq_params->operating_points_cnt_minus_1 =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002562 cm->number_spatial_layers > 1 ? cm->number_spatial_layers - 1 : 0;
Andrey Norkin26495512018-06-20 17:13:11 -07002563 init_seq_coding_tools(&cm->seq_params, cm, oxcf);
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002564 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002565}
2566
Yaowu Xuf883b422016-08-30 14:01:10 -07002567AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
2568 BufferPool *const pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002569 unsigned int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07002570 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2571 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002572
2573 if (!cm) return NULL;
2574
Yaowu Xuf883b422016-08-30 14:01:10 -07002575 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002576
Wan-Teh Changa2fad3e2018-07-19 16:55:19 -07002577 // The jmp_buf is valid only for the duration of the function that calls
2578 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
2579 // before it returns.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002580 if (setjmp(cm->error.jmp)) {
2581 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002582 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002583 return 0;
2584 }
2585
2586 cm->error.setjmp = 1;
Cheng Chen46f30c72017-09-07 11:13:33 -07002587 cm->alloc_mi = enc_alloc_mi;
2588 cm->free_mi = enc_free_mi;
2589 cm->setup_mi = enc_setup_mi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002590
Angie Chianga5d96c42016-10-21 16:16:56 -07002591 CHECK_MEM_ERROR(cm, cm->fc,
2592 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
2593 CHECK_MEM_ERROR(cm, cm->frame_contexts,
2594 (FRAME_CONTEXT *)aom_memalign(
2595 32, FRAME_CONTEXTS * sizeof(*cm->frame_contexts)));
2596 memset(cm->fc, 0, sizeof(*cm->fc));
2597 memset(cm->frame_contexts, 0, FRAME_CONTEXTS * sizeof(*cm->frame_contexts));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002598
2599 cpi->resize_state = 0;
2600 cpi->resize_avg_qp = 0;
2601 cpi->resize_buffer_underflow = 0;
Fergus Simpsonddc846e2017-04-24 18:09:13 -07002602
Yaowu Xuc27fc142016-08-22 16:08:15 -07002603 cpi->common.buffer_pool = pool;
2604
2605 init_config(cpi, oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07002606 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002607
2608 cm->current_video_frame = 0;
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002609 cpi->seq_params_locked = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002610 cpi->partition_search_skippable_frame = 0;
2611 cpi->tile_data = NULL;
2612 cpi->last_show_frame_buf_idx = INVALID_IDX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002613 realloc_segmentation_maps(cpi);
2614
Jingning Hanf050fc12018-03-09 14:53:33 -08002615 memset(cpi->nmv_costs, 0, sizeof(cpi->nmv_costs));
2616 memset(cpi->nmv_costs_hp, 0, sizeof(cpi->nmv_costs_hp));
James Zern01a9d702017-08-25 19:09:33 +00002617
Yaowu Xuc27fc142016-08-22 16:08:15 -07002618 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2619 i++) {
2620 CHECK_MEM_ERROR(
2621 cm, cpi->mbgraph_stats[i].mb_stats,
Yaowu Xuf883b422016-08-30 14:01:10 -07002622 aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002623 }
2624
2625#if CONFIG_FP_MB_STATS
2626 cpi->use_fp_mb_stats = 0;
2627 if (cpi->use_fp_mb_stats) {
2628 // a place holder used to store the first pass mb stats in the first pass
2629 CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
Yaowu Xuf883b422016-08-30 14:01:10 -07002630 aom_calloc(cm->MBs * sizeof(uint8_t), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002631 } else {
2632 cpi->twopass.frame_mb_stats_buf = NULL;
2633 }
2634#endif
2635
2636 cpi->refresh_alt_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002637
2638 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2639#if CONFIG_INTERNAL_STATS
2640 cpi->b_calculate_blockiness = 1;
2641 cpi->b_calculate_consistency = 1;
2642 cpi->total_inconsistency = 0;
2643 cpi->psnr.worst = 100.0;
2644 cpi->worst_ssim = 100.0;
2645
2646 cpi->count = 0;
2647 cpi->bytes = 0;
2648
2649 if (cpi->b_calculate_psnr) {
2650 cpi->total_sq_error = 0;
2651 cpi->total_samples = 0;
2652 cpi->tot_recode_hits = 0;
2653 cpi->summed_quality = 0;
2654 cpi->summed_weights = 0;
2655 }
2656
2657 cpi->fastssim.worst = 100.0;
2658 cpi->psnrhvs.worst = 100.0;
2659
2660 if (cpi->b_calculate_blockiness) {
2661 cpi->total_blockiness = 0;
2662 cpi->worst_blockiness = 0.0;
2663 }
2664
2665 if (cpi->b_calculate_consistency) {
2666 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
Yaowu Xuf883b422016-08-30 14:01:10 -07002667 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07002668 cpi->common.mi_rows * cpi->common.mi_cols));
2669 cpi->worst_consistency = 100.0;
2670 }
2671#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002672#if CONFIG_ENTROPY_STATS
2673 av1_zero(aggregate_fc);
2674#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002675
2676 cpi->first_time_stamp_ever = INT64_MAX;
2677
Jingning Hanf050fc12018-03-09 14:53:33 -08002678 cpi->td.mb.nmvcost[0] = &cpi->nmv_costs[0][MV_MAX];
2679 cpi->td.mb.nmvcost[1] = &cpi->nmv_costs[1][MV_MAX];
2680 cpi->td.mb.nmvcost_hp[0] = &cpi->nmv_costs_hp[0][MV_MAX];
2681 cpi->td.mb.nmvcost_hp[1] = &cpi->nmv_costs_hp[1][MV_MAX];
James Zern01a9d702017-08-25 19:09:33 +00002682
Yaowu Xuc27fc142016-08-22 16:08:15 -07002683#ifdef OUTPUT_YUV_SKINMAP
2684 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
2685#endif
2686#ifdef OUTPUT_YUV_REC
2687 yuv_rec_file = fopen("rec.yuv", "wb");
2688#endif
2689
Yaowu Xuc27fc142016-08-22 16:08:15 -07002690 if (oxcf->pass == 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002691 av1_init_first_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002692 } else if (oxcf->pass == 2) {
2693 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2694 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2695
2696#if CONFIG_FP_MB_STATS
2697 if (cpi->use_fp_mb_stats) {
2698 const size_t psz = cpi->common.MBs * sizeof(uint8_t);
2699 const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
2700
2701 cpi->twopass.firstpass_mb_stats.mb_stats_start =
2702 oxcf->firstpass_mb_stats_in.buf;
2703 cpi->twopass.firstpass_mb_stats.mb_stats_end =
2704 cpi->twopass.firstpass_mb_stats.mb_stats_start +
2705 (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
2706 }
2707#endif
2708
2709 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2710 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2711 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2712
Yaowu Xuf883b422016-08-30 14:01:10 -07002713 av1_init_second_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002714 }
2715
Jingning Hand064cf02017-06-01 10:00:39 -07002716 CHECK_MEM_ERROR(
2717 cm, cpi->td.mb.above_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002718 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002719 sizeof(*cpi->td.mb.above_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002720 CHECK_MEM_ERROR(
2721 cm, cpi->td.mb.left_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002722 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002723 sizeof(*cpi->td.mb.left_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002724
2725 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
2726 (int32_t *)aom_memalign(
2727 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
2728
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05302729#if CONFIG_COLLECT_INTER_MODE_RD_STATS
2730 CHECK_MEM_ERROR(
2731 cm, cpi->td.mb.inter_modes_info,
2732 (InterModesInfo *)aom_malloc(sizeof(*cpi->td.mb.inter_modes_info)));
2733#endif
2734
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05302735 for (int x = 0; x < 2; x++)
2736 for (int y = 0; y < 2; y++)
2737 CHECK_MEM_ERROR(
2738 cm, cpi->td.mb.hash_value_buffer[x][y],
2739 (uint32_t *)aom_malloc(AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
2740 sizeof(*cpi->td.mb.hash_value_buffer[0][0])));
2741
2742 cpi->td.mb.g_crc_initialized = 0;
2743
Jingning Hand064cf02017-06-01 10:00:39 -07002744 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
2745 (int32_t *)aom_memalign(
2746 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
2747
Yaowu Xuf883b422016-08-30 14:01:10 -07002748 av1_set_speed_features_framesize_independent(cpi);
2749 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002750
Yue Chen7cae98f2018-08-24 10:43:16 -07002751 for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) {
2752 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
2753 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
2754
2755 CHECK_MEM_ERROR(cm, cpi->tpl_stats[frame].tpl_stats_ptr,
2756 aom_calloc(mi_rows * mi_cols,
2757 sizeof(*cpi->tpl_stats[frame].tpl_stats_ptr)));
2758 cpi->tpl_stats[frame].is_valid = 0;
2759 cpi->tpl_stats[frame].width = mi_cols;
2760 cpi->tpl_stats[frame].height = mi_rows;
2761 cpi->tpl_stats[frame].stride = mi_cols;
2762 cpi->tpl_stats[frame].mi_rows = cm->mi_rows;
2763 cpi->tpl_stats[frame].mi_cols = cm->mi_cols;
2764 }
2765
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002766#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
2767 cpi->fn_ptr[BT].sdf = SDF; \
2768 cpi->fn_ptr[BT].sdaf = SDAF; \
2769 cpi->fn_ptr[BT].vf = VF; \
2770 cpi->fn_ptr[BT].svf = SVF; \
2771 cpi->fn_ptr[BT].svaf = SVAF; \
2772 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
2773 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenf78632e2017-10-20 15:30:51 -07002774 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002775
Cheng Chenf78632e2017-10-20 15:30:51 -07002776 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002777 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
Cheng Chend0179a62017-11-16 17:02:53 -08002778 aom_sad4x16x4d, aom_jnt_sad4x16_avg, aom_jnt_sub_pixel_avg_variance4x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002779
2780 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002781 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
Cheng Chend0179a62017-11-16 17:02:53 -08002782 aom_sad16x4x4d, aom_jnt_sad16x4_avg, aom_jnt_sub_pixel_avg_variance16x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002783
2784 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002785 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
Cheng Chend0179a62017-11-16 17:02:53 -08002786 aom_sad8x32x4d, aom_jnt_sad8x32_avg, aom_jnt_sub_pixel_avg_variance8x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002787
2788 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002789 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
Cheng Chend0179a62017-11-16 17:02:53 -08002790 aom_sad32x8x4d, aom_jnt_sad32x8_avg, aom_jnt_sub_pixel_avg_variance32x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002791
2792 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002793 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
Cheng Chend0179a62017-11-16 17:02:53 -08002794 aom_sad16x64x4d, aom_jnt_sad16x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002795 aom_jnt_sub_pixel_avg_variance16x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002796
2797 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002798 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
Cheng Chend0179a62017-11-16 17:02:53 -08002799 aom_sad64x16x4d, aom_jnt_sad64x16_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002800 aom_jnt_sub_pixel_avg_variance64x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002801
Cheng Chenf78632e2017-10-20 15:30:51 -07002802 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
2803 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002804 aom_sad128x128x4d, aom_jnt_sad128x128_avg,
2805 aom_jnt_sub_pixel_avg_variance128x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002806
2807 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002808 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
2809 aom_sad128x64x4d, aom_jnt_sad128x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002810 aom_jnt_sub_pixel_avg_variance128x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002811
2812 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002813 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
2814 aom_sad64x128x4d, aom_jnt_sad64x128_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002815 aom_jnt_sub_pixel_avg_variance64x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002816
2817 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002818 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
Cheng Chend0179a62017-11-16 17:02:53 -08002819 aom_sad32x16x4d, aom_jnt_sad32x16_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002820 aom_jnt_sub_pixel_avg_variance32x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002821
2822 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002823 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
Cheng Chend0179a62017-11-16 17:02:53 -08002824 aom_sad16x32x4d, aom_jnt_sad16x32_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002825 aom_jnt_sub_pixel_avg_variance16x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002826
2827 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002828 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
Cheng Chend0179a62017-11-16 17:02:53 -08002829 aom_sad64x32x4d, aom_jnt_sad64x32_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002830 aom_jnt_sub_pixel_avg_variance64x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002831
2832 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002833 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
Cheng Chend0179a62017-11-16 17:02:53 -08002834 aom_sad32x64x4d, aom_jnt_sad32x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002835 aom_jnt_sub_pixel_avg_variance32x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002836
2837 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
2838 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002839 aom_sad32x32x4d, aom_jnt_sad32x32_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002840 aom_jnt_sub_pixel_avg_variance32x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002841
2842 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
2843 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002844 aom_sad64x64x4d, aom_jnt_sad64x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002845 aom_jnt_sub_pixel_avg_variance64x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002846
2847 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
2848 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002849 aom_sad16x16x4d, aom_jnt_sad16x16_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002850 aom_jnt_sub_pixel_avg_variance16x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002851
2852 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002853 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
2854 aom_sad16x8x4d, aom_jnt_sad16x8_avg, aom_jnt_sub_pixel_avg_variance16x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002855
2856 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002857 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
2858 aom_sad8x16x4d, aom_jnt_sad8x16_avg, aom_jnt_sub_pixel_avg_variance8x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002859
2860 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002861 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
2862 aom_jnt_sad8x8_avg, aom_jnt_sub_pixel_avg_variance8x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002863
2864 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002865 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
2866 aom_jnt_sad8x4_avg, aom_jnt_sub_pixel_avg_variance8x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002867
2868 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002869 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
2870 aom_jnt_sad4x8_avg, aom_jnt_sub_pixel_avg_variance4x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002871
2872 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002873 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
2874 aom_jnt_sad4x4_avg, aom_jnt_sub_pixel_avg_variance4x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002875
Yaowu Xuc27fc142016-08-22 16:08:15 -07002876#define OBFP(BT, OSDF, OVF, OSVF) \
2877 cpi->fn_ptr[BT].osdf = OSDF; \
2878 cpi->fn_ptr[BT].ovf = OVF; \
2879 cpi->fn_ptr[BT].osvf = OSVF;
2880
Yaowu Xuf883b422016-08-30 14:01:10 -07002881 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
2882 aom_obmc_sub_pixel_variance128x128)
2883 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
2884 aom_obmc_sub_pixel_variance128x64)
2885 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
2886 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002887 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
2888 aom_obmc_sub_pixel_variance64x64)
2889 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
2890 aom_obmc_sub_pixel_variance64x32)
2891 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
2892 aom_obmc_sub_pixel_variance32x64)
2893 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
2894 aom_obmc_sub_pixel_variance32x32)
2895 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
2896 aom_obmc_sub_pixel_variance32x16)
2897 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
2898 aom_obmc_sub_pixel_variance16x32)
2899 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
2900 aom_obmc_sub_pixel_variance16x16)
2901 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
2902 aom_obmc_sub_pixel_variance16x8)
2903 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
2904 aom_obmc_sub_pixel_variance8x16)
2905 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
2906 aom_obmc_sub_pixel_variance8x8)
2907 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
2908 aom_obmc_sub_pixel_variance4x8)
2909 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
2910 aom_obmc_sub_pixel_variance8x4)
2911 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
2912 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002913 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
2914 aom_obmc_sub_pixel_variance4x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002915 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
2916 aom_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002917 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
2918 aom_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002919 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
2920 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002921 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
2922 aom_obmc_sub_pixel_variance16x64)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002923 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
2924 aom_obmc_sub_pixel_variance64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002925
David Barkerf19f35f2017-05-22 16:33:22 +01002926#define MBFP(BT, MCSDF, MCSVF) \
2927 cpi->fn_ptr[BT].msdf = MCSDF; \
2928 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002929
David Barkerf19f35f2017-05-22 16:33:22 +01002930 MBFP(BLOCK_128X128, aom_masked_sad128x128,
2931 aom_masked_sub_pixel_variance128x128)
2932 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
2933 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002934 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
2935 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
2936 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
2937 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
2938 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
2939 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
2940 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
2941 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
2942 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
2943 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
2944 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
2945 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
2946 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002947
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002948 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
2949
2950 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
2951
2952 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
2953
2954 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002955
2956 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
2957
2958 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002959
Yaowu Xuc27fc142016-08-22 16:08:15 -07002960 highbd_set_var_fns(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002961
Yaowu Xuf883b422016-08-30 14:01:10 -07002962 /* av1_init_quantizer() is first called here. Add check in
2963 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07002964 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07002965 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002966 */
Yaowu Xuf883b422016-08-30 14:01:10 -07002967 av1_init_quantizer(cpi);
Zoe Liud902b742018-02-19 17:02:41 -08002968 av1_qm_init(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002969
Yaowu Xuf883b422016-08-30 14:01:10 -07002970 av1_loop_filter_init(cm);
Urvang Joshide71d142017-10-05 12:12:15 -07002971 cm->superres_scale_denominator = SCALE_NUMERATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07002972 cm->superres_upscaled_width = oxcf->width;
2973 cm->superres_upscaled_height = oxcf->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002974 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002975
2976 cm->error.setjmp = 0;
2977
2978 return cpi;
2979}
2980
Urvang Joshiee2c8112018-05-04 14:53:15 -07002981#if CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002982#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
2983
2984#define SNPRINT2(H, T, V) \
2985 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
Urvang Joshiee2c8112018-05-04 14:53:15 -07002986#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002987
Yaowu Xuf883b422016-08-30 14:01:10 -07002988void av1_remove_compressor(AV1_COMP *cpi) {
2989 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002990 unsigned int i;
2991 int t;
2992
2993 if (!cpi) return;
2994
2995 cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002996 const int num_planes = av1_num_planes(cm);
2997
Yaowu Xuc27fc142016-08-22 16:08:15 -07002998 if (cm->current_video_frame > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002999#if CONFIG_ENTROPY_STATS
3000 if (cpi->oxcf.pass != 1) {
3001 fprintf(stderr, "Writing counts.stt\n");
3002 FILE *f = fopen("counts.stt", "wb");
3003 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
3004 fclose(f);
3005 }
3006#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003007#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003008 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003009
3010 if (cpi->oxcf.pass != 1) {
3011 char headings[512] = { 0 };
3012 char results[512] = { 0 };
3013 FILE *f = fopen("opsnr.stt", "a");
3014 double time_encoded =
3015 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
3016 10000000.000;
3017 double total_encode_time =
3018 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
3019 const double dr =
3020 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
3021 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
3022 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
3023 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
3024
3025 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003026 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003027 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
3028 const double total_ssim =
3029 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
3030 snprintf(headings, sizeof(headings),
Jingning Han87651b22017-11-28 20:02:26 -08003031 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07003032 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003033 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
Jingning Han87651b22017-11-28 20:02:26 -08003034 "AVPsrnY\tAPsnrCb\tAPsnrCr");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003035 snprintf(results, sizeof(results),
3036 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3037 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003038 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Han87651b22017-11-28 20:02:26 -08003039 "%7.3f\t%7.3f\t%7.3f",
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003040 dr, cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr,
3041 cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr, total_ssim,
3042 total_ssim, cpi->fastssim.stat[STAT_ALL] / cpi->count,
3043 cpi->psnrhvs.stat[STAT_ALL] / cpi->count, cpi->psnr.worst,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003044 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003045 cpi->psnr.stat[STAT_Y] / cpi->count,
3046 cpi->psnr.stat[STAT_U] / cpi->count,
3047 cpi->psnr.stat[STAT_V] / cpi->count);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003048
3049 if (cpi->b_calculate_blockiness) {
3050 SNPRINT(headings, "\t Block\tWstBlck");
3051 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
3052 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
3053 }
3054
3055 if (cpi->b_calculate_consistency) {
3056 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07003057 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003058 (double)cpi->total_inconsistency);
3059
3060 SNPRINT(headings, "\tConsist\tWstCons");
3061 SNPRINT2(results, "\t%7.3f", consistency);
3062 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
3063 }
Sarah Parkerf97b7862016-08-25 17:42:57 -07003064 fprintf(f, "%s\t Time\tRcErr\tAbsErr\n", headings);
3065 fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003066 rate_err, fabs(rate_err));
3067 }
3068
3069 fclose(f);
3070 }
Urvang Joshiee2c8112018-05-04 14:53:15 -07003071#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003072 }
3073
Yue Chen7cae98f2018-08-24 10:43:16 -07003074 for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) {
3075 aom_free(cpi->tpl_stats[frame].tpl_stats_ptr);
3076 cpi->tpl_stats[frame].is_valid = 0;
3077 }
3078
Yaowu Xuc27fc142016-08-22 16:08:15 -07003079 for (t = 0; t < cpi->num_workers; ++t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003080 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003081 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
3082
3083 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07003084 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003085
3086 // Deallocate allocated thread data.
Ravi Chaudhary1cf7d162018-10-09 17:00:43 +05303087 if (cpi->row_mt == 1) aom_free(thread_data->td->tctx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003088 if (t < cpi->num_workers - 1) {
hui sud9a812b2017-07-06 14:34:37 -07003089 aom_free(thread_data->td->palette_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003090 aom_free(thread_data->td->tmp_conv_dst);
3091 for (int j = 0; j < 2; ++j) {
3092 aom_free(thread_data->td->tmp_obmc_bufs[j]);
3093 }
Jingning Hand064cf02017-06-01 10:00:39 -07003094 aom_free(thread_data->td->above_pred_buf);
3095 aom_free(thread_data->td->left_pred_buf);
3096 aom_free(thread_data->td->wsrc_buf);
wenyao.liu22d8ab32018-10-16 09:11:29 +08003097
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303098#if CONFIG_COLLECT_INTER_MODE_RD_STATS
3099 aom_free(thread_data->td->inter_modes_info);
3100#endif
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003101 for (int x = 0; x < 2; x++) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303102 for (int y = 0; y < 2; y++) {
3103 aom_free(thread_data->td->hash_value_buffer[x][y]);
3104 thread_data->td->hash_value_buffer[x][y] = NULL;
3105 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003106 }
Jingning Hand064cf02017-06-01 10:00:39 -07003107 aom_free(thread_data->td->mask_buf);
Yaowu Xuf883b422016-08-30 14:01:10 -07003108 aom_free(thread_data->td->counts);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003109 av1_free_pc_tree(thread_data->td, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07003110 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003111 }
3112 }
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303113#if CONFIG_MULTITHREAD
3114 if (cpi->row_mt == 1) {
3115 if (cpi->row_mt_mutex_ != NULL) {
3116 pthread_mutex_destroy(cpi->row_mt_mutex_);
3117 aom_free(cpi->row_mt_mutex_);
3118 }
3119 }
3120#endif
Ravi Chaudharyc5e74692018-10-08 16:05:38 +05303121 av1_row_mt_mem_dealloc(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07003122 aom_free(cpi->tile_thr_data);
3123 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003124
Deepa K G964e72e2018-05-16 16:56:01 +05303125 if (cpi->num_workers > 1) {
3126 av1_loop_filter_dealloc(&cpi->lf_row_sync);
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05303127 av1_loop_restoration_dealloc(&cpi->lr_row_sync, cpi->num_workers);
Deepa K G964e72e2018-05-16 16:56:01 +05303128 }
3129
Yaowu Xuc27fc142016-08-22 16:08:15 -07003130 dealloc_compressor_data(cpi);
3131
3132 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
3133 ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003134 aom_free(cpi->mbgraph_stats[i].mb_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003135 }
3136
3137#if CONFIG_FP_MB_STATS
3138 if (cpi->use_fp_mb_stats) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003139 aom_free(cpi->twopass.frame_mb_stats_buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003140 cpi->twopass.frame_mb_stats_buf = NULL;
3141 }
3142#endif
Debargha Mukherjee5d157212017-01-10 14:44:47 -08003143#if CONFIG_INTERNAL_STATS
3144 aom_free(cpi->ssim_vars);
3145 cpi->ssim_vars = NULL;
3146#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003147
Yaowu Xuf883b422016-08-30 14:01:10 -07003148 av1_remove_common(cm);
RogerZhou80d52342017-11-20 10:56:26 -08003149 for (i = 0; i < FRAME_BUFFERS; ++i) {
3150 av1_hash_table_destroy(&cm->buffer_pool->frame_bufs[i].hash_table);
3151 }
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08003152 if (cpi->sf.use_hash_based_trellis) hbt_destroy();
Yaowu Xuf883b422016-08-30 14:01:10 -07003153 av1_free_ref_frame_buffers(cm->buffer_pool);
3154 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003155
3156#ifdef OUTPUT_YUV_SKINMAP
3157 fclose(yuv_skinmap_file);
3158#endif
3159#ifdef OUTPUT_YUV_REC
3160 fclose(yuv_rec_file);
3161#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003162}
3163
Yaowu Xuf883b422016-08-30 14:01:10 -07003164static void generate_psnr_packet(AV1_COMP *cpi) {
3165 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003166 int i;
3167 PSNR_STATS psnr;
Alex Conversef77fd0b2017-04-20 11:00:24 -07003168 aom_calc_highbd_psnr(cpi->source, cpi->common.frame_to_show, &psnr,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003169 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003170
3171 for (i = 0; i < 4; ++i) {
3172 pkt.data.psnr.samples[i] = psnr.samples[i];
3173 pkt.data.psnr.sse[i] = psnr.sse[i];
3174 pkt.data.psnr.psnr[i] = psnr.psnr[i];
3175 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003176 pkt.kind = AOM_CODEC_PSNR_PKT;
3177 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003178}
3179
Yaowu Xuf883b422016-08-30 14:01:10 -07003180int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003181 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
3182
Yunqing Wangf2e7a392017-11-08 00:27:21 -08003183 cpi->ext_ref_frame_flags = ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003184 return 0;
3185}
3186
Yunqing Wang9a50fec2017-11-02 17:02:00 -07003187void av1_update_reference(AV1_COMP *cpi, int ref_frame_upd_flags) {
3188 cpi->ext_refresh_last_frame = (ref_frame_upd_flags & AOM_LAST_FLAG) != 0;
3189 cpi->ext_refresh_golden_frame = (ref_frame_upd_flags & AOM_GOLD_FLAG) != 0;
3190 cpi->ext_refresh_alt_ref_frame = (ref_frame_upd_flags & AOM_ALT_FLAG) != 0;
3191 cpi->ext_refresh_bwd_ref_frame = (ref_frame_upd_flags & AOM_BWD_FLAG) != 0;
3192 cpi->ext_refresh_alt2_ref_frame = (ref_frame_upd_flags & AOM_ALT2_FLAG) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003193 cpi->ext_refresh_frame_flags_pending = 1;
3194}
3195
Thomas Daede497d1952017-08-08 17:33:06 -07003196int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3197 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003198 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003199 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003200 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003201 aom_yv12_copy_frame(cfg, sd, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003202 return 0;
3203 } else {
3204 return -1;
3205 }
3206}
3207
Thomas Daede497d1952017-08-08 17:33:06 -07003208int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3209 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003210 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003211 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003212 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003213 aom_yv12_copy_frame(sd, cfg, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07003214 return 0;
3215 } else {
3216 return -1;
3217 }
3218}
3219
3220int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003221 cpi->ext_refresh_frame_context = update;
3222 cpi->ext_refresh_frame_context_pending = 1;
3223 return 0;
3224}
3225
3226#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
3227// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
3228// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
3229// not denoise the UV channels at this time. If ever we implement UV channel
3230// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07003231void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003232 uint8_t *src = s->y_buffer;
3233 int h = s->y_height;
3234
3235 do {
3236 fwrite(src, s->y_width, 1, f);
3237 src += s->y_stride;
3238 } while (--h);
3239
3240 src = s->u_buffer;
3241 h = s->uv_height;
3242
3243 do {
3244 fwrite(src, s->uv_width, 1, f);
3245 src += s->uv_stride;
3246 } while (--h);
3247
3248 src = s->v_buffer;
3249 h = s->uv_height;
3250
3251 do {
3252 fwrite(src, s->uv_width, 1, f);
3253 src += s->uv_stride;
3254 } while (--h);
3255}
3256#endif
3257
Yaowu Xuf883b422016-08-30 14:01:10 -07003258static void check_show_existing_frame(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003259 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Yaowu Xuf883b422016-08-30 14:01:10 -07003260 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003261 const FRAME_UPDATE_TYPE next_frame_update_type =
3262 gf_group->update_type[gf_group->index];
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003263#if USE_SYMM_MULTI_LAYER
3264 const int which_arf = (cpi->new_bwdref_update_rule == 1)
3265 ? gf_group->arf_update_idx[gf_group->index] > 0
3266 : gf_group->arf_update_idx[gf_group->index];
3267#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003268 const int which_arf = gf_group->arf_update_idx[gf_group->index];
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003269#endif
Zoe Liu5fca7242016-10-10 17:18:57 -07003270
3271 if (cm->show_existing_frame == 1) {
3272 cm->show_existing_frame = 0;
3273 } else if (cpi->rc.is_last_bipred_frame) {
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003274#if USE_SYMM_MULTI_LAYER
3275 // NOTE: When new structure is used, every bwdref will have one overlay
3276 // frame. Therefore, there is no need to find out which frame to
3277 // show in advance.
3278 if (cpi->new_bwdref_update_rule == 0) {
3279#endif
3280 // NOTE: If the current frame is a last bi-predictive frame, it is
3281 // needed next to show the BWDREF_FRAME, which is pointed by
3282 // the last_fb_idxes[0] after reference frame buffer update
3283 cpi->rc.is_last_bipred_frame = 0;
3284 cm->show_existing_frame = 1;
Urvang Joshi4d9f15f2018-11-05 15:26:22 -08003285 cpi->existing_fb_idx_to_show = cpi->remapped_ref_idx[0];
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003286#if USE_SYMM_MULTI_LAYER
3287 }
3288#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003289 } else if (cpi->is_arf_filter_off[which_arf] &&
3290 (next_frame_update_type == OVERLAY_UPDATE ||
3291 next_frame_update_type == INTNL_OVERLAY_UPDATE)) {
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003292#if USE_SYMM_MULTI_LAYER
3293 const int bwdref_to_show =
3294 (cpi->new_bwdref_update_rule == 1) ? BWDREF_FRAME : ALTREF2_FRAME;
3295#else
3296 const int bwdref_to_show = ALTREF2_FRAME;
3297#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003298 // Other parameters related to OVERLAY_UPDATE will be taken care of
Yaowu Xuf883b422016-08-30 14:01:10 -07003299 // in av1_rc_get_second_pass_params(cpi)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003300 cm->show_existing_frame = 1;
3301 cpi->rc.is_src_frame_alt_ref = 1;
Urvang Joshi4d9f15f2018-11-05 15:26:22 -08003302 cpi->existing_fb_idx_to_show =
3303 (next_frame_update_type == OVERLAY_UPDATE)
Urvang Joshi39585822018-11-07 17:47:32 -08003304 ? get_ref_frame_map_idx(cpi, ALTREF_FRAME)
3305 : get_ref_frame_map_idx(cpi, bwdref_to_show);
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003306#if USE_SYMM_MULTI_LAYER
3307 if (cpi->new_bwdref_update_rule == 0)
3308#endif
3309 cpi->is_arf_filter_off[which_arf] = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003310 }
3311 cpi->rc.is_src_frame_ext_arf = 0;
3312}
Yaowu Xuc27fc142016-08-22 16:08:15 -07003313
3314#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003315void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003316 uint8_t *src = s->y_buffer;
3317 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07003318 if (yuv_rec_file == NULL) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003319 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3320 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3321
3322 do {
3323 fwrite(src16, s->y_width, 2, yuv_rec_file);
3324 src16 += s->y_stride;
3325 } while (--h);
3326
3327 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3328 h = s->uv_height;
3329
3330 do {
3331 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3332 src16 += s->uv_stride;
3333 } while (--h);
3334
3335 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3336 h = s->uv_height;
3337
3338 do {
3339 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3340 src16 += s->uv_stride;
3341 } while (--h);
3342
3343 fflush(yuv_rec_file);
3344 return;
3345 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003346
3347 do {
3348 fwrite(src, s->y_width, 1, yuv_rec_file);
3349 src += s->y_stride;
3350 } while (--h);
3351
3352 src = s->u_buffer;
3353 h = s->uv_height;
3354
3355 do {
3356 fwrite(src, s->uv_width, 1, yuv_rec_file);
3357 src += s->uv_stride;
3358 } while (--h);
3359
3360 src = s->v_buffer;
3361 h = s->uv_height;
3362
3363 do {
3364 fwrite(src, s->uv_width, 1, yuv_rec_file);
3365 src += s->uv_stride;
3366 } while (--h);
3367
3368 fflush(yuv_rec_file);
3369}
3370#endif // OUTPUT_YUV_REC
3371
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003372#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003373static int recode_loop_test_global_motion(AV1_COMP *cpi) {
3374 int i;
3375 int recode = 0;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003376 RD_COUNTS *const rdc = &cpi->td.rd_counts;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003377 AV1_COMMON *const cm = &cpi->common;
3378 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3379 if (cm->global_motion[i].wmtype != IDENTITY &&
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003380 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003381 cpi->gmparams_cost[i]) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003382 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003383 assert(cm->global_motion[i].wmtype == IDENTITY);
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003384 cpi->gmparams_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003385 recode = 1;
Urvang Joshi02aade82017-12-18 17:18:16 -08003386 // TODO(sarahparker): The earlier condition for recoding here was:
3387 // "recode |= (rdc->global_motion_used[i] > 0);". Can we bring something
3388 // similar to that back to speed up global motion?
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003389 }
3390 }
3391 return recode;
3392}
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003393
Yaowu Xuc27fc142016-08-22 16:08:15 -07003394// Function to test for conditions that indicate we should loop
3395// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003396static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3397 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003398 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003399 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003400 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3401 int force_recode = 0;
3402
3403 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3404 (cpi->sf.recode_loop == ALLOW_RECODE) ||
3405 (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003406 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3407 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3408 (rc->projected_frame_size < low_limit && q > minq)) {
3409 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003410 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003411 // Deal with frame undershoot and whether or not we are
3412 // below the automatically set cq level.
3413 if (q > oxcf->cq_level &&
3414 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3415 force_recode = 1;
3416 }
3417 }
3418 }
3419 return force_recode;
3420}
3421
Yaowu Xuc27fc142016-08-22 16:08:15 -07003422#define DUMP_REF_FRAME_IMAGES 0
3423
3424#if DUMP_REF_FRAME_IMAGES == 1
Yaowu Xuf883b422016-08-30 14:01:10 -07003425static int dump_one_image(AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003426 const YV12_BUFFER_CONFIG *const ref_buf,
3427 char *file_name) {
3428 int h;
3429 FILE *f_ref = NULL;
3430
3431 if (ref_buf == NULL) {
3432 printf("Frame data buffer is NULL.\n");
Yaowu Xuf883b422016-08-30 14:01:10 -07003433 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003434 }
3435
3436 if ((f_ref = fopen(file_name, "wb")) == NULL) {
3437 printf("Unable to open file %s to write.\n", file_name);
Yaowu Xuf883b422016-08-30 14:01:10 -07003438 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003439 }
3440
3441 // --- Y ---
3442 for (h = 0; h < cm->height; ++h) {
3443 fwrite(&ref_buf->y_buffer[h * ref_buf->y_stride], 1, cm->width, f_ref);
3444 }
3445 // --- U ---
3446 for (h = 0; h < (cm->height >> 1); ++h) {
3447 fwrite(&ref_buf->u_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3448 f_ref);
3449 }
3450 // --- V ---
3451 for (h = 0; h < (cm->height >> 1); ++h) {
3452 fwrite(&ref_buf->v_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3453 f_ref);
3454 }
3455
3456 fclose(f_ref);
3457
Yaowu Xuf883b422016-08-30 14:01:10 -07003458 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003459}
3460
Yaowu Xuf883b422016-08-30 14:01:10 -07003461static void dump_ref_frame_images(AV1_COMP *cpi) {
3462 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003463 MV_REFERENCE_FRAME ref_frame;
3464
3465 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3466 char file_name[256] = "";
3467 snprintf(file_name, sizeof(file_name), "/tmp/enc_F%d_ref_%d.yuv",
3468 cm->current_video_frame, ref_frame);
3469 dump_one_image(cm, get_ref_frame_buffer(cpi, ref_frame), file_name);
3470 }
3471}
3472#endif // DUMP_REF_FRAME_IMAGES == 1
3473
Yaowu Xuc27fc142016-08-22 16:08:15 -07003474// This function is used to shift the virtual indices of last reference frames
3475// as follows:
3476// LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3477// when the LAST_FRAME is updated.
Yaowu Xuf883b422016-08-30 14:01:10 -07003478static INLINE void shift_last_ref_frames(AV1_COMP *cpi) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003479 // TODO(isbs): shift the scaled indices as well
Urvang Joshia130dcc2018-11-06 10:27:35 -08003480 for (int ref_frame = LAST3_FRAME; ref_frame > LAST_FRAME; --ref_frame) {
3481 const int ref_idx = ref_frame - LAST_FRAME;
3482 cpi->remapped_ref_idx[ref_idx] = cpi->remapped_ref_idx[ref_idx - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003483
Yaowu Xuc27fc142016-08-22 16:08:15 -07003484 if (!cpi->rc.is_src_frame_alt_ref) {
Urvang Joshia130dcc2018-11-06 10:27:35 -08003485 memcpy(cpi->interp_filter_selected[ref_frame],
3486 cpi->interp_filter_selected[ref_frame - 1],
3487 sizeof(cpi->interp_filter_selected[ref_frame - 1]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003488 }
3489 }
3490}
Yaowu Xuc27fc142016-08-22 16:08:15 -07003491
Wei-Ting Lin240d9b42018-07-12 11:48:02 -07003492#if USE_SYMM_MULTI_LAYER
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003493// This function is used to shift the virtual indices of bwd reference
3494// frames as follows:
3495// BWD_REF -> ALT2_REF -> EXT_REF
3496// to clear a space to store the closest bwdref
3497static INLINE void rshift_bwd_ref_frames(AV1_COMP *cpi) {
3498 // TODO(isbs): shift the scaled indices as well
Urvang Joshi03d8ebe2018-11-08 17:13:44 -08003499 static const int ordered_bwd[3] = { BWDREF_FRAME, ALTREF2_FRAME,
3500 EXTREF_FRAME };
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003501
3502 for (int i = 2; i > 0; --i) {
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003503 // [0] is allocated to the current coded frame, i.e. bwdref
Urvang Joshi03d8ebe2018-11-08 17:13:44 -08003504 memcpy(cpi->interp_filter_selected[ordered_bwd[i]],
3505 cpi->interp_filter_selected[ordered_bwd[i - 1]],
3506 sizeof(cpi->interp_filter_selected[ordered_bwd[i - 1]]));
SmilingWolfdad22822018-09-12 15:43:43 -07003507
Urvang Joshi03d8ebe2018-11-08 17:13:44 -08003508 cpi->remapped_ref_idx[ordered_bwd[i] - LAST_FRAME] =
3509 cpi->remapped_ref_idx[ordered_bwd[i - 1] - LAST_FRAME];
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003510 }
3511}
3512
3513// This function is used to shift the virtual indices of bwd reference
3514// frames as follows:
3515// BWD_REF <- ALT2_REF <- EXT_REF
3516// to update the bwd reference frame for coding the next frame.
3517static INLINE void lshift_bwd_ref_frames(AV1_COMP *cpi) {
3518 // TODO(isbs): shift the scaled indices as well
Urvang Joshi03d8ebe2018-11-08 17:13:44 -08003519 static const int ordered_bwd[3] = { BWDREF_FRAME, ALTREF2_FRAME,
3520 EXTREF_FRAME };
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003521
3522 for (int i = 0; i < 2; ++i) {
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003523 // [0] is allocated to the current coded frame, i.e. bwdref
Urvang Joshi03d8ebe2018-11-08 17:13:44 -08003524 memcpy(cpi->interp_filter_selected[ordered_bwd[i]],
3525 cpi->interp_filter_selected[ordered_bwd[i + 1]],
3526 sizeof(cpi->interp_filter_selected[ordered_bwd[i + 1]]));
SmilingWolfdad22822018-09-12 15:43:43 -07003527
Urvang Joshi03d8ebe2018-11-08 17:13:44 -08003528 cpi->remapped_ref_idx[ordered_bwd[i] - LAST_FRAME] =
3529 cpi->remapped_ref_idx[ordered_bwd[i + 1] - LAST_FRAME];
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003530 }
3531}
Wei-Ting Lin240d9b42018-07-12 11:48:02 -07003532#endif // USE_SYMM_MULTI_LAYER
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003533
Zoe Liu8dd1c982017-09-11 10:14:35 -07003534static void update_reference_frames(AV1_COMP *cpi) {
3535 AV1_COMMON *const cm = &cpi->common;
3536
Yaowu Xuc27fc142016-08-22 16:08:15 -07003537 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3538 // for the purpose to verify no mismatch between encoder and decoder.
3539 if (cm->show_frame) cpi->last_show_frame_buf_idx = cm->new_fb_idx;
3540
Sarah Parker33005522018-07-27 14:46:25 -07003541 // In the case of show_existing frame, we will not send fresh flag
3542 // to decoder. Any change in the reference frame buffer can be done by
3543 // switching the virtual indices.
3544 if (cm->show_existing_frame) {
Sarah Parker29147cf2018-10-16 20:34:51 -07003545 // If we are not indicating to the decoder that this frame is
3546 // a show_existing_frame, which occurs in error_resilient mode,
Sarah Parkera9e19052018-10-18 17:49:26 -07003547 // we still want to refresh the LAST_FRAME when the current frame
3548 // was the source of an ext_arf.
3549 cpi->refresh_last_frame =
3550 !encode_show_existing_frame(cm) && cpi->rc.is_src_frame_ext_arf;
Sarah Parker33005522018-07-27 14:46:25 -07003551 cpi->refresh_golden_frame = 0;
3552 cpi->refresh_bwd_ref_frame = 0;
3553 cpi->refresh_alt2_ref_frame = 0;
3554 cpi->refresh_alt_ref_frame = 0;
3555
3556 cpi->rc.is_bwd_ref_frame = 0;
3557 cpi->rc.is_last_bipred_frame = 0;
3558 cpi->rc.is_bipred_frame = 0;
3559 }
3560
Zoe Liu8dd1c982017-09-11 10:14:35 -07003561 BufferPool *const pool = cm->buffer_pool;
Zoe Liubcef1e62018-04-06 20:56:11 -07003562
Yaowu Xuc27fc142016-08-22 16:08:15 -07003563 // At this point the new frame has been encoded.
3564 // If any buffer copy / swapping is signaled it should be done here.
Zoe Liubcef1e62018-04-06 20:56:11 -07003565
Sarah Parkerb9041612018-05-22 19:06:47 -07003566 // Only update all of the reference buffers if a KEY_FRAME is also a
3567 // show_frame. This ensures a fwd keyframe does not update all of the buffers
3568 if ((cm->frame_type == KEY_FRAME && cm->show_frame) || frame_is_sframe(cm)) {
Zoe Liubcef1e62018-04-06 20:56:11 -07003569 for (int ref_frame = 0; ref_frame < REF_FRAMES; ++ref_frame) {
Urvang Joshidf802ee2018-11-05 16:43:26 -08003570 assign_frame_buffer(pool->frame_bufs,
3571 &cm->ref_frame_map[cpi->remapped_ref_idx[ref_frame]],
3572 cm->new_fb_idx);
Zoe Liubcef1e62018-04-06 20:56:11 -07003573 }
3574 return;
3575 }
3576
3577 if (av1_preserve_existing_gf(cpi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003578 // We have decided to preserve the previously existing golden frame as our
3579 // new ARF frame. However, in the short term in function
Yaowu Xuf883b422016-08-30 14:01:10 -07003580 // av1_bitstream.c::get_refresh_mask() we left it in the GF slot and, if
Yaowu Xuc27fc142016-08-22 16:08:15 -07003581 // we're updating the GF with the current decoded frame, we save it to the
3582 // ARF slot instead.
3583 // We now have to update the ARF with the current frame and swap gld_fb_idx
3584 // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
3585 // slot and, if we're updating the GF, the current frame becomes the new GF.
3586 int tmp;
3587
Wei-Ting Lina8c02452018-08-13 11:04:06 -07003588 // ARF in general is a better reference than overlay. We shouldkeep ARF as
3589 // reference instead of replacing it with overlay.
3590
3591 if (!cpi->preserve_arf_as_gld) {
Urvang Joshidf802ee2018-11-05 16:43:26 -08003592 assign_frame_buffer(
3593 pool->frame_bufs,
Urvang Joshi39585822018-11-07 17:47:32 -08003594 &cm->ref_frame_map[get_ref_frame_map_idx(cpi, ALTREF_FRAME)],
Urvang Joshidf802ee2018-11-05 16:43:26 -08003595 cm->new_fb_idx);
Wei-Ting Lina8c02452018-08-13 11:04:06 -07003596 }
3597
Urvang Joshi39585822018-11-07 17:47:32 -08003598 tmp = get_ref_frame_map_idx(cpi, ALTREF_FRAME);
Urvang Joshi4d9f15f2018-11-05 15:26:22 -08003599 cpi->remapped_ref_idx[ALTREF_FRAME - 1] =
Urvang Joshi39585822018-11-07 17:47:32 -08003600 get_ref_frame_map_idx(cpi, GOLDEN_FRAME);
Urvang Joshi4d9f15f2018-11-05 15:26:22 -08003601 cpi->remapped_ref_idx[GOLDEN_FRAME - 1] = tmp;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003602
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003603 // TODO(zoeliu): Do we need to copy cpi->interp_filter_selected[0] over to
3604 // cpi->interp_filter_selected[GOLDEN_FRAME]?
Sarah Parker7a9bb782018-10-11 14:52:42 -07003605 } else if (cpi->rc.is_src_frame_ext_arf && encode_show_existing_frame(cm)) {
Wei-Ting Linb72453f2018-06-26 14:05:38 -07003606#if CONFIG_DEBUG
3607 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3608 assert(gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE);
3609#endif
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003610#if USE_SYMM_MULTI_LAYER
3611 const int bwdref_to_show =
3612 (cpi->new_bwdref_update_rule == 1) ? BWDREF_FRAME : ALTREF2_FRAME;
3613#else
3614 const int bwdref_to_show = ALTREF2_FRAME;
3615#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003616 // Deal with the special case for showing existing internal ALTREF_FRAME
3617 // Refresh the LAST_FRAME with the ALTREF_FRAME and retire the LAST3_FRAME
3618 // by updating the virtual indices.
Urvang Joshi39585822018-11-07 17:47:32 -08003619 const int last3_remapped_idx = get_ref_frame_map_idx(cpi, LAST3_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003620 shift_last_ref_frames(cpi);
Zoe Liue9b15e22017-07-19 15:53:01 -07003621
Urvang Joshi4d9f15f2018-11-05 15:26:22 -08003622 cpi->remapped_ref_idx[LAST_FRAME - 1] =
Urvang Joshi39585822018-11-07 17:47:32 -08003623 get_ref_frame_map_idx(cpi, bwdref_to_show);
Zoe Liue9b15e22017-07-19 15:53:01 -07003624
3625 memcpy(cpi->interp_filter_selected[LAST_FRAME],
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003626 cpi->interp_filter_selected[bwdref_to_show],
3627 sizeof(cpi->interp_filter_selected[bwdref_to_show]));
Wei-Ting Lin240d9b42018-07-12 11:48:02 -07003628#if USE_SYMM_MULTI_LAYER
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003629 if (cpi->new_bwdref_update_rule == 1) {
3630 lshift_bwd_ref_frames(cpi);
3631 // pass outdated forward reference frame (previous LAST3) to the
3632 // spared space
Urvang Joshia130dcc2018-11-06 10:27:35 -08003633 cpi->remapped_ref_idx[EXTREF_FRAME - 1] = last3_remapped_idx;
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003634 } else {
3635#endif
Urvang Joshia130dcc2018-11-06 10:27:35 -08003636 cpi->remapped_ref_idx[bwdref_to_show - 1] = last3_remapped_idx;
Wei-Ting Lin240d9b42018-07-12 11:48:02 -07003637#if USE_SYMM_MULTI_LAYER
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003638 }
3639#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003640 } else { /* For non key/golden frames */
Zoe Liue9b15e22017-07-19 15:53:01 -07003641 // === ALTREF_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003642 if (cpi->refresh_alt_ref_frame) {
Urvang Joshi39585822018-11-07 17:47:32 -08003643 int arf_idx = get_ref_frame_map_idx(cpi, ALTREF_FRAME);
Urvang Joshidf802ee2018-11-05 16:43:26 -08003644 assign_frame_buffer(pool->frame_bufs, &cm->ref_frame_map[arf_idx],
3645 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003646
Wei-Ting Lin2e8d0452018-06-27 09:32:39 -07003647 memcpy(cpi->interp_filter_selected[ALTREF_FRAME],
Yaowu Xuc27fc142016-08-22 16:08:15 -07003648 cpi->interp_filter_selected[0],
3649 sizeof(cpi->interp_filter_selected[0]));
3650 }
3651
Zoe Liue9b15e22017-07-19 15:53:01 -07003652 // === GOLDEN_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003653 if (cpi->refresh_golden_frame) {
Urvang Joshidf802ee2018-11-05 16:43:26 -08003654 assign_frame_buffer(
3655 pool->frame_bufs,
Urvang Joshi39585822018-11-07 17:47:32 -08003656 &cm->ref_frame_map[get_ref_frame_map_idx(cpi, GOLDEN_FRAME)],
Urvang Joshidf802ee2018-11-05 16:43:26 -08003657 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003658
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003659 memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
3660 cpi->interp_filter_selected[0],
3661 sizeof(cpi->interp_filter_selected[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003662 }
3663
Zoe Liue9b15e22017-07-19 15:53:01 -07003664 // === BWDREF_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003665 if (cpi->refresh_bwd_ref_frame) {
Wei-Ting Lin240d9b42018-07-12 11:48:02 -07003666#if USE_SYMM_MULTI_LAYER
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003667 if (cpi->new_bwdref_update_rule) {
3668 // We shift the backward reference frame as follows:
3669 // BWDREF -> ALTREF2 -> EXTREF
3670 // and assign the newly coded frame to BWDREF so that it always
3671 // keeps the nearest future frame
Urvang Joshi39585822018-11-07 17:47:32 -08003672 int tmp = get_ref_frame_map_idx(cpi, EXTREF_FRAME);
Urvang Joshidf802ee2018-11-05 16:43:26 -08003673 assign_frame_buffer(pool->frame_bufs, &cm->ref_frame_map[tmp],
3674 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003675
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003676 rshift_bwd_ref_frames(cpi);
Urvang Joshi4d9f15f2018-11-05 15:26:22 -08003677 cpi->remapped_ref_idx[BWDREF_FRAME - 1] = tmp;
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003678 } else {
Wei-Ting Lin240d9b42018-07-12 11:48:02 -07003679#endif // USE_SYMM_MULTI_LAYER
Urvang Joshidf802ee2018-11-05 16:43:26 -08003680 assign_frame_buffer(
3681 pool->frame_bufs,
Urvang Joshi39585822018-11-07 17:47:32 -08003682 &cm->ref_frame_map[get_ref_frame_map_idx(cpi, BWDREF_FRAME)],
Urvang Joshidf802ee2018-11-05 16:43:26 -08003683 cm->new_fb_idx);
Wei-Ting Lin240d9b42018-07-12 11:48:02 -07003684#if USE_SYMM_MULTI_LAYER
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003685 }
3686#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003687 memcpy(cpi->interp_filter_selected[BWDREF_FRAME],
3688 cpi->interp_filter_selected[0],
3689 sizeof(cpi->interp_filter_selected[0]));
3690 }
Zoe Liue9b15e22017-07-19 15:53:01 -07003691
Zoe Liue9b15e22017-07-19 15:53:01 -07003692 // === ALTREF2_FRAME ===
3693 if (cpi->refresh_alt2_ref_frame) {
Urvang Joshidf802ee2018-11-05 16:43:26 -08003694 assign_frame_buffer(
3695 pool->frame_bufs,
Urvang Joshi39585822018-11-07 17:47:32 -08003696 &cm->ref_frame_map[get_ref_frame_map_idx(cpi, ALTREF2_FRAME)],
Urvang Joshidf802ee2018-11-05 16:43:26 -08003697 cm->new_fb_idx);
Zoe Liue9b15e22017-07-19 15:53:01 -07003698
3699 memcpy(cpi->interp_filter_selected[ALTREF2_FRAME],
3700 cpi->interp_filter_selected[0],
3701 sizeof(cpi->interp_filter_selected[0]));
3702 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003703 }
3704
3705 if (cpi->refresh_last_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003706 // NOTE(zoeliu): We have two layers of mapping (1) from the per-frame
3707 // reference to the reference frame buffer virtual index; and then (2) from
3708 // the virtual index to the reference frame buffer physical index:
3709 //
Urvang Joshi4d9f15f2018-11-05 15:26:22 -08003710 // LAST_FRAME, ..., EXTREF_FRAME
3711 // | |
3712 // v v
3713 // remapped_ref_idx[LAST_FRAME - 1], ..., remapped_ref_idx[EXTREF_FRAME - 1]
3714 // | |
3715 // v v
3716 // ref_frame_map[], ..., ref_frame_map[]
Yaowu Xuc27fc142016-08-22 16:08:15 -07003717 //
3718 // When refresh_last_frame is set, it is intended to retire LAST3_FRAME,
3719 // have the other 2 LAST reference frames shifted as follows:
3720 // LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3721 // , and then have LAST_FRAME refreshed by the newly coded frame.
3722 //
3723 // To fulfill it, the decoder will be notified to execute following 2 steps:
3724 //
3725 // (a) To change ref_frame_map[] and have the virtual index of LAST3_FRAME
3726 // to point to the newly coded frame, i.e.
3727 // ref_frame_map[lst_fb_idexes[2]] => new_fb_idx;
3728 //
3729 // (b) To change the 1st layer mapping to have LAST_FRAME mapped to the
3730 // original virtual index of LAST3_FRAME and have the other mappings
3731 // shifted as follows:
Urvang Joshi4d9f15f2018-11-05 15:26:22 -08003732 // LAST_FRAME, LAST2_FRAME, LAST3_FRAME
3733 // | | |
3734 // v v v
3735 // remapped_ref_idx[2], remapped_ref_idx[0], remapped_ref_idx[1]
Urvang Joshidf802ee2018-11-05 16:43:26 -08003736 assign_frame_buffer(
3737 pool->frame_bufs,
Urvang Joshi39585822018-11-07 17:47:32 -08003738 &cm->ref_frame_map[get_ref_frame_map_idx(cpi, LAST3_FRAME)],
Urvang Joshidf802ee2018-11-05 16:43:26 -08003739 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003740
Urvang Joshi39585822018-11-07 17:47:32 -08003741 int last3_remapped_idx = get_ref_frame_map_idx(cpi, LAST3_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003742
Zoe Liubcef1e62018-04-06 20:56:11 -07003743 shift_last_ref_frames(cpi);
Urvang Joshia130dcc2018-11-06 10:27:35 -08003744 cpi->remapped_ref_idx[LAST_FRAME - 1] = last3_remapped_idx;
Zoe Liubcef1e62018-04-06 20:56:11 -07003745
Sarah Parker5336b9c2018-10-18 11:34:20 -07003746 assert(!encode_show_existing_frame(cm));
Zoe Liubcef1e62018-04-06 20:56:11 -07003747 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3748 cpi->interp_filter_selected[0],
3749 sizeof(cpi->interp_filter_selected[0]));
3750
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003751 // If the new structure is used, we will always have overlay frames coupled
3752 // with bwdref frames. Therefore, we won't have to perform this update
3753 // in advance (we do this update when the overlay frame shows up).
3754#if USE_SYMM_MULTI_LAYER
3755 if (cpi->new_bwdref_update_rule == 0 && cpi->rc.is_last_bipred_frame) {
3756#else
Zoe Liubcef1e62018-04-06 20:56:11 -07003757 if (cpi->rc.is_last_bipred_frame) {
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003758#endif
Zoe Liubcef1e62018-04-06 20:56:11 -07003759 // Refresh the LAST_FRAME with the BWDREF_FRAME and retire the
3760 // LAST3_FRAME by updating the virtual indices.
3761 //
3762 // NOTE: The source frame for BWDREF does not have a holding position as
3763 // the OVERLAY frame for ALTREF's. Hence, to resolve the reference
3764 // virtual index reshuffling for BWDREF, the encoder always
3765 // specifies a LAST_BIPRED right before BWDREF and completes the
3766 // reshuffling job accordingly.
Urvang Joshi39585822018-11-07 17:47:32 -08003767 last3_remapped_idx = get_ref_frame_map_idx(cpi, LAST3_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003768
3769 shift_last_ref_frames(cpi);
Urvang Joshia130dcc2018-11-06 10:27:35 -08003770 cpi->remapped_ref_idx[LAST_FRAME - 1] =
Urvang Joshi39585822018-11-07 17:47:32 -08003771 get_ref_frame_map_idx(cpi, BWDREF_FRAME);
Urvang Joshia130dcc2018-11-06 10:27:35 -08003772 cpi->remapped_ref_idx[BWDREF_FRAME - 1] = last3_remapped_idx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003773
Zoe Liuf0e46692016-10-12 12:31:43 -07003774 memcpy(cpi->interp_filter_selected[LAST_FRAME],
Zoe Liubcef1e62018-04-06 20:56:11 -07003775 cpi->interp_filter_selected[BWDREF_FRAME],
3776 sizeof(cpi->interp_filter_selected[BWDREF_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003777 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003778 }
3779
3780#if DUMP_REF_FRAME_IMAGES == 1
3781 // Dump out all reference frame images.
3782 dump_ref_frame_images(cpi);
3783#endif // DUMP_REF_FRAME_IMAGES
3784}
3785
Yaowu Xuf883b422016-08-30 14:01:10 -07003786static INLINE void alloc_frame_mvs(AV1_COMMON *const cm, int buffer_idx) {
Debargha Mukherjee887069f2017-06-16 18:54:36 -07003787 assert(buffer_idx != INVALID_IDX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003788 RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
Rupert Swarbrick1f990a62017-07-11 11:09:33 +01003789 ensure_mv_buffer(new_fb_ptr, cm);
3790 new_fb_ptr->width = cm->width;
3791 new_fb_ptr->height = cm->height;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003792}
3793
Cheng Chen46f30c72017-09-07 11:13:33 -07003794static void scale_references(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003795 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003796 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003797 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07003798 const AOM_REFFRAME ref_mask[INTER_REFS_PER_FRAME] = {
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003799 AOM_LAST_FLAG, AOM_LAST2_FLAG, AOM_LAST3_FLAG, AOM_GOLD_FLAG,
3800 AOM_BWD_FLAG, AOM_ALT2_FLAG, AOM_ALT_FLAG
Yaowu Xuc27fc142016-08-22 16:08:15 -07003801 };
3802
3803 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003804 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Yaowu Xuc27fc142016-08-22 16:08:15 -07003805 if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
3806 BufferPool *const pool = cm->buffer_pool;
3807 const YV12_BUFFER_CONFIG *const ref =
3808 get_ref_frame_buffer(cpi, ref_frame);
3809
3810 if (ref == NULL) {
3811 cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3812 continue;
3813 }
3814
Yaowu Xuc27fc142016-08-22 16:08:15 -07003815 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3816 RefCntBuffer *new_fb_ptr = NULL;
3817 int force_scaling = 0;
3818 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3819 if (new_fb == INVALID_IDX) {
3820 new_fb = get_free_fb(cm);
Wan-Teh Chang4a8c0042018-10-05 09:41:52 -07003821 if (new_fb == INVALID_IDX)
3822 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3823 "Unable to find free frame buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003824 force_scaling = 1;
3825 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003826 new_fb_ptr = &pool->frame_bufs[new_fb];
3827 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3828 new_fb_ptr->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003829 if (aom_realloc_frame_buffer(
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003830 &new_fb_ptr->buf, cm->width, cm->height,
3831 cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
3832 cm->seq_params.use_highbitdepth, AOM_BORDER_IN_PIXELS,
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003833 cm->byte_alignment, NULL, NULL, NULL)) {
3834 if (force_scaling) {
3835 // Release the reference acquired in the get_free_fb() call above.
3836 --new_fb_ptr->ref_count;
3837 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003838 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003839 "Failed to allocate frame buffer");
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003840 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003841 av1_resize_and_extend_frame(
3842 ref, &new_fb_ptr->buf, (int)cm->seq_params.bit_depth, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003843 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3844 alloc_frame_mvs(cm, new_fb);
3845 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003846 } else {
3847 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3848 RefCntBuffer *const buf = &pool->frame_bufs[buf_idx];
3849 buf->buf.y_crop_width = ref->y_crop_width;
3850 buf->buf.y_crop_height = ref->y_crop_height;
3851 cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
3852 ++buf->ref_count;
3853 }
3854 } else {
3855 if (cpi->oxcf.pass != 0) cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3856 }
3857 }
3858}
3859
Yaowu Xuf883b422016-08-30 14:01:10 -07003860static void release_scaled_references(AV1_COMP *cpi) {
3861 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003862 int i;
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003863 // TODO(isbs): only refresh the necessary frames, rather than all of them
Wan-Teh Changbb6a3d22018-10-05 11:22:48 -07003864 for (i = 0; i < INTER_REFS_PER_FRAME; ++i) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003865 const int idx = cpi->scaled_ref_idx[i];
Wan-Teh Changd05e0332018-10-03 12:00:43 -07003866 if (idx != INVALID_IDX) {
3867 RefCntBuffer *const buf = &cm->buffer_pool->frame_bufs[idx];
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003868 --buf->ref_count;
3869 cpi->scaled_ref_idx[i] = INVALID_IDX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003870 }
3871 }
3872}
3873
Yaowu Xuf883b422016-08-30 14:01:10 -07003874static void set_mv_search_params(AV1_COMP *cpi) {
3875 const AV1_COMMON *const cm = &cpi->common;
3876 const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003877
3878 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003879 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003880
3881 if (cpi->sf.mv.auto_mv_step_size) {
3882 if (frame_is_intra_only(cm)) {
3883 // Initialize max_mv_magnitude for use in the first INTER frame
3884 // after a key/intra-only frame.
3885 cpi->max_mv_magnitude = max_mv_def;
3886 } else {
3887 if (cm->show_frame) {
3888 // Allow mv_steps to correspond to twice the max mv magnitude found
3889 // in the previous frame, capped by the default max_mv_magnitude based
3890 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003891 cpi->mv_step_param = av1_init_search_range(
3892 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003893 }
3894 cpi->max_mv_magnitude = 0;
3895 }
3896 }
3897}
3898
Yaowu Xuf883b422016-08-30 14:01:10 -07003899static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003900 int i;
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003901 AV1_COMMON *cm = &cpi->common;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003902 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003903 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003904 }
3905 cpi->global_motion_search_done = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003906 av1_set_speed_features_framesize_independent(cpi);
3907 av1_set_rd_speed_thresholds(cpi);
3908 av1_set_rd_speed_thresholds_sub8x8(cpi);
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003909 cm->interp_filter = SWITCHABLE;
3910 cm->switchable_motion_mode = 1;
3911
3912 if (frame_is_intra_only(cm)) {
3913 if (cm->seq_params.force_screen_content_tools == 2) {
3914 cm->allow_screen_content_tools =
3915 cpi->oxcf.content == AOM_CONTENT_SCREEN ||
3916 is_screen_content(cpi->source->y_buffer,
3917 cpi->source->flags & YV12_FLAG_HIGHBITDEPTH,
3918 cm->seq_params.bit_depth, cpi->source->y_stride,
3919 cpi->source->y_width, cpi->source->y_height);
3920 } else {
3921 cm->allow_screen_content_tools =
3922 cm->seq_params.force_screen_content_tools;
3923 }
3924 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003925}
3926
Yaowu Xuf883b422016-08-30 14:01:10 -07003927static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003928 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003929 AV1_COMMON *const cm = &cpi->common;
3930 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003931
3932 // Setup variables that depend on the dimensions of the frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003933 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003934
Sebastien Alaiwan41cae6a2018-01-12 12:22:29 +01003935 // Decide q and q bounds.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003936 *q = av1_rc_pick_q_and_bounds(cpi, cm->width, cm->height, bottom_index,
3937 top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003938
James Zern01a9d702017-08-25 19:09:33 +00003939 if (!frame_is_intra_only(cm)) {
RogerZhou3b635242017-09-19 10:06:46 -07003940 set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH,
RogerZhou10a03802017-10-26 11:49:48 -07003941 cpi->common.cur_frame_force_integer_mv);
James Zern01a9d702017-08-25 19:09:33 +00003942 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003943
3944 // Configure experimental use of segmentation for enhanced coding of
3945 // static regions if indicated.
3946 // Only allowed in the second pass of a two pass encode, as it requires
3947 // lagged coding, and if the relevant speed feature flag is set.
3948 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
3949 configure_static_seg_features(cpi);
3950}
3951
Yaowu Xuf883b422016-08-30 14:01:10 -07003952static void init_motion_estimation(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003953 int y_stride = cpi->scaled_source.y_stride;
3954
3955 if (cpi->sf.mv.search_method == NSTEP) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003956 av1_init3smotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003957 } else if (cpi->sf.mv.search_method == DIAMOND) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003958 av1_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003959 }
3960}
3961
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003962#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003963static void set_restoration_unit_size(int width, int height, int sx, int sy,
3964 RestorationInfo *rst) {
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003965 (void)width;
3966 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003967 (void)sx;
3968 (void)sy;
3969#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
3970 int s = AOMMIN(sx, sy);
3971#else
3972 int s = 0;
3973#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
3974
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003975 if (width * height > 352 * 288)
Urvang Joshi813186b2018-03-08 15:38:46 -08003976 rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003977 else
Urvang Joshi813186b2018-03-08 15:38:46 -08003978 rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003979 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
3980 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003981}
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003982
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303983static void init_ref_frame_bufs(AV1_COMP *cpi) {
3984 AV1_COMMON *const cm = &cpi->common;
Cheng Chen46f30c72017-09-07 11:13:33 -07003985 int i;
3986 BufferPool *const pool = cm->buffer_pool;
3987 cm->new_fb_idx = INVALID_IDX;
3988 for (i = 0; i < REF_FRAMES; ++i) {
3989 cm->ref_frame_map[i] = INVALID_IDX;
Wan-Teh Changd05e0332018-10-03 12:00:43 -07003990 }
3991 for (i = 0; i < FRAME_BUFFERS; ++i) {
Cheng Chen46f30c72017-09-07 11:13:33 -07003992 pool->frame_bufs[i].ref_count = 0;
3993 }
RogerZhou86902d02018-02-28 15:29:16 -08003994 if (cm->seq_params.force_screen_content_tools) {
Hui Su2d5fd742018-02-21 18:10:37 -08003995 for (i = 0; i < FRAME_BUFFERS; ++i) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303996 av1_hash_table_init(&pool->frame_bufs[i].hash_table, &cpi->td.mb);
Hui Su2d5fd742018-02-21 18:10:37 -08003997 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003998 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003999}
4000
Yaowu Xud3e7c682017-12-21 14:08:25 -08004001static void check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
Cheng Chen46f30c72017-09-07 11:13:33 -07004002 int subsampling_x, int subsampling_y) {
4003 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004004 SequenceHeader *const seq_params = &cm->seq_params;
Cheng Chen46f30c72017-09-07 11:13:33 -07004005
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004006 if (!cpi->initial_width || seq_params->use_highbitdepth != use_highbitdepth ||
4007 seq_params->subsampling_x != subsampling_x ||
4008 seq_params->subsampling_y != subsampling_y) {
4009 seq_params->subsampling_x = subsampling_x;
4010 seq_params->subsampling_y = subsampling_y;
4011 seq_params->use_highbitdepth = use_highbitdepth;
Cheng Chen46f30c72017-09-07 11:13:33 -07004012
4013 alloc_raw_frame_buffers(cpi);
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05304014 init_ref_frame_bufs(cpi);
Cheng Chen46f30c72017-09-07 11:13:33 -07004015 alloc_util_frame_buffers(cpi);
4016
4017 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
4018
4019 cpi->initial_width = cm->width;
4020 cpi->initial_height = cm->height;
4021 cpi->initial_mbs = cm->MBs;
4022 }
4023}
4024
4025// Returns 1 if the assigned width or height was <= 0.
4026static int set_size_literal(AV1_COMP *cpi, int width, int height) {
4027 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004028 const int num_planes = av1_num_planes(cm);
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004029 check_initial_width(cpi, cm->seq_params.use_highbitdepth,
4030 cm->seq_params.subsampling_x,
4031 cm->seq_params.subsampling_y);
Cheng Chen46f30c72017-09-07 11:13:33 -07004032
4033 if (width <= 0 || height <= 0) return 1;
4034
4035 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07004036 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004037
4038 if (cpi->initial_width && cpi->initial_height &&
4039 (cm->width > cpi->initial_width || cm->height > cpi->initial_height)) {
4040 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004041 av1_free_pc_tree(&cpi->td, num_planes);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004042 alloc_compressor_data(cpi);
4043 realloc_segmentation_maps(cpi);
4044 cpi->initial_width = cpi->initial_height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07004045 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004046 update_frame_size(cpi);
4047
4048 return 0;
4049}
4050
Fergus Simpsonbc189932017-05-16 17:02:39 -07004051static void set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07004052 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004053 const SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004054 const int num_planes = av1_num_planes(cm);
Fergus Simpsonbc189932017-05-16 17:02:39 -07004055 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004056 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004057
Fergus Simpsonbc189932017-05-16 17:02:39 -07004058 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07004059 // There has been a change in the encoded frame size
Cheng Chen46f30c72017-09-07 11:13:33 -07004060 set_size_literal(cpi, width, height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004061 set_mv_search_params(cpi);
Urvang Joshic8b52d52018-03-23 13:16:51 -07004062 // Recalculate 'all_lossless' in case super-resolution was (un)selected.
Cheng Chen09c83a52018-06-05 12:27:36 -07004063 cm->all_lossless = cm->coded_lossless && !av1_superres_scaled(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004064 }
4065
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004066 if (cpi->oxcf.pass == 2) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004067 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004068 }
4069
4070 alloc_frame_mvs(cm, cm->new_fb_idx);
4071
Cherma Rajan A71d20db2018-04-27 11:15:32 +05304072 // Allocate above context buffers
Cherma Rajan Af1479082018-05-09 14:26:34 +05304073 if (cm->num_allocated_above_context_planes < av1_num_planes(cm) ||
4074 cm->num_allocated_above_context_mi_col < cm->mi_cols ||
Cherma Rajan A71d20db2018-04-27 11:15:32 +05304075 cm->num_allocated_above_contexts < cm->tile_rows) {
4076 av1_free_above_context_buffers(cm, cm->num_allocated_above_contexts);
4077 if (av1_alloc_above_context_buffers(cm, cm->tile_rows))
4078 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
4079 "Failed to allocate context buffers");
4080 }
4081
Yaowu Xuc27fc142016-08-22 16:08:15 -07004082 // Reset the frame pointers to the current frame size.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004083 if (aom_realloc_frame_buffer(
4084 get_frame_new_buffer(cm), cm->width, cm->height,
4085 seq_params->subsampling_x, seq_params->subsampling_y,
4086 seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS,
4087 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07004088 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004089 "Failed to allocate frame buffer");
4090
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004091 const int frame_width = cm->superres_upscaled_width;
4092 const int frame_height = cm->superres_upscaled_height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004093 set_restoration_unit_size(frame_width, frame_height,
4094 seq_params->subsampling_x,
4095 seq_params->subsampling_y, cm->rst_info);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004096 for (int i = 0; i < num_planes; ++i)
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01004097 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004098
4099 av1_alloc_restoration_buffers(cm);
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07004100 alloc_util_frame_buffers(cpi); // TODO(afergs): Remove? Gets called anyways.
Yaowu Xuc27fc142016-08-22 16:08:15 -07004101 init_motion_estimation(cpi);
4102
4103 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
4104 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - LAST_FRAME];
4105 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
4106
4107 ref_buf->idx = buf_idx;
4108
4109 if (buf_idx != INVALID_IDX) {
4110 YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
4111 ref_buf->buf = buf;
Debargha Mukherjeee242a812018-03-07 21:43:09 -08004112 av1_setup_scale_factors_for_frame(&ref_buf->sf, buf->y_crop_width,
4113 buf->y_crop_height, cm->width,
4114 cm->height);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004115 if (av1_is_scaled(&ref_buf->sf))
4116 aom_extend_frame_borders(buf, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004117 } else {
4118 ref_buf->buf = NULL;
4119 }
4120 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07004121
Hui Su5ebd8702018-01-08 18:09:20 -08004122 av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08004123 cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004124
4125 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
4126}
4127
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004128static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
4129 // Choose an arbitrary random number
4130 static unsigned int seed = 56789;
4131 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07004132 if (oxcf->pass == 1) return SCALE_NUMERATOR;
4133 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004134
Debargha Mukherjee2b7c2b32018-04-10 07:35:28 -07004135 if (cpi->common.seq_params.reduced_still_picture_hdr) return SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004136 switch (oxcf->resize_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004137 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004138 case RESIZE_FIXED:
4139 if (cpi->common.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004140 new_denom = oxcf->resize_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004141 else
Urvang Joshide71d142017-10-05 12:12:15 -07004142 new_denom = oxcf->resize_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004143 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004144 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004145 default: assert(0);
4146 }
Urvang Joshide71d142017-10-05 12:12:15 -07004147 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004148}
4149
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004150static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
4151 // Choose an arbitrary random number
4152 static unsigned int seed = 34567;
4153 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07004154 if (oxcf->pass == 1) return SCALE_NUMERATOR;
4155 uint8_t new_denom = SCALE_NUMERATOR;
Urvang Joshi2c92b072018-03-19 17:23:31 -07004156
4157 // Make sure that superres mode of the frame is consistent with the
4158 // sequence-level flag.
4159 assert(IMPLIES(oxcf->superres_mode != SUPERRES_NONE,
4160 cpi->common.seq_params.enable_superres));
4161 assert(IMPLIES(!cpi->common.seq_params.enable_superres,
4162 oxcf->superres_mode == SUPERRES_NONE));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004163
4164 switch (oxcf->superres_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004165 case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004166 case SUPERRES_FIXED:
4167 if (cpi->common.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004168 new_denom = oxcf->superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004169 else
Urvang Joshide71d142017-10-05 12:12:15 -07004170 new_denom = oxcf->superres_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004171 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004172 case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004173 case SUPERRES_QTHRESH: {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07004174 // Do not use superres when screen content tools are used.
4175 if (cpi->common.allow_screen_content_tools) break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004176 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4177 const RATE_FACTOR_LEVEL rf_level = gf_group->rf_level[gf_group->index];
4178 const double rate_factor_delta = rate_factor_deltas[rf_level];
Urvang Joshi2c92b072018-03-19 17:23:31 -07004179 const int qthresh = (rate_factor_delta <= 1.0)
4180 ? oxcf->superres_qthresh
4181 : oxcf->superres_kf_qthresh;
Debargha Mukherjee2b2c5fd2018-11-14 13:21:24 -08004182 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
4183 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi2c92b072018-03-19 17:23:31 -07004184 int bottom_index, top_index;
4185 const int q = av1_rc_pick_q_and_bounds(
4186 cpi, cpi->oxcf.width, cpi->oxcf.height, &bottom_index, &top_index);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004187 if (q < qthresh) {
Urvang Joshide71d142017-10-05 12:12:15 -07004188 new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004189 } else {
Urvang Joshi28983f72017-10-25 14:41:06 -07004190 const uint8_t min_denom = SCALE_NUMERATOR + 1;
Debargha Mukherjee2b2c5fd2018-11-14 13:21:24 -08004191 const uint8_t denom_step = (MAXQ - qthresh + 4) >> 3;
Imdad Sardharwallaf6154fd2018-03-02 16:53:41 +00004192
4193 if (q == qthresh) {
4194 new_denom = min_denom;
4195 } else if (denom_step == 0) {
4196 new_denom = SCALE_NUMERATOR << 1;
4197 } else {
4198 const uint8_t additional_denom = (q - qthresh) / denom_step;
4199 new_denom =
4200 AOMMIN(min_denom + additional_denom, SCALE_NUMERATOR << 1);
4201 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004202 }
4203 break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004204 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004205 default: assert(0);
4206 }
Urvang Joshide71d142017-10-05 12:12:15 -07004207 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004208}
4209
Urvang Joshide71d142017-10-05 12:12:15 -07004210static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
4211 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
4212}
4213
4214static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
Urvang Joshi94ad3702017-12-06 11:38:08 -08004215 // Only need to check the width, as scaling is horizontal only.
4216 (void)oheight;
4217 return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom);
Urvang Joshide71d142017-10-05 12:12:15 -07004218}
4219
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004220static int validate_size_scales(RESIZE_MODE resize_mode,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004221 SUPERRES_MODE superres_mode, int owidth,
4222 int oheight, size_params_type *rsz) {
Urvang Joshide71d142017-10-05 12:12:15 -07004223 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004224 return 1;
Urvang Joshide71d142017-10-05 12:12:15 -07004225 }
4226
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004227 // Calculate current resize scale.
Urvang Joshide71d142017-10-05 12:12:15 -07004228 int resize_denom =
4229 AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
4230 DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
4231
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004232 if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004233 // Alter superres scale as needed to enforce conformity.
4234 rsz->superres_denom =
4235 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
4236 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4237 if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004238 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004239 } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004240 // Alter resize scale as needed to enforce conformity.
4241 resize_denom =
4242 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004243 rsz->resize_width = owidth;
4244 rsz->resize_height = oheight;
4245 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004246 resize_denom);
4247 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4248 if (resize_denom > SCALE_NUMERATOR) {
4249 --resize_denom;
4250 rsz->resize_width = owidth;
4251 rsz->resize_height = oheight;
4252 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
4253 resize_denom);
4254 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004255 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004256 } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004257 // Alter both resize and superres scales as needed to enforce conformity.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004258 do {
Urvang Joshide71d142017-10-05 12:12:15 -07004259 if (resize_denom > rsz->superres_denom)
4260 --resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004261 else
Urvang Joshide71d142017-10-05 12:12:15 -07004262 --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004263 rsz->resize_width = owidth;
4264 rsz->resize_height = oheight;
4265 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004266 resize_denom);
4267 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
4268 (resize_denom > SCALE_NUMERATOR ||
4269 rsz->superres_denom > SCALE_NUMERATOR));
Urvang Joshif1fa6862018-01-08 16:39:33 -08004270 } else { // We are allowed to alter neither resize scale nor superres
4271 // scale.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004272 return 0;
4273 }
Urvang Joshide71d142017-10-05 12:12:15 -07004274 return dimensions_are_ok(owidth, oheight, rsz);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004275}
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004276
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004277// Calculates resize and superres params for next frame
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004278size_params_type av1_calculate_next_size_params(AV1_COMP *cpi) {
4279 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -08004280 size_params_type rsz = { oxcf->width, oxcf->height, SCALE_NUMERATOR };
Urvang Joshide71d142017-10-05 12:12:15 -07004281 int resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004282 if (oxcf->pass == 1) return rsz;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004283 if (cpi->resize_pending_width && cpi->resize_pending_height) {
4284 rsz.resize_width = cpi->resize_pending_width;
4285 rsz.resize_height = cpi->resize_pending_height;
4286 cpi->resize_pending_width = cpi->resize_pending_height = 0;
4287 } else {
Urvang Joshide71d142017-10-05 12:12:15 -07004288 resize_denom = calculate_next_resize_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004289 rsz.resize_width = cpi->oxcf.width;
4290 rsz.resize_height = cpi->oxcf.height;
4291 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004292 resize_denom);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004293 }
Urvang Joshide71d142017-10-05 12:12:15 -07004294 rsz.superres_denom = calculate_next_superres_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004295 if (!validate_size_scales(oxcf->resize_mode, oxcf->superres_mode, oxcf->width,
4296 oxcf->height, &rsz))
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004297 assert(0 && "Invalid scale parameters");
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004298 return rsz;
4299}
4300
4301static void setup_frame_size_from_params(AV1_COMP *cpi, size_params_type *rsz) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004302 int encode_width = rsz->resize_width;
4303 int encode_height = rsz->resize_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004304
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004305 AV1_COMMON *cm = &cpi->common;
4306 cm->superres_upscaled_width = encode_width;
4307 cm->superres_upscaled_height = encode_height;
Urvang Joshide71d142017-10-05 12:12:15 -07004308 cm->superres_scale_denominator = rsz->superres_denom;
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004309 av1_calculate_scaled_superres_size(&encode_width, &encode_height,
4310 rsz->superres_denom);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004311 set_frame_size(cpi, encode_width, encode_height);
4312}
4313
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004314static void setup_frame_size(AV1_COMP *cpi) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004315 size_params_type rsz = av1_calculate_next_size_params(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004316 setup_frame_size_from_params(cpi, &rsz);
4317}
4318
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004319static void superres_post_encode(AV1_COMP *cpi) {
4320 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004321 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004322
Cheng Chen09c83a52018-06-05 12:27:36 -07004323 if (!av1_superres_scaled(cm)) return;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004324
Urvang Joshid6b5d512018-03-20 13:34:38 -07004325 assert(cpi->oxcf.enable_superres);
4326 assert(!is_lossless_requested(&cpi->oxcf));
Urvang Joshic8b52d52018-03-23 13:16:51 -07004327 assert(!cm->all_lossless);
Urvang Joshid6b5d512018-03-20 13:34:38 -07004328
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004329 av1_superres_upscale(cm, NULL);
4330
4331 // If regular resizing is occurring the source will need to be downscaled to
4332 // match the upscaled superres resolution. Otherwise the original source is
4333 // used.
Cheng Chen09c83a52018-06-05 12:27:36 -07004334 if (!av1_resize_scaled(cm)) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004335 cpi->source = cpi->unscaled_source;
4336 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
4337 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07004338 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
4339 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Urvang Joshif1fa6862018-01-08 16:39:33 -08004340 // Do downscale. cm->(width|height) has been updated by
4341 // av1_superres_upscale
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004342 if (aom_realloc_frame_buffer(
4343 &cpi->scaled_source, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004344 cm->superres_upscaled_height, cm->seq_params.subsampling_x,
4345 cm->seq_params.subsampling_y, cm->seq_params.use_highbitdepth,
4346 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004347 aom_internal_error(
4348 &cm->error, AOM_CODEC_MEM_ERROR,
4349 "Failed to reallocate scaled source buffer for superres");
4350 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4351 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004352 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004353 (int)cm->seq_params.bit_depth, num_planes);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004354 cpi->source = &cpi->scaled_source;
4355 }
4356}
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004357
4358static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004359 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004360 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
Urvang Joshi14072aa2018-03-21 17:43:36 -07004361
Urvang Joshic8b52d52018-03-23 13:16:51 -07004362 assert(IMPLIES(is_lossless_requested(&cpi->oxcf),
4363 cm->coded_lossless && cm->all_lossless));
4364
4365 const int no_loopfilter = cm->coded_lossless || cm->large_scale_tile;
4366 const int no_cdef =
Debargha Mukherjee98a311c2018-03-25 16:33:11 -07004367 !cm->seq_params.enable_cdef || cm->coded_lossless || cm->large_scale_tile;
4368 const int no_restoration = !cm->seq_params.enable_restoration ||
4369 cm->all_lossless || cm->large_scale_tile;
Urvang Joshi14072aa2018-03-21 17:43:36 -07004370
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004371 struct loopfilter *lf = &cm->lf;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07004372
4373 if (no_loopfilter) {
Cheng Chen179479f2017-08-04 10:56:39 -07004374 lf->filter_level[0] = 0;
4375 lf->filter_level[1] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004376 } else {
4377 struct aom_usec_timer timer;
4378
4379 aom_clear_system_state();
4380
4381 aom_usec_timer_start(&timer);
4382
4383 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
4384
4385 aom_usec_timer_mark(&timer);
4386 cpi->time_pick_lpf += aom_usec_timer_elapsed(&timer);
4387 }
4388
Debargha Mukherjee2382b142018-02-26 14:31:32 -08004389 if (lf->filter_level[0] || lf->filter_level[1]) {
Deepa K G964e72e2018-05-16 16:56:01 +05304390 if (cpi->num_workers > 1)
4391 av1_loop_filter_frame_mt(cm->frame_to_show, cm, xd, 0, num_planes, 0,
Cheng Chene3600cd2018-09-21 18:45:42 -07004392#if LOOP_FILTER_BITMASK
4393 0,
4394#endif
Deepa K G964e72e2018-05-16 16:56:01 +05304395 cpi->workers, cpi->num_workers,
4396 &cpi->lf_row_sync);
4397 else
Cheng Chen84b09932018-08-12 17:35:13 -07004398 av1_loop_filter_frame(cm->frame_to_show, cm, xd,
4399#if LOOP_FILTER_BITMASK
4400 0,
Cheng Chen8ab1f442018-04-27 18:01:52 -07004401#endif
Cheng Chen84b09932018-08-12 17:35:13 -07004402 0, num_planes, 0);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004403 }
Debargha Mukherjeee168a782017-08-31 12:30:10 -07004404
Yaowu Xu35ee2342017-11-08 11:50:46 -08004405 if (!no_restoration)
4406 av1_loop_restoration_save_boundary_lines(cm->frame_to_show, cm, 0);
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02004407
Yaowu Xu35ee2342017-11-08 11:50:46 -08004408 if (no_cdef) {
David Turnerebf96f42018-11-14 16:57:57 +00004409 cm->cdef_info.cdef_bits = 0;
4410 cm->cdef_info.cdef_strengths[0] = 0;
4411 cm->cdef_info.nb_cdef_strengths = 1;
4412 cm->cdef_info.cdef_uv_strengths[0] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004413 } else {
Steinar Midtskogen59782122017-07-20 08:49:43 +02004414 // Find CDEF parameters
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004415 av1_cdef_search(cm->frame_to_show, cpi->source, cm, xd,
Debargha Mukherjeed7338aa2017-11-04 07:34:50 -07004416 cpi->sf.fast_cdef_search);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004417
4418 // Apply the filter
4419 av1_cdef_frame(cm->frame_to_show, cm, xd);
4420 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004421
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004422 superres_post_encode(cpi);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004423
Yaowu Xu35ee2342017-11-08 11:50:46 -08004424 if (no_restoration) {
4425 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4426 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4427 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
4428 } else {
4429 av1_loop_restoration_save_boundary_lines(cm->frame_to_show, cm, 1);
4430 av1_pick_filter_restoration(cpi->source, cpi);
4431 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4432 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4433 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304434 if (cpi->num_workers > 1)
4435 av1_loop_restoration_filter_frame_mt(cm->frame_to_show, cm, 0,
4436 cpi->workers, cpi->num_workers,
4437 &cpi->lr_row_sync, &cpi->lr_ctxt);
4438 else
4439 av1_loop_restoration_filter_frame(cm->frame_to_show, cm, 0,
4440 &cpi->lr_ctxt);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004441 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004442 }
Fergus Simpsonbc189932017-05-16 17:02:39 -07004443}
4444
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07004445static int encode_without_recode_loop(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004446 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004447 int q = 0, bottom_index = 0, top_index = 0; // Dummy variables.
Yaowu Xuc27fc142016-08-22 16:08:15 -07004448
Yaowu Xuf883b422016-08-30 14:01:10 -07004449 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004450
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004451 set_size_independent_vars(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004452
Fergus Simpsonbc189932017-05-16 17:02:39 -07004453 setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004454
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004455 assert(cm->width == cpi->scaled_source.y_crop_width);
4456 assert(cm->height == cpi->scaled_source.y_crop_height);
Fergus Simpsonfecb2ab2017-04-30 15:49:57 -07004457
Yaowu Xuc27fc142016-08-22 16:08:15 -07004458 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4459
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004460 cpi->source =
4461 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
4462 if (cpi->unscaled_last_source != NULL)
4463 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4464 &cpi->scaled_last_source);
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004465 cpi->source->buf_8bit_valid = 0;
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004466 if (frame_is_intra_only(cm) == 0) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004467 scale_references(cpi);
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004468 }
4469
Yaowu Xuf883b422016-08-30 14:01:10 -07004470 av1_set_quantizer(cm, q);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004471 setup_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004472 suppress_active_map(cpi);
hui sued5a30f2017-04-27 16:02:49 -07004473
Yaowu Xuc27fc142016-08-22 16:08:15 -07004474 // Variance adaptive and in frame q adjustment experiments are mutually
4475 // exclusive.
4476 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004477 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004478 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004479 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004480 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004481 av1_cyclic_refresh_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004482 }
4483 apply_active_map(cpi);
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004484 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00004485 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004486 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
David Barker11c93562018-06-05 12:00:07 +01004487 } else {
4488 calculate_segdata(&cm->seg);
Yue Chend90d3432018-03-16 11:28:42 -07004489 }
David Barkercab37552018-03-21 11:56:24 +00004490 } else {
4491 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004492 }
David Barkercab37552018-03-21 11:56:24 +00004493 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004494
4495 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07004496 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004497
4498 // Update some stats from cyclic refresh, and check if we should not update
4499 // golden reference, for 1 pass CBR.
4500 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->frame_type != KEY_FRAME &&
Yaowu Xuf883b422016-08-30 14:01:10 -07004501 (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == AOM_CBR))
4502 av1_cyclic_refresh_check_golden_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004503
4504 // Update the skip mb flag probabilities based on the distribution
4505 // seen in the last encoder iteration.
4506 // update_base_skip_probs(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004507 aom_clear_system_state();
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07004508 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004509}
4510
Tom Finegane4099e32018-01-23 12:01:51 -08004511static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004512 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004513 RATE_CONTROL *const rc = &cpi->rc;
4514 int bottom_index, top_index;
4515 int loop_count = 0;
4516 int loop_at_this_size = 0;
4517 int loop = 0;
4518 int overshoot_seen = 0;
4519 int undershoot_seen = 0;
4520 int frame_over_shoot_limit;
4521 int frame_under_shoot_limit;
4522 int q = 0, q_low = 0, q_high = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004523
4524 set_size_independent_vars(cpi);
4525
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004526 cpi->source->buf_8bit_valid = 0;
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004527
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004528 aom_clear_system_state();
Debargha Mukherjeedf713102018-10-02 12:33:32 -07004529
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004530 setup_frame_size(cpi);
4531 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4532
Yaowu Xuc27fc142016-08-22 16:08:15 -07004533 do {
Yaowu Xuf883b422016-08-30 14:01:10 -07004534 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004535
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004536 if (loop_count == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004537 // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
4538 set_mv_search_params(cpi);
4539
4540 // Reset the loop state for new frame size.
4541 overshoot_seen = 0;
4542 undershoot_seen = 0;
4543
Yaowu Xuc27fc142016-08-22 16:08:15 -07004544 q_low = bottom_index;
4545 q_high = top_index;
4546
4547 loop_at_this_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004548
Urvang Joshi2a74cf22017-12-18 17:21:41 -08004549 // Decide frame size bounds first time through.
Yaowu Xuf883b422016-08-30 14:01:10 -07004550 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4551 &frame_under_shoot_limit,
4552 &frame_over_shoot_limit);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004553 }
4554
Urvang Joshif1fa6862018-01-08 16:39:33 -08004555 // if frame was scaled calculate global_motion_search again if already
4556 // done
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004557 if (loop_count > 0 && cpi->source && cpi->global_motion_search_done)
4558 if (cpi->source->y_crop_width != cm->width ||
4559 cpi->source->y_crop_height != cm->height)
4560 cpi->global_motion_search_done = 0;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004561 cpi->source =
4562 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004563 if (cpi->unscaled_last_source != NULL)
4564 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4565 &cpi->scaled_last_source);
4566
Yaowu Xuc27fc142016-08-22 16:08:15 -07004567 if (frame_is_intra_only(cm) == 0) {
4568 if (loop_count > 0) {
4569 release_scaled_references(cpi);
4570 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004571 scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004572 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004573 av1_set_quantizer(cm, q);
Yaowu Xu2e587232018-05-10 09:30:08 -07004574 // printf("Frame %d/%d: q = %d, frame_type = %d\n", cm->current_video_frame,
4575 // cm->show_frame, q, cm->frame_type);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004576
4577 if (loop_count == 0) setup_frame(cpi);
4578
Yaowu Xuc27fc142016-08-22 16:08:15 -07004579 // Base q-index may have changed, so we need to assign proper default coef
4580 // probs before every iteration.
David Barkercc615a82018-03-19 14:38:51 +00004581 if (cm->primary_ref_frame == PRIMARY_REF_NONE ||
4582 cm->frame_refs[cm->primary_ref_frame].idx < 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004583 av1_default_coef_probs(cm);
Hui Su3694c832017-11-10 14:15:58 -08004584 av1_setup_frame_contexts(cm);
David Barkerfc91b392018-03-09 15:32:03 +00004585 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004586
Yaowu Xuc27fc142016-08-22 16:08:15 -07004587 // Variance adaptive and in frame q adjustment experiments are mutually
4588 // exclusive.
4589 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004590 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004591 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004592 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004593 }
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004594 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00004595 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004596 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
David Barker11c93562018-06-05 12:00:07 +01004597 } else {
4598 calculate_segdata(&cm->seg);
Yue Chend90d3432018-03-16 11:28:42 -07004599 }
David Barkercab37552018-03-21 11:56:24 +00004600 } else {
4601 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004602 }
David Barkercab37552018-03-21 11:56:24 +00004603 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004604
4605 // transform / motion compensation build reconstruction frame
Jingning Han8f661602017-08-19 08:16:50 -07004606 save_coding_context(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004607 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004608
4609 // Update the skip mb flag probabilities based on the distribution
4610 // seen in the last encoder iteration.
4611 // update_base_skip_probs(cpi);
4612
Yaowu Xuf883b422016-08-30 14:01:10 -07004613 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004614
4615 // Dummy pack of the bitstream using up to date stats to get an
4616 // accurate estimate of output frame size to determine if we need
4617 // to recode.
4618 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
Jingning Han8f661602017-08-19 08:16:50 -07004619 restore_coding_context(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08004620
4621 if (av1_pack_bitstream(cpi, dest, size) != AOM_CODEC_OK)
4622 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004623
4624 rc->projected_frame_size = (int)(*size) << 3;
4625 restore_coding_context(cpi);
4626
4627 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4628 }
4629
Yaowu Xuf883b422016-08-30 14:01:10 -07004630 if (cpi->oxcf.rc_mode == AOM_Q) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004631 loop = 0;
4632 } else {
4633 if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
4634 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
4635 int last_q = q;
4636 int64_t kf_err;
4637
4638 int64_t high_err_target = cpi->ambient_err;
4639 int64_t low_err_target = cpi->ambient_err >> 1;
4640
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004641 if (cm->seq_params.use_highbitdepth) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004642 kf_err = aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004643 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004644 kf_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004645 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004646 // Prevent possible divide by zero error below for perfect KF
4647 kf_err += !kf_err;
4648
4649 // The key frame is not good enough or we can afford
4650 // to make it better without undue risk of popping.
4651 if ((kf_err > high_err_target &&
4652 rc->projected_frame_size <= frame_over_shoot_limit) ||
4653 (kf_err > low_err_target &&
4654 rc->projected_frame_size <= frame_under_shoot_limit)) {
4655 // Lower q_high
4656 q_high = q > q_low ? q - 1 : q_low;
4657
4658 // Adjust Q
4659 q = (int)((q * high_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004660 q = AOMMIN(q, (q_high + q_low) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004661 } else if (kf_err < low_err_target &&
4662 rc->projected_frame_size >= frame_under_shoot_limit) {
4663 // The key frame is much better than the previous frame
4664 // Raise q_low
4665 q_low = q < q_high ? q + 1 : q_high;
4666
4667 // Adjust Q
4668 q = (int)((q * low_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004669 q = AOMMIN(q, (q_high + q_low + 1) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004670 }
4671
4672 // Clamp Q to upper and lower limits:
4673 q = clamp(q, q_low, q_high);
4674
4675 loop = q != last_q;
4676 } else if (recode_loop_test(cpi, frame_over_shoot_limit,
4677 frame_under_shoot_limit, q,
Yaowu Xuf883b422016-08-30 14:01:10 -07004678 AOMMAX(q_high, top_index), bottom_index)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004679 // Is the projected frame size out of range and are we allowed
4680 // to attempt to recode.
4681 int last_q = q;
4682 int retries = 0;
4683
Yaowu Xuc27fc142016-08-22 16:08:15 -07004684 // Frame size out of permitted range:
4685 // Update correction factor & compute new Q to try...
Yaowu Xuc27fc142016-08-22 16:08:15 -07004686 // Frame is too large
4687 if (rc->projected_frame_size > rc->this_frame_target) {
4688 // Special case if the projected size is > the max allowed.
4689 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
4690 q_high = rc->worst_quality;
4691
4692 // Raise Qlow as to at least the current value
4693 q_low = q < q_high ? q + 1 : q_high;
4694
4695 if (undershoot_seen || loop_at_this_size > 1) {
4696 // Update rate_correction_factor unless
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004697 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004698
4699 q = (q_high + q_low + 1) / 2;
4700 } else {
4701 // Update rate_correction_factor unless
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004702 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004703
Yaowu Xuf883b422016-08-30 14:01:10 -07004704 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004705 AOMMAX(q_high, top_index), cm->width,
4706 cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004707
4708 while (q < q_low && retries < 10) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004709 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004710 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004711 AOMMAX(q_high, top_index), cm->width,
4712 cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004713 retries++;
4714 }
4715 }
4716
4717 overshoot_seen = 1;
4718 } else {
4719 // Frame is too small
4720 q_high = q > q_low ? q - 1 : q_low;
4721
4722 if (overshoot_seen || loop_at_this_size > 1) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004723 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004724 q = (q_high + q_low) / 2;
4725 } else {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004726 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004727 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004728 top_index, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004729 // Special case reset for qlow for constrained quality.
4730 // This should only trigger where there is very substantial
4731 // undershoot on a frame and the auto cq level is above
4732 // the user passsed in value.
Yaowu Xuf883b422016-08-30 14:01:10 -07004733 if (cpi->oxcf.rc_mode == AOM_CQ && q < q_low) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004734 q_low = q;
4735 }
4736
4737 while (q > q_high && retries < 10) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004738 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004739 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004740 top_index, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004741 retries++;
4742 }
4743 }
4744
4745 undershoot_seen = 1;
4746 }
4747
4748 // Clamp Q to upper and lower limits:
4749 q = clamp(q, q_low, q_high);
4750
4751 loop = (q != last_q);
4752 } else {
4753 loop = 0;
4754 }
4755 }
4756
4757 // Special case for overlay frame.
4758 if (rc->is_src_frame_alt_ref &&
4759 rc->projected_frame_size < rc->max_frame_bandwidth)
4760 loop = 0;
4761
Debargha Mukherjee79853eb2018-09-13 17:38:02 -07004762 if (!cpi->sf.gm_disable_recode) {
4763 if (recode_loop_test_global_motion(cpi)) loop = 1;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004764 }
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004765
Yaowu Xuc27fc142016-08-22 16:08:15 -07004766 if (loop) {
4767 ++loop_count;
4768 ++loop_at_this_size;
4769
4770#if CONFIG_INTERNAL_STATS
4771 ++cpi->tot_recode_hits;
4772#endif
4773 }
4774 } while (loop);
Tom Finegane4099e32018-01-23 12:01:51 -08004775
4776 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004777}
4778
Yaowu Xuf883b422016-08-30 14:01:10 -07004779static int get_ref_frame_flags(const AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004780 const int *const map = cpi->common.ref_frame_map;
4781
Zoe Liu368bf162017-11-03 20:10:19 -07004782 // No.1 Priority: LAST_FRAME
Urvang Joshi4d9f15f2018-11-05 15:26:22 -08004783 const int last2_is_last =
4784 map[cpi->remapped_ref_idx[1]] == map[cpi->remapped_ref_idx[0]];
4785 const int last3_is_last =
4786 map[cpi->remapped_ref_idx[2]] == map[cpi->remapped_ref_idx[0]];
Urvang Joshi39585822018-11-07 17:47:32 -08004787 const int gld_is_last = map[get_ref_frame_map_idx(cpi, GOLDEN_FRAME)] ==
Urvang Joshi4d9f15f2018-11-05 15:26:22 -08004788 map[cpi->remapped_ref_idx[0]];
Urvang Joshi39585822018-11-07 17:47:32 -08004789 const int bwd_is_last = map[get_ref_frame_map_idx(cpi, BWDREF_FRAME)] ==
Urvang Joshi4d9f15f2018-11-05 15:26:22 -08004790 map[cpi->remapped_ref_idx[0]];
Urvang Joshi39585822018-11-07 17:47:32 -08004791 const int alt2_is_last = map[get_ref_frame_map_idx(cpi, ALTREF2_FRAME)] ==
Urvang Joshi4d9f15f2018-11-05 15:26:22 -08004792 map[cpi->remapped_ref_idx[0]];
Urvang Joshi39585822018-11-07 17:47:32 -08004793 const int alt_is_last = map[get_ref_frame_map_idx(cpi, ALTREF_FRAME)] ==
Urvang Joshi4d9f15f2018-11-05 15:26:22 -08004794 map[cpi->remapped_ref_idx[0]];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004795
Zoe Liu368bf162017-11-03 20:10:19 -07004796 // No.2 Priority: ALTREF_FRAME
Urvang Joshi4d9f15f2018-11-05 15:26:22 -08004797 const int last2_is_alt = map[cpi->remapped_ref_idx[1]] ==
Urvang Joshi39585822018-11-07 17:47:32 -08004798 map[get_ref_frame_map_idx(cpi, ALTREF_FRAME)];
Urvang Joshi4d9f15f2018-11-05 15:26:22 -08004799 const int last3_is_alt = map[cpi->remapped_ref_idx[2]] ==
Urvang Joshi39585822018-11-07 17:47:32 -08004800 map[get_ref_frame_map_idx(cpi, ALTREF_FRAME)];
4801 const int gld_is_alt = map[get_ref_frame_map_idx(cpi, GOLDEN_FRAME)] ==
4802 map[get_ref_frame_map_idx(cpi, ALTREF_FRAME)];
4803 const int bwd_is_alt = map[get_ref_frame_map_idx(cpi, BWDREF_FRAME)] ==
4804 map[get_ref_frame_map_idx(cpi, ALTREF_FRAME)];
4805 const int alt2_is_alt = map[get_ref_frame_map_idx(cpi, ALTREF2_FRAME)] ==
4806 map[get_ref_frame_map_idx(cpi, ALTREF_FRAME)];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004807
Zoe Liu368bf162017-11-03 20:10:19 -07004808 // No.3 Priority: LAST2_FRAME
Urvang Joshi4d9f15f2018-11-05 15:26:22 -08004809 const int last3_is_last2 =
4810 map[cpi->remapped_ref_idx[2]] == map[cpi->remapped_ref_idx[1]];
Urvang Joshi39585822018-11-07 17:47:32 -08004811 const int gld_is_last2 = map[get_ref_frame_map_idx(cpi, GOLDEN_FRAME)] ==
Urvang Joshi4d9f15f2018-11-05 15:26:22 -08004812 map[cpi->remapped_ref_idx[1]];
Urvang Joshi39585822018-11-07 17:47:32 -08004813 const int bwd_is_last2 = map[get_ref_frame_map_idx(cpi, BWDREF_FRAME)] ==
Urvang Joshi4d9f15f2018-11-05 15:26:22 -08004814 map[cpi->remapped_ref_idx[1]];
Urvang Joshi39585822018-11-07 17:47:32 -08004815 const int alt2_is_last2 = map[get_ref_frame_map_idx(cpi, ALTREF2_FRAME)] ==
Urvang Joshi4d9f15f2018-11-05 15:26:22 -08004816 map[cpi->remapped_ref_idx[1]];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004817
Zoe Liu368bf162017-11-03 20:10:19 -07004818 // No.4 Priority: LAST3_FRAME
Urvang Joshi39585822018-11-07 17:47:32 -08004819 const int gld_is_last3 = map[get_ref_frame_map_idx(cpi, GOLDEN_FRAME)] ==
Urvang Joshi4d9f15f2018-11-05 15:26:22 -08004820 map[cpi->remapped_ref_idx[2]];
Urvang Joshi39585822018-11-07 17:47:32 -08004821 const int bwd_is_last3 = map[get_ref_frame_map_idx(cpi, BWDREF_FRAME)] ==
Urvang Joshi4d9f15f2018-11-05 15:26:22 -08004822 map[cpi->remapped_ref_idx[2]];
Urvang Joshi39585822018-11-07 17:47:32 -08004823 const int alt2_is_last3 = map[get_ref_frame_map_idx(cpi, ALTREF2_FRAME)] ==
Urvang Joshi4d9f15f2018-11-05 15:26:22 -08004824 map[cpi->remapped_ref_idx[2]];
Zoe Liu368bf162017-11-03 20:10:19 -07004825
4826 // No.5 Priority: GOLDEN_FRAME
Urvang Joshi39585822018-11-07 17:47:32 -08004827 const int bwd_is_gld = map[get_ref_frame_map_idx(cpi, BWDREF_FRAME)] ==
4828 map[get_ref_frame_map_idx(cpi, GOLDEN_FRAME)];
4829 const int alt2_is_gld = map[get_ref_frame_map_idx(cpi, ALTREF2_FRAME)] ==
4830 map[get_ref_frame_map_idx(cpi, GOLDEN_FRAME)];
Zoe Liu368bf162017-11-03 20:10:19 -07004831
4832 // No.6 Priority: BWDREF_FRAME
Urvang Joshi39585822018-11-07 17:47:32 -08004833 const int alt2_is_bwd = map[get_ref_frame_map_idx(cpi, ALTREF2_FRAME)] ==
4834 map[get_ref_frame_map_idx(cpi, BWDREF_FRAME)];
Zoe Liu368bf162017-11-03 20:10:19 -07004835
4836 // No.7 Priority: ALTREF2_FRAME
4837
Yunqing Wang9a50fec2017-11-02 17:02:00 -07004838 // After av1_apply_encoding_flags() is called, cpi->ref_frame_flags might be
4839 // adjusted according to external encoder flags.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08004840 int flags = cpi->ext_ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004841
Yaowu Xuf883b422016-08-30 14:01:10 -07004842 if (cpi->rc.frames_till_gf_update_due == INT_MAX) flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004843
Yaowu Xuf883b422016-08-30 14:01:10 -07004844 if (alt_is_last) flags &= ~AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004845
Yaowu Xuf883b422016-08-30 14:01:10 -07004846 if (last2_is_last || last2_is_alt) flags &= ~AOM_LAST2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004847
Zoe Liu368bf162017-11-03 20:10:19 -07004848 if (last3_is_last || last3_is_alt || last3_is_last2) flags &= ~AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004849
Zoe Liu368bf162017-11-03 20:10:19 -07004850 if (gld_is_last || gld_is_alt || gld_is_last2 || gld_is_last3)
4851 flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004852
Zoe Liu368bf162017-11-03 20:10:19 -07004853 if ((bwd_is_last || bwd_is_alt || bwd_is_last2 || bwd_is_last3 ||
4854 bwd_is_gld) &&
Yaowu Xuf883b422016-08-30 14:01:10 -07004855 (flags & AOM_BWD_FLAG))
4856 flags &= ~AOM_BWD_FLAG;
Zoe Liue9b15e22017-07-19 15:53:01 -07004857
Zoe Liu368bf162017-11-03 20:10:19 -07004858 if ((alt2_is_last || alt2_is_alt || alt2_is_last2 || alt2_is_last3 ||
4859 alt2_is_gld || alt2_is_bwd) &&
Zoe Liue9b15e22017-07-19 15:53:01 -07004860 (flags & AOM_ALT2_FLAG))
4861 flags &= ~AOM_ALT2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004862
4863 return flags;
4864}
4865
Yaowu Xuf883b422016-08-30 14:01:10 -07004866static void set_ext_overrides(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004867 // Overrides the defaults with the externally supplied values with
Yaowu Xuf883b422016-08-30 14:01:10 -07004868 // av1_update_reference() and av1_update_entropy() calls
Yaowu Xuc27fc142016-08-22 16:08:15 -07004869 // Note: The overrides are valid only for the next frame passed
4870 // to encode_frame_to_data_rate() function
sarahparker9806fed2018-03-30 17:43:44 -07004871 if (cpi->ext_use_s_frame) cpi->common.frame_type = S_FRAME;
Sarah Parker50b6d6e2018-04-11 19:21:54 -07004872 cpi->common.force_primary_ref_none = cpi->ext_use_primary_ref_none;
Debargha Mukherjee52fb0472018-03-29 15:48:11 -07004873
Yaowu Xuc27fc142016-08-22 16:08:15 -07004874 if (cpi->ext_refresh_frame_context_pending) {
4875 cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
4876 cpi->ext_refresh_frame_context_pending = 0;
4877 }
4878 if (cpi->ext_refresh_frame_flags_pending) {
4879 cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
4880 cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
4881 cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07004882 cpi->refresh_bwd_ref_frame = cpi->ext_refresh_bwd_ref_frame;
4883 cpi->refresh_alt2_ref_frame = cpi->ext_refresh_alt2_ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004884 cpi->ext_refresh_frame_flags_pending = 0;
4885 }
sarahparker21dbca42018-03-30 17:43:44 -07004886 cpi->common.allow_ref_frame_mvs = cpi->ext_use_ref_frame_mvs;
Sarah Parker14feea42018-07-06 16:41:41 -07004887 // A keyframe is already error resilient and keyframes with
4888 // error_resilient_mode interferes with the use of show_existing_frame
4889 // when forward reference keyframes are enabled.
4890 cpi->common.error_resilient_mode =
4891 cpi->ext_use_error_resilient && cpi->common.frame_type != KEY_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004892}
4893
Yaowu Xuc27fc142016-08-22 16:08:15 -07004894#define DUMP_RECON_FRAMES 0
4895
4896#if DUMP_RECON_FRAMES == 1
4897// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07004898static void dump_filtered_recon_frames(AV1_COMP *cpi) {
4899 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004900 const YV12_BUFFER_CONFIG *recon_buf = cm->frame_to_show;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004901
Zoe Liub4f31032017-11-03 23:48:35 -07004902 if (recon_buf == NULL) {
4903 printf("Frame %d is not ready.\n", cm->current_video_frame);
4904 return;
4905 }
4906
Zoe Liu27deb382018-03-27 15:13:56 -07004907 static const int flag_list[REF_FRAMES] = { 0,
4908 AOM_LAST_FLAG,
4909 AOM_LAST2_FLAG,
4910 AOM_LAST3_FLAG,
4911 AOM_GOLD_FLAG,
4912 AOM_BWD_FLAG,
4913 AOM_ALT2_FLAG,
4914 AOM_ALT_FLAG };
Zoe Liub4f31032017-11-03 23:48:35 -07004915 printf(
4916 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
4917 "show_existing_frame=%d) "
4918 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
4919 cm->current_video_frame, cm->frame_offset, cm->show_frame,
4920 cm->show_existing_frame);
4921 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
4922 const int buf_idx = cm->frame_refs[ref_frame - LAST_FRAME].idx;
4923 const int ref_offset =
4924 (buf_idx >= 0)
4925 ? (int)cm->buffer_pool->frame_bufs[buf_idx].cur_frame_offset
4926 : -1;
Zoe Liuf452fdf2017-11-02 23:08:12 -07004927 printf(
4928 " %d(%c-%d-%4.2f)", ref_offset,
4929 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N',
4930 (buf_idx >= 0) ? (int)cpi->frame_rf_level[buf_idx] : -1,
4931 (buf_idx >= 0) ? rate_factor_deltas[cpi->frame_rf_level[buf_idx]] : -1);
Zoe Liub4f31032017-11-03 23:48:35 -07004932 }
4933 printf(" ]\n");
Zoe Liub4f31032017-11-03 23:48:35 -07004934
4935 if (!cm->show_frame) {
4936 printf("Frame %d is a no show frame, so no image dump.\n",
Yaowu Xuc27fc142016-08-22 16:08:15 -07004937 cm->current_video_frame);
4938 return;
4939 }
4940
Zoe Liub4f31032017-11-03 23:48:35 -07004941 int h;
4942 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
4943 FILE *f_recon = NULL;
4944
Yaowu Xuc27fc142016-08-22 16:08:15 -07004945 if (cm->current_video_frame == 0) {
4946 if ((f_recon = fopen(file_name, "wb")) == NULL) {
4947 printf("Unable to open file %s to write.\n", file_name);
4948 return;
4949 }
4950 } else {
4951 if ((f_recon = fopen(file_name, "ab")) == NULL) {
4952 printf("Unable to open file %s to append.\n", file_name);
4953 return;
4954 }
4955 }
4956 printf(
Zoe Liuf40a9572017-10-13 12:37:19 -07004957 "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, "
4958 "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
4959 "refresh_alt_ref_frame=%d, rf_level=%d, "
4960 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
Yaowu Xuc27fc142016-08-22 16:08:15 -07004961 cm->current_video_frame, cpi->twopass.gf_group.index,
4962 cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index],
Zoe Liuf40a9572017-10-13 12:37:19 -07004963 cm->frame_offset, cm->show_frame, cm->show_existing_frame,
4964 cpi->rc.source_alt_ref_active, cpi->refresh_alt_ref_frame,
4965 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index],
4966 recon_buf->y_stride, recon_buf->uv_stride, cm->width, cm->height);
Zoe Liue9b15e22017-07-19 15:53:01 -07004967#if 0
4968 int ref_frame;
4969 printf("get_ref_frame_map_idx: [");
4970 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
4971 printf(" %d", get_ref_frame_map_idx(cpi, ref_frame));
4972 printf(" ]\n");
4973 printf("cm->new_fb_idx = %d\n", cm->new_fb_idx);
4974 printf("cm->ref_frame_map = [");
4975 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
4976 printf(" %d", cm->ref_frame_map[ref_frame - LAST_FRAME]);
4977 }
4978 printf(" ]\n");
4979#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07004980
4981 // --- Y ---
4982 for (h = 0; h < cm->height; ++h) {
4983 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
4984 f_recon);
4985 }
4986 // --- U ---
4987 for (h = 0; h < (cm->height >> 1); ++h) {
4988 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4989 f_recon);
4990 }
4991 // --- V ---
4992 for (h = 0; h < (cm->height >> 1); ++h) {
4993 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4994 f_recon);
4995 }
4996
4997 fclose(f_recon);
4998}
4999#endif // DUMP_RECON_FRAMES
5000
Wei-Ting Linfb7dc062018-06-28 18:26:13 -07005001static INLINE int is_frame_droppable(AV1_COMP *cpi) {
5002 return !(cpi->refresh_alt_ref_frame || cpi->refresh_alt2_ref_frame ||
5003 cpi->refresh_bwd_ref_frame || cpi->refresh_golden_frame ||
5004 cpi->refresh_last_frame);
5005}
5006
Tom Finegane4099e32018-01-23 12:01:51 -08005007static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size, uint8_t *dest,
5008 int skip_adapt,
5009 unsigned int *frame_flags) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005010 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005011 SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuf883b422016-08-30 14:01:10 -07005012 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005013 struct segmentation *const seg = &cm->seg;
Thomas Davies4822e142017-10-10 11:30:36 +01005014
Yaowu Xuc27fc142016-08-22 16:08:15 -07005015 set_ext_overrides(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07005016 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005017
Fangwen Fu8d164de2016-12-14 13:40:54 -08005018 // frame type has been decided outside of this function call
Zoe Liud78ce2d2018-02-24 05:50:57 -08005019 cm->cur_frame->intra_only = frame_is_intra_only(cm);
Zoe Liu2723a9d2018-02-22 20:17:00 -08005020 cm->cur_frame->frame_type = cm->frame_type;
Debargha Mukherjee07a7c1f2018-03-21 17:39:13 -07005021
5022 // S_FRAMEs are always error resilient
sarahparker27d686a2018-03-30 17:43:44 -07005023 cm->error_resilient_mode |= frame_is_sframe(cm);
Yunqing Wang9612d552018-05-15 14:58:30 -07005024
5025 cm->large_scale_tile = cpi->oxcf.large_scale_tile;
5026 cm->single_tile_decoding = cpi->oxcf.single_tile_decoding;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005027 if (cm->large_scale_tile) seq_params->frame_id_numbers_present_flag = 0;
Yunqing Wang9612d552018-05-15 14:58:30 -07005028
sarahparker21dbca42018-03-30 17:43:44 -07005029 cm->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
Yunqing Wangd48fb162018-06-15 10:55:28 -07005030 // cm->allow_ref_frame_mvs needs to be written into the frame header while
5031 // cm->large_scale_tile is 1, therefore, "cm->large_scale_tile=1" case is
5032 // separated from frame_might_allow_ref_frame_mvs().
5033 cm->allow_ref_frame_mvs &= !cm->large_scale_tile;
5034
Debargha Mukherjee1d7217e2018-03-26 13:32:13 -07005035 cm->allow_warped_motion =
Debargha Mukherjeea5b810a2018-03-26 19:19:55 -07005036 cpi->oxcf.allow_warped_motion && frame_might_allow_warped_motion(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005037
Jingning Hand8a15a62017-10-30 10:53:42 -07005038 // Reset the frame packet stamp index.
Sarah Parkerb9041612018-05-22 19:06:47 -07005039 if (cm->frame_type == KEY_FRAME && cm->show_frame)
5040 cm->current_video_frame = 0;
Jingning Hand8a15a62017-10-30 10:53:42 -07005041
Yaowu Xuc27fc142016-08-22 16:08:15 -07005042 // NOTE:
5043 // (1) Move the setup of the ref_frame_flags upfront as it would be
5044 // determined by the current frame properties;
Urvang Joshif1fa6862018-01-08 16:39:33 -08005045 // (2) The setup of the ref_frame_flags applies to both
5046 // show_existing_frame's
Yaowu Xuc27fc142016-08-22 16:08:15 -07005047 // and the other cases.
5048 if (cm->current_video_frame > 0)
5049 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
5050
Sarah Parker33005522018-07-27 14:46:25 -07005051 if (encode_show_existing_frame(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005052 // NOTE(zoeliu): In BIDIR_PRED, the existing frame to show is the current
5053 // BWDREF_FRAME in the reference frame buffer.
Sarah Parkerb9041612018-05-22 19:06:47 -07005054 if (cm->frame_type == KEY_FRAME) {
5055 cm->reset_decoder_state = 1;
5056 } else {
5057 cm->frame_type = INTER_FRAME;
5058 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005059 cm->show_frame = 1;
5060 cpi->frame_flags = *frame_flags;
5061
Jingning Han8f661602017-08-19 08:16:50 -07005062 restore_coding_context(cpi);
Zoe Liub4f31032017-11-03 23:48:35 -07005063
Yaowu Xuc27fc142016-08-22 16:08:15 -07005064 // Build the bitstream
Tom Finegane4099e32018-01-23 12:01:51 -08005065 if (av1_pack_bitstream(cpi, dest, size) != AOM_CODEC_OK)
5066 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005067
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07005068 cpi->seq_params_locked = 1;
5069
Yaowu Xuc27fc142016-08-22 16:08:15 -07005070 // Set up frame to show to get ready for stats collection.
5071 cm->frame_to_show = get_frame_new_buffer(cm);
5072
Zoe Liub4f31032017-11-03 23:48:35 -07005073 // Update current frame offset.
5074 cm->frame_offset =
5075 cm->buffer_pool->frame_bufs[cm->new_fb_idx].cur_frame_offset;
Zoe Liub4f31032017-11-03 23:48:35 -07005076
Yaowu Xuc27fc142016-08-22 16:08:15 -07005077#if DUMP_RECON_FRAMES == 1
5078 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
5079 dump_filtered_recon_frames(cpi);
5080#endif // DUMP_RECON_FRAMES
5081
5082 // Update the LAST_FRAME in the reference frame buffer.
Zoe Liue9b15e22017-07-19 15:53:01 -07005083 // NOTE:
5084 // (1) For BWDREF_FRAME as the show_existing_frame, the reference frame
5085 // update has been done previously when handling the LAST_BIPRED_FRAME
5086 // right before BWDREF_FRAME (in the display order);
5087 // (2) For INTNL_OVERLAY as the show_existing_frame, the reference frame
Urvang Joshif1fa6862018-01-08 16:39:33 -08005088 // update will be done when the following is called, which will
5089 // exchange
Zoe Liue9b15e22017-07-19 15:53:01 -07005090 // the virtual indexes between LAST_FRAME and ALTREF2_FRAME, so that
Urvang Joshif1fa6862018-01-08 16:39:33 -08005091 // LAST3 will get retired, LAST2 becomes LAST3, LAST becomes LAST2,
5092 // and
Zoe Liue9b15e22017-07-19 15:53:01 -07005093 // ALTREF2_FRAME will serve as the new LAST_FRAME.
Cheng Chen46f30c72017-09-07 11:13:33 -07005094 update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005095
5096 // Update frame flags
5097 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
5098 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
5099 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
5100
5101 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
5102
5103 // Update the frame type
5104 cm->last_frame_type = cm->frame_type;
5105
Yaowu Xuc27fc142016-08-22 16:08:15 -07005106 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
5107 // to do post-encoding update accordingly.
5108 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005109 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07005110 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005111 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005112
Yaowu Xuc27fc142016-08-22 16:08:15 -07005113 ++cm->current_video_frame;
5114
Tom Finegane4099e32018-01-23 12:01:51 -08005115 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005116 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005117
5118 // Set default state for segment based loop filter update flags.
5119 cm->lf.mode_ref_delta_update = 0;
5120
Yaowu Xuc27fc142016-08-22 16:08:15 -07005121 // Set various flags etc to special state if it is a key frame.
Tarek AMARAc9813852018-03-05 18:40:18 -05005122 if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005123 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07005124 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005125
5126 // If segmentation is enabled force a map update for key frames.
5127 if (seg->enabled) {
5128 seg->update_map = 1;
5129 seg->update_data = 1;
5130 }
5131
5132 // The alternate reference frame cannot be active for a key frame.
5133 cpi->rc.source_alt_ref_active = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005134 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00005135 if (cpi->oxcf.mtu == 0) {
5136 cm->num_tg = cpi->oxcf.num_tile_groups;
5137 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08005138 // Use a default value for the purposes of weighting costs in probability
5139 // updates
Thomas Daviesaf6df172016-11-09 14:04:18 +00005140 cm->num_tg = DEFAULT_MAX_NUM_TG;
5141 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005142
5143 // For 1 pass CBR, check if we are dropping this frame.
5144 // Never drop on key frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07005145 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005146 cm->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005147 if (av1_rc_drop_frame(cpi)) {
5148 av1_rc_postencode_update_drop_frame(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08005149 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005150 }
5151 }
5152
Yaowu Xuf883b422016-08-30 14:01:10 -07005153 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005154
5155#if CONFIG_INTERNAL_STATS
5156 memset(cpi->mode_chosen_counts, 0,
5157 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
5158#endif
5159
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005160 if (seq_params->frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005161 /* Non-normative definition of current_frame_id ("frame counter" with
Johann123e8a62017-12-28 14:40:49 -08005162 * wraparound) */
Sebastien Alaiwand418f682017-10-19 15:06:52 +02005163 const int frame_id_length = FRAME_ID_LENGTH;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005164 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00005165 int lsb, msb;
Yaowu Xud3e7c682017-12-21 14:08:25 -08005166 /* quasi-random initialization of current_frame_id for a key frame */
Alex Conversef77fd0b2017-04-20 11:00:24 -07005167 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
5168 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
5169 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005170 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005171 lsb = cpi->source->y_buffer[0] & 0xff;
5172 msb = cpi->source->y_buffer[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005173 }
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01005174 cm->current_frame_id = ((msb << 8) + lsb) % (1 << frame_id_length);
Tarek AMARAc9813852018-03-05 18:40:18 -05005175
5176 // S_frame is meant for stitching different streams of different
5177 // resolutions together, so current_frame_id must be the
5178 // same across different streams of the same content current_frame_id
5179 // should be the same and not random. 0x37 is a chosen number as start
5180 // point
5181 if (cpi->oxcf.sframe_enabled) cm->current_frame_id = 0x37;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005182 } else {
5183 cm->current_frame_id =
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01005184 (cm->current_frame_id + 1 + (1 << frame_id_length)) %
5185 (1 << frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005186 }
5187 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005188
Hui Su483a8452018-02-26 12:28:48 -08005189 switch (cpi->oxcf.cdf_update_mode) {
5190 case 0: // No CDF update for any frames(4~6% compression loss).
5191 cm->disable_cdf_update = 1;
5192 break;
5193 case 1: // Enable CDF update for all frames.
5194 cm->disable_cdf_update = 0;
5195 break;
5196 case 2:
5197 // Strategically determine at which frames to do CDF update.
5198 // Currently only enable CDF update for all-intra and no-show frames(1.5%
5199 // compression loss).
5200 // TODO(huisu@google.com): design schemes for various trade-offs between
5201 // compression quality and decoding speed.
Hui Sub1b76b32018-02-27 15:24:48 -08005202 cm->disable_cdf_update =
5203 (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
Hui Su483a8452018-02-26 12:28:48 -08005204 break;
5205 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005206 cm->timing_info_present &= !seq_params->reduced_still_picture_hdr;
Hui Su483a8452018-02-26 12:28:48 -08005207
Yaowu Xuc27fc142016-08-22 16:08:15 -07005208 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07005209 if (encode_without_recode_loop(cpi) != AOM_CODEC_OK) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005210 } else {
Tom Finegane4099e32018-01-23 12:01:51 -08005211 if (encode_with_recode_loop(cpi, size, dest) != AOM_CODEC_OK)
5212 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005213 }
5214
Yi Luo10e23002017-07-31 11:54:43 -07005215 cm->last_tile_cols = cm->tile_cols;
5216 cm->last_tile_rows = cm->tile_rows;
5217
Yaowu Xuc27fc142016-08-22 16:08:15 -07005218#ifdef OUTPUT_YUV_SKINMAP
5219 if (cpi->common.current_video_frame > 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005220 av1_compute_skin_map(cpi, yuv_skinmap_file);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005221 }
5222#endif // OUTPUT_YUV_SKINMAP
5223
5224 // Special case code to reduce pulsing when key frames are forced at a
5225 // fixed interval. Note the reconstruction error if it is the frame before
5226 // the force key frame
5227 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005228 if (seq_params->use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005229 cpi->ambient_err =
Alex Conversef77fd0b2017-04-20 11:00:24 -07005230 aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005231 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005232 cpi->ambient_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005233 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005234 }
5235
Tarek AMARAc9813852018-03-05 18:40:18 -05005236 // If the encoder forced a KEY_FRAME decision or if frame is an S_FRAME
Sarah Parkerb9041612018-05-22 19:06:47 -07005237 if ((cm->frame_type == KEY_FRAME && cm->show_frame) || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005238 cpi->refresh_last_frame = 1;
5239 }
5240
5241 cm->frame_to_show = get_frame_new_buffer(cm);
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005242 cm->frame_to_show->color_primaries = seq_params->color_primaries;
5243 cm->frame_to_show->transfer_characteristics =
5244 seq_params->transfer_characteristics;
5245 cm->frame_to_show->matrix_coefficients = seq_params->matrix_coefficients;
5246 cm->frame_to_show->monochrome = seq_params->monochrome;
5247 cm->frame_to_show->chroma_sample_position =
5248 seq_params->chroma_sample_position;
5249 cm->frame_to_show->color_range = seq_params->color_range;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005250 cm->frame_to_show->render_width = cm->render_width;
5251 cm->frame_to_show->render_height = cm->render_height;
5252
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005253 // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
5254 // off.
Yaowu Xuc27fc142016-08-22 16:08:15 -07005255
5256 // Pick the loop filter level for the frame.
Cheng Chen68dc9142018-05-02 17:46:28 -07005257 if (!cm->allow_intrabc) {
David Barker218556e2018-02-14 14:23:12 +00005258 loopfilter_frame(cpi, cm);
Hui Su06463e42018-02-23 22:17:36 -08005259 } else {
Hui Su06463e42018-02-23 22:17:36 -08005260 cm->lf.filter_level[0] = 0;
5261 cm->lf.filter_level[1] = 0;
David Turnerebf96f42018-11-14 16:57:57 +00005262 cm->cdef_info.cdef_bits = 0;
5263 cm->cdef_info.cdef_strengths[0] = 0;
5264 cm->cdef_info.nb_cdef_strengths = 1;
5265 cm->cdef_info.cdef_uv_strengths[0] = 0;
Hui Su06463e42018-02-23 22:17:36 -08005266 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5267 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5268 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Hui Su06463e42018-02-23 22:17:36 -08005269 }
David Barker218556e2018-02-14 14:23:12 +00005270
5271 // TODO(debargha): Fix mv search range on encoder side
5272 // aom_extend_frame_inner_borders(cm->frame_to_show, av1_num_planes(cm));
5273 aom_extend_frame_borders(cm->frame_to_show, av1_num_planes(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005274
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07005275#ifdef OUTPUT_YUV_REC
5276 aom_write_one_yuv_frame(cm, cm->frame_to_show);
5277#endif
5278
Yaowu Xuc27fc142016-08-22 16:08:15 -07005279 // Build the bitstream
Tom Finegane4099e32018-01-23 12:01:51 -08005280 if (av1_pack_bitstream(cpi, dest, size) != AOM_CODEC_OK)
5281 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005282
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07005283 cpi->seq_params_locked = 1;
5284
Hui Sudc54be62018-03-14 19:14:28 -07005285 if (skip_adapt) return AOM_CODEC_OK;
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005286
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005287 if (seq_params->frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005288 int i;
Zoe Liu630a89f2018-04-16 09:36:50 -07005289 // Update reference frame id values based on the value of refresh_frame_mask
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005290 for (i = 0; i < REF_FRAMES; i++) {
Zoe Liu630a89f2018-04-16 09:36:50 -07005291 if ((cpi->refresh_frame_mask >> i) & 1) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005292 cm->ref_frame_id[i] = cm->current_frame_id;
5293 }
5294 }
5295 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005296
Yaowu Xuc27fc142016-08-22 16:08:15 -07005297#if DUMP_RECON_FRAMES == 1
5298 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Zoe Liub4f31032017-11-03 23:48:35 -07005299 dump_filtered_recon_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005300#endif // DUMP_RECON_FRAMES
5301
Soo-Chul Han934af352017-10-15 15:21:51 -04005302 if (cm->seg.enabled) {
5303 if (cm->seg.update_map) {
5304 update_reference_segmentation_map(cpi);
Yue Chend90d3432018-03-16 11:28:42 -07005305 } else if (cm->last_frame_seg_map) {
Soo-Chul Han934af352017-10-15 15:21:51 -04005306 memcpy(cm->current_frame_seg_map, cm->last_frame_seg_map,
5307 cm->mi_cols * cm->mi_rows * sizeof(uint8_t));
5308 }
5309 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005310
5311 if (frame_is_intra_only(cm) == 0) {
5312 release_scaled_references(cpi);
5313 }
5314
Cheng Chen46f30c72017-09-07 11:13:33 -07005315 update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005316
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005317#if CONFIG_ENTROPY_STATS
Yue Chencc6a6ef2018-05-21 16:21:05 -07005318 av1_accumulate_frame_counts(&aggregate_fc, &cpi->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005319#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005320
Hui Sudc54be62018-03-14 19:14:28 -07005321 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
5322 *cm->fc = cpi->tile_data[cm->largest_tile_id].tctx;
5323 av1_reset_cdf_symbol_counters(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005324 }
5325
5326 if (cpi->refresh_golden_frame == 1)
5327 cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
5328 else
5329 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
5330
5331 if (cpi->refresh_alt_ref_frame == 1)
5332 cpi->frame_flags |= FRAMEFLAGS_ALTREF;
5333 else
5334 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
5335
Yaowu Xuc27fc142016-08-22 16:08:15 -07005336 if (cpi->refresh_bwd_ref_frame == 1)
5337 cpi->frame_flags |= FRAMEFLAGS_BWDREF;
5338 else
5339 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005340
Yaowu Xuc27fc142016-08-22 16:08:15 -07005341 cm->last_frame_type = cm->frame_type;
5342
Yaowu Xuf883b422016-08-30 14:01:10 -07005343 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005344
Yaowu Xuc27fc142016-08-22 16:08:15 -07005345 if (cm->frame_type == KEY_FRAME) {
5346 // Tell the caller that the frame was coded as a key frame
5347 *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
5348 } else {
5349 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
5350 }
5351
5352 // Clear the one shot update flags for segmentation map and mode/ref loop
5353 // filter deltas.
5354 cm->seg.update_map = 0;
5355 cm->seg.update_data = 0;
5356 cm->lf.mode_ref_delta_update = 0;
5357
Wei-Ting Linfb7dc062018-06-28 18:26:13 -07005358 // A droppable frame might not be shown but it always
5359 // takes a space in the gf group. Therefore, even when
5360 // it is not shown, we still need update the count down.
5361
Yaowu Xuc27fc142016-08-22 16:08:15 -07005362 if (cm->show_frame) {
Urvang Joshif1fa6862018-01-08 16:39:33 -08005363 // TODO(zoeliu): We may only swamp mi and prev_mi for those frames that
5364 // are
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005365 // being used as reference.
Cheng Chen46f30c72017-09-07 11:13:33 -07005366 swap_mi_and_prev_mi(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005367 // Don't increment frame counters if this was an altref buffer
5368 // update not a real frame
Wei-Ting Lin96ee0eb2018-06-22 15:27:22 -07005369
Yaowu Xuc27fc142016-08-22 16:08:15 -07005370 ++cm->current_video_frame;
5371 }
5372
Yaowu Xuc27fc142016-08-22 16:08:15 -07005373 // NOTE: Shall not refer to any frame not used as reference.
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005374 if (cm->is_reference_frame) {
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005375 // keep track of the last coded dimensions
5376 cm->last_width = cm->width;
5377 cm->last_height = cm->height;
5378
5379 // reset to normal state now that we are done.
5380 cm->last_show_frame = cm->show_frame;
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005381 }
Yi Luo10e23002017-07-31 11:54:43 -07005382
Tom Finegane4099e32018-01-23 12:01:51 -08005383 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005384}
5385
Sarah Parker3491dd22018-08-08 18:38:31 -07005386static INLINE void update_keyframe_counters(AV1_COMP *cpi) {
5387 // TODO(zoeliu): To investigate whether we should treat BWDREF_FRAME
5388 // differently here for rc->avg_frame_bandwidth.
5389 if (cpi->common.show_frame || cpi->rc.is_bwd_ref_frame) {
5390 if (!cpi->common.show_existing_frame || cpi->rc.is_src_frame_alt_ref ||
5391 cpi->common.frame_type == KEY_FRAME) {
5392 // If this is a show_existing_frame with a source other than altref,
5393 // or if it is not a displayed forward keyframe, the keyframe update
5394 // counters were incremented when it was originally encoded.
5395 cpi->rc.frames_since_key++;
5396 cpi->rc.frames_to_key--;
5397 }
5398 }
5399}
5400
5401static INLINE void update_frames_till_gf_update(AV1_COMP *cpi) {
5402 // TODO(weitinglin): Updating this counter for is_frame_droppable
5403 // is a work-around to handle the condition when a frame is drop.
5404 // We should fix the cpi->common.show_frame flag
5405 // instead of checking the other condition to update the counter properly.
5406 if (cpi->common.show_frame || is_frame_droppable(cpi)) {
5407 // Decrement count down till next gf
5408 if (cpi->rc.frames_till_gf_update_due > 0)
5409 cpi->rc.frames_till_gf_update_due--;
5410 }
5411}
5412
5413static INLINE void update_twopass_gf_group_index(AV1_COMP *cpi) {
5414 // Increment the gf group index ready for the next frame. If this is
5415 // a show_existing_frame with a source other than altref, or if it is not
5416 // a displayed forward keyframe, the index was incremented when it was
5417 // originally encoded.
5418 if (!cpi->common.show_existing_frame || cpi->rc.is_src_frame_alt_ref ||
5419 cpi->common.frame_type == KEY_FRAME) {
5420 ++cpi->twopass.gf_group.index;
5421 }
5422}
5423
5424static void update_rc_counts(AV1_COMP *cpi) {
5425 update_keyframe_counters(cpi);
5426 update_frames_till_gf_update(cpi);
5427 if (cpi->oxcf.pass == 2) update_twopass_gf_group_index(cpi);
5428}
5429
Debargha Mukherjee57378252018-09-21 18:29:37 -07005430static void set_additional_frame_flags(AV1_COMMON *const cm,
5431 unsigned int *frame_flags) {
5432 if (frame_is_intra_only(cm)) *frame_flags |= FRAMEFLAGS_INTRAONLY;
5433 if (frame_is_sframe(cm)) *frame_flags |= FRAMEFLAGS_SWITCH;
5434 if (cm->error_resilient_mode) *frame_flags |= FRAMEFLAGS_ERROR_RESILIENT;
5435}
5436
Tom Finegane4099e32018-01-23 12:01:51 -08005437static int Pass0Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
5438 int skip_adapt, unsigned int *frame_flags) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005439 if (cpi->oxcf.rc_mode == AOM_CBR) {
5440 av1_rc_get_one_pass_cbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005441 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07005442 av1_rc_get_one_pass_vbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005443 }
Debargha Mukherjeeff48c092018-04-04 23:53:40 -07005444 if (encode_frame_to_data_rate(cpi, size, dest, skip_adapt, frame_flags) !=
5445 AOM_CODEC_OK) {
5446 return AOM_CODEC_ERROR;
5447 }
Debargha Mukherjee57378252018-09-21 18:29:37 -07005448 set_additional_frame_flags(&cpi->common, frame_flags);
5449
Sarah Parker3491dd22018-08-08 18:38:31 -07005450 update_rc_counts(cpi);
Debargha Mukherjeeff48c092018-04-04 23:53:40 -07005451 check_show_existing_frame(cpi);
5452 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005453}
5454
Tom Finegane4099e32018-01-23 12:01:51 -08005455static int Pass2Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
5456 unsigned int *frame_flags) {
Angie Chiang5b5f4df2017-12-06 10:41:12 -08005457#if CONFIG_MISMATCH_DEBUG
5458 mismatch_move_frame_idx_w();
5459#endif
Angie Chiang4d55d762017-12-13 16:18:37 -08005460#if TXCOEFF_COST_TIMER
5461 AV1_COMMON *cm = &cpi->common;
5462 cm->txcoeff_cost_timer = 0;
5463 cm->txcoeff_cost_count = 0;
5464#endif
Tom Finegane4099e32018-01-23 12:01:51 -08005465
5466 if (encode_frame_to_data_rate(cpi, size, dest, 0, frame_flags) !=
5467 AOM_CODEC_OK) {
5468 return AOM_CODEC_ERROR;
5469 }
Debargha Mukherjee57378252018-09-21 18:29:37 -07005470 set_additional_frame_flags(&cpi->common, frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005471
Angie Chiang4d55d762017-12-13 16:18:37 -08005472#if TXCOEFF_COST_TIMER
5473 cm->cum_txcoeff_cost_timer += cm->txcoeff_cost_timer;
5474 fprintf(stderr,
5475 "\ntxb coeff cost block number: %ld, frame time: %ld, cum time %ld "
5476 "in us\n",
5477 cm->txcoeff_cost_count, cm->txcoeff_cost_timer,
5478 cm->cum_txcoeff_cost_timer);
5479#endif
5480
Sarah Parker3491dd22018-08-08 18:38:31 -07005481 av1_twopass_postencode_update(cpi);
5482 update_rc_counts(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005483 check_show_existing_frame(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08005484 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005485}
5486
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005487#if CONFIG_DENOISE
5488static int apply_denoise_2d(AV1_COMP *cpi, YV12_BUFFER_CONFIG *sd,
5489 int block_size, float noise_level,
5490 int64_t time_stamp, int64_t end_time) {
5491 AV1_COMMON *const cm = &cpi->common;
5492 if (!cpi->denoise_and_model) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005493 cpi->denoise_and_model = aom_denoise_and_model_alloc(
5494 cm->seq_params.bit_depth, block_size, noise_level);
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005495 if (!cpi->denoise_and_model) {
5496 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5497 "Error allocating denoise and model");
5498 return -1;
5499 }
5500 }
5501 if (!cpi->film_grain_table) {
5502 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
5503 if (!cpi->film_grain_table) {
5504 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5505 "Error allocating grain table");
5506 return -1;
5507 }
5508 memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
5509 }
5510 if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
5511 &cm->film_grain_params)) {
5512 if (cm->film_grain_params.apply_grain) {
5513 aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
5514 &cm->film_grain_params);
5515 }
5516 }
5517 return 0;
5518}
5519#endif
5520
James Zern3e2613b2017-03-30 23:14:40 -07005521int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07005522 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
5523 int64_t end_time) {
5524 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005525 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuf883b422016-08-30 14:01:10 -07005526 struct aom_usec_timer timer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005527 int res = 0;
5528 const int subsampling_x = sd->subsampling_x;
5529 const int subsampling_y = sd->subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005530 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005531
Yaowu Xuc27fc142016-08-22 16:08:15 -07005532 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005533
Yaowu Xuf883b422016-08-30 14:01:10 -07005534 aom_usec_timer_start(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005535
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005536#if CONFIG_DENOISE
5537 if (cpi->oxcf.noise_level > 0)
5538 if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
5539 cpi->oxcf.noise_level, time_stamp, end_time) < 0)
5540 res = -1;
5541#endif // CONFIG_DENOISE
5542
Yaowu Xuf883b422016-08-30 14:01:10 -07005543 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Yaowu Xud3e7c682017-12-21 14:08:25 -08005544 use_highbitdepth, frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07005545 res = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -07005546 aom_usec_timer_mark(&timer);
5547 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005548
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005549 if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005550 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005551 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005552 "Non-4:2:0 color format requires profile 1 or 2");
Yaowu Xuc27fc142016-08-22 16:08:15 -07005553 res = -1;
5554 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005555 if ((seq_params->profile == PROFILE_1) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005556 !(subsampling_x == 0 && subsampling_y == 0)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005557 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005558 "Profile 1 requires 4:4:4 color format");
5559 res = -1;
5560 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005561 if ((seq_params->profile == PROFILE_2) &&
5562 (seq_params->bit_depth <= AOM_BITS_10) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005563 !(subsampling_x == 1 && subsampling_y == 0)) {
5564 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
5565 "Profile 2 bit-depth < 10 requires 4:2:2 color format");
Yaowu Xuc27fc142016-08-22 16:08:15 -07005566 res = -1;
5567 }
5568
5569 return res;
5570}
5571
Yaowu Xuf883b422016-08-30 14:01:10 -07005572static void adjust_frame_rate(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005573 const struct lookahead_entry *source) {
5574 int64_t this_duration;
5575 int step = 0;
5576
5577 if (source->ts_start == cpi->first_time_stamp_ever) {
5578 this_duration = source->ts_end - source->ts_start;
5579 step = 1;
5580 } else {
5581 int64_t last_duration =
5582 cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
5583
5584 this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
5585
5586 // do a step update if the duration changes by 10%
5587 if (last_duration)
5588 step = (int)((this_duration - last_duration) * 10 / last_duration);
5589 }
5590
5591 if (this_duration) {
5592 if (step) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005593 av1_new_framerate(cpi, 10000000.0 / this_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005594 } else {
5595 // Average this frame's rate into the last second's average
5596 // frame rate. If we haven't seen 1 second yet, then average
5597 // over the whole interval seen.
Yaowu Xuf883b422016-08-30 14:01:10 -07005598 const double interval = AOMMIN(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005599 (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
5600 double avg_duration = 10000000.0 / cpi->framerate;
5601 avg_duration *= (interval - avg_duration + this_duration);
5602 avg_duration /= interval;
5603
Yaowu Xuf883b422016-08-30 14:01:10 -07005604 av1_new_framerate(cpi, 10000000.0 / avg_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005605 }
5606 }
5607 cpi->last_time_stamp_seen = source->ts_start;
5608 cpi->last_end_time_stamp_seen = source->ts_end;
5609}
5610
5611// Returns 0 if this is not an alt ref else the offset of the source frame
5612// used as the arf midpoint.
Yaowu Xuf883b422016-08-30 14:01:10 -07005613static int get_arf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005614 RATE_CONTROL *const rc = &cpi->rc;
5615 int arf_src_index = 0;
5616 if (is_altref_enabled(cpi)) {
5617 if (cpi->oxcf.pass == 2) {
5618 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5619 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
5620 arf_src_index = gf_group->arf_src_offset[gf_group->index];
5621 }
5622 } else if (rc->source_alt_ref_pending) {
5623 arf_src_index = rc->frames_till_gf_update_due;
5624 }
5625 }
5626 return arf_src_index;
5627}
5628
Yaowu Xuf883b422016-08-30 14:01:10 -07005629static int get_brf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005630 int brf_src_index = 0;
5631 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5632
5633 // TODO(zoeliu): We need to add the check on the -bwd_ref command line setup
5634 // flag.
5635 if (gf_group->bidir_pred_enabled[gf_group->index]) {
5636 if (cpi->oxcf.pass == 2) {
5637 if (gf_group->update_type[gf_group->index] == BRF_UPDATE)
5638 brf_src_index = gf_group->brf_src_offset[gf_group->index];
5639 } else {
5640 // TODO(zoeliu): To re-visit the setup for this scenario
5641 brf_src_index = cpi->rc.bipred_group_interval - 1;
5642 }
5643 }
5644
5645 return brf_src_index;
5646}
Zoe Liue9b15e22017-07-19 15:53:01 -07005647
Zoe Liue9b15e22017-07-19 15:53:01 -07005648// Returns 0 if this is not an alt ref else the offset of the source frame
5649// used as the arf midpoint.
5650static int get_arf2_src_index(AV1_COMP *cpi) {
5651 int arf2_src_index = 0;
5652 if (is_altref_enabled(cpi) && cpi->num_extra_arfs) {
5653 if (cpi->oxcf.pass == 2) {
5654 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5655 if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE) {
5656 arf2_src_index = gf_group->arf_src_offset[gf_group->index];
5657 }
5658 }
5659 }
5660 return arf2_src_index;
5661}
Yaowu Xuc27fc142016-08-22 16:08:15 -07005662
Yaowu Xuf883b422016-08-30 14:01:10 -07005663static void check_src_altref(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005664 const struct lookahead_entry *source) {
5665 RATE_CONTROL *const rc = &cpi->rc;
5666
5667 // If pass == 2, the parameters set here will be reset in
Yaowu Xuf883b422016-08-30 14:01:10 -07005668 // av1_rc_get_second_pass_params()
Yaowu Xuc27fc142016-08-22 16:08:15 -07005669
5670 if (cpi->oxcf.pass == 2) {
5671 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5672 rc->is_src_frame_alt_ref =
Yaowu Xuc27fc142016-08-22 16:08:15 -07005673 (gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE) ||
Yaowu Xuc27fc142016-08-22 16:08:15 -07005674 (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
Zoe Liue9b15e22017-07-19 15:53:01 -07005675 rc->is_src_frame_ext_arf =
5676 gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005677 } else {
5678 rc->is_src_frame_alt_ref =
5679 cpi->alt_ref_source && (source == cpi->alt_ref_source);
5680 }
5681
5682 if (rc->is_src_frame_alt_ref) {
5683 // Current frame is an ARF overlay frame.
5684 cpi->alt_ref_source = NULL;
5685
Zoe Liue9b15e22017-07-19 15:53:01 -07005686 if (rc->is_src_frame_ext_arf && !cpi->common.show_existing_frame) {
5687 // For INTNL_OVERLAY, when show_existing_frame == 0, they do need to
5688 // refresh the LAST_FRAME, i.e. LAST3 gets retired, LAST2 becomes LAST3,
5689 // LAST becomes LAST2, and INTNL_OVERLAY becomes LAST.
5690 cpi->refresh_last_frame = 1;
5691 } else {
Zoe Liue9b15e22017-07-19 15:53:01 -07005692 // Don't refresh the last buffer for an ARF overlay frame. It will
5693 // become the GF so preserve last as an alternative prediction option.
5694 cpi->refresh_last_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005695 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005696 }
5697}
5698
5699#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005700extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
5701 const unsigned char *img2, int img2_pitch,
5702 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005703
5704static void adjust_image_stat(double y, double u, double v, double all,
5705 ImageStat *s) {
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07005706 s->stat[STAT_Y] += y;
5707 s->stat[STAT_U] += u;
5708 s->stat[STAT_V] += v;
5709 s->stat[STAT_ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07005710 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005711}
5712
Angie Chiang08a22a62017-07-17 17:29:17 -07005713static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005714 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005715 double samples = 0.0;
5716 uint32_t in_bit_depth = 8;
5717 uint32_t bit_depth = 8;
5718
Angie Chiang08a22a62017-07-17 17:29:17 -07005719#if CONFIG_INTER_STATS_ONLY
Yaowu Xu1b4ffc42017-07-26 09:54:07 -07005720 if (cm->frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07005721#endif
5722 cpi->bytes += frame_bytes;
5723
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005724 if (cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005725 in_bit_depth = cpi->oxcf.input_bit_depth;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005726 bit_depth = cm->seq_params.bit_depth;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005727 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005728 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005729 const YV12_BUFFER_CONFIG *orig = cpi->source;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005730 const YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5731 double y, u, v, frame_all;
5732
5733 cpi->count++;
5734 if (cpi->b_calculate_psnr) {
5735 PSNR_STATS psnr;
5736 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005737 aom_clear_system_state();
Yaowu Xud3e7c682017-12-21 14:08:25 -08005738 // TODO(yaowu): unify these two versions into one.
Yaowu Xuf883b422016-08-30 14:01:10 -07005739 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005740
5741 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
5742 &cpi->psnr);
5743 cpi->total_sq_error += psnr.sse[0];
5744 cpi->total_samples += psnr.samples[0];
5745 samples = psnr.samples[0];
Yaowu Xud3e7c682017-12-21 14:08:25 -08005746 // TODO(yaowu): unify these two versions into one.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005747 if (cm->seq_params.use_highbitdepth)
Yaowu Xuc27fc142016-08-22 16:08:15 -07005748 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07005749 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005750 else
Yaowu Xuf883b422016-08-30 14:01:10 -07005751 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005752
Yaowu Xuf883b422016-08-30 14:01:10 -07005753 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005754 cpi->summed_quality += frame_ssim2 * weight;
5755 cpi->summed_weights += weight;
5756
5757#if 0
5758 {
5759 FILE *f = fopen("q_used.stt", "a");
Zoe Liuee202be2017-11-17 12:14:33 -08005760 double y2 = psnr.psnr[1];
5761 double u2 = psnr.psnr[2];
5762 double v2 = psnr.psnr[3];
5763 double frame_psnr2 = psnr.psnr[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005764 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
Zoe Liuee202be2017-11-17 12:14:33 -08005765 cm->current_video_frame, y2, u2, v2,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005766 frame_psnr2, frame_ssim2);
5767 fclose(f);
5768 }
5769#endif
5770 }
5771 if (cpi->b_calculate_blockiness) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005772 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005773 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07005774 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
5775 recon->y_stride, orig->y_width, orig->y_height);
5776 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005777 cpi->total_blockiness += frame_blockiness;
5778 }
5779
5780 if (cpi->b_calculate_consistency) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005781 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005782 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005783 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
5784 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
5785
5786 const double peak = (double)((1 << in_bit_depth) - 1);
5787 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005788 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005789 if (consistency > 0.0)
5790 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005791 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005792 cpi->total_inconsistency += this_inconsistency;
5793 }
5794 }
5795 }
5796
5797 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07005798 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005799 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07005800 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005801 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
5802 }
5803}
5804#endif // CONFIG_INTERNAL_STATS
5805
RogerZhou3b635242017-09-19 10:06:46 -07005806static int is_integer_mv(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *cur_picture,
5807 const YV12_BUFFER_CONFIG *last_picture,
5808 hash_table *last_hash_table) {
5809 aom_clear_system_state();
5810 // check use hash ME
5811 int k;
5812 uint32_t hash_value_1;
5813 uint32_t hash_value_2;
5814
5815 const int block_size = 8;
5816 const double threshold_current = 0.8;
5817 const double threshold_average = 0.95;
5818 const int max_history_size = 32;
5819 int T = 0; // total block
5820 int C = 0; // match with collocated block
5821 int S = 0; // smooth region but not match with collocated block
5822 int M = 0; // match with other block
5823
5824 const int pic_width = cur_picture->y_width;
5825 const int pic_height = cur_picture->y_height;
5826 for (int i = 0; i + block_size <= pic_height; i += block_size) {
5827 for (int j = 0; j + block_size <= pic_width; j += block_size) {
5828 const int x_pos = j;
5829 const int y_pos = i;
5830 int match = 1;
5831 T++;
5832
5833 // check whether collocated block match with current
5834 uint8_t *p_cur = cur_picture->y_buffer;
5835 uint8_t *p_ref = last_picture->y_buffer;
5836 int stride_cur = cur_picture->y_stride;
5837 int stride_ref = last_picture->y_stride;
5838 p_cur += (y_pos * stride_cur + x_pos);
5839 p_ref += (y_pos * stride_ref + x_pos);
5840
Debargha Mukherjee1c583012018-02-28 22:16:16 -08005841 if (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH) {
5842 uint16_t *p16_cur = CONVERT_TO_SHORTPTR(p_cur);
5843 uint16_t *p16_ref = CONVERT_TO_SHORTPTR(p_ref);
5844 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
5845 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
5846 if (p16_cur[tmpX] != p16_ref[tmpX]) {
5847 match = 0;
5848 }
RogerZhou3b635242017-09-19 10:06:46 -07005849 }
Debargha Mukherjee1c583012018-02-28 22:16:16 -08005850 p16_cur += stride_cur;
5851 p16_ref += stride_ref;
RogerZhou3b635242017-09-19 10:06:46 -07005852 }
Debargha Mukherjee1c583012018-02-28 22:16:16 -08005853 } else {
5854 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
5855 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
5856 if (p_cur[tmpX] != p_ref[tmpX]) {
5857 match = 0;
5858 }
5859 }
5860 p_cur += stride_cur;
5861 p_ref += stride_ref;
5862 }
RogerZhou3b635242017-09-19 10:06:46 -07005863 }
5864
5865 if (match) {
5866 C++;
5867 continue;
5868 }
5869
5870 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
5871 y_pos) ||
5872 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
5873 S++;
5874 continue;
5875 }
5876
5877 av1_get_block_hash_value(
5878 cur_picture->y_buffer + y_pos * stride_cur + x_pos, stride_cur,
Debargha Mukherjee1c583012018-02-28 22:16:16 -08005879 block_size, &hash_value_1, &hash_value_2,
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05305880 (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH), &cpi->td.mb);
Debargha Mukherjee1c583012018-02-28 22:16:16 -08005881 // Hashing does not work for highbitdepth currently.
5882 // TODO(Roger): Make it work for highbitdepth.
5883 if (av1_use_hash_me(&cpi->common)) {
5884 if (av1_has_exact_match(last_hash_table, hash_value_1, hash_value_2)) {
5885 M++;
5886 }
RogerZhou3b635242017-09-19 10:06:46 -07005887 }
5888 }
5889 }
5890
5891 assert(T > 0);
5892 double csm_rate = ((double)(C + S + M)) / ((double)(T));
5893 double m_rate = ((double)(M)) / ((double)(T));
5894
5895 cpi->csm_rate_array[cpi->rate_index] = csm_rate;
5896 cpi->m_rate_array[cpi->rate_index] = m_rate;
5897
5898 cpi->rate_index = (cpi->rate_index + 1) % max_history_size;
5899 cpi->rate_size++;
5900 cpi->rate_size = AOMMIN(cpi->rate_size, max_history_size);
5901
5902 if (csm_rate < threshold_current) {
5903 return 0;
5904 }
5905
5906 if (C == T) {
5907 return 1;
5908 }
5909
5910 double csm_average = 0.0;
5911 double m_average = 0.0;
5912
5913 for (k = 0; k < cpi->rate_size; k++) {
5914 csm_average += cpi->csm_rate_array[k];
5915 m_average += cpi->m_rate_array[k];
5916 }
5917 csm_average /= cpi->rate_size;
5918 m_average /= cpi->rate_size;
5919
5920 if (csm_average < threshold_average) {
5921 return 0;
5922 }
5923
5924 if (M > (T - C - S) / 3) {
5925 return 1;
5926 }
5927
5928 if (csm_rate > 0.99 && m_rate > 0.01) {
5929 return 1;
5930 }
5931
5932 if (csm_average + m_average > 1.01) {
5933 return 1;
5934 }
5935
5936 return 0;
5937}
RogerZhou3b635242017-09-19 10:06:46 -07005938
Yue Chen7cae98f2018-08-24 10:43:16 -07005939// Code for temporal dependency model
5940typedef struct GF_PICTURE {
5941 YV12_BUFFER_CONFIG *frame;
5942 int ref_frame[7];
5943} GF_PICTURE;
5944
Sarah Parkercf644442018-10-11 15:23:44 -07005945static void init_gop_frames(AV1_COMP *cpi, GF_PICTURE *gf_picture,
5946 const GF_GROUP *gf_group, int *tpl_group_frames) {
Yue Chen7cae98f2018-08-24 10:43:16 -07005947 AV1_COMMON *cm = &cpi->common;
5948 const SequenceHeader *const seq_params = &cm->seq_params;
5949 int frame_idx = 0;
5950 int i;
5951 int gld_index = -1;
5952 int alt_index = -1;
5953 int lst_index = -1;
5954 int extend_frame_count = 0;
5955 int pframe_qindex = cpi->tpl_stats[2].base_qindex;
5956
5957 RefCntBuffer *frame_bufs = cm->buffer_pool->frame_bufs;
5958 int recon_frame_index[INTER_REFS_PER_FRAME + 1] = { -1, -1, -1, -1,
5959 -1, -1, -1, -1 };
5960
5961 // TODO(jingning): To be used later for gf frame type parsing.
5962 (void)gf_group;
5963
5964 for (i = 0; i < FRAME_BUFFERS && frame_idx < INTER_REFS_PER_FRAME + 1; ++i) {
5965 if (frame_bufs[i].ref_count == 0) {
5966 alloc_frame_mvs(cm, i);
5967 if (aom_realloc_frame_buffer(
5968 &frame_bufs[i].buf, cm->width, cm->height,
5969 seq_params->subsampling_x, seq_params->subsampling_y,
5970 seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS,
5971 cm->byte_alignment, NULL, NULL, NULL))
5972 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5973 "Failed to allocate frame buffer");
5974
5975 recon_frame_index[frame_idx] = i;
5976 ++frame_idx;
5977 }
5978 }
5979
5980 for (i = 0; i < INTER_REFS_PER_FRAME + 1; ++i) {
5981 assert(recon_frame_index[i] >= 0);
5982 cpi->tpl_recon_frames[i] = &frame_bufs[recon_frame_index[i]].buf;
5983 }
5984
5985 *tpl_group_frames = 0;
5986
5987 // Initialize Golden reference frame.
5988 gf_picture[0].frame = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
5989 for (i = 0; i < 7; ++i) gf_picture[0].ref_frame[i] = -1;
5990 gld_index = 0;
5991 ++*tpl_group_frames;
5992
5993 // Initialize ARF frame
5994 gf_picture[1].frame = cpi->source;
5995 gf_picture[1].ref_frame[0] = gld_index;
5996 gf_picture[1].ref_frame[1] = lst_index;
5997 gf_picture[1].ref_frame[2] = alt_index;
5998 // TODO(yuec) Need o figure out full AV1 reference model
5999 for (i = 3; i < 7; ++i) gf_picture[1].ref_frame[i] = -1;
6000 alt_index = 1;
6001 ++*tpl_group_frames;
6002
6003 // Initialize P frames
6004 for (frame_idx = 2; frame_idx < MAX_LAG_BUFFERS; ++frame_idx) {
6005 struct lookahead_entry *buf =
6006 av1_lookahead_peek(cpi->lookahead, frame_idx - 2);
6007
6008 if (buf == NULL) break;
6009
6010 gf_picture[frame_idx].frame = &buf->img;
6011 gf_picture[frame_idx].ref_frame[0] = gld_index;
6012 gf_picture[frame_idx].ref_frame[1] = lst_index;
6013 gf_picture[frame_idx].ref_frame[2] = alt_index;
6014 for (i = 3; i < 7; ++i) gf_picture[frame_idx].ref_frame[i] = -1;
6015
6016 ++*tpl_group_frames;
6017 lst_index = frame_idx;
6018
6019 if (frame_idx == cpi->rc.baseline_gf_interval + 1) break;
6020 }
6021
6022 gld_index = frame_idx;
6023 lst_index = AOMMAX(0, frame_idx - 1);
6024 alt_index = -1;
6025 ++frame_idx;
6026
6027 // Extend two frames outside the current gf group.
6028 for (; frame_idx < MAX_LAG_BUFFERS && extend_frame_count < 2; ++frame_idx) {
6029 struct lookahead_entry *buf =
6030 av1_lookahead_peek(cpi->lookahead, frame_idx - 2);
6031
6032 if (buf == NULL) break;
6033
6034 cpi->tpl_stats[frame_idx].base_qindex = pframe_qindex;
6035
6036 gf_picture[frame_idx].frame = &buf->img;
6037 gf_picture[frame_idx].ref_frame[0] = gld_index;
6038 gf_picture[frame_idx].ref_frame[1] = lst_index;
6039 gf_picture[frame_idx].ref_frame[2] = alt_index;
6040 for (i = 3; i < 7; ++i) gf_picture[frame_idx].ref_frame[i] = -1;
6041 lst_index = frame_idx;
6042 ++*tpl_group_frames;
6043 ++extend_frame_count;
6044 }
6045}
6046
Sarah Parkercf644442018-10-11 15:23:44 -07006047static void init_tpl_stats(AV1_COMP *cpi) {
Yue Chen7cae98f2018-08-24 10:43:16 -07006048 int frame_idx;
6049 for (frame_idx = 0; frame_idx < MAX_LAG_BUFFERS; ++frame_idx) {
6050 TplDepFrame *tpl_frame = &cpi->tpl_stats[frame_idx];
6051 memset(tpl_frame->tpl_stats_ptr, 0,
6052 tpl_frame->height * tpl_frame->width *
6053 sizeof(*tpl_frame->tpl_stats_ptr));
6054 tpl_frame->is_valid = 0;
6055 }
6056}
6057
Sarah Parkercf644442018-10-11 15:23:44 -07006058static uint32_t motion_compensated_prediction(AV1_COMP *cpi, ThreadData *td,
6059 uint8_t *cur_frame_buf,
6060 uint8_t *ref_frame_buf,
6061 int stride, BLOCK_SIZE bsize,
6062 int mi_row, int mi_col) {
Yue Chen7cae98f2018-08-24 10:43:16 -07006063 AV1_COMMON *cm = &cpi->common;
6064 MACROBLOCK *const x = &td->mb;
6065 MACROBLOCKD *const xd = &x->e_mbd;
6066 MV_SPEED_FEATURES *const mv_sf = &cpi->sf.mv;
6067 const SEARCH_METHODS search_method = NSTEP;
6068 int step_param;
6069 int sadpb = x->sadperbit16;
6070 uint32_t bestsme = UINT_MAX;
6071 int distortion;
6072 uint32_t sse;
6073 int cost_list[5];
6074 const MvLimits tmp_mv_limits = x->mv_limits;
6075
6076 MV best_ref_mv1 = { 0, 0 };
6077 MV best_ref_mv1_full; /* full-pixel value of best_ref_mv1 */
6078
6079 best_ref_mv1_full.col = best_ref_mv1.col >> 3;
6080 best_ref_mv1_full.row = best_ref_mv1.row >> 3;
6081
6082 // Setup frame pointers
6083 x->plane[0].src.buf = cur_frame_buf;
6084 x->plane[0].src.stride = stride;
6085 xd->plane[0].pre[0].buf = ref_frame_buf;
6086 xd->plane[0].pre[0].stride = stride;
6087
6088 step_param = mv_sf->reduce_first_step_size;
6089 step_param = AOMMIN(step_param, MAX_MVSEARCH_STEPS - 2);
6090
6091 av1_set_mv_search_range(&x->mv_limits, &best_ref_mv1);
6092
6093 av1_full_pixel_search(cpi, x, bsize, &best_ref_mv1_full, step_param,
6094 search_method, 0, sadpb, cond_cost_list(cpi, cost_list),
6095 &best_ref_mv1, INT_MAX, 0, (MI_SIZE * mi_col),
6096 (MI_SIZE * mi_row), 0);
6097
6098 /* restore UMV window */
6099 x->mv_limits = tmp_mv_limits;
6100
6101 const int pw = block_size_wide[bsize];
6102 const int ph = block_size_high[bsize];
6103 bestsme = cpi->find_fractional_mv_step(
6104 x, cm, mi_row, mi_col, &best_ref_mv1, cpi->common.allow_high_precision_mv,
6105 x->errorperbit, &cpi->fn_ptr[bsize], 0, mv_sf->subpel_iters_per_step,
6106 cond_cost_list(cpi, cost_list), NULL, NULL, &distortion, &sse, NULL, NULL,
6107 0, 0, pw, ph, 1, 1);
6108
6109 return bestsme;
6110}
6111
Sarah Parkercf644442018-10-11 15:23:44 -07006112static int get_overlap_area(int grid_pos_row, int grid_pos_col, int ref_pos_row,
6113 int ref_pos_col, int block, BLOCK_SIZE bsize) {
Yue Chen7cae98f2018-08-24 10:43:16 -07006114 int width = 0, height = 0;
6115 int bw = 4 << mi_size_wide_log2[bsize];
6116 int bh = 4 << mi_size_high_log2[bsize];
6117
6118 switch (block) {
6119 case 0:
6120 width = grid_pos_col + bw - ref_pos_col;
6121 height = grid_pos_row + bh - ref_pos_row;
6122 break;
6123 case 1:
6124 width = ref_pos_col + bw - grid_pos_col;
6125 height = grid_pos_row + bh - ref_pos_row;
6126 break;
6127 case 2:
6128 width = grid_pos_col + bw - ref_pos_col;
6129 height = ref_pos_row + bh - grid_pos_row;
6130 break;
6131 case 3:
6132 width = ref_pos_col + bw - grid_pos_col;
6133 height = ref_pos_row + bh - grid_pos_row;
6134 break;
6135 default: assert(0);
6136 }
6137
6138 return width * height;
6139}
6140
Sarah Parkercf644442018-10-11 15:23:44 -07006141static int round_floor(int ref_pos, int bsize_pix) {
Yue Chen7cae98f2018-08-24 10:43:16 -07006142 int round;
6143 if (ref_pos < 0)
6144 round = -(1 + (-ref_pos - 1) / bsize_pix);
6145 else
6146 round = ref_pos / bsize_pix;
6147
6148 return round;
6149}
6150
Sarah Parkercf644442018-10-11 15:23:44 -07006151static void tpl_model_store(TplDepStats *tpl_stats, int mi_row, int mi_col,
6152 BLOCK_SIZE bsize, int stride,
6153 const TplDepStats *src_stats) {
Yue Chen7cae98f2018-08-24 10:43:16 -07006154 const int mi_height = mi_size_high[bsize];
6155 const int mi_width = mi_size_wide[bsize];
6156 int idx, idy;
6157
6158 int64_t intra_cost = src_stats->intra_cost / (mi_height * mi_width);
6159 int64_t inter_cost = src_stats->inter_cost / (mi_height * mi_width);
6160
6161 TplDepStats *tpl_ptr;
6162
6163 intra_cost = AOMMAX(1, intra_cost);
6164 inter_cost = AOMMAX(1, inter_cost);
6165
6166 for (idy = 0; idy < mi_height; ++idy) {
6167 tpl_ptr = &tpl_stats[(mi_row + idy) * stride + mi_col];
6168 for (idx = 0; idx < mi_width; ++idx) {
6169 tpl_ptr->intra_cost = intra_cost;
6170 tpl_ptr->inter_cost = inter_cost;
6171 tpl_ptr->mc_dep_cost = tpl_ptr->intra_cost + tpl_ptr->mc_flow;
6172 tpl_ptr->ref_frame_index = src_stats->ref_frame_index;
6173 tpl_ptr->mv.as_int = src_stats->mv.as_int;
6174 ++tpl_ptr;
6175 }
6176 }
6177}
6178
Sarah Parkercf644442018-10-11 15:23:44 -07006179static void tpl_model_update_b(TplDepFrame *tpl_frame, TplDepStats *tpl_stats,
6180 int mi_row, int mi_col, const BLOCK_SIZE bsize) {
Yue Chen7cae98f2018-08-24 10:43:16 -07006181 TplDepFrame *ref_tpl_frame = &tpl_frame[tpl_stats->ref_frame_index];
6182 TplDepStats *ref_stats = ref_tpl_frame->tpl_stats_ptr;
6183 MV mv = tpl_stats->mv.as_mv;
6184 int mv_row = mv.row >> 3;
6185 int mv_col = mv.col >> 3;
6186
6187 int ref_pos_row = mi_row * MI_SIZE + mv_row;
6188 int ref_pos_col = mi_col * MI_SIZE + mv_col;
6189
6190 const int bw = 4 << mi_size_wide_log2[bsize];
6191 const int bh = 4 << mi_size_high_log2[bsize];
6192 const int mi_height = mi_size_high[bsize];
6193 const int mi_width = mi_size_wide[bsize];
6194 const int pix_num = bw * bh;
6195
6196 // top-left on grid block location in pixel
6197 int grid_pos_row_base = round_floor(ref_pos_row, bh) * bh;
6198 int grid_pos_col_base = round_floor(ref_pos_col, bw) * bw;
6199 int block;
6200
6201 for (block = 0; block < 4; ++block) {
6202 int grid_pos_row = grid_pos_row_base + bh * (block >> 1);
6203 int grid_pos_col = grid_pos_col_base + bw * (block & 0x01);
6204
6205 if (grid_pos_row >= 0 && grid_pos_row < ref_tpl_frame->mi_rows * MI_SIZE &&
6206 grid_pos_col >= 0 && grid_pos_col < ref_tpl_frame->mi_cols * MI_SIZE) {
6207 int overlap_area = get_overlap_area(
6208 grid_pos_row, grid_pos_col, ref_pos_row, ref_pos_col, block, bsize);
6209 int ref_mi_row = round_floor(grid_pos_row, bh) * mi_height;
6210 int ref_mi_col = round_floor(grid_pos_col, bw) * mi_width;
6211
6212 int64_t mc_flow = tpl_stats->mc_dep_cost -
6213 (tpl_stats->mc_dep_cost * tpl_stats->inter_cost) /
6214 tpl_stats->intra_cost;
6215
6216 int idx, idy;
6217
6218 for (idy = 0; idy < mi_height; ++idy) {
6219 for (idx = 0; idx < mi_width; ++idx) {
6220 TplDepStats *des_stats =
6221 &ref_stats[(ref_mi_row + idy) * ref_tpl_frame->stride +
6222 (ref_mi_col + idx)];
6223
6224 des_stats->mc_flow += (mc_flow * overlap_area) / pix_num;
6225 des_stats->mc_ref_cost +=
6226 ((tpl_stats->intra_cost - tpl_stats->inter_cost) * overlap_area) /
6227 pix_num;
6228 assert(overlap_area >= 0);
6229 }
6230 }
6231 }
6232 }
6233}
6234
Sarah Parkercf644442018-10-11 15:23:44 -07006235static void tpl_model_update(TplDepFrame *tpl_frame, TplDepStats *tpl_stats,
6236 int mi_row, int mi_col, const BLOCK_SIZE bsize) {
Yue Chen7cae98f2018-08-24 10:43:16 -07006237 int idx, idy;
6238 const int mi_height = mi_size_high[bsize];
6239 const int mi_width = mi_size_wide[bsize];
6240
6241 for (idy = 0; idy < mi_height; ++idy) {
6242 for (idx = 0; idx < mi_width; ++idx) {
6243 TplDepStats *tpl_ptr =
6244 &tpl_stats[(mi_row + idy) * tpl_frame->stride + (mi_col + idx)];
6245 tpl_model_update_b(tpl_frame, tpl_ptr, mi_row + idy, mi_col + idx,
6246 BLOCK_4X4);
6247 }
6248 }
6249}
6250
Sarah Parkercf644442018-10-11 15:23:44 -07006251static void get_quantize_error(MACROBLOCK *x, int plane, tran_low_t *coeff,
6252 tran_low_t *qcoeff, tran_low_t *dqcoeff,
6253 TX_SIZE tx_size, int64_t *recon_error,
6254 int64_t *sse) {
Yue Chen7cae98f2018-08-24 10:43:16 -07006255 const struct macroblock_plane *const p = &x->plane[plane];
6256 const SCAN_ORDER *const scan_order = &av1_default_scan_orders[tx_size];
6257 uint16_t eob;
6258 int pix_num = 1 << num_pels_log2_lookup[txsize_to_bsize[tx_size]];
6259 const int shift = tx_size == TX_32X32 ? 0 : 2;
6260
6261 av1_quantize_fp_32x32(coeff, pix_num, p->zbin_QTX, p->round_fp_QTX,
6262 p->quant_fp_QTX, p->quant_shift_QTX, qcoeff, dqcoeff,
6263 p->dequant_QTX, &eob, scan_order->scan,
6264 scan_order->iscan);
6265
6266 *recon_error = av1_block_error(coeff, dqcoeff, pix_num, sse) >> shift;
6267 *recon_error = AOMMAX(*recon_error, 1);
6268
6269 *sse = (*sse) >> shift;
6270 *sse = AOMMAX(*sse, 1);
6271}
6272
Sarah Parkercf644442018-10-11 15:23:44 -07006273static void wht_fwd_txfm(int16_t *src_diff, int bw, tran_low_t *coeff,
6274 TX_SIZE tx_size) {
Yue Chen7cae98f2018-08-24 10:43:16 -07006275 switch (tx_size) {
6276 case TX_8X8: aom_hadamard_8x8(src_diff, bw, coeff); break;
6277 case TX_16X16: aom_hadamard_16x16(src_diff, bw, coeff); break;
6278 case TX_32X32: aom_hadamard_32x32(src_diff, bw, coeff); break;
6279 default: assert(0);
6280 }
6281}
6282
Sarah Parkercf644442018-10-11 15:23:44 -07006283static void mode_estimation(AV1_COMP *cpi, MACROBLOCK *x, MACROBLOCKD *xd,
6284 struct scale_factors *sf, GF_PICTURE *gf_picture,
6285 int frame_idx, int16_t *src_diff, tran_low_t *coeff,
6286 tran_low_t *qcoeff, tran_low_t *dqcoeff, int mi_row,
6287 int mi_col, BLOCK_SIZE bsize, TX_SIZE tx_size,
6288 YV12_BUFFER_CONFIG *ref_frame[], uint8_t *predictor,
6289 int64_t *recon_error, int64_t *sse,
6290 TplDepStats *tpl_stats) {
Yue Chen7cae98f2018-08-24 10:43:16 -07006291 AV1_COMMON *cm = &cpi->common;
6292 ThreadData *td = &cpi->td;
6293
6294 const int bw = 4 << mi_size_wide_log2[bsize];
6295 const int bh = 4 << mi_size_high_log2[bsize];
6296 const int pix_num = bw * bh;
6297 int best_rf_idx = -1;
6298 int_mv best_mv;
6299 int64_t best_inter_cost = INT64_MAX;
6300 int64_t inter_cost;
6301 int rf_idx;
6302 const InterpFilters kernel =
6303 av1_make_interp_filters(EIGHTTAP_REGULAR, EIGHTTAP_REGULAR);
6304
6305 int64_t best_intra_cost = INT64_MAX;
6306 int64_t intra_cost;
6307 PREDICTION_MODE mode;
6308 int mb_y_offset = mi_row * MI_SIZE * xd->cur_buf->y_stride + mi_col * MI_SIZE;
6309 MB_MODE_INFO mi_above, mi_left;
6310
6311 memset(tpl_stats, 0, sizeof(*tpl_stats));
6312
6313 xd->mb_to_top_edge = -((mi_row * MI_SIZE) * 8);
6314 xd->mb_to_bottom_edge = ((cm->mi_rows - 1 - mi_row) * MI_SIZE) * 8;
6315 xd->mb_to_left_edge = -((mi_col * MI_SIZE) * 8);
6316 xd->mb_to_right_edge = ((cm->mi_cols - 1 - mi_col) * MI_SIZE) * 8;
6317 xd->above_mbmi = (mi_row > 0) ? &mi_above : NULL;
6318 xd->left_mbmi = (mi_col > 0) ? &mi_left : NULL;
6319
6320 // Intra prediction search
6321 for (mode = DC_PRED; mode <= PAETH_PRED; ++mode) {
6322 uint8_t *src, *dst;
6323 int src_stride, dst_stride;
6324
6325 src = xd->cur_buf->y_buffer + mb_y_offset;
6326 src_stride = xd->cur_buf->y_stride;
6327
6328 dst = &predictor[0];
6329 dst_stride = bw;
6330
6331 xd->mi[0]->sb_type = bsize;
6332 xd->mi[0]->ref_frame[0] = INTRA_FRAME;
6333
6334 av1_predict_intra_block(
6335 cm, xd, block_size_wide[bsize], block_size_high[bsize], tx_size, mode,
6336 0, 0, FILTER_INTRA_MODES, src, src_stride, dst, dst_stride, 0, 0, 0);
6337
6338 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
6339 aom_highbd_subtract_block(bh, bw, src_diff, bw, src, src_stride, dst,
6340 dst_stride, xd->bd);
6341 } else {
6342 aom_subtract_block(bh, bw, src_diff, bw, src, src_stride, dst,
6343 dst_stride);
6344 }
6345
6346 wht_fwd_txfm(src_diff, bw, coeff, tx_size);
6347
6348 intra_cost = aom_satd(coeff, pix_num);
6349
6350 if (intra_cost < best_intra_cost) best_intra_cost = intra_cost;
6351 }
6352
6353 // Motion compensated prediction
6354 best_mv.as_int = 0;
6355
6356 (void)mb_y_offset;
6357 // Motion estimation column boundary
6358 x->mv_limits.col_min = -((mi_col * MI_SIZE) + (17 - 2 * AOM_INTERP_EXTEND));
6359 x->mv_limits.col_max =
6360 ((cm->mi_cols - 1 - mi_col) * MI_SIZE) + (17 - 2 * AOM_INTERP_EXTEND);
6361
6362 for (rf_idx = 0; rf_idx < 7; ++rf_idx) {
6363 if (ref_frame[rf_idx] == NULL) continue;
6364
6365 motion_compensated_prediction(cpi, td, xd->cur_buf->y_buffer + mb_y_offset,
6366 ref_frame[rf_idx]->y_buffer + mb_y_offset,
6367 xd->cur_buf->y_stride, bsize, mi_row, mi_col);
6368
6369 // TODO(jingning): Not yet support high bit-depth in the next three
6370 // steps.
6371 ConvolveParams conv_params = get_conv_params(0, 0, xd->bd);
6372 WarpTypesAllowed warp_types;
6373 memset(&warp_types, 0, sizeof(WarpTypesAllowed));
6374
6375 av1_build_inter_predictor(
6376 ref_frame[rf_idx]->y_buffer + mb_y_offset, ref_frame[rf_idx]->y_stride,
6377 &predictor[0], bw, &x->best_mv.as_mv, sf, bw, bh, &conv_params, kernel,
6378 &warp_types, mi_col * MI_SIZE, mi_row * MI_SIZE, 0, 0, MV_PRECISION_Q3,
6379 mi_col * MI_SIZE, mi_row * MI_SIZE, xd, 0);
6380 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
6381 aom_highbd_subtract_block(
6382 bh, bw, src_diff, bw, xd->cur_buf->y_buffer + mb_y_offset,
6383 xd->cur_buf->y_stride, &predictor[0], bw, xd->bd);
6384 } else {
6385 aom_subtract_block(bh, bw, src_diff, bw,
6386 xd->cur_buf->y_buffer + mb_y_offset,
6387 xd->cur_buf->y_stride, &predictor[0], bw);
6388 }
6389 wht_fwd_txfm(src_diff, bw, coeff, tx_size);
6390
6391 inter_cost = aom_satd(coeff, pix_num);
6392 if (inter_cost < best_inter_cost) {
6393 best_rf_idx = rf_idx;
6394 best_inter_cost = inter_cost;
6395 best_mv.as_int = x->best_mv.as_int;
6396 get_quantize_error(x, 0, coeff, qcoeff, dqcoeff, tx_size, recon_error,
6397 sse);
6398 }
6399 }
6400 best_intra_cost = AOMMAX(best_intra_cost, 1);
6401 best_inter_cost = AOMMIN(best_intra_cost, best_inter_cost);
6402 tpl_stats->inter_cost = best_inter_cost << TPL_DEP_COST_SCALE_LOG2;
6403 tpl_stats->intra_cost = best_intra_cost << TPL_DEP_COST_SCALE_LOG2;
6404 tpl_stats->mc_dep_cost = tpl_stats->intra_cost + tpl_stats->mc_flow;
6405
6406 tpl_stats->ref_frame_index = gf_picture[frame_idx].ref_frame[best_rf_idx];
6407 tpl_stats->mv.as_int = best_mv.as_int;
6408}
6409
Sarah Parkercf644442018-10-11 15:23:44 -07006410static void mc_flow_dispenser(AV1_COMP *cpi, GF_PICTURE *gf_picture,
6411 int frame_idx) {
Yue Chen7cae98f2018-08-24 10:43:16 -07006412 TplDepFrame *tpl_frame = &cpi->tpl_stats[frame_idx];
6413 YV12_BUFFER_CONFIG *this_frame = gf_picture[frame_idx].frame;
6414 YV12_BUFFER_CONFIG *ref_frame[7] = {
6415 NULL, NULL, NULL, NULL, NULL, NULL, NULL
6416 };
6417
6418 AV1_COMMON *cm = &cpi->common;
6419 struct scale_factors sf;
6420 int rdmult, idx;
6421 ThreadData *td = &cpi->td;
6422 MACROBLOCK *x = &td->mb;
6423 MACROBLOCKD *xd = &x->e_mbd;
6424 int mi_row, mi_col;
6425
6426 DECLARE_ALIGNED(16, uint16_t, predictor16[32 * 32 * 3]);
6427 DECLARE_ALIGNED(16, uint8_t, predictor8[32 * 32 * 3]);
6428 uint8_t *predictor;
6429 DECLARE_ALIGNED(16, int16_t, src_diff[32 * 32]);
6430 DECLARE_ALIGNED(16, tran_low_t, coeff[32 * 32]);
6431 DECLARE_ALIGNED(16, tran_low_t, qcoeff[32 * 32]);
6432 DECLARE_ALIGNED(16, tran_low_t, dqcoeff[32 * 32]);
6433
6434 const BLOCK_SIZE bsize = BLOCK_32X32;
6435 const TX_SIZE tx_size = max_txsize_lookup[bsize];
6436 const int mi_height = mi_size_high[bsize];
6437 const int mi_width = mi_size_wide[bsize];
6438 int64_t recon_error, sse;
6439
6440 // Setup scaling factor
6441 av1_setup_scale_factors_for_frame(
6442 &sf, this_frame->y_crop_width, this_frame->y_crop_height,
6443 this_frame->y_crop_width, this_frame->y_crop_height);
6444
6445 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
6446 predictor = CONVERT_TO_BYTEPTR(predictor16);
6447 else
6448 predictor = predictor8;
6449
6450 // Prepare reference frame pointers. If any reference frame slot is
6451 // unavailable, the pointer will be set to Null.
6452 for (idx = 0; idx < 7; ++idx) {
6453 int rf_idx = gf_picture[frame_idx].ref_frame[idx];
6454 if (rf_idx != -1) ref_frame[idx] = gf_picture[rf_idx].frame;
6455 }
6456
6457 xd->mi = cm->mi_grid_visible;
6458 xd->mi[0] = cm->mi;
6459 xd->cur_buf = this_frame;
6460
6461 // Get rd multiplier set up.
6462 rdmult = (int)av1_compute_rd_mult(cpi, tpl_frame->base_qindex);
6463 if (rdmult < 1) rdmult = 1;
6464 set_error_per_bit(&cpi->td.mb, rdmult);
6465 av1_initialize_me_consts(cpi, &cpi->td.mb, tpl_frame->base_qindex);
6466
6467 tpl_frame->is_valid = 1;
6468
6469 cm->base_qindex = tpl_frame->base_qindex;
6470 av1_frame_init_quantizer(cpi);
6471
6472 for (mi_row = 0; mi_row < cm->mi_rows; mi_row += mi_height) {
6473 // Motion estimation row boundary
6474 x->mv_limits.row_min = -((mi_row * MI_SIZE) + (17 - 2 * AOM_INTERP_EXTEND));
6475 x->mv_limits.row_max =
6476 (cm->mi_rows - 1 - mi_row) * MI_SIZE + (17 - 2 * AOM_INTERP_EXTEND);
6477 for (mi_col = 0; mi_col < cm->mi_cols; mi_col += mi_width) {
6478 TplDepStats tpl_stats;
6479 mode_estimation(cpi, x, xd, &sf, gf_picture, frame_idx, src_diff, coeff,
6480 qcoeff, dqcoeff, mi_row, mi_col, bsize, tx_size,
6481 ref_frame, predictor, &recon_error, &sse, &tpl_stats);
6482
6483 // Motion flow dependency dispenser.
6484 tpl_model_store(tpl_frame->tpl_stats_ptr, mi_row, mi_col, bsize,
6485 tpl_frame->stride, &tpl_stats);
6486
6487 tpl_model_update(cpi->tpl_stats, tpl_frame->tpl_stats_ptr, mi_row, mi_col,
6488 bsize);
6489 }
6490 }
6491}
6492
6493static void setup_tpl_stats(AV1_COMP *cpi) {
6494 GF_PICTURE gf_picture[MAX_LAG_BUFFERS];
6495 const GF_GROUP *gf_group = &cpi->twopass.gf_group;
6496 int tpl_group_frames = 0;
6497 int frame_idx;
6498
6499 init_gop_frames(cpi, gf_picture, gf_group, &tpl_group_frames);
6500
6501 init_tpl_stats(cpi);
6502
6503 // Backward propagation from tpl_group_frames to 1.
6504 for (frame_idx = tpl_group_frames - 1; frame_idx > 0; --frame_idx)
6505 mc_flow_dispenser(cpi, gf_picture, frame_idx);
6506}
6507
Andrey Norkin795ba872018-03-06 13:24:14 -08006508int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
6509 size_t *size, uint8_t *dest, int64_t *time_stamp,
6510 int64_t *time_end, int flush,
6511 const aom_rational_t *timebase) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006512 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
6513 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00006514 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006515 BufferPool *const pool = cm->buffer_pool;
6516 RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07006517 struct aom_usec_timer cmptimer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006518 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
6519 struct lookahead_entry *last_source = NULL;
6520 struct lookahead_entry *source = NULL;
6521 int arf_src_index;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006522 int brf_src_index;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006523 int i;
6524
6525#if CONFIG_BITSTREAM_DEBUG
6526 assert(cpi->oxcf.max_threads == 0 &&
6527 "bitstream debug tool does not support multithreading");
6528 bitstream_queue_record_write();
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07006529 bitstream_queue_set_frame_write(cm->current_video_frame * 2 + cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006530#endif
6531
Dominic Symesd4929012018-01-31 17:32:01 +01006532 cm->showable_frame = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07006533 aom_usec_timer_start(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006534
RogerZhou3b635242017-09-19 10:06:46 -07006535 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006536
Debargha Mukherjeeba7b8fe2018-03-15 23:10:07 -07006537 // Normal defaults
sarahparker27d686a2018-03-30 17:43:44 -07006538 cm->refresh_frame_context = oxcf->frame_parallel_decoding_mode
6539 ? REFRESH_FRAME_CONTEXT_DISABLED
6540 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01006541 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08006542 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006543
Wei-Ting Lin2e8d0452018-06-27 09:32:39 -07006544 // default reference buffers update config
6545 av1_configure_buffer_updates_firstpass(cpi, LF_UPDATE);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006546
Sarah Parkerb9041612018-05-22 19:06:47 -07006547 // Initialize fields related to forward keyframes
Sarah Parkeraf32a7b2018-06-29 14:59:05 -07006548 cpi->no_show_kf = 0;
Zoe Liub4991202017-12-21 15:31:06 -08006549 cm->reset_decoder_state = 0;
Zoe Liub4991202017-12-21 15:31:06 -08006550
Sarah Parker7e13da82018-04-30 17:47:24 -07006551 // Don't allow a show_existing_frame to coincide with an error resilient or
Sarah Parker14feea42018-07-06 16:41:41 -07006552 // S-Frame. An exception can be made in the case of a keyframe, since it
6553 // does not depend on any previous frames. We must make this exception here
6554 // because of the use of show_existing_frame with forward coded keyframes.
Sarah Parker7e13da82018-04-30 17:47:24 -07006555 struct lookahead_entry *lookahead_src = NULL;
6556 if (cm->current_video_frame > 0)
6557 lookahead_src = av1_lookahead_peek(cpi->lookahead, 0);
Sarah Parker349e0bd2018-08-03 14:18:05 -07006558
6559 int use_show_existing = 1;
6560 if (lookahead_src != NULL) {
6561 const int is_error_resilient =
6562 cpi->oxcf.error_resilient_mode ||
6563 (lookahead_src->flags & AOM_EFLAG_ERROR_RESILIENT);
6564 const int is_s_frame = cpi->oxcf.s_frame_mode ||
6565 (lookahead_src->flags & AOM_EFLAG_SET_S_FRAME);
6566 const int is_key_frame =
6567 (rc->frames_to_key == 0) || (cpi->frame_flags & FRAMEFLAGS_KEY);
6568 use_show_existing = !(is_error_resilient || is_s_frame) || is_key_frame;
6569 }
Sarah Parker7e13da82018-04-30 17:47:24 -07006570
Sarah Parker33005522018-07-27 14:46:25 -07006571 if (oxcf->pass == 2 && cm->show_existing_frame && use_show_existing) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006572 // Manage the source buffer and flush out the source frame that has been
6573 // coded already; Also get prepared for PSNR calculation if needed.
Yaowu Xuf883b422016-08-30 14:01:10 -07006574 if ((source = av1_lookahead_pop(cpi->lookahead, flush)) == NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006575 *size = 0;
6576 return -1;
6577 }
sarahparker21dbca42018-03-30 17:43:44 -07006578 av1_apply_encoding_flags(cpi, source->flags);
Alex Conversef77fd0b2017-04-20 11:00:24 -07006579 cpi->source = &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006580 // TODO(zoeliu): To track down to determine whether it's needed to adjust
6581 // the frame rate.
6582 *time_stamp = source->ts_start;
6583 *time_end = source->ts_end;
6584
6585 // We need to adjust frame rate for an overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07006586 if (cpi->rc.is_src_frame_alt_ref) adjust_frame_rate(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006587
Urvang Joshif1fa6862018-01-08 16:39:33 -08006588 // Find a free buffer for the new frame, releasing the reference
Wan-Teh Changd05e0332018-10-03 12:00:43 -07006589 // previously held.
Yaowu Xuc27fc142016-08-22 16:08:15 -07006590 if (cm->new_fb_idx != INVALID_IDX) {
6591 --pool->frame_bufs[cm->new_fb_idx].ref_count;
6592 }
6593 cm->new_fb_idx = get_free_fb(cm);
6594
Wan-Teh Chang4a8c0042018-10-05 09:41:52 -07006595 if (cm->new_fb_idx == INVALID_IDX) return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006596
6597 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07006598 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006599
6600 // Start with a 0 size frame.
6601 *size = 0;
6602
6603 // We need to update the gf_group for show_existing overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07006604 if (cpi->rc.is_src_frame_alt_ref) av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006605
Tom Finegane4099e32018-01-23 12:01:51 -08006606 if (Pass2Encode(cpi, size, dest, frame_flags) != AOM_CODEC_OK)
6607 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006608
6609 if (cpi->b_calculate_psnr) generate_psnr_packet(cpi);
6610
6611#if CONFIG_INTERNAL_STATS
Angie Chiang08a22a62017-07-17 17:29:17 -07006612 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07006613#endif // CONFIG_INTERNAL_STATS
6614
6615 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07006616 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006617
6618 cm->show_existing_frame = 0;
6619 return 0;
6620 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006621
6622 // Should we encode an arf frame.
6623 arf_src_index = get_arf_src_index(cpi);
6624 if (arf_src_index) {
6625 for (i = 0; i <= arf_src_index; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006626 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006627 // Avoid creating an alt-ref if there's a forced keyframe pending.
6628 if (e == NULL) {
6629 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07006630 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006631 arf_src_index = 0;
6632 flush = 1;
6633 break;
6634 }
6635 }
6636 }
6637
6638 if (arf_src_index) {
6639 assert(arf_src_index <= rc->frames_to_key);
6640
Yaowu Xuf883b422016-08-30 14:01:10 -07006641 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
Dominic Symesd4929012018-01-31 17:32:01 +01006642 cm->showable_frame = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006643 cpi->alt_ref_source = source;
Sarah Parkeraf32a7b2018-06-29 14:59:05 -07006644 // When arf_src_index == rc->frames_to_key, it indicates a fwd_kf
Sarah Parkerb9041612018-05-22 19:06:47 -07006645 if (arf_src_index == rc->frames_to_key) {
6646 // Skip temporal filtering and mark as intra_only if we have a fwd_kf
6647 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
6648 int which_arf = gf_group->arf_update_idx[gf_group->index];
6649 cpi->is_arf_filter_off[which_arf] = 1;
Sarah Parkeraf32a7b2018-06-29 14:59:05 -07006650 cpi->no_show_kf = 1;
Sarah Parkerb9041612018-05-22 19:06:47 -07006651 } else {
6652 if (oxcf->arnr_max_frames > 0) {
6653 // Produce the filtered ARF frame.
6654 av1_temporal_filter(cpi, arf_src_index);
6655 aom_extend_frame_borders(&cpi->alt_ref_buffer, num_planes);
6656 force_src_buffer = &cpi->alt_ref_buffer;
6657 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006658 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006659 cm->show_frame = 0;
6660 cm->intra_only = 0;
Wei-Ting Lin2e8d0452018-06-27 09:32:39 -07006661
6662 if (oxcf->pass < 2) {
6663 // In second pass, the buffer updates configure will be set
6664 // in the function av1_rc_get_second_pass_params
6665 av1_configure_buffer_updates_firstpass(cpi, ARF_UPDATE);
6666 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006667 }
6668 rc->source_alt_ref_pending = 0;
6669 }
6670
Zoe Liue9b15e22017-07-19 15:53:01 -07006671 // Should we encode an arf2 frame.
6672 arf_src_index = get_arf2_src_index(cpi);
6673 if (arf_src_index) {
6674 for (i = 0; i <= arf_src_index; ++i) {
6675 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
6676 // Avoid creating an alt-ref if there's a forced keyframe pending.
6677 if (e == NULL) {
6678 break;
6679 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
6680 arf_src_index = 0;
6681 flush = 1;
6682 break;
6683 }
6684 }
6685 }
6686
6687 if (arf_src_index) {
6688 assert(arf_src_index <= rc->frames_to_key);
6689
6690 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
Dominic Symesd4929012018-01-31 17:32:01 +01006691 cm->showable_frame = 1;
Zoe Liue9b15e22017-07-19 15:53:01 -07006692 cpi->alt_ref_source = source;
6693
6694 if (oxcf->arnr_max_frames > 0) {
6695 // Produce the filtered ARF frame.
Sebastien Alaiwan6697acf2018-02-21 16:59:17 +01006696 av1_temporal_filter(cpi, arf_src_index);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00006697 aom_extend_frame_borders(&cpi->alt_ref_buffer, num_planes);
Zoe Liue9b15e22017-07-19 15:53:01 -07006698 force_src_buffer = &cpi->alt_ref_buffer;
6699 }
6700
6701 cm->show_frame = 0;
6702 cm->intra_only = 0;
Wei-Ting Lin2e8d0452018-06-27 09:32:39 -07006703
6704 if (oxcf->pass < 2) {
6705 // In second pass, the buffer updates configure will be set
6706 // in the function av1_rc_get_second_pass_params
6707 av1_configure_buffer_updates_firstpass(cpi, INTNL_ARF_UPDATE);
6708 }
Zoe Liue9b15e22017-07-19 15:53:01 -07006709 }
6710 rc->source_alt_ref_pending = 0;
6711 }
Zoe Liue9b15e22017-07-19 15:53:01 -07006712
Yaowu Xuc27fc142016-08-22 16:08:15 -07006713 rc->is_bwd_ref_frame = 0;
6714 brf_src_index = get_brf_src_index(cpi);
6715 if (brf_src_index) {
6716 assert(brf_src_index <= rc->frames_to_key);
Yaowu Xuf883b422016-08-30 14:01:10 -07006717 if ((source = av1_lookahead_peek(cpi->lookahead, brf_src_index)) != NULL) {
Dominic Symesd4929012018-01-31 17:32:01 +01006718 cm->showable_frame = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006719 cm->show_frame = 0;
6720 cm->intra_only = 0;
6721
Wei-Ting Lin2e8d0452018-06-27 09:32:39 -07006722 if (oxcf->pass < 2) {
6723 // In second pass, the buffer updates configure will be set
6724 // in the function av1_rc_get_second_pass_params
6725 av1_configure_buffer_updates_firstpass(cpi, BIPRED_UPDATE);
6726 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006727 }
6728 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006729
6730 if (!source) {
6731 // Get last frame source.
6732 if (cm->current_video_frame > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006733 if ((last_source = av1_lookahead_peek(cpi->lookahead, -1)) == NULL)
Yaowu Xuc27fc142016-08-22 16:08:15 -07006734 return -1;
6735 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07006736 if (cm->current_video_frame > 0) assert(last_source != NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006737 // Read in the source frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07006738 source = av1_lookahead_pop(cpi->lookahead, flush);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006739
6740 if (source != NULL) {
6741 cm->show_frame = 1;
6742 cm->intra_only = 0;
6743
6744 // Check to see if the frame should be encoded as an arf overlay.
6745 check_src_altref(cpi, source);
6746 }
6747 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006748 if (source) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07006749 cpi->unscaled_source = cpi->source =
Yaowu Xuc27fc142016-08-22 16:08:15 -07006750 force_src_buffer ? force_src_buffer : &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006751 cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
6752
6753 *time_stamp = source->ts_start;
6754 *time_end = source->ts_end;
Sarah Parker73556772018-03-28 18:28:05 -07006755 av1_apply_encoding_flags(cpi, source->flags);
Yaowu Xuf883b422016-08-30 14:01:10 -07006756 *frame_flags = (source->flags & AOM_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006757
6758 } else {
6759 *size = 0;
6760 if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006761 av1_end_first_pass(cpi); /* get last stats packet */
Yaowu Xuc27fc142016-08-22 16:08:15 -07006762 cpi->twopass.first_pass_done = 1;
6763 }
6764 return -1;
6765 }
6766
6767 if (source->ts_start < cpi->first_time_stamp_ever) {
6768 cpi->first_time_stamp_ever = source->ts_start;
6769 cpi->last_end_time_stamp_seen = source->ts_start;
6770 }
6771
6772 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07006773 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006774
6775 // adjust frame rates based on timestamps given
6776 if (cm->show_frame) adjust_frame_rate(cpi, source);
6777
6778 // Find a free buffer for the new frame, releasing the reference previously
6779 // held.
6780 if (cm->new_fb_idx != INVALID_IDX) {
6781 --pool->frame_bufs[cm->new_fb_idx].ref_count;
6782 }
6783 cm->new_fb_idx = get_free_fb(cm);
6784
Wan-Teh Chang4a8c0042018-10-05 09:41:52 -07006785 if (cm->new_fb_idx == INVALID_IDX) return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006786
Zoe Liuf452fdf2017-11-02 23:08:12 -07006787 // Retain the RF_LEVEL for the current newly coded frame.
6788 cpi->frame_rf_level[cm->new_fb_idx] =
6789 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
Zoe Liuf452fdf2017-11-02 23:08:12 -07006790
Yaowu Xuc27fc142016-08-22 16:08:15 -07006791 cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
Yaowu Xu9b0f7032017-07-31 11:01:19 -07006792 cm->cur_frame->buf.buf_8bit_valid = 0;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -07006793
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006794 if (cpi->film_grain_table) {
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -07006795 cm->seq_params.film_grain_params_present = aom_film_grain_table_lookup(
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006796 cpi->film_grain_table, *time_stamp, *time_end, 0 /* =erase */,
Neil Birkbeckeb895ef2018-03-14 17:51:03 -07006797 &cm->film_grain_params);
6798 }
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -07006799 cm->cur_frame->film_grain_params_present =
6800 cm->seq_params.film_grain_params_present;
Zoe Liu6cfaff92016-10-18 17:12:11 -07006801
Andrey Norkin795ba872018-03-06 13:24:14 -08006802 // only one operating point supported now
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07006803 const int64_t pts64 = ticks_to_timebase_units(timebase, *time_stamp);
6804 if (pts64 < 0 || pts64 > UINT32_MAX) return AOM_CODEC_ERROR;
6805 cpi->common.frame_presentation_time = (uint32_t)pts64;
Andrey Norkin795ba872018-03-06 13:24:14 -08006806
Yaowu Xuc27fc142016-08-22 16:08:15 -07006807 // Start with a 0 size frame.
6808 *size = 0;
6809
6810 cpi->frame_flags = *frame_flags;
6811
6812 if (oxcf->pass == 2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006813 av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006814 } else if (oxcf->pass == 1) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07006815 setup_frame_size(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006816 }
6817
6818 if (cpi->oxcf.pass != 0 || frame_is_intra_only(cm) == 1) {
Wan-Teh Changbb6a3d22018-10-05 11:22:48 -07006819 for (i = 0; i < INTER_REFS_PER_FRAME; ++i)
6820 cpi->scaled_ref_idx[i] = INVALID_IDX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006821 }
6822
Yaowu Xuc27fc142016-08-22 16:08:15 -07006823 cm->using_qmatrix = cpi->oxcf.using_qm;
6824 cm->min_qmlevel = cpi->oxcf.qm_minlevel;
6825 cm->max_qmlevel = cpi->oxcf.qm_maxlevel;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006826
David Barker5e70a112017-10-03 14:28:17 +01006827 if (cm->seq_params.frame_id_numbers_present_flag) {
Debargha Mukherjee778023d2017-09-26 17:50:27 -07006828 if (*time_stamp == 0) {
6829 cpi->common.current_frame_id = -1;
6830 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006831 }
Zoe Liuca0cd3f2018-02-26 15:07:50 -08006832
RogerZhou3b635242017-09-19 10:06:46 -07006833 cpi->cur_poc++;
Debargha Mukherjeee41a6672018-02-27 11:56:31 -08006834 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools &&
6835 !frame_is_intra_only(cm)) {
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00006836 if (cpi->common.seq_params.force_integer_mv == 2) {
RogerZhou3b635242017-09-19 10:06:46 -07006837 struct lookahead_entry *previous_entry =
Debargha Mukherjeea71e3db2018-02-28 07:47:17 -08006838 av1_lookahead_peek(cpi->lookahead, cpi->previous_index);
6839 if (!previous_entry)
6840 cpi->common.cur_frame_force_integer_mv = 0;
6841 else
6842 cpi->common.cur_frame_force_integer_mv = is_integer_mv(
6843 cpi, cpi->source, &previous_entry->img, cpi->previous_hash_table);
RogerZhou3b635242017-09-19 10:06:46 -07006844 } else {
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00006845 cpi->common.cur_frame_force_integer_mv =
6846 cpi->common.seq_params.force_integer_mv;
RogerZhou3b635242017-09-19 10:06:46 -07006847 }
6848 } else {
RogerZhou10a03802017-10-26 11:49:48 -07006849 cpi->common.cur_frame_force_integer_mv = 0;
RogerZhou3b635242017-09-19 10:06:46 -07006850 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006851
Yue Chen7cae98f2018-08-24 10:43:16 -07006852 if (cpi->twopass.gf_group.index == 1 && cpi->oxcf.enable_tpl_model) {
6853 set_frame_size(cpi, cm->width, cm->height);
6854 setup_tpl_stats(cpi);
6855 }
6856
Yaowu Xuc27fc142016-08-22 16:08:15 -07006857 if (oxcf->pass == 1) {
6858 cpi->td.mb.e_mbd.lossless[0] = is_lossless_requested(oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07006859 av1_first_pass(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006860 } else if (oxcf->pass == 2) {
Tom Finegane4099e32018-01-23 12:01:51 -08006861 if (Pass2Encode(cpi, size, dest, frame_flags) != AOM_CODEC_OK)
6862 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006863 } else {
6864 // One pass encode
Tom Finegane4099e32018-01-23 12:01:51 -08006865 if (Pass0Encode(cpi, size, dest, 0, frame_flags) != AOM_CODEC_OK)
6866 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006867 }
RogerZhoucc5d35d2017-08-07 22:20:15 -07006868 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools) {
Debargha Mukherjeee41a6672018-02-27 11:56:31 -08006869 cpi->previous_hash_table = &cm->cur_frame->hash_table;
RogerZhou3b635242017-09-19 10:06:46 -07006870 {
6871 int l;
6872 for (l = -MAX_PRE_FRAMES; l < cpi->lookahead->max_sz; l++) {
6873 if ((cpi->lookahead->buf + l) == source) {
Debargha Mukherjeee41a6672018-02-27 11:56:31 -08006874 cpi->previous_index = l;
RogerZhou3b635242017-09-19 10:06:46 -07006875 break;
6876 }
6877 }
6878
6879 if (l == cpi->lookahead->max_sz) {
6880 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
6881 "Failed to find last frame original buffer");
6882 }
6883 }
RogerZhoucc5d35d2017-08-07 22:20:15 -07006884 }
6885
Yunqing Wang267e3272017-11-09 14:23:22 -08006886 if (!cm->large_scale_tile) {
Yunqing Wang267e3272017-11-09 14:23:22 -08006887 cm->frame_contexts[cm->new_fb_idx] = *cm->fc;
Yunqing Wang267e3272017-11-09 14:23:22 -08006888 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006889
Yunqing Wangb041d8a2017-11-15 12:31:18 -08006890#define EXT_TILE_DEBUG 0
6891#if EXT_TILE_DEBUG
6892 if (cm->large_scale_tile && oxcf->pass == 2) {
6893 char fn[20] = "./fc";
6894 fn[4] = cm->current_video_frame / 100 + '0';
6895 fn[5] = (cm->current_video_frame % 100) / 10 + '0';
6896 fn[6] = (cm->current_video_frame % 10) + '0';
6897 fn[7] = '\0';
6898 av1_print_frame_contexts(cm->fc, fn);
6899 }
6900#endif // EXT_TILE_DEBUG
6901#undef EXT_TILE_DEBUG
Yaowu Xuc7119a72018-03-29 09:59:37 -07006902
Dominic Symesd4929012018-01-31 17:32:01 +01006903 cm->showable_frame = !cm->show_frame && cm->showable_frame;
Yunqing Wangb041d8a2017-11-15 12:31:18 -08006904
Yaowu Xuc27fc142016-08-22 16:08:15 -07006905 // No frame encoded, or frame was dropped, release scaled references.
6906 if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
6907 release_scaled_references(cpi);
6908 }
6909
6910 if (*size > 0) {
Debargha Mukherjee8adee102018-09-25 11:01:00 -07006911 cpi->droppable = is_frame_droppable(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006912 }
6913
Yaowu Xuf883b422016-08-30 14:01:10 -07006914 aom_usec_timer_mark(&cmptimer);
6915 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006916
6917 if (cpi->b_calculate_psnr && oxcf->pass != 1 && cm->show_frame)
6918 generate_psnr_packet(cpi);
6919
6920#if CONFIG_INTERNAL_STATS
6921 if (oxcf->pass != 1) {
Angie Chiang08a22a62017-07-17 17:29:17 -07006922 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07006923 }
6924#endif // CONFIG_INTERNAL_STATS
6925
Yaowu Xuf883b422016-08-30 14:01:10 -07006926 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006927
6928 return 0;
6929}
6930
Yaowu Xuf883b422016-08-30 14:01:10 -07006931int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
6932 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006933 if (!cm->show_frame) {
6934 return -1;
6935 } else {
6936 int ret;
6937 if (cm->frame_to_show) {
6938 *dest = *cm->frame_to_show;
6939 dest->y_width = cm->width;
6940 dest->y_height = cm->height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006941 dest->uv_width = cm->width >> cm->seq_params.subsampling_x;
6942 dest->uv_height = cm->height >> cm->seq_params.subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006943 ret = 0;
6944 } else {
6945 ret = -1;
6946 }
Yaowu Xuf883b422016-08-30 14:01:10 -07006947 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006948 return ret;
6949 }
6950}
6951
Yaowu Xuf883b422016-08-30 14:01:10 -07006952int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006953 if (cpi->last_show_frame_buf_idx == INVALID_IDX) return -1;
6954
6955 *frame =
6956 cpi->common.buffer_pool->frame_bufs[cpi->last_show_frame_buf_idx].buf;
6957 return 0;
6958}
6959
Yunqing Wangff9bfca2018-06-06 11:46:08 -07006960static int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a,
6961 const YV12_BUFFER_CONFIG *b) {
6962 return a->y_height == b->y_height && a->y_width == b->y_width &&
6963 a->uv_height == b->uv_height && a->uv_width == b->uv_width &&
6964 a->y_stride == b->y_stride && a->uv_stride == b->uv_stride &&
6965 a->border == b->border &&
6966 (a->flags & YV12_FLAG_HIGHBITDEPTH) ==
6967 (b->flags & YV12_FLAG_HIGHBITDEPTH);
6968}
6969
Yunqing Wang93b18f32018-06-08 21:08:29 -07006970aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
6971 YV12_BUFFER_CONFIG *new_frame,
6972 YV12_BUFFER_CONFIG *sd) {
Yunqing Wangff9bfca2018-06-06 11:46:08 -07006973 const int num_planes = av1_num_planes(cm);
6974 if (!equal_dimensions_and_border(new_frame, sd))
6975 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
6976 "Incorrect buffer dimensions");
6977 else
6978 aom_yv12_copy_frame(new_frame, sd, num_planes);
6979
6980 return cm->error.error_code;
6981}
6982
Yaowu Xuf883b422016-08-30 14:01:10 -07006983int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
6984 AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006985 int hr = 0, hs = 0, vr = 0, vs = 0;
6986
6987 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
6988
6989 Scale2Ratio(horiz_mode, &hr, &hs);
6990 Scale2Ratio(vert_mode, &vr, &vs);
6991
6992 // always go to the next whole number
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07006993 cpi->resize_pending_width = (hs - 1 + cpi->oxcf.width * hr) / hs;
6994 cpi->resize_pending_height = (vs - 1 + cpi->oxcf.height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006995
6996 return 0;
6997}
6998
Yaowu Xuf883b422016-08-30 14:01:10 -07006999int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
Yaowu Xuc27fc142016-08-22 16:08:15 -07007000
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04007001int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
7002 size_t output_size = 0;
7003 size_t total_bytes_read = 0;
7004 size_t remaining_size = *frame_size;
7005 uint8_t *buff_ptr = buffer;
7006
7007 // go through each OBUs
7008 while (total_bytes_read < *frame_size) {
7009 uint8_t saved_obu_header[2];
7010 uint64_t obu_payload_size;
7011 size_t length_of_payload_size;
7012 size_t length_of_obu_size;
7013 uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
7014 size_t obu_bytes_read = obu_header_size; // bytes read for current obu
7015
7016 // save the obu header (1 or 2 bytes)
7017 memmove(saved_obu_header, buff_ptr, obu_header_size);
7018 // clear the obu_has_size_field
7019 saved_obu_header[0] = saved_obu_header[0] & (~0x2);
7020
7021 // get the payload_size and length of payload_size
7022 if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
7023 &obu_payload_size, &length_of_payload_size) != 0) {
7024 return AOM_CODEC_ERROR;
7025 }
7026 obu_bytes_read += length_of_payload_size;
7027
7028 // calculate the length of size of the obu header plus payload
7029 length_of_obu_size =
7030 aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
7031
7032 // move the rest of data to new location
7033 memmove(buff_ptr + length_of_obu_size + obu_header_size,
7034 buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
Yaowu Xu9e494202018-04-03 11:19:49 -07007035 obu_bytes_read += (size_t)obu_payload_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04007036
7037 // write the new obu size
7038 const uint64_t obu_size = obu_header_size + obu_payload_size;
7039 size_t coded_obu_size;
7040 if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
7041 &coded_obu_size) != 0) {
7042 return AOM_CODEC_ERROR;
7043 }
7044
7045 // write the saved (modified) obu_header following obu size
7046 memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
7047
7048 total_bytes_read += obu_bytes_read;
7049 remaining_size -= obu_bytes_read;
7050 buff_ptr += length_of_obu_size + obu_size;
Yaowu Xu9e494202018-04-03 11:19:49 -07007051 output_size += length_of_obu_size + (size_t)obu_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04007052 }
7053
7054 *frame_size = output_size;
7055 return AOM_CODEC_OK;
7056}
7057
Yaowu Xuf883b422016-08-30 14:01:10 -07007058void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007059 // TODO(yunqingwang): For what references to use, external encoding flags
7060 // should be consistent with internal reference frame selection. Need to
7061 // ensure that there is not conflict between the two. In AV1 encoder, the
7062 // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
7063 // GOLDEN, BWDREF, ALTREF2. If only one reference frame is used, it must be
7064 // LAST.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08007065 cpi->ext_ref_frame_flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007066 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007067 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
7068 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
7069 AOM_EFLAG_NO_REF_ARF2)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07007070 if (flags & AOM_EFLAG_NO_REF_LAST) {
Yunqing Wangf2e7a392017-11-08 00:27:21 -08007071 cpi->ext_ref_frame_flags = 0;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007072 } else {
7073 int ref = AOM_REFFRAME_ALL;
7074
7075 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
7076 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
7077
7078 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
7079
7080 if (flags & AOM_EFLAG_NO_REF_ARF) {
7081 ref ^= AOM_ALT_FLAG;
7082 ref ^= AOM_BWD_FLAG;
7083 ref ^= AOM_ALT2_FLAG;
7084 } else {
7085 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
7086 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
7087 }
7088
7089 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007090 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07007091 }
7092
7093 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007094 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07007095 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007096
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007097 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
7098 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007099
Yaowu Xuf883b422016-08-30 14:01:10 -07007100 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07007101
Yunqing Wang9a50fec2017-11-02 17:02:00 -07007102 if (flags & AOM_EFLAG_NO_UPD_ARF) {
7103 upd ^= AOM_ALT_FLAG;
7104 upd ^= AOM_BWD_FLAG;
7105 upd ^= AOM_ALT2_FLAG;
7106 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07007107
Yaowu Xuf883b422016-08-30 14:01:10 -07007108 av1_update_reference(cpi, upd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007109 }
7110
sarahparker21dbca42018-03-30 17:43:44 -07007111 cpi->ext_use_ref_frame_mvs = cpi->oxcf.allow_ref_frame_mvs &
7112 ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
sarahparker27d686a2018-03-30 17:43:44 -07007113 cpi->ext_use_error_resilient = cpi->oxcf.error_resilient_mode |
7114 ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
sarahparker9806fed2018-03-30 17:43:44 -07007115 cpi->ext_use_s_frame =
7116 cpi->oxcf.s_frame_mode | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
Sarah Parker50b6d6e2018-04-11 19:21:54 -07007117 cpi->ext_use_primary_ref_none = (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
sarahparker21dbca42018-03-30 17:43:44 -07007118
Yaowu Xuf883b422016-08-30 14:01:10 -07007119 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
7120 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07007121 }
7122}
Andrey Norkin795ba872018-03-06 13:24:14 -08007123
Andrey Norkin795ba872018-03-06 13:24:14 -08007124int64_t timebase_units_to_ticks(const aom_rational_t *timebase, int64_t n) {
7125 return n * TICKS_PER_SEC * timebase->num / timebase->den;
7126}
7127
7128int64_t ticks_to_timebase_units(const aom_rational_t *timebase, int64_t n) {
7129 const int64_t round = TICKS_PER_SEC * timebase->num / 2 - 1;
7130 return (n * timebase->den + round) / timebase->num / TICKS_PER_SEC;
7131}
Tom Fineganf8d6a162018-08-21 10:47:55 -07007132
7133aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi) {
7134 if (!cpi) return NULL;
7135
7136 uint8_t header_buf[512] = { 0 };
7137 const uint32_t sequence_header_size =
7138 write_sequence_header_obu(cpi, &header_buf[0]);
7139 assert(sequence_header_size <= sizeof(header_buf));
7140 if (sequence_header_size == 0) return NULL;
7141
7142 const size_t obu_header_size = 1;
7143 const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size);
7144 const size_t payload_offset = obu_header_size + size_field_size;
7145
7146 if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL;
7147 memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size);
7148
7149 if (write_obu_header(OBU_SEQUENCE_HEADER, 0, &header_buf[0]) !=
7150 obu_header_size) {
7151 return NULL;
7152 }
7153
7154 size_t coded_size_field_size = 0;
7155 if (aom_uleb_encode(sequence_header_size, size_field_size,
7156 &header_buf[obu_header_size],
7157 &coded_size_field_size) != 0) {
7158 return NULL;
7159 }
7160 assert(coded_size_field_size == size_field_size);
7161
7162 aom_fixed_buf_t *global_headers =
7163 (aom_fixed_buf_t *)malloc(sizeof(*global_headers));
7164 if (!global_headers) return NULL;
7165
7166 const size_t global_header_buf_size =
7167 obu_header_size + size_field_size + sequence_header_size;
7168
7169 global_headers->buf = malloc(global_header_buf_size);
7170 if (!global_headers->buf) {
7171 free(global_headers);
7172 return NULL;
7173 }
7174
7175 memcpy(global_headers->buf, &header_buf[0], global_header_buf_size);
7176 global_headers->sz = global_header_buf_size;
7177 return global_headers;
7178}