Coverage Report

Created: 2026-12-01 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libavc/decoder/ih264d_utils.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
 **************************************************************************
22
 * \file ih264d_utils.c
23
 *
24
 * \brief
25
 *    Contains routines that handle of start and end of pic processing
26
 *
27
 * \date
28
 *    19/12/2002
29
 *
30
 * \author  AI
31
 **************************************************************************
32
 */
33
34
#include <string.h>
35
#include "ih264_typedefs.h"
36
#include "ithread.h"
37
#include "ih264d_deblocking.h"
38
#include "ih264d_parse_slice.h"
39
#include "ih264d_parse_cavlc.h"
40
#include "ih264d_dpb_manager.h"
41
#include "ih264d_defs.h"
42
#include "ih264d_structs.h"
43
#include "ih264d_mem_request.h"
44
#include "ih264_typedefs.h"
45
#include "ih264_macros.h"
46
#include "ih264_platform_macros.h"
47
#include "ih264d_tables.h"
48
#include "ih264d_debug.h"
49
#include "ih264d_mb_utils.h"
50
#include "ih264d_error_handler.h"
51
#include "ih264d_dpb_manager.h"
52
#include "ih264d_utils.h"
53
#include "ih264d_defs.h"
54
#include "ih264d_tables.h"
55
#include "ih264d_inter_pred.h"
56
#include "ih264d_dpb_manager.h"
57
#include "iv.h"
58
#include "ivd.h"
59
#include "ih264d_format_conv.h"
60
#include "ih264_error.h"
61
#include "ih264_disp_mgr.h"
62
#include "ih264_buf_mgr.h"
63
#include "ih264d_utils.h"
64
65
/*!
66
 **************************************************************************
67
 * \if Function name : ih264d_is_end_of_pic \endif
68
 *
69
 * \brief
70
 *    Determines whether current slice is first slice of a new picture as
71
 *    defined in 7.4.1.2.4 of 14496-10.
72
 *
73
 * \return
74
 *    Return 1 if current slice is first slice of a new picture
75
 *    Otherwise it returns 0
76
 **************************************************************************
77
 */
78
UWORD8 ih264d_is_end_of_pic(UWORD16 u2_frame_num,
79
                            UWORD8 u1_nal_ref_idc,
80
                            pocstruct_t *ps_cur_poc,
81
                            pocstruct_t *ps_prev_poc,
82
                            dec_slice_params_t * ps_prev_slice, /*!< Previous slice parameters*/
83
                            UWORD8 u1_pic_order_cnt_type,
84
                            UWORD8 u1_nal_unit_type,
85
                            UWORD32 u4_idr_pic_id,
86
                            UWORD8 u1_field_pic_flag,
87
                            UWORD8 u1_bottom_field_flag)
88
0
{
89
0
    WORD8 i1_is_end_of_pic;
90
0
    WORD8 a, b, c, d, e, f, g, h;
91
92
0
    a = b = c = d = e = f = g = h = 0;
93
0
    a = (ps_prev_slice->u2_frame_num != u2_frame_num);
94
0
    b = (ps_prev_slice->u1_field_pic_flag != u1_field_pic_flag);
95
0
    if(u1_field_pic_flag && ps_prev_slice->u1_field_pic_flag)
96
0
        c = (u1_bottom_field_flag != ps_prev_slice->u1_bottom_field_flag);
97
0
    d =
98
0
                    (u1_nal_ref_idc == 0 && ps_prev_slice->u1_nal_ref_idc != 0)
99
0
                                    || (u1_nal_ref_idc != 0
100
0
                                                    && ps_prev_slice->u1_nal_ref_idc
101
0
                                                                    == 0);
102
0
    if(!a)
103
0
    {
104
0
        if((u1_pic_order_cnt_type == 0)
105
0
                        && (ps_prev_slice->u1_pic_order_cnt_type == 0))
106
0
        {
107
0
            e =
108
0
                            ((ps_cur_poc->i4_pic_order_cnt_lsb
109
0
                                            != ps_prev_poc->i4_pic_order_cnt_lsb)
110
0
                                            || (ps_cur_poc->i4_delta_pic_order_cnt_bottom
111
0
                                                            != ps_prev_poc->i4_delta_pic_order_cnt_bottom));
112
0
        }
113
114
0
        if((u1_pic_order_cnt_type == 1)
115
0
                        && (ps_prev_slice->u1_pic_order_cnt_type == 1))
116
0
        {
117
0
            f =
118
0
                            ((ps_cur_poc->i4_delta_pic_order_cnt[0]
119
0
                                            != ps_prev_poc->i4_delta_pic_order_cnt[0])
120
0
                                            || (ps_cur_poc->i4_delta_pic_order_cnt[1]
121
0
                                                            != ps_prev_poc->i4_delta_pic_order_cnt[1]));
122
0
        }
123
0
    }
124
125
0
    if((u1_nal_unit_type == IDR_SLICE_NAL)
126
0
                    && (ps_prev_slice->u1_nal_unit_type == IDR_SLICE_NAL))
127
0
    {
128
0
        g = (u4_idr_pic_id != ps_prev_slice->u4_idr_pic_id);
129
0
    }
130
131
0
    if((u1_nal_unit_type == IDR_SLICE_NAL)
132
0
                    && (ps_prev_slice->u1_nal_unit_type != IDR_SLICE_NAL))
133
0
    {
134
0
        h = 1;
135
0
    }
136
0
    i1_is_end_of_pic = a + b + c + d + e + f + g + h;
137
0
    return (i1_is_end_of_pic);
138
0
}
139
140
/*!
141
 **************************************************************************
142
 * \if Function name : ih264d_decode_pic_order_cnt \endif
143
 *
144
 * \brief
145
 *    Calculates picture order count of picture.
146
 *
147
 * \return
148
 *    Returns the pic order count of the picture to which current
149
 *    Slice belongs.
150
 *
151
 **************************************************************************
152
 */
153
WORD32 ih264d_decode_pic_order_cnt(UWORD8 u1_is_idr_slice,
154
                                   UWORD32 u2_frame_num,
155
                                   pocstruct_t *ps_prev_poc,
156
                                   pocstruct_t *ps_cur_poc,
157
                                   dec_slice_params_t *ps_cur_slice, /*!< Pointer to current slice Params*/
158
                                   dec_pic_params_t * ps_pps,
159
                                   UWORD8 u1_nal_ref_idc,
160
                                   UWORD8 u1_bottom_field_flag,
161
                                   UWORD8 u1_field_pic_flag,
162
                                   WORD32 *pi4_poc)
163
207k
{
164
207k
    WORD64 i8_pic_msb;
165
207k
    WORD32 i4_top_field_order_cnt = 0, i4_bottom_field_order_cnt = 0;
166
207k
    dec_seq_params_t *ps_seq = ps_pps->ps_sps;
167
207k
    WORD32 i4_prev_frame_num_ofst;
168
169
207k
    switch(ps_seq->u1_pic_order_cnt_type)
170
207k
    {
171
33.6k
        case 0:
172
            /* POC TYPE 0 */
173
33.6k
            if(u1_is_idr_slice)
174
8.66k
            {
175
8.66k
                ps_cur_poc->i4_pic_order_cnt_msb = 0;
176
8.66k
                ps_cur_poc->i4_pic_order_cnt_lsb = 0;
177
8.66k
            }
178
33.6k
            if(ps_prev_poc->u1_mmco_equalto5)
179
0
            {
180
0
                if(ps_prev_poc->u1_bot_field != 1)
181
0
                {
182
0
                    ps_prev_poc->i4_pic_order_cnt_msb = 0;
183
0
                    ps_prev_poc->i4_pic_order_cnt_lsb =
184
0
                                    ps_prev_poc->i4_top_field_order_count;
185
0
                }
186
0
                else
187
0
                {
188
0
                    ps_prev_poc->i4_pic_order_cnt_msb = 0;
189
0
                    ps_prev_poc->i4_pic_order_cnt_lsb = 0;
190
0
                }
191
0
            }
192
193
33.6k
            if((ps_cur_poc->i4_pic_order_cnt_lsb
194
33.6k
                            < ps_prev_poc->i4_pic_order_cnt_lsb)
195
5.98k
                            && ((ps_prev_poc->i4_pic_order_cnt_lsb
196
5.98k
                                            - ps_cur_poc->i4_pic_order_cnt_lsb)
197
5.98k
                                            >= (ps_seq->i4_max_pic_order_cntLsb
198
5.98k
                                                            >> 1)))
199
2.47k
            {
200
2.47k
                i8_pic_msb = (WORD64)ps_prev_poc->i4_pic_order_cnt_msb
201
2.47k
                                + ps_seq->i4_max_pic_order_cntLsb;
202
2.47k
            }
203
31.1k
            else if((ps_cur_poc->i4_pic_order_cnt_lsb
204
31.1k
                            > ps_prev_poc->i4_pic_order_cnt_lsb)
205
8.87k
                            && ((ps_cur_poc->i4_pic_order_cnt_lsb
206
8.87k
                                            - ps_prev_poc->i4_pic_order_cnt_lsb)
207
8.87k
                                            >= (ps_seq->i4_max_pic_order_cntLsb
208
8.87k
                                                            >> 1)))
209
3.56k
            {
210
3.56k
                i8_pic_msb = (WORD64)ps_prev_poc->i4_pic_order_cnt_msb
211
3.56k
                                - ps_seq->i4_max_pic_order_cntLsb;
212
3.56k
            }
213
27.6k
            else
214
27.6k
            {
215
27.6k
                i8_pic_msb = ps_prev_poc->i4_pic_order_cnt_msb;
216
27.6k
            }
217
218
33.6k
            if(!u1_field_pic_flag || !u1_bottom_field_flag)
219
33.6k
            {
220
33.6k
                WORD64 i8_result = i8_pic_msb + ps_cur_poc->i4_pic_order_cnt_lsb;
221
33.6k
                if(IS_OUT_OF_RANGE_S32(i8_result))
222
0
                {
223
0
                    return ERROR_INV_POC;
224
0
                }
225
33.6k
                i4_top_field_order_cnt = i8_result;
226
33.6k
            }
227
228
33.6k
            if(!u1_field_pic_flag)
229
33.6k
            {
230
33.6k
                WORD64 i8_result = (WORD64)i4_top_field_order_cnt
231
33.6k
                                     + ps_cur_poc->i4_delta_pic_order_cnt_bottom;
232
33.6k
                if(IS_OUT_OF_RANGE_S32(i8_result))
233
11
                {
234
11
                    return ERROR_INV_POC;
235
11
                }
236
33.6k
                i4_bottom_field_order_cnt = i8_result;
237
33.6k
            }
238
0
            else if(u1_bottom_field_flag)
239
0
            {
240
0
                WORD64 i8_result = i8_pic_msb + ps_cur_poc->i4_pic_order_cnt_lsb;
241
0
                if(IS_OUT_OF_RANGE_S32(i8_result))
242
0
                {
243
0
                    return ERROR_INV_POC;
244
0
                }
245
0
                i4_bottom_field_order_cnt = i8_result;
246
0
            }
247
248
33.6k
            if(IS_OUT_OF_RANGE_S32(i8_pic_msb))
249
0
            {
250
0
                return ERROR_INV_POC;
251
0
            }
252
33.6k
            ps_cur_poc->i4_pic_order_cnt_msb = i8_pic_msb;
253
33.6k
            break;
254
255
107k
        case 1:
256
107k
        {
257
            /* POC TYPE 1 */
258
107k
            UWORD8 i;
259
107k
            WORD32 prev_frame_num;
260
107k
            WORD32 frame_num_ofst;
261
107k
            WORD32 abs_frm_num;
262
107k
            WORD32 poc_cycle_cnt, frame_num_in_poc_cycle;
263
107k
            WORD64 i8_expected_delta_poc_cycle;
264
107k
            WORD32 expected_poc;
265
107k
            WORD64 i8_result;
266
267
107k
            prev_frame_num = (WORD32)ps_cur_slice->u2_frame_num;
268
107k
            if(!u1_is_idr_slice)
269
100k
            {
270
100k
                if(ps_cur_slice->u1_mmco_equalto5)
271
2.62k
                {
272
2.62k
                    prev_frame_num = 0;
273
2.62k
                    i4_prev_frame_num_ofst = 0;
274
2.62k
                }
275
98.2k
                else
276
98.2k
                {
277
98.2k
                    i4_prev_frame_num_ofst = ps_prev_poc->i4_prev_frame_num_ofst;
278
98.2k
                }
279
100k
            }
280
6.95k
            else
281
6.95k
                i4_prev_frame_num_ofst = 0;
282
283
            /* 1. Derivation for FrameNumOffset */
284
107k
            if(u1_is_idr_slice)
285
6.95k
            {
286
6.95k
                frame_num_ofst = 0;
287
6.95k
                ps_cur_poc->i4_delta_pic_order_cnt[0] = 0;
288
6.95k
                ps_cur_poc->i4_delta_pic_order_cnt[1] = 0;
289
6.95k
            }
290
100k
            else if(prev_frame_num > ((WORD32)u2_frame_num))
291
8.34k
            {
292
8.34k
                WORD64 i8_result = i4_prev_frame_num_ofst
293
8.34k
                                + (WORD64)ps_seq->u2_u4_max_pic_num_minus1 + 1;
294
8.34k
                if(IS_OUT_OF_RANGE_S32(i8_result))
295
0
                {
296
0
                    return ERROR_INV_FRAME_NUM;
297
0
                }
298
8.34k
                frame_num_ofst = i8_result;
299
8.34k
            }
300
92.5k
            else
301
92.5k
                frame_num_ofst = i4_prev_frame_num_ofst;
302
303
            /* 2. Derivation for absFrameNum */
304
107k
            if(0 != ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle)
305
10.7k
            {
306
10.7k
                WORD64 i8_result = frame_num_ofst + (WORD64)u2_frame_num;
307
10.7k
                if(IS_OUT_OF_RANGE_S32(i8_result))
308
0
                {
309
0
                    return ERROR_INV_FRAME_NUM;
310
0
                }
311
10.7k
                abs_frm_num = i8_result;
312
10.7k
            }
313
97.0k
            else
314
97.0k
                abs_frm_num = 0;
315
107k
            if((u1_nal_ref_idc == 0) && (abs_frm_num > 0))
316
653
                abs_frm_num = abs_frm_num - 1;
317
318
            /* 4. expectedDeltaPerPicOrderCntCycle is derived as */
319
107k
            i8_expected_delta_poc_cycle = 0;
320
181k
            for(i = 0; i < ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle;
321
107k
                            i++)
322
73.4k
            {
323
73.4k
                i8_expected_delta_poc_cycle +=
324
73.4k
                                ps_seq->i4_ofst_for_ref_frame[i];
325
73.4k
            }
326
327
            /* 3. When absFrameNum > 0, picOrderCntCycleCnt and
328
             frame_num_in_poc_cycle are derived as : */
329
            /* 5. expectedPicOrderCnt is derived as : */
330
107k
            if(abs_frm_num > 0)
331
10.4k
            {
332
10.4k
                poc_cycle_cnt =
333
10.4k
                                DIV((abs_frm_num - 1),
334
10.4k
                                    ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle);
335
10.4k
                frame_num_in_poc_cycle =
336
10.4k
                                MOD((abs_frm_num - 1),
337
10.4k
                                    ps_seq->u1_num_ref_frames_in_pic_order_cnt_cycle);
338
339
10.4k
                i8_result = poc_cycle_cnt
340
10.4k
                                * i8_expected_delta_poc_cycle;
341
342
42.7k
                for(i = 0; i <= frame_num_in_poc_cycle; i++)
343
32.3k
                {
344
32.3k
                    i8_result = i8_result
345
32.3k
                                    + ps_seq->i4_ofst_for_ref_frame[i];
346
32.3k
                }
347
348
10.4k
                if(IS_OUT_OF_RANGE_S32(i8_result))
349
8
                    return ERROR_INV_POC;
350
351
10.3k
                expected_poc = (WORD32)i8_result;
352
353
10.3k
            }
354
97.4k
            else
355
97.4k
                expected_poc = 0;
356
357
107k
            if(u1_nal_ref_idc == 0)
358
7.03k
            {
359
7.03k
                i8_result = (WORD64)expected_poc
360
7.03k
                                + ps_seq->i4_ofst_for_non_ref_pic;
361
362
7.03k
                if(IS_OUT_OF_RANGE_S32(i8_result))
363
0
                    return ERROR_INV_POC;
364
365
7.03k
                expected_poc = (WORD32)i8_result;
366
7.03k
            }
367
368
            /* 6. TopFieldOrderCnt or BottomFieldOrderCnt are derived as */
369
107k
            if(!u1_field_pic_flag)
370
107k
            {
371
107k
                i8_result = (WORD64)expected_poc
372
107k
                                + ps_cur_poc->i4_delta_pic_order_cnt[0];
373
374
107k
                if(IS_OUT_OF_RANGE_S32(i8_result))
375
11
                    return ERROR_INV_POC;
376
107k
                i4_top_field_order_cnt = (WORD32)i8_result;
377
378
107k
                i8_result = (WORD64)i4_top_field_order_cnt
379
107k
                                + ps_seq->i4_ofst_for_top_to_bottom_field
380
107k
                                + ps_cur_poc->i4_delta_pic_order_cnt[1];
381
382
107k
                if(IS_OUT_OF_RANGE_S32(i8_result))
383
2
                    return ERROR_INV_POC;
384
107k
                i4_bottom_field_order_cnt = (WORD32)i8_result;
385
107k
            }
386
0
            else if(!u1_bottom_field_flag)
387
0
            {
388
0
                i8_result = (WORD64)expected_poc
389
0
                                + ps_cur_poc->i4_delta_pic_order_cnt[0];
390
391
0
                if(IS_OUT_OF_RANGE_S32(i8_result))
392
0
                    return ERROR_INV_POC;
393
0
                i4_top_field_order_cnt = (WORD32)i8_result;
394
0
            }
395
0
            else
396
0
            {
397
0
                i8_result = (WORD64)expected_poc
398
0
                                + ps_seq->i4_ofst_for_top_to_bottom_field
399
0
                                + ps_cur_poc->i4_delta_pic_order_cnt[0];
400
401
0
                if(IS_OUT_OF_RANGE_S32(i8_result))
402
0
                    return ERROR_INV_POC;
403
0
                i4_bottom_field_order_cnt = (WORD32)i8_result;
404
0
            }
405
            /* Copy the current POC info into Previous POC structure */
406
107k
            ps_cur_poc->i4_prev_frame_num_ofst = frame_num_ofst;
407
107k
        }
408
409
0
            break;
410
65.6k
        case 2:
411
65.6k
        {
412
            /* POC TYPE 2 */
413
65.6k
            WORD32 prev_frame_num;
414
65.6k
            WORD32 frame_num_ofst;
415
65.6k
            WORD32 tmp_poc;
416
417
65.6k
            prev_frame_num = (WORD32)ps_cur_slice->u2_frame_num;
418
65.6k
            if(!u1_is_idr_slice)
419
56.2k
            {
420
56.2k
                if(ps_cur_slice->u1_mmco_equalto5)
421
232
                {
422
232
                    prev_frame_num = 0;
423
232
                    i4_prev_frame_num_ofst = 0;
424
232
                }
425
55.9k
                else
426
55.9k
                    i4_prev_frame_num_ofst = ps_prev_poc->i4_prev_frame_num_ofst;
427
56.2k
            }
428
9.44k
            else
429
9.44k
                i4_prev_frame_num_ofst = 0;
430
431
            /* 1. Derivation for FrameNumOffset */
432
65.6k
            if(u1_is_idr_slice)
433
9.44k
            {
434
9.44k
                frame_num_ofst = 0;
435
9.44k
                ps_cur_poc->i4_delta_pic_order_cnt[0] = 0;
436
9.44k
                ps_cur_poc->i4_delta_pic_order_cnt[1] = 0;
437
9.44k
            }
438
56.2k
            else if(prev_frame_num > ((WORD32)u2_frame_num))
439
3.22k
            {
440
3.22k
                WORD64 i8_result = i4_prev_frame_num_ofst
441
3.22k
                                + (WORD64)ps_seq->u2_u4_max_pic_num_minus1 + 1;
442
3.22k
                if(IS_OUT_OF_RANGE_S32(i8_result))
443
0
                {
444
0
                    return ERROR_INV_FRAME_NUM;
445
0
                }
446
3.22k
                frame_num_ofst = i8_result;
447
3.22k
            }
448
52.9k
            else
449
52.9k
                frame_num_ofst = i4_prev_frame_num_ofst;
450
451
            /* 2. Derivation for tempPicOrderCnt */
452
65.6k
            if(u1_is_idr_slice)
453
9.44k
                tmp_poc = 0;
454
56.2k
            else if(u1_nal_ref_idc == 0)
455
3.48k
            {
456
3.48k
                WORD64 i8_result = ((frame_num_ofst + (WORD64)u2_frame_num) << 1) - 1;
457
3.48k
                if(IS_OUT_OF_RANGE_S32(i8_result))
458
0
                {
459
0
                    return ERROR_INV_POC;
460
0
                }
461
3.48k
                tmp_poc = i8_result;
462
3.48k
            }
463
52.7k
            else
464
52.7k
            {
465
52.7k
                WORD64 i8_result = (frame_num_ofst + (WORD64)u2_frame_num) << 1;
466
52.7k
                if(IS_OUT_OF_RANGE_S32(i8_result))
467
0
                {
468
0
                    return ERROR_INV_POC;
469
0
                }
470
52.7k
                tmp_poc = i8_result;
471
52.7k
            }
472
473
            /* 6. TopFieldOrderCnt or BottomFieldOrderCnt are derived as */
474
65.6k
            if(!u1_field_pic_flag)
475
65.6k
            {
476
65.6k
                i4_top_field_order_cnt = tmp_poc;
477
65.6k
                i4_bottom_field_order_cnt = tmp_poc;
478
65.6k
            }
479
0
            else if(!u1_bottom_field_flag)
480
0
                i4_top_field_order_cnt = tmp_poc;
481
0
            else
482
0
                i4_bottom_field_order_cnt = tmp_poc;
483
484
            /* Copy the current POC info into Previous POC structure */
485
65.6k
            ps_prev_poc->i4_prev_frame_num_ofst = frame_num_ofst;
486
65.6k
            ps_cur_poc->i4_prev_frame_num_ofst = frame_num_ofst;
487
65.6k
        }
488
0
            break;
489
0
        default:
490
0
            return ERROR_INV_POC_TYPE_T;
491
0
            break;
492
207k
    }
493
494
207k
    if(!u1_field_pic_flag) // or a complementary field pair
495
207k
    {
496
207k
        *pi4_poc = MIN(i4_top_field_order_cnt, i4_bottom_field_order_cnt);
497
207k
        ps_pps->i4_top_field_order_cnt = i4_top_field_order_cnt;
498
207k
        ps_pps->i4_bottom_field_order_cnt = i4_bottom_field_order_cnt;
499
207k
    }
500
0
    else if(!u1_bottom_field_flag)
501
0
    {
502
0
        *pi4_poc = i4_top_field_order_cnt;
503
0
        ps_pps->i4_top_field_order_cnt = i4_top_field_order_cnt;
504
0
    }
505
0
    else
506
0
    {
507
0
        *pi4_poc = i4_bottom_field_order_cnt;
508
0
        ps_pps->i4_bottom_field_order_cnt = i4_bottom_field_order_cnt;
509
0
    }
510
511
207k
    ps_pps->i4_avg_poc = *pi4_poc;
512
513
207k
    return OK;
514
207k
}
515
516
/*!
517
 **************************************************************************
518
 * \if Function name : ih264d_end_of_pic_processing \endif
519
 *
520
 * \brief
521
 *    Performs the end of picture processing.
522
 *
523
 * It performs deblocking on the current picture and sets the i4_status of
524
 * current picture as decoded.
525
 *
526
 * \return
527
 *    0 on Success and Error code otherwise.
528
 **************************************************************************
529
 */
