Coverage Report

Created: 2025-12-29 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libhevc/encoder/ihevce_encode_header.c
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Copyright (C) 2018 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
******************************************************************************
23
* @file ihevce_encode_header.c
24
*
25
* @brief
26
*   This file contains function definitions related to header encoding
27
*
28
* @author
29
*   Ittiam
30
*
31
* List of Functions
32
*   ihevce_generate_nal_unit_header
33
*   ihevce_generate_when_profile_present
34
*   ihevce_generate_profile_tier_level
35
*   ihevce_short_term_ref_pic_set
36
*   ihevce_generate_bit_rate_pic_rate_info
37
*   ihevce_generate_aud
38
*   ihevce_generate_eos
39
*   ihevce_generate_vps
40
*   ihevce_generate_sps
41
*   ihevce_generate_pps
42
*   ihevce_generate_slice_header
43
*   ihevce_populate_vps
44
*   ihevce_populate_sps
45
*   ihevce_populate_pps
46
*   ihevce_populate_slice_header
47
*   ihevce_insert_entry_offset_slice_header
48
*
49
******************************************************************************
50
*/
51
52
/*****************************************************************************/
53
/* File Includes                                                             */
54
/*****************************************************************************/
55
/* System include files */
56
#include <stdio.h>
57
#include <string.h>
58
#include <stdlib.h>
59
#include <assert.h>
60
#include <stdarg.h>
61
#include <math.h>
62
63
/* User include files */
64
#include "ihevc_typedefs.h"
65
#include "itt_video_api.h"
66
#include "ihevce_api.h"
67
68
#include "rc_cntrl_param.h"
69
#include "rc_frame_info_collector.h"
70
#include "rc_look_ahead_params.h"
71
72
#include "ihevc_defs.h"
73
#include "ihevc_macros.h"
74
#include "ihevc_debug.h"
75
#include "ihevc_structs.h"
76
#include "ihevc_platform_macros.h"
77
#include "ihevc_deblk.h"
78
#include "ihevc_itrans_recon.h"
79
#include "ihevc_chroma_itrans_recon.h"
80
#include "ihevc_chroma_intra_pred.h"
81
#include "ihevc_intra_pred.h"
82
#include "ihevc_inter_pred.h"
83
#include "ihevc_mem_fns.h"
84
#include "ihevc_padding.h"
85
#include "ihevc_weighted_pred.h"
86
#include "ihevc_sao.h"
87
#include "ihevc_resi_trans.h"
88
#include "ihevc_quant_iquant_ssd.h"
89
#include "ihevc_cabac_tables.h"
90
#include "ihevc_trans_tables.h"
91
#include "ihevc_trans_macros.h"
92
93
#include "ihevce_defs.h"
94
#include "ihevce_lap_enc_structs.h"
95
#include "ihevce_multi_thrd_structs.h"
96
#include "ihevce_multi_thrd_funcs.h"
97
#include "ihevce_me_common_defs.h"
98
#include "ihevce_had_satd.h"
99
#include "ihevce_error_codes.h"
100
#include "ihevce_error_checks.h"
101
#include "ihevce_bitstream.h"
102
#include "ihevce_cabac.h"
103
#include "ihevce_rdoq_macros.h"
104
#include "ihevce_function_selector.h"
105
#include "ihevce_enc_structs.h"
106
#include "ihevce_global_tables.h"
107
#include "ihevce_encode_header.h"
108
#include "ihevce_encode_header_sei_vui.h"
109
#include "ihevce_trace.h"
110
111
/*****************************************************************************/
112
/* Constant Macros                                                           */
113
/*****************************************************************************/
114
261
#define CU_LEVEL_QP_LIMIT_8x8 3
115
#define CU_LEVEL_QP_LIMIT_16x16 2
116
#define CU_LEVEL_QP_LIMIT_32x32 1
117
118
/*****************************************************************************/
119
/* Function Definitions                                                      */
120
/*****************************************************************************/
121
122
/**
123
******************************************************************************
124
*
125
*  @brief Generate nal unit header in the stream as per section 7.3.1.2
126
*
127
*  @par   Description
128
*  Inserts the nal type and temporal id plus 1 as per section 7.3.1.2 Nal unit
129
*  header syntax
130
*
131
*  @param[inout]   ps_bitstrm
132
*  pointer to bitstream context (handle)
133
*
134
*  @param[in]   nal_unit_type
135
*  nal type to be inserted
136
*
137
*  @param[in]   temporal id
138
*  temporal id to be inserted
139
*
140
*  @return      success or failure error code
141
*
142
******************************************************************************
143
*/
144
WORD32 ihevce_generate_nal_unit_header(
145
    bitstrm_t *ps_bitstrm, WORD32 nal_unit_type, WORD32 nuh_temporal_id)
146
9.68k
{
147
9.68k
    WORD32 return_status = IHEVCE_SUCCESS;
148
149
    /* sanity checks */
150
9.68k
    ASSERT((nal_unit_type >= 0) && (nal_unit_type < 64));
151
9.68k
    ASSERT((nuh_temporal_id >= 0) && (nuh_temporal_id < 7));
152
153
    /* forbidden_zero_bit + nal_unit_type */
154
9.68k
    PUT_BITS(
155
9.68k
        ps_bitstrm,
156
9.68k
        nal_unit_type,
157
9.68k
        (1 + 6), /*extra 1 is for forbidden zero bit */
158
9.68k
        return_status);
159
160
    /* nuh_reserved_zero_6bits */
161
9.68k
    PUT_BITS(ps_bitstrm, 0, 6, return_status);
162
163
    /* nuh_temporal_id_plus1 */
164
9.68k
    PUT_BITS(ps_bitstrm, (nuh_temporal_id + 1), 3, return_status);
165
166
9.68k
    return (return_status);
167
9.68k
}
168
169
/**
170
******************************************************************************
171
*
172
*  @brief Generates fields related to Profile, Tier and Level data.
173
*
174
*  @par   Description
175
*  Generates fields related to Profile, Tier and Level data.
176
*  Called when profile_present flag is 1
177
*
178
*  @param[in]   ps_bitstrm
179
*  pointer to bitstream context (handle)
180
*
181
*  @param[in]   ps_ptl
182
*  pointer to structure containing Profile, Tier and Level data data
183
*
184
*  @return      success or failure error code
185
*
186
******************************************************************************
187
*/
188
static WORD32
189
    ihevce_generate_when_profile_present(bitstrm_t *ps_bitstrm, profile_tier_lvl_t *ps_ptl)
190
2.29k
{
191
2.29k
    WORD32 return_status = IHEVCE_SUCCESS;
192
2.29k
    WORD32 i;
193
194
    /* XXX_profile_space[] */
195
2.29k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_profile_space, 2, return_status);
196
2.29k
    ENTROPY_TRACE("XXX_profile_space[]", ps_ptl->i1_profile_space);
197
198
    /* XXX_tier_flag[] */
199
2.29k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_tier_flag, 1, return_status);
200
2.29k
    ENTROPY_TRACE("XXX_tier_flag[]", ps_ptl->i1_tier_flag);
201
202
    /* XXX_profile_idc[] */
203
2.29k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_profile_idc, 5, return_status);
204
2.29k
    ENTROPY_TRACE("XXX_profile_idc[]", ps_ptl->i1_profile_idc);
205
206
75.5k
    for(i = 0; i < MAX_PROFILE_COMPATBLTY; i++)
207
73.2k
    {
208
        /* XXX_profile_compatibility_flag[][j] */
209
73.2k
        PUT_BITS(ps_bitstrm, ps_ptl->ai1_profile_compatibility_flag[i], 1, return_status);
210
73.2k
        ENTROPY_TRACE(
211
73.2k
            "XXX_profile_compatibility_flag[][j]", ps_ptl->ai1_profile_compatibility_flag[i]);
212
73.2k
    }
213
214
    /* XXX_progressive_source_flag[] */
215
2.29k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_progressive_source_flag, 1, return_status);
216
2.29k
    ENTROPY_TRACE("XXX_progressive_source_flag[]", ps_ptl->i1_general_progressive_source_flag);
217
218
    /* XXX_interlaced_source_flag[] */
219
2.29k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_interlaced_source_flag, 1, return_status);
220
2.29k
    ENTROPY_TRACE("XXX_interlaced_source_flag[]", ps_ptl->i1_general_interlaced_source_flag);
221
222
    /* XXX_non_packed_constraint_flag[] */
223
2.29k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_non_packed_constraint_flag, 1, return_status);
224
2.29k
    ENTROPY_TRACE(
225
2.29k
        "XXX_non_packed_constraint_flag[]", ps_ptl->i1_general_non_packed_constraint_flag);
226
227
    /* XXX_frame_only_constraint_flag[] */
228
2.29k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_frame_only_constraint_flag, 1, return_status);
229
2.29k
    ENTROPY_TRACE("XXX_frame_only_constraint_flag[]", ps_ptl->i1_frame_only_constraint_flag);
230
231
    /* XXX_general_max_12bit_constraint_flag[] */
232
2.29k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_max_12bit_constraint_flag, 1, return_status);
233
2.29k
    ENTROPY_TRACE(
234
2.29k
        "XXX_general_max_12bit_constraint_flag[]", ps_ptl->i1_general_max_12bit_constraint_flag);
235
236
    /* XXX_general_max_10bit_constraint_flag[] */
237
2.29k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_max_10bit_constraint_flag, 1, return_status);
238
2.29k
    ENTROPY_TRACE(
239
2.29k
        "XXX_general_max_10bit_constraint_flag[]", ps_ptl->i1_general_max_10bit_constraint_flag);
240
241
    /* XXX_general_max_8bit_constraint_flag[] */
242
2.29k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_max_8bit_constraint_flag, 1, return_status);
243
2.29k
    ENTROPY_TRACE(
244
2.29k
        "XXX_general_max_8bit_constraint_flag[]", ps_ptl->i1_general_max_8bit_constraint_flag);
245
246
    /* XXX_general_max_422chroma_constraint_flag[] */
247
2.29k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_max_422chroma_constraint_flag, 1, return_status);
248
2.29k
    ENTROPY_TRACE(
249
2.29k
        "XXX_general_max_422chroma_constraint_flag[]",
250
2.29k
        ps_ptl->i1_general_max_422chroma_constraint_flag);
251
252
    /* XXX_general_max_420chroma_constraint_flag[] */
253
2.29k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_max_420chroma_constraint_flag, 1, return_status);
254
2.29k
    ENTROPY_TRACE(
255
2.29k
        "XXX_general_max_420chroma_constraint_flag[]",
256
2.29k
        ps_ptl->i1_general_max_420chroma_constraint_flag);
257
258
    /* XXX_general_max_monochrome_constraint_flag[] */
259
2.29k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_max_monochrome_constraint_flag, 1, return_status);
260
2.29k
    ENTROPY_TRACE(
261
2.29k
        "XXX_general_max_monochrome_constraint_flag[]",
262
2.29k
        ps_ptl->i1_general_max_monochrome_constraint_flag);
263
264
    /* XXX_general_intra_constraint_flag[] */
265
2.29k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_intra_constraint_flag, 1, return_status);
266
2.29k
    ENTROPY_TRACE("XXX_general_intra_constraint_flag[]", ps_ptl->i1_general_intra_constraint_flag);
267
268
    /* XXX_general_one_picture_only_constraint_flag[] */
269
2.29k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_one_picture_only_constraint_flag, 1, return_status);
270
2.29k
    ENTROPY_TRACE(
271
2.29k
        "XXX_general_one_picture_only_constraint_flag[]",
272
2.29k
        ps_ptl->i1_general_one_picture_only_constraint_flag);
273
274
    /* XXX_general_lower_bit_rate_constraint_flag[] */
275
2.29k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_lower_bit_rate_constraint_flag, 1, return_status);
276
2.29k
    ENTROPY_TRACE(
277
2.29k
        "XXX_general_lower_bit_rate_constraint_flag[]",
278
2.29k
        ps_ptl->i1_general_lower_bit_rate_constraint_flag);
279
280
    /* XXX_reserved_zero_35bits[] */
281
2.29k
    PUT_BITS(ps_bitstrm, 0, 16, return_status);
282
2.29k
    PUT_BITS(ps_bitstrm, 0, 16, return_status);
283
2.29k
    PUT_BITS(ps_bitstrm, 0, 3, return_status);
284
2.29k
    ENTROPY_TRACE("XXX_reserved_zero_35bits[]", 0);
285
286
2.29k
    return return_status;
287
2.29k
}
288
289
/**
290
******************************************************************************
291
*
292
*  @brief Generates Profile, Tier and Level data
293
*
294
*  @par   Description
295
*  Generates Profile, Tier and Level data as per Section 7.3.3
296
*
297
*  @param[in]   ps_bitstrm
298
*  pointer to bitstream context (handle)
299
*
300
*  @param[in]   ps_ptl
301
*  pointer to structure containing Profile, Tier and Level data data
302
*
303
*  @param[in]   i1_profile_present_flag
304
*  flag that indicates whether profile-related data is present
305
*
306
*  @param[in]   i1_vps_max_sub_layers_minus1
307
*  (Maximum number of sub_layers present) minus 1
308
*
309
*  @return      success or failure error code
310
*
311
******************************************************************************
312
*/
313
static WORD32 ihevce_generate_profile_tier_level(
314
    bitstrm_t *ps_bitstrm,
315
    profile_tier_lvl_info_t *ps_ptl,
316
    WORD8 i1_profile_present_flag,
317
    WORD8 i1_max_sub_layers_minus1)
318
2.29k
{
319
2.29k
    WORD32 i;
320
2.29k
    WORD32 return_status = IHEVCE_SUCCESS;
321
322
2.29k
    if(i1_profile_present_flag)
323
2.29k
    {
324
2.29k
        ihevce_generate_when_profile_present(ps_bitstrm, &ps_ptl->s_ptl_gen);
325
2.29k
    }
326
327
    /* general_level_idc */
328
2.29k
    PUT_BITS(ps_bitstrm, ps_ptl->s_ptl_gen.u1_level_idc, 8, return_status);
329
2.29k
    ENTROPY_TRACE("general_level_idc", ps_ptl->s_ptl_gen.u1_level_idc);
330
331
2.29k
    for(i = 0; i < i1_max_sub_layers_minus1; i++)
332
0
    {
333
        /* sub_layer_profile_present_flag[i] */
334
0
        PUT_BITS(ps_bitstrm, ps_ptl->ai1_sub_layer_profile_present_flag[i], 1, return_status);
335
0
        ENTROPY_TRACE(
336
0
            "sub_layer_profile_present_flag[i]", ps_ptl->ai1_sub_layer_profile_present_flag[i]);
337
338
        /* sub_layer_level_present_flag[i] */
339
0
        PUT_BITS(ps_bitstrm, ps_ptl->ai1_sub_layer_level_present_flag[i], 1, return_status);
340
0
        ENTROPY_TRACE(
341
0
            "sub_layer_level_present_flag[i]", ps_ptl->ai1_sub_layer_level_present_flag[i]);
342
0
    }
343
344
2.29k
    if(i1_max_sub_layers_minus1 > 0)
345
0
    {
346
0
        for(i = i1_max_sub_layers_minus1; i < 8; i++)
347
0
        {
348
            /* reserved_zero_2bits[i] */
349
0
            PUT_BITS(ps_bitstrm, 0, 2, return_status);
350
0
            ENTROPY_TRACE("reserved_zero_2bits[i]", 0);
351
0
        }
352
0
    }
353
354
2.29k
    for(i = 0; i < i1_max_sub_layers_minus1; i++)
355
0
    {
356
0
        if(ps_ptl->ai1_sub_layer_profile_present_flag[i])
357
0
        {
358
0
            ihevce_generate_when_profile_present(ps_bitstrm, &ps_ptl->as_ptl_sub[i]);
359
0
        }
360
361
0
        if(ps_ptl->ai1_sub_layer_level_present_flag[i])  //TEMPORALA_SCALABILITY CHANGES BUG_FIX
362
0
        {
363
            /* sub_layer_level_idc[i] */
364
0
            PUT_BITS(ps_bitstrm, ps_ptl->as_ptl_sub[i].u1_level_idc, 8, return_status);
365
0
            ENTROPY_TRACE("sub_layer_level_idc[i]", ps_ptl->as_ptl_sub[i].u1_level_idc);
366
0
        }
367
0
    }
368
369
2.29k
    return return_status;
370
2.29k
}
371
372
/**
373
*******************************************************************************
374
*
375
* @brief
376
*  Generates short term reference picture set
377
*
378
* @par   Description
379
*  Generates short term reference picture set as per section 7.3.5.2.
380
*  Can be called by either SPS or Slice header parsing modules.
381
*
382
* @param[in] ps_bitstrm
383
*  Pointer to bitstream structure
384
*
385
* @param[out] ps_stref_picset_base
386
*  Pointer to first short term ref pic set structure
387
*
388
* @param[in] num_short_term_ref_pic_sets
389
*  Number of short term reference pic sets
390
*
391
* @param[in] idx
392
*  Current short term ref pic set id
393
*
394
* @returns Error code from WORD32
395
*
396
*
397
*******************************************************************************
398
*/
399
static WORD32 ihevce_short_term_ref_pic_set(
400
    bitstrm_t *ps_bitstrm,
401
    stref_picset_t *ps_stref_picset_base,
402
    WORD32 num_short_term_ref_pic_sets,
403
    WORD32 idx,
404
    WORD32 *pi4_NumPocTotalCurr)
405
5.45k
{
406
5.45k
    WORD32 i;
407
5.45k
    WORD32 return_status = IHEVCE_SUCCESS;
408
5.45k
    stref_picset_t *ps_stref_picset = ps_stref_picset_base + idx;
409
410
5.45k
    (void)num_short_term_ref_pic_sets;
411
5.45k
    if(idx > 0)
412
0
    {
413
        /* inter_ref_pic_set_prediction_flag */
414
0
        PUT_BITS(
415
0
            ps_bitstrm, ps_stref_picset->i1_inter_ref_pic_set_prediction_flag, 1, return_status);
416
0
        ENTROPY_TRACE(
417
0
            "inter_ref_pic_set_prediction_flag",
418
0
            ps_stref_picset->i1_inter_ref_pic_set_prediction_flag);
419
0
    }
420
421
    /* This flag is assumed to be 0 for now */
422
5.45k
    ASSERT(0 == ps_stref_picset->i1_inter_ref_pic_set_prediction_flag);
423
424
    /* num_negative_pics */
425
5.45k
    PUT_BITS_UEV(ps_bitstrm, ps_stref_picset->i1_num_neg_pics, return_status);
426
5.45k
    ENTROPY_TRACE("num_negative_pics", ps_stref_picset->i1_num_neg_pics);
427
428
    /* num_positive_pics */
429
5.45k
    PUT_BITS_UEV(ps_bitstrm, ps_stref_picset->i1_num_pos_pics, return_status);
430
5.45k
    ENTROPY_TRACE("num_positive_pics", ps_stref_picset->i1_num_pos_pics);
431
432
21.8k
    for(i = 0; i < ps_stref_picset->i1_num_neg_pics; i++)
433
16.3k
    {
434
        /* delta_poc_s0_minus1 */
435
16.3k
        PUT_BITS_UEV(ps_bitstrm, ps_stref_picset->ai2_delta_poc[i] - 1, return_status);
436
16.3k
        ENTROPY_TRACE("delta_poc_s0_minus1", ps_stref_picset->ai2_delta_poc[i] - 1);
437
438
        /* used_by_curr_pic_s0_flag */
439
16.3k
        PUT_BITS(ps_bitstrm, ps_stref_picset->ai1_used[i], 1, return_status);
440
16.3k
        ENTROPY_TRACE("used_by_curr_pic_s0_flag", ps_stref_picset->ai1_used[i]);
441
        /*get the num pocs used for cur pic*/
442
16.3k
        if(ps_stref_picset->ai1_used[i])
443
15.9k
        {
444
15.9k
            *pi4_NumPocTotalCurr += 1;
445
15.9k
        }
446
16.3k
    }
447
448
6.50k
    for(; i < (ps_stref_picset->i1_num_pos_pics + ps_stref_picset->i1_num_neg_pics); i++)
449
1.04k
    {
450
        /* delta_poc_s1_minus1 */
451
1.04k
        PUT_BITS_UEV(ps_bitstrm, ps_stref_picset->ai2_delta_poc[i] - 1, return_status);
452
1.04k
        ENTROPY_TRACE("delta_poc_s1_minus1", ps_stref_picset->ai2_delta_poc[i] - 1);
453
454
        /* used_by_curr_pic_s1_flag */
455
1.04k
        PUT_BITS(ps_bitstrm, ps_stref_picset->ai1_used[i], 1, return_status);
456
1.04k
        ENTROPY_TRACE("used_by_curr_pic_s1_flag", ps_stref_picset->ai1_used[i]);
457
        /*get the num pocs used for cur pic*/
458
1.04k
        if(ps_stref_picset->ai1_used[i])
459
1.04k
        {
460
1.04k
            *pi4_NumPocTotalCurr += 1;
461
1.04k
        }
462
1.04k
    }
463
464
5.45k
    return return_status;
465
5.45k
}
466
467
/**
468
******************************************************************************
469
*
470
*  @brief Generates ref pic list modification
471
*
472
*  @par   Description
473
*  Generate ref pic list modification syntax as per Section 7.3.6.2
474
*
475
*  @param[in]   ps_bitstrm
476
*  pointer to bitstream context (handle)
477
*
478
*  @param[in]   ps_slice_hdr
479
*  pointer to structure containing slice header
480
*
481
*  @return      success or failure error code
482
*
483
******************************************************************************
484
*/
485
static WORD32 ref_pic_list_modification(
486
    bitstrm_t *ps_bitstrm, slice_header_t *ps_slice_hdr, WORD32 i4_NumPocTotalCurr)
