2010-05-18 19:58:33 +04:00
|
|
|
/*
|
2010-09-09 16:16:39 +04:00
|
|
|
* Copyright (c) 2010 The WebM project authors. All Rights Reserved.
|
2010-05-18 19:58:33 +04:00
|
|
|
*
|
2010-06-18 20:39:21 +04:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
2010-06-05 00:19:40 +04:00
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
2010-06-18 20:39:21 +04:00
|
|
|
* in the file PATENTS. All contributing project authors may
|
2010-06-05 00:19:40 +04:00
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2010-05-18 19:58:33 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2013-12-16 06:25:05 +04:00
|
|
|
#ifndef VP8_ENCODER_TREEWRITER_H_
|
|
|
|
#define VP8_ENCODER_TREEWRITER_H_
|
2010-05-18 19:58:33 +04:00
|
|
|
|
|
|
|
/* Trees map alphabets into huffman-like codes suitable for an arithmetic
|
|
|
|
bit coder. Timothy S Murphy 11 October 2004 */
|
|
|
|
|
2015-08-26 02:50:32 +03:00
|
|
|
#include "./vpx_config.h"
|
2011-02-10 22:41:38 +03:00
|
|
|
#include "vp8/common/treecoder.h"
|
2010-05-18 19:58:33 +04:00
|
|
|
|
|
|
|
#include "boolhuff.h" /* for now */
|
|
|
|
|
2014-01-19 00:16:11 +04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2010-05-18 19:58:33 +04:00
|
|
|
typedef BOOL_CODER vp8_writer;
|
|
|
|
|
|
|
|
#define vp8_write vp8_encode_bool
|
|
|
|
#define vp8_write_literal vp8_encode_value
|
|
|
|
#define vp8_write_bit( W, V) vp8_write( W, V, vp8_prob_half)
|
|
|
|
|
|
|
|
#define vp8bc_write vp8bc_write_bool
|
|
|
|
#define vp8bc_write_literal vp8bc_write_bits
|
|
|
|
#define vp8bc_write_bit( W, V) vp8bc_write_bits( W, V, 1)
|
|
|
|
|
|
|
|
|
|
|
|
/* Approximate length of an encoded bool in 256ths of a bit at given prob */
|
|
|
|
|
|
|
|
#define vp8_cost_zero( x) ( vp8_prob_cost[x])
|
|
|
|
#define vp8_cost_one( x) vp8_cost_zero( vp8_complement(x))
|
|
|
|
|
|
|
|
#define vp8_cost_bit( x, b) vp8_cost_zero( (b)? vp8_complement(x) : (x) )
|
|
|
|
|
|
|
|
/* VP8BC version is scaled by 2^20 rather than 2^8; see bool_coder.h */
|
|
|
|
|
|
|
|
|
|
|
|
/* Both of these return bits, not scaled bits. */
|
|
|
|
|
2015-08-26 02:50:32 +03:00
|
|
|
static INLINE unsigned int vp8_cost_branch(const unsigned int ct[2], vp8_prob p)
|
2010-05-18 19:58:33 +04:00
|
|
|
{
|
|
|
|
/* Imitate existing calculation */
|
|
|
|
|
|
|
|
return ((ct[0] * vp8_cost_zero(p))
|
|
|
|
+ (ct[1] * vp8_cost_one(p))) >> 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Small functions to write explicit values and tokens, as well as
|
|
|
|
estimate their lengths. */
|
|
|
|
|
2012-03-21 22:11:10 +04:00
|
|
|
static void vp8_treed_write
|
2010-05-18 19:58:33 +04:00
|
|
|
(
|
|
|
|
vp8_writer *const w,
|
|
|
|
vp8_tree t,
|
|
|
|
const vp8_prob *const p,
|
|
|
|
int v,
|
|
|
|
int n /* number of bits in v, assumed nonzero */
|
|
|
|
)
|
|
|
|
{
|
|
|
|
vp8_tree_index i = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
const int b = (v >> --n) & 1;
|
|
|
|
vp8_write(w, b, p[i>>1]);
|
|
|
|
i = t[i+b];
|
|
|
|
}
|
|
|
|
while (n);
|
|
|
|
}
|
2015-08-26 02:50:32 +03:00
|
|
|
static INLINE void vp8_write_token
|
2010-05-18 19:58:33 +04:00
|
|
|
(
|
|
|
|
vp8_writer *const w,
|
|
|
|
vp8_tree t,
|
|
|
|
const vp8_prob *const p,
|
|
|
|
vp8_token *const x
|
|
|
|
)
|
|
|
|
{
|
|
|
|
vp8_treed_write(w, t, p, x->value, x->Len);
|
|
|
|
}
|
|
|
|
|
2012-03-21 22:11:10 +04:00
|
|
|
static int vp8_treed_cost(
|
2010-05-18 19:58:33 +04:00
|
|
|
vp8_tree t,
|
|
|
|
const vp8_prob *const p,
|
|
|
|
int v,
|
|
|
|
int n /* number of bits in v, assumed nonzero */
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int c = 0;
|
|
|
|
vp8_tree_index i = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
const int b = (v >> --n) & 1;
|
|
|
|
c += vp8_cost_bit(p[i>>1], b);
|
|
|
|
i = t[i+b];
|
|
|
|
}
|
|
|
|
while (n);
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
2015-08-26 02:50:32 +03:00
|
|
|
static INLINE int vp8_cost_token
|
2010-05-18 19:58:33 +04:00
|
|
|
(
|
|
|
|
vp8_tree t,
|
|
|
|
const vp8_prob *const p,
|
|
|
|
vp8_token *const x
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return vp8_treed_cost(t, p, x->value, x->Len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill array of costs for all possible token values. */
|
|
|
|
|
|
|
|
void vp8_cost_tokens(
|
|
|
|
int *Costs, const vp8_prob *, vp8_tree
|
|
|
|
);
|
|
|
|
|
2012-03-05 20:20:42 +04:00
|
|
|
void vp8_cost_tokens2(
|
|
|
|
int *Costs, const vp8_prob *, vp8_tree, int
|
|
|
|
);
|
|
|
|
|
2014-01-19 00:16:11 +04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|
|
|
|
|
2013-12-16 06:25:05 +04:00
|
|
|
#endif // VP8_ENCODER_TREEWRITER_H_
|