• 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 #ifdef KEEP_THREADS_ACTIVE
949     UWORD32 id = ps_dec->currThreadId;
950     dec_state_multi_core_t *ps_dec_state_multi_core = ps_dec->ps_dec_state_multi_core;
951 #endif
952 
953     while (1)
954     {
955 #ifdef KEEP_THREADS_ACTIVE
956         if(id != 0)
957         {
958             e_error = ithread_mutex_lock(ps_dec->pv_proc_start_mutex);
959             if((IMPEG2D_ERROR_CODES_T)IV_SUCCESS != e_error)
960                 break;
961 
962             while(!ps_dec->ai4_process_start)
963             {
964                 ithread_cond_wait(ps_dec->pv_proc_start_condition,
965                                   ps_dec->pv_proc_start_mutex);
966             }
967             ps_dec->ai4_process_start = 0;
968             e_error = ithread_mutex_unlock(ps_dec->pv_proc_start_mutex);
969             if((IMPEG2D_ERROR_CODES_T)IV_SUCCESS != e_error)
970                 break;
971             // break off at the end of decoding all the frames
972             if(ps_dec_state_multi_core->i4_break_threads)
973                 break;
974         }
975 #endif
976         i4_cur_row = ps_dec->u2_mb_y + 1;
977 
978         i4_continue_decode = 1;
979 
980         i4_dequeue_job = 1;
981         do
982         {
983             if(i4_cur_row > ps_dec->u2_num_vert_mb)
984             {
985                 i4_continue_decode = 0;
986                 break;
987             }
988 
989             if((ps_dec->i4_num_cores> 1) && (i4_dequeue_job))
990             {
991                 job_t s_job;
992                 IV_API_CALL_STATUS_T e_ret;
993                 UWORD8 *pu1_buf;
994 
995                 e_ret = impeg2_jobq_dequeue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 1);
996                 if(e_ret != IV_SUCCESS)
997                     break;
998 
999                 if(CMD_PROCESS == s_job.i4_cmd)
1000                 {
1001                     pu1_buf = ps_dec->pu1_inp_bits_buf + s_job.i4_bistream_ofst;
1002                     impeg2d_bit_stream_init(&(ps_dec->s_bit_stream), pu1_buf,
1003                             (ps_dec->u4_num_inp_bytes - s_job.i4_bistream_ofst));
1004                     i4_cur_row      = s_job.i2_start_mb_y;
1005                     ps_dec->i4_start_mb_y = s_job.i2_start_mb_y;
1006                     ps_dec->i4_end_mb_y = s_job.i2_end_mb_y;
1007                     ps_dec->u2_mb_x = 0;
1008                     ps_dec->u2_mb_y = ps_dec->i4_start_mb_y;
1009                     ps_dec->u2_num_mbs_left = (ps_dec->i4_end_mb_y - ps_dec->i4_start_mb_y) * ps_dec->u2_num_horiz_mb;
1010 
1011                 }
1012                 else
1013                 {
1014                     WORD32 start_row;
1015                     WORD32 num_rows;
1016                     start_row = s_job.i2_start_mb_y << 4;
1017                     num_rows = MIN((s_job.i2_end_mb_y << 4), ps_dec->u2_vertical_size);
1018                     num_rows -= start_row;
1019 
1020                     if(ps_dec->u4_deinterlace && (0 == ps_dec->u2_progressive_frame))
1021                     {
1022                         impeg2d_deinterlace(ps_dec,
1023                                             ps_dec->ps_disp_pic,
1024                                             ps_dec->ps_disp_frm_buf,
1025                                             start_row,
1026                                             num_rows);
1027 
1028                     }
1029                     else
1030                     {
1031                         impeg2d_format_convert(ps_dec, ps_dec->ps_disp_pic,
1032                                                ps_dec->ps_disp_frm_buf,
1033                                                start_row, num_rows);
1034                     }
1035                     break;
1036 
1037                 }
1038 
1039             }
1040             e_error = impeg2d_dec_slice(ps_dec);
1041 
1042             if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1043             {
1044                 impeg2d_next_start_code(ps_dec);
1045                 if(ps_dec->s_bit_stream.u4_offset >= ps_dec->s_bit_stream.u4_max_offset)
1046                 {
1047                     ps_dec->u4_error_code = IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1048                     return;
1049                 }
1050             }
1051 
1052             /* Detecting next slice start code */
1053             while(1)
1054             {
1055                 // skip (dec->u4_num_cores-1) rows
1056                 u4_bits_read = impeg2d_bit_stream_nxt(&ps_dec->s_bit_stream,START_CODE_LEN);
1057                 temp = u4_bits_read & 0xFF;
1058                 i4_continue_decode = (((u4_bits_read >> 8) == 0x01) && (temp) && (temp <= 0xAF));
1059 
1060                 if (1 == ps_dec->i4_num_cores && 0 == ps_dec->u2_num_mbs_left)
1061                 {
1062                     i4_continue_decode = 0;
1063 #ifdef __ANDROID__
1064                     android_errorWriteLog(0x534e4554, "26070014");
1065 #endif
1066                 }
1067 
1068                 if(i4_continue_decode)
1069                 {
1070                     if (0 != ps_dec->u2_num_mbs_left)
1071                     {
1072                         /* If the slice is from the same row, then continue decoding without dequeue */
1073                         if((temp - 1) == i4_cur_row)
1074                         {
1075                             i4_dequeue_job = 0;
1076                         }
1077                         else
1078                         {
1079                             if(temp < ps_dec->i4_end_mb_y)
1080                             {
1081                                 i4_cur_row = ps_dec->u2_mb_y;
1082                             }
1083                             else
1084                             {
1085                                 i4_dequeue_job = 1;
1086                             }
1087                         }
1088                     }
1089                     else
1090                     {
1091                         i4_dequeue_job = 1;
1092                     }
1093                     break;
1094                 }
1095                 else
1096                     break;
1097             }
1098 
1099         }while(i4_continue_decode);
1100         if(ps_dec->i4_num_cores > 1)
1101         {
1102             while(1)
1103             {
1104                 job_t s_job;
1105                 IV_API_CALL_STATUS_T e_ret;
1106 
1107                 e_ret = impeg2_jobq_dequeue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 1);
1108                 if(e_ret != IV_SUCCESS)
1109                     break;
1110                 if(CMD_FMTCONV == s_job.i4_cmd)
1111                 {
1112                     WORD32 start_row;
1113                     WORD32 num_rows;
1114                     start_row = s_job.i2_start_mb_y << 4;
1115                     num_rows = MIN((s_job.i2_end_mb_y << 4), ps_dec->u2_vertical_size);
1116                     num_rows -= start_row;
1117                     if(ps_dec->u4_deinterlace && (0 == ps_dec->u2_progressive_frame))
1118                     {
1119                         impeg2d_deinterlace(ps_dec,
1120                                             ps_dec->ps_disp_pic,
1121                                             ps_dec->ps_disp_frm_buf,
1122                                             start_row,
1123                                             num_rows);
1124 
1125                     }
1126                     else
1127                     {
1128                         impeg2d_format_convert(ps_dec,
1129                                                ps_dec->ps_disp_pic,
1130                                                ps_dec->ps_disp_frm_buf,
1131                                                start_row,
1132                                                num_rows);
1133                     }
1134                 }
1135             }
1136         }
1137         else
1138         {
1139             if((NULL != ps_dec->ps_disp_pic) && ((0 == ps_dec->u4_share_disp_buf) || (IV_YUV_420P != ps_dec->i4_chromaFormat)))
1140             {
1141                 if(ps_dec->u4_deinterlace && (0 == ps_dec->u2_progressive_frame))
1142                 {
1143                     impeg2d_deinterlace(ps_dec,
1144                                         ps_dec->ps_disp_pic,
1145                                         ps_dec->ps_disp_frm_buf,
1146                                         0,
1147                                         ps_dec->u2_vertical_size);
1148 
1149                 }
1150                 else
1151                 {
1152                     impeg2d_format_convert(ps_dec, ps_dec->ps_disp_pic,
1153                                             ps_dec->ps_disp_frm_buf,
1154                                             0, ps_dec->u2_vertical_size);
1155                 }
1156             }
1157         }
1158 #ifdef KEEP_THREADS_ACTIVE
1159         if(id != 0)
1160         {
1161             e_error = ithread_mutex_lock(ps_dec->pv_proc_done_mutex);
1162             if((IMPEG2D_ERROR_CODES_T)IV_SUCCESS != e_error)
1163                 break;
1164 
1165             ps_dec->ai4_process_done = 1;
1166             ithread_cond_signal(ps_dec->pv_proc_done_condition);
1167 
1168             e_error = ithread_mutex_unlock(ps_dec->pv_proc_done_mutex);
1169             if((IMPEG2D_ERROR_CODES_T)IV_SUCCESS != e_error)
1170                 break;
1171         }
1172         else
1173         {
1174             break;
1175         }
1176 #else
1177         break;
1178 #endif
1179     }
1180 }
1181 
impeg2d_init_thread_dec_ctxt(dec_state_t * ps_dec,dec_state_t * ps_dec_thd,WORD32 i4_min_mb_y)1182 static WORD32 impeg2d_init_thread_dec_ctxt(dec_state_t *ps_dec,
1183                                            dec_state_t *ps_dec_thd,
1184                                            WORD32 i4_min_mb_y)
1185 {
1186     UNUSED(i4_min_mb_y);
1187     ps_dec_thd->i4_start_mb_y = 0;
1188     ps_dec_thd->i4_end_mb_y = ps_dec->u2_num_vert_mb;
1189     ps_dec_thd->u2_mb_x = 0;
1190     ps_dec_thd->u2_mb_y = 0;
1191     ps_dec_thd->u2_is_mpeg2 = ps_dec->u2_is_mpeg2;
1192     ps_dec_thd->i4_pic_count = ps_dec->i4_pic_count;
1193     ps_dec_thd->u2_frame_width = ps_dec->u2_frame_width;
1194     ps_dec_thd->u2_frame_height = ps_dec->u2_frame_height;
1195     ps_dec_thd->u2_picture_width = ps_dec->u2_picture_width;
1196     ps_dec_thd->u2_horizontal_size = ps_dec->u2_horizontal_size;
1197     ps_dec_thd->u2_vertical_size = ps_dec->u2_vertical_size;
1198     ps_dec_thd->u2_create_max_width = ps_dec->u2_create_max_width;
1199     ps_dec_thd->u2_create_max_height = ps_dec->u2_create_max_height;
1200     ps_dec_thd->u2_header_done = ps_dec->u2_header_done;
1201     ps_dec_thd->u2_decode_header = ps_dec->u2_decode_header;
1202 
1203     ps_dec_thd->u2_num_horiz_mb = ps_dec->u2_num_horiz_mb;
1204     ps_dec_thd->u2_num_vert_mb = ps_dec->u2_num_vert_mb;
1205     ps_dec_thd->u2_num_flds_decoded = ps_dec->u2_num_flds_decoded;
1206 
1207     ps_dec_thd->u4_frm_buf_stride = ps_dec->u4_frm_buf_stride;
1208 
1209     ps_dec_thd->u2_field_dct = ps_dec->u2_field_dct;
1210     ps_dec_thd->u2_read_dct_type = ps_dec->u2_read_dct_type;
1211 
1212     ps_dec_thd->u2_read_motion_type = ps_dec->u2_read_motion_type;
1213     ps_dec_thd->u2_motion_type = ps_dec->u2_motion_type;
1214 
1215     ps_dec_thd->pu2_mb_type = ps_dec->pu2_mb_type;
1216     ps_dec_thd->u2_fld_pic = ps_dec->u2_fld_pic;
1217     ps_dec_thd->u2_frm_pic = ps_dec->u2_frm_pic;
1218 
1219     ps_dec_thd->u2_fld_parity = ps_dec->u2_fld_parity;
1220 
1221     ps_dec_thd->au2_fcode_data[0] = ps_dec->au2_fcode_data[0];
1222     ps_dec_thd->au2_fcode_data[1] = ps_dec->au2_fcode_data[1];
1223 
1224     ps_dec_thd->u1_quant_scale = ps_dec->u1_quant_scale;
1225 
1226     ps_dec_thd->u2_num_mbs_left = ps_dec->u2_num_mbs_left;
1227     ps_dec_thd->u2_first_mb = ps_dec->u2_first_mb;
1228     ps_dec_thd->u2_num_skipped_mbs = ps_dec->u2_num_skipped_mbs;
1229 
1230     memcpy(&ps_dec_thd->s_cur_frm_buf, &ps_dec->s_cur_frm_buf, sizeof(yuv_buf_t));
1231     memcpy(&ps_dec_thd->as_recent_fld[0][0], &ps_dec->as_recent_fld[0][0], sizeof(yuv_buf_t));
1232     memcpy(&ps_dec_thd->as_recent_fld[0][1], &ps_dec->as_recent_fld[0][1], sizeof(yuv_buf_t));
1233     memcpy(&ps_dec_thd->as_recent_fld[1][0], &ps_dec->as_recent_fld[1][0], sizeof(yuv_buf_t));
1234     memcpy(&ps_dec_thd->as_recent_fld[1][1], &ps_dec->as_recent_fld[1][1], sizeof(yuv_buf_t));
1235     memcpy(&ps_dec_thd->as_ref_buf, &ps_dec->as_ref_buf, sizeof(yuv_buf_t) * 2 * 2);
1236 
1237 
1238     ps_dec_thd->pf_decode_slice = ps_dec->pf_decode_slice;
1239 
1240     ps_dec_thd->pf_vld_inv_quant = ps_dec->pf_vld_inv_quant;
1241 
1242     memcpy(ps_dec_thd->pf_idct_recon, ps_dec->pf_idct_recon, sizeof(ps_dec->pf_idct_recon));
1243 
1244     memcpy(ps_dec_thd->pf_mc, ps_dec->pf_mc, sizeof(ps_dec->pf_mc));
1245     ps_dec_thd->pf_interpolate = ps_dec->pf_interpolate;
1246     ps_dec_thd->pf_copy_mb = ps_dec->pf_copy_mb;
1247     ps_dec_thd->pf_fullx_halfy_8x8              =  ps_dec->pf_fullx_halfy_8x8;
1248     ps_dec_thd->pf_halfx_fully_8x8              =  ps_dec->pf_halfx_fully_8x8;
1249     ps_dec_thd->pf_halfx_halfy_8x8              =  ps_dec->pf_halfx_halfy_8x8;
1250     ps_dec_thd->pf_fullx_fully_8x8              =  ps_dec->pf_fullx_fully_8x8;
1251 
1252     ps_dec_thd->pf_memset_8bit_8x8_block        =  ps_dec->pf_memset_8bit_8x8_block;
1253     ps_dec_thd->pf_memset_16bit_8x8_linear_block        =  ps_dec->pf_memset_16bit_8x8_linear_block;
1254     ps_dec_thd->pf_copy_yuv420p_buf             =   ps_dec->pf_copy_yuv420p_buf;
1255     ps_dec_thd->pf_fmt_conv_yuv420p_to_yuv422ile    =   ps_dec->pf_fmt_conv_yuv420p_to_yuv422ile;
1256     ps_dec_thd->pf_fmt_conv_yuv420p_to_yuv420sp_uv  =   ps_dec->pf_fmt_conv_yuv420p_to_yuv420sp_uv;
1257     ps_dec_thd->pf_fmt_conv_yuv420p_to_yuv420sp_vu  =   ps_dec->pf_fmt_conv_yuv420p_to_yuv420sp_vu;
1258 
1259 
1260     memcpy(ps_dec_thd->au1_intra_quant_matrix, ps_dec->au1_intra_quant_matrix, NUM_PELS_IN_BLOCK * sizeof(UWORD8));
1261     memcpy(ps_dec_thd->au1_inter_quant_matrix, ps_dec->au1_inter_quant_matrix, NUM_PELS_IN_BLOCK * sizeof(UWORD8));
1262     ps_dec_thd->pu1_inv_scan_matrix = ps_dec->pu1_inv_scan_matrix;
1263 
1264 
1265     ps_dec_thd->u2_progressive_sequence = ps_dec->u2_progressive_sequence;
1266     ps_dec_thd->e_pic_type =  ps_dec->e_pic_type;
1267     ps_dec_thd->u2_full_pel_forw_vector = ps_dec->u2_full_pel_forw_vector;
1268     ps_dec_thd->u2_forw_f_code =   ps_dec->u2_forw_f_code;
1269     ps_dec_thd->u2_full_pel_back_vector = ps_dec->u2_full_pel_back_vector;
1270     ps_dec_thd->u2_back_f_code = ps_dec->u2_back_f_code;
1271 
1272     memcpy(ps_dec_thd->ai2_mv, ps_dec->ai2_mv, (2*2*2)*sizeof(WORD16));
1273     memcpy(ps_dec_thd->au2_f_code, ps_dec->au2_f_code, (2*2)*sizeof(UWORD16));
1274     ps_dec_thd->u2_intra_dc_precision = ps_dec->u2_intra_dc_precision;
1275     ps_dec_thd->u2_picture_structure = ps_dec->u2_picture_structure;
1276     ps_dec_thd->u2_top_field_first = ps_dec->u2_top_field_first;
1277     ps_dec_thd->u2_frame_pred_frame_dct = ps_dec->u2_frame_pred_frame_dct;
1278     ps_dec_thd->u2_concealment_motion_vectors = ps_dec->u2_concealment_motion_vectors;
1279     ps_dec_thd->u2_q_scale_type =  ps_dec->u2_q_scale_type;
1280     ps_dec_thd->u2_intra_vlc_format = ps_dec->u2_intra_vlc_format;
1281     ps_dec_thd->u2_alternate_scan = ps_dec->u2_alternate_scan;
1282     ps_dec_thd->u2_repeat_first_field = ps_dec->u2_repeat_first_field;
1283     ps_dec_thd->u2_progressive_frame = ps_dec->u2_progressive_frame;
1284     ps_dec_thd->pu1_inp_bits_buf = ps_dec->pu1_inp_bits_buf;
1285     ps_dec_thd->u4_num_inp_bytes = ps_dec->u4_num_inp_bytes;
1286     ps_dec_thd->pv_jobq = ps_dec->pv_jobq;
1287     ps_dec_thd->pv_jobq_buf = ps_dec->pv_jobq_buf;
1288     ps_dec_thd->i4_jobq_buf_size = ps_dec->i4_jobq_buf_size;
1289 
1290 
1291     ps_dec_thd->u2_frame_rate_code = ps_dec->u2_frame_rate_code;
1292     ps_dec_thd->u2_frame_rate_extension_n = ps_dec->u2_frame_rate_extension_n;
1293     ps_dec_thd->u2_frame_rate_extension_d = ps_dec->u2_frame_rate_extension_d;
1294     ps_dec_thd->u2_framePeriod =   ps_dec->u2_framePeriod;
1295     ps_dec_thd->u2_display_horizontal_size = ps_dec->u2_display_horizontal_size;
1296     ps_dec_thd->u2_display_vertical_size = ps_dec->u2_display_vertical_size;
1297     ps_dec_thd->u2_aspect_ratio_info = ps_dec->u2_aspect_ratio_info;
1298 
1299     ps_dec_thd->ps_func_bi_direct = ps_dec->ps_func_bi_direct;
1300     ps_dec_thd->ps_func_forw_or_back = ps_dec->ps_func_forw_or_back;
1301     ps_dec_thd->pv_deinterlacer_ctxt = ps_dec->pv_deinterlacer_ctxt;
1302     ps_dec_thd->ps_deint_pic = ps_dec->ps_deint_pic;
1303     ps_dec_thd->pu1_deint_fmt_buf = ps_dec->pu1_deint_fmt_buf;
1304 
1305     return 0;
1306 }
1307 
1308 
impeg2d_get_slice_pos(dec_state_multi_core_t * ps_dec_state_multi_core)1309 WORD32 impeg2d_get_slice_pos(dec_state_multi_core_t *ps_dec_state_multi_core)
1310 {
1311     WORD32 u4_bits;
1312     WORD32 i4_row;
1313 
1314 
1315     dec_state_t *ps_dec = ps_dec_state_multi_core->ps_dec_state[0];
1316     WORD32 i4_prev_row;
1317     stream_t s_bitstrm;
1318     WORD32 i4_start_row;
1319     WORD32 i4_slice_bistream_ofst;
1320     WORD32 i;
1321     s_bitstrm = ps_dec->s_bit_stream;
1322     i4_prev_row = -1;
1323 
1324     ps_dec_state_multi_core->ps_dec_state[0]->i4_start_mb_y = 0;
1325     ps_dec_state_multi_core->ps_dec_state[1]->i4_start_mb_y = -1;
1326     ps_dec_state_multi_core->ps_dec_state[2]->i4_start_mb_y = -1;
1327     ps_dec_state_multi_core->ps_dec_state[3]->i4_start_mb_y = -1;
1328 
1329     ps_dec_state_multi_core->ps_dec_state[0]->i4_end_mb_y = ps_dec->u2_num_vert_mb;
1330     ps_dec_state_multi_core->ps_dec_state[1]->i4_end_mb_y = -1;
1331     ps_dec_state_multi_core->ps_dec_state[2]->i4_end_mb_y = -1;
1332     ps_dec_state_multi_core->ps_dec_state[3]->i4_end_mb_y = -1;
1333 
1334     if(ps_dec->i4_num_cores == 1)
1335         return 0;
1336     /* Reset the jobq to start of the jobq buffer */
1337     impeg2_jobq_reset((jobq_t *)ps_dec->pv_jobq);
1338 
1339     i4_start_row = -1;
1340     i4_slice_bistream_ofst = 0;
1341     while(1)
1342     {
1343         WORD32 i4_is_slice;
1344 
1345         if(s_bitstrm.u4_offset + START_CODE_LEN >= s_bitstrm.u4_max_offset)
1346         {
1347             break;
1348         }
1349         u4_bits = impeg2d_bit_stream_nxt(&s_bitstrm,START_CODE_LEN);
1350 
1351         i4_row = u4_bits & 0xFF;
1352 
1353         /* Detect end of frame */
1354         i4_is_slice = (((u4_bits >> 8) == 0x01) && (i4_row) && (i4_row <= ps_dec->u2_num_vert_mb));
1355         if(!i4_is_slice)
1356             break;
1357 
1358         i4_row -= 1;
1359 
1360 
1361         if(i4_prev_row < i4_row)
1362         {
1363             /* Create a job for previous slice row */
1364             if(i4_start_row != -1)
1365             {
1366                 job_t s_job;
1367                 IV_API_CALL_STATUS_T ret;
1368                 s_job.i2_start_mb_y = i4_start_row;
1369                 s_job.i2_end_mb_y = i4_row;
1370                 s_job.i4_cmd = CMD_PROCESS;
1371                 s_job.i4_bistream_ofst = i4_slice_bistream_ofst;
1372                 ret = impeg2_jobq_queue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 0);
1373                 if(ret != IV_SUCCESS)
1374                     return ret;
1375 
1376             }
1377             /* Store current slice's bitstream offset */
1378             i4_slice_bistream_ofst = s_bitstrm.u4_offset >> 3;
1379             i4_slice_bistream_ofst -= (size_t)s_bitstrm.pv_bs_buf & 3;
1380             i4_prev_row = i4_row;
1381 
1382             /* Store current slice's row position */
1383             i4_start_row = i4_row;
1384 
1385         }
1386 #ifdef __ANDROID__
1387         else if (i4_prev_row > i4_row)
1388         {
1389             android_errorWriteLog(0x534e4554, "26070014");
1390         }
1391 #endif
1392 
1393         impeg2d_bit_stream_flush(&s_bitstrm, START_CODE_LEN);
1394 
1395         // flush bytes till next start code
1396         /* Flush the bytes till a  start code is encountered  */
1397         while(impeg2d_bit_stream_nxt(&s_bitstrm, 24) != START_CODE_PREFIX)
1398         {
1399             impeg2d_bit_stream_get(&s_bitstrm, 8);
1400 
1401             if(s_bitstrm.u4_offset >= s_bitstrm.u4_max_offset)
1402             {
1403                 break;
1404             }
1405         }
1406     }
1407 
1408     /* Create job for the last slice row */
1409     {
1410         job_t s_job;
1411         IV_API_CALL_STATUS_T e_ret;
1412         s_job.i2_start_mb_y = i4_start_row;
1413         s_job.i2_end_mb_y = ps_dec->u2_num_vert_mb;
1414         s_job.i4_cmd = CMD_PROCESS;
1415         s_job.i4_bistream_ofst = i4_slice_bistream_ofst;
1416         e_ret = impeg2_jobq_queue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 0);
1417         if(e_ret != IV_SUCCESS)
1418             return e_ret;
1419 
1420     }
1421     if((NULL != ps_dec->ps_disp_pic) && ((0 == ps_dec->u4_share_disp_buf) || (IV_YUV_420P != ps_dec->i4_chromaFormat)))
1422     {
1423         for(i = 0; i < ps_dec->u2_vertical_size; i+=64)
1424         {
1425             job_t s_job;
1426             IV_API_CALL_STATUS_T ret;
1427             s_job.i2_start_mb_y = i;
1428             s_job.i2_start_mb_y >>= 4;
1429             s_job.i2_end_mb_y = (i + 64);
1430             s_job.i2_end_mb_y >>= 4;
1431             s_job.i4_cmd = CMD_FMTCONV;
1432             s_job.i4_bistream_ofst = 0;
1433             ret = impeg2_jobq_queue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 0);
1434             if(ret != IV_SUCCESS)
1435                 return ret;
1436 
1437         }
1438     }
1439 
1440     impeg2_jobq_terminate(ps_dec->pv_jobq);
1441     ps_dec->i4_bytes_consumed = s_bitstrm.u4_offset >> 3;
1442     ps_dec->i4_bytes_consumed -= ((size_t)s_bitstrm.pv_bs_buf & 3);
1443 
1444     return 0;
1445 }
1446 
1447 /*******************************************************************************
1448 *
1449 *  Function Name   : impeg2d_dec_pic_data
1450 *
1451 *  Description     : It intializes several parameters and decodes a Picture
1452 *                    till any slice is left.
1453 *
1454 *  Arguments       :
1455 *  dec             : Decoder context
1456 *
1457 *  Values Returned : None
1458 *******************************************************************************/
1459 
impeg2d_dec_pic_data(dec_state_t * ps_dec)1460 void impeg2d_dec_pic_data(dec_state_t *ps_dec)
1461 {
1462 
1463     WORD32 i;
1464     dec_state_multi_core_t *ps_dec_state_multi_core;
1465 
1466     dec_state_t *ps_dec_thd;
1467     WORD32 i4_status;
1468     WORD32 i4_min_mb_y;
1469 
1470 
1471     /* Resetting the MB address and MB coordinates at the start of the Frame */
1472     ps_dec->u2_mb_x = ps_dec->u2_mb_y = 0;
1473 
1474     ps_dec_state_multi_core = ps_dec->ps_dec_state_multi_core;
1475     impeg2d_get_slice_pos(ps_dec_state_multi_core);
1476 
1477 #ifdef KEEP_THREADS_ACTIVE
1478     ps_dec->currThreadId = 0;
1479 #endif
1480 
1481     i4_min_mb_y = 1;
1482     for(i=1; i < ps_dec->i4_num_cores; i++)
1483     {
1484         // initialize decoder context for thread
1485         // launch dec->u4_num_cores-1 threads
1486 
1487         ps_dec_thd = ps_dec_state_multi_core->ps_dec_state[i];
1488 
1489         ps_dec_thd->ps_disp_pic = ps_dec->ps_disp_pic;
1490         ps_dec_thd->ps_disp_frm_buf = ps_dec->ps_disp_frm_buf;
1491 
1492         i4_status = impeg2d_init_thread_dec_ctxt(ps_dec, ps_dec_thd, i4_min_mb_y);
1493         //impeg2d_dec_pic_data_thread(ps_dec_thd);
1494 
1495         if(i4_status == 0 && !ps_dec_state_multi_core->au4_thread_launched[i])
1496         {
1497 #ifdef KEEP_THREADS_ACTIVE
1498             ps_dec_thd->currThreadId = i;
1499 #endif
1500             ithread_create(ps_dec_thd->pv_codec_thread_handle, NULL, (void *)impeg2d_dec_pic_data_thread, ps_dec_thd);
1501             ps_dec_state_multi_core->au4_thread_launched[i] = 1;
1502             i4_min_mb_y = ps_dec_thd->u2_mb_y + 1;
1503         }
1504 #ifndef KEEP_THREADS_ACTIVE
1505         else
1506         {
1507             ps_dec_state_multi_core->au4_thread_launched[i] = 0;
1508             break;
1509         }
1510 #else
1511         i4_status = ithread_mutex_lock(ps_dec_thd->pv_proc_start_mutex);
1512         if((IMPEG2D_ERROR_CODES_T)IV_SUCCESS != i4_status) return;
1513 
1514         ps_dec_thd->ai4_process_start = 1;
1515         ithread_cond_signal(ps_dec_thd->pv_proc_start_condition);
1516 
1517         i4_status = ithread_mutex_unlock(ps_dec_thd->pv_proc_start_mutex);
1518         if((IMPEG2D_ERROR_CODES_T)IV_SUCCESS != i4_status) return;
1519 #endif
1520     }
1521 
1522     impeg2d_dec_pic_data_thread(ps_dec);
1523 
1524     // wait for threads to complete
1525     for(i=1; i < ps_dec->i4_num_cores; i++)
1526     {
1527         if(ps_dec_state_multi_core->au4_thread_launched[i])
1528         {
1529             ps_dec_thd = ps_dec_state_multi_core->ps_dec_state[i];
1530 #ifdef KEEP_THREADS_ACTIVE
1531             i4_status = ithread_mutex_lock(ps_dec_thd->pv_proc_done_mutex);
1532             if((IMPEG2D_ERROR_CODES_T)IV_SUCCESS != i4_status) return;
1533 
1534             while(!ps_dec_thd->ai4_process_done)
1535             {
1536                 ithread_cond_wait(ps_dec_thd->pv_proc_done_condition,
1537                                   ps_dec_thd->pv_proc_done_mutex);
1538             }
1539             ps_dec_thd->ai4_process_done = 0;
1540             i4_status = ithread_mutex_unlock(ps_dec_thd->pv_proc_done_mutex);
1541             if((IMPEG2D_ERROR_CODES_T)IV_SUCCESS != i4_status) return;
1542 #else
1543             ithread_join(ps_dec_thd->pv_codec_thread_handle, NULL);
1544             ps_dec_state_multi_core->au4_thread_launched[i] = 0;
1545 #endif
1546         }
1547     }
1548 
1549 }
1550 /*******************************************************************************
1551 *
1552 *  Function Name   : impeg2d_flush_ext_and_user_data
1553 *
1554 *  Description     : Flushes the extension and user data present in the
1555 *                    stream_t
1556 *
1557 *  Arguments       :
1558 *  dec             : Decoder context
1559 *
1560 *  Values Returned : None
1561 *******************************************************************************/
impeg2d_flush_ext_and_user_data(dec_state_t * ps_dec)1562 void impeg2d_flush_ext_and_user_data(dec_state_t *ps_dec)
1563 {
1564     UWORD32 u4_start_code;
1565     stream_t *ps_stream;
1566 
1567     ps_stream    = &ps_dec->s_bit_stream;
1568     u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1569 
1570     while((u4_start_code == EXTENSION_START_CODE || u4_start_code == USER_DATA_START_CODE) &&
1571             (ps_stream->u4_offset < ps_stream->u4_max_offset))
1572     {
1573         impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
1574         while(impeg2d_bit_stream_nxt(ps_stream,START_CODE_PREFIX_LEN) != START_CODE_PREFIX &&
1575                 (ps_stream->u4_offset < ps_stream->u4_max_offset))
1576         {
1577             impeg2d_bit_stream_flush(ps_stream,8);
1578         }
1579         u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1580     }
1581 }
1582 /*******************************************************************************
1583 *
1584 *  Function Name   : impeg2d_dec_user_data
1585 *
1586 *  Description     : Flushes the user data present in the stream_t
1587 *
1588 *  Arguments       :
1589 *  dec             : Decoder context
1590 *
1591 *  Values Returned : None
1592 *******************************************************************************/
impeg2d_dec_user_data(dec_state_t * ps_dec)1593 void impeg2d_dec_user_data(dec_state_t *ps_dec)
1594 {
1595     UWORD32 u4_start_code;
1596     stream_t *ps_stream;
1597 
1598     ps_stream    = &ps_dec->s_bit_stream;
1599     u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1600 
1601     while(u4_start_code == USER_DATA_START_CODE)
1602     {
1603         impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
1604         while((impeg2d_bit_stream_nxt(ps_stream,START_CODE_PREFIX_LEN) != START_CODE_PREFIX) &&
1605                 (ps_stream->u4_offset < ps_stream->u4_max_offset))
1606         {
1607             impeg2d_bit_stream_flush(ps_stream,8);
1608         }
1609         u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1610     }
1611 }
1612 /*******************************************************************************
1613 *  Function Name   : impeg2d_dec_seq_ext_data
1614 *
1615 *  Description     : Decodes the extension data following Sequence
1616 *                    Extension. It flushes any user data if present
1617 *
1618 *  Arguments       :
1619 *  dec             : Decoder context
1620 *
1621 *  Values Returned : None
1622 *******************************************************************************/
impeg2d_dec_seq_ext_data(dec_state_t * ps_dec)1623 IMPEG2D_ERROR_CODES_T impeg2d_dec_seq_ext_data(dec_state_t *ps_dec)
1624 {
1625     stream_t   *ps_stream;
1626     UWORD32     u4_start_code;
1627     IMPEG2D_ERROR_CODES_T e_error;
1628 
1629     e_error = (IMPEG2D_ERROR_CODES_T) IVD_ERROR_NONE;
1630 
1631     ps_stream      = &ps_dec->s_bit_stream;
1632     u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1633     while( (u4_start_code == EXTENSION_START_CODE ||
1634             u4_start_code == USER_DATA_START_CODE) &&
1635             (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE == e_error &&
1636             (ps_stream->u4_offset < ps_stream->u4_max_offset))
1637     {
1638         if(u4_start_code == USER_DATA_START_CODE)
1639         {
1640             impeg2d_dec_user_data(ps_dec);
1641         }
1642         else
1643         {
1644             impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
1645             u4_start_code   = impeg2d_bit_stream_nxt(ps_stream,EXT_ID_LEN);
1646             switch(u4_start_code)
1647             {
1648             case SEQ_DISPLAY_EXT_ID:
1649                 impeg2d_dec_seq_disp_ext(ps_dec);
1650                 break;
1651             case SEQ_SCALABLE_EXT_ID:
1652                 e_error = IMPEG2D_SCALABILITIY_NOT_SUPPORTED;
1653                 break;
1654             default:
1655                 /* In case its a reserved extension code */
1656                 impeg2d_bit_stream_flush(ps_stream,EXT_ID_LEN);
1657                 impeg2d_peek_next_start_code(ps_dec);
1658                 break;
1659             }
1660         }
1661         u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1662     }
1663     return e_error;
1664 }
1665 /*******************************************************************************
1666 *  Function Name   : impeg2d_dec_pic_ext_data
1667 *
1668 *  Description     : Decodes the extension data following Picture Coding
1669 *                    Extension. It flushes any user data if present
1670 *
1671 *  Arguments       :
1672 *  dec             : Decoder context
1673 *
1674 *  Values Returned : None
1675 *******************************************************************************/
impeg2d_dec_pic_ext_data(dec_state_t * ps_dec)1676 IMPEG2D_ERROR_CODES_T impeg2d_dec_pic_ext_data(dec_state_t *ps_dec)
1677 {
1678     stream_t   *ps_stream;
1679     UWORD32     u4_start_code;
1680     IMPEG2D_ERROR_CODES_T e_error;
1681 
1682     e_error = (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
1683 
1684     ps_stream      = &ps_dec->s_bit_stream;
1685     u4_start_code   = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1686     while ( (u4_start_code == EXTENSION_START_CODE ||
1687             u4_start_code == USER_DATA_START_CODE) &&
1688             (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE == e_error &&
1689             (ps_stream->u4_offset < ps_stream->u4_max_offset))
1690     {
1691         if(u4_start_code == USER_DATA_START_CODE)
1692         {
1693             impeg2d_dec_user_data(ps_dec);
1694         }
1695         else
1696         {
1697             impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
1698             u4_start_code   = impeg2d_bit_stream_nxt(ps_stream,EXT_ID_LEN);
1699             switch(u4_start_code)
1700             {
1701             case QUANT_MATRIX_EXT_ID:
1702                 impeg2d_dec_quant_matrix_ext(ps_dec);
1703                 break;
1704             case COPYRIGHT_EXT_ID:
1705                 impeg2d_dec_copyright_ext(ps_dec);
1706                 break;
1707             case PIC_DISPLAY_EXT_ID:
1708                 impeg2d_dec_pic_disp_ext(ps_dec);
1709                 break;
1710             case CAMERA_PARAM_EXT_ID:
1711                 impeg2d_dec_cam_param_ext(ps_dec);
1712                 break;
1713             case ITU_T_EXT_ID:
1714                 impeg2d_dec_itu_t_ext(ps_dec);
1715                 break;
1716             case PIC_SPATIAL_SCALABLE_EXT_ID:
1717             case PIC_TEMPORAL_SCALABLE_EXT_ID:
1718                 e_error = IMPEG2D_SCALABLITY_NOT_SUP;
1719                 break;
1720             default:
1721                 /* In case its a reserved extension code */
1722                 impeg2d_bit_stream_flush(ps_stream,EXT_ID_LEN);
1723                 impeg2d_next_start_code(ps_dec);
1724                 break;
1725             }
1726         }
1727         u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1728     }
1729     return e_error;
1730 }
1731 
1732 /*******************************************************************************
1733 *
1734 *  Function Name   : impeg2d_process_video_header
1735 *
1736 *  Description     : Processes video sequence header information
1737 *
1738 *  Arguments       :
1739 *  dec             : Decoder context
1740 *
1741 *  Values Returned : None
1742 *******************************************************************************/
impeg2d_process_video_header(dec_state_t * ps_dec)1743 IMPEG2D_ERROR_CODES_T impeg2d_process_video_header(dec_state_t *ps_dec)
1744 {
1745     stream_t *ps_stream;
1746     ps_stream = &ps_dec->s_bit_stream;
1747     IMPEG2D_ERROR_CODES_T e_error;
1748 
1749     impeg2d_next_code(ps_dec, SEQUENCE_HEADER_CODE);
1750     if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1751     {
1752         e_error = impeg2d_dec_seq_hdr(ps_dec);
1753         if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1754         {
1755             return e_error;
1756         }
1757     }
1758     else
1759     {
1760       return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1761     }
1762     if (impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) == EXTENSION_START_CODE)
1763     {
1764         /* MPEG2 Decoder */
1765         if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1766         {
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         }
1773         else
1774         {
1775           return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1776         }
1777         if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1778         {
1779             e_error = impeg2d_dec_seq_ext_data(ps_dec);
1780             if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1781             {
1782                 return e_error;
1783             }
1784         }
1785         return impeg2d_init_video_state(ps_dec,MPEG_2_VIDEO);
1786     }
1787     else
1788     {
1789          /* MPEG1 Decoder */
1790         if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1791         {
1792             impeg2d_flush_ext_and_user_data(ps_dec);
1793         }
1794         return impeg2d_init_video_state(ps_dec,MPEG_1_VIDEO);
1795     }
1796 }
1797 /*******************************************************************************
1798 *
1799 *  Function Name   : impeg2d_process_video_bit_stream
1800 *
1801 *  Description     : Processes video sequence header information
1802 *
1803 *  Arguments       :
1804 *  dec             : Decoder context
1805 *
1806 *  Values Returned : None
1807 *******************************************************************************/
impeg2d_process_video_bit_stream(dec_state_t * ps_dec)1808 IMPEG2D_ERROR_CODES_T impeg2d_process_video_bit_stream(dec_state_t *ps_dec)
1809 {
1810     stream_t *ps_stream;
1811     UWORD32 u4_next_bits, u4_start_code_found;
1812     IMPEG2D_ERROR_CODES_T e_error;
1813 
1814     ps_stream = &ps_dec->s_bit_stream;
1815     impeg2d_next_start_code(ps_dec);
1816     /* If the stream is MPEG-2 compliant stream */
1817     u4_start_code_found = 0;
1818 
1819     if(ps_dec->u2_is_mpeg2)
1820     {
1821         /* MPEG2 decoding starts */
1822         while((u4_start_code_found == 0) && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
1823         {
1824             u4_next_bits = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1825 
1826             if(u4_next_bits == SEQUENCE_HEADER_CODE)
1827             {
1828                 if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1829                 {
1830                     e_error = impeg2d_dec_seq_hdr(ps_dec);
1831                     if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1832                     {
1833                         return e_error;
1834                     }
1835 
1836                     u4_start_code_found = 0;
1837 
1838                 }
1839                 else
1840                 {
1841                     return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1842                 }
1843 
1844 
1845                 if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1846                 {
1847                     IMPEG2D_ERROR_CODES_T e_error;
1848                     e_error = impeg2d_dec_seq_ext(ps_dec);
1849                     if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1850                     {
1851                         return e_error;
1852                     }
1853                     u4_start_code_found = 0;
1854 
1855                 }
1856                 else
1857                 {
1858                     return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1859                 }
1860             }
1861             else if((u4_next_bits == USER_DATA_START_CODE) || (u4_next_bits == EXTENSION_START_CODE))
1862             {
1863                 if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1864                 {
1865                     impeg2d_dec_seq_ext_data(ps_dec);
1866                     u4_start_code_found = 0;
1867 
1868                 }
1869 
1870             }
1871             else if((ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1872                     && (u4_next_bits == GOP_START_CODE))
1873             {
1874                 impeg2d_dec_grp_of_pic_hdr(ps_dec);
1875                 impeg2d_dec_user_data(ps_dec);
1876                 u4_start_code_found = 0;
1877 
1878             }
1879             else if((ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1880                     && (u4_next_bits == PICTURE_START_CODE))
1881             {
1882                 ps_dec->i4_pic_count++;
1883 
1884                 e_error = impeg2d_dec_pic_hdr(ps_dec);
1885                 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1886                 {
1887                     return e_error;
1888                 }
1889                 e_error = impeg2d_dec_pic_coding_ext(ps_dec);
1890                 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1891                 {
1892                     return e_error;
1893                 }
1894                 e_error = impeg2d_dec_pic_ext_data(ps_dec);
1895                 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1896                 {
1897                     return e_error;
1898                 }
1899                 e_error = impeg2d_pre_pic_dec_proc(ps_dec);
1900                 if ((IMPEG2D_ERROR_CODES_T) IVD_ERROR_NONE != e_error)
1901                 {
1902                     return e_error;
1903                 }
1904                 impeg2d_dec_pic_data(ps_dec);
1905                 impeg2d_post_pic_dec_proc(ps_dec);
1906                 u4_start_code_found = 1;
1907             }
1908             else
1909 
1910             {
1911                 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);
1912 
1913             }
1914             if(u4_start_code_found == 0)
1915             {
1916                 impeg2d_next_start_code(ps_dec);
1917                 /* In case a dec_pic_data call has not been made, the number of
1918                  * bytes consumed in the previous header decode has to be
1919                  * consumed. Not consuming it will result in zero bytes consumed
1920                  * loops in case there are multiple headers and the second
1921                  * or a future header has a resolution change/other error where
1922                  * the bytes of the last header are not consumed.
1923                  */
1924                 ps_dec->i4_bytes_consumed = (ps_dec->s_bit_stream.u4_offset + 7) >> 3;
1925                 ps_dec->i4_bytes_consumed -= ((size_t)ps_dec->s_bit_stream.pv_bs_buf & 3);
1926             }
1927         }
1928         if((u4_start_code_found == 0) && (ps_dec->s_bit_stream.u4_offset > ps_dec->s_bit_stream.u4_max_offset))
1929         {
1930             return IMPEG2D_FRM_HDR_START_CODE_NOT_FOUND;
1931         }
1932 
1933     }
1934         /* If the stream is MPEG-1 compliant stream */
1935     else
1936     {
1937         while((u4_start_code_found == 0) && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
1938         {
1939             u4_next_bits = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1940 
1941             if(impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) == SEQUENCE_HEADER_CODE)
1942             {
1943                 if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1944                 {
1945                     e_error = impeg2d_dec_seq_hdr(ps_dec);
1946                     if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1947                     {
1948                         return e_error;
1949                     }
1950 
1951                     u4_start_code_found = 0;
1952                 }
1953                 else
1954                 {
1955                     return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1956                 }
1957             }
1958             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))
1959             {
1960                 impeg2d_flush_ext_and_user_data(ps_dec);
1961                 u4_start_code_found = 0;
1962             }
1963 
1964 
1965             else if ((impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) == GOP_START_CODE)
1966                     && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
1967             {
1968                 impeg2d_dec_grp_of_pic_hdr(ps_dec);
1969                 impeg2d_flush_ext_and_user_data(ps_dec);
1970                 u4_start_code_found = 0;
1971             }
1972             else if ((impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) == PICTURE_START_CODE)
1973                     && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
1974             {
1975                 ps_dec->i4_pic_count++;
1976 
1977                 e_error = impeg2d_dec_pic_hdr(ps_dec);
1978                 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1979                 {
1980                     return e_error;
1981                 }
1982                 impeg2d_flush_ext_and_user_data(ps_dec);
1983                 e_error = impeg2d_pre_pic_dec_proc(ps_dec);
1984                 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1985                 {
1986                     return e_error;
1987                 }
1988                 impeg2d_dec_pic_data(ps_dec);
1989                 impeg2d_post_pic_dec_proc(ps_dec);
1990                 u4_start_code_found = 1;
1991             }
1992             else
1993             {
1994                 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);
1995             }
1996             impeg2d_next_start_code(ps_dec);
1997             if (0 == u4_start_code_found)
1998             {
1999                 /* In case a dec_pic_data call has not been made, the number of
2000                  * bytes consumed in the previous header decode has to be
2001                  * consumed. Not consuming it will result in zero bytes consumed
2002                  * loops in case there are multiple headers and the second
2003                  * or a future header has a resolution change/other error where
2004                  * the bytes of the last header are not consumed.
2005                  */
2006                 ps_dec->i4_bytes_consumed = (ps_dec->s_bit_stream.u4_offset + 7) >> 3;
2007                 ps_dec->i4_bytes_consumed -= ((size_t)ps_dec->s_bit_stream.pv_bs_buf & 3);
2008             }
2009         }
2010         if((u4_start_code_found == 0) && (ps_dec->s_bit_stream.u4_offset > ps_dec->s_bit_stream.u4_max_offset))
2011         {
2012            return IMPEG2D_FRM_HDR_START_CODE_NOT_FOUND;
2013         }
2014     }
2015 
2016     return (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
2017 }
2018