487
0
{
488
0
    WORD32 return_status = IHEVCE_SUCCESS;
489
0
    WORD32 i;
490
491
    /* ref_pic_list_modification_flag_l0 */
492
0
    PUT_BITS(
493
0
        ps_bitstrm, ps_slice_hdr->s_rplm.i1_ref_pic_list_modification_flag_l0, 1, return_status);
494
0
    ENTROPY_TRACE(
495
0
        "ref_pic_list_modification_flag_l0",
496
0
        ps_slice_hdr->s_rplm.i1_ref_pic_list_modification_flag_l0);
497
498
0
    if(ps_slice_hdr->s_rplm.i1_ref_pic_list_modification_flag_l0)
499
0
    {
500
0
        for(i = 0; i <= (ps_slice_hdr->i1_num_ref_idx_l0_active - 1); i++)
501
0
        {
502
0
            WORD32 num_bits = 32 - CLZ(i4_NumPocTotalCurr - 1);
503
504
            /* list_entry_l0[ i ] */
505
0
            PUT_BITS(ps_bitstrm, ps_slice_hdr->s_rplm.i1_list_entry_l0[i], num_bits, return_status);
506
0
            ENTROPY_TRACE("list_entry_l0", ps_slice_hdr->s_rplm.i1_list_entry_l0[i]);
507
0
        }
508
0
    }
509
510
0
    if((BSLICE == ps_slice_hdr->i1_slice_type))
511
0
    {
512
        /* ref_pic_list_modification_flag_l1 */
513
0
        PUT_BITS(
514
0
            ps_bitstrm, ps_slice_hdr->s_rplm.i1_ref_pic_list_modification_flag_l1, 1, return_status);
515
0
        ENTROPY_TRACE(
516
0
            "ref_pic_list_modification_flag_l1",
517
0
            ps_slice_hdr->s_rplm.i1_ref_pic_list_modification_flag_l1);
518
519
0
        if(ps_slice_hdr->s_rplm.i1_ref_pic_list_modification_flag_l1)
520
0
        {
521
0
            for(i = 0; i <= (ps_slice_hdr->i1_num_ref_idx_l1_active - 1); i++)
522
0
            {
523
0
                WORD32 num_bits = 32 - CLZ(i4_NumPocTotalCurr - 1);
524
525
                /* list_entry_l1[ i ] */
526
0
                PUT_BITS(
527
0
                    ps_bitstrm, ps_slice_hdr->s_rplm.i1_list_entry_l1[i], num_bits, return_status);
528
0
                ENTROPY_TRACE("list_entry_l1", ps_slice_hdr->s_rplm.i1_list_entry_l1[i]);
529
0
            }
530
0
        }
531
0
    } /*end of B slice check*/
532
533
0
    return return_status;
534
0
}
535
536
/**
537
******************************************************************************
538
*
539
*  @brief Generates Pred Weight Table
540
*
541
*  @par   Description
542
*  Generate Pred Weight Table as per Section 7.3.5.4
543
*
544
*  @param[in]   ps_bitstrm
545
*  pointer to bitstream context (handle)
546
*
547
*  @param[in]   ps_sps
548
*  pointer to structure containing SPS data
549
*
550
*  @param[in]   ps_pps
551
*  pointer to structure containing PPS data
552
*
553
*  @param[in]   ps_slice_hdr
554
*  pointer to structure containing slice header
555
*
556
*  @return      success or failure error code
557
*
558
******************************************************************************
559
*/
560
static WORD32 ihevce_generate_pred_weight_table(
561
    bitstrm_t *ps_bitstrm, sps_t *ps_sps, pps_t *ps_pps, slice_header_t *ps_slice_hdr)
562
0
{
563
0
    WORD32 i;
564
0
    WORD32 delta_luma_weight;
565
0
    WORD32 delta_chroma_weight;
566
0
    WORD32 return_status = IHEVCE_SUCCESS;
567
0
    pred_wt_ofst_t *ps_wt_ofst = &ps_slice_hdr->s_wt_ofst;
568
0
    UWORD32 u4_luma_log2_weight_denom = ps_wt_ofst->i1_luma_log2_weight_denom;
569
0
    WORD32 chroma_log2_weight_denom = (ps_wt_ofst->i1_chroma_log2_weight_denom);
570
0
    WORD32 i4_wght_count = 0;
571
572
0
    (void)ps_pps;
573
    /* luma_log2_weight_denom */
574
0
    PUT_BITS_UEV(ps_bitstrm, u4_luma_log2_weight_denom, return_status);
575
0
    ENTROPY_TRACE("luma_log2_weight_denom", u4_luma_log2_weight_denom);
576
577
0
    if(ps_sps->i1_chroma_format_idc != 0)
578
0
    {
579
        /* delta_chroma_log2_weight_denom */
580
0
        PUT_BITS_SEV(
581
0
            ps_bitstrm, chroma_log2_weight_denom - u4_luma_log2_weight_denom, return_status);
582
0
        ENTROPY_TRACE(
583
0
            "delta_chroma_log2_weight_denom", chroma_log2_weight_denom - u4_luma_log2_weight_denom);
584
0
    }
585
586
0
    for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
587
0
    {
588
        /* luma_weight_l0_flag[ i ] */
589
0
        PUT_BITS(ps_bitstrm, ps_wt_ofst->i1_luma_weight_l0_flag[i], 1, return_status);
590
0
        i4_wght_count += ps_wt_ofst->i1_luma_weight_l0_flag[i];
591
0
        assert(i4_wght_count <= 24);
592
0
        ENTROPY_TRACE("luma_weight_l0_flag[ i ]", ps_wt_ofst->i1_luma_weight_l0_flag[i]);
593
0
    }
594
595
0
    if(ps_sps->i1_chroma_format_idc != 0)
596
0
    {
597
0
        for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
598
0
        {
599
            /* chroma_weight_l0_flag[ i ] */
600
0
            PUT_BITS(ps_bitstrm, ps_wt_ofst->i1_chroma_weight_l0_flag[i], 1, return_status);
601
0
            i4_wght_count += 2 * ps_wt_ofst->i1_chroma_weight_l0_flag[i];
602
0
            assert(i4_wght_count <= 24);
603
0
            ENTROPY_TRACE("chroma_weight_l0_flag[ i ]", ps_wt_ofst->i1_chroma_weight_l0_flag[i]);
604
0
        }
605
0
    }
606
607
0
    delta_luma_weight = (1 << u4_luma_log2_weight_denom);
608
0
    delta_chroma_weight = (1 << chroma_log2_weight_denom);
609
610
0
    for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
611
0
    {
612
0
        if(ps_wt_ofst->i1_luma_weight_l0_flag[i])
613
0
        {
614
            /* delta_luma_weight_l0[ i ] */
615
0
            PUT_BITS_SEV(
616
0
                ps_bitstrm, ps_wt_ofst->i2_luma_weight_l0[i] - delta_luma_weight, return_status);
617
0
            ENTROPY_TRACE(
618
0
                "delta_luma_weight_l0[ i ]", ps_wt_ofst->i2_luma_weight_l0[i] - delta_luma_weight);
619
620
            /* luma_offset_l0[ i ] */
621
0
            PUT_BITS_SEV(ps_bitstrm, ps_wt_ofst->i2_luma_offset_l0[i], return_status);
622
0
            ENTROPY_TRACE("luma_offset_l0[ i ]", ps_wt_ofst->i2_luma_offset_l0[i]);
623
0
        }
624
625
0
        if(ps_wt_ofst->i1_chroma_weight_l0_flag[i])
626
0
        {
627
0
            WORD32 shift = (1 << (BIT_DEPTH_CHROMA - 1));
628
0
            WORD32 delta_chroma_weight_l0[2];
629
0
            WORD32 delta_chroma_offset_l0[2];
630
631
0
            delta_chroma_weight_l0[0] = ps_wt_ofst->i2_chroma_weight_l0_cb[i] - delta_chroma_weight;
632
0
            delta_chroma_weight_l0[1] = ps_wt_ofst->i2_chroma_weight_l0_cr[i] - delta_chroma_weight;
633
634
0
            delta_chroma_offset_l0[0] =
635
0
                ps_wt_ofst->i2_chroma_offset_l0_cb[i] +
636
0
                ((shift * ps_wt_ofst->i2_chroma_weight_l0_cb[i]) >> chroma_log2_weight_denom) -
637
0
                shift;
638
0
            delta_chroma_offset_l0[1] =
639
0
                ps_wt_ofst->i2_chroma_offset_l0_cr[i] +
640
0
                ((shift * ps_wt_ofst->i2_chroma_weight_l0_cr[i]) >> chroma_log2_weight_denom) -
641
0
                shift;
642
643
            /* delta_chroma_weight_l0[ i ][j] */
644
0
            PUT_BITS_SEV(ps_bitstrm, delta_chroma_weight_l0[0], return_status);
645
0
            ENTROPY_TRACE("delta_chroma_weight_l0[ i ]", delta_chroma_weight_l0[0]);
646
647
            /* delta_chroma_offset_l0[ i ][j] */
648
0
            PUT_BITS_SEV(ps_bitstrm, delta_chroma_offset_l0[0], return_status);
649
0
            ENTROPY_TRACE("delta_chroma_offset_l0[ i ]", delta_chroma_offset_l0[0]);
650
651
            /* delta_chroma_weight_l0[ i ][j] */
652
0
            PUT_BITS_SEV(ps_bitstrm, delta_chroma_weight_l0[1], return_status);
653
0
            ENTROPY_TRACE("delta_chroma_weight_l0[ i ]", delta_chroma_weight_l0[1]);
654
655
            /* delta_chroma_offset_l0[ i ][j] */
656
0
            PUT_BITS_SEV(ps_bitstrm, delta_chroma_offset_l0[1], return_status);
657
0
            ENTROPY_TRACE("delta_chroma_offset_l0[ i ]", delta_chroma_offset_l0[1]);
658
0
        }
659
0
    }
660
661
0
    if(BSLICE == ps_slice_hdr->i1_slice_type)
662
0
    {
663
0
        for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
664
0
        {
665
            /* luma_weight_l1_flag[ i ] */
666
0
            PUT_BITS(ps_bitstrm, ps_wt_ofst->i1_luma_weight_l1_flag[i], 1, return_status);
667
0
            i4_wght_count += ps_wt_ofst->i1_luma_weight_l1_flag[i];
668
0
            assert(i4_wght_count <= 24);
669
0
            ENTROPY_TRACE("luma_weight_l1_flag[ i ]", ps_wt_ofst->i1_luma_weight_l1_flag[i]);
670
0
        }
671
672
0
        if(ps_sps->i1_chroma_format_idc != 0)
673
0
        {
674
0
            for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
675
0
            {
676
                /* chroma_weight_l1_flag[ i ] */
677
0
                PUT_BITS(ps_bitstrm, ps_wt_ofst->i1_chroma_weight_l1_flag[i], 1, return_status);
678
0
                i4_wght_count += ps_wt_ofst->i1_chroma_weight_l1_flag[i];
679
0
                assert(i4_wght_count <= 24);
680
0
                ENTROPY_TRACE(
681
0
                    "chroma_weight_l1_flag[ i ]", ps_wt_ofst->i1_chroma_weight_l1_flag[i]);
682
0
            }
683
0
        }
684
685
0
        for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
686
0
        {
687
0
            if(ps_wt_ofst->i1_luma_weight_l1_flag[i])
688
0
            {
689
                /* delta_luma_weight_l1[ i ] */
690
0
                PUT_BITS_SEV(
691
0
                    ps_bitstrm,
692
0
                    ps_wt_ofst->i2_luma_weight_l1[i] - delta_luma_weight,
693
0
                    return_status);
694
0
                ENTROPY_TRACE(
695
0
                    "delta_luma_weight_l1[ i ]",
696
0
                    ps_wt_ofst->i2_luma_weight_l1[i] - delta_luma_weight);
697
698
                /* luma_offset_l1[ i ] */
699
0
                PUT_BITS_SEV(ps_bitstrm, ps_wt_ofst->i2_luma_offset_l1[i], return_status);
700
0
                ENTROPY_TRACE("luma_offset_l1[ i ]", ps_wt_ofst->i2_luma_offset_l1[i]);
701
0
            }
702
703
0
            if(ps_wt_ofst->i1_chroma_weight_l1_flag[i])
704
0
            {
705
0
                WORD32 shift = (1 << (BIT_DEPTH_CHROMA - 1));
706
0
                WORD32 delta_chroma_weight_l1[2];
707
0
                WORD32 delta_chroma_offset_l1[2];
708
709
0
                delta_chroma_weight_l1[0] =
710
0
                    ps_wt_ofst->i2_chroma_weight_l1_cb[i] - delta_chroma_weight;
711
0
                delta_chroma_weight_l1[1] =
712
0
                    ps_wt_ofst->i2_chroma_weight_l1_cr[i] - delta_chroma_weight;
713
714
0
                delta_chroma_offset_l1[0] =
715
0
                    ps_wt_ofst->i2_chroma_offset_l1_cb[i] +
716
0
                    ((shift * ps_wt_ofst->i2_chroma_weight_l1_cb[i]) >> chroma_log2_weight_denom) -
717
0
                    shift;
718
0
                delta_chroma_offset_l1[1] =
719
0
                    ps_wt_ofst->i2_chroma_offset_l1_cr[i] +
720
0
                    ((shift * ps_wt_ofst->i2_chroma_weight_l1_cr[i]) >> chroma_log2_weight_denom) -
721
0
                    shift;
722
723
                /* delta_chroma_weight_l1[ i ][j] */
724
0
                PUT_BITS_SEV(ps_bitstrm, delta_chroma_weight_l1[0], return_status);
725
0
                ENTROPY_TRACE("delta_chroma_weight_l1[ i ]", delta_chroma_weight_l1[0]);
726
727
                /* delta_chroma_offset_l1[ i ][j] */
728
0
                PUT_BITS_SEV(ps_bitstrm, delta_chroma_offset_l1[0], return_status);
729
0
                ENTROPY_TRACE("delta_chroma_offset_l1[ i ]", delta_chroma_offset_l1[0]);
730
731
                /* delta_chroma_weight_l1[ i ][j] */
732
0
                PUT_BITS_SEV(ps_bitstrm, delta_chroma_weight_l1[1], return_status);
733
0
                ENTROPY_TRACE("delta_chroma_weight_l1[ i ]", delta_chroma_weight_l1[1]);
734
735
                /* delta_chroma_offset_l1[ i ][j] */
736
0
                PUT_BITS_SEV(ps_bitstrm, delta_chroma_offset_l1[1], return_status);
737
0
                ENTROPY_TRACE("delta_chroma_offset_l1[ i ]", delta_chroma_offset_l1[1]);
738
0
            }
739
0
        }
740
0
    }
741
742
0
    return return_status;
743
0
}
744
745
/**
746
******************************************************************************
747
*
748
*  @brief Generates AUD (Access Unit Delimiter)
749
*
750
*  @par   Description
751
*  Generate Access Unit Delimiter as per Section 7.3.2.5
752
*
753
*  @param[in]   ps_bitstrm
754
*  pointer to bitstream context (handle)
755
*
756
*  @param[in]   pic_type
757
*  picture type
758
*
759
*  @return      success or failure error code
760
*
761
******************************************************************************
762
*/
763
WORD32 ihevce_generate_aud(bitstrm_t *ps_bitstrm, WORD32 pic_type)
764
0
{
765
0
    WORD32 return_status = IHEVCE_SUCCESS;
766
767
    /* Insert the NAL start code */
768
0
    return_status = ihevce_put_nal_start_code_prefix(ps_bitstrm, 1);
769
770
    /* Insert Nal Unit Header */
771
0
    return_status |= ihevce_generate_nal_unit_header(ps_bitstrm, NAL_AUD, 0);
772
773
    /* pic_type */
774
0
    PUT_BITS(ps_bitstrm, pic_type, 3, return_status);
775
0
    ENTROPY_TRACE("pic type", pic_type);
776
777
0
    ihevce_put_rbsp_trailing_bits(ps_bitstrm);
778
779
0
    return return_status;
780
0
}
781
782
/**
783
******************************************************************************
784
*
785
*  @brief Generates EOS (End of Sequence)
786
*
787
*  @par   Description
788
*  Generate End of sequence as per Section 7.3.2.6
789
*
790
*  @param[in]   ps_bitstrm
791
*  pointer to bitstream context (handle)
792
*
793
*  @return      success or failure error code
794
*
795
******************************************************************************
796
*/
797
WORD32 ihevce_generate_eos(bitstrm_t *ps_bitstrm)
798
0
{
799
0
    WORD32 return_status = IHEVCE_SUCCESS;
800
801
    /* Insert the NAL start code */
802
0
    return_status = ihevce_put_nal_start_code_prefix(ps_bitstrm, 1);
803
804
    /* Insert Nal Unit Header */
805
0
    return_status |= ihevce_generate_nal_unit_header(ps_bitstrm, NAL_EOS, 0);
806
807
0
    ihevce_put_rbsp_trailing_bits(ps_bitstrm);
808
809
0
    return return_status;
810
0
}
811
812
/**
813
******************************************************************************
814
*
815
*  @brief Generates VPS (Video Parameter Set)
816
*
817
*  @par   Description
818
*  Generate Video Parameter Set as per Section 7.3.2.1
819
*
820
*  @param[in]   ps_bitstrm
821
*  pointer to bitstream context (handle)
822
*
823
*  @param[in]   ps_vps
824
*  pointer to structure containing VPS data
825
*
826
*  @return      success or failure error code
827
*
828
******************************************************************************
829
*/
830
WORD32 ihevce_generate_vps(bitstrm_t *ps_bitstrm, vps_t *ps_vps)
831
1.14k
{
832
1.14k
    WORD32 i;
833
1.14k
    WORD8 i1_vps_max_sub_layers_minus1 = ps_vps->i1_vps_max_sub_layers - 1;
834
1.14k
    WORD32 return_status = IHEVCE_SUCCESS;
835
836
    /* Insert Start Code */
837
1.14k
    ihevce_put_nal_start_code_prefix(ps_bitstrm, 1);
838
839
    /* Insert Nal Unit Header */
840
1.14k
    ihevce_generate_nal_unit_header(ps_bitstrm, NAL_VPS, 0);
841
842
    /* video_parameter_set_id */
843
1.14k
    PUT_BITS(ps_bitstrm, ps_vps->i1_vps_id, 4, return_status);
844
1.14k
    ENTROPY_TRACE("video_parameter_set_id", ps_vps->i1_vps_id);
845
846
    /* vps_reserved_three_2bits */
847
1.14k
    PUT_BITS(ps_bitstrm, 3, 2, return_status);
848
1.14k
    ENTROPY_TRACE("vps_reserved_three_2bits", 3);
849
850
    /* vps_max_layers_minus1  */
851
1.14k
    PUT_BITS(ps_bitstrm, 0, 6, return_status);
852
1.14k
    ENTROPY_TRACE("vps_max_layers_minus1 ", 3);
853
854
    /* vps_max_sub_layers_minus1 */
855
1.14k
    PUT_BITS(ps_bitstrm, i1_vps_max_sub_layers_minus1, 3, return_status);
856
1.14k
    ENTROPY_TRACE("vps_max_sub_layers_minus1", i1_vps_max_sub_layers_minus1);
857
858
    /* vps_temporal_id_nesting_flag */
859
1.14k
    PUT_BITS(ps_bitstrm, ps_vps->i1_vps_temporal_id_nesting_flag, 1, return_status);
860
1.14k
    ENTROPY_TRACE("vps_temporal_id_nesting_flag", ps_vps->i1_vps_temporal_id_nesting_flag);
861
862
    /* vps_reserved_0xffff_16bits */
863
1.14k
    PUT_BITS(ps_bitstrm, 0xffff, 16, return_status);
864
1.14k
    ENTROPY_TRACE("vps_reserved_0xffff_16bits", 0xffff);
865
866
    /* profile-tier and level info */
867
1.14k
    ihevce_generate_profile_tier_level(ps_bitstrm, &ps_vps->s_ptl, 1, i1_vps_max_sub_layers_minus1);
868
869
    /* vps_sub_layer_ordering_info_present_flag */
870
1.14k
    PUT_BITS(ps_bitstrm, ps_vps->i1_sub_layer_ordering_info_present_flag, 1, return_status);
871
1.14k
    ENTROPY_TRACE(
872
1.14k
        "vps_sub_layer_ordering_info_present_flag",
873
1.14k
        ps_vps->i1_sub_layer_ordering_info_present_flag);
874
875
1.14k
    i = ps_vps->i1_sub_layer_ordering_info_present_flag ? 0 : i1_vps_max_sub_layers_minus1;
876
877
2.29k
    for(; i <= i1_vps_max_sub_layers_minus1; i++)
878
1.14k
    {
879
        /* vps_max_dec_pic_buffering[i] */
880
1.14k
        PUT_BITS_UEV(ps_bitstrm, ps_vps->ai1_vps_max_dec_pic_buffering[i], return_status);
881
1.14k
        ENTROPY_TRACE(
882
1.14k
            "vps_max_dec_pic_buffering_minus1[i]", ps_vps->ai1_vps_max_dec_pic_buffering[i]);
883
884
        /* vps_num_reorder_pics[i] */
885
1.14k
        PUT_BITS_UEV(ps_bitstrm, ps_vps->ai1_vps_max_num_reorder_pics[i], return_status);
886
1.14k
        ENTROPY_TRACE("ai1_vps_max_num_reorder_pics[i]", ps_vps->ai1_vps_max_num_reorder_pics[i]);
887
888
        /* vps_max_latency_increase[i] */
889
1.14k
        PUT_BITS_UEV(ps_bitstrm, ps_vps->ai1_vps_max_latency_increase[i], return_status);
890
1.14k
        ENTROPY_TRACE("ai1_vps_max_latency_increase[i]", ps_vps->ai1_vps_max_latency_increase[i]);
891
1.14k
    }
892
893
    /* vps_max_layer_id */
894
1.14k
    PUT_BITS(ps_bitstrm, ps_vps->i1_vps_max_nuh_reserved_zero_layer_id, 6, return_status);
895
1.14k
    ENTROPY_TRACE("vps_max_layer_id", ps_vps->i1_vps_max_nuh_reserved_zero_layer_id);
896
897
    /* vps_num_layer_sets_minus1 */
898
1.14k
    PUT_BITS_UEV(ps_bitstrm, 0, return_status);
899
1.14k
    ENTROPY_TRACE("vps_num_layer_sets_minus1", 0);
900
901
    /* vps_timing_info_present_flag */
902
1.14k
    PUT_BITS(ps_bitstrm, 0, 1, return_status);
903
1.14k
    ENTROPY_TRACE("vps_timing_info_present_flag", 0);
904
905
    /* vps_extension_flag */
906
1.14k
    PUT_BITS(ps_bitstrm, 0, 1, return_status);
907
1.14k
    ENTROPY_TRACE("vps_extension_flag", 0);
908
909
    /* rbsp trailing bits */
910
1.14k
    ihevce_put_rbsp_trailing_bits(ps_bitstrm);
911
912
1.14k
    return return_status;
913
1.14k
}
914
915
/**
916
******************************************************************************
917
*
918
*  @brief Generates SPS (Video Parameter Set)
919
*
920
*  @par   Description
921
*  Parse Video Parameter Set as per Section 7.3.2.2
922
*
923
*  @param[in]   ps_bitstrm
924
*  pointer to bitstream context (handle)
925
*
926
*  @param[in]   ps_sps
927
*  pointer to structure containing SPS data
928
*
929
*  @return      success or failure error code
930
*
931
******************************************************************************
932
*/
933
WORD32 ihevce_generate_sps(bitstrm_t *ps_bitstrm, sps_t *ps_sps)
934
1.14k
{
935
1.14k
    WORD32 i;
936
1.14k
    WORD32 return_status = IHEVCE_SUCCESS;
937
1.14k
    WORD8 i1_max_sub_layers_minus1 = ps_sps->i1_sps_max_sub_layers - 1;
938
939
1.14k
    UWORD32 u4_log2_max_pic_order_cnt_lsb = (UWORD32)(ps_sps->i1_log2_max_pic_order_cnt_lsb);
940
941
1.14k
    UWORD32 u4_log2_min_coding_block_size_minus3 =
942
1.14k
        (UWORD32)(ps_sps->i1_log2_min_coding_block_size) - 3;
943
944
1.14k
    UWORD32 u4_log2_diff_max_min_coding_block_size =
945
1.14k
        (UWORD32)(ps_sps->i1_log2_diff_max_min_coding_block_size);
946
947
1.14k
    UWORD32 u4_log2_min_transform_block_size_minus2 =
948
1.14k
        (UWORD32)(ps_sps->i1_log2_min_transform_block_size) - 2;
949
950
1.14k
    UWORD32 u4_log2_diff_max_min_transform_block_size =
951
1.14k
        (UWORD32)(ps_sps->i1_log2_diff_max_min_transform_block_size);
952
953
    /* Insert Start Code */
954
1.14k
    return_status = ihevce_put_nal_start_code_prefix(ps_bitstrm, 1);
955
956
    /* Insert Nal Unit Header */
957
1.14k
    return_status |= ihevce_generate_nal_unit_header(ps_bitstrm, NAL_SPS, 0);
958
959
    /* video_parameter_set_id */
960
1.14k
    PUT_BITS(ps_bitstrm, ps_sps->i1_vps_id, 4, return_status);
961
1.14k
    ENTROPY_TRACE("video_parameter_set_id", ps_sps->i1_vps_id);
962
963
    /* sps_max_sub_layers_minus1 */
964
1.14k
    PUT_BITS(ps_bitstrm, i1_max_sub_layers_minus1, 3, return_status);
965
1.14k
    ENTROPY_TRACE("sps_max_sub_layers_minus1", i1_max_sub_layers_minus1);
966
967
    /* sps_temporal_id_nesting_flag */
968
1.14k
    PUT_BITS(ps_bitstrm, ps_sps->i1_sps_temporal_id_nesting_flag, 1, return_status);
969
1.14k
    ENTROPY_TRACE("sps_temporal_id_nesting_flag", ps_sps->i1_sps_temporal_id_nesting_flag);
970
971
    /* profile-tier and level info */
972
1.14k
    ihevce_generate_profile_tier_level(ps_bitstrm, &ps_sps->s_ptl, 1, i1_max_sub_layers_minus1);
973
974
    /* seq_parameter_set_id */
975
1.14k
    PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_sps_id, return_status);
