Coverage Report

Created: 2026-04-12 06:14

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