Coverage Report

Created: 2025-10-10 06:56

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