blob: 86f97a3534d6e06f14ef6c2333030e7e72f43a09 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * 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.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
Yaowu Xuf883b422016-08-30 14:01:10 -070012#ifndef AOM_DSP_BITWRITER_H_
13#define AOM_DSP_BITWRITER_H_
Yaowu Xuc27fc142016-08-22 16:08:15 -070014
Alex Converse080a2cc2016-09-20 16:39:01 -070015#include <assert.h>
16#include "./aom_config.h"
17#if CONFIG_ANS
18#include "aom_dsp/buf_ans.h"
19#elif CONFIG_DAALA_EC
20#include "aom_dsp/daalaboolwriter.h"
21#else
Alex Converseeb00cb22016-06-06 15:12:06 -070022#include "aom_dsp/dkboolwriter.h"
Alex Converse080a2cc2016-09-20 16:39:01 -070023#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070024#include "aom_dsp/prob.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070025
26#ifdef __cplusplus
27extern "C" {
28#endif
29
Alex Converse080a2cc2016-09-20 16:39:01 -070030#if CONFIG_ANS
31typedef struct BufAnsCoder aom_writer;
Nathan E. Egge8043cc42016-03-06 12:42:47 -050032#elif CONFIG_DAALA_EC
33typedef struct daala_writer aom_writer;
Alex Converse080a2cc2016-09-20 16:39:01 -070034#else
Alex Converseeb00cb22016-06-06 15:12:06 -070035typedef struct aom_dk_writer aom_writer;
Alex Converse080a2cc2016-09-20 16:39:01 -070036#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070037
Alex Converseeb00cb22016-06-06 15:12:06 -070038static INLINE void aom_start_encode(aom_writer *bc, uint8_t *buffer) {
Alex Converse080a2cc2016-09-20 16:39:01 -070039#if CONFIG_ANS
40 (void)bc;
41 (void)buffer;
42 assert(0 && "buf_ans requires a more complicated startup procedure");
Nathan E. Egge8043cc42016-03-06 12:42:47 -050043#elif CONFIG_DAALA_EC
44 aom_daala_start_encode(bc, buffer);
Alex Converse080a2cc2016-09-20 16:39:01 -070045#else
Alex Converseeb00cb22016-06-06 15:12:06 -070046 aom_dk_start_encode(bc, buffer);
Alex Converse080a2cc2016-09-20 16:39:01 -070047#endif
Alex Converseeb00cb22016-06-06 15:12:06 -070048}
49
Alex Converse080a2cc2016-09-20 16:39:01 -070050static INLINE void aom_stop_encode(aom_writer *bc) {
51#if CONFIG_ANS
52 (void)bc;
53 assert(0 && "buf_ans requires a more complicated shutdown procedure");
Nathan E. Egge8043cc42016-03-06 12:42:47 -050054#elif CONFIG_DAALA_EC
55 aom_daala_stop_encode(bc);
Alex Converse080a2cc2016-09-20 16:39:01 -070056#else
57 aom_dk_stop_encode(bc);
58#endif
59}
Yaowu Xuc27fc142016-08-22 16:08:15 -070060
Yaowu Xuf883b422016-08-30 14:01:10 -070061static INLINE void aom_write(aom_writer *br, int bit, int probability) {
Alex Converse080a2cc2016-09-20 16:39:01 -070062#if CONFIG_ANS
63 buf_uabs_write(br, bit, probability);
Nathan E. Egge8043cc42016-03-06 12:42:47 -050064#elif CONFIG_DAALA_EC
65 aom_daala_write(br, bit, probability);
Alex Converse080a2cc2016-09-20 16:39:01 -070066#else
Alex Converseeb00cb22016-06-06 15:12:06 -070067 aom_dk_write(br, bit, probability);
Alex Converse080a2cc2016-09-20 16:39:01 -070068#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070069}
70
Yaowu Xuf883b422016-08-30 14:01:10 -070071static INLINE void aom_write_bit(aom_writer *w, int bit) {
Alex Converse080a2cc2016-09-20 16:39:01 -070072 aom_write(w, bit, 128); // aom_prob_half
Yaowu Xuc27fc142016-08-22 16:08:15 -070073}
74
Yaowu Xuf883b422016-08-30 14:01:10 -070075static INLINE void aom_write_literal(aom_writer *w, int data, int bits) {
Alex Converse080a2cc2016-09-20 16:39:01 -070076 int bit;
77
78 for (bit = bits - 1; bit >= 0; bit--) aom_write_bit(w, 1 & (data >> bit));
Yaowu Xuc27fc142016-08-22 16:08:15 -070079}
80
Nathan E. Eggeeeedc632016-06-19 12:02:33 -040081static INLINE void aom_write_tree_bits(aom_writer *w, const aom_tree_index *tr,
82 const aom_prob *probs, int bits, int len,
83 aom_tree_index i) {
84 do {
85 const int bit = (bits >> --len) & 1;
86 aom_write(w, bit, probs[i >> 1]);
87 i = tr[i + bit];
88 } while (len);
89}
90
91static INLINE void aom_write_tree(aom_writer *w, const aom_tree_index *tree,
92 const aom_prob *probs, int bits, int len,
93 aom_tree_index i) {
Nathan E. Egge43acafd2016-03-06 13:41:53 -050094#if CONFIG_DAALA_EC
95 daala_write_tree_bits(w, tree, probs, bits, len, i);
96#else
Nathan E. Eggeeeedc632016-06-19 12:02:33 -040097 aom_write_tree_bits(w, tree, probs, bits, len, i);
Nathan E. Egge43acafd2016-03-06 13:41:53 -050098#endif
Nathan E. Eggeeeedc632016-06-19 12:02:33 -040099}
100
Alex Conversea1ac9722016-10-12 15:59:58 -0700101static INLINE void aom_write_symbol(aom_writer *w, int symb,
102 const aom_cdf_prob *cdf, int nsymbs) {
103#if CONFIG_ANS
104 struct rans_sym s;
105 (void)nsymbs;
106 assert(cdf);
Alex Conversee9f70f82016-08-22 12:44:16 -0700107 s.cum_prob = symb > 0 ? cdf[symb - 1] : 0;
108 s.prob = cdf[symb] - s.cum_prob;
Alex Conversea1ac9722016-10-12 15:59:58 -0700109 buf_rans_write(w, &s);
110#else
111 (void)w;
112 (void)symb;
113 (void)cdf;
114 (void)nsymbs;
115 assert(0 && "Unsupported bitwriter operation");
116#endif
117}
118
Yaowu Xuc27fc142016-08-22 16:08:15 -0700119#ifdef __cplusplus
120} // extern "C"
121#endif
122
Yaowu Xuf883b422016-08-30 14:01:10 -0700123#endif // AOM_DSP_BITWRITER_H_