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