Coverage Report

Created: 2026-07-16 07:11

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
207
#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
4.82k
{
147
4.82k
    WORD32 return_status = IHEVCE_SUCCESS;
148
149
    /* sanity checks */
150
4.82k
    ASSERT((nal_unit_type >= 0) && (nal_unit_type < 64));
151
4.82k
    ASSERT((nuh_temporal_id >= 0) && (nuh_temporal_id < 7));
152
153
    /* forbidden_zero_bit + nal_unit_type */
154
4.82k
    PUT_BITS(
155
4.82k
        ps_bitstrm,
156
4.82k
        nal_unit_type,
157
4.82k
        (1 + 6), /*extra 1 is for forbidden zero bit */
158
4.82k
        return_status);
159
160
    /* nuh_reserved_zero_6bits */
161
4.82k
    PUT_BITS(ps_bitstrm, 0, 6, return_status);
162
163
    /* nuh_temporal_id_plus1 */
164
4.82k
    PUT_BITS(ps_bitstrm, (nuh_temporal_id + 1), 3, return_status);
165
166
4.82k
    return (return_status);
167
4.82k
}
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
1.34k
{
191
1.34k
    WORD32 return_status = IHEVCE_SUCCESS;
192
1.34k
    WORD32 i;
193
194
    /* XXX_profile_space[] */
195
1.34k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_profile_space, 2, return_status);
196
1.34k
    ENTROPY_TRACE("XXX_profile_space[]", ps_ptl->i1_profile_space);
197
198
    /* XXX_tier_flag[] */
199
1.34k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_tier_flag, 1, return_status);
200
1.34k
    ENTROPY_TRACE("XXX_tier_flag[]", ps_ptl->i1_tier_flag);
201
202
    /* XXX_profile_idc[] */
203
1.34k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_profile_idc, 5, return_status);
204
1.34k
    ENTROPY_TRACE("XXX_profile_idc[]", ps_ptl->i1_profile_idc);
205
206
44.4k
    for(i = 0; i < MAX_PROFILE_COMPATBLTY; i++)
207
43.1k
    {
208
        /* XXX_profile_compatibility_flag[][j] */
209
43.1k
        PUT_BITS(ps_bitstrm, ps_ptl->ai1_profile_compatibility_flag[i], 1, return_status);
210
43.1k
        ENTROPY_TRACE(
211
43.1k
            "XXX_profile_compatibility_flag[][j]", ps_ptl->ai1_profile_compatibility_flag[i]);
212
43.1k
    }
213
214
    /* XXX_progressive_source_flag[] */
215
1.34k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_progressive_source_flag, 1, return_status);
216
1.34k
    ENTROPY_TRACE("XXX_progressive_source_flag[]", ps_ptl->i1_general_progressive_source_flag);
217
218
    /* XXX_interlaced_source_flag[] */
219
1.34k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_interlaced_source_flag, 1, return_status);
220
1.34k
    ENTROPY_TRACE("XXX_interlaced_source_flag[]", ps_ptl->i1_general_interlaced_source_flag);
221
222
    /* XXX_non_packed_constraint_flag[] */
223
1.34k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_non_packed_constraint_flag, 1, return_status);
224
1.34k
    ENTROPY_TRACE(
225
1.34k
        "XXX_non_packed_constraint_flag[]", ps_ptl->i1_general_non_packed_constraint_flag);
226
227
    /* XXX_frame_only_constraint_flag[] */
228
1.34k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_frame_only_constraint_flag, 1, return_status);
229
1.34k
    ENTROPY_TRACE("XXX_frame_only_constraint_flag[]", ps_ptl->i1_frame_only_constraint_flag);
230
231
    /* XXX_general_max_12bit_constraint_flag[] */
232
1.34k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_max_12bit_constraint_flag, 1, return_status);
233
1.34k
    ENTROPY_TRACE(
234
1.34k
        "XXX_general_max_12bit_constraint_flag[]", ps_ptl->i1_general_max_12bit_constraint_flag);
235
236
    /* XXX_general_max_10bit_constraint_flag[] */
237
1.34k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_max_10bit_constraint_flag, 1, return_status);
238
1.34k
    ENTROPY_TRACE(
239
1.34k
        "XXX_general_max_10bit_constraint_flag[]", ps_ptl->i1_general_max_10bit_constraint_flag);
240
241
    /* XXX_general_max_8bit_constraint_flag[] */
242
1.34k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_max_8bit_constraint_flag, 1, return_status);
243
1.34k
    ENTROPY_TRACE(
244
1.34k
        "XXX_general_max_8bit_constraint_flag[]", ps_ptl->i1_general_max_8bit_constraint_flag);
245
246
    /* XXX_general_max_422chroma_constraint_flag[] */
247
1.34k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_max_422chroma_constraint_flag, 1, return_status);
248
1.34k
    ENTROPY_TRACE(
249
1.34k
        "XXX_general_max_422chroma_constraint_flag[]",
250
1.34k
        ps_ptl->i1_general_max_422chroma_constraint_flag);
251
252
    /* XXX_general_max_420chroma_constraint_flag[] */
253
1.34k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_max_420chroma_constraint_flag, 1, return_status);
254
1.34k
    ENTROPY_TRACE(
255
1.34k
        "XXX_general_max_420chroma_constraint_flag[]",
256
1.34k
        ps_ptl->i1_general_max_420chroma_constraint_flag);
257
258
    /* XXX_general_max_monochrome_constraint_flag[] */
259
1.34k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_max_monochrome_constraint_flag, 1, return_status);
260
1.34k
    ENTROPY_TRACE(
261
1.34k
        "XXX_general_max_monochrome_constraint_flag[]",
262
1.34k
        ps_ptl->i1_general_max_monochrome_constraint_flag);
263
264
    /* XXX_general_intra_constraint_flag[] */
265
1.34k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_intra_constraint_flag, 1, return_status);
266
1.34k
    ENTROPY_TRACE("XXX_general_intra_constraint_flag[]", ps_ptl->i1_general_intra_constraint_flag);
267
268
    /* XXX_general_one_picture_only_constraint_flag[] */
269
1.34k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_one_picture_only_constraint_flag, 1, return_status);
270
1.34k
    ENTROPY_TRACE(
271
1.34k
        "XXX_general_one_picture_only_constraint_flag[]",
272
1.34k
        ps_ptl->i1_general_one_picture_only_constraint_flag);
273
274
    /* XXX_general_lower_bit_rate_constraint_flag[] */
275
1.34k
    PUT_BITS(ps_bitstrm, ps_ptl->i1_general_lower_bit_rate_constraint_flag, 1, return_status);
276
1.34k
    ENTROPY_TRACE(
277
1.34k
        "XXX_general_lower_bit_rate_constraint_flag[]",
278
1.34k
        ps_ptl->i1_general_lower_bit_rate_constraint_flag);
279
280
    /* XXX_reserved_zero_35bits[] */
281
1.34k
    PUT_BITS(ps_bitstrm, 0, 16, return_status);
282
1.34k
    PUT_BITS(ps_bitstrm, 0, 16, return_status);
283
1.34k
    PUT_BITS(ps_bitstrm, 0, 3, return_status);
284
1.34k
    ENTROPY_TRACE("XXX_reserved_zero_35bits[]", 0);
285
286
1.34k
    return return_status;
