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