976
1.14k
    ENTROPY_TRACE("seq_parameter_set_id", ps_sps->i1_sps_id);
977
978
    /* chroma_format_idc */
979
1.14k
    PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_chroma_format_idc, return_status);
980
1.14k
    ENTROPY_TRACE("chroma_format_idc", ps_sps->i1_chroma_format_idc);
981
982
1.14k
    if(CHROMA_FMT_IDC_YUV444 == ps_sps->i1_chroma_format_idc)
983
0
    {
984
        /* separate_colour_plane_flag */
985
0
        PUT_BITS(ps_bitstrm, 1, 1, return_status);
986
0
        ENTROPY_TRACE("separate_colour_plane_flag", 1);
987
0
    }
988
989
    /* pic_width_in_luma_samples */
990
1.14k
    PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_width_in_luma_samples, return_status);
991
1.14k
    ENTROPY_TRACE("pic_width_in_luma_samples", ps_sps->i2_pic_width_in_luma_samples);
992
993
    /* pic_height_in_luma_samples */
994
1.14k
    PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_height_in_luma_samples, return_status);
995
1.14k
    ENTROPY_TRACE("pic_height_in_luma_samples", ps_sps->i2_pic_height_in_luma_samples);
996
997
    /* pic_cropping_flag */
998
1.14k
    PUT_BITS(ps_bitstrm, ps_sps->i1_pic_cropping_flag, 1, return_status);
999
1.14k
    ENTROPY_TRACE("pic_cropping_flag", ps_sps->i1_pic_cropping_flag);
1000
1001
1.14k
    if(ps_sps->i1_pic_cropping_flag)
1002
1.14k
    {
1003
        /* pic_crop_left_offset */
1004
1.14k
        PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_crop_left_offset, return_status);
1005
1.14k
        ENTROPY_TRACE("pic_crop_left_offset", ps_sps->i2_pic_crop_left_offset);
1006
1007
        /* pic_crop_right_offset */
1008
1.14k
        PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_crop_right_offset, return_status);
1009
1.14k
        ENTROPY_TRACE("pic_crop_right_offset", ps_sps->i2_pic_crop_right_offset);
1010
1011
        /* pic_crop_top_offset */
1012
1.14k
        PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_crop_top_offset, return_status);
1013
1.14k
        ENTROPY_TRACE("pic_crop_top_offset", ps_sps->i2_pic_crop_top_offset);
1014
1015
        /* pic_crop_bottom_offset */
1016
1.14k
        PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_crop_bottom_offset, return_status);
1017
1.14k
        ENTROPY_TRACE("pic_crop_bottom_offset", ps_sps->i2_pic_crop_bottom_offset);
1018
1.14k
    }
1019
1020
    /* bit_depth_luma_minus8 */
1021
1.14k
    PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_bit_depth_luma_minus8, return_status);
1022
1.14k
    ENTROPY_TRACE("bit_depth_luma_minus8", ps_sps->i1_bit_depth_luma_minus8);
1023
1024
    /* bit_depth_chroma_minus8 */
1025
1.14k
    PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_bit_depth_chroma_minus8, return_status);
1026
1.14k
    ENTROPY_TRACE("i1_bit_depth_chroma_minus8", ps_sps->i1_bit_depth_chroma_minus8);
1027
1028
    /* log2_max_pic_order_cnt_lsb_minus4 */
1029
1.14k
    PUT_BITS_UEV(ps_bitstrm, u4_log2_max_pic_order_cnt_lsb - 4, return_status);
1030
1.14k
    ENTROPY_TRACE("log2_max_pic_order_cnt_lsb_minus4", u4_log2_max_pic_order_cnt_lsb - 4);
1031
1032
    /* sps_sub_layer_ordering_info_present_flag */
1033
1.14k
    PUT_BITS(ps_bitstrm, ps_sps->i1_sps_sub_layer_ordering_info_present_flag, 1, return_status);
1034
1.14k
    ENTROPY_TRACE(
1035
1.14k
        "sps_sub_layer_ordering_info_present_flag",
1036
1.14k
        ps_sps->i1_sps_sub_layer_ordering_info_present_flag);
1037
1038
1.14k
    i = ps_sps->i1_sps_sub_layer_ordering_info_present_flag ? 0 : i1_max_sub_layers_minus1;
1039
1040
2.29k
    for(; i <= i1_max_sub_layers_minus1; i++)
1041
1.14k
    {
1042
        /* max_dec_pic_buffering */
1043
1.14k
        PUT_BITS_UEV(ps_bitstrm, ps_sps->ai1_sps_max_dec_pic_buffering[i], return_status);
1044
1.14k
        ENTROPY_TRACE("max_dec_pic_buffering_minus1", ps_sps->ai1_sps_max_dec_pic_buffering[i]);
1045
1046
        /* num_reorder_pics */
1047
1.14k
        PUT_BITS_UEV(ps_bitstrm, ps_sps->ai1_sps_max_num_reorder_pics[i], return_status);
1048
1.14k
        ENTROPY_TRACE("num_reorder_pics", ps_sps->ai1_sps_max_num_reorder_pics[i]);
1049
1050
        /* max_latency_increase */
1051
1.14k
        PUT_BITS_UEV(ps_bitstrm, ps_sps->ai1_sps_max_latency_increase[i], return_status);
1052
1.14k
        ENTROPY_TRACE("max_latency_increase", ps_sps->ai1_sps_max_latency_increase[i]);
1053
1.14k
    }
1054
1055
    /* log2_min_coding_block_size_minus3 */
1056
1.14k
    PUT_BITS_UEV(ps_bitstrm, u4_log2_min_coding_block_size_minus3, return_status);
1057
1.14k
    ENTROPY_TRACE("log2_min_coding_block_size_minus3", u4_log2_min_coding_block_size_minus3);
1058
1059
    /* log2_diff_max_min_coding_block_size */
1060
1.14k
    PUT_BITS_UEV(ps_bitstrm, u4_log2_diff_max_min_coding_block_size, return_status);
1061
1.14k
    ENTROPY_TRACE("log2_diff_max_min_coding_block_size", u4_log2_diff_max_min_coding_block_size);
1062
1063
    /* log2_min_transform_block_size_minus2 */
1064
1.14k
    PUT_BITS_UEV(ps_bitstrm, u4_log2_min_transform_block_size_minus2, return_status);
1065
1.14k
    ENTROPY_TRACE("log2_min_transform_block_size_minus2", u4_log2_min_transform_block_size_minus2);
1066
1067
    /* log2_diff_max_min_transform_block_size */
1068
1.14k
    PUT_BITS_UEV(ps_bitstrm, u4_log2_diff_max_min_transform_block_size, return_status);
1069
1.14k
    ENTROPY_TRACE(
1070
1.14k
        "log2_diff_max_min_transform_block_size", u4_log2_diff_max_min_transform_block_size);
1071
1072
    /* max_transform_hierarchy_depth_inter */
1073
1.14k
    PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_max_transform_hierarchy_depth_inter, return_status);
1074
1.14k
    ENTROPY_TRACE(
1075
1.14k
        "max_transform_hierarchy_depth_inter", ps_sps->i1_max_transform_hierarchy_depth_inter);
1076
1077
    /* max_transform_hierarchy_depth_intra */
1078
1.14k
    PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_max_transform_hierarchy_depth_intra, return_status);
1079
1.14k
    ENTROPY_TRACE(
1080
1.14k
        "max_transform_hierarchy_depth_intra", ps_sps->i1_max_transform_hierarchy_depth_intra);
1081
1082
    /* scaling_list_enabled_flag */
1083
1.14k
    PUT_BITS(ps_bitstrm, ps_sps->i1_scaling_list_enable_flag, 1, return_status);
1084
1.14k
    ENTROPY_TRACE("scaling_list_enabled_flag", ps_sps->i1_scaling_list_enable_flag);
1085
1086
1.14k
    if(ps_sps->i1_scaling_list_enable_flag)
1087
495
    {
1088
        /* sps_scaling_list_data_present_flag */
1089
495
        PUT_BITS(ps_bitstrm, ps_sps->i1_sps_scaling_list_data_present_flag, 1, return_status);
1090
495
        ENTROPY_TRACE(
1091
495
            "sps_scaling_list_data_present_flag", ps_sps->i1_sps_scaling_list_data_present_flag);
1092
1093
#if 0 /* TODO: Will be enabled once scaling list support is added */
1094
        if(ps_sps->i1_sps_scaling_list_data_present_flag)
1095
        {
1096
            //TODO
1097
            ihevce_generate_scaling_list_data(ps_bitstrm);
1098
        }
1099
#endif
1100
495
    }
1101
1102
    /* asymmetric_motion_partitions_enabled_flag */
1103
1.14k
    PUT_BITS(ps_bitstrm, ps_sps->i1_amp_enabled_flag, 1, return_status);
1104
1.14k
    ENTROPY_TRACE("asymmetric_motion_partitions_enabled_flag", ps_sps->i1_amp_enabled_flag);
1105
1106
    /* sample_adaptive_offset_enabled_flag */
1107
1.14k
    PUT_BITS(ps_bitstrm, ps_sps->i1_sample_adaptive_offset_enabled_flag, 1, return_status);
1108
1.14k
    ENTROPY_TRACE(
1109
1.14k
        "sample_adaptive_offset_enabled_flag", ps_sps->i1_sample_adaptive_offset_enabled_flag);
1110
1111
    /* pcm_enabled_flag */
1112
1.14k
    PUT_BITS(ps_bitstrm, ps_sps->i1_pcm_enabled_flag, 1, return_status);
1113
1.14k
    ENTROPY_TRACE("pcm_enabled_flag", ps_sps->i1_pcm_enabled_flag);
1114
1.14k
    if(ps_sps->i1_pcm_enabled_flag)
1115
0
    {
1116
0
        UWORD32 u4_log2_min_pcm_coding_block_size = (ps_sps->i1_log2_min_pcm_coding_block_size);
1117
0
        UWORD32 u4_log2_diff_max_min_pcm_coding_block_size =
1118
0
            (ps_sps->i1_log2_diff_max_min_pcm_coding_block_size);
1119
1120
        /* pcm_sample_bit_depth_luma_minus1 */
1121
0
        PUT_BITS(ps_bitstrm, ps_sps->i1_pcm_sample_bit_depth_luma - 1, 4, return_status);
1122
0
        ENTROPY_TRACE("pcm_sample_bit_depth_luma", ps_sps->i1_pcm_sample_bit_depth_luma - 1);
1123
1124
        /* pcm_sample_bit_depth_chroma_minus1 */
1125
0
        PUT_BITS(ps_bitstrm, ps_sps->i1_pcm_sample_bit_depth_chroma - 1, 4, return_status);
1126
0
        ENTROPY_TRACE("pcm_sample_bit_depth_chroma", ps_sps->i1_pcm_sample_bit_depth_chroma - 1);
1127
1128
        /* log2_min_pcm_coding_block_size_minus3 */
1129
0
        PUT_BITS_UEV(ps_bitstrm, u4_log2_min_pcm_coding_block_size - 3, return_status);
1130
0
        ENTROPY_TRACE(
1131
0
            "log2_min_pcm_coding_block_size_minus3", u4_log2_min_pcm_coding_block_size - 3);
1132
1133
        /* log2_diff_max_min_pcm_coding_block_size */
1134
0
        PUT_BITS_UEV(ps_bitstrm, u4_log2_diff_max_min_pcm_coding_block_size, return_status);
1135
0
        ENTROPY_TRACE(
1136
0
            "log2_diff_max_min_pcm_coding_block_size", u4_log2_diff_max_min_pcm_coding_block_size);
1137
1138
        /* pcm_loop_filter_disable_flag */
1139
0
        PUT_BITS(ps_bitstrm, ps_sps->i1_pcm_loop_filter_disable_flag, 1, return_status);
1140
0
        ENTROPY_TRACE("pcm_loop_filter_disable_flag", ps_sps->i1_pcm_loop_filter_disable_flag);
1141
0
    }
1142
1143
    /* num_short_term_ref_pic_sets */
1144
1.14k
    PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_num_short_term_ref_pic_sets, return_status);
1145
1.14k
    ENTROPY_TRACE("num_short_term_ref_pic_sets", ps_sps->i1_num_short_term_ref_pic_sets);
1146
1147
1.14k
    for(i = 0; i < ps_sps->i1_num_short_term_ref_pic_sets; i++)
1148
0
    {
1149
0
        WORD32 i4_NumPocTotalCurr = 0;
1150
0
        ihevce_short_term_ref_pic_set(
1151
0
            ps_bitstrm,
1152
0
            &ps_sps->as_stref_picset[0],
1153
0
            ps_sps->i1_num_short_term_ref_pic_sets,
1154
0
            i,
1155
0
            &i4_NumPocTotalCurr);
1156
0
    }
1157
1158
    /* long_term_ref_pics_present_flag */
1159
1.14k
    PUT_BITS(ps_bitstrm, ps_sps->i1_long_term_ref_pics_present_flag, 1, return_status);
1160
1.14k
    ENTROPY_TRACE("long_term_ref_pics_present_flag", ps_sps->i1_long_term_ref_pics_present_flag);
1161
1162
1.14k
    if(ps_sps->i1_long_term_ref_pics_present_flag)
1163
0
    {
1164
        /* num_long_term_ref_pics_sps */
1165
0
        PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_num_long_term_ref_pics_sps, return_status);
1166
0
        ENTROPY_TRACE("num_long_term_ref_pics_sps", ps_sps->i1_num_long_term_ref_pics_sps);
1167
1168
0
        for(i = 0; i < ps_sps->i1_num_long_term_ref_pics_sps; i++)
1169
0
        {
1170
            /* lt_ref_pic_poc_lsb_sps[i] */
1171
0
            PUT_BITS(
1172
0
                ps_bitstrm,
1173
0
                ps_sps->au2_lt_ref_pic_poc_lsb_sps[i],
1174
0
                u4_log2_max_pic_order_cnt_lsb,
1175
0
                return_status);
1176
0
            ENTROPY_TRACE("lt_ref_pic_poc_lsb_sps[i]", ps_sps->au2_lt_ref_pic_poc_lsb_sps[i]);
1177
1178
            /* used_by_curr_pic_lt_sps_flag[i] */
1179
0
            PUT_BITS(ps_bitstrm, ps_sps->ai1_used_by_curr_pic_lt_sps_flag[i], 1, return_status);
1180
0
            ENTROPY_TRACE(
1181
0
                "used_by_curr_pic_lt_sps_flag[i]", ps_sps->ai1_used_by_curr_pic_lt_sps_flag[i]);
1182
0
        }
1183
0
    }
1184
1185
    /* sps_temporal_mvp_enable_flag */
1186
1.14k
    PUT_BITS(ps_bitstrm, ps_sps->i1_sps_temporal_mvp_enable_flag, 1, return_status);
1187
1.14k
    ENTROPY_TRACE("sps_temporal_mvp_enable_flag", ps_sps->i1_sps_temporal_mvp_enable_flag);
1188
1189
1.14k
#if !HM_8DOT1_SYNTAX
1190
    /* strong_intra_smoothing_enable_flag */
1191
1.14k
    PUT_BITS(ps_bitstrm, ps_sps->i1_strong_intra_smoothing_enable_flag, 1, return_status);
1192
1.14k
    ENTROPY_TRACE(
1193
1.14k
        "sps_strong_intra_smoothing_enable_flag", ps_sps->i1_strong_intra_smoothing_enable_flag);
1194
1.14k
#endif
1195
1196
    /* vui_parameters_present_flag */
1197
1.14k
    PUT_BITS(ps_bitstrm, ps_sps->i1_vui_parameters_present_flag, 1, return_status);
1198
1.14k
    ENTROPY_TRACE("vui_parameters_present_flag", ps_sps->i1_vui_parameters_present_flag);
1199
1200
1.14k
    ENTROPY_TRACE("----------- vui_parameters -----------", 0);
1201
1202
1.14k
    if(ps_sps->i1_vui_parameters_present_flag)
1203
458
    {
1204
        /* Add vui parameters to the bitstream */
1205
458
        ihevce_generate_vui(ps_bitstrm, ps_sps, ps_sps->s_vui_parameters);
1206
458
    }
1207
1208
    /* sps_extension_flag */
1209
1.14k
    PUT_BITS(ps_bitstrm, 0, 1, return_status);
1210
1.14k
    ENTROPY_TRACE("sps_extension_flag", 0);
1211
1212
    /* rbsp trailing bits */
1213
1.14k
    ihevce_put_rbsp_trailing_bits(ps_bitstrm);
1214
1215
1.14k
    return return_status;
1216
1.14k
}
1217
1218
/**
1219
******************************************************************************
1220
*
1221
*  @brief Generates PPS (Picture Parameter Set)
1222
*
1223
*  @par   Description
1224
*  Generate Picture Parameter Set as per Section 7.3.2.3
1225
*
1226
*  @param[in]   ps_bitstrm
1227
*  pointer to bitstream context (handle)
1228
*
1229
*  @param[in]   ps_pps
1230
*  pointer to structure containing PPS data
1231
*
1232
*  @return      success or failure error code
1233
*
1234
******************************************************************************
1235
*/
1236
WORD32 ihevce_generate_pps(bitstrm_t *ps_bitstrm, pps_t *ps_pps)
1237
1.14k
{
1238
1.14k
    WORD32 i;
1239
1.14k
    WORD32 return_status = IHEVCE_SUCCESS;
1240
1241
    /* Insert the NAL start code */
1242
1.14k
    return_status = ihevce_put_nal_start_code_prefix(ps_bitstrm, 1);
1243
1244
    /* Insert Nal Unit Header */
1245
1.14k
    return_status |= ihevce_generate_nal_unit_header(ps_bitstrm, NAL_PPS, 0);
1246
1247
    /* pic_parameter_set_id */
1248
1.14k
    PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_pps_id, return_status);
1249
1.14k
    ENTROPY_TRACE("pic_parameter_set_id", ps_pps->i1_pps_id);
1250
1251
    /* seq_parameter_set_id */
1252
1.14k
    PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_sps_id, return_status);
1253
1.14k
    ENTROPY_TRACE("seq_parameter_set_id", ps_pps->i1_sps_id);
1254
1255
    /* dependent_slices_enabled_flag */
1256
1.14k
    PUT_BITS(ps_bitstrm, ps_pps->i1_dependent_slice_enabled_flag, 1, return_status);
