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