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 #include <assert.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include "aom_util/debug_util.h"
16 
17 #define QUEUE_MAX_SIZE 2000000
18 static int result_queue[QUEUE_MAX_SIZE];
19 static int nsymbs_queue[QUEUE_MAX_SIZE];
20 static aom_cdf_prob cdf_queue[QUEUE_MAX_SIZE][16];
21 
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 
28 static int frame_idx_w = 0;
29 
30 static int frame_idx_r = 0;
31 
bitstream_queue_set_frame_write(int frame_idx)32 void bitstream_queue_set_frame_write(int frame_idx) { frame_idx_w = frame_idx; }
33 
bitstream_queue_get_frame_write(void)34 int bitstream_queue_get_frame_write(void) { return frame_idx_w; }
35 
bitstream_queue_set_frame_read(int frame_idx)36 void bitstream_queue_set_frame_read(int frame_idx) { frame_idx_r = frame_idx; }
37 
bitstream_queue_get_frame_read(void)38 int bitstream_queue_get_frame_read(void) { return frame_idx_r; }
39 
bitstream_queue_set_skip_write(int skip)40 void bitstream_queue_set_skip_write(int skip) { skip_w = skip; }
41 
bitstream_queue_set_skip_read(int skip)42 void bitstream_queue_set_skip_read(int skip) { skip_r = skip; }
43 
bitstream_queue_record_write(void)44 void bitstream_queue_record_write(void) { queue_prev_w = queue_w; }
45 
bitstream_queue_reset_write(void)46 void bitstream_queue_reset_write(void) { queue_w = queue_prev_w; }
47 
bitstream_queue_get_write(void)48 int bitstream_queue_get_write(void) { return queue_w; }
49 
bitstream_queue_get_read(void)50 int bitstream_queue_get_read(void) { return queue_r; }
51 
bitstream_queue_pop(int * result,aom_cdf_prob * cdf,int * nsymbs)52 void bitstream_queue_pop(int *result, aom_cdf_prob *cdf, int *nsymbs) {
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 
bitstream_queue_push(int result,const aom_cdf_prob * cdf,int nsymbs)65 void bitstream_queue_push(int result, const aom_cdf_prob *cdf, int nsymbs) {
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));
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 }
77