/media/libvpx/vp8/encoder/onyx_if.c
http://github.com/zpao/v8monkey · C · 5152 lines · 3740 code · 972 blank · 440 comment · 740 complexity · 724943c382813bdcad79d2b2804f0307 MD5 · raw file
Large files are truncated click here to view the full file
- /*
- * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
- *
- * Use of this source code is governed by a BSD-style license
- * that can be found in the LICENSE file in the root of the source
- * tree. An additional intellectual property rights grant can be found
- * in the file PATENTS. All contributing project authors may
- * be found in the AUTHORS file in the root of the source tree.
- */
- #include "vpx_config.h"
- #include "vp8/common/onyxc_int.h"
- #include "onyx_int.h"
- #include "vp8/common/systemdependent.h"
- #include "quantize.h"
- #include "vp8/common/alloccommon.h"
- #include "mcomp.h"
- #include "firstpass.h"
- #include "psnr.h"
- #include "vpx_scale/vpxscale.h"
- #include "vp8/common/extend.h"
- #include "ratectrl.h"
- #include "vp8/common/quant_common.h"
- #include "segmentation.h"
- #include "vp8/common/g_common.h"
- #include "vpx_scale/yv12extend.h"
- #if CONFIG_POSTPROC
- #include "vp8/common/postproc.h"
- #endif
- #include "vpx_mem/vpx_mem.h"
- #include "vp8/common/swapyv12buffer.h"
- #include "vp8/common/threading.h"
- #include "vpx_ports/vpx_timer.h"
- #include "temporal_filter.h"
- #if ARCH_ARM
- #include "vpx_ports/arm.h"
- #endif
- #include <math.h>
- #include <stdio.h>
- #include <limits.h>
- #if CONFIG_RUNTIME_CPU_DETECT
- #define IF_RTCD(x) (x)
- #define RTCD(x) &cpi->common.rtcd.x
- #else
- #define IF_RTCD(x) NULL
- #define RTCD(x) NULL
- #endif
- extern void vp8cx_pick_filter_level_fast(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
- extern void vp8cx_set_alt_lf_level(VP8_COMP *cpi, int filt_val);
- extern void vp8cx_pick_filter_level(YV12_BUFFER_CONFIG *sd, VP8_COMP *cpi);
- extern void vp8_dmachine_specific_config(VP8_COMP *cpi);
- extern void vp8_cmachine_specific_config(VP8_COMP *cpi);
- extern void vp8_deblock_frame(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *post, int filt_lvl, int low_var_thresh, int flag);
- extern void print_parms(VP8_CONFIG *ocf, char *filenam);
- extern unsigned int vp8_get_processor_freq();
- extern void print_tree_update_probs();
- extern void vp8cx_create_encoder_threads(VP8_COMP *cpi);
- extern void vp8cx_remove_encoder_threads(VP8_COMP *cpi);
- #if HAVE_ARMV7
- extern void vp8_yv12_copy_frame_func_neon(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc);
- extern void vp8_yv12_copy_src_frame_func_neon(YV12_BUFFER_CONFIG *src_ybc, YV12_BUFFER_CONFIG *dst_ybc);
- #endif
- int vp8_estimate_entropy_savings(VP8_COMP *cpi);
- int vp8_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest, const vp8_variance_rtcd_vtable_t *rtcd);
- extern void vp8_temporal_filter_prepare_c(VP8_COMP *cpi, int distance);
- static void set_default_lf_deltas(VP8_COMP *cpi);
- extern const int vp8_gf_interval_table[101];
- #if CONFIG_INTERNAL_STATS
- #include "math.h"
- extern double vp8_calc_ssim
- (
- YV12_BUFFER_CONFIG *source,
- YV12_BUFFER_CONFIG *dest,
- int lumamask,
- double *weight,
- const vp8_variance_rtcd_vtable_t *rtcd
- );
- extern double vp8_calc_ssimg
- (
- YV12_BUFFER_CONFIG *source,
- YV12_BUFFER_CONFIG *dest,
- double *ssim_y,
- double *ssim_u,
- double *ssim_v,
- const vp8_variance_rtcd_vtable_t *rtcd
- );
- #endif
- #ifdef OUTPUT_YUV_SRC
- FILE *yuv_file;
- #endif
- #if 0
- FILE *framepsnr;
- FILE *kf_list;
- FILE *keyfile;
- #endif
- #if 0
- extern int skip_true_count;
- extern int skip_false_count;
- #endif
- #ifdef ENTROPY_STATS
- extern int intra_mode_stats[10][10][10];
- #endif
- #ifdef SPEEDSTATS
- unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
- unsigned int tot_pm = 0;
- unsigned int cnt_pm = 0;
- unsigned int tot_ef = 0;
- unsigned int cnt_ef = 0;
- #endif
- #ifdef MODE_STATS
- extern unsigned __int64 Sectionbits[50];
- extern int y_modes[5] ;
- extern int uv_modes[4] ;
- extern int b_modes[10] ;
- extern int inter_y_modes[10] ;
- extern int inter_uv_modes[4] ;
- extern unsigned int inter_b_modes[15];
- #endif
- extern void (*vp8_short_fdct4x4)(short *input, short *output, int pitch);
- extern void (*vp8_short_fdct8x4)(short *input, short *output, int pitch);
- extern const int vp8_bits_per_mb[2][QINDEX_RANGE];
- extern const int qrounding_factors[129];
- extern const int qzbin_factors[129];
- extern void vp8cx_init_quantizer(VP8_COMP *cpi);
- extern const int vp8cx_base_skip_false_prob[128];
- // Tables relating active max Q to active min Q
- static const int kf_low_motion_minq[QINDEX_RANGE] =
- {
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
- 3,3,3,3,3,3,4,4,4,5,5,5,5,5,6,6,
- 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
- 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
- 16,16,17,17,18,18,18,18,19,20,20,21,21,22,23,23
- };
- static const int kf_high_motion_minq[QINDEX_RANGE] =
- {
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 1,1,1,1,1,1,1,1,2,2,2,2,3,3,3,3,
- 3,3,3,3,4,4,4,4,5,5,5,5,5,5,6,6,
- 6,6,7,7,8,8,8,8,9,9,10,10,10,10,11,11,
- 11,11,12,12,13,13,13,13,14,14,15,15,15,15,16,16,
- 16,16,17,17,18,18,18,18,19,19,20,20,20,20,21,21,
- 21,21,22,22,23,23,24,25,25,26,26,27,28,28,29,30
- };
- static const int gf_low_motion_minq[QINDEX_RANGE] =
- {
- 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,
- 3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,
- 7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,
- 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,
- 19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,
- 27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,
- 35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,
- 43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58
- };
- static const int gf_mid_motion_minq[QINDEX_RANGE] =
- {
- 0,0,0,0,1,1,1,1,1,1,2,2,3,3,3,4,
- 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
- 9,10,10,10,10,11,11,11,12,12,12,12,13,13,13,14,
- 14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,
- 22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,
- 30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,
- 38,39,39,40,40,41,41,42,42,43,43,44,45,46,47,48,
- 49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64
- };
- static const int gf_high_motion_minq[QINDEX_RANGE] =
- {
- 0,0,0,0,1,1,1,1,1,2,2,2,3,3,3,4,
- 4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,
- 9,10,10,10,11,11,12,12,13,13,14,14,15,15,16,16,
- 17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,
- 25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,
- 33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,
- 41,41,42,42,43,44,45,46,47,48,49,50,51,52,53,54,
- 55,56,57,58,59,60,62,64,66,68,70,72,74,76,78,80
- };
- static const int inter_minq[QINDEX_RANGE] =
- {
- 0,0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,
- 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,20,
- 20,21,22,22,23,24,24,25,26,27,27,28,29,30,30,31,
- 32,33,33,34,35,36,36,37,38,39,39,40,41,42,42,43,
- 44,45,46,46,47,48,49,50,50,51,52,53,54,55,55,56,
- 57,58,59,60,60,61,62,63,64,65,66,67,67,68,69,70,
- 71,72,73,74,75,75,76,77,78,79,80,81,82,83,84,85,
- 86,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
- };
- void vp8_initialize()
- {
- static int init_done = 0;
- if (!init_done)
- {
- vp8_scale_machine_specific_config();
- vp8_initialize_common();
- //vp8_dmachine_specific_config();
- vp8_tokenize_initialize();
- init_done = 1;
- }
- }
- #ifdef PACKET_TESTING
- extern FILE *vpxlogc;
- #endif
- static void setup_features(VP8_COMP *cpi)
- {
- // Set up default state for MB feature flags
- cpi->mb.e_mbd.segmentation_enabled = 0;
- cpi->mb.e_mbd.update_mb_segmentation_map = 0;
- cpi->mb.e_mbd.update_mb_segmentation_data = 0;
- vpx_memset(cpi->mb.e_mbd.mb_segment_tree_probs, 255, sizeof(cpi->mb.e_mbd.mb_segment_tree_probs));
- vpx_memset(cpi->mb.e_mbd.segment_feature_data, 0, sizeof(cpi->mb.e_mbd.segment_feature_data));
- cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 0;
- cpi->mb.e_mbd.mode_ref_lf_delta_update = 0;
- vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
- vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
- vpx_memset(cpi->mb.e_mbd.last_ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
- vpx_memset(cpi->mb.e_mbd.last_mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
- set_default_lf_deltas(cpi);
- }
- static void dealloc_compressor_data(VP8_COMP *cpi)
- {
- vpx_free(cpi->tplist);
- cpi->tplist = NULL;
- // Delete last frame MV storage buffers
- vpx_free(cpi->lfmv);
- cpi->lfmv = 0;
- vpx_free(cpi->lf_ref_frame_sign_bias);
- cpi->lf_ref_frame_sign_bias = 0;
- vpx_free(cpi->lf_ref_frame);
- cpi->lf_ref_frame = 0;
- // Delete sementation map
- vpx_free(cpi->segmentation_map);
- cpi->segmentation_map = 0;
- vpx_free(cpi->active_map);
- cpi->active_map = 0;
- vp8_de_alloc_frame_buffers(&cpi->common);
- vp8_yv12_de_alloc_frame_buffer(&cpi->last_frame_uf);
- vp8_yv12_de_alloc_frame_buffer(&cpi->scaled_source);
- #if VP8_TEMPORAL_ALT_REF
- vp8_yv12_de_alloc_frame_buffer(&cpi->alt_ref_buffer);
- #endif
- vp8_lookahead_destroy(cpi->lookahead);
- vpx_free(cpi->tok);
- cpi->tok = 0;
- // Structure used to monitor GF usage
- vpx_free(cpi->gf_active_flags);
- cpi->gf_active_flags = 0;
- // Activity mask based per mb zbin adjustments
- vpx_free(cpi->mb_activity_map);
- cpi->mb_activity_map = 0;
- vpx_free(cpi->mb_norm_activity_map);
- cpi->mb_norm_activity_map = 0;
- vpx_free(cpi->mb.pip);
- cpi->mb.pip = 0;
- #if !(CONFIG_REALTIME_ONLY)
- vpx_free(cpi->twopass.total_stats);
- cpi->twopass.total_stats = 0;
- vpx_free(cpi->twopass.this_frame_stats);
- cpi->twopass.this_frame_stats = 0;
- #endif
- }
- static void enable_segmentation(VP8_PTR ptr)
- {
- VP8_COMP *cpi = (VP8_COMP *)(ptr);
- // Set the appropriate feature bit
- cpi->mb.e_mbd.segmentation_enabled = 1;
- cpi->mb.e_mbd.update_mb_segmentation_map = 1;
- cpi->mb.e_mbd.update_mb_segmentation_data = 1;
- }
- static void disable_segmentation(VP8_PTR ptr)
- {
- VP8_COMP *cpi = (VP8_COMP *)(ptr);
- // Clear the appropriate feature bit
- cpi->mb.e_mbd.segmentation_enabled = 0;
- }
- // Valid values for a segment are 0 to 3
- // Segmentation map is arrange as [Rows][Columns]
- static void set_segmentation_map(VP8_PTR ptr, unsigned char *segmentation_map)
- {
- VP8_COMP *cpi = (VP8_COMP *)(ptr);
- // Copy in the new segmentation map
- vpx_memcpy(cpi->segmentation_map, segmentation_map, (cpi->common.mb_rows * cpi->common.mb_cols));
- // Signal that the map should be updated.
- cpi->mb.e_mbd.update_mb_segmentation_map = 1;
- cpi->mb.e_mbd.update_mb_segmentation_data = 1;
- }
- // The values given for each segment can be either deltas (from the default value chosen for the frame) or absolute values.
- //
- // Valid range for abs values is (0-127 for MB_LVL_ALT_Q) , (0-63 for SEGMENT_ALT_LF)
- // Valid range for delta values are (+/-127 for MB_LVL_ALT_Q) , (+/-63 for SEGMENT_ALT_LF)
- //
- // abs_delta = SEGMENT_DELTADATA (deltas) abs_delta = SEGMENT_ABSDATA (use the absolute values given).
- //
- //
- static void set_segment_data(VP8_PTR ptr, signed char *feature_data, unsigned char abs_delta)
- {
- VP8_COMP *cpi = (VP8_COMP *)(ptr);
- cpi->mb.e_mbd.mb_segement_abs_delta = abs_delta;
- vpx_memcpy(cpi->segment_feature_data, feature_data, sizeof(cpi->segment_feature_data));
- }
- static void segmentation_test_function(VP8_PTR ptr)
- {
- VP8_COMP *cpi = (VP8_COMP *)(ptr);
- unsigned char *seg_map;
- signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
- // Create a temporary map for segmentation data.
- CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
- // MB loop to set local segmentation map
- /*for ( i = 0; i < cpi->common.mb_rows; i++ )
- {
- for ( j = 0; j < cpi->common.mb_cols; j++ )
- {
- //seg_map[(i*cpi->common.mb_cols) + j] = (j % 2) + ((i%2)* 2);
- //if ( j < cpi->common.mb_cols/2 )
- // Segment 1 around the edge else 0
- if ( (i == 0) || (j == 0) || (i == (cpi->common.mb_rows-1)) || (j == (cpi->common.mb_cols-1)) )
- seg_map[(i*cpi->common.mb_cols) + j] = 1;
- //else if ( (i < 2) || (j < 2) || (i > (cpi->common.mb_rows-3)) || (j > (cpi->common.mb_cols-3)) )
- // seg_map[(i*cpi->common.mb_cols) + j] = 2;
- //else if ( (i < 5) || (j < 5) || (i > (cpi->common.mb_rows-6)) || (j > (cpi->common.mb_cols-6)) )
- // seg_map[(i*cpi->common.mb_cols) + j] = 3;
- else
- seg_map[(i*cpi->common.mb_cols) + j] = 0;
- }
- }*/
- // Set the segmentation Map
- set_segmentation_map(ptr, seg_map);
- // Activate segmentation.
- enable_segmentation(ptr);
- // Set up the quant segment data
- feature_data[MB_LVL_ALT_Q][0] = 0;
- feature_data[MB_LVL_ALT_Q][1] = 4;
- feature_data[MB_LVL_ALT_Q][2] = 0;
- feature_data[MB_LVL_ALT_Q][3] = 0;
- // Set up the loop segment data
- feature_data[MB_LVL_ALT_LF][0] = 0;
- feature_data[MB_LVL_ALT_LF][1] = 0;
- feature_data[MB_LVL_ALT_LF][2] = 0;
- feature_data[MB_LVL_ALT_LF][3] = 0;
- // Initialise the feature data structure
- // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
- set_segment_data(ptr, &feature_data[0][0], SEGMENT_DELTADATA);
- // Delete sementation map
- vpx_free(seg_map);
- seg_map = 0;
- }
- // A simple function to cyclically refresh the background at a lower Q
- static void cyclic_background_refresh(VP8_COMP *cpi, int Q, int lf_adjustment)
- {
- unsigned char *seg_map;
- signed char feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
- int i;
- int block_count = cpi->cyclic_refresh_mode_max_mbs_perframe;
- int mbs_in_frame = cpi->common.mb_rows * cpi->common.mb_cols;
- // Create a temporary map for segmentation data.
- CHECK_MEM_ERROR(seg_map, vpx_calloc(cpi->common.mb_rows * cpi->common.mb_cols, 1));
- cpi->cyclic_refresh_q = Q;
- for (i = Q; i > 0; i--)
- {
- if (vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*(Q + 128)) / 64))
- //if ( vp8_bits_per_mb[cpi->common.frame_type][i] >= ((vp8_bits_per_mb[cpi->common.frame_type][Q]*((2*Q)+96))/64) )
- {
- break;
- }
- }
- cpi->cyclic_refresh_q = i;
- // Only update for inter frames
- if (cpi->common.frame_type != KEY_FRAME)
- {
- // Cycle through the macro_block rows
- // MB loop to set local segmentation map
- for (i = cpi->cyclic_refresh_mode_index; i < mbs_in_frame; i++)
- {
- // If the MB is as a candidate for clean up then mark it for possible boost/refresh (segment 1)
- // The segment id may get reset to 0 later if the MB gets coded anything other than last frame 0,0
- // as only (last frame 0,0) MBs are eligable for refresh : that is to say Mbs likely to be background blocks.
- if (cpi->cyclic_refresh_map[i] == 0)
- {
- seg_map[i] = 1;
- }
- else
- {
- seg_map[i] = 0;
- // Skip blocks that have been refreshed recently anyway.
- if (cpi->cyclic_refresh_map[i] < 0)
- //cpi->cyclic_refresh_map[i] = cpi->cyclic_refresh_map[i] / 16;
- cpi->cyclic_refresh_map[i]++;
- }
- if (block_count > 0)
- block_count--;
- else
- break;
- }
- // If we have gone through the frame reset to the start
- cpi->cyclic_refresh_mode_index = i;
- if (cpi->cyclic_refresh_mode_index >= mbs_in_frame)
- cpi->cyclic_refresh_mode_index = 0;
- }
- // Set the segmentation Map
- set_segmentation_map((VP8_PTR)cpi, seg_map);
- // Activate segmentation.
- enable_segmentation((VP8_PTR)cpi);
- // Set up the quant segment data
- feature_data[MB_LVL_ALT_Q][0] = 0;
- feature_data[MB_LVL_ALT_Q][1] = (cpi->cyclic_refresh_q - Q);
- feature_data[MB_LVL_ALT_Q][2] = 0;
- feature_data[MB_LVL_ALT_Q][3] = 0;
- // Set up the loop segment data
- feature_data[MB_LVL_ALT_LF][0] = 0;
- feature_data[MB_LVL_ALT_LF][1] = lf_adjustment;
- feature_data[MB_LVL_ALT_LF][2] = 0;
- feature_data[MB_LVL_ALT_LF][3] = 0;
- // Initialise the feature data structure
- // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
- set_segment_data((VP8_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
- // Delete sementation map
- vpx_free(seg_map);
- seg_map = 0;
- }
- static void set_default_lf_deltas(VP8_COMP *cpi)
- {
- cpi->mb.e_mbd.mode_ref_lf_delta_enabled = 1;
- cpi->mb.e_mbd.mode_ref_lf_delta_update = 1;
- vpx_memset(cpi->mb.e_mbd.ref_lf_deltas, 0, sizeof(cpi->mb.e_mbd.ref_lf_deltas));
- vpx_memset(cpi->mb.e_mbd.mode_lf_deltas, 0, sizeof(cpi->mb.e_mbd.mode_lf_deltas));
- // Test of ref frame deltas
- cpi->mb.e_mbd.ref_lf_deltas[INTRA_FRAME] = 2;
- cpi->mb.e_mbd.ref_lf_deltas[LAST_FRAME] = 0;
- cpi->mb.e_mbd.ref_lf_deltas[GOLDEN_FRAME] = -2;
- cpi->mb.e_mbd.ref_lf_deltas[ALTREF_FRAME] = -2;
- cpi->mb.e_mbd.mode_lf_deltas[0] = 4; // BPRED
- cpi->mb.e_mbd.mode_lf_deltas[1] = -2; // Zero
- cpi->mb.e_mbd.mode_lf_deltas[2] = 2; // New mv
- cpi->mb.e_mbd.mode_lf_deltas[3] = 4; // Split mv
- }
- void vp8_set_speed_features(VP8_COMP *cpi)
- {
- SPEED_FEATURES *sf = &cpi->sf;
- int Mode = cpi->compressor_speed;
- int Speed = cpi->Speed;
- int i;
- VP8_COMMON *cm = &cpi->common;
- int last_improved_quant = sf->improved_quant;
- // Initialise default mode frequency sampling variables
- for (i = 0; i < MAX_MODES; i ++)
- {
- cpi->mode_check_freq[i] = 0;
- cpi->mode_test_hit_counts[i] = 0;
- cpi->mode_chosen_counts[i] = 0;
- }
- cpi->mbs_tested_so_far = 0;
- // best quality defaults
- sf->RD = 1;
- sf->search_method = NSTEP;
- sf->improved_quant = 1;
- sf->improved_dct = 1;
- sf->auto_filter = 1;
- sf->recode_loop = 1;
- sf->quarter_pixel_search = 1;
- sf->half_pixel_search = 1;
- sf->iterative_sub_pixel = 1;
- sf->optimize_coefficients = 1;
- sf->use_fastquant_for_pick = 0;
- sf->no_skip_block4x4_search = 1;
- sf->first_step = 0;
- sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
- sf->improved_mv_pred = 1;
- // default thresholds to 0
- for (i = 0; i < MAX_MODES; i++)
- sf->thresh_mult[i] = 0;
- switch (Mode)
- {
- #if !(CONFIG_REALTIME_ONLY)
- case 0: // best quality mode
- sf->thresh_mult[THR_ZEROMV ] = 0;
- sf->thresh_mult[THR_ZEROG ] = 0;
- sf->thresh_mult[THR_ZEROA ] = 0;
- sf->thresh_mult[THR_NEARESTMV] = 0;
- sf->thresh_mult[THR_NEARESTG ] = 0;
- sf->thresh_mult[THR_NEARESTA ] = 0;
- sf->thresh_mult[THR_NEARMV ] = 0;
- sf->thresh_mult[THR_NEARG ] = 0;
- sf->thresh_mult[THR_NEARA ] = 0;
- sf->thresh_mult[THR_DC ] = 0;
- sf->thresh_mult[THR_V_PRED ] = 1000;
- sf->thresh_mult[THR_H_PRED ] = 1000;
- sf->thresh_mult[THR_B_PRED ] = 2000;
- sf->thresh_mult[THR_TM ] = 1000;
- sf->thresh_mult[THR_NEWMV ] = 1000;
- sf->thresh_mult[THR_NEWG ] = 1000;
- sf->thresh_mult[THR_NEWA ] = 1000;
- sf->thresh_mult[THR_SPLITMV ] = 2500;
- sf->thresh_mult[THR_SPLITG ] = 5000;
- sf->thresh_mult[THR_SPLITA ] = 5000;
- sf->first_step = 0;
- sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
- break;
- case 1:
- case 3:
- sf->thresh_mult[THR_NEARESTMV] = 0;
- sf->thresh_mult[THR_ZEROMV ] = 0;
- sf->thresh_mult[THR_DC ] = 0;
- sf->thresh_mult[THR_NEARMV ] = 0;
- sf->thresh_mult[THR_V_PRED ] = 1000;
- sf->thresh_mult[THR_H_PRED ] = 1000;
- sf->thresh_mult[THR_B_PRED ] = 2500;
- sf->thresh_mult[THR_TM ] = 1000;
- sf->thresh_mult[THR_NEARESTG ] = 1000;
- sf->thresh_mult[THR_NEARESTA ] = 1000;
- sf->thresh_mult[THR_ZEROG ] = 1000;
- sf->thresh_mult[THR_ZEROA ] = 1000;
- sf->thresh_mult[THR_NEARG ] = 1000;
- sf->thresh_mult[THR_NEARA ] = 1000;
- #if 1
- sf->thresh_mult[THR_ZEROMV ] = 0;
- sf->thresh_mult[THR_ZEROG ] = 0;
- sf->thresh_mult[THR_ZEROA ] = 0;
- sf->thresh_mult[THR_NEARESTMV] = 0;
- sf->thresh_mult[THR_NEARESTG ] = 0;
- sf->thresh_mult[THR_NEARESTA ] = 0;
- sf->thresh_mult[THR_NEARMV ] = 0;
- sf->thresh_mult[THR_NEARG ] = 0;
- sf->thresh_mult[THR_NEARA ] = 0;
- // sf->thresh_mult[THR_DC ] = 0;
- // sf->thresh_mult[THR_V_PRED ] = 1000;
- // sf->thresh_mult[THR_H_PRED ] = 1000;
- // sf->thresh_mult[THR_B_PRED ] = 2000;
- // sf->thresh_mult[THR_TM ] = 1000;
- sf->thresh_mult[THR_NEWMV ] = 1000;
- sf->thresh_mult[THR_NEWG ] = 1000;
- sf->thresh_mult[THR_NEWA ] = 1000;
- sf->thresh_mult[THR_SPLITMV ] = 1700;
- sf->thresh_mult[THR_SPLITG ] = 4500;
- sf->thresh_mult[THR_SPLITA ] = 4500;
- #else
- sf->thresh_mult[THR_NEWMV ] = 1500;
- sf->thresh_mult[THR_NEWG ] = 1500;
- sf->thresh_mult[THR_NEWA ] = 1500;
- sf->thresh_mult[THR_SPLITMV ] = 5000;
- sf->thresh_mult[THR_SPLITG ] = 10000;
- sf->thresh_mult[THR_SPLITA ] = 10000;
- #endif
- if (Speed > 0)
- {
- /* Disable coefficient optimization above speed 0 */
- sf->optimize_coefficients = 0;
- sf->use_fastquant_for_pick = 1;
- sf->no_skip_block4x4_search = 0;
- sf->first_step = 1;
- cpi->mode_check_freq[THR_SPLITG] = 2;
- cpi->mode_check_freq[THR_SPLITA] = 2;
- cpi->mode_check_freq[THR_SPLITMV] = 0;
- }
- if (Speed > 1)
- {
- cpi->mode_check_freq[THR_SPLITG] = 4;
- cpi->mode_check_freq[THR_SPLITA] = 4;
- cpi->mode_check_freq[THR_SPLITMV] = 2;
- sf->thresh_mult[THR_TM ] = 1500;
- sf->thresh_mult[THR_V_PRED ] = 1500;
- sf->thresh_mult[THR_H_PRED ] = 1500;
- sf->thresh_mult[THR_B_PRED ] = 5000;
- if (cpi->ref_frame_flags & VP8_LAST_FLAG)
- {
- sf->thresh_mult[THR_NEWMV ] = 2000;
- sf->thresh_mult[THR_SPLITMV ] = 10000;
- }
- if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
- {
- sf->thresh_mult[THR_NEARESTG ] = 1500;
- sf->thresh_mult[THR_ZEROG ] = 1500;
- sf->thresh_mult[THR_NEARG ] = 1500;
- sf->thresh_mult[THR_NEWG ] = 2000;
- sf->thresh_mult[THR_SPLITG ] = 20000;
- }
- if (cpi->ref_frame_flags & VP8_ALT_FLAG)
- {
- sf->thresh_mult[THR_NEARESTA ] = 1500;
- sf->thresh_mult[THR_ZEROA ] = 1500;
- sf->thresh_mult[THR_NEARA ] = 1500;
- sf->thresh_mult[THR_NEWA ] = 2000;
- sf->thresh_mult[THR_SPLITA ] = 20000;
- }
- }
- if (Speed > 2)
- {
- cpi->mode_check_freq[THR_SPLITG] = 15;
- cpi->mode_check_freq[THR_SPLITA] = 15;
- cpi->mode_check_freq[THR_SPLITMV] = 7;
- sf->thresh_mult[THR_TM ] = 2000;
- sf->thresh_mult[THR_V_PRED ] = 2000;
- sf->thresh_mult[THR_H_PRED ] = 2000;
- sf->thresh_mult[THR_B_PRED ] = 7500;
- if (cpi->ref_frame_flags & VP8_LAST_FLAG)
- {
- sf->thresh_mult[THR_NEWMV ] = 2000;
- sf->thresh_mult[THR_SPLITMV ] = 25000;
- }
- if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
- {
- sf->thresh_mult[THR_NEARESTG ] = 2000;
- sf->thresh_mult[THR_ZEROG ] = 2000;
- sf->thresh_mult[THR_NEARG ] = 2000;
- sf->thresh_mult[THR_NEWG ] = 2500;
- sf->thresh_mult[THR_SPLITG ] = 50000;
- }
- if (cpi->ref_frame_flags & VP8_ALT_FLAG)
- {
- sf->thresh_mult[THR_NEARESTA ] = 2000;
- sf->thresh_mult[THR_ZEROA ] = 2000;
- sf->thresh_mult[THR_NEARA ] = 2000;
- sf->thresh_mult[THR_NEWA ] = 2500;
- sf->thresh_mult[THR_SPLITA ] = 50000;
- }
- sf->improved_quant = 0;
- sf->improved_dct = 0;
- // Only do recode loop on key frames, golden frames and
- // alt ref frames
- sf->recode_loop = 2;
- }
- if (Speed > 3)
- {
- sf->thresh_mult[THR_SPLITA ] = INT_MAX;
- sf->thresh_mult[THR_SPLITG ] = INT_MAX;
- sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
- cpi->mode_check_freq[THR_V_PRED] = 0;
- cpi->mode_check_freq[THR_H_PRED] = 0;
- cpi->mode_check_freq[THR_B_PRED] = 0;
- cpi->mode_check_freq[THR_NEARG] = 0;
- cpi->mode_check_freq[THR_NEWG] = 0;
- cpi->mode_check_freq[THR_NEARA] = 0;
- cpi->mode_check_freq[THR_NEWA] = 0;
- sf->auto_filter = 1;
- sf->recode_loop = 0; // recode loop off
- sf->RD = 0; // Turn rd off
- }
- if (Speed > 4)
- {
- sf->auto_filter = 0; // Faster selection of loop filter
- cpi->mode_check_freq[THR_V_PRED] = 2;
- cpi->mode_check_freq[THR_H_PRED] = 2;
- cpi->mode_check_freq[THR_B_PRED] = 2;
- if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
- {
- cpi->mode_check_freq[THR_NEARG] = 2;
- cpi->mode_check_freq[THR_NEWG] = 4;
- }
- if (cpi->ref_frame_flags & VP8_ALT_FLAG)
- {
- cpi->mode_check_freq[THR_NEARA] = 2;
- cpi->mode_check_freq[THR_NEWA] = 4;
- }
- if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
- {
- sf->thresh_mult[THR_NEARESTG ] = 2000;
- sf->thresh_mult[THR_ZEROG ] = 2000;
- sf->thresh_mult[THR_NEARG ] = 2000;
- sf->thresh_mult[THR_NEWG ] = 4000;
- }
- if (cpi->ref_frame_flags & VP8_ALT_FLAG)
- {
- sf->thresh_mult[THR_NEARESTA ] = 2000;
- sf->thresh_mult[THR_ZEROA ] = 2000;
- sf->thresh_mult[THR_NEARA ] = 2000;
- sf->thresh_mult[THR_NEWA ] = 4000;
- }
- }
- break;
- #endif
- case 2:
- sf->optimize_coefficients = 0;
- sf->recode_loop = 0;
- sf->auto_filter = 1;
- sf->iterative_sub_pixel = 1;
- sf->thresh_mult[THR_NEARESTMV] = 0;
- sf->thresh_mult[THR_ZEROMV ] = 0;
- sf->thresh_mult[THR_DC ] = 0;
- sf->thresh_mult[THR_TM ] = 0;
- sf->thresh_mult[THR_NEARMV ] = 0;
- sf->thresh_mult[THR_V_PRED ] = 1000;
- sf->thresh_mult[THR_H_PRED ] = 1000;
- sf->thresh_mult[THR_B_PRED ] = 2500;
- sf->thresh_mult[THR_NEARESTG ] = 1000;
- sf->thresh_mult[THR_ZEROG ] = 1000;
- sf->thresh_mult[THR_NEARG ] = 1000;
- sf->thresh_mult[THR_NEARESTA ] = 1000;
- sf->thresh_mult[THR_ZEROA ] = 1000;
- sf->thresh_mult[THR_NEARA ] = 1000;
- sf->thresh_mult[THR_NEWMV ] = 2000;
- sf->thresh_mult[THR_NEWG ] = 2000;
- sf->thresh_mult[THR_NEWA ] = 2000;
- sf->thresh_mult[THR_SPLITMV ] = 5000;
- sf->thresh_mult[THR_SPLITG ] = 10000;
- sf->thresh_mult[THR_SPLITA ] = 10000;
- sf->search_method = NSTEP;
- if (Speed > 0)
- {
- cpi->mode_check_freq[THR_SPLITG] = 4;
- cpi->mode_check_freq[THR_SPLITA] = 4;
- cpi->mode_check_freq[THR_SPLITMV] = 2;
- sf->thresh_mult[THR_DC ] = 0;
- sf->thresh_mult[THR_TM ] = 1000;
- sf->thresh_mult[THR_V_PRED ] = 2000;
- sf->thresh_mult[THR_H_PRED ] = 2000;
- sf->thresh_mult[THR_B_PRED ] = 5000;
- if (cpi->ref_frame_flags & VP8_LAST_FLAG)
- {
- sf->thresh_mult[THR_NEARESTMV] = 0;
- sf->thresh_mult[THR_ZEROMV ] = 0;
- sf->thresh_mult[THR_NEARMV ] = 0;
- sf->thresh_mult[THR_NEWMV ] = 2000;
- sf->thresh_mult[THR_SPLITMV ] = 10000;
- }
- if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
- {
- sf->thresh_mult[THR_NEARESTG ] = 1000;
- sf->thresh_mult[THR_ZEROG ] = 1000;
- sf->thresh_mult[THR_NEARG ] = 1000;
- sf->thresh_mult[THR_NEWG ] = 2000;
- sf->thresh_mult[THR_SPLITG ] = 20000;
- }
- if (cpi->ref_frame_flags & VP8_ALT_FLAG)
- {
- sf->thresh_mult[THR_NEARESTA ] = 1000;
- sf->thresh_mult[THR_ZEROA ] = 1000;
- sf->thresh_mult[THR_NEARA ] = 1000;
- sf->thresh_mult[THR_NEWA ] = 2000;
- sf->thresh_mult[THR_SPLITA ] = 20000;
- }
- sf->improved_quant = 0;
- sf->improved_dct = 0;
- sf->use_fastquant_for_pick = 1;
- sf->no_skip_block4x4_search = 0;
- sf->first_step = 1;
- }
- if (Speed > 1)
- {
- cpi->mode_check_freq[THR_SPLITMV] = 7;
- cpi->mode_check_freq[THR_SPLITG] = 15;
- cpi->mode_check_freq[THR_SPLITA] = 15;
- sf->thresh_mult[THR_TM ] = 2000;
- sf->thresh_mult[THR_V_PRED ] = 2000;
- sf->thresh_mult[THR_H_PRED ] = 2000;
- sf->thresh_mult[THR_B_PRED ] = 5000;
- if (cpi->ref_frame_flags & VP8_LAST_FLAG)
- {
- sf->thresh_mult[THR_NEWMV ] = 2000;
- sf->thresh_mult[THR_SPLITMV ] = 25000;
- }
- if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
- {
- sf->thresh_mult[THR_NEARESTG ] = 2000;
- sf->thresh_mult[THR_ZEROG ] = 2000;
- sf->thresh_mult[THR_NEARG ] = 2000;
- sf->thresh_mult[THR_NEWG ] = 2500;
- sf->thresh_mult[THR_SPLITG ] = 50000;
- }
- if (cpi->ref_frame_flags & VP8_ALT_FLAG)
- {
- sf->thresh_mult[THR_NEARESTA ] = 2000;
- sf->thresh_mult[THR_ZEROA ] = 2000;
- sf->thresh_mult[THR_NEARA ] = 2000;
- sf->thresh_mult[THR_NEWA ] = 2500;
- sf->thresh_mult[THR_SPLITA ] = 50000;
- }
- }
- if (Speed > 2)
- {
- sf->auto_filter = 0; // Faster selection of loop filter
- cpi->mode_check_freq[THR_V_PRED] = 2;
- cpi->mode_check_freq[THR_H_PRED] = 2;
- cpi->mode_check_freq[THR_B_PRED] = 2;
- if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
- {
- cpi->mode_check_freq[THR_NEARG] = 2;
- cpi->mode_check_freq[THR_NEWG] = 4;
- }
- if (cpi->ref_frame_flags & VP8_ALT_FLAG)
- {
- cpi->mode_check_freq[THR_NEARA] = 2;
- cpi->mode_check_freq[THR_NEWA] = 4;
- }
- sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
- sf->thresh_mult[THR_SPLITG ] = INT_MAX;
- sf->thresh_mult[THR_SPLITA ] = INT_MAX;
- }
- if (Speed > 3)
- {
- sf->RD = 0;
- sf->auto_filter = 1;
- }
- if (Speed > 4)
- {
- sf->auto_filter = 0; // Faster selection of loop filter
- sf->search_method = HEX;
- //sf->search_method = DIAMOND;
- sf->iterative_sub_pixel = 0;
- cpi->mode_check_freq[THR_V_PRED] = 4;
- cpi->mode_check_freq[THR_H_PRED] = 4;
- cpi->mode_check_freq[THR_B_PRED] = 4;
- if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
- {
- cpi->mode_check_freq[THR_NEARG] = 2;
- cpi->mode_check_freq[THR_NEWG] = 4;
- }
- if (cpi->ref_frame_flags & VP8_ALT_FLAG)
- {
- cpi->mode_check_freq[THR_NEARA] = 2;
- cpi->mode_check_freq[THR_NEWA] = 4;
- }
- sf->thresh_mult[THR_TM ] = 2000;
- sf->thresh_mult[THR_B_PRED ] = 5000;
- if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
- {
- sf->thresh_mult[THR_NEARESTG ] = 2000;
- sf->thresh_mult[THR_ZEROG ] = 2000;
- sf->thresh_mult[THR_NEARG ] = 2000;
- sf->thresh_mult[THR_NEWG ] = 4000;
- }
- if (cpi->ref_frame_flags & VP8_ALT_FLAG)
- {
- sf->thresh_mult[THR_NEARESTA ] = 2000;
- sf->thresh_mult[THR_ZEROA ] = 2000;
- sf->thresh_mult[THR_NEARA ] = 2000;
- sf->thresh_mult[THR_NEWA ] = 4000;
- }
- }
- if (Speed > 5)
- {
- // Disable split MB intra prediction mode
- sf->thresh_mult[THR_B_PRED] = INT_MAX;
- }
- if (Speed > 6)
- {
- unsigned int i, sum = 0;
- unsigned int total_mbs = cm->MBs;
- int thresh;
- int total_skip;
- int min = 2000;
- if (cpi->oxcf.encode_breakout > 2000)
- min = cpi->oxcf.encode_breakout;
- min >>= 7;
- for (i = 0; i < min; i++)
- {
- sum += cpi->error_bins[i];
- }
- total_skip = sum;
- sum = 0;
- // i starts from 2 to make sure thresh started from 2048
- for (; i < 1024; i++)
- {
- sum += cpi->error_bins[i];
- if (10 * sum >= (unsigned int)(cpi->Speed - 6)*(total_mbs - total_skip))
- break;
- }
- i--;
- thresh = (i << 7);
- if (thresh < 2000)
- thresh = 2000;
- if (cpi->ref_frame_flags & VP8_LAST_FLAG)
- {
- sf->thresh_mult[THR_NEWMV] = thresh;
- sf->thresh_mult[THR_NEARESTMV ] = thresh >> 1;
- sf->thresh_mult[THR_NEARMV ] = thresh >> 1;
- }
- if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
- {
- sf->thresh_mult[THR_NEWG] = thresh << 1;
- sf->thresh_mult[THR_NEARESTG ] = thresh;
- sf->thresh_mult[THR_NEARG ] = thresh;
- }
- if (cpi->ref_frame_flags & VP8_ALT_FLAG)
- {
- sf->thresh_mult[THR_NEWA] = thresh << 1;
- sf->thresh_mult[THR_NEARESTA ] = thresh;
- sf->thresh_mult[THR_NEARA ] = thresh;
- }
- // Disable other intra prediction modes
- sf->thresh_mult[THR_TM] = INT_MAX;
- sf->thresh_mult[THR_V_PRED] = INT_MAX;
- sf->thresh_mult[THR_H_PRED] = INT_MAX;
- sf->improved_mv_pred = 0;
- }
- if (Speed > 8)
- {
- sf->quarter_pixel_search = 0;
- }
- if (Speed > 9)
- {
- int Tmp = cpi->Speed - 8;
- if (Tmp > 4)
- Tmp = 4;
- if (cpi->ref_frame_flags & VP8_GOLD_FLAG)
- {
- cpi->mode_check_freq[THR_ZEROG] = 1 << (Tmp - 1);
- cpi->mode_check_freq[THR_NEARESTG] = 1 << (Tmp - 1);
- cpi->mode_check_freq[THR_NEARG] = 1 << Tmp;
- cpi->mode_check_freq[THR_NEWG] = 1 << (Tmp + 1);
- }
- if (cpi->ref_frame_flags & VP8_ALT_FLAG)
- {
- cpi->mode_check_freq[THR_ZEROA] = 1 << (Tmp - 1);
- cpi->mode_check_freq[THR_NEARESTA] = 1 << (Tmp - 1);
- cpi->mode_check_freq[THR_NEARA] = 1 << Tmp;
- cpi->mode_check_freq[THR_NEWA] = 1 << (Tmp + 1);
- }
- cpi->mode_check_freq[THR_NEWMV] = 1 << (Tmp - 1);
- }
- cm->filter_type = NORMAL_LOOPFILTER;
- if (Speed >= 14)
- cm->filter_type = SIMPLE_LOOPFILTER;
- if (Speed >= 15)
- {
- sf->half_pixel_search = 0; // This has a big hit on quality. Last resort
- }
- vpx_memset(cpi->error_bins, 0, sizeof(cpi->error_bins));
- }; /* switch */
- /* disable frame modes if flags not set */
- if (!(cpi->ref_frame_flags & VP8_LAST_FLAG))
- {
- sf->thresh_mult[THR_NEWMV ] = INT_MAX;
- sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
- sf->thresh_mult[THR_ZEROMV ] = INT_MAX;
- sf->thresh_mult[THR_NEARMV ] = INT_MAX;
- sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
- }
- if (!(cpi->ref_frame_flags & VP8_GOLD_FLAG))
- {
- sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
- sf->thresh_mult[THR_ZEROG ] = INT_MAX;
- sf->thresh_mult[THR_NEARG ] = INT_MAX;
- sf->thresh_mult[THR_NEWG ] = INT_MAX;
- sf->thresh_mult[THR_SPLITG ] = INT_MAX;
- }
- if (!(cpi->ref_frame_flags & VP8_ALT_FLAG))
- {
- sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
- sf->thresh_mult[THR_ZEROA ] = INT_MAX;
- sf->thresh_mult[THR_NEARA ] = INT_MAX;
- sf->thresh_mult[THR_NEWA ] = INT_MAX;
- sf->thresh_mult[THR_SPLITA ] = INT_MAX;
- }
- // Slow quant, dct and trellis not worthwhile for first pass
- // so make sure they are always turned off.
- if ( cpi->pass == 1 )
- {
- sf->improved_quant = 0;
- sf->optimize_coefficients = 0;
- sf->improved_dct = 0;
- }
- if (cpi->sf.search_method == NSTEP)
- {
- vp8_init3smotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
- }
- else if (cpi->sf.search_method == DIAMOND)
- {
- vp8_init_dsmotion_compensation(&cpi->mb, cm->yv12_fb[cm->lst_fb_idx].y_stride);
- }
- if (cpi->sf.improved_dct)
- {
- cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short8x4);
- cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, short4x4);
- }
- else
- {
- cpi->mb.vp8_short_fdct8x4 = FDCT_INVOKE(&cpi->rtcd.fdct, fast8x4);
- cpi->mb.vp8_short_fdct4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, fast4x4);
- }
- cpi->mb.short_walsh4x4 = FDCT_INVOKE(&cpi->rtcd.fdct, walsh_short4x4);
- if (cpi->sf.improved_quant)
- {
- cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
- quantb);
- cpi->mb.quantize_b_pair = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
- quantb_pair);
- }
- else
- {
- cpi->mb.quantize_b = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
- fastquantb);
- cpi->mb.quantize_b_pair = QUANTIZE_INVOKE(&cpi->rtcd.quantize,
- fastquantb_pair);
- }
- if (cpi->sf.improved_quant != last_improved_quant)
- vp8cx_init_quantizer(cpi);
- #if CONFIG_RUNTIME_CPU_DETECT
- cpi->mb.e_mbd.rtcd = &cpi->common.rtcd;
- #endif
- if (cpi->sf.iterative_sub_pixel == 1)
- {
- cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step_iteratively;
- }
- else if (cpi->sf.quarter_pixel_search)
- {
- cpi->find_fractional_mv_step = vp8_find_best_sub_pixel_step;
- }
- else if (cpi->sf.half_pixel_search)
- {
- cpi->find_fractional_mv_step = vp8_find_best_half_pixel_step;
- }
- else
- {
- cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
- }
- if (cpi->sf.optimize_coefficients == 1 && cpi->pass!=1)
- cpi->mb.optimize = 1;
- else
- cpi->mb.optimize = 0;
- if (cpi->common.full_pixel)
- cpi->find_fractional_mv_step = vp8_skip_fractional_mv_step;
- #ifdef SPEEDSTATS
- frames_at_speed[cpi->Speed]++;
- #endif
- }
- static void alloc_raw_frame_buffers(VP8_COMP *cpi)
- {
- int width = (cpi->oxcf.Width + 15) & ~15;
- int height = (cpi->oxcf.Height + 15) & ~15;
- cpi->lookahead = vp8_lookahead_init(cpi->oxcf.Width, cpi->oxcf.Height,
- cpi->oxcf.lag_in_frames);
- if(!cpi->lookahead)
- vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
- "Failed to allocate lag buffers");
- #if VP8_TEMPORAL_ALT_REF
- if (vp8_yv12_alloc_frame_buffer(&cpi->alt_ref_buffer,
- width, height, VP8BORDERINPIXELS))
- vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
- "Failed to allocate altref buffer");
- #endif
- }
- static int vp8_alloc_partition_data(VP8_COMP *cpi)
- {
- vpx_free(cpi->mb.pip);
- cpi->mb.pip = vpx_calloc((cpi->common.mb_cols + 1) *
- (cpi->common.mb_rows + 1),
- sizeof(PARTITION_INFO));
- if(!cpi->mb.pip)
- return 1;
- cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
- return 0;
- }
- void vp8_alloc_compressor_data(VP8_COMP *cpi)
- {
- VP8_COMMON *cm = & cpi->common;
- int width = cm->Width;
- int height = cm->Height;
- if (vp8_alloc_frame_buffers(cm, width, height))
- vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
- "Failed to allocate frame buffers");
- if (vp8_alloc_partition_data(cpi))
- vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
- "Failed to allocate partition data");
- if ((width & 0xf) != 0)
- width += 16 - (width & 0xf);
- if ((height & 0xf) != 0)
- height += 16 - (height & 0xf);
- if (vp8_yv12_alloc_frame_buffer(&cpi->last_frame_uf,
- width, height, VP8BORDERINPIXELS))
- vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
- "Failed to allocate last frame buffer");
- if (vp8_yv12_alloc_frame_buffer(&cpi->scaled_source,
- width, height, VP8BORDERINPIXELS))
- vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
- "Failed to allocate scaled source buffer");
- vpx_free(cpi->tok);
- {
- unsigned int tokens = cm->mb_rows * cm->mb_cols * 24 * 16;
- CHECK_MEM_ERROR(cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
- }
- // Data used for real time vc mode to see if gf needs refreshing
- cpi->inter_zz_count = 0;
- cpi->gf_bad_count = 0;
- cpi->gf_update_recommended = 0;
- // Structures used to minitor GF usage
- vpx_free(cpi->gf_active_flags);
- CHECK_MEM_ERROR(cpi->gf_active_flags,
- vpx_calloc(1, cm->mb_rows * cm->mb_cols));
- cpi->gf_active_count = cm->mb_rows * cm->mb_cols;
- vpx_free(cpi->mb_activity_map);
- CHECK_MEM_ERROR(cpi->mb_activity_map,
- vpx_calloc(sizeof(unsigned int),
- cm->mb_rows * cm->mb_cols));
- vpx_free(cpi->mb_norm_activity_map);
- CHECK_MEM_ERROR(cpi->mb_norm_activity_map,
- vpx_calloc(sizeof(unsigned int),
- cm->mb_rows * cm->mb_cols));
- #if !(CONFIG_REALTIME_ONLY)
- vpx_free(cpi->twopass.total_stats);
- cpi->twopass.total_stats = vpx_calloc(1, sizeof(FIRSTPASS_STATS));
- vpx_free(cpi->twopass.this_frame_stats);
- cpi->twopass.this_frame_stats = vpx_calloc(1, sizeof(FIRSTPASS_STATS));
- if(!cpi->twopass.total_stats || !cpi->twopass.this_frame_stats)
- vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
- "Failed to allocate firstpass stats");
- #endif
- #if CONFIG_MULTITHREAD
- if (width < 640)
- cpi->mt_sync_range = 1;
- else if (width <= 1280)
- cpi->mt_sync_range = 4;
- else if (width <= 2560)
- cpi->mt_sync_range = 8;
- else
- cpi->mt_sync_range = 16;
- #endif
- vpx_free(cpi->tplist);
- CHECK_MEM_ERROR(cpi->tplist, vpx_malloc(sizeof(TOKENLIST) * cpi->common.mb_rows));
- }
- // Quant MOD
- static const int q_trans[] =
- {
- 0, 1, 2, 3, 4, 5, 7, 8,
- 9, 10, 12, 13, 15, 17, 18, 19,
- 20, 21, 23, 24, 25, 26, 27, 28,
- 29, 30, 31, 33, 35, 37, 39, 41,
- 43, 45, 47, 49, 51, 53, 55, 57,
- 59, 61, 64, 67, 70, 73, 76, 79,
- 82, 85, 88, 91, 94, 97, 100, 103,
- 106, 109, 112, 115, 118, 121, 124, 127,
- };
- int vp8_reverse_trans(int x)
- {
- int i;
- for (i = 0; i < 64; i++)
- if (q_trans[i] >= x)
- return i;
- return 63;
- };
- void vp8_new_frame_rate(VP8_COMP *cpi, double framerate)
- {
- if(framerate < .1)
- framerate = 30;
- cpi->oxcf.frame_rate = framerate;
- cpi->output_frame_rate = cpi->oxcf.frame_rate;
- cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
- cpi->av_per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_frame_rate);
- cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
- // Set Maximum gf/arf interval
- cpi->max_gf_interval = ((int)(cpi->output_frame_rate / 2.0) + 2);
- if(cpi->max_gf_interval < 12)
- cpi->max_gf_interval = 12;
- // Extended interval for genuinely static scenes
- cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
- // Special conditions when altr ref frame enabled in lagged compress mode
- if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames)
- {
- if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1)
- cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
- if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1)
- cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
- }
- if ( cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval )
- cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval;
- }
- static int
- rescale(int val, int num, int denom)
- {
- int64_t llnum = num;
- int64_t llden = denom;
- int64_t llval = val;
- return llval * llnum / llden;
- }
- static void init_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
- {
- VP8_COMP *cpi = (VP8_COMP *)(ptr);
- VP8_COMMON *cm = &cpi->common;
- cpi->oxcf = *oxcf;
- cpi->auto_gold = 1;
- cpi->auto_adjust_gold_quantizer = 1;
- cpi->goldfreq = 7;
- cm->version = oxcf->Version;
- vp8_setup_version(cm);
- // change includes all joint functionality
- vp8_change_config(ptr, oxcf);
- // Initialize active best and worst q and average q values.
- cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
- cpi->active_best_quality = cpi->oxcf.best_allowed_q;
- cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
- // Initialise the starting buffer levels
- cpi->buffer_level = cpi->oxcf.starting_buffer_level;
- cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
- cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
- cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
- cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
- cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
- cpi->total_actual_bits = 0;
- cpi->total_target_vs_actual = 0;
- #if VP8_TEMPORAL_ALT_REF
- {
- int i;
- cpi->fixed_divide[0] = 0;
- for (i = 1; i < 512; i++)
- cpi->fixed_divide[i] = 0x80000 / i;
- }
- #endif
- }
- void vp8_change_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
- {
- VP8_COMP *cpi = (VP8_COMP *)(ptr);
- VP8_COMMON *cm = &cpi->common;
- if (!cpi)
- return;
- if (!oxcf)
- return;
- if (cm->version != oxcf->Version)
- {
- cm->version = oxcf->Version;
- vp8_setup_version(cm);
- }
- cpi->oxcf = *oxcf;
- switch (cpi->oxcf.Mode)
- {
- case MODE_REALTIME:
- cpi->pass = 0;
- cpi->compressor_speed = 2;
- if (cpi->oxcf.cpu_used < -16)
- {
- cpi->oxcf.cpu_used = -16;
- }
- if (cpi->oxcf.cpu_used > 16)
- cpi->oxcf.cpu_used = 16;
- break;
- case MODE_GOODQUALITY:
- cpi->pass = 0;
- cpi->compressor_speed = 1;
- if (cpi->oxcf.cpu_used < -5)
- {
- cpi->oxcf.cpu_used = -5;
- }
- if (cpi->oxcf.cpu_used > 5)
- cpi->oxcf.cpu_used = 5;
- break;
- case MODE_BESTQUALITY:
- cpi->pass = 0;
- cpi->compressor_speed = 0;
- break;
- case MODE_FIRSTPASS:
- cpi->pass = 1;
- cpi->compressor_speed = 1;
- break;
- case MODE_SECONDPASS:
- cpi->pass = 2;
- cpi->compressor_speed = 1;
- if (cpi->oxcf.cpu_used < -5)
- {
- cpi->oxcf.cpu_used = -5;
- }
- if (cpi->oxcf.cpu_used > 5)
- cpi->oxcf.cpu_used = 5;
- break;
- case MODE_SECONDPASS_BEST:
- cpi->pass = 2;
- cpi->compressor_speed = 0;
- break;
- }
- if (cpi->pass == 0)
- cpi->auto_worst_q = 1;
- cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
- cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
- cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
- if (oxcf->fixed_q >= 0)
- {
- if (oxcf->worst_allowed_q < 0)
- cpi->oxcf.fixed_q = q_trans[0];
- else
- cpi->oxcf.fixed_q = q_trans[oxcf->worst_allowed_q];
- if (oxcf->alt_q < 0)
- cpi->oxcf.alt_q = q_trans[0];
- else
- cpi->oxcf.alt_q = q_trans[oxcf->alt_q];
- if (oxcf->key_q < 0)
- cpi->oxcf.key_q = q_trans[0];
- else
- cpi->oxcf.key_q = q_trans[oxcf->key_q];
- if (oxcf->gold_q < 0)
- cpi->oxcf.gold_q = q_trans[0];
- else
- cpi->oxcf.gold_q = q_trans[oxcf->gold_q];
- }
- cpi->baseline_gf_interval =
- cpi->oxcf.alt_freq ? cpi->oxcf.alt_freq : DEFAULT_GF_INTERVAL;
- cpi->ref_frame_flags = VP8_ALT_FLAG | VP8_GOLD_FLAG | VP8_LAST_FLAG;
- //cpi->use_golden_frame_only = 0;
- //cpi->use_last_frame_only =…