| OLD | NEW |
| 1 // Copyright 2011 Google Inc. All Rights Reserved. | 1 // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 // | 2 // |
| 3 // Use of this source code is governed by a BSD-style license | 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 | 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 | 5 // tree. An additional intellectual property rights grant can be found |
| 6 // in the file PATENTS. All contributing project authors may | 6 // in the file PATENTS. All contributing project authors may |
| 7 // be found in the AUTHORS file in the root of the source tree. | 7 // be found in the AUTHORS file in the root of the source tree. |
| 8 // ----------------------------------------------------------------------------- | 8 // ----------------------------------------------------------------------------- |
| 9 // | 9 // |
| 10 // Quantization | 10 // Quantization |
| 11 // | 11 // |
| 12 // Author: Skal (pascal.massimino@gmail.com) | 12 // Author: Skal (pascal.massimino@gmail.com) |
| 13 | 13 |
| 14 #include <assert.h> | 14 #include <assert.h> |
| 15 #include <math.h> | 15 #include <math.h> |
| 16 #include <stdlib.h> // for abs() | 16 #include <stdlib.h> // for abs() |
| 17 | 17 |
| 18 #include "./vp8enci.h" | 18 #include "./vp8i_enc.h" |
| 19 #include "./cost.h" | 19 #include "./cost_enc.h" |
| 20 | 20 |
| 21 #define DO_TRELLIS_I4 1 | 21 #define DO_TRELLIS_I4 1 |
| 22 #define DO_TRELLIS_I16 1 // not a huge gain, but ok at low bitrate. | 22 #define DO_TRELLIS_I16 1 // not a huge gain, but ok at low bitrate. |
| 23 #define DO_TRELLIS_UV 0 // disable trellis for UV. Risky. Not worth. | 23 #define DO_TRELLIS_UV 0 // disable trellis for UV. Risky. Not worth. |
| 24 #define USE_TDISTO 1 | 24 #define USE_TDISTO 1 |
| 25 | 25 |
| 26 #define MID_ALPHA 64 // neutral value for susceptibility | 26 #define MID_ALPHA 64 // neutral value for susceptibility |
| 27 #define MIN_ALPHA 30 // lowest usable value for susceptibility | 27 #define MIN_ALPHA 30 // lowest usable value for susceptibility |
| 28 #define MAX_ALPHA 100 // higher meaningful value for susceptibility | 28 #define MAX_ALPHA 100 // higher meaningful value for susceptibility |
| 29 | 29 |
| (...skipping 606 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 636 for (n = first; n <= last; ++n) { | 636 for (n = first; n <= last; ++n) { |
| 637 const int j = kZigzag[n]; | 637 const int j = kZigzag[n]; |
| 638 const uint32_t Q = mtx->q_[j]; | 638 const uint32_t Q = mtx->q_[j]; |
| 639 const uint32_t iQ = mtx->iq_[j]; | 639 const uint32_t iQ = mtx->iq_[j]; |
| 640 const uint32_t B = BIAS(0x00); // neutral bias | 640 const uint32_t B = BIAS(0x00); // neutral bias |
| 641 // note: it's important to take sign of the _original_ coeff, | 641 // note: it's important to take sign of the _original_ coeff, |
| 642 // so we don't have to consider level < 0 afterward. | 642 // so we don't have to consider level < 0 afterward. |
| 643 const int sign = (in[j] < 0); | 643 const int sign = (in[j] < 0); |
| 644 const uint32_t coeff0 = (sign ? -in[j] : in[j]) + mtx->sharpen_[j]; | 644 const uint32_t coeff0 = (sign ? -in[j] : in[j]) + mtx->sharpen_[j]; |
| 645 int level0 = QUANTDIV(coeff0, iQ, B); | 645 int level0 = QUANTDIV(coeff0, iQ, B); |
| 646 int thresh_level = QUANTDIV(coeff0, iQ, BIAS(0x80)); |
| 647 if (thresh_level > MAX_LEVEL) thresh_level = MAX_LEVEL; |
| 646 if (level0 > MAX_LEVEL) level0 = MAX_LEVEL; | 648 if (level0 > MAX_LEVEL) level0 = MAX_LEVEL; |
| 647 | 649 |
| 648 { // Swap current and previous score states | 650 { // Swap current and previous score states |
| 649 ScoreState* const tmp = ss_cur; | 651 ScoreState* const tmp = ss_cur; |
| 650 ss_cur = ss_prev; | 652 ss_cur = ss_prev; |
| 651 ss_prev = tmp; | 653 ss_prev = tmp; |
| 652 } | 654 } |
| 653 | 655 |
| 654 // test all alternate level values around level0. | 656 // test all alternate level values around level0. |
| 655 for (m = -MIN_DELTA; m <= MAX_DELTA; ++m) { | 657 for (m = -MIN_DELTA; m <= MAX_DELTA; ++m) { |
| 656 Node* const cur = &NODE(n, m); | 658 Node* const cur = &NODE(n, m); |
| 657 int level = level0 + m; | 659 int level = level0 + m; |
| 658 const int ctx = (level > 2) ? 2 : level; | 660 const int ctx = (level > 2) ? 2 : level; |
| 659 const int band = VP8EncBands[n + 1]; | 661 const int band = VP8EncBands[n + 1]; |
| 660 score_t base_score, last_pos_score; | 662 score_t base_score; |
| 661 score_t best_cur_score = MAX_COST; | 663 score_t best_cur_score = MAX_COST; |
| 662 int best_prev = 0; // default, in case | 664 int best_prev = 0; // default, in case |
| 663 | 665 |
| 664 ss_cur[m].score = MAX_COST; | 666 ss_cur[m].score = MAX_COST; |
| 665 ss_cur[m].costs = costs[n + 1][ctx]; | 667 ss_cur[m].costs = costs[n + 1][ctx]; |
| 666 if (level > MAX_LEVEL || level < 0) { // node is dead? | 668 if (level < 0 || level > thresh_level) { |
| 669 // Node is dead. |
| 667 continue; | 670 continue; |
| 668 } | 671 } |
| 669 | 672 |
| 670 // Compute extra rate cost if last coeff's position is < 15 | |
| 671 { | |
| 672 const score_t last_pos_cost = | |
| 673 (n < 15) ? VP8BitCost(0, probas[band][ctx][0]) : 0; | |
| 674 last_pos_score = RDScoreTrellis(lambda, last_pos_cost, 0); | |
| 675 } | |
| 676 | |
| 677 { | 673 { |
| 678 // Compute delta_error = how much coding this level will | 674 // Compute delta_error = how much coding this level will |
| 679 // subtract to max_error as distortion. | 675 // subtract to max_error as distortion. |
| 680 // Here, distortion = sum of (|coeff_i| - level_i * Q_i)^2 | 676 // Here, distortion = sum of (|coeff_i| - level_i * Q_i)^2 |
| 681 const int new_error = coeff0 - level * Q; | 677 const int new_error = coeff0 - level * Q; |
| 682 const int delta_error = | 678 const int delta_error = |
| 683 kWeightTrellis[j] * (new_error * new_error - coeff0 * coeff0); | 679 kWeightTrellis[j] * (new_error * new_error - coeff0 * coeff0); |
| 684 base_score = RDScoreTrellis(lambda, 0, delta_error); | 680 base_score = RDScoreTrellis(lambda, 0, delta_error); |
| 685 } | 681 } |
| 686 | 682 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 698 } | 694 } |
| 699 } | 695 } |
| 700 // Store best finding in current node. | 696 // Store best finding in current node. |
| 701 cur->sign = sign; | 697 cur->sign = sign; |
| 702 cur->level = level; | 698 cur->level = level; |
| 703 cur->prev = best_prev; | 699 cur->prev = best_prev; |
| 704 ss_cur[m].score = best_cur_score; | 700 ss_cur[m].score = best_cur_score; |
| 705 | 701 |
| 706 // Now, record best terminal node (and thus best entry in the graph). | 702 // Now, record best terminal node (and thus best entry in the graph). |
| 707 if (level != 0) { | 703 if (level != 0) { |
| 704 const score_t last_pos_cost = |
| 705 (n < 15) ? VP8BitCost(0, probas[band][ctx][0]) : 0; |
| 706 const score_t last_pos_score = RDScoreTrellis(lambda, last_pos_cost, 0); |
| 708 const score_t score = best_cur_score + last_pos_score; | 707 const score_t score = best_cur_score + last_pos_score; |
| 709 if (score < best_score) { | 708 if (score < best_score) { |
| 710 best_score = score; | 709 best_score = score; |
| 711 best_path[0] = n; // best eob position | 710 best_path[0] = n; // best eob position |
| 712 best_path[1] = m; // best node index | 711 best_path[1] = m; // best node index |
| 713 best_path[2] = best_prev; // best predecessor | 712 best_path[2] = best_prev; // best predecessor |
| 714 } | 713 } |
| 715 } | 714 } |
| 716 } | 715 } |
| 717 } | 716 } |
| (...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1275 // At this point we have heuristically decided intra16 / intra4. | 1274 // At this point we have heuristically decided intra16 / intra4. |
| 1276 // For method >= 2, pick the best intra4/intra16 based on SSE (~tad slower). | 1275 // For method >= 2, pick the best intra4/intra16 based on SSE (~tad slower). |
| 1277 // For method <= 1, we don't re-examine the decision but just go ahead with | 1276 // For method <= 1, we don't re-examine the decision but just go ahead with |
| 1278 // quantization/reconstruction. | 1277 // quantization/reconstruction. |
| 1279 RefineUsingDistortion(it, (method >= 2), (method >= 1), rd); | 1278 RefineUsingDistortion(it, (method >= 2), (method >= 1), rd); |
| 1280 } | 1279 } |
| 1281 is_skipped = (rd->nz == 0); | 1280 is_skipped = (rd->nz == 0); |
| 1282 VP8SetSkip(it, is_skipped); | 1281 VP8SetSkip(it, is_skipped); |
| 1283 return is_skipped; | 1282 return is_skipped; |
| 1284 } | 1283 } |
| OLD | NEW |