blob: 186f477ee0a54693505c744053f78f52587c181e [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
Venkatarama Avadhanif0d50ce2017-04-21 10:08:14 +0530155 if (0 == u2_width || 0 == u2_height)
156 {
157 IMPEG2D_ERROR_CODES_T e_error = IMPEG2D_FRM_HDR_DECODE_ERR;
158 return e_error;
159 }
160
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +0530161 if ((u2_width != ps_dec->u2_horizontal_size)
162 || (u2_height != ps_dec->u2_vertical_size))
163 {
164 if (0 == ps_dec->u2_header_done)
165 {
166 /* This is the first time we are reading the resolution */
167 ps_dec->u2_horizontal_size = u2_width;
168 ps_dec->u2_vertical_size = u2_height;
169 if (0 == ps_dec->u4_frm_buf_stride)
170 {
Harish Mahendrakar903fd2b2015-08-05 15:47:51 +0530171 ps_dec->u4_frm_buf_stride = (UWORD32) (u2_width);
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +0530172 }
173 }
174 else
175 {
Venkatarama Avadhanib8578df2017-08-31 15:30:27 +0530176 if (0 == ps_dec->i4_pic_count)
177 {
178 /* Decoder has not decoded a single frame since the last
179 * reset/init. This implies that we have two headers in the
180 * input stream. So, do not indicate a resolution change, since
181 * this can take the decoder into an infinite loop.
182 */
183 return (IMPEG2D_ERROR_CODES_T) IMPEG2D_FRM_HDR_DECODE_ERR;
184 }
185 else if((u2_width > ps_dec->u2_create_max_width)
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +0530186 || (u2_height > ps_dec->u2_create_max_height))
187 {
188 IMPEG2D_ERROR_CODES_T e_error = IMPEG2D_UNSUPPORTED_DIMENSIONS;
189
190 ps_dec->u2_reinit_max_height = u2_height;
191 ps_dec->u2_reinit_max_width = u2_width;
192
193 return e_error;
194 }
195 else
196 {
197 /* The resolution has changed */
198 return (IMPEG2D_ERROR_CODES_T)IVD_RES_CHANGED;
199 }
200 }
201 }
202
203 if((ps_dec->u2_horizontal_size > ps_dec->u2_create_max_width)
204 || (ps_dec->u2_vertical_size > ps_dec->u2_create_max_height))
205 {
206 IMPEG2D_ERROR_CODES_T e_error = IMPEG2D_UNSUPPORTED_DIMENSIONS;
Harish Mahendrakarcb449c92016-06-01 15:37:18 +0530207 ps_dec->u2_reinit_max_height = ps_dec->u2_vertical_size;
208 ps_dec->u2_reinit_max_width = ps_dec->u2_horizontal_size;
209 return e_error;
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +0530210 }
211
212
213 /*------------------------------------------------------------------------*/
214 /* Flush the following as they are not being used */
215 /* aspect_ratio_info (4 bits) */
216 /*------------------------------------------------------------------------*/
217 ps_dec->u2_aspect_ratio_info = impeg2d_bit_stream_get(ps_stream,4);
218
219 /*------------------------------------------------------------------------*/
220 /* Frame rate code(4 bits) */
221 /*------------------------------------------------------------------------*/
222 ps_dec->u2_frame_rate_code = impeg2d_bit_stream_get(ps_stream,4);
Venkatarama Avadhani4dbcab92017-02-13 17:03:04 +0530223 if (ps_dec->u2_frame_rate_code > MPEG2_MAX_FRAME_RATE_CODE)
224 {
225 return IMPEG2D_FRM_HDR_DECODE_ERR;
226 }
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +0530227 /*------------------------------------------------------------------------*/
228 /* Flush the following as they are not being used */
229 /* bit_rate_value (18 bits) */
230 /*------------------------------------------------------------------------*/
231 impeg2d_bit_stream_flush(ps_stream,18);
232 GET_MARKER_BIT(ps_dec,ps_stream);
233 /*------------------------------------------------------------------------*/
234 /* Flush the following as they are not being used */
235 /* vbv_buffer_size_value(10 bits), constrained_parameter_flag (1 bit) */
236 /*------------------------------------------------------------------------*/
237 impeg2d_bit_stream_flush(ps_stream,11);
238
239 /*------------------------------------------------------------------------*/
240 /* Quantization matrix for the intra blocks */
241 /*------------------------------------------------------------------------*/
242 if(impeg2d_bit_stream_get_bit(ps_stream) == 1)
243 {
244 UWORD16 i;
245 for(i = 0; i < NUM_PELS_IN_BLOCK; i++)
246 {
247 ps_dec->au1_intra_quant_matrix[gau1_impeg2_inv_scan_zig_zag[i]] = (UWORD8)impeg2d_bit_stream_get(ps_stream,8);
248 }
249
250 }
251 else
252 {
253 memcpy(ps_dec->au1_intra_quant_matrix,gau1_impeg2_intra_quant_matrix_default,
254 NUM_PELS_IN_BLOCK);
255 }
256
257 /*------------------------------------------------------------------------*/
258 /* Quantization matrix for the inter blocks */
259 /*------------------------------------------------------------------------*/
260 if(impeg2d_bit_stream_get_bit(ps_stream) == 1)
261 {
262 UWORD16 i;
263 for(i = 0; i < NUM_PELS_IN_BLOCK; i++)
264 {
265 ps_dec->au1_inter_quant_matrix[gau1_impeg2_inv_scan_zig_zag[i]] = (UWORD8)impeg2d_bit_stream_get(ps_stream,8);
266 }
267 }
268 else
269 {
270 memcpy(ps_dec->au1_inter_quant_matrix,gau1_impeg2_inter_quant_matrix_default,
271 NUM_PELS_IN_BLOCK);
272 }
273 impeg2d_next_start_code(ps_dec);
274
275 return (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
276}
277
278/******************************************************************************
279*
280* Function Name : impeg2d_dec_seq_ext
281*
282* Description : Gets additional sequence data.
283*
284* Arguments :
285* dec : Decoder Context
286*
287* Values Returned : None
288******************************************************************************/
289IMPEG2D_ERROR_CODES_T impeg2d_dec_seq_ext(dec_state_t *ps_dec)
290{
291 stream_t *ps_stream;
Venkatarama Avadhani2e34c4f2017-06-27 11:31:45 +0530292 UWORD16 horizontal_value;
293 UWORD16 vertical_value;
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +0530294
295 ps_stream = &ps_dec->s_bit_stream;
296
297 if (impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) != EXTENSION_START_CODE)
298 {
299 impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
300 return IMPEG2D_FRM_HDR_START_CODE_NOT_FOUND;
301
302 }
303 /* Flush the extension start code */
304 impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
305
306 /* Flush extension start code identifier */
307 impeg2d_bit_stream_flush(ps_stream,4);
308
309 /*----------------------------------------------------------------------*/
310 /* Profile and Level information */
311 /*----------------------------------------------------------------------*/
312 {
313 UWORD32 u4_esc_bit, u4_profile, u4_level;
314
315 /* Read the profile and level information */
316 /* check_profile_and_level: Table 8-1 */
317 /* [7:7] 1 Escape bit */
318 /* [6:4] 3 Profile identification */
319 /* [3:0] 4 Level identification */
320
321 u4_esc_bit = impeg2d_bit_stream_get_bit(ps_stream);
322 u4_profile = impeg2d_bit_stream_get(ps_stream,3);
323 u4_level = impeg2d_bit_stream_get(ps_stream,4);
324 UNUSED(u4_profile);
325 UNUSED(u4_level);
326 /*
327 if( escBit == 1 ||
328 profile < MPEG2_MAIN_PROFILE ||
329 level < MPEG2_MAIN_LEVEL)
330 */
331 if (1 == u4_esc_bit)
332 {
333 return IMPEG2D_PROF_LEVEL_NOT_SUPPORTED;
334 }
335 }
336
337 ps_dec->u2_progressive_sequence = impeg2d_bit_stream_get_bit(ps_stream);
338
339 /* Read the chrominance format */
340 if(impeg2d_bit_stream_get(ps_stream,2) != 0x1)
341 return IMPEG2D_CHROMA_FMT_NOT_SUP;
342
Venkatarama Avadhani2e34c4f2017-06-27 11:31:45 +0530343 /* Error resilience: store the 2 most significant bit in horizontal and vertical */
344 /* variables.Use it only if adding them to the vertical and horizontal sizes */
345 /* respectively, doesn't exceed the MAX_WD and MAX_HT supported by the application.*/
346
347
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +0530348 /* Read the 2 most significant bits from horizontal_size */
Venkatarama Avadhani2e34c4f2017-06-27 11:31:45 +0530349 horizontal_value = (impeg2d_bit_stream_get(ps_stream,2) << 12);
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +0530350
351 /* Read the 2 most significant bits from vertical_size */
Venkatarama Avadhani2e34c4f2017-06-27 11:31:45 +0530352 vertical_value = (impeg2d_bit_stream_get(ps_stream,2) << 12);
353
354 /* Error resilience: The height and width should not be more than the*/
355 /*max height and width the application can support*/
356 if(ps_dec->u2_create_max_height < (ps_dec->u2_vertical_size + vertical_value))
357 {
358 return (IMPEG2D_ERROR_CODES_T) IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
359 }
360
361 if(ps_dec->u2_create_max_width < (ps_dec->u2_horizontal_size + horizontal_value))
362 {
363 return (IMPEG2D_ERROR_CODES_T) IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
364 }
365 ps_dec->u2_vertical_size += vertical_value;
366 ps_dec->u2_horizontal_size += horizontal_value;
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +0530367
368 /*-----------------------------------------------------------------------*/
369 /* Flush the following as they are not used now */
370 /* bit_rate_extension 12 */
371 /* marker_bit 1 */
372 /* vbv_buffer_size_extension 8 */
373 /* low_delay 1 */
374 /*-----------------------------------------------------------------------*/
375 impeg2d_bit_stream_flush(ps_stream,12);
376 GET_MARKER_BIT(ps_dec,ps_stream);
377 impeg2d_bit_stream_flush(ps_stream,9);
378 /*-----------------------------------------------------------------------*/
379 /* frame_rate_extension_n 2 */
380 /* frame_rate_extension_d 5 */
381 /*-----------------------------------------------------------------------*/
382 ps_dec->u2_frame_rate_extension_n = impeg2d_bit_stream_get(ps_stream,2);
383 ps_dec->u2_frame_rate_extension_d = impeg2d_bit_stream_get(ps_stream,5);
384
385 return (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
386}
387
388/*******************************************************************************
389*
390* Function Name : impeg2d_dec_seq_disp_ext
391*
392* Description : This function is eqvt to sequence_display_extension() of
393* standard. It flushes data present as it is not being used
394*
395* Arguments :
396* dec : Decoder Context
397*
398* Values Returned : None
399******************************************************************************/
400void impeg2d_dec_seq_disp_ext(dec_state_t *ps_dec)
401{
402 stream_t *ps_stream;
403 ps_stream = &ps_dec->s_bit_stream;
404
405 /*
406 sequence_display_extension()
407 {
408 extension_start_code_identifier 4
409 video_format 3
410 colour_description 1
411 if (colour_description)
412 {
413 colour_primaries 8
414 transfer_characteristics 8
415 matrix_coefficients 8
416 }
417 display_horizontal_size 14
418 marker_bit 1
419 display_vertical_size 14
420 next_start_code()
421 }
422 */
423
424 impeg2d_bit_stream_get(ps_stream,7);
425 if (impeg2d_bit_stream_get_bit(ps_stream) == 1)
426 {
427 impeg2d_bit_stream_get(ps_stream,24);
428 }
429
430 /* display_horizontal_size and display_vertical_size */
431 ps_dec->u2_display_horizontal_size = impeg2d_bit_stream_get(ps_stream,14);;
432 GET_MARKER_BIT(ps_dec,ps_stream);
433 ps_dec->u2_display_vertical_size = impeg2d_bit_stream_get(ps_stream,14);
434
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*******************************************************************************/
450IMPEG2D_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*******************************************************************************/
466void 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*******************************************************************************/
517void 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*******************************************************************************/
559void 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
577void 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
609void 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
642void 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*******************************************************************************/
673IMPEG2D_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 /*-----------------------------------------------------------------------*/
Harish Mahendrakar8c743b52015-12-30 17:38:27 +0530720 while (impeg2d_bit_stream_nxt(ps_stream,1) == 1 &&
721 ps_stream->u4_offset < ps_stream->u4_max_offset)
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +0530722 {
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*
Venkatarama Avadhani849e1dd2017-04-20 09:26:54 +0530741* Values Returned : Error
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +0530742*******************************************************************************/
Venkatarama Avadhani849e1dd2017-04-20 09:26:54 +0530743IMPEG2D_ERROR_CODES_T impeg2d_dec_pic_coding_ext(dec_state_t *ps_dec)
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +0530744{
745 stream_t *ps_stream;
Venkatarama Avadhani849e1dd2017-04-20 09:26:54 +0530746 IMPEG2D_ERROR_CODES_T e_error = (IMPEG2D_ERROR_CODES_T) IV_SUCCESS;
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +0530747
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);
Venkatarama Avadhani849e1dd2017-04-20 09:26:54 +0530759 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 }
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +0530764 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 }
Venkatarama Avadhani849e1dd2017-04-20 09:26:54 +0530791 return e_error;
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +0530792}
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*******************************************************************************/
806IMPEG2D_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;
Venkatarama Avadhani806ede92017-05-30 09:18:44 +0530842
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;
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +0530848 }
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 */
Harish Mahendrakar8c743b52015-12-30 17:38:27 +0530865 while (impeg2d_bit_stream_nxt(ps_stream,1) == 1 &&
866 ps_stream->u4_offset < ps_stream->u4_max_offset)
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +0530867 {
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
902void 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,
Venkatarama Avadhanic4634492017-06-08 14:07:29 +0530939 (ps_dec->u4_num_inp_bytes - s_job.i4_bistream_ofst));
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +0530940 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;
Harish Mahendrakar85206902015-08-13 10:59:13 +0530955
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 }
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +0530971 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);
Venkatarama Avadhani6235e862017-07-10 16:24:33 +0530981 if(ps_dec->s_bit_stream.u4_offset >= ps_dec->s_bit_stream.u4_max_offset)
982 {
983 ps_dec->u4_error_code = IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
984 return;
985 }
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +0530986 }
987 }
988
989 /* Detecting next slice start code */
990 while(1)
991 {
992 // skip (dec->u4_num_cores-1) rows
993 u4_bits_read = impeg2d_bit_stream_nxt(&ps_dec->s_bit_stream,START_CODE_LEN);
994 temp = u4_bits_read & 0xFF;
995 i4_continue_decode = (((u4_bits_read >> 8) == 0x01) && (temp) && (temp <= 0xAF));
996
Harish Mahendrakarc8113542015-12-31 19:06:30 +0530997 if (1 == ps_dec->i4_num_cores && 0 == ps_dec->u2_num_mbs_left)
998 {
999 i4_continue_decode = 0;
1000 android_errorWriteLog(0x534e4554, "26070014");
1001 }
1002
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301003 if(i4_continue_decode)
1004 {
1005 /* If the slice is from the same row, then continue decoding without dequeue */
1006 if((temp - 1) == i4_cur_row)
1007 {
1008 i4_dequeue_job = 0;
1009 break;
1010 }
1011
1012 if(temp < ps_dec->i4_end_mb_y)
1013 {
1014 i4_cur_row = ps_dec->u2_mb_y;
1015 }
1016 else
1017 {
1018 i4_dequeue_job = 1;
1019 }
1020 break;
1021
1022 }
1023 else
1024 break;
1025 }
1026
1027 }while(i4_continue_decode);
1028 if(ps_dec->i4_num_cores > 1)
1029 {
1030 while(1)
1031 {
1032 job_t s_job;
1033 IV_API_CALL_STATUS_T e_ret;
1034
1035 e_ret = impeg2_jobq_dequeue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 1);
1036 if(e_ret != IV_SUCCESS)
1037 break;
1038 if(CMD_FMTCONV == s_job.i4_cmd)
1039 {
1040 WORD32 start_row;
1041 WORD32 num_rows;
1042 start_row = s_job.i2_start_mb_y << 4;
1043 num_rows = MIN((s_job.i2_end_mb_y << 4), ps_dec->u2_vertical_size);
1044 num_rows -= start_row;
Harish Mahendrakar85206902015-08-13 10:59:13 +05301045 if(ps_dec->u4_deinterlace && (0 == ps_dec->u2_progressive_frame))
1046 {
1047 impeg2d_deinterlace(ps_dec,
1048 ps_dec->ps_disp_pic,
1049 ps_dec->ps_disp_frm_buf,
1050 start_row,
1051 num_rows);
1052
1053 }
1054 else
1055 {
1056 impeg2d_format_convert(ps_dec,
1057 ps_dec->ps_disp_pic,
1058 ps_dec->ps_disp_frm_buf,
1059 start_row,
1060 num_rows);
1061 }
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301062 }
1063 }
1064 }
1065 else
1066 {
1067 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 +05301068 {
1069 if(ps_dec->u4_deinterlace && (0 == ps_dec->u2_progressive_frame))
1070 {
1071 impeg2d_deinterlace(ps_dec,
1072 ps_dec->ps_disp_pic,
1073 ps_dec->ps_disp_frm_buf,
1074 0,
1075 ps_dec->u2_vertical_size);
1076
1077 }
1078 else
1079 {
1080 impeg2d_format_convert(ps_dec, ps_dec->ps_disp_pic,
1081 ps_dec->ps_disp_frm_buf,
1082 0, ps_dec->u2_vertical_size);
1083 }
1084 }
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301085 }
1086}
1087
1088static WORD32 impeg2d_init_thread_dec_ctxt(dec_state_t *ps_dec,
1089 dec_state_t *ps_dec_thd,
1090 WORD32 i4_min_mb_y)
1091{
1092 UNUSED(i4_min_mb_y);
1093 ps_dec_thd->i4_start_mb_y = 0;
1094 ps_dec_thd->i4_end_mb_y = ps_dec->u2_num_vert_mb;
1095 ps_dec_thd->u2_mb_x = 0;
1096 ps_dec_thd->u2_mb_y = 0;
1097 ps_dec_thd->u2_is_mpeg2 = ps_dec->u2_is_mpeg2;
Venkatarama Avadhanib8578df2017-08-31 15:30:27 +05301098 ps_dec_thd->i4_pic_count = ps_dec->i4_pic_count;
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301099 ps_dec_thd->u2_frame_width = ps_dec->u2_frame_width;
1100 ps_dec_thd->u2_frame_height = ps_dec->u2_frame_height;
1101 ps_dec_thd->u2_picture_width = ps_dec->u2_picture_width;
1102 ps_dec_thd->u2_horizontal_size = ps_dec->u2_horizontal_size;
1103 ps_dec_thd->u2_vertical_size = ps_dec->u2_vertical_size;
1104 ps_dec_thd->u2_create_max_width = ps_dec->u2_create_max_width;
1105 ps_dec_thd->u2_create_max_height = ps_dec->u2_create_max_height;
1106 ps_dec_thd->u2_header_done = ps_dec->u2_header_done;
1107 ps_dec_thd->u2_decode_header = ps_dec->u2_decode_header;
1108
1109 ps_dec_thd->u2_num_horiz_mb = ps_dec->u2_num_horiz_mb;
1110 ps_dec_thd->u2_num_vert_mb = ps_dec->u2_num_vert_mb;
1111 ps_dec_thd->u2_num_flds_decoded = ps_dec->u2_num_flds_decoded;
1112
1113 ps_dec_thd->u4_frm_buf_stride = ps_dec->u4_frm_buf_stride;
1114
1115 ps_dec_thd->u2_field_dct = ps_dec->u2_field_dct;
1116 ps_dec_thd->u2_read_dct_type = ps_dec->u2_read_dct_type;
1117
1118 ps_dec_thd->u2_read_motion_type = ps_dec->u2_read_motion_type;
1119 ps_dec_thd->u2_motion_type = ps_dec->u2_motion_type;
1120
1121 ps_dec_thd->pu2_mb_type = ps_dec->pu2_mb_type;
1122 ps_dec_thd->u2_fld_pic = ps_dec->u2_fld_pic;
1123 ps_dec_thd->u2_frm_pic = ps_dec->u2_frm_pic;
1124
1125 ps_dec_thd->u2_fld_parity = ps_dec->u2_fld_parity;
1126
1127 ps_dec_thd->au2_fcode_data[0] = ps_dec->au2_fcode_data[0];
1128 ps_dec_thd->au2_fcode_data[1] = ps_dec->au2_fcode_data[1];
1129
1130 ps_dec_thd->u1_quant_scale = ps_dec->u1_quant_scale;
1131
1132 ps_dec_thd->u2_num_mbs_left = ps_dec->u2_num_mbs_left;
1133 ps_dec_thd->u2_first_mb = ps_dec->u2_first_mb;
1134 ps_dec_thd->u2_num_skipped_mbs = ps_dec->u2_num_skipped_mbs;
1135
1136 memcpy(&ps_dec_thd->s_cur_frm_buf, &ps_dec->s_cur_frm_buf, sizeof(yuv_buf_t));
1137 memcpy(&ps_dec_thd->as_recent_fld[0][0], &ps_dec->as_recent_fld[0][0], sizeof(yuv_buf_t));
1138 memcpy(&ps_dec_thd->as_recent_fld[0][1], &ps_dec->as_recent_fld[0][1], sizeof(yuv_buf_t));
1139 memcpy(&ps_dec_thd->as_recent_fld[1][0], &ps_dec->as_recent_fld[1][0], sizeof(yuv_buf_t));
1140 memcpy(&ps_dec_thd->as_recent_fld[1][1], &ps_dec->as_recent_fld[1][1], sizeof(yuv_buf_t));
1141 memcpy(&ps_dec_thd->as_ref_buf, &ps_dec->as_ref_buf, sizeof(yuv_buf_t) * 2 * 2);
1142
1143
1144 ps_dec_thd->pf_decode_slice = ps_dec->pf_decode_slice;
1145
1146 ps_dec_thd->pf_vld_inv_quant = ps_dec->pf_vld_inv_quant;
1147
1148 memcpy(ps_dec_thd->pf_idct_recon, ps_dec->pf_idct_recon, sizeof(ps_dec->pf_idct_recon));
1149
1150 memcpy(ps_dec_thd->pf_mc, ps_dec->pf_mc, sizeof(ps_dec->pf_mc));
1151 ps_dec_thd->pf_interpolate = ps_dec->pf_interpolate;
1152 ps_dec_thd->pf_copy_mb = ps_dec->pf_copy_mb;
1153 ps_dec_thd->pf_fullx_halfy_8x8 = ps_dec->pf_fullx_halfy_8x8;
1154 ps_dec_thd->pf_halfx_fully_8x8 = ps_dec->pf_halfx_fully_8x8;
1155 ps_dec_thd->pf_halfx_halfy_8x8 = ps_dec->pf_halfx_halfy_8x8;
1156 ps_dec_thd->pf_fullx_fully_8x8 = ps_dec->pf_fullx_fully_8x8;
1157
1158 ps_dec_thd->pf_memset_8bit_8x8_block = ps_dec->pf_memset_8bit_8x8_block;
1159 ps_dec_thd->pf_memset_16bit_8x8_linear_block = ps_dec->pf_memset_16bit_8x8_linear_block;
1160 ps_dec_thd->pf_copy_yuv420p_buf = ps_dec->pf_copy_yuv420p_buf;
1161 ps_dec_thd->pf_fmt_conv_yuv420p_to_yuv422ile = ps_dec->pf_fmt_conv_yuv420p_to_yuv422ile;
1162 ps_dec_thd->pf_fmt_conv_yuv420p_to_yuv420sp_uv = ps_dec->pf_fmt_conv_yuv420p_to_yuv420sp_uv;
1163 ps_dec_thd->pf_fmt_conv_yuv420p_to_yuv420sp_vu = ps_dec->pf_fmt_conv_yuv420p_to_yuv420sp_vu;
1164
1165
1166 memcpy(ps_dec_thd->au1_intra_quant_matrix, ps_dec->au1_intra_quant_matrix, NUM_PELS_IN_BLOCK * sizeof(UWORD8));
1167 memcpy(ps_dec_thd->au1_inter_quant_matrix, ps_dec->au1_inter_quant_matrix, NUM_PELS_IN_BLOCK * sizeof(UWORD8));
1168 ps_dec_thd->pu1_inv_scan_matrix = ps_dec->pu1_inv_scan_matrix;
1169
1170
1171 ps_dec_thd->u2_progressive_sequence = ps_dec->u2_progressive_sequence;
1172 ps_dec_thd->e_pic_type = ps_dec->e_pic_type;
1173 ps_dec_thd->u2_full_pel_forw_vector = ps_dec->u2_full_pel_forw_vector;
1174 ps_dec_thd->u2_forw_f_code = ps_dec->u2_forw_f_code;
1175 ps_dec_thd->u2_full_pel_back_vector = ps_dec->u2_full_pel_back_vector;
1176 ps_dec_thd->u2_back_f_code = ps_dec->u2_back_f_code;
1177
1178 memcpy(ps_dec_thd->ai2_mv, ps_dec->ai2_mv, (2*2*2)*sizeof(WORD16));
1179 memcpy(ps_dec_thd->au2_f_code, ps_dec->au2_f_code, (2*2)*sizeof(UWORD16));
1180 ps_dec_thd->u2_intra_dc_precision = ps_dec->u2_intra_dc_precision;
1181 ps_dec_thd->u2_picture_structure = ps_dec->u2_picture_structure;
1182 ps_dec_thd->u2_top_field_first = ps_dec->u2_top_field_first;
1183 ps_dec_thd->u2_frame_pred_frame_dct = ps_dec->u2_frame_pred_frame_dct;
1184 ps_dec_thd->u2_concealment_motion_vectors = ps_dec->u2_concealment_motion_vectors;
1185 ps_dec_thd->u2_q_scale_type = ps_dec->u2_q_scale_type;
1186 ps_dec_thd->u2_intra_vlc_format = ps_dec->u2_intra_vlc_format;
1187 ps_dec_thd->u2_alternate_scan = ps_dec->u2_alternate_scan;
1188 ps_dec_thd->u2_repeat_first_field = ps_dec->u2_repeat_first_field;
1189 ps_dec_thd->u2_progressive_frame = ps_dec->u2_progressive_frame;
1190 ps_dec_thd->pu1_inp_bits_buf = ps_dec->pu1_inp_bits_buf;
1191 ps_dec_thd->u4_num_inp_bytes = ps_dec->u4_num_inp_bytes;
1192 ps_dec_thd->pv_jobq = ps_dec->pv_jobq;
1193 ps_dec_thd->pv_jobq_buf = ps_dec->pv_jobq_buf;
1194 ps_dec_thd->i4_jobq_buf_size = ps_dec->i4_jobq_buf_size;
1195
1196
1197 ps_dec_thd->u2_frame_rate_code = ps_dec->u2_frame_rate_code;
1198 ps_dec_thd->u2_frame_rate_extension_n = ps_dec->u2_frame_rate_extension_n;
1199 ps_dec_thd->u2_frame_rate_extension_d = ps_dec->u2_frame_rate_extension_d;
1200 ps_dec_thd->u2_framePeriod = ps_dec->u2_framePeriod;
1201 ps_dec_thd->u2_display_horizontal_size = ps_dec->u2_display_horizontal_size;
1202 ps_dec_thd->u2_display_vertical_size = ps_dec->u2_display_vertical_size;
1203 ps_dec_thd->u2_aspect_ratio_info = ps_dec->u2_aspect_ratio_info;
1204
1205 ps_dec_thd->ps_func_bi_direct = ps_dec->ps_func_bi_direct;
1206 ps_dec_thd->ps_func_forw_or_back = ps_dec->ps_func_forw_or_back;
Harish Mahendrakar85206902015-08-13 10:59:13 +05301207 ps_dec_thd->pv_deinterlacer_ctxt = ps_dec->pv_deinterlacer_ctxt;
1208 ps_dec_thd->ps_deint_pic = ps_dec->ps_deint_pic;
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301209
1210 return 0;
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301211}
1212
1213
1214WORD32 impeg2d_get_slice_pos(dec_state_multi_core_t *ps_dec_state_multi_core)
1215{
1216 WORD32 u4_bits;
1217 WORD32 i4_row;
1218
1219
1220 dec_state_t *ps_dec = ps_dec_state_multi_core->ps_dec_state[0];
1221 WORD32 i4_prev_row;
1222 stream_t s_bitstrm;
1223 WORD32 i4_start_row;
1224 WORD32 i4_slice_bistream_ofst;
1225 WORD32 i;
1226 s_bitstrm = ps_dec->s_bit_stream;
1227 i4_prev_row = -1;
1228
1229 ps_dec_state_multi_core->ps_dec_state[0]->i4_start_mb_y = 0;
1230 ps_dec_state_multi_core->ps_dec_state[1]->i4_start_mb_y = -1;
1231 ps_dec_state_multi_core->ps_dec_state[2]->i4_start_mb_y = -1;
1232 ps_dec_state_multi_core->ps_dec_state[3]->i4_start_mb_y = -1;
1233
1234 ps_dec_state_multi_core->ps_dec_state[0]->i4_end_mb_y = ps_dec->u2_num_vert_mb;
1235 ps_dec_state_multi_core->ps_dec_state[1]->i4_end_mb_y = -1;
1236 ps_dec_state_multi_core->ps_dec_state[2]->i4_end_mb_y = -1;
1237 ps_dec_state_multi_core->ps_dec_state[3]->i4_end_mb_y = -1;
1238
1239 if(ps_dec->i4_num_cores == 1)
1240 return 0;
1241 /* Reset the jobq to start of the jobq buffer */
1242 impeg2_jobq_reset((jobq_t *)ps_dec->pv_jobq);
1243
1244 i4_start_row = -1;
1245 i4_slice_bistream_ofst = 0;
1246 while(1)
1247 {
1248 WORD32 i4_is_slice;
Venkatarama Avadhanib3491382015-04-16 17:39:55 +05301249
1250 if(s_bitstrm.u4_offset + START_CODE_LEN >= s_bitstrm.u4_max_offset)
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301251 {
1252 break;
1253 }
Venkatarama Avadhanib3491382015-04-16 17:39:55 +05301254 u4_bits = impeg2d_bit_stream_nxt(&s_bitstrm,START_CODE_LEN);
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301255
1256 i4_row = u4_bits & 0xFF;
1257
1258 /* Detect end of frame */
1259 i4_is_slice = (((u4_bits >> 8) == 0x01) && (i4_row) && (i4_row <= ps_dec->u2_num_vert_mb));
1260 if(!i4_is_slice)
1261 break;
1262
1263 i4_row -= 1;
1264
1265
Harish Mahendrakarc8113542015-12-31 19:06:30 +05301266 if(i4_prev_row < i4_row)
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301267 {
1268 /* Create a job for previous slice row */
1269 if(i4_start_row != -1)
1270 {
1271 job_t s_job;
1272 IV_API_CALL_STATUS_T ret;
1273 s_job.i2_start_mb_y = i4_start_row;
1274 s_job.i2_end_mb_y = i4_row;
1275 s_job.i4_cmd = CMD_PROCESS;
1276 s_job.i4_bistream_ofst = i4_slice_bistream_ofst;
1277 ret = impeg2_jobq_queue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 0);
1278 if(ret != IV_SUCCESS)
1279 return ret;
1280
1281 }
1282 /* Store current slice's bitstream offset */
1283 i4_slice_bistream_ofst = s_bitstrm.u4_offset >> 3;
1284 i4_slice_bistream_ofst -= (size_t)s_bitstrm.pv_bs_buf & 3;
1285 i4_prev_row = i4_row;
1286
1287 /* Store current slice's row position */
1288 i4_start_row = i4_row;
1289
Harish Mahendrakarc8113542015-12-31 19:06:30 +05301290 } else if (i4_prev_row > i4_row) {
1291 android_errorWriteLog(0x534e4554, "26070014");
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301292 }
1293
1294
1295 impeg2d_bit_stream_flush(&s_bitstrm, START_CODE_LEN);
1296
1297 // flush bytes till next start code
1298 /* Flush the bytes till a start code is encountered */
1299 while(impeg2d_bit_stream_nxt(&s_bitstrm, 24) != START_CODE_PREFIX)
1300 {
1301 impeg2d_bit_stream_get(&s_bitstrm, 8);
1302
1303 if(s_bitstrm.u4_offset >= s_bitstrm.u4_max_offset)
1304 {
1305 break;
1306 }
1307 }
1308 }
1309
1310 /* Create job for the last slice row */
1311 {
1312 job_t s_job;
1313 IV_API_CALL_STATUS_T e_ret;
1314 s_job.i2_start_mb_y = i4_start_row;
1315 s_job.i2_end_mb_y = ps_dec->u2_num_vert_mb;
1316 s_job.i4_cmd = CMD_PROCESS;
1317 s_job.i4_bistream_ofst = i4_slice_bistream_ofst;
1318 e_ret = impeg2_jobq_queue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 0);
1319 if(e_ret != IV_SUCCESS)
1320 return e_ret;
1321
1322 }
1323 if((NULL != ps_dec->ps_disp_pic) && ((0 == ps_dec->u4_share_disp_buf) || (IV_YUV_420P != ps_dec->i4_chromaFormat)))
1324 {
1325 for(i = 0; i < ps_dec->u2_vertical_size; i+=64)
1326 {
1327 job_t s_job;
1328 IV_API_CALL_STATUS_T ret;
1329 s_job.i2_start_mb_y = i;
1330 s_job.i2_start_mb_y >>= 4;
1331 s_job.i2_end_mb_y = (i + 64);
1332 s_job.i2_end_mb_y >>= 4;
1333 s_job.i4_cmd = CMD_FMTCONV;
1334 s_job.i4_bistream_ofst = 0;
1335 ret = impeg2_jobq_queue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 0);
1336 if(ret != IV_SUCCESS)
1337 return ret;
1338
1339 }
1340 }
1341
1342 impeg2_jobq_terminate(ps_dec->pv_jobq);
1343 ps_dec->i4_bytes_consumed = s_bitstrm.u4_offset >> 3;
1344 ps_dec->i4_bytes_consumed -= ((size_t)s_bitstrm.pv_bs_buf & 3);
1345
1346 return 0;
1347}
1348
1349/*******************************************************************************
1350*
1351* Function Name : impeg2d_dec_pic_data
1352*
1353* Description : It intializes several parameters and decodes a Picture
1354* till any slice is left.
1355*
1356* Arguments :
1357* dec : Decoder context
1358*
1359* Values Returned : None
1360*******************************************************************************/
1361
1362void impeg2d_dec_pic_data(dec_state_t *ps_dec)
1363{
1364
1365 WORD32 i;
1366 dec_state_multi_core_t *ps_dec_state_multi_core;
1367
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301368 dec_state_t *ps_dec_thd;
1369 WORD32 i4_status;
1370 WORD32 i4_min_mb_y;
1371
1372
1373 /* Resetting the MB address and MB coordinates at the start of the Frame */
1374 ps_dec->u2_mb_x = ps_dec->u2_mb_y = 0;
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301375
1376 ps_dec_state_multi_core = ps_dec->ps_dec_state_multi_core;
1377 impeg2d_get_slice_pos(ps_dec_state_multi_core);
1378
1379 i4_min_mb_y = 1;
1380 for(i=0; i < ps_dec->i4_num_cores - 1; i++)
1381 {
1382 // initialize decoder context for thread
1383 // launch dec->u4_num_cores-1 threads
1384
1385 ps_dec_thd = ps_dec_state_multi_core->ps_dec_state[i+1];
1386
1387 ps_dec_thd->ps_disp_pic = ps_dec->ps_disp_pic;
1388 ps_dec_thd->ps_disp_frm_buf = ps_dec->ps_disp_frm_buf;
1389
1390 i4_status = impeg2d_init_thread_dec_ctxt(ps_dec, ps_dec_thd, i4_min_mb_y);
1391 //impeg2d_dec_pic_data_thread(ps_dec_thd);
1392
1393 if(i4_status == 0)
1394 {
1395 ithread_create(ps_dec_thd->pv_codec_thread_handle, NULL, (void *)impeg2d_dec_pic_data_thread, ps_dec_thd);
1396 ps_dec_state_multi_core->au4_thread_launched[i + 1] = 1;
1397 i4_min_mb_y = ps_dec_thd->u2_mb_y + 1;
1398 }
1399 else
1400 {
1401 ps_dec_state_multi_core->au4_thread_launched[i + 1] = 0;
1402 break;
1403 }
1404 }
1405
1406 impeg2d_dec_pic_data_thread(ps_dec);
1407
1408 // wait for threads to complete
1409 for(i=0; i < (ps_dec->i4_num_cores - 1); i++)
1410 {
1411 if(ps_dec_state_multi_core->au4_thread_launched[i + 1] == 1)
1412 {
1413 ps_dec_thd = ps_dec_state_multi_core->ps_dec_state[i+1];
1414 ithread_join(ps_dec_thd->pv_codec_thread_handle, NULL);
1415 }
1416 }
1417
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301418}
1419/*******************************************************************************
1420*
1421* Function Name : impeg2d_flush_ext_and_user_data
1422*
1423* Description : Flushes the extension and user data present in the
1424* stream_t
1425*
1426* Arguments :
1427* dec : Decoder context
1428*
1429* Values Returned : None
1430*******************************************************************************/
1431void impeg2d_flush_ext_and_user_data(dec_state_t *ps_dec)
1432{
1433 UWORD32 u4_start_code;
1434 stream_t *ps_stream;
1435
1436 ps_stream = &ps_dec->s_bit_stream;
1437 u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1438
Harish Mahendrakar8c743b52015-12-30 17:38:27 +05301439 while((u4_start_code == EXTENSION_START_CODE || u4_start_code == USER_DATA_START_CODE) &&
1440 (ps_stream->u4_offset < ps_stream->u4_max_offset))
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301441 {
1442 impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
Harish Mahendrakar8c743b52015-12-30 17:38:27 +05301443 while(impeg2d_bit_stream_nxt(ps_stream,START_CODE_PREFIX_LEN) != START_CODE_PREFIX &&
1444 (ps_stream->u4_offset < ps_stream->u4_max_offset))
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301445 {
1446 impeg2d_bit_stream_flush(ps_stream,8);
1447 }
1448 u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1449 }
1450}
1451/*******************************************************************************
1452*
1453* Function Name : impeg2d_dec_user_data
1454*
1455* Description : Flushes the user data present in the stream_t
1456*
1457* Arguments :
1458* dec : Decoder context
1459*
1460* Values Returned : None
1461*******************************************************************************/
1462void impeg2d_dec_user_data(dec_state_t *ps_dec)
1463{
1464 UWORD32 u4_start_code;
1465 stream_t *ps_stream;
1466
1467 ps_stream = &ps_dec->s_bit_stream;
1468 u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1469
1470 while(u4_start_code == USER_DATA_START_CODE)
1471 {
1472 impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
Harish Mahendrakar8c743b52015-12-30 17:38:27 +05301473 while((impeg2d_bit_stream_nxt(ps_stream,START_CODE_PREFIX_LEN) != START_CODE_PREFIX) &&
1474 (ps_stream->u4_offset < ps_stream->u4_max_offset))
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301475 {
1476 impeg2d_bit_stream_flush(ps_stream,8);
1477 }
1478 u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1479 }
1480}
1481/*******************************************************************************
1482* Function Name : impeg2d_dec_seq_ext_data
1483*
1484* Description : Decodes the extension data following Sequence
1485* Extension. It flushes any user data if present
1486*
1487* Arguments :
1488* dec : Decoder context
1489*
1490* Values Returned : None
1491*******************************************************************************/
1492IMPEG2D_ERROR_CODES_T impeg2d_dec_seq_ext_data(dec_state_t *ps_dec)
1493{
1494 stream_t *ps_stream;
1495 UWORD32 u4_start_code;
1496 IMPEG2D_ERROR_CODES_T e_error;
1497
1498 e_error = (IMPEG2D_ERROR_CODES_T) IVD_ERROR_NONE;
1499
1500 ps_stream = &ps_dec->s_bit_stream;
1501 u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1502 while( (u4_start_code == EXTENSION_START_CODE ||
1503 u4_start_code == USER_DATA_START_CODE) &&
Harish Mahendrakar8c743b52015-12-30 17:38:27 +05301504 (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE == e_error &&
1505 (ps_stream->u4_offset < ps_stream->u4_max_offset))
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301506 {
1507 if(u4_start_code == USER_DATA_START_CODE)
1508 {
1509 impeg2d_dec_user_data(ps_dec);
1510 }
1511 else
1512 {
1513 impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
1514 u4_start_code = impeg2d_bit_stream_nxt(ps_stream,EXT_ID_LEN);
1515 switch(u4_start_code)
1516 {
1517 case SEQ_DISPLAY_EXT_ID:
1518 impeg2d_dec_seq_disp_ext(ps_dec);
1519 break;
1520 case SEQ_SCALABLE_EXT_ID:
1521 e_error = IMPEG2D_SCALABILITIY_NOT_SUPPORTED;
1522 break;
1523 default:
1524 /* In case its a reserved extension code */
1525 impeg2d_bit_stream_flush(ps_stream,EXT_ID_LEN);
1526 impeg2d_peek_next_start_code(ps_dec);
1527 break;
1528 }
1529 }
1530 u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1531 }
1532 return e_error;
1533}
1534/*******************************************************************************
1535* Function Name : impeg2d_dec_pic_ext_data
1536*
1537* Description : Decodes the extension data following Picture Coding
1538* Extension. It flushes any user data if present
1539*
1540* Arguments :
1541* dec : Decoder context
1542*
1543* Values Returned : None
1544*******************************************************************************/
1545IMPEG2D_ERROR_CODES_T impeg2d_dec_pic_ext_data(dec_state_t *ps_dec)
1546{
1547 stream_t *ps_stream;
1548 UWORD32 u4_start_code;
1549 IMPEG2D_ERROR_CODES_T e_error;
1550
1551 e_error = (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
1552
1553 ps_stream = &ps_dec->s_bit_stream;
1554 u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1555 while ( (u4_start_code == EXTENSION_START_CODE ||
1556 u4_start_code == USER_DATA_START_CODE) &&
Harish Mahendrakar8c743b52015-12-30 17:38:27 +05301557 (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE == e_error &&
1558 (ps_stream->u4_offset < ps_stream->u4_max_offset))
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301559 {
1560 if(u4_start_code == USER_DATA_START_CODE)
1561 {
1562 impeg2d_dec_user_data(ps_dec);
1563 }
1564 else
1565 {
1566 impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
1567 u4_start_code = impeg2d_bit_stream_nxt(ps_stream,EXT_ID_LEN);
1568 switch(u4_start_code)
1569 {
1570 case QUANT_MATRIX_EXT_ID:
1571 impeg2d_dec_quant_matrix_ext(ps_dec);
1572 break;
1573 case COPYRIGHT_EXT_ID:
1574 impeg2d_dec_copyright_ext(ps_dec);
1575 break;
1576 case PIC_DISPLAY_EXT_ID:
1577 impeg2d_dec_pic_disp_ext(ps_dec);
1578 break;
1579 case CAMERA_PARAM_EXT_ID:
1580 impeg2d_dec_cam_param_ext(ps_dec);
1581 break;
1582 case ITU_T_EXT_ID:
1583 impeg2d_dec_itu_t_ext(ps_dec);
1584 break;
1585 case PIC_SPATIAL_SCALABLE_EXT_ID:
1586 case PIC_TEMPORAL_SCALABLE_EXT_ID:
1587 e_error = IMPEG2D_SCALABLITY_NOT_SUP;
1588 break;
1589 default:
1590 /* In case its a reserved extension code */
1591 impeg2d_bit_stream_flush(ps_stream,EXT_ID_LEN);
1592 impeg2d_next_start_code(ps_dec);
1593 break;
1594 }
1595 }
1596 u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1597 }
1598 return e_error;
1599}
1600
1601/*******************************************************************************
1602*
1603* Function Name : impeg2d_process_video_header
1604*
1605* Description : Processes video sequence header information
1606*
1607* Arguments :
1608* dec : Decoder context
1609*
1610* Values Returned : None
1611*******************************************************************************/
1612IMPEG2D_ERROR_CODES_T impeg2d_process_video_header(dec_state_t *ps_dec)
1613{
1614 stream_t *ps_stream;
1615 ps_stream = &ps_dec->s_bit_stream;
1616 IMPEG2D_ERROR_CODES_T e_error;
1617
1618 impeg2d_next_code(ps_dec, SEQUENCE_HEADER_CODE);
1619 if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1620 {
1621 e_error = impeg2d_dec_seq_hdr(ps_dec);
1622 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1623 {
1624 return e_error;
1625 }
1626 }
1627 else
1628 {
1629 return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1630 }
1631 if (impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) == EXTENSION_START_CODE)
1632 {
1633 /* MPEG2 Decoder */
1634 if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1635 {
1636 e_error = impeg2d_dec_seq_ext(ps_dec);
1637 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1638 {
1639 return e_error;
1640 }
1641 }
1642 else
1643 {
1644 return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1645 }
1646 if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1647 {
1648 e_error = impeg2d_dec_seq_ext_data(ps_dec);
1649 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1650 {
1651 return e_error;
1652 }
1653 }
1654 return impeg2d_init_video_state(ps_dec,MPEG_2_VIDEO);
1655 }
1656 else
1657 {
1658 /* MPEG1 Decoder */
1659 if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1660 {
1661 impeg2d_flush_ext_and_user_data(ps_dec);
1662 }
1663 return impeg2d_init_video_state(ps_dec,MPEG_1_VIDEO);
1664 }
1665}
1666/*******************************************************************************
1667*
1668* Function Name : impeg2d_process_video_bit_stream
1669*
1670* Description : Processes video sequence header information
1671*
1672* Arguments :
1673* dec : Decoder context
1674*
1675* Values Returned : None
1676*******************************************************************************/
1677IMPEG2D_ERROR_CODES_T impeg2d_process_video_bit_stream(dec_state_t *ps_dec)
1678{
1679 stream_t *ps_stream;
1680 UWORD32 u4_next_bits, u4_start_code_found;
1681 IMPEG2D_ERROR_CODES_T e_error;
1682
1683 ps_stream = &ps_dec->s_bit_stream;
1684 impeg2d_next_start_code(ps_dec);
1685 /* If the stream is MPEG-2 compliant stream */
1686 u4_start_code_found = 0;
1687
1688 if(ps_dec->u2_is_mpeg2)
1689 {
1690 /* MPEG2 decoding starts */
1691 while((u4_start_code_found == 0) && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
1692 {
1693 u4_next_bits = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1694
1695 if(u4_next_bits == SEQUENCE_HEADER_CODE)
1696 {
1697 if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1698 {
1699 e_error = impeg2d_dec_seq_hdr(ps_dec);
1700 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1701 {
1702 return e_error;
1703 }
1704
1705 u4_start_code_found = 0;
1706
1707 }
1708 else
1709 {
1710 return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1711 }
1712
1713
1714 if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1715 {
1716 IMPEG2D_ERROR_CODES_T e_error;
1717 e_error = impeg2d_dec_seq_ext(ps_dec);
1718 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1719 {
1720 return e_error;
1721 }
1722 u4_start_code_found = 0;
1723
1724 }
1725 else
1726 {
1727 return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1728 }
1729 }
1730 else if((u4_next_bits == USER_DATA_START_CODE) || (u4_next_bits == EXTENSION_START_CODE))
1731 {
1732 if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1733 {
1734 impeg2d_dec_seq_ext_data(ps_dec);
1735 u4_start_code_found = 0;
1736
1737 }
1738
1739 }
1740 else if((ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1741 && (u4_next_bits == GOP_START_CODE))
1742 {
1743 impeg2d_dec_grp_of_pic_hdr(ps_dec);
1744 impeg2d_dec_user_data(ps_dec);
1745 u4_start_code_found = 0;
1746
1747 }
1748 else if((ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1749 && (u4_next_bits == PICTURE_START_CODE))
1750 {
Venkatarama Avadhanib8578df2017-08-31 15:30:27 +05301751 ps_dec->i4_pic_count++;
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301752
1753 e_error = impeg2d_dec_pic_hdr(ps_dec);
1754 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1755 {
1756 return e_error;
1757 }
Venkatarama Avadhani849e1dd2017-04-20 09:26:54 +05301758 e_error = impeg2d_dec_pic_coding_ext(ps_dec);
1759 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1760 {
1761 return e_error;
1762 }
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301763 e_error = impeg2d_dec_pic_ext_data(ps_dec);
1764 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1765 {
1766 return e_error;
1767 }
Venkatarama Avadhani2249eda2017-06-09 12:12:03 +05301768 e_error = impeg2d_pre_pic_dec_proc(ps_dec);
1769 if ((IMPEG2D_ERROR_CODES_T) IVD_ERROR_NONE != e_error)
1770 {
1771 return e_error;
1772 }
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301773 impeg2d_dec_pic_data(ps_dec);
1774 impeg2d_post_pic_dec_proc(ps_dec);
1775 u4_start_code_found = 1;
1776 }
1777 else
1778
1779 {
1780 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);
1781
1782 }
1783 if(u4_start_code_found == 0)
1784 {
1785 impeg2d_next_start_code(ps_dec);
Venkatarama Avadhani9e92a782017-04-06 12:08:34 +05301786 /* In case a dec_pic_data call has not been made, the number of
1787 * bytes consumed in the previous header decode has to be
1788 * consumed. Not consuming it will result in zero bytes consumed
1789 * loops in case there are multiple headers and the second
1790 * or a future header has a resolution change/other error where
1791 * the bytes of the last header are not consumed.
1792 */
1793 ps_dec->i4_bytes_consumed = (ps_dec->s_bit_stream.u4_offset + 7) >> 3;
1794 ps_dec->i4_bytes_consumed -= ((size_t)ps_dec->s_bit_stream.pv_bs_buf & 3);
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301795 }
1796 }
1797 if((u4_start_code_found == 0) && (ps_dec->s_bit_stream.u4_offset > ps_dec->s_bit_stream.u4_max_offset))
1798 {
1799 return IMPEG2D_FRM_HDR_START_CODE_NOT_FOUND;
1800 }
1801
1802 }
1803 /* If the stream is MPEG-1 compliant stream */
1804 else
1805 {
1806 while((u4_start_code_found == 0) && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
1807 {
1808 u4_next_bits = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1809
1810 if(impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) == SEQUENCE_HEADER_CODE)
1811 {
1812 if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1813 {
1814 e_error = impeg2d_dec_seq_hdr(ps_dec);
1815 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1816 {
1817 return e_error;
1818 }
1819
1820 u4_start_code_found = 0;
1821 }
1822 else
1823 {
1824 return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1825 }
1826 }
1827 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))
1828 {
1829 impeg2d_flush_ext_and_user_data(ps_dec);
1830 u4_start_code_found = 0;
1831 }
1832
1833
1834 else if ((impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) == GOP_START_CODE)
1835 && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
1836 {
1837 impeg2d_dec_grp_of_pic_hdr(ps_dec);
1838 impeg2d_flush_ext_and_user_data(ps_dec);
1839 u4_start_code_found = 0;
1840 }
1841 else if ((impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) == PICTURE_START_CODE)
1842 && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
1843 {
Venkatarama Avadhanib8578df2017-08-31 15:30:27 +05301844 ps_dec->i4_pic_count++;
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301845
1846 e_error = impeg2d_dec_pic_hdr(ps_dec);
1847 if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1848 {
1849 return e_error;
1850 }
1851 impeg2d_flush_ext_and_user_data(ps_dec);
1852 impeg2d_pre_pic_dec_proc(ps_dec);
1853 impeg2d_dec_pic_data(ps_dec);
1854 impeg2d_post_pic_dec_proc(ps_dec);
1855 u4_start_code_found = 1;
1856 }
1857 else
1858 {
1859 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);
1860 }
1861 impeg2d_next_start_code(ps_dec);
Venkatarama Avadhani9e92a782017-04-06 12:08:34 +05301862 if (0 == u4_start_code_found)
1863 {
1864 /* In case a dec_pic_data call has not been made, the number of
1865 * bytes consumed in the previous header decode has to be
1866 * consumed. Not consuming it will result in zero bytes consumed
1867 * loops in case there are multiple headers and the second
1868 * or a future header has a resolution change/other error where
1869 * the bytes of the last header are not consumed.
1870 */
1871 ps_dec->i4_bytes_consumed = (ps_dec->s_bit_stream.u4_offset + 7) >> 3;
1872 ps_dec->i4_bytes_consumed -= ((size_t)ps_dec->s_bit_stream.pv_bs_buf & 3);
1873 }
Venkatarama Avadhaniaed24ee2015-03-11 10:08:57 +05301874 }
1875 if((u4_start_code_found == 0) && (ps_dec->s_bit_stream.u4_offset > ps_dec->s_bit_stream.u4_max_offset))
1876 {
1877 return IMPEG2D_FRM_HDR_START_CODE_NOT_FOUND;
1878 }
1879 }
1880
1881 return (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
1882}