Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 | #include <math.h> |
michael@0 | 12 | #include <stdio.h> |
michael@0 | 13 | #include <limits.h> |
michael@0 | 14 | |
michael@0 | 15 | #include "./vpx_config.h" |
michael@0 | 16 | #include "./vpx_scale_rtcd.h" |
michael@0 | 17 | |
michael@0 | 18 | #include "vp9/common/vp9_alloccommon.h" |
michael@0 | 19 | #include "vp9/common/vp9_filter.h" |
michael@0 | 20 | #include "vp9/common/vp9_idct.h" |
michael@0 | 21 | #if CONFIG_VP9_POSTPROC |
michael@0 | 22 | #include "vp9/common/vp9_postproc.h" |
michael@0 | 23 | #endif |
michael@0 | 24 | #include "vp9/common/vp9_reconinter.h" |
michael@0 | 25 | #include "vp9/common/vp9_systemdependent.h" |
michael@0 | 26 | #include "vp9/common/vp9_tile_common.h" |
michael@0 | 27 | #include "vp9/encoder/vp9_firstpass.h" |
michael@0 | 28 | #include "vp9/encoder/vp9_mbgraph.h" |
michael@0 | 29 | #include "vp9/encoder/vp9_onyx_int.h" |
michael@0 | 30 | #include "vp9/encoder/vp9_picklpf.h" |
michael@0 | 31 | #include "vp9/encoder/vp9_psnr.h" |
michael@0 | 32 | #include "vp9/encoder/vp9_ratectrl.h" |
michael@0 | 33 | #include "vp9/encoder/vp9_rdopt.h" |
michael@0 | 34 | #include "vp9/encoder/vp9_segmentation.h" |
michael@0 | 35 | #include "vp9/encoder/vp9_temporal_filter.h" |
michael@0 | 36 | #include "vp9/encoder/vp9_vaq.h" |
michael@0 | 37 | |
michael@0 | 38 | #include "vpx_ports/vpx_timer.h" |
michael@0 | 39 | |
michael@0 | 40 | |
michael@0 | 41 | extern void print_tree_update_probs(); |
michael@0 | 42 | |
michael@0 | 43 | static void set_default_lf_deltas(struct loopfilter *lf); |
michael@0 | 44 | |
michael@0 | 45 | #define DEFAULT_INTERP_FILTER SWITCHABLE |
michael@0 | 46 | |
michael@0 | 47 | #define SHARP_FILTER_QTHRESH 0 /* Q threshold for 8-tap sharp filter */ |
michael@0 | 48 | |
michael@0 | 49 | #define ALTREF_HIGH_PRECISION_MV 1 // Whether to use high precision mv |
michael@0 | 50 | // for altref computation. |
michael@0 | 51 | #define HIGH_PRECISION_MV_QTHRESH 200 // Q threshold for high precision |
michael@0 | 52 | // mv. Choose a very high value for |
michael@0 | 53 | // now so that HIGH_PRECISION is always |
michael@0 | 54 | // chosen. |
michael@0 | 55 | |
michael@0 | 56 | // Masks for partially or completely disabling split mode |
michael@0 | 57 | #define DISABLE_ALL_SPLIT 0x3F |
michael@0 | 58 | #define DISABLE_ALL_INTER_SPLIT 0x1F |
michael@0 | 59 | #define DISABLE_COMPOUND_SPLIT 0x18 |
michael@0 | 60 | #define LAST_AND_INTRA_SPLIT_ONLY 0x1E |
michael@0 | 61 | |
michael@0 | 62 | #if CONFIG_INTERNAL_STATS |
michael@0 | 63 | extern double vp9_calc_ssim(YV12_BUFFER_CONFIG *source, |
michael@0 | 64 | YV12_BUFFER_CONFIG *dest, int lumamask, |
michael@0 | 65 | double *weight); |
michael@0 | 66 | |
michael@0 | 67 | |
michael@0 | 68 | extern double vp9_calc_ssimg(YV12_BUFFER_CONFIG *source, |
michael@0 | 69 | YV12_BUFFER_CONFIG *dest, double *ssim_y, |
michael@0 | 70 | double *ssim_u, double *ssim_v); |
michael@0 | 71 | |
michael@0 | 72 | |
michael@0 | 73 | #endif |
michael@0 | 74 | |
michael@0 | 75 | // #define OUTPUT_YUV_REC |
michael@0 | 76 | |
michael@0 | 77 | #ifdef OUTPUT_YUV_SRC |
michael@0 | 78 | FILE *yuv_file; |
michael@0 | 79 | #endif |
michael@0 | 80 | #ifdef OUTPUT_YUV_REC |
michael@0 | 81 | FILE *yuv_rec_file; |
michael@0 | 82 | #endif |
michael@0 | 83 | |
michael@0 | 84 | #if 0 |
michael@0 | 85 | FILE *framepsnr; |
michael@0 | 86 | FILE *kf_list; |
michael@0 | 87 | FILE *keyfile; |
michael@0 | 88 | #endif |
michael@0 | 89 | |
michael@0 | 90 | |
michael@0 | 91 | #ifdef ENTROPY_STATS |
michael@0 | 92 | extern int intra_mode_stats[INTRA_MODES] |
michael@0 | 93 | [INTRA_MODES] |
michael@0 | 94 | [INTRA_MODES]; |
michael@0 | 95 | #endif |
michael@0 | 96 | |
michael@0 | 97 | #ifdef MODE_STATS |
michael@0 | 98 | extern void init_tx_count_stats(); |
michael@0 | 99 | extern void write_tx_count_stats(); |
michael@0 | 100 | extern void init_switchable_interp_stats(); |
michael@0 | 101 | extern void write_switchable_interp_stats(); |
michael@0 | 102 | #endif |
michael@0 | 103 | |
michael@0 | 104 | #ifdef SPEEDSTATS |
michael@0 | 105 | unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
michael@0 | 106 | 0, 0, 0}; |
michael@0 | 107 | #endif |
michael@0 | 108 | |
michael@0 | 109 | #if defined(SECTIONBITS_OUTPUT) |
michael@0 | 110 | extern unsigned __int64 Sectionbits[500]; |
michael@0 | 111 | #endif |
michael@0 | 112 | |
michael@0 | 113 | extern void vp9_init_quantizer(VP9_COMP *cpi); |
michael@0 | 114 | |
michael@0 | 115 | // Tables relating active max Q to active min Q |
michael@0 | 116 | static int kf_low_motion_minq[QINDEX_RANGE]; |
michael@0 | 117 | static int kf_high_motion_minq[QINDEX_RANGE]; |
michael@0 | 118 | static int gf_low_motion_minq[QINDEX_RANGE]; |
michael@0 | 119 | static int gf_high_motion_minq[QINDEX_RANGE]; |
michael@0 | 120 | static int inter_minq[QINDEX_RANGE]; |
michael@0 | 121 | static int afq_low_motion_minq[QINDEX_RANGE]; |
michael@0 | 122 | static int afq_high_motion_minq[QINDEX_RANGE]; |
michael@0 | 123 | |
michael@0 | 124 | static INLINE void Scale2Ratio(int mode, int *hr, int *hs) { |
michael@0 | 125 | switch (mode) { |
michael@0 | 126 | case NORMAL: |
michael@0 | 127 | *hr = 1; |
michael@0 | 128 | *hs = 1; |
michael@0 | 129 | break; |
michael@0 | 130 | case FOURFIVE: |
michael@0 | 131 | *hr = 4; |
michael@0 | 132 | *hs = 5; |
michael@0 | 133 | break; |
michael@0 | 134 | case THREEFIVE: |
michael@0 | 135 | *hr = 3; |
michael@0 | 136 | *hs = 5; |
michael@0 | 137 | break; |
michael@0 | 138 | case ONETWO: |
michael@0 | 139 | *hr = 1; |
michael@0 | 140 | *hs = 2; |
michael@0 | 141 | break; |
michael@0 | 142 | default: |
michael@0 | 143 | *hr = 1; |
michael@0 | 144 | *hs = 1; |
michael@0 | 145 | assert(0); |
michael@0 | 146 | break; |
michael@0 | 147 | } |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | // Functions to compute the active minq lookup table entries based on a |
michael@0 | 151 | // formulaic approach to facilitate easier adjustment of the Q tables. |
michael@0 | 152 | // The formulae were derived from computing a 3rd order polynomial best |
michael@0 | 153 | // fit to the original data (after plotting real maxq vs minq (not q index)) |
michael@0 | 154 | static int calculate_minq_index(double maxq, |
michael@0 | 155 | double x3, double x2, double x1, double c) { |
michael@0 | 156 | int i; |
michael@0 | 157 | const double minqtarget = MIN(((x3 * maxq + x2) * maxq + x1) * maxq + c, |
michael@0 | 158 | maxq); |
michael@0 | 159 | |
michael@0 | 160 | // Special case handling to deal with the step from q2.0 |
michael@0 | 161 | // down to lossless mode represented by q 1.0. |
michael@0 | 162 | if (minqtarget <= 2.0) |
michael@0 | 163 | return 0; |
michael@0 | 164 | |
michael@0 | 165 | for (i = 0; i < QINDEX_RANGE; i++) { |
michael@0 | 166 | if (minqtarget <= vp9_convert_qindex_to_q(i)) |
michael@0 | 167 | return i; |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | return QINDEX_RANGE - 1; |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | static void init_minq_luts(void) { |
michael@0 | 174 | int i; |
michael@0 | 175 | |
michael@0 | 176 | for (i = 0; i < QINDEX_RANGE; i++) { |
michael@0 | 177 | const double maxq = vp9_convert_qindex_to_q(i); |
michael@0 | 178 | |
michael@0 | 179 | |
michael@0 | 180 | kf_low_motion_minq[i] = calculate_minq_index(maxq, |
michael@0 | 181 | 0.000001, |
michael@0 | 182 | -0.0004, |
michael@0 | 183 | 0.15, |
michael@0 | 184 | 0.0); |
michael@0 | 185 | kf_high_motion_minq[i] = calculate_minq_index(maxq, |
michael@0 | 186 | 0.000002, |
michael@0 | 187 | -0.0012, |
michael@0 | 188 | 0.5, |
michael@0 | 189 | 0.0); |
michael@0 | 190 | |
michael@0 | 191 | gf_low_motion_minq[i] = calculate_minq_index(maxq, |
michael@0 | 192 | 0.0000015, |
michael@0 | 193 | -0.0009, |
michael@0 | 194 | 0.32, |
michael@0 | 195 | 0.0); |
michael@0 | 196 | gf_high_motion_minq[i] = calculate_minq_index(maxq, |
michael@0 | 197 | 0.0000021, |
michael@0 | 198 | -0.00125, |
michael@0 | 199 | 0.50, |
michael@0 | 200 | 0.0); |
michael@0 | 201 | inter_minq[i] = calculate_minq_index(maxq, |
michael@0 | 202 | 0.00000271, |
michael@0 | 203 | -0.00113, |
michael@0 | 204 | 0.75, |
michael@0 | 205 | 0.0); |
michael@0 | 206 | afq_low_motion_minq[i] = calculate_minq_index(maxq, |
michael@0 | 207 | 0.0000015, |
michael@0 | 208 | -0.0009, |
michael@0 | 209 | 0.33, |
michael@0 | 210 | 0.0); |
michael@0 | 211 | afq_high_motion_minq[i] = calculate_minq_index(maxq, |
michael@0 | 212 | 0.0000021, |
michael@0 | 213 | -0.00125, |
michael@0 | 214 | 0.55, |
michael@0 | 215 | 0.0); |
michael@0 | 216 | } |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | static int get_active_quality(int q, |
michael@0 | 220 | int gfu_boost, |
michael@0 | 221 | int low, |
michael@0 | 222 | int high, |
michael@0 | 223 | int *low_motion_minq, |
michael@0 | 224 | int *high_motion_minq) { |
michael@0 | 225 | int active_best_quality; |
michael@0 | 226 | if (gfu_boost > high) { |
michael@0 | 227 | active_best_quality = low_motion_minq[q]; |
michael@0 | 228 | } else if (gfu_boost < low) { |
michael@0 | 229 | active_best_quality = high_motion_minq[q]; |
michael@0 | 230 | } else { |
michael@0 | 231 | const int gap = high - low; |
michael@0 | 232 | const int offset = high - gfu_boost; |
michael@0 | 233 | const int qdiff = high_motion_minq[q] - low_motion_minq[q]; |
michael@0 | 234 | const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap; |
michael@0 | 235 | active_best_quality = low_motion_minq[q] + adjustment; |
michael@0 | 236 | } |
michael@0 | 237 | return active_best_quality; |
michael@0 | 238 | } |
michael@0 | 239 | |
michael@0 | 240 | static void set_mvcost(VP9_COMP *cpi) { |
michael@0 | 241 | MACROBLOCK *const mb = &cpi->mb; |
michael@0 | 242 | if (cpi->common.allow_high_precision_mv) { |
michael@0 | 243 | mb->mvcost = mb->nmvcost_hp; |
michael@0 | 244 | mb->mvsadcost = mb->nmvsadcost_hp; |
michael@0 | 245 | } else { |
michael@0 | 246 | mb->mvcost = mb->nmvcost; |
michael@0 | 247 | mb->mvsadcost = mb->nmvsadcost; |
michael@0 | 248 | } |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | void vp9_initialize_enc() { |
michael@0 | 252 | static int init_done = 0; |
michael@0 | 253 | |
michael@0 | 254 | if (!init_done) { |
michael@0 | 255 | vp9_initialize_common(); |
michael@0 | 256 | vp9_tokenize_initialize(); |
michael@0 | 257 | vp9_init_quant_tables(); |
michael@0 | 258 | vp9_init_me_luts(); |
michael@0 | 259 | init_minq_luts(); |
michael@0 | 260 | // init_base_skip_probs(); |
michael@0 | 261 | init_done = 1; |
michael@0 | 262 | } |
michael@0 | 263 | } |
michael@0 | 264 | |
michael@0 | 265 | static void setup_features(VP9_COMMON *cm) { |
michael@0 | 266 | struct loopfilter *const lf = &cm->lf; |
michael@0 | 267 | struct segmentation *const seg = &cm->seg; |
michael@0 | 268 | |
michael@0 | 269 | // Set up default state for MB feature flags |
michael@0 | 270 | seg->enabled = 0; |
michael@0 | 271 | |
michael@0 | 272 | seg->update_map = 0; |
michael@0 | 273 | seg->update_data = 0; |
michael@0 | 274 | vpx_memset(seg->tree_probs, 255, sizeof(seg->tree_probs)); |
michael@0 | 275 | |
michael@0 | 276 | vp9_clearall_segfeatures(seg); |
michael@0 | 277 | |
michael@0 | 278 | lf->mode_ref_delta_enabled = 0; |
michael@0 | 279 | lf->mode_ref_delta_update = 0; |
michael@0 | 280 | vp9_zero(lf->ref_deltas); |
michael@0 | 281 | vp9_zero(lf->mode_deltas); |
michael@0 | 282 | vp9_zero(lf->last_ref_deltas); |
michael@0 | 283 | vp9_zero(lf->last_mode_deltas); |
michael@0 | 284 | |
michael@0 | 285 | set_default_lf_deltas(lf); |
michael@0 | 286 | } |
michael@0 | 287 | |
michael@0 | 288 | static void dealloc_compressor_data(VP9_COMP *cpi) { |
michael@0 | 289 | // Delete sementation map |
michael@0 | 290 | vpx_free(cpi->segmentation_map); |
michael@0 | 291 | cpi->segmentation_map = 0; |
michael@0 | 292 | vpx_free(cpi->common.last_frame_seg_map); |
michael@0 | 293 | cpi->common.last_frame_seg_map = 0; |
michael@0 | 294 | vpx_free(cpi->coding_context.last_frame_seg_map_copy); |
michael@0 | 295 | cpi->coding_context.last_frame_seg_map_copy = 0; |
michael@0 | 296 | |
michael@0 | 297 | vpx_free(cpi->active_map); |
michael@0 | 298 | cpi->active_map = 0; |
michael@0 | 299 | |
michael@0 | 300 | vp9_free_frame_buffers(&cpi->common); |
michael@0 | 301 | |
michael@0 | 302 | vp9_free_frame_buffer(&cpi->last_frame_uf); |
michael@0 | 303 | vp9_free_frame_buffer(&cpi->scaled_source); |
michael@0 | 304 | vp9_free_frame_buffer(&cpi->alt_ref_buffer); |
michael@0 | 305 | vp9_lookahead_destroy(cpi->lookahead); |
michael@0 | 306 | |
michael@0 | 307 | vpx_free(cpi->tok); |
michael@0 | 308 | cpi->tok = 0; |
michael@0 | 309 | |
michael@0 | 310 | // Activity mask based per mb zbin adjustments |
michael@0 | 311 | vpx_free(cpi->mb_activity_map); |
michael@0 | 312 | cpi->mb_activity_map = 0; |
michael@0 | 313 | vpx_free(cpi->mb_norm_activity_map); |
michael@0 | 314 | cpi->mb_norm_activity_map = 0; |
michael@0 | 315 | |
michael@0 | 316 | vpx_free(cpi->above_context[0]); |
michael@0 | 317 | cpi->above_context[0] = NULL; |
michael@0 | 318 | |
michael@0 | 319 | vpx_free(cpi->above_seg_context); |
michael@0 | 320 | cpi->above_seg_context = NULL; |
michael@0 | 321 | } |
michael@0 | 322 | |
michael@0 | 323 | // Computes a q delta (in "q index" terms) to get from a starting q value |
michael@0 | 324 | // to a target value |
michael@0 | 325 | // target q value |
michael@0 | 326 | int vp9_compute_qdelta(VP9_COMP *cpi, double qstart, double qtarget) { |
michael@0 | 327 | int i; |
michael@0 | 328 | int start_index = cpi->worst_quality; |
michael@0 | 329 | int target_index = cpi->worst_quality; |
michael@0 | 330 | |
michael@0 | 331 | // Convert the average q value to an index. |
michael@0 | 332 | for (i = cpi->best_quality; i < cpi->worst_quality; i++) { |
michael@0 | 333 | start_index = i; |
michael@0 | 334 | if (vp9_convert_qindex_to_q(i) >= qstart) |
michael@0 | 335 | break; |
michael@0 | 336 | } |
michael@0 | 337 | |
michael@0 | 338 | // Convert the q target to an index |
michael@0 | 339 | for (i = cpi->best_quality; i < cpi->worst_quality; i++) { |
michael@0 | 340 | target_index = i; |
michael@0 | 341 | if (vp9_convert_qindex_to_q(i) >= qtarget) |
michael@0 | 342 | break; |
michael@0 | 343 | } |
michael@0 | 344 | |
michael@0 | 345 | return target_index - start_index; |
michael@0 | 346 | } |
michael@0 | 347 | |
michael@0 | 348 | static void configure_static_seg_features(VP9_COMP *cpi) { |
michael@0 | 349 | VP9_COMMON *cm = &cpi->common; |
michael@0 | 350 | struct segmentation *seg = &cm->seg; |
michael@0 | 351 | |
michael@0 | 352 | int high_q = (int)(cpi->avg_q > 48.0); |
michael@0 | 353 | int qi_delta; |
michael@0 | 354 | |
michael@0 | 355 | // Disable and clear down for KF |
michael@0 | 356 | if (cm->frame_type == KEY_FRAME) { |
michael@0 | 357 | // Clear down the global segmentation map |
michael@0 | 358 | vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols); |
michael@0 | 359 | seg->update_map = 0; |
michael@0 | 360 | seg->update_data = 0; |
michael@0 | 361 | cpi->static_mb_pct = 0; |
michael@0 | 362 | |
michael@0 | 363 | // Disable segmentation |
michael@0 | 364 | vp9_disable_segmentation((VP9_PTR)cpi); |
michael@0 | 365 | |
michael@0 | 366 | // Clear down the segment features. |
michael@0 | 367 | vp9_clearall_segfeatures(seg); |
michael@0 | 368 | } else if (cpi->refresh_alt_ref_frame) { |
michael@0 | 369 | // If this is an alt ref frame |
michael@0 | 370 | // Clear down the global segmentation map |
michael@0 | 371 | vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols); |
michael@0 | 372 | seg->update_map = 0; |
michael@0 | 373 | seg->update_data = 0; |
michael@0 | 374 | cpi->static_mb_pct = 0; |
michael@0 | 375 | |
michael@0 | 376 | // Disable segmentation and individual segment features by default |
michael@0 | 377 | vp9_disable_segmentation((VP9_PTR)cpi); |
michael@0 | 378 | vp9_clearall_segfeatures(seg); |
michael@0 | 379 | |
michael@0 | 380 | // Scan frames from current to arf frame. |
michael@0 | 381 | // This function re-enables segmentation if appropriate. |
michael@0 | 382 | vp9_update_mbgraph_stats(cpi); |
michael@0 | 383 | |
michael@0 | 384 | // If segmentation was enabled set those features needed for the |
michael@0 | 385 | // arf itself. |
michael@0 | 386 | if (seg->enabled) { |
michael@0 | 387 | seg->update_map = 1; |
michael@0 | 388 | seg->update_data = 1; |
michael@0 | 389 | |
michael@0 | 390 | qi_delta = vp9_compute_qdelta(cpi, cpi->avg_q, (cpi->avg_q * 0.875)); |
michael@0 | 391 | vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, (qi_delta - 2)); |
michael@0 | 392 | vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2); |
michael@0 | 393 | |
michael@0 | 394 | vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q); |
michael@0 | 395 | vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF); |
michael@0 | 396 | |
michael@0 | 397 | // Where relevant assume segment data is delta data |
michael@0 | 398 | seg->abs_delta = SEGMENT_DELTADATA; |
michael@0 | 399 | } |
michael@0 | 400 | } else if (seg->enabled) { |
michael@0 | 401 | // All other frames if segmentation has been enabled |
michael@0 | 402 | |
michael@0 | 403 | // First normal frame in a valid gf or alt ref group |
michael@0 | 404 | if (cpi->frames_since_golden == 0) { |
michael@0 | 405 | // Set up segment features for normal frames in an arf group |
michael@0 | 406 | if (cpi->source_alt_ref_active) { |
michael@0 | 407 | seg->update_map = 0; |
michael@0 | 408 | seg->update_data = 1; |
michael@0 | 409 | seg->abs_delta = SEGMENT_DELTADATA; |
michael@0 | 410 | |
michael@0 | 411 | qi_delta = vp9_compute_qdelta(cpi, cpi->avg_q, |
michael@0 | 412 | (cpi->avg_q * 1.125)); |
michael@0 | 413 | vp9_set_segdata(seg, 1, SEG_LVL_ALT_Q, (qi_delta + 2)); |
michael@0 | 414 | vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_Q); |
michael@0 | 415 | |
michael@0 | 416 | vp9_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2); |
michael@0 | 417 | vp9_enable_segfeature(seg, 1, SEG_LVL_ALT_LF); |
michael@0 | 418 | |
michael@0 | 419 | // Segment coding disabled for compred testing |
michael@0 | 420 | if (high_q || (cpi->static_mb_pct == 100)) { |
michael@0 | 421 | vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME); |
michael@0 | 422 | vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME); |
michael@0 | 423 | vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP); |
michael@0 | 424 | } |
michael@0 | 425 | } else { |
michael@0 | 426 | // Disable segmentation and clear down features if alt ref |
michael@0 | 427 | // is not active for this group |
michael@0 | 428 | |
michael@0 | 429 | vp9_disable_segmentation((VP9_PTR)cpi); |
michael@0 | 430 | |
michael@0 | 431 | vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols); |
michael@0 | 432 | |
michael@0 | 433 | seg->update_map = 0; |
michael@0 | 434 | seg->update_data = 0; |
michael@0 | 435 | |
michael@0 | 436 | vp9_clearall_segfeatures(seg); |
michael@0 | 437 | } |
michael@0 | 438 | } else if (cpi->is_src_frame_alt_ref) { |
michael@0 | 439 | // Special case where we are coding over the top of a previous |
michael@0 | 440 | // alt ref frame. |
michael@0 | 441 | // Segment coding disabled for compred testing |
michael@0 | 442 | |
michael@0 | 443 | // Enable ref frame features for segment 0 as well |
michael@0 | 444 | vp9_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME); |
michael@0 | 445 | vp9_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME); |
michael@0 | 446 | |
michael@0 | 447 | // All mbs should use ALTREF_FRAME |
michael@0 | 448 | vp9_clear_segdata(seg, 0, SEG_LVL_REF_FRAME); |
michael@0 | 449 | vp9_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME); |
michael@0 | 450 | vp9_clear_segdata(seg, 1, SEG_LVL_REF_FRAME); |
michael@0 | 451 | vp9_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME); |
michael@0 | 452 | |
michael@0 | 453 | // Skip all MBs if high Q (0,0 mv and skip coeffs) |
michael@0 | 454 | if (high_q) { |
michael@0 | 455 | vp9_enable_segfeature(seg, 0, SEG_LVL_SKIP); |
michael@0 | 456 | vp9_enable_segfeature(seg, 1, SEG_LVL_SKIP); |
michael@0 | 457 | } |
michael@0 | 458 | // Enable data update |
michael@0 | 459 | seg->update_data = 1; |
michael@0 | 460 | } else { |
michael@0 | 461 | // All other frames. |
michael@0 | 462 | |
michael@0 | 463 | // No updates.. leave things as they are. |
michael@0 | 464 | seg->update_map = 0; |
michael@0 | 465 | seg->update_data = 0; |
michael@0 | 466 | } |
michael@0 | 467 | } |
michael@0 | 468 | } |
michael@0 | 469 | |
michael@0 | 470 | #ifdef ENTROPY_STATS |
michael@0 | 471 | void vp9_update_mode_context_stats(VP9_COMP *cpi) { |
michael@0 | 472 | VP9_COMMON *cm = &cpi->common; |
michael@0 | 473 | int i, j; |
michael@0 | 474 | unsigned int (*inter_mode_counts)[INTER_MODES - 1][2] = |
michael@0 | 475 | cm->fc.inter_mode_counts; |
michael@0 | 476 | int64_t (*mv_ref_stats)[INTER_MODES - 1][2] = cpi->mv_ref_stats; |
michael@0 | 477 | FILE *f; |
michael@0 | 478 | |
michael@0 | 479 | // Read the past stats counters |
michael@0 | 480 | f = fopen("mode_context.bin", "rb"); |
michael@0 | 481 | if (!f) { |
michael@0 | 482 | vpx_memset(cpi->mv_ref_stats, 0, sizeof(cpi->mv_ref_stats)); |
michael@0 | 483 | } else { |
michael@0 | 484 | fread(cpi->mv_ref_stats, sizeof(cpi->mv_ref_stats), 1, f); |
michael@0 | 485 | fclose(f); |
michael@0 | 486 | } |
michael@0 | 487 | |
michael@0 | 488 | // Add in the values for this frame |
michael@0 | 489 | for (i = 0; i < INTER_MODE_CONTEXTS; i++) { |
michael@0 | 490 | for (j = 0; j < INTER_MODES - 1; j++) { |
michael@0 | 491 | mv_ref_stats[i][j][0] += (int64_t)inter_mode_counts[i][j][0]; |
michael@0 | 492 | mv_ref_stats[i][j][1] += (int64_t)inter_mode_counts[i][j][1]; |
michael@0 | 493 | } |
michael@0 | 494 | } |
michael@0 | 495 | |
michael@0 | 496 | // Write back the accumulated stats |
michael@0 | 497 | f = fopen("mode_context.bin", "wb"); |
michael@0 | 498 | fwrite(cpi->mv_ref_stats, sizeof(cpi->mv_ref_stats), 1, f); |
michael@0 | 499 | fclose(f); |
michael@0 | 500 | } |
michael@0 | 501 | |
michael@0 | 502 | void print_mode_context(VP9_COMP *cpi) { |
michael@0 | 503 | FILE *f = fopen("vp9_modecont.c", "a"); |
michael@0 | 504 | int i, j; |
michael@0 | 505 | |
michael@0 | 506 | fprintf(f, "#include \"vp9_entropy.h\"\n"); |
michael@0 | 507 | fprintf( |
michael@0 | 508 | f, |
michael@0 | 509 | "const int inter_mode_probs[INTER_MODE_CONTEXTS][INTER_MODES - 1] ="); |
michael@0 | 510 | fprintf(f, "{\n"); |
michael@0 | 511 | for (j = 0; j < INTER_MODE_CONTEXTS; j++) { |
michael@0 | 512 | fprintf(f, " {/* %d */ ", j); |
michael@0 | 513 | fprintf(f, " "); |
michael@0 | 514 | for (i = 0; i < INTER_MODES - 1; i++) { |
michael@0 | 515 | int this_prob; |
michael@0 | 516 | int64_t count = cpi->mv_ref_stats[j][i][0] + cpi->mv_ref_stats[j][i][1]; |
michael@0 | 517 | if (count) |
michael@0 | 518 | this_prob = ((cpi->mv_ref_stats[j][i][0] * 256) + (count >> 1)) / count; |
michael@0 | 519 | else |
michael@0 | 520 | this_prob = 128; |
michael@0 | 521 | |
michael@0 | 522 | // context probs |
michael@0 | 523 | fprintf(f, "%5d, ", this_prob); |
michael@0 | 524 | } |
michael@0 | 525 | fprintf(f, " },\n"); |
michael@0 | 526 | } |
michael@0 | 527 | |
michael@0 | 528 | fprintf(f, "};\n"); |
michael@0 | 529 | fclose(f); |
michael@0 | 530 | } |
michael@0 | 531 | #endif // ENTROPY_STATS |
michael@0 | 532 | |
michael@0 | 533 | // DEBUG: Print out the segment id of each MB in the current frame. |
michael@0 | 534 | static void print_seg_map(VP9_COMP *cpi) { |
michael@0 | 535 | VP9_COMMON *cm = &cpi->common; |
michael@0 | 536 | int row, col; |
michael@0 | 537 | int map_index = 0; |
michael@0 | 538 | FILE *statsfile = fopen("segmap.stt", "a"); |
michael@0 | 539 | |
michael@0 | 540 | fprintf(statsfile, "%10d\n", cm->current_video_frame); |
michael@0 | 541 | |
michael@0 | 542 | for (row = 0; row < cpi->common.mi_rows; row++) { |
michael@0 | 543 | for (col = 0; col < cpi->common.mi_cols; col++) { |
michael@0 | 544 | fprintf(statsfile, "%10d", cpi->segmentation_map[map_index]); |
michael@0 | 545 | map_index++; |
michael@0 | 546 | } |
michael@0 | 547 | fprintf(statsfile, "\n"); |
michael@0 | 548 | } |
michael@0 | 549 | fprintf(statsfile, "\n"); |
michael@0 | 550 | |
michael@0 | 551 | fclose(statsfile); |
michael@0 | 552 | } |
michael@0 | 553 | |
michael@0 | 554 | static void update_reference_segmentation_map(VP9_COMP *cpi) { |
michael@0 | 555 | VP9_COMMON *const cm = &cpi->common; |
michael@0 | 556 | int row, col; |
michael@0 | 557 | MODE_INFO **mi_8x8, **mi_8x8_ptr = cm->mi_grid_visible; |
michael@0 | 558 | uint8_t *cache_ptr = cm->last_frame_seg_map, *cache; |
michael@0 | 559 | |
michael@0 | 560 | for (row = 0; row < cm->mi_rows; row++) { |
michael@0 | 561 | mi_8x8 = mi_8x8_ptr; |
michael@0 | 562 | cache = cache_ptr; |
michael@0 | 563 | for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++) |
michael@0 | 564 | cache[0] = mi_8x8[0]->mbmi.segment_id; |
michael@0 | 565 | mi_8x8_ptr += cm->mode_info_stride; |
michael@0 | 566 | cache_ptr += cm->mi_cols; |
michael@0 | 567 | } |
michael@0 | 568 | } |
michael@0 | 569 | |
michael@0 | 570 | static void set_default_lf_deltas(struct loopfilter *lf) { |
michael@0 | 571 | lf->mode_ref_delta_enabled = 1; |
michael@0 | 572 | lf->mode_ref_delta_update = 1; |
michael@0 | 573 | |
michael@0 | 574 | vp9_zero(lf->ref_deltas); |
michael@0 | 575 | vp9_zero(lf->mode_deltas); |
michael@0 | 576 | |
michael@0 | 577 | // Test of ref frame deltas |
michael@0 | 578 | lf->ref_deltas[INTRA_FRAME] = 2; |
michael@0 | 579 | lf->ref_deltas[LAST_FRAME] = 0; |
michael@0 | 580 | lf->ref_deltas[GOLDEN_FRAME] = -2; |
michael@0 | 581 | lf->ref_deltas[ALTREF_FRAME] = -2; |
michael@0 | 582 | |
michael@0 | 583 | lf->mode_deltas[0] = 0; // Zero |
michael@0 | 584 | lf->mode_deltas[1] = 0; // New mv |
michael@0 | 585 | } |
michael@0 | 586 | |
michael@0 | 587 | static void set_rd_speed_thresholds(VP9_COMP *cpi, int mode) { |
michael@0 | 588 | SPEED_FEATURES *sf = &cpi->sf; |
michael@0 | 589 | int i; |
michael@0 | 590 | |
michael@0 | 591 | // Set baseline threshold values |
michael@0 | 592 | for (i = 0; i < MAX_MODES; ++i) |
michael@0 | 593 | sf->thresh_mult[i] = mode == 0 ? -500 : 0; |
michael@0 | 594 | |
michael@0 | 595 | sf->thresh_mult[THR_NEARESTMV] = 0; |
michael@0 | 596 | sf->thresh_mult[THR_NEARESTG] = 0; |
michael@0 | 597 | sf->thresh_mult[THR_NEARESTA] = 0; |
michael@0 | 598 | |
michael@0 | 599 | sf->thresh_mult[THR_DC] += 1000; |
michael@0 | 600 | |
michael@0 | 601 | sf->thresh_mult[THR_NEWMV] += 1000; |
michael@0 | 602 | sf->thresh_mult[THR_NEWA] += 1000; |
michael@0 | 603 | sf->thresh_mult[THR_NEWG] += 1000; |
michael@0 | 604 | |
michael@0 | 605 | sf->thresh_mult[THR_NEARMV] += 1000; |
michael@0 | 606 | sf->thresh_mult[THR_NEARA] += 1000; |
michael@0 | 607 | sf->thresh_mult[THR_COMP_NEARESTLA] += 1000; |
michael@0 | 608 | sf->thresh_mult[THR_COMP_NEARESTGA] += 1000; |
michael@0 | 609 | |
michael@0 | 610 | sf->thresh_mult[THR_TM] += 1000; |
michael@0 | 611 | |
michael@0 | 612 | sf->thresh_mult[THR_COMP_NEARLA] += 1500; |
michael@0 | 613 | sf->thresh_mult[THR_COMP_NEWLA] += 2000; |
michael@0 | 614 | sf->thresh_mult[THR_NEARG] += 1000; |
michael@0 | 615 | sf->thresh_mult[THR_COMP_NEARGA] += 1500; |
michael@0 | 616 | sf->thresh_mult[THR_COMP_NEWGA] += 2000; |
michael@0 | 617 | |
michael@0 | 618 | sf->thresh_mult[THR_ZEROMV] += 2000; |
michael@0 | 619 | sf->thresh_mult[THR_ZEROG] += 2000; |
michael@0 | 620 | sf->thresh_mult[THR_ZEROA] += 2000; |
michael@0 | 621 | sf->thresh_mult[THR_COMP_ZEROLA] += 2500; |
michael@0 | 622 | sf->thresh_mult[THR_COMP_ZEROGA] += 2500; |
michael@0 | 623 | |
michael@0 | 624 | sf->thresh_mult[THR_H_PRED] += 2000; |
michael@0 | 625 | sf->thresh_mult[THR_V_PRED] += 2000; |
michael@0 | 626 | sf->thresh_mult[THR_D45_PRED ] += 2500; |
michael@0 | 627 | sf->thresh_mult[THR_D135_PRED] += 2500; |
michael@0 | 628 | sf->thresh_mult[THR_D117_PRED] += 2500; |
michael@0 | 629 | sf->thresh_mult[THR_D153_PRED] += 2500; |
michael@0 | 630 | sf->thresh_mult[THR_D207_PRED] += 2500; |
michael@0 | 631 | sf->thresh_mult[THR_D63_PRED] += 2500; |
michael@0 | 632 | |
michael@0 | 633 | /* disable frame modes if flags not set */ |
michael@0 | 634 | if (!(cpi->ref_frame_flags & VP9_LAST_FLAG)) { |
michael@0 | 635 | sf->thresh_mult[THR_NEWMV ] = INT_MAX; |
michael@0 | 636 | sf->thresh_mult[THR_NEARESTMV] = INT_MAX; |
michael@0 | 637 | sf->thresh_mult[THR_ZEROMV ] = INT_MAX; |
michael@0 | 638 | sf->thresh_mult[THR_NEARMV ] = INT_MAX; |
michael@0 | 639 | } |
michael@0 | 640 | if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG)) { |
michael@0 | 641 | sf->thresh_mult[THR_NEARESTG ] = INT_MAX; |
michael@0 | 642 | sf->thresh_mult[THR_ZEROG ] = INT_MAX; |
michael@0 | 643 | sf->thresh_mult[THR_NEARG ] = INT_MAX; |
michael@0 | 644 | sf->thresh_mult[THR_NEWG ] = INT_MAX; |
michael@0 | 645 | } |
michael@0 | 646 | if (!(cpi->ref_frame_flags & VP9_ALT_FLAG)) { |
michael@0 | 647 | sf->thresh_mult[THR_NEARESTA ] = INT_MAX; |
michael@0 | 648 | sf->thresh_mult[THR_ZEROA ] = INT_MAX; |
michael@0 | 649 | sf->thresh_mult[THR_NEARA ] = INT_MAX; |
michael@0 | 650 | sf->thresh_mult[THR_NEWA ] = INT_MAX; |
michael@0 | 651 | } |
michael@0 | 652 | |
michael@0 | 653 | if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) != |
michael@0 | 654 | (VP9_LAST_FLAG | VP9_ALT_FLAG)) { |
michael@0 | 655 | sf->thresh_mult[THR_COMP_ZEROLA ] = INT_MAX; |
michael@0 | 656 | sf->thresh_mult[THR_COMP_NEARESTLA] = INT_MAX; |
michael@0 | 657 | sf->thresh_mult[THR_COMP_NEARLA ] = INT_MAX; |
michael@0 | 658 | sf->thresh_mult[THR_COMP_NEWLA ] = INT_MAX; |
michael@0 | 659 | } |
michael@0 | 660 | if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) != |
michael@0 | 661 | (VP9_GOLD_FLAG | VP9_ALT_FLAG)) { |
michael@0 | 662 | sf->thresh_mult[THR_COMP_ZEROGA ] = INT_MAX; |
michael@0 | 663 | sf->thresh_mult[THR_COMP_NEARESTGA] = INT_MAX; |
michael@0 | 664 | sf->thresh_mult[THR_COMP_NEARGA ] = INT_MAX; |
michael@0 | 665 | sf->thresh_mult[THR_COMP_NEWGA ] = INT_MAX; |
michael@0 | 666 | } |
michael@0 | 667 | } |
michael@0 | 668 | |
michael@0 | 669 | static void set_rd_speed_thresholds_sub8x8(VP9_COMP *cpi, int mode) { |
michael@0 | 670 | SPEED_FEATURES *sf = &cpi->sf; |
michael@0 | 671 | int i; |
michael@0 | 672 | |
michael@0 | 673 | for (i = 0; i < MAX_REFS; ++i) |
michael@0 | 674 | sf->thresh_mult_sub8x8[i] = mode == 0 ? -500 : 0; |
michael@0 | 675 | |
michael@0 | 676 | sf->thresh_mult_sub8x8[THR_LAST] += 2500; |
michael@0 | 677 | sf->thresh_mult_sub8x8[THR_GOLD] += 2500; |
michael@0 | 678 | sf->thresh_mult_sub8x8[THR_ALTR] += 2500; |
michael@0 | 679 | sf->thresh_mult_sub8x8[THR_INTRA] += 2500; |
michael@0 | 680 | sf->thresh_mult_sub8x8[THR_COMP_LA] += 4500; |
michael@0 | 681 | sf->thresh_mult_sub8x8[THR_COMP_GA] += 4500; |
michael@0 | 682 | |
michael@0 | 683 | // Check for masked out split cases. |
michael@0 | 684 | for (i = 0; i < MAX_REFS; i++) { |
michael@0 | 685 | if (sf->disable_split_mask & (1 << i)) |
michael@0 | 686 | sf->thresh_mult_sub8x8[i] = INT_MAX; |
michael@0 | 687 | } |
michael@0 | 688 | |
michael@0 | 689 | // disable mode test if frame flag is not set |
michael@0 | 690 | if (!(cpi->ref_frame_flags & VP9_LAST_FLAG)) |
michael@0 | 691 | sf->thresh_mult_sub8x8[THR_LAST] = INT_MAX; |
michael@0 | 692 | if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG)) |
michael@0 | 693 | sf->thresh_mult_sub8x8[THR_GOLD] = INT_MAX; |
michael@0 | 694 | if (!(cpi->ref_frame_flags & VP9_ALT_FLAG)) |
michael@0 | 695 | sf->thresh_mult_sub8x8[THR_ALTR] = INT_MAX; |
michael@0 | 696 | if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) != |
michael@0 | 697 | (VP9_LAST_FLAG | VP9_ALT_FLAG)) |
michael@0 | 698 | sf->thresh_mult_sub8x8[THR_COMP_LA] = INT_MAX; |
michael@0 | 699 | if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) != |
michael@0 | 700 | (VP9_GOLD_FLAG | VP9_ALT_FLAG)) |
michael@0 | 701 | sf->thresh_mult_sub8x8[THR_COMP_GA] = INT_MAX; |
michael@0 | 702 | } |
michael@0 | 703 | |
michael@0 | 704 | void vp9_set_speed_features(VP9_COMP *cpi) { |
michael@0 | 705 | SPEED_FEATURES *sf = &cpi->sf; |
michael@0 | 706 | int mode = cpi->compressor_speed; |
michael@0 | 707 | int speed = cpi->speed; |
michael@0 | 708 | int i; |
michael@0 | 709 | |
michael@0 | 710 | // Only modes 0 and 1 supported for now in experimental code basae |
michael@0 | 711 | if (mode > 1) |
michael@0 | 712 | mode = 1; |
michael@0 | 713 | |
michael@0 | 714 | for (i = 0; i < MAX_MODES; ++i) |
michael@0 | 715 | cpi->mode_chosen_counts[i] = 0; |
michael@0 | 716 | |
michael@0 | 717 | // best quality defaults |
michael@0 | 718 | sf->RD = 1; |
michael@0 | 719 | sf->search_method = NSTEP; |
michael@0 | 720 | sf->auto_filter = 1; |
michael@0 | 721 | sf->recode_loop = 1; |
michael@0 | 722 | sf->subpel_search_method = SUBPEL_TREE; |
michael@0 | 723 | sf->subpel_iters_per_step = 2; |
michael@0 | 724 | sf->optimize_coefficients = !cpi->oxcf.lossless; |
michael@0 | 725 | sf->reduce_first_step_size = 0; |
michael@0 | 726 | sf->auto_mv_step_size = 0; |
michael@0 | 727 | sf->max_step_search_steps = MAX_MVSEARCH_STEPS; |
michael@0 | 728 | sf->comp_inter_joint_search_thresh = BLOCK_4X4; |
michael@0 | 729 | sf->adaptive_rd_thresh = 0; |
michael@0 | 730 | sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_OFF; |
michael@0 | 731 | sf->tx_size_search_method = USE_FULL_RD; |
michael@0 | 732 | sf->use_lp32x32fdct = 0; |
michael@0 | 733 | sf->adaptive_motion_search = 0; |
michael@0 | 734 | sf->use_avoid_tested_higherror = 0; |
michael@0 | 735 | sf->reference_masking = 0; |
michael@0 | 736 | sf->use_one_partition_size_always = 0; |
michael@0 | 737 | sf->less_rectangular_check = 0; |
michael@0 | 738 | sf->use_square_partition_only = 0; |
michael@0 | 739 | sf->auto_min_max_partition_size = 0; |
michael@0 | 740 | sf->max_partition_size = BLOCK_64X64; |
michael@0 | 741 | sf->min_partition_size = BLOCK_4X4; |
michael@0 | 742 | sf->adjust_partitioning_from_last_frame = 0; |
michael@0 | 743 | sf->last_partitioning_redo_frequency = 4; |
michael@0 | 744 | sf->disable_split_mask = 0; |
michael@0 | 745 | sf->mode_search_skip_flags = 0; |
michael@0 | 746 | sf->disable_split_var_thresh = 0; |
michael@0 | 747 | sf->disable_filter_search_var_thresh = 0; |
michael@0 | 748 | for (i = 0; i < TX_SIZES; i++) { |
michael@0 | 749 | sf->intra_y_mode_mask[i] = ALL_INTRA_MODES; |
michael@0 | 750 | sf->intra_uv_mode_mask[i] = ALL_INTRA_MODES; |
michael@0 | 751 | } |
michael@0 | 752 | sf->use_rd_breakout = 0; |
michael@0 | 753 | sf->skip_encode_sb = 0; |
michael@0 | 754 | sf->use_uv_intra_rd_estimate = 0; |
michael@0 | 755 | sf->use_fast_lpf_pick = 0; |
michael@0 | 756 | sf->use_fast_coef_updates = 0; |
michael@0 | 757 | sf->using_small_partition_info = 0; |
michael@0 | 758 | sf->mode_skip_start = MAX_MODES; // Mode index at which mode skip mask set |
michael@0 | 759 | |
michael@0 | 760 | #if CONFIG_MULTIPLE_ARF |
michael@0 | 761 | // Switch segmentation off. |
michael@0 | 762 | sf->static_segmentation = 0; |
michael@0 | 763 | #else |
michael@0 | 764 | sf->static_segmentation = 0; |
michael@0 | 765 | #endif |
michael@0 | 766 | |
michael@0 | 767 | switch (mode) { |
michael@0 | 768 | case 0: // This is the best quality mode. |
michael@0 | 769 | break; |
michael@0 | 770 | |
michael@0 | 771 | case 1: |
michael@0 | 772 | #if CONFIG_MULTIPLE_ARF |
michael@0 | 773 | // Switch segmentation off. |
michael@0 | 774 | sf->static_segmentation = 0; |
michael@0 | 775 | #else |
michael@0 | 776 | sf->static_segmentation = 0; |
michael@0 | 777 | #endif |
michael@0 | 778 | sf->use_avoid_tested_higherror = 1; |
michael@0 | 779 | sf->adaptive_rd_thresh = 1; |
michael@0 | 780 | sf->recode_loop = (speed < 1); |
michael@0 | 781 | |
michael@0 | 782 | if (speed == 1) { |
michael@0 | 783 | sf->use_square_partition_only = !frame_is_intra_only(&cpi->common); |
michael@0 | 784 | sf->less_rectangular_check = 1; |
michael@0 | 785 | sf->tx_size_search_method = frame_is_intra_only(&cpi->common) |
michael@0 | 786 | ? USE_FULL_RD : USE_LARGESTALL; |
michael@0 | 787 | |
michael@0 | 788 | if (MIN(cpi->common.width, cpi->common.height) >= 720) |
michael@0 | 789 | sf->disable_split_mask = cpi->common.show_frame ? |
michael@0 | 790 | DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT; |
michael@0 | 791 | else |
michael@0 | 792 | sf->disable_split_mask = DISABLE_COMPOUND_SPLIT; |
michael@0 | 793 | |
michael@0 | 794 | sf->use_rd_breakout = 1; |
michael@0 | 795 | sf->adaptive_motion_search = 1; |
michael@0 | 796 | sf->auto_mv_step_size = 1; |
michael@0 | 797 | sf->adaptive_rd_thresh = 2; |
michael@0 | 798 | sf->recode_loop = 2; |
michael@0 | 799 | sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V; |
michael@0 | 800 | sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V; |
michael@0 | 801 | sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V; |
michael@0 | 802 | } |
michael@0 | 803 | if (speed == 2) { |
michael@0 | 804 | sf->use_square_partition_only = !frame_is_intra_only(&cpi->common); |
michael@0 | 805 | sf->less_rectangular_check = 1; |
michael@0 | 806 | sf->tx_size_search_method = frame_is_intra_only(&cpi->common) |
michael@0 | 807 | ? USE_FULL_RD : USE_LARGESTALL; |
michael@0 | 808 | |
michael@0 | 809 | if (MIN(cpi->common.width, cpi->common.height) >= 720) |
michael@0 | 810 | sf->disable_split_mask = cpi->common.show_frame ? |
michael@0 | 811 | DISABLE_ALL_SPLIT : DISABLE_ALL_INTER_SPLIT; |
michael@0 | 812 | else |
michael@0 | 813 | sf->disable_split_mask = LAST_AND_INTRA_SPLIT_ONLY; |
michael@0 | 814 | |
michael@0 | 815 | |
michael@0 | 816 | sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH | |
michael@0 | 817 | FLAG_SKIP_INTRA_BESTINTER | |
michael@0 | 818 | FLAG_SKIP_COMP_BESTINTRA | |
michael@0 | 819 | FLAG_SKIP_INTRA_LOWVAR; |
michael@0 | 820 | |
michael@0 | 821 | sf->use_rd_breakout = 1; |
michael@0 | 822 | sf->adaptive_motion_search = 1; |
michael@0 | 823 | sf->auto_mv_step_size = 1; |
michael@0 | 824 | |
michael@0 | 825 | sf->disable_filter_search_var_thresh = 16; |
michael@0 | 826 | sf->comp_inter_joint_search_thresh = BLOCK_SIZES; |
michael@0 | 827 | |
michael@0 | 828 | sf->auto_min_max_partition_size = 1; |
michael@0 | 829 | sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_LOW_MOTION; |
michael@0 | 830 | sf->adjust_partitioning_from_last_frame = 1; |
michael@0 | 831 | sf->last_partitioning_redo_frequency = 3; |
michael@0 | 832 | |
michael@0 | 833 | sf->adaptive_rd_thresh = 2; |
michael@0 | 834 | sf->recode_loop = 2; |
michael@0 | 835 | sf->use_lp32x32fdct = 1; |
michael@0 | 836 | sf->mode_skip_start = 11; |
michael@0 | 837 | sf->intra_y_mode_mask[TX_32X32] = INTRA_DC_H_V; |
michael@0 | 838 | sf->intra_y_mode_mask[TX_16X16] = INTRA_DC_H_V; |
michael@0 | 839 | sf->intra_uv_mode_mask[TX_32X32] = INTRA_DC_H_V; |
michael@0 | 840 | sf->intra_uv_mode_mask[TX_16X16] = INTRA_DC_H_V; |
michael@0 | 841 | } |
michael@0 | 842 | if (speed == 3) { |
michael@0 | 843 | sf->use_square_partition_only = 1; |
michael@0 | 844 | sf->tx_size_search_method = USE_LARGESTALL; |
michael@0 | 845 | |
michael@0 | 846 | if (MIN(cpi->common.width, cpi->common.height) >= 720) |
michael@0 | 847 | sf->disable_split_mask = DISABLE_ALL_SPLIT; |
michael@0 | 848 | else |
michael@0 | 849 | sf->disable_split_mask = DISABLE_ALL_INTER_SPLIT; |
michael@0 | 850 | |
michael@0 | 851 | sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH | |
michael@0 | 852 | FLAG_SKIP_INTRA_BESTINTER | |
michael@0 | 853 | FLAG_SKIP_COMP_BESTINTRA | |
michael@0 | 854 | FLAG_SKIP_INTRA_LOWVAR; |
michael@0 | 855 | |
michael@0 | 856 | sf->use_rd_breakout = 1; |
michael@0 | 857 | sf->adaptive_motion_search = 1; |
michael@0 | 858 | sf->auto_mv_step_size = 1; |
michael@0 | 859 | |
michael@0 | 860 | sf->disable_filter_search_var_thresh = 16; |
michael@0 | 861 | sf->comp_inter_joint_search_thresh = BLOCK_SIZES; |
michael@0 | 862 | |
michael@0 | 863 | sf->auto_min_max_partition_size = 1; |
michael@0 | 864 | sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL; |
michael@0 | 865 | sf->adjust_partitioning_from_last_frame = 1; |
michael@0 | 866 | sf->last_partitioning_redo_frequency = 3; |
michael@0 | 867 | |
michael@0 | 868 | sf->use_uv_intra_rd_estimate = 1; |
michael@0 | 869 | sf->skip_encode_sb = 1; |
michael@0 | 870 | sf->use_lp32x32fdct = 1; |
michael@0 | 871 | sf->subpel_iters_per_step = 1; |
michael@0 | 872 | sf->use_fast_coef_updates = 2; |
michael@0 | 873 | |
michael@0 | 874 | sf->adaptive_rd_thresh = 4; |
michael@0 | 875 | sf->mode_skip_start = 6; |
michael@0 | 876 | } |
michael@0 | 877 | if (speed == 4) { |
michael@0 | 878 | sf->use_square_partition_only = 1; |
michael@0 | 879 | sf->tx_size_search_method = USE_LARGESTALL; |
michael@0 | 880 | sf->disable_split_mask = DISABLE_ALL_SPLIT; |
michael@0 | 881 | |
michael@0 | 882 | sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH | |
michael@0 | 883 | FLAG_SKIP_INTRA_BESTINTER | |
michael@0 | 884 | FLAG_SKIP_COMP_BESTINTRA | |
michael@0 | 885 | FLAG_SKIP_COMP_REFMISMATCH | |
michael@0 | 886 | FLAG_SKIP_INTRA_LOWVAR | |
michael@0 | 887 | FLAG_EARLY_TERMINATE; |
michael@0 | 888 | |
michael@0 | 889 | sf->use_rd_breakout = 1; |
michael@0 | 890 | sf->adaptive_motion_search = 1; |
michael@0 | 891 | sf->auto_mv_step_size = 1; |
michael@0 | 892 | |
michael@0 | 893 | sf->disable_filter_search_var_thresh = 16; |
michael@0 | 894 | sf->comp_inter_joint_search_thresh = BLOCK_SIZES; |
michael@0 | 895 | |
michael@0 | 896 | sf->auto_min_max_partition_size = 1; |
michael@0 | 897 | sf->use_lastframe_partitioning = LAST_FRAME_PARTITION_ALL; |
michael@0 | 898 | sf->adjust_partitioning_from_last_frame = 1; |
michael@0 | 899 | sf->last_partitioning_redo_frequency = 3; |
michael@0 | 900 | |
michael@0 | 901 | sf->use_uv_intra_rd_estimate = 1; |
michael@0 | 902 | sf->skip_encode_sb = 1; |
michael@0 | 903 | sf->use_lp32x32fdct = 1; |
michael@0 | 904 | sf->subpel_iters_per_step = 1; |
michael@0 | 905 | sf->use_fast_coef_updates = 2; |
michael@0 | 906 | |
michael@0 | 907 | sf->adaptive_rd_thresh = 4; |
michael@0 | 908 | sf->mode_skip_start = 6; |
michael@0 | 909 | |
michael@0 | 910 | /* sf->intra_y_mode_mask = INTRA_DC_ONLY; |
michael@0 | 911 | sf->intra_uv_mode_mask = INTRA_DC_ONLY; |
michael@0 | 912 | sf->search_method = BIGDIA; |
michael@0 | 913 | sf->disable_split_var_thresh = 64; |
michael@0 | 914 | sf->disable_filter_search_var_thresh = 64; */ |
michael@0 | 915 | } |
michael@0 | 916 | if (speed == 5) { |
michael@0 | 917 | sf->comp_inter_joint_search_thresh = BLOCK_SIZES; |
michael@0 | 918 | sf->use_one_partition_size_always = 1; |
michael@0 | 919 | sf->always_this_block_size = BLOCK_16X16; |
michael@0 | 920 | sf->tx_size_search_method = frame_is_intra_only(&cpi->common) ? |
michael@0 | 921 | USE_FULL_RD : USE_LARGESTALL; |
michael@0 | 922 | sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH | |
michael@0 | 923 | FLAG_SKIP_INTRA_BESTINTER | |
michael@0 | 924 | FLAG_SKIP_COMP_BESTINTRA | |
michael@0 | 925 | FLAG_SKIP_COMP_REFMISMATCH | |
michael@0 | 926 | FLAG_SKIP_INTRA_LOWVAR | |
michael@0 | 927 | FLAG_EARLY_TERMINATE; |
michael@0 | 928 | sf->use_rd_breakout = 1; |
michael@0 | 929 | sf->use_lp32x32fdct = 1; |
michael@0 | 930 | sf->optimize_coefficients = 0; |
michael@0 | 931 | sf->auto_mv_step_size = 1; |
michael@0 | 932 | // sf->reduce_first_step_size = 1; |
michael@0 | 933 | // sf->reference_masking = 1; |
michael@0 | 934 | |
michael@0 | 935 | sf->disable_split_mask = DISABLE_ALL_SPLIT; |
michael@0 | 936 | sf->search_method = HEX; |
michael@0 | 937 | sf->subpel_iters_per_step = 1; |
michael@0 | 938 | sf->disable_split_var_thresh = 64; |
michael@0 | 939 | sf->disable_filter_search_var_thresh = 96; |
michael@0 | 940 | for (i = 0; i < TX_SIZES; i++) { |
michael@0 | 941 | sf->intra_y_mode_mask[i] = INTRA_DC_ONLY; |
michael@0 | 942 | sf->intra_uv_mode_mask[i] = INTRA_DC_ONLY; |
michael@0 | 943 | } |
michael@0 | 944 | sf->use_fast_coef_updates = 2; |
michael@0 | 945 | sf->adaptive_rd_thresh = 4; |
michael@0 | 946 | sf->mode_skip_start = 6; |
michael@0 | 947 | } |
michael@0 | 948 | break; |
michael@0 | 949 | }; /* switch */ |
michael@0 | 950 | |
michael@0 | 951 | // Set rd thresholds based on mode and speed setting |
michael@0 | 952 | set_rd_speed_thresholds(cpi, mode); |
michael@0 | 953 | set_rd_speed_thresholds_sub8x8(cpi, mode); |
michael@0 | 954 | |
michael@0 | 955 | // Slow quant, dct and trellis not worthwhile for first pass |
michael@0 | 956 | // so make sure they are always turned off. |
michael@0 | 957 | if (cpi->pass == 1) { |
michael@0 | 958 | sf->optimize_coefficients = 0; |
michael@0 | 959 | } |
michael@0 | 960 | |
michael@0 | 961 | // No recode for 1 pass. |
michael@0 | 962 | if (cpi->pass == 0) { |
michael@0 | 963 | sf->recode_loop = 0; |
michael@0 | 964 | sf->optimize_coefficients = 0; |
michael@0 | 965 | } |
michael@0 | 966 | |
michael@0 | 967 | cpi->mb.fwd_txm4x4 = vp9_fdct4x4; |
michael@0 | 968 | if (cpi->oxcf.lossless || cpi->mb.e_mbd.lossless) { |
michael@0 | 969 | cpi->mb.fwd_txm4x4 = vp9_fwht4x4; |
michael@0 | 970 | } |
michael@0 | 971 | |
michael@0 | 972 | if (cpi->sf.subpel_search_method == SUBPEL_ITERATIVE) { |
michael@0 | 973 | cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_iterative; |
michael@0 | 974 | cpi->find_fractional_mv_step_comp = vp9_find_best_sub_pixel_comp_iterative; |
michael@0 | 975 | } else if (cpi->sf.subpel_search_method == SUBPEL_TREE) { |
michael@0 | 976 | cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_tree; |
michael@0 | 977 | cpi->find_fractional_mv_step_comp = vp9_find_best_sub_pixel_comp_tree; |
michael@0 | 978 | } |
michael@0 | 979 | |
michael@0 | 980 | cpi->mb.optimize = cpi->sf.optimize_coefficients == 1 && cpi->pass != 1; |
michael@0 | 981 | |
michael@0 | 982 | #ifdef SPEEDSTATS |
michael@0 | 983 | frames_at_speed[cpi->speed]++; |
michael@0 | 984 | #endif |
michael@0 | 985 | } |
michael@0 | 986 | |
michael@0 | 987 | static void alloc_raw_frame_buffers(VP9_COMP *cpi) { |
michael@0 | 988 | VP9_COMMON *cm = &cpi->common; |
michael@0 | 989 | |
michael@0 | 990 | cpi->lookahead = vp9_lookahead_init(cpi->oxcf.width, cpi->oxcf.height, |
michael@0 | 991 | cm->subsampling_x, cm->subsampling_y, |
michael@0 | 992 | cpi->oxcf.lag_in_frames); |
michael@0 | 993 | if (!cpi->lookahead) |
michael@0 | 994 | vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, |
michael@0 | 995 | "Failed to allocate lag buffers"); |
michael@0 | 996 | |
michael@0 | 997 | if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer, |
michael@0 | 998 | cpi->oxcf.width, cpi->oxcf.height, |
michael@0 | 999 | cm->subsampling_x, cm->subsampling_y, |
michael@0 | 1000 | VP9BORDERINPIXELS)) |
michael@0 | 1001 | vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, |
michael@0 | 1002 | "Failed to allocate altref buffer"); |
michael@0 | 1003 | } |
michael@0 | 1004 | |
michael@0 | 1005 | void vp9_alloc_compressor_data(VP9_COMP *cpi) { |
michael@0 | 1006 | VP9_COMMON *cm = &cpi->common; |
michael@0 | 1007 | |
michael@0 | 1008 | if (vp9_alloc_frame_buffers(cm, cm->width, cm->height)) |
michael@0 | 1009 | vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, |
michael@0 | 1010 | "Failed to allocate frame buffers"); |
michael@0 | 1011 | |
michael@0 | 1012 | if (vp9_alloc_frame_buffer(&cpi->last_frame_uf, |
michael@0 | 1013 | cm->width, cm->height, |
michael@0 | 1014 | cm->subsampling_x, cm->subsampling_y, |
michael@0 | 1015 | VP9BORDERINPIXELS)) |
michael@0 | 1016 | vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, |
michael@0 | 1017 | "Failed to allocate last frame buffer"); |
michael@0 | 1018 | |
michael@0 | 1019 | if (vp9_alloc_frame_buffer(&cpi->scaled_source, |
michael@0 | 1020 | cm->width, cm->height, |
michael@0 | 1021 | cm->subsampling_x, cm->subsampling_y, |
michael@0 | 1022 | VP9BORDERINPIXELS)) |
michael@0 | 1023 | vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, |
michael@0 | 1024 | "Failed to allocate scaled source buffer"); |
michael@0 | 1025 | |
michael@0 | 1026 | vpx_free(cpi->tok); |
michael@0 | 1027 | |
michael@0 | 1028 | { |
michael@0 | 1029 | unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols); |
michael@0 | 1030 | |
michael@0 | 1031 | CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok))); |
michael@0 | 1032 | } |
michael@0 | 1033 | |
michael@0 | 1034 | vpx_free(cpi->mb_activity_map); |
michael@0 | 1035 | CHECK_MEM_ERROR(cm, cpi->mb_activity_map, |
michael@0 | 1036 | vpx_calloc(sizeof(unsigned int), |
michael@0 | 1037 | cm->mb_rows * cm->mb_cols)); |
michael@0 | 1038 | |
michael@0 | 1039 | vpx_free(cpi->mb_norm_activity_map); |
michael@0 | 1040 | CHECK_MEM_ERROR(cm, cpi->mb_norm_activity_map, |
michael@0 | 1041 | vpx_calloc(sizeof(unsigned int), |
michael@0 | 1042 | cm->mb_rows * cm->mb_cols)); |
michael@0 | 1043 | |
michael@0 | 1044 | // 2 contexts per 'mi unit', so that we have one context per 4x4 txfm |
michael@0 | 1045 | // block where mi unit size is 8x8. |
michael@0 | 1046 | vpx_free(cpi->above_context[0]); |
michael@0 | 1047 | CHECK_MEM_ERROR(cm, cpi->above_context[0], |
michael@0 | 1048 | vpx_calloc(2 * mi_cols_aligned_to_sb(cm->mi_cols) * |
michael@0 | 1049 | MAX_MB_PLANE, |
michael@0 | 1050 | sizeof(*cpi->above_context[0]))); |
michael@0 | 1051 | |
michael@0 | 1052 | vpx_free(cpi->above_seg_context); |
michael@0 | 1053 | CHECK_MEM_ERROR(cm, cpi->above_seg_context, |
michael@0 | 1054 | vpx_calloc(mi_cols_aligned_to_sb(cm->mi_cols), |
michael@0 | 1055 | sizeof(*cpi->above_seg_context))); |
michael@0 | 1056 | } |
michael@0 | 1057 | |
michael@0 | 1058 | |
michael@0 | 1059 | static void update_frame_size(VP9_COMP *cpi) { |
michael@0 | 1060 | VP9_COMMON *cm = &cpi->common; |
michael@0 | 1061 | |
michael@0 | 1062 | vp9_update_frame_size(cm); |
michael@0 | 1063 | |
michael@0 | 1064 | // Update size of buffers local to this frame |
michael@0 | 1065 | if (vp9_realloc_frame_buffer(&cpi->last_frame_uf, |
michael@0 | 1066 | cm->width, cm->height, |
michael@0 | 1067 | cm->subsampling_x, cm->subsampling_y, |
michael@0 | 1068 | VP9BORDERINPIXELS)) |
michael@0 | 1069 | vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, |
michael@0 | 1070 | "Failed to reallocate last frame buffer"); |
michael@0 | 1071 | |
michael@0 | 1072 | if (vp9_realloc_frame_buffer(&cpi->scaled_source, |
michael@0 | 1073 | cm->width, cm->height, |
michael@0 | 1074 | cm->subsampling_x, cm->subsampling_y, |
michael@0 | 1075 | VP9BORDERINPIXELS)) |
michael@0 | 1076 | vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR, |
michael@0 | 1077 | "Failed to reallocate scaled source buffer"); |
michael@0 | 1078 | |
michael@0 | 1079 | { |
michael@0 | 1080 | int y_stride = cpi->scaled_source.y_stride; |
michael@0 | 1081 | |
michael@0 | 1082 | if (cpi->sf.search_method == NSTEP) { |
michael@0 | 1083 | vp9_init3smotion_compensation(&cpi->mb, y_stride); |
michael@0 | 1084 | } else if (cpi->sf.search_method == DIAMOND) { |
michael@0 | 1085 | vp9_init_dsmotion_compensation(&cpi->mb, y_stride); |
michael@0 | 1086 | } |
michael@0 | 1087 | } |
michael@0 | 1088 | |
michael@0 | 1089 | { |
michael@0 | 1090 | int i; |
michael@0 | 1091 | for (i = 1; i < MAX_MB_PLANE; ++i) { |
michael@0 | 1092 | cpi->above_context[i] = cpi->above_context[0] + |
michael@0 | 1093 | i * sizeof(*cpi->above_context[0]) * 2 * |
michael@0 | 1094 | mi_cols_aligned_to_sb(cm->mi_cols); |
michael@0 | 1095 | } |
michael@0 | 1096 | } |
michael@0 | 1097 | } |
michael@0 | 1098 | |
michael@0 | 1099 | |
michael@0 | 1100 | // Table that converts 0-63 Q range values passed in outside to the Qindex |
michael@0 | 1101 | // range used internally. |
michael@0 | 1102 | static const int q_trans[] = { |
michael@0 | 1103 | 0, 4, 8, 12, 16, 20, 24, 28, |
michael@0 | 1104 | 32, 36, 40, 44, 48, 52, 56, 60, |
michael@0 | 1105 | 64, 68, 72, 76, 80, 84, 88, 92, |
michael@0 | 1106 | 96, 100, 104, 108, 112, 116, 120, 124, |
michael@0 | 1107 | 128, 132, 136, 140, 144, 148, 152, 156, |
michael@0 | 1108 | 160, 164, 168, 172, 176, 180, 184, 188, |
michael@0 | 1109 | 192, 196, 200, 204, 208, 212, 216, 220, |
michael@0 | 1110 | 224, 228, 232, 236, 240, 244, 249, 255, |
michael@0 | 1111 | }; |
michael@0 | 1112 | |
michael@0 | 1113 | int vp9_reverse_trans(int x) { |
michael@0 | 1114 | int i; |
michael@0 | 1115 | |
michael@0 | 1116 | for (i = 0; i < 64; i++) |
michael@0 | 1117 | if (q_trans[i] >= x) |
michael@0 | 1118 | return i; |
michael@0 | 1119 | |
michael@0 | 1120 | return 63; |
michael@0 | 1121 | }; |
michael@0 | 1122 | void vp9_new_framerate(VP9_COMP *cpi, double framerate) { |
michael@0 | 1123 | if (framerate < 0.1) |
michael@0 | 1124 | framerate = 30; |
michael@0 | 1125 | |
michael@0 | 1126 | cpi->oxcf.framerate = framerate; |
michael@0 | 1127 | cpi->output_framerate = cpi->oxcf.framerate; |
michael@0 | 1128 | cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth |
michael@0 | 1129 | / cpi->output_framerate); |
michael@0 | 1130 | cpi->av_per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth |
michael@0 | 1131 | / cpi->output_framerate); |
michael@0 | 1132 | cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth * |
michael@0 | 1133 | cpi->oxcf.two_pass_vbrmin_section / 100); |
michael@0 | 1134 | |
michael@0 | 1135 | |
michael@0 | 1136 | cpi->min_frame_bandwidth = MAX(cpi->min_frame_bandwidth, FRAME_OVERHEAD_BITS); |
michael@0 | 1137 | |
michael@0 | 1138 | // Set Maximum gf/arf interval |
michael@0 | 1139 | cpi->max_gf_interval = 16; |
michael@0 | 1140 | |
michael@0 | 1141 | // Extended interval for genuinely static scenes |
michael@0 | 1142 | cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1; |
michael@0 | 1143 | |
michael@0 | 1144 | // Special conditions when alt ref frame enabled in lagged compress mode |
michael@0 | 1145 | if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames) { |
michael@0 | 1146 | if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1) |
michael@0 | 1147 | cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1; |
michael@0 | 1148 | |
michael@0 | 1149 | if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1) |
michael@0 | 1150 | cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1; |
michael@0 | 1151 | } |
michael@0 | 1152 | |
michael@0 | 1153 | if (cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval) |
michael@0 | 1154 | cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval; |
michael@0 | 1155 | } |
michael@0 | 1156 | |
michael@0 | 1157 | static int64_t rescale(int val, int64_t num, int denom) { |
michael@0 | 1158 | int64_t llnum = num; |
michael@0 | 1159 | int64_t llden = denom; |
michael@0 | 1160 | int64_t llval = val; |
michael@0 | 1161 | |
michael@0 | 1162 | return (llval * llnum / llden); |
michael@0 | 1163 | } |
michael@0 | 1164 | |
michael@0 | 1165 | static void set_tile_limits(VP9_COMP *cpi) { |
michael@0 | 1166 | VP9_COMMON *const cm = &cpi->common; |
michael@0 | 1167 | |
michael@0 | 1168 | int min_log2_tile_cols, max_log2_tile_cols; |
michael@0 | 1169 | vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols); |
michael@0 | 1170 | |
michael@0 | 1171 | cm->log2_tile_cols = clamp(cpi->oxcf.tile_columns, |
michael@0 | 1172 | min_log2_tile_cols, max_log2_tile_cols); |
michael@0 | 1173 | cm->log2_tile_rows = cpi->oxcf.tile_rows; |
michael@0 | 1174 | } |
michael@0 | 1175 | |
michael@0 | 1176 | static void init_config(VP9_PTR ptr, VP9_CONFIG *oxcf) { |
michael@0 | 1177 | VP9_COMP *cpi = (VP9_COMP *)(ptr); |
michael@0 | 1178 | VP9_COMMON *const cm = &cpi->common; |
michael@0 | 1179 | int i; |
michael@0 | 1180 | |
michael@0 | 1181 | cpi->oxcf = *oxcf; |
michael@0 | 1182 | |
michael@0 | 1183 | cm->version = oxcf->version; |
michael@0 | 1184 | |
michael@0 | 1185 | cm->width = oxcf->width; |
michael@0 | 1186 | cm->height = oxcf->height; |
michael@0 | 1187 | cm->subsampling_x = 0; |
michael@0 | 1188 | cm->subsampling_y = 0; |
michael@0 | 1189 | vp9_alloc_compressor_data(cpi); |
michael@0 | 1190 | |
michael@0 | 1191 | // change includes all joint functionality |
michael@0 | 1192 | vp9_change_config(ptr, oxcf); |
michael@0 | 1193 | |
michael@0 | 1194 | // Initialize active best and worst q and average q values. |
michael@0 | 1195 | cpi->active_worst_quality = cpi->oxcf.worst_allowed_q; |
michael@0 | 1196 | cpi->active_best_quality = cpi->oxcf.best_allowed_q; |
michael@0 | 1197 | cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q; |
michael@0 | 1198 | |
michael@0 | 1199 | // Initialise the starting buffer levels |
michael@0 | 1200 | cpi->buffer_level = cpi->oxcf.starting_buffer_level; |
michael@0 | 1201 | cpi->bits_off_target = cpi->oxcf.starting_buffer_level; |
michael@0 | 1202 | |
michael@0 | 1203 | cpi->rolling_target_bits = cpi->av_per_frame_bandwidth; |
michael@0 | 1204 | cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth; |
michael@0 | 1205 | cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth; |
michael@0 | 1206 | cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth; |
michael@0 | 1207 | |
michael@0 | 1208 | cpi->total_actual_bits = 0; |
michael@0 | 1209 | cpi->total_target_vs_actual = 0; |
michael@0 | 1210 | |
michael@0 | 1211 | cpi->static_mb_pct = 0; |
michael@0 | 1212 | |
michael@0 | 1213 | cpi->lst_fb_idx = 0; |
michael@0 | 1214 | cpi->gld_fb_idx = 1; |
michael@0 | 1215 | cpi->alt_fb_idx = 2; |
michael@0 | 1216 | |
michael@0 | 1217 | cpi->current_layer = 0; |
michael@0 | 1218 | cpi->use_svc = 0; |
michael@0 | 1219 | |
michael@0 | 1220 | set_tile_limits(cpi); |
michael@0 | 1221 | |
michael@0 | 1222 | cpi->fixed_divide[0] = 0; |
michael@0 | 1223 | for (i = 1; i < 512; i++) |
michael@0 | 1224 | cpi->fixed_divide[i] = 0x80000 / i; |
michael@0 | 1225 | } |
michael@0 | 1226 | |
michael@0 | 1227 | |
michael@0 | 1228 | void vp9_change_config(VP9_PTR ptr, VP9_CONFIG *oxcf) { |
michael@0 | 1229 | VP9_COMP *cpi = (VP9_COMP *)(ptr); |
michael@0 | 1230 | VP9_COMMON *const cm = &cpi->common; |
michael@0 | 1231 | |
michael@0 | 1232 | if (!cpi || !oxcf) |
michael@0 | 1233 | return; |
michael@0 | 1234 | |
michael@0 | 1235 | if (cm->version != oxcf->version) { |
michael@0 | 1236 | cm->version = oxcf->version; |
michael@0 | 1237 | } |
michael@0 | 1238 | |
michael@0 | 1239 | cpi->oxcf = *oxcf; |
michael@0 | 1240 | |
michael@0 | 1241 | switch (cpi->oxcf.Mode) { |
michael@0 | 1242 | // Real time and one pass deprecated in test code base |
michael@0 | 1243 | case MODE_GOODQUALITY: |
michael@0 | 1244 | cpi->pass = 0; |
michael@0 | 1245 | cpi->compressor_speed = 2; |
michael@0 | 1246 | cpi->oxcf.cpu_used = clamp(cpi->oxcf.cpu_used, -5, 5); |
michael@0 | 1247 | break; |
michael@0 | 1248 | |
michael@0 | 1249 | case MODE_FIRSTPASS: |
michael@0 | 1250 | cpi->pass = 1; |
michael@0 | 1251 | cpi->compressor_speed = 1; |
michael@0 | 1252 | break; |
michael@0 | 1253 | |
michael@0 | 1254 | case MODE_SECONDPASS: |
michael@0 | 1255 | cpi->pass = 2; |
michael@0 | 1256 | cpi->compressor_speed = 1; |
michael@0 | 1257 | cpi->oxcf.cpu_used = clamp(cpi->oxcf.cpu_used, -5, 5); |
michael@0 | 1258 | break; |
michael@0 | 1259 | |
michael@0 | 1260 | case MODE_SECONDPASS_BEST: |
michael@0 | 1261 | cpi->pass = 2; |
michael@0 | 1262 | cpi->compressor_speed = 0; |
michael@0 | 1263 | break; |
michael@0 | 1264 | } |
michael@0 | 1265 | |
michael@0 | 1266 | cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q]; |
michael@0 | 1267 | cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q]; |
michael@0 | 1268 | cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level]; |
michael@0 | 1269 | |
michael@0 | 1270 | cpi->oxcf.lossless = oxcf->lossless; |
michael@0 | 1271 | cpi->mb.e_mbd.itxm_add = cpi->oxcf.lossless ? vp9_iwht4x4_add |
michael@0 | 1272 | : vp9_idct4x4_add; |
michael@0 | 1273 | cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL; |
michael@0 | 1274 | |
michael@0 | 1275 | cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG; |
michael@0 | 1276 | |
michael@0 | 1277 | // cpi->use_golden_frame_only = 0; |
michael@0 | 1278 | // cpi->use_last_frame_only = 0; |
michael@0 | 1279 | cpi->refresh_golden_frame = 0; |
michael@0 | 1280 | cpi->refresh_last_frame = 1; |
michael@0 | 1281 | cm->refresh_frame_context = 1; |
michael@0 | 1282 | cm->reset_frame_context = 0; |
michael@0 | 1283 | |
michael@0 | 1284 | setup_features(cm); |
michael@0 | 1285 | cpi->common.allow_high_precision_mv = 0; // Default mv precision |
michael@0 | 1286 | set_mvcost(cpi); |
michael@0 | 1287 | |
michael@0 | 1288 | { |
michael@0 | 1289 | int i; |
michael@0 | 1290 | |
michael@0 | 1291 | for (i = 0; i < MAX_SEGMENTS; i++) |
michael@0 | 1292 | cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout; |
michael@0 | 1293 | } |
michael@0 | 1294 | |
michael@0 | 1295 | // At the moment the first order values may not be > MAXQ |
michael@0 | 1296 | cpi->oxcf.fixed_q = MIN(cpi->oxcf.fixed_q, MAXQ); |
michael@0 | 1297 | |
michael@0 | 1298 | // local file playback mode == really big buffer |
michael@0 | 1299 | if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK) { |
michael@0 | 1300 | cpi->oxcf.starting_buffer_level = 60000; |
michael@0 | 1301 | cpi->oxcf.optimal_buffer_level = 60000; |
michael@0 | 1302 | cpi->oxcf.maximum_buffer_size = 240000; |
michael@0 | 1303 | } |
michael@0 | 1304 | |
michael@0 | 1305 | // Convert target bandwidth from Kbit/s to Bit/s |
michael@0 | 1306 | cpi->oxcf.target_bandwidth *= 1000; |
michael@0 | 1307 | |
michael@0 | 1308 | cpi->oxcf.starting_buffer_level = rescale(cpi->oxcf.starting_buffer_level, |
michael@0 | 1309 | cpi->oxcf.target_bandwidth, 1000); |
michael@0 | 1310 | |
michael@0 | 1311 | // Set or reset optimal and maximum buffer levels. |
michael@0 | 1312 | if (cpi->oxcf.optimal_buffer_level == 0) |
michael@0 | 1313 | cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8; |
michael@0 | 1314 | else |
michael@0 | 1315 | cpi->oxcf.optimal_buffer_level = rescale(cpi->oxcf.optimal_buffer_level, |
michael@0 | 1316 | cpi->oxcf.target_bandwidth, 1000); |
michael@0 | 1317 | |
michael@0 | 1318 | if (cpi->oxcf.maximum_buffer_size == 0) |
michael@0 | 1319 | cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8; |
michael@0 | 1320 | else |
michael@0 | 1321 | cpi->oxcf.maximum_buffer_size = rescale(cpi->oxcf.maximum_buffer_size, |
michael@0 | 1322 | cpi->oxcf.target_bandwidth, 1000); |
michael@0 | 1323 | |
michael@0 | 1324 | // Set up frame rate and related parameters rate control values. |
michael@0 | 1325 | vp9_new_framerate(cpi, cpi->oxcf.framerate); |
michael@0 | 1326 | |
michael@0 | 1327 | // Set absolute upper and lower quality limits |
michael@0 | 1328 | cpi->worst_quality = cpi->oxcf.worst_allowed_q; |
michael@0 | 1329 | cpi->best_quality = cpi->oxcf.best_allowed_q; |
michael@0 | 1330 | |
michael@0 | 1331 | // active values should only be modified if out of new range |
michael@0 | 1332 | cpi->active_worst_quality = clamp(cpi->active_worst_quality, |
michael@0 | 1333 | cpi->oxcf.best_allowed_q, |
michael@0 | 1334 | cpi->oxcf.worst_allowed_q); |
michael@0 | 1335 | |
michael@0 | 1336 | cpi->active_best_quality = clamp(cpi->active_best_quality, |
michael@0 | 1337 | cpi->oxcf.best_allowed_q, |
michael@0 | 1338 | cpi->oxcf.worst_allowed_q); |
michael@0 | 1339 | |
michael@0 | 1340 | cpi->buffered_mode = cpi->oxcf.optimal_buffer_level > 0; |
michael@0 | 1341 | |
michael@0 | 1342 | cpi->cq_target_quality = cpi->oxcf.cq_level; |
michael@0 | 1343 | |
michael@0 | 1344 | cm->mcomp_filter_type = DEFAULT_INTERP_FILTER; |
michael@0 | 1345 | |
michael@0 | 1346 | cpi->target_bandwidth = cpi->oxcf.target_bandwidth; |
michael@0 | 1347 | |
michael@0 | 1348 | cm->display_width = cpi->oxcf.width; |
michael@0 | 1349 | cm->display_height = cpi->oxcf.height; |
michael@0 | 1350 | |
michael@0 | 1351 | // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs) |
michael@0 | 1352 | cpi->oxcf.Sharpness = MIN(7, cpi->oxcf.Sharpness); |
michael@0 | 1353 | |
michael@0 | 1354 | cpi->common.lf.sharpness_level = cpi->oxcf.Sharpness; |
michael@0 | 1355 | |
michael@0 | 1356 | if (cpi->initial_width) { |
michael@0 | 1357 | // Increasing the size of the frame beyond the first seen frame, or some |
michael@0 | 1358 | // otherwise signalled maximum size, is not supported. |
michael@0 | 1359 | // TODO(jkoleszar): exit gracefully. |
michael@0 | 1360 | assert(cm->width <= cpi->initial_width); |
michael@0 | 1361 | assert(cm->height <= cpi->initial_height); |
michael@0 | 1362 | } |
michael@0 | 1363 | update_frame_size(cpi); |
michael@0 | 1364 | |
michael@0 | 1365 | if (cpi->oxcf.fixed_q >= 0) { |
michael@0 | 1366 | cpi->last_q[0] = cpi->oxcf.fixed_q; |
michael@0 | 1367 | cpi->last_q[1] = cpi->oxcf.fixed_q; |
michael@0 | 1368 | cpi->last_boosted_qindex = cpi->oxcf.fixed_q; |
michael@0 | 1369 | } |
michael@0 | 1370 | |
michael@0 | 1371 | cpi->speed = cpi->oxcf.cpu_used; |
michael@0 | 1372 | |
michael@0 | 1373 | if (cpi->oxcf.lag_in_frames == 0) { |
michael@0 | 1374 | // force to allowlag to 0 if lag_in_frames is 0; |
michael@0 | 1375 | cpi->oxcf.allow_lag = 0; |
michael@0 | 1376 | } else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS) { |
michael@0 | 1377 | // Limit on lag buffers as these are not currently dynamically allocated |
michael@0 | 1378 | cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS; |
michael@0 | 1379 | } |
michael@0 | 1380 | |
michael@0 | 1381 | // YX Temp |
michael@0 | 1382 | #if CONFIG_MULTIPLE_ARF |
michael@0 | 1383 | vp9_zero(cpi->alt_ref_source); |
michael@0 | 1384 | #else |
michael@0 | 1385 | cpi->alt_ref_source = NULL; |
michael@0 | 1386 | #endif |
michael@0 | 1387 | cpi->is_src_frame_alt_ref = 0; |
michael@0 | 1388 | |
michael@0 | 1389 | #if 0 |
michael@0 | 1390 | // Experimental RD Code |
michael@0 | 1391 | cpi->frame_distortion = 0; |
michael@0 | 1392 | cpi->last_frame_distortion = 0; |
michael@0 | 1393 | #endif |
michael@0 | 1394 | |
michael@0 | 1395 | set_tile_limits(cpi); |
michael@0 | 1396 | } |
michael@0 | 1397 | |
michael@0 | 1398 | #define M_LOG2_E 0.693147180559945309417 |
michael@0 | 1399 | #define log2f(x) (log (x) / (float) M_LOG2_E) |
michael@0 | 1400 | |
michael@0 | 1401 | static void cal_nmvjointsadcost(int *mvjointsadcost) { |
michael@0 | 1402 | mvjointsadcost[0] = 600; |
michael@0 | 1403 | mvjointsadcost[1] = 300; |
michael@0 | 1404 | mvjointsadcost[2] = 300; |
michael@0 | 1405 | mvjointsadcost[0] = 300; |
michael@0 | 1406 | } |
michael@0 | 1407 | |
michael@0 | 1408 | static void cal_nmvsadcosts(int *mvsadcost[2]) { |
michael@0 | 1409 | int i = 1; |
michael@0 | 1410 | |
michael@0 | 1411 | mvsadcost[0][0] = 0; |
michael@0 | 1412 | mvsadcost[1][0] = 0; |
michael@0 | 1413 | |
michael@0 | 1414 | do { |
michael@0 | 1415 | double z = 256 * (2 * (log2f(8 * i) + .6)); |
michael@0 | 1416 | mvsadcost[0][i] = (int)z; |
michael@0 | 1417 | mvsadcost[1][i] = (int)z; |
michael@0 | 1418 | mvsadcost[0][-i] = (int)z; |
michael@0 | 1419 | mvsadcost[1][-i] = (int)z; |
michael@0 | 1420 | } while (++i <= MV_MAX); |
michael@0 | 1421 | } |
michael@0 | 1422 | |
michael@0 | 1423 | static void cal_nmvsadcosts_hp(int *mvsadcost[2]) { |
michael@0 | 1424 | int i = 1; |
michael@0 | 1425 | |
michael@0 | 1426 | mvsadcost[0][0] = 0; |
michael@0 | 1427 | mvsadcost[1][0] = 0; |
michael@0 | 1428 | |
michael@0 | 1429 | do { |
michael@0 | 1430 | double z = 256 * (2 * (log2f(8 * i) + .6)); |
michael@0 | 1431 | mvsadcost[0][i] = (int)z; |
michael@0 | 1432 | mvsadcost[1][i] = (int)z; |
michael@0 | 1433 | mvsadcost[0][-i] = (int)z; |
michael@0 | 1434 | mvsadcost[1][-i] = (int)z; |
michael@0 | 1435 | } while (++i <= MV_MAX); |
michael@0 | 1436 | } |
michael@0 | 1437 | |
michael@0 | 1438 | static void alloc_mode_context(VP9_COMMON *cm, int num_4x4_blk, |
michael@0 | 1439 | PICK_MODE_CONTEXT *ctx) { |
michael@0 | 1440 | int num_pix = num_4x4_blk << 4; |
michael@0 | 1441 | int i, k; |
michael@0 | 1442 | ctx->num_4x4_blk = num_4x4_blk; |
michael@0 | 1443 | CHECK_MEM_ERROR(cm, ctx->zcoeff_blk, |
michael@0 | 1444 | vpx_calloc(num_4x4_blk, sizeof(uint8_t))); |
michael@0 | 1445 | for (i = 0; i < MAX_MB_PLANE; ++i) { |
michael@0 | 1446 | for (k = 0; k < 3; ++k) { |
michael@0 | 1447 | CHECK_MEM_ERROR(cm, ctx->coeff[i][k], |
michael@0 | 1448 | vpx_memalign(16, num_pix * sizeof(int16_t))); |
michael@0 | 1449 | CHECK_MEM_ERROR(cm, ctx->qcoeff[i][k], |
michael@0 | 1450 | vpx_memalign(16, num_pix * sizeof(int16_t))); |
michael@0 | 1451 | CHECK_MEM_ERROR(cm, ctx->dqcoeff[i][k], |
michael@0 | 1452 | vpx_memalign(16, num_pix * sizeof(int16_t))); |
michael@0 | 1453 | CHECK_MEM_ERROR(cm, ctx->eobs[i][k], |
michael@0 | 1454 | vpx_memalign(16, num_pix * sizeof(uint16_t))); |
michael@0 | 1455 | ctx->coeff_pbuf[i][k] = ctx->coeff[i][k]; |
michael@0 | 1456 | ctx->qcoeff_pbuf[i][k] = ctx->qcoeff[i][k]; |
michael@0 | 1457 | ctx->dqcoeff_pbuf[i][k] = ctx->dqcoeff[i][k]; |
michael@0 | 1458 | ctx->eobs_pbuf[i][k] = ctx->eobs[i][k]; |
michael@0 | 1459 | } |
michael@0 | 1460 | } |
michael@0 | 1461 | } |
michael@0 | 1462 | |
michael@0 | 1463 | static void free_mode_context(PICK_MODE_CONTEXT *ctx) { |
michael@0 | 1464 | int i, k; |
michael@0 | 1465 | vpx_free(ctx->zcoeff_blk); |
michael@0 | 1466 | ctx->zcoeff_blk = 0; |
michael@0 | 1467 | for (i = 0; i < MAX_MB_PLANE; ++i) { |
michael@0 | 1468 | for (k = 0; k < 3; ++k) { |
michael@0 | 1469 | vpx_free(ctx->coeff[i][k]); |
michael@0 | 1470 | ctx->coeff[i][k] = 0; |
michael@0 | 1471 | vpx_free(ctx->qcoeff[i][k]); |
michael@0 | 1472 | ctx->qcoeff[i][k] = 0; |
michael@0 | 1473 | vpx_free(ctx->dqcoeff[i][k]); |
michael@0 | 1474 | ctx->dqcoeff[i][k] = 0; |
michael@0 | 1475 | vpx_free(ctx->eobs[i][k]); |
michael@0 | 1476 | ctx->eobs[i][k] = 0; |
michael@0 | 1477 | } |
michael@0 | 1478 | } |
michael@0 | 1479 | } |
michael@0 | 1480 | |
michael@0 | 1481 | static void init_pick_mode_context(VP9_COMP *cpi) { |
michael@0 | 1482 | int i; |
michael@0 | 1483 | VP9_COMMON *const cm = &cpi->common; |
michael@0 | 1484 | MACROBLOCK *const x = &cpi->mb; |
michael@0 | 1485 | |
michael@0 | 1486 | |
michael@0 | 1487 | for (i = 0; i < BLOCK_SIZES; ++i) { |
michael@0 | 1488 | const int num_4x4_w = num_4x4_blocks_wide_lookup[i]; |
michael@0 | 1489 | const int num_4x4_h = num_4x4_blocks_high_lookup[i]; |
michael@0 | 1490 | const int num_4x4_blk = MAX(4, num_4x4_w * num_4x4_h); |
michael@0 | 1491 | if (i < BLOCK_16X16) { |
michael@0 | 1492 | for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) { |
michael@0 | 1493 | for (x->mb_index = 0; x->mb_index < 4; ++x->mb_index) { |
michael@0 | 1494 | for (x->b_index = 0; x->b_index < 16 / num_4x4_blk; ++x->b_index) { |
michael@0 | 1495 | PICK_MODE_CONTEXT *ctx = get_block_context(x, i); |
michael@0 | 1496 | alloc_mode_context(cm, num_4x4_blk, ctx); |
michael@0 | 1497 | } |
michael@0 | 1498 | } |
michael@0 | 1499 | } |
michael@0 | 1500 | } else if (i < BLOCK_32X32) { |
michael@0 | 1501 | for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) { |
michael@0 | 1502 | for (x->mb_index = 0; x->mb_index < 64 / num_4x4_blk; ++x->mb_index) { |
michael@0 | 1503 | PICK_MODE_CONTEXT *ctx = get_block_context(x, i); |
michael@0 | 1504 | ctx->num_4x4_blk = num_4x4_blk; |
michael@0 | 1505 | alloc_mode_context(cm, num_4x4_blk, ctx); |
michael@0 | 1506 | } |
michael@0 | 1507 | } |
michael@0 | 1508 | } else if (i < BLOCK_64X64) { |
michael@0 | 1509 | for (x->sb_index = 0; x->sb_index < 256 / num_4x4_blk; ++x->sb_index) { |
michael@0 | 1510 | PICK_MODE_CONTEXT *ctx = get_block_context(x, i); |
michael@0 | 1511 | ctx->num_4x4_blk = num_4x4_blk; |
michael@0 | 1512 | alloc_mode_context(cm, num_4x4_blk, ctx); |
michael@0 | 1513 | } |
michael@0 | 1514 | } else { |
michael@0 | 1515 | PICK_MODE_CONTEXT *ctx = get_block_context(x, i); |
michael@0 | 1516 | ctx->num_4x4_blk = num_4x4_blk; |
michael@0 | 1517 | alloc_mode_context(cm, num_4x4_blk, ctx); |
michael@0 | 1518 | } |
michael@0 | 1519 | } |
michael@0 | 1520 | } |
michael@0 | 1521 | |
michael@0 | 1522 | static void free_pick_mode_context(MACROBLOCK *x) { |
michael@0 | 1523 | int i; |
michael@0 | 1524 | |
michael@0 | 1525 | for (i = 0; i < BLOCK_SIZES; ++i) { |
michael@0 | 1526 | const int num_4x4_w = num_4x4_blocks_wide_lookup[i]; |
michael@0 | 1527 | const int num_4x4_h = num_4x4_blocks_high_lookup[i]; |
michael@0 | 1528 | const int num_4x4_blk = MAX(4, num_4x4_w * num_4x4_h); |
michael@0 | 1529 | if (i < BLOCK_16X16) { |
michael@0 | 1530 | for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) { |
michael@0 | 1531 | for (x->mb_index = 0; x->mb_index < 4; ++x->mb_index) { |
michael@0 | 1532 | for (x->b_index = 0; x->b_index < 16 / num_4x4_blk; ++x->b_index) { |
michael@0 | 1533 | PICK_MODE_CONTEXT *ctx = get_block_context(x, i); |
michael@0 | 1534 | free_mode_context(ctx); |
michael@0 | 1535 | } |
michael@0 | 1536 | } |
michael@0 | 1537 | } |
michael@0 | 1538 | } else if (i < BLOCK_32X32) { |
michael@0 | 1539 | for (x->sb_index = 0; x->sb_index < 4; ++x->sb_index) { |
michael@0 | 1540 | for (x->mb_index = 0; x->mb_index < 64 / num_4x4_blk; ++x->mb_index) { |
michael@0 | 1541 | PICK_MODE_CONTEXT *ctx = get_block_context(x, i); |
michael@0 | 1542 | free_mode_context(ctx); |
michael@0 | 1543 | } |
michael@0 | 1544 | } |
michael@0 | 1545 | } else if (i < BLOCK_64X64) { |
michael@0 | 1546 | for (x->sb_index = 0; x->sb_index < 256 / num_4x4_blk; ++x->sb_index) { |
michael@0 | 1547 | PICK_MODE_CONTEXT *ctx = get_block_context(x, i); |
michael@0 | 1548 | free_mode_context(ctx); |
michael@0 | 1549 | } |
michael@0 | 1550 | } else { |
michael@0 | 1551 | PICK_MODE_CONTEXT *ctx = get_block_context(x, i); |
michael@0 | 1552 | free_mode_context(ctx); |
michael@0 | 1553 | } |
michael@0 | 1554 | } |
michael@0 | 1555 | } |
michael@0 | 1556 | |
michael@0 | 1557 | VP9_PTR vp9_create_compressor(VP9_CONFIG *oxcf) { |
michael@0 | 1558 | int i, j; |
michael@0 | 1559 | volatile union { |
michael@0 | 1560 | VP9_COMP *cpi; |
michael@0 | 1561 | VP9_PTR ptr; |
michael@0 | 1562 | } ctx; |
michael@0 | 1563 | |
michael@0 | 1564 | VP9_COMP *cpi; |
michael@0 | 1565 | VP9_COMMON *cm; |
michael@0 | 1566 | |
michael@0 | 1567 | cpi = ctx.cpi = vpx_memalign(32, sizeof(VP9_COMP)); |
michael@0 | 1568 | // Check that the CPI instance is valid |
michael@0 | 1569 | if (!cpi) |
michael@0 | 1570 | return 0; |
michael@0 | 1571 | |
michael@0 | 1572 | cm = &cpi->common; |
michael@0 | 1573 | |
michael@0 | 1574 | vp9_zero(*cpi); |
michael@0 | 1575 | |
michael@0 | 1576 | if (setjmp(cm->error.jmp)) { |
michael@0 | 1577 | VP9_PTR ptr = ctx.ptr; |
michael@0 | 1578 | |
michael@0 | 1579 | ctx.cpi->common.error.setjmp = 0; |
michael@0 | 1580 | vp9_remove_compressor(&ptr); |
michael@0 | 1581 | return 0; |
michael@0 | 1582 | } |
michael@0 | 1583 | |
michael@0 | 1584 | cm->error.setjmp = 1; |
michael@0 | 1585 | |
michael@0 | 1586 | CHECK_MEM_ERROR(cm, cpi->mb.ss, vpx_calloc(sizeof(search_site), |
michael@0 | 1587 | (MAX_MVSEARCH_STEPS * 8) + 1)); |
michael@0 | 1588 | |
michael@0 | 1589 | vp9_create_common(cm); |
michael@0 | 1590 | |
michael@0 | 1591 | init_config((VP9_PTR)cpi, oxcf); |
michael@0 | 1592 | |
michael@0 | 1593 | init_pick_mode_context(cpi); |
michael@0 | 1594 | |
michael@0 | 1595 | cm->current_video_frame = 0; |
michael@0 | 1596 | cpi->kf_overspend_bits = 0; |
michael@0 | 1597 | cpi->kf_bitrate_adjustment = 0; |
michael@0 | 1598 | cpi->frames_till_gf_update_due = 0; |
michael@0 | 1599 | cpi->gf_overspend_bits = 0; |
michael@0 | 1600 | cpi->non_gf_bitrate_adjustment = 0; |
michael@0 | 1601 | |
michael@0 | 1602 | // Set reference frame sign bias for ALTREF frame to 1 (for now) |
michael@0 | 1603 | cm->ref_frame_sign_bias[ALTREF_FRAME] = 1; |
michael@0 | 1604 | |
michael@0 | 1605 | cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL; |
michael@0 | 1606 | |
michael@0 | 1607 | cpi->gold_is_last = 0; |
michael@0 | 1608 | cpi->alt_is_last = 0; |
michael@0 | 1609 | cpi->gold_is_alt = 0; |
michael@0 | 1610 | |
michael@0 | 1611 | // Spatial scalability |
michael@0 | 1612 | cpi->number_spatial_layers = oxcf->ss_number_layers; |
michael@0 | 1613 | |
michael@0 | 1614 | // Create the encoder segmentation map and set all entries to 0 |
michael@0 | 1615 | CHECK_MEM_ERROR(cm, cpi->segmentation_map, |
michael@0 | 1616 | vpx_calloc(cm->mi_rows * cm->mi_cols, 1)); |
michael@0 | 1617 | |
michael@0 | 1618 | // And a place holder structure is the coding context |
michael@0 | 1619 | // for use if we want to save and restore it |
michael@0 | 1620 | CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy, |
michael@0 | 1621 | vpx_calloc(cm->mi_rows * cm->mi_cols, 1)); |
michael@0 | 1622 | |
michael@0 | 1623 | CHECK_MEM_ERROR(cm, cpi->active_map, vpx_calloc(cm->MBs, 1)); |
michael@0 | 1624 | vpx_memset(cpi->active_map, 1, cm->MBs); |
michael@0 | 1625 | cpi->active_map_enabled = 0; |
michael@0 | 1626 | |
michael@0 | 1627 | for (i = 0; i < (sizeof(cpi->mbgraph_stats) / |
michael@0 | 1628 | sizeof(cpi->mbgraph_stats[0])); i++) { |
michael@0 | 1629 | CHECK_MEM_ERROR(cm, cpi->mbgraph_stats[i].mb_stats, |
michael@0 | 1630 | vpx_calloc(cm->MBs * |
michael@0 | 1631 | sizeof(*cpi->mbgraph_stats[i].mb_stats), 1)); |
michael@0 | 1632 | } |
michael@0 | 1633 | |
michael@0 | 1634 | #ifdef ENTROPY_STATS |
michael@0 | 1635 | if (cpi->pass != 1) |
michael@0 | 1636 | init_context_counters(); |
michael@0 | 1637 | #endif |
michael@0 | 1638 | |
michael@0 | 1639 | #ifdef MODE_STATS |
michael@0 | 1640 | init_tx_count_stats(); |
michael@0 | 1641 | init_switchable_interp_stats(); |
michael@0 | 1642 | #endif |
michael@0 | 1643 | |
michael@0 | 1644 | /*Initialize the feed-forward activity masking.*/ |
michael@0 | 1645 | cpi->activity_avg = 90 << 12; |
michael@0 | 1646 | |
michael@0 | 1647 | cpi->frames_since_key = 8; // Sensible default for first frame. |
michael@0 | 1648 | cpi->key_frame_frequency = cpi->oxcf.key_freq; |
michael@0 | 1649 | cpi->this_key_frame_forced = 0; |
michael@0 | 1650 | cpi->next_key_frame_forced = 0; |
michael@0 | 1651 | |
michael@0 | 1652 | cpi->source_alt_ref_pending = 0; |
michael@0 | 1653 | cpi->source_alt_ref_active = 0; |
michael@0 | 1654 | cpi->refresh_alt_ref_frame = 0; |
michael@0 | 1655 | |
michael@0 | 1656 | #if CONFIG_MULTIPLE_ARF |
michael@0 | 1657 | // Turn multiple ARF usage on/off. This is a quick hack for the initial test |
michael@0 | 1658 | // version. It should eventually be set via the codec API. |
michael@0 | 1659 | cpi->multi_arf_enabled = 1; |
michael@0 | 1660 | |
michael@0 | 1661 | if (cpi->multi_arf_enabled) { |
michael@0 | 1662 | cpi->sequence_number = 0; |
michael@0 | 1663 | cpi->frame_coding_order_period = 0; |
michael@0 | 1664 | vp9_zero(cpi->frame_coding_order); |
michael@0 | 1665 | vp9_zero(cpi->arf_buffer_idx); |
michael@0 | 1666 | } |
michael@0 | 1667 | #endif |
michael@0 | 1668 | |
michael@0 | 1669 | cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS; |
michael@0 | 1670 | #if CONFIG_INTERNAL_STATS |
michael@0 | 1671 | cpi->b_calculate_ssimg = 0; |
michael@0 | 1672 | |
michael@0 | 1673 | cpi->count = 0; |
michael@0 | 1674 | cpi->bytes = 0; |
michael@0 | 1675 | |
michael@0 | 1676 | if (cpi->b_calculate_psnr) { |
michael@0 | 1677 | cpi->total_sq_error = 0.0; |
michael@0 | 1678 | cpi->total_sq_error2 = 0.0; |
michael@0 | 1679 | cpi->total_y = 0.0; |
michael@0 | 1680 | cpi->total_u = 0.0; |
michael@0 | 1681 | cpi->total_v = 0.0; |
michael@0 | 1682 | cpi->total = 0.0; |
michael@0 | 1683 | cpi->totalp_y = 0.0; |
michael@0 | 1684 | cpi->totalp_u = 0.0; |
michael@0 | 1685 | cpi->totalp_v = 0.0; |
michael@0 | 1686 | cpi->totalp = 0.0; |
michael@0 | 1687 | cpi->tot_recode_hits = 0; |
michael@0 | 1688 | cpi->summed_quality = 0; |
michael@0 | 1689 | cpi->summed_weights = 0; |
michael@0 | 1690 | cpi->summedp_quality = 0; |
michael@0 | 1691 | cpi->summedp_weights = 0; |
michael@0 | 1692 | } |
michael@0 | 1693 | |
michael@0 | 1694 | if (cpi->b_calculate_ssimg) { |
michael@0 | 1695 | cpi->total_ssimg_y = 0; |
michael@0 | 1696 | cpi->total_ssimg_u = 0; |
michael@0 | 1697 | cpi->total_ssimg_v = 0; |
michael@0 | 1698 | cpi->total_ssimg_all = 0; |
michael@0 | 1699 | } |
michael@0 | 1700 | |
michael@0 | 1701 | #endif |
michael@0 | 1702 | |
michael@0 | 1703 | cpi->first_time_stamp_ever = INT64_MAX; |
michael@0 | 1704 | |
michael@0 | 1705 | cpi->frames_till_gf_update_due = 0; |
michael@0 | 1706 | cpi->key_frame_count = 1; |
michael@0 | 1707 | |
michael@0 | 1708 | cpi->ni_av_qi = cpi->oxcf.worst_allowed_q; |
michael@0 | 1709 | cpi->ni_tot_qi = 0; |
michael@0 | 1710 | cpi->ni_frames = 0; |
michael@0 | 1711 | cpi->tot_q = 0.0; |
michael@0 | 1712 | cpi->avg_q = vp9_convert_qindex_to_q(cpi->oxcf.worst_allowed_q); |
michael@0 | 1713 | cpi->total_byte_count = 0; |
michael@0 | 1714 | |
michael@0 | 1715 | cpi->rate_correction_factor = 1.0; |
michael@0 | 1716 | cpi->key_frame_rate_correction_factor = 1.0; |
michael@0 | 1717 | cpi->gf_rate_correction_factor = 1.0; |
michael@0 | 1718 | cpi->twopass.est_max_qcorrection_factor = 1.0; |
michael@0 | 1719 | |
michael@0 | 1720 | cal_nmvjointsadcost(cpi->mb.nmvjointsadcost); |
michael@0 | 1721 | cpi->mb.nmvcost[0] = &cpi->mb.nmvcosts[0][MV_MAX]; |
michael@0 | 1722 | cpi->mb.nmvcost[1] = &cpi->mb.nmvcosts[1][MV_MAX]; |
michael@0 | 1723 | cpi->mb.nmvsadcost[0] = &cpi->mb.nmvsadcosts[0][MV_MAX]; |
michael@0 | 1724 | cpi->mb.nmvsadcost[1] = &cpi->mb.nmvsadcosts[1][MV_MAX]; |
michael@0 | 1725 | cal_nmvsadcosts(cpi->mb.nmvsadcost); |
michael@0 | 1726 | |
michael@0 | 1727 | cpi->mb.nmvcost_hp[0] = &cpi->mb.nmvcosts_hp[0][MV_MAX]; |
michael@0 | 1728 | cpi->mb.nmvcost_hp[1] = &cpi->mb.nmvcosts_hp[1][MV_MAX]; |
michael@0 | 1729 | cpi->mb.nmvsadcost_hp[0] = &cpi->mb.nmvsadcosts_hp[0][MV_MAX]; |
michael@0 | 1730 | cpi->mb.nmvsadcost_hp[1] = &cpi->mb.nmvsadcosts_hp[1][MV_MAX]; |
michael@0 | 1731 | cal_nmvsadcosts_hp(cpi->mb.nmvsadcost_hp); |
michael@0 | 1732 | |
michael@0 | 1733 | for (i = 0; i < KEY_FRAME_CONTEXT; i++) |
michael@0 | 1734 | cpi->prior_key_frame_distance[i] = (int)cpi->output_framerate; |
michael@0 | 1735 | |
michael@0 | 1736 | #ifdef OUTPUT_YUV_SRC |
michael@0 | 1737 | yuv_file = fopen("bd.yuv", "ab"); |
michael@0 | 1738 | #endif |
michael@0 | 1739 | #ifdef OUTPUT_YUV_REC |
michael@0 | 1740 | yuv_rec_file = fopen("rec.yuv", "wb"); |
michael@0 | 1741 | #endif |
michael@0 | 1742 | |
michael@0 | 1743 | #if 0 |
michael@0 | 1744 | framepsnr = fopen("framepsnr.stt", "a"); |
michael@0 | 1745 | kf_list = fopen("kf_list.stt", "w"); |
michael@0 | 1746 | #endif |
michael@0 | 1747 | |
michael@0 | 1748 | cpi->output_pkt_list = oxcf->output_pkt_list; |
michael@0 | 1749 | |
michael@0 | 1750 | cpi->enable_encode_breakout = 1; |
michael@0 | 1751 | |
michael@0 | 1752 | if (cpi->pass == 1) { |
michael@0 | 1753 | vp9_init_first_pass(cpi); |
michael@0 | 1754 | } else if (cpi->pass == 2) { |
michael@0 | 1755 | size_t packet_sz = sizeof(FIRSTPASS_STATS); |
michael@0 | 1756 | int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz); |
michael@0 | 1757 | |
michael@0 | 1758 | cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf; |
michael@0 | 1759 | cpi->twopass.stats_in = cpi->twopass.stats_in_start; |
michael@0 | 1760 | cpi->twopass.stats_in_end = (void *)((char *)cpi->twopass.stats_in |
michael@0 | 1761 | + (packets - 1) * packet_sz); |
michael@0 | 1762 | vp9_init_second_pass(cpi); |
michael@0 | 1763 | } |
michael@0 | 1764 | |
michael@0 | 1765 | vp9_set_speed_features(cpi); |
michael@0 | 1766 | |
michael@0 | 1767 | // Default rd threshold factors for mode selection |
michael@0 | 1768 | for (i = 0; i < BLOCK_SIZES; ++i) { |
michael@0 | 1769 | for (j = 0; j < MAX_MODES; ++j) |
michael@0 | 1770 | cpi->rd_thresh_freq_fact[i][j] = 32; |
michael@0 | 1771 | for (j = 0; j < MAX_REFS; ++j) |
michael@0 | 1772 | cpi->rd_thresh_freq_sub8x8[i][j] = 32; |
michael@0 | 1773 | } |
michael@0 | 1774 | |
michael@0 | 1775 | #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SVFHH, SVFHV, SVFHHV, \ |
michael@0 | 1776 | SDX3F, SDX8F, SDX4DF)\ |
michael@0 | 1777 | cpi->fn_ptr[BT].sdf = SDF; \ |
michael@0 | 1778 | cpi->fn_ptr[BT].sdaf = SDAF; \ |
michael@0 | 1779 | cpi->fn_ptr[BT].vf = VF; \ |
michael@0 | 1780 | cpi->fn_ptr[BT].svf = SVF; \ |
michael@0 | 1781 | cpi->fn_ptr[BT].svaf = SVAF; \ |
michael@0 | 1782 | cpi->fn_ptr[BT].svf_halfpix_h = SVFHH; \ |
michael@0 | 1783 | cpi->fn_ptr[BT].svf_halfpix_v = SVFHV; \ |
michael@0 | 1784 | cpi->fn_ptr[BT].svf_halfpix_hv = SVFHHV; \ |
michael@0 | 1785 | cpi->fn_ptr[BT].sdx3f = SDX3F; \ |
michael@0 | 1786 | cpi->fn_ptr[BT].sdx8f = SDX8F; \ |
michael@0 | 1787 | cpi->fn_ptr[BT].sdx4df = SDX4DF; |
michael@0 | 1788 | |
michael@0 | 1789 | BFP(BLOCK_32X16, vp9_sad32x16, vp9_sad32x16_avg, |
michael@0 | 1790 | vp9_variance32x16, vp9_sub_pixel_variance32x16, |
michael@0 | 1791 | vp9_sub_pixel_avg_variance32x16, NULL, NULL, |
michael@0 | 1792 | NULL, NULL, NULL, |
michael@0 | 1793 | vp9_sad32x16x4d) |
michael@0 | 1794 | |
michael@0 | 1795 | BFP(BLOCK_16X32, vp9_sad16x32, vp9_sad16x32_avg, |
michael@0 | 1796 | vp9_variance16x32, vp9_sub_pixel_variance16x32, |
michael@0 | 1797 | vp9_sub_pixel_avg_variance16x32, NULL, NULL, |
michael@0 | 1798 | NULL, NULL, NULL, |
michael@0 | 1799 | vp9_sad16x32x4d) |
michael@0 | 1800 | |
michael@0 | 1801 | BFP(BLOCK_64X32, vp9_sad64x32, vp9_sad64x32_avg, |
michael@0 | 1802 | vp9_variance64x32, vp9_sub_pixel_variance64x32, |
michael@0 | 1803 | vp9_sub_pixel_avg_variance64x32, NULL, NULL, |
michael@0 | 1804 | NULL, NULL, NULL, |
michael@0 | 1805 | vp9_sad64x32x4d) |
michael@0 | 1806 | |
michael@0 | 1807 | BFP(BLOCK_32X64, vp9_sad32x64, vp9_sad32x64_avg, |
michael@0 | 1808 | vp9_variance32x64, vp9_sub_pixel_variance32x64, |
michael@0 | 1809 | vp9_sub_pixel_avg_variance32x64, NULL, NULL, |
michael@0 | 1810 | NULL, NULL, NULL, |
michael@0 | 1811 | vp9_sad32x64x4d) |
michael@0 | 1812 | |
michael@0 | 1813 | BFP(BLOCK_32X32, vp9_sad32x32, vp9_sad32x32_avg, |
michael@0 | 1814 | vp9_variance32x32, vp9_sub_pixel_variance32x32, |
michael@0 | 1815 | vp9_sub_pixel_avg_variance32x32, vp9_variance_halfpixvar32x32_h, |
michael@0 | 1816 | vp9_variance_halfpixvar32x32_v, |
michael@0 | 1817 | vp9_variance_halfpixvar32x32_hv, vp9_sad32x32x3, vp9_sad32x32x8, |
michael@0 | 1818 | vp9_sad32x32x4d) |
michael@0 | 1819 | |
michael@0 | 1820 | BFP(BLOCK_64X64, vp9_sad64x64, vp9_sad64x64_avg, |
michael@0 | 1821 | vp9_variance64x64, vp9_sub_pixel_variance64x64, |
michael@0 | 1822 | vp9_sub_pixel_avg_variance64x64, vp9_variance_halfpixvar64x64_h, |
michael@0 | 1823 | vp9_variance_halfpixvar64x64_v, |
michael@0 | 1824 | vp9_variance_halfpixvar64x64_hv, vp9_sad64x64x3, vp9_sad64x64x8, |
michael@0 | 1825 | vp9_sad64x64x4d) |
michael@0 | 1826 | |
michael@0 | 1827 | BFP(BLOCK_16X16, vp9_sad16x16, vp9_sad16x16_avg, |
michael@0 | 1828 | vp9_variance16x16, vp9_sub_pixel_variance16x16, |
michael@0 | 1829 | vp9_sub_pixel_avg_variance16x16, vp9_variance_halfpixvar16x16_h, |
michael@0 | 1830 | vp9_variance_halfpixvar16x16_v, |
michael@0 | 1831 | vp9_variance_halfpixvar16x16_hv, vp9_sad16x16x3, vp9_sad16x16x8, |
michael@0 | 1832 | vp9_sad16x16x4d) |
michael@0 | 1833 | |
michael@0 | 1834 | BFP(BLOCK_16X8, vp9_sad16x8, vp9_sad16x8_avg, |
michael@0 | 1835 | vp9_variance16x8, vp9_sub_pixel_variance16x8, |
michael@0 | 1836 | vp9_sub_pixel_avg_variance16x8, NULL, NULL, NULL, |
michael@0 | 1837 | vp9_sad16x8x3, vp9_sad16x8x8, vp9_sad16x8x4d) |
michael@0 | 1838 | |
michael@0 | 1839 | BFP(BLOCK_8X16, vp9_sad8x16, vp9_sad8x16_avg, |
michael@0 | 1840 | vp9_variance8x16, vp9_sub_pixel_variance8x16, |
michael@0 | 1841 | vp9_sub_pixel_avg_variance8x16, NULL, NULL, NULL, |
michael@0 | 1842 | vp9_sad8x16x3, vp9_sad8x16x8, vp9_sad8x16x4d) |
michael@0 | 1843 | |
michael@0 | 1844 | BFP(BLOCK_8X8, vp9_sad8x8, vp9_sad8x8_avg, |
michael@0 | 1845 | vp9_variance8x8, vp9_sub_pixel_variance8x8, |
michael@0 | 1846 | vp9_sub_pixel_avg_variance8x8, NULL, NULL, NULL, |
michael@0 | 1847 | vp9_sad8x8x3, vp9_sad8x8x8, vp9_sad8x8x4d) |
michael@0 | 1848 | |
michael@0 | 1849 | BFP(BLOCK_8X4, vp9_sad8x4, vp9_sad8x4_avg, |
michael@0 | 1850 | vp9_variance8x4, vp9_sub_pixel_variance8x4, |
michael@0 | 1851 | vp9_sub_pixel_avg_variance8x4, NULL, NULL, |
michael@0 | 1852 | NULL, NULL, vp9_sad8x4x8, |
michael@0 | 1853 | vp9_sad8x4x4d) |
michael@0 | 1854 | |
michael@0 | 1855 | BFP(BLOCK_4X8, vp9_sad4x8, vp9_sad4x8_avg, |
michael@0 | 1856 | vp9_variance4x8, vp9_sub_pixel_variance4x8, |
michael@0 | 1857 | vp9_sub_pixel_avg_variance4x8, NULL, NULL, |
michael@0 | 1858 | NULL, NULL, vp9_sad4x8x8, |
michael@0 | 1859 | vp9_sad4x8x4d) |
michael@0 | 1860 | |
michael@0 | 1861 | BFP(BLOCK_4X4, vp9_sad4x4, vp9_sad4x4_avg, |
michael@0 | 1862 | vp9_variance4x4, vp9_sub_pixel_variance4x4, |
michael@0 | 1863 | vp9_sub_pixel_avg_variance4x4, NULL, NULL, NULL, |
michael@0 | 1864 | vp9_sad4x4x3, vp9_sad4x4x8, vp9_sad4x4x4d) |
michael@0 | 1865 | |
michael@0 | 1866 | cpi->full_search_sad = vp9_full_search_sad; |
michael@0 | 1867 | cpi->diamond_search_sad = vp9_diamond_search_sad; |
michael@0 | 1868 | cpi->refining_search_sad = vp9_refining_search_sad; |
michael@0 | 1869 | |
michael@0 | 1870 | // make sure frame 1 is okay |
michael@0 | 1871 | cpi->error_bins[0] = cpi->common.MBs; |
michael@0 | 1872 | |
michael@0 | 1873 | /* vp9_init_quantizer() is first called here. Add check in |
michael@0 | 1874 | * vp9_frame_init_quantizer() so that vp9_init_quantizer is only |
michael@0 | 1875 | * called later when needed. This will avoid unnecessary calls of |
michael@0 | 1876 | * vp9_init_quantizer() for every frame. |
michael@0 | 1877 | */ |
michael@0 | 1878 | vp9_init_quantizer(cpi); |
michael@0 | 1879 | |
michael@0 | 1880 | vp9_loop_filter_init(cm); |
michael@0 | 1881 | |
michael@0 | 1882 | cpi->common.error.setjmp = 0; |
michael@0 | 1883 | |
michael@0 | 1884 | vp9_zero(cpi->y_uv_mode_count); |
michael@0 | 1885 | |
michael@0 | 1886 | #ifdef MODE_TEST_HIT_STATS |
michael@0 | 1887 | vp9_zero(cpi->mode_test_hits); |
michael@0 | 1888 | #endif |
michael@0 | 1889 | |
michael@0 | 1890 | return (VP9_PTR) cpi; |
michael@0 | 1891 | } |
michael@0 | 1892 | |
michael@0 | 1893 | void vp9_remove_compressor(VP9_PTR *ptr) { |
michael@0 | 1894 | VP9_COMP *cpi = (VP9_COMP *)(*ptr); |
michael@0 | 1895 | int i; |
michael@0 | 1896 | |
michael@0 | 1897 | if (!cpi) |
michael@0 | 1898 | return; |
michael@0 | 1899 | |
michael@0 | 1900 | if (cpi && (cpi->common.current_video_frame > 0)) { |
michael@0 | 1901 | if (cpi->pass == 2) { |
michael@0 | 1902 | vp9_end_second_pass(cpi); |
michael@0 | 1903 | } |
michael@0 | 1904 | |
michael@0 | 1905 | #ifdef ENTROPY_STATS |
michael@0 | 1906 | if (cpi->pass != 1) { |
michael@0 | 1907 | print_context_counters(); |
michael@0 | 1908 | print_tree_update_probs(); |
michael@0 | 1909 | print_mode_context(cpi); |
michael@0 | 1910 | } |
michael@0 | 1911 | #endif |
michael@0 | 1912 | |
michael@0 | 1913 | #ifdef MODE_STATS |
michael@0 | 1914 | if (cpi->pass != 1) { |
michael@0 | 1915 | write_tx_count_stats(); |
michael@0 | 1916 | write_switchable_interp_stats(); |
michael@0 | 1917 | } |
michael@0 | 1918 | #endif |
michael@0 | 1919 | |
michael@0 | 1920 | #if CONFIG_INTERNAL_STATS |
michael@0 | 1921 | |
michael@0 | 1922 | vp9_clear_system_state(); |
michael@0 | 1923 | |
michael@0 | 1924 | // printf("\n8x8-4x4:%d-%d\n", cpi->t8x8_count, cpi->t4x4_count); |
michael@0 | 1925 | if (cpi->pass != 1) { |
michael@0 | 1926 | FILE *f = fopen("opsnr.stt", "a"); |
michael@0 | 1927 | double time_encoded = (cpi->last_end_time_stamp_seen |
michael@0 | 1928 | - cpi->first_time_stamp_ever) / 10000000.000; |
michael@0 | 1929 | double total_encode_time = (cpi->time_receive_data + |
michael@0 | 1930 | cpi->time_compress_data) / 1000.000; |
michael@0 | 1931 | double dr = (double)cpi->bytes * (double) 8 / (double)1000 |
michael@0 | 1932 | / time_encoded; |
michael@0 | 1933 | |
michael@0 | 1934 | if (cpi->b_calculate_psnr) { |
michael@0 | 1935 | YV12_BUFFER_CONFIG *lst_yv12 = |
michael@0 | 1936 | &cpi->common.yv12_fb[cpi->common.ref_frame_map[cpi->lst_fb_idx]]; |
michael@0 | 1937 | double samples = 3.0 / 2 * cpi->count * |
michael@0 | 1938 | lst_yv12->y_width * lst_yv12->y_height; |
michael@0 | 1939 | double total_psnr = vp9_mse2psnr(samples, 255.0, cpi->total_sq_error); |
michael@0 | 1940 | double total_psnr2 = vp9_mse2psnr(samples, 255.0, cpi->total_sq_error2); |
michael@0 | 1941 | double total_ssim = 100 * pow(cpi->summed_quality / |
michael@0 | 1942 | cpi->summed_weights, 8.0); |
michael@0 | 1943 | double total_ssimp = 100 * pow(cpi->summedp_quality / |
michael@0 | 1944 | cpi->summedp_weights, 8.0); |
michael@0 | 1945 | |
michael@0 | 1946 | fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t" |
michael@0 | 1947 | "VPXSSIM\tVPSSIMP\t Time(ms)\n"); |
michael@0 | 1948 | fprintf(f, "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%8.0f\n", |
michael@0 | 1949 | dr, cpi->total / cpi->count, total_psnr, |
michael@0 | 1950 | cpi->totalp / cpi->count, total_psnr2, total_ssim, total_ssimp, |
michael@0 | 1951 | total_encode_time); |
michael@0 | 1952 | } |
michael@0 | 1953 | |
michael@0 | 1954 | if (cpi->b_calculate_ssimg) { |
michael@0 | 1955 | fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t Time(ms)\n"); |
michael@0 | 1956 | fprintf(f, "%7.2f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr, |
michael@0 | 1957 | cpi->total_ssimg_y / cpi->count, |
michael@0 | 1958 | cpi->total_ssimg_u / cpi->count, |
michael@0 | 1959 | cpi->total_ssimg_v / cpi->count, |
michael@0 | 1960 | cpi->total_ssimg_all / cpi->count, total_encode_time); |
michael@0 | 1961 | } |
michael@0 | 1962 | |
michael@0 | 1963 | fclose(f); |
michael@0 | 1964 | } |
michael@0 | 1965 | |
michael@0 | 1966 | #endif |
michael@0 | 1967 | |
michael@0 | 1968 | #ifdef MODE_TEST_HIT_STATS |
michael@0 | 1969 | if (cpi->pass != 1) { |
michael@0 | 1970 | double norm_per_pixel_mode_tests = 0; |
michael@0 | 1971 | double norm_counts[BLOCK_SIZES]; |
michael@0 | 1972 | int i; |
michael@0 | 1973 | int sb64_per_frame; |
michael@0 | 1974 | int norm_factors[BLOCK_SIZES] = |
michael@0 | 1975 | {256, 128, 128, 64, 32, 32, 16, 8, 8, 4, 2, 2, 1}; |
michael@0 | 1976 | FILE *f = fopen("mode_hit_stats.stt", "a"); |
michael@0 | 1977 | |
michael@0 | 1978 | // On average, how many mode tests do we do |
michael@0 | 1979 | for (i = 0; i < BLOCK_SIZES; ++i) { |
michael@0 | 1980 | norm_counts[i] = (double)cpi->mode_test_hits[i] / |
michael@0 | 1981 | (double)norm_factors[i]; |
michael@0 | 1982 | norm_per_pixel_mode_tests += norm_counts[i]; |
michael@0 | 1983 | } |
michael@0 | 1984 | // Convert to a number per 64x64 and per frame |
michael@0 | 1985 | sb64_per_frame = ((cpi->common.height + 63) / 64) * |
michael@0 | 1986 | ((cpi->common.width + 63) / 64); |
michael@0 | 1987 | norm_per_pixel_mode_tests = |
michael@0 | 1988 | norm_per_pixel_mode_tests / |
michael@0 | 1989 | (double)(cpi->common.current_video_frame * sb64_per_frame); |
michael@0 | 1990 | |
michael@0 | 1991 | fprintf(f, "%6.4f\n", norm_per_pixel_mode_tests); |
michael@0 | 1992 | fclose(f); |
michael@0 | 1993 | } |
michael@0 | 1994 | #endif |
michael@0 | 1995 | |
michael@0 | 1996 | #ifdef ENTROPY_STATS |
michael@0 | 1997 | { |
michael@0 | 1998 | int i, j, k; |
michael@0 | 1999 | FILE *fmode = fopen("vp9_modecontext.c", "w"); |
michael@0 | 2000 | |
michael@0 | 2001 | fprintf(fmode, "\n#include \"vp9_entropymode.h\"\n\n"); |
michael@0 | 2002 | fprintf(fmode, "const unsigned int vp9_kf_default_bmode_counts "); |
michael@0 | 2003 | fprintf(fmode, "[INTRA_MODES][INTRA_MODES]" |
michael@0 | 2004 | "[INTRA_MODES] =\n{\n"); |
michael@0 | 2005 | |
michael@0 | 2006 | for (i = 0; i < INTRA_MODES; i++) { |
michael@0 | 2007 | fprintf(fmode, " { // Above Mode : %d\n", i); |
michael@0 | 2008 | |
michael@0 | 2009 | for (j = 0; j < INTRA_MODES; j++) { |
michael@0 | 2010 | fprintf(fmode, " {"); |
michael@0 | 2011 | |
michael@0 | 2012 | for (k = 0; k < INTRA_MODES; k++) { |
michael@0 | 2013 | if (!intra_mode_stats[i][j][k]) |
michael@0 | 2014 | fprintf(fmode, " %5d, ", 1); |
michael@0 | 2015 | else |
michael@0 | 2016 | fprintf(fmode, " %5d, ", intra_mode_stats[i][j][k]); |
michael@0 | 2017 | } |
michael@0 | 2018 | |
michael@0 | 2019 | fprintf(fmode, "}, // left_mode %d\n", j); |
michael@0 | 2020 | } |
michael@0 | 2021 | |
michael@0 | 2022 | fprintf(fmode, " },\n"); |
michael@0 | 2023 | } |
michael@0 | 2024 | |
michael@0 | 2025 | fprintf(fmode, "};\n"); |
michael@0 | 2026 | fclose(fmode); |
michael@0 | 2027 | } |
michael@0 | 2028 | #endif |
michael@0 | 2029 | |
michael@0 | 2030 | |
michael@0 | 2031 | #if defined(SECTIONBITS_OUTPUT) |
michael@0 | 2032 | |
michael@0 | 2033 | if (0) { |
michael@0 | 2034 | int i; |
michael@0 | 2035 | FILE *f = fopen("tokenbits.stt", "a"); |
michael@0 | 2036 | |
michael@0 | 2037 | for (i = 0; i < 28; i++) |
michael@0 | 2038 | fprintf(f, "%8d", (int)(Sectionbits[i] / 256)); |
michael@0 | 2039 | |
michael@0 | 2040 | fprintf(f, "\n"); |
michael@0 | 2041 | fclose(f); |
michael@0 | 2042 | } |
michael@0 | 2043 | |
michael@0 | 2044 | #endif |
michael@0 | 2045 | |
michael@0 | 2046 | #if 0 |
michael@0 | 2047 | { |
michael@0 | 2048 | printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000); |
michael@0 | 2049 | printf("\n_frames recive_data encod_mb_row compress_frame Total\n"); |
michael@0 | 2050 | printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame, |
michael@0 | 2051 | cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000, |
michael@0 | 2052 | cpi->time_compress_data / 1000, |
michael@0 | 2053 | (cpi->time_receive_data + cpi->time_compress_data) / 1000); |
michael@0 | 2054 | } |
michael@0 | 2055 | #endif |
michael@0 | 2056 | } |
michael@0 | 2057 | |
michael@0 | 2058 | free_pick_mode_context(&cpi->mb); |
michael@0 | 2059 | dealloc_compressor_data(cpi); |
michael@0 | 2060 | vpx_free(cpi->mb.ss); |
michael@0 | 2061 | vpx_free(cpi->tok); |
michael@0 | 2062 | |
michael@0 | 2063 | for (i = 0; i < sizeof(cpi->mbgraph_stats) / |
michael@0 | 2064 | sizeof(cpi->mbgraph_stats[0]); ++i) { |
michael@0 | 2065 | vpx_free(cpi->mbgraph_stats[i].mb_stats); |
michael@0 | 2066 | } |
michael@0 | 2067 | |
michael@0 | 2068 | vp9_remove_common(&cpi->common); |
michael@0 | 2069 | vpx_free(cpi); |
michael@0 | 2070 | *ptr = 0; |
michael@0 | 2071 | |
michael@0 | 2072 | #ifdef OUTPUT_YUV_SRC |
michael@0 | 2073 | fclose(yuv_file); |
michael@0 | 2074 | #endif |
michael@0 | 2075 | #ifdef OUTPUT_YUV_REC |
michael@0 | 2076 | fclose(yuv_rec_file); |
michael@0 | 2077 | #endif |
michael@0 | 2078 | |
michael@0 | 2079 | #if 0 |
michael@0 | 2080 | |
michael@0 | 2081 | if (keyfile) |
michael@0 | 2082 | fclose(keyfile); |
michael@0 | 2083 | |
michael@0 | 2084 | if (framepsnr) |
michael@0 | 2085 | fclose(framepsnr); |
michael@0 | 2086 | |
michael@0 | 2087 | if (kf_list) |
michael@0 | 2088 | fclose(kf_list); |
michael@0 | 2089 | |
michael@0 | 2090 | #endif |
michael@0 | 2091 | } |
michael@0 | 2092 | |
michael@0 | 2093 | |
michael@0 | 2094 | static uint64_t calc_plane_error(uint8_t *orig, int orig_stride, |
michael@0 | 2095 | uint8_t *recon, int recon_stride, |
michael@0 | 2096 | unsigned int cols, unsigned int rows) { |
michael@0 | 2097 | unsigned int row, col; |
michael@0 | 2098 | uint64_t total_sse = 0; |
michael@0 | 2099 | int diff; |
michael@0 | 2100 | |
michael@0 | 2101 | for (row = 0; row + 16 <= rows; row += 16) { |
michael@0 | 2102 | for (col = 0; col + 16 <= cols; col += 16) { |
michael@0 | 2103 | unsigned int sse; |
michael@0 | 2104 | |
michael@0 | 2105 | vp9_mse16x16(orig + col, orig_stride, recon + col, recon_stride, &sse); |
michael@0 | 2106 | total_sse += sse; |
michael@0 | 2107 | } |
michael@0 | 2108 | |
michael@0 | 2109 | /* Handle odd-sized width */ |
michael@0 | 2110 | if (col < cols) { |
michael@0 | 2111 | unsigned int border_row, border_col; |
michael@0 | 2112 | uint8_t *border_orig = orig; |
michael@0 | 2113 | uint8_t *border_recon = recon; |
michael@0 | 2114 | |
michael@0 | 2115 | for (border_row = 0; border_row < 16; border_row++) { |
michael@0 | 2116 | for (border_col = col; border_col < cols; border_col++) { |
michael@0 | 2117 | diff = border_orig[border_col] - border_recon[border_col]; |
michael@0 | 2118 | total_sse += diff * diff; |
michael@0 | 2119 | } |
michael@0 | 2120 | |
michael@0 | 2121 | border_orig += orig_stride; |
michael@0 | 2122 | border_recon += recon_stride; |
michael@0 | 2123 | } |
michael@0 | 2124 | } |
michael@0 | 2125 | |
michael@0 | 2126 | orig += orig_stride * 16; |
michael@0 | 2127 | recon += recon_stride * 16; |
michael@0 | 2128 | } |
michael@0 | 2129 | |
michael@0 | 2130 | /* Handle odd-sized height */ |
michael@0 | 2131 | for (; row < rows; row++) { |
michael@0 | 2132 | for (col = 0; col < cols; col++) { |
michael@0 | 2133 | diff = orig[col] - recon[col]; |
michael@0 | 2134 | total_sse += diff * diff; |
michael@0 | 2135 | } |
michael@0 | 2136 | |
michael@0 | 2137 | orig += orig_stride; |
michael@0 | 2138 | recon += recon_stride; |
michael@0 | 2139 | } |
michael@0 | 2140 | |
michael@0 | 2141 | return total_sse; |
michael@0 | 2142 | } |
michael@0 | 2143 | |
michael@0 | 2144 | |
michael@0 | 2145 | static void generate_psnr_packet(VP9_COMP *cpi) { |
michael@0 | 2146 | YV12_BUFFER_CONFIG *orig = cpi->Source; |
michael@0 | 2147 | YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show; |
michael@0 | 2148 | struct vpx_codec_cx_pkt pkt; |
michael@0 | 2149 | uint64_t sse; |
michael@0 | 2150 | int i; |
michael@0 | 2151 | unsigned int width = orig->y_crop_width; |
michael@0 | 2152 | unsigned int height = orig->y_crop_height; |
michael@0 | 2153 | |
michael@0 | 2154 | pkt.kind = VPX_CODEC_PSNR_PKT; |
michael@0 | 2155 | sse = calc_plane_error(orig->y_buffer, orig->y_stride, |
michael@0 | 2156 | recon->y_buffer, recon->y_stride, |
michael@0 | 2157 | width, height); |
michael@0 | 2158 | pkt.data.psnr.sse[0] = sse; |
michael@0 | 2159 | pkt.data.psnr.sse[1] = sse; |
michael@0 | 2160 | pkt.data.psnr.samples[0] = width * height; |
michael@0 | 2161 | pkt.data.psnr.samples[1] = width * height; |
michael@0 | 2162 | |
michael@0 | 2163 | width = orig->uv_crop_width; |
michael@0 | 2164 | height = orig->uv_crop_height; |
michael@0 | 2165 | |
michael@0 | 2166 | sse = calc_plane_error(orig->u_buffer, orig->uv_stride, |
michael@0 | 2167 | recon->u_buffer, recon->uv_stride, |
michael@0 | 2168 | width, height); |
michael@0 | 2169 | pkt.data.psnr.sse[0] += sse; |
michael@0 | 2170 | pkt.data.psnr.sse[2] = sse; |
michael@0 | 2171 | pkt.data.psnr.samples[0] += width * height; |
michael@0 | 2172 | pkt.data.psnr.samples[2] = width * height; |
michael@0 | 2173 | |
michael@0 | 2174 | sse = calc_plane_error(orig->v_buffer, orig->uv_stride, |
michael@0 | 2175 | recon->v_buffer, recon->uv_stride, |
michael@0 | 2176 | width, height); |
michael@0 | 2177 | pkt.data.psnr.sse[0] += sse; |
michael@0 | 2178 | pkt.data.psnr.sse[3] = sse; |
michael@0 | 2179 | pkt.data.psnr.samples[0] += width * height; |
michael@0 | 2180 | pkt.data.psnr.samples[3] = width * height; |
michael@0 | 2181 | |
michael@0 | 2182 | for (i = 0; i < 4; i++) |
michael@0 | 2183 | pkt.data.psnr.psnr[i] = vp9_mse2psnr(pkt.data.psnr.samples[i], 255.0, |
michael@0 | 2184 | (double)pkt.data.psnr.sse[i]); |
michael@0 | 2185 | |
michael@0 | 2186 | vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt); |
michael@0 | 2187 | } |
michael@0 | 2188 | |
michael@0 | 2189 | |
michael@0 | 2190 | int vp9_use_as_reference(VP9_PTR ptr, int ref_frame_flags) { |
michael@0 | 2191 | VP9_COMP *cpi = (VP9_COMP *)(ptr); |
michael@0 | 2192 | |
michael@0 | 2193 | if (ref_frame_flags > 7) |
michael@0 | 2194 | return -1; |
michael@0 | 2195 | |
michael@0 | 2196 | cpi->ref_frame_flags = ref_frame_flags; |
michael@0 | 2197 | return 0; |
michael@0 | 2198 | } |
michael@0 | 2199 | int vp9_update_reference(VP9_PTR ptr, int ref_frame_flags) { |
michael@0 | 2200 | VP9_COMP *cpi = (VP9_COMP *)(ptr); |
michael@0 | 2201 | |
michael@0 | 2202 | if (ref_frame_flags > 7) |
michael@0 | 2203 | return -1; |
michael@0 | 2204 | |
michael@0 | 2205 | cpi->refresh_golden_frame = 0; |
michael@0 | 2206 | cpi->refresh_alt_ref_frame = 0; |
michael@0 | 2207 | cpi->refresh_last_frame = 0; |
michael@0 | 2208 | |
michael@0 | 2209 | if (ref_frame_flags & VP9_LAST_FLAG) |
michael@0 | 2210 | cpi->refresh_last_frame = 1; |
michael@0 | 2211 | |
michael@0 | 2212 | if (ref_frame_flags & VP9_GOLD_FLAG) |
michael@0 | 2213 | cpi->refresh_golden_frame = 1; |
michael@0 | 2214 | |
michael@0 | 2215 | if (ref_frame_flags & VP9_ALT_FLAG) |
michael@0 | 2216 | cpi->refresh_alt_ref_frame = 1; |
michael@0 | 2217 | |
michael@0 | 2218 | return 0; |
michael@0 | 2219 | } |
michael@0 | 2220 | |
michael@0 | 2221 | int vp9_copy_reference_enc(VP9_PTR ptr, VP9_REFFRAME ref_frame_flag, |
michael@0 | 2222 | YV12_BUFFER_CONFIG *sd) { |
michael@0 | 2223 | VP9_COMP *cpi = (VP9_COMP *)(ptr); |
michael@0 | 2224 | VP9_COMMON *cm = &cpi->common; |
michael@0 | 2225 | int ref_fb_idx; |
michael@0 | 2226 | |
michael@0 | 2227 | if (ref_frame_flag == VP9_LAST_FLAG) |
michael@0 | 2228 | ref_fb_idx = cm->ref_frame_map[cpi->lst_fb_idx]; |
michael@0 | 2229 | else if (ref_frame_flag == VP9_GOLD_FLAG) |
michael@0 | 2230 | ref_fb_idx = cm->ref_frame_map[cpi->gld_fb_idx]; |
michael@0 | 2231 | else if (ref_frame_flag == VP9_ALT_FLAG) |
michael@0 | 2232 | ref_fb_idx = cm->ref_frame_map[cpi->alt_fb_idx]; |
michael@0 | 2233 | else |
michael@0 | 2234 | return -1; |
michael@0 | 2235 | |
michael@0 | 2236 | vp8_yv12_copy_frame(&cm->yv12_fb[ref_fb_idx], sd); |
michael@0 | 2237 | |
michael@0 | 2238 | return 0; |
michael@0 | 2239 | } |
michael@0 | 2240 | |
michael@0 | 2241 | int vp9_get_reference_enc(VP9_PTR ptr, int index, YV12_BUFFER_CONFIG **fb) { |
michael@0 | 2242 | VP9_COMP *cpi = (VP9_COMP *)(ptr); |
michael@0 | 2243 | VP9_COMMON *cm = &cpi->common; |
michael@0 | 2244 | |
michael@0 | 2245 | if (index < 0 || index >= NUM_REF_FRAMES) |
michael@0 | 2246 | return -1; |
michael@0 | 2247 | |
michael@0 | 2248 | *fb = &cm->yv12_fb[cm->ref_frame_map[index]]; |
michael@0 | 2249 | return 0; |
michael@0 | 2250 | } |
michael@0 | 2251 | |
michael@0 | 2252 | int vp9_set_reference_enc(VP9_PTR ptr, VP9_REFFRAME ref_frame_flag, |
michael@0 | 2253 | YV12_BUFFER_CONFIG *sd) { |
michael@0 | 2254 | VP9_COMP *cpi = (VP9_COMP *)(ptr); |
michael@0 | 2255 | VP9_COMMON *cm = &cpi->common; |
michael@0 | 2256 | |
michael@0 | 2257 | int ref_fb_idx; |
michael@0 | 2258 | |
michael@0 | 2259 | if (ref_frame_flag == VP9_LAST_FLAG) |
michael@0 | 2260 | ref_fb_idx = cm->ref_frame_map[cpi->lst_fb_idx]; |
michael@0 | 2261 | else if (ref_frame_flag == VP9_GOLD_FLAG) |
michael@0 | 2262 | ref_fb_idx = cm->ref_frame_map[cpi->gld_fb_idx]; |
michael@0 | 2263 | else if (ref_frame_flag == VP9_ALT_FLAG) |
michael@0 | 2264 | ref_fb_idx = cm->ref_frame_map[cpi->alt_fb_idx]; |
michael@0 | 2265 | else |
michael@0 | 2266 | return -1; |
michael@0 | 2267 | |
michael@0 | 2268 | vp8_yv12_copy_frame(sd, &cm->yv12_fb[ref_fb_idx]); |
michael@0 | 2269 | |
michael@0 | 2270 | return 0; |
michael@0 | 2271 | } |
michael@0 | 2272 | int vp9_update_entropy(VP9_PTR comp, int update) { |
michael@0 | 2273 | ((VP9_COMP *)comp)->common.refresh_frame_context = update; |
michael@0 | 2274 | return 0; |
michael@0 | 2275 | } |
michael@0 | 2276 | |
michael@0 | 2277 | |
michael@0 | 2278 | #ifdef OUTPUT_YUV_SRC |
michael@0 | 2279 | void vp9_write_yuv_frame(YV12_BUFFER_CONFIG *s) { |
michael@0 | 2280 | uint8_t *src = s->y_buffer; |
michael@0 | 2281 | int h = s->y_height; |
michael@0 | 2282 | |
michael@0 | 2283 | do { |
michael@0 | 2284 | fwrite(src, s->y_width, 1, yuv_file); |
michael@0 | 2285 | src += s->y_stride; |
michael@0 | 2286 | } while (--h); |
michael@0 | 2287 | |
michael@0 | 2288 | src = s->u_buffer; |
michael@0 | 2289 | h = s->uv_height; |
michael@0 | 2290 | |
michael@0 | 2291 | do { |
michael@0 | 2292 | fwrite(src, s->uv_width, 1, yuv_file); |
michael@0 | 2293 | src += s->uv_stride; |
michael@0 | 2294 | } while (--h); |
michael@0 | 2295 | |
michael@0 | 2296 | src = s->v_buffer; |
michael@0 | 2297 | h = s->uv_height; |
michael@0 | 2298 | |
michael@0 | 2299 | do { |
michael@0 | 2300 | fwrite(src, s->uv_width, 1, yuv_file); |
michael@0 | 2301 | src += s->uv_stride; |
michael@0 | 2302 | } while (--h); |
michael@0 | 2303 | } |
michael@0 | 2304 | #endif |
michael@0 | 2305 | |
michael@0 | 2306 | #ifdef OUTPUT_YUV_REC |
michael@0 | 2307 | void vp9_write_yuv_rec_frame(VP9_COMMON *cm) { |
michael@0 | 2308 | YV12_BUFFER_CONFIG *s = cm->frame_to_show; |
michael@0 | 2309 | uint8_t *src = s->y_buffer; |
michael@0 | 2310 | int h = cm->height; |
michael@0 | 2311 | |
michael@0 | 2312 | do { |
michael@0 | 2313 | fwrite(src, s->y_width, 1, yuv_rec_file); |
michael@0 | 2314 | src += s->y_stride; |
michael@0 | 2315 | } while (--h); |
michael@0 | 2316 | |
michael@0 | 2317 | src = s->u_buffer; |
michael@0 | 2318 | h = s->uv_height; |
michael@0 | 2319 | |
michael@0 | 2320 | do { |
michael@0 | 2321 | fwrite(src, s->uv_width, 1, yuv_rec_file); |
michael@0 | 2322 | src += s->uv_stride; |
michael@0 | 2323 | } while (--h); |
michael@0 | 2324 | |
michael@0 | 2325 | src = s->v_buffer; |
michael@0 | 2326 | h = s->uv_height; |
michael@0 | 2327 | |
michael@0 | 2328 | do { |
michael@0 | 2329 | fwrite(src, s->uv_width, 1, yuv_rec_file); |
michael@0 | 2330 | src += s->uv_stride; |
michael@0 | 2331 | } while (--h); |
michael@0 | 2332 | |
michael@0 | 2333 | #if CONFIG_ALPHA |
michael@0 | 2334 | if (s->alpha_buffer) { |
michael@0 | 2335 | src = s->alpha_buffer; |
michael@0 | 2336 | h = s->alpha_height; |
michael@0 | 2337 | do { |
michael@0 | 2338 | fwrite(src, s->alpha_width, 1, yuv_rec_file); |
michael@0 | 2339 | src += s->alpha_stride; |
michael@0 | 2340 | } while (--h); |
michael@0 | 2341 | } |
michael@0 | 2342 | #endif |
michael@0 | 2343 | |
michael@0 | 2344 | fflush(yuv_rec_file); |
michael@0 | 2345 | } |
michael@0 | 2346 | #endif |
michael@0 | 2347 | |
michael@0 | 2348 | static void scale_and_extend_frame(YV12_BUFFER_CONFIG *src_fb, |
michael@0 | 2349 | YV12_BUFFER_CONFIG *dst_fb) { |
michael@0 | 2350 | const int in_w = src_fb->y_crop_width; |
michael@0 | 2351 | const int in_h = src_fb->y_crop_height; |
michael@0 | 2352 | const int out_w = dst_fb->y_crop_width; |
michael@0 | 2353 | const int out_h = dst_fb->y_crop_height; |
michael@0 | 2354 | int x, y, i; |
michael@0 | 2355 | |
michael@0 | 2356 | uint8_t *srcs[4] = {src_fb->y_buffer, src_fb->u_buffer, src_fb->v_buffer, |
michael@0 | 2357 | src_fb->alpha_buffer}; |
michael@0 | 2358 | int src_strides[4] = {src_fb->y_stride, src_fb->uv_stride, src_fb->uv_stride, |
michael@0 | 2359 | src_fb->alpha_stride}; |
michael@0 | 2360 | |
michael@0 | 2361 | uint8_t *dsts[4] = {dst_fb->y_buffer, dst_fb->u_buffer, dst_fb->v_buffer, |
michael@0 | 2362 | dst_fb->alpha_buffer}; |
michael@0 | 2363 | int dst_strides[4] = {dst_fb->y_stride, dst_fb->uv_stride, dst_fb->uv_stride, |
michael@0 | 2364 | dst_fb->alpha_stride}; |
michael@0 | 2365 | |
michael@0 | 2366 | for (y = 0; y < out_h; y += 16) { |
michael@0 | 2367 | for (x = 0; x < out_w; x += 16) { |
michael@0 | 2368 | for (i = 0; i < MAX_MB_PLANE; ++i) { |
michael@0 | 2369 | const int factor = i == 0 ? 1 : 2; |
michael@0 | 2370 | const int x_q4 = x * (16 / factor) * in_w / out_w; |
michael@0 | 2371 | const int y_q4 = y * (16 / factor) * in_h / out_h; |
michael@0 | 2372 | const int src_stride = src_strides[i]; |
michael@0 | 2373 | const int dst_stride = dst_strides[i]; |
michael@0 | 2374 | uint8_t *src = srcs[i] + y / factor * in_h / out_h * src_stride + |
michael@0 | 2375 | x / factor * in_w / out_w; |
michael@0 | 2376 | uint8_t *dst = dsts[i] + y / factor * dst_stride + x / factor; |
michael@0 | 2377 | |
michael@0 | 2378 | vp9_convolve8(src, src_stride, dst, dst_stride, |
michael@0 | 2379 | vp9_sub_pel_filters_8[x_q4 & 0xf], 16 * in_w / out_w, |
michael@0 | 2380 | vp9_sub_pel_filters_8[y_q4 & 0xf], 16 * in_h / out_h, |
michael@0 | 2381 | 16 / factor, 16 / factor); |
michael@0 | 2382 | } |
michael@0 | 2383 | } |
michael@0 | 2384 | } |
michael@0 | 2385 | |
michael@0 | 2386 | vp8_yv12_extend_frame_borders(dst_fb); |
michael@0 | 2387 | } |
michael@0 | 2388 | |
michael@0 | 2389 | |
michael@0 | 2390 | static void update_alt_ref_frame_stats(VP9_COMP *cpi) { |
michael@0 | 2391 | // this frame refreshes means next frames don't unless specified by user |
michael@0 | 2392 | cpi->frames_since_golden = 0; |
michael@0 | 2393 | |
michael@0 | 2394 | #if CONFIG_MULTIPLE_ARF |
michael@0 | 2395 | if (!cpi->multi_arf_enabled) |
michael@0 | 2396 | #endif |
michael@0 | 2397 | // Clear the alternate reference update pending flag. |
michael@0 | 2398 | cpi->source_alt_ref_pending = 0; |
michael@0 | 2399 | |
michael@0 | 2400 | // Set the alternate reference frame active flag |
michael@0 | 2401 | cpi->source_alt_ref_active = 1; |
michael@0 | 2402 | } |
michael@0 | 2403 | static void update_golden_frame_stats(VP9_COMP *cpi) { |
michael@0 | 2404 | // Update the Golden frame usage counts. |
michael@0 | 2405 | if (cpi->refresh_golden_frame) { |
michael@0 | 2406 | // this frame refreshes means next frames don't unless specified by user |
michael@0 | 2407 | cpi->refresh_golden_frame = 0; |
michael@0 | 2408 | cpi->frames_since_golden = 0; |
michael@0 | 2409 | |
michael@0 | 2410 | // ******** Fixed Q test code only ************ |
michael@0 | 2411 | // If we are going to use the ALT reference for the next group of frames |
michael@0 | 2412 | // set a flag to say so. |
michael@0 | 2413 | if (cpi->oxcf.fixed_q >= 0 && |
michael@0 | 2414 | cpi->oxcf.play_alternate && !cpi->refresh_alt_ref_frame) { |
michael@0 | 2415 | cpi->source_alt_ref_pending = 1; |
michael@0 | 2416 | cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; |
michael@0 | 2417 | |
michael@0 | 2418 | // TODO(ivan): For SVC encoder, GF automatic update is disabled by using |
michael@0 | 2419 | // a large GF_interval. |
michael@0 | 2420 | if (cpi->use_svc) { |
michael@0 | 2421 | cpi->frames_till_gf_update_due = INT_MAX; |
michael@0 | 2422 | } |
michael@0 | 2423 | } |
michael@0 | 2424 | |
michael@0 | 2425 | if (!cpi->source_alt_ref_pending) |
michael@0 | 2426 | cpi->source_alt_ref_active = 0; |
michael@0 | 2427 | |
michael@0 | 2428 | // Decrement count down till next gf |
michael@0 | 2429 | if (cpi->frames_till_gf_update_due > 0) |
michael@0 | 2430 | cpi->frames_till_gf_update_due--; |
michael@0 | 2431 | |
michael@0 | 2432 | } else if (!cpi->refresh_alt_ref_frame) { |
michael@0 | 2433 | // Decrement count down till next gf |
michael@0 | 2434 | if (cpi->frames_till_gf_update_due > 0) |
michael@0 | 2435 | cpi->frames_till_gf_update_due--; |
michael@0 | 2436 | |
michael@0 | 2437 | if (cpi->frames_till_alt_ref_frame) |
michael@0 | 2438 | cpi->frames_till_alt_ref_frame--; |
michael@0 | 2439 | |
michael@0 | 2440 | cpi->frames_since_golden++; |
michael@0 | 2441 | } |
michael@0 | 2442 | } |
michael@0 | 2443 | |
michael@0 | 2444 | static int find_fp_qindex() { |
michael@0 | 2445 | int i; |
michael@0 | 2446 | |
michael@0 | 2447 | for (i = 0; i < QINDEX_RANGE; i++) { |
michael@0 | 2448 | if (vp9_convert_qindex_to_q(i) >= 30.0) { |
michael@0 | 2449 | break; |
michael@0 | 2450 | } |
michael@0 | 2451 | } |
michael@0 | 2452 | |
michael@0 | 2453 | if (i == QINDEX_RANGE) |
michael@0 | 2454 | i--; |
michael@0 | 2455 | |
michael@0 | 2456 | return i; |
michael@0 | 2457 | } |
michael@0 | 2458 | |
michael@0 | 2459 | static void Pass1Encode(VP9_COMP *cpi, unsigned long *size, unsigned char *dest, |
michael@0 | 2460 | unsigned int *frame_flags) { |
michael@0 | 2461 | (void) size; |
michael@0 | 2462 | (void) dest; |
michael@0 | 2463 | (void) frame_flags; |
michael@0 | 2464 | |
michael@0 | 2465 | vp9_set_quantizer(cpi, find_fp_qindex()); |
michael@0 | 2466 | vp9_first_pass(cpi); |
michael@0 | 2467 | } |
michael@0 | 2468 | |
michael@0 | 2469 | #define WRITE_RECON_BUFFER 0 |
michael@0 | 2470 | #if WRITE_RECON_BUFFER |
michael@0 | 2471 | void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame) { |
michael@0 | 2472 | FILE *yframe; |
michael@0 | 2473 | int i; |
michael@0 | 2474 | char filename[255]; |
michael@0 | 2475 | |
michael@0 | 2476 | snprintf(filename, sizeof(filename), "cx\\y%04d.raw", this_frame); |
michael@0 | 2477 | yframe = fopen(filename, "wb"); |
michael@0 | 2478 | |
michael@0 | 2479 | for (i = 0; i < frame->y_height; i++) |
michael@0 | 2480 | fwrite(frame->y_buffer + i * frame->y_stride, |
michael@0 | 2481 | frame->y_width, 1, yframe); |
michael@0 | 2482 | |
michael@0 | 2483 | fclose(yframe); |
michael@0 | 2484 | snprintf(filename, sizeof(filename), "cx\\u%04d.raw", this_frame); |
michael@0 | 2485 | yframe = fopen(filename, "wb"); |
michael@0 | 2486 | |
michael@0 | 2487 | for (i = 0; i < frame->uv_height; i++) |
michael@0 | 2488 | fwrite(frame->u_buffer + i * frame->uv_stride, |
michael@0 | 2489 | frame->uv_width, 1, yframe); |
michael@0 | 2490 | |
michael@0 | 2491 | fclose(yframe); |
michael@0 | 2492 | snprintf(filename, sizeof(filename), "cx\\v%04d.raw", this_frame); |
michael@0 | 2493 | yframe = fopen(filename, "wb"); |
michael@0 | 2494 | |
michael@0 | 2495 | for (i = 0; i < frame->uv_height; i++) |
michael@0 | 2496 | fwrite(frame->v_buffer + i * frame->uv_stride, |
michael@0 | 2497 | frame->uv_width, 1, yframe); |
michael@0 | 2498 | |
michael@0 | 2499 | fclose(yframe); |
michael@0 | 2500 | } |
michael@0 | 2501 | #endif |
michael@0 | 2502 | |
michael@0 | 2503 | static double compute_edge_pixel_proportion(YV12_BUFFER_CONFIG *frame) { |
michael@0 | 2504 | #define EDGE_THRESH 128 |
michael@0 | 2505 | int i, j; |
michael@0 | 2506 | int num_edge_pels = 0; |
michael@0 | 2507 | int num_pels = (frame->y_height - 2) * (frame->y_width - 2); |
michael@0 | 2508 | uint8_t *prev = frame->y_buffer + 1; |
michael@0 | 2509 | uint8_t *curr = frame->y_buffer + 1 + frame->y_stride; |
michael@0 | 2510 | uint8_t *next = frame->y_buffer + 1 + 2 * frame->y_stride; |
michael@0 | 2511 | for (i = 1; i < frame->y_height - 1; i++) { |
michael@0 | 2512 | for (j = 1; j < frame->y_width - 1; j++) { |
michael@0 | 2513 | /* Sobel hor and ver gradients */ |
michael@0 | 2514 | int v = 2 * (curr[1] - curr[-1]) + (prev[1] - prev[-1]) + |
michael@0 | 2515 | (next[1] - next[-1]); |
michael@0 | 2516 | int h = 2 * (prev[0] - next[0]) + (prev[1] - next[1]) + |
michael@0 | 2517 | (prev[-1] - next[-1]); |
michael@0 | 2518 | h = (h < 0 ? -h : h); |
michael@0 | 2519 | v = (v < 0 ? -v : v); |
michael@0 | 2520 | if (h > EDGE_THRESH || v > EDGE_THRESH) |
michael@0 | 2521 | num_edge_pels++; |
michael@0 | 2522 | curr++; |
michael@0 | 2523 | prev++; |
michael@0 | 2524 | next++; |
michael@0 | 2525 | } |
michael@0 | 2526 | curr += frame->y_stride - frame->y_width + 2; |
michael@0 | 2527 | prev += frame->y_stride - frame->y_width + 2; |
michael@0 | 2528 | next += frame->y_stride - frame->y_width + 2; |
michael@0 | 2529 | } |
michael@0 | 2530 | return (double)num_edge_pels / num_pels; |
michael@0 | 2531 | } |
michael@0 | 2532 | |
michael@0 | 2533 | // Function to test for conditions that indicate we should loop |
michael@0 | 2534 | // back and recode a frame. |
michael@0 | 2535 | static int recode_loop_test(VP9_COMP *cpi, |
michael@0 | 2536 | int high_limit, int low_limit, |
michael@0 | 2537 | int q, int maxq, int minq) { |
michael@0 | 2538 | int force_recode = 0; |
michael@0 | 2539 | VP9_COMMON *cm = &cpi->common; |
michael@0 | 2540 | |
michael@0 | 2541 | // Is frame recode allowed at all |
michael@0 | 2542 | // Yes if either recode mode 1 is selected or mode two is selected |
michael@0 | 2543 | // and the frame is a key frame. golden frame or alt_ref_frame |
michael@0 | 2544 | if ((cpi->sf.recode_loop == 1) || |
michael@0 | 2545 | ((cpi->sf.recode_loop == 2) && |
michael@0 | 2546 | ((cm->frame_type == KEY_FRAME) || |
michael@0 | 2547 | cpi->refresh_golden_frame || |
michael@0 | 2548 | cpi->refresh_alt_ref_frame))) { |
michael@0 | 2549 | // General over and under shoot tests |
michael@0 | 2550 | if (((cpi->projected_frame_size > high_limit) && (q < maxq)) || |
michael@0 | 2551 | ((cpi->projected_frame_size < low_limit) && (q > minq))) { |
michael@0 | 2552 | force_recode = 1; |
michael@0 | 2553 | } else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) { |
michael@0 | 2554 | // Deal with frame undershoot and whether or not we are |
michael@0 | 2555 | // below the automatically set cq level. |
michael@0 | 2556 | if (q > cpi->cq_target_quality && |
michael@0 | 2557 | cpi->projected_frame_size < ((cpi->this_frame_target * 7) >> 3)) { |
michael@0 | 2558 | force_recode = 1; |
michael@0 | 2559 | } else if (q > cpi->oxcf.cq_level && |
michael@0 | 2560 | cpi->projected_frame_size < cpi->min_frame_bandwidth && |
michael@0 | 2561 | cpi->active_best_quality > cpi->oxcf.cq_level) { |
michael@0 | 2562 | // Severe undershoot and between auto and user cq level |
michael@0 | 2563 | force_recode = 1; |
michael@0 | 2564 | cpi->active_best_quality = cpi->oxcf.cq_level; |
michael@0 | 2565 | } |
michael@0 | 2566 | } |
michael@0 | 2567 | } |
michael@0 | 2568 | |
michael@0 | 2569 | return force_recode; |
michael@0 | 2570 | } |
michael@0 | 2571 | |
michael@0 | 2572 | static void update_reference_frames(VP9_COMP * const cpi) { |
michael@0 | 2573 | VP9_COMMON * const cm = &cpi->common; |
michael@0 | 2574 | |
michael@0 | 2575 | // At this point the new frame has been encoded. |
michael@0 | 2576 | // If any buffer copy / swapping is signaled it should be done here. |
michael@0 | 2577 | if (cm->frame_type == KEY_FRAME) { |
michael@0 | 2578 | ref_cnt_fb(cm->fb_idx_ref_cnt, |
michael@0 | 2579 | &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx); |
michael@0 | 2580 | ref_cnt_fb(cm->fb_idx_ref_cnt, |
michael@0 | 2581 | &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx); |
michael@0 | 2582 | } |
michael@0 | 2583 | #if CONFIG_MULTIPLE_ARF |
michael@0 | 2584 | else if (!cpi->multi_arf_enabled && cpi->refresh_golden_frame && |
michael@0 | 2585 | !cpi->refresh_alt_ref_frame) { |
michael@0 | 2586 | #else |
michael@0 | 2587 | else if (cpi->refresh_golden_frame && !cpi->refresh_alt_ref_frame && |
michael@0 | 2588 | !cpi->use_svc) { |
michael@0 | 2589 | #endif |
michael@0 | 2590 | /* Preserve the previously existing golden frame and update the frame in |
michael@0 | 2591 | * the alt ref slot instead. This is highly specific to the current use of |
michael@0 | 2592 | * alt-ref as a forward reference, and this needs to be generalized as |
michael@0 | 2593 | * other uses are implemented (like RTC/temporal scaling) |
michael@0 | 2594 | * |
michael@0 | 2595 | * The update to the buffer in the alt ref slot was signaled in |
michael@0 | 2596 | * vp9_pack_bitstream(), now swap the buffer pointers so that it's treated |
michael@0 | 2597 | * as the golden frame next time. |
michael@0 | 2598 | */ |
michael@0 | 2599 | int tmp; |
michael@0 | 2600 | |
michael@0 | 2601 | ref_cnt_fb(cm->fb_idx_ref_cnt, |
michael@0 | 2602 | &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx); |
michael@0 | 2603 | |
michael@0 | 2604 | tmp = cpi->alt_fb_idx; |
michael@0 | 2605 | cpi->alt_fb_idx = cpi->gld_fb_idx; |
michael@0 | 2606 | cpi->gld_fb_idx = tmp; |
michael@0 | 2607 | } else { /* For non key/golden frames */ |
michael@0 | 2608 | if (cpi->refresh_alt_ref_frame) { |
michael@0 | 2609 | int arf_idx = cpi->alt_fb_idx; |
michael@0 | 2610 | #if CONFIG_MULTIPLE_ARF |
michael@0 | 2611 | if (cpi->multi_arf_enabled) { |
michael@0 | 2612 | arf_idx = cpi->arf_buffer_idx[cpi->sequence_number + 1]; |
michael@0 | 2613 | } |
michael@0 | 2614 | #endif |
michael@0 | 2615 | ref_cnt_fb(cm->fb_idx_ref_cnt, |
michael@0 | 2616 | &cm->ref_frame_map[arf_idx], cm->new_fb_idx); |
michael@0 | 2617 | } |
michael@0 | 2618 | |
michael@0 | 2619 | if (cpi->refresh_golden_frame) { |
michael@0 | 2620 | ref_cnt_fb(cm->fb_idx_ref_cnt, |
michael@0 | 2621 | &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx); |
michael@0 | 2622 | } |
michael@0 | 2623 | } |
michael@0 | 2624 | |
michael@0 | 2625 | if (cpi->refresh_last_frame) { |
michael@0 | 2626 | ref_cnt_fb(cm->fb_idx_ref_cnt, |
michael@0 | 2627 | &cm->ref_frame_map[cpi->lst_fb_idx], cm->new_fb_idx); |
michael@0 | 2628 | } |
michael@0 | 2629 | } |
michael@0 | 2630 | |
michael@0 | 2631 | static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) { |
michael@0 | 2632 | MACROBLOCKD *xd = &cpi->mb.e_mbd; |
michael@0 | 2633 | struct loopfilter *lf = &cm->lf; |
michael@0 | 2634 | if (xd->lossless) { |
michael@0 | 2635 | lf->filter_level = 0; |
michael@0 | 2636 | } else { |
michael@0 | 2637 | struct vpx_usec_timer timer; |
michael@0 | 2638 | |
michael@0 | 2639 | vp9_clear_system_state(); |
michael@0 | 2640 | |
michael@0 | 2641 | vpx_usec_timer_start(&timer); |
michael@0 | 2642 | |
michael@0 | 2643 | vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.use_fast_lpf_pick); |
michael@0 | 2644 | |
michael@0 | 2645 | vpx_usec_timer_mark(&timer); |
michael@0 | 2646 | cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer); |
michael@0 | 2647 | } |
michael@0 | 2648 | |
michael@0 | 2649 | if (lf->filter_level > 0) { |
michael@0 | 2650 | vp9_set_alt_lf_level(cpi, lf->filter_level); |
michael@0 | 2651 | vp9_loop_filter_frame(cm, xd, lf->filter_level, 0, 0); |
michael@0 | 2652 | } |
michael@0 | 2653 | |
michael@0 | 2654 | vp9_extend_frame_inner_borders(cm->frame_to_show, |
michael@0 | 2655 | cm->subsampling_x, cm->subsampling_y); |
michael@0 | 2656 | } |
michael@0 | 2657 | |
michael@0 | 2658 | static void scale_references(VP9_COMP *cpi) { |
michael@0 | 2659 | VP9_COMMON *cm = &cpi->common; |
michael@0 | 2660 | int i; |
michael@0 | 2661 | int refs[ALLOWED_REFS_PER_FRAME] = {cpi->lst_fb_idx, cpi->gld_fb_idx, |
michael@0 | 2662 | cpi->alt_fb_idx}; |
michael@0 | 2663 | |
michael@0 | 2664 | for (i = 0; i < 3; i++) { |
michael@0 | 2665 | YV12_BUFFER_CONFIG *ref = &cm->yv12_fb[cm->ref_frame_map[refs[i]]]; |
michael@0 | 2666 | |
michael@0 | 2667 | if (ref->y_crop_width != cm->width || |
michael@0 | 2668 | ref->y_crop_height != cm->height) { |
michael@0 | 2669 | int new_fb = get_free_fb(cm); |
michael@0 | 2670 | |
michael@0 | 2671 | vp9_realloc_frame_buffer(&cm->yv12_fb[new_fb], |
michael@0 | 2672 | cm->width, cm->height, |
michael@0 | 2673 | cm->subsampling_x, cm->subsampling_y, |
michael@0 | 2674 | VP9BORDERINPIXELS); |
michael@0 | 2675 | scale_and_extend_frame(ref, &cm->yv12_fb[new_fb]); |
michael@0 | 2676 | cpi->scaled_ref_idx[i] = new_fb; |
michael@0 | 2677 | } else { |
michael@0 | 2678 | cpi->scaled_ref_idx[i] = cm->ref_frame_map[refs[i]]; |
michael@0 | 2679 | cm->fb_idx_ref_cnt[cm->ref_frame_map[refs[i]]]++; |
michael@0 | 2680 | } |
michael@0 | 2681 | } |
michael@0 | 2682 | } |
michael@0 | 2683 | |
michael@0 | 2684 | static void release_scaled_references(VP9_COMP *cpi) { |
michael@0 | 2685 | VP9_COMMON *cm = &cpi->common; |
michael@0 | 2686 | int i; |
michael@0 | 2687 | |
michael@0 | 2688 | for (i = 0; i < 3; i++) |
michael@0 | 2689 | cm->fb_idx_ref_cnt[cpi->scaled_ref_idx[i]]--; |
michael@0 | 2690 | } |
michael@0 | 2691 | |
michael@0 | 2692 | static void full_to_model_count(unsigned int *model_count, |
michael@0 | 2693 | unsigned int *full_count) { |
michael@0 | 2694 | int n; |
michael@0 | 2695 | model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN]; |
michael@0 | 2696 | model_count[ONE_TOKEN] = full_count[ONE_TOKEN]; |
michael@0 | 2697 | model_count[TWO_TOKEN] = full_count[TWO_TOKEN]; |
michael@0 | 2698 | for (n = THREE_TOKEN; n < DCT_EOB_TOKEN; ++n) |
michael@0 | 2699 | model_count[TWO_TOKEN] += full_count[n]; |
michael@0 | 2700 | model_count[DCT_EOB_MODEL_TOKEN] = full_count[DCT_EOB_TOKEN]; |
michael@0 | 2701 | } |
michael@0 | 2702 | |
michael@0 | 2703 | static void full_to_model_counts( |
michael@0 | 2704 | vp9_coeff_count_model *model_count, vp9_coeff_count *full_count) { |
michael@0 | 2705 | int i, j, k, l; |
michael@0 | 2706 | for (i = 0; i < BLOCK_TYPES; ++i) |
michael@0 | 2707 | for (j = 0; j < REF_TYPES; ++j) |
michael@0 | 2708 | for (k = 0; k < COEF_BANDS; ++k) |
michael@0 | 2709 | for (l = 0; l < PREV_COEF_CONTEXTS; ++l) { |
michael@0 | 2710 | if (l >= 3 && k == 0) |
michael@0 | 2711 | continue; |
michael@0 | 2712 | full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]); |
michael@0 | 2713 | } |
michael@0 | 2714 | } |
michael@0 | 2715 | |
michael@0 | 2716 | #if 0 && CONFIG_INTERNAL_STATS |
michael@0 | 2717 | static void output_frame_level_debug_stats(VP9_COMP *cpi) { |
michael@0 | 2718 | VP9_COMMON *const cm = &cpi->common; |
michael@0 | 2719 | FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w"); |
michael@0 | 2720 | int recon_err; |
michael@0 | 2721 | |
michael@0 | 2722 | vp9_clear_system_state(); // __asm emms; |
michael@0 | 2723 | |
michael@0 | 2724 | recon_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm)); |
michael@0 | 2725 | |
michael@0 | 2726 | if (cpi->twopass.total_left_stats.coded_error != 0.0) |
michael@0 | 2727 | fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %10d" |
michael@0 | 2728 | "%7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f" |
michael@0 | 2729 | "%6d %6d %5d %5d %5d %8.2f %10d %10.3f" |
michael@0 | 2730 | "%10.3f %8d %10d %10d %10d\n", |
michael@0 | 2731 | cpi->common.current_video_frame, cpi->this_frame_target, |
michael@0 | 2732 | cpi->projected_frame_size, 0, |
michael@0 | 2733 | (cpi->projected_frame_size - cpi->this_frame_target), |
michael@0 | 2734 | (int)cpi->total_target_vs_actual, |
michael@0 | 2735 | (int)(cpi->oxcf.starting_buffer_level - cpi->bits_off_target), |
michael@0 | 2736 | (int)cpi->total_actual_bits, cm->base_qindex, |
michael@0 | 2737 | vp9_convert_qindex_to_q(cm->base_qindex), |
michael@0 | 2738 | (double)vp9_dc_quant(cm->base_qindex, 0) / 4.0, |
michael@0 | 2739 | vp9_convert_qindex_to_q(cpi->active_best_quality), |
michael@0 | 2740 | vp9_convert_qindex_to_q(cpi->active_worst_quality), cpi->avg_q, |
michael@0 | 2741 | vp9_convert_qindex_to_q(cpi->ni_av_qi), |
michael@0 | 2742 | vp9_convert_qindex_to_q(cpi->cq_target_quality), |
michael@0 | 2743 | cpi->refresh_last_frame, cpi->refresh_golden_frame, |
michael@0 | 2744 | cpi->refresh_alt_ref_frame, cm->frame_type, cpi->gfu_boost, |
michael@0 | 2745 | cpi->twopass.est_max_qcorrection_factor, (int)cpi->twopass.bits_left, |
michael@0 | 2746 | cpi->twopass.total_left_stats.coded_error, |
michael@0 | 2747 | (double)cpi->twopass.bits_left / |
michael@0 | 2748 | (1 + cpi->twopass.total_left_stats.coded_error), |
michael@0 | 2749 | cpi->tot_recode_hits, recon_err, cpi->kf_boost, cpi->kf_zeromotion_pct); |
michael@0 | 2750 | |
michael@0 | 2751 | fclose(f); |
michael@0 | 2752 | |
michael@0 | 2753 | if (0) { |
michael@0 | 2754 | FILE *const fmodes = fopen("Modes.stt", "a"); |
michael@0 | 2755 | int i; |
michael@0 | 2756 | |
michael@0 | 2757 | fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame, |
michael@0 | 2758 | cm->frame_type, cpi->refresh_golden_frame, |
michael@0 | 2759 | cpi->refresh_alt_ref_frame); |
michael@0 | 2760 | |
michael@0 | 2761 | for (i = 0; i < MAX_MODES; ++i) |
michael@0 | 2762 | fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]); |
michael@0 | 2763 | for (i = 0; i < MAX_REFS; ++i) |
michael@0 | 2764 | fprintf(fmodes, "%5d ", cpi->sub8x8_mode_chosen_counts[i]); |
michael@0 | 2765 | |
michael@0 | 2766 | fprintf(fmodes, "\n"); |
michael@0 | 2767 | |
michael@0 | 2768 | fclose(fmodes); |
michael@0 | 2769 | } |
michael@0 | 2770 | } |
michael@0 | 2771 | #endif |
michael@0 | 2772 | |
michael@0 | 2773 | static int pick_q_and_adjust_q_bounds(VP9_COMP *cpi, |
michael@0 | 2774 | int * bottom_index, int * top_index) { |
michael@0 | 2775 | // Set an active best quality and if necessary active worst quality |
michael@0 | 2776 | int q = cpi->active_worst_quality; |
michael@0 | 2777 | VP9_COMMON *const cm = &cpi->common; |
michael@0 | 2778 | |
michael@0 | 2779 | if (frame_is_intra_only(cm)) { |
michael@0 | 2780 | #if !CONFIG_MULTIPLE_ARF |
michael@0 | 2781 | // Handle the special case for key frames forced when we have75 reached |
michael@0 | 2782 | // the maximum key frame interval. Here force the Q to a range |
michael@0 | 2783 | // based on the ambient Q to reduce the risk of popping. |
michael@0 | 2784 | if (cpi->this_key_frame_forced) { |
michael@0 | 2785 | int delta_qindex; |
michael@0 | 2786 | int qindex = cpi->last_boosted_qindex; |
michael@0 | 2787 | double last_boosted_q = vp9_convert_qindex_to_q(qindex); |
michael@0 | 2788 | |
michael@0 | 2789 | delta_qindex = vp9_compute_qdelta(cpi, last_boosted_q, |
michael@0 | 2790 | (last_boosted_q * 0.75)); |
michael@0 | 2791 | |
michael@0 | 2792 | cpi->active_best_quality = MAX(qindex + delta_qindex, |
michael@0 | 2793 | cpi->best_quality); |
michael@0 | 2794 | } else { |
michael@0 | 2795 | int high = 5000; |
michael@0 | 2796 | int low = 400; |
michael@0 | 2797 | double q_adj_factor = 1.0; |
michael@0 | 2798 | double q_val; |
michael@0 | 2799 | |
michael@0 | 2800 | // Baseline value derived from cpi->active_worst_quality and kf boost |
michael@0 | 2801 | cpi->active_best_quality = get_active_quality(q, cpi->kf_boost, |
michael@0 | 2802 | low, high, |
michael@0 | 2803 | kf_low_motion_minq, |
michael@0 | 2804 | kf_high_motion_minq); |
michael@0 | 2805 | |
michael@0 | 2806 | // Allow somewhat lower kf minq with small image formats. |
michael@0 | 2807 | if ((cm->width * cm->height) <= (352 * 288)) { |
michael@0 | 2808 | q_adj_factor -= 0.25; |
michael@0 | 2809 | } |
michael@0 | 2810 | |
michael@0 | 2811 | // Make a further adjustment based on the kf zero motion measure. |
michael@0 | 2812 | q_adj_factor += 0.05 - (0.001 * (double)cpi->kf_zeromotion_pct); |
michael@0 | 2813 | |
michael@0 | 2814 | // Convert the adjustment factor to a qindex delta |
michael@0 | 2815 | // on active_best_quality. |
michael@0 | 2816 | q_val = vp9_convert_qindex_to_q(cpi->active_best_quality); |
michael@0 | 2817 | cpi->active_best_quality += |
michael@0 | 2818 | vp9_compute_qdelta(cpi, q_val, (q_val * q_adj_factor)); |
michael@0 | 2819 | } |
michael@0 | 2820 | #else |
michael@0 | 2821 | double current_q; |
michael@0 | 2822 | // Force the KF quantizer to be 30% of the active_worst_quality. |
michael@0 | 2823 | current_q = vp9_convert_qindex_to_q(cpi->active_worst_quality); |
michael@0 | 2824 | cpi->active_best_quality = cpi->active_worst_quality |
michael@0 | 2825 | + vp9_compute_qdelta(cpi, current_q, current_q * 0.3); |
michael@0 | 2826 | #endif |
michael@0 | 2827 | } else if (!cpi->is_src_frame_alt_ref && |
michael@0 | 2828 | (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { |
michael@0 | 2829 | int high = 2000; |
michael@0 | 2830 | int low = 400; |
michael@0 | 2831 | |
michael@0 | 2832 | // Use the lower of cpi->active_worst_quality and recent |
michael@0 | 2833 | // average Q as basis for GF/ARF best Q limit unless last frame was |
michael@0 | 2834 | // a key frame. |
michael@0 | 2835 | if (cpi->frames_since_key > 1 && |
michael@0 | 2836 | cpi->avg_frame_qindex < cpi->active_worst_quality) { |
michael@0 | 2837 | q = cpi->avg_frame_qindex; |
michael@0 | 2838 | } |
michael@0 | 2839 | // For constrained quality dont allow Q less than the cq level |
michael@0 | 2840 | if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) { |
michael@0 | 2841 | if (q < cpi->cq_target_quality) |
michael@0 | 2842 | q = cpi->cq_target_quality; |
michael@0 | 2843 | if (cpi->frames_since_key > 1) { |
michael@0 | 2844 | cpi->active_best_quality = get_active_quality(q, cpi->gfu_boost, |
michael@0 | 2845 | low, high, |
michael@0 | 2846 | afq_low_motion_minq, |
michael@0 | 2847 | afq_high_motion_minq); |
michael@0 | 2848 | } else { |
michael@0 | 2849 | cpi->active_best_quality = get_active_quality(q, cpi->gfu_boost, |
michael@0 | 2850 | low, high, |
michael@0 | 2851 | gf_low_motion_minq, |
michael@0 | 2852 | gf_high_motion_minq); |
michael@0 | 2853 | } |
michael@0 | 2854 | // Constrained quality use slightly lower active best. |
michael@0 | 2855 | cpi->active_best_quality = cpi->active_best_quality * 15 / 16; |
michael@0 | 2856 | |
michael@0 | 2857 | } else if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { |
michael@0 | 2858 | if (!cpi->refresh_alt_ref_frame) { |
michael@0 | 2859 | cpi->active_best_quality = cpi->cq_target_quality; |
michael@0 | 2860 | } else { |
michael@0 | 2861 | if (cpi->frames_since_key > 1) { |
michael@0 | 2862 | cpi->active_best_quality = get_active_quality(q, cpi->gfu_boost, |
michael@0 | 2863 | low, high, |
michael@0 | 2864 | afq_low_motion_minq, |
michael@0 | 2865 | afq_high_motion_minq); |
michael@0 | 2866 | } else { |
michael@0 | 2867 | cpi->active_best_quality = get_active_quality(q, cpi->gfu_boost, |
michael@0 | 2868 | low, high, |
michael@0 | 2869 | gf_low_motion_minq, |
michael@0 | 2870 | gf_high_motion_minq); |
michael@0 | 2871 | } |
michael@0 | 2872 | } |
michael@0 | 2873 | } else { |
michael@0 | 2874 | cpi->active_best_quality = get_active_quality(q, cpi->gfu_boost, |
michael@0 | 2875 | low, high, |
michael@0 | 2876 | gf_low_motion_minq, |
michael@0 | 2877 | gf_high_motion_minq); |
michael@0 | 2878 | } |
michael@0 | 2879 | } else { |
michael@0 | 2880 | if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { |
michael@0 | 2881 | cpi->active_best_quality = cpi->cq_target_quality; |
michael@0 | 2882 | } else { |
michael@0 | 2883 | cpi->active_best_quality = inter_minq[q]; |
michael@0 | 2884 | // 1-pass: for now, use the average Q for the active_best, if its lower |
michael@0 | 2885 | // than active_worst. |
michael@0 | 2886 | if (cpi->pass == 0 && (cpi->avg_frame_qindex < q)) |
michael@0 | 2887 | cpi->active_best_quality = inter_minq[cpi->avg_frame_qindex]; |
michael@0 | 2888 | |
michael@0 | 2889 | // For the constrained quality mode we don't want |
michael@0 | 2890 | // q to fall below the cq level. |
michael@0 | 2891 | if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) && |
michael@0 | 2892 | (cpi->active_best_quality < cpi->cq_target_quality)) { |
michael@0 | 2893 | // If we are strongly undershooting the target rate in the last |
michael@0 | 2894 | // frames then use the user passed in cq value not the auto |
michael@0 | 2895 | // cq value. |
michael@0 | 2896 | if (cpi->rolling_actual_bits < cpi->min_frame_bandwidth) |
michael@0 | 2897 | cpi->active_best_quality = cpi->oxcf.cq_level; |
michael@0 | 2898 | else |
michael@0 | 2899 | cpi->active_best_quality = cpi->cq_target_quality; |
michael@0 | 2900 | } |
michael@0 | 2901 | } |
michael@0 | 2902 | } |
michael@0 | 2903 | |
michael@0 | 2904 | // Clip the active best and worst quality values to limits |
michael@0 | 2905 | if (cpi->active_worst_quality > cpi->worst_quality) |
michael@0 | 2906 | cpi->active_worst_quality = cpi->worst_quality; |
michael@0 | 2907 | |
michael@0 | 2908 | if (cpi->active_best_quality < cpi->best_quality) |
michael@0 | 2909 | cpi->active_best_quality = cpi->best_quality; |
michael@0 | 2910 | |
michael@0 | 2911 | if (cpi->active_best_quality > cpi->worst_quality) |
michael@0 | 2912 | cpi->active_best_quality = cpi->worst_quality; |
michael@0 | 2913 | |
michael@0 | 2914 | if (cpi->active_worst_quality < cpi->active_best_quality) |
michael@0 | 2915 | cpi->active_worst_quality = cpi->active_best_quality; |
michael@0 | 2916 | |
michael@0 | 2917 | // Limit Q range for the adaptive loop. |
michael@0 | 2918 | if (cm->frame_type == KEY_FRAME && !cpi->this_key_frame_forced) { |
michael@0 | 2919 | *top_index = |
michael@0 | 2920 | (cpi->active_worst_quality + cpi->active_best_quality * 3) / 4; |
michael@0 | 2921 | // If this is the first (key) frame in 1-pass, active best is the user |
michael@0 | 2922 | // best-allowed, and leave the top_index to active_worst. |
michael@0 | 2923 | if (cpi->pass == 0 && cpi->common.current_video_frame == 0) { |
michael@0 | 2924 | cpi->active_best_quality = cpi->oxcf.best_allowed_q; |
michael@0 | 2925 | *top_index = cpi->oxcf.worst_allowed_q; |
michael@0 | 2926 | } |
michael@0 | 2927 | } else if (!cpi->is_src_frame_alt_ref && |
michael@0 | 2928 | (cpi->oxcf.end_usage != USAGE_STREAM_FROM_SERVER) && |
michael@0 | 2929 | (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { |
michael@0 | 2930 | *top_index = |
michael@0 | 2931 | (cpi->active_worst_quality + cpi->active_best_quality) / 2; |
michael@0 | 2932 | } else { |
michael@0 | 2933 | *top_index = cpi->active_worst_quality; |
michael@0 | 2934 | } |
michael@0 | 2935 | *bottom_index = cpi->active_best_quality; |
michael@0 | 2936 | |
michael@0 | 2937 | if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { |
michael@0 | 2938 | q = cpi->active_best_quality; |
michael@0 | 2939 | // Special case code to try and match quality with forced key frames |
michael@0 | 2940 | } else if ((cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced) { |
michael@0 | 2941 | q = cpi->last_boosted_qindex; |
michael@0 | 2942 | } else { |
michael@0 | 2943 | // Determine initial Q to try. |
michael@0 | 2944 | if (cpi->pass == 0) { |
michael@0 | 2945 | // 1-pass: for now, use per-frame-bw for target size of frame, scaled |
michael@0 | 2946 | // by |x| for key frame. |
michael@0 | 2947 | int scale = (cm->frame_type == KEY_FRAME) ? 5 : 1; |
michael@0 | 2948 | q = vp9_regulate_q(cpi, scale * cpi->av_per_frame_bandwidth); |
michael@0 | 2949 | } else { |
michael@0 | 2950 | q = vp9_regulate_q(cpi, cpi->this_frame_target); |
michael@0 | 2951 | } |
michael@0 | 2952 | if (q > *top_index) |
michael@0 | 2953 | q = *top_index; |
michael@0 | 2954 | } |
michael@0 | 2955 | |
michael@0 | 2956 | return q; |
michael@0 | 2957 | } |
michael@0 | 2958 | static void encode_frame_to_data_rate(VP9_COMP *cpi, |
michael@0 | 2959 | unsigned long *size, |
michael@0 | 2960 | unsigned char *dest, |
michael@0 | 2961 | unsigned int *frame_flags) { |
michael@0 | 2962 | VP9_COMMON *const cm = &cpi->common; |
michael@0 | 2963 | TX_SIZE t; |
michael@0 | 2964 | int q; |
michael@0 | 2965 | int frame_over_shoot_limit; |
michael@0 | 2966 | int frame_under_shoot_limit; |
michael@0 | 2967 | |
michael@0 | 2968 | int loop = 0; |
michael@0 | 2969 | int loop_count; |
michael@0 | 2970 | |
michael@0 | 2971 | int q_low; |
michael@0 | 2972 | int q_high; |
michael@0 | 2973 | |
michael@0 | 2974 | int top_index; |
michael@0 | 2975 | int bottom_index; |
michael@0 | 2976 | int active_worst_qchanged = 0; |
michael@0 | 2977 | |
michael@0 | 2978 | int overshoot_seen = 0; |
michael@0 | 2979 | int undershoot_seen = 0; |
michael@0 | 2980 | |
michael@0 | 2981 | SPEED_FEATURES *const sf = &cpi->sf; |
michael@0 | 2982 | unsigned int max_mv_def = MIN(cpi->common.width, cpi->common.height); |
michael@0 | 2983 | struct segmentation *const seg = &cm->seg; |
michael@0 | 2984 | |
michael@0 | 2985 | /* Scale the source buffer, if required. */ |
michael@0 | 2986 | if (cm->mi_cols * 8 != cpi->un_scaled_source->y_width || |
michael@0 | 2987 | cm->mi_rows * 8 != cpi->un_scaled_source->y_height) { |
michael@0 | 2988 | scale_and_extend_frame(cpi->un_scaled_source, &cpi->scaled_source); |
michael@0 | 2989 | cpi->Source = &cpi->scaled_source; |
michael@0 | 2990 | } else { |
michael@0 | 2991 | cpi->Source = cpi->un_scaled_source; |
michael@0 | 2992 | } |
michael@0 | 2993 | scale_references(cpi); |
michael@0 | 2994 | |
michael@0 | 2995 | // Clear down mmx registers to allow floating point in what follows. |
michael@0 | 2996 | vp9_clear_system_state(); |
michael@0 | 2997 | |
michael@0 | 2998 | // For an alt ref frame in 2 pass we skip the call to the second |
michael@0 | 2999 | // pass function that sets the target bandwidth so we must set it here. |
michael@0 | 3000 | if (cpi->refresh_alt_ref_frame) { |
michael@0 | 3001 | // Set a per frame bit target for the alt ref frame. |
michael@0 | 3002 | cpi->per_frame_bandwidth = cpi->twopass.gf_bits; |
michael@0 | 3003 | // Set a per second target bitrate. |
michael@0 | 3004 | cpi->target_bandwidth = (int)(cpi->twopass.gf_bits * cpi->output_framerate); |
michael@0 | 3005 | } |
michael@0 | 3006 | |
michael@0 | 3007 | // Clear zbin over-quant value and mode boost values. |
michael@0 | 3008 | cpi->zbin_mode_boost = 0; |
michael@0 | 3009 | |
michael@0 | 3010 | // Enable or disable mode based tweaking of the zbin. |
michael@0 | 3011 | // For 2 pass only used where GF/ARF prediction quality |
michael@0 | 3012 | // is above a threshold. |
michael@0 | 3013 | cpi->zbin_mode_boost = 0; |
michael@0 | 3014 | cpi->zbin_mode_boost_enabled = 0; |
michael@0 | 3015 | |
michael@0 | 3016 | // Current default encoder behavior for the altref sign bias. |
michael@0 | 3017 | cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = cpi->source_alt_ref_active; |
michael@0 | 3018 | |
michael@0 | 3019 | // Check to see if a key frame is signaled. |
michael@0 | 3020 | // For two pass with auto key frame enabled cm->frame_type may already be |
michael@0 | 3021 | // set, but not for one pass. |
michael@0 | 3022 | if ((cm->current_video_frame == 0) || |
michael@0 | 3023 | (cm->frame_flags & FRAMEFLAGS_KEY) || |
michael@0 | 3024 | (cpi->oxcf.auto_key && (cpi->frames_since_key % |
michael@0 | 3025 | cpi->key_frame_frequency == 0))) { |
michael@0 | 3026 | // Set frame type to key frame for the force key frame, if we exceed the |
michael@0 | 3027 | // maximum distance in an automatic keyframe selection or for the first |
michael@0 | 3028 | // frame. |
michael@0 | 3029 | cm->frame_type = KEY_FRAME; |
michael@0 | 3030 | } |
michael@0 | 3031 | |
michael@0 | 3032 | // Set default state for segment based loop filter update flags. |
michael@0 | 3033 | cm->lf.mode_ref_delta_update = 0; |
michael@0 | 3034 | |
michael@0 | 3035 | // Initialize cpi->mv_step_param to default based on max resolution. |
michael@0 | 3036 | cpi->mv_step_param = vp9_init_search_range(cpi, max_mv_def); |
michael@0 | 3037 | // Initialize cpi->max_mv_magnitude and cpi->mv_step_param if appropriate. |
michael@0 | 3038 | if (sf->auto_mv_step_size) { |
michael@0 | 3039 | if (frame_is_intra_only(&cpi->common)) { |
michael@0 | 3040 | // Initialize max_mv_magnitude for use in the first INTER frame |
michael@0 | 3041 | // after a key/intra-only frame. |
michael@0 | 3042 | cpi->max_mv_magnitude = max_mv_def; |
michael@0 | 3043 | } else { |
michael@0 | 3044 | if (cm->show_frame) |
michael@0 | 3045 | // Allow mv_steps to correspond to twice the max mv magnitude found |
michael@0 | 3046 | // in the previous frame, capped by the default max_mv_magnitude based |
michael@0 | 3047 | // on resolution. |
michael@0 | 3048 | cpi->mv_step_param = vp9_init_search_range( |
michael@0 | 3049 | cpi, MIN(max_mv_def, 2 * cpi->max_mv_magnitude)); |
michael@0 | 3050 | cpi->max_mv_magnitude = 0; |
michael@0 | 3051 | } |
michael@0 | 3052 | } |
michael@0 | 3053 | |
michael@0 | 3054 | // Set various flags etc to special state if it is a key frame. |
michael@0 | 3055 | if (frame_is_intra_only(cm)) { |
michael@0 | 3056 | vp9_setup_key_frame(cpi); |
michael@0 | 3057 | // Reset the loop filter deltas and segmentation map. |
michael@0 | 3058 | setup_features(cm); |
michael@0 | 3059 | |
michael@0 | 3060 | // If segmentation is enabled force a map update for key frames. |
michael@0 | 3061 | if (seg->enabled) { |
michael@0 | 3062 | seg->update_map = 1; |
michael@0 | 3063 | seg->update_data = 1; |
michael@0 | 3064 | } |
michael@0 | 3065 | |
michael@0 | 3066 | // The alternate reference frame cannot be active for a key frame. |
michael@0 | 3067 | cpi->source_alt_ref_active = 0; |
michael@0 | 3068 | |
michael@0 | 3069 | cm->error_resilient_mode = (cpi->oxcf.error_resilient_mode != 0); |
michael@0 | 3070 | cm->frame_parallel_decoding_mode = |
michael@0 | 3071 | (cpi->oxcf.frame_parallel_decoding_mode != 0); |
michael@0 | 3072 | if (cm->error_resilient_mode) { |
michael@0 | 3073 | cm->frame_parallel_decoding_mode = 1; |
michael@0 | 3074 | cm->reset_frame_context = 0; |
michael@0 | 3075 | cm->refresh_frame_context = 0; |
michael@0 | 3076 | } else if (cm->intra_only) { |
michael@0 | 3077 | // Only reset the current context. |
michael@0 | 3078 | cm->reset_frame_context = 2; |
michael@0 | 3079 | } |
michael@0 | 3080 | } |
michael@0 | 3081 | |
michael@0 | 3082 | // Configure experimental use of segmentation for enhanced coding of |
michael@0 | 3083 | // static regions if indicated. |
michael@0 | 3084 | // Only allowed in second pass of two pass (as requires lagged coding) |
michael@0 | 3085 | // and if the relevant speed feature flag is set. |
michael@0 | 3086 | if ((cpi->pass == 2) && (cpi->sf.static_segmentation)) { |
michael@0 | 3087 | configure_static_seg_features(cpi); |
michael@0 | 3088 | } |
michael@0 | 3089 | |
michael@0 | 3090 | // Decide how big to make the frame. |
michael@0 | 3091 | vp9_pick_frame_size(cpi); |
michael@0 | 3092 | |
michael@0 | 3093 | vp9_clear_system_state(); |
michael@0 | 3094 | |
michael@0 | 3095 | q = pick_q_and_adjust_q_bounds(cpi, &bottom_index, &top_index); |
michael@0 | 3096 | |
michael@0 | 3097 | q_high = top_index; |
michael@0 | 3098 | q_low = bottom_index; |
michael@0 | 3099 | |
michael@0 | 3100 | vp9_compute_frame_size_bounds(cpi, &frame_under_shoot_limit, |
michael@0 | 3101 | &frame_over_shoot_limit); |
michael@0 | 3102 | |
michael@0 | 3103 | #if CONFIG_MULTIPLE_ARF |
michael@0 | 3104 | // Force the quantizer determined by the coding order pattern. |
michael@0 | 3105 | if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) && |
michael@0 | 3106 | cpi->oxcf.end_usage != USAGE_CONSTANT_QUALITY) { |
michael@0 | 3107 | double new_q; |
michael@0 | 3108 | double current_q = vp9_convert_qindex_to_q(cpi->active_worst_quality); |
michael@0 | 3109 | int level = cpi->this_frame_weight; |
michael@0 | 3110 | assert(level >= 0); |
michael@0 | 3111 | |
michael@0 | 3112 | // Set quantizer steps at 10% increments. |
michael@0 | 3113 | new_q = current_q * (1.0 - (0.2 * (cpi->max_arf_level - level))); |
michael@0 | 3114 | q = cpi->active_worst_quality + vp9_compute_qdelta(cpi, current_q, new_q); |
michael@0 | 3115 | |
michael@0 | 3116 | bottom_index = q; |
michael@0 | 3117 | top_index = q; |
michael@0 | 3118 | q_low = q; |
michael@0 | 3119 | q_high = q; |
michael@0 | 3120 | |
michael@0 | 3121 | printf("frame:%d q:%d\n", cm->current_video_frame, q); |
michael@0 | 3122 | } |
michael@0 | 3123 | #endif |
michael@0 | 3124 | |
michael@0 | 3125 | loop_count = 0; |
michael@0 | 3126 | vp9_zero(cpi->rd_tx_select_threshes); |
michael@0 | 3127 | |
michael@0 | 3128 | if (!frame_is_intra_only(cm)) { |
michael@0 | 3129 | cm->mcomp_filter_type = DEFAULT_INTERP_FILTER; |
michael@0 | 3130 | /* TODO: Decide this more intelligently */ |
michael@0 | 3131 | cm->allow_high_precision_mv = q < HIGH_PRECISION_MV_QTHRESH; |
michael@0 | 3132 | set_mvcost(cpi); |
michael@0 | 3133 | } |
michael@0 | 3134 | |
michael@0 | 3135 | #if CONFIG_VP9_POSTPROC |
michael@0 | 3136 | |
michael@0 | 3137 | if (cpi->oxcf.noise_sensitivity > 0) { |
michael@0 | 3138 | int l = 0; |
michael@0 | 3139 | |
michael@0 | 3140 | switch (cpi->oxcf.noise_sensitivity) { |
michael@0 | 3141 | case 1: |
michael@0 | 3142 | l = 20; |
michael@0 | 3143 | break; |
michael@0 | 3144 | case 2: |
michael@0 | 3145 | l = 40; |
michael@0 | 3146 | break; |
michael@0 | 3147 | case 3: |
michael@0 | 3148 | l = 60; |
michael@0 | 3149 | break; |
michael@0 | 3150 | case 4: |
michael@0 | 3151 | case 5: |
michael@0 | 3152 | l = 100; |
michael@0 | 3153 | break; |
michael@0 | 3154 | case 6: |
michael@0 | 3155 | l = 150; |
michael@0 | 3156 | break; |
michael@0 | 3157 | } |
michael@0 | 3158 | |
michael@0 | 3159 | vp9_denoise(cpi->Source, cpi->Source, l); |
michael@0 | 3160 | } |
michael@0 | 3161 | |
michael@0 | 3162 | #endif |
michael@0 | 3163 | |
michael@0 | 3164 | #ifdef OUTPUT_YUV_SRC |
michael@0 | 3165 | vp9_write_yuv_frame(cpi->Source); |
michael@0 | 3166 | #endif |
michael@0 | 3167 | |
michael@0 | 3168 | do { |
michael@0 | 3169 | vp9_clear_system_state(); // __asm emms; |
michael@0 | 3170 | |
michael@0 | 3171 | vp9_set_quantizer(cpi, q); |
michael@0 | 3172 | |
michael@0 | 3173 | if (loop_count == 0) { |
michael@0 | 3174 | // Set up entropy context depending on frame type. The decoder mandates |
michael@0 | 3175 | // the use of the default context, index 0, for keyframes and inter |
michael@0 | 3176 | // frames where the error_resilient_mode or intra_only flag is set. For |
michael@0 | 3177 | // other inter-frames the encoder currently uses only two contexts; |
michael@0 | 3178 | // context 1 for ALTREF frames and context 0 for the others. |
michael@0 | 3179 | if (cm->frame_type == KEY_FRAME) { |
michael@0 | 3180 | vp9_setup_key_frame(cpi); |
michael@0 | 3181 | } else { |
michael@0 | 3182 | if (!cm->intra_only && !cm->error_resilient_mode) { |
michael@0 | 3183 | cpi->common.frame_context_idx = cpi->refresh_alt_ref_frame; |
michael@0 | 3184 | } |
michael@0 | 3185 | vp9_setup_inter_frame(cpi); |
michael@0 | 3186 | } |
michael@0 | 3187 | } |
michael@0 | 3188 | |
michael@0 | 3189 | if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
michael@0 | 3190 | vp9_vaq_frame_setup(cpi); |
michael@0 | 3191 | } |
michael@0 | 3192 | |
michael@0 | 3193 | // transform / motion compensation build reconstruction frame |
michael@0 | 3194 | |
michael@0 | 3195 | vp9_encode_frame(cpi); |
michael@0 | 3196 | |
michael@0 | 3197 | // Update the skip mb flag probabilities based on the distribution |
michael@0 | 3198 | // seen in the last encoder iteration. |
michael@0 | 3199 | // update_base_skip_probs(cpi); |
michael@0 | 3200 | |
michael@0 | 3201 | vp9_clear_system_state(); // __asm emms; |
michael@0 | 3202 | |
michael@0 | 3203 | // Dummy pack of the bitstream using up to date stats to get an |
michael@0 | 3204 | // accurate estimate of output frame size to determine if we need |
michael@0 | 3205 | // to recode. |
michael@0 | 3206 | vp9_save_coding_context(cpi); |
michael@0 | 3207 | cpi->dummy_packing = 1; |
michael@0 | 3208 | vp9_pack_bitstream(cpi, dest, size); |
michael@0 | 3209 | cpi->projected_frame_size = (*size) << 3; |
michael@0 | 3210 | vp9_restore_coding_context(cpi); |
michael@0 | 3211 | |
michael@0 | 3212 | if (frame_over_shoot_limit == 0) |
michael@0 | 3213 | frame_over_shoot_limit = 1; |
michael@0 | 3214 | active_worst_qchanged = 0; |
michael@0 | 3215 | |
michael@0 | 3216 | if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { |
michael@0 | 3217 | loop = 0; |
michael@0 | 3218 | } else { |
michael@0 | 3219 | // Special case handling for forced key frames |
michael@0 | 3220 | if ((cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced) { |
michael@0 | 3221 | int last_q = q; |
michael@0 | 3222 | int kf_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm)); |
michael@0 | 3223 | |
michael@0 | 3224 | int high_err_target = cpi->ambient_err; |
michael@0 | 3225 | int low_err_target = cpi->ambient_err >> 1; |
michael@0 | 3226 | |
michael@0 | 3227 | // Prevent possible divide by zero error below for perfect KF |
michael@0 | 3228 | kf_err += !kf_err; |
michael@0 | 3229 | |
michael@0 | 3230 | // The key frame is not good enough or we can afford |
michael@0 | 3231 | // to make it better without undue risk of popping. |
michael@0 | 3232 | if ((kf_err > high_err_target && |
michael@0 | 3233 | cpi->projected_frame_size <= frame_over_shoot_limit) || |
michael@0 | 3234 | (kf_err > low_err_target && |
michael@0 | 3235 | cpi->projected_frame_size <= frame_under_shoot_limit)) { |
michael@0 | 3236 | // Lower q_high |
michael@0 | 3237 | q_high = q > q_low ? q - 1 : q_low; |
michael@0 | 3238 | |
michael@0 | 3239 | // Adjust Q |
michael@0 | 3240 | q = (q * high_err_target) / kf_err; |
michael@0 | 3241 | q = MIN(q, (q_high + q_low) >> 1); |
michael@0 | 3242 | } else if (kf_err < low_err_target && |
michael@0 | 3243 | cpi->projected_frame_size >= frame_under_shoot_limit) { |
michael@0 | 3244 | // The key frame is much better than the previous frame |
michael@0 | 3245 | // Raise q_low |
michael@0 | 3246 | q_low = q < q_high ? q + 1 : q_high; |
michael@0 | 3247 | |
michael@0 | 3248 | // Adjust Q |
michael@0 | 3249 | q = (q * low_err_target) / kf_err; |
michael@0 | 3250 | q = MIN(q, (q_high + q_low + 1) >> 1); |
michael@0 | 3251 | } |
michael@0 | 3252 | |
michael@0 | 3253 | // Clamp Q to upper and lower limits: |
michael@0 | 3254 | q = clamp(q, q_low, q_high); |
michael@0 | 3255 | |
michael@0 | 3256 | loop = q != last_q; |
michael@0 | 3257 | } else if (recode_loop_test( |
michael@0 | 3258 | cpi, frame_over_shoot_limit, frame_under_shoot_limit, |
michael@0 | 3259 | q, top_index, bottom_index)) { |
michael@0 | 3260 | // Is the projected frame size out of range and are we allowed |
michael@0 | 3261 | // to attempt to recode. |
michael@0 | 3262 | int last_q = q; |
michael@0 | 3263 | int retries = 0; |
michael@0 | 3264 | |
michael@0 | 3265 | // Frame size out of permitted range: |
michael@0 | 3266 | // Update correction factor & compute new Q to try... |
michael@0 | 3267 | |
michael@0 | 3268 | // Frame is too large |
michael@0 | 3269 | if (cpi->projected_frame_size > cpi->this_frame_target) { |
michael@0 | 3270 | // Raise Qlow as to at least the current value |
michael@0 | 3271 | q_low = q < q_high ? q + 1 : q_high; |
michael@0 | 3272 | |
michael@0 | 3273 | if (undershoot_seen || loop_count > 1) { |
michael@0 | 3274 | // Update rate_correction_factor unless |
michael@0 | 3275 | // cpi->active_worst_quality has changed. |
michael@0 | 3276 | if (!active_worst_qchanged) |
michael@0 | 3277 | vp9_update_rate_correction_factors(cpi, 1); |
michael@0 | 3278 | |
michael@0 | 3279 | q = (q_high + q_low + 1) / 2; |
michael@0 | 3280 | } else { |
michael@0 | 3281 | // Update rate_correction_factor unless |
michael@0 | 3282 | // cpi->active_worst_quality has changed. |
michael@0 | 3283 | if (!active_worst_qchanged) |
michael@0 | 3284 | vp9_update_rate_correction_factors(cpi, 0); |
michael@0 | 3285 | |
michael@0 | 3286 | q = vp9_regulate_q(cpi, cpi->this_frame_target); |
michael@0 | 3287 | |
michael@0 | 3288 | while (q < q_low && retries < 10) { |
michael@0 | 3289 | vp9_update_rate_correction_factors(cpi, 0); |
michael@0 | 3290 | q = vp9_regulate_q(cpi, cpi->this_frame_target); |
michael@0 | 3291 | retries++; |
michael@0 | 3292 | } |
michael@0 | 3293 | } |
michael@0 | 3294 | |
michael@0 | 3295 | overshoot_seen = 1; |
michael@0 | 3296 | } else { |
michael@0 | 3297 | // Frame is too small |
michael@0 | 3298 | q_high = q > q_low ? q - 1 : q_low; |
michael@0 | 3299 | |
michael@0 | 3300 | if (overshoot_seen || loop_count > 1) { |
michael@0 | 3301 | // Update rate_correction_factor unless |
michael@0 | 3302 | // cpi->active_worst_quality has changed. |
michael@0 | 3303 | if (!active_worst_qchanged) |
michael@0 | 3304 | vp9_update_rate_correction_factors(cpi, 1); |
michael@0 | 3305 | |
michael@0 | 3306 | q = (q_high + q_low) / 2; |
michael@0 | 3307 | } else { |
michael@0 | 3308 | // Update rate_correction_factor unless |
michael@0 | 3309 | // cpi->active_worst_quality has changed. |
michael@0 | 3310 | if (!active_worst_qchanged) |
michael@0 | 3311 | vp9_update_rate_correction_factors(cpi, 0); |
michael@0 | 3312 | |
michael@0 | 3313 | q = vp9_regulate_q(cpi, cpi->this_frame_target); |
michael@0 | 3314 | |
michael@0 | 3315 | // Special case reset for qlow for constrained quality. |
michael@0 | 3316 | // This should only trigger where there is very substantial |
michael@0 | 3317 | // undershoot on a frame and the auto cq level is above |
michael@0 | 3318 | // the user passsed in value. |
michael@0 | 3319 | if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY && q < q_low) { |
michael@0 | 3320 | q_low = q; |
michael@0 | 3321 | } |
michael@0 | 3322 | |
michael@0 | 3323 | while (q > q_high && retries < 10) { |
michael@0 | 3324 | vp9_update_rate_correction_factors(cpi, 0); |
michael@0 | 3325 | q = vp9_regulate_q(cpi, cpi->this_frame_target); |
michael@0 | 3326 | retries++; |
michael@0 | 3327 | } |
michael@0 | 3328 | } |
michael@0 | 3329 | |
michael@0 | 3330 | undershoot_seen = 1; |
michael@0 | 3331 | } |
michael@0 | 3332 | |
michael@0 | 3333 | // Clamp Q to upper and lower limits: |
michael@0 | 3334 | q = clamp(q, q_low, q_high); |
michael@0 | 3335 | |
michael@0 | 3336 | loop = q != last_q; |
michael@0 | 3337 | } else { |
michael@0 | 3338 | loop = 0; |
michael@0 | 3339 | } |
michael@0 | 3340 | } |
michael@0 | 3341 | |
michael@0 | 3342 | if (cpi->is_src_frame_alt_ref) |
michael@0 | 3343 | loop = 0; |
michael@0 | 3344 | |
michael@0 | 3345 | if (loop) { |
michael@0 | 3346 | loop_count++; |
michael@0 | 3347 | |
michael@0 | 3348 | #if CONFIG_INTERNAL_STATS |
michael@0 | 3349 | cpi->tot_recode_hits++; |
michael@0 | 3350 | #endif |
michael@0 | 3351 | } |
michael@0 | 3352 | } while (loop); |
michael@0 | 3353 | |
michael@0 | 3354 | // Special case code to reduce pulsing when key frames are forced at a |
michael@0 | 3355 | // fixed interval. Note the reconstruction error if it is the frame before |
michael@0 | 3356 | // the force key frame |
michael@0 | 3357 | if (cpi->next_key_frame_forced && (cpi->twopass.frames_to_key == 0)) { |
michael@0 | 3358 | cpi->ambient_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm)); |
michael@0 | 3359 | } |
michael@0 | 3360 | |
michael@0 | 3361 | if (cm->frame_type == KEY_FRAME) |
michael@0 | 3362 | cpi->refresh_last_frame = 1; |
michael@0 | 3363 | |
michael@0 | 3364 | cm->frame_to_show = get_frame_new_buffer(cm); |
michael@0 | 3365 | |
michael@0 | 3366 | #if WRITE_RECON_BUFFER |
michael@0 | 3367 | if (cm->show_frame) |
michael@0 | 3368 | write_cx_frame_to_file(cm->frame_to_show, |
michael@0 | 3369 | cm->current_video_frame); |
michael@0 | 3370 | else |
michael@0 | 3371 | write_cx_frame_to_file(cm->frame_to_show, |
michael@0 | 3372 | cm->current_video_frame + 1000); |
michael@0 | 3373 | #endif |
michael@0 | 3374 | |
michael@0 | 3375 | // Pick the loop filter level for the frame. |
michael@0 | 3376 | loopfilter_frame(cpi, cm); |
michael@0 | 3377 | |
michael@0 | 3378 | #if WRITE_RECON_BUFFER |
michael@0 | 3379 | if (cm->show_frame) |
michael@0 | 3380 | write_cx_frame_to_file(cm->frame_to_show, |
michael@0 | 3381 | cm->current_video_frame + 2000); |
michael@0 | 3382 | else |
michael@0 | 3383 | write_cx_frame_to_file(cm->frame_to_show, |
michael@0 | 3384 | cm->current_video_frame + 3000); |
michael@0 | 3385 | #endif |
michael@0 | 3386 | |
michael@0 | 3387 | // build the bitstream |
michael@0 | 3388 | cpi->dummy_packing = 0; |
michael@0 | 3389 | vp9_pack_bitstream(cpi, dest, size); |
michael@0 | 3390 | |
michael@0 | 3391 | if (cm->seg.update_map) |
michael@0 | 3392 | update_reference_segmentation_map(cpi); |
michael@0 | 3393 | |
michael@0 | 3394 | release_scaled_references(cpi); |
michael@0 | 3395 | update_reference_frames(cpi); |
michael@0 | 3396 | |
michael@0 | 3397 | for (t = TX_4X4; t <= TX_32X32; t++) |
michael@0 | 3398 | full_to_model_counts(cpi->common.counts.coef[t], |
michael@0 | 3399 | cpi->coef_counts[t]); |
michael@0 | 3400 | if (!cpi->common.error_resilient_mode && |
michael@0 | 3401 | !cpi->common.frame_parallel_decoding_mode) { |
michael@0 | 3402 | vp9_adapt_coef_probs(&cpi->common); |
michael@0 | 3403 | } |
michael@0 | 3404 | |
michael@0 | 3405 | if (!frame_is_intra_only(&cpi->common)) { |
michael@0 | 3406 | FRAME_COUNTS *counts = &cpi->common.counts; |
michael@0 | 3407 | |
michael@0 | 3408 | vp9_copy(counts->y_mode, cpi->y_mode_count); |
michael@0 | 3409 | vp9_copy(counts->uv_mode, cpi->y_uv_mode_count); |
michael@0 | 3410 | vp9_copy(counts->partition, cpi->partition_count); |
michael@0 | 3411 | vp9_copy(counts->intra_inter, cpi->intra_inter_count); |
michael@0 | 3412 | vp9_copy(counts->comp_inter, cpi->comp_inter_count); |
michael@0 | 3413 | vp9_copy(counts->single_ref, cpi->single_ref_count); |
michael@0 | 3414 | vp9_copy(counts->comp_ref, cpi->comp_ref_count); |
michael@0 | 3415 | counts->mv = cpi->NMVcount; |
michael@0 | 3416 | if (!cpi->common.error_resilient_mode && |
michael@0 | 3417 | !cpi->common.frame_parallel_decoding_mode) { |
michael@0 | 3418 | vp9_adapt_mode_probs(&cpi->common); |
michael@0 | 3419 | vp9_adapt_mv_probs(&cpi->common, cpi->common.allow_high_precision_mv); |
michael@0 | 3420 | } |
michael@0 | 3421 | } |
michael@0 | 3422 | |
michael@0 | 3423 | #ifdef ENTROPY_STATS |
michael@0 | 3424 | vp9_update_mode_context_stats(cpi); |
michael@0 | 3425 | #endif |
michael@0 | 3426 | |
michael@0 | 3427 | /* Move storing frame_type out of the above loop since it is also |
michael@0 | 3428 | * needed in motion search besides loopfilter */ |
michael@0 | 3429 | cm->last_frame_type = cm->frame_type; |
michael@0 | 3430 | |
michael@0 | 3431 | // Update rate control heuristics |
michael@0 | 3432 | cpi->total_byte_count += (*size); |
michael@0 | 3433 | cpi->projected_frame_size = (*size) << 3; |
michael@0 | 3434 | |
michael@0 | 3435 | // Post encode loop adjustment of Q prediction. |
michael@0 | 3436 | if (!active_worst_qchanged) |
michael@0 | 3437 | vp9_update_rate_correction_factors(cpi, (cpi->sf.recode_loop || |
michael@0 | 3438 | cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) ? 2 : 0); |
michael@0 | 3439 | |
michael@0 | 3440 | |
michael@0 | 3441 | cpi->last_q[cm->frame_type] = cm->base_qindex; |
michael@0 | 3442 | |
michael@0 | 3443 | // Keep record of last boosted (KF/KF/ARF) Q value. |
michael@0 | 3444 | // If the current frame is coded at a lower Q then we also update it. |
michael@0 | 3445 | // If all mbs in this group are skipped only update if the Q value is |
michael@0 | 3446 | // better than that already stored. |
michael@0 | 3447 | // This is used to help set quality in forced key frames to reduce popping |
michael@0 | 3448 | if ((cm->base_qindex < cpi->last_boosted_qindex) || |
michael@0 | 3449 | ((cpi->static_mb_pct < 100) && |
michael@0 | 3450 | ((cm->frame_type == KEY_FRAME) || |
michael@0 | 3451 | cpi->refresh_alt_ref_frame || |
michael@0 | 3452 | (cpi->refresh_golden_frame && !cpi->is_src_frame_alt_ref)))) { |
michael@0 | 3453 | cpi->last_boosted_qindex = cm->base_qindex; |
michael@0 | 3454 | } |
michael@0 | 3455 | |
michael@0 | 3456 | if (cm->frame_type == KEY_FRAME) { |
michael@0 | 3457 | vp9_adjust_key_frame_context(cpi); |
michael@0 | 3458 | } |
michael@0 | 3459 | |
michael@0 | 3460 | // Keep a record of ambient average Q. |
michael@0 | 3461 | if (cm->frame_type != KEY_FRAME) |
michael@0 | 3462 | cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex + |
michael@0 | 3463 | cm->base_qindex) >> 2; |
michael@0 | 3464 | |
michael@0 | 3465 | // Keep a record from which we can calculate the average Q excluding GF |
michael@0 | 3466 | // updates and key frames. |
michael@0 | 3467 | if (cm->frame_type != KEY_FRAME && |
michael@0 | 3468 | !cpi->refresh_golden_frame && |
michael@0 | 3469 | !cpi->refresh_alt_ref_frame) { |
michael@0 | 3470 | cpi->ni_frames++; |
michael@0 | 3471 | cpi->tot_q += vp9_convert_qindex_to_q(q); |
michael@0 | 3472 | cpi->avg_q = cpi->tot_q / (double)cpi->ni_frames; |
michael@0 | 3473 | |
michael@0 | 3474 | // Calculate the average Q for normal inter frames (not key or GFU frames). |
michael@0 | 3475 | cpi->ni_tot_qi += q; |
michael@0 | 3476 | cpi->ni_av_qi = cpi->ni_tot_qi / cpi->ni_frames; |
michael@0 | 3477 | } |
michael@0 | 3478 | |
michael@0 | 3479 | // Update the buffer level variable. |
michael@0 | 3480 | // Non-viewable frames are a special case and are treated as pure overhead. |
michael@0 | 3481 | if (!cm->show_frame) |
michael@0 | 3482 | cpi->bits_off_target -= cpi->projected_frame_size; |
michael@0 | 3483 | else |
michael@0 | 3484 | cpi->bits_off_target += cpi->av_per_frame_bandwidth - |
michael@0 | 3485 | cpi->projected_frame_size; |
michael@0 | 3486 | |
michael@0 | 3487 | // Clip the buffer level at the maximum buffer size |
michael@0 | 3488 | if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size) |
michael@0 | 3489 | cpi->bits_off_target = cpi->oxcf.maximum_buffer_size; |
michael@0 | 3490 | |
michael@0 | 3491 | // Rolling monitors of whether we are over or underspending used to help |
michael@0 | 3492 | // regulate min and Max Q in two pass. |
michael@0 | 3493 | if (cm->frame_type != KEY_FRAME) { |
michael@0 | 3494 | cpi->rolling_target_bits = |
michael@0 | 3495 | ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4; |
michael@0 | 3496 | cpi->rolling_actual_bits = |
michael@0 | 3497 | ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4; |
michael@0 | 3498 | cpi->long_rolling_target_bits = |
michael@0 | 3499 | ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32; |
michael@0 | 3500 | cpi->long_rolling_actual_bits = |
michael@0 | 3501 | ((cpi->long_rolling_actual_bits * 31) + |
michael@0 | 3502 | cpi->projected_frame_size + 16) / 32; |
michael@0 | 3503 | } |
michael@0 | 3504 | |
michael@0 | 3505 | // Actual bits spent |
michael@0 | 3506 | cpi->total_actual_bits += cpi->projected_frame_size; |
michael@0 | 3507 | |
michael@0 | 3508 | // Debug stats |
michael@0 | 3509 | cpi->total_target_vs_actual += (cpi->this_frame_target - |
michael@0 | 3510 | cpi->projected_frame_size); |
michael@0 | 3511 | |
michael@0 | 3512 | cpi->buffer_level = cpi->bits_off_target; |
michael@0 | 3513 | |
michael@0 | 3514 | #ifndef DISABLE_RC_LONG_TERM_MEM |
michael@0 | 3515 | // Update bits left to the kf and gf groups to account for overshoot or |
michael@0 | 3516 | // undershoot on these frames |
michael@0 | 3517 | if (cm->frame_type == KEY_FRAME) { |
michael@0 | 3518 | cpi->twopass.kf_group_bits += cpi->this_frame_target - |
michael@0 | 3519 | cpi->projected_frame_size; |
michael@0 | 3520 | |
michael@0 | 3521 | cpi->twopass.kf_group_bits = MAX(cpi->twopass.kf_group_bits, 0); |
michael@0 | 3522 | } else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) { |
michael@0 | 3523 | cpi->twopass.gf_group_bits += cpi->this_frame_target - |
michael@0 | 3524 | cpi->projected_frame_size; |
michael@0 | 3525 | |
michael@0 | 3526 | cpi->twopass.gf_group_bits = MAX(cpi->twopass.gf_group_bits, 0); |
michael@0 | 3527 | } |
michael@0 | 3528 | #endif |
michael@0 | 3529 | |
michael@0 | 3530 | #if 0 |
michael@0 | 3531 | output_frame_level_debug_stats(cpi); |
michael@0 | 3532 | #endif |
michael@0 | 3533 | if (cpi->refresh_golden_frame == 1) |
michael@0 | 3534 | cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN; |
michael@0 | 3535 | else |
michael@0 | 3536 | cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_GOLDEN; |
michael@0 | 3537 | |
michael@0 | 3538 | if (cpi->refresh_alt_ref_frame == 1) |
michael@0 | 3539 | cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF; |
michael@0 | 3540 | else |
michael@0 | 3541 | cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_ALTREF; |
michael@0 | 3542 | |
michael@0 | 3543 | |
michael@0 | 3544 | if (cpi->refresh_last_frame & cpi->refresh_golden_frame) |
michael@0 | 3545 | cpi->gold_is_last = 1; |
michael@0 | 3546 | else if (cpi->refresh_last_frame ^ cpi->refresh_golden_frame) |
michael@0 | 3547 | cpi->gold_is_last = 0; |
michael@0 | 3548 | |
michael@0 | 3549 | if (cpi->refresh_last_frame & cpi->refresh_alt_ref_frame) |
michael@0 | 3550 | cpi->alt_is_last = 1; |
michael@0 | 3551 | else if (cpi->refresh_last_frame ^ cpi->refresh_alt_ref_frame) |
michael@0 | 3552 | cpi->alt_is_last = 0; |
michael@0 | 3553 | |
michael@0 | 3554 | if (cpi->refresh_alt_ref_frame & cpi->refresh_golden_frame) |
michael@0 | 3555 | cpi->gold_is_alt = 1; |
michael@0 | 3556 | else if (cpi->refresh_alt_ref_frame ^ cpi->refresh_golden_frame) |
michael@0 | 3557 | cpi->gold_is_alt = 0; |
michael@0 | 3558 | |
michael@0 | 3559 | cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG; |
michael@0 | 3560 | |
michael@0 | 3561 | if (cpi->gold_is_last) |
michael@0 | 3562 | cpi->ref_frame_flags &= ~VP9_GOLD_FLAG; |
michael@0 | 3563 | |
michael@0 | 3564 | if (cpi->alt_is_last) |
michael@0 | 3565 | cpi->ref_frame_flags &= ~VP9_ALT_FLAG; |
michael@0 | 3566 | |
michael@0 | 3567 | if (cpi->gold_is_alt) |
michael@0 | 3568 | cpi->ref_frame_flags &= ~VP9_ALT_FLAG; |
michael@0 | 3569 | |
michael@0 | 3570 | if (cpi->oxcf.play_alternate && cpi->refresh_alt_ref_frame |
michael@0 | 3571 | && (cm->frame_type != KEY_FRAME)) |
michael@0 | 3572 | // Update the alternate reference frame stats as appropriate. |
michael@0 | 3573 | update_alt_ref_frame_stats(cpi); |
michael@0 | 3574 | else |
michael@0 | 3575 | // Update the Golden frame stats as appropriate. |
michael@0 | 3576 | update_golden_frame_stats(cpi); |
michael@0 | 3577 | |
michael@0 | 3578 | if (cm->frame_type == KEY_FRAME) { |
michael@0 | 3579 | // Tell the caller that the frame was coded as a key frame |
michael@0 | 3580 | *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY; |
michael@0 | 3581 | |
michael@0 | 3582 | #if CONFIG_MULTIPLE_ARF |
michael@0 | 3583 | // Reset the sequence number. |
michael@0 | 3584 | if (cpi->multi_arf_enabled) { |
michael@0 | 3585 | cpi->sequence_number = 0; |
michael@0 | 3586 | cpi->frame_coding_order_period = cpi->new_frame_coding_order_period; |
michael@0 | 3587 | cpi->new_frame_coding_order_period = -1; |
michael@0 | 3588 | } |
michael@0 | 3589 | #endif |
michael@0 | 3590 | |
michael@0 | 3591 | // As this frame is a key frame the next defaults to an inter frame. |
michael@0 | 3592 | cm->frame_type = INTER_FRAME; |
michael@0 | 3593 | } else { |
michael@0 | 3594 | *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY; |
michael@0 | 3595 | |
michael@0 | 3596 | #if CONFIG_MULTIPLE_ARF |
michael@0 | 3597 | /* Increment position in the coded frame sequence. */ |
michael@0 | 3598 | if (cpi->multi_arf_enabled) { |
michael@0 | 3599 | ++cpi->sequence_number; |
michael@0 | 3600 | if (cpi->sequence_number >= cpi->frame_coding_order_period) { |
michael@0 | 3601 | cpi->sequence_number = 0; |
michael@0 | 3602 | cpi->frame_coding_order_period = cpi->new_frame_coding_order_period; |
michael@0 | 3603 | cpi->new_frame_coding_order_period = -1; |
michael@0 | 3604 | } |
michael@0 | 3605 | cpi->this_frame_weight = cpi->arf_weight[cpi->sequence_number]; |
michael@0 | 3606 | assert(cpi->this_frame_weight >= 0); |
michael@0 | 3607 | } |
michael@0 | 3608 | #endif |
michael@0 | 3609 | } |
michael@0 | 3610 | |
michael@0 | 3611 | // Clear the one shot update flags for segmentation map and mode/ref loop |
michael@0 | 3612 | // filter deltas. |
michael@0 | 3613 | cm->seg.update_map = 0; |
michael@0 | 3614 | cm->seg.update_data = 0; |
michael@0 | 3615 | cm->lf.mode_ref_delta_update = 0; |
michael@0 | 3616 | |
michael@0 | 3617 | // keep track of the last coded dimensions |
michael@0 | 3618 | cm->last_width = cm->width; |
michael@0 | 3619 | cm->last_height = cm->height; |
michael@0 | 3620 | |
michael@0 | 3621 | // reset to normal state now that we are done. |
michael@0 | 3622 | cm->last_show_frame = cm->show_frame; |
michael@0 | 3623 | if (cm->show_frame) { |
michael@0 | 3624 | // current mip will be the prev_mip for the next frame |
michael@0 | 3625 | MODE_INFO *temp = cm->prev_mip; |
michael@0 | 3626 | MODE_INFO **temp2 = cm->prev_mi_grid_base; |
michael@0 | 3627 | cm->prev_mip = cm->mip; |
michael@0 | 3628 | cm->mip = temp; |
michael@0 | 3629 | cm->prev_mi_grid_base = cm->mi_grid_base; |
michael@0 | 3630 | cm->mi_grid_base = temp2; |
michael@0 | 3631 | |
michael@0 | 3632 | // update the upper left visible macroblock ptrs |
michael@0 | 3633 | cm->mi = cm->mip + cm->mode_info_stride + 1; |
michael@0 | 3634 | cm->mi_grid_visible = cm->mi_grid_base + cm->mode_info_stride + 1; |
michael@0 | 3635 | |
michael@0 | 3636 | cpi->mb.e_mbd.mi_8x8 = cm->mi_grid_visible; |
michael@0 | 3637 | cpi->mb.e_mbd.mi_8x8[0] = cm->mi; |
michael@0 | 3638 | |
michael@0 | 3639 | // Don't increment frame counters if this was an altref buffer |
michael@0 | 3640 | // update not a real frame |
michael@0 | 3641 | ++cm->current_video_frame; |
michael@0 | 3642 | ++cpi->frames_since_key; |
michael@0 | 3643 | } |
michael@0 | 3644 | // restore prev_mi |
michael@0 | 3645 | cm->prev_mi = cm->prev_mip + cm->mode_info_stride + 1; |
michael@0 | 3646 | cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mode_info_stride + 1; |
michael@0 | 3647 | } |
michael@0 | 3648 | |
michael@0 | 3649 | static void Pass2Encode(VP9_COMP *cpi, unsigned long *size, |
michael@0 | 3650 | unsigned char *dest, unsigned int *frame_flags) { |
michael@0 | 3651 | cpi->enable_encode_breakout = 1; |
michael@0 | 3652 | |
michael@0 | 3653 | if (!cpi->refresh_alt_ref_frame) |
michael@0 | 3654 | vp9_second_pass(cpi); |
michael@0 | 3655 | |
michael@0 | 3656 | encode_frame_to_data_rate(cpi, size, dest, frame_flags); |
michael@0 | 3657 | // vp9_print_modes_and_motion_vectors(&cpi->common, "encode.stt"); |
michael@0 | 3658 | #ifdef DISABLE_RC_LONG_TERM_MEM |
michael@0 | 3659 | cpi->twopass.bits_left -= cpi->this_frame_target; |
michael@0 | 3660 | #else |
michael@0 | 3661 | cpi->twopass.bits_left -= 8 * *size; |
michael@0 | 3662 | #endif |
michael@0 | 3663 | |
michael@0 | 3664 | if (!cpi->refresh_alt_ref_frame) { |
michael@0 | 3665 | double lower_bounds_min_rate = FRAME_OVERHEAD_BITS * cpi->oxcf.framerate; |
michael@0 | 3666 | double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth |
michael@0 | 3667 | * cpi->oxcf.two_pass_vbrmin_section |
michael@0 | 3668 | / 100); |
michael@0 | 3669 | |
michael@0 | 3670 | if (two_pass_min_rate < lower_bounds_min_rate) |
michael@0 | 3671 | two_pass_min_rate = lower_bounds_min_rate; |
michael@0 | 3672 | |
michael@0 | 3673 | cpi->twopass.bits_left += (int64_t)(two_pass_min_rate |
michael@0 | 3674 | / cpi->oxcf.framerate); |
michael@0 | 3675 | } |
michael@0 | 3676 | } |
michael@0 | 3677 | |
michael@0 | 3678 | static void check_initial_width(VP9_COMP *cpi, YV12_BUFFER_CONFIG *sd) { |
michael@0 | 3679 | VP9_COMMON *cm = &cpi->common; |
michael@0 | 3680 | if (!cpi->initial_width) { |
michael@0 | 3681 | // TODO(jkoleszar): Support 1/4 subsampling? |
michael@0 | 3682 | cm->subsampling_x = (sd != NULL) && sd->uv_width < sd->y_width; |
michael@0 | 3683 | cm->subsampling_y = (sd != NULL) && sd->uv_height < sd->y_height; |
michael@0 | 3684 | alloc_raw_frame_buffers(cpi); |
michael@0 | 3685 | |
michael@0 | 3686 | cpi->initial_width = cm->width; |
michael@0 | 3687 | cpi->initial_height = cm->height; |
michael@0 | 3688 | } |
michael@0 | 3689 | } |
michael@0 | 3690 | |
michael@0 | 3691 | |
michael@0 | 3692 | int vp9_receive_raw_frame(VP9_PTR ptr, unsigned int frame_flags, |
michael@0 | 3693 | YV12_BUFFER_CONFIG *sd, int64_t time_stamp, |
michael@0 | 3694 | int64_t end_time) { |
michael@0 | 3695 | VP9_COMP *cpi = (VP9_COMP *) ptr; |
michael@0 | 3696 | struct vpx_usec_timer timer; |
michael@0 | 3697 | int res = 0; |
michael@0 | 3698 | |
michael@0 | 3699 | check_initial_width(cpi, sd); |
michael@0 | 3700 | vpx_usec_timer_start(&timer); |
michael@0 | 3701 | if (vp9_lookahead_push(cpi->lookahead, sd, time_stamp, end_time, frame_flags, |
michael@0 | 3702 | cpi->active_map_enabled ? cpi->active_map : NULL)) |
michael@0 | 3703 | res = -1; |
michael@0 | 3704 | vpx_usec_timer_mark(&timer); |
michael@0 | 3705 | cpi->time_receive_data += vpx_usec_timer_elapsed(&timer); |
michael@0 | 3706 | |
michael@0 | 3707 | return res; |
michael@0 | 3708 | } |
michael@0 | 3709 | |
michael@0 | 3710 | |
michael@0 | 3711 | static int frame_is_reference(const VP9_COMP *cpi) { |
michael@0 | 3712 | const VP9_COMMON *cm = &cpi->common; |
michael@0 | 3713 | |
michael@0 | 3714 | return cm->frame_type == KEY_FRAME || |
michael@0 | 3715 | cpi->refresh_last_frame || |
michael@0 | 3716 | cpi->refresh_golden_frame || |
michael@0 | 3717 | cpi->refresh_alt_ref_frame || |
michael@0 | 3718 | cm->refresh_frame_context || |
michael@0 | 3719 | cm->lf.mode_ref_delta_update || |
michael@0 | 3720 | cm->seg.update_map || |
michael@0 | 3721 | cm->seg.update_data; |
michael@0 | 3722 | } |
michael@0 | 3723 | |
michael@0 | 3724 | #if CONFIG_MULTIPLE_ARF |
michael@0 | 3725 | int is_next_frame_arf(VP9_COMP *cpi) { |
michael@0 | 3726 | // Negative entry in frame_coding_order indicates an ARF at this position. |
michael@0 | 3727 | return cpi->frame_coding_order[cpi->sequence_number + 1] < 0 ? 1 : 0; |
michael@0 | 3728 | } |
michael@0 | 3729 | #endif |
michael@0 | 3730 | |
michael@0 | 3731 | int vp9_get_compressed_data(VP9_PTR ptr, unsigned int *frame_flags, |
michael@0 | 3732 | unsigned long *size, unsigned char *dest, |
michael@0 | 3733 | int64_t *time_stamp, int64_t *time_end, int flush) { |
michael@0 | 3734 | VP9_COMP *cpi = (VP9_COMP *) ptr; |
michael@0 | 3735 | VP9_COMMON *cm = &cpi->common; |
michael@0 | 3736 | struct vpx_usec_timer cmptimer; |
michael@0 | 3737 | YV12_BUFFER_CONFIG *force_src_buffer = NULL; |
michael@0 | 3738 | int i; |
michael@0 | 3739 | // FILE *fp_out = fopen("enc_frame_type.txt", "a"); |
michael@0 | 3740 | |
michael@0 | 3741 | if (!cpi) |
michael@0 | 3742 | return -1; |
michael@0 | 3743 | |
michael@0 | 3744 | vpx_usec_timer_start(&cmptimer); |
michael@0 | 3745 | |
michael@0 | 3746 | cpi->source = NULL; |
michael@0 | 3747 | |
michael@0 | 3748 | cpi->common.allow_high_precision_mv = ALTREF_HIGH_PRECISION_MV; |
michael@0 | 3749 | set_mvcost(cpi); |
michael@0 | 3750 | |
michael@0 | 3751 | // Should we code an alternate reference frame. |
michael@0 | 3752 | if (cpi->oxcf.play_alternate && cpi->source_alt_ref_pending) { |
michael@0 | 3753 | int frames_to_arf; |
michael@0 | 3754 | |
michael@0 | 3755 | #if CONFIG_MULTIPLE_ARF |
michael@0 | 3756 | assert(!cpi->multi_arf_enabled || |
michael@0 | 3757 | cpi->frame_coding_order[cpi->sequence_number] < 0); |
michael@0 | 3758 | |
michael@0 | 3759 | if (cpi->multi_arf_enabled && (cpi->pass == 2)) |
michael@0 | 3760 | frames_to_arf = (-cpi->frame_coding_order[cpi->sequence_number]) |
michael@0 | 3761 | - cpi->next_frame_in_order; |
michael@0 | 3762 | else |
michael@0 | 3763 | #endif |
michael@0 | 3764 | frames_to_arf = cpi->frames_till_gf_update_due; |
michael@0 | 3765 | |
michael@0 | 3766 | assert(frames_to_arf < cpi->twopass.frames_to_key); |
michael@0 | 3767 | |
michael@0 | 3768 | if ((cpi->source = vp9_lookahead_peek(cpi->lookahead, frames_to_arf))) { |
michael@0 | 3769 | #if CONFIG_MULTIPLE_ARF |
michael@0 | 3770 | cpi->alt_ref_source[cpi->arf_buffered] = cpi->source; |
michael@0 | 3771 | #else |
michael@0 | 3772 | cpi->alt_ref_source = cpi->source; |
michael@0 | 3773 | #endif |
michael@0 | 3774 | |
michael@0 | 3775 | if (cpi->oxcf.arnr_max_frames > 0) { |
michael@0 | 3776 | // Produce the filtered ARF frame. |
michael@0 | 3777 | // TODO(agrange) merge these two functions. |
michael@0 | 3778 | configure_arnr_filter(cpi, cm->current_video_frame + frames_to_arf, |
michael@0 | 3779 | cpi->gfu_boost); |
michael@0 | 3780 | vp9_temporal_filter_prepare(cpi, frames_to_arf); |
michael@0 | 3781 | vp9_extend_frame_borders(&cpi->alt_ref_buffer, |
michael@0 | 3782 | cm->subsampling_x, cm->subsampling_y); |
michael@0 | 3783 | force_src_buffer = &cpi->alt_ref_buffer; |
michael@0 | 3784 | } |
michael@0 | 3785 | |
michael@0 | 3786 | cm->show_frame = 0; |
michael@0 | 3787 | cpi->refresh_alt_ref_frame = 1; |
michael@0 | 3788 | cpi->refresh_golden_frame = 0; |
michael@0 | 3789 | cpi->refresh_last_frame = 0; |
michael@0 | 3790 | cpi->is_src_frame_alt_ref = 0; |
michael@0 | 3791 | |
michael@0 | 3792 | // TODO(agrange) This needs to vary depending on where the next ARF is. |
michael@0 | 3793 | cpi->frames_till_alt_ref_frame = frames_to_arf; |
michael@0 | 3794 | |
michael@0 | 3795 | #if CONFIG_MULTIPLE_ARF |
michael@0 | 3796 | if (!cpi->multi_arf_enabled) |
michael@0 | 3797 | #endif |
michael@0 | 3798 | cpi->source_alt_ref_pending = 0; // Clear Pending altf Ref flag. |
michael@0 | 3799 | } |
michael@0 | 3800 | } |
michael@0 | 3801 | |
michael@0 | 3802 | if (!cpi->source) { |
michael@0 | 3803 | #if CONFIG_MULTIPLE_ARF |
michael@0 | 3804 | int i; |
michael@0 | 3805 | #endif |
michael@0 | 3806 | if ((cpi->source = vp9_lookahead_pop(cpi->lookahead, flush))) { |
michael@0 | 3807 | cm->show_frame = 1; |
michael@0 | 3808 | cm->intra_only = 0; |
michael@0 | 3809 | |
michael@0 | 3810 | #if CONFIG_MULTIPLE_ARF |
michael@0 | 3811 | // Is this frame the ARF overlay. |
michael@0 | 3812 | cpi->is_src_frame_alt_ref = 0; |
michael@0 | 3813 | for (i = 0; i < cpi->arf_buffered; ++i) { |
michael@0 | 3814 | if (cpi->source == cpi->alt_ref_source[i]) { |
michael@0 | 3815 | cpi->is_src_frame_alt_ref = 1; |
michael@0 | 3816 | cpi->refresh_golden_frame = 1; |
michael@0 | 3817 | break; |
michael@0 | 3818 | } |
michael@0 | 3819 | } |
michael@0 | 3820 | #else |
michael@0 | 3821 | cpi->is_src_frame_alt_ref = cpi->alt_ref_source |
michael@0 | 3822 | && (cpi->source == cpi->alt_ref_source); |
michael@0 | 3823 | #endif |
michael@0 | 3824 | if (cpi->is_src_frame_alt_ref) { |
michael@0 | 3825 | // Current frame is an ARF overlay frame. |
michael@0 | 3826 | #if CONFIG_MULTIPLE_ARF |
michael@0 | 3827 | cpi->alt_ref_source[i] = NULL; |
michael@0 | 3828 | #else |
michael@0 | 3829 | cpi->alt_ref_source = NULL; |
michael@0 | 3830 | #endif |
michael@0 | 3831 | // Don't refresh the last buffer for an ARF overlay frame. It will |
michael@0 | 3832 | // become the GF so preserve last as an alternative prediction option. |
michael@0 | 3833 | cpi->refresh_last_frame = 0; |
michael@0 | 3834 | } |
michael@0 | 3835 | #if CONFIG_MULTIPLE_ARF |
michael@0 | 3836 | ++cpi->next_frame_in_order; |
michael@0 | 3837 | #endif |
michael@0 | 3838 | } |
michael@0 | 3839 | } |
michael@0 | 3840 | |
michael@0 | 3841 | if (cpi->source) { |
michael@0 | 3842 | cpi->un_scaled_source = cpi->Source = force_src_buffer ? force_src_buffer |
michael@0 | 3843 | : &cpi->source->img; |
michael@0 | 3844 | *time_stamp = cpi->source->ts_start; |
michael@0 | 3845 | *time_end = cpi->source->ts_end; |
michael@0 | 3846 | *frame_flags = cpi->source->flags; |
michael@0 | 3847 | |
michael@0 | 3848 | // fprintf(fp_out, " Frame:%d", cm->current_video_frame); |
michael@0 | 3849 | #if CONFIG_MULTIPLE_ARF |
michael@0 | 3850 | if (cpi->multi_arf_enabled) { |
michael@0 | 3851 | // fprintf(fp_out, " seq_no:%d this_frame_weight:%d", |
michael@0 | 3852 | // cpi->sequence_number, cpi->this_frame_weight); |
michael@0 | 3853 | } else { |
michael@0 | 3854 | // fprintf(fp_out, "\n"); |
michael@0 | 3855 | } |
michael@0 | 3856 | #else |
michael@0 | 3857 | // fprintf(fp_out, "\n"); |
michael@0 | 3858 | #endif |
michael@0 | 3859 | |
michael@0 | 3860 | #if CONFIG_MULTIPLE_ARF |
michael@0 | 3861 | if ((cm->frame_type != KEY_FRAME) && (cpi->pass == 2)) |
michael@0 | 3862 | cpi->source_alt_ref_pending = is_next_frame_arf(cpi); |
michael@0 | 3863 | #endif |
michael@0 | 3864 | } else { |
michael@0 | 3865 | *size = 0; |
michael@0 | 3866 | if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done) { |
michael@0 | 3867 | vp9_end_first_pass(cpi); /* get last stats packet */ |
michael@0 | 3868 | cpi->twopass.first_pass_done = 1; |
michael@0 | 3869 | } |
michael@0 | 3870 | |
michael@0 | 3871 | // fclose(fp_out); |
michael@0 | 3872 | return -1; |
michael@0 | 3873 | } |
michael@0 | 3874 | |
michael@0 | 3875 | if (cpi->source->ts_start < cpi->first_time_stamp_ever) { |
michael@0 | 3876 | cpi->first_time_stamp_ever = cpi->source->ts_start; |
michael@0 | 3877 | cpi->last_end_time_stamp_seen = cpi->source->ts_start; |
michael@0 | 3878 | } |
michael@0 | 3879 | |
michael@0 | 3880 | // adjust frame rates based on timestamps given |
michael@0 | 3881 | if (!cpi->refresh_alt_ref_frame) { |
michael@0 | 3882 | int64_t this_duration; |
michael@0 | 3883 | int step = 0; |
michael@0 | 3884 | |
michael@0 | 3885 | if (cpi->source->ts_start == cpi->first_time_stamp_ever) { |
michael@0 | 3886 | this_duration = cpi->source->ts_end - cpi->source->ts_start; |
michael@0 | 3887 | step = 1; |
michael@0 | 3888 | } else { |
michael@0 | 3889 | int64_t last_duration = cpi->last_end_time_stamp_seen |
michael@0 | 3890 | - cpi->last_time_stamp_seen; |
michael@0 | 3891 | |
michael@0 | 3892 | this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen; |
michael@0 | 3893 | |
michael@0 | 3894 | // do a step update if the duration changes by 10% |
michael@0 | 3895 | if (last_duration) |
michael@0 | 3896 | step = (int)((this_duration - last_duration) * 10 / last_duration); |
michael@0 | 3897 | } |
michael@0 | 3898 | |
michael@0 | 3899 | if (this_duration) { |
michael@0 | 3900 | if (step) { |
michael@0 | 3901 | vp9_new_framerate(cpi, 10000000.0 / this_duration); |
michael@0 | 3902 | } else { |
michael@0 | 3903 | // Average this frame's rate into the last second's average |
michael@0 | 3904 | // frame rate. If we haven't seen 1 second yet, then average |
michael@0 | 3905 | // over the whole interval seen. |
michael@0 | 3906 | const double interval = MIN((double)(cpi->source->ts_end |
michael@0 | 3907 | - cpi->first_time_stamp_ever), 10000000.0); |
michael@0 | 3908 | double avg_duration = 10000000.0 / cpi->oxcf.framerate; |
michael@0 | 3909 | avg_duration *= (interval - avg_duration + this_duration); |
michael@0 | 3910 | avg_duration /= interval; |
michael@0 | 3911 | |
michael@0 | 3912 | vp9_new_framerate(cpi, 10000000.0 / avg_duration); |
michael@0 | 3913 | } |
michael@0 | 3914 | } |
michael@0 | 3915 | |
michael@0 | 3916 | cpi->last_time_stamp_seen = cpi->source->ts_start; |
michael@0 | 3917 | cpi->last_end_time_stamp_seen = cpi->source->ts_end; |
michael@0 | 3918 | } |
michael@0 | 3919 | |
michael@0 | 3920 | // start with a 0 size frame |
michael@0 | 3921 | *size = 0; |
michael@0 | 3922 | |
michael@0 | 3923 | // Clear down mmx registers |
michael@0 | 3924 | vp9_clear_system_state(); // __asm emms; |
michael@0 | 3925 | |
michael@0 | 3926 | /* find a free buffer for the new frame, releasing the reference previously |
michael@0 | 3927 | * held. |
michael@0 | 3928 | */ |
michael@0 | 3929 | cm->fb_idx_ref_cnt[cm->new_fb_idx]--; |
michael@0 | 3930 | cm->new_fb_idx = get_free_fb(cm); |
michael@0 | 3931 | |
michael@0 | 3932 | #if CONFIG_MULTIPLE_ARF |
michael@0 | 3933 | /* Set up the correct ARF frame. */ |
michael@0 | 3934 | if (cpi->refresh_alt_ref_frame) { |
michael@0 | 3935 | ++cpi->arf_buffered; |
michael@0 | 3936 | } |
michael@0 | 3937 | if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) && |
michael@0 | 3938 | (cpi->pass == 2)) { |
michael@0 | 3939 | cpi->alt_fb_idx = cpi->arf_buffer_idx[cpi->sequence_number]; |
michael@0 | 3940 | } |
michael@0 | 3941 | #endif |
michael@0 | 3942 | |
michael@0 | 3943 | /* Get the mapping of L/G/A to the reference buffer pool */ |
michael@0 | 3944 | cm->active_ref_idx[0] = cm->ref_frame_map[cpi->lst_fb_idx]; |
michael@0 | 3945 | cm->active_ref_idx[1] = cm->ref_frame_map[cpi->gld_fb_idx]; |
michael@0 | 3946 | cm->active_ref_idx[2] = cm->ref_frame_map[cpi->alt_fb_idx]; |
michael@0 | 3947 | |
michael@0 | 3948 | #if 0 // CONFIG_MULTIPLE_ARF |
michael@0 | 3949 | if (cpi->multi_arf_enabled) { |
michael@0 | 3950 | fprintf(fp_out, " idx(%d, %d, %d, %d) active(%d, %d, %d)", |
michael@0 | 3951 | cpi->lst_fb_idx, cpi->gld_fb_idx, cpi->alt_fb_idx, cm->new_fb_idx, |
michael@0 | 3952 | cm->active_ref_idx[0], cm->active_ref_idx[1], cm->active_ref_idx[2]); |
michael@0 | 3953 | if (cpi->refresh_alt_ref_frame) |
michael@0 | 3954 | fprintf(fp_out, " type:ARF"); |
michael@0 | 3955 | if (cpi->is_src_frame_alt_ref) |
michael@0 | 3956 | fprintf(fp_out, " type:OVERLAY[%d]", cpi->alt_fb_idx); |
michael@0 | 3957 | fprintf(fp_out, "\n"); |
michael@0 | 3958 | } |
michael@0 | 3959 | #endif |
michael@0 | 3960 | |
michael@0 | 3961 | cm->frame_type = INTER_FRAME; |
michael@0 | 3962 | cm->frame_flags = *frame_flags; |
michael@0 | 3963 | |
michael@0 | 3964 | // Reset the frame pointers to the current frame size |
michael@0 | 3965 | vp9_realloc_frame_buffer(get_frame_new_buffer(cm), |
michael@0 | 3966 | cm->width, cm->height, |
michael@0 | 3967 | cm->subsampling_x, cm->subsampling_y, |
michael@0 | 3968 | VP9BORDERINPIXELS); |
michael@0 | 3969 | |
michael@0 | 3970 | // Calculate scaling factors for each of the 3 available references |
michael@0 | 3971 | for (i = 0; i < ALLOWED_REFS_PER_FRAME; ++i) |
michael@0 | 3972 | vp9_setup_scale_factors(cm, i); |
michael@0 | 3973 | |
michael@0 | 3974 | vp9_setup_interp_filters(&cpi->mb.e_mbd, DEFAULT_INTERP_FILTER, cm); |
michael@0 | 3975 | |
michael@0 | 3976 | if (cpi->oxcf.aq_mode == VARIANCE_AQ) { |
michael@0 | 3977 | vp9_vaq_init(); |
michael@0 | 3978 | } |
michael@0 | 3979 | |
michael@0 | 3980 | if (cpi->pass == 1) { |
michael@0 | 3981 | Pass1Encode(cpi, size, dest, frame_flags); |
michael@0 | 3982 | } else if (cpi->pass == 2) { |
michael@0 | 3983 | Pass2Encode(cpi, size, dest, frame_flags); |
michael@0 | 3984 | } else { |
michael@0 | 3985 | encode_frame_to_data_rate(cpi, size, dest, frame_flags); |
michael@0 | 3986 | } |
michael@0 | 3987 | |
michael@0 | 3988 | if (cm->refresh_frame_context) |
michael@0 | 3989 | cm->frame_contexts[cm->frame_context_idx] = cm->fc; |
michael@0 | 3990 | |
michael@0 | 3991 | if (*size > 0) { |
michael@0 | 3992 | // if its a dropped frame honor the requests on subsequent frames |
michael@0 | 3993 | cpi->droppable = !frame_is_reference(cpi); |
michael@0 | 3994 | |
michael@0 | 3995 | // return to normal state |
michael@0 | 3996 | cm->reset_frame_context = 0; |
michael@0 | 3997 | cm->refresh_frame_context = 1; |
michael@0 | 3998 | cpi->refresh_alt_ref_frame = 0; |
michael@0 | 3999 | cpi->refresh_golden_frame = 0; |
michael@0 | 4000 | cpi->refresh_last_frame = 1; |
michael@0 | 4001 | cm->frame_type = INTER_FRAME; |
michael@0 | 4002 | } |
michael@0 | 4003 | |
michael@0 | 4004 | vpx_usec_timer_mark(&cmptimer); |
michael@0 | 4005 | cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer); |
michael@0 | 4006 | |
michael@0 | 4007 | if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame) |
michael@0 | 4008 | generate_psnr_packet(cpi); |
michael@0 | 4009 | |
michael@0 | 4010 | #if CONFIG_INTERNAL_STATS |
michael@0 | 4011 | |
michael@0 | 4012 | if (cpi->pass != 1) { |
michael@0 | 4013 | cpi->bytes += *size; |
michael@0 | 4014 | |
michael@0 | 4015 | if (cm->show_frame) { |
michael@0 | 4016 | cpi->count++; |
michael@0 | 4017 | |
michael@0 | 4018 | if (cpi->b_calculate_psnr) { |
michael@0 | 4019 | double ye, ue, ve; |
michael@0 | 4020 | double frame_psnr; |
michael@0 | 4021 | YV12_BUFFER_CONFIG *orig = cpi->Source; |
michael@0 | 4022 | YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show; |
michael@0 | 4023 | YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer; |
michael@0 | 4024 | int y_samples = orig->y_height * orig->y_width; |
michael@0 | 4025 | int uv_samples = orig->uv_height * orig->uv_width; |
michael@0 | 4026 | int t_samples = y_samples + 2 * uv_samples; |
michael@0 | 4027 | double sq_error; |
michael@0 | 4028 | |
michael@0 | 4029 | ye = (double)calc_plane_error(orig->y_buffer, orig->y_stride, |
michael@0 | 4030 | recon->y_buffer, recon->y_stride, |
michael@0 | 4031 | orig->y_crop_width, orig->y_crop_height); |
michael@0 | 4032 | |
michael@0 | 4033 | ue = (double)calc_plane_error(orig->u_buffer, orig->uv_stride, |
michael@0 | 4034 | recon->u_buffer, recon->uv_stride, |
michael@0 | 4035 | orig->uv_crop_width, orig->uv_crop_height); |
michael@0 | 4036 | |
michael@0 | 4037 | ve = (double)calc_plane_error(orig->v_buffer, orig->uv_stride, |
michael@0 | 4038 | recon->v_buffer, recon->uv_stride, |
michael@0 | 4039 | orig->uv_crop_width, orig->uv_crop_height); |
michael@0 | 4040 | |
michael@0 | 4041 | sq_error = ye + ue + ve; |
michael@0 | 4042 | |
michael@0 | 4043 | frame_psnr = vp9_mse2psnr(t_samples, 255.0, sq_error); |
michael@0 | 4044 | |
michael@0 | 4045 | cpi->total_y += vp9_mse2psnr(y_samples, 255.0, ye); |
michael@0 | 4046 | cpi->total_u += vp9_mse2psnr(uv_samples, 255.0, ue); |
michael@0 | 4047 | cpi->total_v += vp9_mse2psnr(uv_samples, 255.0, ve); |
michael@0 | 4048 | cpi->total_sq_error += sq_error; |
michael@0 | 4049 | cpi->total += frame_psnr; |
michael@0 | 4050 | { |
michael@0 | 4051 | double frame_psnr2, frame_ssim2 = 0; |
michael@0 | 4052 | double weight = 0; |
michael@0 | 4053 | #if CONFIG_VP9_POSTPROC |
michael@0 | 4054 | vp9_deblock(cm->frame_to_show, &cm->post_proc_buffer, |
michael@0 | 4055 | cm->lf.filter_level * 10 / 6); |
michael@0 | 4056 | #endif |
michael@0 | 4057 | vp9_clear_system_state(); |
michael@0 | 4058 | |
michael@0 | 4059 | ye = (double)calc_plane_error(orig->y_buffer, orig->y_stride, |
michael@0 | 4060 | pp->y_buffer, pp->y_stride, |
michael@0 | 4061 | orig->y_crop_width, orig->y_crop_height); |
michael@0 | 4062 | |
michael@0 | 4063 | ue = (double)calc_plane_error(orig->u_buffer, orig->uv_stride, |
michael@0 | 4064 | pp->u_buffer, pp->uv_stride, |
michael@0 | 4065 | orig->uv_crop_width, orig->uv_crop_height); |
michael@0 | 4066 | |
michael@0 | 4067 | ve = (double)calc_plane_error(orig->v_buffer, orig->uv_stride, |
michael@0 | 4068 | pp->v_buffer, pp->uv_stride, |
michael@0 | 4069 | orig->uv_crop_width, orig->uv_crop_height); |
michael@0 | 4070 | |
michael@0 | 4071 | sq_error = ye + ue + ve; |
michael@0 | 4072 | |
michael@0 | 4073 | frame_psnr2 = vp9_mse2psnr(t_samples, 255.0, sq_error); |
michael@0 | 4074 | |
michael@0 | 4075 | cpi->totalp_y += vp9_mse2psnr(y_samples, 255.0, ye); |
michael@0 | 4076 | cpi->totalp_u += vp9_mse2psnr(uv_samples, 255.0, ue); |
michael@0 | 4077 | cpi->totalp_v += vp9_mse2psnr(uv_samples, 255.0, ve); |
michael@0 | 4078 | cpi->total_sq_error2 += sq_error; |
michael@0 | 4079 | cpi->totalp += frame_psnr2; |
michael@0 | 4080 | |
michael@0 | 4081 | frame_ssim2 = vp9_calc_ssim(cpi->Source, |
michael@0 | 4082 | recon, 1, &weight); |
michael@0 | 4083 | |
michael@0 | 4084 | cpi->summed_quality += frame_ssim2 * weight; |
michael@0 | 4085 | cpi->summed_weights += weight; |
michael@0 | 4086 | |
michael@0 | 4087 | frame_ssim2 = vp9_calc_ssim(cpi->Source, |
michael@0 | 4088 | &cm->post_proc_buffer, 1, &weight); |
michael@0 | 4089 | |
michael@0 | 4090 | cpi->summedp_quality += frame_ssim2 * weight; |
michael@0 | 4091 | cpi->summedp_weights += weight; |
michael@0 | 4092 | #if 0 |
michael@0 | 4093 | { |
michael@0 | 4094 | FILE *f = fopen("q_used.stt", "a"); |
michael@0 | 4095 | fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n", |
michael@0 | 4096 | cpi->common.current_video_frame, y2, u2, v2, |
michael@0 | 4097 | frame_psnr2, frame_ssim2); |
michael@0 | 4098 | fclose(f); |
michael@0 | 4099 | } |
michael@0 | 4100 | #endif |
michael@0 | 4101 | } |
michael@0 | 4102 | } |
michael@0 | 4103 | |
michael@0 | 4104 | if (cpi->b_calculate_ssimg) { |
michael@0 | 4105 | double y, u, v, frame_all; |
michael@0 | 4106 | frame_all = vp9_calc_ssimg(cpi->Source, cm->frame_to_show, |
michael@0 | 4107 | &y, &u, &v); |
michael@0 | 4108 | cpi->total_ssimg_y += y; |
michael@0 | 4109 | cpi->total_ssimg_u += u; |
michael@0 | 4110 | cpi->total_ssimg_v += v; |
michael@0 | 4111 | cpi->total_ssimg_all += frame_all; |
michael@0 | 4112 | } |
michael@0 | 4113 | } |
michael@0 | 4114 | } |
michael@0 | 4115 | |
michael@0 | 4116 | #endif |
michael@0 | 4117 | // fclose(fp_out); |
michael@0 | 4118 | return 0; |
michael@0 | 4119 | } |
michael@0 | 4120 | |
michael@0 | 4121 | int vp9_get_preview_raw_frame(VP9_PTR comp, YV12_BUFFER_CONFIG *dest, |
michael@0 | 4122 | vp9_ppflags_t *flags) { |
michael@0 | 4123 | VP9_COMP *cpi = (VP9_COMP *) comp; |
michael@0 | 4124 | |
michael@0 | 4125 | if (!cpi->common.show_frame) { |
michael@0 | 4126 | return -1; |
michael@0 | 4127 | } else { |
michael@0 | 4128 | int ret; |
michael@0 | 4129 | #if CONFIG_VP9_POSTPROC |
michael@0 | 4130 | ret = vp9_post_proc_frame(&cpi->common, dest, flags); |
michael@0 | 4131 | #else |
michael@0 | 4132 | |
michael@0 | 4133 | if (cpi->common.frame_to_show) { |
michael@0 | 4134 | *dest = *cpi->common.frame_to_show; |
michael@0 | 4135 | dest->y_width = cpi->common.width; |
michael@0 | 4136 | dest->y_height = cpi->common.height; |
michael@0 | 4137 | dest->uv_height = cpi->common.height / 2; |
michael@0 | 4138 | ret = 0; |
michael@0 | 4139 | } else { |
michael@0 | 4140 | ret = -1; |
michael@0 | 4141 | } |
michael@0 | 4142 | |
michael@0 | 4143 | #endif // !CONFIG_VP9_POSTPROC |
michael@0 | 4144 | vp9_clear_system_state(); |
michael@0 | 4145 | return ret; |
michael@0 | 4146 | } |
michael@0 | 4147 | } |
michael@0 | 4148 | |
michael@0 | 4149 | int vp9_set_roimap(VP9_PTR comp, unsigned char *map, unsigned int rows, |
michael@0 | 4150 | unsigned int cols, int delta_q[MAX_SEGMENTS], |
michael@0 | 4151 | int delta_lf[MAX_SEGMENTS], |
michael@0 | 4152 | unsigned int threshold[MAX_SEGMENTS]) { |
michael@0 | 4153 | VP9_COMP *cpi = (VP9_COMP *) comp; |
michael@0 | 4154 | signed char feature_data[SEG_LVL_MAX][MAX_SEGMENTS]; |
michael@0 | 4155 | struct segmentation *seg = &cpi->common.seg; |
michael@0 | 4156 | int i; |
michael@0 | 4157 | |
michael@0 | 4158 | if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols) |
michael@0 | 4159 | return -1; |
michael@0 | 4160 | |
michael@0 | 4161 | if (!map) { |
michael@0 | 4162 | vp9_disable_segmentation((VP9_PTR)cpi); |
michael@0 | 4163 | return 0; |
michael@0 | 4164 | } |
michael@0 | 4165 | |
michael@0 | 4166 | // Set the segmentation Map |
michael@0 | 4167 | vp9_set_segmentation_map((VP9_PTR)cpi, map); |
michael@0 | 4168 | |
michael@0 | 4169 | // Activate segmentation. |
michael@0 | 4170 | vp9_enable_segmentation((VP9_PTR)cpi); |
michael@0 | 4171 | |
michael@0 | 4172 | // Set up the quant, LF and breakout threshold segment data |
michael@0 | 4173 | for (i = 0; i < MAX_SEGMENTS; i++) { |
michael@0 | 4174 | feature_data[SEG_LVL_ALT_Q][i] = delta_q[i]; |
michael@0 | 4175 | feature_data[SEG_LVL_ALT_LF][i] = delta_lf[i]; |
michael@0 | 4176 | cpi->segment_encode_breakout[i] = threshold[i]; |
michael@0 | 4177 | } |
michael@0 | 4178 | |
michael@0 | 4179 | // Enable the loop and quant changes in the feature mask |
michael@0 | 4180 | for (i = 0; i < MAX_SEGMENTS; i++) { |
michael@0 | 4181 | if (delta_q[i]) |
michael@0 | 4182 | vp9_enable_segfeature(seg, i, SEG_LVL_ALT_Q); |
michael@0 | 4183 | else |
michael@0 | 4184 | vp9_disable_segfeature(seg, i, SEG_LVL_ALT_Q); |
michael@0 | 4185 | |
michael@0 | 4186 | if (delta_lf[i]) |
michael@0 | 4187 | vp9_enable_segfeature(seg, i, SEG_LVL_ALT_LF); |
michael@0 | 4188 | else |
michael@0 | 4189 | vp9_disable_segfeature(seg, i, SEG_LVL_ALT_LF); |
michael@0 | 4190 | } |
michael@0 | 4191 | |
michael@0 | 4192 | // Initialize the feature data structure |
michael@0 | 4193 | // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1 |
michael@0 | 4194 | vp9_set_segment_data((VP9_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA); |
michael@0 | 4195 | |
michael@0 | 4196 | return 0; |
michael@0 | 4197 | } |
michael@0 | 4198 | |
michael@0 | 4199 | int vp9_set_active_map(VP9_PTR comp, unsigned char *map, |
michael@0 | 4200 | unsigned int rows, unsigned int cols) { |
michael@0 | 4201 | VP9_COMP *cpi = (VP9_COMP *) comp; |
michael@0 | 4202 | |
michael@0 | 4203 | if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) { |
michael@0 | 4204 | if (map) { |
michael@0 | 4205 | vpx_memcpy(cpi->active_map, map, rows * cols); |
michael@0 | 4206 | cpi->active_map_enabled = 1; |
michael@0 | 4207 | } else { |
michael@0 | 4208 | cpi->active_map_enabled = 0; |
michael@0 | 4209 | } |
michael@0 | 4210 | |
michael@0 | 4211 | return 0; |
michael@0 | 4212 | } else { |
michael@0 | 4213 | // cpi->active_map_enabled = 0; |
michael@0 | 4214 | return -1; |
michael@0 | 4215 | } |
michael@0 | 4216 | } |
michael@0 | 4217 | |
michael@0 | 4218 | int vp9_set_internal_size(VP9_PTR comp, |
michael@0 | 4219 | VPX_SCALING horiz_mode, VPX_SCALING vert_mode) { |
michael@0 | 4220 | VP9_COMP *cpi = (VP9_COMP *) comp; |
michael@0 | 4221 | VP9_COMMON *cm = &cpi->common; |
michael@0 | 4222 | int hr = 0, hs = 0, vr = 0, vs = 0; |
michael@0 | 4223 | |
michael@0 | 4224 | if (horiz_mode > ONETWO || vert_mode > ONETWO) |
michael@0 | 4225 | return -1; |
michael@0 | 4226 | |
michael@0 | 4227 | Scale2Ratio(horiz_mode, &hr, &hs); |
michael@0 | 4228 | Scale2Ratio(vert_mode, &vr, &vs); |
michael@0 | 4229 | |
michael@0 | 4230 | // always go to the next whole number |
michael@0 | 4231 | cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs; |
michael@0 | 4232 | cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs; |
michael@0 | 4233 | |
michael@0 | 4234 | assert(cm->width <= cpi->initial_width); |
michael@0 | 4235 | assert(cm->height <= cpi->initial_height); |
michael@0 | 4236 | update_frame_size(cpi); |
michael@0 | 4237 | return 0; |
michael@0 | 4238 | } |
michael@0 | 4239 | |
michael@0 | 4240 | int vp9_set_size_literal(VP9_PTR comp, unsigned int width, |
michael@0 | 4241 | unsigned int height) { |
michael@0 | 4242 | VP9_COMP *cpi = (VP9_COMP *)comp; |
michael@0 | 4243 | VP9_COMMON *cm = &cpi->common; |
michael@0 | 4244 | |
michael@0 | 4245 | check_initial_width(cpi, NULL); |
michael@0 | 4246 | |
michael@0 | 4247 | if (width) { |
michael@0 | 4248 | cm->width = width; |
michael@0 | 4249 | if (cm->width * 5 < cpi->initial_width) { |
michael@0 | 4250 | cm->width = cpi->initial_width / 5 + 1; |
michael@0 | 4251 | printf("Warning: Desired width too small, changed to %d \n", cm->width); |
michael@0 | 4252 | } |
michael@0 | 4253 | if (cm->width > cpi->initial_width) { |
michael@0 | 4254 | cm->width = cpi->initial_width; |
michael@0 | 4255 | printf("Warning: Desired width too large, changed to %d \n", cm->width); |
michael@0 | 4256 | } |
michael@0 | 4257 | } |
michael@0 | 4258 | |
michael@0 | 4259 | if (height) { |
michael@0 | 4260 | cm->height = height; |
michael@0 | 4261 | if (cm->height * 5 < cpi->initial_height) { |
michael@0 | 4262 | cm->height = cpi->initial_height / 5 + 1; |
michael@0 | 4263 | printf("Warning: Desired height too small, changed to %d \n", cm->height); |
michael@0 | 4264 | } |
michael@0 | 4265 | if (cm->height > cpi->initial_height) { |
michael@0 | 4266 | cm->height = cpi->initial_height; |
michael@0 | 4267 | printf("Warning: Desired height too large, changed to %d \n", cm->height); |
michael@0 | 4268 | } |
michael@0 | 4269 | } |
michael@0 | 4270 | |
michael@0 | 4271 | assert(cm->width <= cpi->initial_width); |
michael@0 | 4272 | assert(cm->height <= cpi->initial_height); |
michael@0 | 4273 | update_frame_size(cpi); |
michael@0 | 4274 | return 0; |
michael@0 | 4275 | } |
michael@0 | 4276 | |
michael@0 | 4277 | void vp9_set_svc(VP9_PTR comp, int use_svc) { |
michael@0 | 4278 | VP9_COMP *cpi = (VP9_COMP *)comp; |
michael@0 | 4279 | cpi->use_svc = use_svc; |
michael@0 | 4280 | return; |
michael@0 | 4281 | } |
michael@0 | 4282 | |
michael@0 | 4283 | int vp9_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest) { |
michael@0 | 4284 | int i, j; |
michael@0 | 4285 | int total = 0; |
michael@0 | 4286 | |
michael@0 | 4287 | uint8_t *src = source->y_buffer; |
michael@0 | 4288 | uint8_t *dst = dest->y_buffer; |
michael@0 | 4289 | |
michael@0 | 4290 | // Loop through the Y plane raw and reconstruction data summing |
michael@0 | 4291 | // (square differences) |
michael@0 | 4292 | for (i = 0; i < source->y_height; i += 16) { |
michael@0 | 4293 | for (j = 0; j < source->y_width; j += 16) { |
michael@0 | 4294 | unsigned int sse; |
michael@0 | 4295 | total += vp9_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride, |
michael@0 | 4296 | &sse); |
michael@0 | 4297 | } |
michael@0 | 4298 | |
michael@0 | 4299 | src += 16 * source->y_stride; |
michael@0 | 4300 | dst += 16 * dest->y_stride; |
michael@0 | 4301 | } |
michael@0 | 4302 | |
michael@0 | 4303 | return total; |
michael@0 | 4304 | } |
michael@0 | 4305 | |
michael@0 | 4306 | |
michael@0 | 4307 | int vp9_get_quantizer(VP9_PTR c) { |
michael@0 | 4308 | return ((VP9_COMP *)c)->common.base_qindex; |
michael@0 | 4309 | } |