Alex Converse | eb00cb2 | 2016-06-06 15:12:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
| 3 | * |
| 4 | * 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. |
| 10 | */ |
| 11 | |
| 12 | #ifndef AOM_DSP_DKBOOLWRITER_H_ |
| 13 | #define AOM_DSP_DKBOOLWRITER_H_ |
| 14 | |
| 15 | #include "./aom_config.h" |
| 16 | |
| 17 | #if CONFIG_BITSTREAM_DEBUG |
| 18 | #include <stdio.h> |
Angie Chiang | 6062a8b | 2016-09-21 16:01:04 -0700 | [diff] [blame] | 19 | #include "aom_util/debug_util.h" |
| 20 | #endif // CONFIG_BITSTREAM_DEBUG |
Alex Converse | eb00cb2 | 2016-06-06 15:12:06 -0700 | [diff] [blame] | 21 | |
| 22 | #include "aom_dsp/prob.h" |
| 23 | #include "aom_ports/mem.h" |
Alex Converse | eb00cb2 | 2016-06-06 15:12:06 -0700 | [diff] [blame] | 24 | |
| 25 | #ifdef __cplusplus |
| 26 | extern "C" { |
| 27 | #endif |
| 28 | |
| 29 | typedef struct aom_dk_writer { |
| 30 | unsigned int lowvalue; |
| 31 | unsigned int range; |
| 32 | int count; |
| 33 | unsigned int pos; |
| 34 | uint8_t *buffer; |
| 35 | } aom_dk_writer; |
| 36 | |
| 37 | void aom_dk_start_encode(aom_dk_writer *bc, uint8_t *buffer); |
| 38 | void aom_dk_stop_encode(aom_dk_writer *bc); |
| 39 | |
| 40 | static INLINE void aom_dk_write(aom_dk_writer *br, int bit, int probability) { |
| 41 | unsigned int split; |
| 42 | int count = br->count; |
| 43 | unsigned int range = br->range; |
| 44 | unsigned int lowvalue = br->lowvalue; |
| 45 | register int shift; |
| 46 | |
| 47 | #if CONFIG_BITSTREAM_DEBUG |
| 48 | // int queue_r = 0; |
| 49 | // int frame_idx_r = 0; |
| 50 | // int queue_w = bitstream_queue_get_write(); |
| 51 | // int frame_idx_w = bitstream_queue_get_frame_write(); |
| 52 | // if (frame_idx_w == frame_idx_r && queue_w == queue_r) { |
| 53 | // fprintf(stderr, "\n *** bitstream queue at frame_idx_w %d queue_w %d\n", |
| 54 | // frame_idx_w, queue_w); |
| 55 | // } |
| 56 | bitstream_queue_push(bit, probability); |
| 57 | #endif // CONFIG_BITSTREAM_DEBUG |
| 58 | |
| 59 | split = 1 + (((range - 1) * probability) >> 8); |
| 60 | |
| 61 | range = split; |
| 62 | |
| 63 | if (bit) { |
| 64 | lowvalue += split; |
| 65 | range = br->range - split; |
| 66 | } |
| 67 | |
| 68 | shift = aom_norm[range]; |
| 69 | |
| 70 | range <<= shift; |
| 71 | count += shift; |
| 72 | |
| 73 | if (count >= 0) { |
| 74 | int offset = shift - count; |
| 75 | |
| 76 | if ((lowvalue << (offset - 1)) & 0x80000000) { |
| 77 | int x = br->pos - 1; |
| 78 | |
| 79 | while (x >= 0 && br->buffer[x] == 0xff) { |
| 80 | br->buffer[x] = 0; |
| 81 | x--; |
| 82 | } |
| 83 | |
| 84 | br->buffer[x] += 1; |
| 85 | } |
| 86 | |
| 87 | br->buffer[br->pos++] = (lowvalue >> (24 - offset)); |
| 88 | lowvalue <<= offset; |
| 89 | shift = count; |
| 90 | lowvalue &= 0xffffff; |
| 91 | count -= 8; |
| 92 | } |
| 93 | |
| 94 | lowvalue <<= shift; |
| 95 | br->count = count; |
| 96 | br->lowvalue = lowvalue; |
| 97 | br->range = range; |
| 98 | } |
| 99 | |
Alex Converse | eb00cb2 | 2016-06-06 15:12:06 -0700 | [diff] [blame] | 100 | #ifdef __cplusplus |
| 101 | } // extern "C" |
| 102 | #endif |
| 103 | |
| 104 | #endif // AOM_DSP_DKBOOLWRITER_H_ |