1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libvpx/vp8/encoder/onyx_if.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,5596 @@ 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 + 1.15 +#include "vpx_config.h" 1.16 +#include "./vpx_scale_rtcd.h" 1.17 +#include "vp8/common/onyxc_int.h" 1.18 +#include "vp8/common/blockd.h" 1.19 +#include "onyx_int.h" 1.20 +#include "vp8/common/systemdependent.h" 1.21 +#include "quantize.h" 1.22 +#include "vp8/common/alloccommon.h" 1.23 +#include "mcomp.h" 1.24 +#include "firstpass.h" 1.25 +#include "psnr.h" 1.26 +#include "vpx_scale/vpx_scale.h" 1.27 +#include "vp8/common/extend.h" 1.28 +#include "ratectrl.h" 1.29 +#include "vp8/common/quant_common.h" 1.30 +#include "segmentation.h" 1.31 +#if CONFIG_POSTPROC 1.32 +#include "vp8/common/postproc.h" 1.33 +#endif 1.34 +#include "vpx_mem/vpx_mem.h" 1.35 +#include "vp8/common/swapyv12buffer.h" 1.36 +#include "vp8/common/threading.h" 1.37 +#include "vpx_ports/vpx_timer.h" 1.38 +#if ARCH_ARM 1.39 +#include "vpx_ports/arm.h" 1.40 +#endif 1.41 +#if CONFIG_MULTI_RES_ENCODING 1.42 +#include "mr_dissim.h" 1.43 +#endif 1.44 +#include "encodeframe.h" 1.45 + 1.46 +#include <math.h> 1.47 +#include <stdio.h> 1.48 +#include <limits.h> 1.49 + 1.50 +#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING 1.51 +extern int vp8_update_coef_context(VP8_COMP *cpi); 1.52 +extern void vp8_update_coef_probs(VP8_COMP *cpi); 1.53 +#endif 1.54 + 1.55 +extern void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi); 1.56 +extern void vp8cx_set_alt_lf_level(VP8_COMP *cpi, int filt_val); 1.57 +extern void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi); 1.58 + 1.59 +extern void vp8_deblock_frame(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *post, int filt_lvl, int low_var_thresh, int flag); 1.60 +extern void print_parms(VP8_CONFIG *ocf, char *filenam); 1.61 +extern unsigned int vp8_get_processor_freq(); 1.62 +extern void print_tree_update_probs(); 1.63 +extern int vp8cx_create_encoder_threads(VP8_COMP *cpi); 1.64 +extern void vp8cx_remove_encoder_threads(VP8_COMP *cpi); 1.65 + 1.66 +int vp8_estimate_entropy_savings(VP8_COMP *cpi); 1.67 + 1.68 +int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest); 1.69 + 1.70 +extern void vp8_temporal_filter_prepare_c(VP8_COMP *cpi, int distance); 1.71 + 1.72 +static void set_default_lf_deltas(VP8_COMP *cpi); 1.73 + 1.74 +extern const int vp8_gf_interval_table[101]; 1.75 + 1.76 +#if CONFIG_INTERNAL_STATS 1.77 +#include "math.h" 1.78 + 1.79 +extern double vp8_calc_ssim 1.80 +( 1.81 + YV12_BUFFER_CONFIG *source, 1.82 + YV12_BUFFER_CONFIG *dest, 1.83 + int lumamask, 1.84 + double *weight 1.85 +); 1.86 + 1.87 + 1.88 +extern double vp8_calc_ssimg 1.89 +( 1.90 + YV12_BUFFER_CONFIG *source, 1.91 + YV12_BUFFER_CONFIG *dest, 1.92 + double *ssim_y, 1.93 + double *ssim_u, 1.94 + double *ssim_v 1.95 +); 1.96 + 1.97 + 1.98 +#endif 1.99 + 1.100 + 1.101 +#ifdef OUTPUT_YUV_SRC 1.102 +FILE *yuv_file; 1.103 +#endif 1.104 + 1.105 +#if 0 1.106 +FILE *framepsnr; 1.107 +FILE *kf_list; 1.108 +FILE *keyfile; 1.109 +#endif 1.110 + 1.111 +#if 0 1.112 +extern int skip_true_count; 1.113 +extern int skip_false_count; 1.114 +#endif 1.115 + 1.116 + 1.117 +#ifdef VP8_ENTROPY_STATS 1.118 +extern int intra_mode_stats[10][10][10]; 1.119 +#endif 1.120 + 1.121 +#ifdef SPEEDSTATS 1.122 +unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 1.123 +unsigned int tot_pm = 0; 1.124 +unsigned int cnt_pm = 0; 1.125 +unsigned int tot_ef = 0; 1.126 +unsigned int cnt_ef = 0; 1.127 +#endif 1.128 + 1.129 +#ifdef MODE_STATS 1.130 +extern unsigned __int64 Sectionbits[50]; 1.131 +extern int y_modes[5] ; 1.132 +extern int uv_modes[4] ; 1.133 +extern int b_modes[10] ; 1.134 + 1.135 +extern int inter_y_modes[10] ; 1.136 +extern int inter_uv_modes[4] ; 1.137 +extern unsigned int inter_b_modes[15]; 1.138 +#endif 1.139 + 1.140 +extern const int vp8_bits_per_mb[2][QINDEX_RANGE]; 1.141 + 1.142 +extern const int qrounding_factors[129]; 1.143 +extern const int qzbin_factors[129]; 1.144 +extern void vp8cx_init_quantizer(VP8_COMP *cpi); 1.145 +extern const int vp8cx_base_skip_false_prob[128]; 1.146 + 1.147 +/* Tables relating active max Q to active min Q */ 1.148 +static const unsigned char kf_low_motion_minq[QINDEX_RANGE] = 1.149 +{ 1.150 + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1.151 + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1.152 + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1.153 + 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2, 1.154 + 3,3,3,3,3,3,4,4,4,5,5,5,5,5,6,6, 1.155 + 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11, 1.156 + 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16, 1.157 + 16,16,17,17,18,18,18,18,19,20,20,21,21,22,23,23 1.158 +}; 1.159 +static const unsigned char kf_high_motion_minq[QINDEX_RANGE] = 1.160 +{ 1.161 + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1.162 + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1.163 + 1,1,1,1,1,1,1,1,2,2,2,2,3,3,3,3, 1.164 + 3,3,3,3,4,4,4,4,5,5,5,5,5,5,6,6, 1.165 + 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11, 1.166 + 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16, 1.167 + 16,16,17,17,18,18,18,18,19,19,20,20,20,20,21,21, 1.168 + 21,21,22,22,23,23,24,25,25,26,26,27,28,28,29,30 1.169 +}; 1.170 +static const unsigned char gf_low_motion_minq[QINDEX_RANGE] = 1.171 +{ 1.172 + 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2, 1.173 + 3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6, 1.174 + 7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10, 1.175 + 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18, 1.176 + 19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26, 1.177 + 27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34, 1.178 + 35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42, 1.179 + 43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58 1.180 +}; 1.181 +static const unsigned char gf_mid_motion_minq[QINDEX_RANGE] = 1.182 +{ 1.183 + 0,0,0,0,1,1,1,1,1,1,2,2,3,3,3,4, 1.184 + 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9, 1.185 + 9,10,10,10,10,11,11,11,12,12,12,12,13,13,13,14, 1.186 + 14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21, 1.187 + 22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29, 1.188 + 30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37, 1.189 + 38,39,39,40,40,41,41,42,42,43,43,44,45,46,47,48, 1.190 + 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64 1.191 +}; 1.192 +static const unsigned char gf_high_motion_minq[QINDEX_RANGE] = 1.193 +{ 1.194 + 0,0,0,0,1,1,1,1,1,2,2,2,3,3,3,4, 1.195 + 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9, 1.196 + 9,10,10,10,11,11,12,12,13,13,14,14,15,15,16,16, 1.197 + 17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24, 1.198 + 25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32, 1.199 + 33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40, 1.200 + 41,41,42,42,43,44,45,46,47,48,49,50,51,52,53,54, 1.201 + 55,56,57,58,59,60,62,64,66,68,70,72,74,76,78,80 1.202 +}; 1.203 +static const unsigned char inter_minq[QINDEX_RANGE] = 1.204 +{ 1.205 + 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9, 1.206 + 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20, 1.207 + 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31, 1.208 + 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43, 1.209 + 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56, 1.210 + 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70, 1.211 + 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85, 1.212 + 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100 1.213 +}; 1.214 + 1.215 +#ifdef PACKET_TESTING 1.216 +extern FILE *vpxlogc; 1.217 +#endif 1.218 + 1.219 +static void save_layer_context(VP8_COMP *cpi) 1.220 +{ 1.221 + LAYER_CONTEXT *lc = &cpi->layer_context[cpi->current_layer]; 1.222 + 1.223 + /* Save layer dependent coding state */ 1.224 + lc->target_bandwidth = cpi->target_bandwidth; 1.225 + lc->starting_buffer_level = cpi->oxcf.starting_buffer_level; 1.226 + lc->optimal_buffer_level = cpi->oxcf.optimal_buffer_level; 1.227 + lc->maximum_buffer_size = cpi->oxcf.maximum_buffer_size; 1.228 + lc->starting_buffer_level_in_ms = cpi->oxcf.starting_buffer_level_in_ms; 1.229 + lc->optimal_buffer_level_in_ms = cpi->oxcf.optimal_buffer_level_in_ms; 1.230 + lc->maximum_buffer_size_in_ms = cpi->oxcf.maximum_buffer_size_in_ms; 1.231 + lc->buffer_level = cpi->buffer_level; 1.232 + lc->bits_off_target = cpi->bits_off_target; 1.233 + lc->total_actual_bits = cpi->total_actual_bits; 1.234 + lc->worst_quality = cpi->worst_quality; 1.235 + lc->active_worst_quality = cpi->active_worst_quality; 1.236 + lc->best_quality = cpi->best_quality; 1.237 + lc->active_best_quality = cpi->active_best_quality; 1.238 + lc->ni_av_qi = cpi->ni_av_qi; 1.239 + lc->ni_tot_qi = cpi->ni_tot_qi; 1.240 + lc->ni_frames = cpi->ni_frames; 1.241 + lc->avg_frame_qindex = cpi->avg_frame_qindex; 1.242 + lc->rate_correction_factor = cpi->rate_correction_factor; 1.243 + lc->key_frame_rate_correction_factor = cpi->key_frame_rate_correction_factor; 1.244 + lc->gf_rate_correction_factor = cpi->gf_rate_correction_factor; 1.245 + lc->zbin_over_quant = cpi->mb.zbin_over_quant; 1.246 + lc->inter_frame_target = cpi->inter_frame_target; 1.247 + lc->total_byte_count = cpi->total_byte_count; 1.248 + lc->filter_level = cpi->common.filter_level; 1.249 + 1.250 + lc->last_frame_percent_intra = cpi->last_frame_percent_intra; 1.251 + 1.252 + memcpy (lc->count_mb_ref_frame_usage, 1.253 + cpi->mb.count_mb_ref_frame_usage, 1.254 + sizeof(cpi->mb.count_mb_ref_frame_usage)); 1.255 +} 1.256 + 1.257 +static void restore_layer_context(VP8_COMP *cpi, const int layer) 1.258 +{ 1.259 + LAYER_CONTEXT *lc = &cpi->layer_context[layer]; 1.260 + 1.261 + /* Restore layer dependent coding state */ 1.262 + cpi->current_layer = layer; 1.263 + cpi->target_bandwidth = lc->target_bandwidth; 1.264 + cpi->oxcf.target_bandwidth = lc->target_bandwidth; 1.265 + cpi->oxcf.starting_buffer_level = lc->starting_buffer_level; 1.266 + cpi->oxcf.optimal_buffer_level = lc->optimal_buffer_level; 1.267 + cpi->oxcf.maximum_buffer_size = lc->maximum_buffer_size; 1.268 + cpi->oxcf.starting_buffer_level_in_ms = lc->starting_buffer_level_in_ms; 1.269 + cpi->oxcf.optimal_buffer_level_in_ms = lc->optimal_buffer_level_in_ms; 1.270 + cpi->oxcf.maximum_buffer_size_in_ms = lc->maximum_buffer_size_in_ms; 1.271 + cpi->buffer_level = lc->buffer_level; 1.272 + cpi->bits_off_target = lc->bits_off_target; 1.273 + cpi->total_actual_bits = lc->total_actual_bits; 1.274 + cpi->active_worst_quality = lc->active_worst_quality; 1.275 + cpi->active_best_quality = lc->active_best_quality; 1.276 + cpi->ni_av_qi = lc->ni_av_qi; 1.277 + cpi->ni_tot_qi = lc->ni_tot_qi; 1.278 + cpi->ni_frames = lc->ni_frames; 1.279 + cpi->avg_frame_qindex = lc->avg_frame_qindex; 1.280 + cpi->rate_correction_factor = lc->rate_correction_factor; 1.281 + cpi->key_frame_rate_correction_factor = lc->key_frame_rate_correction_factor; 1.282 + cpi->gf_rate_correction_factor = lc->gf_rate_correction_factor; 1.283 + cpi->mb.zbin_over_quant = lc->zbin_over_quant; 1.284 + cpi->inter_frame_target = lc->inter_frame_target; 1.285 + cpi->total_byte_count = lc->total_byte_count; 1.286 + cpi->common.filter_level = lc->filter_level; 1.287 + 1.288 + cpi->last_frame_percent_intra = lc->last_frame_percent_intra; 1.289 + 1.290 + memcpy (cpi->mb.count_mb_ref_frame_usage, 1.291 + lc->count_mb_ref_frame_usage, 1.292 + sizeof(cpi->mb.count_mb_ref_frame_usage)); 1.293 +} 1.294 + 1.295 +static int rescale(int val, int num, int denom) 1.296 +{ 1.297 + int64_t llnum = num; 1.298 + int64_t llden = denom; 1.299 + int64_t llval = val; 1.300 + 1.301 + return (int)(llval * llnum / llden); 1.302 +} 1.303 + 1.304 +static void init_temporal_layer_context(VP8_COMP *cpi, 1.305 + VP8_CONFIG *oxcf, 1.306 + const int layer, 1.307 + double prev_layer_framerate) 1.308 +{ 1.309 + LAYER_CONTEXT *lc = &cpi->layer_context[layer]; 1.310 + 1.311 + lc->framerate = cpi->output_framerate / cpi->oxcf.rate_decimator[layer]; 1.312 + lc->target_bandwidth = cpi->oxcf.target_bitrate[layer] * 1000; 1.313 + 1.314 + lc->starting_buffer_level_in_ms = oxcf->starting_buffer_level; 1.315 + lc->optimal_buffer_level_in_ms = oxcf->optimal_buffer_level; 1.316 + lc->maximum_buffer_size_in_ms = oxcf->maximum_buffer_size; 1.317 + 1.318 + lc->starting_buffer_level = 1.319 + rescale((int)(oxcf->starting_buffer_level), 1.320 + lc->target_bandwidth, 1000); 1.321 + 1.322 + if (oxcf->optimal_buffer_level == 0) 1.323 + lc->optimal_buffer_level = lc->target_bandwidth / 8; 1.324 + else 1.325 + lc->optimal_buffer_level = 1.326 + rescale((int)(oxcf->optimal_buffer_level), 1.327 + lc->target_bandwidth, 1000); 1.328 + 1.329 + if (oxcf->maximum_buffer_size == 0) 1.330 + lc->maximum_buffer_size = lc->target_bandwidth / 8; 1.331 + else 1.332 + lc->maximum_buffer_size = 1.333 + rescale((int)(oxcf->maximum_buffer_size), 1.334 + lc->target_bandwidth, 1000); 1.335 + 1.336 + /* Work out the average size of a frame within this layer */ 1.337 + if (layer > 0) 1.338 + lc->avg_frame_size_for_layer = 1.339 + (int)((cpi->oxcf.target_bitrate[layer] - 1.340 + cpi->oxcf.target_bitrate[layer-1]) * 1000 / 1.341 + (lc->framerate - prev_layer_framerate)); 1.342 + 1.343 + lc->active_worst_quality = cpi->oxcf.worst_allowed_q; 1.344 + lc->active_best_quality = cpi->oxcf.best_allowed_q; 1.345 + lc->avg_frame_qindex = cpi->oxcf.worst_allowed_q; 1.346 + 1.347 + lc->buffer_level = lc->starting_buffer_level; 1.348 + lc->bits_off_target = lc->starting_buffer_level; 1.349 + 1.350 + lc->total_actual_bits = 0; 1.351 + lc->ni_av_qi = 0; 1.352 + lc->ni_tot_qi = 0; 1.353 + lc->ni_frames = 0; 1.354 + lc->rate_correction_factor = 1.0; 1.355 + lc->key_frame_rate_correction_factor = 1.0; 1.356 + lc->gf_rate_correction_factor = 1.0; 1.357 + lc->inter_frame_target = 0; 1.358 +} 1.359 + 1.360 +// Upon a run-time change in temporal layers, reset the layer context parameters 1.361 +// for any "new" layers. For "existing" layers, let them inherit the parameters 1.362 +// from the previous layer state (at the same layer #). In future we may want 1.363 +// to better map the previous layer state(s) to the "new" ones. 1.364 +static void reset_temporal_layer_change(VP8_COMP *cpi, 1.365 + VP8_CONFIG *oxcf, 1.366 + const int prev_num_layers) 1.367 +{ 1.368 + int i; 1.369 + double prev_layer_framerate = 0; 1.370 + const int curr_num_layers = cpi->oxcf.number_of_layers; 1.371 + // If the previous state was 1 layer, get current layer context from cpi. 1.372 + // We need this to set the layer context for the new layers below. 1.373 + if (prev_num_layers == 1) 1.374 + { 1.375 + cpi->current_layer = 0; 1.376 + save_layer_context(cpi); 1.377 + } 1.378 + for (i = 0; i < curr_num_layers; i++) 1.379 + { 1.380 + LAYER_CONTEXT *lc = &cpi->layer_context[i]; 1.381 + if (i >= prev_num_layers) 1.382 + { 1.383 + init_temporal_layer_context(cpi, oxcf, i, prev_layer_framerate); 1.384 + } 1.385 + // The initial buffer levels are set based on their starting levels. 1.386 + // We could set the buffer levels based on the previous state (normalized 1.387 + // properly by the layer bandwidths) but we would need to keep track of 1.388 + // the previous set of layer bandwidths (i.e., target_bitrate[i]) 1.389 + // before the layer change. For now, reset to the starting levels. 1.390 + lc->buffer_level = cpi->oxcf.starting_buffer_level_in_ms * 1.391 + cpi->oxcf.target_bitrate[i]; 1.392 + lc->bits_off_target = lc->buffer_level; 1.393 + // TDOD(marpan): Should we set the rate_correction_factor and 1.394 + // active_worst/best_quality to values derived from the previous layer 1.395 + // state (to smooth-out quality dips/rate fluctuation at transition)? 1.396 + 1.397 + // We need to treat the 1 layer case separately: oxcf.target_bitrate[i] 1.398 + // is not set for 1 layer, and the restore_layer_context/save_context() 1.399 + // are not called in the encoding loop, so we need to call it here to 1.400 + // pass the layer context state to |cpi|. 1.401 + if (curr_num_layers == 1) 1.402 + { 1.403 + lc->target_bandwidth = cpi->oxcf.target_bandwidth; 1.404 + lc->buffer_level = cpi->oxcf.starting_buffer_level_in_ms * 1.405 + lc->target_bandwidth / 1000; 1.406 + lc->bits_off_target = lc->buffer_level; 1.407 + restore_layer_context(cpi, 0); 1.408 + } 1.409 + prev_layer_framerate = cpi->output_framerate / 1.410 + cpi->oxcf.rate_decimator[i]; 1.411 + } 1.412 +} 1.413 + 1.414 +static void setup_features(VP8_COMP *cpi) 1.415 +{ 1.416 + // If segmentation enabled set the update flags 1.417 + if ( cpi->mb.e_mbd.segmentation_enabled ) 1.418 + { 1.419 + cpi->mb.e_mbd.update_mb_segmentation_map = 1; 1.420 + cpi->mb.e_mbd.update_mb_segmentation_data = 1; 1.421 + } 1.422 + else 1.423 + { 1.424 + cpi->mb.e_mbd.update_mb_segmentation_map = 0; 1.425 + cpi->mb.e_mbd.update_mb_segmentation_data = 0; 1.426 + } 1.427 + 1.428 + cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 0; 1.429 + cpi->mb.e_mbd.mode_ref_lf_delta_update = 0; 1.430 + vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas)); 1.431 + vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas)); 1.432 + vpx_memset(cpi->mb.e_mbd.last_ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas)); 1.433 + vpx_memset(cpi->mb.e_mbd.last_mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas)); 1.434 + 1.435 + set_default_lf_deltas(cpi); 1.436 + 1.437 +} 1.438 + 1.439 + 1.440 +static void dealloc_raw_frame_buffers(VP8_COMP *cpi); 1.441 + 1.442 + 1.443 +static void dealloc_compressor_data(VP8_COMP *cpi) 1.444 +{ 1.445 + vpx_free(cpi->tplist); 1.446 + cpi->tplist = NULL; 1.447 + 1.448 + /* Delete last frame MV storage buffers */ 1.449 + vpx_free(cpi->lfmv); 1.450 + cpi->lfmv = 0; 1.451 + 1.452 + vpx_free(cpi->lf_ref_frame_sign_bias); 1.453 + cpi->lf_ref_frame_sign_bias = 0; 1.454 + 1.455 + vpx_free(cpi->lf_ref_frame); 1.456 + cpi->lf_ref_frame = 0; 1.457 + 1.458 + /* Delete sementation map */ 1.459 + vpx_free(cpi->segmentation_map); 1.460 + cpi->segmentation_map = 0; 1.461 + 1.462 + vpx_free(cpi->active_map); 1.463 + cpi->active_map = 0; 1.464 + 1.465 + vp8_de_alloc_frame_buffers(&cpi->common); 1.466 + 1.467 + vp8_yv12_de_alloc_frame_buffer(&cpi->pick_lf_lvl_frame); 1.468 + vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source); 1.469 + dealloc_raw_frame_buffers(cpi); 1.470 + 1.471 + vpx_free(cpi->tok); 1.472 + cpi->tok = 0; 1.473 + 1.474 + /* Structure used to monitor GF usage */ 1.475 + vpx_free(cpi->gf_active_flags); 1.476 + cpi->gf_active_flags = 0; 1.477 + 1.478 + /* Activity mask based per mb zbin adjustments */ 1.479 + vpx_free(cpi->mb_activity_map); 1.480 + cpi->mb_activity_map = 0; 1.481 + 1.482 + vpx_free(cpi->mb.pip); 1.483 + cpi->mb.pip = 0; 1.484 + 1.485 +#if CONFIG_MULTITHREAD 1.486 + vpx_free(cpi->mt_current_mb_col); 1.487 + cpi->mt_current_mb_col = NULL; 1.488 +#endif 1.489 +} 1.490 + 1.491 +static void enable_segmentation(VP8_COMP *cpi) 1.492 +{ 1.493 + /* Set the appropriate feature bit */ 1.494 + cpi->mb.e_mbd.segmentation_enabled = 1; 1.495 + cpi->mb.e_mbd.update_mb_segmentation_map = 1; 1.496 + cpi->mb.e_mbd.update_mb_segmentation_data = 1; 1.497 +} 1.498 +static void disable_segmentation(VP8_COMP *cpi) 1.499 +{ 1.500 + /* Clear the appropriate feature bit */ 1.501 + cpi->mb.e_mbd.segmentation_enabled = 0; 1.502 +} 1.503 + 1.504 +/* Valid values for a segment are 0 to 3 1.505 + * Segmentation map is arrange as [Rows][Columns] 1.506 + */ 1.507 +static void set_segmentation_map(VP8_COMP *cpi, unsigned char *segmentation_map) 1.508 +{ 1.509 + /* Copy in the new segmentation map */ 1.510 + vpx_memcpy(cpi->segmentation_map, segmentation_map, (cpi->common.mb_rows * cpi->common.mb_cols)); 1.511 + 1.512 + /* Signal that the map should be updated. */ 1.513 + cpi->mb.e_mbd.update_mb_segmentation_map = 1; 1.514 + cpi->mb.e_mbd.update_mb_segmentation_data = 1; 1.515 +} 1.516 + 1.517 +/* The values given for each segment can be either deltas (from the default 1.518 + * value chosen for the frame) or absolute values. 1.519 + * 1.520 + * Valid range for abs values is: 1.521 + * (0-127 for MB_LVL_ALT_Q), (0-63 for SEGMENT_ALT_LF) 1.522 + * Valid range for delta values are: 1.523 + * (+/-127 for MB_LVL_ALT_Q), (+/-63 for SEGMENT_ALT_LF) 1.524 + * 1.525 + * abs_delta = SEGMENT_DELTADATA (deltas) 1.526 + * abs_delta = SEGMENT_ABSDATA (use the absolute values given). 1.527 + * 1.528 + */ 1.529 +static void set_segment_data(VP8_COMP *cpi, signed char *feature_data, unsigned char abs_delta) 1.530 +{ 1.531 + cpi->mb.e_mbd.mb_segement_abs_delta = abs_delta; 1.532 + vpx_memcpy(cpi->segment_feature_data, feature_data, sizeof(cpi->segment_feature_data)); 1.533 +} 1.534 + 1.535 + 1.536 +static void segmentation_test_function(VP8_COMP *cpi) 1.537 +{ 1.538 + unsigned char *seg_map; 1.539 + signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS]; 1.540 + 1.541 + // Create a temporary map for segmentation data. 1.542 + CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1)); 1.543 + 1.544 + // Set the segmentation Map 1.545 + set_segmentation_map(cpi, seg_map); 1.546 + 1.547 + // Activate segmentation. 1.548 + enable_segmentation(cpi); 1.549 + 1.550 + // Set up the quant segment data 1.551 + feature_data[MB_LVL_ALT_Q][0] = 0; 1.552 + feature_data[MB_LVL_ALT_Q][1] = 4; 1.553 + feature_data[MB_LVL_ALT_Q][2] = 0; 1.554 + feature_data[MB_LVL_ALT_Q][3] = 0; 1.555 + // Set up the loop segment data 1.556 + feature_data[MB_LVL_ALT_LF][0] = 0; 1.557 + feature_data[MB_LVL_ALT_LF][1] = 0; 1.558 + feature_data[MB_LVL_ALT_LF][2] = 0; 1.559 + feature_data[MB_LVL_ALT_LF][3] = 0; 1.560 + 1.561 + // Initialise the feature data structure 1.562 + // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1 1.563 + set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA); 1.564 + 1.565 + // Delete sementation map 1.566 + vpx_free(seg_map); 1.567 + 1.568 + seg_map = 0; 1.569 +} 1.570 + 1.571 +/* A simple function to cyclically refresh the background at a lower Q */ 1.572 +static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment) 1.573 +{ 1.574 + unsigned char *seg_map = cpi->segmentation_map; 1.575 + signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS]; 1.576 + int i; 1.577 + int block_count = cpi->cyclic_refresh_mode_max_mbs_perframe; 1.578 + int mbs_in_frame = cpi->common.mb_rows * cpi->common.mb_cols; 1.579 + 1.580 + cpi->cyclic_refresh_q = Q / 2; 1.581 + 1.582 + // Set every macroblock to be eligible for update. 1.583 + // For key frame this will reset seg map to 0. 1.584 + vpx_memset(cpi->segmentation_map, 0, mbs_in_frame); 1.585 + 1.586 + if (cpi->common.frame_type != KEY_FRAME) 1.587 + { 1.588 + /* Cycle through the macro_block rows */ 1.589 + /* MB loop to set local segmentation map */ 1.590 + i = cpi->cyclic_refresh_mode_index; 1.591 + assert(i < mbs_in_frame); 1.592 + do 1.593 + { 1.594 + /* If the MB is as a candidate for clean up then mark it for 1.595 + * possible boost/refresh (segment 1) The segment id may get 1.596 + * reset to 0 later if the MB gets coded anything other than 1.597 + * last frame 0,0 as only (last frame 0,0) MBs are eligable for 1.598 + * refresh : that is to say Mbs likely to be background blocks. 1.599 + */ 1.600 + if (cpi->cyclic_refresh_map[i] == 0) 1.601 + { 1.602 + seg_map[i] = 1; 1.603 + block_count --; 1.604 + } 1.605 + else if (cpi->cyclic_refresh_map[i] < 0) 1.606 + cpi->cyclic_refresh_map[i]++; 1.607 + 1.608 + i++; 1.609 + if (i == mbs_in_frame) 1.610 + i = 0; 1.611 + 1.612 + } 1.613 + while(block_count && i != cpi->cyclic_refresh_mode_index); 1.614 + 1.615 + cpi->cyclic_refresh_mode_index = i; 1.616 + } 1.617 + 1.618 + /* Activate segmentation. */ 1.619 + cpi->mb.e_mbd.update_mb_segmentation_map = 1; 1.620 + cpi->mb.e_mbd.update_mb_segmentation_data = 1; 1.621 + enable_segmentation(cpi); 1.622 + 1.623 + /* Set up the quant segment data */ 1.624 + feature_data[MB_LVL_ALT_Q][0] = 0; 1.625 + feature_data[MB_LVL_ALT_Q][1] = (cpi->cyclic_refresh_q - Q); 1.626 + feature_data[MB_LVL_ALT_Q][2] = 0; 1.627 + feature_data[MB_LVL_ALT_Q][3] = 0; 1.628 + 1.629 + /* Set up the loop segment data */ 1.630 + feature_data[MB_LVL_ALT_LF][0] = 0; 1.631 + feature_data[MB_LVL_ALT_LF][1] = lf_adjustment; 1.632 + feature_data[MB_LVL_ALT_LF][2] = 0; 1.633 + feature_data[MB_LVL_ALT_LF][3] = 0; 1.634 + 1.635 + /* Initialise the feature data structure */ 1.636 + set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA); 1.637 + 1.638 +} 1.639 + 1.640 +static void set_default_lf_deltas(VP8_COMP *cpi) 1.641 +{ 1.642 + cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1; 1.643 + cpi->mb.e_mbd.mode_ref_lf_delta_update = 1; 1.644 + 1.645 + vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas)); 1.646 + vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas)); 1.647 + 1.648 + /* Test of ref frame deltas */ 1.649 + cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2; 1.650 + cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0; 1.651 + cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2; 1.652 + cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2; 1.653 + 1.654 + cpi->mb.e_mbd.mode_lf_deltas[0] = 4; /* BPRED */ 1.655 + 1.656 + if(cpi->oxcf.Mode == MODE_REALTIME) 1.657 + cpi->mb.e_mbd.mode_lf_deltas[1] = -12; /* Zero */ 1.658 + else 1.659 + cpi->mb.e_mbd.mode_lf_deltas[1] = -2; /* Zero */ 1.660 + 1.661 + cpi->mb.e_mbd.mode_lf_deltas[2] = 2; /* New mv */ 1.662 + cpi->mb.e_mbd.mode_lf_deltas[3] = 4; /* Split mv */ 1.663 +} 1.664 + 1.665 +/* Convenience macros for mapping speed and mode into a continuous 1.666 + * range 1.667 + */ 1.668 +#define GOOD(x) (x+1) 1.669 +#define RT(x) (x+7) 1.670 + 1.671 +static int speed_map(int speed, const int *map) 1.672 +{ 1.673 + int res; 1.674 + 1.675 + do 1.676 + { 1.677 + res = *map++; 1.678 + } while(speed >= *map++); 1.679 + return res; 1.680 +} 1.681 + 1.682 +static const int thresh_mult_map_znn[] = { 1.683 + /* map common to zero, nearest, and near */ 1.684 + 0, GOOD(2), 1500, GOOD(3), 2000, RT(0), 1000, RT(2), 2000, INT_MAX 1.685 +}; 1.686 + 1.687 +static const int thresh_mult_map_vhpred[] = { 1.688 + 1000, GOOD(2), 1500, GOOD(3), 2000, RT(0), 1000, RT(1), 2000, 1.689 + RT(7), INT_MAX, INT_MAX 1.690 +}; 1.691 + 1.692 +static const int thresh_mult_map_bpred[] = { 1.693 + 2000, GOOD(0), 2500, GOOD(2), 5000, GOOD(3), 7500, RT(0), 2500, RT(1), 5000, 1.694 + RT(6), INT_MAX, INT_MAX 1.695 +}; 1.696 + 1.697 +static const int thresh_mult_map_tm[] = { 1.698 + 1000, GOOD(2), 1500, GOOD(3), 2000, RT(0), 0, RT(1), 1000, RT(2), 2000, 1.699 + RT(7), INT_MAX, INT_MAX 1.700 +}; 1.701 + 1.702 +static const int thresh_mult_map_new1[] = { 1.703 + 1000, GOOD(2), 2000, RT(0), 2000, INT_MAX 1.704 +}; 1.705 + 1.706 +static const int thresh_mult_map_new2[] = { 1.707 + 1000, GOOD(2), 2000, GOOD(3), 2500, GOOD(5), 4000, RT(0), 2000, RT(2), 2500, 1.708 + RT(5), 4000, INT_MAX 1.709 +}; 1.710 + 1.711 +static const int thresh_mult_map_split1[] = { 1.712 + 2500, GOOD(0), 1700, GOOD(2), 10000, GOOD(3), 25000, GOOD(4), INT_MAX, 1.713 + RT(0), 5000, RT(1), 10000, RT(2), 25000, RT(3), INT_MAX, INT_MAX 1.714 +}; 1.715 + 1.716 +static const int thresh_mult_map_split2[] = { 1.717 + 5000, GOOD(0), 4500, GOOD(2), 20000, GOOD(3), 50000, GOOD(4), INT_MAX, 1.718 + RT(0), 10000, RT(1), 20000, RT(2), 50000, RT(3), INT_MAX, INT_MAX 1.719 +}; 1.720 + 1.721 +static const int mode_check_freq_map_zn2[] = { 1.722 + /* {zero,nearest}{2,3} */ 1.723 + 0, RT(10), 1<<1, RT(11), 1<<2, RT(12), 1<<3, INT_MAX 1.724 +}; 1.725 + 1.726 +static const int mode_check_freq_map_vhbpred[] = { 1.727 + 0, GOOD(5), 2, RT(0), 0, RT(3), 2, RT(5), 4, INT_MAX 1.728 +}; 1.729 + 1.730 +static const int mode_check_freq_map_near2[] = { 1.731 + 0, GOOD(5), 2, RT(0), 0, RT(3), 2, RT(10), 1<<2, RT(11), 1<<3, RT(12), 1<<4, 1.732 + INT_MAX 1.733 +}; 1.734 + 1.735 +static const int mode_check_freq_map_new1[] = { 1.736 + 0, RT(10), 1<<1, RT(11), 1<<2, RT(12), 1<<3, INT_MAX 1.737 +}; 1.738 + 1.739 +static const int mode_check_freq_map_new2[] = { 1.740 + 0, GOOD(5), 4, RT(0), 0, RT(3), 4, RT(10), 1<<3, RT(11), 1<<4, RT(12), 1<<5, 1.741 + INT_MAX 1.742 +}; 1.743 + 1.744 +static const int mode_check_freq_map_split1[] = { 1.745 + 0, GOOD(2), 2, GOOD(3), 7, RT(1), 2, RT(2), 7, INT_MAX 1.746 +}; 1.747 + 1.748 +static const int mode_check_freq_map_split2[] = { 1.749 + 0, GOOD(1), 2, GOOD(2), 4, GOOD(3), 15, RT(1), 4, RT(2), 15, INT_MAX 1.750 +}; 1.751 + 1.752 +void vp8_set_speed_features(VP8_COMP *cpi) 1.753 +{ 1.754 + SPEED_FEATURES *sf = &cpi->sf; 1.755 + int Mode = cpi->compressor_speed; 1.756 + int Speed = cpi->Speed; 1.757 + int i; 1.758 + VP8_COMMON *cm = &cpi->common; 1.759 + int last_improved_quant = sf->improved_quant; 1.760 + int ref_frames; 1.761 + 1.762 + /* Initialise default mode frequency sampling variables */ 1.763 + for (i = 0; i < MAX_MODES; i ++) 1.764 + { 1.765 + cpi->mode_check_freq[i] = 0; 1.766 + } 1.767 + 1.768 + cpi->mb.mbs_tested_so_far = 0; 1.769 + 1.770 + /* best quality defaults */ 1.771 + sf->RD = 1; 1.772 + sf->search_method = NSTEP; 1.773 + sf->improved_quant = 1; 1.774 + sf->improved_dct = 1; 1.775 + sf->auto_filter = 1; 1.776 + sf->recode_loop = 1; 1.777 + sf->quarter_pixel_search = 1; 1.778 + sf->half_pixel_search = 1; 1.779 + sf->iterative_sub_pixel = 1; 1.780 + sf->optimize_coefficients = 1; 1.781 + sf->use_fastquant_for_pick = 0; 1.782 + sf->no_skip_block4x4_search = 1; 1.783 + 1.784 + sf->first_step = 0; 1.785 + sf->max_step_search_steps = MAX_MVSEARCH_STEPS; 1.786 + sf->improved_mv_pred = 1; 1.787 + 1.788 + /* default thresholds to 0 */ 1.789 + for (i = 0; i < MAX_MODES; i++) 1.790 + sf->thresh_mult[i] = 0; 1.791 + 1.792 + /* Count enabled references */ 1.793 + ref_frames = 1; 1.794 + if (cpi->ref_frame_flags & VP8_LAST_FRAME) 1.795 + ref_frames++; 1.796 + if (cpi->ref_frame_flags & VP8_GOLD_FRAME) 1.797 + ref_frames++; 1.798 + if (cpi->ref_frame_flags & VP8_ALTR_FRAME) 1.799 + ref_frames++; 1.800 + 1.801 + /* Convert speed to continuous range, with clamping */ 1.802 + if (Mode == 0) 1.803 + Speed = 0; 1.804 + else if (Mode == 2) 1.805 + Speed = RT(Speed); 1.806 + else 1.807 + { 1.808 + if (Speed > 5) 1.809 + Speed = 5; 1.810 + Speed = GOOD(Speed); 1.811 + } 1.812 + 1.813 + sf->thresh_mult[THR_ZERO1] = 1.814 + sf->thresh_mult[THR_NEAREST1] = 1.815 + sf->thresh_mult[THR_NEAR1] = 1.816 + sf->thresh_mult[THR_DC] = 0; /* always */ 1.817 + 1.818 + sf->thresh_mult[THR_ZERO2] = 1.819 + sf->thresh_mult[THR_ZERO3] = 1.820 + sf->thresh_mult[THR_NEAREST2] = 1.821 + sf->thresh_mult[THR_NEAREST3] = 1.822 + sf->thresh_mult[THR_NEAR2] = 1.823 + sf->thresh_mult[THR_NEAR3] = speed_map(Speed, thresh_mult_map_znn); 1.824 + 1.825 + sf->thresh_mult[THR_V_PRED] = 1.826 + sf->thresh_mult[THR_H_PRED] = speed_map(Speed, thresh_mult_map_vhpred); 1.827 + sf->thresh_mult[THR_B_PRED] = speed_map(Speed, thresh_mult_map_bpred); 1.828 + sf->thresh_mult[THR_TM] = speed_map(Speed, thresh_mult_map_tm); 1.829 + sf->thresh_mult[THR_NEW1] = speed_map(Speed, thresh_mult_map_new1); 1.830 + sf->thresh_mult[THR_NEW2] = 1.831 + sf->thresh_mult[THR_NEW3] = speed_map(Speed, thresh_mult_map_new2); 1.832 + sf->thresh_mult[THR_SPLIT1] = speed_map(Speed, thresh_mult_map_split1); 1.833 + sf->thresh_mult[THR_SPLIT2] = 1.834 + sf->thresh_mult[THR_SPLIT3] = speed_map(Speed, thresh_mult_map_split2); 1.835 + 1.836 + cpi->mode_check_freq[THR_ZERO1] = 1.837 + cpi->mode_check_freq[THR_NEAREST1] = 1.838 + cpi->mode_check_freq[THR_NEAR1] = 1.839 + cpi->mode_check_freq[THR_TM] = 1.840 + cpi->mode_check_freq[THR_DC] = 0; /* always */ 1.841 + 1.842 + cpi->mode_check_freq[THR_ZERO2] = 1.843 + cpi->mode_check_freq[THR_ZERO3] = 1.844 + cpi->mode_check_freq[THR_NEAREST2] = 1.845 + cpi->mode_check_freq[THR_NEAREST3] = speed_map(Speed, 1.846 + mode_check_freq_map_zn2); 1.847 + 1.848 + cpi->mode_check_freq[THR_NEAR2] = 1.849 + cpi->mode_check_freq[THR_NEAR3] = speed_map(Speed, 1.850 + mode_check_freq_map_near2); 1.851 + 1.852 + cpi->mode_check_freq[THR_V_PRED] = 1.853 + cpi->mode_check_freq[THR_H_PRED] = 1.854 + cpi->mode_check_freq[THR_B_PRED] = speed_map(Speed, 1.855 + mode_check_freq_map_vhbpred); 1.856 + cpi->mode_check_freq[THR_NEW1] = speed_map(Speed, 1.857 + mode_check_freq_map_new1); 1.858 + cpi->mode_check_freq[THR_NEW2] = 1.859 + cpi->mode_check_freq[THR_NEW3] = speed_map(Speed, 1.860 + mode_check_freq_map_new2); 1.861 + cpi->mode_check_freq[THR_SPLIT1] = speed_map(Speed, 1.862 + mode_check_freq_map_split1); 1.863 + cpi->mode_check_freq[THR_SPLIT2] = 1.864 + cpi->mode_check_freq[THR_SPLIT3] = speed_map(Speed, 1.865 + mode_check_freq_map_split2); 1.866 + Speed = cpi->Speed; 1.867 + switch (Mode) 1.868 + { 1.869 +#if !(CONFIG_REALTIME_ONLY) 1.870 + case 0: /* best quality mode */ 1.871 + sf->first_step = 0; 1.872 + sf->max_step_search_steps = MAX_MVSEARCH_STEPS; 1.873 + break; 1.874 + case 1: 1.875 + case 3: 1.876 + if (Speed > 0) 1.877 + { 1.878 + /* Disable coefficient optimization above speed 0 */ 1.879 + sf->optimize_coefficients = 0; 1.880 + sf->use_fastquant_for_pick = 1; 1.881 + sf->no_skip_block4x4_search = 0; 1.882 + 1.883 + sf->first_step = 1; 1.884 + } 1.885 + 1.886 + if (Speed > 2) 1.887 + { 1.888 + sf->improved_quant = 0; 1.889 + sf->improved_dct = 0; 1.890 + 1.891 + /* Only do recode loop on key frames, golden frames and 1.892 + * alt ref frames 1.893 + */ 1.894 + sf->recode_loop = 2; 1.895 + 1.896 + } 1.897 + 1.898 + if (Speed > 3) 1.899 + { 1.900 + sf->auto_filter = 1; 1.901 + sf->recode_loop = 0; /* recode loop off */ 1.902 + sf->RD = 0; /* Turn rd off */ 1.903 + 1.904 + } 1.905 + 1.906 + if (Speed > 4) 1.907 + { 1.908 + sf->auto_filter = 0; /* Faster selection of loop filter */ 1.909 + } 1.910 + 1.911 + break; 1.912 +#endif 1.913 + case 2: 1.914 + sf->optimize_coefficients = 0; 1.915 + sf->recode_loop = 0; 1.916 + sf->auto_filter = 1; 1.917 + sf->iterative_sub_pixel = 1; 1.918 + sf->search_method = NSTEP; 1.919 + 1.920 + if (Speed > 0) 1.921 + { 1.922 + sf->improved_quant = 0; 1.923 + sf->improved_dct = 0; 1.924 + 1.925 + sf->use_fastquant_for_pick = 1; 1.926 + sf->no_skip_block4x4_search = 0; 1.927 + sf->first_step = 1; 1.928 + } 1.929 + 1.930 + if (Speed > 2) 1.931 + sf->auto_filter = 0; /* Faster selection of loop filter */ 1.932 + 1.933 + if (Speed > 3) 1.934 + { 1.935 + sf->RD = 0; 1.936 + sf->auto_filter = 1; 1.937 + } 1.938 + 1.939 + if (Speed > 4) 1.940 + { 1.941 + sf->auto_filter = 0; /* Faster selection of loop filter */ 1.942 + sf->search_method = HEX; 1.943 + sf->iterative_sub_pixel = 0; 1.944 + } 1.945 + 1.946 + if (Speed > 6) 1.947 + { 1.948 + unsigned int sum = 0; 1.949 + unsigned int total_mbs = cm->MBs; 1.950 + int thresh; 1.951 + unsigned int total_skip; 1.952 + 1.953 + int min = 2000; 1.954 + 1.955 + if (cpi->oxcf.encode_breakout > 2000) 1.956 + min = cpi->oxcf.encode_breakout; 1.957 + 1.958 + min >>= 7; 1.959 + 1.960 + for (i = 0; i < min; i++) 1.961 + { 1.962 + sum += cpi->mb.error_bins[i]; 1.963 + } 1.964 + 1.965 + total_skip = sum; 1.966 + sum = 0; 1.967 + 1.968 + /* i starts from 2 to make sure thresh started from 2048 */ 1.969 + for (; i < 1024; i++) 1.970 + { 1.971 + sum += cpi->mb.error_bins[i]; 1.972 + 1.973 + if (10 * sum >= (unsigned int)(cpi->Speed - 6)*(total_mbs - total_skip)) 1.974 + break; 1.975 + } 1.976 + 1.977 + i--; 1.978 + thresh = (i << 7); 1.979 + 1.980 + if (thresh < 2000) 1.981 + thresh = 2000; 1.982 + 1.983 + if (ref_frames > 1) 1.984 + { 1.985 + sf->thresh_mult[THR_NEW1 ] = thresh; 1.986 + sf->thresh_mult[THR_NEAREST1 ] = thresh >> 1; 1.987 + sf->thresh_mult[THR_NEAR1 ] = thresh >> 1; 1.988 + } 1.989 + 1.990 + if (ref_frames > 2) 1.991 + { 1.992 + sf->thresh_mult[THR_NEW2] = thresh << 1; 1.993 + sf->thresh_mult[THR_NEAREST2 ] = thresh; 1.994 + sf->thresh_mult[THR_NEAR2 ] = thresh; 1.995 + } 1.996 + 1.997 + if (ref_frames > 3) 1.998 + { 1.999 + sf->thresh_mult[THR_NEW3] = thresh << 1; 1.1000 + sf->thresh_mult[THR_NEAREST3 ] = thresh; 1.1001 + sf->thresh_mult[THR_NEAR3 ] = thresh; 1.1002 + } 1.1003 + 1.1004 + sf->improved_mv_pred = 0; 1.1005 + } 1.1006 + 1.1007 + if (Speed > 8) 1.1008 + sf->quarter_pixel_search = 0; 1.1009 + 1.1010 + if(cm->version == 0) 1.1011 + { 1.1012 + cm->filter_type = NORMAL_LOOPFILTER; 1.1013 + 1.1014 + if (Speed >= 14) 1.1015 + cm->filter_type = SIMPLE_LOOPFILTER; 1.1016 + } 1.1017 + else 1.1018 + { 1.1019 + cm->filter_type = SIMPLE_LOOPFILTER; 1.1020 + } 1.1021 + 1.1022 + /* This has a big hit on quality. Last resort */ 1.1023 + if (Speed >= 15) 1.1024 + sf->half_pixel_search = 0; 1.1025 + 1.1026 + vpx_memset(cpi->mb.error_bins, 0, sizeof(cpi->mb.error_bins)); 1.1027 + 1.1028 + }; /* switch */ 1.1029 + 1.1030 + /* Slow quant, dct and trellis not worthwhile for first pass 1.1031 + * so make sure they are always turned off. 1.1032 + */ 1.1033 + if ( cpi->pass == 1 ) 1.1034 + { 1.1035 + sf->improved_quant = 0; 1.1036 + sf->optimize_coefficients = 0; 1.1037 + sf->improved_dct = 0; 1.1038 + } 1.1039 + 1.1040 + if (cpi->sf.search_method == NSTEP) 1.1041 + { 1.1042 + vp8_init3smotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride); 1.1043 + } 1.1044 + else if (cpi->sf.search_method == DIAMOND) 1.1045 + { 1.1046 + vp8_init_dsmotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride); 1.1047 + } 1.1048 + 1.1049 + if (cpi->sf.improved_dct) 1.1050 + { 1.1051 + cpi->mb.short_fdct8x4 = vp8_short_fdct8x4; 1.1052 + cpi->mb.short_fdct4x4 = vp8_short_fdct4x4; 1.1053 + } 1.1054 + else 1.1055 + { 1.1056 + /* No fast FDCT defined for any platform at this time. */ 1.1057 + cpi->mb.short_fdct8x4 = vp8_short_fdct8x4; 1.1058 + cpi->mb.short_fdct4x4 = vp8_short_fdct4x4; 1.1059 + } 1.1060 + 1.1061 + cpi->mb.short_walsh4x4 = vp8_short_walsh4x4; 1.1062 + 1.1063 + if (cpi->sf.improved_quant) 1.1064 + { 1.1065 + cpi->mb.quantize_b = vp8_regular_quantize_b; 1.1066 + cpi->mb.quantize_b_pair = vp8_regular_quantize_b_pair; 1.1067 + } 1.1068 + else 1.1069 + { 1.1070 + cpi->mb.quantize_b = vp8_fast_quantize_b; 1.1071 + cpi->mb.quantize_b_pair = vp8_fast_quantize_b_pair; 1.1072 + } 1.1073 + if (cpi->sf.improved_quant != last_improved_quant) 1.1074 + vp8cx_init_quantizer(cpi); 1.1075 + 1.1076 + if (cpi->sf.iterative_sub_pixel == 1) 1.1077 + { 1.1078 + cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step_iteratively; 1.1079 + } 1.1080 + else if (cpi->sf.quarter_pixel_search) 1.1081 + { 1.1082 + cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step; 1.1083 + } 1.1084 + else if (cpi->sf.half_pixel_search) 1.1085 + { 1.1086 + cpi->find_fractional_mv_step = vp8_find_best_half_pixel_step; 1.1087 + } 1.1088 + else 1.1089 + { 1.1090 + cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step; 1.1091 + } 1.1092 + 1.1093 + if (cpi->sf.optimize_coefficients == 1 && cpi->pass!=1) 1.1094 + cpi->mb.optimize = 1; 1.1095 + else 1.1096 + cpi->mb.optimize = 0; 1.1097 + 1.1098 + if (cpi->common.full_pixel) 1.1099 + cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step; 1.1100 + 1.1101 +#ifdef SPEEDSTATS 1.1102 + frames_at_speed[cpi->Speed]++; 1.1103 +#endif 1.1104 +} 1.1105 +#undef GOOD 1.1106 +#undef RT 1.1107 + 1.1108 +static void alloc_raw_frame_buffers(VP8_COMP *cpi) 1.1109 +{ 1.1110 +#if VP8_TEMPORAL_ALT_REF 1.1111 + int width = (cpi->oxcf.Width + 15) & ~15; 1.1112 + int height = (cpi->oxcf.Height + 15) & ~15; 1.1113 +#endif 1.1114 + 1.1115 + cpi->lookahead = vp8_lookahead_init(cpi->oxcf.Width, cpi->oxcf.Height, 1.1116 + cpi->oxcf.lag_in_frames); 1.1117 + if(!cpi->lookahead) 1.1118 + vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 1.1119 + "Failed to allocate lag buffers"); 1.1120 + 1.1121 +#if VP8_TEMPORAL_ALT_REF 1.1122 + 1.1123 + if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer, 1.1124 + width, height, VP8BORDERINPIXELS)) 1.1125 + vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 1.1126 + "Failed to allocate altref buffer"); 1.1127 + 1.1128 +#endif 1.1129 +} 1.1130 + 1.1131 + 1.1132 +static void dealloc_raw_frame_buffers(VP8_COMP *cpi) 1.1133 +{ 1.1134 +#if VP8_TEMPORAL_ALT_REF 1.1135 + vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer); 1.1136 +#endif 1.1137 + vp8_lookahead_destroy(cpi->lookahead); 1.1138 +} 1.1139 + 1.1140 + 1.1141 +static int vp8_alloc_partition_data(VP8_COMP *cpi) 1.1142 +{ 1.1143 + vpx_free(cpi->mb.pip); 1.1144 + 1.1145 + cpi->mb.pip = vpx_calloc((cpi->common.mb_cols + 1) * 1.1146 + (cpi->common.mb_rows + 1), 1.1147 + sizeof(PARTITION_INFO)); 1.1148 + if(!cpi->mb.pip) 1.1149 + return 1; 1.1150 + 1.1151 + cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1; 1.1152 + 1.1153 + return 0; 1.1154 +} 1.1155 + 1.1156 +void vp8_alloc_compressor_data(VP8_COMP *cpi) 1.1157 +{ 1.1158 + VP8_COMMON *cm = & cpi->common; 1.1159 + 1.1160 + int width = cm->Width; 1.1161 + int height = cm->Height; 1.1162 + 1.1163 + if (vp8_alloc_frame_buffers(cm, width, height)) 1.1164 + vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 1.1165 + "Failed to allocate frame buffers"); 1.1166 + 1.1167 + if (vp8_alloc_partition_data(cpi)) 1.1168 + vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 1.1169 + "Failed to allocate partition data"); 1.1170 + 1.1171 + 1.1172 + if ((width & 0xf) != 0) 1.1173 + width += 16 - (width & 0xf); 1.1174 + 1.1175 + if ((height & 0xf) != 0) 1.1176 + height += 16 - (height & 0xf); 1.1177 + 1.1178 + 1.1179 + if (vp8_yv12_alloc_frame_buffer(&cpi->pick_lf_lvl_frame, 1.1180 + width, height, VP8BORDERINPIXELS)) 1.1181 + vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 1.1182 + "Failed to allocate last frame buffer"); 1.1183 + 1.1184 + if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source, 1.1185 + width, height, VP8BORDERINPIXELS)) 1.1186 + vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, 1.1187 + "Failed to allocate scaled source buffer"); 1.1188 + 1.1189 + vpx_free(cpi->tok); 1.1190 + 1.1191 + { 1.1192 +#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING 1.1193 + unsigned int tokens = 8 * 24 * 16; /* one MB for each thread */ 1.1194 +#else 1.1195 + unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16; 1.1196 +#endif 1.1197 + CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok))); 1.1198 + } 1.1199 + 1.1200 + /* Data used for real time vc mode to see if gf needs refreshing */ 1.1201 + cpi->zeromv_count = 0; 1.1202 + 1.1203 + 1.1204 + /* Structures used to monitor GF usage */ 1.1205 + vpx_free(cpi->gf_active_flags); 1.1206 + CHECK_MEM_ERROR(cpi->gf_active_flags, 1.1207 + vpx_calloc(sizeof(*cpi->gf_active_flags), 1.1208 + cm->mb_rows * cm->mb_cols)); 1.1209 + cpi->gf_active_count = cm->mb_rows * cm->mb_cols; 1.1210 + 1.1211 + vpx_free(cpi->mb_activity_map); 1.1212 + CHECK_MEM_ERROR(cpi->mb_activity_map, 1.1213 + vpx_calloc(sizeof(*cpi->mb_activity_map), 1.1214 + cm->mb_rows * cm->mb_cols)); 1.1215 + 1.1216 + /* allocate memory for storing last frame's MVs for MV prediction. */ 1.1217 + vpx_free(cpi->lfmv); 1.1218 + CHECK_MEM_ERROR(cpi->lfmv, vpx_calloc((cm->mb_rows+2) * (cm->mb_cols+2), 1.1219 + sizeof(*cpi->lfmv))); 1.1220 + vpx_free(cpi->lf_ref_frame_sign_bias); 1.1221 + CHECK_MEM_ERROR(cpi->lf_ref_frame_sign_bias, 1.1222 + vpx_calloc((cm->mb_rows+2) * (cm->mb_cols+2), 1.1223 + sizeof(*cpi->lf_ref_frame_sign_bias))); 1.1224 + vpx_free(cpi->lf_ref_frame); 1.1225 + CHECK_MEM_ERROR(cpi->lf_ref_frame, 1.1226 + vpx_calloc((cm->mb_rows+2) * (cm->mb_cols+2), 1.1227 + sizeof(*cpi->lf_ref_frame))); 1.1228 + 1.1229 + /* Create the encoder segmentation map and set all entries to 0 */ 1.1230 + vpx_free(cpi->segmentation_map); 1.1231 + CHECK_MEM_ERROR(cpi->segmentation_map, 1.1232 + vpx_calloc(cm->mb_rows * cm->mb_cols, 1.1233 + sizeof(*cpi->segmentation_map))); 1.1234 + cpi->cyclic_refresh_mode_index = 0; 1.1235 + vpx_free(cpi->active_map); 1.1236 + CHECK_MEM_ERROR(cpi->active_map, 1.1237 + vpx_calloc(cm->mb_rows * cm->mb_cols, 1.1238 + sizeof(*cpi->active_map))); 1.1239 + vpx_memset(cpi->active_map , 1, (cm->mb_rows * cm->mb_cols)); 1.1240 + 1.1241 +#if CONFIG_MULTITHREAD 1.1242 + if (width < 640) 1.1243 + cpi->mt_sync_range = 1; 1.1244 + else if (width <= 1280) 1.1245 + cpi->mt_sync_range = 4; 1.1246 + else if (width <= 2560) 1.1247 + cpi->mt_sync_range = 8; 1.1248 + else 1.1249 + cpi->mt_sync_range = 16; 1.1250 + 1.1251 + if (cpi->oxcf.multi_threaded > 1) 1.1252 + { 1.1253 + vpx_free(cpi->mt_current_mb_col); 1.1254 + CHECK_MEM_ERROR(cpi->mt_current_mb_col, 1.1255 + vpx_malloc(sizeof(*cpi->mt_current_mb_col) * cm->mb_rows)); 1.1256 + } 1.1257 + 1.1258 +#endif 1.1259 + 1.1260 + vpx_free(cpi->tplist); 1.1261 + CHECK_MEM_ERROR(cpi->tplist, vpx_malloc(sizeof(TOKENLIST) * cm->mb_rows)); 1.1262 +} 1.1263 + 1.1264 + 1.1265 +/* Quant MOD */ 1.1266 +static const int q_trans[] = 1.1267 +{ 1.1268 + 0, 1, 2, 3, 4, 5, 7, 8, 1.1269 + 9, 10, 12, 13, 15, 17, 18, 19, 1.1270 + 20, 21, 23, 24, 25, 26, 27, 28, 1.1271 + 29, 30, 31, 33, 35, 37, 39, 41, 1.1272 + 43, 45, 47, 49, 51, 53, 55, 57, 1.1273 + 59, 61, 64, 67, 70, 73, 76, 79, 1.1274 + 82, 85, 88, 91, 94, 97, 100, 103, 1.1275 + 106, 109, 112, 115, 118, 121, 124, 127, 1.1276 +}; 1.1277 + 1.1278 +int vp8_reverse_trans(int x) 1.1279 +{ 1.1280 + int i; 1.1281 + 1.1282 + for (i = 0; i < 64; i++) 1.1283 + if (q_trans[i] >= x) 1.1284 + return i; 1.1285 + 1.1286 + return 63; 1.1287 +} 1.1288 +void vp8_new_framerate(VP8_COMP *cpi, double framerate) 1.1289 +{ 1.1290 + if(framerate < .1) 1.1291 + framerate = 30; 1.1292 + 1.1293 + cpi->framerate = framerate; 1.1294 + cpi->output_framerate = framerate; 1.1295 + cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / 1.1296 + cpi->output_framerate); 1.1297 + cpi->av_per_frame_bandwidth = cpi->per_frame_bandwidth; 1.1298 + cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth * 1.1299 + cpi->oxcf.two_pass_vbrmin_section / 100); 1.1300 + 1.1301 + /* Set Maximum gf/arf interval */ 1.1302 + cpi->max_gf_interval = ((int)(cpi->output_framerate / 2.0) + 2); 1.1303 + 1.1304 + if(cpi->max_gf_interval < 12) 1.1305 + cpi->max_gf_interval = 12; 1.1306 + 1.1307 + /* Extended interval for genuinely static scenes */ 1.1308 + cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1; 1.1309 + 1.1310 + /* Special conditions when altr ref frame enabled in lagged compress mode */ 1.1311 + if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames) 1.1312 + { 1.1313 + if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1) 1.1314 + cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1; 1.1315 + 1.1316 + if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1) 1.1317 + cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1; 1.1318 + } 1.1319 + 1.1320 + if ( cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval ) 1.1321 + cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval; 1.1322 +} 1.1323 + 1.1324 + 1.1325 +static void init_config(VP8_COMP *cpi, VP8_CONFIG *oxcf) 1.1326 +{ 1.1327 + VP8_COMMON *cm = &cpi->common; 1.1328 + 1.1329 + cpi->oxcf = *oxcf; 1.1330 + 1.1331 + cpi->auto_gold = 1; 1.1332 + cpi->auto_adjust_gold_quantizer = 1; 1.1333 + 1.1334 + cm->version = oxcf->Version; 1.1335 + vp8_setup_version(cm); 1.1336 + 1.1337 + /* frame rate is not available on the first frame, as it's derived from 1.1338 + * the observed timestamps. The actual value used here doesn't matter 1.1339 + * too much, as it will adapt quickly. If the reciprocal of the timebase 1.1340 + * seems like a reasonable framerate, then use that as a guess, otherwise 1.1341 + * use 30. 1.1342 + */ 1.1343 + cpi->framerate = (double)(oxcf->timebase.den) / 1.1344 + (double)(oxcf->timebase.num); 1.1345 + 1.1346 + if (cpi->framerate > 180) 1.1347 + cpi->framerate = 30; 1.1348 + 1.1349 + cpi->ref_framerate = cpi->framerate; 1.1350 + 1.1351 + /* change includes all joint functionality */ 1.1352 + vp8_change_config(cpi, oxcf); 1.1353 + 1.1354 + /* Initialize active best and worst q and average q values. */ 1.1355 + cpi->active_worst_quality = cpi->oxcf.worst_allowed_q; 1.1356 + cpi->active_best_quality = cpi->oxcf.best_allowed_q; 1.1357 + cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q; 1.1358 + 1.1359 + /* Initialise the starting buffer levels */ 1.1360 + cpi->buffer_level = cpi->oxcf.starting_buffer_level; 1.1361 + cpi->bits_off_target = cpi->oxcf.starting_buffer_level; 1.1362 + 1.1363 + cpi->rolling_target_bits = cpi->av_per_frame_bandwidth; 1.1364 + cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth; 1.1365 + cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth; 1.1366 + cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth; 1.1367 + 1.1368 + cpi->total_actual_bits = 0; 1.1369 + cpi->total_target_vs_actual = 0; 1.1370 + 1.1371 + /* Temporal scalabilty */ 1.1372 + if (cpi->oxcf.number_of_layers > 1) 1.1373 + { 1.1374 + unsigned int i; 1.1375 + double prev_layer_framerate=0; 1.1376 + 1.1377 + for (i=0; i<cpi->oxcf.number_of_layers; i++) 1.1378 + { 1.1379 + init_temporal_layer_context(cpi, oxcf, i, prev_layer_framerate); 1.1380 + prev_layer_framerate = cpi->output_framerate / 1.1381 + cpi->oxcf.rate_decimator[i]; 1.1382 + } 1.1383 + } 1.1384 + 1.1385 +#if VP8_TEMPORAL_ALT_REF 1.1386 + { 1.1387 + int i; 1.1388 + 1.1389 + cpi->fixed_divide[0] = 0; 1.1390 + 1.1391 + for (i = 1; i < 512; i++) 1.1392 + cpi->fixed_divide[i] = 0x80000 / i; 1.1393 + } 1.1394 +#endif 1.1395 +} 1.1396 + 1.1397 +static void update_layer_contexts (VP8_COMP *cpi) 1.1398 +{ 1.1399 + VP8_CONFIG *oxcf = &cpi->oxcf; 1.1400 + 1.1401 + /* Update snapshots of the layer contexts to reflect new parameters */ 1.1402 + if (oxcf->number_of_layers > 1) 1.1403 + { 1.1404 + unsigned int i; 1.1405 + double prev_layer_framerate=0; 1.1406 + 1.1407 + for (i=0; i<oxcf->number_of_layers; i++) 1.1408 + { 1.1409 + LAYER_CONTEXT *lc = &cpi->layer_context[i]; 1.1410 + 1.1411 + lc->framerate = 1.1412 + cpi->ref_framerate / oxcf->rate_decimator[i]; 1.1413 + lc->target_bandwidth = oxcf->target_bitrate[i] * 1000; 1.1414 + 1.1415 + lc->starting_buffer_level = rescale( 1.1416 + (int)oxcf->starting_buffer_level_in_ms, 1.1417 + lc->target_bandwidth, 1000); 1.1418 + 1.1419 + if (oxcf->optimal_buffer_level == 0) 1.1420 + lc->optimal_buffer_level = lc->target_bandwidth / 8; 1.1421 + else 1.1422 + lc->optimal_buffer_level = rescale( 1.1423 + (int)oxcf->optimal_buffer_level_in_ms, 1.1424 + lc->target_bandwidth, 1000); 1.1425 + 1.1426 + if (oxcf->maximum_buffer_size == 0) 1.1427 + lc->maximum_buffer_size = lc->target_bandwidth / 8; 1.1428 + else 1.1429 + lc->maximum_buffer_size = rescale( 1.1430 + (int)oxcf->maximum_buffer_size_in_ms, 1.1431 + lc->target_bandwidth, 1000); 1.1432 + 1.1433 + /* Work out the average size of a frame within this layer */ 1.1434 + if (i > 0) 1.1435 + lc->avg_frame_size_for_layer = 1.1436 + (int)((oxcf->target_bitrate[i] - 1.1437 + oxcf->target_bitrate[i-1]) * 1000 / 1.1438 + (lc->framerate - prev_layer_framerate)); 1.1439 + 1.1440 + prev_layer_framerate = lc->framerate; 1.1441 + } 1.1442 + } 1.1443 +} 1.1444 + 1.1445 +void vp8_change_config(VP8_COMP *cpi, VP8_CONFIG *oxcf) 1.1446 +{ 1.1447 + VP8_COMMON *cm = &cpi->common; 1.1448 + int last_w, last_h, prev_number_of_layers; 1.1449 + 1.1450 + if (!cpi) 1.1451 + return; 1.1452 + 1.1453 + if (!oxcf) 1.1454 + return; 1.1455 + 1.1456 +#if CONFIG_MULTITHREAD 1.1457 + /* wait for the last picture loopfilter thread done */ 1.1458 + if (cpi->b_lpf_running) 1.1459 + { 1.1460 + sem_wait(&cpi->h_event_end_lpf); 1.1461 + cpi->b_lpf_running = 0; 1.1462 + } 1.1463 +#endif 1.1464 + 1.1465 + if (cm->version != oxcf->Version) 1.1466 + { 1.1467 + cm->version = oxcf->Version; 1.1468 + vp8_setup_version(cm); 1.1469 + } 1.1470 + 1.1471 + last_w = cpi->oxcf.Width; 1.1472 + last_h = cpi->oxcf.Height; 1.1473 + prev_number_of_layers = cpi->oxcf.number_of_layers; 1.1474 + 1.1475 + cpi->oxcf = *oxcf; 1.1476 + 1.1477 + switch (cpi->oxcf.Mode) 1.1478 + { 1.1479 + 1.1480 + case MODE_REALTIME: 1.1481 + cpi->pass = 0; 1.1482 + cpi->compressor_speed = 2; 1.1483 + 1.1484 + if (cpi->oxcf.cpu_used < -16) 1.1485 + { 1.1486 + cpi->oxcf.cpu_used = -16; 1.1487 + } 1.1488 + 1.1489 + if (cpi->oxcf.cpu_used > 16) 1.1490 + cpi->oxcf.cpu_used = 16; 1.1491 + 1.1492 + break; 1.1493 + 1.1494 + case MODE_GOODQUALITY: 1.1495 + cpi->pass = 0; 1.1496 + cpi->compressor_speed = 1; 1.1497 + 1.1498 + if (cpi->oxcf.cpu_used < -5) 1.1499 + { 1.1500 + cpi->oxcf.cpu_used = -5; 1.1501 + } 1.1502 + 1.1503 + if (cpi->oxcf.cpu_used > 5) 1.1504 + cpi->oxcf.cpu_used = 5; 1.1505 + 1.1506 + break; 1.1507 + 1.1508 + case MODE_BESTQUALITY: 1.1509 + cpi->pass = 0; 1.1510 + cpi->compressor_speed = 0; 1.1511 + break; 1.1512 + 1.1513 + case MODE_FIRSTPASS: 1.1514 + cpi->pass = 1; 1.1515 + cpi->compressor_speed = 1; 1.1516 + break; 1.1517 + case MODE_SECONDPASS: 1.1518 + cpi->pass = 2; 1.1519 + cpi->compressor_speed = 1; 1.1520 + 1.1521 + if (cpi->oxcf.cpu_used < -5) 1.1522 + { 1.1523 + cpi->oxcf.cpu_used = -5; 1.1524 + } 1.1525 + 1.1526 + if (cpi->oxcf.cpu_used > 5) 1.1527 + cpi->oxcf.cpu_used = 5; 1.1528 + 1.1529 + break; 1.1530 + case MODE_SECONDPASS_BEST: 1.1531 + cpi->pass = 2; 1.1532 + cpi->compressor_speed = 0; 1.1533 + break; 1.1534 + } 1.1535 + 1.1536 + if (cpi->pass == 0) 1.1537 + cpi->auto_worst_q = 1; 1.1538 + 1.1539 + cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q]; 1.1540 + cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q]; 1.1541 + cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level]; 1.1542 + 1.1543 + if (oxcf->fixed_q >= 0) 1.1544 + { 1.1545 + if (oxcf->worst_allowed_q < 0) 1.1546 + cpi->oxcf.fixed_q = q_trans[0]; 1.1547 + else 1.1548 + cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q]; 1.1549 + 1.1550 + if (oxcf->alt_q < 0) 1.1551 + cpi->oxcf.alt_q = q_trans[0]; 1.1552 + else 1.1553 + cpi->oxcf.alt_q = q_trans[oxcf->alt_q]; 1.1554 + 1.1555 + if (oxcf->key_q < 0) 1.1556 + cpi->oxcf.key_q = q_trans[0]; 1.1557 + else 1.1558 + cpi->oxcf.key_q = q_trans[oxcf->key_q]; 1.1559 + 1.1560 + if (oxcf->gold_q < 0) 1.1561 + cpi->oxcf.gold_q = q_trans[0]; 1.1562 + else 1.1563 + cpi->oxcf.gold_q = q_trans[oxcf->gold_q]; 1.1564 + 1.1565 + } 1.1566 + 1.1567 + cpi->baseline_gf_interval = 1.1568 + cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL; 1.1569 + 1.1570 + cpi->ref_frame_flags = VP8_ALTR_FRAME | VP8_GOLD_FRAME | VP8_LAST_FRAME; 1.1571 + 1.1572 + cm->refresh_golden_frame = 0; 1.1573 + cm->refresh_last_frame = 1; 1.1574 + cm->refresh_entropy_probs = 1; 1.1575 + 1.1576 +#if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING) 1.1577 + cpi->oxcf.token_partitions = 3; 1.1578 +#endif 1.1579 + 1.1580 + if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3) 1.1581 + cm->multi_token_partition = 1.1582 + (TOKEN_PARTITION) cpi->oxcf.token_partitions; 1.1583 + 1.1584 + setup_features(cpi); 1.1585 + 1.1586 + { 1.1587 + int i; 1.1588 + 1.1589 + for (i = 0; i < MAX_MB_SEGMENTS; i++) 1.1590 + cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout; 1.1591 + } 1.1592 + 1.1593 + /* At the moment the first order values may not be > MAXQ */ 1.1594 + if (cpi->oxcf.fixed_q > MAXQ) 1.1595 + cpi->oxcf.fixed_q = MAXQ; 1.1596 + 1.1597 + /* local file playback mode == really big buffer */ 1.1598 + if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK) 1.1599 + { 1.1600 + cpi->oxcf.starting_buffer_level = 60000; 1.1601 + cpi->oxcf.optimal_buffer_level = 60000; 1.1602 + cpi->oxcf.maximum_buffer_size = 240000; 1.1603 + cpi->oxcf.starting_buffer_level_in_ms = 60000; 1.1604 + cpi->oxcf.optimal_buffer_level_in_ms = 60000; 1.1605 + cpi->oxcf.maximum_buffer_size_in_ms = 240000; 1.1606 + } 1.1607 + 1.1608 + /* Convert target bandwidth from Kbit/s to Bit/s */ 1.1609 + cpi->oxcf.target_bandwidth *= 1000; 1.1610 + 1.1611 + cpi->oxcf.starting_buffer_level = 1.1612 + rescale((int)cpi->oxcf.starting_buffer_level, 1.1613 + cpi->oxcf.target_bandwidth, 1000); 1.1614 + 1.1615 + /* Set or reset optimal and maximum buffer levels. */ 1.1616 + if (cpi->oxcf.optimal_buffer_level == 0) 1.1617 + cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8; 1.1618 + else 1.1619 + cpi->oxcf.optimal_buffer_level = 1.1620 + rescale((int)cpi->oxcf.optimal_buffer_level, 1.1621 + cpi->oxcf.target_bandwidth, 1000); 1.1622 + 1.1623 + if (cpi->oxcf.maximum_buffer_size == 0) 1.1624 + cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8; 1.1625 + else 1.1626 + cpi->oxcf.maximum_buffer_size = 1.1627 + rescale((int)cpi->oxcf.maximum_buffer_size, 1.1628 + cpi->oxcf.target_bandwidth, 1000); 1.1629 + 1.1630 + /* Set up frame rate and related parameters rate control values. */ 1.1631 + vp8_new_framerate(cpi, cpi->framerate); 1.1632 + 1.1633 + /* Set absolute upper and lower quality limits */ 1.1634 + cpi->worst_quality = cpi->oxcf.worst_allowed_q; 1.1635 + cpi->best_quality = cpi->oxcf.best_allowed_q; 1.1636 + 1.1637 + /* active values should only be modified if out of new range */ 1.1638 + if (cpi->active_worst_quality > cpi->oxcf.worst_allowed_q) 1.1639 + { 1.1640 + cpi->active_worst_quality = cpi->oxcf.worst_allowed_q; 1.1641 + } 1.1642 + /* less likely */ 1.1643 + else if (cpi->active_worst_quality < cpi->oxcf.best_allowed_q) 1.1644 + { 1.1645 + cpi->active_worst_quality = cpi->oxcf.best_allowed_q; 1.1646 + } 1.1647 + if (cpi->active_best_quality < cpi->oxcf.best_allowed_q) 1.1648 + { 1.1649 + cpi->active_best_quality = cpi->oxcf.best_allowed_q; 1.1650 + } 1.1651 + /* less likely */ 1.1652 + else if (cpi->active_best_quality > cpi->oxcf.worst_allowed_q) 1.1653 + { 1.1654 + cpi->active_best_quality = cpi->oxcf.worst_allowed_q; 1.1655 + } 1.1656 + 1.1657 + cpi->buffered_mode = cpi->oxcf.optimal_buffer_level > 0; 1.1658 + 1.1659 + cpi->cq_target_quality = cpi->oxcf.cq_level; 1.1660 + 1.1661 + /* Only allow dropped frames in buffered mode */ 1.1662 + cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode; 1.1663 + 1.1664 + cpi->target_bandwidth = cpi->oxcf.target_bandwidth; 1.1665 + 1.1666 + // Check if the number of temporal layers has changed, and if so reset the 1.1667 + // pattern counter and set/initialize the temporal layer context for the 1.1668 + // new layer configuration. 1.1669 + if (cpi->oxcf.number_of_layers != prev_number_of_layers) 1.1670 + { 1.1671 + // If the number of temporal layers are changed we must start at the 1.1672 + // base of the pattern cycle, so reset temporal_pattern_counter. 1.1673 + cpi->temporal_pattern_counter = 0; 1.1674 + reset_temporal_layer_change(cpi, oxcf, prev_number_of_layers); 1.1675 + } 1.1676 + 1.1677 + cm->Width = cpi->oxcf.Width; 1.1678 + cm->Height = cpi->oxcf.Height; 1.1679 + 1.1680 + /* TODO(jkoleszar): if an internal spatial resampling is active, 1.1681 + * and we downsize the input image, maybe we should clear the 1.1682 + * internal scale immediately rather than waiting for it to 1.1683 + * correct. 1.1684 + */ 1.1685 + 1.1686 + /* VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs) */ 1.1687 + if (cpi->oxcf.Sharpness > 7) 1.1688 + cpi->oxcf.Sharpness = 7; 1.1689 + 1.1690 + cm->sharpness_level = cpi->oxcf.Sharpness; 1.1691 + 1.1692 + if (cm->horiz_scale != NORMAL || cm->vert_scale != NORMAL) 1.1693 + { 1.1694 + int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs); 1.1695 + int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs); 1.1696 + 1.1697 + Scale2Ratio(cm->horiz_scale, &hr, &hs); 1.1698 + Scale2Ratio(cm->vert_scale, &vr, &vs); 1.1699 + 1.1700 + /* always go to the next whole number */ 1.1701 + cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs; 1.1702 + cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs; 1.1703 + } 1.1704 + 1.1705 + if (last_w != cpi->oxcf.Width || last_h != cpi->oxcf.Height) 1.1706 + cpi->force_next_frame_intra = 1; 1.1707 + 1.1708 + if (((cm->Width + 15) & 0xfffffff0) != 1.1709 + cm->yv12_fb[cm->lst_fb_idx].y_width || 1.1710 + ((cm->Height + 15) & 0xfffffff0) != 1.1711 + cm->yv12_fb[cm->lst_fb_idx].y_height || 1.1712 + cm->yv12_fb[cm->lst_fb_idx].y_width == 0) 1.1713 + { 1.1714 + dealloc_raw_frame_buffers(cpi); 1.1715 + alloc_raw_frame_buffers(cpi); 1.1716 + vp8_alloc_compressor_data(cpi); 1.1717 + } 1.1718 + 1.1719 + if (cpi->oxcf.fixed_q >= 0) 1.1720 + { 1.1721 + cpi->last_q[0] = cpi->oxcf.fixed_q; 1.1722 + cpi->last_q[1] = cpi->oxcf.fixed_q; 1.1723 + } 1.1724 + 1.1725 + cpi->Speed = cpi->oxcf.cpu_used; 1.1726 + 1.1727 + /* force to allowlag to 0 if lag_in_frames is 0; */ 1.1728 + if (cpi->oxcf.lag_in_frames == 0) 1.1729 + { 1.1730 + cpi->oxcf.allow_lag = 0; 1.1731 + } 1.1732 + /* Limit on lag buffers as these are not currently dynamically allocated */ 1.1733 + else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS) 1.1734 + cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS; 1.1735 + 1.1736 + /* YX Temp */ 1.1737 + cpi->alt_ref_source = NULL; 1.1738 + cpi->is_src_frame_alt_ref = 0; 1.1739 + 1.1740 +#if CONFIG_TEMPORAL_DENOISING 1.1741 + if (cpi->oxcf.noise_sensitivity) 1.1742 + { 1.1743 + if (!cpi->denoiser.yv12_mc_running_avg.buffer_alloc) 1.1744 + { 1.1745 + int width = (cpi->oxcf.Width + 15) & ~15; 1.1746 + int height = (cpi->oxcf.Height + 15) & ~15; 1.1747 + vp8_denoiser_allocate(&cpi->denoiser, width, height); 1.1748 + } 1.1749 + } 1.1750 +#endif 1.1751 + 1.1752 +#if 0 1.1753 + /* Experimental RD Code */ 1.1754 + cpi->frame_distortion = 0; 1.1755 + cpi->last_frame_distortion = 0; 1.1756 +#endif 1.1757 + 1.1758 +} 1.1759 + 1.1760 +#define M_LOG2_E 0.693147180559945309417 1.1761 +#define log2f(x) (log (x) / (float) M_LOG2_E) 1.1762 +static void cal_mvsadcosts(int *mvsadcost[2]) 1.1763 +{ 1.1764 + int i = 1; 1.1765 + 1.1766 + mvsadcost [0] [0] = 300; 1.1767 + mvsadcost [1] [0] = 300; 1.1768 + 1.1769 + do 1.1770 + { 1.1771 + double z = 256 * (2 * (log2f(8 * i) + .6)); 1.1772 + mvsadcost [0][i] = (int) z; 1.1773 + mvsadcost [1][i] = (int) z; 1.1774 + mvsadcost [0][-i] = (int) z; 1.1775 + mvsadcost [1][-i] = (int) z; 1.1776 + } 1.1777 + while (++i <= mvfp_max); 1.1778 +} 1.1779 + 1.1780 +struct VP8_COMP* vp8_create_compressor(VP8_CONFIG *oxcf) 1.1781 +{ 1.1782 + int i; 1.1783 + 1.1784 + VP8_COMP *cpi; 1.1785 + VP8_COMMON *cm; 1.1786 + 1.1787 + cpi = vpx_memalign(32, sizeof(VP8_COMP)); 1.1788 + /* Check that the CPI instance is valid */ 1.1789 + if (!cpi) 1.1790 + return 0; 1.1791 + 1.1792 + cm = &cpi->common; 1.1793 + 1.1794 + vpx_memset(cpi, 0, sizeof(VP8_COMP)); 1.1795 + 1.1796 + if (setjmp(cm->error.jmp)) 1.1797 + { 1.1798 + cpi->common.error.setjmp = 0; 1.1799 + vp8_remove_compressor(&cpi); 1.1800 + return 0; 1.1801 + } 1.1802 + 1.1803 + cpi->common.error.setjmp = 1; 1.1804 + 1.1805 + CHECK_MEM_ERROR(cpi->mb.ss, vpx_calloc(sizeof(search_site), (MAX_MVSEARCH_STEPS * 8) + 1)); 1.1806 + 1.1807 + vp8_create_common(&cpi->common); 1.1808 + 1.1809 + init_config(cpi, oxcf); 1.1810 + 1.1811 + memcpy(cpi->base_skip_false_prob, vp8cx_base_skip_false_prob, sizeof(vp8cx_base_skip_false_prob)); 1.1812 + cpi->common.current_video_frame = 0; 1.1813 + cpi->temporal_pattern_counter = 0; 1.1814 + cpi->kf_overspend_bits = 0; 1.1815 + cpi->kf_bitrate_adjustment = 0; 1.1816 + cpi->frames_till_gf_update_due = 0; 1.1817 + cpi->gf_overspend_bits = 0; 1.1818 + cpi->non_gf_bitrate_adjustment = 0; 1.1819 + cpi->prob_last_coded = 128; 1.1820 + cpi->prob_gf_coded = 128; 1.1821 + cpi->prob_intra_coded = 63; 1.1822 + 1.1823 + /* Prime the recent reference frame usage counters. 1.1824 + * Hereafter they will be maintained as a sort of moving average 1.1825 + */ 1.1826 + cpi->recent_ref_frame_usage[INTRA_FRAME] = 1; 1.1827 + cpi->recent_ref_frame_usage[LAST_FRAME] = 1; 1.1828 + cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1; 1.1829 + cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1; 1.1830 + 1.1831 + /* Set reference frame sign bias for ALTREF frame to 1 (for now) */ 1.1832 + cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1; 1.1833 + 1.1834 + cpi->twopass.gf_decay_rate = 0; 1.1835 + cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL; 1.1836 + 1.1837 + cpi->gold_is_last = 0 ; 1.1838 + cpi->alt_is_last = 0 ; 1.1839 + cpi->gold_is_alt = 0 ; 1.1840 + 1.1841 + cpi->active_map_enabled = 0; 1.1842 + 1.1843 +#if 0 1.1844 + /* Experimental code for lagged and one pass */ 1.1845 + /* Initialise one_pass GF frames stats */ 1.1846 + /* Update stats used for GF selection */ 1.1847 + if (cpi->pass == 0) 1.1848 + { 1.1849 + cpi->one_pass_frame_index = 0; 1.1850 + 1.1851 + for (i = 0; i < MAX_LAG_BUFFERS; i++) 1.1852 + { 1.1853 + cpi->one_pass_frame_stats[i].frames_so_far = 0; 1.1854 + cpi->one_pass_frame_stats[i].frame_intra_error = 0.0; 1.1855 + cpi->one_pass_frame_stats[i].frame_coded_error = 0.0; 1.1856 + cpi->one_pass_frame_stats[i].frame_pcnt_inter = 0.0; 1.1857 + cpi->one_pass_frame_stats[i].frame_pcnt_motion = 0.0; 1.1858 + cpi->one_pass_frame_stats[i].frame_mvr = 0.0; 1.1859 + cpi->one_pass_frame_stats[i].frame_mvr_abs = 0.0; 1.1860 + cpi->one_pass_frame_stats[i].frame_mvc = 0.0; 1.1861 + cpi->one_pass_frame_stats[i].frame_mvc_abs = 0.0; 1.1862 + } 1.1863 + } 1.1864 +#endif 1.1865 + 1.1866 + /* Should we use the cyclic refresh method. 1.1867 + * Currently this is tied to error resilliant mode 1.1868 + */ 1.1869 + cpi->cyclic_refresh_mode_enabled = cpi->oxcf.error_resilient_mode; 1.1870 + cpi->cyclic_refresh_mode_max_mbs_perframe = (cpi->common.mb_rows * cpi->common.mb_cols) / 5; 1.1871 + cpi->cyclic_refresh_mode_index = 0; 1.1872 + cpi->cyclic_refresh_q = 32; 1.1873 + 1.1874 + if (cpi->cyclic_refresh_mode_enabled) 1.1875 + { 1.1876 + CHECK_MEM_ERROR(cpi->cyclic_refresh_map, vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1)); 1.1877 + } 1.1878 + else 1.1879 + cpi->cyclic_refresh_map = (signed char *) NULL; 1.1880 + 1.1881 +#ifdef VP8_ENTROPY_STATS 1.1882 + init_context_counters(); 1.1883 +#endif 1.1884 + 1.1885 + /*Initialize the feed-forward activity masking.*/ 1.1886 + cpi->activity_avg = 90<<12; 1.1887 + 1.1888 + /* Give a sensible default for the first frame. */ 1.1889 + cpi->frames_since_key = 8; 1.1890 + cpi->key_frame_frequency = cpi->oxcf.key_freq; 1.1891 + cpi->this_key_frame_forced = 0; 1.1892 + cpi->next_key_frame_forced = 0; 1.1893 + 1.1894 + cpi->source_alt_ref_pending = 0; 1.1895 + cpi->source_alt_ref_active = 0; 1.1896 + cpi->common.refresh_alt_ref_frame = 0; 1.1897 + 1.1898 + cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS; 1.1899 +#if CONFIG_INTERNAL_STATS 1.1900 + cpi->b_calculate_ssimg = 0; 1.1901 + 1.1902 + cpi->count = 0; 1.1903 + cpi->bytes = 0; 1.1904 + 1.1905 + if (cpi->b_calculate_psnr) 1.1906 + { 1.1907 + cpi->total_sq_error = 0.0; 1.1908 + cpi->total_sq_error2 = 0.0; 1.1909 + cpi->total_y = 0.0; 1.1910 + cpi->total_u = 0.0; 1.1911 + cpi->total_v = 0.0; 1.1912 + cpi->total = 0.0; 1.1913 + cpi->totalp_y = 0.0; 1.1914 + cpi->totalp_u = 0.0; 1.1915 + cpi->totalp_v = 0.0; 1.1916 + cpi->totalp = 0.0; 1.1917 + cpi->tot_recode_hits = 0; 1.1918 + cpi->summed_quality = 0; 1.1919 + cpi->summed_weights = 0; 1.1920 + } 1.1921 + 1.1922 + if (cpi->b_calculate_ssimg) 1.1923 + { 1.1924 + cpi->total_ssimg_y = 0; 1.1925 + cpi->total_ssimg_u = 0; 1.1926 + cpi->total_ssimg_v = 0; 1.1927 + cpi->total_ssimg_all = 0; 1.1928 + } 1.1929 + 1.1930 +#endif 1.1931 + 1.1932 + cpi->first_time_stamp_ever = 0x7FFFFFFF; 1.1933 + 1.1934 + cpi->frames_till_gf_update_due = 0; 1.1935 + cpi->key_frame_count = 1; 1.1936 + 1.1937 + cpi->ni_av_qi = cpi->oxcf.worst_allowed_q; 1.1938 + cpi->ni_tot_qi = 0; 1.1939 + cpi->ni_frames = 0; 1.1940 + cpi->total_byte_count = 0; 1.1941 + 1.1942 + cpi->drop_frame = 0; 1.1943 + 1.1944 + cpi->rate_correction_factor = 1.0; 1.1945 + cpi->key_frame_rate_correction_factor = 1.0; 1.1946 + cpi->gf_rate_correction_factor = 1.0; 1.1947 + cpi->twopass.est_max_qcorrection_factor = 1.0; 1.1948 + 1.1949 + for (i = 0; i < KEY_FRAME_CONTEXT; i++) 1.1950 + { 1.1951 + cpi->prior_key_frame_distance[i] = (int)cpi->output_framerate; 1.1952 + } 1.1953 + 1.1954 +#ifdef OUTPUT_YUV_SRC 1.1955 + yuv_file = fopen("bd.yuv", "ab"); 1.1956 +#endif 1.1957 + 1.1958 +#if 0 1.1959 + framepsnr = fopen("framepsnr.stt", "a"); 1.1960 + kf_list = fopen("kf_list.stt", "w"); 1.1961 +#endif 1.1962 + 1.1963 + cpi->output_pkt_list = oxcf->output_pkt_list; 1.1964 + 1.1965 +#if !(CONFIG_REALTIME_ONLY) 1.1966 + 1.1967 + if (cpi->pass == 1) 1.1968 + { 1.1969 + vp8_init_first_pass(cpi); 1.1970 + } 1.1971 + else if (cpi->pass == 2) 1.1972 + { 1.1973 + size_t packet_sz = sizeof(FIRSTPASS_STATS); 1.1974 + int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz); 1.1975 + 1.1976 + cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf; 1.1977 + cpi->twopass.stats_in = cpi->twopass.stats_in_start; 1.1978 + cpi->twopass.stats_in_end = (void*)((char *)cpi->twopass.stats_in 1.1979 + + (packets - 1) * packet_sz); 1.1980 + vp8_init_second_pass(cpi); 1.1981 + } 1.1982 + 1.1983 +#endif 1.1984 + 1.1985 + if (cpi->compressor_speed == 2) 1.1986 + { 1.1987 + cpi->avg_encode_time = 0; 1.1988 + cpi->avg_pick_mode_time = 0; 1.1989 + } 1.1990 + 1.1991 + vp8_set_speed_features(cpi); 1.1992 + 1.1993 + /* Set starting values of RD threshold multipliers (128 = *1) */ 1.1994 + for (i = 0; i < MAX_MODES; i++) 1.1995 + { 1.1996 + cpi->mb.rd_thresh_mult[i] = 128; 1.1997 + } 1.1998 + 1.1999 +#ifdef VP8_ENTROPY_STATS 1.2000 + init_mv_ref_counts(); 1.2001 +#endif 1.2002 + 1.2003 +#if CONFIG_MULTITHREAD 1.2004 + if(vp8cx_create_encoder_threads(cpi)) 1.2005 + { 1.2006 + vp8_remove_compressor(&cpi); 1.2007 + return 0; 1.2008 + } 1.2009 +#endif 1.2010 + 1.2011 + cpi->fn_ptr[BLOCK_16X16].sdf = vp8_sad16x16; 1.2012 + cpi->fn_ptr[BLOCK_16X16].vf = vp8_variance16x16; 1.2013 + cpi->fn_ptr[BLOCK_16X16].svf = vp8_sub_pixel_variance16x16; 1.2014 + cpi->fn_ptr[BLOCK_16X16].svf_halfpix_h = vp8_variance_halfpixvar16x16_h; 1.2015 + cpi->fn_ptr[BLOCK_16X16].svf_halfpix_v = vp8_variance_halfpixvar16x16_v; 1.2016 + cpi->fn_ptr[BLOCK_16X16].svf_halfpix_hv = vp8_variance_halfpixvar16x16_hv; 1.2017 + cpi->fn_ptr[BLOCK_16X16].sdx3f = vp8_sad16x16x3; 1.2018 + cpi->fn_ptr[BLOCK_16X16].sdx8f = vp8_sad16x16x8; 1.2019 + cpi->fn_ptr[BLOCK_16X16].sdx4df = vp8_sad16x16x4d; 1.2020 + 1.2021 + cpi->fn_ptr[BLOCK_16X8].sdf = vp8_sad16x8; 1.2022 + cpi->fn_ptr[BLOCK_16X8].vf = vp8_variance16x8; 1.2023 + cpi->fn_ptr[BLOCK_16X8].svf = vp8_sub_pixel_variance16x8; 1.2024 + cpi->fn_ptr[BLOCK_16X8].svf_halfpix_h = NULL; 1.2025 + cpi->fn_ptr[BLOCK_16X8].svf_halfpix_v = NULL; 1.2026 + cpi->fn_ptr[BLOCK_16X8].svf_halfpix_hv = NULL; 1.2027 + cpi->fn_ptr[BLOCK_16X8].sdx3f = vp8_sad16x8x3; 1.2028 + cpi->fn_ptr[BLOCK_16X8].sdx8f = vp8_sad16x8x8; 1.2029 + cpi->fn_ptr[BLOCK_16X8].sdx4df = vp8_sad16x8x4d; 1.2030 + 1.2031 + cpi->fn_ptr[BLOCK_8X16].sdf = vp8_sad8x16; 1.2032 + cpi->fn_ptr[BLOCK_8X16].vf = vp8_variance8x16; 1.2033 + cpi->fn_ptr[BLOCK_8X16].svf = vp8_sub_pixel_variance8x16; 1.2034 + cpi->fn_ptr[BLOCK_8X16].svf_halfpix_h = NULL; 1.2035 + cpi->fn_ptr[BLOCK_8X16].svf_halfpix_v = NULL; 1.2036 + cpi->fn_ptr[BLOCK_8X16].svf_halfpix_hv = NULL; 1.2037 + cpi->fn_ptr[BLOCK_8X16].sdx3f = vp8_sad8x16x3; 1.2038 + cpi->fn_ptr[BLOCK_8X16].sdx8f = vp8_sad8x16x8; 1.2039 + cpi->fn_ptr[BLOCK_8X16].sdx4df = vp8_sad8x16x4d; 1.2040 + 1.2041 + cpi->fn_ptr[BLOCK_8X8].sdf = vp8_sad8x8; 1.2042 + cpi->fn_ptr[BLOCK_8X8].vf = vp8_variance8x8; 1.2043 + cpi->fn_ptr[BLOCK_8X8].svf = vp8_sub_pixel_variance8x8; 1.2044 + cpi->fn_ptr[BLOCK_8X8].svf_halfpix_h = NULL; 1.2045 + cpi->fn_ptr[BLOCK_8X8].svf_halfpix_v = NULL; 1.2046 + cpi->fn_ptr[BLOCK_8X8].svf_halfpix_hv = NULL; 1.2047 + cpi->fn_ptr[BLOCK_8X8].sdx3f = vp8_sad8x8x3; 1.2048 + cpi->fn_ptr[BLOCK_8X8].sdx8f = vp8_sad8x8x8; 1.2049 + cpi->fn_ptr[BLOCK_8X8].sdx4df = vp8_sad8x8x4d; 1.2050 + 1.2051 + cpi->fn_ptr[BLOCK_4X4].sdf = vp8_sad4x4; 1.2052 + cpi->fn_ptr[BLOCK_4X4].vf = vp8_variance4x4; 1.2053 + cpi->fn_ptr[BLOCK_4X4].svf = vp8_sub_pixel_variance4x4; 1.2054 + cpi->fn_ptr[BLOCK_4X4].svf_halfpix_h = NULL; 1.2055 + cpi->fn_ptr[BLOCK_4X4].svf_halfpix_v = NULL; 1.2056 + cpi->fn_ptr[BLOCK_4X4].svf_halfpix_hv = NULL; 1.2057 + cpi->fn_ptr[BLOCK_4X4].sdx3f = vp8_sad4x4x3; 1.2058 + cpi->fn_ptr[BLOCK_4X4].sdx8f = vp8_sad4x4x8; 1.2059 + cpi->fn_ptr[BLOCK_4X4].sdx4df = vp8_sad4x4x4d; 1.2060 + 1.2061 +#if ARCH_X86 || ARCH_X86_64 1.2062 + cpi->fn_ptr[BLOCK_16X16].copymem = vp8_copy32xn; 1.2063 + cpi->fn_ptr[BLOCK_16X8].copymem = vp8_copy32xn; 1.2064 + cpi->fn_ptr[BLOCK_8X16].copymem = vp8_copy32xn; 1.2065 + cpi->fn_ptr[BLOCK_8X8].copymem = vp8_copy32xn; 1.2066 + cpi->fn_ptr[BLOCK_4X4].copymem = vp8_copy32xn; 1.2067 +#endif 1.2068 + 1.2069 + cpi->full_search_sad = vp8_full_search_sad; 1.2070 + cpi->diamond_search_sad = vp8_diamond_search_sad; 1.2071 + cpi->refining_search_sad = vp8_refining_search_sad; 1.2072 + 1.2073 + /* make sure frame 1 is okay */ 1.2074 + cpi->mb.error_bins[0] = cpi->common.MBs; 1.2075 + 1.2076 + /* vp8cx_init_quantizer() is first called here. Add check in 1.2077 + * vp8cx_frame_init_quantizer() so that vp8cx_init_quantizer is only 1.2078 + * called later when needed. This will avoid unnecessary calls of 1.2079 + * vp8cx_init_quantizer() for every frame. 1.2080 + */ 1.2081 + vp8cx_init_quantizer(cpi); 1.2082 + 1.2083 + vp8_loop_filter_init(cm); 1.2084 + 1.2085 + cpi->common.error.setjmp = 0; 1.2086 + 1.2087 +#if CONFIG_MULTI_RES_ENCODING 1.2088 + 1.2089 + /* Calculate # of MBs in a row in lower-resolution level image. */ 1.2090 + if (cpi->oxcf.mr_encoder_id > 0) 1.2091 + vp8_cal_low_res_mb_cols(cpi); 1.2092 + 1.2093 +#endif 1.2094 + 1.2095 + /* setup RD costs to MACROBLOCK struct */ 1.2096 + 1.2097 + cpi->mb.mvcost[0] = &cpi->rd_costs.mvcosts[0][mv_max+1]; 1.2098 + cpi->mb.mvcost[1] = &cpi->rd_costs.mvcosts[1][mv_max+1]; 1.2099 + cpi->mb.mvsadcost[0] = &cpi->rd_costs.mvsadcosts[0][mvfp_max+1]; 1.2100 + cpi->mb.mvsadcost[1] = &cpi->rd_costs.mvsadcosts[1][mvfp_max+1]; 1.2101 + 1.2102 + cal_mvsadcosts(cpi->mb.mvsadcost); 1.2103 + 1.2104 + cpi->mb.mbmode_cost = cpi->rd_costs.mbmode_cost; 1.2105 + cpi->mb.intra_uv_mode_cost = cpi->rd_costs.intra_uv_mode_cost; 1.2106 + cpi->mb.bmode_costs = cpi->rd_costs.bmode_costs; 1.2107 + cpi->mb.inter_bmode_costs = cpi->rd_costs.inter_bmode_costs; 1.2108 + cpi->mb.token_costs = cpi->rd_costs.token_costs; 1.2109 + 1.2110 + /* setup block ptrs & offsets */ 1.2111 + vp8_setup_block_ptrs(&cpi->mb); 1.2112 + vp8_setup_block_dptrs(&cpi->mb.e_mbd); 1.2113 + 1.2114 + return cpi; 1.2115 +} 1.2116 + 1.2117 + 1.2118 +void vp8_remove_compressor(VP8_COMP **ptr) 1.2119 +{ 1.2120 + VP8_COMP *cpi = *ptr; 1.2121 + 1.2122 + if (!cpi) 1.2123 + return; 1.2124 + 1.2125 + if (cpi && (cpi->common.current_video_frame > 0)) 1.2126 + { 1.2127 +#if !(CONFIG_REALTIME_ONLY) 1.2128 + 1.2129 + if (cpi->pass == 2) 1.2130 + { 1.2131 + vp8_end_second_pass(cpi); 1.2132 + } 1.2133 + 1.2134 +#endif 1.2135 + 1.2136 +#ifdef VP8_ENTROPY_STATS 1.2137 + print_context_counters(); 1.2138 + print_tree_update_probs(); 1.2139 + print_mode_context(); 1.2140 +#endif 1.2141 + 1.2142 +#if CONFIG_INTERNAL_STATS 1.2143 + 1.2144 + if (cpi->pass != 1) 1.2145 + { 1.2146 + FILE *f = fopen("opsnr.stt", "a"); 1.2147 + double time_encoded = (cpi->last_end_time_stamp_seen 1.2148 + - cpi->first_time_stamp_ever) / 10000000.000; 1.2149 + double total_encode_time = (cpi->time_receive_data + 1.2150 + cpi->time_compress_data) / 1000.000; 1.2151 + double dr = (double)cpi->bytes * 8.0 / 1000.0 / time_encoded; 1.2152 + 1.2153 + if (cpi->b_calculate_psnr) 1.2154 + { 1.2155 + YV12_BUFFER_CONFIG *lst_yv12 = 1.2156 + &cpi->common.yv12_fb[cpi->common.lst_fb_idx]; 1.2157 + 1.2158 + if (cpi->oxcf.number_of_layers > 1) 1.2159 + { 1.2160 + int i; 1.2161 + 1.2162 + fprintf(f, "Layer\tBitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\t" 1.2163 + "GLPsnrP\tVPXSSIM\t\n"); 1.2164 + for (i=0; i<(int)cpi->oxcf.number_of_layers; i++) 1.2165 + { 1.2166 + double dr = (double)cpi->bytes_in_layer[i] * 1.2167 + 8.0 / 1000.0 / time_encoded; 1.2168 + double samples = 3.0 / 2 * cpi->frames_in_layer[i] * 1.2169 + lst_yv12->y_width * lst_yv12->y_height; 1.2170 + double total_psnr = vp8_mse2psnr(samples, 255.0, 1.2171 + cpi->total_error2[i]); 1.2172 + double total_psnr2 = vp8_mse2psnr(samples, 255.0, 1.2173 + cpi->total_error2_p[i]); 1.2174 + double total_ssim = 100 * pow(cpi->sum_ssim[i] / 1.2175 + cpi->sum_weights[i], 8.0); 1.2176 + 1.2177 + fprintf(f, "%5d\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t" 1.2178 + "%7.3f\t%7.3f\n", 1.2179 + i, dr, 1.2180 + cpi->sum_psnr[i] / cpi->frames_in_layer[i], 1.2181 + total_psnr, 1.2182 + cpi->sum_psnr_p[i] / cpi->frames_in_layer[i], 1.2183 + total_psnr2, total_ssim); 1.2184 + } 1.2185 + } 1.2186 + else 1.2187 + { 1.2188 + double samples = 3.0 / 2 * cpi->count * 1.2189 + lst_yv12->y_width * lst_yv12->y_height; 1.2190 + double total_psnr = vp8_mse2psnr(samples, 255.0, 1.2191 + cpi->total_sq_error); 1.2192 + double total_psnr2 = vp8_mse2psnr(samples, 255.0, 1.2193 + cpi->total_sq_error2); 1.2194 + double total_ssim = 100 * pow(cpi->summed_quality / 1.2195 + cpi->summed_weights, 8.0); 1.2196 + 1.2197 + fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\t" 1.2198 + "GLPsnrP\tVPXSSIM\t Time(us)\n"); 1.2199 + fprintf(f, "%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t" 1.2200 + "%7.3f\t%8.0f\n", 1.2201 + dr, cpi->total / cpi->count, total_psnr, 1.2202 + cpi->totalp / cpi->count, total_psnr2, 1.2203 + total_ssim, total_encode_time); 1.2204 + } 1.2205 + } 1.2206 + 1.2207 + if (cpi->b_calculate_ssimg) 1.2208 + { 1.2209 + if (cpi->oxcf.number_of_layers > 1) 1.2210 + { 1.2211 + int i; 1.2212 + 1.2213 + fprintf(f, "Layer\tBitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t" 1.2214 + "Time(us)\n"); 1.2215 + for (i=0; i<(int)cpi->oxcf.number_of_layers; i++) 1.2216 + { 1.2217 + double dr = (double)cpi->bytes_in_layer[i] * 1.2218 + 8.0 / 1000.0 / time_encoded; 1.2219 + fprintf(f, "%5d\t%7.3f\t%6.4f\t" 1.2220 + "%6.4f\t%6.4f\t%6.4f\t%8.0f\n", 1.2221 + i, dr, 1.2222 + cpi->total_ssimg_y_in_layer[i] / 1.2223 + cpi->frames_in_layer[i], 1.2224 + cpi->total_ssimg_u_in_layer[i] / 1.2225 + cpi->frames_in_layer[i], 1.2226 + cpi->total_ssimg_v_in_layer[i] / 1.2227 + cpi->frames_in_layer[i], 1.2228 + cpi->total_ssimg_all_in_layer[i] / 1.2229 + cpi->frames_in_layer[i], 1.2230 + total_encode_time); 1.2231 + } 1.2232 + } 1.2233 + else 1.2234 + { 1.2235 + fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t" 1.2236 + "Time(us)\n"); 1.2237 + fprintf(f, "%7.3f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr, 1.2238 + cpi->total_ssimg_y / cpi->count, 1.2239 + cpi->total_ssimg_u / cpi->count, 1.2240 + cpi->total_ssimg_v / cpi->count, 1.2241 + cpi->total_ssimg_all / cpi->count, total_encode_time); 1.2242 + } 1.2243 + } 1.2244 + 1.2245 + fclose(f); 1.2246 +#if 0 1.2247 + f = fopen("qskip.stt", "a"); 1.2248 + fprintf(f, "minq:%d -maxq:%d skiptrue:skipfalse = %d:%d\n", cpi->oxcf.best_allowed_q, cpi->oxcf.worst_allowed_q, skiptruecount, skipfalsecount); 1.2249 + fclose(f); 1.2250 +#endif 1.2251 + 1.2252 + } 1.2253 + 1.2254 +#endif 1.2255 + 1.2256 + 1.2257 +#ifdef SPEEDSTATS 1.2258 + 1.2259 + if (cpi->compressor_speed == 2) 1.2260 + { 1.2261 + int i; 1.2262 + FILE *f = fopen("cxspeed.stt", "a"); 1.2263 + cnt_pm /= cpi->common.MBs; 1.2264 + 1.2265 + for (i = 0; i < 16; i++) 1.2266 + fprintf(f, "%5d", frames_at_speed[i]); 1.2267 + 1.2268 + fprintf(f, "\n"); 1.2269 + fclose(f); 1.2270 + } 1.2271 + 1.2272 +#endif 1.2273 + 1.2274 + 1.2275 +#ifdef MODE_STATS 1.2276 + { 1.2277 + extern int count_mb_seg[4]; 1.2278 + FILE *f = fopen("modes.stt", "a"); 1.2279 + double dr = (double)cpi->framerate * (double)bytes * (double)8 / (double)count / (double)1000 ; 1.2280 + fprintf(f, "intra_mode in Intra Frames:\n"); 1.2281 + fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d\n", y_modes[0], y_modes[1], y_modes[2], y_modes[3], y_modes[4]); 1.2282 + fprintf(f, "UV:%8d, %8d, %8d, %8d\n", uv_modes[0], uv_modes[1], uv_modes[2], uv_modes[3]); 1.2283 + fprintf(f, "B: "); 1.2284 + { 1.2285 + int i; 1.2286 + 1.2287 + for (i = 0; i < 10; i++) 1.2288 + fprintf(f, "%8d, ", b_modes[i]); 1.2289 + 1.2290 + fprintf(f, "\n"); 1.2291 + 1.2292 + } 1.2293 + 1.2294 + fprintf(f, "Modes in Inter Frames:\n"); 1.2295 + fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d\n", 1.2296 + inter_y_modes[0], inter_y_modes[1], inter_y_modes[2], inter_y_modes[3], inter_y_modes[4], 1.2297 + inter_y_modes[5], inter_y_modes[6], inter_y_modes[7], inter_y_modes[8], inter_y_modes[9]); 1.2298 + fprintf(f, "UV:%8d, %8d, %8d, %8d\n", inter_uv_modes[0], inter_uv_modes[1], inter_uv_modes[2], inter_uv_modes[3]); 1.2299 + fprintf(f, "B: "); 1.2300 + { 1.2301 + int i; 1.2302 + 1.2303 + for (i = 0; i < 15; i++) 1.2304 + fprintf(f, "%8d, ", inter_b_modes[i]); 1.2305 + 1.2306 + fprintf(f, "\n"); 1.2307 + 1.2308 + } 1.2309 + fprintf(f, "P:%8d, %8d, %8d, %8d\n", count_mb_seg[0], count_mb_seg[1], count_mb_seg[2], count_mb_seg[3]); 1.2310 + fprintf(f, "PB:%8d, %8d, %8d, %8d\n", inter_b_modes[LEFT4X4], inter_b_modes[ABOVE4X4], inter_b_modes[ZERO4X4], inter_b_modes[NEW4X4]); 1.2311 + 1.2312 + 1.2313 + 1.2314 + fclose(f); 1.2315 + } 1.2316 +#endif 1.2317 + 1.2318 +#ifdef VP8_ENTROPY_STATS 1.2319 + { 1.2320 + int i, j, k; 1.2321 + FILE *fmode = fopen("modecontext.c", "w"); 1.2322 + 1.2323 + fprintf(fmode, "\n#include \"entropymode.h\"\n\n"); 1.2324 + fprintf(fmode, "const unsigned int vp8_kf_default_bmode_counts "); 1.2325 + fprintf(fmode, "[VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES] =\n{\n"); 1.2326 + 1.2327 + for (i = 0; i < 10; i++) 1.2328 + { 1.2329 + 1.2330 + fprintf(fmode, " { /* Above Mode : %d */\n", i); 1.2331 + 1.2332 + for (j = 0; j < 10; j++) 1.2333 + { 1.2334 + 1.2335 + fprintf(fmode, " {"); 1.2336 + 1.2337 + for (k = 0; k < 10; k++) 1.2338 + { 1.2339 + if (!intra_mode_stats[i][j][k]) 1.2340 + fprintf(fmode, " %5d, ", 1); 1.2341 + else 1.2342 + fprintf(fmode, " %5d, ", intra_mode_stats[i][j][k]); 1.2343 + } 1.2344 + 1.2345 + fprintf(fmode, "}, /* left_mode %d */\n", j); 1.2346 + 1.2347 + } 1.2348 + 1.2349 + fprintf(fmode, " },\n"); 1.2350 + 1.2351 + } 1.2352 + 1.2353 + fprintf(fmode, "};\n"); 1.2354 + fclose(fmode); 1.2355 + } 1.2356 +#endif 1.2357 + 1.2358 + 1.2359 +#if defined(SECTIONBITS_OUTPUT) 1.2360 + 1.2361 + if (0) 1.2362 + { 1.2363 + int i; 1.2364 + FILE *f = fopen("tokenbits.stt", "a"); 1.2365 + 1.2366 + for (i = 0; i < 28; i++) 1.2367 + fprintf(f, "%8d", (int)(Sectionbits[i] / 256)); 1.2368 + 1.2369 + fprintf(f, "\n"); 1.2370 + fclose(f); 1.2371 + } 1.2372 + 1.2373 +#endif 1.2374 + 1.2375 +#if 0 1.2376 + { 1.2377 + printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000); 1.2378 + printf("\n_frames recive_data encod_mb_row compress_frame Total\n"); 1.2379 + printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame, cpi->time_receive_data / 1000, cpi->time_encode_mb_row / 1000, cpi->time_compress_data / 1000, (cpi->time_receive_data + cpi->time_compress_data) / 1000); 1.2380 + } 1.2381 +#endif 1.2382 + 1.2383 + } 1.2384 + 1.2385 +#if CONFIG_MULTITHREAD 1.2386 + vp8cx_remove_encoder_threads(cpi); 1.2387 +#endif 1.2388 + 1.2389 +#if CONFIG_TEMPORAL_DENOISING 1.2390 + vp8_denoiser_free(&cpi->denoiser); 1.2391 +#endif 1.2392 + dealloc_compressor_data(cpi); 1.2393 + vpx_free(cpi->mb.ss); 1.2394 + vpx_free(cpi->tok); 1.2395 + vpx_free(cpi->cyclic_refresh_map); 1.2396 + 1.2397 + vp8_remove_common(&cpi->common); 1.2398 + vpx_free(cpi); 1.2399 + *ptr = 0; 1.2400 + 1.2401 +#ifdef OUTPUT_YUV_SRC 1.2402 + fclose(yuv_file); 1.2403 +#endif 1.2404 + 1.2405 +#if 0 1.2406 + 1.2407 + if (keyfile) 1.2408 + fclose(keyfile); 1.2409 + 1.2410 + if (framepsnr) 1.2411 + fclose(framepsnr); 1.2412 + 1.2413 + if (kf_list) 1.2414 + fclose(kf_list); 1.2415 + 1.2416 +#endif 1.2417 + 1.2418 +} 1.2419 + 1.2420 + 1.2421 +static uint64_t calc_plane_error(unsigned char *orig, int orig_stride, 1.2422 + unsigned char *recon, int recon_stride, 1.2423 + unsigned int cols, unsigned int rows) 1.2424 +{ 1.2425 + unsigned int row, col; 1.2426 + uint64_t total_sse = 0; 1.2427 + int diff; 1.2428 + 1.2429 + for (row = 0; row + 16 <= rows; row += 16) 1.2430 + { 1.2431 + for (col = 0; col + 16 <= cols; col += 16) 1.2432 + { 1.2433 + unsigned int sse; 1.2434 + 1.2435 + vp8_mse16x16(orig + col, orig_stride, 1.2436 + recon + col, recon_stride, 1.2437 + &sse); 1.2438 + total_sse += sse; 1.2439 + } 1.2440 + 1.2441 + /* Handle odd-sized width */ 1.2442 + if (col < cols) 1.2443 + { 1.2444 + unsigned int border_row, border_col; 1.2445 + unsigned char *border_orig = orig; 1.2446 + unsigned char *border_recon = recon; 1.2447 + 1.2448 + for (border_row = 0; border_row < 16; border_row++) 1.2449 + { 1.2450 + for (border_col = col; border_col < cols; border_col++) 1.2451 + { 1.2452 + diff = border_orig[border_col] - border_recon[border_col]; 1.2453 + total_sse += diff * diff; 1.2454 + } 1.2455 + 1.2456 + border_orig += orig_stride; 1.2457 + border_recon += recon_stride; 1.2458 + } 1.2459 + } 1.2460 + 1.2461 + orig += orig_stride * 16; 1.2462 + recon += recon_stride * 16; 1.2463 + } 1.2464 + 1.2465 + /* Handle odd-sized height */ 1.2466 + for (; row < rows; row++) 1.2467 + { 1.2468 + for (col = 0; col < cols; col++) 1.2469 + { 1.2470 + diff = orig[col] - recon[col]; 1.2471 + total_sse += diff * diff; 1.2472 + } 1.2473 + 1.2474 + orig += orig_stride; 1.2475 + recon += recon_stride; 1.2476 + } 1.2477 + 1.2478 + vp8_clear_system_state(); 1.2479 + return total_sse; 1.2480 +} 1.2481 + 1.2482 + 1.2483 +static void generate_psnr_packet(VP8_COMP *cpi) 1.2484 +{ 1.2485 + YV12_BUFFER_CONFIG *orig = cpi->Source; 1.2486 + YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show; 1.2487 + struct vpx_codec_cx_pkt pkt; 1.2488 + uint64_t sse; 1.2489 + int i; 1.2490 + unsigned int width = cpi->common.Width; 1.2491 + unsigned int height = cpi->common.Height; 1.2492 + 1.2493 + pkt.kind = VPX_CODEC_PSNR_PKT; 1.2494 + sse = calc_plane_error(orig->y_buffer, orig->y_stride, 1.2495 + recon->y_buffer, recon->y_stride, 1.2496 + width, height); 1.2497 + pkt.data.psnr.sse[0] = sse; 1.2498 + pkt.data.psnr.sse[1] = sse; 1.2499 + pkt.data.psnr.samples[0] = width * height; 1.2500 + pkt.data.psnr.samples[1] = width * height; 1.2501 + 1.2502 + width = (width + 1) / 2; 1.2503 + height = (height + 1) / 2; 1.2504 + 1.2505 + sse = calc_plane_error(orig->u_buffer, orig->uv_stride, 1.2506 + recon->u_buffer, recon->uv_stride, 1.2507 + width, height); 1.2508 + pkt.data.psnr.sse[0] += sse; 1.2509 + pkt.data.psnr.sse[2] = sse; 1.2510 + pkt.data.psnr.samples[0] += width * height; 1.2511 + pkt.data.psnr.samples[2] = width * height; 1.2512 + 1.2513 + sse = calc_plane_error(orig->v_buffer, orig->uv_stride, 1.2514 + recon->v_buffer, recon->uv_stride, 1.2515 + width, height); 1.2516 + pkt.data.psnr.sse[0] += sse; 1.2517 + pkt.data.psnr.sse[3] = sse; 1.2518 + pkt.data.psnr.samples[0] += width * height; 1.2519 + pkt.data.psnr.samples[3] = width * height; 1.2520 + 1.2521 + for (i = 0; i < 4; i++) 1.2522 + pkt.data.psnr.psnr[i] = vp8_mse2psnr(pkt.data.psnr.samples[i], 255.0, 1.2523 + (double)(pkt.data.psnr.sse[i])); 1.2524 + 1.2525 + vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt); 1.2526 +} 1.2527 + 1.2528 + 1.2529 +int vp8_use_as_reference(VP8_COMP *cpi, int ref_frame_flags) 1.2530 +{ 1.2531 + if (ref_frame_flags > 7) 1.2532 + return -1 ; 1.2533 + 1.2534 + cpi->ref_frame_flags = ref_frame_flags; 1.2535 + return 0; 1.2536 +} 1.2537 +int vp8_update_reference(VP8_COMP *cpi, int ref_frame_flags) 1.2538 +{ 1.2539 + if (ref_frame_flags > 7) 1.2540 + return -1 ; 1.2541 + 1.2542 + cpi->common.refresh_golden_frame = 0; 1.2543 + cpi->common.refresh_alt_ref_frame = 0; 1.2544 + cpi->common.refresh_last_frame = 0; 1.2545 + 1.2546 + if (ref_frame_flags & VP8_LAST_FRAME) 1.2547 + cpi->common.refresh_last_frame = 1; 1.2548 + 1.2549 + if (ref_frame_flags & VP8_GOLD_FRAME) 1.2550 + cpi->common.refresh_golden_frame = 1; 1.2551 + 1.2552 + if (ref_frame_flags & VP8_ALTR_FRAME) 1.2553 + cpi->common.refresh_alt_ref_frame = 1; 1.2554 + 1.2555 + return 0; 1.2556 +} 1.2557 + 1.2558 +int vp8_get_reference(VP8_COMP *cpi, enum vpx_ref_frame_type ref_frame_flag, YV12_BUFFER_CONFIG *sd) 1.2559 +{ 1.2560 + VP8_COMMON *cm = &cpi->common; 1.2561 + int ref_fb_idx; 1.2562 + 1.2563 + if (ref_frame_flag == VP8_LAST_FRAME) 1.2564 + ref_fb_idx = cm->lst_fb_idx; 1.2565 + else if (ref_frame_flag == VP8_GOLD_FRAME) 1.2566 + ref_fb_idx = cm->gld_fb_idx; 1.2567 + else if (ref_frame_flag == VP8_ALTR_FRAME) 1.2568 + ref_fb_idx = cm->alt_fb_idx; 1.2569 + else 1.2570 + return -1; 1.2571 + 1.2572 + vp8_yv12_copy_frame(&cm->yv12_fb[ref_fb_idx], sd); 1.2573 + 1.2574 + return 0; 1.2575 +} 1.2576 +int vp8_set_reference(VP8_COMP *cpi, enum vpx_ref_frame_type ref_frame_flag, YV12_BUFFER_CONFIG *sd) 1.2577 +{ 1.2578 + VP8_COMMON *cm = &cpi->common; 1.2579 + 1.2580 + int ref_fb_idx; 1.2581 + 1.2582 + if (ref_frame_flag == VP8_LAST_FRAME) 1.2583 + ref_fb_idx = cm->lst_fb_idx; 1.2584 + else if (ref_frame_flag == VP8_GOLD_FRAME) 1.2585 + ref_fb_idx = cm->gld_fb_idx; 1.2586 + else if (ref_frame_flag == VP8_ALTR_FRAME) 1.2587 + ref_fb_idx = cm->alt_fb_idx; 1.2588 + else 1.2589 + return -1; 1.2590 + 1.2591 + vp8_yv12_copy_frame(sd, &cm->yv12_fb[ref_fb_idx]); 1.2592 + 1.2593 + return 0; 1.2594 +} 1.2595 +int vp8_update_entropy(VP8_COMP *cpi, int update) 1.2596 +{ 1.2597 + VP8_COMMON *cm = &cpi->common; 1.2598 + cm->refresh_entropy_probs = update; 1.2599 + 1.2600 + return 0; 1.2601 +} 1.2602 + 1.2603 + 1.2604 +#if OUTPUT_YUV_SRC 1.2605 +void vp8_write_yuv_frame(const char *name, YV12_BUFFER_CONFIG *s) 1.2606 +{ 1.2607 + FILE *yuv_file = fopen(name, "ab"); 1.2608 + unsigned char *src = s->y_buffer; 1.2609 + int h = s->y_height; 1.2610 + 1.2611 + do 1.2612 + { 1.2613 + fwrite(src, s->y_width, 1, yuv_file); 1.2614 + src += s->y_stride; 1.2615 + } 1.2616 + while (--h); 1.2617 + 1.2618 + src = s->u_buffer; 1.2619 + h = s->uv_height; 1.2620 + 1.2621 + do 1.2622 + { 1.2623 + fwrite(src, s->uv_width, 1, yuv_file); 1.2624 + src += s->uv_stride; 1.2625 + } 1.2626 + while (--h); 1.2627 + 1.2628 + src = s->v_buffer; 1.2629 + h = s->uv_height; 1.2630 + 1.2631 + do 1.2632 + { 1.2633 + fwrite(src, s->uv_width, 1, yuv_file); 1.2634 + src += s->uv_stride; 1.2635 + } 1.2636 + while (--h); 1.2637 + 1.2638 + fclose(yuv_file); 1.2639 +} 1.2640 +#endif 1.2641 + 1.2642 + 1.2643 +static void scale_and_extend_source(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi) 1.2644 +{ 1.2645 + VP8_COMMON *cm = &cpi->common; 1.2646 + 1.2647 + /* are we resizing the image */ 1.2648 + if (cm->horiz_scale != 0 || cm->vert_scale != 0) 1.2649 + { 1.2650 +#if CONFIG_SPATIAL_RESAMPLING 1.2651 + int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs); 1.2652 + int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs); 1.2653 + int tmp_height; 1.2654 + 1.2655 + if (cm->vert_scale == 3) 1.2656 + tmp_height = 9; 1.2657 + else 1.2658 + tmp_height = 11; 1.2659 + 1.2660 + Scale2Ratio(cm->horiz_scale, &hr, &hs); 1.2661 + Scale2Ratio(cm->vert_scale, &vr, &vs); 1.2662 + 1.2663 + vpx_scale_frame(sd, &cpi->scaled_source, cm->temp_scale_frame.y_buffer, 1.2664 + tmp_height, hs, hr, vs, vr, 0); 1.2665 + 1.2666 + vp8_yv12_extend_frame_borders(&cpi->scaled_source); 1.2667 + cpi->Source = &cpi->scaled_source; 1.2668 +#endif 1.2669 + } 1.2670 + else 1.2671 + cpi->Source = sd; 1.2672 +} 1.2673 + 1.2674 + 1.2675 +static int resize_key_frame(VP8_COMP *cpi) 1.2676 +{ 1.2677 +#if CONFIG_SPATIAL_RESAMPLING 1.2678 + VP8_COMMON *cm = &cpi->common; 1.2679 + 1.2680 + /* Do we need to apply resampling for one pass cbr. 1.2681 + * In one pass this is more limited than in two pass cbr 1.2682 + * The test and any change is only made one per key frame sequence 1.2683 + */ 1.2684 + if (cpi->oxcf.allow_spatial_resampling && (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) 1.2685 + { 1.2686 + int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs); 1.2687 + int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs); 1.2688 + int new_width, new_height; 1.2689 + 1.2690 + /* If we are below the resample DOWN watermark then scale down a 1.2691 + * notch. 1.2692 + */ 1.2693 + if (cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100)) 1.2694 + { 1.2695 + cm->horiz_scale = (cm->horiz_scale < ONETWO) ? cm->horiz_scale + 1 : ONETWO; 1.2696 + cm->vert_scale = (cm->vert_scale < ONETWO) ? cm->vert_scale + 1 : ONETWO; 1.2697 + } 1.2698 + /* Should we now start scaling back up */ 1.2699 + else if (cpi->buffer_level > (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100)) 1.2700 + { 1.2701 + cm->horiz_scale = (cm->horiz_scale > NORMAL) ? cm->horiz_scale - 1 : NORMAL; 1.2702 + cm->vert_scale = (cm->vert_scale > NORMAL) ? cm->vert_scale - 1 : NORMAL; 1.2703 + } 1.2704 + 1.2705 + /* Get the new hieght and width */ 1.2706 + Scale2Ratio(cm->horiz_scale, &hr, &hs); 1.2707 + Scale2Ratio(cm->vert_scale, &vr, &vs); 1.2708 + new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs; 1.2709 + new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs; 1.2710 + 1.2711 + /* If the image size has changed we need to reallocate the buffers 1.2712 + * and resample the source image 1.2713 + */ 1.2714 + if ((cm->Width != new_width) || (cm->Height != new_height)) 1.2715 + { 1.2716 + cm->Width = new_width; 1.2717 + cm->Height = new_height; 1.2718 + vp8_alloc_compressor_data(cpi); 1.2719 + scale_and_extend_source(cpi->un_scaled_source, cpi); 1.2720 + return 1; 1.2721 + } 1.2722 + } 1.2723 + 1.2724 +#endif 1.2725 + return 0; 1.2726 +} 1.2727 + 1.2728 + 1.2729 +static void update_alt_ref_frame_stats(VP8_COMP *cpi) 1.2730 +{ 1.2731 + VP8_COMMON *cm = &cpi->common; 1.2732 + 1.2733 + /* Select an interval before next GF or altref */ 1.2734 + if (!cpi->auto_gold) 1.2735 + cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL; 1.2736 + 1.2737 + if ((cpi->pass != 2) && cpi->frames_till_gf_update_due) 1.2738 + { 1.2739 + cpi->current_gf_interval = cpi->frames_till_gf_update_due; 1.2740 + 1.2741 + /* Set the bits per frame that we should try and recover in 1.2742 + * subsequent inter frames to account for the extra GF spend... 1.2743 + * note that his does not apply for GF updates that occur 1.2744 + * coincident with a key frame as the extra cost of key frames is 1.2745 + * dealt with elsewhere. 1.2746 + */ 1.2747 + cpi->gf_overspend_bits += cpi->projected_frame_size; 1.2748 + cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due; 1.2749 + } 1.2750 + 1.2751 + /* Update data structure that monitors level of reference to last GF */ 1.2752 + vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols)); 1.2753 + cpi->gf_active_count = cm->mb_rows * cm->mb_cols; 1.2754 + 1.2755 + /* this frame refreshes means next frames don't unless specified by user */ 1.2756 + cpi->frames_since_golden = 0; 1.2757 + 1.2758 + /* Clear the alternate reference update pending flag. */ 1.2759 + cpi->source_alt_ref_pending = 0; 1.2760 + 1.2761 + /* Set the alternate reference frame active flag */ 1.2762 + cpi->source_alt_ref_active = 1; 1.2763 + 1.2764 + 1.2765 +} 1.2766 +static void update_golden_frame_stats(VP8_COMP *cpi) 1.2767 +{ 1.2768 + VP8_COMMON *cm = &cpi->common; 1.2769 + 1.2770 + /* Update the Golden frame usage counts. */ 1.2771 + if (cm->refresh_golden_frame) 1.2772 + { 1.2773 + /* Select an interval before next GF */ 1.2774 + if (!cpi->auto_gold) 1.2775 + cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL; 1.2776 + 1.2777 + if ((cpi->pass != 2) && (cpi->frames_till_gf_update_due > 0)) 1.2778 + { 1.2779 + cpi->current_gf_interval = cpi->frames_till_gf_update_due; 1.2780 + 1.2781 + /* Set the bits per frame that we should try and recover in 1.2782 + * subsequent inter frames to account for the extra GF spend... 1.2783 + * note that his does not apply for GF updates that occur 1.2784 + * coincident with a key frame as the extra cost of key frames 1.2785 + * is dealt with elsewhere. 1.2786 + */ 1.2787 + if ((cm->frame_type != KEY_FRAME) && !cpi->source_alt_ref_active) 1.2788 + { 1.2789 + /* Calcluate GF bits to be recovered 1.2790 + * Projected size - av frame bits available for inter 1.2791 + * frames for clip as a whole 1.2792 + */ 1.2793 + cpi->gf_overspend_bits += (cpi->projected_frame_size - cpi->inter_frame_target); 1.2794 + } 1.2795 + 1.2796 + cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due; 1.2797 + 1.2798 + } 1.2799 + 1.2800 + /* Update data structure that monitors level of reference to last GF */ 1.2801 + vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols)); 1.2802 + cpi->gf_active_count = cm->mb_rows * cm->mb_cols; 1.2803 + 1.2804 + /* this frame refreshes means next frames don't unless specified by 1.2805 + * user 1.2806 + */ 1.2807 + cm->refresh_golden_frame = 0; 1.2808 + cpi->frames_since_golden = 0; 1.2809 + 1.2810 + cpi->recent_ref_frame_usage[INTRA_FRAME] = 1; 1.2811 + cpi->recent_ref_frame_usage[LAST_FRAME] = 1; 1.2812 + cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1; 1.2813 + cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1; 1.2814 + 1.2815 + /* ******** Fixed Q test code only ************ */ 1.2816 + /* If we are going to use the ALT reference for the next group of 1.2817 + * frames set a flag to say so. 1.2818 + */ 1.2819 + if (cpi->oxcf.fixed_q >= 0 && 1.2820 + cpi->oxcf.play_alternate && !cpi->common.refresh_alt_ref_frame) 1.2821 + { 1.2822 + cpi->source_alt_ref_pending = 1; 1.2823 + cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; 1.2824 + } 1.2825 + 1.2826 + if (!cpi->source_alt_ref_pending) 1.2827 + cpi->source_alt_ref_active = 0; 1.2828 + 1.2829 + /* Decrement count down till next gf */ 1.2830 + if (cpi->frames_till_gf_update_due > 0) 1.2831 + cpi->frames_till_gf_update_due--; 1.2832 + 1.2833 + } 1.2834 + else if (!cpi->common.refresh_alt_ref_frame) 1.2835 + { 1.2836 + /* Decrement count down till next gf */ 1.2837 + if (cpi->frames_till_gf_update_due > 0) 1.2838 + cpi->frames_till_gf_update_due--; 1.2839 + 1.2840 + if (cpi->frames_till_alt_ref_frame) 1.2841 + cpi->frames_till_alt_ref_frame --; 1.2842 + 1.2843 + cpi->frames_since_golden ++; 1.2844 + 1.2845 + if (cpi->frames_since_golden > 1) 1.2846 + { 1.2847 + cpi->recent_ref_frame_usage[INTRA_FRAME] += 1.2848 + cpi->mb.count_mb_ref_frame_usage[INTRA_FRAME]; 1.2849 + cpi->recent_ref_frame_usage[LAST_FRAME] += 1.2850 + cpi->mb.count_mb_ref_frame_usage[LAST_FRAME]; 1.2851 + cpi->recent_ref_frame_usage[GOLDEN_FRAME] += 1.2852 + cpi->mb.count_mb_ref_frame_usage[GOLDEN_FRAME]; 1.2853 + cpi->recent_ref_frame_usage[ALTREF_FRAME] += 1.2854 + cpi->mb.count_mb_ref_frame_usage[ALTREF_FRAME]; 1.2855 + } 1.2856 + } 1.2857 +} 1.2858 + 1.2859 +/* This function updates the reference frame probability estimates that 1.2860 + * will be used during mode selection 1.2861 + */ 1.2862 +static void update_rd_ref_frame_probs(VP8_COMP *cpi) 1.2863 +{ 1.2864 + VP8_COMMON *cm = &cpi->common; 1.2865 + 1.2866 + const int *const rfct = cpi->mb.count_mb_ref_frame_usage; 1.2867 + const int rf_intra = rfct[INTRA_FRAME]; 1.2868 + const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME]; 1.2869 + 1.2870 + if (cm->frame_type == KEY_FRAME) 1.2871 + { 1.2872 + cpi->prob_intra_coded = 255; 1.2873 + cpi->prob_last_coded = 128; 1.2874 + cpi->prob_gf_coded = 128; 1.2875 + } 1.2876 + else if (!(rf_intra + rf_inter)) 1.2877 + { 1.2878 + cpi->prob_intra_coded = 63; 1.2879 + cpi->prob_last_coded = 128; 1.2880 + cpi->prob_gf_coded = 128; 1.2881 + } 1.2882 + 1.2883 + /* update reference frame costs since we can do better than what we got 1.2884 + * last frame. 1.2885 + */ 1.2886 + if (cpi->oxcf.number_of_layers == 1) 1.2887 + { 1.2888 + if (cpi->common.refresh_alt_ref_frame) 1.2889 + { 1.2890 + cpi->prob_intra_coded += 40; 1.2891 + if (cpi->prob_intra_coded > 255) 1.2892 + cpi->prob_intra_coded = 255; 1.2893 + cpi->prob_last_coded = 200; 1.2894 + cpi->prob_gf_coded = 1; 1.2895 + } 1.2896 + else if (cpi->frames_since_golden == 0) 1.2897 + { 1.2898 + cpi->prob_last_coded = 214; 1.2899 + } 1.2900 + else if (cpi->frames_since_golden == 1) 1.2901 + { 1.2902 + cpi->prob_last_coded = 192; 1.2903 + cpi->prob_gf_coded = 220; 1.2904 + } 1.2905 + else if (cpi->source_alt_ref_active) 1.2906 + { 1.2907 + cpi->prob_gf_coded -= 20; 1.2908 + 1.2909 + if (cpi->prob_gf_coded < 10) 1.2910 + cpi->prob_gf_coded = 10; 1.2911 + } 1.2912 + if (!cpi->source_alt_ref_active) 1.2913 + cpi->prob_gf_coded = 255; 1.2914 + } 1.2915 +} 1.2916 + 1.2917 + 1.2918 +/* 1 = key, 0 = inter */ 1.2919 +static int decide_key_frame(VP8_COMP *cpi) 1.2920 +{ 1.2921 + VP8_COMMON *cm = &cpi->common; 1.2922 + 1.2923 + int code_key_frame = 0; 1.2924 + 1.2925 + cpi->kf_boost = 0; 1.2926 + 1.2927 + if (cpi->Speed > 11) 1.2928 + return 0; 1.2929 + 1.2930 + /* Clear down mmx registers */ 1.2931 + vp8_clear_system_state(); 1.2932 + 1.2933 + if ((cpi->compressor_speed == 2) && (cpi->Speed >= 5) && (cpi->sf.RD == 0)) 1.2934 + { 1.2935 + double change = 1.0 * abs((int)(cpi->mb.intra_error - 1.2936 + cpi->last_intra_error)) / (1 + cpi->last_intra_error); 1.2937 + double change2 = 1.0 * abs((int)(cpi->mb.prediction_error - 1.2938 + cpi->last_prediction_error)) / (1 + cpi->last_prediction_error); 1.2939 + double minerror = cm->MBs * 256; 1.2940 + 1.2941 + cpi->last_intra_error = cpi->mb.intra_error; 1.2942 + cpi->last_prediction_error = cpi->mb.prediction_error; 1.2943 + 1.2944 + if (10 * cpi->mb.intra_error / (1 + cpi->mb.prediction_error) < 15 1.2945 + && cpi->mb.prediction_error > minerror 1.2946 + && (change > .25 || change2 > .25)) 1.2947 + { 1.2948 + /*(change > 1.4 || change < .75)&& cpi->this_frame_percent_intra > cpi->last_frame_percent_intra + 3*/ 1.2949 + return 1; 1.2950 + } 1.2951 + 1.2952 + return 0; 1.2953 + 1.2954 + } 1.2955 + 1.2956 + /* If the following are true we might as well code a key frame */ 1.2957 + if (((cpi->this_frame_percent_intra == 100) && 1.2958 + (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 2))) || 1.2959 + ((cpi->this_frame_percent_intra > 95) && 1.2960 + (cpi->this_frame_percent_intra >= (cpi->last_frame_percent_intra + 5)))) 1.2961 + { 1.2962 + code_key_frame = 1; 1.2963 + } 1.2964 + /* in addition if the following are true and this is not a golden frame 1.2965 + * then code a key frame Note that on golden frames there often seems 1.2966 + * to be a pop in intra useage anyway hence this restriction is 1.2967 + * designed to prevent spurious key frames. The Intra pop needs to be 1.2968 + * investigated. 1.2969 + */ 1.2970 + else if (((cpi->this_frame_percent_intra > 60) && 1.2971 + (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 2))) || 1.2972 + ((cpi->this_frame_percent_intra > 75) && 1.2973 + (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 3 / 2))) || 1.2974 + ((cpi->this_frame_percent_intra > 90) && 1.2975 + (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 10)))) 1.2976 + { 1.2977 + if (!cm->refresh_golden_frame) 1.2978 + code_key_frame = 1; 1.2979 + } 1.2980 + 1.2981 + return code_key_frame; 1.2982 + 1.2983 +} 1.2984 + 1.2985 +#if !(CONFIG_REALTIME_ONLY) 1.2986 +static void Pass1Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags) 1.2987 +{ 1.2988 + (void) size; 1.2989 + (void) dest; 1.2990 + (void) frame_flags; 1.2991 + vp8_set_quantizer(cpi, 26); 1.2992 + 1.2993 + vp8_first_pass(cpi); 1.2994 +} 1.2995 +#endif 1.2996 + 1.2997 +#if 0 1.2998 +void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame) 1.2999 +{ 1.3000 + 1.3001 + /* write the frame */ 1.3002 + FILE *yframe; 1.3003 + int i; 1.3004 + char filename[255]; 1.3005 + 1.3006 + sprintf(filename, "cx\\y%04d.raw", this_frame); 1.3007 + yframe = fopen(filename, "wb"); 1.3008 + 1.3009 + for (i = 0; i < frame->y_height; i++) 1.3010 + fwrite(frame->y_buffer + i * frame->y_stride, frame->y_width, 1, yframe); 1.3011 + 1.3012 + fclose(yframe); 1.3013 + sprintf(filename, "cx\\u%04d.raw", this_frame); 1.3014 + yframe = fopen(filename, "wb"); 1.3015 + 1.3016 + for (i = 0; i < frame->uv_height; i++) 1.3017 + fwrite(frame->u_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe); 1.3018 + 1.3019 + fclose(yframe); 1.3020 + sprintf(filename, "cx\\v%04d.raw", this_frame); 1.3021 + yframe = fopen(filename, "wb"); 1.3022 + 1.3023 + for (i = 0; i < frame->uv_height; i++) 1.3024 + fwrite(frame->v_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe); 1.3025 + 1.3026 + fclose(yframe); 1.3027 +} 1.3028 +#endif 1.3029 +/* return of 0 means drop frame */ 1.3030 + 1.3031 +/* Function to test for conditions that indeicate we should loop 1.3032 + * back and recode a frame. 1.3033 + */ 1.3034 +static int recode_loop_test( VP8_COMP *cpi, 1.3035 + int high_limit, int low_limit, 1.3036 + int q, int maxq, int minq ) 1.3037 +{ 1.3038 + int force_recode = 0; 1.3039 + VP8_COMMON *cm = &cpi->common; 1.3040 + 1.3041 + /* Is frame recode allowed at all 1.3042 + * Yes if either recode mode 1 is selected or mode two is selcted 1.3043 + * and the frame is a key frame. golden frame or alt_ref_frame 1.3044 + */ 1.3045 + if ( (cpi->sf.recode_loop == 1) || 1.3046 + ( (cpi->sf.recode_loop == 2) && 1.3047 + ( (cm->frame_type == KEY_FRAME) || 1.3048 + cm->refresh_golden_frame || 1.3049 + cm->refresh_alt_ref_frame ) ) ) 1.3050 + { 1.3051 + /* General over and under shoot tests */ 1.3052 + if ( ((cpi->projected_frame_size > high_limit) && (q < maxq)) || 1.3053 + ((cpi->projected_frame_size < low_limit) && (q > minq)) ) 1.3054 + { 1.3055 + force_recode = 1; 1.3056 + } 1.3057 + /* Special Constrained quality tests */ 1.3058 + else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) 1.3059 + { 1.3060 + /* Undershoot and below auto cq level */ 1.3061 + if ( (q > cpi->cq_target_quality) && 1.3062 + (cpi->projected_frame_size < 1.3063 + ((cpi->this_frame_target * 7) >> 3))) 1.3064 + { 1.3065 + force_recode = 1; 1.3066 + } 1.3067 + /* Severe undershoot and between auto and user cq level */ 1.3068 + else if ( (q > cpi->oxcf.cq_level) && 1.3069 + (cpi->projected_frame_size < cpi->min_frame_bandwidth) && 1.3070 + (cpi->active_best_quality > cpi->oxcf.cq_level)) 1.3071 + { 1.3072 + force_recode = 1; 1.3073 + cpi->active_best_quality = cpi->oxcf.cq_level; 1.3074 + } 1.3075 + } 1.3076 + } 1.3077 + 1.3078 + return force_recode; 1.3079 +} 1.3080 + 1.3081 +static void update_reference_frames(VP8_COMP *cpi) 1.3082 +{ 1.3083 + VP8_COMMON *cm = &cpi->common; 1.3084 + YV12_BUFFER_CONFIG *yv12_fb = cm->yv12_fb; 1.3085 + 1.3086 + /* At this point the new frame has been encoded. 1.3087 + * If any buffer copy / swapping is signaled it should be done here. 1.3088 + */ 1.3089 + 1.3090 + if (cm->frame_type == KEY_FRAME) 1.3091 + { 1.3092 + yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FRAME | VP8_ALTR_FRAME ; 1.3093 + 1.3094 + yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME; 1.3095 + yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME; 1.3096 + 1.3097 + cm->alt_fb_idx = cm->gld_fb_idx = cm->new_fb_idx; 1.3098 + 1.3099 +#if CONFIG_MULTI_RES_ENCODING 1.3100 + cpi->current_ref_frames[GOLDEN_FRAME] = cm->current_video_frame; 1.3101 + cpi->current_ref_frames[ALTREF_FRAME] = cm->current_video_frame; 1.3102 +#endif 1.3103 + } 1.3104 + else /* For non key frames */ 1.3105 + { 1.3106 + if (cm->refresh_alt_ref_frame) 1.3107 + { 1.3108 + assert(!cm->copy_buffer_to_arf); 1.3109 + 1.3110 + cm->yv12_fb[cm->new_fb_idx].flags |= VP8_ALTR_FRAME; 1.3111 + cm->yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME; 1.3112 + cm->alt_fb_idx = cm->new_fb_idx; 1.3113 + 1.3114 +#if CONFIG_MULTI_RES_ENCODING 1.3115 + cpi->current_ref_frames[ALTREF_FRAME] = cm->current_video_frame; 1.3116 +#endif 1.3117 + } 1.3118 + else if (cm->copy_buffer_to_arf) 1.3119 + { 1.3120 + assert(!(cm->copy_buffer_to_arf & ~0x3)); 1.3121 + 1.3122 + if (cm->copy_buffer_to_arf == 1) 1.3123 + { 1.3124 + if(cm->alt_fb_idx != cm->lst_fb_idx) 1.3125 + { 1.3126 + yv12_fb[cm->lst_fb_idx].flags |= VP8_ALTR_FRAME; 1.3127 + yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME; 1.3128 + cm->alt_fb_idx = cm->lst_fb_idx; 1.3129 + 1.3130 +#if CONFIG_MULTI_RES_ENCODING 1.3131 + cpi->current_ref_frames[ALTREF_FRAME] = 1.3132 + cpi->current_ref_frames[LAST_FRAME]; 1.3133 +#endif 1.3134 + } 1.3135 + } 1.3136 + else /* if (cm->copy_buffer_to_arf == 2) */ 1.3137 + { 1.3138 + if(cm->alt_fb_idx != cm->gld_fb_idx) 1.3139 + { 1.3140 + yv12_fb[cm->gld_fb_idx].flags |= VP8_ALTR_FRAME; 1.3141 + yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME; 1.3142 + cm->alt_fb_idx = cm->gld_fb_idx; 1.3143 + 1.3144 +#if CONFIG_MULTI_RES_ENCODING 1.3145 + cpi->current_ref_frames[ALTREF_FRAME] = 1.3146 + cpi->current_ref_frames[GOLDEN_FRAME]; 1.3147 +#endif 1.3148 + } 1.3149 + } 1.3150 + } 1.3151 + 1.3152 + if (cm->refresh_golden_frame) 1.3153 + { 1.3154 + assert(!cm->copy_buffer_to_gf); 1.3155 + 1.3156 + cm->yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FRAME; 1.3157 + cm->yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME; 1.3158 + cm->gld_fb_idx = cm->new_fb_idx; 1.3159 + 1.3160 +#if CONFIG_MULTI_RES_ENCODING 1.3161 + cpi->current_ref_frames[GOLDEN_FRAME] = cm->current_video_frame; 1.3162 +#endif 1.3163 + } 1.3164 + else if (cm->copy_buffer_to_gf) 1.3165 + { 1.3166 + assert(!(cm->copy_buffer_to_arf & ~0x3)); 1.3167 + 1.3168 + if (cm->copy_buffer_to_gf == 1) 1.3169 + { 1.3170 + if(cm->gld_fb_idx != cm->lst_fb_idx) 1.3171 + { 1.3172 + yv12_fb[cm->lst_fb_idx].flags |= VP8_GOLD_FRAME; 1.3173 + yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME; 1.3174 + cm->gld_fb_idx = cm->lst_fb_idx; 1.3175 + 1.3176 +#if CONFIG_MULTI_RES_ENCODING 1.3177 + cpi->current_ref_frames[GOLDEN_FRAME] = 1.3178 + cpi->current_ref_frames[LAST_FRAME]; 1.3179 +#endif 1.3180 + } 1.3181 + } 1.3182 + else /* if (cm->copy_buffer_to_gf == 2) */ 1.3183 + { 1.3184 + if(cm->alt_fb_idx != cm->gld_fb_idx) 1.3185 + { 1.3186 + yv12_fb[cm->alt_fb_idx].flags |= VP8_GOLD_FRAME; 1.3187 + yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME; 1.3188 + cm->gld_fb_idx = cm->alt_fb_idx; 1.3189 + 1.3190 +#if CONFIG_MULTI_RES_ENCODING 1.3191 + cpi->current_ref_frames[GOLDEN_FRAME] = 1.3192 + cpi->current_ref_frames[ALTREF_FRAME]; 1.3193 +#endif 1.3194 + } 1.3195 + } 1.3196 + } 1.3197 + } 1.3198 + 1.3199 + if (cm->refresh_last_frame) 1.3200 + { 1.3201 + cm->yv12_fb[cm->new_fb_idx].flags |= VP8_LAST_FRAME; 1.3202 + cm->yv12_fb[cm->lst_fb_idx].flags &= ~VP8_LAST_FRAME; 1.3203 + cm->lst_fb_idx = cm->new_fb_idx; 1.3204 + 1.3205 +#if CONFIG_MULTI_RES_ENCODING 1.3206 + cpi->current_ref_frames[LAST_FRAME] = cm->current_video_frame; 1.3207 +#endif 1.3208 + } 1.3209 + 1.3210 +#if CONFIG_TEMPORAL_DENOISING 1.3211 + if (cpi->oxcf.noise_sensitivity) 1.3212 + { 1.3213 + /* we shouldn't have to keep multiple copies as we know in advance which 1.3214 + * buffer we should start - for now to get something up and running 1.3215 + * I've chosen to copy the buffers 1.3216 + */ 1.3217 + if (cm->frame_type == KEY_FRAME) 1.3218 + { 1.3219 + int i; 1.3220 + vp8_yv12_copy_frame( 1.3221 + cpi->Source, 1.3222 + &cpi->denoiser.yv12_running_avg[LAST_FRAME]); 1.3223 + 1.3224 + vp8_yv12_extend_frame_borders( 1.3225 + &cpi->denoiser.yv12_running_avg[LAST_FRAME]); 1.3226 + 1.3227 + for (i = 2; i < MAX_REF_FRAMES - 1; i++) 1.3228 + vp8_yv12_copy_frame( 1.3229 + &cpi->denoiser.yv12_running_avg[LAST_FRAME], 1.3230 + &cpi->denoiser.yv12_running_avg[i]); 1.3231 + } 1.3232 + else /* For non key frames */ 1.3233 + { 1.3234 + vp8_yv12_extend_frame_borders( 1.3235 + &cpi->denoiser.yv12_running_avg[INTRA_FRAME]); 1.3236 + 1.3237 + if (cm->refresh_alt_ref_frame || cm->copy_buffer_to_arf) 1.3238 + { 1.3239 + vp8_yv12_copy_frame( 1.3240 + &cpi->denoiser.yv12_running_avg[INTRA_FRAME], 1.3241 + &cpi->denoiser.yv12_running_avg[ALTREF_FRAME]); 1.3242 + } 1.3243 + if (cm->refresh_golden_frame || cm->copy_buffer_to_gf) 1.3244 + { 1.3245 + vp8_yv12_copy_frame( 1.3246 + &cpi->denoiser.yv12_running_avg[INTRA_FRAME], 1.3247 + &cpi->denoiser.yv12_running_avg[GOLDEN_FRAME]); 1.3248 + } 1.3249 + if(cm->refresh_last_frame) 1.3250 + { 1.3251 + vp8_yv12_copy_frame( 1.3252 + &cpi->denoiser.yv12_running_avg[INTRA_FRAME], 1.3253 + &cpi->denoiser.yv12_running_avg[LAST_FRAME]); 1.3254 + } 1.3255 + } 1.3256 + 1.3257 + } 1.3258 +#endif 1.3259 + 1.3260 +} 1.3261 + 1.3262 +void vp8_loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm) 1.3263 +{ 1.3264 + const FRAME_TYPE frame_type = cm->frame_type; 1.3265 + 1.3266 + if (cm->no_lpf) 1.3267 + { 1.3268 + cm->filter_level = 0; 1.3269 + } 1.3270 + else 1.3271 + { 1.3272 + struct vpx_usec_timer timer; 1.3273 + 1.3274 + vp8_clear_system_state(); 1.3275 + 1.3276 + vpx_usec_timer_start(&timer); 1.3277 + if (cpi->sf.auto_filter == 0) 1.3278 + vp8cx_pick_filter_level_fast(cpi->Source, cpi); 1.3279 + 1.3280 + else 1.3281 + vp8cx_pick_filter_level(cpi->Source, cpi); 1.3282 + 1.3283 + if (cm->filter_level > 0) 1.3284 + { 1.3285 + vp8cx_set_alt_lf_level(cpi, cm->filter_level); 1.3286 + } 1.3287 + 1.3288 + vpx_usec_timer_mark(&timer); 1.3289 + cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer); 1.3290 + } 1.3291 + 1.3292 +#if CONFIG_MULTITHREAD 1.3293 + if (cpi->b_multi_threaded) 1.3294 + sem_post(&cpi->h_event_end_lpf); /* signal that we have set filter_level */ 1.3295 +#endif 1.3296 + 1.3297 + if (cm->filter_level > 0) 1.3298 + { 1.3299 + vp8_loop_filter_frame(cm, &cpi->mb.e_mbd, frame_type); 1.3300 + } 1.3301 + 1.3302 + vp8_yv12_extend_frame_borders(cm->frame_to_show); 1.3303 + 1.3304 +} 1.3305 + 1.3306 +static void encode_frame_to_data_rate 1.3307 +( 1.3308 + VP8_COMP *cpi, 1.3309 + unsigned long *size, 1.3310 + unsigned char *dest, 1.3311 + unsigned char* dest_end, 1.3312 + unsigned int *frame_flags 1.3313 +) 1.3314 +{ 1.3315 + int Q; 1.3316 + int frame_over_shoot_limit; 1.3317 + int frame_under_shoot_limit; 1.3318 + 1.3319 + int Loop = 0; 1.3320 + int loop_count; 1.3321 + 1.3322 + VP8_COMMON *cm = &cpi->common; 1.3323 + int active_worst_qchanged = 0; 1.3324 + 1.3325 +#if !(CONFIG_REALTIME_ONLY) 1.3326 + int q_low; 1.3327 + int q_high; 1.3328 + int zbin_oq_high; 1.3329 + int zbin_oq_low = 0; 1.3330 + int top_index; 1.3331 + int bottom_index; 1.3332 + int overshoot_seen = 0; 1.3333 + int undershoot_seen = 0; 1.3334 +#endif 1.3335 + 1.3336 + int drop_mark = (int)(cpi->oxcf.drop_frames_water_mark * 1.3337 + cpi->oxcf.optimal_buffer_level / 100); 1.3338 + int drop_mark75 = drop_mark * 2 / 3; 1.3339 + int drop_mark50 = drop_mark / 4; 1.3340 + int drop_mark25 = drop_mark / 8; 1.3341 + 1.3342 + 1.3343 + /* Clear down mmx registers to allow floating point in what follows */ 1.3344 + vp8_clear_system_state(); 1.3345 + 1.3346 +#if CONFIG_MULTITHREAD 1.3347 + /* wait for the last picture loopfilter thread done */ 1.3348 + if (cpi->b_lpf_running) 1.3349 + { 1.3350 + sem_wait(&cpi->h_event_end_lpf); 1.3351 + cpi->b_lpf_running = 0; 1.3352 + } 1.3353 +#endif 1.3354 + 1.3355 + if(cpi->force_next_frame_intra) 1.3356 + { 1.3357 + cm->frame_type = KEY_FRAME; /* delayed intra frame */ 1.3358 + cpi->force_next_frame_intra = 0; 1.3359 + } 1.3360 + 1.3361 + /* For an alt ref frame in 2 pass we skip the call to the second pass 1.3362 + * function that sets the target bandwidth 1.3363 + */ 1.3364 +#if !(CONFIG_REALTIME_ONLY) 1.3365 + 1.3366 + if (cpi->pass == 2) 1.3367 + { 1.3368 + if (cpi->common.refresh_alt_ref_frame) 1.3369 + { 1.3370 + /* Per frame bit target for the alt ref frame */ 1.3371 + cpi->per_frame_bandwidth = cpi->twopass.gf_bits; 1.3372 + /* per second target bitrate */ 1.3373 + cpi->target_bandwidth = (int)(cpi->twopass.gf_bits * 1.3374 + cpi->output_framerate); 1.3375 + } 1.3376 + } 1.3377 + else 1.3378 +#endif 1.3379 + cpi->per_frame_bandwidth = (int)(cpi->target_bandwidth / cpi->output_framerate); 1.3380 + 1.3381 + /* Default turn off buffer to buffer copying */ 1.3382 + cm->copy_buffer_to_gf = 0; 1.3383 + cm->copy_buffer_to_arf = 0; 1.3384 + 1.3385 + /* Clear zbin over-quant value and mode boost values. */ 1.3386 + cpi->mb.zbin_over_quant = 0; 1.3387 + cpi->mb.zbin_mode_boost = 0; 1.3388 + 1.3389 + /* Enable or disable mode based tweaking of the zbin 1.3390 + * For 2 Pass Only used where GF/ARF prediction quality 1.3391 + * is above a threshold 1.3392 + */ 1.3393 + cpi->mb.zbin_mode_boost_enabled = 1; 1.3394 + if (cpi->pass == 2) 1.3395 + { 1.3396 + if ( cpi->gfu_boost <= 400 ) 1.3397 + { 1.3398 + cpi->mb.zbin_mode_boost_enabled = 0; 1.3399 + } 1.3400 + } 1.3401 + 1.3402 + /* Current default encoder behaviour for the altref sign bias */ 1.3403 + if (cpi->source_alt_ref_active) 1.3404 + cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1; 1.3405 + else 1.3406 + cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 0; 1.3407 + 1.3408 + /* Check to see if a key frame is signaled 1.3409 + * For two pass with auto key frame enabled cm->frame_type may already 1.3410 + * be set, but not for one pass. 1.3411 + */ 1.3412 + if ((cm->current_video_frame == 0) || 1.3413 + (cm->frame_flags & FRAMEFLAGS_KEY) || 1.3414 + (cpi->oxcf.auto_key && (cpi->frames_since_key % cpi->key_frame_frequency == 0))) 1.3415 + { 1.3416 + /* Key frame from VFW/auto-keyframe/first frame */ 1.3417 + cm->frame_type = KEY_FRAME; 1.3418 + } 1.3419 + 1.3420 +#if CONFIG_MULTI_RES_ENCODING 1.3421 + /* In multi-resolution encoding, frame_type is decided by lowest-resolution 1.3422 + * encoder. Same frame_type is adopted while encoding at other resolution. 1.3423 + */ 1.3424 + if (cpi->oxcf.mr_encoder_id) 1.3425 + { 1.3426 + LOWER_RES_FRAME_INFO* low_res_frame_info 1.3427 + = (LOWER_RES_FRAME_INFO*)cpi->oxcf.mr_low_res_mode_info; 1.3428 + 1.3429 + cm->frame_type = low_res_frame_info->frame_type; 1.3430 + 1.3431 + if(cm->frame_type != KEY_FRAME) 1.3432 + { 1.3433 + cpi->mr_low_res_mv_avail = 1; 1.3434 + cpi->mr_low_res_mv_avail &= !(low_res_frame_info->is_frame_dropped); 1.3435 + 1.3436 + if (cpi->ref_frame_flags & VP8_LAST_FRAME) 1.3437 + cpi->mr_low_res_mv_avail &= (cpi->current_ref_frames[LAST_FRAME] 1.3438 + == low_res_frame_info->low_res_ref_frames[LAST_FRAME]); 1.3439 + 1.3440 + if (cpi->ref_frame_flags & VP8_GOLD_FRAME) 1.3441 + cpi->mr_low_res_mv_avail &= (cpi->current_ref_frames[GOLDEN_FRAME] 1.3442 + == low_res_frame_info->low_res_ref_frames[GOLDEN_FRAME]); 1.3443 + 1.3444 + if (cpi->ref_frame_flags & VP8_ALTR_FRAME) 1.3445 + cpi->mr_low_res_mv_avail &= (cpi->current_ref_frames[ALTREF_FRAME] 1.3446 + == low_res_frame_info->low_res_ref_frames[ALTREF_FRAME]); 1.3447 + } 1.3448 + } 1.3449 +#endif 1.3450 + 1.3451 + /* Set various flags etc to special state if it is a key frame */ 1.3452 + if (cm->frame_type == KEY_FRAME) 1.3453 + { 1.3454 + int i; 1.3455 + 1.3456 + // Set the loop filter deltas and segmentation map update 1.3457 + setup_features(cpi); 1.3458 + 1.3459 + /* The alternate reference frame cannot be active for a key frame */ 1.3460 + cpi->source_alt_ref_active = 0; 1.3461 + 1.3462 + /* Reset the RD threshold multipliers to default of * 1 (128) */ 1.3463 + for (i = 0; i < MAX_MODES; i++) 1.3464 + { 1.3465 + cpi->mb.rd_thresh_mult[i] = 128; 1.3466 + } 1.3467 + } 1.3468 + 1.3469 +#if 0 1.3470 + /* Experimental code for lagged compress and one pass 1.3471 + * Initialise one_pass GF frames stats 1.3472 + * Update stats used for GF selection 1.3473 + */ 1.3474 + { 1.3475 + cpi->one_pass_frame_index = cm->current_video_frame % MAX_LAG_BUFFERS; 1.3476 + 1.3477 + cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frames_so_far = 0; 1.3478 + cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_intra_error = 0.0; 1.3479 + cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_coded_error = 0.0; 1.3480 + cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_inter = 0.0; 1.3481 + cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_motion = 0.0; 1.3482 + cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr = 0.0; 1.3483 + cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr_abs = 0.0; 1.3484 + cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc = 0.0; 1.3485 + cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc_abs = 0.0; 1.3486 + } 1.3487 +#endif 1.3488 + 1.3489 + update_rd_ref_frame_probs(cpi); 1.3490 + 1.3491 + if (cpi->drop_frames_allowed) 1.3492 + { 1.3493 + /* The reset to decimation 0 is only done here for one pass. 1.3494 + * Once it is set two pass leaves decimation on till the next kf. 1.3495 + */ 1.3496 + if ((cpi->buffer_level > drop_mark) && (cpi->decimation_factor > 0)) 1.3497 + cpi->decimation_factor --; 1.3498 + 1.3499 + if (cpi->buffer_level > drop_mark75 && cpi->decimation_factor > 0) 1.3500 + cpi->decimation_factor = 1; 1.3501 + 1.3502 + else if (cpi->buffer_level < drop_mark25 && (cpi->decimation_factor == 2 || cpi->decimation_factor == 3)) 1.3503 + { 1.3504 + cpi->decimation_factor = 3; 1.3505 + } 1.3506 + else if (cpi->buffer_level < drop_mark50 && (cpi->decimation_factor == 1 || cpi->decimation_factor == 2)) 1.3507 + { 1.3508 + cpi->decimation_factor = 2; 1.3509 + } 1.3510 + else if (cpi->buffer_level < drop_mark75 && (cpi->decimation_factor == 0 || cpi->decimation_factor == 1)) 1.3511 + { 1.3512 + cpi->decimation_factor = 1; 1.3513 + } 1.3514 + } 1.3515 + 1.3516 + /* The following decimates the frame rate according to a regular 1.3517 + * pattern (i.e. to 1/2 or 2/3 frame rate) This can be used to help 1.3518 + * prevent buffer under-run in CBR mode. Alternatively it might be 1.3519 + * desirable in some situations to drop frame rate but throw more bits 1.3520 + * at each frame. 1.3521 + * 1.3522 + * Note that dropping a key frame can be problematic if spatial 1.3523 + * resampling is also active 1.3524 + */ 1.3525 + if (cpi->decimation_factor > 0) 1.3526 + { 1.3527 + switch (cpi->decimation_factor) 1.3528 + { 1.3529 + case 1: 1.3530 + cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 3 / 2; 1.3531 + break; 1.3532 + case 2: 1.3533 + cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4; 1.3534 + break; 1.3535 + case 3: 1.3536 + cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4; 1.3537 + break; 1.3538 + } 1.3539 + 1.3540 + /* Note that we should not throw out a key frame (especially when 1.3541 + * spatial resampling is enabled). 1.3542 + */ 1.3543 + if (cm->frame_type == KEY_FRAME) 1.3544 + { 1.3545 + cpi->decimation_count = cpi->decimation_factor; 1.3546 + } 1.3547 + else if (cpi->decimation_count > 0) 1.3548 + { 1.3549 + cpi->decimation_count --; 1.3550 + 1.3551 + cpi->bits_off_target += cpi->av_per_frame_bandwidth; 1.3552 + if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size) 1.3553 + cpi->bits_off_target = cpi->oxcf.maximum_buffer_size; 1.3554 + 1.3555 +#if CONFIG_MULTI_RES_ENCODING 1.3556 + vp8_store_drop_frame_info(cpi); 1.3557 +#endif 1.3558 + 1.3559 + cm->current_video_frame++; 1.3560 + cpi->frames_since_key++; 1.3561 + // We advance the temporal pattern for dropped frames. 1.3562 + cpi->temporal_pattern_counter++; 1.3563 + 1.3564 +#if CONFIG_INTERNAL_STATS 1.3565 + cpi->count ++; 1.3566 +#endif 1.3567 + 1.3568 + cpi->buffer_level = cpi->bits_off_target; 1.3569 + 1.3570 + if (cpi->oxcf.number_of_layers > 1) 1.3571 + { 1.3572 + unsigned int i; 1.3573 + 1.3574 + /* Propagate bits saved by dropping the frame to higher 1.3575 + * layers 1.3576 + */ 1.3577 + for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++) 1.3578 + { 1.3579 + LAYER_CONTEXT *lc = &cpi->layer_context[i]; 1.3580 + lc->bits_off_target += (int)(lc->target_bandwidth / 1.3581 + lc->framerate); 1.3582 + if (lc->bits_off_target > lc->maximum_buffer_size) 1.3583 + lc->bits_off_target = lc->maximum_buffer_size; 1.3584 + lc->buffer_level = lc->bits_off_target; 1.3585 + } 1.3586 + } 1.3587 + 1.3588 + return; 1.3589 + } 1.3590 + else 1.3591 + cpi->decimation_count = cpi->decimation_factor; 1.3592 + } 1.3593 + else 1.3594 + cpi->decimation_count = 0; 1.3595 + 1.3596 + /* Decide how big to make the frame */ 1.3597 + if (!vp8_pick_frame_size(cpi)) 1.3598 + { 1.3599 + /*TODO: 2 drop_frame and return code could be put together. */ 1.3600 +#if CONFIG_MULTI_RES_ENCODING 1.3601 + vp8_store_drop_frame_info(cpi); 1.3602 +#endif 1.3603 + cm->current_video_frame++; 1.3604 + cpi->frames_since_key++; 1.3605 + // We advance the temporal pattern for dropped frames. 1.3606 + cpi->temporal_pattern_counter++; 1.3607 + return; 1.3608 + } 1.3609 + 1.3610 + /* Reduce active_worst_allowed_q for CBR if our buffer is getting too full. 1.3611 + * This has a knock on effect on active best quality as well. 1.3612 + * For CBR if the buffer reaches its maximum level then we can no longer 1.3613 + * save up bits for later frames so we might as well use them up 1.3614 + * on the current frame. 1.3615 + */ 1.3616 + if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) && 1.3617 + (cpi->buffer_level >= cpi->oxcf.optimal_buffer_level) && cpi->buffered_mode) 1.3618 + { 1.3619 + /* Max adjustment is 1/4 */ 1.3620 + int Adjustment = cpi->active_worst_quality / 4; 1.3621 + 1.3622 + if (Adjustment) 1.3623 + { 1.3624 + int buff_lvl_step; 1.3625 + 1.3626 + if (cpi->buffer_level < cpi->oxcf.maximum_buffer_size) 1.3627 + { 1.3628 + buff_lvl_step = (int) 1.3629 + ((cpi->oxcf.maximum_buffer_size - 1.3630 + cpi->oxcf.optimal_buffer_level) / 1.3631 + Adjustment); 1.3632 + 1.3633 + if (buff_lvl_step) 1.3634 + Adjustment = (int) 1.3635 + ((cpi->buffer_level - 1.3636 + cpi->oxcf.optimal_buffer_level) / 1.3637 + buff_lvl_step); 1.3638 + else 1.3639 + Adjustment = 0; 1.3640 + } 1.3641 + 1.3642 + cpi->active_worst_quality -= Adjustment; 1.3643 + 1.3644 + if(cpi->active_worst_quality < cpi->active_best_quality) 1.3645 + cpi->active_worst_quality = cpi->active_best_quality; 1.3646 + } 1.3647 + } 1.3648 + 1.3649 + /* Set an active best quality and if necessary active worst quality 1.3650 + * There is some odd behavior for one pass here that needs attention. 1.3651 + */ 1.3652 + if ( (cpi->pass == 2) || (cpi->ni_frames > 150)) 1.3653 + { 1.3654 + vp8_clear_system_state(); 1.3655 + 1.3656 + Q = cpi->active_worst_quality; 1.3657 + 1.3658 + if ( cm->frame_type == KEY_FRAME ) 1.3659 + { 1.3660 + if ( cpi->pass == 2 ) 1.3661 + { 1.3662 + if (cpi->gfu_boost > 600) 1.3663 + cpi->active_best_quality = kf_low_motion_minq[Q]; 1.3664 + else 1.3665 + cpi->active_best_quality = kf_high_motion_minq[Q]; 1.3666 + 1.3667 + /* Special case for key frames forced because we have reached 1.3668 + * the maximum key frame interval. Here force the Q to a range 1.3669 + * based on the ambient Q to reduce the risk of popping 1.3670 + */ 1.3671 + if ( cpi->this_key_frame_forced ) 1.3672 + { 1.3673 + if ( cpi->active_best_quality > cpi->avg_frame_qindex * 7/8) 1.3674 + cpi->active_best_quality = cpi->avg_frame_qindex * 7/8; 1.3675 + else if ( cpi->active_best_quality < cpi->avg_frame_qindex >> 2 ) 1.3676 + cpi->active_best_quality = cpi->avg_frame_qindex >> 2; 1.3677 + } 1.3678 + } 1.3679 + /* One pass more conservative */ 1.3680 + else 1.3681 + cpi->active_best_quality = kf_high_motion_minq[Q]; 1.3682 + } 1.3683 + 1.3684 + else if (cpi->oxcf.number_of_layers==1 && 1.3685 + (cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame)) 1.3686 + { 1.3687 + /* Use the lower of cpi->active_worst_quality and recent 1.3688 + * average Q as basis for GF/ARF Q limit unless last frame was 1.3689 + * a key frame. 1.3690 + */ 1.3691 + if ( (cpi->frames_since_key > 1) && 1.3692 + (cpi->avg_frame_qindex < cpi->active_worst_quality) ) 1.3693 + { 1.3694 + Q = cpi->avg_frame_qindex; 1.3695 + } 1.3696 + 1.3697 + /* For constrained quality dont allow Q less than the cq level */ 1.3698 + if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) && 1.3699 + (Q < cpi->cq_target_quality) ) 1.3700 + { 1.3701 + Q = cpi->cq_target_quality; 1.3702 + } 1.3703 + 1.3704 + if ( cpi->pass == 2 ) 1.3705 + { 1.3706 + if ( cpi->gfu_boost > 1000 ) 1.3707 + cpi->active_best_quality = gf_low_motion_minq[Q]; 1.3708 + else if ( cpi->gfu_boost < 400 ) 1.3709 + cpi->active_best_quality = gf_high_motion_minq[Q]; 1.3710 + else 1.3711 + cpi->active_best_quality = gf_mid_motion_minq[Q]; 1.3712 + 1.3713 + /* Constrained quality use slightly lower active best. */ 1.3714 + if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY ) 1.3715 + { 1.3716 + cpi->active_best_quality = 1.3717 + cpi->active_best_quality * 15/16; 1.3718 + } 1.3719 + } 1.3720 + /* One pass more conservative */ 1.3721 + else 1.3722 + cpi->active_best_quality = gf_high_motion_minq[Q]; 1.3723 + } 1.3724 + else 1.3725 + { 1.3726 + cpi->active_best_quality = inter_minq[Q]; 1.3727 + 1.3728 + /* For the constant/constrained quality mode we dont want 1.3729 + * q to fall below the cq level. 1.3730 + */ 1.3731 + if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) && 1.3732 + (cpi->active_best_quality < cpi->cq_target_quality) ) 1.3733 + { 1.3734 + /* If we are strongly undershooting the target rate in the last 1.3735 + * frames then use the user passed in cq value not the auto 1.3736 + * cq value. 1.3737 + */ 1.3738 + if ( cpi->rolling_actual_bits < cpi->min_frame_bandwidth ) 1.3739 + cpi->active_best_quality = cpi->oxcf.cq_level; 1.3740 + else 1.3741 + cpi->active_best_quality = cpi->cq_target_quality; 1.3742 + } 1.3743 + } 1.3744 + 1.3745 + /* If CBR and the buffer is as full then it is reasonable to allow 1.3746 + * higher quality on the frames to prevent bits just going to waste. 1.3747 + */ 1.3748 + if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) 1.3749 + { 1.3750 + /* Note that the use of >= here elliminates the risk of a devide 1.3751 + * by 0 error in the else if clause 1.3752 + */ 1.3753 + if (cpi->buffer_level >= cpi->oxcf.maximum_buffer_size) 1.3754 + cpi->active_best_quality = cpi->best_quality; 1.3755 + 1.3756 + else if (cpi->buffer_level > cpi->oxcf.optimal_buffer_level) 1.3757 + { 1.3758 + int Fraction = (int) 1.3759 + (((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) * 128) 1.3760 + / (cpi->oxcf.maximum_buffer_size - 1.3761 + cpi->oxcf.optimal_buffer_level)); 1.3762 + int min_qadjustment = ((cpi->active_best_quality - 1.3763 + cpi->best_quality) * Fraction) / 128; 1.3764 + 1.3765 + cpi->active_best_quality -= min_qadjustment; 1.3766 + } 1.3767 + } 1.3768 + } 1.3769 + /* Make sure constrained quality mode limits are adhered to for the first 1.3770 + * few frames of one pass encodes 1.3771 + */ 1.3772 + else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) 1.3773 + { 1.3774 + if ( (cm->frame_type == KEY_FRAME) || 1.3775 + cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame ) 1.3776 + { 1.3777 + cpi->active_best_quality = cpi->best_quality; 1.3778 + } 1.3779 + else if (cpi->active_best_quality < cpi->cq_target_quality) 1.3780 + { 1.3781 + cpi->active_best_quality = cpi->cq_target_quality; 1.3782 + } 1.3783 + } 1.3784 + 1.3785 + /* Clip the active best and worst quality values to limits */ 1.3786 + if (cpi->active_worst_quality > cpi->worst_quality) 1.3787 + cpi->active_worst_quality = cpi->worst_quality; 1.3788 + 1.3789 + if (cpi->active_best_quality < cpi->best_quality) 1.3790 + cpi->active_best_quality = cpi->best_quality; 1.3791 + 1.3792 + if ( cpi->active_worst_quality < cpi->active_best_quality ) 1.3793 + cpi->active_worst_quality = cpi->active_best_quality; 1.3794 + 1.3795 + /* Determine initial Q to try */ 1.3796 + Q = vp8_regulate_q(cpi, cpi->this_frame_target); 1.3797 + 1.3798 +#if !(CONFIG_REALTIME_ONLY) 1.3799 + 1.3800 + /* Set highest allowed value for Zbin over quant */ 1.3801 + if (cm->frame_type == KEY_FRAME) 1.3802 + zbin_oq_high = 0; 1.3803 + else if ((cpi->oxcf.number_of_layers == 1) && ((cm->refresh_alt_ref_frame || 1.3804 + (cm->refresh_golden_frame && !cpi->source_alt_ref_active)))) 1.3805 + { 1.3806 + zbin_oq_high = 16; 1.3807 + } 1.3808 + else 1.3809 + zbin_oq_high = ZBIN_OQ_MAX; 1.3810 +#endif 1.3811 + 1.3812 + /* Setup background Q adjustment for error resilient mode. 1.3813 + * For multi-layer encodes only enable this for the base layer. 1.3814 + */ 1.3815 + if (cpi->cyclic_refresh_mode_enabled) 1.3816 + { 1.3817 + if (cpi->current_layer==0) 1.3818 + cyclic_background_refresh(cpi, Q, 0); 1.3819 + else 1.3820 + disable_segmentation(cpi); 1.3821 + } 1.3822 + 1.3823 + vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit); 1.3824 + 1.3825 +#if !(CONFIG_REALTIME_ONLY) 1.3826 + /* Limit Q range for the adaptive loop. */ 1.3827 + bottom_index = cpi->active_best_quality; 1.3828 + top_index = cpi->active_worst_quality; 1.3829 + q_low = cpi->active_best_quality; 1.3830 + q_high = cpi->active_worst_quality; 1.3831 +#endif 1.3832 + 1.3833 + vp8_save_coding_context(cpi); 1.3834 + 1.3835 + loop_count = 0; 1.3836 + 1.3837 + scale_and_extend_source(cpi->un_scaled_source, cpi); 1.3838 + 1.3839 +#if !(CONFIG_REALTIME_ONLY) && CONFIG_POSTPROC && !(CONFIG_TEMPORAL_DENOISING) 1.3840 + 1.3841 + if (cpi->oxcf.noise_sensitivity > 0) 1.3842 + { 1.3843 + unsigned char *src; 1.3844 + int l = 0; 1.3845 + 1.3846 + switch (cpi->oxcf.noise_sensitivity) 1.3847 + { 1.3848 + case 1: 1.3849 + l = 20; 1.3850 + break; 1.3851 + case 2: 1.3852 + l = 40; 1.3853 + break; 1.3854 + case 3: 1.3855 + l = 60; 1.3856 + break; 1.3857 + case 4: 1.3858 + l = 80; 1.3859 + break; 1.3860 + case 5: 1.3861 + l = 100; 1.3862 + break; 1.3863 + case 6: 1.3864 + l = 150; 1.3865 + break; 1.3866 + } 1.3867 + 1.3868 + 1.3869 + if (cm->frame_type == KEY_FRAME) 1.3870 + { 1.3871 + vp8_de_noise(cm, cpi->Source, cpi->Source, l , 1, 0); 1.3872 + } 1.3873 + else 1.3874 + { 1.3875 + vp8_de_noise(cm, cpi->Source, cpi->Source, l , 1, 0); 1.3876 + 1.3877 + src = cpi->Source->y_buffer; 1.3878 + 1.3879 + if (cpi->Source->y_stride < 0) 1.3880 + { 1.3881 + src += cpi->Source->y_stride * (cpi->Source->y_height - 1); 1.3882 + } 1.3883 + } 1.3884 + } 1.3885 + 1.3886 +#endif 1.3887 + 1.3888 +#ifdef OUTPUT_YUV_SRC 1.3889 + vp8_write_yuv_frame(cpi->Source); 1.3890 +#endif 1.3891 + 1.3892 + do 1.3893 + { 1.3894 + vp8_clear_system_state(); 1.3895 + 1.3896 + vp8_set_quantizer(cpi, Q); 1.3897 + 1.3898 + /* setup skip prob for costing in mode/mv decision */ 1.3899 + if (cpi->common.mb_no_coeff_skip) 1.3900 + { 1.3901 + cpi->prob_skip_false = cpi->base_skip_false_prob[Q]; 1.3902 + 1.3903 + if (cm->frame_type != KEY_FRAME) 1.3904 + { 1.3905 + if (cpi->common.refresh_alt_ref_frame) 1.3906 + { 1.3907 + if (cpi->last_skip_false_probs[2] != 0) 1.3908 + cpi->prob_skip_false = cpi->last_skip_false_probs[2]; 1.3909 + 1.3910 + /* 1.3911 + if(cpi->last_skip_false_probs[2]!=0 && abs(Q- cpi->last_skip_probs_q[2])<=16 ) 1.3912 + cpi->prob_skip_false = cpi->last_skip_false_probs[2]; 1.3913 + else if (cpi->last_skip_false_probs[2]!=0) 1.3914 + cpi->prob_skip_false = (cpi->last_skip_false_probs[2] + cpi->prob_skip_false ) / 2; 1.3915 + */ 1.3916 + } 1.3917 + else if (cpi->common.refresh_golden_frame) 1.3918 + { 1.3919 + if (cpi->last_skip_false_probs[1] != 0) 1.3920 + cpi->prob_skip_false = cpi->last_skip_false_probs[1]; 1.3921 + 1.3922 + /* 1.3923 + if(cpi->last_skip_false_probs[1]!=0 && abs(Q- cpi->last_skip_probs_q[1])<=16 ) 1.3924 + cpi->prob_skip_false = cpi->last_skip_false_probs[1]; 1.3925 + else if (cpi->last_skip_false_probs[1]!=0) 1.3926 + cpi->prob_skip_false = (cpi->last_skip_false_probs[1] + cpi->prob_skip_false ) / 2; 1.3927 + */ 1.3928 + } 1.3929 + else 1.3930 + { 1.3931 + if (cpi->last_skip_false_probs[0] != 0) 1.3932 + cpi->prob_skip_false = cpi->last_skip_false_probs[0]; 1.3933 + 1.3934 + /* 1.3935 + if(cpi->last_skip_false_probs[0]!=0 && abs(Q- cpi->last_skip_probs_q[0])<=16 ) 1.3936 + cpi->prob_skip_false = cpi->last_skip_false_probs[0]; 1.3937 + else if(cpi->last_skip_false_probs[0]!=0) 1.3938 + cpi->prob_skip_false = (cpi->last_skip_false_probs[0] + cpi->prob_skip_false ) / 2; 1.3939 + */ 1.3940 + } 1.3941 + 1.3942 + /* as this is for cost estimate, let's make sure it does not 1.3943 + * go extreme eitehr way 1.3944 + */ 1.3945 + if (cpi->prob_skip_false < 5) 1.3946 + cpi->prob_skip_false = 5; 1.3947 + 1.3948 + if (cpi->prob_skip_false > 250) 1.3949 + cpi->prob_skip_false = 250; 1.3950 + 1.3951 + if (cpi->oxcf.number_of_layers == 1 && cpi->is_src_frame_alt_ref) 1.3952 + cpi->prob_skip_false = 1; 1.3953 + } 1.3954 + 1.3955 +#if 0 1.3956 + 1.3957 + if (cpi->pass != 1) 1.3958 + { 1.3959 + FILE *f = fopen("skip.stt", "a"); 1.3960 + fprintf(f, "%d, %d, %4d ", cpi->common.refresh_golden_frame, cpi->common.refresh_alt_ref_frame, cpi->prob_skip_false); 1.3961 + fclose(f); 1.3962 + } 1.3963 + 1.3964 +#endif 1.3965 + 1.3966 + } 1.3967 + 1.3968 + if (cm->frame_type == KEY_FRAME) 1.3969 + { 1.3970 + if(resize_key_frame(cpi)) 1.3971 + { 1.3972 + /* If the frame size has changed, need to reset Q, quantizer, 1.3973 + * and background refresh. 1.3974 + */ 1.3975 + Q = vp8_regulate_q(cpi, cpi->this_frame_target); 1.3976 + if (cpi->cyclic_refresh_mode_enabled) 1.3977 + { 1.3978 + if (cpi->current_layer==0) 1.3979 + cyclic_background_refresh(cpi, Q, 0); 1.3980 + else 1.3981 + disable_segmentation(cpi); 1.3982 + } 1.3983 + vp8_set_quantizer(cpi, Q); 1.3984 + } 1.3985 + 1.3986 + vp8_setup_key_frame(cpi); 1.3987 + } 1.3988 + 1.3989 + 1.3990 + 1.3991 +#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING 1.3992 + { 1.3993 + if(cpi->oxcf.error_resilient_mode) 1.3994 + cm->refresh_entropy_probs = 0; 1.3995 + 1.3996 + if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS) 1.3997 + { 1.3998 + if (cm->frame_type == KEY_FRAME) 1.3999 + cm->refresh_entropy_probs = 1; 1.4000 + } 1.4001 + 1.4002 + if (cm->refresh_entropy_probs == 0) 1.4003 + { 1.4004 + /* save a copy for later refresh */ 1.4005 + vpx_memcpy(&cm->lfc, &cm->fc, sizeof(cm->fc)); 1.4006 + } 1.4007 + 1.4008 + vp8_update_coef_context(cpi); 1.4009 + 1.4010 + vp8_update_coef_probs(cpi); 1.4011 + 1.4012 + /* transform / motion compensation build reconstruction frame 1.4013 + * +pack coef partitions 1.4014 + */ 1.4015 + vp8_encode_frame(cpi); 1.4016 + 1.4017 + /* cpi->projected_frame_size is not needed for RT mode */ 1.4018 + } 1.4019 +#else 1.4020 + /* transform / motion compensation build reconstruction frame */ 1.4021 + vp8_encode_frame(cpi); 1.4022 + 1.4023 + cpi->projected_frame_size -= vp8_estimate_entropy_savings(cpi); 1.4024 + cpi->projected_frame_size = (cpi->projected_frame_size > 0) ? cpi->projected_frame_size : 0; 1.4025 +#endif 1.4026 + vp8_clear_system_state(); 1.4027 + 1.4028 + /* Test to see if the stats generated for this frame indicate that 1.4029 + * we should have coded a key frame (assuming that we didn't)! 1.4030 + */ 1.4031 + 1.4032 + if (cpi->pass != 2 && cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME 1.4033 + && cpi->compressor_speed != 2) 1.4034 + { 1.4035 +#if !(CONFIG_REALTIME_ONLY) 1.4036 + if (decide_key_frame(cpi)) 1.4037 + { 1.4038 + /* Reset all our sizing numbers and recode */ 1.4039 + cm->frame_type = KEY_FRAME; 1.4040 + 1.4041 + vp8_pick_frame_size(cpi); 1.4042 + 1.4043 + /* Clear the Alt reference frame active flag when we have 1.4044 + * a key frame 1.4045 + */ 1.4046 + cpi->source_alt_ref_active = 0; 1.4047 + 1.4048 + // Set the loop filter deltas and segmentation map update 1.4049 + setup_features(cpi); 1.4050 + 1.4051 + vp8_restore_coding_context(cpi); 1.4052 + 1.4053 + Q = vp8_regulate_q(cpi, cpi->this_frame_target); 1.4054 + 1.4055 + vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit); 1.4056 + 1.4057 + /* Limit Q range for the adaptive loop. */ 1.4058 + bottom_index = cpi->active_best_quality; 1.4059 + top_index = cpi->active_worst_quality; 1.4060 + q_low = cpi->active_best_quality; 1.4061 + q_high = cpi->active_worst_quality; 1.4062 + 1.4063 + loop_count++; 1.4064 + Loop = 1; 1.4065 + 1.4066 + continue; 1.4067 + } 1.4068 +#endif 1.4069 + } 1.4070 + 1.4071 + vp8_clear_system_state(); 1.4072 + 1.4073 + if (frame_over_shoot_limit == 0) 1.4074 + frame_over_shoot_limit = 1; 1.4075 + 1.4076 + /* Are we are overshooting and up against the limit of active max Q. */ 1.4077 + if (((cpi->pass != 2) || (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) && 1.4078 + (Q == cpi->active_worst_quality) && 1.4079 + (cpi->active_worst_quality < cpi->worst_quality) && 1.4080 + (cpi->projected_frame_size > frame_over_shoot_limit)) 1.4081 + { 1.4082 + int over_size_percent = ((cpi->projected_frame_size - frame_over_shoot_limit) * 100) / frame_over_shoot_limit; 1.4083 + 1.4084 + /* If so is there any scope for relaxing it */ 1.4085 + while ((cpi->active_worst_quality < cpi->worst_quality) && (over_size_percent > 0)) 1.4086 + { 1.4087 + cpi->active_worst_quality++; 1.4088 + /* Assume 1 qstep = about 4% on frame size. */ 1.4089 + over_size_percent = (int)(over_size_percent * 0.96); 1.4090 + } 1.4091 +#if !(CONFIG_REALTIME_ONLY) 1.4092 + top_index = cpi->active_worst_quality; 1.4093 +#endif 1.4094 + /* If we have updated the active max Q do not call 1.4095 + * vp8_update_rate_correction_factors() this loop. 1.4096 + */ 1.4097 + active_worst_qchanged = 1; 1.4098 + } 1.4099 + else 1.4100 + active_worst_qchanged = 0; 1.4101 + 1.4102 +#if !(CONFIG_REALTIME_ONLY) 1.4103 + /* Special case handling for forced key frames */ 1.4104 + if ( (cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced ) 1.4105 + { 1.4106 + int last_q = Q; 1.4107 + int kf_err = vp8_calc_ss_err(cpi->Source, 1.4108 + &cm->yv12_fb[cm->new_fb_idx]); 1.4109 + 1.4110 + /* The key frame is not good enough */ 1.4111 + if ( kf_err > ((cpi->ambient_err * 7) >> 3) ) 1.4112 + { 1.4113 + /* Lower q_high */ 1.4114 + q_high = (Q > q_low) ? (Q - 1) : q_low; 1.4115 + 1.4116 + /* Adjust Q */ 1.4117 + Q = (q_high + q_low) >> 1; 1.4118 + } 1.4119 + /* The key frame is much better than the previous frame */ 1.4120 + else if ( kf_err < (cpi->ambient_err >> 1) ) 1.4121 + { 1.4122 + /* Raise q_low */ 1.4123 + q_low = (Q < q_high) ? (Q + 1) : q_high; 1.4124 + 1.4125 + /* Adjust Q */ 1.4126 + Q = (q_high + q_low + 1) >> 1; 1.4127 + } 1.4128 + 1.4129 + /* Clamp Q to upper and lower limits: */ 1.4130 + if (Q > q_high) 1.4131 + Q = q_high; 1.4132 + else if (Q < q_low) 1.4133 + Q = q_low; 1.4134 + 1.4135 + Loop = Q != last_q; 1.4136 + } 1.4137 + 1.4138 + /* Is the projected frame size out of range and are we allowed 1.4139 + * to attempt to recode. 1.4140 + */ 1.4141 + else if ( recode_loop_test( cpi, 1.4142 + frame_over_shoot_limit, frame_under_shoot_limit, 1.4143 + Q, top_index, bottom_index ) ) 1.4144 + { 1.4145 + int last_q = Q; 1.4146 + int Retries = 0; 1.4147 + 1.4148 + /* Frame size out of permitted range. Update correction factor 1.4149 + * & compute new Q to try... 1.4150 + */ 1.4151 + 1.4152 + /* Frame is too large */ 1.4153 + if (cpi->projected_frame_size > cpi->this_frame_target) 1.4154 + { 1.4155 + /* Raise Qlow as to at least the current value */ 1.4156 + q_low = (Q < q_high) ? (Q + 1) : q_high; 1.4157 + 1.4158 + /* If we are using over quant do the same for zbin_oq_low */ 1.4159 + if (cpi->mb.zbin_over_quant > 0) 1.4160 + zbin_oq_low = (cpi->mb.zbin_over_quant < zbin_oq_high) ? 1.4161 + (cpi->mb.zbin_over_quant + 1) : zbin_oq_high; 1.4162 + 1.4163 + if (undershoot_seen) 1.4164 + { 1.4165 + /* Update rate_correction_factor unless 1.4166 + * cpi->active_worst_quality has changed. 1.4167 + */ 1.4168 + if (!active_worst_qchanged) 1.4169 + vp8_update_rate_correction_factors(cpi, 1); 1.4170 + 1.4171 + Q = (q_high + q_low + 1) / 2; 1.4172 + 1.4173 + /* Adjust cpi->zbin_over_quant (only allowed when Q 1.4174 + * is max) 1.4175 + */ 1.4176 + if (Q < MAXQ) 1.4177 + cpi->mb.zbin_over_quant = 0; 1.4178 + else 1.4179 + { 1.4180 + zbin_oq_low = (cpi->mb.zbin_over_quant < zbin_oq_high) ? 1.4181 + (cpi->mb.zbin_over_quant + 1) : zbin_oq_high; 1.4182 + cpi->mb.zbin_over_quant = 1.4183 + (zbin_oq_high + zbin_oq_low) / 2; 1.4184 + } 1.4185 + } 1.4186 + else 1.4187 + { 1.4188 + /* Update rate_correction_factor unless 1.4189 + * cpi->active_worst_quality has changed. 1.4190 + */ 1.4191 + if (!active_worst_qchanged) 1.4192 + vp8_update_rate_correction_factors(cpi, 0); 1.4193 + 1.4194 + Q = vp8_regulate_q(cpi, cpi->this_frame_target); 1.4195 + 1.4196 + while (((Q < q_low) || 1.4197 + (cpi->mb.zbin_over_quant < zbin_oq_low)) && 1.4198 + (Retries < 10)) 1.4199 + { 1.4200 + vp8_update_rate_correction_factors(cpi, 0); 1.4201 + Q = vp8_regulate_q(cpi, cpi->this_frame_target); 1.4202 + Retries ++; 1.4203 + } 1.4204 + } 1.4205 + 1.4206 + overshoot_seen = 1; 1.4207 + } 1.4208 + /* Frame is too small */ 1.4209 + else 1.4210 + { 1.4211 + if (cpi->mb.zbin_over_quant == 0) 1.4212 + /* Lower q_high if not using over quant */ 1.4213 + q_high = (Q > q_low) ? (Q - 1) : q_low; 1.4214 + else 1.4215 + /* else lower zbin_oq_high */ 1.4216 + zbin_oq_high = (cpi->mb.zbin_over_quant > zbin_oq_low) ? 1.4217 + (cpi->mb.zbin_over_quant - 1) : zbin_oq_low; 1.4218 + 1.4219 + if (overshoot_seen) 1.4220 + { 1.4221 + /* Update rate_correction_factor unless 1.4222 + * cpi->active_worst_quality has changed. 1.4223 + */ 1.4224 + if (!active_worst_qchanged) 1.4225 + vp8_update_rate_correction_factors(cpi, 1); 1.4226 + 1.4227 + Q = (q_high + q_low) / 2; 1.4228 + 1.4229 + /* Adjust cpi->zbin_over_quant (only allowed when Q 1.4230 + * is max) 1.4231 + */ 1.4232 + if (Q < MAXQ) 1.4233 + cpi->mb.zbin_over_quant = 0; 1.4234 + else 1.4235 + cpi->mb.zbin_over_quant = 1.4236 + (zbin_oq_high + zbin_oq_low) / 2; 1.4237 + } 1.4238 + else 1.4239 + { 1.4240 + /* Update rate_correction_factor unless 1.4241 + * cpi->active_worst_quality has changed. 1.4242 + */ 1.4243 + if (!active_worst_qchanged) 1.4244 + vp8_update_rate_correction_factors(cpi, 0); 1.4245 + 1.4246 + Q = vp8_regulate_q(cpi, cpi->this_frame_target); 1.4247 + 1.4248 + /* Special case reset for qlow for constrained quality. 1.4249 + * This should only trigger where there is very substantial 1.4250 + * undershoot on a frame and the auto cq level is above 1.4251 + * the user passsed in value. 1.4252 + */ 1.4253 + if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) && 1.4254 + (Q < q_low) ) 1.4255 + { 1.4256 + q_low = Q; 1.4257 + } 1.4258 + 1.4259 + while (((Q > q_high) || 1.4260 + (cpi->mb.zbin_over_quant > zbin_oq_high)) && 1.4261 + (Retries < 10)) 1.4262 + { 1.4263 + vp8_update_rate_correction_factors(cpi, 0); 1.4264 + Q = vp8_regulate_q(cpi, cpi->this_frame_target); 1.4265 + Retries ++; 1.4266 + } 1.4267 + } 1.4268 + 1.4269 + undershoot_seen = 1; 1.4270 + } 1.4271 + 1.4272 + /* Clamp Q to upper and lower limits: */ 1.4273 + if (Q > q_high) 1.4274 + Q = q_high; 1.4275 + else if (Q < q_low) 1.4276 + Q = q_low; 1.4277 + 1.4278 + /* Clamp cpi->zbin_over_quant */ 1.4279 + cpi->mb.zbin_over_quant = (cpi->mb.zbin_over_quant < zbin_oq_low) ? 1.4280 + zbin_oq_low : (cpi->mb.zbin_over_quant > zbin_oq_high) ? 1.4281 + zbin_oq_high : cpi->mb.zbin_over_quant; 1.4282 + 1.4283 + Loop = Q != last_q; 1.4284 + } 1.4285 + else 1.4286 +#endif 1.4287 + Loop = 0; 1.4288 + 1.4289 + if (cpi->is_src_frame_alt_ref) 1.4290 + Loop = 0; 1.4291 + 1.4292 + if (Loop == 1) 1.4293 + { 1.4294 + vp8_restore_coding_context(cpi); 1.4295 + loop_count++; 1.4296 +#if CONFIG_INTERNAL_STATS 1.4297 + cpi->tot_recode_hits++; 1.4298 +#endif 1.4299 + } 1.4300 + } 1.4301 + while (Loop == 1); 1.4302 + 1.4303 +#if 0 1.4304 + /* Experimental code for lagged and one pass 1.4305 + * Update stats used for one pass GF selection 1.4306 + */ 1.4307 + { 1.4308 + cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_coded_error = (double)cpi->prediction_error; 1.4309 + cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_intra_error = (double)cpi->intra_error; 1.4310 + cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_pcnt_inter = (double)(100 - cpi->this_frame_percent_intra) / 100.0; 1.4311 + } 1.4312 +#endif 1.4313 + 1.4314 + /* Special case code to reduce pulsing when key frames are forced at a 1.4315 + * fixed interval. Note the reconstruction error if it is the frame before 1.4316 + * the force key frame 1.4317 + */ 1.4318 + if ( cpi->next_key_frame_forced && (cpi->twopass.frames_to_key == 0) ) 1.4319 + { 1.4320 + cpi->ambient_err = vp8_calc_ss_err(cpi->Source, 1.4321 + &cm->yv12_fb[cm->new_fb_idx]); 1.4322 + } 1.4323 + 1.4324 + /* This frame's MVs are saved and will be used in next frame's MV predictor. 1.4325 + * Last frame has one more line(add to bottom) and one more column(add to 1.4326 + * right) than cm->mip. The edge elements are initialized to 0. 1.4327 + */ 1.4328 +#if CONFIG_MULTI_RES_ENCODING 1.4329 + if(!cpi->oxcf.mr_encoder_id && cm->show_frame) 1.4330 +#else 1.4331 + if(cm->show_frame) /* do not save for altref frame */ 1.4332 +#endif 1.4333 + { 1.4334 + int mb_row; 1.4335 + int mb_col; 1.4336 + /* Point to beginning of allocated MODE_INFO arrays. */ 1.4337 + MODE_INFO *tmp = cm->mip; 1.4338 + 1.4339 + if(cm->frame_type != KEY_FRAME) 1.4340 + { 1.4341 + for (mb_row = 0; mb_row < cm->mb_rows+1; mb_row ++) 1.4342 + { 1.4343 + for (mb_col = 0; mb_col < cm->mb_cols+1; mb_col ++) 1.4344 + { 1.4345 + if(tmp->mbmi.ref_frame != INTRA_FRAME) 1.4346 + cpi->lfmv[mb_col + mb_row*(cm->mode_info_stride+1)].as_int = tmp->mbmi.mv.as_int; 1.4347 + 1.4348 + cpi->lf_ref_frame_sign_bias[mb_col + mb_row*(cm->mode_info_stride+1)] = cm->ref_frame_sign_bias[tmp->mbmi.ref_frame]; 1.4349 + cpi->lf_ref_frame[mb_col + mb_row*(cm->mode_info_stride+1)] = tmp->mbmi.ref_frame; 1.4350 + tmp++; 1.4351 + } 1.4352 + } 1.4353 + } 1.4354 + } 1.4355 + 1.4356 + /* Count last ref frame 0,0 usage on current encoded frame. */ 1.4357 + { 1.4358 + int mb_row; 1.4359 + int mb_col; 1.4360 + /* Point to beginning of MODE_INFO arrays. */ 1.4361 + MODE_INFO *tmp = cm->mi; 1.4362 + 1.4363 + cpi->zeromv_count = 0; 1.4364 + 1.4365 + if(cm->frame_type != KEY_FRAME) 1.4366 + { 1.4367 + for (mb_row = 0; mb_row < cm->mb_rows; mb_row ++) 1.4368 + { 1.4369 + for (mb_col = 0; mb_col < cm->mb_cols; mb_col ++) 1.4370 + { 1.4371 + if(tmp->mbmi.mode == ZEROMV) 1.4372 + cpi->zeromv_count++; 1.4373 + tmp++; 1.4374 + } 1.4375 + tmp++; 1.4376 + } 1.4377 + } 1.4378 + } 1.4379 + 1.4380 +#if CONFIG_MULTI_RES_ENCODING 1.4381 + vp8_cal_dissimilarity(cpi); 1.4382 +#endif 1.4383 + 1.4384 + /* Update the GF useage maps. 1.4385 + * This is done after completing the compression of a frame when all 1.4386 + * modes etc. are finalized but before loop filter 1.4387 + */ 1.4388 + if (cpi->oxcf.number_of_layers == 1) 1.4389 + vp8_update_gf_useage_maps(cpi, cm, &cpi->mb); 1.4390 + 1.4391 + if (cm->frame_type == KEY_FRAME) 1.4392 + cm->refresh_last_frame = 1; 1.4393 + 1.4394 +#if 0 1.4395 + { 1.4396 + FILE *f = fopen("gfactive.stt", "a"); 1.4397 + fprintf(f, "%8d %8d %8d %8d %8d\n", cm->current_video_frame, (100 * cpi->gf_active_count) / (cpi->common.mb_rows * cpi->common.mb_cols), cpi->this_iiratio, cpi->next_iiratio, cm->refresh_golden_frame); 1.4398 + fclose(f); 1.4399 + } 1.4400 +#endif 1.4401 + 1.4402 + /* For inter frames the current default behavior is that when 1.4403 + * cm->refresh_golden_frame is set we copy the old GF over to the ARF buffer 1.4404 + * This is purely an encoder decision at present. 1.4405 + */ 1.4406 + if (!cpi->oxcf.error_resilient_mode && cm->refresh_golden_frame) 1.4407 + cm->copy_buffer_to_arf = 2; 1.4408 + else 1.4409 + cm->copy_buffer_to_arf = 0; 1.4410 + 1.4411 + cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx]; 1.4412 + 1.4413 +#if CONFIG_MULTITHREAD 1.4414 + if (cpi->b_multi_threaded) 1.4415 + { 1.4416 + /* start loopfilter in separate thread */ 1.4417 + sem_post(&cpi->h_event_start_lpf); 1.4418 + cpi->b_lpf_running = 1; 1.4419 + } 1.4420 + else 1.4421 +#endif 1.4422 + { 1.4423 + vp8_loopfilter_frame(cpi, cm); 1.4424 + } 1.4425 + 1.4426 + update_reference_frames(cpi); 1.4427 + 1.4428 +#if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING) 1.4429 + if (cpi->oxcf.error_resilient_mode) 1.4430 + { 1.4431 + cm->refresh_entropy_probs = 0; 1.4432 + } 1.4433 +#endif 1.4434 + 1.4435 +#if CONFIG_MULTITHREAD 1.4436 + /* wait that filter_level is picked so that we can continue with stream packing */ 1.4437 + if (cpi->b_multi_threaded) 1.4438 + sem_wait(&cpi->h_event_end_lpf); 1.4439 +#endif 1.4440 + 1.4441 + /* build the bitstream */ 1.4442 + vp8_pack_bitstream(cpi, dest, dest_end, size); 1.4443 + 1.4444 +#if CONFIG_MULTITHREAD 1.4445 + /* if PSNR packets are generated we have to wait for the lpf */ 1.4446 + if (cpi->b_lpf_running && cpi->b_calculate_psnr) 1.4447 + { 1.4448 + sem_wait(&cpi->h_event_end_lpf); 1.4449 + cpi->b_lpf_running = 0; 1.4450 + } 1.4451 +#endif 1.4452 + 1.4453 + /* Move storing frame_type out of the above loop since it is also 1.4454 + * needed in motion search besides loopfilter */ 1.4455 + cm->last_frame_type = cm->frame_type; 1.4456 + 1.4457 + /* Update rate control heuristics */ 1.4458 + cpi->total_byte_count += (*size); 1.4459 + cpi->projected_frame_size = (*size) << 3; 1.4460 + 1.4461 + if (cpi->oxcf.number_of_layers > 1) 1.4462 + { 1.4463 + unsigned int i; 1.4464 + for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++) 1.4465 + cpi->layer_context[i].total_byte_count += (*size); 1.4466 + } 1.4467 + 1.4468 + if (!active_worst_qchanged) 1.4469 + vp8_update_rate_correction_factors(cpi, 2); 1.4470 + 1.4471 + cpi->last_q[cm->frame_type] = cm->base_qindex; 1.4472 + 1.4473 + if (cm->frame_type == KEY_FRAME) 1.4474 + { 1.4475 + vp8_adjust_key_frame_context(cpi); 1.4476 + } 1.4477 + 1.4478 + /* Keep a record of ambient average Q. */ 1.4479 + if (cm->frame_type != KEY_FRAME) 1.4480 + cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex + cm->base_qindex) >> 2; 1.4481 + 1.4482 + /* Keep a record from which we can calculate the average Q excluding 1.4483 + * GF updates and key frames 1.4484 + */ 1.4485 + if ((cm->frame_type != KEY_FRAME) && ((cpi->oxcf.number_of_layers > 1) || 1.4486 + (!cm->refresh_golden_frame && !cm->refresh_alt_ref_frame))) 1.4487 + { 1.4488 + cpi->ni_frames++; 1.4489 + 1.4490 + /* Calculate the average Q for normal inter frames (not key or GFU 1.4491 + * frames). 1.4492 + */ 1.4493 + if ( cpi->pass == 2 ) 1.4494 + { 1.4495 + cpi->ni_tot_qi += Q; 1.4496 + cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames); 1.4497 + } 1.4498 + else 1.4499 + { 1.4500 + /* Damp value for first few frames */ 1.4501 + if (cpi->ni_frames > 150 ) 1.4502 + { 1.4503 + cpi->ni_tot_qi += Q; 1.4504 + cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames); 1.4505 + } 1.4506 + /* For one pass, early in the clip ... average the current frame Q 1.4507 + * value with the worstq entered by the user as a dampening measure 1.4508 + */ 1.4509 + else 1.4510 + { 1.4511 + cpi->ni_tot_qi += Q; 1.4512 + cpi->ni_av_qi = ((cpi->ni_tot_qi / cpi->ni_frames) + cpi->worst_quality + 1) / 2; 1.4513 + } 1.4514 + 1.4515 + /* If the average Q is higher than what was used in the last 1.4516 + * frame (after going through the recode loop to keep the frame 1.4517 + * size within range) then use the last frame value - 1. The -1 1.4518 + * is designed to stop Q and hence the data rate, from 1.4519 + * progressively falling away during difficult sections, but at 1.4520 + * the same time reduce the number of itterations around the 1.4521 + * recode loop. 1.4522 + */ 1.4523 + if (Q > cpi->ni_av_qi) 1.4524 + cpi->ni_av_qi = Q - 1; 1.4525 + } 1.4526 + } 1.4527 + 1.4528 + /* Update the buffer level variable. */ 1.4529 + /* Non-viewable frames are a special case and are treated as pure overhead. */ 1.4530 + if ( !cm->show_frame ) 1.4531 + cpi->bits_off_target -= cpi->projected_frame_size; 1.4532 + else 1.4533 + cpi->bits_off_target += cpi->av_per_frame_bandwidth - cpi->projected_frame_size; 1.4534 + 1.4535 + /* Clip the buffer level to the maximum specified buffer size */ 1.4536 + if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size) 1.4537 + cpi->bits_off_target = cpi->oxcf.maximum_buffer_size; 1.4538 + 1.4539 + /* Rolling monitors of whether we are over or underspending used to 1.4540 + * help regulate min and Max Q in two pass. 1.4541 + */ 1.4542 + cpi->rolling_target_bits = ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4; 1.4543 + cpi->rolling_actual_bits = ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4; 1.4544 + cpi->long_rolling_target_bits = ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32; 1.4545 + cpi->long_rolling_actual_bits = ((cpi->long_rolling_actual_bits * 31) + cpi->projected_frame_size + 16) / 32; 1.4546 + 1.4547 + /* Actual bits spent */ 1.4548 + cpi->total_actual_bits += cpi->projected_frame_size; 1.4549 + 1.4550 + /* Debug stats */ 1.4551 + cpi->total_target_vs_actual += (cpi->this_frame_target - cpi->projected_frame_size); 1.4552 + 1.4553 + cpi->buffer_level = cpi->bits_off_target; 1.4554 + 1.4555 + /* Propagate values to higher temporal layers */ 1.4556 + if (cpi->oxcf.number_of_layers > 1) 1.4557 + { 1.4558 + unsigned int i; 1.4559 + 1.4560 + for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++) 1.4561 + { 1.4562 + LAYER_CONTEXT *lc = &cpi->layer_context[i]; 1.4563 + int bits_off_for_this_layer = 1.4564 + (int)(lc->target_bandwidth / lc->framerate - 1.4565 + cpi->projected_frame_size); 1.4566 + 1.4567 + lc->bits_off_target += bits_off_for_this_layer; 1.4568 + 1.4569 + /* Clip buffer level to maximum buffer size for the layer */ 1.4570 + if (lc->bits_off_target > lc->maximum_buffer_size) 1.4571 + lc->bits_off_target = lc->maximum_buffer_size; 1.4572 + 1.4573 + lc->total_actual_bits += cpi->projected_frame_size; 1.4574 + lc->total_target_vs_actual += bits_off_for_this_layer; 1.4575 + lc->buffer_level = lc->bits_off_target; 1.4576 + } 1.4577 + } 1.4578 + 1.4579 + /* Update bits left to the kf and gf groups to account for overshoot 1.4580 + * or undershoot on these frames 1.4581 + */ 1.4582 + if (cm->frame_type == KEY_FRAME) 1.4583 + { 1.4584 + cpi->twopass.kf_group_bits += cpi->this_frame_target - cpi->projected_frame_size; 1.4585 + 1.4586 + if (cpi->twopass.kf_group_bits < 0) 1.4587 + cpi->twopass.kf_group_bits = 0 ; 1.4588 + } 1.4589 + else if (cm->refresh_golden_frame || cm->refresh_alt_ref_frame) 1.4590 + { 1.4591 + cpi->twopass.gf_group_bits += cpi->this_frame_target - cpi->projected_frame_size; 1.4592 + 1.4593 + if (cpi->twopass.gf_group_bits < 0) 1.4594 + cpi->twopass.gf_group_bits = 0 ; 1.4595 + } 1.4596 + 1.4597 + if (cm->frame_type != KEY_FRAME) 1.4598 + { 1.4599 + if (cpi->common.refresh_alt_ref_frame) 1.4600 + { 1.4601 + cpi->last_skip_false_probs[2] = cpi->prob_skip_false; 1.4602 + cpi->last_skip_probs_q[2] = cm->base_qindex; 1.4603 + } 1.4604 + else if (cpi->common.refresh_golden_frame) 1.4605 + { 1.4606 + cpi->last_skip_false_probs[1] = cpi->prob_skip_false; 1.4607 + cpi->last_skip_probs_q[1] = cm->base_qindex; 1.4608 + } 1.4609 + else 1.4610 + { 1.4611 + cpi->last_skip_false_probs[0] = cpi->prob_skip_false; 1.4612 + cpi->last_skip_probs_q[0] = cm->base_qindex; 1.4613 + 1.4614 + /* update the baseline */ 1.4615 + cpi->base_skip_false_prob[cm->base_qindex] = cpi->prob_skip_false; 1.4616 + 1.4617 + } 1.4618 + } 1.4619 + 1.4620 +#if 0 && CONFIG_INTERNAL_STATS 1.4621 + { 1.4622 + FILE *f = fopen("tmp.stt", "a"); 1.4623 + 1.4624 + vp8_clear_system_state(); 1.4625 + 1.4626 + if (cpi->twopass.total_left_stats.coded_error != 0.0) 1.4627 + fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %10d %6d %6d" 1.4628 + "%6d %6d %6d %5d %5d %5d %8d %8.2f %10d %10.3f" 1.4629 + "%10.3f %8d\n", 1.4630 + cpi->common.current_video_frame, cpi->this_frame_target, 1.4631 + cpi->projected_frame_size, 1.4632 + (cpi->projected_frame_size - cpi->this_frame_target), 1.4633 + (int)cpi->total_target_vs_actual, 1.4634 + cpi->buffer_level, 1.4635 + (cpi->oxcf.starting_buffer_level-cpi->bits_off_target), 1.4636 + (int)cpi->total_actual_bits, cm->base_qindex, 1.4637 + cpi->active_best_quality, cpi->active_worst_quality, 1.4638 + cpi->ni_av_qi, cpi->cq_target_quality, 1.4639 + cpi->zbin_over_quant, 1.4640 + cm->refresh_golden_frame, cm->refresh_alt_ref_frame, 1.4641 + cm->frame_type, cpi->gfu_boost, 1.4642 + cpi->twopass.est_max_qcorrection_factor, 1.4643 + (int)cpi->twopass.bits_left, 1.4644 + cpi->twopass.total_left_stats.coded_error, 1.4645 + (double)cpi->twopass.bits_left / 1.4646 + cpi->twopass.total_left_stats.coded_error, 1.4647 + cpi->tot_recode_hits); 1.4648 + else 1.4649 + fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %10d %6d %6d" 1.4650 + "%6d %6d %6d %5d %5d %5d %8d %8.2f %10d %10.3f" 1.4651 + "%8d\n", 1.4652 + cpi->common.current_video_frame, 1.4653 + cpi->this_frame_target, cpi->projected_frame_size, 1.4654 + (cpi->projected_frame_size - cpi->this_frame_target), 1.4655 + (int)cpi->total_target_vs_actual, 1.4656 + cpi->buffer_level, 1.4657 + (cpi->oxcf.starting_buffer_level-cpi->bits_off_target), 1.4658 + (int)cpi->total_actual_bits, cm->base_qindex, 1.4659 + cpi->active_best_quality, cpi->active_worst_quality, 1.4660 + cpi->ni_av_qi, cpi->cq_target_quality, 1.4661 + cpi->zbin_over_quant, 1.4662 + cm->refresh_golden_frame, cm->refresh_alt_ref_frame, 1.4663 + cm->frame_type, cpi->gfu_boost, 1.4664 + cpi->twopass.est_max_qcorrection_factor, 1.4665 + (int)cpi->twopass.bits_left, 1.4666 + cpi->twopass.total_left_stats.coded_error, 1.4667 + cpi->tot_recode_hits); 1.4668 + 1.4669 + fclose(f); 1.4670 + 1.4671 + { 1.4672 + FILE *fmodes = fopen("Modes.stt", "a"); 1.4673 + int i; 1.4674 + 1.4675 + fprintf(fmodes, "%6d:%1d:%1d:%1d ", 1.4676 + cpi->common.current_video_frame, 1.4677 + cm->frame_type, cm->refresh_golden_frame, 1.4678 + cm->refresh_alt_ref_frame); 1.4679 + 1.4680 + fprintf(fmodes, "\n"); 1.4681 + 1.4682 + fclose(fmodes); 1.4683 + } 1.4684 + } 1.4685 + 1.4686 +#endif 1.4687 + 1.4688 + if (cm->refresh_golden_frame == 1) 1.4689 + cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN; 1.4690 + else 1.4691 + cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_GOLDEN; 1.4692 + 1.4693 + if (cm->refresh_alt_ref_frame == 1) 1.4694 + cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF; 1.4695 + else 1.4696 + cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_ALTREF; 1.4697 + 1.4698 + 1.4699 + if (cm->refresh_last_frame & cm->refresh_golden_frame) 1.4700 + /* both refreshed */ 1.4701 + cpi->gold_is_last = 1; 1.4702 + else if (cm->refresh_last_frame ^ cm->refresh_golden_frame) 1.4703 + /* 1 refreshed but not the other */ 1.4704 + cpi->gold_is_last = 0; 1.4705 + 1.4706 + if (cm->refresh_last_frame & cm->refresh_alt_ref_frame) 1.4707 + /* both refreshed */ 1.4708 + cpi->alt_is_last = 1; 1.4709 + else if (cm->refresh_last_frame ^ cm->refresh_alt_ref_frame) 1.4710 + /* 1 refreshed but not the other */ 1.4711 + cpi->alt_is_last = 0; 1.4712 + 1.4713 + if (cm->refresh_alt_ref_frame & cm->refresh_golden_frame) 1.4714 + /* both refreshed */ 1.4715 + cpi->gold_is_alt = 1; 1.4716 + else if (cm->refresh_alt_ref_frame ^ cm->refresh_golden_frame) 1.4717 + /* 1 refreshed but not the other */ 1.4718 + cpi->gold_is_alt = 0; 1.4719 + 1.4720 + cpi->ref_frame_flags = VP8_ALTR_FRAME | VP8_GOLD_FRAME | VP8_LAST_FRAME; 1.4721 + 1.4722 + if (cpi->gold_is_last) 1.4723 + cpi->ref_frame_flags &= ~VP8_GOLD_FRAME; 1.4724 + 1.4725 + if (cpi->alt_is_last) 1.4726 + cpi->ref_frame_flags &= ~VP8_ALTR_FRAME; 1.4727 + 1.4728 + if (cpi->gold_is_alt) 1.4729 + cpi->ref_frame_flags &= ~VP8_ALTR_FRAME; 1.4730 + 1.4731 + 1.4732 + if (!cpi->oxcf.error_resilient_mode) 1.4733 + { 1.4734 + if (cpi->oxcf.play_alternate && cm->refresh_alt_ref_frame && (cm->frame_type != KEY_FRAME)) 1.4735 + /* Update the alternate reference frame stats as appropriate. */ 1.4736 + update_alt_ref_frame_stats(cpi); 1.4737 + else 1.4738 + /* Update the Golden frame stats as appropriate. */ 1.4739 + update_golden_frame_stats(cpi); 1.4740 + } 1.4741 + 1.4742 + if (cm->frame_type == KEY_FRAME) 1.4743 + { 1.4744 + /* Tell the caller that the frame was coded as a key frame */ 1.4745 + *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY; 1.4746 + 1.4747 + /* As this frame is a key frame the next defaults to an inter frame. */ 1.4748 + cm->frame_type = INTER_FRAME; 1.4749 + 1.4750 + cpi->last_frame_percent_intra = 100; 1.4751 + } 1.4752 + else 1.4753 + { 1.4754 + *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY; 1.4755 + 1.4756 + cpi->last_frame_percent_intra = cpi->this_frame_percent_intra; 1.4757 + } 1.4758 + 1.4759 + /* Clear the one shot update flags for segmentation map and mode/ref 1.4760 + * loop filter deltas. 1.4761 + */ 1.4762 + cpi->mb.e_mbd.update_mb_segmentation_map = 0; 1.4763 + cpi->mb.e_mbd.update_mb_segmentation_data = 0; 1.4764 + cpi->mb.e_mbd.mode_ref_lf_delta_update = 0; 1.4765 + 1.4766 + 1.4767 + /* Dont increment frame counters if this was an altref buffer update 1.4768 + * not a real frame 1.4769 + */ 1.4770 + if (cm->show_frame) 1.4771 + { 1.4772 + cm->current_video_frame++; 1.4773 + cpi->frames_since_key++; 1.4774 + cpi->temporal_pattern_counter++; 1.4775 + } 1.4776 + 1.4777 + /* reset to normal state now that we are done. */ 1.4778 + 1.4779 + 1.4780 + 1.4781 +#if 0 1.4782 + { 1.4783 + char filename[512]; 1.4784 + FILE *recon_file; 1.4785 + sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame); 1.4786 + recon_file = fopen(filename, "wb"); 1.4787 + fwrite(cm->yv12_fb[cm->lst_fb_idx].buffer_alloc, 1.4788 + cm->yv12_fb[cm->lst_fb_idx].frame_size, 1, recon_file); 1.4789 + fclose(recon_file); 1.4790 + } 1.4791 +#endif 1.4792 + 1.4793 + /* DEBUG */ 1.4794 + /* vp8_write_yuv_frame("encoder_recon.yuv", cm->frame_to_show); */ 1.4795 + 1.4796 + 1.4797 +} 1.4798 +#if !(CONFIG_REALTIME_ONLY) 1.4799 +static void Pass2Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned char * dest_end, unsigned int *frame_flags) 1.4800 +{ 1.4801 + 1.4802 + if (!cpi->common.refresh_alt_ref_frame) 1.4803 + vp8_second_pass(cpi); 1.4804 + 1.4805 + encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags); 1.4806 + cpi->twopass.bits_left -= 8 * *size; 1.4807 + 1.4808 + if (!cpi->common.refresh_alt_ref_frame) 1.4809 + { 1.4810 + double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth 1.4811 + *cpi->oxcf.two_pass_vbrmin_section / 100); 1.4812 + cpi->twopass.bits_left += (int64_t)(two_pass_min_rate / cpi->framerate); 1.4813 + } 1.4814 +} 1.4815 +#endif 1.4816 + 1.4817 +/* For ARM NEON, d8-d15 are callee-saved registers, and need to be saved. */ 1.4818 +#if HAVE_NEON 1.4819 +extern void vp8_push_neon(int64_t *store); 1.4820 +extern void vp8_pop_neon(int64_t *store); 1.4821 +#endif 1.4822 + 1.4823 + 1.4824 +int vp8_receive_raw_frame(VP8_COMP *cpi, unsigned int frame_flags, YV12_BUFFER_CONFIG *sd, int64_t time_stamp, int64_t end_time) 1.4825 +{ 1.4826 +#if HAVE_NEON 1.4827 + int64_t store_reg[8]; 1.4828 +#if CONFIG_RUNTIME_CPU_DETECT 1.4829 + VP8_COMMON *cm = &cpi->common; 1.4830 +#endif 1.4831 +#endif 1.4832 + struct vpx_usec_timer timer; 1.4833 + int res = 0; 1.4834 + 1.4835 +#if HAVE_NEON 1.4836 +#if CONFIG_RUNTIME_CPU_DETECT 1.4837 + if (cm->cpu_caps & HAS_NEON) 1.4838 +#endif 1.4839 + { 1.4840 + vp8_push_neon(store_reg); 1.4841 + } 1.4842 +#endif 1.4843 + 1.4844 + vpx_usec_timer_start(&timer); 1.4845 + 1.4846 + /* Reinit the lookahead buffer if the frame size changes */ 1.4847 + if (sd->y_width != cpi->oxcf.Width || sd->y_height != cpi->oxcf.Height) 1.4848 + { 1.4849 + assert(cpi->oxcf.lag_in_frames < 2); 1.4850 + dealloc_raw_frame_buffers(cpi); 1.4851 + alloc_raw_frame_buffers(cpi); 1.4852 + } 1.4853 + 1.4854 + if(vp8_lookahead_push(cpi->lookahead, sd, time_stamp, end_time, 1.4855 + frame_flags, cpi->active_map_enabled ? cpi->active_map : NULL)) 1.4856 + res = -1; 1.4857 + vpx_usec_timer_mark(&timer); 1.4858 + cpi->time_receive_data += vpx_usec_timer_elapsed(&timer); 1.4859 + 1.4860 +#if HAVE_NEON 1.4861 +#if CONFIG_RUNTIME_CPU_DETECT 1.4862 + if (cm->cpu_caps & HAS_NEON) 1.4863 +#endif 1.4864 + { 1.4865 + vp8_pop_neon(store_reg); 1.4866 + } 1.4867 +#endif 1.4868 + 1.4869 + return res; 1.4870 +} 1.4871 + 1.4872 + 1.4873 +static int frame_is_reference(const VP8_COMP *cpi) 1.4874 +{ 1.4875 + const VP8_COMMON *cm = &cpi->common; 1.4876 + const MACROBLOCKD *xd = &cpi->mb.e_mbd; 1.4877 + 1.4878 + return cm->frame_type == KEY_FRAME || cm->refresh_last_frame 1.4879 + || cm->refresh_golden_frame || cm->refresh_alt_ref_frame 1.4880 + || cm->copy_buffer_to_gf || cm->copy_buffer_to_arf 1.4881 + || cm->refresh_entropy_probs 1.4882 + || xd->mode_ref_lf_delta_update 1.4883 + || xd->update_mb_segmentation_map || xd->update_mb_segmentation_data; 1.4884 +} 1.4885 + 1.4886 + 1.4887 +int vp8_get_compressed_data(VP8_COMP *cpi, unsigned int *frame_flags, unsigned long *size, unsigned char *dest, unsigned char *dest_end, int64_t *time_stamp, int64_t *time_end, int flush) 1.4888 +{ 1.4889 +#if HAVE_NEON 1.4890 + int64_t store_reg[8]; 1.4891 +#endif 1.4892 + VP8_COMMON *cm; 1.4893 + struct vpx_usec_timer tsctimer; 1.4894 + struct vpx_usec_timer ticktimer; 1.4895 + struct vpx_usec_timer cmptimer; 1.4896 + YV12_BUFFER_CONFIG *force_src_buffer = NULL; 1.4897 + 1.4898 + if (!cpi) 1.4899 + return -1; 1.4900 + 1.4901 + cm = &cpi->common; 1.4902 + 1.4903 + if (setjmp(cpi->common.error.jmp)) 1.4904 + { 1.4905 + cpi->common.error.setjmp = 0; 1.4906 + return VPX_CODEC_CORRUPT_FRAME; 1.4907 + } 1.4908 + 1.4909 + cpi->common.error.setjmp = 1; 1.4910 + 1.4911 +#if HAVE_NEON 1.4912 +#if CONFIG_RUNTIME_CPU_DETECT 1.4913 + if (cm->cpu_caps & HAS_NEON) 1.4914 +#endif 1.4915 + { 1.4916 + vp8_push_neon(store_reg); 1.4917 + } 1.4918 +#endif 1.4919 + 1.4920 + vpx_usec_timer_start(&cmptimer); 1.4921 + 1.4922 + cpi->source = NULL; 1.4923 + 1.4924 +#if !(CONFIG_REALTIME_ONLY) 1.4925 + /* Should we code an alternate reference frame */ 1.4926 + if (cpi->oxcf.error_resilient_mode == 0 && 1.4927 + cpi->oxcf.play_alternate && 1.4928 + cpi->source_alt_ref_pending) 1.4929 + { 1.4930 + if ((cpi->source = vp8_lookahead_peek(cpi->lookahead, 1.4931 + cpi->frames_till_gf_update_due, 1.4932 + PEEK_FORWARD))) 1.4933 + { 1.4934 + cpi->alt_ref_source = cpi->source; 1.4935 + if (cpi->oxcf.arnr_max_frames > 0) 1.4936 + { 1.4937 + vp8_temporal_filter_prepare_c(cpi, 1.4938 + cpi->frames_till_gf_update_due); 1.4939 + force_src_buffer = &cpi->alt_ref_buffer; 1.4940 + } 1.4941 + cpi->frames_till_alt_ref_frame = cpi->frames_till_gf_update_due; 1.4942 + cm->refresh_alt_ref_frame = 1; 1.4943 + cm->refresh_golden_frame = 0; 1.4944 + cm->refresh_last_frame = 0; 1.4945 + cm->show_frame = 0; 1.4946 + /* Clear Pending alt Ref flag. */ 1.4947 + cpi->source_alt_ref_pending = 0; 1.4948 + cpi->is_src_frame_alt_ref = 0; 1.4949 + } 1.4950 + } 1.4951 +#endif 1.4952 + 1.4953 + if (!cpi->source) 1.4954 + { 1.4955 + /* Read last frame source if we are encoding first pass. */ 1.4956 + if (cpi->pass == 1 && cm->current_video_frame > 0) 1.4957 + { 1.4958 + if((cpi->last_source = vp8_lookahead_peek(cpi->lookahead, 1, 1.4959 + PEEK_BACKWARD)) == NULL) 1.4960 + return -1; 1.4961 + } 1.4962 + 1.4963 + 1.4964 + if ((cpi->source = vp8_lookahead_pop(cpi->lookahead, flush))) 1.4965 + { 1.4966 + cm->show_frame = 1; 1.4967 + 1.4968 + cpi->is_src_frame_alt_ref = cpi->alt_ref_source 1.4969 + && (cpi->source == cpi->alt_ref_source); 1.4970 + 1.4971 + if(cpi->is_src_frame_alt_ref) 1.4972 + cpi->alt_ref_source = NULL; 1.4973 + } 1.4974 + } 1.4975 + 1.4976 + if (cpi->source) 1.4977 + { 1.4978 + cpi->Source = force_src_buffer ? force_src_buffer : &cpi->source->img; 1.4979 + cpi->un_scaled_source = cpi->Source; 1.4980 + *time_stamp = cpi->source->ts_start; 1.4981 + *time_end = cpi->source->ts_end; 1.4982 + *frame_flags = cpi->source->flags; 1.4983 + 1.4984 + if (cpi->pass == 1 && cm->current_video_frame > 0) 1.4985 + { 1.4986 + cpi->last_frame_unscaled_source = &cpi->last_source->img; 1.4987 + } 1.4988 + } 1.4989 + else 1.4990 + { 1.4991 + *size = 0; 1.4992 +#if !(CONFIG_REALTIME_ONLY) 1.4993 + 1.4994 + if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done) 1.4995 + { 1.4996 + vp8_end_first_pass(cpi); /* get last stats packet */ 1.4997 + cpi->twopass.first_pass_done = 1; 1.4998 + } 1.4999 + 1.5000 +#endif 1.5001 + 1.5002 +#if HAVE_NEON 1.5003 +#if CONFIG_RUNTIME_CPU_DETECT 1.5004 + if (cm->cpu_caps & HAS_NEON) 1.5005 +#endif 1.5006 + { 1.5007 + vp8_pop_neon(store_reg); 1.5008 + } 1.5009 +#endif 1.5010 + return -1; 1.5011 + } 1.5012 + 1.5013 + if (cpi->source->ts_start < cpi->first_time_stamp_ever) 1.5014 + { 1.5015 + cpi->first_time_stamp_ever = cpi->source->ts_start; 1.5016 + cpi->last_end_time_stamp_seen = cpi->source->ts_start; 1.5017 + } 1.5018 + 1.5019 + /* adjust frame rates based on timestamps given */ 1.5020 + if (cm->show_frame) 1.5021 + { 1.5022 + int64_t this_duration; 1.5023 + int step = 0; 1.5024 + 1.5025 + if (cpi->source->ts_start == cpi->first_time_stamp_ever) 1.5026 + { 1.5027 + this_duration = cpi->source->ts_end - cpi->source->ts_start; 1.5028 + step = 1; 1.5029 + } 1.5030 + else 1.5031 + { 1.5032 + int64_t last_duration; 1.5033 + 1.5034 + this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen; 1.5035 + last_duration = cpi->last_end_time_stamp_seen 1.5036 + - cpi->last_time_stamp_seen; 1.5037 + /* do a step update if the duration changes by 10% */ 1.5038 + if (last_duration) 1.5039 + step = (int)(((this_duration - last_duration) * 1.5040 + 10 / last_duration)); 1.5041 + } 1.5042 + 1.5043 + if (this_duration) 1.5044 + { 1.5045 + if (step) 1.5046 + cpi->ref_framerate = 10000000.0 / this_duration; 1.5047 + else 1.5048 + { 1.5049 + double avg_duration, interval; 1.5050 + 1.5051 + /* Average this frame's rate into the last second's average 1.5052 + * frame rate. If we haven't seen 1 second yet, then average 1.5053 + * over the whole interval seen. 1.5054 + */ 1.5055 + interval = (double)(cpi->source->ts_end - 1.5056 + cpi->first_time_stamp_ever); 1.5057 + if(interval > 10000000.0) 1.5058 + interval = 10000000; 1.5059 + 1.5060 + avg_duration = 10000000.0 / cpi->ref_framerate; 1.5061 + avg_duration *= (interval - avg_duration + this_duration); 1.5062 + avg_duration /= interval; 1.5063 + 1.5064 + cpi->ref_framerate = 10000000.0 / avg_duration; 1.5065 + } 1.5066 + 1.5067 + if (cpi->oxcf.number_of_layers > 1) 1.5068 + { 1.5069 + unsigned int i; 1.5070 + 1.5071 + /* Update frame rates for each layer */ 1.5072 + for (i=0; i<cpi->oxcf.number_of_layers; i++) 1.5073 + { 1.5074 + LAYER_CONTEXT *lc = &cpi->layer_context[i]; 1.5075 + lc->framerate = cpi->ref_framerate / 1.5076 + cpi->oxcf.rate_decimator[i]; 1.5077 + } 1.5078 + } 1.5079 + else 1.5080 + vp8_new_framerate(cpi, cpi->ref_framerate); 1.5081 + } 1.5082 + 1.5083 + cpi->last_time_stamp_seen = cpi->source->ts_start; 1.5084 + cpi->last_end_time_stamp_seen = cpi->source->ts_end; 1.5085 + } 1.5086 + 1.5087 + if (cpi->oxcf.number_of_layers > 1) 1.5088 + { 1.5089 + int layer; 1.5090 + 1.5091 + update_layer_contexts (cpi); 1.5092 + 1.5093 + /* Restore layer specific context & set frame rate */ 1.5094 + layer = cpi->oxcf.layer_id[ 1.5095 + cpi->temporal_pattern_counter % cpi->oxcf.periodicity]; 1.5096 + restore_layer_context (cpi, layer); 1.5097 + vp8_new_framerate(cpi, cpi->layer_context[layer].framerate); 1.5098 + } 1.5099 + 1.5100 + if (cpi->compressor_speed == 2) 1.5101 + { 1.5102 + vpx_usec_timer_start(&tsctimer); 1.5103 + vpx_usec_timer_start(&ticktimer); 1.5104 + } 1.5105 + 1.5106 + cpi->lf_zeromv_pct = (cpi->zeromv_count * 100)/cm->MBs; 1.5107 + 1.5108 +#if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING 1.5109 + { 1.5110 + int i; 1.5111 + const int num_part = (1 << cm->multi_token_partition); 1.5112 + /* the available bytes in dest */ 1.5113 + const unsigned long dest_size = dest_end - dest; 1.5114 + const int tok_part_buff_size = (dest_size * 9) / (10 * num_part); 1.5115 + 1.5116 + unsigned char *dp = dest; 1.5117 + 1.5118 + cpi->partition_d[0] = dp; 1.5119 + dp += dest_size/10; /* reserve 1/10 for control partition */ 1.5120 + cpi->partition_d_end[0] = dp; 1.5121 + 1.5122 + for(i = 0; i < num_part; i++) 1.5123 + { 1.5124 + cpi->partition_d[i + 1] = dp; 1.5125 + dp += tok_part_buff_size; 1.5126 + cpi->partition_d_end[i + 1] = dp; 1.5127 + } 1.5128 + } 1.5129 +#endif 1.5130 + 1.5131 + /* start with a 0 size frame */ 1.5132 + *size = 0; 1.5133 + 1.5134 + /* Clear down mmx registers */ 1.5135 + vp8_clear_system_state(); 1.5136 + 1.5137 + cm->frame_type = INTER_FRAME; 1.5138 + cm->frame_flags = *frame_flags; 1.5139 + 1.5140 +#if 0 1.5141 + 1.5142 + if (cm->refresh_alt_ref_frame) 1.5143 + { 1.5144 + cm->refresh_golden_frame = 0; 1.5145 + cm->refresh_last_frame = 0; 1.5146 + } 1.5147 + else 1.5148 + { 1.5149 + cm->refresh_golden_frame = 0; 1.5150 + cm->refresh_last_frame = 1; 1.5151 + } 1.5152 + 1.5153 +#endif 1.5154 + /* find a free buffer for the new frame */ 1.5155 + { 1.5156 + int i = 0; 1.5157 + for(; i < NUM_YV12_BUFFERS; i++) 1.5158 + { 1.5159 + if(!cm->yv12_fb[i].flags) 1.5160 + { 1.5161 + cm->new_fb_idx = i; 1.5162 + break; 1.5163 + } 1.5164 + } 1.5165 + 1.5166 + assert(i < NUM_YV12_BUFFERS ); 1.5167 + } 1.5168 +#if !(CONFIG_REALTIME_ONLY) 1.5169 + 1.5170 + if (cpi->pass == 1) 1.5171 + { 1.5172 + Pass1Encode(cpi, size, dest, frame_flags); 1.5173 + } 1.5174 + else if (cpi->pass == 2) 1.5175 + { 1.5176 + Pass2Encode(cpi, size, dest, dest_end, frame_flags); 1.5177 + } 1.5178 + else 1.5179 +#endif 1.5180 + encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags); 1.5181 + 1.5182 + if (cpi->compressor_speed == 2) 1.5183 + { 1.5184 + unsigned int duration, duration2; 1.5185 + vpx_usec_timer_mark(&tsctimer); 1.5186 + vpx_usec_timer_mark(&ticktimer); 1.5187 + 1.5188 + duration = (int)(vpx_usec_timer_elapsed(&ticktimer)); 1.5189 + duration2 = (unsigned int)((double)duration / 2); 1.5190 + 1.5191 + if (cm->frame_type != KEY_FRAME) 1.5192 + { 1.5193 + if (cpi->avg_encode_time == 0) 1.5194 + cpi->avg_encode_time = duration; 1.5195 + else 1.5196 + cpi->avg_encode_time = (7 * cpi->avg_encode_time + duration) >> 3; 1.5197 + } 1.5198 + 1.5199 + if (duration2) 1.5200 + { 1.5201 + { 1.5202 + 1.5203 + if (cpi->avg_pick_mode_time == 0) 1.5204 + cpi->avg_pick_mode_time = duration2; 1.5205 + else 1.5206 + cpi->avg_pick_mode_time = (7 * cpi->avg_pick_mode_time + duration2) >> 3; 1.5207 + } 1.5208 + } 1.5209 + 1.5210 + } 1.5211 + 1.5212 + if (cm->refresh_entropy_probs == 0) 1.5213 + { 1.5214 + vpx_memcpy(&cm->fc, &cm->lfc, sizeof(cm->fc)); 1.5215 + } 1.5216 + 1.5217 + /* Save the contexts separately for alt ref, gold and last. */ 1.5218 + /* (TODO jbb -> Optimize this with pointers to avoid extra copies. ) */ 1.5219 + if(cm->refresh_alt_ref_frame) 1.5220 + vpx_memcpy(&cpi->lfc_a, &cm->fc, sizeof(cm->fc)); 1.5221 + 1.5222 + if(cm->refresh_golden_frame) 1.5223 + vpx_memcpy(&cpi->lfc_g, &cm->fc, sizeof(cm->fc)); 1.5224 + 1.5225 + if(cm->refresh_last_frame) 1.5226 + vpx_memcpy(&cpi->lfc_n, &cm->fc, sizeof(cm->fc)); 1.5227 + 1.5228 + /* if its a dropped frame honor the requests on subsequent frames */ 1.5229 + if (*size > 0) 1.5230 + { 1.5231 + cpi->droppable = !frame_is_reference(cpi); 1.5232 + 1.5233 + /* return to normal state */ 1.5234 + cm->refresh_entropy_probs = 1; 1.5235 + cm->refresh_alt_ref_frame = 0; 1.5236 + cm->refresh_golden_frame = 0; 1.5237 + cm->refresh_last_frame = 1; 1.5238 + cm->frame_type = INTER_FRAME; 1.5239 + 1.5240 + } 1.5241 + 1.5242 + /* Save layer specific state */ 1.5243 + if (cpi->oxcf.number_of_layers > 1) 1.5244 + save_layer_context (cpi); 1.5245 + 1.5246 + vpx_usec_timer_mark(&cmptimer); 1.5247 + cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer); 1.5248 + 1.5249 + if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame) 1.5250 + { 1.5251 + generate_psnr_packet(cpi); 1.5252 + } 1.5253 + 1.5254 +#if CONFIG_INTERNAL_STATS 1.5255 + 1.5256 + if (cpi->pass != 1) 1.5257 + { 1.5258 + cpi->bytes += *size; 1.5259 + 1.5260 + if (cm->show_frame) 1.5261 + { 1.5262 + cpi->common.show_frame_mi = cpi->common.mi; 1.5263 + cpi->count ++; 1.5264 + 1.5265 + if (cpi->b_calculate_psnr) 1.5266 + { 1.5267 + uint64_t ye,ue,ve; 1.5268 + double frame_psnr; 1.5269 + YV12_BUFFER_CONFIG *orig = cpi->Source; 1.5270 + YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show; 1.5271 + int y_samples = orig->y_height * orig->y_width ; 1.5272 + int uv_samples = orig->uv_height * orig->uv_width ; 1.5273 + int t_samples = y_samples + 2 * uv_samples; 1.5274 + double sq_error, sq_error2; 1.5275 + 1.5276 + ye = calc_plane_error(orig->y_buffer, orig->y_stride, 1.5277 + recon->y_buffer, recon->y_stride, orig->y_width, orig->y_height); 1.5278 + 1.5279 + ue = calc_plane_error(orig->u_buffer, orig->uv_stride, 1.5280 + recon->u_buffer, recon->uv_stride, orig->uv_width, orig->uv_height); 1.5281 + 1.5282 + ve = calc_plane_error(orig->v_buffer, orig->uv_stride, 1.5283 + recon->v_buffer, recon->uv_stride, orig->uv_width, orig->uv_height); 1.5284 + 1.5285 + sq_error = (double)(ye + ue + ve); 1.5286 + 1.5287 + frame_psnr = vp8_mse2psnr(t_samples, 255.0, sq_error); 1.5288 + 1.5289 + cpi->total_y += vp8_mse2psnr(y_samples, 255.0, (double)ye); 1.5290 + cpi->total_u += vp8_mse2psnr(uv_samples, 255.0, (double)ue); 1.5291 + cpi->total_v += vp8_mse2psnr(uv_samples, 255.0, (double)ve); 1.5292 + cpi->total_sq_error += sq_error; 1.5293 + cpi->total += frame_psnr; 1.5294 +#if CONFIG_POSTPROC 1.5295 + { 1.5296 + YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer; 1.5297 + double frame_psnr2, frame_ssim2 = 0; 1.5298 + double weight = 0; 1.5299 + 1.5300 + vp8_deblock(cm, cm->frame_to_show, &cm->post_proc_buffer, cm->filter_level * 10 / 6, 1, 0); 1.5301 + vp8_clear_system_state(); 1.5302 + 1.5303 + ye = calc_plane_error(orig->y_buffer, orig->y_stride, 1.5304 + pp->y_buffer, pp->y_stride, orig->y_width, orig->y_height); 1.5305 + 1.5306 + ue = calc_plane_error(orig->u_buffer, orig->uv_stride, 1.5307 + pp->u_buffer, pp->uv_stride, orig->uv_width, orig->uv_height); 1.5308 + 1.5309 + ve = calc_plane_error(orig->v_buffer, orig->uv_stride, 1.5310 + pp->v_buffer, pp->uv_stride, orig->uv_width, orig->uv_height); 1.5311 + 1.5312 + sq_error2 = (double)(ye + ue + ve); 1.5313 + 1.5314 + frame_psnr2 = vp8_mse2psnr(t_samples, 255.0, sq_error2); 1.5315 + 1.5316 + cpi->totalp_y += vp8_mse2psnr(y_samples, 1.5317 + 255.0, (double)ye); 1.5318 + cpi->totalp_u += vp8_mse2psnr(uv_samples, 1.5319 + 255.0, (double)ue); 1.5320 + cpi->totalp_v += vp8_mse2psnr(uv_samples, 1.5321 + 255.0, (double)ve); 1.5322 + cpi->total_sq_error2 += sq_error2; 1.5323 + cpi->totalp += frame_psnr2; 1.5324 + 1.5325 + frame_ssim2 = vp8_calc_ssim(cpi->Source, 1.5326 + &cm->post_proc_buffer, 1, &weight); 1.5327 + 1.5328 + cpi->summed_quality += frame_ssim2 * weight; 1.5329 + cpi->summed_weights += weight; 1.5330 + 1.5331 + if (cpi->oxcf.number_of_layers > 1) 1.5332 + { 1.5333 + unsigned int i; 1.5334 + 1.5335 + for (i=cpi->current_layer; 1.5336 + i<cpi->oxcf.number_of_layers; i++) 1.5337 + { 1.5338 + cpi->frames_in_layer[i]++; 1.5339 + 1.5340 + cpi->bytes_in_layer[i] += *size; 1.5341 + cpi->sum_psnr[i] += frame_psnr; 1.5342 + cpi->sum_psnr_p[i] += frame_psnr2; 1.5343 + cpi->total_error2[i] += sq_error; 1.5344 + cpi->total_error2_p[i] += sq_error2; 1.5345 + cpi->sum_ssim[i] += frame_ssim2 * weight; 1.5346 + cpi->sum_weights[i] += weight; 1.5347 + } 1.5348 + } 1.5349 + } 1.5350 +#endif 1.5351 + } 1.5352 + 1.5353 + if (cpi->b_calculate_ssimg) 1.5354 + { 1.5355 + double y, u, v, frame_all; 1.5356 + frame_all = vp8_calc_ssimg(cpi->Source, cm->frame_to_show, 1.5357 + &y, &u, &v); 1.5358 + 1.5359 + if (cpi->oxcf.number_of_layers > 1) 1.5360 + { 1.5361 + unsigned int i; 1.5362 + 1.5363 + for (i=cpi->current_layer; 1.5364 + i<cpi->oxcf.number_of_layers; i++) 1.5365 + { 1.5366 + if (!cpi->b_calculate_psnr) 1.5367 + cpi->frames_in_layer[i]++; 1.5368 + 1.5369 + cpi->total_ssimg_y_in_layer[i] += y; 1.5370 + cpi->total_ssimg_u_in_layer[i] += u; 1.5371 + cpi->total_ssimg_v_in_layer[i] += v; 1.5372 + cpi->total_ssimg_all_in_layer[i] += frame_all; 1.5373 + } 1.5374 + } 1.5375 + else 1.5376 + { 1.5377 + cpi->total_ssimg_y += y; 1.5378 + cpi->total_ssimg_u += u; 1.5379 + cpi->total_ssimg_v += v; 1.5380 + cpi->total_ssimg_all += frame_all; 1.5381 + } 1.5382 + } 1.5383 + 1.5384 + } 1.5385 + } 1.5386 + 1.5387 +#if 0 1.5388 + 1.5389 + if (cpi->common.frame_type != 0 && cpi->common.base_qindex == cpi->oxcf.worst_allowed_q) 1.5390 + { 1.5391 + skiptruecount += cpi->skip_true_count; 1.5392 + skipfalsecount += cpi->skip_false_count; 1.5393 + } 1.5394 + 1.5395 +#endif 1.5396 +#if 0 1.5397 + 1.5398 + if (cpi->pass != 1) 1.5399 + { 1.5400 + FILE *f = fopen("skip.stt", "a"); 1.5401 + fprintf(f, "frame:%4d flags:%4x Q:%4d P:%4d Size:%5d\n", cpi->common.current_video_frame, *frame_flags, cpi->common.base_qindex, cpi->prob_skip_false, *size); 1.5402 + 1.5403 + if (cpi->is_src_frame_alt_ref == 1) 1.5404 + fprintf(f, "skipcount: %4d framesize: %d\n", cpi->skip_true_count , *size); 1.5405 + 1.5406 + fclose(f); 1.5407 + } 1.5408 + 1.5409 +#endif 1.5410 +#endif 1.5411 + 1.5412 +#if HAVE_NEON 1.5413 +#if CONFIG_RUNTIME_CPU_DETECT 1.5414 + if (cm->cpu_caps & HAS_NEON) 1.5415 +#endif 1.5416 + { 1.5417 + vp8_pop_neon(store_reg); 1.5418 + } 1.5419 +#endif 1.5420 + 1.5421 + cpi->common.error.setjmp = 0; 1.5422 + 1.5423 + return 0; 1.5424 +} 1.5425 + 1.5426 +int vp8_get_preview_raw_frame(VP8_COMP *cpi, YV12_BUFFER_CONFIG *dest, vp8_ppflags_t *flags) 1.5427 +{ 1.5428 + if (cpi->common.refresh_alt_ref_frame) 1.5429 + return -1; 1.5430 + else 1.5431 + { 1.5432 + int ret; 1.5433 + 1.5434 +#if CONFIG_MULTITHREAD 1.5435 + if(cpi->b_lpf_running) 1.5436 + { 1.5437 + sem_wait(&cpi->h_event_end_lpf); 1.5438 + cpi->b_lpf_running = 0; 1.5439 + } 1.5440 +#endif 1.5441 + 1.5442 +#if CONFIG_POSTPROC 1.5443 + cpi->common.show_frame_mi = cpi->common.mi; 1.5444 + ret = vp8_post_proc_frame(&cpi->common, dest, flags); 1.5445 +#else 1.5446 + 1.5447 + if (cpi->common.frame_to_show) 1.5448 + { 1.5449 + *dest = *cpi->common.frame_to_show; 1.5450 + dest->y_width = cpi->common.Width; 1.5451 + dest->y_height = cpi->common.Height; 1.5452 + dest->uv_height = cpi->common.Height / 2; 1.5453 + ret = 0; 1.5454 + } 1.5455 + else 1.5456 + { 1.5457 + ret = -1; 1.5458 + } 1.5459 + 1.5460 +#endif 1.5461 + vp8_clear_system_state(); 1.5462 + return ret; 1.5463 + } 1.5464 +} 1.5465 + 1.5466 +int vp8_set_roimap(VP8_COMP *cpi, unsigned char *map, unsigned int rows, unsigned int cols, int delta_q[4], int delta_lf[4], unsigned int threshold[4]) 1.5467 +{ 1.5468 + signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS]; 1.5469 + int internal_delta_q[MAX_MB_SEGMENTS]; 1.5470 + const int range = 63; 1.5471 + int i; 1.5472 + 1.5473 + // This method is currently incompatible with the cyclic refresh method 1.5474 + if ( cpi->cyclic_refresh_mode_enabled ) 1.5475 + return -1; 1.5476 + 1.5477 + // Check number of rows and columns match 1.5478 + if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols) 1.5479 + return -1; 1.5480 + 1.5481 + // Range check the delta Q values and convert the external Q range values 1.5482 + // to internal ones. 1.5483 + if ( (abs(delta_q[0]) > range) || (abs(delta_q[1]) > range) || 1.5484 + (abs(delta_q[2]) > range) || (abs(delta_q[3]) > range) ) 1.5485 + return -1; 1.5486 + 1.5487 + // Range check the delta lf values 1.5488 + if ( (abs(delta_lf[0]) > range) || (abs(delta_lf[1]) > range) || 1.5489 + (abs(delta_lf[2]) > range) || (abs(delta_lf[3]) > range) ) 1.5490 + return -1; 1.5491 + 1.5492 + if (!map) 1.5493 + { 1.5494 + disable_segmentation(cpi); 1.5495 + return 0; 1.5496 + } 1.5497 + 1.5498 + // Translate the external delta q values to internal values. 1.5499 + for ( i = 0; i < MAX_MB_SEGMENTS; i++ ) 1.5500 + internal_delta_q[i] = 1.5501 + ( delta_q[i] >= 0 ) ? q_trans[delta_q[i]] : -q_trans[-delta_q[i]]; 1.5502 + 1.5503 + /* Set the segmentation Map */ 1.5504 + set_segmentation_map(cpi, map); 1.5505 + 1.5506 + /* Activate segmentation. */ 1.5507 + enable_segmentation(cpi); 1.5508 + 1.5509 + /* Set up the quant segment data */ 1.5510 + feature_data[MB_LVL_ALT_Q][0] = internal_delta_q[0]; 1.5511 + feature_data[MB_LVL_ALT_Q][1] = internal_delta_q[1]; 1.5512 + feature_data[MB_LVL_ALT_Q][2] = internal_delta_q[2]; 1.5513 + feature_data[MB_LVL_ALT_Q][3] = internal_delta_q[3]; 1.5514 + 1.5515 + /* Set up the loop segment data s */ 1.5516 + feature_data[MB_LVL_ALT_LF][0] = delta_lf[0]; 1.5517 + feature_data[MB_LVL_ALT_LF][1] = delta_lf[1]; 1.5518 + feature_data[MB_LVL_ALT_LF][2] = delta_lf[2]; 1.5519 + feature_data[MB_LVL_ALT_LF][3] = delta_lf[3]; 1.5520 + 1.5521 + cpi->segment_encode_breakout[0] = threshold[0]; 1.5522 + cpi->segment_encode_breakout[1] = threshold[1]; 1.5523 + cpi->segment_encode_breakout[2] = threshold[2]; 1.5524 + cpi->segment_encode_breakout[3] = threshold[3]; 1.5525 + 1.5526 + /* Initialise the feature data structure */ 1.5527 + set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA); 1.5528 + 1.5529 + return 0; 1.5530 +} 1.5531 + 1.5532 +int vp8_set_active_map(VP8_COMP *cpi, unsigned char *map, unsigned int rows, unsigned int cols) 1.5533 +{ 1.5534 + if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) 1.5535 + { 1.5536 + if (map) 1.5537 + { 1.5538 + vpx_memcpy(cpi->active_map, map, rows * cols); 1.5539 + cpi->active_map_enabled = 1; 1.5540 + } 1.5541 + else 1.5542 + cpi->active_map_enabled = 0; 1.5543 + 1.5544 + return 0; 1.5545 + } 1.5546 + else 1.5547 + { 1.5548 + return -1 ; 1.5549 + } 1.5550 +} 1.5551 + 1.5552 +int vp8_set_internal_size(VP8_COMP *cpi, VPX_SCALING horiz_mode, VPX_SCALING vert_mode) 1.5553 +{ 1.5554 + if (horiz_mode <= ONETWO) 1.5555 + cpi->common.horiz_scale = horiz_mode; 1.5556 + else 1.5557 + return -1; 1.5558 + 1.5559 + if (vert_mode <= ONETWO) 1.5560 + cpi->common.vert_scale = vert_mode; 1.5561 + else 1.5562 + return -1; 1.5563 + 1.5564 + return 0; 1.5565 +} 1.5566 + 1.5567 + 1.5568 + 1.5569 +int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest) 1.5570 +{ 1.5571 + int i, j; 1.5572 + int Total = 0; 1.5573 + 1.5574 + unsigned char *src = source->y_buffer; 1.5575 + unsigned char *dst = dest->y_buffer; 1.5576 + 1.5577 + /* Loop through the Y plane raw and reconstruction data summing 1.5578 + * (square differences) 1.5579 + */ 1.5580 + for (i = 0; i < source->y_height; i += 16) 1.5581 + { 1.5582 + for (j = 0; j < source->y_width; j += 16) 1.5583 + { 1.5584 + unsigned int sse; 1.5585 + Total += vp8_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride, &sse); 1.5586 + } 1.5587 + 1.5588 + src += 16 * source->y_stride; 1.5589 + dst += 16 * dest->y_stride; 1.5590 + } 1.5591 + 1.5592 + return Total; 1.5593 +} 1.5594 + 1.5595 + 1.5596 +int vp8_get_quantizer(VP8_COMP *cpi) 1.5597 +{ 1.5598 + return cpi->common.base_qindex; 1.5599 +}