Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Alex Converse | 7fe2ae8 | 2016-09-28 11:33:20 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3 | * |
Alex Converse | 7fe2ae8 | 2016-09-28 11:33:20 -0700 | [diff] [blame] | 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. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <string.h> |
| 13 | |
Alex Converse | 1ac1ae7 | 2016-09-17 15:11:16 -0700 | [diff] [blame] | 14 | #include "aom_dsp/buf_ans.h" |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 15 | #include "aom_mem/aom_mem.h" |
Alex Converse | 1ac1ae7 | 2016-09-17 15:11:16 -0700 | [diff] [blame] | 16 | #include "aom/internal/aom_codec_internal.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 17 | |
Alex Converse | 1ac1ae7 | 2016-09-17 15:11:16 -0700 | [diff] [blame] | 18 | void aom_buf_ans_alloc(struct BufAnsCoder *c, |
Alex Converse | 2cdf0d8 | 2016-12-13 13:53:09 -0800 | [diff] [blame] | 19 | struct aom_internal_error_info *error, int size) { |
Alex Converse | 1ac1ae7 | 2016-09-17 15:11:16 -0700 | [diff] [blame] | 20 | c->error = error; |
Alex Converse | 2cdf0d8 | 2016-12-13 13:53:09 -0800 | [diff] [blame] | 21 | c->size = size; |
Alex Converse | eb780e7 | 2016-12-13 12:46:41 -0800 | [diff] [blame] | 22 | assert(c->size > 1); |
Alex Converse | 1ac1ae7 | 2016-09-17 15:11:16 -0700 | [diff] [blame] | 23 | AOM_CHECK_MEM_ERROR(error, c->buf, aom_malloc(c->size * sizeof(*c->buf))); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 24 | // Initialize to overfull to trigger the assert in write. |
| 25 | c->offset = c->size + 1; |
| 26 | } |
| 27 | |
Alex Converse | 1ac1ae7 | 2016-09-17 15:11:16 -0700 | [diff] [blame] | 28 | void aom_buf_ans_free(struct BufAnsCoder *c) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 29 | aom_free(c->buf); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 30 | c->buf = NULL; |
| 31 | c->size = 0; |
| 32 | } |
| 33 | |
Alex Converse | 2cdf0d8 | 2016-12-13 13:53:09 -0800 | [diff] [blame] | 34 | #if !ANS_MAX_SYMBOLS |
Alex Converse | 1ac1ae7 | 2016-09-17 15:11:16 -0700 | [diff] [blame] | 35 | void aom_buf_ans_grow(struct BufAnsCoder *c) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 36 | struct buffered_ans_symbol *new_buf = NULL; |
| 37 | int new_size = c->size * 2; |
Alex Converse | 1ac1ae7 | 2016-09-17 15:11:16 -0700 | [diff] [blame] | 38 | AOM_CHECK_MEM_ERROR(c->error, new_buf, |
| 39 | aom_malloc(new_size * sizeof(*new_buf))); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 40 | memcpy(new_buf, c->buf, c->size * sizeof(*c->buf)); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 41 | aom_free(c->buf); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 42 | c->buf = new_buf; |
| 43 | c->size = new_size; |
| 44 | } |
Alex Converse | 2cdf0d8 | 2016-12-13 13:53:09 -0800 | [diff] [blame] | 45 | #endif |
Alex Converse | 1ecdf2b | 2016-11-30 15:51:12 -0800 | [diff] [blame] | 46 | |
| 47 | void aom_buf_ans_flush(struct BufAnsCoder *const c) { |
| 48 | int offset; |
Alex Converse | b0be641 | 2016-11-30 15:51:50 -0800 | [diff] [blame] | 49 | #if ANS_MAX_SYMBOLS |
| 50 | if (c->offset == 0) return; |
| 51 | #endif |
| 52 | assert(c->offset > 0); |
Alex Converse | 3920406 | 2017-02-02 15:59:29 -0800 | [diff] [blame] | 53 | offset = c->offset - 1; |
| 54 | // Code the first symbol such that it brings the state to the smallest normal |
| 55 | // state from an initial state that would have been a subnormal/refill state. |
| 56 | if (c->buf[offset].method == ANS_METHOD_RANS) { |
| 57 | c->ans.state += c->buf[offset].val_start; |
| 58 | } else { |
| 59 | c->ans.state += c->buf[offset].val_start ? c->buf[offset].prob : 0; |
| 60 | } |
| 61 | for (offset = offset - 1; offset >= 0; --offset) { |
Alex Converse | 1ecdf2b | 2016-11-30 15:51:12 -0800 | [diff] [blame] | 62 | if (c->buf[offset].method == ANS_METHOD_RANS) { |
Alex Converse | 8c687d2 | 2017-02-07 15:55:24 -0800 | [diff] [blame] | 63 | rans_write(&c->ans, c->buf[offset].val_start, c->buf[offset].prob); |
Alex Converse | 1ecdf2b | 2016-11-30 15:51:12 -0800 | [diff] [blame] | 64 | } else { |
Alex Converse | c54692b | 2017-01-25 16:41:05 -0800 | [diff] [blame] | 65 | rabs_write(&c->ans, (uint8_t)c->buf[offset].val_start, |
Alex Converse | 1ecdf2b | 2016-11-30 15:51:12 -0800 | [diff] [blame] | 66 | (AnsP8)c->buf[offset].prob); |
| 67 | } |
| 68 | } |
| 69 | c->offset = 0; |
| 70 | c->output_bytes += ans_write_end(&c->ans); |
| 71 | } |