530
WORD32 ih264d_end_of_pic_processing(dec_struct_t *ps_dec)
531
0
{
532
0
    UWORD8 u1_pic_type, u1_nal_ref_idc;
533
0
    dec_slice_params_t *ps_cur_slice = ps_dec->ps_cur_slice;
534
535
    /* If nal_ref_idc is equal to 0 for one slice or slice data partition NAL
536
     unit of a particular picture, it shall be equal to 0 for all slice and
537
     slice data partition NAL units of the picture. nal_ref_idc greater
538
     than 0 indicates that the content of the NAL unit belongs to a decoded
539
     picture that is stored and marked for use as a reference picture in the
540
     decoded picture buffer. */
541
542
    /* 1. Do MMCO
543
     2. Add Cur Pic to list of reference pics.
544
     */
545
546
    /* Call MMCO */
547
0
    u1_pic_type = 0;
548
0
    u1_nal_ref_idc = ps_cur_slice->u1_nal_ref_idc;
549
550
0
    if(ps_dec->u4_disable_ref_retention &&
551
0
       (0 == ps_dec->u1_top_bottom_decoded))
552
0
    {
553
        /* Frame is complete; do not retain references across frames */
554
0
        ih264d_reset_ref_bufs(ps_dec->ps_dpb_mgr);
555
0
        ih264d_update_default_index_list(ps_dec->ps_dpb_mgr);
556
0
        ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
557
0
                              ps_dec->u1_pic_buf_id,
558
0
                              BUF_MGR_REF);
559
0
        ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_mv_buf_mgr,
560
0
                              ps_dec->as_buf_id_info_map[ps_dec->u1_pic_buf_id].mv_buf_id,
561
0
                              BUF_MGR_REF);
562
0
    }
563
0
    else if(u1_nal_ref_idc)
564
0
    {
565
0
        if(ps_cur_slice->u1_nal_unit_type == IDR_SLICE_NAL)
566
0
        {
567
0
            ps_dec->ps_dpb_mgr->u1_mmco_error_in_seq = 0;
568
0
            if(ps_dec->ps_dpb_cmds->u1_long_term_reference_flag == 0)
569
0
            {
570
0
                ih264d_reset_ref_bufs(ps_dec->ps_dpb_mgr);
571
                /* ignore DPB errors */
572
0
                ih264d_insert_st_node(ps_dec->ps_dpb_mgr,
573
0
                                      ps_dec->ps_cur_pic,
574
0
                                      ps_dec->u1_pic_buf_id,
575
0
                                      ps_cur_slice->u2_frame_num);
576
0
                ps_dec->ps_dpb_mgr->u1_max_lt_frame_idx = NO_LONG_TERM_INDICIES;
577
0
            }
578
0
            else
579
0
            {
580
                /* Equivalent of inserting a pic directly as longterm Pic */
581
582
0
                {
583
                    /* ignore DPB errors */
584
0
                    ih264d_insert_st_node(ps_dec->ps_dpb_mgr,
585
0
                                          ps_dec->ps_cur_pic,
586
0
                                          ps_dec->u1_pic_buf_id,
587
0
                                          ps_cur_slice->u2_frame_num);
588
589
                    /* Set longTermIdx = 0, MaxLongTermFrameIdx = 0 */
590
0
                    ih264d_delete_st_node_or_make_lt(
591
0
                                    ps_dec->ps_dpb_mgr,
592
0
                                    ps_cur_slice->u2_frame_num, 0,
593
0
                                    ps_cur_slice->u1_field_pic_flag);
594
595
0
                    ps_dec->ps_dpb_mgr->u1_max_lt_frame_idx = 0;
596
0
                }
597
0
            }
598
0
        }
599
0
        else
600
0
        {
601
602
0
            {
603
0
                UWORD16 u2_pic_num = ps_cur_slice->u2_frame_num;
604
605
0
                if(!ps_dec->ps_dpb_mgr->u1_mmco_error_in_seq)
606
0
                {
607
0
                    WORD32 ret = ih264d_do_mmco_buffer(ps_dec->ps_dpb_cmds, ps_dec->ps_dpb_mgr,
608
0
                                               ps_dec->ps_cur_sps->u1_num_ref_frames, u2_pic_num,
609
0
                                               (ps_dec->ps_cur_sps->u2_u4_max_pic_num_minus1),
610
0
                                               ps_dec->u1_nal_unit_type, ps_dec->ps_cur_pic,
611
0
                                               ps_dec->u1_pic_buf_id,
612
0
                                               ps_cur_slice->u1_field_pic_flag,
613
0
                                               ps_dec->e_dec_status);
614
0
                    ps_dec->ps_dpb_mgr->u1_mmco_error_in_seq = ret != OK;
615
0
                }
616
0
            }
617
0
        }
618
0
        ih264d_update_default_index_list(ps_dec->ps_dpb_mgr);
619
0
    }
620
621
0
    if(ps_cur_slice->u1_field_pic_flag)
622
0
    {
623
0
        if(ps_cur_slice->u1_bottom_field_flag)
624
0
        {
625
0
            if(u1_nal_ref_idc)
626
0
                u1_pic_type = u1_pic_type | BOT_REF;
627
0
            u1_pic_type = u1_pic_type | BOT_FLD;
628
0
        }
629
0
        else
630
0
        {
631
0
            if(u1_nal_ref_idc)
632
0
                u1_pic_type = u1_pic_type | TOP_REF;
633
0
            u1_pic_type = u1_pic_type | TOP_FLD;
634
0
        }
635
0
    }
636
0
    else
637
0
        u1_pic_type = TOP_REF | BOT_REF;
638
0
    ps_dec->ps_cur_pic->u1_pic_type |= u1_pic_type;
639
640
641
0
    if(ps_cur_slice->u1_field_pic_flag)
642
0
    {
643
0
        H264_DEC_DEBUG_PRINT("Toggling secondField\n");
644
0
        ps_dec->u1_second_field = 1 - ps_dec->u1_second_field;
645
0
    }
646
647
0
    return OK;
