Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | 6e0d64c | 2016-10-10 16:21:45 -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 | * |
Yaowu Xu | 6e0d64c | 2016-10-10 16:21:45 -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 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 12 | #include <assert.h> |
| 13 | #include <stdio.h> |
David Barker | fa2865b | 2016-11-11 09:25:56 +0000 | [diff] [blame] | 14 | #include <string.h> |
Angie Chiang | 6062a8b | 2016-09-21 16:01:04 -0700 | [diff] [blame] | 15 | #include "aom_util/debug_util.h" |
David Barker | fa2865b | 2016-11-11 09:25:56 +0000 | [diff] [blame] | 16 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 17 | #define QUEUE_MAX_SIZE 2000000 |
| 18 | static int result_queue[QUEUE_MAX_SIZE]; |
David Barker | fa2865b | 2016-11-11 09:25:56 +0000 | [diff] [blame] | 19 | static int nsymbs_queue[QUEUE_MAX_SIZE]; |
| 20 | static aom_cdf_prob cdf_queue[QUEUE_MAX_SIZE][16]; |
David Barker | fa2865b | 2016-11-11 09:25:56 +0000 | [diff] [blame] | 21 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 22 | static int queue_r = 0; |
| 23 | static int queue_w = 0; |
| 24 | static int queue_prev_w = -1; |
| 25 | static int skip_r = 0; |
| 26 | static int skip_w = 0; |
| 27 | |
Angie Chiang | cb9a9eb | 2016-09-01 16:10:50 -0700 | [diff] [blame] | 28 | static int frame_idx_w = 0; |
| 29 | |
| 30 | static int frame_idx_r = 0; |
| 31 | |
| 32 | void bitstream_queue_set_frame_write(int frame_idx) { frame_idx_w = frame_idx; } |
| 33 | |
Angie Chiang | 6062a8b | 2016-09-21 16:01:04 -0700 | [diff] [blame] | 34 | int bitstream_queue_get_frame_write(void) { return frame_idx_w; } |
Angie Chiang | cb9a9eb | 2016-09-01 16:10:50 -0700 | [diff] [blame] | 35 | |
| 36 | void bitstream_queue_set_frame_read(int frame_idx) { frame_idx_r = frame_idx; } |
| 37 | |
Angie Chiang | 6062a8b | 2016-09-21 16:01:04 -0700 | [diff] [blame] | 38 | int bitstream_queue_get_frame_read(void) { return frame_idx_r; } |
Angie Chiang | cb9a9eb | 2016-09-01 16:10:50 -0700 | [diff] [blame] | 39 | |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 40 | void bitstream_queue_set_skip_write(int skip) { skip_w = skip; } |
| 41 | |
| 42 | void bitstream_queue_set_skip_read(int skip) { skip_r = skip; } |
| 43 | |
| 44 | void bitstream_queue_record_write(void) { queue_prev_w = queue_w; } |
| 45 | |
| 46 | void bitstream_queue_reset_write(void) { queue_w = queue_prev_w; } |
| 47 | |
| 48 | int bitstream_queue_get_write(void) { return queue_w; } |
| 49 | |
| 50 | int bitstream_queue_get_read(void) { return queue_r; } |
| 51 | |
Nathan E. Egge | 476c63c | 2017-05-18 18:35:16 -0400 | [diff] [blame] | 52 | void bitstream_queue_pop(int *result, aom_cdf_prob *cdf, int *nsymbs) { |
David Barker | fa2865b | 2016-11-11 09:25:56 +0000 | [diff] [blame] | 53 | if (!skip_r) { |
| 54 | if (queue_w == queue_r) { |
| 55 | printf("buffer underflow queue_w %d queue_r %d\n", queue_w, queue_r); |
| 56 | assert(0); |
| 57 | } |
| 58 | *result = result_queue[queue_r]; |
| 59 | *nsymbs = nsymbs_queue[queue_r]; |
| 60 | memcpy(cdf, cdf_queue[queue_r], *nsymbs * sizeof(*cdf)); |
| 61 | queue_r = (queue_r + 1) % QUEUE_MAX_SIZE; |
| 62 | } |
| 63 | } |
| 64 | |
Nathan E. Egge | 476c63c | 2017-05-18 18:35:16 -0400 | [diff] [blame] | 65 | void bitstream_queue_push(int result, const aom_cdf_prob *cdf, int nsymbs) { |
David Barker | fa2865b | 2016-11-11 09:25:56 +0000 | [diff] [blame] | 66 | if (!skip_w) { |
| 67 | result_queue[queue_w] = result; |
| 68 | nsymbs_queue[queue_w] = nsymbs; |
| 69 | memcpy(cdf_queue[queue_w], cdf, nsymbs * sizeof(*cdf)); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 70 | queue_w = (queue_w + 1) % QUEUE_MAX_SIZE; |
| 71 | if (queue_w == queue_r) { |
| 72 | printf("buffer overflow queue_w %d queue_r %d\n", queue_w, queue_r); |
| 73 | assert(0); |
| 74 | } |
| 75 | } |
| 76 | } |