1257
1.14k
    ENTROPY_TRACE("dependent_slices_enabled_flag", ps_pps->i1_dependent_slice_enabled_flag);
1258
1259
    /* output_flag_present_flag */
1260
1.14k
    PUT_BITS(ps_bitstrm, ps_pps->i1_output_flag_present_flag, 1, return_status);
1261
1.14k
    ENTROPY_TRACE("output_flag_present_flag", ps_pps->i1_output_flag_present_flag);
1262
1263
    /* num_extra_slice_header_bits */
1264
1.14k
    PUT_BITS(ps_bitstrm, ps_pps->i1_num_extra_slice_header_bits, 3, return_status);
1265
1.14k
    ENTROPY_TRACE("num_extra_slice_header_bits", ps_pps->i1_num_extra_slice_header_bits);
1266
1267
    /* sign_data_hiding_flag */
1268
1.14k
    PUT_BITS(ps_bitstrm, ps_pps->i1_sign_data_hiding_flag, 1, return_status);
1269
1.14k
    ENTROPY_TRACE("sign_data_hiding_flag", ps_pps->i1_sign_data_hiding_flag);
1270
1271
    /* cabac_init_present_flag */
1272
1.14k
    PUT_BITS(ps_bitstrm, ps_pps->i1_cabac_init_present_flag, 1, return_status);
1273
1.14k
    ENTROPY_TRACE("cabac_init_present_flag", ps_pps->i1_cabac_init_present_flag);
1274
1275
    /* num_ref_idx_l0_default_active_minus1 */
1276
1.14k
    PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_num_ref_idx_l0_default_active - 1, return_status);
1277
1.14k
    ENTROPY_TRACE(
1278
1.14k
        "num_ref_idx_l0_default_active_minus1", ps_pps->i1_num_ref_idx_l0_default_active - 1);
1279
1280
    /* num_ref_idx_l1_default_active_minus1 */
1281
1.14k
    PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_num_ref_idx_l1_default_active - 1, return_status);
1282
1.14k
    ENTROPY_TRACE(
1283
1.14k
        "num_ref_idx_l1_default_active_minus1", ps_pps->i1_num_ref_idx_l1_default_active - 1);
1284
1285
    /* pic_init_qp_minus26 */
1286
1.14k
    PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_pic_init_qp - 26, return_status);
1287
1.14k
    ENTROPY_TRACE("pic_init_qp_minus26", ps_pps->i1_pic_init_qp - 26);
1288
1289
    /* constrained_intra_pred_flag */
1290
1.14k
    PUT_BITS(ps_bitstrm, ps_pps->i1_constrained_intra_pred_flag, 1, return_status);
1291
1.14k
    ENTROPY_TRACE("constrained_intra_pred_flag", ps_pps->i1_constrained_intra_pred_flag);
1292
1293
    /* transform_skip_enabled_flag */
1294
1.14k
    PUT_BITS(ps_bitstrm, ps_pps->i1_transform_skip_enabled_flag, 1, return_status);
1295
1.14k
    ENTROPY_TRACE("transform_skip_enabled_flag", ps_pps->i1_transform_skip_enabled_flag);
1296
1297
    /* cu_qp_delta_enabled_flag */
1298
1.14k
    PUT_BITS(ps_bitstrm, ps_pps->i1_cu_qp_delta_enabled_flag, 1, return_status);
1299
1.14k
    ENTROPY_TRACE("cu_qp_delta_enabled_flag", ps_pps->i1_cu_qp_delta_enabled_flag);
1300
1301
1.14k
    if(ps_pps->i1_cu_qp_delta_enabled_flag)
1302
340
    {
1303
        /* diff_cu_qp_delta_depth */
1304
340
        PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_diff_cu_qp_delta_depth, return_status);
1305
340
        ENTROPY_TRACE("diff_cu_qp_delta_depth", ps_pps->i1_diff_cu_qp_delta_depth);
1306
340
    }
1307
1308
    /* cb_qp_offset */
1309
1.14k
    PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_pic_cb_qp_offset, return_status);
1310
1.14k
    ENTROPY_TRACE("cb_qp_offset", ps_pps->i1_pic_cb_qp_offset);
1311
1312
    /* cr_qp_offset */
1313
1.14k
    PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_pic_cr_qp_offset, return_status);
1314
1.14k
    ENTROPY_TRACE("cr_qp_offset", ps_pps->i1_pic_cr_qp_offset);
1315
1316
    /* slicelevel_chroma_qp_flag */
1317
1.14k
    PUT_BITS(
1318
1.14k
        ps_bitstrm, ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag, 1, return_status);
1319
1.14k
    ENTROPY_TRACE(
1320
1.14k
        "slicelevel_chroma_qp_flag", ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag);
1321
1322
    /* weighted_pred_flag */
1323
1.14k
    PUT_BITS(ps_bitstrm, ps_pps->i1_weighted_pred_flag, 1, return_status);
1324
1.14k
    ENTROPY_TRACE("weighted_pred_flag", ps_pps->i1_weighted_pred_flag);
1325
1326
    /* weighted_bipred_flag */
1327
1.14k
    PUT_BITS(ps_bitstrm, ps_pps->i1_weighted_bipred_flag, 1, return_status);
1328
1.14k
    ENTROPY_TRACE("weighted_bipred_flag", ps_pps->i1_weighted_bipred_flag);
1329
1330
    /* transquant_bypass_enable_flag */
1331
1.14k
    PUT_BITS(ps_bitstrm, ps_pps->i1_transquant_bypass_enable_flag, 1, return_status);
1332
1.14k
    ENTROPY_TRACE("transquant_bypass_enable_flag", ps_pps->i1_transquant_bypass_enable_flag);
1333
1334
    /* tiles_enabled_flag */
1335
1.14k
    PUT_BITS(ps_bitstrm, ps_pps->i1_tiles_enabled_flag, 1, return_status);
1336
1.14k
    ENTROPY_TRACE("tiles_enabled_flag", ps_pps->i1_tiles_enabled_flag);
1337
1338
    /* entropy_coding_sync_enabled_flag */
1339
1.14k
    PUT_BITS(ps_bitstrm, ps_pps->i1_entropy_coding_sync_enabled_flag, 1, return_status);
1340
1.14k
    ENTROPY_TRACE("entropy_coding_sync_enabled_flag", ps_pps->i1_entropy_coding_sync_enabled_flag);
1341
1342
1.14k
    if(ps_pps->i1_tiles_enabled_flag)
1343
0
    {
1344
        /* num_tile_columns_minus1 */
1345
0
        PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_num_tile_columns - 1, return_status);
1346
0
        ENTROPY_TRACE("num_tile_columns_minus1", ps_pps->i1_num_tile_columns - 1);
1347
1348
        /* num_tile_rows_minus1 */
1349
0
        PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_num_tile_rows - 1, return_status);
1350
0
        ENTROPY_TRACE("num_tile_rows_minus1", ps_pps->i1_num_tile_rows - 1);
1351
1352
        /* uniform_spacing_flag */
1353
0
        PUT_BITS(ps_bitstrm, ps_pps->i1_uniform_spacing_flag, 1, return_status);
1354
0
        ENTROPY_TRACE("uniform_spacing_flag", ps_pps->i1_uniform_spacing_flag);
1355
1356
0
        if(!ps_pps->i1_uniform_spacing_flag)
1357
0
        {
1358
0
            for(i = 0; i < ps_pps->i1_num_tile_columns - 1; i++)
1359
0
            {
1360
                /* column_width_minus1[i] */
1361
0
                PUT_BITS_UEV(ps_bitstrm, ps_pps->ps_tile[i].u2_wd - 1, return_status);
1362
0
                ENTROPY_TRACE("column_width_minus1[i]", ps_pps->ps_tile[i].u2_wd - 1);
1363
0
            }
1364
0
            for(i = 0; i < ps_pps->i1_num_tile_rows - 1; i++)
1365
0
            {
1366
                /* row_height_minus1[i] */
1367
0
                PUT_BITS_UEV(ps_bitstrm, ps_pps->ps_tile[i].u2_ht - 1, return_status);
1368
0
                ENTROPY_TRACE("row_height_minus1[i]", ps_pps->ps_tile[i].u2_ht - 1);
1369
0
            }
1370
0
        }
1371
1372
        /* loop_filter_across_tiles_enabled_flag */
1373
0
        PUT_BITS(ps_bitstrm, ps_pps->i1_loop_filter_across_tiles_enabled_flag, 1, return_status);
1374
0
        ENTROPY_TRACE(
1375
0
            "loop_filter_across_tiles_enabled_flag",
1376
0
            ps_pps->i1_loop_filter_across_tiles_enabled_flag);
1377
0
    }
1378
1379
    /* loop_filter_across_slices_enabled_flag */
1380
1.14k
    PUT_BITS(ps_bitstrm, ps_pps->i1_loop_filter_across_slices_enabled_flag, 1, return_status);
1381
1.14k
    ENTROPY_TRACE(
1382
1.14k
        "loop_filter_across_slices_enabled_flag",
1383
1.14k
        ps_pps->i1_loop_filter_across_slices_enabled_flag);
1384
1385
    /* deblocking_filter_control_present_flag */
1386
1.14k
    PUT_BITS(ps_bitstrm, ps_pps->i1_deblocking_filter_control_present_flag, 1, return_status);
1387
1.14k
    ENTROPY_TRACE(
1388
1.14k
        "deblocking_filter_control_present_flag",
1389
1.14k
        ps_pps->i1_deblocking_filter_control_present_flag);
1390
1391
1.14k
    if(ps_pps->i1_deblocking_filter_control_present_flag)
1392
1.14k
    {
1393
        /* deblocking_filter_override_enabled_flag */
1394
1.14k
        PUT_BITS(ps_bitstrm, ps_pps->i1_deblocking_filter_override_enabled_flag, 1, return_status);
1395
1.14k
        ENTROPY_TRACE(
1396
1.14k
            "deblocking_filter_override_enabled_flag",
1397
1.14k
            ps_pps->i1_deblocking_filter_override_enabled_flag);
1398
1399
        /* pic_disable_deblocking_filter_flag */
1400
1.14k
        PUT_BITS(ps_bitstrm, ps_pps->i1_pic_disable_deblocking_filter_flag, 1, return_status);
1401
1.14k
        ENTROPY_TRACE(
1402
1.14k
            "pic_disable_deblocking_filter_flag", ps_pps->i1_pic_disable_deblocking_filter_flag);
1403
1404
1.14k
        if(!ps_pps->i1_pic_disable_deblocking_filter_flag)
1405
756
        {
1406
            /* beta_offset_div2 */
1407
756
            PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_beta_offset_div2 >> 1, return_status);
1408
756
            ENTROPY_TRACE("beta_offset_div2", ps_pps->i1_beta_offset_div2 >> 1);
1409
1410
            /* tc_offset_div2 */
1411
756
            PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_tc_offset_div2 >> 1, return_status);
1412
756
            ENTROPY_TRACE("tc_offset_div2", ps_pps->i1_tc_offset_div2 >> 1);
1413
756
        }
1414
1.14k
    }
1415
1416
    /* pps_scaling_list_data_present_flag */
1417
1.14k
    PUT_BITS(ps_bitstrm, ps_pps->i1_pps_scaling_list_data_present_flag, 1, return_status);
1418
1.14k
    ENTROPY_TRACE(
1419
1.14k
        "pps_scaling_list_data_present_flag", ps_pps->i1_pps_scaling_list_data_present_flag);
1420
1421
#if 0 /* TODO: Will be enabled once scaling list support is added */
1422
    if(ps_pps->i1_pps_scaling_list_data_present_flag )
1423
    {
1424
        //TODO
1425
        ihevce_scaling_list_data();
1426
    }
1427
#endif
1428
1429
    /* lists_modification_present_flag */
1430
1.14k
    PUT_BITS(ps_bitstrm, ps_pps->i1_lists_modification_present_flag, 1, return_status);
1431
1.14k
    ENTROPY_TRACE("lists_modification_present_flag", ps_pps->i1_lists_modification_present_flag);
1432
1433
1.14k
    {
1434
1.14k
        UWORD32 u4_log2_parallel_merge_level_minus2 = ps_pps->i1_log2_parallel_merge_level;
1435
1436
1.14k
        u4_log2_parallel_merge_level_minus2 -= 2;
1437
1438
        /* log2_parallel_merge_level_minus2 */
1439
1.14k
        PUT_BITS_UEV(ps_bitstrm, u4_log2_parallel_merge_level_minus2, return_status);
1440
1.14k
        ENTROPY_TRACE("log2_parallel_merge_level_minus2", u4_log2_parallel_merge_level_minus2);
1441
1.14k
    }
1442
1443
    /* slice_header_extension_present_flag */
1444
1.14k
    PUT_BITS(ps_bitstrm, ps_pps->i1_slice_header_extension_present_flag, 1, return_status);
1445
1.14k
    ENTROPY_TRACE(
1446
1.14k
        "slice_header_extension_present_flag", ps_pps->i1_slice_header_extension_present_flag);
1447
1448
    /* pps_extension_flag */
1449
1.14k
    PUT_BITS(ps_bitstrm, 0, 1, return_status);
1450
1.14k
    ENTROPY_TRACE("pps_extension_flag", 0);
1451
1452
1.14k
    ihevce_put_rbsp_trailing_bits(ps_bitstrm);
1453
1454
1.14k
    return return_status;
1455
1.14k
}
1456
1457
/**
1458
******************************************************************************
1459
*
1460
*  @brief Generates Slice Header
1461
*
1462
*  @par   Description
1463
*  Generate Slice Header as per Section 7.3.5.1
1464
*
1465
*  @param[inout]   ps_bitstrm
1466
*  pointer to bitstream context for generating slice header
1467
*
1468
*  @param[in]   i1_nal_unit_type
1469
*  nal unit type
1470
*
1471
*  @param[in]   ps_slice_hdr
1472
*  pointer to slice header params
1473
*
1474
*  @param[in]   ps_pps
1475
*  pointer to pps params referred by slice
1476
*
1477
*  @param[in]   ps_sps
1478
*  pointer to sps params referred by slice
1479
*
1480
*  @return      success or failure error code
1481
*
1482
******************************************************************************
1483
*/
1484
WORD32 ihevce_generate_slice_header(
1485
    bitstrm_t *ps_bitstrm,
1486
    WORD8 i1_nal_unit_type,
1487
    slice_header_t *ps_slice_hdr,
1488
    pps_t *ps_pps,
1489
    sps_t *ps_sps,
1490
    bitstrm_t *ps_dup_bit_strm_ent_offset,
1491
    UWORD32 *pu4_first_slice_start_offset,
1492
    ihevce_tile_params_t *ps_tile_params,
1493
    WORD32 i4_next_slice_seg_x,
1494
    WORD32 i4_next_slice_seg_y)
1495
6.25k
{
1496
6.25k
    WORD32 i;
1497
6.25k
    WORD32 return_status = IHEVCE_SUCCESS;
1498
1499
6.25k
    WORD32 RapPicFlag = (i1_nal_unit_type >= NAL_BLA_W_LP) &&
1500
957
                        (i1_nal_unit_type <= NAL_RSV_RAP_VCL23);
1501
6.25k
    WORD32 idr_pic_flag = (NAL_IDR_W_LP == i1_nal_unit_type) || (NAL_IDR_N_LP == i1_nal_unit_type);
1502
1503
6.25k
    WORD32 disable_deblocking_filter_flag;
1504
1505
6.25k
    WORD32 i4_NumPocTotalCurr = 0;
1506
    /* Initialize the pic width and pic height from sps parameters */
1507
6.25k
    WORD32 pic_width = ps_sps->i2_pic_width_in_luma_samples;
1508
6.25k
    WORD32 pic_height = ps_sps->i2_pic_height_in_luma_samples;
1509
1510
    /* Initialize the CTB size from sps parameters */
1511
6.25k
    WORD32 log2_ctb_size =
1512
6.25k
        ps_sps->i1_log2_min_coding_block_size + ps_sps->i1_log2_diff_max_min_coding_block_size;
1513
6.25k
    WORD32 ctb_size = (1 << log2_ctb_size);
1514
1515
    /* Update ps_slice_hdr->i2_slice_address based on tile position in frame */
1516
6.25k
    WORD32 num_ctb_in_row = (pic_width + ctb_size - 1) >> log2_ctb_size;
1517
1518
    /* Overwrite i2_slice_address here as pre-enc didn't had tile structure
1519
    available in it's scope. Otherwise i2_slice_address would be set in
1520
    populate_slice_header() itself */
1521
6.25k
    if(1 == ps_tile_params->i4_tiles_enabled_flag)
1522
0
    {
1523
0
        ps_slice_hdr->i2_slice_address =
1524
0
            ps_tile_params->i4_first_ctb_y * num_ctb_in_row + ps_tile_params->i4_first_ctb_x;
1525
0
    }
1526
6.25k
    else
1527
6.25k
    {
1528
6.25k
        ps_slice_hdr->i2_slice_address = i4_next_slice_seg_x + i4_next_slice_seg_y * num_ctb_in_row;
1529
6.25k
    }
1530
1531
    /* Overwrite i1_first_slice_in_pic_flag here as pre-enc didn't had tile structure
1532
    available in it's scope. Otherwise i1_first_slice_in_pic_flag would be set in
1533
    populate_slice_header() itself */
1534
6.25k
    ps_slice_hdr->i1_first_slice_in_pic_flag = (ps_slice_hdr->i2_slice_address == 0);
1535
1536
    /* Currently if dependent slices are enabled, then all slices
1537
    after first slice of picture, are made dependent slices */
1538
6.25k
    if((1 == ps_pps->i1_dependent_slice_enabled_flag) &&
1539
0
       (0 == ps_slice_hdr->i1_first_slice_in_pic_flag))
1540
0
    {
1541
0
        ps_slice_hdr->i1_dependent_slice_flag = 1;
1542
0
    }
1543
6.25k
    else
1544
6.25k
    {
1545
6.25k
        ps_slice_hdr->i1_dependent_slice_flag = 0;
1546
6.25k
    }
1547
1548
    /* Insert start code */
1549
6.25k
    return_status |= ihevce_put_nal_start_code_prefix(ps_bitstrm, 1);
1550
1551
    /* Insert Nal Unit Header */
1552
6.25k
    return_status |= ihevce_generate_nal_unit_header(
1553
6.25k
        ps_bitstrm,
1554
6.25k
        i1_nal_unit_type,
1555
6.25k
        ps_slice_hdr->u4_nuh_temporal_id);  //TEMPORALA_SCALABILITY CHANGES
1556
1557
    /* first_slice_in_pic_flag */
1558
6.25k
    PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_first_slice_in_pic_flag, 1, return_status);
1559
6.25k
    ENTROPY_TRACE("first_slice_in_pic_flag", ps_slice_hdr->i1_first_slice_in_pic_flag);
1560
1561
6.25k
    if(RapPicFlag)
1562
957
    {
1563
        /* no_output_of_prior_pics_flag */
1564
957
        PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_no_output_of_prior_pics_flag, 1, return_status);
1565
957
        ENTROPY_TRACE(
1566
957
            "no_output_of_prior_pics_flag", ps_slice_hdr->i1_no_output_of_prior_pics_flag);
1567
957
    }
1568
1569
    /* pic_parameter_set_id */
1570
6.25k
    PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_pps_id, return_status);
1571
6.25k
    ENTROPY_TRACE("pic_parameter_set_id", ps_slice_hdr->i1_pps_id);
1572
1573
    /* If ps_pps->i1_dependent_slice_enabled_flag is enabled and
1574
    curent slice is not the first slice of picture then put
1575
    i1_dependent_slice_flag into the bitstream */
1576
6.25k
    if((ps_pps->i1_dependent_slice_enabled_flag) && (!ps_slice_hdr->i1_first_slice_in_pic_flag))
1577
0
    {
1578
        /* dependent_slice_flag */
1579
0
        PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_dependent_slice_flag, 1, return_status);
1580
0
        ENTROPY_TRACE("dependent_slice_flag", ps_slice_hdr->i1_dependent_slice_flag);
1581
0
    }
1582
1583
6.25k
    if(!ps_slice_hdr->i1_first_slice_in_pic_flag)
1584
0
    {
1585
0
        WORD32 num_bits;
1586
0
        WORD32 num_ctb_in_pic;
1587
1588
        /* ctbs in frame ceiled for width / height not multiple of ctb size */
1589
0
        num_ctb_in_pic = ((pic_width + (ctb_size - 1)) >> log2_ctb_size) *
1590
0
                         ((pic_height + (ctb_size - 1)) >> log2_ctb_size);
1591
1592
        /* Use CLZ to compute Ceil( Log2( PicSizeInCtbsY ) ) */
1593
0
        num_bits = 32 - CLZ(num_ctb_in_pic - 1);
1594
1595
        /* slice_address */
1596
0
        PUT_BITS(ps_bitstrm, ps_slice_hdr->i2_slice_address, num_bits, return_status);
1597
0
        ENTROPY_TRACE("slice_address", ps_slice_hdr->i2_slice_address);
1598
0
    }
1599
1600
6.25k
    if(!ps_slice_hdr->i1_dependent_slice_flag)
1601
6.25k
    {
1602
6.25k
        for(i = 0; i < ps_pps->i1_num_extra_slice_header_bits; i++)
1603
0
        {
1604
            /* slice_reserved_undetermined_flag */
1605
0
            PUT_BITS(ps_bitstrm, 0, 1, return_status);
1606
0
            ENTROPY_TRACE("slice_reserved_undetermined_flag", 0);
1607
0
        }
1608
        /* slice_type */
1609
6.25k
        PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_slice_type, return_status);
1610
6.25k
        ENTROPY_TRACE("slice_type", ps_slice_hdr->i1_slice_type);
1611
1612
6.25k
        if(ps_pps->i1_output_flag_present_flag)
1613
0
        {
1614
            /* pic_output_flag */
1615
0
            PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_pic_output_flag, 1, return_status);
1616
0
            ENTROPY_TRACE("pic_output_flag", ps_slice_hdr->i1_pic_output_flag);
1617
0
        }
1618
1619
6.25k
        if(!idr_pic_flag)