648
0
}
649
650
/*****************************************************************************/
651
/*                                                                           */
652
/*  Function Name : init_dpb_size                                            */
653
/*                                                                           */
654
/*  Description   : This function calculates the DBP i4_size in frames          */
655
/*  Inputs        : ps_seq - current sequence params                         */
656
/*                                                                           */
657
/*  Globals       : None                                                     */
658
/*                                                                           */
659
/*  Outputs       : None                                                     */
660
/*                                                                           */
661
/*  Returns       : DPB in frames                                            */
662
/*                                                                           */
663
/*  Issues        : None                                                     */
664
/*                                                                           */
665
/*  Revision History:                                                        */
666
/*                                                                           */
667
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
668
/*         28 04 2005   NS              Draft                                */
669
/*                                                                           */
670
/*****************************************************************************/
671
WORD32 ih264d_get_dpb_size(dec_seq_params_t *ps_seq)
672
20.4k
{
673
20.4k
    WORD32 i4_size;
674
20.4k
    UWORD8 u1_level_idc;
675
676
20.4k
    u1_level_idc = ps_seq->u1_level_idc;
677
678
20.4k
    switch(u1_level_idc)
679
20.4k
    {
680
512
        case 10:
681
512
            i4_size = 152064;
682
512
            break;
683
878
        case 11:
684
878
            i4_size = 345600;
685
878
            break;
686
511
        case 12:
687
511
            i4_size = 912384;
688
511
            break;
689
378
        case 13:
690
378
            i4_size = 912384;
691
378
            break;
692
792
        case 20:
693
792
            i4_size = 912384;
694
792
            break;
695
409
        case 21:
696
409
            i4_size = 1824768;
697
409
            break;
698
239
        case 22:
699
239
            i4_size = 3110400;
700
239
            break;
701
343
        case 30:
702
343
            i4_size = 3110400;
703
343
            break;
704
1.11k
        case 31:
705
1.11k
            i4_size = 6912000;
706
1.11k
            break;
707
1.05k
        case 32:
708
1.05k
            i4_size = 7864320;
709
1.05k
            break;
710
411
        case 40:
711
411
            i4_size = 12582912;
712
411
            break;
713
439
        case 41:
714
439
            i4_size = 12582912;
715
439
            break;
716
340
        case 42:
717
340
            i4_size = 12582912;
718
340
            break;
719
382
        case 50:
720
382
            i4_size = 42393600;
721
382
            break;
722
190
        case 51:
723
190
            i4_size = 70778880;
724
190
            break;
725
0
        case 52:
726
0
            i4_size = 70778880;
727
0
            break;
728
12.4k
        default:
729
12.4k
            i4_size = 70778880;
730
12.4k
            break;
731
20.4k
    }
732
733
20.4k
    i4_size /= (ps_seq->u2_frm_wd_in_mbs * (ps_seq->u2_frm_ht_in_mbs << (1 - ps_seq->u1_frame_mbs_only_flag)));
734
20.4k
    i4_size /= 384;
735
20.4k
    i4_size = MIN(i4_size, 16);
736
20.4k
    i4_size = MAX(i4_size, 1);
737
20.4k
    return (i4_size);
738
20.4k
}
739
740
/**************************************************************************/
741
/* This function initialises the value of ps_dec->u4_recon_mb_grp         */
742
/* ps_dec->u4_recon_mb_grp must satisfy the following criteria            */
743
/*  - multiple of 2 (required for N/2 parse-mvpred design)                */
744
/*  - multiple of 4 (if it is not a frame_mbs_only sequence),             */
745
/*         in this case N/2 itself needs to be even for mbpair processing */
746
/*  - lesser than ps_dec->u2_frm_wd_in_mbs/2 (at least 3 N-Chunks       */
747
/*         should make a row to ensure proper MvTop transferring)         */
748
/**************************************************************************/
749
WORD32 ih264d_init_dec_mb_grp(dec_struct_t *ps_dec)
750
3.30k
{
751
3.30k
    dec_seq_params_t *ps_seq = ps_dec->ps_cur_sps;
752
3.30k
    UWORD8 u1_frm = ps_seq->u1_frame_mbs_only_flag;
753
754
3.30k
    ps_dec->u4_recon_mb_grp = ps_dec->u2_frm_wd_in_mbs << ps_seq->u1_mb_aff_flag;
755
756
3.30k
    ps_dec->u4_recon_mb_grp_pair = ps_dec->u4_recon_mb_grp >> 1;
757
758
3.30k
    if(!ps_dec->u4_recon_mb_grp)
759
0
    {
760
0
        return ERROR_MB_GROUP_ASSGN_T;
761
0
    }
762
763
3.30k
    ps_dec->u4_num_mbs_prev_nmb = ps_dec->u4_recon_mb_grp;
764
765
3.30k
    return OK;
766
3.30k
}
767
768
769
/*!
770
 **************************************************************************
771
 * \if Function name : ih264d_init_pic \endif
772
 *
773
 * \brief
774
 *    Initializes the picture.
775
 *
776
 * \return
777
 *    0 on Success and Error code otherwise
778
 *
779
 * \note
780
 *    This function is called when first slice of the
781
 *    NON -IDR picture is encountered.
782
 **************************************************************************
783
 */
784
WORD32 ih264d_init_pic(dec_struct_t *ps_dec,
785
                       UWORD16 u2_frame_num,
786
                       WORD32 i4_poc,
787
                       dec_pic_params_t *ps_pps)
788
0
{
789
0
    dec_seq_params_t *ps_seq = ps_pps->ps_sps;
790
0
    prev_seq_params_t * ps_prev_seq_params = &ps_dec->s_prev_seq_params;
791
0
    WORD32 i4_pic_bufs;
792
0
    WORD32 ret;
793
794
0
    ps_dec->ps_cur_slice->u2_frame_num = u2_frame_num;
795
0
    ps_dec->ps_cur_slice->i4_poc = i4_poc;
796
0
    ps_dec->ps_cur_pps = ps_pps;
797
0
    ps_dec->ps_cur_pps->pv_codec_handle = ps_dec;
798
799
0
    ps_dec->ps_cur_sps = ps_seq;
800
0
    ps_dec->ps_dpb_mgr->i4_max_frm_num = ps_seq->u2_u4_max_pic_num_minus1
801
0
                    + 1;
802
803
0
    ps_dec->ps_dpb_mgr->u2_pic_ht = ps_dec->u2_pic_ht;
804
0
    ps_dec->ps_dpb_mgr->u2_pic_wd = ps_dec->u2_pic_wd;
805
0
    ps_dec->i4_pic_type = NA_SLICE;
806
0
    ps_dec->i4_frametype = IV_NA_FRAME;
807
0
    ps_dec->i4_content_type = IV_CONTENTTYPE_NA;
808
809
    /*--------------------------------------------------------------------*/
810
    /* Get the value of MaxMbAddress and frmheight in Mbs                 */
811
    /*--------------------------------------------------------------------*/
812
0
    ps_seq->u4_max_mb_addr =
813
0
                    ((UWORD32)ps_seq->u2_frm_wd_in_mbs
814
0
                                    * ((UWORD32)ps_dec->u2_pic_ht
815
0
                                                    >> (4
816
0
                                                                    + ps_dec->ps_cur_slice->u1_field_pic_flag)))
817
0
                                    - 1;
818
0
    ps_dec->u2_frm_ht_in_mbs = (ps_dec->u2_pic_ht
819
0
                    >> (4 + ps_dec->ps_cur_slice->u1_field_pic_flag));
820
821
    /***************************************************************************/
822
    /* If change in Level or the required PicBuffers i4_size is more than the  */
823
    /* current one FREE the current PicBuffers and allocate affresh            */
824
    /***************************************************************************/
825
0
    if(!ps_dec->u1_init_dec_flag)
826
0
    {
827
0
        ps_dec->u1_max_dec_frame_buffering = ih264d_get_dpb_size(ps_seq);
828
829
0
        ps_dec->i4_display_delay = ps_dec->u1_max_dec_frame_buffering;
830
0
        if((1 == ps_seq->u1_vui_parameters_present_flag) &&
831
0
           (1 == ps_seq->s_vui.u1_bitstream_restriction_flag))
832
0
        {
833
0
            if(ps_seq->u1_frame_mbs_only_flag == 1)
834
0
                ps_dec->i4_display_delay = ps_seq->s_vui.u4_num_reorder_frames + 1;
835
0
            else
836
0
                ps_dec->i4_display_delay = ps_seq->s_vui.u4_num_reorder_frames * 2 + 2;
837
0
        }
838
839
0
        if(IVD_DECODE_FRAME_OUT == ps_dec->e_frm_out_mode)
840
0
            ps_dec->i4_display_delay = 0;
841
842
0
        if(ps_dec->u4_share_disp_buf == 0)
843
0
        {
844
0
            UWORD8 u1_num_ref_frames =
845
0
                ps_dec->u4_disable_ref_retention ? 0 : ps_seq->u1_num_ref_frames;
846
0
            if(ps_seq->u1_frame_mbs_only_flag == 1)
847
0
                ps_dec->u1_pic_bufs = ps_dec->i4_display_delay + u1_num_ref_frames + 1;
848
0
            else
849
0
                ps_dec->u1_pic_bufs = ps_dec->i4_display_delay + u1_num_ref_frames * 2 + 2;
850
0
        }
851
0
        else
852
0
        {
853
0
            ps_dec->u1_pic_bufs = (WORD32)ps_dec->u4_num_disp_bufs;
854
0
        }
855
856
0
        if(!ps_dec->u4_disable_ref_retention)
857
0
        {
858
            /* Ensure at least two buffers are allocated */
859
0
            ps_dec->u1_pic_bufs = MAX(ps_dec->u1_pic_bufs, 2);
860
0
        }
861
862
0
        if(ps_dec->u4_share_disp_buf == 0)
863
0
            ps_dec->u1_pic_bufs = MIN(ps_dec->u1_pic_bufs,
864
0
                                      (H264_MAX_REF_PICS * 2));
865
866
0
        ps_dec->u1_max_dec_frame_buffering = MIN(
867
0
                        ps_dec->u1_max_dec_frame_buffering,
868
0
                        ps_dec->u1_pic_bufs);
869
870
        /* Temporary hack to run Tractor Cav/Cab/MbAff Profiler streams  also for CAFI1_SVA_C.264 in conformance*/
871
0
        if(ps_dec->u1_init_dec_flag)
872
0
        {
873
0
            ih264d_release_pics_in_dpb((void *)ps_dec,
874
0
                                       ps_dec->u1_pic_bufs);
875
0
            ih264d_release_display_bufs(ps_dec);
876
0
            ih264d_reset_ref_bufs(ps_dec->ps_dpb_mgr);
877
0
        }
878
879
        /*********************************************************************/
880
        /* Configuring decoder parameters based on level and then            */
881
        /* fresh pointer initialisation in decoder scratch and state buffers */
882
        /*********************************************************************/
883
0
        if(!ps_dec->u1_init_dec_flag ||
884
0
                ((ps_seq->u1_level_idc < H264_LEVEL_3_0) ^ (ps_prev_seq_params->u1_level_idc < H264_LEVEL_3_0)))
885
0
        {
886
0
            ret = ih264d_init_dec_mb_grp(ps_dec);
887
0
            if(ret != OK)
888
0
                return ret;
889
0
        }
890
891
0
        ret = ih264d_allocate_dynamic_bufs(ps_dec);
892
0
        if(ret != OK)
893
0
        {
894
            /* Free any dynamic buffers that are allocated */
895
0
            ih264d_free_dynamic_bufs(ps_dec);
896
0
            ps_dec->i4_error_code = IVD_MEM_ALLOC_FAILED;
897
0
            return IVD_MEM_ALLOC_FAILED;
898
0
        }
899
900
0
        ret = ih264d_create_pic_buffers(ps_dec->u1_pic_bufs,
901
0
                                        ps_dec);
902
0
        if(ret != OK)
903
0
            return ret;
904
905
906
907
0
        ret = ih264d_create_mv_bank(ps_dec, ps_dec->u2_pic_wd,
908
0
                                    ps_dec->u2_pic_ht);
909
0
        if(ret != OK)
910
0
            return ret;
911
912
        /* In shared mode, set all of them as used by display */
913
0
        if(ps_dec->u4_share_disp_buf == 1)
914
0
        {
915
0
            WORD32 i;
916
917
0
            for(i = 0; i < ps_dec->u1_pic_bufs; i++)
918
0
            {
919
0
                ih264_buf_mgr_set_status((buf_mgr_t *)ps_dec->pv_pic_buf_mgr, i,
920
0
                                         BUF_MGR_IO);
921
0
            }
922
0
        }
923
924
0
        ps_dec->u1_init_dec_flag = 1;
925
0
        ps_prev_seq_params->u2_frm_wd_in_mbs = ps_seq->u2_frm_wd_in_mbs;
926
0
        ps_prev_seq_params->u1_level_idc = ps_seq->u1_level_idc;
927
0
        ps_prev_seq_params->u1_profile_idc = ps_seq->u1_profile_idc;
928
0
        ps_prev_seq_params->u2_frm_ht_in_mbs = ps_seq->u2_frm_ht_in_mbs;
929
0
        ps_prev_seq_params->u1_frame_mbs_only_flag =
930
0
                        ps_seq->u1_frame_mbs_only_flag;
931
0
        ps_prev_seq_params->u1_direct_8x8_inference_flag =
932
0
                        ps_seq->u1_direct_8x8_inference_flag;
933
934
0
        ps_dec->i4_cur_display_seq = 0;
935
0
        ps_dec->i4_prev_max_display_seq = 0;
936
0
        ps_dec->i4_max_poc = 0;
937
938
0
        {
939
            /* 0th entry of CtxtIncMbMap will be always be containing default values
940
             for CABAC context representing MB not available */
941
0
            ctxt_inc_mb_info_t *p_DefCtxt = ps_dec->p_ctxt_inc_mb_map - 1;
942
0
            UWORD8 *pu1_temp;
943
0
            WORD8 i;
944
0
            p_DefCtxt->u1_mb_type = CAB_SKIP;
945
946
0
            p_DefCtxt->u1_cbp = 0x0f;
947
0
            p_DefCtxt->u1_intra_chroma_pred_mode = 0;
948
949
0
            p_DefCtxt->u1_yuv_dc_csbp = 0x7;
950
951
0
            p_DefCtxt->u1_transform8x8_ctxt = 0;
952
953
0
            pu1_temp = (UWORD8*)p_DefCtxt->i1_ref_idx;
954
0
            for(i = 0; i < 4; i++, pu1_temp++)
955
0
                (*pu1_temp) = 0;
956
0
            pu1_temp = (UWORD8*)p_DefCtxt->u1_mv;
957
0
            for(i = 0; i < 16; i++, pu1_temp++)
958
0
                (*pu1_temp) = 0;
959
0
            ps_dec->ps_def_ctxt_mb_info = p_DefCtxt;
960
0
        }
961
962
0
    }
963
    /* reset DBP commands read u4_flag */
964
0
    ps_dec->ps_dpb_cmds->u1_dpb_commands_read = 0;
965
966
0
    return OK;
967
0
}
968
969
/*****************************************************************************/
970
/*                                                                           */
971
/*  Function Name : ih264d_get_next_display_field                                   */
972
/*                                                                           */
973
/*  Description   : Application calls this module to get the next field      */
974
/*                  to be displayed                                          */
975
/*                                                                           */
976
/*  Inputs        : 1.   IBUFAPI_Handle Hnadle to the Display buffer         */
977
/*                  2.   IH264DEC_DispUnit    Pointer to the display struct  */
978
/*                                                                           */
979
/*  Globals       :                                                          */
980
/*                                                                           */
981
/*                                                                           */
982
/*  Processing    : None                                                     */
983
/*  Outputs       : None                                                     */
984
/*  Returns       : None                                                     */
985
/*  Issues        : None                                                     */
986
/*                                                                           */
987
/*  Revision History:                                                        */
988
/*                                                                           */
989
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
990
/*         27 05 2005   Ittiam          Draft                                */
991
/*                                                                           */
992
/*****************************************************************************/
993
994
WORD32 ih264d_get_next_display_field(dec_struct_t * ps_dec,
995
                                  ivd_out_bufdesc_t *ps_out_buffer,
996
                                  ivd_get_display_frame_op_t *pv_disp_op)
