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