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