Lines Matching refs:cpi

84 void vp9_init_quantizer(VP9_COMP *cpi);
112 static void set_high_precision_mv(VP9_COMP *cpi, int allow_high_precision_mv) {
113 MACROBLOCK *const mb = &cpi->mb;
114 cpi->common.allow_high_precision_mv = allow_high_precision_mv;
115 if (cpi->common.allow_high_precision_mv) {
124 static void setup_key_frame(VP9_COMP *cpi) {
125 vp9_setup_past_independence(&cpi->common);
128 cpi->refresh_golden_frame = 1;
129 cpi->refresh_alt_ref_frame = 1;
157 static void dealloc_compressor_data(VP9_COMP *cpi) {
158 VP9_COMMON *const cm = &cpi->common;
162 vpx_free(cpi->segmentation_map);
163 cpi->segmentation_map = NULL;
166 vpx_free(cpi->coding_context.last_frame_seg_map_copy);
167 cpi->coding_context.last_frame_seg_map_copy = NULL;
169 vpx_free(cpi->complexity_map);
170 cpi->complexity_map = NULL;
172 vp9_cyclic_refresh_free(cpi->cyclic_refresh);
173 cpi->cyclic_refresh = NULL;
175 vpx_free(cpi->active_map);
176 cpi->active_map = NULL;
180 vp9_free_frame_buffer(&cpi->last_frame_uf);
181 vp9_free_frame_buffer(&cpi->scaled_source);
182 vp9_free_frame_buffer(&cpi->scaled_last_source);
183 vp9_free_frame_buffer(&cpi->alt_ref_buffer);
184 vp9_lookahead_destroy(cpi->lookahead);
186 vpx_free(cpi->tok);
187 cpi->tok = 0;
190 vpx_free(cpi->mb_activity_map);
191 cpi->mb_activity_map = 0;
192 vpx_free(cpi->mb_norm_activity_map);
193 cpi->mb_norm_activity_map = 0;
195 for (i = 0; i < cpi->svc.number_spatial_layers; ++i) {
196 LAYER_CONTEXT *const lc = &cpi->svc.layer_context[i];
203 static void save_coding_context(VP9_COMP *cpi) {
204 CODING_CONTEXT *const cc = &cpi->coding_context;
205 VP9_COMMON *cm = &cpi->common;
211 vp9_copy(cc->nmvjointcost, cpi->mb.nmvjointcost);
212 vp9_copy(cc->nmvcosts, cpi->mb.nmvcosts);
213 vp9_copy(cc->nmvcosts_hp, cpi->mb.nmvcosts_hp);
217 vpx_memcpy(cpi->coding_context.last_frame_seg_map_copy,
226 static void restore_coding_context(VP9_COMP *cpi) {
227 CODING_CONTEXT *const cc = &cpi->coding_context;
228 VP9_COMMON *cm = &cpi->common;
232 vp9_copy(cpi->mb.nmvjointcost, cc->nmvjointcost);
233 vp9_copy(cpi->mb.nmvcosts, cc->nmvcosts);
234 vp9_copy(cpi->mb.nmvcosts_hp, cc->nmvcosts_hp);
239 cpi->coding_context.last_frame_seg_map_copy,
248 static void configure_static_seg_features(VP9_COMP *cpi) {
249 VP9_COMMON *const cm = &cpi->common;
250 const RATE_CONTROL *const rc = &cpi->rc;
259 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
262 cpi->static_mb_pct = 0;
269 } else if (cpi->refresh_alt_ref_frame) {
272 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
275 cpi->static_mb_pct = 0;
283 vp9_update_mbgraph_stats(cpi);
320 if (high_q || (cpi->static_mb_pct == 100)) {
331 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
371 static void print_seg_map(VP9_COMP *cpi) {
372 VP9_COMMON *cm = &cpi->common;
379 for (row = 0; row < cpi->common.mi_rows; row++) {
380 for (col = 0; col < cpi->common.mi_cols; col++) {
381 fprintf(statsfile, "%10d", cpi->segmentation_map[map_index]);
391 static void update_reference_segmentation_map(VP9_COMP *cpi) {
392 VP9_COMMON *const cm = &cpi->common;
410 static void set_rd_speed_thresholds(VP9_COMP *cpi) {
415 cpi->rd_thresh_mult[i] = is_slowest_mode(cpi->oxcf.mode) ? -500 : 0;
417 cpi->rd_thresh_mult[THR_NEARESTMV] = 0;
418 cpi->rd_thresh_mult[THR_NEARESTG] = 0;
419 cpi->rd_thresh_mult[THR_NEARESTA] = 0;
421 cpi->rd_thresh_mult[THR_DC] += 1000;
423 cpi->rd_thresh_mult[THR_NEWMV] += 1000;
424 cpi->rd_thresh_mult[THR_NEWA] += 1000;
425 cpi->rd_thresh_mult[THR_NEWG] += 1000;
427 cpi->rd_thresh_mult[THR_NEARMV] += 1000;
428 cpi->rd_thresh_mult[THR_NEARA] += 1000;
429 cpi->rd_thresh_mult[THR_COMP_NEARESTLA] += 1000;
430 cpi->rd_thresh_mult[THR_COMP_NEARESTGA] += 1000;
432 cpi->rd_thresh_mult[THR_TM] += 1000;
434 cpi->rd_thresh_mult[THR_COMP_NEARLA] += 1500;
435 cpi->rd_thresh_mult[THR_COMP_NEWLA] += 2000;
436 cpi->rd_thresh_mult[THR_NEARG] += 1000;
437 cpi->rd_thresh_mult[THR_COMP_NEARGA] += 1500;
438 cpi->rd_thresh_mult[THR_COMP_NEWGA] += 2000;
440 cpi->rd_thresh_mult[THR_ZEROMV] += 2000;
441 cpi->rd_thresh_mult[THR_ZEROG] += 2000;
442 cpi->rd_thresh_mult[THR_ZEROA] += 2000;
443 cpi->rd_thresh_mult[THR_COMP_ZEROLA] += 2500;
444 cpi->rd_thresh_mult[THR_COMP_ZEROGA] += 2500;
446 cpi->rd_thresh_mult[THR_H_PRED] += 2000;
447 cpi->rd_thresh_mult[THR_V_PRED] += 2000;
448 cpi->rd_thresh_mult[THR_D45_PRED ] += 2500;
449 cpi->rd_thresh_mult[THR_D135_PRED] += 2500;
450 cpi->rd_thresh_mult[THR_D117_PRED] += 2500;
451 cpi->rd_thresh_mult[THR_D153_PRED] += 2500;
452 cpi->rd_thresh_mult[THR_D207_PRED] += 2500;
453 cpi->rd_thresh_mult[THR_D63_PRED] += 2500;
456 if (!(cpi->ref_frame_flags & VP9_LAST_FLAG)) {
457 cpi->rd_thresh_mult[THR_NEWMV ] = INT_MAX;
458 cpi->rd_thresh_mult[THR_NEARESTMV] = INT_MAX;
459 cpi->rd_thresh_mult[THR_ZEROMV ] = INT_MAX;
460 cpi->rd_thresh_mult[THR_NEARMV ] = INT_MAX;
462 if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG)) {
463 cpi->rd_thresh_mult[THR_NEARESTG ] = INT_MAX;
464 cpi->rd_thresh_mult[THR_ZEROG ] = INT_MAX;
465 cpi->rd_thresh_mult[THR_NEARG ] = INT_MAX;
466 cpi->rd_thresh_mult[THR_NEWG ] = INT_MAX;
468 if (!(cpi->ref_frame_flags & VP9_ALT_FLAG)) {
469 cpi->rd_thresh_mult[THR_NEARESTA ] = INT_MAX;
470 cpi->rd_thresh_mult[THR_ZEROA ] = INT_MAX;
471 cpi->rd_thresh_mult[THR_NEARA ] = INT_MAX;
472 cpi->rd_thresh_mult[THR_NEWA ] = INT_MAX;
475 if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
477 cpi->rd_thresh_mult[THR_COMP_ZEROLA ] = INT_MAX;
478 cpi->rd_thresh_mult[THR_COMP_NEARESTLA] = INT_MAX;
479 cpi->rd_thresh_mult[THR_COMP_NEARLA ] = INT_MAX;
480 cpi->rd_thresh_mult[THR_COMP_NEWLA ] = INT_MAX;
482 if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
484 cpi->rd_thresh_mult[THR_COMP_ZEROGA ] = INT_MAX;
485 cpi->rd_thresh_mult[THR_COMP_NEARESTGA] = INT_MAX;
486 cpi->rd_thresh_mult[THR_COMP_NEARGA ] = INT_MAX;
487 cpi->rd_thresh_mult[THR_COMP_NEWGA ] = INT_MAX;
491 static void set_rd_speed_thresholds_sub8x8(VP9_COMP *cpi) {
492 const SPEED_FEATURES *const sf = &cpi->sf;
496 cpi->rd_thresh_mult_sub8x8[i] = is_slowest_mode(cpi->oxcf.mode) ? -500 : 0;
498 cpi->rd_thresh_mult_sub8x8[THR_LAST] += 2500;
499 cpi->rd_thresh_mult_sub8x8[THR_GOLD] += 2500;
500 cpi->rd_thresh_mult_sub8x8[THR_ALTR] += 2500;
501 cpi->rd_thresh_mult_sub8x8[THR_INTRA] += 2500;
502 cpi->rd_thresh_mult_sub8x8[THR_COMP_LA] += 4500;
503 cpi->rd_thresh_mult_sub8x8[THR_COMP_GA] += 4500;
508 cpi->rd_thresh_mult_sub8x8[i] = INT_MAX;
511 if (!(cpi->ref_frame_flags & VP9_LAST_FLAG))
512 cpi->rd_thresh_mult_sub8x8[THR_LAST] = INT_MAX;
513 if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG))
514 cpi->rd_thresh_mult_sub8x8[THR_GOLD] = INT_MAX;
515 if (!(cpi->ref_frame_flags & VP9_ALT_FLAG))
516 cpi->rd_thresh_mult_sub8x8[THR_ALTR] = INT_MAX;
517 if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
519 cpi->rd_thresh_mult_sub8x8[THR_COMP_LA] = INT_MAX;
520 if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
522 cpi->rd_thresh_mult_sub8x8[THR_COMP_GA] = INT_MAX;
525 static void set_speed_features(VP9_COMP *cpi) {
529 cpi->mode_chosen_counts[i] = 0;
532 vp9_set_speed_features(cpi);
535 set_rd_speed_thresholds(cpi);
536 set_rd_speed_thresholds_sub8x8(cpi);
538 cpi->mb.fwd_txm4x4 = vp9_fdct4x4;
539 if (cpi->oxcf.lossless || cpi->mb.e_mbd.lossless) {
540 cpi->mb.fwd_txm4x4 = vp9_fwht4x4;
544 static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
545 VP9_COMMON *cm = &cpi->common;
546 const VP9_CONFIG *oxcf = &cpi->oxcf;
548 cpi->lookahead = vp9_lookahead_init(oxcf->width, oxcf->height,
551 if (!cpi->lookahead)
555 if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
563 void vp9_alloc_compressor_data(VP9_COMP *cpi) {
564 VP9_COMMON *cm = &cpi->common;
570 if (vp9_alloc_frame_buffer(&cpi->last_frame_uf,
577 if (vp9_alloc_frame_buffer(&cpi->scaled_source,
584 if (vp9_alloc_frame_buffer(&cpi->scaled_last_source,
591 vpx_free(cpi->tok);
596 CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
599 vpx_free(cpi->mb_activity_map);
600 CHECK_MEM_ERROR(cm, cpi->mb_activity_map,
604 vpx_free(cpi->mb_norm_activity_map);
605 CHECK_MEM_ERROR(cm, cpi->mb_norm_activity_map,
611 static void update_frame_size(VP9_COMP *cpi) {
612 VP9_COMMON *const cm = &cpi->common;
613 MACROBLOCKD *const xd = &cpi->mb.e_mbd;
618 if (vp9_realloc_frame_buffer(&cpi->last_frame_uf,
625 if (vp9_realloc_frame_buffer(&cpi->scaled_source,
632 if (vp9_realloc_frame_buffer(&cpi->scaled_last_source,
640 int y_stride = cpi->scaled_source.y_stride;
642 if (cpi->sf.search_method == NSTEP) {
643 vp9_init3smotion_compensation(&cpi->mb, y_stride);
644 } else if (cpi->sf.search_method == DIAMOND) {
645 vp9_init_dsmotion_compensation(&cpi->mb, y_stride);
675 void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
676 VP9_COMMON *const cm = &cpi->common;
677 RATE_CONTROL *const rc = &cpi->rc;
678 VP9_CONFIG *const oxcf = &cpi->oxcf;
682 cpi->output_framerate = cpi->oxcf.framerate;
684 cpi->output_framerate);
707 rc->static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
730 static void set_tile_limits(VP9_COMP *cpi) {
731 VP9_COMMON *const cm = &cpi->common;
736 cm->log2_tile_cols = clamp(cpi->oxcf.tile_columns,
738 cm->log2_tile_rows = cpi->oxcf.tile_rows;
741 static void init_config(struct VP9_COMP *cpi, VP9_CONFIG *oxcf) {
742 VP9_COMMON *const cm = &cpi->common;
745 cpi->oxcf = *oxcf;
754 vp9_alloc_compressor_data(cpi);
757 cpi->svc.number_spatial_layers = oxcf->ss_number_layers;
759 cpi->svc.number_temporal_layers = oxcf->ts_number_layers;
761 if ((cpi->svc.number_temporal_layers > 1 &&
762 cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) ||
763 (cpi->svc.number_spatial_layers > 1 &&
764 cpi->oxcf.mode == MODE_SECONDPASS_BEST)) {
765 vp9_init_layer_context(cpi);
769 vp9_change_config(cpi, oxcf);
771 cpi->static_mb_pct = 0;
773 cpi->lst_fb_idx = 0;
774 cpi->gld_fb_idx = 1;
775 cpi->alt_fb_idx = 2;
777 set_tile_limits(cpi);
779 cpi->fixed_divide[0] = 0;
781 cpi->fixed_divide[i] = 0x80000 / i;
784 void vp9_change_config(struct VP9_COMP *cpi, const VP9_CONFIG *oxcf) {
785 VP9_COMMON *const cm = &cpi->common;
786 RATE_CONTROL *const rc = &cpi->rc;
797 cpi->oxcf = *oxcf;
799 if (cpi->oxcf.cpu_used == -6)
800 cpi->oxcf.play_alternate = 0;
802 switch (cpi->oxcf.mode) {
805 cpi->pass = 0;
806 cpi->oxcf.cpu_used = clamp(cpi->oxcf.cpu_used, -5, 5);
810 cpi->pass = 0;
814 cpi->pass = 1;
818 cpi->pass = 2;
819 cpi->oxcf.cpu_used = clamp(cpi->oxcf.cpu_used, -5, 5);
823 cpi->pass = 2;
827 cpi->pass = 0;
831 cpi->oxcf.lossless = oxcf->lossless;
832 if (cpi->oxcf.lossless) {
835 cpi->oxcf.worst_allowed_q = 0;
836 cpi->oxcf.best_allowed_q = 0;
837 cpi->mb.e_mbd.itxm_add = vp9_iwht4x4_add;
839 cpi->mb.e_mbd.itxm_add = vp9_idct4x4_add;
842 cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
844 cpi->refresh_golden_frame = 0;
845 cpi->refresh_last_frame = 1;
850 set_high_precision_mv(cpi, 0);
856 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
858 cpi->encode_breakout = cpi->oxcf.encode_breakout;
861 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK) {
862 cpi->oxcf.starting_buffer_level = 60000;
863 cpi->oxcf.optimal_buffer_level = 60000;
864 cpi->oxcf.maximum_buffer_size = 240000;
868 cpi->oxcf.target_bandwidth *= 1000;
870 cpi->oxcf.starting_buffer_level =
871 vp9_rescale(cpi->oxcf.starting_buffer_level,
872 cpi->oxcf.target_bandwidth, 1000);
875 if (cpi->oxcf.optimal_buffer_level == 0)
876 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
878 cpi->oxcf.optimal_buffer_level =
879 vp9_rescale(cpi->oxcf.optimal_buffer_level,
880 cpi->oxcf.target_bandwidth, 1000);
882 if (cpi->oxcf.maximum_buffer_size == 0)
883 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
885 cpi->oxcf.maximum_buffer_size =
886 vp9_rescale(cpi->oxcf.maximum_buffer_size,
887 cpi->oxcf.target_bandwidth, 1000);
890 rc->bits_off_target = MIN(rc->bits_off_target, cpi->oxcf.maximum_buffer_size);
891 rc->buffer_level = MIN(rc->buffer_level, cpi->oxcf.maximum_buffer_size);
894 vp9_new_framerate(cpi, cpi->oxcf.framerate);
897 rc->worst_quality = cpi->oxcf.worst_allowed_q;
898 rc->best_quality = cpi->oxcf.best_allowed_q;
902 cpi->cq_target_quality = cpi->oxcf.cq_level;
906 cm->display_width = cpi->oxcf.width;
907 cm->display_height = cpi->oxcf.height;
910 cpi->oxcf.sharpness = MIN(7, cpi->oxcf.sharpness);
912 cpi->common.lf.sharpness_level = cpi->oxcf.sharpness;
914 if (cpi->initial_width) {
918 assert(cm->width <= cpi->initial_width);
919 assert(cm->height <= cpi->initial_height);
921 update_frame_size(cpi);
923 if ((cpi->svc.number_temporal_layers > 1 &&
924 cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) ||
925 (cpi->svc.number_spatial_layers > 1 && cpi->pass == 2)) {
926 vp9_update_layer_context_change_config(cpi,
927 (int)cpi->oxcf.target_bandwidth);
930 cpi->speed = abs(cpi->oxcf.cpu_used);
933 if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS)
934 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
937 vp9_zero(cpi->alt_ref_source);
939 cpi->alt_ref_source = NULL;
945 cpi->frame_distortion = 0;
946 cpi->last_frame_distortion = 0;
949 set_tile_limits(cpi);
951 cpi->ext_refresh_frame_flags_pending = 0;
952 cpi->ext_refresh_frame_context_pending = 0;
1039 static void init_pick_mode_context(VP9_COMP *cpi) {
1041 VP9_COMMON *const cm = &cpi->common;
1042 MACROBLOCK *const x = &cpi->mb;
1116 VP9_COMP *const cpi = vpx_memalign(32, sizeof(VP9_COMP));
1117 VP9_COMMON *const cm = cpi != NULL ? &cpi->common : NULL;
1122 vp9_zero(*cpi);
1126 vp9_remove_compressor(cpi);
1132 CHECK_MEM_ERROR(cm, cpi->mb.ss, vpx_calloc(sizeof(search_site),
1137 cpi->use_svc = 0;
1139 init_config(cpi, oxcf);
1140 vp9_rc_init(&cpi->oxcf, cpi->pass, &cpi->rc);
1141 init_pick_mode_context(cpi);
1148 cpi->gold_is_last = 0;
1149 cpi->alt_is_last = 0;
1150 cpi->gold_is_alt = 0;
1153 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
1157 CHECK_MEM_ERROR(cm, cpi->complexity_map,
1161 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
1166 CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy,
1169 CHECK_MEM_ERROR(cm, cpi->active_map, vpx_calloc(cm->MBs, 1));
1170 vpx_memset(cpi->active_map, 1, cm->MBs);
1171 cpi->active_map_enabled = 0;
1173 for (i = 0; i < (sizeof(cpi->mbgraph_stats) /
1174 sizeof(cpi->mbgraph_stats[0])); i++) {
1175 CHECK_MEM_ERROR(cm, cpi->mbgraph_stats[i].mb_stats,
1177 sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
1181 cpi->activity_avg = 90 << 12;
1182 cpi->key_frame_frequency = cpi->oxcf.key_freq;
1183 cpi->refresh_alt_ref_frame = 0;
1188 cpi->multi_arf_enabled = 1;
1190 if (cpi->multi_arf_enabled) {
1191 cpi->sequence_number = 0;
1192 cpi->frame_coding_order_period = 0;
1193 vp9_zero(cpi->frame_coding_order);
1194 vp9_zero(cpi->arf_buffer_idx);
1198 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1200 cpi->b_calculate_ssimg = 0;
1202 cpi->count = 0;
1203 cpi->bytes = 0;
1205 if (cpi->b_calculate_psnr) {
1206 cpi->total_y = 0.0;
1207 cpi->total_u = 0.0;
1208 cpi->total_v = 0.0;
1209 cpi->total = 0.0;
1210 cpi->total_sq_error = 0;
1211 cpi->total_samples = 0;
1213 cpi->totalp_y = 0.0;
1214 cpi->totalp_u = 0.0;
1215 cpi->totalp_v = 0.0;
1216 cpi->totalp = 0.0;
1217 cpi->totalp_sq_error = 0;
1218 cpi->totalp_samples = 0;
1220 cpi->tot_recode_hits = 0;
1221 cpi->summed_quality = 0;
1222 cpi->summed_weights = 0;
1223 cpi->summedp_quality = 0;
1224 cpi->summedp_weights = 0;
1227 if (cpi->b_calculate_ssimg) {
1228 cpi->total_ssimg_y = 0;
1229 cpi->total_ssimg_u = 0;
1230 cpi->total_ssimg_v = 0;
1231 cpi->total_ssimg_all = 0;
1236 cpi->first_time_stamp_ever = INT64_MAX;
1238 cal_nmvjointsadcost(cpi->mb.nmvjointsadcost);
1239 cpi->mb.nmvcost[0] = &cpi->mb.nmvcosts[0][MV_MAX];
1240 cpi->mb.nmvcost[1] = &cpi->mb.nmvcosts[1][MV_MAX];
1241 cpi->mb.nmvsadcost[0] = &cpi->mb.nmvsadcosts[0][MV_MAX];
1242 cpi->mb.nmvsadcost[1] = &cpi->mb.nmvsadcosts[1][MV_MAX];
1243 cal_nmvsadcosts(cpi->mb.nmvsadcost);
1245 cpi->mb.nmvcost_hp[0] = &cpi->mb.nmvcosts_hp[0][MV_MAX];
1246 cpi->mb.nmvcost_hp[1] = &cpi->mb.nmvcosts_hp[1][MV_MAX];
1247 cpi->mb.nmvsadcost_hp[0] = &cpi->mb.nmvsadcosts_hp[0][MV_MAX];
1248 cpi->mb.nmvsadcost_hp[1] = &cpi->mb.nmvsadcosts_hp[1][MV_MAX];
1249 cal_nmvsadcosts_hp(cpi->mb.nmvsadcost_hp);
1263 cpi->output_pkt_list = oxcf->output_pkt_list;
1265 cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
1267 if (cpi->pass == 1) {
1268 vp9_init_first_pass(cpi);
1269 } else if (cpi->pass == 2) {
1273 if (cpi->svc.number_spatial_layers > 1
1274 && cpi->svc.number_temporal_layers == 1) {
1285 LAYER_CONTEXT *const lc = &cpi->svc.layer_context[layer_id];
1309 vp9_init_second_pass_spatial_svc(cpi);
1311 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
1312 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
1313 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
1315 vp9_init_second_pass(cpi);
1319 set_speed_features(cpi);
1324 cpi->rd_thresh_freq_fact[i][j] = 32;
1326 cpi->rd_thresh_freq_sub8x8[i][j] = 32;
1331 cpi->fn_ptr[BT].sdf = SDF; \
1332 cpi->fn_ptr[BT].sdaf = SDAF; \
1333 cpi->fn_ptr[BT].vf = VF; \
1334 cpi->fn_ptr[BT].svf = SVF; \
1335 cpi->fn_ptr[BT].svaf = SVAF; \
1336 cpi->fn_ptr[BT].svf_halfpix_h = SVFHH; \
1337 cpi->fn_ptr[BT].svf_halfpix_v = SVFHV; \
1338 cpi->fn_ptr[BT].svf_halfpix_hv = SVFHHV; \
1339 cpi->fn_ptr[BT].sdx3f = SDX3F; \
1340 cpi->fn_ptr[BT].sdx8f = SDX8F; \
1341 cpi->fn_ptr[BT].sdx4df = SDX4DF;
1420 cpi->full_search_sad = vp9_full_search_sad;
1421 cpi->diamond_search_sad = vp9_diamond_search_sad;
1422 cpi->refining_search_sad = vp9_refining_search_sad;
1429 vp9_init_quantizer(cpi);
1435 vp9_zero(cpi->common.counts.uv_mode);
1438 vp9_zero(cpi->mode_test_hits);
1441 return cpi;
1444 void vp9_remove_compressor(VP9_COMP *cpi) {
1447 if (!cpi)
1450 if (cpi && (cpi->common.current_video_frame > 0)) {
1455 // printf("\n8x8-4x4:%d-%d\n", cpi->t8x8_count, cpi->t4x4_count);
1456 if (cpi->pass != 1) {
1458 double time_encoded = (cpi->last_end_time_stamp_seen
1459 - cpi->first_time_stamp_ever) / 10000000.000;
1460 double total_encode_time = (cpi->time_receive_data +
1461 cpi->time_compress_data) / 1000.000;
1462 double dr = (double)cpi->bytes * (double) 8 / (double)1000
1465 if (cpi->b_calculate_psnr) {
1467 vpx_sse_to_psnr((double)cpi->total_samples, 255.0,
1468 (double)cpi->total_sq_error);
1470 vpx_sse_to_psnr((double)cpi->totalp_samples, 255.0,
1471 (double)cpi->totalp_sq_error);
1472 const double total_ssim = 100 * pow(cpi->summed_quality /
1473 cpi->summed_weights, 8.0);
1474 const double totalp_ssim = 100 * pow(cpi->summedp_quality /
1475 cpi->summedp_weights, 8.0);
1480 dr, cpi->total / cpi->count, total_psnr,
1481 cpi->totalp / cpi->count, totalp_psnr, total_ssim, totalp_ssim,
1485 if (cpi->b_calculate_ssimg) {
1488 cpi->total_ssimg_y / cpi->count,
1489 cpi->total_ssimg_u / cpi->count,
1490 cpi->total_ssimg_v / cpi->count,
1491 cpi->total_ssimg_all / cpi->count, total_encode_time);
1500 if (cpi->pass != 1) {
1511 norm_counts[i] = (double)cpi->mode_test_hits[i] /
1516 sb64_per_frame = ((cpi->common.height + 63) / 64) *
1517 ((cpi->common.width + 63) / 64);
1520 (double)(cpi->common.current_video_frame * sb64_per_frame);
1529 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
1531 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
1532 cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
1533 cpi->time_compress_data / 1000,
1534 (cpi->time_receive_data + cpi->time_compress_data) / 1000);
1539 free_pick_mode_context(&cpi->mb);
1540 dealloc_compressor_data(cpi);
1541 vpx_free(cpi->mb.ss);
1542 vpx_free(cpi->tok);
1544 for (i = 0; i < sizeof(cpi->mbgraph_stats) /
1545 sizeof(cpi->mbgraph_stats[0]); ++i) {
1546 vpx_free(cpi->mbgraph_stats[i].mb_stats);
1549 vp9_remove_common(&cpi->common);
1550 vpx_free(cpi);
1652 static void generate_psnr_packet(VP9_COMP *cpi) {
1656 calc_psnr(cpi->Source, cpi->common.frame_to_show, &psnr);
1663 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
1666 int vp9_use_as_reference(VP9_COMP *cpi, int ref_frame_flags) {
1670 cpi->ref_frame_flags = ref_frame_flags;
1674 void vp9_update_reference(VP9_COMP *cpi, int ref_frame_flags) {
1675 cpi->ext_refresh_golden_frame = (ref_frame_flags & VP9_GOLD_FLAG) != 0;
1676 cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & VP9_ALT_FLAG) != 0;
1677 cpi->ext_refresh_last_frame = (ref_frame_flags & VP9_LAST_FLAG) != 0;
1678 cpi->ext_refresh_frame_flags_pending = 1;
1681 static YV12_BUFFER_CONFIG *get_vp9_ref_frame_buffer(VP9_COMP *cpi,
1691 return ref_frame == NONE ? NULL : get_ref_frame_buffer(cpi, ref_frame);
1694 int vp9_copy_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
1696 YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
1705 int vp9_get_reference_enc(VP9_COMP *cpi, int index, YV12_BUFFER_CONFIG **fb) {
1706 VP9_COMMON *cm = &cpi->common;
1715 int vp9_set_reference_enc(VP9_COMP *cpi, VP9_REFFRAME ref_frame_flag,
1717 YV12_BUFFER_CONFIG *cfg = get_vp9_ref_frame_buffer(cpi, ref_frame_flag);
1726 int vp9_update_entropy(VP9_COMP * cpi, int update) {
1727 cpi->ext_refresh_frame_context = update;
1728 cpi->ext_refresh_frame_context_pending = 1;
1931 static int recode_loop_test(const VP9_COMP *cpi,
1934 const VP9_COMMON *const cm = &cpi->common;
1935 const RATE_CONTROL *const rc = &cpi->rc;
1945 } else if ((cpi->sf.recode_loop == ALLOW_RECODE) ||
1946 ((cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF) &&
1948 cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))) {
1953 } else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
1956 if (q > cpi->cq_target_quality &&
1965 void vp9_update_reference_frames(VP9_COMP *cpi) {
1966 VP9_COMMON * const cm = &cpi->common;
1972 &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
1974 &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
1977 else if (!cpi->multi_arf_enabled && cpi->refresh_golden_frame &&
1978 !cpi->refresh_alt_ref_frame) {
1980 else if (cpi->refresh_golden_frame && !cpi->refresh_alt_ref_frame &&
1981 !cpi->use_svc) {
1995 &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
1997 tmp = cpi->alt_fb_idx;
1998 cpi->alt_fb_idx = cpi->gld_fb_idx;
1999 cpi->gld_fb_idx = tmp;
2001 if (cpi->refresh_alt_ref_frame) {
2002 int arf_idx = cpi->alt_fb_idx;
2004 if (cpi->multi_arf_enabled) {
2005 arf_idx = cpi->arf_buffer_idx[cpi->sequence_number + 1];
2012 if (cpi->refresh_golden_frame) {
2014 &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
2018 if (cpi->refresh_last_frame) {
2020 &cm->ref_frame_map[cpi->lst_fb_idx], cm->new_fb_idx);
2024 static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) {
2025 MACROBLOCKD *xd = &cpi->mb.e_mbd;
2036 vp9_pick_filter_level(cpi->Source, cpi, cpi->sf.lpf_pick);
2039 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
2049 void vp9_scale_references(VP9_COMP *cpi) {
2050 VP9_COMMON *cm = &cpi->common;
2054 const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)];
2065 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
2067 cpi->scaled_ref_idx[ref_frame - 1] = idx;
2073 static void release_scaled_references(VP9_COMP *cpi) {
2074 VP9_COMMON *cm = &cpi->common;
2078 cm->frame_bufs[cpi->scaled_ref_idx[i]].ref_count--;
2104 static void output_frame_level_debug_stats(VP9_COMP *cpi) {
2105 VP9_COMMON *const cm = &cpi->common;
2111 recon_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
2113 if (cpi->twopass.total_left_stats.coded_error != 0.0)
2120 cpi->common.current_video_frame, cpi->rc.this_frame_target,
2121 cpi->rc.projected_frame_size,
2122 cpi->rc.projected_frame_size / cpi->common.MBs,
2123 (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
2124 cpi->rc.total_target_vs_actual,
2125 (cpi->oxcf.starting_buffer_level - cpi->rc.bits_off_target),
2126 cpi->rc.total_actual_bits, cm->base_qindex,
2129 cpi->rc.avg_q,
2130 vp9_convert_qindex_to_q(cpi->rc.ni_av_qi),
2131 vp9_convert_qindex_to_q(cpi->cq_target_quality),
2132 cpi->refresh_last_frame, cpi->refresh_golden_frame,
2133 cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
2134 cpi->twopass.bits_left,
2135 cpi->twopass.total_left_stats.coded_error,
2136 cpi->twopass.bits_left /
2137 (1 + cpi->twopass.total_left_stats.coded_error),
2138 cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
2139 cpi->twopass.kf_zeromotion_pct);
2147 fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
2148 cm->frame_type, cpi->refresh_golden_frame,
2149 cpi->refresh_alt_ref_frame);
2152 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
2161 static void encode_without_recode_loop(VP9_COMP *cpi,
2165 VP9_COMMON *const cm = &cpi->common;
2175 setup_key_frame(cpi);
2177 if (!cm->intra_only && !cm->error_resilient_mode && !cpi->use_svc)
2178 cm->frame_context_idx = cpi->refresh_alt_ref_frame;
2184 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
2185 vp9_vaq_frame_setup(cpi);
2186 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
2187 vp9_setup_in_frame_q_adj(cpi);
2188 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
2189 vp9_cyclic_refresh_setup(cpi);
2192 vp9_encode_frame(cpi);
2196 // update_base_skip_probs(cpi);
2200 static void encode_with_recode_loop(VP9_COMP *cpi,
2206 VP9_COMMON *const cm = &cpi->common;
2207 RATE_CONTROL *const rc = &cpi->rc;
2217 vp9_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
2233 setup_key_frame(cpi);
2235 if (!cm->intra_only && !cm->error_resilient_mode && !cpi->use_svc)
2236 cpi->common.frame_context_idx = cpi->refresh_alt_ref_frame;
2244 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
2245 vp9_vaq_frame_setup(cpi);
2246 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
2247 vp9_setup_in_frame_q_adj(cpi);
2251 vp9_encode_frame(cpi);
2255 // update_base_skip_probs(cpi);
2262 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
2263 save_coding_context(cpi);
2264 cpi->dummy_packing = 1;
2265 if (!cpi->sf.use_nonrd_pick_mode)
2266 vp9_pack_bitstream(cpi, dest, size);
2269 restore_coding_context(cpi);
2275 if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) {
2282 int kf_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
2284 int high_err_target = cpi->ambient_err;
2285 int low_err_target = cpi->ambient_err >> 1;
2318 cpi, frame_over_shoot_limit, frame_under_shoot_limit,
2339 vp9_rc_update_rate_correction_factors(cpi, 1);
2344 vp9_rc_update_rate_correction_factors(cpi, 0);
2346 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
2350 vp9_rc_update_rate_correction_factors(cpi, 0);
2351 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
2363 vp9_rc_update_rate_correction_factors(cpi, 1);
2366 vp9_rc_update_rate_correction_factors(cpi, 0);
2367 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
2373 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY &&
2379 vp9_rc_update_rate_correction_factors(cpi, 0);
2380 q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
2407 cpi->tot_recode_hits++;
2413 static void get_ref_frame_flags(VP9_COMP *cpi) {
2414 if (cpi->refresh_last_frame & cpi->refresh_golden_frame)
2415 cpi->gold_is_last = 1;
2416 else if (cpi->refresh_last_frame ^ cpi->refresh_golden_frame)
2417 cpi->gold_is_last = 0;
2419 if (cpi->refresh_last_frame & cpi->refresh_alt_ref_frame)
2420 cpi->alt_is_last = 1;
2421 else if (cpi->refresh_last_frame ^ cpi->refresh_alt_ref_frame)
2422 cpi->alt_is_last = 0;
2424 if (cpi->refresh_alt_ref_frame & cpi->refresh_golden_frame)
2425 cpi->gold_is_alt = 1;
2426 else if (cpi->refresh_alt_ref_frame ^ cpi->refresh_golden_frame)
2427 cpi->gold_is_alt = 0;
2429 cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
2431 if (cpi->gold_is_last)
2432 cpi->ref_frame_flags &= ~VP9_GOLD_FLAG;
2434 if (cpi->rc.frames_till_gf_update_due == INT_MAX)
2435 cpi->ref_frame_flags &= ~VP9_GOLD_FLAG;
2437 if (cpi->alt_is_last)
2438 cpi->ref_frame_flags &= ~VP9_ALT_FLAG;
2440 if (cpi->gold_is_alt)
2441 cpi->ref_frame_flags &= ~VP9_ALT_FLAG;
2444 static void set_ext_overrides(VP9_COMP *cpi) {
2449 if (cpi->ext_refresh_frame_context_pending) {
2450 cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
2451 cpi->ext_refresh_frame_context_pending = 0;
2453 if (cpi->ext_refresh_frame_flags_pending) {
2454 cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
2455 cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
2456 cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
2457 cpi->ext_refresh_frame_flags_pending = 0;
2461 static void encode_frame_to_data_rate(VP9_COMP *cpi,
2465 VP9_COMMON *const cm = &cpi->common;
2471 const SPEED_FEATURES *const sf = &cpi->sf;
2475 set_ext_overrides(cpi);
2478 if (cm->mi_cols * MI_SIZE != cpi->un_scaled_source->y_width ||
2479 cm->mi_rows * MI_SIZE != cpi->un_scaled_source->y_height) {
2480 scale_and_extend_frame_nonnormative(cpi->un_scaled_source,
2481 &cpi->scaled_source);
2482 cpi->Source = &cpi->scaled_source;
2484 cpi->Source = cpi->un_scaled_source;
2488 if (cpi->unscaled_last_source != NULL) {
2489 if (cm->mi_cols * MI_SIZE != cpi->unscaled_last_source->y_width ||
2490 cm->mi_rows * MI_SIZE != cpi->unscaled_last_source->y_height) {
2491 scale_and_extend_frame_nonnormative(cpi->unscaled_last_source,
2492 &cpi->scaled_last_source);
2493 cpi->Last_Source = &cpi->scaled_last_source;
2495 cpi->Last_Source = cpi->unscaled_last_source;
2499 vp9_scale_references(cpi);
2506 cpi->zbin_mode_boost = 0;
2507 cpi->zbin_mode_boost_enabled = 0;
2510 cm->ref_frame_sign_bias[ALTREF_FRAME] = cpi->rc.source_alt_ref_active;
2515 // Initialize cpi->mv_step_param to default based on max resolution.
2516 cpi->mv_step_param = vp9_init_search_range(cpi, max_mv_def);
2517 // Initialize cpi->max_mv_magnitude and cpi->mv_step_param if appropriate.
2522 cpi->max_mv_magnitude = max_mv_def;
2528 cpi->mv_step_param = vp9_init_search_range(cpi, MIN(max_mv_def, 2 *
2529 cpi->max_mv_magnitude));
2530 cpi->max_mv_magnitude = 0;
2536 setup_key_frame(cpi);
2547 cpi->rc.source_alt_ref_active = 0;
2549 cm->error_resilient_mode = (cpi->oxcf.error_resilient_mode != 0);
2551 (cpi->oxcf.frame_parallel_decoding_mode != 0);
2570 if (cpi->pass == 2 && cpi->sf.static_segmentation)
2571 configure_static_seg_features(cpi);
2575 if (cpi->pass == 0 &&
2576 cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER &&
2578 if (vp9_rc_drop_frame(cpi)) {
2579 vp9_rc_postencode_update_drop_frame(cpi);
2587 vp9_zero(cpi->rd_tx_select_threshes);
2590 if (cpi->oxcf.noise_sensitivity > 0) {
2592 switch (cpi->oxcf.noise_sensitivity) {
2610 vp9_denoise(cpi->Source, cpi->Source, l);
2615 vp9_write_yuv_frame(cpi->Source);
2618 set_speed_features(cpi);
2621 q = vp9_rc_pick_q_and_bounds(cpi, &bottom_index, &top_index);
2626 set_high_precision_mv(cpi, q < HIGH_PRECISION_MV_QTHRESH);
2629 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
2630 encode_without_recode_loop(cpi, size, dest, q);
2632 encode_with_recode_loop(cpi, size, dest, q, bottom_index, top_index);
2638 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
2639 cpi->ambient_err = vp9_get_y_sse(cpi->Source, get_frame_new_buffer(cm));
2644 cpi->refresh_last_frame = 1;
2658 loopfilter_frame(cpi, cm);
2670 cpi->dummy_packing = 0;
2671 vp9_pack_bitstream(cpi, dest, size);
2674 update_reference_segmentation_map(cpi);
2676 release_scaled_references(cpi);
2677 vp9_update_reference_frames(cpi);
2680 full_to_model_counts(cm->counts.coef[t], cpi->coef_counts[t]);
2693 output_frame_level_debug_stats(cpi);
2695 if (cpi->refresh_golden_frame == 1)
2700 if (cpi->refresh_alt_ref_frame == 1)
2705 get_ref_frame_flags(cpi);
2707 vp9_rc_postencode_update(cpi, *size);
2715 if (cpi->multi_arf_enabled) {
2716 cpi->sequence_number = 0;
2717 cpi->frame_coding_order_period = cpi->new_frame_coding_order_period;
2718 cpi->new_frame_coding_order_period = -1;
2726 if (cpi->multi_arf_enabled) {
2727 ++cpi->sequence_number;
2728 if (cpi->sequence_number >= cpi->frame_coding_order_period) {
2729 cpi->sequence_number = 0;
2730 cpi->frame_coding_order_period = cpi->new_frame_coding_order_period;
2731 cpi->new_frame_coding_order_period = -1;
2733 cpi->this_frame_weight = cpi->arf_weight[cpi->sequence_number];
2734 assert(cpi->this_frame_weight >= 0);
2759 if (cpi->use_svc)
2760 vp9_inc_frame_in_layer(&cpi->svc);
2768 static void SvcEncode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
2770 vp9_rc_get_svc_params(cpi);
2771 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
2774 static void Pass0Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
2776 if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
2777 vp9_rc_get_one_pass_cbr_params(cpi);
2779 vp9_rc_get_one_pass_vbr_params(cpi);
2781 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
2784 static void Pass1Encode(VP9_COMP *cpi, size_t *size, uint8_t *dest,
2790 vp9_rc_get_first_pass_params(cpi);
2791 vp9_set_quantizer(&cpi->common, find_fp_qindex());
2792 vp9_first_pass(cpi);
2795 static void Pass2Encode(VP9_COMP *cpi, size_t *size,
2797 cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
2799 vp9_rc_get_second_pass_params(cpi);
2800 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
2802 vp9_twopass_postencode_update(cpi);
2805 static void check_initial_width(VP9_COMP *cpi, int subsampling_x,
2807 VP9_COMMON *const cm = &cpi->common;
2809 if (!cpi->initial_width) {
2812 alloc_raw_frame_buffers(cpi);
2813 cpi->initial_width = cm->width;
2814 cpi->initial_height = cm->height;
2819 int vp9_receive_raw_frame(VP9_COMP *cpi, unsigned int frame_flags,
2822 VP9_COMMON *cm = &cpi->common;
2828 check_initial_width(cpi, subsampling_x, subsampling_y);
2830 if (vp9_lookahead_push(cpi->lookahead,
2834 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
2846 static int frame_is_reference(const VP9_COMP *cpi) {
2847 const VP9_COMMON *cm = &cpi->common;
2850 cpi->refresh_last_frame ||
2851 cpi->refresh_golden_frame ||
2852 cpi->refresh_alt_ref_frame ||
2860 int is_next_frame_arf(VP9_COMP *cpi) {
2862 return cpi->frame_coding_order[cpi->sequence_number + 1] < 0 ? 1 : 0;
2866 void adjust_frame_rate(VP9_COMP *cpi) {
2870 if (cpi->source->ts_start == cpi->first_time_stamp_ever) {
2871 this_duration = cpi->source->ts_end - cpi->source->ts_start;
2874 int64_t last_duration = cpi->last_end_time_stamp_seen
2875 - cpi->last_time_stamp_seen;
2877 this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
2886 vp9_new_framerate(cpi, 10000000.0 / this_duration);
2891 const double interval = MIN((double)(cpi->source->ts_end
2892 - cpi->first_time_stamp_ever), 10000000.0);
2893 double avg_duration = 10000000.0 / cpi->oxcf.framerate;
2897 vp9_new_framerate(cpi, 10000000.0 / avg_duration);
2900 cpi->last_time_stamp_seen = cpi->source->ts_start;
2901 cpi->last_end_time_stamp_seen = cpi->source->ts_end;
2904 int vp9_get_compressed_data(VP9_COMP *cpi, unsigned int *frame_flags,
2907 VP9_COMMON *const cm = &cpi->common;
2908 MACROBLOCKD *const xd = &cpi->mb.e_mbd;
2909 RATE_CONTROL *const rc = &cpi->rc;
2914 if (!cpi)
2917 if (cpi->svc.number_spatial_layers > 1 && cpi->pass == 2) {
2918 vp9_restore_layer_context(cpi);
2923 cpi->source = NULL;
2924 cpi->last_source = NULL;
2926 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
2931 cpi->refresh_last_frame = 1;
2932 cpi->refresh_golden_frame = 0;
2933 cpi->refresh_alt_ref_frame = 0;
2936 if (cpi->oxcf.play_alternate && rc->source_alt_ref_pending) {
2940 assert(!cpi->multi_arf_enabled ||
2941 cpi->frame_coding_order[cpi->sequence_number] < 0);
2943 if (cpi->multi_arf_enabled && (cpi->pass == 2))
2944 frames_to_arf = (-cpi->frame_coding_order[cpi->sequence_number])
2945 - cpi->next_frame_in_order;
2952 if ((cpi->source = vp9_lookahead_peek(cpi->lookahead, frames_to_arf))) {
2954 cpi->alt_ref_source[cpi->arf_buffered] = cpi->source;
2956 cpi->alt_ref_source = cpi->source;
2959 if (cpi->oxcf.arnr_max_frames > 0) {
2962 vp9_configure_arnr_filter(cpi, frames_to_arf, rc->gfu_boost);
2963 vp9_temporal_filter_prepare(cpi, frames_to_arf);
2964 vp9_extend_frame_borders(&cpi->alt_ref_buffer);
2965 force_src_buffer = &cpi->alt_ref_buffer;
2969 cpi->refresh_alt_ref_frame = 1;
2970 cpi->refresh_golden_frame = 0;
2971 cpi->refresh_last_frame = 0;
2975 if (!cpi->multi_arf_enabled)
2983 if (!cpi->source) {
2990 if ((cpi->last_source = vp9_lookahead_peek(cpi->lookahead, -1)) == NULL)
2994 if ((cpi->source = vp9_lookahead_pop(cpi->lookahead, flush))) {
3001 for (i = 0; i < cpi->arf_buffered; ++i) {
3002 if (cpi->source == cpi->alt_ref_source[i]) {
3004 cpi->refresh_golden_frame = 1;
3009 rc->is_src_frame_alt_ref = cpi->alt_ref_source &&
3010 (cpi->source == cpi->alt_ref_source);
3015 cpi->alt_ref_source[i] = NULL;
3017 cpi->alt_ref_source = NULL;
3021 cpi->refresh_last_frame = 0;
3024 ++cpi->next_frame_in_order;
3029 if (cpi->source) {
3030 cpi->un_scaled_source = cpi->Source = force_src_buffer ? force_src_buffer
3031 : &cpi->source->img;
3033 if (cpi->last_source != NULL) {
3034 cpi->unscaled_last_source = &cpi->last_source->img;
3036 cpi->unscaled_last_source = NULL;
3039 *time_stamp = cpi->source->ts_start;
3040 *time_end = cpi->source->ts_end;
3041 *frame_flags = cpi->source->flags;
3044 if (cm->frame_type != KEY_FRAME && cpi->pass == 2)
3045 rc->source_alt_ref_pending = is_next_frame_arf(cpi);
3049 if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done) {
3050 vp9_end_first_pass(cpi); /* get last stats packet */
3051 cpi->twopass.first_pass_done = 1;
3056 if (cpi->source->ts_start < cpi->first_time_stamp_ever) {
3057 cpi->first_time_stamp_ever = cpi->source->ts_start;
3058 cpi->last_end_time_stamp_seen = cpi->source->ts_start;
3063 adjust_frame_rate(cpi);
3066 if (cpi->svc.number_temporal_layers > 1 &&
3067 cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
3068 vp9_update_temporal_layer_framerate(cpi);
3069 vp9_restore_layer_context(cpi);
3086 if (cpi->refresh_alt_ref_frame) {
3087 ++cpi->arf_buffered;
3089 if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) &&
3090 (cpi->pass == 2)) {
3091 cpi->alt_fb_idx = cpi->arf_buffer_idx[cpi->sequence_number];
3104 const int idx = cm->ref_frame_map[get_ref_frame_idx(cpi, ref_frame)];
3119 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
3123 if (cpi->pass == 1 &&
3124 (!cpi->use_svc || cpi->svc.number_temporal_layers == 1)) {
3125 Pass1Encode(cpi, size, dest, frame_flags);
3126 } else if (cpi->pass == 2 &&
3127 (!cpi->use_svc || cpi->svc.number_temporal_layers == 1)) {
3128 Pass2Encode(cpi, size, dest, frame_flags);
3129 } else if (cpi->use_svc) {
3130 SvcEncode(cpi, size, dest, frame_flags);
3133 Pass0Encode(cpi, size, dest, frame_flags);
3141 release_scaled_references(cpi);
3145 cpi->droppable = !frame_is_reference(cpi);
3149 if ((cpi->svc.number_temporal_layers > 1 &&
3150 cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) ||
3151 (cpi->svc.number_spatial_layers > 1 && cpi->pass == 2)) {
3152 vp9_save_layer_context(cpi);
3156 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
3158 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame)
3159 generate_psnr_packet(cpi);
3163 if (cpi->pass != 1) {
3164 cpi->bytes += (int)(*size);
3167 cpi->count++;
3169 if (cpi->b_calculate_psnr) {
3170 YV12_BUFFER_CONFIG *orig = cpi->Source;
3171 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
3176 cpi->total += psnr.psnr[0];
3177 cpi->total_y += psnr.psnr[1];
3178 cpi->total_u += psnr.psnr[2];
3179 cpi->total_v += psnr.psnr[3];
3180 cpi->total_sq_error += psnr.sse[0];
3181 cpi->total_samples += psnr.samples[0];
3194 cpi->totalp += psnr2.psnr[0];
3195 cpi->totalp_y += psnr2.psnr[1];
3196 cpi->totalp_u += psnr2.psnr[2];
3197 cpi->totalp_v += psnr2.psnr[3];
3198 cpi->totalp_sq_error += psnr2.sse[0];
3199 cpi->totalp_samples += psnr2.samples[0];
3203 cpi->summed_quality += frame_ssim2 * weight;
3204 cpi->summed_weights += weight;
3208 cpi->summedp_quality += frame_ssim2 * weight;
3209 cpi->summedp_weights += weight;
3214 cpi->common.current_video_frame, y2, u2, v2,
3222 if (cpi->b_calculate_ssimg) {
3224 frame_all = vp9_calc_ssimg(cpi->Source, cm->frame_to_show, &y, &u, &v);
3225 cpi->total_ssimg_y += y;
3226 cpi->total_ssimg_u += u;
3227 cpi->total_ssimg_v += v;
3228 cpi->total_ssimg_all += frame_all;
3237 int vp9_get_preview_raw_frame(VP9_COMP *cpi, YV12_BUFFER_CONFIG *dest,
3239 VP9_COMMON *cm = &cpi->common;
3266 int vp9_set_roimap(VP9_COMP *cpi, unsigned char *map, unsigned int rows,
3271 struct segmentation *seg = &cpi->common.seg;
3274 if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols)
3283 vp9_set_segmentation_map(cpi, map);
3292 cpi->segment_encode_breakout[i] = threshold[i];
3315 int vp9_set_active_map(VP9_COMP *cpi, unsigned char *map,
3317 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
3319 vpx_memcpy(cpi->active_map, map, rows * cols);
3320 cpi->active_map_enabled = 1;
3322 cpi->active_map_enabled = 0;
3327 // cpi->active_map_enabled = 0;
3332 int vp9_set_internal_size(VP9_COMP *cpi,
3334 VP9_COMMON *cm = &cpi->common;
3344 cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
3345 cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
3347 assert(cm->width <= cpi->initial_width);
3348 assert(cm->height <= cpi->initial_height);
3349 update_frame_size(cpi);
3353 int vp9_set_size_literal(VP9_COMP *cpi, unsigned int width,
3355 VP9_COMMON *cm = &cpi->common;
3357 check_initial_width(cpi, 1, 1);
3361 if (cm->width * 5 < cpi->initial_width) {
3362 cm->width = cpi->initial_width / 5 + 1;
3365 if (cm->width > cpi->initial_width) {
3366 cm->width = cpi->initial_width;
3373 if (cm->height * 5 < cpi->initial_height) {
3374 cm->height = cpi->initial_height / 5 + 1;
3377 if (cm->height > cpi->initial_height) {
3378 cm->height = cpi->initial_height;
3383 assert(cm->width <= cpi->initial_width);
3384 assert(cm->height <= cpi->initial_height);
3385 update_frame_size(cpi);
3389 void vp9_set_svc(VP9_COMP *cpi, int use_svc) {
3390 cpi->use_svc = use_svc;
3403 int vp9_get_quantizer(VP9_COMP *cpi) {
3404 return cpi->common.base_qindex;