1620
5.45k
        {
1621
            /* pic_order_cnt_lsb */
1622
5.45k
            PUT_BITS(
1623
5.45k
                ps_bitstrm,
1624
5.45k
                ps_slice_hdr->i4_pic_order_cnt_lsb,
1625
5.45k
                ps_sps->i1_log2_max_pic_order_cnt_lsb,
1626
5.45k
                return_status);
1627
5.45k
            ENTROPY_TRACE("pic_order_cnt_lsb", ps_slice_hdr->i4_pic_order_cnt_lsb);
1628
1629
            /* short_term_ref_pic_set_sps_flag */
1630
5.45k
            PUT_BITS(
1631
5.45k
                ps_bitstrm, ps_slice_hdr->i1_short_term_ref_pic_set_sps_flag, 1, return_status);
1632
5.45k
            ENTROPY_TRACE(
1633
5.45k
                "short_term_ref_pic_set_sps_flag",
1634
5.45k
                ps_slice_hdr->i1_short_term_ref_pic_set_sps_flag);
1635
1636
5.45k
            if(!ps_slice_hdr->i1_short_term_ref_pic_set_sps_flag)
1637
5.45k
            {
1638
5.45k
                ihevce_short_term_ref_pic_set(
1639
5.45k
                    ps_bitstrm, &ps_slice_hdr->s_stref_picset, 1, 0, &i4_NumPocTotalCurr);
1640
5.45k
            }
1641
0
            else
1642
0
            {
1643
0
                WORD32 num_bits = 32 - CLZ(ps_sps->i1_num_short_term_ref_pic_sets);
1644
1645
                /* short_term_ref_pic_set_idx */
1646
0
                PUT_BITS(
1647
0
                    ps_bitstrm,
1648
0
                    ps_slice_hdr->i1_short_term_ref_pic_set_idx,
1649
0
                    num_bits,
1650
0
                    return_status);
1651
0
                ENTROPY_TRACE(
1652
0
                    "short_term_ref_pic_set_idx", ps_slice_hdr->i1_short_term_ref_pic_set_idx);
1653
0
            }
1654
1655
5.45k
            if(ps_sps->i1_long_term_ref_pics_present_flag)
1656
0
            {
1657
0
                if(ps_sps->i1_num_long_term_ref_pics_sps > 0)
1658
0
                {
1659
                    /* num_long_term_sps */
1660
0
                    PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_num_long_term_sps, return_status);
1661
0
                    ENTROPY_TRACE("num_long_term_sps", ps_slice_hdr->i1_num_long_term_sps);
1662
0
                }
1663
1664
                /* num_long_term_pics */
1665
0
                PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_num_long_term_pics, return_status);
1666
0
                ENTROPY_TRACE("num_long_term_pics", ps_slice_hdr->i1_num_long_term_pics);
1667
1668
0
                for(i = 0;
1669
0
                    i < (ps_slice_hdr->i1_num_long_term_sps + ps_slice_hdr->i1_num_long_term_pics);
1670
0
                    i++)
1671
0
                {
1672
0
                    if(i < ps_slice_hdr->i1_num_long_term_sps)
1673
0
                    {
1674
                        /* Use CLZ to compute Ceil( Log2
1675
                        ( num_long_term_ref_pics_sps ) ) */
1676
0
                        WORD32 num_bits = 32 - CLZ(ps_sps->i1_num_long_term_ref_pics_sps);
1677
1678
                        /* lt_idx_sps[i] */
1679
0
                        PUT_BITS(
1680
0
                            ps_bitstrm, ps_slice_hdr->ai1_lt_idx_sps[i], num_bits, return_status);
1681
0
                        ENTROPY_TRACE("lt_idx_sps[i]", ps_slice_hdr->ai1_lt_idx_sps[i]);
1682
0
                    }
1683
0
                    else
1684
0
                    {
1685
                        /* poc_lsb_lt[i] */
1686
0
                        PUT_BITS(
1687
0
                            ps_bitstrm,
1688
0
                            ps_slice_hdr->ai4_poc_lsb_lt[i],
1689
0
                            ps_sps->i1_log2_max_pic_order_cnt_lsb,
1690
0
                            return_status);
1691
0
                        ENTROPY_TRACE("poc_lsb_lt[i]", ps_slice_hdr->ai4_poc_lsb_lt[i]);
1692
1693
                        /* used_by_curr_pic_lt_flag[i] */
1694
0
                        PUT_BITS(
1695
0
                            ps_bitstrm,
1696
0
                            ps_slice_hdr->ai1_used_by_curr_pic_lt_flag[i],
1697
0
                            1,
1698
0
                            return_status);
1699
0
                        ENTROPY_TRACE(
1700
0
                            "used_by_curr_pic_lt_flag[i]",
1701
0
                            ps_slice_hdr->ai1_used_by_curr_pic_lt_flag[i]);
1702
0
                    }
1703
1704
                    /* delta_poc_msb_present_flag[i] */
1705
0
                    PUT_BITS(
1706
0
                        ps_bitstrm,
1707
0
                        ps_slice_hdr->ai1_delta_poc_msb_present_flag[i],
1708
0
                        1,
1709
0
                        return_status);
1710
0
                    ENTROPY_TRACE(
1711
0
                        "delta_poc_msb_present_flag[i]",
1712
0
                        ps_slice_hdr->ai1_delta_poc_msb_present_flag[i]);
1713
1714
0
                    if(ps_slice_hdr->ai1_delta_poc_msb_present_flag[i])
1715
0
                    {
1716
                        /* delata_poc_msb_cycle_lt[i] */
1717
0
                        PUT_BITS_UEV(
1718
0
                            ps_bitstrm, ps_slice_hdr->ai1_delta_poc_msb_cycle_lt[i], return_status);
1719
0
                        ENTROPY_TRACE(
1720
0
                            "delata_poc_msb_cycle_lt", ps_slice_hdr->ai1_delta_poc_msb_cycle_lt[i]);
1721
0
                    }
1722
0
                }
1723
0
            }
1724
1725
5.45k
            if(ps_sps->i1_sps_temporal_mvp_enable_flag)
1726
5.45k
            {
1727
                /* slice_temporal_mvp_enable_flag */
1728
5.45k
                PUT_BITS(
1729
5.45k
                    ps_bitstrm, ps_slice_hdr->i1_slice_temporal_mvp_enable_flag, 1, return_status);
1730
5.45k
                ENTROPY_TRACE(
1731
5.45k
                    "slice_temporal_mvp_enable_flag",
1732
5.45k
                    ps_slice_hdr->i1_slice_temporal_mvp_enable_flag);
1733
5.45k
            }
1734
5.45k
        }
1735
1736
6.25k
        if(ps_sps->i1_sample_adaptive_offset_enabled_flag)
1737
2.15k
        {
1738
            /* slice_sao_luma_flag */
1739
2.15k
            PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_slice_sao_luma_flag, 1, return_status);
1740
2.15k
            ENTROPY_TRACE("slice_sao_luma_flag", ps_slice_hdr->i1_slice_sao_luma_flag);
1741
1742
            /* slice_sao_chroma_flag */
1743
2.15k
            PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_slice_sao_chroma_flag, 1, return_status);
1744
2.15k
            ENTROPY_TRACE("slice_sao_chroma_flag", ps_slice_hdr->i1_slice_sao_chroma_flag);
1745
2.15k
        }
1746
6.25k
        if((PSLICE == ps_slice_hdr->i1_slice_type) || (BSLICE == ps_slice_hdr->i1_slice_type))
1747
4.77k
        {
1748
            /* num_ref_idx_active_override_flag */
1749
4.77k
            PUT_BITS(
1750
4.77k
                ps_bitstrm, ps_slice_hdr->i1_num_ref_idx_active_override_flag, 1, return_status);
1751
4.77k
            ENTROPY_TRACE(
1752
4.77k
                "num_ref_idx_active_override_flag",
1753
4.77k
                ps_slice_hdr->i1_num_ref_idx_active_override_flag);
1754
1755
4.77k
            if(ps_slice_hdr->i1_num_ref_idx_active_override_flag)
1756
4.77k
            {
1757
                /* i1_num_ref_idx_l0_active_minus1 */
1758
4.77k
                PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_num_ref_idx_l0_active - 1, return_status);
1759
4.77k
                ENTROPY_TRACE(
1760
4.77k
                    "i1_num_ref_idx_l0_active_minus1", ps_slice_hdr->i1_num_ref_idx_l0_active - 1);
1761
1762
4.77k
                if(BSLICE == ps_slice_hdr->i1_slice_type)
1763
783
                {
1764
                    /* i1_num_ref_idx_l1_active */
1765
783
                    PUT_BITS_UEV(
1766
783
                        ps_bitstrm, ps_slice_hdr->i1_num_ref_idx_l1_active - 1, return_status);
1767
783
                    ENTROPY_TRACE(
1768
783
                        "i1_num_ref_idx_l1_active", ps_slice_hdr->i1_num_ref_idx_l1_active - 1);
1769
783
                }
1770
4.77k
            }
1771
1772
4.77k
            if(ps_pps->i1_lists_modification_present_flag && i4_NumPocTotalCurr > 1)
1773
0
            {
1774
0
                ref_pic_list_modification(ps_bitstrm, ps_slice_hdr, i4_NumPocTotalCurr);
1775
0
            }
1776
1777
4.77k
            if(BSLICE == ps_slice_hdr->i1_slice_type)
1778
783
            {
1779
                /* mvd_l1_zero_flag */
1780
783
                PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_mvd_l1_zero_flag, 1, return_status);
1781
783
                ENTROPY_TRACE("mvd_l1_zero_flag", ps_slice_hdr->i1_mvd_l1_zero_flag);
1782
783
            }
1783
1784
4.77k
            if(ps_pps->i1_cabac_init_present_flag)
1785
0
            {
1786
                /* cabac_init_flag */
1787
0
                PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_cabac_init_flag, 1, return_status);
1788
0
                ENTROPY_TRACE("cabac_init_flag", ps_slice_hdr->i1_cabac_init_flag);
1789
0
            }
1790
1791
4.77k
            if(ps_slice_hdr->i1_slice_temporal_mvp_enable_flag)
1792
4.77k
            {
1793
4.77k
                if(BSLICE == ps_slice_hdr->i1_slice_type)
1794
783
                {
1795
                    /* collocated_from_l0_flag */
1796
783
                    PUT_BITS(
1797
783
                        ps_bitstrm, ps_slice_hdr->i1_collocated_from_l0_flag, 1, return_status);
1798
783
                    ENTROPY_TRACE(
1799
783
                        "collocated_from_l0_flag", ps_slice_hdr->i1_collocated_from_l0_flag);
1800
783
                }
1801
4.77k
                if((ps_slice_hdr->i1_collocated_from_l0_flag &&
1802
4.27k
                    (ps_slice_hdr->i1_num_ref_idx_l0_active > 1)) ||
1803
2.12k
                   (!ps_slice_hdr->i1_collocated_from_l0_flag &&
1804
493
                    (ps_slice_hdr->i1_num_ref_idx_l1_active > 1)))
1805
2.68k
                {
1806
                    /* collocated_ref_idx */
1807
2.68k
                    PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_collocated_ref_idx, return_status);
1808
2.68k
                    ENTROPY_TRACE("collocated_ref_idx", ps_slice_hdr->i1_collocated_ref_idx);
1809
2.68k
                }
1810
4.77k
            }
1811
1812
4.77k
            if((ps_pps->i1_weighted_pred_flag && (PSLICE == ps_slice_hdr->i1_slice_type)) ||
1813
4.77k
               (ps_pps->i1_weighted_bipred_flag && (BSLICE == ps_slice_hdr->i1_slice_type)))
1814
0
            {
1815
0
                ihevce_generate_pred_weight_table(ps_bitstrm, ps_sps, ps_pps, ps_slice_hdr);
1816
0
            }
1817
1818
4.77k
#if !HM_8DOT1_SYNTAX
1819
            /* five_minus_max_num_merge_cand */
1820
4.77k
            PUT_BITS_UEV(ps_bitstrm, 5 - ps_slice_hdr->i1_max_num_merge_cand, return_status);
1821
4.77k
            ENTROPY_TRACE("five_minus_max_num_merge_cand", 5 - ps_slice_hdr->i1_max_num_merge_cand);
1822
4.77k
#endif
1823
4.77k
        }
1824
#if HM_8DOT1_SYNTAX
1825
        /* five_minus_max_num_merge_cand */
1826
        PUT_BITS_UEV(ps_bitstrm, 5 - ps_slice_hdr->i1_max_num_merge_cand, return_status);
1827
        ENTROPY_TRACE("five_minus_max_num_merge_cand", 5 - ps_slice_hdr->i1_max_num_merge_cand);
1828
#endif
1829
1830
        /* slice_qp_delta */
1831
6.25k
        PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_slice_qp_delta, return_status);
1832
6.25k
        ENTROPY_TRACE("slice_qp_delta", ps_slice_hdr->i1_slice_qp_delta);
1833
1834
6.25k
        if(ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag)
1835
0
        {
1836
            /* slice_cb_qp_offset */
1837
0
            PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_slice_cb_qp_offset, return_status);
1838
0
            ENTROPY_TRACE("slice_cb_qp_offset", ps_slice_hdr->i1_slice_cb_qp_offset);
1839
1840
            /* slice_cr_qp_offset */
1841
0
            PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_slice_cr_qp_offset, return_status);
1842
0
            ENTROPY_TRACE("slice_cr_qp_offset", ps_slice_hdr->i1_slice_cr_qp_offset);
1843
0
        }
1844
1845
6.25k
        if(ps_pps->i1_deblocking_filter_control_present_flag)
1846
6.25k
        {
1847
6.25k
            if(ps_pps->i1_deblocking_filter_override_enabled_flag)
1848
0
            {
1849
                /* deblocking_filter_override_flag */
1850
0
                PUT_BITS(
1851
0
                    ps_bitstrm, ps_slice_hdr->i1_deblocking_filter_override_flag, 1, return_status);
1852
0
                ENTROPY_TRACE(
1853
0
                    "deblocking_filter_override_flag",
1854
0
                    ps_slice_hdr->i1_deblocking_filter_override_flag);
1855
0
            }
1856
1857
6.25k
            if(ps_slice_hdr->i1_deblocking_filter_override_flag)
1858
0
            {
1859
                /* slice_disable_deblocking_filter_flag */
1860
0
                PUT_BITS(
1861
0
                    ps_bitstrm,
1862
0
                    ps_slice_hdr->i1_slice_disable_deblocking_filter_flag,
1863
0
                    1,
1864
0
                    return_status);
1865
0
                ENTROPY_TRACE(
1866
0
                    "slice_disable_deblocking_filter_flag",
1867
0
                    ps_slice_hdr->i1_slice_disable_deblocking_filter_flag);
1868
1869
0
                if(!ps_slice_hdr->i1_slice_disable_deblocking_filter_flag)
1870
0
                {
1871
                    /* beta_offset_div2 */
1872
0
                    PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_beta_offset_div2 >> 1, return_status);
1873
0
                    ENTROPY_TRACE("beta_offset_div2", ps_slice_hdr->i1_beta_offset_div2 >> 1);
1874
1875
                    /* tc_offset_div2 */
1876
0
                    PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_tc_offset_div2 >> 1, return_status);
1877
0
                    ENTROPY_TRACE("tc_offset_div2", ps_slice_hdr->i1_tc_offset_div2 >> 1);
1878
0
                }
1879
0
            }
1880
6.25k
        }
1881
1882
6.25k
        disable_deblocking_filter_flag = ps_slice_hdr->i1_slice_disable_deblocking_filter_flag |
1883
6.25k
                                         ps_pps->i1_pic_disable_deblocking_filter_flag;
1884
1885
6.25k
        if(ps_pps->i1_loop_filter_across_slices_enabled_flag &&
1886
6.25k
           (ps_slice_hdr->i1_slice_sao_luma_flag || ps_slice_hdr->i1_slice_sao_chroma_flag ||
1887
4.09k
            !disable_deblocking_filter_flag))
1888
4.57k
        {
1889
            /* slice_loop_filter_across_slices_enabled_flag */
1890
4.57k
            PUT_BITS(
1891
4.57k
                ps_bitstrm,
1892
4.57k
                ps_slice_hdr->i1_slice_loop_filter_across_slices_enabled_flag,
1893
4.57k
                1,
1894
4.57k
                return_status);
1895
4.57k
            ENTROPY_TRACE(
1896
4.57k
                "slice_loop_filter_across_slices_enabled_flag",
1897
4.57k
                ps_slice_hdr->i1_slice_loop_filter_across_slices_enabled_flag);
1898
4.57k
        }
1899
6.25k
    }
1900
1901
6.25k
    if((ps_pps->i1_tiles_enabled_flag) || (ps_pps->i1_entropy_coding_sync_enabled_flag))
1902
797
    {
1903
        /* num_entry_point_offsets */
1904
797
        PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i4_num_entry_point_offsets, return_status);
1905
797
        ENTROPY_TRACE("num_entry_point_offsets", ps_slice_hdr->i4_num_entry_point_offsets);
1906
1907
        /*copy the bitstream state at this stage, later once all the offset are known the duplicated state is used to write offset in bitstream*/
1908
797
        memcpy(ps_dup_bit_strm_ent_offset, ps_bitstrm, sizeof(bitstrm_t));
1909
1910
797
        if(ps_slice_hdr->i4_num_entry_point_offsets > 0)
1911
797
        {
1912
            /* offset_len_minus1 */
1913
797
            PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_offset_len - 1, return_status);
1914
797
            ENTROPY_TRACE("offset_len_minus1", ps_slice_hdr->i1_offset_len - 1);
1915
1916
            /*check the bitstream offset here, the first offset will be fixed here based on num_entry_offset and maximum possible emulaiton prevention bytes*/
1917
            /*This offset is used to generate bitstream, In the end of frame processing actual offset are updated and if there was no emulation bits the extra bytes
1918
            shall be filled with 0xFF so that decoder discards it as part of slice header extension*/
1919
1920
            /*assume one byte of emulation preention for every offset we signal*/
1921
            /*considering emulation prevention bytes and assuming incomplete word(4 bytes) that is yet to filled and offset length(4 bytes) that will be calc
1922
            based on max offset length after frame is encoded*/
1923
797
            pu4_first_slice_start_offset[0] =
1924
797
                ps_bitstrm->u4_strm_buf_offset +
1925
797
                ((ps_slice_hdr->i4_num_entry_point_offsets * ps_slice_hdr->i1_offset_len) >> 3) +
1926
797
                ps_slice_hdr->i4_num_entry_point_offsets + 4 + 4;
1927
1928
797
            ps_slice_hdr->pu4_entry_point_offset[0] = (*pu4_first_slice_start_offset);
1929
1930
2.24k
            for(i = 0; i < ps_slice_hdr->i4_num_entry_point_offsets; i++)
1931
1.44k
            {
1932
                /* entry_point_offset[i] */
1933
1.44k
                PUT_BITS(
1934
1.44k
                    ps_bitstrm,
1935
1.44k
                    ps_slice_hdr->pu4_entry_point_offset[i],
1936
1.44k
                    ps_slice_hdr->i1_offset_len,
1937
1.44k
                    return_status);
1938
1.44k
                ENTROPY_TRACE("entry_point_offset[i]", ps_slice_hdr->pu4_entry_point_offset[i]);
1939
1.44k
            }
1940
797
        }
1941
797
    }
1942
1943
6.25k
    if(ps_pps->i1_slice_header_extension_present_flag)
1944
0
    {
1945
        /* slice_header_extension_length */
1946
0
        PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i2_slice_header_extension_length, return_status);
1947
0
        ENTROPY_TRACE(
1948
0
            "slice_header_extension_length", ps_slice_hdr->i2_slice_header_extension_length);
1949
1950
0
        for(i = 0; i < ps_slice_hdr->i2_slice_header_extension_length; i++)
1951
0
        {
1952
            /* slice_header_extension_data_byte[i] */
1953
0
            PUT_BITS(ps_bitstrm, 0, 8, return_status);
1954
0
            ENTROPY_TRACE("slice_header_extension_data_byte[i]", 0);
1955
0
        }
1956
0
    }
1957
1958
6.25k
    BYTE_ALIGNMENT(ps_bitstrm);
1959
1960
6.25k
    return return_status;
1961
6.25k
}
1962
1963
/**
1964
******************************************************************************
1965
*
1966
*  @brief Populates vps structure
1967
*
1968
*  @par   Description
1969
*  All the parameters in vps are currently hard coded
1970
*
1971
*  @param[out]  ps_vps
1972
*  pointer to vps params that needs to be populated
1973
*
1974
*  @param[in]   ps_src_params
1975
*  pointer to source config params; resolution, frame rate etc
1976
*
1977
*  @param[in]   ps_out_strm_params
1978
*  pointer to output stream config params
1979
*
1980
*  @param[in]   ps_coding_params
1981
*  pointer to coding params; to enable/disable various toolsets in pps
1982
*
1983
*  @param[in]   ps_config_prms
1984
*  pointer to configuration params like bitrate, HRD buffer sizes, cu, tu sizes
1985
*
1986
*
1987
*  @return      success or failure error code
1988
*
1989
******************************************************************************
1990
*/
1991
WORD32 ihevce_populate_vps(
1992
    enc_ctxt_t *ps_enc_ctxt,
1993
    vps_t *ps_vps,
1994
    ihevce_src_params_t *ps_src_params,
1995
    ihevce_out_strm_params_t *ps_out_strm_params,
1996
    ihevce_coding_params_t *ps_coding_params,
1997
    ihevce_config_prms_t *ps_config_prms,
1998
    ihevce_static_cfg_params_t *ps_stat_cfg_prms,
1999
    WORD32 i4_resolution_id)