997
0
{
998
0
    pic_buffer_t *pic_buf;
999
1000
0
    UWORD8 i1_cur_fld;
1001
0
    WORD32 u4_api_ret = -1;
1002
0
    WORD32 i4_disp_buf_id;
1003
0
    iv_yuv_buf_t *ps_op_frm;
1004
1005
1006
1007
0
    ps_op_frm = &(ps_dec->s_disp_frame_info);
1008
0
    H264_MUTEX_LOCK(&ps_dec->process_disp_mutex);
1009
0
    pic_buf = (pic_buffer_t *)ih264_disp_mgr_get(
1010
0
                    (disp_mgr_t *)ps_dec->pv_disp_buf_mgr, &i4_disp_buf_id);
1011
0
    ps_dec->u4_num_fld_in_frm = 0;
1012
0
    u4_api_ret = -1;
1013
0
    pv_disp_op->u4_ts = 0;
1014
0
    pv_disp_op->e_output_format = ps_dec->u1_chroma_format;
1015
1016
0
    pv_disp_op->s_disp_frm_buf.pv_y_buf = ps_out_buffer->pu1_bufs[0];
1017
0
    pv_disp_op->s_disp_frm_buf.pv_u_buf = ps_out_buffer->pu1_bufs[1];
1018
0
    pv_disp_op->s_disp_frm_buf.pv_v_buf = ps_out_buffer->pu1_bufs[2];
1019
0
    ps_dec->i4_display_index  = DEFAULT_POC;
1020
0
    if(pic_buf != NULL)
1021
0
    {
1022
0
        ps_dec->pv_disp_sei_params = &pic_buf->s_sei_pic;
1023
0
        pv_disp_op->e4_fld_type = 0;
1024
0
        pv_disp_op->u4_disp_buf_id = i4_disp_buf_id;
1025
1026
0
        ps_op_frm->u4_y_ht = pic_buf->u2_disp_height << 1;
1027
0
        ps_op_frm->u4_u_ht = ps_op_frm->u4_v_ht = ps_op_frm->u4_y_ht >> 1;
1028
0
        ps_op_frm->u4_y_wd = pic_buf->u2_disp_width;
1029
1030
0
        ps_op_frm->u4_u_wd = ps_op_frm->u4_v_wd = ps_op_frm->u4_y_wd >> 1;
1031
1032
0
        ps_op_frm->u4_y_strd = pic_buf->u2_frm_wd_y;
1033
0
        ps_op_frm->u4_u_strd = ps_op_frm->u4_v_strd = pic_buf->u2_frm_wd_uv;
1034
1035
        /* ! */
1036
0
        pv_disp_op->u4_ts = pic_buf->u4_ts;
1037
0
        ps_dec->i4_display_index = pic_buf->i4_poc;
1038
1039
        /* set the start of the Y, U and V buffer pointer for display    */
1040
0
        ps_op_frm->pv_y_buf = pic_buf->pu1_buf1 + pic_buf->u2_crop_offset_y;
1041
0
        ps_op_frm->pv_u_buf = pic_buf->pu1_buf2 + pic_buf->u2_crop_offset_uv;
1042
0
        ps_op_frm->pv_v_buf = pic_buf->pu1_buf3 + pic_buf->u2_crop_offset_uv;
1043
0
        ps_dec->u4_num_fld_in_frm++;
1044
0
        ps_dec->u4_num_fld_in_frm++;
1045
0
        u4_api_ret = 0;
1046
1047
0
        if(pic_buf->u1_picturetype == 0)
1048
0
            pv_disp_op->u4_progressive_frame_flag = 1;
1049
0
        else
1050
0
            pv_disp_op->u4_progressive_frame_flag = 0;
1051
1052
0
    } H264_MUTEX_UNLOCK(&ps_dec->process_disp_mutex);
1053
0
    pv_disp_op->u4_error_code = u4_api_ret;
1054
0
    pv_disp_op->e_pic_type = 0xFFFFFFFF; //Junk;
1055
1056
0
    if(u4_api_ret)
1057
0
    {
1058
0
        pv_disp_op->u4_error_code = 1; //put a proper error code here
1059
0
    }
1060
0
    else
1061
0
    {
1062
1063
        //Release the buffer if being sent for display
1064
0
        UWORD32 temp;
1065
0
        UWORD32 dest_inc_Y = 0, dest_inc_UV = 0;
1066
1067
0
        pv_disp_op->s_disp_frm_buf.u4_y_wd = temp = MIN(ps_op_frm->u4_y_wd,
1068
0
                                                        ps_op_frm->u4_y_strd);
1069
0
        pv_disp_op->s_disp_frm_buf.u4_u_wd = pv_disp_op->s_disp_frm_buf.u4_y_wd
1070
0
                        >> 1;
1071
0
        pv_disp_op->s_disp_frm_buf.u4_v_wd = pv_disp_op->s_disp_frm_buf.u4_y_wd
1072
0
                        >> 1;
1073
1074
0
        pv_disp_op->s_disp_frm_buf.u4_y_ht = ps_op_frm->u4_y_ht;
1075
0
        pv_disp_op->s_disp_frm_buf.u4_u_ht = pv_disp_op->s_disp_frm_buf.u4_y_ht
1076
0
                        >> 1;
1077
0
        pv_disp_op->s_disp_frm_buf.u4_v_ht = pv_disp_op->s_disp_frm_buf.u4_y_ht
1078
0
                        >> 1;
1079
0
        if(0 == ps_dec->u4_share_disp_buf)
1080
0
        {
1081
0
            pv_disp_op->s_disp_frm_buf.u4_y_strd =
1082
0
                            pv_disp_op->s_disp_frm_buf.u4_y_wd;
1083
0
            pv_disp_op->s_disp_frm_buf.u4_u_strd =
1084
0
                            pv_disp_op->s_disp_frm_buf.u4_y_wd >> 1;
1085
0
            pv_disp_op->s_disp_frm_buf.u4_v_strd =
1086
0
                            pv_disp_op->s_disp_frm_buf.u4_y_wd >> 1;
1087
1088
0
        }
1089
0
        else
1090
0
        {
1091
0
            pv_disp_op->s_disp_frm_buf.u4_y_strd = ps_op_frm->u4_y_strd;
1092
0
        }
1093
1094
0
        if(ps_dec->u4_app_disp_width)
1095
0
        {
1096
0
            pv_disp_op->s_disp_frm_buf.u4_y_strd = MAX(
1097
0
                            ps_dec->u4_app_disp_width,
1098
0
                            pv_disp_op->s_disp_frm_buf.u4_y_strd);
1099
0
        }
1100
1101
0
        pv_disp_op->u4_error_code = 0;
1102
0
        if(pv_disp_op->e_output_format == IV_YUV_420P)
1103
0
        {
1104
0
            UWORD32 i;
1105
0
            pv_disp_op->s_disp_frm_buf.u4_u_strd =
1106
0
                            pv_disp_op->s_disp_frm_buf.u4_y_strd >> 1;
1107
0
            pv_disp_op->s_disp_frm_buf.u4_v_strd =
1108
0
                            pv_disp_op->s_disp_frm_buf.u4_y_strd >> 1;
1109
1110
0
            pv_disp_op->s_disp_frm_buf.u4_u_wd = ps_op_frm->u4_y_wd >> 1;
1111
0
            pv_disp_op->s_disp_frm_buf.u4_v_wd = ps_op_frm->u4_y_wd >> 1;
1112
1113
0
            if(1 == ps_dec->u4_share_disp_buf)
1114
0
            {
1115
0
                pv_disp_op->s_disp_frm_buf.pv_y_buf = ps_op_frm->pv_y_buf;
1116
1117
0
                for(i = 0; i < MAX_DISP_BUFS_NEW; i++)
1118
0
                {
1119
0
                    UWORD8 *buf = ps_dec->disp_bufs[i].buf[0];
1120
0
                    buf += ps_dec->disp_bufs[i].u4_ofst[0];
1121
0
                    if(((UWORD8 *)pv_disp_op->s_disp_frm_buf.pv_y_buf
1122
0
                                    - pic_buf->u2_crop_offset_y) == buf)
1123
0
                    {
1124
0
                        buf = ps_dec->disp_bufs[i].buf[1];
1125
0
                        buf += ps_dec->disp_bufs[i].u4_ofst[1];
1126
0
                        pv_disp_op->s_disp_frm_buf.pv_u_buf = buf
1127
0
                                        + (pic_buf->u2_crop_offset_uv
1128
0
                                           / YUV420SP_FACTOR);
1129
1130
0
                        buf = ps_dec->disp_bufs[i].buf[2];
1131
0
                        buf += ps_dec->disp_bufs[i].u4_ofst[2];
1132
0
                        pv_disp_op->s_disp_frm_buf.pv_v_buf = buf
1133
0
                                        + (pic_buf->u2_crop_offset_uv
1134
0
                                           / YUV420SP_FACTOR);
1135
1136
0
                    }
1137
0
                }
1138
0
            }
1139
1140
0
        }
1141
0
        else if((pv_disp_op->e_output_format == IV_YUV_420SP_UV)
1142
0
                        || (pv_disp_op->e_output_format == IV_YUV_420SP_VU))
1143
0
        {
1144
0
            pv_disp_op->s_disp_frm_buf.u4_u_strd =
1145
0
                            pv_disp_op->s_disp_frm_buf.u4_y_strd;
1146
0
            pv_disp_op->s_disp_frm_buf.u4_v_strd = 0;
1147
1148
0
            if(1 == ps_dec->u4_share_disp_buf)
1149
0
            {
1150
0
                UWORD32 i;
1151
1152
0
                pv_disp_op->s_disp_frm_buf.pv_y_buf = ps_op_frm->pv_y_buf;
1153
1154
0
                for(i = 0; i < MAX_DISP_BUFS_NEW; i++)
1155
0
                {
1156
0
                    UWORD8 *buf = ps_dec->disp_bufs[i].buf[0];
1157
0
                    buf += ps_dec->disp_bufs[i].u4_ofst[0];
1158
0
                    if((UWORD8 *)pv_disp_op->s_disp_frm_buf.pv_y_buf
1159
0
                                    - pic_buf->u2_crop_offset_y == buf)
1160
0
                    {
1161
0
                        buf = ps_dec->disp_bufs[i].buf[1];
1162
0
                        buf += ps_dec->disp_bufs[i].u4_ofst[1];
1163
0
                        pv_disp_op->s_disp_frm_buf.pv_u_buf = buf
1164
0
                                        + pic_buf->u2_crop_offset_uv;
1165
0
                        ;
1166
1167
0
                        buf = ps_dec->disp_bufs[i].buf[2];
1168
0
                        buf += ps_dec->disp_bufs[i].u4_ofst[2];
1169
0
                        pv_disp_op->s_disp_frm_buf.pv_v_buf = buf
1170
0
                                        + pic_buf->u2_crop_offset_uv;
1171
0
                        ;
1172
0
                    }
1173
0
                }
1174
0
            }
1175
0
            pv_disp_op->s_disp_frm_buf.u4_u_wd =
1176
0
                            pv_disp_op->s_disp_frm_buf.u4_y_wd;
1177
0
            pv_disp_op->s_disp_frm_buf.u4_v_wd = 0;
1178
1179
0
        }
1180
0
        else if((pv_disp_op->e_output_format == IV_RGB_565)
1181
0
                        || (pv_disp_op->e_output_format == IV_YUV_422ILE))
1182
0
        {
1183
1184
0
            pv_disp_op->s_disp_frm_buf.u4_u_strd = 0;
1185
0
            pv_disp_op->s_disp_frm_buf.u4_v_strd = 0;
1186
0
            pv_disp_op->s_disp_frm_buf.u4_u_wd = 0;
1187
0
            pv_disp_op->s_disp_frm_buf.u4_v_wd = 0;
1188
0
            pv_disp_op->s_disp_frm_buf.u4_u_ht = 0;
1189
0
            pv_disp_op->s_disp_frm_buf.u4_v_ht = 0;
1190
1191
0
        }
1192
1193
1194
0
    }
1195
1196
0
    return u4_api_ret;
1197
0
}
1198
1199
1200
/*****************************************************************************/
1201
/*  Function Name : ih264d_release_display_field                                         */
1202
/*                                                                           */
1203
/*  Description   : This function releases the display field that was returned   */
1204
/*                  here.                                                    */
1205
/*  Inputs        : ps_dec - Decoder parameters                              */
1206
/*  Globals       : None                                                     */
1207
/*  Processing    : Refer bumping process in the standard                    */
1208
/*  Outputs       : Assigns display sequence number.                         */
1209
/*  Returns       : None                                                     */
1210
/*                                                                           */
1211
/*  Issues        : None                                                     */
1212
/*                                                                           */
1213
/*  Revision History:                                                        */
1214
/*                                                                           */
1215
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
1216
/*         27 04 2005   NS              Draft                                */
1217
/*                                                                           */
1218
/*****************************************************************************/
1219
void ih264d_release_display_field(dec_struct_t *ps_dec,
1220
                                  ivd_get_display_frame_op_t *pv_disp_op)
1221
0
{
1222
0
    if(1 == pv_disp_op->u4_error_code)
1223
0
    {
1224
0
        if(1 == ps_dec->u1_flushfrm)
1225
0
        {
1226
0
            UWORD32 i;
1227
1228
0
            if(1 == ps_dec->u4_share_disp_buf)
1229
0
            {
1230
0
                H264_MUTEX_LOCK(&ps_dec->process_disp_mutex);
1231
0
                for(i = 0; i < (MAX_DISP_BUFS_NEW); i++)
1232
0
                {
1233
0
                    if(1 == ps_dec->u4_disp_buf_mapping[i])
1234
0
                    {
1235
0
                        ih264_buf_mgr_release(
1236
0
                                        (buf_mgr_t *)ps_dec->pv_pic_buf_mgr, i,
1237
0
                                        BUF_MGR_IO);
1238
0
                        ps_dec->u4_disp_buf_mapping[i] = 0;
1239
0
                    }
1240
0
                } H264_MUTEX_UNLOCK(&ps_dec->process_disp_mutex);
1241
1242
0
                memset(ps_dec->u4_disp_buf_to_be_freed, 0,
1243
0
                       (MAX_DISP_BUFS_NEW) * sizeof(UWORD32));
1244
0
                for(i = 0; i < ps_dec->u1_pic_bufs; i++)
1245
0
                    ps_dec->u4_disp_buf_mapping[i] = 1;
1246
0
            }
1247
0
            ps_dec->u1_flushfrm = 0;
1248
1249
0
        }
1250
0
    }
1251
0
    else
1252
0
    {
1253
0
        H264_MUTEX_LOCK(&ps_dec->process_disp_mutex);
1254
1255
0
        if(0 == ps_dec->u4_share_disp_buf)
1256
0
        {
1257
0
            ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
1258
0
                                  pv_disp_op->u4_disp_buf_id,
1259
0
                                  BUF_MGR_IO);
1260
1261
0
        }
1262
0
        else
1263
0
        {
1264
0
            ps_dec->u4_disp_buf_mapping[pv_disp_op->u4_disp_buf_id] = 1;
1265
0
        } H264_MUTEX_UNLOCK(&ps_dec->process_disp_mutex);
1266
1267
0
    }
