blob: 5e34fd69924958c37bbdaddea56fd36b3e2ef423 [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;
32#else
Alex Converseeb00cb22016-06-06 15:12:06 -070033typedef struct aom_dk_writer aom_writer;
Alex Converse080a2cc2016-09-20 16:39:01 -070034#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070035
Alex Converseeb00cb22016-06-06 15:12:06 -070036static INLINE void aom_start_encode(aom_writer *bc, uint8_t *buffer) {
Alex Converse080a2cc2016-09-20 16:39:01 -070037#if CONFIG_ANS
38 (void)bc;
39 (void)buffer;
40 assert(0 && "buf_ans requires a more complicated startup procedure");
41#else
Alex Converseeb00cb22016-06-06 15:12:06 -070042 aom_dk_start_encode(bc, buffer);
Alex Converse080a2cc2016-09-20 16:39:01 -070043#endif
Alex Converseeb00cb22016-06-06 15:12:06 -070044}
45
Alex Converse080a2cc2016-09-20 16:39:01 -070046static INLINE void aom_stop_encode(aom_writer *bc) {
47#if CONFIG_ANS
48 (void)bc;
49 assert(0 && "buf_ans requires a more complicated shutdown procedure");
50#else
51 aom_dk_stop_encode(bc);
52#endif
53}
Yaowu Xuc27fc142016-08-22 16:08:15 -070054
Yaowu Xuf883b422016-08-30 14:01:10 -070055static INLINE void aom_write(aom_writer *br, int bit, int probability) {
Alex Converse080a2cc2016-09-20 16:39:01 -070056#if CONFIG_ANS
57 buf_uabs_write(br, bit, probability);
58#else
Alex Converseeb00cb22016-06-06 15:12:06 -070059 aom_dk_write(br, bit, probability);
Alex Converse080a2cc2016-09-20 16:39:01 -070060#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070061}
62
Yaowu Xuf883b422016-08-30 14:01:10 -070063static INLINE void aom_write_bit(aom_writer *w, int bit) {
Alex Converse080a2cc2016-09-20 16:39:01 -070064 aom_write(w, bit, 128); // aom_prob_half
Yaowu Xuc27fc142016-08-22 16:08:15 -070065}
66
Yaowu Xuf883b422016-08-30 14:01:10 -070067static INLINE void aom_write_literal(aom_writer *w, int data, int bits) {
Alex Converse080a2cc2016-09-20 16:39:01 -070068 int bit;
69
70 for (bit = bits - 1; bit >= 0; bit--) aom_write_bit(w, 1 & (data >> bit));
Yaowu Xuc27fc142016-08-22 16:08:15 -070071}
72
Nathan E. Eggeeeedc632016-06-19 12:02:33 -040073static INLINE void aom_write_tree_bits(aom_writer *w, const aom_tree_index *tr,
74 const aom_prob *probs, int bits, int len,
75 aom_tree_index i) {
76 do {
77 const int bit = (bits >> --len) & 1;
78 aom_write(w, bit, probs[i >> 1]);
79 i = tr[i + bit];
80 } while (len);
81}
82
83static INLINE void aom_write_tree(aom_writer *w, const aom_tree_index *tree,
84 const aom_prob *probs, int bits, int len,
85 aom_tree_index i) {
86 aom_write_tree_bits(w, tree, probs, bits, len, i);
87}
88
Yaowu Xuc27fc142016-08-22 16:08:15 -070089#ifdef __cplusplus
90} // extern "C"
91#endif
92
Yaowu Xuf883b422016-08-30 14:01:10 -070093#endif // AOM_DSP_BITWRITER_H_