16fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org/* 20e29f91ae7876791fc422e9c7cea72b1866439a6johannkoenig@chromium.org * Copyright (c) 2013 The WebM project authors. All Rights Reserved. 36fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org * 46fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org * Use of this source code is governed by a BSD-style license 56fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org * that can be found in the LICENSE file in the root of the source 66fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org * tree. An additional intellectual property rights grant can be found 76fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org * in the file PATENTS. All contributing project authors may 86fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org * be found in the AUTHORS file in the root of the source tree. 96fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org */ 106fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org 110e29f91ae7876791fc422e9c7cea72b1866439a6johannkoenig@chromium.org#ifndef VP9_COMMON_VP9_PROB_H_ 120e29f91ae7876791fc422e9c7cea72b1866439a6johannkoenig@chromium.org#define VP9_COMMON_VP9_PROB_H_ 136fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org 141958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.org#include "./vpx_config.h" 150e29f91ae7876791fc422e9c7cea72b1866439a6johannkoenig@chromium.org 160e29f91ae7876791fc422e9c7cea72b1866439a6johannkoenig@chromium.org#include "vpx_ports/mem.h" 17d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org#include "vpx/vpx_integer.h" 180e29f91ae7876791fc422e9c7cea72b1866439a6johannkoenig@chromium.org 1910a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org#include "vp9/common/vp9_common.h" 206fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org 21dddee1ec7cedf276305b107429f684539b105276johannkoenig@chromium.org#ifdef __cplusplus 22dddee1ec7cedf276305b107429f684539b105276johannkoenig@chromium.orgextern "C" { 23dddee1ec7cedf276305b107429f684539b105276johannkoenig@chromium.org#endif 24dddee1ec7cedf276305b107429f684539b105276johannkoenig@chromium.org 25d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.orgtypedef uint8_t vp9_prob; 266fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org 270e29f91ae7876791fc422e9c7cea72b1866439a6johannkoenig@chromium.org#define MAX_PROB 255 280e29f91ae7876791fc422e9c7cea72b1866439a6johannkoenig@chromium.org 29d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org#define vp9_prob_half ((vp9_prob) 128) 306fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org 31d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.orgtypedef int8_t vp9_tree_index; 326fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org 33ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org#define TREE_SIZE(leaf_count) (2 * (leaf_count) - 2) 34ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.org 35d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org#define vp9_complement(x) (255 - x) 366fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org 376fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org/* We build coding trees compactly in arrays. 386fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org Each node of the tree is a pair of vp9_tree_indices. 396fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org Array index often references a corresponding probability table. 406fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org Index <= 0 means done encoding/decoding and value = -Index, 416fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org Index > 0 means need another bit, specification at index. 426fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org Nonnegative indices are always even; processing begins at node 0. */ 436fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org 44ecee051929d6ced19cf324688774acccc9ad4a0ajohannkoenig@chromium.orgtypedef const vp9_tree_index vp9_tree[]; 456fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org 461958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.orgstatic INLINE vp9_prob clip_prob(int p) { 47d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org return (p > 255) ? 255u : (p < 1) ? 1u : p; 48d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org} 49d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org 501958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.org// int64 is not needed for normal frame level calculations. 51dddee1ec7cedf276305b107429f684539b105276johannkoenig@chromium.org// However when outputting entropy stats accumulated over many frames 521958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.org// or even clips we can overflow int math. 531958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.org#ifdef ENTROPY_STATS 541958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.orgstatic INLINE vp9_prob get_prob(int num, int den) { 551958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.org return (den == 0) ? 128u : clip_prob(((int64_t)num * 256 + (den >> 1)) / den); 561958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.org} 571958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.org#else 581958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.orgstatic INLINE vp9_prob get_prob(int num, int den) { 59d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org return (den == 0) ? 128u : clip_prob((num * 256 + (den >> 1)) / den); 606fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org} 611958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.org#endif 626fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org 631958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.orgstatic INLINE vp9_prob get_binary_prob(int n0, int n1) { 64d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org return get_prob(n0, n0 + n1); 65d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org} 66d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org 67dddee1ec7cedf276305b107429f684539b105276johannkoenig@chromium.org/* This function assumes prob1 and prob2 are already within [1,255] range. */ 681958a6b43506c5fdf713554177fdd12c3b255c54johannkoenig@chromium.orgstatic INLINE vp9_prob weighted_prob(int prob1, int prob2, int factor) { 6910a9a0d835561a7f2300c561c514efcf374554d6fgalligan@chromium.org return ROUND_POWER_OF_TWO(prob1 * (256 - factor) + prob2 * factor, 8); 70d348b8d765c019ee7250075d663a83db00c65c08tomfinegan@chromium.org} 716fefe538d859300e7febe78271828198c10f1b52fgalligan@chromium.org 72d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.orgstatic INLINE vp9_prob merge_probs(vp9_prob pre_prob, 7353a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org const unsigned int ct[2], 7453a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org unsigned int count_sat, 7553a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org unsigned int max_update_factor) { 76d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org const vp9_prob prob = get_binary_prob(ct[0], ct[1]); 7753a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org const unsigned int count = MIN(ct[0] + ct[1], count_sat); 7853a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org const unsigned int factor = max_update_factor * count / count_sat; 7953a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org return weighted_prob(pre_prob, prob, factor); 8053a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org} 8153a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org 8276e516e2154f353aa02c504bac88afb0f95fefa7johannkoenig@chromium.orgvoid vp9_tree_merge_probs(const vp9_tree_index *tree, const vp9_prob *pre_probs, 8376e516e2154f353aa02c504bac88afb0f95fefa7johannkoenig@chromium.org const unsigned int *counts, unsigned int count_sat, 8476e516e2154f353aa02c504bac88afb0f95fefa7johannkoenig@chromium.org unsigned int max_update_factor, vp9_prob *probs); 85d851b91d14ef0bd71acdce7b90c9a8f1af1181adjohannkoenig@chromium.org 8653a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org 870e29f91ae7876791fc422e9c7cea72b1866439a6johannkoenig@chromium.orgDECLARE_ALIGNED(16, extern const uint8_t, vp9_norm[256]); 8853a13f1fa964820f7a8f9d3932a6f3c0433f8bf5fgalligan@chromium.org 89dddee1ec7cedf276305b107429f684539b105276johannkoenig@chromium.org#ifdef __cplusplus 90dddee1ec7cedf276305b107429f684539b105276johannkoenig@chromium.org} // extern "C" 91dddee1ec7cedf276305b107429f684539b105276johannkoenig@chromium.org#endif 92dddee1ec7cedf276305b107429f684539b105276johannkoenig@chromium.org 930e29f91ae7876791fc422e9c7cea72b1866439a6johannkoenig@chromium.org#endif // VP9_COMMON_VP9_PROB_H_ 94