media/libvpx/vp8/common/treecoder.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license
michael@0 5 * that can be found in the LICENSE file in the root of the source
michael@0 6 * tree. An additional intellectual property rights grant can be found
michael@0 7 * in the file PATENTS. All contributing project authors may
michael@0 8 * be found in the AUTHORS file in the root of the source tree.
michael@0 9 */
michael@0 10
michael@0 11
michael@0 12 #if CONFIG_DEBUG
michael@0 13 #include <assert.h>
michael@0 14 #endif
michael@0 15 #include <stdio.h>
michael@0 16
michael@0 17 #include "treecoder.h"
michael@0 18
michael@0 19 static void tree2tok(
michael@0 20 struct vp8_token_struct *const p,
michael@0 21 vp8_tree t,
michael@0 22 int i,
michael@0 23 int v,
michael@0 24 int L
michael@0 25 )
michael@0 26 {
michael@0 27 v += v;
michael@0 28 ++L;
michael@0 29
michael@0 30 do
michael@0 31 {
michael@0 32 const vp8_tree_index j = t[i++];
michael@0 33
michael@0 34 if (j <= 0)
michael@0 35 {
michael@0 36 p[-j].value = v;
michael@0 37 p[-j].Len = L;
michael@0 38 }
michael@0 39 else
michael@0 40 tree2tok(p, t, j, v, L);
michael@0 41 }
michael@0 42 while (++v & 1);
michael@0 43 }
michael@0 44
michael@0 45 void vp8_tokens_from_tree(struct vp8_token_struct *p, vp8_tree t)
michael@0 46 {
michael@0 47 tree2tok(p, t, 0, 0, 0);
michael@0 48 }
michael@0 49
michael@0 50 void vp8_tokens_from_tree_offset(struct vp8_token_struct *p, vp8_tree t,
michael@0 51 int offset)
michael@0 52 {
michael@0 53 tree2tok(p - offset, t, 0, 0, 0);
michael@0 54 }
michael@0 55
michael@0 56 static void branch_counts(
michael@0 57 int n, /* n = size of alphabet */
michael@0 58 vp8_token tok [ /* n */ ],
michael@0 59 vp8_tree tree,
michael@0 60 unsigned int branch_ct [ /* n-1 */ ] [2],
michael@0 61 const unsigned int num_events[ /* n */ ]
michael@0 62 )
michael@0 63 {
michael@0 64 const int tree_len = n - 1;
michael@0 65 int t = 0;
michael@0 66
michael@0 67 #if CONFIG_DEBUG
michael@0 68 assert(tree_len);
michael@0 69 #endif
michael@0 70
michael@0 71 do
michael@0 72 {
michael@0 73 branch_ct[t][0] = branch_ct[t][1] = 0;
michael@0 74 }
michael@0 75 while (++t < tree_len);
michael@0 76
michael@0 77 t = 0;
michael@0 78
michael@0 79 do
michael@0 80 {
michael@0 81 int L = tok[t].Len;
michael@0 82 const int enc = tok[t].value;
michael@0 83 const unsigned int ct = num_events[t];
michael@0 84
michael@0 85 vp8_tree_index i = 0;
michael@0 86
michael@0 87 do
michael@0 88 {
michael@0 89 const int b = (enc >> --L) & 1;
michael@0 90 const int j = i >> 1;
michael@0 91 #if CONFIG_DEBUG
michael@0 92 assert(j < tree_len && 0 <= L);
michael@0 93 #endif
michael@0 94
michael@0 95 branch_ct [j] [b] += ct;
michael@0 96 i = tree[ i + b];
michael@0 97 }
michael@0 98 while (i > 0);
michael@0 99
michael@0 100 #if CONFIG_DEBUG
michael@0 101 assert(!L);
michael@0 102 #endif
michael@0 103 }
michael@0 104 while (++t < n);
michael@0 105
michael@0 106 }
michael@0 107
michael@0 108
michael@0 109 void vp8_tree_probs_from_distribution(
michael@0 110 int n, /* n = size of alphabet */
michael@0 111 vp8_token tok [ /* n */ ],
michael@0 112 vp8_tree tree,
michael@0 113 vp8_prob probs [ /* n-1 */ ],
michael@0 114 unsigned int branch_ct [ /* n-1 */ ] [2],
michael@0 115 const unsigned int num_events[ /* n */ ],
michael@0 116 unsigned int Pfac,
michael@0 117 int rd
michael@0 118 )
michael@0 119 {
michael@0 120 const int tree_len = n - 1;
michael@0 121 int t = 0;
michael@0 122
michael@0 123 branch_counts(n, tok, tree, branch_ct, num_events);
michael@0 124
michael@0 125 do
michael@0 126 {
michael@0 127 const unsigned int *const c = branch_ct[t];
michael@0 128 const unsigned int tot = c[0] + c[1];
michael@0 129
michael@0 130 #if CONFIG_DEBUG
michael@0 131 assert(tot < (1 << 24)); /* no overflow below */
michael@0 132 #endif
michael@0 133
michael@0 134 if (tot)
michael@0 135 {
michael@0 136 const unsigned int p = ((c[0] * Pfac) + (rd ? tot >> 1 : 0)) / tot;
michael@0 137 probs[t] = p < 256 ? (p ? p : 1) : 255; /* agree w/old version for now */
michael@0 138 }
michael@0 139 else
michael@0 140 probs[t] = vp8_prob_half;
michael@0 141 }
michael@0 142 while (++t < tree_len);
michael@0 143 }

mercurial