John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1 | /* |
John Koleszar | c2140b8 | 2010-09-09 08:16:39 -0400 | [diff] [blame] | 2 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 3 | * |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 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 |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 7 | * in the file PATENTS. All contributing project authors may |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 8 | * be found in the AUTHORS file in the root of the source tree. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | |
| 12 | #include <stdlib.h> |
| 13 | #include <stdio.h> |
| 14 | #include <string.h> |
| 15 | #include <limits.h> |
| 16 | #include <assert.h> |
| 17 | |
| 18 | #include "math.h" |
John Koleszar | 02321de | 2011-02-10 14:41:38 -0500 | [diff] [blame] | 19 | #include "vp8/common/common.h" |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 20 | #include "ratectrl.h" |
John Koleszar | 02321de | 2011-02-10 14:41:38 -0500 | [diff] [blame] | 21 | #include "vp8/common/entropymode.h" |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 22 | #include "vpx_mem/vpx_mem.h" |
John Koleszar | 02321de | 2011-02-10 14:41:38 -0500 | [diff] [blame] | 23 | #include "vp8/common/systemdependent.h" |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 24 | #include "encodemv.h" |
James Zern | 820302a | 2015-08-27 14:46:08 -0700 | [diff] [blame] | 25 | #include "vpx_dsp/vpx_dsp_common.h" |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 26 | |
| 27 | |
| 28 | #define MIN_BPB_FACTOR 0.01 |
| 29 | #define MAX_BPB_FACTOR 50 |
| 30 | |
| 31 | extern const MB_PREDICTION_MODE vp8_mode_order[MAX_MODES]; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 32 | |
| 33 | |
| 34 | |
| 35 | #ifdef MODE_STATS |
| 36 | extern int y_modes[5]; |
| 37 | extern int uv_modes[4]; |
| 38 | extern int b_modes[10]; |
| 39 | |
| 40 | extern int inter_y_modes[10]; |
| 41 | extern int inter_uv_modes[4]; |
| 42 | extern int inter_b_modes[10]; |
| 43 | #endif |
| 44 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 45 | /* Bits Per MB at different Q (Multiplied by 512) */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 46 | #define BPER_MB_NORMBITS 9 |
| 47 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 48 | /* Work in progress recalibration of baseline rate tables based on |
| 49 | * the assumption that bits per mb is inversely proportional to the |
| 50 | * quantizer value. |
| 51 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 52 | const int vp8_bits_per_mb[2][QINDEX_RANGE] = |
| 53 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 54 | /* Intra case 450000/Qintra */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 55 | { |
Paul Wilkins | ad6150f | 2010-11-22 13:17:35 +0000 | [diff] [blame] | 56 | 1125000,900000, 750000, 642857, 562500, 500000, 450000, 450000, |
| 57 | 409090, 375000, 346153, 321428, 300000, 281250, 264705, 264705, |
| 58 | 250000, 236842, 225000, 225000, 214285, 214285, 204545, 204545, |
| 59 | 195652, 195652, 187500, 180000, 180000, 173076, 166666, 160714, |
| 60 | 155172, 150000, 145161, 140625, 136363, 132352, 128571, 125000, |
| 61 | 121621, 121621, 118421, 115384, 112500, 109756, 107142, 104651, |
| 62 | 102272, 100000, 97826, 97826, 95744, 93750, 91836, 90000, |
| 63 | 88235, 86538, 84905, 83333, 81818, 80357, 78947, 77586, |
| 64 | 76271, 75000, 73770, 72580, 71428, 70312, 69230, 68181, |
| 65 | 67164, 66176, 65217, 64285, 63380, 62500, 61643, 60810, |
| 66 | 60000, 59210, 59210, 58441, 57692, 56962, 56250, 55555, |
| 67 | 54878, 54216, 53571, 52941, 52325, 51724, 51136, 50561, |
| 68 | 49450, 48387, 47368, 46875, 45918, 45000, 44554, 44117, |
| 69 | 43269, 42452, 41666, 40909, 40178, 39473, 38793, 38135, |
| 70 | 36885, 36290, 35714, 35156, 34615, 34090, 33582, 33088, |
| 71 | 32608, 32142, 31468, 31034, 30405, 29801, 29220, 28662, |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 72 | }, |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 73 | /* Inter case 285000/Qinter */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 74 | { |
Paul Wilkins | ad6150f | 2010-11-22 13:17:35 +0000 | [diff] [blame] | 75 | 712500, 570000, 475000, 407142, 356250, 316666, 285000, 259090, |
| 76 | 237500, 219230, 203571, 190000, 178125, 167647, 158333, 150000, |
| 77 | 142500, 135714, 129545, 123913, 118750, 114000, 109615, 105555, |
| 78 | 101785, 98275, 95000, 91935, 89062, 86363, 83823, 81428, |
| 79 | 79166, 77027, 75000, 73076, 71250, 69512, 67857, 66279, |
| 80 | 64772, 63333, 61956, 60638, 59375, 58163, 57000, 55882, |
| 81 | 54807, 53773, 52777, 51818, 50892, 50000, 49137, 47500, |
| 82 | 45967, 44531, 43181, 41911, 40714, 39583, 38513, 37500, |
| 83 | 36538, 35625, 34756, 33928, 33139, 32386, 31666, 30978, |
| 84 | 30319, 29687, 29081, 28500, 27941, 27403, 26886, 26388, |
| 85 | 25909, 25446, 25000, 24568, 23949, 23360, 22800, 22265, |
| 86 | 21755, 21268, 20802, 20357, 19930, 19520, 19127, 18750, |
| 87 | 18387, 18037, 17701, 17378, 17065, 16764, 16473, 16101, |
| 88 | 15745, 15405, 15079, 14766, 14467, 14179, 13902, 13636, |
| 89 | 13380, 13133, 12895, 12666, 12445, 12179, 11924, 11632, |
| 90 | 11445, 11220, 11003, 10795, 10594, 10401, 10215, 10035, |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 91 | } |
| 92 | }; |
| 93 | |
John Koleszar | 429dc67 | 2011-03-17 17:07:59 -0400 | [diff] [blame] | 94 | static const int kf_boost_qadjustment[QINDEX_RANGE] = |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 95 | { |
| 96 | 128, 129, 130, 131, 132, 133, 134, 135, |
| 97 | 136, 137, 138, 139, 140, 141, 142, 143, |
| 98 | 144, 145, 146, 147, 148, 149, 150, 151, |
| 99 | 152, 153, 154, 155, 156, 157, 158, 159, |
| 100 | 160, 161, 162, 163, 164, 165, 166, 167, |
| 101 | 168, 169, 170, 171, 172, 173, 174, 175, |
| 102 | 176, 177, 178, 179, 180, 181, 182, 183, |
| 103 | 184, 185, 186, 187, 188, 189, 190, 191, |
| 104 | 192, 193, 194, 195, 196, 197, 198, 199, |
| 105 | 200, 200, 201, 201, 202, 203, 203, 203, |
| 106 | 204, 204, 205, 205, 206, 206, 207, 207, |
| 107 | 208, 208, 209, 209, 210, 210, 211, 211, |
| 108 | 212, 212, 213, 213, 214, 214, 215, 215, |
| 109 | 216, 216, 217, 217, 218, 218, 219, 219, |
| 110 | 220, 220, 220, 220, 220, 220, 220, 220, |
| 111 | 220, 220, 220, 220, 220, 220, 220, 220, |
| 112 | }; |
| 113 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 114 | /* #define GFQ_ADJUSTMENT (Q+100) */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 115 | #define GFQ_ADJUSTMENT vp8_gf_boost_qadjustment[Q] |
| 116 | const int vp8_gf_boost_qadjustment[QINDEX_RANGE] = |
| 117 | { |
| 118 | 80, 82, 84, 86, 88, 90, 92, 94, |
| 119 | 96, 97, 98, 99, 100, 101, 102, 103, |
| 120 | 104, 105, 106, 107, 108, 109, 110, 111, |
| 121 | 112, 113, 114, 115, 116, 117, 118, 119, |
| 122 | 120, 121, 122, 123, 124, 125, 126, 127, |
| 123 | 128, 129, 130, 131, 132, 133, 134, 135, |
| 124 | 136, 137, 138, 139, 140, 141, 142, 143, |
| 125 | 144, 145, 146, 147, 148, 149, 150, 151, |
| 126 | 152, 153, 154, 155, 156, 157, 158, 159, |
| 127 | 160, 161, 162, 163, 164, 165, 166, 167, |
| 128 | 168, 169, 170, 171, 172, 173, 174, 175, |
| 129 | 176, 177, 178, 179, 180, 181, 182, 183, |
| 130 | 184, 184, 185, 185, 186, 186, 187, 187, |
| 131 | 188, 188, 189, 189, 190, 190, 191, 191, |
| 132 | 192, 192, 193, 193, 194, 194, 194, 194, |
| 133 | 195, 195, 196, 196, 197, 197, 198, 198 |
| 134 | }; |
| 135 | |
| 136 | /* |
| 137 | const int vp8_gf_boost_qadjustment[QINDEX_RANGE] = |
| 138 | { |
| 139 | 100,101,102,103,104,105,105,106, |
| 140 | 106,107,107,108,109,109,110,111, |
| 141 | 112,113,114,115,116,117,118,119, |
| 142 | 120,121,122,123,124,125,126,127, |
| 143 | 128,129,130,131,132,133,134,135, |
| 144 | 136,137,138,139,140,141,142,143, |
| 145 | 144,145,146,147,148,149,150,151, |
| 146 | 152,153,154,155,156,157,158,159, |
| 147 | 160,161,162,163,164,165,166,167, |
| 148 | 168,169,170,170,171,171,172,172, |
| 149 | 173,173,173,174,174,174,175,175, |
| 150 | 175,176,176,176,177,177,177,177, |
| 151 | 178,178,179,179,180,180,181,181, |
| 152 | 182,182,183,183,184,184,185,185, |
| 153 | 186,186,187,187,188,188,189,189, |
| 154 | 190,190,191,191,192,192,193,193, |
| 155 | }; |
| 156 | */ |
| 157 | |
John Koleszar | 429dc67 | 2011-03-17 17:07:59 -0400 | [diff] [blame] | 158 | static const int kf_gf_boost_qlimits[QINDEX_RANGE] = |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 159 | { |
| 160 | 150, 155, 160, 165, 170, 175, 180, 185, |
| 161 | 190, 195, 200, 205, 210, 215, 220, 225, |
| 162 | 230, 235, 240, 245, 250, 255, 260, 265, |
| 163 | 270, 275, 280, 285, 290, 295, 300, 305, |
| 164 | 310, 320, 330, 340, 350, 360, 370, 380, |
| 165 | 390, 400, 410, 420, 430, 440, 450, 460, |
| 166 | 470, 480, 490, 500, 510, 520, 530, 540, |
| 167 | 550, 560, 570, 580, 590, 600, 600, 600, |
| 168 | 600, 600, 600, 600, 600, 600, 600, 600, |
| 169 | 600, 600, 600, 600, 600, 600, 600, 600, |
| 170 | 600, 600, 600, 600, 600, 600, 600, 600, |
| 171 | 600, 600, 600, 600, 600, 600, 600, 600, |
| 172 | 600, 600, 600, 600, 600, 600, 600, 600, |
| 173 | 600, 600, 600, 600, 600, 600, 600, 600, |
| 174 | 600, 600, 600, 600, 600, 600, 600, 600, |
| 175 | 600, 600, 600, 600, 600, 600, 600, 600, |
| 176 | }; |
| 177 | |
John Koleszar | 429dc67 | 2011-03-17 17:07:59 -0400 | [diff] [blame] | 178 | static const int gf_adjust_table[101] = |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 179 | { |
| 180 | 100, |
| 181 | 115, 130, 145, 160, 175, 190, 200, 210, 220, 230, |
| 182 | 240, 260, 270, 280, 290, 300, 310, 320, 330, 340, |
| 183 | 350, 360, 370, 380, 390, 400, 400, 400, 400, 400, |
| 184 | 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, |
| 185 | 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, |
| 186 | 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, |
| 187 | 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, |
| 188 | 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, |
| 189 | 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, |
| 190 | 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, |
| 191 | }; |
| 192 | |
John Koleszar | 429dc67 | 2011-03-17 17:07:59 -0400 | [diff] [blame] | 193 | static const int gf_intra_usage_adjustment[20] = |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 194 | { |
| 195 | 125, 120, 115, 110, 105, 100, 95, 85, 80, 75, |
| 196 | 70, 65, 60, 55, 50, 50, 50, 50, 50, 50, |
| 197 | }; |
| 198 | |
John Koleszar | 429dc67 | 2011-03-17 17:07:59 -0400 | [diff] [blame] | 199 | static const int gf_interval_table[101] = |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 200 | { |
| 201 | 7, |
| 202 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 203 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 204 | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
| 205 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
| 206 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, |
| 207 | 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, |
| 208 | 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, |
| 209 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 210 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 211 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 212 | }; |
| 213 | |
| 214 | static const unsigned int prior_key_frame_weight[KEY_FRAME_CONTEXT] = { 1, 2, 3, 4, 5 }; |
| 215 | |
| 216 | |
| 217 | void vp8_save_coding_context(VP8_COMP *cpi) |
| 218 | { |
| 219 | CODING_CONTEXT *const cc = & cpi->coding_context; |
| 220 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 221 | /* Stores a snapshot of key state variables which can subsequently be |
| 222 | * restored with a call to vp8_restore_coding_context. These functions are |
| 223 | * intended for use in a re-code loop in vp8_compress_frame where the |
| 224 | * quantizer value is adjusted between loop iterations. |
| 225 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 226 | |
| 227 | cc->frames_since_key = cpi->frames_since_key; |
| 228 | cc->filter_level = cpi->common.filter_level; |
| 229 | cc->frames_till_gf_update_due = cpi->frames_till_gf_update_due; |
James Zern | 5f30a0c | 2013-07-18 14:09:21 -0700 | [diff] [blame] | 230 | cc->frames_since_golden = cpi->frames_since_golden; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 231 | |
| 232 | vp8_copy(cc->mvc, cpi->common.fc.mvc); |
Attila Nagy | b41c17d | 2012-04-17 10:40:56 +0300 | [diff] [blame] | 233 | vp8_copy(cc->mvcosts, cpi->rd_costs.mvcosts); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 234 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 235 | vp8_copy(cc->ymode_prob, cpi->common.fc.ymode_prob); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 236 | vp8_copy(cc->uv_mode_prob, cpi->common.fc.uv_mode_prob); |
| 237 | |
Scott LaVarnway | 95390b2 | 2012-11-05 12:25:18 -0800 | [diff] [blame] | 238 | vp8_copy(cc->ymode_count, cpi->mb.ymode_count); |
| 239 | vp8_copy(cc->uv_mode_count, cpi->mb.uv_mode_count); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 240 | |
| 241 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 242 | /* Stats */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 243 | #ifdef MODE_STATS |
| 244 | vp8_copy(cc->y_modes, y_modes); |
| 245 | vp8_copy(cc->uv_modes, uv_modes); |
| 246 | vp8_copy(cc->b_modes, b_modes); |
| 247 | vp8_copy(cc->inter_y_modes, inter_y_modes); |
| 248 | vp8_copy(cc->inter_uv_modes, inter_uv_modes); |
| 249 | vp8_copy(cc->inter_b_modes, inter_b_modes); |
| 250 | #endif |
| 251 | |
| 252 | cc->this_frame_percent_intra = cpi->this_frame_percent_intra; |
| 253 | } |
| 254 | |
| 255 | |
| 256 | void vp8_restore_coding_context(VP8_COMP *cpi) |
| 257 | { |
| 258 | CODING_CONTEXT *const cc = & cpi->coding_context; |
| 259 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 260 | /* Restore key state variables to the snapshot state stored in the |
| 261 | * previous call to vp8_save_coding_context. |
| 262 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 263 | |
| 264 | cpi->frames_since_key = cc->frames_since_key; |
| 265 | cpi->common.filter_level = cc->filter_level; |
| 266 | cpi->frames_till_gf_update_due = cc->frames_till_gf_update_due; |
James Zern | 5f30a0c | 2013-07-18 14:09:21 -0700 | [diff] [blame] | 267 | cpi->frames_since_golden = cc->frames_since_golden; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 268 | |
| 269 | vp8_copy(cpi->common.fc.mvc, cc->mvc); |
| 270 | |
Attila Nagy | b41c17d | 2012-04-17 10:40:56 +0300 | [diff] [blame] | 271 | vp8_copy(cpi->rd_costs.mvcosts, cc->mvcosts); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 272 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 273 | vp8_copy(cpi->common.fc.ymode_prob, cc->ymode_prob); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 274 | vp8_copy(cpi->common.fc.uv_mode_prob, cc->uv_mode_prob); |
| 275 | |
Scott LaVarnway | 95390b2 | 2012-11-05 12:25:18 -0800 | [diff] [blame] | 276 | vp8_copy(cpi->mb.ymode_count, cc->ymode_count); |
| 277 | vp8_copy(cpi->mb.uv_mode_count, cc->uv_mode_count); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 278 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 279 | /* Stats */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 280 | #ifdef MODE_STATS |
| 281 | vp8_copy(y_modes, cc->y_modes); |
| 282 | vp8_copy(uv_modes, cc->uv_modes); |
| 283 | vp8_copy(b_modes, cc->b_modes); |
| 284 | vp8_copy(inter_y_modes, cc->inter_y_modes); |
| 285 | vp8_copy(inter_uv_modes, cc->inter_uv_modes); |
| 286 | vp8_copy(inter_b_modes, cc->inter_b_modes); |
| 287 | #endif |
| 288 | |
| 289 | |
| 290 | cpi->this_frame_percent_intra = cc->this_frame_percent_intra; |
| 291 | } |
| 292 | |
| 293 | |
| 294 | void vp8_setup_key_frame(VP8_COMP *cpi) |
| 295 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 296 | /* Setup for Key frame: */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 297 | |
| 298 | vp8_default_coef_probs(& cpi->common); |
Jim Bankoski | 6de67cd | 2011-12-09 16:56:18 -0800 | [diff] [blame] | 299 | |
James Zern | f274c21 | 2015-04-23 20:42:19 -0700 | [diff] [blame] | 300 | memcpy(cpi->common.fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context)); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 301 | { |
| 302 | int flag[2] = {1, 1}; |
Yunqing Wang | 3d68158 | 2011-04-01 16:41:58 -0400 | [diff] [blame] | 303 | vp8_build_component_cost_table(cpi->mb.mvcost, (const MV_CONTEXT *) cpi->common.fc.mvc, flag); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 304 | } |
| 305 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 306 | /* Make sure we initialize separate contexts for altref,gold, and normal. |
| 307 | * TODO shouldn't need 3 different copies of structure to do this! |
| 308 | */ |
James Zern | f274c21 | 2015-04-23 20:42:19 -0700 | [diff] [blame] | 309 | memcpy(&cpi->lfc_a, &cpi->common.fc, sizeof(cpi->common.fc)); |
| 310 | memcpy(&cpi->lfc_g, &cpi->common.fc, sizeof(cpi->common.fc)); |
| 311 | memcpy(&cpi->lfc_n, &cpi->common.fc, sizeof(cpi->common.fc)); |
Jim Bankoski | 6de67cd | 2011-12-09 16:56:18 -0800 | [diff] [blame] | 312 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 313 | cpi->common.filter_level = cpi->common.base_qindex * 3 / 8 ; |
| 314 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 315 | /* Provisional interval before next GF */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 316 | if (cpi->auto_gold) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 317 | cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; |
| 318 | else |
Yunqing Wang | 0ca0c4f | 2012-06-12 11:57:26 -0400 | [diff] [blame] | 319 | cpi->frames_till_gf_update_due = DEFAULT_GF_INTERVAL; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 320 | |
John Koleszar | f56918b | 2011-12-21 14:21:29 -0800 | [diff] [blame] | 321 | cpi->common.refresh_golden_frame = 1; |
| 322 | cpi->common.refresh_alt_ref_frame = 1; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 323 | } |
| 324 | |
John Koleszar | 81d2206 | 2011-04-25 15:02:54 -0400 | [diff] [blame] | 325 | |
John Koleszar | db5057c | 2011-04-26 16:45:30 -0400 | [diff] [blame] | 326 | static int estimate_bits_at_q(int frame_kind, int Q, int MBs, |
| 327 | double correction_factor) |
| 328 | { |
| 329 | int Bpm = (int)(.5 + correction_factor * vp8_bits_per_mb[frame_kind][Q]); |
| 330 | |
| 331 | /* Attempt to retain reasonable accuracy without overflow. The cutoff is |
| 332 | * chosen such that the maximum product of Bpm and MBs fits 31 bits. The |
| 333 | * largest Bpm takes 20 bits. |
| 334 | */ |
| 335 | if (MBs > (1 << 11)) |
| 336 | return (Bpm >> BPER_MB_NORMBITS) * MBs; |
| 337 | else |
| 338 | return (Bpm * MBs) >> BPER_MB_NORMBITS; |
| 339 | } |
John Koleszar | 81d2206 | 2011-04-25 15:02:54 -0400 | [diff] [blame] | 340 | |
| 341 | |
John Koleszar | db5057c | 2011-04-26 16:45:30 -0400 | [diff] [blame] | 342 | static void calc_iframe_target_size(VP8_COMP *cpi) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 343 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 344 | /* boost defaults to half second */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 345 | int kf_boost; |
Yaowu Xu | d71ba03 | 2012-08-17 10:05:35 -0700 | [diff] [blame] | 346 | uint64_t target; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 347 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 348 | /* Clear down mmx registers to allow floating point in what follows */ |
| 349 | vp8_clear_system_state(); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 350 | |
| 351 | if (cpi->oxcf.fixed_q >= 0) |
| 352 | { |
John Koleszar | db5057c | 2011-04-26 16:45:30 -0400 | [diff] [blame] | 353 | int Q = cpi->oxcf.key_q; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 354 | |
John Koleszar | db5057c | 2011-04-26 16:45:30 -0400 | [diff] [blame] | 355 | target = estimate_bits_at_q(INTRA_FRAME, Q, cpi->common.MBs, |
| 356 | cpi->key_frame_rate_correction_factor); |
| 357 | } |
| 358 | else if (cpi->pass == 2) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 359 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 360 | /* New Two pass RC */ |
John Koleszar | db5057c | 2011-04-26 16:45:30 -0400 | [diff] [blame] | 361 | target = cpi->per_frame_bandwidth; |
| 362 | } |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 363 | /* First Frame is a special case */ |
John Koleszar | db5057c | 2011-04-26 16:45:30 -0400 | [diff] [blame] | 364 | else if (cpi->common.current_video_frame == 0) |
| 365 | { |
| 366 | /* 1 Pass there is no information on which to base size so use |
| 367 | * bandwidth per second * fraction of the initial buffer |
| 368 | * level |
| 369 | */ |
| 370 | target = cpi->oxcf.starting_buffer_level / 2; |
| 371 | |
| 372 | if(target > cpi->oxcf.target_bandwidth * 3 / 2) |
| 373 | target = cpi->oxcf.target_bandwidth * 3 / 2; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 374 | } |
| 375 | else |
| 376 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 377 | /* if this keyframe was forced, use a more recent Q estimate */ |
John Koleszar | db5057c | 2011-04-26 16:45:30 -0400 | [diff] [blame] | 378 | int Q = (cpi->common.frame_flags & FRAMEFLAGS_KEY) |
| 379 | ? cpi->avg_frame_qindex : cpi->ni_av_qi; |
| 380 | |
Marco Paniconi | ef4a967 | 2012-07-27 17:24:29 -0700 | [diff] [blame] | 381 | int initial_boost = 32; /* |3.0 * per_frame_bandwidth| */ |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 382 | /* Boost depends somewhat on frame rate: only used for 1 layer case. */ |
Marco Paniconi | 507ee87 | 2012-02-22 14:39:17 -0800 | [diff] [blame] | 383 | if (cpi->oxcf.number_of_layers == 1) { |
James Zern | 820302a | 2015-08-27 14:46:08 -0700 | [diff] [blame] | 384 | kf_boost = VPXMAX(initial_boost, |
| 385 | (int)(2 * cpi->output_framerate - 16)); |
Marco Paniconi | 507ee87 | 2012-02-22 14:39:17 -0800 | [diff] [blame] | 386 | } |
| 387 | else { |
Marco Paniconi | ef4a967 | 2012-07-27 17:24:29 -0700 | [diff] [blame] | 388 | /* Initial factor: set target size to: |3.0 * per_frame_bandwidth|. */ |
Marco Paniconi | 3014091 | 2012-03-12 16:23:08 -0700 | [diff] [blame] | 389 | kf_boost = initial_boost; |
Marco Paniconi | 507ee87 | 2012-02-22 14:39:17 -0800 | [diff] [blame] | 390 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 391 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 392 | /* adjustment up based on q: this factor ranges from ~1.2 to 2.2. */ |
John Koleszar | db5057c | 2011-04-26 16:45:30 -0400 | [diff] [blame] | 393 | kf_boost = kf_boost * kf_boost_qadjustment[Q] / 100; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 394 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 395 | /* frame separation adjustment ( down) */ |
James Zern | 9581eb6 | 2013-07-12 17:12:46 -0700 | [diff] [blame] | 396 | if (cpi->frames_since_key < cpi->output_framerate / 2) |
John Koleszar | db5057c | 2011-04-26 16:45:30 -0400 | [diff] [blame] | 397 | kf_boost = (int)(kf_boost |
James Zern | 9581eb6 | 2013-07-12 17:12:46 -0700 | [diff] [blame] | 398 | * cpi->frames_since_key / (cpi->output_framerate / 2)); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 399 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 400 | /* Minimal target size is |2* per_frame_bandwidth|. */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 401 | if (kf_boost < 16) |
| 402 | kf_boost = 16; |
| 403 | |
John Koleszar | db5057c | 2011-04-26 16:45:30 -0400 | [diff] [blame] | 404 | target = ((16 + kf_boost) * cpi->per_frame_bandwidth) >> 4; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | |
John Koleszar | db5057c | 2011-04-26 16:45:30 -0400 | [diff] [blame] | 408 | if (cpi->oxcf.rc_max_intra_bitrate_pct) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 409 | { |
John Koleszar | db5057c | 2011-04-26 16:45:30 -0400 | [diff] [blame] | 410 | unsigned int max_rate = cpi->per_frame_bandwidth |
| 411 | * cpi->oxcf.rc_max_intra_bitrate_pct / 100; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 412 | |
John Koleszar | db5057c | 2011-04-26 16:45:30 -0400 | [diff] [blame] | 413 | if (target > max_rate) |
| 414 | target = max_rate; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 415 | } |
| 416 | |
Yaowu Xu | d71ba03 | 2012-08-17 10:05:35 -0700 | [diff] [blame] | 417 | cpi->this_frame_target = (int)target; |
John Koleszar | db5057c | 2011-04-26 16:45:30 -0400 | [diff] [blame] | 418 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 419 | /* TODO: if we separate rate targeting from Q targetting, move this. |
| 420 | * Reset the active worst quality to the baseline value for key frames. |
| 421 | */ |
John Koleszar | abc9958 | 2011-05-06 11:48:50 -0400 | [diff] [blame] | 422 | if (cpi->pass != 2) |
| 423 | cpi->active_worst_quality = cpi->worst_quality; |
John Koleszar | db5057c | 2011-04-26 16:45:30 -0400 | [diff] [blame] | 424 | |
| 425 | #if 0 |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 426 | { |
| 427 | FILE *f; |
| 428 | |
| 429 | f = fopen("kf_boost.stt", "a"); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 430 | fprintf(f, " %8u %10d %10d %10d\n", |
| 431 | cpi->common.current_video_frame, cpi->gfu_boost, cpi->baseline_gf_interval, cpi->source_alt_ref_pending); |
| 432 | |
| 433 | fclose(f); |
| 434 | } |
John Koleszar | db5057c | 2011-04-26 16:45:30 -0400 | [diff] [blame] | 435 | #endif |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 436 | } |
| 437 | |
John Koleszar | db5057c | 2011-04-26 16:45:30 -0400 | [diff] [blame] | 438 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 439 | /* Do the best we can to define the parameters for the next GF based on what |
| 440 | * information we have available. |
| 441 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 442 | static void calc_gf_params(VP8_COMP *cpi) |
| 443 | { |
| 444 | int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q; |
| 445 | int Boost = 0; |
| 446 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 447 | int gf_frame_useage = 0; /* Golden frame useage since last GF */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 448 | int tot_mbs = cpi->recent_ref_frame_usage[INTRA_FRAME] + |
| 449 | cpi->recent_ref_frame_usage[LAST_FRAME] + |
| 450 | cpi->recent_ref_frame_usage[GOLDEN_FRAME] + |
| 451 | cpi->recent_ref_frame_usage[ALTREF_FRAME]; |
| 452 | |
Scott LaVarnway | 99f46d6 | 2010-08-11 11:02:31 -0400 | [diff] [blame] | 453 | int pct_gf_active = (100 * cpi->gf_active_count) / (cpi->common.mb_rows * cpi->common.mb_cols); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 454 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 455 | if (tot_mbs) |
| 456 | gf_frame_useage = (cpi->recent_ref_frame_usage[GOLDEN_FRAME] + cpi->recent_ref_frame_usage[ALTREF_FRAME]) * 100 / tot_mbs; |
| 457 | |
| 458 | if (pct_gf_active > gf_frame_useage) |
| 459 | gf_frame_useage = pct_gf_active; |
| 460 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 461 | /* Not two pass */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 462 | if (cpi->pass != 2) |
| 463 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 464 | /* Single Pass lagged mode: TBD */ |
John Koleszar | f56918b | 2011-12-21 14:21:29 -0800 | [diff] [blame] | 465 | if (0) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 466 | { |
| 467 | } |
| 468 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 469 | /* Single Pass compression: Has to use current and historical data */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 470 | else |
| 471 | { |
| 472 | #if 0 |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 473 | /* Experimental code */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 474 | int index = cpi->one_pass_frame_index; |
| 475 | int frames_to_scan = (cpi->max_gf_interval <= MAX_LAG_BUFFERS) ? cpi->max_gf_interval : MAX_LAG_BUFFERS; |
| 476 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 477 | /* ************** Experimental code - incomplete */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 478 | /* |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 479 | double decay_val = 1.0; |
| 480 | double IIAccumulator = 0.0; |
| 481 | double last_iiaccumulator = 0.0; |
| 482 | double IIRatio; |
| 483 | |
| 484 | cpi->one_pass_frame_index = cpi->common.current_video_frame%MAX_LAG_BUFFERS; |
| 485 | |
| 486 | for ( i = 0; i < (frames_to_scan - 1); i++ ) |
| 487 | { |
| 488 | if ( index < 0 ) |
| 489 | index = MAX_LAG_BUFFERS; |
| 490 | index --; |
| 491 | |
| 492 | if ( cpi->one_pass_frame_stats[index].frame_coded_error > 0.0 ) |
| 493 | { |
| 494 | IIRatio = cpi->one_pass_frame_stats[index].frame_intra_error / cpi->one_pass_frame_stats[index].frame_coded_error; |
| 495 | |
| 496 | if ( IIRatio > 30.0 ) |
| 497 | IIRatio = 30.0; |
| 498 | } |
| 499 | else |
| 500 | IIRatio = 30.0; |
| 501 | |
| 502 | IIAccumulator += IIRatio * decay_val; |
| 503 | |
| 504 | decay_val = decay_val * cpi->one_pass_frame_stats[index].frame_pcnt_inter; |
| 505 | |
| 506 | if ( (i > MIN_GF_INTERVAL) && |
| 507 | ((IIAccumulator - last_iiaccumulator) < 2.0) ) |
| 508 | { |
| 509 | break; |
| 510 | } |
| 511 | last_iiaccumulator = IIAccumulator; |
| 512 | } |
| 513 | |
| 514 | Boost = IIAccumulator*100.0/16.0; |
| 515 | cpi->baseline_gf_interval = i; |
| 516 | |
| 517 | */ |
| 518 | #else |
| 519 | |
| 520 | /*************************************************************/ |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 521 | /* OLD code */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 522 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 523 | /* Adjust boost based upon ambient Q */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 524 | Boost = GFQ_ADJUSTMENT; |
| 525 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 526 | /* Adjust based upon most recently measure intra useage */ |
John Koleszar | 429dc67 | 2011-03-17 17:07:59 -0400 | [diff] [blame] | 527 | Boost = Boost * gf_intra_usage_adjustment[(cpi->this_frame_percent_intra < 15) ? cpi->this_frame_percent_intra : 14] / 100; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 528 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 529 | /* Adjust gf boost based upon GF usage since last GF */ |
John Koleszar | 429dc67 | 2011-03-17 17:07:59 -0400 | [diff] [blame] | 530 | Boost = Boost * gf_adjust_table[gf_frame_useage] / 100; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 531 | #endif |
| 532 | } |
| 533 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 534 | /* golden frame boost without recode loop often goes awry. be |
| 535 | * safe by keeping numbers down. |
| 536 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 537 | if (!cpi->sf.recode_loop) |
| 538 | { |
| 539 | if (cpi->compressor_speed == 2) |
| 540 | Boost = Boost / 2; |
| 541 | } |
| 542 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 543 | /* Apply an upper limit based on Q for 1 pass encodes */ |
John Koleszar | 429dc67 | 2011-03-17 17:07:59 -0400 | [diff] [blame] | 544 | if (Boost > kf_gf_boost_qlimits[Q] && (cpi->pass == 0)) |
| 545 | Boost = kf_gf_boost_qlimits[Q]; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 546 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 547 | /* Apply lower limits to boost. */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 548 | else if (Boost < 110) |
| 549 | Boost = 110; |
| 550 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 551 | /* Note the boost used */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 552 | cpi->last_boost = Boost; |
| 553 | |
| 554 | } |
| 555 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 556 | /* Estimate next interval |
| 557 | * This is updated once the real frame size/boost is known. |
| 558 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 559 | if (cpi->oxcf.fixed_q == -1) |
| 560 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 561 | if (cpi->pass == 2) /* 2 Pass */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 562 | { |
| 563 | cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; |
| 564 | } |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 565 | else /* 1 Pass */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 566 | { |
| 567 | cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; |
| 568 | |
| 569 | if (cpi->last_boost > 750) |
| 570 | cpi->frames_till_gf_update_due++; |
| 571 | |
| 572 | if (cpi->last_boost > 1000) |
| 573 | cpi->frames_till_gf_update_due++; |
| 574 | |
| 575 | if (cpi->last_boost > 1250) |
| 576 | cpi->frames_till_gf_update_due++; |
| 577 | |
| 578 | if (cpi->last_boost >= 1500) |
| 579 | cpi->frames_till_gf_update_due ++; |
| 580 | |
John Koleszar | 429dc67 | 2011-03-17 17:07:59 -0400 | [diff] [blame] | 581 | if (gf_interval_table[gf_frame_useage] > cpi->frames_till_gf_update_due) |
| 582 | cpi->frames_till_gf_update_due = gf_interval_table[gf_frame_useage]; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 583 | |
| 584 | if (cpi->frames_till_gf_update_due > cpi->max_gf_interval) |
| 585 | cpi->frames_till_gf_update_due = cpi->max_gf_interval; |
| 586 | } |
| 587 | } |
| 588 | else |
| 589 | cpi->frames_till_gf_update_due = cpi->baseline_gf_interval; |
| 590 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 591 | /* ARF on or off */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 592 | if (cpi->pass != 2) |
| 593 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 594 | /* For now Alt ref is not allowed except in 2 pass modes. */ |
John Koleszar | f56918b | 2011-12-21 14:21:29 -0800 | [diff] [blame] | 595 | cpi->source_alt_ref_pending = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 596 | |
| 597 | /*if ( cpi->oxcf.fixed_q == -1) |
| 598 | { |
| 599 | if ( cpi->oxcf.play_alternate && (cpi->last_boost > (100 + (AF_THRESH*cpi->frames_till_gf_update_due)) ) ) |
John Koleszar | f56918b | 2011-12-21 14:21:29 -0800 | [diff] [blame] | 600 | cpi->source_alt_ref_pending = 1; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 601 | else |
John Koleszar | f56918b | 2011-12-21 14:21:29 -0800 | [diff] [blame] | 602 | cpi->source_alt_ref_pending = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 603 | }*/ |
| 604 | } |
| 605 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 606 | |
| 607 | |
John Koleszar | 81d2206 | 2011-04-25 15:02:54 -0400 | [diff] [blame] | 608 | static void calc_pframe_target_size(VP8_COMP *cpi) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 609 | { |
John Koleszar | db67dcb | 2011-06-23 11:47:09 -0400 | [diff] [blame] | 610 | int min_frame_target; |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 611 | int old_per_frame_bandwidth = cpi->per_frame_bandwidth; |
| 612 | |
| 613 | if ( cpi->current_layer > 0) |
| 614 | cpi->per_frame_bandwidth = |
| 615 | cpi->layer_context[cpi->current_layer].avg_frame_size_for_layer; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 616 | |
John Koleszar | e961317 | 2011-08-12 14:51:36 -0400 | [diff] [blame] | 617 | min_frame_target = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 618 | |
| 619 | if (cpi->pass == 2) |
| 620 | { |
| 621 | min_frame_target = cpi->min_frame_bandwidth; |
| 622 | |
| 623 | if (min_frame_target < (cpi->av_per_frame_bandwidth >> 5)) |
| 624 | min_frame_target = cpi->av_per_frame_bandwidth >> 5; |
John Koleszar | 212f618 | 2011-05-03 11:17:05 -0400 | [diff] [blame] | 625 | } |
John Koleszar | e961317 | 2011-08-12 14:51:36 -0400 | [diff] [blame] | 626 | else if (min_frame_target < cpi->per_frame_bandwidth / 4) |
| 627 | min_frame_target = cpi->per_frame_bandwidth / 4; |
| 628 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 629 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 630 | /* Special alt reference frame case */ |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 631 | if((cpi->common.refresh_alt_ref_frame) && (cpi->oxcf.number_of_layers == 1)) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 632 | { |
| 633 | if (cpi->pass == 2) |
| 634 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 635 | /* Per frame bit target for the alt ref frame */ |
| 636 | cpi->per_frame_bandwidth = cpi->twopass.gf_bits; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 637 | cpi->this_frame_target = cpi->per_frame_bandwidth; |
| 638 | } |
| 639 | |
| 640 | /* One Pass ??? TBD */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 641 | } |
| 642 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 643 | /* Normal frames (gf,and inter) */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 644 | else |
| 645 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 646 | /* 2 pass */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 647 | if (cpi->pass == 2) |
| 648 | { |
| 649 | cpi->this_frame_target = cpi->per_frame_bandwidth; |
| 650 | } |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 651 | /* 1 pass */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 652 | else |
| 653 | { |
Scott LaVarnway | 96e41cb | 2013-03-14 09:49:38 -0700 | [diff] [blame] | 654 | int Adjustment; |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 655 | /* Make rate adjustment to recover bits spent in key frame |
| 656 | * Test to see if the key frame inter data rate correction |
| 657 | * should still be in force |
| 658 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 659 | if (cpi->kf_overspend_bits > 0) |
| 660 | { |
| 661 | Adjustment = (cpi->kf_bitrate_adjustment <= cpi->kf_overspend_bits) ? cpi->kf_bitrate_adjustment : cpi->kf_overspend_bits; |
| 662 | |
| 663 | if (Adjustment > (cpi->per_frame_bandwidth - min_frame_target)) |
| 664 | Adjustment = (cpi->per_frame_bandwidth - min_frame_target); |
| 665 | |
| 666 | cpi->kf_overspend_bits -= Adjustment; |
| 667 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 668 | /* Calculate an inter frame bandwidth target for the next |
| 669 | * few frames designed to recover any extra bits spent on |
| 670 | * the key frame. |
| 671 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 672 | cpi->this_frame_target = cpi->per_frame_bandwidth - Adjustment; |
| 673 | |
| 674 | if (cpi->this_frame_target < min_frame_target) |
| 675 | cpi->this_frame_target = min_frame_target; |
| 676 | } |
| 677 | else |
| 678 | cpi->this_frame_target = cpi->per_frame_bandwidth; |
| 679 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 680 | /* If appropriate make an adjustment to recover bits spent on a |
| 681 | * recent GF |
| 682 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 683 | if ((cpi->gf_overspend_bits > 0) && (cpi->this_frame_target > min_frame_target)) |
| 684 | { |
Scott LaVarnway | 96e41cb | 2013-03-14 09:49:38 -0700 | [diff] [blame] | 685 | Adjustment = (cpi->non_gf_bitrate_adjustment <= cpi->gf_overspend_bits) ? cpi->non_gf_bitrate_adjustment : cpi->gf_overspend_bits; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 686 | |
| 687 | if (Adjustment > (cpi->this_frame_target - min_frame_target)) |
| 688 | Adjustment = (cpi->this_frame_target - min_frame_target); |
| 689 | |
| 690 | cpi->gf_overspend_bits -= Adjustment; |
| 691 | cpi->this_frame_target -= Adjustment; |
| 692 | } |
| 693 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 694 | /* Apply small + and - boosts for non gf frames */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 695 | if ((cpi->last_boost > 150) && (cpi->frames_till_gf_update_due > 0) && |
| 696 | (cpi->current_gf_interval >= (MIN_GF_INTERVAL << 1))) |
| 697 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 698 | /* % Adjustment limited to the range 1% to 10% */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 699 | Adjustment = (cpi->last_boost - 100) >> 5; |
| 700 | |
| 701 | if (Adjustment < 1) |
| 702 | Adjustment = 1; |
| 703 | else if (Adjustment > 10) |
| 704 | Adjustment = 10; |
| 705 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 706 | /* Convert to bits */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 707 | Adjustment = (cpi->this_frame_target * Adjustment) / 100; |
| 708 | |
| 709 | if (Adjustment > (cpi->this_frame_target - min_frame_target)) |
| 710 | Adjustment = (cpi->this_frame_target - min_frame_target); |
| 711 | |
James Zern | 5f30a0c | 2013-07-18 14:09:21 -0700 | [diff] [blame] | 712 | if (cpi->frames_since_golden == (cpi->current_gf_interval >> 1)) |
Marco | af898b5 | 2014-11-10 13:07:05 -0800 | [diff] [blame] | 713 | { |
| 714 | Adjustment = (cpi->current_gf_interval - 1) * Adjustment; |
| 715 | // Limit adjustment to 10% of current target. |
| 716 | if (Adjustment > (10 * cpi->this_frame_target) / 100) |
| 717 | Adjustment = (10 * cpi->this_frame_target) / 100; |
| 718 | cpi->this_frame_target += Adjustment; |
| 719 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 720 | else |
| 721 | cpi->this_frame_target -= Adjustment; |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 726 | /* Sanity check that the total sum of adjustments is not above the |
| 727 | * maximum allowed That is that having allowed for KF and GF penalties |
| 728 | * we have not pushed the current interframe target to low. If the |
| 729 | * adjustment we apply here is not capable of recovering all the extra |
| 730 | * bits we have spent in the KF or GF then the remainder will have to |
| 731 | * be recovered over a longer time span via other buffer / rate control |
| 732 | * mechanisms. |
| 733 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 734 | if (cpi->this_frame_target < min_frame_target) |
| 735 | cpi->this_frame_target = min_frame_target; |
| 736 | |
| 737 | if (!cpi->common.refresh_alt_ref_frame) |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 738 | /* Note the baseline target data rate for this inter frame. */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 739 | cpi->inter_frame_target = cpi->this_frame_target; |
| 740 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 741 | /* One Pass specific code */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 742 | if (cpi->pass == 0) |
| 743 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 744 | /* Adapt target frame size with respect to any buffering constraints: */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 745 | if (cpi->buffered_mode) |
| 746 | { |
Yaowu Xu | d71ba03 | 2012-08-17 10:05:35 -0700 | [diff] [blame] | 747 | int one_percent_bits = (int) |
| 748 | (1 + cpi->oxcf.optimal_buffer_level / 100); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 749 | |
Paul Wilkins | 2ae91fb | 2011-03-10 16:11:39 +0000 | [diff] [blame] | 750 | if ((cpi->buffer_level < cpi->oxcf.optimal_buffer_level) || |
| 751 | (cpi->bits_off_target < cpi->oxcf.optimal_buffer_level)) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 752 | { |
| 753 | int percent_low = 0; |
| 754 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 755 | /* Decide whether or not we need to adjust the frame data |
| 756 | * rate target. |
| 757 | * |
| 758 | * If we are are below the optimal buffer fullness level |
| 759 | * and adherence to buffering constraints is important to |
| 760 | * the end usage then adjust the per frame target. |
| 761 | */ |
Paul Wilkins | 2ae91fb | 2011-03-10 16:11:39 +0000 | [diff] [blame] | 762 | if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) && |
| 763 | (cpi->buffer_level < cpi->oxcf.optimal_buffer_level)) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 764 | { |
Yaowu Xu | d71ba03 | 2012-08-17 10:05:35 -0700 | [diff] [blame] | 765 | percent_low = (int) |
| 766 | ((cpi->oxcf.optimal_buffer_level - cpi->buffer_level) / |
| 767 | one_percent_bits); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 768 | } |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 769 | /* Are we overshooting the long term clip data rate... */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 770 | else if (cpi->bits_off_target < 0) |
| 771 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 772 | /* Adjust per frame data target downwards to compensate. */ |
Paul Wilkins | 2ae91fb | 2011-03-10 16:11:39 +0000 | [diff] [blame] | 773 | percent_low = (int)(100 * -cpi->bits_off_target / |
| 774 | (cpi->total_byte_count * 8)); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 775 | } |
| 776 | |
John Koleszar | c99f9d7 | 2011-04-11 11:29:23 -0400 | [diff] [blame] | 777 | if (percent_low > cpi->oxcf.under_shoot_pct) |
| 778 | percent_low = cpi->oxcf.under_shoot_pct; |
| 779 | else if (percent_low < 0) |
| 780 | percent_low = 0; |
| 781 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 782 | /* lower the target bandwidth for this frame. */ |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 783 | cpi->this_frame_target -= |
| 784 | (cpi->this_frame_target * percent_low) / 200; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 785 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 786 | /* Are we using allowing control of active_worst_allowed_q |
| 787 | * according to buffer level. |
| 788 | */ |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 789 | if (cpi->auto_worst_q && cpi->ni_frames > 150) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 790 | { |
Yaowu Xu | d71ba03 | 2012-08-17 10:05:35 -0700 | [diff] [blame] | 791 | int64_t critical_buffer_level; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 792 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 793 | /* For streaming applications the most important factor is |
| 794 | * cpi->buffer_level as this takes into account the |
| 795 | * specified short term buffering constraints. However, |
| 796 | * hitting the long term clip data rate target is also |
| 797 | * important. |
| 798 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 799 | if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) |
| 800 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 801 | /* Take the smaller of cpi->buffer_level and |
| 802 | * cpi->bits_off_target |
| 803 | */ |
Paul Wilkins | 2ae91fb | 2011-03-10 16:11:39 +0000 | [diff] [blame] | 804 | critical_buffer_level = |
| 805 | (cpi->buffer_level < cpi->bits_off_target) |
| 806 | ? cpi->buffer_level : cpi->bits_off_target; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 807 | } |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 808 | /* For local file playback short term buffering constraints |
| 809 | * are less of an issue |
| 810 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 811 | else |
| 812 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 813 | /* Consider only how we are doing for the clip as a |
| 814 | * whole |
| 815 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 816 | critical_buffer_level = cpi->bits_off_target; |
| 817 | } |
| 818 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 819 | /* Set the active worst quality based upon the selected |
| 820 | * buffer fullness number. |
| 821 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 822 | if (critical_buffer_level < cpi->oxcf.optimal_buffer_level) |
| 823 | { |
Paul Wilkins | 2ae91fb | 2011-03-10 16:11:39 +0000 | [diff] [blame] | 824 | if ( critical_buffer_level > |
| 825 | (cpi->oxcf.optimal_buffer_level >> 2) ) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 826 | { |
James Zern | b45065d | 2011-07-25 18:44:59 -0700 | [diff] [blame] | 827 | int64_t qadjustment_range = |
Paul Wilkins | 2ae91fb | 2011-03-10 16:11:39 +0000 | [diff] [blame] | 828 | cpi->worst_quality - cpi->ni_av_qi; |
James Zern | b45065d | 2011-07-25 18:44:59 -0700 | [diff] [blame] | 829 | int64_t above_base = |
Paul Wilkins | 2ae91fb | 2011-03-10 16:11:39 +0000 | [diff] [blame] | 830 | (critical_buffer_level - |
| 831 | (cpi->oxcf.optimal_buffer_level >> 2)); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 832 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 833 | /* Step active worst quality down from |
| 834 | * cpi->ni_av_qi when (critical_buffer_level == |
| 835 | * cpi->optimal_buffer_level) to |
| 836 | * cpi->worst_quality when |
| 837 | * (critical_buffer_level == |
| 838 | * cpi->optimal_buffer_level >> 2) |
| 839 | */ |
Paul Wilkins | 2ae91fb | 2011-03-10 16:11:39 +0000 | [diff] [blame] | 840 | cpi->active_worst_quality = |
| 841 | cpi->worst_quality - |
Yaowu Xu | d71ba03 | 2012-08-17 10:05:35 -0700 | [diff] [blame] | 842 | (int)((qadjustment_range * above_base) / |
Paul Wilkins | 2ae91fb | 2011-03-10 16:11:39 +0000 | [diff] [blame] | 843 | (cpi->oxcf.optimal_buffer_level*3>>2)); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 844 | } |
| 845 | else |
| 846 | { |
| 847 | cpi->active_worst_quality = cpi->worst_quality; |
| 848 | } |
| 849 | } |
| 850 | else |
| 851 | { |
| 852 | cpi->active_worst_quality = cpi->ni_av_qi; |
| 853 | } |
| 854 | } |
| 855 | else |
| 856 | { |
| 857 | cpi->active_worst_quality = cpi->worst_quality; |
| 858 | } |
| 859 | } |
| 860 | else |
| 861 | { |
John Koleszar | c99f9d7 | 2011-04-11 11:29:23 -0400 | [diff] [blame] | 862 | int percent_high = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 863 | |
John Koleszar | c99f9d7 | 2011-04-11 11:29:23 -0400 | [diff] [blame] | 864 | if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) |
| 865 | && (cpi->buffer_level > cpi->oxcf.optimal_buffer_level)) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 866 | { |
Yaowu Xu | d71ba03 | 2012-08-17 10:05:35 -0700 | [diff] [blame] | 867 | percent_high = (int)((cpi->buffer_level |
John Koleszar | c99f9d7 | 2011-04-11 11:29:23 -0400 | [diff] [blame] | 868 | - cpi->oxcf.optimal_buffer_level) |
Yaowu Xu | d71ba03 | 2012-08-17 10:05:35 -0700 | [diff] [blame] | 869 | / one_percent_bits); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 870 | } |
John Koleszar | c99f9d7 | 2011-04-11 11:29:23 -0400 | [diff] [blame] | 871 | else if (cpi->bits_off_target > cpi->oxcf.optimal_buffer_level) |
| 872 | { |
| 873 | percent_high = (int)((100 * cpi->bits_off_target) |
| 874 | / (cpi->total_byte_count * 8)); |
| 875 | } |
| 876 | |
| 877 | if (percent_high > cpi->oxcf.over_shoot_pct) |
| 878 | percent_high = cpi->oxcf.over_shoot_pct; |
| 879 | else if (percent_high < 0) |
| 880 | percent_high = 0; |
| 881 | |
| 882 | cpi->this_frame_target += (cpi->this_frame_target * |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 883 | percent_high) / 200; |
John Koleszar | c99f9d7 | 2011-04-11 11:29:23 -0400 | [diff] [blame] | 884 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 885 | /* Are we allowing control of active_worst_allowed_q according |
| 886 | * to buffer level. |
| 887 | */ |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 888 | if (cpi->auto_worst_q && cpi->ni_frames > 150) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 889 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 890 | /* When using the relaxed buffer model stick to the |
| 891 | * user specified value |
| 892 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 893 | cpi->active_worst_quality = cpi->ni_av_qi; |
| 894 | } |
| 895 | else |
| 896 | { |
| 897 | cpi->active_worst_quality = cpi->worst_quality; |
| 898 | } |
| 899 | } |
| 900 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 901 | /* Set active_best_quality to prevent quality rising too high */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 902 | cpi->active_best_quality = cpi->best_quality; |
| 903 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 904 | /* Worst quality obviously must not be better than best quality */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 905 | if (cpi->active_worst_quality <= cpi->active_best_quality) |
| 906 | cpi->active_worst_quality = cpi->active_best_quality + 1; |
| 907 | |
James Berry | f8b431c | 2011-12-06 13:08:44 -0500 | [diff] [blame] | 908 | if(cpi->active_worst_quality > 127) |
| 909 | cpi->active_worst_quality = 127; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 910 | } |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 911 | /* Unbuffered mode (eg. video conferencing) */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 912 | else |
| 913 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 914 | /* Set the active worst quality */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 915 | cpi->active_worst_quality = cpi->worst_quality; |
| 916 | } |
Paul Wilkins | 2ae91fb | 2011-03-10 16:11:39 +0000 | [diff] [blame] | 917 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 918 | /* Special trap for constrained quality mode |
| 919 | * "active_worst_quality" may never drop below cq level |
| 920 | * for any frame type. |
| 921 | */ |
Paul Wilkins | 2ae91fb | 2011-03-10 16:11:39 +0000 | [diff] [blame] | 922 | if ( cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY && |
| 923 | cpi->active_worst_quality < cpi->cq_target_quality) |
| 924 | { |
| 925 | cpi->active_worst_quality = cpi->cq_target_quality; |
| 926 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 927 | } |
| 928 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 929 | /* Test to see if we have to drop a frame |
| 930 | * The auto-drop frame code is only used in buffered mode. |
| 931 | * In unbufferd mode (eg vide conferencing) the descision to |
| 932 | * code or drop a frame is made outside the codec in response to real |
| 933 | * world comms or buffer considerations. |
| 934 | */ |
Yunqing Wang | 9904c28 | 2012-06-07 11:26:47 -0400 | [diff] [blame] | 935 | if (cpi->drop_frames_allowed && |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 936 | (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) && |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 937 | ((cpi->common.frame_type != KEY_FRAME))) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 938 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 939 | /* Check for a buffer underun-crisis in which case we have to drop |
| 940 | * a frame |
| 941 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 942 | if ((cpi->buffer_level < 0)) |
| 943 | { |
| 944 | #if 0 |
| 945 | FILE *f = fopen("dec.stt", "a"); |
| 946 | fprintf(f, "%10d %10d %10d %10d ***** BUFFER EMPTY\n", |
| 947 | (int) cpi->common.current_video_frame, |
| 948 | cpi->decimation_factor, cpi->common.horiz_scale, |
| 949 | (cpi->buffer_level * 100) / cpi->oxcf.optimal_buffer_level); |
| 950 | fclose(f); |
| 951 | #endif |
John Koleszar | f56918b | 2011-12-21 14:21:29 -0800 | [diff] [blame] | 952 | cpi->drop_frame = 1; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 953 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 954 | /* Update the buffer level variable. */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 955 | cpi->bits_off_target += cpi->av_per_frame_bandwidth; |
Adrian Grange | b615a6d | 2011-11-07 17:15:28 -0800 | [diff] [blame] | 956 | if (cpi->bits_off_target > cpi->oxcf.maximum_buffer_size) |
Yaowu Xu | d71ba03 | 2012-08-17 10:05:35 -0700 | [diff] [blame] | 957 | cpi->bits_off_target = (int)cpi->oxcf.maximum_buffer_size; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 958 | cpi->buffer_level = cpi->bits_off_target; |
Marco Paniconi | 41138e0 | 2013-11-01 11:03:03 -0700 | [diff] [blame] | 959 | |
| 960 | if (cpi->oxcf.number_of_layers > 1) { |
| 961 | unsigned int i; |
| 962 | |
| 963 | // Propagate bits saved by dropping the frame to higher layers. |
| 964 | for (i = cpi->current_layer + 1; i < cpi->oxcf.number_of_layers; |
| 965 | i++) { |
| 966 | LAYER_CONTEXT *lc = &cpi->layer_context[i]; |
| 967 | lc->bits_off_target += (int)(lc->target_bandwidth / |
| 968 | lc->framerate); |
| 969 | if (lc->bits_off_target > lc->maximum_buffer_size) |
| 970 | lc->bits_off_target = lc->maximum_buffer_size; |
| 971 | lc->buffer_level = lc->bits_off_target; |
| 972 | } |
| 973 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 974 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 975 | } |
| 976 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 977 | /* Adjust target frame size for Golden Frames: */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 978 | if (cpi->oxcf.error_resilient_mode == 0 && |
| 979 | (cpi->frames_till_gf_update_due == 0) && !cpi->drop_frame) |
| 980 | { |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 981 | int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q; |
| 982 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 983 | int gf_frame_useage = 0; /* Golden frame useage since last GF */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 984 | int tot_mbs = cpi->recent_ref_frame_usage[INTRA_FRAME] + |
| 985 | cpi->recent_ref_frame_usage[LAST_FRAME] + |
| 986 | cpi->recent_ref_frame_usage[GOLDEN_FRAME] + |
| 987 | cpi->recent_ref_frame_usage[ALTREF_FRAME]; |
| 988 | |
Scott LaVarnway | 99f46d6 | 2010-08-11 11:02:31 -0400 | [diff] [blame] | 989 | int pct_gf_active = (100 * cpi->gf_active_count) / (cpi->common.mb_rows * cpi->common.mb_cols); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 990 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 991 | if (tot_mbs) |
| 992 | gf_frame_useage = (cpi->recent_ref_frame_usage[GOLDEN_FRAME] + cpi->recent_ref_frame_usage[ALTREF_FRAME]) * 100 / tot_mbs; |
| 993 | |
| 994 | if (pct_gf_active > gf_frame_useage) |
| 995 | gf_frame_useage = pct_gf_active; |
| 996 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 997 | /* Is a fixed manual GF frequency being used */ |
Patrik Westin | 8534071 | 2010-11-16 11:06:00 +0100 | [diff] [blame] | 998 | if (cpi->auto_gold) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 999 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1000 | /* For one pass throw a GF if recent frame intra useage is |
| 1001 | * low or the GF useage is high |
| 1002 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1003 | if ((cpi->pass == 0) && (cpi->this_frame_percent_intra < 15 || gf_frame_useage >= 5)) |
John Koleszar | f56918b | 2011-12-21 14:21:29 -0800 | [diff] [blame] | 1004 | cpi->common.refresh_golden_frame = 1; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1005 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1006 | /* Two pass GF descision */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1007 | else if (cpi->pass == 2) |
John Koleszar | f56918b | 2011-12-21 14:21:29 -0800 | [diff] [blame] | 1008 | cpi->common.refresh_golden_frame = 1; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | #if 0 |
| 1012 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1013 | /* Debug stats */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1014 | if (0) |
| 1015 | { |
| 1016 | FILE *f; |
| 1017 | |
| 1018 | f = fopen("gf_useaget.stt", "a"); |
| 1019 | fprintf(f, " %8ld %10ld %10ld %10ld %10ld\n", |
| 1020 | cpi->common.current_video_frame, cpi->gfu_boost, GFQ_ADJUSTMENT, cpi->gfu_boost, gf_frame_useage); |
| 1021 | fclose(f); |
| 1022 | } |
| 1023 | |
| 1024 | #endif |
| 1025 | |
John Koleszar | f56918b | 2011-12-21 14:21:29 -0800 | [diff] [blame] | 1026 | if (cpi->common.refresh_golden_frame == 1) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1027 | { |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1028 | #if 0 |
| 1029 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1030 | if (0) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1031 | { |
| 1032 | FILE *f; |
| 1033 | |
| 1034 | f = fopen("GFexit.stt", "a"); |
| 1035 | fprintf(f, "%8ld GF coded\n", cpi->common.current_video_frame); |
| 1036 | fclose(f); |
| 1037 | } |
| 1038 | |
| 1039 | #endif |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1040 | |
| 1041 | if (cpi->auto_adjust_gold_quantizer) |
| 1042 | { |
| 1043 | calc_gf_params(cpi); |
| 1044 | } |
| 1045 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1046 | /* If we are using alternate ref instead of gf then do not apply the |
| 1047 | * boost It will instead be applied to the altref update Jims |
| 1048 | * modified boost |
| 1049 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1050 | if (!cpi->source_alt_ref_active) |
| 1051 | { |
| 1052 | if (cpi->oxcf.fixed_q < 0) |
| 1053 | { |
| 1054 | if (cpi->pass == 2) |
| 1055 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1056 | /* The spend on the GF is defined in the two pass |
| 1057 | * code for two pass encodes |
| 1058 | */ |
| 1059 | cpi->this_frame_target = cpi->per_frame_bandwidth; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1060 | } |
| 1061 | else |
| 1062 | { |
| 1063 | int Boost = cpi->last_boost; |
| 1064 | int frames_in_section = cpi->frames_till_gf_update_due + 1; |
| 1065 | int allocation_chunks = (frames_in_section * 100) + (Boost - 100); |
| 1066 | int bits_in_section = cpi->inter_frame_target * frames_in_section; |
| 1067 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1068 | /* Normalize Altboost and allocations chunck down to |
| 1069 | * prevent overflow |
| 1070 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1071 | while (Boost > 1000) |
| 1072 | { |
| 1073 | Boost /= 2; |
| 1074 | allocation_chunks /= 2; |
| 1075 | } |
| 1076 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1077 | /* Avoid loss of precision but avoid overflow */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1078 | if ((bits_in_section >> 7) > allocation_chunks) |
| 1079 | cpi->this_frame_target = Boost * (bits_in_section / allocation_chunks); |
| 1080 | else |
| 1081 | cpi->this_frame_target = (Boost * bits_in_section) / allocation_chunks; |
| 1082 | } |
| 1083 | } |
| 1084 | else |
John Koleszar | db5057c | 2011-04-26 16:45:30 -0400 | [diff] [blame] | 1085 | cpi->this_frame_target = |
| 1086 | (estimate_bits_at_q(1, Q, cpi->common.MBs, 1.0) |
| 1087 | * cpi->last_boost) / 100; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1088 | |
| 1089 | } |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1090 | /* If there is an active ARF at this location use the minimum |
| 1091 | * bits on this frame even if it is a contructed arf. |
| 1092 | * The active maximum quantizer insures that an appropriate |
| 1093 | * number of bits will be spent if needed for contstructed ARFs. |
| 1094 | */ |
Paul Wilkins | bf18069 | 2010-07-19 14:10:07 +0100 | [diff] [blame] | 1095 | else |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1096 | { |
Paul Wilkins | bf18069 | 2010-07-19 14:10:07 +0100 | [diff] [blame] | 1097 | cpi->this_frame_target = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | cpi->current_gf_interval = cpi->frames_till_gf_update_due; |
| 1101 | |
| 1102 | } |
| 1103 | } |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 1104 | |
| 1105 | cpi->per_frame_bandwidth = old_per_frame_bandwidth; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | |
| 1109 | void vp8_update_rate_correction_factors(VP8_COMP *cpi, int damp_var) |
| 1110 | { |
| 1111 | int Q = cpi->common.base_qindex; |
| 1112 | int correction_factor = 100; |
| 1113 | double rate_correction_factor; |
| 1114 | double adjustment_limit; |
| 1115 | |
| 1116 | int projected_size_based_on_q = 0; |
| 1117 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1118 | /* Clear down mmx registers to allow floating point in what follows */ |
| 1119 | vp8_clear_system_state(); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1120 | |
| 1121 | if (cpi->common.frame_type == KEY_FRAME) |
| 1122 | { |
| 1123 | rate_correction_factor = cpi->key_frame_rate_correction_factor; |
| 1124 | } |
| 1125 | else |
| 1126 | { |
Marco Paniconi | 464b1df | 2012-11-26 14:14:50 -0800 | [diff] [blame] | 1127 | if (cpi->oxcf.number_of_layers == 1 && |
| 1128 | (cpi->common.refresh_alt_ref_frame || |
| 1129 | cpi->common.refresh_golden_frame)) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1130 | rate_correction_factor = cpi->gf_rate_correction_factor; |
| 1131 | else |
| 1132 | rate_correction_factor = cpi->rate_correction_factor; |
| 1133 | } |
| 1134 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1135 | /* Work out how big we would have expected the frame to be at this Q |
| 1136 | * given the current correction factor. Stay in double to avoid int |
| 1137 | * overflow when values are large |
| 1138 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1139 | projected_size_based_on_q = (int)(((.5 + rate_correction_factor * vp8_bits_per_mb[cpi->common.frame_type][Q]) * cpi->common.MBs) / (1 << BPER_MB_NORMBITS)); |
| 1140 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1141 | /* Make some allowance for cpi->zbin_over_quant */ |
Scott LaVarnway | 3a19eeb | 2012-12-10 10:51:42 -0800 | [diff] [blame] | 1142 | if (cpi->mb.zbin_over_quant > 0) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1143 | { |
Scott LaVarnway | 3a19eeb | 2012-12-10 10:51:42 -0800 | [diff] [blame] | 1144 | int Z = cpi->mb.zbin_over_quant; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1145 | double Factor = 0.99; |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1146 | double factor_adjustment = 0.01 / 256.0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1147 | |
| 1148 | while (Z > 0) |
| 1149 | { |
| 1150 | Z --; |
Paul Wilkins | c012d63 | 2010-05-20 16:49:39 +0100 | [diff] [blame] | 1151 | projected_size_based_on_q = |
| 1152 | (int)(Factor * projected_size_based_on_q); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1153 | Factor += factor_adjustment; |
| 1154 | |
| 1155 | if (Factor >= 0.999) |
| 1156 | Factor = 0.999; |
| 1157 | } |
| 1158 | } |
| 1159 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1160 | /* Work out a size correction factor. */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1161 | if (projected_size_based_on_q > 0) |
| 1162 | correction_factor = (100 * cpi->projected_frame_size) / projected_size_based_on_q; |
| 1163 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1164 | /* More heavily damped adjustment used if we have been oscillating |
| 1165 | * either side of target |
| 1166 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1167 | switch (damp_var) |
| 1168 | { |
| 1169 | case 0: |
| 1170 | adjustment_limit = 0.75; |
| 1171 | break; |
| 1172 | case 1: |
| 1173 | adjustment_limit = 0.375; |
| 1174 | break; |
| 1175 | case 2: |
| 1176 | default: |
| 1177 | adjustment_limit = 0.25; |
| 1178 | break; |
| 1179 | } |
| 1180 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1181 | if (correction_factor > 102) |
| 1182 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1183 | /* We are not already at the worst allowable quality */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1184 | correction_factor = (int)(100.5 + ((correction_factor - 100) * adjustment_limit)); |
| 1185 | rate_correction_factor = ((rate_correction_factor * correction_factor) / 100); |
| 1186 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1187 | /* Keep rate_correction_factor within limits */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1188 | if (rate_correction_factor > MAX_BPB_FACTOR) |
| 1189 | rate_correction_factor = MAX_BPB_FACTOR; |
| 1190 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1191 | else if (correction_factor < 99) |
| 1192 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1193 | /* We are not already at the best allowable quality */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1194 | correction_factor = (int)(100.5 - ((100 - correction_factor) * adjustment_limit)); |
| 1195 | rate_correction_factor = ((rate_correction_factor * correction_factor) / 100); |
| 1196 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1197 | /* Keep rate_correction_factor within limits */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1198 | if (rate_correction_factor < MIN_BPB_FACTOR) |
| 1199 | rate_correction_factor = MIN_BPB_FACTOR; |
| 1200 | } |
| 1201 | |
| 1202 | if (cpi->common.frame_type == KEY_FRAME) |
| 1203 | cpi->key_frame_rate_correction_factor = rate_correction_factor; |
| 1204 | else |
| 1205 | { |
Marco Paniconi | 464b1df | 2012-11-26 14:14:50 -0800 | [diff] [blame] | 1206 | if (cpi->oxcf.number_of_layers == 1 && |
| 1207 | (cpi->common.refresh_alt_ref_frame || |
| 1208 | cpi->common.refresh_golden_frame)) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1209 | cpi->gf_rate_correction_factor = rate_correction_factor; |
| 1210 | else |
| 1211 | cpi->rate_correction_factor = rate_correction_factor; |
| 1212 | } |
| 1213 | } |
| 1214 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1215 | |
| 1216 | int vp8_regulate_q(VP8_COMP *cpi, int target_bits_per_frame) |
| 1217 | { |
| 1218 | int Q = cpi->active_worst_quality; |
| 1219 | |
Marco | 976f7f4 | 2015-04-28 08:29:48 -0700 | [diff] [blame] | 1220 | if (cpi->force_maxqp == 1) { |
| 1221 | cpi->active_worst_quality = cpi->worst_quality; |
| 1222 | return cpi->worst_quality; |
| 1223 | } |
| 1224 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1225 | /* Reset Zbin OQ value */ |
Scott LaVarnway | 3a19eeb | 2012-12-10 10:51:42 -0800 | [diff] [blame] | 1226 | cpi->mb.zbin_over_quant = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1227 | |
| 1228 | if (cpi->oxcf.fixed_q >= 0) |
| 1229 | { |
| 1230 | Q = cpi->oxcf.fixed_q; |
| 1231 | |
| 1232 | if (cpi->common.frame_type == KEY_FRAME) |
| 1233 | { |
| 1234 | Q = cpi->oxcf.key_q; |
| 1235 | } |
Marco Paniconi | 464b1df | 2012-11-26 14:14:50 -0800 | [diff] [blame] | 1236 | else if (cpi->oxcf.number_of_layers == 1 && |
| 1237 | cpi->common.refresh_alt_ref_frame) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1238 | { |
| 1239 | Q = cpi->oxcf.alt_q; |
| 1240 | } |
Marco Paniconi | 464b1df | 2012-11-26 14:14:50 -0800 | [diff] [blame] | 1241 | else if (cpi->oxcf.number_of_layers == 1 && |
| 1242 | cpi->common.refresh_golden_frame) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1243 | { |
| 1244 | Q = cpi->oxcf.gold_q; |
| 1245 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1246 | } |
| 1247 | else |
| 1248 | { |
| 1249 | int i; |
| 1250 | int last_error = INT_MAX; |
| 1251 | int target_bits_per_mb; |
| 1252 | int bits_per_mb_at_this_q; |
| 1253 | double correction_factor; |
| 1254 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1255 | /* Select the appropriate correction factor based upon type of frame. */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1256 | if (cpi->common.frame_type == KEY_FRAME) |
| 1257 | correction_factor = cpi->key_frame_rate_correction_factor; |
| 1258 | else |
| 1259 | { |
Marco Paniconi | 464b1df | 2012-11-26 14:14:50 -0800 | [diff] [blame] | 1260 | if (cpi->oxcf.number_of_layers == 1 && |
| 1261 | (cpi->common.refresh_alt_ref_frame || |
| 1262 | cpi->common.refresh_golden_frame)) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1263 | correction_factor = cpi->gf_rate_correction_factor; |
| 1264 | else |
| 1265 | correction_factor = cpi->rate_correction_factor; |
| 1266 | } |
| 1267 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1268 | /* Calculate required scaling factor based on target frame size and |
| 1269 | * size of frame produced using previous Q |
| 1270 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1271 | if (target_bits_per_frame >= (INT_MAX >> BPER_MB_NORMBITS)) |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1272 | /* Case where we would overflow int */ |
| 1273 | target_bits_per_mb = (target_bits_per_frame / cpi->common.MBs) << BPER_MB_NORMBITS; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1274 | else |
| 1275 | target_bits_per_mb = (target_bits_per_frame << BPER_MB_NORMBITS) / cpi->common.MBs; |
| 1276 | |
| 1277 | i = cpi->active_best_quality; |
| 1278 | |
| 1279 | do |
| 1280 | { |
| 1281 | bits_per_mb_at_this_q = (int)(.5 + correction_factor * vp8_bits_per_mb[cpi->common.frame_type][i]); |
| 1282 | |
| 1283 | if (bits_per_mb_at_this_q <= target_bits_per_mb) |
| 1284 | { |
| 1285 | if ((target_bits_per_mb - bits_per_mb_at_this_q) <= last_error) |
| 1286 | Q = i; |
| 1287 | else |
| 1288 | Q = i - 1; |
| 1289 | |
| 1290 | break; |
| 1291 | } |
| 1292 | else |
| 1293 | last_error = bits_per_mb_at_this_q - target_bits_per_mb; |
| 1294 | } |
| 1295 | while (++i <= cpi->active_worst_quality); |
| 1296 | |
| 1297 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1298 | /* If we are at MAXQ then enable Q over-run which seeks to claw |
| 1299 | * back additional bits through things like the RD multiplier |
| 1300 | * and zero bin size. |
| 1301 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1302 | if (Q >= MAXQ) |
| 1303 | { |
| 1304 | int zbin_oqmax; |
| 1305 | |
| 1306 | double Factor = 0.99; |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1307 | double factor_adjustment = 0.01 / 256.0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1308 | |
| 1309 | if (cpi->common.frame_type == KEY_FRAME) |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1310 | zbin_oqmax = 0; |
Marco Paniconi | 464b1df | 2012-11-26 14:14:50 -0800 | [diff] [blame] | 1311 | else if (cpi->oxcf.number_of_layers == 1 && |
| 1312 | (cpi->common.refresh_alt_ref_frame || |
| 1313 | (cpi->common.refresh_golden_frame && |
| 1314 | !cpi->source_alt_ref_active))) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1315 | zbin_oqmax = 16; |
| 1316 | else |
| 1317 | zbin_oqmax = ZBIN_OQ_MAX; |
| 1318 | |
| 1319 | /*{ |
| 1320 | double Factor = (double)target_bits_per_mb/(double)bits_per_mb_at_this_q; |
| 1321 | double Oq; |
| 1322 | |
| 1323 | Factor = Factor/1.2683; |
| 1324 | |
| 1325 | Oq = pow( Factor, (1.0/-0.165) ); |
| 1326 | |
| 1327 | if ( Oq > zbin_oqmax ) |
| 1328 | Oq = zbin_oqmax; |
| 1329 | |
| 1330 | cpi->zbin_over_quant = (int)Oq; |
| 1331 | }*/ |
| 1332 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1333 | /* Each incrment in the zbin is assumed to have a fixed effect |
| 1334 | * on bitrate. This is not of course true. The effect will be |
| 1335 | * highly clip dependent and may well have sudden steps. The |
| 1336 | * idea here is to acheive higher effective quantizers than the |
| 1337 | * normal maximum by expanding the zero bin and hence |
| 1338 | * decreasing the number of low magnitude non zero coefficients. |
| 1339 | */ |
Scott LaVarnway | 3a19eeb | 2012-12-10 10:51:42 -0800 | [diff] [blame] | 1340 | while (cpi->mb.zbin_over_quant < zbin_oqmax) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1341 | { |
Scott LaVarnway | 3a19eeb | 2012-12-10 10:51:42 -0800 | [diff] [blame] | 1342 | cpi->mb.zbin_over_quant ++; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1343 | |
Scott LaVarnway | 3a19eeb | 2012-12-10 10:51:42 -0800 | [diff] [blame] | 1344 | if (cpi->mb.zbin_over_quant > zbin_oqmax) |
| 1345 | cpi->mb.zbin_over_quant = zbin_oqmax; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1346 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1347 | /* Adjust bits_per_mb_at_this_q estimate */ |
Paul Wilkins | c012d63 | 2010-05-20 16:49:39 +0100 | [diff] [blame] | 1348 | bits_per_mb_at_this_q = (int)(Factor * bits_per_mb_at_this_q); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1349 | Factor += factor_adjustment; |
| 1350 | |
| 1351 | if (Factor >= 0.999) |
| 1352 | Factor = 0.999; |
| 1353 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1354 | /* Break out if we get down to the target rate */ |
| 1355 | if (bits_per_mb_at_this_q <= target_bits_per_mb) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1356 | break; |
| 1357 | } |
| 1358 | |
| 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | return Q; |
| 1363 | } |
| 1364 | |
John Koleszar | ad6a8ca | 2011-04-19 16:08:45 -0400 | [diff] [blame] | 1365 | |
| 1366 | static int estimate_keyframe_frequency(VP8_COMP *cpi) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1367 | { |
| 1368 | int i; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1369 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1370 | /* Average key frame frequency */ |
John Koleszar | ad6a8ca | 2011-04-19 16:08:45 -0400 | [diff] [blame] | 1371 | int av_key_frame_frequency = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1372 | |
John Koleszar | ad6a8ca | 2011-04-19 16:08:45 -0400 | [diff] [blame] | 1373 | /* First key frame at start of sequence is a special case. We have no |
| 1374 | * frequency data. |
| 1375 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1376 | if (cpi->key_frame_count == 1) |
| 1377 | { |
John Koleszar | ad6a8ca | 2011-04-19 16:08:45 -0400 | [diff] [blame] | 1378 | /* Assume a default of 1 kf every 2 seconds, or the max kf interval, |
| 1379 | * whichever is smaller. |
| 1380 | */ |
James Berry | 8d5ce81 | 2011-04-22 11:54:18 -0400 | [diff] [blame] | 1381 | int key_freq = cpi->oxcf.key_freq>0 ? cpi->oxcf.key_freq : 1; |
James Zern | 9581eb6 | 2013-07-12 17:12:46 -0700 | [diff] [blame] | 1382 | av_key_frame_frequency = 1 + (int)cpi->output_framerate * 2; |
James Berry | 8d5ce81 | 2011-04-22 11:54:18 -0400 | [diff] [blame] | 1383 | |
| 1384 | if (cpi->oxcf.auto_key && av_key_frame_frequency > key_freq) |
Marco Paniconi | ec6cf49 | 2013-01-31 10:46:39 -0800 | [diff] [blame] | 1385 | av_key_frame_frequency = key_freq; |
John Koleszar | ad6a8ca | 2011-04-19 16:08:45 -0400 | [diff] [blame] | 1386 | |
| 1387 | cpi->prior_key_frame_distance[KEY_FRAME_CONTEXT - 1] |
| 1388 | = av_key_frame_frequency; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1389 | } |
| 1390 | else |
| 1391 | { |
John Koleszar | ad6a8ca | 2011-04-19 16:08:45 -0400 | [diff] [blame] | 1392 | unsigned int total_weight = 0; |
Paul Wilkins | c239a1b | 2010-08-20 12:27:26 +0100 | [diff] [blame] | 1393 | int last_kf_interval = |
| 1394 | (cpi->frames_since_key > 0) ? cpi->frames_since_key : 1; |
| 1395 | |
John Koleszar | ad6a8ca | 2011-04-19 16:08:45 -0400 | [diff] [blame] | 1396 | /* reset keyframe context and calculate weighted average of last |
| 1397 | * KEY_FRAME_CONTEXT keyframes |
| 1398 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1399 | for (i = 0; i < KEY_FRAME_CONTEXT; i++) |
| 1400 | { |
| 1401 | if (i < KEY_FRAME_CONTEXT - 1) |
John Koleszar | ad6a8ca | 2011-04-19 16:08:45 -0400 | [diff] [blame] | 1402 | cpi->prior_key_frame_distance[i] |
| 1403 | = cpi->prior_key_frame_distance[i+1]; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1404 | else |
Paul Wilkins | c239a1b | 2010-08-20 12:27:26 +0100 | [diff] [blame] | 1405 | cpi->prior_key_frame_distance[i] = last_kf_interval; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1406 | |
John Koleszar | ad6a8ca | 2011-04-19 16:08:45 -0400 | [diff] [blame] | 1407 | av_key_frame_frequency += prior_key_frame_weight[i] |
| 1408 | * cpi->prior_key_frame_distance[i]; |
| 1409 | total_weight += prior_key_frame_weight[i]; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1410 | } |
| 1411 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1412 | av_key_frame_frequency /= total_weight; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1413 | |
| 1414 | } |
Marco Paniconi | ec6cf49 | 2013-01-31 10:46:39 -0800 | [diff] [blame] | 1415 | // TODO (marpan): Given the checks above, |av_key_frame_frequency| |
| 1416 | // should always be above 0. But for now we keep the sanity check in. |
| 1417 | if (av_key_frame_frequency == 0) |
| 1418 | av_key_frame_frequency = 1; |
John Koleszar | ad6a8ca | 2011-04-19 16:08:45 -0400 | [diff] [blame] | 1419 | return av_key_frame_frequency; |
| 1420 | } |
| 1421 | |
| 1422 | |
| 1423 | void vp8_adjust_key_frame_context(VP8_COMP *cpi) |
| 1424 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1425 | /* Clear down mmx registers to allow floating point in what follows */ |
John Koleszar | ad6a8ca | 2011-04-19 16:08:45 -0400 | [diff] [blame] | 1426 | vp8_clear_system_state(); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1427 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1428 | /* Do we have any key frame overspend to recover? */ |
| 1429 | /* Two-pass overspend handled elsewhere. */ |
John Koleszar | ad6a8ca | 2011-04-19 16:08:45 -0400 | [diff] [blame] | 1430 | if ((cpi->pass != 2) |
| 1431 | && (cpi->projected_frame_size > cpi->per_frame_bandwidth)) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1432 | { |
John Koleszar | ad6a8ca | 2011-04-19 16:08:45 -0400 | [diff] [blame] | 1433 | int overspend; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1434 | |
John Koleszar | ad6a8ca | 2011-04-19 16:08:45 -0400 | [diff] [blame] | 1435 | /* Update the count of key frame overspend to be recovered in |
| 1436 | * subsequent frames. A portion of the KF overspend is treated as gf |
| 1437 | * overspend (and hence recovered more quickly) as the kf is also a |
| 1438 | * gf. Otherwise the few frames following each kf tend to get more |
| 1439 | * bits allocated than those following other gfs. |
| 1440 | */ |
| 1441 | overspend = (cpi->projected_frame_size - cpi->per_frame_bandwidth); |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 1442 | |
| 1443 | if (cpi->oxcf.number_of_layers > 1) |
| 1444 | cpi->kf_overspend_bits += overspend; |
| 1445 | else |
| 1446 | { |
| 1447 | cpi->kf_overspend_bits += overspend * 7 / 8; |
| 1448 | cpi->gf_overspend_bits += overspend * 1 / 8; |
| 1449 | } |
John Koleszar | ad6a8ca | 2011-04-19 16:08:45 -0400 | [diff] [blame] | 1450 | |
| 1451 | /* Work out how much to try and recover per frame. */ |
| 1452 | cpi->kf_bitrate_adjustment = cpi->kf_overspend_bits |
| 1453 | / estimate_keyframe_frequency(cpi); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | cpi->frames_since_key = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1457 | cpi->key_frame_count++; |
| 1458 | } |
| 1459 | |
John Koleszar | fd6da3b | 2011-04-25 14:30:57 -0400 | [diff] [blame] | 1460 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1461 | void vp8_compute_frame_size_bounds(VP8_COMP *cpi, int *frame_under_shoot_limit, int *frame_over_shoot_limit) |
| 1462 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1463 | /* Set-up bounds on acceptable frame size: */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1464 | if (cpi->oxcf.fixed_q >= 0) |
| 1465 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1466 | /* Fixed Q scenario: frame size never outranges target |
| 1467 | * (there is no target!) |
| 1468 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1469 | *frame_under_shoot_limit = 0; |
| 1470 | *frame_over_shoot_limit = INT_MAX; |
| 1471 | } |
| 1472 | else |
| 1473 | { |
| 1474 | if (cpi->common.frame_type == KEY_FRAME) |
| 1475 | { |
| 1476 | *frame_over_shoot_limit = cpi->this_frame_target * 9 / 8; |
| 1477 | *frame_under_shoot_limit = cpi->this_frame_target * 7 / 8; |
| 1478 | } |
| 1479 | else |
| 1480 | { |
Adrian Grange | 217591f | 2011-10-06 15:49:11 -0700 | [diff] [blame] | 1481 | if (cpi->oxcf.number_of_layers > 1 || |
| 1482 | cpi->common.refresh_alt_ref_frame || |
| 1483 | cpi->common.refresh_golden_frame) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1484 | { |
| 1485 | *frame_over_shoot_limit = cpi->this_frame_target * 9 / 8; |
| 1486 | *frame_under_shoot_limit = cpi->this_frame_target * 7 / 8; |
| 1487 | } |
| 1488 | else |
| 1489 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1490 | /* For CBR take buffer fullness into account */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1491 | if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) |
| 1492 | { |
| 1493 | if (cpi->buffer_level >= ((cpi->oxcf.optimal_buffer_level + cpi->oxcf.maximum_buffer_size) >> 1)) |
| 1494 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1495 | /* Buffer is too full so relax overshoot and tighten |
| 1496 | * undershoot |
| 1497 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1498 | *frame_over_shoot_limit = cpi->this_frame_target * 12 / 8; |
| 1499 | *frame_under_shoot_limit = cpi->this_frame_target * 6 / 8; |
| 1500 | } |
| 1501 | else if (cpi->buffer_level <= (cpi->oxcf.optimal_buffer_level >> 1)) |
| 1502 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1503 | /* Buffer is too low so relax undershoot and tighten |
| 1504 | * overshoot |
| 1505 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1506 | *frame_over_shoot_limit = cpi->this_frame_target * 10 / 8; |
| 1507 | *frame_under_shoot_limit = cpi->this_frame_target * 4 / 8; |
| 1508 | } |
| 1509 | else |
| 1510 | { |
| 1511 | *frame_over_shoot_limit = cpi->this_frame_target * 11 / 8; |
| 1512 | *frame_under_shoot_limit = cpi->this_frame_target * 5 / 8; |
| 1513 | } |
| 1514 | } |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1515 | /* VBR and CQ mode */ |
| 1516 | /* Note that tighter restrictions here can help quality |
| 1517 | * but hurt encode speed |
| 1518 | */ |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1519 | else |
| 1520 | { |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1521 | /* Stron overshoot limit for constrained quality */ |
Paul Wilkins | e0846c9 | 2011-01-07 18:29:37 +0000 | [diff] [blame] | 1522 | if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) |
| 1523 | { |
| 1524 | *frame_over_shoot_limit = cpi->this_frame_target * 11 / 8; |
| 1525 | *frame_under_shoot_limit = cpi->this_frame_target * 2 / 8; |
| 1526 | } |
| 1527 | else |
| 1528 | { |
| 1529 | *frame_over_shoot_limit = cpi->this_frame_target * 11 / 8; |
| 1530 | *frame_under_shoot_limit = cpi->this_frame_target * 5 / 8; |
| 1531 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1532 | } |
| 1533 | } |
| 1534 | } |
Paul Wilkins | 6d84322 | 2012-02-29 12:32:46 -0800 | [diff] [blame] | 1535 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1536 | /* For very small rate targets where the fractional adjustment |
| 1537 | * (eg * 7/8) may be tiny make sure there is at least a minimum |
| 1538 | * range. |
| 1539 | */ |
Paul Wilkins | 6d84322 | 2012-02-29 12:32:46 -0800 | [diff] [blame] | 1540 | *frame_over_shoot_limit += 200; |
| 1541 | *frame_under_shoot_limit -= 200; |
| 1542 | if ( *frame_under_shoot_limit < 0 ) |
| 1543 | *frame_under_shoot_limit = 0; |
| 1544 | |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1545 | } |
| 1546 | } |
John Koleszar | 81d2206 | 2011-04-25 15:02:54 -0400 | [diff] [blame] | 1547 | |
| 1548 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1549 | /* return of 0 means drop frame */ |
John Koleszar | 81d2206 | 2011-04-25 15:02:54 -0400 | [diff] [blame] | 1550 | int vp8_pick_frame_size(VP8_COMP *cpi) |
| 1551 | { |
| 1552 | VP8_COMMON *cm = &cpi->common; |
| 1553 | |
John Koleszar | db5057c | 2011-04-26 16:45:30 -0400 | [diff] [blame] | 1554 | if (cm->frame_type == KEY_FRAME) |
John Koleszar | 81d2206 | 2011-04-25 15:02:54 -0400 | [diff] [blame] | 1555 | calc_iframe_target_size(cpi); |
John Koleszar | 81d2206 | 2011-04-25 15:02:54 -0400 | [diff] [blame] | 1556 | else |
| 1557 | { |
John Koleszar | 81d2206 | 2011-04-25 15:02:54 -0400 | [diff] [blame] | 1558 | calc_pframe_target_size(cpi); |
| 1559 | |
John Koleszar | 0164a1c | 2012-05-21 14:30:56 -0700 | [diff] [blame] | 1560 | /* Check if we're dropping the frame: */ |
John Koleszar | 81d2206 | 2011-04-25 15:02:54 -0400 | [diff] [blame] | 1561 | if (cpi->drop_frame) |
| 1562 | { |
John Koleszar | f56918b | 2011-12-21 14:21:29 -0800 | [diff] [blame] | 1563 | cpi->drop_frame = 0; |
John Koleszar | 81d2206 | 2011-04-25 15:02:54 -0400 | [diff] [blame] | 1564 | return 0; |
| 1565 | } |
| 1566 | } |
John Koleszar | 81d2206 | 2011-04-25 15:02:54 -0400 | [diff] [blame] | 1567 | return 1; |
| 1568 | } |
Marco | 976f7f4 | 2015-04-28 08:29:48 -0700 | [diff] [blame] | 1569 | // If this just encoded frame (mcomp/transform/quant, but before loopfilter and |
| 1570 | // pack_bitstream) has large overshoot, and was not being encoded close to the |
| 1571 | // max QP, then drop this frame and force next frame to be encoded at max QP. |
| 1572 | // Condition this on 1 pass CBR with screen content mode and frame dropper off. |
| 1573 | // TODO(marpan): Should do this exit condition during the encode_frame |
| 1574 | // (i.e., halfway during the encoding of the frame) to save cycles. |
| 1575 | int vp8_drop_encodedframe_overshoot(VP8_COMP *cpi, int Q) { |
| 1576 | if (cpi->pass == 0 && |
| 1577 | cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER && |
| 1578 | cpi->drop_frames_allowed == 0 && |
| 1579 | cpi->common.frame_type != KEY_FRAME) { |
| 1580 | // Note: the "projected_frame_size" from encode_frame() only gives estimate |
| 1581 | // of mode/motion vector rate (in non-rd mode): so below we only require |
| 1582 | // that projected_frame_size is somewhat greater than per-frame-bandwidth, |
| 1583 | // but add additional condition with high threshold on prediction residual. |
| 1584 | |
| 1585 | // QP threshold: only allow dropping if we are not close to qp_max. |
| 1586 | int thresh_qp = 3 * cpi->worst_quality >> 2; |
| 1587 | // Rate threshold, in bytes. |
| 1588 | int thresh_rate = 2 * (cpi->av_per_frame_bandwidth >> 3); |
| 1589 | // Threshold for the average (over all macroblocks) of the pixel-sum |
| 1590 | // residual error over 16x16 block. Should add QP dependence on threshold? |
| 1591 | int thresh_pred_err_mb = (256 << 4); |
Marco | 7ca1743 | 2015-05-21 14:07:21 -0700 | [diff] [blame] | 1592 | int pred_err_mb = (int)(cpi->mb.prediction_error / cpi->common.MBs); |
Marco | 976f7f4 | 2015-04-28 08:29:48 -0700 | [diff] [blame] | 1593 | if (Q < thresh_qp && |
| 1594 | cpi->projected_frame_size > thresh_rate && |
| 1595 | pred_err_mb > thresh_pred_err_mb) { |
Yaowu Xu | 7c514e2 | 2015-09-28 15:55:46 -0700 | [diff] [blame] | 1596 | double new_correction_factor = cpi->rate_correction_factor; |
| 1597 | const int target_size = cpi->av_per_frame_bandwidth; |
| 1598 | int target_bits_per_mb; |
Marco | 976f7f4 | 2015-04-28 08:29:48 -0700 | [diff] [blame] | 1599 | // Drop this frame: advance frame counters, and set force_maxqp flag. |
| 1600 | cpi->common.current_video_frame++; |
| 1601 | cpi->frames_since_key++; |
| 1602 | // Flag to indicate we will force next frame to be encoded at max QP. |
| 1603 | cpi->force_maxqp = 1; |
Yaowu Xu | 7c514e2 | 2015-09-28 15:55:46 -0700 | [diff] [blame] | 1604 | // Reset the buffer levels. |
| 1605 | cpi->buffer_level = cpi->oxcf.optimal_buffer_level; |
| 1606 | cpi->bits_off_target = cpi->oxcf.optimal_buffer_level; |
| 1607 | // Compute a new rate correction factor, corresponding to the current |
| 1608 | // target frame size and max_QP, and adjust the rate correction factor |
| 1609 | // upwards, if needed. |
| 1610 | // This is to prevent a bad state where the re-encoded frame at max_QP |
| 1611 | // undershoots significantly, and then we end up dropping every other |
| 1612 | // frame because the QP/rate_correction_factor may have been too low |
| 1613 | // before the drop and then takes too long to come up. |
| 1614 | if (target_size >= (INT_MAX >> BPER_MB_NORMBITS)) |
| 1615 | target_bits_per_mb = |
| 1616 | (target_size / cpi->common.MBs) << BPER_MB_NORMBITS; |
| 1617 | else |
| 1618 | target_bits_per_mb = |
| 1619 | (target_size << BPER_MB_NORMBITS) / cpi->common.MBs; |
| 1620 | // Rate correction factor based on target_size_per_mb and max_QP. |
| 1621 | new_correction_factor = (double)target_bits_per_mb / |
| 1622 | (double)vp8_bits_per_mb[INTER_FRAME][cpi->worst_quality]; |
| 1623 | if (new_correction_factor > cpi->rate_correction_factor) |
| 1624 | cpi->rate_correction_factor = |
| 1625 | VPXMIN(2.0 * cpi->rate_correction_factor, new_correction_factor); |
| 1626 | if (cpi->rate_correction_factor > MAX_BPB_FACTOR) |
| 1627 | cpi->rate_correction_factor = MAX_BPB_FACTOR; |
Marco | 976f7f4 | 2015-04-28 08:29:48 -0700 | [diff] [blame] | 1628 | return 1; |
| 1629 | } else { |
| 1630 | cpi->force_maxqp = 0; |
| 1631 | return 0; |
| 1632 | } |
Marco | 6f41e29 | 2015-05-20 12:20:17 -0700 | [diff] [blame] | 1633 | cpi->force_maxqp = 0; |
Marco | 976f7f4 | 2015-04-28 08:29:48 -0700 | [diff] [blame] | 1634 | return 0; |
| 1635 | } |
Marco | 6f41e29 | 2015-05-20 12:20:17 -0700 | [diff] [blame] | 1636 | cpi->force_maxqp = 0; |
Marco | 976f7f4 | 2015-04-28 08:29:48 -0700 | [diff] [blame] | 1637 | return 0; |
| 1638 | } |