2000
261
{
2001
261
    WORD8 *pi1_profile_compatiblity_flags;
2002
261
    WORD32 i;
2003
261
    WORD32 i4_field_pic = ps_src_params->i4_field_pic;
2004
261
    WORD32 i4_codec_level_index;
2005
261
    ps_vps->i1_vps_id = DEFAULT_VPS_ID;
2006
2007
261
    (void)ps_config_prms;
2008
    /* default sub layers is 1 */
2009
261
    ps_vps->i1_vps_max_sub_layers = 1;
2010
261
    if(1 == ps_stat_cfg_prms->s_tgt_lyr_prms.i4_enable_temporal_scalability)
2011
0
    {
2012
0
        ps_vps->i1_vps_max_sub_layers = 2;
2013
0
    }
2014
2015
522
    for(i = 0; i < ps_vps->i1_vps_max_sub_layers; i++)
2016
261
    {
2017
        /* currently bit rate and pic rate signalling is disabled */
2018
261
        ps_vps->ai1_bit_rate_info_present_flag[i] = 0;
2019
261
        ps_vps->ai1_pic_rate_info_present_flag[i] = 0;
2020
2021
261
        if(ps_vps->ai1_bit_rate_info_present_flag[i])
2022
0
        {
2023
            /* TODO: Add support for bitrate and max bitrate */
2024
0
            ps_vps->au2_avg_bit_rate[i] = 0;
2025
0
            ps_vps->au2_max_bit_rate[i] = 0;
2026
0
        }
2027
2028
261
        if(ps_vps->ai1_pic_rate_info_present_flag[i])
2029
0
        {
2030
            /* TODO: Add support for pic rate idc and avg pic rate */
2031
0
        }
2032
261
    }
2033
2034
    /* default sub layer ordering info present flag */
2035
261
    ps_vps->i1_sub_layer_ordering_info_present_flag = VPS_SUB_LAYER_ORDERING_INFO_ABSENT;
2036
2037
    /* hrd and temporal id nesting not supported for now */
2038
261
    ps_vps->i1_vps_num_hrd_parameters = 0;
2039
2040
261
    if(ps_vps->i1_vps_max_sub_layers == 1)
2041
261
    {
2042
261
        ps_vps->i1_vps_temporal_id_nesting_flag = 1;
2043
261
    }
2044
0
    else
2045
0
    {
2046
0
        ps_vps->i1_vps_temporal_id_nesting_flag = 0;
2047
0
    }
2048
2049
    /* populate the general profile, tier and level information */
2050
261
    ps_vps->s_ptl.s_ptl_gen.i1_profile_space = 0;  // BLU_RAY specific change is default
2051
2052
    /* set the profile according to user input */
2053
261
    ps_vps->s_ptl.s_ptl_gen.i1_profile_idc = ps_out_strm_params->i4_codec_profile;
2054
2055
    /***************************************************************/
2056
    /* set the profile compatibility flag for current profile to 1 */
2057
    /* the rest of the flags are set to 0                          */
2058
    /***************************************************************/
2059
261
    pi1_profile_compatiblity_flags = &ps_vps->s_ptl.s_ptl_gen.ai1_profile_compatibility_flag[0];
2060
2061
522
    for(i = 0; i < ps_vps->i1_vps_max_sub_layers; i++)  //TEMPORALA_SCALABILITY CHANGES
2062
261
    {
2063
261
        ps_vps->ai1_vps_max_dec_pic_buffering[i] =
2064
261
            ps_coding_params->i4_max_reference_frames + (2 << i4_field_pic) - 1;
2065
2066
261
        ps_vps->ai1_vps_max_num_reorder_pics[i] = ps_coding_params->i4_max_temporal_layers
2067
261
                                                  << i4_field_pic;
2068
2069
261
        ps_vps->ai1_vps_max_latency_increase[i] = 0;
2070
2071
261
        ps_vps->s_ptl.ai1_sub_layer_level_present_flag[i] = 1;  //TEMPORALA_SCALABILITY CHANGES
2072
2073
261
        ps_vps->s_ptl.ai1_sub_layer_profile_present_flag[i] = 0;  //TEMPORALA_SCALABILITY CHANGES
2074
2075
261
        ps_vps->s_ptl.as_ptl_sub[i].i1_profile_space = 0;  // BLU_RAY specific change is default
2076
2077
261
        ps_vps->s_ptl.as_ptl_sub[i].i1_profile_idc = ps_out_strm_params->i4_codec_profile;
2078
2079
261
        memset(
2080
261
            ps_vps->s_ptl.as_ptl_sub[i].ai1_profile_compatibility_flag,
2081
261
            0,
2082
261
            MAX_PROFILE_COMPATBLTY * sizeof(WORD8));
2083
2084
261
        ps_vps->s_ptl.as_ptl_sub[i]
2085
261
            .ai1_profile_compatibility_flag[ps_out_strm_params->i4_codec_profile] = 1;
2086
2087
261
        ps_vps->s_ptl.as_ptl_sub[i].u1_level_idc =
2088
261
            ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_codec_level;
2089
2090
261
        if(0 == i)  // Only one level temporal scalability suport has been added.
2091
261
        {
2092
261
            i4_codec_level_index = ihevce_get_level_index(
2093
261
                ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_codec_level);
2094
2095
261
            if(i4_codec_level_index)
2096
261
                i4_codec_level_index -= 1;
2097
2098
261
            ps_vps->s_ptl.as_ptl_sub[i].u1_level_idc =
2099
261
                (WORD32)g_as_level_data[i4_codec_level_index].e_level;
2100
261
        }
2101
2102
261
        ps_vps->s_ptl.as_ptl_sub[i].i1_tier_flag = ps_out_strm_params->i4_codec_tier;
2103
2104
261
        if(ps_src_params->i4_field_pic == IV_PROGRESSIVE)
2105
261
        {
2106
261
            ps_vps->s_ptl.as_ptl_sub[i].i1_general_progressive_source_flag = 1;
2107
2108
261
            ps_vps->s_ptl.as_ptl_sub[i].i1_general_interlaced_source_flag = 0;
2109
261
        }
2110
0
        else if(ps_src_params->i4_field_pic == IV_INTERLACED)
2111
0
        {
2112
0
            ps_vps->s_ptl.as_ptl_sub[i].i1_general_progressive_source_flag = 0;
2113
2114
0
            ps_vps->s_ptl.as_ptl_sub[i].i1_general_interlaced_source_flag = 1;
2115
0
        }
2116
0
        else if(ps_src_params->i4_field_pic == IV_CONTENTTYPE_NA)
2117
0
        {
2118
0
            ps_vps->s_ptl.as_ptl_sub[i].i1_general_progressive_source_flag = 0;
2119
2120
0
            ps_vps->s_ptl.as_ptl_sub[i].i1_general_interlaced_source_flag = 0;
2121
0
        }
2122
2123
261
        ps_vps->s_ptl.as_ptl_sub[i].i1_general_non_packed_constraint_flag =
2124
261
            DEFAULT_NON_PACKED_CONSTRAINT_FLAG;
2125
2126
261
        if(ps_enc_ctxt->i4_blu_ray_spec == 1)
2127
0
        {
2128
0
            ps_vps->s_ptl.as_ptl_sub[i].i1_frame_only_constraint_flag = 1;
2129
0
        }
2130
261
        else
2131
261
        {
2132
261
            ps_vps->s_ptl.as_ptl_sub[i].i1_frame_only_constraint_flag =
2133
261
                DEFAULT_FRAME_ONLY_CONSTRAINT_FLAG;
2134
261
        }
2135
261
    }
2136
2137
261
    memset(pi1_profile_compatiblity_flags, 0, MAX_PROFILE_COMPATBLTY);
2138
261
    pi1_profile_compatiblity_flags[ps_out_strm_params->i4_codec_profile] = 1;
2139
2140
    /* set the level idc according to user input */
2141
261
    ps_vps->s_ptl.s_ptl_gen.u1_level_idc =
2142
261
        ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_codec_level;
2143
2144
261
    ps_vps->s_ptl.s_ptl_gen.i1_tier_flag = ps_out_strm_params->i4_codec_tier;
2145
2146
261
    if(ps_src_params->i4_field_pic == IV_PROGRESSIVE)
2147
261
    {
2148
261
        ps_vps->s_ptl.s_ptl_gen.i1_general_progressive_source_flag = 1;
2149
2150
261
        ps_vps->s_ptl.s_ptl_gen.i1_general_interlaced_source_flag = 0;
2151
261
    }
2152
0
    else if(ps_src_params->i4_field_pic == IV_INTERLACED)
2153
0
    {
2154
0
        ps_vps->s_ptl.s_ptl_gen.i1_general_progressive_source_flag = 0;
2155
2156
0
        ps_vps->s_ptl.s_ptl_gen.i1_general_interlaced_source_flag = 1;
2157
0
    }
2158
0
    else if(ps_src_params->i4_field_pic == IV_CONTENTTYPE_NA)
2159
0
    {
2160
0
        ps_vps->s_ptl.s_ptl_gen.i1_general_progressive_source_flag = 0;
2161
2162
0
        ps_vps->s_ptl.s_ptl_gen.i1_general_interlaced_source_flag = 0;
2163
0
    }
2164
2165
261
    ps_vps->s_ptl.s_ptl_gen.i1_general_non_packed_constraint_flag =
2166
261
        DEFAULT_NON_PACKED_CONSTRAINT_FLAG;
2167
2168
261
    if(ps_enc_ctxt->i4_blu_ray_spec == 1)
2169
0
    {
2170
0
        ps_vps->s_ptl.s_ptl_gen.i1_frame_only_constraint_flag = 1;
2171
0
    }
2172
261
    else
2173
261
    {
2174
261
        ps_vps->s_ptl.s_ptl_gen.i1_frame_only_constraint_flag = DEFAULT_FRAME_ONLY_CONSTRAINT_FLAG;
2175
261
    }
2176
261
    if((ps_out_strm_params->i4_codec_profile == 4) &&
2177
0
       (ps_src_params->i4_chr_format == IV_YUV_420SP_UV))
2178
0
    {
2179
0
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_12bit_constraint_flag = 1;
2180
2181
0
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_10bit_constraint_flag = 0;
2182
2183
0
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_8bit_constraint_flag = 0;
2184
2185
0
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_422chroma_constraint_flag = 1;
2186
2187
0
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_420chroma_constraint_flag = 1;
2188
2189
0
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_monochrome_constraint_flag = 0;
2190
2191
0
        ps_vps->s_ptl.s_ptl_gen.i1_general_intra_constraint_flag = 0;
2192
2193
0
        ps_vps->s_ptl.s_ptl_gen.i1_general_one_picture_only_constraint_flag = 0;
2194
2195
0
        ps_vps->s_ptl.s_ptl_gen.i1_general_lower_bit_rate_constraint_flag = 1;
2196
0
    }
2197
261
    else if(
2198
261
        (ps_out_strm_params->i4_codec_profile == 4) &&
2199
0
        (ps_src_params->i4_chr_format == IV_YUV_422SP_UV))
2200
0
    {
2201
0
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_12bit_constraint_flag = 1;
2202
2203
0
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_10bit_constraint_flag = 0;
2204
2205
0
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_8bit_constraint_flag = 0;
2206
2207
0
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_422chroma_constraint_flag = 1;
2208
2209
0
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_420chroma_constraint_flag = 0;
2210
2211
0
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_monochrome_constraint_flag = 0;
2212
2213
0
        ps_vps->s_ptl.s_ptl_gen.i1_general_intra_constraint_flag = 0;
2214
2215
0
        ps_vps->s_ptl.s_ptl_gen.i1_general_one_picture_only_constraint_flag = 0;
2216
2217
0
        ps_vps->s_ptl.s_ptl_gen.i1_general_lower_bit_rate_constraint_flag = 1;
2218
0
    }
2219
261
    else
2220
261
    {
2221
261
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_12bit_constraint_flag = 0;
2222
2223
261
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_10bit_constraint_flag = 0;
2224
2225
261
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_8bit_constraint_flag = 0;
2226
2227
261
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_422chroma_constraint_flag = 0;
2228
2229
261
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_420chroma_constraint_flag = 0;
2230
2231
261
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_monochrome_constraint_flag = 0;
2232
2233
261
        ps_vps->s_ptl.s_ptl_gen.i1_general_intra_constraint_flag = 0;
2234
2235
261
        ps_vps->s_ptl.s_ptl_gen.i1_general_one_picture_only_constraint_flag = 0;
2236
2237
261
        ps_vps->s_ptl.s_ptl_gen.i1_general_lower_bit_rate_constraint_flag = 0;
2238
261
    }
2239
2240
261
    ps_vps->i1_vps_max_nuh_reserved_zero_layer_id = 0;
2241
2242
261
    return IHEVCE_SUCCESS;
2243
261
}
2244
2245
/**
2246
******************************************************************************
2247
*
2248
*  @brief Populates sps structure
2249
*
2250
*  @par   Description
2251
*  Populates sps structure for its use in header generation
2252
*
2253
*  @param[out]  ps_sps
2254
*  pointer to sps params that needs to be populated
2255
*
2256
*  @param[in]   ps_vps
2257
*  pointer to vps params referred by the sps
2258
*
2259
*  @param[in]   ps_src_params
2260
*  pointer to source config params; resolution, frame rate etc
2261
*
2262
*  @param[in]   ps_out_strm_params
2263
*  pointer to output stream config params
2264
*
2265
*  @param[in]   ps_coding_params
2266
*  pointer to coding params; to enable/disable various toolsets in pps
2267
*
2268
*  @param[in]   ps_config_prms
2269
*  pointer to configuration params like bitrate, HRD buffer sizes, cu, tu sizes
2270
*
2271
*  @return      success or failure error code
2272
*
2273
******************************************************************************
2274
*/
2275
WORD32 ihevce_populate_sps(
2276
    enc_ctxt_t *ps_enc_ctxt,
2277
    sps_t *ps_sps,
2278
    vps_t *ps_vps,
2279
    ihevce_src_params_t *ps_src_params,
2280
    ihevce_out_strm_params_t *ps_out_strm_params,
2281
    ihevce_coding_params_t *ps_coding_params,
2282
    ihevce_config_prms_t *ps_config_prms,
2283
    frm_ctb_ctxt_t *ps_frm_ctb_prms,
2284
    ihevce_static_cfg_params_t *ps_stat_cfg_prms,
2285
    WORD32 i4_resolution_id)
