1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libvpx/vp9/encoder/vp9_onyx_if.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,4309 @@ 1.4 +/* 1.5 + * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license 1.8 + * that can be found in the LICENSE file in the root of the source 1.9 + * tree. An additional intellectual property rights grant can be found 1.10 + * in the file PATENTS. All contributing project authors may 1.11 + * be found in the AUTHORS file in the root of the source tree. 1.12 + */ 1.13 + 1.14 +#include <math.h> 1.15 +#include <stdio.h> 1.16 +#include <limits.h> 1.17 + 1.18 +#include "./vpx_config.h" 1.19 +#include "./vpx_scale_rtcd.h" 1.20 + 1.21 +#include "vp9/common/vp9_alloccommon.h" 1.22 +#include "vp9/common/vp9_filter.h" 1.23 +#include "vp9/common/vp9_idct.h" 1.24 +#if CONFIG_VP9_POSTPROC 1.25 +#include "vp9/common/vp9_postproc.h" 1.26 +#endif 1.27 +#include "vp9/common/vp9_reconinter.h" 1.28 +#include "vp9/common/vp9_systemdependent.h" 1.29 +#include "vp9/common/vp9_tile_common.h" 1.30 +#include "vp9/encoder/vp9_firstpass.h" 1.31 +#include "vp9/encoder/vp9_mbgraph.h" 1.32 +#include "vp9/encoder/vp9_onyx_int.h" 1.33 +#include "vp9/encoder/vp9_picklpf.h" 1.34 +#include "vp9/encoder/vp9_psnr.h" 1.35 +#include "vp9/encoder/vp9_ratectrl.h" 1.36 +#include "vp9/encoder/vp9_rdopt.h" 1.37 +#include "vp9/encoder/vp9_segmentation.h" 1.38 +#include "vp9/encoder/vp9_temporal_filter.h" 1.39 +#include "vp9/encoder/vp9_vaq.h" 1.40 + 1.41 +#include "vpx_ports/vpx_timer.h" 1.42 + 1.43 + 1.44 +extern void print_tree_update_probs(); 1.45 + 1.46 +static void set_default_lf_deltas(struct loopfilter *lf); 1.47 + 1.48 +#define DEFAULT_INTERP_FILTER SWITCHABLE 1.49 + 1.50 +#define SHARP_FILTER_QTHRESH 0 /* Q threshold for 8-tap sharp filter */ 1.51 + 1.52 +#define ALTREF_HIGH_PRECISION_MV 1 // Whether to use high precision mv 1.53 + // for altref computation. 1.54 +#define HIGH_PRECISION_MV_QTHRESH 200 // Q threshold for high precision 1.55 + // mv. Choose a very high value for 1.56 + // now so that HIGH_PRECISION is always 1.57 + // chosen. 1.58 + 1.59 +// Masks for partially or completely disabling split mode 1.60 +#define DISABLE_ALL_SPLIT 0x3F 1.61 +#define DISABLE_ALL_INTER_SPLIT 0x1F 1.62 +#define DISABLE_COMPOUND_SPLIT 0x18 1.63 +#define LAST_AND_INTRA_SPLIT_ONLY 0x1E 1.64 + 1.65 +#if CONFIG_INTERNAL_STATS 1.66 +extern double vp9_calc_ssim(YV12_BUFFER_CONFIG *source, 1.67 + YV12_BUFFER_CONFIG *dest, int lumamask, 1.68 + double *weight); 1.69 + 1.70 + 1.71 +extern double vp9_calc_ssimg(YV12_BUFFER_CONFIG *source, 1.72 + YV12_BUFFER_CONFIG *dest, double *ssim_y, 1.73 + double *ssim_u, double *ssim_v); 1.74 + 1.75 + 1.76 +#endif 1.77 + 1.78 +// #define OUTPUT_YUV_REC 1.79 + 1.80 +#ifdef OUTPUT_YUV_SRC 1.81 +FILE *yuv_file; 1.82 +#endif 1.83 +#ifdef OUTPUT_YUV_REC 1.84 +FILE *yuv_rec_file; 1.85 +#endif 1.86 + 1.87 +#if 0 1.88 +FILE *framepsnr; 1.89 +FILE *kf_list; 1.90 +FILE *keyfile; 1.91 +#endif 1.92 + 1.93 + 1.94 +#ifdef ENTROPY_STATS 1.95 +extern int intra_mode_stats[INTRA_MODES] 1.96 + [INTRA_MODES] 1.97 + [INTRA_MODES]; 1.98 +#endif 1.99 + 1.100 +#ifdef MODE_STATS 1.101 +extern void init_tx_count_stats(); 1.102 +extern void write_tx_count_stats(); 1.103 +extern void init_switchable_interp_stats(); 1.104 +extern void write_switchable_interp_stats(); 1.105 +#endif 1.106 + 1.107 +#ifdef SPEEDSTATS 1.108 +unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.109 + 0, 0, 0}; 1.110 +#endif 1.111 + 1.112 +#if defined(SECTIONBITS_OUTPUT) 1.113 +extern unsigned __int64 Sectionbits[500]; 1.114 +#endif 1.115 + 1.116 +extern void vp9_init_quantizer(VP9_COMP *cpi); 1.117 + 1.118 +// Tables relating active max Q to active min Q 1.119 +static int kf_low_motion_minq[QINDEX_RANGE]; 1.120 +static int kf_high_motion_minq[QINDEX_RANGE]; 1.121 +static int gf_low_motion_minq[QINDEX_RANGE]; 1.122 +static int gf_high_motion_minq[QINDEX_RANGE]; 1.123 +static int inter_minq[QINDEX_RANGE]; 1.124 +static int afq_low_motion_minq[QINDEX_RANGE]; 1.125 +static int afq_high_motion_minq[QINDEX_RANGE]; 1.126 + 1.127 +static INLINE void Scale2Ratio(int mode, int *hr, int *hs) { 1.128 + switch (mode) { 1.129 + case NORMAL: 1.130 + *hr = 1; 1.131 + *hs = 1; 1.132 + break; 1.133 + case FOURFIVE: 1.134 + *hr = 4; 1.135 + *hs = 5; 1.136 + break; 1.137 + case THREEFIVE: 1.138 + *hr = 3; 1.139 + *hs = 5; 1.140 + break; 1.141 + case ONETWO: 1.142 + *hr = 1; 1.143 + *hs = 2; 1.144 + break; 1.145 + default: 1.146 + *hr = 1; 1.147 + *hs = 1; 1.148 + assert(0); 1.149 + break; 1.150 + } 1.151 +} 1.152 + 1.153 +// Functions to compute the active minq lookup table entries based on a 1.154 +// formulaic approach to facilitate easier adjustment of the Q tables. 1.155 +// The formulae were derived from computing a 3rd order polynomial best 1.156 +// fit to the original data (after plotting real maxq vs minq (not q index)) 1.157 +static int calculate_minq_index(double maxq, 1.158 + double x3, double x2, double x1, double c) { 1.159 + int i; 1.160 + const double minqtarget = MIN(((x3 * maxq + x2) * maxq + x1) * maxq + c, 1.161 + maxq); 1.162 + 1.163 + // Special case handling to deal with the step from q2.0 1.164 + // down to lossless mode represented by q 1.0. 1.165 + if (minqtarget <= 2.0) 1.166 + return 0; 1.167 + 1.168 + for (i = 0; i < QINDEX_RANGE; i++) { 1.169 + if (minqtarget <= vp9_convert_qindex_to_q(i)) 1.170 + return i; 1.171 + } 1.172 + 1.173 + return QINDEX_RANGE - 1; 1.174 +} 1.175 + 1.176 +static void init_minq_luts(void) { 1.177 + int i; 1.178 + 1.179 + for (i = 0; i < QINDEX_RANGE; i++) { 1.180 + const double maxq = vp9_convert_qindex_to_q(i); 1.181 + 1.182 + 1.183 + kf_low_motion_minq[i] = calculate_minq_index(maxq, 1.184 + 0.000001, 1.185 + -0.0004, 1.186 + 0.15, 1.187 + 0.0); 1.188 + kf_high_motion_minq[i] = calculate_minq_index(maxq, 1.189 + 0.000002, 1.190 + -0.0012, 1.191 + 0.5, 1.192 + 0.0); 1.193 + 1.194 + gf_low_motion_minq[i] = calculate_minq_index(maxq, 1.195 + 0.0000015, 1.196 + -0.0009, 1.197 + 0.32, 1.198 + 0.0); 1.199 + gf_high_motion_minq[i] = calculate_minq_index(maxq, 1.200 + 0.0000021, 1.201 + -0.00125, 1.202 + 0.50, 1.203 + 0.0); 1.204 + inter_minq[i] = calculate_minq_index(maxq, 1.205 + 0.00000271, 1.206 + -0.00113, 1.207 + 0.75, 1.208 + 0.0); 1.209 + afq_low_motion_minq[i] = calculate_minq_index(maxq, 1.210 + 0.0000015, 1.211 + -0.0009, 1.212 + 0.33, 1.213 + 0.0); 1.214 + afq_high_motion_minq[i] = calculate_minq_index(maxq, 1.215 + 0.0000021, 1.216 + -0.00125, 1.217 + 0.55, 1.218 + 0.0); 1.219 + } 1.220 +} 1.221 + 1.222 +static int get_active_quality(int q, 1.223 + int gfu_boost, 1.224 + int low, 1.225 + int high, 1.226 + int *low_motion_minq, 1.227 + int *high_motion_minq) { 1.228 + int active_best_quality; 1.229 + if (gfu_boost > high) { 1.230 + active_best_quality = low_motion_minq[q]; 1.231 + } else if (gfu_boost < low) { 1.232 + active_best_quality = high_motion_minq[q]; 1.233 + } else { 1.234 + const int gap = high - low; 1.235 + const int offset = high - gfu_boost; 1.236 + const int qdiff = high_motion_minq[q] - low_motion_minq[q]; 1.237 + const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap; 1.238 + active_best_quality = low_motion_minq[q] + adjustment; 1.239 + } 1.240 + return active_best_quality; 1.241 +} 1.242 + 1.243 +static void set_mvcost(VP9_COMP *cpi) { 1.244 + MACROBLOCK *const mb = &cpi->mb; 1.245 + if (cpi->common.allow_high_precision_mv) { 1.246 + mb->mvcost = mb->nmvcost_hp; 1.247 + mb->mvsadcost = mb->nmvsadcost_hp; 1.248 + } else { 1.249 + mb->mvcost = mb->nmvcost; 1.250 + mb->mvsadcost = mb->nmvsadcost; 1.251 + } 1.252 +} 1.253 + 1.254 +void vp9_initialize_enc() { 1.255 + static int init_done = 0; 1.256 + 1.257 + if (!init_done) { 1.258 + vp9_initialize_common(); 1.259 + vp9_tokenize_initialize(); 1.260 + vp9_init_quant_tables(); 1.261 + vp9_init_me_luts(); 1.262 + init_minq_luts(); 1.263 + // init_base_skip_probs(); 1.264 + init_done = 1; 1.265 + } 1.266 +} 1.267 + 1.268 +static void setup_features(VP9_COMMON *cm) { 1.269 + struct loopfilter *const lf = &cm->lf; 1.270 + struct segmentation *const seg = &cm->seg; 1.271 + 1.272 + // Set up default state for MB feature flags 1.273 + seg->enabled = 0; 1.274 + 1.275 + seg->update_map = 0; 1.276 + seg->update_data = 0; 1.277 + vpx_memset(seg->tree_probs, 255, sizeof(seg->tree_probs)); 1.278 + 1.279 + vp9_clearall_segfeatures(seg); 1.280 + 1.281 + lf->mode_ref_delta_enabled = 0; 1.282 + lf->mode_ref_delta_update = 0; 1.283 + vp9_zero(lf->ref_deltas); 1.284 + vp9_zero(lf->mode_deltas); 1.285 + vp9_zero(lf->last_ref_deltas); 1.286 + vp9_zero(lf->last_mode_deltas); 1.287 + 1.288 + set_default_lf_deltas(lf); 1.289 +} 1.290 + 1.291 +static void dealloc_compressor_data(VP9_COMP *cpi) { 1.292 + // Delete sementation map 1.293 + vpx_free(cpi->segmentation_map); 1.294 + cpi->segmentation_map = 0; 1.295 + vpx_free(cpi->common.last_frame_seg_map); 1.296 + cpi->common.last_frame_seg_map = 0; 1.297 + vpx_free(cpi->coding_context.last_frame_seg_map_copy); 1.298 + cpi->coding_context.last_frame_seg_map_copy = 0; 1.299 + 1.300 + vpx_free(cpi->active_map); 1.301 + cpi->active_map = 0; 1.302 + 1.303 + vp9_free_frame_buffers(&cpi->common); 1.304 + 1.305 + vp9_free_frame_buffer(&cpi->last_frame_uf); 1.306 + vp9_free_frame_buffer(&cpi->scaled_source); 1.307 + vp9_free_frame_buffer(&cpi->alt_ref_buffer); 1.308 + vp9_lookahead_destroy(cpi->lookahead); 1.309 + 1.310 + vpx_free(cpi->tok); 1.311 + cpi->tok = 0; 1.312 + 1.313 + // Activity mask based per mb zbin adjustments 1.314 + vpx_free(cpi->mb_activity_map); 1.315 + cpi->mb_activity_map = 0; 1.316 + vpx_free(cpi->mb_norm_activity_map); 1.317 + cpi->mb_norm_activity_map = 0; 1.318 + 1.319 + vpx_free(cpi->above_context[0]); 1.320 + cpi->above_context[0] = NULL; 1.321 + 1.322 + vpx_free(cpi->above_seg_context); 1.323 + cpi->above_seg_context = NULL; 1.324 +} 1.325 + 1.326 +// Computes a q delta (in "q index" terms) to get from a starting q value 1.327 +// to a target value 1.328 +// target q value 1.329 +int vp9_compute_qdelta(VP9_COMP *cpi, double qstart, double qtarget) { 1.330 + int i; 1.331 + int start_index = cpi->worst_quality; 1.332 + int target_index = cpi->worst_quality; 1.333 + 1.334 + // Convert the average q value to an index. 1.335 + for (i = cpi->best_quality; i < cpi->worst_quality; i++) { 1.336 + start_index = i; 1.337 + if (vp9_convert_qindex_to_q(i) >= qstart) 1.338 + break; 1.339 + } 1.340 + 1.341 + // Convert the q target to an index 1.342 + for (i = cpi->best_quality; i < cpi->worst_quality; i++) { 1.343 + target_index = i; 1.344 + if (vp9_convert_qindex_to_q(i) >= qtarget) 1.345 + break; 1.346 + } 1.347 + 1.348 + return target_index - start_index; 1.349 +} 1.350 + 1.351 +static void configure_static_seg_features(VP9_COMP *cpi) { 1.352 + VP9_COMMON *cm = &cpi->common; 1.353 + struct segmentation *seg = &cm->seg; 1.354 + 1.355 + int high_q = (int)(cpi->avg_q > 48.0); 1.356 + int qi_delta; 1.357 + 1.358 + // Disable and clear down for KF 1.359 + if (cm->frame_type == KEY_FRAME) { 1.360 + // Clear down the global segmentation map 1.361 + vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols); 1.362 + seg->update_map = 0; 1.363 + seg->update_data = 0; 1.364 + cpi->static_mb_pct = 0; 1.365 + 1.366 + // Disable segmentation 1.367 + vp9_disable_segmentation((VP9_PTR)cpi); 1.368 + 1.369 + // Clear down the segment features. 1.370 + vp9_clearall_segfeatures(seg); 1.371 + } else if (cpi->refresh_alt_ref_frame) { 1.372 + // If this is an alt ref frame 1.373 + // Clear down the global segmentation map 1.374 + vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols); 1.375 + seg->update_map = 0; 1.376 + seg->update_data = 0; 1.377 + cpi->static_mb_pct = 0; 1.378 + 1.379 + // Disable segmentation and individual segment features by default 1.380 + vp9_disable_segmentation((VP9_PTR)cpi); 1.381 + vp9_clearall_segfeatures(seg); 1.382 + 1.383 + // Scan frames from current to arf frame. 1.384 + // This function re-enables segmentation if appropriate. 1.385 + vp9_update_mbgraph_stats(cpi); 1.386 + 1.387 + // If segmentation was enabled set those features needed for the 1.388 + // arf itself. 1.389 + if (seg->enabled) { 1.390 + seg->update_map = 1; 1.391 + seg->update_data = 1; 1.392 + 1.393 + qi_delta = vp9_compute_qdelta(cpi, cpi->avg_q, (cpi->avg_q * 0.875)); 1.394 + vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, (qi_delta - 2)); 1.395 + vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2); 1.396 + 1.397 + vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q); 1.398 + vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF); 1.399 + 1.400 + // Where relevant assume segment data is delta data 1.401 + seg->abs_delta = SEGMENT_DELTADATA; 1.402 + } 1.403 + } else if (seg->enabled) { 1.404 + // All other frames if segmentation has been enabled 1.405 + 1.406 + // First normal frame in a valid gf or alt ref group 1.407 + if (cpi->frames_since_golden == 0) { 1.408 + // Set up segment features for normal frames in an arf group 1.409 + if (cpi->source_alt_ref_active) { 1.410 + seg->update_map = 0; 1.411 + seg->update_data = 1; 1.412 + seg->abs_delta = SEGMENT_DELTADATA; 1.413 + 1.414 + qi_delta = vp9_compute_qdelta(cpi, cpi->avg_q, 1.415 + (cpi->avg_q * 1.125)); 1.416 + vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, (qi_delta + 2)); 1.417 + vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q); 1.418 + 1.419 + vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2); 1.420 + vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF); 1.421 + 1.422 + // Segment coding disabled for compred testing 1.423 + if (high_q || (cpi->static_mb_pct == 100)) { 1.424 + vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME); 1.425 + vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME); 1.426 + vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP); 1.427 + } 1.428 + } else { 1.429 + // Disable segmentation and clear down features if alt ref 1.430 + // is not active for this group 1.431 + 1.432 + vp9_disable_segmentation((VP9_PTR)cpi); 1.433 + 1.434 + vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols); 1.435 + 1.436 + seg->update_map = 0; 1.437 + seg->update_data = 0; 1.438 + 1.439 + vp9_clearall_segfeatures(seg); 1.440 + } 1.441 + } else if (cpi->is_src_frame_alt_ref) { 1.442 + // Special case where we are coding over the top of a previous 1.443 + // alt ref frame. 1.444 + // Segment coding disabled for compred testing 1.445 + 1.446 + // Enable ref frame features for segment 0 as well 1.447 + vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME); 1.448 + vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME); 1.449 + 1.450 + // All mbs should use ALTREF_FRAME 1.451 + vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME); 1.452 + vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME); 1.453 + vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME); 1.454 + vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME); 1.455 + 1.456 + // Skip all MBs if high Q (0,0 mv and skip coeffs) 1.457 + if (high_q) { 1.458 + vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP); 1.459 + vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP); 1.460 + } 1.461 + // Enable data update 1.462 + seg->update_data = 1; 1.463 + } else { 1.464 + // All other frames. 1.465 + 1.466 + // No updates.. leave things as they are. 1.467 + seg->update_map = 0; 1.468 + seg->update_data = 0; 1.469 + } 1.470 + } 1.471 +} 1.472 + 1.473 +#ifdef ENTROPY_STATS 1.474 +void vp9_update_mode_context_stats(VP9_COMP *cpi) { 1.475 + VP9_COMMON *cm = &cpi->common; 1.476 + int i, j; 1.477 + unsigned int (*inter_mode_counts)[INTER_MODES - 1][2] = 1.478 + cm->fc.inter_mode_counts; 1.479 + int64_t (*mv_ref_stats)[INTER_MODES - 1][2] = cpi->mv_ref_stats; 1.480 + FILE *f; 1.481 + 1.482 + // Read the past stats counters 1.483 + f = fopen("mode_context.bin", "rb"); 1.484 + if (!f) { 1.485 + vpx_memset(cpi->mv_ref_stats, 0, sizeof(cpi->mv_ref_stats)); 1.486 + } else { 1.487 + fread(cpi->mv_ref_stats, sizeof(cpi->mv_ref_stats), 1, f); 1.488 + fclose(f); 1.489 + } 1.490 + 1.491 + // Add in the values for this frame 1.492 + for (i = 0; i < INTER_MODE_CONTEXTS; i++) { 1.493 + for (j = 0; j < INTER_MODES - 1; j++) { 1.494 + mv_ref_stats[i][j][0] += (int64_t)inter_mode_counts[i][j][0]; 1.495 + mv_ref_stats[i][j][1] += (int64_t)inter_mode_counts[i][j][1]; 1.496 + } 1.497 + } 1.498 + 1.499 + // Write back the accumulated stats 1.500 + f = fopen("mode_context.bin", "wb"); 1.501 + fwrite(cpi->mv_ref_stats, sizeof(cpi->mv_ref_stats), 1, f); 1.502 + fclose(f); 1.503 +} 1.504 + 1.505 +void print_mode_context(VP9_COMP *cpi) { 1.506 + FILE *f = fopen("vp9_modecont.c", "a"); 1.507 + int i, j; 1.508 + 1.509 + fprintf(f, "#include \"vp9_entropy.h\"\n"); 1.510 + fprintf( 1.511 + f, 1.512 + "const int inter_mode_probs[INTER_MODE_CONTEXTS][INTER_MODES - 1] ="); 1.513 + fprintf(f, "{\n"); 1.514 + for (j = 0; j < INTER_MODE_CONTEXTS; j++) { 1.515 + fprintf(f, " {/* %d */ ", j); 1.516 + fprintf(f, " "); 1.517 + for (i = 0; i < INTER_MODES - 1; i++) { 1.518 + int this_prob; 1.519 + int64_t count = cpi->mv_ref_stats[j][i][0] + cpi->mv_ref_stats[j][i][1]; 1.520 + if (count) 1.521 + this_prob = ((cpi->mv_ref_stats[j][i][0] * 256) + (count >> 1)) / count; 1.522 + else 1.523 + this_prob = 128; 1.524 + 1.525 + // context probs 1.526 + fprintf(f, "%5d, ", this_prob); 1.527 + } 1.528 + fprintf(f, " },\n"); 1.529 + } 1.530 + 1.531 + fprintf(f, "};\n"); 1.532 + fclose(f); 1.533 +} 1.534 +#endif // ENTROPY_STATS 1.535 + 1.536 +// DEBUG: Print out the segment id of each MB in the current frame. 1.537 +static void print_seg_map(VP9_COMP *cpi) { 1.538 + VP9_COMMON *cm = &cpi->common; 1.539 + int row, col; 1.540 + int map_index = 0; 1.541 + FILE *statsfile = fopen("segmap.stt", "a"); 1.542 + 1.543 + fprintf(statsfile, "%10d\n", cm->current_video_frame); 1.544 + 1.545 + for (row = 0; row < cpi->common.mi_rows; row++) { 1.546 + for (col = 0; col < cpi->common.mi_cols; col++) { 1.547 + fprintf(statsfile, "%10d", cpi->segmentation_map[map_index]); 1.548 + map_index++; 1.549 + } 1.550 + fprintf(statsfile, "\n"); 1.551 + } 1.552 + fprintf(statsfile, "\n"); 1.553 + 1.554 + fclose(statsfile); 1.555 +} 1.556 + 1.557 +static void update_reference_segmentation_map(VP9_COMP *cpi) { 1.558 + VP9_COMMON *const cm = &cpi->common; 1.559 + int row, col; 1.560 + MODE_INFO **mi_8x8, **mi_8x8_ptr = cm->mi_grid_visible; 1.561 + uint8_t *cache_ptr = cm->last_frame_seg_map, *cache; 1.562 + 1.563 + for (row = 0; row < cm->mi_rows; row++) { 1.564 + mi_8x8 = mi_8x8_ptr; 1.565 + cache = cache_ptr; 1.566 + for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++) 1.567 + cache[0] = mi_8x8[0]->mbmi.segment_id; 1.568 + mi_8x8_ptr += cm->mode_info_stride; 1.569 + cache_ptr += cm->mi_cols; 1.570 + } 1.571 +} 1.572 + 1.573 +static void set_default_lf_deltas(struct loopfilter *lf) { 1.574 + lf->mode_ref_delta_enabled = 1; 1.575 + lf->mode_ref_delta_update = 1; 1.576 + 1.577 + vp9_zero(lf->ref_deltas); 1.578 + vp9_zero(lf->mode_deltas); 1.579 + 1.580 + // Test of ref frame deltas 1.581 + lf->ref_deltas[INTRA_FRAME] = 2; 1.582 + lf->ref_deltas[LAST_FRAME] = 0; 1.583 + lf->ref_deltas[GOLDEN_FRAME] = -2; 1.584 + lf->ref_deltas[ALTREF_FRAME] = -2; 1.585 + 1.586 + lf->mode_deltas[0] = 0; // Zero 1.587 + lf->mode_deltas[1] = 0; // New mv 1.588 +} 1.589 + 1.590 +static void set_rd_speed_thresholds(VP9_COMP *cpi, int mode) { 1.591 + SPEED_FEATURES *sf = &cpi->sf; 1.592 + int i; 1.593 + 1.594 + // Set baseline threshold values 1.595 + for (i = 0; i < MAX_MODES; ++i) 1.596 + sf->thresh_mult[i] = mode == 0 ? -500 : 0; 1.597 + 1.598 + sf->thresh_mult[THR_NEARESTMV] = 0; 1.599 + sf->thresh_mult[THR_NEARESTG] = 0; 1.600 + sf->thresh_mult[THR_NEARESTA] = 0; 1.601 + 1.602 + sf->thresh_mult[THR_DC] += 1000; 1.603 + 1.604 + sf->thresh_mult[THR_NEWMV] += 1000; 1.605 + sf->thresh_mult[THR_NEWA] += 1000; 1.606 + sf->thresh_mult[THR_NEWG] += 1000; 1.607 + 1.608 + sf->thresh_mult[THR_NEARMV] += 1000; 1.609 + sf->thresh_mult[THR_NEARA] += 1000; 1.610 + sf->thresh_mult[THR_COMP_NEARESTLA] += 1000; 1.611 + sf->thresh_mult[THR_COMP_NEARESTGA] += 1000; 1.612 + 1.613 + sf->thresh_mult[THR_TM] += 1000; 1.614 + 1.615 + sf->thresh_mult[THR_COMP_NEARLA] += 1500; 1.616 + sf->thresh_mult[THR_COMP_NEWLA] += 2000; 1.617 + sf->thresh_mult[THR_NEARG] += 1000; 1.618 + sf->thresh_mult[THR_COMP_NEARGA] += 1500; 1.619 + sf->thresh_mult[THR_COMP_NEWGA] += 2000; 1.620 + 1.621 + sf->thresh_mult[THR_ZEROMV] += 2000; 1.622 + sf->thresh_mult[THR_ZEROG] += 2000; 1.623 + sf->thresh_mult[THR_ZEROA] += 2000; 1.624 + sf->thresh_mult[THR_COMP_ZEROLA] += 2500; 1.625 + sf->thresh_mult[THR_COMP_ZEROGA] += 2500; 1.626 + 1.627 + sf->thresh_mult[THR_H_PRED] += 2000; 1.628 + sf->thresh_mult[THR_V_PRED] += 2000; 1.629 + sf->thresh_mult[THR_D45_PRED ] += 2500; 1.630 + sf->thresh_mult[THR_D135_PRED] += 2500; 1.631 + sf->thresh_mult[THR_D117_PRED] += 2500; 1.632 + sf->thresh_mult[THR_D153_PRED] += 2500; 1.633 + sf->thresh_mult[THR_D207_PRED] += 2500; 1.634 + sf->thresh_mult[THR_D63_PRED] += 2500; 1.635 + 1.636 + /* disable frame modes if flags not set */ 1.637 + if (!(cpi->ref_frame_flags & VP9_LAST_FLAG)) { 1.638 + sf->thresh_mult[THR_NEWMV ] = INT_MAX; 1.639 + sf->thresh_mult[THR_NEARESTMV] = INT_MAX; 1.640 + sf->thresh_mult[THR_ZEROMV ] = INT_MAX; 1.641 + sf->thresh_mult[THR_NEARMV ] = INT_MAX; 1.642 + } 1.643 + if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG)) { 1.644 + sf->thresh_mult[THR_NEARESTG ] = INT_MAX; 1.645 + sf->thresh_mult[THR_ZEROG ] = INT_MAX; 1.646 + sf->thresh_mult[THR_NEARG ] = INT_MAX; 1.647 + sf->thresh_mult[THR_NEWG ] = INT_MAX; 1.648 + } 1.649 + if (!(cpi->ref_frame_flags & VP9_ALT_FLAG)) { 1.650 + sf->thresh_mult[THR_NEARESTA ] = INT_MAX; 1.651 + sf->thresh_mult[THR_ZEROA ] = INT_MAX; 1.652 + sf->thresh_mult[THR_NEARA ] = INT_MAX; 1.653 + sf->thresh_mult[THR_NEWA ] = INT_MAX; 1.654 + } 1.655 + 1.656 + if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) != 1.657 + (VP9_LAST_FLAG | VP9_ALT_FLAG)) { 1.658 + sf->thresh_mult[THR_COMP_ZEROLA ] = INT_MAX; 1.659 + sf->thresh_mult[THR_COMP_NEARESTLA] = INT_MAX; 1.660 + sf->thresh_mult[THR_COMP_NEARLA ] = INT_MAX; 1.661 + sf->thresh_mult[THR_COMP_NEWLA ] = INT_MAX; 1.662 + } 1.663 + if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) != 1.664 + (VP9_GOLD_FLAG | VP9_ALT_FLAG)) { 1.665 + sf->thresh_mult[THR_COMP_ZEROGA ] = INT_MAX; 1.666 + sf->thresh_mult[THR_COMP_NEARESTGA] = INT_MAX; 1.667 + sf->thresh_mult[THR_COMP_NEARGA ] = INT_MAX; 1.668 + sf->thresh_mult[THR_COMP_NEWGA ] = INT_MAX; 1.669 + } 1.670 +} 1.671 + 1.672 +static void set_rd_speed_thresholds_sub8x8(VP9_COMP *cpi, int mode) { 1.673 + SPEED_FEATURES *sf = &cpi->sf; 1.674 + int i; 1.675 + 1.676 + for (i = 0; i < MAX_REFS; ++i) 1.677 + sf->thresh_mult_sub8x8[i] = mode == 0 ? -500 : 0; 1.678 + 1.679 + sf->thresh_mult_sub8x8[THR_LAST] += 2500; 1.680 + sf->thresh_mult_sub8x8[THR_GOLD] += 2500; 1.681 + sf->thresh_mult_sub8x8[THR_ALTR] += 2500; 1.682 + sf->thresh_mult_sub8x8[THR_INTRA] += 2500; 1.683 + sf->thresh_mult_sub8x8[THR_COMP_LA] += 4500; 1.684 + sf->thresh_mult_sub8x8[THR_COMP_GA] += 4500; 1.685 + 1.686 + // Check for masked out split cases. 1.687 + for (i = 0; i < MAX_REFS; i++) { 1.688 + if (sf->disable_split_mask & (1 << i)) 1.689 + sf->thresh_mult_sub8x8[i] = INT_MAX; 1.690 + } 1.691 + 1.692 + // disable mode test if frame flag is not set 1.693 + if (!(cpi->ref_frame_flags & VP9_LAST_FLAG)) 1.694 + sf->thresh_mult_sub8x8[THR_LAST] = INT_MAX; 1.695 + if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG)) 1.696 + sf->thresh_mult_sub8x8[THR_GOLD] = INT_MAX; 1.697 + if (!(cpi->ref_frame_flags & VP9_ALT_FLAG)) 1.698 + sf->thresh_mult_sub8x8[THR_ALTR] = INT_MAX; 1.699 + if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) != 1.700 + (VP9_LAST_FLAG | VP9_ALT_FLAG)) 1.701 + sf->thresh_mult_sub8x8[THR_COMP_LA] = INT_MAX; 1.702 + if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) != 1.703 + (VP9_GOLD_FLAG | VP9_ALT_FLAG)) 1.704 + sf->thresh_mult_sub8x8[THR_COMP_GA] = INT_MAX; 1.705 +} 1.706 + 1.707 +void vp9_set_speed_features(VP9_COMP *cpi) { 1.708 + SPEED_FEATURES *sf = &cpi->sf; 1.709 + int mode = cpi->compressor_speed; 1.710 + int speed = cpi->speed; 1.711 + int i; 1.712 + 1.713 + // Only modes 0 and 1 supported for now in experimental code basae 1.714 + if (mode > 1) 1.715 + mode = 1; 1.716 + 1.717 + for (i = 0; i < MAX_MODES; ++i) 1.718 + cpi->mode_chosen_counts[i] = 0; 1.719 + 1.720 + // best quality defaults 1.721 + sf->RD = 1; 1.722 + sf->search_method = NSTEP; 1.723 + sf->auto_filter = 1; 1.724 + sf->recode_loop = 1; 1.725 + sf->subpel_search_method = SUBPEL_TREE; 1.726 + sf->subpel_iters_per_step = 2; 1.727 + sf->optimize_coefficients = !cpi->oxcf.lossless; 1.728 + sf->reduce_first_step_size = 0; 1.729 + sf->auto_mv_step_size = 0; 1.730 + sf->max_step_search_steps = MAX_MVSEARCH_STEPS; 1.731 + sf->comp_inter_joint_search_thresh = BLOCK_4X4; 1.732 + sf->adaptive_rd_thresh = 0; 1.733 + sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_OFF; 1.734 + sf->tx_size_search_method = USE_FULL_RD; 1.735 + sf->use_lp32x32fdct = 0; 1.736 + sf->adaptive_motion_search = 0; 1.737 + sf->use_avoid_tested_higherror = 0; 1.738 + sf->reference_masking = 0; 1.739 + sf->use_one_partition_size_always = 0; 1.740 + sf->less_rectangular_check = 0; 1.741 + sf->use_square_partition_only = 0; 1.742 + sf->auto_min_max_partition_size = 0; 1.743 + sf->max_partition_size = BLOCK_64X64; 1.744 + sf->min_partition_size = BLOCK_4X4; 1.745 + sf->adjust_partitioning_from_last_frame = 0; 1.746 + sf->last_partitioning_redo_frequency = 4; 1.747 + sf->disable_split_mask = 0; 1.748 + sf->mode_search_skip_flags = 0; 1.749 + sf->disable_split_var_thresh = 0; 1.750 + sf->disable_filter_search_var_thresh = 0; 1.751 + for (i = 0; i < TX_SIZES; i++) { 1.752 + sf->intra_y_mode_mask[i] = ALL_INTRA_MODES; 1.753 + sf->intra_uv_mode_mask[i] = ALL_INTRA_MODES; 1.754 + } 1.755 + sf->use_rd_breakout = 0; 1.756 + sf->skip_encode_sb = 0; 1.757 + sf->use_uv_intra_rd_estimate = 0; 1.758 + sf->use_fast_lpf_pick = 0; 1.759 + sf->use_fast_coef_updates = 0; 1.760 + sf->using_small_partition_info = 0; 1.761 + sf->mode_skip_start = MAX_MODES; // Mode index at which mode skip mask set 1.762 + 1.763 +#if CONFIG_MULTIPLE_ARF 1.764 + // Switch segmentation off. 1.765 + sf->static_segmentation = 0; 1.766 +#else 1.767 + sf->static_segmentation = 0; 1.768 +#endif 1.769 + 1.770 + switch (mode) { 1.771 + case 0: // This is the best quality mode. 1.772 + break; 1.773 + 1.774 + case 1: 1.775 +#if CONFIG_MULTIPLE_ARF 1.776 + // Switch segmentation off. 1.777 + sf->static_segmentation = 0; 1.778 +#else 1.779 + sf->static_segmentation = 0; 1.780 +#endif 1.781 + sf->use_avoid_tested_higherror = 1; 1.782 + sf->adaptive_rd_thresh = 1; 1.783 + sf->recode_loop = (speed < 1); 1.784 + 1.785 + if (speed == 1) { 1.786 + sf->use_square_partition_only = !frame_is_intra_only(&cpi->common); 1.787 + sf->less_rectangular_check = 1; 1.788 + sf->tx_size_search_method = frame_is_intra_only(&cpi->common) 1.789 + ? USE_FULL_RD : USE_LARGESTALL; 1.790 + 1.791 + if (MIN(cpi->common.width, cpi->common.height) >= 720) 1.792 + sf->disable_split_mask = cpi->common.show_frame ? 1.793 + DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT; 1.794 + else 1.795 + sf->disable_split_mask = DISABLE_COMPOUND_SPLIT; 1.796 + 1.797 + sf->use_rd_breakout = 1; 1.798 + sf->adaptive_motion_search = 1; 1.799 + sf->auto_mv_step_size = 1; 1.800 + sf->adaptive_rd_thresh = 2; 1.801 + sf->recode_loop = 2; 1.802 + sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V; 1.803 + sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V; 1.804 + sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V; 1.805 + } 1.806 + if (speed == 2) { 1.807 + sf->use_square_partition_only = !frame_is_intra_only(&cpi->common); 1.808 + sf->less_rectangular_check = 1; 1.809 + sf->tx_size_search_method = frame_is_intra_only(&cpi->common) 1.810 + ? USE_FULL_RD : USE_LARGESTALL; 1.811 + 1.812 + if (MIN(cpi->common.width, cpi->common.height) >= 720) 1.813 + sf->disable_split_mask = cpi->common.show_frame ? 1.814 + DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT; 1.815 + else 1.816 + sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY; 1.817 + 1.818 + 1.819 + sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH | 1.820 + FLAG_SKIP_INTRA_BESTINTER | 1.821 + FLAG_SKIP_COMP_BESTINTRA | 1.822 + FLAG_SKIP_INTRA_LOWVAR; 1.823 + 1.824 + sf->use_rd_breakout = 1; 1.825 + sf->adaptive_motion_search = 1; 1.826 + sf->auto_mv_step_size = 1; 1.827 + 1.828 + sf->disable_filter_search_var_thresh = 16; 1.829 + sf->comp_inter_joint_search_thresh = BLOCK_SIZES; 1.830 + 1.831 + sf->auto_min_max_partition_size = 1; 1.832 + sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_LOW_MOTION; 1.833 + sf->adjust_partitioning_from_last_frame = 1; 1.834 + sf->last_partitioning_redo_frequency = 3; 1.835 + 1.836 + sf->adaptive_rd_thresh = 2; 1.837 + sf->recode_loop = 2; 1.838 + sf->use_lp32x32fdct = 1; 1.839 + sf->mode_skip_start = 11; 1.840 + sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V; 1.841 + sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V; 1.842 + sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V; 1.843 + sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V; 1.844 + } 1.845 + if (speed == 3) { 1.846 + sf->use_square_partition_only = 1; 1.847 + sf->tx_size_search_method = USE_LARGESTALL; 1.848 + 1.849 + if (MIN(cpi->common.width, cpi->common.height) >= 720) 1.850 + sf->disable_split_mask = DISABLE_ALL_SPLIT; 1.851 + else 1.852 + sf->disable_split_mask = DISABLE_ALL_INTER_SPLIT; 1.853 + 1.854 + sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH | 1.855 + FLAG_SKIP_INTRA_BESTINTER | 1.856 + FLAG_SKIP_COMP_BESTINTRA | 1.857 + FLAG_SKIP_INTRA_LOWVAR; 1.858 + 1.859 + sf->use_rd_breakout = 1; 1.860 + sf->adaptive_motion_search = 1; 1.861 + sf->auto_mv_step_size = 1; 1.862 + 1.863 + sf->disable_filter_search_var_thresh = 16; 1.864 + sf->comp_inter_joint_search_thresh = BLOCK_SIZES; 1.865 + 1.866 + sf->auto_min_max_partition_size = 1; 1.867 + sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL; 1.868 + sf->adjust_partitioning_from_last_frame = 1; 1.869 + sf->last_partitioning_redo_frequency = 3; 1.870 + 1.871 + sf->use_uv_intra_rd_estimate = 1; 1.872 + sf->skip_encode_sb = 1; 1.873 + sf->use_lp32x32fdct = 1; 1.874 + sf->subpel_iters_per_step = 1; 1.875 + sf->use_fast_coef_updates = 2; 1.876 + 1.877 + sf->adaptive_rd_thresh = 4; 1.878 + sf->mode_skip_start = 6; 1.879 + } 1.880 + if (speed == 4) { 1.881 + sf->use_square_partition_only = 1; 1.882 + sf->tx_size_search_method = USE_LARGESTALL; 1.883 + sf->disable_split_mask = DISABLE_ALL_SPLIT; 1.884 + 1.885 + sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH | 1.886 + FLAG_SKIP_INTRA_BESTINTER | 1.887 + FLAG_SKIP_COMP_BESTINTRA | 1.888 + FLAG_SKIP_COMP_REFMISMATCH | 1.889 + FLAG_SKIP_INTRA_LOWVAR | 1.890 + FLAG_EARLY_TERMINATE; 1.891 + 1.892 + sf->use_rd_breakout = 1; 1.893 + sf->adaptive_motion_search = 1; 1.894 + sf->auto_mv_step_size = 1; 1.895 + 1.896 + sf->disable_filter_search_var_thresh = 16; 1.897 + sf->comp_inter_joint_search_thresh = BLOCK_SIZES; 1.898 + 1.899 + sf->auto_min_max_partition_size = 1; 1.900 + sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL; 1.901 + sf->adjust_partitioning_from_last_frame = 1; 1.902 + sf->last_partitioning_redo_frequency = 3; 1.903 + 1.904 + sf->use_uv_intra_rd_estimate = 1; 1.905 + sf->skip_encode_sb = 1; 1.906 + sf->use_lp32x32fdct = 1; 1.907 + sf->subpel_iters_per_step = 1; 1.908 + sf->use_fast_coef_updates = 2; 1.909 + 1.910 + sf->adaptive_rd_thresh = 4; 1.911 + sf->mode_skip_start = 6; 1.912 + 1.913 + /* sf->intra_y_mode_mask = INTRA_DC_ONLY; 1.914 + sf->intra_uv_mode_mask = INTRA_DC_ONLY; 1.915 + sf->search_method = BIGDIA; 1.916 + sf->disable_split_var_thresh = 64; 1.917 + sf->disable_filter_search_var_thresh = 64; */ 1.918 + } 1.919 + if (speed == 5) { 1.920 + sf->comp_inter_joint_search_thresh = BLOCK_SIZES; 1.921 + sf->use_one_partition_size_always = 1; 1.922 + sf->always_this_block_size = BLOCK_16X16; 1.923 + sf->tx_size_search_method = frame_is_intra_only(&cpi->common) ? 1.924 + USE_FULL_RD : USE_LARGESTALL; 1.925 + sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH | 1.926 + FLAG_SKIP_INTRA_BESTINTER | 1.927 + FLAG_SKIP_COMP_BESTINTRA | 1.928 + FLAG_SKIP_COMP_REFMISMATCH | 1.929 + FLAG_SKIP_INTRA_LOWVAR | 1.930 + FLAG_EARLY_TERMINATE; 1.931 + sf->use_rd_breakout = 1; 1.932 + sf->use_lp32x32fdct = 1; 1.933 + sf->optimize_coefficients = 0; 1.934 + sf->auto_mv_step_size = 1; 1.935 + // sf->reduce_first_step_size = 1; 1.936 + // sf->reference_masking = 1; 1.937 + 1.938 + sf->disable_split_mask = DISABLE_ALL_SPLIT; 1.939 + sf->search_method = HEX; 1.940 + sf->subpel_iters_per_step = 1; 1.941 + sf->disable_split_var_thresh = 64; 1.942 + sf->disable_filter_search_var_thresh = 96; 1.943 + for (i = 0; i < TX_SIZES; i++) { 1.944 + sf->intra_y_mode_mask[i] = INTRA_DC_ONLY; 1.945 + sf->intra_uv_mode_mask[i] = INTRA_DC_ONLY; 1.946 + } 1.947 + sf->use_fast_coef_updates = 2; 1.948 + sf->adaptive_rd_thresh = 4; 1.949 + sf->mode_skip_start = 6; 1.950 + } 1.951 + break; 1.952 + }; /* switch */ 1.953 + 1.954 + // Set rd thresholds based on mode and speed setting 1.955 + set_rd_speed_thresholds(cpi, mode); 1.956 + set_rd_speed_thresholds_sub8x8(cpi, mode); 1.957 + 1.958 + // Slow quant, dct and trellis not worthwhile for first pass 1.959 + // so make sure they are always turned off. 1.960 + if (cpi->pass == 1) { 1.961 + sf->optimize_coefficients = 0; 1.962 + } 1.963 + 1.964 + // No recode for 1 pass. 1.965 + if (cpi->pass == 0) { 1.966 + sf->recode_loop = 0; 1.967 + sf->optimize_coefficients = 0; 1.968 + } 1.969 + 1.970 + cpi->mb.fwd_txm4x4 = vp9_fdct4x4; 1.971 + if (cpi->oxcf.lossless || cpi->mb.e_mbd.lossless) { 1.972 + cpi->mb.fwd_txm4x4 = vp9_fwht4x4; 1.973 + } 1.974 + 1.975 + if (cpi->sf.subpel_search_method == SUBPEL_ITERATIVE) { 1.976 + cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_iterative; 1.977 + cpi->find_fractional_mv_step_comp = vp9_find_best_sub_pixel_comp_iterative; 1.978 + } else if (cpi->sf.subpel_search_method == SUBPEL_TREE) { 1.979 + cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree; 1.980 + cpi->find_fractional_mv_step_comp = vp9_find_best_sub_pixel_comp_tree; 1.981 + } 1.982 + 1.983 + cpi->mb.optimize = cpi->sf.optimize_coefficients == 1 && cpi->pass != 1; 1.984 + 1.985 +#ifdef SPEEDSTATS 1.986 + frames_at_speed[cpi->speed]++; 1.987 +#endif 1.988 +} 1.989 + 1.990 +static void alloc_raw_frame_buffers(VP9_COMP *cpi) { 1.991 + VP9_COMMON *cm = &cpi->common; 1.992 + 1.993 + cpi->lookahead = vp9_lookahead_init(cpi->oxcf.width, cpi->oxcf.height, 1.994 + cm->subsampling_x, cm->subsampling_y, 1.995 + cpi->oxcf.lag_in_frames); 1.996 + if (!cpi->lookahead) 1.997 + vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 1.998 + "Failed to allocate lag buffers"); 1.999 + 1.1000 + if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer, 1.1001 + cpi->oxcf.width, cpi->oxcf.height, 1.1002 + cm->subsampling_x, cm->subsampling_y, 1.1003 + VP9BORDERINPIXELS)) 1.1004 + vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 1.1005 + "Failed to allocate altref buffer"); 1.1006 +} 1.1007 + 1.1008 +void vp9_alloc_compressor_data(VP9_COMP *cpi) { 1.1009 + VP9_COMMON *cm = &cpi->common; 1.1010 + 1.1011 + if (vp9_alloc_frame_buffers(cm, cm->width, cm->height)) 1.1012 + vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 1.1013 + "Failed to allocate frame buffers"); 1.1014 + 1.1015 + if (vp9_alloc_frame_buffer(&cpi->last_frame_uf, 1.1016 + cm->width, cm->height, 1.1017 + cm->subsampling_x, cm->subsampling_y, 1.1018 + VP9BORDERINPIXELS)) 1.1019 + vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 1.1020 + "Failed to allocate last frame buffer"); 1.1021 + 1.1022 + if (vp9_alloc_frame_buffer(&cpi->scaled_source, 1.1023 + cm->width, cm->height, 1.1024 + cm->subsampling_x, cm->subsampling_y, 1.1025 + VP9BORDERINPIXELS)) 1.1026 + vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 1.1027 + "Failed to allocate scaled source buffer"); 1.1028 + 1.1029 + vpx_free(cpi->tok); 1.1030 + 1.1031 + { 1.1032 + unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols); 1.1033 + 1.1034 + CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok))); 1.1035 + } 1.1036 + 1.1037 + vpx_free(cpi->mb_activity_map); 1.1038 + CHECK_MEM_ERROR(cm, cpi->mb_activity_map, 1.1039 + vpx_calloc(sizeof(unsigned int), 1.1040 + cm->mb_rows * cm->mb_cols)); 1.1041 + 1.1042 + vpx_free(cpi->mb_norm_activity_map); 1.1043 + CHECK_MEM_ERROR(cm, cpi->mb_norm_activity_map, 1.1044 + vpx_calloc(sizeof(unsigned int), 1.1045 + cm->mb_rows * cm->mb_cols)); 1.1046 + 1.1047 + // 2 contexts per 'mi unit', so that we have one context per 4x4 txfm 1.1048 + // block where mi unit size is 8x8. 1.1049 + vpx_free(cpi->above_context[0]); 1.1050 + CHECK_MEM_ERROR(cm, cpi->above_context[0], 1.1051 + vpx_calloc(2 * mi_cols_aligned_to_sb(cm->mi_cols) * 1.1052 + MAX_MB_PLANE, 1.1053 + sizeof(*cpi->above_context[0]))); 1.1054 + 1.1055 + vpx_free(cpi->above_seg_context); 1.1056 + CHECK_MEM_ERROR(cm, cpi->above_seg_context, 1.1057 + vpx_calloc(mi_cols_aligned_to_sb(cm->mi_cols), 1.1058 + sizeof(*cpi->above_seg_context))); 1.1059 +} 1.1060 + 1.1061 + 1.1062 +static void update_frame_size(VP9_COMP *cpi) { 1.1063 + VP9_COMMON *cm = &cpi->common; 1.1064 + 1.1065 + vp9_update_frame_size(cm); 1.1066 + 1.1067 + // Update size of buffers local to this frame 1.1068 + if (vp9_realloc_frame_buffer(&cpi->last_frame_uf, 1.1069 + cm->width, cm->height, 1.1070 + cm->subsampling_x, cm->subsampling_y, 1.1071 + VP9BORDERINPIXELS)) 1.1072 + vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 1.1073 + "Failed to reallocate last frame buffer"); 1.1074 + 1.1075 + if (vp9_realloc_frame_buffer(&cpi->scaled_source, 1.1076 + cm->width, cm->height, 1.1077 + cm->subsampling_x, cm->subsampling_y, 1.1078 + VP9BORDERINPIXELS)) 1.1079 + vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 1.1080 + "Failed to reallocate scaled source buffer"); 1.1081 + 1.1082 + { 1.1083 + int y_stride = cpi->scaled_source.y_stride; 1.1084 + 1.1085 + if (cpi->sf.search_method == NSTEP) { 1.1086 + vp9_init3smotion_compensation(&cpi->mb, y_stride); 1.1087 + } else if (cpi->sf.search_method == DIAMOND) { 1.1088 + vp9_init_dsmotion_compensation(&cpi->mb, y_stride); 1.1089 + } 1.1090 + } 1.1091 + 1.1092 + { 1.1093 + int i; 1.1094 + for (i = 1; i < MAX_MB_PLANE; ++i) { 1.1095 + cpi->above_context[i] = cpi->above_context[0] + 1.1096 + i * sizeof(*cpi->above_context[0]) * 2 * 1.1097 + mi_cols_aligned_to_sb(cm->mi_cols); 1.1098 + } 1.1099 + } 1.1100 +} 1.1101 + 1.1102 + 1.1103 +// Table that converts 0-63 Q range values passed in outside to the Qindex 1.1104 +// range used internally. 1.1105 +static const int q_trans[] = { 1.1106 + 0, 4, 8, 12, 16, 20, 24, 28, 1.1107 + 32, 36, 40, 44, 48, 52, 56, 60, 1.1108 + 64, 68, 72, 76, 80, 84, 88, 92, 1.1109 + 96, 100, 104, 108, 112, 116, 120, 124, 1.1110 + 128, 132, 136, 140, 144, 148, 152, 156, 1.1111 + 160, 164, 168, 172, 176, 180, 184, 188, 1.1112 + 192, 196, 200, 204, 208, 212, 216, 220, 1.1113 + 224, 228, 232, 236, 240, 244, 249, 255, 1.1114 +}; 1.1115 + 1.1116 +int vp9_reverse_trans(int x) { 1.1117 + int i; 1.1118 + 1.1119 + for (i = 0; i < 64; i++) 1.1120 + if (q_trans[i] >= x) 1.1121 + return i; 1.1122 + 1.1123 + return 63; 1.1124 +}; 1.1125 +void vp9_new_framerate(VP9_COMP *cpi, double framerate) { 1.1126 + if (framerate < 0.1) 1.1127 + framerate = 30; 1.1128 + 1.1129 + cpi->oxcf.framerate = framerate; 1.1130 + cpi->output_framerate = cpi->oxcf.framerate; 1.1131 + cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth 1.1132 + / cpi->output_framerate); 1.1133 + cpi->av_per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth 1.1134 + / cpi->output_framerate); 1.1135 + cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth * 1.1136 + cpi->oxcf.two_pass_vbrmin_section / 100); 1.1137 + 1.1138 + 1.1139 + cpi->min_frame_bandwidth = MAX(cpi->min_frame_bandwidth, FRAME_OVERHEAD_BITS); 1.1140 + 1.1141 + // Set Maximum gf/arf interval 1.1142 + cpi->max_gf_interval = 16; 1.1143 + 1.1144 + // Extended interval for genuinely static scenes 1.1145 + cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1; 1.1146 + 1.1147 + // Special conditions when alt ref frame enabled in lagged compress mode 1.1148 + if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames) { 1.1149 + if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1) 1.1150 + cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1; 1.1151 + 1.1152 + if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1) 1.1153 + cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1; 1.1154 + } 1.1155 + 1.1156 + if (cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval) 1.1157 + cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval; 1.1158 +} 1.1159 + 1.1160 +static int64_t rescale(int val, int64_t num, int denom) { 1.1161 + int64_t llnum = num; 1.1162 + int64_t llden = denom; 1.1163 + int64_t llval = val; 1.1164 + 1.1165 + return (llval * llnum / llden); 1.1166 +} 1.1167 + 1.1168 +static void set_tile_limits(VP9_COMP *cpi) { 1.1169 + VP9_COMMON *const cm = &cpi->common; 1.1170 + 1.1171 + int min_log2_tile_cols, max_log2_tile_cols; 1.1172 + vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols); 1.1173 + 1.1174 + cm->log2_tile_cols = clamp(cpi->oxcf.tile_columns, 1.1175 + min_log2_tile_cols, max_log2_tile_cols); 1.1176 + cm->log2_tile_rows = cpi->oxcf.tile_rows; 1.1177 +} 1.1178 + 1.1179 +static void init_config(VP9_PTR ptr, VP9_CONFIG *oxcf) { 1.1180 + VP9_COMP *cpi = (VP9_COMP *)(ptr); 1.1181 + VP9_COMMON *const cm = &cpi->common; 1.1182 + int i; 1.1183 + 1.1184 + cpi->oxcf = *oxcf; 1.1185 + 1.1186 + cm->version = oxcf->version; 1.1187 + 1.1188 + cm->width = oxcf->width; 1.1189 + cm->height = oxcf->height; 1.1190 + cm->subsampling_x = 0; 1.1191 + cm->subsampling_y = 0; 1.1192 + vp9_alloc_compressor_data(cpi); 1.1193 + 1.1194 + // change includes all joint functionality 1.1195 + vp9_change_config(ptr, oxcf); 1.1196 + 1.1197 + // Initialize active best and worst q and average q values. 1.1198 + cpi->active_worst_quality = cpi->oxcf.worst_allowed_q; 1.1199 + cpi->active_best_quality = cpi->oxcf.best_allowed_q; 1.1200 + cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q; 1.1201 + 1.1202 + // Initialise the starting buffer levels 1.1203 + cpi->buffer_level = cpi->oxcf.starting_buffer_level; 1.1204 + cpi->bits_off_target = cpi->oxcf.starting_buffer_level; 1.1205 + 1.1206 + cpi->rolling_target_bits = cpi->av_per_frame_bandwidth; 1.1207 + cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth; 1.1208 + cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth; 1.1209 + cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth; 1.1210 + 1.1211 + cpi->total_actual_bits = 0; 1.1212 + cpi->total_target_vs_actual = 0; 1.1213 + 1.1214 + cpi->static_mb_pct = 0; 1.1215 + 1.1216 + cpi->lst_fb_idx = 0; 1.1217 + cpi->gld_fb_idx = 1; 1.1218 + cpi->alt_fb_idx = 2; 1.1219 + 1.1220 + cpi->current_layer = 0; 1.1221 + cpi->use_svc = 0; 1.1222 + 1.1223 + set_tile_limits(cpi); 1.1224 + 1.1225 + cpi->fixed_divide[0] = 0; 1.1226 + for (i = 1; i < 512; i++) 1.1227 + cpi->fixed_divide[i] = 0x80000 / i; 1.1228 +} 1.1229 + 1.1230 + 1.1231 +void vp9_change_config(VP9_PTR ptr, VP9_CONFIG *oxcf) { 1.1232 + VP9_COMP *cpi = (VP9_COMP *)(ptr); 1.1233 + VP9_COMMON *const cm = &cpi->common; 1.1234 + 1.1235 + if (!cpi || !oxcf) 1.1236 + return; 1.1237 + 1.1238 + if (cm->version != oxcf->version) { 1.1239 + cm->version = oxcf->version; 1.1240 + } 1.1241 + 1.1242 + cpi->oxcf = *oxcf; 1.1243 + 1.1244 + switch (cpi->oxcf.Mode) { 1.1245 + // Real time and one pass deprecated in test code base 1.1246 + case MODE_GOODQUALITY: 1.1247 + cpi->pass = 0; 1.1248 + cpi->compressor_speed = 2; 1.1249 + cpi->oxcf.cpu_used = clamp(cpi->oxcf.cpu_used, -5, 5); 1.1250 + break; 1.1251 + 1.1252 + case MODE_FIRSTPASS: 1.1253 + cpi->pass = 1; 1.1254 + cpi->compressor_speed = 1; 1.1255 + break; 1.1256 + 1.1257 + case MODE_SECONDPASS: 1.1258 + cpi->pass = 2; 1.1259 + cpi->compressor_speed = 1; 1.1260 + cpi->oxcf.cpu_used = clamp(cpi->oxcf.cpu_used, -5, 5); 1.1261 + break; 1.1262 + 1.1263 + case MODE_SECONDPASS_BEST: 1.1264 + cpi->pass = 2; 1.1265 + cpi->compressor_speed = 0; 1.1266 + break; 1.1267 + } 1.1268 + 1.1269 + cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q]; 1.1270 + cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q]; 1.1271 + cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level]; 1.1272 + 1.1273 + cpi->oxcf.lossless = oxcf->lossless; 1.1274 + cpi->mb.e_mbd.itxm_add = cpi->oxcf.lossless ? vp9_iwht4x4_add 1.1275 + : vp9_idct4x4_add; 1.1276 + cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL; 1.1277 + 1.1278 + cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG; 1.1279 + 1.1280 + // cpi->use_golden_frame_only = 0; 1.1281 + // cpi->use_last_frame_only = 0; 1.1282 + cpi->refresh_golden_frame = 0; 1.1283 + cpi->refresh_last_frame = 1; 1.1284 + cm->refresh_frame_context = 1; 1.1285 + cm->reset_frame_context = 0; 1.1286 + 1.1287 + setup_features(cm); 1.1288 + cpi->common.allow_high_precision_mv = 0; // Default mv precision 1.1289 + set_mvcost(cpi); 1.1290 + 1.1291 + { 1.1292 + int i; 1.1293 + 1.1294 + for (i = 0; i < MAX_SEGMENTS; i++) 1.1295 + cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout; 1.1296 + } 1.1297 + 1.1298 + // At the moment the first order values may not be > MAXQ 1.1299 + cpi->oxcf.fixed_q = MIN(cpi->oxcf.fixed_q, MAXQ); 1.1300 + 1.1301 + // local file playback mode == really big buffer 1.1302 + if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK) { 1.1303 + cpi->oxcf.starting_buffer_level = 60000; 1.1304 + cpi->oxcf.optimal_buffer_level = 60000; 1.1305 + cpi->oxcf.maximum_buffer_size = 240000; 1.1306 + } 1.1307 + 1.1308 + // Convert target bandwidth from Kbit/s to Bit/s 1.1309 + cpi->oxcf.target_bandwidth *= 1000; 1.1310 + 1.1311 + cpi->oxcf.starting_buffer_level = rescale(cpi->oxcf.starting_buffer_level, 1.1312 + cpi->oxcf.target_bandwidth, 1000); 1.1313 + 1.1314 + // Set or reset optimal and maximum buffer levels. 1.1315 + if (cpi->oxcf.optimal_buffer_level == 0) 1.1316 + cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8; 1.1317 + else 1.1318 + cpi->oxcf.optimal_buffer_level = rescale(cpi->oxcf.optimal_buffer_level, 1.1319 + cpi->oxcf.target_bandwidth, 1000); 1.1320 + 1.1321 + if (cpi->oxcf.maximum_buffer_size == 0) 1.1322 + cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8; 1.1323 + else 1.1324 + cpi->oxcf.maximum_buffer_size = rescale(cpi->oxcf.maximum_buffer_size, 1.1325 + cpi->oxcf.target_bandwidth, 1000); 1.1326 + 1.1327 + // Set up frame rate and related parameters rate control values. 1.1328 + vp9_new_framerate(cpi, cpi->oxcf.framerate); 1.1329 + 1.1330 + // Set absolute upper and lower quality limits 1.1331 + cpi->worst_quality = cpi->oxcf.worst_allowed_q; 1.1332 + cpi->best_quality = cpi->oxcf.best_allowed_q; 1.1333 + 1.1334 + // active values should only be modified if out of new range 1.1335 + cpi->active_worst_quality = clamp(cpi->active_worst_quality, 1.1336 + cpi->oxcf.best_allowed_q, 1.1337 + cpi->oxcf.worst_allowed_q); 1.1338 + 1.1339 + cpi->active_best_quality = clamp(cpi->active_best_quality, 1.1340 + cpi->oxcf.best_allowed_q, 1.1341 + cpi->oxcf.worst_allowed_q); 1.1342 + 1.1343 + cpi->buffered_mode = cpi->oxcf.optimal_buffer_level > 0; 1.1344 + 1.1345 + cpi->cq_target_quality = cpi->oxcf.cq_level; 1.1346 + 1.1347 + cm->mcomp_filter_type = DEFAULT_INTERP_FILTER; 1.1348 + 1.1349 + cpi->target_bandwidth = cpi->oxcf.target_bandwidth; 1.1350 + 1.1351 + cm->display_width = cpi->oxcf.width; 1.1352 + cm->display_height = cpi->oxcf.height; 1.1353 + 1.1354 + // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs) 1.1355 + cpi->oxcf.Sharpness = MIN(7, cpi->oxcf.Sharpness); 1.1356 + 1.1357 + cpi->common.lf.sharpness_level = cpi->oxcf.Sharpness; 1.1358 + 1.1359 + if (cpi->initial_width) { 1.1360 + // Increasing the size of the frame beyond the first seen frame, or some 1.1361 + // otherwise signalled maximum size, is not supported. 1.1362 + // TODO(jkoleszar): exit gracefully. 1.1363 + assert(cm->width <= cpi->initial_width); 1.1364 + assert(cm->height <= cpi->initial_height); 1.1365 + } 1.1366 + update_frame_size(cpi); 1.1367 + 1.1368 + if (cpi->oxcf.fixed_q >= 0) { 1.1369 + cpi->last_q[0] = cpi->oxcf.fixed_q; 1.1370 + cpi->last_q[1] = cpi->oxcf.fixed_q; 1.1371 + cpi->last_boosted_qindex = cpi->oxcf.fixed_q; 1.1372 + } 1.1373 + 1.1374 + cpi->speed = cpi->oxcf.cpu_used; 1.1375 + 1.1376 + if (cpi->oxcf.lag_in_frames == 0) { 1.1377 + // force to allowlag to 0 if lag_in_frames is 0; 1.1378 + cpi->oxcf.allow_lag = 0; 1.1379 + } else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS) { 1.1380 + // Limit on lag buffers as these are not currently dynamically allocated 1.1381 + cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS; 1.1382 + } 1.1383 + 1.1384 + // YX Temp 1.1385 +#if CONFIG_MULTIPLE_ARF 1.1386 + vp9_zero(cpi->alt_ref_source); 1.1387 +#else 1.1388 + cpi->alt_ref_source = NULL; 1.1389 +#endif 1.1390 + cpi->is_src_frame_alt_ref = 0; 1.1391 + 1.1392 +#if 0 1.1393 + // Experimental RD Code 1.1394 + cpi->frame_distortion = 0; 1.1395 + cpi->last_frame_distortion = 0; 1.1396 +#endif 1.1397 + 1.1398 + set_tile_limits(cpi); 1.1399 +} 1.1400 + 1.1401 +#define M_LOG2_E 0.693147180559945309417 1.1402 +#define log2f(x) (log (x) / (float) M_LOG2_E) 1.1403 + 1.1404 +static void cal_nmvjointsadcost(int *mvjointsadcost) { 1.1405 + mvjointsadcost[0] = 600; 1.1406 + mvjointsadcost[1] = 300; 1.1407 + mvjointsadcost[2] = 300; 1.1408 + mvjointsadcost[0] = 300; 1.1409 +} 1.1410 + 1.1411 +static void cal_nmvsadcosts(int *mvsadcost[2]) { 1.1412 + int i = 1; 1.1413 + 1.1414 + mvsadcost[0][0] = 0; 1.1415 + mvsadcost[1][0] = 0; 1.1416 + 1.1417 + do { 1.1418 + double z = 256 * (2 * (log2f(8 * i) + .6)); 1.1419 + mvsadcost[0][i] = (int)z; 1.1420 + mvsadcost[1][i] = (int)z; 1.1421 + mvsadcost[0][-i] = (int)z; 1.1422 + mvsadcost[1][-i] = (int)z; 1.1423 + } while (++i <= MV_MAX); 1.1424 +} 1.1425 + 1.1426 +static void cal_nmvsadcosts_hp(int *mvsadcost[2]) { 1.1427 + int i = 1; 1.1428 + 1.1429 + mvsadcost[0][0] = 0; 1.1430 + mvsadcost[1][0] = 0; 1.1431 + 1.1432 + do { 1.1433 + double z = 256 * (2 * (log2f(8 * i) + .6)); 1.1434 + mvsadcost[0][i] = (int)z; 1.1435 + mvsadcost[1][i] = (int)z; 1.1436 + mvsadcost[0][-i] = (int)z; 1.1437 + mvsadcost[1][-i] = (int)z; 1.1438 + } while (++i <= MV_MAX); 1.1439 +} 1.1440 + 1.1441 +static void alloc_mode_context(VP9_COMMON *cm, int num_4x4_blk, 1.1442 + PICK_MODE_CONTEXT *ctx) { 1.1443 + int num_pix = num_4x4_blk << 4; 1.1444 + int i, k; 1.1445 + ctx->num_4x4_blk = num_4x4_blk; 1.1446 + CHECK_MEM_ERROR(cm, ctx->zcoeff_blk, 1.1447 + vpx_calloc(num_4x4_blk, sizeof(uint8_t))); 1.1448 + for (i = 0; i < MAX_MB_PLANE; ++i) { 1.1449 + for (k = 0; k < 3; ++k) { 1.1450 + CHECK_MEM_ERROR(cm, ctx->coeff[i][k], 1.1451 + vpx_memalign(16, num_pix * sizeof(int16_t))); 1.1452 + CHECK_MEM_ERROR(cm, ctx->qcoeff[i][k], 1.1453 + vpx_memalign(16, num_pix * sizeof(int16_t))); 1.1454 + CHECK_MEM_ERROR(cm, ctx->dqcoeff[i][k], 1.1455 + vpx_memalign(16, num_pix * sizeof(int16_t))); 1.1456 + CHECK_MEM_ERROR(cm, ctx->eobs[i][k], 1.1457 + vpx_memalign(16, num_pix * sizeof(uint16_t))); 1.1458 + ctx->coeff_pbuf[i][k] = ctx->coeff[i][k]; 1.1459 + ctx->qcoeff_pbuf[i][k] = ctx->qcoeff[i][k]; 1.1460 + ctx->dqcoeff_pbuf[i][k] = ctx->dqcoeff[i][k]; 1.1461 + ctx->eobs_pbuf[i][k] = ctx->eobs[i][k]; 1.1462 + } 1.1463 + } 1.1464 +} 1.1465 + 1.1466 +static void free_mode_context(PICK_MODE_CONTEXT *ctx) { 1.1467 + int i, k; 1.1468 + vpx_free(ctx->zcoeff_blk); 1.1469 + ctx->zcoeff_blk = 0; 1.1470 + for (i = 0; i < MAX_MB_PLANE; ++i) { 1.1471 + for (k = 0; k < 3; ++k) { 1.1472 + vpx_free(ctx->coeff[i][k]); 1.1473 + ctx->coeff[i][k] = 0; 1.1474 + vpx_free(ctx->qcoeff[i][k]); 1.1475 + ctx->qcoeff[i][k] = 0; 1.1476 + vpx_free(ctx->dqcoeff[i][k]); 1.1477 + ctx->dqcoeff[i][k] = 0; 1.1478 + vpx_free(ctx->eobs[i][k]); 1.1479 + ctx->eobs[i][k] = 0; 1.1480 + } 1.1481 + } 1.1482 +} 1.1483 + 1.1484 +static void init_pick_mode_context(VP9_COMP *cpi) { 1.1485 + int i; 1.1486 + VP9_COMMON *const cm = &cpi->common; 1.1487 + MACROBLOCK *const x = &cpi->mb; 1.1488 + 1.1489 + 1.1490 + for (i = 0; i < BLOCK_SIZES; ++i) { 1.1491 + const int num_4x4_w = num_4x4_blocks_wide_lookup[i]; 1.1492 + const int num_4x4_h = num_4x4_blocks_high_lookup[i]; 1.1493 + const int num_4x4_blk = MAX(4, num_4x4_w * num_4x4_h); 1.1494 + if (i < BLOCK_16X16) { 1.1495 + for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) { 1.1496 + for (x->mb_index = 0; x->mb_index < 4; ++x->mb_index) { 1.1497 + for (x->b_index = 0; x->b_index < 16 / num_4x4_blk; ++x->b_index) { 1.1498 + PICK_MODE_CONTEXT *ctx = get_block_context(x, i); 1.1499 + alloc_mode_context(cm, num_4x4_blk, ctx); 1.1500 + } 1.1501 + } 1.1502 + } 1.1503 + } else if (i < BLOCK_32X32) { 1.1504 + for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) { 1.1505 + for (x->mb_index = 0; x->mb_index < 64 / num_4x4_blk; ++x->mb_index) { 1.1506 + PICK_MODE_CONTEXT *ctx = get_block_context(x, i); 1.1507 + ctx->num_4x4_blk = num_4x4_blk; 1.1508 + alloc_mode_context(cm, num_4x4_blk, ctx); 1.1509 + } 1.1510 + } 1.1511 + } else if (i < BLOCK_64X64) { 1.1512 + for (x->sb_index = 0; x->sb_index < 256 / num_4x4_blk; ++x->sb_index) { 1.1513 + PICK_MODE_CONTEXT *ctx = get_block_context(x, i); 1.1514 + ctx->num_4x4_blk = num_4x4_blk; 1.1515 + alloc_mode_context(cm, num_4x4_blk, ctx); 1.1516 + } 1.1517 + } else { 1.1518 + PICK_MODE_CONTEXT *ctx = get_block_context(x, i); 1.1519 + ctx->num_4x4_blk = num_4x4_blk; 1.1520 + alloc_mode_context(cm, num_4x4_blk, ctx); 1.1521 + } 1.1522 + } 1.1523 +} 1.1524 + 1.1525 +static void free_pick_mode_context(MACROBLOCK *x) { 1.1526 + int i; 1.1527 + 1.1528 + for (i = 0; i < BLOCK_SIZES; ++i) { 1.1529 + const int num_4x4_w = num_4x4_blocks_wide_lookup[i]; 1.1530 + const int num_4x4_h = num_4x4_blocks_high_lookup[i]; 1.1531 + const int num_4x4_blk = MAX(4, num_4x4_w * num_4x4_h); 1.1532 + if (i < BLOCK_16X16) { 1.1533 + for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) { 1.1534 + for (x->mb_index = 0; x->mb_index < 4; ++x->mb_index) { 1.1535 + for (x->b_index = 0; x->b_index < 16 / num_4x4_blk; ++x->b_index) { 1.1536 + PICK_MODE_CONTEXT *ctx = get_block_context(x, i); 1.1537 + free_mode_context(ctx); 1.1538 + } 1.1539 + } 1.1540 + } 1.1541 + } else if (i < BLOCK_32X32) { 1.1542 + for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) { 1.1543 + for (x->mb_index = 0; x->mb_index < 64 / num_4x4_blk; ++x->mb_index) { 1.1544 + PICK_MODE_CONTEXT *ctx = get_block_context(x, i); 1.1545 + free_mode_context(ctx); 1.1546 + } 1.1547 + } 1.1548 + } else if (i < BLOCK_64X64) { 1.1549 + for (x->sb_index = 0; x->sb_index < 256 / num_4x4_blk; ++x->sb_index) { 1.1550 + PICK_MODE_CONTEXT *ctx = get_block_context(x, i); 1.1551 + free_mode_context(ctx); 1.1552 + } 1.1553 + } else { 1.1554 + PICK_MODE_CONTEXT *ctx = get_block_context(x, i); 1.1555 + free_mode_context(ctx); 1.1556 + } 1.1557 + } 1.1558 +} 1.1559 + 1.1560 +VP9_PTR vp9_create_compressor(VP9_CONFIG *oxcf) { 1.1561 + int i, j; 1.1562 + volatile union { 1.1563 + VP9_COMP *cpi; 1.1564 + VP9_PTR ptr; 1.1565 + } ctx; 1.1566 + 1.1567 + VP9_COMP *cpi; 1.1568 + VP9_COMMON *cm; 1.1569 + 1.1570 + cpi = ctx.cpi = vpx_memalign(32, sizeof(VP9_COMP)); 1.1571 + // Check that the CPI instance is valid 1.1572 + if (!cpi) 1.1573 + return 0; 1.1574 + 1.1575 + cm = &cpi->common; 1.1576 + 1.1577 + vp9_zero(*cpi); 1.1578 + 1.1579 + if (setjmp(cm->error.jmp)) { 1.1580 + VP9_PTR ptr = ctx.ptr; 1.1581 + 1.1582 + ctx.cpi->common.error.setjmp = 0; 1.1583 + vp9_remove_compressor(&ptr); 1.1584 + return 0; 1.1585 + } 1.1586 + 1.1587 + cm->error.setjmp = 1; 1.1588 + 1.1589 + CHECK_MEM_ERROR(cm, cpi->mb.ss, vpx_calloc(sizeof(search_site), 1.1590 + (MAX_MVSEARCH_STEPS * 8) + 1)); 1.1591 + 1.1592 + vp9_create_common(cm); 1.1593 + 1.1594 + init_config((VP9_PTR)cpi, oxcf); 1.1595 + 1.1596 + init_pick_mode_context(cpi); 1.1597 + 1.1598 + cm->current_video_frame = 0; 1.1599 + cpi->kf_overspend_bits = 0; 1.1600 + cpi->kf_bitrate_adjustment = 0; 1.1601 + cpi->frames_till_gf_update_due = 0; 1.1602 + cpi->gf_overspend_bits = 0; 1.1603 + cpi->non_gf_bitrate_adjustment = 0; 1.1604 + 1.1605 + // Set reference frame sign bias for ALTREF frame to 1 (for now) 1.1606 + cm->ref_frame_sign_bias[ALTREF_FRAME] = 1; 1.1607 + 1.1608 + cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL; 1.1609 + 1.1610 + cpi->gold_is_last = 0; 1.1611 + cpi->alt_is_last = 0; 1.1612 + cpi->gold_is_alt = 0; 1.1613 + 1.1614 + // Spatial scalability 1.1615 + cpi->number_spatial_layers = oxcf->ss_number_layers; 1.1616 + 1.1617 + // Create the encoder segmentation map and set all entries to 0 1.1618 + CHECK_MEM_ERROR(cm, cpi->segmentation_map, 1.1619 + vpx_calloc(cm->mi_rows * cm->mi_cols, 1)); 1.1620 + 1.1621 + // And a place holder structure is the coding context 1.1622 + // for use if we want to save and restore it 1.1623 + CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy, 1.1624 + vpx_calloc(cm->mi_rows * cm->mi_cols, 1)); 1.1625 + 1.1626 + CHECK_MEM_ERROR(cm, cpi->active_map, vpx_calloc(cm->MBs, 1)); 1.1627 + vpx_memset(cpi->active_map, 1, cm->MBs); 1.1628 + cpi->active_map_enabled = 0; 1.1629 + 1.1630 + for (i = 0; i < (sizeof(cpi->mbgraph_stats) / 1.1631 + sizeof(cpi->mbgraph_stats[0])); i++) { 1.1632 + CHECK_MEM_ERROR(cm, cpi->mbgraph_stats[i].mb_stats, 1.1633 + vpx_calloc(cm->MBs * 1.1634 + sizeof(*cpi->mbgraph_stats[i].mb_stats), 1)); 1.1635 + } 1.1636 + 1.1637 +#ifdef ENTROPY_STATS 1.1638 + if (cpi->pass != 1) 1.1639 + init_context_counters(); 1.1640 +#endif 1.1641 + 1.1642 +#ifdef MODE_STATS 1.1643 + init_tx_count_stats(); 1.1644 + init_switchable_interp_stats(); 1.1645 +#endif 1.1646 + 1.1647 + /*Initialize the feed-forward activity masking.*/ 1.1648 + cpi->activity_avg = 90 << 12; 1.1649 + 1.1650 + cpi->frames_since_key = 8; // Sensible default for first frame. 1.1651 + cpi->key_frame_frequency = cpi->oxcf.key_freq; 1.1652 + cpi->this_key_frame_forced = 0; 1.1653 + cpi->next_key_frame_forced = 0; 1.1654 + 1.1655 + cpi->source_alt_ref_pending = 0; 1.1656 + cpi->source_alt_ref_active = 0; 1.1657 + cpi->refresh_alt_ref_frame = 0; 1.1658 + 1.1659 +#if CONFIG_MULTIPLE_ARF 1.1660 + // Turn multiple ARF usage on/off. This is a quick hack for the initial test 1.1661 + // version. It should eventually be set via the codec API. 1.1662 + cpi->multi_arf_enabled = 1; 1.1663 + 1.1664 + if (cpi->multi_arf_enabled) { 1.1665 + cpi->sequence_number = 0; 1.1666 + cpi->frame_coding_order_period = 0; 1.1667 + vp9_zero(cpi->frame_coding_order); 1.1668 + vp9_zero(cpi->arf_buffer_idx); 1.1669 + } 1.1670 +#endif 1.1671 + 1.1672 + cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS; 1.1673 +#if CONFIG_INTERNAL_STATS 1.1674 + cpi->b_calculate_ssimg = 0; 1.1675 + 1.1676 + cpi->count = 0; 1.1677 + cpi->bytes = 0; 1.1678 + 1.1679 + if (cpi->b_calculate_psnr) { 1.1680 + cpi->total_sq_error = 0.0; 1.1681 + cpi->total_sq_error2 = 0.0; 1.1682 + cpi->total_y = 0.0; 1.1683 + cpi->total_u = 0.0; 1.1684 + cpi->total_v = 0.0; 1.1685 + cpi->total = 0.0; 1.1686 + cpi->totalp_y = 0.0; 1.1687 + cpi->totalp_u = 0.0; 1.1688 + cpi->totalp_v = 0.0; 1.1689 + cpi->totalp = 0.0; 1.1690 + cpi->tot_recode_hits = 0; 1.1691 + cpi->summed_quality = 0; 1.1692 + cpi->summed_weights = 0; 1.1693 + cpi->summedp_quality = 0; 1.1694 + cpi->summedp_weights = 0; 1.1695 + } 1.1696 + 1.1697 + if (cpi->b_calculate_ssimg) { 1.1698 + cpi->total_ssimg_y = 0; 1.1699 + cpi->total_ssimg_u = 0; 1.1700 + cpi->total_ssimg_v = 0; 1.1701 + cpi->total_ssimg_all = 0; 1.1702 + } 1.1703 + 1.1704 +#endif 1.1705 + 1.1706 + cpi->first_time_stamp_ever = INT64_MAX; 1.1707 + 1.1708 + cpi->frames_till_gf_update_due = 0; 1.1709 + cpi->key_frame_count = 1; 1.1710 + 1.1711 + cpi->ni_av_qi = cpi->oxcf.worst_allowed_q; 1.1712 + cpi->ni_tot_qi = 0; 1.1713 + cpi->ni_frames = 0; 1.1714 + cpi->tot_q = 0.0; 1.1715 + cpi->avg_q = vp9_convert_qindex_to_q(cpi->oxcf.worst_allowed_q); 1.1716 + cpi->total_byte_count = 0; 1.1717 + 1.1718 + cpi->rate_correction_factor = 1.0; 1.1719 + cpi->key_frame_rate_correction_factor = 1.0; 1.1720 + cpi->gf_rate_correction_factor = 1.0; 1.1721 + cpi->twopass.est_max_qcorrection_factor = 1.0; 1.1722 + 1.1723 + cal_nmvjointsadcost(cpi->mb.nmvjointsadcost); 1.1724 + cpi->mb.nmvcost[0] = &cpi->mb.nmvcosts[0][MV_MAX]; 1.1725 + cpi->mb.nmvcost[1] = &cpi->mb.nmvcosts[1][MV_MAX]; 1.1726 + cpi->mb.nmvsadcost[0] = &cpi->mb.nmvsadcosts[0][MV_MAX]; 1.1727 + cpi->mb.nmvsadcost[1] = &cpi->mb.nmvsadcosts[1][MV_MAX]; 1.1728 + cal_nmvsadcosts(cpi->mb.nmvsadcost); 1.1729 + 1.1730 + cpi->mb.nmvcost_hp[0] = &cpi->mb.nmvcosts_hp[0][MV_MAX]; 1.1731 + cpi->mb.nmvcost_hp[1] = &cpi->mb.nmvcosts_hp[1][MV_MAX]; 1.1732 + cpi->mb.nmvsadcost_hp[0] = &cpi->mb.nmvsadcosts_hp[0][MV_MAX]; 1.1733 + cpi->mb.nmvsadcost_hp[1] = &cpi->mb.nmvsadcosts_hp[1][MV_MAX]; 1.1734 + cal_nmvsadcosts_hp(cpi->mb.nmvsadcost_hp); 1.1735 + 1.1736 + for (i = 0; i < KEY_FRAME_CONTEXT; i++) 1.1737 + cpi->prior_key_frame_distance[i] = (int)cpi->output_framerate; 1.1738 + 1.1739 +#ifdef OUTPUT_YUV_SRC 1.1740 + yuv_file = fopen("bd.yuv", "ab"); 1.1741 +#endif 1.1742 +#ifdef OUTPUT_YUV_REC 1.1743 + yuv_rec_file = fopen("rec.yuv", "wb"); 1.1744 +#endif 1.1745 + 1.1746 +#if 0 1.1747 + framepsnr = fopen("framepsnr.stt", "a"); 1.1748 + kf_list = fopen("kf_list.stt", "w"); 1.1749 +#endif 1.1750 + 1.1751 + cpi->output_pkt_list = oxcf->output_pkt_list; 1.1752 + 1.1753 + cpi->enable_encode_breakout = 1; 1.1754 + 1.1755 + if (cpi->pass == 1) { 1.1756 + vp9_init_first_pass(cpi); 1.1757 + } else if (cpi->pass == 2) { 1.1758 + size_t packet_sz = sizeof(FIRSTPASS_STATS); 1.1759 + int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz); 1.1760 + 1.1761 + cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf; 1.1762 + cpi->twopass.stats_in = cpi->twopass.stats_in_start; 1.1763 + cpi->twopass.stats_in_end = (void *)((char *)cpi->twopass.stats_in 1.1764 + + (packets - 1) * packet_sz); 1.1765 + vp9_init_second_pass(cpi); 1.1766 + } 1.1767 + 1.1768 + vp9_set_speed_features(cpi); 1.1769 + 1.1770 + // Default rd threshold factors for mode selection 1.1771 + for (i = 0; i < BLOCK_SIZES; ++i) { 1.1772 + for (j = 0; j < MAX_MODES; ++j) 1.1773 + cpi->rd_thresh_freq_fact[i][j] = 32; 1.1774 + for (j = 0; j < MAX_REFS; ++j) 1.1775 + cpi->rd_thresh_freq_sub8x8[i][j] = 32; 1.1776 + } 1.1777 + 1.1778 +#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SVFHH, SVFHV, SVFHHV, \ 1.1779 + SDX3F, SDX8F, SDX4DF)\ 1.1780 + cpi->fn_ptr[BT].sdf = SDF; \ 1.1781 + cpi->fn_ptr[BT].sdaf = SDAF; \ 1.1782 + cpi->fn_ptr[BT].vf = VF; \ 1.1783 + cpi->fn_ptr[BT].svf = SVF; \ 1.1784 + cpi->fn_ptr[BT].svaf = SVAF; \ 1.1785 + cpi->fn_ptr[BT].svf_halfpix_h = SVFHH; \ 1.1786 + cpi->fn_ptr[BT].svf_halfpix_v = SVFHV; \ 1.1787 + cpi->fn_ptr[BT].svf_halfpix_hv = SVFHHV; \ 1.1788 + cpi->fn_ptr[BT].sdx3f = SDX3F; \ 1.1789 + cpi->fn_ptr[BT].sdx8f = SDX8F; \ 1.1790 + cpi->fn_ptr[BT].sdx4df = SDX4DF; 1.1791 + 1.1792 + BFP(BLOCK_32X16, vp9_sad32x16, vp9_sad32x16_avg, 1.1793 + vp9_variance32x16, vp9_sub_pixel_variance32x16, 1.1794 + vp9_sub_pixel_avg_variance32x16, NULL, NULL, 1.1795 + NULL, NULL, NULL, 1.1796 + vp9_sad32x16x4d) 1.1797 + 1.1798 + BFP(BLOCK_16X32, vp9_sad16x32, vp9_sad16x32_avg, 1.1799 + vp9_variance16x32, vp9_sub_pixel_variance16x32, 1.1800 + vp9_sub_pixel_avg_variance16x32, NULL, NULL, 1.1801 + NULL, NULL, NULL, 1.1802 + vp9_sad16x32x4d) 1.1803 + 1.1804 + BFP(BLOCK_64X32, vp9_sad64x32, vp9_sad64x32_avg, 1.1805 + vp9_variance64x32, vp9_sub_pixel_variance64x32, 1.1806 + vp9_sub_pixel_avg_variance64x32, NULL, NULL, 1.1807 + NULL, NULL, NULL, 1.1808 + vp9_sad64x32x4d) 1.1809 + 1.1810 + BFP(BLOCK_32X64, vp9_sad32x64, vp9_sad32x64_avg, 1.1811 + vp9_variance32x64, vp9_sub_pixel_variance32x64, 1.1812 + vp9_sub_pixel_avg_variance32x64, NULL, NULL, 1.1813 + NULL, NULL, NULL, 1.1814 + vp9_sad32x64x4d) 1.1815 + 1.1816 + BFP(BLOCK_32X32, vp9_sad32x32, vp9_sad32x32_avg, 1.1817 + vp9_variance32x32, vp9_sub_pixel_variance32x32, 1.1818 + vp9_sub_pixel_avg_variance32x32, vp9_variance_halfpixvar32x32_h, 1.1819 + vp9_variance_halfpixvar32x32_v, 1.1820 + vp9_variance_halfpixvar32x32_hv, vp9_sad32x32x3, vp9_sad32x32x8, 1.1821 + vp9_sad32x32x4d) 1.1822 + 1.1823 + BFP(BLOCK_64X64, vp9_sad64x64, vp9_sad64x64_avg, 1.1824 + vp9_variance64x64, vp9_sub_pixel_variance64x64, 1.1825 + vp9_sub_pixel_avg_variance64x64, vp9_variance_halfpixvar64x64_h, 1.1826 + vp9_variance_halfpixvar64x64_v, 1.1827 + vp9_variance_halfpixvar64x64_hv, vp9_sad64x64x3, vp9_sad64x64x8, 1.1828 + vp9_sad64x64x4d) 1.1829 + 1.1830 + BFP(BLOCK_16X16, vp9_sad16x16, vp9_sad16x16_avg, 1.1831 + vp9_variance16x16, vp9_sub_pixel_variance16x16, 1.1832 + vp9_sub_pixel_avg_variance16x16, vp9_variance_halfpixvar16x16_h, 1.1833 + vp9_variance_halfpixvar16x16_v, 1.1834 + vp9_variance_halfpixvar16x16_hv, vp9_sad16x16x3, vp9_sad16x16x8, 1.1835 + vp9_sad16x16x4d) 1.1836 + 1.1837 + BFP(BLOCK_16X8, vp9_sad16x8, vp9_sad16x8_avg, 1.1838 + vp9_variance16x8, vp9_sub_pixel_variance16x8, 1.1839 + vp9_sub_pixel_avg_variance16x8, NULL, NULL, NULL, 1.1840 + vp9_sad16x8x3, vp9_sad16x8x8, vp9_sad16x8x4d) 1.1841 + 1.1842 + BFP(BLOCK_8X16, vp9_sad8x16, vp9_sad8x16_avg, 1.1843 + vp9_variance8x16, vp9_sub_pixel_variance8x16, 1.1844 + vp9_sub_pixel_avg_variance8x16, NULL, NULL, NULL, 1.1845 + vp9_sad8x16x3, vp9_sad8x16x8, vp9_sad8x16x4d) 1.1846 + 1.1847 + BFP(BLOCK_8X8, vp9_sad8x8, vp9_sad8x8_avg, 1.1848 + vp9_variance8x8, vp9_sub_pixel_variance8x8, 1.1849 + vp9_sub_pixel_avg_variance8x8, NULL, NULL, NULL, 1.1850 + vp9_sad8x8x3, vp9_sad8x8x8, vp9_sad8x8x4d) 1.1851 + 1.1852 + BFP(BLOCK_8X4, vp9_sad8x4, vp9_sad8x4_avg, 1.1853 + vp9_variance8x4, vp9_sub_pixel_variance8x4, 1.1854 + vp9_sub_pixel_avg_variance8x4, NULL, NULL, 1.1855 + NULL, NULL, vp9_sad8x4x8, 1.1856 + vp9_sad8x4x4d) 1.1857 + 1.1858 + BFP(BLOCK_4X8, vp9_sad4x8, vp9_sad4x8_avg, 1.1859 + vp9_variance4x8, vp9_sub_pixel_variance4x8, 1.1860 + vp9_sub_pixel_avg_variance4x8, NULL, NULL, 1.1861 + NULL, NULL, vp9_sad4x8x8, 1.1862 + vp9_sad4x8x4d) 1.1863 + 1.1864 + BFP(BLOCK_4X4, vp9_sad4x4, vp9_sad4x4_avg, 1.1865 + vp9_variance4x4, vp9_sub_pixel_variance4x4, 1.1866 + vp9_sub_pixel_avg_variance4x4, NULL, NULL, NULL, 1.1867 + vp9_sad4x4x3, vp9_sad4x4x8, vp9_sad4x4x4d) 1.1868 + 1.1869 + cpi->full_search_sad = vp9_full_search_sad; 1.1870 + cpi->diamond_search_sad = vp9_diamond_search_sad; 1.1871 + cpi->refining_search_sad = vp9_refining_search_sad; 1.1872 + 1.1873 + // make sure frame 1 is okay 1.1874 + cpi->error_bins[0] = cpi->common.MBs; 1.1875 + 1.1876 + /* vp9_init_quantizer() is first called here. Add check in 1.1877 + * vp9_frame_init_quantizer() so that vp9_init_quantizer is only 1.1878 + * called later when needed. This will avoid unnecessary calls of 1.1879 + * vp9_init_quantizer() for every frame. 1.1880 + */ 1.1881 + vp9_init_quantizer(cpi); 1.1882 + 1.1883 + vp9_loop_filter_init(cm); 1.1884 + 1.1885 + cpi->common.error.setjmp = 0; 1.1886 + 1.1887 + vp9_zero(cpi->y_uv_mode_count); 1.1888 + 1.1889 +#ifdef MODE_TEST_HIT_STATS 1.1890 + vp9_zero(cpi->mode_test_hits); 1.1891 +#endif 1.1892 + 1.1893 + return (VP9_PTR) cpi; 1.1894 +} 1.1895 + 1.1896 +void vp9_remove_compressor(VP9_PTR *ptr) { 1.1897 + VP9_COMP *cpi = (VP9_COMP *)(*ptr); 1.1898 + int i; 1.1899 + 1.1900 + if (!cpi) 1.1901 + return; 1.1902 + 1.1903 + if (cpi && (cpi->common.current_video_frame > 0)) { 1.1904 + if (cpi->pass == 2) { 1.1905 + vp9_end_second_pass(cpi); 1.1906 + } 1.1907 + 1.1908 +#ifdef ENTROPY_STATS 1.1909 + if (cpi->pass != 1) { 1.1910 + print_context_counters(); 1.1911 + print_tree_update_probs(); 1.1912 + print_mode_context(cpi); 1.1913 + } 1.1914 +#endif 1.1915 + 1.1916 +#ifdef MODE_STATS 1.1917 + if (cpi->pass != 1) { 1.1918 + write_tx_count_stats(); 1.1919 + write_switchable_interp_stats(); 1.1920 + } 1.1921 +#endif 1.1922 + 1.1923 +#if CONFIG_INTERNAL_STATS 1.1924 + 1.1925 + vp9_clear_system_state(); 1.1926 + 1.1927 + // printf("\n8x8-4x4:%d-%d\n", cpi->t8x8_count, cpi->t4x4_count); 1.1928 + if (cpi->pass != 1) { 1.1929 + FILE *f = fopen("opsnr.stt", "a"); 1.1930 + double time_encoded = (cpi->last_end_time_stamp_seen 1.1931 + - cpi->first_time_stamp_ever) / 10000000.000; 1.1932 + double total_encode_time = (cpi->time_receive_data + 1.1933 + cpi->time_compress_data) / 1000.000; 1.1934 + double dr = (double)cpi->bytes * (double) 8 / (double)1000 1.1935 + / time_encoded; 1.1936 + 1.1937 + if (cpi->b_calculate_psnr) { 1.1938 + YV12_BUFFER_CONFIG *lst_yv12 = 1.1939 + &cpi->common.yv12_fb[cpi->common.ref_frame_map[cpi->lst_fb_idx]]; 1.1940 + double samples = 3.0 / 2 * cpi->count * 1.1941 + lst_yv12->y_width * lst_yv12->y_height; 1.1942 + double total_psnr = vp9_mse2psnr(samples, 255.0, cpi->total_sq_error); 1.1943 + double total_psnr2 = vp9_mse2psnr(samples, 255.0, cpi->total_sq_error2); 1.1944 + double total_ssim = 100 * pow(cpi->summed_quality / 1.1945 + cpi->summed_weights, 8.0); 1.1946 + double total_ssimp = 100 * pow(cpi->summedp_quality / 1.1947 + cpi->summedp_weights, 8.0); 1.1948 + 1.1949 + fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t" 1.1950 + "VPXSSIM\tVPSSIMP\t Time(ms)\n"); 1.1951 + fprintf(f, "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%8.0f\n", 1.1952 + dr, cpi->total / cpi->count, total_psnr, 1.1953 + cpi->totalp / cpi->count, total_psnr2, total_ssim, total_ssimp, 1.1954 + total_encode_time); 1.1955 + } 1.1956 + 1.1957 + if (cpi->b_calculate_ssimg) { 1.1958 + fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t Time(ms)\n"); 1.1959 + fprintf(f, "%7.2f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr, 1.1960 + cpi->total_ssimg_y / cpi->count, 1.1961 + cpi->total_ssimg_u / cpi->count, 1.1962 + cpi->total_ssimg_v / cpi->count, 1.1963 + cpi->total_ssimg_all / cpi->count, total_encode_time); 1.1964 + } 1.1965 + 1.1966 + fclose(f); 1.1967 + } 1.1968 + 1.1969 +#endif 1.1970 + 1.1971 +#ifdef MODE_TEST_HIT_STATS 1.1972 + if (cpi->pass != 1) { 1.1973 + double norm_per_pixel_mode_tests = 0; 1.1974 + double norm_counts[BLOCK_SIZES]; 1.1975 + int i; 1.1976 + int sb64_per_frame; 1.1977 + int norm_factors[BLOCK_SIZES] = 1.1978 + {256, 128, 128, 64, 32, 32, 16, 8, 8, 4, 2, 2, 1}; 1.1979 + FILE *f = fopen("mode_hit_stats.stt", "a"); 1.1980 + 1.1981 + // On average, how many mode tests do we do 1.1982 + for (i = 0; i < BLOCK_SIZES; ++i) { 1.1983 + norm_counts[i] = (double)cpi->mode_test_hits[i] / 1.1984 + (double)norm_factors[i]; 1.1985 + norm_per_pixel_mode_tests += norm_counts[i]; 1.1986 + } 1.1987 + // Convert to a number per 64x64 and per frame 1.1988 + sb64_per_frame = ((cpi->common.height + 63) / 64) * 1.1989 + ((cpi->common.width + 63) / 64); 1.1990 + norm_per_pixel_mode_tests = 1.1991 + norm_per_pixel_mode_tests / 1.1992 + (double)(cpi->common.current_video_frame * sb64_per_frame); 1.1993 + 1.1994 + fprintf(f, "%6.4f\n", norm_per_pixel_mode_tests); 1.1995 + fclose(f); 1.1996 + } 1.1997 +#endif 1.1998 + 1.1999 +#ifdef ENTROPY_STATS 1.2000 + { 1.2001 + int i, j, k; 1.2002 + FILE *fmode = fopen("vp9_modecontext.c", "w"); 1.2003 + 1.2004 + fprintf(fmode, "\n#include \"vp9_entropymode.h\"\n\n"); 1.2005 + fprintf(fmode, "const unsigned int vp9_kf_default_bmode_counts "); 1.2006 + fprintf(fmode, "[INTRA_MODES][INTRA_MODES]" 1.2007 + "[INTRA_MODES] =\n{\n"); 1.2008 + 1.2009 + for (i = 0; i < INTRA_MODES; i++) { 1.2010 + fprintf(fmode, " { // Above Mode : %d\n", i); 1.2011 + 1.2012 + for (j = 0; j < INTRA_MODES; j++) { 1.2013 + fprintf(fmode, " {"); 1.2014 + 1.2015 + for (k = 0; k < INTRA_MODES; k++) { 1.2016 + if (!intra_mode_stats[i][j][k]) 1.2017 + fprintf(fmode, " %5d, ", 1); 1.2018 + else 1.2019 + fprintf(fmode, " %5d, ", intra_mode_stats[i][j][k]); 1.2020 + } 1.2021 + 1.2022 + fprintf(fmode, "}, // left_mode %d\n", j); 1.2023 + } 1.2024 + 1.2025 + fprintf(fmode, " },\n"); 1.2026 + } 1.2027 + 1.2028 + fprintf(fmode, "};\n"); 1.2029 + fclose(fmode); 1.2030 + } 1.2031 +#endif 1.2032 + 1.2033 + 1.2034 +#if defined(SECTIONBITS_OUTPUT) 1.2035 + 1.2036 + if (0) { 1.2037 + int i; 1.2038 + FILE *f = fopen("tokenbits.stt", "a"); 1.2039 + 1.2040 + for (i = 0; i < 28; i++) 1.2041 + fprintf(f, "%8d", (int)(Sectionbits[i] / 256)); 1.2042 + 1.2043 + fprintf(f, "\n"); 1.2044 + fclose(f); 1.2045 + } 1.2046 + 1.2047 +#endif 1.2048 + 1.2049 +#if 0 1.2050 + { 1.2051 + printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000); 1.2052 + printf("\n_frames recive_data encod_mb_row compress_frame Total\n"); 1.2053 + printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame, 1.2054 + cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000, 1.2055 + cpi->time_compress_data / 1000, 1.2056 + (cpi->time_receive_data + cpi->time_compress_data) / 1000); 1.2057 + } 1.2058 +#endif 1.2059 + } 1.2060 + 1.2061 + free_pick_mode_context(&cpi->mb); 1.2062 + dealloc_compressor_data(cpi); 1.2063 + vpx_free(cpi->mb.ss); 1.2064 + vpx_free(cpi->tok); 1.2065 + 1.2066 + for (i = 0; i < sizeof(cpi->mbgraph_stats) / 1.2067 + sizeof(cpi->mbgraph_stats[0]); ++i) { 1.2068 + vpx_free(cpi->mbgraph_stats[i].mb_stats); 1.2069 + } 1.2070 + 1.2071 + vp9_remove_common(&cpi->common); 1.2072 + vpx_free(cpi); 1.2073 + *ptr = 0; 1.2074 + 1.2075 +#ifdef OUTPUT_YUV_SRC 1.2076 + fclose(yuv_file); 1.2077 +#endif 1.2078 +#ifdef OUTPUT_YUV_REC 1.2079 + fclose(yuv_rec_file); 1.2080 +#endif 1.2081 + 1.2082 +#if 0 1.2083 + 1.2084 + if (keyfile) 1.2085 + fclose(keyfile); 1.2086 + 1.2087 + if (framepsnr) 1.2088 + fclose(framepsnr); 1.2089 + 1.2090 + if (kf_list) 1.2091 + fclose(kf_list); 1.2092 + 1.2093 +#endif 1.2094 +} 1.2095 + 1.2096 + 1.2097 +static uint64_t calc_plane_error(uint8_t *orig, int orig_stride, 1.2098 + uint8_t *recon, int recon_stride, 1.2099 + unsigned int cols, unsigned int rows) { 1.2100 + unsigned int row, col; 1.2101 + uint64_t total_sse = 0; 1.2102 + int diff; 1.2103 + 1.2104 + for (row = 0; row + 16 <= rows; row += 16) { 1.2105 + for (col = 0; col + 16 <= cols; col += 16) { 1.2106 + unsigned int sse; 1.2107 + 1.2108 + vp9_mse16x16(orig + col, orig_stride, recon + col, recon_stride, &sse); 1.2109 + total_sse += sse; 1.2110 + } 1.2111 + 1.2112 + /* Handle odd-sized width */ 1.2113 + if (col < cols) { 1.2114 + unsigned int border_row, border_col; 1.2115 + uint8_t *border_orig = orig; 1.2116 + uint8_t *border_recon = recon; 1.2117 + 1.2118 + for (border_row = 0; border_row < 16; border_row++) { 1.2119 + for (border_col = col; border_col < cols; border_col++) { 1.2120 + diff = border_orig[border_col] - border_recon[border_col]; 1.2121 + total_sse += diff * diff; 1.2122 + } 1.2123 + 1.2124 + border_orig += orig_stride; 1.2125 + border_recon += recon_stride; 1.2126 + } 1.2127 + } 1.2128 + 1.2129 + orig += orig_stride * 16; 1.2130 + recon += recon_stride * 16; 1.2131 + } 1.2132 + 1.2133 + /* Handle odd-sized height */ 1.2134 + for (; row < rows; row++) { 1.2135 + for (col = 0; col < cols; col++) { 1.2136 + diff = orig[col] - recon[col]; 1.2137 + total_sse += diff * diff; 1.2138 + } 1.2139 + 1.2140 + orig += orig_stride; 1.2141 + recon += recon_stride; 1.2142 + } 1.2143 + 1.2144 + return total_sse; 1.2145 +} 1.2146 + 1.2147 + 1.2148 +static void generate_psnr_packet(VP9_COMP *cpi) { 1.2149 + YV12_BUFFER_CONFIG *orig = cpi->Source; 1.2150 + YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show; 1.2151 + struct vpx_codec_cx_pkt pkt; 1.2152 + uint64_t sse; 1.2153 + int i; 1.2154 + unsigned int width = orig->y_crop_width; 1.2155 + unsigned int height = orig->y_crop_height; 1.2156 + 1.2157 + pkt.kind = VPX_CODEC_PSNR_PKT; 1.2158 + sse = calc_plane_error(orig->y_buffer, orig->y_stride, 1.2159 + recon->y_buffer, recon->y_stride, 1.2160 + width, height); 1.2161 + pkt.data.psnr.sse[0] = sse; 1.2162 + pkt.data.psnr.sse[1] = sse; 1.2163 + pkt.data.psnr.samples[0] = width * height; 1.2164 + pkt.data.psnr.samples[1] = width * height; 1.2165 + 1.2166 + width = orig->uv_crop_width; 1.2167 + height = orig->uv_crop_height; 1.2168 + 1.2169 + sse = calc_plane_error(orig->u_buffer, orig->uv_stride, 1.2170 + recon->u_buffer, recon->uv_stride, 1.2171 + width, height); 1.2172 + pkt.data.psnr.sse[0] += sse; 1.2173 + pkt.data.psnr.sse[2] = sse; 1.2174 + pkt.data.psnr.samples[0] += width * height; 1.2175 + pkt.data.psnr.samples[2] = width * height; 1.2176 + 1.2177 + sse = calc_plane_error(orig->v_buffer, orig->uv_stride, 1.2178 + recon->v_buffer, recon->uv_stride, 1.2179 + width, height); 1.2180 + pkt.data.psnr.sse[0] += sse; 1.2181 + pkt.data.psnr.sse[3] = sse; 1.2182 + pkt.data.psnr.samples[0] += width * height; 1.2183 + pkt.data.psnr.samples[3] = width * height; 1.2184 + 1.2185 + for (i = 0; i < 4; i++) 1.2186 + pkt.data.psnr.psnr[i] = vp9_mse2psnr(pkt.data.psnr.samples[i], 255.0, 1.2187 + (double)pkt.data.psnr.sse[i]); 1.2188 + 1.2189 + vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt); 1.2190 +} 1.2191 + 1.2192 + 1.2193 +int vp9_use_as_reference(VP9_PTR ptr, int ref_frame_flags) { 1.2194 + VP9_COMP *cpi = (VP9_COMP *)(ptr); 1.2195 + 1.2196 + if (ref_frame_flags > 7) 1.2197 + return -1; 1.2198 + 1.2199 + cpi->ref_frame_flags = ref_frame_flags; 1.2200 + return 0; 1.2201 +} 1.2202 +int vp9_update_reference(VP9_PTR ptr, int ref_frame_flags) { 1.2203 + VP9_COMP *cpi = (VP9_COMP *)(ptr); 1.2204 + 1.2205 + if (ref_frame_flags > 7) 1.2206 + return -1; 1.2207 + 1.2208 + cpi->refresh_golden_frame = 0; 1.2209 + cpi->refresh_alt_ref_frame = 0; 1.2210 + cpi->refresh_last_frame = 0; 1.2211 + 1.2212 + if (ref_frame_flags & VP9_LAST_FLAG) 1.2213 + cpi->refresh_last_frame = 1; 1.2214 + 1.2215 + if (ref_frame_flags & VP9_GOLD_FLAG) 1.2216 + cpi->refresh_golden_frame = 1; 1.2217 + 1.2218 + if (ref_frame_flags & VP9_ALT_FLAG) 1.2219 + cpi->refresh_alt_ref_frame = 1; 1.2220 + 1.2221 + return 0; 1.2222 +} 1.2223 + 1.2224 +int vp9_copy_reference_enc(VP9_PTR ptr, VP9_REFFRAME ref_frame_flag, 1.2225 + YV12_BUFFER_CONFIG *sd) { 1.2226 + VP9_COMP *cpi = (VP9_COMP *)(ptr); 1.2227 + VP9_COMMON *cm = &cpi->common; 1.2228 + int ref_fb_idx; 1.2229 + 1.2230 + if (ref_frame_flag == VP9_LAST_FLAG) 1.2231 + ref_fb_idx = cm->ref_frame_map[cpi->lst_fb_idx]; 1.2232 + else if (ref_frame_flag == VP9_GOLD_FLAG) 1.2233 + ref_fb_idx = cm->ref_frame_map[cpi->gld_fb_idx]; 1.2234 + else if (ref_frame_flag == VP9_ALT_FLAG) 1.2235 + ref_fb_idx = cm->ref_frame_map[cpi->alt_fb_idx]; 1.2236 + else 1.2237 + return -1; 1.2238 + 1.2239 + vp8_yv12_copy_frame(&cm->yv12_fb[ref_fb_idx], sd); 1.2240 + 1.2241 + return 0; 1.2242 +} 1.2243 + 1.2244 +int vp9_get_reference_enc(VP9_PTR ptr, int index, YV12_BUFFER_CONFIG **fb) { 1.2245 + VP9_COMP *cpi = (VP9_COMP *)(ptr); 1.2246 + VP9_COMMON *cm = &cpi->common; 1.2247 + 1.2248 + if (index < 0 || index >= NUM_REF_FRAMES) 1.2249 + return -1; 1.2250 + 1.2251 + *fb = &cm->yv12_fb[cm->ref_frame_map[index]]; 1.2252 + return 0; 1.2253 +} 1.2254 + 1.2255 +int vp9_set_reference_enc(VP9_PTR ptr, VP9_REFFRAME ref_frame_flag, 1.2256 + YV12_BUFFER_CONFIG *sd) { 1.2257 + VP9_COMP *cpi = (VP9_COMP *)(ptr); 1.2258 + VP9_COMMON *cm = &cpi->common; 1.2259 + 1.2260 + int ref_fb_idx; 1.2261 + 1.2262 + if (ref_frame_flag == VP9_LAST_FLAG) 1.2263 + ref_fb_idx = cm->ref_frame_map[cpi->lst_fb_idx]; 1.2264 + else if (ref_frame_flag == VP9_GOLD_FLAG) 1.2265 + ref_fb_idx = cm->ref_frame_map[cpi->gld_fb_idx]; 1.2266 + else if (ref_frame_flag == VP9_ALT_FLAG) 1.2267 + ref_fb_idx = cm->ref_frame_map[cpi->alt_fb_idx]; 1.2268 + else 1.2269 + return -1; 1.2270 + 1.2271 + vp8_yv12_copy_frame(sd, &cm->yv12_fb[ref_fb_idx]); 1.2272 + 1.2273 + return 0; 1.2274 +} 1.2275 +int vp9_update_entropy(VP9_PTR comp, int update) { 1.2276 + ((VP9_COMP *)comp)->common.refresh_frame_context = update; 1.2277 + return 0; 1.2278 +} 1.2279 + 1.2280 + 1.2281 +#ifdef OUTPUT_YUV_SRC 1.2282 +void vp9_write_yuv_frame(YV12_BUFFER_CONFIG *s) { 1.2283 + uint8_t *src = s->y_buffer; 1.2284 + int h = s->y_height; 1.2285 + 1.2286 + do { 1.2287 + fwrite(src, s->y_width, 1, yuv_file); 1.2288 + src += s->y_stride; 1.2289 + } while (--h); 1.2290 + 1.2291 + src = s->u_buffer; 1.2292 + h = s->uv_height; 1.2293 + 1.2294 + do { 1.2295 + fwrite(src, s->uv_width, 1, yuv_file); 1.2296 + src += s->uv_stride; 1.2297 + } while (--h); 1.2298 + 1.2299 + src = s->v_buffer; 1.2300 + h = s->uv_height; 1.2301 + 1.2302 + do { 1.2303 + fwrite(src, s->uv_width, 1, yuv_file); 1.2304 + src += s->uv_stride; 1.2305 + } while (--h); 1.2306 +} 1.2307 +#endif 1.2308 + 1.2309 +#ifdef OUTPUT_YUV_REC 1.2310 +void vp9_write_yuv_rec_frame(VP9_COMMON *cm) { 1.2311 + YV12_BUFFER_CONFIG *s = cm->frame_to_show; 1.2312 + uint8_t *src = s->y_buffer; 1.2313 + int h = cm->height; 1.2314 + 1.2315 + do { 1.2316 + fwrite(src, s->y_width, 1, yuv_rec_file); 1.2317 + src += s->y_stride; 1.2318 + } while (--h); 1.2319 + 1.2320 + src = s->u_buffer; 1.2321 + h = s->uv_height; 1.2322 + 1.2323 + do { 1.2324 + fwrite(src, s->uv_width, 1, yuv_rec_file); 1.2325 + src += s->uv_stride; 1.2326 + } while (--h); 1.2327 + 1.2328 + src = s->v_buffer; 1.2329 + h = s->uv_height; 1.2330 + 1.2331 + do { 1.2332 + fwrite(src, s->uv_width, 1, yuv_rec_file); 1.2333 + src += s->uv_stride; 1.2334 + } while (--h); 1.2335 + 1.2336 +#if CONFIG_ALPHA 1.2337 + if (s->alpha_buffer) { 1.2338 + src = s->alpha_buffer; 1.2339 + h = s->alpha_height; 1.2340 + do { 1.2341 + fwrite(src, s->alpha_width, 1, yuv_rec_file); 1.2342 + src += s->alpha_stride; 1.2343 + } while (--h); 1.2344 + } 1.2345 +#endif 1.2346 + 1.2347 + fflush(yuv_rec_file); 1.2348 +} 1.2349 +#endif 1.2350 + 1.2351 +static void scale_and_extend_frame(YV12_BUFFER_CONFIG *src_fb, 1.2352 + YV12_BUFFER_CONFIG *dst_fb) { 1.2353 + const int in_w = src_fb->y_crop_width; 1.2354 + const int in_h = src_fb->y_crop_height; 1.2355 + const int out_w = dst_fb->y_crop_width; 1.2356 + const int out_h = dst_fb->y_crop_height; 1.2357 + int x, y, i; 1.2358 + 1.2359 + uint8_t *srcs[4] = {src_fb->y_buffer, src_fb->u_buffer, src_fb->v_buffer, 1.2360 + src_fb->alpha_buffer}; 1.2361 + int src_strides[4] = {src_fb->y_stride, src_fb->uv_stride, src_fb->uv_stride, 1.2362 + src_fb->alpha_stride}; 1.2363 + 1.2364 + uint8_t *dsts[4] = {dst_fb->y_buffer, dst_fb->u_buffer, dst_fb->v_buffer, 1.2365 + dst_fb->alpha_buffer}; 1.2366 + int dst_strides[4] = {dst_fb->y_stride, dst_fb->uv_stride, dst_fb->uv_stride, 1.2367 + dst_fb->alpha_stride}; 1.2368 + 1.2369 + for (y = 0; y < out_h; y += 16) { 1.2370 + for (x = 0; x < out_w; x += 16) { 1.2371 + for (i = 0; i < MAX_MB_PLANE; ++i) { 1.2372 + const int factor = i == 0 ? 1 : 2; 1.2373 + const int x_q4 = x * (16 / factor) * in_w / out_w; 1.2374 + const int y_q4 = y * (16 / factor) * in_h / out_h; 1.2375 + const int src_stride = src_strides[i]; 1.2376 + const int dst_stride = dst_strides[i]; 1.2377 + uint8_t *src = srcs[i] + y / factor * in_h / out_h * src_stride + 1.2378 + x / factor * in_w / out_w; 1.2379 + uint8_t *dst = dsts[i] + y / factor * dst_stride + x / factor; 1.2380 + 1.2381 + vp9_convolve8(src, src_stride, dst, dst_stride, 1.2382 + vp9_sub_pel_filters_8[x_q4 & 0xf], 16 * in_w / out_w, 1.2383 + vp9_sub_pel_filters_8[y_q4 & 0xf], 16 * in_h / out_h, 1.2384 + 16 / factor, 16 / factor); 1.2385 + } 1.2386 + } 1.2387 + } 1.2388 + 1.2389 + vp8_yv12_extend_frame_borders(dst_fb); 1.2390 +} 1.2391 + 1.2392 + 1.2393 +static void update_alt_ref_frame_stats(VP9_COMP *cpi) { 1.2394 + // this frame refreshes means next frames don't unless specified by user 1.2395 + cpi->frames_since_golden = 0; 1.2396 + 1.2397 +#if CONFIG_MULTIPLE_ARF 1.2398 + if (!cpi->multi_arf_enabled) 1.2399 +#endif 1.2400 + // Clear the alternate reference update pending flag. 1.2401 + cpi->source_alt_ref_pending = 0; 1.2402 + 1.2403 + // Set the alternate reference frame active flag 1.2404 + cpi->source_alt_ref_active = 1; 1.2405 +} 1.2406 +static void update_golden_frame_stats(VP9_COMP *cpi) { 1.2407 + // Update the Golden frame usage counts. 1.2408 + if (cpi->refresh_golden_frame) { 1.2409 + // this frame refreshes means next frames don't unless specified by user 1.2410 + cpi->refresh_golden_frame = 0; 1.2411 + cpi->frames_since_golden = 0; 1.2412 + 1.2413 + // ******** Fixed Q test code only ************ 1.2414 + // If we are going to use the ALT reference for the next group of frames 1.2415 + // set a flag to say so. 1.2416 + if (cpi->oxcf.fixed_q >= 0 && 1.2417 + cpi->oxcf.play_alternate && !cpi->refresh_alt_ref_frame) { 1.2418 + cpi->source_alt_ref_pending = 1; 1.2419 + cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; 1.2420 + 1.2421 + // TODO(ivan): For SVC encoder, GF automatic update is disabled by using 1.2422 + // a large GF_interval. 1.2423 + if (cpi->use_svc) { 1.2424 + cpi->frames_till_gf_update_due = INT_MAX; 1.2425 + } 1.2426 + } 1.2427 + 1.2428 + if (!cpi->source_alt_ref_pending) 1.2429 + cpi->source_alt_ref_active = 0; 1.2430 + 1.2431 + // Decrement count down till next gf 1.2432 + if (cpi->frames_till_gf_update_due > 0) 1.2433 + cpi->frames_till_gf_update_due--; 1.2434 + 1.2435 + } else if (!cpi->refresh_alt_ref_frame) { 1.2436 + // Decrement count down till next gf 1.2437 + if (cpi->frames_till_gf_update_due > 0) 1.2438 + cpi->frames_till_gf_update_due--; 1.2439 + 1.2440 + if (cpi->frames_till_alt_ref_frame) 1.2441 + cpi->frames_till_alt_ref_frame--; 1.2442 + 1.2443 + cpi->frames_since_golden++; 1.2444 + } 1.2445 +} 1.2446 + 1.2447 +static int find_fp_qindex() { 1.2448 + int i; 1.2449 + 1.2450 + for (i = 0; i < QINDEX_RANGE; i++) { 1.2451 + if (vp9_convert_qindex_to_q(i) >= 30.0) { 1.2452 + break; 1.2453 + } 1.2454 + } 1.2455 + 1.2456 + if (i == QINDEX_RANGE) 1.2457 + i--; 1.2458 + 1.2459 + return i; 1.2460 +} 1.2461 + 1.2462 +static void Pass1Encode(VP9_COMP *cpi, unsigned long *size, unsigned char *dest, 1.2463 + unsigned int *frame_flags) { 1.2464 + (void) size; 1.2465 + (void) dest; 1.2466 + (void) frame_flags; 1.2467 + 1.2468 + vp9_set_quantizer(cpi, find_fp_qindex()); 1.2469 + vp9_first_pass(cpi); 1.2470 +} 1.2471 + 1.2472 +#define WRITE_RECON_BUFFER 0 1.2473 +#if WRITE_RECON_BUFFER 1.2474 +void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame) { 1.2475 + FILE *yframe; 1.2476 + int i; 1.2477 + char filename[255]; 1.2478 + 1.2479 + snprintf(filename, sizeof(filename), "cx\\y%04d.raw", this_frame); 1.2480 + yframe = fopen(filename, "wb"); 1.2481 + 1.2482 + for (i = 0; i < frame->y_height; i++) 1.2483 + fwrite(frame->y_buffer + i * frame->y_stride, 1.2484 + frame->y_width, 1, yframe); 1.2485 + 1.2486 + fclose(yframe); 1.2487 + snprintf(filename, sizeof(filename), "cx\\u%04d.raw", this_frame); 1.2488 + yframe = fopen(filename, "wb"); 1.2489 + 1.2490 + for (i = 0; i < frame->uv_height; i++) 1.2491 + fwrite(frame->u_buffer + i * frame->uv_stride, 1.2492 + frame->uv_width, 1, yframe); 1.2493 + 1.2494 + fclose(yframe); 1.2495 + snprintf(filename, sizeof(filename), "cx\\v%04d.raw", this_frame); 1.2496 + yframe = fopen(filename, "wb"); 1.2497 + 1.2498 + for (i = 0; i < frame->uv_height; i++) 1.2499 + fwrite(frame->v_buffer + i * frame->uv_stride, 1.2500 + frame->uv_width, 1, yframe); 1.2501 + 1.2502 + fclose(yframe); 1.2503 +} 1.2504 +#endif 1.2505 + 1.2506 +static double compute_edge_pixel_proportion(YV12_BUFFER_CONFIG *frame) { 1.2507 +#define EDGE_THRESH 128 1.2508 + int i, j; 1.2509 + int num_edge_pels = 0; 1.2510 + int num_pels = (frame->y_height - 2) * (frame->y_width - 2); 1.2511 + uint8_t *prev = frame->y_buffer + 1; 1.2512 + uint8_t *curr = frame->y_buffer + 1 + frame->y_stride; 1.2513 + uint8_t *next = frame->y_buffer + 1 + 2 * frame->y_stride; 1.2514 + for (i = 1; i < frame->y_height - 1; i++) { 1.2515 + for (j = 1; j < frame->y_width - 1; j++) { 1.2516 + /* Sobel hor and ver gradients */ 1.2517 + int v = 2 * (curr[1] - curr[-1]) + (prev[1] - prev[-1]) + 1.2518 + (next[1] - next[-1]); 1.2519 + int h = 2 * (prev[0] - next[0]) + (prev[1] - next[1]) + 1.2520 + (prev[-1] - next[-1]); 1.2521 + h = (h < 0 ? -h : h); 1.2522 + v = (v < 0 ? -v : v); 1.2523 + if (h > EDGE_THRESH || v > EDGE_THRESH) 1.2524 + num_edge_pels++; 1.2525 + curr++; 1.2526 + prev++; 1.2527 + next++; 1.2528 + } 1.2529 + curr += frame->y_stride - frame->y_width + 2; 1.2530 + prev += frame->y_stride - frame->y_width + 2; 1.2531 + next += frame->y_stride - frame->y_width + 2; 1.2532 + } 1.2533 + return (double)num_edge_pels / num_pels; 1.2534 +} 1.2535 + 1.2536 +// Function to test for conditions that indicate we should loop 1.2537 +// back and recode a frame. 1.2538 +static int recode_loop_test(VP9_COMP *cpi, 1.2539 + int high_limit, int low_limit, 1.2540 + int q, int maxq, int minq) { 1.2541 + int force_recode = 0; 1.2542 + VP9_COMMON *cm = &cpi->common; 1.2543 + 1.2544 + // Is frame recode allowed at all 1.2545 + // Yes if either recode mode 1 is selected or mode two is selected 1.2546 + // and the frame is a key frame. golden frame or alt_ref_frame 1.2547 + if ((cpi->sf.recode_loop == 1) || 1.2548 + ((cpi->sf.recode_loop == 2) && 1.2549 + ((cm->frame_type == KEY_FRAME) || 1.2550 + cpi->refresh_golden_frame || 1.2551 + cpi->refresh_alt_ref_frame))) { 1.2552 + // General over and under shoot tests 1.2553 + if (((cpi->projected_frame_size > high_limit) && (q < maxq)) || 1.2554 + ((cpi->projected_frame_size < low_limit) && (q > minq))) { 1.2555 + force_recode = 1; 1.2556 + } else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) { 1.2557 + // Deal with frame undershoot and whether or not we are 1.2558 + // below the automatically set cq level. 1.2559 + if (q > cpi->cq_target_quality && 1.2560 + cpi->projected_frame_size < ((cpi->this_frame_target * 7) >> 3)) { 1.2561 + force_recode = 1; 1.2562 + } else if (q > cpi->oxcf.cq_level && 1.2563 + cpi->projected_frame_size < cpi->min_frame_bandwidth && 1.2564 + cpi->active_best_quality > cpi->oxcf.cq_level) { 1.2565 + // Severe undershoot and between auto and user cq level 1.2566 + force_recode = 1; 1.2567 + cpi->active_best_quality = cpi->oxcf.cq_level; 1.2568 + } 1.2569 + } 1.2570 + } 1.2571 + 1.2572 + return force_recode; 1.2573 +} 1.2574 + 1.2575 +static void update_reference_frames(VP9_COMP * const cpi) { 1.2576 + VP9_COMMON * const cm = &cpi->common; 1.2577 + 1.2578 + // At this point the new frame has been encoded. 1.2579 + // If any buffer copy / swapping is signaled it should be done here. 1.2580 + if (cm->frame_type == KEY_FRAME) { 1.2581 + ref_cnt_fb(cm->fb_idx_ref_cnt, 1.2582 + &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx); 1.2583 + ref_cnt_fb(cm->fb_idx_ref_cnt, 1.2584 + &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx); 1.2585 + } 1.2586 +#if CONFIG_MULTIPLE_ARF 1.2587 + else if (!cpi->multi_arf_enabled && cpi->refresh_golden_frame && 1.2588 + !cpi->refresh_alt_ref_frame) { 1.2589 +#else 1.2590 + else if (cpi->refresh_golden_frame && !cpi->refresh_alt_ref_frame && 1.2591 + !cpi->use_svc) { 1.2592 +#endif 1.2593 + /* Preserve the previously existing golden frame and update the frame in 1.2594 + * the alt ref slot instead. This is highly specific to the current use of 1.2595 + * alt-ref as a forward reference, and this needs to be generalized as 1.2596 + * other uses are implemented (like RTC/temporal scaling) 1.2597 + * 1.2598 + * The update to the buffer in the alt ref slot was signaled in 1.2599 + * vp9_pack_bitstream(), now swap the buffer pointers so that it's treated 1.2600 + * as the golden frame next time. 1.2601 + */ 1.2602 + int tmp; 1.2603 + 1.2604 + ref_cnt_fb(cm->fb_idx_ref_cnt, 1.2605 + &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx); 1.2606 + 1.2607 + tmp = cpi->alt_fb_idx; 1.2608 + cpi->alt_fb_idx = cpi->gld_fb_idx; 1.2609 + cpi->gld_fb_idx = tmp; 1.2610 + } else { /* For non key/golden frames */ 1.2611 + if (cpi->refresh_alt_ref_frame) { 1.2612 + int arf_idx = cpi->alt_fb_idx; 1.2613 +#if CONFIG_MULTIPLE_ARF 1.2614 + if (cpi->multi_arf_enabled) { 1.2615 + arf_idx = cpi->arf_buffer_idx[cpi->sequence_number + 1]; 1.2616 + } 1.2617 +#endif 1.2618 + ref_cnt_fb(cm->fb_idx_ref_cnt, 1.2619 + &cm->ref_frame_map[arf_idx], cm->new_fb_idx); 1.2620 + } 1.2621 + 1.2622 + if (cpi->refresh_golden_frame) { 1.2623 + ref_cnt_fb(cm->fb_idx_ref_cnt, 1.2624 + &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx); 1.2625 + } 1.2626 + } 1.2627 + 1.2628 + if (cpi->refresh_last_frame) { 1.2629 + ref_cnt_fb(cm->fb_idx_ref_cnt, 1.2630 + &cm->ref_frame_map[cpi->lst_fb_idx], cm->new_fb_idx); 1.2631 + } 1.2632 +} 1.2633 + 1.2634 +static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) { 1.2635 + MACROBLOCKD *xd = &cpi->mb.e_mbd; 1.2636 + struct loopfilter *lf = &cm->lf; 1.2637 + if (xd->lossless) { 1.2638 + lf->filter_level = 0; 1.2639 + } else { 1.2640 + struct vpx_usec_timer timer; 1.2641 + 1.2642 + vp9_clear_system_state(); 1.2643 + 1.2644 + vpx_usec_timer_start(&timer); 1.2645 + 1.2646 + vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.use_fast_lpf_pick); 1.2647 + 1.2648 + vpx_usec_timer_mark(&timer); 1.2649 + cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer); 1.2650 + } 1.2651 + 1.2652 + if (lf->filter_level > 0) { 1.2653 + vp9_set_alt_lf_level(cpi, lf->filter_level); 1.2654 + vp9_loop_filter_frame(cm, xd, lf->filter_level, 0, 0); 1.2655 + } 1.2656 + 1.2657 + vp9_extend_frame_inner_borders(cm->frame_to_show, 1.2658 + cm->subsampling_x, cm->subsampling_y); 1.2659 +} 1.2660 + 1.2661 +static void scale_references(VP9_COMP *cpi) { 1.2662 + VP9_COMMON *cm = &cpi->common; 1.2663 + int i; 1.2664 + int refs[ALLOWED_REFS_PER_FRAME] = {cpi->lst_fb_idx, cpi->gld_fb_idx, 1.2665 + cpi->alt_fb_idx}; 1.2666 + 1.2667 + for (i = 0; i < 3; i++) { 1.2668 + YV12_BUFFER_CONFIG *ref = &cm->yv12_fb[cm->ref_frame_map[refs[i]]]; 1.2669 + 1.2670 + if (ref->y_crop_width != cm->width || 1.2671 + ref->y_crop_height != cm->height) { 1.2672 + int new_fb = get_free_fb(cm); 1.2673 + 1.2674 + vp9_realloc_frame_buffer(&cm->yv12_fb[new_fb], 1.2675 + cm->width, cm->height, 1.2676 + cm->subsampling_x, cm->subsampling_y, 1.2677 + VP9BORDERINPIXELS); 1.2678 + scale_and_extend_frame(ref, &cm->yv12_fb[new_fb]); 1.2679 + cpi->scaled_ref_idx[i] = new_fb; 1.2680 + } else { 1.2681 + cpi->scaled_ref_idx[i] = cm->ref_frame_map[refs[i]]; 1.2682 + cm->fb_idx_ref_cnt[cm->ref_frame_map[refs[i]]]++; 1.2683 + } 1.2684 + } 1.2685 +} 1.2686 + 1.2687 +static void release_scaled_references(VP9_COMP *cpi) { 1.2688 + VP9_COMMON *cm = &cpi->common; 1.2689 + int i; 1.2690 + 1.2691 + for (i = 0; i < 3; i++) 1.2692 + cm->fb_idx_ref_cnt[cpi->scaled_ref_idx[i]]--; 1.2693 +} 1.2694 + 1.2695 +static void full_to_model_count(unsigned int *model_count, 1.2696 + unsigned int *full_count) { 1.2697 + int n; 1.2698 + model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN]; 1.2699 + model_count[ONE_TOKEN] = full_count[ONE_TOKEN]; 1.2700 + model_count[TWO_TOKEN] = full_count[TWO_TOKEN]; 1.2701 + for (n = THREE_TOKEN; n < DCT_EOB_TOKEN; ++n) 1.2702 + model_count[TWO_TOKEN] += full_count[n]; 1.2703 + model_count[DCT_EOB_MODEL_TOKEN] = full_count[DCT_EOB_TOKEN]; 1.2704 +} 1.2705 + 1.2706 +static void full_to_model_counts( 1.2707 + vp9_coeff_count_model *model_count, vp9_coeff_count *full_count) { 1.2708 + int i, j, k, l; 1.2709 + for (i = 0; i < BLOCK_TYPES; ++i) 1.2710 + for (j = 0; j < REF_TYPES; ++j) 1.2711 + for (k = 0; k < COEF_BANDS; ++k) 1.2712 + for (l = 0; l < PREV_COEF_CONTEXTS; ++l) { 1.2713 + if (l >= 3 && k == 0) 1.2714 + continue; 1.2715 + full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]); 1.2716 + } 1.2717 +} 1.2718 + 1.2719 +#if 0 && CONFIG_INTERNAL_STATS 1.2720 +static void output_frame_level_debug_stats(VP9_COMP *cpi) { 1.2721 + VP9_COMMON *const cm = &cpi->common; 1.2722 + FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w"); 1.2723 + int recon_err; 1.2724 + 1.2725 + vp9_clear_system_state(); // __asm emms; 1.2726 + 1.2727 + recon_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm)); 1.2728 + 1.2729 + if (cpi->twopass.total_left_stats.coded_error != 0.0) 1.2730 + fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %10d" 1.2731 + "%7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f" 1.2732 + "%6d %6d %5d %5d %5d %8.2f %10d %10.3f" 1.2733 + "%10.3f %8d %10d %10d %10d\n", 1.2734 + cpi->common.current_video_frame, cpi->this_frame_target, 1.2735 + cpi->projected_frame_size, 0, 1.2736 + (cpi->projected_frame_size - cpi->this_frame_target), 1.2737 + (int)cpi->total_target_vs_actual, 1.2738 + (int)(cpi->oxcf.starting_buffer_level - cpi->bits_off_target), 1.2739 + (int)cpi->total_actual_bits, cm->base_qindex, 1.2740 + vp9_convert_qindex_to_q(cm->base_qindex), 1.2741 + (double)vp9_dc_quant(cm->base_qindex, 0) / 4.0, 1.2742 + vp9_convert_qindex_to_q(cpi->active_best_quality), 1.2743 + vp9_convert_qindex_to_q(cpi->active_worst_quality), cpi->avg_q, 1.2744 + vp9_convert_qindex_to_q(cpi->ni_av_qi), 1.2745 + vp9_convert_qindex_to_q(cpi->cq_target_quality), 1.2746 + cpi->refresh_last_frame, cpi->refresh_golden_frame, 1.2747 + cpi->refresh_alt_ref_frame, cm->frame_type, cpi->gfu_boost, 1.2748 + cpi->twopass.est_max_qcorrection_factor, (int)cpi->twopass.bits_left, 1.2749 + cpi->twopass.total_left_stats.coded_error, 1.2750 + (double)cpi->twopass.bits_left / 1.2751 + (1 + cpi->twopass.total_left_stats.coded_error), 1.2752 + cpi->tot_recode_hits, recon_err, cpi->kf_boost, cpi->kf_zeromotion_pct); 1.2753 + 1.2754 + fclose(f); 1.2755 + 1.2756 + if (0) { 1.2757 + FILE *const fmodes = fopen("Modes.stt", "a"); 1.2758 + int i; 1.2759 + 1.2760 + fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame, 1.2761 + cm->frame_type, cpi->refresh_golden_frame, 1.2762 + cpi->refresh_alt_ref_frame); 1.2763 + 1.2764 + for (i = 0; i < MAX_MODES; ++i) 1.2765 + fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]); 1.2766 + for (i = 0; i < MAX_REFS; ++i) 1.2767 + fprintf(fmodes, "%5d ", cpi->sub8x8_mode_chosen_counts[i]); 1.2768 + 1.2769 + fprintf(fmodes, "\n"); 1.2770 + 1.2771 + fclose(fmodes); 1.2772 + } 1.2773 +} 1.2774 +#endif 1.2775 + 1.2776 +static int pick_q_and_adjust_q_bounds(VP9_COMP *cpi, 1.2777 + int * bottom_index, int * top_index) { 1.2778 + // Set an active best quality and if necessary active worst quality 1.2779 + int q = cpi->active_worst_quality; 1.2780 + VP9_COMMON *const cm = &cpi->common; 1.2781 + 1.2782 + if (frame_is_intra_only(cm)) { 1.2783 +#if !CONFIG_MULTIPLE_ARF 1.2784 + // Handle the special case for key frames forced when we have75 reached 1.2785 + // the maximum key frame interval. Here force the Q to a range 1.2786 + // based on the ambient Q to reduce the risk of popping. 1.2787 + if (cpi->this_key_frame_forced) { 1.2788 + int delta_qindex; 1.2789 + int qindex = cpi->last_boosted_qindex; 1.2790 + double last_boosted_q = vp9_convert_qindex_to_q(qindex); 1.2791 + 1.2792 + delta_qindex = vp9_compute_qdelta(cpi, last_boosted_q, 1.2793 + (last_boosted_q * 0.75)); 1.2794 + 1.2795 + cpi->active_best_quality = MAX(qindex + delta_qindex, 1.2796 + cpi->best_quality); 1.2797 + } else { 1.2798 + int high = 5000; 1.2799 + int low = 400; 1.2800 + double q_adj_factor = 1.0; 1.2801 + double q_val; 1.2802 + 1.2803 + // Baseline value derived from cpi->active_worst_quality and kf boost 1.2804 + cpi->active_best_quality = get_active_quality(q, cpi->kf_boost, 1.2805 + low, high, 1.2806 + kf_low_motion_minq, 1.2807 + kf_high_motion_minq); 1.2808 + 1.2809 + // Allow somewhat lower kf minq with small image formats. 1.2810 + if ((cm->width * cm->height) <= (352 * 288)) { 1.2811 + q_adj_factor -= 0.25; 1.2812 + } 1.2813 + 1.2814 + // Make a further adjustment based on the kf zero motion measure. 1.2815 + q_adj_factor += 0.05 - (0.001 * (double)cpi->kf_zeromotion_pct); 1.2816 + 1.2817 + // Convert the adjustment factor to a qindex delta 1.2818 + // on active_best_quality. 1.2819 + q_val = vp9_convert_qindex_to_q(cpi->active_best_quality); 1.2820 + cpi->active_best_quality += 1.2821 + vp9_compute_qdelta(cpi, q_val, (q_val * q_adj_factor)); 1.2822 + } 1.2823 +#else 1.2824 + double current_q; 1.2825 + // Force the KF quantizer to be 30% of the active_worst_quality. 1.2826 + current_q = vp9_convert_qindex_to_q(cpi->active_worst_quality); 1.2827 + cpi->active_best_quality = cpi->active_worst_quality 1.2828 + + vp9_compute_qdelta(cpi, current_q, current_q * 0.3); 1.2829 +#endif 1.2830 + } else if (!cpi->is_src_frame_alt_ref && 1.2831 + (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { 1.2832 + int high = 2000; 1.2833 + int low = 400; 1.2834 + 1.2835 + // Use the lower of cpi->active_worst_quality and recent 1.2836 + // average Q as basis for GF/ARF best Q limit unless last frame was 1.2837 + // a key frame. 1.2838 + if (cpi->frames_since_key > 1 && 1.2839 + cpi->avg_frame_qindex < cpi->active_worst_quality) { 1.2840 + q = cpi->avg_frame_qindex; 1.2841 + } 1.2842 + // For constrained quality dont allow Q less than the cq level 1.2843 + if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) { 1.2844 + if (q < cpi->cq_target_quality) 1.2845 + q = cpi->cq_target_quality; 1.2846 + if (cpi->frames_since_key > 1) { 1.2847 + cpi->active_best_quality = get_active_quality(q, cpi->gfu_boost, 1.2848 + low, high, 1.2849 + afq_low_motion_minq, 1.2850 + afq_high_motion_minq); 1.2851 + } else { 1.2852 + cpi->active_best_quality = get_active_quality(q, cpi->gfu_boost, 1.2853 + low, high, 1.2854 + gf_low_motion_minq, 1.2855 + gf_high_motion_minq); 1.2856 + } 1.2857 + // Constrained quality use slightly lower active best. 1.2858 + cpi->active_best_quality = cpi->active_best_quality * 15 / 16; 1.2859 + 1.2860 + } else if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { 1.2861 + if (!cpi->refresh_alt_ref_frame) { 1.2862 + cpi->active_best_quality = cpi->cq_target_quality; 1.2863 + } else { 1.2864 + if (cpi->frames_since_key > 1) { 1.2865 + cpi->active_best_quality = get_active_quality(q, cpi->gfu_boost, 1.2866 + low, high, 1.2867 + afq_low_motion_minq, 1.2868 + afq_high_motion_minq); 1.2869 + } else { 1.2870 + cpi->active_best_quality = get_active_quality(q, cpi->gfu_boost, 1.2871 + low, high, 1.2872 + gf_low_motion_minq, 1.2873 + gf_high_motion_minq); 1.2874 + } 1.2875 + } 1.2876 + } else { 1.2877 + cpi->active_best_quality = get_active_quality(q, cpi->gfu_boost, 1.2878 + low, high, 1.2879 + gf_low_motion_minq, 1.2880 + gf_high_motion_minq); 1.2881 + } 1.2882 + } else { 1.2883 + if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { 1.2884 + cpi->active_best_quality = cpi->cq_target_quality; 1.2885 + } else { 1.2886 + cpi->active_best_quality = inter_minq[q]; 1.2887 + // 1-pass: for now, use the average Q for the active_best, if its lower 1.2888 + // than active_worst. 1.2889 + if (cpi->pass == 0 && (cpi->avg_frame_qindex < q)) 1.2890 + cpi->active_best_quality = inter_minq[cpi->avg_frame_qindex]; 1.2891 + 1.2892 + // For the constrained quality mode we don't want 1.2893 + // q to fall below the cq level. 1.2894 + if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) && 1.2895 + (cpi->active_best_quality < cpi->cq_target_quality)) { 1.2896 + // If we are strongly undershooting the target rate in the last 1.2897 + // frames then use the user passed in cq value not the auto 1.2898 + // cq value. 1.2899 + if (cpi->rolling_actual_bits < cpi->min_frame_bandwidth) 1.2900 + cpi->active_best_quality = cpi->oxcf.cq_level; 1.2901 + else 1.2902 + cpi->active_best_quality = cpi->cq_target_quality; 1.2903 + } 1.2904 + } 1.2905 + } 1.2906 + 1.2907 + // Clip the active best and worst quality values to limits 1.2908 + if (cpi->active_worst_quality > cpi->worst_quality) 1.2909 + cpi->active_worst_quality = cpi->worst_quality; 1.2910 + 1.2911 + if (cpi->active_best_quality < cpi->best_quality) 1.2912 + cpi->active_best_quality = cpi->best_quality; 1.2913 + 1.2914 + if (cpi->active_best_quality > cpi->worst_quality) 1.2915 + cpi->active_best_quality = cpi->worst_quality; 1.2916 + 1.2917 + if (cpi->active_worst_quality < cpi->active_best_quality) 1.2918 + cpi->active_worst_quality = cpi->active_best_quality; 1.2919 + 1.2920 + // Limit Q range for the adaptive loop. 1.2921 + if (cm->frame_type == KEY_FRAME && !cpi->this_key_frame_forced) { 1.2922 + *top_index = 1.2923 + (cpi->active_worst_quality + cpi->active_best_quality * 3) / 4; 1.2924 + // If this is the first (key) frame in 1-pass, active best is the user 1.2925 + // best-allowed, and leave the top_index to active_worst. 1.2926 + if (cpi->pass == 0 && cpi->common.current_video_frame == 0) { 1.2927 + cpi->active_best_quality = cpi->oxcf.best_allowed_q; 1.2928 + *top_index = cpi->oxcf.worst_allowed_q; 1.2929 + } 1.2930 + } else if (!cpi->is_src_frame_alt_ref && 1.2931 + (cpi->oxcf.end_usage != USAGE_STREAM_FROM_SERVER) && 1.2932 + (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { 1.2933 + *top_index = 1.2934 + (cpi->active_worst_quality + cpi->active_best_quality) / 2; 1.2935 + } else { 1.2936 + *top_index = cpi->active_worst_quality; 1.2937 + } 1.2938 + *bottom_index = cpi->active_best_quality; 1.2939 + 1.2940 + if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { 1.2941 + q = cpi->active_best_quality; 1.2942 + // Special case code to try and match quality with forced key frames 1.2943 + } else if ((cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced) { 1.2944 + q = cpi->last_boosted_qindex; 1.2945 + } else { 1.2946 + // Determine initial Q to try. 1.2947 + if (cpi->pass == 0) { 1.2948 + // 1-pass: for now, use per-frame-bw for target size of frame, scaled 1.2949 + // by |x| for key frame. 1.2950 + int scale = (cm->frame_type == KEY_FRAME) ? 5 : 1; 1.2951 + q = vp9_regulate_q(cpi, scale * cpi->av_per_frame_bandwidth); 1.2952 + } else { 1.2953 + q = vp9_regulate_q(cpi, cpi->this_frame_target); 1.2954 + } 1.2955 + if (q > *top_index) 1.2956 + q = *top_index; 1.2957 + } 1.2958 + 1.2959 + return q; 1.2960 +} 1.2961 +static void encode_frame_to_data_rate(VP9_COMP *cpi, 1.2962 + unsigned long *size, 1.2963 + unsigned char *dest, 1.2964 + unsigned int *frame_flags) { 1.2965 + VP9_COMMON *const cm = &cpi->common; 1.2966 + TX_SIZE t; 1.2967 + int q; 1.2968 + int frame_over_shoot_limit; 1.2969 + int frame_under_shoot_limit; 1.2970 + 1.2971 + int loop = 0; 1.2972 + int loop_count; 1.2973 + 1.2974 + int q_low; 1.2975 + int q_high; 1.2976 + 1.2977 + int top_index; 1.2978 + int bottom_index; 1.2979 + int active_worst_qchanged = 0; 1.2980 + 1.2981 + int overshoot_seen = 0; 1.2982 + int undershoot_seen = 0; 1.2983 + 1.2984 + SPEED_FEATURES *const sf = &cpi->sf; 1.2985 + unsigned int max_mv_def = MIN(cpi->common.width, cpi->common.height); 1.2986 + struct segmentation *const seg = &cm->seg; 1.2987 + 1.2988 + /* Scale the source buffer, if required. */ 1.2989 + if (cm->mi_cols * 8 != cpi->un_scaled_source->y_width || 1.2990 + cm->mi_rows * 8 != cpi->un_scaled_source->y_height) { 1.2991 + scale_and_extend_frame(cpi->un_scaled_source, &cpi->scaled_source); 1.2992 + cpi->Source = &cpi->scaled_source; 1.2993 + } else { 1.2994 + cpi->Source = cpi->un_scaled_source; 1.2995 + } 1.2996 + scale_references(cpi); 1.2997 + 1.2998 + // Clear down mmx registers to allow floating point in what follows. 1.2999 + vp9_clear_system_state(); 1.3000 + 1.3001 + // For an alt ref frame in 2 pass we skip the call to the second 1.3002 + // pass function that sets the target bandwidth so we must set it here. 1.3003 + if (cpi->refresh_alt_ref_frame) { 1.3004 + // Set a per frame bit target for the alt ref frame. 1.3005 + cpi->per_frame_bandwidth = cpi->twopass.gf_bits; 1.3006 + // Set a per second target bitrate. 1.3007 + cpi->target_bandwidth = (int)(cpi->twopass.gf_bits * cpi->output_framerate); 1.3008 + } 1.3009 + 1.3010 + // Clear zbin over-quant value and mode boost values. 1.3011 + cpi->zbin_mode_boost = 0; 1.3012 + 1.3013 + // Enable or disable mode based tweaking of the zbin. 1.3014 + // For 2 pass only used where GF/ARF prediction quality 1.3015 + // is above a threshold. 1.3016 + cpi->zbin_mode_boost = 0; 1.3017 + cpi->zbin_mode_boost_enabled = 0; 1.3018 + 1.3019 + // Current default encoder behavior for the altref sign bias. 1.3020 + cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = cpi->source_alt_ref_active; 1.3021 + 1.3022 + // Check to see if a key frame is signaled. 1.3023 + // For two pass with auto key frame enabled cm->frame_type may already be 1.3024 + // set, but not for one pass. 1.3025 + if ((cm->current_video_frame == 0) || 1.3026 + (cm->frame_flags & FRAMEFLAGS_KEY) || 1.3027 + (cpi->oxcf.auto_key && (cpi->frames_since_key % 1.3028 + cpi->key_frame_frequency == 0))) { 1.3029 + // Set frame type to key frame for the force key frame, if we exceed the 1.3030 + // maximum distance in an automatic keyframe selection or for the first 1.3031 + // frame. 1.3032 + cm->frame_type = KEY_FRAME; 1.3033 + } 1.3034 + 1.3035 + // Set default state for segment based loop filter update flags. 1.3036 + cm->lf.mode_ref_delta_update = 0; 1.3037 + 1.3038 + // Initialize cpi->mv_step_param to default based on max resolution. 1.3039 + cpi->mv_step_param = vp9_init_search_range(cpi, max_mv_def); 1.3040 + // Initialize cpi->max_mv_magnitude and cpi->mv_step_param if appropriate. 1.3041 + if (sf->auto_mv_step_size) { 1.3042 + if (frame_is_intra_only(&cpi->common)) { 1.3043 + // Initialize max_mv_magnitude for use in the first INTER frame 1.3044 + // after a key/intra-only frame. 1.3045 + cpi->max_mv_magnitude = max_mv_def; 1.3046 + } else { 1.3047 + if (cm->show_frame) 1.3048 + // Allow mv_steps to correspond to twice the max mv magnitude found 1.3049 + // in the previous frame, capped by the default max_mv_magnitude based 1.3050 + // on resolution. 1.3051 + cpi->mv_step_param = vp9_init_search_range( 1.3052 + cpi, MIN(max_mv_def, 2 * cpi->max_mv_magnitude)); 1.3053 + cpi->max_mv_magnitude = 0; 1.3054 + } 1.3055 + } 1.3056 + 1.3057 + // Set various flags etc to special state if it is a key frame. 1.3058 + if (frame_is_intra_only(cm)) { 1.3059 + vp9_setup_key_frame(cpi); 1.3060 + // Reset the loop filter deltas and segmentation map. 1.3061 + setup_features(cm); 1.3062 + 1.3063 + // If segmentation is enabled force a map update for key frames. 1.3064 + if (seg->enabled) { 1.3065 + seg->update_map = 1; 1.3066 + seg->update_data = 1; 1.3067 + } 1.3068 + 1.3069 + // The alternate reference frame cannot be active for a key frame. 1.3070 + cpi->source_alt_ref_active = 0; 1.3071 + 1.3072 + cm->error_resilient_mode = (cpi->oxcf.error_resilient_mode != 0); 1.3073 + cm->frame_parallel_decoding_mode = 1.3074 + (cpi->oxcf.frame_parallel_decoding_mode != 0); 1.3075 + if (cm->error_resilient_mode) { 1.3076 + cm->frame_parallel_decoding_mode = 1; 1.3077 + cm->reset_frame_context = 0; 1.3078 + cm->refresh_frame_context = 0; 1.3079 + } else if (cm->intra_only) { 1.3080 + // Only reset the current context. 1.3081 + cm->reset_frame_context = 2; 1.3082 + } 1.3083 + } 1.3084 + 1.3085 + // Configure experimental use of segmentation for enhanced coding of 1.3086 + // static regions if indicated. 1.3087 + // Only allowed in second pass of two pass (as requires lagged coding) 1.3088 + // and if the relevant speed feature flag is set. 1.3089 + if ((cpi->pass == 2) && (cpi->sf.static_segmentation)) { 1.3090 + configure_static_seg_features(cpi); 1.3091 + } 1.3092 + 1.3093 + // Decide how big to make the frame. 1.3094 + vp9_pick_frame_size(cpi); 1.3095 + 1.3096 + vp9_clear_system_state(); 1.3097 + 1.3098 + q = pick_q_and_adjust_q_bounds(cpi, &bottom_index, &top_index); 1.3099 + 1.3100 + q_high = top_index; 1.3101 + q_low = bottom_index; 1.3102 + 1.3103 + vp9_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, 1.3104 + &frame_over_shoot_limit); 1.3105 + 1.3106 +#if CONFIG_MULTIPLE_ARF 1.3107 + // Force the quantizer determined by the coding order pattern. 1.3108 + if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) && 1.3109 + cpi->oxcf.end_usage != USAGE_CONSTANT_QUALITY) { 1.3110 + double new_q; 1.3111 + double current_q = vp9_convert_qindex_to_q(cpi->active_worst_quality); 1.3112 + int level = cpi->this_frame_weight; 1.3113 + assert(level >= 0); 1.3114 + 1.3115 + // Set quantizer steps at 10% increments. 1.3116 + new_q = current_q * (1.0 - (0.2 * (cpi->max_arf_level - level))); 1.3117 + q = cpi->active_worst_quality + vp9_compute_qdelta(cpi, current_q, new_q); 1.3118 + 1.3119 + bottom_index = q; 1.3120 + top_index = q; 1.3121 + q_low = q; 1.3122 + q_high = q; 1.3123 + 1.3124 + printf("frame:%d q:%d\n", cm->current_video_frame, q); 1.3125 + } 1.3126 +#endif 1.3127 + 1.3128 + loop_count = 0; 1.3129 + vp9_zero(cpi->rd_tx_select_threshes); 1.3130 + 1.3131 + if (!frame_is_intra_only(cm)) { 1.3132 + cm->mcomp_filter_type = DEFAULT_INTERP_FILTER; 1.3133 + /* TODO: Decide this more intelligently */ 1.3134 + cm->allow_high_precision_mv = q < HIGH_PRECISION_MV_QTHRESH; 1.3135 + set_mvcost(cpi); 1.3136 + } 1.3137 + 1.3138 +#if CONFIG_VP9_POSTPROC 1.3139 + 1.3140 + if (cpi->oxcf.noise_sensitivity > 0) { 1.3141 + int l = 0; 1.3142 + 1.3143 + switch (cpi->oxcf.noise_sensitivity) { 1.3144 + case 1: 1.3145 + l = 20; 1.3146 + break; 1.3147 + case 2: 1.3148 + l = 40; 1.3149 + break; 1.3150 + case 3: 1.3151 + l = 60; 1.3152 + break; 1.3153 + case 4: 1.3154 + case 5: 1.3155 + l = 100; 1.3156 + break; 1.3157 + case 6: 1.3158 + l = 150; 1.3159 + break; 1.3160 + } 1.3161 + 1.3162 + vp9_denoise(cpi->Source, cpi->Source, l); 1.3163 + } 1.3164 + 1.3165 +#endif 1.3166 + 1.3167 +#ifdef OUTPUT_YUV_SRC 1.3168 + vp9_write_yuv_frame(cpi->Source); 1.3169 +#endif 1.3170 + 1.3171 + do { 1.3172 + vp9_clear_system_state(); // __asm emms; 1.3173 + 1.3174 + vp9_set_quantizer(cpi, q); 1.3175 + 1.3176 + if (loop_count == 0) { 1.3177 + // Set up entropy context depending on frame type. The decoder mandates 1.3178 + // the use of the default context, index 0, for keyframes and inter 1.3179 + // frames where the error_resilient_mode or intra_only flag is set. For 1.3180 + // other inter-frames the encoder currently uses only two contexts; 1.3181 + // context 1 for ALTREF frames and context 0 for the others. 1.3182 + if (cm->frame_type == KEY_FRAME) { 1.3183 + vp9_setup_key_frame(cpi); 1.3184 + } else { 1.3185 + if (!cm->intra_only && !cm->error_resilient_mode) { 1.3186 + cpi->common.frame_context_idx = cpi->refresh_alt_ref_frame; 1.3187 + } 1.3188 + vp9_setup_inter_frame(cpi); 1.3189 + } 1.3190 + } 1.3191 + 1.3192 + if (cpi->oxcf.aq_mode == VARIANCE_AQ) { 1.3193 + vp9_vaq_frame_setup(cpi); 1.3194 + } 1.3195 + 1.3196 + // transform / motion compensation build reconstruction frame 1.3197 + 1.3198 + vp9_encode_frame(cpi); 1.3199 + 1.3200 + // Update the skip mb flag probabilities based on the distribution 1.3201 + // seen in the last encoder iteration. 1.3202 + // update_base_skip_probs(cpi); 1.3203 + 1.3204 + vp9_clear_system_state(); // __asm emms; 1.3205 + 1.3206 + // Dummy pack of the bitstream using up to date stats to get an 1.3207 + // accurate estimate of output frame size to determine if we need 1.3208 + // to recode. 1.3209 + vp9_save_coding_context(cpi); 1.3210 + cpi->dummy_packing = 1; 1.3211 + vp9_pack_bitstream(cpi, dest, size); 1.3212 + cpi->projected_frame_size = (*size) << 3; 1.3213 + vp9_restore_coding_context(cpi); 1.3214 + 1.3215 + if (frame_over_shoot_limit == 0) 1.3216 + frame_over_shoot_limit = 1; 1.3217 + active_worst_qchanged = 0; 1.3218 + 1.3219 + if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { 1.3220 + loop = 0; 1.3221 + } else { 1.3222 + // Special case handling for forced key frames 1.3223 + if ((cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced) { 1.3224 + int last_q = q; 1.3225 + int kf_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm)); 1.3226 + 1.3227 + int high_err_target = cpi->ambient_err; 1.3228 + int low_err_target = cpi->ambient_err >> 1; 1.3229 + 1.3230 + // Prevent possible divide by zero error below for perfect KF 1.3231 + kf_err += !kf_err; 1.3232 + 1.3233 + // The key frame is not good enough or we can afford 1.3234 + // to make it better without undue risk of popping. 1.3235 + if ((kf_err > high_err_target && 1.3236 + cpi->projected_frame_size <= frame_over_shoot_limit) || 1.3237 + (kf_err > low_err_target && 1.3238 + cpi->projected_frame_size <= frame_under_shoot_limit)) { 1.3239 + // Lower q_high 1.3240 + q_high = q > q_low ? q - 1 : q_low; 1.3241 + 1.3242 + // Adjust Q 1.3243 + q = (q * high_err_target) / kf_err; 1.3244 + q = MIN(q, (q_high + q_low) >> 1); 1.3245 + } else if (kf_err < low_err_target && 1.3246 + cpi->projected_frame_size >= frame_under_shoot_limit) { 1.3247 + // The key frame is much better than the previous frame 1.3248 + // Raise q_low 1.3249 + q_low = q < q_high ? q + 1 : q_high; 1.3250 + 1.3251 + // Adjust Q 1.3252 + q = (q * low_err_target) / kf_err; 1.3253 + q = MIN(q, (q_high + q_low + 1) >> 1); 1.3254 + } 1.3255 + 1.3256 + // Clamp Q to upper and lower limits: 1.3257 + q = clamp(q, q_low, q_high); 1.3258 + 1.3259 + loop = q != last_q; 1.3260 + } else if (recode_loop_test( 1.3261 + cpi, frame_over_shoot_limit, frame_under_shoot_limit, 1.3262 + q, top_index, bottom_index)) { 1.3263 + // Is the projected frame size out of range and are we allowed 1.3264 + // to attempt to recode. 1.3265 + int last_q = q; 1.3266 + int retries = 0; 1.3267 + 1.3268 + // Frame size out of permitted range: 1.3269 + // Update correction factor & compute new Q to try... 1.3270 + 1.3271 + // Frame is too large 1.3272 + if (cpi->projected_frame_size > cpi->this_frame_target) { 1.3273 + // Raise Qlow as to at least the current value 1.3274 + q_low = q < q_high ? q + 1 : q_high; 1.3275 + 1.3276 + if (undershoot_seen || loop_count > 1) { 1.3277 + // Update rate_correction_factor unless 1.3278 + // cpi->active_worst_quality has changed. 1.3279 + if (!active_worst_qchanged) 1.3280 + vp9_update_rate_correction_factors(cpi, 1); 1.3281 + 1.3282 + q = (q_high + q_low + 1) / 2; 1.3283 + } else { 1.3284 + // Update rate_correction_factor unless 1.3285 + // cpi->active_worst_quality has changed. 1.3286 + if (!active_worst_qchanged) 1.3287 + vp9_update_rate_correction_factors(cpi, 0); 1.3288 + 1.3289 + q = vp9_regulate_q(cpi, cpi->this_frame_target); 1.3290 + 1.3291 + while (q < q_low && retries < 10) { 1.3292 + vp9_update_rate_correction_factors(cpi, 0); 1.3293 + q = vp9_regulate_q(cpi, cpi->this_frame_target); 1.3294 + retries++; 1.3295 + } 1.3296 + } 1.3297 + 1.3298 + overshoot_seen = 1; 1.3299 + } else { 1.3300 + // Frame is too small 1.3301 + q_high = q > q_low ? q - 1 : q_low; 1.3302 + 1.3303 + if (overshoot_seen || loop_count > 1) { 1.3304 + // Update rate_correction_factor unless 1.3305 + // cpi->active_worst_quality has changed. 1.3306 + if (!active_worst_qchanged) 1.3307 + vp9_update_rate_correction_factors(cpi, 1); 1.3308 + 1.3309 + q = (q_high + q_low) / 2; 1.3310 + } else { 1.3311 + // Update rate_correction_factor unless 1.3312 + // cpi->active_worst_quality has changed. 1.3313 + if (!active_worst_qchanged) 1.3314 + vp9_update_rate_correction_factors(cpi, 0); 1.3315 + 1.3316 + q = vp9_regulate_q(cpi, cpi->this_frame_target); 1.3317 + 1.3318 + // Special case reset for qlow for constrained quality. 1.3319 + // This should only trigger where there is very substantial 1.3320 + // undershoot on a frame and the auto cq level is above 1.3321 + // the user passsed in value. 1.3322 + if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY && q < q_low) { 1.3323 + q_low = q; 1.3324 + } 1.3325 + 1.3326 + while (q > q_high && retries < 10) { 1.3327 + vp9_update_rate_correction_factors(cpi, 0); 1.3328 + q = vp9_regulate_q(cpi, cpi->this_frame_target); 1.3329 + retries++; 1.3330 + } 1.3331 + } 1.3332 + 1.3333 + undershoot_seen = 1; 1.3334 + } 1.3335 + 1.3336 + // Clamp Q to upper and lower limits: 1.3337 + q = clamp(q, q_low, q_high); 1.3338 + 1.3339 + loop = q != last_q; 1.3340 + } else { 1.3341 + loop = 0; 1.3342 + } 1.3343 + } 1.3344 + 1.3345 + if (cpi->is_src_frame_alt_ref) 1.3346 + loop = 0; 1.3347 + 1.3348 + if (loop) { 1.3349 + loop_count++; 1.3350 + 1.3351 +#if CONFIG_INTERNAL_STATS 1.3352 + cpi->tot_recode_hits++; 1.3353 +#endif 1.3354 + } 1.3355 + } while (loop); 1.3356 + 1.3357 + // Special case code to reduce pulsing when key frames are forced at a 1.3358 + // fixed interval. Note the reconstruction error if it is the frame before 1.3359 + // the force key frame 1.3360 + if (cpi->next_key_frame_forced && (cpi->twopass.frames_to_key == 0)) { 1.3361 + cpi->ambient_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm)); 1.3362 + } 1.3363 + 1.3364 + if (cm->frame_type == KEY_FRAME) 1.3365 + cpi->refresh_last_frame = 1; 1.3366 + 1.3367 + cm->frame_to_show = get_frame_new_buffer(cm); 1.3368 + 1.3369 +#if WRITE_RECON_BUFFER 1.3370 + if (cm->show_frame) 1.3371 + write_cx_frame_to_file(cm->frame_to_show, 1.3372 + cm->current_video_frame); 1.3373 + else 1.3374 + write_cx_frame_to_file(cm->frame_to_show, 1.3375 + cm->current_video_frame + 1000); 1.3376 +#endif 1.3377 + 1.3378 + // Pick the loop filter level for the frame. 1.3379 + loopfilter_frame(cpi, cm); 1.3380 + 1.3381 +#if WRITE_RECON_BUFFER 1.3382 + if (cm->show_frame) 1.3383 + write_cx_frame_to_file(cm->frame_to_show, 1.3384 + cm->current_video_frame + 2000); 1.3385 + else 1.3386 + write_cx_frame_to_file(cm->frame_to_show, 1.3387 + cm->current_video_frame + 3000); 1.3388 +#endif 1.3389 + 1.3390 + // build the bitstream 1.3391 + cpi->dummy_packing = 0; 1.3392 + vp9_pack_bitstream(cpi, dest, size); 1.3393 + 1.3394 + if (cm->seg.update_map) 1.3395 + update_reference_segmentation_map(cpi); 1.3396 + 1.3397 + release_scaled_references(cpi); 1.3398 + update_reference_frames(cpi); 1.3399 + 1.3400 + for (t = TX_4X4; t <= TX_32X32; t++) 1.3401 + full_to_model_counts(cpi->common.counts.coef[t], 1.3402 + cpi->coef_counts[t]); 1.3403 + if (!cpi->common.error_resilient_mode && 1.3404 + !cpi->common.frame_parallel_decoding_mode) { 1.3405 + vp9_adapt_coef_probs(&cpi->common); 1.3406 + } 1.3407 + 1.3408 + if (!frame_is_intra_only(&cpi->common)) { 1.3409 + FRAME_COUNTS *counts = &cpi->common.counts; 1.3410 + 1.3411 + vp9_copy(counts->y_mode, cpi->y_mode_count); 1.3412 + vp9_copy(counts->uv_mode, cpi->y_uv_mode_count); 1.3413 + vp9_copy(counts->partition, cpi->partition_count); 1.3414 + vp9_copy(counts->intra_inter, cpi->intra_inter_count); 1.3415 + vp9_copy(counts->comp_inter, cpi->comp_inter_count); 1.3416 + vp9_copy(counts->single_ref, cpi->single_ref_count); 1.3417 + vp9_copy(counts->comp_ref, cpi->comp_ref_count); 1.3418 + counts->mv = cpi->NMVcount; 1.3419 + if (!cpi->common.error_resilient_mode && 1.3420 + !cpi->common.frame_parallel_decoding_mode) { 1.3421 + vp9_adapt_mode_probs(&cpi->common); 1.3422 + vp9_adapt_mv_probs(&cpi->common, cpi->common.allow_high_precision_mv); 1.3423 + } 1.3424 + } 1.3425 + 1.3426 +#ifdef ENTROPY_STATS 1.3427 + vp9_update_mode_context_stats(cpi); 1.3428 +#endif 1.3429 + 1.3430 + /* Move storing frame_type out of the above loop since it is also 1.3431 + * needed in motion search besides loopfilter */ 1.3432 + cm->last_frame_type = cm->frame_type; 1.3433 + 1.3434 + // Update rate control heuristics 1.3435 + cpi->total_byte_count += (*size); 1.3436 + cpi->projected_frame_size = (*size) << 3; 1.3437 + 1.3438 + // Post encode loop adjustment of Q prediction. 1.3439 + if (!active_worst_qchanged) 1.3440 + vp9_update_rate_correction_factors(cpi, (cpi->sf.recode_loop || 1.3441 + cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) ? 2 : 0); 1.3442 + 1.3443 + 1.3444 + cpi->last_q[cm->frame_type] = cm->base_qindex; 1.3445 + 1.3446 + // Keep record of last boosted (KF/KF/ARF) Q value. 1.3447 + // If the current frame is coded at a lower Q then we also update it. 1.3448 + // If all mbs in this group are skipped only update if the Q value is 1.3449 + // better than that already stored. 1.3450 + // This is used to help set quality in forced key frames to reduce popping 1.3451 + if ((cm->base_qindex < cpi->last_boosted_qindex) || 1.3452 + ((cpi->static_mb_pct < 100) && 1.3453 + ((cm->frame_type == KEY_FRAME) || 1.3454 + cpi->refresh_alt_ref_frame || 1.3455 + (cpi->refresh_golden_frame && !cpi->is_src_frame_alt_ref)))) { 1.3456 + cpi->last_boosted_qindex = cm->base_qindex; 1.3457 + } 1.3458 + 1.3459 + if (cm->frame_type == KEY_FRAME) { 1.3460 + vp9_adjust_key_frame_context(cpi); 1.3461 + } 1.3462 + 1.3463 + // Keep a record of ambient average Q. 1.3464 + if (cm->frame_type != KEY_FRAME) 1.3465 + cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex + 1.3466 + cm->base_qindex) >> 2; 1.3467 + 1.3468 + // Keep a record from which we can calculate the average Q excluding GF 1.3469 + // updates and key frames. 1.3470 + if (cm->frame_type != KEY_FRAME && 1.3471 + !cpi->refresh_golden_frame && 1.3472 + !cpi->refresh_alt_ref_frame) { 1.3473 + cpi->ni_frames++; 1.3474 + cpi->tot_q += vp9_convert_qindex_to_q(q); 1.3475 + cpi->avg_q = cpi->tot_q / (double)cpi->ni_frames; 1.3476 + 1.3477 + // Calculate the average Q for normal inter frames (not key or GFU frames). 1.3478 + cpi->ni_tot_qi += q; 1.3479 + cpi->ni_av_qi = cpi->ni_tot_qi / cpi->ni_frames; 1.3480 + } 1.3481 + 1.3482 + // Update the buffer level variable. 1.3483 + // Non-viewable frames are a special case and are treated as pure overhead. 1.3484 + if (!cm->show_frame) 1.3485 + cpi->bits_off_target -= cpi->projected_frame_size; 1.3486 + else 1.3487 + cpi->bits_off_target += cpi->av_per_frame_bandwidth - 1.3488 + cpi->projected_frame_size; 1.3489 + 1.3490 + // Clip the buffer level at the maximum buffer size 1.3491 + if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size) 1.3492 + cpi->bits_off_target = cpi->oxcf.maximum_buffer_size; 1.3493 + 1.3494 + // Rolling monitors of whether we are over or underspending used to help 1.3495 + // regulate min and Max Q in two pass. 1.3496 + if (cm->frame_type != KEY_FRAME) { 1.3497 + cpi->rolling_target_bits = 1.3498 + ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4; 1.3499 + cpi->rolling_actual_bits = 1.3500 + ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4; 1.3501 + cpi->long_rolling_target_bits = 1.3502 + ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32; 1.3503 + cpi->long_rolling_actual_bits = 1.3504 + ((cpi->long_rolling_actual_bits * 31) + 1.3505 + cpi->projected_frame_size + 16) / 32; 1.3506 + } 1.3507 + 1.3508 + // Actual bits spent 1.3509 + cpi->total_actual_bits += cpi->projected_frame_size; 1.3510 + 1.3511 + // Debug stats 1.3512 + cpi->total_target_vs_actual += (cpi->this_frame_target - 1.3513 + cpi->projected_frame_size); 1.3514 + 1.3515 + cpi->buffer_level = cpi->bits_off_target; 1.3516 + 1.3517 +#ifndef DISABLE_RC_LONG_TERM_MEM 1.3518 + // Update bits left to the kf and gf groups to account for overshoot or 1.3519 + // undershoot on these frames 1.3520 + if (cm->frame_type == KEY_FRAME) { 1.3521 + cpi->twopass.kf_group_bits += cpi->this_frame_target - 1.3522 + cpi->projected_frame_size; 1.3523 + 1.3524 + cpi->twopass.kf_group_bits = MAX(cpi->twopass.kf_group_bits, 0); 1.3525 + } else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) { 1.3526 + cpi->twopass.gf_group_bits += cpi->this_frame_target - 1.3527 + cpi->projected_frame_size; 1.3528 + 1.3529 + cpi->twopass.gf_group_bits = MAX(cpi->twopass.gf_group_bits, 0); 1.3530 + } 1.3531 +#endif 1.3532 + 1.3533 +#if 0 1.3534 + output_frame_level_debug_stats(cpi); 1.3535 +#endif 1.3536 + if (cpi->refresh_golden_frame == 1) 1.3537 + cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN; 1.3538 + else 1.3539 + cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_GOLDEN; 1.3540 + 1.3541 + if (cpi->refresh_alt_ref_frame == 1) 1.3542 + cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF; 1.3543 + else 1.3544 + cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_ALTREF; 1.3545 + 1.3546 + 1.3547 + if (cpi->refresh_last_frame & cpi->refresh_golden_frame) 1.3548 + cpi->gold_is_last = 1; 1.3549 + else if (cpi->refresh_last_frame ^ cpi->refresh_golden_frame) 1.3550 + cpi->gold_is_last = 0; 1.3551 + 1.3552 + if (cpi->refresh_last_frame & cpi->refresh_alt_ref_frame) 1.3553 + cpi->alt_is_last = 1; 1.3554 + else if (cpi->refresh_last_frame ^ cpi->refresh_alt_ref_frame) 1.3555 + cpi->alt_is_last = 0; 1.3556 + 1.3557 + if (cpi->refresh_alt_ref_frame & cpi->refresh_golden_frame) 1.3558 + cpi->gold_is_alt = 1; 1.3559 + else if (cpi->refresh_alt_ref_frame ^ cpi->refresh_golden_frame) 1.3560 + cpi->gold_is_alt = 0; 1.3561 + 1.3562 + cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG; 1.3563 + 1.3564 + if (cpi->gold_is_last) 1.3565 + cpi->ref_frame_flags &= ~VP9_GOLD_FLAG; 1.3566 + 1.3567 + if (cpi->alt_is_last) 1.3568 + cpi->ref_frame_flags &= ~VP9_ALT_FLAG; 1.3569 + 1.3570 + if (cpi->gold_is_alt) 1.3571 + cpi->ref_frame_flags &= ~VP9_ALT_FLAG; 1.3572 + 1.3573 + if (cpi->oxcf.play_alternate && cpi->refresh_alt_ref_frame 1.3574 + && (cm->frame_type != KEY_FRAME)) 1.3575 + // Update the alternate reference frame stats as appropriate. 1.3576 + update_alt_ref_frame_stats(cpi); 1.3577 + else 1.3578 + // Update the Golden frame stats as appropriate. 1.3579 + update_golden_frame_stats(cpi); 1.3580 + 1.3581 + if (cm->frame_type == KEY_FRAME) { 1.3582 + // Tell the caller that the frame was coded as a key frame 1.3583 + *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY; 1.3584 + 1.3585 +#if CONFIG_MULTIPLE_ARF 1.3586 + // Reset the sequence number. 1.3587 + if (cpi->multi_arf_enabled) { 1.3588 + cpi->sequence_number = 0; 1.3589 + cpi->frame_coding_order_period = cpi->new_frame_coding_order_period; 1.3590 + cpi->new_frame_coding_order_period = -1; 1.3591 + } 1.3592 +#endif 1.3593 + 1.3594 + // As this frame is a key frame the next defaults to an inter frame. 1.3595 + cm->frame_type = INTER_FRAME; 1.3596 + } else { 1.3597 + *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY; 1.3598 + 1.3599 +#if CONFIG_MULTIPLE_ARF 1.3600 + /* Increment position in the coded frame sequence. */ 1.3601 + if (cpi->multi_arf_enabled) { 1.3602 + ++cpi->sequence_number; 1.3603 + if (cpi->sequence_number >= cpi->frame_coding_order_period) { 1.3604 + cpi->sequence_number = 0; 1.3605 + cpi->frame_coding_order_period = cpi->new_frame_coding_order_period; 1.3606 + cpi->new_frame_coding_order_period = -1; 1.3607 + } 1.3608 + cpi->this_frame_weight = cpi->arf_weight[cpi->sequence_number]; 1.3609 + assert(cpi->this_frame_weight >= 0); 1.3610 + } 1.3611 +#endif 1.3612 + } 1.3613 + 1.3614 + // Clear the one shot update flags for segmentation map and mode/ref loop 1.3615 + // filter deltas. 1.3616 + cm->seg.update_map = 0; 1.3617 + cm->seg.update_data = 0; 1.3618 + cm->lf.mode_ref_delta_update = 0; 1.3619 + 1.3620 + // keep track of the last coded dimensions 1.3621 + cm->last_width = cm->width; 1.3622 + cm->last_height = cm->height; 1.3623 + 1.3624 + // reset to normal state now that we are done. 1.3625 + cm->last_show_frame = cm->show_frame; 1.3626 + if (cm->show_frame) { 1.3627 + // current mip will be the prev_mip for the next frame 1.3628 + MODE_INFO *temp = cm->prev_mip; 1.3629 + MODE_INFO **temp2 = cm->prev_mi_grid_base; 1.3630 + cm->prev_mip = cm->mip; 1.3631 + cm->mip = temp; 1.3632 + cm->prev_mi_grid_base = cm->mi_grid_base; 1.3633 + cm->mi_grid_base = temp2; 1.3634 + 1.3635 + // update the upper left visible macroblock ptrs 1.3636 + cm->mi = cm->mip + cm->mode_info_stride + 1; 1.3637 + cm->mi_grid_visible = cm->mi_grid_base + cm->mode_info_stride + 1; 1.3638 + 1.3639 + cpi->mb.e_mbd.mi_8x8 = cm->mi_grid_visible; 1.3640 + cpi->mb.e_mbd.mi_8x8[0] = cm->mi; 1.3641 + 1.3642 + // Don't increment frame counters if this was an altref buffer 1.3643 + // update not a real frame 1.3644 + ++cm->current_video_frame; 1.3645 + ++cpi->frames_since_key; 1.3646 + } 1.3647 + // restore prev_mi 1.3648 + cm->prev_mi = cm->prev_mip + cm->mode_info_stride + 1; 1.3649 + cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mode_info_stride + 1; 1.3650 +} 1.3651 + 1.3652 +static void Pass2Encode(VP9_COMP *cpi, unsigned long *size, 1.3653 + unsigned char *dest, unsigned int *frame_flags) { 1.3654 + cpi->enable_encode_breakout = 1; 1.3655 + 1.3656 + if (!cpi->refresh_alt_ref_frame) 1.3657 + vp9_second_pass(cpi); 1.3658 + 1.3659 + encode_frame_to_data_rate(cpi, size, dest, frame_flags); 1.3660 + // vp9_print_modes_and_motion_vectors(&cpi->common, "encode.stt"); 1.3661 +#ifdef DISABLE_RC_LONG_TERM_MEM 1.3662 + cpi->twopass.bits_left -= cpi->this_frame_target; 1.3663 +#else 1.3664 + cpi->twopass.bits_left -= 8 * *size; 1.3665 +#endif 1.3666 + 1.3667 + if (!cpi->refresh_alt_ref_frame) { 1.3668 + double lower_bounds_min_rate = FRAME_OVERHEAD_BITS * cpi->oxcf.framerate; 1.3669 + double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth 1.3670 + * cpi->oxcf.two_pass_vbrmin_section 1.3671 + / 100); 1.3672 + 1.3673 + if (two_pass_min_rate < lower_bounds_min_rate) 1.3674 + two_pass_min_rate = lower_bounds_min_rate; 1.3675 + 1.3676 + cpi->twopass.bits_left += (int64_t)(two_pass_min_rate 1.3677 + / cpi->oxcf.framerate); 1.3678 + } 1.3679 +} 1.3680 + 1.3681 +static void check_initial_width(VP9_COMP *cpi, YV12_BUFFER_CONFIG *sd) { 1.3682 + VP9_COMMON *cm = &cpi->common; 1.3683 + if (!cpi->initial_width) { 1.3684 + // TODO(jkoleszar): Support 1/4 subsampling? 1.3685 + cm->subsampling_x = (sd != NULL) && sd->uv_width < sd->y_width; 1.3686 + cm->subsampling_y = (sd != NULL) && sd->uv_height < sd->y_height; 1.3687 + alloc_raw_frame_buffers(cpi); 1.3688 + 1.3689 + cpi->initial_width = cm->width; 1.3690 + cpi->initial_height = cm->height; 1.3691 + } 1.3692 +} 1.3693 + 1.3694 + 1.3695 +int vp9_receive_raw_frame(VP9_PTR ptr, unsigned int frame_flags, 1.3696 + YV12_BUFFER_CONFIG *sd, int64_t time_stamp, 1.3697 + int64_t end_time) { 1.3698 + VP9_COMP *cpi = (VP9_COMP *) ptr; 1.3699 + struct vpx_usec_timer timer; 1.3700 + int res = 0; 1.3701 + 1.3702 + check_initial_width(cpi, sd); 1.3703 + vpx_usec_timer_start(&timer); 1.3704 + if (vp9_lookahead_push(cpi->lookahead, sd, time_stamp, end_time, frame_flags, 1.3705 + cpi->active_map_enabled ? cpi->active_map : NULL)) 1.3706 + res = -1; 1.3707 + vpx_usec_timer_mark(&timer); 1.3708 + cpi->time_receive_data += vpx_usec_timer_elapsed(&timer); 1.3709 + 1.3710 + return res; 1.3711 +} 1.3712 + 1.3713 + 1.3714 +static int frame_is_reference(const VP9_COMP *cpi) { 1.3715 + const VP9_COMMON *cm = &cpi->common; 1.3716 + 1.3717 + return cm->frame_type == KEY_FRAME || 1.3718 + cpi->refresh_last_frame || 1.3719 + cpi->refresh_golden_frame || 1.3720 + cpi->refresh_alt_ref_frame || 1.3721 + cm->refresh_frame_context || 1.3722 + cm->lf.mode_ref_delta_update || 1.3723 + cm->seg.update_map || 1.3724 + cm->seg.update_data; 1.3725 +} 1.3726 + 1.3727 +#if CONFIG_MULTIPLE_ARF 1.3728 +int is_next_frame_arf(VP9_COMP *cpi) { 1.3729 + // Negative entry in frame_coding_order indicates an ARF at this position. 1.3730 + return cpi->frame_coding_order[cpi->sequence_number + 1] < 0 ? 1 : 0; 1.3731 +} 1.3732 +#endif 1.3733 + 1.3734 +int vp9_get_compressed_data(VP9_PTR ptr, unsigned int *frame_flags, 1.3735 + unsigned long *size, unsigned char *dest, 1.3736 + int64_t *time_stamp, int64_t *time_end, int flush) { 1.3737 + VP9_COMP *cpi = (VP9_COMP *) ptr; 1.3738 + VP9_COMMON *cm = &cpi->common; 1.3739 + struct vpx_usec_timer cmptimer; 1.3740 + YV12_BUFFER_CONFIG *force_src_buffer = NULL; 1.3741 + int i; 1.3742 + // FILE *fp_out = fopen("enc_frame_type.txt", "a"); 1.3743 + 1.3744 + if (!cpi) 1.3745 + return -1; 1.3746 + 1.3747 + vpx_usec_timer_start(&cmptimer); 1.3748 + 1.3749 + cpi->source = NULL; 1.3750 + 1.3751 + cpi->common.allow_high_precision_mv = ALTREF_HIGH_PRECISION_MV; 1.3752 + set_mvcost(cpi); 1.3753 + 1.3754 + // Should we code an alternate reference frame. 1.3755 + if (cpi->oxcf.play_alternate && cpi->source_alt_ref_pending) { 1.3756 + int frames_to_arf; 1.3757 + 1.3758 +#if CONFIG_MULTIPLE_ARF 1.3759 + assert(!cpi->multi_arf_enabled || 1.3760 + cpi->frame_coding_order[cpi->sequence_number] < 0); 1.3761 + 1.3762 + if (cpi->multi_arf_enabled && (cpi->pass == 2)) 1.3763 + frames_to_arf = (-cpi->frame_coding_order[cpi->sequence_number]) 1.3764 + - cpi->next_frame_in_order; 1.3765 + else 1.3766 +#endif 1.3767 + frames_to_arf = cpi->frames_till_gf_update_due; 1.3768 + 1.3769 + assert(frames_to_arf < cpi->twopass.frames_to_key); 1.3770 + 1.3771 + if ((cpi->source = vp9_lookahead_peek(cpi->lookahead, frames_to_arf))) { 1.3772 +#if CONFIG_MULTIPLE_ARF 1.3773 + cpi->alt_ref_source[cpi->arf_buffered] = cpi->source; 1.3774 +#else 1.3775 + cpi->alt_ref_source = cpi->source; 1.3776 +#endif 1.3777 + 1.3778 + if (cpi->oxcf.arnr_max_frames > 0) { 1.3779 + // Produce the filtered ARF frame. 1.3780 + // TODO(agrange) merge these two functions. 1.3781 + configure_arnr_filter(cpi, cm->current_video_frame + frames_to_arf, 1.3782 + cpi->gfu_boost); 1.3783 + vp9_temporal_filter_prepare(cpi, frames_to_arf); 1.3784 + vp9_extend_frame_borders(&cpi->alt_ref_buffer, 1.3785 + cm->subsampling_x, cm->subsampling_y); 1.3786 + force_src_buffer = &cpi->alt_ref_buffer; 1.3787 + } 1.3788 + 1.3789 + cm->show_frame = 0; 1.3790 + cpi->refresh_alt_ref_frame = 1; 1.3791 + cpi->refresh_golden_frame = 0; 1.3792 + cpi->refresh_last_frame = 0; 1.3793 + cpi->is_src_frame_alt_ref = 0; 1.3794 + 1.3795 + // TODO(agrange) This needs to vary depending on where the next ARF is. 1.3796 + cpi->frames_till_alt_ref_frame = frames_to_arf; 1.3797 + 1.3798 +#if CONFIG_MULTIPLE_ARF 1.3799 + if (!cpi->multi_arf_enabled) 1.3800 +#endif 1.3801 + cpi->source_alt_ref_pending = 0; // Clear Pending altf Ref flag. 1.3802 + } 1.3803 + } 1.3804 + 1.3805 + if (!cpi->source) { 1.3806 +#if CONFIG_MULTIPLE_ARF 1.3807 + int i; 1.3808 +#endif 1.3809 + if ((cpi->source = vp9_lookahead_pop(cpi->lookahead, flush))) { 1.3810 + cm->show_frame = 1; 1.3811 + cm->intra_only = 0; 1.3812 + 1.3813 +#if CONFIG_MULTIPLE_ARF 1.3814 + // Is this frame the ARF overlay. 1.3815 + cpi->is_src_frame_alt_ref = 0; 1.3816 + for (i = 0; i < cpi->arf_buffered; ++i) { 1.3817 + if (cpi->source == cpi->alt_ref_source[i]) { 1.3818 + cpi->is_src_frame_alt_ref = 1; 1.3819 + cpi->refresh_golden_frame = 1; 1.3820 + break; 1.3821 + } 1.3822 + } 1.3823 +#else 1.3824 + cpi->is_src_frame_alt_ref = cpi->alt_ref_source 1.3825 + && (cpi->source == cpi->alt_ref_source); 1.3826 +#endif 1.3827 + if (cpi->is_src_frame_alt_ref) { 1.3828 + // Current frame is an ARF overlay frame. 1.3829 +#if CONFIG_MULTIPLE_ARF 1.3830 + cpi->alt_ref_source[i] = NULL; 1.3831 +#else 1.3832 + cpi->alt_ref_source = NULL; 1.3833 +#endif 1.3834 + // Don't refresh the last buffer for an ARF overlay frame. It will 1.3835 + // become the GF so preserve last as an alternative prediction option. 1.3836 + cpi->refresh_last_frame = 0; 1.3837 + } 1.3838 +#if CONFIG_MULTIPLE_ARF 1.3839 + ++cpi->next_frame_in_order; 1.3840 +#endif 1.3841 + } 1.3842 + } 1.3843 + 1.3844 + if (cpi->source) { 1.3845 + cpi->un_scaled_source = cpi->Source = force_src_buffer ? force_src_buffer 1.3846 + : &cpi->source->img; 1.3847 + *time_stamp = cpi->source->ts_start; 1.3848 + *time_end = cpi->source->ts_end; 1.3849 + *frame_flags = cpi->source->flags; 1.3850 + 1.3851 + // fprintf(fp_out, " Frame:%d", cm->current_video_frame); 1.3852 +#if CONFIG_MULTIPLE_ARF 1.3853 + if (cpi->multi_arf_enabled) { 1.3854 + // fprintf(fp_out, " seq_no:%d this_frame_weight:%d", 1.3855 + // cpi->sequence_number, cpi->this_frame_weight); 1.3856 + } else { 1.3857 + // fprintf(fp_out, "\n"); 1.3858 + } 1.3859 +#else 1.3860 + // fprintf(fp_out, "\n"); 1.3861 +#endif 1.3862 + 1.3863 +#if CONFIG_MULTIPLE_ARF 1.3864 + if ((cm->frame_type != KEY_FRAME) && (cpi->pass == 2)) 1.3865 + cpi->source_alt_ref_pending = is_next_frame_arf(cpi); 1.3866 +#endif 1.3867 + } else { 1.3868 + *size = 0; 1.3869 + if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done) { 1.3870 + vp9_end_first_pass(cpi); /* get last stats packet */ 1.3871 + cpi->twopass.first_pass_done = 1; 1.3872 + } 1.3873 + 1.3874 + // fclose(fp_out); 1.3875 + return -1; 1.3876 + } 1.3877 + 1.3878 + if (cpi->source->ts_start < cpi->first_time_stamp_ever) { 1.3879 + cpi->first_time_stamp_ever = cpi->source->ts_start; 1.3880 + cpi->last_end_time_stamp_seen = cpi->source->ts_start; 1.3881 + } 1.3882 + 1.3883 + // adjust frame rates based on timestamps given 1.3884 + if (!cpi->refresh_alt_ref_frame) { 1.3885 + int64_t this_duration; 1.3886 + int step = 0; 1.3887 + 1.3888 + if (cpi->source->ts_start == cpi->first_time_stamp_ever) { 1.3889 + this_duration = cpi->source->ts_end - cpi->source->ts_start; 1.3890 + step = 1; 1.3891 + } else { 1.3892 + int64_t last_duration = cpi->last_end_time_stamp_seen 1.3893 + - cpi->last_time_stamp_seen; 1.3894 + 1.3895 + this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen; 1.3896 + 1.3897 + // do a step update if the duration changes by 10% 1.3898 + if (last_duration) 1.3899 + step = (int)((this_duration - last_duration) * 10 / last_duration); 1.3900 + } 1.3901 + 1.3902 + if (this_duration) { 1.3903 + if (step) { 1.3904 + vp9_new_framerate(cpi, 10000000.0 / this_duration); 1.3905 + } else { 1.3906 + // Average this frame's rate into the last second's average 1.3907 + // frame rate. If we haven't seen 1 second yet, then average 1.3908 + // over the whole interval seen. 1.3909 + const double interval = MIN((double)(cpi->source->ts_end 1.3910 + - cpi->first_time_stamp_ever), 10000000.0); 1.3911 + double avg_duration = 10000000.0 / cpi->oxcf.framerate; 1.3912 + avg_duration *= (interval - avg_duration + this_duration); 1.3913 + avg_duration /= interval; 1.3914 + 1.3915 + vp9_new_framerate(cpi, 10000000.0 / avg_duration); 1.3916 + } 1.3917 + } 1.3918 + 1.3919 + cpi->last_time_stamp_seen = cpi->source->ts_start; 1.3920 + cpi->last_end_time_stamp_seen = cpi->source->ts_end; 1.3921 + } 1.3922 + 1.3923 + // start with a 0 size frame 1.3924 + *size = 0; 1.3925 + 1.3926 + // Clear down mmx registers 1.3927 + vp9_clear_system_state(); // __asm emms; 1.3928 + 1.3929 + /* find a free buffer for the new frame, releasing the reference previously 1.3930 + * held. 1.3931 + */ 1.3932 + cm->fb_idx_ref_cnt[cm->new_fb_idx]--; 1.3933 + cm->new_fb_idx = get_free_fb(cm); 1.3934 + 1.3935 +#if CONFIG_MULTIPLE_ARF 1.3936 + /* Set up the correct ARF frame. */ 1.3937 + if (cpi->refresh_alt_ref_frame) { 1.3938 + ++cpi->arf_buffered; 1.3939 + } 1.3940 + if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) && 1.3941 + (cpi->pass == 2)) { 1.3942 + cpi->alt_fb_idx = cpi->arf_buffer_idx[cpi->sequence_number]; 1.3943 + } 1.3944 +#endif 1.3945 + 1.3946 + /* Get the mapping of L/G/A to the reference buffer pool */ 1.3947 + cm->active_ref_idx[0] = cm->ref_frame_map[cpi->lst_fb_idx]; 1.3948 + cm->active_ref_idx[1] = cm->ref_frame_map[cpi->gld_fb_idx]; 1.3949 + cm->active_ref_idx[2] = cm->ref_frame_map[cpi->alt_fb_idx]; 1.3950 + 1.3951 +#if 0 // CONFIG_MULTIPLE_ARF 1.3952 + if (cpi->multi_arf_enabled) { 1.3953 + fprintf(fp_out, " idx(%d, %d, %d, %d) active(%d, %d, %d)", 1.3954 + cpi->lst_fb_idx, cpi->gld_fb_idx, cpi->alt_fb_idx, cm->new_fb_idx, 1.3955 + cm->active_ref_idx[0], cm->active_ref_idx[1], cm->active_ref_idx[2]); 1.3956 + if (cpi->refresh_alt_ref_frame) 1.3957 + fprintf(fp_out, " type:ARF"); 1.3958 + if (cpi->is_src_frame_alt_ref) 1.3959 + fprintf(fp_out, " type:OVERLAY[%d]", cpi->alt_fb_idx); 1.3960 + fprintf(fp_out, "\n"); 1.3961 + } 1.3962 +#endif 1.3963 + 1.3964 + cm->frame_type = INTER_FRAME; 1.3965 + cm->frame_flags = *frame_flags; 1.3966 + 1.3967 + // Reset the frame pointers to the current frame size 1.3968 + vp9_realloc_frame_buffer(get_frame_new_buffer(cm), 1.3969 + cm->width, cm->height, 1.3970 + cm->subsampling_x, cm->subsampling_y, 1.3971 + VP9BORDERINPIXELS); 1.3972 + 1.3973 + // Calculate scaling factors for each of the 3 available references 1.3974 + for (i = 0; i < ALLOWED_REFS_PER_FRAME; ++i) 1.3975 + vp9_setup_scale_factors(cm, i); 1.3976 + 1.3977 + vp9_setup_interp_filters(&cpi->mb.e_mbd, DEFAULT_INTERP_FILTER, cm); 1.3978 + 1.3979 + if (cpi->oxcf.aq_mode == VARIANCE_AQ) { 1.3980 + vp9_vaq_init(); 1.3981 + } 1.3982 + 1.3983 + if (cpi->pass == 1) { 1.3984 + Pass1Encode(cpi, size, dest, frame_flags); 1.3985 + } else if (cpi->pass == 2) { 1.3986 + Pass2Encode(cpi, size, dest, frame_flags); 1.3987 + } else { 1.3988 + encode_frame_to_data_rate(cpi, size, dest, frame_flags); 1.3989 + } 1.3990 + 1.3991 + if (cm->refresh_frame_context) 1.3992 + cm->frame_contexts[cm->frame_context_idx] = cm->fc; 1.3993 + 1.3994 + if (*size > 0) { 1.3995 + // if its a dropped frame honor the requests on subsequent frames 1.3996 + cpi->droppable = !frame_is_reference(cpi); 1.3997 + 1.3998 + // return to normal state 1.3999 + cm->reset_frame_context = 0; 1.4000 + cm->refresh_frame_context = 1; 1.4001 + cpi->refresh_alt_ref_frame = 0; 1.4002 + cpi->refresh_golden_frame = 0; 1.4003 + cpi->refresh_last_frame = 1; 1.4004 + cm->frame_type = INTER_FRAME; 1.4005 + } 1.4006 + 1.4007 + vpx_usec_timer_mark(&cmptimer); 1.4008 + cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer); 1.4009 + 1.4010 + if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame) 1.4011 + generate_psnr_packet(cpi); 1.4012 + 1.4013 +#if CONFIG_INTERNAL_STATS 1.4014 + 1.4015 + if (cpi->pass != 1) { 1.4016 + cpi->bytes += *size; 1.4017 + 1.4018 + if (cm->show_frame) { 1.4019 + cpi->count++; 1.4020 + 1.4021 + if (cpi->b_calculate_psnr) { 1.4022 + double ye, ue, ve; 1.4023 + double frame_psnr; 1.4024 + YV12_BUFFER_CONFIG *orig = cpi->Source; 1.4025 + YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show; 1.4026 + YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer; 1.4027 + int y_samples = orig->y_height * orig->y_width; 1.4028 + int uv_samples = orig->uv_height * orig->uv_width; 1.4029 + int t_samples = y_samples + 2 * uv_samples; 1.4030 + double sq_error; 1.4031 + 1.4032 + ye = (double)calc_plane_error(orig->y_buffer, orig->y_stride, 1.4033 + recon->y_buffer, recon->y_stride, 1.4034 + orig->y_crop_width, orig->y_crop_height); 1.4035 + 1.4036 + ue = (double)calc_plane_error(orig->u_buffer, orig->uv_stride, 1.4037 + recon->u_buffer, recon->uv_stride, 1.4038 + orig->uv_crop_width, orig->uv_crop_height); 1.4039 + 1.4040 + ve = (double)calc_plane_error(orig->v_buffer, orig->uv_stride, 1.4041 + recon->v_buffer, recon->uv_stride, 1.4042 + orig->uv_crop_width, orig->uv_crop_height); 1.4043 + 1.4044 + sq_error = ye + ue + ve; 1.4045 + 1.4046 + frame_psnr = vp9_mse2psnr(t_samples, 255.0, sq_error); 1.4047 + 1.4048 + cpi->total_y += vp9_mse2psnr(y_samples, 255.0, ye); 1.4049 + cpi->total_u += vp9_mse2psnr(uv_samples, 255.0, ue); 1.4050 + cpi->total_v += vp9_mse2psnr(uv_samples, 255.0, ve); 1.4051 + cpi->total_sq_error += sq_error; 1.4052 + cpi->total += frame_psnr; 1.4053 + { 1.4054 + double frame_psnr2, frame_ssim2 = 0; 1.4055 + double weight = 0; 1.4056 +#if CONFIG_VP9_POSTPROC 1.4057 + vp9_deblock(cm->frame_to_show, &cm->post_proc_buffer, 1.4058 + cm->lf.filter_level * 10 / 6); 1.4059 +#endif 1.4060 + vp9_clear_system_state(); 1.4061 + 1.4062 + ye = (double)calc_plane_error(orig->y_buffer, orig->y_stride, 1.4063 + pp->y_buffer, pp->y_stride, 1.4064 + orig->y_crop_width, orig->y_crop_height); 1.4065 + 1.4066 + ue = (double)calc_plane_error(orig->u_buffer, orig->uv_stride, 1.4067 + pp->u_buffer, pp->uv_stride, 1.4068 + orig->uv_crop_width, orig->uv_crop_height); 1.4069 + 1.4070 + ve = (double)calc_plane_error(orig->v_buffer, orig->uv_stride, 1.4071 + pp->v_buffer, pp->uv_stride, 1.4072 + orig->uv_crop_width, orig->uv_crop_height); 1.4073 + 1.4074 + sq_error = ye + ue + ve; 1.4075 + 1.4076 + frame_psnr2 = vp9_mse2psnr(t_samples, 255.0, sq_error); 1.4077 + 1.4078 + cpi->totalp_y += vp9_mse2psnr(y_samples, 255.0, ye); 1.4079 + cpi->totalp_u += vp9_mse2psnr(uv_samples, 255.0, ue); 1.4080 + cpi->totalp_v += vp9_mse2psnr(uv_samples, 255.0, ve); 1.4081 + cpi->total_sq_error2 += sq_error; 1.4082 + cpi->totalp += frame_psnr2; 1.4083 + 1.4084 + frame_ssim2 = vp9_calc_ssim(cpi->Source, 1.4085 + recon, 1, &weight); 1.4086 + 1.4087 + cpi->summed_quality += frame_ssim2 * weight; 1.4088 + cpi->summed_weights += weight; 1.4089 + 1.4090 + frame_ssim2 = vp9_calc_ssim(cpi->Source, 1.4091 + &cm->post_proc_buffer, 1, &weight); 1.4092 + 1.4093 + cpi->summedp_quality += frame_ssim2 * weight; 1.4094 + cpi->summedp_weights += weight; 1.4095 +#if 0 1.4096 + { 1.4097 + FILE *f = fopen("q_used.stt", "a"); 1.4098 + fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n", 1.4099 + cpi->common.current_video_frame, y2, u2, v2, 1.4100 + frame_psnr2, frame_ssim2); 1.4101 + fclose(f); 1.4102 + } 1.4103 +#endif 1.4104 + } 1.4105 + } 1.4106 + 1.4107 + if (cpi->b_calculate_ssimg) { 1.4108 + double y, u, v, frame_all; 1.4109 + frame_all = vp9_calc_ssimg(cpi->Source, cm->frame_to_show, 1.4110 + &y, &u, &v); 1.4111 + cpi->total_ssimg_y += y; 1.4112 + cpi->total_ssimg_u += u; 1.4113 + cpi->total_ssimg_v += v; 1.4114 + cpi->total_ssimg_all += frame_all; 1.4115 + } 1.4116 + } 1.4117 + } 1.4118 + 1.4119 +#endif 1.4120 + // fclose(fp_out); 1.4121 + return 0; 1.4122 +} 1.4123 + 1.4124 +int vp9_get_preview_raw_frame(VP9_PTR comp, YV12_BUFFER_CONFIG *dest, 1.4125 + vp9_ppflags_t *flags) { 1.4126 + VP9_COMP *cpi = (VP9_COMP *) comp; 1.4127 + 1.4128 + if (!cpi->common.show_frame) { 1.4129 + return -1; 1.4130 + } else { 1.4131 + int ret; 1.4132 +#if CONFIG_VP9_POSTPROC 1.4133 + ret = vp9_post_proc_frame(&cpi->common, dest, flags); 1.4134 +#else 1.4135 + 1.4136 + if (cpi->common.frame_to_show) { 1.4137 + *dest = *cpi->common.frame_to_show; 1.4138 + dest->y_width = cpi->common.width; 1.4139 + dest->y_height = cpi->common.height; 1.4140 + dest->uv_height = cpi->common.height / 2; 1.4141 + ret = 0; 1.4142 + } else { 1.4143 + ret = -1; 1.4144 + } 1.4145 + 1.4146 +#endif // !CONFIG_VP9_POSTPROC 1.4147 + vp9_clear_system_state(); 1.4148 + return ret; 1.4149 + } 1.4150 +} 1.4151 + 1.4152 +int vp9_set_roimap(VP9_PTR comp, unsigned char *map, unsigned int rows, 1.4153 + unsigned int cols, int delta_q[MAX_SEGMENTS], 1.4154 + int delta_lf[MAX_SEGMENTS], 1.4155 + unsigned int threshold[MAX_SEGMENTS]) { 1.4156 + VP9_COMP *cpi = (VP9_COMP *) comp; 1.4157 + signed char feature_data[SEG_LVL_MAX][MAX_SEGMENTS]; 1.4158 + struct segmentation *seg = &cpi->common.seg; 1.4159 + int i; 1.4160 + 1.4161 + if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols) 1.4162 + return -1; 1.4163 + 1.4164 + if (!map) { 1.4165 + vp9_disable_segmentation((VP9_PTR)cpi); 1.4166 + return 0; 1.4167 + } 1.4168 + 1.4169 + // Set the segmentation Map 1.4170 + vp9_set_segmentation_map((VP9_PTR)cpi, map); 1.4171 + 1.4172 + // Activate segmentation. 1.4173 + vp9_enable_segmentation((VP9_PTR)cpi); 1.4174 + 1.4175 + // Set up the quant, LF and breakout threshold segment data 1.4176 + for (i = 0; i < MAX_SEGMENTS; i++) { 1.4177 + feature_data[SEG_LVL_ALT_Q][i] = delta_q[i]; 1.4178 + feature_data[SEG_LVL_ALT_LF][i] = delta_lf[i]; 1.4179 + cpi->segment_encode_breakout[i] = threshold[i]; 1.4180 + } 1.4181 + 1.4182 + // Enable the loop and quant changes in the feature mask 1.4183 + for (i = 0; i < MAX_SEGMENTS; i++) { 1.4184 + if (delta_q[i]) 1.4185 + vp9_enable_segfeature(seg, i, SEG_LVL_ALT_Q); 1.4186 + else 1.4187 + vp9_disable_segfeature(seg, i, SEG_LVL_ALT_Q); 1.4188 + 1.4189 + if (delta_lf[i]) 1.4190 + vp9_enable_segfeature(seg, i, SEG_LVL_ALT_LF); 1.4191 + else 1.4192 + vp9_disable_segfeature(seg, i, SEG_LVL_ALT_LF); 1.4193 + } 1.4194 + 1.4195 + // Initialize the feature data structure 1.4196 + // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1 1.4197 + vp9_set_segment_data((VP9_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA); 1.4198 + 1.4199 + return 0; 1.4200 +} 1.4201 + 1.4202 +int vp9_set_active_map(VP9_PTR comp, unsigned char *map, 1.4203 + unsigned int rows, unsigned int cols) { 1.4204 + VP9_COMP *cpi = (VP9_COMP *) comp; 1.4205 + 1.4206 + if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) { 1.4207 + if (map) { 1.4208 + vpx_memcpy(cpi->active_map, map, rows * cols); 1.4209 + cpi->active_map_enabled = 1; 1.4210 + } else { 1.4211 + cpi->active_map_enabled = 0; 1.4212 + } 1.4213 + 1.4214 + return 0; 1.4215 + } else { 1.4216 + // cpi->active_map_enabled = 0; 1.4217 + return -1; 1.4218 + } 1.4219 +} 1.4220 + 1.4221 +int vp9_set_internal_size(VP9_PTR comp, 1.4222 + VPX_SCALING horiz_mode, VPX_SCALING vert_mode) { 1.4223 + VP9_COMP *cpi = (VP9_COMP *) comp; 1.4224 + VP9_COMMON *cm = &cpi->common; 1.4225 + int hr = 0, hs = 0, vr = 0, vs = 0; 1.4226 + 1.4227 + if (horiz_mode > ONETWO || vert_mode > ONETWO) 1.4228 + return -1; 1.4229 + 1.4230 + Scale2Ratio(horiz_mode, &hr, &hs); 1.4231 + Scale2Ratio(vert_mode, &vr, &vs); 1.4232 + 1.4233 + // always go to the next whole number 1.4234 + cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs; 1.4235 + cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs; 1.4236 + 1.4237 + assert(cm->width <= cpi->initial_width); 1.4238 + assert(cm->height <= cpi->initial_height); 1.4239 + update_frame_size(cpi); 1.4240 + return 0; 1.4241 +} 1.4242 + 1.4243 +int vp9_set_size_literal(VP9_PTR comp, unsigned int width, 1.4244 + unsigned int height) { 1.4245 + VP9_COMP *cpi = (VP9_COMP *)comp; 1.4246 + VP9_COMMON *cm = &cpi->common; 1.4247 + 1.4248 + check_initial_width(cpi, NULL); 1.4249 + 1.4250 + if (width) { 1.4251 + cm->width = width; 1.4252 + if (cm->width * 5 < cpi->initial_width) { 1.4253 + cm->width = cpi->initial_width / 5 + 1; 1.4254 + printf("Warning: Desired width too small, changed to %d \n", cm->width); 1.4255 + } 1.4256 + if (cm->width > cpi->initial_width) { 1.4257 + cm->width = cpi->initial_width; 1.4258 + printf("Warning: Desired width too large, changed to %d \n", cm->width); 1.4259 + } 1.4260 + } 1.4261 + 1.4262 + if (height) { 1.4263 + cm->height = height; 1.4264 + if (cm->height * 5 < cpi->initial_height) { 1.4265 + cm->height = cpi->initial_height / 5 + 1; 1.4266 + printf("Warning: Desired height too small, changed to %d \n", cm->height); 1.4267 + } 1.4268 + if (cm->height > cpi->initial_height) { 1.4269 + cm->height = cpi->initial_height; 1.4270 + printf("Warning: Desired height too large, changed to %d \n", cm->height); 1.4271 + } 1.4272 + } 1.4273 + 1.4274 + assert(cm->width <= cpi->initial_width); 1.4275 + assert(cm->height <= cpi->initial_height); 1.4276 + update_frame_size(cpi); 1.4277 + return 0; 1.4278 +} 1.4279 + 1.4280 +void vp9_set_svc(VP9_PTR comp, int use_svc) { 1.4281 + VP9_COMP *cpi = (VP9_COMP *)comp; 1.4282 + cpi->use_svc = use_svc; 1.4283 + return; 1.4284 +} 1.4285 + 1.4286 +int vp9_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest) { 1.4287 + int i, j; 1.4288 + int total = 0; 1.4289 + 1.4290 + uint8_t *src = source->y_buffer; 1.4291 + uint8_t *dst = dest->y_buffer; 1.4292 + 1.4293 + // Loop through the Y plane raw and reconstruction data summing 1.4294 + // (square differences) 1.4295 + for (i = 0; i < source->y_height; i += 16) { 1.4296 + for (j = 0; j < source->y_width; j += 16) { 1.4297 + unsigned int sse; 1.4298 + total += vp9_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride, 1.4299 + &sse); 1.4300 + } 1.4301 + 1.4302 + src += 16 * source->y_stride; 1.4303 + dst += 16 * dest->y_stride; 1.4304 + } 1.4305 + 1.4306 + return total; 1.4307 +} 1.4308 + 1.4309 + 1.4310 +int vp9_get_quantizer(VP9_PTR c) { 1.4311 + return ((VP9_COMP *)c)->common.base_qindex; 1.4312 +}