hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
| 3 | * |
| 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. |
| 9 | */ |
| 10 | |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 11 | #include <limits.h> |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 12 | #include <math.h> |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 13 | #include <stdio.h> |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 14 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 15 | #include "./vpx_dsp_rtcd.h" |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 16 | #include "./vpx_scale_rtcd.h" |
| 17 | |
| 18 | #include "vpx_mem/vpx_mem.h" |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 19 | #include "vpx_ports/mem.h" |
| 20 | #include "vpx_ports/system_state.h" |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 21 | #include "vpx_scale/vpx_scale.h" |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 22 | #include "vpx_scale/yv12config.h" |
| 23 | |
| 24 | #include "vp9/common/vp9_entropymv.h" |
| 25 | #include "vp9/common/vp9_quant_common.h" |
| 26 | #include "vp9/common/vp9_reconinter.h" // vp9_setup_dst_planes() |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 27 | #include "vp9/encoder/vp9_aq_variance.h" |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 28 | #include "vp9/encoder/vp9_block.h" |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 29 | #include "vp9/encoder/vp9_encodeframe.h" |
| 30 | #include "vp9/encoder/vp9_encodemb.h" |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 31 | #include "vp9/encoder/vp9_encodemv.h" |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 32 | #include "vp9/encoder/vp9_encoder.h" |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 33 | #include "vp9/encoder/vp9_extend.h" |
| 34 | #include "vp9/encoder/vp9_firstpass.h" |
| 35 | #include "vp9/encoder/vp9_mcomp.h" |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 36 | #include "vp9/encoder/vp9_quantize.h" |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 37 | #include "vp9/encoder/vp9_rd.h" |
| 38 | #include "vpx_dsp/variance.h" |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 39 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 40 | #define OUTPUT_FPF 0 |
| 41 | #define ARF_STATS_OUTPUT 0 |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 42 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 43 | #define GROUP_ADAPTIVE_MAXQ 1 |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 44 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 45 | #define BOOST_BREAKOUT 12.5 |
| 46 | #define BOOST_FACTOR 12.5 |
| 47 | #define ERR_DIVISOR 128.0 |
| 48 | #define FACTOR_PT_LOW 0.70 |
| 49 | #define FACTOR_PT_HIGH 0.90 |
| 50 | #define FIRST_PASS_Q 10.0 |
| 51 | #define GF_MAX_BOOST 96.0 |
| 52 | #define INTRA_MODE_PENALTY 1024 |
| 53 | #define KF_MAX_BOOST 128.0 |
| 54 | #define MIN_ARF_GF_BOOST 240 |
| 55 | #define MIN_DECAY_FACTOR 0.01 |
| 56 | #define MIN_KF_BOOST 300 |
| 57 | #define NEW_MV_MODE_PENALTY 32 |
| 58 | #define SVC_FACTOR_PT_LOW 0.45 |
| 59 | #define DARK_THRESH 64 |
| 60 | #define DEFAULT_GRP_WEIGHT 1.0 |
| 61 | #define RC_FACTOR_MIN 0.75 |
| 62 | #define RC_FACTOR_MAX 1.75 |
| 63 | |
| 64 | |
| 65 | #define NCOUNT_INTRA_THRESH 8192 |
| 66 | #define NCOUNT_INTRA_FACTOR 3 |
| 67 | #define NCOUNT_FRAME_II_THRESH 5.0 |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 68 | |
| 69 | #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001) |
| 70 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 71 | #if ARF_STATS_OUTPUT |
| 72 | unsigned int arf_count = 0; |
Vignesh Venkatasubramanian | a72801d | 2014-03-20 16:30:56 -0700 | [diff] [blame] | 73 | #endif |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 74 | |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 75 | // Resets the first pass file to the given position using a relative seek from |
| 76 | // the current position. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 77 | static void reset_fpf_position(TWO_PASS *p, |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 78 | const FIRSTPASS_STATS *position) { |
| 79 | p->stats_in = position; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 82 | // Read frame stats at an offset from the current position. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 83 | static const FIRSTPASS_STATS *read_frame_stats(const TWO_PASS *p, int offset) { |
| 84 | if ((offset >= 0 && p->stats_in + offset >= p->stats_in_end) || |
| 85 | (offset < 0 && p->stats_in + offset < p->stats_in_start)) { |
| 86 | return NULL; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 89 | return &p->stats_in[offset]; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 92 | static int input_stats(TWO_PASS *p, FIRSTPASS_STATS *fps) { |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 93 | if (p->stats_in >= p->stats_in_end) |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 94 | return EOF; |
| 95 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 96 | *fps = *p->stats_in; |
| 97 | ++p->stats_in; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 98 | return 1; |
| 99 | } |
| 100 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 101 | static void output_stats(FIRSTPASS_STATS *stats, |
| 102 | struct vpx_codec_pkt_list *pktlist) { |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 103 | struct vpx_codec_cx_pkt pkt; |
| 104 | pkt.kind = VPX_CODEC_STATS_PKT; |
| 105 | pkt.data.twopass_stats.buf = stats; |
| 106 | pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS); |
| 107 | vpx_codec_pkt_list_add(pktlist, &pkt); |
| 108 | |
| 109 | // TEMP debug code |
| 110 | #if OUTPUT_FPF |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 111 | { |
| 112 | FILE *fpfile; |
| 113 | fpfile = fopen("firstpass.stt", "a"); |
| 114 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 115 | fprintf(fpfile, "%12.0lf %12.4lf %12.0lf %12.0lf %12.0lf %12.4lf %12.4lf" |
| 116 | "%12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf" |
| 117 | "%12.4lf %12.4lf %12.0lf %12.0lf %12.0lf %12.4lf\n", |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 118 | stats->frame, |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 119 | stats->weight, |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 120 | stats->intra_error, |
| 121 | stats->coded_error, |
| 122 | stats->sr_coded_error, |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 123 | stats->pcnt_inter, |
| 124 | stats->pcnt_motion, |
| 125 | stats->pcnt_second_ref, |
| 126 | stats->pcnt_neutral, |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 127 | stats->intra_skip_pct, |
| 128 | stats->inactive_zone_rows, |
| 129 | stats->inactive_zone_cols, |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 130 | stats->MVr, |
| 131 | stats->mvr_abs, |
| 132 | stats->MVc, |
| 133 | stats->mvc_abs, |
| 134 | stats->MVrv, |
| 135 | stats->MVcv, |
| 136 | stats->mv_in_out_count, |
| 137 | stats->new_mv_count, |
| 138 | stats->count, |
| 139 | stats->duration); |
| 140 | fclose(fpfile); |
| 141 | } |
| 142 | #endif |
| 143 | } |
| 144 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 145 | #if CONFIG_FP_MB_STATS |
| 146 | static void output_fpmb_stats(uint8_t *this_frame_mb_stats, VP9_COMMON *cm, |
| 147 | struct vpx_codec_pkt_list *pktlist) { |
| 148 | struct vpx_codec_cx_pkt pkt; |
| 149 | pkt.kind = VPX_CODEC_FPMB_STATS_PKT; |
| 150 | pkt.data.firstpass_mb_stats.buf = this_frame_mb_stats; |
| 151 | pkt.data.firstpass_mb_stats.sz = cm->initial_mbs * sizeof(uint8_t); |
| 152 | vpx_codec_pkt_list_add(pktlist, &pkt); |
| 153 | } |
| 154 | #endif |
| 155 | |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 156 | static void zero_stats(FIRSTPASS_STATS *section) { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 157 | section->frame = 0.0; |
| 158 | section->weight = 0.0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 159 | section->intra_error = 0.0; |
| 160 | section->coded_error = 0.0; |
| 161 | section->sr_coded_error = 0.0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 162 | section->pcnt_inter = 0.0; |
| 163 | section->pcnt_motion = 0.0; |
| 164 | section->pcnt_second_ref = 0.0; |
| 165 | section->pcnt_neutral = 0.0; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 166 | section->intra_skip_pct = 0.0; |
| 167 | section->inactive_zone_rows = 0.0; |
| 168 | section->inactive_zone_cols = 0.0; |
| 169 | section->MVr = 0.0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 170 | section->mvr_abs = 0.0; |
| 171 | section->MVc = 0.0; |
| 172 | section->mvc_abs = 0.0; |
| 173 | section->MVrv = 0.0; |
| 174 | section->MVcv = 0.0; |
| 175 | section->mv_in_out_count = 0.0; |
| 176 | section->new_mv_count = 0.0; |
| 177 | section->count = 0.0; |
| 178 | section->duration = 1.0; |
Vignesh Venkatasubramanian | a72801d | 2014-03-20 16:30:56 -0700 | [diff] [blame] | 179 | section->spatial_layer_id = 0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Vignesh Venkatasubramanian | a72801d | 2014-03-20 16:30:56 -0700 | [diff] [blame] | 182 | static void accumulate_stats(FIRSTPASS_STATS *section, |
| 183 | const FIRSTPASS_STATS *frame) { |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 184 | section->frame += frame->frame; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 185 | section->weight += frame->weight; |
Vignesh Venkatasubramanian | a72801d | 2014-03-20 16:30:56 -0700 | [diff] [blame] | 186 | section->spatial_layer_id = frame->spatial_layer_id; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 187 | section->intra_error += frame->intra_error; |
| 188 | section->coded_error += frame->coded_error; |
| 189 | section->sr_coded_error += frame->sr_coded_error; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 190 | section->pcnt_inter += frame->pcnt_inter; |
| 191 | section->pcnt_motion += frame->pcnt_motion; |
| 192 | section->pcnt_second_ref += frame->pcnt_second_ref; |
| 193 | section->pcnt_neutral += frame->pcnt_neutral; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 194 | section->intra_skip_pct += frame->intra_skip_pct; |
| 195 | section->inactive_zone_rows += frame->inactive_zone_rows; |
| 196 | section->inactive_zone_cols += frame->inactive_zone_cols; |
| 197 | section->MVr += frame->MVr; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 198 | section->mvr_abs += frame->mvr_abs; |
| 199 | section->MVc += frame->MVc; |
| 200 | section->mvc_abs += frame->mvc_abs; |
| 201 | section->MVrv += frame->MVrv; |
| 202 | section->MVcv += frame->MVcv; |
| 203 | section->mv_in_out_count += frame->mv_in_out_count; |
| 204 | section->new_mv_count += frame->new_mv_count; |
| 205 | section->count += frame->count; |
| 206 | section->duration += frame->duration; |
| 207 | } |
| 208 | |
Vignesh Venkatasubramanian | a72801d | 2014-03-20 16:30:56 -0700 | [diff] [blame] | 209 | static void subtract_stats(FIRSTPASS_STATS *section, |
| 210 | const FIRSTPASS_STATS *frame) { |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 211 | section->frame -= frame->frame; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 212 | section->weight -= frame->weight; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 213 | section->intra_error -= frame->intra_error; |
| 214 | section->coded_error -= frame->coded_error; |
| 215 | section->sr_coded_error -= frame->sr_coded_error; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 216 | section->pcnt_inter -= frame->pcnt_inter; |
| 217 | section->pcnt_motion -= frame->pcnt_motion; |
| 218 | section->pcnt_second_ref -= frame->pcnt_second_ref; |
| 219 | section->pcnt_neutral -= frame->pcnt_neutral; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 220 | section->intra_skip_pct -= frame->intra_skip_pct; |
| 221 | section->inactive_zone_rows -= frame->inactive_zone_rows; |
| 222 | section->inactive_zone_cols -= frame->inactive_zone_cols; |
| 223 | section->MVr -= frame->MVr; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 224 | section->mvr_abs -= frame->mvr_abs; |
| 225 | section->MVc -= frame->MVc; |
| 226 | section->mvc_abs -= frame->mvc_abs; |
| 227 | section->MVrv -= frame->MVrv; |
| 228 | section->MVcv -= frame->MVcv; |
| 229 | section->mv_in_out_count -= frame->mv_in_out_count; |
| 230 | section->new_mv_count -= frame->new_mv_count; |
| 231 | section->count -= frame->count; |
| 232 | section->duration -= frame->duration; |
| 233 | } |
| 234 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 235 | // Calculate an active area of the image that discounts formatting |
| 236 | // bars and partially discounts other 0 energy areas. |
| 237 | #define MIN_ACTIVE_AREA 0.5 |
| 238 | #define MAX_ACTIVE_AREA 1.0 |
| 239 | static double calculate_active_area(const VP9_COMP *cpi, |
| 240 | const FIRSTPASS_STATS *this_frame) { |
| 241 | double active_pct; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 242 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 243 | active_pct = 1.0 - |
| 244 | ((this_frame->intra_skip_pct / 2) + |
| 245 | ((this_frame->inactive_zone_rows * 2) / (double)cpi->common.mb_rows)); |
| 246 | return fclamp(active_pct, MIN_ACTIVE_AREA, MAX_ACTIVE_AREA); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 247 | } |
| 248 | |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 249 | // Calculate a modified Error used in distributing bits between easier and |
| 250 | // harder frames. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 251 | #define ACT_AREA_CORRECTION 0.5 |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 252 | static double calculate_modified_err(const VP9_COMP *cpi, |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 253 | const TWO_PASS *twopass, |
| 254 | const VP9EncoderConfig *oxcf, |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 255 | const FIRSTPASS_STATS *this_frame) { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 256 | const FIRSTPASS_STATS *const stats = &twopass->total_stats; |
| 257 | const double av_weight = stats->weight / stats->count; |
| 258 | const double av_err = (stats->coded_error * av_weight) / stats->count; |
| 259 | double modified_error = |
| 260 | av_err * pow(this_frame->coded_error * this_frame->weight / |
| 261 | DOUBLE_DIVIDE_CHECK(av_err), oxcf->two_pass_vbrbias / 100.0); |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 262 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 263 | // Correction for active area. Frames with a reduced active area |
| 264 | // (eg due to formatting bars) have a higher error per mb for the |
| 265 | // remaining active MBs. The correction here assumes that coding |
| 266 | // 0.5N blocks of complexity 2X is a little easier than coding N |
| 267 | // blocks of complexity X. |
| 268 | modified_error *= |
| 269 | pow(calculate_active_area(cpi, this_frame), ACT_AREA_CORRECTION); |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 270 | |
| 271 | return fclamp(modified_error, |
| 272 | twopass->modified_error_min, twopass->modified_error_max); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 275 | // This function returns the maximum target rate per frame. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 276 | static int frame_max_bits(const RATE_CONTROL *rc, |
| 277 | const VP9EncoderConfig *oxcf) { |
| 278 | int64_t max_bits = ((int64_t)rc->avg_frame_bandwidth * |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 279 | (int64_t)oxcf->two_pass_vbrmax_section) / 100; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 280 | if (max_bits < 0) |
| 281 | max_bits = 0; |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 282 | else if (max_bits > rc->max_frame_bandwidth) |
| 283 | max_bits = rc->max_frame_bandwidth; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 284 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 285 | return (int)max_bits; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | void vp9_init_first_pass(VP9_COMP *cpi) { |
| 289 | zero_stats(&cpi->twopass.total_stats); |
| 290 | } |
| 291 | |
| 292 | void vp9_end_first_pass(VP9_COMP *cpi) { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 293 | if (is_two_pass_svc(cpi)) { |
Vignesh Venkatasubramanian | a72801d | 2014-03-20 16:30:56 -0700 | [diff] [blame] | 294 | int i; |
| 295 | for (i = 0; i < cpi->svc.number_spatial_layers; ++i) { |
| 296 | output_stats(&cpi->svc.layer_context[i].twopass.total_stats, |
| 297 | cpi->output_pkt_list); |
| 298 | } |
| 299 | } else { |
| 300 | output_stats(&cpi->twopass.total_stats, cpi->output_pkt_list); |
| 301 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 304 | static vpx_variance_fn_t get_block_variance_fn(BLOCK_SIZE bsize) { |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 305 | switch (bsize) { |
hkuang | f3bed91 | 2013-08-06 11:07:19 -0700 | [diff] [blame] | 306 | case BLOCK_8X8: |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 307 | return vpx_mse8x8; |
hkuang | f3bed91 | 2013-08-06 11:07:19 -0700 | [diff] [blame] | 308 | case BLOCK_16X8: |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 309 | return vpx_mse16x8; |
hkuang | f3bed91 | 2013-08-06 11:07:19 -0700 | [diff] [blame] | 310 | case BLOCK_8X16: |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 311 | return vpx_mse8x16; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 312 | default: |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 313 | return vpx_mse16x16; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 314 | } |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 317 | static unsigned int get_prediction_error(BLOCK_SIZE bsize, |
| 318 | const struct buf_2d *src, |
| 319 | const struct buf_2d *ref) { |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 320 | unsigned int sse; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 321 | const vpx_variance_fn_t fn = get_block_variance_fn(bsize); |
| 322 | fn(src->buf, src->stride, ref->buf, ref->stride, &sse); |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 323 | return sse; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 324 | } |
| 325 | |
| 326 | #if CONFIG_VP9_HIGHBITDEPTH |
| 327 | static vpx_variance_fn_t highbd_get_block_variance_fn(BLOCK_SIZE bsize, |
| 328 | int bd) { |
| 329 | switch (bd) { |
| 330 | default: |
| 331 | switch (bsize) { |
| 332 | case BLOCK_8X8: |
| 333 | return vpx_highbd_8_mse8x8; |
| 334 | case BLOCK_16X8: |
| 335 | return vpx_highbd_8_mse16x8; |
| 336 | case BLOCK_8X16: |
| 337 | return vpx_highbd_8_mse8x16; |
| 338 | default: |
| 339 | return vpx_highbd_8_mse16x16; |
| 340 | } |
| 341 | break; |
| 342 | case 10: |
| 343 | switch (bsize) { |
| 344 | case BLOCK_8X8: |
| 345 | return vpx_highbd_10_mse8x8; |
| 346 | case BLOCK_16X8: |
| 347 | return vpx_highbd_10_mse16x8; |
| 348 | case BLOCK_8X16: |
| 349 | return vpx_highbd_10_mse8x16; |
| 350 | default: |
| 351 | return vpx_highbd_10_mse16x16; |
| 352 | } |
| 353 | break; |
| 354 | case 12: |
| 355 | switch (bsize) { |
| 356 | case BLOCK_8X8: |
| 357 | return vpx_highbd_12_mse8x8; |
| 358 | case BLOCK_16X8: |
| 359 | return vpx_highbd_12_mse16x8; |
| 360 | case BLOCK_8X16: |
| 361 | return vpx_highbd_12_mse8x16; |
| 362 | default: |
| 363 | return vpx_highbd_12_mse16x16; |
| 364 | } |
| 365 | break; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | static unsigned int highbd_get_prediction_error(BLOCK_SIZE bsize, |
| 370 | const struct buf_2d *src, |
| 371 | const struct buf_2d *ref, |
| 372 | int bd) { |
| 373 | unsigned int sse; |
| 374 | const vpx_variance_fn_t fn = highbd_get_block_variance_fn(bsize, bd); |
| 375 | fn(src->buf, src->stride, ref->buf, ref->stride, &sse); |
| 376 | return sse; |
| 377 | } |
| 378 | #endif // CONFIG_VP9_HIGHBITDEPTH |
| 379 | |
| 380 | // Refine the motion search range according to the frame dimension |
| 381 | // for first pass test. |
| 382 | static int get_search_range(const VP9_COMP *cpi) { |
| 383 | int sr = 0; |
| 384 | const int dim = MIN(cpi->initial_width, cpi->initial_height); |
| 385 | |
| 386 | while ((dim << sr) < MAX_FULL_PEL_VAL) |
| 387 | ++sr; |
| 388 | return sr; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 389 | } |
| 390 | |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 391 | static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x, |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 392 | const MV *ref_mv, MV *best_mv, |
| 393 | int *best_motion_err) { |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 394 | MACROBLOCKD *const xd = &x->e_mbd; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 395 | MV tmp_mv = {0, 0}; |
| 396 | MV ref_mv_full = {ref_mv->row >> 3, ref_mv->col >> 3}; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 397 | int num00, tmp_err, n; |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 398 | const BLOCK_SIZE bsize = xd->mi[0]->mbmi.sb_type; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 399 | vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[bsize]; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 400 | const int new_mv_mode_penalty = NEW_MV_MODE_PENALTY; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 401 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 402 | int step_param = 3; |
| 403 | int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; |
| 404 | const int sr = get_search_range(cpi); |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 405 | step_param += sr; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 406 | further_steps -= sr; |
| 407 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 408 | // Override the default variance function to use MSE. |
| 409 | v_fn_ptr.vf = get_block_variance_fn(bsize); |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 410 | #if CONFIG_VP9_HIGHBITDEPTH |
| 411 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 412 | v_fn_ptr.vf = highbd_get_block_variance_fn(bsize, xd->bd); |
| 413 | } |
| 414 | #endif // CONFIG_VP9_HIGHBITDEPTH |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 415 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 416 | // Center the initial step/diamond search on best mv. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 417 | tmp_err = cpi->diamond_search_sad(x, &cpi->ss_cfg, &ref_mv_full, &tmp_mv, |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 418 | step_param, |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 419 | x->sadperbit16, &num00, &v_fn_ptr, ref_mv); |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 420 | if (tmp_err < INT_MAX) |
| 421 | tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 422 | if (tmp_err < INT_MAX - new_mv_mode_penalty) |
| 423 | tmp_err += new_mv_mode_penalty; |
| 424 | |
| 425 | if (tmp_err < *best_motion_err) { |
| 426 | *best_motion_err = tmp_err; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 427 | *best_mv = tmp_mv; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 430 | // Carry out further step/diamond searches as necessary. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 431 | n = num00; |
| 432 | num00 = 0; |
| 433 | |
| 434 | while (n < further_steps) { |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 435 | ++n; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 436 | |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 437 | if (num00) { |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 438 | --num00; |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 439 | } else { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 440 | tmp_err = cpi->diamond_search_sad(x, &cpi->ss_cfg, &ref_mv_full, &tmp_mv, |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 441 | step_param + n, x->sadperbit16, |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 442 | &num00, &v_fn_ptr, ref_mv); |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 443 | if (tmp_err < INT_MAX) |
| 444 | tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 445 | if (tmp_err < INT_MAX - new_mv_mode_penalty) |
| 446 | tmp_err += new_mv_mode_penalty; |
| 447 | |
| 448 | if (tmp_err < *best_motion_err) { |
| 449 | *best_motion_err = tmp_err; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 450 | *best_mv = tmp_mv; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 451 | } |
| 452 | } |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
| 456 | static BLOCK_SIZE get_bsize(const VP9_COMMON *cm, int mb_row, int mb_col) { |
| 457 | if (2 * mb_col + 1 < cm->mi_cols) { |
| 458 | return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_16X16 |
| 459 | : BLOCK_16X8; |
| 460 | } else { |
| 461 | return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_8X16 |
| 462 | : BLOCK_8X8; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 466 | static int find_fp_qindex(vpx_bit_depth_t bit_depth) { |
| 467 | int i; |
| 468 | |
| 469 | for (i = 0; i < QINDEX_RANGE; ++i) |
| 470 | if (vp9_convert_qindex_to_q(i, bit_depth) >= FIRST_PASS_Q) |
| 471 | break; |
| 472 | |
| 473 | if (i == QINDEX_RANGE) |
| 474 | i--; |
| 475 | |
| 476 | return i; |
| 477 | } |
| 478 | |
| 479 | static void set_first_pass_params(VP9_COMP *cpi) { |
| 480 | VP9_COMMON *const cm = &cpi->common; |
| 481 | if (!cpi->refresh_alt_ref_frame && |
| 482 | (cm->current_video_frame == 0 || |
| 483 | (cpi->frame_flags & FRAMEFLAGS_KEY))) { |
| 484 | cm->frame_type = KEY_FRAME; |
| 485 | } else { |
| 486 | cm->frame_type = INTER_FRAME; |
| 487 | } |
| 488 | // Do not use periodic key frames. |
| 489 | cpi->rc.frames_to_key = INT_MAX; |
| 490 | } |
| 491 | |
| 492 | #define UL_INTRA_THRESH 50 |
| 493 | #define INVALID_ROW -1 |
| 494 | void vp9_first_pass(VP9_COMP *cpi, const struct lookahead_entry *source) { |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 495 | int mb_row, mb_col; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 496 | MACROBLOCK *const x = &cpi->td.mb; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 497 | VP9_COMMON *const cm = &cpi->common; |
| 498 | MACROBLOCKD *const xd = &x->e_mbd; |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 499 | TileInfo tile; |
hkuang | 9b35249 | 2013-11-14 16:48:58 -0800 | [diff] [blame] | 500 | struct macroblock_plane *const p = x->plane; |
| 501 | struct macroblockd_plane *const pd = xd->plane; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 502 | const PICK_MODE_CONTEXT *ctx = &cpi->td.pc_root->none; |
hkuang | 9b35249 | 2013-11-14 16:48:58 -0800 | [diff] [blame] | 503 | int i; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 504 | |
| 505 | int recon_yoffset, recon_uvoffset; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 506 | int64_t intra_error = 0; |
| 507 | int64_t coded_error = 0; |
| 508 | int64_t sr_coded_error = 0; |
| 509 | |
| 510 | int sum_mvr = 0, sum_mvc = 0; |
| 511 | int sum_mvr_abs = 0, sum_mvc_abs = 0; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 512 | int64_t sum_mvrs = 0, sum_mvcs = 0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 513 | int mvcount = 0; |
| 514 | int intercount = 0; |
| 515 | int second_ref_count = 0; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 516 | const int intrapenalty = INTRA_MODE_PENALTY; |
| 517 | double neutral_count; |
| 518 | int intra_skip_count = 0; |
| 519 | int image_data_start_row = INVALID_ROW; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 520 | int new_mv_count = 0; |
| 521 | int sum_in_vectors = 0; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 522 | MV lastmv = {0, 0}; |
| 523 | TWO_PASS *twopass = &cpi->twopass; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 524 | const MV zero_mv = {0, 0}; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 525 | int recon_y_stride, recon_uv_stride, uv_mb_height; |
| 526 | |
| 527 | YV12_BUFFER_CONFIG *const lst_yv12 = get_ref_frame_buffer(cpi, LAST_FRAME); |
| 528 | YV12_BUFFER_CONFIG *gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME); |
| 529 | YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm); |
Vignesh Venkatasubramanian | a72801d | 2014-03-20 16:30:56 -0700 | [diff] [blame] | 530 | const YV12_BUFFER_CONFIG *first_ref_buf = lst_yv12; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 531 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 532 | LAYER_CONTEXT *const lc = is_two_pass_svc(cpi) ? |
| 533 | &cpi->svc.layer_context[cpi->svc.spatial_layer_id] : NULL; |
| 534 | double intra_factor; |
| 535 | double brightness_factor; |
| 536 | BufferPool *const pool = cm->buffer_pool; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 537 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 538 | // First pass code requires valid last and new frame buffers. |
| 539 | assert(new_yv12 != NULL); |
| 540 | assert((lc != NULL) || frame_is_intra_only(cm) || (lst_yv12 != NULL)); |
| 541 | |
| 542 | #if CONFIG_FP_MB_STATS |
| 543 | if (cpi->use_fp_mb_stats) { |
| 544 | vp9_zero_array(cpi->twopass.frame_mb_stats_buf, cm->initial_mbs); |
| 545 | } |
| 546 | #endif |
| 547 | |
| 548 | vpx_clear_system_state(); |
| 549 | |
| 550 | intra_factor = 0.0; |
| 551 | brightness_factor = 0.0; |
| 552 | neutral_count = 0.0; |
| 553 | |
| 554 | set_first_pass_params(cpi); |
| 555 | vp9_set_quantizer(cm, find_fp_qindex(cm->bit_depth)); |
| 556 | |
| 557 | if (lc != NULL) { |
| 558 | twopass = &lc->twopass; |
| 559 | |
| 560 | cpi->lst_fb_idx = cpi->svc.spatial_layer_id; |
| 561 | cpi->ref_frame_flags = VP9_LAST_FLAG; |
| 562 | |
| 563 | if (cpi->svc.number_spatial_layers + cpi->svc.spatial_layer_id < |
| 564 | REF_FRAMES) { |
| 565 | cpi->gld_fb_idx = |
| 566 | cpi->svc.number_spatial_layers + cpi->svc.spatial_layer_id; |
| 567 | cpi->ref_frame_flags |= VP9_GOLD_FLAG; |
| 568 | cpi->refresh_golden_frame = (lc->current_video_frame_in_layer == 0); |
| 569 | } else { |
| 570 | cpi->refresh_golden_frame = 0; |
| 571 | } |
| 572 | |
| 573 | if (lc->current_video_frame_in_layer == 0) |
| 574 | cpi->ref_frame_flags = 0; |
Vignesh Venkatasubramanian | a72801d | 2014-03-20 16:30:56 -0700 | [diff] [blame] | 575 | |
| 576 | vp9_scale_references(cpi); |
| 577 | |
| 578 | // Use either last frame or alt frame for motion search. |
| 579 | if (cpi->ref_frame_flags & VP9_LAST_FLAG) { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 580 | first_ref_buf = vp9_get_scaled_ref_frame(cpi, LAST_FRAME); |
| 581 | if (first_ref_buf == NULL) |
| 582 | first_ref_buf = get_ref_frame_buffer(cpi, LAST_FRAME); |
Vignesh Venkatasubramanian | a72801d | 2014-03-20 16:30:56 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 585 | if (cpi->ref_frame_flags & VP9_GOLD_FLAG) { |
| 586 | gld_yv12 = vp9_get_scaled_ref_frame(cpi, GOLDEN_FRAME); |
| 587 | if (gld_yv12 == NULL) { |
| 588 | gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME); |
| 589 | } |
| 590 | } else { |
| 591 | gld_yv12 = NULL; |
Vignesh Venkatasubramanian | a72801d | 2014-03-20 16:30:56 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 594 | set_ref_ptrs(cm, xd, |
| 595 | (cpi->ref_frame_flags & VP9_LAST_FLAG) ? LAST_FRAME: NONE, |
| 596 | (cpi->ref_frame_flags & VP9_GOLD_FLAG) ? GOLDEN_FRAME : NONE); |
| 597 | |
| 598 | cpi->Source = vp9_scale_if_required(cm, cpi->un_scaled_source, |
| 599 | &cpi->scaled_source); |
Vignesh Venkatasubramanian | a72801d | 2014-03-20 16:30:56 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 602 | vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y); |
| 603 | |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 604 | vp9_setup_src_planes(x, cpi->Source, 0, 0); |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 605 | vp9_setup_dst_planes(xd->plane, new_yv12, 0, 0); |
| 606 | |
| 607 | if (!frame_is_intra_only(cm)) { |
| 608 | vp9_setup_pre_planes(xd, 0, first_ref_buf, 0, 0, NULL); |
| 609 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 610 | |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 611 | xd->mi = cm->mi_grid_visible; |
| 612 | xd->mi[0] = cm->mi; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 613 | |
| 614 | vp9_frame_init_quantizer(cpi); |
| 615 | |
hkuang | 9b35249 | 2013-11-14 16:48:58 -0800 | [diff] [blame] | 616 | for (i = 0; i < MAX_MB_PLANE; ++i) { |
| 617 | p[i].coeff = ctx->coeff_pbuf[i][1]; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 618 | p[i].qcoeff = ctx->qcoeff_pbuf[i][1]; |
hkuang | 9b35249 | 2013-11-14 16:48:58 -0800 | [diff] [blame] | 619 | pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1]; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 620 | p[i].eobs = ctx->eobs_pbuf[i][1]; |
hkuang | 9b35249 | 2013-11-14 16:48:58 -0800 | [diff] [blame] | 621 | } |
| 622 | x->skip_recode = 0; |
| 623 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 624 | vp9_init_mv_probs(cm); |
| 625 | vp9_initialize_rd_consts(cpi); |
hkuang | 9b35249 | 2013-11-14 16:48:58 -0800 | [diff] [blame] | 626 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 627 | // Tiling is ignored in the first pass. |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 628 | vp9_tile_init(&tile, cm, 0, 0); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 629 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 630 | recon_y_stride = new_yv12->y_stride; |
| 631 | recon_uv_stride = new_yv12->uv_stride; |
| 632 | uv_mb_height = 16 >> (new_yv12->y_height > new_yv12->uv_height); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 633 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 634 | for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) { |
| 635 | MV best_ref_mv = {0, 0}; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 636 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 637 | // Reset above block coeffs. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 638 | xd->up_available = (mb_row != 0); |
| 639 | recon_yoffset = (mb_row * recon_y_stride * 16); |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 640 | recon_uvoffset = (mb_row * recon_uv_stride * uv_mb_height); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 641 | |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 642 | // Set up limit values for motion vectors to prevent them extending |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 643 | // outside the UMV borders. |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 644 | x->mv_row_min = -((mb_row * 16) + BORDER_MV_PIXELS_B16); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 645 | x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 646 | + BORDER_MV_PIXELS_B16; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 647 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 648 | for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) { |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 649 | int this_error; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 650 | const int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row); |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 651 | const BLOCK_SIZE bsize = get_bsize(cm, mb_row, mb_col); |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 652 | double log_intra; |
| 653 | int level_sample; |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 654 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 655 | #if CONFIG_FP_MB_STATS |
| 656 | const int mb_index = mb_row * cm->mb_cols + mb_col; |
| 657 | #endif |
| 658 | |
| 659 | vpx_clear_system_state(); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 660 | |
| 661 | xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset; |
| 662 | xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset; |
| 663 | xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset; |
| 664 | xd->left_available = (mb_col != 0); |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 665 | xd->mi[0]->mbmi.sb_type = bsize; |
| 666 | xd->mi[0]->mbmi.ref_frame[0] = INTRA_FRAME; |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 667 | set_mi_row_col(xd, &tile, |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 668 | mb_row << 1, num_8x8_blocks_high_lookup[bsize], |
| 669 | mb_col << 1, num_8x8_blocks_wide_lookup[bsize], |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 670 | cm->mi_rows, cm->mi_cols); |
| 671 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 672 | // Do intra 16x16 prediction. |
| 673 | x->skip_encode = 0; |
| 674 | xd->mi[0]->mbmi.mode = DC_PRED; |
| 675 | xd->mi[0]->mbmi.tx_size = use_dc_pred ? |
| 676 | (bsize >= BLOCK_16X16 ? TX_16X16 : TX_8X8) : TX_4X4; |
| 677 | vp9_encode_intra_block_plane(x, bsize, 0); |
| 678 | this_error = vpx_get_mb_ss(x->plane[0].src_diff); |
| 679 | |
| 680 | // Keep a record of blocks that have almost no intra error residual |
| 681 | // (i.e. are in effect completely flat and untextured in the intra |
| 682 | // domain). In natural videos this is uncommon, but it is much more |
| 683 | // common in animations, graphics and screen content, so may be used |
| 684 | // as a signal to detect these types of content. |
| 685 | if (this_error < UL_INTRA_THRESH) { |
| 686 | ++intra_skip_count; |
| 687 | } else if ((mb_col > 0) && (image_data_start_row == INVALID_ROW)) { |
| 688 | image_data_start_row = mb_row; |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 689 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 690 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 691 | #if CONFIG_VP9_HIGHBITDEPTH |
| 692 | if (cm->use_highbitdepth) { |
| 693 | switch (cm->bit_depth) { |
| 694 | case VPX_BITS_8: |
| 695 | break; |
| 696 | case VPX_BITS_10: |
| 697 | this_error >>= 4; |
| 698 | break; |
| 699 | case VPX_BITS_12: |
| 700 | this_error >>= 8; |
| 701 | break; |
| 702 | default: |
| 703 | assert(0 && "cm->bit_depth should be VPX_BITS_8, " |
| 704 | "VPX_BITS_10 or VPX_BITS_12"); |
| 705 | return; |
| 706 | } |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 707 | } |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 708 | #endif // CONFIG_VP9_HIGHBITDEPTH |
| 709 | |
| 710 | vpx_clear_system_state(); |
| 711 | log_intra = log(this_error + 1.0); |
| 712 | if (log_intra < 10.0) |
| 713 | intra_factor += 1.0 + ((10.0 - log_intra) * 0.05); |
| 714 | else |
| 715 | intra_factor += 1.0; |
| 716 | |
| 717 | #if CONFIG_VP9_HIGHBITDEPTH |
| 718 | if (cm->use_highbitdepth) |
| 719 | level_sample = CONVERT_TO_SHORTPTR(x->plane[0].src.buf)[0]; |
| 720 | else |
| 721 | level_sample = x->plane[0].src.buf[0]; |
| 722 | #else |
| 723 | level_sample = x->plane[0].src.buf[0]; |
| 724 | #endif |
| 725 | if ((level_sample < DARK_THRESH) && (log_intra < 9.0)) |
| 726 | brightness_factor += 1.0 + (0.01 * (DARK_THRESH - level_sample)); |
| 727 | else |
| 728 | brightness_factor += 1.0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 729 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 730 | // Intrapenalty below deals with situations where the intra and inter |
| 731 | // error scores are very low (e.g. a plain black frame). |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 732 | // We do not have special cases in first pass for 0,0 and nearest etc so |
| 733 | // all inter modes carry an overhead cost estimate for the mv. |
| 734 | // When the error score is very low this causes us to pick all or lots of |
| 735 | // INTRA modes and throw lots of key frames. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 736 | // This penalty adds a cost matching that of a 0,0 mv to the intra case. |
| 737 | this_error += intrapenalty; |
| 738 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 739 | // Accumulate the intra error. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 740 | intra_error += (int64_t)this_error; |
| 741 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 742 | #if CONFIG_FP_MB_STATS |
| 743 | if (cpi->use_fp_mb_stats) { |
| 744 | // initialization |
| 745 | cpi->twopass.frame_mb_stats_buf[mb_index] = 0; |
| 746 | } |
| 747 | #endif |
| 748 | |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 749 | // Set up limit values for motion vectors to prevent them extending |
| 750 | // outside the UMV borders. |
| 751 | x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16); |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 752 | x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16) + BORDER_MV_PIXELS_B16; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 753 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 754 | // Other than for the first frame do a motion search. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 755 | if ((lc == NULL && cm->current_video_frame > 0) || |
| 756 | (lc != NULL && lc->current_video_frame_in_layer > 0)) { |
| 757 | int tmp_err, motion_error, raw_motion_error; |
| 758 | // Assume 0,0 motion with no mv overhead. |
| 759 | MV mv = {0, 0} , tmp_mv = {0, 0}; |
| 760 | struct buf_2d unscaled_last_source_buf_2d; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 761 | |
Vignesh Venkatasubramanian | a72801d | 2014-03-20 16:30:56 -0700 | [diff] [blame] | 762 | xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 763 | #if CONFIG_VP9_HIGHBITDEPTH |
| 764 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 765 | motion_error = highbd_get_prediction_error( |
| 766 | bsize, &x->plane[0].src, &xd->plane[0].pre[0], xd->bd); |
| 767 | } else { |
| 768 | motion_error = get_prediction_error( |
| 769 | bsize, &x->plane[0].src, &xd->plane[0].pre[0]); |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 770 | } |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 771 | #else |
| 772 | motion_error = get_prediction_error( |
| 773 | bsize, &x->plane[0].src, &xd->plane[0].pre[0]); |
| 774 | #endif // CONFIG_VP9_HIGHBITDEPTH |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 775 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 776 | // Compute the motion error of the 0,0 motion using the last source |
| 777 | // frame as the reference. Skip the further motion search on |
| 778 | // reconstructed frame if this error is small. |
| 779 | unscaled_last_source_buf_2d.buf = |
| 780 | cpi->unscaled_last_source->y_buffer + recon_yoffset; |
| 781 | unscaled_last_source_buf_2d.stride = |
| 782 | cpi->unscaled_last_source->y_stride; |
| 783 | #if CONFIG_VP9_HIGHBITDEPTH |
| 784 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 785 | raw_motion_error = highbd_get_prediction_error( |
| 786 | bsize, &x->plane[0].src, &unscaled_last_source_buf_2d, xd->bd); |
| 787 | } else { |
| 788 | raw_motion_error = get_prediction_error( |
| 789 | bsize, &x->plane[0].src, &unscaled_last_source_buf_2d); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 790 | } |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 791 | #else |
| 792 | raw_motion_error = get_prediction_error( |
| 793 | bsize, &x->plane[0].src, &unscaled_last_source_buf_2d); |
| 794 | #endif // CONFIG_VP9_HIGHBITDEPTH |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 795 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 796 | // TODO(pengchong): Replace the hard-coded threshold |
| 797 | if (raw_motion_error > 25 || lc != NULL) { |
| 798 | // Test last reference frame using the previous best mv as the |
| 799 | // starting point (best reference) for the search. |
| 800 | first_pass_motion_search(cpi, x, &best_ref_mv, &mv, &motion_error); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 801 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 802 | // If the current best reference mv is not centered on 0,0 then do a |
| 803 | // 0,0 based search as well. |
| 804 | if (!is_zero_mv(&best_ref_mv)) { |
| 805 | tmp_err = INT_MAX; |
| 806 | first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv, &tmp_err); |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 807 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 808 | if (tmp_err < motion_error) { |
| 809 | motion_error = tmp_err; |
| 810 | mv = tmp_mv; |
| 811 | } |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 812 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 813 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 814 | // Search in an older reference frame. |
| 815 | if (((lc == NULL && cm->current_video_frame > 1) || |
| 816 | (lc != NULL && lc->current_video_frame_in_layer > 1)) |
| 817 | && gld_yv12 != NULL) { |
| 818 | // Assume 0,0 motion with no mv overhead. |
| 819 | int gf_motion_error; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 820 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 821 | xd->plane[0].pre[0].buf = gld_yv12->y_buffer + recon_yoffset; |
| 822 | #if CONFIG_VP9_HIGHBITDEPTH |
| 823 | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 824 | gf_motion_error = highbd_get_prediction_error( |
| 825 | bsize, &x->plane[0].src, &xd->plane[0].pre[0], xd->bd); |
| 826 | } else { |
| 827 | gf_motion_error = get_prediction_error( |
| 828 | bsize, &x->plane[0].src, &xd->plane[0].pre[0]); |
| 829 | } |
| 830 | #else |
| 831 | gf_motion_error = get_prediction_error( |
| 832 | bsize, &x->plane[0].src, &xd->plane[0].pre[0]); |
| 833 | #endif // CONFIG_VP9_HIGHBITDEPTH |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 834 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 835 | first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv, |
| 836 | &gf_motion_error); |
| 837 | |
| 838 | if (gf_motion_error < motion_error && gf_motion_error < this_error) |
| 839 | ++second_ref_count; |
| 840 | |
| 841 | // Reset to last frame as reference buffer. |
| 842 | xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset; |
| 843 | xd->plane[1].pre[0].buf = first_ref_buf->u_buffer + recon_uvoffset; |
| 844 | xd->plane[2].pre[0].buf = first_ref_buf->v_buffer + recon_uvoffset; |
| 845 | |
| 846 | // In accumulating a score for the older reference frame take the |
| 847 | // best of the motion predicted score and the intra coded error |
| 848 | // (just as will be done for) accumulation of "coded_error" for |
| 849 | // the last frame. |
| 850 | if (gf_motion_error < this_error) |
| 851 | sr_coded_error += gf_motion_error; |
| 852 | else |
| 853 | sr_coded_error += this_error; |
| 854 | } else { |
| 855 | sr_coded_error += motion_error; |
| 856 | } |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 857 | } else { |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 858 | sr_coded_error += motion_error; |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 859 | } |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 860 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 861 | // Start by assuming that intra mode is best. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 862 | best_ref_mv.row = 0; |
| 863 | best_ref_mv.col = 0; |
| 864 | |
| 865 | #if CONFIG_FP_MB_STATS |
| 866 | if (cpi->use_fp_mb_stats) { |
| 867 | // intra predication statistics |
| 868 | cpi->twopass.frame_mb_stats_buf[mb_index] = 0; |
| 869 | cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_DCINTRA_MASK; |
| 870 | cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_MOTION_ZERO_MASK; |
| 871 | if (this_error > FPMB_ERROR_LARGE_TH) { |
| 872 | cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_LARGE_MASK; |
| 873 | } else if (this_error < FPMB_ERROR_SMALL_TH) { |
| 874 | cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_SMALL_MASK; |
| 875 | } |
| 876 | } |
| 877 | #endif |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 878 | |
| 879 | if (motion_error <= this_error) { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 880 | vpx_clear_system_state(); |
| 881 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 882 | // Keep a count of cases where the inter and intra were very close |
| 883 | // and very low. This helps with scene cut detection for example in |
| 884 | // cropped clips with black bars at the sides or top and bottom. |
| 885 | if (((this_error - intrapenalty) * 9 <= motion_error * 10) && |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 886 | (this_error < (2 * intrapenalty))) { |
| 887 | neutral_count += 1.0; |
| 888 | // Also track cases where the intra is not much worse than the inter |
| 889 | // and use this in limiting the GF/arf group length. |
| 890 | } else if ((this_error > NCOUNT_INTRA_THRESH) && |
| 891 | (this_error < (NCOUNT_INTRA_FACTOR * motion_error))) { |
| 892 | neutral_count += (double)motion_error / |
| 893 | DOUBLE_DIVIDE_CHECK((double)this_error); |
| 894 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 895 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 896 | mv.row *= 8; |
| 897 | mv.col *= 8; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 898 | this_error = motion_error; |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 899 | xd->mi[0]->mbmi.mode = NEWMV; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 900 | xd->mi[0]->mbmi.mv[0].as_mv = mv; |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 901 | xd->mi[0]->mbmi.tx_size = TX_4X4; |
| 902 | xd->mi[0]->mbmi.ref_frame[0] = LAST_FRAME; |
| 903 | xd->mi[0]->mbmi.ref_frame[1] = NONE; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 904 | vp9_build_inter_predictors_sby(xd, mb_row << 1, mb_col << 1, bsize); |
| 905 | vp9_encode_sby_pass1(x, bsize); |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 906 | sum_mvr += mv.row; |
| 907 | sum_mvr_abs += abs(mv.row); |
| 908 | sum_mvc += mv.col; |
| 909 | sum_mvc_abs += abs(mv.col); |
| 910 | sum_mvrs += mv.row * mv.row; |
| 911 | sum_mvcs += mv.col * mv.col; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 912 | ++intercount; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 913 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 914 | best_ref_mv = mv; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 915 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 916 | #if CONFIG_FP_MB_STATS |
| 917 | if (cpi->use_fp_mb_stats) { |
| 918 | // inter predication statistics |
| 919 | cpi->twopass.frame_mb_stats_buf[mb_index] = 0; |
| 920 | cpi->twopass.frame_mb_stats_buf[mb_index] &= ~FPMB_DCINTRA_MASK; |
| 921 | cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_MOTION_ZERO_MASK; |
| 922 | if (this_error > FPMB_ERROR_LARGE_TH) { |
| 923 | cpi->twopass.frame_mb_stats_buf[mb_index] |= |
| 924 | FPMB_ERROR_LARGE_MASK; |
| 925 | } else if (this_error < FPMB_ERROR_SMALL_TH) { |
| 926 | cpi->twopass.frame_mb_stats_buf[mb_index] |= |
| 927 | FPMB_ERROR_SMALL_MASK; |
| 928 | } |
| 929 | } |
| 930 | #endif |
| 931 | |
| 932 | if (!is_zero_mv(&mv)) { |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 933 | ++mvcount; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 934 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 935 | #if CONFIG_FP_MB_STATS |
| 936 | if (cpi->use_fp_mb_stats) { |
| 937 | cpi->twopass.frame_mb_stats_buf[mb_index] &= |
| 938 | ~FPMB_MOTION_ZERO_MASK; |
| 939 | // check estimated motion direction |
| 940 | if (mv.as_mv.col > 0 && mv.as_mv.col >= abs(mv.as_mv.row)) { |
| 941 | // right direction |
| 942 | cpi->twopass.frame_mb_stats_buf[mb_index] |= |
| 943 | FPMB_MOTION_RIGHT_MASK; |
| 944 | } else if (mv.as_mv.row < 0 && |
| 945 | abs(mv.as_mv.row) >= abs(mv.as_mv.col)) { |
| 946 | // up direction |
| 947 | cpi->twopass.frame_mb_stats_buf[mb_index] |= |
| 948 | FPMB_MOTION_UP_MASK; |
| 949 | } else if (mv.as_mv.col < 0 && |
| 950 | abs(mv.as_mv.col) >= abs(mv.as_mv.row)) { |
| 951 | // left direction |
| 952 | cpi->twopass.frame_mb_stats_buf[mb_index] |= |
| 953 | FPMB_MOTION_LEFT_MASK; |
| 954 | } else { |
| 955 | // down direction |
| 956 | cpi->twopass.frame_mb_stats_buf[mb_index] |= |
| 957 | FPMB_MOTION_DOWN_MASK; |
| 958 | } |
| 959 | } |
| 960 | #endif |
| 961 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 962 | // Non-zero vector, was it different from the last non zero vector? |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 963 | if (!is_equal_mv(&mv, &lastmv)) |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 964 | ++new_mv_count; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 965 | lastmv = mv; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 966 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 967 | // Does the row vector point inwards or outwards? |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 968 | if (mb_row < cm->mb_rows / 2) { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 969 | if (mv.row > 0) |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 970 | --sum_in_vectors; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 971 | else if (mv.row < 0) |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 972 | ++sum_in_vectors; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 973 | } else if (mb_row > cm->mb_rows / 2) { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 974 | if (mv.row > 0) |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 975 | ++sum_in_vectors; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 976 | else if (mv.row < 0) |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 977 | --sum_in_vectors; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 978 | } |
| 979 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 980 | // Does the col vector point inwards or outwards? |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 981 | if (mb_col < cm->mb_cols / 2) { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 982 | if (mv.col > 0) |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 983 | --sum_in_vectors; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 984 | else if (mv.col < 0) |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 985 | ++sum_in_vectors; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 986 | } else if (mb_col > cm->mb_cols / 2) { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 987 | if (mv.col > 0) |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 988 | ++sum_in_vectors; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 989 | else if (mv.col < 0) |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 990 | --sum_in_vectors; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 991 | } |
| 992 | } |
| 993 | } |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 994 | } else { |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 995 | sr_coded_error += (int64_t)this_error; |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 996 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 997 | coded_error += (int64_t)this_error; |
| 998 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 999 | // Adjust to the next column of MBs. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1000 | x->plane[0].src.buf += 16; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1001 | x->plane[1].src.buf += uv_mb_height; |
| 1002 | x->plane[2].src.buf += uv_mb_height; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1003 | |
| 1004 | recon_yoffset += 16; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1005 | recon_uvoffset += uv_mb_height; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1008 | // Adjust to the next row of MBs. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1009 | x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1010 | x->plane[1].src.buf += uv_mb_height * x->plane[1].src.stride - |
| 1011 | uv_mb_height * cm->mb_cols; |
| 1012 | x->plane[2].src.buf += uv_mb_height * x->plane[1].src.stride - |
| 1013 | uv_mb_height * cm->mb_cols; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1014 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1015 | vpx_clear_system_state(); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1018 | // Clamp the image start to rows/2. This number of rows is discarded top |
| 1019 | // and bottom as dead data so rows / 2 means the frame is blank. |
| 1020 | if ((image_data_start_row > cm->mb_rows / 2) || |
| 1021 | (image_data_start_row == INVALID_ROW)) { |
| 1022 | image_data_start_row = cm->mb_rows / 2; |
| 1023 | } |
| 1024 | // Exclude any image dead zone |
| 1025 | if (image_data_start_row > 0) { |
| 1026 | intra_skip_count = |
| 1027 | MAX(0, intra_skip_count - (image_data_start_row * cm->mb_cols * 2)); |
| 1028 | } |
| 1029 | |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1030 | { |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1031 | FIRSTPASS_STATS fps; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1032 | // The minimum error here insures some bit allocation to frames even |
| 1033 | // in static regions. The allocation per MB declines for larger formats |
| 1034 | // where the typical "real" energy per MB also falls. |
| 1035 | // Initial estimate here uses sqrt(mbs) to define the min_err, where the |
| 1036 | // number of mbs is proportional to the image area. |
| 1037 | const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE) |
| 1038 | ? cpi->initial_mbs : cpi->common.MBs; |
| 1039 | const double min_err = 200 * sqrt(num_mbs); |
| 1040 | |
| 1041 | intra_factor = intra_factor / (double)num_mbs; |
| 1042 | brightness_factor = brightness_factor / (double)num_mbs; |
| 1043 | fps.weight = intra_factor * brightness_factor; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1044 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1045 | fps.frame = cm->current_video_frame; |
Vignesh Venkatasubramanian | a72801d | 2014-03-20 16:30:56 -0700 | [diff] [blame] | 1046 | fps.spatial_layer_id = cpi->svc.spatial_layer_id; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1047 | fps.coded_error = (double)(coded_error >> 8) + min_err; |
| 1048 | fps.sr_coded_error = (double)(sr_coded_error >> 8) + min_err; |
| 1049 | fps.intra_error = (double)(intra_error >> 8) + min_err; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1050 | fps.count = 1.0; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1051 | fps.pcnt_inter = (double)intercount / num_mbs; |
| 1052 | fps.pcnt_second_ref = (double)second_ref_count / num_mbs; |
| 1053 | fps.pcnt_neutral = (double)neutral_count / num_mbs; |
| 1054 | fps.intra_skip_pct = (double)intra_skip_count / num_mbs; |
| 1055 | fps.inactive_zone_rows = (double)image_data_start_row; |
| 1056 | fps.inactive_zone_cols = (double)0; // TODO(paulwilkins): fix |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1057 | |
| 1058 | if (mvcount > 0) { |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1059 | fps.MVr = (double)sum_mvr / mvcount; |
| 1060 | fps.mvr_abs = (double)sum_mvr_abs / mvcount; |
| 1061 | fps.MVc = (double)sum_mvc / mvcount; |
| 1062 | fps.mvc_abs = (double)sum_mvc_abs / mvcount; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1063 | fps.MVrv = ((double)sum_mvrs - |
| 1064 | ((double)sum_mvr * sum_mvr / mvcount)) / mvcount; |
| 1065 | fps.MVcv = ((double)sum_mvcs - |
| 1066 | ((double)sum_mvc * sum_mvc / mvcount)) / mvcount; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1067 | fps.mv_in_out_count = (double)sum_in_vectors / (mvcount * 2); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1068 | fps.new_mv_count = new_mv_count; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1069 | fps.pcnt_motion = (double)mvcount / num_mbs; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1070 | } else { |
| 1071 | fps.MVr = 0.0; |
| 1072 | fps.mvr_abs = 0.0; |
| 1073 | fps.MVc = 0.0; |
| 1074 | fps.mvc_abs = 0.0; |
| 1075 | fps.MVrv = 0.0; |
| 1076 | fps.MVcv = 0.0; |
| 1077 | fps.mv_in_out_count = 0.0; |
| 1078 | fps.new_mv_count = 0.0; |
| 1079 | fps.pcnt_motion = 0.0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 1082 | // TODO(paulwilkins): Handle the case when duration is set to 0, or |
| 1083 | // something less than the full time between subsequent values of |
| 1084 | // cpi->source_time_stamp. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1085 | fps.duration = (double)(source->ts_end - source->ts_start); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1086 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1087 | // Don't want to do output stats with a stack variable! |
| 1088 | twopass->this_frame_stats = fps; |
| 1089 | output_stats(&twopass->this_frame_stats, cpi->output_pkt_list); |
| 1090 | accumulate_stats(&twopass->total_stats, &fps); |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1091 | |
| 1092 | #if CONFIG_FP_MB_STATS |
| 1093 | if (cpi->use_fp_mb_stats) { |
| 1094 | output_fpmb_stats(twopass->frame_mb_stats_buf, cm, cpi->output_pkt_list); |
| 1095 | } |
| 1096 | #endif |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | // Copy the previous Last Frame back into gf and and arf buffers if |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1100 | // the prediction is good enough... but also don't allow it to lag too far. |
| 1101 | if ((twopass->sr_update_lag > 3) || |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1102 | ((cm->current_video_frame > 0) && |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1103 | (twopass->this_frame_stats.pcnt_inter > 0.20) && |
| 1104 | ((twopass->this_frame_stats.intra_error / |
| 1105 | DOUBLE_DIVIDE_CHECK(twopass->this_frame_stats.coded_error)) > 2.0))) { |
Vignesh Venkatasubramanian | a72801d | 2014-03-20 16:30:56 -0700 | [diff] [blame] | 1106 | if (gld_yv12 != NULL) { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1107 | ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx], |
| 1108 | cm->ref_frame_map[cpi->lst_fb_idx]); |
Vignesh Venkatasubramanian | a72801d | 2014-03-20 16:30:56 -0700 | [diff] [blame] | 1109 | } |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1110 | twopass->sr_update_lag = 1; |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 1111 | } else { |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1112 | ++twopass->sr_update_lag; |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 1113 | } |
Vignesh Venkatasubramanian | a72801d | 2014-03-20 16:30:56 -0700 | [diff] [blame] | 1114 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1115 | vpx_extend_frame_borders(new_yv12); |
| 1116 | |
| 1117 | if (lc != NULL) { |
Vignesh Venkatasubramanian | a72801d | 2014-03-20 16:30:56 -0700 | [diff] [blame] | 1118 | vp9_update_reference_frames(cpi); |
| 1119 | } else { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1120 | // The frame we just compressed now becomes the last frame. |
| 1121 | ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->lst_fb_idx], |
| 1122 | cm->new_fb_idx); |
Vignesh Venkatasubramanian | a72801d | 2014-03-20 16:30:56 -0700 | [diff] [blame] | 1123 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1124 | |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 1125 | // Special case for the first frame. Copy into the GF buffer as a second |
| 1126 | // reference. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1127 | if (cm->current_video_frame == 0 && cpi->gld_fb_idx != INVALID_IDX && |
| 1128 | lc == NULL) { |
| 1129 | ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx], |
| 1130 | cm->ref_frame_map[cpi->lst_fb_idx]); |
Vignesh Venkatasubramanian | a72801d | 2014-03-20 16:30:56 -0700 | [diff] [blame] | 1131 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1132 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1133 | // Use this to see what the first pass reconstruction looks like. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1134 | if (0) { |
| 1135 | char filename[512]; |
| 1136 | FILE *recon_file; |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 1137 | snprintf(filename, sizeof(filename), "enc%04d.yuv", |
| 1138 | (int)cm->current_video_frame); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1139 | |
| 1140 | if (cm->current_video_frame == 0) |
| 1141 | recon_file = fopen(filename, "wb"); |
| 1142 | else |
| 1143 | recon_file = fopen(filename, "ab"); |
| 1144 | |
| 1145 | (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file); |
| 1146 | fclose(recon_file); |
| 1147 | } |
| 1148 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1149 | ++cm->current_video_frame; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1150 | if (cpi->use_svc) |
| 1151 | vp9_inc_frame_in_layer(cpi); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | static double calc_correction_factor(double err_per_mb, |
| 1155 | double err_divisor, |
| 1156 | double pt_low, |
| 1157 | double pt_high, |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1158 | int q, |
| 1159 | vpx_bit_depth_t bit_depth) { |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1160 | const double error_term = err_per_mb / err_divisor; |
| 1161 | |
| 1162 | // Adjustment based on actual quantizer to power term. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1163 | const double power_term = |
| 1164 | MIN(vp9_convert_qindex_to_q(q, bit_depth) * 0.01 + pt_low, pt_high); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1165 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1166 | // Calculate correction factor. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1167 | if (power_term < 1.0) |
| 1168 | assert(error_term >= 0.0); |
| 1169 | |
| 1170 | return fclamp(pow(error_term, power_term), 0.05, 5.0); |
| 1171 | } |
| 1172 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1173 | // Larger image formats are expected to be a little harder to code relatively |
| 1174 | // given the same prediction error score. This in part at least relates to the |
| 1175 | // increased size and hence coding cost of motion vectors. |
| 1176 | #define EDIV_SIZE_FACTOR 800 |
| 1177 | |
| 1178 | static int get_twopass_worst_quality(const VP9_COMP *cpi, |
| 1179 | const double section_err, |
| 1180 | double inactive_zone, |
| 1181 | int section_target_bandwidth, |
| 1182 | double group_weight_factor) { |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1183 | const RATE_CONTROL *const rc = &cpi->rc; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1184 | const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1185 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1186 | inactive_zone = fclamp(inactive_zone, 0.0, 1.0); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1187 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1188 | if (section_target_bandwidth <= 0) { |
| 1189 | return rc->worst_quality; // Highest value allowed |
| 1190 | } else { |
| 1191 | const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE) |
| 1192 | ? cpi->initial_mbs : cpi->common.MBs; |
| 1193 | const int active_mbs = MAX(1, num_mbs - (int)(num_mbs * inactive_zone)); |
| 1194 | const double av_err_per_mb = section_err / active_mbs; |
| 1195 | const double speed_term = 1.0 + 0.04 * oxcf->speed; |
| 1196 | const double ediv_size_correction = (double)num_mbs / EDIV_SIZE_FACTOR; |
| 1197 | const int target_norm_bits_per_mb = ((uint64_t)section_target_bandwidth << |
| 1198 | BPER_MB_NORMBITS) / active_mbs; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1199 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1200 | int q; |
| 1201 | int is_svc_upper_layer = 0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1202 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1203 | if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0) |
| 1204 | is_svc_upper_layer = 1; |
| 1205 | |
| 1206 | |
| 1207 | // Try and pick a max Q that will be high enough to encode the |
| 1208 | // content at the given rate. |
| 1209 | for (q = rc->best_quality; q < rc->worst_quality; ++q) { |
| 1210 | const double factor = |
| 1211 | calc_correction_factor(av_err_per_mb, |
| 1212 | ERR_DIVISOR - ediv_size_correction, |
| 1213 | is_svc_upper_layer ? SVC_FACTOR_PT_LOW : |
| 1214 | FACTOR_PT_LOW, FACTOR_PT_HIGH, q, |
| 1215 | cpi->common.bit_depth); |
| 1216 | const int bits_per_mb = |
| 1217 | vp9_rc_bits_per_mb(INTER_FRAME, q, |
| 1218 | factor * speed_term * group_weight_factor, |
| 1219 | cpi->common.bit_depth); |
| 1220 | if (bits_per_mb <= target_norm_bits_per_mb) |
| 1221 | break; |
| 1222 | } |
| 1223 | |
| 1224 | // Restriction on active max q for constrained quality mode. |
| 1225 | if (cpi->oxcf.rc_mode == VPX_CQ) |
| 1226 | q = MAX(q, oxcf->cq_level); |
| 1227 | return q; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1228 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1229 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1230 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1231 | static void setup_rf_level_maxq(VP9_COMP *cpi) { |
| 1232 | int i; |
| 1233 | RATE_CONTROL *const rc = &cpi->rc; |
| 1234 | for (i = INTER_NORMAL; i < RATE_FACTOR_LEVELS; ++i) { |
| 1235 | int qdelta = vp9_frame_type_qdelta(cpi, i, rc->worst_quality); |
| 1236 | rc->rf_level_maxq[i] = MAX(rc->worst_quality + qdelta, rc->best_quality); |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | void vp9_init_subsampling(VP9_COMP *cpi) { |
| 1241 | const VP9_COMMON *const cm = &cpi->common; |
| 1242 | RATE_CONTROL *const rc = &cpi->rc; |
| 1243 | const int w = cm->width; |
| 1244 | const int h = cm->height; |
| 1245 | int i; |
| 1246 | |
| 1247 | for (i = 0; i < FRAME_SCALE_STEPS; ++i) { |
| 1248 | // Note: Frames with odd-sized dimensions may result from this scaling. |
| 1249 | rc->frame_width[i] = (w * 16) / frame_scale_factor[i]; |
| 1250 | rc->frame_height[i] = (h * 16) / frame_scale_factor[i]; |
| 1251 | } |
| 1252 | |
| 1253 | setup_rf_level_maxq(cpi); |
| 1254 | } |
| 1255 | |
| 1256 | void calculate_coded_size(VP9_COMP *cpi, |
| 1257 | int *scaled_frame_width, |
| 1258 | int *scaled_frame_height) { |
| 1259 | RATE_CONTROL *const rc = &cpi->rc; |
| 1260 | *scaled_frame_width = rc->frame_width[rc->frame_size_selector]; |
| 1261 | *scaled_frame_height = rc->frame_height[rc->frame_size_selector]; |
| 1262 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1263 | |
| 1264 | void vp9_init_second_pass(VP9_COMP *cpi) { |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 1265 | SVC *const svc = &cpi->svc; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1266 | const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 1267 | const int is_two_pass_svc = (svc->number_spatial_layers > 1) || |
| 1268 | (svc->number_temporal_layers > 1); |
| 1269 | TWO_PASS *const twopass = is_two_pass_svc ? |
| 1270 | &svc->layer_context[svc->spatial_layer_id].twopass : &cpi->twopass; |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 1271 | double frame_rate; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1272 | FIRSTPASS_STATS *stats; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1273 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1274 | zero_stats(&twopass->total_stats); |
| 1275 | zero_stats(&twopass->total_left_stats); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1276 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1277 | if (!twopass->stats_in_end) |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1278 | return; |
| 1279 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1280 | stats = &twopass->total_stats; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1281 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1282 | *stats = *twopass->stats_in_end; |
| 1283 | twopass->total_left_stats = *stats; |
| 1284 | |
| 1285 | frame_rate = 10000000.0 * stats->count / stats->duration; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1286 | // Each frame can have a different duration, as the frame rate in the source |
| 1287 | // isn't guaranteed to be constant. The frame rate prior to the first frame |
| 1288 | // encoded in the second pass is a guess. However, the sum duration is not. |
| 1289 | // It is calculated based on the actual durations of all frames from the |
| 1290 | // first pass. |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 1291 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1292 | if (is_two_pass_svc) { |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 1293 | vp9_update_spatial_layer_framerate(cpi, frame_rate); |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1294 | twopass->bits_left = (int64_t)(stats->duration * |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 1295 | svc->layer_context[svc->spatial_layer_id].target_bandwidth / |
| 1296 | 10000000.0); |
| 1297 | } else { |
| 1298 | vp9_new_framerate(cpi, frame_rate); |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1299 | twopass->bits_left = (int64_t)(stats->duration * oxcf->target_bandwidth / |
| 1300 | 10000000.0); |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 1301 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1302 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1303 | // This variable monitors how far behind the second ref update is lagging. |
| 1304 | twopass->sr_update_lag = 1; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1305 | |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 1306 | // Scan the first pass file and calculate a modified total error based upon |
| 1307 | // the bias/power function used to allocate bits. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1308 | { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1309 | const double avg_error = stats->coded_error / |
| 1310 | DOUBLE_DIVIDE_CHECK(stats->count); |
| 1311 | const FIRSTPASS_STATS *s = twopass->stats_in; |
| 1312 | double modified_error_total = 0.0; |
| 1313 | twopass->modified_error_min = (avg_error * |
| 1314 | oxcf->two_pass_vbrmin_section) / 100; |
| 1315 | twopass->modified_error_max = (avg_error * |
| 1316 | oxcf->two_pass_vbrmax_section) / 100; |
| 1317 | while (s < twopass->stats_in_end) { |
| 1318 | modified_error_total += calculate_modified_err(cpi, twopass, oxcf, s); |
| 1319 | ++s; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1320 | } |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1321 | twopass->modified_error_left = modified_error_total; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1322 | } |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1323 | |
| 1324 | // Reset the vbr bits off target counters |
| 1325 | cpi->rc.vbr_bits_off_target = 0; |
| 1326 | cpi->rc.vbr_bits_off_target_fast = 0; |
| 1327 | |
| 1328 | cpi->rc.rate_error_estimate = 0; |
| 1329 | |
| 1330 | // Static sequence monitor variables. |
| 1331 | twopass->kf_zeromotion_pct = 100; |
| 1332 | twopass->last_kfgroup_zeromotion_pct = 100; |
| 1333 | |
| 1334 | if (oxcf->resize_mode != RESIZE_NONE) { |
| 1335 | vp9_init_subsampling(cpi); |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | #define SR_DIFF_PART 0.0015 |
| 1340 | #define MOTION_AMP_PART 0.003 |
| 1341 | #define INTRA_PART 0.005 |
| 1342 | #define DEFAULT_DECAY_LIMIT 0.75 |
| 1343 | #define LOW_SR_DIFF_TRHESH 0.1 |
| 1344 | #define SR_DIFF_MAX 128.0 |
| 1345 | |
| 1346 | static double get_sr_decay_rate(const VP9_COMP *cpi, |
| 1347 | const FIRSTPASS_STATS *frame) { |
| 1348 | const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE) |
| 1349 | ? cpi->initial_mbs : cpi->common.MBs; |
| 1350 | double sr_diff = |
| 1351 | (frame->sr_coded_error - frame->coded_error) / num_mbs; |
| 1352 | double sr_decay = 1.0; |
| 1353 | double modified_pct_inter; |
| 1354 | double modified_pcnt_intra; |
| 1355 | const double motion_amplitude_factor = |
| 1356 | frame->pcnt_motion * ((frame->mvc_abs + frame->mvr_abs) / 2); |
| 1357 | |
| 1358 | modified_pct_inter = frame->pcnt_inter; |
| 1359 | if ((frame->intra_error / DOUBLE_DIVIDE_CHECK(frame->coded_error)) < |
| 1360 | (double)NCOUNT_FRAME_II_THRESH) { |
| 1361 | modified_pct_inter = frame->pcnt_inter - frame->pcnt_neutral; |
| 1362 | } |
| 1363 | modified_pcnt_intra = 100 * (1.0 - modified_pct_inter); |
| 1364 | |
| 1365 | |
| 1366 | if ((sr_diff > LOW_SR_DIFF_TRHESH)) { |
| 1367 | sr_diff = MIN(sr_diff, SR_DIFF_MAX); |
| 1368 | sr_decay = 1.0 - (SR_DIFF_PART * sr_diff) - |
| 1369 | (MOTION_AMP_PART * motion_amplitude_factor) - |
| 1370 | (INTRA_PART * modified_pcnt_intra); |
| 1371 | } |
| 1372 | return MAX(sr_decay, MIN(DEFAULT_DECAY_LIMIT, modified_pct_inter)); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1373 | } |
| 1374 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1375 | // This function gives an estimate of how badly we believe the prediction |
| 1376 | // quality is decaying from frame to frame. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1377 | static double get_zero_motion_factor(const VP9_COMP *cpi, |
| 1378 | const FIRSTPASS_STATS *frame) { |
| 1379 | const double zero_motion_pct = frame->pcnt_inter - |
| 1380 | frame->pcnt_motion; |
| 1381 | double sr_decay = get_sr_decay_rate(cpi, frame); |
| 1382 | return MIN(sr_decay, zero_motion_pct); |
| 1383 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1384 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1385 | #define ZM_POWER_FACTOR 0.75 |
| 1386 | |
| 1387 | static double get_prediction_decay_rate(const VP9_COMP *cpi, |
| 1388 | const FIRSTPASS_STATS *next_frame) { |
| 1389 | const double sr_decay_rate = get_sr_decay_rate(cpi, next_frame); |
| 1390 | const double zero_motion_factor = |
| 1391 | (0.95 * pow((next_frame->pcnt_inter - next_frame->pcnt_motion), |
| 1392 | ZM_POWER_FACTOR)); |
| 1393 | |
| 1394 | return MAX(zero_motion_factor, |
| 1395 | (sr_decay_rate + ((1.0 - sr_decay_rate) * zero_motion_factor))); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | // Function to test for a condition where a complex transition is followed |
| 1399 | // by a static section. For example in slide shows where there is a fade |
| 1400 | // between slides. This is to help with more optimal kf and gf positioning. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1401 | static int detect_transition_to_still(VP9_COMP *cpi, |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 1402 | int frame_interval, int still_interval, |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1403 | double loop_decay_rate, |
| 1404 | double last_decay_rate) { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1405 | TWO_PASS *const twopass = &cpi->twopass; |
| 1406 | RATE_CONTROL *const rc = &cpi->rc; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1407 | |
| 1408 | // Break clause to detect very still sections after motion |
| 1409 | // For example a static image after a fade or other transition |
| 1410 | // instead of a clean scene cut. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1411 | if (frame_interval > rc->min_gf_interval && |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1412 | loop_decay_rate >= 0.999 && |
| 1413 | last_decay_rate < 0.9) { |
| 1414 | int j; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1415 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1416 | // Look ahead a few frames to see if static condition persists... |
| 1417 | for (j = 0; j < still_interval; ++j) { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1418 | const FIRSTPASS_STATS *stats = &twopass->stats_in[j]; |
| 1419 | if (stats >= twopass->stats_in_end) |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1420 | break; |
| 1421 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1422 | if (stats->pcnt_inter - stats->pcnt_motion < 0.999) |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1423 | break; |
| 1424 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1425 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1426 | // Only if it does do we signal a transition to still. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1427 | return j == still_interval; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1428 | } |
| 1429 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1430 | return 0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | // This function detects a flash through the high relative pcnt_second_ref |
| 1434 | // score in the frame following a flash frame. The offset passed in should |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1435 | // reflect this. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1436 | static int detect_flash(const TWO_PASS *twopass, int offset) { |
| 1437 | const FIRSTPASS_STATS *const next_frame = read_frame_stats(twopass, offset); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1438 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1439 | // What we are looking for here is a situation where there is a |
| 1440 | // brief break in prediction (such as a flash) but subsequent frames |
| 1441 | // are reasonably well predicted by an earlier (pre flash) frame. |
| 1442 | // The recovery after a flash is indicated by a high pcnt_second_ref |
| 1443 | // compared to pcnt_inter. |
| 1444 | return next_frame != NULL && |
| 1445 | next_frame->pcnt_second_ref > next_frame->pcnt_inter && |
| 1446 | next_frame->pcnt_second_ref >= 0.5; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1447 | } |
| 1448 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1449 | // Update the motion related elements to the GF arf boost calculation. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1450 | static void accumulate_frame_motion_stats(const FIRSTPASS_STATS *stats, |
| 1451 | double *mv_in_out, |
| 1452 | double *mv_in_out_accumulator, |
| 1453 | double *abs_mv_in_out_accumulator, |
| 1454 | double *mv_ratio_accumulator) { |
| 1455 | const double pct = stats->pcnt_motion; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1456 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1457 | // Accumulate Motion In/Out of frame stats. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1458 | *mv_in_out = stats->mv_in_out_count * pct; |
| 1459 | *mv_in_out_accumulator += *mv_in_out; |
| 1460 | *abs_mv_in_out_accumulator += fabs(*mv_in_out); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1461 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1462 | // Accumulate a measure of how uniform (or conversely how random) the motion |
| 1463 | // field is (a ratio of abs(mv) / mv). |
| 1464 | if (pct > 0.05) { |
| 1465 | const double mvr_ratio = fabs(stats->mvr_abs) / |
| 1466 | DOUBLE_DIVIDE_CHECK(fabs(stats->MVr)); |
| 1467 | const double mvc_ratio = fabs(stats->mvc_abs) / |
| 1468 | DOUBLE_DIVIDE_CHECK(fabs(stats->MVc)); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1469 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1470 | *mv_ratio_accumulator += pct * (mvr_ratio < stats->mvr_abs ? |
| 1471 | mvr_ratio : stats->mvr_abs); |
| 1472 | *mv_ratio_accumulator += pct * (mvc_ratio < stats->mvc_abs ? |
| 1473 | mvc_ratio : stats->mvc_abs); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1474 | } |
| 1475 | } |
| 1476 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1477 | #define BASELINE_ERR_PER_MB 1000.0 |
| 1478 | static double calc_frame_boost(VP9_COMP *cpi, |
| 1479 | const FIRSTPASS_STATS *this_frame, |
| 1480 | double this_frame_mv_in_out, |
| 1481 | double max_boost) { |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1482 | double frame_boost; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1483 | const double lq = |
| 1484 | vp9_convert_qindex_to_q(cpi->rc.avg_frame_qindex[INTER_FRAME], |
| 1485 | cpi->common.bit_depth); |
| 1486 | const double boost_q_correction = MIN((0.5 + (lq * 0.015)), 1.5); |
| 1487 | int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE) |
| 1488 | ? cpi->initial_mbs : cpi->common.MBs; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1489 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1490 | // Correct for any inactive region in the image |
| 1491 | num_mbs = (int)MAX(1, num_mbs * calculate_active_area(cpi, this_frame)); |
| 1492 | |
| 1493 | // Underlying boost factor is based on inter error ratio. |
| 1494 | frame_boost = (BASELINE_ERR_PER_MB * num_mbs) / |
| 1495 | DOUBLE_DIVIDE_CHECK(this_frame->coded_error); |
| 1496 | frame_boost = frame_boost * BOOST_FACTOR * boost_q_correction; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1497 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1498 | // Increase boost for frames where new data coming into frame (e.g. zoom out). |
| 1499 | // Slightly reduce boost if there is a net balance of motion out of the frame |
| 1500 | // (zoom in). The range for this_frame_mv_in_out is -1.0 to +1.0. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1501 | if (this_frame_mv_in_out > 0.0) |
| 1502 | frame_boost += frame_boost * (this_frame_mv_in_out * 2.0); |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1503 | // In the extreme case the boost is halved. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1504 | else |
| 1505 | frame_boost += frame_boost * (this_frame_mv_in_out / 2.0); |
| 1506 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1507 | return MIN(frame_boost, max_boost * boost_q_correction); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | static int calc_arf_boost(VP9_COMP *cpi, int offset, |
| 1511 | int f_frames, int b_frames, |
| 1512 | int *f_boost, int *b_boost) { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1513 | TWO_PASS *const twopass = &cpi->twopass; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1514 | int i; |
| 1515 | double boost_score = 0.0; |
| 1516 | double mv_ratio_accumulator = 0.0; |
| 1517 | double decay_accumulator = 1.0; |
| 1518 | double this_frame_mv_in_out = 0.0; |
| 1519 | double mv_in_out_accumulator = 0.0; |
| 1520 | double abs_mv_in_out_accumulator = 0.0; |
| 1521 | int arf_boost; |
| 1522 | int flash_detected = 0; |
| 1523 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1524 | // Search forward from the proposed arf/next gf position. |
| 1525 | for (i = 0; i < f_frames; ++i) { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1526 | const FIRSTPASS_STATS *this_frame = read_frame_stats(twopass, i + offset); |
| 1527 | if (this_frame == NULL) |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1528 | break; |
| 1529 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1530 | // Update the motion related elements to the boost calculation. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1531 | accumulate_frame_motion_stats(this_frame, |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1532 | &this_frame_mv_in_out, &mv_in_out_accumulator, |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 1533 | &abs_mv_in_out_accumulator, |
| 1534 | &mv_ratio_accumulator); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1535 | |
| 1536 | // We want to discount the flash frame itself and the recovery |
| 1537 | // frame that follows as both will have poor scores. |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1538 | flash_detected = detect_flash(twopass, i + offset) || |
| 1539 | detect_flash(twopass, i + offset + 1); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1540 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1541 | // Accumulate the effect of prediction quality decay. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1542 | if (!flash_detected) { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1543 | decay_accumulator *= get_prediction_decay_rate(cpi, this_frame); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1544 | decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR |
| 1545 | ? MIN_DECAY_FACTOR : decay_accumulator; |
| 1546 | } |
| 1547 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1548 | boost_score += decay_accumulator * calc_frame_boost(cpi, this_frame, |
| 1549 | this_frame_mv_in_out, |
| 1550 | GF_MAX_BOOST); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | *f_boost = (int)boost_score; |
| 1554 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1555 | // Reset for backward looking loop. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1556 | boost_score = 0.0; |
| 1557 | mv_ratio_accumulator = 0.0; |
| 1558 | decay_accumulator = 1.0; |
| 1559 | this_frame_mv_in_out = 0.0; |
| 1560 | mv_in_out_accumulator = 0.0; |
| 1561 | abs_mv_in_out_accumulator = 0.0; |
| 1562 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1563 | // Search backward towards last gf position. |
| 1564 | for (i = -1; i >= -b_frames; --i) { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1565 | const FIRSTPASS_STATS *this_frame = read_frame_stats(twopass, i + offset); |
| 1566 | if (this_frame == NULL) |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1567 | break; |
| 1568 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1569 | // Update the motion related elements to the boost calculation. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1570 | accumulate_frame_motion_stats(this_frame, |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1571 | &this_frame_mv_in_out, &mv_in_out_accumulator, |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 1572 | &abs_mv_in_out_accumulator, |
| 1573 | &mv_ratio_accumulator); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1574 | |
| 1575 | // We want to discount the the flash frame itself and the recovery |
| 1576 | // frame that follows as both will have poor scores. |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1577 | flash_detected = detect_flash(twopass, i + offset) || |
| 1578 | detect_flash(twopass, i + offset + 1); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1579 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1580 | // Cumulative effect of prediction quality decay. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1581 | if (!flash_detected) { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1582 | decay_accumulator *= get_prediction_decay_rate(cpi, this_frame); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1583 | decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR |
| 1584 | ? MIN_DECAY_FACTOR : decay_accumulator; |
| 1585 | } |
| 1586 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1587 | boost_score += decay_accumulator * calc_frame_boost(cpi, this_frame, |
| 1588 | this_frame_mv_in_out, |
| 1589 | GF_MAX_BOOST); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1590 | } |
| 1591 | *b_boost = (int)boost_score; |
| 1592 | |
| 1593 | arf_boost = (*f_boost + *b_boost); |
| 1594 | if (arf_boost < ((b_frames + f_frames) * 20)) |
| 1595 | arf_boost = ((b_frames + f_frames) * 20); |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1596 | arf_boost = MAX(arf_boost, MIN_ARF_GF_BOOST); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1597 | |
| 1598 | return arf_boost; |
| 1599 | } |
| 1600 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1601 | // Calculate a section intra ratio used in setting max loop filter. |
| 1602 | static int calculate_section_intra_ratio(const FIRSTPASS_STATS *begin, |
| 1603 | const FIRSTPASS_STATS *end, |
| 1604 | int section_length) { |
| 1605 | const FIRSTPASS_STATS *s = begin; |
| 1606 | double intra_error = 0.0; |
| 1607 | double coded_error = 0.0; |
| 1608 | int i = 0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1609 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1610 | while (s < end && i < section_length) { |
| 1611 | intra_error += s->intra_error; |
| 1612 | coded_error += s->coded_error; |
| 1613 | ++s; |
| 1614 | ++i; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1615 | } |
| 1616 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1617 | return (int)(intra_error / DOUBLE_DIVIDE_CHECK(coded_error)); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1618 | } |
| 1619 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1620 | // Calculate the total bits to allocate in this GF/ARF group. |
| 1621 | static int64_t calculate_total_gf_group_bits(VP9_COMP *cpi, |
| 1622 | double gf_group_err) { |
| 1623 | const RATE_CONTROL *const rc = &cpi->rc; |
| 1624 | const TWO_PASS *const twopass = &cpi->twopass; |
| 1625 | const int max_bits = frame_max_bits(rc, &cpi->oxcf); |
| 1626 | int64_t total_group_bits; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1627 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1628 | // Calculate the bits to be allocated to the group as a whole. |
| 1629 | if ((twopass->kf_group_bits > 0) && (twopass->kf_group_error_left > 0)) { |
| 1630 | total_group_bits = (int64_t)(twopass->kf_group_bits * |
| 1631 | (gf_group_err / twopass->kf_group_error_left)); |
| 1632 | } else { |
| 1633 | total_group_bits = 0; |
| 1634 | } |
| 1635 | |
| 1636 | // Clamp odd edge cases. |
| 1637 | total_group_bits = (total_group_bits < 0) ? |
| 1638 | 0 : (total_group_bits > twopass->kf_group_bits) ? |
| 1639 | twopass->kf_group_bits : total_group_bits; |
| 1640 | |
| 1641 | // Clip based on user supplied data rate variability limit. |
| 1642 | if (total_group_bits > (int64_t)max_bits * rc->baseline_gf_interval) |
| 1643 | total_group_bits = (int64_t)max_bits * rc->baseline_gf_interval; |
| 1644 | |
| 1645 | return total_group_bits; |
| 1646 | } |
| 1647 | |
| 1648 | // Calculate the number bits extra to assign to boosted frames in a group. |
| 1649 | static int calculate_boost_bits(int frame_count, |
| 1650 | int boost, int64_t total_group_bits) { |
| 1651 | int allocation_chunks; |
| 1652 | |
| 1653 | // return 0 for invalid inputs (could arise e.g. through rounding errors) |
| 1654 | if (!boost || (total_group_bits <= 0) || (frame_count <= 0) ) |
| 1655 | return 0; |
| 1656 | |
| 1657 | allocation_chunks = (frame_count * 100) + boost; |
| 1658 | |
| 1659 | // Prevent overflow. |
| 1660 | if (boost > 1023) { |
| 1661 | int divisor = boost >> 10; |
| 1662 | boost /= divisor; |
| 1663 | allocation_chunks /= divisor; |
| 1664 | } |
| 1665 | |
| 1666 | // Calculate the number of extra bits for use in the boosted frame or frames. |
| 1667 | return MAX((int)(((int64_t)boost * total_group_bits) / allocation_chunks), 0); |
| 1668 | } |
| 1669 | |
| 1670 | // Current limit on maximum number of active arfs in a GF/ARF group. |
| 1671 | #define MAX_ACTIVE_ARFS 2 |
| 1672 | #define ARF_SLOT1 2 |
| 1673 | #define ARF_SLOT2 3 |
| 1674 | // This function indirects the choice of buffers for arfs. |
| 1675 | // At the moment the values are fixed but this may change as part of |
| 1676 | // the integration process with other codec features that swap buffers around. |
| 1677 | static void get_arf_buffer_indices(unsigned char *arf_buffer_indices) { |
| 1678 | arf_buffer_indices[0] = ARF_SLOT1; |
| 1679 | arf_buffer_indices[1] = ARF_SLOT2; |
| 1680 | } |
| 1681 | |
| 1682 | static void allocate_gf_group_bits(VP9_COMP *cpi, int64_t gf_group_bits, |
| 1683 | double group_error, int gf_arf_bits) { |
| 1684 | RATE_CONTROL *const rc = &cpi->rc; |
| 1685 | const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 1686 | TWO_PASS *const twopass = &cpi->twopass; |
| 1687 | GF_GROUP *const gf_group = &twopass->gf_group; |
| 1688 | FIRSTPASS_STATS frame_stats; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1689 | int i; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1690 | int frame_index = 1; |
| 1691 | int target_frame_size; |
| 1692 | int key_frame; |
| 1693 | const int max_bits = frame_max_bits(&cpi->rc, &cpi->oxcf); |
| 1694 | int64_t total_group_bits = gf_group_bits; |
| 1695 | double modified_err = 0.0; |
| 1696 | double err_fraction; |
| 1697 | int mid_boost_bits = 0; |
| 1698 | int mid_frame_idx; |
| 1699 | unsigned char arf_buffer_indices[MAX_ACTIVE_ARFS]; |
| 1700 | int alt_frame_index = frame_index; |
| 1701 | int has_temporal_layers = is_two_pass_svc(cpi) && |
| 1702 | cpi->svc.number_temporal_layers > 1; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1703 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1704 | // Only encode alt reference frame in temporal base layer. |
| 1705 | if (has_temporal_layers) |
| 1706 | alt_frame_index = cpi->svc.number_temporal_layers; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1707 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1708 | key_frame = cpi->common.frame_type == KEY_FRAME || |
| 1709 | vp9_is_upper_layer_key_frame(cpi); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1710 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1711 | get_arf_buffer_indices(arf_buffer_indices); |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1712 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1713 | // For key frames the frame target rate is already set and it |
| 1714 | // is also the golden frame. |
| 1715 | if (!key_frame) { |
| 1716 | if (rc->source_alt_ref_active) { |
| 1717 | gf_group->update_type[0] = OVERLAY_UPDATE; |
| 1718 | gf_group->rf_level[0] = INTER_NORMAL; |
| 1719 | gf_group->bit_allocation[0] = 0; |
| 1720 | gf_group->arf_update_idx[0] = arf_buffer_indices[0]; |
| 1721 | gf_group->arf_ref_idx[0] = arf_buffer_indices[0]; |
| 1722 | } else { |
| 1723 | gf_group->update_type[0] = GF_UPDATE; |
| 1724 | gf_group->rf_level[0] = GF_ARF_STD; |
| 1725 | gf_group->bit_allocation[0] = gf_arf_bits; |
| 1726 | gf_group->arf_update_idx[0] = arf_buffer_indices[0]; |
| 1727 | gf_group->arf_ref_idx[0] = arf_buffer_indices[0]; |
| 1728 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1729 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1730 | // Step over the golden frame / overlay frame |
| 1731 | if (EOF == input_stats(twopass, &frame_stats)) |
| 1732 | return; |
| 1733 | } |
| 1734 | |
| 1735 | // Deduct the boost bits for arf (or gf if it is not a key frame) |
| 1736 | // from the group total. |
| 1737 | if (rc->source_alt_ref_pending || !key_frame) |
| 1738 | total_group_bits -= gf_arf_bits; |
| 1739 | |
| 1740 | // Store the bits to spend on the ARF if there is one. |
| 1741 | if (rc->source_alt_ref_pending) { |
| 1742 | gf_group->update_type[alt_frame_index] = ARF_UPDATE; |
| 1743 | gf_group->rf_level[alt_frame_index] = GF_ARF_STD; |
| 1744 | gf_group->bit_allocation[alt_frame_index] = gf_arf_bits; |
| 1745 | |
| 1746 | if (has_temporal_layers) |
| 1747 | gf_group->arf_src_offset[alt_frame_index] = |
| 1748 | (unsigned char)(rc->baseline_gf_interval - |
| 1749 | cpi->svc.number_temporal_layers); |
| 1750 | else |
| 1751 | gf_group->arf_src_offset[alt_frame_index] = |
| 1752 | (unsigned char)(rc->baseline_gf_interval - 1); |
| 1753 | |
| 1754 | gf_group->arf_update_idx[alt_frame_index] = arf_buffer_indices[0]; |
| 1755 | gf_group->arf_ref_idx[alt_frame_index] = |
| 1756 | arf_buffer_indices[cpi->multi_arf_last_grp_enabled && |
| 1757 | rc->source_alt_ref_active]; |
| 1758 | if (!has_temporal_layers) |
| 1759 | ++frame_index; |
| 1760 | |
| 1761 | if (cpi->multi_arf_enabled) { |
| 1762 | // Set aside a slot for a level 1 arf. |
| 1763 | gf_group->update_type[frame_index] = ARF_UPDATE; |
| 1764 | gf_group->rf_level[frame_index] = GF_ARF_LOW; |
| 1765 | gf_group->arf_src_offset[frame_index] = |
| 1766 | (unsigned char)((rc->baseline_gf_interval >> 1) - 1); |
| 1767 | gf_group->arf_update_idx[frame_index] = arf_buffer_indices[1]; |
| 1768 | gf_group->arf_ref_idx[frame_index] = arf_buffer_indices[0]; |
| 1769 | ++frame_index; |
| 1770 | } |
| 1771 | } |
| 1772 | |
| 1773 | // Define middle frame |
| 1774 | mid_frame_idx = frame_index + (rc->baseline_gf_interval >> 1) - 1; |
| 1775 | |
| 1776 | // Allocate bits to the other frames in the group. |
| 1777 | for (i = 0; i < rc->baseline_gf_interval - rc->source_alt_ref_pending; ++i) { |
| 1778 | int arf_idx = 0; |
| 1779 | if (EOF == input_stats(twopass, &frame_stats)) |
| 1780 | break; |
| 1781 | |
| 1782 | if (has_temporal_layers && frame_index == alt_frame_index) { |
| 1783 | ++frame_index; |
| 1784 | } |
| 1785 | |
| 1786 | modified_err = calculate_modified_err(cpi, twopass, oxcf, &frame_stats); |
| 1787 | |
| 1788 | if (group_error > 0) |
| 1789 | err_fraction = modified_err / DOUBLE_DIVIDE_CHECK(group_error); |
| 1790 | else |
| 1791 | err_fraction = 0.0; |
| 1792 | |
| 1793 | target_frame_size = (int)((double)total_group_bits * err_fraction); |
| 1794 | |
| 1795 | if (rc->source_alt_ref_pending && cpi->multi_arf_enabled) { |
| 1796 | mid_boost_bits += (target_frame_size >> 4); |
| 1797 | target_frame_size -= (target_frame_size >> 4); |
| 1798 | |
| 1799 | if (frame_index <= mid_frame_idx) |
| 1800 | arf_idx = 1; |
| 1801 | } |
| 1802 | gf_group->arf_update_idx[frame_index] = arf_buffer_indices[arf_idx]; |
| 1803 | gf_group->arf_ref_idx[frame_index] = arf_buffer_indices[arf_idx]; |
| 1804 | |
| 1805 | target_frame_size = clamp(target_frame_size, 0, |
| 1806 | MIN(max_bits, (int)total_group_bits)); |
| 1807 | |
| 1808 | gf_group->update_type[frame_index] = LF_UPDATE; |
| 1809 | gf_group->rf_level[frame_index] = INTER_NORMAL; |
| 1810 | |
| 1811 | gf_group->bit_allocation[frame_index] = target_frame_size; |
| 1812 | ++frame_index; |
| 1813 | } |
| 1814 | |
| 1815 | // Note: |
| 1816 | // We need to configure the frame at the end of the sequence + 1 that will be |
| 1817 | // the start frame for the next group. Otherwise prior to the call to |
| 1818 | // vp9_rc_get_second_pass_params() the data will be undefined. |
| 1819 | gf_group->arf_update_idx[frame_index] = arf_buffer_indices[0]; |
| 1820 | gf_group->arf_ref_idx[frame_index] = arf_buffer_indices[0]; |
| 1821 | |
| 1822 | if (rc->source_alt_ref_pending) { |
| 1823 | gf_group->update_type[frame_index] = OVERLAY_UPDATE; |
| 1824 | gf_group->rf_level[frame_index] = INTER_NORMAL; |
| 1825 | |
| 1826 | // Final setup for second arf and its overlay. |
| 1827 | if (cpi->multi_arf_enabled) { |
| 1828 | gf_group->bit_allocation[2] = |
| 1829 | gf_group->bit_allocation[mid_frame_idx] + mid_boost_bits; |
| 1830 | gf_group->update_type[mid_frame_idx] = OVERLAY_UPDATE; |
| 1831 | gf_group->bit_allocation[mid_frame_idx] = 0; |
| 1832 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1833 | } else { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1834 | gf_group->update_type[frame_index] = GF_UPDATE; |
| 1835 | gf_group->rf_level[frame_index] = GF_ARF_STD; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1836 | } |
| 1837 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1838 | // Note whether multi-arf was enabled this group for next time. |
| 1839 | cpi->multi_arf_last_grp_enabled = cpi->multi_arf_enabled; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1840 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1841 | |
| 1842 | // Analyse and define a gf/arf group. |
| 1843 | static void define_gf_group(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1844 | VP9_COMMON *const cm = &cpi->common; |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 1845 | RATE_CONTROL *const rc = &cpi->rc; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1846 | VP9EncoderConfig *const oxcf = &cpi->oxcf; |
| 1847 | TWO_PASS *const twopass = &cpi->twopass; |
| 1848 | FIRSTPASS_STATS next_frame; |
| 1849 | const FIRSTPASS_STATS *const start_pos = twopass->stats_in; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1850 | int i; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1851 | |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1852 | double boost_score = 0.0; |
| 1853 | double old_boost_score = 0.0; |
| 1854 | double gf_group_err = 0.0; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1855 | #if GROUP_ADAPTIVE_MAXQ |
| 1856 | double gf_group_raw_error = 0.0; |
| 1857 | #endif |
| 1858 | double gf_group_skip_pct = 0.0; |
| 1859 | double gf_group_inactive_zone_rows = 0.0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1860 | double gf_first_frame_err = 0.0; |
| 1861 | double mod_frame_err = 0.0; |
| 1862 | |
| 1863 | double mv_ratio_accumulator = 0.0; |
| 1864 | double decay_accumulator = 1.0; |
| 1865 | double zero_motion_accumulator = 1.0; |
| 1866 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1867 | double loop_decay_rate = 1.00; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1868 | double last_loop_decay_rate = 1.00; |
| 1869 | |
| 1870 | double this_frame_mv_in_out = 0.0; |
| 1871 | double mv_in_out_accumulator = 0.0; |
| 1872 | double abs_mv_in_out_accumulator = 0.0; |
| 1873 | double mv_ratio_accumulator_thresh; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1874 | unsigned int allow_alt_ref = is_altref_enabled(cpi); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1875 | |
| 1876 | int f_boost = 0; |
| 1877 | int b_boost = 0; |
| 1878 | int flash_detected; |
| 1879 | int active_max_gf_interval; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1880 | int active_min_gf_interval; |
| 1881 | int64_t gf_group_bits; |
| 1882 | double gf_group_error_left; |
| 1883 | int gf_arf_bits; |
| 1884 | const int is_key_frame = frame_is_intra_only(cm); |
| 1885 | const int arf_active_or_kf = is_key_frame || rc->source_alt_ref_active; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1886 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1887 | // Reset the GF group data structures unless this is a key |
| 1888 | // frame in which case it will already have been done. |
| 1889 | if (is_key_frame == 0) { |
| 1890 | vp9_zero(twopass->gf_group); |
| 1891 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1892 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1893 | vpx_clear_system_state(); |
| 1894 | vp9_zero(next_frame); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1895 | |
| 1896 | // Load stats for the current frame. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1897 | mod_frame_err = calculate_modified_err(cpi, twopass, oxcf, this_frame); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1898 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1899 | // Note the error of the frame at the start of the group. This will be |
| 1900 | // the GF frame error if we code a normal gf. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1901 | gf_first_frame_err = mod_frame_err; |
| 1902 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1903 | // If this is a key frame or the overlay from a previous arf then |
| 1904 | // the error score / cost of this frame has already been accounted for. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1905 | if (arf_active_or_kf) { |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1906 | gf_group_err -= gf_first_frame_err; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1907 | #if GROUP_ADAPTIVE_MAXQ |
| 1908 | gf_group_raw_error -= this_frame->coded_error; |
| 1909 | #endif |
| 1910 | gf_group_skip_pct -= this_frame->intra_skip_pct; |
| 1911 | gf_group_inactive_zone_rows -= this_frame->inactive_zone_rows; |
| 1912 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1913 | |
| 1914 | // Motion breakout threshold for loop below depends on image size. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1915 | mv_ratio_accumulator_thresh = |
| 1916 | (cpi->initial_height + cpi->initial_width) / 4.0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1917 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1918 | // Set a maximum and minimum interval for the GF group. |
| 1919 | // If the image appears almost completely static we can extend beyond this. |
| 1920 | { |
| 1921 | int int_max_q = |
| 1922 | (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality, |
| 1923 | cpi->common.bit_depth)); |
| 1924 | int int_lbq = |
| 1925 | (int)(vp9_convert_qindex_to_q(rc->last_boosted_qindex, |
| 1926 | cpi->common.bit_depth)); |
| 1927 | active_min_gf_interval = rc->min_gf_interval + MIN(2, int_max_q / 200); |
| 1928 | if (active_min_gf_interval > rc->max_gf_interval) |
| 1929 | active_min_gf_interval = rc->max_gf_interval; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1930 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1931 | if (cpi->multi_arf_allowed) { |
| 1932 | active_max_gf_interval = rc->max_gf_interval; |
| 1933 | } else { |
| 1934 | // The value chosen depends on the active Q range. At low Q we have |
| 1935 | // bits to spare and are better with a smaller interval and smaller boost. |
| 1936 | // At high Q when there are few bits to spare we are better with a longer |
| 1937 | // interval to spread the cost of the GF. |
| 1938 | active_max_gf_interval = 12 + MIN(4, (int_lbq / 6)); |
| 1939 | if (active_max_gf_interval < active_min_gf_interval) |
| 1940 | active_max_gf_interval = active_min_gf_interval; |
| 1941 | |
| 1942 | if (active_max_gf_interval > rc->max_gf_interval) |
| 1943 | active_max_gf_interval = rc->max_gf_interval; |
| 1944 | if (active_max_gf_interval < active_min_gf_interval) |
| 1945 | active_max_gf_interval = active_min_gf_interval; |
| 1946 | } |
| 1947 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1948 | |
| 1949 | i = 0; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1950 | while (i < rc->static_scene_max_gf_interval && i < rc->frames_to_key) { |
| 1951 | ++i; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1952 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1953 | // Accumulate error score of frames in this gf group. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1954 | mod_frame_err = calculate_modified_err(cpi, twopass, oxcf, this_frame); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1955 | gf_group_err += mod_frame_err; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1956 | #if GROUP_ADAPTIVE_MAXQ |
| 1957 | gf_group_raw_error += this_frame->coded_error; |
| 1958 | #endif |
| 1959 | gf_group_skip_pct += this_frame->intra_skip_pct; |
| 1960 | gf_group_inactive_zone_rows += this_frame->inactive_zone_rows; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1961 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1962 | if (EOF == input_stats(twopass, &next_frame)) |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1963 | break; |
| 1964 | |
| 1965 | // Test for the case where there is a brief flash but the prediction |
| 1966 | // quality back to an earlier frame is then restored. |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1967 | flash_detected = detect_flash(twopass, 0); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1968 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1969 | // Update the motion related elements to the boost calculation. |
hkuang | f3bed91 | 2013-08-06 11:07:19 -0700 | [diff] [blame] | 1970 | accumulate_frame_motion_stats(&next_frame, |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1971 | &this_frame_mv_in_out, &mv_in_out_accumulator, |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 1972 | &abs_mv_in_out_accumulator, |
| 1973 | &mv_ratio_accumulator); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1974 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1975 | // Accumulate the effect of prediction quality decay. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1976 | if (!flash_detected) { |
| 1977 | last_loop_decay_rate = loop_decay_rate; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1978 | loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame); |
| 1979 | |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1980 | decay_accumulator = decay_accumulator * loop_decay_rate; |
| 1981 | |
| 1982 | // Monitor for static sections. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1983 | zero_motion_accumulator = |
| 1984 | MIN(zero_motion_accumulator, get_zero_motion_factor(cpi, &next_frame)); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1985 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1986 | // Break clause to detect very still sections after motion. For example, |
| 1987 | // a static image after a fade or other transition. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1988 | if (detect_transition_to_still(cpi, i, 5, loop_decay_rate, |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1989 | last_loop_decay_rate)) { |
| 1990 | allow_alt_ref = 0; |
| 1991 | break; |
| 1992 | } |
| 1993 | } |
| 1994 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 1995 | // Calculate a boost number for this frame. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 1996 | boost_score += decay_accumulator * calc_frame_boost(cpi, &next_frame, |
| 1997 | this_frame_mv_in_out, |
| 1998 | GF_MAX_BOOST); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 1999 | |
| 2000 | // Break out conditions. |
| 2001 | if ( |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2002 | // Break at active_max_gf_interval unless almost totally static. |
| 2003 | (i >= (active_max_gf_interval + arf_active_or_kf) && |
| 2004 | zero_motion_accumulator < 0.995) || |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2005 | ( |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2006 | // Don't break out with a very short interval. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2007 | (i >= active_min_gf_interval + arf_active_or_kf) && |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2008 | (!flash_detected) && |
| 2009 | ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) || |
| 2010 | (abs_mv_in_out_accumulator > 3.0) || |
| 2011 | (mv_in_out_accumulator < -2.0) || |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2012 | ((boost_score - old_boost_score) < BOOST_BREAKOUT)))) { |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2013 | boost_score = old_boost_score; |
| 2014 | break; |
| 2015 | } |
| 2016 | |
| 2017 | *this_frame = next_frame; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2018 | old_boost_score = boost_score; |
| 2019 | } |
hkuang | 1184aeb | 2013-09-16 15:09:58 -0700 | [diff] [blame] | 2020 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2021 | twopass->gf_zeromotion_pct = (int)(zero_motion_accumulator * 1000.0); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2022 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2023 | // Was the group length constrained by the requirement for a new KF? |
| 2024 | rc->constrained_gf_group = (i >= rc->frames_to_key) ? 1 : 0; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2025 | |
| 2026 | // Should we use the alternate reference frame. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2027 | if (allow_alt_ref && |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2028 | (i < cpi->oxcf.lag_in_frames) && |
| 2029 | (i >= rc->min_gf_interval)) { |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2030 | // Calculate the boost for alt ref. |
| 2031 | rc->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost, |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2032 | &b_boost); |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2033 | rc->source_alt_ref_pending = 1; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2034 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2035 | // Test to see if multi arf is appropriate. |
| 2036 | cpi->multi_arf_enabled = |
| 2037 | (cpi->multi_arf_allowed && (rc->baseline_gf_interval >= 6) && |
| 2038 | (zero_motion_accumulator < 0.995)) ? 1 : 0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2039 | } else { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2040 | rc->gfu_boost = MAX((int)boost_score, MIN_ARF_GF_BOOST); |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2041 | rc->source_alt_ref_pending = 0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2042 | } |
| 2043 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2044 | // Set the interval until the next gf. |
| 2045 | rc->baseline_gf_interval = i - (is_key_frame || rc->source_alt_ref_pending); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2046 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2047 | // Only encode alt reference frame in temporal base layer. So |
| 2048 | // baseline_gf_interval should be multiple of a temporal layer group |
| 2049 | // (typically the frame distance between two base layer frames) |
| 2050 | if (is_two_pass_svc(cpi) && cpi->svc.number_temporal_layers > 1) { |
| 2051 | int count = (1 << (cpi->svc.number_temporal_layers - 1)) - 1; |
| 2052 | int new_gf_interval = (rc->baseline_gf_interval + count) & (~count); |
| 2053 | int j; |
| 2054 | for (j = 0; j < new_gf_interval - rc->baseline_gf_interval; ++j) { |
| 2055 | if (EOF == input_stats(twopass, this_frame)) |
| 2056 | break; |
| 2057 | gf_group_err += calculate_modified_err(cpi, twopass, oxcf, this_frame); |
| 2058 | #if GROUP_ADAPTIVE_MAXQ |
| 2059 | gf_group_raw_error += this_frame->coded_error; |
| 2060 | #endif |
| 2061 | gf_group_skip_pct += this_frame->intra_skip_pct; |
| 2062 | gf_group_inactive_zone_rows += this_frame->inactive_zone_rows; |
| 2063 | } |
| 2064 | rc->baseline_gf_interval = new_gf_interval; |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 2065 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2066 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2067 | rc->frames_till_gf_update_due = rc->baseline_gf_interval; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2068 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2069 | // Reset the file position. |
| 2070 | reset_fpf_position(twopass, start_pos); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2071 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2072 | // Calculate the bits to be allocated to the gf/arf group as a whole |
| 2073 | gf_group_bits = calculate_total_gf_group_bits(cpi, gf_group_err); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2074 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2075 | #if GROUP_ADAPTIVE_MAXQ |
| 2076 | // Calculate an estimate of the maxq needed for the group. |
| 2077 | // We are more agressive about correcting for sections |
| 2078 | // where there could be significant overshoot than for easier |
| 2079 | // sections where we do not wish to risk creating an overshoot |
| 2080 | // of the allocated bit budget. |
| 2081 | if ((cpi->oxcf.rc_mode != VPX_Q) && (rc->baseline_gf_interval > 1)) { |
| 2082 | const int vbr_group_bits_per_frame = |
| 2083 | (int)(gf_group_bits / rc->baseline_gf_interval); |
| 2084 | const double group_av_err = gf_group_raw_error / rc->baseline_gf_interval; |
| 2085 | const double group_av_skip_pct = |
| 2086 | gf_group_skip_pct / rc->baseline_gf_interval; |
| 2087 | const double group_av_inactive_zone = |
| 2088 | ((gf_group_inactive_zone_rows * 2) / |
| 2089 | (rc->baseline_gf_interval * (double)cm->mb_rows)); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2090 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2091 | int tmp_q; |
| 2092 | // rc factor is a weight factor that corrects for local rate control drift. |
| 2093 | double rc_factor = 1.0; |
| 2094 | if (rc->rate_error_estimate > 0) { |
| 2095 | rc_factor = MAX(RC_FACTOR_MIN, |
| 2096 | (double)(100 - rc->rate_error_estimate) / 100.0); |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 2097 | } else { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2098 | rc_factor = MIN(RC_FACTOR_MAX, |
| 2099 | (double)(100 - rc->rate_error_estimate) / 100.0); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2100 | } |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2101 | tmp_q = |
| 2102 | get_twopass_worst_quality(cpi, group_av_err, |
| 2103 | (group_av_skip_pct + group_av_inactive_zone), |
| 2104 | vbr_group_bits_per_frame, |
| 2105 | twopass->kfgroup_inter_fraction * rc_factor); |
| 2106 | twopass->active_worst_quality = |
| 2107 | MAX(tmp_q, twopass->active_worst_quality >> 1); |
| 2108 | } |
| 2109 | #endif |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2110 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2111 | // Calculate the extra bits to be used for boosted frame(s) |
| 2112 | gf_arf_bits = calculate_boost_bits(rc->baseline_gf_interval, |
| 2113 | rc->gfu_boost, gf_group_bits); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2114 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2115 | // Adjust KF group bits and error remaining. |
| 2116 | twopass->kf_group_error_left -= (int64_t)gf_group_err; |
| 2117 | |
| 2118 | // If this is an arf update we want to remove the score for the overlay |
| 2119 | // frame at the end which will usually be very cheap to code. |
| 2120 | // The overlay frame has already, in effect, been coded so we want to spread |
| 2121 | // the remaining bits among the other frames. |
| 2122 | // For normal GFs remove the score for the GF itself unless this is |
| 2123 | // also a key frame in which case it has already been accounted for. |
| 2124 | if (rc->source_alt_ref_pending) { |
| 2125 | gf_group_error_left = gf_group_err - mod_frame_err; |
| 2126 | } else if (is_key_frame == 0) { |
| 2127 | gf_group_error_left = gf_group_err - gf_first_frame_err; |
| 2128 | } else { |
| 2129 | gf_group_error_left = gf_group_err; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2130 | } |
| 2131 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2132 | // Allocate bits to each of the frames in the GF group. |
| 2133 | allocate_gf_group_bits(cpi, gf_group_bits, gf_group_error_left, gf_arf_bits); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2134 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2135 | // Reset the file position. |
| 2136 | reset_fpf_position(twopass, start_pos); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2137 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2138 | // Calculate a section intra ratio used in setting max loop filter. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2139 | if (cpi->common.frame_type != KEY_FRAME) { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2140 | twopass->section_intra_rating = |
| 2141 | calculate_section_intra_ratio(start_pos, twopass->stats_in_end, |
| 2142 | rc->baseline_gf_interval); |
| 2143 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2144 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2145 | if (oxcf->resize_mode == RESIZE_DYNAMIC) { |
| 2146 | // Default to starting GF groups at normal frame size. |
| 2147 | cpi->rc.next_frame_size_selector = UNSCALED; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2148 | } |
| 2149 | } |
| 2150 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2151 | // Threshold for use of the lagging second reference frame. High second ref |
| 2152 | // usage may point to a transient event like a flash or occlusion rather than |
| 2153 | // a real scene cut. |
| 2154 | #define SECOND_REF_USEAGE_THRESH 0.1 |
| 2155 | // Minimum % intra coding observed in first pass (1.0 = 100%) |
| 2156 | #define MIN_INTRA_LEVEL 0.25 |
| 2157 | // Minimum ratio between the % of intra coding and inter coding in the first |
| 2158 | // pass after discounting neutral blocks (discounting neutral blocks in this |
| 2159 | // way helps catch scene cuts in clips with very flat areas or letter box |
| 2160 | // format clips with image padding. |
| 2161 | #define INTRA_VS_INTER_THRESH 2.0 |
| 2162 | // Hard threshold where the first pass chooses intra for almost all blocks. |
| 2163 | // In such a case even if the frame is not a scene cut coding a key frame |
| 2164 | // may be a good option. |
| 2165 | #define VERY_LOW_INTER_THRESH 0.05 |
| 2166 | // Maximum threshold for the relative ratio of intra error score vs best |
| 2167 | // inter error score. |
| 2168 | #define KF_II_ERR_THRESHOLD 2.5 |
| 2169 | // In real scene cuts there is almost always a sharp change in the intra |
| 2170 | // or inter error score. |
| 2171 | #define ERR_CHANGE_THRESHOLD 0.4 |
| 2172 | // For real scene cuts we expect an improvment in the intra inter error |
| 2173 | // ratio in the next frame. |
| 2174 | #define II_IMPROVEMENT_THRESHOLD 3.5 |
| 2175 | #define KF_II_MAX 128.0 |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2176 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2177 | static int test_candidate_kf(TWO_PASS *twopass, |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2178 | const FIRSTPASS_STATS *last_frame, |
| 2179 | const FIRSTPASS_STATS *this_frame, |
| 2180 | const FIRSTPASS_STATS *next_frame) { |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2181 | int is_viable_kf = 0; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2182 | double pcnt_intra = 1.0 - this_frame->pcnt_inter; |
| 2183 | double modified_pcnt_inter = |
| 2184 | this_frame->pcnt_inter - this_frame->pcnt_neutral; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2185 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2186 | // Does the frame satisfy the primary criteria of a key frame? |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2187 | // See above for an explanation of the test criteria. |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2188 | // If so, then examine how well it predicts subsequent frames. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2189 | if ((this_frame->pcnt_second_ref < SECOND_REF_USEAGE_THRESH) && |
| 2190 | (next_frame->pcnt_second_ref < SECOND_REF_USEAGE_THRESH) && |
| 2191 | ((this_frame->pcnt_inter < VERY_LOW_INTER_THRESH) || |
| 2192 | ((pcnt_intra > MIN_INTRA_LEVEL) && |
| 2193 | (pcnt_intra > (INTRA_VS_INTER_THRESH * modified_pcnt_inter)) && |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 2194 | ((this_frame->intra_error / |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2195 | DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < |
| 2196 | KF_II_ERR_THRESHOLD) && |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 2197 | ((fabs(last_frame->coded_error - this_frame->coded_error) / |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2198 | DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > |
| 2199 | ERR_CHANGE_THRESHOLD) || |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 2200 | (fabs(last_frame->intra_error - this_frame->intra_error) / |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2201 | DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > |
| 2202 | ERR_CHANGE_THRESHOLD) || |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 2203 | ((next_frame->intra_error / |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2204 | DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > |
| 2205 | II_IMPROVEMENT_THRESHOLD))))) { |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2206 | int i; |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 2207 | const FIRSTPASS_STATS *start_pos = twopass->stats_in; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2208 | FIRSTPASS_STATS local_next_frame = *next_frame; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2209 | double boost_score = 0.0; |
| 2210 | double old_boost_score = 0.0; |
| 2211 | double decay_accumulator = 1.0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2212 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2213 | // Examine how well the key frame predicts subsequent frames. |
| 2214 | for (i = 0; i < 16; ++i) { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2215 | double next_iiratio = (BOOST_FACTOR * local_next_frame.intra_error / |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2216 | DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error)); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2217 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2218 | if (next_iiratio > KF_II_MAX) |
| 2219 | next_iiratio = KF_II_MAX; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2220 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2221 | // Cumulative effect of decay in prediction quality. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2222 | if (local_next_frame.pcnt_inter > 0.85) |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2223 | decay_accumulator *= local_next_frame.pcnt_inter; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2224 | else |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2225 | decay_accumulator *= (0.85 + local_next_frame.pcnt_inter) / 2.0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2226 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2227 | // Keep a running total. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2228 | boost_score += (decay_accumulator * next_iiratio); |
| 2229 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2230 | // Test various breakout clauses. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2231 | if ((local_next_frame.pcnt_inter < 0.05) || |
| 2232 | (next_iiratio < 1.5) || |
| 2233 | (((local_next_frame.pcnt_inter - |
| 2234 | local_next_frame.pcnt_neutral) < 0.20) && |
| 2235 | (next_iiratio < 3.0)) || |
| 2236 | ((boost_score - old_boost_score) < 3.0) || |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2237 | (local_next_frame.intra_error < 200)) { |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2238 | break; |
| 2239 | } |
| 2240 | |
| 2241 | old_boost_score = boost_score; |
| 2242 | |
| 2243 | // Get the next frame details |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 2244 | if (EOF == input_stats(twopass, &local_next_frame)) |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2245 | break; |
| 2246 | } |
| 2247 | |
| 2248 | // If there is tolerable prediction for at least the next 3 frames then |
| 2249 | // break out else discard this potential key frame and move on |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 2250 | if (boost_score > 30.0 && (i > 3)) { |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2251 | is_viable_kf = 1; |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 2252 | } else { |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2253 | // Reset the file position |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 2254 | reset_fpf_position(twopass, start_pos); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2255 | |
| 2256 | is_viable_kf = 0; |
| 2257 | } |
| 2258 | } |
| 2259 | |
| 2260 | return is_viable_kf; |
| 2261 | } |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2262 | |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2263 | static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) { |
| 2264 | int i, j; |
Vignesh Venkatasubramanian | a72801d | 2014-03-20 16:30:56 -0700 | [diff] [blame] | 2265 | RATE_CONTROL *const rc = &cpi->rc; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2266 | TWO_PASS *const twopass = &cpi->twopass; |
| 2267 | GF_GROUP *const gf_group = &twopass->gf_group; |
| 2268 | const VP9EncoderConfig *const oxcf = &cpi->oxcf; |
Vignesh Venkatasubramanian | a72801d | 2014-03-20 16:30:56 -0700 | [diff] [blame] | 2269 | const FIRSTPASS_STATS first_frame = *this_frame; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2270 | const FIRSTPASS_STATS *const start_position = twopass->stats_in; |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 2271 | FIRSTPASS_STATS next_frame; |
| 2272 | FIRSTPASS_STATS last_frame; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2273 | int kf_bits = 0; |
| 2274 | int loop_decay_counter = 0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2275 | double decay_accumulator = 1.0; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2276 | double av_decay_accumulator = 0.0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2277 | double zero_motion_accumulator = 1.0; |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 2278 | double boost_score = 0.0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2279 | double kf_mod_err = 0.0; |
| 2280 | double kf_group_err = 0.0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2281 | double recent_loop_decay[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2282 | |
hkuang | f3bed91 | 2013-08-06 11:07:19 -0700 | [diff] [blame] | 2283 | vp9_zero(next_frame); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2284 | |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2285 | cpi->common.frame_type = KEY_FRAME; |
| 2286 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2287 | // Reset the GF group data structures. |
| 2288 | vp9_zero(*gf_group); |
| 2289 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2290 | // Is this a forced key frame by interval. |
| 2291 | rc->this_key_frame_forced = rc->next_key_frame_forced; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2292 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2293 | // Clear the alt ref active flag and last group multi arf flags as they |
| 2294 | // can never be set for a key frame. |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2295 | rc->source_alt_ref_active = 0; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2296 | cpi->multi_arf_last_grp_enabled = 0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2297 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2298 | // KF is always a GF so clear frames till next gf counter. |
| 2299 | rc->frames_till_gf_update_due = 0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2300 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2301 | rc->frames_to_key = 1; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2302 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2303 | twopass->kf_group_bits = 0; // Total bits available to kf group |
| 2304 | twopass->kf_group_error_left = 0; // Group modified error score. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2305 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2306 | kf_mod_err = calculate_modified_err(cpi, twopass, oxcf, this_frame); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2307 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2308 | // Find the next keyframe. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2309 | i = 0; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2310 | while (twopass->stats_in < twopass->stats_in_end && |
| 2311 | rc->frames_to_key < cpi->oxcf.key_freq) { |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2312 | // Accumulate kf group error. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2313 | kf_group_err += calculate_modified_err(cpi, twopass, oxcf, this_frame); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2314 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2315 | // Load the next frame's stats. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2316 | last_frame = *this_frame; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2317 | input_stats(twopass, this_frame); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2318 | |
| 2319 | // Provided that we are not at the end of the file... |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2320 | if (cpi->oxcf.auto_key && twopass->stats_in < twopass->stats_in_end) { |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 2321 | double loop_decay_rate; |
| 2322 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2323 | // Check for a scene cut. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2324 | if (test_candidate_kf(twopass, &last_frame, this_frame, |
| 2325 | twopass->stats_in)) |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2326 | break; |
| 2327 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2328 | // How fast is the prediction quality decaying? |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2329 | loop_decay_rate = get_prediction_decay_rate(cpi, twopass->stats_in); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2330 | |
| 2331 | // We want to know something about the recent past... rather than |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2332 | // as used elsewhere where we are concerned with decay in prediction |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2333 | // quality since the last GF or KF. |
| 2334 | recent_loop_decay[i % 8] = loop_decay_rate; |
| 2335 | decay_accumulator = 1.0; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2336 | for (j = 0; j < 8; ++j) |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2337 | decay_accumulator *= recent_loop_decay[j]; |
| 2338 | |
| 2339 | // Special check for transition or high motion followed by a |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2340 | // static scene. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2341 | if (detect_transition_to_still(cpi, i, cpi->oxcf.key_freq - i, |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2342 | loop_decay_rate, decay_accumulator)) |
| 2343 | break; |
| 2344 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2345 | // Step on to the next frame. |
| 2346 | ++rc->frames_to_key; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2347 | |
| 2348 | // If we don't have a real key frame within the next two |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2349 | // key_freq intervals then break out of the loop. |
| 2350 | if (rc->frames_to_key >= 2 * cpi->oxcf.key_freq) |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2351 | break; |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 2352 | } else { |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2353 | ++rc->frames_to_key; |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 2354 | } |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2355 | ++i; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | // If there is a max kf interval set by the user we must obey it. |
| 2359 | // We already breakout of the loop above at 2x max. |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2360 | // This code centers the extra kf if the actual natural interval |
| 2361 | // is between 1x and 2x. |
| 2362 | if (cpi->oxcf.auto_key && |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2363 | rc->frames_to_key > cpi->oxcf.key_freq) { |
Vignesh Venkatasubramanian | a72801d | 2014-03-20 16:30:56 -0700 | [diff] [blame] | 2364 | FIRSTPASS_STATS tmp_frame = first_frame; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2365 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2366 | rc->frames_to_key /= 2; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2367 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2368 | // Reset to the start of the group. |
| 2369 | reset_fpf_position(twopass, start_position); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2370 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2371 | kf_group_err = 0.0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2372 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2373 | // Rescan to get the correct error data for the forced kf group. |
| 2374 | for (i = 0; i < rc->frames_to_key; ++i) { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2375 | kf_group_err += calculate_modified_err(cpi, twopass, oxcf, &tmp_frame); |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2376 | input_stats(twopass, &tmp_frame); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2377 | } |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2378 | rc->next_key_frame_forced = 1; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2379 | } else if (twopass->stats_in == twopass->stats_in_end || |
| 2380 | rc->frames_to_key >= cpi->oxcf.key_freq) { |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2381 | rc->next_key_frame_forced = 1; |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 2382 | } else { |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2383 | rc->next_key_frame_forced = 0; |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 2384 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2385 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2386 | if (is_two_pass_svc(cpi) && cpi->svc.number_temporal_layers > 1) { |
| 2387 | int count = (1 << (cpi->svc.number_temporal_layers - 1)) - 1; |
| 2388 | int new_frame_to_key = (rc->frames_to_key + count) & (~count); |
| 2389 | int j; |
| 2390 | for (j = 0; j < new_frame_to_key - rc->frames_to_key; ++j) { |
| 2391 | if (EOF == input_stats(twopass, this_frame)) |
| 2392 | break; |
| 2393 | kf_group_err += calculate_modified_err(cpi, twopass, oxcf, this_frame); |
| 2394 | } |
| 2395 | rc->frames_to_key = new_frame_to_key; |
| 2396 | } |
| 2397 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2398 | // Special case for the last key frame of the file. |
| 2399 | if (twopass->stats_in >= twopass->stats_in_end) { |
| 2400 | // Accumulate kf group error. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2401 | kf_group_err += calculate_modified_err(cpi, twopass, oxcf, this_frame); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2402 | } |
| 2403 | |
| 2404 | // Calculate the number of bits that should be assigned to the kf group. |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2405 | if (twopass->bits_left > 0 && twopass->modified_error_left > 0.0) { |
| 2406 | // Maximum number of bits for a single normal frame (not key frame). |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 2407 | const int max_bits = frame_max_bits(rc, &cpi->oxcf); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2408 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2409 | // Maximum number of bits allocated to the key frame group. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2410 | int64_t max_grp_bits; |
| 2411 | |
| 2412 | // Default allocation based on bits left and relative |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2413 | // complexity of the section. |
| 2414 | twopass->kf_group_bits = (int64_t)(twopass->bits_left * |
| 2415 | (kf_group_err / twopass->modified_error_left)); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2416 | |
| 2417 | // Clip based on maximum per frame rate defined by the user. |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2418 | max_grp_bits = (int64_t)max_bits * (int64_t)rc->frames_to_key; |
| 2419 | if (twopass->kf_group_bits > max_grp_bits) |
| 2420 | twopass->kf_group_bits = max_grp_bits; |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 2421 | } else { |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2422 | twopass->kf_group_bits = 0; |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 2423 | } |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2424 | twopass->kf_group_bits = MAX(0, twopass->kf_group_bits); |
| 2425 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2426 | // Reset the first pass file position. |
| 2427 | reset_fpf_position(twopass, start_position); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2428 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2429 | // Scan through the kf group collating various stats used to determine |
| 2430 | // how many bits to spend on it. |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2431 | decay_accumulator = 1.0; |
| 2432 | boost_score = 0.0; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2433 | for (i = 0; i < (rc->frames_to_key - 1); ++i) { |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2434 | if (EOF == input_stats(twopass, &next_frame)) |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2435 | break; |
| 2436 | |
| 2437 | // Monitor for static sections. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2438 | zero_motion_accumulator = |
| 2439 | MIN(zero_motion_accumulator, |
| 2440 | get_zero_motion_factor(cpi, &next_frame)); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2441 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2442 | // Not all frames in the group are necessarily used in calculating boost. |
| 2443 | if ((i <= rc->max_gf_interval) || |
| 2444 | ((i <= (rc->max_gf_interval * 4)) && (decay_accumulator > 0.5))) { |
| 2445 | const double frame_boost = |
| 2446 | calc_frame_boost(cpi, this_frame, 0, KF_MAX_BOOST); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2447 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2448 | // How fast is prediction quality decaying. |
| 2449 | if (!detect_flash(twopass, 0)) { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2450 | const double loop_decay_rate = |
| 2451 | get_prediction_decay_rate(cpi, &next_frame); |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2452 | decay_accumulator *= loop_decay_rate; |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 2453 | decay_accumulator = MAX(decay_accumulator, MIN_DECAY_FACTOR); |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2454 | av_decay_accumulator += decay_accumulator; |
| 2455 | ++loop_decay_counter; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2456 | } |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2457 | boost_score += (decay_accumulator * frame_boost); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2458 | } |
| 2459 | } |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2460 | av_decay_accumulator /= (double)loop_decay_counter; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2461 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2462 | reset_fpf_position(twopass, start_position); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2463 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2464 | // Store the zero motion percentage |
| 2465 | twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0); |
| 2466 | |
| 2467 | // Calculate a section intra ratio used in setting max loop filter. |
| 2468 | twopass->section_intra_rating = |
| 2469 | calculate_section_intra_ratio(start_position, twopass->stats_in_end, |
| 2470 | rc->frames_to_key); |
| 2471 | |
| 2472 | // Apply various clamps for min and max boost |
| 2473 | rc->kf_boost = (int)(av_decay_accumulator * boost_score); |
| 2474 | rc->kf_boost = MAX(rc->kf_boost, (rc->frames_to_key * 3)); |
| 2475 | rc->kf_boost = MAX(rc->kf_boost, MIN_KF_BOOST); |
| 2476 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2477 | // Work out how many bits to allocate for the key frame itself. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2478 | kf_bits = calculate_boost_bits((rc->frames_to_key - 1), |
| 2479 | rc->kf_boost, twopass->kf_group_bits); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2480 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2481 | // Work out the fraction of the kf group bits reserved for the inter frames |
| 2482 | // within the group after discounting the bits for the kf itself. |
| 2483 | if (twopass->kf_group_bits) { |
| 2484 | twopass->kfgroup_inter_fraction = |
| 2485 | (double)(twopass->kf_group_bits - kf_bits) / |
| 2486 | (double)twopass->kf_group_bits; |
| 2487 | } else { |
| 2488 | twopass->kfgroup_inter_fraction = 1.0; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2489 | } |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2490 | |
| 2491 | twopass->kf_group_bits -= kf_bits; |
| 2492 | |
| 2493 | // Save the bits to spend on the key frame. |
| 2494 | gf_group->bit_allocation[0] = kf_bits; |
| 2495 | gf_group->update_type[0] = KF_UPDATE; |
| 2496 | gf_group->rf_level[0] = KF_STD; |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2497 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2498 | // Note the total error score of the kf group minus the key frame itself. |
| 2499 | twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err); |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2500 | |
| 2501 | // Adjust the count of total modified error left. |
hkuang | 5ae7ac4 | 2013-11-07 15:50:31 -0800 | [diff] [blame] | 2502 | // The count of bits left is adjusted elsewhere based on real coded frame |
| 2503 | // sizes. |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2504 | twopass->modified_error_left -= kf_group_err; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2505 | |
| 2506 | if (oxcf->resize_mode == RESIZE_DYNAMIC) { |
| 2507 | // Default to normal-sized frame on keyframes. |
| 2508 | cpi->rc.next_frame_size_selector = UNSCALED; |
| 2509 | } |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2510 | } |
| 2511 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2512 | // Define the reference buffers that will be updated post encode. |
| 2513 | static void configure_buffer_updates(VP9_COMP *cpi) { |
| 2514 | TWO_PASS *const twopass = &cpi->twopass; |
| 2515 | |
| 2516 | cpi->rc.is_src_frame_alt_ref = 0; |
| 2517 | switch (twopass->gf_group.update_type[twopass->gf_group.index]) { |
| 2518 | case KF_UPDATE: |
| 2519 | cpi->refresh_last_frame = 1; |
| 2520 | cpi->refresh_golden_frame = 1; |
| 2521 | cpi->refresh_alt_ref_frame = 1; |
| 2522 | break; |
| 2523 | case LF_UPDATE: |
| 2524 | cpi->refresh_last_frame = 1; |
| 2525 | cpi->refresh_golden_frame = 0; |
| 2526 | cpi->refresh_alt_ref_frame = 0; |
| 2527 | break; |
| 2528 | case GF_UPDATE: |
| 2529 | cpi->refresh_last_frame = 1; |
| 2530 | cpi->refresh_golden_frame = 1; |
| 2531 | cpi->refresh_alt_ref_frame = 0; |
| 2532 | break; |
| 2533 | case OVERLAY_UPDATE: |
| 2534 | cpi->refresh_last_frame = 0; |
| 2535 | cpi->refresh_golden_frame = 1; |
| 2536 | cpi->refresh_alt_ref_frame = 0; |
| 2537 | cpi->rc.is_src_frame_alt_ref = 1; |
| 2538 | break; |
| 2539 | case ARF_UPDATE: |
| 2540 | cpi->refresh_last_frame = 0; |
| 2541 | cpi->refresh_golden_frame = 0; |
| 2542 | cpi->refresh_alt_ref_frame = 1; |
| 2543 | break; |
| 2544 | default: |
| 2545 | assert(0); |
| 2546 | break; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2547 | } |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2548 | if (is_two_pass_svc(cpi)) { |
| 2549 | if (cpi->svc.temporal_layer_id > 0) { |
| 2550 | cpi->refresh_last_frame = 0; |
| 2551 | cpi->refresh_golden_frame = 0; |
| 2552 | } |
| 2553 | if (cpi->svc.layer_context[cpi->svc.spatial_layer_id].gold_ref_idx < 0) |
| 2554 | cpi->refresh_golden_frame = 0; |
| 2555 | if (cpi->alt_ref_source == NULL) |
| 2556 | cpi->refresh_alt_ref_frame = 0; |
| 2557 | } |
| 2558 | } |
| 2559 | |
| 2560 | static int is_skippable_frame(const VP9_COMP *cpi) { |
| 2561 | // If the current frame does not have non-zero motion vector detected in the |
| 2562 | // first pass, and so do its previous and forward frames, then this frame |
| 2563 | // can be skipped for partition check, and the partition size is assigned |
| 2564 | // according to the variance |
| 2565 | const SVC *const svc = &cpi->svc; |
| 2566 | const TWO_PASS *const twopass = is_two_pass_svc(cpi) ? |
| 2567 | &svc->layer_context[svc->spatial_layer_id].twopass : &cpi->twopass; |
| 2568 | |
| 2569 | return (!frame_is_intra_only(&cpi->common) && |
| 2570 | twopass->stats_in - 2 > twopass->stats_in_start && |
| 2571 | twopass->stats_in < twopass->stats_in_end && |
| 2572 | (twopass->stats_in - 1)->pcnt_inter - (twopass->stats_in - 1)->pcnt_motion |
| 2573 | == 1 && |
| 2574 | (twopass->stats_in - 2)->pcnt_inter - (twopass->stats_in - 2)->pcnt_motion |
| 2575 | == 1 && |
| 2576 | twopass->stats_in->pcnt_inter - twopass->stats_in->pcnt_motion == 1); |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2577 | } |
| 2578 | |
| 2579 | void vp9_rc_get_second_pass_params(VP9_COMP *cpi) { |
| 2580 | VP9_COMMON *const cm = &cpi->common; |
| 2581 | RATE_CONTROL *const rc = &cpi->rc; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2582 | TWO_PASS *const twopass = &cpi->twopass; |
| 2583 | GF_GROUP *const gf_group = &twopass->gf_group; |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 2584 | int frames_left; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2585 | FIRSTPASS_STATS this_frame; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2586 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2587 | int target_rate; |
| 2588 | LAYER_CONTEXT *const lc = is_two_pass_svc(cpi) ? |
| 2589 | &cpi->svc.layer_context[cpi->svc.spatial_layer_id] : 0; |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 2590 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2591 | if (lc != NULL) { |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 2592 | frames_left = (int)(twopass->total_stats.count - |
| 2593 | lc->current_video_frame_in_layer); |
| 2594 | } else { |
| 2595 | frames_left = (int)(twopass->total_stats.count - |
| 2596 | cm->current_video_frame); |
| 2597 | } |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2598 | |
| 2599 | if (!twopass->stats_in) |
| 2600 | return; |
| 2601 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2602 | // If this is an arf frame then we dont want to read the stats file or |
| 2603 | // advance the input pointer as we already have what we need. |
| 2604 | if (gf_group->update_type[gf_group->index] == ARF_UPDATE) { |
| 2605 | int target_rate; |
| 2606 | configure_buffer_updates(cpi); |
| 2607 | target_rate = gf_group->bit_allocation[gf_group->index]; |
| 2608 | target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate); |
| 2609 | rc->base_frame_target = target_rate; |
| 2610 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2611 | cm->frame_type = INTER_FRAME; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2612 | |
| 2613 | if (lc != NULL) { |
| 2614 | if (cpi->svc.spatial_layer_id == 0) { |
| 2615 | lc->is_key_frame = 0; |
| 2616 | } else { |
| 2617 | lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame; |
| 2618 | |
| 2619 | if (lc->is_key_frame) |
| 2620 | cpi->ref_frame_flags &= (~VP9_LAST_FLAG); |
| 2621 | } |
| 2622 | } |
| 2623 | |
| 2624 | // Do the firstpass stats indicate that this frame is skippable for the |
| 2625 | // partition search? |
| 2626 | if (cpi->sf.allow_partition_search_skip && |
| 2627 | cpi->oxcf.pass == 2 && (!cpi->use_svc || is_two_pass_svc(cpi))) { |
| 2628 | cpi->partition_search_skippable_frame = is_skippable_frame(cpi); |
| 2629 | } |
| 2630 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2631 | return; |
| 2632 | } |
| 2633 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2634 | vpx_clear_system_state(); |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2635 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2636 | if (cpi->oxcf.rc_mode == VPX_Q) { |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2637 | twopass->active_worst_quality = cpi->oxcf.cq_level; |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 2638 | } else if (cm->current_video_frame == 0 || |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2639 | (lc != NULL && lc->current_video_frame_in_layer == 0)) { |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2640 | // Special case code for first frame. |
| 2641 | const int section_target_bandwidth = (int)(twopass->bits_left / |
| 2642 | frames_left); |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2643 | const double section_length = twopass->total_left_stats.count; |
| 2644 | const double section_error = |
| 2645 | twopass->total_left_stats.coded_error / section_length; |
| 2646 | const double section_intra_skip = |
| 2647 | twopass->total_left_stats.intra_skip_pct / section_length; |
| 2648 | const double section_inactive_zone = |
| 2649 | (twopass->total_left_stats.inactive_zone_rows * 2) / |
| 2650 | ((double)cm->mb_rows * section_length); |
| 2651 | const int tmp_q = |
| 2652 | get_twopass_worst_quality(cpi, section_error, |
| 2653 | section_intra_skip + section_inactive_zone, |
| 2654 | section_target_bandwidth, DEFAULT_GRP_WEIGHT); |
| 2655 | |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2656 | twopass->active_worst_quality = tmp_q; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2657 | twopass->baseline_active_worst_quality = tmp_q; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2658 | rc->ni_av_qi = tmp_q; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2659 | rc->last_q[INTER_FRAME] = tmp_q; |
| 2660 | rc->avg_q = vp9_convert_qindex_to_q(tmp_q, cm->bit_depth); |
| 2661 | rc->avg_frame_qindex[INTER_FRAME] = tmp_q; |
| 2662 | rc->last_q[KEY_FRAME] = (tmp_q + cpi->oxcf.best_allowed_q) / 2; |
| 2663 | rc->avg_frame_qindex[KEY_FRAME] = rc->last_q[KEY_FRAME]; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2664 | } |
| 2665 | vp9_zero(this_frame); |
| 2666 | if (EOF == input_stats(twopass, &this_frame)) |
| 2667 | return; |
| 2668 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2669 | // Set the frame content type flag. |
| 2670 | if (this_frame.intra_skip_pct >= FC_ANIMATION_THRESH) |
| 2671 | twopass->fr_content_type = FC_GRAPHICS_ANIMATION; |
| 2672 | else |
| 2673 | twopass->fr_content_type = FC_NORMAL; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2674 | |
| 2675 | // Keyframe and section processing. |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2676 | if (rc->frames_to_key == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY)) { |
| 2677 | FIRSTPASS_STATS this_frame_copy; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2678 | this_frame_copy = this_frame; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2679 | // Define next KF group and assign bits to it. |
| 2680 | find_next_key_frame(cpi, &this_frame); |
| 2681 | this_frame = this_frame_copy; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2682 | } else { |
| 2683 | cm->frame_type = INTER_FRAME; |
| 2684 | } |
| 2685 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2686 | if (lc != NULL) { |
| 2687 | if (cpi->svc.spatial_layer_id == 0) { |
| 2688 | lc->is_key_frame = (cm->frame_type == KEY_FRAME); |
| 2689 | if (lc->is_key_frame) { |
| 2690 | cpi->ref_frame_flags &= |
| 2691 | (~VP9_LAST_FLAG & ~VP9_GOLD_FLAG & ~VP9_ALT_FLAG); |
| 2692 | lc->frames_from_key_frame = 0; |
| 2693 | // Encode an intra only empty frame since we have a key frame. |
| 2694 | cpi->svc.encode_intra_empty_frame = 1; |
| 2695 | } |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2696 | } else { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2697 | cm->frame_type = INTER_FRAME; |
| 2698 | lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2699 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2700 | if (lc->is_key_frame) { |
| 2701 | cpi->ref_frame_flags &= (~VP9_LAST_FLAG); |
| 2702 | lc->frames_from_key_frame = 0; |
| 2703 | } |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2704 | } |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2705 | } |
| 2706 | |
| 2707 | // Define a new GF/ARF group. (Should always enter here for key frames). |
| 2708 | if (rc->frames_till_gf_update_due == 0) { |
| 2709 | define_gf_group(cpi, &this_frame); |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2710 | |
| 2711 | rc->frames_till_gf_update_due = rc->baseline_gf_interval; |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2712 | if (lc != NULL) |
| 2713 | cpi->refresh_golden_frame = 1; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2714 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2715 | #if ARF_STATS_OUTPUT |
| 2716 | { |
| 2717 | FILE *fpfile; |
| 2718 | fpfile = fopen("arf.stt", "a"); |
| 2719 | ++arf_count; |
| 2720 | fprintf(fpfile, "%10d %10ld %10d %10d %10ld\n", |
| 2721 | cm->current_video_frame, rc->frames_till_gf_update_due, |
| 2722 | rc->kf_boost, arf_count, rc->gfu_boost); |
| 2723 | |
| 2724 | fclose(fpfile); |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2725 | } |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2726 | #endif |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2727 | } |
| 2728 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2729 | configure_buffer_updates(cpi); |
| 2730 | |
| 2731 | // Do the firstpass stats indicate that this frame is skippable for the |
| 2732 | // partition search? |
| 2733 | if (cpi->sf.allow_partition_search_skip && cpi->oxcf.pass == 2 && |
| 2734 | (!cpi->use_svc || is_two_pass_svc(cpi))) { |
| 2735 | cpi->partition_search_skippable_frame = is_skippable_frame(cpi); |
| 2736 | } |
| 2737 | |
| 2738 | target_rate = gf_group->bit_allocation[gf_group->index]; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2739 | if (cpi->common.frame_type == KEY_FRAME) |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2740 | target_rate = vp9_rc_clamp_iframe_target_size(cpi, target_rate); |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2741 | else |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2742 | target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate); |
| 2743 | |
| 2744 | rc->base_frame_target = target_rate; |
| 2745 | |
| 2746 | { |
| 2747 | const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE) |
| 2748 | ? cpi->initial_mbs : cpi->common.MBs; |
| 2749 | // The multiplication by 256 reverses a scaling factor of (>> 8) |
| 2750 | // applied when combining MB error values for the frame. |
| 2751 | twopass->mb_av_energy = |
| 2752 | log(((this_frame.intra_error * 256.0) / num_mbs) + 1.0); |
| 2753 | } |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2754 | |
| 2755 | // Update the total stats remaining structure. |
| 2756 | subtract_stats(&twopass->total_left_stats, &this_frame); |
| 2757 | } |
| 2758 | |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2759 | #define MINQ_ADJ_LIMIT 48 |
| 2760 | #define MINQ_ADJ_LIMIT_CQ 20 |
| 2761 | #define HIGH_UNDERSHOOT_RATIO 2 |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 2762 | void vp9_twopass_postencode_update(VP9_COMP *cpi) { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2763 | TWO_PASS *const twopass = &cpi->twopass; |
| 2764 | RATE_CONTROL *const rc = &cpi->rc; |
| 2765 | const int bits_used = rc->base_frame_target; |
| 2766 | |
| 2767 | // VBR correction is done through rc->vbr_bits_off_target. Based on the |
| 2768 | // sign of this value, a limited % adjustment is made to the target rate |
| 2769 | // of subsequent frames, to try and push it back towards 0. This method |
| 2770 | // is designed to prevent extreme behaviour at the end of a clip |
| 2771 | // or group of frames. |
| 2772 | rc->vbr_bits_off_target += rc->base_frame_target - rc->projected_frame_size; |
| 2773 | twopass->bits_left = MAX(twopass->bits_left - bits_used, 0); |
| 2774 | |
| 2775 | // Calculate the pct rc error. |
| 2776 | if (rc->total_actual_bits) { |
| 2777 | rc->rate_error_estimate = |
| 2778 | (int)((rc->vbr_bits_off_target * 100) / rc->total_actual_bits); |
| 2779 | rc->rate_error_estimate = clamp(rc->rate_error_estimate, -100, 100); |
hkuang | 6ac915a | 2014-04-09 14:20:00 -0700 | [diff] [blame] | 2780 | } else { |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2781 | rc->rate_error_estimate = 0; |
Vignesh Venkatasubramanian | b08e2e2 | 2014-03-10 09:50:31 -0700 | [diff] [blame] | 2782 | } |
Vignesh Venkatasubramanian | 5a9753f | 2016-01-19 11:05:09 -0800 | [diff] [blame^] | 2783 | |
| 2784 | if (cpi->common.frame_type != KEY_FRAME && |
| 2785 | !vp9_is_upper_layer_key_frame(cpi)) { |
| 2786 | twopass->kf_group_bits -= bits_used; |
| 2787 | twopass->last_kfgroup_zeromotion_pct = twopass->kf_zeromotion_pct; |
| 2788 | } |
| 2789 | twopass->kf_group_bits = MAX(twopass->kf_group_bits, 0); |
| 2790 | |
| 2791 | // Increment the gf group index ready for the next frame. |
| 2792 | ++twopass->gf_group.index; |
| 2793 | |
| 2794 | // If the rate control is drifting consider adjustment to min or maxq. |
| 2795 | if ((cpi->oxcf.rc_mode != VPX_Q) && |
| 2796 | (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD) && |
| 2797 | !cpi->rc.is_src_frame_alt_ref) { |
| 2798 | const int maxq_adj_limit = |
| 2799 | rc->worst_quality - twopass->active_worst_quality; |
| 2800 | const int minq_adj_limit = |
| 2801 | (cpi->oxcf.rc_mode == VPX_CQ ? MINQ_ADJ_LIMIT_CQ : MINQ_ADJ_LIMIT); |
| 2802 | |
| 2803 | // Undershoot. |
| 2804 | if (rc->rate_error_estimate > cpi->oxcf.under_shoot_pct) { |
| 2805 | --twopass->extend_maxq; |
| 2806 | if (rc->rolling_target_bits >= rc->rolling_actual_bits) |
| 2807 | ++twopass->extend_minq; |
| 2808 | // Overshoot. |
| 2809 | } else if (rc->rate_error_estimate < -cpi->oxcf.over_shoot_pct) { |
| 2810 | --twopass->extend_minq; |
| 2811 | if (rc->rolling_target_bits < rc->rolling_actual_bits) |
| 2812 | ++twopass->extend_maxq; |
| 2813 | } else { |
| 2814 | // Adjustment for extreme local overshoot. |
| 2815 | if (rc->projected_frame_size > (2 * rc->base_frame_target) && |
| 2816 | rc->projected_frame_size > (2 * rc->avg_frame_bandwidth)) |
| 2817 | ++twopass->extend_maxq; |
| 2818 | |
| 2819 | // Unwind undershoot or overshoot adjustment. |
| 2820 | if (rc->rolling_target_bits < rc->rolling_actual_bits) |
| 2821 | --twopass->extend_minq; |
| 2822 | else if (rc->rolling_target_bits > rc->rolling_actual_bits) |
| 2823 | --twopass->extend_maxq; |
| 2824 | } |
| 2825 | |
| 2826 | twopass->extend_minq = clamp(twopass->extend_minq, 0, minq_adj_limit); |
| 2827 | twopass->extend_maxq = clamp(twopass->extend_maxq, 0, maxq_adj_limit); |
| 2828 | |
| 2829 | // If there is a big and undexpected undershoot then feed the extra |
| 2830 | // bits back in quickly. One situation where this may happen is if a |
| 2831 | // frame is unexpectedly almost perfectly predicted by the ARF or GF |
| 2832 | // but not very well predcited by the previous frame. |
| 2833 | if (!frame_is_kf_gf_arf(cpi) && !cpi->rc.is_src_frame_alt_ref) { |
| 2834 | int fast_extra_thresh = rc->base_frame_target / HIGH_UNDERSHOOT_RATIO; |
| 2835 | if (rc->projected_frame_size < fast_extra_thresh) { |
| 2836 | rc->vbr_bits_off_target_fast += |
| 2837 | fast_extra_thresh - rc->projected_frame_size; |
| 2838 | rc->vbr_bits_off_target_fast = |
| 2839 | MIN(rc->vbr_bits_off_target_fast, (4 * rc->avg_frame_bandwidth)); |
| 2840 | |
| 2841 | // Fast adaptation of minQ if necessary to use up the extra bits. |
| 2842 | if (rc->avg_frame_bandwidth) { |
| 2843 | twopass->extend_minq_fast = |
| 2844 | (int)(rc->vbr_bits_off_target_fast * 8 / rc->avg_frame_bandwidth); |
| 2845 | } |
| 2846 | twopass->extend_minq_fast = MIN(twopass->extend_minq_fast, |
| 2847 | minq_adj_limit - twopass->extend_minq); |
| 2848 | } else if (rc->vbr_bits_off_target_fast) { |
| 2849 | twopass->extend_minq_fast = MIN(twopass->extend_minq_fast, |
| 2850 | minq_adj_limit - twopass->extend_minq); |
| 2851 | } else { |
| 2852 | twopass->extend_minq_fast = 0; |
| 2853 | } |
| 2854 | } |
| 2855 | } |
hkuang | ba164df | 2013-06-19 15:33:45 -0700 | [diff] [blame] | 2856 | } |