2286
261
{
2287
261
    WORD32 i;
2288
261
    WORD32 i4_field_pic = ps_src_params->i4_field_pic;
2289
261
    WORD32 i4_quality_preset =
2290
261
        ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_quality_preset;
2291
261
    WORD32 i4_codec_level_index;
2292
2293
261
    if(i4_quality_preset == IHEVCE_QUALITY_P7)
2294
34
    {
2295
34
        i4_quality_preset = IHEVCE_QUALITY_P6;
2296
34
    }
2297
2298
261
    ps_sps->i1_sps_id = DEFAULT_SPS_ID;
2299
2300
261
    if(1 == ps_stat_cfg_prms->s_tgt_lyr_prms.i4_mres_single_out)
2301
0
    {
2302
0
        ps_sps->i1_sps_id = i4_resolution_id;
2303
0
    }
2304
2305
261
    ps_sps->i1_vps_id = ps_vps->i1_vps_id;
2306
2307
261
    ps_sps->i2_pic_height_in_luma_samples = ps_frm_ctb_prms->i4_cu_aligned_pic_ht;
2308
2309
261
    ps_sps->i2_pic_width_in_luma_samples = ps_frm_ctb_prms->i4_cu_aligned_pic_wd;
2310
2311
261
    ps_sps->i1_amp_enabled_flag = AMP_ENABLED;
2312
2313
261
    ps_sps->i1_chroma_format_idc = (ps_src_params->i4_chr_format == IV_YUV_422SP_UV) ? 2 : 1;
2314
2315
261
    ps_sps->i1_separate_colour_plane_flag = 0;
2316
2317
261
    ps_sps->i1_bit_depth_luma_minus8 = ps_stat_cfg_prms->s_tgt_lyr_prms.i4_internal_bit_depth - 8;
2318
2319
261
    ps_sps->i1_bit_depth_chroma_minus8 = ps_stat_cfg_prms->s_tgt_lyr_prms.i4_internal_bit_depth - 8;
2320
2321
261
    ps_sps->i1_log2_min_coding_block_size = ps_config_prms->i4_min_log2_cu_size;
2322
2323
261
    ps_sps->i1_log2_diff_max_min_coding_block_size =
2324
261
        ps_config_prms->i4_max_log2_cu_size - ps_config_prms->i4_min_log2_cu_size;
2325
2326
261
    ps_sps->i1_log2_ctb_size =
2327
261
        ps_sps->i1_log2_min_coding_block_size + ps_sps->i1_log2_diff_max_min_coding_block_size;
2328
2329
261
    ps_sps->i1_log2_diff_max_min_transform_block_size =
2330
261
        ps_config_prms->i4_max_log2_tu_size - ps_config_prms->i4_min_log2_tu_size;
2331
2332
261
    ps_sps->i1_log2_min_transform_block_size = ps_config_prms->i4_min_log2_tu_size;
2333
2334
261
    ps_sps->i1_long_term_ref_pics_present_flag = LONG_TERM_REF_PICS_ABSENT;
2335
2336
261
    ps_sps->i1_max_transform_hierarchy_depth_inter = ps_config_prms->i4_max_tr_tree_depth_nI;
2337
2338
261
    ps_sps->i1_max_transform_hierarchy_depth_intra = ps_config_prms->i4_max_tr_tree_depth_I;
2339
2340
261
    ps_sps->i1_pcm_enabled_flag = PCM_DISABLED;
2341
2342
261
    ps_sps->i1_pcm_loop_filter_disable_flag = PCM_LOOP_FILTER_DISABLED;
2343
2344
261
    ps_sps->i1_pic_cropping_flag = !!ps_coding_params->i4_cropping_mode;
2345
2346
261
    if(i4_quality_preset < IHEVCE_QUALITY_P4)
2347
167
    {
2348
        /*** Enable SAO for PQ,HQ,MS presets **/
2349
167
        ps_sps->i1_sample_adaptive_offset_enabled_flag = SAO_ENABLED;
2350
167
    }
2351
94
    else
2352
94
    {
2353
94
        ps_sps->i1_sample_adaptive_offset_enabled_flag = SAO_DISABLED;
2354
94
    }
2355
#if DISABLE_SAO
2356
    ps_sps->i1_sample_adaptive_offset_enabled_flag = SAO_DISABLED;
2357
#endif
2358
2359
261
    if(ps_coding_params->i4_use_default_sc_mtx == 1)
2360
91
    {
2361
91
        ps_sps->i1_scaling_list_enable_flag = SCALING_LIST_ENABLED;
2362
91
    }
2363
170
    else
2364
170
    {
2365
170
        ps_sps->i1_scaling_list_enable_flag = SCALING_LIST_DISABLED;
2366
170
    }
2367
2368
261
    ps_sps->i1_sps_max_sub_layers = DEFAULT_SPS_MAX_SUB_LAYERS;
2369
2370
261
    if(1 == ps_stat_cfg_prms->s_tgt_lyr_prms.i4_enable_temporal_scalability)
2371
0
    {
2372
0
        ps_sps->i1_sps_max_sub_layers = DEFAULT_SPS_MAX_SUB_LAYERS + 1;
2373
0
    }
2374
2375
261
    ps_sps->i1_sps_sub_layer_ordering_info_present_flag = SPS_SUB_LAYER_ORDERING_INFO_ABSENT;
2376
2377
261
    ps_sps->i1_sps_scaling_list_data_present_flag = SCALING_LIST_DATA_ABSENT;
2378
2379
261
    if(ps_sps->i1_sps_max_sub_layers == 1)
2380
261
    {
2381
261
        ps_sps->i1_sps_temporal_id_nesting_flag = 1;  //NO_SPS_TEMPORAL_ID_NESTING_DONE;
2382
261
    }
2383
0
    else
2384
0
    {
2385
0
        ps_sps->i1_sps_temporal_id_nesting_flag = 0;  //NO_SPS_TEMPORAL_ID_NESTING_DONE;
2386
0
    }
2387
2388
    /* short term and long term ref pic set not signalled in sps */
2389
261
    ps_sps->i1_num_short_term_ref_pic_sets = 0;
2390
261
    ps_sps->i1_long_term_ref_pics_present_flag = 0;
2391
2392
261
    ps_sps->i1_num_long_term_ref_pics_sps = 0;
2393
261
    ps_sps->i1_sps_temporal_mvp_enable_flag = !DISABLE_TMVP;
2394
2395
261
    ps_sps->i1_strong_intra_smoothing_enable_flag = STRONG_INTRA_SMOOTHING_FLAG_ENABLE;
2396
2397
261
    ps_sps->i1_vui_parameters_present_flag = ps_out_strm_params->i4_vui_enable;
2398
2399
    /*required in generation of slice header*/
2400
261
    ps_sps->i2_pic_ht_in_ctb = ps_frm_ctb_prms->i4_num_ctbs_vert;
2401
2402
261
    ps_sps->i2_pic_wd_in_ctb = ps_frm_ctb_prms->i4_num_ctbs_horz;
2403
2404
261
    ps_sps->i1_log2_max_pic_order_cnt_lsb = DEFAULT_LOG2_MAX_POC_LSB;
2405
2406
261
    if(ps_sps->i1_pic_cropping_flag)
2407
261
    {
2408
261
        WORD32 num_rows_to_pad_bottom =
2409
261
            ps_sps->i2_pic_height_in_luma_samples - ps_stat_cfg_prms->s_src_prms.i4_orig_height;
2410
261
        WORD32 num_rows_to_pad_right =
2411
261
            ps_sps->i2_pic_width_in_luma_samples - ps_stat_cfg_prms->s_src_prms.i4_orig_width;
2412
2413
261
        ps_sps->i2_pic_crop_top_offset = DEFAULT_PIC_CROP_TOP_OFFSET;
2414
2415
261
        ps_sps->i2_pic_crop_left_offset = DEFAULT_PIC_CROP_LEFT_OFFSET;
2416
2417
        /* picture offsets should be signalled in terms of chroma unit */
2418
261
        ps_sps->i2_pic_crop_bottom_offset = num_rows_to_pad_bottom >> 1;
2419
2420
        /* picture offsets should be signalled in terms of chroma unit */
2421
261
        ps_sps->i2_pic_crop_right_offset = num_rows_to_pad_right >> 1;
2422
261
    }
2423
2424
522
    for(i = 0; i < (ps_sps->i1_sps_max_sub_layers); i++)
2425
261
    {
2426
261
        ps_sps->ai1_sps_max_dec_pic_buffering[i] =
2427
261
            ps_coding_params->i4_max_reference_frames + (2 << i4_field_pic) - 1;
2428
2429
261
        ps_sps->ai1_sps_max_num_reorder_pics[i] = ps_coding_params->i4_max_temporal_layers
2430
261
                                                  << i4_field_pic;
2431
2432
261
        ps_sps->ai1_sps_max_latency_increase[i] = 0;
2433
2434
261
        ps_sps->s_ptl.ai1_sub_layer_level_present_flag[i] = 1;  //TEMPORALA_SCALABILITY CHANGES
2435
2436
261
        ps_sps->s_ptl.ai1_sub_layer_profile_present_flag[i] = 0;  //TEMPORALA_SCALABILITY CHANGES
2437
2438
261
        ps_sps->s_ptl.as_ptl_sub[i].i1_profile_space = 0;  // BLU_RAY specific change is default
2439
2440
261
        ps_sps->s_ptl.as_ptl_sub[i].i1_profile_idc = ps_out_strm_params->i4_codec_profile;
2441
2442
261
        memset(
2443
261
            ps_sps->s_ptl.as_ptl_sub[i].ai1_profile_compatibility_flag,
2444
261
            0,
2445
261
            MAX_PROFILE_COMPATBLTY * sizeof(WORD8));
2446
2447
261
        ps_sps->s_ptl.as_ptl_sub[i]
2448
261
            .ai1_profile_compatibility_flag[ps_out_strm_params->i4_codec_profile] = 1;
2449
2450
261
        ps_sps->s_ptl.as_ptl_sub[i].u1_level_idc =
2451
261
            ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_codec_level;
2452
2453
261
        if(0 == i)  // Only one level temporal scalability suport has been added.
2454
261
        {
2455
261
            i4_codec_level_index = ihevce_get_level_index(
2456
261
                ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_codec_level);
2457
2458
261
            if(i4_codec_level_index)
2459
261
                i4_codec_level_index -= 1;
2460
2461
261
            ps_sps->s_ptl.as_ptl_sub[i].u1_level_idc =
2462
261
                (WORD32)g_as_level_data[i4_codec_level_index].e_level;
2463
261
        }
2464
261
        ps_sps->s_ptl.as_ptl_sub[i].i1_tier_flag = ps_out_strm_params->i4_codec_tier;
2465
2466
261
        if(ps_src_params->i4_field_pic == IV_PROGRESSIVE)
2467
261
        {
2468
261
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_progressive_source_flag = 1;
2469
2470
261
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_interlaced_source_flag = 0;
2471
261
        }
2472
0
        else if(ps_src_params->i4_field_pic == IV_INTERLACED)
2473
0
        {
2474
0
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_progressive_source_flag = 0;
2475
2476
0
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_interlaced_source_flag = 1;
2477
0
        }
2478
0
        else if(ps_src_params->i4_field_pic == IV_CONTENTTYPE_NA)
2479
0
        {
2480
0
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_progressive_source_flag = 0;
2481
2482
0
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_interlaced_source_flag = 0;
2483
0
        }
2484
2485
261
        ps_sps->s_ptl.as_ptl_sub[i].i1_general_non_packed_constraint_flag =
2486
261
            DEFAULT_NON_PACKED_CONSTRAINT_FLAG;
2487
2488
261
        if(ps_enc_ctxt->i4_blu_ray_spec == 1)
2489
0
        {
2490
0
            ps_sps->s_ptl.as_ptl_sub[i].i1_frame_only_constraint_flag = 1;
2491
0
        }
2492
261
        else
2493
261
        {
2494
261
            ps_sps->s_ptl.as_ptl_sub[i].i1_frame_only_constraint_flag =
2495
261
                DEFAULT_FRAME_ONLY_CONSTRAINT_FLAG;
2496
261
        }
2497
261
        if((ps_out_strm_params->i4_codec_profile == 4) && (ps_sps->i1_chroma_format_idc == 1))
2498
0
        {
2499
0
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_12bit_constraint_flag = 1;
2500
2501
0
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_10bit_constraint_flag = 0;
2502
2503
0
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_8bit_constraint_flag = 0;
2504
2505
0
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_422chroma_constraint_flag = 1;
2506
2507
0
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_420chroma_constraint_flag = 1;
2508
2509
0
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_monochrome_constraint_flag = 0;
2510
2511
0
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_intra_constraint_flag = 0;
2512
2513
0
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_one_picture_only_constraint_flag = 0;
2514
2515
0
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_lower_bit_rate_constraint_flag = 1;
2516
0
        }
2517
261
        else if((ps_out_strm_params->i4_codec_profile == 4) && (ps_sps->i1_chroma_format_idc == 2))
2518
0
        {
2519
0
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_12bit_constraint_flag = 1;
2520
2521
0
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_10bit_constraint_flag = 0;
2522
2523
0
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_8bit_constraint_flag = 0;
2524
2525
0
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_422chroma_constraint_flag = 1;
2526
2527
0
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_420chroma_constraint_flag = 0;
2528
2529
0
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_monochrome_constraint_flag = 0;
2530
2531
0
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_intra_constraint_flag = 0;
2532
2533
0
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_one_picture_only_constraint_flag = 0;
2534
2535
0
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_lower_bit_rate_constraint_flag = 1;
2536
0
        }
2537
261
        else
2538
261
        {
2539
261
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_12bit_constraint_flag = 0;
2540
2541
261
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_10bit_constraint_flag = 0;
2542
2543
261
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_8bit_constraint_flag = 0;
2544
2545
261
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_422chroma_constraint_flag = 0;
2546
2547
261
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_420chroma_constraint_flag = 0;
2548
2549
261
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_monochrome_constraint_flag = 0;
2550
2551
261
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_intra_constraint_flag = 0;
2552
2553
261
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_one_picture_only_constraint_flag = 0;
2554
2555
261
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_lower_bit_rate_constraint_flag = 0;
2556
261
        }
2557
261
    }
2558
2559
261
    memset(
2560
261
        ps_sps->s_ptl.s_ptl_gen.ai1_profile_compatibility_flag,
2561
261
        0,
2562
261
        MAX_PROFILE_COMPATBLTY * sizeof(WORD8));
2563
2564
    /* populate the general profile, tier and level information */
2565
261
    ps_sps->s_ptl.s_ptl_gen.i1_profile_space = 0;  // BLU_RAY specific change is default
2566
2567
261
    ps_sps->s_ptl.s_ptl_gen.i1_profile_idc = ps_out_strm_params->i4_codec_profile;
2568
2569
261
    ps_sps->s_ptl.s_ptl_gen.ai1_profile_compatibility_flag[ps_out_strm_params->i4_codec_profile] =
2570
261
        1;
2571
2572
261
    ps_sps->s_ptl.s_ptl_gen.u1_level_idc =
2573
261
        ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_codec_level;
2574
2575
261
    ps_sps->s_ptl.s_ptl_gen.i1_tier_flag = ps_out_strm_params->i4_codec_tier;
2576
2577
261
    if(ps_src_params->i4_field_pic == IV_PROGRESSIVE)
2578
261
    {
2579
261
        ps_sps->s_ptl.s_ptl_gen.i1_general_progressive_source_flag = 1;
2580
2581
261
        ps_sps->s_ptl.s_ptl_gen.i1_general_interlaced_source_flag = 0;
2582
261
    }
2583
0
    else if(ps_src_params->i4_field_pic == IV_INTERLACED)
2584
0
    {
2585
0
        ps_sps->s_ptl.s_ptl_gen.i1_general_progressive_source_flag = 0;
2586
2587
0
        ps_sps->s_ptl.s_ptl_gen.i1_general_interlaced_source_flag = 1;
2588
0
    }
2589
0
    else if(ps_src_params->i4_field_pic == IV_CONTENTTYPE_NA)
2590
0
    {
2591
0
        ps_sps->s_ptl.s_ptl_gen.i1_general_progressive_source_flag = 0;
2592
2593
0
        ps_sps->s_ptl.s_ptl_gen.i1_general_interlaced_source_flag = 0;
2594
0
    }
2595
2596
261
    ps_sps->s_ptl.s_ptl_gen.i1_general_non_packed_constraint_flag =
2597
261
        DEFAULT_NON_PACKED_CONSTRAINT_FLAG;
2598
2599
261
    if(ps_enc_ctxt->i4_blu_ray_spec == 1)
2600
0
    {
2601
0
        ps_sps->s_ptl.s_ptl_gen.i1_frame_only_constraint_flag = 1;
2602
0
    }
2603
261
    else
2604
261
    {
2605
261
        ps_sps->s_ptl.s_ptl_gen.i1_frame_only_constraint_flag = DEFAULT_FRAME_ONLY_CONSTRAINT_FLAG;
2606
261
    }
2607
261
    if((ps_out_strm_params->i4_codec_profile == 4) && (ps_sps->i1_chroma_format_idc == 1))
2608
0
    {
2609
0
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_12bit_constraint_flag = 1;
2610
2611
0
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_10bit_constraint_flag = 0;
2612
2613
0
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_8bit_constraint_flag = 0;
2614
2615
0
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_422chroma_constraint_flag = 1;
2616
2617
0
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_420chroma_constraint_flag = 1;
2618
2619
0
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_monochrome_constraint_flag = 0;
2620
2621
0
        ps_sps->s_ptl.s_ptl_gen.i1_general_intra_constraint_flag = 0;
2622
2623
0
        ps_sps->s_ptl.s_ptl_gen.i1_general_one_picture_only_constraint_flag = 0;
2624
2625
0
        ps_sps->s_ptl.s_ptl_gen.i1_general_lower_bit_rate_constraint_flag = 1;
2626
0
    }
2627
261
    else if((ps_out_strm_params->i4_codec_profile == 4) && (ps_sps->i1_chroma_format_idc == 2))
2628
0
    {
2629
0
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_12bit_constraint_flag = 1;
2630
2631
0
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_10bit_constraint_flag = 0;
2632
2633
0
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_8bit_constraint_flag = 0;
2634
2635
0
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_422chroma_constraint_flag = 1;
2636
2637
0
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_420chroma_constraint_flag = 0;
2638
2639
0
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_monochrome_constraint_flag = 0;
2640
2641
0
        ps_sps->s_ptl.s_ptl_gen.i1_general_intra_constraint_flag = 0;
2642
2643
0
        ps_sps->s_ptl.s_ptl_gen.i1_general_one_picture_only_constraint_flag = 0;
2644
2645
0
        ps_sps->s_ptl.s_ptl_gen.i1_general_lower_bit_rate_constraint_flag = 1;
2646
0
    }
2647
261
    else
2648
261
    {
2649
261
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_12bit_constraint_flag = 0;
2650
2651
261
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_10bit_constraint_flag = 0;
2652
2653
261
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_8bit_constraint_flag = 0;
2654
2655
261
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_422chroma_constraint_flag = 0;
2656
2657
261
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_420chroma_constraint_flag = 0;
2658
2659
261
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_monochrome_constraint_flag = 0;
2660
2661
261
        ps_sps->s_ptl.s_ptl_gen.i1_general_intra_constraint_flag = 0;
2662
2663
261
        ps_sps->s_ptl.s_ptl_gen.i1_general_one_picture_only_constraint_flag = 0;
2664
2665
261
        ps_sps->s_ptl.s_ptl_gen.i1_general_lower_bit_rate_constraint_flag = 0;
2666
261
    }
2667
2668
261
    return IHEVCE_SUCCESS;
2669
261
}
2670
2671
/**
2672
******************************************************************************
2673
*
2674
*  @brief Populates pps structure based on input cofiguration params
2675
*
2676
*  @par   Description
2677
*  Populates pps structure for its use in header generation
2678
*
2679
*  @param[out]  ps_pps
2680
*  pointer to pps params structure which needs to be populated
2681
*
2682
*  @param[in]   ps_sps
2683
*  pointer to sps params refered by the pps
2684
*
2685
*  @param[in]   ps_src_params
2686
*  pointer to source config params; resolution, frame rate etc
2687
*
2688
*  @param[in]   ps_out_strm_params
2689
*  pointer to output stream config params
2690
*
2691
*  @param[in]   ps_coding_params
2692
*  pointer to coding params; to enable/disable various toolsets in pps
2693
*
2694
*  @param[in]   ps_config_prms
2695
*  pointer to configuration params like bitrate, HRD buffer sizes, cu, tu sizes
2696
*
2697
*  @return      success or failure error code
2698
*
2699
******************************************************************************
2700
*/
2701
WORD32 ihevce_populate_pps(
2702
    pps_t *ps_pps,
2703
    sps_t *ps_sps,
2704
    ihevce_src_params_t *ps_src_params,
2705
    ihevce_out_strm_params_t *ps_out_strm_params,
2706
    ihevce_coding_params_t *ps_coding_params,
2707
    ihevce_config_prms_t *ps_config_prms,
2708
    ihevce_static_cfg_params_t *ps_stat_cfg_prms,
2709
    WORD32 i4_bitrate_instance_id,
2710
    WORD32 i4_resolution_id,
2711
    ihevce_tile_params_t *ps_tile_params_base,
2712
    WORD32 *pi4_column_width_array,
2713
    WORD32 *pi4_row_height_array)
2714
261
{
2715
261
    (void)ps_src_params;
2716
261
    (void)ps_out_strm_params;
2717
2718
261
    ps_pps->i1_beta_offset_div2 = DEFAULT_BETA_OFFSET;
2719
2720
261
    ps_pps->i1_cabac_init_present_flag = CABAC_INIT_ABSENT;
2721
2722
261
    ps_pps->i1_constrained_intra_pred_flag = CONSTR_IPRED_DISABLED;
2723
    /*delta qp can be disabled for constant qp mode to save on qp signalling bits*/
2724
261
    ps_pps->i1_cu_qp_delta_enabled_flag = ps_config_prms->i4_cu_level_rc;
2725
2726
261
    ps_pps->i1_deblocking_filter_control_present_flag = DEBLOCKING_FILTER_CONTROL_PRESENT;
2727
2728
261
    ps_pps->i1_deblocking_filter_override_enabled_flag = DEBLOCKING_FILTER_OVERRIDE_DISABLED;
2729
2730
261
    ps_pps->i1_pic_disable_deblocking_filter_flag = ps_coding_params->i4_deblocking_type;
2731
2732
261
    if(0 != ps_stat_cfg_prms->s_slice_params.i4_slice_segment_mode)
2733
0
    {
2734
0
        ps_pps->i1_dependent_slice_enabled_flag = DEPENDENT_SLICE_ENABLED;
2735
0
    }
2736
261
    else
2737
261
    {
2738
261
        ps_pps->i1_dependent_slice_enabled_flag = DEPENDENT_SLICE_DISABLED;
2739
261
    }
2740
2741
    /* Assign the diff_cu_qp_delta_depth with 3,2,1 for making
2742
    CU_LEVEL_QP_MODULATION limited to 8x8, 16x16, 32x32 respectively : Lokesh */
2743
261
    ps_pps->i1_diff_cu_qp_delta_depth = CU_LEVEL_QP_LIMIT_8x8;
2744
2745
261
    if(1 == ps_coding_params->i4_enable_entropy_sync)
2746
48
    {
2747
48
        ps_pps->i1_entropy_coding_sync_enabled_flag = ENTROPY_CODING_SYNC_ENABLED;
2748
48
    }
2749
213
    else
2750
213
    {
2751
213
        ps_pps->i1_entropy_coding_sync_enabled_flag = ENTROPY_CODING_SYNC_DISABLED;
2752
213
    }
2753
2754
261
    ps_pps->i1_entropy_slice_enabled_flag = ENTROPY_SLICE_DISABLED;
2755
2756
261
    ps_pps->i1_lists_modification_present_flag = ps_coding_params->i4_weighted_pred_enable;
2757
2758
261
    ps_pps->i1_log2_parallel_merge_level = DEFAULT_PARALLEL_MERGE_LEVEL;
2759
2760
261
    ps_pps->i1_num_extra_slice_header_bits = 0;
2761
2762
    /* SAO_note_01: Currently SAO is implemented is such a way that the
2763
    loop-filter has to be enabled across syntatical-tiles and slices.
2764
    Search for <SAO_note_01> in workspace to know more */
2765
261
    ps_pps->i1_loop_filter_across_slices_enabled_flag = LF_ACROSS_SLICES_ENABLED;
2766
2767
261
    ps_pps->i1_num_ref_idx_l0_default_active = DEFAULT_NUM_REF_IDX_L0_DEFAULT_ACTIVE;
2768
2769
261
    ps_pps->i1_num_ref_idx_l1_default_active = DEFAULT_NUM_REF_IDX_L1_DEFAULT_ACTIVE;
2770
2771
261
    if(0 == ps_tile_params_base->i4_tiles_enabled_flag)
2772
261
    {
2773
261
        ps_pps->i1_num_tile_columns = NUM_TILES_COLS;
2774
2775
261
        ps_pps->i1_num_tile_rows = NUM_TILES_ROWS;
2776
2777
261
        ps_pps->i1_tiles_enabled_flag = TILES_DISABLED;
2778
2779
261
        ps_pps->i1_uniform_spacing_flag = SPACING_IS_UNIFORM;
2780
261
    }
2781
0
    else
2782
0
    {
2783
0
        ps_pps->i1_num_tile_columns = ps_tile_params_base->i4_num_tile_cols;
2784
2785
0
        ps_pps->i1_num_tile_rows = ps_tile_params_base->i4_num_tile_rows;
2786
2787
0
        ps_pps->i1_tiles_enabled_flag = TILES_ENABLED;
2788
2789
0
        ps_pps->i1_uniform_spacing_flag = ps_tile_params_base->i4_uniform_spacing_flag;
2790
2791
0
        if(SPACING_IS_NONUNIFORM == ps_pps->i1_uniform_spacing_flag)
2792
0
        {
2793
0
            WORD32 i4_i;
2794
0
            for(i4_i = 0; i4_i < ps_tile_params_base->i4_num_tile_cols; i4_i++)
2795
0
            {
2796
0
                ps_pps->ps_tile[i4_i].u2_wd = pi4_column_width_array[i4_i] >>
2797
0
                                              ps_config_prms->i4_max_log2_cu_size;
2798
0
            }
2799
0
            for(i4_i = 0; i4_i < ps_tile_params_base->i4_num_tile_rows; i4_i++)
2800
0
            {
2801
0
                ps_pps->ps_tile[i4_i].u2_ht = pi4_row_height_array[i4_i] >>
2802
0
                                              ps_config_prms->i4_max_log2_cu_size;
2803
0
            }
2804
0
        }
2805
0
    }
2806
2807
    /* SAO_note_01: Currently SAO is implemented is such a way that the
2808
    loop-filter has to be enabled across syntatical-tiles and slices.
2809
    Search for <SAO_note_01> in workspace to know more */
2810
261
    if(0 == ps_tile_params_base->i4_tiles_enabled_flag)
2811
261
    {
2812
261
        ps_pps->i1_loop_filter_across_tiles_enabled_flag = 1;
2813
261
    }
2814
0
    else
2815
0
    {
2816
0
        ps_pps->i1_loop_filter_across_tiles_enabled_flag = 0;
2817
0
    }
2818
2819
261
    ps_pps->i1_output_flag_present_flag = OUTPUT_FLAG_ABSENT;
2820
2821
261
    ps_pps->i1_pic_cb_qp_offset = DEFAULT_PIC_CB_QP_OFFSET;
2822
2823
261
    ps_pps->i1_pic_cr_qp_offset = DEFAULT_PIC_CR_QP_OFFSET;
2824
2825
    /*init qp is different for each bit-rate instance */
2826
261
    ps_pps->i1_pic_init_qp = CLIP3(
2827
261
        ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id]
2828
261
            .ai4_frame_qp[i4_bitrate_instance_id],
2829
261
        ps_config_prms->i4_min_frame_qp,
2830
261
        ps_config_prms->i4_max_frame_qp);
2831
2832
    /* enable chroma QP offset only if stasino or psy rd is present */
2833
261
    if(((ps_coding_params->i4_vqet & (1 << BITPOS_IN_VQ_TOGGLE_FOR_CONTROL_TOGGLER)) &&
2834
0
        ((ps_coding_params->i4_vqet & (1 << BITPOS_IN_VQ_TOGGLE_FOR_ENABLING_NOISE_PRESERVATION)) ||
2835
0
         (ps_coding_params->i4_vqet & (1 << BITPOS_IN_VQ_TOGGLE_FOR_ENABLING_PSYRDOPT_1)) ||
2836
0
         (ps_coding_params->i4_vqet & (1 << BITPOS_IN_VQ_TOGGLE_FOR_ENABLING_PSYRDOPT_2)) ||
2837
0
         (ps_coding_params->i4_vqet & (1 << BITPOS_IN_VQ_TOGGLE_FOR_ENABLING_PSYRDOPT_3)))))
2838
0
    {
2839
0
        ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag =
2840
0
            SLICE_LEVEL_CHROMA_QP_OFFSETS_PRESENT;
2841
0
    }
2842
261
    else
2843
261
    {
2844
261
        ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag =
2845
261
            SLICE_LEVEL_CHROMA_QP_OFFSETS_ABSENT;
2846
261
    }
2847
2848
261
    ps_pps->i1_pps_id = DEFAULT_PPS_ID;
2849
2850
261
    if(1 == ps_stat_cfg_prms->s_tgt_lyr_prms.i4_mres_single_out)
2851
0
    {
2852
0
        ps_pps->i1_pps_id = i4_resolution_id;
2853
0
    }
2854
2855
261
    ps_pps->i1_pps_scaling_list_data_present_flag = SCALING_LIST_DATA_ABSENT;
2856
2857
261
    if(ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_quality_preset <
2858
261
       IHEVCE_QUALITY_P3)
2859
130
    {
2860
130
        ps_pps->i1_sign_data_hiding_flag = SIGN_DATA_HIDDEN;
2861
130
    }
2862
131
    else if(
2863
131
        ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_quality_preset ==
2864
131
        IHEVCE_QUALITY_P3)
2865
37
    {
2866
37
        ps_pps->i1_sign_data_hiding_flag = SIGN_DATA_UNHIDDEN;
2867
37
    }
2868
94
    else
2869
94
    {
2870
94
        ps_pps->i1_sign_data_hiding_flag = SIGN_DATA_UNHIDDEN;
2871
94
    }
