media/libvpx/vp8/encoder/onyx_if.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license
michael@0 5 * that can be found in the LICENSE file in the root of the source
michael@0 6 * tree. An additional intellectual property rights grant can be found
michael@0 7 * in the file PATENTS. All contributing project authors may
michael@0 8 * be found in the AUTHORS file in the root of the source tree.
michael@0 9 */
michael@0 10
michael@0 11
michael@0 12 #include "vpx_config.h"
michael@0 13 #include "./vpx_scale_rtcd.h"
michael@0 14 #include "vp8/common/onyxc_int.h"
michael@0 15 #include "vp8/common/blockd.h"
michael@0 16 #include "onyx_int.h"
michael@0 17 #include "vp8/common/systemdependent.h"
michael@0 18 #include "quantize.h"
michael@0 19 #include "vp8/common/alloccommon.h"
michael@0 20 #include "mcomp.h"
michael@0 21 #include "firstpass.h"
michael@0 22 #include "psnr.h"
michael@0 23 #include "vpx_scale/vpx_scale.h"
michael@0 24 #include "vp8/common/extend.h"
michael@0 25 #include "ratectrl.h"
michael@0 26 #include "vp8/common/quant_common.h"
michael@0 27 #include "segmentation.h"
michael@0 28 #if CONFIG_POSTPROC
michael@0 29 #include "vp8/common/postproc.h"
michael@0 30 #endif
michael@0 31 #include "vpx_mem/vpx_mem.h"
michael@0 32 #include "vp8/common/swapyv12buffer.h"
michael@0 33 #include "vp8/common/threading.h"
michael@0 34 #include "vpx_ports/vpx_timer.h"
michael@0 35 #if ARCH_ARM
michael@0 36 #include "vpx_ports/arm.h"
michael@0 37 #endif
michael@0 38 #if CONFIG_MULTI_RES_ENCODING
michael@0 39 #include "mr_dissim.h"
michael@0 40 #endif
michael@0 41 #include "encodeframe.h"
michael@0 42
michael@0 43 #include <math.h>
michael@0 44 #include <stdio.h>
michael@0 45 #include <limits.h>
michael@0 46
michael@0 47 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
michael@0 48 extern int vp8_update_coef_context(VP8_COMP *cpi);
michael@0 49 extern void vp8_update_coef_probs(VP8_COMP *cpi);
michael@0 50 #endif
michael@0 51
michael@0 52 extern void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
michael@0 53 extern void vp8cx_set_alt_lf_level(VP8_COMP *cpi, int filt_val);
michael@0 54 extern void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
michael@0 55
michael@0 56 extern void vp8_deblock_frame(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *post, int filt_lvl, int low_var_thresh, int flag);
michael@0 57 extern void print_parms(VP8_CONFIG *ocf, char *filenam);
michael@0 58 extern unsigned int vp8_get_processor_freq();
michael@0 59 extern void print_tree_update_probs();
michael@0 60 extern int vp8cx_create_encoder_threads(VP8_COMP *cpi);
michael@0 61 extern void vp8cx_remove_encoder_threads(VP8_COMP *cpi);
michael@0 62
michael@0 63 int vp8_estimate_entropy_savings(VP8_COMP *cpi);
michael@0 64
michael@0 65 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest);
michael@0 66
michael@0 67 extern void vp8_temporal_filter_prepare_c(VP8_COMP *cpi, int distance);
michael@0 68
michael@0 69 static void set_default_lf_deltas(VP8_COMP *cpi);
michael@0 70
michael@0 71 extern const int vp8_gf_interval_table[101];
michael@0 72
michael@0 73 #if CONFIG_INTERNAL_STATS
michael@0 74 #include "math.h"
michael@0 75
michael@0 76 extern double vp8_calc_ssim
michael@0 77 (
michael@0 78 YV12_BUFFER_CONFIG *source,
michael@0 79 YV12_BUFFER_CONFIG *dest,
michael@0 80 int lumamask,
michael@0 81 double *weight
michael@0 82 );
michael@0 83
michael@0 84
michael@0 85 extern double vp8_calc_ssimg
michael@0 86 (
michael@0 87 YV12_BUFFER_CONFIG *source,
michael@0 88 YV12_BUFFER_CONFIG *dest,
michael@0 89 double *ssim_y,
michael@0 90 double *ssim_u,
michael@0 91 double *ssim_v
michael@0 92 );
michael@0 93
michael@0 94
michael@0 95 #endif
michael@0 96
michael@0 97
michael@0 98 #ifdef OUTPUT_YUV_SRC
michael@0 99 FILE *yuv_file;
michael@0 100 #endif
michael@0 101
michael@0 102 #if 0
michael@0 103 FILE *framepsnr;
michael@0 104 FILE *kf_list;
michael@0 105 FILE *keyfile;
michael@0 106 #endif
michael@0 107
michael@0 108 #if 0
michael@0 109 extern int skip_true_count;
michael@0 110 extern int skip_false_count;
michael@0 111 #endif
michael@0 112
michael@0 113
michael@0 114 #ifdef VP8_ENTROPY_STATS
michael@0 115 extern int intra_mode_stats[10][10][10];
michael@0 116 #endif
michael@0 117
michael@0 118 #ifdef SPEEDSTATS
michael@0 119 unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
michael@0 120 unsigned int tot_pm = 0;
michael@0 121 unsigned int cnt_pm = 0;
michael@0 122 unsigned int tot_ef = 0;
michael@0 123 unsigned int cnt_ef = 0;
michael@0 124 #endif
michael@0 125
michael@0 126 #ifdef MODE_STATS
michael@0 127 extern unsigned __int64 Sectionbits[50];
michael@0 128 extern int y_modes[5] ;
michael@0 129 extern int uv_modes[4] ;
michael@0 130 extern int b_modes[10] ;
michael@0 131
michael@0 132 extern int inter_y_modes[10] ;
michael@0 133 extern int inter_uv_modes[4] ;
michael@0 134 extern unsigned int inter_b_modes[15];
michael@0 135 #endif
michael@0 136
michael@0 137 extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
michael@0 138
michael@0 139 extern const int qrounding_factors[129];
michael@0 140 extern const int qzbin_factors[129];
michael@0 141 extern void vp8cx_init_quantizer(VP8_COMP *cpi);
michael@0 142 extern const int vp8cx_base_skip_false_prob[128];
michael@0 143
michael@0 144 /* Tables relating active max Q to active min Q */
michael@0 145 static const unsigned char kf_low_motion_minq[QINDEX_RANGE] =
michael@0 146 {
michael@0 147 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
michael@0 148 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
michael@0 149 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
michael@0 150 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
michael@0 151 3,3,3,3,3,3,4,4,4,5,5,5,5,5,6,6,
michael@0 152 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
michael@0 153 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
michael@0 154 16,16,17,17,18,18,18,18,19,20,20,21,21,22,23,23
michael@0 155 };
michael@0 156 static const unsigned char kf_high_motion_minq[QINDEX_RANGE] =
michael@0 157 {
michael@0 158 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
michael@0 159 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
michael@0 160 1,1,1,1,1,1,1,1,2,2,2,2,3,3,3,3,
michael@0 161 3,3,3,3,4,4,4,4,5,5,5,5,5,5,6,6,
michael@0 162 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
michael@0 163 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
michael@0 164 16,16,17,17,18,18,18,18,19,19,20,20,20,20,21,21,
michael@0 165 21,21,22,22,23,23,24,25,25,26,26,27,28,28,29,30
michael@0 166 };
michael@0 167 static const unsigned char gf_low_motion_minq[QINDEX_RANGE] =
michael@0 168 {
michael@0 169 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
michael@0 170 3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,
michael@0 171 7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,
michael@0 172 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,
michael@0 173 19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,
michael@0 174 27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,
michael@0 175 35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,
michael@0 176 43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58
michael@0 177 };
michael@0 178 static const unsigned char gf_mid_motion_minq[QINDEX_RANGE] =
michael@0 179 {
michael@0 180 0,0,0,0,1,1,1,1,1,1,2,2,3,3,3,4,
michael@0 181 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
michael@0 182 9,10,10,10,10,11,11,11,12,12,12,12,13,13,13,14,
michael@0 183 14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,
michael@0 184 22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,
michael@0 185 30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,
michael@0 186 38,39,39,40,40,41,41,42,42,43,43,44,45,46,47,48,
michael@0 187 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64
michael@0 188 };
michael@0 189 static const unsigned char gf_high_motion_minq[QINDEX_RANGE] =
michael@0 190 {
michael@0 191 0,0,0,0,1,1,1,1,1,2,2,2,3,3,3,4,
michael@0 192 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
michael@0 193 9,10,10,10,11,11,12,12,13,13,14,14,15,15,16,16,
michael@0 194 17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,
michael@0 195 25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,
michael@0 196 33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,
michael@0 197 41,41,42,42,43,44,45,46,47,48,49,50,51,52,53,54,
michael@0 198 55,56,57,58,59,60,62,64,66,68,70,72,74,76,78,80
michael@0 199 };
michael@0 200 static const unsigned char inter_minq[QINDEX_RANGE] =
michael@0 201 {
michael@0 202 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
michael@0 203 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
michael@0 204 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
michael@0 205 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
michael@0 206 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
michael@0 207 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
michael@0 208 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
michael@0 209 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
michael@0 210 };
michael@0 211
michael@0 212 #ifdef PACKET_TESTING
michael@0 213 extern FILE *vpxlogc;
michael@0 214 #endif
michael@0 215
michael@0 216 static void save_layer_context(VP8_COMP *cpi)
michael@0 217 {
michael@0 218 LAYER_CONTEXT *lc = &cpi->layer_context[cpi->current_layer];
michael@0 219
michael@0 220 /* Save layer dependent coding state */
michael@0 221 lc->target_bandwidth = cpi->target_bandwidth;
michael@0 222 lc->starting_buffer_level = cpi->oxcf.starting_buffer_level;
michael@0 223 lc->optimal_buffer_level = cpi->oxcf.optimal_buffer_level;
michael@0 224 lc->maximum_buffer_size = cpi->oxcf.maximum_buffer_size;
michael@0 225 lc->starting_buffer_level_in_ms = cpi->oxcf.starting_buffer_level_in_ms;
michael@0 226 lc->optimal_buffer_level_in_ms = cpi->oxcf.optimal_buffer_level_in_ms;
michael@0 227 lc->maximum_buffer_size_in_ms = cpi->oxcf.maximum_buffer_size_in_ms;
michael@0 228 lc->buffer_level = cpi->buffer_level;
michael@0 229 lc->bits_off_target = cpi->bits_off_target;
michael@0 230 lc->total_actual_bits = cpi->total_actual_bits;
michael@0 231 lc->worst_quality = cpi->worst_quality;
michael@0 232 lc->active_worst_quality = cpi->active_worst_quality;
michael@0 233 lc->best_quality = cpi->best_quality;
michael@0 234 lc->active_best_quality = cpi->active_best_quality;
michael@0 235 lc->ni_av_qi = cpi->ni_av_qi;
michael@0 236 lc->ni_tot_qi = cpi->ni_tot_qi;
michael@0 237 lc->ni_frames = cpi->ni_frames;
michael@0 238 lc->avg_frame_qindex = cpi->avg_frame_qindex;
michael@0 239 lc->rate_correction_factor = cpi->rate_correction_factor;
michael@0 240 lc->key_frame_rate_correction_factor = cpi->key_frame_rate_correction_factor;
michael@0 241 lc->gf_rate_correction_factor = cpi->gf_rate_correction_factor;
michael@0 242 lc->zbin_over_quant = cpi->mb.zbin_over_quant;
michael@0 243 lc->inter_frame_target = cpi->inter_frame_target;
michael@0 244 lc->total_byte_count = cpi->total_byte_count;
michael@0 245 lc->filter_level = cpi->common.filter_level;
michael@0 246
michael@0 247 lc->last_frame_percent_intra = cpi->last_frame_percent_intra;
michael@0 248
michael@0 249 memcpy (lc->count_mb_ref_frame_usage,
michael@0 250 cpi->mb.count_mb_ref_frame_usage,
michael@0 251 sizeof(cpi->mb.count_mb_ref_frame_usage));
michael@0 252 }
michael@0 253
michael@0 254 static void restore_layer_context(VP8_COMP *cpi, const int layer)
michael@0 255 {
michael@0 256 LAYER_CONTEXT *lc = &cpi->layer_context[layer];
michael@0 257
michael@0 258 /* Restore layer dependent coding state */
michael@0 259 cpi->current_layer = layer;
michael@0 260 cpi->target_bandwidth = lc->target_bandwidth;
michael@0 261 cpi->oxcf.target_bandwidth = lc->target_bandwidth;
michael@0 262 cpi->oxcf.starting_buffer_level = lc->starting_buffer_level;
michael@0 263 cpi->oxcf.optimal_buffer_level = lc->optimal_buffer_level;
michael@0 264 cpi->oxcf.maximum_buffer_size = lc->maximum_buffer_size;
michael@0 265 cpi->oxcf.starting_buffer_level_in_ms = lc->starting_buffer_level_in_ms;
michael@0 266 cpi->oxcf.optimal_buffer_level_in_ms = lc->optimal_buffer_level_in_ms;
michael@0 267 cpi->oxcf.maximum_buffer_size_in_ms = lc->maximum_buffer_size_in_ms;
michael@0 268 cpi->buffer_level = lc->buffer_level;
michael@0 269 cpi->bits_off_target = lc->bits_off_target;
michael@0 270 cpi->total_actual_bits = lc->total_actual_bits;
michael@0 271 cpi->active_worst_quality = lc->active_worst_quality;
michael@0 272 cpi->active_best_quality = lc->active_best_quality;
michael@0 273 cpi->ni_av_qi = lc->ni_av_qi;
michael@0 274 cpi->ni_tot_qi = lc->ni_tot_qi;
michael@0 275 cpi->ni_frames = lc->ni_frames;
michael@0 276 cpi->avg_frame_qindex = lc->avg_frame_qindex;
michael@0 277 cpi->rate_correction_factor = lc->rate_correction_factor;
michael@0 278 cpi->key_frame_rate_correction_factor = lc->key_frame_rate_correction_factor;
michael@0 279 cpi->gf_rate_correction_factor = lc->gf_rate_correction_factor;
michael@0 280 cpi->mb.zbin_over_quant = lc->zbin_over_quant;
michael@0 281 cpi->inter_frame_target = lc->inter_frame_target;
michael@0 282 cpi->total_byte_count = lc->total_byte_count;
michael@0 283 cpi->common.filter_level = lc->filter_level;
michael@0 284
michael@0 285 cpi->last_frame_percent_intra = lc->last_frame_percent_intra;
michael@0 286
michael@0 287 memcpy (cpi->mb.count_mb_ref_frame_usage,
michael@0 288 lc->count_mb_ref_frame_usage,
michael@0 289 sizeof(cpi->mb.count_mb_ref_frame_usage));
michael@0 290 }
michael@0 291
michael@0 292 static int rescale(int val, int num, int denom)
michael@0 293 {
michael@0 294 int64_t llnum = num;
michael@0 295 int64_t llden = denom;
michael@0 296 int64_t llval = val;
michael@0 297
michael@0 298 return (int)(llval * llnum / llden);
michael@0 299 }
michael@0 300
michael@0 301 static void init_temporal_layer_context(VP8_COMP *cpi,
michael@0 302 VP8_CONFIG *oxcf,
michael@0 303 const int layer,
michael@0 304 double prev_layer_framerate)
michael@0 305 {
michael@0 306 LAYER_CONTEXT *lc = &cpi->layer_context[layer];
michael@0 307
michael@0 308 lc->framerate = cpi->output_framerate / cpi->oxcf.rate_decimator[layer];
michael@0 309 lc->target_bandwidth = cpi->oxcf.target_bitrate[layer] * 1000;
michael@0 310
michael@0 311 lc->starting_buffer_level_in_ms = oxcf->starting_buffer_level;
michael@0 312 lc->optimal_buffer_level_in_ms = oxcf->optimal_buffer_level;
michael@0 313 lc->maximum_buffer_size_in_ms = oxcf->maximum_buffer_size;
michael@0 314
michael@0 315 lc->starting_buffer_level =
michael@0 316 rescale((int)(oxcf->starting_buffer_level),
michael@0 317 lc->target_bandwidth, 1000);
michael@0 318
michael@0 319 if (oxcf->optimal_buffer_level == 0)
michael@0 320 lc->optimal_buffer_level = lc->target_bandwidth / 8;
michael@0 321 else
michael@0 322 lc->optimal_buffer_level =
michael@0 323 rescale((int)(oxcf->optimal_buffer_level),
michael@0 324 lc->target_bandwidth, 1000);
michael@0 325
michael@0 326 if (oxcf->maximum_buffer_size == 0)
michael@0 327 lc->maximum_buffer_size = lc->target_bandwidth / 8;
michael@0 328 else
michael@0 329 lc->maximum_buffer_size =
michael@0 330 rescale((int)(oxcf->maximum_buffer_size),
michael@0 331 lc->target_bandwidth, 1000);
michael@0 332
michael@0 333 /* Work out the average size of a frame within this layer */
michael@0 334 if (layer > 0)
michael@0 335 lc->avg_frame_size_for_layer =
michael@0 336 (int)((cpi->oxcf.target_bitrate[layer] -
michael@0 337 cpi->oxcf.target_bitrate[layer-1]) * 1000 /
michael@0 338 (lc->framerate - prev_layer_framerate));
michael@0 339
michael@0 340 lc->active_worst_quality = cpi->oxcf.worst_allowed_q;
michael@0 341 lc->active_best_quality = cpi->oxcf.best_allowed_q;
michael@0 342 lc->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
michael@0 343
michael@0 344 lc->buffer_level = lc->starting_buffer_level;
michael@0 345 lc->bits_off_target = lc->starting_buffer_level;
michael@0 346
michael@0 347 lc->total_actual_bits = 0;
michael@0 348 lc->ni_av_qi = 0;
michael@0 349 lc->ni_tot_qi = 0;
michael@0 350 lc->ni_frames = 0;
michael@0 351 lc->rate_correction_factor = 1.0;
michael@0 352 lc->key_frame_rate_correction_factor = 1.0;
michael@0 353 lc->gf_rate_correction_factor = 1.0;
michael@0 354 lc->inter_frame_target = 0;
michael@0 355 }
michael@0 356
michael@0 357 // Upon a run-time change in temporal layers, reset the layer context parameters
michael@0 358 // for any "new" layers. For "existing" layers, let them inherit the parameters
michael@0 359 // from the previous layer state (at the same layer #). In future we may want
michael@0 360 // to better map the previous layer state(s) to the "new" ones.
michael@0 361 static void reset_temporal_layer_change(VP8_COMP *cpi,
michael@0 362 VP8_CONFIG *oxcf,
michael@0 363 const int prev_num_layers)
michael@0 364 {
michael@0 365 int i;
michael@0 366 double prev_layer_framerate = 0;
michael@0 367 const int curr_num_layers = cpi->oxcf.number_of_layers;
michael@0 368 // If the previous state was 1 layer, get current layer context from cpi.
michael@0 369 // We need this to set the layer context for the new layers below.
michael@0 370 if (prev_num_layers == 1)
michael@0 371 {
michael@0 372 cpi->current_layer = 0;
michael@0 373 save_layer_context(cpi);
michael@0 374 }
michael@0 375 for (i = 0; i < curr_num_layers; i++)
michael@0 376 {
michael@0 377 LAYER_CONTEXT *lc = &cpi->layer_context[i];
michael@0 378 if (i >= prev_num_layers)
michael@0 379 {
michael@0 380 init_temporal_layer_context(cpi, oxcf, i, prev_layer_framerate);
michael@0 381 }
michael@0 382 // The initial buffer levels are set based on their starting levels.
michael@0 383 // We could set the buffer levels based on the previous state (normalized
michael@0 384 // properly by the layer bandwidths) but we would need to keep track of
michael@0 385 // the previous set of layer bandwidths (i.e., target_bitrate[i])
michael@0 386 // before the layer change. For now, reset to the starting levels.
michael@0 387 lc->buffer_level = cpi->oxcf.starting_buffer_level_in_ms *
michael@0 388 cpi->oxcf.target_bitrate[i];
michael@0 389 lc->bits_off_target = lc->buffer_level;
michael@0 390 // TDOD(marpan): Should we set the rate_correction_factor and
michael@0 391 // active_worst/best_quality to values derived from the previous layer
michael@0 392 // state (to smooth-out quality dips/rate fluctuation at transition)?
michael@0 393
michael@0 394 // We need to treat the 1 layer case separately: oxcf.target_bitrate[i]
michael@0 395 // is not set for 1 layer, and the restore_layer_context/save_context()
michael@0 396 // are not called in the encoding loop, so we need to call it here to
michael@0 397 // pass the layer context state to |cpi|.
michael@0 398 if (curr_num_layers == 1)
michael@0 399 {
michael@0 400 lc->target_bandwidth = cpi->oxcf.target_bandwidth;
michael@0 401 lc->buffer_level = cpi->oxcf.starting_buffer_level_in_ms *
michael@0 402 lc->target_bandwidth / 1000;
michael@0 403 lc->bits_off_target = lc->buffer_level;
michael@0 404 restore_layer_context(cpi, 0);
michael@0 405 }
michael@0 406 prev_layer_framerate = cpi->output_framerate /
michael@0 407 cpi->oxcf.rate_decimator[i];
michael@0 408 }
michael@0 409 }
michael@0 410
michael@0 411 static void setup_features(VP8_COMP *cpi)
michael@0 412 {
michael@0 413 // If segmentation enabled set the update flags
michael@0 414 if ( cpi->mb.e_mbd.segmentation_enabled )
michael@0 415 {
michael@0 416 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
michael@0 417 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
michael@0 418 }
michael@0 419 else
michael@0 420 {
michael@0 421 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
michael@0 422 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
michael@0 423 }
michael@0 424
michael@0 425 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 0;
michael@0 426 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
michael@0 427 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
michael@0 428 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
michael@0 429 vpx_memset(cpi->mb.e_mbd.last_ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
michael@0 430 vpx_memset(cpi->mb.e_mbd.last_mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
michael@0 431
michael@0 432 set_default_lf_deltas(cpi);
michael@0 433
michael@0 434 }
michael@0 435
michael@0 436
michael@0 437 static void dealloc_raw_frame_buffers(VP8_COMP *cpi);
michael@0 438
michael@0 439
michael@0 440 static void dealloc_compressor_data(VP8_COMP *cpi)
michael@0 441 {
michael@0 442 vpx_free(cpi->tplist);
michael@0 443 cpi->tplist = NULL;
michael@0 444
michael@0 445 /* Delete last frame MV storage buffers */
michael@0 446 vpx_free(cpi->lfmv);
michael@0 447 cpi->lfmv = 0;
michael@0 448
michael@0 449 vpx_free(cpi->lf_ref_frame_sign_bias);
michael@0 450 cpi->lf_ref_frame_sign_bias = 0;
michael@0 451
michael@0 452 vpx_free(cpi->lf_ref_frame);
michael@0 453 cpi->lf_ref_frame = 0;
michael@0 454
michael@0 455 /* Delete sementation map */
michael@0 456 vpx_free(cpi->segmentation_map);
michael@0 457 cpi->segmentation_map = 0;
michael@0 458
michael@0 459 vpx_free(cpi->active_map);
michael@0 460 cpi->active_map = 0;
michael@0 461
michael@0 462 vp8_de_alloc_frame_buffers(&cpi->common);
michael@0 463
michael@0 464 vp8_yv12_de_alloc_frame_buffer(&cpi->pick_lf_lvl_frame);
michael@0 465 vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
michael@0 466 dealloc_raw_frame_buffers(cpi);
michael@0 467
michael@0 468 vpx_free(cpi->tok);
michael@0 469 cpi->tok = 0;
michael@0 470
michael@0 471 /* Structure used to monitor GF usage */
michael@0 472 vpx_free(cpi->gf_active_flags);
michael@0 473 cpi->gf_active_flags = 0;
michael@0 474
michael@0 475 /* Activity mask based per mb zbin adjustments */
michael@0 476 vpx_free(cpi->mb_activity_map);
michael@0 477 cpi->mb_activity_map = 0;
michael@0 478
michael@0 479 vpx_free(cpi->mb.pip);
michael@0 480 cpi->mb.pip = 0;
michael@0 481
michael@0 482 #if CONFIG_MULTITHREAD
michael@0 483 vpx_free(cpi->mt_current_mb_col);
michael@0 484 cpi->mt_current_mb_col = NULL;
michael@0 485 #endif
michael@0 486 }
michael@0 487
michael@0 488 static void enable_segmentation(VP8_COMP *cpi)
michael@0 489 {
michael@0 490 /* Set the appropriate feature bit */
michael@0 491 cpi->mb.e_mbd.segmentation_enabled = 1;
michael@0 492 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
michael@0 493 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
michael@0 494 }
michael@0 495 static void disable_segmentation(VP8_COMP *cpi)
michael@0 496 {
michael@0 497 /* Clear the appropriate feature bit */
michael@0 498 cpi->mb.e_mbd.segmentation_enabled = 0;
michael@0 499 }
michael@0 500
michael@0 501 /* Valid values for a segment are 0 to 3
michael@0 502 * Segmentation map is arrange as [Rows][Columns]
michael@0 503 */
michael@0 504 static void set_segmentation_map(VP8_COMP *cpi, unsigned char *segmentation_map)
michael@0 505 {
michael@0 506 /* Copy in the new segmentation map */
michael@0 507 vpx_memcpy(cpi->segmentation_map, segmentation_map, (cpi->common.mb_rows * cpi->common.mb_cols));
michael@0 508
michael@0 509 /* Signal that the map should be updated. */
michael@0 510 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
michael@0 511 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
michael@0 512 }
michael@0 513
michael@0 514 /* The values given for each segment can be either deltas (from the default
michael@0 515 * value chosen for the frame) or absolute values.
michael@0 516 *
michael@0 517 * Valid range for abs values is:
michael@0 518 * (0-127 for MB_LVL_ALT_Q), (0-63 for SEGMENT_ALT_LF)
michael@0 519 * Valid range for delta values are:
michael@0 520 * (+/-127 for MB_LVL_ALT_Q), (+/-63 for SEGMENT_ALT_LF)
michael@0 521 *
michael@0 522 * abs_delta = SEGMENT_DELTADATA (deltas)
michael@0 523 * abs_delta = SEGMENT_ABSDATA (use the absolute values given).
michael@0 524 *
michael@0 525 */
michael@0 526 static void set_segment_data(VP8_COMP *cpi, signed char *feature_data, unsigned char abs_delta)
michael@0 527 {
michael@0 528 cpi->mb.e_mbd.mb_segement_abs_delta = abs_delta;
michael@0 529 vpx_memcpy(cpi->segment_feature_data, feature_data, sizeof(cpi->segment_feature_data));
michael@0 530 }
michael@0 531
michael@0 532
michael@0 533 static void segmentation_test_function(VP8_COMP *cpi)
michael@0 534 {
michael@0 535 unsigned char *seg_map;
michael@0 536 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
michael@0 537
michael@0 538 // Create a temporary map for segmentation data.
michael@0 539 CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
michael@0 540
michael@0 541 // Set the segmentation Map
michael@0 542 set_segmentation_map(cpi, seg_map);
michael@0 543
michael@0 544 // Activate segmentation.
michael@0 545 enable_segmentation(cpi);
michael@0 546
michael@0 547 // Set up the quant segment data
michael@0 548 feature_data[MB_LVL_ALT_Q][0] = 0;
michael@0 549 feature_data[MB_LVL_ALT_Q][1] = 4;
michael@0 550 feature_data[MB_LVL_ALT_Q][2] = 0;
michael@0 551 feature_data[MB_LVL_ALT_Q][3] = 0;
michael@0 552 // Set up the loop segment data
michael@0 553 feature_data[MB_LVL_ALT_LF][0] = 0;
michael@0 554 feature_data[MB_LVL_ALT_LF][1] = 0;
michael@0 555 feature_data[MB_LVL_ALT_LF][2] = 0;
michael@0 556 feature_data[MB_LVL_ALT_LF][3] = 0;
michael@0 557
michael@0 558 // Initialise the feature data structure
michael@0 559 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
michael@0 560 set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
michael@0 561
michael@0 562 // Delete sementation map
michael@0 563 vpx_free(seg_map);
michael@0 564
michael@0 565 seg_map = 0;
michael@0 566 }
michael@0 567
michael@0 568 /* A simple function to cyclically refresh the background at a lower Q */
michael@0 569 static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment)
michael@0 570 {
michael@0 571 unsigned char *seg_map = cpi->segmentation_map;
michael@0 572 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
michael@0 573 int i;
michael@0 574 int block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
michael@0 575 int mbs_in_frame = cpi->common.mb_rows * cpi->common.mb_cols;
michael@0 576
michael@0 577 cpi->cyclic_refresh_q = Q / 2;
michael@0 578
michael@0 579 // Set every macroblock to be eligible for update.
michael@0 580 // For key frame this will reset seg map to 0.
michael@0 581 vpx_memset(cpi->segmentation_map, 0, mbs_in_frame);
michael@0 582
michael@0 583 if (cpi->common.frame_type != KEY_FRAME)
michael@0 584 {
michael@0 585 /* Cycle through the macro_block rows */
michael@0 586 /* MB loop to set local segmentation map */
michael@0 587 i = cpi->cyclic_refresh_mode_index;
michael@0 588 assert(i < mbs_in_frame);
michael@0 589 do
michael@0 590 {
michael@0 591 /* If the MB is as a candidate for clean up then mark it for
michael@0 592 * possible boost/refresh (segment 1) The segment id may get
michael@0 593 * reset to 0 later if the MB gets coded anything other than
michael@0 594 * last frame 0,0 as only (last frame 0,0) MBs are eligable for
michael@0 595 * refresh : that is to say Mbs likely to be background blocks.
michael@0 596 */
michael@0 597 if (cpi->cyclic_refresh_map[i] == 0)
michael@0 598 {
michael@0 599 seg_map[i] = 1;
michael@0 600 block_count --;
michael@0 601 }
michael@0 602 else if (cpi->cyclic_refresh_map[i] < 0)
michael@0 603 cpi->cyclic_refresh_map[i]++;
michael@0 604
michael@0 605 i++;
michael@0 606 if (i == mbs_in_frame)
michael@0 607 i = 0;
michael@0 608
michael@0 609 }
michael@0 610 while(block_count && i != cpi->cyclic_refresh_mode_index);
michael@0 611
michael@0 612 cpi->cyclic_refresh_mode_index = i;
michael@0 613 }
michael@0 614
michael@0 615 /* Activate segmentation. */
michael@0 616 cpi->mb.e_mbd.update_mb_segmentation_map = 1;
michael@0 617 cpi->mb.e_mbd.update_mb_segmentation_data = 1;
michael@0 618 enable_segmentation(cpi);
michael@0 619
michael@0 620 /* Set up the quant segment data */
michael@0 621 feature_data[MB_LVL_ALT_Q][0] = 0;
michael@0 622 feature_data[MB_LVL_ALT_Q][1] = (cpi->cyclic_refresh_q - Q);
michael@0 623 feature_data[MB_LVL_ALT_Q][2] = 0;
michael@0 624 feature_data[MB_LVL_ALT_Q][3] = 0;
michael@0 625
michael@0 626 /* Set up the loop segment data */
michael@0 627 feature_data[MB_LVL_ALT_LF][0] = 0;
michael@0 628 feature_data[MB_LVL_ALT_LF][1] = lf_adjustment;
michael@0 629 feature_data[MB_LVL_ALT_LF][2] = 0;
michael@0 630 feature_data[MB_LVL_ALT_LF][3] = 0;
michael@0 631
michael@0 632 /* Initialise the feature data structure */
michael@0 633 set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
michael@0 634
michael@0 635 }
michael@0 636
michael@0 637 static void set_default_lf_deltas(VP8_COMP *cpi)
michael@0 638 {
michael@0 639 cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
michael@0 640 cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;
michael@0 641
michael@0 642 vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
michael@0 643 vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
michael@0 644
michael@0 645 /* Test of ref frame deltas */
michael@0 646 cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
michael@0 647 cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
michael@0 648 cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
michael@0 649 cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;
michael@0 650
michael@0 651 cpi->mb.e_mbd.mode_lf_deltas[0] = 4; /* BPRED */
michael@0 652
michael@0 653 if(cpi->oxcf.Mode == MODE_REALTIME)
michael@0 654 cpi->mb.e_mbd.mode_lf_deltas[1] = -12; /* Zero */
michael@0 655 else
michael@0 656 cpi->mb.e_mbd.mode_lf_deltas[1] = -2; /* Zero */
michael@0 657
michael@0 658 cpi->mb.e_mbd.mode_lf_deltas[2] = 2; /* New mv */
michael@0 659 cpi->mb.e_mbd.mode_lf_deltas[3] = 4; /* Split mv */
michael@0 660 }
michael@0 661
michael@0 662 /* Convenience macros for mapping speed and mode into a continuous
michael@0 663 * range
michael@0 664 */
michael@0 665 #define GOOD(x) (x+1)
michael@0 666 #define RT(x) (x+7)
michael@0 667
michael@0 668 static int speed_map(int speed, const int *map)
michael@0 669 {
michael@0 670 int res;
michael@0 671
michael@0 672 do
michael@0 673 {
michael@0 674 res = *map++;
michael@0 675 } while(speed >= *map++);
michael@0 676 return res;
michael@0 677 }
michael@0 678
michael@0 679 static const int thresh_mult_map_znn[] = {
michael@0 680 /* map common to zero, nearest, and near */
michael@0 681 0, GOOD(2), 1500, GOOD(3), 2000, RT(0), 1000, RT(2), 2000, INT_MAX
michael@0 682 };
michael@0 683
michael@0 684 static const int thresh_mult_map_vhpred[] = {
michael@0 685 1000, GOOD(2), 1500, GOOD(3), 2000, RT(0), 1000, RT(1), 2000,
michael@0 686 RT(7), INT_MAX, INT_MAX
michael@0 687 };
michael@0 688
michael@0 689 static const int thresh_mult_map_bpred[] = {
michael@0 690 2000, GOOD(0), 2500, GOOD(2), 5000, GOOD(3), 7500, RT(0), 2500, RT(1), 5000,
michael@0 691 RT(6), INT_MAX, INT_MAX
michael@0 692 };
michael@0 693
michael@0 694 static const int thresh_mult_map_tm[] = {
michael@0 695 1000, GOOD(2), 1500, GOOD(3), 2000, RT(0), 0, RT(1), 1000, RT(2), 2000,
michael@0 696 RT(7), INT_MAX, INT_MAX
michael@0 697 };
michael@0 698
michael@0 699 static const int thresh_mult_map_new1[] = {
michael@0 700 1000, GOOD(2), 2000, RT(0), 2000, INT_MAX
michael@0 701 };
michael@0 702
michael@0 703 static const int thresh_mult_map_new2[] = {
michael@0 704 1000, GOOD(2), 2000, GOOD(3), 2500, GOOD(5), 4000, RT(0), 2000, RT(2), 2500,
michael@0 705 RT(5), 4000, INT_MAX
michael@0 706 };
michael@0 707
michael@0 708 static const int thresh_mult_map_split1[] = {
michael@0 709 2500, GOOD(0), 1700, GOOD(2), 10000, GOOD(3), 25000, GOOD(4), INT_MAX,
michael@0 710 RT(0), 5000, RT(1), 10000, RT(2), 25000, RT(3), INT_MAX, INT_MAX
michael@0 711 };
michael@0 712
michael@0 713 static const int thresh_mult_map_split2[] = {
michael@0 714 5000, GOOD(0), 4500, GOOD(2), 20000, GOOD(3), 50000, GOOD(4), INT_MAX,
michael@0 715 RT(0), 10000, RT(1), 20000, RT(2), 50000, RT(3), INT_MAX, INT_MAX
michael@0 716 };
michael@0 717
michael@0 718 static const int mode_check_freq_map_zn2[] = {
michael@0 719 /* {zero,nearest}{2,3} */
michael@0 720 0, RT(10), 1<<1, RT(11), 1<<2, RT(12), 1<<3, INT_MAX
michael@0 721 };
michael@0 722
michael@0 723 static const int mode_check_freq_map_vhbpred[] = {
michael@0 724 0, GOOD(5), 2, RT(0), 0, RT(3), 2, RT(5), 4, INT_MAX
michael@0 725 };
michael@0 726
michael@0 727 static const int mode_check_freq_map_near2[] = {
michael@0 728 0, GOOD(5), 2, RT(0), 0, RT(3), 2, RT(10), 1<<2, RT(11), 1<<3, RT(12), 1<<4,
michael@0 729 INT_MAX
michael@0 730 };
michael@0 731
michael@0 732 static const int mode_check_freq_map_new1[] = {
michael@0 733 0, RT(10), 1<<1, RT(11), 1<<2, RT(12), 1<<3, INT_MAX
michael@0 734 };
michael@0 735
michael@0 736 static const int mode_check_freq_map_new2[] = {
michael@0 737 0, GOOD(5), 4, RT(0), 0, RT(3), 4, RT(10), 1<<3, RT(11), 1<<4, RT(12), 1<<5,
michael@0 738 INT_MAX
michael@0 739 };
michael@0 740
michael@0 741 static const int mode_check_freq_map_split1[] = {
michael@0 742 0, GOOD(2), 2, GOOD(3), 7, RT(1), 2, RT(2), 7, INT_MAX
michael@0 743 };
michael@0 744
michael@0 745 static const int mode_check_freq_map_split2[] = {
michael@0 746 0, GOOD(1), 2, GOOD(2), 4, GOOD(3), 15, RT(1), 4, RT(2), 15, INT_MAX
michael@0 747 };
michael@0 748
michael@0 749 void vp8_set_speed_features(VP8_COMP *cpi)
michael@0 750 {
michael@0 751 SPEED_FEATURES *sf = &cpi->sf;
michael@0 752 int Mode = cpi->compressor_speed;
michael@0 753 int Speed = cpi->Speed;
michael@0 754 int i;
michael@0 755 VP8_COMMON *cm = &cpi->common;
michael@0 756 int last_improved_quant = sf->improved_quant;
michael@0 757 int ref_frames;
michael@0 758
michael@0 759 /* Initialise default mode frequency sampling variables */
michael@0 760 for (i = 0; i < MAX_MODES; i ++)
michael@0 761 {
michael@0 762 cpi->mode_check_freq[i] = 0;
michael@0 763 }
michael@0 764
michael@0 765 cpi->mb.mbs_tested_so_far = 0;
michael@0 766
michael@0 767 /* best quality defaults */
michael@0 768 sf->RD = 1;
michael@0 769 sf->search_method = NSTEP;
michael@0 770 sf->improved_quant = 1;
michael@0 771 sf->improved_dct = 1;
michael@0 772 sf->auto_filter = 1;
michael@0 773 sf->recode_loop = 1;
michael@0 774 sf->quarter_pixel_search = 1;
michael@0 775 sf->half_pixel_search = 1;
michael@0 776 sf->iterative_sub_pixel = 1;
michael@0 777 sf->optimize_coefficients = 1;
michael@0 778 sf->use_fastquant_for_pick = 0;
michael@0 779 sf->no_skip_block4x4_search = 1;
michael@0 780
michael@0 781 sf->first_step = 0;
michael@0 782 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
michael@0 783 sf->improved_mv_pred = 1;
michael@0 784
michael@0 785 /* default thresholds to 0 */
michael@0 786 for (i = 0; i < MAX_MODES; i++)
michael@0 787 sf->thresh_mult[i] = 0;
michael@0 788
michael@0 789 /* Count enabled references */
michael@0 790 ref_frames = 1;
michael@0 791 if (cpi->ref_frame_flags & VP8_LAST_FRAME)
michael@0 792 ref_frames++;
michael@0 793 if (cpi->ref_frame_flags & VP8_GOLD_FRAME)
michael@0 794 ref_frames++;
michael@0 795 if (cpi->ref_frame_flags & VP8_ALTR_FRAME)
michael@0 796 ref_frames++;
michael@0 797
michael@0 798 /* Convert speed to continuous range, with clamping */
michael@0 799 if (Mode == 0)
michael@0 800 Speed = 0;
michael@0 801 else if (Mode == 2)
michael@0 802 Speed = RT(Speed);
michael@0 803 else
michael@0 804 {
michael@0 805 if (Speed > 5)
michael@0 806 Speed = 5;
michael@0 807 Speed = GOOD(Speed);
michael@0 808 }
michael@0 809
michael@0 810 sf->thresh_mult[THR_ZERO1] =
michael@0 811 sf->thresh_mult[THR_NEAREST1] =
michael@0 812 sf->thresh_mult[THR_NEAR1] =
michael@0 813 sf->thresh_mult[THR_DC] = 0; /* always */
michael@0 814
michael@0 815 sf->thresh_mult[THR_ZERO2] =
michael@0 816 sf->thresh_mult[THR_ZERO3] =
michael@0 817 sf->thresh_mult[THR_NEAREST2] =
michael@0 818 sf->thresh_mult[THR_NEAREST3] =
michael@0 819 sf->thresh_mult[THR_NEAR2] =
michael@0 820 sf->thresh_mult[THR_NEAR3] = speed_map(Speed, thresh_mult_map_znn);
michael@0 821
michael@0 822 sf->thresh_mult[THR_V_PRED] =
michael@0 823 sf->thresh_mult[THR_H_PRED] = speed_map(Speed, thresh_mult_map_vhpred);
michael@0 824 sf->thresh_mult[THR_B_PRED] = speed_map(Speed, thresh_mult_map_bpred);
michael@0 825 sf->thresh_mult[THR_TM] = speed_map(Speed, thresh_mult_map_tm);
michael@0 826 sf->thresh_mult[THR_NEW1] = speed_map(Speed, thresh_mult_map_new1);
michael@0 827 sf->thresh_mult[THR_NEW2] =
michael@0 828 sf->thresh_mult[THR_NEW3] = speed_map(Speed, thresh_mult_map_new2);
michael@0 829 sf->thresh_mult[THR_SPLIT1] = speed_map(Speed, thresh_mult_map_split1);
michael@0 830 sf->thresh_mult[THR_SPLIT2] =
michael@0 831 sf->thresh_mult[THR_SPLIT3] = speed_map(Speed, thresh_mult_map_split2);
michael@0 832
michael@0 833 cpi->mode_check_freq[THR_ZERO1] =
michael@0 834 cpi->mode_check_freq[THR_NEAREST1] =
michael@0 835 cpi->mode_check_freq[THR_NEAR1] =
michael@0 836 cpi->mode_check_freq[THR_TM] =
michael@0 837 cpi->mode_check_freq[THR_DC] = 0; /* always */
michael@0 838
michael@0 839 cpi->mode_check_freq[THR_ZERO2] =
michael@0 840 cpi->mode_check_freq[THR_ZERO3] =
michael@0 841 cpi->mode_check_freq[THR_NEAREST2] =
michael@0 842 cpi->mode_check_freq[THR_NEAREST3] = speed_map(Speed,
michael@0 843 mode_check_freq_map_zn2);
michael@0 844
michael@0 845 cpi->mode_check_freq[THR_NEAR2] =
michael@0 846 cpi->mode_check_freq[THR_NEAR3] = speed_map(Speed,
michael@0 847 mode_check_freq_map_near2);
michael@0 848
michael@0 849 cpi->mode_check_freq[THR_V_PRED] =
michael@0 850 cpi->mode_check_freq[THR_H_PRED] =
michael@0 851 cpi->mode_check_freq[THR_B_PRED] = speed_map(Speed,
michael@0 852 mode_check_freq_map_vhbpred);
michael@0 853 cpi->mode_check_freq[THR_NEW1] = speed_map(Speed,
michael@0 854 mode_check_freq_map_new1);
michael@0 855 cpi->mode_check_freq[THR_NEW2] =
michael@0 856 cpi->mode_check_freq[THR_NEW3] = speed_map(Speed,
michael@0 857 mode_check_freq_map_new2);
michael@0 858 cpi->mode_check_freq[THR_SPLIT1] = speed_map(Speed,
michael@0 859 mode_check_freq_map_split1);
michael@0 860 cpi->mode_check_freq[THR_SPLIT2] =
michael@0 861 cpi->mode_check_freq[THR_SPLIT3] = speed_map(Speed,
michael@0 862 mode_check_freq_map_split2);
michael@0 863 Speed = cpi->Speed;
michael@0 864 switch (Mode)
michael@0 865 {
michael@0 866 #if !(CONFIG_REALTIME_ONLY)
michael@0 867 case 0: /* best quality mode */
michael@0 868 sf->first_step = 0;
michael@0 869 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
michael@0 870 break;
michael@0 871 case 1:
michael@0 872 case 3:
michael@0 873 if (Speed > 0)
michael@0 874 {
michael@0 875 /* Disable coefficient optimization above speed 0 */
michael@0 876 sf->optimize_coefficients = 0;
michael@0 877 sf->use_fastquant_for_pick = 1;
michael@0 878 sf->no_skip_block4x4_search = 0;
michael@0 879
michael@0 880 sf->first_step = 1;
michael@0 881 }
michael@0 882
michael@0 883 if (Speed > 2)
michael@0 884 {
michael@0 885 sf->improved_quant = 0;
michael@0 886 sf->improved_dct = 0;
michael@0 887
michael@0 888 /* Only do recode loop on key frames, golden frames and
michael@0 889 * alt ref frames
michael@0 890 */
michael@0 891 sf->recode_loop = 2;
michael@0 892
michael@0 893 }
michael@0 894
michael@0 895 if (Speed > 3)
michael@0 896 {
michael@0 897 sf->auto_filter = 1;
michael@0 898 sf->recode_loop = 0; /* recode loop off */
michael@0 899 sf->RD = 0; /* Turn rd off */
michael@0 900
michael@0 901 }
michael@0 902
michael@0 903 if (Speed > 4)
michael@0 904 {
michael@0 905 sf->auto_filter = 0; /* Faster selection of loop filter */
michael@0 906 }
michael@0 907
michael@0 908 break;
michael@0 909 #endif
michael@0 910 case 2:
michael@0 911 sf->optimize_coefficients = 0;
michael@0 912 sf->recode_loop = 0;
michael@0 913 sf->auto_filter = 1;
michael@0 914 sf->iterative_sub_pixel = 1;
michael@0 915 sf->search_method = NSTEP;
michael@0 916
michael@0 917 if (Speed > 0)
michael@0 918 {
michael@0 919 sf->improved_quant = 0;
michael@0 920 sf->improved_dct = 0;
michael@0 921
michael@0 922 sf->use_fastquant_for_pick = 1;
michael@0 923 sf->no_skip_block4x4_search = 0;
michael@0 924 sf->first_step = 1;
michael@0 925 }
michael@0 926
michael@0 927 if (Speed > 2)
michael@0 928 sf->auto_filter = 0; /* Faster selection of loop filter */
michael@0 929
michael@0 930 if (Speed > 3)
michael@0 931 {
michael@0 932 sf->RD = 0;
michael@0 933 sf->auto_filter = 1;
michael@0 934 }
michael@0 935
michael@0 936 if (Speed > 4)
michael@0 937 {
michael@0 938 sf->auto_filter = 0; /* Faster selection of loop filter */
michael@0 939 sf->search_method = HEX;
michael@0 940 sf->iterative_sub_pixel = 0;
michael@0 941 }
michael@0 942
michael@0 943 if (Speed > 6)
michael@0 944 {
michael@0 945 unsigned int sum = 0;
michael@0 946 unsigned int total_mbs = cm->MBs;
michael@0 947 int thresh;
michael@0 948 unsigned int total_skip;
michael@0 949
michael@0 950 int min = 2000;
michael@0 951
michael@0 952 if (cpi->oxcf.encode_breakout > 2000)
michael@0 953 min = cpi->oxcf.encode_breakout;
michael@0 954
michael@0 955 min >>= 7;
michael@0 956
michael@0 957 for (i = 0; i < min; i++)
michael@0 958 {
michael@0 959 sum += cpi->mb.error_bins[i];
michael@0 960 }
michael@0 961
michael@0 962 total_skip = sum;
michael@0 963 sum = 0;
michael@0 964
michael@0 965 /* i starts from 2 to make sure thresh started from 2048 */
michael@0 966 for (; i < 1024; i++)
michael@0 967 {
michael@0 968 sum += cpi->mb.error_bins[i];
michael@0 969
michael@0 970 if (10 * sum >= (unsigned int)(cpi->Speed - 6)*(total_mbs - total_skip))
michael@0 971 break;
michael@0 972 }
michael@0 973
michael@0 974 i--;
michael@0 975 thresh = (i << 7);
michael@0 976
michael@0 977 if (thresh < 2000)
michael@0 978 thresh = 2000;
michael@0 979
michael@0 980 if (ref_frames > 1)
michael@0 981 {
michael@0 982 sf->thresh_mult[THR_NEW1 ] = thresh;
michael@0 983 sf->thresh_mult[THR_NEAREST1 ] = thresh >> 1;
michael@0 984 sf->thresh_mult[THR_NEAR1 ] = thresh >> 1;
michael@0 985 }
michael@0 986
michael@0 987 if (ref_frames > 2)
michael@0 988 {
michael@0 989 sf->thresh_mult[THR_NEW2] = thresh << 1;
michael@0 990 sf->thresh_mult[THR_NEAREST2 ] = thresh;
michael@0 991 sf->thresh_mult[THR_NEAR2 ] = thresh;
michael@0 992 }
michael@0 993
michael@0 994 if (ref_frames > 3)
michael@0 995 {
michael@0 996 sf->thresh_mult[THR_NEW3] = thresh << 1;
michael@0 997 sf->thresh_mult[THR_NEAREST3 ] = thresh;
michael@0 998 sf->thresh_mult[THR_NEAR3 ] = thresh;
michael@0 999 }
michael@0 1000
michael@0 1001 sf->improved_mv_pred = 0;
michael@0 1002 }
michael@0 1003
michael@0 1004 if (Speed > 8)
michael@0 1005 sf->quarter_pixel_search = 0;
michael@0 1006
michael@0 1007 if(cm->version == 0)
michael@0 1008 {
michael@0 1009 cm->filter_type = NORMAL_LOOPFILTER;
michael@0 1010
michael@0 1011 if (Speed >= 14)
michael@0 1012 cm->filter_type = SIMPLE_LOOPFILTER;
michael@0 1013 }
michael@0 1014 else
michael@0 1015 {
michael@0 1016 cm->filter_type = SIMPLE_LOOPFILTER;
michael@0 1017 }
michael@0 1018
michael@0 1019 /* This has a big hit on quality. Last resort */
michael@0 1020 if (Speed >= 15)
michael@0 1021 sf->half_pixel_search = 0;
michael@0 1022
michael@0 1023 vpx_memset(cpi->mb.error_bins, 0, sizeof(cpi->mb.error_bins));
michael@0 1024
michael@0 1025 }; /* switch */
michael@0 1026
michael@0 1027 /* Slow quant, dct and trellis not worthwhile for first pass
michael@0 1028 * so make sure they are always turned off.
michael@0 1029 */
michael@0 1030 if ( cpi->pass == 1 )
michael@0 1031 {
michael@0 1032 sf->improved_quant = 0;
michael@0 1033 sf->optimize_coefficients = 0;
michael@0 1034 sf->improved_dct = 0;
michael@0 1035 }
michael@0 1036
michael@0 1037 if (cpi->sf.search_method == NSTEP)
michael@0 1038 {
michael@0 1039 vp8_init3smotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
michael@0 1040 }
michael@0 1041 else if (cpi->sf.search_method == DIAMOND)
michael@0 1042 {
michael@0 1043 vp8_init_dsmotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
michael@0 1044 }
michael@0 1045
michael@0 1046 if (cpi->sf.improved_dct)
michael@0 1047 {
michael@0 1048 cpi->mb.short_fdct8x4 = vp8_short_fdct8x4;
michael@0 1049 cpi->mb.short_fdct4x4 = vp8_short_fdct4x4;
michael@0 1050 }
michael@0 1051 else
michael@0 1052 {
michael@0 1053 /* No fast FDCT defined for any platform at this time. */
michael@0 1054 cpi->mb.short_fdct8x4 = vp8_short_fdct8x4;
michael@0 1055 cpi->mb.short_fdct4x4 = vp8_short_fdct4x4;
michael@0 1056 }
michael@0 1057
michael@0 1058 cpi->mb.short_walsh4x4 = vp8_short_walsh4x4;
michael@0 1059
michael@0 1060 if (cpi->sf.improved_quant)
michael@0 1061 {
michael@0 1062 cpi->mb.quantize_b = vp8_regular_quantize_b;
michael@0 1063 cpi->mb.quantize_b_pair = vp8_regular_quantize_b_pair;
michael@0 1064 }
michael@0 1065 else
michael@0 1066 {
michael@0 1067 cpi->mb.quantize_b = vp8_fast_quantize_b;
michael@0 1068 cpi->mb.quantize_b_pair = vp8_fast_quantize_b_pair;
michael@0 1069 }
michael@0 1070 if (cpi->sf.improved_quant != last_improved_quant)
michael@0 1071 vp8cx_init_quantizer(cpi);
michael@0 1072
michael@0 1073 if (cpi->sf.iterative_sub_pixel == 1)
michael@0 1074 {
michael@0 1075 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step_iteratively;
michael@0 1076 }
michael@0 1077 else if (cpi->sf.quarter_pixel_search)
michael@0 1078 {
michael@0 1079 cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step;
michael@0 1080 }
michael@0 1081 else if (cpi->sf.half_pixel_search)
michael@0 1082 {
michael@0 1083 cpi->find_fractional_mv_step = vp8_find_best_half_pixel_step;
michael@0 1084 }
michael@0 1085 else
michael@0 1086 {
michael@0 1087 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
michael@0 1088 }
michael@0 1089
michael@0 1090 if (cpi->sf.optimize_coefficients == 1 && cpi->pass!=1)
michael@0 1091 cpi->mb.optimize = 1;
michael@0 1092 else
michael@0 1093 cpi->mb.optimize = 0;
michael@0 1094
michael@0 1095 if (cpi->common.full_pixel)
michael@0 1096 cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
michael@0 1097
michael@0 1098 #ifdef SPEEDSTATS
michael@0 1099 frames_at_speed[cpi->Speed]++;
michael@0 1100 #endif
michael@0 1101 }
michael@0 1102 #undef GOOD
michael@0 1103 #undef RT
michael@0 1104
michael@0 1105 static void alloc_raw_frame_buffers(VP8_COMP *cpi)
michael@0 1106 {
michael@0 1107 #if VP8_TEMPORAL_ALT_REF
michael@0 1108 int width = (cpi->oxcf.Width + 15) & ~15;
michael@0 1109 int height = (cpi->oxcf.Height + 15) & ~15;
michael@0 1110 #endif
michael@0 1111
michael@0 1112 cpi->lookahead = vp8_lookahead_init(cpi->oxcf.Width, cpi->oxcf.Height,
michael@0 1113 cpi->oxcf.lag_in_frames);
michael@0 1114 if(!cpi->lookahead)
michael@0 1115 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
michael@0 1116 "Failed to allocate lag buffers");
michael@0 1117
michael@0 1118 #if VP8_TEMPORAL_ALT_REF
michael@0 1119
michael@0 1120 if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer,
michael@0 1121 width, height, VP8BORDERINPIXELS))
michael@0 1122 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
michael@0 1123 "Failed to allocate altref buffer");
michael@0 1124
michael@0 1125 #endif
michael@0 1126 }
michael@0 1127
michael@0 1128
michael@0 1129 static void dealloc_raw_frame_buffers(VP8_COMP *cpi)
michael@0 1130 {
michael@0 1131 #if VP8_TEMPORAL_ALT_REF
michael@0 1132 vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer);
michael@0 1133 #endif
michael@0 1134 vp8_lookahead_destroy(cpi->lookahead);
michael@0 1135 }
michael@0 1136
michael@0 1137
michael@0 1138 static int vp8_alloc_partition_data(VP8_COMP *cpi)
michael@0 1139 {
michael@0 1140 vpx_free(cpi->mb.pip);
michael@0 1141
michael@0 1142 cpi->mb.pip = vpx_calloc((cpi->common.mb_cols + 1) *
michael@0 1143 (cpi->common.mb_rows + 1),
michael@0 1144 sizeof(PARTITION_INFO));
michael@0 1145 if(!cpi->mb.pip)
michael@0 1146 return 1;
michael@0 1147
michael@0 1148 cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
michael@0 1149
michael@0 1150 return 0;
michael@0 1151 }
michael@0 1152
michael@0 1153 void vp8_alloc_compressor_data(VP8_COMP *cpi)
michael@0 1154 {
michael@0 1155 VP8_COMMON *cm = & cpi->common;
michael@0 1156
michael@0 1157 int width = cm->Width;
michael@0 1158 int height = cm->Height;
michael@0 1159
michael@0 1160 if (vp8_alloc_frame_buffers(cm, width, height))
michael@0 1161 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
michael@0 1162 "Failed to allocate frame buffers");
michael@0 1163
michael@0 1164 if (vp8_alloc_partition_data(cpi))
michael@0 1165 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
michael@0 1166 "Failed to allocate partition data");
michael@0 1167
michael@0 1168
michael@0 1169 if ((width & 0xf) != 0)
michael@0 1170 width += 16 - (width & 0xf);
michael@0 1171
michael@0 1172 if ((height & 0xf) != 0)
michael@0 1173 height += 16 - (height & 0xf);
michael@0 1174
michael@0 1175
michael@0 1176 if (vp8_yv12_alloc_frame_buffer(&cpi->pick_lf_lvl_frame,
michael@0 1177 width, height, VP8BORDERINPIXELS))
michael@0 1178 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
michael@0 1179 "Failed to allocate last frame buffer");
michael@0 1180
michael@0 1181 if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source,
michael@0 1182 width, height, VP8BORDERINPIXELS))
michael@0 1183 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
michael@0 1184 "Failed to allocate scaled source buffer");
michael@0 1185
michael@0 1186 vpx_free(cpi->tok);
michael@0 1187
michael@0 1188 {
michael@0 1189 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
michael@0 1190 unsigned int tokens = 8 * 24 * 16; /* one MB for each thread */
michael@0 1191 #else
michael@0 1192 unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16;
michael@0 1193 #endif
michael@0 1194 CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
michael@0 1195 }
michael@0 1196
michael@0 1197 /* Data used for real time vc mode to see if gf needs refreshing */
michael@0 1198 cpi->zeromv_count = 0;
michael@0 1199
michael@0 1200
michael@0 1201 /* Structures used to monitor GF usage */
michael@0 1202 vpx_free(cpi->gf_active_flags);
michael@0 1203 CHECK_MEM_ERROR(cpi->gf_active_flags,
michael@0 1204 vpx_calloc(sizeof(*cpi->gf_active_flags),
michael@0 1205 cm->mb_rows * cm->mb_cols));
michael@0 1206 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
michael@0 1207
michael@0 1208 vpx_free(cpi->mb_activity_map);
michael@0 1209 CHECK_MEM_ERROR(cpi->mb_activity_map,
michael@0 1210 vpx_calloc(sizeof(*cpi->mb_activity_map),
michael@0 1211 cm->mb_rows * cm->mb_cols));
michael@0 1212
michael@0 1213 /* allocate memory for storing last frame's MVs for MV prediction. */
michael@0 1214 vpx_free(cpi->lfmv);
michael@0 1215 CHECK_MEM_ERROR(cpi->lfmv, vpx_calloc((cm->mb_rows+2) * (cm->mb_cols+2),
michael@0 1216 sizeof(*cpi->lfmv)));
michael@0 1217 vpx_free(cpi->lf_ref_frame_sign_bias);
michael@0 1218 CHECK_MEM_ERROR(cpi->lf_ref_frame_sign_bias,
michael@0 1219 vpx_calloc((cm->mb_rows+2) * (cm->mb_cols+2),
michael@0 1220 sizeof(*cpi->lf_ref_frame_sign_bias)));
michael@0 1221 vpx_free(cpi->lf_ref_frame);
michael@0 1222 CHECK_MEM_ERROR(cpi->lf_ref_frame,
michael@0 1223 vpx_calloc((cm->mb_rows+2) * (cm->mb_cols+2),
michael@0 1224 sizeof(*cpi->lf_ref_frame)));
michael@0 1225
michael@0 1226 /* Create the encoder segmentation map and set all entries to 0 */
michael@0 1227 vpx_free(cpi->segmentation_map);
michael@0 1228 CHECK_MEM_ERROR(cpi->segmentation_map,
michael@0 1229 vpx_calloc(cm->mb_rows * cm->mb_cols,
michael@0 1230 sizeof(*cpi->segmentation_map)));
michael@0 1231 cpi->cyclic_refresh_mode_index = 0;
michael@0 1232 vpx_free(cpi->active_map);
michael@0 1233 CHECK_MEM_ERROR(cpi->active_map,
michael@0 1234 vpx_calloc(cm->mb_rows * cm->mb_cols,
michael@0 1235 sizeof(*cpi->active_map)));
michael@0 1236 vpx_memset(cpi->active_map , 1, (cm->mb_rows * cm->mb_cols));
michael@0 1237
michael@0 1238 #if CONFIG_MULTITHREAD
michael@0 1239 if (width < 640)
michael@0 1240 cpi->mt_sync_range = 1;
michael@0 1241 else if (width <= 1280)
michael@0 1242 cpi->mt_sync_range = 4;
michael@0 1243 else if (width <= 2560)
michael@0 1244 cpi->mt_sync_range = 8;
michael@0 1245 else
michael@0 1246 cpi->mt_sync_range = 16;
michael@0 1247
michael@0 1248 if (cpi->oxcf.multi_threaded > 1)
michael@0 1249 {
michael@0 1250 vpx_free(cpi->mt_current_mb_col);
michael@0 1251 CHECK_MEM_ERROR(cpi->mt_current_mb_col,
michael@0 1252 vpx_malloc(sizeof(*cpi->mt_current_mb_col) * cm->mb_rows));
michael@0 1253 }
michael@0 1254
michael@0 1255 #endif
michael@0 1256
michael@0 1257 vpx_free(cpi->tplist);
michael@0 1258 CHECK_MEM_ERROR(cpi->tplist, vpx_malloc(sizeof(TOKENLIST) * cm->mb_rows));
michael@0 1259 }
michael@0 1260
michael@0 1261
michael@0 1262 /* Quant MOD */
michael@0 1263 static const int q_trans[] =
michael@0 1264 {
michael@0 1265 0, 1, 2, 3, 4, 5, 7, 8,
michael@0 1266 9, 10, 12, 13, 15, 17, 18, 19,
michael@0 1267 20, 21, 23, 24, 25, 26, 27, 28,
michael@0 1268 29, 30, 31, 33, 35, 37, 39, 41,
michael@0 1269 43, 45, 47, 49, 51, 53, 55, 57,
michael@0 1270 59, 61, 64, 67, 70, 73, 76, 79,
michael@0 1271 82, 85, 88, 91, 94, 97, 100, 103,
michael@0 1272 106, 109, 112, 115, 118, 121, 124, 127,
michael@0 1273 };
michael@0 1274
michael@0 1275 int vp8_reverse_trans(int x)
michael@0 1276 {
michael@0 1277 int i;
michael@0 1278
michael@0 1279 for (i = 0; i < 64; i++)
michael@0 1280 if (q_trans[i] >= x)
michael@0 1281 return i;
michael@0 1282
michael@0 1283 return 63;
michael@0 1284 }
michael@0 1285 void vp8_new_framerate(VP8_COMP *cpi, double framerate)
michael@0 1286 {
michael@0 1287 if(framerate < .1)
michael@0 1288 framerate = 30;
michael@0 1289
michael@0 1290 cpi->framerate = framerate;
michael@0 1291 cpi->output_framerate = framerate;
michael@0 1292 cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth /
michael@0 1293 cpi->output_framerate);
michael@0 1294 cpi->av_per_frame_bandwidth = cpi->per_frame_bandwidth;
michael@0 1295 cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth *
michael@0 1296 cpi->oxcf.two_pass_vbrmin_section / 100);
michael@0 1297
michael@0 1298 /* Set Maximum gf/arf interval */
michael@0 1299 cpi->max_gf_interval = ((int)(cpi->output_framerate / 2.0) + 2);
michael@0 1300
michael@0 1301 if(cpi->max_gf_interval < 12)
michael@0 1302 cpi->max_gf_interval = 12;
michael@0 1303
michael@0 1304 /* Extended interval for genuinely static scenes */
michael@0 1305 cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
michael@0 1306
michael@0 1307 /* Special conditions when altr ref frame enabled in lagged compress mode */
michael@0 1308 if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames)
michael@0 1309 {
michael@0 1310 if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1)
michael@0 1311 cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
michael@0 1312
michael@0 1313 if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1)
michael@0 1314 cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
michael@0 1315 }
michael@0 1316
michael@0 1317 if ( cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval )
michael@0 1318 cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval;
michael@0 1319 }
michael@0 1320
michael@0 1321
michael@0 1322 static void init_config(VP8_COMP *cpi, VP8_CONFIG *oxcf)
michael@0 1323 {
michael@0 1324 VP8_COMMON *cm = &cpi->common;
michael@0 1325
michael@0 1326 cpi->oxcf = *oxcf;
michael@0 1327
michael@0 1328 cpi->auto_gold = 1;
michael@0 1329 cpi->auto_adjust_gold_quantizer = 1;
michael@0 1330
michael@0 1331 cm->version = oxcf->Version;
michael@0 1332 vp8_setup_version(cm);
michael@0 1333
michael@0 1334 /* frame rate is not available on the first frame, as it's derived from
michael@0 1335 * the observed timestamps. The actual value used here doesn't matter
michael@0 1336 * too much, as it will adapt quickly. If the reciprocal of the timebase
michael@0 1337 * seems like a reasonable framerate, then use that as a guess, otherwise
michael@0 1338 * use 30.
michael@0 1339 */
michael@0 1340 cpi->framerate = (double)(oxcf->timebase.den) /
michael@0 1341 (double)(oxcf->timebase.num);
michael@0 1342
michael@0 1343 if (cpi->framerate > 180)
michael@0 1344 cpi->framerate = 30;
michael@0 1345
michael@0 1346 cpi->ref_framerate = cpi->framerate;
michael@0 1347
michael@0 1348 /* change includes all joint functionality */
michael@0 1349 vp8_change_config(cpi, oxcf);
michael@0 1350
michael@0 1351 /* Initialize active best and worst q and average q values. */
michael@0 1352 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
michael@0 1353 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
michael@0 1354 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
michael@0 1355
michael@0 1356 /* Initialise the starting buffer levels */
michael@0 1357 cpi->buffer_level = cpi->oxcf.starting_buffer_level;
michael@0 1358 cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
michael@0 1359
michael@0 1360 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
michael@0 1361 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
michael@0 1362 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
michael@0 1363 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
michael@0 1364
michael@0 1365 cpi->total_actual_bits = 0;
michael@0 1366 cpi->total_target_vs_actual = 0;
michael@0 1367
michael@0 1368 /* Temporal scalabilty */
michael@0 1369 if (cpi->oxcf.number_of_layers > 1)
michael@0 1370 {
michael@0 1371 unsigned int i;
michael@0 1372 double prev_layer_framerate=0;
michael@0 1373
michael@0 1374 for (i=0; i<cpi->oxcf.number_of_layers; i++)
michael@0 1375 {
michael@0 1376 init_temporal_layer_context(cpi, oxcf, i, prev_layer_framerate);
michael@0 1377 prev_layer_framerate = cpi->output_framerate /
michael@0 1378 cpi->oxcf.rate_decimator[i];
michael@0 1379 }
michael@0 1380 }
michael@0 1381
michael@0 1382 #if VP8_TEMPORAL_ALT_REF
michael@0 1383 {
michael@0 1384 int i;
michael@0 1385
michael@0 1386 cpi->fixed_divide[0] = 0;
michael@0 1387
michael@0 1388 for (i = 1; i < 512; i++)
michael@0 1389 cpi->fixed_divide[i] = 0x80000 / i;
michael@0 1390 }
michael@0 1391 #endif
michael@0 1392 }
michael@0 1393
michael@0 1394 static void update_layer_contexts (VP8_COMP *cpi)
michael@0 1395 {
michael@0 1396 VP8_CONFIG *oxcf = &cpi->oxcf;
michael@0 1397
michael@0 1398 /* Update snapshots of the layer contexts to reflect new parameters */
michael@0 1399 if (oxcf->number_of_layers > 1)
michael@0 1400 {
michael@0 1401 unsigned int i;
michael@0 1402 double prev_layer_framerate=0;
michael@0 1403
michael@0 1404 for (i=0; i<oxcf->number_of_layers; i++)
michael@0 1405 {
michael@0 1406 LAYER_CONTEXT *lc = &cpi->layer_context[i];
michael@0 1407
michael@0 1408 lc->framerate =
michael@0 1409 cpi->ref_framerate / oxcf->rate_decimator[i];
michael@0 1410 lc->target_bandwidth = oxcf->target_bitrate[i] * 1000;
michael@0 1411
michael@0 1412 lc->starting_buffer_level = rescale(
michael@0 1413 (int)oxcf->starting_buffer_level_in_ms,
michael@0 1414 lc->target_bandwidth, 1000);
michael@0 1415
michael@0 1416 if (oxcf->optimal_buffer_level == 0)
michael@0 1417 lc->optimal_buffer_level = lc->target_bandwidth / 8;
michael@0 1418 else
michael@0 1419 lc->optimal_buffer_level = rescale(
michael@0 1420 (int)oxcf->optimal_buffer_level_in_ms,
michael@0 1421 lc->target_bandwidth, 1000);
michael@0 1422
michael@0 1423 if (oxcf->maximum_buffer_size == 0)
michael@0 1424 lc->maximum_buffer_size = lc->target_bandwidth / 8;
michael@0 1425 else
michael@0 1426 lc->maximum_buffer_size = rescale(
michael@0 1427 (int)oxcf->maximum_buffer_size_in_ms,
michael@0 1428 lc->target_bandwidth, 1000);
michael@0 1429
michael@0 1430 /* Work out the average size of a frame within this layer */
michael@0 1431 if (i > 0)
michael@0 1432 lc->avg_frame_size_for_layer =
michael@0 1433 (int)((oxcf->target_bitrate[i] -
michael@0 1434 oxcf->target_bitrate[i-1]) * 1000 /
michael@0 1435 (lc->framerate - prev_layer_framerate));
michael@0 1436
michael@0 1437 prev_layer_framerate = lc->framerate;
michael@0 1438 }
michael@0 1439 }
michael@0 1440 }
michael@0 1441
michael@0 1442 void vp8_change_config(VP8_COMP *cpi, VP8_CONFIG *oxcf)
michael@0 1443 {
michael@0 1444 VP8_COMMON *cm = &cpi->common;
michael@0 1445 int last_w, last_h, prev_number_of_layers;
michael@0 1446
michael@0 1447 if (!cpi)
michael@0 1448 return;
michael@0 1449
michael@0 1450 if (!oxcf)
michael@0 1451 return;
michael@0 1452
michael@0 1453 #if CONFIG_MULTITHREAD
michael@0 1454 /* wait for the last picture loopfilter thread done */
michael@0 1455 if (cpi->b_lpf_running)
michael@0 1456 {
michael@0 1457 sem_wait(&cpi->h_event_end_lpf);
michael@0 1458 cpi->b_lpf_running = 0;
michael@0 1459 }
michael@0 1460 #endif
michael@0 1461
michael@0 1462 if (cm->version != oxcf->Version)
michael@0 1463 {
michael@0 1464 cm->version = oxcf->Version;
michael@0 1465 vp8_setup_version(cm);
michael@0 1466 }
michael@0 1467
michael@0 1468 last_w = cpi->oxcf.Width;
michael@0 1469 last_h = cpi->oxcf.Height;
michael@0 1470 prev_number_of_layers = cpi->oxcf.number_of_layers;
michael@0 1471
michael@0 1472 cpi->oxcf = *oxcf;
michael@0 1473
michael@0 1474 switch (cpi->oxcf.Mode)
michael@0 1475 {
michael@0 1476
michael@0 1477 case MODE_REALTIME:
michael@0 1478 cpi->pass = 0;
michael@0 1479 cpi->compressor_speed = 2;
michael@0 1480
michael@0 1481 if (cpi->oxcf.cpu_used < -16)
michael@0 1482 {
michael@0 1483 cpi->oxcf.cpu_used = -16;
michael@0 1484 }
michael@0 1485
michael@0 1486 if (cpi->oxcf.cpu_used > 16)
michael@0 1487 cpi->oxcf.cpu_used = 16;
michael@0 1488
michael@0 1489 break;
michael@0 1490
michael@0 1491 case MODE_GOODQUALITY:
michael@0 1492 cpi->pass = 0;
michael@0 1493 cpi->compressor_speed = 1;
michael@0 1494
michael@0 1495 if (cpi->oxcf.cpu_used < -5)
michael@0 1496 {
michael@0 1497 cpi->oxcf.cpu_used = -5;
michael@0 1498 }
michael@0 1499
michael@0 1500 if (cpi->oxcf.cpu_used > 5)
michael@0 1501 cpi->oxcf.cpu_used = 5;
michael@0 1502
michael@0 1503 break;
michael@0 1504
michael@0 1505 case MODE_BESTQUALITY:
michael@0 1506 cpi->pass = 0;
michael@0 1507 cpi->compressor_speed = 0;
michael@0 1508 break;
michael@0 1509
michael@0 1510 case MODE_FIRSTPASS:
michael@0 1511 cpi->pass = 1;
michael@0 1512 cpi->compressor_speed = 1;
michael@0 1513 break;
michael@0 1514 case MODE_SECONDPASS:
michael@0 1515 cpi->pass = 2;
michael@0 1516 cpi->compressor_speed = 1;
michael@0 1517
michael@0 1518 if (cpi->oxcf.cpu_used < -5)
michael@0 1519 {
michael@0 1520 cpi->oxcf.cpu_used = -5;
michael@0 1521 }
michael@0 1522
michael@0 1523 if (cpi->oxcf.cpu_used > 5)
michael@0 1524 cpi->oxcf.cpu_used = 5;
michael@0 1525
michael@0 1526 break;
michael@0 1527 case MODE_SECONDPASS_BEST:
michael@0 1528 cpi->pass = 2;
michael@0 1529 cpi->compressor_speed = 0;
michael@0 1530 break;
michael@0 1531 }
michael@0 1532
michael@0 1533 if (cpi->pass == 0)
michael@0 1534 cpi->auto_worst_q = 1;
michael@0 1535
michael@0 1536 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
michael@0 1537 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
michael@0 1538 cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
michael@0 1539
michael@0 1540 if (oxcf->fixed_q >= 0)
michael@0 1541 {
michael@0 1542 if (oxcf->worst_allowed_q < 0)
michael@0 1543 cpi->oxcf.fixed_q = q_trans[0];
michael@0 1544 else
michael@0 1545 cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
michael@0 1546
michael@0 1547 if (oxcf->alt_q < 0)
michael@0 1548 cpi->oxcf.alt_q = q_trans[0];
michael@0 1549 else
michael@0 1550 cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
michael@0 1551
michael@0 1552 if (oxcf->key_q < 0)
michael@0 1553 cpi->oxcf.key_q = q_trans[0];
michael@0 1554 else
michael@0 1555 cpi->oxcf.key_q = q_trans[oxcf->key_q];
michael@0 1556
michael@0 1557 if (oxcf->gold_q < 0)
michael@0 1558 cpi->oxcf.gold_q = q_trans[0];
michael@0 1559 else
michael@0 1560 cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
michael@0 1561
michael@0 1562 }
michael@0 1563
michael@0 1564 cpi->baseline_gf_interval =
michael@0 1565 cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
michael@0 1566
michael@0 1567 cpi->ref_frame_flags = VP8_ALTR_FRAME | VP8_GOLD_FRAME | VP8_LAST_FRAME;
michael@0 1568
michael@0 1569 cm->refresh_golden_frame = 0;
michael@0 1570 cm->refresh_last_frame = 1;
michael@0 1571 cm->refresh_entropy_probs = 1;
michael@0 1572
michael@0 1573 #if (CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
michael@0 1574 cpi->oxcf.token_partitions = 3;
michael@0 1575 #endif
michael@0 1576
michael@0 1577 if (cpi->oxcf.token_partitions >= 0 && cpi->oxcf.token_partitions <= 3)
michael@0 1578 cm->multi_token_partition =
michael@0 1579 (TOKEN_PARTITION) cpi->oxcf.token_partitions;
michael@0 1580
michael@0 1581 setup_features(cpi);
michael@0 1582
michael@0 1583 {
michael@0 1584 int i;
michael@0 1585
michael@0 1586 for (i = 0; i < MAX_MB_SEGMENTS; i++)
michael@0 1587 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
michael@0 1588 }
michael@0 1589
michael@0 1590 /* At the moment the first order values may not be > MAXQ */
michael@0 1591 if (cpi->oxcf.fixed_q > MAXQ)
michael@0 1592 cpi->oxcf.fixed_q = MAXQ;
michael@0 1593
michael@0 1594 /* local file playback mode == really big buffer */
michael@0 1595 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK)
michael@0 1596 {
michael@0 1597 cpi->oxcf.starting_buffer_level = 60000;
michael@0 1598 cpi->oxcf.optimal_buffer_level = 60000;
michael@0 1599 cpi->oxcf.maximum_buffer_size = 240000;
michael@0 1600 cpi->oxcf.starting_buffer_level_in_ms = 60000;
michael@0 1601 cpi->oxcf.optimal_buffer_level_in_ms = 60000;
michael@0 1602 cpi->oxcf.maximum_buffer_size_in_ms = 240000;
michael@0 1603 }
michael@0 1604
michael@0 1605 /* Convert target bandwidth from Kbit/s to Bit/s */
michael@0 1606 cpi->oxcf.target_bandwidth *= 1000;
michael@0 1607
michael@0 1608 cpi->oxcf.starting_buffer_level =
michael@0 1609 rescale((int)cpi->oxcf.starting_buffer_level,
michael@0 1610 cpi->oxcf.target_bandwidth, 1000);
michael@0 1611
michael@0 1612 /* Set or reset optimal and maximum buffer levels. */
michael@0 1613 if (cpi->oxcf.optimal_buffer_level == 0)
michael@0 1614 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
michael@0 1615 else
michael@0 1616 cpi->oxcf.optimal_buffer_level =
michael@0 1617 rescale((int)cpi->oxcf.optimal_buffer_level,
michael@0 1618 cpi->oxcf.target_bandwidth, 1000);
michael@0 1619
michael@0 1620 if (cpi->oxcf.maximum_buffer_size == 0)
michael@0 1621 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
michael@0 1622 else
michael@0 1623 cpi->oxcf.maximum_buffer_size =
michael@0 1624 rescale((int)cpi->oxcf.maximum_buffer_size,
michael@0 1625 cpi->oxcf.target_bandwidth, 1000);
michael@0 1626
michael@0 1627 /* Set up frame rate and related parameters rate control values. */
michael@0 1628 vp8_new_framerate(cpi, cpi->framerate);
michael@0 1629
michael@0 1630 /* Set absolute upper and lower quality limits */
michael@0 1631 cpi->worst_quality = cpi->oxcf.worst_allowed_q;
michael@0 1632 cpi->best_quality = cpi->oxcf.best_allowed_q;
michael@0 1633
michael@0 1634 /* active values should only be modified if out of new range */
michael@0 1635 if (cpi->active_worst_quality > cpi->oxcf.worst_allowed_q)
michael@0 1636 {
michael@0 1637 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
michael@0 1638 }
michael@0 1639 /* less likely */
michael@0 1640 else if (cpi->active_worst_quality < cpi->oxcf.best_allowed_q)
michael@0 1641 {
michael@0 1642 cpi->active_worst_quality = cpi->oxcf.best_allowed_q;
michael@0 1643 }
michael@0 1644 if (cpi->active_best_quality < cpi->oxcf.best_allowed_q)
michael@0 1645 {
michael@0 1646 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
michael@0 1647 }
michael@0 1648 /* less likely */
michael@0 1649 else if (cpi->active_best_quality > cpi->oxcf.worst_allowed_q)
michael@0 1650 {
michael@0 1651 cpi->active_best_quality = cpi->oxcf.worst_allowed_q;
michael@0 1652 }
michael@0 1653
michael@0 1654 cpi->buffered_mode = cpi->oxcf.optimal_buffer_level > 0;
michael@0 1655
michael@0 1656 cpi->cq_target_quality = cpi->oxcf.cq_level;
michael@0 1657
michael@0 1658 /* Only allow dropped frames in buffered mode */
michael@0 1659 cpi->drop_frames_allowed = cpi->oxcf.allow_df && cpi->buffered_mode;
michael@0 1660
michael@0 1661 cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
michael@0 1662
michael@0 1663 // Check if the number of temporal layers has changed, and if so reset the
michael@0 1664 // pattern counter and set/initialize the temporal layer context for the
michael@0 1665 // new layer configuration.
michael@0 1666 if (cpi->oxcf.number_of_layers != prev_number_of_layers)
michael@0 1667 {
michael@0 1668 // If the number of temporal layers are changed we must start at the
michael@0 1669 // base of the pattern cycle, so reset temporal_pattern_counter.
michael@0 1670 cpi->temporal_pattern_counter = 0;
michael@0 1671 reset_temporal_layer_change(cpi, oxcf, prev_number_of_layers);
michael@0 1672 }
michael@0 1673
michael@0 1674 cm->Width = cpi->oxcf.Width;
michael@0 1675 cm->Height = cpi->oxcf.Height;
michael@0 1676
michael@0 1677 /* TODO(jkoleszar): if an internal spatial resampling is active,
michael@0 1678 * and we downsize the input image, maybe we should clear the
michael@0 1679 * internal scale immediately rather than waiting for it to
michael@0 1680 * correct.
michael@0 1681 */
michael@0 1682
michael@0 1683 /* VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs) */
michael@0 1684 if (cpi->oxcf.Sharpness > 7)
michael@0 1685 cpi->oxcf.Sharpness = 7;
michael@0 1686
michael@0 1687 cm->sharpness_level = cpi->oxcf.Sharpness;
michael@0 1688
michael@0 1689 if (cm->horiz_scale != NORMAL || cm->vert_scale != NORMAL)
michael@0 1690 {
michael@0 1691 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
michael@0 1692 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
michael@0 1693
michael@0 1694 Scale2Ratio(cm->horiz_scale, &hr, &hs);
michael@0 1695 Scale2Ratio(cm->vert_scale, &vr, &vs);
michael@0 1696
michael@0 1697 /* always go to the next whole number */
michael@0 1698 cm->Width = (hs - 1 + cpi->oxcf.Width * hr) / hs;
michael@0 1699 cm->Height = (vs - 1 + cpi->oxcf.Height * vr) / vs;
michael@0 1700 }
michael@0 1701
michael@0 1702 if (last_w != cpi->oxcf.Width || last_h != cpi->oxcf.Height)
michael@0 1703 cpi->force_next_frame_intra = 1;
michael@0 1704
michael@0 1705 if (((cm->Width + 15) & 0xfffffff0) !=
michael@0 1706 cm->yv12_fb[cm->lst_fb_idx].y_width ||
michael@0 1707 ((cm->Height + 15) & 0xfffffff0) !=
michael@0 1708 cm->yv12_fb[cm->lst_fb_idx].y_height ||
michael@0 1709 cm->yv12_fb[cm->lst_fb_idx].y_width == 0)
michael@0 1710 {
michael@0 1711 dealloc_raw_frame_buffers(cpi);
michael@0 1712 alloc_raw_frame_buffers(cpi);
michael@0 1713 vp8_alloc_compressor_data(cpi);
michael@0 1714 }
michael@0 1715
michael@0 1716 if (cpi->oxcf.fixed_q >= 0)
michael@0 1717 {
michael@0 1718 cpi->last_q[0] = cpi->oxcf.fixed_q;
michael@0 1719 cpi->last_q[1] = cpi->oxcf.fixed_q;
michael@0 1720 }
michael@0 1721
michael@0 1722 cpi->Speed = cpi->oxcf.cpu_used;
michael@0 1723
michael@0 1724 /* force to allowlag to 0 if lag_in_frames is 0; */
michael@0 1725 if (cpi->oxcf.lag_in_frames == 0)
michael@0 1726 {
michael@0 1727 cpi->oxcf.allow_lag = 0;
michael@0 1728 }
michael@0 1729 /* Limit on lag buffers as these are not currently dynamically allocated */
michael@0 1730 else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS)
michael@0 1731 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
michael@0 1732
michael@0 1733 /* YX Temp */
michael@0 1734 cpi->alt_ref_source = NULL;
michael@0 1735 cpi->is_src_frame_alt_ref = 0;
michael@0 1736
michael@0 1737 #if CONFIG_TEMPORAL_DENOISING
michael@0 1738 if (cpi->oxcf.noise_sensitivity)
michael@0 1739 {
michael@0 1740 if (!cpi->denoiser.yv12_mc_running_avg.buffer_alloc)
michael@0 1741 {
michael@0 1742 int width = (cpi->oxcf.Width + 15) & ~15;
michael@0 1743 int height = (cpi->oxcf.Height + 15) & ~15;
michael@0 1744 vp8_denoiser_allocate(&cpi->denoiser, width, height);
michael@0 1745 }
michael@0 1746 }
michael@0 1747 #endif
michael@0 1748
michael@0 1749 #if 0
michael@0 1750 /* Experimental RD Code */
michael@0 1751 cpi->frame_distortion = 0;
michael@0 1752 cpi->last_frame_distortion = 0;
michael@0 1753 #endif
michael@0 1754
michael@0 1755 }
michael@0 1756
michael@0 1757 #define M_LOG2_E 0.693147180559945309417
michael@0 1758 #define log2f(x) (log (x) / (float) M_LOG2_E)
michael@0 1759 static void cal_mvsadcosts(int *mvsadcost[2])
michael@0 1760 {
michael@0 1761 int i = 1;
michael@0 1762
michael@0 1763 mvsadcost [0] [0] = 300;
michael@0 1764 mvsadcost [1] [0] = 300;
michael@0 1765
michael@0 1766 do
michael@0 1767 {
michael@0 1768 double z = 256 * (2 * (log2f(8 * i) + .6));
michael@0 1769 mvsadcost [0][i] = (int) z;
michael@0 1770 mvsadcost [1][i] = (int) z;
michael@0 1771 mvsadcost [0][-i] = (int) z;
michael@0 1772 mvsadcost [1][-i] = (int) z;
michael@0 1773 }
michael@0 1774 while (++i <= mvfp_max);
michael@0 1775 }
michael@0 1776
michael@0 1777 struct VP8_COMP* vp8_create_compressor(VP8_CONFIG *oxcf)
michael@0 1778 {
michael@0 1779 int i;
michael@0 1780
michael@0 1781 VP8_COMP *cpi;
michael@0 1782 VP8_COMMON *cm;
michael@0 1783
michael@0 1784 cpi = vpx_memalign(32, sizeof(VP8_COMP));
michael@0 1785 /* Check that the CPI instance is valid */
michael@0 1786 if (!cpi)
michael@0 1787 return 0;
michael@0 1788
michael@0 1789 cm = &cpi->common;
michael@0 1790
michael@0 1791 vpx_memset(cpi, 0, sizeof(VP8_COMP));
michael@0 1792
michael@0 1793 if (setjmp(cm->error.jmp))
michael@0 1794 {
michael@0 1795 cpi->common.error.setjmp = 0;
michael@0 1796 vp8_remove_compressor(&cpi);
michael@0 1797 return 0;
michael@0 1798 }
michael@0 1799
michael@0 1800 cpi->common.error.setjmp = 1;
michael@0 1801
michael@0 1802 CHECK_MEM_ERROR(cpi->mb.ss, vpx_calloc(sizeof(search_site), (MAX_MVSEARCH_STEPS * 8) + 1));
michael@0 1803
michael@0 1804 vp8_create_common(&cpi->common);
michael@0 1805
michael@0 1806 init_config(cpi, oxcf);
michael@0 1807
michael@0 1808 memcpy(cpi->base_skip_false_prob, vp8cx_base_skip_false_prob, sizeof(vp8cx_base_skip_false_prob));
michael@0 1809 cpi->common.current_video_frame = 0;
michael@0 1810 cpi->temporal_pattern_counter = 0;
michael@0 1811 cpi->kf_overspend_bits = 0;
michael@0 1812 cpi->kf_bitrate_adjustment = 0;
michael@0 1813 cpi->frames_till_gf_update_due = 0;
michael@0 1814 cpi->gf_overspend_bits = 0;
michael@0 1815 cpi->non_gf_bitrate_adjustment = 0;
michael@0 1816 cpi->prob_last_coded = 128;
michael@0 1817 cpi->prob_gf_coded = 128;
michael@0 1818 cpi->prob_intra_coded = 63;
michael@0 1819
michael@0 1820 /* Prime the recent reference frame usage counters.
michael@0 1821 * Hereafter they will be maintained as a sort of moving average
michael@0 1822 */
michael@0 1823 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
michael@0 1824 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
michael@0 1825 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
michael@0 1826 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
michael@0 1827
michael@0 1828 /* Set reference frame sign bias for ALTREF frame to 1 (for now) */
michael@0 1829 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
michael@0 1830
michael@0 1831 cpi->twopass.gf_decay_rate = 0;
michael@0 1832 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
michael@0 1833
michael@0 1834 cpi->gold_is_last = 0 ;
michael@0 1835 cpi->alt_is_last = 0 ;
michael@0 1836 cpi->gold_is_alt = 0 ;
michael@0 1837
michael@0 1838 cpi->active_map_enabled = 0;
michael@0 1839
michael@0 1840 #if 0
michael@0 1841 /* Experimental code for lagged and one pass */
michael@0 1842 /* Initialise one_pass GF frames stats */
michael@0 1843 /* Update stats used for GF selection */
michael@0 1844 if (cpi->pass == 0)
michael@0 1845 {
michael@0 1846 cpi->one_pass_frame_index = 0;
michael@0 1847
michael@0 1848 for (i = 0; i < MAX_LAG_BUFFERS; i++)
michael@0 1849 {
michael@0 1850 cpi->one_pass_frame_stats[i].frames_so_far = 0;
michael@0 1851 cpi->one_pass_frame_stats[i].frame_intra_error = 0.0;
michael@0 1852 cpi->one_pass_frame_stats[i].frame_coded_error = 0.0;
michael@0 1853 cpi->one_pass_frame_stats[i].frame_pcnt_inter = 0.0;
michael@0 1854 cpi->one_pass_frame_stats[i].frame_pcnt_motion = 0.0;
michael@0 1855 cpi->one_pass_frame_stats[i].frame_mvr = 0.0;
michael@0 1856 cpi->one_pass_frame_stats[i].frame_mvr_abs = 0.0;
michael@0 1857 cpi->one_pass_frame_stats[i].frame_mvc = 0.0;
michael@0 1858 cpi->one_pass_frame_stats[i].frame_mvc_abs = 0.0;
michael@0 1859 }
michael@0 1860 }
michael@0 1861 #endif
michael@0 1862
michael@0 1863 /* Should we use the cyclic refresh method.
michael@0 1864 * Currently this is tied to error resilliant mode
michael@0 1865 */
michael@0 1866 cpi->cyclic_refresh_mode_enabled = cpi->oxcf.error_resilient_mode;
michael@0 1867 cpi->cyclic_refresh_mode_max_mbs_perframe = (cpi->common.mb_rows * cpi->common.mb_cols) / 5;
michael@0 1868 cpi->cyclic_refresh_mode_index = 0;
michael@0 1869 cpi->cyclic_refresh_q = 32;
michael@0 1870
michael@0 1871 if (cpi->cyclic_refresh_mode_enabled)
michael@0 1872 {
michael@0 1873 CHECK_MEM_ERROR(cpi->cyclic_refresh_map, vpx_calloc((cpi->common.mb_rows * cpi->common.mb_cols), 1));
michael@0 1874 }
michael@0 1875 else
michael@0 1876 cpi->cyclic_refresh_map = (signed char *) NULL;
michael@0 1877
michael@0 1878 #ifdef VP8_ENTROPY_STATS
michael@0 1879 init_context_counters();
michael@0 1880 #endif
michael@0 1881
michael@0 1882 /*Initialize the feed-forward activity masking.*/
michael@0 1883 cpi->activity_avg = 90<<12;
michael@0 1884
michael@0 1885 /* Give a sensible default for the first frame. */
michael@0 1886 cpi->frames_since_key = 8;
michael@0 1887 cpi->key_frame_frequency = cpi->oxcf.key_freq;
michael@0 1888 cpi->this_key_frame_forced = 0;
michael@0 1889 cpi->next_key_frame_forced = 0;
michael@0 1890
michael@0 1891 cpi->source_alt_ref_pending = 0;
michael@0 1892 cpi->source_alt_ref_active = 0;
michael@0 1893 cpi->common.refresh_alt_ref_frame = 0;
michael@0 1894
michael@0 1895 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
michael@0 1896 #if CONFIG_INTERNAL_STATS
michael@0 1897 cpi->b_calculate_ssimg = 0;
michael@0 1898
michael@0 1899 cpi->count = 0;
michael@0 1900 cpi->bytes = 0;
michael@0 1901
michael@0 1902 if (cpi->b_calculate_psnr)
michael@0 1903 {
michael@0 1904 cpi->total_sq_error = 0.0;
michael@0 1905 cpi->total_sq_error2 = 0.0;
michael@0 1906 cpi->total_y = 0.0;
michael@0 1907 cpi->total_u = 0.0;
michael@0 1908 cpi->total_v = 0.0;
michael@0 1909 cpi->total = 0.0;
michael@0 1910 cpi->totalp_y = 0.0;
michael@0 1911 cpi->totalp_u = 0.0;
michael@0 1912 cpi->totalp_v = 0.0;
michael@0 1913 cpi->totalp = 0.0;
michael@0 1914 cpi->tot_recode_hits = 0;
michael@0 1915 cpi->summed_quality = 0;
michael@0 1916 cpi->summed_weights = 0;
michael@0 1917 }
michael@0 1918
michael@0 1919 if (cpi->b_calculate_ssimg)
michael@0 1920 {
michael@0 1921 cpi->total_ssimg_y = 0;
michael@0 1922 cpi->total_ssimg_u = 0;
michael@0 1923 cpi->total_ssimg_v = 0;
michael@0 1924 cpi->total_ssimg_all = 0;
michael@0 1925 }
michael@0 1926
michael@0 1927 #endif
michael@0 1928
michael@0 1929 cpi->first_time_stamp_ever = 0x7FFFFFFF;
michael@0 1930
michael@0 1931 cpi->frames_till_gf_update_due = 0;
michael@0 1932 cpi->key_frame_count = 1;
michael@0 1933
michael@0 1934 cpi->ni_av_qi = cpi->oxcf.worst_allowed_q;
michael@0 1935 cpi->ni_tot_qi = 0;
michael@0 1936 cpi->ni_frames = 0;
michael@0 1937 cpi->total_byte_count = 0;
michael@0 1938
michael@0 1939 cpi->drop_frame = 0;
michael@0 1940
michael@0 1941 cpi->rate_correction_factor = 1.0;
michael@0 1942 cpi->key_frame_rate_correction_factor = 1.0;
michael@0 1943 cpi->gf_rate_correction_factor = 1.0;
michael@0 1944 cpi->twopass.est_max_qcorrection_factor = 1.0;
michael@0 1945
michael@0 1946 for (i = 0; i < KEY_FRAME_CONTEXT; i++)
michael@0 1947 {
michael@0 1948 cpi->prior_key_frame_distance[i] = (int)cpi->output_framerate;
michael@0 1949 }
michael@0 1950
michael@0 1951 #ifdef OUTPUT_YUV_SRC
michael@0 1952 yuv_file = fopen("bd.yuv", "ab");
michael@0 1953 #endif
michael@0 1954
michael@0 1955 #if 0
michael@0 1956 framepsnr = fopen("framepsnr.stt", "a");
michael@0 1957 kf_list = fopen("kf_list.stt", "w");
michael@0 1958 #endif
michael@0 1959
michael@0 1960 cpi->output_pkt_list = oxcf->output_pkt_list;
michael@0 1961
michael@0 1962 #if !(CONFIG_REALTIME_ONLY)
michael@0 1963
michael@0 1964 if (cpi->pass == 1)
michael@0 1965 {
michael@0 1966 vp8_init_first_pass(cpi);
michael@0 1967 }
michael@0 1968 else if (cpi->pass == 2)
michael@0 1969 {
michael@0 1970 size_t packet_sz = sizeof(FIRSTPASS_STATS);
michael@0 1971 int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
michael@0 1972
michael@0 1973 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
michael@0 1974 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
michael@0 1975 cpi->twopass.stats_in_end = (void*)((char *)cpi->twopass.stats_in
michael@0 1976 + (packets - 1) * packet_sz);
michael@0 1977 vp8_init_second_pass(cpi);
michael@0 1978 }
michael@0 1979
michael@0 1980 #endif
michael@0 1981
michael@0 1982 if (cpi->compressor_speed == 2)
michael@0 1983 {
michael@0 1984 cpi->avg_encode_time = 0;
michael@0 1985 cpi->avg_pick_mode_time = 0;
michael@0 1986 }
michael@0 1987
michael@0 1988 vp8_set_speed_features(cpi);
michael@0 1989
michael@0 1990 /* Set starting values of RD threshold multipliers (128 = *1) */
michael@0 1991 for (i = 0; i < MAX_MODES; i++)
michael@0 1992 {
michael@0 1993 cpi->mb.rd_thresh_mult[i] = 128;
michael@0 1994 }
michael@0 1995
michael@0 1996 #ifdef VP8_ENTROPY_STATS
michael@0 1997 init_mv_ref_counts();
michael@0 1998 #endif
michael@0 1999
michael@0 2000 #if CONFIG_MULTITHREAD
michael@0 2001 if(vp8cx_create_encoder_threads(cpi))
michael@0 2002 {
michael@0 2003 vp8_remove_compressor(&cpi);
michael@0 2004 return 0;
michael@0 2005 }
michael@0 2006 #endif
michael@0 2007
michael@0 2008 cpi->fn_ptr[BLOCK_16X16].sdf = vp8_sad16x16;
michael@0 2009 cpi->fn_ptr[BLOCK_16X16].vf = vp8_variance16x16;
michael@0 2010 cpi->fn_ptr[BLOCK_16X16].svf = vp8_sub_pixel_variance16x16;
michael@0 2011 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_h = vp8_variance_halfpixvar16x16_h;
michael@0 2012 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_v = vp8_variance_halfpixvar16x16_v;
michael@0 2013 cpi->fn_ptr[BLOCK_16X16].svf_halfpix_hv = vp8_variance_halfpixvar16x16_hv;
michael@0 2014 cpi->fn_ptr[BLOCK_16X16].sdx3f = vp8_sad16x16x3;
michael@0 2015 cpi->fn_ptr[BLOCK_16X16].sdx8f = vp8_sad16x16x8;
michael@0 2016 cpi->fn_ptr[BLOCK_16X16].sdx4df = vp8_sad16x16x4d;
michael@0 2017
michael@0 2018 cpi->fn_ptr[BLOCK_16X8].sdf = vp8_sad16x8;
michael@0 2019 cpi->fn_ptr[BLOCK_16X8].vf = vp8_variance16x8;
michael@0 2020 cpi->fn_ptr[BLOCK_16X8].svf = vp8_sub_pixel_variance16x8;
michael@0 2021 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_h = NULL;
michael@0 2022 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_v = NULL;
michael@0 2023 cpi->fn_ptr[BLOCK_16X8].svf_halfpix_hv = NULL;
michael@0 2024 cpi->fn_ptr[BLOCK_16X8].sdx3f = vp8_sad16x8x3;
michael@0 2025 cpi->fn_ptr[BLOCK_16X8].sdx8f = vp8_sad16x8x8;
michael@0 2026 cpi->fn_ptr[BLOCK_16X8].sdx4df = vp8_sad16x8x4d;
michael@0 2027
michael@0 2028 cpi->fn_ptr[BLOCK_8X16].sdf = vp8_sad8x16;
michael@0 2029 cpi->fn_ptr[BLOCK_8X16].vf = vp8_variance8x16;
michael@0 2030 cpi->fn_ptr[BLOCK_8X16].svf = vp8_sub_pixel_variance8x16;
michael@0 2031 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_h = NULL;
michael@0 2032 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_v = NULL;
michael@0 2033 cpi->fn_ptr[BLOCK_8X16].svf_halfpix_hv = NULL;
michael@0 2034 cpi->fn_ptr[BLOCK_8X16].sdx3f = vp8_sad8x16x3;
michael@0 2035 cpi->fn_ptr[BLOCK_8X16].sdx8f = vp8_sad8x16x8;
michael@0 2036 cpi->fn_ptr[BLOCK_8X16].sdx4df = vp8_sad8x16x4d;
michael@0 2037
michael@0 2038 cpi->fn_ptr[BLOCK_8X8].sdf = vp8_sad8x8;
michael@0 2039 cpi->fn_ptr[BLOCK_8X8].vf = vp8_variance8x8;
michael@0 2040 cpi->fn_ptr[BLOCK_8X8].svf = vp8_sub_pixel_variance8x8;
michael@0 2041 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_h = NULL;
michael@0 2042 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_v = NULL;
michael@0 2043 cpi->fn_ptr[BLOCK_8X8].svf_halfpix_hv = NULL;
michael@0 2044 cpi->fn_ptr[BLOCK_8X8].sdx3f = vp8_sad8x8x3;
michael@0 2045 cpi->fn_ptr[BLOCK_8X8].sdx8f = vp8_sad8x8x8;
michael@0 2046 cpi->fn_ptr[BLOCK_8X8].sdx4df = vp8_sad8x8x4d;
michael@0 2047
michael@0 2048 cpi->fn_ptr[BLOCK_4X4].sdf = vp8_sad4x4;
michael@0 2049 cpi->fn_ptr[BLOCK_4X4].vf = vp8_variance4x4;
michael@0 2050 cpi->fn_ptr[BLOCK_4X4].svf = vp8_sub_pixel_variance4x4;
michael@0 2051 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_h = NULL;
michael@0 2052 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_v = NULL;
michael@0 2053 cpi->fn_ptr[BLOCK_4X4].svf_halfpix_hv = NULL;
michael@0 2054 cpi->fn_ptr[BLOCK_4X4].sdx3f = vp8_sad4x4x3;
michael@0 2055 cpi->fn_ptr[BLOCK_4X4].sdx8f = vp8_sad4x4x8;
michael@0 2056 cpi->fn_ptr[BLOCK_4X4].sdx4df = vp8_sad4x4x4d;
michael@0 2057
michael@0 2058 #if ARCH_X86 || ARCH_X86_64
michael@0 2059 cpi->fn_ptr[BLOCK_16X16].copymem = vp8_copy32xn;
michael@0 2060 cpi->fn_ptr[BLOCK_16X8].copymem = vp8_copy32xn;
michael@0 2061 cpi->fn_ptr[BLOCK_8X16].copymem = vp8_copy32xn;
michael@0 2062 cpi->fn_ptr[BLOCK_8X8].copymem = vp8_copy32xn;
michael@0 2063 cpi->fn_ptr[BLOCK_4X4].copymem = vp8_copy32xn;
michael@0 2064 #endif
michael@0 2065
michael@0 2066 cpi->full_search_sad = vp8_full_search_sad;
michael@0 2067 cpi->diamond_search_sad = vp8_diamond_search_sad;
michael@0 2068 cpi->refining_search_sad = vp8_refining_search_sad;
michael@0 2069
michael@0 2070 /* make sure frame 1 is okay */
michael@0 2071 cpi->mb.error_bins[0] = cpi->common.MBs;
michael@0 2072
michael@0 2073 /* vp8cx_init_quantizer() is first called here. Add check in
michael@0 2074 * vp8cx_frame_init_quantizer() so that vp8cx_init_quantizer is only
michael@0 2075 * called later when needed. This will avoid unnecessary calls of
michael@0 2076 * vp8cx_init_quantizer() for every frame.
michael@0 2077 */
michael@0 2078 vp8cx_init_quantizer(cpi);
michael@0 2079
michael@0 2080 vp8_loop_filter_init(cm);
michael@0 2081
michael@0 2082 cpi->common.error.setjmp = 0;
michael@0 2083
michael@0 2084 #if CONFIG_MULTI_RES_ENCODING
michael@0 2085
michael@0 2086 /* Calculate # of MBs in a row in lower-resolution level image. */
michael@0 2087 if (cpi->oxcf.mr_encoder_id > 0)
michael@0 2088 vp8_cal_low_res_mb_cols(cpi);
michael@0 2089
michael@0 2090 #endif
michael@0 2091
michael@0 2092 /* setup RD costs to MACROBLOCK struct */
michael@0 2093
michael@0 2094 cpi->mb.mvcost[0] = &cpi->rd_costs.mvcosts[0][mv_max+1];
michael@0 2095 cpi->mb.mvcost[1] = &cpi->rd_costs.mvcosts[1][mv_max+1];
michael@0 2096 cpi->mb.mvsadcost[0] = &cpi->rd_costs.mvsadcosts[0][mvfp_max+1];
michael@0 2097 cpi->mb.mvsadcost[1] = &cpi->rd_costs.mvsadcosts[1][mvfp_max+1];
michael@0 2098
michael@0 2099 cal_mvsadcosts(cpi->mb.mvsadcost);
michael@0 2100
michael@0 2101 cpi->mb.mbmode_cost = cpi->rd_costs.mbmode_cost;
michael@0 2102 cpi->mb.intra_uv_mode_cost = cpi->rd_costs.intra_uv_mode_cost;
michael@0 2103 cpi->mb.bmode_costs = cpi->rd_costs.bmode_costs;
michael@0 2104 cpi->mb.inter_bmode_costs = cpi->rd_costs.inter_bmode_costs;
michael@0 2105 cpi->mb.token_costs = cpi->rd_costs.token_costs;
michael@0 2106
michael@0 2107 /* setup block ptrs & offsets */
michael@0 2108 vp8_setup_block_ptrs(&cpi->mb);
michael@0 2109 vp8_setup_block_dptrs(&cpi->mb.e_mbd);
michael@0 2110
michael@0 2111 return cpi;
michael@0 2112 }
michael@0 2113
michael@0 2114
michael@0 2115 void vp8_remove_compressor(VP8_COMP **ptr)
michael@0 2116 {
michael@0 2117 VP8_COMP *cpi = *ptr;
michael@0 2118
michael@0 2119 if (!cpi)
michael@0 2120 return;
michael@0 2121
michael@0 2122 if (cpi && (cpi->common.current_video_frame > 0))
michael@0 2123 {
michael@0 2124 #if !(CONFIG_REALTIME_ONLY)
michael@0 2125
michael@0 2126 if (cpi->pass == 2)
michael@0 2127 {
michael@0 2128 vp8_end_second_pass(cpi);
michael@0 2129 }
michael@0 2130
michael@0 2131 #endif
michael@0 2132
michael@0 2133 #ifdef VP8_ENTROPY_STATS
michael@0 2134 print_context_counters();
michael@0 2135 print_tree_update_probs();
michael@0 2136 print_mode_context();
michael@0 2137 #endif
michael@0 2138
michael@0 2139 #if CONFIG_INTERNAL_STATS
michael@0 2140
michael@0 2141 if (cpi->pass != 1)
michael@0 2142 {
michael@0 2143 FILE *f = fopen("opsnr.stt", "a");
michael@0 2144 double time_encoded = (cpi->last_end_time_stamp_seen
michael@0 2145 - cpi->first_time_stamp_ever) / 10000000.000;
michael@0 2146 double total_encode_time = (cpi->time_receive_data +
michael@0 2147 cpi->time_compress_data) / 1000.000;
michael@0 2148 double dr = (double)cpi->bytes * 8.0 / 1000.0 / time_encoded;
michael@0 2149
michael@0 2150 if (cpi->b_calculate_psnr)
michael@0 2151 {
michael@0 2152 YV12_BUFFER_CONFIG *lst_yv12 =
michael@0 2153 &cpi->common.yv12_fb[cpi->common.lst_fb_idx];
michael@0 2154
michael@0 2155 if (cpi->oxcf.number_of_layers > 1)
michael@0 2156 {
michael@0 2157 int i;
michael@0 2158
michael@0 2159 fprintf(f, "Layer\tBitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\t"
michael@0 2160 "GLPsnrP\tVPXSSIM\t\n");
michael@0 2161 for (i=0; i<(int)cpi->oxcf.number_of_layers; i++)
michael@0 2162 {
michael@0 2163 double dr = (double)cpi->bytes_in_layer[i] *
michael@0 2164 8.0 / 1000.0 / time_encoded;
michael@0 2165 double samples = 3.0 / 2 * cpi->frames_in_layer[i] *
michael@0 2166 lst_yv12->y_width * lst_yv12->y_height;
michael@0 2167 double total_psnr = vp8_mse2psnr(samples, 255.0,
michael@0 2168 cpi->total_error2[i]);
michael@0 2169 double total_psnr2 = vp8_mse2psnr(samples, 255.0,
michael@0 2170 cpi->total_error2_p[i]);
michael@0 2171 double total_ssim = 100 * pow(cpi->sum_ssim[i] /
michael@0 2172 cpi->sum_weights[i], 8.0);
michael@0 2173
michael@0 2174 fprintf(f, "%5d\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
michael@0 2175 "%7.3f\t%7.3f\n",
michael@0 2176 i, dr,
michael@0 2177 cpi->sum_psnr[i] / cpi->frames_in_layer[i],
michael@0 2178 total_psnr,
michael@0 2179 cpi->sum_psnr_p[i] / cpi->frames_in_layer[i],
michael@0 2180 total_psnr2, total_ssim);
michael@0 2181 }
michael@0 2182 }
michael@0 2183 else
michael@0 2184 {
michael@0 2185 double samples = 3.0 / 2 * cpi->count *
michael@0 2186 lst_yv12->y_width * lst_yv12->y_height;
michael@0 2187 double total_psnr = vp8_mse2psnr(samples, 255.0,
michael@0 2188 cpi->total_sq_error);
michael@0 2189 double total_psnr2 = vp8_mse2psnr(samples, 255.0,
michael@0 2190 cpi->total_sq_error2);
michael@0 2191 double total_ssim = 100 * pow(cpi->summed_quality /
michael@0 2192 cpi->summed_weights, 8.0);
michael@0 2193
michael@0 2194 fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\t"
michael@0 2195 "GLPsnrP\tVPXSSIM\t Time(us)\n");
michael@0 2196 fprintf(f, "%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
michael@0 2197 "%7.3f\t%8.0f\n",
michael@0 2198 dr, cpi->total / cpi->count, total_psnr,
michael@0 2199 cpi->totalp / cpi->count, total_psnr2,
michael@0 2200 total_ssim, total_encode_time);
michael@0 2201 }
michael@0 2202 }
michael@0 2203
michael@0 2204 if (cpi->b_calculate_ssimg)
michael@0 2205 {
michael@0 2206 if (cpi->oxcf.number_of_layers > 1)
michael@0 2207 {
michael@0 2208 int i;
michael@0 2209
michael@0 2210 fprintf(f, "Layer\tBitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t"
michael@0 2211 "Time(us)\n");
michael@0 2212 for (i=0; i<(int)cpi->oxcf.number_of_layers; i++)
michael@0 2213 {
michael@0 2214 double dr = (double)cpi->bytes_in_layer[i] *
michael@0 2215 8.0 / 1000.0 / time_encoded;
michael@0 2216 fprintf(f, "%5d\t%7.3f\t%6.4f\t"
michael@0 2217 "%6.4f\t%6.4f\t%6.4f\t%8.0f\n",
michael@0 2218 i, dr,
michael@0 2219 cpi->total_ssimg_y_in_layer[i] /
michael@0 2220 cpi->frames_in_layer[i],
michael@0 2221 cpi->total_ssimg_u_in_layer[i] /
michael@0 2222 cpi->frames_in_layer[i],
michael@0 2223 cpi->total_ssimg_v_in_layer[i] /
michael@0 2224 cpi->frames_in_layer[i],
michael@0 2225 cpi->total_ssimg_all_in_layer[i] /
michael@0 2226 cpi->frames_in_layer[i],
michael@0 2227 total_encode_time);
michael@0 2228 }
michael@0 2229 }
michael@0 2230 else
michael@0 2231 {
michael@0 2232 fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t"
michael@0 2233 "Time(us)\n");
michael@0 2234 fprintf(f, "%7.3f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
michael@0 2235 cpi->total_ssimg_y / cpi->count,
michael@0 2236 cpi->total_ssimg_u / cpi->count,
michael@0 2237 cpi->total_ssimg_v / cpi->count,
michael@0 2238 cpi->total_ssimg_all / cpi->count, total_encode_time);
michael@0 2239 }
michael@0 2240 }
michael@0 2241
michael@0 2242 fclose(f);
michael@0 2243 #if 0
michael@0 2244 f = fopen("qskip.stt", "a");
michael@0 2245 fprintf(f, "minq:%d -maxq:%d skiptrue:skipfalse = %d:%d\n", cpi->oxcf.best_allowed_q, cpi->oxcf.worst_allowed_q, skiptruecount, skipfalsecount);
michael@0 2246 fclose(f);
michael@0 2247 #endif
michael@0 2248
michael@0 2249 }
michael@0 2250
michael@0 2251 #endif
michael@0 2252
michael@0 2253
michael@0 2254 #ifdef SPEEDSTATS
michael@0 2255
michael@0 2256 if (cpi->compressor_speed == 2)
michael@0 2257 {
michael@0 2258 int i;
michael@0 2259 FILE *f = fopen("cxspeed.stt", "a");
michael@0 2260 cnt_pm /= cpi->common.MBs;
michael@0 2261
michael@0 2262 for (i = 0; i < 16; i++)
michael@0 2263 fprintf(f, "%5d", frames_at_speed[i]);
michael@0 2264
michael@0 2265 fprintf(f, "\n");
michael@0 2266 fclose(f);
michael@0 2267 }
michael@0 2268
michael@0 2269 #endif
michael@0 2270
michael@0 2271
michael@0 2272 #ifdef MODE_STATS
michael@0 2273 {
michael@0 2274 extern int count_mb_seg[4];
michael@0 2275 FILE *f = fopen("modes.stt", "a");
michael@0 2276 double dr = (double)cpi->framerate * (double)bytes * (double)8 / (double)count / (double)1000 ;
michael@0 2277 fprintf(f, "intra_mode in Intra Frames:\n");
michael@0 2278 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d\n", y_modes[0], y_modes[1], y_modes[2], y_modes[3], y_modes[4]);
michael@0 2279 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", uv_modes[0], uv_modes[1], uv_modes[2], uv_modes[3]);
michael@0 2280 fprintf(f, "B: ");
michael@0 2281 {
michael@0 2282 int i;
michael@0 2283
michael@0 2284 for (i = 0; i < 10; i++)
michael@0 2285 fprintf(f, "%8d, ", b_modes[i]);
michael@0 2286
michael@0 2287 fprintf(f, "\n");
michael@0 2288
michael@0 2289 }
michael@0 2290
michael@0 2291 fprintf(f, "Modes in Inter Frames:\n");
michael@0 2292 fprintf(f, "Y: %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d, %8d\n",
michael@0 2293 inter_y_modes[0], inter_y_modes[1], inter_y_modes[2], inter_y_modes[3], inter_y_modes[4],
michael@0 2294 inter_y_modes[5], inter_y_modes[6], inter_y_modes[7], inter_y_modes[8], inter_y_modes[9]);
michael@0 2295 fprintf(f, "UV:%8d, %8d, %8d, %8d\n", inter_uv_modes[0], inter_uv_modes[1], inter_uv_modes[2], inter_uv_modes[3]);
michael@0 2296 fprintf(f, "B: ");
michael@0 2297 {
michael@0 2298 int i;
michael@0 2299
michael@0 2300 for (i = 0; i < 15; i++)
michael@0 2301 fprintf(f, "%8d, ", inter_b_modes[i]);
michael@0 2302
michael@0 2303 fprintf(f, "\n");
michael@0 2304
michael@0 2305 }
michael@0 2306 fprintf(f, "P:%8d, %8d, %8d, %8d\n", count_mb_seg[0], count_mb_seg[1], count_mb_seg[2], count_mb_seg[3]);
michael@0 2307 fprintf(f, "PB:%8d, %8d, %8d, %8d\n", inter_b_modes[LEFT4X4], inter_b_modes[ABOVE4X4], inter_b_modes[ZERO4X4], inter_b_modes[NEW4X4]);
michael@0 2308
michael@0 2309
michael@0 2310
michael@0 2311 fclose(f);
michael@0 2312 }
michael@0 2313 #endif
michael@0 2314
michael@0 2315 #ifdef VP8_ENTROPY_STATS
michael@0 2316 {
michael@0 2317 int i, j, k;
michael@0 2318 FILE *fmode = fopen("modecontext.c", "w");
michael@0 2319
michael@0 2320 fprintf(fmode, "\n#include \"entropymode.h\"\n\n");
michael@0 2321 fprintf(fmode, "const unsigned int vp8_kf_default_bmode_counts ");
michael@0 2322 fprintf(fmode, "[VP8_BINTRAMODES] [VP8_BINTRAMODES] [VP8_BINTRAMODES] =\n{\n");
michael@0 2323
michael@0 2324 for (i = 0; i < 10; i++)
michael@0 2325 {
michael@0 2326
michael@0 2327 fprintf(fmode, " { /* Above Mode : %d */\n", i);
michael@0 2328
michael@0 2329 for (j = 0; j < 10; j++)
michael@0 2330 {
michael@0 2331
michael@0 2332 fprintf(fmode, " {");
michael@0 2333
michael@0 2334 for (k = 0; k < 10; k++)
michael@0 2335 {
michael@0 2336 if (!intra_mode_stats[i][j][k])
michael@0 2337 fprintf(fmode, " %5d, ", 1);
michael@0 2338 else
michael@0 2339 fprintf(fmode, " %5d, ", intra_mode_stats[i][j][k]);
michael@0 2340 }
michael@0 2341
michael@0 2342 fprintf(fmode, "}, /* left_mode %d */\n", j);
michael@0 2343
michael@0 2344 }
michael@0 2345
michael@0 2346 fprintf(fmode, " },\n");
michael@0 2347
michael@0 2348 }
michael@0 2349
michael@0 2350 fprintf(fmode, "};\n");
michael@0 2351 fclose(fmode);
michael@0 2352 }
michael@0 2353 #endif
michael@0 2354
michael@0 2355
michael@0 2356 #if defined(SECTIONBITS_OUTPUT)
michael@0 2357
michael@0 2358 if (0)
michael@0 2359 {
michael@0 2360 int i;
michael@0 2361 FILE *f = fopen("tokenbits.stt", "a");
michael@0 2362
michael@0 2363 for (i = 0; i < 28; i++)
michael@0 2364 fprintf(f, "%8d", (int)(Sectionbits[i] / 256));
michael@0 2365
michael@0 2366 fprintf(f, "\n");
michael@0 2367 fclose(f);
michael@0 2368 }
michael@0 2369
michael@0 2370 #endif
michael@0 2371
michael@0 2372 #if 0
michael@0 2373 {
michael@0 2374 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
michael@0 2375 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
michael@0 2376 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);
michael@0 2377 }
michael@0 2378 #endif
michael@0 2379
michael@0 2380 }
michael@0 2381
michael@0 2382 #if CONFIG_MULTITHREAD
michael@0 2383 vp8cx_remove_encoder_threads(cpi);
michael@0 2384 #endif
michael@0 2385
michael@0 2386 #if CONFIG_TEMPORAL_DENOISING
michael@0 2387 vp8_denoiser_free(&cpi->denoiser);
michael@0 2388 #endif
michael@0 2389 dealloc_compressor_data(cpi);
michael@0 2390 vpx_free(cpi->mb.ss);
michael@0 2391 vpx_free(cpi->tok);
michael@0 2392 vpx_free(cpi->cyclic_refresh_map);
michael@0 2393
michael@0 2394 vp8_remove_common(&cpi->common);
michael@0 2395 vpx_free(cpi);
michael@0 2396 *ptr = 0;
michael@0 2397
michael@0 2398 #ifdef OUTPUT_YUV_SRC
michael@0 2399 fclose(yuv_file);
michael@0 2400 #endif
michael@0 2401
michael@0 2402 #if 0
michael@0 2403
michael@0 2404 if (keyfile)
michael@0 2405 fclose(keyfile);
michael@0 2406
michael@0 2407 if (framepsnr)
michael@0 2408 fclose(framepsnr);
michael@0 2409
michael@0 2410 if (kf_list)
michael@0 2411 fclose(kf_list);
michael@0 2412
michael@0 2413 #endif
michael@0 2414
michael@0 2415 }
michael@0 2416
michael@0 2417
michael@0 2418 static uint64_t calc_plane_error(unsigned char *orig, int orig_stride,
michael@0 2419 unsigned char *recon, int recon_stride,
michael@0 2420 unsigned int cols, unsigned int rows)
michael@0 2421 {
michael@0 2422 unsigned int row, col;
michael@0 2423 uint64_t total_sse = 0;
michael@0 2424 int diff;
michael@0 2425
michael@0 2426 for (row = 0; row + 16 <= rows; row += 16)
michael@0 2427 {
michael@0 2428 for (col = 0; col + 16 <= cols; col += 16)
michael@0 2429 {
michael@0 2430 unsigned int sse;
michael@0 2431
michael@0 2432 vp8_mse16x16(orig + col, orig_stride,
michael@0 2433 recon + col, recon_stride,
michael@0 2434 &sse);
michael@0 2435 total_sse += sse;
michael@0 2436 }
michael@0 2437
michael@0 2438 /* Handle odd-sized width */
michael@0 2439 if (col < cols)
michael@0 2440 {
michael@0 2441 unsigned int border_row, border_col;
michael@0 2442 unsigned char *border_orig = orig;
michael@0 2443 unsigned char *border_recon = recon;
michael@0 2444
michael@0 2445 for (border_row = 0; border_row < 16; border_row++)
michael@0 2446 {
michael@0 2447 for (border_col = col; border_col < cols; border_col++)
michael@0 2448 {
michael@0 2449 diff = border_orig[border_col] - border_recon[border_col];
michael@0 2450 total_sse += diff * diff;
michael@0 2451 }
michael@0 2452
michael@0 2453 border_orig += orig_stride;
michael@0 2454 border_recon += recon_stride;
michael@0 2455 }
michael@0 2456 }
michael@0 2457
michael@0 2458 orig += orig_stride * 16;
michael@0 2459 recon += recon_stride * 16;
michael@0 2460 }
michael@0 2461
michael@0 2462 /* Handle odd-sized height */
michael@0 2463 for (; row < rows; row++)
michael@0 2464 {
michael@0 2465 for (col = 0; col < cols; col++)
michael@0 2466 {
michael@0 2467 diff = orig[col] - recon[col];
michael@0 2468 total_sse += diff * diff;
michael@0 2469 }
michael@0 2470
michael@0 2471 orig += orig_stride;
michael@0 2472 recon += recon_stride;
michael@0 2473 }
michael@0 2474
michael@0 2475 vp8_clear_system_state();
michael@0 2476 return total_sse;
michael@0 2477 }
michael@0 2478
michael@0 2479
michael@0 2480 static void generate_psnr_packet(VP8_COMP *cpi)
michael@0 2481 {
michael@0 2482 YV12_BUFFER_CONFIG *orig = cpi->Source;
michael@0 2483 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
michael@0 2484 struct vpx_codec_cx_pkt pkt;
michael@0 2485 uint64_t sse;
michael@0 2486 int i;
michael@0 2487 unsigned int width = cpi->common.Width;
michael@0 2488 unsigned int height = cpi->common.Height;
michael@0 2489
michael@0 2490 pkt.kind = VPX_CODEC_PSNR_PKT;
michael@0 2491 sse = calc_plane_error(orig->y_buffer, orig->y_stride,
michael@0 2492 recon->y_buffer, recon->y_stride,
michael@0 2493 width, height);
michael@0 2494 pkt.data.psnr.sse[0] = sse;
michael@0 2495 pkt.data.psnr.sse[1] = sse;
michael@0 2496 pkt.data.psnr.samples[0] = width * height;
michael@0 2497 pkt.data.psnr.samples[1] = width * height;
michael@0 2498
michael@0 2499 width = (width + 1) / 2;
michael@0 2500 height = (height + 1) / 2;
michael@0 2501
michael@0 2502 sse = calc_plane_error(orig->u_buffer, orig->uv_stride,
michael@0 2503 recon->u_buffer, recon->uv_stride,
michael@0 2504 width, height);
michael@0 2505 pkt.data.psnr.sse[0] += sse;
michael@0 2506 pkt.data.psnr.sse[2] = sse;
michael@0 2507 pkt.data.psnr.samples[0] += width * height;
michael@0 2508 pkt.data.psnr.samples[2] = width * height;
michael@0 2509
michael@0 2510 sse = calc_plane_error(orig->v_buffer, orig->uv_stride,
michael@0 2511 recon->v_buffer, recon->uv_stride,
michael@0 2512 width, height);
michael@0 2513 pkt.data.psnr.sse[0] += sse;
michael@0 2514 pkt.data.psnr.sse[3] = sse;
michael@0 2515 pkt.data.psnr.samples[0] += width * height;
michael@0 2516 pkt.data.psnr.samples[3] = width * height;
michael@0 2517
michael@0 2518 for (i = 0; i < 4; i++)
michael@0 2519 pkt.data.psnr.psnr[i] = vp8_mse2psnr(pkt.data.psnr.samples[i], 255.0,
michael@0 2520 (double)(pkt.data.psnr.sse[i]));
michael@0 2521
michael@0 2522 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
michael@0 2523 }
michael@0 2524
michael@0 2525
michael@0 2526 int vp8_use_as_reference(VP8_COMP *cpi, int ref_frame_flags)
michael@0 2527 {
michael@0 2528 if (ref_frame_flags > 7)
michael@0 2529 return -1 ;
michael@0 2530
michael@0 2531 cpi->ref_frame_flags = ref_frame_flags;
michael@0 2532 return 0;
michael@0 2533 }
michael@0 2534 int vp8_update_reference(VP8_COMP *cpi, int ref_frame_flags)
michael@0 2535 {
michael@0 2536 if (ref_frame_flags > 7)
michael@0 2537 return -1 ;
michael@0 2538
michael@0 2539 cpi->common.refresh_golden_frame = 0;
michael@0 2540 cpi->common.refresh_alt_ref_frame = 0;
michael@0 2541 cpi->common.refresh_last_frame = 0;
michael@0 2542
michael@0 2543 if (ref_frame_flags & VP8_LAST_FRAME)
michael@0 2544 cpi->common.refresh_last_frame = 1;
michael@0 2545
michael@0 2546 if (ref_frame_flags & VP8_GOLD_FRAME)
michael@0 2547 cpi->common.refresh_golden_frame = 1;
michael@0 2548
michael@0 2549 if (ref_frame_flags & VP8_ALTR_FRAME)
michael@0 2550 cpi->common.refresh_alt_ref_frame = 1;
michael@0 2551
michael@0 2552 return 0;
michael@0 2553 }
michael@0 2554
michael@0 2555 int vp8_get_reference(VP8_COMP *cpi, enum vpx_ref_frame_type ref_frame_flag, YV12_BUFFER_CONFIG *sd)
michael@0 2556 {
michael@0 2557 VP8_COMMON *cm = &cpi->common;
michael@0 2558 int ref_fb_idx;
michael@0 2559
michael@0 2560 if (ref_frame_flag == VP8_LAST_FRAME)
michael@0 2561 ref_fb_idx = cm->lst_fb_idx;
michael@0 2562 else if (ref_frame_flag == VP8_GOLD_FRAME)
michael@0 2563 ref_fb_idx = cm->gld_fb_idx;
michael@0 2564 else if (ref_frame_flag == VP8_ALTR_FRAME)
michael@0 2565 ref_fb_idx = cm->alt_fb_idx;
michael@0 2566 else
michael@0 2567 return -1;
michael@0 2568
michael@0 2569 vp8_yv12_copy_frame(&cm->yv12_fb[ref_fb_idx], sd);
michael@0 2570
michael@0 2571 return 0;
michael@0 2572 }
michael@0 2573 int vp8_set_reference(VP8_COMP *cpi, enum vpx_ref_frame_type ref_frame_flag, YV12_BUFFER_CONFIG *sd)
michael@0 2574 {
michael@0 2575 VP8_COMMON *cm = &cpi->common;
michael@0 2576
michael@0 2577 int ref_fb_idx;
michael@0 2578
michael@0 2579 if (ref_frame_flag == VP8_LAST_FRAME)
michael@0 2580 ref_fb_idx = cm->lst_fb_idx;
michael@0 2581 else if (ref_frame_flag == VP8_GOLD_FRAME)
michael@0 2582 ref_fb_idx = cm->gld_fb_idx;
michael@0 2583 else if (ref_frame_flag == VP8_ALTR_FRAME)
michael@0 2584 ref_fb_idx = cm->alt_fb_idx;
michael@0 2585 else
michael@0 2586 return -1;
michael@0 2587
michael@0 2588 vp8_yv12_copy_frame(sd, &cm->yv12_fb[ref_fb_idx]);
michael@0 2589
michael@0 2590 return 0;
michael@0 2591 }
michael@0 2592 int vp8_update_entropy(VP8_COMP *cpi, int update)
michael@0 2593 {
michael@0 2594 VP8_COMMON *cm = &cpi->common;
michael@0 2595 cm->refresh_entropy_probs = update;
michael@0 2596
michael@0 2597 return 0;
michael@0 2598 }
michael@0 2599
michael@0 2600
michael@0 2601 #if OUTPUT_YUV_SRC
michael@0 2602 void vp8_write_yuv_frame(const char *name, YV12_BUFFER_CONFIG *s)
michael@0 2603 {
michael@0 2604 FILE *yuv_file = fopen(name, "ab");
michael@0 2605 unsigned char *src = s->y_buffer;
michael@0 2606 int h = s->y_height;
michael@0 2607
michael@0 2608 do
michael@0 2609 {
michael@0 2610 fwrite(src, s->y_width, 1, yuv_file);
michael@0 2611 src += s->y_stride;
michael@0 2612 }
michael@0 2613 while (--h);
michael@0 2614
michael@0 2615 src = s->u_buffer;
michael@0 2616 h = s->uv_height;
michael@0 2617
michael@0 2618 do
michael@0 2619 {
michael@0 2620 fwrite(src, s->uv_width, 1, yuv_file);
michael@0 2621 src += s->uv_stride;
michael@0 2622 }
michael@0 2623 while (--h);
michael@0 2624
michael@0 2625 src = s->v_buffer;
michael@0 2626 h = s->uv_height;
michael@0 2627
michael@0 2628 do
michael@0 2629 {
michael@0 2630 fwrite(src, s->uv_width, 1, yuv_file);
michael@0 2631 src += s->uv_stride;
michael@0 2632 }
michael@0 2633 while (--h);
michael@0 2634
michael@0 2635 fclose(yuv_file);
michael@0 2636 }
michael@0 2637 #endif
michael@0 2638
michael@0 2639
michael@0 2640 static void scale_and_extend_source(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi)
michael@0 2641 {
michael@0 2642 VP8_COMMON *cm = &cpi->common;
michael@0 2643
michael@0 2644 /* are we resizing the image */
michael@0 2645 if (cm->horiz_scale != 0 || cm->vert_scale != 0)
michael@0 2646 {
michael@0 2647 #if CONFIG_SPATIAL_RESAMPLING
michael@0 2648 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
michael@0 2649 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
michael@0 2650 int tmp_height;
michael@0 2651
michael@0 2652 if (cm->vert_scale == 3)
michael@0 2653 tmp_height = 9;
michael@0 2654 else
michael@0 2655 tmp_height = 11;
michael@0 2656
michael@0 2657 Scale2Ratio(cm->horiz_scale, &hr, &hs);
michael@0 2658 Scale2Ratio(cm->vert_scale, &vr, &vs);
michael@0 2659
michael@0 2660 vpx_scale_frame(sd, &cpi->scaled_source, cm->temp_scale_frame.y_buffer,
michael@0 2661 tmp_height, hs, hr, vs, vr, 0);
michael@0 2662
michael@0 2663 vp8_yv12_extend_frame_borders(&cpi->scaled_source);
michael@0 2664 cpi->Source = &cpi->scaled_source;
michael@0 2665 #endif
michael@0 2666 }
michael@0 2667 else
michael@0 2668 cpi->Source = sd;
michael@0 2669 }
michael@0 2670
michael@0 2671
michael@0 2672 static int resize_key_frame(VP8_COMP *cpi)
michael@0 2673 {
michael@0 2674 #if CONFIG_SPATIAL_RESAMPLING
michael@0 2675 VP8_COMMON *cm = &cpi->common;
michael@0 2676
michael@0 2677 /* Do we need to apply resampling for one pass cbr.
michael@0 2678 * In one pass this is more limited than in two pass cbr
michael@0 2679 * The test and any change is only made one per key frame sequence
michael@0 2680 */
michael@0 2681 if (cpi->oxcf.allow_spatial_resampling && (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER))
michael@0 2682 {
michael@0 2683 int UNINITIALIZED_IS_SAFE(hr), UNINITIALIZED_IS_SAFE(hs);
michael@0 2684 int UNINITIALIZED_IS_SAFE(vr), UNINITIALIZED_IS_SAFE(vs);
michael@0 2685 int new_width, new_height;
michael@0 2686
michael@0 2687 /* If we are below the resample DOWN watermark then scale down a
michael@0 2688 * notch.
michael@0 2689 */
michael@0 2690 if (cpi->buffer_level < (cpi->oxcf.resample_down_water_mark * cpi->oxcf.optimal_buffer_level / 100))
michael@0 2691 {
michael@0 2692 cm->horiz_scale = (cm->horiz_scale < ONETWO) ? cm->horiz_scale + 1 : ONETWO;
michael@0 2693 cm->vert_scale = (cm->vert_scale < ONETWO) ? cm->vert_scale + 1 : ONETWO;
michael@0 2694 }
michael@0 2695 /* Should we now start scaling back up */
michael@0 2696 else if (cpi->buffer_level > (cpi->oxcf.resample_up_water_mark * cpi->oxcf.optimal_buffer_level / 100))
michael@0 2697 {
michael@0 2698 cm->horiz_scale = (cm->horiz_scale > NORMAL) ? cm->horiz_scale - 1 : NORMAL;
michael@0 2699 cm->vert_scale = (cm->vert_scale > NORMAL) ? cm->vert_scale - 1 : NORMAL;
michael@0 2700 }
michael@0 2701
michael@0 2702 /* Get the new hieght and width */
michael@0 2703 Scale2Ratio(cm->horiz_scale, &hr, &hs);
michael@0 2704 Scale2Ratio(cm->vert_scale, &vr, &vs);
michael@0 2705 new_width = ((hs - 1) + (cpi->oxcf.Width * hr)) / hs;
michael@0 2706 new_height = ((vs - 1) + (cpi->oxcf.Height * vr)) / vs;
michael@0 2707
michael@0 2708 /* If the image size has changed we need to reallocate the buffers
michael@0 2709 * and resample the source image
michael@0 2710 */
michael@0 2711 if ((cm->Width != new_width) || (cm->Height != new_height))
michael@0 2712 {
michael@0 2713 cm->Width = new_width;
michael@0 2714 cm->Height = new_height;
michael@0 2715 vp8_alloc_compressor_data(cpi);
michael@0 2716 scale_and_extend_source(cpi->un_scaled_source, cpi);
michael@0 2717 return 1;
michael@0 2718 }
michael@0 2719 }
michael@0 2720
michael@0 2721 #endif
michael@0 2722 return 0;
michael@0 2723 }
michael@0 2724
michael@0 2725
michael@0 2726 static void update_alt_ref_frame_stats(VP8_COMP *cpi)
michael@0 2727 {
michael@0 2728 VP8_COMMON *cm = &cpi->common;
michael@0 2729
michael@0 2730 /* Select an interval before next GF or altref */
michael@0 2731 if (!cpi->auto_gold)
michael@0 2732 cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL;
michael@0 2733
michael@0 2734 if ((cpi->pass != 2) && cpi->frames_till_gf_update_due)
michael@0 2735 {
michael@0 2736 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
michael@0 2737
michael@0 2738 /* Set the bits per frame that we should try and recover in
michael@0 2739 * subsequent inter frames to account for the extra GF spend...
michael@0 2740 * note that his does not apply for GF updates that occur
michael@0 2741 * coincident with a key frame as the extra cost of key frames is
michael@0 2742 * dealt with elsewhere.
michael@0 2743 */
michael@0 2744 cpi->gf_overspend_bits += cpi->projected_frame_size;
michael@0 2745 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
michael@0 2746 }
michael@0 2747
michael@0 2748 /* Update data structure that monitors level of reference to last GF */
michael@0 2749 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
michael@0 2750 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
michael@0 2751
michael@0 2752 /* this frame refreshes means next frames don't unless specified by user */
michael@0 2753 cpi->frames_since_golden = 0;
michael@0 2754
michael@0 2755 /* Clear the alternate reference update pending flag. */
michael@0 2756 cpi->source_alt_ref_pending = 0;
michael@0 2757
michael@0 2758 /* Set the alternate reference frame active flag */
michael@0 2759 cpi->source_alt_ref_active = 1;
michael@0 2760
michael@0 2761
michael@0 2762 }
michael@0 2763 static void update_golden_frame_stats(VP8_COMP *cpi)
michael@0 2764 {
michael@0 2765 VP8_COMMON *cm = &cpi->common;
michael@0 2766
michael@0 2767 /* Update the Golden frame usage counts. */
michael@0 2768 if (cm->refresh_golden_frame)
michael@0 2769 {
michael@0 2770 /* Select an interval before next GF */
michael@0 2771 if (!cpi->auto_gold)
michael@0 2772 cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL;
michael@0 2773
michael@0 2774 if ((cpi->pass != 2) && (cpi->frames_till_gf_update_due > 0))
michael@0 2775 {
michael@0 2776 cpi->current_gf_interval = cpi->frames_till_gf_update_due;
michael@0 2777
michael@0 2778 /* Set the bits per frame that we should try and recover in
michael@0 2779 * subsequent inter frames to account for the extra GF spend...
michael@0 2780 * note that his does not apply for GF updates that occur
michael@0 2781 * coincident with a key frame as the extra cost of key frames
michael@0 2782 * is dealt with elsewhere.
michael@0 2783 */
michael@0 2784 if ((cm->frame_type != KEY_FRAME) && !cpi->source_alt_ref_active)
michael@0 2785 {
michael@0 2786 /* Calcluate GF bits to be recovered
michael@0 2787 * Projected size - av frame bits available for inter
michael@0 2788 * frames for clip as a whole
michael@0 2789 */
michael@0 2790 cpi->gf_overspend_bits += (cpi->projected_frame_size - cpi->inter_frame_target);
michael@0 2791 }
michael@0 2792
michael@0 2793 cpi->non_gf_bitrate_adjustment = cpi->gf_overspend_bits / cpi->frames_till_gf_update_due;
michael@0 2794
michael@0 2795 }
michael@0 2796
michael@0 2797 /* Update data structure that monitors level of reference to last GF */
michael@0 2798 vpx_memset(cpi->gf_active_flags, 1, (cm->mb_rows * cm->mb_cols));
michael@0 2799 cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
michael@0 2800
michael@0 2801 /* this frame refreshes means next frames don't unless specified by
michael@0 2802 * user
michael@0 2803 */
michael@0 2804 cm->refresh_golden_frame = 0;
michael@0 2805 cpi->frames_since_golden = 0;
michael@0 2806
michael@0 2807 cpi->recent_ref_frame_usage[INTRA_FRAME] = 1;
michael@0 2808 cpi->recent_ref_frame_usage[LAST_FRAME] = 1;
michael@0 2809 cpi->recent_ref_frame_usage[GOLDEN_FRAME] = 1;
michael@0 2810 cpi->recent_ref_frame_usage[ALTREF_FRAME] = 1;
michael@0 2811
michael@0 2812 /* ******** Fixed Q test code only ************ */
michael@0 2813 /* If we are going to use the ALT reference for the next group of
michael@0 2814 * frames set a flag to say so.
michael@0 2815 */
michael@0 2816 if (cpi->oxcf.fixed_q >= 0 &&
michael@0 2817 cpi->oxcf.play_alternate && !cpi->common.refresh_alt_ref_frame)
michael@0 2818 {
michael@0 2819 cpi->source_alt_ref_pending = 1;
michael@0 2820 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
michael@0 2821 }
michael@0 2822
michael@0 2823 if (!cpi->source_alt_ref_pending)
michael@0 2824 cpi->source_alt_ref_active = 0;
michael@0 2825
michael@0 2826 /* Decrement count down till next gf */
michael@0 2827 if (cpi->frames_till_gf_update_due > 0)
michael@0 2828 cpi->frames_till_gf_update_due--;
michael@0 2829
michael@0 2830 }
michael@0 2831 else if (!cpi->common.refresh_alt_ref_frame)
michael@0 2832 {
michael@0 2833 /* Decrement count down till next gf */
michael@0 2834 if (cpi->frames_till_gf_update_due > 0)
michael@0 2835 cpi->frames_till_gf_update_due--;
michael@0 2836
michael@0 2837 if (cpi->frames_till_alt_ref_frame)
michael@0 2838 cpi->frames_till_alt_ref_frame --;
michael@0 2839
michael@0 2840 cpi->frames_since_golden ++;
michael@0 2841
michael@0 2842 if (cpi->frames_since_golden > 1)
michael@0 2843 {
michael@0 2844 cpi->recent_ref_frame_usage[INTRA_FRAME] +=
michael@0 2845 cpi->mb.count_mb_ref_frame_usage[INTRA_FRAME];
michael@0 2846 cpi->recent_ref_frame_usage[LAST_FRAME] +=
michael@0 2847 cpi->mb.count_mb_ref_frame_usage[LAST_FRAME];
michael@0 2848 cpi->recent_ref_frame_usage[GOLDEN_FRAME] +=
michael@0 2849 cpi->mb.count_mb_ref_frame_usage[GOLDEN_FRAME];
michael@0 2850 cpi->recent_ref_frame_usage[ALTREF_FRAME] +=
michael@0 2851 cpi->mb.count_mb_ref_frame_usage[ALTREF_FRAME];
michael@0 2852 }
michael@0 2853 }
michael@0 2854 }
michael@0 2855
michael@0 2856 /* This function updates the reference frame probability estimates that
michael@0 2857 * will be used during mode selection
michael@0 2858 */
michael@0 2859 static void update_rd_ref_frame_probs(VP8_COMP *cpi)
michael@0 2860 {
michael@0 2861 VP8_COMMON *cm = &cpi->common;
michael@0 2862
michael@0 2863 const int *const rfct = cpi->mb.count_mb_ref_frame_usage;
michael@0 2864 const int rf_intra = rfct[INTRA_FRAME];
michael@0 2865 const int rf_inter = rfct[LAST_FRAME] + rfct[GOLDEN_FRAME] + rfct[ALTREF_FRAME];
michael@0 2866
michael@0 2867 if (cm->frame_type == KEY_FRAME)
michael@0 2868 {
michael@0 2869 cpi->prob_intra_coded = 255;
michael@0 2870 cpi->prob_last_coded = 128;
michael@0 2871 cpi->prob_gf_coded = 128;
michael@0 2872 }
michael@0 2873 else if (!(rf_intra + rf_inter))
michael@0 2874 {
michael@0 2875 cpi->prob_intra_coded = 63;
michael@0 2876 cpi->prob_last_coded = 128;
michael@0 2877 cpi->prob_gf_coded = 128;
michael@0 2878 }
michael@0 2879
michael@0 2880 /* update reference frame costs since we can do better than what we got
michael@0 2881 * last frame.
michael@0 2882 */
michael@0 2883 if (cpi->oxcf.number_of_layers == 1)
michael@0 2884 {
michael@0 2885 if (cpi->common.refresh_alt_ref_frame)
michael@0 2886 {
michael@0 2887 cpi->prob_intra_coded += 40;
michael@0 2888 if (cpi->prob_intra_coded > 255)
michael@0 2889 cpi->prob_intra_coded = 255;
michael@0 2890 cpi->prob_last_coded = 200;
michael@0 2891 cpi->prob_gf_coded = 1;
michael@0 2892 }
michael@0 2893 else if (cpi->frames_since_golden == 0)
michael@0 2894 {
michael@0 2895 cpi->prob_last_coded = 214;
michael@0 2896 }
michael@0 2897 else if (cpi->frames_since_golden == 1)
michael@0 2898 {
michael@0 2899 cpi->prob_last_coded = 192;
michael@0 2900 cpi->prob_gf_coded = 220;
michael@0 2901 }
michael@0 2902 else if (cpi->source_alt_ref_active)
michael@0 2903 {
michael@0 2904 cpi->prob_gf_coded -= 20;
michael@0 2905
michael@0 2906 if (cpi->prob_gf_coded < 10)
michael@0 2907 cpi->prob_gf_coded = 10;
michael@0 2908 }
michael@0 2909 if (!cpi->source_alt_ref_active)
michael@0 2910 cpi->prob_gf_coded = 255;
michael@0 2911 }
michael@0 2912 }
michael@0 2913
michael@0 2914
michael@0 2915 /* 1 = key, 0 = inter */
michael@0 2916 static int decide_key_frame(VP8_COMP *cpi)
michael@0 2917 {
michael@0 2918 VP8_COMMON *cm = &cpi->common;
michael@0 2919
michael@0 2920 int code_key_frame = 0;
michael@0 2921
michael@0 2922 cpi->kf_boost = 0;
michael@0 2923
michael@0 2924 if (cpi->Speed > 11)
michael@0 2925 return 0;
michael@0 2926
michael@0 2927 /* Clear down mmx registers */
michael@0 2928 vp8_clear_system_state();
michael@0 2929
michael@0 2930 if ((cpi->compressor_speed == 2) && (cpi->Speed >= 5) && (cpi->sf.RD == 0))
michael@0 2931 {
michael@0 2932 double change = 1.0 * abs((int)(cpi->mb.intra_error -
michael@0 2933 cpi->last_intra_error)) / (1 + cpi->last_intra_error);
michael@0 2934 double change2 = 1.0 * abs((int)(cpi->mb.prediction_error -
michael@0 2935 cpi->last_prediction_error)) / (1 + cpi->last_prediction_error);
michael@0 2936 double minerror = cm->MBs * 256;
michael@0 2937
michael@0 2938 cpi->last_intra_error = cpi->mb.intra_error;
michael@0 2939 cpi->last_prediction_error = cpi->mb.prediction_error;
michael@0 2940
michael@0 2941 if (10 * cpi->mb.intra_error / (1 + cpi->mb.prediction_error) < 15
michael@0 2942 && cpi->mb.prediction_error > minerror
michael@0 2943 && (change > .25 || change2 > .25))
michael@0 2944 {
michael@0 2945 /*(change > 1.4 || change < .75)&& cpi->this_frame_percent_intra > cpi->last_frame_percent_intra + 3*/
michael@0 2946 return 1;
michael@0 2947 }
michael@0 2948
michael@0 2949 return 0;
michael@0 2950
michael@0 2951 }
michael@0 2952
michael@0 2953 /* If the following are true we might as well code a key frame */
michael@0 2954 if (((cpi->this_frame_percent_intra == 100) &&
michael@0 2955 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 2))) ||
michael@0 2956 ((cpi->this_frame_percent_intra > 95) &&
michael@0 2957 (cpi->this_frame_percent_intra >= (cpi->last_frame_percent_intra + 5))))
michael@0 2958 {
michael@0 2959 code_key_frame = 1;
michael@0 2960 }
michael@0 2961 /* in addition if the following are true and this is not a golden frame
michael@0 2962 * then code a key frame Note that on golden frames there often seems
michael@0 2963 * to be a pop in intra useage anyway hence this restriction is
michael@0 2964 * designed to prevent spurious key frames. The Intra pop needs to be
michael@0 2965 * investigated.
michael@0 2966 */
michael@0 2967 else if (((cpi->this_frame_percent_intra > 60) &&
michael@0 2968 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 2))) ||
michael@0 2969 ((cpi->this_frame_percent_intra > 75) &&
michael@0 2970 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra * 3 / 2))) ||
michael@0 2971 ((cpi->this_frame_percent_intra > 90) &&
michael@0 2972 (cpi->this_frame_percent_intra > (cpi->last_frame_percent_intra + 10))))
michael@0 2973 {
michael@0 2974 if (!cm->refresh_golden_frame)
michael@0 2975 code_key_frame = 1;
michael@0 2976 }
michael@0 2977
michael@0 2978 return code_key_frame;
michael@0 2979
michael@0 2980 }
michael@0 2981
michael@0 2982 #if !(CONFIG_REALTIME_ONLY)
michael@0 2983 static void Pass1Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags)
michael@0 2984 {
michael@0 2985 (void) size;
michael@0 2986 (void) dest;
michael@0 2987 (void) frame_flags;
michael@0 2988 vp8_set_quantizer(cpi, 26);
michael@0 2989
michael@0 2990 vp8_first_pass(cpi);
michael@0 2991 }
michael@0 2992 #endif
michael@0 2993
michael@0 2994 #if 0
michael@0 2995 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame)
michael@0 2996 {
michael@0 2997
michael@0 2998 /* write the frame */
michael@0 2999 FILE *yframe;
michael@0 3000 int i;
michael@0 3001 char filename[255];
michael@0 3002
michael@0 3003 sprintf(filename, "cx\\y%04d.raw", this_frame);
michael@0 3004 yframe = fopen(filename, "wb");
michael@0 3005
michael@0 3006 for (i = 0; i < frame->y_height; i++)
michael@0 3007 fwrite(frame->y_buffer + i * frame->y_stride, frame->y_width, 1, yframe);
michael@0 3008
michael@0 3009 fclose(yframe);
michael@0 3010 sprintf(filename, "cx\\u%04d.raw", this_frame);
michael@0 3011 yframe = fopen(filename, "wb");
michael@0 3012
michael@0 3013 for (i = 0; i < frame->uv_height; i++)
michael@0 3014 fwrite(frame->u_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
michael@0 3015
michael@0 3016 fclose(yframe);
michael@0 3017 sprintf(filename, "cx\\v%04d.raw", this_frame);
michael@0 3018 yframe = fopen(filename, "wb");
michael@0 3019
michael@0 3020 for (i = 0; i < frame->uv_height; i++)
michael@0 3021 fwrite(frame->v_buffer + i * frame->uv_stride, frame->uv_width, 1, yframe);
michael@0 3022
michael@0 3023 fclose(yframe);
michael@0 3024 }
michael@0 3025 #endif
michael@0 3026 /* return of 0 means drop frame */
michael@0 3027
michael@0 3028 /* Function to test for conditions that indeicate we should loop
michael@0 3029 * back and recode a frame.
michael@0 3030 */
michael@0 3031 static int recode_loop_test( VP8_COMP *cpi,
michael@0 3032 int high_limit, int low_limit,
michael@0 3033 int q, int maxq, int minq )
michael@0 3034 {
michael@0 3035 int force_recode = 0;
michael@0 3036 VP8_COMMON *cm = &cpi->common;
michael@0 3037
michael@0 3038 /* Is frame recode allowed at all
michael@0 3039 * Yes if either recode mode 1 is selected or mode two is selcted
michael@0 3040 * and the frame is a key frame. golden frame or alt_ref_frame
michael@0 3041 */
michael@0 3042 if ( (cpi->sf.recode_loop == 1) ||
michael@0 3043 ( (cpi->sf.recode_loop == 2) &&
michael@0 3044 ( (cm->frame_type == KEY_FRAME) ||
michael@0 3045 cm->refresh_golden_frame ||
michael@0 3046 cm->refresh_alt_ref_frame ) ) )
michael@0 3047 {
michael@0 3048 /* General over and under shoot tests */
michael@0 3049 if ( ((cpi->projected_frame_size > high_limit) && (q < maxq)) ||
michael@0 3050 ((cpi->projected_frame_size < low_limit) && (q > minq)) )
michael@0 3051 {
michael@0 3052 force_recode = 1;
michael@0 3053 }
michael@0 3054 /* Special Constrained quality tests */
michael@0 3055 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
michael@0 3056 {
michael@0 3057 /* Undershoot and below auto cq level */
michael@0 3058 if ( (q > cpi->cq_target_quality) &&
michael@0 3059 (cpi->projected_frame_size <
michael@0 3060 ((cpi->this_frame_target * 7) >> 3)))
michael@0 3061 {
michael@0 3062 force_recode = 1;
michael@0 3063 }
michael@0 3064 /* Severe undershoot and between auto and user cq level */
michael@0 3065 else if ( (q > cpi->oxcf.cq_level) &&
michael@0 3066 (cpi->projected_frame_size < cpi->min_frame_bandwidth) &&
michael@0 3067 (cpi->active_best_quality > cpi->oxcf.cq_level))
michael@0 3068 {
michael@0 3069 force_recode = 1;
michael@0 3070 cpi->active_best_quality = cpi->oxcf.cq_level;
michael@0 3071 }
michael@0 3072 }
michael@0 3073 }
michael@0 3074
michael@0 3075 return force_recode;
michael@0 3076 }
michael@0 3077
michael@0 3078 static void update_reference_frames(VP8_COMP *cpi)
michael@0 3079 {
michael@0 3080 VP8_COMMON *cm = &cpi->common;
michael@0 3081 YV12_BUFFER_CONFIG *yv12_fb = cm->yv12_fb;
michael@0 3082
michael@0 3083 /* At this point the new frame has been encoded.
michael@0 3084 * If any buffer copy / swapping is signaled it should be done here.
michael@0 3085 */
michael@0 3086
michael@0 3087 if (cm->frame_type == KEY_FRAME)
michael@0 3088 {
michael@0 3089 yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FRAME | VP8_ALTR_FRAME ;
michael@0 3090
michael@0 3091 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
michael@0 3092 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
michael@0 3093
michael@0 3094 cm->alt_fb_idx = cm->gld_fb_idx = cm->new_fb_idx;
michael@0 3095
michael@0 3096 #if CONFIG_MULTI_RES_ENCODING
michael@0 3097 cpi->current_ref_frames[GOLDEN_FRAME] = cm->current_video_frame;
michael@0 3098 cpi->current_ref_frames[ALTREF_FRAME] = cm->current_video_frame;
michael@0 3099 #endif
michael@0 3100 }
michael@0 3101 else /* For non key frames */
michael@0 3102 {
michael@0 3103 if (cm->refresh_alt_ref_frame)
michael@0 3104 {
michael@0 3105 assert(!cm->copy_buffer_to_arf);
michael@0 3106
michael@0 3107 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_ALTR_FRAME;
michael@0 3108 cm->yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
michael@0 3109 cm->alt_fb_idx = cm->new_fb_idx;
michael@0 3110
michael@0 3111 #if CONFIG_MULTI_RES_ENCODING
michael@0 3112 cpi->current_ref_frames[ALTREF_FRAME] = cm->current_video_frame;
michael@0 3113 #endif
michael@0 3114 }
michael@0 3115 else if (cm->copy_buffer_to_arf)
michael@0 3116 {
michael@0 3117 assert(!(cm->copy_buffer_to_arf & ~0x3));
michael@0 3118
michael@0 3119 if (cm->copy_buffer_to_arf == 1)
michael@0 3120 {
michael@0 3121 if(cm->alt_fb_idx != cm->lst_fb_idx)
michael@0 3122 {
michael@0 3123 yv12_fb[cm->lst_fb_idx].flags |= VP8_ALTR_FRAME;
michael@0 3124 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
michael@0 3125 cm->alt_fb_idx = cm->lst_fb_idx;
michael@0 3126
michael@0 3127 #if CONFIG_MULTI_RES_ENCODING
michael@0 3128 cpi->current_ref_frames[ALTREF_FRAME] =
michael@0 3129 cpi->current_ref_frames[LAST_FRAME];
michael@0 3130 #endif
michael@0 3131 }
michael@0 3132 }
michael@0 3133 else /* if (cm->copy_buffer_to_arf == 2) */
michael@0 3134 {
michael@0 3135 if(cm->alt_fb_idx != cm->gld_fb_idx)
michael@0 3136 {
michael@0 3137 yv12_fb[cm->gld_fb_idx].flags |= VP8_ALTR_FRAME;
michael@0 3138 yv12_fb[cm->alt_fb_idx].flags &= ~VP8_ALTR_FRAME;
michael@0 3139 cm->alt_fb_idx = cm->gld_fb_idx;
michael@0 3140
michael@0 3141 #if CONFIG_MULTI_RES_ENCODING
michael@0 3142 cpi->current_ref_frames[ALTREF_FRAME] =
michael@0 3143 cpi->current_ref_frames[GOLDEN_FRAME];
michael@0 3144 #endif
michael@0 3145 }
michael@0 3146 }
michael@0 3147 }
michael@0 3148
michael@0 3149 if (cm->refresh_golden_frame)
michael@0 3150 {
michael@0 3151 assert(!cm->copy_buffer_to_gf);
michael@0 3152
michael@0 3153 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_GOLD_FRAME;
michael@0 3154 cm->yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
michael@0 3155 cm->gld_fb_idx = cm->new_fb_idx;
michael@0 3156
michael@0 3157 #if CONFIG_MULTI_RES_ENCODING
michael@0 3158 cpi->current_ref_frames[GOLDEN_FRAME] = cm->current_video_frame;
michael@0 3159 #endif
michael@0 3160 }
michael@0 3161 else if (cm->copy_buffer_to_gf)
michael@0 3162 {
michael@0 3163 assert(!(cm->copy_buffer_to_arf & ~0x3));
michael@0 3164
michael@0 3165 if (cm->copy_buffer_to_gf == 1)
michael@0 3166 {
michael@0 3167 if(cm->gld_fb_idx != cm->lst_fb_idx)
michael@0 3168 {
michael@0 3169 yv12_fb[cm->lst_fb_idx].flags |= VP8_GOLD_FRAME;
michael@0 3170 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
michael@0 3171 cm->gld_fb_idx = cm->lst_fb_idx;
michael@0 3172
michael@0 3173 #if CONFIG_MULTI_RES_ENCODING
michael@0 3174 cpi->current_ref_frames[GOLDEN_FRAME] =
michael@0 3175 cpi->current_ref_frames[LAST_FRAME];
michael@0 3176 #endif
michael@0 3177 }
michael@0 3178 }
michael@0 3179 else /* if (cm->copy_buffer_to_gf == 2) */
michael@0 3180 {
michael@0 3181 if(cm->alt_fb_idx != cm->gld_fb_idx)
michael@0 3182 {
michael@0 3183 yv12_fb[cm->alt_fb_idx].flags |= VP8_GOLD_FRAME;
michael@0 3184 yv12_fb[cm->gld_fb_idx].flags &= ~VP8_GOLD_FRAME;
michael@0 3185 cm->gld_fb_idx = cm->alt_fb_idx;
michael@0 3186
michael@0 3187 #if CONFIG_MULTI_RES_ENCODING
michael@0 3188 cpi->current_ref_frames[GOLDEN_FRAME] =
michael@0 3189 cpi->current_ref_frames[ALTREF_FRAME];
michael@0 3190 #endif
michael@0 3191 }
michael@0 3192 }
michael@0 3193 }
michael@0 3194 }
michael@0 3195
michael@0 3196 if (cm->refresh_last_frame)
michael@0 3197 {
michael@0 3198 cm->yv12_fb[cm->new_fb_idx].flags |= VP8_LAST_FRAME;
michael@0 3199 cm->yv12_fb[cm->lst_fb_idx].flags &= ~VP8_LAST_FRAME;
michael@0 3200 cm->lst_fb_idx = cm->new_fb_idx;
michael@0 3201
michael@0 3202 #if CONFIG_MULTI_RES_ENCODING
michael@0 3203 cpi->current_ref_frames[LAST_FRAME] = cm->current_video_frame;
michael@0 3204 #endif
michael@0 3205 }
michael@0 3206
michael@0 3207 #if CONFIG_TEMPORAL_DENOISING
michael@0 3208 if (cpi->oxcf.noise_sensitivity)
michael@0 3209 {
michael@0 3210 /* we shouldn't have to keep multiple copies as we know in advance which
michael@0 3211 * buffer we should start - for now to get something up and running
michael@0 3212 * I've chosen to copy the buffers
michael@0 3213 */
michael@0 3214 if (cm->frame_type == KEY_FRAME)
michael@0 3215 {
michael@0 3216 int i;
michael@0 3217 vp8_yv12_copy_frame(
michael@0 3218 cpi->Source,
michael@0 3219 &cpi->denoiser.yv12_running_avg[LAST_FRAME]);
michael@0 3220
michael@0 3221 vp8_yv12_extend_frame_borders(
michael@0 3222 &cpi->denoiser.yv12_running_avg[LAST_FRAME]);
michael@0 3223
michael@0 3224 for (i = 2; i < MAX_REF_FRAMES - 1; i++)
michael@0 3225 vp8_yv12_copy_frame(
michael@0 3226 &cpi->denoiser.yv12_running_avg[LAST_FRAME],
michael@0 3227 &cpi->denoiser.yv12_running_avg[i]);
michael@0 3228 }
michael@0 3229 else /* For non key frames */
michael@0 3230 {
michael@0 3231 vp8_yv12_extend_frame_borders(
michael@0 3232 &cpi->denoiser.yv12_running_avg[INTRA_FRAME]);
michael@0 3233
michael@0 3234 if (cm->refresh_alt_ref_frame || cm->copy_buffer_to_arf)
michael@0 3235 {
michael@0 3236 vp8_yv12_copy_frame(
michael@0 3237 &cpi->denoiser.yv12_running_avg[INTRA_FRAME],
michael@0 3238 &cpi->denoiser.yv12_running_avg[ALTREF_FRAME]);
michael@0 3239 }
michael@0 3240 if (cm->refresh_golden_frame || cm->copy_buffer_to_gf)
michael@0 3241 {
michael@0 3242 vp8_yv12_copy_frame(
michael@0 3243 &cpi->denoiser.yv12_running_avg[INTRA_FRAME],
michael@0 3244 &cpi->denoiser.yv12_running_avg[GOLDEN_FRAME]);
michael@0 3245 }
michael@0 3246 if(cm->refresh_last_frame)
michael@0 3247 {
michael@0 3248 vp8_yv12_copy_frame(
michael@0 3249 &cpi->denoiser.yv12_running_avg[INTRA_FRAME],
michael@0 3250 &cpi->denoiser.yv12_running_avg[LAST_FRAME]);
michael@0 3251 }
michael@0 3252 }
michael@0 3253
michael@0 3254 }
michael@0 3255 #endif
michael@0 3256
michael@0 3257 }
michael@0 3258
michael@0 3259 void vp8_loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm)
michael@0 3260 {
michael@0 3261 const FRAME_TYPE frame_type = cm->frame_type;
michael@0 3262
michael@0 3263 if (cm->no_lpf)
michael@0 3264 {
michael@0 3265 cm->filter_level = 0;
michael@0 3266 }
michael@0 3267 else
michael@0 3268 {
michael@0 3269 struct vpx_usec_timer timer;
michael@0 3270
michael@0 3271 vp8_clear_system_state();
michael@0 3272
michael@0 3273 vpx_usec_timer_start(&timer);
michael@0 3274 if (cpi->sf.auto_filter == 0)
michael@0 3275 vp8cx_pick_filter_level_fast(cpi->Source, cpi);
michael@0 3276
michael@0 3277 else
michael@0 3278 vp8cx_pick_filter_level(cpi->Source, cpi);
michael@0 3279
michael@0 3280 if (cm->filter_level > 0)
michael@0 3281 {
michael@0 3282 vp8cx_set_alt_lf_level(cpi, cm->filter_level);
michael@0 3283 }
michael@0 3284
michael@0 3285 vpx_usec_timer_mark(&timer);
michael@0 3286 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
michael@0 3287 }
michael@0 3288
michael@0 3289 #if CONFIG_MULTITHREAD
michael@0 3290 if (cpi->b_multi_threaded)
michael@0 3291 sem_post(&cpi->h_event_end_lpf); /* signal that we have set filter_level */
michael@0 3292 #endif
michael@0 3293
michael@0 3294 if (cm->filter_level > 0)
michael@0 3295 {
michael@0 3296 vp8_loop_filter_frame(cm, &cpi->mb.e_mbd, frame_type);
michael@0 3297 }
michael@0 3298
michael@0 3299 vp8_yv12_extend_frame_borders(cm->frame_to_show);
michael@0 3300
michael@0 3301 }
michael@0 3302
michael@0 3303 static void encode_frame_to_data_rate
michael@0 3304 (
michael@0 3305 VP8_COMP *cpi,
michael@0 3306 unsigned long *size,
michael@0 3307 unsigned char *dest,
michael@0 3308 unsigned char* dest_end,
michael@0 3309 unsigned int *frame_flags
michael@0 3310 )
michael@0 3311 {
michael@0 3312 int Q;
michael@0 3313 int frame_over_shoot_limit;
michael@0 3314 int frame_under_shoot_limit;
michael@0 3315
michael@0 3316 int Loop = 0;
michael@0 3317 int loop_count;
michael@0 3318
michael@0 3319 VP8_COMMON *cm = &cpi->common;
michael@0 3320 int active_worst_qchanged = 0;
michael@0 3321
michael@0 3322 #if !(CONFIG_REALTIME_ONLY)
michael@0 3323 int q_low;
michael@0 3324 int q_high;
michael@0 3325 int zbin_oq_high;
michael@0 3326 int zbin_oq_low = 0;
michael@0 3327 int top_index;
michael@0 3328 int bottom_index;
michael@0 3329 int overshoot_seen = 0;
michael@0 3330 int undershoot_seen = 0;
michael@0 3331 #endif
michael@0 3332
michael@0 3333 int drop_mark = (int)(cpi->oxcf.drop_frames_water_mark *
michael@0 3334 cpi->oxcf.optimal_buffer_level / 100);
michael@0 3335 int drop_mark75 = drop_mark * 2 / 3;
michael@0 3336 int drop_mark50 = drop_mark / 4;
michael@0 3337 int drop_mark25 = drop_mark / 8;
michael@0 3338
michael@0 3339
michael@0 3340 /* Clear down mmx registers to allow floating point in what follows */
michael@0 3341 vp8_clear_system_state();
michael@0 3342
michael@0 3343 #if CONFIG_MULTITHREAD
michael@0 3344 /* wait for the last picture loopfilter thread done */
michael@0 3345 if (cpi->b_lpf_running)
michael@0 3346 {
michael@0 3347 sem_wait(&cpi->h_event_end_lpf);
michael@0 3348 cpi->b_lpf_running = 0;
michael@0 3349 }
michael@0 3350 #endif
michael@0 3351
michael@0 3352 if(cpi->force_next_frame_intra)
michael@0 3353 {
michael@0 3354 cm->frame_type = KEY_FRAME; /* delayed intra frame */
michael@0 3355 cpi->force_next_frame_intra = 0;
michael@0 3356 }
michael@0 3357
michael@0 3358 /* For an alt ref frame in 2 pass we skip the call to the second pass
michael@0 3359 * function that sets the target bandwidth
michael@0 3360 */
michael@0 3361 #if !(CONFIG_REALTIME_ONLY)
michael@0 3362
michael@0 3363 if (cpi->pass == 2)
michael@0 3364 {
michael@0 3365 if (cpi->common.refresh_alt_ref_frame)
michael@0 3366 {
michael@0 3367 /* Per frame bit target for the alt ref frame */
michael@0 3368 cpi->per_frame_bandwidth = cpi->twopass.gf_bits;
michael@0 3369 /* per second target bitrate */
michael@0 3370 cpi->target_bandwidth = (int)(cpi->twopass.gf_bits *
michael@0 3371 cpi->output_framerate);
michael@0 3372 }
michael@0 3373 }
michael@0 3374 else
michael@0 3375 #endif
michael@0 3376 cpi->per_frame_bandwidth = (int)(cpi->target_bandwidth / cpi->output_framerate);
michael@0 3377
michael@0 3378 /* Default turn off buffer to buffer copying */
michael@0 3379 cm->copy_buffer_to_gf = 0;
michael@0 3380 cm->copy_buffer_to_arf = 0;
michael@0 3381
michael@0 3382 /* Clear zbin over-quant value and mode boost values. */
michael@0 3383 cpi->mb.zbin_over_quant = 0;
michael@0 3384 cpi->mb.zbin_mode_boost = 0;
michael@0 3385
michael@0 3386 /* Enable or disable mode based tweaking of the zbin
michael@0 3387 * For 2 Pass Only used where GF/ARF prediction quality
michael@0 3388 * is above a threshold
michael@0 3389 */
michael@0 3390 cpi->mb.zbin_mode_boost_enabled = 1;
michael@0 3391 if (cpi->pass == 2)
michael@0 3392 {
michael@0 3393 if ( cpi->gfu_boost <= 400 )
michael@0 3394 {
michael@0 3395 cpi->mb.zbin_mode_boost_enabled = 0;
michael@0 3396 }
michael@0 3397 }
michael@0 3398
michael@0 3399 /* Current default encoder behaviour for the altref sign bias */
michael@0 3400 if (cpi->source_alt_ref_active)
michael@0 3401 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 1;
michael@0 3402 else
michael@0 3403 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = 0;
michael@0 3404
michael@0 3405 /* Check to see if a key frame is signaled
michael@0 3406 * For two pass with auto key frame enabled cm->frame_type may already
michael@0 3407 * be set, but not for one pass.
michael@0 3408 */
michael@0 3409 if ((cm->current_video_frame == 0) ||
michael@0 3410 (cm->frame_flags & FRAMEFLAGS_KEY) ||
michael@0 3411 (cpi->oxcf.auto_key && (cpi->frames_since_key % cpi->key_frame_frequency == 0)))
michael@0 3412 {
michael@0 3413 /* Key frame from VFW/auto-keyframe/first frame */
michael@0 3414 cm->frame_type = KEY_FRAME;
michael@0 3415 }
michael@0 3416
michael@0 3417 #if CONFIG_MULTI_RES_ENCODING
michael@0 3418 /* In multi-resolution encoding, frame_type is decided by lowest-resolution
michael@0 3419 * encoder. Same frame_type is adopted while encoding at other resolution.
michael@0 3420 */
michael@0 3421 if (cpi->oxcf.mr_encoder_id)
michael@0 3422 {
michael@0 3423 LOWER_RES_FRAME_INFO* low_res_frame_info
michael@0 3424 = (LOWER_RES_FRAME_INFO*)cpi->oxcf.mr_low_res_mode_info;
michael@0 3425
michael@0 3426 cm->frame_type = low_res_frame_info->frame_type;
michael@0 3427
michael@0 3428 if(cm->frame_type != KEY_FRAME)
michael@0 3429 {
michael@0 3430 cpi->mr_low_res_mv_avail = 1;
michael@0 3431 cpi->mr_low_res_mv_avail &= !(low_res_frame_info->is_frame_dropped);
michael@0 3432
michael@0 3433 if (cpi->ref_frame_flags & VP8_LAST_FRAME)
michael@0 3434 cpi->mr_low_res_mv_avail &= (cpi->current_ref_frames[LAST_FRAME]
michael@0 3435 == low_res_frame_info->low_res_ref_frames[LAST_FRAME]);
michael@0 3436
michael@0 3437 if (cpi->ref_frame_flags & VP8_GOLD_FRAME)
michael@0 3438 cpi->mr_low_res_mv_avail &= (cpi->current_ref_frames[GOLDEN_FRAME]
michael@0 3439 == low_res_frame_info->low_res_ref_frames[GOLDEN_FRAME]);
michael@0 3440
michael@0 3441 if (cpi->ref_frame_flags & VP8_ALTR_FRAME)
michael@0 3442 cpi->mr_low_res_mv_avail &= (cpi->current_ref_frames[ALTREF_FRAME]
michael@0 3443 == low_res_frame_info->low_res_ref_frames[ALTREF_FRAME]);
michael@0 3444 }
michael@0 3445 }
michael@0 3446 #endif
michael@0 3447
michael@0 3448 /* Set various flags etc to special state if it is a key frame */
michael@0 3449 if (cm->frame_type == KEY_FRAME)
michael@0 3450 {
michael@0 3451 int i;
michael@0 3452
michael@0 3453 // Set the loop filter deltas and segmentation map update
michael@0 3454 setup_features(cpi);
michael@0 3455
michael@0 3456 /* The alternate reference frame cannot be active for a key frame */
michael@0 3457 cpi->source_alt_ref_active = 0;
michael@0 3458
michael@0 3459 /* Reset the RD threshold multipliers to default of * 1 (128) */
michael@0 3460 for (i = 0; i < MAX_MODES; i++)
michael@0 3461 {
michael@0 3462 cpi->mb.rd_thresh_mult[i] = 128;
michael@0 3463 }
michael@0 3464 }
michael@0 3465
michael@0 3466 #if 0
michael@0 3467 /* Experimental code for lagged compress and one pass
michael@0 3468 * Initialise one_pass GF frames stats
michael@0 3469 * Update stats used for GF selection
michael@0 3470 */
michael@0 3471 {
michael@0 3472 cpi->one_pass_frame_index = cm->current_video_frame % MAX_LAG_BUFFERS;
michael@0 3473
michael@0 3474 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frames_so_far = 0;
michael@0 3475 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_intra_error = 0.0;
michael@0 3476 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_coded_error = 0.0;
michael@0 3477 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_inter = 0.0;
michael@0 3478 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_pcnt_motion = 0.0;
michael@0 3479 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr = 0.0;
michael@0 3480 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvr_abs = 0.0;
michael@0 3481 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc = 0.0;
michael@0 3482 cpi->one_pass_frame_stats[cpi->one_pass_frame_index ].frame_mvc_abs = 0.0;
michael@0 3483 }
michael@0 3484 #endif
michael@0 3485
michael@0 3486 update_rd_ref_frame_probs(cpi);
michael@0 3487
michael@0 3488 if (cpi->drop_frames_allowed)
michael@0 3489 {
michael@0 3490 /* The reset to decimation 0 is only done here for one pass.
michael@0 3491 * Once it is set two pass leaves decimation on till the next kf.
michael@0 3492 */
michael@0 3493 if ((cpi->buffer_level > drop_mark) && (cpi->decimation_factor > 0))
michael@0 3494 cpi->decimation_factor --;
michael@0 3495
michael@0 3496 if (cpi->buffer_level > drop_mark75 && cpi->decimation_factor > 0)
michael@0 3497 cpi->decimation_factor = 1;
michael@0 3498
michael@0 3499 else if (cpi->buffer_level < drop_mark25 && (cpi->decimation_factor == 2 || cpi->decimation_factor == 3))
michael@0 3500 {
michael@0 3501 cpi->decimation_factor = 3;
michael@0 3502 }
michael@0 3503 else if (cpi->buffer_level < drop_mark50 && (cpi->decimation_factor == 1 || cpi->decimation_factor == 2))
michael@0 3504 {
michael@0 3505 cpi->decimation_factor = 2;
michael@0 3506 }
michael@0 3507 else if (cpi->buffer_level < drop_mark75 && (cpi->decimation_factor == 0 || cpi->decimation_factor == 1))
michael@0 3508 {
michael@0 3509 cpi->decimation_factor = 1;
michael@0 3510 }
michael@0 3511 }
michael@0 3512
michael@0 3513 /* The following decimates the frame rate according to a regular
michael@0 3514 * pattern (i.e. to 1/2 or 2/3 frame rate) This can be used to help
michael@0 3515 * prevent buffer under-run in CBR mode. Alternatively it might be
michael@0 3516 * desirable in some situations to drop frame rate but throw more bits
michael@0 3517 * at each frame.
michael@0 3518 *
michael@0 3519 * Note that dropping a key frame can be problematic if spatial
michael@0 3520 * resampling is also active
michael@0 3521 */
michael@0 3522 if (cpi->decimation_factor > 0)
michael@0 3523 {
michael@0 3524 switch (cpi->decimation_factor)
michael@0 3525 {
michael@0 3526 case 1:
michael@0 3527 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 3 / 2;
michael@0 3528 break;
michael@0 3529 case 2:
michael@0 3530 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
michael@0 3531 break;
michael@0 3532 case 3:
michael@0 3533 cpi->per_frame_bandwidth = cpi->per_frame_bandwidth * 5 / 4;
michael@0 3534 break;
michael@0 3535 }
michael@0 3536
michael@0 3537 /* Note that we should not throw out a key frame (especially when
michael@0 3538 * spatial resampling is enabled).
michael@0 3539 */
michael@0 3540 if (cm->frame_type == KEY_FRAME)
michael@0 3541 {
michael@0 3542 cpi->decimation_count = cpi->decimation_factor;
michael@0 3543 }
michael@0 3544 else if (cpi->decimation_count > 0)
michael@0 3545 {
michael@0 3546 cpi->decimation_count --;
michael@0 3547
michael@0 3548 cpi->bits_off_target += cpi->av_per_frame_bandwidth;
michael@0 3549 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size)
michael@0 3550 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
michael@0 3551
michael@0 3552 #if CONFIG_MULTI_RES_ENCODING
michael@0 3553 vp8_store_drop_frame_info(cpi);
michael@0 3554 #endif
michael@0 3555
michael@0 3556 cm->current_video_frame++;
michael@0 3557 cpi->frames_since_key++;
michael@0 3558 // We advance the temporal pattern for dropped frames.
michael@0 3559 cpi->temporal_pattern_counter++;
michael@0 3560
michael@0 3561 #if CONFIG_INTERNAL_STATS
michael@0 3562 cpi->count ++;
michael@0 3563 #endif
michael@0 3564
michael@0 3565 cpi->buffer_level = cpi->bits_off_target;
michael@0 3566
michael@0 3567 if (cpi->oxcf.number_of_layers > 1)
michael@0 3568 {
michael@0 3569 unsigned int i;
michael@0 3570
michael@0 3571 /* Propagate bits saved by dropping the frame to higher
michael@0 3572 * layers
michael@0 3573 */
michael@0 3574 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
michael@0 3575 {
michael@0 3576 LAYER_CONTEXT *lc = &cpi->layer_context[i];
michael@0 3577 lc->bits_off_target += (int)(lc->target_bandwidth /
michael@0 3578 lc->framerate);
michael@0 3579 if (lc->bits_off_target > lc->maximum_buffer_size)
michael@0 3580 lc->bits_off_target = lc->maximum_buffer_size;
michael@0 3581 lc->buffer_level = lc->bits_off_target;
michael@0 3582 }
michael@0 3583 }
michael@0 3584
michael@0 3585 return;
michael@0 3586 }
michael@0 3587 else
michael@0 3588 cpi->decimation_count = cpi->decimation_factor;
michael@0 3589 }
michael@0 3590 else
michael@0 3591 cpi->decimation_count = 0;
michael@0 3592
michael@0 3593 /* Decide how big to make the frame */
michael@0 3594 if (!vp8_pick_frame_size(cpi))
michael@0 3595 {
michael@0 3596 /*TODO: 2 drop_frame and return code could be put together. */
michael@0 3597 #if CONFIG_MULTI_RES_ENCODING
michael@0 3598 vp8_store_drop_frame_info(cpi);
michael@0 3599 #endif
michael@0 3600 cm->current_video_frame++;
michael@0 3601 cpi->frames_since_key++;
michael@0 3602 // We advance the temporal pattern for dropped frames.
michael@0 3603 cpi->temporal_pattern_counter++;
michael@0 3604 return;
michael@0 3605 }
michael@0 3606
michael@0 3607 /* Reduce active_worst_allowed_q for CBR if our buffer is getting too full.
michael@0 3608 * This has a knock on effect on active best quality as well.
michael@0 3609 * For CBR if the buffer reaches its maximum level then we can no longer
michael@0 3610 * save up bits for later frames so we might as well use them up
michael@0 3611 * on the current frame.
michael@0 3612 */
michael@0 3613 if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
michael@0 3614 (cpi->buffer_level >= cpi->oxcf.optimal_buffer_level) && cpi->buffered_mode)
michael@0 3615 {
michael@0 3616 /* Max adjustment is 1/4 */
michael@0 3617 int Adjustment = cpi->active_worst_quality / 4;
michael@0 3618
michael@0 3619 if (Adjustment)
michael@0 3620 {
michael@0 3621 int buff_lvl_step;
michael@0 3622
michael@0 3623 if (cpi->buffer_level < cpi->oxcf.maximum_buffer_size)
michael@0 3624 {
michael@0 3625 buff_lvl_step = (int)
michael@0 3626 ((cpi->oxcf.maximum_buffer_size -
michael@0 3627 cpi->oxcf.optimal_buffer_level) /
michael@0 3628 Adjustment);
michael@0 3629
michael@0 3630 if (buff_lvl_step)
michael@0 3631 Adjustment = (int)
michael@0 3632 ((cpi->buffer_level -
michael@0 3633 cpi->oxcf.optimal_buffer_level) /
michael@0 3634 buff_lvl_step);
michael@0 3635 else
michael@0 3636 Adjustment = 0;
michael@0 3637 }
michael@0 3638
michael@0 3639 cpi->active_worst_quality -= Adjustment;
michael@0 3640
michael@0 3641 if(cpi->active_worst_quality < cpi->active_best_quality)
michael@0 3642 cpi->active_worst_quality = cpi->active_best_quality;
michael@0 3643 }
michael@0 3644 }
michael@0 3645
michael@0 3646 /* Set an active best quality and if necessary active worst quality
michael@0 3647 * There is some odd behavior for one pass here that needs attention.
michael@0 3648 */
michael@0 3649 if ( (cpi->pass == 2) || (cpi->ni_frames > 150))
michael@0 3650 {
michael@0 3651 vp8_clear_system_state();
michael@0 3652
michael@0 3653 Q = cpi->active_worst_quality;
michael@0 3654
michael@0 3655 if ( cm->frame_type == KEY_FRAME )
michael@0 3656 {
michael@0 3657 if ( cpi->pass == 2 )
michael@0 3658 {
michael@0 3659 if (cpi->gfu_boost > 600)
michael@0 3660 cpi->active_best_quality = kf_low_motion_minq[Q];
michael@0 3661 else
michael@0 3662 cpi->active_best_quality = kf_high_motion_minq[Q];
michael@0 3663
michael@0 3664 /* Special case for key frames forced because we have reached
michael@0 3665 * the maximum key frame interval. Here force the Q to a range
michael@0 3666 * based on the ambient Q to reduce the risk of popping
michael@0 3667 */
michael@0 3668 if ( cpi->this_key_frame_forced )
michael@0 3669 {
michael@0 3670 if ( cpi->active_best_quality > cpi->avg_frame_qindex * 7/8)
michael@0 3671 cpi->active_best_quality = cpi->avg_frame_qindex * 7/8;
michael@0 3672 else if ( cpi->active_best_quality < cpi->avg_frame_qindex >> 2 )
michael@0 3673 cpi->active_best_quality = cpi->avg_frame_qindex >> 2;
michael@0 3674 }
michael@0 3675 }
michael@0 3676 /* One pass more conservative */
michael@0 3677 else
michael@0 3678 cpi->active_best_quality = kf_high_motion_minq[Q];
michael@0 3679 }
michael@0 3680
michael@0 3681 else if (cpi->oxcf.number_of_layers==1 &&
michael@0 3682 (cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame))
michael@0 3683 {
michael@0 3684 /* Use the lower of cpi->active_worst_quality and recent
michael@0 3685 * average Q as basis for GF/ARF Q limit unless last frame was
michael@0 3686 * a key frame.
michael@0 3687 */
michael@0 3688 if ( (cpi->frames_since_key > 1) &&
michael@0 3689 (cpi->avg_frame_qindex < cpi->active_worst_quality) )
michael@0 3690 {
michael@0 3691 Q = cpi->avg_frame_qindex;
michael@0 3692 }
michael@0 3693
michael@0 3694 /* For constrained quality dont allow Q less than the cq level */
michael@0 3695 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
michael@0 3696 (Q < cpi->cq_target_quality) )
michael@0 3697 {
michael@0 3698 Q = cpi->cq_target_quality;
michael@0 3699 }
michael@0 3700
michael@0 3701 if ( cpi->pass == 2 )
michael@0 3702 {
michael@0 3703 if ( cpi->gfu_boost > 1000 )
michael@0 3704 cpi->active_best_quality = gf_low_motion_minq[Q];
michael@0 3705 else if ( cpi->gfu_boost < 400 )
michael@0 3706 cpi->active_best_quality = gf_high_motion_minq[Q];
michael@0 3707 else
michael@0 3708 cpi->active_best_quality = gf_mid_motion_minq[Q];
michael@0 3709
michael@0 3710 /* Constrained quality use slightly lower active best. */
michael@0 3711 if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY )
michael@0 3712 {
michael@0 3713 cpi->active_best_quality =
michael@0 3714 cpi->active_best_quality * 15/16;
michael@0 3715 }
michael@0 3716 }
michael@0 3717 /* One pass more conservative */
michael@0 3718 else
michael@0 3719 cpi->active_best_quality = gf_high_motion_minq[Q];
michael@0 3720 }
michael@0 3721 else
michael@0 3722 {
michael@0 3723 cpi->active_best_quality = inter_minq[Q];
michael@0 3724
michael@0 3725 /* For the constant/constrained quality mode we dont want
michael@0 3726 * q to fall below the cq level.
michael@0 3727 */
michael@0 3728 if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
michael@0 3729 (cpi->active_best_quality < cpi->cq_target_quality) )
michael@0 3730 {
michael@0 3731 /* If we are strongly undershooting the target rate in the last
michael@0 3732 * frames then use the user passed in cq value not the auto
michael@0 3733 * cq value.
michael@0 3734 */
michael@0 3735 if ( cpi->rolling_actual_bits < cpi->min_frame_bandwidth )
michael@0 3736 cpi->active_best_quality = cpi->oxcf.cq_level;
michael@0 3737 else
michael@0 3738 cpi->active_best_quality = cpi->cq_target_quality;
michael@0 3739 }
michael@0 3740 }
michael@0 3741
michael@0 3742 /* If CBR and the buffer is as full then it is reasonable to allow
michael@0 3743 * higher quality on the frames to prevent bits just going to waste.
michael@0 3744 */
michael@0 3745 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
michael@0 3746 {
michael@0 3747 /* Note that the use of >= here elliminates the risk of a devide
michael@0 3748 * by 0 error in the else if clause
michael@0 3749 */
michael@0 3750 if (cpi->buffer_level >= cpi->oxcf.maximum_buffer_size)
michael@0 3751 cpi->active_best_quality = cpi->best_quality;
michael@0 3752
michael@0 3753 else if (cpi->buffer_level > cpi->oxcf.optimal_buffer_level)
michael@0 3754 {
michael@0 3755 int Fraction = (int)
michael@0 3756 (((cpi->buffer_level - cpi->oxcf.optimal_buffer_level) * 128)
michael@0 3757 / (cpi->oxcf.maximum_buffer_size -
michael@0 3758 cpi->oxcf.optimal_buffer_level));
michael@0 3759 int min_qadjustment = ((cpi->active_best_quality -
michael@0 3760 cpi->best_quality) * Fraction) / 128;
michael@0 3761
michael@0 3762 cpi->active_best_quality -= min_qadjustment;
michael@0 3763 }
michael@0 3764 }
michael@0 3765 }
michael@0 3766 /* Make sure constrained quality mode limits are adhered to for the first
michael@0 3767 * few frames of one pass encodes
michael@0 3768 */
michael@0 3769 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
michael@0 3770 {
michael@0 3771 if ( (cm->frame_type == KEY_FRAME) ||
michael@0 3772 cm->refresh_golden_frame || cpi->common.refresh_alt_ref_frame )
michael@0 3773 {
michael@0 3774 cpi->active_best_quality = cpi->best_quality;
michael@0 3775 }
michael@0 3776 else if (cpi->active_best_quality < cpi->cq_target_quality)
michael@0 3777 {
michael@0 3778 cpi->active_best_quality = cpi->cq_target_quality;
michael@0 3779 }
michael@0 3780 }
michael@0 3781
michael@0 3782 /* Clip the active best and worst quality values to limits */
michael@0 3783 if (cpi->active_worst_quality > cpi->worst_quality)
michael@0 3784 cpi->active_worst_quality = cpi->worst_quality;
michael@0 3785
michael@0 3786 if (cpi->active_best_quality < cpi->best_quality)
michael@0 3787 cpi->active_best_quality = cpi->best_quality;
michael@0 3788
michael@0 3789 if ( cpi->active_worst_quality < cpi->active_best_quality )
michael@0 3790 cpi->active_worst_quality = cpi->active_best_quality;
michael@0 3791
michael@0 3792 /* Determine initial Q to try */
michael@0 3793 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
michael@0 3794
michael@0 3795 #if !(CONFIG_REALTIME_ONLY)
michael@0 3796
michael@0 3797 /* Set highest allowed value for Zbin over quant */
michael@0 3798 if (cm->frame_type == KEY_FRAME)
michael@0 3799 zbin_oq_high = 0;
michael@0 3800 else if ((cpi->oxcf.number_of_layers == 1) && ((cm->refresh_alt_ref_frame ||
michael@0 3801 (cm->refresh_golden_frame && !cpi->source_alt_ref_active))))
michael@0 3802 {
michael@0 3803 zbin_oq_high = 16;
michael@0 3804 }
michael@0 3805 else
michael@0 3806 zbin_oq_high = ZBIN_OQ_MAX;
michael@0 3807 #endif
michael@0 3808
michael@0 3809 /* Setup background Q adjustment for error resilient mode.
michael@0 3810 * For multi-layer encodes only enable this for the base layer.
michael@0 3811 */
michael@0 3812 if (cpi->cyclic_refresh_mode_enabled)
michael@0 3813 {
michael@0 3814 if (cpi->current_layer==0)
michael@0 3815 cyclic_background_refresh(cpi, Q, 0);
michael@0 3816 else
michael@0 3817 disable_segmentation(cpi);
michael@0 3818 }
michael@0 3819
michael@0 3820 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
michael@0 3821
michael@0 3822 #if !(CONFIG_REALTIME_ONLY)
michael@0 3823 /* Limit Q range for the adaptive loop. */
michael@0 3824 bottom_index = cpi->active_best_quality;
michael@0 3825 top_index = cpi->active_worst_quality;
michael@0 3826 q_low = cpi->active_best_quality;
michael@0 3827 q_high = cpi->active_worst_quality;
michael@0 3828 #endif
michael@0 3829
michael@0 3830 vp8_save_coding_context(cpi);
michael@0 3831
michael@0 3832 loop_count = 0;
michael@0 3833
michael@0 3834 scale_and_extend_source(cpi->un_scaled_source, cpi);
michael@0 3835
michael@0 3836 #if !(CONFIG_REALTIME_ONLY) && CONFIG_POSTPROC && !(CONFIG_TEMPORAL_DENOISING)
michael@0 3837
michael@0 3838 if (cpi->oxcf.noise_sensitivity > 0)
michael@0 3839 {
michael@0 3840 unsigned char *src;
michael@0 3841 int l = 0;
michael@0 3842
michael@0 3843 switch (cpi->oxcf.noise_sensitivity)
michael@0 3844 {
michael@0 3845 case 1:
michael@0 3846 l = 20;
michael@0 3847 break;
michael@0 3848 case 2:
michael@0 3849 l = 40;
michael@0 3850 break;
michael@0 3851 case 3:
michael@0 3852 l = 60;
michael@0 3853 break;
michael@0 3854 case 4:
michael@0 3855 l = 80;
michael@0 3856 break;
michael@0 3857 case 5:
michael@0 3858 l = 100;
michael@0 3859 break;
michael@0 3860 case 6:
michael@0 3861 l = 150;
michael@0 3862 break;
michael@0 3863 }
michael@0 3864
michael@0 3865
michael@0 3866 if (cm->frame_type == KEY_FRAME)
michael@0 3867 {
michael@0 3868 vp8_de_noise(cm, cpi->Source, cpi->Source, l , 1, 0);
michael@0 3869 }
michael@0 3870 else
michael@0 3871 {
michael@0 3872 vp8_de_noise(cm, cpi->Source, cpi->Source, l , 1, 0);
michael@0 3873
michael@0 3874 src = cpi->Source->y_buffer;
michael@0 3875
michael@0 3876 if (cpi->Source->y_stride < 0)
michael@0 3877 {
michael@0 3878 src += cpi->Source->y_stride * (cpi->Source->y_height - 1);
michael@0 3879 }
michael@0 3880 }
michael@0 3881 }
michael@0 3882
michael@0 3883 #endif
michael@0 3884
michael@0 3885 #ifdef OUTPUT_YUV_SRC
michael@0 3886 vp8_write_yuv_frame(cpi->Source);
michael@0 3887 #endif
michael@0 3888
michael@0 3889 do
michael@0 3890 {
michael@0 3891 vp8_clear_system_state();
michael@0 3892
michael@0 3893 vp8_set_quantizer(cpi, Q);
michael@0 3894
michael@0 3895 /* setup skip prob for costing in mode/mv decision */
michael@0 3896 if (cpi->common.mb_no_coeff_skip)
michael@0 3897 {
michael@0 3898 cpi->prob_skip_false = cpi->base_skip_false_prob[Q];
michael@0 3899
michael@0 3900 if (cm->frame_type != KEY_FRAME)
michael@0 3901 {
michael@0 3902 if (cpi->common.refresh_alt_ref_frame)
michael@0 3903 {
michael@0 3904 if (cpi->last_skip_false_probs[2] != 0)
michael@0 3905 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
michael@0 3906
michael@0 3907 /*
michael@0 3908 if(cpi->last_skip_false_probs[2]!=0 && abs(Q- cpi->last_skip_probs_q[2])<=16 )
michael@0 3909 cpi->prob_skip_false = cpi->last_skip_false_probs[2];
michael@0 3910 else if (cpi->last_skip_false_probs[2]!=0)
michael@0 3911 cpi->prob_skip_false = (cpi->last_skip_false_probs[2] + cpi->prob_skip_false ) / 2;
michael@0 3912 */
michael@0 3913 }
michael@0 3914 else if (cpi->common.refresh_golden_frame)
michael@0 3915 {
michael@0 3916 if (cpi->last_skip_false_probs[1] != 0)
michael@0 3917 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
michael@0 3918
michael@0 3919 /*
michael@0 3920 if(cpi->last_skip_false_probs[1]!=0 && abs(Q- cpi->last_skip_probs_q[1])<=16 )
michael@0 3921 cpi->prob_skip_false = cpi->last_skip_false_probs[1];
michael@0 3922 else if (cpi->last_skip_false_probs[1]!=0)
michael@0 3923 cpi->prob_skip_false = (cpi->last_skip_false_probs[1] + cpi->prob_skip_false ) / 2;
michael@0 3924 */
michael@0 3925 }
michael@0 3926 else
michael@0 3927 {
michael@0 3928 if (cpi->last_skip_false_probs[0] != 0)
michael@0 3929 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
michael@0 3930
michael@0 3931 /*
michael@0 3932 if(cpi->last_skip_false_probs[0]!=0 && abs(Q- cpi->last_skip_probs_q[0])<=16 )
michael@0 3933 cpi->prob_skip_false = cpi->last_skip_false_probs[0];
michael@0 3934 else if(cpi->last_skip_false_probs[0]!=0)
michael@0 3935 cpi->prob_skip_false = (cpi->last_skip_false_probs[0] + cpi->prob_skip_false ) / 2;
michael@0 3936 */
michael@0 3937 }
michael@0 3938
michael@0 3939 /* as this is for cost estimate, let's make sure it does not
michael@0 3940 * go extreme eitehr way
michael@0 3941 */
michael@0 3942 if (cpi->prob_skip_false < 5)
michael@0 3943 cpi->prob_skip_false = 5;
michael@0 3944
michael@0 3945 if (cpi->prob_skip_false > 250)
michael@0 3946 cpi->prob_skip_false = 250;
michael@0 3947
michael@0 3948 if (cpi->oxcf.number_of_layers == 1 && cpi->is_src_frame_alt_ref)
michael@0 3949 cpi->prob_skip_false = 1;
michael@0 3950 }
michael@0 3951
michael@0 3952 #if 0
michael@0 3953
michael@0 3954 if (cpi->pass != 1)
michael@0 3955 {
michael@0 3956 FILE *f = fopen("skip.stt", "a");
michael@0 3957 fprintf(f, "%d, %d, %4d ", cpi->common.refresh_golden_frame, cpi->common.refresh_alt_ref_frame, cpi->prob_skip_false);
michael@0 3958 fclose(f);
michael@0 3959 }
michael@0 3960
michael@0 3961 #endif
michael@0 3962
michael@0 3963 }
michael@0 3964
michael@0 3965 if (cm->frame_type == KEY_FRAME)
michael@0 3966 {
michael@0 3967 if(resize_key_frame(cpi))
michael@0 3968 {
michael@0 3969 /* If the frame size has changed, need to reset Q, quantizer,
michael@0 3970 * and background refresh.
michael@0 3971 */
michael@0 3972 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
michael@0 3973 if (cpi->cyclic_refresh_mode_enabled)
michael@0 3974 {
michael@0 3975 if (cpi->current_layer==0)
michael@0 3976 cyclic_background_refresh(cpi, Q, 0);
michael@0 3977 else
michael@0 3978 disable_segmentation(cpi);
michael@0 3979 }
michael@0 3980 vp8_set_quantizer(cpi, Q);
michael@0 3981 }
michael@0 3982
michael@0 3983 vp8_setup_key_frame(cpi);
michael@0 3984 }
michael@0 3985
michael@0 3986
michael@0 3987
michael@0 3988 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
michael@0 3989 {
michael@0 3990 if(cpi->oxcf.error_resilient_mode)
michael@0 3991 cm->refresh_entropy_probs = 0;
michael@0 3992
michael@0 3993 if (cpi->oxcf.error_resilient_mode & VPX_ERROR_RESILIENT_PARTITIONS)
michael@0 3994 {
michael@0 3995 if (cm->frame_type == KEY_FRAME)
michael@0 3996 cm->refresh_entropy_probs = 1;
michael@0 3997 }
michael@0 3998
michael@0 3999 if (cm->refresh_entropy_probs == 0)
michael@0 4000 {
michael@0 4001 /* save a copy for later refresh */
michael@0 4002 vpx_memcpy(&cm->lfc, &cm->fc, sizeof(cm->fc));
michael@0 4003 }
michael@0 4004
michael@0 4005 vp8_update_coef_context(cpi);
michael@0 4006
michael@0 4007 vp8_update_coef_probs(cpi);
michael@0 4008
michael@0 4009 /* transform / motion compensation build reconstruction frame
michael@0 4010 * +pack coef partitions
michael@0 4011 */
michael@0 4012 vp8_encode_frame(cpi);
michael@0 4013
michael@0 4014 /* cpi->projected_frame_size is not needed for RT mode */
michael@0 4015 }
michael@0 4016 #else
michael@0 4017 /* transform / motion compensation build reconstruction frame */
michael@0 4018 vp8_encode_frame(cpi);
michael@0 4019
michael@0 4020 cpi->projected_frame_size -= vp8_estimate_entropy_savings(cpi);
michael@0 4021 cpi->projected_frame_size = (cpi->projected_frame_size > 0) ? cpi->projected_frame_size : 0;
michael@0 4022 #endif
michael@0 4023 vp8_clear_system_state();
michael@0 4024
michael@0 4025 /* Test to see if the stats generated for this frame indicate that
michael@0 4026 * we should have coded a key frame (assuming that we didn't)!
michael@0 4027 */
michael@0 4028
michael@0 4029 if (cpi->pass != 2 && cpi->oxcf.auto_key && cm->frame_type != KEY_FRAME
michael@0 4030 && cpi->compressor_speed != 2)
michael@0 4031 {
michael@0 4032 #if !(CONFIG_REALTIME_ONLY)
michael@0 4033 if (decide_key_frame(cpi))
michael@0 4034 {
michael@0 4035 /* Reset all our sizing numbers and recode */
michael@0 4036 cm->frame_type = KEY_FRAME;
michael@0 4037
michael@0 4038 vp8_pick_frame_size(cpi);
michael@0 4039
michael@0 4040 /* Clear the Alt reference frame active flag when we have
michael@0 4041 * a key frame
michael@0 4042 */
michael@0 4043 cpi->source_alt_ref_active = 0;
michael@0 4044
michael@0 4045 // Set the loop filter deltas and segmentation map update
michael@0 4046 setup_features(cpi);
michael@0 4047
michael@0 4048 vp8_restore_coding_context(cpi);
michael@0 4049
michael@0 4050 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
michael@0 4051
michael@0 4052 vp8_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, &frame_over_shoot_limit);
michael@0 4053
michael@0 4054 /* Limit Q range for the adaptive loop. */
michael@0 4055 bottom_index = cpi->active_best_quality;
michael@0 4056 top_index = cpi->active_worst_quality;
michael@0 4057 q_low = cpi->active_best_quality;
michael@0 4058 q_high = cpi->active_worst_quality;
michael@0 4059
michael@0 4060 loop_count++;
michael@0 4061 Loop = 1;
michael@0 4062
michael@0 4063 continue;
michael@0 4064 }
michael@0 4065 #endif
michael@0 4066 }
michael@0 4067
michael@0 4068 vp8_clear_system_state();
michael@0 4069
michael@0 4070 if (frame_over_shoot_limit == 0)
michael@0 4071 frame_over_shoot_limit = 1;
michael@0 4072
michael@0 4073 /* Are we are overshooting and up against the limit of active max Q. */
michael@0 4074 if (((cpi->pass != 2) || (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)) &&
michael@0 4075 (Q == cpi->active_worst_quality) &&
michael@0 4076 (cpi->active_worst_quality < cpi->worst_quality) &&
michael@0 4077 (cpi->projected_frame_size > frame_over_shoot_limit))
michael@0 4078 {
michael@0 4079 int over_size_percent = ((cpi->projected_frame_size - frame_over_shoot_limit) * 100) / frame_over_shoot_limit;
michael@0 4080
michael@0 4081 /* If so is there any scope for relaxing it */
michael@0 4082 while ((cpi->active_worst_quality < cpi->worst_quality) && (over_size_percent > 0))
michael@0 4083 {
michael@0 4084 cpi->active_worst_quality++;
michael@0 4085 /* Assume 1 qstep = about 4% on frame size. */
michael@0 4086 over_size_percent = (int)(over_size_percent * 0.96);
michael@0 4087 }
michael@0 4088 #if !(CONFIG_REALTIME_ONLY)
michael@0 4089 top_index = cpi->active_worst_quality;
michael@0 4090 #endif
michael@0 4091 /* If we have updated the active max Q do not call
michael@0 4092 * vp8_update_rate_correction_factors() this loop.
michael@0 4093 */
michael@0 4094 active_worst_qchanged = 1;
michael@0 4095 }
michael@0 4096 else
michael@0 4097 active_worst_qchanged = 0;
michael@0 4098
michael@0 4099 #if !(CONFIG_REALTIME_ONLY)
michael@0 4100 /* Special case handling for forced key frames */
michael@0 4101 if ( (cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced )
michael@0 4102 {
michael@0 4103 int last_q = Q;
michael@0 4104 int kf_err = vp8_calc_ss_err(cpi->Source,
michael@0 4105 &cm->yv12_fb[cm->new_fb_idx]);
michael@0 4106
michael@0 4107 /* The key frame is not good enough */
michael@0 4108 if ( kf_err > ((cpi->ambient_err * 7) >> 3) )
michael@0 4109 {
michael@0 4110 /* Lower q_high */
michael@0 4111 q_high = (Q > q_low) ? (Q - 1) : q_low;
michael@0 4112
michael@0 4113 /* Adjust Q */
michael@0 4114 Q = (q_high + q_low) >> 1;
michael@0 4115 }
michael@0 4116 /* The key frame is much better than the previous frame */
michael@0 4117 else if ( kf_err < (cpi->ambient_err >> 1) )
michael@0 4118 {
michael@0 4119 /* Raise q_low */
michael@0 4120 q_low = (Q < q_high) ? (Q + 1) : q_high;
michael@0 4121
michael@0 4122 /* Adjust Q */
michael@0 4123 Q = (q_high + q_low + 1) >> 1;
michael@0 4124 }
michael@0 4125
michael@0 4126 /* Clamp Q to upper and lower limits: */
michael@0 4127 if (Q > q_high)
michael@0 4128 Q = q_high;
michael@0 4129 else if (Q < q_low)
michael@0 4130 Q = q_low;
michael@0 4131
michael@0 4132 Loop = Q != last_q;
michael@0 4133 }
michael@0 4134
michael@0 4135 /* Is the projected frame size out of range and are we allowed
michael@0 4136 * to attempt to recode.
michael@0 4137 */
michael@0 4138 else if ( recode_loop_test( cpi,
michael@0 4139 frame_over_shoot_limit, frame_under_shoot_limit,
michael@0 4140 Q, top_index, bottom_index ) )
michael@0 4141 {
michael@0 4142 int last_q = Q;
michael@0 4143 int Retries = 0;
michael@0 4144
michael@0 4145 /* Frame size out of permitted range. Update correction factor
michael@0 4146 * & compute new Q to try...
michael@0 4147 */
michael@0 4148
michael@0 4149 /* Frame is too large */
michael@0 4150 if (cpi->projected_frame_size > cpi->this_frame_target)
michael@0 4151 {
michael@0 4152 /* Raise Qlow as to at least the current value */
michael@0 4153 q_low = (Q < q_high) ? (Q + 1) : q_high;
michael@0 4154
michael@0 4155 /* If we are using over quant do the same for zbin_oq_low */
michael@0 4156 if (cpi->mb.zbin_over_quant > 0)
michael@0 4157 zbin_oq_low = (cpi->mb.zbin_over_quant < zbin_oq_high) ?
michael@0 4158 (cpi->mb.zbin_over_quant + 1) : zbin_oq_high;
michael@0 4159
michael@0 4160 if (undershoot_seen)
michael@0 4161 {
michael@0 4162 /* Update rate_correction_factor unless
michael@0 4163 * cpi->active_worst_quality has changed.
michael@0 4164 */
michael@0 4165 if (!active_worst_qchanged)
michael@0 4166 vp8_update_rate_correction_factors(cpi, 1);
michael@0 4167
michael@0 4168 Q = (q_high + q_low + 1) / 2;
michael@0 4169
michael@0 4170 /* Adjust cpi->zbin_over_quant (only allowed when Q
michael@0 4171 * is max)
michael@0 4172 */
michael@0 4173 if (Q < MAXQ)
michael@0 4174 cpi->mb.zbin_over_quant = 0;
michael@0 4175 else
michael@0 4176 {
michael@0 4177 zbin_oq_low = (cpi->mb.zbin_over_quant < zbin_oq_high) ?
michael@0 4178 (cpi->mb.zbin_over_quant + 1) : zbin_oq_high;
michael@0 4179 cpi->mb.zbin_over_quant =
michael@0 4180 (zbin_oq_high + zbin_oq_low) / 2;
michael@0 4181 }
michael@0 4182 }
michael@0 4183 else
michael@0 4184 {
michael@0 4185 /* Update rate_correction_factor unless
michael@0 4186 * cpi->active_worst_quality has changed.
michael@0 4187 */
michael@0 4188 if (!active_worst_qchanged)
michael@0 4189 vp8_update_rate_correction_factors(cpi, 0);
michael@0 4190
michael@0 4191 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
michael@0 4192
michael@0 4193 while (((Q < q_low) ||
michael@0 4194 (cpi->mb.zbin_over_quant < zbin_oq_low)) &&
michael@0 4195 (Retries < 10))
michael@0 4196 {
michael@0 4197 vp8_update_rate_correction_factors(cpi, 0);
michael@0 4198 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
michael@0 4199 Retries ++;
michael@0 4200 }
michael@0 4201 }
michael@0 4202
michael@0 4203 overshoot_seen = 1;
michael@0 4204 }
michael@0 4205 /* Frame is too small */
michael@0 4206 else
michael@0 4207 {
michael@0 4208 if (cpi->mb.zbin_over_quant == 0)
michael@0 4209 /* Lower q_high if not using over quant */
michael@0 4210 q_high = (Q > q_low) ? (Q - 1) : q_low;
michael@0 4211 else
michael@0 4212 /* else lower zbin_oq_high */
michael@0 4213 zbin_oq_high = (cpi->mb.zbin_over_quant > zbin_oq_low) ?
michael@0 4214 (cpi->mb.zbin_over_quant - 1) : zbin_oq_low;
michael@0 4215
michael@0 4216 if (overshoot_seen)
michael@0 4217 {
michael@0 4218 /* Update rate_correction_factor unless
michael@0 4219 * cpi->active_worst_quality has changed.
michael@0 4220 */
michael@0 4221 if (!active_worst_qchanged)
michael@0 4222 vp8_update_rate_correction_factors(cpi, 1);
michael@0 4223
michael@0 4224 Q = (q_high + q_low) / 2;
michael@0 4225
michael@0 4226 /* Adjust cpi->zbin_over_quant (only allowed when Q
michael@0 4227 * is max)
michael@0 4228 */
michael@0 4229 if (Q < MAXQ)
michael@0 4230 cpi->mb.zbin_over_quant = 0;
michael@0 4231 else
michael@0 4232 cpi->mb.zbin_over_quant =
michael@0 4233 (zbin_oq_high + zbin_oq_low) / 2;
michael@0 4234 }
michael@0 4235 else
michael@0 4236 {
michael@0 4237 /* Update rate_correction_factor unless
michael@0 4238 * cpi->active_worst_quality has changed.
michael@0 4239 */
michael@0 4240 if (!active_worst_qchanged)
michael@0 4241 vp8_update_rate_correction_factors(cpi, 0);
michael@0 4242
michael@0 4243 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
michael@0 4244
michael@0 4245 /* Special case reset for qlow for constrained quality.
michael@0 4246 * This should only trigger where there is very substantial
michael@0 4247 * undershoot on a frame and the auto cq level is above
michael@0 4248 * the user passsed in value.
michael@0 4249 */
michael@0 4250 if ( (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
michael@0 4251 (Q < q_low) )
michael@0 4252 {
michael@0 4253 q_low = Q;
michael@0 4254 }
michael@0 4255
michael@0 4256 while (((Q > q_high) ||
michael@0 4257 (cpi->mb.zbin_over_quant > zbin_oq_high)) &&
michael@0 4258 (Retries < 10))
michael@0 4259 {
michael@0 4260 vp8_update_rate_correction_factors(cpi, 0);
michael@0 4261 Q = vp8_regulate_q(cpi, cpi->this_frame_target);
michael@0 4262 Retries ++;
michael@0 4263 }
michael@0 4264 }
michael@0 4265
michael@0 4266 undershoot_seen = 1;
michael@0 4267 }
michael@0 4268
michael@0 4269 /* Clamp Q to upper and lower limits: */
michael@0 4270 if (Q > q_high)
michael@0 4271 Q = q_high;
michael@0 4272 else if (Q < q_low)
michael@0 4273 Q = q_low;
michael@0 4274
michael@0 4275 /* Clamp cpi->zbin_over_quant */
michael@0 4276 cpi->mb.zbin_over_quant = (cpi->mb.zbin_over_quant < zbin_oq_low) ?
michael@0 4277 zbin_oq_low : (cpi->mb.zbin_over_quant > zbin_oq_high) ?
michael@0 4278 zbin_oq_high : cpi->mb.zbin_over_quant;
michael@0 4279
michael@0 4280 Loop = Q != last_q;
michael@0 4281 }
michael@0 4282 else
michael@0 4283 #endif
michael@0 4284 Loop = 0;
michael@0 4285
michael@0 4286 if (cpi->is_src_frame_alt_ref)
michael@0 4287 Loop = 0;
michael@0 4288
michael@0 4289 if (Loop == 1)
michael@0 4290 {
michael@0 4291 vp8_restore_coding_context(cpi);
michael@0 4292 loop_count++;
michael@0 4293 #if CONFIG_INTERNAL_STATS
michael@0 4294 cpi->tot_recode_hits++;
michael@0 4295 #endif
michael@0 4296 }
michael@0 4297 }
michael@0 4298 while (Loop == 1);
michael@0 4299
michael@0 4300 #if 0
michael@0 4301 /* Experimental code for lagged and one pass
michael@0 4302 * Update stats used for one pass GF selection
michael@0 4303 */
michael@0 4304 {
michael@0 4305 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_coded_error = (double)cpi->prediction_error;
michael@0 4306 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_intra_error = (double)cpi->intra_error;
michael@0 4307 cpi->one_pass_frame_stats[cpi->one_pass_frame_index].frame_pcnt_inter = (double)(100 - cpi->this_frame_percent_intra) / 100.0;
michael@0 4308 }
michael@0 4309 #endif
michael@0 4310
michael@0 4311 /* Special case code to reduce pulsing when key frames are forced at a
michael@0 4312 * fixed interval. Note the reconstruction error if it is the frame before
michael@0 4313 * the force key frame
michael@0 4314 */
michael@0 4315 if ( cpi->next_key_frame_forced && (cpi->twopass.frames_to_key == 0) )
michael@0 4316 {
michael@0 4317 cpi->ambient_err = vp8_calc_ss_err(cpi->Source,
michael@0 4318 &cm->yv12_fb[cm->new_fb_idx]);
michael@0 4319 }
michael@0 4320
michael@0 4321 /* This frame's MVs are saved and will be used in next frame's MV predictor.
michael@0 4322 * Last frame has one more line(add to bottom) and one more column(add to
michael@0 4323 * right) than cm->mip. The edge elements are initialized to 0.
michael@0 4324 */
michael@0 4325 #if CONFIG_MULTI_RES_ENCODING
michael@0 4326 if(!cpi->oxcf.mr_encoder_id && cm->show_frame)
michael@0 4327 #else
michael@0 4328 if(cm->show_frame) /* do not save for altref frame */
michael@0 4329 #endif
michael@0 4330 {
michael@0 4331 int mb_row;
michael@0 4332 int mb_col;
michael@0 4333 /* Point to beginning of allocated MODE_INFO arrays. */
michael@0 4334 MODE_INFO *tmp = cm->mip;
michael@0 4335
michael@0 4336 if(cm->frame_type != KEY_FRAME)
michael@0 4337 {
michael@0 4338 for (mb_row = 0; mb_row < cm->mb_rows+1; mb_row ++)
michael@0 4339 {
michael@0 4340 for (mb_col = 0; mb_col < cm->mb_cols+1; mb_col ++)
michael@0 4341 {
michael@0 4342 if(tmp->mbmi.ref_frame != INTRA_FRAME)
michael@0 4343 cpi->lfmv[mb_col + mb_row*(cm->mode_info_stride+1)].as_int = tmp->mbmi.mv.as_int;
michael@0 4344
michael@0 4345 cpi->lf_ref_frame_sign_bias[mb_col + mb_row*(cm->mode_info_stride+1)] = cm->ref_frame_sign_bias[tmp->mbmi.ref_frame];
michael@0 4346 cpi->lf_ref_frame[mb_col + mb_row*(cm->mode_info_stride+1)] = tmp->mbmi.ref_frame;
michael@0 4347 tmp++;
michael@0 4348 }
michael@0 4349 }
michael@0 4350 }
michael@0 4351 }
michael@0 4352
michael@0 4353 /* Count last ref frame 0,0 usage on current encoded frame. */
michael@0 4354 {
michael@0 4355 int mb_row;
michael@0 4356 int mb_col;
michael@0 4357 /* Point to beginning of MODE_INFO arrays. */
michael@0 4358 MODE_INFO *tmp = cm->mi;
michael@0 4359
michael@0 4360 cpi->zeromv_count = 0;
michael@0 4361
michael@0 4362 if(cm->frame_type != KEY_FRAME)
michael@0 4363 {
michael@0 4364 for (mb_row = 0; mb_row < cm->mb_rows; mb_row ++)
michael@0 4365 {
michael@0 4366 for (mb_col = 0; mb_col < cm->mb_cols; mb_col ++)
michael@0 4367 {
michael@0 4368 if(tmp->mbmi.mode == ZEROMV)
michael@0 4369 cpi->zeromv_count++;
michael@0 4370 tmp++;
michael@0 4371 }
michael@0 4372 tmp++;
michael@0 4373 }
michael@0 4374 }
michael@0 4375 }
michael@0 4376
michael@0 4377 #if CONFIG_MULTI_RES_ENCODING
michael@0 4378 vp8_cal_dissimilarity(cpi);
michael@0 4379 #endif
michael@0 4380
michael@0 4381 /* Update the GF useage maps.
michael@0 4382 * This is done after completing the compression of a frame when all
michael@0 4383 * modes etc. are finalized but before loop filter
michael@0 4384 */
michael@0 4385 if (cpi->oxcf.number_of_layers == 1)
michael@0 4386 vp8_update_gf_useage_maps(cpi, cm, &cpi->mb);
michael@0 4387
michael@0 4388 if (cm->frame_type == KEY_FRAME)
michael@0 4389 cm->refresh_last_frame = 1;
michael@0 4390
michael@0 4391 #if 0
michael@0 4392 {
michael@0 4393 FILE *f = fopen("gfactive.stt", "a");
michael@0 4394 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);
michael@0 4395 fclose(f);
michael@0 4396 }
michael@0 4397 #endif
michael@0 4398
michael@0 4399 /* For inter frames the current default behavior is that when
michael@0 4400 * cm->refresh_golden_frame is set we copy the old GF over to the ARF buffer
michael@0 4401 * This is purely an encoder decision at present.
michael@0 4402 */
michael@0 4403 if (!cpi->oxcf.error_resilient_mode && cm->refresh_golden_frame)
michael@0 4404 cm->copy_buffer_to_arf = 2;
michael@0 4405 else
michael@0 4406 cm->copy_buffer_to_arf = 0;
michael@0 4407
michael@0 4408 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
michael@0 4409
michael@0 4410 #if CONFIG_MULTITHREAD
michael@0 4411 if (cpi->b_multi_threaded)
michael@0 4412 {
michael@0 4413 /* start loopfilter in separate thread */
michael@0 4414 sem_post(&cpi->h_event_start_lpf);
michael@0 4415 cpi->b_lpf_running = 1;
michael@0 4416 }
michael@0 4417 else
michael@0 4418 #endif
michael@0 4419 {
michael@0 4420 vp8_loopfilter_frame(cpi, cm);
michael@0 4421 }
michael@0 4422
michael@0 4423 update_reference_frames(cpi);
michael@0 4424
michael@0 4425 #if !(CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING)
michael@0 4426 if (cpi->oxcf.error_resilient_mode)
michael@0 4427 {
michael@0 4428 cm->refresh_entropy_probs = 0;
michael@0 4429 }
michael@0 4430 #endif
michael@0 4431
michael@0 4432 #if CONFIG_MULTITHREAD
michael@0 4433 /* wait that filter_level is picked so that we can continue with stream packing */
michael@0 4434 if (cpi->b_multi_threaded)
michael@0 4435 sem_wait(&cpi->h_event_end_lpf);
michael@0 4436 #endif
michael@0 4437
michael@0 4438 /* build the bitstream */
michael@0 4439 vp8_pack_bitstream(cpi, dest, dest_end, size);
michael@0 4440
michael@0 4441 #if CONFIG_MULTITHREAD
michael@0 4442 /* if PSNR packets are generated we have to wait for the lpf */
michael@0 4443 if (cpi->b_lpf_running && cpi->b_calculate_psnr)
michael@0 4444 {
michael@0 4445 sem_wait(&cpi->h_event_end_lpf);
michael@0 4446 cpi->b_lpf_running = 0;
michael@0 4447 }
michael@0 4448 #endif
michael@0 4449
michael@0 4450 /* Move storing frame_type out of the above loop since it is also
michael@0 4451 * needed in motion search besides loopfilter */
michael@0 4452 cm->last_frame_type = cm->frame_type;
michael@0 4453
michael@0 4454 /* Update rate control heuristics */
michael@0 4455 cpi->total_byte_count += (*size);
michael@0 4456 cpi->projected_frame_size = (*size) << 3;
michael@0 4457
michael@0 4458 if (cpi->oxcf.number_of_layers > 1)
michael@0 4459 {
michael@0 4460 unsigned int i;
michael@0 4461 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
michael@0 4462 cpi->layer_context[i].total_byte_count += (*size);
michael@0 4463 }
michael@0 4464
michael@0 4465 if (!active_worst_qchanged)
michael@0 4466 vp8_update_rate_correction_factors(cpi, 2);
michael@0 4467
michael@0 4468 cpi->last_q[cm->frame_type] = cm->base_qindex;
michael@0 4469
michael@0 4470 if (cm->frame_type == KEY_FRAME)
michael@0 4471 {
michael@0 4472 vp8_adjust_key_frame_context(cpi);
michael@0 4473 }
michael@0 4474
michael@0 4475 /* Keep a record of ambient average Q. */
michael@0 4476 if (cm->frame_type != KEY_FRAME)
michael@0 4477 cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex + cm->base_qindex) >> 2;
michael@0 4478
michael@0 4479 /* Keep a record from which we can calculate the average Q excluding
michael@0 4480 * GF updates and key frames
michael@0 4481 */
michael@0 4482 if ((cm->frame_type != KEY_FRAME) && ((cpi->oxcf.number_of_layers > 1) ||
michael@0 4483 (!cm->refresh_golden_frame && !cm->refresh_alt_ref_frame)))
michael@0 4484 {
michael@0 4485 cpi->ni_frames++;
michael@0 4486
michael@0 4487 /* Calculate the average Q for normal inter frames (not key or GFU
michael@0 4488 * frames).
michael@0 4489 */
michael@0 4490 if ( cpi->pass == 2 )
michael@0 4491 {
michael@0 4492 cpi->ni_tot_qi += Q;
michael@0 4493 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
michael@0 4494 }
michael@0 4495 else
michael@0 4496 {
michael@0 4497 /* Damp value for first few frames */
michael@0 4498 if (cpi->ni_frames > 150 )
michael@0 4499 {
michael@0 4500 cpi->ni_tot_qi += Q;
michael@0 4501 cpi->ni_av_qi = (cpi->ni_tot_qi / cpi->ni_frames);
michael@0 4502 }
michael@0 4503 /* For one pass, early in the clip ... average the current frame Q
michael@0 4504 * value with the worstq entered by the user as a dampening measure
michael@0 4505 */
michael@0 4506 else
michael@0 4507 {
michael@0 4508 cpi->ni_tot_qi += Q;
michael@0 4509 cpi->ni_av_qi = ((cpi->ni_tot_qi / cpi->ni_frames) + cpi->worst_quality + 1) / 2;
michael@0 4510 }
michael@0 4511
michael@0 4512 /* If the average Q is higher than what was used in the last
michael@0 4513 * frame (after going through the recode loop to keep the frame
michael@0 4514 * size within range) then use the last frame value - 1. The -1
michael@0 4515 * is designed to stop Q and hence the data rate, from
michael@0 4516 * progressively falling away during difficult sections, but at
michael@0 4517 * the same time reduce the number of itterations around the
michael@0 4518 * recode loop.
michael@0 4519 */
michael@0 4520 if (Q > cpi->ni_av_qi)
michael@0 4521 cpi->ni_av_qi = Q - 1;
michael@0 4522 }
michael@0 4523 }
michael@0 4524
michael@0 4525 /* Update the buffer level variable. */
michael@0 4526 /* Non-viewable frames are a special case and are treated as pure overhead. */
michael@0 4527 if ( !cm->show_frame )
michael@0 4528 cpi->bits_off_target -= cpi->projected_frame_size;
michael@0 4529 else
michael@0 4530 cpi->bits_off_target += cpi->av_per_frame_bandwidth - cpi->projected_frame_size;
michael@0 4531
michael@0 4532 /* Clip the buffer level to the maximum specified buffer size */
michael@0 4533 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size)
michael@0 4534 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
michael@0 4535
michael@0 4536 /* Rolling monitors of whether we are over or underspending used to
michael@0 4537 * help regulate min and Max Q in two pass.
michael@0 4538 */
michael@0 4539 cpi->rolling_target_bits = ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4;
michael@0 4540 cpi->rolling_actual_bits = ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4;
michael@0 4541 cpi->long_rolling_target_bits = ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32;
michael@0 4542 cpi->long_rolling_actual_bits = ((cpi->long_rolling_actual_bits * 31) + cpi->projected_frame_size + 16) / 32;
michael@0 4543
michael@0 4544 /* Actual bits spent */
michael@0 4545 cpi->total_actual_bits += cpi->projected_frame_size;
michael@0 4546
michael@0 4547 /* Debug stats */
michael@0 4548 cpi->total_target_vs_actual += (cpi->this_frame_target - cpi->projected_frame_size);
michael@0 4549
michael@0 4550 cpi->buffer_level = cpi->bits_off_target;
michael@0 4551
michael@0 4552 /* Propagate values to higher temporal layers */
michael@0 4553 if (cpi->oxcf.number_of_layers > 1)
michael@0 4554 {
michael@0 4555 unsigned int i;
michael@0 4556
michael@0 4557 for (i=cpi->current_layer+1; i<cpi->oxcf.number_of_layers; i++)
michael@0 4558 {
michael@0 4559 LAYER_CONTEXT *lc = &cpi->layer_context[i];
michael@0 4560 int bits_off_for_this_layer =
michael@0 4561 (int)(lc->target_bandwidth / lc->framerate -
michael@0 4562 cpi->projected_frame_size);
michael@0 4563
michael@0 4564 lc->bits_off_target += bits_off_for_this_layer;
michael@0 4565
michael@0 4566 /* Clip buffer level to maximum buffer size for the layer */
michael@0 4567 if (lc->bits_off_target > lc->maximum_buffer_size)
michael@0 4568 lc->bits_off_target = lc->maximum_buffer_size;
michael@0 4569
michael@0 4570 lc->total_actual_bits += cpi->projected_frame_size;
michael@0 4571 lc->total_target_vs_actual += bits_off_for_this_layer;
michael@0 4572 lc->buffer_level = lc->bits_off_target;
michael@0 4573 }
michael@0 4574 }
michael@0 4575
michael@0 4576 /* Update bits left to the kf and gf groups to account for overshoot
michael@0 4577 * or undershoot on these frames
michael@0 4578 */
michael@0 4579 if (cm->frame_type == KEY_FRAME)
michael@0 4580 {
michael@0 4581 cpi->twopass.kf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
michael@0 4582
michael@0 4583 if (cpi->twopass.kf_group_bits < 0)
michael@0 4584 cpi->twopass.kf_group_bits = 0 ;
michael@0 4585 }
michael@0 4586 else if (cm->refresh_golden_frame || cm->refresh_alt_ref_frame)
michael@0 4587 {
michael@0 4588 cpi->twopass.gf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
michael@0 4589
michael@0 4590 if (cpi->twopass.gf_group_bits < 0)
michael@0 4591 cpi->twopass.gf_group_bits = 0 ;
michael@0 4592 }
michael@0 4593
michael@0 4594 if (cm->frame_type != KEY_FRAME)
michael@0 4595 {
michael@0 4596 if (cpi->common.refresh_alt_ref_frame)
michael@0 4597 {
michael@0 4598 cpi->last_skip_false_probs[2] = cpi->prob_skip_false;
michael@0 4599 cpi->last_skip_probs_q[2] = cm->base_qindex;
michael@0 4600 }
michael@0 4601 else if (cpi->common.refresh_golden_frame)
michael@0 4602 {
michael@0 4603 cpi->last_skip_false_probs[1] = cpi->prob_skip_false;
michael@0 4604 cpi->last_skip_probs_q[1] = cm->base_qindex;
michael@0 4605 }
michael@0 4606 else
michael@0 4607 {
michael@0 4608 cpi->last_skip_false_probs[0] = cpi->prob_skip_false;
michael@0 4609 cpi->last_skip_probs_q[0] = cm->base_qindex;
michael@0 4610
michael@0 4611 /* update the baseline */
michael@0 4612 cpi->base_skip_false_prob[cm->base_qindex] = cpi->prob_skip_false;
michael@0 4613
michael@0 4614 }
michael@0 4615 }
michael@0 4616
michael@0 4617 #if 0 && CONFIG_INTERNAL_STATS
michael@0 4618 {
michael@0 4619 FILE *f = fopen("tmp.stt", "a");
michael@0 4620
michael@0 4621 vp8_clear_system_state();
michael@0 4622
michael@0 4623 if (cpi->twopass.total_left_stats.coded_error != 0.0)
michael@0 4624 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %10d %6d %6d"
michael@0 4625 "%6d %6d %6d %5d %5d %5d %8d %8.2f %10d %10.3f"
michael@0 4626 "%10.3f %8d\n",
michael@0 4627 cpi->common.current_video_frame, cpi->this_frame_target,
michael@0 4628 cpi->projected_frame_size,
michael@0 4629 (cpi->projected_frame_size - cpi->this_frame_target),
michael@0 4630 (int)cpi->total_target_vs_actual,
michael@0 4631 cpi->buffer_level,
michael@0 4632 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
michael@0 4633 (int)cpi->total_actual_bits, cm->base_qindex,
michael@0 4634 cpi->active_best_quality, cpi->active_worst_quality,
michael@0 4635 cpi->ni_av_qi, cpi->cq_target_quality,
michael@0 4636 cpi->zbin_over_quant,
michael@0 4637 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
michael@0 4638 cm->frame_type, cpi->gfu_boost,
michael@0 4639 cpi->twopass.est_max_qcorrection_factor,
michael@0 4640 (int)cpi->twopass.bits_left,
michael@0 4641 cpi->twopass.total_left_stats.coded_error,
michael@0 4642 (double)cpi->twopass.bits_left /
michael@0 4643 cpi->twopass.total_left_stats.coded_error,
michael@0 4644 cpi->tot_recode_hits);
michael@0 4645 else
michael@0 4646 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %10d %6d %6d"
michael@0 4647 "%6d %6d %6d %5d %5d %5d %8d %8.2f %10d %10.3f"
michael@0 4648 "%8d\n",
michael@0 4649 cpi->common.current_video_frame,
michael@0 4650 cpi->this_frame_target, cpi->projected_frame_size,
michael@0 4651 (cpi->projected_frame_size - cpi->this_frame_target),
michael@0 4652 (int)cpi->total_target_vs_actual,
michael@0 4653 cpi->buffer_level,
michael@0 4654 (cpi->oxcf.starting_buffer_level-cpi->bits_off_target),
michael@0 4655 (int)cpi->total_actual_bits, cm->base_qindex,
michael@0 4656 cpi->active_best_quality, cpi->active_worst_quality,
michael@0 4657 cpi->ni_av_qi, cpi->cq_target_quality,
michael@0 4658 cpi->zbin_over_quant,
michael@0 4659 cm->refresh_golden_frame, cm->refresh_alt_ref_frame,
michael@0 4660 cm->frame_type, cpi->gfu_boost,
michael@0 4661 cpi->twopass.est_max_qcorrection_factor,
michael@0 4662 (int)cpi->twopass.bits_left,
michael@0 4663 cpi->twopass.total_left_stats.coded_error,
michael@0 4664 cpi->tot_recode_hits);
michael@0 4665
michael@0 4666 fclose(f);
michael@0 4667
michael@0 4668 {
michael@0 4669 FILE *fmodes = fopen("Modes.stt", "a");
michael@0 4670 int i;
michael@0 4671
michael@0 4672 fprintf(fmodes, "%6d:%1d:%1d:%1d ",
michael@0 4673 cpi->common.current_video_frame,
michael@0 4674 cm->frame_type, cm->refresh_golden_frame,
michael@0 4675 cm->refresh_alt_ref_frame);
michael@0 4676
michael@0 4677 fprintf(fmodes, "\n");
michael@0 4678
michael@0 4679 fclose(fmodes);
michael@0 4680 }
michael@0 4681 }
michael@0 4682
michael@0 4683 #endif
michael@0 4684
michael@0 4685 if (cm->refresh_golden_frame == 1)
michael@0 4686 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN;
michael@0 4687 else
michael@0 4688 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_GOLDEN;
michael@0 4689
michael@0 4690 if (cm->refresh_alt_ref_frame == 1)
michael@0 4691 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF;
michael@0 4692 else
michael@0 4693 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_ALTREF;
michael@0 4694
michael@0 4695
michael@0 4696 if (cm->refresh_last_frame & cm->refresh_golden_frame)
michael@0 4697 /* both refreshed */
michael@0 4698 cpi->gold_is_last = 1;
michael@0 4699 else if (cm->refresh_last_frame ^ cm->refresh_golden_frame)
michael@0 4700 /* 1 refreshed but not the other */
michael@0 4701 cpi->gold_is_last = 0;
michael@0 4702
michael@0 4703 if (cm->refresh_last_frame & cm->refresh_alt_ref_frame)
michael@0 4704 /* both refreshed */
michael@0 4705 cpi->alt_is_last = 1;
michael@0 4706 else if (cm->refresh_last_frame ^ cm->refresh_alt_ref_frame)
michael@0 4707 /* 1 refreshed but not the other */
michael@0 4708 cpi->alt_is_last = 0;
michael@0 4709
michael@0 4710 if (cm->refresh_alt_ref_frame & cm->refresh_golden_frame)
michael@0 4711 /* both refreshed */
michael@0 4712 cpi->gold_is_alt = 1;
michael@0 4713 else if (cm->refresh_alt_ref_frame ^ cm->refresh_golden_frame)
michael@0 4714 /* 1 refreshed but not the other */
michael@0 4715 cpi->gold_is_alt = 0;
michael@0 4716
michael@0 4717 cpi->ref_frame_flags = VP8_ALTR_FRAME | VP8_GOLD_FRAME | VP8_LAST_FRAME;
michael@0 4718
michael@0 4719 if (cpi->gold_is_last)
michael@0 4720 cpi->ref_frame_flags &= ~VP8_GOLD_FRAME;
michael@0 4721
michael@0 4722 if (cpi->alt_is_last)
michael@0 4723 cpi->ref_frame_flags &= ~VP8_ALTR_FRAME;
michael@0 4724
michael@0 4725 if (cpi->gold_is_alt)
michael@0 4726 cpi->ref_frame_flags &= ~VP8_ALTR_FRAME;
michael@0 4727
michael@0 4728
michael@0 4729 if (!cpi->oxcf.error_resilient_mode)
michael@0 4730 {
michael@0 4731 if (cpi->oxcf.play_alternate && cm->refresh_alt_ref_frame && (cm->frame_type != KEY_FRAME))
michael@0 4732 /* Update the alternate reference frame stats as appropriate. */
michael@0 4733 update_alt_ref_frame_stats(cpi);
michael@0 4734 else
michael@0 4735 /* Update the Golden frame stats as appropriate. */
michael@0 4736 update_golden_frame_stats(cpi);
michael@0 4737 }
michael@0 4738
michael@0 4739 if (cm->frame_type == KEY_FRAME)
michael@0 4740 {
michael@0 4741 /* Tell the caller that the frame was coded as a key frame */
michael@0 4742 *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY;
michael@0 4743
michael@0 4744 /* As this frame is a key frame the next defaults to an inter frame. */
michael@0 4745 cm->frame_type = INTER_FRAME;
michael@0 4746
michael@0 4747 cpi->last_frame_percent_intra = 100;
michael@0 4748 }
michael@0 4749 else
michael@0 4750 {
michael@0 4751 *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY;
michael@0 4752
michael@0 4753 cpi->last_frame_percent_intra = cpi->this_frame_percent_intra;
michael@0 4754 }
michael@0 4755
michael@0 4756 /* Clear the one shot update flags for segmentation map and mode/ref
michael@0 4757 * loop filter deltas.
michael@0 4758 */
michael@0 4759 cpi->mb.e_mbd.update_mb_segmentation_map = 0;
michael@0 4760 cpi->mb.e_mbd.update_mb_segmentation_data = 0;
michael@0 4761 cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
michael@0 4762
michael@0 4763
michael@0 4764 /* Dont increment frame counters if this was an altref buffer update
michael@0 4765 * not a real frame
michael@0 4766 */
michael@0 4767 if (cm->show_frame)
michael@0 4768 {
michael@0 4769 cm->current_video_frame++;
michael@0 4770 cpi->frames_since_key++;
michael@0 4771 cpi->temporal_pattern_counter++;
michael@0 4772 }
michael@0 4773
michael@0 4774 /* reset to normal state now that we are done. */
michael@0 4775
michael@0 4776
michael@0 4777
michael@0 4778 #if 0
michael@0 4779 {
michael@0 4780 char filename[512];
michael@0 4781 FILE *recon_file;
michael@0 4782 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
michael@0 4783 recon_file = fopen(filename, "wb");
michael@0 4784 fwrite(cm->yv12_fb[cm->lst_fb_idx].buffer_alloc,
michael@0 4785 cm->yv12_fb[cm->lst_fb_idx].frame_size, 1, recon_file);
michael@0 4786 fclose(recon_file);
michael@0 4787 }
michael@0 4788 #endif
michael@0 4789
michael@0 4790 /* DEBUG */
michael@0 4791 /* vp8_write_yuv_frame("encoder_recon.yuv", cm->frame_to_show); */
michael@0 4792
michael@0 4793
michael@0 4794 }
michael@0 4795 #if !(CONFIG_REALTIME_ONLY)
michael@0 4796 static void Pass2Encode(VP8_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned char * dest_end, unsigned int *frame_flags)
michael@0 4797 {
michael@0 4798
michael@0 4799 if (!cpi->common.refresh_alt_ref_frame)
michael@0 4800 vp8_second_pass(cpi);
michael@0 4801
michael@0 4802 encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags);
michael@0 4803 cpi->twopass.bits_left -= 8 * *size;
michael@0 4804
michael@0 4805 if (!cpi->common.refresh_alt_ref_frame)
michael@0 4806 {
michael@0 4807 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
michael@0 4808 *cpi->oxcf.two_pass_vbrmin_section / 100);
michael@0 4809 cpi->twopass.bits_left += (int64_t)(two_pass_min_rate / cpi->framerate);
michael@0 4810 }
michael@0 4811 }
michael@0 4812 #endif
michael@0 4813
michael@0 4814 /* For ARM NEON, d8-d15 are callee-saved registers, and need to be saved. */
michael@0 4815 #if HAVE_NEON
michael@0 4816 extern void vp8_push_neon(int64_t *store);
michael@0 4817 extern void vp8_pop_neon(int64_t *store);
michael@0 4818 #endif
michael@0 4819
michael@0 4820
michael@0 4821 int vp8_receive_raw_frame(VP8_COMP *cpi, unsigned int frame_flags, YV12_BUFFER_CONFIG *sd, int64_t time_stamp, int64_t end_time)
michael@0 4822 {
michael@0 4823 #if HAVE_NEON
michael@0 4824 int64_t store_reg[8];
michael@0 4825 #if CONFIG_RUNTIME_CPU_DETECT
michael@0 4826 VP8_COMMON *cm = &cpi->common;
michael@0 4827 #endif
michael@0 4828 #endif
michael@0 4829 struct vpx_usec_timer timer;
michael@0 4830 int res = 0;
michael@0 4831
michael@0 4832 #if HAVE_NEON
michael@0 4833 #if CONFIG_RUNTIME_CPU_DETECT
michael@0 4834 if (cm->cpu_caps & HAS_NEON)
michael@0 4835 #endif
michael@0 4836 {
michael@0 4837 vp8_push_neon(store_reg);
michael@0 4838 }
michael@0 4839 #endif
michael@0 4840
michael@0 4841 vpx_usec_timer_start(&timer);
michael@0 4842
michael@0 4843 /* Reinit the lookahead buffer if the frame size changes */
michael@0 4844 if (sd->y_width != cpi->oxcf.Width || sd->y_height != cpi->oxcf.Height)
michael@0 4845 {
michael@0 4846 assert(cpi->oxcf.lag_in_frames < 2);
michael@0 4847 dealloc_raw_frame_buffers(cpi);
michael@0 4848 alloc_raw_frame_buffers(cpi);
michael@0 4849 }
michael@0 4850
michael@0 4851 if(vp8_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
michael@0 4852 frame_flags, cpi->active_map_enabled ? cpi->active_map : NULL))
michael@0 4853 res = -1;
michael@0 4854 vpx_usec_timer_mark(&timer);
michael@0 4855 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
michael@0 4856
michael@0 4857 #if HAVE_NEON
michael@0 4858 #if CONFIG_RUNTIME_CPU_DETECT
michael@0 4859 if (cm->cpu_caps & HAS_NEON)
michael@0 4860 #endif
michael@0 4861 {
michael@0 4862 vp8_pop_neon(store_reg);
michael@0 4863 }
michael@0 4864 #endif
michael@0 4865
michael@0 4866 return res;
michael@0 4867 }
michael@0 4868
michael@0 4869
michael@0 4870 static int frame_is_reference(const VP8_COMP *cpi)
michael@0 4871 {
michael@0 4872 const VP8_COMMON *cm = &cpi->common;
michael@0 4873 const MACROBLOCKD *xd = &cpi->mb.e_mbd;
michael@0 4874
michael@0 4875 return cm->frame_type == KEY_FRAME || cm->refresh_last_frame
michael@0 4876 || cm->refresh_golden_frame || cm->refresh_alt_ref_frame
michael@0 4877 || cm->copy_buffer_to_gf || cm->copy_buffer_to_arf
michael@0 4878 || cm->refresh_entropy_probs
michael@0 4879 || xd->mode_ref_lf_delta_update
michael@0 4880 || xd->update_mb_segmentation_map || xd->update_mb_segmentation_data;
michael@0 4881 }
michael@0 4882
michael@0 4883
michael@0 4884 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)
michael@0 4885 {
michael@0 4886 #if HAVE_NEON
michael@0 4887 int64_t store_reg[8];
michael@0 4888 #endif
michael@0 4889 VP8_COMMON *cm;
michael@0 4890 struct vpx_usec_timer tsctimer;
michael@0 4891 struct vpx_usec_timer ticktimer;
michael@0 4892 struct vpx_usec_timer cmptimer;
michael@0 4893 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
michael@0 4894
michael@0 4895 if (!cpi)
michael@0 4896 return -1;
michael@0 4897
michael@0 4898 cm = &cpi->common;
michael@0 4899
michael@0 4900 if (setjmp(cpi->common.error.jmp))
michael@0 4901 {
michael@0 4902 cpi->common.error.setjmp = 0;
michael@0 4903 return VPX_CODEC_CORRUPT_FRAME;
michael@0 4904 }
michael@0 4905
michael@0 4906 cpi->common.error.setjmp = 1;
michael@0 4907
michael@0 4908 #if HAVE_NEON
michael@0 4909 #if CONFIG_RUNTIME_CPU_DETECT
michael@0 4910 if (cm->cpu_caps & HAS_NEON)
michael@0 4911 #endif
michael@0 4912 {
michael@0 4913 vp8_push_neon(store_reg);
michael@0 4914 }
michael@0 4915 #endif
michael@0 4916
michael@0 4917 vpx_usec_timer_start(&cmptimer);
michael@0 4918
michael@0 4919 cpi->source = NULL;
michael@0 4920
michael@0 4921 #if !(CONFIG_REALTIME_ONLY)
michael@0 4922 /* Should we code an alternate reference frame */
michael@0 4923 if (cpi->oxcf.error_resilient_mode == 0 &&
michael@0 4924 cpi->oxcf.play_alternate &&
michael@0 4925 cpi->source_alt_ref_pending)
michael@0 4926 {
michael@0 4927 if ((cpi->source = vp8_lookahead_peek(cpi->lookahead,
michael@0 4928 cpi->frames_till_gf_update_due,
michael@0 4929 PEEK_FORWARD)))
michael@0 4930 {
michael@0 4931 cpi->alt_ref_source = cpi->source;
michael@0 4932 if (cpi->oxcf.arnr_max_frames > 0)
michael@0 4933 {
michael@0 4934 vp8_temporal_filter_prepare_c(cpi,
michael@0 4935 cpi->frames_till_gf_update_due);
michael@0 4936 force_src_buffer = &cpi->alt_ref_buffer;
michael@0 4937 }
michael@0 4938 cpi->frames_till_alt_ref_frame = cpi->frames_till_gf_update_due;
michael@0 4939 cm->refresh_alt_ref_frame = 1;
michael@0 4940 cm->refresh_golden_frame = 0;
michael@0 4941 cm->refresh_last_frame = 0;
michael@0 4942 cm->show_frame = 0;
michael@0 4943 /* Clear Pending alt Ref flag. */
michael@0 4944 cpi->source_alt_ref_pending = 0;
michael@0 4945 cpi->is_src_frame_alt_ref = 0;
michael@0 4946 }
michael@0 4947 }
michael@0 4948 #endif
michael@0 4949
michael@0 4950 if (!cpi->source)
michael@0 4951 {
michael@0 4952 /* Read last frame source if we are encoding first pass. */
michael@0 4953 if (cpi->pass == 1 && cm->current_video_frame > 0)
michael@0 4954 {
michael@0 4955 if((cpi->last_source = vp8_lookahead_peek(cpi->lookahead, 1,
michael@0 4956 PEEK_BACKWARD)) == NULL)
michael@0 4957 return -1;
michael@0 4958 }
michael@0 4959
michael@0 4960
michael@0 4961 if ((cpi->source = vp8_lookahead_pop(cpi->lookahead, flush)))
michael@0 4962 {
michael@0 4963 cm->show_frame = 1;
michael@0 4964
michael@0 4965 cpi->is_src_frame_alt_ref = cpi->alt_ref_source
michael@0 4966 && (cpi->source == cpi->alt_ref_source);
michael@0 4967
michael@0 4968 if(cpi->is_src_frame_alt_ref)
michael@0 4969 cpi->alt_ref_source = NULL;
michael@0 4970 }
michael@0 4971 }
michael@0 4972
michael@0 4973 if (cpi->source)
michael@0 4974 {
michael@0 4975 cpi->Source = force_src_buffer ? force_src_buffer : &cpi->source->img;
michael@0 4976 cpi->un_scaled_source = cpi->Source;
michael@0 4977 *time_stamp = cpi->source->ts_start;
michael@0 4978 *time_end = cpi->source->ts_end;
michael@0 4979 *frame_flags = cpi->source->flags;
michael@0 4980
michael@0 4981 if (cpi->pass == 1 && cm->current_video_frame > 0)
michael@0 4982 {
michael@0 4983 cpi->last_frame_unscaled_source = &cpi->last_source->img;
michael@0 4984 }
michael@0 4985 }
michael@0 4986 else
michael@0 4987 {
michael@0 4988 *size = 0;
michael@0 4989 #if !(CONFIG_REALTIME_ONLY)
michael@0 4990
michael@0 4991 if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done)
michael@0 4992 {
michael@0 4993 vp8_end_first_pass(cpi); /* get last stats packet */
michael@0 4994 cpi->twopass.first_pass_done = 1;
michael@0 4995 }
michael@0 4996
michael@0 4997 #endif
michael@0 4998
michael@0 4999 #if HAVE_NEON
michael@0 5000 #if CONFIG_RUNTIME_CPU_DETECT
michael@0 5001 if (cm->cpu_caps & HAS_NEON)
michael@0 5002 #endif
michael@0 5003 {
michael@0 5004 vp8_pop_neon(store_reg);
michael@0 5005 }
michael@0 5006 #endif
michael@0 5007 return -1;
michael@0 5008 }
michael@0 5009
michael@0 5010 if (cpi->source->ts_start < cpi->first_time_stamp_ever)
michael@0 5011 {
michael@0 5012 cpi->first_time_stamp_ever = cpi->source->ts_start;
michael@0 5013 cpi->last_end_time_stamp_seen = cpi->source->ts_start;
michael@0 5014 }
michael@0 5015
michael@0 5016 /* adjust frame rates based on timestamps given */
michael@0 5017 if (cm->show_frame)
michael@0 5018 {
michael@0 5019 int64_t this_duration;
michael@0 5020 int step = 0;
michael@0 5021
michael@0 5022 if (cpi->source->ts_start == cpi->first_time_stamp_ever)
michael@0 5023 {
michael@0 5024 this_duration = cpi->source->ts_end - cpi->source->ts_start;
michael@0 5025 step = 1;
michael@0 5026 }
michael@0 5027 else
michael@0 5028 {
michael@0 5029 int64_t last_duration;
michael@0 5030
michael@0 5031 this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
michael@0 5032 last_duration = cpi->last_end_time_stamp_seen
michael@0 5033 - cpi->last_time_stamp_seen;
michael@0 5034 /* do a step update if the duration changes by 10% */
michael@0 5035 if (last_duration)
michael@0 5036 step = (int)(((this_duration - last_duration) *
michael@0 5037 10 / last_duration));
michael@0 5038 }
michael@0 5039
michael@0 5040 if (this_duration)
michael@0 5041 {
michael@0 5042 if (step)
michael@0 5043 cpi->ref_framerate = 10000000.0 / this_duration;
michael@0 5044 else
michael@0 5045 {
michael@0 5046 double avg_duration, interval;
michael@0 5047
michael@0 5048 /* Average this frame's rate into the last second's average
michael@0 5049 * frame rate. If we haven't seen 1 second yet, then average
michael@0 5050 * over the whole interval seen.
michael@0 5051 */
michael@0 5052 interval = (double)(cpi->source->ts_end -
michael@0 5053 cpi->first_time_stamp_ever);
michael@0 5054 if(interval > 10000000.0)
michael@0 5055 interval = 10000000;
michael@0 5056
michael@0 5057 avg_duration = 10000000.0 / cpi->ref_framerate;
michael@0 5058 avg_duration *= (interval - avg_duration + this_duration);
michael@0 5059 avg_duration /= interval;
michael@0 5060
michael@0 5061 cpi->ref_framerate = 10000000.0 / avg_duration;
michael@0 5062 }
michael@0 5063
michael@0 5064 if (cpi->oxcf.number_of_layers > 1)
michael@0 5065 {
michael@0 5066 unsigned int i;
michael@0 5067
michael@0 5068 /* Update frame rates for each layer */
michael@0 5069 for (i=0; i<cpi->oxcf.number_of_layers; i++)
michael@0 5070 {
michael@0 5071 LAYER_CONTEXT *lc = &cpi->layer_context[i];
michael@0 5072 lc->framerate = cpi->ref_framerate /
michael@0 5073 cpi->oxcf.rate_decimator[i];
michael@0 5074 }
michael@0 5075 }
michael@0 5076 else
michael@0 5077 vp8_new_framerate(cpi, cpi->ref_framerate);
michael@0 5078 }
michael@0 5079
michael@0 5080 cpi->last_time_stamp_seen = cpi->source->ts_start;
michael@0 5081 cpi->last_end_time_stamp_seen = cpi->source->ts_end;
michael@0 5082 }
michael@0 5083
michael@0 5084 if (cpi->oxcf.number_of_layers > 1)
michael@0 5085 {
michael@0 5086 int layer;
michael@0 5087
michael@0 5088 update_layer_contexts (cpi);
michael@0 5089
michael@0 5090 /* Restore layer specific context & set frame rate */
michael@0 5091 layer = cpi->oxcf.layer_id[
michael@0 5092 cpi->temporal_pattern_counter % cpi->oxcf.periodicity];
michael@0 5093 restore_layer_context (cpi, layer);
michael@0 5094 vp8_new_framerate(cpi, cpi->layer_context[layer].framerate);
michael@0 5095 }
michael@0 5096
michael@0 5097 if (cpi->compressor_speed == 2)
michael@0 5098 {
michael@0 5099 vpx_usec_timer_start(&tsctimer);
michael@0 5100 vpx_usec_timer_start(&ticktimer);
michael@0 5101 }
michael@0 5102
michael@0 5103 cpi->lf_zeromv_pct = (cpi->zeromv_count * 100)/cm->MBs;
michael@0 5104
michael@0 5105 #if CONFIG_REALTIME_ONLY & CONFIG_ONTHEFLY_BITPACKING
michael@0 5106 {
michael@0 5107 int i;
michael@0 5108 const int num_part = (1 << cm->multi_token_partition);
michael@0 5109 /* the available bytes in dest */
michael@0 5110 const unsigned long dest_size = dest_end - dest;
michael@0 5111 const int tok_part_buff_size = (dest_size * 9) / (10 * num_part);
michael@0 5112
michael@0 5113 unsigned char *dp = dest;
michael@0 5114
michael@0 5115 cpi->partition_d[0] = dp;
michael@0 5116 dp += dest_size/10; /* reserve 1/10 for control partition */
michael@0 5117 cpi->partition_d_end[0] = dp;
michael@0 5118
michael@0 5119 for(i = 0; i < num_part; i++)
michael@0 5120 {
michael@0 5121 cpi->partition_d[i + 1] = dp;
michael@0 5122 dp += tok_part_buff_size;
michael@0 5123 cpi->partition_d_end[i + 1] = dp;
michael@0 5124 }
michael@0 5125 }
michael@0 5126 #endif
michael@0 5127
michael@0 5128 /* start with a 0 size frame */
michael@0 5129 *size = 0;
michael@0 5130
michael@0 5131 /* Clear down mmx registers */
michael@0 5132 vp8_clear_system_state();
michael@0 5133
michael@0 5134 cm->frame_type = INTER_FRAME;
michael@0 5135 cm->frame_flags = *frame_flags;
michael@0 5136
michael@0 5137 #if 0
michael@0 5138
michael@0 5139 if (cm->refresh_alt_ref_frame)
michael@0 5140 {
michael@0 5141 cm->refresh_golden_frame = 0;
michael@0 5142 cm->refresh_last_frame = 0;
michael@0 5143 }
michael@0 5144 else
michael@0 5145 {
michael@0 5146 cm->refresh_golden_frame = 0;
michael@0 5147 cm->refresh_last_frame = 1;
michael@0 5148 }
michael@0 5149
michael@0 5150 #endif
michael@0 5151 /* find a free buffer for the new frame */
michael@0 5152 {
michael@0 5153 int i = 0;
michael@0 5154 for(; i < NUM_YV12_BUFFERS; i++)
michael@0 5155 {
michael@0 5156 if(!cm->yv12_fb[i].flags)
michael@0 5157 {
michael@0 5158 cm->new_fb_idx = i;
michael@0 5159 break;
michael@0 5160 }
michael@0 5161 }
michael@0 5162
michael@0 5163 assert(i < NUM_YV12_BUFFERS );
michael@0 5164 }
michael@0 5165 #if !(CONFIG_REALTIME_ONLY)
michael@0 5166
michael@0 5167 if (cpi->pass == 1)
michael@0 5168 {
michael@0 5169 Pass1Encode(cpi, size, dest, frame_flags);
michael@0 5170 }
michael@0 5171 else if (cpi->pass == 2)
michael@0 5172 {
michael@0 5173 Pass2Encode(cpi, size, dest, dest_end, frame_flags);
michael@0 5174 }
michael@0 5175 else
michael@0 5176 #endif
michael@0 5177 encode_frame_to_data_rate(cpi, size, dest, dest_end, frame_flags);
michael@0 5178
michael@0 5179 if (cpi->compressor_speed == 2)
michael@0 5180 {
michael@0 5181 unsigned int duration, duration2;
michael@0 5182 vpx_usec_timer_mark(&tsctimer);
michael@0 5183 vpx_usec_timer_mark(&ticktimer);
michael@0 5184
michael@0 5185 duration = (int)(vpx_usec_timer_elapsed(&ticktimer));
michael@0 5186 duration2 = (unsigned int)((double)duration / 2);
michael@0 5187
michael@0 5188 if (cm->frame_type != KEY_FRAME)
michael@0 5189 {
michael@0 5190 if (cpi->avg_encode_time == 0)
michael@0 5191 cpi->avg_encode_time = duration;
michael@0 5192 else
michael@0 5193 cpi->avg_encode_time = (7 * cpi->avg_encode_time + duration) >> 3;
michael@0 5194 }
michael@0 5195
michael@0 5196 if (duration2)
michael@0 5197 {
michael@0 5198 {
michael@0 5199
michael@0 5200 if (cpi->avg_pick_mode_time == 0)
michael@0 5201 cpi->avg_pick_mode_time = duration2;
michael@0 5202 else
michael@0 5203 cpi->avg_pick_mode_time = (7 * cpi->avg_pick_mode_time + duration2) >> 3;
michael@0 5204 }
michael@0 5205 }
michael@0 5206
michael@0 5207 }
michael@0 5208
michael@0 5209 if (cm->refresh_entropy_probs == 0)
michael@0 5210 {
michael@0 5211 vpx_memcpy(&cm->fc, &cm->lfc, sizeof(cm->fc));
michael@0 5212 }
michael@0 5213
michael@0 5214 /* Save the contexts separately for alt ref, gold and last. */
michael@0 5215 /* (TODO jbb -> Optimize this with pointers to avoid extra copies. ) */
michael@0 5216 if(cm->refresh_alt_ref_frame)
michael@0 5217 vpx_memcpy(&cpi->lfc_a, &cm->fc, sizeof(cm->fc));
michael@0 5218
michael@0 5219 if(cm->refresh_golden_frame)
michael@0 5220 vpx_memcpy(&cpi->lfc_g, &cm->fc, sizeof(cm->fc));
michael@0 5221
michael@0 5222 if(cm->refresh_last_frame)
michael@0 5223 vpx_memcpy(&cpi->lfc_n, &cm->fc, sizeof(cm->fc));
michael@0 5224
michael@0 5225 /* if its a dropped frame honor the requests on subsequent frames */
michael@0 5226 if (*size > 0)
michael@0 5227 {
michael@0 5228 cpi->droppable = !frame_is_reference(cpi);
michael@0 5229
michael@0 5230 /* return to normal state */
michael@0 5231 cm->refresh_entropy_probs = 1;
michael@0 5232 cm->refresh_alt_ref_frame = 0;
michael@0 5233 cm->refresh_golden_frame = 0;
michael@0 5234 cm->refresh_last_frame = 1;
michael@0 5235 cm->frame_type = INTER_FRAME;
michael@0 5236
michael@0 5237 }
michael@0 5238
michael@0 5239 /* Save layer specific state */
michael@0 5240 if (cpi->oxcf.number_of_layers > 1)
michael@0 5241 save_layer_context (cpi);
michael@0 5242
michael@0 5243 vpx_usec_timer_mark(&cmptimer);
michael@0 5244 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
michael@0 5245
michael@0 5246 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame)
michael@0 5247 {
michael@0 5248 generate_psnr_packet(cpi);
michael@0 5249 }
michael@0 5250
michael@0 5251 #if CONFIG_INTERNAL_STATS
michael@0 5252
michael@0 5253 if (cpi->pass != 1)
michael@0 5254 {
michael@0 5255 cpi->bytes += *size;
michael@0 5256
michael@0 5257 if (cm->show_frame)
michael@0 5258 {
michael@0 5259 cpi->common.show_frame_mi = cpi->common.mi;
michael@0 5260 cpi->count ++;
michael@0 5261
michael@0 5262 if (cpi->b_calculate_psnr)
michael@0 5263 {
michael@0 5264 uint64_t ye,ue,ve;
michael@0 5265 double frame_psnr;
michael@0 5266 YV12_BUFFER_CONFIG *orig = cpi->Source;
michael@0 5267 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
michael@0 5268 int y_samples = orig->y_height * orig->y_width ;
michael@0 5269 int uv_samples = orig->uv_height * orig->uv_width ;
michael@0 5270 int t_samples = y_samples + 2 * uv_samples;
michael@0 5271 double sq_error, sq_error2;
michael@0 5272
michael@0 5273 ye = calc_plane_error(orig->y_buffer, orig->y_stride,
michael@0 5274 recon->y_buffer, recon->y_stride, orig->y_width, orig->y_height);
michael@0 5275
michael@0 5276 ue = calc_plane_error(orig->u_buffer, orig->uv_stride,
michael@0 5277 recon->u_buffer, recon->uv_stride, orig->uv_width, orig->uv_height);
michael@0 5278
michael@0 5279 ve = calc_plane_error(orig->v_buffer, orig->uv_stride,
michael@0 5280 recon->v_buffer, recon->uv_stride, orig->uv_width, orig->uv_height);
michael@0 5281
michael@0 5282 sq_error = (double)(ye + ue + ve);
michael@0 5283
michael@0 5284 frame_psnr = vp8_mse2psnr(t_samples, 255.0, sq_error);
michael@0 5285
michael@0 5286 cpi->total_y += vp8_mse2psnr(y_samples, 255.0, (double)ye);
michael@0 5287 cpi->total_u += vp8_mse2psnr(uv_samples, 255.0, (double)ue);
michael@0 5288 cpi->total_v += vp8_mse2psnr(uv_samples, 255.0, (double)ve);
michael@0 5289 cpi->total_sq_error += sq_error;
michael@0 5290 cpi->total += frame_psnr;
michael@0 5291 #if CONFIG_POSTPROC
michael@0 5292 {
michael@0 5293 YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
michael@0 5294 double frame_psnr2, frame_ssim2 = 0;
michael@0 5295 double weight = 0;
michael@0 5296
michael@0 5297 vp8_deblock(cm, cm->frame_to_show, &cm->post_proc_buffer, cm->filter_level * 10 / 6, 1, 0);
michael@0 5298 vp8_clear_system_state();
michael@0 5299
michael@0 5300 ye = calc_plane_error(orig->y_buffer, orig->y_stride,
michael@0 5301 pp->y_buffer, pp->y_stride, orig->y_width, orig->y_height);
michael@0 5302
michael@0 5303 ue = calc_plane_error(orig->u_buffer, orig->uv_stride,
michael@0 5304 pp->u_buffer, pp->uv_stride, orig->uv_width, orig->uv_height);
michael@0 5305
michael@0 5306 ve = calc_plane_error(orig->v_buffer, orig->uv_stride,
michael@0 5307 pp->v_buffer, pp->uv_stride, orig->uv_width, orig->uv_height);
michael@0 5308
michael@0 5309 sq_error2 = (double)(ye + ue + ve);
michael@0 5310
michael@0 5311 frame_psnr2 = vp8_mse2psnr(t_samples, 255.0, sq_error2);
michael@0 5312
michael@0 5313 cpi->totalp_y += vp8_mse2psnr(y_samples,
michael@0 5314 255.0, (double)ye);
michael@0 5315 cpi->totalp_u += vp8_mse2psnr(uv_samples,
michael@0 5316 255.0, (double)ue);
michael@0 5317 cpi->totalp_v += vp8_mse2psnr(uv_samples,
michael@0 5318 255.0, (double)ve);
michael@0 5319 cpi->total_sq_error2 += sq_error2;
michael@0 5320 cpi->totalp += frame_psnr2;
michael@0 5321
michael@0 5322 frame_ssim2 = vp8_calc_ssim(cpi->Source,
michael@0 5323 &cm->post_proc_buffer, 1, &weight);
michael@0 5324
michael@0 5325 cpi->summed_quality += frame_ssim2 * weight;
michael@0 5326 cpi->summed_weights += weight;
michael@0 5327
michael@0 5328 if (cpi->oxcf.number_of_layers > 1)
michael@0 5329 {
michael@0 5330 unsigned int i;
michael@0 5331
michael@0 5332 for (i=cpi->current_layer;
michael@0 5333 i<cpi->oxcf.number_of_layers; i++)
michael@0 5334 {
michael@0 5335 cpi->frames_in_layer[i]++;
michael@0 5336
michael@0 5337 cpi->bytes_in_layer[i] += *size;
michael@0 5338 cpi->sum_psnr[i] += frame_psnr;
michael@0 5339 cpi->sum_psnr_p[i] += frame_psnr2;
michael@0 5340 cpi->total_error2[i] += sq_error;
michael@0 5341 cpi->total_error2_p[i] += sq_error2;
michael@0 5342 cpi->sum_ssim[i] += frame_ssim2 * weight;
michael@0 5343 cpi->sum_weights[i] += weight;
michael@0 5344 }
michael@0 5345 }
michael@0 5346 }
michael@0 5347 #endif
michael@0 5348 }
michael@0 5349
michael@0 5350 if (cpi->b_calculate_ssimg)
michael@0 5351 {
michael@0 5352 double y, u, v, frame_all;
michael@0 5353 frame_all = vp8_calc_ssimg(cpi->Source, cm->frame_to_show,
michael@0 5354 &y, &u, &v);
michael@0 5355
michael@0 5356 if (cpi->oxcf.number_of_layers > 1)
michael@0 5357 {
michael@0 5358 unsigned int i;
michael@0 5359
michael@0 5360 for (i=cpi->current_layer;
michael@0 5361 i<cpi->oxcf.number_of_layers; i++)
michael@0 5362 {
michael@0 5363 if (!cpi->b_calculate_psnr)
michael@0 5364 cpi->frames_in_layer[i]++;
michael@0 5365
michael@0 5366 cpi->total_ssimg_y_in_layer[i] += y;
michael@0 5367 cpi->total_ssimg_u_in_layer[i] += u;
michael@0 5368 cpi->total_ssimg_v_in_layer[i] += v;
michael@0 5369 cpi->total_ssimg_all_in_layer[i] += frame_all;
michael@0 5370 }
michael@0 5371 }
michael@0 5372 else
michael@0 5373 {
michael@0 5374 cpi->total_ssimg_y += y;
michael@0 5375 cpi->total_ssimg_u += u;
michael@0 5376 cpi->total_ssimg_v += v;
michael@0 5377 cpi->total_ssimg_all += frame_all;
michael@0 5378 }
michael@0 5379 }
michael@0 5380
michael@0 5381 }
michael@0 5382 }
michael@0 5383
michael@0 5384 #if 0
michael@0 5385
michael@0 5386 if (cpi->common.frame_type != 0 && cpi->common.base_qindex == cpi->oxcf.worst_allowed_q)
michael@0 5387 {
michael@0 5388 skiptruecount += cpi->skip_true_count;
michael@0 5389 skipfalsecount += cpi->skip_false_count;
michael@0 5390 }
michael@0 5391
michael@0 5392 #endif
michael@0 5393 #if 0
michael@0 5394
michael@0 5395 if (cpi->pass != 1)
michael@0 5396 {
michael@0 5397 FILE *f = fopen("skip.stt", "a");
michael@0 5398 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);
michael@0 5399
michael@0 5400 if (cpi->is_src_frame_alt_ref == 1)
michael@0 5401 fprintf(f, "skipcount: %4d framesize: %d\n", cpi->skip_true_count , *size);
michael@0 5402
michael@0 5403 fclose(f);
michael@0 5404 }
michael@0 5405
michael@0 5406 #endif
michael@0 5407 #endif
michael@0 5408
michael@0 5409 #if HAVE_NEON
michael@0 5410 #if CONFIG_RUNTIME_CPU_DETECT
michael@0 5411 if (cm->cpu_caps & HAS_NEON)
michael@0 5412 #endif
michael@0 5413 {
michael@0 5414 vp8_pop_neon(store_reg);
michael@0 5415 }
michael@0 5416 #endif
michael@0 5417
michael@0 5418 cpi->common.error.setjmp = 0;
michael@0 5419
michael@0 5420 return 0;
michael@0 5421 }
michael@0 5422
michael@0 5423 int vp8_get_preview_raw_frame(VP8_COMP *cpi, YV12_BUFFER_CONFIG *dest, vp8_ppflags_t *flags)
michael@0 5424 {
michael@0 5425 if (cpi->common.refresh_alt_ref_frame)
michael@0 5426 return -1;
michael@0 5427 else
michael@0 5428 {
michael@0 5429 int ret;
michael@0 5430
michael@0 5431 #if CONFIG_MULTITHREAD
michael@0 5432 if(cpi->b_lpf_running)
michael@0 5433 {
michael@0 5434 sem_wait(&cpi->h_event_end_lpf);
michael@0 5435 cpi->b_lpf_running = 0;
michael@0 5436 }
michael@0 5437 #endif
michael@0 5438
michael@0 5439 #if CONFIG_POSTPROC
michael@0 5440 cpi->common.show_frame_mi = cpi->common.mi;
michael@0 5441 ret = vp8_post_proc_frame(&cpi->common, dest, flags);
michael@0 5442 #else
michael@0 5443
michael@0 5444 if (cpi->common.frame_to_show)
michael@0 5445 {
michael@0 5446 *dest = *cpi->common.frame_to_show;
michael@0 5447 dest->y_width = cpi->common.Width;
michael@0 5448 dest->y_height = cpi->common.Height;
michael@0 5449 dest->uv_height = cpi->common.Height / 2;
michael@0 5450 ret = 0;
michael@0 5451 }
michael@0 5452 else
michael@0 5453 {
michael@0 5454 ret = -1;
michael@0 5455 }
michael@0 5456
michael@0 5457 #endif
michael@0 5458 vp8_clear_system_state();
michael@0 5459 return ret;
michael@0 5460 }
michael@0 5461 }
michael@0 5462
michael@0 5463 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])
michael@0 5464 {
michael@0 5465 signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
michael@0 5466 int internal_delta_q[MAX_MB_SEGMENTS];
michael@0 5467 const int range = 63;
michael@0 5468 int i;
michael@0 5469
michael@0 5470 // This method is currently incompatible with the cyclic refresh method
michael@0 5471 if ( cpi->cyclic_refresh_mode_enabled )
michael@0 5472 return -1;
michael@0 5473
michael@0 5474 // Check number of rows and columns match
michael@0 5475 if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols)
michael@0 5476 return -1;
michael@0 5477
michael@0 5478 // Range check the delta Q values and convert the external Q range values
michael@0 5479 // to internal ones.
michael@0 5480 if ( (abs(delta_q[0]) > range) || (abs(delta_q[1]) > range) ||
michael@0 5481 (abs(delta_q[2]) > range) || (abs(delta_q[3]) > range) )
michael@0 5482 return -1;
michael@0 5483
michael@0 5484 // Range check the delta lf values
michael@0 5485 if ( (abs(delta_lf[0]) > range) || (abs(delta_lf[1]) > range) ||
michael@0 5486 (abs(delta_lf[2]) > range) || (abs(delta_lf[3]) > range) )
michael@0 5487 return -1;
michael@0 5488
michael@0 5489 if (!map)
michael@0 5490 {
michael@0 5491 disable_segmentation(cpi);
michael@0 5492 return 0;
michael@0 5493 }
michael@0 5494
michael@0 5495 // Translate the external delta q values to internal values.
michael@0 5496 for ( i = 0; i < MAX_MB_SEGMENTS; i++ )
michael@0 5497 internal_delta_q[i] =
michael@0 5498 ( delta_q[i] >= 0 ) ? q_trans[delta_q[i]] : -q_trans[-delta_q[i]];
michael@0 5499
michael@0 5500 /* Set the segmentation Map */
michael@0 5501 set_segmentation_map(cpi, map);
michael@0 5502
michael@0 5503 /* Activate segmentation. */
michael@0 5504 enable_segmentation(cpi);
michael@0 5505
michael@0 5506 /* Set up the quant segment data */
michael@0 5507 feature_data[MB_LVL_ALT_Q][0] = internal_delta_q[0];
michael@0 5508 feature_data[MB_LVL_ALT_Q][1] = internal_delta_q[1];
michael@0 5509 feature_data[MB_LVL_ALT_Q][2] = internal_delta_q[2];
michael@0 5510 feature_data[MB_LVL_ALT_Q][3] = internal_delta_q[3];
michael@0 5511
michael@0 5512 /* Set up the loop segment data s */
michael@0 5513 feature_data[MB_LVL_ALT_LF][0] = delta_lf[0];
michael@0 5514 feature_data[MB_LVL_ALT_LF][1] = delta_lf[1];
michael@0 5515 feature_data[MB_LVL_ALT_LF][2] = delta_lf[2];
michael@0 5516 feature_data[MB_LVL_ALT_LF][3] = delta_lf[3];
michael@0 5517
michael@0 5518 cpi->segment_encode_breakout[0] = threshold[0];
michael@0 5519 cpi->segment_encode_breakout[1] = threshold[1];
michael@0 5520 cpi->segment_encode_breakout[2] = threshold[2];
michael@0 5521 cpi->segment_encode_breakout[3] = threshold[3];
michael@0 5522
michael@0 5523 /* Initialise the feature data structure */
michael@0 5524 set_segment_data(cpi, &feature_data[0][0], SEGMENT_DELTADATA);
michael@0 5525
michael@0 5526 return 0;
michael@0 5527 }
michael@0 5528
michael@0 5529 int vp8_set_active_map(VP8_COMP *cpi, unsigned char *map, unsigned int rows, unsigned int cols)
michael@0 5530 {
michael@0 5531 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols)
michael@0 5532 {
michael@0 5533 if (map)
michael@0 5534 {
michael@0 5535 vpx_memcpy(cpi->active_map, map, rows * cols);
michael@0 5536 cpi->active_map_enabled = 1;
michael@0 5537 }
michael@0 5538 else
michael@0 5539 cpi->active_map_enabled = 0;
michael@0 5540
michael@0 5541 return 0;
michael@0 5542 }
michael@0 5543 else
michael@0 5544 {
michael@0 5545 return -1 ;
michael@0 5546 }
michael@0 5547 }
michael@0 5548
michael@0 5549 int vp8_set_internal_size(VP8_COMP *cpi, VPX_SCALING horiz_mode, VPX_SCALING vert_mode)
michael@0 5550 {
michael@0 5551 if (horiz_mode <= ONETWO)
michael@0 5552 cpi->common.horiz_scale = horiz_mode;
michael@0 5553 else
michael@0 5554 return -1;
michael@0 5555
michael@0 5556 if (vert_mode <= ONETWO)
michael@0 5557 cpi->common.vert_scale = vert_mode;
michael@0 5558 else
michael@0 5559 return -1;
michael@0 5560
michael@0 5561 return 0;
michael@0 5562 }
michael@0 5563
michael@0 5564
michael@0 5565
michael@0 5566 int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest)
michael@0 5567 {
michael@0 5568 int i, j;
michael@0 5569 int Total = 0;
michael@0 5570
michael@0 5571 unsigned char *src = source->y_buffer;
michael@0 5572 unsigned char *dst = dest->y_buffer;
michael@0 5573
michael@0 5574 /* Loop through the Y plane raw and reconstruction data summing
michael@0 5575 * (square differences)
michael@0 5576 */
michael@0 5577 for (i = 0; i < source->y_height; i += 16)
michael@0 5578 {
michael@0 5579 for (j = 0; j < source->y_width; j += 16)
michael@0 5580 {
michael@0 5581 unsigned int sse;
michael@0 5582 Total += vp8_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride, &sse);
michael@0 5583 }
michael@0 5584
michael@0 5585 src += 16 * source->y_stride;
michael@0 5586 dst += 16 * dest->y_stride;
michael@0 5587 }
michael@0 5588
michael@0 5589 return Total;
michael@0 5590 }
michael@0 5591
michael@0 5592
michael@0 5593 int vp8_get_quantizer(VP8_COMP *cpi)
michael@0 5594 {
michael@0 5595 return cpi->common.base_qindex;
michael@0 5596 }

mercurial