pnw_MPEG4.c revision 39eff7a021afd8ec1a6f375dbc6476cbaa485d5d
1/*
2 * Copyright (c) 2011 Intel Corporation. All Rights Reserved.
3 * Copyright (c) Imagination Technologies Limited, UK
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 *    Waldo Bastian <waldo.bastian@intel.com>
27 *    Li Zeng <li.zeng@intel.com>
28 *
29 */
30
31#include "pnw_MPEG4.h"
32#include "tng_vld_dec.h"
33#include "psb_def.h"
34#include "psb_drv_debug.h"
35
36#include "hwdefs/reg_io2.h"
37#include "hwdefs/msvdx_offsets.h"
38#include "hwdefs/msvdx_cmds_io2.h"
39#include "hwdefs/msvdx_vec_reg_io2.h"
40#include "hwdefs/msvdx_vec_mpeg4_reg_io2.h"
41#include "hwdefs/dxva_fw_ctrl.h"
42
43#include <stdlib.h>
44#include <stdint.h>
45#include <string.h>
46
47#define GET_SURFACE_INFO_is_defined(psb_surface) ((int) (psb_surface->extra_info[0]))
48#define SET_SURFACE_INFO_is_defined(psb_surface, val) psb_surface->extra_info[0] = (uint32_t) val;
49#define GET_SURFACE_INFO_picture_structure(psb_surface) (psb_surface->extra_info[1])
50#define SET_SURFACE_INFO_picture_structure(psb_surface, val) psb_surface->extra_info[1] = val;
51#define GET_SURFACE_INFO_picture_coding_type(psb_surface) ((int) (psb_surface->extra_info[2]))
52#define SET_SURFACE_INFO_picture_coding_type(psb_surface, val) psb_surface->extra_info[2] = (uint32_t) val;
53
54#define SLICEDATA_BUFFER_TYPE(type) ((type==VASliceDataBufferType)?"VASliceDataBufferType":"VAProtectedSliceDataBufferType")
55
56#define PIXELS_TO_MB(x)    ((x + 15) / 16)
57
58/*
59 * Frame types - format dependant!
60 */
61#define PICTURE_CODING_I    0x00
62#define PICTURE_CODING_P    0x01
63#define PICTURE_CODING_B    0x02
64#define PICTURE_CODING_S    0x03
65
66
67#define FE_STATE_BUFFER_SIZE    4096
68#define FE_STATE_SAVE_SIZE      ( 0xB40 - 0x700 )
69
70#define MPEG4_PROFILE_SIMPLE    0
71#define MPEG4_PROFILE_ASP    2
72
73#define HW_SUPPORTED_MAX_PICTURE_WIDTH_MPEG4   1920
74#define HW_SUPPORTED_MAX_PICTURE_HEIGHT_MPEG4  1088
75#define HW_SUPPORTED_MAX_PICTURE_WIDTH_H263    720
76#define HW_SUPPORTED_MAX_PICTURE_HEIGHT_H263   576
77
78/* Table V2-2 ISO/IEC 14496-2:2001(E) - sprite enable codewords */
79typedef enum {
80    SPRITE_NOT_USED = 0,
81    STATIC,
82    GMC,
83} MPEG4_eSpriteEnable;
84
85
86#define MAX_QUANT_TABLES    (2) /* only 2 tables for 4:2:0 decode */
87
88static int scan0[64] = { // spec, fig 7-2
89    /*u 0  .....                   7*/
90    0,  1,  5,  6,  14, 15, 27, 28,  /* v = 0 */
91    2,  4,  7,  13, 16, 26, 29, 42,
92    3,  8,  12, 17, 25, 30, 41, 43,
93    9,  11, 18, 24, 31, 40, 44, 53,
94    10, 19, 23, 32, 39, 45, 52, 54,
95    20, 22, 33, 38, 46, 51, 55, 60,
96    21, 34, 37, 47, 50, 56, 59, 61,
97    35, 36, 48, 49, 57, 58, 62, 63  /* v = 7 */
98};
99
100typedef enum {
101    NONINTRA_LUMA_Q = 0,
102    INTRA_LUMA_Q = 1
103} QUANT_IDX;
104
105/************************************************************************************/
106/*                Variable length codes in 'packed' format                            */
107/************************************************************************************/
108
109/* Format is: opcode, width, symbol. All VLC tables are concatenated.                 */
110#define VLC_PACK(a,b,c)         ( ( (a) << 12 ) | ( (b) << 9  ) | (c) )
111const static IMG_UINT16 gaui16mpeg4VlcTableDataPacked[] =
112{
113/* B6_mcbpc_i_s_vops_piece.out */
114	VLC_PACK( 4 , 0 , 12 ) ,
115	VLC_PACK( 5 , 0 , 7 ) ,
116	VLC_PACK( 4 , 2 , 13 ) ,
117	VLC_PACK( 4 , 3 , 16 ) ,
118	VLC_PACK( 5 , 0 , 9 ) ,
119	VLC_PACK( 4 , 5 , 17 ) ,
120	VLC_PACK( 2 , 2 , 1 ) ,
121	VLC_PACK( 3 , 2 , 0 ) ,
122	VLC_PACK( 3 , 2 , 0 ) ,
123	VLC_PACK( 4 , 2 , 36 ) ,
124	VLC_PACK( 3 , 2 , 0 ) ,
125/* B7_mcbpc_p_s_vops_update.out */
126	VLC_PACK( 4 , 0 , 0 ) ,
127	VLC_PACK( 0 , 0 , 6 ) ,
128	VLC_PACK( 0 , 0 , 7 ) ,
129	VLC_PACK( 2 , 1 , 8 ) ,
130	VLC_PACK( 0 , 1 , 10 ) ,
131	VLC_PACK( 2 , 1 , 13 ) ,
132	VLC_PACK( 0 , 2 , 15 ) ,
133	VLC_PACK( 4 , 0 , 8 ) ,
134	VLC_PACK( 4 , 0 , 4 ) ,
135	VLC_PACK( 4 , 0 , 2 ) ,
136	VLC_PACK( 4 , 0 , 1 ) ,
137	VLC_PACK( 4 , 0 , 12 ) ,
138	VLC_PACK( 4 , 1 , 3 ) ,
139	VLC_PACK( 4 , 1 , 16 ) ,
140	VLC_PACK( 4 , 1 , 10 ) ,
141	VLC_PACK( 4 , 1 , 9 ) ,
142	VLC_PACK( 4 , 1 , 6 ) ,
143	VLC_PACK( 4 , 1 , 5 ) ,
144	VLC_PACK( 4 , 0 , 15 ) ,
145	VLC_PACK( 4 , 1 , 11 ) ,
146	VLC_PACK( 4 , 1 , 13 ) ,
147	VLC_PACK( 3 , 2 , 0 ) ,
148	VLC_PACK( 4 , 2 , 36 ) ,
149	VLC_PACK( 4 , 2 , 19 ) ,
150	VLC_PACK( 4 , 2 , 18 ) ,
151	VLC_PACK( 4 , 2 , 17 ) ,
152	VLC_PACK( 4 , 2 , 7 ) ,
153	VLC_PACK( 4 , 1 , 14 ) ,
154	VLC_PACK( 4 , 1 , 14 ) ,
155/* B8_cbpy_intra.out */
156	VLC_PACK( 1 , 1 , 16 ) ,
157	VLC_PACK( 0 , 0 , 18 ) ,
158	VLC_PACK( 0 , 0 , 19 ) ,
159	VLC_PACK( 4 , 3 , 0 ) ,
160	VLC_PACK( 4 , 3 , 12 ) ,
161	VLC_PACK( 4 , 3 , 10 ) ,
162	VLC_PACK( 4 , 3 , 14 ) ,
163	VLC_PACK( 4 , 3 , 5 ) ,
164	VLC_PACK( 4 , 3 , 13 ) ,
165	VLC_PACK( 4 , 3 , 3 ) ,
166	VLC_PACK( 4 , 3 , 11 ) ,
167	VLC_PACK( 4 , 3 , 7 ) ,
168	VLC_PACK( 4 , 1 , 15 ) ,
169	VLC_PACK( 4 , 1 , 15 ) ,
170	VLC_PACK( 4 , 1 , 15 ) ,
171	VLC_PACK( 4 , 1 , 15 ) ,
172	VLC_PACK( 3 , 1 , 0 ) ,
173	VLC_PACK( 4 , 1 , 6 ) ,
174	VLC_PACK( 4 , 1 , 9 ) ,
175	VLC_PACK( 4 , 0 , 8 ) ,
176	VLC_PACK( 4 , 0 , 4 ) ,
177	VLC_PACK( 4 , 0 , 2 ) ,
178	VLC_PACK( 4 , 0 , 1 ) ,
179/* B8_cbpy_inter.out */
180	VLC_PACK( 1 , 1 , 16 ) ,
181	VLC_PACK( 0 , 0 , 18 ) ,
182	VLC_PACK( 6 , 0 , 6 ) ,
183	VLC_PACK( 4 , 3 , 15 ) ,
184	VLC_PACK( 4 , 3 , 3 ) ,
185	VLC_PACK( 4 , 3 , 5 ) ,
186	VLC_PACK( 4 , 3 , 1 ) ,
187	VLC_PACK( 4 , 3 , 10 ) ,
188	VLC_PACK( 4 , 3 , 2 ) ,
189	VLC_PACK( 4 , 3 , 12 ) ,
190	VLC_PACK( 4 , 3 , 4 ) ,
191	VLC_PACK( 4 , 3 , 8 ) ,
192	VLC_PACK( 4 , 1 , 0 ) ,
193	VLC_PACK( 4 , 1 , 0 ) ,
194	VLC_PACK( 4 , 1 , 0 ) ,
195	VLC_PACK( 4 , 1 , 0 ) ,
196	VLC_PACK( 3 , 1 , 0 ) ,
197	VLC_PACK( 4 , 1 , 9 ) ,
198	VLC_PACK( 4 , 1 , 6 ) ,
199	VLC_PACK( 4 , 0 , 7 ) ,
200	VLC_PACK( 4 , 0 , 11 ) ,
201/* B3_modb.out */
202	VLC_PACK( 4 , 0 , 0 ) ,
203	VLC_PACK( 4 , 1 , 1 ) ,
204	VLC_PACK( 4 , 1 , 2 ) ,
205/* B4_mb_type.out */
206	VLC_PACK( 4 , 0 , 0 ) ,
207	VLC_PACK( 4 , 1 , 1 ) ,
208	VLC_PACK( 4 , 2 , 2 ) ,
209	VLC_PACK( 4 , 3 , 3 ) ,
210	VLC_PACK( 3 , 3 , 0 ) ,
211/* 6_33_dbquant.out */
212	VLC_PACK( 4 , 0 , 0 ) ,
213	VLC_PACK( 4 , 1 , 6 ) ,
214	VLC_PACK( 4 , 1 , 2 ) ,
215/* B12_mvd.out */
216	VLC_PACK( 4 , 0 , 0 ) ,
217	VLC_PACK( 5 , 0 , 1 ) ,
218	VLC_PACK( 5 , 0 , 2 ) ,
219	VLC_PACK( 5 , 0 , 3 ) ,
220	VLC_PACK( 2 , 1 , 3 ) ,
221	VLC_PACK( 2 , 3 , 5 ) ,
222	VLC_PACK( 0 , 3 , 11 ) ,
223	VLC_PACK( 5 , 0 , 4 ) ,
224	VLC_PACK( 5 , 0 , 5 ) ,
225	VLC_PACK( 5 , 0 , 6 ) ,
226	VLC_PACK( 5 , 0 , 7 ) ,
227	VLC_PACK( 0 , 0 , 4 ) ,
228	VLC_PACK( 5 , 0 , 10 ) ,
229	VLC_PACK( 5 , 0 , 11 ) ,
230	VLC_PACK( 5 , 0 , 12 ) ,
231	VLC_PACK( 5 , 0 , 9 ) ,
232	VLC_PACK( 5 , 0 , 8 ) ,
233	VLC_PACK( 1 , 1 , 16 ) ,
234	VLC_PACK( 0 , 0 , 18 ) ,
235	VLC_PACK( 0 , 0 , 19 ) ,
236	VLC_PACK( 0 , 0 , 20 ) ,
237	VLC_PACK( 5 , 0 , 24 ) ,
238	VLC_PACK( 5 , 0 , 23 ) ,
239	VLC_PACK( 5 , 0 , 22 ) ,
240	VLC_PACK( 5 , 0 , 21 ) ,
241	VLC_PACK( 5 , 0 , 20 ) ,
242	VLC_PACK( 5 , 0 , 19 ) ,
243	VLC_PACK( 5 , 0 , 18 ) ,
244	VLC_PACK( 5 , 0 , 17 ) ,
245	VLC_PACK( 5 , 0 , 16 ) ,
246	VLC_PACK( 5 , 0 , 15 ) ,
247	VLC_PACK( 5 , 0 , 14 ) ,
248	VLC_PACK( 5 , 0 , 13 ) ,
249	VLC_PACK( 3 , 1 , 0 ) ,
250	VLC_PACK( 5 , 0 , 32 ) ,
251	VLC_PACK( 5 , 0 , 31 ) ,
252	VLC_PACK( 5 , 0 , 30 ) ,
253	VLC_PACK( 5 , 0 , 29 ) ,
254	VLC_PACK( 5 , 0 , 28 ) ,
255	VLC_PACK( 5 , 0 , 27 ) ,
256	VLC_PACK( 5 , 0 , 26 ) ,
257	VLC_PACK( 5 , 0 , 25 ) ,
258/* B13_dct_dc_size_luminance.out */
259	VLC_PACK( 2 , 5 , 4 ) ,
260	VLC_PACK( 0 , 0 , 14 ) ,
261	VLC_PACK( 4 , 1 , 2 ) ,
262	VLC_PACK( 4 , 1 , 1 ) ,
263	VLC_PACK( 4 , 0 , 4 ) ,
264	VLC_PACK( 4 , 1 , 5 ) ,
265	VLC_PACK( 4 , 2 , 6 ) ,
266	VLC_PACK( 4 , 3 , 7 ) ,
267	VLC_PACK( 4 , 4 , 8 ) ,
268	VLC_PACK( 4 , 5 , 9 ) ,
269	VLC_PACK( 2 , 2 , 1 ) ,
270	VLC_PACK( 4 , 0 , 10 ) ,
271	VLC_PACK( 4 , 1 , 11 ) ,
272	VLC_PACK( 4 , 2 , 12 ) ,
273	VLC_PACK( 3 , 2 , 0 ) ,
274	VLC_PACK( 4 , 0 , 3 ) ,
275	VLC_PACK( 4 , 0 , 0 ) ,
276/* B14_dct_dc_size_chrominance.out */
277	VLC_PACK( 2 , 5 , 4 ) ,
278	VLC_PACK( 4 , 1 , 2 ) ,
279	VLC_PACK( 4 , 1 , 1 ) ,
280	VLC_PACK( 4 , 1 , 0 ) ,
281	VLC_PACK( 4 , 0 , 3 ) ,
282	VLC_PACK( 4 , 1 , 4 ) ,
283	VLC_PACK( 4 , 2 , 5 ) ,
284	VLC_PACK( 4 , 3 , 6 ) ,
285	VLC_PACK( 4 , 4 , 7 ) ,
286	VLC_PACK( 4 , 5 , 8 ) ,
287	VLC_PACK( 2 , 3 , 1 ) ,
288	VLC_PACK( 4 , 0 , 9 ) ,
289	VLC_PACK( 4 , 1 , 10 ) ,
290	VLC_PACK( 4 , 2 , 11 ) ,
291	VLC_PACK( 4 , 3 , 12 ) ,
292	VLC_PACK( 3 , 3 , 0 ) ,
293/* B16_intra_tcoeff.out */
294	VLC_PACK( 2 , 1 , 16 ) ,
295	VLC_PACK( 0 , 3 , 77 ) ,
296	VLC_PACK( 0 , 2 , 96 ) ,
297	VLC_PACK( 0 , 1 , 103 ) ,
298	VLC_PACK( 2 , 1 , 106 ) ,
299	VLC_PACK( 2 , 1 , 108 ) ,
300	VLC_PACK( 5 , 1 , 250 ) ,
301	VLC_PACK( 7 , 0 , 254 ) ,
302	VLC_PACK( 4 , 2 , 508 ) ,
303	VLC_PACK( 4 , 2 , 508 ) ,
304	VLC_PACK( 4 , 2 , 509 ) ,
305	VLC_PACK( 4 , 2 , 509 ) ,
306	VLC_PACK( 4 , 3 , 506 ) ,
307	VLC_PACK( 4 , 3 , 507 ) ,
308	VLC_PACK( 5 , 0 , 222 ) ,
309	VLC_PACK( 5 , 0 , 252 ) ,
310	VLC_PACK( 0 , 3 , 3 ) ,
311	VLC_PACK( 2 , 1 , 18 ) ,
312	VLC_PACK( 0 , 3 , 44 ) ,
313	VLC_PACK( 5 , 1 , 237 ) ,
314	VLC_PACK( 7 , 0 , 433 ) ,
315	VLC_PACK( 7 , 0 , 434 ) ,
316	VLC_PACK( 7 , 0 , 435 ) ,
317	VLC_PACK( 7 , 0 , 436 ) ,
318	VLC_PACK( 7 , 0 , 437 ) ,
319	VLC_PACK( 7 , 0 , 221 ) ,
320	VLC_PACK( 7 , 0 , 251 ) ,
321	VLC_PACK( 5 , 0 , 435 ) ,
322	VLC_PACK( 5 , 0 , 436 ) ,
323	VLC_PACK( 5 , 0 , 29 ) ,
324	VLC_PACK( 5 , 0 , 61 ) ,
325	VLC_PACK( 5 , 0 , 93 ) ,
326	VLC_PACK( 5 , 0 , 156 ) ,
327	VLC_PACK( 5 , 0 , 188 ) ,
328	VLC_PACK( 5 , 0 , 217 ) ,
329	VLC_PACK( 4 , 0 , 255 ) ,
330	VLC_PACK( 0 , 3 , 2 ) ,
331	VLC_PACK( 0 , 2 , 17 ) ,
332	VLC_PACK( 5 , 0 , 230 ) ,
333	VLC_PACK( 5 , 0 , 229 ) ,
334	VLC_PACK( 5 , 0 , 228 ) ,
335	VLC_PACK( 5 , 0 , 214 ) ,
336	VLC_PACK( 5 , 0 , 60 ) ,
337	VLC_PACK( 5 , 0 , 213 ) ,
338	VLC_PACK( 5 , 0 , 186 ) ,
339	VLC_PACK( 5 , 0 , 28 ) ,
340	VLC_PACK( 5 , 0 , 433 ) ,
341	VLC_PACK( 7 , 0 , 247 ) ,
342	VLC_PACK( 7 , 0 , 93 ) ,
343	VLC_PACK( 7 , 0 , 61 ) ,
344	VLC_PACK( 7 , 0 , 430 ) ,
345	VLC_PACK( 7 , 0 , 429 ) ,
346	VLC_PACK( 7 , 0 , 428 ) ,
347	VLC_PACK( 7 , 0 , 427 ) ,
348	VLC_PACK( 5 , 0 , 232 ) ,
349	VLC_PACK( 5 , 0 , 231 ) ,
350	VLC_PACK( 5 , 0 , 215 ) ,
351	VLC_PACK( 5 , 0 , 374 ) ,
352	VLC_PACK( 7 , 0 , 157 ) ,
353	VLC_PACK( 7 , 0 , 125 ) ,
354	VLC_PACK( 7 , 0 , 432 ) ,
355	VLC_PACK( 7 , 0 , 431 ) ,
356	VLC_PACK( 3 , 3 , 0 ) ,
357	VLC_PACK( 3 , 3 , 0 ) ,
358	VLC_PACK( 7 , 1 , 248 ) ,
359	VLC_PACK( 5 , 1 , 233 ) ,
360	VLC_PACK( 7 , 0 , 189 ) ,
361	VLC_PACK( 7 , 0 , 220 ) ,
362	VLC_PACK( 7 , 0 , 250 ) ,
363	VLC_PACK( 5 , 0 , 434 ) ,
364	VLC_PACK( 5 , 0 , 92 ) ,
365	VLC_PACK( 5 , 0 , 375 ) ,
366	VLC_PACK( 5 , 0 , 124 ) ,
367	VLC_PACK( 5 , 0 , 155 ) ,
368	VLC_PACK( 5 , 0 , 187 ) ,
369	VLC_PACK( 5 , 0 , 216 ) ,
370	VLC_PACK( 5 , 0 , 235 ) ,
371	VLC_PACK( 5 , 0 , 236 ) ,
372	VLC_PACK( 0 , 0 , 16 ) ,
373	VLC_PACK( 0 , 0 , 17 ) ,
374	VLC_PACK( 5 , 1 , 241 ) ,
375	VLC_PACK( 7 , 0 , 439 ) ,
376	VLC_PACK( 7 , 0 , 30 ) ,
377	VLC_PACK( 7 , 0 , 62 ) ,
378	VLC_PACK( 7 , 0 , 252 ) ,
379	VLC_PACK( 5 , 0 , 437 ) ,
380	VLC_PACK( 5 , 0 , 438 ) ,
381	VLC_PACK( 5 , 0 , 439 ) ,
382	VLC_PACK( 7 , 0 , 438 ) ,
383	VLC_PACK( 5 , 0 , 157 ) ,
384	VLC_PACK( 5 , 0 , 219 ) ,
385	VLC_PACK( 5 , 0 , 243 ) ,
386	VLC_PACK( 5 , 0 , 244 ) ,
387	VLC_PACK( 5 , 0 , 245 ) ,
388	VLC_PACK( 5 , 0 , 218 ) ,
389	VLC_PACK( 5 , 0 , 239 ) ,
390	VLC_PACK( 5 , 0 , 125 ) ,
391	VLC_PACK( 5 , 0 , 240 ) ,
392	VLC_PACK( 7 , 0 , 126 ) ,
393	VLC_PACK( 7 , 0 , 158 ) ,
394	VLC_PACK( 5 , 0 , 62 ) ,
395	VLC_PACK( 7 , 0 , 94 ) ,
396	VLC_PACK( 5 , 0 , 30 ) ,
397	VLC_PACK( 5 , 0 , 189 ) ,
398	VLC_PACK( 5 , 0 , 220 ) ,
399	VLC_PACK( 5 , 0 , 246 ) ,
400	VLC_PACK( 7 , 0 , 253 ) ,
401	VLC_PACK( 5 , 0 , 94 ) ,
402	VLC_PACK( 7 , 0 , 190 ) ,
403	VLC_PACK( 7 , 0 , 222 ) ,
404	VLC_PACK( 5 , 1 , 247 ) ,
405	VLC_PACK( 5 , 0 , 158 ) ,
406	VLC_PACK( 5 , 0 , 126 ) ,
407	VLC_PACK( 5 , 0 , 190 ) ,
408	VLC_PACK( 5 , 0 , 249 ) ,
409	VLC_PACK( 5 , 0 , 221 ) ,
410/* B17_inter_tcoeff.out */
411	VLC_PACK( 2 , 4 , 16 ) ,
412	VLC_PACK( 2 , 3 , 68 ) ,
413	VLC_PACK( 0 , 2 , 84 ) ,
414	VLC_PACK( 0 , 1 , 91 ) ,
415	VLC_PACK( 1 , 1 , 94 ) ,
416	VLC_PACK( 2 , 1 , 96 ) ,
417	VLC_PACK( 0 , 0 , 98 ) ,
418	VLC_PACK( 7 , 0 , 254 ) ,
419	VLC_PACK( 4 , 2 , 508 ) ,
420	VLC_PACK( 4 , 2 , 508 ) ,
421	VLC_PACK( 4 , 2 , 509 ) ,
422	VLC_PACK( 4 , 2 , 509 ) ,
423	VLC_PACK( 4 , 3 , 444 ) ,
424	VLC_PACK( 4 , 3 , 445 ) ,
425	VLC_PACK( 5 , 0 , 190 ) ,
426	VLC_PACK( 5 , 0 , 253 ) ,
427	VLC_PACK( 2 , 3 , 6 ) ,
428	VLC_PACK( 2 , 1 , 14 ) ,
429	VLC_PACK( 0 , 2 , 40 ) ,
430	VLC_PACK( 7 , 2 , 419 ) ,
431	VLC_PACK( 2 , 1 , 46 ) ,
432	VLC_PACK( 3 , 4 , 0 ) ,
433	VLC_PACK( 2 , 2 , 5 ) ,
434	VLC_PACK( 7 , 2 , 426 ) ,
435	VLC_PACK( 7 , 1 , 424 ) ,
436	VLC_PACK( 7 , 0 , 423 ) ,
437	VLC_PACK( 5 , 1 , 246 ) ,
438	VLC_PACK( 5 , 2 , 427 ) ,
439	VLC_PACK( 5 , 1 , 425 ) ,
440	VLC_PACK( 7 , 0 , 253 ) ,
441	VLC_PACK( 7 , 0 , 430 ) ,
442	VLC_PACK( 4 , 0 , 255 ) ,
443	VLC_PACK( 0 , 3 , 2 ) ,
444	VLC_PACK( 0 , 2 , 17 ) ,
445	VLC_PACK( 5 , 0 , 217 ) ,
446	VLC_PACK( 5 , 0 , 187 ) ,
447	VLC_PACK( 5 , 0 , 124 ) ,
448	VLC_PACK( 5 , 0 , 92 ) ,
449	VLC_PACK( 5 , 0 , 60 ) ,
450	VLC_PACK( 5 , 0 , 373 ) ,
451	VLC_PACK( 5 , 0 , 422 ) ,
452	VLC_PACK( 5 , 0 , 421 ) ,
453	VLC_PACK( 7 , 0 , 414 ) ,
454	VLC_PACK( 7 , 0 , 413 ) ,
455	VLC_PACK( 7 , 0 , 412 ) ,
456	VLC_PACK( 7 , 0 , 411 ) ,
457	VLC_PACK( 7 , 0 , 410 ) ,
458	VLC_PACK( 7 , 0 , 409 ) ,
459	VLC_PACK( 7 , 0 , 408 ) ,
460	VLC_PACK( 7 , 0 , 407 ) ,
461	VLC_PACK( 5 , 0 , 243 ) ,
462	VLC_PACK( 5 , 0 , 218 ) ,
463	VLC_PACK( 5 , 0 , 424 ) ,
464	VLC_PACK( 5 , 0 , 423 ) ,
465	VLC_PACK( 7 , 0 , 418 ) ,
466	VLC_PACK( 7 , 0 , 417 ) ,
467	VLC_PACK( 7 , 0 , 416 ) ,
468	VLC_PACK( 7 , 0 , 415 ) ,
469	VLC_PACK( 5 , 0 , 374 ) ,
470	VLC_PACK( 5 , 0 , 375 ) ,
471	VLC_PACK( 5 , 0 , 29 ) ,
472	VLC_PACK( 5 , 0 , 61 ) ,
473	VLC_PACK( 5 , 0 , 93 ) ,
474	VLC_PACK( 5 , 0 , 156 ) ,
475	VLC_PACK( 5 , 0 , 188 ) ,
476	VLC_PACK( 5 , 0 , 219 ) ,
477	VLC_PACK( 5 , 1 , 244 ) ,
478	VLC_PACK( 7 , 0 , 252 ) ,
479	VLC_PACK( 7 , 0 , 221 ) ,
480	VLC_PACK( 0 , 2 , 5 ) ,
481	VLC_PACK( 7 , 2 , 432 ) ,
482	VLC_PACK( 0 , 0 , 11 ) ,
483	VLC_PACK( 0 , 0 , 12 ) ,
484	VLC_PACK( 5 , 1 , 431 ) ,
485	VLC_PACK( 7 , 0 , 436 ) ,
486	VLC_PACK( 7 , 0 , 437 ) ,
487	VLC_PACK( 7 , 0 , 438 ) ,
488	VLC_PACK( 5 , 0 , 433 ) ,
489	VLC_PACK( 5 , 0 , 434 ) ,
490	VLC_PACK( 5 , 0 , 189 ) ,
491	VLC_PACK( 5 , 0 , 220 ) ,
492	VLC_PACK( 5 , 0 , 250 ) ,
493	VLC_PACK( 5 , 1 , 248 ) ,
494	VLC_PACK( 7 , 0 , 431 ) ,
495	VLC_PACK( 5 , 0 , 125 ) ,
496	VLC_PACK( 5 , 0 , 157 ) ,
497	VLC_PACK( 7 , 0 , 439 ) ,
498	VLC_PACK( 7 , 0 , 30 ) ,
499	VLC_PACK( 7 , 0 , 62 ) ,
500	VLC_PACK( 7 , 0 , 94 ) ,
501	VLC_PACK( 5 , 0 , 435 ) ,
502	VLC_PACK( 5 , 0 , 436 ) ,
503	VLC_PACK( 5 , 0 , 437 ) ,
504	VLC_PACK( 5 , 0 , 251 ) ,
505	VLC_PACK( 7 , 0 , 126 ) ,
506	VLC_PACK( 7 , 0 , 158 ) ,
507	VLC_PACK( 7 , 0 , 190 ) ,
508	VLC_PACK( 7 , 0 , 222 ) ,
509	VLC_PACK( 5 , 1 , 438 ) ,
510	VLC_PACK( 5 , 0 , 30 ) ,
511	VLC_PACK( 5 , 0 , 62 ) ,
512	VLC_PACK( 5 , 0 , 94 ) ,
513	VLC_PACK( 5 , 0 , 252 ) ,
514	VLC_PACK( 5 , 0 , 221 ) ,
515	VLC_PACK( 5 , 0 , 126 ) ,
516	VLC_PACK( 5 , 0 , 158 ) ,
517/* B23_rvlc_tcoeff_0.out */
518	VLC_PACK( 5 , 0 , 255 ) ,
519	VLC_PACK( 5 , 0 , 222 ) ,
520	VLC_PACK( 0 , 0 , 14 ) ,
521	VLC_PACK( 0 , 1 , 15 ) ,
522	VLC_PACK( 0 , 0 , 44 ) ,
523	VLC_PACK( 0 , 1 , 45 ) ,
524	VLC_PACK( 0 , 1 , 74 ) ,
525	VLC_PACK( 0 , 2 , 103 ) ,
526	VLC_PACK( 0 , 1 , 230 ) ,
527	VLC_PACK( 0 , 0 , 256 ) ,
528	VLC_PACK( 5 , 0 , 252 ) ,
529	VLC_PACK( 7 , 0 , 254 ) ,
530	VLC_PACK( 4 , 3 , 508 ) ,
531	VLC_PACK( 4 , 3 , 509 ) ,
532	VLC_PACK( 4 , 3 , 506 ) ,
533	VLC_PACK( 4 , 3 , 507 ) ,
534	VLC_PACK( 5 , 0 , 190 ) ,
535	VLC_PACK( 5 , 0 , 158 ) ,
536	VLC_PACK( 5 , 0 , 126 ) ,
537	VLC_PACK( 5 , 0 , 94 ) ,
538	VLC_PACK( 0 , 0 , 2 ) ,
539	VLC_PACK( 0 , 1 , 3 ) ,
540	VLC_PACK( 5 , 0 , 62 ) ,
541	VLC_PACK( 5 , 0 , 30 ) ,
542	VLC_PACK( 5 , 0 , 439 ) ,
543	VLC_PACK( 5 , 0 , 438 ) ,
544	VLC_PACK( 0 , 0 , 2 ) ,
545	VLC_PACK( 0 , 1 , 3 ) ,
546	VLC_PACK( 5 , 0 , 437 ) ,
547	VLC_PACK( 5 , 0 , 93 ) ,
548	VLC_PACK( 5 , 0 , 436 ) ,
549	VLC_PACK( 5 , 0 , 435 ) ,
550	VLC_PACK( 0 , 0 , 2 ) ,
551	VLC_PACK( 0 , 1 , 3 ) ,
552	VLC_PACK( 5 , 0 , 434 ) ,
553	VLC_PACK( 5 , 0 , 374 ) ,
554	VLC_PACK( 5 , 0 , 373 ) ,
555	VLC_PACK( 5 , 0 , 123 ) ,
556	VLC_PACK( 0 , 0 , 2 ) ,
557	VLC_PACK( 0 , 1 , 3 ) ,
558	VLC_PACK( 5 , 0 , 433 ) ,
559	VLC_PACK( 5 , 0 , 432 ) ,
560	VLC_PACK( 5 , 0 , 431 ) ,
561	VLC_PACK( 5 , 0 , 430 ) ,
562	VLC_PACK( 0 , 0 , 2 ) ,
563	VLC_PACK( 3 , 1 , 0 ) ,
564	VLC_PACK( 5 , 0 , 228 ) ,
565	VLC_PACK( 5 , 0 , 150 ) ,
566	VLC_PACK( 5 , 0 , 221 ) ,
567	VLC_PACK( 5 , 0 , 251 ) ,
568	VLC_PACK( 5 , 0 , 250 ) ,
569	VLC_PACK( 5 , 0 , 249 ) ,
570	VLC_PACK( 0 , 0 , 2 ) ,
571	VLC_PACK( 0 , 1 , 3 ) ,
572	VLC_PACK( 5 , 0 , 189 ) ,
573	VLC_PACK( 5 , 0 , 220 ) ,
574	VLC_PACK( 5 , 0 , 157 ) ,
575	VLC_PACK( 5 , 0 , 125 ) ,
576	VLC_PACK( 0 , 0 , 2 ) ,
577	VLC_PACK( 0 , 1 , 3 ) ,
578	VLC_PACK( 5 , 0 , 188 ) ,
579	VLC_PACK( 5 , 0 , 156 ) ,
580	VLC_PACK( 5 , 0 , 61 ) ,
581	VLC_PACK( 5 , 0 , 29 ) ,
582	VLC_PACK( 0 , 0 , 2 ) ,
583	VLC_PACK( 0 , 1 , 3 ) ,
584	VLC_PACK( 5 , 0 , 92 ) ,
585	VLC_PACK( 5 , 0 , 60 ) ,
586	VLC_PACK( 5 , 0 , 91 ) ,
587	VLC_PACK( 5 , 0 , 59 ) ,
588	VLC_PACK( 0 , 0 , 2 ) ,
589	VLC_PACK( 0 , 1 , 3 ) ,
590	VLC_PACK( 5 , 0 , 372 ) ,
591	VLC_PACK( 5 , 0 , 311 ) ,
592	VLC_PACK( 5 , 0 , 429 ) ,
593	VLC_PACK( 5 , 0 , 503 ) ,
594	VLC_PACK( 0 , 0 , 2 ) ,
595	VLC_PACK( 3 , 1 , 0 ) ,
596	VLC_PACK( 5 , 0 , 58 ) ,
597	VLC_PACK( 5 , 0 , 26 ) ,
598	VLC_PACK( 7 , 0 , 158 ) ,
599	VLC_PACK( 7 , 0 , 126 ) ,
600	VLC_PACK( 0 , 0 , 2 ) ,
601	VLC_PACK( 0 , 1 , 3 ) ,
602	VLC_PACK( 5 , 0 , 248 ) ,
603	VLC_PACK( 7 , 0 , 30 ) ,
604	VLC_PACK( 5 , 0 , 219 ) ,
605	VLC_PACK( 5 , 0 , 218 ) ,
606	VLC_PACK( 0 , 0 , 2 ) ,
607	VLC_PACK( 0 , 1 , 3 ) ,
608	VLC_PACK( 5 , 0 , 217 ) ,
609	VLC_PACK( 5 , 0 , 245 ) ,
610	VLC_PACK( 5 , 0 , 375 ) ,
611	VLC_PACK( 5 , 0 , 124 ) ,
612	VLC_PACK( 0 , 0 , 2 ) ,
613	VLC_PACK( 0 , 1 , 3 ) ,
614	VLC_PACK( 5 , 0 , 28 ) ,
615	VLC_PACK( 5 , 0 , 155 ) ,
616	VLC_PACK( 5 , 0 , 154 ) ,
617	VLC_PACK( 5 , 0 , 122 ) ,
618	VLC_PACK( 0 , 0 , 2 ) ,
619	VLC_PACK( 0 , 1 , 3 ) ,
620	VLC_PACK( 5 , 0 , 310 ) ,
621	VLC_PACK( 5 , 0 , 27 ) ,
622	VLC_PACK( 5 , 0 , 90 ) ,
623	VLC_PACK( 5 , 0 , 121 ) ,
624	VLC_PACK( 0 , 0 , 2 ) ,
625	VLC_PACK( 3 , 1 , 0 ) ,
626	VLC_PACK( 5 , 0 , 502 ) ,
627	VLC_PACK( 5 , 0 , 371 ) ,
628	VLC_PACK( 7 , 0 , 439 ) ,
629	VLC_PACK( 7 , 0 , 438 ) ,
630	VLC_PACK( 0 , 0 , 6 ) ,
631	VLC_PACK( 0 , 1 , 7 ) ,
632	VLC_PACK( 0 , 0 , 27 ) ,
633	VLC_PACK( 0 , 1 , 28 ) ,
634	VLC_PACK( 0 , 1 , 48 ) ,
635	VLC_PACK( 0 , 2 , 68 ) ,
636	VLC_PACK( 5 , 0 , 247 ) ,
637	VLC_PACK( 5 , 0 , 246 ) ,
638	VLC_PACK( 5 , 0 , 244 ) ,
639	VLC_PACK( 7 , 0 , 221 ) ,
640	VLC_PACK( 0 , 0 , 2 ) ,
641	VLC_PACK( 0 , 1 , 3 ) ,
642	VLC_PACK( 5 , 0 , 187 ) ,
643	VLC_PACK( 5 , 0 , 216 ) ,
644	VLC_PACK( 5 , 0 , 186 ) ,
645	VLC_PACK( 5 , 0 , 185 ) ,
646	VLC_PACK( 0 , 0 , 2 ) ,
647	VLC_PACK( 0 , 1 , 3 ) ,
648	VLC_PACK( 5 , 0 , 213 ) ,
649	VLC_PACK( 5 , 0 , 237 ) ,
650	VLC_PACK( 5 , 0 , 153 ) ,
651	VLC_PACK( 5 , 0 , 184 ) ,
652	VLC_PACK( 0 , 0 , 2 ) ,
653	VLC_PACK( 2 , 1 , 3 ) ,
654	VLC_PACK( 5 , 0 , 89 ) ,
655	VLC_PACK( 5 , 0 , 152 ) ,
656	VLC_PACK( 3 , 1 , 0 ) ,
657	VLC_PACK( 7 , 0 , 218 ) ,
658	VLC_PACK( 5 , 0 , 428 ) ,
659	VLC_PACK( 7 , 0 , 253 ) ,
660	VLC_PACK( 7 , 0 , 435 ) ,
661	VLC_PACK( 7 , 0 , 432 ) ,
662	VLC_PACK( 7 , 0 , 431 ) ,
663	VLC_PACK( 0 , 0 , 2 ) ,
664	VLC_PACK( 0 , 1 , 3 ) ,
665	VLC_PACK( 5 , 0 , 243 ) ,
666	VLC_PACK( 5 , 0 , 242 ) ,
667	VLC_PACK( 5 , 0 , 215 ) ,
668	VLC_PACK( 5 , 0 , 214 ) ,
669	VLC_PACK( 0 , 0 , 2 ) ,
670	VLC_PACK( 0 , 1 , 3 ) ,
671	VLC_PACK( 5 , 0 , 236 ) ,
672	VLC_PACK( 5 , 0 , 233 ) ,
673	VLC_PACK( 5 , 0 , 183 ) ,
674	VLC_PACK( 5 , 0 , 182 ) ,
675	VLC_PACK( 0 , 0 , 2 ) ,
676	VLC_PACK( 2 , 1 , 3 ) ,
677	VLC_PACK( 5 , 0 , 151 ) ,
678	VLC_PACK( 5 , 0 , 181 ) ,
679	VLC_PACK( 3 , 1 , 0 ) ,
680	VLC_PACK( 7 , 0 , 370 ) ,
681	VLC_PACK( 7 , 0 , 188 ) ,
682	VLC_PACK( 7 , 0 , 430 ) ,
683	VLC_PACK( 7 , 0 , 429 ) ,
684	VLC_PACK( 0 , 0 , 2 ) ,
685	VLC_PACK( 0 , 1 , 3 ) ,
686	VLC_PACK( 5 , 0 , 241 ) ,
687	VLC_PACK( 7 , 0 , 426 ) ,
688	VLC_PACK( 5 , 0 , 240 ) ,
689	VLC_PACK( 5 , 0 , 239 ) ,
690	VLC_PACK( 0 , 0 , 2 ) ,
691	VLC_PACK( 0 , 1 , 3 ) ,
692	VLC_PACK( 7 , 0 , 220 ) ,
693	VLC_PACK( 7 , 0 , 157 ) ,
694	VLC_PACK( 5 , 0 , 212 ) ,
695	VLC_PACK( 5 , 0 , 235 ) ,
696	VLC_PACK( 0 , 0 , 2 ) ,
697	VLC_PACK( 2 , 1 , 3 ) ,
698	VLC_PACK( 5 , 0 , 180 ) ,
699	VLC_PACK( 5 , 0 , 211 ) ,
700	VLC_PACK( 3 , 1 , 0 ) ,
701	VLC_PACK( 7 , 0 , 405 ) ,
702	VLC_PACK( 7 , 0 , 406 ) ,
703	VLC_PACK( 7 , 0 , 425 ) ,
704	VLC_PACK( 7 , 0 , 424 ) ,
705	VLC_PACK( 0 , 0 , 6 ) ,
706	VLC_PACK( 0 , 1 , 7 ) ,
707	VLC_PACK( 0 , 0 , 18 ) ,
708	VLC_PACK( 0 , 1 , 19 ) ,
709	VLC_PACK( 0 , 1 , 27 ) ,
710	VLC_PACK( 2 , 2 , 35 ) ,
711	VLC_PACK( 5 , 0 , 238 ) ,
712	VLC_PACK( 7 , 0 , 252 ) ,
713	VLC_PACK( 7 , 0 , 125 ) ,
714	VLC_PACK( 7 , 0 , 418 ) ,
715	VLC_PACK( 0 , 0 , 2 ) ,
716	VLC_PACK( 0 , 1 , 3 ) ,
717	VLC_PACK( 5 , 0 , 234 ) ,
718	VLC_PACK( 5 , 0 , 232 ) ,
719	VLC_PACK( 5 , 0 , 210 ) ,
720	VLC_PACK( 5 , 0 , 231 ) ,
721	VLC_PACK( 0 , 0 , 2 ) ,
722	VLC_PACK( 3 , 1 , 0 ) ,
723	VLC_PACK( 7 , 0 , 404 ) ,
724	VLC_PACK( 7 , 0 , 403 ) ,
725	VLC_PACK( 7 , 0 , 189 ) ,
726	VLC_PACK( 7 , 0 , 421 ) ,
727	VLC_PACK( 7 , 0 , 417 ) ,
728	VLC_PACK( 7 , 0 , 416 ) ,
729	VLC_PACK( 0 , 0 , 2 ) ,
730	VLC_PACK( 2 , 1 , 3 ) ,
731	VLC_PACK( 7 , 0 , 251 ) ,
732	VLC_PACK( 7 , 0 , 93 ) ,
733	VLC_PACK( 3 , 1 , 0 ) ,
734	VLC_PACK( 5 , 0 , 229 ) ,
735	VLC_PACK( 5 , 0 , 230 ) ,
736	VLC_PACK( 7 , 0 , 415 ) ,
737	VLC_PACK( 7 , 0 , 414 ) ,
738	VLC_PACK( 0 , 0 , 2 ) ,
739	VLC_PACK( 2 , 1 , 3 ) ,
740	VLC_PACK( 7 , 0 , 61 ) ,
741	VLC_PACK( 7 , 0 , 29 ) ,
742	VLC_PACK( 3 , 1 , 0 ) ,
743	VLC_PACK( 7 , 0 , 219 ) ,
744	VLC_PACK( 7 , 0 , 250 ) ,
745	VLC_PACK( 2 , 2 , 4 ) ,
746	VLC_PACK( 2 , 1 , 7 ) ,
747	VLC_PACK( 7 , 0 , 374 ) ,
748	VLC_PACK( 7 , 0 , 375 ) ,
749	VLC_PACK( 3 , 2 , 0 ) ,
750	VLC_PACK( 3 , 2 , 0 ) ,
751	VLC_PACK( 7 , 0 , 409 ) ,
752	VLC_PACK( 7 , 0 , 371 ) ,
753	VLC_PACK( 3 , 1 , 0 ) ,
754	VLC_PACK( 7 , 0 , 372 ) ,
755	VLC_PACK( 7 , 0 , 373 ) ,
756	VLC_PACK( 0 , 1 , 4 ) ,
757	VLC_PACK( 0 , 0 , 24 ) ,
758	VLC_PACK( 7 , 0 , 94 ) ,
759	VLC_PACK( 7 , 0 , 62 ) ,
760	VLC_PACK( 0 , 1 , 4 ) ,
761	VLC_PACK( 0 , 0 , 18 ) ,
762	VLC_PACK( 7 , 0 , 434 ) ,
763	VLC_PACK( 7 , 0 , 433 ) ,
764	VLC_PACK( 0 , 1 , 4 ) ,
765	VLC_PACK( 0 , 0 , 12 ) ,
766	VLC_PACK( 7 , 0 , 423 ) ,
767	VLC_PACK( 7 , 0 , 422 ) ,
768	VLC_PACK( 1 , 1 , 4 ) ,
769	VLC_PACK( 0 , 0 , 6 ) ,
770	VLC_PACK( 7 , 0 , 413 ) ,
771	VLC_PACK( 7 , 0 , 412 ) ,
772	VLC_PACK( 3 , 1 , 0 ) ,
773	VLC_PACK( 7 , 0 , 408 ) ,
774	VLC_PACK( 7 , 0 , 407 ) ,
775	VLC_PACK( 7 , 0 , 411 ) ,
776	VLC_PACK( 7 , 0 , 410 ) ,
777	VLC_PACK( 7 , 0 , 420 ) ,
778	VLC_PACK( 7 , 0 , 419 ) ,
779	VLC_PACK( 7 , 0 , 428 ) ,
780	VLC_PACK( 7 , 0 , 427 ) ,
781	VLC_PACK( 7 , 0 , 437 ) ,
782	VLC_PACK( 7 , 0 , 436 ) ,
783	VLC_PACK( 7 , 0 , 222 ) ,
784	VLC_PACK( 7 , 0 , 190 ) ,
785/* B23_rvlc_tcoeff_1.out */
786	VLC_PACK( 5 , 0 , 255 ) ,
787	VLC_PACK( 5 , 0 , 253 ) ,
788	VLC_PACK( 0 , 0 , 14 ) ,
789	VLC_PACK( 0 , 1 , 15 ) ,
790	VLC_PACK( 0 , 0 , 44 ) ,
791	VLC_PACK( 0 , 1 , 45 ) ,
792	VLC_PACK( 0 , 1 , 74 ) ,
793	VLC_PACK( 0 , 2 , 103 ) ,
794	VLC_PACK( 0 , 1 , 230 ) ,
795	VLC_PACK( 0 , 0 , 256 ) ,
796	VLC_PACK( 5 , 0 , 190 ) ,
797	VLC_PACK( 7 , 0 , 254 ) ,
798	VLC_PACK( 4 , 3 , 508 ) ,
799	VLC_PACK( 4 , 3 , 509 ) ,
800	VLC_PACK( 4 , 3 , 444 ) ,
801	VLC_PACK( 4 , 3 , 445 ) ,
802	VLC_PACK( 5 , 0 , 252 ) ,
803	VLC_PACK( 5 , 0 , 158 ) ,
804	VLC_PACK( 5 , 0 , 221 ) ,
805	VLC_PACK( 5 , 0 , 62 ) ,
806	VLC_PACK( 0 , 0 , 2 ) ,
807	VLC_PACK( 0 , 1 , 3 ) ,
808	VLC_PACK( 5 , 0 , 251 ) ,
809	VLC_PACK( 5 , 0 , 189 ) ,
810	VLC_PACK( 5 , 0 , 250 ) ,
811	VLC_PACK( 5 , 0 , 249 ) ,
812	VLC_PACK( 0 , 0 , 2 ) ,
813	VLC_PACK( 0 , 1 , 3 ) ,
814	VLC_PACK( 5 , 0 , 248 ) ,
815	VLC_PACK( 5 , 0 , 219 ) ,
816	VLC_PACK( 5 , 0 , 247 ) ,
817	VLC_PACK( 5 , 0 , 246 ) ,
818	VLC_PACK( 0 , 0 , 2 ) ,
819	VLC_PACK( 0 , 1 , 3 ) ,
820	VLC_PACK( 5 , 0 , 245 ) ,
821	VLC_PACK( 5 , 0 , 244 ) ,
822	VLC_PACK( 5 , 0 , 243 ) ,
823	VLC_PACK( 5 , 0 , 216 ) ,
824	VLC_PACK( 0 , 0 , 2 ) ,
825	VLC_PACK( 0 , 1 , 3 ) ,
826	VLC_PACK( 5 , 0 , 242 ) ,
827	VLC_PACK( 5 , 0 , 241 ) ,
828	VLC_PACK( 5 , 0 , 238 ) ,
829	VLC_PACK( 5 , 0 , 237 ) ,
830	VLC_PACK( 0 , 0 , 2 ) ,
831	VLC_PACK( 3 , 1 , 0 ) ,
832	VLC_PACK( 5 , 0 , 236 ) ,
833	VLC_PACK( 5 , 0 , 152 ) ,
834	VLC_PACK( 5 , 0 , 126 ) ,
835	VLC_PACK( 5 , 0 , 94 ) ,
836	VLC_PACK( 5 , 0 , 30 ) ,
837	VLC_PACK( 5 , 0 , 439 ) ,
838	VLC_PACK( 0 , 0 , 2 ) ,
839	VLC_PACK( 0 , 1 , 3 ) ,
840	VLC_PACK( 5 , 0 , 438 ) ,
841	VLC_PACK( 5 , 0 , 437 ) ,
842	VLC_PACK( 5 , 0 , 220 ) ,
843	VLC_PACK( 5 , 0 , 157 ) ,
844	VLC_PACK( 0 , 0 , 2 ) ,
845	VLC_PACK( 0 , 1 , 3 ) ,
846	VLC_PACK( 5 , 0 , 188 ) ,
847	VLC_PACK( 5 , 0 , 93 ) ,
848	VLC_PACK( 5 , 0 , 218 ) ,
849	VLC_PACK( 5 , 0 , 156 ) ,
850	VLC_PACK( 0 , 0 , 2 ) ,
851	VLC_PACK( 0 , 1 , 3 ) ,
852	VLC_PACK( 5 , 0 , 217 ) ,
853	VLC_PACK( 5 , 0 , 187 ) ,
854	VLC_PACK( 5 , 0 , 186 ) ,
855	VLC_PACK( 5 , 0 , 155 ) ,
856	VLC_PACK( 0 , 0 , 2 ) ,
857	VLC_PACK( 0 , 1 , 3 ) ,
858	VLC_PACK( 5 , 0 , 240 ) ,
859	VLC_PACK( 5 , 0 , 239 ) ,
860	VLC_PACK( 5 , 0 , 214 ) ,
861	VLC_PACK( 5 , 0 , 213 ) ,
862	VLC_PACK( 0 , 0 , 2 ) ,
863	VLC_PACK( 3 , 1 , 0 ) ,
864	VLC_PACK( 5 , 0 , 122 ) ,
865	VLC_PACK( 5 , 0 , 27 ) ,
866	VLC_PACK( 7 , 0 , 158 ) ,
867	VLC_PACK( 7 , 0 , 126 ) ,
868	VLC_PACK( 0 , 0 , 2 ) ,
869	VLC_PACK( 0 , 1 , 3 ) ,
870	VLC_PACK( 5 , 0 , 436 ) ,
871	VLC_PACK( 7 , 0 , 30 ) ,
872	VLC_PACK( 5 , 0 , 125 ) ,
873	VLC_PACK( 5 , 0 , 435 ) ,
874	VLC_PACK( 0 , 0 , 2 ) ,
875	VLC_PACK( 0 , 1 , 3 ) ,
876	VLC_PACK( 5 , 0 , 432 ) ,
877	VLC_PACK( 5 , 0 , 431 ) ,
878	VLC_PACK( 5 , 0 , 61 ) ,
879	VLC_PACK( 5 , 0 , 29 ) ,
880	VLC_PACK( 0 , 0 , 2 ) ,
881	VLC_PACK( 0 , 1 , 3 ) ,
882	VLC_PACK( 5 , 0 , 124 ) ,
883	VLC_PACK( 5 , 0 , 92 ) ,
884	VLC_PACK( 5 , 0 , 60 ) ,
885	VLC_PACK( 5 , 0 , 28 ) ,
886	VLC_PACK( 0 , 0 , 2 ) ,
887	VLC_PACK( 0 , 1 , 3 ) ,
888	VLC_PACK( 5 , 0 , 215 ) ,
889	VLC_PACK( 5 , 0 , 154 ) ,
890	VLC_PACK( 5 , 0 , 185 ) ,
891	VLC_PACK( 5 , 0 , 184 ) ,
892	VLC_PACK( 0 , 0 , 2 ) ,
893	VLC_PACK( 3 , 1 , 0 ) ,
894	VLC_PACK( 5 , 0 , 366 ) ,
895	VLC_PACK( 5 , 0 , 410 ) ,
896	VLC_PACK( 7 , 0 , 439 ) ,
897	VLC_PACK( 7 , 0 , 438 ) ,
898	VLC_PACK( 0 , 0 , 6 ) ,
899	VLC_PACK( 0 , 1 , 7 ) ,
900	VLC_PACK( 0 , 0 , 27 ) ,
901	VLC_PACK( 0 , 1 , 28 ) ,
902	VLC_PACK( 0 , 1 , 48 ) ,
903	VLC_PACK( 0 , 2 , 68 ) ,
904	VLC_PACK( 5 , 0 , 434 ) ,
905	VLC_PACK( 5 , 0 , 433 ) ,
906	VLC_PACK( 5 , 0 , 430 ) ,
907	VLC_PACK( 7 , 0 , 221 ) ,
908	VLC_PACK( 0 , 0 , 2 ) ,
909	VLC_PACK( 0 , 1 , 3 ) ,
910	VLC_PACK( 5 , 0 , 375 ) ,
911	VLC_PACK( 5 , 0 , 374 ) ,
912	VLC_PACK( 5 , 0 , 373 ) ,
913	VLC_PACK( 5 , 0 , 426 ) ,
914	VLC_PACK( 0 , 0 , 2 ) ,
915	VLC_PACK( 0 , 1 , 3 ) ,
916	VLC_PACK( 5 , 0 , 372 ) ,
917	VLC_PACK( 5 , 0 , 420 ) ,
918	VLC_PACK( 5 , 0 , 123 ) ,
919	VLC_PACK( 5 , 0 , 91 ) ,
920	VLC_PACK( 0 , 0 , 2 ) ,
921	VLC_PACK( 2 , 1 , 3 ) ,
922	VLC_PACK( 5 , 0 , 153 ) ,
923	VLC_PACK( 5 , 0 , 59 ) ,
924	VLC_PACK( 3 , 1 , 0 ) ,
925	VLC_PACK( 7 , 0 , 218 ) ,
926	VLC_PACK( 5 , 0 , 409 ) ,
927	VLC_PACK( 7 , 0 , 253 ) ,
928	VLC_PACK( 7 , 0 , 435 ) ,
929	VLC_PACK( 7 , 0 , 432 ) ,
930	VLC_PACK( 7 , 0 , 431 ) ,
931	VLC_PACK( 0 , 0 , 2 ) ,
932	VLC_PACK( 0 , 1 , 3 ) ,
933	VLC_PACK( 5 , 0 , 429 ) ,
934	VLC_PACK( 5 , 0 , 428 ) ,
935	VLC_PACK( 5 , 0 , 425 ) ,
936	VLC_PACK( 5 , 0 , 424 ) ,
937	VLC_PACK( 0 , 0 , 2 ) ,
938	VLC_PACK( 0 , 1 , 3 ) ,
939	VLC_PACK( 5 , 0 , 419 ) ,
940	VLC_PACK( 5 , 0 , 418 ) ,
941	VLC_PACK( 5 , 0 , 311 ) ,
942	VLC_PACK( 5 , 0 , 371 ) ,
943	VLC_PACK( 0 , 0 , 2 ) ,
944	VLC_PACK( 2 , 1 , 3 ) ,
945	VLC_PACK( 5 , 0 , 310 ) ,
946	VLC_PACK( 5 , 0 , 370 ) ,
947	VLC_PACK( 3 , 1 , 0 ) ,
948	VLC_PACK( 7 , 0 , 370 ) ,
949	VLC_PACK( 7 , 0 , 188 ) ,
950	VLC_PACK( 7 , 0 , 430 ) ,
951	VLC_PACK( 7 , 0 , 429 ) ,
952	VLC_PACK( 0 , 0 , 2 ) ,
953	VLC_PACK( 0 , 1 , 3 ) ,
954	VLC_PACK( 5 , 0 , 427 ) ,
955	VLC_PACK( 7 , 0 , 426 ) ,
956	VLC_PACK( 5 , 0 , 423 ) ,
957	VLC_PACK( 5 , 0 , 422 ) ,
958	VLC_PACK( 0 , 0 , 2 ) ,
959	VLC_PACK( 0 , 1 , 3 ) ,
960	VLC_PACK( 7 , 0 , 220 ) ,
961	VLC_PACK( 7 , 0 , 157 ) ,
962	VLC_PACK( 5 , 0 , 417 ) ,
963	VLC_PACK( 5 , 0 , 416 ) ,
964	VLC_PACK( 0 , 0 , 2 ) ,
965	VLC_PACK( 2 , 1 , 3 ) ,
966	VLC_PACK( 5 , 0 , 369 ) ,
967	VLC_PACK( 5 , 0 , 368 ) ,
968	VLC_PACK( 3 , 1 , 0 ) ,
969	VLC_PACK( 7 , 0 , 405 ) ,
970	VLC_PACK( 7 , 0 , 406 ) ,
971	VLC_PACK( 7 , 0 , 425 ) ,
972	VLC_PACK( 7 , 0 , 424 ) ,
973	VLC_PACK( 0 , 0 , 6 ) ,
974	VLC_PACK( 0 , 1 , 7 ) ,
975	VLC_PACK( 0 , 0 , 18 ) ,
976	VLC_PACK( 0 , 1 , 19 ) ,
977	VLC_PACK( 0 , 1 , 27 ) ,
978	VLC_PACK( 2 , 2 , 35 ) ,
979	VLC_PACK( 5 , 0 , 421 ) ,
980	VLC_PACK( 7 , 0 , 252 ) ,
981	VLC_PACK( 7 , 0 , 125 ) ,
982	VLC_PACK( 7 , 0 , 418 ) ,
983	VLC_PACK( 0 , 0 , 2 ) ,
984	VLC_PACK( 0 , 1 , 3 ) ,
985	VLC_PACK( 5 , 0 , 415 ) ,
986	VLC_PACK( 5 , 0 , 414 ) ,
987	VLC_PACK( 5 , 0 , 367 ) ,
988	VLC_PACK( 5 , 0 , 413 ) ,
989	VLC_PACK( 0 , 0 , 2 ) ,
990	VLC_PACK( 3 , 1 , 0 ) ,
991	VLC_PACK( 7 , 0 , 404 ) ,
992	VLC_PACK( 7 , 0 , 403 ) ,
993	VLC_PACK( 7 , 0 , 189 ) ,
994	VLC_PACK( 7 , 0 , 421 ) ,
995	VLC_PACK( 7 , 0 , 417 ) ,
996	VLC_PACK( 7 , 0 , 416 ) ,
997	VLC_PACK( 0 , 0 , 2 ) ,
998	VLC_PACK( 2 , 1 , 3 ) ,
999	VLC_PACK( 7 , 0 , 251 ) ,
1000	VLC_PACK( 7 , 0 , 93 ) ,
1001	VLC_PACK( 3 , 1 , 0 ) ,
1002	VLC_PACK( 5 , 0 , 411 ) ,
1003	VLC_PACK( 5 , 0 , 412 ) ,
1004	VLC_PACK( 7 , 0 , 415 ) ,
1005	VLC_PACK( 7 , 0 , 414 ) ,
1006	VLC_PACK( 0 , 0 , 2 ) ,
1007	VLC_PACK( 2 , 1 , 3 ) ,
1008	VLC_PACK( 7 , 0 , 61 ) ,
1009	VLC_PACK( 7 , 0 , 29 ) ,
1010	VLC_PACK( 3 , 1 , 0 ) ,
1011	VLC_PACK( 7 , 0 , 219 ) ,
1012	VLC_PACK( 7 , 0 , 250 ) ,
1013	VLC_PACK( 2 , 2 , 4 ) ,
1014	VLC_PACK( 2 , 1 , 7 ) ,
1015	VLC_PACK( 7 , 0 , 374 ) ,
1016	VLC_PACK( 7 , 0 , 375 ) ,
1017	VLC_PACK( 3 , 2 , 0 ) ,
1018	VLC_PACK( 3 , 2 , 0 ) ,
1019	VLC_PACK( 7 , 0 , 409 ) ,
1020	VLC_PACK( 7 , 0 , 371 ) ,
1021	VLC_PACK( 3 , 1 , 0 ) ,
1022	VLC_PACK( 7 , 0 , 372 ) ,
1023	VLC_PACK( 7 , 0 , 373 ) ,
1024	VLC_PACK( 0 , 1 , 4 ) ,
1025	VLC_PACK( 0 , 0 , 24 ) ,
1026	VLC_PACK( 7 , 0 , 94 ) ,
1027	VLC_PACK( 7 , 0 , 62 ) ,
1028	VLC_PACK( 0 , 1 , 4 ) ,
1029	VLC_PACK( 0 , 0 , 18 ) ,
1030	VLC_PACK( 7 , 0 , 434 ) ,
1031	VLC_PACK( 7 , 0 , 433 ) ,
1032	VLC_PACK( 0 , 1 , 4 ) ,
1033	VLC_PACK( 0 , 0 , 12 ) ,
1034	VLC_PACK( 7 , 0 , 423 ) ,
1035	VLC_PACK( 7 , 0 , 422 ) ,
1036	VLC_PACK( 1 , 1 , 4 ) ,
1037	VLC_PACK( 0 , 0 , 6 ) ,
1038	VLC_PACK( 7 , 0 , 413 ) ,
1039	VLC_PACK( 7 , 0 , 412 ) ,
1040	VLC_PACK( 3 , 1 , 0 ) ,
1041	VLC_PACK( 7 , 0 , 408 ) ,
1042	VLC_PACK( 7 , 0 , 407 ) ,
1043	VLC_PACK( 7 , 0 , 411 ) ,
1044	VLC_PACK( 7 , 0 , 410 ) ,
1045	VLC_PACK( 7 , 0 , 420 ) ,
1046	VLC_PACK( 7 , 0 , 419 ) ,
1047	VLC_PACK( 7 , 0 , 428 ) ,
1048	VLC_PACK( 7 , 0 , 427 ) ,
1049	VLC_PACK( 7 , 0 , 437 ) ,
1050	VLC_PACK( 7 , 0 , 436 ) ,
1051	VLC_PACK( 7 , 0 , 222 ) ,
1052	VLC_PACK( 7 , 0 , 190 ) ,
1053/* B29_MVDs.out */
1054	VLC_PACK( 4 , 0 , 0 ) ,
1055	VLC_PACK( 5 , 0 , 1 ) ,
1056	VLC_PACK( 5 , 0 , 2 ) ,
1057	VLC_PACK( 5 , 0 , 3 ) ,
1058	VLC_PACK( 5 , 0 , 4 ) ,
1059	VLC_PACK( 5 , 0 , 5 ) ,
1060	VLC_PACK( 1 , 5 , 1 ) ,
1061	VLC_PACK( 5 , 0 , 6 ) ,
1062	VLC_PACK( 5 , 0 , 7 ) ,
1063	VLC_PACK( 5 , 0 , 8 ) ,
1064	VLC_PACK( 5 , 0 , 9 ) ,
1065	VLC_PACK( 5 , 0 , 10 ) ,
1066	VLC_PACK( 5 , 0 , 11 ) ,
1067	VLC_PACK( 1 , 3 , 1 ) ,
1068	VLC_PACK( 5 , 0 , 12 ) ,
1069	VLC_PACK( 5 , 0 , 13 ) ,
1070	VLC_PACK( 5 , 0 , 14 ) ,
1071	VLC_PACK( 5 , 0 , 15 ) ,
1072	VLC_PACK( 5 , 1 , 16 ) ,
1073/* B30_MVDs.out */
1074	VLC_PACK( 5 , 0 , 1 ) ,
1075	VLC_PACK( 5 , 0 , 2 ) ,
1076	VLC_PACK( 5 , 0 , 3 ) ,
1077	VLC_PACK( 5 , 0 , 4 ) ,
1078	VLC_PACK( 5 , 0 , 5 ) ,
1079	VLC_PACK( 5 , 0 , 6 ) ,
1080	VLC_PACK( 1 , 5 , 1 ) ,
1081	VLC_PACK( 5 , 0 , 7 ) ,
1082	VLC_PACK( 5 , 0 , 8 ) ,
1083	VLC_PACK( 5 , 0 , 9 ) ,
1084	VLC_PACK( 5 , 0 , 10 ) ,
1085	VLC_PACK( 5 , 0 , 11 ) ,
1086	VLC_PACK( 5 , 0 , 12 ) ,
1087	VLC_PACK( 1 , 2 , 1 ) ,
1088	VLC_PACK( 5 , 0 , 13 ) ,
1089	VLC_PACK( 5 , 0 , 14 ) ,
1090	VLC_PACK( 5 , 0 , 15 ) ,
1091	VLC_PACK( 5 , 1 , 16 ) ,
1092/* B31_conv_ratio.out */
1093	VLC_PACK( 4 , 0 , 1 ) ,
1094	VLC_PACK( 4 , 1 , 2 ) ,
1095	VLC_PACK( 4 , 1 , 4 ) ,
1096};
1097
1098
1099struct context_MPEG4_s {
1100    struct context_DEC_s dec_ctx;
1101    object_context_p obj_context; /* back reference */
1102
1103    uint32_t profile;
1104
1105    /* Picture parameters */
1106    VAPictureParameterBufferMPEG4 *pic_params;
1107    object_surface_p forward_ref_surface;
1108    object_surface_p backward_ref_surface;
1109
1110    uint32_t display_picture_width;    /* in pixels */
1111    uint32_t display_picture_height;    /* in pixels */
1112
1113    uint32_t coded_picture_width;    /* in pixels */
1114    uint32_t coded_picture_height;    /* in pixels */
1115
1116    uint32_t picture_width_mb;        /* in macroblocks */
1117    uint32_t picture_height_mb;        /* in macroblocks */
1118    uint32_t size_mb;                /* in macroblocks */
1119
1120    uint32_t FEControl;
1121    uint32_t FE_SPS0;
1122    uint32_t FE_VOP_PPS0;
1123    uint32_t FE_VOP_SPS0;
1124    uint32_t FE_PICSH_PPS0;
1125
1126    uint32_t BE_SPS0;
1127    uint32_t BE_SPS1;
1128    uint32_t BE_VOP_PPS0;
1129    uint32_t BE_VOP_SPS0;
1130    uint32_t BE_VOP_SPS1;
1131    uint32_t BE_PICSH_PPS0;
1132
1133    /* IQ Matrix */
1134    uint32_t qmatrix_data[MAX_QUANT_TABLES][16];
1135    int load_non_intra_quant_mat;
1136    int load_intra_quant_mat;
1137
1138    /* VLC packed data */
1139    struct psb_buffer_s vlc_packed_table;
1140
1141    /* FE state buffer */
1142    struct psb_buffer_s preload_buffer;
1143
1144    struct psb_buffer_s *data_partition_buffer0;
1145    struct psb_buffer_s *data_partition_buffer1;
1146
1147    uint32_t field_type;
1148};
1149
1150typedef struct context_MPEG4_s *context_MPEG4_p;
1151
1152#define INIT_CONTEXT_MPEG4    context_MPEG4_p ctx = (context_MPEG4_p) obj_context->format_data;
1153
1154#define SURFACE(id)    ((object_surface_p) object_heap_lookup( &ctx->obj_context->driver_data->surface_heap, id ))
1155
1156static const char *psb__debug_picture_coding_str(unsigned char vop_coding_type)
1157{
1158    switch (vop_coding_type) {
1159    case PICTURE_CODING_I:
1160        return ("PICTURE_CODING_I");
1161    case PICTURE_CODING_P:
1162        return ("PICTURE_CODING_P");
1163    case PICTURE_CODING_B:
1164        return ("PICTURE_CODING_B");
1165    case PICTURE_CODING_S:
1166        return ("PICTURE_CODING_S");
1167    }
1168    return ("UNKNOWN!!!");
1169}
1170
1171
1172static void pnw_MPEG4_QueryConfigAttributes(
1173    VAProfile profile,
1174    VAEntrypoint entrypoint,
1175    VAConfigAttrib *attrib_list,
1176    int num_attribs)
1177{
1178    int i;
1179    drv_debug_msg(VIDEO_DEBUG_GENERAL, "pnw_MPEG4_QueryConfigAttributes\n");
1180
1181    for (i = 0; i < num_attribs; i++) {
1182        switch (attrib_list[i].type) {
1183        case VAConfigAttribMaxPictureWidth:
1184            if (entrypoint == VAEntrypointVLD) {
1185                if (profile == VAProfileMPEG4AdvancedSimple)
1186                    attrib_list[i].value = HW_SUPPORTED_MAX_PICTURE_WIDTH_MPEG4;
1187                else if(profile == VAProfileH263Baseline)
1188                    attrib_list[i].value = HW_SUPPORTED_MAX_PICTURE_WIDTH_H263;
1189                else
1190                    attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
1191            }
1192            else
1193                attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
1194            break;
1195        case VAConfigAttribMaxPictureHeight:
1196            if (entrypoint == VAEntrypointVLD) {
1197                if (profile == VAProfileMPEG4AdvancedSimple)
1198                    attrib_list[i].value = HW_SUPPORTED_MAX_PICTURE_HEIGHT_MPEG4;
1199                else if(profile == VAProfileH263Baseline)
1200                    attrib_list[i].value = HW_SUPPORTED_MAX_PICTURE_HEIGHT_H263;
1201            }
1202            else
1203                attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
1204            break;
1205        default:
1206            break;
1207        }
1208    }
1209}
1210
1211static VAStatus pnw_MPEG4_ValidateConfig(
1212    object_config_p obj_config)
1213{
1214    int i;
1215    /* Check all attributes */
1216    for (i = 0; i < obj_config->attrib_count; i++) {
1217        switch (obj_config->attrib_list[i].type) {
1218        case VAConfigAttribRTFormat:
1219            /* Ignore */
1220            break;
1221
1222        default:
1223            return VA_STATUS_ERROR_ATTR_NOT_SUPPORTED;
1224        }
1225    }
1226
1227    return VA_STATUS_SUCCESS;
1228}
1229
1230static VAStatus psb__MPEG4_check_legal_picture(object_context_p obj_context, object_config_p obj_config)
1231{
1232    VAStatus vaStatus = VA_STATUS_SUCCESS;
1233
1234    CHECK_CONTEXT(obj_context);
1235
1236    CHECK_CONFIG(obj_config);
1237
1238    /* MSVDX decode capability for MPEG4:
1239     *     SP@L3
1240     *     ASP@L5
1241     *
1242     * Refer to the "MSVDX MPEG4 decode capability" table of "Poulsbo Media Software Overview".
1243     */
1244    switch (obj_config->profile) {
1245    case VAProfileMPEG4Simple:
1246    case VAProfileMPEG4AdvancedSimple:
1247        if ((obj_context->picture_width <= 0) || (obj_context->picture_height <= 0)) {
1248            vaStatus = VA_STATUS_ERROR_RESOLUTION_NOT_SUPPORTED;
1249        }
1250        break;
1251
1252    default:
1253        vaStatus = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
1254        break;
1255    }
1256
1257    return vaStatus;
1258}
1259
1260static void pnw_MPEG4_DestroyContext(object_context_p obj_context);
1261static void psb__MPEG4_process_slice_data(context_DEC_p dec_ctx, VASliceParameterBufferBase *vld_slice_param);
1262static void psb__MPEG4_end_slice(context_DEC_p dec_ctx);
1263static void psb__MPEG4_begin_slice(context_DEC_p dec_ctx, VASliceParameterBufferBase *vld_slice_param);
1264static VAStatus pnw_MPEG4_process_buffer(context_DEC_p dec_ctx, object_buffer_p buffer);
1265
1266static VAStatus pnw_MPEG4_CreateContext(
1267    object_context_p obj_context,
1268    object_config_p obj_config)
1269{
1270    VAStatus vaStatus = VA_STATUS_SUCCESS;
1271    context_MPEG4_p ctx;
1272    /* Validate flag */
1273    /* Validate picture dimensions */
1274    vaStatus = psb__MPEG4_check_legal_picture(obj_context, obj_config);
1275    CHECK_VASTATUS();
1276
1277    ctx = (context_MPEG4_p) calloc(1, sizeof(struct context_MPEG4_s));
1278    CHECK_ALLOCATION(ctx);
1279
1280    obj_context->format_data = (void*) ctx;
1281    ctx->obj_context = obj_context;
1282    ctx->pic_params = NULL;
1283    ctx->load_non_intra_quant_mat = FALSE;
1284    ctx->load_intra_quant_mat = FALSE;
1285
1286    ctx->dec_ctx.begin_slice = psb__MPEG4_begin_slice;
1287    ctx->dec_ctx.process_slice = psb__MPEG4_process_slice_data;
1288    ctx->dec_ctx.end_slice = psb__MPEG4_end_slice;
1289    ctx->dec_ctx.process_buffer = pnw_MPEG4_process_buffer;
1290
1291    ctx->data_partition_buffer0 = NULL;
1292    ctx->data_partition_buffer1 = NULL;
1293
1294    switch (obj_config->profile) {
1295    case VAProfileMPEG4Simple:
1296        drv_debug_msg(VIDEO_DEBUG_GENERAL, "MPEG4_PROFILE_SIMPLE\n");
1297        ctx->profile = MPEG4_PROFILE_SIMPLE;
1298        break;
1299
1300    case VAProfileMPEG4AdvancedSimple:
1301        drv_debug_msg(VIDEO_DEBUG_GENERAL, "MPEG4_PROFILE_ASP\n");
1302        ctx->profile = MPEG4_PROFILE_ASP;
1303        break;
1304
1305    default:
1306        ASSERT(0 == 1);
1307        vaStatus = VA_STATUS_ERROR_UNKNOWN;
1308    }
1309
1310    // TODO
1311
1312    if (vaStatus == VA_STATUS_SUCCESS) {
1313        vaStatus = psb_buffer_create(obj_context->driver_data,
1314                                     FE_STATE_BUFFER_SIZE,
1315                                     psb_bt_vpu_only,
1316                                     &ctx->preload_buffer);
1317        DEBUG_FAILURE;
1318    }
1319    ctx->dec_ctx.preload_buffer = &ctx->preload_buffer;
1320
1321    if (vaStatus == VA_STATUS_SUCCESS) {
1322        vaStatus = psb_buffer_create(obj_context->driver_data,
1323                                     sizeof(gaui16mpeg4VlcTableDataPacked),
1324                                     psb_bt_cpu_vpu,
1325                                     &ctx->vlc_packed_table);
1326        DEBUG_FAILURE;
1327    }
1328    if (vaStatus == VA_STATUS_SUCCESS) {
1329        unsigned char *vlc_packed_data_address;
1330        if (0 ==  psb_buffer_map(&ctx->vlc_packed_table, &vlc_packed_data_address)) {
1331            memcpy(vlc_packed_data_address, gaui16mpeg4VlcTableDataPacked, sizeof(gaui16mpeg4VlcTableDataPacked));
1332            psb_buffer_unmap(&ctx->vlc_packed_table);
1333        } else {
1334            vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
1335            DEBUG_FAILURE;
1336        }
1337    }
1338
1339    if (vaStatus == VA_STATUS_SUCCESS) {
1340        vaStatus = vld_dec_CreateContext(&ctx->dec_ctx, obj_context);
1341        DEBUG_FAILURE;
1342    }
1343
1344    ctx->field_type = 2;
1345
1346    if (vaStatus != VA_STATUS_SUCCESS) {
1347        pnw_MPEG4_DestroyContext(obj_context);
1348    }
1349
1350    return vaStatus;
1351}
1352
1353static void pnw_MPEG4_DestroyContext(
1354    object_context_p obj_context)
1355{
1356    INIT_CONTEXT_MPEG4
1357    int i;
1358
1359    vld_dec_DestroyContext(&ctx->dec_ctx);
1360
1361    psb_buffer_destroy(&ctx->vlc_packed_table);
1362    psb_buffer_destroy(&ctx->preload_buffer);
1363
1364    if(ctx->data_partition_buffer0)
1365        psb_buffer_destroy(ctx->data_partition_buffer0);
1366
1367    if(ctx->data_partition_buffer1)
1368        psb_buffer_destroy(ctx->data_partition_buffer1);
1369
1370    ctx->data_partition_buffer0 = NULL;
1371    ctx->data_partition_buffer1 = NULL;
1372
1373    if (ctx->pic_params) {
1374        free(ctx->pic_params);
1375        ctx->pic_params = NULL;
1376    }
1377
1378    free(obj_context->format_data);
1379    obj_context->format_data = NULL;
1380}
1381
1382static VAStatus psb__MPEG4_process_picture_param(context_MPEG4_p ctx, object_buffer_p obj_buffer)
1383{
1384    VAStatus vaStatus;
1385    ASSERT(obj_buffer->type == VAPictureParameterBufferType);
1386    ASSERT(obj_buffer->num_elements == 1);
1387    ASSERT(obj_buffer->size == sizeof(VAPictureParameterBufferMPEG4));
1388
1389    if ((obj_buffer->num_elements != 1) ||
1390        (obj_buffer->size != sizeof(VAPictureParameterBufferMPEG4))) {
1391        return VA_STATUS_ERROR_UNKNOWN;
1392    }
1393
1394    /* Transfer ownership of VAPictureParameterBufferMPEG4 data */
1395    if (ctx->pic_params) {
1396        free(ctx->pic_params);
1397    }
1398    ctx->pic_params = (VAPictureParameterBufferMPEG4 *) obj_buffer->buffer_data;
1399    obj_buffer->buffer_data = NULL;
1400    obj_buffer->size = 0;
1401
1402
1403    /* Lookup surfaces for backward/forward references */
1404    /* Lookup surfaces for backward/forward references */
1405    switch (ctx->pic_params->vop_fields.bits.vop_coding_type) {
1406    case PICTURE_CODING_I:
1407        ctx->forward_ref_surface = NULL;
1408        ctx->backward_ref_surface = NULL;
1409        drv_debug_msg(VIDEO_DEBUG_GENERAL, "PICTURE_CODING_I\nTarget surface = %08x (%08x)\n", ctx->obj_context->current_render_target->psb_surface, ctx->obj_context->current_render_target->base.id);
1410        drv_debug_msg(VIDEO_DEBUG_GENERAL, "Forward ref  = NULL\n");
1411        drv_debug_msg(VIDEO_DEBUG_GENERAL, "Backward ref = NULL\n");
1412        break;
1413
1414    case PICTURE_CODING_P:
1415        ctx->forward_ref_surface = SURFACE(ctx->pic_params->forward_reference_picture);
1416        ctx->backward_ref_surface = NULL;
1417
1418        if (ctx->pic_params->forward_reference_picture == VA_INVALID_SURFACE)
1419            ctx->forward_ref_surface = NULL;
1420        if (NULL == ctx->forward_ref_surface && ctx->pic_params->forward_reference_picture != VA_INVALID_SURFACE) {
1421            return VA_STATUS_ERROR_INVALID_SURFACE;
1422        }
1423        drv_debug_msg(VIDEO_DEBUG_GENERAL, "PICTURE_CODING_P\nTarget surface = %08x (%08x)\n", ctx->obj_context->current_render_target->psb_surface, ctx->obj_context->current_render_target->base.id);
1424        drv_debug_msg(VIDEO_DEBUG_GENERAL, "Forward ref  = %08x (%08x)\n", (ctx->forward_ref_surface ? ctx->forward_ref_surface->psb_surface : 0), ctx->pic_params->forward_reference_picture);
1425        drv_debug_msg(VIDEO_DEBUG_GENERAL, "Backward ref = NULL\n");
1426        break;
1427
1428    case PICTURE_CODING_B:
1429        ctx->forward_ref_surface = SURFACE(ctx->pic_params->forward_reference_picture);
1430        ctx->backward_ref_surface = SURFACE(ctx->pic_params->backward_reference_picture);
1431        if ((NULL == ctx->forward_ref_surface) ||
1432            (NULL == ctx->backward_ref_surface)) {
1433            return VA_STATUS_ERROR_INVALID_SURFACE;
1434        }
1435        drv_debug_msg(VIDEO_DEBUG_GENERAL, "PICTURE_CODING_B\nTarget surface = %08x (%08x)\n", ctx->obj_context->current_render_target->psb_surface, ctx->obj_context->current_render_target->base.id);
1436        drv_debug_msg(VIDEO_DEBUG_GENERAL, "Forward ref  = %08x (%08x)\n", ctx->forward_ref_surface->psb_surface, ctx->pic_params->forward_reference_picture);
1437        drv_debug_msg(VIDEO_DEBUG_GENERAL, "Backward ref = %08x (%08x)\n", ctx->backward_ref_surface->psb_surface, ctx->pic_params->backward_reference_picture);
1438        break;
1439
1440    case PICTURE_CODING_S:
1441        ctx->forward_ref_surface = SURFACE(ctx->pic_params->forward_reference_picture);
1442        ctx->backward_ref_surface = SURFACE(ctx->pic_params->backward_reference_picture);
1443        drv_debug_msg(VIDEO_DEBUG_GENERAL, "PICTURE_CODING_S\nTarget surface = %08x (%08x)\n", ctx->obj_context->current_render_target->psb_surface, ctx->obj_context->current_render_target->base.id);
1444        drv_debug_msg(VIDEO_DEBUG_GENERAL, "Forward ref  = %08x (%08x)\n", ctx->forward_ref_surface ? ctx->forward_ref_surface->psb_surface : 0, ctx->pic_params->forward_reference_picture);
1445        drv_debug_msg(VIDEO_DEBUG_GENERAL, "Backward ref = %08x (%08x)\n", ctx->backward_ref_surface ? ctx->backward_ref_surface->psb_surface : 0, ctx->pic_params->backward_reference_picture);
1446        break;
1447
1448    default:
1449        drv_debug_msg(VIDEO_DEBUG_ERROR, "Unhandled MPEG4 vop_coding_type '%d'\n", ctx->pic_params->vop_fields.bits.vop_coding_type);
1450        return VA_STATUS_ERROR_UNKNOWN;
1451    }
1452
1453    if (NULL == ctx->forward_ref_surface) {
1454        /* for mmu fault protection */
1455        ctx->forward_ref_surface = ctx->obj_context->current_render_target;
1456    }
1457    if (NULL == ctx->backward_ref_surface) {
1458        /* for mmu fault protection */
1459        ctx->backward_ref_surface = ctx->obj_context->current_render_target;
1460    }
1461
1462    ctx->display_picture_width = ctx->pic_params->vop_width;
1463    ctx->display_picture_height = ctx->pic_params->vop_height;
1464    ctx->picture_width_mb = PIXELS_TO_MB(ctx->display_picture_width);
1465    ctx->picture_height_mb = PIXELS_TO_MB(ctx->display_picture_height);
1466    ctx->coded_picture_width = ctx->picture_width_mb * 16;
1467    ctx->coded_picture_height = ctx->picture_height_mb * 16;
1468    ctx->size_mb = ctx->picture_width_mb * ctx->picture_height_mb;
1469
1470    uint32_t mbInPic = ctx->picture_width_mb * ctx->picture_height_mb;
1471
1472    mbInPic += 4;
1473
1474    uint32_t colocated_size = ((mbInPic * 200) + 0xfff) & ~0xfff;
1475
1476    vaStatus = vld_dec_allocate_colocated_buffer(&ctx->dec_ctx, ctx->obj_context->current_render_target, colocated_size);
1477    CHECK_VASTATUS();
1478
1479    vaStatus = vld_dec_allocate_colocated_buffer(&ctx->dec_ctx, ctx->forward_ref_surface, colocated_size);
1480    CHECK_VASTATUS();
1481
1482    ctx->FEControl = 0;
1483    REGIO_WRITE_FIELD_LITE(ctx->FEControl ,
1484                           MSVDX_VEC,
1485                           CR_VEC_ENTDEC_FE_CONTROL,
1486                           ENTDEC_FE_PROFILE,
1487                           ctx->profile);    /* MPEG4 SP / ASP profile    */
1488
1489    REGIO_WRITE_FIELD_LITE(ctx->FEControl ,
1490                           MSVDX_VEC,
1491                           CR_VEC_ENTDEC_FE_CONTROL,
1492                           ENTDEC_FE_MODE,
1493                           4);                                /* Set MPEG4 mode            */
1494
1495    /* FE_SPS0                                                                        */
1496    ctx->FE_SPS0 = 0;
1497    REGIO_WRITE_FIELD_LITE(ctx->FE_SPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_SPS0, FE_VOP_WIDTH_IN_MBS_LESS_1,    ctx->picture_width_mb - 1);
1498    REGIO_WRITE_FIELD_LITE(ctx->FE_SPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_SPS0, FE_SHORT_HEADER_FLAG,               ctx->pic_params->vol_fields.bits.short_video_header);
1499    REGIO_WRITE_FIELD_LITE(ctx->FE_SPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_SPS0, FE_PROFILE,                    ctx->profile);
1500
1501    /* FE_VOP_SPS0                                                                    */
1502    ctx->FE_VOP_SPS0 = 0;
1503    REGIO_WRITE_FIELD_LITE(ctx->FE_VOP_SPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_VOP_SPS0, FE_VOP_HEIGHT_IN_MBS_LESS_1, ctx->picture_height_mb - 1);
1504    REGIO_WRITE_FIELD_LITE(ctx->FE_VOP_SPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_VOP_SPS0, QUANT_PRECISION,        ctx->pic_params->quant_precision);
1505    REGIO_WRITE_FIELD_LITE(ctx->FE_VOP_SPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_VOP_SPS0, FE_NO_OF_GMC_WARPING_POINTS, ((ctx->pic_params->no_of_sprite_warping_points) ? 1 : 0));
1506    REGIO_WRITE_FIELD_LITE(ctx->FE_VOP_SPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_VOP_SPS0, FE_GMC_ENABLE, (ctx->pic_params->vol_fields.bits.sprite_enable == GMC));
1507    REGIO_WRITE_FIELD_LITE(ctx->FE_VOP_SPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_VOP_SPS0, REVERSIBLE_VLC,        ctx->pic_params->vol_fields.bits.reversible_vlc);
1508    REGIO_WRITE_FIELD_LITE(ctx->FE_VOP_SPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_VOP_SPS0, FE_DATA_PARTITIONED,    ctx->pic_params->vol_fields.bits.data_partitioned);
1509    REGIO_WRITE_FIELD_LITE(ctx->FE_VOP_SPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_VOP_SPS0, FE_INTERLACED,            ctx->pic_params->vol_fields.bits.interlaced);
1510
1511    if (ctx->pic_params->vol_fields.bits.short_video_header) {
1512        /* FE_PICSH_PPS0                                                            */
1513        ctx->FE_PICSH_PPS0 = 0;
1514        REGIO_WRITE_FIELD_LITE(ctx->FE_PICSH_PPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_PICSH_PPS0, NUM_MBS_IN_GOB,        ctx->pic_params->num_macroblocks_in_gob);
1515        REGIO_WRITE_FIELD_LITE(ctx->FE_PICSH_PPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_PICSH_PPS0, NUM_GOBS_IN_VOP,        ctx->pic_params->num_gobs_in_vop);
1516        REGIO_WRITE_FIELD_LITE(ctx->FE_PICSH_PPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_PICSH_PPS0, FE_PICSH_CODING_TYPE,    ctx->pic_params->vop_fields.bits.vop_coding_type);
1517    }
1518
1519    /* FE_VOP_PPS0 */
1520    ctx->FE_VOP_PPS0 = 0;
1521    REGIO_WRITE_FIELD_LITE(ctx->FE_VOP_PPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_VOP_PPS0, BACKWARD_REF_VOP_CODING_TYPE,    ctx->pic_params->vop_fields.bits.backward_reference_vop_coding_type);
1522    REGIO_WRITE_FIELD_LITE(ctx->FE_VOP_PPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_VOP_PPS0, FE_FCODE_BACKWARD,                ctx->pic_params->vop_fcode_backward);
1523    REGIO_WRITE_FIELD_LITE(ctx->FE_VOP_PPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_VOP_PPS0, FE_FCODE_FORWARD,                ctx->pic_params->vop_fcode_forward);
1524    REGIO_WRITE_FIELD_LITE(ctx->FE_VOP_PPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_VOP_PPS0, INTRA_DC_VLC_THR,                ctx->pic_params->vop_fields.bits.intra_dc_vlc_thr);
1525    REGIO_WRITE_FIELD_LITE(ctx->FE_VOP_PPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_VOP_PPS0, FE_VOP_CODING_TYPE,              ctx->pic_params->vop_fields.bits.vop_coding_type);
1526
1527    /* BE_SPS0                                                                        */
1528    /* Common for VOPs and pictures with short header                                */
1529    ctx->BE_SPS0 = 0;
1530    REGIO_WRITE_FIELD_LITE(ctx->BE_SPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_SPS0, BE_SHORT_HEADER_FLAG,    ctx->pic_params->vol_fields.bits.short_video_header);
1531    REGIO_WRITE_FIELD_LITE(ctx->BE_SPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_SPS0, BE_PROFILE,            ctx->profile);
1532
1533    /* BE_SPS1                                                                        */
1534    /* Common for VOPs and pictures with short header                                */
1535    ctx->BE_SPS1 = 0;
1536    REGIO_WRITE_FIELD_LITE(ctx->BE_SPS1, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_SPS1, BE_VOP_WIDTH_IN_MBS_LESS_1, ctx->picture_width_mb - 1);
1537    REGIO_WRITE_FIELD_LITE(ctx->BE_SPS1, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_SPS1, VOP_HEIGHT_IN_MBS_LESS_1,   ctx->picture_height_mb - 1);
1538
1539    if (0 == ctx->pic_params->vol_fields.bits.short_video_header) {
1540        /* BE_VOP_SPS0                                                                */
1541        ctx->BE_VOP_SPS0 = 0;
1542        REGIO_WRITE_FIELD_LITE(ctx->BE_VOP_SPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_VOP_SPS0, QUANT_TYPE,     ctx->pic_params->vol_fields.bits.quant_type);
1543        REGIO_WRITE_FIELD_LITE(ctx->BE_VOP_SPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_VOP_SPS0, OBMC_DISABLE,   ctx->pic_params->vol_fields.bits.obmc_disable);
1544        REGIO_WRITE_FIELD_LITE(ctx->BE_VOP_SPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_VOP_SPS0, QUARTER_SAMPLE, ctx->pic_params->vol_fields.bits.quarter_sample);
1545
1546        /* BE_VOP_SPS1                                                                */
1547        ctx->BE_VOP_SPS1 = 0;
1548        REGIO_WRITE_FIELD_LITE(ctx->BE_VOP_SPS1, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_VOP_SPS1, GMC_WARPING_ACCURACY,        ctx->pic_params->vol_fields.bits.sprite_warping_accuracy);
1549        REGIO_WRITE_FIELD_LITE(ctx->BE_VOP_SPS1, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_VOP_SPS1, BE_NO_OF_GMC_WARPING_POINTS, ((ctx->pic_params->no_of_sprite_warping_points) ? 1 : 0));
1550        REGIO_WRITE_FIELD_LITE(ctx->BE_VOP_SPS1, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_VOP_SPS1, BE_GMC_ENABLE, (ctx->pic_params->vol_fields.bits.sprite_enable == GMC));
1551        REGIO_WRITE_FIELD_LITE(ctx->BE_VOP_SPS1, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_VOP_SPS1, BE_DATA_PARTITIONED,         ctx->pic_params->vol_fields.bits.data_partitioned);
1552        REGIO_WRITE_FIELD_LITE(ctx->BE_VOP_SPS1, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_VOP_SPS1, BE_INTERLACED,               ctx->pic_params->vol_fields.bits.interlaced);
1553
1554        /* BE_VOP_PPS0                                                                */
1555        ctx->BE_VOP_PPS0 = 0;
1556        REGIO_WRITE_FIELD_LITE(ctx->BE_VOP_PPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_VOP_PPS0, BE_FCODE_BACKWARD,                ctx->pic_params->vop_fcode_backward);
1557        REGIO_WRITE_FIELD_LITE(ctx->BE_VOP_PPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_VOP_PPS0, BE_FCODE_FORWARD,                ctx->pic_params->vop_fcode_forward);
1558        REGIO_WRITE_FIELD_LITE(ctx->BE_VOP_PPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_VOP_PPS0, ALTERNATE_VERTICAL_SCAN_FLAG,    ctx->pic_params->vop_fields.bits.alternate_vertical_scan_flag);
1559        REGIO_WRITE_FIELD_LITE(ctx->BE_VOP_PPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_VOP_PPS0, TOP_FIELD_FIRST,                ctx->pic_params->vop_fields.bits.top_field_first);
1560        REGIO_WRITE_FIELD_LITE(ctx->BE_VOP_PPS0,
1561                               MSVDX_VEC_MPEG4,
1562                               CR_VEC_MPEG4_BE_VOP_PPS0,
1563                               ROUNDING_TYPE,
1564                               ((PICTURE_CODING_I == ctx->pic_params->vop_fields.bits.vop_coding_type ||
1565                                 PICTURE_CODING_B == ctx->pic_params->vop_fields.bits.vop_coding_type) ?
1566                                0 : ctx->pic_params->vop_fields.bits.vop_rounding_type));
1567        REGIO_WRITE_FIELD_LITE(ctx->BE_VOP_PPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_VOP_PPS0, BE_VOP_CODING_TYPE,                ctx->pic_params->vop_fields.bits.vop_coding_type);
1568    } else {
1569        /* BE_VOP_PPS0                                                                */
1570        ctx->BE_VOP_PPS0 = 0;
1571        REGIO_WRITE_FIELD_LITE(ctx->BE_VOP_PPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_VOP_PPS0, BE_FCODE_FORWARD,                1);  // Always 1 in short header mode 6.3.5.2
1572        REGIO_WRITE_FIELD_LITE(ctx->BE_VOP_PPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_VOP_PPS0, ALTERNATE_VERTICAL_SCAN_FLAG,    ctx->pic_params->vop_fields.bits.alternate_vertical_scan_flag);
1573        REGIO_WRITE_FIELD_LITE(ctx->BE_VOP_PPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_VOP_PPS0, TOP_FIELD_FIRST,                ctx->pic_params->vop_fields.bits.top_field_first);
1574        REGIO_WRITE_FIELD_LITE(ctx->BE_VOP_PPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_VOP_PPS0, ROUNDING_TYPE,                     0);  // Always 0 in short header mode 6.3.5.2
1575        REGIO_WRITE_FIELD_LITE(ctx->BE_VOP_PPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_VOP_PPS0, BE_VOP_CODING_TYPE,            ctx->pic_params->vop_fields.bits.vop_coding_type);
1576
1577        /* BE_PICSH_PPS0                                                            */
1578        ctx->BE_PICSH_PPS0 = 0;
1579        REGIO_WRITE_FIELD_LITE(ctx->BE_PICSH_PPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_PICSH_PPS0, BE_PICSH_CODING_TYPE,         ctx->pic_params->vop_fields.bits.vop_coding_type);
1580    }
1581
1582    if(ctx->pic_params->vol_fields.bits.data_partitioned) {
1583        if(!ctx->data_partition_buffer0) {
1584            int size = 16 * ctx->size_mb;
1585            ctx->data_partition_buffer0 = (psb_buffer_p) calloc(1, sizeof(struct psb_buffer_s));
1586            CHECK_ALLOCATION(ctx->data_partition_buffer0);
1587            size = (size + 0xfff) & (~0xfff);
1588            vaStatus = psb_buffer_create(ctx->obj_context->driver_data, size, psb_bt_vpu_only, ctx->data_partition_buffer0);
1589            CHECK_VASTATUS();
1590        }
1591        if(!ctx->data_partition_buffer1) {
1592            int size = 16 * ctx->size_mb;
1593            ctx->data_partition_buffer1 = (psb_buffer_p) calloc(1, sizeof(struct psb_buffer_s));
1594            CHECK_ALLOCATION(ctx->data_partition_buffer1);
1595            size = (size + 0xfff) & (~0xfff);
1596            vaStatus = psb_buffer_create(ctx->obj_context->driver_data, size, psb_bt_vpu_only, ctx->data_partition_buffer1);
1597            CHECK_VASTATUS();
1598        }
1599    }
1600
1601    return VA_STATUS_SUCCESS;
1602}
1603
1604static void psb__MPEG4_convert_iq_matrix(uint32_t *dest32, unsigned char *src)
1605{
1606    int i;
1607    int *idx = scan0;
1608    uint8_t *dest8 = (uint8_t*) dest32;
1609
1610    for (i = 0; i < 64; i++) {
1611        *dest8++ = src[*idx++];
1612    }
1613}
1614
1615static VAStatus psb__MPEG4_process_iq_matrix(context_MPEG4_p ctx, object_buffer_p obj_buffer)
1616{
1617    VAIQMatrixBufferMPEG4 *iq_matrix = (VAIQMatrixBufferMPEG4 *) obj_buffer->buffer_data;
1618    ASSERT(obj_buffer->type == VAIQMatrixBufferType);
1619    ASSERT(obj_buffer->num_elements == 1);
1620    ASSERT(obj_buffer->size == sizeof(VAIQMatrixBufferMPEG4));
1621
1622    if ((obj_buffer->num_elements != 1) ||
1623        (obj_buffer->size != sizeof(VAIQMatrixBufferMPEG4))) {
1624        return VA_STATUS_ERROR_UNKNOWN;
1625    }
1626
1627    if (iq_matrix->load_non_intra_quant_mat) {
1628        psb__MPEG4_convert_iq_matrix(ctx->qmatrix_data[NONINTRA_LUMA_Q], iq_matrix->non_intra_quant_mat);
1629    }
1630    if (iq_matrix->load_intra_quant_mat) {
1631        psb__MPEG4_convert_iq_matrix(ctx->qmatrix_data[INTRA_LUMA_Q], iq_matrix->intra_quant_mat);
1632    }
1633    ctx->load_non_intra_quant_mat = iq_matrix->load_non_intra_quant_mat;
1634    ctx->load_intra_quant_mat = iq_matrix->load_intra_quant_mat;
1635
1636    return VA_STATUS_SUCCESS;
1637}
1638
1639static void psb__MPEG4_write_qmatrices(context_MPEG4_p ctx)
1640{
1641    psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
1642    int i;
1643
1644    // TODO: Verify that this is indeed the same as MPEG2
1645
1646    /* Since we only decode 4:2:0 We only need to the Intra tables.
1647    Chroma quant tables are only used in Mpeg 4:2:2 and 4:4:4.
1648    The hardware wants non-intra followed by intra */
1649    /* psb_cmdbuf_rendec_start_block( cmdbuf ); */
1650    psb_cmdbuf_rendec_start(cmdbuf, REG_MSVDX_VEC_IQRAM_OFFSET);
1651
1652    /* todo : optimisation here is to only load the need table */
1653    if (ctx->load_non_intra_quant_mat) {
1654        /*  NONINTRA_LUMA_Q --> REG_MSVDX_VEC_IQRAM_OFFSET + 0 */
1655        for (i = 0; i < 16; i++) {
1656            psb_cmdbuf_rendec_write(cmdbuf, ctx->qmatrix_data[NONINTRA_LUMA_Q][i]);
1657        }
1658    } else {
1659        for (i = 0; i < 16; i++) {
1660            psb_cmdbuf_rendec_write(cmdbuf, 0);
1661        }
1662    }
1663    if (ctx->load_intra_quant_mat) {
1664        /*  INTRA_LUMA_Q --> REG_MSVDX_VEC_IQRAM_OFFSET + (16*4) */
1665        for (i = 0; i < 16; i++) {
1666            psb_cmdbuf_rendec_write(cmdbuf, ctx->qmatrix_data[INTRA_LUMA_Q][i]);
1667        }
1668    } else {
1669        for (i = 0; i < 16; i++) {
1670            psb_cmdbuf_rendec_write(cmdbuf, 0);
1671        }
1672    }
1673
1674    psb_cmdbuf_rendec_end(cmdbuf);
1675    /* psb_cmdbuf_rendec_end_block( cmdbuf ); */
1676}
1677
1678/* Precalculated values */
1679#define ADDR0        (0x00005800)
1680#define ADDR1        (0x0001f828)
1681#define ADDR2        (0x0002b854)
1682#define ADDR3        (0x0002f85c)
1683#define ADDR4        (0x0004d089)
1684#define ADDR5        (0x0008f0aa)
1685#define ADDR6        (0x00149988)
1686#define ADDR7        (0x001d8b9e)
1687#define ADDR8        (0x000003c3)
1688#define WIDTH0        (0x09a596ed)
1689#define WIDTH1        (0x0006d6db)
1690#define OPCODE0        (0x50009a0a)
1691#define OPCODE1        (0x00000001)
1692
1693static void psb__MPEG4_write_VLC_tables(context_MPEG4_p ctx)
1694{
1695    psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
1696
1697    psb_cmdbuf_skip_start_block(cmdbuf, SKIP_ON_CONTEXT_SWITCH);
1698    /* VLC Table */
1699    /* Write a LLDMA Cmd to transfer VLD Table data */
1700    psb_cmdbuf_dma_write_cmdbuf(cmdbuf, &ctx->vlc_packed_table, 0,
1701                                  sizeof(gaui16mpeg4VlcTableDataPacked), 0,
1702                                  DMA_TYPE_VLC_TABLE);
1703
1704    /* Write the vec registers with the index data for each of the tables and then write    */
1705    /* the actual table data.                                                                */
1706    psb_cmdbuf_reg_start_block(cmdbuf, 0);
1707    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_ADDR0),            ADDR0);
1708    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_ADDR1),            ADDR1);
1709    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_ADDR2),            ADDR2);
1710    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_ADDR3),            ADDR3);
1711    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_ADDR4),            ADDR4);
1712    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_ADDR5),            ADDR5);
1713    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_ADDR6),            ADDR6);
1714    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_ADDR7),            ADDR7);
1715    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_ADDR8),            ADDR8);
1716    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_INITIAL_WIDTH0),    WIDTH0);
1717    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_INITIAL_WIDTH1),    WIDTH1);
1718    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_INITIAL_OPCODE0),    OPCODE0);
1719    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_INITIAL_OPCODE1),    OPCODE1);
1720    psb_cmdbuf_reg_end_block(cmdbuf);
1721
1722    psb_cmdbuf_skip_end_block(cmdbuf);
1723}
1724
1725static void psb__MPEG4_set_picture_params(context_MPEG4_p ctx, VASliceParameterBufferMPEG4 *slice_param)
1726{
1727    uint32_t cmd;
1728    psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
1729    psb_surface_p target_surface = ctx->obj_context->current_render_target->psb_surface;
1730
1731    psb_buffer_p colocated_target_buffer = vld_dec_lookup_colocated_buffer(&ctx->dec_ctx, target_surface);
1732    psb_buffer_p colocated_ref_buffer = vld_dec_lookup_colocated_buffer(&ctx->dec_ctx, ctx->forward_ref_surface->psb_surface); /* FIXME DE2.0 use backward ref surface */
1733    ASSERT(colocated_target_buffer);
1734    ASSERT(colocated_ref_buffer);
1735
1736    /* psb_cmdbuf_rendec_start_block( cmdbuf ); */
1737
1738    /* BE_PARAM_BASE_ADDR                                                            */
1739    psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_VEC, MPEG4_CR_VEC_MPEG4_BE_PARAM_BASE_ADDR));
1740    if (colocated_target_buffer) {
1741        psb_cmdbuf_rendec_write_address(cmdbuf, colocated_target_buffer, 0);
1742    } else {
1743        /* This is an error */
1744        psb_cmdbuf_rendec_write(cmdbuf, 0);
1745    }
1746    psb_cmdbuf_rendec_end(cmdbuf);
1747
1748    /* PARAM_BASE_ADDRESS                                                            */
1749    psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_VEC, MPEG4_CR_VEC_MPEG4_BE_COLPARAM_BASE_ADDR));
1750    if (colocated_ref_buffer) {
1751        psb_cmdbuf_rendec_write_address(cmdbuf, colocated_ref_buffer, 0);
1752    } else {
1753        /* This is an error */
1754        psb_cmdbuf_rendec_write(cmdbuf, 0);
1755    }
1756    psb_cmdbuf_rendec_end(cmdbuf);
1757
1758    vld_dec_setup_alternative_frame(ctx->obj_context);
1759
1760    /* Send VDMC and VDEB commands                                                    */
1761    psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_CMDS, DISPLAY_PICTURE_SIZE));
1762
1763    /* Display picture size cmd                                                        */
1764    cmd = 0;
1765    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, DISPLAY_PICTURE_SIZE, DISPLAY_PICTURE_HEIGHT, ctx->coded_picture_height - 1);
1766    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, DISPLAY_PICTURE_SIZE, DISPLAY_PICTURE_WIDTH,  ctx->coded_picture_width - 1);
1767    psb_cmdbuf_rendec_write(cmdbuf, cmd);
1768
1769    /* Coded picture size cmd                                                        */
1770    cmd = 0;
1771    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, CODED_PICTURE_SIZE, CODED_PICTURE_HEIGHT, ctx->coded_picture_height - 1);
1772    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, CODED_PICTURE_SIZE, CODED_PICTURE_WIDTH,  ctx->coded_picture_width - 1);
1773    psb_cmdbuf_rendec_write(cmdbuf, cmd);
1774
1775    /* Operating mode cmd                                                            */
1776    cmd = 0;
1777    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, OPERATING_MODE, CHROMA_INTERLEAVED, 0);                                 /* 0 = CbCr, 1 = CrCb        */
1778    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, OPERATING_MODE, ROW_STRIDE,         ctx->obj_context->current_render_target->psb_surface->stride_mode);
1779    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, OPERATING_MODE, CODEC_PROFILE,      ctx->profile); /* MPEG4 SP / ASP profile    */
1780    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, OPERATING_MODE, CODEC_MODE,         4);                                 /* MPEG4                    */
1781    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, OPERATING_MODE, ASYNC_MODE,         1);                                 /* VDMC only                */
1782    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, OPERATING_MODE, CHROMA_FORMAT,      1);
1783    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, OPERATING_MODE, INTERLACED,         ctx->pic_params->vol_fields.bits.interlaced);
1784    psb_cmdbuf_rendec_write(cmdbuf, cmd);
1785    ctx->obj_context->operating_mode = cmd;
1786
1787    drv_debug_msg(VIDEO_DEBUG_GENERAL, "    vop_coding_type = %s\n", psb__debug_picture_coding_str(ctx->pic_params->vop_fields.bits.vop_coding_type));
1788    drv_debug_msg(VIDEO_DEBUG_GENERAL, "    backward ref vop_coding_type = %s\n", psb__debug_picture_coding_str(ctx->pic_params->vop_fields.bits.backward_reference_vop_coding_type));
1789
1790    /* LUMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES                                    */
1791    psb_cmdbuf_rendec_write_address(cmdbuf, &target_surface->buf, target_surface->buf.buffer_ofs);
1792    /* CHROMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES                                    */
1793    psb_cmdbuf_rendec_write_address(cmdbuf, &target_surface->buf, target_surface->buf.buffer_ofs + target_surface->chroma_offset);
1794
1795    psb_cmdbuf_rendec_end(cmdbuf);
1796
1797    /* Reference pictures base addresses                                            */
1798    psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_CMDS, REFERENCE_PICTURE_BASE_ADDRESSES));
1799
1800//drv_debug_msg(VIDEO_DEBUG_GENERAL, "Target surface = %08x\n", target_surface);
1801//drv_debug_msg(VIDEO_DEBUG_GENERAL, "Forward ref = %08x\n", ctx->forward_ref_surface->psb_surface);
1802//drv_debug_msg(VIDEO_DEBUG_GENERAL, "Backward ref = %08x\n", ctx->backward_ref_surface->psb_surface);
1803
1804    /* forward reference picture */
1805    /* LUMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES                                    */
1806    psb_cmdbuf_rendec_write_address(cmdbuf, &ctx->forward_ref_surface->psb_surface->buf, ctx->forward_ref_surface->psb_surface->buf.buffer_ofs);
1807    /* CHROMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES                                    */
1808    psb_cmdbuf_rendec_write_address(cmdbuf, &ctx->forward_ref_surface->psb_surface->buf, ctx->forward_ref_surface->psb_surface->buf.buffer_ofs + ctx->forward_ref_surface->psb_surface->chroma_offset);
1809
1810    /* backward reference picture */
1811    /* LUMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES                                    */
1812    psb_cmdbuf_rendec_write_address(cmdbuf, &ctx->backward_ref_surface->psb_surface->buf, ctx->backward_ref_surface->psb_surface->buf.buffer_ofs);
1813    /* CHROMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES                                    */
1814    psb_cmdbuf_rendec_write_address(cmdbuf, &ctx->backward_ref_surface->psb_surface->buf, ctx->backward_ref_surface->psb_surface->buf.buffer_ofs + ctx->backward_ref_surface->psb_surface->chroma_offset);
1815
1816    psb_cmdbuf_rendec_end(cmdbuf);
1817    /* psb_cmdbuf_rendec_end_block( cmdbuf ); */
1818}
1819
1820static void psb__MPEG4_set_backend_registers(context_MPEG4_p ctx, VASliceParameterBufferMPEG4 *slice_param)
1821{
1822    psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
1823    uint32_t cmd;
1824    unsigned short width_mb = PIXELS_TO_MB(ctx->pic_params->vop_width);
1825
1826    /* psb_cmdbuf_rendec_start_block( cmdbuf ); */
1827
1828    /* Write Back-End EntDec registers                                                */
1829    psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_VEC, MPEG4_CR_VEC_MPEG4_BE_SPS0));
1830    /* BE_SPS0                                                                        */
1831    /* Common for VOPs and pictures with short header                                */
1832    psb_cmdbuf_rendec_write(cmdbuf, ctx->BE_SPS0);
1833    /* BE_SPS1                                                                        */
1834    /* Common for VOPs and pictures with short header                                */
1835    psb_cmdbuf_rendec_write(cmdbuf, ctx->BE_SPS1);
1836    psb_cmdbuf_rendec_end(cmdbuf);
1837
1838    psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_VEC, MPEG4_CR_VEC_MPEG4_BE_VOP_SPS0));
1839    if (0 == ctx->pic_params->vol_fields.bits.short_video_header) {
1840        /* BE_VOP_SPS0                                                                */
1841        psb_cmdbuf_rendec_write(cmdbuf, ctx->BE_VOP_SPS0);
1842        /* BE_VOP_SPS1                                                                */
1843        psb_cmdbuf_rendec_write(cmdbuf, ctx->BE_VOP_SPS1);
1844        /* BE_VOP_PPS0                                                                */
1845        psb_cmdbuf_rendec_write(cmdbuf, ctx->BE_VOP_PPS0);
1846    } else { /* Short-header mode */
1847        /* BE_VOP_SPS0 */
1848        psb_cmdbuf_rendec_write(cmdbuf, 0);
1849        /* BE_VOP_SPS1 */
1850        psb_cmdbuf_rendec_write(cmdbuf, 0);
1851        /* BE_VOP_PPS0                                                                */
1852        psb_cmdbuf_rendec_write(cmdbuf, ctx->BE_VOP_PPS0);
1853        /* BE_PICSH_PPS0                                                            */
1854        psb_cmdbuf_rendec_write(cmdbuf, ctx->BE_PICSH_PPS0);
1855    }
1856    psb_cmdbuf_rendec_end(cmdbuf);
1857
1858    if (0 == ctx->pic_params->vol_fields.bits.short_video_header) {
1859        if ((GMC == ctx->pic_params->vol_fields.bits.sprite_enable) &&
1860            (PICTURE_CODING_S == ctx->pic_params->vop_fields.bits.vop_coding_type)) {
1861            psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_VEC, MPEG4_CR_VEC_MPEG4_BE_GMC_X));
1862
1863            /* TODO: GMC Motion Vectors */
1864            /* It is still needed to specify the precision of the motion vectors (should they be in        */
1865            /* half-sample, quarter-sample...?) and how much processing    is done on the firmware on        */
1866            /* the values of the warping points.                                                        */
1867
1868            // TODO: Which index to use?
1869            int sprite_index = 0;
1870            while (sprite_index < 3) {
1871                if (ctx->pic_params->sprite_trajectory_du[sprite_index] || ctx->pic_params->sprite_trajectory_dv[sprite_index])
1872                    break;
1873                sprite_index++;
1874            }
1875            if (sprite_index >= 3)
1876                sprite_index = 0;
1877
1878            /* BE_GMC_X                                                                */
1879            cmd = 0;
1880            REGIO_WRITE_FIELD_LITE(cmd, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_GMC_X, GMC_X, ctx->pic_params->sprite_trajectory_du[sprite_index] & 0x3FFF);
1881            psb_cmdbuf_rendec_write(cmdbuf, cmd);
1882
1883            /* BE_GMC_Y                                                                */
1884            cmd = 0;
1885            REGIO_WRITE_FIELD_LITE(cmd, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_GMC_Y, GMC_Y, ctx->pic_params->sprite_trajectory_dv[sprite_index] & 0x3FFF);
1886            psb_cmdbuf_rendec_write(cmdbuf, cmd);
1887
1888            psb_cmdbuf_rendec_end(cmdbuf);
1889        }
1890    }
1891
1892    psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_VEC, MPEG4_CR_VEC_MPEG4_BE_SLICE0));
1893
1894    /* BE_SLICE0                                                                    */
1895    cmd = 0;
1896    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_SLICE0, BE_FIRST_MB_IN_SLICE_Y, slice_param->macroblock_number / width_mb);
1897    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_SLICE0, BE_FIRST_MB_IN_SLICE_X, slice_param->macroblock_number % width_mb);
1898    psb_cmdbuf_rendec_write(cmdbuf, cmd);
1899
1900    /* CR_VEC_MPEG4_BE_VOP_TR                                                        */
1901    cmd = 0;
1902    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_VOP_TRB, BE_TRB, ctx->pic_params->TRB);
1903    psb_cmdbuf_rendec_write(cmdbuf, cmd);
1904
1905    cmd = 0;
1906    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_VOP_TRD, BE_TRD, ctx->pic_params->TRD);
1907    psb_cmdbuf_rendec_write(cmdbuf, cmd);
1908
1909    psb_cmdbuf_rendec_end(cmdbuf);
1910
1911
1912    /* Send Slice Data for every slice */
1913    /* MUST be the last slice sent */
1914    psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_CMDS, SLICE_PARAMS));
1915
1916    /* Slice params command                                                            */
1917    cmd = 0;
1918    REGIO_WRITE_FIELD_LITE(cmd,
1919                           MSVDX_CMDS,
1920                           SLICE_PARAMS,
1921                           RND_CTL_BIT,
1922                           ((PICTURE_CODING_I == ctx->pic_params->vop_fields.bits.vop_coding_type ||
1923                             PICTURE_CODING_B == ctx->pic_params->vop_fields.bits.vop_coding_type) ?
1924                            0 : ctx->pic_params->vop_fields.bits.vop_rounding_type));
1925    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, SLICE_PARAMS, MODE_CONFIG,            ctx->pic_params->vol_fields.bits.sprite_warping_accuracy);
1926    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, SLICE_PARAMS, SUBPEL_FILTER_MODE,    ctx->pic_params->vol_fields.bits.quarter_sample);
1927    /* SP and ASP profiles don't support field coding in different VOPs */
1928    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, SLICE_PARAMS, SLICE_FIELD_TYPE,   2);
1929    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, SLICE_PARAMS, SLICE_CODE_TYPE,    ctx->pic_params->vop_fields.bits.vop_coding_type);
1930    psb_cmdbuf_rendec_write(cmdbuf, cmd);
1931
1932    psb_cmdbuf_rendec_end(cmdbuf);
1933
1934    *ctx->dec_ctx.p_slice_params = cmd;
1935
1936    /* CHUNK: Entdec back-end profile and level                                        */
1937    psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_VEC, CR_VEC_ENTDEC_BE_CONTROL));
1938
1939    cmd = 0;
1940    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_VEC, CR_VEC_ENTDEC_BE_CONTROL, ENTDEC_BE_PROFILE, ctx->profile);     /* MPEG4 SP / ASP profile*/
1941    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_VEC, CR_VEC_ENTDEC_BE_CONTROL, ENTDEC_BE_MODE,    4);             /* 4 - MPEG4             */
1942    psb_cmdbuf_rendec_write(cmdbuf, cmd);
1943
1944    psb_cmdbuf_rendec_end(cmdbuf);
1945    /* psb_cmdbuf_rendec_end_block( cmdbuf ); */
1946    if (ctx->pic_params->vol_fields.bits.data_partitioned)
1947    {
1948        /*set buffer pointer to store the parsed data-partition data */
1949        psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_DATAPARTITION0_BASE_ADDR));
1950        psb_cmdbuf_rendec_write_address(cmdbuf, ctx->data_partition_buffer0, ctx->data_partition_buffer0->buffer_ofs);
1951        psb_cmdbuf_rendec_end(cmdbuf);
1952
1953        psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_DATAPARTITION1_BASE_ADDR));
1954        psb_cmdbuf_rendec_write_address(cmdbuf, ctx->data_partition_buffer1, ctx->data_partition_buffer1->buffer_ofs);
1955        psb_cmdbuf_rendec_end(cmdbuf);
1956   }
1957
1958    /* Send IQ matrices to Rendec */
1959    psb__MPEG4_write_qmatrices(ctx);
1960}
1961
1962static void psb__MPEG4_set_frontend_registers(context_MPEG4_p ctx, VASliceParameterBufferMPEG4 *slice_param)
1963{
1964    psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
1965    uint32_t FE_slice0;
1966    unsigned short width_mb = PIXELS_TO_MB(ctx->pic_params->vop_width);
1967
1968    psb_cmdbuf_reg_start_block(cmdbuf, 0);
1969
1970    /* FE_SLICE0                                                                    */
1971    FE_slice0 = 0;
1972    REGIO_WRITE_FIELD_LITE(FE_slice0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_SLICE0, FE_VOP_QUANT,            slice_param->quant_scale);
1973    REGIO_WRITE_FIELD_LITE(FE_slice0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_SLICE0, FE_FIRST_MB_IN_SLICE_Y, slice_param->macroblock_number / width_mb);
1974    REGIO_WRITE_FIELD_LITE(FE_slice0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_SLICE0, FE_FIRST_MB_IN_SLICE_X, slice_param->macroblock_number % width_mb);
1975
1976    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_SLICE0) , FE_slice0);
1977
1978    /* Entdec Front-End controls*/
1979    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_ENTDEC_FE_CONTROL) , ctx->FEControl);
1980
1981    /* FE_SPS0                                                                        */
1982    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_SPS0) , ctx->FE_SPS0);
1983
1984    /* FE_VOP_SPS0                                                                    */
1985    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_VOP_SPS0) , ctx->FE_VOP_SPS0);
1986
1987
1988    if (ctx->pic_params->vol_fields.bits.short_video_header) {
1989        /* FE_PICSH_PPS0                                                            */
1990        psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_PICSH_PPS0) , ctx->FE_PICSH_PPS0);
1991    }
1992
1993    /* FE_VOP_PPS0 */
1994    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_VOP_PPS0) , ctx->FE_VOP_PPS0);
1995
1996    psb_cmdbuf_reg_end_block(cmdbuf);
1997
1998    if (ctx->pic_params->vol_fields.bits.data_partitioned)
1999    {
2000        /*set buffer pointer to store the parsed data-partition data */
2001        psb_cmdbuf_reg_start_block(cmdbuf, 0);
2002        psb_cmdbuf_reg_set_address(cmdbuf, REGISTER_OFFSET(MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_DATAPARTITION0_BASE_ADDR),
2003                                   ctx->data_partition_buffer0, ctx->data_partition_buffer0->buffer_ofs);
2004        psb_cmdbuf_reg_end_block(cmdbuf);
2005
2006        psb_cmdbuf_reg_start_block(cmdbuf, 0);
2007        psb_cmdbuf_reg_set_address(cmdbuf, REGISTER_OFFSET(MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_DATAPARTITION1_BASE_ADDR),
2008                                   ctx->data_partition_buffer1, ctx->data_partition_buffer1->buffer_ofs);
2009        psb_cmdbuf_reg_end_block(cmdbuf);
2010   }
2011}
2012
2013static void psb__MPEG4_begin_slice(context_DEC_p dec_ctx, VASliceParameterBufferBase *vld_slice_param)
2014{
2015    VASliceParameterBufferMPEG4 *slice_param = (VASliceParameterBufferMPEG4 *) vld_slice_param;
2016
2017    dec_ctx->bits_offset = slice_param->macroblock_offset;
2018    /* dec_ctx->SR_flags = 0; */
2019}
2020
2021static void psb__MPEG4_process_slice_data(context_DEC_p dec_ctx, VASliceParameterBufferBase *vld_slice_param)
2022{
2023    VASliceParameterBufferMPEG4 *slice_param = (VASliceParameterBufferMPEG4 *) vld_slice_param;
2024    context_MPEG4_p ctx = (context_MPEG4_p)dec_ctx;
2025
2026    psb__MPEG4_write_VLC_tables(ctx);
2027    psb__MPEG4_set_picture_params(ctx, slice_param);
2028    psb__MPEG4_set_frontend_registers(ctx, slice_param);
2029    psb__MPEG4_set_backend_registers(ctx, slice_param);
2030}
2031
2032static void psb__MPEG4_end_slice(context_DEC_p dec_ctx)
2033{
2034    context_MPEG4_p ctx = (context_MPEG4_p)dec_ctx;
2035
2036#ifdef PSBVIDEO_MSVDX_EC
2037    if (ctx->obj_context->driver_data->ec_enabled)
2038        ctx->obj_context->flags |= (FW_ERROR_DETECTION_AND_RECOVERY); /* FW_ERROR_DETECTION_AND_RECOVERY */
2039#endif
2040
2041    ctx->obj_context->first_mb = 0;
2042    ctx->obj_context->last_mb = ((ctx->picture_height_mb - 1) << 8) | (ctx->picture_width_mb - 1);
2043    *(ctx->dec_ctx.slice_first_pic_last) = (ctx->obj_context->first_mb << 16) | (ctx->obj_context->last_mb);
2044}
2045
2046#ifdef PSBVIDEO_MSVDX_EC
2047static void psb__MPEG4_choose_ec_frames(context_MPEG4_p ctx)
2048{
2049    int is_inter = (ctx->pic_params->vop_fields.bits.vop_coding_type == PICTURE_CODING_P ||
2050		    ctx->pic_params->vop_fields.bits.vop_coding_type == PICTURE_CODING_B);
2051
2052    ctx->obj_context->ec_target = NULL;
2053
2054    /* choose forward ref frame as possible */
2055    if (is_inter && ctx->forward_ref_surface)
2056        ctx->obj_context->ec_target = ctx->forward_ref_surface;
2057
2058    /* Otherwise we conceal from the previous I or P frame*/
2059    if (!ctx->obj_context->ec_target)
2060    {
2061        ctx->obj_context->ec_target = ctx->obj_context->ec_candidate;
2062    }
2063
2064    if (ctx->pic_params->vop_fields.bits.vop_coding_type != PICTURE_CODING_B)
2065    {
2066        ctx->obj_context->ec_candidate = ctx->obj_context->current_render_target; /* in case the next frame is an I frame we will need this */
2067    }
2068    if (!ctx->obj_context->ec_target) {
2069        ctx->obj_context->ec_target = ctx->obj_context->current_render_target;
2070    }
2071}
2072#endif
2073
2074static VAStatus pnw_MPEG4_BeginPicture(
2075    object_context_p obj_context)
2076{
2077    INIT_CONTEXT_MPEG4
2078
2079    if (ctx->pic_params) {
2080        free(ctx->pic_params);
2081        ctx->pic_params = NULL;
2082    }
2083    ctx->load_non_intra_quant_mat = FALSE;
2084    ctx->load_intra_quant_mat = FALSE;
2085
2086    return VA_STATUS_SUCCESS;
2087}
2088
2089static VAStatus pnw_MPEG4_process_buffer(
2090    context_DEC_p dec_ctx,
2091    object_buffer_p buffer)
2092{
2093    VAStatus vaStatus = VA_STATUS_SUCCESS;
2094    context_MPEG4_p ctx = (context_MPEG4_p)dec_ctx;
2095    object_buffer_p obj_buffer = buffer;
2096
2097    switch (obj_buffer->type) {
2098    case VAPictureParameterBufferType:
2099        drv_debug_msg(VIDEO_DEBUG_GENERAL, "pnw_MPEG4_RenderPicture got VAPictureParameterBuffer\n");
2100        vaStatus = psb__MPEG4_process_picture_param(ctx, obj_buffer);
2101        DEBUG_FAILURE;
2102        break;
2103
2104    case VAIQMatrixBufferType:
2105        drv_debug_msg(VIDEO_DEBUG_GENERAL, "pnw_MPEG4_RenderPicture got VAIQMatrixBufferType\n");
2106        vaStatus = psb__MPEG4_process_iq_matrix(ctx, obj_buffer);
2107        DEBUG_FAILURE;
2108        break;
2109
2110    default:
2111        vaStatus = VA_STATUS_ERROR_UNKNOWN;
2112        DEBUG_FAILURE;
2113    }
2114
2115    return vaStatus;
2116}
2117
2118static VAStatus pnw_MPEG4_EndPicture(
2119    object_context_p obj_context)
2120{
2121    INIT_CONTEXT_MPEG4
2122    psb_surface_p target_surface = ctx->obj_context->current_render_target->psb_surface;
2123    psb_driver_data_p driver_data = obj_context->driver_data;
2124
2125#ifdef PSBVIDEO_MSVDX_EC
2126    /* Sent the HOST_BE_OPP command to detect slice error */
2127    if (ctx->pic_params && ctx->pic_params->vol_fields.bits.interlaced)
2128        driver_data->ec_enabled = 0;
2129
2130    if (driver_data->ec_enabled) {
2131        uint32_t rotation_flags = 0;
2132        uint32_t ext_stride_a = 0;
2133        object_surface_p ec_target;
2134
2135        psb__MPEG4_choose_ec_frames(ctx);
2136        ec_target = ctx->obj_context->ec_target;
2137        REGIO_WRITE_FIELD_LITE(ext_stride_a, MSVDX_CMDS, EXTENDED_ROW_STRIDE, EXT_ROW_STRIDE, target_surface->stride / 64);
2138
2139    /* FIXME ec ignor rotate condition */
2140        if(ec_target)
2141            if (psb_context_submit_host_be_opp(ctx->obj_context,
2142                                          &target_surface->buf,
2143                                          &ec_target->psb_surface->buf,
2144                                          NULL,
2145                                          ctx->picture_width_mb,
2146                                          ctx->picture_height_mb,
2147                                          rotation_flags,
2148                                          ctx->field_type,
2149                                          ext_stride_a,
2150                                          target_surface->chroma_offset + target_surface->buf.buffer_ofs,
2151                                          ec_target->psb_surface->chroma_offset + ec_target->psb_surface->buf.buffer_ofs)) {
2152            return VA_STATUS_ERROR_UNKNOWN;
2153        }
2154    }
2155#endif
2156
2157    if (psb_context_flush_cmdbuf(ctx->obj_context)) {
2158        return VA_STATUS_ERROR_UNKNOWN;
2159    }
2160
2161    if (ctx->pic_params) {
2162        free(ctx->pic_params);
2163        ctx->pic_params = NULL;
2164    }
2165
2166    return VA_STATUS_SUCCESS;
2167}
2168
2169struct format_vtable_s pnw_MPEG4_vtable = {
2170queryConfigAttributes:
2171    pnw_MPEG4_QueryConfigAttributes,
2172validateConfig:
2173    pnw_MPEG4_ValidateConfig,
2174createContext:
2175    pnw_MPEG4_CreateContext,
2176destroyContext:
2177    pnw_MPEG4_DestroyContext,
2178beginPicture:
2179    pnw_MPEG4_BeginPicture,
2180renderPicture:
2181    vld_dec_RenderPicture,
2182endPicture:
2183    pnw_MPEG4_EndPicture
2184};
2185