287
1.34k
}
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
1.34k
{
319
1.34k
    WORD32 i;
320
1.34k
    WORD32 return_status = IHEVCE_SUCCESS;
321
322
1.34k
    if(i1_profile_present_flag)
323
1.34k
    {
324
1.34k
        ihevce_generate_when_profile_present(ps_bitstrm, &ps_ptl->s_ptl_gen);
325
1.34k
    }
326
327
    /* general_level_idc */
328
1.34k
    PUT_BITS(ps_bitstrm, ps_ptl->s_ptl_gen.u1_level_idc, 8, return_status);
329
1.34k
    ENTROPY_TRACE("general_level_idc", ps_ptl->s_ptl_gen.u1_level_idc);
330
331
1.34k
    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
1.34k
    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
1.34k
    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
1.34k
    return return_status;
370
1.34k
}
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
2.34k
{
406
2.34k
    WORD32 i;
407
2.34k
    WORD32 return_status = IHEVCE_SUCCESS;
408
2.34k
    stref_picset_t *ps_stref_picset = ps_stref_picset_base + idx;
409
410
2.34k
    (void)num_short_term_ref_pic_sets;
411
2.34k
    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
2.34k
    ASSERT(0 == ps_stref_picset->i1_inter_ref_pic_set_prediction_flag);
423
424
    /* num_negative_pics */
425
2.34k
    PUT_BITS_UEV(ps_bitstrm, ps_stref_picset->i1_num_neg_pics, return_status);
426
2.34k
    ENTROPY_TRACE("num_negative_pics", ps_stref_picset->i1_num_neg_pics);
427
428
    /* num_positive_pics */
429
2.34k
    PUT_BITS_UEV(ps_bitstrm, ps_stref_picset->i1_num_pos_pics, return_status);
430
2.34k
    ENTROPY_TRACE("num_positive_pics", ps_stref_picset->i1_num_pos_pics);
431
432
9.50k
    for(i = 0; i < ps_stref_picset->i1_num_neg_pics; i++)
433
7.16k
    {
434
        /* delta_poc_s0_minus1 */
435
7.16k
        PUT_BITS_UEV(ps_bitstrm, ps_stref_picset->ai2_delta_poc[i] - 1, return_status);
436
7.16k
        ENTROPY_TRACE("delta_poc_s0_minus1", ps_stref_picset->ai2_delta_poc[i] - 1);
437
438
        /* used_by_curr_pic_s0_flag */
439
7.16k
        PUT_BITS(ps_bitstrm, ps_stref_picset->ai1_used[i], 1, return_status);
440
7.16k
        ENTROPY_TRACE("used_by_curr_pic_s0_flag", ps_stref_picset->ai1_used[i]);
441
        /*get the num pocs used for cur pic*/
442
7.16k
        if(ps_stref_picset->ai1_used[i])
443
6.92k
        {
444
6.92k
            *pi4_NumPocTotalCurr += 1;
445
6.92k
        }
446
7.16k
    }
447
448
2.54k
    for(; i < (ps_stref_picset->i1_num_pos_pics + ps_stref_picset->i1_num_neg_pics); i++)
449
206
    {
450
        /* delta_poc_s1_minus1 */
451
206
        PUT_BITS_UEV(ps_bitstrm, ps_stref_picset->ai2_delta_poc[i] - 1, return_status);
452
206
        ENTROPY_TRACE("delta_poc_s1_minus1", ps_stref_picset->ai2_delta_poc[i] - 1);
453
454
        /* used_by_curr_pic_s1_flag */
455
206
        PUT_BITS(ps_bitstrm, ps_stref_picset->ai1_used[i], 1, return_status);
456
206
        ENTROPY_TRACE("used_by_curr_pic_s1_flag", ps_stref_picset->ai1_used[i]);
457
        /*get the num pocs used for cur pic*/
458
206
        if(ps_stref_picset->ai1_used[i])
459
206
        {
460
206
            *pi4_NumPocTotalCurr += 1;
461
206
        }
462
206
    }
463
464
2.34k
    return return_status;
465
2.34k
}
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
674
{
832
674
    WORD32 i;
833
674
    WORD8 i1_vps_max_sub_layers_minus1 = ps_vps->i1_vps_max_sub_layers - 1;
834
674
    WORD32 return_status = IHEVCE_SUCCESS;
835
836
    /* Insert Start Code */
837
674
    ihevce_put_nal_start_code_prefix(ps_bitstrm, 1);
838
839
    /* Insert Nal Unit Header */
840
674
    ihevce_generate_nal_unit_header(ps_bitstrm, NAL_VPS, 0);
841
842
    /* video_parameter_set_id */
843
674
    PUT_BITS(ps_bitstrm, ps_vps->i1_vps_id, 4, return_status);
844
674
    ENTROPY_TRACE("video_parameter_set_id", ps_vps->i1_vps_id);
845
846
    /* vps_reserved_three_2bits */
847
674
    PUT_BITS(ps_bitstrm, 3, 2, return_status);
848
674
    ENTROPY_TRACE("vps_reserved_three_2bits", 3);
849
850
    /* vps_max_layers_minus1  */
851
674
    PUT_BITS(ps_bitstrm, 0, 6, return_status);
852
674
    ENTROPY_TRACE("vps_max_layers_minus1 ", 3);
853
854
    /* vps_max_sub_layers_minus1 */
855
674
    PUT_BITS(ps_bitstrm, i1_vps_max_sub_layers_minus1, 3, return_status);
856
674
    ENTROPY_TRACE("vps_max_sub_layers_minus1", i1_vps_max_sub_layers_minus1);
857
858
    /* vps_temporal_id_nesting_flag */
859
674
    PUT_BITS(ps_bitstrm, ps_vps->i1_vps_temporal_id_nesting_flag, 1, return_status);
860
674
    ENTROPY_TRACE("vps_temporal_id_nesting_flag", ps_vps->i1_vps_temporal_id_nesting_flag);
861
862
    /* vps_reserved_0xffff_16bits */
863
674
    PUT_BITS(ps_bitstrm, 0xffff, 16, return_status);
864
674
    ENTROPY_TRACE("vps_reserved_0xffff_16bits", 0xffff);
865
866
    /* profile-tier and level info */
867
674
    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
674
    PUT_BITS(ps_bitstrm, ps_vps->i1_sub_layer_ordering_info_present_flag, 1, return_status);
871
674
    ENTROPY_TRACE(
872
674
        "vps_sub_layer_ordering_info_present_flag",
873
674
        ps_vps->i1_sub_layer_ordering_info_present_flag);
874
875
674
    i = ps_vps->i1_sub_layer_ordering_info_present_flag ? 0 : i1_vps_max_sub_layers_minus1;
876
877
1.34k
    for(; i <= i1_vps_max_sub_layers_minus1; i++)
878
674
    {
879
        /* vps_max_dec_pic_buffering[i] */
880
674
        PUT_BITS_UEV(ps_bitstrm, ps_vps->ai1_vps_max_dec_pic_buffering[i], return_status);
881
674
        ENTROPY_TRACE(
882
674
            "vps_max_dec_pic_buffering_minus1[i]", ps_vps->ai1_vps_max_dec_pic_buffering[i]);
883
884
        /* vps_num_reorder_pics[i] */
885
674
        PUT_BITS_UEV(ps_bitstrm, ps_vps->ai1_vps_max_num_reorder_pics[i], return_status);
886
674
        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
674
        PUT_BITS_UEV(ps_bitstrm, ps_vps->ai1_vps_max_latency_increase[i], return_status);
890
674
        ENTROPY_TRACE("ai1_vps_max_latency_increase[i]", ps_vps->ai1_vps_max_latency_increase[i]);
891
674
    }
892
893
    /* vps_max_layer_id */
894
674
    PUT_BITS(ps_bitstrm, ps_vps->i1_vps_max_nuh_reserved_zero_layer_id, 6, return_status);
895
674
    ENTROPY_TRACE("vps_max_layer_id", ps_vps->i1_vps_max_nuh_reserved_zero_layer_id);
896
897
    /* vps_num_layer_sets_minus1 */
898
674
    PUT_BITS_UEV(ps_bitstrm, 0, return_status);
899
674
    ENTROPY_TRACE("vps_num_layer_sets_minus1", 0);
900
901
    /* vps_timing_info_present_flag */
902
674
    PUT_BITS(ps_bitstrm, 0, 1, return_status);
903
674
    ENTROPY_TRACE("vps_timing_info_present_flag", 0);
904
905
    /* vps_extension_flag */
906
674
    PUT_BITS(ps_bitstrm, 0, 1, return_status);
907
674
    ENTROPY_TRACE("vps_extension_flag", 0);
908
909
    /* rbsp trailing bits */
910
674
    ihevce_put_rbsp_trailing_bits(ps_bitstrm);
911
912
674
    return return_status;
913
674
}
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
674
{
935
674
    WORD32 i;
936
674
    WORD32 return_status = IHEVCE_SUCCESS;
937
674
    WORD8 i1_max_sub_layers_minus1 = ps_sps->i1_sps_max_sub_layers - 1;
938
939
674
    UWORD32 u4_log2_max_pic_order_cnt_lsb = (UWORD32)(ps_sps->i1_log2_max_pic_order_cnt_lsb);
940
941
674
    UWORD32 u4_log2_min_coding_block_size_minus3 =
942
674
        (UWORD32)(ps_sps->i1_log2_min_coding_block_size) - 3;
943
944
674
    UWORD32 u4_log2_diff_max_min_coding_block_size =
945
674
        (UWORD32)(ps_sps->i1_log2_diff_max_min_coding_block_size);
946
947
674
    UWORD32 u4_log2_min_transform_block_size_minus2 =
948
674
        (UWORD32)(ps_sps->i1_log2_min_transform_block_size) - 2;
949
950
674
    UWORD32 u4_log2_diff_max_min_transform_block_size =
951
674
        (UWORD32)(ps_sps->i1_log2_diff_max_min_transform_block_size);
952
953
    /* Insert Start Code */
954
674
    return_status = ihevce_put_nal_start_code_prefix(ps_bitstrm, 1);
955
956
    /* Insert Nal Unit Header */
957
674
    return_status |= ihevce_generate_nal_unit_header(ps_bitstrm, NAL_SPS, 0);
958
959
    /* video_parameter_set_id */
960
674
    PUT_BITS(ps_bitstrm, ps_sps->i1_vps_id, 4, return_status);
961
674
    ENTROPY_TRACE("video_parameter_set_id", ps_sps->i1_vps_id);
962
963
    /* sps_max_sub_layers_minus1 */
964
674
    PUT_BITS(ps_bitstrm, i1_max_sub_layers_minus1, 3, return_status);
965
674
    ENTROPY_TRACE("sps_max_sub_layers_minus1", i1_max_sub_layers_minus1);
966
967
    /* sps_temporal_id_nesting_flag */
968
674
    PUT_BITS(ps_bitstrm, ps_sps->i1_sps_temporal_id_nesting_flag, 1, return_status);
969
674
    ENTROPY_TRACE("sps_temporal_id_nesting_flag", ps_sps->i1_sps_temporal_id_nesting_flag);
970
971
    /* profile-tier and level info */
972
674
    ihevce_generate_profile_tier_level(ps_bitstrm, &ps_sps->s_ptl, 1, i1_max_sub_layers_minus1);
973
974
    /* seq_parameter_set_id */
975
674
    PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_sps_id, return_status);
976
674
    ENTROPY_TRACE("seq_parameter_set_id", ps_sps->i1_sps_id);
977
978
    /* chroma_format_idc */
979
674
    PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_chroma_format_idc, return_status);
980
674
    ENTROPY_TRACE("chroma_format_idc", ps_sps->i1_chroma_format_idc);
981
982
674
    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
674
    PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_width_in_luma_samples, return_status);
991
674
    ENTROPY_TRACE("pic_width_in_luma_samples", ps_sps->i2_pic_width_in_luma_samples);
992
993
    /* pic_height_in_luma_samples */
994
674
    PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_height_in_luma_samples, return_status);
995
674
    ENTROPY_TRACE("pic_height_in_luma_samples", ps_sps->i2_pic_height_in_luma_samples);
996
997
    /* pic_cropping_flag */
998
674
    PUT_BITS(ps_bitstrm, ps_sps->i1_pic_cropping_flag, 1, return_status);
999
674
    ENTROPY_TRACE("pic_cropping_flag", ps_sps->i1_pic_cropping_flag);
1000
1001
674
    if(ps_sps->i1_pic_cropping_flag)
1002
674
    {
1003
        /* pic_crop_left_offset */
1004
674
        PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_crop_left_offset, return_status);
1005
674
        ENTROPY_TRACE("pic_crop_left_offset", ps_sps->i2_pic_crop_left_offset);
1006
1007
        /* pic_crop_right_offset */
1008
674
        PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_crop_right_offset, return_status);
1009
674
        ENTROPY_TRACE("pic_crop_right_offset", ps_sps->i2_pic_crop_right_offset);
1010
1011
        /* pic_crop_top_offset */
1012
674
        PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_crop_top_offset, return_status);
1013
674
        ENTROPY_TRACE("pic_crop_top_offset", ps_sps->i2_pic_crop_top_offset);
1014
1015
        /* pic_crop_bottom_offset */
1016
674
        PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_crop_bottom_offset, return_status);
1017
674
        ENTROPY_TRACE("pic_crop_bottom_offset", ps_sps->i2_pic_crop_bottom_offset);
1018
674
    }
1019
1020
    /* bit_depth_luma_minus8 */
1021
674
    PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_bit_depth_luma_minus8, return_status);
1022
674
    ENTROPY_TRACE("bit_depth_luma_minus8", ps_sps->i1_bit_depth_luma_minus8);
1023
1024
    /* bit_depth_chroma_minus8 */
1025
674
    PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_bit_depth_chroma_minus8, return_status);
1026
674
    ENTROPY_TRACE("i1_bit_depth_chroma_minus8", ps_sps->i1_bit_depth_chroma_minus8);
1027
1028
    /* log2_max_pic_order_cnt_lsb_minus4 */
1029
674
    PUT_BITS_UEV(ps_bitstrm, u4_log2_max_pic_order_cnt_lsb - 4, return_status);
1030
674
    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
674
    PUT_BITS(ps_bitstrm, ps_sps->i1_sps_sub_layer_ordering_info_present_flag, 1, return_status);
1034
674
    ENTROPY_TRACE(
1035
674
        "sps_sub_layer_ordering_info_present_flag",
1036
674
        ps_sps->i1_sps_sub_layer_ordering_info_present_flag);
1037
1038
674
    i = ps_sps->i1_sps_sub_layer_ordering_info_present_flag ? 0 : i1_max_sub_layers_minus1;
1039
1040
1.34k
    for(; i <= i1_max_sub_layers_minus1; i++)
1041
674
    {
1042
        /* max_dec_pic_buffering */
1043
674
        PUT_BITS_UEV(ps_bitstrm, ps_sps->ai1_sps_max_dec_pic_buffering[i], return_status);
1044
674
        ENTROPY_TRACE("max_dec_pic_buffering_minus1", ps_sps->ai1_sps_max_dec_pic_buffering[i]);
1045
1046
        /* num_reorder_pics */
1047
674
        PUT_BITS_UEV(ps_bitstrm, ps_sps->ai1_sps_max_num_reorder_pics[i], return_status);
1048
674
        ENTROPY_TRACE("num_reorder_pics", ps_sps->ai1_sps_max_num_reorder_pics[i]);
1049
1050
        /* max_latency_increase */
1051
674
        PUT_BITS_UEV(ps_bitstrm, ps_sps->ai1_sps_max_latency_increase[i], return_status);
1052
674
        ENTROPY_TRACE("max_latency_increase", ps_sps->ai1_sps_max_latency_increase[i]);
1053
674
    }
1054
1055
    /* log2_min_coding_block_size_minus3 */
1056
674
    PUT_BITS_UEV(ps_bitstrm, u4_log2_min_coding_block_size_minus3, return_status);
1057
674
    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
674
    PUT_BITS_UEV(ps_bitstrm, u4_log2_diff_max_min_coding_block_size, return_status);
1061
674
    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
674
    PUT_BITS_UEV(ps_bitstrm, u4_log2_min_transform_block_size_minus2, return_status);
1065
674
    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
674
    PUT_BITS_UEV(ps_bitstrm, u4_log2_diff_max_min_transform_block_size, return_status);