1268
0
}
1269
/*****************************************************************************/
1270
/*  Function Name : ih264d_assign_display_seq                                         */
1271
/*                                                                           */
1272
/*  Description   : This function implments bumping process. Every outgoing  */
1273
/*                  frame from DPB is assigned a display sequence number     */
1274
/*                  which increases monotonically. System looks for this     */
1275
/*                  number to display a frame.                              */
1276
/*                  here.                                                    */
1277
/*  Inputs        : ps_dec - Decoder parameters                              */
1278
/*  Globals       : None                                                     */
1279
/*  Processing    : Refer bumping process in the standard                    */
1280
/*  Outputs       : Assigns display sequence number.                         */
1281
/*  Returns       : None                                                     */
1282
/*                                                                           */
1283
/*  Issues        : None                                                     */
1284
/*                                                                           */
1285
/*  Revision History:                                                        */
1286
/*                                                                           */
1287
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
1288
/*         27 04 2005   NS              Draft                                */
1289
/*                                                                           */
1290
/*****************************************************************************/
1291
WORD32 ih264d_assign_display_seq(dec_struct_t *ps_dec)
1292
0
{
1293
0
    WORD32 i;
1294
0
    WORD32 i4_min_poc;
1295
0
    WORD32 i4_min_poc_buf_id;
1296
0
    WORD32 i4_min_index;
1297
0
    dpb_manager_t *ps_dpb_mgr = ps_dec->ps_dpb_mgr;
1298
0
    WORD32 (*i4_poc_buf_id_map)[3] = ps_dpb_mgr->ai4_poc_buf_id_map;
1299
1300
0
    i4_min_poc = 0x7fffffff;
1301
0
    i4_min_poc_buf_id = -1;
1302
0
    i4_min_index = -1;
1303
1304
0
    if(ps_dpb_mgr->i1_poc_buf_id_entries >= ps_dec->i4_display_delay)
1305
0
    {
1306
0
        for(i = 0; i < MAX_BUF_ID_MAP_ENTRIES; i++)
1307
0
        {
1308
0
            if((i4_poc_buf_id_map[i][0] != -1)
1309
0
                            && (DO_NOT_DISP
1310
0
                                            != ps_dpb_mgr->ai4_poc_buf_id_map[i][0]))
1311
0
            {
1312
                /* Checking for <= is necessary to handle cases where there is one
1313
                   valid buffer with poc set to 0x7FFFFFFF. */
1314
0
                if(i4_poc_buf_id_map[i][1] <= i4_min_poc)
1315
0
                {
1316
0
                    i4_min_poc = i4_poc_buf_id_map[i][1];
1317
0
                    i4_min_poc_buf_id = i4_poc_buf_id_map[i][0];
1318
0
                    i4_min_index = i;
1319
0
                }
1320
0
            }
1321
0
        }
1322
1323
0
        if((i4_min_index != -1) && (DO_NOT_DISP != i4_min_poc_buf_id))
1324
0
        {
1325
0
            ps_dec->i4_cur_display_seq++;
1326
0
            ih264_disp_mgr_add(
1327
0
                            (disp_mgr_t *)ps_dec->pv_disp_buf_mgr,
1328
0
                            i4_min_poc_buf_id, ps_dec->i4_cur_display_seq,
1329
0
                            ps_dec->apv_buf_id_pic_buf_map[i4_min_poc_buf_id]);
1330
0
            i4_poc_buf_id_map[i4_min_index][0] = -1;
1331
0
            i4_poc_buf_id_map[i4_min_index][1] = 0x7fffffff;
1332
0
            ps_dpb_mgr->i1_poc_buf_id_entries--;
1333
0
        }
1334
0
        else if(DO_NOT_DISP == i4_min_poc_buf_id)
1335
0
        {
1336
0
            WORD32 i4_error_code;
1337
0
            i4_error_code = ERROR_GAPS_IN_FRM_NUM;
1338
//          i4_error_code |= 1<<IVD_CORRUPTEDDATA;
1339
0
            return i4_error_code;
1340
0
        }
1341
0
    }
1342
0
    return OK;
1343
0
}
1344
1345
/*****************************************************************************/
1346
/*                                                                           */
1347
/*  Function Name : ih264d_release_display_bufs                                       */
1348
/*                                                                           */
1349
/*  Description   : This function implments bumping process when mmco = 5.   */
1350
/*                  Each outgoing frame from DPB is assigned a display       */
1351
/*                  sequence number which increases monotonically. System    */
1352
/*                  looks for this number to display a frame.                */
1353
/*  Inputs        : ps_dec - Decoder parameters                              */
1354
/*  Globals       : None                                                     */
1355
/*  Processing    : Refer bumping process in the standard for mmco = 5       */
1356
/*  Outputs       : Assigns display sequence number.                         */
1357
/*  Returns       : None                                                     */
1358
/*                                                                           */
1359
/*  Issues        : None                                                     */
1360
/*                                                                           */
1361
/*  Revision History:                                                        */
1362
/*                                                                           */
1363
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
1364
/*         27 04 2005   NS              Draft                                */
1365
/*                                                                           */
1366
/*****************************************************************************/
1367
void ih264d_release_display_bufs(dec_struct_t *ps_dec)
1368
0
{
1369
0
    WORD32 i, j;
1370
0
    WORD32 i4_min_poc;
1371
0
    WORD32 i4_min_poc_buf_id;
1372
0
    WORD32 i4_min_index;
1373
0
    WORD64 i8_temp;
1374
0
    dpb_manager_t *ps_dpb_mgr = ps_dec->ps_dpb_mgr;
1375
0
    WORD32 (*i4_poc_buf_id_map)[3] = ps_dpb_mgr->ai4_poc_buf_id_map;
1376
1377
0
    i4_min_poc = 0x7fffffff;
1378
0
    i4_min_poc_buf_id = 0;
1379
0
    i4_min_index = 0;
1380
1381
0
    ih264d_delete_nonref_nondisplay_pics(ps_dpb_mgr);
1382
1383
0
    for(j = 0; j < ps_dpb_mgr->i1_poc_buf_id_entries; j++)
1384
0
    {
1385
0
        i4_min_poc = 0x7fffffff;
1386
0
        for(i = 0; i < MAX_BUF_ID_MAP_ENTRIES; i++)
1387
0
        {
1388
0
            if(i4_poc_buf_id_map[i][0] != -1)
1389
0
            {
1390
                /* Checking for <= is necessary to handle cases where there is one
1391
                   valid buffer with poc set to 0x7FFFFFFF. */
1392
0
                if(i4_poc_buf_id_map[i][1] <= i4_min_poc)
1393
0
                {
1394
0
                    i4_min_poc = i4_poc_buf_id_map[i][1];
1395
0
                    i4_min_poc_buf_id = i4_poc_buf_id_map[i][0];
1396
0
                    i4_min_index = i;
1397
0
                }
1398
0
            }
1399
0
        }
1400
1401
0
        if(DO_NOT_DISP != i4_min_poc_buf_id)
1402
0
        {
1403
0
            ps_dec->i4_cur_display_seq++;
1404
0
            ih264_disp_mgr_add(
1405
0
                            (disp_mgr_t *)ps_dec->pv_disp_buf_mgr,
1406
0
                            i4_min_poc_buf_id, ps_dec->i4_cur_display_seq,
1407
0
                            ps_dec->apv_buf_id_pic_buf_map[i4_min_poc_buf_id]);
1408
0
            i4_poc_buf_id_map[i4_min_index][0] = -1;
1409
0
            i4_poc_buf_id_map[i4_min_index][1] = 0x7fffffff;
1410
0
            ps_dpb_mgr->ai4_poc_buf_id_map[i4_min_index][2] = 0;
1411
0
        }
1412
0
        else
1413
0
        {
1414
0
            i4_poc_buf_id_map[i4_min_index][0] = -1;
1415
0
            i4_poc_buf_id_map[i4_min_index][1] = 0x7fffffff;
1416
0
            ps_dpb_mgr->ai4_poc_buf_id_map[i4_min_index][2] = 0;
1417
0
        }
1418
0
    }
1419
0
    ps_dpb_mgr->i1_poc_buf_id_entries = 0;
1420
0
    i8_temp = (WORD64)ps_dec->i4_prev_max_display_seq + ps_dec->i4_max_poc
1421
0
              + ps_dec->u1_max_dec_frame_buffering + 1;
1422
    /*If i4_prev_max_display_seq overflows integer range, reset it */
1423
0
    ps_dec->i4_prev_max_display_seq = IS_OUT_OF_RANGE_S32(i8_temp)?
1424
0
                                      0 : i8_temp;
1425
0
    ps_dec->i4_max_poc = 0;
1426
0
}
1427
1428
/*****************************************************************************/
1429
/*                                                                           */
1430
/*  Function Name : ih264d_assign_pic_num                                           */
1431
/*                                                                           */
1432
/*  Description   : This function assigns pic num to each reference frame    */
1433
/*                  depending on the cur_frame_num as speified in section    */
1434
/*                  8.2.4.1                                                  */
1435
/*                                                                           */
1436
/*  Inputs        : ps_dec                                                   */
1437
/*                                                                           */
1438
/*  Globals       : NO globals used                                          */
1439
/*                                                                           */
1440
/*  Processing    : for all ST pictures                                      */
1441
/*                    if( FrameNum > cur_frame_num)                          */
1442
/*                    PicNum = FrameNum - MaxFrameNum                        */
1443
/*                    else                                                   */
1444
/*                    PicNum = FrameNum                                      */
1445
/*                                                                           */
1446
/*  Returns       : void                                                     */
1447
/*                                                                           */
1448
/*  Issues        : NO                                                       */
1449
/*                                                                           */
1450
/*  Revision History:                                                        */
1451
/*                                                                           */
1452
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
1453
/*         13 07 2002   Jay             Draft                                */
1454
/*                                                                           */
1455
/*****************************************************************************/
1456
1457
void ih264d_assign_pic_num(dec_struct_t *ps_dec)
1458
0
{
1459
0
    dpb_manager_t *ps_dpb_mgr;
1460
0
    struct dpb_info_t *ps_next_dpb;
1461
0
    WORD8 i;
1462
0
    WORD32 i4_cur_frame_num, i4_max_frame_num;
1463
0
    WORD32 i4_ref_frame_num;
1464
0
    UWORD8 u1_fld_pic_flag = ps_dec->ps_cur_slice->u1_field_pic_flag;
1465
1466
0
    i4_max_frame_num = ps_dec->ps_cur_sps->u2_u4_max_pic_num_minus1 + 1;
1467
0
    i4_cur_frame_num = ps_dec->ps_cur_pic->i4_frame_num;
1468
0
    ps_dpb_mgr = ps_dec->ps_dpb_mgr;
1469
1470
    /* Start from ST head */
1471
0
    ps_next_dpb = ps_dpb_mgr->ps_dpb_st_head;
1472
0
    for(i = 0; i < ps_dpb_mgr->u1_num_st_ref_bufs; i++)
1473
0
    {
1474
0
        WORD32 i4_pic_num;
1475
1476
0
        i4_ref_frame_num = ps_next_dpb->ps_pic_buf->i4_frame_num;
1477
0
        if(i4_ref_frame_num > i4_cur_frame_num)
1478
0
        {
1479
            /* RefPic Buf frame_num is before Current frame_num in decode order */
1480
0
            i4_pic_num = i4_ref_frame_num - i4_max_frame_num;
1481
0
        }
1482
0
        else
1483
0
        {
1484
            /* RefPic Buf frame_num is after Current frame_num in decode order */
1485
0
            i4_pic_num = i4_ref_frame_num;
1486
0
        }
1487
1488
0
        ps_next_dpb->ps_pic_buf->i4_pic_num = i4_pic_num;
1489
0
        ps_next_dpb->i4_frame_num = i4_pic_num;
1490
0
        ps_next_dpb->ps_pic_buf->u1_long_term_frm_idx = MAX_REF_BUFS + 1;
1491
0
        if(u1_fld_pic_flag)
1492
0
        {
1493
            /* Assign the pic num to top fields and bot fields */
1494
1495
0
            ps_next_dpb->s_top_field.i4_pic_num = i4_pic_num * 2
1496
0
                            + !(ps_dec->ps_cur_slice->u1_bottom_field_flag);
1497
0
            ps_next_dpb->s_bot_field.i4_pic_num = i4_pic_num * 2
1498
0
                            + ps_dec->ps_cur_slice->u1_bottom_field_flag;
1499
0
        }
1500
        /* Chase the next link */
1501
0
        ps_next_dpb = ps_next_dpb->ps_prev_short;
1502
0
    }
1503
1504
0
    if(ps_dec->ps_cur_sps->u1_gaps_in_frame_num_value_allowed_flag
1505
0
                    && ps_dpb_mgr->u1_num_gaps)
1506
0
    {
1507
0
        WORD32 i4_start_frm, i4_end_frm;
1508
        /* Assign pic numbers for gaps */
1509
0
        for(i = 0; i < MAX_BUF_ID_MAP_ENTRIES; i++)
1510
0
        {
1511
0
            i4_start_frm = ps_dpb_mgr->ai4_gaps_start_frm_num[i];
1512
0
            if(i4_start_frm != INVALID_FRAME_NUM)
1513
0
            {
1514
0
                if(i4_start_frm > i4_cur_frame_num)
1515
0
                {
1516
                    /* gap's frame_num is before Current frame_num in
1517
                     decode order */
1518
0
                    i4_start_frm -= i4_max_frame_num;
1519
0
                }
1520
0
                ps_dpb_mgr->ai4_gaps_start_frm_num[i] = i4_start_frm;
1521
0
                i4_end_frm = ps_dpb_mgr->ai4_gaps_end_frm_num[i];
1522
1523
0
                if(i4_end_frm > i4_cur_frame_num)
1524
0
                {
1525
                    /* gap's frame_num is before Current frame_num in
1526
                     decode order */
1527
0
                    i4_end_frm -= i4_max_frame_num;
1528
0
                }
1529
0
                ps_dpb_mgr->ai4_gaps_end_frm_num[i] = i4_end_frm;
1530
0
            }
1531
0
        }
1532
0
    }
1533
0
}
1534
1535
/*!
1536
 **************************************************************************
1537
 * \if Function name : ih264d_update_qp \endif
1538
 *
1539
 * \brief
1540
 *    Updates the values of QP and its related entities
1541
 *
1542
 * \return
1543
 *    0 on Success and Error code otherwise
1544
 *
1545
 **************************************************************************
1546
 */
1547
WORD32 ih264d_update_qp(dec_struct_t * ps_dec, const WORD8 i1_qp)
1548
561k
{
1549
561k
    WORD32 i_temp;
1550
561k
    i_temp = (ps_dec->u1_qp + i1_qp + 52) % 52;
1551
1552
561k
    if((i_temp < 0) || (i_temp > 51) || (i1_qp < -26) || (i1_qp > 25))
1553
0
        return ERROR_INV_RANGE_QP_T;
1554
1555
561k
    ps_dec->u1_qp = i_temp;
1556
561k
    ps_dec->u1_qp_y_rem6 = ps_dec->u1_qp % 6;
1557
561k
    ps_dec->u1_qp_y_div6 = ps_dec->u1_qp / 6;
1558
561k
    i_temp = CLIP3(0, 51, ps_dec->u1_qp + ps_dec->ps_cur_pps->i1_chroma_qp_index_offset);
1559
561k
    ps_dec->u1_qp_u_rem6 = MOD(gau1_ih264d_qp_scale_cr[12 + i_temp], 6);
1560
561k
    ps_dec->u1_qp_u_div6 = DIV(gau1_ih264d_qp_scale_cr[12 + i_temp], 6);
1561
1562
561k
    i_temp = CLIP3(0, 51, ps_dec->u1_qp + ps_dec->ps_cur_pps->i1_second_chroma_qp_index_offset);
1563
561k
    ps_dec->u1_qp_v_rem6 = MOD(gau1_ih264d_qp_scale_cr[12 + i_temp], 6);
1564
561k
    ps_dec->u1_qp_v_div6 = DIV(gau1_ih264d_qp_scale_cr[12 + i_temp], 6);
1565
1566
561k
    ps_dec->pu2_quant_scale_y =
1567
561k
                    gau2_ih264_iquant_scale_4x4[ps_dec->u1_qp_y_rem6];
1568
561k
    ps_dec->pu2_quant_scale_u =
1569
561k
                    gau2_ih264_iquant_scale_4x4[ps_dec->u1_qp_u_rem6];
1570
561k
    ps_dec->pu2_quant_scale_v =
1571
561k
                    gau2_ih264_iquant_scale_4x4[ps_dec->u1_qp_v_rem6];
1572
561k
    return OK;
1573
561k
}
1574
1575
/*****************************************************************************/
1576
/*                                                                           */
1577
/*  Function Name : ih264d_decode_gaps_in_frame_num                                 */
1578
/*                                                                           */
1579
/*  Description   : This function decodes gaps in frame number               */
1580
/*                                                                           */
1581
/*  Inputs        : ps_dec          Decoder parameters                       */
1582
/*                  u2_frame_num   current frame number                     */
1583
/*                                                                           */
1584
/*  Globals       : None                                                     */
1585
/*  Processing    : This functionality needs to be implemented               */
1586
/*  Outputs       : None                                                     */
1587
/*  Returns       : None                                                     */
1588
/*                                                                           */
1589
/*  Issues        : Not implemented                                          */
1590
/*                                                                           */
1591
/*  Revision History:                                                        */
1592
/*                                                                           */
1593
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
1594
/*         06 05 2002   NS              Draft                                */
1595
/*                                                                           */
1596
/*****************************************************************************/
1597
WORD32 ih264d_decode_gaps_in_frame_num(dec_struct_t *ps_dec,
1598
                                       UWORD16 u2_frame_num)
