| OLD | NEW |
| (Empty) |
| 1 // Copyright 2011 Google Inc. All Rights Reserved. | |
| 2 // | |
| 3 // Use of this source code is governed by a BSD-style license | |
| 4 // that can be found in the COPYING file in the root of the source | |
| 5 // tree. An additional intellectual property rights grant can be found | |
| 6 // in the file PATENTS. All contributing project authors may | |
| 7 // be found in the AUTHORS file in the root of the source tree. | |
| 8 // ----------------------------------------------------------------------------- | |
| 9 // | |
| 10 // frame coding and analysis | |
| 11 // | |
| 12 // Author: Skal (pascal.massimino@gmail.com) | |
| 13 | |
| 14 #include <string.h> | |
| 15 #include <math.h> | |
| 16 | |
| 17 #include "./cost.h" | |
| 18 #include "./vp8enci.h" | |
| 19 #include "../dsp/dsp.h" | |
| 20 #include "../webp/format_constants.h" // RIFF constants | |
| 21 | |
| 22 #define SEGMENT_VISU 0 | |
| 23 #define DEBUG_SEARCH 0 // useful to track search convergence | |
| 24 | |
| 25 //------------------------------------------------------------------------------ | |
| 26 // multi-pass convergence | |
| 27 | |
| 28 #define HEADER_SIZE_ESTIMATE (RIFF_HEADER_SIZE + CHUNK_HEADER_SIZE + \ | |
| 29 VP8_FRAME_HEADER_SIZE) | |
| 30 #define DQ_LIMIT 0.4 // convergence is considered reached if dq < DQ_LIMIT | |
| 31 // we allow 2k of extra head-room in PARTITION0 limit. | |
| 32 #define PARTITION0_SIZE_LIMIT ((VP8_MAX_PARTITION0_SIZE - 2048ULL) << 11) | |
| 33 | |
| 34 typedef struct { // struct for organizing convergence in either size or PSNR | |
| 35 int is_first; | |
| 36 float dq; | |
| 37 float q, last_q; | |
| 38 double value, last_value; // PSNR or size | |
| 39 double target; | |
| 40 int do_size_search; | |
| 41 } PassStats; | |
| 42 | |
| 43 static int InitPassStats(const VP8Encoder* const enc, PassStats* const s) { | |
| 44 const uint64_t target_size = (uint64_t)enc->config_->target_size; | |
| 45 const int do_size_search = (target_size != 0); | |
| 46 const float target_PSNR = enc->config_->target_PSNR; | |
| 47 | |
| 48 s->is_first = 1; | |
| 49 s->dq = 10.f; | |
| 50 s->q = s->last_q = enc->config_->quality; | |
| 51 s->target = do_size_search ? (double)target_size | |
| 52 : (target_PSNR > 0.) ? target_PSNR | |
| 53 : 40.; // default, just in case | |
| 54 s->value = s->last_value = 0.; | |
| 55 s->do_size_search = do_size_search; | |
| 56 return do_size_search; | |
| 57 } | |
| 58 | |
| 59 static float Clamp(float v, float min, float max) { | |
| 60 return (v < min) ? min : (v > max) ? max : v; | |
| 61 } | |
| 62 | |
| 63 static float ComputeNextQ(PassStats* const s) { | |
| 64 float dq; | |
| 65 if (s->is_first) { | |
| 66 dq = (s->value > s->target) ? -s->dq : s->dq; | |
| 67 s->is_first = 0; | |
| 68 } else if (s->value != s->last_value) { | |
| 69 const double slope = (s->target - s->value) / (s->last_value - s->value); | |
| 70 dq = (float)(slope * (s->last_q - s->q)); | |
| 71 } else { | |
| 72 dq = 0.; // we're done?! | |
| 73 } | |
| 74 // Limit variable to avoid large swings. | |
| 75 s->dq = Clamp(dq, -30.f, 30.f); | |
| 76 s->last_q = s->q; | |
| 77 s->last_value = s->value; | |
| 78 s->q = Clamp(s->q + s->dq, 0.f, 100.f); | |
| 79 return s->q; | |
| 80 } | |
| 81 | |
| 82 //------------------------------------------------------------------------------ | |
| 83 // Tables for level coding | |
| 84 | |
| 85 const uint8_t VP8Cat3[] = { 173, 148, 140 }; | |
| 86 const uint8_t VP8Cat4[] = { 176, 155, 140, 135 }; | |
| 87 const uint8_t VP8Cat5[] = { 180, 157, 141, 134, 130 }; | |
| 88 const uint8_t VP8Cat6[] = | |
| 89 { 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129 }; | |
| 90 | |
| 91 //------------------------------------------------------------------------------ | |
| 92 // Reset the statistics about: number of skips, token proba, level cost,... | |
| 93 | |
| 94 static void ResetStats(VP8Encoder* const enc) { | |
| 95 VP8EncProba* const proba = &enc->proba_; | |
| 96 VP8CalculateLevelCosts(proba); | |
| 97 proba->nb_skip_ = 0; | |
| 98 } | |
| 99 | |
| 100 //------------------------------------------------------------------------------ | |
| 101 // Skip decision probability | |
| 102 | |
| 103 #define SKIP_PROBA_THRESHOLD 250 // value below which using skip_proba is OK. | |
| 104 | |
| 105 static int CalcSkipProba(uint64_t nb, uint64_t total) { | |
| 106 return (int)(total ? (total - nb) * 255 / total : 255); | |
| 107 } | |
| 108 | |
| 109 // Returns the bit-cost for coding the skip probability. | |
| 110 static int FinalizeSkipProba(VP8Encoder* const enc) { | |
| 111 VP8EncProba* const proba = &enc->proba_; | |
| 112 const int nb_mbs = enc->mb_w_ * enc->mb_h_; | |
| 113 const int nb_events = proba->nb_skip_; | |
| 114 int size; | |
| 115 proba->skip_proba_ = CalcSkipProba(nb_events, nb_mbs); | |
| 116 proba->use_skip_proba_ = (proba->skip_proba_ < SKIP_PROBA_THRESHOLD); | |
| 117 size = 256; // 'use_skip_proba' bit | |
| 118 if (proba->use_skip_proba_) { | |
| 119 size += nb_events * VP8BitCost(1, proba->skip_proba_) | |
| 120 + (nb_mbs - nb_events) * VP8BitCost(0, proba->skip_proba_); | |
| 121 size += 8 * 256; // cost of signaling the skip_proba_ itself. | |
| 122 } | |
| 123 return size; | |
| 124 } | |
| 125 | |
| 126 // Collect statistics and deduce probabilities for next coding pass. | |
| 127 // Return the total bit-cost for coding the probability updates. | |
| 128 static int CalcTokenProba(int nb, int total) { | |
| 129 assert(nb <= total); | |
| 130 return nb ? (255 - nb * 255 / total) : 255; | |
| 131 } | |
| 132 | |
| 133 // Cost of coding 'nb' 1's and 'total-nb' 0's using 'proba' probability. | |
| 134 static int BranchCost(int nb, int total, int proba) { | |
| 135 return nb * VP8BitCost(1, proba) + (total - nb) * VP8BitCost(0, proba); | |
| 136 } | |
| 137 | |
| 138 static void ResetTokenStats(VP8Encoder* const enc) { | |
| 139 VP8EncProba* const proba = &enc->proba_; | |
| 140 memset(proba->stats_, 0, sizeof(proba->stats_)); | |
| 141 } | |
| 142 | |
| 143 static int FinalizeTokenProbas(VP8EncProba* const proba) { | |
| 144 int has_changed = 0; | |
| 145 int size = 0; | |
| 146 int t, b, c, p; | |
| 147 for (t = 0; t < NUM_TYPES; ++t) { | |
| 148 for (b = 0; b < NUM_BANDS; ++b) { | |
| 149 for (c = 0; c < NUM_CTX; ++c) { | |
| 150 for (p = 0; p < NUM_PROBAS; ++p) { | |
| 151 const proba_t stats = proba->stats_[t][b][c][p]; | |
| 152 const int nb = (stats >> 0) & 0xffff; | |
| 153 const int total = (stats >> 16) & 0xffff; | |
| 154 const int update_proba = VP8CoeffsUpdateProba[t][b][c][p]; | |
| 155 const int old_p = VP8CoeffsProba0[t][b][c][p]; | |
| 156 const int new_p = CalcTokenProba(nb, total); | |
| 157 const int old_cost = BranchCost(nb, total, old_p) | |
| 158 + VP8BitCost(0, update_proba); | |
| 159 const int new_cost = BranchCost(nb, total, new_p) | |
| 160 + VP8BitCost(1, update_proba) | |
| 161 + 8 * 256; | |
| 162 const int use_new_p = (old_cost > new_cost); | |
| 163 size += VP8BitCost(use_new_p, update_proba); | |
| 164 if (use_new_p) { // only use proba that seem meaningful enough. | |
| 165 proba->coeffs_[t][b][c][p] = new_p; | |
| 166 has_changed |= (new_p != old_p); | |
| 167 size += 8 * 256; | |
| 168 } else { | |
| 169 proba->coeffs_[t][b][c][p] = old_p; | |
| 170 } | |
| 171 } | |
| 172 } | |
| 173 } | |
| 174 } | |
| 175 proba->dirty_ = has_changed; | |
| 176 return size; | |
| 177 } | |
| 178 | |
| 179 //------------------------------------------------------------------------------ | |
| 180 // Finalize Segment probability based on the coding tree | |
| 181 | |
| 182 static int GetProba(int a, int b) { | |
| 183 const int total = a + b; | |
| 184 return (total == 0) ? 255 // that's the default probability. | |
| 185 : (255 * a + total / 2) / total; // rounded proba | |
| 186 } | |
| 187 | |
| 188 static void ResetSegments(VP8Encoder* const enc) { | |
| 189 int n; | |
| 190 for (n = 0; n < enc->mb_w_ * enc->mb_h_; ++n) { | |
| 191 enc->mb_info_[n].segment_ = 0; | |
| 192 } | |
| 193 } | |
| 194 | |
| 195 static void SetSegmentProbas(VP8Encoder* const enc) { | |
| 196 int p[NUM_MB_SEGMENTS] = { 0 }; | |
| 197 int n; | |
| 198 | |
| 199 for (n = 0; n < enc->mb_w_ * enc->mb_h_; ++n) { | |
| 200 const VP8MBInfo* const mb = &enc->mb_info_[n]; | |
| 201 p[mb->segment_]++; | |
| 202 } | |
| 203 if (enc->pic_->stats != NULL) { | |
| 204 for (n = 0; n < NUM_MB_SEGMENTS; ++n) { | |
| 205 enc->pic_->stats->segment_size[n] = p[n]; | |
| 206 } | |
| 207 } | |
| 208 if (enc->segment_hdr_.num_segments_ > 1) { | |
| 209 uint8_t* const probas = enc->proba_.segments_; | |
| 210 probas[0] = GetProba(p[0] + p[1], p[2] + p[3]); | |
| 211 probas[1] = GetProba(p[0], p[1]); | |
| 212 probas[2] = GetProba(p[2], p[3]); | |
| 213 | |
| 214 enc->segment_hdr_.update_map_ = | |
| 215 (probas[0] != 255) || (probas[1] != 255) || (probas[2] != 255); | |
| 216 if (!enc->segment_hdr_.update_map_) ResetSegments(enc); | |
| 217 enc->segment_hdr_.size_ = | |
| 218 p[0] * (VP8BitCost(0, probas[0]) + VP8BitCost(0, probas[1])) + | |
| 219 p[1] * (VP8BitCost(0, probas[0]) + VP8BitCost(1, probas[1])) + | |
| 220 p[2] * (VP8BitCost(1, probas[0]) + VP8BitCost(0, probas[2])) + | |
| 221 p[3] * (VP8BitCost(1, probas[0]) + VP8BitCost(1, probas[2])); | |
| 222 } else { | |
| 223 enc->segment_hdr_.update_map_ = 0; | |
| 224 enc->segment_hdr_.size_ = 0; | |
| 225 } | |
| 226 } | |
| 227 | |
| 228 //------------------------------------------------------------------------------ | |
| 229 // Coefficient coding | |
| 230 | |
| 231 static int PutCoeffs(VP8BitWriter* const bw, int ctx, const VP8Residual* res) { | |
| 232 int n = res->first; | |
| 233 // should be prob[VP8EncBands[n]], but it's equivalent for n=0 or 1 | |
| 234 const uint8_t* p = res->prob[n][ctx]; | |
| 235 if (!VP8PutBit(bw, res->last >= 0, p[0])) { | |
| 236 return 0; | |
| 237 } | |
| 238 | |
| 239 while (n < 16) { | |
| 240 const int c = res->coeffs[n++]; | |
| 241 const int sign = c < 0; | |
| 242 int v = sign ? -c : c; | |
| 243 if (!VP8PutBit(bw, v != 0, p[1])) { | |
| 244 p = res->prob[VP8EncBands[n]][0]; | |
| 245 continue; | |
| 246 } | |
| 247 if (!VP8PutBit(bw, v > 1, p[2])) { | |
| 248 p = res->prob[VP8EncBands[n]][1]; | |
| 249 } else { | |
| 250 if (!VP8PutBit(bw, v > 4, p[3])) { | |
| 251 if (VP8PutBit(bw, v != 2, p[4])) | |
| 252 VP8PutBit(bw, v == 4, p[5]); | |
| 253 } else if (!VP8PutBit(bw, v > 10, p[6])) { | |
| 254 if (!VP8PutBit(bw, v > 6, p[7])) { | |
| 255 VP8PutBit(bw, v == 6, 159); | |
| 256 } else { | |
| 257 VP8PutBit(bw, v >= 9, 165); | |
| 258 VP8PutBit(bw, !(v & 1), 145); | |
| 259 } | |
| 260 } else { | |
| 261 int mask; | |
| 262 const uint8_t* tab; | |
| 263 if (v < 3 + (8 << 1)) { // VP8Cat3 (3b) | |
| 264 VP8PutBit(bw, 0, p[8]); | |
| 265 VP8PutBit(bw, 0, p[9]); | |
| 266 v -= 3 + (8 << 0); | |
| 267 mask = 1 << 2; | |
| 268 tab = VP8Cat3; | |
| 269 } else if (v < 3 + (8 << 2)) { // VP8Cat4 (4b) | |
| 270 VP8PutBit(bw, 0, p[8]); | |
| 271 VP8PutBit(bw, 1, p[9]); | |
| 272 v -= 3 + (8 << 1); | |
| 273 mask = 1 << 3; | |
| 274 tab = VP8Cat4; | |
| 275 } else if (v < 3 + (8 << 3)) { // VP8Cat5 (5b) | |
| 276 VP8PutBit(bw, 1, p[8]); | |
| 277 VP8PutBit(bw, 0, p[10]); | |
| 278 v -= 3 + (8 << 2); | |
| 279 mask = 1 << 4; | |
| 280 tab = VP8Cat5; | |
| 281 } else { // VP8Cat6 (11b) | |
| 282 VP8PutBit(bw, 1, p[8]); | |
| 283 VP8PutBit(bw, 1, p[10]); | |
| 284 v -= 3 + (8 << 3); | |
| 285 mask = 1 << 10; | |
| 286 tab = VP8Cat6; | |
| 287 } | |
| 288 while (mask) { | |
| 289 VP8PutBit(bw, !!(v & mask), *tab++); | |
| 290 mask >>= 1; | |
| 291 } | |
| 292 } | |
| 293 p = res->prob[VP8EncBands[n]][2]; | |
| 294 } | |
| 295 VP8PutBitUniform(bw, sign); | |
| 296 if (n == 16 || !VP8PutBit(bw, n <= res->last, p[0])) { | |
| 297 return 1; // EOB | |
| 298 } | |
| 299 } | |
| 300 return 1; | |
| 301 } | |
| 302 | |
| 303 static void CodeResiduals(VP8BitWriter* const bw, VP8EncIterator* const it, | |
| 304 const VP8ModeScore* const rd) { | |
| 305 int x, y, ch; | |
| 306 VP8Residual res; | |
| 307 uint64_t pos1, pos2, pos3; | |
| 308 const int i16 = (it->mb_->type_ == 1); | |
| 309 const int segment = it->mb_->segment_; | |
| 310 VP8Encoder* const enc = it->enc_; | |
| 311 | |
| 312 VP8IteratorNzToBytes(it); | |
| 313 | |
| 314 pos1 = VP8BitWriterPos(bw); | |
| 315 if (i16) { | |
| 316 VP8InitResidual(0, 1, enc, &res); | |
| 317 VP8SetResidualCoeffs(rd->y_dc_levels, &res); | |
| 318 it->top_nz_[8] = it->left_nz_[8] = | |
| 319 PutCoeffs(bw, it->top_nz_[8] + it->left_nz_[8], &res); | |
| 320 VP8InitResidual(1, 0, enc, &res); | |
| 321 } else { | |
| 322 VP8InitResidual(0, 3, enc, &res); | |
| 323 } | |
| 324 | |
| 325 // luma-AC | |
| 326 for (y = 0; y < 4; ++y) { | |
| 327 for (x = 0; x < 4; ++x) { | |
| 328 const int ctx = it->top_nz_[x] + it->left_nz_[y]; | |
| 329 VP8SetResidualCoeffs(rd->y_ac_levels[x + y * 4], &res); | |
| 330 it->top_nz_[x] = it->left_nz_[y] = PutCoeffs(bw, ctx, &res); | |
| 331 } | |
| 332 } | |
| 333 pos2 = VP8BitWriterPos(bw); | |
| 334 | |
| 335 // U/V | |
| 336 VP8InitResidual(0, 2, enc, &res); | |
| 337 for (ch = 0; ch <= 2; ch += 2) { | |
| 338 for (y = 0; y < 2; ++y) { | |
| 339 for (x = 0; x < 2; ++x) { | |
| 340 const int ctx = it->top_nz_[4 + ch + x] + it->left_nz_[4 + ch + y]; | |
| 341 VP8SetResidualCoeffs(rd->uv_levels[ch * 2 + x + y * 2], &res); | |
| 342 it->top_nz_[4 + ch + x] = it->left_nz_[4 + ch + y] = | |
| 343 PutCoeffs(bw, ctx, &res); | |
| 344 } | |
| 345 } | |
| 346 } | |
| 347 pos3 = VP8BitWriterPos(bw); | |
| 348 it->luma_bits_ = pos2 - pos1; | |
| 349 it->uv_bits_ = pos3 - pos2; | |
| 350 it->bit_count_[segment][i16] += it->luma_bits_; | |
| 351 it->bit_count_[segment][2] += it->uv_bits_; | |
| 352 VP8IteratorBytesToNz(it); | |
| 353 } | |
| 354 | |
| 355 // Same as CodeResiduals, but doesn't actually write anything. | |
| 356 // Instead, it just records the event distribution. | |
| 357 static void RecordResiduals(VP8EncIterator* const it, | |
| 358 const VP8ModeScore* const rd) { | |
| 359 int x, y, ch; | |
| 360 VP8Residual res; | |
| 361 VP8Encoder* const enc = it->enc_; | |
| 362 | |
| 363 VP8IteratorNzToBytes(it); | |
| 364 | |
| 365 if (it->mb_->type_ == 1) { // i16x16 | |
| 366 VP8InitResidual(0, 1, enc, &res); | |
| 367 VP8SetResidualCoeffs(rd->y_dc_levels, &res); | |
| 368 it->top_nz_[8] = it->left_nz_[8] = | |
| 369 VP8RecordCoeffs(it->top_nz_[8] + it->left_nz_[8], &res); | |
| 370 VP8InitResidual(1, 0, enc, &res); | |
| 371 } else { | |
| 372 VP8InitResidual(0, 3, enc, &res); | |
| 373 } | |
| 374 | |
| 375 // luma-AC | |
| 376 for (y = 0; y < 4; ++y) { | |
| 377 for (x = 0; x < 4; ++x) { | |
| 378 const int ctx = it->top_nz_[x] + it->left_nz_[y]; | |
| 379 VP8SetResidualCoeffs(rd->y_ac_levels[x + y * 4], &res); | |
| 380 it->top_nz_[x] = it->left_nz_[y] = VP8RecordCoeffs(ctx, &res); | |
| 381 } | |
| 382 } | |
| 383 | |
| 384 // U/V | |
| 385 VP8InitResidual(0, 2, enc, &res); | |
| 386 for (ch = 0; ch <= 2; ch += 2) { | |
| 387 for (y = 0; y < 2; ++y) { | |
| 388 for (x = 0; x < 2; ++x) { | |
| 389 const int ctx = it->top_nz_[4 + ch + x] + it->left_nz_[4 + ch + y]; | |
| 390 VP8SetResidualCoeffs(rd->uv_levels[ch * 2 + x + y * 2], &res); | |
| 391 it->top_nz_[4 + ch + x] = it->left_nz_[4 + ch + y] = | |
| 392 VP8RecordCoeffs(ctx, &res); | |
| 393 } | |
| 394 } | |
| 395 } | |
| 396 | |
| 397 VP8IteratorBytesToNz(it); | |
| 398 } | |
| 399 | |
| 400 //------------------------------------------------------------------------------ | |
| 401 // Token buffer | |
| 402 | |
| 403 #if !defined(DISABLE_TOKEN_BUFFER) | |
| 404 | |
| 405 static int RecordTokens(VP8EncIterator* const it, const VP8ModeScore* const rd, | |
| 406 VP8TBuffer* const tokens) { | |
| 407 int x, y, ch; | |
| 408 VP8Residual res; | |
| 409 VP8Encoder* const enc = it->enc_; | |
| 410 | |
| 411 VP8IteratorNzToBytes(it); | |
| 412 if (it->mb_->type_ == 1) { // i16x16 | |
| 413 const int ctx = it->top_nz_[8] + it->left_nz_[8]; | |
| 414 VP8InitResidual(0, 1, enc, &res); | |
| 415 VP8SetResidualCoeffs(rd->y_dc_levels, &res); | |
| 416 it->top_nz_[8] = it->left_nz_[8] = | |
| 417 VP8RecordCoeffTokens(ctx, &res, tokens); | |
| 418 VP8InitResidual(1, 0, enc, &res); | |
| 419 } else { | |
| 420 VP8InitResidual(0, 3, enc, &res); | |
| 421 } | |
| 422 | |
| 423 // luma-AC | |
| 424 for (y = 0; y < 4; ++y) { | |
| 425 for (x = 0; x < 4; ++x) { | |
| 426 const int ctx = it->top_nz_[x] + it->left_nz_[y]; | |
| 427 VP8SetResidualCoeffs(rd->y_ac_levels[x + y * 4], &res); | |
| 428 it->top_nz_[x] = it->left_nz_[y] = | |
| 429 VP8RecordCoeffTokens(ctx, &res, tokens); | |
| 430 } | |
| 431 } | |
| 432 | |
| 433 // U/V | |
| 434 VP8InitResidual(0, 2, enc, &res); | |
| 435 for (ch = 0; ch <= 2; ch += 2) { | |
| 436 for (y = 0; y < 2; ++y) { | |
| 437 for (x = 0; x < 2; ++x) { | |
| 438 const int ctx = it->top_nz_[4 + ch + x] + it->left_nz_[4 + ch + y]; | |
| 439 VP8SetResidualCoeffs(rd->uv_levels[ch * 2 + x + y * 2], &res); | |
| 440 it->top_nz_[4 + ch + x] = it->left_nz_[4 + ch + y] = | |
| 441 VP8RecordCoeffTokens(ctx, &res, tokens); | |
| 442 } | |
| 443 } | |
| 444 } | |
| 445 VP8IteratorBytesToNz(it); | |
| 446 return !tokens->error_; | |
| 447 } | |
| 448 | |
| 449 #endif // !DISABLE_TOKEN_BUFFER | |
| 450 | |
| 451 //------------------------------------------------------------------------------ | |
| 452 // ExtraInfo map / Debug function | |
| 453 | |
| 454 #if SEGMENT_VISU | |
| 455 static void SetBlock(uint8_t* p, int value, int size) { | |
| 456 int y; | |
| 457 for (y = 0; y < size; ++y) { | |
| 458 memset(p, value, size); | |
| 459 p += BPS; | |
| 460 } | |
| 461 } | |
| 462 #endif | |
| 463 | |
| 464 static void ResetSSE(VP8Encoder* const enc) { | |
| 465 enc->sse_[0] = 0; | |
| 466 enc->sse_[1] = 0; | |
| 467 enc->sse_[2] = 0; | |
| 468 // Note: enc->sse_[3] is managed by alpha.c | |
| 469 enc->sse_count_ = 0; | |
| 470 } | |
| 471 | |
| 472 static void StoreSSE(const VP8EncIterator* const it) { | |
| 473 VP8Encoder* const enc = it->enc_; | |
| 474 const uint8_t* const in = it->yuv_in_; | |
| 475 const uint8_t* const out = it->yuv_out_; | |
| 476 // Note: not totally accurate at boundary. And doesn't include in-loop filter. | |
| 477 enc->sse_[0] += VP8SSE16x16(in + Y_OFF_ENC, out + Y_OFF_ENC); | |
| 478 enc->sse_[1] += VP8SSE8x8(in + U_OFF_ENC, out + U_OFF_ENC); | |
| 479 enc->sse_[2] += VP8SSE8x8(in + V_OFF_ENC, out + V_OFF_ENC); | |
| 480 enc->sse_count_ += 16 * 16; | |
| 481 } | |
| 482 | |
| 483 static void StoreSideInfo(const VP8EncIterator* const it) { | |
| 484 VP8Encoder* const enc = it->enc_; | |
| 485 const VP8MBInfo* const mb = it->mb_; | |
| 486 WebPPicture* const pic = enc->pic_; | |
| 487 | |
| 488 if (pic->stats != NULL) { | |
| 489 StoreSSE(it); | |
| 490 enc->block_count_[0] += (mb->type_ == 0); | |
| 491 enc->block_count_[1] += (mb->type_ == 1); | |
| 492 enc->block_count_[2] += (mb->skip_ != 0); | |
| 493 } | |
| 494 | |
| 495 if (pic->extra_info != NULL) { | |
| 496 uint8_t* const info = &pic->extra_info[it->x_ + it->y_ * enc->mb_w_]; | |
| 497 switch (pic->extra_info_type) { | |
| 498 case 1: *info = mb->type_; break; | |
| 499 case 2: *info = mb->segment_; break; | |
| 500 case 3: *info = enc->dqm_[mb->segment_].quant_; break; | |
| 501 case 4: *info = (mb->type_ == 1) ? it->preds_[0] : 0xff; break; | |
| 502 case 5: *info = mb->uv_mode_; break; | |
| 503 case 6: { | |
| 504 const int b = (int)((it->luma_bits_ + it->uv_bits_ + 7) >> 3); | |
| 505 *info = (b > 255) ? 255 : b; break; | |
| 506 } | |
| 507 case 7: *info = mb->alpha_; break; | |
| 508 default: *info = 0; break; | |
| 509 } | |
| 510 } | |
| 511 #if SEGMENT_VISU // visualize segments and prediction modes | |
| 512 SetBlock(it->yuv_out_ + Y_OFF_ENC, mb->segment_ * 64, 16); | |
| 513 SetBlock(it->yuv_out_ + U_OFF_ENC, it->preds_[0] * 64, 8); | |
| 514 SetBlock(it->yuv_out_ + V_OFF_ENC, mb->uv_mode_ * 64, 8); | |
| 515 #endif | |
| 516 } | |
| 517 | |
| 518 static double GetPSNR(uint64_t mse, uint64_t size) { | |
| 519 return (mse > 0 && size > 0) ? 10. * log10(255. * 255. * size / mse) : 99; | |
| 520 } | |
| 521 | |
| 522 //------------------------------------------------------------------------------ | |
| 523 // StatLoop(): only collect statistics (number of skips, token usage, ...). | |
| 524 // This is used for deciding optimal probabilities. It also modifies the | |
| 525 // quantizer value if some target (size, PSNR) was specified. | |
| 526 | |
| 527 static void SetLoopParams(VP8Encoder* const enc, float q) { | |
| 528 // Make sure the quality parameter is inside valid bounds | |
| 529 q = Clamp(q, 0.f, 100.f); | |
| 530 | |
| 531 VP8SetSegmentParams(enc, q); // setup segment quantizations and filters | |
| 532 SetSegmentProbas(enc); // compute segment probabilities | |
| 533 | |
| 534 ResetStats(enc); | |
| 535 ResetSSE(enc); | |
| 536 } | |
| 537 | |
| 538 static uint64_t OneStatPass(VP8Encoder* const enc, VP8RDLevel rd_opt, | |
| 539 int nb_mbs, int percent_delta, | |
| 540 PassStats* const s) { | |
| 541 VP8EncIterator it; | |
| 542 uint64_t size = 0; | |
| 543 uint64_t size_p0 = 0; | |
| 544 uint64_t distortion = 0; | |
| 545 const uint64_t pixel_count = nb_mbs * 384; | |
| 546 | |
| 547 VP8IteratorInit(enc, &it); | |
| 548 SetLoopParams(enc, s->q); | |
| 549 do { | |
| 550 VP8ModeScore info; | |
| 551 VP8IteratorImport(&it, NULL); | |
| 552 if (VP8Decimate(&it, &info, rd_opt)) { | |
| 553 // Just record the number of skips and act like skip_proba is not used. | |
| 554 enc->proba_.nb_skip_++; | |
| 555 } | |
| 556 RecordResiduals(&it, &info); | |
| 557 size += info.R + info.H; | |
| 558 size_p0 += info.H; | |
| 559 distortion += info.D; | |
| 560 if (percent_delta && !VP8IteratorProgress(&it, percent_delta)) | |
| 561 return 0; | |
| 562 VP8IteratorSaveBoundary(&it); | |
| 563 } while (VP8IteratorNext(&it) && --nb_mbs > 0); | |
| 564 | |
| 565 size_p0 += enc->segment_hdr_.size_; | |
| 566 if (s->do_size_search) { | |
| 567 size += FinalizeSkipProba(enc); | |
| 568 size += FinalizeTokenProbas(&enc->proba_); | |
| 569 size = ((size + size_p0 + 1024) >> 11) + HEADER_SIZE_ESTIMATE; | |
| 570 s->value = (double)size; | |
| 571 } else { | |
| 572 s->value = GetPSNR(distortion, pixel_count); | |
| 573 } | |
| 574 return size_p0; | |
| 575 } | |
| 576 | |
| 577 static int StatLoop(VP8Encoder* const enc) { | |
| 578 const int method = enc->method_; | |
| 579 const int do_search = enc->do_search_; | |
| 580 const int fast_probe = ((method == 0 || method == 3) && !do_search); | |
| 581 int num_pass_left = enc->config_->pass; | |
| 582 const int task_percent = 20; | |
| 583 const int percent_per_pass = | |
| 584 (task_percent + num_pass_left / 2) / num_pass_left; | |
| 585 const int final_percent = enc->percent_ + task_percent; | |
| 586 const VP8RDLevel rd_opt = | |
| 587 (method >= 3 || do_search) ? RD_OPT_BASIC : RD_OPT_NONE; | |
| 588 int nb_mbs = enc->mb_w_ * enc->mb_h_; | |
| 589 PassStats stats; | |
| 590 | |
| 591 InitPassStats(enc, &stats); | |
| 592 ResetTokenStats(enc); | |
| 593 | |
| 594 // Fast mode: quick analysis pass over few mbs. Better than nothing. | |
| 595 if (fast_probe) { | |
| 596 if (method == 3) { // we need more stats for method 3 to be reliable. | |
| 597 nb_mbs = (nb_mbs > 200) ? nb_mbs >> 1 : 100; | |
| 598 } else { | |
| 599 nb_mbs = (nb_mbs > 200) ? nb_mbs >> 2 : 50; | |
| 600 } | |
| 601 } | |
| 602 | |
| 603 while (num_pass_left-- > 0) { | |
| 604 const int is_last_pass = (fabs(stats.dq) <= DQ_LIMIT) || | |
| 605 (num_pass_left == 0) || | |
| 606 (enc->max_i4_header_bits_ == 0); | |
| 607 const uint64_t size_p0 = | |
| 608 OneStatPass(enc, rd_opt, nb_mbs, percent_per_pass, &stats); | |
| 609 if (size_p0 == 0) return 0; | |
| 610 #if (DEBUG_SEARCH > 0) | |
| 611 printf("#%d value:%.1lf -> %.1lf q:%.2f -> %.2f\n", | |
| 612 num_pass_left, stats.last_value, stats.value, stats.last_q, stats.q); | |
| 613 #endif | |
| 614 if (enc->max_i4_header_bits_ > 0 && size_p0 > PARTITION0_SIZE_LIMIT) { | |
| 615 ++num_pass_left; | |
| 616 enc->max_i4_header_bits_ >>= 1; // strengthen header bit limitation... | |
| 617 continue; // ...and start over | |
| 618 } | |
| 619 if (is_last_pass) { | |
| 620 break; | |
| 621 } | |
| 622 // If no target size: just do several pass without changing 'q' | |
| 623 if (do_search) { | |
| 624 ComputeNextQ(&stats); | |
| 625 if (fabs(stats.dq) <= DQ_LIMIT) break; | |
| 626 } | |
| 627 } | |
| 628 if (!do_search || !stats.do_size_search) { | |
| 629 // Need to finalize probas now, since it wasn't done during the search. | |
| 630 FinalizeSkipProba(enc); | |
| 631 FinalizeTokenProbas(&enc->proba_); | |
| 632 } | |
| 633 VP8CalculateLevelCosts(&enc->proba_); // finalize costs | |
| 634 return WebPReportProgress(enc->pic_, final_percent, &enc->percent_); | |
| 635 } | |
| 636 | |
| 637 //------------------------------------------------------------------------------ | |
| 638 // Main loops | |
| 639 // | |
| 640 | |
| 641 static const int kAverageBytesPerMB[8] = { 50, 24, 16, 9, 7, 5, 3, 2 }; | |
| 642 | |
| 643 static int PreLoopInitialize(VP8Encoder* const enc) { | |
| 644 int p; | |
| 645 int ok = 1; | |
| 646 const int average_bytes_per_MB = kAverageBytesPerMB[enc->base_quant_ >> 4]; | |
| 647 const int bytes_per_parts = | |
| 648 enc->mb_w_ * enc->mb_h_ * average_bytes_per_MB / enc->num_parts_; | |
| 649 // Initialize the bit-writers | |
| 650 for (p = 0; ok && p < enc->num_parts_; ++p) { | |
| 651 ok = VP8BitWriterInit(enc->parts_ + p, bytes_per_parts); | |
| 652 } | |
| 653 if (!ok) { | |
| 654 VP8EncFreeBitWriters(enc); // malloc error occurred | |
| 655 WebPEncodingSetError(enc->pic_, VP8_ENC_ERROR_OUT_OF_MEMORY); | |
| 656 } | |
| 657 return ok; | |
| 658 } | |
| 659 | |
| 660 static int PostLoopFinalize(VP8EncIterator* const it, int ok) { | |
| 661 VP8Encoder* const enc = it->enc_; | |
| 662 if (ok) { // Finalize the partitions, check for extra errors. | |
| 663 int p; | |
| 664 for (p = 0; p < enc->num_parts_; ++p) { | |
| 665 VP8BitWriterFinish(enc->parts_ + p); | |
| 666 ok &= !enc->parts_[p].error_; | |
| 667 } | |
| 668 } | |
| 669 | |
| 670 if (ok) { // All good. Finish up. | |
| 671 if (enc->pic_->stats != NULL) { // finalize byte counters... | |
| 672 int i, s; | |
| 673 for (i = 0; i <= 2; ++i) { | |
| 674 for (s = 0; s < NUM_MB_SEGMENTS; ++s) { | |
| 675 enc->residual_bytes_[i][s] = (int)((it->bit_count_[s][i] + 7) >> 3); | |
| 676 } | |
| 677 } | |
| 678 } | |
| 679 VP8AdjustFilterStrength(it); // ...and store filter stats. | |
| 680 } else { | |
| 681 // Something bad happened -> need to do some memory cleanup. | |
| 682 VP8EncFreeBitWriters(enc); | |
| 683 } | |
| 684 return ok; | |
| 685 } | |
| 686 | |
| 687 //------------------------------------------------------------------------------ | |
| 688 // VP8EncLoop(): does the final bitstream coding. | |
| 689 | |
| 690 static void ResetAfterSkip(VP8EncIterator* const it) { | |
| 691 if (it->mb_->type_ == 1) { | |
| 692 *it->nz_ = 0; // reset all predictors | |
| 693 it->left_nz_[8] = 0; | |
| 694 } else { | |
| 695 *it->nz_ &= (1 << 24); // preserve the dc_nz bit | |
| 696 } | |
| 697 } | |
| 698 | |
| 699 int VP8EncLoop(VP8Encoder* const enc) { | |
| 700 VP8EncIterator it; | |
| 701 int ok = PreLoopInitialize(enc); | |
| 702 if (!ok) return 0; | |
| 703 | |
| 704 StatLoop(enc); // stats-collection loop | |
| 705 | |
| 706 VP8IteratorInit(enc, &it); | |
| 707 VP8InitFilter(&it); | |
| 708 do { | |
| 709 VP8ModeScore info; | |
| 710 const int dont_use_skip = !enc->proba_.use_skip_proba_; | |
| 711 const VP8RDLevel rd_opt = enc->rd_opt_level_; | |
| 712 | |
| 713 VP8IteratorImport(&it, NULL); | |
| 714 // Warning! order is important: first call VP8Decimate() and | |
| 715 // *then* decide how to code the skip decision if there's one. | |
| 716 if (!VP8Decimate(&it, &info, rd_opt) || dont_use_skip) { | |
| 717 CodeResiduals(it.bw_, &it, &info); | |
| 718 } else { // reset predictors after a skip | |
| 719 ResetAfterSkip(&it); | |
| 720 } | |
| 721 StoreSideInfo(&it); | |
| 722 VP8StoreFilterStats(&it); | |
| 723 VP8IteratorExport(&it); | |
| 724 ok = VP8IteratorProgress(&it, 20); | |
| 725 VP8IteratorSaveBoundary(&it); | |
| 726 } while (ok && VP8IteratorNext(&it)); | |
| 727 | |
| 728 return PostLoopFinalize(&it, ok); | |
| 729 } | |
| 730 | |
| 731 //------------------------------------------------------------------------------ | |
| 732 // Single pass using Token Buffer. | |
| 733 | |
| 734 #if !defined(DISABLE_TOKEN_BUFFER) | |
| 735 | |
| 736 #define MIN_COUNT 96 // minimum number of macroblocks before updating stats | |
| 737 | |
| 738 int VP8EncTokenLoop(VP8Encoder* const enc) { | |
| 739 // Roughly refresh the proba eight times per pass | |
| 740 int max_count = (enc->mb_w_ * enc->mb_h_) >> 3; | |
| 741 int num_pass_left = enc->config_->pass; | |
| 742 const int do_search = enc->do_search_; | |
| 743 VP8EncIterator it; | |
| 744 VP8EncProba* const proba = &enc->proba_; | |
| 745 const VP8RDLevel rd_opt = enc->rd_opt_level_; | |
| 746 const uint64_t pixel_count = enc->mb_w_ * enc->mb_h_ * 384; | |
| 747 PassStats stats; | |
| 748 int ok; | |
| 749 | |
| 750 InitPassStats(enc, &stats); | |
| 751 ok = PreLoopInitialize(enc); | |
| 752 if (!ok) return 0; | |
| 753 | |
| 754 if (max_count < MIN_COUNT) max_count = MIN_COUNT; | |
| 755 | |
| 756 assert(enc->num_parts_ == 1); | |
| 757 assert(enc->use_tokens_); | |
| 758 assert(proba->use_skip_proba_ == 0); | |
| 759 assert(rd_opt >= RD_OPT_BASIC); // otherwise, token-buffer won't be useful | |
| 760 assert(num_pass_left > 0); | |
| 761 | |
| 762 while (ok && num_pass_left-- > 0) { | |
| 763 const int is_last_pass = (fabs(stats.dq) <= DQ_LIMIT) || | |
| 764 (num_pass_left == 0) || | |
| 765 (enc->max_i4_header_bits_ == 0); | |
| 766 uint64_t size_p0 = 0; | |
| 767 uint64_t distortion = 0; | |
| 768 int cnt = max_count; | |
| 769 VP8IteratorInit(enc, &it); | |
| 770 SetLoopParams(enc, stats.q); | |
| 771 if (is_last_pass) { | |
| 772 ResetTokenStats(enc); | |
| 773 VP8InitFilter(&it); // don't collect stats until last pass (too costly) | |
| 774 } | |
| 775 VP8TBufferClear(&enc->tokens_); | |
| 776 do { | |
| 777 VP8ModeScore info; | |
| 778 VP8IteratorImport(&it, NULL); | |
| 779 if (--cnt < 0) { | |
| 780 FinalizeTokenProbas(proba); | |
| 781 VP8CalculateLevelCosts(proba); // refresh cost tables for rd-opt | |
| 782 cnt = max_count; | |
| 783 } | |
| 784 VP8Decimate(&it, &info, rd_opt); | |
| 785 ok = RecordTokens(&it, &info, &enc->tokens_); | |
| 786 if (!ok) { | |
| 787 WebPEncodingSetError(enc->pic_, VP8_ENC_ERROR_OUT_OF_MEMORY); | |
| 788 break; | |
| 789 } | |
| 790 size_p0 += info.H; | |
| 791 distortion += info.D; | |
| 792 if (is_last_pass) { | |
| 793 StoreSideInfo(&it); | |
| 794 VP8StoreFilterStats(&it); | |
| 795 VP8IteratorExport(&it); | |
| 796 ok = VP8IteratorProgress(&it, 20); | |
| 797 } | |
| 798 VP8IteratorSaveBoundary(&it); | |
| 799 } while (ok && VP8IteratorNext(&it)); | |
| 800 if (!ok) break; | |
| 801 | |
| 802 size_p0 += enc->segment_hdr_.size_; | |
| 803 if (stats.do_size_search) { | |
| 804 uint64_t size = FinalizeTokenProbas(&enc->proba_); | |
| 805 size += VP8EstimateTokenSize(&enc->tokens_, | |
| 806 (const uint8_t*)proba->coeffs_); | |
| 807 size = (size + size_p0 + 1024) >> 11; // -> size in bytes | |
| 808 size += HEADER_SIZE_ESTIMATE; | |
| 809 stats.value = (double)size; | |
| 810 } else { // compute and store PSNR | |
| 811 stats.value = GetPSNR(distortion, pixel_count); | |
| 812 } | |
| 813 | |
| 814 #if (DEBUG_SEARCH > 0) | |
| 815 printf("#%2d metric:%.1lf -> %.1lf last_q=%.2lf q=%.2lf dq=%.2lf\n", | |
| 816 num_pass_left, stats.last_value, stats.value, | |
| 817 stats.last_q, stats.q, stats.dq); | |
| 818 #endif | |
| 819 if (enc->max_i4_header_bits_ > 0 && size_p0 > PARTITION0_SIZE_LIMIT) { | |
| 820 ++num_pass_left; | |
| 821 enc->max_i4_header_bits_ >>= 1; // strengthen header bit limitation... | |
| 822 continue; // ...and start over | |
| 823 } | |
| 824 if (is_last_pass) { | |
| 825 break; // done | |
| 826 } | |
| 827 if (do_search) { | |
| 828 ComputeNextQ(&stats); // Adjust q | |
| 829 } | |
| 830 } | |
| 831 if (ok) { | |
| 832 if (!stats.do_size_search) { | |
| 833 FinalizeTokenProbas(&enc->proba_); | |
| 834 } | |
| 835 ok = VP8EmitTokens(&enc->tokens_, enc->parts_ + 0, | |
| 836 (const uint8_t*)proba->coeffs_, 1); | |
| 837 } | |
| 838 ok = ok && WebPReportProgress(enc->pic_, enc->percent_ + 20, &enc->percent_); | |
| 839 return PostLoopFinalize(&it, ok); | |
| 840 } | |
| 841 | |
| 842 #else | |
| 843 | |
| 844 int VP8EncTokenLoop(VP8Encoder* const enc) { | |
| 845 (void)enc; | |
| 846 return 0; // we shouldn't be here. | |
| 847 } | |
| 848 | |
| 849 #endif // DISABLE_TOKEN_BUFFER | |
| 850 | |
| 851 //------------------------------------------------------------------------------ | |
| 852 | |
| OLD | NEW |