pnw_MPEG4.c revision 35405e1aaaefd5bb307ba7f7161e138a86880020
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 *    Zeng Li <zeng.li@intel.com>
28 *
29 */
30
31#include "pnw_MPEG4.h"
32#include "psb_def.h"
33#include "psb_surface.h"
34#include "psb_cmdbuf.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#define GET_SURFACE_INFO_colocated_index(psb_surface) ((int) (psb_surface->extra_info[3]))
54#define SET_SURFACE_INFO_colocated_index(psb_surface, val) psb_surface->extra_info[3] = (uint32_t) val;
55
56#define SLICEDATA_BUFFER_TYPE(type) ((type==VASliceDataBufferType)?"VASliceDataBufferType":"VAProtectedSliceDataBufferType")
57
58#define PIXELS_TO_MB(x)    ((x + 15) / 16)
59
60/*
61 * Frame types - format dependant!
62 */
63#define PICTURE_CODING_I    0x00
64#define PICTURE_CODING_P    0x01
65#define PICTURE_CODING_B    0x02
66#define PICTURE_CODING_S    0x03
67
68
69#define FE_STATE_BUFFER_SIZE    4096
70#define FE_STATE_SAVE_SIZE      ( 0xB40 - 0x700 )
71
72#define MPEG4_PROFILE_SIMPLE    0
73#define MPEG4_PROFILE_ASP    2
74
75/* Table V2-2 ISO/IEC 14496-2:2001(E) - sprite enable codewords */
76typedef enum {
77    SPRITE_NOT_USED = 0,
78    STATIC,
79    GMC,
80} MPEG4_eSpriteEnable;
81
82
83#define MAX_QUANT_TABLES    (2) /* only 2 tables for 4:2:0 decode */
84
85static int scan0[64] = { // spec, fig 7-2
86    /*u 0  .....                   7*/
87    0,  1,  5,  6,  14, 15, 27, 28,  /* v = 0 */
88    2,  4,  7,  13, 16, 26, 29, 42,
89    3,  8,  12, 17, 25, 30, 41, 43,
90    9,  11, 18, 24, 31, 40, 44, 53,
91    10, 19, 23, 32, 39, 45, 52, 54,
92    20, 22, 33, 38, 46, 51, 55, 60,
93    21, 34, 37, 47, 50, 56, 59, 61,
94    35, 36, 48, 49, 57, 58, 62, 63  /* v = 7 */
95};
96
97typedef enum {
98    NONINTRA_LUMA_Q = 0,
99    INTRA_LUMA_Q = 1
100} QUANT_IDX;
101
102/************************************************************************************/
103/*                Variable length codes in 'packed' format                            */
104/************************************************************************************/
105
106/* Format is: opcode, width, symbol. All VLC tables are concatenated.                 */
107#define VLC_PACK(a,b,c)         ( ( (a) << 12 ) | ( (b) << 9  ) | (c) )
108static const  IMG_UINT16 gaui16mpeg4VlcTableDataPacked[] = {
109    VLC_PACK(4 , 0 , 12),    VLC_PACK(5 , 0 , 7),    VLC_PACK(4 , 2 , 13),    VLC_PACK(4 , 3 , 16),    VLC_PACK(5 , 0 , 9),    VLC_PACK(4 , 5 , 17),
110    VLC_PACK(2 , 2 , 1),    VLC_PACK(3 , 2 , 0),    VLC_PACK(3 , 2 , 0),    VLC_PACK(4 , 2 , 36),    VLC_PACK(3 , 2 , 0),    VLC_PACK(4 , 0 , 0),
111    VLC_PACK(0 , 0 , 6),    VLC_PACK(0 , 0 , 7),    VLC_PACK(2 , 1 , 8),    VLC_PACK(0 , 1 , 10),    VLC_PACK(2 , 1 , 13),    VLC_PACK(0 , 2 , 15),
112    VLC_PACK(4 , 0 , 8),    VLC_PACK(4 , 0 , 4),    VLC_PACK(4 , 0 , 2),    VLC_PACK(4 , 0 , 1),    VLC_PACK(4 , 0 , 12),    VLC_PACK(4 , 1 , 3),
113    VLC_PACK(4 , 1 , 16),    VLC_PACK(4 , 1 , 10),    VLC_PACK(4 , 1 , 9),    VLC_PACK(4 , 1 , 6),    VLC_PACK(4 , 1 , 5),    VLC_PACK(4 , 0 , 15),
114    VLC_PACK(4 , 1 , 11),    VLC_PACK(4 , 1 , 13),    VLC_PACK(3 , 2 , 0),    VLC_PACK(4 , 2 , 36),    VLC_PACK(4 , 2 , 19),    VLC_PACK(4 , 2 , 18),
115    VLC_PACK(4 , 2 , 17),    VLC_PACK(4 , 2 , 7),    VLC_PACK(4 , 1 , 14),    VLC_PACK(4 , 1 , 14),    VLC_PACK(1 , 1 , 16),    VLC_PACK(0 , 0 , 18),
116    VLC_PACK(0 , 0 , 19),    VLC_PACK(4 , 3 , 0),    VLC_PACK(4 , 3 , 12),    VLC_PACK(4 , 3 , 10),    VLC_PACK(4 , 3 , 14),    VLC_PACK(4 , 3 , 5),
117    VLC_PACK(4 , 3 , 13),    VLC_PACK(4 , 3 , 3),    VLC_PACK(4 , 3 , 11),    VLC_PACK(4 , 3 , 7),    VLC_PACK(4 , 1 , 15),    VLC_PACK(4 , 1 , 15),
118    VLC_PACK(4 , 1 , 15),    VLC_PACK(4 , 1 , 15),    VLC_PACK(3 , 1 , 0),    VLC_PACK(4 , 1 , 6),    VLC_PACK(4 , 1 , 9),    VLC_PACK(4 , 0 , 8),
119    VLC_PACK(4 , 0 , 4),    VLC_PACK(4 , 0 , 2),    VLC_PACK(4 , 0 , 1),    VLC_PACK(1 , 1 , 16),    VLC_PACK(0 , 0 , 18),    VLC_PACK(6 , 0 , 6),
120    VLC_PACK(4 , 3 , 15),    VLC_PACK(4 , 3 , 3),    VLC_PACK(4 , 3 , 5),    VLC_PACK(4 , 3 , 1),    VLC_PACK(4 , 3 , 10),    VLC_PACK(4 , 3 , 2),
121    VLC_PACK(4 , 3 , 12),    VLC_PACK(4 , 3 , 4),    VLC_PACK(4 , 3 , 8),    VLC_PACK(4 , 1 , 0),    VLC_PACK(4 , 1 , 0),    VLC_PACK(4 , 1 , 0),
122    VLC_PACK(4 , 1 , 0),    VLC_PACK(3 , 1 , 0),    VLC_PACK(4 , 1 , 9),    VLC_PACK(4 , 1 , 6),    VLC_PACK(4 , 0 , 7),    VLC_PACK(4 , 0 , 11),
123    VLC_PACK(4 , 0 , 0),    VLC_PACK(4 , 1 , 1),    VLC_PACK(4 , 1 , 2),    VLC_PACK(4 , 0 , 0),    VLC_PACK(4 , 1 , 1),    VLC_PACK(4 , 2 , 2),
124    VLC_PACK(4 , 3 , 3),    VLC_PACK(3 , 3 , 0),    VLC_PACK(4 , 0 , 0),    VLC_PACK(4 , 1 , 4),    VLC_PACK(4 , 1 , 2),    VLC_PACK(4 , 0 , 0),
125    VLC_PACK(5 , 0 , 1),    VLC_PACK(5 , 0 , 2),    VLC_PACK(5 , 0 , 3),    VLC_PACK(2 , 1 , 3),    VLC_PACK(2 , 3 , 5),    VLC_PACK(0 , 3 , 11),
126    VLC_PACK(5 , 0 , 4),    VLC_PACK(5 , 0 , 5),    VLC_PACK(5 , 0 , 6),    VLC_PACK(5 , 0 , 7),    VLC_PACK(0 , 0 , 4),    VLC_PACK(5 , 0 , 10),
127    VLC_PACK(5 , 0 , 11),    VLC_PACK(5 , 0 , 12),    VLC_PACK(5 , 0 , 9),    VLC_PACK(5 , 0 , 8),    VLC_PACK(1 , 1 , 16),    VLC_PACK(0 , 0 , 18),
128    VLC_PACK(0 , 0 , 19),    VLC_PACK(0 , 0 , 20),    VLC_PACK(5 , 0 , 24),    VLC_PACK(5 , 0 , 23),    VLC_PACK(5 , 0 , 22),    VLC_PACK(5 , 0 , 21),
129    VLC_PACK(5 , 0 , 20),    VLC_PACK(5 , 0 , 19),    VLC_PACK(5 , 0 , 18),    VLC_PACK(5 , 0 , 17),    VLC_PACK(5 , 0 , 16),    VLC_PACK(5 , 0 , 15),
130    VLC_PACK(5 , 0 , 14),    VLC_PACK(5 , 0 , 13),    VLC_PACK(3 , 1 , 0),    VLC_PACK(5 , 0 , 32),    VLC_PACK(5 , 0 , 31),    VLC_PACK(5 , 0 , 30),
131    VLC_PACK(5 , 0 , 29),    VLC_PACK(5 , 0 , 28),    VLC_PACK(5 , 0 , 27),    VLC_PACK(5 , 0 , 26),    VLC_PACK(5 , 0 , 25),    VLC_PACK(2 , 5 , 4),
132    VLC_PACK(0 , 0 , 14),    VLC_PACK(4 , 1 , 2),    VLC_PACK(4 , 1 , 1),    VLC_PACK(4 , 0 , 4),    VLC_PACK(4 , 1 , 5),    VLC_PACK(4 , 2 , 6),
133    VLC_PACK(4 , 3 , 7),    VLC_PACK(4 , 4 , 8),    VLC_PACK(4 , 5 , 9),    VLC_PACK(2 , 2 , 1),    VLC_PACK(4 , 0 , 10),    VLC_PACK(4 , 1 , 11),
134    VLC_PACK(4 , 2 , 12),    VLC_PACK(3 , 2 , 0),    VLC_PACK(4 , 0 , 3),    VLC_PACK(4 , 0 , 0),    VLC_PACK(2 , 5 , 4),    VLC_PACK(4 , 1 , 2),
135    VLC_PACK(4 , 1 , 1),    VLC_PACK(4 , 1 , 0),    VLC_PACK(4 , 0 , 3),    VLC_PACK(4 , 1 , 4),    VLC_PACK(4 , 2 , 5),    VLC_PACK(4 , 3 , 6),
136    VLC_PACK(4 , 4 , 7),    VLC_PACK(4 , 5 , 8),    VLC_PACK(2 , 3 , 1),    VLC_PACK(4 , 0 , 9),    VLC_PACK(4 , 1 , 10),    VLC_PACK(4 , 2 , 11),
137    VLC_PACK(4 , 3 , 12),    VLC_PACK(3 , 3 , 0),    VLC_PACK(2 , 1 , 16),    VLC_PACK(0 , 3 , 77),    VLC_PACK(0 , 2 , 96),    VLC_PACK(0 , 1 , 103),
138    VLC_PACK(2 , 1 , 106),    VLC_PACK(2 , 1 , 108),    VLC_PACK(5 , 1 , 250),    VLC_PACK(7 , 0 , 254),    VLC_PACK(4 , 2 , 508),    VLC_PACK(4 , 2 , 508),
139    VLC_PACK(4 , 2 , 509),    VLC_PACK(4 , 2 , 509),    VLC_PACK(4 , 3 , 506),    VLC_PACK(4 , 3 , 507),    VLC_PACK(5 , 0 , 222),    VLC_PACK(5 , 0 , 252),
140    VLC_PACK(0 , 3 , 3),    VLC_PACK(2 , 1 , 18),    VLC_PACK(0 , 3 , 44),    VLC_PACK(5 , 1 , 237),    VLC_PACK(7 , 0 , 433),    VLC_PACK(7 , 0 , 434),
141    VLC_PACK(7 , 0 , 435),    VLC_PACK(7 , 0 , 436),    VLC_PACK(7 , 0 , 437),    VLC_PACK(7 , 0 , 221),    VLC_PACK(7 , 0 , 251),    VLC_PACK(5 , 0 , 435),
142    VLC_PACK(5 , 0 , 436),    VLC_PACK(5 , 0 , 29),    VLC_PACK(5 , 0 , 61),    VLC_PACK(5 , 0 , 93),    VLC_PACK(5 , 0 , 156),    VLC_PACK(5 , 0 , 188),
143    VLC_PACK(5 , 0 , 217),    VLC_PACK(4 , 0 , 255),    VLC_PACK(0 , 3 , 2),    VLC_PACK(0 , 2 , 17),    VLC_PACK(5 , 0 , 230),    VLC_PACK(5 , 0 , 229),
144    VLC_PACK(5 , 0 , 228),    VLC_PACK(5 , 0 , 214),    VLC_PACK(5 , 0 , 60),    VLC_PACK(5 , 0 , 213),    VLC_PACK(5 , 0 , 186),    VLC_PACK(5 , 0 , 28),
145    VLC_PACK(5 , 0 , 433),    VLC_PACK(7 , 0 , 247),    VLC_PACK(7 , 0 , 93),    VLC_PACK(7 , 0 , 61),    VLC_PACK(7 , 0 , 430),    VLC_PACK(7 , 0 , 429),
146    VLC_PACK(7 , 0 , 428),    VLC_PACK(7 , 0 , 427),    VLC_PACK(5 , 0 , 232),    VLC_PACK(5 , 0 , 231),    VLC_PACK(5 , 0 , 215),    VLC_PACK(5 , 0 , 374),
147    VLC_PACK(7 , 0 , 157),    VLC_PACK(7 , 0 , 125),    VLC_PACK(7 , 0 , 432),    VLC_PACK(7 , 0 , 431),    VLC_PACK(3 , 3 , 0),    VLC_PACK(3 , 3 , 0),
148    VLC_PACK(7 , 1 , 248),    VLC_PACK(5 , 1 , 233),    VLC_PACK(7 , 0 , 189),    VLC_PACK(7 , 0 , 220),    VLC_PACK(7 , 0 , 250),    VLC_PACK(5 , 0 , 434),
149    VLC_PACK(5 , 0 , 92),    VLC_PACK(5 , 0 , 375),    VLC_PACK(5 , 0 , 124),    VLC_PACK(5 , 0 , 155),    VLC_PACK(5 , 0 , 187),    VLC_PACK(5 , 0 , 216),
150    VLC_PACK(5 , 0 , 235),    VLC_PACK(5 , 0 , 236),    VLC_PACK(0 , 0 , 16),    VLC_PACK(0 , 0 , 17),    VLC_PACK(5 , 1 , 241),    VLC_PACK(7 , 0 , 439),
151    VLC_PACK(7 , 0 , 30),    VLC_PACK(7 , 0 , 62),    VLC_PACK(7 , 0 , 252),    VLC_PACK(5 , 0 , 437),    VLC_PACK(5 , 0 , 438),    VLC_PACK(5 , 0 , 439),
152    VLC_PACK(7 , 0 , 438),    VLC_PACK(5 , 0 , 157),    VLC_PACK(5 , 0 , 219),    VLC_PACK(5 , 0 , 243),    VLC_PACK(5 , 0 , 244),    VLC_PACK(5 , 0 , 245),
153    VLC_PACK(5 , 0 , 218),    VLC_PACK(5 , 0 , 239),    VLC_PACK(5 , 0 , 125),    VLC_PACK(5 , 0 , 240),    VLC_PACK(7 , 0 , 126),    VLC_PACK(7 , 0 , 158),
154    VLC_PACK(5 , 0 , 62),    VLC_PACK(7 , 0 , 94),    VLC_PACK(5 , 0 , 30),    VLC_PACK(5 , 0 , 189),    VLC_PACK(5 , 0 , 220),    VLC_PACK(5 , 0 , 246),
155    VLC_PACK(7 , 0 , 253),    VLC_PACK(5 , 0 , 94),    VLC_PACK(7 , 0 , 190),    VLC_PACK(7 , 0 , 222),    VLC_PACK(5 , 1 , 247),    VLC_PACK(5 , 0 , 158),
156    VLC_PACK(5 , 0 , 126),    VLC_PACK(5 , 0 , 190),    VLC_PACK(5 , 0 , 249),    VLC_PACK(5 , 0 , 221),    VLC_PACK(2 , 4 , 16),    VLC_PACK(2 , 3 , 68),
157    VLC_PACK(0 , 2 , 84),    VLC_PACK(0 , 1 , 91),    VLC_PACK(1 , 1 , 94),    VLC_PACK(2 , 1 , 96),    VLC_PACK(0 , 0 , 98),    VLC_PACK(7 , 0 , 254),
158    VLC_PACK(4 , 2 , 508),    VLC_PACK(4 , 2 , 508),    VLC_PACK(4 , 2 , 509),    VLC_PACK(4 , 2 , 509),    VLC_PACK(4 , 3 , 444),    VLC_PACK(4 , 3 , 445),
159    VLC_PACK(5 , 0 , 190),    VLC_PACK(5 , 0 , 253),    VLC_PACK(2 , 3 , 6),    VLC_PACK(2 , 1 , 14),    VLC_PACK(0 , 2 , 40),    VLC_PACK(7 , 2 , 419),
160    VLC_PACK(2 , 1 , 46),    VLC_PACK(3 , 4 , 0),    VLC_PACK(2 , 2 , 5),    VLC_PACK(7 , 2 , 426),    VLC_PACK(7 , 1 , 424),    VLC_PACK(7 , 0 , 423),
161    VLC_PACK(5 , 1 , 246),    VLC_PACK(5 , 2 , 427),    VLC_PACK(5 , 1 , 425),    VLC_PACK(7 , 0 , 253),    VLC_PACK(7 , 0 , 430),    VLC_PACK(4 , 0 , 255),
162    VLC_PACK(0 , 3 , 2),    VLC_PACK(0 , 2 , 17),    VLC_PACK(5 , 0 , 217),    VLC_PACK(5 , 0 , 187),    VLC_PACK(5 , 0 , 124),    VLC_PACK(5 , 0 , 92),
163    VLC_PACK(5 , 0 , 60),    VLC_PACK(5 , 0 , 373),    VLC_PACK(5 , 0 , 422),    VLC_PACK(5 , 0 , 421),    VLC_PACK(7 , 0 , 414),    VLC_PACK(7 , 0 , 413),
164    VLC_PACK(7 , 0 , 412),    VLC_PACK(7 , 0 , 411),    VLC_PACK(7 , 0 , 410),    VLC_PACK(7 , 0 , 409),    VLC_PACK(7 , 0 , 408),    VLC_PACK(7 , 0 , 407),
165    VLC_PACK(5 , 0 , 243),    VLC_PACK(5 , 0 , 218),    VLC_PACK(5 , 0 , 424),    VLC_PACK(5 , 0 , 423),    VLC_PACK(7 , 0 , 418),    VLC_PACK(7 , 0 , 417),
166    VLC_PACK(7 , 0 , 416),    VLC_PACK(7 , 0 , 415),    VLC_PACK(5 , 0 , 374),    VLC_PACK(5 , 0 , 375),    VLC_PACK(5 , 0 , 29),    VLC_PACK(5 , 0 , 61),
167    VLC_PACK(5 , 0 , 93),    VLC_PACK(5 , 0 , 156),    VLC_PACK(5 , 0 , 188),    VLC_PACK(5 , 0 , 219),    VLC_PACK(5 , 1 , 244),    VLC_PACK(7 , 0 , 252),
168    VLC_PACK(7 , 0 , 221),    VLC_PACK(0 , 2 , 5),    VLC_PACK(7 , 2 , 432),    VLC_PACK(0 , 0 , 11),    VLC_PACK(0 , 0 , 12),    VLC_PACK(5 , 1 , 431),
169    VLC_PACK(7 , 0 , 436),    VLC_PACK(7 , 0 , 437),    VLC_PACK(7 , 0 , 438),    VLC_PACK(5 , 0 , 433),    VLC_PACK(5 , 0 , 434),    VLC_PACK(5 , 0 , 189),
170    VLC_PACK(5 , 0 , 220),    VLC_PACK(5 , 0 , 250),    VLC_PACK(5 , 1 , 248),    VLC_PACK(7 , 0 , 431),    VLC_PACK(5 , 0 , 125),    VLC_PACK(5 , 0 , 157),
171    VLC_PACK(7 , 0 , 439),    VLC_PACK(7 , 0 , 30),    VLC_PACK(7 , 0 , 62),    VLC_PACK(7 , 0 , 94),    VLC_PACK(5 , 0 , 435),    VLC_PACK(5 , 0 , 436),
172    VLC_PACK(5 , 0 , 437),    VLC_PACK(5 , 0 , 251),    VLC_PACK(7 , 0 , 126),    VLC_PACK(7 , 0 , 158),    VLC_PACK(7 , 0 , 190),    VLC_PACK(7 , 0 , 222),
173    VLC_PACK(5 , 1 , 438),    VLC_PACK(5 , 0 , 30),    VLC_PACK(5 , 0 , 62),    VLC_PACK(5 , 0 , 94),    VLC_PACK(5 , 0 , 252),    VLC_PACK(5 , 0 , 221),
174    VLC_PACK(5 , 0 , 126),    VLC_PACK(5 , 0 , 158),    VLC_PACK(5 , 0 , 255),    VLC_PACK(5 , 0 , 222),    VLC_PACK(0 , 0 , 14),    VLC_PACK(0 , 1 , 15),
175    VLC_PACK(0 , 0 , 44),    VLC_PACK(0 , 1 , 45),    VLC_PACK(0 , 1 , 74),    VLC_PACK(0 , 2 , 103),    VLC_PACK(0 , 1 , 230),    VLC_PACK(0 , 0 , 256),
176    VLC_PACK(5 , 0 , 252),    VLC_PACK(7 , 0 , 254),    VLC_PACK(4 , 3 , 508),    VLC_PACK(4 , 3 , 509),    VLC_PACK(4 , 3 , 506),    VLC_PACK(4 , 3 , 507),
177    VLC_PACK(5 , 0 , 190),    VLC_PACK(5 , 0 , 158),    VLC_PACK(5 , 0 , 126),    VLC_PACK(5 , 0 , 94),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),
178    VLC_PACK(5 , 0 , 62),    VLC_PACK(5 , 0 , 30),    VLC_PACK(5 , 0 , 439),    VLC_PACK(5 , 0 , 438),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),
179    VLC_PACK(5 , 0 , 437),    VLC_PACK(5 , 0 , 93),    VLC_PACK(5 , 0 , 436),    VLC_PACK(5 , 0 , 435),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),
180    VLC_PACK(5 , 0 , 434),    VLC_PACK(5 , 0 , 374),    VLC_PACK(5 , 0 , 373),    VLC_PACK(5 , 0 , 123),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),
181    VLC_PACK(5 , 0 , 433),    VLC_PACK(5 , 0 , 432),    VLC_PACK(5 , 0 , 431),    VLC_PACK(5 , 0 , 430),    VLC_PACK(0 , 0 , 2),    VLC_PACK(3 , 1 , 0),
182    VLC_PACK(5 , 0 , 228),    VLC_PACK(5 , 0 , 150),    VLC_PACK(5 , 0 , 221),    VLC_PACK(5 , 0 , 251),    VLC_PACK(5 , 0 , 250),    VLC_PACK(5 , 0 , 249),
183    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 189),    VLC_PACK(5 , 0 , 220),    VLC_PACK(5 , 0 , 157),    VLC_PACK(5 , 0 , 125),
184    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 188),    VLC_PACK(5 , 0 , 156),    VLC_PACK(5 , 0 , 61),    VLC_PACK(5 , 0 , 29),
185    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 92),    VLC_PACK(5 , 0 , 60),    VLC_PACK(5 , 0 , 91),    VLC_PACK(5 , 0 , 59),
186    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 372),    VLC_PACK(5 , 0 , 311),    VLC_PACK(5 , 0 , 429),    VLC_PACK(5 , 0 , 503),
187    VLC_PACK(0 , 0 , 2),    VLC_PACK(3 , 1 , 0),    VLC_PACK(5 , 0 , 58),    VLC_PACK(5 , 0 , 26),    VLC_PACK(7 , 0 , 158),    VLC_PACK(7 , 0 , 126),
188    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 248),    VLC_PACK(7 , 0 , 30),    VLC_PACK(5 , 0 , 219),    VLC_PACK(5 , 0 , 218),
189    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 217),    VLC_PACK(5 , 0 , 245),    VLC_PACK(5 , 0 , 375),    VLC_PACK(5 , 0 , 124),
190    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 28),    VLC_PACK(5 , 0 , 155),    VLC_PACK(5 , 0 , 154),    VLC_PACK(5 , 0 , 122),
191    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 310),    VLC_PACK(5 , 0 , 27),    VLC_PACK(5 , 0 , 90),    VLC_PACK(5 , 0 , 121),
192    VLC_PACK(0 , 0 , 2),    VLC_PACK(3 , 1 , 0),    VLC_PACK(5 , 0 , 502),    VLC_PACK(5 , 0 , 371),    VLC_PACK(7 , 0 , 439),    VLC_PACK(7 , 0 , 438),
193    VLC_PACK(0 , 0 , 6),    VLC_PACK(0 , 1 , 7),    VLC_PACK(0 , 0 , 27),    VLC_PACK(0 , 1 , 28),    VLC_PACK(0 , 1 , 48),    VLC_PACK(0 , 2 , 68),
194    VLC_PACK(5 , 0 , 247),    VLC_PACK(5 , 0 , 246),    VLC_PACK(5 , 0 , 244),    VLC_PACK(7 , 0 , 221),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),
195    VLC_PACK(5 , 0 , 187),    VLC_PACK(5 , 0 , 216),    VLC_PACK(5 , 0 , 186),    VLC_PACK(5 , 0 , 185),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),
196    VLC_PACK(5 , 0 , 213),    VLC_PACK(5 , 0 , 237),    VLC_PACK(5 , 0 , 153),    VLC_PACK(5 , 0 , 184),    VLC_PACK(0 , 0 , 2),    VLC_PACK(2 , 1 , 3),
197    VLC_PACK(5 , 0 , 89),    VLC_PACK(5 , 0 , 152),    VLC_PACK(3 , 1 , 0),    VLC_PACK(7 , 0 , 218),    VLC_PACK(5 , 0 , 428),    VLC_PACK(7 , 0 , 253),
198    VLC_PACK(7 , 0 , 435),    VLC_PACK(7 , 0 , 432),    VLC_PACK(7 , 0 , 431),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 243),
199    VLC_PACK(5 , 0 , 242),    VLC_PACK(5 , 0 , 215),    VLC_PACK(5 , 0 , 214),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 236),
200    VLC_PACK(5 , 0 , 233),    VLC_PACK(5 , 0 , 183),    VLC_PACK(5 , 0 , 182),    VLC_PACK(0 , 0 , 2),    VLC_PACK(2 , 1 , 3),    VLC_PACK(5 , 0 , 151),
201    VLC_PACK(5 , 0 , 181),    VLC_PACK(3 , 1 , 0),    VLC_PACK(7 , 0 , 370),    VLC_PACK(7 , 0 , 188),    VLC_PACK(7 , 0 , 430),    VLC_PACK(7 , 0 , 429),
202    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 241),    VLC_PACK(7 , 0 , 426),    VLC_PACK(5 , 0 , 240),    VLC_PACK(5 , 0 , 239),
203    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(7 , 0 , 220),    VLC_PACK(7 , 0 , 157),    VLC_PACK(5 , 0 , 212),    VLC_PACK(5 , 0 , 235),
204    VLC_PACK(0 , 0 , 2),    VLC_PACK(2 , 1 , 3),    VLC_PACK(5 , 0 , 180),    VLC_PACK(5 , 0 , 211),    VLC_PACK(3 , 1 , 0),    VLC_PACK(7 , 0 , 405),
205    VLC_PACK(7 , 0 , 406),    VLC_PACK(7 , 0 , 425),    VLC_PACK(7 , 0 , 424),    VLC_PACK(0 , 0 , 6),    VLC_PACK(0 , 1 , 7),    VLC_PACK(0 , 0 , 18),
206    VLC_PACK(0 , 1 , 19),    VLC_PACK(0 , 1 , 27),    VLC_PACK(2 , 2 , 35),    VLC_PACK(5 , 0 , 238),    VLC_PACK(7 , 0 , 252),    VLC_PACK(7 , 0 , 125),
207    VLC_PACK(7 , 0 , 418),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 234),    VLC_PACK(5 , 0 , 232),    VLC_PACK(5 , 0 , 210),
208    VLC_PACK(5 , 0 , 231),    VLC_PACK(0 , 0 , 2),    VLC_PACK(3 , 1 , 0),    VLC_PACK(7 , 0 , 404),    VLC_PACK(7 , 0 , 403),    VLC_PACK(7 , 0 , 189),
209    VLC_PACK(7 , 0 , 421),    VLC_PACK(7 , 0 , 417),    VLC_PACK(7 , 0 , 416),    VLC_PACK(0 , 0 , 2),    VLC_PACK(2 , 1 , 3),    VLC_PACK(7 , 0 , 251),
210    VLC_PACK(7 , 0 , 93),    VLC_PACK(3 , 1 , 0),    VLC_PACK(5 , 0 , 229),    VLC_PACK(5 , 0 , 230),    VLC_PACK(7 , 0 , 415),    VLC_PACK(7 , 0 , 414),
211    VLC_PACK(0 , 0 , 2),    VLC_PACK(2 , 1 , 3),    VLC_PACK(7 , 0 , 61),    VLC_PACK(7 , 0 , 29),    VLC_PACK(3 , 1 , 0),    VLC_PACK(7 , 0 , 219),
212    VLC_PACK(7 , 0 , 250),    VLC_PACK(2 , 2 , 4),    VLC_PACK(2 , 1 , 7),    VLC_PACK(7 , 0 , 374),    VLC_PACK(7 , 0 , 375),    VLC_PACK(3 , 2 , 0),
213    VLC_PACK(3 , 2 , 0),    VLC_PACK(7 , 0 , 409),    VLC_PACK(7 , 0 , 371),    VLC_PACK(3 , 1 , 0),    VLC_PACK(7 , 0 , 372),    VLC_PACK(7 , 0 , 373),
214    VLC_PACK(0 , 1 , 4),    VLC_PACK(0 , 0 , 24),    VLC_PACK(7 , 0 , 94),    VLC_PACK(7 , 0 , 62),    VLC_PACK(0 , 1 , 4),    VLC_PACK(0 , 0 , 18),
215    VLC_PACK(7 , 0 , 434),    VLC_PACK(7 , 0 , 433),    VLC_PACK(0 , 1 , 4),    VLC_PACK(0 , 0 , 12),    VLC_PACK(7 , 0 , 423),    VLC_PACK(7 , 0 , 422),
216    VLC_PACK(1 , 1 , 4),    VLC_PACK(0 , 0 , 6),    VLC_PACK(7 , 0 , 413),    VLC_PACK(7 , 0 , 412),    VLC_PACK(3 , 1 , 0),    VLC_PACK(7 , 0 , 408),
217    VLC_PACK(7 , 0 , 407),    VLC_PACK(7 , 0 , 411),    VLC_PACK(7 , 0 , 410),    VLC_PACK(7 , 0 , 420),    VLC_PACK(7 , 0 , 419),    VLC_PACK(7 , 0 , 428),
218    VLC_PACK(7 , 0 , 427),    VLC_PACK(7 , 0 , 437),    VLC_PACK(7 , 0 , 436),    VLC_PACK(7 , 0 , 222),    VLC_PACK(7 , 0 , 190),    VLC_PACK(5 , 0 , 255),
219    VLC_PACK(5 , 0 , 253),    VLC_PACK(0 , 0 , 14),    VLC_PACK(0 , 1 , 15),    VLC_PACK(0 , 0 , 44),    VLC_PACK(0 , 1 , 45),    VLC_PACK(0 , 1 , 74),
220    VLC_PACK(0 , 2 , 103),    VLC_PACK(0 , 1 , 230),    VLC_PACK(0 , 0 , 256),    VLC_PACK(5 , 0 , 190),    VLC_PACK(7 , 0 , 254),    VLC_PACK(4 , 3 , 508),
221    VLC_PACK(4 , 3 , 509),    VLC_PACK(4 , 3 , 444),    VLC_PACK(4 , 3 , 445),    VLC_PACK(5 , 0 , 252),    VLC_PACK(5 , 0 , 158),    VLC_PACK(5 , 0 , 221),
222    VLC_PACK(5 , 0 , 62),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 251),    VLC_PACK(5 , 0 , 189),    VLC_PACK(5 , 0 , 250),
223    VLC_PACK(5 , 0 , 249),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 248),    VLC_PACK(5 , 0 , 219),    VLC_PACK(5 , 0 , 247),
224    VLC_PACK(5 , 0 , 246),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 245),    VLC_PACK(5 , 0 , 244),    VLC_PACK(5 , 0 , 243),
225    VLC_PACK(5 , 0 , 216),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 242),    VLC_PACK(5 , 0 , 241),    VLC_PACK(5 , 0 , 238),
226    VLC_PACK(5 , 0 , 237),    VLC_PACK(0 , 0 , 2),    VLC_PACK(3 , 1 , 0),    VLC_PACK(5 , 0 , 236),    VLC_PACK(5 , 0 , 152),    VLC_PACK(5 , 0 , 126),
227    VLC_PACK(5 , 0 , 94),    VLC_PACK(5 , 0 , 30),    VLC_PACK(5 , 0 , 439),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 438),
228    VLC_PACK(5 , 0 , 437),    VLC_PACK(5 , 0 , 220),    VLC_PACK(5 , 0 , 157),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 188),
229    VLC_PACK(5 , 0 , 93),    VLC_PACK(5 , 0 , 218),    VLC_PACK(5 , 0 , 156),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 217),
230    VLC_PACK(5 , 0 , 187),    VLC_PACK(5 , 0 , 186),    VLC_PACK(5 , 0 , 155),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 240),
231    VLC_PACK(5 , 0 , 239),    VLC_PACK(5 , 0 , 214),    VLC_PACK(5 , 0 , 213),    VLC_PACK(0 , 0 , 2),    VLC_PACK(3 , 1 , 0),    VLC_PACK(5 , 0 , 122),
232    VLC_PACK(5 , 0 , 27),    VLC_PACK(7 , 0 , 158),    VLC_PACK(7 , 0 , 126),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 436),
233    VLC_PACK(7 , 0 , 30),    VLC_PACK(5 , 0 , 125),    VLC_PACK(5 , 0 , 435),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 432),
234    VLC_PACK(5 , 0 , 431),    VLC_PACK(5 , 0 , 61),    VLC_PACK(5 , 0 , 29),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 124),
235    VLC_PACK(5 , 0 , 92),    VLC_PACK(5 , 0 , 60),    VLC_PACK(5 , 0 , 28),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 215),
236    VLC_PACK(5 , 0 , 154),    VLC_PACK(5 , 0 , 185),    VLC_PACK(5 , 0 , 184),    VLC_PACK(0 , 0 , 2),    VLC_PACK(3 , 1 , 0),    VLC_PACK(5 , 0 , 366),
237    VLC_PACK(5 , 0 , 410),    VLC_PACK(7 , 0 , 439),    VLC_PACK(7 , 0 , 438),    VLC_PACK(0 , 0 , 6),    VLC_PACK(0 , 1 , 7),    VLC_PACK(0 , 0 , 27),
238    VLC_PACK(0 , 1 , 28),    VLC_PACK(0 , 1 , 48),    VLC_PACK(0 , 2 , 68),    VLC_PACK(5 , 0 , 434),    VLC_PACK(5 , 0 , 433),    VLC_PACK(5 , 0 , 430),
239    VLC_PACK(7 , 0 , 221),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 375),    VLC_PACK(5 , 0 , 374),    VLC_PACK(5 , 0 , 373),
240    VLC_PACK(5 , 0 , 426),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 372),    VLC_PACK(5 , 0 , 420),    VLC_PACK(5 , 0 , 123),
241    VLC_PACK(5 , 0 , 91),    VLC_PACK(0 , 0 , 2),    VLC_PACK(2 , 1 , 3),    VLC_PACK(5 , 0 , 153),    VLC_PACK(5 , 0 , 59),    VLC_PACK(3 , 1 , 0),
242    VLC_PACK(7 , 0 , 218),    VLC_PACK(5 , 0 , 409),    VLC_PACK(7 , 0 , 253),    VLC_PACK(7 , 0 , 435),    VLC_PACK(7 , 0 , 432),    VLC_PACK(7 , 0 , 431),
243    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 429),    VLC_PACK(5 , 0 , 428),    VLC_PACK(5 , 0 , 425),    VLC_PACK(5 , 0 , 424),
244    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 419),    VLC_PACK(5 , 0 , 418),    VLC_PACK(5 , 0 , 311),    VLC_PACK(5 , 0 , 371),
245    VLC_PACK(0 , 0 , 2),    VLC_PACK(2 , 1 , 3),    VLC_PACK(5 , 0 , 310),    VLC_PACK(5 , 0 , 370),    VLC_PACK(3 , 1 , 0),    VLC_PACK(7 , 0 , 370),
246    VLC_PACK(7 , 0 , 188),    VLC_PACK(7 , 0 , 430),    VLC_PACK(7 , 0 , 429),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(5 , 0 , 427),
247    VLC_PACK(7 , 0 , 426),    VLC_PACK(5 , 0 , 423),    VLC_PACK(5 , 0 , 422),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),    VLC_PACK(7 , 0 , 220),
248    VLC_PACK(7 , 0 , 157),    VLC_PACK(5 , 0 , 417),    VLC_PACK(5 , 0 , 416),    VLC_PACK(0 , 0 , 2),    VLC_PACK(2 , 1 , 3),    VLC_PACK(5 , 0 , 369),
249    VLC_PACK(5 , 0 , 368),    VLC_PACK(3 , 1 , 0),    VLC_PACK(7 , 0 , 405),    VLC_PACK(7 , 0 , 406),    VLC_PACK(7 , 0 , 425),    VLC_PACK(7 , 0 , 424),
250    VLC_PACK(0 , 0 , 6),    VLC_PACK(0 , 1 , 7),    VLC_PACK(0 , 0 , 18),    VLC_PACK(0 , 1 , 19),    VLC_PACK(0 , 1 , 27),    VLC_PACK(2 , 2 , 35),
251    VLC_PACK(5 , 0 , 421),    VLC_PACK(7 , 0 , 252),    VLC_PACK(7 , 0 , 125),    VLC_PACK(7 , 0 , 418),    VLC_PACK(0 , 0 , 2),    VLC_PACK(0 , 1 , 3),
252    VLC_PACK(5 , 0 , 415),    VLC_PACK(5 , 0 , 414),    VLC_PACK(5 , 0 , 367),    VLC_PACK(5 , 0 , 413),    VLC_PACK(0 , 0 , 2),    VLC_PACK(3 , 1 , 0),
253    VLC_PACK(7 , 0 , 404),    VLC_PACK(7 , 0 , 403),    VLC_PACK(7 , 0 , 189),    VLC_PACK(7 , 0 , 421),    VLC_PACK(7 , 0 , 417),    VLC_PACK(7 , 0 , 416),
254    VLC_PACK(0 , 0 , 2),    VLC_PACK(2 , 1 , 3),    VLC_PACK(7 , 0 , 251),    VLC_PACK(7 , 0 , 93),    VLC_PACK(3 , 1 , 0),    VLC_PACK(5 , 0 , 411),
255    VLC_PACK(5 , 0 , 412),    VLC_PACK(7 , 0 , 415),    VLC_PACK(7 , 0 , 414),    VLC_PACK(0 , 0 , 2),    VLC_PACK(2 , 1 , 3),    VLC_PACK(7 , 0 , 61),
256    VLC_PACK(7 , 0 , 29),    VLC_PACK(3 , 1 , 0),    VLC_PACK(7 , 0 , 219),    VLC_PACK(7 , 0 , 250),    VLC_PACK(2 , 2 , 4),    VLC_PACK(2 , 1 , 7),
257    VLC_PACK(7 , 0 , 374),    VLC_PACK(7 , 0 , 375),    VLC_PACK(3 , 2 , 0),    VLC_PACK(3 , 2 , 0),    VLC_PACK(7 , 0 , 409),    VLC_PACK(7 , 0 , 371),
258    VLC_PACK(3 , 1 , 0),    VLC_PACK(7 , 0 , 372),    VLC_PACK(7 , 0 , 373),    VLC_PACK(0 , 1 , 4),    VLC_PACK(0 , 0 , 24),    VLC_PACK(7 , 0 , 94),
259    VLC_PACK(7 , 0 , 62),    VLC_PACK(0 , 1 , 4),    VLC_PACK(0 , 0 , 18),    VLC_PACK(7 , 0 , 434),    VLC_PACK(7 , 0 , 433),    VLC_PACK(0 , 1 , 4),
260    VLC_PACK(0 , 0 , 12),    VLC_PACK(7 , 0 , 423),    VLC_PACK(7 , 0 , 422),    VLC_PACK(1 , 1 , 4),    VLC_PACK(0 , 0 , 6),    VLC_PACK(7 , 0 , 413),
261    VLC_PACK(7 , 0 , 412),    VLC_PACK(3 , 1 , 0),    VLC_PACK(7 , 0 , 408),    VLC_PACK(7 , 0 , 407),    VLC_PACK(7 , 0 , 411),    VLC_PACK(7 , 0 , 410),
262    VLC_PACK(7 , 0 , 420),    VLC_PACK(7 , 0 , 419),    VLC_PACK(7 , 0 , 428),    VLC_PACK(7 , 0 , 427),    VLC_PACK(7 , 0 , 437),    VLC_PACK(7 , 0 , 436),
263    VLC_PACK(7 , 0 , 222),    VLC_PACK(7 , 0 , 190),    VLC_PACK(4 , 0 , 0),    VLC_PACK(5 , 0 , 1),    VLC_PACK(5 , 0 , 2),    VLC_PACK(5 , 0 , 3),
264    VLC_PACK(5 , 0 , 4),    VLC_PACK(5 , 0 , 5),    VLC_PACK(1 , 5 , 1),    VLC_PACK(5 , 0 , 6),    VLC_PACK(5 , 0 , 7),    VLC_PACK(5 , 0 , 8),
265    VLC_PACK(5 , 0 , 9),    VLC_PACK(5 , 0 , 10),    VLC_PACK(5 , 0 , 11),    VLC_PACK(1 , 3 , 1),    VLC_PACK(5 , 0 , 12),    VLC_PACK(5 , 0 , 13),
266    VLC_PACK(5 , 0 , 14),    VLC_PACK(5 , 0 , 15),    VLC_PACK(5 , 1 , 16),    VLC_PACK(5 , 0 , 1),    VLC_PACK(5 , 0 , 2),    VLC_PACK(5 , 0 , 3),
267    VLC_PACK(5 , 0 , 4),    VLC_PACK(5 , 0 , 5),    VLC_PACK(5 , 0 , 6),    VLC_PACK(1 , 5 , 1),    VLC_PACK(5 , 0 , 7),    VLC_PACK(5 , 0 , 8),
268    VLC_PACK(5 , 0 , 9),    VLC_PACK(5 , 0 , 10),    VLC_PACK(5 , 0 , 11),    VLC_PACK(5 , 0 , 12),    VLC_PACK(1 , 2 , 1),    VLC_PACK(5 , 0 , 13),
269    VLC_PACK(5 , 0 , 14),    VLC_PACK(5 , 0 , 15),    VLC_PACK(5 , 1 , 16),    VLC_PACK(4 , 0 , 1),    VLC_PACK(4 , 1 , 2),    VLC_PACK(4 , 1 , 4),
270    VLC_PACK(0 , 0 , 2),
271};
272
273
274struct context_MPEG4_s {
275    object_context_p obj_context; /* back reference */
276
277    uint32_t profile;
278
279    /* Picture parameters */
280    VAPictureParameterBufferMPEG4 *pic_params;
281    object_surface_p forward_ref_surface;
282    object_surface_p backward_ref_surface;
283
284    uint32_t display_picture_width;    /* in pixels */
285    uint32_t display_picture_height;    /* in pixels */
286
287    uint32_t coded_picture_width;    /* in pixels */
288    uint32_t coded_picture_height;    /* in pixels */
289
290    uint32_t picture_width_mb;        /* in macroblocks */
291    uint32_t picture_height_mb;        /* in macroblocks */
292    uint32_t size_mb;                /* in macroblocks */
293
294    uint32_t FEControl;
295    uint32_t FE_SPS0;
296    uint32_t FE_VOP_PPS0;
297    uint32_t FE_VOP_SPS0;
298    uint32_t FE_PICSH_PPS0;
299
300    uint32_t BE_SPS0;
301    uint32_t BE_SPS1;
302    uint32_t BE_VOP_PPS0;
303    uint32_t BE_VOP_SPS0;
304    uint32_t BE_VOP_SPS1;
305    uint32_t BE_PICSH_PPS0;
306
307    /* IQ Matrix */
308    uint32_t qmatrix_data[MAX_QUANT_TABLES][16];
309    int load_non_intra_quant_mat;
310    int load_intra_quant_mat;
311
312    /* Split buffers */
313    int split_buffer_pending;
314
315    /* List of VASliceParameterBuffers */
316    object_buffer_p *slice_param_list;
317    int slice_param_list_size;
318    int slice_param_list_idx;
319
320    /* VLC packed data */
321    struct psb_buffer_s vlc_packed_table;
322
323    /* FE state buffer */
324    struct psb_buffer_s FE_state_buffer;
325
326    /* CoLocated buffers */
327    struct psb_buffer_s *colocated_buffers;
328    int colocated_buffers_size;
329    int colocated_buffers_idx;
330
331    uint32_t *p_range_mapping_base0;
332    uint32_t *p_range_mapping_base1;
333    uint32_t *p_slice_params; /* pointer to ui32SliceParams in CMD_HEADER */
334    uint32_t *slice_first_pic_last;
335    uint32_t *alt_output_flags;
336};
337
338typedef struct context_MPEG4_s *context_MPEG4_p;
339
340#define INIT_CONTEXT_MPEG4    context_MPEG4_p ctx = (context_MPEG4_p) obj_context->format_data;
341
342#define SURFACE(id)    ((object_surface_p) object_heap_lookup( &ctx->obj_context->driver_data->surface_heap, id ))
343
344static const char *psb__debug_picture_coding_str(unsigned char vop_coding_type)
345{
346    switch (vop_coding_type) {
347    case PICTURE_CODING_I:
348        return ("PICTURE_CODING_I");
349    case PICTURE_CODING_P:
350        return ("PICTURE_CODING_P");
351    case PICTURE_CODING_B:
352        return ("PICTURE_CODING_B");
353    case PICTURE_CODING_S:
354        return ("PICTURE_CODING_S");
355    }
356    return ("UNKNOWN!!!");
357}
358
359
360static void pnw_MPEG4_QueryConfigAttributes(
361    VAProfile profile,
362    VAEntrypoint entrypoint,
363    VAConfigAttrib *attrib_list,
364    int num_attribs)
365{
366    /* No MPEG4 specific attributes */
367}
368
369static VAStatus pnw_MPEG4_ValidateConfig(
370    object_config_p obj_config)
371{
372    int i;
373    /* Check all attributes */
374    for (i = 0; i < obj_config->attrib_count; i++) {
375        switch (obj_config->attrib_list[i].type) {
376        case VAConfigAttribRTFormat:
377            /* Ignore */
378            break;
379
380        default:
381            return VA_STATUS_ERROR_ATTR_NOT_SUPPORTED;
382        }
383    }
384
385    return VA_STATUS_SUCCESS;
386}
387
388static VAStatus psb__MPEG4_check_legal_picture(object_context_p obj_context, object_config_p obj_config)
389{
390    VAStatus vaStatus = VA_STATUS_SUCCESS;
391
392    if (NULL == obj_context) {
393        vaStatus = VA_STATUS_ERROR_INVALID_CONTEXT;
394        DEBUG_FAILURE;
395        return vaStatus;
396    }
397
398    if (NULL == obj_config) {
399        vaStatus = VA_STATUS_ERROR_INVALID_CONFIG;
400        DEBUG_FAILURE;
401        return vaStatus;
402    }
403
404    /* MSVDX decode capability for MPEG4:
405     *     SP@L3
406     *     ASP@L5
407     *
408     * Refer to the "MSVDX MPEG4 decode capability" table of "Poulsbo Media Software Overview".
409     */
410    switch (obj_config->profile) {
411    case VAProfileMPEG4Simple:
412    case VAProfileMPEG4AdvancedSimple:
413        if ((obj_context->picture_width <= 0) || (obj_context->picture_height <= 0)) {
414            vaStatus = VA_STATUS_ERROR_RESOLUTION_NOT_SUPPORTED;
415        }
416        break;
417
418    default:
419        vaStatus = VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
420        break;
421    }
422
423    return vaStatus;
424}
425
426static void pnw_MPEG4_DestroyContext(object_context_p obj_context);
427
428static VAStatus pnw_MPEG4_CreateContext(
429    object_context_p obj_context,
430    object_config_p obj_config)
431{
432    VAStatus vaStatus = VA_STATUS_SUCCESS;
433    context_MPEG4_p ctx;
434    /* Validate flag */
435    /* Validate picture dimensions */
436    vaStatus = psb__MPEG4_check_legal_picture(obj_context, obj_config);
437    if (VA_STATUS_SUCCESS != vaStatus) {
438        DEBUG_FAILURE;
439        return vaStatus;
440    }
441
442    ctx = (context_MPEG4_p) calloc(1, sizeof(struct context_MPEG4_s));
443    if (NULL == ctx) {
444        vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
445        DEBUG_FAILURE;
446        return vaStatus;
447    }
448    obj_context->format_data = (void*) ctx;
449    ctx->obj_context = obj_context;
450    ctx->pic_params = NULL;
451    ctx->load_non_intra_quant_mat = FALSE;
452    ctx->load_intra_quant_mat = FALSE;
453
454    ctx->split_buffer_pending = FALSE;
455
456    ctx->slice_param_list_size = 8;
457    ctx->slice_param_list = (object_buffer_p*) calloc(1, sizeof(object_buffer_p) * ctx->slice_param_list_size);
458    ctx->slice_param_list_idx = 0;
459
460    if (NULL == ctx->slice_param_list) {
461        vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
462        DEBUG_FAILURE;
463        free(ctx);
464        return vaStatus;
465    }
466
467    ctx->colocated_buffers_size = obj_context->num_render_targets;
468    ctx->colocated_buffers_idx = 0;
469    ctx->colocated_buffers = (psb_buffer_p) calloc(1, sizeof(struct psb_buffer_s) * ctx->colocated_buffers_size);
470    if (NULL == ctx->colocated_buffers) {
471        vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
472        DEBUG_FAILURE;
473        free(ctx->slice_param_list);
474        free(ctx);
475        return vaStatus;
476    }
477
478    switch (obj_config->profile) {
479    case VAProfileMPEG4Simple:
480        psb__information_message("MPEG4_PROFILE_SIMPLE\n");
481        ctx->profile = MPEG4_PROFILE_SIMPLE;
482        break;
483
484    case VAProfileMPEG4AdvancedSimple:
485        psb__information_message("MPEG4_PROFILE_ASP\n");
486        ctx->profile = MPEG4_PROFILE_ASP;
487        break;
488
489    default:
490        ASSERT(0 == 1);
491        vaStatus = VA_STATUS_ERROR_UNKNOWN;
492    }
493
494    // TODO
495
496    if (vaStatus == VA_STATUS_SUCCESS) {
497        vaStatus = psb_buffer_create(obj_context->driver_data,
498                                     FE_STATE_BUFFER_SIZE,
499                                     psb_bt_vpu_only,
500                                     &ctx->FE_state_buffer);
501        DEBUG_FAILURE;
502    }
503
504    if (vaStatus == VA_STATUS_SUCCESS) {
505        vaStatus = psb_buffer_create(obj_context->driver_data,
506                                     sizeof(gaui16mpeg4VlcTableDataPacked),
507                                     psb_bt_cpu_vpu,
508                                     &ctx->vlc_packed_table);
509        DEBUG_FAILURE;
510    }
511    if (vaStatus == VA_STATUS_SUCCESS) {
512        unsigned char *vlc_packed_data_address;
513        if (0 ==  psb_buffer_map(&ctx->vlc_packed_table, &vlc_packed_data_address)) {
514            memcpy(vlc_packed_data_address, gaui16mpeg4VlcTableDataPacked, sizeof(gaui16mpeg4VlcTableDataPacked));
515            psb_buffer_unmap(&ctx->vlc_packed_table);
516        } else {
517            vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
518            DEBUG_FAILURE;
519        }
520    }
521
522    if (vaStatus != VA_STATUS_SUCCESS) {
523        pnw_MPEG4_DestroyContext(obj_context);
524    }
525
526    return vaStatus;
527}
528
529static void pnw_MPEG4_DestroyContext(
530    object_context_p obj_context)
531{
532    INIT_CONTEXT_MPEG4
533    int i;
534
535    psb_buffer_destroy(&ctx->vlc_packed_table);
536    psb_buffer_destroy(&ctx->FE_state_buffer);
537
538    if (ctx->pic_params) {
539        free(ctx->pic_params);
540        ctx->pic_params = NULL;
541    }
542
543    if (ctx->colocated_buffers) {
544        for (i = 0; i < ctx->colocated_buffers_idx; ++i)
545            psb_buffer_destroy(&(ctx->colocated_buffers[i]));
546
547        free(ctx->colocated_buffers);
548        ctx->colocated_buffers = NULL;
549    }
550    if (ctx->slice_param_list) {
551        free(ctx->slice_param_list);
552        ctx->slice_param_list = NULL;
553    }
554
555    free(obj_context->format_data);
556    obj_context->format_data = NULL;
557}
558
559static VAStatus psb__MPEG4_allocate_colocated_buffer(context_MPEG4_p ctx, object_surface_p obj_surface, uint32_t size)
560{
561    psb_surface_p surface = obj_surface->psb_surface;
562
563    if (!GET_SURFACE_INFO_colocated_index(surface)) {
564        VAStatus vaStatus;
565        psb_buffer_p buf;
566        int index = ctx->colocated_buffers_idx;
567        if (index >= ctx->colocated_buffers_size) {
568            return VA_STATUS_ERROR_UNKNOWN;
569        }
570        buf = &(ctx->colocated_buffers[index]);
571        vaStatus = psb_buffer_create(ctx->obj_context->driver_data, size, psb_bt_vpu_only, buf);
572        if (VA_STATUS_SUCCESS != vaStatus) {
573            return vaStatus;
574        }
575        ctx->colocated_buffers_idx++;
576        SET_SURFACE_INFO_colocated_index(surface, index + 1); /* 0 means unset, index is offset by 1 */
577    }
578    return VA_STATUS_SUCCESS;
579}
580
581static psb_buffer_p psb__MPEG4_lookup_colocated_buffer(context_MPEG4_p ctx, psb_surface_p surface)
582{
583    int index = GET_SURFACE_INFO_colocated_index(surface);
584    if (!index) {
585        return NULL;
586    }
587    return &(ctx->colocated_buffers[index-1]); /* 0 means unset, index is offset by 1 */
588}
589
590static VAStatus psb__MPEG4_process_picture_param(context_MPEG4_p ctx, object_buffer_p obj_buffer)
591{
592    VAStatus vaStatus;
593    ASSERT(obj_buffer->type == VAPictureParameterBufferType);
594    ASSERT(obj_buffer->num_elements == 1);
595    ASSERT(obj_buffer->size == sizeof(VAPictureParameterBufferMPEG4));
596
597    if ((obj_buffer->num_elements != 1) ||
598        (obj_buffer->size != sizeof(VAPictureParameterBufferMPEG4))) {
599        return VA_STATUS_ERROR_UNKNOWN;
600    }
601
602    /* Transfer ownership of VAPictureParameterBufferMPEG4 data */
603    if (ctx->pic_params) {
604        free(ctx->pic_params);
605    }
606    ctx->pic_params = (VAPictureParameterBufferMPEG4 *) obj_buffer->buffer_data;
607    obj_buffer->buffer_data = NULL;
608    obj_buffer->size = 0;
609
610
611    /* Lookup surfaces for backward/forward references */
612    /* Lookup surfaces for backward/forward references */
613    switch (ctx->pic_params->vop_fields.bits.vop_coding_type) {
614    case PICTURE_CODING_I:
615        ctx->forward_ref_surface = NULL;
616        ctx->backward_ref_surface = NULL;
617        psb__information_message("PICTURE_CODING_I\nTarget surface = %08x (%08x)\n", ctx->obj_context->current_render_target->psb_surface, ctx->obj_context->current_render_target->base.id);
618        psb__information_message("Forward ref  = NULL\n");
619        psb__information_message("Backward ref = NULL\n");
620        break;
621
622    case PICTURE_CODING_P:
623        ctx->forward_ref_surface = SURFACE(ctx->pic_params->forward_reference_picture);
624        ctx->backward_ref_surface = NULL;
625
626        if (ctx->pic_params->forward_reference_picture == VA_INVALID_SURFACE)
627            ctx->forward_ref_surface = NULL;
628        if (NULL == ctx->forward_ref_surface && ctx->pic_params->forward_reference_picture != VA_INVALID_SURFACE) {
629            return VA_STATUS_ERROR_INVALID_SURFACE;
630        }
631        psb__information_message("PICTURE_CODING_P\nTarget surface = %08x (%08x)\n", ctx->obj_context->current_render_target->psb_surface, ctx->obj_context->current_render_target->base.id);
632        psb__information_message("Forward ref  = %08x (%08x)\n", (ctx->forward_ref_surface ? ctx->forward_ref_surface->psb_surface : 0), ctx->pic_params->forward_reference_picture);
633        psb__information_message("Backward ref = NULL\n");
634        break;
635
636    case PICTURE_CODING_B:
637        ctx->forward_ref_surface = SURFACE(ctx->pic_params->forward_reference_picture);
638        ctx->backward_ref_surface = SURFACE(ctx->pic_params->backward_reference_picture);
639        if ((NULL == ctx->forward_ref_surface) ||
640            (NULL == ctx->backward_ref_surface)) {
641            return VA_STATUS_ERROR_INVALID_SURFACE;
642        }
643        psb__information_message("PICTURE_CODING_B\nTarget surface = %08x (%08x)\n", ctx->obj_context->current_render_target->psb_surface, ctx->obj_context->current_render_target->base.id);
644        psb__information_message("Forward ref  = %08x (%08x)\n", ctx->forward_ref_surface->psb_surface, ctx->pic_params->forward_reference_picture);
645        psb__information_message("Backward ref = %08x (%08x)\n", ctx->backward_ref_surface->psb_surface, ctx->pic_params->backward_reference_picture);
646        break;
647
648    case PICTURE_CODING_S:
649        ctx->forward_ref_surface = SURFACE(ctx->pic_params->forward_reference_picture);
650        ctx->backward_ref_surface = SURFACE(ctx->pic_params->backward_reference_picture);
651        psb__information_message("PICTURE_CODING_S\nTarget surface = %08x (%08x)\n", ctx->obj_context->current_render_target->psb_surface, ctx->obj_context->current_render_target->base.id);
652        psb__information_message("Forward ref  = %08x (%08x)\n", ctx->forward_ref_surface ? ctx->forward_ref_surface->psb_surface : 0, ctx->pic_params->forward_reference_picture);
653        psb__information_message("Backward ref = %08x (%08x)\n", ctx->backward_ref_surface ? ctx->backward_ref_surface->psb_surface : 0, ctx->pic_params->backward_reference_picture);
654        break;
655
656    default:
657        psb__error_message("Unhandled MPEG4 vop_coding_type '%d'\n", ctx->pic_params->vop_fields.bits.vop_coding_type);
658        return VA_STATUS_ERROR_UNKNOWN;
659    }
660
661    if (NULL == ctx->forward_ref_surface) {
662        /* for mmu fault protection */
663        ctx->forward_ref_surface = ctx->obj_context->current_render_target;
664    }
665    if (NULL == ctx->backward_ref_surface) {
666        /* for mmu fault protection */
667        ctx->backward_ref_surface = ctx->obj_context->current_render_target;
668    }
669
670    ctx->display_picture_width = ctx->pic_params->vop_width;
671    ctx->display_picture_height = ctx->pic_params->vop_height;
672    ctx->picture_width_mb = PIXELS_TO_MB(ctx->display_picture_width);
673    ctx->picture_height_mb = PIXELS_TO_MB(ctx->display_picture_height);
674    ctx->coded_picture_width = ctx->picture_width_mb * 16;
675    ctx->coded_picture_height = ctx->picture_height_mb * 16;
676    ctx->size_mb = ctx->picture_width_mb * ctx->picture_height_mb;
677
678    uint32_t mbInPic = ctx->picture_width_mb * ctx->picture_height_mb;
679
680    mbInPic += 4;
681
682    uint32_t colocated_size = ((mbInPic * 200) + 0xfff) & ~0xfff;
683
684    vaStatus = psb__MPEG4_allocate_colocated_buffer(ctx, ctx->obj_context->current_render_target, colocated_size);
685    if (VA_STATUS_SUCCESS != vaStatus) {
686        DEBUG_FAILURE;
687        return vaStatus;
688    }
689    vaStatus = psb__MPEG4_allocate_colocated_buffer(ctx, ctx->forward_ref_surface, colocated_size);
690    if (VA_STATUS_SUCCESS != vaStatus) {
691        DEBUG_FAILURE;
692        return vaStatus;
693    }
694
695    ctx->FEControl = 0;
696    REGIO_WRITE_FIELD_LITE(ctx->FEControl ,
697                           MSVDX_VEC,
698                           CR_VEC_ENTDEC_FE_CONTROL,
699                           ENTDEC_FE_PROFILE,
700                           ctx->profile);    /* MPEG4 SP / ASP profile    */
701
702    REGIO_WRITE_FIELD_LITE(ctx->FEControl ,
703                           MSVDX_VEC,
704                           CR_VEC_ENTDEC_FE_CONTROL,
705                           ENTDEC_FE_MODE,
706                           4);                                /* Set MPEG4 mode            */
707
708    /* FE_SPS0                                                                        */
709    ctx->FE_SPS0 = 0;
710    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);
711    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);
712    REGIO_WRITE_FIELD_LITE(ctx->FE_SPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_SPS0, FE_PROFILE,                    ctx->profile);
713
714    /* FE_VOP_SPS0                                                                    */
715    ctx->FE_VOP_SPS0 = 0;
716    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);
717    REGIO_WRITE_FIELD_LITE(ctx->FE_VOP_SPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_VOP_SPS0, QUANT_PRECISION,        ctx->pic_params->quant_precision);
718    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);
719    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));
720    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);
721    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);
722    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);
723
724    if (ctx->pic_params->vol_fields.bits.short_video_header) {
725        /* FE_PICSH_PPS0                                                            */
726        ctx->FE_PICSH_PPS0 = 0;
727        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);
728        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);
729        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);
730    }
731
732    /* FE_VOP_PPS0 */
733    ctx->FE_VOP_PPS0 = 0;
734    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);
735    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);
736    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);
737    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);
738    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);
739
740    /* BE_SPS0                                                                        */
741    /* Common for VOPs and pictures with short header                                */
742    ctx->BE_SPS0 = 0;
743    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);
744    REGIO_WRITE_FIELD_LITE(ctx->BE_SPS0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_SPS0, BE_PROFILE,            ctx->profile);
745
746    /* BE_SPS1                                                                        */
747    /* Common for VOPs and pictures with short header                                */
748    ctx->BE_SPS1 = 0;
749    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);
750    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);
751
752    if (0 == ctx->pic_params->vol_fields.bits.short_video_header) {
753        /* BE_VOP_SPS0                                                                */
754        ctx->BE_VOP_SPS0 = 0;
755        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);
756        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);
757        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);
758
759        /* BE_VOP_SPS1                                                                */
760        ctx->BE_VOP_SPS1 = 0;
761        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);
762        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);
763        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));
764        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);
765        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);
766
767        /* BE_VOP_PPS0                                                                */
768        ctx->BE_VOP_PPS0 = 0;
769        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);
770        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);
771        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);
772        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);
773        REGIO_WRITE_FIELD_LITE(ctx->BE_VOP_PPS0,
774                               MSVDX_VEC_MPEG4,
775                               CR_VEC_MPEG4_BE_VOP_PPS0,
776                               ROUNDING_TYPE,
777                               ((PICTURE_CODING_I == ctx->pic_params->vop_fields.bits.vop_coding_type ||
778                                 PICTURE_CODING_B == ctx->pic_params->vop_fields.bits.vop_coding_type) ?
779                                0 : ctx->pic_params->vop_fields.bits.vop_rounding_type));
780        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);
781    } else {
782        /* BE_VOP_PPS0                                                                */
783        ctx->BE_VOP_PPS0 = 0;
784        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
785        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);
786        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);
787        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
788        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);
789
790        /* BE_PICSH_PPS0                                                            */
791        ctx->BE_PICSH_PPS0 = 0;
792        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);
793    }
794
795    return VA_STATUS_SUCCESS;
796}
797
798static void psb__MPEG4_convert_iq_matrix(uint32_t *dest32, unsigned char *src)
799{
800    int i;
801    int *idx = scan0;
802    uint8_t *dest8 = (uint8_t*) dest32;
803
804    for (i = 0; i < 64; i++) {
805        *dest8++ = src[*idx++];
806    }
807}
808
809static VAStatus psb__MPEG4_process_iq_matrix(context_MPEG4_p ctx, object_buffer_p obj_buffer)
810{
811    VAIQMatrixBufferMPEG4 *iq_matrix = (VAIQMatrixBufferMPEG4 *) obj_buffer->buffer_data;
812    ASSERT(obj_buffer->type == VAIQMatrixBufferType);
813    ASSERT(obj_buffer->num_elements == 1);
814    ASSERT(obj_buffer->size == sizeof(VAIQMatrixBufferMPEG4));
815
816    if ((obj_buffer->num_elements != 1) ||
817        (obj_buffer->size != sizeof(VAIQMatrixBufferMPEG4))) {
818        return VA_STATUS_ERROR_UNKNOWN;
819    }
820
821    if (iq_matrix->load_non_intra_quant_mat) {
822        psb__MPEG4_convert_iq_matrix(ctx->qmatrix_data[NONINTRA_LUMA_Q], iq_matrix->non_intra_quant_mat);
823    }
824    if (iq_matrix->load_intra_quant_mat) {
825        psb__MPEG4_convert_iq_matrix(ctx->qmatrix_data[INTRA_LUMA_Q], iq_matrix->intra_quant_mat);
826    }
827    ctx->load_non_intra_quant_mat = iq_matrix->load_non_intra_quant_mat;
828    ctx->load_intra_quant_mat = iq_matrix->load_intra_quant_mat;
829
830    return VA_STATUS_SUCCESS;
831}
832
833static void psb__MPEG4_write_qmatrices(context_MPEG4_p ctx)
834{
835    psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
836    int i;
837
838    // TODO: Verify that this is indeed the same as MPEG2
839
840    /* Since we only decode 4:2:0 We only need to the Intra tables.
841    Chroma quant tables are only used in Mpeg 4:2:2 and 4:4:4.
842    The hardware wants non-intra followed by intra */
843    /* psb_cmdbuf_rendec_start_block( cmdbuf ); */
844    psb_cmdbuf_rendec_start(cmdbuf, REG_MSVDX_VEC_IQRAM_OFFSET);
845
846    /* todo : optimisation here is to only load the need table */
847    if (ctx->load_non_intra_quant_mat) {
848        /*  NONINTRA_LUMA_Q --> REG_MSVDX_VEC_IQRAM_OFFSET + 0 */
849        for (i = 0; i < 16; i++) {
850            psb_cmdbuf_rendec_write(cmdbuf, ctx->qmatrix_data[NONINTRA_LUMA_Q][i]);
851        }
852    } else {
853        for (i = 0; i < 16; i++) {
854            psb_cmdbuf_rendec_write(cmdbuf, 0);
855        }
856    }
857    if (ctx->load_intra_quant_mat) {
858        /*  INTRA_LUMA_Q --> REG_MSVDX_VEC_IQRAM_OFFSET + (16*4) */
859        for (i = 0; i < 16; i++) {
860            psb_cmdbuf_rendec_write(cmdbuf, ctx->qmatrix_data[INTRA_LUMA_Q][i]);
861        }
862    } else {
863        for (i = 0; i < 16; i++) {
864            psb_cmdbuf_rendec_write(cmdbuf, 0);
865        }
866    }
867
868    psb_cmdbuf_rendec_end(cmdbuf);
869    /* psb_cmdbuf_rendec_end_block( cmdbuf ); */
870}
871
872
873/*
874 * Adds a VASliceParameterBuffer to the list of slice params
875 */
876static VAStatus psb__MPEG4_add_slice_param(context_MPEG4_p ctx, object_buffer_p obj_buffer)
877{
878    ASSERT(obj_buffer->type == VASliceParameterBufferType);
879    if (ctx->slice_param_list_idx >= ctx->slice_param_list_size) {
880        unsigned char *new_list;
881        ctx->slice_param_list_size += 8;
882        new_list = realloc(ctx->slice_param_list,
883                           sizeof(object_buffer_p) * ctx->slice_param_list_size);
884        if (NULL == new_list) {
885            return VA_STATUS_ERROR_ALLOCATION_FAILED;
886        }
887        ctx->slice_param_list = (object_buffer_p*) new_list;
888    }
889    ctx->slice_param_list[ctx->slice_param_list_idx] = obj_buffer;
890    ctx->slice_param_list_idx++;
891    return VA_STATUS_SUCCESS;
892}
893
894/* Precalculated values */
895#define ADDR0        (0x00005800)
896#define ADDR1        (0x0001f828)
897#define ADDR2        (0x0002b854)
898#define ADDR3        (0x0002f85c)
899#define ADDR4        (0x0004d089)
900#define ADDR5        (0x0008f0aa)
901#define ADDR6        (0x00149988)
902#define ADDR7        (0x001d8b9e)
903#define ADDR8        (0x000003c3)
904#define WIDTH0        (0x09a596ed)
905#define WIDTH1        (0x0006d6db)
906#define OPCODE0        (0x50009a0a)
907#define OPCODE1        (0x00000001)
908
909static void psb__MPEG4_write_VLC_tables(context_MPEG4_p ctx)
910{
911    psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
912
913    psb_cmdbuf_skip_start_block(cmdbuf, SKIP_ON_CONTEXT_SWITCH);
914    /* VLC Table */
915    /* Write a LLDMA Cmd to transfer VLD Table data */
916#ifndef DE3_FIRMWARE
917    psb_cmdbuf_lldma_write_cmdbuf(cmdbuf, &ctx->vlc_packed_table, 0,
918                                  sizeof(gaui16mpeg4VlcTableDataPacked),
919                                  0, LLDMA_TYPE_VLC_TABLE);
920#else
921    psb_cmdbuf_dma_write_cmdbuf(cmdbuf, &ctx->vlc_packed_table, 0,
922                                  sizeof(gaui16mpeg4VlcTableDataPacked), 0,
923                                  DMA_TYPE_VLC_TABLE);
924#endif
925
926    /* Write the vec registers with the index data for each of the tables and then write    */
927    /* the actual table data.                                                                */
928    psb_cmdbuf_reg_start_block(cmdbuf, 0);
929    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_ADDR0),            ADDR0);
930    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_ADDR1),            ADDR1);
931    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_ADDR2),            ADDR2);
932    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_ADDR3),            ADDR3);
933    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_ADDR4),            ADDR4);
934    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_ADDR5),            ADDR5);
935    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_ADDR6),            ADDR6);
936    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_ADDR7),            ADDR7);
937    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_ADDR8),            ADDR8);
938    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_INITIAL_WIDTH0),    WIDTH0);
939    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_INITIAL_WIDTH1),    WIDTH1);
940    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_INITIAL_OPCODE0),    OPCODE0);
941    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_VLC_TABLE_INITIAL_OPCODE1),    OPCODE1);
942    psb_cmdbuf_reg_end_block(cmdbuf);
943
944    psb_cmdbuf_skip_end_block(cmdbuf);
945}
946
947
948static void psb__MPEG4_write_kick(context_MPEG4_p ctx, VASliceParameterBufferMPEG4 *slice_param)
949{
950    psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
951
952    (void) slice_param; /* Unused for now */
953
954    *cmdbuf->cmd_idx++ = CMD_COMPLETION;
955}
956
957/* Programme the Alt output if there is a rotation*/
958static void psb__MPEG4_setup_alternative_frame(context_MPEG4_p ctx)
959{
960    uint32_t cmd;
961    psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
962    psb_surface_p rotate_surface = ctx->obj_context->current_render_target->psb_surface_rotate;
963    object_context_p obj_context = ctx->obj_context;
964
965    if (rotate_surface == NULL) {
966        psb__information_message("rotate surface is NULL, abort msvdx rotation\n");
967        return;
968    }
969
970    if (GET_SURFACE_INFO_rotate(rotate_surface) != obj_context->msvdx_rotate)
971        psb__error_message("Display rotate mode does not match surface rotate mode!\n");
972
973
974    /* CRendecBlock    RendecBlk( mCtrlAlloc , RENDEC_REGISTER_OFFSET(MSVDX_CMDS, VC1_LUMA_RANGE_MAPPING_BASE_ADDRESS) ); */
975    psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_CMDS, VC1_LUMA_RANGE_MAPPING_BASE_ADDRESS));
976
977    psb_cmdbuf_rendec_write_address(cmdbuf, &rotate_surface->buf, rotate_surface->buf.buffer_ofs);
978    psb_cmdbuf_rendec_write_address(cmdbuf, &rotate_surface->buf, rotate_surface->buf.buffer_ofs + rotate_surface->chroma_offset);
979
980    psb_cmdbuf_rendec_end(cmdbuf);
981
982    /* Set the rotation registers */
983    psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_CMDS, ALTERNATIVE_OUTPUT_PICTURE_ROTATION));
984    cmd = 0;
985    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, ALTERNATIVE_OUTPUT_PICTURE_ROTATION , ALT_PICTURE_ENABLE, 1);
986    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, ALTERNATIVE_OUTPUT_PICTURE_ROTATION , ROTATION_ROW_STRIDE, rotate_surface->stride_mode);
987    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, ALTERNATIVE_OUTPUT_PICTURE_ROTATION , RECON_WRITE_DISABLE, 0); /* FIXME Always generate Rec */
988    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, ALTERNATIVE_OUTPUT_PICTURE_ROTATION , ROTATION_MODE, GET_SURFACE_INFO_rotate(rotate_surface));
989
990    psb_cmdbuf_rendec_write(cmdbuf, cmd);
991
992    psb_cmdbuf_rendec_end(cmdbuf);
993
994    *ctx->alt_output_flags = cmd;
995    RELOC(*ctx->p_range_mapping_base0, rotate_surface->buf.buffer_ofs, &rotate_surface->buf);
996    RELOC(*ctx->p_range_mapping_base1, rotate_surface->buf.buffer_ofs + rotate_surface->chroma_offset, &rotate_surface->buf);
997}
998
999
1000static void psb__MPEG4_set_picture_params(context_MPEG4_p ctx, VASliceParameterBufferMPEG4 *slice_param)
1001{
1002    uint32_t cmd;
1003    psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
1004    psb_surface_p target_surface = ctx->obj_context->current_render_target->psb_surface;
1005
1006    psb_buffer_p colocated_target_buffer = psb__MPEG4_lookup_colocated_buffer(ctx, target_surface);
1007    psb_buffer_p colocated_ref_buffer = psb__MPEG4_lookup_colocated_buffer(ctx, ctx->forward_ref_surface->psb_surface); /* FIXME DE2.0 use backward ref surface */
1008    ASSERT(colocated_target_buffer);
1009    ASSERT(colocated_ref_buffer);
1010
1011    /* psb_cmdbuf_rendec_start_block( cmdbuf ); */
1012
1013    /* BE_PARAM_BASE_ADDR                                                            */
1014    psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_VEC, MPEG4_CR_VEC_MPEG4_BE_PARAM_BASE_ADDR));
1015    if (colocated_target_buffer) {
1016        psb_cmdbuf_rendec_write_address(cmdbuf, colocated_target_buffer, 0);
1017    } else {
1018        /* This is an error */
1019        psb_cmdbuf_rendec_write(cmdbuf, 0);
1020    }
1021    psb_cmdbuf_rendec_end(cmdbuf);
1022
1023    /* PARAM_BASE_ADDRESS                                                            */
1024    psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_VEC, MPEG4_CR_VEC_MPEG4_BE_COLPARAM_BASE_ADDR));
1025    if (colocated_ref_buffer) {
1026        psb_cmdbuf_rendec_write_address(cmdbuf, colocated_ref_buffer, 0);
1027    } else {
1028        /* This is an error */
1029        psb_cmdbuf_rendec_write(cmdbuf, 0);
1030    }
1031    psb_cmdbuf_rendec_end(cmdbuf);
1032
1033    if (CONTEXT_ROTATE(ctx->obj_context))
1034        psb__MPEG4_setup_alternative_frame(ctx);
1035
1036    /* Send VDMC and VDEB commands                                                    */
1037    psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_CMDS, DISPLAY_PICTURE_SIZE));
1038
1039    /* Display picture size cmd                                                        */
1040    cmd = 0;
1041    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, DISPLAY_PICTURE_SIZE, DISPLAY_PICTURE_HEIGHT, ctx->coded_picture_height - 1);
1042    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, DISPLAY_PICTURE_SIZE, DISPLAY_PICTURE_WIDTH,  ctx->coded_picture_width - 1);
1043    psb_cmdbuf_rendec_write(cmdbuf, cmd);
1044
1045    /* Coded picture size cmd                                                        */
1046    cmd = 0;
1047    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, CODED_PICTURE_SIZE, CODED_PICTURE_HEIGHT, ctx->coded_picture_height - 1);
1048    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, CODED_PICTURE_SIZE, CODED_PICTURE_WIDTH,  ctx->coded_picture_width - 1);
1049    psb_cmdbuf_rendec_write(cmdbuf, cmd);
1050
1051    /* Operating mode cmd                                                            */
1052    cmd = 0;
1053    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, OPERATING_MODE, CHROMA_INTERLEAVED, 0);                                 /* 0 = CbCr, 1 = CrCb        */
1054    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, OPERATING_MODE, ROW_STRIDE,         ctx->obj_context->current_render_target->psb_surface->stride_mode);
1055    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, OPERATING_MODE, CODEC_PROFILE,      ctx->profile); /* MPEG4 SP / ASP profile    */
1056    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, OPERATING_MODE, CODEC_MODE,         4);                                 /* MPEG4                    */
1057    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, OPERATING_MODE, ASYNC_MODE,         1);                                 /* VDMC only                */
1058    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, OPERATING_MODE, CHROMA_FORMAT,      1);
1059    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, OPERATING_MODE, INTERLACED,         ctx->pic_params->vol_fields.bits.interlaced);
1060    psb_cmdbuf_rendec_write(cmdbuf, cmd);
1061    ctx->obj_context->operating_mode = cmd;
1062
1063    psb__information_message("    vop_coding_type = %s\n", psb__debug_picture_coding_str(ctx->pic_params->vop_fields.bits.vop_coding_type));
1064    psb__information_message("    backward ref vop_coding_type = %s\n", psb__debug_picture_coding_str(ctx->pic_params->vop_fields.bits.backward_reference_vop_coding_type));
1065
1066    /* LUMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES                                    */
1067    psb_cmdbuf_rendec_write_address(cmdbuf, &target_surface->buf, target_surface->buf.buffer_ofs);
1068    /* CHROMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES                                    */
1069    psb_cmdbuf_rendec_write_address(cmdbuf, &target_surface->buf, target_surface->buf.buffer_ofs + target_surface->chroma_offset);
1070
1071    psb_cmdbuf_rendec_end(cmdbuf);
1072
1073    /* Reference pictures base addresses                                            */
1074    psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_CMDS, REFERENCE_PICTURE_BASE_ADDRESSES));
1075
1076//psb__information_message("Target surface = %08x\n", target_surface);
1077//psb__information_message("Forward ref = %08x\n", ctx->forward_ref_surface->psb_surface);
1078//psb__information_message("Backward ref = %08x\n", ctx->backward_ref_surface->psb_surface);
1079
1080    /* forward reference picture */
1081    /* LUMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES                                    */
1082    psb_cmdbuf_rendec_write_address(cmdbuf, &ctx->forward_ref_surface->psb_surface->buf, ctx->forward_ref_surface->psb_surface->buf.buffer_ofs);
1083    /* CHROMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES                                    */
1084    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);
1085
1086    /* backward reference picture */
1087    /* LUMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES                                    */
1088    psb_cmdbuf_rendec_write_address(cmdbuf, &ctx->backward_ref_surface->psb_surface->buf, ctx->backward_ref_surface->psb_surface->buf.buffer_ofs);
1089    /* CHROMA_RECONSTRUCTED_PICTURE_BASE_ADDRESSES                                    */
1090    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);
1091
1092    psb_cmdbuf_rendec_end(cmdbuf);
1093    /* psb_cmdbuf_rendec_end_block( cmdbuf ); */
1094}
1095
1096static void psb__MPEG4_set_backend_registers(context_MPEG4_p ctx, VASliceParameterBufferMPEG4 *slice_param)
1097{
1098    psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
1099    uint32_t cmd;
1100    unsigned short width_mb = PIXELS_TO_MB(ctx->pic_params->vop_width);
1101
1102    /* psb_cmdbuf_rendec_start_block( cmdbuf ); */
1103
1104    /* Write Back-End EntDec registers                                                */
1105    psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_VEC, MPEG4_CR_VEC_MPEG4_BE_SPS0));
1106    /* BE_SPS0                                                                        */
1107    /* Common for VOPs and pictures with short header                                */
1108    psb_cmdbuf_rendec_write(cmdbuf, ctx->BE_SPS0);
1109    /* BE_SPS1                                                                        */
1110    /* Common for VOPs and pictures with short header                                */
1111    psb_cmdbuf_rendec_write(cmdbuf, ctx->BE_SPS1);
1112    psb_cmdbuf_rendec_end(cmdbuf);
1113
1114    psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_VEC, MPEG4_CR_VEC_MPEG4_BE_VOP_SPS0));
1115    if (0 == ctx->pic_params->vol_fields.bits.short_video_header) {
1116        /* BE_VOP_SPS0                                                                */
1117        psb_cmdbuf_rendec_write(cmdbuf, ctx->BE_VOP_SPS0);
1118        /* BE_VOP_SPS1                                                                */
1119        psb_cmdbuf_rendec_write(cmdbuf, ctx->BE_VOP_SPS1);
1120        /* BE_VOP_PPS0                                                                */
1121        psb_cmdbuf_rendec_write(cmdbuf, ctx->BE_VOP_PPS0);
1122    } else { /* Short-header mode */
1123        /* BE_VOP_SPS0 */
1124        psb_cmdbuf_rendec_write(cmdbuf, 0);
1125        /* BE_VOP_SPS1 */
1126        psb_cmdbuf_rendec_write(cmdbuf, 0);
1127        /* BE_VOP_PPS0                                                                */
1128        psb_cmdbuf_rendec_write(cmdbuf, ctx->BE_VOP_PPS0);
1129        /* BE_PICSH_PPS0                                                            */
1130        psb_cmdbuf_rendec_write(cmdbuf, ctx->BE_PICSH_PPS0);
1131    }
1132    psb_cmdbuf_rendec_end(cmdbuf);
1133
1134    if (0 == ctx->pic_params->vol_fields.bits.short_video_header) {
1135        if ((GMC == ctx->pic_params->vol_fields.bits.sprite_enable) &&
1136            (PICTURE_CODING_S == ctx->pic_params->vop_fields.bits.vop_coding_type)) {
1137            psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_VEC, MPEG4_CR_VEC_MPEG4_BE_GMC_X));
1138
1139            /* TODO: GMC Motion Vectors */
1140            /* It is still needed to specify the precision of the motion vectors (should they be in        */
1141            /* half-sample, quarter-sample...?) and how much processing    is done on the firmware on        */
1142            /* the values of the warping points.                                                        */
1143
1144            // TODO: Which index to use?
1145            int sprite_index = (ctx->pic_params->sprite_trajectory_du[0] || ctx->pic_params->sprite_trajectory_dv[0]) ? 0 : 1;
1146
1147            /* BE_GMC_X                                                                */
1148            cmd = 0;
1149            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);
1150            psb_cmdbuf_rendec_write(cmdbuf, cmd);
1151
1152            /* BE_GMC_Y                                                                */
1153            cmd = 0;
1154            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);
1155            psb_cmdbuf_rendec_write(cmdbuf, cmd);
1156
1157            psb_cmdbuf_rendec_end(cmdbuf);
1158        }
1159    }
1160
1161    psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_VEC, MPEG4_CR_VEC_MPEG4_BE_SLICE0));
1162
1163    /* BE_SLICE0                                                                    */
1164    cmd = 0;
1165    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);
1166    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);
1167    psb_cmdbuf_rendec_write(cmdbuf, cmd);
1168
1169    /* CR_VEC_MPEG4_BE_VOP_TR                                                        */
1170    cmd = 0;
1171    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_VOP_TRB, BE_TRB, ctx->pic_params->TRB);
1172    psb_cmdbuf_rendec_write(cmdbuf, cmd);
1173
1174    cmd = 0;
1175    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_BE_VOP_TRD, BE_TRD, ctx->pic_params->TRD);
1176    psb_cmdbuf_rendec_write(cmdbuf, cmd);
1177
1178    psb_cmdbuf_rendec_end(cmdbuf);
1179
1180
1181    /* Send Slice Data for every slice */
1182    /* MUST be the last slice sent */
1183    psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_CMDS, SLICE_PARAMS));
1184
1185    /* Slice params command                                                            */
1186    cmd = 0;
1187    REGIO_WRITE_FIELD_LITE(cmd,
1188                           MSVDX_CMDS,
1189                           SLICE_PARAMS,
1190                           RND_CTL_BIT,
1191                           ((PICTURE_CODING_I == ctx->pic_params->vop_fields.bits.vop_coding_type ||
1192                             PICTURE_CODING_B == ctx->pic_params->vop_fields.bits.vop_coding_type) ?
1193                            0 : ctx->pic_params->vop_fields.bits.vop_rounding_type));
1194    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, SLICE_PARAMS, MODE_CONFIG,            ctx->pic_params->vol_fields.bits.sprite_warping_accuracy);
1195    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, SLICE_PARAMS, SUBPEL_FILTER_MODE,    ctx->pic_params->vol_fields.bits.quarter_sample);
1196    /* SP and ASP profiles don't support field coding in different VOPs */
1197    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, SLICE_PARAMS, SLICE_FIELD_TYPE,   2);
1198    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_CMDS, SLICE_PARAMS, SLICE_CODE_TYPE,    ctx->pic_params->vop_fields.bits.vop_coding_type);
1199    psb_cmdbuf_rendec_write(cmdbuf, cmd);
1200
1201    psb_cmdbuf_rendec_end(cmdbuf);
1202
1203    *ctx->p_slice_params = cmd;
1204
1205    /* CHUNK: Entdec back-end profile and level                                        */
1206    psb_cmdbuf_rendec_start(cmdbuf, RENDEC_REGISTER_OFFSET(MSVDX_VEC, CR_VEC_ENTDEC_BE_CONTROL));
1207
1208    cmd = 0;
1209    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_VEC, CR_VEC_ENTDEC_BE_CONTROL, ENTDEC_BE_PROFILE, ctx->profile);     /* MPEG4 SP / ASP profile*/
1210    REGIO_WRITE_FIELD_LITE(cmd, MSVDX_VEC, CR_VEC_ENTDEC_BE_CONTROL, ENTDEC_BE_MODE,    4);             /* 4 - MPEG4             */
1211    psb_cmdbuf_rendec_write(cmdbuf, cmd);
1212
1213    psb_cmdbuf_rendec_end(cmdbuf);
1214    /* psb_cmdbuf_rendec_end_block( cmdbuf ); */
1215
1216    /* Send IQ matrices to Rendec */
1217    psb__MPEG4_write_qmatrices(ctx);
1218}
1219
1220static void psb__MPEG4_set_frontend_registers(context_MPEG4_p ctx, VASliceParameterBufferMPEG4 *slice_param)
1221{
1222    psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
1223    uint32_t FE_slice0;
1224    unsigned short width_mb = PIXELS_TO_MB(ctx->pic_params->vop_width);
1225
1226    psb_cmdbuf_reg_start_block(cmdbuf, 0);
1227
1228    /* FE_SLICE0                                                                    */
1229    FE_slice0 = 0;
1230    REGIO_WRITE_FIELD_LITE(FE_slice0, MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_SLICE0, FE_VOP_QUANT,            slice_param->quant_scale);
1231    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);
1232    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);
1233
1234    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_SLICE0) , FE_slice0);
1235
1236    /* Entdec Front-End controls*/
1237    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC, CR_VEC_ENTDEC_FE_CONTROL) , ctx->FEControl);
1238
1239    /* FE_SPS0                                                                        */
1240    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_SPS0) , ctx->FE_SPS0);
1241
1242    /* FE_VOP_SPS0                                                                    */
1243    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_VOP_SPS0) , ctx->FE_VOP_SPS0);
1244
1245
1246    if (ctx->pic_params->vol_fields.bits.short_video_header) {
1247        /* FE_PICSH_PPS0                                                            */
1248        psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_PICSH_PPS0) , ctx->FE_PICSH_PPS0);
1249    }
1250
1251    /* FE_VOP_PPS0 */
1252    psb_cmdbuf_reg_set(cmdbuf, REGISTER_OFFSET(MSVDX_VEC_MPEG4, CR_VEC_MPEG4_FE_VOP_PPS0) , ctx->FE_VOP_PPS0);
1253
1254    psb_cmdbuf_reg_end_block(cmdbuf);
1255}
1256
1257/*
1258static void psb__MPEG4_FE_state(context_MPEG4_p ctx)
1259{
1260    uint32_t lldma_record_offset;
1261    psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
1262
1263    *cmdbuf->cmd_idx++ = CMD_HEADER_VC1;
1264
1265    ctx->p_range_mapping_base0 = cmdbuf->cmd_idx++;
1266    ctx->p_range_mapping_base1 = cmdbuf->cmd_idx++;
1267    ctx->p_slice_params = cmdbuf->cmd_idx;
1268    *cmdbuf->cmd_idx++ = 0;
1269
1270    lldma_record_offset = psb_cmdbuf_lldma_create( cmdbuf, &(ctx->FE_state_buffer), 0,
1271                                FE_STATE_SAVE_SIZE, 0, LLDMA_TYPE_MPEG4_FESTATE_SAVE );
1272    RELOC(*cmdbuf->cmd_idx, lldma_record_offset, &(cmdbuf->buf));
1273    cmdbuf->cmd_idx++;
1274
1275    lldma_record_offset = psb_cmdbuf_lldma_create( cmdbuf, &(ctx->FE_state_buffer), 0,
1276                                FE_STATE_SAVE_SIZE, 0, LLDMA_TYPE_MPEG4_FESTATE_RESTORE );
1277    RELOC(*cmdbuf->cmd_idx, lldma_record_offset, &(cmdbuf->buf));
1278    cmdbuf->cmd_idx++;
1279
1280    ctx->slice_first_pic_last = cmdbuf->cmd_idx++;
1281}
1282*/
1283#ifndef DE3_FIRMWARE
1284static void psb__MPEG4_FE_state(context_MPEG4_p ctx)
1285{
1286    uint32_t lldma_record_offset;
1287    psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
1288
1289    *cmdbuf->cmd_idx++ = CMD_HEADER_VC1;
1290    ctx->p_slice_params = cmdbuf->cmd_idx;
1291    *cmdbuf->cmd_idx++ = 0;
1292
1293
1294    lldma_record_offset = psb_cmdbuf_lldma_create(cmdbuf, &(ctx->FE_state_buffer), 0,
1295                          FE_STATE_SAVE_SIZE, 0, LLDMA_TYPE_MPEG4_FESTATE_SAVE);
1296    RELOC(*cmdbuf->cmd_idx, lldma_record_offset, &(cmdbuf->buf));
1297    cmdbuf->cmd_idx++;
1298
1299    lldma_record_offset = psb_cmdbuf_lldma_create(cmdbuf, &(ctx->FE_state_buffer), 0,
1300                          FE_STATE_SAVE_SIZE, 0, LLDMA_TYPE_MPEG4_FESTATE_RESTORE);
1301    RELOC(*cmdbuf->cmd_idx, lldma_record_offset, &(cmdbuf->buf));
1302    cmdbuf->cmd_idx++;
1303
1304    ctx->slice_first_pic_last = cmdbuf->cmd_idx++;
1305
1306    ctx->p_range_mapping_base0 = cmdbuf->cmd_idx++;
1307    ctx->p_range_mapping_base1 = cmdbuf->cmd_idx++;
1308
1309    ctx->alt_output_flags = cmdbuf->cmd_idx++;
1310    *ctx->alt_output_flags = 0;
1311}
1312#else
1313static void psb__MPEG4_FE_state(context_MPEG4_p ctx)
1314{
1315    psb_cmdbuf_p cmdbuf = ctx->obj_context->cmdbuf;
1316    CTRL_ALLOC_HEADER *cmd_header = (CTRL_ALLOC_HEADER *)psb_cmdbuf_alloc_space(cmdbuf, sizeof(CTRL_ALLOC_HEADER));
1317
1318    memset(cmd_header, 0, sizeof(CTRL_ALLOC_HEADER));
1319    cmd_header->ui32Cmd_AdditionalParams = CMD_CTRL_ALLOC_HEADER;
1320    RELOC(cmd_header->ui32ExternStateBuffAddr, 0, &ctx->FE_state_buffer);
1321    cmd_header->ui32MacroblockParamAddr = 0; /* Only EC needs to set this */
1322
1323    ctx->p_slice_params = &cmd_header->ui32SliceParams;
1324    cmd_header->ui32SliceParams = 0;
1325
1326    ctx->slice_first_pic_last = &cmd_header->uiSliceFirstMbYX_uiPicLastMbYX;
1327
1328    ctx->p_range_mapping_base0 = &cmd_header->ui32AltOutputAddr[0];
1329    ctx->p_range_mapping_base1 = &cmd_header->ui32AltOutputAddr[1];
1330
1331    ctx->alt_output_flags = &cmd_header->ui32AltOutputFlags;
1332    cmd_header->ui32AltOutputFlags = 0;
1333}
1334#endif
1335static VAStatus psb__MPEG4_process_slice(context_MPEG4_p ctx,
1336        VASliceParameterBufferMPEG4 *slice_param,
1337        object_buffer_p obj_buffer)
1338{
1339    VAStatus vaStatus = VA_STATUS_SUCCESS;
1340
1341    ASSERT((obj_buffer->type == VASliceDataBufferType) || (obj_buffer->type == VAProtectedSliceDataBufferType));
1342
1343    psb__information_message("MPEG4 process slice\n");
1344    psb__information_message("    size = %08x offset = %08x\n", slice_param->slice_data_size, slice_param->slice_data_offset);
1345    psb__information_message("    macroblock nr = %d offset = %d\n", slice_param->macroblock_number, slice_param->macroblock_offset);
1346    psb__information_message("    slice_data_flag = %d\n", slice_param->slice_data_flag);
1347    psb__information_message("    interlaced = %d\n", ctx->pic_params->vol_fields.bits.interlaced);
1348    psb__information_message("    coded size = %dx%d\n", ctx->picture_width_mb, ctx->picture_height_mb);
1349
1350    if ((slice_param->slice_data_flag == VA_SLICE_DATA_FLAG_BEGIN) ||
1351        (slice_param->slice_data_flag == VA_SLICE_DATA_FLAG_ALL)) {
1352        if (0 == slice_param->slice_data_size) {
1353            vaStatus = VA_STATUS_ERROR_UNKNOWN;
1354            DEBUG_FAILURE;
1355            return vaStatus;
1356        }
1357        ASSERT(!ctx->split_buffer_pending);
1358
1359        /* Initialise the command buffer */
1360        /* TODO: Reuse current command buffer until full */
1361        psb_context_get_next_cmdbuf(ctx->obj_context);
1362
1363        psb__MPEG4_FE_state(ctx);
1364
1365#ifndef DE3_FIRMWARE
1366        psb_cmdbuf_lldma_write_bitstream(ctx->obj_context->cmdbuf,
1367                                         obj_buffer->psb_buffer,
1368                                         obj_buffer->psb_buffer->buffer_ofs + slice_param->slice_data_offset,
1369                                         slice_param->slice_data_size,
1370                                         slice_param->macroblock_offset,
1371                                         0);
1372#else
1373
1374        psb_cmdbuf_dma_write_bitstream(ctx->obj_context->cmdbuf,
1375                                         obj_buffer->psb_buffer,
1376                                         obj_buffer->psb_buffer->buffer_ofs + slice_param->slice_data_offset,
1377                                         slice_param->slice_data_size,
1378                                         slice_param->macroblock_offset,
1379                                         0);
1380#endif
1381
1382        if (slice_param->slice_data_flag == VA_SLICE_DATA_FLAG_BEGIN) {
1383            ctx->split_buffer_pending = TRUE;
1384        }
1385    } else {
1386        ASSERT(ctx->split_buffer_pending);
1387        ASSERT(0 == slice_param->slice_data_offset);
1388        /* Create LLDMA chain to continue buffer */
1389        if (slice_param->slice_data_size) {
1390#ifndef DE3_FIRMWARE
1391            psb_cmdbuf_lldma_write_bitstream_chained(ctx->obj_context->cmdbuf,
1392                    obj_buffer->psb_buffer,
1393                    slice_param->slice_data_size);
1394#else
1395            psb_cmdbuf_dma_write_bitstream_chained(ctx->obj_context->cmdbuf,
1396                    obj_buffer->psb_buffer,
1397                    slice_param->slice_data_size);
1398#endif
1399        }
1400    }
1401
1402    if ((slice_param->slice_data_flag == VA_SLICE_DATA_FLAG_ALL) ||
1403        (slice_param->slice_data_flag == VA_SLICE_DATA_FLAG_END)) {
1404        if (slice_param->slice_data_flag == VA_SLICE_DATA_FLAG_END) {
1405            ASSERT(ctx->split_buffer_pending);
1406        }
1407
1408        psb__MPEG4_write_VLC_tables(ctx);
1409
1410        psb__MPEG4_set_picture_params(ctx, slice_param);
1411
1412        psb__MPEG4_set_frontend_registers(ctx, slice_param);
1413
1414        psb__MPEG4_set_backend_registers(ctx, slice_param);
1415
1416        psb__MPEG4_write_kick(ctx, slice_param);
1417
1418        ctx->split_buffer_pending = FALSE;
1419        ctx->obj_context->video_op = psb_video_vld;
1420        ctx->obj_context->flags = 0;
1421        ctx->obj_context->first_mb = 0;
1422        ctx->obj_context->last_mb = ((ctx->picture_height_mb - 1) << 8) | (ctx->picture_width_mb - 1);
1423
1424        *ctx->slice_first_pic_last = (ctx->obj_context->first_mb << 16) | (ctx->obj_context->last_mb);
1425
1426        if (psb_context_submit_cmdbuf(ctx->obj_context)) {
1427            vaStatus = VA_STATUS_ERROR_UNKNOWN;
1428        }
1429    }
1430    return vaStatus;
1431}
1432
1433static VAStatus psb__MPEG4_process_slice_data(context_MPEG4_p ctx, object_buffer_p obj_buffer)
1434{
1435    VAStatus vaStatus = VA_STATUS_SUCCESS;
1436    VASliceParameterBufferMPEG4 *slice_param;
1437    int buffer_idx = 0;
1438    unsigned int element_idx = 0;
1439
1440
1441    ASSERT((obj_buffer->type == VASliceDataBufferType) || (obj_buffer->type == VAProtectedSliceDataBufferType));
1442
1443    ASSERT(ctx->pic_params);
1444    ASSERT(ctx->slice_param_list_idx);
1445
1446    if (!ctx->pic_params) {
1447        /* Picture params missing */
1448        return VA_STATUS_ERROR_UNKNOWN;
1449    }
1450    if ((NULL == obj_buffer->psb_buffer) ||
1451        (0 == obj_buffer->size)) {
1452        /* We need to have data in the bitstream buffer */
1453        return VA_STATUS_ERROR_UNKNOWN;
1454    }
1455
1456    while (buffer_idx < ctx->slice_param_list_idx) {
1457        object_buffer_p slice_buf = ctx->slice_param_list[buffer_idx];
1458        if (element_idx >= slice_buf->num_elements) {
1459            /* Move to next buffer */
1460            element_idx = 0;
1461            buffer_idx++;
1462            continue;
1463        }
1464
1465        slice_param = (VASliceParameterBufferMPEG4 *) slice_buf->buffer_data;
1466        slice_param += element_idx;
1467        element_idx++;
1468        vaStatus = psb__MPEG4_process_slice(ctx, slice_param, obj_buffer);
1469        if (vaStatus != VA_STATUS_SUCCESS) {
1470            DEBUG_FAILURE;
1471            break;
1472        }
1473    }
1474    ctx->slice_param_list_idx = 0;
1475
1476    return vaStatus;
1477}
1478
1479static VAStatus pnw_MPEG4_BeginPicture(
1480    object_context_p obj_context)
1481{
1482    INIT_CONTEXT_MPEG4
1483
1484    if (ctx->pic_params) {
1485        free(ctx->pic_params);
1486        ctx->pic_params = NULL;
1487    }
1488    ctx->load_non_intra_quant_mat = FALSE;
1489    ctx->load_intra_quant_mat = FALSE;
1490
1491    return VA_STATUS_SUCCESS;
1492}
1493
1494static VAStatus pnw_MPEG4_RenderPicture(
1495    object_context_p obj_context,
1496    object_buffer_p *buffers,
1497    int num_buffers)
1498{
1499    int i;
1500    INIT_CONTEXT_MPEG4
1501    VAStatus vaStatus = VA_STATUS_SUCCESS;
1502
1503    for (i = 0; i < num_buffers; i++) {
1504        object_buffer_p obj_buffer = buffers[i];
1505
1506        switch (obj_buffer->type) {
1507        case VAPictureParameterBufferType:
1508            psb__information_message("pnw_MPEG4_RenderPicture got VAPictureParameterBuffer\n");
1509            vaStatus = psb__MPEG4_process_picture_param(ctx, obj_buffer);
1510            DEBUG_FAILURE;
1511            break;
1512
1513        case VAIQMatrixBufferType:
1514            psb__information_message("pnw_MPEG4_RenderPicture got VAIQMatrixBufferType\n");
1515            vaStatus = psb__MPEG4_process_iq_matrix(ctx, obj_buffer);
1516            DEBUG_FAILURE;
1517            break;
1518
1519        case VASliceParameterBufferType:
1520            psb__information_message("pnw_MPEG4_RenderPicture got VASliceParameterBufferType\n");
1521            vaStatus = psb__MPEG4_add_slice_param(ctx, obj_buffer);
1522            DEBUG_FAILURE;
1523            break;
1524
1525        case VASliceDataBufferType:
1526        case VAProtectedSliceDataBufferType:
1527
1528            psb__information_message("pnw_MPEG4_RenderPicture got %s\n", SLICEDATA_BUFFER_TYPE(obj_buffer->type));
1529            vaStatus = psb__MPEG4_process_slice_data(ctx, obj_buffer);
1530            DEBUG_FAILURE;
1531            break;
1532
1533        default:
1534            vaStatus = VA_STATUS_ERROR_UNKNOWN;
1535            DEBUG_FAILURE;
1536        }
1537        if (vaStatus != VA_STATUS_SUCCESS) {
1538            break;
1539        }
1540    }
1541
1542    return vaStatus;
1543}
1544
1545static VAStatus pnw_MPEG4_EndPicture(
1546    object_context_p obj_context)
1547{
1548    INIT_CONTEXT_MPEG4
1549
1550    if (psb_context_flush_cmdbuf(ctx->obj_context)) {
1551        return VA_STATUS_ERROR_UNKNOWN;
1552    }
1553
1554    if (ctx->pic_params) {
1555        free(ctx->pic_params);
1556        ctx->pic_params = NULL;
1557    }
1558
1559    return VA_STATUS_SUCCESS;
1560}
1561
1562struct format_vtable_s pnw_MPEG4_vtable = {
1563queryConfigAttributes:
1564    pnw_MPEG4_QueryConfigAttributes,
1565validateConfig:
1566    pnw_MPEG4_ValidateConfig,
1567createContext:
1568    pnw_MPEG4_CreateContext,
1569destroyContext:
1570    pnw_MPEG4_DestroyContext,
1571beginPicture:
1572    pnw_MPEG4_BeginPicture,
1573renderPicture:
1574    pnw_MPEG4_RenderPicture,
1575endPicture:
1576    pnw_MPEG4_EndPicture
1577};
1578