1599
0
{
1600
0
    UWORD32 u4_next_frm_num, u4_start_frm_num;
1601
0
    UWORD32 u4_max_frm_num;
1602
0
    pocstruct_t s_tmp_poc;
1603
0
    WORD32 i4_poc;
1604
0
    dec_slice_params_t *ps_cur_slice;
1605
1606
0
    dec_pic_params_t *ps_pic_params;
1607
0
    WORD8 i1_gap_idx;
1608
0
    WORD32 *i4_gaps_start_frm_num;
1609
0
    dpb_manager_t *ps_dpb_mgr;
1610
0
    WORD32 i4_frame_gaps;
1611
0
    WORD8 *pi1_gaps_per_seq;
1612
0
    WORD32 ret;
1613
1614
0
    ps_cur_slice = ps_dec->ps_cur_slice;
1615
0
    if(ps_cur_slice->u1_field_pic_flag)
1616
0
    {
1617
0
        if(ps_dec->u2_prev_ref_frame_num == u2_frame_num)
1618
0
            return 0;
1619
0
    }
1620
0
    ps_pic_params = ps_dec->ps_cur_pps;
1621
1622
0
    u4_next_frm_num = ps_dec->u2_prev_ref_frame_num + 1;
1623
0
    u4_max_frm_num = ps_pic_params->ps_sps->u2_u4_max_pic_num_minus1 + 1;
1624
1625
    // check
1626
0
    if(u4_next_frm_num >= u4_max_frm_num)
1627
0
    {
1628
0
        u4_next_frm_num -= u4_max_frm_num;
1629
0
    }
1630
1631
0
    if(u4_next_frm_num == u2_frame_num)
1632
0
    {
1633
0
        return (0);
1634
0
    }
1635
1636
    // check
1637
0
    if((ps_dec->u1_nal_unit_type == IDR_SLICE_NAL)
1638
0
                    && (u4_next_frm_num >= u2_frame_num))
1639
0
    {
1640
0
        return (0);
1641
0
    }
1642
0
    u4_start_frm_num = u4_next_frm_num;
1643
1644
0
    s_tmp_poc.i4_pic_order_cnt_lsb = 0;
1645
0
    s_tmp_poc.i4_delta_pic_order_cnt_bottom = 0;
1646
0
    s_tmp_poc.i4_pic_order_cnt_lsb = 0;
1647
0
    s_tmp_poc.i4_delta_pic_order_cnt_bottom = 0;
1648
0
    s_tmp_poc.i4_delta_pic_order_cnt[0] = 0;
1649
0
    s_tmp_poc.i4_delta_pic_order_cnt[1] = 0;
1650
1651
0
    ps_cur_slice = ps_dec->ps_cur_slice;
1652
1653
0
    i4_frame_gaps = 0;
1654
0
    ps_dpb_mgr = ps_dec->ps_dpb_mgr;
1655
1656
    /* Find a empty slot to store gap seqn info */
1657
0
    i4_gaps_start_frm_num = ps_dpb_mgr->ai4_gaps_start_frm_num;
1658
0
    for(i1_gap_idx = 0; i1_gap_idx < MAX_BUF_ID_MAP_ENTRIES; i1_gap_idx++)
1659
0
    {
1660
0
        if(INVALID_FRAME_NUM == i4_gaps_start_frm_num[i1_gap_idx])
1661
0
            break;
1662
0
    }
1663
0
    if(MAX_BUF_ID_MAP_ENTRIES == i1_gap_idx)
1664
0
    {
1665
0
        UWORD32 i4_error_code;
1666
0
        i4_error_code = ERROR_DBP_MANAGER_T;
1667
//          i4_error_code |= 1<<IVD_CORRUPTEDDATA;
1668
0
        return i4_error_code;
1669
0
    }
1670
1671
0
    i4_poc = 0;
1672
0
    i4_gaps_start_frm_num[i1_gap_idx] = u4_start_frm_num;
1673
0
    ps_dpb_mgr->ai4_gaps_end_frm_num[i1_gap_idx] = u2_frame_num - 1;
1674
0
    pi1_gaps_per_seq = ps_dpb_mgr->ai1_gaps_per_seq;
1675
0
    pi1_gaps_per_seq[i1_gap_idx] = 0;
1676
0
    while(u4_next_frm_num != u2_frame_num)
1677
0
    {
1678
0
        ih264d_delete_nonref_nondisplay_pics(ps_dpb_mgr);
1679
0
        if(ps_pic_params->ps_sps->u1_pic_order_cnt_type)
1680
0
        {
1681
            /* allocate a picture buffer and insert it as ST node */
1682
0
            ret = ih264d_decode_pic_order_cnt(0, u4_next_frm_num,
1683
0
                                              &ps_dec->s_prev_pic_poc,
1684
0
                                              &s_tmp_poc, ps_cur_slice,
1685
0
                                              ps_pic_params, 1, 0, 0,
1686
0
                                              &i4_poc);
1687
0
            if(ret != OK)
1688
0
                return ret;
1689
1690
            /* Display seq no calculations */
1691
0
            if(i4_poc >= ps_dec->i4_max_poc)
1692
0
                ps_dec->i4_max_poc = i4_poc;
1693
            /* IDR Picture or POC wrap around */
1694
0
            if(i4_poc == 0)
1695
0
            {
1696
0
                WORD64 i8_temp;
1697
0
                i8_temp = (WORD64)ps_dec->i4_prev_max_display_seq
1698
0
                          + ps_dec->i4_max_poc
1699
0
                          + ps_dec->u1_max_dec_frame_buffering + 1;
1700
                /*If i4_prev_max_display_seq overflows integer range, reset it */
1701
0
                ps_dec->i4_prev_max_display_seq = IS_OUT_OF_RANGE_S32(i8_temp)?
1702
0
                                                  0 : i8_temp;
1703
0
                ps_dec->i4_max_poc = 0;
1704
0
            }
1705
1706
0
            ps_cur_slice->u1_mmco_equalto5 = 0;
1707
0
            ps_cur_slice->u2_frame_num = u4_next_frm_num;
1708
0
        }
1709
1710
        // check
1711
0
        if(ps_dpb_mgr->i1_poc_buf_id_entries
1712
0
                        >= ps_dec->u1_max_dec_frame_buffering)
1713
0
        {
1714
0
            ret = ih264d_assign_display_seq(ps_dec);
1715
0
            if(ret != OK)
1716
0
                return ret;
1717
0
        }
1718
1719
0
        {
1720
0
            WORD64 i8_display_poc;
1721
0
            i8_display_poc = (WORD64)ps_dec->i4_prev_max_display_seq +
1722
0
                        i4_poc;
1723
0
            if(IS_OUT_OF_RANGE_S32(i8_display_poc))
1724
0
            {
1725
0
                ps_dec->i4_prev_max_display_seq = 0;
1726
0
            }
1727
0
        }
1728
0
        ret = ih264d_insert_pic_in_display_list(
1729
0
                        ps_dec->ps_dpb_mgr, (WORD8) DO_NOT_DISP,
1730
0
                        (WORD32)(ps_dec->i4_prev_max_display_seq + i4_poc),
1731
0
                        u4_next_frm_num);
1732
0
        if(ret != OK)
1733
0
            return ret;
1734
1735
0
        pi1_gaps_per_seq[i1_gap_idx]++;
1736
0
        ret = ih264d_do_mmco_for_gaps(ps_dpb_mgr,
1737
0
                                ps_dec->ps_cur_sps->u1_num_ref_frames);
1738
0
        if(ret != OK)
1739
0
            return ret;
1740
1741
0
        ih264d_delete_nonref_nondisplay_pics(ps_dpb_mgr);
1742
1743
0
        u4_next_frm_num++;
1744
0
        if(u4_next_frm_num >= u4_max_frm_num)
1745
0
        {
1746
0
            u4_next_frm_num -= u4_max_frm_num;
1747
0
        }
1748
1749
0
        i4_frame_gaps++;
1750
0
    }
1751
1752
0
    return OK;
1753
0
}
1754
1755
/*!
1756
 **************************************************************************
1757
 * \if Function name : ih264d_create_pic_buffers \endif
1758
 *
1759
 * \brief
1760
 *    This function creates Picture Buffers.
1761
 *
1762
 * \return
1763
 *    0 on Success and -1 on error
1764
 **************************************************************************
1765
 */
1766
WORD32 ih264d_create_pic_buffers(UWORD8 u1_num_of_buf,
1767
                               dec_struct_t *ps_dec)
