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