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 <stdio.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_globals.h"
41 #include "impeg2d_vld_tables.h"
42 #include "impeg2d_pic_proc.h"
43 #include "impeg2d_debug.h"
44 #include "impeg2d_mv_dec.h"
45 #include "impeg2d_mc.h"
46
47 /*******************************************************************************
48 * Function name : impeg2d_dec_1mv
49 *
50 * Description : Decodes a motion vector and updates the predictors
51 *
52 * Arguments :
53 * stream : Bitstream
54 * predMv : Prediction for the motion vectors
55 * mv : Motion vectors
56 * fCode : fcode to the used for the decoding
57 * shift : Shift value to be used. This will be equal to
58 * (mv_format == "field") && (picture_structure == "Frame picture")
59 * i : 0 - MV_X and 1 - MV_Y
60 *
61 * Value Returned: None
62 *******************************************************************************/
impeg2d_dec_1mv(stream_t * ps_stream,WORD16 ai2_pred_mv[],WORD16 ai2_mv[],UWORD16 au2_fCode[],UWORD16 u2_mv_y_shift,WORD16 ai2_dmv[])63 INLINE void impeg2d_dec_1mv(stream_t *ps_stream, WORD16 ai2_pred_mv[], WORD16 ai2_mv[],UWORD16 au2_fCode[],
64 UWORD16 u2_mv_y_shift, WORD16 ai2_dmv[])
65 {
66 WORD16 i2_f;
67 WORD16 i2_r_size;
68 WORD16 i2_high,i2_low,i2_range;
69 UWORD32 u4_mv_code;
70 WORD16 i2_delta;
71 UWORD16 u2_first_bit;
72 WORD32 i;
73 WORD32 ai2_shifts[2];
74 UWORD32 u4_buf;
75 UWORD32 u4_buf_nxt;
76 UWORD32 u4_offset;
77 UWORD32 *pu4_buf_aligned;
78
79 ai2_shifts[0] = 0;
80 ai2_shifts[1] = u2_mv_y_shift;
81
82
83 GET_TEMP_STREAM_DATA(u4_buf,u4_buf_nxt,u4_offset,pu4_buf_aligned,ps_stream)
84 for(i = 0; i < 2; i++)
85 {
86 WORD32 i4_shift = ai2_shifts[i];
87 /* Decode the motion_code */
88 IBITS_NXT(u4_buf, u4_buf_nxt, u4_offset, u4_mv_code, MV_CODE_LEN)
89 u2_first_bit = (u4_mv_code >> (MV_CODE_LEN - 1)) & 0x01;
90 if(u2_first_bit == 1) /* mvCode == 0 */
91 {
92 i2_delta = 0;
93 FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,1,pu4_buf_aligned)
94
95 ai2_mv[i] = (ai2_pred_mv[i] >> i4_shift);
96
97 ai2_pred_mv[i] = (ai2_mv[i] << i4_shift);
98
99 }
100 else
101 {
102 UWORD16 u2_index;
103 UWORD16 u2_value;
104 UWORD16 u2_mv_len;
105 UWORD16 u2_abs_mvcode_minus1;
106 UWORD16 u2_sign_bit;
107
108 i2_r_size = au2_fCode[i] - 1;
109 i2_f = 1 << i2_r_size;
110 i2_high = (16 * i2_f) - 1;
111 i2_low = ((-16) * i2_f);
112 i2_range = (32 * i2_f);
113
114 u2_index = (u4_mv_code >> 1) & 0x1FF;
115 u2_value = gau2_impeg2d_mv_code[u2_index];
116 u2_mv_len = (u2_value & 0x0F);
117 u2_abs_mvcode_minus1 = (u2_value >> 8) & 0x0FF;
118 u4_mv_code >>= (MV_CODE_LEN - u2_mv_len - 1);
119 u2_sign_bit = u4_mv_code & 0x1;
120
121 FLUSH_BITS(u4_offset,u4_buf,u4_buf_nxt,(u2_mv_len + 1),pu4_buf_aligned)
122 i2_delta = u2_abs_mvcode_minus1 * i2_f + 1;
123 if(i2_r_size)
124 {
125 UWORD32 val;
126 IBITS_GET(u4_buf, u4_buf_nxt, u4_offset, val, pu4_buf_aligned, i2_r_size)
127 i2_delta += val;
128 }
129
130 if(u2_sign_bit)
131 i2_delta = -i2_delta;
132
133 ai2_mv[i] = (ai2_pred_mv[i] >> i4_shift) + i2_delta;
134
135 if(ai2_mv[i] < i2_low)
136 {
137 ai2_mv[i] += i2_range;
138 }
139
140 if(ai2_mv[i] > i2_high)
141 {
142 ai2_mv[i] -= i2_range;
143 }
144 ai2_pred_mv[i] = (ai2_mv[i] << i4_shift);
145
146 }
147 if(ai2_dmv)
148 {
149 UWORD32 u4_val;
150 ai2_dmv[i] = 0;
151 IBITS_GET(u4_buf, u4_buf_nxt, u4_offset, u4_val, pu4_buf_aligned, 1)
152 if(u4_val)
153 {
154 IBITS_GET(u4_buf, u4_buf_nxt, u4_offset, u4_val, pu4_buf_aligned, 1)
155 ai2_dmv[i] = gai2_impeg2d_dec_mv[u4_val];
156 }
157 }
158 }
159 PUT_TEMP_STREAM_DATA(u4_buf, u4_buf_nxt, u4_offset, pu4_buf_aligned, ps_stream)
160
161 }
162 /*******************************************************************************
163 * Function name : impeg2d_dec_mv
164 *
165 * Description : Decodes a motion vector and updates the predictors
166 *
167 * Arguments :
168 * stream : Bitstream
169 * predMv : Prediction for the motion vectors
170 * mv : Motion vectors
171 * fCode : fcode to the used for the decoding
172 * shift : Shift value to be used. This will be equal to
173 * (mv_format == "field") && (picture_structure == "Frame picture")
174 *
175 * Value Returned: None
176 *******************************************************************************/
impeg2d_dec_mv(stream_t * ps_stream,WORD16 ai2_pred_mv[],WORD16 ai2_mv[],UWORD16 au2_f_code[],UWORD16 u2_shift,UWORD16 u2_fld_sel)177 e_field_t impeg2d_dec_mv(stream_t *ps_stream, WORD16 ai2_pred_mv[], WORD16 ai2_mv[],UWORD16 au2_f_code[],
178 UWORD16 u2_shift, UWORD16 u2_fld_sel)
179 {
180 e_field_t e_fld;
181 if(u2_fld_sel)
182 {
183 e_fld = (e_field_t)impeg2d_bit_stream_get_bit(ps_stream);
184 }
185 else
186 {
187 e_fld = TOP;
188 }
189
190 impeg2d_dec_1mv(ps_stream,ai2_pred_mv,ai2_mv,au2_f_code,u2_shift,NULL);
191
192 return(e_fld);
193 }
194
195 /*****************************************************************************
196 * Function Name : impeg2d_dec_1mv_mb
197 *
198 * Description : Decodes mc params for 1 MV MB
199 *
200 * Arguments :
201 * dec : Decoder state
202 *
203 * Values Returned : None
204 *****************************************************************************/
impeg2d_dec_1mv_mb(dec_state_t * ps_dec)205 void impeg2d_dec_1mv_mb(dec_state_t *ps_dec)
206 {
207 stream_t *ps_stream;
208 WORD16 *pi2_mv;
209 e_field_t e_fld;
210 mb_mc_params_t *ps_mc;
211 e_pred_direction_t e_ref_pic;
212
213
214 ps_stream = &ps_dec->s_bit_stream;
215 e_ref_pic = ps_dec->e_mb_pred;
216 /************************************************************************/
217 /* Decode the motion vector */
218 /************************************************************************/
219 pi2_mv = (WORD16 *)&ps_dec->ai2_mv[FORW][FIRST];
220 e_fld = impeg2d_dec_mv(ps_stream,ps_dec->ai2_pred_mv[e_ref_pic][FIRST],pi2_mv,
221 ps_dec->au2_f_code[e_ref_pic],0, ps_dec->u2_fld_pic);
222
223 ps_dec->ai2_pred_mv[e_ref_pic][SECOND][MV_X] = ps_dec->ai2_pred_mv[e_ref_pic][FIRST][MV_X];
224 ps_dec->ai2_pred_mv[e_ref_pic][SECOND][MV_Y] = ps_dec->ai2_pred_mv[e_ref_pic][FIRST][MV_Y];
225 /************************************************************************/
226 /* Set the motion vector params */
227 /************************************************************************/
228 ps_mc = &ps_dec->as_mb_mc_params[e_ref_pic][FIRST];
229 ps_mc->s_ref = ps_dec->as_ref_buf[e_ref_pic][e_fld];
230 impeg2d_set_mc_params(&ps_mc->s_luma, &ps_mc->s_chroma, ps_dec->s_mb_type, 0,
231 pi2_mv, ps_dec->u2_mb_x, ps_dec->u2_mb_y, ps_dec->u2_frame_width, ps_dec->u2_frame_height,ps_dec->u2_picture_width);
232
233 }
234
235 /*****************************************************************************
236 * Function Name : impeg2d_dec_2mv_fw_or_bk_mb
237 *
238 * Description : Decodes first part of params for 2 MV Interpolated MB
239 *
240 * Arguments :
241 * dec : Decoder state
242 *
243 * Values Returned : None
244 *****************************************************************************/
impeg2d_dec_2mv_fw_or_bk_mb(dec_state_t * ps_dec)245 void impeg2d_dec_2mv_fw_or_bk_mb(dec_state_t *ps_dec)
246 {
247 stream_t *ps_stream;
248 WORD16 *pi2_mv;
249 e_field_t e_fld;
250 mb_mc_params_t *ps_mc;
251 e_pred_direction_t e_ref_pic;
252 UWORD16 i;
253
254 ps_stream = &ps_dec->s_bit_stream;
255 e_ref_pic = ps_dec->e_mb_pred;
256 for(i = 0; i < 2; i++)
257 {
258 /********************************************************************/
259 /* Decode the first motion vector */
260 /********************************************************************/
261 pi2_mv = (WORD16 *)&ps_dec->ai2_mv[FORW][i];
262 e_fld = impeg2d_dec_mv(ps_stream,ps_dec->ai2_pred_mv[e_ref_pic][i],pi2_mv,
263 ps_dec->au2_f_code[e_ref_pic],ps_dec->u2_frm_pic, 1);
264
265 /********************************************************************/
266 /* Set the motion vector params */
267 /********************************************************************/
268 ps_mc = &ps_dec->as_mb_mc_params[FORW][i];
269 ps_mc->s_ref = ps_dec->as_ref_buf[e_ref_pic][e_fld];
270 impeg2d_set_mc_params(&ps_mc->s_luma, &ps_mc->s_chroma, ps_dec->s_mb_type, i,
271 pi2_mv, ps_dec->u2_mb_x, ps_dec->u2_mb_y, ps_dec->u2_frame_width, ps_dec->u2_frame_height,ps_dec->u2_picture_width);
272 }
273 }
274
275 /*****************************************************************************
276 * Function Name : impeg2d_dec_frm_dual_prime
277 *
278 * Description : Decodes first part of params for 2 MV Interpolated MB
279 *
280 * Arguments :
281 * dec : Decoder state
282 *
283 * Values Returned : None
284 *****************************************************************************/
impeg2d_dec_frm_dual_prime(dec_state_t * ps_dec)285 void impeg2d_dec_frm_dual_prime(dec_state_t *ps_dec)
286 {
287 stream_t *ps_stream;
288 WORD16 *pi2_mv;
289 mb_mc_params_t *ps_mc;
290
291 WORD16 ai2_dmv[2];
292 WORD16 *pi2_mv1, *pi2_mv2, *pi2_mv3, *pi2_mv4;
293 UWORD16 i,j;
294
295 pi2_mv1 = (WORD16 *)&(ps_dec->ai2_mv[FORW][FIRST]);
296 pi2_mv2 = (WORD16 *)&(ps_dec->ai2_mv[FORW][SECOND]);
297 pi2_mv3 = (WORD16 *)&(ps_dec->ai2_mv[BACK][FIRST]);
298 pi2_mv4 = (WORD16 *)&(ps_dec->ai2_mv[BACK][SECOND]);
299
300
301
302 ps_stream = &ps_dec->s_bit_stream;
303
304 /************************************************************************/
305 /* Decode the motion vector MV_X, MV_Y and dmv[0], dmv[1] */
306 /************************************************************************/
307 impeg2d_dec_1mv(ps_stream,ps_dec->ai2_pred_mv[FORW][FIRST],pi2_mv1,ps_dec->au2_f_code[FORW],ps_dec->u2_frm_pic,ai2_dmv);
308
309 {
310 WORD16 ai2_m[2][2];
311
312 if(ps_dec->u2_top_field_first)
313 {
314 ai2_m[1][0] = 1;
315 ai2_m[0][1] = 3;
316 }
317 else
318 {
319 ai2_m[1][0] = 3;
320 ai2_m[0][1] = 1;
321 }
322
323 pi2_mv2[MV_X] = pi2_mv1[MV_X];
324 pi2_mv2[MV_Y] = pi2_mv1[MV_Y];
325
326 pi2_mv3[MV_X] = ai2_dmv[0] + DIV_2_RND(pi2_mv1[MV_X] * ai2_m[1][0]);
327 pi2_mv4[MV_X] = ai2_dmv[0] + DIV_2_RND(pi2_mv1[MV_X] * ai2_m[0][1]);
328
329 pi2_mv3[MV_Y] = ai2_dmv[1] + DIV_2_RND(pi2_mv1[MV_Y] * ai2_m[1][0]) - 1;
330 pi2_mv4[MV_Y] = ai2_dmv[1] + DIV_2_RND(pi2_mv1[MV_Y] * ai2_m[0][1]) + 1;
331 }
332
333 ps_dec->ai2_pred_mv[FORW][SECOND][MV_X] = ps_dec->ai2_pred_mv[FORW][FIRST][MV_X];
334 ps_dec->ai2_pred_mv[FORW][SECOND][MV_Y] = ps_dec->ai2_pred_mv[FORW][FIRST][MV_Y];
335
336 /************************************************************************/
337 /* Set the motion vector params */
338 /************************************************************************/
339 for(j = 0; j < 2; j++)
340 {
341 for(i = 0; i < 2; i++)
342 {
343 pi2_mv = (WORD16 *)&ps_dec->ai2_mv[j][i];
344 ps_mc = &ps_dec->as_mb_mc_params[j][i];
345 ps_mc->s_ref = ps_dec->as_ref_buf[FORW][(i ^ j) & 1];
346 impeg2d_set_mc_params(&ps_mc->s_luma, &ps_mc->s_chroma, ps_dec->s_mb_type, i,
347 pi2_mv, ps_dec->u2_mb_x, ps_dec->u2_mb_y, ps_dec->u2_frame_width, ps_dec->u2_frame_height,ps_dec->u2_picture_width);
348 }
349 }
350
351 }
352 /*****************************************************************************
353 * Function Name : impeg2d_dec_fld_dual_prime
354 *
355 * Description : Decodes first part of params for 2 MV Interpolated MB
356 *
357 * Arguments :
358 * dec : Decoder state
359 *
360 * Values Returned : None
361 *****************************************************************************/
impeg2d_dec_fld_dual_prime(dec_state_t * ps_dec)362 void impeg2d_dec_fld_dual_prime(dec_state_t *ps_dec)
363 {
364 stream_t *ps_stream;
365 WORD16 *pi2_mv;
366 mb_mc_params_t *ps_mc;
367
368 WORD16 *pi2_mv1, *pi2_mv2;
369 WORD16 ai2_dmv[2];
370
371
372 pi2_mv1 = (WORD16 *)&(ps_dec->ai2_mv[FORW][FIRST]);
373 pi2_mv2 = (WORD16 *)&(ps_dec->ai2_mv[FORW][SECOND]);
374 ps_stream = &ps_dec->s_bit_stream;
375
376 /************************************************************************/
377 /* Decode the motion vector MV_X, MV_Y and dmv[0], dmv[1] */
378 /************************************************************************/
379 impeg2d_dec_1mv(ps_stream,ps_dec->ai2_pred_mv[FORW][FIRST],pi2_mv1,ps_dec->au2_f_code[FORW],0,ai2_dmv);
380
381
382 pi2_mv2[MV_X] = ai2_dmv[0] + DIV_2_RND(pi2_mv1[MV_X]);
383 pi2_mv2[MV_Y] = ai2_dmv[1] + DIV_2_RND(pi2_mv1[MV_Y]);
384
385 if(ps_dec->u2_picture_structure == TOP_FIELD)
386 pi2_mv2[MV_Y] -= 1;
387 else
388 pi2_mv2[MV_Y] += 1;
389
390 ps_dec->ai2_pred_mv[FORW][SECOND][MV_X] = ps_dec->ai2_pred_mv[FORW][FIRST][MV_X];
391 ps_dec->ai2_pred_mv[FORW][SECOND][MV_Y] = ps_dec->ai2_pred_mv[FORW][FIRST][MV_Y];
392
393 /************************************************************************/
394 /* Set the motion vector params */
395 /************************************************************************/
396 pi2_mv = (WORD16 *)&ps_dec->ai2_mv[FORW][0];
397 ps_mc = &ps_dec->as_mb_mc_params[FORW][0];
398 ps_mc->s_ref = ps_dec->as_ref_buf[FORW][ps_dec->u2_fld_parity];
399 impeg2d_set_mc_params(&ps_mc->s_luma, &ps_mc->s_chroma, ps_dec->s_mb_type, 0,
400 pi2_mv, ps_dec->u2_mb_x, ps_dec->u2_mb_y, ps_dec->u2_frame_width, ps_dec->u2_frame_height,ps_dec->u2_picture_width);
401
402 pi2_mv = (WORD16 *)&ps_dec->ai2_mv[FORW][1];
403 ps_mc = &ps_dec->as_mb_mc_params[FORW][1];
404 ps_mc->s_ref = ps_dec->as_ref_buf[FORW][!ps_dec->u2_fld_parity];
405 impeg2d_set_mc_params(&ps_mc->s_luma, &ps_mc->s_chroma, ps_dec->s_mb_type, 0,
406 pi2_mv, ps_dec->u2_mb_x, ps_dec->u2_mb_y, ps_dec->u2_frame_width, ps_dec->u2_frame_height,ps_dec->u2_picture_width);
407
408
409 }
410 /*****************************************************************************
411 * Function Name : impeg2d_dec_4mv_mb
412 *
413 * Description : Decodes first part of params for 2 MV Interpolated MB
414 *
415 * Arguments :
416 * dec : Decoder state
417 *
418 * Values Returned : None
419 *****************************************************************************/
impeg2d_dec_4mv_mb(dec_state_t * ps_dec)420 void impeg2d_dec_4mv_mb(dec_state_t *ps_dec)
421 {
422 stream_t *ps_stream;
423 WORD16 *pi2_mv;
424 e_field_t e_fld;
425 mb_mc_params_t *ps_mc;
426
427 UWORD16 i,j;
428
429 ps_stream = &ps_dec->s_bit_stream;
430
431 /***********************************************/
432 /* loop for FW & BK */
433 /***********************************************/
434 for(j = 0; j < 2; j++)
435 {
436 /***********************************************/
437 /* loop for decoding 2 mvs of same reference frame*/
438 /***********************************************/
439 for(i = 0; i < 2; i++)
440 {
441 /****************************************************************/
442 /* Decode the first motion vector */
443 /****************************************************************/
444 pi2_mv = (WORD16 *)&ps_dec->ai2_mv[j][i];
445 e_fld = impeg2d_dec_mv(ps_stream,ps_dec->ai2_pred_mv[j][i],pi2_mv,
446 ps_dec->au2_f_code[j],ps_dec->u2_frm_pic, 1);
447
448 /****************************************************************/
449 /* Set the motion vector params */
450 /****************************************************************/
451 ps_mc = &ps_dec->as_mb_mc_params[j][i];
452 ps_mc->s_ref = ps_dec->as_ref_buf[j][e_fld];
453 impeg2d_set_mc_params(&ps_mc->s_luma, &ps_mc->s_chroma, ps_dec->s_mb_type, i,
454 pi2_mv, ps_dec->u2_mb_x, ps_dec->u2_mb_y, ps_dec->u2_frame_width, ps_dec->u2_frame_height,ps_dec->u2_picture_width);
455 }
456 }
457
458 }
459 /*******************************************************************************
460 * Function Name : impeg2d_dec_2mv_interp_mb
461 *
462 * Description : Decodes first part of params for 2 MV Interpolated MB
463 *
464 * Arguments :
465 * dec : Decoder state
466 *
467 * Values Returned : None
468 *******************************************************************************/
impeg2d_dec_2mv_interp_mb(dec_state_t * ps_dec)469 void impeg2d_dec_2mv_interp_mb(dec_state_t *ps_dec)
470 {
471 stream_t *ps_stream;
472 WORD16 *pi2_mv;
473 e_field_t e_fld;
474 mb_mc_params_t *ps_mc;
475 UWORD16 i;
476
477 ps_stream = &ps_dec->s_bit_stream;
478
479 for(i = 0; i < 2; i++)
480 {
481 /********************************************************************/
482 /* Decode the first motion vector */
483 /********************************************************************/
484 pi2_mv = (WORD16 *)&ps_dec->ai2_mv[i][FIRST];
485 e_fld = impeg2d_dec_mv(ps_stream,ps_dec->ai2_pred_mv[i][FIRST],pi2_mv,
486 ps_dec->au2_f_code[i],0, ps_dec->u2_fld_pic);
487
488 ps_dec->ai2_pred_mv[i][SECOND][MV_X] = ps_dec->ai2_pred_mv[i][FIRST][MV_X];
489 ps_dec->ai2_pred_mv[i][SECOND][MV_Y] = ps_dec->ai2_pred_mv[i][FIRST][MV_Y];
490 /********************************************************************/
491 /* Set the motion vector params */
492 /********************************************************************/
493 ps_mc = &ps_dec->as_mb_mc_params[i][FIRST];
494 ps_mc->s_ref = ps_dec->as_ref_buf[i][e_fld];
495 impeg2d_set_mc_params(&ps_mc->s_luma, &ps_mc->s_chroma, ps_dec->s_mb_type,i,
496 pi2_mv, ps_dec->u2_mb_x, ps_dec->u2_mb_y, ps_dec->u2_frame_width, ps_dec->u2_frame_height,ps_dec->u2_picture_width);
497 }
498
499 }
500