1768
0
{
1769
0
    struct pic_buffer_t *ps_pic_buf;
1770
0
    UWORD8 i;
1771
0
    UWORD32 u4_luma_size, u4_chroma_size;
1772
0
    UWORD8 u1_frm = ps_dec->ps_cur_sps->u1_frame_mbs_only_flag;
1773
0
    WORD32 j;
1774
0
    UWORD8 *pu1_buf;
1775
1776
0
    ps_pic_buf = ps_dec->ps_pic_buf_base;
1777
0
    ih264_disp_mgr_init((disp_mgr_t *)ps_dec->pv_disp_buf_mgr);
1778
0
    ih264_buf_mgr_init((buf_mgr_t *)ps_dec->pv_pic_buf_mgr);
1779
0
    u4_luma_size = ps_dec->u2_frm_wd_y * ps_dec->u2_frm_ht_y;
1780
0
    u4_chroma_size = ps_dec->u2_frm_wd_uv * ps_dec->u2_frm_ht_uv;
1781
1782
0
    {
1783
0
        if(ps_dec->u4_share_disp_buf == 1)
1784
0
        {
1785
            /* In case of buffers getting shared between application and library
1786
             there is no need of reference memtabs. Instead of setting the i4_size
1787
             to zero, it is reduced to a small i4_size to ensure that changes
1788
             in the code are minimal */
1789
0
            if((ps_dec->u1_chroma_format == IV_YUV_420SP_UV)
1790
0
                            || (ps_dec->u1_chroma_format == IV_YUV_420SP_VU)
1791
0
                            || (ps_dec->u1_chroma_format == IV_YUV_420P))
1792
0
            {
1793
0
                u4_luma_size = 64;
1794
0
            }
1795
1796
0
            if(ps_dec->u1_chroma_format == IV_YUV_420SP_UV)
1797
0
            {
1798
0
                u4_chroma_size = 64;
1799
0
            }
1800
1801
0
        }
1802
0
    }
1803
1804
0
    pu1_buf = ps_dec->pu1_pic_buf_base;
1805
1806
    /* Allocate memory for refernce buffers */
1807
0
    for(i = 0; i < u1_num_of_buf; i++)
1808
0
    {
1809
0
        UWORD32 u4_offset;
1810
0
        WORD32 buf_ret;
1811
0
        UWORD8 *pu1_luma, *pu1_chroma;
1812
0
        void *pv_mem_ctxt = ps_dec->pv_mem_ctxt;
1813
1814
0
        pu1_luma = pu1_buf;
1815
0
        pu1_buf += ALIGN64(u4_luma_size);
1816
0
        pu1_chroma = pu1_buf;
1817
0
        pu1_buf += ALIGN64(u4_chroma_size);
1818
1819
        /* Offset to the start of the pic from the top left corner of the frame
1820
         buffer */
1821
1822
0
        if((0 == ps_dec->u4_share_disp_buf)
1823
0
                        || (NULL == ps_dec->disp_bufs[i].buf[0]))
1824
0
        {
1825
0
            UWORD32 pad_len_h, pad_len_v;
1826
1827
0
            u4_offset = ps_dec->u2_frm_wd_y * (PAD_LEN_Y_V << 1) + PAD_LEN_Y_H;
1828
0
            ps_pic_buf->pu1_buf1 = (UWORD8 *)(pu1_luma) + u4_offset;
1829
1830
0
            pad_len_h = MAX(PAD_LEN_UV_H, (PAD_LEN_Y_H >> 1));
1831
0
            pad_len_v = MAX(PAD_LEN_UV_V, PAD_LEN_Y_V);
1832
1833
0
            u4_offset = ps_dec->u2_frm_wd_uv * pad_len_v + pad_len_h;
1834
1835
0
            ps_pic_buf->pu1_buf2 = (UWORD8 *)(pu1_chroma) + u4_offset;
1836
0
            ps_pic_buf->pu1_buf3 = (UWORD8 *)(NULL) + u4_offset;
1837
1838
0
        }
1839
0
        else
1840
0
        {
1841
0
            UWORD32 pad_len_h, pad_len_v;
1842
0
            u4_offset = ps_dec->u2_frm_wd_y * (PAD_LEN_Y_V << 1) + PAD_LEN_Y_H;
1843
0
            ps_pic_buf->pu1_buf1 = (UWORD8 *)ps_dec->disp_bufs[i].buf[0]
1844
0
                            + u4_offset;
1845
1846
0
            ps_dec->disp_bufs[i].u4_ofst[0] = u4_offset;
1847
1848
0
            if(ps_dec->u1_chroma_format == IV_YUV_420P)
1849
0
            {
1850
0
                pad_len_h = MAX(PAD_LEN_UV_H * YUV420SP_FACTOR,
1851
0
                                (PAD_LEN_Y_H >> 1));
1852
0
                pad_len_v = MAX(PAD_LEN_UV_V, PAD_LEN_Y_V);
1853
1854
0
                u4_offset = ps_dec->u2_frm_wd_uv * pad_len_v + pad_len_h;
1855
0
                ps_pic_buf->pu1_buf2 = (UWORD8 *)(pu1_chroma) + u4_offset;
1856
0
                ps_pic_buf->pu1_buf3 = (UWORD8 *)(NULL) + u4_offset;
1857
1858
0
                ps_dec->disp_bufs[i].u4_ofst[1] = u4_offset;
1859
0
                ps_dec->disp_bufs[i].u4_ofst[2] = u4_offset;
1860
1861
0
            }
1862
0
            else
1863
0
            {
1864
0
                pad_len_h = MAX(PAD_LEN_UV_H * YUV420SP_FACTOR,
1865
0
                                (PAD_LEN_Y_H >> 1));
1866
0
                pad_len_v = MAX(PAD_LEN_UV_V, PAD_LEN_Y_V);
1867
1868
0
                u4_offset = ps_dec->u2_frm_wd_uv * pad_len_v + pad_len_h;
1869
0
                ps_pic_buf->pu1_buf2 = (UWORD8 *)(ps_dec->disp_bufs[i].buf[1])
1870
0
                                + u4_offset;
1871
0
                ps_pic_buf->pu1_buf3 = (UWORD8 *)(ps_dec->disp_bufs[i].buf[1])
1872
0
                                + u4_offset;
1873
1874
0
                ps_dec->disp_bufs[i].u4_ofst[1] = u4_offset;
1875
0
                ps_dec->disp_bufs[i].u4_ofst[2] = u4_offset;
1876
0
            }
1877
0
        }
1878
1879
0
        ps_pic_buf->u2_frm_ht_y = ps_dec->u2_frm_ht_y;
1880
0
        ps_pic_buf->u2_frm_ht_uv = ps_dec->u2_frm_ht_uv;
1881
0
        ps_pic_buf->u2_frm_wd_y = ps_dec->u2_frm_wd_y;
1882
0
        ps_pic_buf->u2_frm_wd_uv = ps_dec->u2_frm_wd_uv;
1883
1884
0
        ps_pic_buf->u1_pic_buf_id = i;
1885
1886
0
        buf_ret = ih264_buf_mgr_add((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
1887
0
                                    ps_pic_buf, i);
1888
0
        if(0 != buf_ret)
1889
0
        {
1890
0
            ps_dec->i4_error_code = ERROR_BUF_MGR;
1891
0
            return ERROR_BUF_MGR;
1892
0
        }
1893
1894
0
        ps_dec->apv_buf_id_pic_buf_map[i] = (void *)ps_pic_buf;
1895
0
        ps_pic_buf++;
1896
0
    }
1897
1898
0
    if(1 == ps_dec->u4_share_disp_buf)
1899
0
    {
1900
0
        for(i = 0; i < u1_num_of_buf; i++)
1901
0
            ps_dec->u4_disp_buf_mapping[i] = 1;
1902
0
    }
1903
0
    return OK;
1904
0
}
1905
1906
/*!
1907
 **************************************************************************
1908
 * \if Function name : ih264d_allocate_dynamic_bufs \endif
1909
 *
1910
 * \brief
1911
 *    This function allocates memory required by Decoder.
1912
 *
1913
 * \param ps_dec: Pointer to dec_struct_t.
1914
 *
1915
 * \return
1916
 *    Returns i4_status as returned by MemManager.
1917
 *
1918
 **************************************************************************
1919
 */
1920
WORD16 ih264d_allocate_dynamic_bufs(dec_struct_t * ps_dec)
1921
0
{
1922
0
    struct MemReq s_MemReq;
1923
0
    struct MemBlock *p_MemBlock;
1924
1925
0
    pred_info_t *ps_pred_frame;
1926
0
    dec_mb_info_t *ps_frm_mb_info;
1927
0
    dec_slice_struct_t *ps_dec_slice_buf;
1928
0
    UWORD8 *pu1_dec_mb_map, *pu1_recon_mb_map;
1929
0
    UWORD16 *pu2_slice_num_map;
1930
1931
0
    WORD16 *pi16_res_coeff;
1932
0
    WORD16 i16_status = 0;
1933
0
    UWORD8 uc_frmOrFld = (1 - ps_dec->ps_cur_sps->u1_frame_mbs_only_flag);
1934
0
    UWORD16 u4_luma_wd = ps_dec->u2_frm_wd_y;
1935
0
    UWORD16 u4_chroma_wd = ps_dec->u2_frm_wd_uv;
1936
0
    WORD8 c_i = 0;
1937
0
    dec_seq_params_t *ps_sps = ps_dec->ps_cur_sps;
1938
0
    UWORD32 u4_total_mbs = ps_sps->u4_total_num_of_mbs << uc_frmOrFld;
1939
0
    UWORD32 u4_wd_mbs = ps_dec->u2_frm_wd_in_mbs;
1940
0
    UWORD32 u4_ht_mbs = ps_dec->u2_frm_ht_in_mbs;
1941
0
    UWORD32 u4_blk_wd;
1942
0
    UWORD32 ui_size = 0;
1943
0
    UWORD32 u4_int_scratch_size = 0, u4_ref_pred_size = 0;
1944
0
    UWORD8 *pu1_buf;
1945
0
    WORD32 num_entries;
1946
0
    WORD32 size;
1947
0
    void *pv_buf;
1948
0
    UWORD32 u4_num_bufs;
1949
0
    UWORD32 u4_luma_size, u4_chroma_size;
1950
0
    void *pv_mem_ctxt = ps_dec->pv_mem_ctxt;
1951
1952
    /* Free any dynamic buffers that are allocated earlier. As ps_dec is memset to zero  */
1953
    /* at the beginning, it is safe to call this even if no memory was allocated earlier */
1954
0
    ih264d_free_dynamic_bufs(ps_dec);
1955
1956
0
    size = u4_total_mbs;
1957
0
    pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
1958
0
    RETURN_IF((NULL == pv_buf), IV_FAIL);
1959
0
    memset(pv_buf, 0, size);
1960
0
    ps_dec->pu1_dec_mb_map = pv_buf;
1961
1962
0
    size = u4_total_mbs;
1963
0
    pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
1964
0
    RETURN_IF((NULL == pv_buf), IV_FAIL);
1965
0
    memset(pv_buf, 0, size);
1966
0
    ps_dec->pu1_recon_mb_map = pv_buf;
1967
1968
0
    size = u4_total_mbs * sizeof(UWORD16);
1969
0
    pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
1970
0
    RETURN_IF((NULL == pv_buf), IV_FAIL);
1971
0
    memset(pv_buf, 0, size);
1972
0
    ps_dec->pu2_slice_num_map = pv_buf;
1973
1974
    /************************************************************/
1975
    /* Post allocation Initialisations                          */
1976
    /************************************************************/
1977
0
    ps_dec->ps_parse_cur_slice = &(ps_dec->ps_dec_slice_buf[0]);
1978
0
    ps_dec->ps_decode_cur_slice = &(ps_dec->ps_dec_slice_buf[0]);
1979
0
    ps_dec->ps_computebs_cur_slice = &(ps_dec->ps_dec_slice_buf[0]);
1980
1981
0
    ps_dec->ps_pred_start = ps_dec->ps_pred;
1982
1983
0
    size = sizeof(parse_pmbarams_t) * (ps_dec->u4_recon_mb_grp);
1984
0
    pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
1985
0
    RETURN_IF((NULL == pv_buf), IV_FAIL);
1986
0
    memset(pv_buf, 0, size);
1987
0
    ps_dec->ps_parse_mb_data = pv_buf;
1988
1989
0
    size = sizeof(parse_part_params_t)
1990
0
                        * ((ps_dec->u4_recon_mb_grp) << 4);
1991
0
    pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
1992
0
    RETURN_IF((NULL == pv_buf), IV_FAIL);
1993
0
    memset(pv_buf, 0, size);
1994
0
    ps_dec->ps_parse_part_params = pv_buf;
1995
1996
0
    size = ((u4_wd_mbs * sizeof(deblkmb_neighbour_t)) << uc_frmOrFld);
1997
0
    pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
1998
0
    RETURN_IF((NULL == pv_buf), IV_FAIL);
1999
0
    memset(pv_buf, 0, size);
2000
0
    ps_dec->ps_deblk_top_mb = pv_buf;
2001
2002
0
    size = ((sizeof(ctxt_inc_mb_info_t))
2003
0
                        * (((u4_wd_mbs + 1) << uc_frmOrFld) + 1));
2004
0
    pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
2005
0
    RETURN_IF((NULL == pv_buf), IV_FAIL);
2006
0
    memset(pv_buf, 0, size);
2007
0
    ps_dec->p_ctxt_inc_mb_map = pv_buf;
2008
2009
    /* 0th entry of CtxtIncMbMap will be always be containing default values
2010
     for CABAC context representing MB not available */
2011
0
    ps_dec->p_ctxt_inc_mb_map += 1;
2012
2013
0
    size = (sizeof(mv_pred_t) * ps_dec->u4_recon_mb_grp
2014
0
                        * 16);
2015
0
    pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
2016
0
    RETURN_IF((NULL == pv_buf), IV_FAIL);
2017
0
    memset(pv_buf, 0, size);
2018
0
    ps_dec->ps_mv_p[0] = pv_buf;
2019
2020
0
    size = (sizeof(mv_pred_t) * ps_dec->u4_recon_mb_grp
2021
0
                        * 16);
2022
0
    pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
2023
0
    RETURN_IF((NULL == pv_buf), IV_FAIL);
2024
0
    memset(pv_buf, 0, size);
2025
0
    ps_dec->ps_mv_p[1] = pv_buf;
2026
2027
0
    {
2028
0
        UWORD8 i;
2029
0
        for(i = 0; i < MV_SCRATCH_BUFS; i++)
2030
0
        {
2031
0
            size = (sizeof(mv_pred_t)
2032
0
                            * ps_dec->u4_recon_mb_grp * 4);
2033
0
            pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
2034
0
            RETURN_IF((NULL == pv_buf), IV_FAIL);
2035
0
            memset(pv_buf, 0, size);
2036
0
            ps_dec->ps_mv_top_p[i] = pv_buf;
2037
0
        }
2038
0
    }
2039
2040
0
    size = sizeof(UWORD8) * ((u4_wd_mbs + 2) * MB_SIZE) * 2;
2041
0
    pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
2042
0
    RETURN_IF((NULL == pv_buf), IV_FAIL);
2043
0
    ps_dec->pu1_y_intra_pred_line = pv_buf;
2044
0
    memset(ps_dec->pu1_y_intra_pred_line, 0, size);
2045
0
    ps_dec->pu1_y_intra_pred_line += MB_SIZE;
2046
2047
0
    size = sizeof(UWORD8) * ((u4_wd_mbs + 2) * MB_SIZE) * 2;
2048
0
    pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
2049
0
    RETURN_IF((NULL == pv_buf), IV_FAIL);
2050
0
    ps_dec->pu1_u_intra_pred_line = pv_buf;
2051
0
    memset(ps_dec->pu1_u_intra_pred_line, 0, size);
2052
0
    ps_dec->pu1_u_intra_pred_line += MB_SIZE;
2053
2054
0
    size = sizeof(UWORD8) * ((u4_wd_mbs + 2) * MB_SIZE) * 2;
2055
0
    pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
2056
0
    RETURN_IF((NULL == pv_buf), IV_FAIL);
2057
0
    ps_dec->pu1_v_intra_pred_line = pv_buf;
2058
0
    memset(ps_dec->pu1_v_intra_pred_line, 0, size);
2059
0
    ps_dec->pu1_v_intra_pred_line += MB_SIZE;
2060
2061
0
    if(ps_dec->u1_separate_parse)
2062
0
    {
2063
        /* Needs one extra row of info, to hold top row data */
2064
0
        size = sizeof(mb_neigbour_params_t)
2065
0
                        * 2 * ((u4_wd_mbs + 2) * (u4_ht_mbs + 1));
2066
0
    }
2067
0
    else
2068
0
    {
2069
0
        size = sizeof(mb_neigbour_params_t)
2070
0
                        * 2 * ((u4_wd_mbs + 2) << uc_frmOrFld);
2071
0
    }
2072
0
    pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
2073
0
    RETURN_IF((NULL == pv_buf), IV_FAIL);
2074
2075
0
    ps_dec->ps_nbr_mb_row = pv_buf;
2076
0
    memset(ps_dec->ps_nbr_mb_row, 0, size);
2077
2078
    /* Allocate deblock MB info */
2079
0
    size = (u4_total_mbs + u4_wd_mbs) * sizeof(deblk_mb_t);
2080
2081
0
    pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
2082
0
    RETURN_IF((NULL == pv_buf), IV_FAIL);
2083
0
    ps_dec->ps_deblk_pic = pv_buf;
2084
2085
0
    memset(ps_dec->ps_deblk_pic, 0, size);
2086
2087
    /* Allocate frame level mb info */
2088
0
    size = sizeof(dec_mb_info_t) * u4_total_mbs;
2089
0
    pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
2090
0
    RETURN_IF((NULL == pv_buf), IV_FAIL);
2091
0
    ps_dec->ps_frm_mb_info = pv_buf;
2092
0
    memset(ps_dec->ps_frm_mb_info, 0, size);
2093
2094
    /* Allocate memory for slice headers dec_slice_struct_t */
2095
0
    num_entries = MAX_FRAMES;
2096
0
    if((1 >= ps_dec->ps_cur_sps->u1_num_ref_frames) &&
2097
0
        (0 == ps_dec->i4_display_delay))
2098
0
    {
2099
0
        num_entries = 1;
2100
0
    }
2101
0
    num_entries = ((2 * num_entries) + 1);
2102
0
    num_entries *= 2;
2103
2104
0
    size = num_entries * sizeof(void *);
2105
0
    size += PAD_MAP_IDX_POC * sizeof(void *);
2106
0
    size *= u4_total_mbs;
2107
0
    size += sizeof(dec_slice_struct_t) * u4_total_mbs;
2108
0
    pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
2109
0
    RETURN_IF((NULL == pv_buf), IV_FAIL);
2110
2111
0
    ps_dec->ps_dec_slice_buf = pv_buf;
2112
0
    memset(ps_dec->ps_dec_slice_buf, 0, size);
2113
0
    pu1_buf = (UWORD8 *)ps_dec->ps_dec_slice_buf;
2114
0
    pu1_buf += sizeof(dec_slice_struct_t) * u4_total_mbs;
2115
0
    ps_dec->pv_map_ref_idx_to_poc_buf = (void *)pu1_buf;
2116
2117
    /* Allocate memory for packed pred info */
2118
0
    num_entries = u4_total_mbs;
2119
0
    num_entries *= 16 * 2;
2120
2121
0
    size = sizeof(pred_info_pkd_t) * num_entries;
2122
0
    pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
2123
0
    RETURN_IF((NULL == pv_buf), IV_FAIL);
2124
0
    memset(pv_buf, 0, size);
2125
0
    ps_dec->ps_pred_pkd = pv_buf;
2126
2127
    /* Allocate memory for coeff data */
2128
0
    size = MB_LUM_SIZE * sizeof(WORD16);
2129
    /*For I16x16 MBs, 16 4x4 AC coeffs and 1 4x4 DC coeff TU blocks will be sent
2130
    For all MBs along with 8 4x4 AC coeffs 2 2x2 DC coeff TU blocks will be sent
2131
    So use 17 4x4 TU blocks for luma and 9 4x4 TU blocks for chroma */
2132
0
    size += u4_total_mbs * (MAX(17 * sizeof(tu_sblk4x4_coeff_data_t),4 * sizeof(tu_blk8x8_coeff_data_t))
2133
0
                                            + 9 * sizeof(tu_sblk4x4_coeff_data_t));
2134
    //32 bytes for each mb to store u1_prev_intra4x4_pred_mode and u1_rem_intra4x4_pred_mode data
2135
0
    size += u4_total_mbs * 32;
2136
0
    pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
2137
0
    RETURN_IF((NULL == pv_buf), IV_FAIL);
2138
0
    memset(pv_buf, 0, size);
2139
2140
0
    ps_dec->pi2_coeff_data = pv_buf;
2141
2142
0
    ps_dec->pv_pic_tu_coeff_data = (void *)(ps_dec->pi2_coeff_data + MB_LUM_SIZE);
2143
2144
    /* Allocate MV bank buffer */
2145
0
    {
2146
0
        UWORD32 col_flag_buffer_size, mvpred_buffer_size;
2147
0
        UWORD8 u1_num_ref_frames = ps_dec->u4_disable_ref_retention ?
2148
0
                        !ps_dec->ps_cur_sps->u1_frame_mbs_only_flag :
2149
0
                        ps_dec->ps_cur_sps->u1_num_ref_frames;
2150
2151
0
        col_flag_buffer_size = ((ps_dec->u2_pic_wd * ps_dec->u2_pic_ht) >> 4);
2152
0
        mvpred_buffer_size = sizeof(mv_pred_t)
2153
0
                        * ((ps_dec->u2_pic_wd * (ps_dec->u2_pic_ht + PAD_MV_BANK_ROW)) >> 4);
2154
2155
0
        u4_num_bufs = u1_num_ref_frames + 1;
2156
2157
0
        u4_num_bufs = MIN(u4_num_bufs, ps_dec->u1_pic_bufs);
2158
0
        if(!ps_dec->u4_disable_ref_retention)
2159
0
        {
2160
0
            u4_num_bufs = MAX(u4_num_bufs, 2);
2161
0
        }
2162
0
        size = ALIGN64(mvpred_buffer_size) + ALIGN64(col_flag_buffer_size);
2163
0
        size *= u4_num_bufs;
2164
0
        pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
2165
0
        RETURN_IF((NULL == pv_buf), IV_FAIL);
2166
0
        memset(pv_buf, 0, size);
2167
0
        ps_dec->pu1_mv_bank_buf_base = pv_buf;
2168
0
    }
2169
2170
    /* Allocate Pic buffer */
2171
0
    u4_luma_size = ps_dec->u2_frm_wd_y * ps_dec->u2_frm_ht_y;
2172
0
    u4_chroma_size = ps_dec->u2_frm_wd_uv * ps_dec->u2_frm_ht_uv;
2173
2174
0
    {
2175
0
        if(ps_dec->u4_share_disp_buf == 1)
2176
0
        {
2177
            /* In case of buffers getting shared between application and library
2178
             there is no need of reference memtabs. Instead of setting the i4_size
2179
             to zero, it is reduced to a small i4_size to ensure that changes
2180
             in the code are minimal */
2181
0
            if((ps_dec->u1_chroma_format == IV_YUV_420SP_UV)
2182
0
                            || (ps_dec->u1_chroma_format == IV_YUV_420SP_VU)
2183
0
                            || (ps_dec->u1_chroma_format == IV_YUV_420P))
2184
0
            {
2185
0
                u4_luma_size = 64;
2186
0
            }
2187
2188
0
            if(ps_dec->u1_chroma_format == IV_YUV_420SP_UV)
2189
0
            {
2190
0
                u4_chroma_size = 64;
2191
0
            }
2192
2193
0
        }
2194
0
    }
2195
2196
0
    size = ALIGN64(u4_luma_size) + ALIGN64(u4_chroma_size);
2197
0
    size *= ps_dec->u1_pic_bufs;
2198
0
    pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
2199
0
    RETURN_IF((NULL == pv_buf), IV_FAIL);
2200
0
    memset(pv_buf, 0, size);
2201
0
    ps_dec->pu1_pic_buf_base = pv_buf;
2202
2203
    /* Allocate memory for mb_info maps */
2204
0
    if(ps_dec->u1_enable_mb_info)
2205
0
    {
2206
0
        size = (u4_total_mbs << 2) * MAX_DISP_BUFS_NEW;
2207
2208
0
        pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
2209
0
        RETURN_IF((NULL == pv_buf), IV_FAIL);
2210
0
        memset(pv_buf, 0, size);
2211
0
        ps_dec->pu1_qp_map_base = pv_buf;
2212
2213
0
        pv_buf = ps_dec->pf_aligned_alloc(pv_mem_ctxt, 128, size);
2214
0
        RETURN_IF((NULL == pv_buf), IV_FAIL);
2215
0
        memset(pv_buf, 0, size);
2216
0
        ps_dec->pu1_mb_type_map_base = pv_buf;
2217
0
    }
2218
2219
    /* Post allocation Increment Actions */
2220
2221
    /***************************************************************************/
2222
    /*Initialize cabac context pointers for every SE that has fixed contextIdx */
2223
    /***************************************************************************/
2224
0
    {
2225
0
        bin_ctxt_model_t * const p_cabac_ctxt_table_t =
2226
0
                        ps_dec->p_cabac_ctxt_table_t;
2227
0
        bin_ctxt_model_t * * p_coeff_abs_level_minus1_t =
2228
0
                        ps_dec->p_coeff_abs_level_minus1_t;
2229
0
        bin_ctxt_model_t * * p_cbf_t = ps_dec->p_cbf_t;
2230
2231
0
        ps_dec->p_mb_field_dec_flag_t = p_cabac_ctxt_table_t
2232
0
                        + MB_FIELD_DECODING_FLAG;
2233
0
        ps_dec->p_prev_intra4x4_pred_mode_flag_t = p_cabac_ctxt_table_t
2234
0
                        + PREV_INTRA4X4_PRED_MODE_FLAG;
2235
0
        ps_dec->p_rem_intra4x4_pred_mode_t = p_cabac_ctxt_table_t
2236
0
                        + REM_INTRA4X4_PRED_MODE;
2237
0
        ps_dec->p_intra_chroma_pred_mode_t = p_cabac_ctxt_table_t
2238
0
                        + INTRA_CHROMA_PRED_MODE;
2239
0
        ps_dec->p_mb_qp_delta_t = p_cabac_ctxt_table_t + MB_QP_DELTA;
2240
0
        ps_dec->p_ref_idx_t = p_cabac_ctxt_table_t + REF_IDX;
2241
0
        ps_dec->p_mvd_x_t = p_cabac_ctxt_table_t + MVD_X;
2242
0
        ps_dec->p_mvd_y_t = p_cabac_ctxt_table_t + MVD_Y;
2243
0
        p_cbf_t[0] = p_cabac_ctxt_table_t + CBF + 0;
2244
0
        p_cbf_t[1] = p_cabac_ctxt_table_t + CBF + 4;
2245
0
        p_cbf_t[2] = p_cabac_ctxt_table_t + CBF + 8;
2246
0
        p_cbf_t[3] = p_cabac_ctxt_table_t + CBF + 12;
2247
0
        p_cbf_t[4] = p_cabac_ctxt_table_t + CBF + 16;
2248
0
        ps_dec->p_cbp_luma_t = p_cabac_ctxt_table_t + CBP_LUMA;
2249
0
        ps_dec->p_cbp_chroma_t = p_cabac_ctxt_table_t + CBP_CHROMA;
2250
2251
0
        p_coeff_abs_level_minus1_t[LUMA_DC_CTXCAT] = p_cabac_ctxt_table_t
2252
0
                        + COEFF_ABS_LEVEL_MINUS1 + COEFF_ABS_LEVEL_CAT_0_OFFSET;
2253
2254
0
        p_coeff_abs_level_minus1_t[LUMA_AC_CTXCAT] = p_cabac_ctxt_table_t
2255
0
                        + COEFF_ABS_LEVEL_MINUS1 + COEFF_ABS_LEVEL_CAT_1_OFFSET;
2256
2257
0
        p_coeff_abs_level_minus1_t[LUMA_4X4_CTXCAT] = p_cabac_ctxt_table_t
2258
0
                        + COEFF_ABS_LEVEL_MINUS1 + COEFF_ABS_LEVEL_CAT_2_OFFSET;
2259
2260
0
        p_coeff_abs_level_minus1_t[CHROMA_DC_CTXCAT] = p_cabac_ctxt_table_t
2261
0
                        + COEFF_ABS_LEVEL_MINUS1 + COEFF_ABS_LEVEL_CAT_3_OFFSET;
2262
2263
0
        p_coeff_abs_level_minus1_t[CHROMA_AC_CTXCAT] = p_cabac_ctxt_table_t
2264
0
                        + COEFF_ABS_LEVEL_MINUS1 + COEFF_ABS_LEVEL_CAT_4_OFFSET;
2265
2266
0
        p_coeff_abs_level_minus1_t[LUMA_8X8_CTXCAT] = p_cabac_ctxt_table_t
2267
0
                        + COEFF_ABS_LEVEL_MINUS1_8X8
2268
0
                        + COEFF_ABS_LEVEL_CAT_5_OFFSET;
2269
2270
        /********************************************************/
2271
        /* context for the high profile related syntax elements */
2272
        /* This is maintained seperately in s_high_profile     */
2273
        /********************************************************/
2274
0
        {
2275
2276
0
            ps_dec->s_high_profile.ps_transform8x8_flag = p_cabac_ctxt_table_t
2277
0
                            + TRANSFORM_SIZE_8X8_FLAG;
2278
2279
0
            ps_dec->s_high_profile.ps_sigcoeff_8x8_frame = p_cabac_ctxt_table_t
2280
0
                            + SIGNIFICANT_COEFF_FLAG_8X8_FRAME;
2281
2282
0
            ps_dec->s_high_profile.ps_last_sigcoeff_8x8_frame =
2283
0
                            p_cabac_ctxt_table_t
2284
0
                                            + LAST_SIGNIFICANT_COEFF_FLAG_8X8_FRAME;
2285
2286
0
            ps_dec->s_high_profile.ps_coeff_abs_levelminus1 =
2287
0
                            p_cabac_ctxt_table_t + COEFF_ABS_LEVEL_MINUS1_8X8;
2288
2289
0
            ps_dec->s_high_profile.ps_sigcoeff_8x8_field = p_cabac_ctxt_table_t
2290
0
                            + SIGNIFICANT_COEFF_FLAG_8X8_FIELD;
2291
2292
0
            ps_dec->s_high_profile.ps_last_sigcoeff_8x8_field =
2293
0
                            p_cabac_ctxt_table_t
2294
0
                                            + LAST_SIGNIFICANT_COEFF_FLAG_8X8_FIELD;
2295
0
        }
2296
0
    }
2297
0
    return (i16_status);
2298
0
}
2299
2300
/*!
2301
 **************************************************************************
2302
 * \if Function name : ih264d_free_buf_mgr_mutexes \endif
2303
 *
2304
 * \brief
2305
 *    Destroys the mutexes of the picture and MV buffer managers if they were
2306
 *    initialized. The buffer managers are initialized again whenever the
2307
 *    picture buffers are created, so their mutexes have to be destroyed
2308
 *    before that, before ih264d_init_decoder() clears the managers, and
2309
 *    when the decoder is deleted. Otherwise each of these leaks a mutex on
2310
 *    platforms where mutexes hold resources.
2311
 *
2312
 * \param ps_dec: Pointer to dec_struct_t.
2313
 *
2314
 * \return
2315
 *    None
2316
 *
2317
 **************************************************************************
2318
 */
2319
static void ih264d_free_buf_mgr_mutexes(dec_struct_t *ps_dec)
2320
0
{
2321
0
    buf_mgr_t *ps_pic_buf_mgr = (buf_mgr_t *)ps_dec->pv_pic_buf_mgr;
2322
0
    buf_mgr_t *ps_mv_buf_mgr = (buf_mgr_t *)ps_dec->pv_mv_buf_mgr;
2323
2324
0
    if(ps_pic_buf_mgr && ps_pic_buf_mgr->pv_mutex)
2325
0
    {
2326
0
        ih264_buf_mgr_free(ps_pic_buf_mgr);
2327
0
        ps_pic_buf_mgr->pv_mutex = NULL;
2328
0
    }
2329
0
    if(ps_mv_buf_mgr && ps_mv_buf_mgr->pv_mutex)
2330
0
    {
2331
0
        ih264_buf_mgr_free(ps_mv_buf_mgr);
2332
0
        ps_mv_buf_mgr->pv_mutex = NULL;
2333
0
    }
2334
0
}
2335
2336
/*!
2337
 **************************************************************************
2338
 * \if Function name : ih264d_free_dynamic_bufs \endif
2339
 *
2340
 * \brief
2341
 *    This function frees dynamic memory allocated by Decoder.
2342
 *
2343
 * \param ps_dec: Pointer to dec_struct_t.
2344
 *
2345
 * \return
2346
 *    Returns i4_status as returned by MemManager.
2347
 *
2348
 **************************************************************************
2349
 */
2350
WORD16 ih264d_free_dynamic_bufs(dec_struct_t * ps_dec)
2351
0
{
2352
0
    ih264d_free_buf_mgr_mutexes(ps_dec);
2353
0
    PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->ps_deblk_pic);
2354
0
    PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->pu1_dec_mb_map);