1069
674
    ENTROPY_TRACE(
1070
674
        "log2_diff_max_min_transform_block_size", u4_log2_diff_max_min_transform_block_size);
1071
1072
    /* max_transform_hierarchy_depth_inter */
1073
674
    PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_max_transform_hierarchy_depth_inter, return_status);
1074
674
    ENTROPY_TRACE(
1075
674
        "max_transform_hierarchy_depth_inter", ps_sps->i1_max_transform_hierarchy_depth_inter);
1076
1077
    /* max_transform_hierarchy_depth_intra */
1078
674
    PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_max_transform_hierarchy_depth_intra, return_status);
1079
674
    ENTROPY_TRACE(
1080
674
        "max_transform_hierarchy_depth_intra", ps_sps->i1_max_transform_hierarchy_depth_intra);
1081
1082
    /* scaling_list_enabled_flag */
1083
674
    PUT_BITS(ps_bitstrm, ps_sps->i1_scaling_list_enable_flag, 1, return_status);
1084
674
    ENTROPY_TRACE("scaling_list_enabled_flag", ps_sps->i1_scaling_list_enable_flag);
1085
1086
674
    if(ps_sps->i1_scaling_list_enable_flag)
1087
258
    {
1088
        /* sps_scaling_list_data_present_flag */
1089
258
        PUT_BITS(ps_bitstrm, ps_sps->i1_sps_scaling_list_data_present_flag, 1, return_status);
1090
258
        ENTROPY_TRACE(
1091
258
            "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
258
    }
1101
1102
    /* asymmetric_motion_partitions_enabled_flag */
1103
674
    PUT_BITS(ps_bitstrm, ps_sps->i1_amp_enabled_flag, 1, return_status);
1104
674
    ENTROPY_TRACE("asymmetric_motion_partitions_enabled_flag", ps_sps->i1_amp_enabled_flag);
1105
1106
    /* sample_adaptive_offset_enabled_flag */
1107
674
    PUT_BITS(ps_bitstrm, ps_sps->i1_sample_adaptive_offset_enabled_flag, 1, return_status);
1108
674
    ENTROPY_TRACE(
1109
674
        "sample_adaptive_offset_enabled_flag", ps_sps->i1_sample_adaptive_offset_enabled_flag);
1110
1111
    /* pcm_enabled_flag */
1112
674
    PUT_BITS(ps_bitstrm, ps_sps->i1_pcm_enabled_flag, 1, return_status);
1113
674
    ENTROPY_TRACE("pcm_enabled_flag", ps_sps->i1_pcm_enabled_flag);
1114
674
    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
674
    PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_num_short_term_ref_pic_sets, return_status);
1145
674
    ENTROPY_TRACE("num_short_term_ref_pic_sets", ps_sps->i1_num_short_term_ref_pic_sets);
1146
1147
674
    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
674
    PUT_BITS(ps_bitstrm, ps_sps->i1_long_term_ref_pics_present_flag, 1, return_status);
1160
674
    ENTROPY_TRACE("long_term_ref_pics_present_flag", ps_sps->i1_long_term_ref_pics_present_flag);
1161
1162
674
    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
674
    PUT_BITS(ps_bitstrm, ps_sps->i1_sps_temporal_mvp_enable_flag, 1, return_status);
1187
674
    ENTROPY_TRACE("sps_temporal_mvp_enable_flag", ps_sps->i1_sps_temporal_mvp_enable_flag);
1188
1189
674
#if !HM_8DOT1_SYNTAX
1190
    /* strong_intra_smoothing_enable_flag */
1191
674
    PUT_BITS(ps_bitstrm, ps_sps->i1_strong_intra_smoothing_enable_flag, 1, return_status);
1192
674
    ENTROPY_TRACE(
1193
674
        "sps_strong_intra_smoothing_enable_flag", ps_sps->i1_strong_intra_smoothing_enable_flag);
1194
674
#endif
1195
1196
    /* vui_parameters_present_flag */
1197
674
    PUT_BITS(ps_bitstrm, ps_sps->i1_vui_parameters_present_flag, 1, return_status);
1198
674
    ENTROPY_TRACE("vui_parameters_present_flag", ps_sps->i1_vui_parameters_present_flag);
1199
1200
674
    ENTROPY_TRACE("----------- vui_parameters -----------", 0);
1201
1202
674
    if(ps_sps->i1_vui_parameters_present_flag)
1203
162
    {
1204
        /* Add vui parameters to the bitstream */
1205
162
        ihevce_generate_vui(ps_bitstrm, ps_sps, ps_sps->s_vui_parameters);
1206
162
    }
1207
1208
    /* sps_extension_flag */
1209
674
    PUT_BITS(ps_bitstrm, 0, 1, return_status);
1210
674
    ENTROPY_TRACE("sps_extension_flag", 0);
1211
1212
    /* rbsp trailing bits */
1213
674
    ihevce_put_rbsp_trailing_bits(ps_bitstrm);
1214
1215
674
    return return_status;
1216
674
}
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
674
{
1238
674
    WORD32 i;
1239
674
    WORD32 return_status = IHEVCE_SUCCESS;
1240
1241
    /* Insert the NAL start code */
1242
674
    return_status = ihevce_put_nal_start_code_prefix(ps_bitstrm, 1);
1243
1244
    /* Insert Nal Unit Header */
1245
674
    return_status |= ihevce_generate_nal_unit_header(ps_bitstrm, NAL_PPS, 0);
1246
1247
    /* pic_parameter_set_id */
1248
674
    PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_pps_id, return_status);
1249
674
    ENTROPY_TRACE("pic_parameter_set_id", ps_pps->i1_pps_id);
1250
1251
    /* seq_parameter_set_id */
1252
674
    PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_sps_id, return_status);
1253
674
    ENTROPY_TRACE("seq_parameter_set_id", ps_pps->i1_sps_id);
1254
1255
    /* dependent_slices_enabled_flag */
1256
674
    PUT_BITS(ps_bitstrm, ps_pps->i1_dependent_slice_enabled_flag, 1, return_status);
1257
674
    ENTROPY_TRACE("dependent_slices_enabled_flag", ps_pps->i1_dependent_slice_enabled_flag);
1258
1259
    /* output_flag_present_flag */
1260
674
    PUT_BITS(ps_bitstrm, ps_pps->i1_output_flag_present_flag, 1, return_status);
1261
674
    ENTROPY_TRACE("output_flag_present_flag", ps_pps->i1_output_flag_present_flag);
1262
1263
    /* num_extra_slice_header_bits */
1264
674
    PUT_BITS(ps_bitstrm, ps_pps->i1_num_extra_slice_header_bits, 3, return_status);
1265
674
    ENTROPY_TRACE("num_extra_slice_header_bits", ps_pps->i1_num_extra_slice_header_bits);
1266
1267
    /* sign_data_hiding_flag */
1268
674
    PUT_BITS(ps_bitstrm, ps_pps->i1_sign_data_hiding_flag, 1, return_status);
1269
674
    ENTROPY_TRACE("sign_data_hiding_flag", ps_pps->i1_sign_data_hiding_flag);
1270
1271
    /* cabac_init_present_flag */
1272
674
    PUT_BITS(ps_bitstrm, ps_pps->i1_cabac_init_present_flag, 1, return_status);
1273
674
    ENTROPY_TRACE("cabac_init_present_flag", ps_pps->i1_cabac_init_present_flag);
1274
1275
    /* num_ref_idx_l0_default_active_minus1 */
1276
674
    PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_num_ref_idx_l0_default_active - 1, return_status);
1277
674
    ENTROPY_TRACE(
1278
674
        "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
674
    PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_num_ref_idx_l1_default_active - 1, return_status);
1282
674
    ENTROPY_TRACE(
1283
674
        "num_ref_idx_l1_default_active_minus1", ps_pps->i1_num_ref_idx_l1_default_active - 1);
1284
1285
    /* pic_init_qp_minus26 */
1286
674
    PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_pic_init_qp - 26, return_status);
1287
674
    ENTROPY_TRACE("pic_init_qp_minus26", ps_pps->i1_pic_init_qp - 26);
1288
1289
    /* constrained_intra_pred_flag */
1290
674
    PUT_BITS(ps_bitstrm, ps_pps->i1_constrained_intra_pred_flag, 1, return_status);
1291
674
    ENTROPY_TRACE("constrained_intra_pred_flag", ps_pps->i1_constrained_intra_pred_flag);
1292
1293
    /* transform_skip_enabled_flag */
1294
674
    PUT_BITS(ps_bitstrm, ps_pps->i1_transform_skip_enabled_flag, 1, return_status);
1295
674
    ENTROPY_TRACE("transform_skip_enabled_flag", ps_pps->i1_transform_skip_enabled_flag);
1296
1297
    /* cu_qp_delta_enabled_flag */
1298
674
    PUT_BITS(ps_bitstrm, ps_pps->i1_cu_qp_delta_enabled_flag, 1, return_status);
1299
674
    ENTROPY_TRACE("cu_qp_delta_enabled_flag", ps_pps->i1_cu_qp_delta_enabled_flag);
1300
1301
674
    if(ps_pps->i1_cu_qp_delta_enabled_flag)
1302
312
    {
1303
        /* diff_cu_qp_delta_depth */
1304
312
        PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_diff_cu_qp_delta_depth, return_status);
1305
312
        ENTROPY_TRACE("diff_cu_qp_delta_depth", ps_pps->i1_diff_cu_qp_delta_depth);
1306
312
    }
1307
1308
    /* cb_qp_offset */
1309
674
    PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_pic_cb_qp_offset, return_status);
1310
674
    ENTROPY_TRACE("cb_qp_offset", ps_pps->i1_pic_cb_qp_offset);
1311
1312
    /* cr_qp_offset */
1313
674
    PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_pic_cr_qp_offset, return_status);
1314
674
    ENTROPY_TRACE("cr_qp_offset", ps_pps->i1_pic_cr_qp_offset);
1315
1316
    /* slicelevel_chroma_qp_flag */
1317
674
    PUT_BITS(
1318
674
        ps_bitstrm, ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag, 1, return_status);
1319
674
    ENTROPY_TRACE(
1320
674
        "slicelevel_chroma_qp_flag", ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag);
1321
1322
    /* weighted_pred_flag */
1323
674
    PUT_BITS(ps_bitstrm, ps_pps->i1_weighted_pred_flag, 1, return_status);
1324
674
    ENTROPY_TRACE("weighted_pred_flag", ps_pps->i1_weighted_pred_flag);
1325
1326
    /* weighted_bipred_flag */
1327
674
    PUT_BITS(ps_bitstrm, ps_pps->i1_weighted_bipred_flag, 1, return_status);
1328
674
    ENTROPY_TRACE("weighted_bipred_flag", ps_pps->i1_weighted_bipred_flag);
1329
1330
    /* transquant_bypass_enable_flag */
1331
674
    PUT_BITS(ps_bitstrm, ps_pps->i1_transquant_bypass_enable_flag, 1, return_status);
1332
674
    ENTROPY_TRACE("transquant_bypass_enable_flag", ps_pps->i1_transquant_bypass_enable_flag);
1333
1334
    /* tiles_enabled_flag */
1335
674
    PUT_BITS(ps_bitstrm, ps_pps->i1_tiles_enabled_flag, 1, return_status);
1336
674
    ENTROPY_TRACE("tiles_enabled_flag", ps_pps->i1_tiles_enabled_flag);
1337
1338
    /* entropy_coding_sync_enabled_flag */
1339
674
    PUT_BITS(ps_bitstrm, ps_pps->i1_entropy_coding_sync_enabled_flag, 1, return_status);
1340
674
    ENTROPY_TRACE("entropy_coding_sync_enabled_flag", ps_pps->i1_entropy_coding_sync_enabled_flag);
1341
1342
674
    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
674
    PUT_BITS(ps_bitstrm, ps_pps->i1_loop_filter_across_slices_enabled_flag, 1, return_status);
