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