2355
0
    PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->pu1_recon_mb_map);
2356
0
    PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->pu2_slice_num_map);
2357
0
    PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->ps_dec_slice_buf);
2358
0
    PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->ps_frm_mb_info);
2359
0
    PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->pi2_coeff_data);
2360
0
    PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->ps_parse_mb_data);
2361
0
    PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->ps_parse_part_params);
2362
0
    PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->ps_deblk_top_mb);
2363
2364
0
    if(ps_dec->p_ctxt_inc_mb_map)
2365
0
    {
2366
0
        ps_dec->p_ctxt_inc_mb_map -= 1;
2367
0
        PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->p_ctxt_inc_mb_map);
2368
0
    }
2369
2370
0
    PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->ps_mv_p[0]);
2371
0
    PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->ps_mv_p[1]);
2372
0
    PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->ps_pred_pkd);
2373
0
    {
2374
0
        UWORD8 i;
2375
0
        for(i = 0; i < MV_SCRATCH_BUFS; i++)
2376
0
        {
2377
0
            PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->ps_mv_top_p[i]);
2378
0
        }
2379
0
    }
2380
2381
0
    if(ps_dec->pu1_y_intra_pred_line)
2382
0
    {
2383
0
        ps_dec->pu1_y_intra_pred_line -= MB_SIZE;
2384
0
    }
2385
0
    PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->pu1_y_intra_pred_line);
2386
2387
0
    if(ps_dec->pu1_u_intra_pred_line)
2388
0
    {
2389
0
        ps_dec->pu1_u_intra_pred_line -= MB_SIZE;
2390
0
    }
2391
0
    PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->pu1_u_intra_pred_line);
2392
2393
0
    if(ps_dec->pu1_v_intra_pred_line)
2394
0
    {
2395
0
        ps_dec->pu1_v_intra_pred_line -= MB_SIZE;
2396
0
    }
2397
0
    PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->pu1_v_intra_pred_line);
2398
0
    PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->ps_nbr_mb_row);
2399
0
    PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->pu1_mv_bank_buf_base);
2400
0
    PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->pu1_pic_buf_base);
2401
2402
    /* Free memory for mb_info maps */
2403
0
    if(ps_dec->u1_enable_mb_info)
2404
0
    {
2405
0
        PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->pu1_qp_map_base);
2406
0
        PS_DEC_ALIGNED_FREE(ps_dec, ps_dec->pu1_mb_type_map_base);
2407
0
    }
2408
0
    return 0;
2409
0
}
2410
2411
/*!
2412
 **************************************************************************
2413
 * \if Function name : ih264d_create_mv_bank \endif
2414
 *
2415
 * \brief
2416
 *    This function creates MV bank.
2417
 *
2418
 * \param memType  : Type of memory being handled
2419
 *                   0: Display Buffer
2420
 *                   1: Decoder Buffer
2421
 *                   2: Internal Buffer
2422
 * \param u1_num_of_buf: Number of decode or display buffers.
2423
 * \param u4_wd : Frame width.
2424
 * \param u4_ht : Frame Height.
2425
 * \param ps_pic_buf_api : Pointer to Picture Buffer API.
2426
 * \param ih264d_dec_mem_manager  : Memory manager utility supplied by system.
2427
 *
2428
 * \return
2429
 *    0 on Success and -1 on error
2430
 *
2431
 **************************************************************************
2432
 */
2433
WORD32 ih264d_create_mv_bank(void *pv_dec,
2434
                             UWORD32 ui_width,
2435
                             UWORD32 ui_height)
2436
0
{
2437
0
    UWORD8  i;
2438
0
    UWORD32 col_flag_buffer_size, mvpred_buffer_size;
2439
0
    UWORD8 *pu1_mv_buf_mgr_base, *pu1_mv_bank_base;
2440
0
    col_mv_buf_t *ps_col_mv;
2441
0
    mv_pred_t *ps_mv;
2442
0
    UWORD8 *pu1_col_zero_flag_buf;
2443
0
    dec_struct_t *ps_dec = (dec_struct_t *)pv_dec;
2444
0
    WORD32 buf_ret;
2445
0
    UWORD32 u4_num_bufs;
2446
0
    UWORD8 *pu1_buf;
2447
0
    WORD32 size;
2448
0
    void *pv_mem_ctxt = ps_dec->pv_mem_ctxt;
2449
2450
0
    col_flag_buffer_size = ((ui_width * ui_height) >> 4);
2451
0
    mvpred_buffer_size = sizeof(mv_pred_t)
2452
0
                    * ((ui_width * (ui_height + PAD_MV_BANK_ROW)) >> 4);
2453
2454
0
    ih264_buf_mgr_init((buf_mgr_t *)ps_dec->pv_mv_buf_mgr);
2455
2456
0
    ps_col_mv = ps_dec->ps_col_mv_base;
2457
2458
0
    {
2459
0
        UWORD8 u1_num_ref_frames = ps_dec->u4_disable_ref_retention ?
2460
0
                        !ps_dec->ps_cur_sps->u1_frame_mbs_only_flag :
2461
0
                        ps_dec->ps_cur_sps->u1_num_ref_frames;
2462
0
        u4_num_bufs = u1_num_ref_frames + 1;
2463
0
    }
2464
2465
0
    u4_num_bufs = MIN(u4_num_bufs, ps_dec->u1_pic_bufs);
2466
0
    if(!ps_dec->u4_disable_ref_retention)
2467
0
    {
2468
0
        u4_num_bufs = MAX(u4_num_bufs, 2);
2469
0
    }
2470
0
    pu1_buf = ps_dec->pu1_mv_bank_buf_base;
2471
0
    for(i = 0 ; i < u4_num_bufs ; i++)
2472
0
    {
2473
0
        pu1_col_zero_flag_buf = pu1_buf;
2474
0
        pu1_buf += ALIGN64(col_flag_buffer_size);
2475
2476
0
        ps_mv = (mv_pred_t *)pu1_buf;
2477
0
        pu1_buf += ALIGN64(mvpred_buffer_size);
2478
2479
0
        memset(ps_mv, 0, ((ui_width * OFFSET_MV_BANK_ROW) >> 4) * sizeof(mv_pred_t));
2480
0
        ps_mv += (ui_width*OFFSET_MV_BANK_ROW) >> 4;
2481
2482
0
        ps_col_mv->pv_col_zero_flag = (void *)pu1_col_zero_flag_buf;
2483
0
        ps_col_mv->pv_mv = (void *)ps_mv;
2484
0
        buf_ret = ih264_buf_mgr_add((buf_mgr_t *)ps_dec->pv_mv_buf_mgr, ps_col_mv, i);
2485
0
        if(0 != buf_ret)
2486
0
        {
2487
0
            ps_dec->i4_error_code = ERROR_BUF_MGR;
2488
0
            return ERROR_BUF_MGR;
2489
0
        }
2490
0
        ps_col_mv++;
2491
0
    }
2492
0
    return OK;
2493
0
}
2494
2495
void ih264d_unpack_coeff4x4_dc_4x4blk(tu_sblk4x4_coeff_data_t *ps_tu_4x4,
2496
                                      WORD16 *pi2_out_coeff_data,
2497
                                      UWORD8 *pu1_inv_scan)
2498
157k
{
2499
157k
    UWORD16 u2_sig_coeff_map = ps_tu_4x4->u2_sig_coeff_map;
2500
157k
    WORD32 idx;
2501
157k
    WORD16 *pi2_coeff_data = &ps_tu_4x4->ai2_level[0];
2502
2503
356k
    while(u2_sig_coeff_map)
2504
198k
    {
2505
198k
        idx = CLZ(u2_sig_coeff_map);
2506
2507
198k
        idx = 31 - idx;
2508
198k
        RESET_BIT(u2_sig_coeff_map,idx);
2509
2510
198k
        idx = pu1_inv_scan[idx];
2511
198k
        pi2_out_coeff_data[idx] = *pi2_coeff_data++;
2512
2513
198k
    }
2514
157k
}