1381
674
    ENTROPY_TRACE(
1382
674
        "loop_filter_across_slices_enabled_flag",
1383
674
        ps_pps->i1_loop_filter_across_slices_enabled_flag);
1384
1385
    /* deblocking_filter_control_present_flag */
1386
674
    PUT_BITS(ps_bitstrm, ps_pps->i1_deblocking_filter_control_present_flag, 1, return_status);
1387
674
    ENTROPY_TRACE(
1388
674
        "deblocking_filter_control_present_flag",
1389
674
        ps_pps->i1_deblocking_filter_control_present_flag);
1390
1391
674
    if(ps_pps->i1_deblocking_filter_control_present_flag)
1392
674
    {
1393
        /* deblocking_filter_override_enabled_flag */
1394
674
        PUT_BITS(ps_bitstrm, ps_pps->i1_deblocking_filter_override_enabled_flag, 1, return_status);
1395
674
        ENTROPY_TRACE(
1396
674
            "deblocking_filter_override_enabled_flag",
1397
674
            ps_pps->i1_deblocking_filter_override_enabled_flag);
1398
1399
        /* pic_disable_deblocking_filter_flag */
1400
674
        PUT_BITS(ps_bitstrm, ps_pps->i1_pic_disable_deblocking_filter_flag, 1, return_status);
1401
674
        ENTROPY_TRACE(
1402
674
            "pic_disable_deblocking_filter_flag", ps_pps->i1_pic_disable_deblocking_filter_flag);
1403
1404
674
        if(!ps_pps->i1_pic_disable_deblocking_filter_flag)
1405
503
        {
1406
            /* beta_offset_div2 */
1407
503
            PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_beta_offset_div2 >> 1, return_status);
1408
503
            ENTROPY_TRACE("beta_offset_div2", ps_pps->i1_beta_offset_div2 >> 1);
1409
1410
            /* tc_offset_div2 */
1411
503
            PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_tc_offset_div2 >> 1, return_status);
1412
503
            ENTROPY_TRACE("tc_offset_div2", ps_pps->i1_tc_offset_div2 >> 1);
1413
503
        }
1414
674
    }
1415
1416
    /* pps_scaling_list_data_present_flag */
1417
674
    PUT_BITS(ps_bitstrm, ps_pps->i1_pps_scaling_list_data_present_flag, 1, return_status);
1418
674
    ENTROPY_TRACE(
1419
674
        "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
674
    PUT_BITS(ps_bitstrm, ps_pps->i1_lists_modification_present_flag, 1, return_status);
1431
674
    ENTROPY_TRACE("lists_modification_present_flag", ps_pps->i1_lists_modification_present_flag);
1432
1433
674
    {
1434
674
        UWORD32 u4_log2_parallel_merge_level_minus2 = ps_pps->i1_log2_parallel_merge_level;
1435
1436
674
        u4_log2_parallel_merge_level_minus2 -= 2;
1437
1438
        /* log2_parallel_merge_level_minus2 */
1439
674
        PUT_BITS_UEV(ps_bitstrm, u4_log2_parallel_merge_level_minus2, return_status);
1440
674
        ENTROPY_TRACE("log2_parallel_merge_level_minus2", u4_log2_parallel_merge_level_minus2);
1441
674
    }
1442
1443
    /* slice_header_extension_present_flag */
1444
674
    PUT_BITS(ps_bitstrm, ps_pps->i1_slice_header_extension_present_flag, 1, return_status);
1445
674
    ENTROPY_TRACE(
1446
674
        "slice_header_extension_present_flag", ps_pps->i1_slice_header_extension_present_flag);
1447
1448
    /* pps_extension_flag */
1449
674
    PUT_BITS(ps_bitstrm, 0, 1, return_status);
1450
674
    ENTROPY_TRACE("pps_extension_flag", 0);
1451
1452
674
    ihevce_put_rbsp_trailing_bits(ps_bitstrm);
1453
1454
674
    return return_status;
1455
674
}
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
2.80k
{
1496
2.80k
    WORD32 i;
1497
2.80k
    WORD32 return_status = IHEVCE_SUCCESS;
1498
1499
2.80k
    WORD32 RapPicFlag = (i1_nal_unit_type >= NAL_BLA_W_LP) &&
1500
525
                        (i1_nal_unit_type <= NAL_RSV_RAP_VCL23);
1501
2.80k
    WORD32 idr_pic_flag = (NAL_IDR_W_LP == i1_nal_unit_type) || (NAL_IDR_N_LP == i1_nal_unit_type);
1502
1503
2.80k
    WORD32 disable_deblocking_filter_flag;
1504
1505
2.80k
    WORD32 i4_NumPocTotalCurr = 0;
1506
    /* Initialize the pic width and pic height from sps parameters */
1507
2.80k
    WORD32 pic_width = ps_sps->i2_pic_width_in_luma_samples;
1508
2.80k
    WORD32 pic_height = ps_sps->i2_pic_height_in_luma_samples;
1509
1510
    /* Initialize the CTB size from sps parameters */
1511
2.80k
    WORD32 log2_ctb_size =
1512
2.80k
        ps_sps->i1_log2_min_coding_block_size + ps_sps->i1_log2_diff_max_min_coding_block_size;
1513
2.80k
    WORD32 ctb_size = (1 << log2_ctb_size);
1514
1515
    /* Update ps_slice_hdr->i2_slice_address based on tile position in frame */
1516
2.80k
    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
2.80k
    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
2.80k
    else
1527
2.80k
    {
1528
2.80k
        ps_slice_hdr->i2_slice_address = i4_next_slice_seg_x + i4_next_slice_seg_y * num_ctb_in_row;
1529
2.80k
    }
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
2.80k
    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
2.80k
    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
2.80k
    else
1544
2.80k
    {
1545
2.80k
        ps_slice_hdr->i1_dependent_slice_flag = 0;
1546
2.80k
    }
1547
1548
    /* Insert start code */
1549
2.80k
    return_status |= ihevce_put_nal_start_code_prefix(ps_bitstrm, 1);
1550
1551
    /* Insert Nal Unit Header */
1552
2.80k
    return_status |= ihevce_generate_nal_unit_header(
1553
2.80k
        ps_bitstrm,
1554
2.80k
        i1_nal_unit_type,
1555
2.80k
        ps_slice_hdr->u4_nuh_temporal_id);  //TEMPORALA_SCALABILITY CHANGES
1556
1557
    /* first_slice_in_pic_flag */
1558
2.80k
    PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_first_slice_in_pic_flag, 1, return_status);
1559
2.80k
    ENTROPY_TRACE("first_slice_in_pic_flag", ps_slice_hdr->i1_first_slice_in_pic_flag);
1560
1561
2.80k
    if(RapPicFlag)
1562
525
    {
1563
        /* no_output_of_prior_pics_flag */
1564
525
        PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_no_output_of_prior_pics_flag, 1, return_status);
1565
525
        ENTROPY_TRACE(
1566
525
            "no_output_of_prior_pics_flag", ps_slice_hdr->i1_no_output_of_prior_pics_flag);
1567
525
    }
1568
1569
    /* pic_parameter_set_id */
1570
2.80k
    PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_pps_id, return_status);
1571
2.80k
    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
2.80k
    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
2.80k
    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
2.80k
    if(!ps_slice_hdr->i1_dependent_slice_flag)
1601
2.80k
    {
1602
2.80k
        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
2.80k
        PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_slice_type, return_status);
1610
2.80k
        ENTROPY_TRACE("slice_type", ps_slice_hdr->i1_slice_type);
1611
1612
2.80k
        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
2.80k
        if(!idr_pic_flag)
1620
2.34k
        {
1621
            /* pic_order_cnt_lsb */
1622
2.34k
            PUT_BITS(
1623
2.34k
                ps_bitstrm,
1624
2.34k
                ps_slice_hdr->i4_pic_order_cnt_lsb,
1625
2.34k
                ps_sps->i1_log2_max_pic_order_cnt_lsb,
1626
2.34k
                return_status);
1627
2.34k
            ENTROPY_TRACE("pic_order_cnt_lsb", ps_slice_hdr->i4_pic_order_cnt_lsb);
1628
1629
            /* short_term_ref_pic_set_sps_flag */
1630
2.34k
            PUT_BITS(
1631
2.34k
                ps_bitstrm, ps_slice_hdr->i1_short_term_ref_pic_set_sps_flag, 1, return_status);
1632
2.34k
            ENTROPY_TRACE(
1633
2.34k
                "short_term_ref_pic_set_sps_flag",
1634
2.34k
                ps_slice_hdr->i1_short_term_ref_pic_set_sps_flag);
1635
1636
2.34k
            if(!ps_slice_hdr->i1_short_term_ref_pic_set_sps_flag)
1637
2.34k
            {
1638
2.34k
                ihevce_short_term_ref_pic_set(
1639
2.34k
                    ps_bitstrm, &ps_slice_hdr->s_stref_picset, 1, 0, &i4_NumPocTotalCurr);
1640
2.34k
            }
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
2.34k
            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
2.34k
            if(ps_sps->i1_sps_temporal_mvp_enable_flag)
1726
2.34k
            {
1727
                /* slice_temporal_mvp_enable_flag */
1728
2.34k
                PUT_BITS(
1729
2.34k
                    ps_bitstrm, ps_slice_hdr->i1_slice_temporal_mvp_enable_flag, 1, return_status);
1730
2.34k
                ENTROPY_TRACE(
1731
2.34k
                    "slice_temporal_mvp_enable_flag",
1732
2.34k
                    ps_slice_hdr->i1_slice_temporal_mvp_enable_flag);
1733
2.34k
            }
1734
2.34k
        }
1735
1736
2.80k
        if(ps_sps->i1_sample_adaptive_offset_enabled_flag)
1737
1.88k
        {
1738
            /* slice_sao_luma_flag */
1739
1.88k
            PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_slice_sao_luma_flag, 1, return_status);
1740
1.88k
            ENTROPY_TRACE("slice_sao_luma_flag", ps_slice_hdr->i1_slice_sao_luma_flag);
1741
1742
            /* slice_sao_chroma_flag */
1743
1.88k
            PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_slice_sao_chroma_flag, 1, return_status);
1744
1.88k
            ENTROPY_TRACE("slice_sao_chroma_flag", ps_slice_hdr->i1_slice_sao_chroma_flag);
1745
1.88k
        }
1746
2.80k
        if((PSLICE == ps_slice_hdr->i1_slice_type) || (BSLICE == ps_slice_hdr->i1_slice_type))
1747
2.22k
        {
1748
            /* num_ref_idx_active_override_flag */
1749
2.22k
            PUT_BITS(
1750
2.22k
                ps_bitstrm, ps_slice_hdr->i1_num_ref_idx_active_override_flag, 1, return_status);
1751
2.22k
            ENTROPY_TRACE(
1752
2.22k
                "num_ref_idx_active_override_flag",
1753
2.22k
                ps_slice_hdr->i1_num_ref_idx_active_override_flag);
1754
1755
2.22k
            if(ps_slice_hdr->i1_num_ref_idx_active_override_flag)
1756
2.22k
            {
1757
                /* i1_num_ref_idx_l0_active_minus1 */
1758
2.22k
                PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_num_ref_idx_l0_active - 1, return_status);
1759
2.22k
                ENTROPY_TRACE(
1760
2.22k
                    "i1_num_ref_idx_l0_active_minus1", ps_slice_hdr->i1_num_ref_idx_l0_active - 1);
1761
1762
2.22k
                if(BSLICE == ps_slice_hdr->i1_slice_type)
1763
154
                {
1764
                    /* i1_num_ref_idx_l1_active */
1765
154
                    PUT_BITS_UEV(
1766
154
                        ps_bitstrm, ps_slice_hdr->i1_num_ref_idx_l1_active - 1, return_status);
1767
154
                    ENTROPY_TRACE(
1768
154
                        "i1_num_ref_idx_l1_active", ps_slice_hdr->i1_num_ref_idx_l1_active - 1);
1769
154
                }
1770
2.22k
            }