2872
2873
#if DISABLE_SBH
2874
    ps_pps->i1_sign_data_hiding_flag = SIGN_DATA_UNHIDDEN;
2875
#endif
2876
2877
261
    ps_pps->i1_slice_extension_present_flag = SLICE_EXTENSION_ABSENT;
2878
2879
261
    ps_pps->i1_slice_header_extension_present_flag = SLICE_HEADER_EXTENSION_ABSENT;
2880
2881
261
    ps_pps->i1_sps_id = ps_sps->i1_sps_id;
2882
2883
261
    ps_pps->i1_tc_offset_div2 = DEFAULT_TC_OFFSET;
2884
2885
261
    ps_pps->i1_transform_skip_enabled_flag = TRANSFORM_SKIP_DISABLED;
2886
2887
261
    ps_pps->i1_transquant_bypass_enable_flag = TRANSFORM_BYPASS_DISABLED;
2888
2889
261
    ps_pps->i1_weighted_bipred_flag = ps_coding_params->i4_weighted_pred_enable;
2890
2891
261
    ps_pps->i1_weighted_pred_flag = ps_coding_params->i4_weighted_pred_enable;
2892
2893
261
    return IHEVCE_SUCCESS;
2894
261
}
2895
2896
/**
2897
******************************************************************************
2898
*
2899
*  @brief Populates slice header structure
2900
*
2901
*  @par   Description
2902
*  Populates slice header structure for its use in header generation
2903
*
2904
*  @param[out]  ps_slice_hdr
2905
*  pointer to slice header structure that needs to be populated
2906
*
2907
*  @param[in]  ps_pps
2908
*  pointer to pps params structure refered by the slice
2909
*
2910
*  @param[in]   ps_sps
2911
*  pointer to sps params refered by the pps
2912
*
2913
*  @param[in]   nal_unit_type
2914
*  nal unit type for current slice
2915
*
2916
*  @param[in]   slice_type
2917
*  current slice type
2918
*
2919
*  @param[in]   ctb_x
2920
*  x offset of first ctb in current slice (ctb units)
2921
*
2922
*  @param[in]   ctb_y
2923
*  y offset of first ctb in current slice (ctb units)
2924
*
2925
*  @param[in]   poc
2926
*  pic order count for current slice (shall be 0 for IDR pics)
2927
*
2928
*  @param[in]   cur_slice_qp
2929
*  qp for the current slice
2930
*
2931
*  @return      success or failure error code
2932
*
2933
******************************************************************************
2934
*/
2935
WORD32 ihevce_populate_slice_header(
2936
    slice_header_t *ps_slice_hdr,
2937
    pps_t *ps_pps,
2938
    sps_t *ps_sps,
2939
    WORD32 nal_unit_type,
2940
    WORD32 slice_type,
2941
    WORD32 ctb_x,
2942
    WORD32 ctb_y,
2943
    WORD32 poc,
2944
    WORD32 cur_slice_qp,
2945
    WORD32 max_merge_candidates,
2946
    WORD32 i4_rc_pass_num,
2947
    WORD32 i4_quality_preset,
2948
    WORD32 stasino_enabled)
2949
6.25k
{
2950
6.25k
    WORD32 i;
2951
6.25k
    WORD32 return_status = IHEVCE_SUCCESS;
2952
6.25k
    WORD32 RapPicFlag = (nal_unit_type >= NAL_BLA_W_LP) && (nal_unit_type <= NAL_RSV_RAP_VCL23);
2953
2954
6.25k
    WORD32 idr_pic_flag = (NAL_IDR_W_LP == nal_unit_type) || (NAL_IDR_N_LP == nal_unit_type);
2955
2956
6.25k
    WORD32 disable_deblocking_filter_flag;
2957
2958
6.25k
    (void)ctb_x;
2959
6.25k
    (void)ctb_y;
2960
    /* first_slice_in_pic_flag  */
2961
6.25k
    if(i4_quality_preset == IHEVCE_QUALITY_P7)
2962
726
    {
2963
726
        i4_quality_preset = IHEVCE_QUALITY_P6;
2964
726
    }
2965
2966
6.25k
    if(RapPicFlag)
2967
957
    {
2968
        /* no_output_of_prior_pics_flag */ /* TODO:revisit this */
2969
957
        ps_slice_hdr->i1_no_output_of_prior_pics_flag = 0;  //BLU_RAY specific already done
2970
957
    }
2971
2972
    /* pic_parameter_set_id */
2973
6.25k
    ps_slice_hdr->i1_pps_id = ps_pps->i1_pps_id;
2974
2975
6.25k
    {
2976
        /* This i1_dependent_slice_flag will further be updated in generate_slice_header() function */
2977
6.25k
        ps_slice_hdr->i1_dependent_slice_flag = 0;
2978
6.25k
    }
2979
2980
6.25k
    if(!ps_slice_hdr->i1_dependent_slice_flag)
2981
6.25k
    {
2982
        /* slice_type */
2983
6.25k
        ps_slice_hdr->i1_slice_type = (WORD8)slice_type;
2984
2985
6.25k
        if(ps_pps->i1_output_flag_present_flag)
2986
0
        {
2987
            /* pic_output_flag */ /* TODO:revisit this */
2988
0
            ps_slice_hdr->i1_pic_output_flag = 0;
2989
0
        }
2990
2991
        /* separate colour plane flag not supported in this encoder */
2992
6.25k
        ASSERT(0 == ps_sps->i1_separate_colour_plane_flag);
2993
2994
6.25k
        if(!idr_pic_flag)
2995
5.45k
        {
2996
5.45k
            WORD32 log2_max_poc_lsb = ps_sps->i1_log2_max_pic_order_cnt_lsb;
2997
2998
            /* pic_order_cnt_lsb */
2999
5.45k
            ps_slice_hdr->i4_pic_order_cnt_lsb = poc & ((1 << log2_max_poc_lsb) - 1);
3000
3001
            /* short_term_ref_pic_set_sps_flag */
3002
            /* TODO : revisit this */
3003
5.45k
            ps_slice_hdr->i1_short_term_ref_pic_set_sps_flag = 0;
3004
3005
5.45k
            if(!ps_slice_hdr->i1_short_term_ref_pic_set_sps_flag)
3006
5.45k
            {
3007
                /* TODO: To populate short term ref pic set for this slice   */
3008
5.45k
            }
3009
3010
            /* long term ref pic flag not supported */
3011
5.45k
            ASSERT(0 == ps_sps->i1_long_term_ref_pics_present_flag);
3012
5.45k
            if(ps_sps->i1_long_term_ref_pics_present_flag)
3013
0
            {
3014
                /* TODO : not supported */
3015
0
            }
3016
5.45k
        }
3017
3018
        //ASSERT(0 == ps_sps->i1_sample_adaptive_offset_enabled_flag);
3019
6.25k
        if(ps_sps->i1_sample_adaptive_offset_enabled_flag)
3020
2.15k
        {
3021
            /* slice_sao_luma_flag */
3022
2.15k
            ps_slice_hdr->i1_slice_sao_luma_flag = 1;
3023
2.15k
            ps_slice_hdr->i1_slice_sao_chroma_flag = 1;
3024
2.15k
        }
3025
3026
#if DISABLE_LUMA_SAO
3027
        ps_slice_hdr->i1_slice_sao_luma_flag = 0;
3028
#endif
3029
3030
#if DISABLE_CHROMA_SAO
3031
        ps_slice_hdr->i1_slice_sao_chroma_flag = 0;
3032
#endif
3033
6.25k
        if((PSLICE == ps_slice_hdr->i1_slice_type) || (BSLICE == ps_slice_hdr->i1_slice_type))
3034
4.77k
        {
3035
            /* TODO: currently temporal mvp disabled, need to enable later */
3036
4.77k
            if(1 == ps_sps->i1_sps_temporal_mvp_enable_flag)
3037
4.77k
            {
3038
4.77k
                ps_slice_hdr->i1_slice_temporal_mvp_enable_flag = 1;
3039
4.77k
            }
3040
0
            else
3041
0
            {
3042
0
                ps_slice_hdr->i1_slice_temporal_mvp_enable_flag = 0;
3043
0
            }
3044
3045
            /* num_ref_idx_active_override_flag */
3046
4.77k
            ps_slice_hdr->i1_num_ref_idx_active_override_flag = 0;
3047
3048
4.77k
            if(ps_slice_hdr->i1_num_ref_idx_active_override_flag)
3049
0
            {
3050
                /* TODO revisit this*/
3051
                /* i1_num_ref_idx_l0_active_minus1 */
3052
0
                ps_slice_hdr->i1_num_ref_idx_l0_active = 1;
3053
3054
0
                if(BSLICE == ps_slice_hdr->i1_slice_type)
3055
0
                {
3056
                    /* i1_num_ref_idx_l1_active */
3057
                    /* TODO revisit this*/
3058
0
                    ps_slice_hdr->i1_num_ref_idx_l1_active = 1;
3059
0
                }
3060
0
            }
3061
3062
4.77k
            if(BSLICE == ps_slice_hdr->i1_slice_type)
3063
783
            {
3064
                /* mvd_l1_zero_flag */
3065
783
                ps_slice_hdr->i1_mvd_l1_zero_flag = 0;
3066
783
            }
3067
3068
4.77k
            {
3069
                /* cabac_init_flag curently set to 0 */
3070
4.77k
                ps_slice_hdr->i1_cabac_init_flag = ps_pps->i1_cabac_init_present_flag ? 1 : 0;
3071
4.77k
            }
3072
3073
4.77k
            if(ps_slice_hdr->i1_slice_temporal_mvp_enable_flag)
3074
4.77k
            {
3075
4.77k
                if(BSLICE == ps_slice_hdr->i1_slice_type)
3076
783
                {
3077
                    /* collocated_from_l0_flag */
3078
783
                    ps_slice_hdr->i1_collocated_from_l0_flag = 0;
3079
783
                }
3080
3.98k
                else if(PSLICE == ps_slice_hdr->i1_slice_type)
3081
3.98k
                {
3082
3.98k
                    ps_slice_hdr->i1_collocated_from_l0_flag = 1;
3083
3.98k
                }
3084
3085
4.77k
                if((ps_slice_hdr->i1_collocated_from_l0_flag &&
3086
3.98k
                    (ps_slice_hdr->i1_num_ref_idx_l0_active > 1)) ||
3087
4.77k
                   (!ps_slice_hdr->i1_collocated_from_l0_flag &&
3088
783
                    (ps_slice_hdr->i1_num_ref_idx_l1_active > 1)))
3089
0
                {
3090
                    /* collocated_ref_idx */
3091
                    /* TODO revisit this*/
3092
0
                    ps_slice_hdr->i1_collocated_ref_idx = 0;
3093
                    //ps_slice_hdr->i1_num_ref_idx_l1_active - 1;
3094
0
                }
3095
4.77k
            }
3096
4.77k
        }
3097
6.25k
        ps_slice_hdr->i1_max_num_merge_cand = max_merge_candidates;
3098
3099
        /* TODO : revisit this */
3100
6.25k
        ps_slice_hdr->i1_slice_qp_delta = (WORD8)cur_slice_qp - ps_pps->i1_pic_init_qp;
3101
3102
6.25k
        if(!ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag || !stasino_enabled)
3103
6.25k
        {
3104
            /* slice_cb_qp_offset */
3105
6.25k
            ps_slice_hdr->i1_slice_cb_qp_offset = 0;
3106
3107
            /* slice_cr_qp_offset */
3108
6.25k
            ps_slice_hdr->i1_slice_cr_qp_offset = 0;
3109
6.25k
        }
3110
0
        else /* only noisy regions have lower Chroma QP rating */
3111
0
        {
3112
0
            ps_slice_hdr->i1_slice_cb_qp_offset = -2;
3113
0
            ps_slice_hdr->i1_slice_cr_qp_offset = -2;
3114
0
        }
3115
3116
6.25k
        if(ps_pps->i1_deblocking_filter_control_present_flag)
3117
6.25k
        {
3118
6.25k
            ps_slice_hdr->i1_deblocking_filter_override_flag = 0;
3119
3120
6.25k
            if(ps_pps->i1_deblocking_filter_override_enabled_flag)
3121
0
            {
3122
                /* deblocking_filter_override_flag */
3123
0
                ps_slice_hdr->i1_deblocking_filter_override_flag = 0;
3124
0
            }
3125
3126
6.25k
            if(ps_slice_hdr->i1_deblocking_filter_override_flag)
3127
0
            {
3128
                /* slice_disable_deblocking_filter_flag */
3129
0
                ps_slice_hdr->i1_slice_disable_deblocking_filter_flag = DISABLE_DEBLOCKING_FLAG;
3130
3131
0
                if(!ps_slice_hdr->i1_slice_disable_deblocking_filter_flag)
3132
0
                {
3133
                    /* beta_offset_div2 */
3134
0
                    ps_slice_hdr->i1_beta_offset_div2 = 0;
3135
3136
                    /* tc_offset_div2 */
3137
0
                    ps_slice_hdr->i1_tc_offset_div2 = 0;
3138
0
                }
3139
0
            }
3140
6.25k
        }
3141
3142
6.25k
        disable_deblocking_filter_flag = ps_slice_hdr->i1_slice_disable_deblocking_filter_flag |
3143
6.25k
                                         ps_pps->i1_pic_disable_deblocking_filter_flag;
3144
3145
6.25k
        if(ps_pps->i1_loop_filter_across_slices_enabled_flag &&
3146
6.25k
           (ps_slice_hdr->i1_slice_sao_luma_flag || ps_slice_hdr->i1_slice_sao_chroma_flag ||
3147
4.09k
            !disable_deblocking_filter_flag))
3148
4.57k
        {
3149
            /* slice_loop_filter_across_slices_enabled_flag */
3150
4.57k
            ps_slice_hdr->i1_slice_loop_filter_across_slices_enabled_flag = 1;
3151
4.57k
        }
3152
6.25k
    }
3153
3154
6.25k
    if(1 == ps_pps->i1_entropy_coding_sync_enabled_flag)
3155
797
    {
3156
        /* num_entry_point_offsets, same as NUM of ctb rows to enable entropy sync at start of every CTB */
3157
797
        ps_slice_hdr->i4_num_entry_point_offsets = ps_sps->i2_pic_ht_in_ctb - 1;
3158
3159
797
        if(ps_slice_hdr->i4_num_entry_point_offsets > 0)
3160
797
        {
3161
            /* generate offset_len here */
3162
            /* fixing the offset lenght assuming 4kx2k is log2(w * h / num_ctb_row) = 20*/
3163
797
            ps_slice_hdr->i1_offset_len = 24;
3164
797
        }
3165
797
    }
3166
5.45k
    else
3167
5.45k
    {
3168
5.45k
        ps_slice_hdr->i4_num_entry_point_offsets = 0;
3169
5.45k
        ps_slice_hdr->i1_offset_len = 0;
3170
5.45k
    }
3171
3172
    /* slice_header_extension_present_flag not supported */
3173
    //if(ps_pps->i1_slice_header_extension_present_flag)
3174
6.25k
    {
3175
        /* slice_header_extension_length */
3176
6.25k
        ps_slice_hdr->i2_slice_header_extension_length = 0;
3177
3178
6.25k
        for(i = 0; i < ps_slice_hdr->i2_slice_header_extension_length; i++)
3179
0
        {
3180
            /* slice_header_extension_data_byte[i] */
3181
0
        }
3182
6.25k
    }
3183
3184
    /* TODO : hard coding ref pix set for now                           */
3185
    /* Need to update this once the ref pics are known from lap output  */
3186
3187
    /*  NOTE                                                            */
3188
    /* inter ref pic prediction is too much of logic for few bit savings*/
3189
    /* at slice header level this is not supported by the encoder       */
3190
6.25k
    ps_slice_hdr->s_stref_picset.i1_inter_ref_pic_set_prediction_flag = 0;
3191
3192
    /* hardcoding 1 ref pic for now ..... will be updated base on lap output */
3193
6.25k
    ps_slice_hdr->s_stref_picset.i1_num_delta_pocs = 1;
3194
6.25k
    ps_slice_hdr->s_stref_picset.i1_num_neg_pics = 1;
3195
6.25k
    ps_slice_hdr->s_stref_picset.i1_num_pos_pics = 0;
3196
3197
6.25k
    memset(
3198
6.25k
        ps_slice_hdr->s_stref_picset.ai2_delta_poc,
3199
6.25k
        0,
3200
6.25k
        MAX_DPB_SIZE * sizeof(*ps_slice_hdr->s_stref_picset.ai2_delta_poc));
3201
6.25k
    ps_slice_hdr->s_stref_picset.ai2_delta_poc[0] = 1;
3202
3203
6.25k
    return return_status;
3204
6.25k
}
3205
3206
/**
3207
******************************************************************************
3208
*
3209
*  @brief Insert entry point offset
3210
*
3211
*  @par   Description
3212
*  Insert entry point offset in slice header after frame processing is done 7.3.5.1
3213
*
3214
*  @param[inout]   ps_bitstrm
3215
*  pointer to bitstream context for generating slice header
3216
*
3217
*  @param[in]   i1_nal_unit_type
3218
*  nal unit type
3219
*
3220
*  @param[in]   ps_slice_hdr
3221
*  pointer to slice header params
3222
*
3223
*  @param[in]   ps_pps
3224
*  pointer to pps params referred by slice
3225
*
3226
*  @param[in]   ps_sps
3227
*  pointer to sps params referred by slice
3228
*
3229
*  @return      success or failure error code
3230
*
3231
******************************************************************************
3232
*/
3233
WORD32 ihevce_insert_entry_offset_slice_header(
3234
    bitstrm_t *ps_bitstrm,
3235
    slice_header_t *ps_slice_hdr,
3236
    pps_t *ps_pps,
3237
    UWORD32 u4_first_slice_start_offset)
3238
797
{
3239
797
    WORD32 i;
3240
797
    WORD32 return_status = IHEVCE_SUCCESS;
3241
797
    UWORD32 max_offset = 0, offset_len = 0, num_bytes_shift = 0;
3242
    /*entire slice data has to be shifted*/
3243
797
    num_bytes_shift =
3244
797
        ps_slice_hdr->pu4_entry_point_offset[ps_slice_hdr->i4_num_entry_point_offsets + 1] -
3245
797
        ps_slice_hdr->pu4_entry_point_offset[0];
3246
    /*generate relative offset*/
3247
2.24k
    for(i = 0; i < ps_slice_hdr->i4_num_entry_point_offsets; i++)
3248
1.44k
    {
3249
1.44k
        ps_slice_hdr->pu4_entry_point_offset[i] =
3250
1.44k
            ps_slice_hdr->pu4_entry_point_offset[i + 1] - ps_slice_hdr->pu4_entry_point_offset[i];
3251
1.44k
        if(ps_slice_hdr->pu4_entry_point_offset[i] > (WORD32)max_offset)
3252
933
        {
3253
933
            max_offset = ps_slice_hdr->pu4_entry_point_offset[i];
3254
933
        }
3255
1.44k
    }
3256
19.5k
    while(1)
3257
19.5k
    {
3258
19.5k
        if(max_offset & 0x80000000)
3259
797
        {
3260
797
            break;
3261
797
        }
3262
18.7k
        max_offset <<= 1;
3263
18.7k
        offset_len++;
3264
18.7k
    }
3265
797
    offset_len = 32 - offset_len;
3266
797
    ps_slice_hdr->i1_offset_len = offset_len;
3267
3268
797
    if(ps_slice_hdr->i4_num_entry_point_offsets > 0)
3269
797
    {
3270
        /* offset_len_minus1 */
3271
797
        PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_offset_len - 1, return_status);
3272
797
        ENTROPY_TRACE("offset_len_minus1", ps_slice_hdr->i1_offset_len - 1);
3273
797
    }
3274
3275
2.24k
    for(i = 0; i < ps_slice_hdr->i4_num_entry_point_offsets; i++)
3276
1.44k
    {
3277
        /* entry_point_offset[i] */
3278
        /* entry point offset minus1 is indicated in 10.0 */
3279
1.44k
        PUT_BITS(
3280
1.44k
            ps_bitstrm,
3281
1.44k
            ps_slice_hdr->pu4_entry_point_offset[i] - 1,
3282
1.44k
            ps_slice_hdr->i1_offset_len,
3283
1.44k
            return_status);
3284
1.44k
        ENTROPY_TRACE("entry_point_offset[i]", ps_slice_hdr->pu4_entry_point_offset[i]);
3285
1.44k
    }
3286
3287
797
    if(ps_pps->i1_slice_header_extension_present_flag)
3288
0
    {
3289
        /* slice_header_extension_length */
3290
0
        PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i2_slice_header_extension_length, return_status);
3291
0
        ENTROPY_TRACE(
3292
0
            "slice_header_extension_length", ps_slice_hdr->i2_slice_header_extension_length);
3293
        /*calculate slice header extension length to fill in the gap*/
3294
3295
0
        for(i = 0; i < ps_slice_hdr->i2_slice_header_extension_length; i++)
3296
0
        {
3297
            /* slice_header_extension_data_byte[i] */
3298
0
            PUT_BITS(ps_bitstrm, 0xFF, 8, return_status);
3299
0
            ENTROPY_TRACE("slice_header_extension_data_byte[i]", 0);
3300
0
        }
3301
0
    }
3302
3303
797
    BYTE_ALIGNMENT(ps_bitstrm);
3304
3305
797
    ASSERT(num_bytes_shift > 0);
3306
    /* copy the bitstream to point where header data has ended*/
3307
797
    memmove(
3308
797
        (UWORD8 *)(ps_bitstrm->pu1_strm_buffer + ps_bitstrm->u4_strm_buf_offset),
3309
797
        (UWORD8 *)(ps_bitstrm->pu1_strm_buffer + u4_first_slice_start_offset),
3310
797
        num_bytes_shift);
3311
3312
    /*send feedback of actual bytes generated*/
3313
797
    ps_bitstrm->u4_strm_buf_offset += num_bytes_shift;
3314
3315
    //ASSERT(ps_bitstrm->u4_strm_buf_offset == u4_first_slice_start_offset);
3316
797
    return return_status;
3317
797
}