2016-03-06 20:42:47 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016, Alliance for Open Media. All rights reserved
|
|
|
|
*
|
|
|
|
* This source code is subject to the terms of the BSD 2 Clause License and
|
|
|
|
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
|
|
|
|
* was not distributed with this source code in the LICENSE file, you can
|
|
|
|
* obtain it at www.aomedia.org/license/software. If the Alliance for Open
|
|
|
|
* Media Patent License 1.0 was not distributed with this source code in the
|
|
|
|
* PATENTS file, you can obtain it at www.aomedia.org/license/patent.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef AOM_DSP_DAALABOOLREADER_H_
|
|
|
|
#define AOM_DSP_DAALABOOLREADER_H_
|
|
|
|
|
2016-08-26 00:40:54 +03:00
|
|
|
#include "aom/aom_integer.h"
|
2016-03-06 20:42:47 +03:00
|
|
|
#include "aom_dsp/entdec.h"
|
|
|
|
#include "aom_dsp/prob.h"
|
2016-08-26 00:40:54 +03:00
|
|
|
#if CONFIG_ACCOUNTING
|
|
|
|
#include "av1/common/accounting.h"
|
|
|
|
#endif
|
2016-11-11 12:25:56 +03:00
|
|
|
#if CONFIG_BITSTREAM_DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "aom_util/debug_util.h"
|
|
|
|
#endif // CONFIG_BITSTREAM_DEBUG
|
2016-03-06 20:42:47 +03:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct daala_reader {
|
|
|
|
const uint8_t *buffer;
|
|
|
|
const uint8_t *buffer_end;
|
|
|
|
od_ec_dec ec;
|
2016-08-26 00:40:54 +03:00
|
|
|
#if CONFIG_ACCOUNTING
|
|
|
|
Accounting *accounting;
|
|
|
|
#endif
|
2016-03-06 20:42:47 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct daala_reader daala_reader;
|
|
|
|
|
|
|
|
int aom_daala_reader_init(daala_reader *r, const uint8_t *buffer, int size);
|
|
|
|
const uint8_t *aom_daala_reader_find_end(daala_reader *r);
|
2016-09-07 06:48:43 +03:00
|
|
|
uint32_t aom_daala_reader_tell(const daala_reader *r);
|
|
|
|
uint32_t aom_daala_reader_tell_frac(const daala_reader *r);
|
2016-03-06 20:42:47 +03:00
|
|
|
|
|
|
|
static INLINE int aom_daala_read(daala_reader *r, int prob) {
|
2016-11-11 12:25:56 +03:00
|
|
|
int bit;
|
|
|
|
int p = ((prob << 15) + (256 - prob)) >> 8;
|
|
|
|
#if CONFIG_BITSTREAM_DEBUG
|
2016-11-17 17:43:33 +03:00
|
|
|
/*{
|
|
|
|
const int queue_r = bitstream_queue_get_read();
|
|
|
|
const int frame_idx = bitstream_queue_get_frame_read();
|
|
|
|
if (frame_idx == 0 && queue_r == 0) {
|
|
|
|
fprintf(stderr, "\n *** bitstream queue at frame_idx_r %d queue_r %d\n",
|
|
|
|
frame_idx, queue_r);
|
2016-11-11 12:25:56 +03:00
|
|
|
}
|
2016-11-17 17:43:33 +03:00
|
|
|
}*/
|
2016-11-11 12:25:56 +03:00
|
|
|
#endif
|
|
|
|
|
2016-11-09 21:40:26 +03:00
|
|
|
bit = od_ec_decode_bool_q15(&r->ec, p);
|
2016-11-11 12:25:56 +03:00
|
|
|
|
|
|
|
#if CONFIG_BITSTREAM_DEBUG
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int ref_bit, ref_nsymbs;
|
|
|
|
aom_cdf_prob ref_cdf[16];
|
|
|
|
const int queue_r = bitstream_queue_get_read();
|
|
|
|
const int frame_idx = bitstream_queue_get_frame_read();
|
|
|
|
bitstream_queue_pop(&ref_bit, ref_cdf, &ref_nsymbs);
|
|
|
|
if (ref_nsymbs != 2) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"\n *** [bit] nsymbs error, frame_idx_r %d nsymbs %d ref_nsymbs "
|
|
|
|
"%d queue_r %d\n",
|
|
|
|
frame_idx, 2, ref_nsymbs, queue_r);
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
if ((ref_nsymbs != 2) || (ref_cdf[0] != (aom_cdf_prob)p) ||
|
|
|
|
(ref_cdf[1] != 32767)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"\n *** [bit] cdf error, frame_idx_r %d cdf {%d, %d} ref_cdf {%d",
|
|
|
|
frame_idx, p, 32767, ref_cdf[0]);
|
|
|
|
for (i = 1; i < ref_nsymbs; ++i) fprintf(stderr, ", %d", ref_cdf[i]);
|
|
|
|
fprintf(stderr, "} queue_r %d\n", queue_r);
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
if (bit != ref_bit) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"\n *** [bit] symb error, frame_idx_r %d symb %d ref_symb %d "
|
|
|
|
"queue_r %d\n",
|
|
|
|
frame_idx, bit, ref_bit, queue_r);
|
|
|
|
assert(0);
|
|
|
|
}
|
2016-03-06 20:42:47 +03:00
|
|
|
}
|
2016-11-11 12:25:56 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return bit;
|
2016-03-06 20:42:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static INLINE int aom_daala_read_bit(daala_reader *r) {
|
2016-11-09 21:40:26 +03:00
|
|
|
return od_ec_dec_bits(&r->ec, 1, "aom_bits");
|
2016-03-06 20:42:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static INLINE int aom_daala_reader_has_error(daala_reader *r) {
|
|
|
|
return r->ec.error;
|
|
|
|
}
|
|
|
|
|
2016-11-11 12:25:56 +03:00
|
|
|
static INLINE int daala_read_symbol(daala_reader *r, const aom_cdf_prob *cdf,
|
|
|
|
int nsymbs) {
|
|
|
|
int symb = od_ec_decode_cdf_q15(&r->ec, cdf, nsymbs);
|
|
|
|
|
|
|
|
#if CONFIG_BITSTREAM_DEBUG
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int cdf_error = 0;
|
|
|
|
int ref_symb, ref_nsymbs;
|
|
|
|
aom_cdf_prob ref_cdf[16];
|
|
|
|
const int queue_r = bitstream_queue_get_read();
|
|
|
|
const int frame_idx = bitstream_queue_get_frame_read();
|
|
|
|
bitstream_queue_pop(&ref_symb, ref_cdf, &ref_nsymbs);
|
|
|
|
if (nsymbs != ref_nsymbs) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"\n *** nsymbs error, frame_idx_r %d nsymbs %d ref_nsymbs %d "
|
|
|
|
"queue_r %d\n",
|
|
|
|
frame_idx, nsymbs, ref_nsymbs, queue_r);
|
|
|
|
cdf_error = 0;
|
|
|
|
assert(0);
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < nsymbs; ++i)
|
|
|
|
if (cdf[i] != ref_cdf[i]) cdf_error = 1;
|
|
|
|
}
|
|
|
|
if (cdf_error) {
|
|
|
|
fprintf(stderr, "\n *** cdf error, frame_idx_r %d cdf {%d", frame_idx,
|
|
|
|
cdf[0]);
|
|
|
|
for (i = 1; i < nsymbs; ++i) fprintf(stderr, ", %d", cdf[i]);
|
|
|
|
fprintf(stderr, "} ref_cdf {%d", ref_cdf[0]);
|
|
|
|
for (i = 1; i < ref_nsymbs; ++i) fprintf(stderr, ", %d", ref_cdf[i]);
|
|
|
|
fprintf(stderr, "} queue_r %d\n", queue_r);
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
if (symb != ref_symb) {
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"\n *** symb error, frame_idx_r %d symb %d ref_symb %d queue_r %d\n",
|
|
|
|
frame_idx, symb, ref_symb, queue_r);
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return symb;
|
|
|
|
}
|
|
|
|
|
2016-03-06 21:41:53 +03:00
|
|
|
static INLINE int daala_read_tree_bits(daala_reader *r,
|
|
|
|
const aom_tree_index *tree,
|
|
|
|
const aom_prob *probs) {
|
|
|
|
aom_tree_index i = 0;
|
|
|
|
do {
|
2016-08-20 00:16:31 +03:00
|
|
|
aom_cdf_prob cdf[16];
|
2016-03-06 21:41:53 +03:00
|
|
|
aom_tree_index index[16];
|
|
|
|
int path[16];
|
|
|
|
int dist[16];
|
|
|
|
int nsymbs;
|
|
|
|
int symb;
|
|
|
|
nsymbs = tree_to_cdf(tree, probs, i, cdf, index, path, dist);
|
2016-11-11 12:25:56 +03:00
|
|
|
symb = daala_read_symbol(r, cdf, nsymbs);
|
2016-03-06 21:41:53 +03:00
|
|
|
OD_ASSERT(symb >= 0 && symb < nsymbs);
|
|
|
|
i = index[symb];
|
|
|
|
} while (i > 0);
|
|
|
|
return -i;
|
|
|
|
}
|
|
|
|
|
2016-03-06 20:42:47 +03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|