1 /******************************************************************************
2  *
3  * Copyright (C) 2015 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 
21 #ifndef __IMPEG2_DEFS_H__
22 #define __IMPEG2_DEFS_H__
23 
24 #include <assert.h>
25 
26 /* Decoder needs at least 4 reference buffers in order to support format conversion in a thread and
27 to support B pictures. Because of format conversion in a thread, codec delay is now 2 frames instead of 1.
28 To reduce this delay, format conversion has to wait for MB status before converting for B pictures.
29 To avoid this check the delay is increased to 2 and hence number of reference frames minimum is 4 */
30 #define NUM_INT_FRAME_BUFFERS                     4
31 
32 
33 #define MAX_WIDTH               4096
34 #define MAX_HEIGHT              2160
35 
36 #define MIN_WIDTH               16
37 #define MIN_HEIGHT              16
38 
39 
40 #define MAX_FRM_SIZE            (MAX_WIDTH * MAX_HEIGHT * 2)  /* Supports only 420P and 422ILE */
41 
42 #define DEC_ORDER               0
43 
44 #define MAX_BITSTREAM_BUFFER_SIZE       2000 * 1024
45 
46 
47 /******************************************************************************
48 * MPEG2 Start code and other code definitions
49 *******************************************************************************/
50 #define START_CODE_PREFIX               0x000001
51 #define SEQUENCE_HEADER_CODE            0x000001B3
52 #define EXTENSION_START_CODE            0x000001B5
53 #define USER_DATA_START_CODE            0x000001B2
54 #define GOP_START_CODE                  0x000001B8
55 #define PICTURE_START_CODE              0x00000100
56 #define SEQUENCE_END_CODE               0x000001B7
57 #define RESERVED_START_CODE             0x000001B0
58 #define MB_ESCAPE_CODE                  0x008
59 
60 /******************************************************************************
61 * MPEG2 Length of various codes definitions
62 *******************************************************************************/
63 #define START_CODE_LEN                  32
64 #define START_CODE_PREFIX_LEN           24
65 #define MB_ESCAPE_CODE_LEN              11
66 #define EXT_ID_LEN                      4
67 #define MB_QUANT_SCALE_CODE_LEN         5
68 #define MB_DCT_TYPE_LEN                 1
69 #define MB_MOTION_TYPE_LEN              2
70 #define BYTE_LEN                        8
71 
72 /******************************************************************************
73 * MPEG1 code definitions
74 *******************************************************************************/
75 #define MB_STUFFING_CODE                0x00F
76 
77 /******************************************************************************
78 * MPEG1 Length of various codes definitions
79 *******************************************************************************/
80 #define MB_STUFFING_CODE_LEN             11
81 
82 /******************************************************************************
83 * MPEG2 MB definitions
84 *******************************************************************************/
85 #define MPEG2_INTRA_MB                  0x04
86 #define MPEG2_INTRAQ_MB                 0x44
87 #define MPEG2_INTER_MB                  0x28
88 #define MB_MOTION_BIDIRECT              0x30
89 #define MB_INTRA_OR_PATTERN             0x0C
90 
91 /******************************************************************************
92 * Tools definitions
93 *******************************************************************************/
94 #define SPATIAL_SCALABILITY             0x01
95 #define TEMPORAL_SCALABILITY            0x03
96 
97 /******************************************************************************
98 * Extension IDs definitions
99 *******************************************************************************/
100 #define SEQ_DISPLAY_EXT_ID              0x02
101 #define SEQ_SCALABLE_EXT_ID             0x05
102 #define QUANT_MATRIX_EXT_ID             0x03
103 #define COPYRIGHT_EXT_ID                0x04
104 #define PIC_DISPLAY_EXT_ID              0x07
105 #define PIC_SPATIAL_SCALABLE_EXT_ID     0x09
106 #define PIC_TEMPORAL_SCALABLE_EXT_ID    0x0A
107 #define CAMERA_PARAM_EXT_ID             0x0B
108 #define ITU_T_EXT_ID                    0x0C
109 /******************************************************************************
110 * Extension IDs Length definitions
111 *******************************************************************************/
112 #define CAMERA_PARAMETER_EXTENSION_LEN  377
113 #define COPYRIGHT_EXTENSION_LEN          88
114 #define GROUP_OF_PICTURE_LEN             59
115 
116 
117 /******************************************************************************
118 * MPEG2 Picture structure definitions
119 *******************************************************************************/
120 #define TOP_FIELD                       1
121 #define BOTTOM_FIELD                    2
122 #define FRAME_PICTURE                   3
123 
124 /******************************************************************************
125 * MPEG2 Profile definitions
126 *******************************************************************************/
127 #define MPEG2_SIMPLE_PROFILE            0x05
128 #define MPEG2_MAIN_PROFILE              0x04
129 
130 /******************************************************************************
131 * MPEG2 Level definitions
132 *******************************************************************************/
133 #define MPEG2_LOW_LEVEL                 0x0a
134 #define MPEG2_MAIN_LEVEL                0x08
135 
136 /******************************************************************************
137 * MPEG2 Prediction types
138 *******************************************************************************/
139 #define FIELD_PRED                      0
140 #define FRAME_PRED                      1
141 #define DUAL_PRED                       2
142 #define RESERVED                        -1
143 #define MC_16X8_PRED                    3
144 
145 /*****************************************************************************
146 * MPEG2 Motion vector format
147 ******************************************************************************/
148 #define FIELD_MV                        0
149 #define FRAME_MV                        1
150 
151 /******************************************************************************/
152 /* General Video related definitions                                          */
153 /******************************************************************************/
154 
155 #define BLK_SIZE 8
156 #define NUM_COEFFS ((BLK_SIZE)*(BLK_SIZE))
157 #define LUMA_BLK_SIZE (2 * (BLK_SIZE))
158 #define CHROMA_BLK_SIZE (BLK_SIZE)
159 #define  BLOCKS_IN_MB            6
160 #define  MB_SIZE                16
161 #define  MB_CHROMA_SIZE          8
162 #define  NUM_PELS_IN_BLOCK      64
163 #define  NUM_LUMA_BLKS           4
164 #define  NUM_CHROMA_BLKS         2
165 #define  MAX_COLR_COMPS          3
166 #define  Y_LUMA                  0
167 #define  U_CHROMA                1
168 #define  V_CHROMA                2
169 #define  MB_LUMA_MEM_SIZE           ((MB_SIZE) * (MB_SIZE))
170 #define  MB_CHROMA_MEM_SIZE         ((MB_SIZE/2) * (MB_SIZE/2))
171 
172 #define BITS_IN_INT     32
173 /******************************************************************************/
174 /* MPEG2 Motion compensation related definitions                              */
175 /******************************************************************************/
176 #define REF_FRM_MB_WIDTH        18
177 #define REF_FRM_MB_HEIGHT       18
178 #define REF_FLD_MB_HEIGHT       10
179 #define REF_FLD_MB_WIDTH        18
180 
181 /******************************************************************************/
182 /* Maximum number of bits per MB                                              */
183 /******************************************************************************/
184 #define I_MB_BIT_SIZE 90
185 #define P_MB_BIT_SIZE 90
186 #define B_MB_BIT_SIZE 150
187 
188 /******************************************************************************/
189 /* Aspect ratio related definitions                                           */
190 /******************************************************************************/
191 #define MPG1_NTSC_4_3       0x8
192 #define MPG1_PAL_4_3        0xc
193 #define MPG1_NTSC_16_9      0x6
194 #define MPG1_PAL_16_9       0x3
195 #define MPG1_1_1            0x1
196 
197 #define MPG2_4_3            0x2
198 #define MPG2_16_9           0x3
199 #define MPG2_1_1            0x1
200 
201 /******************************************************************************/
202 /* Inverse Quantizer Output range                                             */
203 /******************************************************************************/
204 #define IQ_OUTPUT_MAX 2047
205 #define IQ_OUTPUT_MIN -2048
206 
207 /******************************************************************************/
208 /* IDCT Output range                                                          */
209 /******************************************************************************/
210 #define IDCT_OUTPUT_MAX  255
211 #define IDCT_OUTPUT_MIN -256
212 
213 /******************************************************************************/
214 /* Output pixel range                                                         */
215 /******************************************************************************/
216 #define PEL_VALUE_MAX 255
217 #define PEL_VALUE_MIN 0
218 
219 /******************************************************************************/
220 /* inv scan types                                                             */
221 /******************************************************************************/
222 #define ZIG_ZAG_SCAN        0
223 #define VERTICAL_SCAN       1
224 
225 /******************************************************************************/
226 /* Related VLD codes                                                          */
227 /******************************************************************************/
228 #define ESC_CODE_VALUE 0x0058
229 #define EOB_CODE_VALUE 0x07d0
230 
231 #define END_OF_BLOCK                    0x01
232 #define ESCAPE_CODE                     0x06
233 
234 #define END_OF_BLOCK_ZERO               0x01ff
235 #define END_OF_BLOCK_ONE                0x01ff
236 
237 /******************** Idct Specific ***************/
238 #define TRANS_SIZE_8            8
239 #define IDCT_STG1_SHIFT        12
240 #define IDCT_STG2_SHIFT        16
241 
242 #define IDCT_STG1_ROUND        ((1 << IDCT_STG1_SHIFT) >> 1)
243 #define IDCT_STG2_ROUND        ((1 << IDCT_STG2_SHIFT) >> 1)
244 
245 
246 /******************************************************************************
247 * Sample Version Definitions
248 *******************************************************************************/
249 #define SAMPLE_VERS_MAX_FRAMES_DECODE   999
250 
251 #define MAX_FRAME_BUFFER                     7
252 
253 /* vop coding type */
254 typedef enum
255 {
256     I_PIC = 1,
257     P_PIC,
258     B_PIC,
259     D_PIC
260 } e_pic_type_t;
261 
262 typedef enum
263 {
264     MPEG_2_VIDEO,
265     MPEG_1_VIDEO
266 } e_video_type_t;
267 
268 typedef enum
269 {
270     FORW,
271     BACK,
272     BIDIRECT
273 } e_pred_direction_t;
274 
275 typedef enum
276 {
277     TOP,
278     BOTTOM
279 } e_field_t;
280 
281 /* Motion vectors (first/second) */
282 enum
283 {
284     FIRST,
285     SECOND,
286     THIRD,
287     FOURTH
288 };
289 
290 enum
291 {
292     MV_X,
293     MV_Y
294 };
295 
296 /* Enumeration defining the various kinds of interpolation possible in
297 motion compensation */
298 typedef enum
299 {
300   FULL_XFULL_Y,
301     FULL_XHALF_Y,
302     HALF_XFULL_Y,
303     HALF_XHALF_Y
304 } e_sample_type_t;
305 typedef enum
306 {
307     /* Params of the reference buffer used as input to MC */
308     /* frame prediction in P frame picture */
309     MC_FRM_FW_OR_BK_1MV,
310     /* field prediction in P frame picture */
311     MC_FRM_FW_OR_BK_2MV,
312     /* frame prediction in B frame picture */
313     MC_FRM_FW_AND_BK_2MV,
314     /* field prediction in B frame picture */
315     MC_FRM_FW_AND_BK_4MV,
316     /* dual prime prediction in P frame picture */
317     MC_FRM_FW_DUAL_PRIME_1MV,
318     /* frame prediction in P field picture */
319     MC_FLD_FW_OR_BK_1MV,
320     /* 16x8 prediction in P field picture */
321     MC_FLD_FW_OR_BK_2MV,
322     /* field prediction in B field picture */
323     MC_FLD_FW_AND_BK_2MV,
324     /* 16x8 prediction in B field picture */
325     MC_FLD_FW_AND_BK_4MV,
326     /* dual prime prediction in P field picture */
327     MC_FLD_FW_DUAL_PRIME_1MV,
328 } e_mb_type_t;
329 
330 #endif /* __IMPEG2_DEFS_H__ */
331 
332