• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef __ANDROID__
22 #include <cutils/log.h>
23 #endif
24 #include "iv_datatypedef.h"
25 #include "iv.h"
26 #include "ivd.h"
27 #include "impeg2_macros.h"
28 #include "impeg2_buf_mgr.h"
29 #include "impeg2_disp_mgr.h"
30 #include "impeg2_defs.h"
31 #include "impeg2_inter_pred.h"
32 #include "impeg2_idct.h"
33 #include "impeg2_format_conv.h"
34 #include "impeg2_mem_func.h"
35 #include "impeg2_platform_macros.h"
36 #include "ithread.h"
37 #include "impeg2_job_queue.h"
38 
39 #include "impeg2d.h"
40 #include "impeg2d_bitstream.h"
41 #include "impeg2d_api.h"
42 #include "impeg2d_structs.h"
43 #include "impeg2_globals.h"
44 #include "impeg2d_pic_proc.h"
45 #include "impeg2d_deinterlace.h"
46 
47 
48 /*****************************************************************************
49 * MPEG2 Constants for Parse Check
50 ******************************************************************************/
51 #define MPEG2_MAX_FRAME_RATE_CODE   8
52 
53 /******************************************************************************
54 *  Function Name   : impeg2d_next_start_code
55 *
56 *  Description     : Peek for next_start_code from the stream_t.
57 *
58 *  Arguments       :
59 *  dec             : Decoder Context
60 *
61 *  Values Returned : None
62 ******************************************************************************/
impeg2d_next_start_code(dec_state_t * ps_dec)63 void impeg2d_next_start_code(dec_state_t *ps_dec)
64 {
65     stream_t *ps_stream;
66     ps_stream = &ps_dec->s_bit_stream;
67     impeg2d_bit_stream_flush_to_byte_boundary(ps_stream);
68 
69     while ((impeg2d_bit_stream_nxt(ps_stream,START_CODE_PREFIX_LEN) != START_CODE_PREFIX)
70         && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
71     {
72         impeg2d_bit_stream_get(ps_stream,8);
73     }
74     return;
75 }
76 /******************************************************************************
77 *  Function Name   : impeg2d_next_code
78 *
79 *  Description     : Peek for next_start_code from the stream_t.
80 *
81 *  Arguments       :
82 *  dec             : Decoder Context
83 *
84 *  Values Returned : None
85 ******************************************************************************/
impeg2d_next_code(dec_state_t * ps_dec,UWORD32 u4_start_code_val)86 void impeg2d_next_code(dec_state_t *ps_dec, UWORD32 u4_start_code_val)
87 {
88     stream_t *ps_stream;
89     ps_stream = &ps_dec->s_bit_stream;
90     impeg2d_bit_stream_flush_to_byte_boundary(ps_stream);
91 
92     while ((impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) != u4_start_code_val) &&
93             (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
94     {
95 
96         if (impeg2d_bit_stream_get(ps_stream,8) != 0)
97         {
98             /* Ignore stuffing bit errors. */
99         }
100 
101     }
102     return;
103 }
104 /******************************************************************************
105 *  Function Name   : impeg2d_peek_next_start_code
106 *
107 *  Description     : Peek for next_start_code from the stream_t.
108 *
109 *  Arguments       :
110 *  dec             : Decoder Context
111 *
112 *  Values Returned : None
113 ******************************************************************************/
impeg2d_peek_next_start_code(dec_state_t * ps_dec)114 void impeg2d_peek_next_start_code(dec_state_t *ps_dec)
115 {
116     stream_t *ps_stream;
117     ps_stream = &ps_dec->s_bit_stream;
118     impeg2d_bit_stream_flush_to_byte_boundary(ps_stream);
119 
120     while ((impeg2d_bit_stream_nxt(ps_stream,START_CODE_PREFIX_LEN) != START_CODE_PREFIX)
121         && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
122     {
123         impeg2d_bit_stream_get(ps_stream,8);
124     }
125     return;
126 }
127 /******************************************************************************
128 *
129 *  Function Name   : impeg2d_dec_seq_hdr
130 *
131 *  Description     : Decodes Sequence header information
132 *
133 *  Arguments       :
134 *  dec             : Decoder Context
135 *
136 *  Values Returned : None
137 ******************************************************************************/
impeg2d_dec_seq_hdr(dec_state_t * ps_dec)138 IMPEG2D_ERROR_CODES_T impeg2d_dec_seq_hdr(dec_state_t *ps_dec)
139 {
140     stream_t *ps_stream;
141     ps_stream = &ps_dec->s_bit_stream;
142     UWORD16 u2_height;
143     UWORD16 u2_width;
144 
145     if (impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) != SEQUENCE_HEADER_CODE)
146     {
147         impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
148         return IMPEG2D_FRM_HDR_START_CODE_NOT_FOUND;
149 
150     }
151     impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
152 
153     u2_width    = impeg2d_bit_stream_get(ps_stream,12);
154     u2_height   = impeg2d_bit_stream_get(ps_stream,12);
155 
156     if (0 == u2_width || 0 == u2_height)
157     {
158         IMPEG2D_ERROR_CODES_T e_error = IMPEG2D_FRM_HDR_DECODE_ERR;
159         return e_error;
160     }
161 
162     if ((u2_width != ps_dec->u2_horizontal_size)
163                     || (u2_height != ps_dec->u2_vertical_size))
164     {
165         if (0 == ps_dec->u2_header_done)
166         {
167             /* This is the first time we are reading the resolution */
168             ps_dec->u2_horizontal_size = u2_width;
169             ps_dec->u2_vertical_size = u2_height;
170         }
171         else
172         {
173             if (0 == ps_dec->i4_pic_count)
174             {
175                 /* Decoder has not decoded a single frame since the last
176                  * reset/init. This implies that we have two headers in the
177                  * input stream. So, do not indicate a resolution change, since
178                  * this can take the decoder into an infinite loop.
179                  */
180                 return (IMPEG2D_ERROR_CODES_T) IMPEG2D_FRM_HDR_DECODE_ERR;
181             }
182             else if((u2_width > ps_dec->u2_create_max_width)
183                             || (u2_height > ps_dec->u2_create_max_height))
184             {
185                 IMPEG2D_ERROR_CODES_T e_error = IMPEG2D_UNSUPPORTED_DIMENSIONS;
186 
187                 ps_dec->u2_reinit_max_height   = u2_height;
188                 ps_dec->u2_reinit_max_width    = u2_width;
189 
190                 return e_error;
191             }
192             else if((ps_dec->u2_horizontal_size < MIN_WIDTH)
193                             || (ps_dec->u2_vertical_size < MIN_HEIGHT))
194             {
195                 return IMPEG2D_UNSUPPORTED_DIMENSIONS;
196             }
197             else
198             {
199                 /* The resolution has changed */
200                 return (IMPEG2D_ERROR_CODES_T)IVD_RES_CHANGED;
201             }
202         }
203     }
204 
205     if((ps_dec->u2_horizontal_size > ps_dec->u2_create_max_width)
206                     || (ps_dec->u2_vertical_size > ps_dec->u2_create_max_height))
207     {
208         IMPEG2D_ERROR_CODES_T e_error = IMPEG2D_UNSUPPORTED_DIMENSIONS;
209         ps_dec->u2_reinit_max_height   = ps_dec->u2_vertical_size;
210         ps_dec->u2_reinit_max_width    = ps_dec->u2_horizontal_size;
211         return e_error;
212     }
213 
214     if((ps_dec->u2_horizontal_size < MIN_WIDTH)
215                     || (ps_dec->u2_vertical_size < MIN_HEIGHT))
216     {
217         return IMPEG2D_UNSUPPORTED_DIMENSIONS;
218     }
219 
220     /*------------------------------------------------------------------------*/
221     /* Flush the following as they are not being used                         */
222     /* aspect_ratio_info (4 bits)                                             */
223     /*------------------------------------------------------------------------*/
224     ps_dec->u2_aspect_ratio_info = impeg2d_bit_stream_get(ps_stream,4);
225 
226     /*------------------------------------------------------------------------*/
227     /* Frame rate code(4 bits)                                                */
228     /*------------------------------------------------------------------------*/
229     ps_dec->u2_frame_rate_code = impeg2d_bit_stream_get(ps_stream,4);
230     if (ps_dec->u2_frame_rate_code > MPEG2_MAX_FRAME_RATE_CODE)
231     {
232         return IMPEG2D_FRM_HDR_DECODE_ERR;
233     }
234     /*------------------------------------------------------------------------*/
235     /* Flush the following as they are not being used                         */
236     /* bit_rate_value (18 bits)                                               */
237     /*------------------------------------------------------------------------*/
238     impeg2d_bit_stream_flush(ps_stream,18);
239     GET_MARKER_BIT(ps_dec,ps_stream);
240     /*------------------------------------------------------------------------*/
241     /* Flush the following as they are not being used                         */
242     /* vbv_buffer_size_value(10 bits), constrained_parameter_flag (1 bit)     */
243     /*------------------------------------------------------------------------*/
244     impeg2d_bit_stream_flush(ps_stream,11);
245 
246     /*------------------------------------------------------------------------*/
247     /* Quantization matrix for the intra blocks                               */
248     /*------------------------------------------------------------------------*/
249     if(impeg2d_bit_stream_get_bit(ps_stream) == 1)
250     {
251         UWORD16 i;
252         for(i = 0; i < NUM_PELS_IN_BLOCK; i++)
253         {
254             ps_dec->au1_intra_quant_matrix[gau1_impeg2_inv_scan_zig_zag[i]] =  (UWORD8)impeg2d_bit_stream_get(ps_stream,8);
255         }
256 
257     }
258     else
259     {
260         memcpy(ps_dec->au1_intra_quant_matrix,gau1_impeg2_intra_quant_matrix_default,
261                 NUM_PELS_IN_BLOCK);
262     }
263 
264     /*------------------------------------------------------------------------*/
265     /* Quantization matrix for the inter blocks                               */
266     /*------------------------------------------------------------------------*/
267     if(impeg2d_bit_stream_get_bit(ps_stream) == 1)
268     {
269         UWORD16 i;
270         for(i = 0; i < NUM_PELS_IN_BLOCK; i++)
271         {
272             ps_dec->au1_inter_quant_matrix[gau1_impeg2_inv_scan_zig_zag[i]] =   (UWORD8)impeg2d_bit_stream_get(ps_stream,8);
273         }
274     }
275     else
276     {
277         memcpy(ps_dec->au1_inter_quant_matrix,gau1_impeg2_inter_quant_matrix_default,
278             NUM_PELS_IN_BLOCK);
279     }
280     impeg2d_next_start_code(ps_dec);
281 
282     return (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
283 }
284 
285 /******************************************************************************
286 *
287 *  Function Name   : impeg2d_dec_seq_ext
288 *
289 *  Description     : Gets additional sequence data.
290 *
291 *  Arguments       :
292 *  dec             : Decoder Context
293 *
294 *  Values Returned : None
295 ******************************************************************************/
impeg2d_dec_seq_ext(dec_state_t * ps_dec)296 IMPEG2D_ERROR_CODES_T impeg2d_dec_seq_ext(dec_state_t *ps_dec)
297 {
298     stream_t *ps_stream;
299     UWORD16 horizontal_value;
300     UWORD16 vertical_value;
301 
302     ps_stream = &ps_dec->s_bit_stream;
303 
304     if (impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) != EXTENSION_START_CODE)
305     {
306         impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
307         return IMPEG2D_FRM_HDR_START_CODE_NOT_FOUND;
308 
309     }
310     /* Flush the extension start code */
311     impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
312 
313     /* Flush extension start code identifier */
314     impeg2d_bit_stream_flush(ps_stream,4);
315 
316     /*----------------------------------------------------------------------*/
317     /* Profile and Level information                                        */
318     /*----------------------------------------------------------------------*/
319     {
320         UWORD32   u4_esc_bit, u4_profile, u4_level;
321 
322         /* Read the profile and level information */
323         /* check_profile_and_level: Table 8-1     */
324         /* [7:7] 1 Escape bit                     */
325         /* [6:4] 3 Profile identification         */
326         /* [3:0] 4 Level identification           */
327 
328         u4_esc_bit   = impeg2d_bit_stream_get_bit(ps_stream);
329         u4_profile   = impeg2d_bit_stream_get(ps_stream,3);
330         u4_level     = impeg2d_bit_stream_get(ps_stream,4);
331         UNUSED(u4_profile);
332         UNUSED(u4_level);
333         /*
334         if( escBit == 1                   ||
335             profile < MPEG2_MAIN_PROFILE  ||
336             level < MPEG2_MAIN_LEVEL)
337             */
338         if (1 == u4_esc_bit)
339         {
340             return IMPEG2D_PROF_LEVEL_NOT_SUPPORTED;
341         }
342     }
343 
344     ps_dec->u2_progressive_sequence = impeg2d_bit_stream_get_bit(ps_stream);
345 
346     /* Read the chrominance format */
347     if(impeg2d_bit_stream_get(ps_stream,2) != 0x1)
348         return IMPEG2D_CHROMA_FMT_NOT_SUP;
349 
350     /* Error resilience: store the 2 most significant bit in horizontal and vertical   */
351     /* variables.Use it only if adding them to the vertical and horizontal sizes       */
352     /* respectively, doesn't exceed the MAX_WD and MAX_HT supported by the application.*/
353 
354 
355     /* Read the 2 most significant bits from horizontal_size */
356     horizontal_value               = (impeg2d_bit_stream_get(ps_stream,2) << 12);
357 
358     /* Read the 2 most significant bits from vertical_size */
359     vertical_value                 = (impeg2d_bit_stream_get(ps_stream,2) << 12);
360 
361     /* Error resilience: The height and width should not be more than the*/
362     /*max height and width the application can support*/
363     if(ps_dec->u2_create_max_height < (ps_dec->u2_vertical_size + vertical_value))
364     {
365         return (IMPEG2D_ERROR_CODES_T) IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
366     }
367 
368     if(ps_dec->u2_create_max_width < (ps_dec->u2_horizontal_size + horizontal_value))
369     {
370         return (IMPEG2D_ERROR_CODES_T) IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
371     }
372     ps_dec->u2_vertical_size       += vertical_value;
373     ps_dec->u2_horizontal_size     += horizontal_value;
374 
375     /*-----------------------------------------------------------------------*/
376     /* Flush the following as they are not used now                          */
377     /* bit_rate_extension          12                                        */
378     /* marker_bit                   1                                        */
379     /* vbv_buffer_size_extension    8                                        */
380     /* low_delay                    1                                        */
381     /*-----------------------------------------------------------------------*/
382     impeg2d_bit_stream_flush(ps_stream,12);
383     GET_MARKER_BIT(ps_dec,ps_stream);
384     impeg2d_bit_stream_flush(ps_stream,9);
385     /*-----------------------------------------------------------------------*/
386     /* frame_rate_extension_n       2                                        */
387     /* frame_rate_extension_d       5                                        */
388     /*-----------------------------------------------------------------------*/
389     ps_dec->u2_frame_rate_extension_n = impeg2d_bit_stream_get(ps_stream,2);
390     ps_dec->u2_frame_rate_extension_d = impeg2d_bit_stream_get(ps_stream,5);
391 
392     return (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
393 }
394 
395 /*******************************************************************************
396 *
397 *  Function Name   : impeg2d_dec_seq_disp_ext
398 *
399 *  Description     : This function is eqvt to sequence_display_extension() of
400 *                    standard. It flushes data present as it is not being used
401 *
402 *  Arguments       :
403 *  dec             : Decoder Context
404 *
405 *  Values Returned : None
406 ******************************************************************************/
impeg2d_dec_seq_disp_ext(dec_state_t * ps_dec)407 void impeg2d_dec_seq_disp_ext(dec_state_t *ps_dec)
408 {
409     stream_t *ps_stream;
410     ps_stream = &ps_dec->s_bit_stream;
411 
412     /*
413     sequence_display_extension()
414     {
415         extension_start_code_identifier 4
416         video_format                    3
417         colour_description              1
418         if (colour_description)
419         {
420             colour_primaries            8
421             transfer_characteristics    8
422             matrix_coefficients         8
423         }
424         display_horizontal_size         14
425         marker_bit                      1
426         display_vertical_size           14
427         next_start_code()
428     }
429     */
430 
431     impeg2d_bit_stream_get(ps_stream, 4);
432     ps_dec->u1_video_format = impeg2d_bit_stream_get(ps_stream, 3);
433     ps_dec->u1_colour_description = impeg2d_bit_stream_get(ps_stream, 1);
434     ps_dec->u1_colour_primaries = 2;
435     ps_dec->u1_transfer_characteristics = 2;
436     ps_dec->u1_matrix_coefficients = 2;
437     if(ps_dec->u1_colour_description)
438     {
439         ps_dec->u1_colour_primaries = impeg2d_bit_stream_get(ps_stream, 8);
440         ps_dec->u1_transfer_characteristics = impeg2d_bit_stream_get(ps_stream, 8);
441         ps_dec->u1_matrix_coefficients = impeg2d_bit_stream_get(ps_stream, 8);
442     }
443 
444     /* display_horizontal_size and display_vertical_size */
445     ps_dec->u2_display_horizontal_size = impeg2d_bit_stream_get(ps_stream,14);;
446     GET_MARKER_BIT(ps_dec,ps_stream);
447     ps_dec->u2_display_vertical_size   = impeg2d_bit_stream_get(ps_stream,14);
448 
449     ps_dec->u1_seq_disp_extn_present = 1;
450     impeg2d_next_start_code(ps_dec);
451 }
452 
453 
454 /*******************************************************************************
455 *
456 *  Function Name   : impeg2d_dec_seq_scale_ext
457 *
458 *  Description     : This function is eqvt to sequence_scalable_extension() of
459 *                    standard.
460 *
461 *  Arguments       : Decoder context
462 *
463 *  Values Returned : None
464 *******************************************************************************/
impeg2d_dec_seq_scale_ext(dec_state_t * ps_dec)465 IMPEG2D_ERROR_CODES_T impeg2d_dec_seq_scale_ext(dec_state_t *ps_dec)
466 {
467     UNUSED(ps_dec);
468     return IMPEG2D_SCALABILITIY_NOT_SUPPORTED;
469 }
470 
471 /*******************************************************************************
472 *
473 *  Function Name   : impeg2d_dec_quant_matrix_ext
474 *
475 *  Description     : Gets Intra and NonIntra quantizer matrix from the stream.
476 *
477 *  Arguments       : Decoder context
478 *
479 *  Values Returned : None
480 *******************************************************************************/
impeg2d_dec_quant_matrix_ext(dec_state_t * ps_dec)481 void impeg2d_dec_quant_matrix_ext(dec_state_t *ps_dec)
482 {
483     stream_t *ps_stream;
484 
485     ps_stream = &ps_dec->s_bit_stream;
486     /* Flush extension_start_code_identifier */
487     impeg2d_bit_stream_flush(ps_stream,4);
488 
489     /*------------------------------------------------------------------------*/
490     /* Quantization matrix for the intra blocks                               */
491     /*------------------------------------------------------------------------*/
492     if(impeg2d_bit_stream_get(ps_stream,1) == 1)
493     {
494         UWORD16 i;
495         for(i = 0; i < NUM_PELS_IN_BLOCK; i++)
496         {
497             ps_dec->au1_intra_quant_matrix[gau1_impeg2_inv_scan_zig_zag[i]] =  (UWORD8)impeg2d_bit_stream_get(ps_stream,8);
498         }
499 
500     }
501 
502 
503     /*------------------------------------------------------------------------*/
504     /* Quantization matrix for the inter blocks                               */
505     /*------------------------------------------------------------------------*/
506     if(impeg2d_bit_stream_get(ps_stream,1) == 1)
507     {
508         UWORD16 i;
509         for(i = 0; i < NUM_PELS_IN_BLOCK; i++)
510         {
511             ps_dec->au1_inter_quant_matrix[gau1_impeg2_inv_scan_zig_zag[i]] =   (UWORD8)impeg2d_bit_stream_get(ps_stream,8);
512         }
513     }
514 
515     /* Note : chroma intra quantizer matrix and chroma non
516     intra quantizer matrix are not needed for 4:2:0 format */
517     impeg2d_next_start_code(ps_dec);
518 }
519 /*******************************************************************************
520 *
521 *  Function Name   : impeg2d_dec_pic_disp_ext
522 *
523 *  Description     : This function is eqvt to picture_display_extension() of
524 *                    standard.The parameters are not used by decoder
525 *
526 *  Arguments       : Pointer to dec_state_t
527 *
528 *  Values Returned : Decoder context
529 *
530 *  Values Returned : None
531 *******************************************************************************/
impeg2d_dec_pic_disp_ext(dec_state_t * ps_dec)532 void impeg2d_dec_pic_disp_ext(dec_state_t *ps_dec)
533 {
534     WORD16 i2_number_of_frame_centre_offsets ;
535     stream_t *ps_stream;
536 
537     ps_stream = &ps_dec->s_bit_stream;
538     impeg2d_bit_stream_flush(ps_stream,4);
539 
540     if (ps_dec->u2_progressive_sequence)
541     {
542         i2_number_of_frame_centre_offsets = (ps_dec->u2_repeat_first_field) ?
543             2 + ps_dec->u2_top_field_first : 1;
544     }
545     else
546     {
547         i2_number_of_frame_centre_offsets =
548             (ps_dec->u2_picture_structure != FRAME_PICTURE) ?
549             1 : 2 + ps_dec->u2_repeat_first_field;
550     }
551     while(i2_number_of_frame_centre_offsets--)
552     {
553         /* frame_centre_horizontal_offset */
554         impeg2d_bit_stream_get(ps_stream,16);
555         GET_MARKER_BIT(ps_dec,ps_stream);
556         /* frame_centre_vertical_offset */
557         impeg2d_bit_stream_get(ps_stream,16);
558         GET_MARKER_BIT(ps_dec,ps_stream);
559     }
560     impeg2d_next_start_code(ps_dec);
561 }
562 
563 /*******************************************************************************
564 *
565 *  Function Name   : impeg2d_dec_itu_t_ext
566 *
567 *  Description     : This function is eqvt to ITU-T_extension() of
568 *                    standard.The parameters are not used by decoder
569 *
570 *  Arguments       : Decoder context
571 *
572 *  Values Returned : None
573 *******************************************************************************/
impeg2d_dec_itu_t_ext(dec_state_t * ps_dec)574 void impeg2d_dec_itu_t_ext(dec_state_t *ps_dec)
575 {
576   impeg2d_bit_stream_flush(&ps_dec->s_bit_stream,EXT_ID_LEN);
577   impeg2d_next_start_code(ps_dec);
578 }
579 
580 /*******************************************************************************
581 *  Function Name   : impeg2d_dec_copyright_ext
582 *
583 *  Description     : This function is eqvt to copyright_extension() of
584 *                    standard. The parameters are not used by decoder
585 *
586 *  Arguments       : Decoder context
587 *
588 *  Values Returned : None
589 *******************************************************************************/
590 
591 
impeg2d_dec_copyright_ext(dec_state_t * ps_dec)592 void impeg2d_dec_copyright_ext(dec_state_t *ps_dec)
593 {
594     UWORD32 u4_bits_to_flush;
595 
596     u4_bits_to_flush = COPYRIGHT_EXTENSION_LEN;
597 
598     while(u4_bits_to_flush >= 32 )
599     {
600         impeg2d_bit_stream_flush(&ps_dec->s_bit_stream,32);
601         u4_bits_to_flush = u4_bits_to_flush - 32;
602     }
603 
604     if(u4_bits_to_flush > 0)
605     {
606         impeg2d_bit_stream_flush(&ps_dec->s_bit_stream,u4_bits_to_flush);
607     }
608 
609 
610   impeg2d_next_start_code(ps_dec);
611 }
612 /*******************************************************************************
613 *  Function Name   : impeg2d_dec_cam_param_ext
614 *
615 *  Description     : This function is eqvt to camera_parameters_extension() of
616 *                    standard. The parameters are not used by decoder
617 *
618 *  Arguments       : Decoder context
619 *
620 *  Values Returned : None
621 *******************************************************************************/
622 
623 
impeg2d_dec_cam_param_ext(dec_state_t * ps_dec)624 void impeg2d_dec_cam_param_ext(dec_state_t *ps_dec)
625 {
626 
627     UWORD32 u4_bits_to_flush;
628 
629     u4_bits_to_flush = CAMERA_PARAMETER_EXTENSION_LEN;
630 
631     while(u4_bits_to_flush >= 32 )
632     {
633         impeg2d_bit_stream_flush(&ps_dec->s_bit_stream,32);
634         u4_bits_to_flush = u4_bits_to_flush - 32;
635     }
636 
637     if(u4_bits_to_flush > 0)
638     {
639         impeg2d_bit_stream_flush(&ps_dec->s_bit_stream,u4_bits_to_flush);
640     }
641 
642   impeg2d_next_start_code(ps_dec);
643 }
644 
645 /*******************************************************************************
646 *
647 *  Function Name   : impeg2d_dec_grp_of_pic_hdr
648 *
649 *  Description     : Gets information at the GOP level.
650 *
651 *  Arguments       : Decoder context
652 *
653 *  Values Returned : None
654 *******************************************************************************/
655 
656 
impeg2d_dec_grp_of_pic_hdr(dec_state_t * ps_dec)657 void impeg2d_dec_grp_of_pic_hdr(dec_state_t *ps_dec)
658 {
659 
660     UWORD32 u4_bits_to_flush;
661 
662     u4_bits_to_flush = GROUP_OF_PICTURE_LEN;
663 
664     while(u4_bits_to_flush >= 32 )
665     {
666         impeg2d_bit_stream_flush(&ps_dec->s_bit_stream,32);
667         u4_bits_to_flush = u4_bits_to_flush - 32;
668     }
669 
670     if(u4_bits_to_flush > 0)
671     {
672         impeg2d_bit_stream_flush(&ps_dec->s_bit_stream,u4_bits_to_flush);
673     }
674 
675 }
676 
677 
678 /*******************************************************************************
679 *
680 *  Function Name   : impeg2d_dec_pic_hdr
681 *
682 *  Description     : Gets the picture header information.
683 *
684 *  Arguments       : Decoder context
685 *
686 *  Values Returned : None
687 *******************************************************************************/
impeg2d_dec_pic_hdr(dec_state_t * ps_dec)688 IMPEG2D_ERROR_CODES_T impeg2d_dec_pic_hdr(dec_state_t *ps_dec)
689 {
690     stream_t *ps_stream;
691     ps_stream = &ps_dec->s_bit_stream;
692 
693     impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
694     /* Flush temporal reference */
695     impeg2d_bit_stream_get(ps_stream,10);
696 
697     /* Picture type */
698     ps_dec->e_pic_type = (e_pic_type_t)impeg2d_bit_stream_get(ps_stream,3);
699     if((ps_dec->e_pic_type < I_PIC) || (ps_dec->e_pic_type > D_PIC))
700     {
701         impeg2d_next_code(ps_dec, PICTURE_START_CODE);
702         return IMPEG2D_INVALID_PIC_TYPE;
703     }
704 
705     /* Flush vbv_delay */
706     impeg2d_bit_stream_get(ps_stream,16);
707 
708     if(ps_dec->e_pic_type == P_PIC || ps_dec->e_pic_type == B_PIC)
709     {
710         ps_dec->u2_full_pel_forw_vector = impeg2d_bit_stream_get_bit(ps_stream);
711         ps_dec->u2_forw_f_code          = impeg2d_bit_stream_get(ps_stream,3);
712     }
713     if(ps_dec->e_pic_type == B_PIC)
714     {
715         ps_dec->u2_full_pel_back_vector = impeg2d_bit_stream_get_bit(ps_stream);
716         ps_dec->u2_back_f_code          = impeg2d_bit_stream_get(ps_stream,3);
717     }
718 
719     if(ps_dec->u2_is_mpeg2 == 0)
720     {
721         if (ps_dec->u2_forw_f_code < 1 || ps_dec->u2_forw_f_code > 7 ||
722                         ps_dec->u2_back_f_code < 1 || ps_dec->u2_back_f_code > 7)
723         {
724             return IMPEG2D_UNKNOWN_ERROR;
725         }
726         ps_dec->au2_f_code[0][0] = ps_dec->au2_f_code[0][1] = ps_dec->u2_forw_f_code;
727         ps_dec->au2_f_code[1][0] = ps_dec->au2_f_code[1][1] = ps_dec->u2_back_f_code;
728     }
729 
730     /*-----------------------------------------------------------------------*/
731     /*  Flush the extra bit value                                            */
732     /*                                                                       */
733     /*  while(impeg2d_bit_stream_nxt() == '1')                                  */
734     /*  {                                                                    */
735     /*      extra_bit_picture         1                                      */
736     /*      extra_information_picture 8                                      */
737     /*  }                                                                    */
738     /*  extra_bit_picture             1                                      */
739     /*-----------------------------------------------------------------------*/
740     while (impeg2d_bit_stream_nxt(ps_stream,1) == 1 &&
741            ps_stream->u4_offset < ps_stream->u4_max_offset)
742     {
743         impeg2d_bit_stream_get(ps_stream,9);
744     }
745     impeg2d_bit_stream_get_bit(ps_stream);
746     impeg2d_next_start_code(ps_dec);
747 
748     return (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
749 }
750 
751 
752 /*******************************************************************************
753 *
754 *  Function Name   : impeg2d_dec_pic_coding_ext
755 *
756 *  Description     : Reads more picture level parameters
757 *
758 *  Arguments       :
759 *  dec             : Decoder context
760 *
761 *  Values Returned : Error
762 *******************************************************************************/
impeg2d_dec_pic_coding_ext(dec_state_t * ps_dec)763 IMPEG2D_ERROR_CODES_T impeg2d_dec_pic_coding_ext(dec_state_t *ps_dec)
764 {
765     stream_t *ps_stream;
766     IMPEG2D_ERROR_CODES_T e_error = (IMPEG2D_ERROR_CODES_T) IV_SUCCESS;
767 
768     ps_stream = &ps_dec->s_bit_stream;
769     impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
770     /* extension code identifier */
771     impeg2d_bit_stream_get(ps_stream,4);
772 
773     ps_dec->au2_f_code[0][0]             = impeg2d_bit_stream_get(ps_stream,4);
774     ps_dec->au2_f_code[0][1]             = impeg2d_bit_stream_get(ps_stream,4);
775     ps_dec->au2_f_code[1][0]             = impeg2d_bit_stream_get(ps_stream,4);
776     ps_dec->au2_f_code[1][1]             = impeg2d_bit_stream_get(ps_stream,4);
777     ps_dec->u2_intra_dc_precision        = impeg2d_bit_stream_get(ps_stream,2);
778     ps_dec->u2_picture_structure            = impeg2d_bit_stream_get(ps_stream,2);
779     if (ps_dec->u2_picture_structure < TOP_FIELD ||
780                     ps_dec->u2_picture_structure > FRAME_PICTURE)
781     {
782         return IMPEG2D_FRM_HDR_DECODE_ERR;
783     }
784     ps_dec->u2_top_field_first              = impeg2d_bit_stream_get_bit(ps_stream);
785     ps_dec->u2_frame_pred_frame_dct         = impeg2d_bit_stream_get_bit(ps_stream);
786     ps_dec->u2_concealment_motion_vectors   = impeg2d_bit_stream_get_bit(ps_stream);
787     ps_dec->u2_q_scale_type                 = impeg2d_bit_stream_get_bit(ps_stream);
788     ps_dec->u2_intra_vlc_format             = impeg2d_bit_stream_get_bit(ps_stream);
789     ps_dec->u2_alternate_scan               = impeg2d_bit_stream_get_bit(ps_stream);
790     ps_dec->u2_repeat_first_field           = impeg2d_bit_stream_get_bit(ps_stream);
791     /* Flush chroma_420_type */
792     impeg2d_bit_stream_get_bit(ps_stream);
793 
794     ps_dec->u2_progressive_frame            = impeg2d_bit_stream_get_bit(ps_stream);
795     if (impeg2d_bit_stream_get_bit(ps_stream))
796     {
797         /* Flush v_axis, field_sequence, burst_amplitude, sub_carrier_phase */
798         impeg2d_bit_stream_flush(ps_stream,20);
799     }
800     impeg2d_next_start_code(ps_dec);
801 
802 
803     if(VERTICAL_SCAN == ps_dec->u2_alternate_scan)
804     {
805         ps_dec->pu1_inv_scan_matrix = (UWORD8 *)gau1_impeg2_inv_scan_vertical;
806     }
807     else
808     {
809         ps_dec->pu1_inv_scan_matrix = (UWORD8 *)gau1_impeg2_inv_scan_zig_zag;
810     }
811     return e_error;
812 }
813 
814 /*******************************************************************************
815 *
816 *  Function Name   : impeg2d_dec_slice
817 *
818 *  Description     : Reads Slice level parameters and calls functions that
819 *                    decode individual MBs of slice
820 *
821 *  Arguments       :
822 *  dec             : Decoder context
823 *
824 *  Values Returned : None
825 *******************************************************************************/
impeg2d_dec_slice(dec_state_t * ps_dec)826 IMPEG2D_ERROR_CODES_T impeg2d_dec_slice(dec_state_t *ps_dec)
827 {
828     stream_t *ps_stream;
829     UWORD32 u4_slice_vertical_position;
830     UWORD32 u4_slice_vertical_position_extension;
831     IMPEG2D_ERROR_CODES_T e_error;
832 
833     ps_stream = &ps_dec->s_bit_stream;
834 
835     /*------------------------------------------------------------------------*/
836     /* All the profiles supported require restricted slice structure. Hence   */
837     /* there is no need to store slice_vertical_position. Note that max       */
838     /* height supported does not exceed 2800 and scalablity is not supported  */
839     /*------------------------------------------------------------------------*/
840 
841     /* Remove the slice start code */
842     impeg2d_bit_stream_flush(ps_stream,START_CODE_PREFIX_LEN);
843     u4_slice_vertical_position = impeg2d_bit_stream_get(ps_stream, 8);
844     if(u4_slice_vertical_position > 2800)
845     {
846         u4_slice_vertical_position_extension = impeg2d_bit_stream_get(ps_stream, 3);
847         u4_slice_vertical_position += (u4_slice_vertical_position_extension << 7);
848     }
849 
850     if((u4_slice_vertical_position > ps_dec->u2_num_vert_mb) ||
851        (u4_slice_vertical_position == 0))
852     {
853         return IMPEG2D_INVALID_VERT_SIZE;
854     }
855 
856     // change the mb_y to point to slice_vertical_position
857     u4_slice_vertical_position--;
858     if (ps_dec->u2_mb_y != u4_slice_vertical_position)
859     {
860         ps_dec->u2_mb_y    = u4_slice_vertical_position;
861         ps_dec->u2_mb_x    = 0;
862 
863         /* Update the number of MBs left, since we have probably missed a slice
864          * (that's why we see a mismatch between u2_mb_y and current position).
865          */
866         ps_dec->u2_num_mbs_left = (ps_dec->u2_num_vert_mb - ps_dec->u2_mb_y)
867                         * ps_dec->u2_num_horiz_mb;
868     }
869     ps_dec->u2_first_mb = 1;
870 
871     /*------------------------------------------------------------------------*/
872     /* Quant scale code decoding                                              */
873     /*------------------------------------------------------------------------*/
874     {
875         UWORD16 u2_quant_scale_code;
876         u2_quant_scale_code = impeg2d_bit_stream_get(ps_stream,5);
877         ps_dec->u1_quant_scale = (ps_dec->u2_q_scale_type) ?
878             gau1_impeg2_non_linear_quant_scale[u2_quant_scale_code] : (u2_quant_scale_code << 1);
879     }
880 
881     if (impeg2d_bit_stream_nxt(ps_stream,1) == 1)
882     {
883         impeg2d_bit_stream_flush(ps_stream,9);
884         /* Flush extra bit information */
885         while (impeg2d_bit_stream_nxt(ps_stream,1) == 1 &&
886                ps_stream->u4_offset < ps_stream->u4_max_offset)
887         {
888             impeg2d_bit_stream_flush(ps_stream,9);
889         }
890     }
891     impeg2d_bit_stream_get_bit(ps_stream);
892 
893     /* Reset the DC predictors to reset values given in Table 7.2 at the start*/
894     /* of slice data */
895     ps_dec->u2_def_dc_pred[Y_LUMA]   = 128 << ps_dec->u2_intra_dc_precision;
896     ps_dec->u2_def_dc_pred[U_CHROMA]   = 128 << ps_dec->u2_intra_dc_precision;
897     ps_dec->u2_def_dc_pred[V_CHROMA]   = 128 << ps_dec->u2_intra_dc_precision;
898     /*------------------------------------------------------------------------*/
899     /* dec->DecMBsinSlice() implements the following psuedo code from standard*/
900     /* do                                                                     */
901     /* {                                                                      */
902     /*      macroblock()                                                      */
903     /* } while (impeg2d_bit_stream_nxt() != '000 0000 0000 0000 0000 0000')      */
904     /*------------------------------------------------------------------------*/
905 
906     e_error = ps_dec->pf_decode_slice(ps_dec);
907     if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
908     {
909         return e_error;
910     }
911 
912     /* Check for the MBy index instead of number of MBs left, because the
913      * number of MBs left in case of multi-thread decode is the number of MBs
914      * in that row only
915      */
916     if(ps_dec->u2_mb_y < ps_dec->u2_num_vert_mb)
917         impeg2d_next_start_code(ps_dec);
918 
919     return (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
920 }
921 
impeg2d_dec_pic_data_thread(dec_state_t * ps_dec)922 void impeg2d_dec_pic_data_thread(dec_state_t *ps_dec)
923 {
924     WORD32 i4_continue_decode;
925 
926     WORD32 i4_cur_row, temp;
927     UWORD32 u4_bits_read;
928     WORD32 i4_dequeue_job;
929     IMPEG2D_ERROR_CODES_T e_error;
930 
931     i4_cur_row = ps_dec->u2_mb_y + 1;
932 
933     i4_continue_decode = 1;
934 
935     i4_dequeue_job = 1;
936     do
937     {
938         if(i4_cur_row > ps_dec->u2_num_vert_mb)
939         {
940             i4_continue_decode = 0;
941             break;
942         }
943 
944         {
945             if((ps_dec->i4_num_cores> 1) && (i4_dequeue_job))
946             {
947                 job_t s_job;
948                 IV_API_CALL_STATUS_T e_ret;
949                 UWORD8 *pu1_buf;
950 
951                 e_ret = impeg2_jobq_dequeue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 1);
952                 if(e_ret != IV_SUCCESS)
953                     break;
954 
955                 if(CMD_PROCESS == s_job.i4_cmd)
956                 {
957                     pu1_buf = ps_dec->pu1_inp_bits_buf + s_job.i4_bistream_ofst;
958                     impeg2d_bit_stream_init(&(ps_dec->s_bit_stream), pu1_buf,
959                             (ps_dec->u4_num_inp_bytes - s_job.i4_bistream_ofst));
960                     i4_cur_row      = s_job.i2_start_mb_y;
961                     ps_dec->i4_start_mb_y = s_job.i2_start_mb_y;
962                     ps_dec->i4_end_mb_y = s_job.i2_end_mb_y;
963                     ps_dec->u2_mb_x = 0;
964                     ps_dec->u2_mb_y = ps_dec->i4_start_mb_y;
965                     ps_dec->u2_num_mbs_left = (ps_dec->i4_end_mb_y - ps_dec->i4_start_mb_y) * ps_dec->u2_num_horiz_mb;
966 
967                 }
968                 else
969                 {
970                     WORD32 start_row;
971                     WORD32 num_rows;
972                     start_row = s_job.i2_start_mb_y << 4;
973                     num_rows = MIN((s_job.i2_end_mb_y << 4), ps_dec->u2_vertical_size);
974                     num_rows -= start_row;
975 
976                     if(ps_dec->u4_deinterlace && (0 == ps_dec->u2_progressive_frame))
977                     {
978                         impeg2d_deinterlace(ps_dec,
979                                             ps_dec->ps_disp_pic,
980                                             ps_dec->ps_disp_frm_buf,
981                                             start_row,
982                                             num_rows);
983 
984                     }
985                     else
986                     {
987                         impeg2d_format_convert(ps_dec, ps_dec->ps_disp_pic,
988                                                ps_dec->ps_disp_frm_buf,
989                                                start_row, num_rows);
990                     }
991                     break;
992 
993                 }
994 
995             }
996             e_error = impeg2d_dec_slice(ps_dec);
997 
998             if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
999             {
1000                 impeg2d_next_start_code(ps_dec);
1001                 if(ps_dec->s_bit_stream.u4_offset >= ps_dec->s_bit_stream.u4_max_offset)
1002                 {
1003                     ps_dec->u4_error_code = IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1004                     return;
1005                 }
1006             }
1007         }
1008 
1009         /* Detecting next slice start code */
1010         while(1)
1011         {
1012             // skip (dec->u4_num_cores-1) rows
1013             u4_bits_read = impeg2d_bit_stream_nxt(&ps_dec->s_bit_stream,START_CODE_LEN);
1014             temp = u4_bits_read & 0xFF;
1015             i4_continue_decode = (((u4_bits_read >> 8) == 0x01) && (temp) && (temp <= 0xAF));
1016 
1017             if (1 == ps_dec->i4_num_cores && 0 == ps_dec->u2_num_mbs_left)
1018             {
1019                 i4_continue_decode = 0;
1020 #ifdef __ANDROID__
1021                 android_errorWriteLog(0x534e4554, "26070014");
1022 #endif
1023             }
1024 
1025             if(i4_continue_decode)
1026             {
1027                 if (0 != ps_dec->u2_num_mbs_left)
1028                 {
1029                     /* If the slice is from the same row, then continue decoding without dequeue */
1030                     if((temp - 1) == i4_cur_row)
1031                     {
1032                         i4_dequeue_job = 0;
1033                     }
1034                     else
1035                     {
1036                         if(temp < ps_dec->i4_end_mb_y)
1037                         {
1038                             i4_cur_row = ps_dec->u2_mb_y;
1039                         }
1040                         else
1041                         {
1042                             i4_dequeue_job = 1;
1043                         }
1044                     }
1045                 }
1046                 else
1047                 {
1048                     i4_dequeue_job = 1;
1049                 }
1050                 break;
1051             }
1052             else
1053                 break;
1054         }
1055 
1056     }while(i4_continue_decode);
1057     if(ps_dec->i4_num_cores > 1)
1058     {
1059         while(1)
1060         {
1061             job_t s_job;
1062             IV_API_CALL_STATUS_T e_ret;
1063 
1064             e_ret = impeg2_jobq_dequeue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 1);
1065             if(e_ret != IV_SUCCESS)
1066                 break;
1067             if(CMD_FMTCONV == s_job.i4_cmd)
1068             {
1069                 WORD32 start_row;
1070                 WORD32 num_rows;
1071                 start_row = s_job.i2_start_mb_y << 4;
1072                 num_rows = MIN((s_job.i2_end_mb_y << 4), ps_dec->u2_vertical_size);
1073                 num_rows -= start_row;
1074                 if(ps_dec->u4_deinterlace && (0 == ps_dec->u2_progressive_frame))
1075                 {
1076                     impeg2d_deinterlace(ps_dec,
1077                                         ps_dec->ps_disp_pic,
1078                                         ps_dec->ps_disp_frm_buf,
1079                                         start_row,
1080                                         num_rows);
1081 
1082                 }
1083                 else
1084                 {
1085                     impeg2d_format_convert(ps_dec,
1086                                            ps_dec->ps_disp_pic,
1087                                            ps_dec->ps_disp_frm_buf,
1088                                            start_row,
1089                                            num_rows);
1090                 }
1091             }
1092         }
1093     }
1094     else
1095     {
1096         if((NULL != ps_dec->ps_disp_pic) && ((0 == ps_dec->u4_share_disp_buf) || (IV_YUV_420P != ps_dec->i4_chromaFormat)))
1097         {
1098             if(ps_dec->u4_deinterlace && (0 == ps_dec->u2_progressive_frame))
1099             {
1100                 impeg2d_deinterlace(ps_dec,
1101                                     ps_dec->ps_disp_pic,
1102                                     ps_dec->ps_disp_frm_buf,
1103                                     0,
1104                                     ps_dec->u2_vertical_size);
1105 
1106             }
1107             else
1108             {
1109                 impeg2d_format_convert(ps_dec, ps_dec->ps_disp_pic,
1110                                         ps_dec->ps_disp_frm_buf,
1111                                         0, ps_dec->u2_vertical_size);
1112             }
1113         }
1114     }
1115 }
1116 
impeg2d_init_thread_dec_ctxt(dec_state_t * ps_dec,dec_state_t * ps_dec_thd,WORD32 i4_min_mb_y)1117 static WORD32 impeg2d_init_thread_dec_ctxt(dec_state_t *ps_dec,
1118                                            dec_state_t *ps_dec_thd,
1119                                            WORD32 i4_min_mb_y)
1120 {
1121     UNUSED(i4_min_mb_y);
1122     ps_dec_thd->i4_start_mb_y = 0;
1123     ps_dec_thd->i4_end_mb_y = ps_dec->u2_num_vert_mb;
1124     ps_dec_thd->u2_mb_x = 0;
1125     ps_dec_thd->u2_mb_y = 0;
1126     ps_dec_thd->u2_is_mpeg2 = ps_dec->u2_is_mpeg2;
1127     ps_dec_thd->i4_pic_count = ps_dec->i4_pic_count;
1128     ps_dec_thd->u2_frame_width = ps_dec->u2_frame_width;
1129     ps_dec_thd->u2_frame_height = ps_dec->u2_frame_height;
1130     ps_dec_thd->u2_picture_width = ps_dec->u2_picture_width;
1131     ps_dec_thd->u2_horizontal_size = ps_dec->u2_horizontal_size;
1132     ps_dec_thd->u2_vertical_size = ps_dec->u2_vertical_size;
1133     ps_dec_thd->u2_create_max_width = ps_dec->u2_create_max_width;
1134     ps_dec_thd->u2_create_max_height = ps_dec->u2_create_max_height;
1135     ps_dec_thd->u2_header_done = ps_dec->u2_header_done;
1136     ps_dec_thd->u2_decode_header = ps_dec->u2_decode_header;
1137 
1138     ps_dec_thd->u2_num_horiz_mb = ps_dec->u2_num_horiz_mb;
1139     ps_dec_thd->u2_num_vert_mb = ps_dec->u2_num_vert_mb;
1140     ps_dec_thd->u2_num_flds_decoded = ps_dec->u2_num_flds_decoded;
1141 
1142     ps_dec_thd->u4_frm_buf_stride = ps_dec->u4_frm_buf_stride;
1143 
1144     ps_dec_thd->u2_field_dct = ps_dec->u2_field_dct;
1145     ps_dec_thd->u2_read_dct_type = ps_dec->u2_read_dct_type;
1146 
1147     ps_dec_thd->u2_read_motion_type = ps_dec->u2_read_motion_type;
1148     ps_dec_thd->u2_motion_type = ps_dec->u2_motion_type;
1149 
1150     ps_dec_thd->pu2_mb_type = ps_dec->pu2_mb_type;
1151     ps_dec_thd->u2_fld_pic = ps_dec->u2_fld_pic;
1152     ps_dec_thd->u2_frm_pic = ps_dec->u2_frm_pic;
1153 
1154     ps_dec_thd->u2_fld_parity = ps_dec->u2_fld_parity;
1155 
1156     ps_dec_thd->au2_fcode_data[0] = ps_dec->au2_fcode_data[0];
1157     ps_dec_thd->au2_fcode_data[1] = ps_dec->au2_fcode_data[1];
1158 
1159     ps_dec_thd->u1_quant_scale = ps_dec->u1_quant_scale;
1160 
1161     ps_dec_thd->u2_num_mbs_left = ps_dec->u2_num_mbs_left;
1162     ps_dec_thd->u2_first_mb = ps_dec->u2_first_mb;
1163     ps_dec_thd->u2_num_skipped_mbs = ps_dec->u2_num_skipped_mbs;
1164 
1165     memcpy(&ps_dec_thd->s_cur_frm_buf, &ps_dec->s_cur_frm_buf, sizeof(yuv_buf_t));
1166     memcpy(&ps_dec_thd->as_recent_fld[0][0], &ps_dec->as_recent_fld[0][0], sizeof(yuv_buf_t));
1167     memcpy(&ps_dec_thd->as_recent_fld[0][1], &ps_dec->as_recent_fld[0][1], sizeof(yuv_buf_t));
1168     memcpy(&ps_dec_thd->as_recent_fld[1][0], &ps_dec->as_recent_fld[1][0], sizeof(yuv_buf_t));
1169     memcpy(&ps_dec_thd->as_recent_fld[1][1], &ps_dec->as_recent_fld[1][1], sizeof(yuv_buf_t));
1170     memcpy(&ps_dec_thd->as_ref_buf, &ps_dec->as_ref_buf, sizeof(yuv_buf_t) * 2 * 2);
1171 
1172 
1173     ps_dec_thd->pf_decode_slice = ps_dec->pf_decode_slice;
1174 
1175     ps_dec_thd->pf_vld_inv_quant = ps_dec->pf_vld_inv_quant;
1176 
1177     memcpy(ps_dec_thd->pf_idct_recon, ps_dec->pf_idct_recon, sizeof(ps_dec->pf_idct_recon));
1178 
1179     memcpy(ps_dec_thd->pf_mc, ps_dec->pf_mc, sizeof(ps_dec->pf_mc));
1180     ps_dec_thd->pf_interpolate = ps_dec->pf_interpolate;
1181     ps_dec_thd->pf_copy_mb = ps_dec->pf_copy_mb;
1182     ps_dec_thd->pf_fullx_halfy_8x8              =  ps_dec->pf_fullx_halfy_8x8;
1183     ps_dec_thd->pf_halfx_fully_8x8              =  ps_dec->pf_halfx_fully_8x8;
1184     ps_dec_thd->pf_halfx_halfy_8x8              =  ps_dec->pf_halfx_halfy_8x8;
1185     ps_dec_thd->pf_fullx_fully_8x8              =  ps_dec->pf_fullx_fully_8x8;
1186 
1187     ps_dec_thd->pf_memset_8bit_8x8_block        =  ps_dec->pf_memset_8bit_8x8_block;
1188     ps_dec_thd->pf_memset_16bit_8x8_linear_block        =  ps_dec->pf_memset_16bit_8x8_linear_block;
1189     ps_dec_thd->pf_copy_yuv420p_buf             =   ps_dec->pf_copy_yuv420p_buf;
1190     ps_dec_thd->pf_fmt_conv_yuv420p_to_yuv422ile    =   ps_dec->pf_fmt_conv_yuv420p_to_yuv422ile;
1191     ps_dec_thd->pf_fmt_conv_yuv420p_to_yuv420sp_uv  =   ps_dec->pf_fmt_conv_yuv420p_to_yuv420sp_uv;
1192     ps_dec_thd->pf_fmt_conv_yuv420p_to_yuv420sp_vu  =   ps_dec->pf_fmt_conv_yuv420p_to_yuv420sp_vu;
1193 
1194 
1195     memcpy(ps_dec_thd->au1_intra_quant_matrix, ps_dec->au1_intra_quant_matrix, NUM_PELS_IN_BLOCK * sizeof(UWORD8));
1196     memcpy(ps_dec_thd->au1_inter_quant_matrix, ps_dec->au1_inter_quant_matrix, NUM_PELS_IN_BLOCK * sizeof(UWORD8));
1197     ps_dec_thd->pu1_inv_scan_matrix = ps_dec->pu1_inv_scan_matrix;
1198 
1199 
1200     ps_dec_thd->u2_progressive_sequence = ps_dec->u2_progressive_sequence;
1201     ps_dec_thd->e_pic_type =  ps_dec->e_pic_type;
1202     ps_dec_thd->u2_full_pel_forw_vector = ps_dec->u2_full_pel_forw_vector;
1203     ps_dec_thd->u2_forw_f_code =   ps_dec->u2_forw_f_code;
1204     ps_dec_thd->u2_full_pel_back_vector = ps_dec->u2_full_pel_back_vector;
1205     ps_dec_thd->u2_back_f_code = ps_dec->u2_back_f_code;
1206 
1207     memcpy(ps_dec_thd->ai2_mv, ps_dec->ai2_mv, (2*2*2)*sizeof(WORD16));
1208     memcpy(ps_dec_thd->au2_f_code, ps_dec->au2_f_code, (2*2)*sizeof(UWORD16));
1209     ps_dec_thd->u2_intra_dc_precision = ps_dec->u2_intra_dc_precision;
1210     ps_dec_thd->u2_picture_structure = ps_dec->u2_picture_structure;
1211     ps_dec_thd->u2_top_field_first = ps_dec->u2_top_field_first;
1212     ps_dec_thd->u2_frame_pred_frame_dct = ps_dec->u2_frame_pred_frame_dct;
1213     ps_dec_thd->u2_concealment_motion_vectors = ps_dec->u2_concealment_motion_vectors;
1214     ps_dec_thd->u2_q_scale_type =  ps_dec->u2_q_scale_type;
1215     ps_dec_thd->u2_intra_vlc_format = ps_dec->u2_intra_vlc_format;
1216     ps_dec_thd->u2_alternate_scan = ps_dec->u2_alternate_scan;
1217     ps_dec_thd->u2_repeat_first_field = ps_dec->u2_repeat_first_field;
1218     ps_dec_thd->u2_progressive_frame = ps_dec->u2_progressive_frame;
1219     ps_dec_thd->pu1_inp_bits_buf = ps_dec->pu1_inp_bits_buf;
1220     ps_dec_thd->u4_num_inp_bytes = ps_dec->u4_num_inp_bytes;
1221     ps_dec_thd->pv_jobq = ps_dec->pv_jobq;
1222     ps_dec_thd->pv_jobq_buf = ps_dec->pv_jobq_buf;
1223     ps_dec_thd->i4_jobq_buf_size = ps_dec->i4_jobq_buf_size;
1224 
1225 
1226     ps_dec_thd->u2_frame_rate_code = ps_dec->u2_frame_rate_code;
1227     ps_dec_thd->u2_frame_rate_extension_n = ps_dec->u2_frame_rate_extension_n;
1228     ps_dec_thd->u2_frame_rate_extension_d = ps_dec->u2_frame_rate_extension_d;
1229     ps_dec_thd->u2_framePeriod =   ps_dec->u2_framePeriod;
1230     ps_dec_thd->u2_display_horizontal_size = ps_dec->u2_display_horizontal_size;
1231     ps_dec_thd->u2_display_vertical_size = ps_dec->u2_display_vertical_size;
1232     ps_dec_thd->u2_aspect_ratio_info = ps_dec->u2_aspect_ratio_info;
1233 
1234     ps_dec_thd->ps_func_bi_direct = ps_dec->ps_func_bi_direct;
1235     ps_dec_thd->ps_func_forw_or_back = ps_dec->ps_func_forw_or_back;
1236     ps_dec_thd->pv_deinterlacer_ctxt = ps_dec->pv_deinterlacer_ctxt;
1237     ps_dec_thd->ps_deint_pic = ps_dec->ps_deint_pic;
1238 
1239     return 0;
1240 }
1241 
1242 
impeg2d_get_slice_pos(dec_state_multi_core_t * ps_dec_state_multi_core)1243 WORD32 impeg2d_get_slice_pos(dec_state_multi_core_t *ps_dec_state_multi_core)
1244 {
1245     WORD32 u4_bits;
1246     WORD32 i4_row;
1247 
1248 
1249     dec_state_t *ps_dec = ps_dec_state_multi_core->ps_dec_state[0];
1250     WORD32 i4_prev_row;
1251     stream_t s_bitstrm;
1252     WORD32 i4_start_row;
1253     WORD32 i4_slice_bistream_ofst;
1254     WORD32 i;
1255     s_bitstrm = ps_dec->s_bit_stream;
1256     i4_prev_row = -1;
1257 
1258     ps_dec_state_multi_core->ps_dec_state[0]->i4_start_mb_y = 0;
1259     ps_dec_state_multi_core->ps_dec_state[1]->i4_start_mb_y = -1;
1260     ps_dec_state_multi_core->ps_dec_state[2]->i4_start_mb_y = -1;
1261     ps_dec_state_multi_core->ps_dec_state[3]->i4_start_mb_y = -1;
1262 
1263     ps_dec_state_multi_core->ps_dec_state[0]->i4_end_mb_y = ps_dec->u2_num_vert_mb;
1264     ps_dec_state_multi_core->ps_dec_state[1]->i4_end_mb_y = -1;
1265     ps_dec_state_multi_core->ps_dec_state[2]->i4_end_mb_y = -1;
1266     ps_dec_state_multi_core->ps_dec_state[3]->i4_end_mb_y = -1;
1267 
1268     if(ps_dec->i4_num_cores == 1)
1269         return 0;
1270     /* Reset the jobq to start of the jobq buffer */
1271     impeg2_jobq_reset((jobq_t *)ps_dec->pv_jobq);
1272 
1273     i4_start_row = -1;
1274     i4_slice_bistream_ofst = 0;
1275     while(1)
1276     {
1277         WORD32 i4_is_slice;
1278 
1279         if(s_bitstrm.u4_offset + START_CODE_LEN >= s_bitstrm.u4_max_offset)
1280         {
1281             break;
1282         }
1283         u4_bits = impeg2d_bit_stream_nxt(&s_bitstrm,START_CODE_LEN);
1284 
1285         i4_row = u4_bits & 0xFF;
1286 
1287         /* Detect end of frame */
1288         i4_is_slice = (((u4_bits >> 8) == 0x01) && (i4_row) && (i4_row <= ps_dec->u2_num_vert_mb));
1289         if(!i4_is_slice)
1290             break;
1291 
1292         i4_row -= 1;
1293 
1294 
1295         if(i4_prev_row < i4_row)
1296         {
1297             /* Create a job for previous slice row */
1298             if(i4_start_row != -1)
1299             {
1300                 job_t s_job;
1301                 IV_API_CALL_STATUS_T ret;
1302                 s_job.i2_start_mb_y = i4_start_row;
1303                 s_job.i2_end_mb_y = i4_row;
1304                 s_job.i4_cmd = CMD_PROCESS;
1305                 s_job.i4_bistream_ofst = i4_slice_bistream_ofst;
1306                 ret = impeg2_jobq_queue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 0);
1307                 if(ret != IV_SUCCESS)
1308                     return ret;
1309 
1310             }
1311             /* Store current slice's bitstream offset */
1312             i4_slice_bistream_ofst = s_bitstrm.u4_offset >> 3;
1313             i4_slice_bistream_ofst -= (size_t)s_bitstrm.pv_bs_buf & 3;
1314             i4_prev_row = i4_row;
1315 
1316             /* Store current slice's row position */
1317             i4_start_row = i4_row;
1318 
1319         }
1320 #ifdef __ANDROID__
1321         else if (i4_prev_row > i4_row)
1322         {
1323             android_errorWriteLog(0x534e4554, "26070014");
1324         }
1325 #endif
1326 
1327         impeg2d_bit_stream_flush(&s_bitstrm, START_CODE_LEN);
1328 
1329         // flush bytes till next start code
1330         /* Flush the bytes till a  start code is encountered  */
1331         while(impeg2d_bit_stream_nxt(&s_bitstrm, 24) != START_CODE_PREFIX)
1332         {
1333             impeg2d_bit_stream_get(&s_bitstrm, 8);
1334 
1335             if(s_bitstrm.u4_offset >= s_bitstrm.u4_max_offset)
1336             {
1337                 break;
1338             }
1339         }
1340     }
1341 
1342     /* Create job for the last slice row */
1343     {
1344         job_t s_job;
1345         IV_API_CALL_STATUS_T e_ret;
1346         s_job.i2_start_mb_y = i4_start_row;
1347         s_job.i2_end_mb_y = ps_dec->u2_num_vert_mb;
1348         s_job.i4_cmd = CMD_PROCESS;
1349         s_job.i4_bistream_ofst = i4_slice_bistream_ofst;
1350         e_ret = impeg2_jobq_queue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 0);
1351         if(e_ret != IV_SUCCESS)
1352             return e_ret;
1353 
1354     }
1355     if((NULL != ps_dec->ps_disp_pic) && ((0 == ps_dec->u4_share_disp_buf) || (IV_YUV_420P != ps_dec->i4_chromaFormat)))
1356     {
1357         for(i = 0; i < ps_dec->u2_vertical_size; i+=64)
1358         {
1359             job_t s_job;
1360             IV_API_CALL_STATUS_T ret;
1361             s_job.i2_start_mb_y = i;
1362             s_job.i2_start_mb_y >>= 4;
1363             s_job.i2_end_mb_y = (i + 64);
1364             s_job.i2_end_mb_y >>= 4;
1365             s_job.i4_cmd = CMD_FMTCONV;
1366             s_job.i4_bistream_ofst = 0;
1367             ret = impeg2_jobq_queue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 0);
1368             if(ret != IV_SUCCESS)
1369                 return ret;
1370 
1371         }
1372     }
1373 
1374     impeg2_jobq_terminate(ps_dec->pv_jobq);
1375     ps_dec->i4_bytes_consumed = s_bitstrm.u4_offset >> 3;
1376     ps_dec->i4_bytes_consumed -= ((size_t)s_bitstrm.pv_bs_buf & 3);
1377 
1378     return 0;
1379 }
1380 
1381 /*******************************************************************************
1382 *
1383 *  Function Name   : impeg2d_dec_pic_data
1384 *
1385 *  Description     : It intializes several parameters and decodes a Picture
1386 *                    till any slice is left.
1387 *
1388 *  Arguments       :
1389 *  dec             : Decoder context
1390 *
1391 *  Values Returned : None
1392 *******************************************************************************/
1393 
impeg2d_dec_pic_data(dec_state_t * ps_dec)1394 void impeg2d_dec_pic_data(dec_state_t *ps_dec)
1395 {
1396 
1397     WORD32 i;
1398     dec_state_multi_core_t *ps_dec_state_multi_core;
1399 
1400     dec_state_t *ps_dec_thd;
1401     WORD32 i4_status;
1402     WORD32 i4_min_mb_y;
1403 
1404 
1405     /* Resetting the MB address and MB coordinates at the start of the Frame */
1406     ps_dec->u2_mb_x = ps_dec->u2_mb_y = 0;
1407 
1408     ps_dec_state_multi_core = ps_dec->ps_dec_state_multi_core;
1409     impeg2d_get_slice_pos(ps_dec_state_multi_core);
1410 
1411     i4_min_mb_y = 1;
1412     for(i=0; i < ps_dec->i4_num_cores - 1; i++)
1413     {
1414         // initialize decoder context for thread
1415         // launch dec->u4_num_cores-1 threads
1416 
1417         ps_dec_thd = ps_dec_state_multi_core->ps_dec_state[i+1];
1418 
1419         ps_dec_thd->ps_disp_pic = ps_dec->ps_disp_pic;
1420         ps_dec_thd->ps_disp_frm_buf = ps_dec->ps_disp_frm_buf;
1421 
1422         i4_status = impeg2d_init_thread_dec_ctxt(ps_dec, ps_dec_thd, i4_min_mb_y);
1423         //impeg2d_dec_pic_data_thread(ps_dec_thd);
1424 
1425         if(i4_status == 0)
1426         {
1427             ithread_create(ps_dec_thd->pv_codec_thread_handle, NULL, (void *)impeg2d_dec_pic_data_thread, ps_dec_thd);
1428             ps_dec_state_multi_core->au4_thread_launched[i + 1] = 1;
1429             i4_min_mb_y = ps_dec_thd->u2_mb_y + 1;
1430         }
1431         else
1432         {
1433             ps_dec_state_multi_core->au4_thread_launched[i + 1] = 0;
1434             break;
1435         }
1436     }
1437 
1438     impeg2d_dec_pic_data_thread(ps_dec);
1439 
1440     // wait for threads to complete
1441     for(i=0; i < (ps_dec->i4_num_cores - 1); i++)
1442     {
1443         if(ps_dec_state_multi_core->au4_thread_launched[i + 1] == 1)
1444         {
1445             ps_dec_thd = ps_dec_state_multi_core->ps_dec_state[i+1];
1446             ithread_join(ps_dec_thd->pv_codec_thread_handle, NULL);
1447         }
1448     }
1449 
1450 }
1451 /*******************************************************************************
1452 *
1453 *  Function Name   : impeg2d_flush_ext_and_user_data
1454 *
1455 *  Description     : Flushes the extension and user data present in the
1456 *                    stream_t
1457 *
1458 *  Arguments       :
1459 *  dec             : Decoder context
1460 *
1461 *  Values Returned : None
1462 *******************************************************************************/
impeg2d_flush_ext_and_user_data(dec_state_t * ps_dec)1463 void impeg2d_flush_ext_and_user_data(dec_state_t *ps_dec)
1464 {
1465     UWORD32 u4_start_code;
1466     stream_t *ps_stream;
1467 
1468     ps_stream    = &ps_dec->s_bit_stream;
1469     u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1470 
1471     while((u4_start_code == EXTENSION_START_CODE || u4_start_code == USER_DATA_START_CODE) &&
1472             (ps_stream->u4_offset < ps_stream->u4_max_offset))
1473     {
1474         impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
1475         while(impeg2d_bit_stream_nxt(ps_stream,START_CODE_PREFIX_LEN) != START_CODE_PREFIX &&
1476                 (ps_stream->u4_offset < ps_stream->u4_max_offset))
1477         {
1478             impeg2d_bit_stream_flush(ps_stream,8);
1479         }
1480         u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1481     }
1482 }
1483 /*******************************************************************************
1484 *
1485 *  Function Name   : impeg2d_dec_user_data
1486 *
1487 *  Description     : Flushes the user data present in the stream_t
1488 *
1489 *  Arguments       :
1490 *  dec             : Decoder context
1491 *
1492 *  Values Returned : None
1493 *******************************************************************************/
impeg2d_dec_user_data(dec_state_t * ps_dec)1494 void impeg2d_dec_user_data(dec_state_t *ps_dec)
1495 {
1496     UWORD32 u4_start_code;
1497     stream_t *ps_stream;
1498 
1499     ps_stream    = &ps_dec->s_bit_stream;
1500     u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1501 
1502     while(u4_start_code == USER_DATA_START_CODE)
1503     {
1504         impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
1505         while((impeg2d_bit_stream_nxt(ps_stream,START_CODE_PREFIX_LEN) != START_CODE_PREFIX) &&
1506                 (ps_stream->u4_offset < ps_stream->u4_max_offset))
1507         {
1508             impeg2d_bit_stream_flush(ps_stream,8);
1509         }
1510         u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1511     }
1512 }
1513 /*******************************************************************************
1514 *  Function Name   : impeg2d_dec_seq_ext_data
1515 *
1516 *  Description     : Decodes the extension data following Sequence
1517 *                    Extension. It flushes any user data if present
1518 *
1519 *  Arguments       :
1520 *  dec             : Decoder context
1521 *
1522 *  Values Returned : None
1523 *******************************************************************************/
impeg2d_dec_seq_ext_data(dec_state_t * ps_dec)1524 IMPEG2D_ERROR_CODES_T impeg2d_dec_seq_ext_data(dec_state_t *ps_dec)
1525 {
1526     stream_t   *ps_stream;
1527     UWORD32     u4_start_code;
1528     IMPEG2D_ERROR_CODES_T e_error;
1529 
1530     e_error = (IMPEG2D_ERROR_CODES_T) IVD_ERROR_NONE;
1531 
1532     ps_stream      = &ps_dec->s_bit_stream;
1533     u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1534     while( (u4_start_code == EXTENSION_START_CODE ||
1535             u4_start_code == USER_DATA_START_CODE) &&
1536             (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE == e_error &&
1537             (ps_stream->u4_offset < ps_stream->u4_max_offset))
1538     {
1539         if(u4_start_code == USER_DATA_START_CODE)
1540         {
1541             impeg2d_dec_user_data(ps_dec);
1542         }
1543         else
1544         {
1545             impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
1546             u4_start_code   = impeg2d_bit_stream_nxt(ps_stream,EXT_ID_LEN);
1547             switch(u4_start_code)
1548             {
1549             case SEQ_DISPLAY_EXT_ID:
1550                 impeg2d_dec_seq_disp_ext(ps_dec);
1551                 break;
1552             case SEQ_SCALABLE_EXT_ID:
1553                 e_error = IMPEG2D_SCALABILITIY_NOT_SUPPORTED;
1554                 break;
1555             default:
1556                 /* In case its a reserved extension code */
1557                 impeg2d_bit_stream_flush(ps_stream,EXT_ID_LEN);
1558                 impeg2d_peek_next_start_code(ps_dec);
1559                 break;
1560             }
1561         }
1562         u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1563     }
1564     return e_error;
1565 }
1566 /*******************************************************************************
1567 *  Function Name   : impeg2d_dec_pic_ext_data
1568 *
1569 *  Description     : Decodes the extension data following Picture Coding
1570 *                    Extension. It flushes any user data if present
1571 *
1572 *  Arguments       :
1573 *  dec             : Decoder context
1574 *
1575 *  Values Returned : None
1576 *******************************************************************************/
impeg2d_dec_pic_ext_data(dec_state_t * ps_dec)1577 IMPEG2D_ERROR_CODES_T impeg2d_dec_pic_ext_data(dec_state_t *ps_dec)
1578 {
1579     stream_t   *ps_stream;
1580     UWORD32     u4_start_code;
1581     IMPEG2D_ERROR_CODES_T e_error;
1582 
1583     e_error = (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
1584 
1585     ps_stream      = &ps_dec->s_bit_stream;
1586     u4_start_code   = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1587     while ( (u4_start_code == EXTENSION_START_CODE ||
1588             u4_start_code == USER_DATA_START_CODE) &&
1589             (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE == e_error &&
1590             (ps_stream->u4_offset < ps_stream->u4_max_offset))
1591     {
1592         if(u4_start_code == USER_DATA_START_CODE)
1593         {
1594             impeg2d_dec_user_data(ps_dec);
1595         }
1596         else
1597         {
1598             impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
1599             u4_start_code   = impeg2d_bit_stream_nxt(ps_stream,EXT_ID_LEN);
1600             switch(u4_start_code)
1601             {
1602             case QUANT_MATRIX_EXT_ID:
1603                 impeg2d_dec_quant_matrix_ext(ps_dec);
1604                 break;
1605             case COPYRIGHT_EXT_ID:
1606                 impeg2d_dec_copyright_ext(ps_dec);
1607                 break;
1608             case PIC_DISPLAY_EXT_ID:
1609                 impeg2d_dec_pic_disp_ext(ps_dec);
1610                 break;
1611             case CAMERA_PARAM_EXT_ID:
1612                 impeg2d_dec_cam_param_ext(ps_dec);
1613                 break;
1614             case ITU_T_EXT_ID:
1615                 impeg2d_dec_itu_t_ext(ps_dec);
1616                 break;
1617             case PIC_SPATIAL_SCALABLE_EXT_ID:
1618             case PIC_TEMPORAL_SCALABLE_EXT_ID:
1619                 e_error = IMPEG2D_SCALABLITY_NOT_SUP;
1620                 break;
1621             default:
1622                 /* In case its a reserved extension code */
1623                 impeg2d_bit_stream_flush(ps_stream,EXT_ID_LEN);
1624                 impeg2d_next_start_code(ps_dec);
1625                 break;
1626             }
1627         }
1628         u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1629     }
1630     return e_error;
1631 }
1632 
1633 /*******************************************************************************
1634 *
1635 *  Function Name   : impeg2d_process_video_header
1636 *
1637 *  Description     : Processes video sequence header information
1638 *
1639 *  Arguments       :
1640 *  dec             : Decoder context
1641 *
1642 *  Values Returned : None
1643 *******************************************************************************/
impeg2d_process_video_header(dec_state_t * ps_dec)1644 IMPEG2D_ERROR_CODES_T impeg2d_process_video_header(dec_state_t *ps_dec)
1645 {
1646     stream_t *ps_stream;
1647     ps_stream = &ps_dec->s_bit_stream;
1648     IMPEG2D_ERROR_CODES_T e_error;
1649 
1650     impeg2d_next_code(ps_dec, SEQUENCE_HEADER_CODE);
1651     if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1652     {
1653         e_error = impeg2d_dec_seq_hdr(ps_dec);
1654         if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1655         {
1656             return e_error;
1657         }
1658     }
1659     else
1660     {
1661       return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1662     }
1663     if (impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) == EXTENSION_START_CODE)
1664     {
1665         /* MPEG2 Decoder */
1666         if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1667         {
1668             e_error = impeg2d_dec_seq_ext(ps_dec);
1669             if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1670             {
1671                 return e_error;
1672             }
1673         }
1674         else
1675         {
1676           return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1677         }
1678         if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1679         {
1680             e_error = impeg2d_dec_seq_ext_data(ps_dec);
1681             if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1682             {
1683                 return e_error;
1684             }
1685         }
1686         return impeg2d_init_video_state(ps_dec,MPEG_2_VIDEO);
1687     }
1688     else
1689     {
1690          /* MPEG1 Decoder */
1691         if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1692         {
1693             impeg2d_flush_ext_and_user_data(ps_dec);
1694         }
1695         return impeg2d_init_video_state(ps_dec,MPEG_1_VIDEO);
1696     }
1697 }
1698 /*******************************************************************************
1699 *
1700 *  Function Name   : impeg2d_process_video_bit_stream
1701 *
1702 *  Description     : Processes video sequence header information
1703 *
1704 *  Arguments       :
1705 *  dec             : Decoder context
1706 *
1707 *  Values Returned : None
1708 *******************************************************************************/
impeg2d_process_video_bit_stream(dec_state_t * ps_dec)1709 IMPEG2D_ERROR_CODES_T impeg2d_process_video_bit_stream(dec_state_t *ps_dec)
1710 {
1711     stream_t *ps_stream;
1712     UWORD32 u4_next_bits, u4_start_code_found;
1713     IMPEG2D_ERROR_CODES_T e_error;
1714 
1715     ps_stream = &ps_dec->s_bit_stream;
1716     impeg2d_next_start_code(ps_dec);
1717     /* If the stream is MPEG-2 compliant stream */
1718     u4_start_code_found = 0;
1719 
1720     if(ps_dec->u2_is_mpeg2)
1721     {
1722         /* MPEG2 decoding starts */
1723         while((u4_start_code_found == 0) && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
1724         {
1725             u4_next_bits = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1726 
1727             if(u4_next_bits == SEQUENCE_HEADER_CODE)
1728             {
1729                 if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1730                 {
1731                     e_error = impeg2d_dec_seq_hdr(ps_dec);
1732                     if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1733                     {
1734                         return e_error;
1735                     }
1736 
1737                     u4_start_code_found = 0;
1738 
1739                 }
1740                 else
1741                 {
1742                     return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1743                 }
1744 
1745 
1746                 if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1747                 {
1748                     IMPEG2D_ERROR_CODES_T e_error;
1749                     e_error = impeg2d_dec_seq_ext(ps_dec);
1750                     if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1751                     {
1752                         return e_error;
1753                     }
1754                     u4_start_code_found = 0;
1755 
1756                 }
1757                 else
1758                 {
1759                     return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1760                 }
1761             }
1762             else if((u4_next_bits == USER_DATA_START_CODE) || (u4_next_bits == EXTENSION_START_CODE))
1763             {
1764                 if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1765                 {
1766                     impeg2d_dec_seq_ext_data(ps_dec);
1767                     u4_start_code_found = 0;
1768 
1769                 }
1770 
1771             }
1772             else if((ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1773                     && (u4_next_bits == GOP_START_CODE))
1774             {
1775                 impeg2d_dec_grp_of_pic_hdr(ps_dec);
1776                 impeg2d_dec_user_data(ps_dec);
1777                 u4_start_code_found = 0;
1778 
1779             }
1780             else if((ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1781                     && (u4_next_bits == PICTURE_START_CODE))
1782             {
1783                 ps_dec->i4_pic_count++;
1784 
1785                 e_error = impeg2d_dec_pic_hdr(ps_dec);
1786                 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1787                 {
1788                     return e_error;
1789                 }
1790                 e_error = impeg2d_dec_pic_coding_ext(ps_dec);
1791                 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1792                 {
1793                     return e_error;
1794                 }
1795                 e_error = impeg2d_dec_pic_ext_data(ps_dec);
1796                 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1797                 {
1798                     return e_error;
1799                 }
1800                 e_error = impeg2d_pre_pic_dec_proc(ps_dec);
1801                 if ((IMPEG2D_ERROR_CODES_T) IVD_ERROR_NONE != e_error)
1802                 {
1803                     return e_error;
1804                 }
1805                 impeg2d_dec_pic_data(ps_dec);
1806                 impeg2d_post_pic_dec_proc(ps_dec);
1807                 u4_start_code_found = 1;
1808             }
1809             else
1810 
1811             {
1812                 FLUSH_BITS(ps_dec->s_bit_stream.u4_offset, ps_dec->s_bit_stream.u4_buf, ps_dec->s_bit_stream.u4_buf_nxt, 8, ps_dec->s_bit_stream.pu4_buf_aligned);
1813 
1814             }
1815             if(u4_start_code_found == 0)
1816             {
1817                 impeg2d_next_start_code(ps_dec);
1818                 /* In case a dec_pic_data call has not been made, the number of
1819                  * bytes consumed in the previous header decode has to be
1820                  * consumed. Not consuming it will result in zero bytes consumed
1821                  * loops in case there are multiple headers and the second
1822                  * or a future header has a resolution change/other error where
1823                  * the bytes of the last header are not consumed.
1824                  */
1825                 ps_dec->i4_bytes_consumed = (ps_dec->s_bit_stream.u4_offset + 7) >> 3;
1826                 ps_dec->i4_bytes_consumed -= ((size_t)ps_dec->s_bit_stream.pv_bs_buf & 3);
1827             }
1828         }
1829         if((u4_start_code_found == 0) && (ps_dec->s_bit_stream.u4_offset > ps_dec->s_bit_stream.u4_max_offset))
1830         {
1831             return IMPEG2D_FRM_HDR_START_CODE_NOT_FOUND;
1832         }
1833 
1834     }
1835         /* If the stream is MPEG-1 compliant stream */
1836     else
1837     {
1838         while((u4_start_code_found == 0) && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
1839         {
1840             u4_next_bits = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1841 
1842             if(impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) == SEQUENCE_HEADER_CODE)
1843             {
1844                 if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1845                 {
1846                     e_error = impeg2d_dec_seq_hdr(ps_dec);
1847                     if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1848                     {
1849                         return e_error;
1850                     }
1851 
1852                     u4_start_code_found = 0;
1853                 }
1854                 else
1855                 {
1856                     return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1857                 }
1858             }
1859             else if((ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset) && (u4_next_bits == EXTENSION_START_CODE || u4_next_bits == USER_DATA_START_CODE))
1860             {
1861                 impeg2d_flush_ext_and_user_data(ps_dec);
1862                 u4_start_code_found = 0;
1863             }
1864 
1865 
1866             else if ((impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) == GOP_START_CODE)
1867                     && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
1868             {
1869                 impeg2d_dec_grp_of_pic_hdr(ps_dec);
1870                 impeg2d_flush_ext_and_user_data(ps_dec);
1871                 u4_start_code_found = 0;
1872             }
1873             else if ((impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) == PICTURE_START_CODE)
1874                     && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
1875             {
1876                 ps_dec->i4_pic_count++;
1877 
1878                 e_error = impeg2d_dec_pic_hdr(ps_dec);
1879                 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1880                 {
1881                     return e_error;
1882                 }
1883                 impeg2d_flush_ext_and_user_data(ps_dec);
1884                 impeg2d_pre_pic_dec_proc(ps_dec);
1885                 impeg2d_dec_pic_data(ps_dec);
1886                 impeg2d_post_pic_dec_proc(ps_dec);
1887                 u4_start_code_found = 1;
1888             }
1889             else
1890             {
1891                 FLUSH_BITS(ps_dec->s_bit_stream.u4_offset, ps_dec->s_bit_stream.u4_buf, ps_dec->s_bit_stream.u4_buf_nxt, 8, ps_dec->s_bit_stream.pu4_buf_aligned);
1892             }
1893             impeg2d_next_start_code(ps_dec);
1894             if (0 == u4_start_code_found)
1895             {
1896                 /* In case a dec_pic_data call has not been made, the number of
1897                  * bytes consumed in the previous header decode has to be
1898                  * consumed. Not consuming it will result in zero bytes consumed
1899                  * loops in case there are multiple headers and the second
1900                  * or a future header has a resolution change/other error where
1901                  * the bytes of the last header are not consumed.
1902                  */
1903                 ps_dec->i4_bytes_consumed = (ps_dec->s_bit_stream.u4_offset + 7) >> 3;
1904                 ps_dec->i4_bytes_consumed -= ((size_t)ps_dec->s_bit_stream.pv_bs_buf & 3);
1905             }
1906         }
1907         if((u4_start_code_found == 0) && (ps_dec->s_bit_stream.u4_offset > ps_dec->s_bit_stream.u4_max_offset))
1908         {
1909            return IMPEG2D_FRM_HDR_START_CODE_NOT_FOUND;
1910         }
1911     }
1912 
1913     return (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
1914 }
1915