• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* The copyright in this software is being made available under the BSD
2  * License, included below. This software may be subject to other third party
3  * and contributor rights, including patent rights, and no such rights are
4  * granted under this license.
5  *
6  * Copyright (c) 2002-2016, Audio Video coding Standard Workgroup of China
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  *  * Redistributions of source code must retain the above copyright notice,
13  *    this list of conditions and the following disclaimer.
14  *  * Redistributions in binary form must reproduce the above copyright notice,
15  *    this list of conditions and the following disclaimer in the documentation
16  *    and/or other materials provided with the distribution.
17  *  * Neither the name of Audio Video coding Standard Workgroup of China
18  *    nor the names of its contributors maybe
19  *    used to endorse or promote products
20  *    derived from this software without
21  *    specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33  * THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 #include <linux/kernel.h>
36 #include <linux/types.h>
37 #include <linux/errno.h>
38 #include <linux/interrupt.h>
39 #include <linux/timer.h>
40 #include <linux/platform_device.h>
41 #include <linux/amlogic/media/utils/amstream.h>
42 #include <linux/amlogic/media/frame_sync/ptsserv.h>
43 #include <linux/amlogic/media/canvas/canvas.h>
44 #include <linux/amlogic/media/vfm/vframe.h>
45 #include <linux/amlogic/media/vfm/vframe_provider.h>
46 #include <linux/amlogic/media/vfm/vframe_receiver.h>
47 #include <linux/amlogic/media/utils/vformat.h>
48 #include <linux/dma-mapping.h>
49 #include <linux/amlogic/media/codec_mm/codec_mm.h>
50 #include <linux/slab.h>
51 /* #include <mach/am_regs.h> */
52 #include <linux/module.h>
53 #include <linux/amlogic/media/utils/vdec_reg.h>
54 #include "../../../stream_input/amports/streambuf_reg.h"
55 #include "../utils/amvdec.h"
56 #include <linux/amlogic/media/registers/register.h>
57 #include "../../../stream_input/amports/amports_priv.h"
58 
59 #include "avs.h"
60 #ifdef AVSP_LONG_CABAC
61 
62 #define DECODING_SANITY_CHECK
63 
64 #define TRACE 0
65 #define LIWR_FIX 0
66 #define pow2(a, b) (1<<b)
67 #define io_printf pr_info
68 
69 static unsigned char *local_heap_adr;
70 static int local_heap_size;
71 static int local_heap_pos;
72 static int transcoding_error_flag;
73 
local_alloc(int num,int size)74 unsigned char *local_alloc(int num, int size)
75 {
76 	unsigned char *ret_buf = NULL;
77 	int alloc_size = num * size;
78 
79 	if ((local_heap_pos + alloc_size) <= local_heap_size) {
80 		ret_buf = local_heap_adr + local_heap_pos;
81 		local_heap_pos += alloc_size;
82 	} else {
83 		pr_info(
84 				"!!!local_alloc(%d) error, local_heap (size %d) is not enough\r\n",
85 				alloc_size, local_heap_size);
86 	}
87 	return ret_buf;
88 }
89 
local_heap_init(int size)90 int local_heap_init(int size)
91 {
92 	/*local_heap_adr = &local_heap[0];*/
93 	local_heap_adr = (unsigned char *)(avsp_heap_adr +
94 	MAX_CODED_FRAME_SIZE);
95 	memset(local_heap_adr, 0, LOCAL_HEAP_SIZE);
96 
97 	local_heap_size = LOCAL_HEAP_SIZE;
98 	local_heap_pos = 0;
99 	return 0;
100 }
101 
local_heap_uninit(void)102 void local_heap_uninit(void)
103 {
104 	local_heap_adr = NULL;
105 	local_heap_size = 0;
106 	local_heap_pos = 0;
107 }
108 
109 #define CODE2D_ESCAPE_SYMBOL 59
110 
111 const int vlc_golomb_order[3][7][2] =
112 
113 {{{2, 9}, {2, 9}, {2, 9}, {2, 9}, {2, 9}, {2, 9}, {2, 9}, }, {{3, 9}, {2, 9}, {
114 		2, 9}, {2, 9}, {2, 9}, {2, 9}, {2, 9}, }, {{2, 9}, {0, 9},
115 		{1, 9}, {1, 9}, {0, 9}, {-1, -1}, {-1, -1}, }, };
116 
117 const int MaxRun[3][7] = {{22, 14, 9, 6, 4, 2, 1}, {25, 18, 13, 9, 6, 4, 3}, {
118 		24, 19, 10, 7, 4, -1, -1} };
119 
120 const int refabslevel[19][26] = {{4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
121 		2, 2, 2, 2, 2, 2, 2, 2, -1, -1, -1}, {7, 4, 4, 3, 3, 3, 3, 3, 2,
122 		2, 2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
123 		10, 6, 4, 4, 3, 3, 3, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1,
124 		-1, -1, -1, -1, -1, -1, -1, -1}, {13, 7, 5, 4, 3, 2, 2, -1, -1,
125 		-1 - 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
126 		-1}, {18, 8, 4, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
127 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {22, 7, 3, -1, -1,
128 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
129 		-1, -1, -1, -1, -1}, {27, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1,
130 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {4,
131 		3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
132 		2, 2, 2, 2}, {5, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
133 		2, 2, -1, -1, -1, -1, -1, -1, -1}, {7, 5, 4, 4, 3, 3, 3, 2, 2,
134 		2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
135 		{10, 6, 5, 4, 3, 3, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1,
136 				-1, -1, -1, -1, -1, -1, -1, -1}, {13, 7, 5, 4,
137 				3, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
138 				-1, -1, -1, -1, -1, -1, -1, -1, -1}, {17, 8, 4,
139 				3, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
140 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
141 				22, 6, 3, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1,
142 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
143 				-1}, {5, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
144 				2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1}, {6, 4, 3,
145 				3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
146 				2, -1, -1, -1, -1, -1, -1}, {10, 6, 4, 4, 3, 3,
147 				2, 2, 2, 2, 2, -1, -1, -1, -1, -1, -1, -1, -1,
148 				-1, -1, -1, -1, -1, -1, -1}, {14, 7, 4, 3, 3, 2,
149 				2, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
150 				-1, -1, -1, -1, -1, -1, -1, -1}, {20, 7, 3, 2,
151 				2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
152 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1} };
153 
154 static const int incvlc_intra[7] = {0, 1, 2, 4, 7, 10, 3000};
155 static const int incvlc_chroma[5] = {0, 1, 2, 4, 3000};
156 
157 const int AVS_2DVLC_INTRA[7][26][27] = {{{0, 22, 38, -1, -1, -1, -1, -1, -1, -1,
158 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
159 		-1}, {2, 32, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
160 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {4, 44, -1, -1,
161 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
162 		-1, -1, -1, -1, -1, -1, -1}, {6, 50, -1, -1, -1, -1, -1, -1, -1,
163 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
164 		-1, -1}, {8, 54, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
165 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {10, -1,
166 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
167 		-1, -1, -1, -1, -1, -1, -1, -1, -1}, {12, -1, -1, -1, -1, -1,
168 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
169 		-1, -1, -1, -1, -1}, {14, -1, -1, -1, -1, -1, -1, -1, -1, -1,
170 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
171 		-1}, {16, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
172 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {18, -1,
173 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
174 		-1, -1, -1, -1, -1, -1, -1, -1, -1}, {20, -1, -1, -1, -1, -1,
175 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
176 		-1, -1, -1, -1, -1}, {24, -1, -1, -1, -1, -1, -1, -1, -1, -1,
177 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
178 		-1}, {26, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
179 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {28, -1,
180 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
181 		-1, -1, -1, -1, -1, -1, -1, -1, -1}, {30, -1, -1, -1, -1, -1,
182 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
183 		-1, -1, -1, -1, -1}, {34, -1, -1, -1, -1, -1, -1, -1, -1, -1,
184 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
185 		-1}, {36, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
186 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {40, -1,
187 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
188 		-1, -1, -1, -1, -1, -1, -1, -1, -1}, {42, -1, -1, -1, -1, -1,
189 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
190 		-1, -1, -1, -1, -1}, {46, -1, -1, -1, -1, -1, -1, -1, -1, -1,
191 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
192 		-1}, {48, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
193 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {52, -1,
194 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
195 		-1, -1, -1, -1, -1, -1, -1, -1, -1}, {56, -1, -1, -1, -1, -1,
196 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
197 		-1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
198 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
199 		-1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
200 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1,
201 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
202 		-1, -1, -1, -1, -1, -1, -1, -1, -1}, }, {{8, 0, 4, 15, 27, 41,
203 		55, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
204 		-1, -1, -1, -1, -1}, {-1, 2, 17, 35, -1, -1, -1, -1, -1, -1, -1,
205 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
206 		{-1, 6, 25, 53, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
207 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
208 				-1, 9, 33, -1, -1, -1, -1, -1, -1, -1, -1, -1,
209 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
210 				-1, -1, -1}, {-1, 11, 39, -1, -1, -1, -1, -1,
211 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
212 				-1, -1, -1, -1, -1, -1, -1}, {-1, 13, 45, -1,
213 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
214 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
215 				-1, 19, 49, -1, -1, -1, -1, -1, -1, -1, -1, -1,
216 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
217 				-1, -1, -1}, {-1, 21, 51, -1, -1, -1, -1, -1,
218 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
219 				-1, -1, -1, -1, -1, -1, -1}, {-1, 23, -1, -1,
220 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
221 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
222 				-1, 29, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
223 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
224 				-1, -1, -1}, {-1, 31, -1, -1, -1, -1, -1, -1,
225 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
226 				-1, -1, -1, -1, -1, -1, -1}, {-1, 37, -1, -1,
227 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
228 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
229 				-1, 43, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
230 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
231 				-1, -1, -1}, {-1, 47, -1, -1, -1, -1, -1, -1,
232 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
233 				-1, -1, -1, -1, -1, -1, -1}, {-1, 57, -1, -1,
234 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
235 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
236 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
237 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
238 				-1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1,
239 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
240 				-1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1,
241 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
242 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
243 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
244 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
245 				-1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1,
246 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
247 				-1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1,
248 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
249 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
250 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
251 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
252 				-1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1,
253 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
254 				-1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1,
255 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
256 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
257 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
258 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
259 				-1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1,
260 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
261 				-1, -1, -1, -1, -1, -1, -1}, }, {{8, 0, 2, 6,
262 		13, 17, 27, 35, 45, 55, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
263 		-1, -1, -1, -1, -1, -1, -1}, {-1, 4, 11, 21, 33, 49, -1, -1, -1,
264 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
265 		-1, -1}, {-1, 9, 23, 37, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
266 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, 15,
267 		29, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
268 		-1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, 19, 39, -1, -1, -1,
269 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
270 		-1, -1, -1, -1, -1}, {-1, 25, 43, -1, -1, -1, -1, -1, -1, -1,
271 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
272 		-1}, {-1, 31, 53, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
273 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, 41,
274 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
275 		-1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, 47, -1, -1, -1, -1,
276 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
277 		-1, -1, -1, -1, -1}, {-1, 57, -1, -1, -1, -1, -1, -1, -1, -1,
278 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
279 		-1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
280 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1,
281 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
282 		-1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1,
283 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
284 		-1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
285 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
286 		-1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
287 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1,
288 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
289 		-1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1,
290 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
291 		-1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
292 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
293 		-1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
294 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1,
295 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
296 		-1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1,
297 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
298 		-1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
299 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
300 		-1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
301 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1,
302 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
303 		-1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1,
304 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
305 		-1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
306 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
307 		-1}, }, {{8, 0, 2, 4, 9, 11, 17, 21, 25, 33, 39, 45, 55, -1, -1,
308 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, 6, 13, 19,
309 		29, 35, 47, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
310 		-1, -1, -1, -1, -1, -1, -1}, {-1, 15, 27, 41, 57, -1, -1, -1,
311 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
312 		-1, -1, -1}, {-1, 23, 37, 53, -1, -1, -1, -1, -1, -1, -1, -1,
313 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
314 		-1, 31, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
315 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, 43, -1, -1,
316 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
317 		-1, -1, -1, -1, -1, -1, -1}, {-1, 49, -1, -1, -1, -1, -1, -1,
318 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
319 		-1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
320 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
321 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
322 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1,
323 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
324 		-1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1,
325 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
326 		-1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
327 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
328 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
329 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1,
330 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
331 		-1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1,
332 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
333 		-1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
334 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
335 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
336 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1,
337 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
338 		-1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1,
339 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
340 		-1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
341 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
342 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
343 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1,
344 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
345 		-1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1,
346 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
347 		-1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
348 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
349 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
350 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1,
351 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
352 		-1, -1, -1, -1, -1, -1, -1}, }, {{6, 0, 2, 4, 7, 9, 11, 15, 17,
353 		21, 23, 29, 33, 35, 43, 47, 49, 57, -1, -1, -1, -1, -1, -1, -1,
354 		-1, -1}, {-1, 13, 19, 27, 31, 37, 45, 55, -1, -1, -1, -1, -1,
355 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1,
356 		25, 41, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
357 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, 39, -1, -1, -1,
358 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
359 		-1, -1, -1, -1, -1, -1}, {-1, 53, -1, -1, -1, -1, -1, -1, -1,
360 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
361 		-1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
362 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1,
363 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
364 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1,
365 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
366 		-1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1,
367 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
368 		-1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
369 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1,
370 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
371 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1,
372 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
373 		-1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1,
374 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
375 		-1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
376 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1,
377 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
378 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1,
379 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
380 		-1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1,
381 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
382 		-1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
383 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1,
384 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
385 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1,
386 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
387 		-1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1,
388 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
389 		-1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
390 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1,
391 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
392 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1,
393 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
394 		-1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1,
395 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
396 		-1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
397 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, }, {{0,
398 		1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 23, 25, 27, 31, 33, 37, 41,
399 		45, 49, 51, 55, -1, -1, -1, -1, -1}, {-1, 21, 29, 35, 43, 47,
400 		53, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
401 		-1, -1, -1, -1, -1}, {-1, 39, 57, -1, -1, -1, -1, -1, -1, -1,
402 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
403 		-1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
404 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1,
405 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
406 		-1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1,
407 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
408 		-1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
409 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
410 		-1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
411 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1,
412 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
413 		-1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1,
414 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
415 		-1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
416 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
417 		-1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
418 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1,
419 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
420 		-1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1,
421 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
422 		-1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
423 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
424 		-1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
425 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1,
426 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
427 		-1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1,
428 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
429 		-1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
430 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
431 		-1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
432 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1,
433 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
434 		-1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1,
435 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
436 		-1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
437 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
438 		-1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
439 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1,
440 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
441 		-1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1,
442 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
443 		-1, -1, -1, -1, -1}, }, {{0, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19,
444 		21, 23, 25, 27, 29, 31, 35, 37, 39, 41, 43, 47, 49, 51, 53, 57},
445 		{-1, 33, 45, 55, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
446 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
447 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
448 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
449 				-1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1,
450 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
451 				-1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1,
452 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
453 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
454 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
455 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
456 				-1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1,
457 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
458 				-1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1,
459 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
460 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
461 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
462 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
463 				-1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1,
464 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
465 				-1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1,
466 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
467 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
468 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
469 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
470 				-1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1,
471 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
472 				-1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1,
473 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
474 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
475 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
476 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
477 				-1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1,
478 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
479 				-1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1,
480 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
481 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
482 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
483 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
484 				-1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1,
485 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
486 				-1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1,
487 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
488 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
489 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
490 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
491 				-1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1,
492 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
493 				-1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1,
494 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
495 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
496 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
497 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
498 				-1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1,
499 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
500 				-1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1,
501 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
502 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} } };
503 
504 const int AVS_2DVLC_CHROMA[5][26][27] = {{{0, 14, 32, 56, -1, -1, -1, -1, -1,
505 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
506 		-1, -1}, {2, 48, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
507 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {4, -1, -1,
508 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
509 		-1, -1, -1, -1, -1, -1, -1, -1}, {6, -1, -1, -1, -1, -1, -1, -1,
510 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
511 		-1, -1, -1}, {8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
512 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {10,
513 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
514 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {12, -1, -1, -1, -1,
515 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
516 		-1, -1, -1, -1, -1, -1}, {16, -1, -1, -1, -1, -1, -1, -1, -1,
517 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
518 		-1, -1}, {18, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
519 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {20,
520 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
521 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {22, -1, -1, -1, -1,
522 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
523 		-1, -1, -1, -1, -1, -1}, {24, -1, -1, -1, -1, -1, -1, -1, -1,
524 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
525 		-1, -1}, {26, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
526 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {28,
527 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
528 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {30, -1, -1, -1, -1,
529 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
530 		-1, -1, -1, -1, -1, -1}, {34, -1, -1, -1, -1, -1, -1, -1, -1,
531 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
532 		-1, -1}, {36, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
533 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {38,
534 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
535 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {40, -1, -1, -1, -1,
536 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
537 		-1, -1, -1, -1, -1, -1}, {42, -1, -1, -1, -1, -1, -1, -1, -1,
538 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
539 		-1, -1}, {44, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
540 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {46,
541 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
542 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {50, -1, -1, -1, -1,
543 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
544 		-1, -1, -1, -1, -1, -1}, {52, -1, -1, -1, -1, -1, -1, -1, -1,
545 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
546 		-1, -1}, {54, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
547 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1,
548 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
549 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, }, {{0, 1, 5, 15, 29,
550 		43, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
551 		-1, -1, -1, -1, -1, -1}, {-1, 3, 21, 45, -1, -1, -1, -1, -1, -1,
552 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
553 		-1}, {-1, 7, 37, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
554 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, 9, 41, -1,
555 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
556 		-1, -1, -1, -1, -1, -1, -1}, {-1, 11, 53, -1, -1, -1, -1, -1,
557 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
558 		-1, -1, -1}, {-1, 13, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
559 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
560 		-1, 17, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
561 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, 19, -1, -1,
562 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
563 		-1, -1, -1, -1, -1, -1, -1}, {-1, 23, -1, -1, -1, -1, -1, -1,
564 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
565 		-1, -1, -1}, {-1, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
566 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
567 		-1, 27, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
568 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, 31, -1, -1,
569 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
570 		-1, -1, -1, -1, -1, -1, -1}, {-1, 33, -1, -1, -1, -1, -1, -1,
571 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
572 		-1, -1, -1}, {-1, 35, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
573 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
574 		-1, 39, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
575 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, 47, -1, -1,
576 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
577 		-1, -1, -1, -1, -1, -1, -1}, {-1, 49, -1, -1, -1, -1, -1, -1,
578 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
579 		-1, -1, -1}, {-1, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
580 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
581 		-1, 55, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
582 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, 57, -1, -1,
583 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
584 		-1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1,
585 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
586 		-1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
587 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
588 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
589 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1,
590 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
591 		-1, -1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1,
592 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
593 		-1, -1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
594 		-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, },
595 		{{2, 0, 3, 7, 11, 17, 27, 33, 47, 53, -1, -1, -1, -1, -1, -1,
596 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {
597 				-1, 5, 13, 21, 37, 55, -1, -1, -1, -1, -1, -1,
598 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
599 				-1, -1, -1}, {-1, 9, 23, 41, -1, -1, -1, -1, -1,
600 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
601 				-1, -1, -1, -1, -1, -1}, {-1, 15, 31, 57, -1,
602 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
603 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1,
604 				19, 43, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
605 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
606 				-1, -1}, {-1, 25, 45, -1, -1, -1, -1, -1, -1,
607 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
608 				-1, -1, -1, -1, -1, -1}, {-1, 29, -1, -1, -1,
609 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
610 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1,
611 				35, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
612 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
613 				-1, -1}, {-1, 39, -1, -1, -1, -1, -1, -1, -1,
614 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
615 				-1, -1, -1, -1, -1, -1}, {-1, 49, -1, -1, -1,
616 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
617 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1,
618 				51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
619 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
620 				-1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1,
621 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
622 				-1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1,
623 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
624 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1,
625 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
626 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
627 				-1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1,
628 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
629 				-1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1,
630 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
631 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1,
632 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
633 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
634 				-1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1,
635 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
636 				-1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1,
637 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
638 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1,
639 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
640 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
641 				-1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1,
642 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
643 				-1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1,
644 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
645 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1,
646 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
647 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
648 				-1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1,
649 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
650 				-1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1,
651 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
652 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1,
653 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
654 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
655 				-1, -1}, }, {{0, 1, 3, 5, 7, 11, 15, 19, 23, 29,
656 				35, 43, 47, 53, -1, -1, -1, -1, -1, -1, -1, -1,
657 				-1, -1, -1, -1, -1}, {-1, 9, 13, 21, 31, 39, 51,
658 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
659 				-1, -1, -1, -1, -1, -1, -1, -1}, {-1, 17, 27,
660 				37, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
661 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
662 				{-1, 25, 41, -1, -1, -1, -1, -1, -1, -1, -1, -1,
663 						-1, -1, -1, -1, -1, -1, -1, -1,
664 						-1, -1, -1, -1, -1, -1, -1}, {
665 						-1, 33, 55, -1, -1, -1, -1, -1,
666 						-1, -1, -1, -1, -1, -1, -1, -1,
667 						-1, -1, -1, -1, -1, -1, -1, -1,
668 						-1, -1, -1}, {-1, 45, -1, -1,
669 						-1, -1, -1, -1, -1, -1, -1, -1,
670 						-1, -1, -1, -1, -1, -1, -1, -1,
671 						-1, -1, -1, -1, -1, -1, -1}, {
672 						-1, 49, -1, -1, -1, -1, -1, -1,
673 						-1, -1, -1, -1, -1, -1, -1, -1,
674 						-1, -1, -1, -1, -1, -1, -1, -1,
675 						-1, -1, -1}, {-1, 57, -1, -1,
676 						-1, -1, -1, -1, -1, -1, -1, -1,
677 						-1, -1, -1, -1, -1, -1, -1, -1,
678 						-1, -1, -1, -1, -1, -1, -1}, {
679 						-1, -1, -1, -1, -1, -1, -1, -1,
680 						-1, -1, -1, -1, -1, -1, -1, -1,
681 						-1, -1, -1, -1, -1, -1, -1, -1,
682 						-1, -1, -1}, {-1, -1, -1, -1,
683 						-1, -1, -1, -1, -1, -1, -1, -1,
684 						-1, -1, -1, -1, -1, -1, -1, -1,
685 						-1, -1, -1, -1, -1, -1, -1}, {
686 						-1, -1, -1, -1, -1, -1, -1, -1,
687 						-1, -1, -1, -1, -1, -1, -1, -1,
688 						-1, -1, -1, -1, -1, -1, -1, -1,
689 						-1, -1, -1}, {-1, -1, -1, -1,
690 						-1, -1, -1, -1, -1, -1, -1, -1,
691 						-1, -1, -1, -1, -1, -1, -1, -1,
692 						-1, -1, -1, -1, -1, -1, -1}, {
693 						-1, -1, -1, -1, -1, -1, -1, -1,
694 						-1, -1, -1, -1, -1, -1, -1, -1,
695 						-1, -1, -1, -1, -1, -1, -1, -1,
696 						-1, -1, -1}, {-1, -1, -1, -1,
697 						-1, -1, -1, -1, -1, -1, -1, -1,
698 						-1, -1, -1, -1, -1, -1, -1, -1,
699 						-1, -1, -1, -1, -1, -1, -1}, {
700 						-1, -1, -1, -1, -1, -1, -1, -1,
701 						-1, -1, -1, -1, -1, -1, -1, -1,
702 						-1, -1, -1, -1, -1, -1, -1, -1,
703 						-1, -1, -1}, {-1, -1, -1, -1,
704 						-1, -1, -1, -1, -1, -1, -1, -1,
705 						-1, -1, -1, -1, -1, -1, -1, -1,
706 						-1, -1, -1, -1, -1, -1, -1}, {
707 						-1, -1, -1, -1, -1, -1, -1, -1,
708 						-1, -1, -1, -1, -1, -1, -1, -1,
709 						-1, -1, -1, -1, -1, -1, -1, -1,
710 						-1, -1, -1}, {-1, -1, -1, -1,
711 						-1, -1, -1, -1, -1, -1, -1, -1,
712 						-1, -1, -1, -1, -1, -1, -1, -1,
713 						-1, -1, -1, -1, -1, -1, -1}, {
714 						-1, -1, -1, -1, -1, -1, -1, -1,
715 						-1, -1, -1, -1, -1, -1, -1, -1,
716 						-1, -1, -1, -1, -1, -1, -1, -1,
717 						-1, -1, -1}, {-1, -1, -1, -1,
718 						-1, -1, -1, -1, -1, -1, -1, -1,
719 						-1, -1, -1, -1, -1, -1, -1, -1,
720 						-1, -1, -1, -1, -1, -1, -1}, {
721 						-1, -1, -1, -1, -1, -1, -1, -1,
722 						-1, -1, -1, -1, -1, -1, -1, -1,
723 						-1, -1, -1, -1, -1, -1, -1, -1,
724 						-1, -1, -1}, {-1, -1, -1, -1,
725 						-1, -1, -1, -1, -1, -1, -1, -1,
726 						-1, -1, -1, -1, -1, -1, -1, -1,
727 						-1, -1, -1, -1, -1, -1, -1}, {
728 						-1, -1, -1, -1, -1, -1, -1, -1,
729 						-1, -1, -1, -1, -1, -1, -1, -1,
730 						-1, -1, -1, -1, -1, -1, -1, -1,
731 						-1, -1, -1}, {-1, -1, -1, -1,
732 						-1, -1, -1, -1, -1, -1, -1, -1,
733 						-1, -1, -1, -1, -1, -1, -1, -1,
734 						-1, -1, -1, -1, -1, -1, -1}, {
735 						-1, -1, -1, -1, -1, -1, -1, -1,
736 						-1, -1, -1, -1, -1, -1, -1, -1,
737 						-1, -1, -1, -1, -1, -1, -1, -1,
738 						-1, -1, -1}, {-1, -1, -1, -1,
739 						-1, -1, -1, -1, -1, -1, -1, -1,
740 						-1, -1, -1, -1, -1, -1, -1, -1,
741 						-1, -1, -1, -1, -1, -1, -1}, },
742 		{{0, 1, 3, 5, 7, 9, 11, 13, 15, 19, 21, 23, 27, 29, 33, 37, 41,
743 				43, 51, 55, -1, -1, -1, -1, -1, -1, -1}, {-1,
744 				17, 25, 31, 39, 45, 53, -1, -1, -1, -1, -1, -1,
745 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
746 				-1, -1}, {-1, 35, 49, -1, -1, -1, -1, -1, -1,
747 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
748 				-1, -1, -1, -1, -1, -1}, {-1, 47, -1, -1, -1,
749 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
750 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1,
751 				57, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
752 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
753 				-1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1,
754 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
755 				-1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1,
756 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
757 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1,
758 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
759 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
760 				-1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1,
761 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
762 				-1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1,
763 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
764 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1,
765 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
766 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
767 				-1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1,
768 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
769 				-1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1,
770 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
771 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1,
772 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
773 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
774 				-1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1,
775 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
776 				-1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1,
777 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
778 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1,
779 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
780 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
781 				-1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1,
782 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
783 				-1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1,
784 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
785 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1,
786 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
787 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
788 				-1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1,
789 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
790 				-1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1,
791 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
792 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1,
793 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
794 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
795 				-1, -1}, {-1, -1, -1, -1, -1, -1, -1, -1, -1,
796 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
797 				-1, -1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1,
798 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
799 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {-1,
800 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
801 				-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
802 				-1, -1}, } };
803 
804 const int UE[64][2] = {{1, 1}, {2, 3}, {3, 3}, {4, 5}, {5, 5}, {6, 5}, {7, 5}, {
805 		8, 7}, {9, 7}, {10, 7}, {11, 7}, {12, 7}, {13, 7}, {14, 7}, {15,
806 		7}, {16, 9}, {17, 9}, {18, 9}, {19, 9}, {20, 9}, {21, 9},
807 		{22, 9}, {23, 9}, {24, 9}, {25, 9}, {26, 9}, {27, 9}, {28, 9}, {
808 				29, 9}, {30, 9}, {31, 9}, {32, 11}, {33, 11}, {
809 				34, 11}, {35, 11}, {36, 11}, {37, 11}, {38, 11},
810 		{39, 11}, {40, 11}, {41, 11}, {42, 11}, {43, 11}, {44, 11}, {45,
811 				11}, {46, 11}, {47, 11}, {48, 11}, {49, 11}, {
812 				50, 11}, {51, 11}, {52, 11}, {53, 11}, {54, 11},
813 		{55, 11}, {56, 11}, {57, 11}, {58, 11}, {59, 11}, {60, 11}, {61,
814 				11}, {62, 11}, {63, 11}, {64, 13} };
815 
816 unsigned int src_start;
817 unsigned int des_start;
818 
819 #ifdef AVSP_LONG_CABAC
820 
821 unsigned char *es_buf;
822 unsigned int es_buf_ptr;
823 unsigned int es_buf_is_overflow;
824 
825 #else
826 FILE *f_es;
827 #endif
828 unsigned int es_ptr;
829 unsigned int es_res;
830 unsigned int es_res_ptr;
831 unsigned int previous_es;
832 
init_es(void)833 void init_es(void)
834 {
835 
836 #ifdef AVSP_LONG_CABAC
837 	es_buf_is_overflow = 0;
838 
839 	es_buf[0] = 0x00;
840 	es_buf[1] = 0x00;
841 	es_buf[2] = 0x01;
842 	es_buf_ptr = 3;
843 	es_ptr = 3;
844 #else
845 	f_es = fopen("es.out", "wb");
846 	if (f_es == NULL)
847 		io_printf(" ERROR : Can not open es.out for write\n");
848 	putc(0x00, f_es);
849 	putc(0x00, f_es);
850 	putc(0x01, f_es);
851 
852 	es_ptr = 3;
853 #endif
854 	es_res = 0;
855 	es_res_ptr = 0;
856 	previous_es = 0xff;
857 
858 }
859 
push_es(int value,int num)860 void push_es(int value, int num)
861 {
862 	unsigned char wr_es_data;
863 	int push_num;
864 	int push_value;
865 
866 #ifdef DUMP_DEBUG
867 	if (avs_get_debug_flag() & ES_DUMP)
868 		io_printf(" push_es : value : 0x%x, num : %d\n", value, num);
869 #endif
870 	while (num > 0) {
871 		if (num >= 8)
872 			push_num = 8;
873 		else
874 			push_num = num;
875 
876 		num = num - push_num;
877 		push_value = (value >> num);
878 
879 		es_res = (es_res << push_num) | push_value;
880 		es_res_ptr = es_res_ptr + push_num;
881 
882 #ifdef DUMP_DEBUG
883 		if (avs_get_debug_flag() & ES_DUMP)
884 			io_printf(" #### es_res : 0x%X, es_res_ptr : %d\n",
885 				es_res, es_res_ptr);
886 #endif
887 
888 		while (es_res_ptr >= 8) {
889 			es_res_ptr = es_res_ptr & 7;
890 			wr_es_data = (es_res >> es_res_ptr) & 0xff;
891 			if ((previous_es == 0) & (wr_es_data < 4)) {
892 				io_printf(
893 						" Insert 2'b10 for emu at position : %d\n",
894 						es_ptr);
895 
896 				es_res_ptr = es_res_ptr + 2;
897 				wr_es_data = 2;
898 			}
899 #ifdef AVSP_LONG_CABAC
900 #ifdef DUMP_DEBUG
901 			if (avs_get_debug_flag() & ES_DUMP)
902 				pr_info("es_buf[%d] = 0x%02x\r\n",
903 					es_buf_ptr, wr_es_data);
904 #endif
905 			if (!es_buf_is_overflow) {
906 				es_buf[es_buf_ptr++] = wr_es_data;
907 				if (es_buf_ptr >= MAX_CODED_FRAME_SIZE)
908 					es_buf_is_overflow = 1;
909 			}
910 #else
911 			putc(wr_es_data, f_es);
912 #endif
913 			es_ptr++;
914 			previous_es = ((previous_es << 8) | wr_es_data)
915 					& 0xffff;
916 		}
917 
918 	}
919 }
920 
921 #ifdef BLOCK_SIZE
922 #undef BLOCK_SIZE
923 #endif
924 
925 #define MIN_QP          0
926 #define MAX_QP          63
927 
928 #define BLOCK_SIZE      4
929 #define B8_SIZE         8
930 #define MB_BLOCK_SIZE   16
931 
932 #define BLOCK_MULTIPLE      (MB_BLOCK_SIZE/(BLOCK_SIZE*2))
933 
934 #define DECODE_COPY_MB  0
935 #define DECODE_MB       1
936 
937 #define NO_INTRA_PMODE  5
938 #define INTRA_PMODE_4x4   10
939 #define NO_INTRA_PMODE_4x4	19
940 /* 8x8 intra prediction modes */
941 #define VERT_PRED             0
942 #define HOR_PRED              1
943 #define DC_PRED               2
944 #define DOWN_LEFT_PRED   3
945 #define DOWN_RIGHT_PRED  4
946 
947 #define VERT_PRED_4x4   0
948 #define HOR_PRED_4x4	1
949 #define DC_PRED_4x4		2
950 #define DOWN_LEFT_PRED_4x4	3
951 #define DOWN_RIGHT_PRED_4x4	4
952 
953 #define HOR_DOWN_PRED_4x4		5
954 #define VERT_LEFT_PRED_4x4	6
955 #define HOR_UP_PRED_4x4	   7
956 #define VERT_RIGHT_PRED_4x4	8
957 
958 #define DC_PRED_8       0
959 #define HOR_PRED_8      1
960 #define VERT_PRED_8     2
961 #define PLANE_8         3
962 
963 #define LUMA_16DC       0
964 #define LUMA_16AC       1
965 #define LUMA_8x8        2
966 #define LUMA_8x4        3
967 #define LUMA_4x8        4
968 #define LUMA_4x4        5
969 #define CHROMA_DC       6
970 #define CHROMA_AC       7
971 #define NUM_BLOCK_TYPES 8
972 
973 #define I_PICTURE_START_CODE    0xB3
974 #define PB_PICTURE_START_CODE   0xB6
975 #define SLICE_START_CODE_MIN    0x00
976 #define SLICE_START_CODE_MAX    0xAF
977 #define USER_DATA_START_CODE    0xB2
978 #define SEQUENCE_HEADER_CODE    0xB0
979 #define EXTENSION_START_CODE    0xB5
980 #define SEQUENCE_END_CODE       0xB1
981 #define VIDEO_EDIT_CODE         0xB7
982 
983 #define EOS             1
984 #define SOP             2
985 #define SOS             3
986 #define P8x8    8
987 #define I8MB    9
988 #define I4MB   10
989 #define IBLOCK  11
990 #define SI4MB   12
991 #define MAXMODE 13
992 
993 #define IS_INTRA(MB)    ((MB)->mb_type == I8MB  || (MB)->mb_type == I4MB)
994 #define IS_NEWINTRA(MB) ((MB)->mb_type == I4MB)
995 #define IS_OLDINTRA(MB) ((MB)->mb_type == I8MB)
996 #define IS_INTER(MB)    ((MB)->mb_type != I8MB  && (MB)->mb_type != I4MB)
997 #define IS_INTERMV(MB)  ((MB)->mb_type != I8MB  && (MB)->mb_type != I4MB\
998 	&& (MB)->mb_type != 0)
999 
1000 #define IS_DIRECT(MB)   ((MB)->mb_type == 0     && (img->type == B_IMG))
1001 #define IS_COPY(MB)     ((MB)->mb_type == 0     && (img->type == P_IMG))
1002 #define IS_P8x8(MB)     ((MB)->mb_type == P8x8)
1003 
1004 #define P_IMG     0
1005 #define B_IMG     1
1006 #define I_IMG     2
1007 
1008 #define FIELD     0
1009 #define FRAME     1
1010 
1011 #define SE_CABP    21
1012 struct decoding_environment_s {
1013 	unsigned int dbuffer;
1014 	int dbits_to_go;
1015 	unsigned char *dcodestrm;
1016 	int *dcodestrm_len;
1017 };
1018 
1019 struct bi_context_type_s {
1020 	unsigned char MPS;
1021 	unsigned int LG_PMPS;
1022 	unsigned char cycno;
1023 };
1024 
1025 
1026 /**********************************************************************
1027  * C O N T E X T S   F O R   R M   S Y N T A X   E L E M E N T S
1028  **********************************************************************
1029  */
1030 
1031 #define NUM_MB_TYPE_CTX  11
1032 #define NUM_B8_TYPE_CTX  9
1033 #define NUM_MV_RES_CTX   10
1034 #define NUM_REF_NO_CTX   6
1035 #define NUM_DELTA_QP_CTX 4
1036 #define NUM_MB_AFF_CTX 4
1037 
1038 struct motion_info_contexts_s {
1039 	struct bi_context_type_s mb_type_contexts[4][NUM_MB_TYPE_CTX];
1040 	struct bi_context_type_s b8_type_contexts[2][NUM_B8_TYPE_CTX];
1041 	struct bi_context_type_s mv_res_contexts[2][NUM_MV_RES_CTX];
1042 	struct bi_context_type_s ref_no_contexts[2][NUM_REF_NO_CTX];
1043 	struct bi_context_type_s delta_qp_contexts[NUM_DELTA_QP_CTX];
1044 	struct bi_context_type_s mb_aff_contexts[NUM_MB_AFF_CTX];
1045 #ifdef TEST_WEIGHTING_AEC
1046 struct bi_context_type_s mb_weighting_pred;
1047 #endif
1048 };
1049 
1050 #define NUM_IPR_CTX    2
1051 #define NUM_CIPR_CTX   4
1052 #define NUM_CBP_CTX    4
1053 #define NUM_BCBP_CTX   4
1054 #define NUM_MAP_CTX   16
1055 #define NUM_LAST_CTX  16
1056 
1057 #define NUM_ONE_CTX    5
1058 #define NUM_ABS_CTX    5
1059 
1060 struct texture_info_contexts {
1061 	struct bi_context_type_s ipr_contexts[NUM_IPR_CTX];
1062 	struct bi_context_type_s cipr_contexts[NUM_CIPR_CTX];
1063 	struct bi_context_type_s cbp_contexts[3][NUM_CBP_CTX];
1064 	struct bi_context_type_s bcbp_contexts[NUM_BLOCK_TYPES][NUM_BCBP_CTX];
1065 	struct bi_context_type_s one_contexts[NUM_BLOCK_TYPES][NUM_ONE_CTX];
1066 	struct bi_context_type_s abs_contexts[NUM_BLOCK_TYPES][NUM_ABS_CTX];
1067 	struct bi_context_type_s fld_map_contexts[NUM_BLOCK_TYPES][NUM_MAP_CTX];
1068 	struct bi_context_type_s fld_last_contexts
1069 	[NUM_BLOCK_TYPES][NUM_LAST_CTX];
1070 	struct bi_context_type_s map_contexts[NUM_BLOCK_TYPES][NUM_MAP_CTX];
1071 	struct bi_context_type_s last_contexts[NUM_BLOCK_TYPES][NUM_LAST_CTX];
1072 };
1073 struct img_par;
1074 
1075 struct syntaxelement {
1076 	int type;
1077 	int value1;
1078 	int value2;
1079 	int len;
1080 	int inf;
1081 	unsigned int bitpattern;
1082 	int context;
1083 	int k;
1084 	int golomb_grad;
1085 	int golomb_maxlevels;
1086 #if TRACE
1087 #define       TRACESTRING_SIZE 100
1088 	char tracestring[TRACESTRING_SIZE];
1089 #endif
1090 
1091 	void (*mapping)(int len, int info, int *value1, int *value2);
1092 
1093 	void (*reading)(struct syntaxelement *, struct img_par *,
1094 			struct decoding_environment_s *);
1095 
1096 };
1097 
1098 struct bitstream_s {
1099 
1100 	int read_len;
1101 	int code_len;
1102 
1103 	int frame_bitoffset;
1104 	int bitstream_length;
1105 
1106 	unsigned char *stream_buffer;
1107 };
1108 
1109 struct datapartition {
1110 
1111 	struct bitstream_s *bitstream;
1112 	struct decoding_environment_s de_aec;
1113 
1114 	int (*read_syntax_element)(struct syntaxelement *, struct img_par *,
1115 			struct datapartition *);
1116 /*!< virtual function;
1117  * actual method depends on chosen data partition and
1118  * entropy coding method
1119  */
1120 };
1121 
1122 struct slice_s {
1123 	int picture_id;
1124 	int qp;
1125 	int picture_type;
1126 	int start_mb_nr;
1127 	int max_part_nr;
1128 	int num_mb;
1129 
1130 	struct datapartition *part_arr;
1131 	struct motion_info_contexts_s *mot_ctx;
1132 	struct texture_info_contexts *tex_ctx;
1133 	int field_ctx[3][2];
1134 };
1135 
1136 struct img_par {
1137 	int number;
1138 	int current_mb_nr;
1139 	int max_mb_nr;
1140 	int current_slice_nr;
1141 	int tr;
1142 	int qp;
1143 	int type;
1144 
1145 	int typeb;
1146 
1147 	int width;
1148 	int height;
1149 	int width_cr;
1150 	int height_cr;
1151 	int source_bitdepth;
1152 	int mb_y;
1153 	int mb_x;
1154 	int block_y;
1155 	int pix_y;
1156 	int pix_x;
1157 	int pix_c_y;
1158 	int block_x;
1159 	int pix_c_x;
1160 
1161 	int ***mv;
1162 	int mpr[16][16];
1163 
1164 	int m7[16][16];
1165 	int m8[/*2*/4][8][8];
1166 	int cof[4][/*6*/8][4][4];
1167 	int cofu[4];
1168 	int **ipredmode;
1169 	int quad[256];
1170 	int cod_counter;
1171 
1172 	int ***dfmv;
1173 	int ***dbmv;
1174 	int **fw_reffrarr;
1175 	int **bw_reffrarr;
1176 
1177 	int ***mv_frm;
1178 	int **fw_reffrarr_frm;
1179 	int **bw_reffrarr_frm;
1180 	int imgtr_next_p;
1181 	int imgtr_last_p;
1182 	int tr_frm;
1183 	int tr_fld;
1184 	int imgtr_last_prev_p;
1185 
1186 	int no_forward_reference;
1187 	int seq_header_indicate;
1188 	int b_discard_flag;
1189 
1190 	int ***fw_mv;
1191 	int ***bw_mv;
1192 	int subblock_x;
1193 	int subblock_y;
1194 
1195 	int buf_cycle;
1196 
1197 	int direct_type;
1198 
1199 	int ***mv_top;
1200 	int ***mv_bot;
1201 	int **fw_reffrarr_top;
1202 	int **bw_reffrarr_top;
1203 	int **fw_reffrarr_bot;
1204 	int **bw_reffrarr_bot;
1205 
1206 	int **ipredmode_top;
1207 	int **ipredmode_bot;
1208 	int ***fw_mv_top;
1209 	int ***fw_mv_bot;
1210 	int ***bw_mv_top;
1211 	int ***bw_mv_bot;
1212 	int ***dfmv_top;
1213 	int ***dbmv_top;
1214 	int ***dfmv_bot;
1215 	int ***dbm_bot;
1216 
1217 	int toppoc;
1218 	int bottompoc;
1219 	int framepoc;
1220 	unsigned int frame_num;
1221 
1222 	unsigned int pic_distance;
1223 	int delta_pic_order_cnt_bottom;
1224 
1225 	signed int pic_distance_msb;
1226 	unsigned int prev_pic_distance_lsb;
1227 	signed int curr_pic_distance_msb;
1228 	unsigned int this_poc;
1229 
1230 	int pic_width_inmbs;
1231 	int pic_height_inmbs;
1232 	int pic_size_inmbs;
1233 
1234 	int block8_x, block8_y;
1235 	int structure;
1236 	int pn;
1237 	int buf_used;
1238 	int buf_size;
1239 	int picture_structure;
1240 	int advanced_pred_mode_disable;
1241 	int types;
1242 	int current_mb_nr_fld;
1243 
1244 	int p_field_enhanced;
1245 	int b_field_enhanced;
1246 
1247 	int slice_weighting_flag;
1248 	int lum_scale[4];
1249 	int lum_shift[4];
1250 	int chroma_scale[4];
1251 	int chroma_shift[4];
1252 	int mb_weighting_flag;
1253 	int weighting_prediction;
1254 	int mpr_weight[16][16];
1255 	int top_bot;
1256 	int bframe_number;
1257 
1258 	int auto_crop_right;
1259 	int auto_crop_bottom;
1260 
1261 	struct slice_s *current_slice;
1262 	int is_v_block;
1263 	int is_intra_block;
1264 
1265 	int new_seq_header_flag;
1266 	int new_sequence_flag;
1267 	int last_pic_bbv_delay;
1268 
1269 	int sequence_end_flag;
1270 	int is_top_field;
1271 
1272 	int abt_flag;
1273 	int qp_shift;
1274 
1275 #ifdef EIGHTH
1276 int eighth_subpixel_flag;
1277 int subpixel_precision;
1278 int unit_length;
1279 int subpixel_mask;
1280 
1281 int max_mvd;
1282 int min_mvd;
1283 #endif
1284 
1285 };
1286 
1287 struct macroblock {
1288 	int qp;
1289 	int slice_nr;
1290 	int delta_quant;
1291 	struct macroblock *mb_available[3][3];
1292 	/*!< pointer to neighboring MBs in a 3x3 window of current MB,
1293 	 *which is located at [1][1]
1294 	 * NULL pointer identifies neighboring MBs which are unavailable
1295 	 */
1296 
1297 	int mb_type;
1298 	int mvd[2][BLOCK_MULTIPLE][BLOCK_MULTIPLE][2];
1299 	int cbp, cbp_blk, cbp01;
1300 	unsigned long cbp_bits;
1301 
1302 	int b8mode[4];
1303 	int b8pdir[4];
1304 	int mb_type_2;
1305 	int c_ipred_mode_2;
1306 	int dct_mode;
1307 
1308 	int c_ipred_mode;
1309 	int lf_disable;
1310 	int lf_alpha_c0_offset;
1311 	int lf_beta_offset;
1312 
1313 	int CABT[4];
1314 	int CABP[4];
1315 	int cbp_4x4[4];
1316 
1317 	int skip_flag;
1318 
1319 	struct macroblock *mb_available_up;
1320 	struct macroblock *mb_available_left;
1321 	unsigned int mbaddr_a, mbaddr_b, mbaddr_c, mbaddr_d;
1322 	unsigned int mbavail_a, mbavail_b, mbavail_c, mbavail_d;
1323 
1324 };
1325 
1326 struct macroblock *mb_data;
1327 
1328 struct img_par *img;
1329 
1330 struct bitstream_s *curr_stream;
1331 
1332 struct datapartition *alloc_partition(int n);
1333 
1334 unsigned int vld_mem_start_addr;
1335 unsigned int vld_mem_end_addr;
1336 
1337 int marker_bit;
1338 
1339 int progressive_sequence;
1340 int horizontal_size;
1341 int vertical_size;
1342 
1343 int second_ifield;
1344 int pre_img_type;
1345 
1346 /* slice_header() */
1347 int slice_vertical_position;
1348 int slice_vertical_position_extension;
1349 int fixed_picture_qp;
1350 int fixed_slice_qp;
1351 int slice_qp;
1352 
1353 /*
1354  *************************************************************************
1355  * Function:ue_v, reads an u(v) syntax element, the length in bits is stored in
1356  the global UsedBits variable
1357  * Input:
1358  tracestring
1359  the string for the trace file
1360  bitstream
1361  the stream to be read from
1362  * Output:
1363  * Return: the value of the coded syntax element
1364  * Attention:
1365  *************************************************************************
1366  */
1367 /*!
1368  *  definition of AVS syntaxelements
1369  *  order of elements follow dependencies for picture reconstruction
1370  */
1371 /*!
1372  * \brief   Assignment of old TYPE partition elements to new
1373  *          elements
1374  *
1375  *  old element     | new elements
1376  *  TYPE_HEADER     | SE_HEADER, SE_PTYPE
1377  *  TYPE_MBHEADER    | SE_MBTYPE, SE_REFFRAME, SE_INTRAPREDMODE
1378  *  TYPE_MVD        | SE_MVD
1379  *  TYPE_CBP        | SE_CBP_INTRA, SE_CBP_INTER * SE_DELTA_QUANT_INTER
1380  *  SE_DELTA_QUANT_INTRA
1381  *  TYPE_COEFF_Y    | SE_LUM_DC_INTRA, SE_LUM_AC_INTRA,
1382     SE_LUM_DC_INTER, SE_LUM_AC_INTER
1383  *  TYPE_2x2DC      | SE_CHR_DC_INTRA, SE_CHR_DC_INTER
1384  *  TYPE_COEFF_C    | SE_CHR_AC_INTRA, SE_CHR_AC_INTER
1385  *  TYPE_EOS        | SE_EOS
1386  */
1387 
1388 #define SE_HEADER           0
1389 #define SE_PTYPE            1
1390 #define SE_MBTYPE           2
1391 #define SE_REFFRAME         3
1392 #define SE_INTRAPREDMODE    4
1393 #define SE_MVD              5
1394 #define SE_CBP_INTRA        6
1395 #define SE_LUM_DC_INTRA     7
1396 #define SE_CHR_DC_INTRA     8
1397 #define SE_LUM_AC_INTRA     9
1398 #define SE_CHR_AC_INTRA     10
1399 #define SE_CBP_INTER        11
1400 #define SE_LUM_DC_INTER     12
1401 #define SE_CHR_DC_INTER     13
1402 #define SE_LUM_AC_INTER     14
1403 #define SE_CHR_AC_INTER     15
1404 #define SE_DELTA_QUANT_INTER      16
1405 #define SE_DELTA_QUANT_INTRA      17
1406 #define SE_BFRAME           18
1407 #define SE_EOS              19
1408 #define SE_MAX_ELEMENTS     20
1409 #define SE_CBP01            21
1410 int chroma_format;
1411 /*
1412  *************************************************************************
1413  * Function:Reads bits from the bitstream buffer
1414  * Input:
1415  byte buffer[]
1416  containing VLC-coded data bits
1417  int totbitoffset
1418  bit offset from start of partition
1419  int bytecount
1420  total bytes in bitstream
1421  int numbits
1422  number of bits to read
1423  * Output:
1424  * Return:
1425  * Attention:
1426  *************************************************************************
1427  */
1428 
get_bits(unsigned char buffer[],int totbitoffset,int * info,int bytecount,int numbits)1429 int get_bits(unsigned char buffer[], int totbitoffset, int *info, int bytecount,
1430 		int numbits)
1431 {
1432 	register int inf;
1433 	long byteoffset;
1434 	int bitoffset;
1435 
1436 	int bitcounter = numbits;
1437 
1438 	byteoffset = totbitoffset / 8;
1439 	bitoffset = 7 - (totbitoffset % 8);
1440 
1441 	inf = 0;
1442 	while (numbits) {
1443 		inf <<= 1;
1444 		inf |= (buffer[byteoffset] & (0x01 << bitoffset)) >> bitoffset;
1445 		numbits--;
1446 		bitoffset--;
1447 		if (bitoffset < 0) {
1448 			byteoffset++;
1449 			bitoffset += 8;
1450 			if (byteoffset > bytecount)
1451 				return -1;
1452 		}
1453 	}
1454 
1455 	*info = inf;
1456 
1457 
1458 	return bitcounter;
1459 }
1460 
1461 /*
1462  *************************************************************************
1463  * Function:read FLC codeword from UVLC-partition
1464  * Input:
1465  * Output:
1466  * Return:
1467  * Attention:
1468  *************************************************************************
1469  */
1470 
read_syntaxelement_flc(struct syntaxelement * sym)1471 int read_syntaxelement_flc(struct syntaxelement *sym)
1472 {
1473 	int frame_bitoffset = curr_stream->frame_bitoffset;
1474 	unsigned char *buf = curr_stream->stream_buffer;
1475 	int bitstreamlengthinbytes = curr_stream->bitstream_length;
1476 
1477 	if ((get_bits(buf, frame_bitoffset, &(sym->inf), bitstreamlengthinbytes,
1478 			sym->len)) < 0)
1479 		return -1;
1480 
1481 	curr_stream->frame_bitoffset += sym->len;
1482 	sym->value1 = sym->inf;
1483 
1484 #if TRACE
1485 	tracebits2(sym->tracestring, sym->len, sym->inf);
1486 #endif
1487 
1488 	return 1;
1489 }
1490 
1491 /*
1492  *************************************************************************
1493  * Function:ue_v, reads an u(1) syntax element, the length in bits is stored in
1494  the global UsedBits variable
1495  * Input:
1496  tracestring
1497  the string for the trace file
1498  bitstream
1499  the stream to be read from
1500  * Output:
1501  * Return: the value of the coded syntax element
1502  * Attention:
1503  *************************************************************************
1504  */
u_1(char * tracestring)1505 int u_1(char *tracestring)
1506 {
1507 	return u_v(1, tracestring);
1508 }
1509 
1510 /*
1511  *************************************************************************
1512  * Function:mapping rule for ue(v) syntax elements
1513  * Input:length and info
1514  * Output:number in the code table
1515  * Return:
1516  * Attention:
1517  *************************************************************************
1518  */
linfo_ue(int len,int info,int * value1,int * dummy)1519 void linfo_ue(int len, int info, int *value1, int *dummy)
1520 {
1521 	*value1 = (int)pow2(2, (len / 2)) + info - 1;
1522 }
1523 
u_v(int leninbits,char * tracestring)1524 int u_v(int leninbits, char *tracestring)
1525 {
1526 	struct syntaxelement symbol, *sym = &symbol;
1527 
1528 #ifdef AVSP_LONG_CABAC
1529 #else
1530 	assert(curr_stream->stream_buffer != NULL);
1531 #endif
1532 	sym->type = SE_HEADER;
1533 	sym->mapping = linfo_ue;
1534 	sym->len = leninbits;
1535 	read_syntaxelement_flc(sym);
1536 
1537 	return sym->inf;
1538 }
1539 
1540 /*
1541  *************************************************************************
1542  * Function:mapping rule for se(v) syntax elements
1543  * Input:length and info
1544  * Output:signed mvd
1545  * Return:
1546  * Attention:
1547  *************************************************************************
1548  */
1549 
linfo_se(int len,int info,int * value1,int * dummy)1550 void linfo_se(int len, int info, int *value1, int *dummy)
1551 {
1552 	int n;
1553 
1554 	n = (int)pow2(2, (len / 2)) + info - 1;
1555 	*value1 = (n + 1) / 2;
1556 	if ((n & 0x01) == 0)
1557 		*value1 = -*value1;
1558 
1559 }
1560 
1561 /*
1562  *************************************************************************
1563  * Function:length and info
1564  * Input:
1565  * Output:cbp (intra)
1566  * Return:
1567  * Attention:
1568  *************************************************************************
1569  */
1570 
linfo_cbp_intra(int len,int info,int * cbp,int * dummy)1571 void linfo_cbp_intra(int len, int info, int *cbp, int *dummy)
1572 {
1573 }
1574 
1575 const int NCBP[64][2] = {{4, 0}, {16, 19}, {17, 16}, {19, 15}, {14, 18},
1576 		{9, 11}, {22, 31}, {8, 13}, {11, 17}, {21, 30}, {10, 12},
1577 		{7, 9}, {12, 10}, {6, 7}, {5, 8}, {1, 1}, {35, 4}, {47, 42}, {
1578 				48, 38}, {38, 27}, {46, 39}, {36, 33}, {50, 59},
1579 		{26, 26}, {45, 40}, {52, 58}, {41, 35}, {28, 25}, {37, 29}, {23,
1580 				24}, {31, 28}, {2, 3}, {43, 5}, {51, 51}, {56,
1581 				52}, {39, 37}, {55, 50}, {33, 43}, {62, 63}, {
1582 				27, 44}, {54, 53}, {60, 62}, {40, 48}, {32, 47},
1583 		{42, 34}, {24, 45}, {29, 49}, {3, 6}, {49, 14}, {53, 55}, {57,
1584 				56}, {25, 36}, {58, 54}, {30, 41}, {59, 60}, {
1585 				15, 21}, {61, 57}, {63, 61}, {44, 46}, {18, 22},
1586 		{34, 32}, {13, 20}, {20, 23}, {0, 2} };
1587 
1588 unsigned int s1, t1, value_s, value_t;
1589 unsigned char dec_bypass, dec_final;
1590 
1591 #define get_byte() {                                         \
1592 	dbuffer = dcodestrm[(*dcodestrm_len)++];\
1593 	dbits_to_go = 7;                        \
1594 }
1595 
1596 #define dbuffer         (dep->dbuffer)
1597 #define dbits_to_go     (dep->dbits_to_go)
1598 #define dcodestrm       (dep->dcodestrm)
1599 #define dcodestrm_len   (dep->dcodestrm_len)
1600 
1601 #define B_BITS	10
1602 
1603 #define LG_PMPS_SHIFTNO 2
1604 
1605 #define HALF      (1 << (B_BITS-1))
1606 #define QUARTER   (1 << (B_BITS-2))
1607 
biari_decode_symbol(struct decoding_environment_s * dep,struct bi_context_type_s * bi_ct)1608 unsigned int biari_decode_symbol(struct decoding_environment_s *dep,
1609 		struct bi_context_type_s *bi_ct)
1610 {
1611 	register unsigned char bit;
1612 	register unsigned char s_flag;
1613 	register unsigned char is_lps = 0;
1614 	register unsigned char cwr;
1615 	register unsigned char cycno = bi_ct->cycno;
1616 	register unsigned int lg_pmps = bi_ct->LG_PMPS;
1617 	register unsigned int t_rlps;
1618 	register unsigned int s2, t2;
1619 
1620 #ifdef DUMP_DEBUG
1621 	if (avs_get_debug_flag() & AEC_DUMP)
1622 		io_printf("LG_PMPS : %03X, MPS : %d, cycno : %d -- %p\n",
1623 			bi_ct->LG_PMPS, bi_ct->MPS, bi_ct->cycno, bi_ct);
1624 #endif
1625 
1626 	bit = bi_ct->MPS;
1627 
1628 	cwr = (cycno <= 1) ? 3 : (cycno == 2) ? 4 : 5;
1629 
1630 	if (t1 >= (lg_pmps >> LG_PMPS_SHIFTNO)) {
1631 		s2 = s1;
1632 		t2 = t1 - (lg_pmps >> LG_PMPS_SHIFTNO);
1633 		s_flag = 0;
1634 	} else {
1635 		s2 = s1 + 1;
1636 		t2 = 256 + t1 - (lg_pmps >> LG_PMPS_SHIFTNO);
1637 		s_flag = 1;
1638 	}
1639 
1640 #ifdef DUMP_DEBUG
1641 	if (avs_get_debug_flag() & AEC_DUMP)
1642 		io_printf(" s2 : %d, t2 : %03X\n", s2, t2);
1643 #endif
1644 
1645 	if (s2 > value_s || (s2 == value_s && value_t >= t2)) {
1646 		is_lps = 1;
1647 		bit = !bit;
1648 
1649 		t_rlps = (s_flag == 0) ?
1650 				(lg_pmps >> LG_PMPS_SHIFTNO) :
1651 				(t1 + (lg_pmps >> LG_PMPS_SHIFTNO));
1652 
1653 		if (s2 == value_s)
1654 			value_t = (value_t - t2);
1655 		else {
1656 			if (--dbits_to_go < 0)
1657 				get_byte();
1658 
1659 			value_t = (value_t << 1)
1660 					| ((dbuffer >> dbits_to_go) & 0x01);
1661 			value_t = 256 + value_t - t2;
1662 
1663 		}
1664 
1665 		while (t_rlps < QUARTER) {
1666 			t_rlps = t_rlps << 1;
1667 			if (--dbits_to_go < 0)
1668 				get_byte();
1669 
1670 			value_t = (value_t << 1)
1671 					| ((dbuffer >> dbits_to_go) & 0x01);
1672 		}
1673 
1674 		s1 = 0;
1675 		t1 = t_rlps & 0xff;
1676 
1677 		value_s = 0;
1678 		while (value_t < QUARTER) {
1679 			int j;
1680 
1681 			if (--dbits_to_go < 0)
1682 				get_byte();
1683 			j = (dbuffer >> dbits_to_go) & 0x01;
1684 
1685 			value_t = (value_t << 1) | j;
1686 			value_s++;
1687 		}
1688 		value_t = value_t & 0xff;
1689 	} else {
1690 
1691 		s1 = s2;
1692 		t1 = t2;
1693 	}
1694 
1695 	if (dec_bypass)
1696 		return bit;
1697 
1698 	if (is_lps)
1699 		cycno = (cycno <= 2) ? (cycno + 1) : 3;
1700 	else if (cycno == 0)
1701 		cycno = 1;
1702 	bi_ct->cycno = cycno;
1703 
1704 	if (is_lps) {
1705 		switch (cwr) {
1706 		case 3:
1707 			lg_pmps = lg_pmps + 197;
1708 			break;
1709 		case 4:
1710 			lg_pmps = lg_pmps + 95;
1711 			break;
1712 		default:
1713 			lg_pmps = lg_pmps + 46;
1714 		}
1715 
1716 		if (lg_pmps >= (256 << LG_PMPS_SHIFTNO)) {
1717 			lg_pmps = (512 << LG_PMPS_SHIFTNO) - 1 - lg_pmps;
1718 			bi_ct->MPS = !(bi_ct->MPS);
1719 		}
1720 	} else {
1721 #ifdef DUMP_DEBUG
1722 		if (avs_get_debug_flag() & AEC_DUMP)
1723 			io_printf(" - lg_pmps_MPS : %X (%X - %X - %X)\n",
1724 					lg_pmps - (unsigned int)(lg_pmps>>cwr)
1725 					- (unsigned int)(lg_pmps>>(cwr+2)),
1726 					lg_pmps,
1727 					(unsigned int)(lg_pmps>>cwr),
1728 					(unsigned int)(lg_pmps>>(cwr+2))
1729 			);
1730 #endif
1731 		lg_pmps = lg_pmps - (unsigned int)(lg_pmps >> cwr)
1732 				- (unsigned int)(lg_pmps >> (cwr + 2));
1733 	}
1734 
1735 	bi_ct->LG_PMPS = lg_pmps;
1736 
1737 	return bit;
1738 }
1739 
biari_decode_symbolw(struct decoding_environment_s * dep,struct bi_context_type_s * bi_ct1,struct bi_context_type_s * bi_ct2)1740 unsigned int biari_decode_symbolw(struct decoding_environment_s *dep,
1741 		struct bi_context_type_s *bi_ct1,
1742 		struct bi_context_type_s *bi_ct2)
1743 {
1744 	register unsigned char bit1, bit2;
1745 	register unsigned char pred_mps, bit;
1746 	register unsigned int lg_pmps;
1747 	register unsigned char cwr1, cycno1 = bi_ct1->cycno;
1748 	register unsigned char cwr2, cycno2 = bi_ct2->cycno;
1749 	register unsigned int lg_pmps1 = bi_ct1->LG_PMPS;
1750 	register unsigned int lg_pmps2 =
1751 			bi_ct2->LG_PMPS;
1752 	register unsigned int t_rlps;
1753 	register unsigned char s_flag, is_lps = 0;
1754 	register unsigned int s2, t2;
1755 
1756 
1757 	bit1 = bi_ct1->MPS;
1758 	bit2 = bi_ct2->MPS;
1759 
1760 	cwr1 = (cycno1 <= 1) ? 3 : (cycno1 == 2) ? 4 : 5;
1761 	cwr2 = (cycno2 <= 1) ? 3 : (cycno2 == 2) ? 4 : 5;
1762 
1763 	if (bit1 == bit2) {
1764 		pred_mps = bit1;
1765 		lg_pmps = (lg_pmps1 + lg_pmps2) / 2;
1766 	} else {
1767 		if (lg_pmps1 < lg_pmps2) {
1768 			pred_mps = bit1;
1769 			lg_pmps = (256 << LG_PMPS_SHIFTNO) - 1
1770 					- ((lg_pmps2 - lg_pmps1) >> 1);
1771 		} else {
1772 			pred_mps = bit2;
1773 			lg_pmps = (256 << LG_PMPS_SHIFTNO) - 1
1774 					- ((lg_pmps1 - lg_pmps2) >> 1);
1775 		}
1776 	}
1777 
1778 #ifdef DUMP_DEBUG
1779 	if (avs_get_debug_flag() & AEC_DUMP)
1780 		io_printf(" - Begin - LG_PMPS : %03X, MPS : %d\n",
1781 			lg_pmps, pred_mps);
1782 #endif
1783 	if (t1 >= (lg_pmps >> LG_PMPS_SHIFTNO)) {
1784 		s2 = s1;
1785 		t2 = t1 - (lg_pmps >> LG_PMPS_SHIFTNO);
1786 		s_flag = 0;
1787 	} else {
1788 		s2 = s1 + 1;
1789 		t2 = 256 + t1 - (lg_pmps >> LG_PMPS_SHIFTNO);
1790 		s_flag = 1;
1791 	}
1792 
1793 	bit = pred_mps;
1794 	if (s2 > value_s || (s2 == value_s && value_t >= t2)) {
1795 		is_lps = 1;
1796 		bit = !bit;
1797 		t_rlps = (s_flag == 0) ?
1798 				(lg_pmps >> LG_PMPS_SHIFTNO) :
1799 				(t1 + (lg_pmps >> LG_PMPS_SHIFTNO));
1800 
1801 		if (s2 == value_s)
1802 			value_t = (value_t - t2);
1803 		else {
1804 			if (--dbits_to_go < 0)
1805 				get_byte();
1806 
1807 			value_t = (value_t << 1)
1808 					| ((dbuffer >> dbits_to_go) & 0x01);
1809 			value_t = 256 + value_t - t2;
1810 		}
1811 
1812 		while (t_rlps < QUARTER) {
1813 			t_rlps = t_rlps << 1;
1814 			if (--dbits_to_go < 0)
1815 				get_byte();
1816 
1817 			value_t = (value_t << 1)
1818 					| ((dbuffer >> dbits_to_go) & 0x01);
1819 		}
1820 		s1 = 0;
1821 		t1 = t_rlps & 0xff;
1822 
1823 		value_s = 0;
1824 		while (value_t < QUARTER) {
1825 			int j;
1826 
1827 			if (--dbits_to_go < 0)
1828 				get_byte();
1829 			j = (dbuffer >> dbits_to_go) & 0x01;
1830 
1831 			value_t = (value_t << 1) | j;
1832 			value_s++;
1833 		}
1834 		value_t = value_t & 0xff;
1835 	} else {
1836 		s1 = s2;
1837 		t1 = t2;
1838 	}
1839 
1840 	if (bit != bit1) {
1841 		cycno1 = (cycno1 <= 2) ? (cycno1 + 1) : 3;
1842 	} else {
1843 		if (cycno1 == 0)
1844 			cycno1 = 1;
1845 	}
1846 
1847 	if (bit != bit2) {
1848 		cycno2 = (cycno2 <= 2) ? (cycno2 + 1) : 3;
1849 	} else {
1850 		if (cycno2 == 0)
1851 			cycno2 = 1;
1852 	}
1853 	bi_ct1->cycno = cycno1;
1854 	bi_ct2->cycno = cycno2;
1855 
1856 	{
1857 
1858 		if (bit == bit1) {
1859 			lg_pmps1 =
1860 					lg_pmps1
1861 				- (unsigned int)(lg_pmps1
1862 					>> cwr1)
1863 				- (unsigned int)(lg_pmps1
1864 					>> (cwr1
1865 					+ 2));
1866 		} else {
1867 			switch (cwr1) {
1868 			case 3:
1869 				lg_pmps1 = lg_pmps1 + 197;
1870 				break;
1871 			case 4:
1872 				lg_pmps1 = lg_pmps1 + 95;
1873 				break;
1874 			default:
1875 				lg_pmps1 = lg_pmps1 + 46;
1876 			}
1877 
1878 			if (lg_pmps1 >= (256 << LG_PMPS_SHIFTNO)) {
1879 				lg_pmps1 = (512 << LG_PMPS_SHIFTNO) - 1
1880 						- lg_pmps1;
1881 				bi_ct1->MPS = !(bi_ct1->MPS);
1882 			}
1883 		}
1884 		bi_ct1->LG_PMPS = lg_pmps1;
1885 
1886 		if (bit == bit2) {
1887 			lg_pmps2 =
1888 					lg_pmps2
1889 				- (unsigned int)(lg_pmps2
1890 				>> cwr2)
1891 				- (unsigned int)(lg_pmps2
1892 				>> (cwr2
1893 				+ 2));
1894 		} else {
1895 			switch (cwr2) {
1896 			case 3:
1897 				lg_pmps2 = lg_pmps2 + 197;
1898 				break;
1899 			case 4:
1900 				lg_pmps2 = lg_pmps2 + 95;
1901 				break;
1902 			default:
1903 				lg_pmps2 = lg_pmps2 + 46;
1904 			}
1905 
1906 			if (lg_pmps2 >= (256 << LG_PMPS_SHIFTNO)) {
1907 				lg_pmps2 = (512 << LG_PMPS_SHIFTNO) - 1
1908 						- lg_pmps2;
1909 				bi_ct2->MPS = !(bi_ct2->MPS);
1910 			}
1911 		}
1912 		bi_ct2->LG_PMPS = lg_pmps2;
1913 	}
1914 
1915 
1916 	return bit;
1917 }
1918 
1919 /*!
1920  ************************************************************************
1921  * \brief
1922  *    biari_decode_symbol_eq_prob():
1923  * \return
1924  *    the decoded symbol
1925  ************************************************************************
1926  */
biari_decode_symbol_eq_prob(struct decoding_environment_s * dep)1927 unsigned int biari_decode_symbol_eq_prob(struct decoding_environment_s *dep)
1928 {
1929 	unsigned char bit;
1930 	struct bi_context_type_s octx;
1931 	struct bi_context_type_s *ctx = &octx;
1932 
1933 	ctx->LG_PMPS = (QUARTER << LG_PMPS_SHIFTNO) - 1;
1934 	ctx->MPS = 0;
1935 	ctx->cycno = 0xfe;
1936 	dec_bypass = 1;
1937 	bit = biari_decode_symbol(dep, ctx);
1938 	dec_bypass = 0;
1939 	return bit;
1940 }
1941 
biari_decode_final(struct decoding_environment_s * dep)1942 unsigned int biari_decode_final(struct decoding_environment_s *dep)
1943 {
1944 	unsigned char bit;
1945 	struct bi_context_type_s octx;
1946 	struct bi_context_type_s *ctx = &octx;
1947 
1948 	ctx->LG_PMPS = 1 << LG_PMPS_SHIFTNO;
1949 	ctx->MPS = 0;
1950 	ctx->cycno = 0xff;
1951 	dec_final = 1;
1952 	bit = biari_decode_symbol(dep, ctx);
1953 	dec_final = 0;
1954 	return bit;
1955 }
1956 
i_8(char * tracestring)1957 int i_8(char *tracestring)
1958 {
1959 	int frame_bitoffset = curr_stream->frame_bitoffset;
1960 	unsigned char *buf = curr_stream->stream_buffer;
1961 	int bitstreamlengthinbytes = curr_stream->bitstream_length;
1962 	struct syntaxelement symbol, *sym = &symbol;
1963 #ifdef AVSP_LONG_CABAC
1964 #else
1965 	assert(curr_stream->stream_buffer != NULL);
1966 #endif
1967 
1968 	sym->len = 8;
1969 	sym->type = SE_HEADER;
1970 	sym->mapping = linfo_ue;
1971 
1972 	if ((get_bits(buf, frame_bitoffset, &(sym->inf), bitstreamlengthinbytes,
1973 			sym->len)) < 0)
1974 		return -1;
1975 	curr_stream->frame_bitoffset += sym->len;
1976 	sym->value1 = sym->inf;
1977 	if (sym->inf & 0x80)
1978 		sym->inf = -(~((int)0xffffff00 | sym->inf) + 1);
1979 #if TRACE
1980 	tracebits2(sym->tracestring, sym->len, sym->inf);
1981 #endif
1982 	return sym->inf;
1983 }
1984 
1985 /*!
1986  ************************************************************************
1987  * \brief
1988  *    arideco_bits_read
1989  ************************************************************************
1990  */
arideco_bits_read(struct decoding_environment_s * dep)1991 int arideco_bits_read(struct decoding_environment_s *dep)
1992 {
1993 
1994 	return 8 * ((*dcodestrm_len) - 1) + (8 - dbits_to_go);
1995 }
1996 
1997 /*!
1998  ************************************************************************
1999  * \brief
2000  *    arithmetic decoding
2001  ************************************************************************
2002  */
read_syntaxelement_aec(struct syntaxelement * se,struct img_par * img,struct datapartition * this_data_part)2003 int read_syntaxelement_aec(struct syntaxelement *se, struct img_par *img,
2004 		struct datapartition *this_data_part)
2005 {
2006 	int curr_len;
2007 	struct decoding_environment_s *dep_dp = &(this_data_part->de_aec);
2008 
2009 	curr_len = arideco_bits_read(dep_dp);
2010 
2011 	se->reading(se, img, dep_dp);
2012 
2013 	se->len = (arideco_bits_read(dep_dp) - curr_len);
2014 	return se->len;
2015 }
2016 
2017 /*!
2018  ************************************************************************
2019  * \brief
2020  *    This function is used to arithmetically decode the
2021  *    run length info of the skip mb
2022  ************************************************************************
2023  */
readrunlenghtfrombuffer_aec(struct syntaxelement * se,struct img_par * img,struct decoding_environment_s * dep_dp)2024 void readrunlenghtfrombuffer_aec(struct syntaxelement *se, struct img_par *img,
2025 		struct decoding_environment_s *dep_dp)
2026 {
2027 	struct bi_context_type_s *pctx;
2028 	int ctx, symbol;
2029 
2030 	pctx = img->current_slice->tex_ctx->one_contexts[0];
2031 	symbol = 0;
2032 	ctx = 0;
2033 	while (biari_decode_symbol(dep_dp, pctx + ctx) == 0) {
2034 		symbol += 1;
2035 		ctx++;
2036 		if (ctx >= 3)
2037 			ctx = 3;
2038 	}
2039 	se->value1 = symbol;
2040 #if TRACE
2041 	fprintf(p_trace, "@%d%s\t\t\t%d\n",
2042 		symbol_count++, se->tracestring, se->value1);
2043 	fflush(p_trace);
2044 #endif
2045 }
2046 
2047 /*!
2048  ************************************************************************
2049  * \brief
2050  *    This function is used to arithmetically decode a pair of
2051  *    intra prediction modes of a given MB.
2052  ************************************************************************
2053  */
2054 int mapd_intrap[5] = {0, 2, 3, 4, 1};
read_intrapredmode_aec(struct syntaxelement * se,struct img_par * img,struct decoding_environment_s * dep_dp)2055 void read_intrapredmode_aec(struct syntaxelement *se, struct img_par *img,
2056 		struct decoding_environment_s *dep_dp)
2057 {
2058 	struct bi_context_type_s *pctx;
2059 	int ctx, symbol;
2060 
2061 	pctx = img->current_slice->tex_ctx->one_contexts[1];
2062 	symbol = 0;
2063 	ctx = 0;
2064 #ifdef DUMP_DEBUG
2065 	if (avs_get_debug_flag() & AEC_DUMP)
2066 		io_printf(" -- read_intrapredmode_aec ctx : %d\n", ctx);
2067 #endif
2068 	while (biari_decode_symbol(dep_dp, pctx + ctx) == 0) {
2069 		symbol += 1;
2070 		ctx++;
2071 		if (ctx >= 3)
2072 			ctx = 3;
2073 #ifdef DUMP_DEBUG
2074 		if (avs_get_debug_flag() & AEC_DUMP)
2075 			io_printf(" -- read_intrapredmode_aec ctx : %d\n", ctx);
2076 #endif
2077 		if (symbol == 4)
2078 			break;
2079 		}
2080 	se->value1 = mapd_intrap[symbol] - 1;
2081 
2082 #if TRACE
2083 	fprintf(p_trace, "@%d %s\t\t\t%d\n",
2084 		symbol_count++, se->tracestring, se->value1);
2085 	fflush(p_trace);
2086 #endif
2087 }
2088 
2089 /*!
2090  ************************************************************************
2091  * \brief
2092  *    decoding of unary binarization using one or 2 distinct
2093  *    models for the first and all remaining bins; no terminating
2094  *    "0" for max_symbol
2095  ***********************************************************************
2096  */
unary_bin_max_decode(struct decoding_environment_s * dep_dp,struct bi_context_type_s * ctx,int ctx_offset,unsigned int max_symbol)2097 unsigned int unary_bin_max_decode(struct decoding_environment_s *dep_dp,
2098 		struct bi_context_type_s *ctx,
2099 		int ctx_offset, unsigned int max_symbol)
2100 {
2101 	unsigned int l;
2102 	unsigned int symbol;
2103 	struct bi_context_type_s *ictx;
2104 
2105 	symbol = biari_decode_symbol(dep_dp, ctx);
2106 
2107 	if (symbol == 0)
2108 		return 0;
2109 
2110 	if (max_symbol == 1)
2111 		return symbol;
2112 	symbol = 0;
2113 	ictx = ctx + ctx_offset;
2114 	do {
2115 		l = biari_decode_symbol(dep_dp, ictx);
2116 		symbol++;
2117 	} while ((l != 0) && (symbol < max_symbol - 1));
2118 	if ((l != 0) && (symbol == max_symbol - 1))
2119 		symbol++;
2120 	return symbol;
2121 }
2122 
2123 /*!
2124  ************************************************************************
2125  * \brief
2126  *    decoding of unary binarization using one or 2 distinct
2127  *    models for the first and all remaining bins
2128  ***********************************************************************
2129  */
unary_bin_decode(struct decoding_environment_s * dep_dp,struct bi_context_type_s * ctx,int ctx_offset)2130 unsigned int unary_bin_decode(struct decoding_environment_s *dep_dp,
2131 		struct bi_context_type_s *ctx, int ctx_offset)
2132 {
2133 	unsigned int l;
2134 	unsigned int symbol;
2135 	struct bi_context_type_s *ictx;
2136 
2137 	symbol = 1 - biari_decode_symbol(dep_dp, ctx);
2138 
2139 	if (symbol == 0)
2140 		return 0;
2141 	symbol = 0;
2142 	ictx = ctx + ctx_offset;
2143 	do {
2144 		l = 1 - biari_decode_symbol(dep_dp, ictx);
2145 		symbol++;
2146 	} while (l != 0);
2147 	return symbol;
2148 }
2149 
2150 /*!
2151  ************************************************************************
2152  * \brief
2153  *    This function is used to arithmetically decode the chroma
2154  *    intra prediction mode of a given MB.
2155  ************************************************************************
2156  */
read_cipredmode_aec(struct syntaxelement * se,struct img_par * img,struct decoding_environment_s * dep_dp)2157 void read_cipredmode_aec(struct syntaxelement *se,
2158 	struct img_par *img,
2159 		struct decoding_environment_s *dep_dp)
2160 {
2161 	struct texture_info_contexts *ctx = img->current_slice->tex_ctx;
2162 	struct macroblock *curr_mb = &mb_data[img->current_mb_nr];
2163 	int act_ctx, a, b;
2164 	int act_sym = se->value1;
2165 
2166 	if (curr_mb->mb_available_up == NULL)
2167 		b = 0;
2168 	else {
2169 		/*if ( (curr_mb->mb_available_up)->mb_type==IPCM)
2170 		 * b=0;
2171 		 * else
2172 		 */
2173 		b = (((curr_mb->mb_available_up)->c_ipred_mode != 0) ? 1 : 0);
2174 	}
2175 
2176 	if (curr_mb->mb_available_left == NULL)
2177 		a = 0;
2178 	else {
2179 		/* if ( (curr_mb->mb_available_left)->mb_type==IPCM)
2180 		 * a=0;
2181 		 * else
2182 		 */
2183 		a = (((curr_mb->mb_available_left)->c_ipred_mode != 0) ? 1 : 0);
2184 	}
2185 
2186 	act_ctx = a + b;
2187 
2188 
2189 	act_sym = biari_decode_symbol(dep_dp, ctx->cipr_contexts + act_ctx);
2190 
2191 	if (act_sym != 0)
2192 		act_sym = unary_bin_max_decode(dep_dp, ctx->cipr_contexts + 3,
2193 				0, 2) + 1;
2194 
2195 	se->value1 = act_sym;
2196 
2197 #if TRACE
2198 	fprintf(p_trace, "@%d %s\t\t%d\n",
2199 		symbol_count++, se->tracestring, se->value1);
2200 	fflush(p_trace);
2201 #endif
2202 
2203 }
2204 
slice_header(char * buf,int startcodepos,int length)2205 int slice_header(char *buf, int startcodepos, int length)
2206 {
2207 	int i;
2208 
2209 	int weight_para_num = 0;
2210 	int mb_row;
2211 	int mb_column;
2212 	int mb_index;
2213 	int mb_width, mb_height;
2214 
2215 	mb_column = 0;
2216 
2217 	memcpy(curr_stream->stream_buffer, buf, length);
2218 	curr_stream->code_len = curr_stream->bitstream_length = length;
2219 
2220 	curr_stream->read_len =
2221 	curr_stream->frame_bitoffset = (startcodepos) * 8;
2222 	slice_vertical_position = u_v(8, "slice vertical position");
2223 
2224 	push_es(slice_vertical_position, 8);
2225 
2226 #ifdef DUMP_DEBUG
2227 	if (avs_get_debug_flag() & SLICE_INFO_DUMP)
2228 		io_printf(" * 8-bits slice_vertical_position : %d\n",
2229 			slice_vertical_position);
2230 #endif
2231 
2232 	if (vertical_size > 2800) {
2233 		slice_vertical_position_extension = u_v(3,
2234 				"slice vertical position extension");
2235 		push_es(slice_vertical_position_extension, 3);
2236 
2237 	}
2238 
2239 	if (vertical_size > 2800)
2240 		mb_row = (slice_vertical_position_extension << 7)
2241 				+ slice_vertical_position;
2242 	else
2243 		mb_row = slice_vertical_position;
2244 
2245 	mb_width = (horizontal_size + 15) / 16;
2246 	if (!progressive_sequence)
2247 		mb_height = 2 * ((vertical_size + 31) / 32);
2248 	else
2249 		mb_height = (vertical_size + 15) / 16;
2250 
2251 
2252 	mb_index = mb_row * mb_width + mb_column;
2253 
2254 	if (!img->picture_structure && img->type == I_IMG
2255 			&& (mb_index >= mb_width * mb_height / 2)) {
2256 		second_ifield = 1;
2257 		img->type = P_IMG;
2258 		pre_img_type = P_IMG;
2259 	}
2260 
2261 	{
2262 		if (!fixed_picture_qp) {
2263 			fixed_slice_qp = u_v(1, "fixed_slice_qp");
2264 			push_es(fixed_slice_qp, 1);
2265 #ifdef DUMP_DEBUG
2266 			if (avs_get_debug_flag() & SLICE_INFO_DUMP)
2267 				io_printf(" * 1-bit fixed_slice_qp : %d\n",
2268 					fixed_slice_qp);
2269 #endif
2270 			slice_qp = u_v(6, "slice_qp");
2271 			push_es(slice_qp, 6);
2272 #ifdef DUMP_DEBUG
2273 			if (avs_get_debug_flag() & SLICE_INFO_DUMP)
2274 				io_printf(" * 6-bits slice_qp : %d\n",
2275 					slice_qp);
2276 #endif
2277 
2278 			img->qp = slice_qp;
2279 		}
2280 
2281 		if (img->type != I_IMG) {
2282 			img->slice_weighting_flag = u_v(1,
2283 					"slice weighting flag");
2284 
2285 			if (img->slice_weighting_flag) {
2286 
2287 				if (second_ifield && !img->picture_structure)
2288 					weight_para_num = 1;
2289 				else if (img->type == P_IMG
2290 						&& img->picture_structure)
2291 					weight_para_num = 2;
2292 				else if (img->type == P_IMG
2293 						&& !img->picture_structure)
2294 					weight_para_num = 4;
2295 				else if (img->type == B_IMG
2296 						&& img->picture_structure)
2297 					weight_para_num = 2;
2298 				else if (img->type == B_IMG
2299 						&& !img->picture_structure)
2300 					weight_para_num = 4;
2301 
2302 #ifdef DUMP_DEBUG
2303 				if (avs_get_debug_flag() & SLICE_INFO_DUMP)
2304 					io_printf(" - weight_para_num : %d\n",
2305 						weight_para_num);
2306 #endif
2307 				for (i = 0; i < weight_para_num; i++) {
2308 					img->lum_scale[i] = u_v(8,
2309 							"luma scale");
2310 
2311 					img->lum_shift[i] = i_8("luma shift");
2312 
2313 					marker_bit = u_1("insert bit");
2314 
2315 
2316 					{
2317 						img->chroma_scale[i] = u_v(8,
2318 								"chroma scale");
2319 
2320 						img->chroma_shift[i] = i_8(
2321 								"chroma shift");
2322 
2323 						marker_bit = u_1("insert bit");
2324 
2325 					}
2326 				}
2327 				img->mb_weighting_flag = u_v(1,
2328 						"MB weighting flag");
2329 
2330 			}
2331 		}
2332 	}
2333 
2334 
2335 #if 1
2336 	return mb_index;
2337 #endif
2338 }
2339 
no_mem_exit(char * where)2340 void no_mem_exit(char *where)
2341 {
2342 	io_printf("%s\r\n", where);
2343 }
2344 
2345 unsigned char bit[8] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
2346 
2347 struct inputstream_s {
2348 	/*FILE *f;*/
2349 	unsigned char buf[SVA_STREAM_BUF_SIZE];
2350 	unsigned int uclear_bits;
2351 	unsigned int upre_3bytes;
2352 	int ibyte_position;
2353 	int ibuf_bytesnum;
2354 	int iclear_bitsnum;
2355 	int istuff_bitsnum;
2356 	int ibits_count;
2357 };
2358 
2359 struct inputstream_s IRABS;
2360 struct inputstream_s *p_irabs = &IRABS;
2361 
2362 struct stat_bits {
2363 	int curr_frame_bits;
2364 	int prev_frame_bits;
2365 	int emulate_bits;
2366 	int prev_emulate_bits;
2367 	int last_unit_bits;
2368 	int bitrate;
2369 	int total_bitrate[1000];
2370 	int coded_pic_num;
2371 	int time_s;
2372 };
2373 
2374 struct stat_bits *stat_bits_ptr;
2375 
2376 unsigned char *temp_slice_buf;
2377 int start_codeposition;
2378 int first_slice_length;
2379 int first_slice_startpos;
2380 
2381 int bitstream_buf_used;
2382 int startcode_offset;
2383 
2384 int bitstream_read_ptr;
2385 
2386 int demulate_enable;
2387 
2388 int last_dquant;
2389 
2390 int total_mb_count;
2391 
2392 int current_mb_skip;
2393 
2394 int skip_mode_flag;
2395 
2396 int current_mb_intra;
2397 
2398 /*
2399  *************************************************************************
2400  * Function: Check start code's type
2401  * Input:
2402  * Output:
2403  * Return:
2404  * Author: XZHENG, 20080515
2405  *************************************************************************
2406  */
check_type(int startcode)2407 void check_type(int startcode)
2408 {
2409 	startcode = startcode & 0x000000ff;
2410 	switch (startcode) {
2411 	case 0xb0:
2412 	case 0xb2:
2413 	case 0xb5:
2414 		demulate_enable = 0;
2415 		break;
2416 	default:
2417 		demulate_enable = 1;
2418 		break;
2419 	}
2420 
2421 }
2422 /*
2423  *************************************************************************
2424  * Function:
2425  * Input:
2426  * Output:
2427  * Return:  0 : OK
2428  -1 : arrive at stream end
2429  -2 : meet another start code
2430  * Attention:
2431  *************************************************************************
2432  */
clear_nextbyte(struct inputstream_s * p)2433 int clear_nextbyte(struct inputstream_s *p)
2434 {
2435 	int i, k, j;
2436 	unsigned char temp[3];
2437 
2438 	i = p->ibyte_position;
2439 	k = p->ibuf_bytesnum - i;
2440 	if (k < 3) {
2441 		for (j = 0; j < k; j++)
2442 			temp[j] = p->buf[i + j];
2443 
2444 		p->ibuf_bytesnum = read_bitstream(p->buf + k,
2445 				SVA_STREAM_BUF_SIZE - k);
2446 		bitstream_buf_used++;
2447 		if (p->ibuf_bytesnum == 0) {
2448 			if (k > 0) {
2449 				while (k > 0) {
2450 					p->upre_3bytes = ((p->upre_3bytes << 8)
2451 							| p->buf[i])
2452 							& 0x00ffffff;
2453 					if (p->upre_3bytes < 4
2454 							&& demulate_enable) {
2455 						p->uclear_bits =
2456 						(p->uclear_bits
2457 						<< 6)
2458 						| (p->buf[i]
2459 						>> 2);
2460 						p->iclear_bitsnum += 6;
2461 						stat_bits_ptr->emulate_bits
2462 						+= 2;
2463 					} else {
2464 						p->uclear_bits = (p->uclear_bits
2465 						<< 8)
2466 						| p->buf[i];
2467 						p->iclear_bitsnum += 8;
2468 					}
2469 					p->ibyte_position++;
2470 					k--;
2471 					i++;
2472 				}
2473 				return 0;
2474 			} else {
2475 				return -1;
2476 			}
2477 		} else {
2478 			for (j = 0; j < k; j++)
2479 				p->buf[j] = temp[j];
2480 			p->ibuf_bytesnum += k;
2481 			i = p->ibyte_position = 0;
2482 		}
2483 	}
2484 	if (p->buf[i] == 0 && p->buf[i + 1] == 0 && p->buf[i + 2] == 1)
2485 		return -2;
2486 	p->upre_3bytes = ((p->upre_3bytes << 8) | p->buf[i]) & 0x00ffffff;
2487 	if (p->upre_3bytes < 4 && demulate_enable) {
2488 		p->uclear_bits = (p->uclear_bits << 6) | (p->buf[i] >> 2);
2489 		p->iclear_bitsnum += 6;
2490 		stat_bits_ptr->emulate_bits += 2;
2491 	} else {
2492 		p->uclear_bits = (p->uclear_bits << 8) | p->buf[i];
2493 		p->iclear_bitsnum += 8;
2494 	}
2495 	p->ibyte_position++;
2496 	return 0;
2497 }
2498 
2499 /*
2500  *************************************************************************
2501  * Function:
2502  * Input:
2503  * Output:
2504  * Return:  0 : OK
2505  -1 : arrive at stream end
2506  -2 : meet another start code
2507  * Attention:
2508  *************************************************************************
2509  */
read_n_bit(struct inputstream_s * p,int n,int * v)2510 int read_n_bit(struct inputstream_s *p, int n, int *v)
2511 {
2512 	int r;
2513 	unsigned int t;
2514 
2515 	while (n > p->iclear_bitsnum) {
2516 		r = clear_nextbyte(p);
2517 		if (r) {
2518 			if (r == -1) {
2519 				if (p->ibuf_bytesnum - p->ibyte_position > 0)
2520 					break;
2521 			}
2522 			return r;
2523 		}
2524 	}
2525 	t = p->uclear_bits;
2526 	r = 32 - p->iclear_bitsnum;
2527 	*v = (t << r) >> (32 - n);
2528 	p->iclear_bitsnum -= n;
2529 	return 0;
2530 }
2531 
2532 #ifdef AVSP_LONG_CABAC
2533 unsigned char TMP_BUF[2 * SVA_STREAM_BUF_SIZE];
2534 int tmp_buf_wr_ptr;
2535 int tmp_buf_rd_ptr;
2536 int tmp_buf_count;
2537 #endif
open_irabs(struct inputstream_s * p)2538 void open_irabs(struct inputstream_s *p)
2539 {
2540 	p->uclear_bits = 0xffffffff;
2541 	p->ibyte_position = 0;
2542 	p->ibuf_bytesnum = 0;
2543 	p->iclear_bitsnum = 0;
2544 	p->istuff_bitsnum = 0;
2545 	p->ibits_count = 0;
2546 	p->upre_3bytes = 0;
2547 
2548 	bitstream_buf_used = 0;
2549 	bitstream_read_ptr = (src_start - 16) & 0xfffffff0;
2550 
2551 #ifdef AVSP_LONG_CABAC
2552 	tmp_buf_count = 0;
2553 	tmp_buf_wr_ptr = 0;
2554 	tmp_buf_rd_ptr = 0;
2555 #endif
2556 
2557 }
2558 
move_bitstream(unsigned int move_from_addr,unsigned int move_to_addr,int move_size)2559 void move_bitstream(unsigned int move_from_addr, unsigned int move_to_addr,
2560 		int move_size)
2561 {
2562 	int move_bytes_left = move_size;
2563 	unsigned int move_read_addr;
2564 	unsigned int move_write_addr = move_to_addr;
2565 
2566 	int move_byte;
2567 	unsigned int data32;
2568 
2569 	while (move_from_addr > vld_mem_end_addr) {
2570 		move_from_addr = move_from_addr + vld_mem_start_addr
2571 				- vld_mem_end_addr - 8;
2572 	}
2573 	move_read_addr = move_from_addr;
2574 	while (move_bytes_left > 0) {
2575 		move_byte = move_bytes_left;
2576 		if (move_byte > 512)
2577 			move_byte = 512;
2578 		if ((move_read_addr + move_byte) > vld_mem_end_addr)
2579 			move_byte = (vld_mem_end_addr + 8) - move_read_addr;
2580 
2581 		WRITE_VREG(LMEM_DMA_ADR, move_read_addr);
2582 		WRITE_VREG(LMEM_DMA_COUNT, move_byte / 2);
2583 		WRITE_VREG(LMEM_DMA_CTRL, 0xc200);
2584 
2585 		data32 = 0x8000;
2586 		while (data32 & 0x8000)
2587 			data32 = READ_VREG(LMEM_DMA_CTRL);
2588 
2589 		WRITE_VREG(LMEM_DMA_ADR, move_write_addr);
2590 		WRITE_VREG(LMEM_DMA_COUNT, move_byte / 2);
2591 		WRITE_VREG(LMEM_DMA_CTRL, 0x8200);
2592 
2593 		data32 = 0x8000;
2594 		while (data32 & 0x8000)
2595 			data32 = READ_VREG(LMEM_DMA_CTRL);
2596 
2597 		data32 = 0x0fff;
2598 		while (data32 & 0x0fff)
2599 			data32 = READ_VREG(WRRSP_LMEM);
2600 
2601 #ifdef DUMP_DEBUG
2602 		if (avs_get_debug_flag() & STREAM_INFO_DUMP)
2603 			io_printf("  2 MOVE %d Bytes from 0x%x to 0x%x\n",
2604 				move_byte, move_read_addr, move_write_addr);
2605 #endif
2606 
2607 		move_read_addr = move_read_addr + move_byte;
2608 		if (move_read_addr > vld_mem_end_addr)
2609 			move_read_addr = vld_mem_start_addr;
2610 		move_write_addr = move_write_addr + move_byte;
2611 		move_bytes_left = move_bytes_left - move_byte;
2612 	}
2613 
2614 }
2615 
read_bitstream(unsigned char * buf,int size)2616 int read_bitstream(unsigned char *buf, int size)
2617 {
2618 	int i;
2619 
2620 #ifdef AVSP_LONG_CABAC
2621 
2622 	unsigned int *TMP_BUF_32 = (unsigned int *)bitstream_read_tmp;
2623 
2624 	if (tmp_buf_count < size) {
2625 		dma_sync_single_for_cpu(amports_get_dma_device(),
2626 				bitstream_read_tmp_phy, SVA_STREAM_BUF_SIZE,
2627 				DMA_FROM_DEVICE);
2628 
2629 		move_bitstream(bitstream_read_ptr, bitstream_read_tmp_phy,
2630 				SVA_STREAM_BUF_SIZE);
2631 
2632 		for (i = 0; i < SVA_STREAM_BUF_SIZE / 8; i++) {
2633 			TMP_BUF[tmp_buf_wr_ptr++] =
2634 					(TMP_BUF_32[2 * i + 1] >> 24) & 0xff;
2635 			if (tmp_buf_wr_ptr >= (2 * SVA_STREAM_BUF_SIZE))
2636 				tmp_buf_wr_ptr = 0;
2637 			TMP_BUF[tmp_buf_wr_ptr++] =
2638 					(TMP_BUF_32[2 * i + 1] >> 16) & 0xff;
2639 			if (tmp_buf_wr_ptr >= (2 * SVA_STREAM_BUF_SIZE))
2640 				tmp_buf_wr_ptr = 0;
2641 			TMP_BUF[tmp_buf_wr_ptr++] = (TMP_BUF_32[2 * i + 1] >> 8)
2642 					& 0xff;
2643 			if (tmp_buf_wr_ptr >= (2 * SVA_STREAM_BUF_SIZE))
2644 				tmp_buf_wr_ptr = 0;
2645 			TMP_BUF[tmp_buf_wr_ptr++] = (TMP_BUF_32[2 * i + 1] >> 0)
2646 					& 0xff;
2647 			if (tmp_buf_wr_ptr >= (2 * SVA_STREAM_BUF_SIZE))
2648 				tmp_buf_wr_ptr = 0;
2649 			TMP_BUF[tmp_buf_wr_ptr++] =
2650 					(TMP_BUF_32[2 * i + 0] >> 24) & 0xff;
2651 			if (tmp_buf_wr_ptr >= (2 * SVA_STREAM_BUF_SIZE))
2652 				tmp_buf_wr_ptr = 0;
2653 			TMP_BUF[tmp_buf_wr_ptr++] =
2654 					(TMP_BUF_32[2 * i + 0] >> 16) & 0xff;
2655 			if (tmp_buf_wr_ptr >= (2 * SVA_STREAM_BUF_SIZE))
2656 				tmp_buf_wr_ptr = 0;
2657 			TMP_BUF[tmp_buf_wr_ptr++] = (TMP_BUF_32[2 * i + 0] >> 8)
2658 					& 0xff;
2659 			if (tmp_buf_wr_ptr >= (2 * SVA_STREAM_BUF_SIZE))
2660 				tmp_buf_wr_ptr = 0;
2661 			TMP_BUF[tmp_buf_wr_ptr++] = (TMP_BUF_32[2 * i + 0] >> 0)
2662 					& 0xff;
2663 			if (tmp_buf_wr_ptr >= (2 * SVA_STREAM_BUF_SIZE))
2664 				tmp_buf_wr_ptr = 0;
2665 		}
2666 		tmp_buf_count = tmp_buf_count + SVA_STREAM_BUF_SIZE;
2667 		bitstream_read_ptr = bitstream_read_ptr + SVA_STREAM_BUF_SIZE;
2668 	}
2669 
2670 #ifdef DUMP_DEBUG
2671 	if (avs_get_debug_flag() & STREAM_INFO_DUMP)
2672 		io_printf(" Read %d bytes from %d, size left : %d\n",
2673 			size, tmp_buf_rd_ptr, tmp_buf_count);
2674 #endif
2675 	for (i = 0; i < size; i++) {
2676 		buf[i] = TMP_BUF[tmp_buf_rd_ptr++];
2677 		if (tmp_buf_rd_ptr >= (2 * SVA_STREAM_BUF_SIZE))
2678 			tmp_buf_rd_ptr = 0;
2679 	}
2680 	tmp_buf_count = tmp_buf_count - size;
2681 
2682 #else
2683 	for (i = 0; i < size; i++)
2684 		buf[i] = tmp_stream[bitstream_read_ptr + i];
2685 	bitstream_read_ptr = bitstream_read_ptr + size;
2686 #endif
2687 
2688 	return size;
2689 }
2690 
next_startcode(struct inputstream_s * p)2691 int next_startcode(struct inputstream_s *p)
2692 {
2693 	int i, m;
2694 	unsigned char a = 0, b = 0;
2695 
2696 	m = 0;
2697 
2698 	while (1) {
2699 		if (p->ibyte_position >= p->ibuf_bytesnum - 2) {
2700 			m = p->ibuf_bytesnum - p->ibyte_position;
2701 			if (m < 0)
2702 				return -2;
2703 			if (m == 1)
2704 				b = p->buf[p->ibyte_position + 1];
2705 			if (m == 2) {
2706 				b = p->buf[p->ibyte_position + 1];
2707 				a = p->buf[p->ibyte_position];
2708 			}
2709 			p->ibuf_bytesnum = read_bitstream(p->buf,
2710 					SVA_STREAM_BUF_SIZE);
2711 			p->ibyte_position = 0;
2712 			bitstream_buf_used++;
2713 		}
2714 
2715 		if (p->ibuf_bytesnum + m < 3)
2716 			return -1;
2717 
2718 		if (m == 1 && b == 0 && p->buf[0] == 0 && p->buf[1] == 1) {
2719 			p->ibyte_position = 2;
2720 			p->iclear_bitsnum = 0;
2721 			p->istuff_bitsnum = 0;
2722 			p->ibits_count += 24;
2723 			p->upre_3bytes = 1;
2724 			return 0;
2725 		}
2726 
2727 		if (m == 2 && b == 0 && a == 0 && p->buf[0] == 1) {
2728 			p->ibyte_position = 1;
2729 			p->iclear_bitsnum = 0;
2730 			p->istuff_bitsnum = 0;
2731 			p->ibits_count += 24;
2732 			p->upre_3bytes = 1;
2733 			return 0;
2734 		}
2735 
2736 		if (m == 2 && b == 0 && p->buf[0] == 0 && p->buf[1] == 1) {
2737 			p->ibyte_position = 2;
2738 			p->iclear_bitsnum = 0;
2739 			p->istuff_bitsnum = 0;
2740 			p->ibits_count += 24;
2741 			p->upre_3bytes = 1;
2742 			return 0;
2743 		}
2744 
2745 		for (i = p->ibyte_position; i < p->ibuf_bytesnum - 2; i++) {
2746 			if (p->buf[i] == 0 && p->buf[i + 1] == 0
2747 					&& p->buf[i + 2] == 1) {
2748 				p->ibyte_position = i + 3;
2749 				p->iclear_bitsnum = 0;
2750 				p->istuff_bitsnum = 0;
2751 				p->ibits_count += 24;
2752 				p->upre_3bytes = 1;
2753 				return 0;
2754 			}
2755 			p->ibits_count += 8;
2756 		}
2757 		p->ibyte_position = i;
2758 	}
2759 }
2760 
get_oneunit(char * buf,int * startcodepos,int * length)2761 int get_oneunit(char *buf, int *startcodepos, int *length)
2762 {
2763 	int i, j, k;
2764 
2765 	i = next_startcode(p_irabs);
2766 
2767 	if (i != 0) {
2768 		if (i == -1)
2769 			io_printf(
2770 					"\narrive at stream end and start code is not found!");
2771 		if (i == -2)
2772 			io_printf("\np->ibyte_position error!");
2773 
2774 	}
2775 	startcode_offset =
2776 			p_irabs->ibyte_position
2777 					- 3 + (bitstream_buf_used-1)
2778 					* SVA_STREAM_BUF_SIZE;
2779 	buf[0] = 0;
2780 	buf[1] = 0;
2781 	buf[2] = 1;
2782 	*startcodepos = 3;
2783 	i = read_n_bit(p_irabs, 8, &j);
2784 	buf[3] = (char)j;
2785 
2786 	check_type(buf[3]);
2787 	if (buf[3] == SEQUENCE_END_CODE) {
2788 		*length = 4;
2789 		return -1;
2790 	}
2791 	k = 4;
2792 	while (1) {
2793 		i = read_n_bit(p_irabs, 8, &j);
2794 		if (i < 0)
2795 			break;
2796 		buf[k++] = (char)j;
2797 		if (k >= (MAX_CODED_FRAME_SIZE - 1))
2798 			break;
2799 	}
2800 	if (p_irabs->iclear_bitsnum > 0) {
2801 		int shift;
2802 
2803 		shift = 8 - p_irabs->iclear_bitsnum;
2804 		i = read_n_bit(p_irabs, p_irabs->iclear_bitsnum, &j);
2805 
2806 		if (j != 0)
2807 			buf[k++] = (char)(j << shift);
2808 		stat_bits_ptr->last_unit_bits += shift;
2809 	}
2810 	*length = k;
2811 	return k;
2812 }
2813 
2814 /*unsigned char tmp_buf[MAX_CODED_FRAME_SIZE] __attribute__ ((aligned(64)));*/
2815 /*unsigned char tmp_buf[MAX_CODED_FRAME_SIZE] __aligned(64);*/
header(void)2816 int header(void)
2817 {
2818 	unsigned char *buf;
2819 	int startcodepos, length;
2820 
2821 	unsigned char *tmp_buf;
2822 
2823 	tmp_buf = (unsigned char *)avsp_heap_adr;
2824 
2825 	buf = &tmp_buf[0];
2826 	while (1) {
2827 		start_codeposition = get_oneunit(buf, &startcodepos, &length);
2828 
2829 		switch (buf[startcodepos]) {
2830 		case SEQUENCE_HEADER_CODE:
2831 			io_printf(
2832 					"# SEQUENCE_HEADER_CODE (0x%02x) found at offset %d (0x%x)\n",
2833 					buf[startcodepos], startcode_offset,
2834 					startcode_offset);
2835 			break;
2836 		case EXTENSION_START_CODE:
2837 			io_printf(
2838 					"# EXTENSION_START_CODE (0x%02x) found at offset %d (0x%x)\n",
2839 					buf[startcodepos], startcode_offset,
2840 					startcode_offset);
2841 			break;
2842 		case USER_DATA_START_CODE:
2843 			io_printf(
2844 					"# USER_DATA_START_CODE (0x%02x) found at offset %d (0x%x)\n",
2845 					buf[startcodepos], startcode_offset,
2846 					startcode_offset);
2847 			break;
2848 		case VIDEO_EDIT_CODE:
2849 			io_printf(
2850 					"# VIDEO_EDIT_CODE (0x%02x) found at offset %d (0x%x)\n",
2851 					buf[startcodepos], startcode_offset,
2852 					startcode_offset);
2853 			break;
2854 		case I_PICTURE_START_CODE:
2855 			io_printf(
2856 					"# I_PICTURE_START_CODE (0x%02x) found at offset %d (0x%x)\n",
2857 					buf[startcodepos], startcode_offset,
2858 					startcode_offset);
2859 			break;
2860 		case PB_PICTURE_START_CODE:
2861 			io_printf(
2862 					"# PB_PICTURE_START_CODE (0x%02x) found at offset %d (0x%x)\n",
2863 					buf[startcodepos], startcode_offset,
2864 					startcode_offset);
2865 			break;
2866 		case SEQUENCE_END_CODE:
2867 			io_printf(
2868 					"# SEQUENCE_END_CODE (0x%02x) found at offset %d (0x%x)\n",
2869 					buf[startcodepos], startcode_offset,
2870 					startcode_offset);
2871 			break;
2872 		default:
2873 			io_printf(
2874 					"# SLICE_START_CODE (0x%02x) found at offset %d (0x%x)\n",
2875 					buf[startcodepos], startcode_offset,
2876 					startcode_offset);
2877 #if 0
2878 			io_printf("VLD_MEM_VIFIFO_START_PTR %x\r\n",
2879 					READ_VREG(VLD_MEM_VIFIFO_START_PTR));
2880 			io_printf("VLD_MEM_VIFIFO_CURR_PTR %x\r\n",
2881 					READ_VREG(VLD_MEM_VIFIFO_CURR_PTR));
2882 			io_printf("VLD_MEM_VIFIFO_END_PTR %x\r\n",
2883 					READ_VREG(VLD_MEM_VIFIFO_END_PTR));
2884 			io_printf("VLD_MEM_VIFIFO_WP %x\r\n"
2885 					READ_VREG(VLD_MEM_VIFIFO_WP));
2886 			io_printf("VLD_MEM_VIFIFO_RP %x\r\n",
2887 					READ_VREG(VLD_MEM_VIFIFO_RP));
2888 			io_printf("VLD_MEM_VBUF_RD_PTR %x\r\n"
2889 					READ_VREG(VLD_MEM_VBUF_RD_PTR));
2890 			io_printf("VLD_MEM_VIFIFO_BUF_CNTL %x\r\n",
2891 					READ_VREG(VLD_MEM_VIFIFO_BUF_CNTL));
2892 			io_printf("PARSER_VIDEO_HOLE %x\r\n",
2893 					READ_MPEG_REG(PARSER_VIDEO_HOLE));
2894 #endif
2895 			if ((buf[startcodepos] >= SLICE_START_CODE_MIN
2896 				&& buf[startcodepos]
2897 				<= SLICE_START_CODE_MAX)
2898 				&& ((!img->seq_header_indicate)
2899 				|| (img->type == B_IMG
2900 				&& img->b_discard_flag
2901 				== 1
2902 				&& !img->no_forward_reference))) {
2903 				break;
2904 			} else if (buf[startcodepos] >= SLICE_START_CODE_MIN) {
2905 
2906 				first_slice_length = length;
2907 				first_slice_startpos = startcodepos;
2908 
2909 				temp_slice_buf = &tmp_buf[0];
2910 				return SOP;
2911 			} else {
2912 				io_printf("Can't find start code");
2913 				return -EOS;
2914 			}
2915 		}
2916 	}
2917 
2918 }
2919 
2920 /*
2921  *************************************************************************
2922  * Function:Allocates a Bitstream
2923  * Input:
2924  * Output:allocated Bitstream point
2925  * Return:
2926  * Attention:
2927  *************************************************************************
2928  */
alloc_bitstream(void)2929 struct bitstream_s *alloc_bitstream(void)
2930 {
2931 	struct bitstream_s *bitstream;
2932 
2933 	bitstream = (struct bitstream_s *)local_alloc(1,
2934 		sizeof(struct bitstream_s));
2935 	if (bitstream == NULL) {
2936 		io_printf(
2937 			"AllocBitstream: Memory allocation for Bitstream failed");
2938 		return NULL;
2939 	}
2940 	bitstream->stream_buffer = (unsigned char *)local_alloc(
2941 			MAX_CODED_FRAME_SIZE,
2942 			sizeof(unsigned char));
2943 	if (bitstream->stream_buffer == NULL) {
2944 		io_printf(
2945 				"AllocBitstream: Memory allocation for streamBuffer failed");
2946 		return NULL;
2947 	}
2948 
2949 	return bitstream;
2950 }
2951 
biari_init_context_logac(struct bi_context_type_s * ctx)2952 void biari_init_context_logac(struct bi_context_type_s *ctx)
2953 {
2954 	ctx->LG_PMPS = (QUARTER << LG_PMPS_SHIFTNO) - 1;
2955 	ctx->MPS = 0;
2956 	ctx->cycno = 0;
2957 }
2958 
2959 #define BIARI_CTX_INIT1_LOG(jj, ctx)\
2960 {\
2961 	for (j = 0; j < jj; j++)\
2962 		biari_init_context_logac(&(ctx[j]));\
2963 }
2964 
2965 #define BIARI_CTX_INIT2_LOG(ii, jj, ctx)\
2966 {\
2967 	for (i = 0; i < ii; i++)\
2968 		for (j = 0; j < jj; j++)\
2969 			biari_init_context_logac(&(ctx[i][j]));\
2970 }
2971 
2972 #define BIARI_CTX_INIT3_LOG(ii, jj, kk, ctx)\
2973 {\
2974 	for (i = 0; i < ii; i++)\
2975 		for (j = 0; j < jj; j++)\
2976 			for (k = 0; k < kk; k++)\
2977 				biari_init_context_logac(&(ctx[i][j][k]));\
2978 }
2979 
2980 #define BIARI_CTX_INIT4_LOG(ii, jj, kk, ll, ctx)\
2981 {\
2982 	for (i = 0; i < ii; i++)\
2983 		for (j = 0; j < jj; j++)\
2984 			for (k = 0; k < kk; k++)\
2985 				for (l = 0; l < ll; l++)\
2986 					biari_init_context_logac\
2987 					(&(ctx[i][j][k][l]));\
2988 }
2989 
init_contexts(struct img_par * img)2990 void init_contexts(struct img_par *img)
2991 {
2992 	struct motion_info_contexts_s *mc = img->current_slice->mot_ctx;
2993 	struct texture_info_contexts *tc = img->current_slice->tex_ctx;
2994 	int i, j;
2995 
2996 #ifdef DUMP_DEBUG
2997 	if (avs_get_debug_flag() & SLICE_INFO_DUMP)
2998 		io_printf(" ---- init_contexts ----\n");
2999 #endif
3000 
3001 	BIARI_CTX_INIT2_LOG(3, NUM_MB_TYPE_CTX, mc->mb_type_contexts);
3002 	BIARI_CTX_INIT2_LOG(2, NUM_B8_TYPE_CTX, mc->b8_type_contexts);
3003 	BIARI_CTX_INIT2_LOG(2, NUM_MV_RES_CTX, mc->mv_res_contexts);
3004 	BIARI_CTX_INIT2_LOG(2, NUM_REF_NO_CTX, mc->ref_no_contexts);
3005 	BIARI_CTX_INIT1_LOG(NUM_DELTA_QP_CTX, mc->delta_qp_contexts);
3006 	BIARI_CTX_INIT1_LOG(NUM_MB_AFF_CTX, mc->mb_aff_contexts);
3007 
3008 	BIARI_CTX_INIT1_LOG(NUM_IPR_CTX, tc->ipr_contexts);
3009 	BIARI_CTX_INIT1_LOG(NUM_CIPR_CTX, tc->cipr_contexts);
3010 	BIARI_CTX_INIT2_LOG(3, NUM_CBP_CTX, tc->cbp_contexts);
3011 	BIARI_CTX_INIT2_LOG(NUM_BLOCK_TYPES, NUM_BCBP_CTX, tc->bcbp_contexts);
3012 	BIARI_CTX_INIT2_LOG(NUM_BLOCK_TYPES, NUM_ONE_CTX, tc->one_contexts);
3013 	BIARI_CTX_INIT2_LOG(NUM_BLOCK_TYPES, NUM_ABS_CTX, tc->abs_contexts);
3014 	BIARI_CTX_INIT2_LOG(NUM_BLOCK_TYPES, NUM_MAP_CTX, tc->fld_map_contexts);
3015 	BIARI_CTX_INIT2_LOG(NUM_BLOCK_TYPES, NUM_LAST_CTX,
3016 			tc->fld_last_contexts);
3017 	BIARI_CTX_INIT2_LOG(NUM_BLOCK_TYPES, NUM_MAP_CTX, tc->map_contexts);
3018 	BIARI_CTX_INIT2_LOG(NUM_BLOCK_TYPES, NUM_LAST_CTX, tc->last_contexts);
3019 #ifdef TEST_WEIGHTING_AEC
3020 	biari_init_context_logac(&mc->mb_weighting_pred);
3021 #endif
3022 }
3023 
3024 /*!
3025  ************************************************************************
3026  * \brief
3027  *    Allocation of contexts models for the motion info
3028  *    used for arithmetic decoding
3029  *
3030  ************************************************************************
3031  */
create_contexts_motioninfo(void)3032 struct motion_info_contexts_s *create_contexts_motioninfo(void)
3033 {
3034 	struct motion_info_contexts_s *deco_ctx;
3035 
3036 	deco_ctx = (struct motion_info_contexts_s *)local_alloc(1,
3037 			sizeof(struct motion_info_contexts_s));
3038 	if (deco_ctx == NULL)
3039 		no_mem_exit("create_contexts_motioninfo: deco_ctx");
3040 
3041 	return deco_ctx;
3042 }
3043 
3044 /*!
3045  ************************************************************************
3046  * \brief
3047  *    Allocates of contexts models for the texture info
3048  *    used for arithmetic decoding
3049  ************************************************************************
3050  */
create_contexts_textureinfo(void)3051 struct texture_info_contexts *create_contexts_textureinfo(void)
3052 {
3053 	struct texture_info_contexts *deco_ctx;
3054 
3055 	deco_ctx = (struct texture_info_contexts *)local_alloc(1,
3056 			sizeof(struct texture_info_contexts));
3057 	if (deco_ctx == NULL)
3058 		no_mem_exit("create_contexts_textureinfo: deco_ctx");
3059 
3060 	return deco_ctx;
3061 }
3062 
alloc_partition(int n)3063 struct datapartition *alloc_partition(int n)
3064 {
3065 	struct datapartition *part_arr, *datapart;
3066 	int i;
3067 
3068 	part_arr =
3069 	(struct datapartition *)local_alloc(n, sizeof(struct datapartition));
3070 	if (part_arr == NULL) {
3071 		no_mem_exit(
3072 				"alloc_partition: Memory allocation for Data Partition failed");
3073 		return NULL;
3074 	}
3075 
3076 #if LIWR_FIX
3077 	part_arr[0].bitstream = NULL;
3078 #else
3079 	for (i = 0; i < n; i++) {
3080 		datapart = &(part_arr[i]);
3081 		datapart->bitstream = (struct bitstream_s *)local_alloc(1,
3082 				sizeof(struct bitstream_s));
3083 		if (datapart->bitstream == NULL) {
3084 			no_mem_exit(
3085 					"alloc_partition: Memory allocation for Bitstream failed");
3086 			return NULL;
3087 		}
3088 	}
3089 #endif
3090 	return part_arr;
3091 }
3092 
malloc_slice(struct img_par * img)3093 int malloc_slice(struct img_par *img)
3094 {
3095 	struct slice_s *currslice;
3096 
3097 	img->current_slice =
3098 	(struct slice_s *)local_alloc(1, sizeof(struct slice_s));
3099 	currslice = img->current_slice;
3100 	if (currslice == NULL) {
3101 		no_mem_exit(
3102 			"Memory allocation for struct slice_s datastruct Failed"
3103 			);
3104 		return 0;
3105 	}
3106 	if (1) {
3107 
3108 		currslice->mot_ctx = create_contexts_motioninfo();
3109 		if (currslice->mot_ctx == NULL)
3110 			return 0;
3111 
3112 		currslice->tex_ctx = create_contexts_textureinfo();
3113 		if (currslice->tex_ctx == NULL)
3114 			return 0;
3115 	}
3116 #if LIWR_FIX
3117 	currslice->max_part_nr = 1;
3118 #else
3119 	currslice->max_part_nr = 3;
3120 #endif
3121 	currslice->part_arr = alloc_partition(currslice->max_part_nr);
3122 	if (currslice->part_arr == NULL)
3123 		return 0;
3124 	return 1;
3125 }
3126 
init(struct img_par * img)3127 void init(struct img_par *img)
3128 {
3129 	int i;
3130 
3131 	for (i = 0; i < 256; i++)
3132 		img->quad[i] = i * i;
3133 }
3134 
3135 /*
3136  *************************************************************************
3137  * Function:Allocate 2D memory array -> int array2D[rows][columns]
3138  * Input:
3139  * Output: memory size in bytes
3140  * Return:
3141  * Attention:
3142  *************************************************************************
3143  */
3144 
get_mem2Dint(int *** array2D,int rows,int columns)3145 int get_mem2Dint(int ***array2D, int rows, int columns)
3146 {
3147 	int i;
3148 
3149 	*array2D = (int **)local_alloc(rows, sizeof(int *));
3150 	if (*array2D == NULL) {
3151 		no_mem_exit("get_mem2Dint: array2D");
3152 		return -1;
3153 	}
3154 	(*array2D)[0] = (int *)local_alloc(rows * columns, sizeof(int));
3155 	if ((*array2D)[0] == NULL) {
3156 		no_mem_exit("get_mem2Dint: array2D");
3157 		return -1;
3158 	}
3159 
3160 	for (i = 1; i < rows; i++)
3161 		(*array2D)[i] = (*array2D)[i - 1] + columns;
3162 
3163 	return rows * columns * sizeof(int);
3164 }
3165 
initial_decode(void)3166 int initial_decode(void)
3167 {
3168 	int i, j;
3169 	int ret;
3170 	int img_height = (vertical_size + img->auto_crop_bottom);
3171 	int memory_size = 0;
3172 
3173 	ret = malloc_slice(img);
3174 	if (ret == 0)
3175 		return 0;
3176 
3177 	mb_data = (struct macroblock *)local_alloc(
3178 			(img->width / MB_BLOCK_SIZE)
3179 			* (img_height /*vertical_size*/
3180 			/ MB_BLOCK_SIZE), sizeof(struct macroblock));
3181 	if (mb_data == NULL) {
3182 		no_mem_exit("init_global_buffers: mb_data");
3183 		return 0;
3184 	}
3185 
3186 	if (progressive_sequence) {
3187 		int size;
3188 		size = get_mem2Dint(&(img->ipredmode),
3189 				img->width / B8_SIZE * 2 + 4,
3190 				vertical_size / B8_SIZE * 2 + 4);
3191 		if (size == -1)
3192 			return 0;
3193 
3194 		memory_size += size;
3195 	} else {
3196 		int size;
3197 		size = get_mem2Dint(&(img->ipredmode),
3198 				img->width / B8_SIZE * 2 + 4,
3199 				(vertical_size + 32) / (2 * B8_SIZE) * 4 + 4);
3200 		if (size == -1)
3201 			return 0;
3202 
3203 		memory_size += size;
3204 	}
3205 
3206 	for (i = 0; i < img->width / (B8_SIZE) * 2 + 4; i++) {
3207 		for (j = 0; j < img->height / (B8_SIZE) * 2 + 4; j++)
3208 			img->ipredmode[i][j] = -1;
3209 	}
3210 
3211 	init(img);
3212 	img->number = 0;
3213 	img->type = I_IMG;
3214 	img->imgtr_last_p = 0;
3215 	img->imgtr_next_p = 0;
3216 
3217 	img->new_seq_header_flag = 1;
3218 	img->new_sequence_flag = 1;
3219 
3220 	return 1;
3221 }
3222 
aec_new_slice(void)3223 void aec_new_slice(void)
3224 {
3225 	last_dquant = 0;
3226 }
3227 
3228 /*!
3229  ************************************************************************
3230  * \brief
3231  *    Initializes the DecodingEnvironment for the arithmetic coder
3232  ************************************************************************
3233  */
3234 
arideco_start_decoding(struct decoding_environment_s * dep,unsigned char * cpixcode,int firstbyte,int * cpixcode_len,int slice_type)3235 void arideco_start_decoding(struct decoding_environment_s *dep,
3236 	unsigned char *cpixcode,
3237 		int firstbyte, int *cpixcode_len, int slice_type)
3238 {
3239 
3240 	dcodestrm = cpixcode;
3241 	dcodestrm_len = cpixcode_len;
3242 	*dcodestrm_len = firstbyte;
3243 
3244 	s1 = 0;
3245 	t1 = QUARTER - 1;
3246 	value_s = 0;
3247 
3248 	value_t = 0;
3249 
3250 	{
3251 		int i;
3252 
3253 		dbits_to_go = 0;
3254 		for (i = 0; i < B_BITS - 1; i++) {
3255 			if (--dbits_to_go < 0)
3256 				get_byte();
3257 
3258 			value_t = (value_t << 1)
3259 					| ((dbuffer >> dbits_to_go) & 0x01);
3260 		}
3261 	}
3262 
3263 	while (value_t < QUARTER) {
3264 		if (--dbits_to_go < 0)
3265 			get_byte();
3266 
3267 		value_t = (value_t << 1) | ((dbuffer >> dbits_to_go) & 0x01);
3268 		value_s++;
3269 	}
3270 	value_t = value_t & 0xff;
3271 
3272 	dec_final = dec_bypass = 0;
3273 
3274 
3275 
3276 }
3277 
3278 /*
3279  *************************************************************************
3280  * Function:Checks the availability of neighboring macroblocks of
3281  the current macroblock for prediction and context determination;
3282  marks the unavailable MBs for intra prediction in the
3283  ipredmode-array by -1. Only neighboring MBs in the causal
3284  past of the current MB are checked.
3285  * Input:
3286  * Output:
3287  * Return:
3288  * Attention:
3289  *************************************************************************
3290  */
3291 
checkavailabilityofneighbors(struct img_par * img)3292 void checkavailabilityofneighbors(struct img_par *img)
3293 {
3294 	int i, j;
3295 	const int mb_width = img->width / MB_BLOCK_SIZE;
3296 	const int mb_nr = img->current_mb_nr;
3297 	struct macroblock *curr_mb = &mb_data[mb_nr];
3298 	int check_value;
3299 	int remove_prediction;
3300 
3301 	curr_mb->mb_available_up = NULL;
3302 	curr_mb->mb_available_left = NULL;
3303 
3304 	for (i = 0; i < 3; i++)
3305 		for (j = 0; j < 3; j++)
3306 			mb_data[mb_nr].mb_available[i][j] = NULL;
3307 
3308 	mb_data[mb_nr].mb_available[1][1] = curr_mb;
3309 
3310 	if (img->pix_x >= MB_BLOCK_SIZE) {
3311 		remove_prediction = curr_mb->slice_nr
3312 				!= mb_data[mb_nr - 1].slice_nr;
3313 
3314 		if (remove_prediction)
3315 
3316 		{
3317 
3318 			img->ipredmode[(img->block_x + 1) * 2 - 1][(img->block_y
3319 					+ 1) * 2] = -1;
3320 			img->ipredmode[(img->block_x + 1) * 2 - 1][(img->block_y
3321 					+ 1) * 2 + 1] = -1;
3322 			img->ipredmode[(img->block_x + 1) * 2 - 1][(img->block_y
3323 					+ 2) * 2] = -1;
3324 			img->ipredmode[(img->block_x + 1) * 2 - 1][(img->block_y
3325 					+ 2) * 2 + 1] = -1;
3326 		}
3327 		if (!remove_prediction)
3328 			curr_mb->mb_available[1][0] = &(mb_data[mb_nr - 1]);
3329 
3330 	}
3331 
3332 	check_value = (img->pix_y >= MB_BLOCK_SIZE);
3333 	if (check_value) {
3334 		remove_prediction = curr_mb->slice_nr
3335 				!= mb_data[mb_nr - mb_width].slice_nr;
3336 
3337 		if (remove_prediction) {
3338 			img->ipredmode
3339 			[(img->block_x + 1) * 2][(img->block_y + 1)
3340 					* 2 - 1] = -1;
3341 			img->ipredmode[(img->block_x + 1) * 2 + 1][(img->block_y
3342 					+ 1) * 2 - 1] = -1;
3343 			img->ipredmode[(img->block_x + 1) * 2 + 2][(img->block_y
3344 					+ 1) * 2 - 1] = -1;
3345 			img->ipredmode[(img->block_x + 1) * 2 + 3][(img->block_y
3346 					+ 1) * 2 - 1] = -1;
3347 		}
3348 
3349 		if (!remove_prediction) {
3350 			curr_mb->mb_available[0][1] =
3351 					&(mb_data[mb_nr - mb_width]);
3352 		}
3353 	}
3354 
3355 	if (img->pix_y >= MB_BLOCK_SIZE && img->pix_x >= MB_BLOCK_SIZE) {
3356 		remove_prediction = curr_mb->slice_nr
3357 				!= mb_data[mb_nr - mb_width - 1].slice_nr;
3358 
3359 		if (remove_prediction) {
3360 			img->ipredmode[img->block_x * 2 + 1][img->block_y * 2
3361 					+ 1] = -1;
3362 		}
3363 		if (!remove_prediction) {
3364 			curr_mb->mb_available[0][0] = &(mb_data[mb_nr - mb_width
3365 					- 1]);
3366 		}
3367 	}
3368 
3369 	if (img->pix_y >= MB_BLOCK_SIZE
3370 			&& img->pix_x < (img->width - MB_BLOCK_SIZE)) {
3371 		if (curr_mb->slice_nr == mb_data[mb_nr - mb_width + 1].slice_nr)
3372 			curr_mb->mb_available[0][2] = &(mb_data[mb_nr - mb_width
3373 					+ 1]);
3374 	}
3375 
3376 	if (1) {
3377 		curr_mb->mbaddr_a = mb_nr - 1;
3378 		curr_mb->mbaddr_b = mb_nr - img->pic_width_inmbs;
3379 		curr_mb->mbaddr_c = mb_nr - img->pic_width_inmbs + 1;
3380 		curr_mb->mbaddr_d = mb_nr - img->pic_width_inmbs - 1;
3381 
3382 		curr_mb->mbavail_a =
3383 		(curr_mb->mb_available[1][0] != NULL) ? 1 : 0;
3384 		curr_mb->mbavail_b =
3385 		(curr_mb->mb_available[0][1] != NULL) ? 1 : 0;
3386 		curr_mb->mbavail_c =
3387 		(curr_mb->mb_available[0][2] != NULL) ? 1 : 0;
3388 		curr_mb->mbavail_d =
3389 		(curr_mb->mb_available[0][0] != NULL) ? 1 : 0;
3390 
3391 	}
3392 
3393 }
3394 
checkavailabilityofneighborsaec(void)3395 void checkavailabilityofneighborsaec(void)
3396 {
3397 
3398 	int i, j;
3399 	const int mb_width = img->width / MB_BLOCK_SIZE;
3400 	const int mb_nr = img->current_mb_nr;
3401 	struct macroblock *curr_mb = &(mb_data[mb_nr]);
3402 	int check_value;
3403 
3404 	for (i = 0; i < 3; i++)
3405 		for (j = 0; j < 3; j++)
3406 			mb_data[mb_nr].mb_available[i][j] = NULL;
3407 	mb_data[mb_nr].mb_available[1][1] = &(mb_data[mb_nr]);
3408 
3409 	if (img->pix_x >= MB_BLOCK_SIZE) {
3410 		int remove_prediction = curr_mb->slice_nr
3411 				!= mb_data[mb_nr - 1].slice_nr;
3412 		if (!remove_prediction)
3413 			curr_mb->mb_available[1][0] = &(mb_data[mb_nr - 1]);
3414 	}
3415 
3416 	check_value = (img->pix_y >= MB_BLOCK_SIZE);
3417 	if (check_value) {
3418 		int remove_prediction = curr_mb->slice_nr
3419 				!= mb_data[mb_nr - mb_width].slice_nr;
3420 
3421 		if (!remove_prediction) {
3422 			curr_mb->mb_available[0][1] =
3423 					&(mb_data[mb_nr - mb_width]);
3424 		}
3425 	}
3426 
3427 	if (img->pix_y >= MB_BLOCK_SIZE && img->pix_x >= MB_BLOCK_SIZE) {
3428 		int remove_prediction = curr_mb->slice_nr
3429 				!= mb_data[mb_nr - mb_width - 1].slice_nr;
3430 		if (!remove_prediction) {
3431 			curr_mb->mb_available[0][0] = &(mb_data[mb_nr - mb_width
3432 					- 1]);
3433 		}
3434 	}
3435 
3436 	if (img->pix_y >= MB_BLOCK_SIZE
3437 			&& img->pix_x < (img->width - MB_BLOCK_SIZE)) {
3438 		if (curr_mb->slice_nr == mb_data[mb_nr - mb_width + 1].slice_nr)
3439 			curr_mb->mb_available[0][2] = &(mb_data[mb_nr - mb_width
3440 					+ 1]);
3441 	}
3442 	curr_mb->mb_available_left = curr_mb->mb_available[1][0];
3443 	curr_mb->mb_available_up = curr_mb->mb_available[0][1];
3444 	curr_mb->mbaddr_a = mb_nr - 1;
3445 	curr_mb->mbaddr_b = mb_nr - img->pic_width_inmbs;
3446 	curr_mb->mbaddr_c = mb_nr - img->pic_width_inmbs + 1;
3447 	curr_mb->mbaddr_d = mb_nr - img->pic_width_inmbs - 1;
3448 
3449 	curr_mb->mbavail_a = (curr_mb->mb_available[1][0] != NULL) ? 1 : 0;
3450 	curr_mb->mbavail_b = (curr_mb->mb_available[0][1] != NULL) ? 1 : 0;
3451 	curr_mb->mbavail_c = (curr_mb->mb_available[0][2] != NULL) ? 1 : 0;
3452 	curr_mb->mbavail_d = (curr_mb->mb_available[0][0] != NULL) ? 1 : 0;
3453 }
3454 
3455 /*
3456  *************************************************************************
3457  * Function:initializes the current macroblock
3458  * Input:
3459  * Output:
3460  * Return:
3461  * Attention:
3462  *************************************************************************
3463  */
3464 
start_macroblock(struct img_par * img)3465 void start_macroblock(struct img_par *img)
3466 {
3467 	int i, j, k, l;
3468 	struct macroblock *curr_mb;
3469 
3470 #ifdef AVSP_LONG_CABAC
3471 #else
3472 
3473 #endif
3474 
3475 	curr_mb = &mb_data[img->current_mb_nr];
3476 
3477 	/* Update coordinates of the current macroblock */
3478 	img->mb_x = (img->current_mb_nr) % (img->width / MB_BLOCK_SIZE);
3479 	img->mb_y = (img->current_mb_nr) / (img->width / MB_BLOCK_SIZE);
3480 
3481 #ifdef DUMP_DEBUG
3482 	if (avs_get_debug_flag() & MB_NUM_DUMP)
3483 		io_printf(" #Begin MB : %d, (%x, %x) es_ptr %d\n",
3484 			img->current_mb_nr, img->mb_x, img->mb_y, es_ptr);
3485 #endif
3486 
3487 
3488 	total_mb_count = total_mb_count + 1;
3489 
3490 	/* Define vertical positions */
3491 	img->block_y = img->mb_y * BLOCK_SIZE / 2; /* luma block position */
3492 	img->block8_y = img->mb_y * BLOCK_SIZE / 2;
3493 	img->pix_y = img->mb_y * MB_BLOCK_SIZE; /* luma macroblock position */
3494 	if (chroma_format == 2)
3495 		img->pix_c_y = img->mb_y *
3496 		MB_BLOCK_SIZE; /* chroma macroblock position */
3497 	else
3498 		img->pix_c_y = img->mb_y *
3499 		MB_BLOCK_SIZE / 2; /* chroma macroblock position */
3500 
3501 	/* Define horizontal positions */
3502 	img->block_x = img->mb_x * BLOCK_SIZE / 2; /* luma block position */
3503 	img->block8_x = img->mb_x * BLOCK_SIZE / 2;
3504 	img->pix_x = img->mb_x * MB_BLOCK_SIZE; /* luma pixel position */
3505 	img->pix_c_x = img->mb_x *
3506 	MB_BLOCK_SIZE / 2; /* chroma pixel position */
3507 
3508 	checkavailabilityofneighbors(img);
3509 
3510 	/*<!*******EDIT START BY lzhang ******************/
3511 
3512 	if (1)
3513 		checkavailabilityofneighborsaec();
3514 	/*<!*******EDIT end BY lzhang ******************/
3515 
3516 	curr_mb->qp = img->qp;
3517 	curr_mb->mb_type = 0;
3518 	curr_mb->delta_quant = 0;
3519 	curr_mb->cbp = 0;
3520 	curr_mb->cbp_blk = 0;
3521 	curr_mb->c_ipred_mode = DC_PRED_8;
3522 	curr_mb->c_ipred_mode_2 = DC_PRED_8;
3523 
3524 	for (l = 0; l < 2; l++)
3525 		for (j = 0; j < BLOCK_MULTIPLE; j++)
3526 			for (i = 0; i < BLOCK_MULTIPLE; i++)
3527 				for (k = 0; k < 2; k++)
3528 					curr_mb->mvd[l][j][i][k] = 0;
3529 
3530 	curr_mb->cbp_bits = 0;
3531 
3532 	for (j = 0; j < MB_BLOCK_SIZE; j++)
3533 		for (i = 0; i < MB_BLOCK_SIZE; i++)
3534 			img->m7[i][j] = 0;
3535 
3536 	for (j = 0; j < 2 * BLOCK_SIZE; j++)
3537 		for (i = 0; i < 2 * BLOCK_SIZE; i++) {
3538 			img->m8[0][i][j] = 0;
3539 			img->m8[1][i][j] = 0;
3540 			img->m8[2][i][j] = 0;
3541 			img->m8[3][i][j] = 0;
3542 		}
3543 
3544 	curr_mb->lf_disable = 1;
3545 
3546 	img->weighting_prediction = 0;
3547 }
3548 
3549 /*
3550  *************************************************************************
3551  * Function:init macroblock I and P frames
3552  * Input:
3553  * Output:
3554  * Return:
3555  * Attention:
3556  *************************************************************************
3557  */
3558 
init_macroblock(struct img_par * img)3559 void init_macroblock(struct img_par *img)
3560 {
3561 	int i, j;
3562 
3563 
3564 	for (i = 0; i < 4; i++) {
3565 		for (j = 0; j < 4; j++) {
3566 			img->ipredmode[img->block_x * 2 + i + 2][img->block_y
3567 					* 2 + j + 2] = -1;
3568 		}
3569 	}
3570 
3571 }
3572 
3573 /*
3574  *************************************************************************
3575  * Function:Interpret the mb mode for I-Frames
3576  * Input:
3577  * Output:
3578  * Return:
3579  * Attention:
3580  *************************************************************************
3581  */
3582 
interpret_mb_mode_i(struct img_par * img)3583 void interpret_mb_mode_i(struct img_par *img)
3584 {
3585 	int i;
3586 
3587 	struct macroblock *curr_mb = &mb_data[img->current_mb_nr];
3588 	int num = 4;
3589 
3590 	curr_mb->mb_type = I8MB;
3591 
3592 
3593 	current_mb_intra = 1;
3594 
3595 	for (i = 0; i < 4; i++) {
3596 		curr_mb->b8mode[i] = IBLOCK;
3597 		curr_mb->b8pdir[i] = -1;
3598 	}
3599 
3600 	for (i = num; i < 4; i++) {
3601 		curr_mb->b8mode[i] =
3602 				curr_mb->mb_type_2 == P8x8 ?
3603 						4 : curr_mb->mb_type_2;
3604 		curr_mb->b8pdir[i] = 0;
3605 	}
3606 }
3607 
3608 const int pred_4x4[9][9] = {{0, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 1, 1, 1, 1, 1, 1,
3609 		1, 1}, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {0, 0, 0, 3, 3, 3, 3, 3, 3},
3610 		{0, 1, 4, 4, 4, 4, 4, 4, 4}, {0, 1, 5, 5, 5, 5, 5, 5, 5}, {0, 0,
3611 				0, 0, 0, 0, 6, 0, 0},
3612 		{0, 1, 7, 7, 7, 7, 7, 7, 7}, {0, 0, 0, 0, 4, 5, 6, 7, 8}
3613 
3614 };
3615 
3616 const int pred_4x4to8x8[9] = {0, 1, 2, 3, 4, 1, 0, 1, 0
3617 
3618 };
3619 
3620 const int pred_8x8to4x4[5] = {0, 1, 2, 3, 4};
3621 
read_ipred_block_modes(struct img_par * img,int b8)3622 void read_ipred_block_modes(struct img_par *img, int b8)
3623 {
3624 	int bi, bj, dec;
3625 	struct syntaxelement curr_se;
3626 	struct macroblock *curr_mb;
3627 	int j2;
3628 	int mostprobableintrapredmode;
3629 	int upintrapredmode;
3630 	int uprightintrapredmode;
3631 	int leftintrapredmode;
3632 	int leftdownintrapredmode;
3633 	int intrachromapredmodeflag;
3634 
3635 	struct slice_s *currslice = img->current_slice;
3636 	struct datapartition *dp;
3637 
3638 	curr_mb = mb_data + img->current_mb_nr;
3639 	intrachromapredmodeflag = IS_INTRA(curr_mb);
3640 
3641 	curr_se.type = SE_INTRAPREDMODE;
3642 #if TRACE
3643 	strncpy(curr_se.tracestring, "Ipred Mode", TRACESTRING_SIZE);
3644 #endif
3645 
3646 	if (b8 < 4) {
3647 		if (curr_mb->b8mode[b8] == IBLOCK) {
3648 			intrachromapredmodeflag = 1;
3649 
3650 			if (1) {
3651 				dp = &(currslice->part_arr[0]);
3652 				curr_se.reading = read_intrapredmode_aec;
3653 				dp->read_syntax_element(&curr_se, img, dp);
3654 
3655 				if (curr_se.value1 == -1)
3656 					push_es(1, 1);
3657 				else
3658 					push_es(curr_se.value1, 3);
3659 
3660 
3661 			}
3662 			bi = img->block_x + (b8 & 1);
3663 			bj = img->block_y + (b8 / 2);
3664 
3665 			upintrapredmode = img->ipredmode[(bi + 1) * 2][(bj) * 2
3666 					+ 1];
3667 			uprightintrapredmode =
3668 					img->ipredmode[(bi + 1) * 2 + 1][(bj)
3669 							* 2 + 1];
3670 			leftintrapredmode =
3671 					img->ipredmode[(bi) * 2 + 1][(bj + 1)
3672 							* 2];
3673 			leftdownintrapredmode = img->ipredmode[(bi) * 2 + 1][(bj
3674 					+ 1) * 2 + 1];
3675 
3676 			if ((upintrapredmode < 0) || (leftintrapredmode < 0)) {
3677 				mostprobableintrapredmode = DC_PRED;
3678 			} else if ((upintrapredmode < NO_INTRA_PMODE)
3679 					&& (leftintrapredmode <
3680 						NO_INTRA_PMODE)) {
3681 				mostprobableintrapredmode =
3682 					upintrapredmode
3683 					< leftintrapredmode ?
3684 					upintrapredmode :
3685 					leftintrapredmode;
3686 			} else if (upintrapredmode < NO_INTRA_PMODE) {
3687 				mostprobableintrapredmode = upintrapredmode;
3688 			} else if (leftintrapredmode < NO_INTRA_PMODE) {
3689 				mostprobableintrapredmode = leftintrapredmode;
3690 			} else {
3691 				mostprobableintrapredmode =
3692 					pred_4x4[leftintrapredmode
3693 					- INTRA_PMODE_4x4][upintrapredmode
3694 					- INTRA_PMODE_4x4];
3695 				mostprobableintrapredmode =
3696 				pred_4x4to8x8[mostprobableintrapredmode];
3697 			}
3698 
3699 
3700 
3701 			dec =
3702 					(curr_se.value1 == -1) ?
3703 					mostprobableintrapredmode :
3704 					curr_se.value1
3705 					+ (curr_se.value1
3706 					>= mostprobableintrapredmode);
3707 
3708 #ifdef DUMP_DEBUG
3709 			if (avs_get_debug_flag() & MB_INFO_DUMP)
3710 				io_printf(" - ipredmode[%d] : %d\n", b8, dec);
3711 #endif
3712 
3713 			img->ipredmode[(1 + bi) * 2][(1 + bj) * 2] = dec;
3714 			img->ipredmode[(1 + bi) * 2 + 1][(1 + bj) * 2] = dec;
3715 			img->ipredmode[(1 + bi) * 2][(1 + bj) * 2 + 1] = dec;
3716 			img->ipredmode[(1 + bi) * 2 + 1][(1 + bj) * 2 + 1] =
3717 					dec;
3718 
3719 			j2 = bj;
3720 		}
3721 	} else if (b8 == 4 && curr_mb->b8mode[b8 - 3] == IBLOCK) {
3722 
3723 		curr_se.type = SE_INTRAPREDMODE;
3724 #if TRACE
3725 		strncpy(curr_se.tracestring,
3726 			"Chroma intra pred mode", TRACESTRING_SIZE);
3727 #endif
3728 
3729 		if (1) {
3730 			dp = &(currslice->part_arr[0]);
3731 			curr_se.reading = read_cipredmode_aec;
3732 			dp->read_syntax_element(&curr_se, img, dp);
3733 		} else
3734 
3735 		{
3736 		}
3737 		curr_mb->c_ipred_mode = curr_se.value1;
3738 
3739 		push_es(UE[curr_se.value1][0], UE[curr_se.value1][1]);
3740 
3741 #ifdef DUMP_DEBUG
3742 		if (avs_get_debug_flag() & MB_INFO_DUMP)
3743 			io_printf(" * UE c_ipred_mode read : %d\n",
3744 				curr_mb->c_ipred_mode);
3745 #endif
3746 
3747 		if (curr_se.value1 < DC_PRED_8 || curr_se.value1 > PLANE_8) {
3748 #ifdef DUMP_DEBUG
3749 			if (avs_get_debug_flag() & MB_INFO_DUMP)
3750 				io_printf("%d\n", img->current_mb_nr);
3751 #endif
3752 			pr_info("illegal chroma intra pred mode!\n");
3753 		}
3754 	}
3755 }
3756 
3757 /*!
3758  ************************************************************************
3759  * \brief
3760  *    This function is used to arithmetically decode the coded
3761  *    block pattern of a given MB.
3762  ************************************************************************
3763  */
readcp_aec(struct syntaxelement * se,struct img_par * img,struct decoding_environment_s * dep_dp)3764 void readcp_aec(struct syntaxelement *se, struct img_par *img,
3765 		struct decoding_environment_s *dep_dp)
3766 {
3767 	struct texture_info_contexts *ctx = img->current_slice->tex_ctx;
3768 	struct macroblock *curr_mb = &mb_data[img->current_mb_nr];
3769 
3770 	int mb_x, mb_y;
3771 	int a, b;
3772 	int curr_cbp_ctx, curr_cbp_idx;
3773 	int cbp = 0;
3774 	int cbp_bit;
3775 	int mask;
3776 
3777 	for (mb_y = 0; mb_y < 4; mb_y += 2) {
3778 		for (mb_x = 0; mb_x < 4; mb_x += 2) {
3779 			if (curr_mb->b8mode[mb_y + (mb_x / 2)] == IBLOCK)
3780 				curr_cbp_idx = 0;
3781 			else
3782 				curr_cbp_idx = 1;
3783 
3784 			if (mb_y == 0) {
3785 				if (curr_mb->mb_available_up == NULL)
3786 					b = 0;
3787 				else {
3788 					b = ((((curr_mb->mb_available_up)->cbp
3789 							& (1 << (2 + mb_x / 2)))
3790 							== 0) ? 1 : 0);
3791 				}
3792 
3793 			} else
3794 				b = (((cbp & (1 << (mb_x / 2))) == 0) ? 1 : 0);
3795 
3796 			if (mb_x == 0) {
3797 				if (curr_mb->mb_available_left == NULL)
3798 					a = 0;
3799 				else {
3800 					a =
3801 					((((curr_mb->mb_available_left)->cbp
3802 					& (1
3803 					<< (2
3804 					* (mb_y
3805 					/ 2)
3806 					+ 1)))
3807 					== 0) ?
3808 					1 : 0);
3809 				}
3810 			} else
3811 				a = (((cbp & (1 << mb_y)) == 0) ? 1 : 0);
3812 			curr_cbp_ctx = a + 2 * b;
3813 			mask = (1 << (mb_y + mb_x / 2));
3814 			cbp_bit = biari_decode_symbol(dep_dp,
3815 					ctx->cbp_contexts[0] + curr_cbp_ctx);
3816 
3817 			if (cbp_bit)
3818 				cbp += mask;
3819 		}
3820 	}
3821 	curr_cbp_ctx = 0;
3822 	cbp_bit = biari_decode_symbol(dep_dp,
3823 			ctx->cbp_contexts[1] + curr_cbp_ctx);
3824 
3825 	if (cbp_bit) {
3826 		curr_cbp_ctx = 1;
3827 		cbp_bit = biari_decode_symbol(dep_dp,
3828 				ctx->cbp_contexts[1] + curr_cbp_ctx);
3829 		if (cbp_bit) {
3830 			cbp += 48;
3831 
3832 		} else {
3833 			curr_cbp_ctx = 1;
3834 			cbp_bit = biari_decode_symbol(dep_dp,
3835 					ctx->cbp_contexts[1] + curr_cbp_ctx);
3836 			cbp += (cbp_bit == 1) ? 32 : 16;
3837 
3838 		}
3839 	}
3840 
3841 	se->value1 = cbp;
3842 	if (!cbp)
3843 		last_dquant = 0;
3844 
3845 
3846 
3847 }
3848 
3849 /*!
3850  ************************************************************************
3851  * \brief
3852  *    This function is used to arithmetically decode the delta qp
3853  *     of a given MB.
3854  ************************************************************************
3855  */
readdquant_aec(struct syntaxelement * se,struct img_par * img,struct decoding_environment_s * dep_dp)3856 void readdquant_aec(struct syntaxelement *se, struct img_par *img,
3857 		struct decoding_environment_s *dep_dp)
3858 {
3859 	struct motion_info_contexts_s *ctx = img->current_slice->mot_ctx;
3860 
3861 	int act_ctx;
3862 	int act_sym;
3863 	int dquant;
3864 
3865 
3866 	act_ctx = ((last_dquant != 0) ? 1 : 0);
3867 
3868 	act_sym = 1
3869 			- biari_decode_symbol(dep_dp,
3870 					ctx->delta_qp_contexts + act_ctx);
3871 	if (act_sym != 0) {
3872 		act_ctx = 2;
3873 		act_sym = unary_bin_decode(dep_dp,
3874 				ctx->delta_qp_contexts + act_ctx, 1);
3875 		act_sym++;
3876 	}
3877 	act_sym &= 0x3f;
3878 	push_es(UE[act_sym][0], UE[act_sym][1]);
3879 
3880 	dquant = (act_sym + 1) / 2;
3881 	if ((act_sym & 0x01) == 0)
3882 		dquant = -dquant;
3883 	se->value1 = dquant;
3884 
3885 	last_dquant = dquant;
3886 
3887 }
3888 
3889 int csyntax;
3890 
3891 #define CHECKDELTAQP {\
3892 	if (img->qp+curr_mb->delta_quant > 63\
3893 			|| img->qp+curr_mb->delta_quant < 0) {\
3894 		csyntax = 0;\
3895 		transcoding_error_flag = 1;\
3896 		io_printf("error(0) (%3d|%3d) @ MB%d\n",\
3897 			curr_mb->delta_quant,\
3898 			img->qp+curr_mb->delta_quant,\
3899 			img->picture_structure == 0 \
3900 			? img->current_mb_nr_fld : img->current_mb_nr);\
3901 		} }
3902 
3903 int dct_level[65];
3904 int dct_run[65];
3905 int pair_pos;
3906 int dct_pairs = -1;
3907 const int t_chr[5] = {0, 1, 2, 4, 3000};
3908 
readrunlevel_aec_ref(struct syntaxelement * se,struct img_par * img,struct decoding_environment_s * dep_dp)3909 void readrunlevel_aec_ref(struct syntaxelement *se, struct img_par *img,
3910 		struct decoding_environment_s *dep_dp)
3911 {
3912 	int pairs, rank, pos;
3913 	int run, level, abslevel, symbol;
3914 	int sign;
3915 
3916 	if (dct_pairs < 0) {
3917 		struct bi_context_type_s (*primary)[NUM_MAP_CTX];
3918 		struct bi_context_type_s *pctx;
3919 		struct bi_context_type_s *pCTX2;
3920 		int ctx, ctx2, offset;
3921 
3922 		if (se->context == LUMA_8x8) {
3923 			if (img->picture_structure == 0) {
3924 				primary =
3925 				img->current_slice->tex_ctx->fld_map_contexts;
3926 			} else {
3927 				primary =
3928 				img->current_slice->tex_ctx->map_contexts;
3929 			}
3930 		} else {
3931 			if (img->picture_structure == 0) {
3932 				primary =
3933 				img->current_slice->tex_ctx->fld_last_contexts;
3934 			} else {
3935 				primary =
3936 				img->current_slice->tex_ctx->last_contexts;
3937 			}
3938 		}
3939 
3940 		rank = 0;
3941 		pos = 0;
3942 		for (pairs = 0; pairs < 65; pairs++) {
3943 #ifdef DECODING_SANITY_CHECK
3944 			/*max index is NUM_BLOCK_TYPES - 1*/
3945 			pctx = primary[rank & 0x7];
3946 #else
3947 			pctx = primary[rank];
3948 #endif
3949 			if (rank > 0) {
3950 #ifdef DECODING_SANITY_CHECK
3951 				/*max index is NUM_BLOCK_TYPES - 1*/
3952 				pCTX2 = primary[(5 + (pos >> 5)) & 0x7];
3953 #else
3954 				pCTX2 = primary[5 + (pos >> 5)];
3955 #endif
3956 				ctx2 = (pos >> 1) & 0x0f;
3957 				ctx = 0;
3958 
3959 
3960 				if (biari_decode_symbolw(dep_dp, pctx + ctx,
3961 						pCTX2 + ctx2)) {
3962 					break;
3963 				}
3964 			}
3965 
3966 			ctx = 1;
3967 			symbol = 0;
3968 			while (biari_decode_symbol(dep_dp, pctx + ctx) == 0) {
3969 				symbol += 1;
3970 				ctx++;
3971 				if (ctx >= 2)
3972 					ctx = 2;
3973 			}
3974 			abslevel = symbol + 1;
3975 
3976 			if (biari_decode_symbol_eq_prob(dep_dp)) {
3977 				level = -abslevel;
3978 				sign = 1;
3979 			} else {
3980 				level = abslevel;
3981 				sign = 0;
3982 			}
3983 #if TRACE
3984 			tracebits2("level", 1, level);
3985 #endif
3986 
3987 			if (abslevel == 1)
3988 				offset = 4;
3989 			else
3990 				offset = 6;
3991 			symbol = 0;
3992 			ctx = 0;
3993 			while (biari_decode_symbol(dep_dp, pctx + ctx + offset)
3994 					== 0) {
3995 				symbol += 1;
3996 				ctx++;
3997 				if (ctx >= 1)
3998 					ctx = 1;
3999 			}
4000 			run = symbol;
4001 
4002 #if TRACE
4003 			tracebits2("run", 1, run);
4004 #endif
4005 			dct_level[pairs] = level;
4006 			dct_run[pairs] = run;
4007 			if (abslevel > t_chr[rank]) {
4008 				if (abslevel <= 2)
4009 					rank = abslevel;
4010 				else if (abslevel <= 4)
4011 					rank = 3;
4012 				else
4013 					rank = 4;
4014 			}
4015 			pos += (run + 1);
4016 			if (pos >= 64)
4017 				pos = 63;
4018 		}
4019 		dct_pairs = pairs;
4020 		pair_pos = dct_pairs;
4021 	}
4022 
4023 	if (dct_pairs > 0) {
4024 		se->value1 = dct_level[pair_pos - 1];
4025 		se->value2 = dct_run[pair_pos - 1];
4026 		pair_pos--;
4027 	} else {
4028 
4029 		se->value1 = se->value2 = 0;
4030 	}
4031 
4032 	if ((dct_pairs--) == 0)
4033 		pair_pos = 0;
4034 }
4035 
4036 int b8_ctr;
4037 #if 0
4038 int curr_residual_chroma[4][16][16];
4039 int curr_residual_luma[16][16];
4040 #endif
4041 
4042 const int SCAN[2][64][2] = {{{0, 0}, {0, 1}, {0, 2}, {1, 0}, {0, 3}, {0, 4}, {1,
4043 		1}, {1, 2}, {0, 5}, {0, 6}, {1, 3}, {2, 0}, {2, 1}, {0, 7}, {1,
4044 		4}, {2, 2}, {3, 0}, {1, 5}, {1, 6}, {2, 3}, {3, 1}, {3, 2}, {4,
4045 		0}, {1, 7}, {2, 4}, {4, 1}, {2, 5}, {3, 3}, {4, 2}, {2, 6}, {3,
4046 		4}, {4, 3}, {5, 0}, {5, 1}, {2, 7}, {3, 5}, {4, 4}, {5, 2}, {6,
4047 		0}, {5, 3}, {3, 6}, {4, 5}, {6, 1}, {6, 2}, {5, 4}, {3, 7}, {4,
4048 		6}, {6, 3}, {5, 5}, {4, 7}, {6, 4}, {5, 6}, {6, 5}, {5, 7}, {6,
4049 		6}, {7, 0}, {6, 7}, {7, 1}, {7, 2}, {7, 3}, {7, 4}, {7, 5}, {7,
4050 		6}, {7, 7} }, {{0, 0}, {1, 0}, {0, 1}, {0, 2}, {1, 1}, {2, 0}, {
4051 		3, 0}, {2, 1}, {1, 2}, {0, 3}, {0, 4}, {1, 3}, {2, 2}, {3, 1}, {
4052 		4, 0}, {5, 0}, {4, 1}, {3, 2}, {2, 3}, {1, 4}, {0, 5}, {0, 6}, {
4053 		1, 5}, {2, 4}, {3, 3}, {4, 2}, {5, 1}, {6, 0}, {7, 0}, {6, 1}, {
4054 		5, 2}, {4, 3}, {3, 4}, {2, 5}, {1, 6}, {0, 7}, {1, 7}, {2, 6}, {
4055 		3, 5}, {4, 4}, {5, 3}, {6, 2}, {7, 1}, {7, 2}, {6, 3}, {5, 4}, {
4056 		4, 5}, {3, 6}, {2, 7}, {3, 7}, {4, 6}, {5, 5}, {6, 4}, {7, 3}, {
4057 		7, 4}, {6, 5}, {5, 6}, {4, 7}, {5, 7}, {6, 6}, {7, 5}, {7, 6}, {
4058 		6, 7}, {7, 7} } };
4059 
4060 const int SCAN_4x4[16][2] = {{0, 0}, {1, 0}, {0, 1}, {0, 2}, {1, 1}, {2, 0}, {3,
4061 		0}, {2, 1}, {1, 2}, {0, 3}, {1, 3}, {2, 2}, {3, 1}, {3, 2}, {2,
4062 		3}, {3, 3} };
4063 
4064 /*
4065  *************************************************************************
4066  * Function:
4067  * Input:
4068  * Output:
4069  * Return:
4070  * Attention:
4071  *************************************************************************
4072  */
4073 
encode_golomb_word(unsigned int symbol,unsigned int grad0,unsigned int max_levels,unsigned int * res_bits,unsigned int * res_len)4074 void encode_golomb_word(unsigned int symbol, unsigned int grad0,
4075 		unsigned int max_levels, unsigned int *res_bits,
4076 		unsigned int *res_len)
4077 {
4078 	unsigned int level, res, numbits;
4079 
4080 	res = 1UL << grad0;
4081 	level = 1UL;
4082 	numbits = 1UL + grad0;
4083 
4084 	while (symbol >= res && level < max_levels) {
4085 		symbol -= res;
4086 		res = res << 1;
4087 		level++;
4088 		numbits += 2UL;
4089 	}
4090 
4091 	if (level >= max_levels) {
4092 		if (symbol >= res)
4093 			symbol = res - 1UL;
4094 	}
4095 
4096 	*res_bits = res | symbol;
4097 	*res_len = numbits;
4098 }
4099 
4100 /*
4101  *************************************************************************
4102  * Function:
4103  * Input:
4104  * Output:
4105  * Return:
4106  * Attention:
4107  *************************************************************************
4108  */
4109 
encode_multilayer_golomb_word(unsigned int symbol,const unsigned int * grad,const unsigned int * max_levels,unsigned int * res_bits,unsigned int * res_len)4110 void encode_multilayer_golomb_word(unsigned int symbol,
4111 		const unsigned int *grad, const unsigned int *max_levels,
4112 		unsigned int *res_bits, unsigned int *res_len)
4113 {
4114 	unsigned int accbits, acclen, bits, len, tmp;
4115 
4116 	accbits = acclen = 0UL;
4117 
4118 	while (1) {
4119 		encode_golomb_word(symbol, *grad, *max_levels, &bits, &len);
4120 		accbits = (accbits << len) | bits;
4121 		acclen += len;
4122 #ifdef AVSP_LONG_CABAC
4123 #else
4124 		assert(acclen <= 32UL);
4125 #endif
4126 		tmp = *max_levels - 1UL;
4127 
4128 		if (!((len == (tmp << 1) + (*grad))
4129 				&& (bits == (1UL << (tmp + *grad)) - 1UL)))
4130 			break;
4131 
4132 		tmp = *max_levels;
4133 		symbol -= (((1UL << tmp) - 1UL) << (*grad)) - 1UL;
4134 		grad++;
4135 		max_levels++;
4136 	}
4137 	*res_bits = accbits;
4138 	*res_len = acclen;
4139 }
4140 
4141 /*
4142  *************************************************************************
4143  * Function:
4144  * Input:
4145  * Output:
4146  * Return:
4147  * Attention:
4148  *************************************************************************
4149  */
4150 
writesyntaxelement_golomb(struct syntaxelement * se,int write_to_stream)4151 int writesyntaxelement_golomb(struct syntaxelement *se, int write_to_stream)
4152 {
4153 	unsigned int bits, len, i;
4154 	unsigned int grad[4], max_lev[4];
4155 
4156 	if (!(se->golomb_maxlevels & ~0xFF))
4157 		encode_golomb_word(se->value1, se->golomb_grad,
4158 				se->golomb_maxlevels, &bits, &len);
4159 	else {
4160 		for (i = 0UL; i < 4UL; i++) {
4161 			grad[i] = (se->golomb_grad >> (i << 3)) & 0xFFUL;
4162 			max_lev[i] = (se->golomb_maxlevels >> (i << 3))
4163 					& 0xFFUL;
4164 		}
4165 		encode_multilayer_golomb_word(se->value1, grad, max_lev, &bits,
4166 				&len);
4167 	}
4168 
4169 	se->len = len;
4170 	se->bitpattern = bits;
4171 
4172 	if (write_to_stream)
4173 		push_es(bits, len);
4174 	return se->len;
4175 }
4176 
4177 /*
4178  *************************************************************************
4179  * Function:Get coded block pattern and coefficients (run/level)
4180  from the bitstream
4181  * Input:
4182  * Output:
4183  * Return:
4184  * Attention:
4185  *************************************************************************
4186  */
4187 
read_cbpandcoeffsfrom_nal(struct img_par * img)4188 void read_cbpandcoeffsfrom_nal(struct img_par *img)
4189 {
4190 
4191 	int tablenum;
4192 	int inumblk;
4193 	int inumcoeff;
4194 	int symbol2D;
4195 	int escape_level_diff;
4196 	const int (*AVS_2DVLC_table_intra)[26][27];
4197 	const int (*AVS_2DVLC_table_chroma)[26][27];
4198 	int write_to_stream;
4199 	struct syntaxelement currse_enc;
4200 	struct syntaxelement *e_currse = &currse_enc;
4201 
4202 	int coeff_save[65][2];
4203 	int coeff_ptr;
4204 
4205 	int ii, jj;
4206 	int mb_nr = img->current_mb_nr;
4207 
4208 	int m2, jg2;
4209 	struct macroblock *curr_mb = &mb_data[mb_nr];
4210 
4211 	int block8x8;
4212 
4213 	int block_x, block_y;
4214 
4215 	struct slice_s *currslice = img->current_slice;
4216 	int level, run, coef_ctr, len, k, i0, j0, uv, qp;
4217 
4218 	int boff_x, boff_y, start_scan;
4219 	struct syntaxelement curr_se;
4220 	struct datapartition *dp;
4221 
4222 	AVS_2DVLC_table_intra = AVS_2DVLC_INTRA;
4223 	AVS_2DVLC_table_chroma = AVS_2DVLC_CHROMA;
4224 	write_to_stream = 1;
4225 
4226 	dct_pairs = -1;
4227 
4228 	curr_mb->qp = img->qp;
4229 	qp = curr_mb->qp;
4230 
4231 
4232 	for (block_y = 0; block_y < 4; block_y += 2) {/* all modes */
4233 	for (block_x = 0; block_x < 4; block_x += 2) {
4234 		block8x8 = 2 * (block_y / 2) + block_x / 2;
4235 		if (curr_mb->cbp & (1 << block8x8)) {
4236 			tablenum = 0;
4237 			inumblk = 1;
4238 			inumcoeff = 65;
4239 			coeff_save[0][0] = 0;
4240 			coeff_save[0][1] = 0;
4241 			coeff_ptr = 1;
4242 
4243 			b8_ctr = block8x8;
4244 
4245 			boff_x = (block8x8 % 2) << 3;
4246 			boff_y = (block8x8 / 2) << 3;
4247 
4248 			img->subblock_x = boff_x >> 2;
4249 			img->subblock_y = boff_y >> 2;
4250 
4251 			start_scan = 0;
4252 			coef_ctr = start_scan - 1;
4253 			level = 1;
4254 			img->is_v_block = 0;
4255 			img->is_intra_block = IS_INTRA(curr_mb);
4256 			for (k = start_scan;
4257 				(k < 65) && (level != 0);
4258 				k++) {
4259 
4260 				curr_se.context = LUMA_8x8;
4261 				curr_se.type =
4262 				(IS_INTRA(curr_mb)) ?
4263 					SE_LUM_AC_INTRA :
4264 					SE_LUM_AC_INTER;
4265 
4266 				dp = &(currslice->part_arr[0]);
4267 				curr_se.reading =
4268 					readrunlevel_aec_ref;
4269 				dp->
4270 				read_syntax_element(&curr_se,
4271 					img, dp);
4272 				level = curr_se.value1;
4273 				run = curr_se.value2;
4274 				len = curr_se.len;
4275 
4276 				if (level != 0) {
4277 					coeff_save[coeff_ptr][0] =
4278 					run;
4279 					coeff_save[coeff_ptr][1] =
4280 					level;
4281 					coeff_ptr++;
4282 				}
4283 
4284 
4285 
4286 				if (level != 0) {/* leave if len = 1 */
4287 					coef_ctr += run + 1;
4288 					if ((img->picture_structure
4289 						== FRAME)) {
4290 						ii =
4291 						SCAN[img->picture_structure]
4292 						[coef_ctr][0];
4293 						jj =
4294 						SCAN[img->picture_structure]
4295 						[coef_ctr][1];
4296 					} else {
4297 						ii =
4298 						SCAN[img->picture_structure]
4299 						[coef_ctr][0];
4300 						jj =
4301 						SCAN[img->picture_structure]
4302 						[coef_ctr][1];
4303 					}
4304 
4305 				}
4306 			}
4307 
4308 			while (coeff_ptr > 0) {
4309 				run =
4310 						coeff_save[coeff_ptr
4311 								- 1][0];
4312 				level =
4313 						coeff_save[coeff_ptr
4314 								- 1][1];
4315 
4316 				coeff_ptr--;
4317 
4318 				symbol2D = CODE2D_ESCAPE_SYMBOL;
4319 				if (level > -27 && level < 27
4320 					&& run < 26) {
4321 					if (tablenum == 0)
4322 
4323 						symbol2D =
4324 						AVS_2DVLC_table_intra
4325 						[tablenum]
4326 						[run][abs(
4327 						level)
4328 						- 1];
4329 					else
4330 
4331 						symbol2D =
4332 						AVS_2DVLC_table_intra
4333 						[tablenum]
4334 						[run][abs(
4335 					level)];
4336 					if (symbol2D >= 0
4337 							&& level
4338 									< 0)
4339 						symbol2D++;
4340 					if (symbol2D < 0)
4341 
4342 						symbol2D =
4343 						(CODE2D_ESCAPE_SYMBOL
4344 						+ (run
4345 						<< 1)
4346 						+ ((level
4347 						> 0) ?
4348 						1 :
4349 						0));
4350 				}
4351 
4352 				else {
4353 
4354 					symbol2D =
4355 						(CODE2D_ESCAPE_SYMBOL
4356 						+ (run
4357 						<< 1)
4358 						+ ((level
4359 						> 0) ?
4360 						1 :
4361 						0));
4362 				}
4363 
4364 
4365 
4366 				e_currse->type = SE_LUM_AC_INTER;
4367 				e_currse->value1 = symbol2D;
4368 				e_currse->value2 = 0;
4369 
4370 				e_currse->golomb_grad =
4371 						vlc_golomb_order
4372 						[0][tablenum][0];
4373 				e_currse->golomb_maxlevels =
4374 						vlc_golomb_order
4375 						[0][tablenum][1];
4376 
4377 				writesyntaxelement_golomb(
4378 						e_currse,
4379 						write_to_stream);
4380 
4381 				if (symbol2D
4382 						>= CODE2D_ESCAPE_SYMBOL) {
4383 
4384 					e_currse->type =
4385 							SE_LUM_AC_INTER;
4386 					e_currse->golomb_grad =
4387 							1;
4388 					e_currse->golomb_maxlevels =
4389 							11;
4390 					escape_level_diff =
4391 						abs(
4392 						level)
4393 						- ((run
4394 						> MaxRun[0][tablenum]) ?
4395 						1 :
4396 						refabslevel[tablenum][run]);
4397 					e_currse->value1 =
4398 							escape_level_diff;
4399 
4400 					writesyntaxelement_golomb(
4401 							e_currse,
4402 							write_to_stream);
4403 
4404 				}
4405 
4406 				if (abs(level)
4407 					> incvlc_intra[tablenum]) {
4408 					if (abs(level) <= 2)
4409 						tablenum =
4410 						abs(
4411 						level);
4412 					else if (abs(level) <= 4)
4413 						tablenum = 3;
4414 					else if (abs(level) <= 7)
4415 						tablenum = 4;
4416 					else if (abs(level)
4417 							<= 10)
4418 						tablenum = 5;
4419 					else
4420 						tablenum = 6;
4421 				}
4422 			}
4423 
4424 
4425 		}
4426 		}
4427 	}
4428 
4429 
4430 
4431 	m2 = img->mb_x * 2;
4432 	jg2 = img->mb_y * 2;
4433 
4434 
4435 	uv = -1;
4436 	block_y = 4;
4437 #if 0
4438 	qp = QP_SCALE_CR[curr_mb->qp];
4439 #endif
4440 	for (block_x = 0; block_x < 4; block_x += 2) {
4441 
4442 		uv++;
4443 
4444 
4445 		b8_ctr = (uv + 4);
4446 		if ((curr_mb->cbp >> (uv + 4)) & 0x1) {
4447 
4448 			tablenum = 0;
4449 			inumblk = 1;
4450 			inumcoeff = 65;
4451 			coeff_save[0][0] = 0;
4452 			coeff_save[0][1] = 0;
4453 			coeff_ptr = 1;
4454 
4455 			coef_ctr = -1;
4456 			level = 1;
4457 			img->subblock_x = 0;
4458 			img->subblock_y = 0;
4459 			curr_se.context = CHROMA_AC;
4460 			curr_se.type = (IS_INTRA(curr_mb) ?
4461 					SE_CHR_AC_INTRA :
4462 					SE_CHR_AC_INTER);
4463 			dp = &(currslice->part_arr[0]);
4464 			curr_se.reading = readrunlevel_aec_ref;
4465 			img->is_v_block = uv;
4466 			img->is_intra_block = IS_INTRA(curr_mb);
4467 			for (k = 0; (k < 65) && (level != 0); k++) {
4468 
4469 				dp->read_syntax_element
4470 				(&curr_se, img, dp);
4471 				level = curr_se.value1;
4472 				run = curr_se.value2;
4473 				len = curr_se.len;
4474 
4475 				if (level != 0) {
4476 					coeff_save[coeff_ptr][0] = run;
4477 					coeff_save[coeff_ptr][1] =
4478 							level;
4479 					coeff_ptr++;
4480 				}
4481 
4482 
4483 				if (level != 0) {
4484 					coef_ctr = coef_ctr + run + 1;
4485 					if ((img->picture_structure
4486 						== FRAME)
4487 						/*&& (!curr_mb->mb_field)*/) {
4488 						i0 =
4489 						SCAN[img->picture_structure]
4490 						[coef_ctr][0];
4491 						j0 =
4492 						SCAN[img->picture_structure]
4493 						[coef_ctr][1];
4494 					} else {
4495 						i0 =
4496 						SCAN[img->picture_structure]
4497 						[coef_ctr][0];
4498 						j0 =
4499 						SCAN[img->picture_structure]
4500 						[coef_ctr][1];
4501 					}
4502 
4503 				}
4504 			}
4505 
4506 			while (coeff_ptr > 0) {
4507 
4508 				run = coeff_save[coeff_ptr - 1][0];
4509 				level = coeff_save[coeff_ptr - 1][1];
4510 
4511 				coeff_ptr--;
4512 
4513 				symbol2D = CODE2D_ESCAPE_SYMBOL;
4514 				if (level > -27 && level < 27
4515 						&& run < 26) {
4516 					if (tablenum == 0)
4517 
4518 						symbol2D =
4519 						AVS_2DVLC_table_chroma
4520 						[tablenum][run][abs(
4521 						level)
4522 						- 1];
4523 					else
4524 						symbol2D =
4525 							AVS_2DVLC_table_chroma
4526 							[tablenum][run][abs(
4527 							level)];
4528 					if (symbol2D >= 0
4529 						&& level < 0)
4530 						symbol2D++;
4531 					if (symbol2D < 0)
4532 						symbol2D =
4533 						(CODE2D_ESCAPE_SYMBOL
4534 						+ (run
4535 						<< 1)
4536 						+ ((level
4537 						> 0) ?
4538 						1 :
4539 						0));
4540 				}
4541 
4542 				else {
4543 					symbol2D =
4544 					(CODE2D_ESCAPE_SYMBOL
4545 					+ (run
4546 					<< 1)
4547 					+ ((level
4548 					> 0) ?
4549 					1 :
4550 					0));
4551 				}
4552 
4553 				e_currse->type = SE_LUM_AC_INTER;
4554 				e_currse->value1 = symbol2D;
4555 				e_currse->value2 = 0;
4556 				e_currse->golomb_grad =
4557 						vlc_golomb_order[2]
4558 						[tablenum][0];
4559 				e_currse->golomb_maxlevels =
4560 						vlc_golomb_order[2]
4561 						[tablenum][1];
4562 
4563 				writesyntaxelement_golomb(e_currse,
4564 						write_to_stream);
4565 
4566 				/*
4567 				 * if (write_to_stream)
4568 				 * {
4569 				 * bitCount[BITS_COEFF_UV_MB]+=e_currse->len;
4570 				 * e_currse++;
4571 				 * curr_mb->currSEnr++;
4572 				 * }
4573 				 * no_bits+=e_currse->len;
4574 
4575 
4576 				 * if (icoef == 0) break;
4577 				 */
4578 
4579 				if (symbol2D >= CODE2D_ESCAPE_SYMBOL) {
4580 
4581 					e_currse->type = SE_LUM_AC_INTER;
4582 					e_currse->golomb_grad = 0;
4583 					e_currse->golomb_maxlevels = 11;
4584 					escape_level_diff =
4585 						abs(level)
4586 						- ((run
4587 						> MaxRun[2][tablenum]) ?
4588 						1 :
4589 						refabslevel[tablenum
4590 						+ 14][run]);
4591 					e_currse->value1 =
4592 							escape_level_diff;
4593 
4594 					writesyntaxelement_golomb(
4595 							e_currse,
4596 							write_to_stream);
4597 
4598 				}
4599 
4600 				if (abs(level)
4601 				> incvlc_chroma[tablenum]) {
4602 					if (abs(level) <= 2)
4603 						tablenum = abs(level);
4604 					else if (abs(level) <= 4)
4605 						tablenum = 3;
4606 					else
4607 						tablenum = 4;
4608 				}
4609 			}
4610 
4611 		}
4612 	}
4613 }
4614 
4615 /*
4616  *************************************************************************
4617  * Function:Get the syntax elements from the NAL
4618  * Input:
4619  * Output:
4620  * Return:
4621  * Attention:
4622  *************************************************************************
4623  */
4624 
read_one_macroblock(struct img_par * img)4625 int read_one_macroblock(struct img_par *img)
4626 {
4627 	int i, j;
4628 
4629 	struct syntaxelement curr_se;
4630 	struct macroblock *curr_mb = &mb_data[img->current_mb_nr];
4631 
4632 	int cabp_flag;
4633 
4634 	int tempcbp;
4635 	int fixqp;
4636 
4637 	struct slice_s *currslice = img->current_slice;
4638 	struct datapartition *dp;
4639 
4640 	fixqp = (fixed_picture_qp || fixed_slice_qp);
4641 
4642 	for (i = 0; i < 8; i++)
4643 		for (j = 0; j < 8; j++) {
4644 			img->m8[0][i][j] = 0;
4645 			img->m8[1][i][j] = 0;
4646 			img->m8[2][i][j] = 0;
4647 			img->m8[3][i][j] = 0;
4648 		}
4649 
4650 	current_mb_skip = 0;
4651 
4652 	curr_mb->qp = img->qp;
4653 	curr_se.type = SE_MBTYPE;
4654 	curr_se.mapping = linfo_ue;
4655 
4656 	curr_mb->mb_type_2 = 0;
4657 
4658 	if (img->type == I_IMG)
4659 		curr_mb->mb_type = 0;
4660 
4661 	interpret_mb_mode_i(img);
4662 
4663 	init_macroblock(img);
4664 
4665 	if ((IS_INTRA(curr_mb)) && (img->abt_flag)) {
4666 
4667 #if TRACE
4668 		strncpy(curr_se.tracestring, "cabp_flag", TRACESTRING_SIZE);
4669 #endif
4670 
4671 		curr_se.len = 1;
4672 		curr_se.type = SE_CABP;
4673 		read_syntaxelement_flc(&curr_se);
4674 		cabp_flag = curr_se.value1;
4675 		if (cabp_flag == 0) {
4676 			curr_mb->CABP[0] = 0;
4677 			curr_mb->CABP[1] = 0;
4678 			curr_mb->CABP[2] = 0;
4679 			curr_mb->CABP[3] = 0;
4680 		} else {
4681 			for (i = 0; i < 4; i++) {
4682 				curr_se.len = 1;
4683 				curr_se.type = SE_CABP;
4684 				read_syntaxelement_flc(&curr_se);
4685 				curr_mb->CABP[i] = curr_se.value1;
4686 			}
4687 		}
4688 
4689 	} else {
4690 		curr_mb->CABP[0] = 0;
4691 		curr_mb->CABP[1] = 0;
4692 		curr_mb->CABP[2] = 0;
4693 		curr_mb->CABP[3] = 0;
4694 
4695 	}
4696 
4697 	if (IS_INTRA(curr_mb)) {
4698 		for (i = 0; i < /*5*/(chroma_format + 4); i++)
4699 
4700 			read_ipred_block_modes(img, i);
4701 	}
4702 
4703 	curr_se.type = SE_CBP_INTRA;
4704 	curr_se.mapping = linfo_cbp_intra;
4705 
4706 #if TRACE
4707 	snprintf(curr_se.tracestring, TRACESTRING_SIZE, "CBP");
4708 #endif
4709 
4710 	if (img->type == I_IMG || IS_INTER(curr_mb)) {
4711 		curr_se.golomb_maxlevels = 0;
4712 
4713 		if (1) {
4714 			dp = &(currslice->part_arr[0]);
4715 			curr_se.reading = readcp_aec;
4716 			dp->read_syntax_element(&curr_se, img, dp);
4717 		}
4718 
4719 
4720 		curr_mb->cbp = curr_se.value1;
4721 		push_es(UE[NCBP[curr_se.value1][0]][0],
4722 				UE[NCBP[curr_se.value1][0]][1]);
4723 
4724 	}
4725 
4726 # if 1
4727 	if (curr_mb->cbp != 0)
4728 		tempcbp = 1;
4729 	else
4730 		tempcbp = 0;
4731 #else
4732 
4733 	if (chroma_format == 2)	{
4734 #if TRACE
4735 		snprintf(curr_se.tracestring, TRACESTRING_SIZE, "CBP422");
4736 #endif
4737 		curr_se.mapping = /*linfo_se*/linfo_ue;
4738 		curr_se.type = SE_CBP_INTRA;
4739 		readsyntaxelement_uvlc(&curr_se, inp);
4740 		curr_mb->cbp01 = curr_se.value1;
4741 		io_printf(" * UE cbp01 read : 0x%02X\n", curr_mb->cbp01);
4742 	}
4743 
4744 	if (chroma_format == 2)	{
4745 		if (curr_mb->cbp != 0 || curr_mb->cbp01 != 0)
4746 			tempcbp = 1;
4747 		else
4748 			tempcbp = 0;
4749 
4750 	} else {
4751 		if (curr_mb->cbp != 0)
4752 			tempcbp = 1;
4753 		else
4754 			tempcbp = 0;
4755 	}
4756 
4757 #endif
4758 
4759 	if (IS_INTRA(curr_mb) && (img->abt_flag) && (curr_mb->cbp & (0xF))) {
4760 		curr_mb->CABT[0] = curr_mb->CABP[0];
4761 		curr_mb->CABT[1] = curr_mb->CABP[1];
4762 		curr_mb->CABT[2] = curr_mb->CABP[2];
4763 		curr_mb->CABT[3] = curr_mb->CABP[3];
4764 	} else {
4765 
4766 		curr_mb->CABT[0] = 0;
4767 		curr_mb->CABT[1] = 0;
4768 		curr_mb->CABT[2] = 0;
4769 		curr_mb->CABT[3] = 0;
4770 
4771 		if (!fixqp && (tempcbp)) {
4772 			if (IS_INTER(curr_mb))
4773 				curr_se.type = SE_DELTA_QUANT_INTER;
4774 			else
4775 				curr_se.type = SE_DELTA_QUANT_INTRA;
4776 
4777 #if TRACE
4778 			snprintf(curr_se.tracestring,
4779 				TRACESTRING_SIZE, "Delta quant ");
4780 #endif
4781 
4782 			if (1) {
4783 				dp = &(currslice->part_arr[0]);
4784 				curr_se.reading = readdquant_aec;
4785 				dp->read_syntax_element(&curr_se, img, dp);
4786 			}
4787 
4788 			curr_mb->delta_quant = curr_se.value1;
4789 #ifdef DUMP_DEBUG
4790 			if (avs_get_debug_flag() & MB_INFO_DUMP) {
4791 				io_printf(" * SE delta_quant read : %d\n",
4792 					curr_mb->delta_quant);
4793 			}
4794 #endif
4795 			CHECKDELTAQP
4796 
4797 			if (transcoding_error_flag)
4798 				return -1;
4799 
4800 			img->qp = (img->qp - MIN_QP + curr_mb->delta_quant
4801 					+ (MAX_QP - MIN_QP + 1))
4802 					% (MAX_QP - MIN_QP + 1) + MIN_QP;
4803 		}
4804 
4805 		if (fixqp) {
4806 			curr_mb->delta_quant = 0;
4807 			img->qp = (img->qp - MIN_QP + curr_mb->delta_quant
4808 					+ (MAX_QP - MIN_QP + 1))
4809 					% (MAX_QP - MIN_QP + 1) + MIN_QP;
4810 
4811 		}
4812 #ifdef DUMP_DEBUG
4813 		if (avs_get_debug_flag() & MB_INFO_DUMP)
4814 			io_printf(" - img->qp : %d\n", img->qp);
4815 #endif
4816 	}
4817 
4818 	read_cbpandcoeffsfrom_nal(img);
4819 	return DECODE_MB;
4820 }
4821 
4822 /*!
4823  ************************************************************************
4824  * \brief
4825  *    finding end of a slice in case this is not the end of a frame
4826  *
4827  * Unsure whether the "correction" below actually solves an off-by-one
4828  * problem or whether it introduces one in some cases :-(  Anyway,
4829  * with this change the bit stream format works with AEC again.
4830  * StW, 8.7.02
4831  ************************************************************************
4832  */
aec_startcode_follows(struct img_par * img,int eos_bit)4833 int aec_startcode_follows(struct img_par *img, int eos_bit)
4834 {
4835 	struct slice_s *currslice = img->current_slice;
4836 	struct datapartition *dp;
4837 	unsigned int bit;
4838 	struct decoding_environment_s *dep_dp;
4839 
4840 	dp = &(currslice->part_arr[0]);
4841 	dep_dp = &(dp->de_aec);
4842 
4843 	if (eos_bit)
4844 		bit = biari_decode_final(dep_dp);
4845 	else
4846 		bit = 0;
4847 
4848 	return bit == 1 ? 1 : 0;
4849 }
4850 
4851 #ifdef AVSP_LONG_CABAC
process_long_cabac(void)4852 int process_long_cabac(void)
4853 #else
4854 void main(void)
4855 #endif
4856 {
4857 	int data32;
4858 	int current_header;
4859 	int i;
4860 	int tmp;
4861 	int ret;
4862 
4863 	int byte_startposition;
4864 	int aec_mb_stuffing_bit;
4865 	struct slice_s *currslice;
4866 #ifdef PERFORMANCE_DEBUG
4867 	pr_info("enter %s\r\n", __func__);
4868 #endif
4869 	transcoding_error_flag = 0;
4870 	ret = 0;
4871 	es_buf = es_write_addr_virt;
4872 
4873 	if (local_heap_init(MAX_CODED_FRAME_SIZE * 4) < 0) {
4874 		ret = -1;
4875 		goto End;
4876 	}
4877 
4878 	img = (struct img_par *)local_alloc(1, sizeof(struct img_par));
4879 	if (img	== NULL) {
4880 		no_mem_exit("main: img");
4881 		ret = -1;
4882 		goto End;
4883 	}
4884 	stat_bits_ptr = (struct stat_bits *)local_alloc(1,
4885 			sizeof(struct stat_bits));
4886 	if (stat_bits_ptr == NULL) {
4887 		no_mem_exit("main: stat_bits");
4888 		ret = -1;
4889 		goto End;
4890 	}
4891 
4892 	curr_stream = alloc_bitstream();
4893 	if (curr_stream == NULL) {
4894 		io_printf("alloc bitstream failed\n");
4895 		ret = -1;
4896 		goto End;
4897 	}
4898 
4899 	chroma_format = 1;
4900 	demulate_enable = 0;
4901 	img->seq_header_indicate = 1;
4902 
4903 #ifdef AVSP_LONG_CABAC
4904 	data32 = READ_VREG(LONG_CABAC_REQ);
4905 	progressive_sequence = (data32 >> 1) & 1;
4906 	fixed_picture_qp = (data32 >> 2) & 1;
4907 	img->picture_structure = (data32 >> 3) & 1;
4908 	img->type = (data32 >> 4) & 3;
4909 	skip_mode_flag = (data32 >> 6) & 1;
4910 
4911 	src_start = READ_VREG(LONG_CABAC_SRC_ADDR);
4912 	des_start = READ_VREG(LONG_CABAC_DES_ADDR);
4913 
4914 	data32 = READ_VREG(LONG_CABAC_PIC_SIZE);
4915 	horizontal_size = (data32 >> 0) & 0xffff;
4916 	vertical_size = (data32 >> 16) & 0xffff;
4917 	if (horizontal_size * vertical_size > 1920 * 1080) {
4918 		io_printf("pic size check failed: width = %d, height = %d\n",
4919 			horizontal_size, vertical_size);
4920 		ret = -1;
4921 		goto End;
4922 	}
4923 
4924 	vld_mem_start_addr = READ_VREG(VLD_MEM_VIFIFO_START_PTR);
4925 	vld_mem_end_addr = READ_VREG(VLD_MEM_VIFIFO_END_PTR);
4926 
4927 #else
4928 	progressive_sequence = 0;
4929 	fixed_picture_qp = 0;
4930 	img->picture_structure = 0;
4931 	img->type = I_IMG;
4932 	skip_mode_flag = 1;
4933 	horizontal_size = 1920;
4934 	vertical_size = 1080;
4935 
4936 	src_start = 0;
4937 #endif
4938 
4939 	if (horizontal_size % 16 != 0)
4940 		img->auto_crop_right = 16 - (horizontal_size % 16);
4941 	else
4942 		img->auto_crop_right = 0;
4943 
4944 	if (!progressive_sequence) {
4945 		if (vertical_size % 32 != 0)
4946 			img->auto_crop_bottom = 32 - (vertical_size % 32);
4947 		else
4948 			img->auto_crop_bottom = 0;
4949 	} else {
4950 		if (vertical_size % 16 != 0)
4951 			img->auto_crop_bottom = 16 - (vertical_size % 16);
4952 		else
4953 			img->auto_crop_bottom = 0;
4954 	}
4955 
4956 	img->width = (horizontal_size + img->auto_crop_right);
4957 	if (img->picture_structure)
4958 		img->height = (vertical_size + img->auto_crop_bottom);
4959 	else
4960 		img->height = (vertical_size + img->auto_crop_bottom) / 2;
4961 	img->width_cr = (img->width >> 1);
4962 
4963 	img->pic_width_inmbs = img->width / MB_BLOCK_SIZE;
4964 	img->pic_height_inmbs = img->height / MB_BLOCK_SIZE;
4965 	img->pic_size_inmbs = img->pic_width_inmbs * img->pic_height_inmbs;
4966 
4967 	io_printf(
4968 			"[LONG CABAC] Start Transcoding from 0x%x to 0x%x Size : %d x %d\r\n",
4969 			src_start, des_start, horizontal_size, vertical_size);
4970 #if 0
4971 	io_printf("VLD_MEM_VIFIFO_START_PTR %x\r\n",
4972 			READ_VREG(VLD_MEM_VIFIFO_START_PTR));
4973 	io_printf("VLD_MEM_VIFIFO_CURR_PTR %x\r\n",
4974 			READ_VREG(VLD_MEM_VIFIFO_CURR_PTR));
4975 	io_printf("VLD_MEM_VIFIFO_END_PTR %x\r\n",
4976 			READ_VREG(VLD_MEM_VIFIFO_END_PTR));
4977 	io_printf("VLD_MEM_VIFIFO_WP %x\r\n",
4978 			READ_VREG(VLD_MEM_VIFIFO_WP));
4979 	io_printf("VLD_MEM_VIFIFO_RP %x\r\n",
4980 			READ_VREG(VLD_MEM_VIFIFO_RP));
4981 	io_printf("VLD_MEM_VBUF_RD_PTR %x\r\n",
4982 			READ_VREG(VLD_MEM_VBUF_RD_PTR));
4983 	io_printf("VLD_MEM_VIFIFO_BUF_CNTL %x\r\n",
4984 			READ_VREG(VLD_MEM_VIFIFO_BUF_CNTL));
4985 #endif
4986 	io_printf(
4987 			"[LONG CABAC] progressive_sequence : %d, fixed_picture_qp : %d, skip_mode_flag : %d\r\n",
4988 			progressive_sequence, fixed_picture_qp, skip_mode_flag);
4989 	io_printf("[LONG CABAC] picture_structure : %d, picture_type : %d\r\n",
4990 			img->picture_structure, img->type);
4991 
4992 	open_irabs(p_irabs);
4993 
4994 
4995 	if (initial_decode() == 0) {
4996 		io_printf("initial_decode failed\n");
4997 		ret = -1;
4998 		goto End;
4999 	}
5000 
5001 	init_es();
5002 
5003 	current_header = header();
5004 	io_printf("[LONG CABAC] header Return : %d\n", current_header);
5005 
5006 	tmp = slice_header(temp_slice_buf, first_slice_startpos,
5007 			first_slice_length);
5008 
5009 	init_contexts(img);
5010 	aec_new_slice();
5011 	byte_startposition = (curr_stream->frame_bitoffset) / 8;
5012 
5013 	currslice = img->current_slice;
5014 
5015 	if (1) {
5016 		for (i = 0; i < 1; i++) {
5017 			img->current_slice->part_arr[i].read_syntax_element =
5018 					read_syntaxelement_aec;
5019 			img->current_slice->part_arr[i].bitstream = curr_stream;
5020 		}
5021 		curr_stream = currslice->part_arr[0].bitstream;
5022 	}
5023 	if ((curr_stream->frame_bitoffset) % 8 != 0)
5024 		byte_startposition++;
5025 
5026 	arideco_start_decoding(&img->current_slice->part_arr[0].de_aec,
5027 			curr_stream->stream_buffer, (byte_startposition),
5028 			&(curr_stream->read_len), img->type);
5029 
5030 	img->current_mb_nr = 0;
5031 	total_mb_count = 0;
5032 	while (img->current_mb_nr < img->pic_size_inmbs)
5033 
5034 	{
5035 		start_macroblock(img);
5036 		if (-1 == read_one_macroblock(img)) {
5037 			ret = -1;
5038 			pr_info("macroblock trans failed, exit\n");
5039 			goto End;
5040 		}
5041 		if (img->cod_counter <= 0)
5042 			aec_mb_stuffing_bit = aec_startcode_follows(img, 1);
5043 		img->current_mb_nr++;
5044 	}
5045 
5046 	push_es(0xff, 8);
5047 	io_printf(" Total ES_LENGTH : %d\n", es_ptr);
5048 
5049 #ifdef AVSP_LONG_CABAC
5050 	push_es(0xff, 64);
5051 	if (es_buf_is_overflow) {
5052 		io_printf("fatal error: es_buf_is_overflow\n");
5053 		ret = -1;
5054 		goto End;
5055 	}
5056 
5057 	if (transcoding_error_flag == 0) {
5058 #if 1
5059 		dma_sync_single_for_device(amports_get_dma_device(),
5060 			es_write_addr_phy,
5061 			es_ptr, DMA_TO_DEVICE);
5062 
5063 		wmb(); /**/
5064 #endif
5065 	}
5066 #else
5067 	fclose(f_es);
5068 #endif
5069 
5070 End:
5071 #ifdef AVSP_LONG_CABAC
5072 	WRITE_VREG(LONG_CABAC_REQ, 0);
5073 #endif
5074 	local_heap_uninit();
5075 #ifdef PERFORMANCE_DEBUG
5076 	pr_info("exit %s\r\n", __func__);
5077 #endif
5078 	return ret;
5079 }
5080 #endif
5081