• 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 <log/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 
766     UWORD32 u4_val;
767     stream_t *ps_stream;
768     IMPEG2D_ERROR_CODES_T e_error = (IMPEG2D_ERROR_CODES_T) IV_SUCCESS;
769 
770     ps_stream = &ps_dec->s_bit_stream;
771     impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
772     /* extension code identifier */
773     impeg2d_bit_stream_get(ps_stream,4);
774 
775     u4_val = impeg2d_bit_stream_get(ps_stream,4);
776     if(u4_val == 0)
777         return IMPEG2D_UNKNOWN_ERROR;
778     ps_dec->au2_f_code[0][0]             = u4_val;
779 
780     u4_val = impeg2d_bit_stream_get(ps_stream,4);
781     if(u4_val == 0)
782         return IMPEG2D_UNKNOWN_ERROR;
783     ps_dec->au2_f_code[0][1]             = u4_val;
784 
785     u4_val = impeg2d_bit_stream_get(ps_stream,4);
786     if(u4_val == 0)
787         return IMPEG2D_UNKNOWN_ERROR;
788     ps_dec->au2_f_code[1][0]             = u4_val;
789 
790     u4_val = impeg2d_bit_stream_get(ps_stream,4);
791     if(u4_val == 0)
792         return IMPEG2D_UNKNOWN_ERROR;
793     ps_dec->au2_f_code[1][1]             = u4_val;
794 
795     ps_dec->u2_intra_dc_precision        = impeg2d_bit_stream_get(ps_stream,2);
796     ps_dec->u2_picture_structure            = impeg2d_bit_stream_get(ps_stream,2);
797     if (ps_dec->u2_picture_structure < TOP_FIELD ||
798                     ps_dec->u2_picture_structure > FRAME_PICTURE)
799     {
800         return IMPEG2D_FRM_HDR_DECODE_ERR;
801     }
802     ps_dec->u2_top_field_first              = impeg2d_bit_stream_get_bit(ps_stream);
803     ps_dec->u2_frame_pred_frame_dct         = impeg2d_bit_stream_get_bit(ps_stream);
804     ps_dec->u2_concealment_motion_vectors   = impeg2d_bit_stream_get_bit(ps_stream);
805     ps_dec->u2_q_scale_type                 = impeg2d_bit_stream_get_bit(ps_stream);
806     ps_dec->u2_intra_vlc_format             = impeg2d_bit_stream_get_bit(ps_stream);
807     ps_dec->u2_alternate_scan               = impeg2d_bit_stream_get_bit(ps_stream);
808     ps_dec->u2_repeat_first_field           = impeg2d_bit_stream_get_bit(ps_stream);
809     /* Flush chroma_420_type */
810     impeg2d_bit_stream_get_bit(ps_stream);
811 
812     ps_dec->u2_progressive_frame            = impeg2d_bit_stream_get_bit(ps_stream);
813     if (impeg2d_bit_stream_get_bit(ps_stream))
814     {
815         /* Flush v_axis, field_sequence, burst_amplitude, sub_carrier_phase */
816         impeg2d_bit_stream_flush(ps_stream,20);
817     }
818     impeg2d_next_start_code(ps_dec);
819 
820 
821     if(VERTICAL_SCAN == ps_dec->u2_alternate_scan)
822     {
823         ps_dec->pu1_inv_scan_matrix = (UWORD8 *)gau1_impeg2_inv_scan_vertical;
824     }
825     else
826     {
827         ps_dec->pu1_inv_scan_matrix = (UWORD8 *)gau1_impeg2_inv_scan_zig_zag;
828     }
829     return e_error;
830 }
831 
832 /*******************************************************************************
833 *
834 *  Function Name   : impeg2d_dec_slice
835 *
836 *  Description     : Reads Slice level parameters and calls functions that
837 *                    decode individual MBs of slice
838 *
839 *  Arguments       :
840 *  dec             : Decoder context
841 *
842 *  Values Returned : None
843 *******************************************************************************/
impeg2d_dec_slice(dec_state_t * ps_dec)844 IMPEG2D_ERROR_CODES_T impeg2d_dec_slice(dec_state_t *ps_dec)
845 {
846     stream_t *ps_stream;
847     UWORD32 u4_slice_vertical_position;
848     UWORD32 u4_slice_vertical_position_extension;
849     IMPEG2D_ERROR_CODES_T e_error;
850 
851     ps_stream = &ps_dec->s_bit_stream;
852 
853     /*------------------------------------------------------------------------*/
854     /* All the profiles supported require restricted slice structure. Hence   */
855     /* there is no need to store slice_vertical_position. Note that max       */
856     /* height supported does not exceed 2800 and scalablity is not supported  */
857     /*------------------------------------------------------------------------*/
858 
859     /* Remove the slice start code */
860     impeg2d_bit_stream_flush(ps_stream,START_CODE_PREFIX_LEN);
861     u4_slice_vertical_position = impeg2d_bit_stream_get(ps_stream, 8);
862     if(u4_slice_vertical_position > 2800)
863     {
864         u4_slice_vertical_position_extension = impeg2d_bit_stream_get(ps_stream, 3);
865         u4_slice_vertical_position += (u4_slice_vertical_position_extension << 7);
866     }
867 
868     if((u4_slice_vertical_position > ps_dec->u2_num_vert_mb) ||
869        (u4_slice_vertical_position == 0))
870     {
871         return IMPEG2D_INVALID_VERT_SIZE;
872     }
873 
874     // change the mb_y to point to slice_vertical_position
875     u4_slice_vertical_position--;
876     if (ps_dec->u2_mb_y != u4_slice_vertical_position)
877     {
878         ps_dec->u2_mb_y    = u4_slice_vertical_position;
879         ps_dec->u2_mb_x    = 0;
880 
881         /* Update the number of MBs left, since we have probably missed a slice
882          * (that's why we see a mismatch between u2_mb_y and current position).
883          */
884         ps_dec->u2_num_mbs_left = (ps_dec->u2_num_vert_mb - ps_dec->u2_mb_y)
885                         * ps_dec->u2_num_horiz_mb;
886     }
887     ps_dec->u2_first_mb = 1;
888 
889     /*------------------------------------------------------------------------*/
890     /* Quant scale code decoding                                              */
891     /*------------------------------------------------------------------------*/
892     {
893         UWORD16 u2_quant_scale_code;
894         u2_quant_scale_code = impeg2d_bit_stream_get(ps_stream,5);
895         ps_dec->u1_quant_scale = (ps_dec->u2_q_scale_type) ?
896             gau1_impeg2_non_linear_quant_scale[u2_quant_scale_code] : (u2_quant_scale_code << 1);
897     }
898 
899     if (impeg2d_bit_stream_nxt(ps_stream,1) == 1)
900     {
901         impeg2d_bit_stream_flush(ps_stream,9);
902         /* Flush extra bit information */
903         while (impeg2d_bit_stream_nxt(ps_stream,1) == 1 &&
904                ps_stream->u4_offset < ps_stream->u4_max_offset)
905         {
906             impeg2d_bit_stream_flush(ps_stream,9);
907         }
908     }
909     impeg2d_bit_stream_get_bit(ps_stream);
910 
911     /* Reset the DC predictors to reset values given in Table 7.2 at the start*/
912     /* of slice data */
913     ps_dec->u2_def_dc_pred[Y_LUMA]   = 128 << ps_dec->u2_intra_dc_precision;
914     ps_dec->u2_def_dc_pred[U_CHROMA]   = 128 << ps_dec->u2_intra_dc_precision;
915     ps_dec->u2_def_dc_pred[V_CHROMA]   = 128 << ps_dec->u2_intra_dc_precision;
916     /*------------------------------------------------------------------------*/
917     /* dec->DecMBsinSlice() implements the following psuedo code from standard*/
918     /* do                                                                     */
919     /* {                                                                      */
920     /*      macroblock()                                                      */
921     /* } while (impeg2d_bit_stream_nxt() != '000 0000 0000 0000 0000 0000')      */
922     /*------------------------------------------------------------------------*/
923 
924     e_error = ps_dec->pf_decode_slice(ps_dec);
925     if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
926     {
927         return e_error;
928     }
929 
930     /* Check for the MBy index instead of number of MBs left, because the
931      * number of MBs left in case of multi-thread decode is the number of MBs
932      * in that row only
933      */
934     if(ps_dec->u2_mb_y < ps_dec->u2_num_vert_mb)
935         impeg2d_next_start_code(ps_dec);
936 
937     return (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
938 }
939 
impeg2d_dec_pic_data_thread(dec_state_t * ps_dec)940 void impeg2d_dec_pic_data_thread(dec_state_t *ps_dec)
941 {
942     WORD32 i4_continue_decode;
943 
944     WORD32 i4_cur_row, temp;
945     UWORD32 u4_bits_read;
946     WORD32 i4_dequeue_job;
947     IMPEG2D_ERROR_CODES_T e_error;
948 
949     i4_cur_row = ps_dec->u2_mb_y + 1;
950 
951     i4_continue_decode = 1;
952 
953     i4_dequeue_job = 1;
954     do
955     {
956         if(i4_cur_row > ps_dec->u2_num_vert_mb)
957         {
958             i4_continue_decode = 0;
959             break;
960         }
961 
962         {
963             if((ps_dec->i4_num_cores> 1) && (i4_dequeue_job))
964             {
965                 job_t s_job;
966                 IV_API_CALL_STATUS_T e_ret;
967                 UWORD8 *pu1_buf;
968 
969                 e_ret = impeg2_jobq_dequeue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 1);
970                 if(e_ret != IV_SUCCESS)
971                     break;
972 
973                 if(CMD_PROCESS == s_job.i4_cmd)
974                 {
975                     pu1_buf = ps_dec->pu1_inp_bits_buf + s_job.i4_bistream_ofst;
976                     impeg2d_bit_stream_init(&(ps_dec->s_bit_stream), pu1_buf,
977                             (ps_dec->u4_num_inp_bytes - s_job.i4_bistream_ofst));
978                     i4_cur_row      = s_job.i2_start_mb_y;
979                     ps_dec->i4_start_mb_y = s_job.i2_start_mb_y;
980                     ps_dec->i4_end_mb_y = s_job.i2_end_mb_y;
981                     ps_dec->u2_mb_x = 0;
982                     ps_dec->u2_mb_y = ps_dec->i4_start_mb_y;
983                     ps_dec->u2_num_mbs_left = (ps_dec->i4_end_mb_y - ps_dec->i4_start_mb_y) * ps_dec->u2_num_horiz_mb;
984 
985                 }
986                 else
987                 {
988                     WORD32 start_row;
989                     WORD32 num_rows;
990                     start_row = s_job.i2_start_mb_y << 4;
991                     num_rows = MIN((s_job.i2_end_mb_y << 4), ps_dec->u2_vertical_size);
992                     num_rows -= start_row;
993 
994                     if(ps_dec->u4_deinterlace && (0 == ps_dec->u2_progressive_frame))
995                     {
996                         impeg2d_deinterlace(ps_dec,
997                                             ps_dec->ps_disp_pic,
998                                             ps_dec->ps_disp_frm_buf,
999                                             start_row,
1000                                             num_rows);
1001 
1002                     }
1003                     else
1004                     {
1005                         impeg2d_format_convert(ps_dec, ps_dec->ps_disp_pic,
1006                                                ps_dec->ps_disp_frm_buf,
1007                                                start_row, num_rows);
1008                     }
1009                     break;
1010 
1011                 }
1012 
1013             }
1014             e_error = impeg2d_dec_slice(ps_dec);
1015 
1016             if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1017             {
1018                 impeg2d_next_start_code(ps_dec);
1019                 if(ps_dec->s_bit_stream.u4_offset >= ps_dec->s_bit_stream.u4_max_offset)
1020                 {
1021                     ps_dec->u4_error_code = IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1022                     return;
1023                 }
1024             }
1025         }
1026 
1027         /* Detecting next slice start code */
1028         while(1)
1029         {
1030             // skip (dec->u4_num_cores-1) rows
1031             u4_bits_read = impeg2d_bit_stream_nxt(&ps_dec->s_bit_stream,START_CODE_LEN);
1032             temp = u4_bits_read & 0xFF;
1033             i4_continue_decode = (((u4_bits_read >> 8) == 0x01) && (temp) && (temp <= 0xAF));
1034 
1035             if (1 == ps_dec->i4_num_cores && 0 == ps_dec->u2_num_mbs_left)
1036             {
1037                 i4_continue_decode = 0;
1038 #ifdef __ANDROID__
1039                 android_errorWriteLog(0x534e4554, "26070014");
1040 #endif
1041             }
1042 
1043             if(i4_continue_decode)
1044             {
1045                 if (0 != ps_dec->u2_num_mbs_left)
1046                 {
1047                     /* If the slice is from the same row, then continue decoding without dequeue */
1048                     if((temp - 1) == i4_cur_row)
1049                     {
1050                         i4_dequeue_job = 0;
1051                     }
1052                     else
1053                     {
1054                         if(temp < ps_dec->i4_end_mb_y)
1055                         {
1056                             i4_cur_row = ps_dec->u2_mb_y;
1057                         }
1058                         else
1059                         {
1060                             i4_dequeue_job = 1;
1061                         }
1062                     }
1063                 }
1064                 else
1065                 {
1066                     i4_dequeue_job = 1;
1067                 }
1068                 break;
1069             }
1070             else
1071                 break;
1072         }
1073 
1074     }while(i4_continue_decode);
1075     if(ps_dec->i4_num_cores > 1)
1076     {
1077         while(1)
1078         {
1079             job_t s_job;
1080             IV_API_CALL_STATUS_T e_ret;
1081 
1082             e_ret = impeg2_jobq_dequeue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 1);
1083             if(e_ret != IV_SUCCESS)
1084                 break;
1085             if(CMD_FMTCONV == s_job.i4_cmd)
1086             {
1087                 WORD32 start_row;
1088                 WORD32 num_rows;
1089                 start_row = s_job.i2_start_mb_y << 4;
1090                 num_rows = MIN((s_job.i2_end_mb_y << 4), ps_dec->u2_vertical_size);
1091                 num_rows -= start_row;
1092                 if(ps_dec->u4_deinterlace && (0 == ps_dec->u2_progressive_frame))
1093                 {
1094                     impeg2d_deinterlace(ps_dec,
1095                                         ps_dec->ps_disp_pic,
1096                                         ps_dec->ps_disp_frm_buf,
1097                                         start_row,
1098                                         num_rows);
1099 
1100                 }
1101                 else
1102                 {
1103                     impeg2d_format_convert(ps_dec,
1104                                            ps_dec->ps_disp_pic,
1105                                            ps_dec->ps_disp_frm_buf,
1106                                            start_row,
1107                                            num_rows);
1108                 }
1109             }
1110         }
1111     }
1112     else
1113     {
1114         if((NULL != ps_dec->ps_disp_pic) && ((0 == ps_dec->u4_share_disp_buf) || (IV_YUV_420P != ps_dec->i4_chromaFormat)))
1115         {
1116             if(ps_dec->u4_deinterlace && (0 == ps_dec->u2_progressive_frame))
1117             {
1118                 impeg2d_deinterlace(ps_dec,
1119                                     ps_dec->ps_disp_pic,
1120                                     ps_dec->ps_disp_frm_buf,
1121                                     0,
1122                                     ps_dec->u2_vertical_size);
1123 
1124             }
1125             else
1126             {
1127                 impeg2d_format_convert(ps_dec, ps_dec->ps_disp_pic,
1128                                         ps_dec->ps_disp_frm_buf,
1129                                         0, ps_dec->u2_vertical_size);
1130             }
1131         }
1132     }
1133 }
1134 
impeg2d_init_thread_dec_ctxt(dec_state_t * ps_dec,dec_state_t * ps_dec_thd,WORD32 i4_min_mb_y)1135 static WORD32 impeg2d_init_thread_dec_ctxt(dec_state_t *ps_dec,
1136                                            dec_state_t *ps_dec_thd,
1137                                            WORD32 i4_min_mb_y)
1138 {
1139     UNUSED(i4_min_mb_y);
1140     ps_dec_thd->i4_start_mb_y = 0;
1141     ps_dec_thd->i4_end_mb_y = ps_dec->u2_num_vert_mb;
1142     ps_dec_thd->u2_mb_x = 0;
1143     ps_dec_thd->u2_mb_y = 0;
1144     ps_dec_thd->u2_is_mpeg2 = ps_dec->u2_is_mpeg2;
1145     ps_dec_thd->i4_pic_count = ps_dec->i4_pic_count;
1146     ps_dec_thd->u2_frame_width = ps_dec->u2_frame_width;
1147     ps_dec_thd->u2_frame_height = ps_dec->u2_frame_height;
1148     ps_dec_thd->u2_picture_width = ps_dec->u2_picture_width;
1149     ps_dec_thd->u2_horizontal_size = ps_dec->u2_horizontal_size;
1150     ps_dec_thd->u2_vertical_size = ps_dec->u2_vertical_size;
1151     ps_dec_thd->u2_create_max_width = ps_dec->u2_create_max_width;
1152     ps_dec_thd->u2_create_max_height = ps_dec->u2_create_max_height;
1153     ps_dec_thd->u2_header_done = ps_dec->u2_header_done;
1154     ps_dec_thd->u2_decode_header = ps_dec->u2_decode_header;
1155 
1156     ps_dec_thd->u2_num_horiz_mb = ps_dec->u2_num_horiz_mb;
1157     ps_dec_thd->u2_num_vert_mb = ps_dec->u2_num_vert_mb;
1158     ps_dec_thd->u2_num_flds_decoded = ps_dec->u2_num_flds_decoded;
1159 
1160     ps_dec_thd->u4_frm_buf_stride = ps_dec->u4_frm_buf_stride;
1161 
1162     ps_dec_thd->u2_field_dct = ps_dec->u2_field_dct;
1163     ps_dec_thd->u2_read_dct_type = ps_dec->u2_read_dct_type;
1164 
1165     ps_dec_thd->u2_read_motion_type = ps_dec->u2_read_motion_type;
1166     ps_dec_thd->u2_motion_type = ps_dec->u2_motion_type;
1167 
1168     ps_dec_thd->pu2_mb_type = ps_dec->pu2_mb_type;
1169     ps_dec_thd->u2_fld_pic = ps_dec->u2_fld_pic;
1170     ps_dec_thd->u2_frm_pic = ps_dec->u2_frm_pic;
1171 
1172     ps_dec_thd->u2_fld_parity = ps_dec->u2_fld_parity;
1173 
1174     ps_dec_thd->au2_fcode_data[0] = ps_dec->au2_fcode_data[0];
1175     ps_dec_thd->au2_fcode_data[1] = ps_dec->au2_fcode_data[1];
1176 
1177     ps_dec_thd->u1_quant_scale = ps_dec->u1_quant_scale;
1178 
1179     ps_dec_thd->u2_num_mbs_left = ps_dec->u2_num_mbs_left;
1180     ps_dec_thd->u2_first_mb = ps_dec->u2_first_mb;
1181     ps_dec_thd->u2_num_skipped_mbs = ps_dec->u2_num_skipped_mbs;
1182 
1183     memcpy(&ps_dec_thd->s_cur_frm_buf, &ps_dec->s_cur_frm_buf, sizeof(yuv_buf_t));
1184     memcpy(&ps_dec_thd->as_recent_fld[0][0], &ps_dec->as_recent_fld[0][0], sizeof(yuv_buf_t));
1185     memcpy(&ps_dec_thd->as_recent_fld[0][1], &ps_dec->as_recent_fld[0][1], sizeof(yuv_buf_t));
1186     memcpy(&ps_dec_thd->as_recent_fld[1][0], &ps_dec->as_recent_fld[1][0], sizeof(yuv_buf_t));
1187     memcpy(&ps_dec_thd->as_recent_fld[1][1], &ps_dec->as_recent_fld[1][1], sizeof(yuv_buf_t));
1188     memcpy(&ps_dec_thd->as_ref_buf, &ps_dec->as_ref_buf, sizeof(yuv_buf_t) * 2 * 2);
1189 
1190 
1191     ps_dec_thd->pf_decode_slice = ps_dec->pf_decode_slice;
1192 
1193     ps_dec_thd->pf_vld_inv_quant = ps_dec->pf_vld_inv_quant;
1194 
1195     memcpy(ps_dec_thd->pf_idct_recon, ps_dec->pf_idct_recon, sizeof(ps_dec->pf_idct_recon));
1196 
1197     memcpy(ps_dec_thd->pf_mc, ps_dec->pf_mc, sizeof(ps_dec->pf_mc));
1198     ps_dec_thd->pf_interpolate = ps_dec->pf_interpolate;
1199     ps_dec_thd->pf_copy_mb = ps_dec->pf_copy_mb;
1200     ps_dec_thd->pf_fullx_halfy_8x8              =  ps_dec->pf_fullx_halfy_8x8;
1201     ps_dec_thd->pf_halfx_fully_8x8              =  ps_dec->pf_halfx_fully_8x8;
1202     ps_dec_thd->pf_halfx_halfy_8x8              =  ps_dec->pf_halfx_halfy_8x8;
1203     ps_dec_thd->pf_fullx_fully_8x8              =  ps_dec->pf_fullx_fully_8x8;
1204 
1205     ps_dec_thd->pf_memset_8bit_8x8_block        =  ps_dec->pf_memset_8bit_8x8_block;
1206     ps_dec_thd->pf_memset_16bit_8x8_linear_block        =  ps_dec->pf_memset_16bit_8x8_linear_block;
1207     ps_dec_thd->pf_copy_yuv420p_buf             =   ps_dec->pf_copy_yuv420p_buf;
1208     ps_dec_thd->pf_fmt_conv_yuv420p_to_yuv422ile    =   ps_dec->pf_fmt_conv_yuv420p_to_yuv422ile;
1209     ps_dec_thd->pf_fmt_conv_yuv420p_to_yuv420sp_uv  =   ps_dec->pf_fmt_conv_yuv420p_to_yuv420sp_uv;
1210     ps_dec_thd->pf_fmt_conv_yuv420p_to_yuv420sp_vu  =   ps_dec->pf_fmt_conv_yuv420p_to_yuv420sp_vu;
1211 
1212 
1213     memcpy(ps_dec_thd->au1_intra_quant_matrix, ps_dec->au1_intra_quant_matrix, NUM_PELS_IN_BLOCK * sizeof(UWORD8));
1214     memcpy(ps_dec_thd->au1_inter_quant_matrix, ps_dec->au1_inter_quant_matrix, NUM_PELS_IN_BLOCK * sizeof(UWORD8));
1215     ps_dec_thd->pu1_inv_scan_matrix = ps_dec->pu1_inv_scan_matrix;
1216 
1217 
1218     ps_dec_thd->u2_progressive_sequence = ps_dec->u2_progressive_sequence;
1219     ps_dec_thd->e_pic_type =  ps_dec->e_pic_type;
1220     ps_dec_thd->u2_full_pel_forw_vector = ps_dec->u2_full_pel_forw_vector;
1221     ps_dec_thd->u2_forw_f_code =   ps_dec->u2_forw_f_code;
1222     ps_dec_thd->u2_full_pel_back_vector = ps_dec->u2_full_pel_back_vector;
1223     ps_dec_thd->u2_back_f_code = ps_dec->u2_back_f_code;
1224 
1225     memcpy(ps_dec_thd->ai2_mv, ps_dec->ai2_mv, (2*2*2)*sizeof(WORD16));
1226     memcpy(ps_dec_thd->au2_f_code, ps_dec->au2_f_code, (2*2)*sizeof(UWORD16));
1227     ps_dec_thd->u2_intra_dc_precision = ps_dec->u2_intra_dc_precision;
1228     ps_dec_thd->u2_picture_structure = ps_dec->u2_picture_structure;
1229     ps_dec_thd->u2_top_field_first = ps_dec->u2_top_field_first;
1230     ps_dec_thd->u2_frame_pred_frame_dct = ps_dec->u2_frame_pred_frame_dct;
1231     ps_dec_thd->u2_concealment_motion_vectors = ps_dec->u2_concealment_motion_vectors;
1232     ps_dec_thd->u2_q_scale_type =  ps_dec->u2_q_scale_type;
1233     ps_dec_thd->u2_intra_vlc_format = ps_dec->u2_intra_vlc_format;
1234     ps_dec_thd->u2_alternate_scan = ps_dec->u2_alternate_scan;
1235     ps_dec_thd->u2_repeat_first_field = ps_dec->u2_repeat_first_field;
1236     ps_dec_thd->u2_progressive_frame = ps_dec->u2_progressive_frame;
1237     ps_dec_thd->pu1_inp_bits_buf = ps_dec->pu1_inp_bits_buf;
1238     ps_dec_thd->u4_num_inp_bytes = ps_dec->u4_num_inp_bytes;
1239     ps_dec_thd->pv_jobq = ps_dec->pv_jobq;
1240     ps_dec_thd->pv_jobq_buf = ps_dec->pv_jobq_buf;
1241     ps_dec_thd->i4_jobq_buf_size = ps_dec->i4_jobq_buf_size;
1242 
1243 
1244     ps_dec_thd->u2_frame_rate_code = ps_dec->u2_frame_rate_code;
1245     ps_dec_thd->u2_frame_rate_extension_n = ps_dec->u2_frame_rate_extension_n;
1246     ps_dec_thd->u2_frame_rate_extension_d = ps_dec->u2_frame_rate_extension_d;
1247     ps_dec_thd->u2_framePeriod =   ps_dec->u2_framePeriod;
1248     ps_dec_thd->u2_display_horizontal_size = ps_dec->u2_display_horizontal_size;
1249     ps_dec_thd->u2_display_vertical_size = ps_dec->u2_display_vertical_size;
1250     ps_dec_thd->u2_aspect_ratio_info = ps_dec->u2_aspect_ratio_info;
1251 
1252     ps_dec_thd->ps_func_bi_direct = ps_dec->ps_func_bi_direct;
1253     ps_dec_thd->ps_func_forw_or_back = ps_dec->ps_func_forw_or_back;
1254     ps_dec_thd->pv_deinterlacer_ctxt = ps_dec->pv_deinterlacer_ctxt;
1255     ps_dec_thd->ps_deint_pic = ps_dec->ps_deint_pic;
1256 
1257     return 0;
1258 }
1259 
1260 
impeg2d_get_slice_pos(dec_state_multi_core_t * ps_dec_state_multi_core)1261 WORD32 impeg2d_get_slice_pos(dec_state_multi_core_t *ps_dec_state_multi_core)
1262 {
1263     WORD32 u4_bits;
1264     WORD32 i4_row;
1265 
1266 
1267     dec_state_t *ps_dec = ps_dec_state_multi_core->ps_dec_state[0];
1268     WORD32 i4_prev_row;
1269     stream_t s_bitstrm;
1270     WORD32 i4_start_row;
1271     WORD32 i4_slice_bistream_ofst;
1272     WORD32 i;
1273     s_bitstrm = ps_dec->s_bit_stream;
1274     i4_prev_row = -1;
1275 
1276     ps_dec_state_multi_core->ps_dec_state[0]->i4_start_mb_y = 0;
1277     ps_dec_state_multi_core->ps_dec_state[1]->i4_start_mb_y = -1;
1278     ps_dec_state_multi_core->ps_dec_state[2]->i4_start_mb_y = -1;
1279     ps_dec_state_multi_core->ps_dec_state[3]->i4_start_mb_y = -1;
1280 
1281     ps_dec_state_multi_core->ps_dec_state[0]->i4_end_mb_y = ps_dec->u2_num_vert_mb;
1282     ps_dec_state_multi_core->ps_dec_state[1]->i4_end_mb_y = -1;
1283     ps_dec_state_multi_core->ps_dec_state[2]->i4_end_mb_y = -1;
1284     ps_dec_state_multi_core->ps_dec_state[3]->i4_end_mb_y = -1;
1285 
1286     if(ps_dec->i4_num_cores == 1)
1287         return 0;
1288     /* Reset the jobq to start of the jobq buffer */
1289     impeg2_jobq_reset((jobq_t *)ps_dec->pv_jobq);
1290 
1291     i4_start_row = -1;
1292     i4_slice_bistream_ofst = 0;
1293     while(1)
1294     {
1295         WORD32 i4_is_slice;
1296 
1297         if(s_bitstrm.u4_offset + START_CODE_LEN >= s_bitstrm.u4_max_offset)
1298         {
1299             break;
1300         }
1301         u4_bits = impeg2d_bit_stream_nxt(&s_bitstrm,START_CODE_LEN);
1302 
1303         i4_row = u4_bits & 0xFF;
1304 
1305         /* Detect end of frame */
1306         i4_is_slice = (((u4_bits >> 8) == 0x01) && (i4_row) && (i4_row <= ps_dec->u2_num_vert_mb));
1307         if(!i4_is_slice)
1308             break;
1309 
1310         i4_row -= 1;
1311 
1312 
1313         if(i4_prev_row < i4_row)
1314         {
1315             /* Create a job for previous slice row */
1316             if(i4_start_row != -1)
1317             {
1318                 job_t s_job;
1319                 IV_API_CALL_STATUS_T ret;
1320                 s_job.i2_start_mb_y = i4_start_row;
1321                 s_job.i2_end_mb_y = i4_row;
1322                 s_job.i4_cmd = CMD_PROCESS;
1323                 s_job.i4_bistream_ofst = i4_slice_bistream_ofst;
1324                 ret = impeg2_jobq_queue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 0);
1325                 if(ret != IV_SUCCESS)
1326                     return ret;
1327 
1328             }
1329             /* Store current slice's bitstream offset */
1330             i4_slice_bistream_ofst = s_bitstrm.u4_offset >> 3;
1331             i4_slice_bistream_ofst -= (size_t)s_bitstrm.pv_bs_buf & 3;
1332             i4_prev_row = i4_row;
1333 
1334             /* Store current slice's row position */
1335             i4_start_row = i4_row;
1336 
1337         }
1338 #ifdef __ANDROID__
1339         else if (i4_prev_row > i4_row)
1340         {
1341             android_errorWriteLog(0x534e4554, "26070014");
1342         }
1343 #endif
1344 
1345         impeg2d_bit_stream_flush(&s_bitstrm, START_CODE_LEN);
1346 
1347         // flush bytes till next start code
1348         /* Flush the bytes till a  start code is encountered  */
1349         while(impeg2d_bit_stream_nxt(&s_bitstrm, 24) != START_CODE_PREFIX)
1350         {
1351             impeg2d_bit_stream_get(&s_bitstrm, 8);
1352 
1353             if(s_bitstrm.u4_offset >= s_bitstrm.u4_max_offset)
1354             {
1355                 break;
1356             }
1357         }
1358     }
1359 
1360     /* Create job for the last slice row */
1361     {
1362         job_t s_job;
1363         IV_API_CALL_STATUS_T e_ret;
1364         s_job.i2_start_mb_y = i4_start_row;
1365         s_job.i2_end_mb_y = ps_dec->u2_num_vert_mb;
1366         s_job.i4_cmd = CMD_PROCESS;
1367         s_job.i4_bistream_ofst = i4_slice_bistream_ofst;
1368         e_ret = impeg2_jobq_queue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 0);
1369         if(e_ret != IV_SUCCESS)
1370             return e_ret;
1371 
1372     }
1373     if((NULL != ps_dec->ps_disp_pic) && ((0 == ps_dec->u4_share_disp_buf) || (IV_YUV_420P != ps_dec->i4_chromaFormat)))
1374     {
1375         for(i = 0; i < ps_dec->u2_vertical_size; i+=64)
1376         {
1377             job_t s_job;
1378             IV_API_CALL_STATUS_T ret;
1379             s_job.i2_start_mb_y = i;
1380             s_job.i2_start_mb_y >>= 4;
1381             s_job.i2_end_mb_y = (i + 64);
1382             s_job.i2_end_mb_y >>= 4;
1383             s_job.i4_cmd = CMD_FMTCONV;
1384             s_job.i4_bistream_ofst = 0;
1385             ret = impeg2_jobq_queue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 0);
1386             if(ret != IV_SUCCESS)
1387                 return ret;
1388 
1389         }
1390     }
1391 
1392     impeg2_jobq_terminate(ps_dec->pv_jobq);
1393     ps_dec->i4_bytes_consumed = s_bitstrm.u4_offset >> 3;
1394     ps_dec->i4_bytes_consumed -= ((size_t)s_bitstrm.pv_bs_buf & 3);
1395 
1396     return 0;
1397 }
1398 
1399 /*******************************************************************************
1400 *
1401 *  Function Name   : impeg2d_dec_pic_data
1402 *
1403 *  Description     : It intializes several parameters and decodes a Picture
1404 *                    till any slice is left.
1405 *
1406 *  Arguments       :
1407 *  dec             : Decoder context
1408 *
1409 *  Values Returned : None
1410 *******************************************************************************/
1411 
impeg2d_dec_pic_data(dec_state_t * ps_dec)1412 void impeg2d_dec_pic_data(dec_state_t *ps_dec)
1413 {
1414 
1415     WORD32 i;
1416     dec_state_multi_core_t *ps_dec_state_multi_core;
1417 
1418     dec_state_t *ps_dec_thd;
1419     WORD32 i4_status;
1420     WORD32 i4_min_mb_y;
1421 
1422 
1423     /* Resetting the MB address and MB coordinates at the start of the Frame */
1424     ps_dec->u2_mb_x = ps_dec->u2_mb_y = 0;
1425 
1426     ps_dec_state_multi_core = ps_dec->ps_dec_state_multi_core;
1427     impeg2d_get_slice_pos(ps_dec_state_multi_core);
1428 
1429     i4_min_mb_y = 1;
1430     for(i=0; i < ps_dec->i4_num_cores - 1; i++)
1431     {
1432         // initialize decoder context for thread
1433         // launch dec->u4_num_cores-1 threads
1434 
1435         ps_dec_thd = ps_dec_state_multi_core->ps_dec_state[i+1];
1436 
1437         ps_dec_thd->ps_disp_pic = ps_dec->ps_disp_pic;
1438         ps_dec_thd->ps_disp_frm_buf = ps_dec->ps_disp_frm_buf;
1439 
1440         i4_status = impeg2d_init_thread_dec_ctxt(ps_dec, ps_dec_thd, i4_min_mb_y);
1441         //impeg2d_dec_pic_data_thread(ps_dec_thd);
1442 
1443         if(i4_status == 0)
1444         {
1445             ithread_create(ps_dec_thd->pv_codec_thread_handle, NULL, (void *)impeg2d_dec_pic_data_thread, ps_dec_thd);
1446             ps_dec_state_multi_core->au4_thread_launched[i + 1] = 1;
1447             i4_min_mb_y = ps_dec_thd->u2_mb_y + 1;
1448         }
1449         else
1450         {
1451             ps_dec_state_multi_core->au4_thread_launched[i + 1] = 0;
1452             break;
1453         }
1454     }
1455 
1456     impeg2d_dec_pic_data_thread(ps_dec);
1457 
1458     // wait for threads to complete
1459     for(i=0; i < (ps_dec->i4_num_cores - 1); i++)
1460     {
1461         if(ps_dec_state_multi_core->au4_thread_launched[i + 1] == 1)
1462         {
1463             ps_dec_thd = ps_dec_state_multi_core->ps_dec_state[i+1];
1464             ithread_join(ps_dec_thd->pv_codec_thread_handle, NULL);
1465         }
1466     }
1467 
1468 }
1469 /*******************************************************************************
1470 *
1471 *  Function Name   : impeg2d_flush_ext_and_user_data
1472 *
1473 *  Description     : Flushes the extension and user data present in the
1474 *                    stream_t
1475 *
1476 *  Arguments       :
1477 *  dec             : Decoder context
1478 *
1479 *  Values Returned : None
1480 *******************************************************************************/
impeg2d_flush_ext_and_user_data(dec_state_t * ps_dec)1481 void impeg2d_flush_ext_and_user_data(dec_state_t *ps_dec)
1482 {
1483     UWORD32 u4_start_code;
1484     stream_t *ps_stream;
1485 
1486     ps_stream    = &ps_dec->s_bit_stream;
1487     u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1488 
1489     while((u4_start_code == EXTENSION_START_CODE || u4_start_code == USER_DATA_START_CODE) &&
1490             (ps_stream->u4_offset < ps_stream->u4_max_offset))
1491     {
1492         impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
1493         while(impeg2d_bit_stream_nxt(ps_stream,START_CODE_PREFIX_LEN) != START_CODE_PREFIX &&
1494                 (ps_stream->u4_offset < ps_stream->u4_max_offset))
1495         {
1496             impeg2d_bit_stream_flush(ps_stream,8);
1497         }
1498         u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1499     }
1500 }
1501 /*******************************************************************************
1502 *
1503 *  Function Name   : impeg2d_dec_user_data
1504 *
1505 *  Description     : Flushes the user data present in the stream_t
1506 *
1507 *  Arguments       :
1508 *  dec             : Decoder context
1509 *
1510 *  Values Returned : None
1511 *******************************************************************************/
impeg2d_dec_user_data(dec_state_t * ps_dec)1512 void impeg2d_dec_user_data(dec_state_t *ps_dec)
1513 {
1514     UWORD32 u4_start_code;
1515     stream_t *ps_stream;
1516 
1517     ps_stream    = &ps_dec->s_bit_stream;
1518     u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1519 
1520     while(u4_start_code == USER_DATA_START_CODE)
1521     {
1522         impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
1523         while((impeg2d_bit_stream_nxt(ps_stream,START_CODE_PREFIX_LEN) != START_CODE_PREFIX) &&
1524                 (ps_stream->u4_offset < ps_stream->u4_max_offset))
1525         {
1526             impeg2d_bit_stream_flush(ps_stream,8);
1527         }
1528         u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1529     }
1530 }
1531 /*******************************************************************************
1532 *  Function Name   : impeg2d_dec_seq_ext_data
1533 *
1534 *  Description     : Decodes the extension data following Sequence
1535 *                    Extension. It flushes any user data if present
1536 *
1537 *  Arguments       :
1538 *  dec             : Decoder context
1539 *
1540 *  Values Returned : None
1541 *******************************************************************************/
impeg2d_dec_seq_ext_data(dec_state_t * ps_dec)1542 IMPEG2D_ERROR_CODES_T impeg2d_dec_seq_ext_data(dec_state_t *ps_dec)
1543 {
1544     stream_t   *ps_stream;
1545     UWORD32     u4_start_code;
1546     IMPEG2D_ERROR_CODES_T e_error;
1547 
1548     e_error = (IMPEG2D_ERROR_CODES_T) IVD_ERROR_NONE;
1549 
1550     ps_stream      = &ps_dec->s_bit_stream;
1551     u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1552     while( (u4_start_code == EXTENSION_START_CODE ||
1553             u4_start_code == USER_DATA_START_CODE) &&
1554             (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE == e_error &&
1555             (ps_stream->u4_offset < ps_stream->u4_max_offset))
1556     {
1557         if(u4_start_code == USER_DATA_START_CODE)
1558         {
1559             impeg2d_dec_user_data(ps_dec);
1560         }
1561         else
1562         {
1563             impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
1564             u4_start_code   = impeg2d_bit_stream_nxt(ps_stream,EXT_ID_LEN);
1565             switch(u4_start_code)
1566             {
1567             case SEQ_DISPLAY_EXT_ID:
1568                 impeg2d_dec_seq_disp_ext(ps_dec);
1569                 break;
1570             case SEQ_SCALABLE_EXT_ID:
1571                 e_error = IMPEG2D_SCALABILITIY_NOT_SUPPORTED;
1572                 break;
1573             default:
1574                 /* In case its a reserved extension code */
1575                 impeg2d_bit_stream_flush(ps_stream,EXT_ID_LEN);
1576                 impeg2d_peek_next_start_code(ps_dec);
1577                 break;
1578             }
1579         }
1580         u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1581     }
1582     return e_error;
1583 }
1584 /*******************************************************************************
1585 *  Function Name   : impeg2d_dec_pic_ext_data
1586 *
1587 *  Description     : Decodes the extension data following Picture Coding
1588 *                    Extension. It flushes any user data if present
1589 *
1590 *  Arguments       :
1591 *  dec             : Decoder context
1592 *
1593 *  Values Returned : None
1594 *******************************************************************************/
impeg2d_dec_pic_ext_data(dec_state_t * ps_dec)1595 IMPEG2D_ERROR_CODES_T impeg2d_dec_pic_ext_data(dec_state_t *ps_dec)
1596 {
1597     stream_t   *ps_stream;
1598     UWORD32     u4_start_code;
1599     IMPEG2D_ERROR_CODES_T e_error;
1600 
1601     e_error = (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
1602 
1603     ps_stream      = &ps_dec->s_bit_stream;
1604     u4_start_code   = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1605     while ( (u4_start_code == EXTENSION_START_CODE ||
1606             u4_start_code == USER_DATA_START_CODE) &&
1607             (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE == e_error &&
1608             (ps_stream->u4_offset < ps_stream->u4_max_offset))
1609     {
1610         if(u4_start_code == USER_DATA_START_CODE)
1611         {
1612             impeg2d_dec_user_data(ps_dec);
1613         }
1614         else
1615         {
1616             impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
1617             u4_start_code   = impeg2d_bit_stream_nxt(ps_stream,EXT_ID_LEN);
1618             switch(u4_start_code)
1619             {
1620             case QUANT_MATRIX_EXT_ID:
1621                 impeg2d_dec_quant_matrix_ext(ps_dec);
1622                 break;
1623             case COPYRIGHT_EXT_ID:
1624                 impeg2d_dec_copyright_ext(ps_dec);
1625                 break;
1626             case PIC_DISPLAY_EXT_ID:
1627                 impeg2d_dec_pic_disp_ext(ps_dec);
1628                 break;
1629             case CAMERA_PARAM_EXT_ID:
1630                 impeg2d_dec_cam_param_ext(ps_dec);
1631                 break;
1632             case ITU_T_EXT_ID:
1633                 impeg2d_dec_itu_t_ext(ps_dec);
1634                 break;
1635             case PIC_SPATIAL_SCALABLE_EXT_ID:
1636             case PIC_TEMPORAL_SCALABLE_EXT_ID:
1637                 e_error = IMPEG2D_SCALABLITY_NOT_SUP;
1638                 break;
1639             default:
1640                 /* In case its a reserved extension code */
1641                 impeg2d_bit_stream_flush(ps_stream,EXT_ID_LEN);
1642                 impeg2d_next_start_code(ps_dec);
1643                 break;
1644             }
1645         }
1646         u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1647     }
1648     return e_error;
1649 }
1650 
1651 /*******************************************************************************
1652 *
1653 *  Function Name   : impeg2d_process_video_header
1654 *
1655 *  Description     : Processes video sequence header information
1656 *
1657 *  Arguments       :
1658 *  dec             : Decoder context
1659 *
1660 *  Values Returned : None
1661 *******************************************************************************/
impeg2d_process_video_header(dec_state_t * ps_dec)1662 IMPEG2D_ERROR_CODES_T impeg2d_process_video_header(dec_state_t *ps_dec)
1663 {
1664     stream_t *ps_stream;
1665     ps_stream = &ps_dec->s_bit_stream;
1666     IMPEG2D_ERROR_CODES_T e_error;
1667 
1668     impeg2d_next_code(ps_dec, SEQUENCE_HEADER_CODE);
1669     if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1670     {
1671         e_error = impeg2d_dec_seq_hdr(ps_dec);
1672         if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1673         {
1674             return e_error;
1675         }
1676     }
1677     else
1678     {
1679       return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1680     }
1681     if (impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) == EXTENSION_START_CODE)
1682     {
1683         /* MPEG2 Decoder */
1684         if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1685         {
1686             e_error = impeg2d_dec_seq_ext(ps_dec);
1687             if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1688             {
1689                 return e_error;
1690             }
1691         }
1692         else
1693         {
1694           return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1695         }
1696         if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1697         {
1698             e_error = impeg2d_dec_seq_ext_data(ps_dec);
1699             if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1700             {
1701                 return e_error;
1702             }
1703         }
1704         return impeg2d_init_video_state(ps_dec,MPEG_2_VIDEO);
1705     }
1706     else
1707     {
1708          /* MPEG1 Decoder */
1709         if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1710         {
1711             impeg2d_flush_ext_and_user_data(ps_dec);
1712         }
1713         return impeg2d_init_video_state(ps_dec,MPEG_1_VIDEO);
1714     }
1715 }
1716 /*******************************************************************************
1717 *
1718 *  Function Name   : impeg2d_process_video_bit_stream
1719 *
1720 *  Description     : Processes video sequence header information
1721 *
1722 *  Arguments       :
1723 *  dec             : Decoder context
1724 *
1725 *  Values Returned : None
1726 *******************************************************************************/
impeg2d_process_video_bit_stream(dec_state_t * ps_dec)1727 IMPEG2D_ERROR_CODES_T impeg2d_process_video_bit_stream(dec_state_t *ps_dec)
1728 {
1729     stream_t *ps_stream;
1730     UWORD32 u4_next_bits, u4_start_code_found;
1731     IMPEG2D_ERROR_CODES_T e_error;
1732 
1733     ps_stream = &ps_dec->s_bit_stream;
1734     impeg2d_next_start_code(ps_dec);
1735     /* If the stream is MPEG-2 compliant stream */
1736     u4_start_code_found = 0;
1737 
1738     if(ps_dec->u2_is_mpeg2)
1739     {
1740         /* MPEG2 decoding starts */
1741         while((u4_start_code_found == 0) && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
1742         {
1743             u4_next_bits = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1744 
1745             if(u4_next_bits == SEQUENCE_HEADER_CODE)
1746             {
1747                 if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1748                 {
1749                     e_error = impeg2d_dec_seq_hdr(ps_dec);
1750                     if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1751                     {
1752                         return e_error;
1753                     }
1754 
1755                     u4_start_code_found = 0;
1756 
1757                 }
1758                 else
1759                 {
1760                     return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1761                 }
1762 
1763 
1764                 if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1765                 {
1766                     IMPEG2D_ERROR_CODES_T e_error;
1767                     e_error = impeg2d_dec_seq_ext(ps_dec);
1768                     if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1769                     {
1770                         return e_error;
1771                     }
1772                     u4_start_code_found = 0;
1773 
1774                 }
1775                 else
1776                 {
1777                     return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1778                 }
1779             }
1780             else if((u4_next_bits == USER_DATA_START_CODE) || (u4_next_bits == EXTENSION_START_CODE))
1781             {
1782                 if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1783                 {
1784                     impeg2d_dec_seq_ext_data(ps_dec);
1785                     u4_start_code_found = 0;
1786 
1787                 }
1788 
1789             }
1790             else if((ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1791                     && (u4_next_bits == GOP_START_CODE))
1792             {
1793                 impeg2d_dec_grp_of_pic_hdr(ps_dec);
1794                 impeg2d_dec_user_data(ps_dec);
1795                 u4_start_code_found = 0;
1796 
1797             }
1798             else if((ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1799                     && (u4_next_bits == PICTURE_START_CODE))
1800             {
1801                 ps_dec->i4_pic_count++;
1802 
1803                 e_error = impeg2d_dec_pic_hdr(ps_dec);
1804                 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1805                 {
1806                     return e_error;
1807                 }
1808                 e_error = impeg2d_dec_pic_coding_ext(ps_dec);
1809                 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1810                 {
1811                     return e_error;
1812                 }
1813                 e_error = impeg2d_dec_pic_ext_data(ps_dec);
1814                 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1815                 {
1816                     return e_error;
1817                 }
1818                 e_error = impeg2d_pre_pic_dec_proc(ps_dec);
1819                 if ((IMPEG2D_ERROR_CODES_T) IVD_ERROR_NONE != e_error)
1820                 {
1821                     return e_error;
1822                 }
1823                 impeg2d_dec_pic_data(ps_dec);
1824                 impeg2d_post_pic_dec_proc(ps_dec);
1825                 u4_start_code_found = 1;
1826             }
1827             else
1828 
1829             {
1830                 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);
1831 
1832             }
1833             if(u4_start_code_found == 0)
1834             {
1835                 impeg2d_next_start_code(ps_dec);
1836                 /* In case a dec_pic_data call has not been made, the number of
1837                  * bytes consumed in the previous header decode has to be
1838                  * consumed. Not consuming it will result in zero bytes consumed
1839                  * loops in case there are multiple headers and the second
1840                  * or a future header has a resolution change/other error where
1841                  * the bytes of the last header are not consumed.
1842                  */
1843                 ps_dec->i4_bytes_consumed = (ps_dec->s_bit_stream.u4_offset + 7) >> 3;
1844                 ps_dec->i4_bytes_consumed -= ((size_t)ps_dec->s_bit_stream.pv_bs_buf & 3);
1845             }
1846         }
1847         if((u4_start_code_found == 0) && (ps_dec->s_bit_stream.u4_offset > ps_dec->s_bit_stream.u4_max_offset))
1848         {
1849             return IMPEG2D_FRM_HDR_START_CODE_NOT_FOUND;
1850         }
1851 
1852     }
1853         /* If the stream is MPEG-1 compliant stream */
1854     else
1855     {
1856         while((u4_start_code_found == 0) && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
1857         {
1858             u4_next_bits = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1859 
1860             if(impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) == SEQUENCE_HEADER_CODE)
1861             {
1862                 if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1863                 {
1864                     e_error = impeg2d_dec_seq_hdr(ps_dec);
1865                     if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1866                     {
1867                         return e_error;
1868                     }
1869 
1870                     u4_start_code_found = 0;
1871                 }
1872                 else
1873                 {
1874                     return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1875                 }
1876             }
1877             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))
1878             {
1879                 impeg2d_flush_ext_and_user_data(ps_dec);
1880                 u4_start_code_found = 0;
1881             }
1882 
1883 
1884             else if ((impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) == GOP_START_CODE)
1885                     && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
1886             {
1887                 impeg2d_dec_grp_of_pic_hdr(ps_dec);
1888                 impeg2d_flush_ext_and_user_data(ps_dec);
1889                 u4_start_code_found = 0;
1890             }
1891             else if ((impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) == PICTURE_START_CODE)
1892                     && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
1893             {
1894                 ps_dec->i4_pic_count++;
1895 
1896                 e_error = impeg2d_dec_pic_hdr(ps_dec);
1897                 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1898                 {
1899                     return e_error;
1900                 }
1901                 impeg2d_flush_ext_and_user_data(ps_dec);
1902                 e_error = impeg2d_pre_pic_dec_proc(ps_dec);
1903                 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1904                 {
1905                     return e_error;
1906                 }
1907                 impeg2d_dec_pic_data(ps_dec);
1908                 impeg2d_post_pic_dec_proc(ps_dec);
1909                 u4_start_code_found = 1;
1910             }
1911             else
1912             {
1913                 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);
1914             }
1915             impeg2d_next_start_code(ps_dec);
1916             if (0 == u4_start_code_found)
1917             {
1918                 /* In case a dec_pic_data call has not been made, the number of
1919                  * bytes consumed in the previous header decode has to be
1920                  * consumed. Not consuming it will result in zero bytes consumed
1921                  * loops in case there are multiple headers and the second
1922                  * or a future header has a resolution change/other error where
1923                  * the bytes of the last header are not consumed.
1924                  */
1925                 ps_dec->i4_bytes_consumed = (ps_dec->s_bit_stream.u4_offset + 7) >> 3;
1926                 ps_dec->i4_bytes_consumed -= ((size_t)ps_dec->s_bit_stream.pv_bs_buf & 3);
1927             }
1928         }
1929         if((u4_start_code_found == 0) && (ps_dec->s_bit_stream.u4_offset > ps_dec->s_bit_stream.u4_max_offset))
1930         {
1931            return IMPEG2D_FRM_HDR_START_CODE_NOT_FOUND;
1932         }
1933     }
1934 
1935     return (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
1936 }
1937