Coverage Report

Created: 2026-09-14 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libmpeg2/decoder/impeg2d_i_pic.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
21
#include "iv_datatypedef.h"
22
#include "iv.h"
23
24
#include "impeg2_buf_mgr.h"
25
#include "impeg2_disp_mgr.h"
26
#include "impeg2_defs.h"
27
#include "impeg2_platform_macros.h"
28
#include "impeg2_inter_pred.h"
29
#include "impeg2_idct.h"
30
#include "impeg2_globals.h"
31
#include "impeg2_format_conv.h"
32
#include "impeg2_macros.h"
33
34
#include "ivd.h"
35
#include "impeg2d.h"
36
#include "impeg2d_bitstream.h"
37
#include "impeg2d_structs.h"
38
#include "impeg2d_vld_tables.h"
39
#include "impeg2d_vld.h"
40
#include "impeg2d_pic_proc.h"
41
#include "impeg2d_debug.h"
42
#include "impeg2d_globals.h"
43
#include "impeg2d_mv_dec.h"
44
45
/*******************************************************************************
46
*  Function Name   : impeg2d_dec_i_mb_params
47
*
48
*  Description     : Decoding I MB parameters.
49
*
50
*  Arguments       :
51
*  dec             : Decoder state
52
*  stream          : Bitstream
53
*
54
*  Values Returned : None
55
*******************************************************************************/
56
void impeg2d_dec_i_mb_params(dec_state_t *ps_dec)
57
377k
{
58
59
377k
    UWORD16 u2_next_bits;
60
377k
    UWORD16 u2_bits_to_flush;
61
377k
    stream_t *ps_stream = &ps_dec->s_bit_stream;
62
63
    /*-----------------------------------------------------------------------*/
64
    /* Flush the MBAddrIncr Bit                                              */
65
    /*                                                                       */
66
    /* Since we are not supporting scalable modes there won't be skipped     */
67
    /* macroblocks in I-Picture and the MBAddrIncr will always be 1,         */
68
    /* The MBAddrIncr can never be greater than 1 for the simple and main    */
69
    /* profile MPEG2.                                                        */
70
    /*-----------------------------------------------------------------------*/
71
377k
    if(impeg2d_bit_stream_nxt(ps_stream,1) == 1) //Making sure the increment is one.
72
296k
    {
73
296k
        impeg2d_bit_stream_flush(ps_stream,1);
74
296k
    }
75
80.5k
    else if(ps_dec->u2_first_mb && ps_dec->u2_mb_x)
76
1.14k
    {
77
1.14k
        WORD32 i4_mb_add_inc = impeg2d_get_mb_addr_incr(ps_stream);
78
79
            //VOLParams->FirstInSlice = 0;
80
            /****************************************************************/
81
            /* Section 6.3.17                                               */
82
            /* The first MB of a slice cannot be skipped                    */
83
            /* But the mb_addr_incr can be > 1, because at the beginning of */
84
            /* a slice, it indicates the offset from the last MB in the     */
85
            /* previous row. Hence for the first slice in a row, the        */
86
            /* mb_addr_incr needs to be 1.                                  */
87
            /****************************************************************/
88
            /* MB_x is set to zero whenever MB_y changes.                   */
89
90
1.14k
            ps_dec->u2_mb_x = i4_mb_add_inc - 1;
91
1.14k
            ps_dec->u2_mb_x = MIN(ps_dec->u2_mb_x, (ps_dec->u2_num_horiz_mb - 1));
92
1.14k
    }
93
94
    /*-----------------------------------------------------------------------*/
95
    /* Decode the macroblock_type, dct_type and quantiser_scale_code         */
96
    /*                                                                       */
97
    /* macroblock_type      2 bits [can be either 1 or 01]                   */
98
    /* dct_type             1 bit                                            */
99
    /* quantiser_scale_code 5 bits                                           */
100
    /*-----------------------------------------------------------------------*/
101
377k
    u2_next_bits = impeg2d_bit_stream_nxt(ps_stream,8);
102
377k
    if(BIT(u2_next_bits,7) == 1)
103
217k
    {
104
        /* read the dct_type if needed */
105
217k
        u2_bits_to_flush = 1;
106
217k
        if(ps_dec->u2_read_dct_type)
107
2.64k
        {
108
2.64k
            u2_bits_to_flush++;
109
2.64k
            ps_dec->u2_field_dct = BIT(u2_next_bits,6);
110
2.64k
        }
111
217k
    }
112
160k
    else
113
160k
    {
114
160k
        u2_bits_to_flush = 7;
115
        /*------------------------------------------------------------------*/
116
        /* read the dct_type if needed                                      */
117
        /*------------------------------------------------------------------*/
118
160k
        if(ps_dec->u2_read_dct_type)
119
990
        {
120
990
            u2_bits_to_flush++;
121
990
            ps_dec->u2_field_dct = BIT(u2_next_bits,5);
122
990
        }
123
159k
        else
124
159k
        {
125
159k
            u2_next_bits >>= 1;
126
159k
        }
127
        /*------------------------------------------------------------------*/
128
        /* Quant scale code decoding                                        */
129
        /*------------------------------------------------------------------*/
130
160k
        {
131
160k
            UWORD16 quant_scale_code;
132
160k
            quant_scale_code = u2_next_bits & 0x1F;
133
134
160k
            ps_dec->u1_quant_scale = (ps_dec->u2_q_scale_type) ?
135
109k
                gau1_impeg2_non_linear_quant_scale[quant_scale_code] :
136
160k
                (quant_scale_code << 1);
137
160k
        }
138
160k
    }
139
377k
    impeg2d_bit_stream_flush(ps_stream,u2_bits_to_flush);
140
    /*************************************************************************/
141
    /* Decoding of motion vectors if concealment motion vectors are present  */
142
    /*************************************************************************/
143
377k
    if(ps_dec->u2_concealment_motion_vectors)
144
115k
    {
145
115k
        if(ps_dec->u2_picture_structure != FRAME_PICTURE)
146
114k
            impeg2d_bit_stream_flush(ps_stream,1);
147
115k
        impeg2d_dec_mv(ps_stream,ps_dec->ai2_pred_mv[FORW][FIRST],ps_dec->ai2_mv[FORW][FIRST],
148
115k
            ps_dec->au2_f_code[FORW],0,0);
149
150
        /* Flush the marker bit */
151
115k
        if(0 == (impeg2d_bit_stream_get(ps_stream,1)))
152
33.3k
        {
153
            /* Ignore marker bit error */
154
33.3k
        }
155
156
115k
    }
157
377k
    ps_dec->u2_first_mb = 0;
158
377k
    return;
159
377k
}
160
/*******************************************************************************
161
*  Function Name   : impeg2d_dec_i_slice
162
*
163
*  Description     : Decodes I slice
164
*
165
*  Arguments       :
166
*  dec             : Decoder state
167
*
168
*  Values Returned : None
169
*******************************************************************************/
170
IMPEG2D_ERROR_CODES_T impeg2d_dec_i_slice(dec_state_t *ps_dec)
171
10.4k
{
172
10.4k
    WORD16 *pi2_vld_out;
173
10.4k
    UWORD32 i;
174
10.4k
    yuv_buf_t *ps_cur_frm_buf = &ps_dec->s_cur_frm_buf;
175
176
10.4k
    UWORD32 u4_frame_width = ps_dec->u2_frame_width;
177
10.4k
    UWORD32 u4_frm_offset = 0;
178
10.4k
    UWORD8  *pu1_out_p;
179
10.4k
    IMPEG2D_ERROR_CODES_T e_error = (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
180
181
182
10.4k
    pi2_vld_out = ps_dec->ai2_vld_buf;
183
184
185
10.4k
    if(ps_dec->u2_picture_structure != FRAME_PICTURE)
186
3.96k
    {
187
3.96k
        u4_frame_width <<= 1;
188
3.96k
        if(ps_dec->u2_picture_structure == BOTTOM_FIELD)
189
365
        {
190
365
            u4_frm_offset = ps_dec->u2_frame_width;
191
365
        }
192
3.96k
    }
193
194
10.4k
    do
195
377k
    {
196
377k
        UWORD32 u4_x_offset,u4_y_offset;
197
377k
        UWORD32 u4_blk_pos;
198
377k
        UWORD32 u4_x_dst_offset = 0;
199
377k
        UWORD32 u4_y_dst_offset = 0;
200
201
202
377k
        IMPEG2D_TRACE_MB_START(ps_dec->u2_mb_x, ps_dec->u2_mb_y);
203
204
377k
        impeg2d_dec_i_mb_params(ps_dec);
205
206
377k
        u4_x_dst_offset = u4_frm_offset + (ps_dec->u2_mb_x << 4);
207
377k
        u4_y_dst_offset = (ps_dec->u2_mb_y << 4) * u4_frame_width;
208
377k
        pu1_out_p = ps_cur_frm_buf->pu1_y + u4_x_dst_offset + u4_y_dst_offset;
209
210
1.85M
        for(i = 0; i < NUM_LUMA_BLKS; ++i)
211
1.48M
        {
212
213
1.48M
            e_error = ps_dec->pf_vld_inv_quant(ps_dec, pi2_vld_out,
214
1.48M
                                            ps_dec->pu1_inv_scan_matrix, 1, Y_LUMA, 0);
215
1.48M
            if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
216
3.74k
            {
217
3.74k
                return e_error;
218
3.74k
            }
219
220
1.47M
            u4_x_offset = gai2_impeg2_blk_x_off[i];
221
222
1.47M
            if(ps_dec->u2_field_dct == 0)
223
1.48M
                u4_y_offset = gai2_impeg2_blk_y_off_frm[i] ;
224
18.4E
            else
225
18.4E
                u4_y_offset = gai2_impeg2_blk_y_off_fld[i] ;
226
227
1.47M
            u4_blk_pos = u4_y_offset * u4_frame_width + u4_x_offset;
228
1.47M
            IMPEG2D_IDCT_INP_STATISTICS(pi2_vld_out, ps_dec->u4_non_zero_cols, ps_dec->u4_non_zero_rows);
229
230
1.47M
            PROFILE_DISABLE_IDCT_IF0
231
1.47M
            {
232
1.47M
                WORD32 i4_idx;
233
1.47M
                i4_idx = 1;
234
1.47M
                if(1 == (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows))
235
437k
                    i4_idx = 0;
236
237
1.47M
                ps_dec->pf_idct_recon[i4_idx * 2 + ps_dec->i4_last_value_one](pi2_vld_out,
238
1.47M
                                                        ps_dec->ai2_idct_stg1,
239
1.47M
                                                        (UWORD8 *)gau1_impeg2_zerobuf,
240
1.47M
                                                        pu1_out_p + u4_blk_pos,
241
1.47M
                                                        8,
242
1.47M
                                                        8,
243
1.47M
                                                        u4_frame_width << ps_dec->u2_field_dct,
244
1.47M
                                                        ~ps_dec->u4_non_zero_cols, ~ps_dec->u4_non_zero_rows);
245
246
1.47M
            }
247
248
1.47M
        }
249
250
        /* For U and V blocks, divide the x and y offsets by 2. */
251
373k
        u4_x_dst_offset >>= 1;
252
373k
        u4_y_dst_offset >>= 2;
253
254
        /* In case of chrominance blocks the DCT will be frame DCT */
255
        /* i = 0, U component and */
256
257
373k
        e_error = ps_dec->pf_vld_inv_quant(ps_dec, pi2_vld_out,
258
373k
                                        ps_dec->pu1_inv_scan_matrix, 1, U_CHROMA, 0);
259
373k
        if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
260
1.13k
        {
261
1.13k
            return e_error;
262
1.13k
        }
263
264
372k
        pu1_out_p = ps_cur_frm_buf->pu1_u + u4_x_dst_offset + u4_y_dst_offset;
265
372k
        IMPEG2D_IDCT_INP_STATISTICS(pi2_vld_out, ps_dec->u4_non_zero_cols, ps_dec->u4_non_zero_rows);
266
372k
        PROFILE_DISABLE_IDCT_IF0
267
372k
        {
268
372k
            WORD32 i4_idx;
269
372k
            i4_idx = 1;
270
372k
            if(1 == (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows))
271
238k
                i4_idx = 0;
272
273
372k
            ps_dec->pf_idct_recon[i4_idx * 2 + ps_dec->i4_last_value_one](pi2_vld_out,
274
372k
                                                    ps_dec->ai2_idct_stg1,
275
372k
                                                    (UWORD8 *)gau1_impeg2_zerobuf,
276
372k
                                                    pu1_out_p,
277
372k
                                                    8,
278
372k
                                                    8,
279
372k
                                                    u4_frame_width >> 1,
280
372k
                                                    ~ps_dec->u4_non_zero_cols, ~ps_dec->u4_non_zero_rows);
281
282
372k
        }
283
        /* Write the idct_out block to the current frame dec->curFrame*/
284
        /* In case of field DCT type, write to alternate lines */
285
372k
        e_error = ps_dec->pf_vld_inv_quant(ps_dec, pi2_vld_out,
286
372k
                                        ps_dec->pu1_inv_scan_matrix, 1, V_CHROMA, 0);
287
372k
        if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
288
908
        {
289
908
            return e_error;
290
908
        }
291
292
371k
        pu1_out_p = ps_cur_frm_buf->pu1_v + u4_x_dst_offset + u4_y_dst_offset;
293
371k
        IMPEG2D_IDCT_INP_STATISTICS(pi2_vld_out, ps_dec->u4_non_zero_cols, ps_dec->u4_non_zero_rows);
294
371k
        PROFILE_DISABLE_IDCT_IF0
295
371k
        {
296
371k
            WORD32 i4_idx;
297
371k
            i4_idx = 1;
298
371k
            if(1 == (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows))
299
241k
                i4_idx = 0;
300
371k
            ps_dec->pf_idct_recon[i4_idx * 2 + ps_dec->i4_last_value_one](pi2_vld_out,
301
371k
                                                    ps_dec->ai2_idct_stg1,
302
371k
                                                    (UWORD8 *)gau1_impeg2_zerobuf,
303
371k
                                                    pu1_out_p,
304
371k
                                                    8,
305
371k
                                                    8,
306
371k
                                                    u4_frame_width >> 1,
307
371k
                                                    ~ps_dec->u4_non_zero_cols, ~ps_dec->u4_non_zero_rows);
308
371k
        }
309
371k
        ps_dec->u2_num_mbs_left--;
310
311
312
371k
        ps_dec->u2_mb_x++;
313
314
371k
        if(ps_dec->s_bit_stream.u4_offset > ps_dec->s_bit_stream.u4_max_offset)
315
1.11k
        {
316
1.11k
            return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
317
1.11k
        }
318
370k
        else if (ps_dec->u2_mb_x == ps_dec->u2_num_horiz_mb)
319
10.0k
        {
320
10.0k
            ps_dec->u2_mb_x = 0;
321
10.0k
            ps_dec->u2_mb_y++;
322
10.0k
        }
323
324
371k
    }
325
370k
    while(ps_dec->u2_num_mbs_left != 0 && impeg2d_bit_stream_nxt(&ps_dec->s_bit_stream,23) != 0x0);
326
3.53k
    return e_error;
327
10.4k
}