2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
12 #include "vpx_config.h"
13 #include "vp9/common/vp9_filter.h"
14 #include "vp9/common/vp9_onyxc_int.h"
15 #include "vp9/common/vp9_reconinter.h"
16 #include "vp9/encoder/vp9_onyx_int.h"
17 #include "vp9/common/vp9_systemdependent.h"
18 #include "vp9/encoder/vp9_quantize.h"
19 #include "vp9/common/vp9_alloccommon.h"
20 #include "vp9/encoder/vp9_mcomp.h"
21 #include "vp9/encoder/vp9_firstpass.h"
22 #include "vp9/encoder/vp9_psnr.h"
23 #include "vpx_scale/vpx_scale.h"
24 #include "vp9/common/vp9_extend.h"
25 #include "vp9/encoder/vp9_ratectrl.h"
26 #include "vp9/common/vp9_quant_common.h"
27 #include "vp9/common/vp9_tile_common.h"
28 #include "vp9/encoder/vp9_segmentation.h"
29 #include "./vp9_rtcd.h"
30 #include "./vpx_scale_rtcd.h"
32 #include "vp9/common/vp9_postproc.h"
34 #include "vpx_mem/vpx_mem.h"
35 #include "vpx_ports/vpx_timer.h"
37 #include "vp9/common/vp9_seg_common.h"
38 #include "vp9/encoder/vp9_mbgraph.h"
39 #include "vp9/common/vp9_pred_common.h"
40 #include "vp9/encoder/vp9_rdopt.h"
41 #include "vp9/encoder/vp9_bitstream.h"
42 #include "vp9/encoder/vp9_picklpf.h"
43 #include "vp9/common/vp9_mvref_common.h"
44 #include "vp9/encoder/vp9_temporal_filter.h"
50 extern void print_tree_update_probs();
52 static void set_default_lf_deltas(VP9_COMP *cpi);
54 #define DEFAULT_INTERP_FILTER SWITCHABLE
56 #define SEARCH_BEST_FILTER 0 /* to search exhaustively for
58 #define RESET_FOREACH_FILTER 0 /* whether to reset the encoder state
59 before trying each new filter */
60 #define SHARP_FILTER_QTHRESH 0 /* Q threshold for 8-tap sharp filter */
62 #define ALTREF_HIGH_PRECISION_MV 1 /* whether to use high precision mv
63 for altref computation */
64 #define HIGH_PRECISION_MV_QTHRESH 200 /* Q threshold for use of high precision
65 mv. Choose a very high value for
66 now so that HIGH_PRECISION is always
69 #if CONFIG_INTERNAL_STATS
72 extern double vp9_calc_ssim(YV12_BUFFER_CONFIG *source,
73 YV12_BUFFER_CONFIG *dest, int lumamask,
77 extern double vp9_calc_ssimg(YV12_BUFFER_CONFIG *source,
78 YV12_BUFFER_CONFIG *dest, double *ssim_y,
79 double *ssim_u, double *ssim_v);
84 // #define OUTPUT_YUV_REC
101 extern int intra_mode_stats[VP9_INTRA_MODES]
107 extern void init_nmvstats();
108 extern void print_nmvstats();
111 extern void init_tx_count_stats();
112 extern void write_tx_count_stats();
113 extern void init_switchable_interp_stats();
114 extern void write_switchable_interp_stats();
118 unsigned int frames_at_speed[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
121 #if defined(SECTIONBITS_OUTPUT)
122 extern unsigned __int64 Sectionbits[500];
125 extern void vp9_init_quantizer(VP9_COMP *cpi);
127 // Tables relating active max Q to active min Q
128 static int kf_low_motion_minq[QINDEX_RANGE];
129 static int kf_high_motion_minq[QINDEX_RANGE];
130 static int gf_low_motion_minq[QINDEX_RANGE];
131 static int gf_high_motion_minq[QINDEX_RANGE];
132 static int inter_minq[QINDEX_RANGE];
134 static INLINE void Scale2Ratio(int mode, int *hr, int *hs) {
160 // Functions to compute the active minq lookup table entries based on a
161 // formulaic approach to facilitate easier adjustment of the Q tables.
162 // The formulae were derived from computing a 3rd order polynomial best
163 // fit to the original data (after plotting real maxq vs minq (not q index))
164 static int calculate_minq_index(double maxq,
165 double x3, double x2, double x1, double c) {
167 const double minqtarget = MIN(((x3 * maxq + x2) * maxq + x1) * maxq + c,
170 // Special case handling to deal with the step from q2.0
171 // down to lossless mode represented by q 1.0.
172 if (minqtarget <= 2.0)
175 for (i = 0; i < QINDEX_RANGE; i++) {
176 if (minqtarget <= vp9_convert_qindex_to_q(i))
180 return QINDEX_RANGE - 1;
183 static void init_minq_luts(void) {
186 for (i = 0; i < QINDEX_RANGE; i++) {
187 const double maxq = vp9_convert_qindex_to_q(i);
190 kf_low_motion_minq[i] = calculate_minq_index(maxq,
195 kf_high_motion_minq[i] = calculate_minq_index(maxq,
201 gf_low_motion_minq[i] = calculate_minq_index(maxq,
206 gf_high_motion_minq[i] = calculate_minq_index(maxq,
211 inter_minq[i] = calculate_minq_index(maxq,
220 static void set_mvcost(MACROBLOCK *mb) {
221 if (mb->e_mbd.allow_high_precision_mv) {
222 mb->mvcost = mb->nmvcost_hp;
223 mb->mvsadcost = mb->nmvsadcost_hp;
225 mb->mvcost = mb->nmvcost;
226 mb->mvsadcost = mb->nmvsadcost;
230 void vp9_initialize_enc() {
231 static int init_done = 0;
234 vp9_initialize_common();
235 vp9_tokenize_initialize();
236 vp9_init_quant_tables();
239 // init_base_skip_probs();
244 static void setup_features(VP9_COMP *cpi) {
245 MACROBLOCKD *xd = &cpi->mb.e_mbd;
246 struct loopfilter *lf = &xd->lf;
248 // Set up default state for MB feature flags
251 xd->seg.update_map = 0;
252 xd->seg.update_data = 0;
253 vpx_memset(xd->seg.tree_probs, 255, sizeof(xd->seg.tree_probs));
255 vp9_clearall_segfeatures(&xd->seg);
257 lf->mode_ref_delta_enabled = 0;
258 lf->mode_ref_delta_update = 0;
259 vp9_zero(lf->ref_deltas);
260 vp9_zero(lf->mode_deltas);
261 vp9_zero(lf->last_ref_deltas);
262 vp9_zero(lf->last_mode_deltas);
264 set_default_lf_deltas(cpi);
267 static void dealloc_compressor_data(VP9_COMP *cpi) {
268 // Delete sementation map
269 vpx_free(cpi->segmentation_map);
270 cpi->segmentation_map = 0;
271 vpx_free(cpi->common.last_frame_seg_map);
272 cpi->common.last_frame_seg_map = 0;
273 vpx_free(cpi->coding_context.last_frame_seg_map_copy);
274 cpi->coding_context.last_frame_seg_map_copy = 0;
276 vpx_free(cpi->active_map);
279 vp9_free_frame_buffers(&cpi->common);
281 vp9_free_frame_buffer(&cpi->last_frame_uf);
282 vp9_free_frame_buffer(&cpi->scaled_source);
283 vp9_free_frame_buffer(&cpi->alt_ref_buffer);
284 vp9_lookahead_destroy(cpi->lookahead);
289 // Activity mask based per mb zbin adjustments
290 vpx_free(cpi->mb_activity_map);
291 cpi->mb_activity_map = 0;
292 vpx_free(cpi->mb_norm_activity_map);
293 cpi->mb_norm_activity_map = 0;
295 vpx_free(cpi->mb.pip);
299 // Computes a q delta (in "q index" terms) to get from a starting q value
302 static int compute_qdelta(VP9_COMP *cpi, double qstart, double qtarget) {
304 int start_index = cpi->worst_quality;
305 int target_index = cpi->worst_quality;
307 // Convert the average q value to an index.
308 for (i = cpi->best_quality; i < cpi->worst_quality; i++) {
310 if (vp9_convert_qindex_to_q(i) >= qstart)
314 // Convert the q target to an index
315 for (i = cpi->best_quality; i < cpi->worst_quality; i++) {
317 if (vp9_convert_qindex_to_q(i) >= qtarget)
321 return target_index - start_index;
324 static void configure_static_seg_features(VP9_COMP *cpi) {
325 VP9_COMMON *cm = &cpi->common;
326 MACROBLOCKD *xd = &cpi->mb.e_mbd;
328 int high_q = (int)(cpi->avg_q > 48.0);
331 // Disable and clear down for KF
332 if (cm->frame_type == KEY_FRAME) {
333 // Clear down the global segmentation map
334 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
335 xd->seg.update_map = 0;
336 xd->seg.update_data = 0;
337 cpi->static_mb_pct = 0;
339 // Disable segmentation
340 vp9_disable_segmentation((VP9_PTR)cpi);
342 // Clear down the segment features.
343 vp9_clearall_segfeatures(&xd->seg);
344 } else if (cpi->refresh_alt_ref_frame) {
345 // If this is an alt ref frame
346 // Clear down the global segmentation map
347 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
348 xd->seg.update_map = 0;
349 xd->seg.update_data = 0;
350 cpi->static_mb_pct = 0;
352 // Disable segmentation and individual segment features by default
353 vp9_disable_segmentation((VP9_PTR)cpi);
354 vp9_clearall_segfeatures(&xd->seg);
356 // Scan frames from current to arf frame.
357 // This function re-enables segmentation if appropriate.
358 vp9_update_mbgraph_stats(cpi);
360 // If segmentation was enabled set those features needed for the
362 if (xd->seg.enabled) {
363 xd->seg.update_map = 1;
364 xd->seg.update_data = 1;
366 qi_delta = compute_qdelta(cpi, cpi->avg_q, (cpi->avg_q * 0.875));
367 vp9_set_segdata(&xd->seg, 1, SEG_LVL_ALT_Q, (qi_delta - 2));
368 vp9_set_segdata(&xd->seg, 1, SEG_LVL_ALT_LF, -2);
370 vp9_enable_segfeature(&xd->seg, 1, SEG_LVL_ALT_Q);
371 vp9_enable_segfeature(&xd->seg, 1, SEG_LVL_ALT_LF);
373 // Where relevant assume segment data is delta data
374 xd->seg.abs_delta = SEGMENT_DELTADATA;
377 } else if (xd->seg.enabled) {
378 // All other frames if segmentation has been enabled
380 // First normal frame in a valid gf or alt ref group
381 if (cpi->frames_since_golden == 0) {
382 // Set up segment features for normal frames in an arf group
383 if (cpi->source_alt_ref_active) {
384 xd->seg.update_map = 0;
385 xd->seg.update_data = 1;
386 xd->seg.abs_delta = SEGMENT_DELTADATA;
388 qi_delta = compute_qdelta(cpi, cpi->avg_q,
389 (cpi->avg_q * 1.125));
390 vp9_set_segdata(&xd->seg, 1, SEG_LVL_ALT_Q, (qi_delta + 2));
391 vp9_enable_segfeature(&xd->seg, 1, SEG_LVL_ALT_Q);
393 vp9_set_segdata(&xd->seg, 1, SEG_LVL_ALT_LF, -2);
394 vp9_enable_segfeature(&xd->seg, 1, SEG_LVL_ALT_LF);
396 // Segment coding disabled for compred testing
397 if (high_q || (cpi->static_mb_pct == 100)) {
398 vp9_set_segdata(&xd->seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
399 vp9_enable_segfeature(&xd->seg, 1, SEG_LVL_REF_FRAME);
400 vp9_enable_segfeature(&xd->seg, 1, SEG_LVL_SKIP);
403 // Disable segmentation and clear down features if alt ref
404 // is not active for this group
406 vp9_disable_segmentation((VP9_PTR)cpi);
408 vpx_memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
410 xd->seg.update_map = 0;
411 xd->seg.update_data = 0;
413 vp9_clearall_segfeatures(&xd->seg);
415 } else if (cpi->is_src_frame_alt_ref) {
416 // Special case where we are coding over the top of a previous
418 // Segment coding disabled for compred testing
420 // Enable ref frame features for segment 0 as well
421 vp9_enable_segfeature(&xd->seg, 0, SEG_LVL_REF_FRAME);
422 vp9_enable_segfeature(&xd->seg, 1, SEG_LVL_REF_FRAME);
424 // All mbs should use ALTREF_FRAME
425 vp9_clear_segdata(&xd->seg, 0, SEG_LVL_REF_FRAME);
426 vp9_set_segdata(&xd->seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
427 vp9_clear_segdata(&xd->seg, 1, SEG_LVL_REF_FRAME);
428 vp9_set_segdata(&xd->seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
430 // Skip all MBs if high Q (0,0 mv and skip coeffs)
432 vp9_enable_segfeature(&xd->seg, 0, SEG_LVL_SKIP);
433 vp9_enable_segfeature(&xd->seg, 1, SEG_LVL_SKIP);
435 // Enable data udpate
436 xd->seg.update_data = 1;
440 // No updates.. leave things as they are.
441 xd->seg.update_map = 0;
442 xd->seg.update_data = 0;
448 void vp9_update_mode_context_stats(VP9_COMP *cpi) {
449 VP9_COMMON *cm = &cpi->common;
451 unsigned int (*inter_mode_counts)[VP9_INTER_MODES - 1][2] =
452 cm->fc.inter_mode_counts;
453 int64_t (*mv_ref_stats)[VP9_INTER_MODES - 1][2] = cpi->mv_ref_stats;
456 // Read the past stats counters
457 f = fopen("mode_context.bin", "rb");
459 vpx_memset(cpi->mv_ref_stats, 0, sizeof(cpi->mv_ref_stats));
461 fread(cpi->mv_ref_stats, sizeof(cpi->mv_ref_stats), 1, f);
465 // Add in the values for this frame
466 for (i = 0; i < INTER_MODE_CONTEXTS; i++) {
467 for (j = 0; j < VP9_INTER_MODES - 1; j++) {
468 mv_ref_stats[i][j][0] += (int64_t)inter_mode_counts[i][j][0];
469 mv_ref_stats[i][j][1] += (int64_t)inter_mode_counts[i][j][1];
473 // Write back the accumulated stats
474 f = fopen("mode_context.bin", "wb");
475 fwrite(cpi->mv_ref_stats, sizeof(cpi->mv_ref_stats), 1, f);
479 void print_mode_context(VP9_COMP *cpi) {
480 FILE *f = fopen("vp9_modecont.c", "a");
483 fprintf(f, "#include \"vp9_entropy.h\"\n");
486 "const int inter_mode_probs[INTER_MODE_CONTEXTS][VP9_INTER_MODES - 1] =");
488 for (j = 0; j < INTER_MODE_CONTEXTS; j++) {
489 fprintf(f, " {/* %d */ ", j);
491 for (i = 0; i < VP9_INTER_MODES - 1; i++) {
493 int64_t count = cpi->mv_ref_stats[j][i][0] + cpi->mv_ref_stats[j][i][1];
495 this_prob = ((cpi->mv_ref_stats[j][i][0] * 256) + (count >> 1)) / count;
500 fprintf(f, "%5d, ", this_prob);
508 #endif // ENTROPY_STATS
510 // DEBUG: Print out the segment id of each MB in the current frame.
511 static void print_seg_map(VP9_COMP *cpi) {
512 VP9_COMMON *cm = &cpi->common;
515 FILE *statsfile = fopen("segmap.stt", "a");
517 fprintf(statsfile, "%10d\n", cm->current_video_frame);
519 for (row = 0; row < cpi->common.mi_rows; row++) {
520 for (col = 0; col < cpi->common.mi_cols; col++) {
521 fprintf(statsfile, "%10d", cpi->segmentation_map[map_index]);
524 fprintf(statsfile, "\n");
526 fprintf(statsfile, "\n");
531 static void update_reference_segmentation_map(VP9_COMP *cpi) {
532 VP9_COMMON *const cm = &cpi->common;
534 MODE_INFO *mi, *mi_ptr = cm->mi;
535 uint8_t *cache_ptr = cm->last_frame_seg_map, *cache;
537 for (row = 0; row < cm->mi_rows; row++) {
540 for (col = 0; col < cm->mi_cols; col++, mi++, cache++)
541 cache[0] = mi->mbmi.segment_id;
542 mi_ptr += cm->mode_info_stride;
543 cache_ptr += cm->mi_cols;
547 static void set_default_lf_deltas(VP9_COMP *cpi) {
548 struct loopfilter *lf = &cpi->mb.e_mbd.lf;
550 lf->mode_ref_delta_enabled = 1;
551 lf->mode_ref_delta_update = 1;
553 vp9_zero(lf->ref_deltas);
554 vp9_zero(lf->mode_deltas);
556 // Test of ref frame deltas
557 lf->ref_deltas[INTRA_FRAME] = 2;
558 lf->ref_deltas[LAST_FRAME] = 0;
559 lf->ref_deltas[GOLDEN_FRAME] = -2;
560 lf->ref_deltas[ALTREF_FRAME] = -2;
562 lf->mode_deltas[0] = 0; // Zero
563 lf->mode_deltas[1] = 0; // New mv
566 static void set_rd_speed_thresholds(VP9_COMP *cpi, int mode, int speed) {
567 SPEED_FEATURES *sf = &cpi->sf;
568 int speed_multiplier = speed + 1;
571 // Set baseline threshold values
572 for (i = 0; i < MAX_MODES; ++i)
573 sf->thresh_mult[i] = mode == 0 ? -500 : 0;
575 sf->thresh_mult[THR_NEARESTMV] = 0;
576 sf->thresh_mult[THR_NEARESTG] = 0;
577 sf->thresh_mult[THR_NEARESTA] = 0;
579 sf->thresh_mult[THR_NEWMV] += speed_multiplier * 1000;
580 sf->thresh_mult[THR_COMP_NEARESTLA] += speed_multiplier * 1000;
581 sf->thresh_mult[THR_NEARMV] += speed_multiplier * 1000;
582 sf->thresh_mult[THR_COMP_NEARESTGA] += speed_multiplier * 1000;
584 sf->thresh_mult[THR_DC] += speed_multiplier * 1000;
586 sf->thresh_mult[THR_NEWG] += speed_multiplier * 1000;
587 sf->thresh_mult[THR_NEWA] += speed_multiplier * 1000;
588 sf->thresh_mult[THR_NEARA] += speed_multiplier * 1000;
590 sf->thresh_mult[THR_TM] += speed_multiplier * 1000;
592 sf->thresh_mult[THR_COMP_NEARLA] += speed_multiplier * 1500;
593 sf->thresh_mult[THR_COMP_NEWLA] += speed_multiplier * 2000;
594 sf->thresh_mult[THR_NEARG] += speed_multiplier * 1000;
595 sf->thresh_mult[THR_COMP_NEARGA] += speed_multiplier * 1500;
596 sf->thresh_mult[THR_COMP_NEWGA] += speed_multiplier * 2000;
598 sf->thresh_mult[THR_SPLITMV] += speed_multiplier * 2500;
599 sf->thresh_mult[THR_SPLITG] += speed_multiplier * 2500;
600 sf->thresh_mult[THR_SPLITA] += speed_multiplier * 2500;
601 sf->thresh_mult[THR_COMP_SPLITLA] += speed_multiplier * 4500;
602 sf->thresh_mult[THR_COMP_SPLITGA] += speed_multiplier * 4500;
604 sf->thresh_mult[THR_ZEROMV] += speed_multiplier * 2000;
605 sf->thresh_mult[THR_ZEROG] += speed_multiplier * 2000;
606 sf->thresh_mult[THR_ZEROA] += speed_multiplier * 2000;
607 sf->thresh_mult[THR_COMP_ZEROLA] += speed_multiplier * 2500;
608 sf->thresh_mult[THR_COMP_ZEROGA] += speed_multiplier * 2500;
610 sf->thresh_mult[THR_B_PRED] += speed_multiplier * 2500;
611 sf->thresh_mult[THR_H_PRED] += speed_multiplier * 2000;
612 sf->thresh_mult[THR_V_PRED] += speed_multiplier * 2000;
613 sf->thresh_mult[THR_D45_PRED ] += speed_multiplier * 2500;
614 sf->thresh_mult[THR_D135_PRED] += speed_multiplier * 2500;
615 sf->thresh_mult[THR_D117_PRED] += speed_multiplier * 2500;
616 sf->thresh_mult[THR_D153_PRED] += speed_multiplier * 2500;
617 sf->thresh_mult[THR_D27_PRED] += speed_multiplier * 2500;
618 sf->thresh_mult[THR_D63_PRED] += speed_multiplier * 2500;
620 if (cpi->sf.skip_lots_of_modes) {
621 for (i = 0; i < MAX_MODES; ++i)
622 sf->thresh_mult[i] = INT_MAX;
624 sf->thresh_mult[THR_DC] = 2000;
625 sf->thresh_mult[THR_TM] = 2000;
626 sf->thresh_mult[THR_NEWMV] = 4000;
627 sf->thresh_mult[THR_NEWG] = 4000;
628 sf->thresh_mult[THR_NEWA] = 4000;
629 sf->thresh_mult[THR_NEARESTMV] = 0;
630 sf->thresh_mult[THR_NEARESTG] = 0;
631 sf->thresh_mult[THR_NEARESTA] = 0;
632 sf->thresh_mult[THR_NEARMV] = 2000;
633 sf->thresh_mult[THR_NEARG] = 2000;
634 sf->thresh_mult[THR_NEARA] = 2000;
635 sf->thresh_mult[THR_COMP_NEARESTLA] = 2000;
636 sf->thresh_mult[THR_SPLITMV] = 2500;
637 sf->thresh_mult[THR_SPLITG] = 2500;
638 sf->thresh_mult[THR_SPLITA] = 2500;
642 /* disable frame modes if flags not set */
643 if (!(cpi->ref_frame_flags & VP9_LAST_FLAG)) {
644 sf->thresh_mult[THR_NEWMV ] = INT_MAX;
645 sf->thresh_mult[THR_NEARESTMV] = INT_MAX;
646 sf->thresh_mult[THR_ZEROMV ] = INT_MAX;
647 sf->thresh_mult[THR_NEARMV ] = INT_MAX;
648 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
650 if (!(cpi->ref_frame_flags & VP9_GOLD_FLAG)) {
651 sf->thresh_mult[THR_NEARESTG ] = INT_MAX;
652 sf->thresh_mult[THR_ZEROG ] = INT_MAX;
653 sf->thresh_mult[THR_NEARG ] = INT_MAX;
654 sf->thresh_mult[THR_NEWG ] = INT_MAX;
655 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
657 if (!(cpi->ref_frame_flags & VP9_ALT_FLAG)) {
658 sf->thresh_mult[THR_NEARESTA ] = INT_MAX;
659 sf->thresh_mult[THR_ZEROA ] = INT_MAX;
660 sf->thresh_mult[THR_NEARA ] = INT_MAX;
661 sf->thresh_mult[THR_NEWA ] = INT_MAX;
662 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
665 if ((cpi->ref_frame_flags & (VP9_LAST_FLAG | VP9_ALT_FLAG)) !=
666 (VP9_LAST_FLAG | VP9_ALT_FLAG)) {
667 sf->thresh_mult[THR_COMP_ZEROLA ] = INT_MAX;
668 sf->thresh_mult[THR_COMP_NEARESTLA] = INT_MAX;
669 sf->thresh_mult[THR_COMP_NEARLA ] = INT_MAX;
670 sf->thresh_mult[THR_COMP_NEWLA ] = INT_MAX;
671 sf->thresh_mult[THR_COMP_SPLITLA ] = INT_MAX;
673 if ((cpi->ref_frame_flags & (VP9_GOLD_FLAG | VP9_ALT_FLAG)) !=
674 (VP9_GOLD_FLAG | VP9_ALT_FLAG)) {
675 sf->thresh_mult[THR_COMP_ZEROGA ] = INT_MAX;
676 sf->thresh_mult[THR_COMP_NEARESTGA] = INT_MAX;
677 sf->thresh_mult[THR_COMP_NEARGA ] = INT_MAX;
678 sf->thresh_mult[THR_COMP_NEWGA ] = INT_MAX;
679 sf->thresh_mult[THR_COMP_SPLITGA ] = INT_MAX;
682 if (sf->disable_splitmv == 1) {
683 sf->thresh_mult[THR_SPLITMV ] = INT_MAX;
684 sf->thresh_mult[THR_SPLITG ] = INT_MAX;
685 sf->thresh_mult[THR_SPLITA ] = INT_MAX;
687 sf->thresh_mult[THR_COMP_SPLITLA ] = INT_MAX;
688 sf->thresh_mult[THR_COMP_SPLITGA ] = INT_MAX;
692 void vp9_set_speed_features(VP9_COMP *cpi) {
693 SPEED_FEATURES *sf = &cpi->sf;
694 int mode = cpi->compressor_speed;
695 int speed = cpi->speed;
698 // Only modes 0 and 1 supported for now in experimental code basae
702 // Initialise default mode frequency sampling variables
703 for (i = 0; i < MAX_MODES; i ++) {
704 cpi->mode_check_freq[i] = 0;
705 cpi->mode_test_hit_counts[i] = 0;
706 cpi->mode_chosen_counts[i] = 0;
709 // best quality defaults
711 sf->search_method = NSTEP;
714 sf->quarter_pixel_search = 1;
715 sf->half_pixel_search = 1;
716 sf->iterative_sub_pixel = 1;
717 sf->optimize_coefficients = !cpi->oxcf.lossless;
718 sf->reduce_first_step_size = 0;
719 sf->auto_mv_step_size = 0;
720 sf->max_step_search_steps = MAX_MVSEARCH_STEPS;
721 sf->comp_inter_joint_search_thresh = BLOCK_SIZE_AB4X4;
722 sf->adaptive_rd_thresh = 0;
723 sf->use_lastframe_partitioning = 0;
724 sf->tx_size_search_method = USE_FULL_RD;
725 sf->use_8tap_always = 0;
726 sf->use_avoid_tested_higherror = 0;
727 sf->reference_masking = 0;
728 sf->skip_lots_of_modes = 0;
729 sf->adjust_thresholds_by_speed = 0;
730 sf->partition_by_variance = 0;
731 sf->use_one_partition_size_always = 0;
732 sf->less_rectangular_check = 0;
733 sf->use_square_partition_only = 0;
734 sf->use_partitions_less_than = 0;
735 sf->less_than_block_size = BLOCK_SIZE_MB16X16;
736 sf->use_partitions_greater_than = 0;
737 sf->greater_than_block_size = BLOCK_SIZE_SB8X8;
738 sf->adjust_partitioning_from_last_frame = 0;
739 sf->last_partitioning_redo_frequency = 4;
740 sf->disable_splitmv = 0;
741 sf->mode_search_skip_flags = 0;
742 sf->last_chroma_intra_mode = TM_PRED;
743 sf->use_rd_breakout = 0;
744 sf->skip_encode_sb = 0;
745 sf->use_uv_intra_rd_estimate = 0;
746 sf->using_small_partition_info = 0;
747 // Skip any mode not chosen at size < X for all sizes > X
748 // Hence BLOCK_SIZE_SB64X64 (skip is off)
749 sf->unused_mode_skip_lvl = BLOCK_SIZE_SB64X64;
751 #if CONFIG_MULTIPLE_ARF
752 // Switch segmentation off.
753 sf->static_segmentation = 0;
755 sf->static_segmentation = 0;
759 case 0: // best quality mode
760 sf->search_best_filter = SEARCH_BEST_FILTER;
764 #if CONFIG_MULTIPLE_ARF
765 // Switch segmentation off.
766 sf->static_segmentation = 0;
768 sf->static_segmentation = 0;
770 sf->use_avoid_tested_higherror = 1;
771 sf->adaptive_rd_thresh = 1;
772 sf->last_chroma_intra_mode = TM_PRED;
775 sf->comp_inter_joint_search_thresh = BLOCK_SIZE_TYPES;
776 sf->less_rectangular_check = 1;
777 sf->tx_size_search_method = ((cpi->common.frame_type == KEY_FRAME ||
778 cpi->common.intra_only ||
779 cpi->common.show_frame == 0) ?
782 sf->use_square_partition_only = !(cpi->common.frame_type == KEY_FRAME ||
783 cpi->common.intra_only ||
784 cpi->common.show_frame == 0);
785 sf->disable_splitmv =
786 (MIN(cpi->common.width, cpi->common.height) >= 720)? 1 : 0;
787 sf->unused_mode_skip_lvl = BLOCK_SIZE_SB32X32;
788 sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
789 FLAG_SKIP_INTRA_BESTINTER |
790 FLAG_SKIP_COMP_BESTINTRA;
791 sf->last_chroma_intra_mode = H_PRED;
792 sf->use_rd_breakout = 1;
793 sf->skip_encode_sb = 1;
794 sf->auto_mv_step_size = 1;
797 sf->adjust_thresholds_by_speed = 1;
798 sf->less_rectangular_check = 1;
799 sf->use_square_partition_only = 1;
800 sf->comp_inter_joint_search_thresh = BLOCK_SIZE_TYPES;
801 sf->use_lastframe_partitioning = 1;
802 sf->adjust_partitioning_from_last_frame = 1;
803 sf->last_partitioning_redo_frequency = 3;
804 sf->unused_mode_skip_lvl = BLOCK_SIZE_SB32X32;
805 sf->tx_size_search_method = ((cpi->common.frame_type == KEY_FRAME ||
806 cpi->common.intra_only ||
807 cpi->common.show_frame == 0) ?
810 sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
811 FLAG_SKIP_INTRA_BESTINTER |
812 FLAG_SKIP_COMP_BESTINTRA |
813 FLAG_SKIP_COMP_REFMISMATCH;
814 sf->last_chroma_intra_mode = DC_PRED;
815 sf->use_rd_breakout = 1;
816 sf->skip_encode_sb = 1;
817 sf->use_uv_intra_rd_estimate = 1;
818 sf->using_small_partition_info = 1;
819 sf->disable_splitmv =
820 (MIN(cpi->common.width, cpi->common.height) >= 720)? 1 : 0;
821 sf->auto_mv_step_size = 1;
824 sf->comp_inter_joint_search_thresh = BLOCK_SIZE_TYPES;
825 sf->partition_by_variance = 1;
826 sf->tx_size_search_method = ((cpi->common.frame_type == KEY_FRAME ||
827 cpi->common.intra_only ||
828 cpi->common.show_frame == 0) ?
831 sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
832 FLAG_SKIP_INTRA_BESTINTER |
833 FLAG_SKIP_COMP_BESTINTRA |
834 FLAG_SKIP_COMP_REFMISMATCH;
835 sf->use_rd_breakout = 1;
836 sf->skip_encode_sb = 1;
837 sf->disable_splitmv = 1;
838 sf->auto_mv_step_size = 1;
841 sf->comp_inter_joint_search_thresh = BLOCK_SIZE_TYPES;
842 sf->use_one_partition_size_always = 1;
843 sf->always_this_block_size = BLOCK_SIZE_MB16X16;
844 sf->tx_size_search_method = ((cpi->common.frame_type == KEY_FRAME ||
845 cpi->common.intra_only ||
846 cpi->common.show_frame == 0) ?
849 sf->mode_search_skip_flags = FLAG_SKIP_INTRA_DIRMISMATCH |
850 FLAG_SKIP_INTRA_BESTINTER |
851 FLAG_SKIP_COMP_BESTINTRA |
852 FLAG_SKIP_COMP_REFMISMATCH;
853 sf->use_rd_breakout = 1;
854 sf->optimize_coefficients = 0;
855 sf->auto_mv_step_size = 1;
856 // sf->reduce_first_step_size = 1;
857 // sf->reference_masking = 1;
859 sf->disable_splitmv = 1;
864 sf->comp_inter_joint_search_thresh = BLOCK_SIZE_SB8X8;
865 sf->use_partitions_less_than = 1;
866 sf->less_than_block_size = BLOCK_SIZE_MB16X16;
870 sf->comp_inter_joint_search_thresh = BLOCK_SIZE_SB8X8;
871 sf->use_partitions_greater_than = 1;
872 sf->greater_than_block_size = BLOCK_SIZE_SB8X8;
880 // Set rd thresholds based on mode and speed setting
881 if (cpi->sf.adjust_thresholds_by_speed)
882 set_rd_speed_thresholds(cpi, mode, speed);
884 set_rd_speed_thresholds(cpi, mode, 0);
886 // Slow quant, dct and trellis not worthwhile for first pass
887 // so make sure they are always turned off.
888 if (cpi->pass == 1) {
889 sf->optimize_coefficients = 0;
892 cpi->mb.fwd_txm16x16 = vp9_short_fdct16x16;
893 cpi->mb.fwd_txm8x8 = vp9_short_fdct8x8;
894 cpi->mb.fwd_txm8x4 = vp9_short_fdct8x4;
895 cpi->mb.fwd_txm4x4 = vp9_short_fdct4x4;
896 if (cpi->oxcf.lossless || cpi->mb.e_mbd.lossless) {
897 cpi->mb.fwd_txm8x4 = vp9_short_walsh8x4;
898 cpi->mb.fwd_txm4x4 = vp9_short_walsh4x4;
901 cpi->mb.quantize_b_4x4 = vp9_regular_quantize_b_4x4;
903 if (cpi->sf.iterative_sub_pixel == 1) {
904 cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_step_iteratively;
905 } else if (cpi->sf.quarter_pixel_search) {
906 cpi->find_fractional_mv_step = vp9_find_best_sub_pixel_step;
907 } else if (cpi->sf.half_pixel_search) {
908 cpi->find_fractional_mv_step = vp9_find_best_half_pixel_step;
911 cpi->mb.optimize = cpi->sf.optimize_coefficients == 1 && cpi->pass != 1;
914 frames_at_speed[cpi->speed]++;
918 static void alloc_raw_frame_buffers(VP9_COMP *cpi) {
919 VP9_COMMON *cm = &cpi->common;
921 cpi->lookahead = vp9_lookahead_init(cpi->oxcf.width, cpi->oxcf.height,
922 cm->subsampling_x, cm->subsampling_y,
923 cpi->oxcf.lag_in_frames);
925 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
926 "Failed to allocate lag buffers");
928 if (vp9_realloc_frame_buffer(&cpi->alt_ref_buffer,
929 cpi->oxcf.width, cpi->oxcf.height,
930 cm->subsampling_x, cm->subsampling_y,
932 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
933 "Failed to allocate altref buffer");
936 static int alloc_partition_data(VP9_COMP *cpi) {
937 vpx_free(cpi->mb.pip);
939 cpi->mb.pip = vpx_calloc(cpi->common.mode_info_stride *
940 (cpi->common.mi_rows + MI_BLOCK_SIZE),
941 sizeof(PARTITION_INFO));
945 cpi->mb.pi = cpi->mb.pip + cpi->common.mode_info_stride + 1;
950 void vp9_alloc_compressor_data(VP9_COMP *cpi) {
951 VP9_COMMON *cm = &cpi->common;
953 if (vp9_alloc_frame_buffers(cm, cm->width, cm->height))
954 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
955 "Failed to allocate frame buffers");
957 if (alloc_partition_data(cpi))
958 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
959 "Failed to allocate partition data");
961 if (vp9_alloc_frame_buffer(&cpi->last_frame_uf,
962 cm->width, cm->height,
963 cm->subsampling_x, cm->subsampling_y,
965 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
966 "Failed to allocate last frame buffer");
968 if (vp9_alloc_frame_buffer(&cpi->scaled_source,
969 cm->width, cm->height,
970 cm->subsampling_x, cm->subsampling_y,
972 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
973 "Failed to allocate scaled source buffer");
978 unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
980 CHECK_MEM_ERROR(cm, cpi->tok, vpx_calloc(tokens, sizeof(*cpi->tok)));
983 // Data used for real time vc mode to see if gf needs refreshing
984 cpi->inter_zz_count = 0;
985 cpi->gf_bad_count = 0;
986 cpi->gf_update_recommended = 0;
988 vpx_free(cpi->mb_activity_map);
989 CHECK_MEM_ERROR(cm, cpi->mb_activity_map,
990 vpx_calloc(sizeof(unsigned int),
991 cm->mb_rows * cm->mb_cols));
993 vpx_free(cpi->mb_norm_activity_map);
994 CHECK_MEM_ERROR(cm, cpi->mb_norm_activity_map,
995 vpx_calloc(sizeof(unsigned int),
996 cm->mb_rows * cm->mb_cols));
1000 static void update_frame_size(VP9_COMP *cpi) {
1001 VP9_COMMON *cm = &cpi->common;
1003 vp9_update_frame_size(cm);
1005 // Update size of buffers local to this frame
1006 if (vp9_realloc_frame_buffer(&cpi->last_frame_uf,
1007 cm->width, cm->height,
1008 cm->subsampling_x, cm->subsampling_y,
1010 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1011 "Failed to reallocate last frame buffer");
1013 if (vp9_realloc_frame_buffer(&cpi->scaled_source,
1014 cm->width, cm->height,
1015 cm->subsampling_x, cm->subsampling_y,
1017 vpx_internal_error(&cpi->common.error, VPX_CODEC_MEM_ERROR,
1018 "Failed to reallocate scaled source buffer");
1021 int y_stride = cpi->scaled_source.y_stride;
1023 if (cpi->sf.search_method == NSTEP) {
1024 vp9_init3smotion_compensation(&cpi->mb, y_stride);
1025 } else if (cpi->sf.search_method == DIAMOND) {
1026 vp9_init_dsmotion_compensation(&cpi->mb, y_stride);
1032 // TODO perhaps change number of steps expose to outside world when setting
1033 // max and min limits. Also this will likely want refining for the extended Q
1036 // Table that converts 0-63 Q range values passed in outside to the Qindex
1037 // range used internally.
1038 static const int q_trans[] = {
1039 0, 4, 8, 12, 16, 20, 24, 28,
1040 32, 36, 40, 44, 48, 52, 56, 60,
1041 64, 68, 72, 76, 80, 84, 88, 92,
1042 96, 100, 104, 108, 112, 116, 120, 124,
1043 128, 132, 136, 140, 144, 148, 152, 156,
1044 160, 164, 168, 172, 176, 180, 184, 188,
1045 192, 196, 200, 204, 208, 212, 216, 220,
1046 224, 228, 232, 236, 240, 244, 249, 255,
1049 int vp9_reverse_trans(int x) {
1052 for (i = 0; i < 64; i++)
1053 if (q_trans[i] >= x)
1058 void vp9_new_framerate(VP9_COMP *cpi, double framerate) {
1059 if (framerate < 0.1)
1062 cpi->oxcf.framerate = framerate;
1063 cpi->output_framerate = cpi->oxcf.framerate;
1064 cpi->per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_framerate);
1065 cpi->av_per_frame_bandwidth = (int)(cpi->oxcf.target_bandwidth / cpi->output_framerate);
1066 cpi->min_frame_bandwidth = (int)(cpi->av_per_frame_bandwidth * cpi->oxcf.two_pass_vbrmin_section / 100);
1069 cpi->min_frame_bandwidth = MAX(cpi->min_frame_bandwidth, FRAME_OVERHEAD_BITS);
1071 // Set Maximum gf/arf interval
1072 cpi->max_gf_interval = 16;
1074 // Extended interval for genuinely static scenes
1075 cpi->twopass.static_scene_max_gf_interval = cpi->key_frame_frequency >> 1;
1077 // Special conditions when alt ref frame enabled in lagged compress mode
1078 if (cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames) {
1079 if (cpi->max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1080 cpi->max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1082 if (cpi->twopass.static_scene_max_gf_interval > cpi->oxcf.lag_in_frames - 1)
1083 cpi->twopass.static_scene_max_gf_interval = cpi->oxcf.lag_in_frames - 1;
1086 if (cpi->max_gf_interval > cpi->twopass.static_scene_max_gf_interval)
1087 cpi->max_gf_interval = cpi->twopass.static_scene_max_gf_interval;
1090 static int64_t rescale(int val, int64_t num, int denom) {
1091 int64_t llnum = num;
1092 int64_t llden = denom;
1093 int64_t llval = val;
1095 return (llval * llnum / llden);
1098 static void set_tile_limits(VP9_COMP *cpi) {
1099 VP9_COMMON *const cm = &cpi->common;
1101 int min_log2_tile_cols, max_log2_tile_cols;
1102 vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
1104 cm->log2_tile_cols = clamp(cpi->oxcf.tile_columns,
1105 min_log2_tile_cols, max_log2_tile_cols);
1106 cm->log2_tile_rows = cpi->oxcf.tile_rows;
1109 static void init_config(VP9_PTR ptr, VP9_CONFIG *oxcf) {
1110 VP9_COMP *cpi = (VP9_COMP *)(ptr);
1111 VP9_COMMON *const cm = &cpi->common;
1117 cm->version = oxcf->version;
1119 cm->width = oxcf->width;
1120 cm->height = oxcf->height;
1121 cm->subsampling_x = 0;
1122 cm->subsampling_y = 0;
1123 vp9_alloc_compressor_data(cpi);
1125 // change includes all joint functionality
1126 vp9_change_config(ptr, oxcf);
1128 // Initialize active best and worst q and average q values.
1129 cpi->active_worst_quality = cpi->oxcf.worst_allowed_q;
1130 cpi->active_best_quality = cpi->oxcf.best_allowed_q;
1131 cpi->avg_frame_qindex = cpi->oxcf.worst_allowed_q;
1133 // Initialise the starting buffer levels
1134 cpi->buffer_level = cpi->oxcf.starting_buffer_level;
1135 cpi->bits_off_target = cpi->oxcf.starting_buffer_level;
1137 cpi->rolling_target_bits = cpi->av_per_frame_bandwidth;
1138 cpi->rolling_actual_bits = cpi->av_per_frame_bandwidth;
1139 cpi->long_rolling_target_bits = cpi->av_per_frame_bandwidth;
1140 cpi->long_rolling_actual_bits = cpi->av_per_frame_bandwidth;
1142 cpi->total_actual_bits = 0;
1143 cpi->total_target_vs_actual = 0;
1145 cpi->static_mb_pct = 0;
1147 cpi->lst_fb_idx = 0;
1148 cpi->gld_fb_idx = 1;
1149 cpi->alt_fb_idx = 2;
1151 set_tile_limits(cpi);
1153 cpi->fixed_divide[0] = 0;
1154 for (i = 1; i < 512; i++)
1155 cpi->fixed_divide[i] = 0x80000 / i;
1159 void vp9_change_config(VP9_PTR ptr, VP9_CONFIG *oxcf) {
1160 VP9_COMP *cpi = (VP9_COMP *)(ptr);
1161 VP9_COMMON *const cm = &cpi->common;
1166 if (cm->version != oxcf->version) {
1167 cm->version = oxcf->version;
1172 switch (cpi->oxcf.Mode) {
1173 // Real time and one pass deprecated in test code base
1174 case MODE_FIRSTPASS:
1176 cpi->compressor_speed = 1;
1179 case MODE_SECONDPASS:
1181 cpi->compressor_speed = 1;
1182 cpi->oxcf.cpu_used = clamp(cpi->oxcf.cpu_used, -5, 5);
1185 case MODE_SECONDPASS_BEST:
1187 cpi->compressor_speed = 0;
1191 cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
1192 cpi->oxcf.best_allowed_q = q_trans[oxcf->best_allowed_q];
1193 cpi->oxcf.cq_level = q_trans[cpi->oxcf.cq_level];
1195 cpi->oxcf.lossless = oxcf->lossless;
1196 if (cpi->oxcf.lossless) {
1197 cpi->mb.e_mbd.inv_txm4x4_1_add = vp9_short_iwalsh4x4_1_add;
1198 cpi->mb.e_mbd.inv_txm4x4_add = vp9_short_iwalsh4x4_add;
1200 cpi->mb.e_mbd.inv_txm4x4_1_add = vp9_short_idct4x4_1_add;
1201 cpi->mb.e_mbd.inv_txm4x4_add = vp9_short_idct4x4_add;
1204 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
1206 cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
1208 // cpi->use_golden_frame_only = 0;
1209 // cpi->use_last_frame_only = 0;
1210 cpi->refresh_golden_frame = 0;
1211 cpi->refresh_last_frame = 1;
1212 cm->refresh_frame_context = 1;
1213 cm->reset_frame_context = 0;
1215 setup_features(cpi);
1216 cpi->mb.e_mbd.allow_high_precision_mv = 0; // Default mv precision adaptation
1217 set_mvcost(&cpi->mb);
1222 for (i = 0; i < MAX_SEGMENTS; i++)
1223 cpi->segment_encode_breakout[i] = cpi->oxcf.encode_breakout;
1226 // At the moment the first order values may not be > MAXQ
1227 cpi->oxcf.fixed_q = MIN(cpi->oxcf.fixed_q, MAXQ);
1229 // local file playback mode == really big buffer
1230 if (cpi->oxcf.end_usage == USAGE_LOCAL_FILE_PLAYBACK) {
1231 cpi->oxcf.starting_buffer_level = 60000;
1232 cpi->oxcf.optimal_buffer_level = 60000;
1233 cpi->oxcf.maximum_buffer_size = 240000;
1236 // Convert target bandwidth from Kbit/s to Bit/s
1237 cpi->oxcf.target_bandwidth *= 1000;
1239 cpi->oxcf.starting_buffer_level = rescale(cpi->oxcf.starting_buffer_level,
1240 cpi->oxcf.target_bandwidth, 1000);
1242 // Set or reset optimal and maximum buffer levels.
1243 if (cpi->oxcf.optimal_buffer_level == 0)
1244 cpi->oxcf.optimal_buffer_level = cpi->oxcf.target_bandwidth / 8;
1246 cpi->oxcf.optimal_buffer_level = rescale(cpi->oxcf.optimal_buffer_level,
1247 cpi->oxcf.target_bandwidth, 1000);
1249 if (cpi->oxcf.maximum_buffer_size == 0)
1250 cpi->oxcf.maximum_buffer_size = cpi->oxcf.target_bandwidth / 8;
1252 cpi->oxcf.maximum_buffer_size = rescale(cpi->oxcf.maximum_buffer_size,
1253 cpi->oxcf.target_bandwidth, 1000);
1255 // Set up frame rate and related parameters rate control values.
1256 vp9_new_framerate(cpi, cpi->oxcf.framerate);
1258 // Set absolute upper and lower quality limits
1259 cpi->worst_quality = cpi->oxcf.worst_allowed_q;
1260 cpi->best_quality = cpi->oxcf.best_allowed_q;
1262 // active values should only be modified if out of new range
1263 cpi->active_worst_quality = clamp(cpi->active_worst_quality,
1264 cpi->oxcf.best_allowed_q,
1265 cpi->oxcf.worst_allowed_q);
1267 cpi->active_best_quality = clamp(cpi->active_best_quality,
1268 cpi->oxcf.best_allowed_q,
1269 cpi->oxcf.worst_allowed_q);
1271 cpi->buffered_mode = cpi->oxcf.optimal_buffer_level > 0;
1273 cpi->cq_target_quality = cpi->oxcf.cq_level;
1275 cm->mcomp_filter_type = DEFAULT_INTERP_FILTER;
1277 cpi->target_bandwidth = cpi->oxcf.target_bandwidth;
1279 cm->display_width = cpi->oxcf.width;
1280 cm->display_height = cpi->oxcf.height;
1282 // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
1283 cpi->oxcf.Sharpness = MIN(7, cpi->oxcf.Sharpness);
1285 cpi->mb.e_mbd.lf.sharpness_level = cpi->oxcf.Sharpness;
1287 if (cpi->initial_width) {
1288 // Increasing the size of the frame beyond the first seen frame, or some
1289 // otherwise signalled maximum size, is not supported.
1290 // TODO(jkoleszar): exit gracefully.
1291 assert(cm->width <= cpi->initial_width);
1292 assert(cm->height <= cpi->initial_height);
1294 update_frame_size(cpi);
1296 if (cpi->oxcf.fixed_q >= 0) {
1297 cpi->last_q[0] = cpi->oxcf.fixed_q;
1298 cpi->last_q[1] = cpi->oxcf.fixed_q;
1299 cpi->last_boosted_qindex = cpi->oxcf.fixed_q;
1302 cpi->speed = cpi->oxcf.cpu_used;
1304 if (cpi->oxcf.lag_in_frames == 0) {
1305 // force to allowlag to 0 if lag_in_frames is 0;
1306 cpi->oxcf.allow_lag = 0;
1307 } else if (cpi->oxcf.lag_in_frames > MAX_LAG_BUFFERS) {
1308 // Limit on lag buffers as these are not currently dynamically allocated
1309 cpi->oxcf.lag_in_frames = MAX_LAG_BUFFERS;
1313 #if CONFIG_MULTIPLE_ARF
1314 vp9_zero(cpi->alt_ref_source);
1316 cpi->alt_ref_source = NULL;
1318 cpi->is_src_frame_alt_ref = 0;
1321 // Experimental RD Code
1322 cpi->frame_distortion = 0;
1323 cpi->last_frame_distortion = 0;
1326 set_tile_limits(cpi);
1329 #define M_LOG2_E 0.693147180559945309417
1330 #define log2f(x) (log (x) / (float) M_LOG2_E)
1332 static void cal_nmvjointsadcost(int *mvjointsadcost) {
1333 mvjointsadcost[0] = 600;
1334 mvjointsadcost[1] = 300;
1335 mvjointsadcost[2] = 300;
1336 mvjointsadcost[0] = 300;
1339 static void cal_nmvsadcosts(int *mvsadcost[2]) {
1342 mvsadcost[0][0] = 0;
1343 mvsadcost[1][0] = 0;
1346 double z = 256 * (2 * (log2f(8 * i) + .6));
1347 mvsadcost[0][i] = (int)z;
1348 mvsadcost[1][i] = (int)z;
1349 mvsadcost[0][-i] = (int)z;
1350 mvsadcost[1][-i] = (int)z;
1351 } while (++i <= MV_MAX);
1354 static void cal_nmvsadcosts_hp(int *mvsadcost[2]) {
1357 mvsadcost[0][0] = 0;
1358 mvsadcost[1][0] = 0;
1361 double z = 256 * (2 * (log2f(8 * i) + .6));
1362 mvsadcost[0][i] = (int)z;
1363 mvsadcost[1][i] = (int)z;
1364 mvsadcost[0][-i] = (int)z;
1365 mvsadcost[1][-i] = (int)z;
1366 } while (++i <= MV_MAX);
1369 VP9_PTR vp9_create_compressor(VP9_CONFIG *oxcf) {
1379 cpi = ctx.cpi = vpx_memalign(32, sizeof(VP9_COMP));
1380 // Check that the CPI instance is valid
1386 vpx_memset(cpi, 0, sizeof(VP9_COMP));
1388 if (setjmp(cm->error.jmp)) {
1389 VP9_PTR ptr = ctx.ptr;
1391 ctx.cpi->common.error.setjmp = 0;
1392 vp9_remove_compressor(&ptr);
1396 cm->error.setjmp = 1;
1398 CHECK_MEM_ERROR(cm, cpi->mb.ss, vpx_calloc(sizeof(search_site),
1399 (MAX_MVSEARCH_STEPS * 8) + 1));
1401 vp9_create_common(cm);
1403 init_config((VP9_PTR)cpi, oxcf);
1405 cm->current_video_frame = 0;
1406 cpi->kf_overspend_bits = 0;
1407 cpi->kf_bitrate_adjustment = 0;
1408 cpi->frames_till_gf_update_due = 0;
1409 cpi->gf_overspend_bits = 0;
1410 cpi->non_gf_bitrate_adjustment = 0;
1412 // Set reference frame sign bias for ALTREF frame to 1 (for now)
1413 cm->ref_frame_sign_bias[ALTREF_FRAME] = 1;
1415 cpi->baseline_gf_interval = DEFAULT_GF_INTERVAL;
1417 cpi->gold_is_last = 0;
1418 cpi->alt_is_last = 0;
1419 cpi->gold_is_alt = 0;
1421 // Create the encoder segmentation map and set all entries to 0
1422 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
1423 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1425 // And a copy in common for temporal coding
1426 CHECK_MEM_ERROR(cm, cm->last_frame_seg_map,
1427 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1429 // And a place holder structure is the coding context
1430 // for use if we want to save and restore it
1431 CHECK_MEM_ERROR(cm, cpi->coding_context.last_frame_seg_map_copy,
1432 vpx_calloc(cm->mi_rows * cm->mi_cols, 1));
1434 CHECK_MEM_ERROR(cm, cpi->active_map, vpx_calloc(cm->MBs, 1));
1435 vpx_memset(cpi->active_map, 1, cm->MBs);
1436 cpi->active_map_enabled = 0;
1438 for (i = 0; i < (sizeof(cpi->mbgraph_stats) /
1439 sizeof(cpi->mbgraph_stats[0])); i++) {
1440 CHECK_MEM_ERROR(cm, cpi->mbgraph_stats[i].mb_stats,
1441 vpx_calloc(cm->MBs *
1442 sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
1445 #ifdef ENTROPY_STATS
1447 init_context_counters();
1454 init_tx_count_stats();
1455 init_switchable_interp_stats();
1458 /*Initialize the feed-forward activity masking.*/
1459 cpi->activity_avg = 90 << 12;
1461 cpi->frames_since_key = 8; // Give a sensible default for the first frame.
1462 cpi->key_frame_frequency = cpi->oxcf.key_freq;
1463 cpi->this_key_frame_forced = 0;
1464 cpi->next_key_frame_forced = 0;
1466 cpi->source_alt_ref_pending = 0;
1467 cpi->source_alt_ref_active = 0;
1468 cpi->refresh_alt_ref_frame = 0;
1470 #if CONFIG_MULTIPLE_ARF
1471 // Turn multiple ARF usage on/off. This is a quick hack for the initial test
1472 // version. It should eventually be set via the codec API.
1473 cpi->multi_arf_enabled = 1;
1475 if (cpi->multi_arf_enabled) {
1476 cpi->sequence_number = 0;
1477 cpi->frame_coding_order_period = 0;
1478 vp9_zero(cpi->frame_coding_order);
1479 vp9_zero(cpi->arf_buffer_idx);
1483 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
1484 #if CONFIG_INTERNAL_STATS
1485 cpi->b_calculate_ssimg = 0;
1490 if (cpi->b_calculate_psnr) {
1491 cpi->total_sq_error = 0.0;
1492 cpi->total_sq_error2 = 0.0;
1497 cpi->totalp_y = 0.0;
1498 cpi->totalp_u = 0.0;
1499 cpi->totalp_v = 0.0;
1501 cpi->tot_recode_hits = 0;
1502 cpi->summed_quality = 0;
1503 cpi->summed_weights = 0;
1504 cpi->summedp_quality = 0;
1505 cpi->summedp_weights = 0;
1508 if (cpi->b_calculate_ssimg) {
1509 cpi->total_ssimg_y = 0;
1510 cpi->total_ssimg_u = 0;
1511 cpi->total_ssimg_v = 0;
1512 cpi->total_ssimg_all = 0;
1517 cpi->first_time_stamp_ever = INT64_MAX;
1519 cpi->frames_till_gf_update_due = 0;
1520 cpi->key_frame_count = 1;
1522 cpi->ni_av_qi = cpi->oxcf.worst_allowed_q;
1526 cpi->avg_q = vp9_convert_qindex_to_q(cpi->oxcf.worst_allowed_q);
1527 cpi->total_byte_count = 0;
1529 cpi->rate_correction_factor = 1.0;
1530 cpi->key_frame_rate_correction_factor = 1.0;
1531 cpi->gf_rate_correction_factor = 1.0;
1532 cpi->twopass.est_max_qcorrection_factor = 1.0;
1534 cal_nmvjointsadcost(cpi->mb.nmvjointsadcost);
1535 cpi->mb.nmvcost[0] = &cpi->mb.nmvcosts[0][MV_MAX];
1536 cpi->mb.nmvcost[1] = &cpi->mb.nmvcosts[1][MV_MAX];
1537 cpi->mb.nmvsadcost[0] = &cpi->mb.nmvsadcosts[0][MV_MAX];
1538 cpi->mb.nmvsadcost[1] = &cpi->mb.nmvsadcosts[1][MV_MAX];
1539 cal_nmvsadcosts(cpi->mb.nmvsadcost);
1541 cpi->mb.nmvcost_hp[0] = &cpi->mb.nmvcosts_hp[0][MV_MAX];
1542 cpi->mb.nmvcost_hp[1] = &cpi->mb.nmvcosts_hp[1][MV_MAX];
1543 cpi->mb.nmvsadcost_hp[0] = &cpi->mb.nmvsadcosts_hp[0][MV_MAX];
1544 cpi->mb.nmvsadcost_hp[1] = &cpi->mb.nmvsadcosts_hp[1][MV_MAX];
1545 cal_nmvsadcosts_hp(cpi->mb.nmvsadcost_hp);
1547 for (i = 0; i < KEY_FRAME_CONTEXT; i++)
1548 cpi->prior_key_frame_distance[i] = (int)cpi->output_framerate;
1550 #ifdef OUTPUT_YUV_SRC
1551 yuv_file = fopen("bd.yuv", "ab");
1553 #ifdef OUTPUT_YUV_REC
1554 yuv_rec_file = fopen("rec.yuv", "wb");
1558 framepsnr = fopen("framepsnr.stt", "a");
1559 kf_list = fopen("kf_list.stt", "w");
1562 cpi->output_pkt_list = oxcf->output_pkt_list;
1564 if (cpi->pass == 1) {
1565 vp9_init_first_pass(cpi);
1566 } else if (cpi->pass == 2) {
1567 size_t packet_sz = sizeof(FIRSTPASS_STATS);
1568 int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
1570 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
1571 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
1572 cpi->twopass.stats_in_end = (void *)((char *)cpi->twopass.stats_in
1573 + (packets - 1) * packet_sz);
1574 vp9_init_second_pass(cpi);
1577 vp9_set_speed_features(cpi);
1579 // Set starting values of RD threshold multipliers (128 = *1)
1580 for (i = 0; i < MAX_MODES; i++)
1581 cpi->rd_thresh_mult[i] = 128;
1583 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SVFHH, SVFHV, SVFHHV, \
1584 SDX3F, SDX8F, SDX4DF)\
1585 cpi->fn_ptr[BT].sdf = SDF; \
1586 cpi->fn_ptr[BT].sdaf = SDAF; \
1587 cpi->fn_ptr[BT].vf = VF; \
1588 cpi->fn_ptr[BT].svf = SVF; \
1589 cpi->fn_ptr[BT].svaf = SVAF; \
1590 cpi->fn_ptr[BT].svf_halfpix_h = SVFHH; \
1591 cpi->fn_ptr[BT].svf_halfpix_v = SVFHV; \
1592 cpi->fn_ptr[BT].svf_halfpix_hv = SVFHHV; \
1593 cpi->fn_ptr[BT].sdx3f = SDX3F; \
1594 cpi->fn_ptr[BT].sdx8f = SDX8F; \
1595 cpi->fn_ptr[BT].sdx4df = SDX4DF;
1597 BFP(BLOCK_32X16, vp9_sad32x16, vp9_sad32x16_avg,
1598 vp9_variance32x16, vp9_sub_pixel_variance32x16,
1599 vp9_sub_pixel_avg_variance32x16, NULL, NULL,
1603 BFP(BLOCK_16X32, vp9_sad16x32, vp9_sad16x32_avg,
1604 vp9_variance16x32, vp9_sub_pixel_variance16x32,
1605 vp9_sub_pixel_avg_variance16x32, NULL, NULL,
1609 BFP(BLOCK_64X32, vp9_sad64x32, vp9_sad64x32_avg,
1610 vp9_variance64x32, vp9_sub_pixel_variance64x32,
1611 vp9_sub_pixel_avg_variance64x32, NULL, NULL,
1615 BFP(BLOCK_32X64, vp9_sad32x64, vp9_sad32x64_avg,
1616 vp9_variance32x64, vp9_sub_pixel_variance32x64,
1617 vp9_sub_pixel_avg_variance32x64, NULL, NULL,
1621 BFP(BLOCK_32X32, vp9_sad32x32, vp9_sad32x32_avg,
1622 vp9_variance32x32, vp9_sub_pixel_variance32x32,
1623 vp9_sub_pixel_avg_variance32x32, vp9_variance_halfpixvar32x32_h,
1624 vp9_variance_halfpixvar32x32_v,
1625 vp9_variance_halfpixvar32x32_hv, vp9_sad32x32x3, vp9_sad32x32x8,
1628 BFP(BLOCK_64X64, vp9_sad64x64, vp9_sad64x64_avg,
1629 vp9_variance64x64, vp9_sub_pixel_variance64x64,
1630 vp9_sub_pixel_avg_variance64x64, vp9_variance_halfpixvar64x64_h,
1631 vp9_variance_halfpixvar64x64_v,
1632 vp9_variance_halfpixvar64x64_hv, vp9_sad64x64x3, vp9_sad64x64x8,
1635 BFP(BLOCK_16X16, vp9_sad16x16, vp9_sad16x16_avg,
1636 vp9_variance16x16, vp9_sub_pixel_variance16x16,
1637 vp9_sub_pixel_avg_variance16x16, vp9_variance_halfpixvar16x16_h,
1638 vp9_variance_halfpixvar16x16_v,
1639 vp9_variance_halfpixvar16x16_hv, vp9_sad16x16x3, vp9_sad16x16x8,
1642 BFP(BLOCK_16X8, vp9_sad16x8, vp9_sad16x8_avg,
1643 vp9_variance16x8, vp9_sub_pixel_variance16x8,
1644 vp9_sub_pixel_avg_variance16x8, NULL, NULL, NULL,
1645 vp9_sad16x8x3, vp9_sad16x8x8, vp9_sad16x8x4d)
1647 BFP(BLOCK_8X16, vp9_sad8x16, vp9_sad8x16_avg,
1648 vp9_variance8x16, vp9_sub_pixel_variance8x16,
1649 vp9_sub_pixel_avg_variance8x16, NULL, NULL, NULL,
1650 vp9_sad8x16x3, vp9_sad8x16x8, vp9_sad8x16x4d)
1652 BFP(BLOCK_8X8, vp9_sad8x8, vp9_sad8x8_avg,
1653 vp9_variance8x8, vp9_sub_pixel_variance8x8,
1654 vp9_sub_pixel_avg_variance8x8, NULL, NULL, NULL,
1655 vp9_sad8x8x3, vp9_sad8x8x8, vp9_sad8x8x4d)
1657 BFP(BLOCK_8X4, vp9_sad8x4, vp9_sad8x4_avg,
1658 vp9_variance8x4, vp9_sub_pixel_variance8x4,
1659 vp9_sub_pixel_avg_variance8x4, NULL, NULL,
1660 NULL, NULL, vp9_sad8x4x8,
1663 BFP(BLOCK_4X8, vp9_sad4x8, vp9_sad4x8_avg,
1664 vp9_variance4x8, vp9_sub_pixel_variance4x8,
1665 vp9_sub_pixel_avg_variance4x8, NULL, NULL,
1666 NULL, NULL, vp9_sad4x8x8,
1669 BFP(BLOCK_4X4, vp9_sad4x4, vp9_sad4x4_avg,
1670 vp9_variance4x4, vp9_sub_pixel_variance4x4,
1671 vp9_sub_pixel_avg_variance4x4, NULL, NULL, NULL,
1672 vp9_sad4x4x3, vp9_sad4x4x8, vp9_sad4x4x4d)
1674 cpi->full_search_sad = vp9_full_search_sad;
1675 cpi->diamond_search_sad = vp9_diamond_search_sad;
1676 cpi->refining_search_sad = vp9_refining_search_sad;
1678 // make sure frame 1 is okay
1679 cpi->error_bins[0] = cpi->common.MBs;
1681 /* vp9_init_quantizer() is first called here. Add check in
1682 * vp9_frame_init_quantizer() so that vp9_init_quantizer is only
1683 * called later when needed. This will avoid unnecessary calls of
1684 * vp9_init_quantizer() for every frame.
1686 vp9_init_quantizer(cpi);
1688 vp9_loop_filter_init(cm, &cpi->mb.e_mbd.lf);
1690 cpi->common.error.setjmp = 0;
1692 vp9_zero(cpi->y_uv_mode_count)
1694 return (VP9_PTR) cpi;
1697 void vp9_remove_compressor(VP9_PTR *ptr) {
1698 VP9_COMP *cpi = (VP9_COMP *)(*ptr);
1704 if (cpi && (cpi->common.current_video_frame > 0)) {
1705 if (cpi->pass == 2) {
1706 vp9_end_second_pass(cpi);
1709 #ifdef ENTROPY_STATS
1710 if (cpi->pass != 1) {
1711 print_context_counters();
1712 print_tree_update_probs();
1713 print_mode_context(cpi);
1721 if (cpi->pass != 1) {
1722 write_tx_count_stats();
1723 write_switchable_interp_stats();
1727 #if CONFIG_INTERNAL_STATS
1729 vp9_clear_system_state();
1731 // printf("\n8x8-4x4:%d-%d\n", cpi->t8x8_count, cpi->t4x4_count);
1732 if (cpi->pass != 1) {
1733 FILE *f = fopen("opsnr.stt", "a");
1734 double time_encoded = (cpi->last_end_time_stamp_seen
1735 - cpi->first_time_stamp_ever) / 10000000.000;
1736 double total_encode_time = (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
1737 double dr = (double)cpi->bytes * (double) 8 / (double)1000 / time_encoded;
1739 if (cpi->b_calculate_psnr) {
1740 YV12_BUFFER_CONFIG *lst_yv12 =
1741 &cpi->common.yv12_fb[cpi->common.ref_frame_map[cpi->lst_fb_idx]];
1742 double samples = 3.0 / 2 * cpi->count *
1743 lst_yv12->y_width * lst_yv12->y_height;
1744 double total_psnr = vp9_mse2psnr(samples, 255.0, cpi->total_sq_error);
1745 double total_psnr2 = vp9_mse2psnr(samples, 255.0, cpi->total_sq_error2);
1746 double total_ssim = 100 * pow(cpi->summed_quality /
1747 cpi->summed_weights, 8.0);
1748 double total_ssimp = 100 * pow(cpi->summedp_quality /
1749 cpi->summedp_weights, 8.0);
1751 fprintf(f, "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
1752 "VPXSSIM\tVPSSIMP\t Time(ms)\n");
1753 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",
1754 dr, cpi->total / cpi->count, total_psnr,
1755 cpi->totalp / cpi->count, total_psnr2, total_ssim, total_ssimp,
1757 // fprintf(f, "%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t%8.0f %10ld\n",
1758 // dr, cpi->total / cpi->count, total_psnr,
1759 // cpi->totalp / cpi->count, total_psnr2, total_ssim,
1760 // total_encode_time, cpi->tot_recode_hits);
1763 if (cpi->b_calculate_ssimg) {
1764 fprintf(f, "BitRate\tSSIM_Y\tSSIM_U\tSSIM_V\tSSIM_A\t Time(ms)\n");
1765 fprintf(f, "%7.2f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f\n", dr,
1766 cpi->total_ssimg_y / cpi->count, cpi->total_ssimg_u / cpi->count,
1767 cpi->total_ssimg_v / cpi->count, cpi->total_ssimg_all / cpi->count, total_encode_time);
1768 // fprintf(f, "%7.3f\t%6.4f\t%6.4f\t%6.4f\t%6.4f\t%8.0f %10ld\n", dr,
1769 // cpi->total_ssimg_y / cpi->count, cpi->total_ssimg_u / cpi->count,
1770 // cpi->total_ssimg_v / cpi->count, cpi->total_ssimg_all / cpi->count, total_encode_time, cpi->tot_recode_hits);
1778 #ifdef ENTROPY_STATS
1781 FILE *fmode = fopen("vp9_modecontext.c", "w");
1783 fprintf(fmode, "\n#include \"vp9_entropymode.h\"\n\n");
1784 fprintf(fmode, "const unsigned int vp9_kf_default_bmode_counts ");
1785 fprintf(fmode, "[VP9_INTRA_MODES][VP9_INTRA_MODES]"
1786 "[VP9_INTRA_MODES] =\n{\n");
1788 for (i = 0; i < VP9_INTRA_MODES; i++) {
1790 fprintf(fmode, " { // Above Mode : %d\n", i);
1792 for (j = 0; j < VP9_INTRA_MODES; j++) {
1794 fprintf(fmode, " {");
1796 for (k = 0; k < VP9_INTRA_MODES; k++) {
1797 if (!intra_mode_stats[i][j][k])
1798 fprintf(fmode, " %5d, ", 1);
1800 fprintf(fmode, " %5d, ", intra_mode_stats[i][j][k]);
1803 fprintf(fmode, "}, // left_mode %d\n", j);
1807 fprintf(fmode, " },\n");
1811 fprintf(fmode, "};\n");
1817 #if defined(SECTIONBITS_OUTPUT)
1821 FILE *f = fopen("tokenbits.stt", "a");
1823 for (i = 0; i < 28; i++)
1824 fprintf(f, "%8d", (int)(Sectionbits[i] / 256));
1834 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
1835 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
1836 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame, cpi->time_receive_data / 1000, cpi->time_encode_mb_row / 1000, cpi->time_compress_data / 1000, (cpi->time_receive_data + cpi->time_compress_data) / 1000);
1842 dealloc_compressor_data(cpi);
1843 vpx_free(cpi->mb.ss);
1846 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]); i++) {
1847 vpx_free(cpi->mbgraph_stats[i].mb_stats);
1850 vp9_remove_common(&cpi->common);
1854 #ifdef OUTPUT_YUV_SRC
1857 #ifdef OUTPUT_YUV_REC
1858 fclose(yuv_rec_file);
1877 static uint64_t calc_plane_error(uint8_t *orig, int orig_stride,
1878 uint8_t *recon, int recon_stride,
1879 unsigned int cols, unsigned int rows) {
1880 unsigned int row, col;
1881 uint64_t total_sse = 0;
1884 for (row = 0; row + 16 <= rows; row += 16) {
1885 for (col = 0; col + 16 <= cols; col += 16) {
1888 vp9_mse16x16(orig + col, orig_stride, recon + col, recon_stride, &sse);
1892 /* Handle odd-sized width */
1894 unsigned int border_row, border_col;
1895 uint8_t *border_orig = orig;
1896 uint8_t *border_recon = recon;
1898 for (border_row = 0; border_row < 16; border_row++) {
1899 for (border_col = col; border_col < cols; border_col++) {
1900 diff = border_orig[border_col] - border_recon[border_col];
1901 total_sse += diff * diff;
1904 border_orig += orig_stride;
1905 border_recon += recon_stride;
1909 orig += orig_stride * 16;
1910 recon += recon_stride * 16;
1913 /* Handle odd-sized height */
1914 for (; row < rows; row++) {
1915 for (col = 0; col < cols; col++) {
1916 diff = orig[col] - recon[col];
1917 total_sse += diff * diff;
1920 orig += orig_stride;
1921 recon += recon_stride;
1928 static void generate_psnr_packet(VP9_COMP *cpi) {
1929 YV12_BUFFER_CONFIG *orig = cpi->Source;
1930 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
1931 struct vpx_codec_cx_pkt pkt;
1934 unsigned int width = orig->y_crop_width;
1935 unsigned int height = orig->y_crop_height;
1937 pkt.kind = VPX_CODEC_PSNR_PKT;
1938 sse = calc_plane_error(orig->y_buffer, orig->y_stride,
1939 recon->y_buffer, recon->y_stride,
1941 pkt.data.psnr.sse[0] = sse;
1942 pkt.data.psnr.sse[1] = sse;
1943 pkt.data.psnr.samples[0] = width * height;
1944 pkt.data.psnr.samples[1] = width * height;
1946 width = orig->uv_crop_width;
1947 height = orig->uv_crop_height;
1949 sse = calc_plane_error(orig->u_buffer, orig->uv_stride,
1950 recon->u_buffer, recon->uv_stride,
1952 pkt.data.psnr.sse[0] += sse;
1953 pkt.data.psnr.sse[2] = sse;
1954 pkt.data.psnr.samples[0] += width * height;
1955 pkt.data.psnr.samples[2] = width * height;
1957 sse = calc_plane_error(orig->v_buffer, orig->uv_stride,
1958 recon->v_buffer, recon->uv_stride,
1960 pkt.data.psnr.sse[0] += sse;
1961 pkt.data.psnr.sse[3] = sse;
1962 pkt.data.psnr.samples[0] += width * height;
1963 pkt.data.psnr.samples[3] = width * height;
1965 for (i = 0; i < 4; i++)
1966 pkt.data.psnr.psnr[i] = vp9_mse2psnr(pkt.data.psnr.samples[i], 255.0,
1967 (double)pkt.data.psnr.sse[i]);
1969 vpx_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
1973 int vp9_use_as_reference(VP9_PTR ptr, int ref_frame_flags) {
1974 VP9_COMP *cpi = (VP9_COMP *)(ptr);
1976 if (ref_frame_flags > 7)
1979 cpi->ref_frame_flags = ref_frame_flags;
1982 int vp9_update_reference(VP9_PTR ptr, int ref_frame_flags) {
1983 VP9_COMP *cpi = (VP9_COMP *)(ptr);
1985 if (ref_frame_flags > 7)
1988 cpi->refresh_golden_frame = 0;
1989 cpi->refresh_alt_ref_frame = 0;
1990 cpi->refresh_last_frame = 0;
1992 if (ref_frame_flags & VP9_LAST_FLAG)
1993 cpi->refresh_last_frame = 1;
1995 if (ref_frame_flags & VP9_GOLD_FLAG)
1996 cpi->refresh_golden_frame = 1;
1998 if (ref_frame_flags & VP9_ALT_FLAG)
1999 cpi->refresh_alt_ref_frame = 1;
2004 int vp9_copy_reference_enc(VP9_PTR ptr, VP9_REFFRAME ref_frame_flag,
2005 YV12_BUFFER_CONFIG *sd) {
2006 VP9_COMP *cpi = (VP9_COMP *)(ptr);
2007 VP9_COMMON *cm = &cpi->common;
2010 if (ref_frame_flag == VP9_LAST_FLAG)
2011 ref_fb_idx = cm->ref_frame_map[cpi->lst_fb_idx];
2012 else if (ref_frame_flag == VP9_GOLD_FLAG)
2013 ref_fb_idx = cm->ref_frame_map[cpi->gld_fb_idx];
2014 else if (ref_frame_flag == VP9_ALT_FLAG)
2015 ref_fb_idx = cm->ref_frame_map[cpi->alt_fb_idx];
2019 vp8_yv12_copy_frame(&cm->yv12_fb[ref_fb_idx], sd);
2024 int vp9_get_reference_enc(VP9_PTR ptr, int index, YV12_BUFFER_CONFIG **fb) {
2025 VP9_COMP *cpi = (VP9_COMP *)(ptr);
2026 VP9_COMMON *cm = &cpi->common;
2028 if (index < 0 || index >= NUM_REF_FRAMES)
2031 *fb = &cm->yv12_fb[cm->ref_frame_map[index]];
2035 int vp9_set_reference_enc(VP9_PTR ptr, VP9_REFFRAME ref_frame_flag,
2036 YV12_BUFFER_CONFIG *sd) {
2037 VP9_COMP *cpi = (VP9_COMP *)(ptr);
2038 VP9_COMMON *cm = &cpi->common;
2042 if (ref_frame_flag == VP9_LAST_FLAG)
2043 ref_fb_idx = cm->ref_frame_map[cpi->lst_fb_idx];
2044 else if (ref_frame_flag == VP9_GOLD_FLAG)
2045 ref_fb_idx = cm->ref_frame_map[cpi->gld_fb_idx];
2046 else if (ref_frame_flag == VP9_ALT_FLAG)
2047 ref_fb_idx = cm->ref_frame_map[cpi->alt_fb_idx];
2051 vp8_yv12_copy_frame(sd, &cm->yv12_fb[ref_fb_idx]);
2055 int vp9_update_entropy(VP9_PTR comp, int update) {
2056 ((VP9_COMP *)comp)->common.refresh_frame_context = update;
2061 #ifdef OUTPUT_YUV_SRC
2062 void vp9_write_yuv_frame(YV12_BUFFER_CONFIG *s) {
2063 uint8_t *src = s->y_buffer;
2064 int h = s->y_height;
2067 fwrite(src, s->y_width, 1, yuv_file);
2075 fwrite(src, s->uv_width, 1, yuv_file);
2076 src += s->uv_stride;
2083 fwrite(src, s->uv_width, 1, yuv_file);
2084 src += s->uv_stride;
2089 #ifdef OUTPUT_YUV_REC
2090 void vp9_write_yuv_rec_frame(VP9_COMMON *cm) {
2091 YV12_BUFFER_CONFIG *s = cm->frame_to_show;
2092 uint8_t *src = s->y_buffer;
2096 fwrite(src, s->y_width, 1, yuv_rec_file);
2104 fwrite(src, s->uv_width, 1, yuv_rec_file);
2105 src += s->uv_stride;
2112 fwrite(src, s->uv_width, 1, yuv_rec_file);
2113 src += s->uv_stride;
2117 if (s->alpha_buffer) {
2118 src = s->alpha_buffer;
2119 h = s->alpha_height;
2121 fwrite(src, s->alpha_width, 1, yuv_rec_file);
2122 src += s->alpha_stride;
2127 fflush(yuv_rec_file);
2131 static void scale_and_extend_frame(YV12_BUFFER_CONFIG *src_fb,
2132 YV12_BUFFER_CONFIG *dst_fb) {
2133 const int in_w = src_fb->y_crop_width;
2134 const int in_h = src_fb->y_crop_height;
2135 const int out_w = dst_fb->y_crop_width;
2136 const int out_h = dst_fb->y_crop_height;
2139 uint8_t *srcs[4] = {src_fb->y_buffer, src_fb->u_buffer, src_fb->v_buffer,
2140 src_fb->alpha_buffer};
2141 int src_strides[4] = {src_fb->y_stride, src_fb->uv_stride, src_fb->uv_stride,
2142 src_fb->alpha_stride};
2144 uint8_t *dsts[4] = {dst_fb->y_buffer, dst_fb->u_buffer, dst_fb->v_buffer,
2145 dst_fb->alpha_buffer};
2146 int dst_strides[4] = {dst_fb->y_stride, dst_fb->uv_stride, dst_fb->uv_stride,
2147 dst_fb->alpha_stride};
2149 for (y = 0; y < out_h; y += 16) {
2150 for (x = 0; x < out_w; x += 16) {
2151 for (i = 0; i < MAX_MB_PLANE; ++i) {
2152 const int factor = i == 0 ? 1 : 2;
2153 const int x_q4 = x * (16 / factor) * in_w / out_w;
2154 const int y_q4 = y * (16 / factor) * in_h / out_h;
2155 const int src_stride = src_strides[i];
2156 const int dst_stride = dst_strides[i];
2157 uint8_t *src = srcs[i] + y / factor * in_h / out_h * src_stride +
2158 x / factor * in_w / out_w;
2159 uint8_t *dst = dsts[i] + y / factor * dst_stride + x / factor;
2161 vp9_convolve8(src, src_stride, dst, dst_stride,
2162 vp9_sub_pel_filters_8[x_q4 & 0xf], 16 * in_w / out_w,
2163 vp9_sub_pel_filters_8[y_q4 & 0xf], 16 * in_h / out_h,
2164 16 / factor, 16 / factor);
2169 vp8_yv12_extend_frame_borders(dst_fb);
2173 static void update_alt_ref_frame_stats(VP9_COMP *cpi) {
2174 // this frame refreshes means next frames don't unless specified by user
2175 cpi->frames_since_golden = 0;
2177 #if CONFIG_MULTIPLE_ARF
2178 if (!cpi->multi_arf_enabled)
2180 // Clear the alternate reference update pending flag.
2181 cpi->source_alt_ref_pending = 0;
2183 // Set the alternate reference frame active flag
2184 cpi->source_alt_ref_active = 1;
2186 static void update_golden_frame_stats(VP9_COMP *cpi) {
2187 // Update the Golden frame usage counts.
2188 if (cpi->refresh_golden_frame) {
2189 // this frame refreshes means next frames don't unless specified by user
2190 cpi->refresh_golden_frame = 0;
2191 cpi->frames_since_golden = 0;
2193 // ******** Fixed Q test code only ************
2194 // If we are going to use the ALT reference for the next group of frames set a flag to say so.
2195 if (cpi->oxcf.fixed_q >= 0 &&
2196 cpi->oxcf.play_alternate && !cpi->refresh_alt_ref_frame) {
2197 cpi->source_alt_ref_pending = 1;
2198 cpi->frames_till_gf_update_due = cpi->baseline_gf_interval;
2201 if (!cpi->source_alt_ref_pending)
2202 cpi->source_alt_ref_active = 0;
2204 // Decrement count down till next gf
2205 if (cpi->frames_till_gf_update_due > 0)
2206 cpi->frames_till_gf_update_due--;
2208 } else if (!cpi->refresh_alt_ref_frame) {
2209 // Decrement count down till next gf
2210 if (cpi->frames_till_gf_update_due > 0)
2211 cpi->frames_till_gf_update_due--;
2213 if (cpi->frames_till_alt_ref_frame)
2214 cpi->frames_till_alt_ref_frame--;
2216 cpi->frames_since_golden++;
2220 static int find_fp_qindex() {
2223 for (i = 0; i < QINDEX_RANGE; i++) {
2224 if (vp9_convert_qindex_to_q(i) >= 30.0) {
2229 if (i == QINDEX_RANGE)
2235 static void Pass1Encode(VP9_COMP *cpi, unsigned long *size, unsigned char *dest, unsigned int *frame_flags) {
2241 vp9_set_quantizer(cpi, find_fp_qindex());
2242 vp9_first_pass(cpi);
2245 #define WRITE_RECON_BUFFER 0
2246 #if WRITE_RECON_BUFFER
2247 void write_cx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame) {
2254 sprintf(filename, "cx\\y%04d.raw", this_frame);
2255 yframe = fopen(filename, "wb");
2257 for (i = 0; i < frame->y_height; i++)
2258 fwrite(frame->y_buffer + i * frame->y_stride,
2259 frame->y_width, 1, yframe);
2262 sprintf(filename, "cx\\u%04d.raw", this_frame);
2263 yframe = fopen(filename, "wb");
2265 for (i = 0; i < frame->uv_height; i++)
2266 fwrite(frame->u_buffer + i * frame->uv_stride,
2267 frame->uv_width, 1, yframe);
2270 sprintf(filename, "cx\\v%04d.raw", this_frame);
2271 yframe = fopen(filename, "wb");
2273 for (i = 0; i < frame->uv_height; i++)
2274 fwrite(frame->v_buffer + i * frame->uv_stride,
2275 frame->uv_width, 1, yframe);
2281 static double compute_edge_pixel_proportion(YV12_BUFFER_CONFIG *frame) {
2282 #define EDGE_THRESH 128
2284 int num_edge_pels = 0;
2285 int num_pels = (frame->y_height - 2) * (frame->y_width - 2);
2286 uint8_t *prev = frame->y_buffer + 1;
2287 uint8_t *curr = frame->y_buffer + 1 + frame->y_stride;
2288 uint8_t *next = frame->y_buffer + 1 + 2 * frame->y_stride;
2289 for (i = 1; i < frame->y_height - 1; i++) {
2290 for (j = 1; j < frame->y_width - 1; j++) {
2291 /* Sobel hor and ver gradients */
2292 int v = 2 * (curr[1] - curr[-1]) + (prev[1] - prev[-1]) + (next[1] - next[-1]);
2293 int h = 2 * (prev[0] - next[0]) + (prev[1] - next[1]) + (prev[-1] - next[-1]);
2294 h = (h < 0 ? -h : h);
2295 v = (v < 0 ? -v : v);
2296 if (h > EDGE_THRESH || v > EDGE_THRESH)
2302 curr += frame->y_stride - frame->y_width + 2;
2303 prev += frame->y_stride - frame->y_width + 2;
2304 next += frame->y_stride - frame->y_width + 2;
2306 return (double)num_edge_pels / num_pels;
2309 // Function to test for conditions that indicate we should loop
2310 // back and recode a frame.
2311 static int recode_loop_test(VP9_COMP *cpi,
2312 int high_limit, int low_limit,
2313 int q, int maxq, int minq) {
2314 int force_recode = 0;
2315 VP9_COMMON *cm = &cpi->common;
2317 // Is frame recode allowed at all
2318 // Yes if either recode mode 1 is selected or mode two is selected
2319 // and the frame is a key frame. golden frame or alt_ref_frame
2320 if ((cpi->sf.recode_loop == 1) ||
2321 ((cpi->sf.recode_loop == 2) &&
2322 ((cm->frame_type == KEY_FRAME) ||
2323 cpi->refresh_golden_frame ||
2324 cpi->refresh_alt_ref_frame))) {
2325 // General over and under shoot tests
2326 if (((cpi->projected_frame_size > high_limit) && (q < maxq)) ||
2327 ((cpi->projected_frame_size < low_limit) && (q > minq))) {
2330 // Special Constrained quality tests
2331 else if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
2332 // Undershoot and below auto cq level
2333 if (q > cpi->cq_target_quality &&
2334 cpi->projected_frame_size < ((cpi->this_frame_target * 7) >> 3)) {
2336 } else if (q > cpi->oxcf.cq_level &&
2337 cpi->projected_frame_size < cpi->min_frame_bandwidth &&
2338 cpi->active_best_quality > cpi->oxcf.cq_level) {
2339 // Severe undershoot and between auto and user cq level
2341 cpi->active_best_quality = cpi->oxcf.cq_level;
2346 return force_recode;
2349 static void update_reference_frames(VP9_COMP * const cpi) {
2350 VP9_COMMON * const cm = &cpi->common;
2352 // At this point the new frame has been encoded.
2353 // If any buffer copy / swapping is signaled it should be done here.
2354 if (cm->frame_type == KEY_FRAME) {
2355 ref_cnt_fb(cm->fb_idx_ref_cnt,
2356 &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
2357 ref_cnt_fb(cm->fb_idx_ref_cnt,
2358 &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
2360 #if CONFIG_MULTIPLE_ARF
2361 else if (!cpi->multi_arf_enabled && cpi->refresh_golden_frame &&
2362 !cpi->refresh_alt_ref_frame) {
2364 else if (cpi->refresh_golden_frame && !cpi->refresh_alt_ref_frame) {
2366 /* Preserve the previously existing golden frame and update the frame in
2367 * the alt ref slot instead. This is highly specific to the current use of
2368 * alt-ref as a forward reference, and this needs to be generalized as
2369 * other uses are implemented (like RTC/temporal scaling)
2371 * The update to the buffer in the alt ref slot was signaled in
2372 * vp9_pack_bitstream(), now swap the buffer pointers so that it's treated
2373 * as the golden frame next time.
2377 ref_cnt_fb(cm->fb_idx_ref_cnt,
2378 &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
2380 tmp = cpi->alt_fb_idx;
2381 cpi->alt_fb_idx = cpi->gld_fb_idx;
2382 cpi->gld_fb_idx = tmp;
2383 } else { /* For non key/golden frames */
2384 if (cpi->refresh_alt_ref_frame) {
2385 int arf_idx = cpi->alt_fb_idx;
2386 #if CONFIG_MULTIPLE_ARF
2387 if (cpi->multi_arf_enabled) {
2388 arf_idx = cpi->arf_buffer_idx[cpi->sequence_number + 1];
2391 ref_cnt_fb(cm->fb_idx_ref_cnt,
2392 &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
2395 if (cpi->refresh_golden_frame) {
2396 ref_cnt_fb(cm->fb_idx_ref_cnt,
2397 &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
2401 if (cpi->refresh_last_frame) {
2402 ref_cnt_fb(cm->fb_idx_ref_cnt,
2403 &cm->ref_frame_map[cpi->lst_fb_idx], cm->new_fb_idx);
2407 static void loopfilter_frame(VP9_COMP *cpi, VP9_COMMON *cm) {
2408 MACROBLOCKD *xd = &cpi->mb.e_mbd;
2410 xd->lf.filter_level = 0;
2412 struct vpx_usec_timer timer;
2414 vp9_clear_system_state();
2416 vpx_usec_timer_start(&timer);
2418 vp9_pick_filter_level(cpi->Source, cpi);
2420 vpx_usec_timer_mark(&timer);
2421 cpi->time_pick_lpf += vpx_usec_timer_elapsed(&timer);
2424 if (xd->lf.filter_level > 0) {
2425 vp9_set_alt_lf_level(cpi, xd->lf.filter_level);
2426 vp9_loop_filter_frame(cm, xd, xd->lf.filter_level, 0);
2429 vp9_extend_frame_inner_borders(cm->frame_to_show,
2430 cm->subsampling_x, cm->subsampling_y);
2433 static void scale_references(VP9_COMP *cpi) {
2434 VP9_COMMON *cm = &cpi->common;
2437 for (i = 0; i < 3; i++) {
2438 YV12_BUFFER_CONFIG *ref = &cm->yv12_fb[cm->ref_frame_map[i]];
2440 if (ref->y_crop_width != cm->width ||
2441 ref->y_crop_height != cm->height) {
2442 int new_fb = get_free_fb(cm);
2444 vp9_realloc_frame_buffer(&cm->yv12_fb[new_fb],
2445 cm->width, cm->height,
2446 cm->subsampling_x, cm->subsampling_y,
2448 scale_and_extend_frame(ref, &cm->yv12_fb[new_fb]);
2449 cpi->scaled_ref_idx[i] = new_fb;
2451 cpi->scaled_ref_idx[i] = cm->ref_frame_map[i];
2452 cm->fb_idx_ref_cnt[cm->ref_frame_map[i]]++;
2457 static void release_scaled_references(VP9_COMP *cpi) {
2458 VP9_COMMON *cm = &cpi->common;
2461 for (i = 0; i < 3; i++)
2462 cm->fb_idx_ref_cnt[cpi->scaled_ref_idx[i]]--;
2465 static void full_to_model_count(unsigned int *model_count,
2466 unsigned int *full_count) {
2468 model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
2469 model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
2470 model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
2471 for (n = THREE_TOKEN; n < DCT_EOB_TOKEN; ++n)
2472 model_count[TWO_TOKEN] += full_count[n];
2473 model_count[DCT_EOB_MODEL_TOKEN] = full_count[DCT_EOB_TOKEN];
2476 static void full_to_model_counts(
2477 vp9_coeff_count_model *model_count, vp9_coeff_count *full_count) {
2479 for (i = 0; i < BLOCK_TYPES; ++i)
2480 for (j = 0; j < REF_TYPES; ++j)
2481 for (k = 0; k < COEF_BANDS; ++k)
2482 for (l = 0; l < PREV_COEF_CONTEXTS; ++l) {
2483 if (l >= 3 && k == 0)
2485 full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
2490 static void encode_frame_to_data_rate(VP9_COMP *cpi,
2491 unsigned long *size,
2492 unsigned char *dest,
2493 unsigned int *frame_flags) {
2494 VP9_COMMON *cm = &cpi->common;
2495 MACROBLOCKD *xd = &cpi->mb.e_mbd;
2498 int frame_over_shoot_limit;
2499 int frame_under_shoot_limit;
2509 int active_worst_qchanged = 0;
2511 int overshoot_seen = 0;
2512 int undershoot_seen = 0;
2514 SPEED_FEATURES *sf = &cpi->sf;
2515 unsigned int max_mv_def = MIN(cpi->common.width, cpi->common.height);
2516 #if RESET_FOREACH_FILTER
2520 int active_best_quality0;
2521 int active_worst_quality0;
2522 double rate_correction_factor0;
2523 double gf_rate_correction_factor0;
2526 /* list of filters to search over */
2527 int mcomp_filters_to_search[] = {
2528 EIGHTTAP, EIGHTTAP_SHARP, EIGHTTAP_SMOOTH, SWITCHABLE
2530 int mcomp_filters = sizeof(mcomp_filters_to_search) /
2531 sizeof(*mcomp_filters_to_search);
2532 int mcomp_filter_index = 0;
2533 int64_t mcomp_filter_cost[4];
2535 /* Scale the source buffer, if required */
2536 if (cm->mi_cols * 8 != cpi->un_scaled_source->y_width ||
2537 cm->mi_rows * 8 != cpi->un_scaled_source->y_height) {
2538 scale_and_extend_frame(cpi->un_scaled_source, &cpi->scaled_source);
2539 cpi->Source = &cpi->scaled_source;
2541 cpi->Source = cpi->un_scaled_source;
2544 scale_references(cpi);
2546 // Clear down mmx registers to allow floating point in what follows
2547 vp9_clear_system_state();
2550 // For an alt ref frame in 2 pass we skip the call to the second
2551 // pass function that sets the target bandwidth so must set it here
2552 if (cpi->refresh_alt_ref_frame) {
2553 // Per frame bit target for the alt ref frame
2554 cpi->per_frame_bandwidth = cpi->twopass.gf_bits;
2555 // per second target bitrate
2556 cpi->target_bandwidth = (int)(cpi->twopass.gf_bits *
2557 cpi->output_framerate);
2560 // Clear zbin over-quant value and mode boost values.
2561 cpi->zbin_mode_boost = 0;
2563 // Enable or disable mode based tweaking of the zbin
2564 // For 2 Pass Only used where GF/ARF prediction quality
2565 // is above a threshold
2566 cpi->zbin_mode_boost = 0;
2568 // if (cpi->oxcf.lossless)
2569 cpi->zbin_mode_boost_enabled = 0;
2571 // cpi->zbin_mode_boost_enabled = 1;
2573 // Current default encoder behaviour for the altref sign bias
2574 cpi->common.ref_frame_sign_bias[ALTREF_FRAME] = cpi->source_alt_ref_active;
2576 // Check to see if a key frame is signaled
2577 // For two pass with auto key frame enabled cm->frame_type may already be set, but not for one pass.
2578 if ((cm->current_video_frame == 0) ||
2579 (cm->frame_flags & FRAMEFLAGS_KEY) ||
2580 (cpi->oxcf.auto_key && (cpi->frames_since_key % cpi->key_frame_frequency == 0))) {
2581 // Key frame from VFW/auto-keyframe/first frame
2582 cm->frame_type = KEY_FRAME;
2585 // Set default state for segment based loop filter update flags
2586 xd->lf.mode_ref_delta_update = 0;
2588 // Initialize cpi->mv_step_param to default based on max resolution
2589 cpi->mv_step_param = vp9_init_search_range(cpi, max_mv_def);
2590 // Initialize cpi->max_mv_magnitude and cpi->mv_step_param if appropriate.
2591 if (sf->auto_mv_step_size) {
2592 if ((cpi->common.frame_type == KEY_FRAME) || cpi->common.intra_only) {
2593 // initialize max_mv_magnitude for use in the first INTER frame
2594 // after a key/intra-only frame
2595 cpi->max_mv_magnitude = max_mv_def;
2598 // allow mv_steps to correspond to twice the max mv magnitude found
2599 // in the previous frame, capped by the default max_mv_magnitude based
2601 cpi->mv_step_param = vp9_init_search_range(
2602 cpi, MIN(max_mv_def, 2 * cpi->max_mv_magnitude));
2603 cpi->max_mv_magnitude = 0;
2607 // Set various flags etc to special state if it is a key frame
2608 if (cm->frame_type == KEY_FRAME) {
2611 // Reset the loop filter deltas and segmentation map
2612 setup_features(cpi);
2614 // If segmentation is enabled force a map update for key frames
2615 if (xd->seg.enabled) {
2616 xd->seg.update_map = 1;
2617 xd->seg.update_data = 1;
2620 // The alternate reference frame cannot be active for a key frame
2621 cpi->source_alt_ref_active = 0;
2623 // Reset the RD threshold multipliers to default of * 1 (128)
2624 for (i = 0; i < MAX_MODES; i++)
2625 cpi->rd_thresh_mult[i] = 128;
2627 cm->error_resilient_mode = (cpi->oxcf.error_resilient_mode != 0);
2628 cm->frame_parallel_decoding_mode =
2629 (cpi->oxcf.frame_parallel_decoding_mode != 0);
2630 if (cm->error_resilient_mode) {
2631 cm->frame_parallel_decoding_mode = 1;
2632 cm->reset_frame_context = 0;
2633 cm->refresh_frame_context = 0;
2637 // Configure experimental use of segmentation for enhanced coding of
2638 // static regions if indicated.
2639 // Only allowed for now in second pass of two pass (as requires lagged coding)
2640 // and if the relevant speed feature flag is set.
2641 if ((cpi->pass == 2) && (cpi->sf.static_segmentation)) {
2642 configure_static_seg_features(cpi);
2645 // Decide how big to make the frame
2646 vp9_pick_frame_size(cpi);
2648 vp9_clear_system_state();
2650 // Set an active best quality and if necessary active worst quality
2651 q = cpi->active_worst_quality;
2653 if (cm->frame_type == KEY_FRAME) {
2654 #if !CONFIG_MULTIPLE_ARF
2655 // Special case for key frames forced because we have reached
2656 // the maximum key frame interval. Here force the Q to a range
2657 // based on the ambient Q to reduce the risk of popping
2658 if (cpi->this_key_frame_forced) {
2660 int qindex = cpi->last_boosted_qindex;
2661 double last_boosted_q = vp9_convert_qindex_to_q(qindex);
2663 delta_qindex = compute_qdelta(cpi, last_boosted_q,
2664 (last_boosted_q * 0.75));
2666 cpi->active_best_quality = MAX(qindex + delta_qindex, cpi->best_quality);
2670 double q_adj_factor = 1.0;
2673 // Baseline value derived from cpi->active_worst_quality and kf boost
2674 if (cpi->kf_boost > high) {
2675 cpi->active_best_quality = kf_low_motion_minq[q];
2676 } else if (cpi->kf_boost < low) {
2677 cpi->active_best_quality = kf_high_motion_minq[q];
2679 const int gap = high - low;
2680 const int offset = high - cpi->kf_boost;
2681 const int qdiff = kf_high_motion_minq[q] - kf_low_motion_minq[q];
2682 const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
2684 cpi->active_best_quality = kf_low_motion_minq[q] + adjustment;
2688 // Allow somewhat lower kf minq with small image formats.
2689 if ((cm->width * cm->height) <= (352 * 288)) {
2690 q_adj_factor -= 0.25;
2693 // Make a further adjustment based on the kf zero motion measure.
2694 q_adj_factor += 0.05 - (0.001 * (double)cpi->kf_zeromotion_pct);
2696 // Convert the adjustment factor to a qindex delta on active_best_quality.
2697 q_val = vp9_convert_qindex_to_q(cpi->active_best_quality);
2698 cpi->active_best_quality +=
2699 compute_qdelta(cpi, q_val, (q_val * q_adj_factor));
2704 // Force the KF quantizer to be 30% of the active_worst_quality.
2705 current_q = vp9_convert_qindex_to_q(cpi->active_worst_quality);
2706 cpi->active_best_quality = cpi->active_worst_quality
2707 + compute_qdelta(cpi, current_q, current_q * 0.3);
2709 } else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) {
2713 // Use the lower of cpi->active_worst_quality and recent
2714 // average Q as basis for GF/ARF Q limit unless last frame was
2716 if (cpi->frames_since_key > 1 &&
2717 cpi->avg_frame_qindex < cpi->active_worst_quality) {
2718 q = cpi->avg_frame_qindex;
2721 // For constrained quality dont allow Q less than the cq level
2722 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY &&
2723 q < cpi->cq_target_quality) {
2724 q = cpi->cq_target_quality;
2727 if (cpi->gfu_boost > high) {
2728 cpi->active_best_quality = gf_low_motion_minq[q];
2729 } else if (cpi->gfu_boost < low) {
2730 cpi->active_best_quality = gf_high_motion_minq[q];
2732 const int gap = high - low;
2733 const int offset = high - cpi->gfu_boost;
2734 const int qdiff = gf_high_motion_minq[q] - gf_low_motion_minq[q];
2735 const int adjustment = ((offset * qdiff) + (gap >> 1)) / gap;
2737 cpi->active_best_quality = gf_low_motion_minq[q] + adjustment;
2740 // Constrained quality use slightly lower active best.
2741 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY)
2742 cpi->active_best_quality = cpi->active_best_quality * 15 / 16;
2744 #ifdef ONE_SHOT_Q_ESTIMATE
2745 #ifdef STRICT_ONE_SHOT_Q
2746 cpi->active_best_quality = q;
2748 cpi->active_best_quality = inter_minq[q];
2751 cpi->active_best_quality = inter_minq[q];
2754 // For the constant/constrained quality mode we don't want
2755 // q to fall below the cq level.
2756 if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
2757 (cpi->active_best_quality < cpi->cq_target_quality)) {
2758 // If we are strongly undershooting the target rate in the last
2759 // frames then use the user passed in cq value not the auto
2761 if (cpi->rolling_actual_bits < cpi->min_frame_bandwidth)
2762 cpi->active_best_quality = cpi->oxcf.cq_level;
2764 cpi->active_best_quality = cpi->cq_target_quality;
2768 // Clip the active best and worst quality values to limits
2769 if (cpi->active_worst_quality > cpi->worst_quality)
2770 cpi->active_worst_quality = cpi->worst_quality;
2772 if (cpi->active_best_quality < cpi->best_quality)
2773 cpi->active_best_quality = cpi->best_quality;
2775 if (cpi->active_best_quality > cpi->worst_quality)
2776 cpi->active_best_quality = cpi->worst_quality;
2778 if (cpi->active_worst_quality < cpi->active_best_quality)
2779 cpi->active_worst_quality = cpi->active_best_quality;
2781 // Special case code to try and match quality with forced key frames
2782 if ((cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced) {
2783 q = cpi->last_boosted_qindex;
2785 // Determine initial Q to try
2786 q = vp9_regulate_q(cpi, cpi->this_frame_target);
2789 vp9_compute_frame_size_bounds(cpi, &frame_under_shoot_limit,
2790 &frame_over_shoot_limit);
2792 #if CONFIG_MULTIPLE_ARF
2793 // Force the quantizer determined by the coding order pattern.
2794 if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME)) {
2796 double current_q = vp9_convert_qindex_to_q(cpi->active_worst_quality);
2797 int level = cpi->this_frame_weight;
2800 // Set quantizer steps at 10% increments.
2801 new_q = current_q * (1.0 - (0.2 * (cpi->max_arf_level - level)));
2802 q = cpi->active_worst_quality + compute_qdelta(cpi, current_q, new_q);
2809 printf("frame:%d q:%d\n", cm->current_video_frame, q);
2812 // Limit Q range for the adaptive loop.
2813 bottom_index = cpi->active_best_quality;
2814 top_index = cpi->active_worst_quality;
2815 q_low = cpi->active_best_quality;
2816 q_high = cpi->active_worst_quality;
2817 #if CONFIG_MULTIPLE_ARF
2821 vpx_memset(cpi->rd_tx_select_threshes, 0, sizeof(cpi->rd_tx_select_threshes));
2823 if (cm->frame_type != KEY_FRAME) {
2824 /* TODO: Decide this more intelligently */
2825 if (sf->search_best_filter) {
2826 cm->mcomp_filter_type = mcomp_filters_to_search[0];
2827 mcomp_filter_index = 0;
2829 cm->mcomp_filter_type = DEFAULT_INTERP_FILTER;
2831 /* TODO: Decide this more intelligently */
2832 xd->allow_high_precision_mv = q < HIGH_PRECISION_MV_QTHRESH;
2833 set_mvcost(&cpi->mb);
2838 if (cpi->oxcf.noise_sensitivity > 0) {
2841 switch (cpi->oxcf.noise_sensitivity) {
2860 vp9_denoise(cpi->Source, cpi->Source, l);
2865 #ifdef OUTPUT_YUV_SRC
2866 vp9_write_yuv_frame(cpi->Source);
2869 #if RESET_FOREACH_FILTER
2870 if (sf->search_best_filter) {
2874 rate_correction_factor0 = cpi->rate_correction_factor;
2875 gf_rate_correction_factor0 = cpi->gf_rate_correction_factor;
2876 active_best_quality0 = cpi->active_best_quality;
2877 active_worst_quality0 = cpi->active_worst_quality;
2881 vp9_clear_system_state(); // __asm emms;
2883 vp9_set_quantizer(cpi, q);
2885 if (loop_count == 0) {
2887 // Set up entropy depending on frame type.
2888 if (cm->frame_type == KEY_FRAME) {
2889 /* Choose which entropy context to use. When using a forward reference
2890 * frame, it immediately follows the keyframe, and thus benefits from
2891 * using the same entropy context established by the keyframe.
2892 * Otherwise, use the default context 0.
2894 cm->frame_context_idx = cpi->oxcf.play_alternate;
2895 vp9_setup_key_frame(cpi);
2897 /* Choose which entropy context to use. Currently there are only two
2898 * contexts used, one for normal frames and one for alt ref frames.
2900 cpi->common.frame_context_idx = cpi->refresh_alt_ref_frame;
2901 vp9_setup_inter_frame(cpi);
2905 // transform / motion compensation build reconstruction frame
2907 vp9_encode_frame(cpi);
2909 // Update the skip mb flag probabilities based on the distribution
2910 // seen in the last encoder iteration.
2911 // update_base_skip_probs(cpi);
2913 vp9_clear_system_state(); // __asm emms;
2915 // Dummy pack of the bitstream using up to date stats to get an
2916 // accurate estimate of output frame size to determine if we need
2918 vp9_save_coding_context(cpi);
2919 cpi->dummy_packing = 1;
2920 vp9_pack_bitstream(cpi, dest, size);
2921 cpi->projected_frame_size = (*size) << 3;
2922 vp9_restore_coding_context(cpi);
2924 if (frame_over_shoot_limit == 0)
2925 frame_over_shoot_limit = 1;
2926 active_worst_qchanged = 0;
2928 // Special case handling for forced key frames
2929 if ((cm->frame_type == KEY_FRAME) && cpi->this_key_frame_forced) {
2931 int kf_err = vp9_calc_ss_err(cpi->Source,
2932 &cm->yv12_fb[cm->new_fb_idx]);
2934 int high_err_target = cpi->ambient_err;
2935 int low_err_target = cpi->ambient_err >> 1;
2937 // Prevent possible divide by zero error below for perfect KF
2940 // The key frame is not good enough or we can afford
2941 // to make it better without undue risk of popping.
2942 if ((kf_err > high_err_target &&
2943 cpi->projected_frame_size <= frame_over_shoot_limit) ||
2944 (kf_err > low_err_target &&
2945 cpi->projected_frame_size <= frame_under_shoot_limit)) {
2947 q_high = q > q_low ? q - 1 : q_low;
2950 q = (q * high_err_target) / kf_err;
2951 q = MIN(q, (q_high + q_low) >> 1);
2952 } else if (kf_err < low_err_target &&
2953 cpi->projected_frame_size >= frame_under_shoot_limit) {
2954 // The key frame is much better than the previous frame
2956 q_low = q < q_high ? q + 1 : q_high;
2959 q = (q * low_err_target) / kf_err;
2960 q = MIN(q, (q_high + q_low + 1) >> 1);
2963 // Clamp Q to upper and lower limits:
2964 q = clamp(q, q_low, q_high);
2969 // Is the projected frame size out of range and are we allowed to attempt to recode.
2970 else if (recode_loop_test(cpi,
2971 frame_over_shoot_limit, frame_under_shoot_limit,
2972 q, top_index, bottom_index)) {
2976 // Frame size out of permitted range:
2977 // Update correction factor & compute new Q to try...
2979 // Frame is too large
2980 if (cpi->projected_frame_size > cpi->this_frame_target) {
2981 // Raise Qlow as to at least the current value
2982 q_low = q < q_high ? q + 1 : q_high;
2984 if (undershoot_seen || loop_count > 1) {
2985 // Update rate_correction_factor unless cpi->active_worst_quality
2987 if (!active_worst_qchanged)
2988 vp9_update_rate_correction_factors(cpi, 1);
2990 q = (q_high + q_low + 1) / 2;
2992 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
2993 if (!active_worst_qchanged)
2994 vp9_update_rate_correction_factors(cpi, 0);
2996 q = vp9_regulate_q(cpi, cpi->this_frame_target);
2998 while (q < q_low && retries < 10) {
2999 vp9_update_rate_correction_factors(cpi, 0);
3000 q = vp9_regulate_q(cpi, cpi->this_frame_target);
3007 // Frame is too small
3008 q_high = q > q_low ? q - 1 : q_low;
3010 if (overshoot_seen || loop_count > 1) {
3011 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
3012 if (!active_worst_qchanged)
3013 vp9_update_rate_correction_factors(cpi, 1);
3015 q = (q_high + q_low) / 2;
3017 // Update rate_correction_factor unless cpi->active_worst_quality has changed.
3018 if (!active_worst_qchanged)
3019 vp9_update_rate_correction_factors(cpi, 0);
3021 q = vp9_regulate_q(cpi, cpi->this_frame_target);
3023 // Special case reset for qlow for constrained quality.
3024 // This should only trigger where there is very substantial
3025 // undershoot on a frame and the auto cq level is above
3026 // the user passsed in value.
3027 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY && q < q_low) {
3031 while (q > q_high && retries < 10) {
3032 vp9_update_rate_correction_factors(cpi, 0);
3033 q = vp9_regulate_q(cpi, cpi->this_frame_target);
3038 undershoot_seen = 1;
3041 // Clamp Q to upper and lower limits:
3042 q = clamp(q, q_low, q_high);
3049 if (cpi->is_src_frame_alt_ref)
3052 if (!loop && cm->frame_type != KEY_FRAME && sf->search_best_filter) {
3053 if (mcomp_filter_index < mcomp_filters) {
3054 int64_t err = vp9_calc_ss_err(cpi->Source,
3055 &cm->yv12_fb[cm->new_fb_idx]);
3056 int64_t rate = cpi->projected_frame_size << 8;
3057 mcomp_filter_cost[mcomp_filter_index] =
3058 (RDCOST(cpi->RDMULT, cpi->RDDIV, rate, err));
3059 mcomp_filter_index++;
3060 if (mcomp_filter_index < mcomp_filters) {
3061 cm->mcomp_filter_type = mcomp_filters_to_search[mcomp_filter_index];
3066 int64_t best_cost = mcomp_filter_cost[0];
3067 int mcomp_best_filter = mcomp_filters_to_search[0];
3068 for (f = 1; f < mcomp_filters; f++) {
3069 if (mcomp_filter_cost[f] < best_cost) {
3070 mcomp_best_filter = mcomp_filters_to_search[f];
3071 best_cost = mcomp_filter_cost[f];
3074 if (mcomp_best_filter != mcomp_filters_to_search[mcomp_filters - 1]) {
3077 cm->mcomp_filter_type = mcomp_best_filter;
3080 printf(" best filter = %d, ( ", mcomp_best_filter);
3081 for (f=0;f<mcomp_filters; f++) printf("%d ", mcomp_filter_cost[f]);
3085 #if RESET_FOREACH_FILTER
3088 undershoot_seen = 0;
3092 cpi->rate_correction_factor = rate_correction_factor0;
3093 cpi->gf_rate_correction_factor = gf_rate_correction_factor0;
3094 cpi->active_best_quality = active_best_quality0;
3095 cpi->active_worst_quality = active_worst_quality0;
3104 #if CONFIG_INTERNAL_STATS
3105 cpi->tot_recode_hits++;
3110 // Special case code to reduce pulsing when key frames are forced at a
3111 // fixed interval. Note the reconstruction error if it is the frame before
3112 // the force key frame
3113 if (cpi->next_key_frame_forced && (cpi->twopass.frames_to_key == 0)) {
3114 cpi->ambient_err = vp9_calc_ss_err(cpi->Source,
3115 &cm->yv12_fb[cm->new_fb_idx]);
3118 if (cm->frame_type == KEY_FRAME)
3119 cpi->refresh_last_frame = 1;
3121 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
3123 #if WRITE_RECON_BUFFER
3125 write_cx_frame_to_file(cm->frame_to_show,
3126 cm->current_video_frame);
3128 write_cx_frame_to_file(cm->frame_to_show,
3129 cm->current_video_frame + 1000);
3132 // Pick the loop filter level for the frame.
3133 loopfilter_frame(cpi, cm);
3135 #if WRITE_RECON_BUFFER
3137 write_cx_frame_to_file(cm->frame_to_show,
3138 cm->current_video_frame + 2000);
3140 write_cx_frame_to_file(cm->frame_to_show,
3141 cm->current_video_frame + 3000);
3144 // build the bitstream
3145 cpi->dummy_packing = 0;
3146 vp9_pack_bitstream(cpi, dest, size);
3148 if (xd->seg.update_map)
3149 update_reference_segmentation_map(cpi);
3151 release_scaled_references(cpi);
3152 update_reference_frames(cpi);
3154 for (t = TX_4X4; t <= TX_32X32; t++)
3155 full_to_model_counts(cpi->common.fc.coef_counts[t],
3156 cpi->coef_counts[t]);
3157 if (!cpi->common.error_resilient_mode &&
3158 !cpi->common.frame_parallel_decoding_mode) {
3159 vp9_adapt_coef_probs(&cpi->common);
3162 if (cpi->common.frame_type != KEY_FRAME) {
3163 vp9_copy(cpi->common.fc.y_mode_counts, cpi->y_mode_count);
3164 vp9_copy(cpi->common.fc.uv_mode_counts, cpi->y_uv_mode_count);
3165 vp9_copy(cpi->common.fc.partition_counts, cpi->partition_count);
3166 vp9_copy(cm->fc.intra_inter_count, cpi->intra_inter_count);
3167 vp9_copy(cm->fc.comp_inter_count, cpi->comp_inter_count);
3168 vp9_copy(cm->fc.single_ref_count, cpi->single_ref_count);
3169 vp9_copy(cm->fc.comp_ref_count, cpi->comp_ref_count);
3170 cpi->common.fc.NMVcount = cpi->NMVcount;
3171 if (!cpi->common.error_resilient_mode &&
3172 !cpi->common.frame_parallel_decoding_mode) {
3173 vp9_adapt_mode_probs(&cpi->common);
3174 vp9_adapt_mode_context(&cpi->common);
3175 vp9_adapt_mv_probs(&cpi->common, cpi->mb.e_mbd.allow_high_precision_mv);
3179 #ifdef ENTROPY_STATS
3180 vp9_update_mode_context_stats(cpi);
3183 /* Move storing frame_type out of the above loop since it is also
3184 * needed in motion search besides loopfilter */
3185 cm->last_frame_type = cm->frame_type;
3187 // Update rate control heuristics
3188 cpi->total_byte_count += (*size);
3189 cpi->projected_frame_size = (*size) << 3;
3191 if (!active_worst_qchanged)
3192 vp9_update_rate_correction_factors(cpi, 2);
3194 cpi->last_q[cm->frame_type] = cm->base_qindex;
3196 // Keep record of last boosted (KF/KF/ARF) Q value.
3197 // If the current frame is coded at a lower Q then we also update it.
3198 // If all mbs in this group are skipped only update if the Q value is
3199 // better than that already stored.
3200 // This is used to help set quality in forced key frames to reduce popping
3201 if ((cm->base_qindex < cpi->last_boosted_qindex) ||
3202 ((cpi->static_mb_pct < 100) &&
3203 ((cm->frame_type == KEY_FRAME) ||
3204 cpi->refresh_alt_ref_frame ||
3205 (cpi->refresh_golden_frame && !cpi->is_src_frame_alt_ref)))) {
3206 cpi->last_boosted_qindex = cm->base_qindex;
3209 if (cm->frame_type == KEY_FRAME) {
3210 vp9_adjust_key_frame_context(cpi);
3213 // Keep a record of ambient average Q.
3214 if (cm->frame_type != KEY_FRAME)
3215 cpi->avg_frame_qindex = (2 + 3 * cpi->avg_frame_qindex + cm->base_qindex) >> 2;
3217 // Keep a record from which we can calculate the average Q excluding GF updates and key frames
3218 if (cm->frame_type != KEY_FRAME &&
3219 !cpi->refresh_golden_frame &&
3220 !cpi->refresh_alt_ref_frame) {
3222 cpi->tot_q += vp9_convert_qindex_to_q(q);
3223 cpi->avg_q = cpi->tot_q / (double)cpi->ni_frames;
3225 // Calculate the average Q for normal inter frames (not key or GFU frames).
3226 cpi->ni_tot_qi += q;
3227 cpi->ni_av_qi = cpi->ni_tot_qi / cpi->ni_frames;
3230 // Update the buffer level variable.
3231 // Non-viewable frames are a special case and are treated as pure overhead.
3232 if (!cm->show_frame)
3233 cpi->bits_off_target -= cpi->projected_frame_size;
3235 cpi->bits_off_target += cpi->av_per_frame_bandwidth - cpi->projected_frame_size;
3237 // Clip the buffer level at the maximum buffer size
3238 if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size)
3239 cpi->bits_off_target = cpi->oxcf.maximum_buffer_size;
3241 // Rolling monitors of whether we are over or underspending used to help
3242 // regulate min and Max Q in two pass.
3243 if (cm->frame_type != KEY_FRAME) {
3244 cpi->rolling_target_bits =
3245 ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4;
3246 cpi->rolling_actual_bits =
3247 ((cpi->rolling_actual_bits * 3) + cpi->projected_frame_size + 2) / 4;
3248 cpi->long_rolling_target_bits =
3249 ((cpi->long_rolling_target_bits * 31) + cpi->this_frame_target + 16) / 32;
3250 cpi->long_rolling_actual_bits =
3251 ((cpi->long_rolling_actual_bits * 31) +
3252 cpi->projected_frame_size + 16) / 32;
3255 // Actual bits spent
3256 cpi->total_actual_bits += cpi->projected_frame_size;
3259 cpi->total_target_vs_actual += (cpi->this_frame_target - cpi->projected_frame_size);
3261 cpi->buffer_level = cpi->bits_off_target;
3263 // Update bits left to the kf and gf groups to account for overshoot or undershoot on these frames
3264 if (cm->frame_type == KEY_FRAME) {
3265 cpi->twopass.kf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
3267 cpi->twopass.kf_group_bits = MAX(cpi->twopass.kf_group_bits, 0);
3268 } else if (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame) {
3269 cpi->twopass.gf_group_bits += cpi->this_frame_target - cpi->projected_frame_size;
3271 cpi->twopass.gf_group_bits = MAX(cpi->twopass.gf_group_bits, 0);
3274 // Update the skip mb flag probabilities based on the distribution seen
3276 // update_base_skip_probs(cpi);
3278 #if 0 && CONFIG_INTERNAL_STATS
3280 FILE *f = fopen("tmp.stt", "a");
3283 vp9_clear_system_state(); // __asm emms;
3285 recon_err = vp9_calc_ss_err(cpi->Source,
3286 &cm->yv12_fb[cm->new_fb_idx]);
3288 if (cpi->twopass.total_left_stats.coded_error != 0.0)
3289 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d"
3290 "%7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f"
3291 "%6d %6d %5d %5d %5d %8.2f %10d %10.3f"
3292 "%10.3f %8d %10d %10d %10d\n",
3293 cpi->common.current_video_frame, cpi->this_frame_target,
3294 cpi->projected_frame_size, 0, //loop_size_estimate,
3295 (cpi->projected_frame_size - cpi->this_frame_target),
3296 (int)cpi->total_target_vs_actual,
3297 (int)(cpi->oxcf.starting_buffer_level - cpi->bits_off_target),
3298 (int)cpi->total_actual_bits,
3299 vp9_convert_qindex_to_q(cm->base_qindex),
3300 (double)vp9_dc_quant(cm->base_qindex, 0) / 4.0,
3301 vp9_convert_qindex_to_q(cpi->active_best_quality),
3302 vp9_convert_qindex_to_q(cpi->active_worst_quality),
3304 vp9_convert_qindex_to_q(cpi->ni_av_qi),
3305 vp9_convert_qindex_to_q(cpi->cq_target_quality),
3306 cpi->refresh_last_frame,
3307 cpi->refresh_golden_frame, cpi->refresh_alt_ref_frame,
3308 cm->frame_type, cpi->gfu_boost,
3309 cpi->twopass.est_max_qcorrection_factor,
3310 (int)cpi->twopass.bits_left,
3311 cpi->twopass.total_left_stats.coded_error,
3312 (double)cpi->twopass.bits_left /
3313 cpi->twopass.total_left_stats.coded_error,
3314 cpi->tot_recode_hits, recon_err, cpi->kf_boost,
3315 cpi->kf_zeromotion_pct);
3317 fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d"
3318 "%7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f"
3319 "%5d %5d %5d %8d %8d %8.2f %10d %10.3f"
3320 "%8d %10d %10d %10d\n",
3321 cpi->common.current_video_frame,
3322 cpi->this_frame_target, cpi->projected_frame_size,
3323 0, //loop_size_estimate,
3324 (cpi->projected_frame_size - cpi->this_frame_target),
3325 (int)cpi->total_target_vs_actual,
3326 (int)(cpi->oxcf.starting_buffer_level - cpi->bits_off_target),
3327 (int)cpi->total_actual_bits,
3328 vp9_convert_qindex_to_q(cm->base_qindex),
3329 (double)vp9_dc_quant(cm->base_qindex, 0) / 4.0,
3330 vp9_convert_qindex_to_q(cpi->active_best_quality),
3331 vp9_convert_qindex_to_q(cpi->active_worst_quality),
3333 vp9_convert_qindex_to_q(cpi->ni_av_qi),
3334 vp9_convert_qindex_to_q(cpi->cq_target_quality),
3335 cpi->refresh_last_frame,
3336 cpi->refresh_golden_frame, cpi->refresh_alt_ref_frame,
3337 cm->frame_type, cpi->gfu_boost,
3338 cpi->twopass.est_max_qcorrection_factor,
3339 (int)cpi->twopass.bits_left,
3340 cpi->twopass.total_left_stats.coded_error,
3341 cpi->tot_recode_hits, recon_err, cpi->kf_boost,
3342 cpi->kf_zeromotion_pct);
3347 FILE *fmodes = fopen("Modes.stt", "a");
3350 fprintf(fmodes, "%6d:%1d:%1d:%1d ",
3351 cpi->common.current_video_frame,
3352 cm->frame_type, cpi->refresh_golden_frame,
3353 cpi->refresh_alt_ref_frame);
3355 for (i = 0; i < MAX_MODES; i++)
3356 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
3358 fprintf(fmodes, "\n");
3367 // Debug stats for segment feature experiments.
3371 // If this was a kf or Gf note the Q
3372 if ((cm->frame_type == KEY_FRAME)
3373 || cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)
3374 cm->last_kf_gf_q = cm->base_qindex;
3376 if (cpi->refresh_golden_frame == 1)
3377 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_GOLDEN;
3379 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_GOLDEN;
3381 if (cpi->refresh_alt_ref_frame == 1)
3382 cm->frame_flags = cm->frame_flags | FRAMEFLAGS_ALTREF;
3384 cm->frame_flags = cm->frame_flags&~FRAMEFLAGS_ALTREF;
3387 if (cpi->refresh_last_frame & cpi->refresh_golden_frame)
3388 cpi->gold_is_last = 1;
3389 else if (cpi->refresh_last_frame ^ cpi->refresh_golden_frame)
3390 cpi->gold_is_last = 0;
3392 if (cpi->refresh_last_frame & cpi->refresh_alt_ref_frame)
3393 cpi->alt_is_last = 1;
3394 else if (cpi->refresh_last_frame ^ cpi->refresh_alt_ref_frame)
3395 cpi->alt_is_last = 0;
3397 if (cpi->refresh_alt_ref_frame & cpi->refresh_golden_frame)
3398 cpi->gold_is_alt = 1;
3399 else if (cpi->refresh_alt_ref_frame ^ cpi->refresh_golden_frame)
3400 cpi->gold_is_alt = 0;
3402 cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
3404 if (cpi->gold_is_last)
3405 cpi->ref_frame_flags &= ~VP9_GOLD_FLAG;
3407 if (cpi->alt_is_last)
3408 cpi->ref_frame_flags &= ~VP9_ALT_FLAG;
3410 if (cpi->gold_is_alt)
3411 cpi->ref_frame_flags &= ~VP9_ALT_FLAG;
3413 if (cpi->oxcf.play_alternate && cpi->refresh_alt_ref_frame
3414 && (cm->frame_type != KEY_FRAME))
3415 // Update the alternate reference frame stats as appropriate.
3416 update_alt_ref_frame_stats(cpi);
3418 // Update the Golden frame stats as appropriate.
3419 update_golden_frame_stats(cpi);
3421 if (cm->frame_type == KEY_FRAME) {
3422 // Tell the caller that the frame was coded as a key frame
3423 *frame_flags = cm->frame_flags | FRAMEFLAGS_KEY;
3425 #if CONFIG_MULTIPLE_ARF
3426 // Reset the sequence number.
3427 if (cpi->multi_arf_enabled) {
3428 cpi->sequence_number = 0;
3429 cpi->frame_coding_order_period = cpi->new_frame_coding_order_period;
3430 cpi->new_frame_coding_order_period = -1;
3434 // As this frame is a key frame the next defaults to an inter frame.
3435 cm->frame_type = INTER_FRAME;
3437 *frame_flags = cm->frame_flags&~FRAMEFLAGS_KEY;
3439 #if CONFIG_MULTIPLE_ARF
3440 /* Increment position in the coded frame sequence. */
3441 if (cpi->multi_arf_enabled) {
3442 ++cpi->sequence_number;
3443 if (cpi->sequence_number >= cpi->frame_coding_order_period) {
3444 cpi->sequence_number = 0;
3445 cpi->frame_coding_order_period = cpi->new_frame_coding_order_period;
3446 cpi->new_frame_coding_order_period = -1;
3448 cpi->this_frame_weight = cpi->arf_weight[cpi->sequence_number];
3449 assert(cpi->this_frame_weight >= 0);
3454 // Clear the one shot update flags for segmentation map and mode/ref loop filter deltas.
3455 xd->seg.update_map = 0;
3456 xd->seg.update_data = 0;
3457 xd->lf.mode_ref_delta_update = 0;
3459 // keep track of the last coded dimensions
3460 cm->last_width = cm->width;
3461 cm->last_height = cm->height;
3463 // reset to normal state now that we are done.
3464 cm->last_show_frame = cm->show_frame;
3465 if (cm->show_frame) {
3466 // current mip will be the prev_mip for the next frame
3467 MODE_INFO *temp = cm->prev_mip;
3468 cm->prev_mip = cm->mip;
3471 // update the upper left visible macroblock ptrs
3472 cm->mi = cm->mip + cm->mode_info_stride + 1;
3474 // Don't increment frame counters if this was an altref buffer
3475 // update not a real frame
3476 ++cm->current_video_frame;
3477 ++cpi->frames_since_key;
3480 cm->prev_mi = cm->prev_mip + cm->mode_info_stride + 1;
3486 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
3487 recon_file = fopen(filename, "wb");
3488 fwrite(cm->yv12_fb[cm->ref_frame_map[cpi->lst_fb_idx]].buffer_alloc,
3489 cm->yv12_fb[cm->ref_frame_map[cpi->lst_fb_idx]].frame_size,
3494 #ifdef OUTPUT_YUV_REC
3495 vp9_write_yuv_rec_frame(cm);
3500 static void Pass2Encode(VP9_COMP *cpi, unsigned long *size,
3501 unsigned char *dest, unsigned int *frame_flags) {
3503 if (!cpi->refresh_alt_ref_frame)
3504 vp9_second_pass(cpi);
3506 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
3507 // vp9_print_modes_and_motion_vectors(&cpi->common, "encode.stt");
3508 #ifdef DISABLE_RC_LONG_TERM_MEM
3509 cpi->twopass.bits_left -= cpi->this_frame_target;
3511 cpi->twopass.bits_left -= 8 * *size;
3514 if (!cpi->refresh_alt_ref_frame) {
3515 double lower_bounds_min_rate = FRAME_OVERHEAD_BITS * cpi->oxcf.framerate;
3516 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
3517 * cpi->oxcf.two_pass_vbrmin_section / 100);
3519 if (two_pass_min_rate < lower_bounds_min_rate)
3520 two_pass_min_rate = lower_bounds_min_rate;
3522 cpi->twopass.bits_left += (int64_t)(two_pass_min_rate / cpi->oxcf.framerate);
3527 int vp9_receive_raw_frame(VP9_PTR ptr, unsigned int frame_flags,
3528 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
3530 VP9_COMP *cpi = (VP9_COMP *) ptr;
3531 VP9_COMMON *cm = &cpi->common;
3532 struct vpx_usec_timer timer;
3535 if (!cpi->initial_width) {
3536 // TODO(jkoleszar): Support 1/4 subsampling?
3537 cm->subsampling_x = sd->uv_width < sd->y_width;
3538 cm->subsampling_y = sd->uv_height < sd->y_height;
3539 alloc_raw_frame_buffers(cpi);
3541 cpi->initial_width = cm->width;
3542 cpi->initial_height = cm->height;
3544 vpx_usec_timer_start(&timer);
3545 if (vp9_lookahead_push(cpi->lookahead, sd, time_stamp, end_time, frame_flags,
3546 cpi->active_map_enabled ? cpi->active_map : NULL))
3548 vpx_usec_timer_mark(&timer);
3549 cpi->time_receive_data += vpx_usec_timer_elapsed(&timer);
3555 static int frame_is_reference(const VP9_COMP *cpi) {
3556 const VP9_COMMON *cm = &cpi->common;
3557 const MACROBLOCKD *mb = &cpi->mb.e_mbd;
3559 return cm->frame_type == KEY_FRAME ||
3560 cpi->refresh_last_frame ||
3561 cpi->refresh_golden_frame ||
3562 cpi->refresh_alt_ref_frame ||
3563 cm->refresh_frame_context ||
3564 mb->lf.mode_ref_delta_update ||
3565 mb->seg.update_map ||
3566 mb->seg.update_data;
3569 #if CONFIG_MULTIPLE_ARF
3570 int is_next_frame_arf(VP9_COMP *cpi) {
3571 // Negative entry in frame_coding_order indicates an ARF at this position.
3572 return cpi->frame_coding_order[cpi->sequence_number + 1] < 0 ? 1 : 0;
3576 int vp9_get_compressed_data(VP9_PTR ptr, unsigned int *frame_flags,
3577 unsigned long *size, unsigned char *dest,
3578 int64_t *time_stamp, int64_t *time_end, int flush) {
3579 VP9_COMP *cpi = (VP9_COMP *) ptr;
3580 VP9_COMMON *cm = &cpi->common;
3581 struct vpx_usec_timer cmptimer;
3582 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
3584 // FILE *fp_out = fopen("enc_frame_type.txt", "a");
3589 vpx_usec_timer_start(&cmptimer);
3593 cpi->mb.e_mbd.allow_high_precision_mv = ALTREF_HIGH_PRECISION_MV;
3594 set_mvcost(&cpi->mb);
3596 // Should we code an alternate reference frame.
3597 if (cpi->oxcf.play_alternate && cpi->source_alt_ref_pending) {
3600 #if CONFIG_MULTIPLE_ARF
3601 assert(!cpi->multi_arf_enabled ||
3602 cpi->frame_coding_order[cpi->sequence_number] < 0);
3604 if (cpi->multi_arf_enabled && (cpi->pass == 2))
3605 frames_to_arf = (-cpi->frame_coding_order[cpi->sequence_number])
3606 - cpi->next_frame_in_order;
3609 frames_to_arf = cpi->frames_till_gf_update_due;
3611 assert(frames_to_arf < cpi->twopass.frames_to_key);
3613 if ((cpi->source = vp9_lookahead_peek(cpi->lookahead, frames_to_arf))) {
3614 #if CONFIG_MULTIPLE_ARF
3615 cpi->alt_ref_source[cpi->arf_buffered] = cpi->source;
3617 cpi->alt_ref_source = cpi->source;
3620 if (cpi->oxcf.arnr_max_frames > 0) {
3621 // Produce the filtered ARF frame.
3622 // TODO(agrange) merge these two functions.
3623 configure_arnr_filter(cpi, cm->current_video_frame + frames_to_arf,
3625 vp9_temporal_filter_prepare(cpi, frames_to_arf);
3626 force_src_buffer = &cpi->alt_ref_buffer;
3631 cpi->refresh_alt_ref_frame = 1;
3632 cpi->refresh_golden_frame = 0;
3633 cpi->refresh_last_frame = 0;
3634 cpi->is_src_frame_alt_ref = 0;
3636 // TODO(agrange) This needs to vary depending on where the next ARF is.
3637 cpi->frames_till_alt_ref_frame = frames_to_arf;
3639 #if CONFIG_MULTIPLE_ARF
3640 if (!cpi->multi_arf_enabled)
3642 cpi->source_alt_ref_pending = 0; // Clear Pending altf Ref flag.
3647 #if CONFIG_MULTIPLE_ARF
3650 if ((cpi->source = vp9_lookahead_pop(cpi->lookahead, flush))) {
3653 #if CONFIG_MULTIPLE_ARF
3654 // Is this frame the ARF overlay.
3655 cpi->is_src_frame_alt_ref = 0;
3656 for (i = 0; i < cpi->arf_buffered; ++i) {
3657 if (cpi->source == cpi->alt_ref_source[i]) {
3658 cpi->is_src_frame_alt_ref = 1;
3659 cpi->refresh_golden_frame = 1;
3664 cpi->is_src_frame_alt_ref = cpi->alt_ref_source
3665 && (cpi->source == cpi->alt_ref_source);
3667 if (cpi->is_src_frame_alt_ref) {
3668 // Current frame is an ARF overlay frame.
3669 #if CONFIG_MULTIPLE_ARF
3670 cpi->alt_ref_source[i] = NULL;
3672 cpi->alt_ref_source = NULL;
3674 // Don't refresh the last buffer for an ARF overlay frame. It will
3675 // become the GF so preserve last as an alternative prediction option.
3676 cpi->refresh_last_frame = 0;
3678 #if CONFIG_MULTIPLE_ARF
3679 ++cpi->next_frame_in_order;
3685 cpi->un_scaled_source = cpi->Source = force_src_buffer ? force_src_buffer
3686 : &cpi->source->img;
3687 *time_stamp = cpi->source->ts_start;
3688 *time_end = cpi->source->ts_end;
3689 *frame_flags = cpi->source->flags;
3691 // fprintf(fp_out, " Frame:%d", cm->current_video_frame);
3692 #if CONFIG_MULTIPLE_ARF
3693 if (cpi->multi_arf_enabled) {
3694 // fprintf(fp_out, " seq_no:%d this_frame_weight:%d",
3695 // cpi->sequence_number, cpi->this_frame_weight);
3697 // fprintf(fp_out, "\n");
3700 // fprintf(fp_out, "\n");
3703 #if CONFIG_MULTIPLE_ARF
3704 if ((cm->frame_type != KEY_FRAME) && (cpi->pass == 2))
3705 cpi->source_alt_ref_pending = is_next_frame_arf(cpi);
3709 if (flush && cpi->pass == 1 && !cpi->twopass.first_pass_done) {
3710 vp9_end_first_pass(cpi); /* get last stats packet */
3711 cpi->twopass.first_pass_done = 1;
3718 if (cpi->source->ts_start < cpi->first_time_stamp_ever) {
3719 cpi->first_time_stamp_ever = cpi->source->ts_start;
3720 cpi->last_end_time_stamp_seen = cpi->source->ts_start;
3723 // adjust frame rates based on timestamps given
3724 if (!cpi->refresh_alt_ref_frame) {
3725 int64_t this_duration;
3728 if (cpi->source->ts_start == cpi->first_time_stamp_ever) {
3729 this_duration = cpi->source->ts_end - cpi->source->ts_start;
3732 int64_t last_duration = cpi->last_end_time_stamp_seen
3733 - cpi->last_time_stamp_seen;
3735 this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
3737 // do a step update if the duration changes by 10%
3739 step = (int)((this_duration - last_duration) * 10 / last_duration);
3742 if (this_duration) {
3744 vp9_new_framerate(cpi, 10000000.0 / this_duration);
3746 // Average this frame's rate into the last second's average
3747 // frame rate. If we haven't seen 1 second yet, then average
3748 // over the whole interval seen.
3749 const double interval = MIN((double)(cpi->source->ts_end
3750 - cpi->first_time_stamp_ever), 10000000.0);
3751 double avg_duration = 10000000.0 / cpi->oxcf.framerate;
3752 avg_duration *= (interval - avg_duration + this_duration);
3753 avg_duration /= interval;
3755 vp9_new_framerate(cpi, 10000000.0 / avg_duration);
3759 cpi->last_time_stamp_seen = cpi->source->ts_start;
3760 cpi->last_end_time_stamp_seen = cpi->source->ts_end;
3763 // start with a 0 size frame
3766 // Clear down mmx registers
3767 vp9_clear_system_state(); // __asm emms;
3769 /* find a free buffer for the new frame, releasing the reference previously
3772 cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
3773 cm->new_fb_idx = get_free_fb(cm);
3775 #if CONFIG_MULTIPLE_ARF
3776 /* Set up the correct ARF frame. */
3777 if (cpi->refresh_alt_ref_frame) {
3778 ++cpi->arf_buffered;
3780 if (cpi->multi_arf_enabled && (cm->frame_type != KEY_FRAME) &&
3782 cpi->alt_fb_idx = cpi->arf_buffer_idx[cpi->sequence_number];
3786 /* Get the mapping of L/G/A to the reference buffer pool */
3787 cm->active_ref_idx[0] = cm->ref_frame_map[cpi->lst_fb_idx];
3788 cm->active_ref_idx[1] = cm->ref_frame_map[cpi->gld_fb_idx];
3789 cm->active_ref_idx[2] = cm->ref_frame_map[cpi->alt_fb_idx];
3791 #if 0 // CONFIG_MULTIPLE_ARF
3792 if (cpi->multi_arf_enabled) {
3793 fprintf(fp_out, " idx(%d, %d, %d, %d) active(%d, %d, %d)",
3794 cpi->lst_fb_idx, cpi->gld_fb_idx, cpi->alt_fb_idx, cm->new_fb_idx,
3795 cm->active_ref_idx[0], cm->active_ref_idx[1], cm->active_ref_idx[2]);
3796 if (cpi->refresh_alt_ref_frame)
3797 fprintf(fp_out, " type:ARF");
3798 if (cpi->is_src_frame_alt_ref)
3799 fprintf(fp_out, " type:OVERLAY[%d]", cpi->alt_fb_idx);
3800 fprintf(fp_out, "\n");
3804 cm->frame_type = INTER_FRAME;
3805 cm->frame_flags = *frame_flags;
3807 // Reset the frame pointers to the current frame size
3808 vp9_realloc_frame_buffer(&cm->yv12_fb[cm->new_fb_idx],
3809 cm->width, cm->height,
3810 cm->subsampling_x, cm->subsampling_y,
3813 // Calculate scaling factors for each of the 3 available references
3814 for (i = 0; i < ALLOWED_REFS_PER_FRAME; ++i)
3815 vp9_setup_scale_factors(cm, i);
3817 vp9_setup_interp_filters(&cpi->mb.e_mbd, DEFAULT_INTERP_FILTER, cm);
3819 if (cpi->pass == 1) {
3820 Pass1Encode(cpi, size, dest, frame_flags);
3821 } else if (cpi->pass == 2) {
3822 Pass2Encode(cpi, size, dest, frame_flags);
3824 encode_frame_to_data_rate(cpi, size, dest, frame_flags);
3827 if (cm->refresh_frame_context)
3828 cm->frame_contexts[cm->frame_context_idx] = cm->fc;
3831 // if its a dropped frame honor the requests on subsequent frames
3832 cpi->droppable = !frame_is_reference(cpi);
3834 // return to normal state
3835 cm->reset_frame_context = 0;
3836 cm->refresh_frame_context = 1;
3837 cpi->refresh_alt_ref_frame = 0;
3838 cpi->refresh_golden_frame = 0;
3839 cpi->refresh_last_frame = 1;
3840 cm->frame_type = INTER_FRAME;
3843 vpx_usec_timer_mark(&cmptimer);
3844 cpi->time_compress_data += vpx_usec_timer_elapsed(&cmptimer);
3846 if (cpi->b_calculate_psnr && cpi->pass != 1 && cm->show_frame)
3847 generate_psnr_packet(cpi);
3849 #if CONFIG_INTERNAL_STATS
3851 if (cpi->pass != 1) {
3852 cpi->bytes += *size;
3854 if (cm->show_frame) {
3858 if (cpi->b_calculate_psnr) {
3861 YV12_BUFFER_CONFIG *orig = cpi->Source;
3862 YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
3863 YV12_BUFFER_CONFIG *pp = &cm->post_proc_buffer;
3864 int y_samples = orig->y_height * orig->y_width;
3865 int uv_samples = orig->uv_height * orig->uv_width;
3866 int t_samples = y_samples + 2 * uv_samples;
3869 ye = (double)calc_plane_error(orig->y_buffer, orig->y_stride,
3870 recon->y_buffer, recon->y_stride,
3871 orig->y_crop_width, orig->y_crop_height);
3873 ue = (double)calc_plane_error(orig->u_buffer, orig->uv_stride,
3874 recon->u_buffer, recon->uv_stride,
3875 orig->uv_crop_width, orig->uv_crop_height);
3877 ve = (double)calc_plane_error(orig->v_buffer, orig->uv_stride,
3878 recon->v_buffer, recon->uv_stride,
3879 orig->uv_crop_width, orig->uv_crop_height);
3881 sq_error = ye + ue + ve;
3883 frame_psnr = vp9_mse2psnr(t_samples, 255.0, sq_error);
3885 cpi->total_y += vp9_mse2psnr(y_samples, 255.0, ye);
3886 cpi->total_u += vp9_mse2psnr(uv_samples, 255.0, ue);
3887 cpi->total_v += vp9_mse2psnr(uv_samples, 255.0, ve);
3888 cpi->total_sq_error += sq_error;
3889 cpi->total += frame_psnr;
3891 double frame_psnr2, frame_ssim2 = 0;
3894 vp9_deblock(cm->frame_to_show, &cm->post_proc_buffer,
3895 cpi->mb.e_mbd.lf.filter_level * 10 / 6);
3897 vp9_clear_system_state();
3899 ye = (double)calc_plane_error(orig->y_buffer, orig->y_stride,
3900 pp->y_buffer, pp->y_stride,
3901 orig->y_crop_width, orig->y_crop_height);
3903 ue = (double)calc_plane_error(orig->u_buffer, orig->uv_stride,
3904 pp->u_buffer, pp->uv_stride,
3905 orig->uv_crop_width, orig->uv_crop_height);
3907 ve = (double)calc_plane_error(orig->v_buffer, orig->uv_stride,
3908 pp->v_buffer, pp->uv_stride,
3909 orig->uv_crop_width, orig->uv_crop_height);
3911 sq_error = ye + ue + ve;
3913 frame_psnr2 = vp9_mse2psnr(t_samples, 255.0, sq_error);
3915 cpi->totalp_y += vp9_mse2psnr(y_samples, 255.0, ye);
3916 cpi->totalp_u += vp9_mse2psnr(uv_samples, 255.0, ue);
3917 cpi->totalp_v += vp9_mse2psnr(uv_samples, 255.0, ve);
3918 cpi->total_sq_error2 += sq_error;
3919 cpi->totalp += frame_psnr2;
3921 frame_ssim2 = vp9_calc_ssim(cpi->Source,
3924 cpi->summed_quality += frame_ssim2 * weight;
3925 cpi->summed_weights += weight;
3927 frame_ssim2 = vp9_calc_ssim(cpi->Source,
3928 &cm->post_proc_buffer, 1, &weight);
3930 cpi->summedp_quality += frame_ssim2 * weight;
3931 cpi->summedp_weights += weight;
3934 FILE *f = fopen("q_used.stt", "a");
3935 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
3936 cpi->common.current_video_frame, y2, u2, v2,
3937 frame_psnr2, frame_ssim2);
3944 if (cpi->b_calculate_ssimg) {
3945 double y, u, v, frame_all;
3946 frame_all = vp9_calc_ssimg(cpi->Source, cm->frame_to_show,
3948 cpi->total_ssimg_y += y;
3949 cpi->total_ssimg_u += u;
3950 cpi->total_ssimg_v += v;
3951 cpi->total_ssimg_all += frame_all;
3961 int vp9_get_preview_raw_frame(VP9_PTR comp, YV12_BUFFER_CONFIG *dest,
3962 vp9_ppflags_t *flags) {
3963 VP9_COMP *cpi = (VP9_COMP *) comp;
3965 if (!cpi->common.show_frame)
3970 ret = vp9_post_proc_frame(&cpi->common, &cpi->mb.e_mbd.lf, dest, flags);
3973 if (cpi->common.frame_to_show) {
3974 *dest = *cpi->common.frame_to_show;
3975 dest->y_width = cpi->common.width;
3976 dest->y_height = cpi->common.height;
3977 dest->uv_height = cpi->common.height / 2;
3983 #endif // !CONFIG_POSTPROC
3984 vp9_clear_system_state();
3989 int vp9_set_roimap(VP9_PTR comp, unsigned char *map, unsigned int rows,
3990 unsigned int cols, int delta_q[MAX_SEGMENTS],
3991 int delta_lf[MAX_SEGMENTS],
3992 unsigned int threshold[MAX_SEGMENTS]) {
3993 VP9_COMP *cpi = (VP9_COMP *) comp;
3994 signed char feature_data[SEG_LVL_MAX][MAX_SEGMENTS];
3995 MACROBLOCKD *xd = &cpi->mb.e_mbd;
3998 if (cpi->common.mb_rows != rows || cpi->common.mb_cols != cols)
4002 vp9_disable_segmentation((VP9_PTR)cpi);
4006 // Set the segmentation Map
4007 vp9_set_segmentation_map((VP9_PTR)cpi, map);
4009 // Activate segmentation.
4010 vp9_enable_segmentation((VP9_PTR)cpi);
4012 // Set up the quan, LF and breakout threshold segment data
4013 for (i = 0; i < MAX_SEGMENTS; i++) {
4014 feature_data[SEG_LVL_ALT_Q][i] = delta_q[i];
4015 feature_data[SEG_LVL_ALT_LF][i] = delta_lf[i];
4016 cpi->segment_encode_breakout[i] = threshold[i];
4019 // Enable the loop and quant changes in the feature mask
4020 for (i = 0; i < MAX_SEGMENTS; i++) {
4022 vp9_enable_segfeature(&xd->seg, i, SEG_LVL_ALT_Q);
4024 vp9_disable_segfeature(&xd->seg, i, SEG_LVL_ALT_Q);
4027 vp9_enable_segfeature(&xd->seg, i, SEG_LVL_ALT_LF);
4029 vp9_disable_segfeature(&xd->seg, i, SEG_LVL_ALT_LF);
4032 // Initialise the feature data structure
4033 // SEGMENT_DELTADATA 0, SEGMENT_ABSDATA 1
4034 vp9_set_segment_data((VP9_PTR)cpi, &feature_data[0][0], SEGMENT_DELTADATA);
4039 int vp9_set_active_map(VP9_PTR comp, unsigned char *map,
4040 unsigned int rows, unsigned int cols) {
4041 VP9_COMP *cpi = (VP9_COMP *) comp;
4043 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
4045 vpx_memcpy(cpi->active_map, map, rows * cols);
4046 cpi->active_map_enabled = 1;
4048 cpi->active_map_enabled = 0;
4053 // cpi->active_map_enabled = 0;
4058 int vp9_set_internal_size(VP9_PTR comp,
4059 VPX_SCALING horiz_mode, VPX_SCALING vert_mode) {
4060 VP9_COMP *cpi = (VP9_COMP *) comp;
4061 VP9_COMMON *cm = &cpi->common;
4062 int hr = 0, hs = 0, vr = 0, vs = 0;
4064 if (horiz_mode > ONETWO || vert_mode > ONETWO)
4067 Scale2Ratio(horiz_mode, &hr, &hs);
4068 Scale2Ratio(vert_mode, &vr, &vs);
4070 // always go to the next whole number
4071 cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
4072 cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
4074 assert(cm->width <= cpi->initial_width);
4075 assert(cm->height <= cpi->initial_height);
4076 update_frame_size(cpi);
4082 int vp9_calc_ss_err(YV12_BUFFER_CONFIG *source, YV12_BUFFER_CONFIG *dest) {
4086 uint8_t *src = source->y_buffer;
4087 uint8_t *dst = dest->y_buffer;
4089 // Loop through the Y plane raw and reconstruction data summing
4090 // (square differences)
4091 for (i = 0; i < source->y_height; i += 16) {
4092 for (j = 0; j < source->y_width; j += 16) {
4094 total += vp9_mse16x16(src + j, source->y_stride, dst + j, dest->y_stride,
4098 src += 16 * source->y_stride;
4099 dst += 16 * dest->y_stride;
4106 int vp9_get_quantizer(VP9_PTR c) {
4107 return ((VP9_COMP *)c)->common.base_qindex;