Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2001-2016, Alliance for Open Media. All rights reserved |
| 3 | * |
| 4 | * This source code is subject to the terms of the BSD 2 Clause License and |
| 5 | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
| 6 | * was not distributed with this source code in the LICENSE file, you can |
| 7 | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
| 8 | * Media Patent License 1.0 was not distributed with this source code in the |
| 9 | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
| 10 | */ |
| 11 | |
| 12 | /* clang-format off */ |
| 13 | |
| 14 | #ifdef HAVE_CONFIG_H |
| 15 | # include "config.h" |
| 16 | #endif |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | |
Nathan E. Egge | fddd3eb | 2016-11-15 10:27:38 -0500 | [diff] [blame] | 20 | #include "aom_dsp/bitreader.h" |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 21 | #include "av1/common/generic_code.h" |
| 22 | #include "av1/common/odintrin.h" |
| 23 | #include "pvq_decoder.h" |
| 24 | |
| 25 | /** Decodes a value from 0 to N-1 (with N up to 16) based on a cdf and adapts |
| 26 | * the cdf accordingly. |
| 27 | * |
Nathan E. Egge | fddd3eb | 2016-11-15 10:27:38 -0500 | [diff] [blame] | 28 | * @param [in,out] r multi-symbol entropy decoder |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 29 | * @param [in,out] cdf CDF of the variable (Q15) |
| 30 | * @param [in] n number of values possible |
| 31 | * @param [in,out] count number of symbols encoded with that cdf so far |
| 32 | * @param [in] rate adaptation rate shift (smaller is faster) |
| 33 | * @return decoded variable |
| 34 | */ |
Nathan E. Egge | fddd3eb | 2016-11-15 10:27:38 -0500 | [diff] [blame] | 35 | int aom_decode_cdf_adapt_q15_(aom_reader *r, uint16_t *cdf, int n, |
Nathan E. Egge | cefb409 | 2017-01-04 16:43:22 -0500 | [diff] [blame] | 36 | int *count, int rate ACCT_STR_PARAM) { |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 37 | int val; |
| 38 | int i; |
| 39 | if (*count == 0) { |
| 40 | int ft; |
| 41 | ft = cdf[n - 1]; |
| 42 | for (i = 0; i < n; i++) { |
| 43 | cdf[i] = cdf[i]*32768/ft; |
| 44 | } |
| 45 | } |
Nathan E. Egge | cefb409 | 2017-01-04 16:43:22 -0500 | [diff] [blame] | 46 | val = aom_read_cdf(r, cdf, n, ACCT_STR_NAME); |
Nathan E. Egge | d6c2dc4 | 2016-11-15 10:07:58 -0500 | [diff] [blame] | 47 | aom_cdf_adapt_q15(val, cdf, n, count, rate); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 48 | return val; |
| 49 | } |
| 50 | |
| 51 | /** Decodes a value from 0 to N-1 (with N up to 16) based on a cdf and adapts |
| 52 | * the cdf accordingly. |
| 53 | * |
| 54 | * @param [in,out] enc range encoder |
| 55 | * @param [in] cdf CDF of the variable (Q15) |
| 56 | * @param [in] n number of values possible |
| 57 | * @param [in] increment adaptation speed (Q15) |
| 58 | * |
| 59 | * @retval decoded variable |
| 60 | */ |
Nathan E. Egge | f25bae4 | 2016-12-29 10:15:06 -0500 | [diff] [blame] | 61 | int aom_decode_cdf_adapt_(aom_reader *r, uint16_t *cdf, int n, |
Nathan E. Egge | cefb409 | 2017-01-04 16:43:22 -0500 | [diff] [blame] | 62 | int increment ACCT_STR_PARAM) { |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 63 | int i; |
| 64 | int val; |
Nathan E. Egge | cefb409 | 2017-01-04 16:43:22 -0500 | [diff] [blame] | 65 | val = aom_read_cdf_unscaled(r, cdf, n, ACCT_STR_NAME); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 66 | if (cdf[n-1] + increment > 32767) { |
| 67 | for (i = 0; i < n; i++) { |
| 68 | /* Second term ensures that the pdf is non-null */ |
| 69 | cdf[i] = (cdf[i] >> 1) + i + 1; |
| 70 | } |
| 71 | } |
| 72 | for (i = val; i < n; i++) cdf[i] += increment; |
| 73 | return val; |
| 74 | } |
| 75 | |
| 76 | /** Encodes a random variable using a "generic" model, assuming that the |
| 77 | * distribution is one-sided (zero and up), has a single mode, and decays |
| 78 | * exponentially past the model. |
| 79 | * |
Nathan E. Egge | 89f5876 | 2016-12-28 16:31:50 -0500 | [diff] [blame] | 80 | * @param [in,out] r multi-symbol entropy decoder |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 81 | * @param [in,out] model generic probability model |
| 82 | * @param [in] x variable being encoded |
| 83 | * @param [in,out] ExQ16 expectation of x (adapted) |
| 84 | * @param [in] integration integration period of ExQ16 (leaky average over |
| 85 | * 1<<integration samples) |
| 86 | * |
| 87 | * @retval decoded variable x |
| 88 | */ |
Nathan E. Egge | 89f5876 | 2016-12-28 16:31:50 -0500 | [diff] [blame] | 89 | int generic_decode_(aom_reader *r, generic_encoder *model, int max, |
Nathan E. Egge | cefb409 | 2017-01-04 16:43:22 -0500 | [diff] [blame] | 90 | int *ex_q16, int integration ACCT_STR_PARAM) { |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 91 | int lg_q1; |
| 92 | int shift; |
| 93 | int id; |
| 94 | uint16_t *cdf; |
| 95 | int xs; |
| 96 | int lsb; |
| 97 | int x; |
| 98 | int ms; |
| 99 | lsb = 0; |
| 100 | if (max == 0) return 0; |
| 101 | lg_q1 = log_ex(*ex_q16); |
| 102 | /* If expectation is too large, shift x to ensure that |
| 103 | all we have past xs=15 is the exponentially decaying tail |
| 104 | of the distribution. */ |
| 105 | shift = OD_MAXI(0, (lg_q1 - 5) >> 1); |
| 106 | /* Choose the cdf to use: we have two per "octave" of ExQ16. */ |
| 107 | id = OD_MINI(GENERIC_TABLES - 1, lg_q1); |
| 108 | cdf = model->cdf[id]; |
| 109 | ms = (max + (1 << shift >> 1)) >> shift; |
Nathan E. Egge | cefb409 | 2017-01-04 16:43:22 -0500 | [diff] [blame] | 110 | if (max == -1) xs = aom_read_cdf_unscaled(r, cdf, 16, ACCT_STR_NAME); |
| 111 | else xs = aom_read_cdf_unscaled(r, cdf, OD_MINI(ms + 1, 16), ACCT_STR_NAME); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 112 | if (xs == 15) { |
| 113 | int e; |
| 114 | unsigned decay; |
| 115 | /* Estimate decay based on the assumption that the distribution is close |
| 116 | to Laplacian for large values. We should probably have an adaptive |
| 117 | estimate instead. Note: The 2* is a kludge that's not fully understood |
| 118 | yet. */ |
| 119 | OD_ASSERT(*ex_q16 < INT_MAX >> 1); |
| 120 | e = ((2**ex_q16 >> 8) + (1 << shift >> 1)) >> shift; |
| 121 | decay = OD_MAXI(2, OD_MINI(254, 256*e/(e + 256))); |
Nathan E. Egge | cefb409 | 2017-01-04 16:43:22 -0500 | [diff] [blame] | 122 | xs += aom_laplace_decode_special(r, decay, (max == -1) ? -1 : ms - 15, ACCT_STR_NAME); |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 123 | } |
| 124 | if (shift != 0) { |
| 125 | int special; |
| 126 | /* Because of the rounding, there's only half the number of possibilities |
| 127 | for xs=0 */ |
| 128 | special = xs == 0; |
Nathan E. Egge | 8fcfcc5 | 2017-01-04 10:20:17 -0500 | [diff] [blame] | 129 | if (shift - special > 0) { |
Nathan E. Egge | cefb409 | 2017-01-04 16:43:22 -0500 | [diff] [blame] | 130 | lsb = aom_read_literal(r, shift - special, ACCT_STR_NAME); |
Nathan E. Egge | 8fcfcc5 | 2017-01-04 10:20:17 -0500 | [diff] [blame] | 131 | } |
Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 132 | lsb -= !special << (shift - 1); |
| 133 | } |
| 134 | x = (xs << shift) + lsb; |
| 135 | generic_model_update(model, ex_q16, x, xs, id, integration); |
| 136 | OD_LOG((OD_LOG_ENTROPY_CODER, OD_LOG_DEBUG, |
| 137 | "dec: %d %d %d %d %d %x", *ex_q16, x, shift, id, xs, dec->rng)); |
| 138 | return x; |
| 139 | } |