1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libvpx/vp8/encoder/treewriter.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,126 @@ 1.4 +/* 1.5 + * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license 1.8 + * that can be found in the LICENSE file in the root of the source 1.9 + * tree. An additional intellectual property rights grant can be found 1.10 + * in the file PATENTS. All contributing project authors may 1.11 + * be found in the AUTHORS file in the root of the source tree. 1.12 + */ 1.13 + 1.14 + 1.15 +#ifndef __INC_TREEWRITER_H 1.16 +#define __INC_TREEWRITER_H 1.17 + 1.18 +/* Trees map alphabets into huffman-like codes suitable for an arithmetic 1.19 + bit coder. Timothy S Murphy 11 October 2004 */ 1.20 + 1.21 +#include "vp8/common/treecoder.h" 1.22 + 1.23 +#include "boolhuff.h" /* for now */ 1.24 + 1.25 +typedef BOOL_CODER vp8_writer; 1.26 + 1.27 +#define vp8_write vp8_encode_bool 1.28 +#define vp8_write_literal vp8_encode_value 1.29 +#define vp8_write_bit( W, V) vp8_write( W, V, vp8_prob_half) 1.30 + 1.31 +#define vp8bc_write vp8bc_write_bool 1.32 +#define vp8bc_write_literal vp8bc_write_bits 1.33 +#define vp8bc_write_bit( W, V) vp8bc_write_bits( W, V, 1) 1.34 + 1.35 + 1.36 +/* Approximate length of an encoded bool in 256ths of a bit at given prob */ 1.37 + 1.38 +#define vp8_cost_zero( x) ( vp8_prob_cost[x]) 1.39 +#define vp8_cost_one( x) vp8_cost_zero( vp8_complement(x)) 1.40 + 1.41 +#define vp8_cost_bit( x, b) vp8_cost_zero( (b)? vp8_complement(x) : (x) ) 1.42 + 1.43 +/* VP8BC version is scaled by 2^20 rather than 2^8; see bool_coder.h */ 1.44 + 1.45 + 1.46 +/* Both of these return bits, not scaled bits. */ 1.47 + 1.48 +static unsigned int vp8_cost_branch(const unsigned int ct[2], vp8_prob p) 1.49 +{ 1.50 + /* Imitate existing calculation */ 1.51 + 1.52 + return ((ct[0] * vp8_cost_zero(p)) 1.53 + + (ct[1] * vp8_cost_one(p))) >> 8; 1.54 +} 1.55 + 1.56 +/* Small functions to write explicit values and tokens, as well as 1.57 + estimate their lengths. */ 1.58 + 1.59 +static void vp8_treed_write 1.60 +( 1.61 + vp8_writer *const w, 1.62 + vp8_tree t, 1.63 + const vp8_prob *const p, 1.64 + int v, 1.65 + int n /* number of bits in v, assumed nonzero */ 1.66 +) 1.67 +{ 1.68 + vp8_tree_index i = 0; 1.69 + 1.70 + do 1.71 + { 1.72 + const int b = (v >> --n) & 1; 1.73 + vp8_write(w, b, p[i>>1]); 1.74 + i = t[i+b]; 1.75 + } 1.76 + while (n); 1.77 +} 1.78 +static void vp8_write_token 1.79 +( 1.80 + vp8_writer *const w, 1.81 + vp8_tree t, 1.82 + const vp8_prob *const p, 1.83 + vp8_token *const x 1.84 +) 1.85 +{ 1.86 + vp8_treed_write(w, t, p, x->value, x->Len); 1.87 +} 1.88 + 1.89 +static int vp8_treed_cost( 1.90 + vp8_tree t, 1.91 + const vp8_prob *const p, 1.92 + int v, 1.93 + int n /* number of bits in v, assumed nonzero */ 1.94 +) 1.95 +{ 1.96 + int c = 0; 1.97 + vp8_tree_index i = 0; 1.98 + 1.99 + do 1.100 + { 1.101 + const int b = (v >> --n) & 1; 1.102 + c += vp8_cost_bit(p[i>>1], b); 1.103 + i = t[i+b]; 1.104 + } 1.105 + while (n); 1.106 + 1.107 + return c; 1.108 +} 1.109 +static int vp8_cost_token 1.110 +( 1.111 + vp8_tree t, 1.112 + const vp8_prob *const p, 1.113 + vp8_token *const x 1.114 +) 1.115 +{ 1.116 + return vp8_treed_cost(t, p, x->value, x->Len); 1.117 +} 1.118 + 1.119 +/* Fill array of costs for all possible token values. */ 1.120 + 1.121 +void vp8_cost_tokens( 1.122 + int *Costs, const vp8_prob *, vp8_tree 1.123 +); 1.124 + 1.125 +void vp8_cost_tokens2( 1.126 + int *Costs, const vp8_prob *, vp8_tree, int 1.127 +); 1.128 + 1.129 +#endif