/src/libmpeg2/decoder/impeg2d_vld.c
Line | Count | Source |
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 | | #include <string.h> |
21 | | |
22 | | #include "iv_datatypedef.h" |
23 | | #include "iv.h" |
24 | | |
25 | | #include "impeg2_buf_mgr.h" |
26 | | #include "impeg2_disp_mgr.h" |
27 | | #include "impeg2_defs.h" |
28 | | #include "impeg2_platform_macros.h" |
29 | | #include "impeg2_inter_pred.h" |
30 | | #include "impeg2_idct.h" |
31 | | #include "impeg2_globals.h" |
32 | | #include "impeg2_format_conv.h" |
33 | | #include "impeg2_macros.h" |
34 | | |
35 | | #include "ivd.h" |
36 | | #include "impeg2d.h" |
37 | | #include "impeg2d_bitstream.h" |
38 | | #include "impeg2d_structs.h" |
39 | | #include "impeg2d_vld_tables.h" |
40 | | #include "impeg2d_vld.h" |
41 | | #include "impeg2d_pic_proc.h" |
42 | | #include "impeg2d_debug.h" |
43 | | |
44 | | |
45 | | /******************************************************************************* |
46 | | * Function name : impeg2d_dec_vld_symbol |
47 | | * |
48 | | * Description : Performs decoding of VLD symbol. It performs decoding by |
49 | | * processing 1 bit at a time |
50 | | * |
51 | | * Arguments : |
52 | | * stream : Bitstream |
53 | | * ai2_code_table : Table used for decoding |
54 | | * maxLen : Maximum Length of the decoded symbol in bits |
55 | | * |
56 | | * Value Returned: Decoded symbol |
57 | | *******************************************************************************/ |
58 | | WORD16 impeg2d_dec_vld_symbol(stream_t *ps_stream,const WORD16 ai2_code_table[][2], UWORD16 u2_max_len) |
59 | 211M | { |
60 | 211M | UWORD16 u2_data; |
61 | 211M | WORD16 u2_end = 0; |
62 | 211M | UWORD16 u2_org_max_len = u2_max_len; |
63 | 211M | UWORD16 u2_i_bit; |
64 | | |
65 | | /* Get the maximum number of bits needed to decode a symbol */ |
66 | 211M | u2_data = impeg2d_bit_stream_nxt(ps_stream,u2_max_len); |
67 | 211M | do |
68 | 552M | { |
69 | 552M | u2_max_len--; |
70 | | /* Read one bit at a time from the variable to decode the huffman code */ |
71 | 552M | u2_i_bit = (UWORD8)((u2_data >> u2_max_len) & 0x1); |
72 | | |
73 | | /* Get the next node pointer or the symbol from the tree */ |
74 | 552M | u2_end = ai2_code_table[u2_end][u2_i_bit]; |
75 | 552M | }while(u2_end > 0); |
76 | | |
77 | | /* Flush the appropriate number of bits from the ps_stream */ |
78 | 211M | impeg2d_bit_stream_flush(ps_stream,(UWORD8)(u2_org_max_len - u2_max_len)); |
79 | 211M | return(u2_end); |
80 | 211M | } |
81 | | /******************************************************************************* |
82 | | * Function name : impeg2d_fast_dec_vld_symbol |
83 | | * |
84 | | * Description : Performs decoding of VLD symbol. It performs decoding by |
85 | | * processing n bits at a time |
86 | | * |
87 | | * Arguments : |
88 | | * stream : Bitstream |
89 | | * ai2_code_table : Code table containing huffman value |
90 | | * indexTable : Index table containing index |
91 | | * maxLen : Maximum Length of the decoded symbol in bits |
92 | | * |
93 | | * Value Returned: Decoded symbol |
94 | | *******************************************************************************/ |
95 | | WORD16 impeg2d_fast_dec_vld_symbol(stream_t *ps_stream, |
96 | | const WORD16 ai2_code_table[][2], |
97 | | const UWORD16 au2_indexTable[][2], |
98 | | UWORD16 u2_max_len) |
99 | 0 | { |
100 | 0 | UWORD16 u2_cur_code; |
101 | 0 | UWORD16 u2_num_bits; |
102 | 0 | UWORD16 u2_vld_offset; |
103 | 0 | UWORD16 u2_start_len; |
104 | 0 | WORD16 u2_value; |
105 | 0 | UWORD16 u2_len; |
106 | 0 | UWORD16 u2_huffCode; |
107 | |
|
108 | 0 | u2_start_len = au2_indexTable[0][0]; |
109 | 0 | u2_vld_offset = 0; |
110 | 0 | u2_huffCode = impeg2d_bit_stream_nxt(ps_stream,u2_max_len); |
111 | 0 | do |
112 | 0 | { |
113 | 0 | u2_cur_code = u2_huffCode >> (u2_max_len - u2_start_len); |
114 | 0 | u2_num_bits = ai2_code_table[u2_cur_code + u2_vld_offset][0]; |
115 | 0 | if(u2_num_bits == 0) |
116 | 0 | { |
117 | 0 | u2_huffCode &= ((1 << (u2_max_len - u2_start_len)) - 1); |
118 | 0 | u2_max_len -= u2_start_len; |
119 | 0 | u2_start_len = au2_indexTable[ai2_code_table[u2_cur_code + u2_vld_offset][1]][0]; |
120 | 0 | u2_vld_offset = au2_indexTable[ai2_code_table[u2_cur_code + u2_vld_offset][1]][1]; |
121 | 0 | } |
122 | 0 | else |
123 | 0 | { |
124 | 0 | u2_value = ai2_code_table[u2_cur_code + u2_vld_offset][1]; |
125 | 0 | u2_len = u2_num_bits; |
126 | 0 | } |
127 | 0 | }while(u2_num_bits == 0); |
128 | 0 | impeg2d_bit_stream_flush(ps_stream,u2_len); |
129 | 0 | return(u2_value); |
130 | 0 | } |
131 | | /****************************************************************************** |
132 | | * |
133 | | * Function Name : impeg2d_dec_ac_coeff_zero |
134 | | * |
135 | | * Description : Decodes using Table B.14 |
136 | | * |
137 | | * Arguments : Pointer to VideoObjectLayerStructure |
138 | | * |
139 | | * Values Returned : Decoded value |
140 | | * |
141 | | * Revision History: |
142 | | * |
143 | | * 28 02 2002 AR Creation |
144 | | *******************************************************************************/ |
145 | | UWORD16 impeg2d_dec_ac_coeff_zero(stream_t *ps_stream, UWORD16* pu2_sym_len, UWORD16* pu2_sym_val) |
146 | 0 | { |
147 | 0 | UWORD16 u2_offset,u2_decoded_value; |
148 | 0 | UWORD8 u1_shift; |
149 | 0 | UWORD32 u4_bits_read; |
150 | |
|
151 | 0 | u4_bits_read = (UWORD16)impeg2d_bit_stream_nxt(ps_stream,MPEG2_AC_COEFF_MAX_LEN); |
152 | |
|
153 | 0 | if ((UWORD16)u4_bits_read >= 0x0800) |
154 | 0 | { |
155 | 0 | u2_offset = (UWORD16)u4_bits_read >> 11; |
156 | 0 | } |
157 | 0 | else if ((UWORD16)u4_bits_read >= 0x40) |
158 | 0 | { |
159 | 0 | u2_offset = 31 + ((UWORD16)u4_bits_read >> 6); |
160 | 0 | } |
161 | 0 | else if ((UWORD16)u4_bits_read >= 0x20) |
162 | 0 | { |
163 | 0 | u2_offset = 64; |
164 | 0 | } |
165 | 0 | else |
166 | 0 | { |
167 | 0 | u2_offset = 63; |
168 | 0 | u4_bits_read = (UWORD16)u4_bits_read - 0x10; |
169 | 0 | } |
170 | | /*----------------------------------------------------------------------- |
171 | | * The table gOffset contains both the offset for the group to which the |
172 | | * Vld code belongs in the Ac Coeff Table and the no of bits with which |
173 | | * the BitsRead should be shifted |
174 | | *-----------------------------------------------------------------------*/ |
175 | 0 | u2_offset = gau2_impeg2d_offset_zero[u2_offset]; |
176 | 0 | u1_shift = u2_offset & 0xF; |
177 | | |
178 | | /*----------------------------------------------------------------------- |
179 | | * Depending upon the vld code, we index exactly to that particular |
180 | | * Vld codes value in the Ac Coeff Table. |
181 | | * (Offset >> 4) gives the offset for the group in the AcCoeffTable. |
182 | | * (BitsRead >> shift) gives the offset within its group |
183 | | *-----------------------------------------------------------------------*/ |
184 | 0 | u2_offset = (u2_offset >> 4) + ((UWORD16)u4_bits_read >> u1_shift); |
185 | | /*----------------------------------------------------------------------- |
186 | | * DecodedValue has the Run, Level and the number of bits used by Vld code |
187 | | *-----------------------------------------------------------------------*/ |
188 | 0 | u2_decoded_value = gau2_impeg2d_dct_coeff_zero[u2_offset]; |
189 | 0 | if(u2_decoded_value == END_OF_BLOCK) |
190 | 0 | { |
191 | 0 | *pu2_sym_len = 2; |
192 | 0 | *pu2_sym_val = EOB_CODE_VALUE; |
193 | 0 | } |
194 | 0 | else if(u2_decoded_value == ESCAPE_CODE) |
195 | 0 | { |
196 | 0 | *pu2_sym_len = u2_decoded_value & 0x1F; |
197 | 0 | *pu2_sym_val = ESC_CODE_VALUE; |
198 | 0 | } |
199 | 0 | else |
200 | 0 | { |
201 | 0 | *pu2_sym_len = u2_decoded_value & 0x1F; |
202 | 0 | *pu2_sym_val = u2_decoded_value >> 5; |
203 | 0 | } |
204 | 0 | return(u2_decoded_value); |
205 | 0 | } |
206 | | |
207 | | /****************************************************************************** |
208 | | * |
209 | | * Function Name : impeg2d_dec_ac_coeff_one |
210 | | * |
211 | | * Description : Decodes using Table B.15 |
212 | | * |
213 | | * Arguments : Pointer to VideoObjectLayerStructure |
214 | | * |
215 | | * Values Returned : Decoded value |
216 | | * |
217 | | * Revision History: |
218 | | * |
219 | | * 28 02 2002 AR Creation |
220 | | *******************************************************************************/ |
221 | | UWORD16 impeg2d_dec_ac_coeff_one(stream_t *ps_stream, UWORD16* pu2_sym_len, UWORD16* pu2_sym_val) |
222 | 0 | { |
223 | 0 | UWORD16 u2_offset, u2_decoded_value; |
224 | 0 | UWORD8 u1_shift; |
225 | 0 | UWORD32 u4_bits_read; |
226 | | |
227 | |
|
228 | 0 | u4_bits_read = (UWORD16)impeg2d_bit_stream_nxt(ps_stream,MPEG2_AC_COEFF_MAX_LEN); |
229 | |
|
230 | 0 | if ((UWORD16)u4_bits_read >= 0x8000) |
231 | 0 | { |
232 | | /* If the MSB of the vld code is 1 */ |
233 | 0 | if (((UWORD16)u4_bits_read >> 12) == 0xF) |
234 | 0 | u2_offset = ((UWORD16)u4_bits_read >> 8) & 0xF; |
235 | 0 | else |
236 | 0 | u2_offset = (UWORD16)u4_bits_read >> 11; |
237 | 0 | u2_offset += gau2_impeg2d_offset_one[0]; |
238 | 0 | } |
239 | 0 | else if ((UWORD16)u4_bits_read >= 0x400) |
240 | 0 | { |
241 | 0 | u2_offset =(UWORD16) u4_bits_read >> 10; |
242 | 0 | u2_offset = gau2_impeg2d_offset_one[u2_offset]; |
243 | 0 | u1_shift = u2_offset & 0xF; |
244 | 0 | u2_offset = (u2_offset >> 4) + ((UWORD16)u4_bits_read >> u1_shift); |
245 | 0 | } |
246 | 0 | else if ((UWORD16)u4_bits_read >= 0x20) |
247 | 0 | { |
248 | 0 | u2_offset = ((UWORD16)u4_bits_read >> 5) + 31; |
249 | 0 | u2_offset = gau2_impeg2d_offset_one[u2_offset]; |
250 | 0 | u1_shift = u2_offset & 0xF; |
251 | 0 | u2_offset = (u2_offset >> 4) + ((UWORD16)u4_bits_read >> u1_shift); |
252 | 0 | } |
253 | 0 | else |
254 | 0 | { |
255 | 0 | u2_offset = gau2_impeg2d_offset_one[63] + ((UWORD16)u4_bits_read & 0xF); |
256 | 0 | } |
257 | | /*----------------------------------------------------------------------- |
258 | | * DecodedValue has the Run, Level and the number of bits used by Vld code |
259 | | *-----------------------------------------------------------------------*/ |
260 | 0 | u2_decoded_value = gau2_impeg2d_dct_coeff_one[u2_offset]; |
261 | |
|
262 | 0 | if(u2_decoded_value == END_OF_BLOCK) |
263 | 0 | { |
264 | 0 | *pu2_sym_len = 4; |
265 | 0 | *pu2_sym_val = EOB_CODE_VALUE; |
266 | 0 | } |
267 | 0 | else if(u2_decoded_value == ESCAPE_CODE) |
268 | 0 | { |
269 | 0 | *pu2_sym_len = u2_decoded_value & 0x1F; |
270 | 0 | *pu2_sym_val = ESC_CODE_VALUE; |
271 | 0 | } |
272 | 0 | else |
273 | 0 | { |
274 | 0 | *pu2_sym_len = u2_decoded_value & 0x1F; |
275 | 0 | *pu2_sym_val = u2_decoded_value >> 5; |
276 | 0 | } |
277 | |
|
278 | 0 | return(u2_decoded_value); |
279 | 0 | } |
280 | | |
281 | | /****************************************************************************** |
282 | | * |
283 | | * Function Name : impeg2d_vld_inv_quant_mpeg1 |
284 | | * |
285 | | * Description : Performs VLD operation for MPEG1/2 |
286 | | * |
287 | | * Arguments : |
288 | | * state : VLCD state parameter |
289 | | * regs : Registers of VLCD |
290 | | * |
291 | | * Values Returned : None |
292 | | ******************************************************************************/ |
293 | | IMPEG2D_ERROR_CODES_T impeg2d_vld_inv_quant_mpeg1( |
294 | | void *pv_dec, /* Decoder State */ |
295 | | WORD16 *pi2_out_addr, /*!< Address where decoded symbols will be stored */ |
296 | | const UWORD8 *pu1_scan, /*!< Scan table to be used */ |
297 | | UWORD16 u2_intra_flag, /*!< Intra Macroblock or not */ |
298 | | UWORD16 u2_colr_comp, /*!< 0 - Luma,1 - U comp, 2 - V comp */ |
299 | | UWORD16 u2_d_picture /*!< D Picture or not */ |
300 | | ) |
301 | 6.32M | { |
302 | 6.32M | UWORD8 *pu1_weighting_matrix; |
303 | 6.32M | dec_state_t *ps_dec = (dec_state_t *) pv_dec; |
304 | 6.32M | IMPEG2D_ERROR_CODES_T e_error = (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE; |
305 | | |
306 | 6.32M | WORD16 pi2_coeffs[NUM_COEFFS]; |
307 | 6.32M | UWORD8 pu1_pos[NUM_COEFFS]; |
308 | 6.32M | WORD32 i4_num_coeffs; |
309 | | |
310 | | /* Perform VLD on the stream to get the coefficients and their positions */ |
311 | 6.32M | e_error = impeg2d_vld_decode(ps_dec, pi2_coeffs, pu1_scan, pu1_pos, u2_intra_flag, |
312 | 6.32M | u2_colr_comp, u2_d_picture, ps_dec->u2_intra_vlc_format, |
313 | 6.32M | ps_dec->u2_is_mpeg2, &i4_num_coeffs); |
314 | 6.32M | if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error) |
315 | 7.23k | { |
316 | 7.23k | return e_error; |
317 | 7.23k | } |
318 | | |
319 | | /* For YUV420 format,Select the weighting matrix according to Table 7.5 */ |
320 | 6.32M | pu1_weighting_matrix = (u2_intra_flag == 1) ? ps_dec->au1_intra_quant_matrix: |
321 | 6.32M | ps_dec->au1_inter_quant_matrix; |
322 | | |
323 | 6.32M | IMPEG2D_IQNT_INP_STATISTICS(pi2_out_addr, ps_dec->u4_non_zero_cols, ps_dec->u4_non_zero_rows); |
324 | | /* Inverse Quantize the Output of VLD */ |
325 | | PROFILE_DISABLE_INVQUANT_IF0 |
326 | | |
327 | 6.32M | { |
328 | | /* Clear output matrix */ |
329 | 6.32M | PROFILE_DISABLE_MEMSET_RESBUF_IF0 |
330 | 6.32M | if (1 != (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows)) |
331 | 5.52M | { |
332 | 5.52M | memset(pi2_out_addr, 0, 64 * sizeof(WORD16)); |
333 | 5.52M | } |
334 | | |
335 | 6.32M | impeg2d_inv_quant_mpeg1(pi2_out_addr, pu1_weighting_matrix, |
336 | 6.32M | ps_dec->u1_quant_scale, u2_intra_flag, |
337 | 6.32M | i4_num_coeffs, pi2_coeffs, pu1_pos, |
338 | 6.32M | pu1_scan, &ps_dec->u2_def_dc_pred[u2_colr_comp], |
339 | 6.32M | ps_dec->u2_intra_dc_precision); |
340 | | |
341 | 6.32M | if (0 != pi2_out_addr[0]) |
342 | 1.86M | { |
343 | | /* The first coeff might've become non-zero due to intra_dc_decision |
344 | | * value. So, check here after inverse quantization. |
345 | | */ |
346 | 1.86M | ps_dec->u4_non_zero_cols |= 0x1; |
347 | 1.86M | ps_dec->u4_non_zero_rows |= 0x1; |
348 | 1.86M | } |
349 | 6.32M | } |
350 | | |
351 | 6.32M | return e_error; |
352 | 6.32M | } |
353 | | |
354 | | /****************************************************************************** |
355 | | * |
356 | | * Function Name : impeg2d_vld_inv_quant_mpeg2 |
357 | | * |
358 | | * Description : Performs VLD operation for MPEG1/2 |
359 | | * |
360 | | * Arguments : |
361 | | * state : VLCD state parameter |
362 | | * regs : Registers of VLCD |
363 | | * |
364 | | * Values Returned : None |
365 | | ******************************************************************************/ |
366 | | IMPEG2D_ERROR_CODES_T impeg2d_vld_inv_quant_mpeg2( |
367 | | void *pv_dec, /* Decoder State */ |
368 | | WORD16 *pi2_out_addr, /*!< Address where decoded symbols will be stored */ |
369 | | const UWORD8 *pu1_scan, /*!< Scan table to be used */ |
370 | | UWORD16 u2_intra_flag, /*!< Intra Macroblock or not */ |
371 | | UWORD16 u2_colr_comp, /*!< 0 - Luma,1 - U comp, 2 - V comp */ |
372 | | UWORD16 u2_d_picture /*!< D Picture or not */ |
373 | | ) |
374 | 675k | { |
375 | 675k | UWORD8 *pu1_weighting_matrix; |
376 | 675k | WORD32 i4_sum; |
377 | 675k | dec_state_t *ps_dec = (dec_state_t *)pv_dec; |
378 | 675k | IMPEG2D_ERROR_CODES_T e_error = (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE; |
379 | | |
380 | 675k | WORD16 pi2_coeffs[NUM_COEFFS]; |
381 | 675k | UWORD8 pi4_pos[NUM_COEFFS]; |
382 | 675k | WORD32 i4_num_coeffs; |
383 | | |
384 | | /* Perform VLD on the stream to get the coefficients and their positions */ |
385 | 675k | e_error = impeg2d_vld_decode(ps_dec, pi2_coeffs, pu1_scan, pi4_pos, u2_intra_flag, |
386 | 675k | u2_colr_comp, u2_d_picture, ps_dec->u2_intra_vlc_format, |
387 | 675k | ps_dec->u2_is_mpeg2, &i4_num_coeffs); |
388 | 675k | if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error) |
389 | 6.08k | { |
390 | 6.08k | return e_error; |
391 | 6.08k | } |
392 | | |
393 | | /* For YUV420 format,Select the weighting matrix according to Table 7.5 */ |
394 | 669k | pu1_weighting_matrix = (u2_intra_flag == 1) ? ps_dec->au1_intra_quant_matrix: |
395 | 669k | ps_dec->au1_inter_quant_matrix; |
396 | | |
397 | | /*mismatch control for mpeg2*/ |
398 | | /* Check if the block has only one non-zero coeff which is DC */ |
399 | 669k | ps_dec->i4_last_value_one = 0; |
400 | | |
401 | 669k | IMPEG2D_IQNT_INP_STATISTICS(pi2_out_addr, ps_dec->u4_non_zero_cols, ps_dec->u4_non_zero_rows); |
402 | | |
403 | | /* Inverse Quantize the Output of VLD */ |
404 | | PROFILE_DISABLE_INVQUANT_IF0 |
405 | | |
406 | 669k | { |
407 | | /* Clear output matrix */ |
408 | 669k | PROFILE_DISABLE_MEMSET_RESBUF_IF0 |
409 | 669k | if (1 != (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows)) |
410 | 481k | { |
411 | 481k | memset(pi2_out_addr, 0, 64 * sizeof(WORD16)); |
412 | 481k | } |
413 | | |
414 | 669k | i4_sum = impeg2d_inv_quant_mpeg2(pi2_out_addr, pu1_weighting_matrix, |
415 | 669k | ps_dec->u1_quant_scale, u2_intra_flag, |
416 | 669k | i4_num_coeffs, pi2_coeffs, |
417 | 669k | pi4_pos, pu1_scan, |
418 | 669k | &ps_dec->u2_def_dc_pred[u2_colr_comp], |
419 | 669k | ps_dec->u2_intra_dc_precision); |
420 | | |
421 | 669k | if (0 != pi2_out_addr[0]) |
422 | 587k | { |
423 | | /* The first coeff might've become non-zero due to intra_dc_decision |
424 | | * value. So, check here after inverse quantization. |
425 | | */ |
426 | 587k | ps_dec->u4_non_zero_cols |= 0x1; |
427 | 587k | ps_dec->u4_non_zero_rows |= 0x1; |
428 | 587k | } |
429 | | |
430 | 669k | if (1 == (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows)) |
431 | 197k | { |
432 | 197k | ps_dec->i4_last_value_one = 1 - (pi2_out_addr[0] & 1); |
433 | 197k | } |
434 | 471k | else |
435 | 471k | { |
436 | | /*toggle last bit if sum is even ,else retain it as it is*/ |
437 | 471k | pi2_out_addr[63] ^= (i4_sum & 1); |
438 | | |
439 | 471k | if (0 != pi2_out_addr[63]) |
440 | 271k | { |
441 | 271k | ps_dec->u4_non_zero_cols |= 0x80; |
442 | 271k | ps_dec->u4_non_zero_rows |= 0x80; |
443 | 271k | } |
444 | 471k | } |
445 | 669k | } |
446 | | |
447 | 669k | return e_error; |
448 | 675k | } |
449 | | |
450 | | |
451 | | /****************************************************************************** |
452 | | * |
453 | | * Function Name : impeg2d_vld_decode |
454 | | * |
455 | | * Description : Performs VLD operation for MPEG1/2 |
456 | | * |
457 | | * Arguments : |
458 | | * state : VLCD state parameter |
459 | | * regs : Registers of VLCD |
460 | | * |
461 | | * Values Returned : None |
462 | | ******************************************************************************/ |
463 | | IMPEG2D_ERROR_CODES_T impeg2d_vld_decode( |
464 | | dec_state_t *ps_dec, |
465 | | WORD16 *pi2_outAddr, /*!< Address where decoded symbols will be stored */ |
466 | | const UWORD8 *pu1_scan, /*!< Scan table to be used */ |
467 | | UWORD8 *pu1_pos, /*!< Scan table to be used */ |
468 | | UWORD16 u2_intra_flag, /*!< Intra Macroblock or not */ |
469 | | UWORD16 u2_chroma_flag, /*!< Chroma Block or not */ |
470 | | UWORD16 u2_d_picture, /*!< D Picture or not */ |
471 | | UWORD16 u2_intra_vlc_format, /*!< Intra VLC format */ |
472 | | UWORD16 u2_mpeg2, /*!< MPEG-2 or not */ |
473 | | WORD32 *pi4_num_coeffs /*!< Returns the number of coeffs in block */ |
474 | | ) |
475 | 7.05M | { |
476 | | |
477 | 7.05M | UWORD32 u4_sym_len; |
478 | | |
479 | 7.05M | UWORD32 u4_decoded_value; |
480 | 7.05M | WORD32 i4_level_first_byte; |
481 | 7.05M | WORD32 i4_level; |
482 | 7.05M | UWORD32 u4_run, u4_numCoeffs; |
483 | 7.05M | UWORD32 u4_buf; |
484 | 7.05M | UWORD32 u4_buf_nxt; |
485 | 7.05M | UWORD32 u4_offset; |
486 | 7.05M | UWORD32 *pu4_buf_aligned; |
487 | 7.05M | UWORD32 u4_bits; |
488 | 7.05M | stream_t *ps_stream = &ps_dec->s_bit_stream; |
489 | 7.05M | WORD32 u4_pos; |
490 | 7.05M | UWORD32 u4_nz_cols; |
491 | 7.05M | UWORD32 u4_nz_rows; |
492 | | |
493 | 7.05M | *pi4_num_coeffs = 0; |
494 | | |
495 | 7.05M | ps_dec->u4_non_zero_cols = 0; |
496 | 7.05M | ps_dec->u4_non_zero_rows = 0; |
497 | 7.05M | u4_nz_cols = ps_dec->u4_non_zero_cols; |
498 | 7.05M | u4_nz_rows = ps_dec->u4_non_zero_rows; |
499 | | |
500 | 7.05M | GET_TEMP_STREAM_DATA(u4_buf,u4_buf_nxt,u4_offset,pu4_buf_aligned,ps_stream) |
501 | | /**************************************************************************/ |
502 | | /* Decode the DC coefficient in case of Intra block */ |
503 | | /**************************************************************************/ |
504 | 7.05M | if(u2_intra_flag) |
505 | 1.43M | { |
506 | 1.43M | WORD32 dc_size; |
507 | 1.43M | WORD32 dc_diff; |
508 | 1.43M | WORD32 maxLen; |
509 | 1.43M | WORD32 idx; |
510 | | |
511 | | |
512 | 1.43M | maxLen = MPEG2_DCT_DC_SIZE_LEN; |
513 | 1.43M | idx = 0; |
514 | 1.43M | if(u2_chroma_flag != 0) |
515 | 503k | { |
516 | 503k | maxLen += 1; |
517 | 503k | idx++; |
518 | 503k | } |
519 | | |
520 | | |
521 | 1.43M | { |
522 | 1.43M | WORD16 end = 0; |
523 | 1.43M | UWORD32 maxLen_tmp = maxLen; |
524 | 1.43M | UWORD16 m_iBit; |
525 | | |
526 | | |
527 | | /* Get the maximum number of bits needed to decode a symbol */ |
528 | 1.43M | IBITS_NXT(u4_buf,u4_buf_nxt,u4_offset,u4_bits,maxLen) |
529 | 1.43M | do |
530 | 3.94M | { |
531 | 3.94M | maxLen_tmp--; |
532 | | /* Read one bit at a time from the variable to decode the huffman code */ |
533 | 3.94M | m_iBit = (UWORD8)((u4_bits >> maxLen_tmp) & 0x1); |
534 | | |
535 | | /* Get the next node pointer or the symbol from the tree */ |
536 | 3.94M | end = gai2_impeg2d_dct_dc_size[idx][end][m_iBit]; |
537 | 3.94M | }while(end > 0); |
538 | 1.43M | dc_size = end + MPEG2_DCT_DC_SIZE_OFFSET; |
539 | | |
540 | | /* Flush the appropriate number of bits from the stream */ |
541 | 1.43M | FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,(maxLen - maxLen_tmp),pu4_buf_aligned) |
542 | | |
543 | 1.43M | } |
544 | | |
545 | | |
546 | | |
547 | 1.43M | if (dc_size != 0) |
548 | 1.37M | { |
549 | 1.37M | UWORD32 u4_bits; |
550 | | |
551 | 1.37M | IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned, dc_size) |
552 | 1.37M | dc_diff = u4_bits; |
553 | | |
554 | 1.37M | if ((dc_diff & (1 << (dc_size - 1))) == 0) //v Probably the prediction algo? |
555 | 350k | dc_diff -= (1 << dc_size) - 1; |
556 | 1.37M | } |
557 | 66.5k | else |
558 | 66.5k | { |
559 | 66.5k | dc_diff = 0; |
560 | 66.5k | } |
561 | | |
562 | | |
563 | 1.43M | pi2_outAddr[*pi4_num_coeffs] = dc_diff; |
564 | | /* This indicates the position of the coefficient. Since this is the DC |
565 | | * coefficient, we put the position as 0. |
566 | | */ |
567 | 1.43M | pu1_pos[*pi4_num_coeffs] = pu1_scan[0]; |
568 | 1.43M | (*pi4_num_coeffs)++; |
569 | | |
570 | 1.43M | if (0 != dc_diff) |
571 | 1.36M | { |
572 | 1.36M | u4_nz_cols |= 0x01; |
573 | 1.36M | u4_nz_rows |= 0x01; |
574 | 1.36M | } |
575 | | |
576 | 1.43M | u4_numCoeffs = 1; |
577 | 1.43M | } |
578 | | /**************************************************************************/ |
579 | | /* Decoding of first AC coefficient in case of non Intra block */ |
580 | | /**************************************************************************/ |
581 | 5.61M | else |
582 | 5.61M | { |
583 | | /* First symbol can be 1s */ |
584 | 5.61M | UWORD32 u4_bits; |
585 | | |
586 | 5.61M | IBITS_NXT(u4_buf,u4_buf_nxt,u4_offset,u4_bits,1) |
587 | | |
588 | 5.61M | if(u4_bits == 1) |
589 | 3.55M | { |
590 | | |
591 | 3.55M | FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,1, pu4_buf_aligned) |
592 | 3.55M | IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned, 1) |
593 | 3.55M | if(u4_bits == 1) |
594 | 3.18M | { |
595 | 3.18M | pi2_outAddr[*pi4_num_coeffs] = -1; |
596 | 3.18M | } |
597 | 365k | else |
598 | 365k | { |
599 | 365k | pi2_outAddr[*pi4_num_coeffs] = 1; |
600 | 365k | } |
601 | | |
602 | | /* This indicates the position of the coefficient. Since this is the DC |
603 | | * coefficient, we put the position as 0. |
604 | | */ |
605 | 3.55M | pu1_pos[*pi4_num_coeffs] = pu1_scan[0]; |
606 | 3.55M | (*pi4_num_coeffs)++; |
607 | 3.55M | u4_numCoeffs = 1; |
608 | | |
609 | 3.55M | u4_nz_cols |= 0x01; |
610 | 3.55M | u4_nz_rows |= 0x01; |
611 | 3.55M | } |
612 | 2.05M | else |
613 | 2.05M | { |
614 | 2.05M | u4_numCoeffs = 0; |
615 | 2.05M | } |
616 | 5.61M | } |
617 | 7.05M | if (1 == u2_d_picture) |
618 | 0 | { |
619 | 0 | PUT_TEMP_STREAM_DATA(u4_buf, u4_buf_nxt, u4_offset, pu4_buf_aligned, ps_stream) |
620 | 0 | ps_dec->u4_non_zero_cols = u4_nz_cols; |
621 | 0 | ps_dec->u4_non_zero_rows = u4_nz_rows; |
622 | 0 | return ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE); |
623 | 0 | } |
624 | | |
625 | | |
626 | | |
627 | 7.05M | if (1 == u2_intra_vlc_format && u2_intra_flag) |
628 | 453k | { |
629 | | |
630 | 2.63M | while(1) |
631 | 2.62M | { |
632 | | //Putting the impeg2d_dec_ac_coeff_one function inline. |
633 | | |
634 | 2.62M | UWORD32 lead_zeros; |
635 | 2.62M | WORD16 DecodedValue; |
636 | | |
637 | 2.62M | u4_sym_len = 17; |
638 | 2.62M | IBITS_NXT(u4_buf,u4_buf_nxt,u4_offset,u4_bits,u4_sym_len) |
639 | | |
640 | | /* There cannot be more than 11 leading zeros in the decoded |
641 | | * symbol. The symbol is only 17 bits long, so we subtract 15. |
642 | | */ |
643 | 2.62M | lead_zeros = CLZ(u4_bits) - 15; |
644 | 2.62M | if (lead_zeros > 11) |
645 | 2.15k | { |
646 | 2.15k | return IMPEG2D_MB_DATA_DECODE_ERR; |
647 | 2.15k | } |
648 | | |
649 | 2.61M | DecodedValue = gau2_impeg2d_tab_one_1_9[u4_bits >> 8]; |
650 | 2.61M | u4_sym_len = (DecodedValue & 0xf); |
651 | 2.61M | i4_level = DecodedValue >> 9; |
652 | | /* One table lookup */ |
653 | 2.61M | if(0 != i4_level) |
654 | 1.75M | { |
655 | 1.75M | u4_run = ((DecodedValue >> 4) & 0x1f); |
656 | 1.75M | u4_numCoeffs += u4_run; |
657 | 1.75M | if (u4_numCoeffs >= NUM_COEFFS) |
658 | 492 | { |
659 | 492 | return IMPEG2D_MB_TEX_DECODE_ERR; |
660 | 492 | } |
661 | 1.75M | u4_pos = pu1_scan[u4_numCoeffs++]; |
662 | 1.75M | pu1_pos[*pi4_num_coeffs] = u4_pos; |
663 | | |
664 | 1.75M | FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned) |
665 | 1.75M | pi2_outAddr[*pi4_num_coeffs] = i4_level; |
666 | | |
667 | 1.75M | (*pi4_num_coeffs)++; |
668 | 1.75M | } |
669 | 859k | else |
670 | 859k | { |
671 | 859k | if (DecodedValue == END_OF_BLOCK_ONE) |
672 | 439k | { |
673 | 439k | u4_sym_len = 4; |
674 | | |
675 | 439k | break; |
676 | 439k | } |
677 | 419k | else |
678 | 419k | { |
679 | | /*Second table lookup*/ |
680 | 419k | lead_zeros = CLZ(u4_bits) - 20;/* -16 since we are dealing with WORD32 */ |
681 | 419k | if (0 != lead_zeros) |
682 | 16.6k | { |
683 | | |
684 | 16.6k | u4_bits = (u4_bits >> (6 - lead_zeros)) & 0x001F; |
685 | | |
686 | | /* Flush the number of bits */ |
687 | 16.6k | if (1 == lead_zeros) |
688 | 7.02k | { |
689 | 7.02k | u4_sym_len = ((u4_bits & 0x18) >> 3) == 2 ? 11:10; |
690 | 7.02k | } |
691 | 9.63k | else |
692 | 9.63k | { |
693 | 9.63k | u4_sym_len = 11 + lead_zeros; |
694 | 9.63k | } |
695 | | /* flushing */ |
696 | 16.6k | FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned) |
697 | | |
698 | | /* Calculate the address */ |
699 | 16.6k | u4_bits = ((lead_zeros - 1) << 5) + u4_bits; |
700 | | |
701 | 16.6k | DecodedValue = gau2_impeg2d_tab_one_10_16[u4_bits]; |
702 | | |
703 | 16.6k | u4_run = BITS(DecodedValue, 8,4); |
704 | 16.6k | i4_level = ((WORD16) DecodedValue) >> 9; |
705 | | |
706 | 16.6k | u4_numCoeffs += u4_run; |
707 | 16.6k | if (u4_numCoeffs >= NUM_COEFFS) |
708 | 202 | { |
709 | 202 | return IMPEG2D_MB_TEX_DECODE_ERR; |
710 | 202 | } |
711 | 16.4k | u4_pos = pu1_scan[u4_numCoeffs++]; |
712 | 16.4k | pu1_pos[*pi4_num_coeffs] = u4_pos; |
713 | 16.4k | pi2_outAddr[*pi4_num_coeffs] = i4_level; |
714 | 16.4k | (*pi4_num_coeffs)++; |
715 | 16.4k | } |
716 | | /*********************************************************************/ |
717 | | /* MPEG2 Escape Code */ |
718 | | /*********************************************************************/ |
719 | 402k | else if(u2_mpeg2 == 1) |
720 | 433k | { |
721 | 433k | u4_sym_len = 6; |
722 | 433k | FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned) |
723 | 433k | IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,18) |
724 | 433k | u4_decoded_value = u4_bits; |
725 | 433k | u4_run = (u4_decoded_value >> 12); |
726 | 433k | i4_level = (u4_decoded_value & 0x0FFF); |
727 | | |
728 | 433k | if (i4_level) |
729 | 229k | i4_level = (i4_level - ((i4_level & 0x0800) << 1)); |
730 | | |
731 | 433k | u4_numCoeffs += u4_run; |
732 | 433k | if (u4_numCoeffs >= NUM_COEFFS) |
733 | 383 | { |
734 | 383 | return IMPEG2D_MB_TEX_DECODE_ERR; |
735 | 383 | } |
736 | 433k | u4_pos = pu1_scan[u4_numCoeffs++]; |
737 | 433k | pu1_pos[*pi4_num_coeffs] = u4_pos; |
738 | 433k | pi2_outAddr[*pi4_num_coeffs] = i4_level; |
739 | 433k | (*pi4_num_coeffs)++; |
740 | 433k | } |
741 | | /*********************************************************************/ |
742 | | /* MPEG1 Escape Code */ |
743 | | /*********************************************************************/ |
744 | 18.4E | else |
745 | 18.4E | { |
746 | | /*----------------------------------------------------------- |
747 | | * MPEG-1 Stream |
748 | | * |
749 | | * <See D.9.3 of MPEG-2> Run-level escape syntax |
750 | | * Run-level values that cannot be coded with a VLC are coded |
751 | | * by the escape code '0000 01' followed by |
752 | | * either a 14-bit FLC (127 <= level <= 127), |
753 | | * or a 22-bit FLC (255 <= level <= 255). |
754 | | * This is described in Annex B,B.5f of MPEG-1.standard |
755 | | *-----------------------------------------------------------*/ |
756 | | |
757 | | /*----------------------------------------------------------- |
758 | | * First 6 bits are the value of the Run. Next is First 8 bits |
759 | | * of Level. These bits decide whether it is 14 bit FLC or |
760 | | * 22-bit FLC. |
761 | | * |
762 | | * If( first 8 bits of Level == '1000000' or '00000000') |
763 | | * then its is 22-bit FLC. |
764 | | * else |
765 | | * it is 14-bit FLC. |
766 | | *-----------------------------------------------------------*/ |
767 | 18.4E | u4_sym_len = 6; |
768 | 18.4E | FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned) |
769 | 18.4E | IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,14) |
770 | 18.4E | u4_decoded_value = u4_bits; |
771 | 18.4E | u4_run = (u4_decoded_value >> 8); |
772 | 18.4E | i4_level_first_byte = (u4_decoded_value & 0x0FF); |
773 | 18.4E | if(i4_level_first_byte & 0x7F) |
774 | 0 | { |
775 | | /*------------------------------------------------------- |
776 | | * First 8 bits of level are neither 1000000 nor 00000000 |
777 | | * Hence 14-bit FLC (Last 8 bits are used to get level) |
778 | | * |
779 | | * Level = (msb of Level_First_Byte is 1)? |
780 | | * Level_First_Byte - 256 : Level_First_Byte |
781 | | *-------------------------------------------------------*/ |
782 | 0 | i4_level = (i4_level_first_byte - |
783 | 0 | ((i4_level_first_byte & 0x80) << 1)); |
784 | 0 | } |
785 | 18.4E | else |
786 | 18.4E | { |
787 | | /*------------------------------------------------------- |
788 | | * Next 8 bits are either 1000000 or 00000000 |
789 | | * Hence 22-bit FLC (Last 16 bits are used to get level) |
790 | | * |
791 | | * Level = (msb of Level_First_Byte is 1)? |
792 | | * Level_Second_Byte - 256 : Level_Second_Byte |
793 | | *-------------------------------------------------------*/ |
794 | 18.4E | IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,8) |
795 | 18.4E | i4_level = u4_bits; |
796 | 18.4E | i4_level = (i4_level - (i4_level_first_byte << 1)); |
797 | 18.4E | } |
798 | 18.4E | u4_numCoeffs += u4_run; |
799 | 18.4E | if (u4_numCoeffs >= NUM_COEFFS) |
800 | 0 | { |
801 | 0 | return IMPEG2D_MB_TEX_DECODE_ERR; |
802 | 0 | } |
803 | | |
804 | 18.4E | u4_pos = pu1_scan[u4_numCoeffs++]; |
805 | | |
806 | 18.4E | pu1_pos[*pi4_num_coeffs] = u4_pos; |
807 | 18.4E | pi2_outAddr[*pi4_num_coeffs] = i4_level; |
808 | 18.4E | (*pi4_num_coeffs)++; |
809 | 18.4E | } |
810 | 419k | } |
811 | 859k | } |
812 | | |
813 | 2.17M | u4_nz_cols |= 1 << (u4_pos & 0x7); |
814 | 2.17M | u4_nz_rows |= 1 << (u4_pos >> 0x3); |
815 | | |
816 | 2.17M | } |
817 | 449k | IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,u4_sym_len) |
818 | 449k | } |
819 | 6.59M | else |
820 | 6.59M | { |
821 | | // Inline |
822 | 16.0M | while(1) |
823 | 15.8M | { |
824 | | |
825 | 15.8M | UWORD32 lead_zeros; |
826 | 15.8M | UWORD16 DecodedValue; |
827 | | |
828 | 15.8M | u4_sym_len = 17; |
829 | 15.8M | IBITS_NXT(u4_buf, u4_buf_nxt, u4_offset, u4_bits, u4_sym_len) |
830 | | |
831 | | /* There cannot be more than 11 leading zeros in the decoded |
832 | | * symbol. The symbol is only 17 bits long, so we subtract 15. |
833 | | */ |
834 | 15.8M | lead_zeros = CLZ(u4_bits) - 15; |
835 | 15.8M | if (lead_zeros > 11) |
836 | 8.05k | { |
837 | 8.05k | return IMPEG2D_MB_DATA_DECODE_ERR; |
838 | 8.05k | } |
839 | | |
840 | 15.8M | DecodedValue = gau2_impeg2d_tab_zero_1_9[u4_bits >> 8]; |
841 | 15.8M | u4_sym_len = BITS(DecodedValue, 3, 0); |
842 | 15.8M | i4_level = ((WORD16) DecodedValue) >> 9; |
843 | | |
844 | 15.8M | if (0 != i4_level) |
845 | 9.46M | { |
846 | 9.46M | u4_run = BITS(DecodedValue, 8,4); |
847 | | |
848 | 9.46M | u4_numCoeffs += u4_run; |
849 | 9.46M | if (u4_numCoeffs >= NUM_COEFFS) |
850 | 815 | { |
851 | 815 | return IMPEG2D_MB_TEX_DECODE_ERR; |
852 | 815 | } |
853 | | |
854 | 9.46M | u4_pos = pu1_scan[u4_numCoeffs++]; |
855 | 9.46M | pu1_pos[*pi4_num_coeffs] = u4_pos; |
856 | | |
857 | 9.46M | FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned) |
858 | 9.46M | pi2_outAddr[*pi4_num_coeffs] = i4_level; |
859 | 9.46M | (*pi4_num_coeffs)++; |
860 | 9.46M | } |
861 | 6.35M | else |
862 | 6.35M | { |
863 | 6.35M | if(DecodedValue == END_OF_BLOCK_ZERO) |
864 | 6.38M | { |
865 | 6.38M | u4_sym_len = 2; |
866 | | |
867 | 6.38M | break; |
868 | 6.38M | } |
869 | 18.4E | else |
870 | 18.4E | { |
871 | 18.4E | lead_zeros = CLZ(u4_bits) - 20;/* -15 since we are dealing with WORD32 */ |
872 | | /*Second table lookup*/ |
873 | 18.4E | if (0 != lead_zeros) |
874 | 40.0k | { |
875 | 40.0k | u4_bits = (u4_bits >> (6 - lead_zeros)) & 0x001F; |
876 | | |
877 | | /* Flush the number of bits */ |
878 | 40.0k | u4_sym_len = 11 + lead_zeros; |
879 | | |
880 | | /* Calculate the address */ |
881 | 40.0k | u4_bits = ((lead_zeros - 1) << 5) + u4_bits; |
882 | | |
883 | 40.0k | DecodedValue = gau2_impeg2d_tab_zero_10_16[u4_bits]; |
884 | | |
885 | 40.0k | u4_run = BITS(DecodedValue, 8,4); |
886 | 40.0k | i4_level = ((WORD16) DecodedValue) >> 9; |
887 | | |
888 | 40.0k | u4_numCoeffs += u4_run; |
889 | 40.0k | if (u4_numCoeffs >= NUM_COEFFS) |
890 | 307 | { |
891 | 307 | return IMPEG2D_MB_TEX_DECODE_ERR; |
892 | 307 | } |
893 | | |
894 | 39.7k | u4_pos = pu1_scan[u4_numCoeffs++]; |
895 | 39.7k | pu1_pos[*pi4_num_coeffs] = u4_pos; |
896 | 39.7k | if (1 == lead_zeros) |
897 | 5.42k | u4_sym_len--; |
898 | | /* flushing */ |
899 | 39.7k | FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned) |
900 | 39.7k | pi2_outAddr[*pi4_num_coeffs] = i4_level; |
901 | | |
902 | 39.7k | (*pi4_num_coeffs)++; |
903 | 39.7k | } |
904 | | /*Escape Sequence*/ |
905 | 18.4E | else if(u2_mpeg2 == 1) |
906 | 5.37k | { |
907 | 5.37k | u4_sym_len = 6; |
908 | 5.37k | FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned) |
909 | 5.37k | IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,18) |
910 | 5.37k | u4_decoded_value = u4_bits; |
911 | 5.37k | u4_run = (u4_decoded_value >> 12); |
912 | 5.37k | i4_level = (u4_decoded_value & 0x0FFF); |
913 | | |
914 | 5.37k | if (i4_level) |
915 | 3.49k | i4_level = (i4_level - ((i4_level & 0x0800) << 1)); |
916 | | |
917 | 5.37k | u4_numCoeffs += u4_run; |
918 | 5.37k | if (u4_numCoeffs >= NUM_COEFFS) |
919 | 330 | { |
920 | 330 | return IMPEG2D_MB_TEX_DECODE_ERR; |
921 | 330 | } |
922 | | |
923 | 5.04k | u4_pos = pu1_scan[u4_numCoeffs++]; |
924 | 5.04k | pu1_pos[*pi4_num_coeffs] = u4_pos; |
925 | 5.04k | pi2_outAddr[*pi4_num_coeffs] = i4_level; |
926 | | |
927 | 5.04k | (*pi4_num_coeffs)++; |
928 | 5.04k | } |
929 | | /*********************************************************************/ |
930 | | /* MPEG1 Escape Code */ |
931 | | /*********************************************************************/ |
932 | 18.4E | else |
933 | 18.4E | { |
934 | | /*----------------------------------------------------------- |
935 | | * MPEG-1 Stream |
936 | | * |
937 | | * <See D.9.3 of MPEG-2> Run-level escape syntax |
938 | | * Run-level values that cannot be coded with a VLC are coded |
939 | | * by the escape code '0000 01' followed by |
940 | | * either a 14-bit FLC (127 <= level <= 127), |
941 | | * or a 22-bit FLC (255 <= level <= 255). |
942 | | * This is described in Annex B,B.5f of MPEG-1.standard |
943 | | *-----------------------------------------------------------*/ |
944 | | |
945 | | /*----------------------------------------------------------- |
946 | | * First 6 bits are the value of the Run. Next is First 8 bits |
947 | | * of Level. These bits decide whether it is 14 bit FLC or |
948 | | * 22-bit FLC. |
949 | | * |
950 | | * If( first 8 bits of Level == '1000000' or '00000000') |
951 | | * then its is 22-bit FLC. |
952 | | * else |
953 | | * it is 14-bit FLC. |
954 | | *-----------------------------------------------------------*/ |
955 | 18.4E | u4_sym_len = 6; |
956 | 18.4E | FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,u4_sym_len,pu4_buf_aligned) |
957 | 18.4E | IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,14) |
958 | 18.4E | u4_decoded_value = u4_bits; |
959 | 18.4E | u4_run = (u4_decoded_value >> 8); |
960 | 18.4E | i4_level_first_byte = (u4_decoded_value & 0x0FF); |
961 | 18.4E | if(i4_level_first_byte & 0x7F) |
962 | 32.0k | { |
963 | | /*------------------------------------------------------- |
964 | | * First 8 bits of level are neither 1000000 nor 00000000 |
965 | | * Hence 14-bit FLC (Last 8 bits are used to get level) |
966 | | * |
967 | | * Level = (msb of Level_First_Byte is 1)? |
968 | | * Level_First_Byte - 256 : Level_First_Byte |
969 | | *-------------------------------------------------------*/ |
970 | 32.0k | i4_level = (i4_level_first_byte - |
971 | 32.0k | ((i4_level_first_byte & 0x80) << 1)); |
972 | 32.0k | } |
973 | 18.4E | else |
974 | 18.4E | { |
975 | | /*------------------------------------------------------- |
976 | | * Next 8 bits are either 1000000 or 00000000 |
977 | | * Hence 22-bit FLC (Last 16 bits are used to get level) |
978 | | * |
979 | | * Level = (msb of Level_First_Byte is 1)? |
980 | | * Level_Second_Byte - 256 : Level_Second_Byte |
981 | | *-------------------------------------------------------*/ |
982 | 18.4E | IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,8) |
983 | 18.4E | i4_level = u4_bits; |
984 | 18.4E | i4_level = (i4_level - (i4_level_first_byte << 1)); |
985 | 18.4E | } |
986 | 18.4E | u4_numCoeffs += u4_run; |
987 | 18.4E | if (u4_numCoeffs >= NUM_COEFFS) |
988 | 587 | { |
989 | 587 | return IMPEG2D_MB_TEX_DECODE_ERR; |
990 | 587 | } |
991 | | |
992 | 18.4E | u4_pos = pu1_scan[u4_numCoeffs++]; |
993 | 18.4E | pu1_pos[*pi4_num_coeffs] = u4_pos; |
994 | 18.4E | pi2_outAddr[*pi4_num_coeffs] = i4_level; |
995 | | |
996 | 18.4E | (*pi4_num_coeffs)++; |
997 | 18.4E | } |
998 | 18.4E | } |
999 | 6.35M | } |
1000 | | |
1001 | 9.43M | u4_nz_cols |= 1 << (u4_pos & 0x7); |
1002 | 9.43M | u4_nz_rows |= 1 << (u4_pos >> 0x3); |
1003 | | |
1004 | 9.43M | } |
1005 | | |
1006 | 6.58M | IBITS_GET(u4_buf,u4_buf_nxt,u4_offset,u4_bits,pu4_buf_aligned,u4_sym_len) |
1007 | | |
1008 | 6.58M | } |
1009 | | |
1010 | 7.03M | PUT_TEMP_STREAM_DATA(u4_buf, u4_buf_nxt, u4_offset, pu4_buf_aligned, ps_stream) |
1011 | | |
1012 | 7.03M | ps_dec->u4_non_zero_cols = u4_nz_cols; |
1013 | 7.03M | ps_dec->u4_non_zero_rows = u4_nz_rows; |
1014 | | |
1015 | 7.03M | return (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE; |
1016 | 7.05M | } |
1017 | | |
1018 | | |
1019 | | |
1020 | | /*****************************************************************************/ |
1021 | | /* */ |
1022 | | /* Function Name : impeg2d_inv_quant_mpeg1 */ |
1023 | | /* */ |
1024 | | /* Description : Inverse quantizes the output of VLD */ |
1025 | | /* */ |
1026 | | /* Inputs : */ |
1027 | | /* blk, - Block to be inverse quantized */ |
1028 | | /* weighting_matrix - Matrix to be used in inverse quant */ |
1029 | | /* intra_dc_precision- Precision reqd to scale intra DC value */ |
1030 | | /* quant_scale - Quanization scale for inverse quant */ |
1031 | | /* intra_flag - Intra or Not */ |
1032 | | /* */ |
1033 | | /* Globals : None */ |
1034 | | /* */ |
1035 | | /* Processing : Implements the inverse quantize equation */ |
1036 | | /* */ |
1037 | | /* Outputs : Inverse quantized values in the block */ |
1038 | | /* */ |
1039 | | /* Returns : None */ |
1040 | | /* */ |
1041 | | /* Issues : None */ |
1042 | | /* */ |
1043 | | /* Revision History: */ |
1044 | | /* */ |
1045 | | /* DD MM YYYY Author(s) Changes */ |
1046 | | /* 05 09 2005 Harish M First Version */ |
1047 | | /* */ |
1048 | | /*****************************************************************************/ |
1049 | | WORD32 impeg2d_inv_quant_mpeg1(WORD16 *pi2_blk, |
1050 | | UWORD8 *pu1_weighting_matrix, |
1051 | | UWORD8 u1_quant_scale, |
1052 | | WORD32 u4_intra_flag, |
1053 | | WORD32 i4_num_coeffs, |
1054 | | WORD16 *pi2_coeffs, |
1055 | | UWORD8 *pu1_pos, |
1056 | | const UWORD8 *pu1_scan, |
1057 | | UWORD16 *pu2_def_dc_pred, |
1058 | | UWORD16 u2_intra_dc_precision) |
1059 | 6.18M | { |
1060 | 6.18M | UWORD16 i4_pos; |
1061 | | |
1062 | 6.18M | WORD32 i4_iter; |
1063 | | |
1064 | | /* Inverse Quantize the predicted DC value for intra MB*/ |
1065 | 6.18M | if(u4_intra_flag == 1) |
1066 | 911k | { |
1067 | | /**************************************************************************/ |
1068 | | /* Decode the DC coefficient in case of Intra block and also update */ |
1069 | | /* DC predictor value of the corresponding color component */ |
1070 | | /**************************************************************************/ |
1071 | 911k | { |
1072 | 911k | pi2_coeffs[0] += *pu2_def_dc_pred; |
1073 | 911k | *pu2_def_dc_pred = pi2_coeffs[0]; |
1074 | 911k | pi2_coeffs[0] <<= (3 - u2_intra_dc_precision); |
1075 | 911k | pi2_coeffs[0] = CLIP_S12(pi2_coeffs[0]); |
1076 | 911k | } |
1077 | | |
1078 | 911k | pi2_blk[pu1_scan[0]] = pi2_coeffs[0]; |
1079 | 911k | } |
1080 | | /************************************************************************/ |
1081 | | /* Inverse quantization of other DCT coefficients */ |
1082 | | /************************************************************************/ |
1083 | 18.6M | for(i4_iter = u4_intra_flag; i4_iter < i4_num_coeffs; i4_iter++) |
1084 | 12.4M | { |
1085 | | |
1086 | 12.4M | WORD16 sign; |
1087 | 12.4M | WORD32 temp, temp1; |
1088 | | |
1089 | | /* Position is the inverse scan of the index stored */ |
1090 | 12.4M | i4_pos = pu1_pos[i4_iter]; |
1091 | 12.4M | pi2_blk[i4_pos] = pi2_coeffs[i4_iter]; |
1092 | | |
1093 | 12.4M | sign = SIGN(pi2_blk[i4_pos]); |
1094 | 12.4M | temp = ABS(pi2_blk[i4_pos] << 1); |
1095 | | |
1096 | | /* pi2_coeffs has only non-zero elements. So no need to check |
1097 | | * if the coeff is non-zero. |
1098 | | */ |
1099 | 12.4M | temp = temp + (1 * !u4_intra_flag); |
1100 | | |
1101 | 12.4M | temp = temp * pu1_weighting_matrix[i4_pos] * u1_quant_scale; |
1102 | | |
1103 | 12.4M | temp = temp >> 5; |
1104 | | |
1105 | 12.4M | temp1 = temp | 1; |
1106 | | |
1107 | 12.4M | temp1 = (temp1 > temp) ? (temp1 - temp) : (temp - temp1); |
1108 | | |
1109 | 12.4M | temp = temp - temp1; |
1110 | | |
1111 | 12.4M | if(temp < 0) |
1112 | 3.40M | { |
1113 | 3.40M | temp = 0; |
1114 | 3.40M | } |
1115 | | |
1116 | 12.4M | temp = temp * sign; |
1117 | | |
1118 | 12.4M | temp = CLIP_S12(temp); |
1119 | | |
1120 | 12.4M | pi2_blk[i4_pos] = temp; |
1121 | 12.4M | } |
1122 | | |
1123 | | /*return value is used in the case of mpeg2 for mismatch control*/ |
1124 | 6.18M | return (0); |
1125 | 6.18M | } /* End of inv_quant() */ |
1126 | | |
1127 | | |
1128 | | |
1129 | | /*****************************************************************************/ |
1130 | | /* */ |
1131 | | /* Function Name : impeg2d_inv_quant_mpeg2 */ |
1132 | | /* */ |
1133 | | /* Description : Inverse quantizes the output of VLD */ |
1134 | | /* */ |
1135 | | /* Inputs : */ |
1136 | | /* blk, - Block to be inverse quantized */ |
1137 | | /* weighting_matrix - Matrix to be used in inverse quant */ |
1138 | | /* intra_dc_precision- Precision reqd to scale intra DC value */ |
1139 | | /* quant_scale - Quanization scale for inverse quant */ |
1140 | | /* intra_flag - Intra or Not */ |
1141 | | /* */ |
1142 | | /* Globals : None */ |
1143 | | /* */ |
1144 | | /* Processing : Implements the inverse quantize equation */ |
1145 | | /* */ |
1146 | | /* Outputs : Inverse quantized values in the block */ |
1147 | | /* */ |
1148 | | /* Returns : None */ |
1149 | | /* */ |
1150 | | /* Issues : None */ |
1151 | | /* */ |
1152 | | /* Revision History: */ |
1153 | | /* */ |
1154 | | /* DD MM YYYY Author(s) Changes */ |
1155 | | /* 05 09 2005 Harish M First Version */ |
1156 | | /* */ |
1157 | | /*****************************************************************************/ |
1158 | | WORD32 impeg2d_inv_quant_mpeg2(WORD16 *pi2_blk, |
1159 | | UWORD8 *pu1_weighting_matrix, |
1160 | | UWORD8 u1_quant_scale, |
1161 | | WORD32 u4_intra_flag, |
1162 | | WORD32 i4_num_coeffs, |
1163 | | WORD16 *pi2_coeffs, |
1164 | | UWORD8 *pu1_pos, |
1165 | | const UWORD8 *pu1_scan, |
1166 | | UWORD16 *pu2_def_dc_pred, |
1167 | | UWORD16 u2_intra_dc_precision) |
1168 | 653k | { |
1169 | | |
1170 | 653k | WORD32 i4_pos; |
1171 | | /* Used for Mismatch control */ |
1172 | 653k | WORD32 sum; |
1173 | | |
1174 | 653k | WORD32 i4_iter; |
1175 | | |
1176 | 653k | sum = 0; |
1177 | | |
1178 | | /* Inverse Quantize the predicted DC value for intra MB*/ |
1179 | 653k | if(u4_intra_flag == 1) |
1180 | 472k | { |
1181 | | /**************************************************************************/ |
1182 | | /* Decode the DC coefficient in case of Intra block and also update */ |
1183 | | /* DC predictor value of the corresponding color component */ |
1184 | | /**************************************************************************/ |
1185 | 472k | { |
1186 | 472k | pi2_coeffs[0] += *pu2_def_dc_pred; |
1187 | 472k | *pu2_def_dc_pred = pi2_coeffs[0]; |
1188 | 472k | pi2_coeffs[0] <<= (3 - u2_intra_dc_precision); |
1189 | 472k | pi2_coeffs[0] = CLIP_S12(pi2_coeffs[0]); |
1190 | 472k | } |
1191 | | |
1192 | 472k | pi2_blk[pu1_scan[0]] = pi2_coeffs[0]; |
1193 | 472k | sum = pi2_blk[0]; |
1194 | 472k | } |
1195 | | |
1196 | | /************************************************************************/ |
1197 | | /* Inverse quantization of other DCT coefficients */ |
1198 | | /************************************************************************/ |
1199 | 3.19M | for(i4_iter = u4_intra_flag; i4_iter < i4_num_coeffs; i4_iter++) |
1200 | 2.53M | { |
1201 | 2.53M | WORD16 sign; |
1202 | 2.53M | WORD32 temp; |
1203 | | /* Position is the inverse scan of the index stored */ |
1204 | 2.53M | i4_pos = pu1_pos[i4_iter]; |
1205 | 2.53M | pi2_blk[i4_pos] = pi2_coeffs[i4_iter]; |
1206 | | |
1207 | 2.53M | sign = SIGN(pi2_blk[i4_pos]); |
1208 | 2.53M | temp = ABS(pi2_blk[i4_pos] << 1); |
1209 | 2.53M | temp = temp + (1 * !u4_intra_flag); |
1210 | 2.53M | temp = temp * pu1_weighting_matrix[i4_pos] * u1_quant_scale; |
1211 | | |
1212 | 2.53M | temp = temp >> 5; |
1213 | | |
1214 | 2.53M | temp = temp * sign; |
1215 | | |
1216 | 2.53M | temp = CLIP_S12(temp); |
1217 | | |
1218 | 2.53M | pi2_blk[i4_pos] = temp; |
1219 | | |
1220 | 2.53M | sum += temp; |
1221 | 2.53M | } |
1222 | 653k | return (sum ^ 1); |
1223 | 653k | } /* End of inv_quant() */ |