1771
1772
2.22k
            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
2.22k
            if(BSLICE == ps_slice_hdr->i1_slice_type)
1778
154
            {
1779
                /* mvd_l1_zero_flag */
1780
154
                PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_mvd_l1_zero_flag, 1, return_status);
1781
154
                ENTROPY_TRACE("mvd_l1_zero_flag", ps_slice_hdr->i1_mvd_l1_zero_flag);
1782
154
            }
1783
1784
2.22k
            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
2.22k
            if(ps_slice_hdr->i1_slice_temporal_mvp_enable_flag)
1792
2.22k
            {
1793
2.22k
                if(BSLICE == ps_slice_hdr->i1_slice_type)
1794
154
                {
1795
                    /* collocated_from_l0_flag */
1796
154
                    PUT_BITS(
1797
154
                        ps_bitstrm, ps_slice_hdr->i1_collocated_from_l0_flag, 1, return_status);
1798
154
                    ENTROPY_TRACE(
1799
154
                        "collocated_from_l0_flag", ps_slice_hdr->i1_collocated_from_l0_flag);
1800
154
                }
1801
2.22k
                if((ps_slice_hdr->i1_collocated_from_l0_flag &&
1802
2.11k
                    (ps_slice_hdr->i1_num_ref_idx_l0_active > 1)) ||
1803
661
                   (!ps_slice_hdr->i1_collocated_from_l0_flag &&
1804
101
                    (ps_slice_hdr->i1_num_ref_idx_l1_active > 1)))
1805
1.56k
                {
1806
                    /* collocated_ref_idx */
1807
1.56k
                    PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_collocated_ref_idx, return_status);
1808
1.56k
                    ENTROPY_TRACE("collocated_ref_idx", ps_slice_hdr->i1_collocated_ref_idx);
1809
1.56k
                }
1810
2.22k
            }
1811
1812
2.22k
            if((ps_pps->i1_weighted_pred_flag && (PSLICE == ps_slice_hdr->i1_slice_type)) ||
1813
2.22k
               (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
2.22k
#if !HM_8DOT1_SYNTAX
1819
            /* five_minus_max_num_merge_cand */
1820
2.22k
            PUT_BITS_UEV(ps_bitstrm, 5 - ps_slice_hdr->i1_max_num_merge_cand, return_status);
1821
2.22k
            ENTROPY_TRACE("five_minus_max_num_merge_cand", 5 - ps_slice_hdr->i1_max_num_merge_cand);
1822
2.22k
#endif
1823
2.22k
        }
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
2.80k
        PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_slice_qp_delta, return_status);
1832
2.80k
        ENTROPY_TRACE("slice_qp_delta", ps_slice_hdr->i1_slice_qp_delta);
1833
1834
2.80k
        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
2.80k
        if(ps_pps->i1_deblocking_filter_control_present_flag)
1846
2.80k
        {
1847
2.80k
            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
2.80k
            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
2.80k
        }
1881
1882
2.80k
        disable_deblocking_filter_flag = ps_slice_hdr->i1_slice_disable_deblocking_filter_flag |
1883
2.80k
                                         ps_pps->i1_pic_disable_deblocking_filter_flag;
1884
1885
2.80k
        if(ps_pps->i1_loop_filter_across_slices_enabled_flag &&
1886
2.80k
           (ps_slice_hdr->i1_slice_sao_luma_flag || ps_slice_hdr->i1_slice_sao_chroma_flag ||
1887
920
            !disable_deblocking_filter_flag))
1888
2.68k
        {
1889
            /* slice_loop_filter_across_slices_enabled_flag */
1890
2.68k
            PUT_BITS(
1891
2.68k
                ps_bitstrm,
1892
2.68k
                ps_slice_hdr->i1_slice_loop_filter_across_slices_enabled_flag,
1893
2.68k
                1,
1894
2.68k
                return_status);
1895
2.68k
            ENTROPY_TRACE(
1896
2.68k
                "slice_loop_filter_across_slices_enabled_flag",
1897
2.68k
                ps_slice_hdr->i1_slice_loop_filter_across_slices_enabled_flag);
1898
2.68k
        }
1899
2.80k
    }
1900
1901
2.80k
    if((ps_pps->i1_tiles_enabled_flag) || (ps_pps->i1_entropy_coding_sync_enabled_flag))
1902
74
    {
1903
        /* num_entry_point_offsets */
1904
74
        PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i4_num_entry_point_offsets, return_status);
1905
74
        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
74
        memcpy(ps_dup_bit_strm_ent_offset, ps_bitstrm, sizeof(bitstrm_t));
1909
1910
74
        if(ps_slice_hdr->i4_num_entry_point_offsets > 0)
1911
74
        {
1912
            /* offset_len_minus1 */
1913
74
            PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_offset_len - 1, return_status);
1914
74
            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
74
            pu4_first_slice_start_offset[0] =
1924
74
                ps_bitstrm->u4_strm_buf_offset +
1925
74
                ((ps_slice_hdr->i4_num_entry_point_offsets * ps_slice_hdr->i1_offset_len) >> 3) +
1926
74
                ps_slice_hdr->i4_num_entry_point_offsets + 4 + 4;
1927
1928
74
            ps_slice_hdr->pu4_entry_point_offset[0] = (*pu4_first_slice_start_offset);
1929
1930
543
            for(i = 0; i < ps_slice_hdr->i4_num_entry_point_offsets; i++)
1931
469
            {
1932
                /* entry_point_offset[i] */
1933
469
                PUT_BITS(
1934
469
                    ps_bitstrm,
1935
469
                    ps_slice_hdr->pu4_entry_point_offset[i],
1936
469
                    ps_slice_hdr->i1_offset_len,
1937
469
                    return_status);
1938
469
                ENTROPY_TRACE("entry_point_offset[i]", ps_slice_hdr->pu4_entry_point_offset[i]);
1939
469
            }
1940
74
        }
1941
74
    }
1942
1943
2.80k
    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
2.80k
    BYTE_ALIGNMENT(ps_bitstrm);
1959
1960
2.80k
    return return_status;
1961
2.80k
}
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
207
{
2001
207
    WORD8 *pi1_profile_compatiblity_flags;
2002
207
    WORD32 i;
2003
207
    WORD32 i4_field_pic = ps_src_params->i4_field_pic;
2004
207
    WORD32 i4_codec_level_index;
2005
207
    ps_vps->i1_vps_id = DEFAULT_VPS_ID;
2006
2007
207
    (void)ps_config_prms;
2008
    /* default sub layers is 1 */
2009
207
    ps_vps->i1_vps_max_sub_layers = 1;
2010
207
    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
414
    for(i = 0; i < ps_vps->i1_vps_max_sub_layers; i++)
2016
207
    {
2017
        /* currently bit rate and pic rate signalling is disabled */
2018
207
        ps_vps->ai1_bit_rate_info_present_flag[i] = 0;
2019
207
        ps_vps->ai1_pic_rate_info_present_flag[i] = 0;
2020
2021
207
        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
207
        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
207
    }
2033
2034
    /* default sub layer ordering info present flag */
2035
207
    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
207
    ps_vps->i1_vps_num_hrd_parameters = 0;
2039
2040
207
    if(ps_vps->i1_vps_max_sub_layers == 1)
2041
207
    {
2042
207
        ps_vps->i1_vps_temporal_id_nesting_flag = 1;
2043
207
    }
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
207
    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
207
    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
207
    pi1_profile_compatiblity_flags = &ps_vps->s_ptl.s_ptl_gen.ai1_profile_compatibility_flag[0];
2060
2061
414
    for(i = 0; i < ps_vps->i1_vps_max_sub_layers; i++)  //TEMPORALA_SCALABILITY CHANGES
2062
207
    {
2063
207
        ps_vps->ai1_vps_max_dec_pic_buffering[i] =
2064
207
            ps_coding_params->i4_max_reference_frames + (2 << i4_field_pic) - 1;
2065
2066
207
        ps_vps->ai1_vps_max_num_reorder_pics[i] = ps_coding_params->i4_max_temporal_layers
2067
207
                                                  << i4_field_pic;
2068
2069
207
        ps_vps->ai1_vps_max_latency_increase[i] = 0;
2070
2071
207
        ps_vps->s_ptl.ai1_sub_layer_level_present_flag[i] = 1;  //TEMPORALA_SCALABILITY CHANGES
2072
2073
207
        ps_vps->s_ptl.ai1_sub_layer_profile_present_flag[i] = 0;  //TEMPORALA_SCALABILITY CHANGES
2074
2075
207
        ps_vps->s_ptl.as_ptl_sub[i].i1_profile_space = 0;  // BLU_RAY specific change is default
2076
2077
207
        ps_vps->s_ptl.as_ptl_sub[i].i1_profile_idc = ps_out_strm_params->i4_codec_profile;
2078
2079
207
        memset(
2080
207
            ps_vps->s_ptl.as_ptl_sub[i].ai1_profile_compatibility_flag,
2081
207
            0,
2082
207
            MAX_PROFILE_COMPATBLTY * sizeof(WORD8));
2083
2084
207
        ps_vps->s_ptl.as_ptl_sub[i]
2085
207
            .ai1_profile_compatibility_flag[ps_out_strm_params->i4_codec_profile] = 1;
2086
2087
207
        ps_vps->s_ptl.as_ptl_sub[i].u1_level_idc =
2088
207
            ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_codec_level;
2089
2090
207
        if(0 == i)  // Only one level temporal scalability suport has been added.
2091
207
        {
2092
207
            i4_codec_level_index = ihevce_get_level_index(
2093
207
                ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_codec_level);
2094
2095
207
            if(i4_codec_level_index)
2096
207
                i4_codec_level_index -= 1;
2097
2098
207
            ps_vps->s_ptl.as_ptl_sub[i].u1_level_idc =
2099
207
                (WORD32)g_as_level_data[i4_codec_level_index].e_level;
2100
207
        }
2101
2102
207
        ps_vps->s_ptl.as_ptl_sub[i].i1_tier_flag = ps_out_strm_params->i4_codec_tier;
2103
2104
207
        if(ps_src_params->i4_field_pic == IV_PROGRESSIVE)
2105
207
        {
2106
207
            ps_vps->s_ptl.as_ptl_sub[i].i1_general_progressive_source_flag = 1;
2107
2108
207
            ps_vps->s_ptl.as_ptl_sub[i].i1_general_interlaced_source_flag = 0;
2109
207
        }
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
207
        ps_vps->s_ptl.as_ptl_sub[i].i1_general_non_packed_constraint_flag =
2124
207
            DEFAULT_NON_PACKED_CONSTRAINT_FLAG;
2125
2126
207
        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
207
        else
2131
207
        {
2132
207
            ps_vps->s_ptl.as_ptl_sub[i].i1_frame_only_constraint_flag =
2133
207
                DEFAULT_FRAME_ONLY_CONSTRAINT_FLAG;
2134
207
        }
2135
207
    }
2136
2137
207
    memset(pi1_profile_compatiblity_flags, 0, MAX_PROFILE_COMPATBLTY);
2138
207
    pi1_profile_compatiblity_flags[ps_out_strm_params->i4_codec_profile] = 1;
2139
2140
    /* set the level idc according to user input */
2141
207
    ps_vps->s_ptl.s_ptl_gen.u1_level_idc =
2142
207
        ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_codec_level;
2143
2144
207
    ps_vps->s_ptl.s_ptl_gen.i1_tier_flag = ps_out_strm_params->i4_codec_tier;
2145
2146
207
    if(ps_src_params->i4_field_pic == IV_PROGRESSIVE)
2147
207
    {
2148
207
        ps_vps->s_ptl.s_ptl_gen.i1_general_progressive_source_flag = 1;
2149
2150
207
        ps_vps->s_ptl.s_ptl_gen.i1_general_interlaced_source_flag = 0;
2151
207
    }
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
207
    ps_vps->s_ptl.s_ptl_gen.i1_general_non_packed_constraint_flag =
2166
207
        DEFAULT_NON_PACKED_CONSTRAINT_FLAG;
2167
2168
207
    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
207
    else
2173
207
    {
2174
207
        ps_vps->s_ptl.s_ptl_gen.i1_frame_only_constraint_flag = DEFAULT_FRAME_ONLY_CONSTRAINT_FLAG;
2175
207
    }
2176
207
    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
207
    else if(
2198
207
        (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
207
    else
2220
207
    {
2221
207
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_12bit_constraint_flag = 0;
2222
2223
207
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_10bit_constraint_flag = 0;
2224
2225
207
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_8bit_constraint_flag = 0;
2226
2227
207
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_422chroma_constraint_flag = 0;
2228
2229
207
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_420chroma_constraint_flag = 0;
2230
2231
207
        ps_vps->s_ptl.s_ptl_gen.i1_general_max_monochrome_constraint_flag = 0;
2232
2233
207
        ps_vps->s_ptl.s_ptl_gen.i1_general_intra_constraint_flag = 0;
2234
2235
207
        ps_vps->s_ptl.s_ptl_gen.i1_general_one_picture_only_constraint_flag = 0;
2236
2237
207
        ps_vps->s_ptl.s_ptl_gen.i1_general_lower_bit_rate_constraint_flag = 0;
2238
207
    }
2239
2240
207
    ps_vps->i1_vps_max_nuh_reserved_zero_layer_id = 0;
2241
2242
207
    return IHEVCE_SUCCESS;
2243
207
}
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
207
{
2287
207
    WORD32 i;
2288
207
    WORD32 i4_field_pic = ps_src_params->i4_field_pic;
2289
207
    WORD32 i4_quality_preset =
2290
207
        ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_quality_preset;
2291
207
    WORD32 i4_codec_level_index;
2292
2293
207
    if(i4_quality_preset == IHEVCE_QUALITY_P7)
2294
14
    {
2295
14
        i4_quality_preset = IHEVCE_QUALITY_P6;
2296
14
    }
2297
2298
207
    ps_sps->i1_sps_id = DEFAULT_SPS_ID;
2299
2300
207
    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
207
    ps_sps->i1_vps_id = ps_vps->i1_vps_id;
2306
2307
207
    ps_sps->i2_pic_height_in_luma_samples = ps_frm_ctb_prms->i4_cu_aligned_pic_ht;
2308
2309
207
    ps_sps->i2_pic_width_in_luma_samples = ps_frm_ctb_prms->i4_cu_aligned_pic_wd;
2310
2311
207
    ps_sps->i1_amp_enabled_flag = AMP_ENABLED;
2312
2313
207
    ps_sps->i1_chroma_format_idc = (ps_src_params->i4_chr_format == IV_YUV_422SP_UV) ? 2 : 1;
2314
2315
207
    ps_sps->i1_separate_colour_plane_flag = 0;
2316
2317
207
    ps_sps->i1_bit_depth_luma_minus8 = ps_stat_cfg_prms->s_tgt_lyr_prms.i4_internal_bit_depth - 8;
2318
2319
207
    ps_sps->i1_bit_depth_chroma_minus8 = ps_stat_cfg_prms->s_tgt_lyr_prms.i4_internal_bit_depth - 8;
2320
2321
207
    ps_sps->i1_log2_min_coding_block_size = ps_config_prms->i4_min_log2_cu_size;
2322
2323
207
    ps_sps->i1_log2_diff_max_min_coding_block_size =
2324
207
        ps_config_prms->i4_max_log2_cu_size - ps_config_prms->i4_min_log2_cu_size;
2325
2326
207
    ps_sps->i1_log2_ctb_size =
2327
207
        ps_sps->i1_log2_min_coding_block_size + ps_sps->i1_log2_diff_max_min_coding_block_size;
2328
2329
207
    ps_sps->i1_log2_diff_max_min_transform_block_size =
2330
207
        ps_config_prms->i4_max_log2_tu_size - ps_config_prms->i4_min_log2_tu_size;
2331
2332
207
    ps_sps->i1_log2_min_transform_block_size = ps_config_prms->i4_min_log2_tu_size;
2333
2334
207
    ps_sps->i1_long_term_ref_pics_present_flag = LONG_TERM_REF_PICS_ABSENT;
2335
2336
207
    ps_sps->i1_max_transform_hierarchy_depth_inter = ps_config_prms->i4_max_tr_tree_depth_nI;
2337
2338
207
    ps_sps->i1_max_transform_hierarchy_depth_intra = ps_config_prms->i4_max_tr_tree_depth_I;
2339
2340
207
    ps_sps->i1_pcm_enabled_flag = PCM_DISABLED;
2341
2342
207
    ps_sps->i1_pcm_loop_filter_disable_flag = PCM_LOOP_FILTER_DISABLED;
2343
2344
207
    ps_sps->i1_pic_cropping_flag = !!ps_coding_params->i4_cropping_mode;
2345
2346
207
    if(i4_quality_preset < IHEVCE_QUALITY_P4)
2347
162
    {
2348
        /*** Enable SAO for PQ,HQ,MS presets **/
2349
162
        ps_sps->i1_sample_adaptive_offset_enabled_flag = SAO_ENABLED;
2350
162
    }
2351
45
    else
2352
45
    {
2353
45
        ps_sps->i1_sample_adaptive_offset_enabled_flag = SAO_DISABLED;
2354
45
    }
2355
#if DISABLE_SAO
2356
    ps_sps->i1_sample_adaptive_offset_enabled_flag = SAO_DISABLED;
2357
#endif
2358
2359
207
    if(ps_coding_params->i4_use_default_sc_mtx == 1)
2360
62
    {
2361
62
        ps_sps->i1_scaling_list_enable_flag = SCALING_LIST_ENABLED;
2362
62
    }
2363
145
    else
2364
145
    {
2365
145
        ps_sps->i1_scaling_list_enable_flag = SCALING_LIST_DISABLED;
2366
145
    }
2367
2368
207
    ps_sps->i1_sps_max_sub_layers = DEFAULT_SPS_MAX_SUB_LAYERS;
2369
2370
207
    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
207
    ps_sps->i1_sps_sub_layer_ordering_info_present_flag = SPS_SUB_LAYER_ORDERING_INFO_ABSENT;
2376
2377
207
    ps_sps->i1_sps_scaling_list_data_present_flag = SCALING_LIST_DATA_ABSENT;
2378
2379
207
    if(ps_sps->i1_sps_max_sub_layers == 1)
2380
207
    {
2381
207
        ps_sps->i1_sps_temporal_id_nesting_flag = 1;  //NO_SPS_TEMPORAL_ID_NESTING_DONE;
2382
207
    }
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
207
    ps_sps->i1_num_short_term_ref_pic_sets = 0;
2390
207
    ps_sps->i1_long_term_ref_pics_present_flag = 0;
2391
2392
207
    ps_sps->i1_num_long_term_ref_pics_sps = 0;
2393
207
    ps_sps->i1_sps_temporal_mvp_enable_flag = !DISABLE_TMVP;
2394
2395
207
    ps_sps->i1_strong_intra_smoothing_enable_flag = STRONG_INTRA_SMOOTHING_FLAG_ENABLE;
2396
2397
207
    ps_sps->i1_vui_parameters_present_flag = ps_out_strm_params->i4_vui_enable;
2398
2399
    /*required in generation of slice header*/
2400
207
    ps_sps->i2_pic_ht_in_ctb = ps_frm_ctb_prms->i4_num_ctbs_vert;
2401
2402
207
    ps_sps->i2_pic_wd_in_ctb = ps_frm_ctb_prms->i4_num_ctbs_horz;
2403
2404
207
    ps_sps->i1_log2_max_pic_order_cnt_lsb = DEFAULT_LOG2_MAX_POC_LSB;
2405
2406
207
    if(ps_sps->i1_pic_cropping_flag)
2407
207
    {
2408
207
        WORD32 num_rows_to_pad_bottom =
2409
207
            ps_sps->i2_pic_height_in_luma_samples - ps_stat_cfg_prms->s_src_prms.i4_orig_height;
2410
207
        WORD32 num_rows_to_pad_right =
2411
207
            ps_sps->i2_pic_width_in_luma_samples - ps_stat_cfg_prms->s_src_prms.i4_orig_width;
2412
2413
207
        ps_sps->i2_pic_crop_top_offset = DEFAULT_PIC_CROP_TOP_OFFSET;
2414
2415
207
        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
207
        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
207
        ps_sps->i2_pic_crop_right_offset = num_rows_to_pad_right >> 1;
2422
207
    }
2423
2424
414
    for(i = 0; i < (ps_sps->i1_sps_max_sub_layers); i++)
2425
207
    {
2426
207
        ps_sps->ai1_sps_max_dec_pic_buffering[i] =
2427
207
            ps_coding_params->i4_max_reference_frames + (2 << i4_field_pic) - 1;
2428
2429
207
        ps_sps->ai1_sps_max_num_reorder_pics[i] = ps_coding_params->i4_max_temporal_layers
2430
207
                                                  << i4_field_pic;
2431
2432
207
        ps_sps->ai1_sps_max_latency_increase[i] = 0;
2433
2434
207
        ps_sps->s_ptl.ai1_sub_layer_level_present_flag[i] = 1;  //TEMPORALA_SCALABILITY CHANGES
2435
2436
207
        ps_sps->s_ptl.ai1_sub_layer_profile_present_flag[i] = 0;  //TEMPORALA_SCALABILITY CHANGES
2437
2438
207
        ps_sps->s_ptl.as_ptl_sub[i].i1_profile_space = 0;  // BLU_RAY specific change is default
2439
2440
207
        ps_sps->s_ptl.as_ptl_sub[i].i1_profile_idc = ps_out_strm_params->i4_codec_profile;
2441
2442
207
        memset(
2443
207
            ps_sps->s_ptl.as_ptl_sub[i].ai1_profile_compatibility_flag,
2444
207
            0,
2445
207
            MAX_PROFILE_COMPATBLTY * sizeof(WORD8));
2446
2447
207
        ps_sps->s_ptl.as_ptl_sub[i]
2448
207
            .ai1_profile_compatibility_flag[ps_out_strm_params->i4_codec_profile] = 1;
2449
2450
207
        ps_sps->s_ptl.as_ptl_sub[i].u1_level_idc =
2451
207
            ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_codec_level;
2452
2453
207
        if(0 == i)  // Only one level temporal scalability suport has been added.
2454
207
        {
2455
207
            i4_codec_level_index = ihevce_get_level_index(
2456
207
                ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_codec_level);
2457
2458
207
            if(i4_codec_level_index)
2459
207
                i4_codec_level_index -= 1;
2460
2461
207
            ps_sps->s_ptl.as_ptl_sub[i].u1_level_idc =
2462
207
                (WORD32)g_as_level_data[i4_codec_level_index].e_level;
2463
207
        }
2464
207
        ps_sps->s_ptl.as_ptl_sub[i].i1_tier_flag = ps_out_strm_params->i4_codec_tier;
2465
2466
207
        if(ps_src_params->i4_field_pic == IV_PROGRESSIVE)
2467
207
        {
2468
207
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_progressive_source_flag = 1;
2469
2470
207
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_interlaced_source_flag = 0;
2471
207
        }
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
207
        ps_sps->s_ptl.as_ptl_sub[i].i1_general_non_packed_constraint_flag =
2486
207
            DEFAULT_NON_PACKED_CONSTRAINT_FLAG;
2487
2488
207
        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
207
        else
2493
207
        {
2494
207
            ps_sps->s_ptl.as_ptl_sub[i].i1_frame_only_constraint_flag =
2495
207
                DEFAULT_FRAME_ONLY_CONSTRAINT_FLAG;
2496
207
        }
2497
207
        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
207
        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
207
        else
2538
207
        {
2539
207
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_12bit_constraint_flag = 0;
2540
2541
207
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_10bit_constraint_flag = 0;
2542
2543
207
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_8bit_constraint_flag = 0;
2544
2545
207
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_422chroma_constraint_flag = 0;
2546
2547
207
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_420chroma_constraint_flag = 0;
2548
2549
207
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_max_monochrome_constraint_flag = 0;
2550
2551
207
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_intra_constraint_flag = 0;
2552
2553
207
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_one_picture_only_constraint_flag = 0;
2554
2555
207
            ps_sps->s_ptl.as_ptl_sub[i].i1_general_lower_bit_rate_constraint_flag = 0;
2556
207
        }
2557
207
    }
2558
2559
207
    memset(
2560
207
        ps_sps->s_ptl.s_ptl_gen.ai1_profile_compatibility_flag,
2561
207
        0,
2562
207
        MAX_PROFILE_COMPATBLTY * sizeof(WORD8));
2563
2564
    /* populate the general profile, tier and level information */
2565
207
    ps_sps->s_ptl.s_ptl_gen.i1_profile_space = 0;  // BLU_RAY specific change is default
2566
2567
207
    ps_sps->s_ptl.s_ptl_gen.i1_profile_idc = ps_out_strm_params->i4_codec_profile;
2568
2569
207
    ps_sps->s_ptl.s_ptl_gen.ai1_profile_compatibility_flag[ps_out_strm_params->i4_codec_profile] =
2570
207
        1;
2571
2572
207
    ps_sps->s_ptl.s_ptl_gen.u1_level_idc =
2573
207
        ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_codec_level;
2574
2575
207
    ps_sps->s_ptl.s_ptl_gen.i1_tier_flag = ps_out_strm_params->i4_codec_tier;
2576
2577
207
    if(ps_src_params->i4_field_pic == IV_PROGRESSIVE)
2578
207
    {
2579
207
        ps_sps->s_ptl.s_ptl_gen.i1_general_progressive_source_flag = 1;
2580
2581
207
        ps_sps->s_ptl.s_ptl_gen.i1_general_interlaced_source_flag = 0;
2582
207
    }
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
207
    ps_sps->s_ptl.s_ptl_gen.i1_general_non_packed_constraint_flag =
2597
207
        DEFAULT_NON_PACKED_CONSTRAINT_FLAG;
2598
2599
207
    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
207
    else
2604
207
    {
2605
207
        ps_sps->s_ptl.s_ptl_gen.i1_frame_only_constraint_flag = DEFAULT_FRAME_ONLY_CONSTRAINT_FLAG;
2606
207
    }
2607
207
    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
207
    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
207
    else
2648
207
    {
2649
207
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_12bit_constraint_flag = 0;
2650
2651
207
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_10bit_constraint_flag = 0;
2652
2653
207
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_8bit_constraint_flag = 0;
2654
2655
207
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_422chroma_constraint_flag = 0;
2656
2657
207
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_420chroma_constraint_flag = 0;
2658
2659
207
        ps_sps->s_ptl.s_ptl_gen.i1_general_max_monochrome_constraint_flag = 0;
2660
2661
207
        ps_sps->s_ptl.s_ptl_gen.i1_general_intra_constraint_flag = 0;
2662
2663
207
        ps_sps->s_ptl.s_ptl_gen.i1_general_one_picture_only_constraint_flag = 0;
2664
2665
207
        ps_sps->s_ptl.s_ptl_gen.i1_general_lower_bit_rate_constraint_flag = 0;
2666
207
    }
2667
2668
207
    return IHEVCE_SUCCESS;
2669
207
}
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
207
{
2715
207
    (void)ps_src_params;
2716
207
    (void)ps_out_strm_params;
2717
2718
207
    ps_pps->i1_beta_offset_div2 = DEFAULT_BETA_OFFSET;
2719
2720
207
    ps_pps->i1_cabac_init_present_flag = CABAC_INIT_ABSENT;
2721
2722
207
    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
207
    ps_pps->i1_cu_qp_delta_enabled_flag = ps_config_prms->i4_cu_level_rc;
2725
2726
207
    ps_pps->i1_deblocking_filter_control_present_flag = DEBLOCKING_FILTER_CONTROL_PRESENT;
2727
2728
207
    ps_pps->i1_deblocking_filter_override_enabled_flag = DEBLOCKING_FILTER_OVERRIDE_DISABLED;
2729
2730
207
    ps_pps->i1_pic_disable_deblocking_filter_flag = ps_coding_params->i4_deblocking_type;
2731
2732
207
    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
207
    else
2737
207
    {
2738
207
        ps_pps->i1_dependent_slice_enabled_flag = DEPENDENT_SLICE_DISABLED;
2739
207
    }
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
207
    ps_pps->i1_diff_cu_qp_delta_depth = CU_LEVEL_QP_LIMIT_8x8;
2744
2745
207
    if(1 == ps_coding_params->i4_enable_entropy_sync)
2746
23
    {
2747
23
        ps_pps->i1_entropy_coding_sync_enabled_flag = ENTROPY_CODING_SYNC_ENABLED;
2748
23
    }
2749
184
    else
2750
184
    {
2751
184
        ps_pps->i1_entropy_coding_sync_enabled_flag = ENTROPY_CODING_SYNC_DISABLED;
2752
184
    }
2753
2754
207
    ps_pps->i1_entropy_slice_enabled_flag = ENTROPY_SLICE_DISABLED;
2755
2756
207
    ps_pps->i1_lists_modification_present_flag = ps_coding_params->i4_weighted_pred_enable;
2757
2758
207
    ps_pps->i1_log2_parallel_merge_level = DEFAULT_PARALLEL_MERGE_LEVEL;
2759
2760
207
    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
207
    ps_pps->i1_loop_filter_across_slices_enabled_flag = LF_ACROSS_SLICES_ENABLED;
2766
2767
207
    ps_pps->i1_num_ref_idx_l0_default_active = DEFAULT_NUM_REF_IDX_L0_DEFAULT_ACTIVE;
2768
2769
207
    ps_pps->i1_num_ref_idx_l1_default_active = DEFAULT_NUM_REF_IDX_L1_DEFAULT_ACTIVE;
2770
2771
207
    if(0 == ps_tile_params_base->i4_tiles_enabled_flag)
2772
207
    {
2773
207
        ps_pps->i1_num_tile_columns = NUM_TILES_COLS;
2774
2775
207
        ps_pps->i1_num_tile_rows = NUM_TILES_ROWS;
2776
2777
207
        ps_pps->i1_tiles_enabled_flag = TILES_DISABLED;
2778
2779
207
        ps_pps->i1_uniform_spacing_flag = SPACING_IS_UNIFORM;
2780
207
    }
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
207
    if(0 == ps_tile_params_base->i4_tiles_enabled_flag)
2811
207
    {
2812
207
        ps_pps->i1_loop_filter_across_tiles_enabled_flag = 1;
2813
207
    }
2814
0
    else
2815
0
    {
2816
0
        ps_pps->i1_loop_filter_across_tiles_enabled_flag = 0;
2817
0
    }
2818
2819
207
    ps_pps->i1_output_flag_present_flag = OUTPUT_FLAG_ABSENT;
2820
2821
207
    ps_pps->i1_pic_cb_qp_offset = DEFAULT_PIC_CB_QP_OFFSET;
2822
2823
207
    ps_pps->i1_pic_cr_qp_offset = DEFAULT_PIC_CR_QP_OFFSET;
2824
2825
    /*init qp is different for each bit-rate instance */
2826
207
    ps_pps->i1_pic_init_qp = CLIP3(
2827
207
        ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id]
2828
207
            .ai4_frame_qp[i4_bitrate_instance_id],
2829
207
        ps_config_prms->i4_min_frame_qp,
2830
207
        ps_config_prms->i4_max_frame_qp);
2831
2832
    /* enable chroma QP offset only if stasino or psy rd is present */
2833
207
    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
207
    else
2843
207
    {
2844
207
        ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag =
2845
207
            SLICE_LEVEL_CHROMA_QP_OFFSETS_ABSENT;
2846
207
    }
2847
2848
207
    ps_pps->i1_pps_id = DEFAULT_PPS_ID;
2849
2850
207
    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
207
    ps_pps->i1_pps_scaling_list_data_present_flag = SCALING_LIST_DATA_ABSENT;
2856
2857
207
    if(ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_quality_preset <
2858
207
       IHEVCE_QUALITY_P3)
2859
120
    {
2860
120
        ps_pps->i1_sign_data_hiding_flag = SIGN_DATA_HIDDEN;
2861
120
    }
2862
87
    else if(
2863
87
        ps_stat_cfg_prms->s_tgt_lyr_prms.as_tgt_params[i4_resolution_id].i4_quality_preset ==
2864
87
        IHEVCE_QUALITY_P3)
2865
42
    {
2866
42
        ps_pps->i1_sign_data_hiding_flag = SIGN_DATA_UNHIDDEN;
2867
42
    }
2868
45
    else
2869
45
    {
2870
45
        ps_pps->i1_sign_data_hiding_flag = SIGN_DATA_UNHIDDEN;
2871
45
    }
2872
2873
#if DISABLE_SBH
2874
    ps_pps->i1_sign_data_hiding_flag = SIGN_DATA_UNHIDDEN;
2875
#endif
2876
2877
207
    ps_pps->i1_slice_extension_present_flag = SLICE_EXTENSION_ABSENT;
2878
2879
207
    ps_pps->i1_slice_header_extension_present_flag = SLICE_HEADER_EXTENSION_ABSENT;
2880
2881
207
    ps_pps->i1_sps_id = ps_sps->i1_sps_id;
2882
2883
207
    ps_pps->i1_tc_offset_div2 = DEFAULT_TC_OFFSET;
2884
2885
207
    ps_pps->i1_transform_skip_enabled_flag = TRANSFORM_SKIP_DISABLED;
2886
2887
207
    ps_pps->i1_transquant_bypass_enable_flag = TRANSFORM_BYPASS_DISABLED;
2888
2889
207
    ps_pps->i1_weighted_bipred_flag = ps_coding_params->i4_weighted_pred_enable;
2890
2891
207
    ps_pps->i1_weighted_pred_flag = ps_coding_params->i4_weighted_pred_enable;
2892
2893
207
    return IHEVCE_SUCCESS;
2894
207
}
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
2.80k
{
2950
2.80k
    WORD32 i;
2951
2.80k
    WORD32 return_status = IHEVCE_SUCCESS;
2952
2.80k
    WORD32 RapPicFlag = (nal_unit_type >= NAL_BLA_W_LP) && (nal_unit_type <= NAL_RSV_RAP_VCL23);
2953
2954
2.80k
    WORD32 idr_pic_flag = (NAL_IDR_W_LP == nal_unit_type) || (NAL_IDR_N_LP == nal_unit_type);
2955
2956
2.80k
    WORD32 disable_deblocking_filter_flag;
2957
2958
2.80k
    (void)ctb_x;
2959
2.80k
    (void)ctb_y;
2960
    /* first_slice_in_pic_flag  */
2961
2.80k
    if(i4_quality_preset == IHEVCE_QUALITY_P7)
2962
94
    {
2963
94
        i4_quality_preset = IHEVCE_QUALITY_P6;
2964
94
    }
2965
2966
2.80k
    if(RapPicFlag)
2967
525
    {
2968
        /* no_output_of_prior_pics_flag */ /* TODO:revisit this */
2969
525
        ps_slice_hdr->i1_no_output_of_prior_pics_flag = 0;  //BLU_RAY specific already done
2970
525
    }
2971
2972
    /* pic_parameter_set_id */
2973
2.80k
    ps_slice_hdr->i1_pps_id = ps_pps->i1_pps_id;
2974
2975
2.80k
    {
2976
        /* This i1_dependent_slice_flag will further be updated in generate_slice_header() function */
2977
2.80k
        ps_slice_hdr->i1_dependent_slice_flag = 0;
2978
2.80k
    }
2979
2980
2.80k
    if(!ps_slice_hdr->i1_dependent_slice_flag)
2981
2.80k
    {
2982
        /* slice_type */
2983
2.80k
        ps_slice_hdr->i1_slice_type = (WORD8)slice_type;
2984
2985
2.80k
        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
2.80k
        ASSERT(0 == ps_sps->i1_separate_colour_plane_flag);
2993
2994
2.80k
        if(!idr_pic_flag)
2995
2.34k
        {
2996
2.34k
            WORD32 log2_max_poc_lsb = ps_sps->i1_log2_max_pic_order_cnt_lsb;
2997
2998
            /* pic_order_cnt_lsb */
2999
2.34k
            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
2.34k
            ps_slice_hdr->i1_short_term_ref_pic_set_sps_flag = 0;
3004
3005
2.34k
            if(!ps_slice_hdr->i1_short_term_ref_pic_set_sps_flag)
3006
2.34k
            {
3007
                /* TODO: To populate short term ref pic set for this slice   */
3008
2.34k
            }
3009
3010
            /* long term ref pic flag not supported */
3011
2.34k
            ASSERT(0 == ps_sps->i1_long_term_ref_pics_present_flag);
3012
2.34k
            if(ps_sps->i1_long_term_ref_pics_present_flag)
3013
0
            {
3014
                /* TODO : not supported */
3015
0
            }
3016
2.34k
        }
3017
3018
        //ASSERT(0 == ps_sps->i1_sample_adaptive_offset_enabled_flag);
3019
2.80k
        if(ps_sps->i1_sample_adaptive_offset_enabled_flag)
3020
1.88k
        {
3021
            /* slice_sao_luma_flag */
3022
1.88k
            ps_slice_hdr->i1_slice_sao_luma_flag = 1;
3023
1.88k
            ps_slice_hdr->i1_slice_sao_chroma_flag = 1;
3024
1.88k
        }
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
2.80k
        if((PSLICE == ps_slice_hdr->i1_slice_type) || (BSLICE == ps_slice_hdr->i1_slice_type))
3034
2.22k
        {
3035
            /* TODO: currently temporal mvp disabled, need to enable later */
3036
2.22k
            if(1 == ps_sps->i1_sps_temporal_mvp_enable_flag)
3037
2.22k
            {
3038
2.22k
                ps_slice_hdr->i1_slice_temporal_mvp_enable_flag = 1;
3039
2.22k
            }
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
2.22k
            ps_slice_hdr->i1_num_ref_idx_active_override_flag = 0;
3047
3048
2.22k
            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
2.22k
            if(BSLICE == ps_slice_hdr->i1_slice_type)
3063
154
            {
3064
                /* mvd_l1_zero_flag */
3065
154
                ps_slice_hdr->i1_mvd_l1_zero_flag = 0;
3066
154
            }
3067
3068
2.22k
            {
3069
                /* cabac_init_flag curently set to 0 */
3070
2.22k
                ps_slice_hdr->i1_cabac_init_flag = ps_pps->i1_cabac_init_present_flag ? 1 : 0;
3071
2.22k
            }
3072
3073
2.22k
            if(ps_slice_hdr->i1_slice_temporal_mvp_enable_flag)
3074
2.22k
            {
3075
2.22k
                if(BSLICE == ps_slice_hdr->i1_slice_type)
3076
154
                {
3077
                    /* collocated_from_l0_flag */
3078
154
                    ps_slice_hdr->i1_collocated_from_l0_flag = 0;
3079
154
                }
3080
2.06k
                else if(PSLICE == ps_slice_hdr->i1_slice_type)
3081
2.06k
                {
3082
2.06k
                    ps_slice_hdr->i1_collocated_from_l0_flag = 1;
3083
2.06k
                }
3084
3085
2.22k
                if((ps_slice_hdr->i1_collocated_from_l0_flag &&
3086
2.06k
                    (ps_slice_hdr->i1_num_ref_idx_l0_active > 1)) ||
3087
2.22k
                   (!ps_slice_hdr->i1_collocated_from_l0_flag &&
3088
154
                    (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
2.22k
            }
3096
2.22k
        }
3097
2.80k
        ps_slice_hdr->i1_max_num_merge_cand = max_merge_candidates;
3098
3099
        /* TODO : revisit this */
3100
2.80k
        ps_slice_hdr->i1_slice_qp_delta = (WORD8)cur_slice_qp - ps_pps->i1_pic_init_qp;
3101
3102
2.80k
        if(!ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag || !stasino_enabled)
3103
2.80k
        {
3104
            /* slice_cb_qp_offset */
3105
2.80k
            ps_slice_hdr->i1_slice_cb_qp_offset = 0;
3106
3107
            /* slice_cr_qp_offset */
3108
2.80k
            ps_slice_hdr->i1_slice_cr_qp_offset = 0;
3109
2.80k
        }
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
2.80k
        if(ps_pps->i1_deblocking_filter_control_present_flag)
3117
2.80k
        {
3118
2.80k
            ps_slice_hdr->i1_deblocking_filter_override_flag = 0;
3119
3120
2.80k
            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
2.80k
            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
2.80k
        }
3141
3142
2.80k
        disable_deblocking_filter_flag = ps_slice_hdr->i1_slice_disable_deblocking_filter_flag |
3143
2.80k
                                         ps_pps->i1_pic_disable_deblocking_filter_flag;
3144
3145
2.80k
        if(ps_pps->i1_loop_filter_across_slices_enabled_flag &&
3146
2.80k
           (ps_slice_hdr->i1_slice_sao_luma_flag || ps_slice_hdr->i1_slice_sao_chroma_flag ||
3147
920
            !disable_deblocking_filter_flag))
3148
2.68k
        {
3149
            /* slice_loop_filter_across_slices_enabled_flag */
3150
2.68k
            ps_slice_hdr->i1_slice_loop_filter_across_slices_enabled_flag = 1;
3151
2.68k
        }
3152
2.80k
    }
3153
3154
2.80k
    if(1 == ps_pps->i1_entropy_coding_sync_enabled_flag)
3155
74
    {
3156
        /* num_entry_point_offsets, same as NUM of ctb rows to enable entropy sync at start of every CTB */
3157
74
        ps_slice_hdr->i4_num_entry_point_offsets = ps_sps->i2_pic_ht_in_ctb - 1;
3158
3159
74
        if(ps_slice_hdr->i4_num_entry_point_offsets > 0)
3160
74
        {
3161
            /* generate offset_len here */
3162
            /* fixing the offset lenght assuming 4kx2k is log2(w * h / num_ctb_row) = 20*/
3163
74
            ps_slice_hdr->i1_offset_len = 24;
3164
74
        }
3165
74
    }
3166
2.72k
    else
3167
2.72k
    {
3168
2.72k
        ps_slice_hdr->i4_num_entry_point_offsets = 0;
3169
2.72k
        ps_slice_hdr->i1_offset_len = 0;
3170
2.72k
    }
3171
3172
    /* slice_header_extension_present_flag not supported */
3173
    //if(ps_pps->i1_slice_header_extension_present_flag)
3174
2.80k
    {
3175
        /* slice_header_extension_length */
3176
2.80k
        ps_slice_hdr->i2_slice_header_extension_length = 0;
3177
3178
2.80k
        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
2.80k
    }
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
2.80k
    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
2.80k
    ps_slice_hdr->s_stref_picset.i1_num_delta_pocs = 1;
3194
2.80k
    ps_slice_hdr->s_stref_picset.i1_num_neg_pics = 1;
3195
2.80k
    ps_slice_hdr->s_stref_picset.i1_num_pos_pics = 0;
3196
3197
2.80k
    memset(
3198
2.80k
        ps_slice_hdr->s_stref_picset.ai2_delta_poc,
3199
2.80k
        0,
3200
2.80k
        MAX_DPB_SIZE * sizeof(*ps_slice_hdr->s_stref_picset.ai2_delta_poc));
3201
2.80k
    ps_slice_hdr->s_stref_picset.ai2_delta_poc[0] = 1;
3202
3203
2.80k
    return return_status;
3204
2.80k
}
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
74
{
3239
74
    WORD32 i;
3240
74
    WORD32 return_status = IHEVCE_SUCCESS;
3241
74
    UWORD32 max_offset = 0, offset_len = 0, num_bytes_shift = 0;
3242
    /*entire slice data has to be shifted*/
3243
74
    num_bytes_shift =
3244
74
        ps_slice_hdr->pu4_entry_point_offset[ps_slice_hdr->i4_num_entry_point_offsets + 1] -
3245
74
        ps_slice_hdr->pu4_entry_point_offset[0];
3246
    /*generate relative offset*/
3247
543
    for(i = 0; i < ps_slice_hdr->i4_num_entry_point_offsets; i++)
3248
469
    {
3249
469
        ps_slice_hdr->pu4_entry_point_offset[i] =
3250
469
            ps_slice_hdr->pu4_entry_point_offset[i + 1] - ps_slice_hdr->pu4_entry_point_offset[i];
3251
469
        if(ps_slice_hdr->pu4_entry_point_offset[i] > (WORD32)max_offset)
3252
130
        {
3253
130
            max_offset = ps_slice_hdr->pu4_entry_point_offset[i];
3254
130
        }
3255
469
    }
3256
1.57k
    while(1)
3257
1.57k
    {
3258
1.57k
        if(max_offset & 0x80000000)
3259
74
        {
3260
74
            break;
3261
74
        }
3262
1.50k
        max_offset <<= 1;
3263
1.50k
        offset_len++;
3264
1.50k
    }
3265
74
    offset_len = 32 - offset_len;
3266
74
    ps_slice_hdr->i1_offset_len = offset_len;
3267
3268
74
    if(ps_slice_hdr->i4_num_entry_point_offsets > 0)
3269
74
    {
3270
        /* offset_len_minus1 */
3271
74
        PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_offset_len - 1, return_status);
3272
74
        ENTROPY_TRACE("offset_len_minus1", ps_slice_hdr->i1_offset_len - 1);
3273
74
    }
3274
3275
543
    for(i = 0; i < ps_slice_hdr->i4_num_entry_point_offsets; i++)
3276
469
    {
3277
        /* entry_point_offset[i] */
3278
        /* entry point offset minus1 is indicated in 10.0 */
3279
469
        PUT_BITS(
3280
469
            ps_bitstrm,
3281
469
            ps_slice_hdr->pu4_entry_point_offset[i] - 1,
3282
469
            ps_slice_hdr->i1_offset_len,
3283
469
            return_status);
3284
469
        ENTROPY_TRACE("entry_point_offset[i]", ps_slice_hdr->pu4_entry_point_offset[i]);
3285
469
    }
3286
3287
74
    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
74
    BYTE_ALIGNMENT(ps_bitstrm);
3304
3305
74
    ASSERT(num_bytes_shift > 0);
3306
    /* copy the bitstream to point where header data has ended*/
3307
74
    memmove(
3308
74
        (UWORD8 *)(ps_bitstrm->pu1_strm_buffer + ps_bitstrm->u4_strm_buf_offset),
3309
74
        (UWORD8 *)(ps_bitstrm->pu1_strm_buffer + u4_first_slice_start_offset),
3310
74
        num_bytes_shift);
3311
3312
    /*send feedback of actual bytes generated*/
3313
74
    ps_bitstrm->u4_strm_buf_offset += num_bytes_shift;
3314
3315
    //ASSERT(ps_bitstrm->u4_strm_buf_offset == u4_first_slice_start_offset);
3316
74
    return return_status;
3317
74
}