Coverage Report

Created: 2025-07-18 07:04

/src/libavc/encoder/ih264e_encode_header.c
Line
Count
Source (jump to first uncovered line)
1
/******************************************************************************
2
 *
3
 * Copyright (C) 2015 The Android Open Source Project
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at:
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 *****************************************************************************
18
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19
*/
20
21
/**
22
*******************************************************************************
23
* @file
24
*  ih264e_encode_header.c
25
*
26
* @brief
27
*  This file contains function definitions related to header encoding.
28
*
29
* @author
30
*  ittiam
31
*
32
* @par List of Functions:
33
*  - ih264e_generate_nal_unit_header
34
*  - ih264e_generate_vui
35
*  - ih264e_generate_aud
36
*  - ih264e_generate_sps
37
*  - ih264e_generate_pps
38
*  - ih264e_generate_slice_header
39
*  - ih264e_populate_vui
40
*  - ih264e_populate_sps
41
*  - ih264e_populate_pps
42
*  - ih264e_populate_slice_header
43
*  - ih264e_add_filler_nal_unit
44
*
45
* @remarks
46
*  none
47
*
48
*******************************************************************************
49
*/
50
51
/*****************************************************************************/
52
/* File Includes                                                             */
53
/*****************************************************************************/
54
55
/* System Include Files */
56
#include <stdio.h>
57
#include <stddef.h>
58
#include <stdlib.h>
59
#include <string.h>
60
#include <assert.h>
61
62
/* User Include Files */
63
#include "ih264e_config.h"
64
#include "ih264_typedefs.h"
65
#include "iv2.h"
66
#include "ive2.h"
67
#include "ithread.h"
68
69
#include "ih264_debug.h"
70
#include "ih264_macros.h"
71
#include "ih264_error.h"
72
#include "ih264_defs.h"
73
#include "ih264_mem_fns.h"
74
#include "ih264_padding.h"
75
#include "ih264_structs.h"
76
#include "ih264_trans_quant_itrans_iquant.h"
77
#include "ih264_inter_pred_filters.h"
78
#include "ih264_intra_pred_filters.h"
79
#include "ih264_deblk_edge_filters.h"
80
#include "ih264_common_tables.h"
81
#include "ih264_cabac_tables.h"
82
83
#include "ime_defs.h"
84
#include "ime_distortion_metrics.h"
85
#include "ime_structs.h"
86
87
#include "irc_cntrl_param.h"
88
#include "irc_frame_info_collector.h"
89
90
#include "ih264e.h"
91
#include "ih264e_error.h"
92
#include "ih264e_defs.h"
93
#include "ih264e_rate_control.h"
94
#include "ih264e_bitstream.h"
95
#include "ih264e_cabac_structs.h"
96
#include "ih264e_structs.h"
97
#include "ih264e_utils.h"
98
#include "ih264e_sei.h"
99
#include "ih264e_encode_header.h"
100
#include "ih264e_trace.h"
101
102
103
/*****************************************************************************/
104
/* Function Definitions                                                      */
105
/*****************************************************************************/
106
107
/**
108
******************************************************************************
109
*
110
* @brief Generate nal unit header in the stream as per section 7.4.1
111
*
112
* @par   Description
113
*  Inserts Nal unit header syntax as per section 7.4.1
114
*
115
* @param[inout]   ps_bitstrm
116
*  pointer to bitstream context (handle)
117
*
118
* @param[in]   nal_unit_type
119
*  nal type to be inserted
120
*
121
* @param[in]   nal_ref_idc
122
*  nal ref idc to be inserted
123
*
124
* @return      success or failure error code
125
*
126
******************************************************************************
127
*/
128
static WORD32 ih264e_generate_nal_unit_header(bitstrm_t *ps_bitstrm,
129
                                              WORD32 nal_unit_type,
130
                                              WORD32 nal_ref_idc)
131
76.6k
{
132
76.6k
    WORD32 return_status = IH264E_SUCCESS;
133
134
    /* sanity checks */
135
76.6k
    ASSERT((nal_unit_type > 0) && (nal_unit_type < 32));
136
137
    /* forbidden_zero_bit + nal_ref_idc + nal_unit_type */
138
76.6k
    PUT_BITS(ps_bitstrm,
139
76.6k
             ((nal_ref_idc << 5) + nal_unit_type),
140
76.6k
             (1+2+5), /*1 forbidden zero bit + 2 nal_ref_idc + 5 nal_unit_type */
141
76.6k
             return_status,
142
76.6k
             "nal_unit_header");
143
144
76.6k
    return(return_status);
145
76.6k
}
146
/**
147
******************************************************************************
148
*
149
* @brief Generates VUI (Video usability information)
150
*
151
* @par   Description
152
*  This function generates VUI header as per the spec
153
*
154
* @param[in]   ps_bitstrm
155
*  pointer to bitstream context (handle)
156
*
157
* @param[in]   ps_vui
158
*  pointer to structure containing VUI data
159
160
*
161
* @return      success or failure error code
162
*
163
******************************************************************************
164
*/
165
WORD32 ih264e_generate_vui(bitstrm_t *ps_bitstrm, vui_t *ps_vui)
166
4.87k
{
167
4.87k
    WORD32 return_status = IH264E_SUCCESS;
168
169
    /* aspect_ratio_info_present_flag */
170
4.87k
    PUT_BITS(ps_bitstrm, ps_vui->u1_aspect_ratio_info_present_flag, 1,
171
4.87k
             return_status, "aspect_ratio_info_present_flag");
172
173
4.87k
    if(ps_vui->u1_aspect_ratio_info_present_flag)
174
1.86k
    { /* aspect_ratio_idc */
175
1.86k
        PUT_BITS(ps_bitstrm, ps_vui->u1_aspect_ratio_idc, 8, return_status,
176
1.86k
                 "aspect_ratio_idc");
177
1.86k
        if(255 == ps_vui->u1_aspect_ratio_idc) /* Extended_SAR */
178
0
        { /* sar_width */
179
0
            PUT_BITS(ps_bitstrm, ps_vui->u2_sar_width, 16, return_status,
180
0
                     "sar_width");
181
            /* sar_height */
182
0
            PUT_BITS(ps_bitstrm, ps_vui->u2_sar_height, 16, return_status,
183
0
                     "sar_height");
184
0
        }
185
186
1.86k
    }
187
    /* overscan_info_present_flag */
188
4.87k
    PUT_BITS(ps_bitstrm, ps_vui->u1_overscan_info_present_flag, 1,
189
4.87k
             return_status, "overscan_info_present_flag");
190
191
4.87k
    if(ps_vui->u1_overscan_info_present_flag)
192
0
    {
193
        /* overscan_appropriate_flag */
194
0
        PUT_BITS(ps_bitstrm, ps_vui->u1_overscan_appropriate_flag, 1,
195
0
                 return_status, "overscan_appropriate_flag");
196
197
0
    }
198
    /* video_signal_type_present_flag */
199
4.87k
    PUT_BITS(ps_bitstrm, ps_vui->u1_video_signal_type_present_flag, 1,
200
4.87k
             return_status, "video_signal_type_present_flag");
201
202
4.87k
    if(ps_vui->u1_video_signal_type_present_flag)
203
4.87k
    { /* video_format */
204
4.87k
        PUT_BITS(ps_bitstrm, ps_vui->u1_video_format, 3, return_status,
205
4.87k
                 "video_format");
206
207
        /* video_full_range_flag */
208
4.87k
        PUT_BITS(ps_bitstrm, ps_vui->u1_video_full_range_flag, 1, return_status,
209
4.87k
                 "video_full_range_flag");
210
211
        /* colour_description_present_flag */
212
4.87k
        PUT_BITS(ps_bitstrm, ps_vui->u1_colour_description_present_flag, 1,
213
4.87k
                 return_status, "colour_description_present_flag");
214
215
4.87k
        if(ps_vui->u1_colour_description_present_flag)
216
4.87k
        {
217
            /* colour_primaries */
218
4.87k
            PUT_BITS(ps_bitstrm, ps_vui->u1_colour_primaries, 8, return_status,
219
4.87k
                     "colour_primaries");
220
221
            /* transfer_characteristics */
222
4.87k
            PUT_BITS(ps_bitstrm, ps_vui->u1_transfer_characteristics, 8,
223
4.87k
                     return_status, "transfer_characteristics");
224
225
            /* matrix_coefficients */
226
4.87k
            PUT_BITS(ps_bitstrm, ps_vui->u1_matrix_coefficients, 8,
227
4.87k
                     return_status, "matrix_coefficients");
228
4.87k
        }
229
230
4.87k
    }
231
232
    /* chroma_loc_info_present_flag */
233
4.87k
    PUT_BITS(ps_bitstrm, ps_vui->u1_chroma_loc_info_present_flag, 1,
234
4.87k
             return_status, "chroma_loc_info_present_flag");
235
236
4.87k
    if(ps_vui->u1_chroma_loc_info_present_flag)
237
0
    {
238
        /* chroma_sample_loc_type_top_field */
239
0
        PUT_BITS_UEV(ps_bitstrm, ps_vui->u1_chroma_sample_loc_type_top_field,
240
0
                     return_status, "chroma_sample_loc_type_top_field");
241
242
        /* chroma_sample_loc_type_bottom_field */
243
0
        PUT_BITS_UEV(ps_bitstrm, ps_vui->u1_chroma_sample_loc_type_bottom_field,
244
0
                     return_status, "chroma_sample_loc_type_bottom_field");
245
0
    }
246
247
    /* timing_info_present_flag */
248
4.87k
    PUT_BITS(ps_bitstrm, ps_vui->u1_vui_timing_info_present_flag, 1,
249
4.87k
             return_status, "timing_info_present_flag");
250
251
4.87k
    if(ps_vui->u1_vui_timing_info_present_flag)
252
0
    {
253
        /* num_units_in_tick */
254
0
        PUT_BITS(ps_bitstrm, ps_vui->u4_vui_num_units_in_tick, 32,
255
0
                 return_status, "num_units_in_tick");
256
257
        /* time_scale */
258
0
        PUT_BITS(ps_bitstrm, ps_vui->u4_vui_time_scale, 32, return_status,
259
0
                 "time_scale");
260
261
        /* fixed_frame_rate_flag */
262
0
        PUT_BITS(ps_bitstrm, ps_vui->u1_fixed_frame_rate_flag, 1, return_status,
263
0
                 "fixed_frame_rate_flag");
264
265
0
    }
266
267
    /* nal_hrd_parameters_present_flag */
268
4.87k
    PUT_BITS(ps_bitstrm, ps_vui->u1_nal_hrd_parameters_present_flag, 1,
269
4.87k
             return_status, "nal_hrd_parameters_present_flag");
270
271
4.87k
    if(ps_vui->u1_nal_hrd_parameters_present_flag)
272
0
    {
273
0
        hrd_params_t * ps_hrd_params = &ps_vui->s_nal_hrd_parameters;
274
0
        WORD32 i;
275
        /* cpb_cnt_minus1 */
276
0
        PUT_BITS_UEV(ps_bitstrm, ps_hrd_params->u1_cpb_cnt_minus1,
277
0
                     return_status, "cpb_cnt_minus1");
278
279
        /* bit_rate_scale */
280
0
        PUT_BITS(ps_bitstrm, ps_hrd_params->u4_bit_rate_scale, 4, return_status,
281
0
                 "bit_rate_scale");
282
283
        /* cpb_size_scale */
284
0
        PUT_BITS(ps_bitstrm, ps_hrd_params->u4_cpb_size_scale, 4, return_status,
285
0
                 "cpb_size_scale");
286
0
        for(i = 0; i < ps_hrd_params->u1_cpb_cnt_minus1; i++)
287
0
        {
288
            /* bit_rate_value_minus1[SchedSelIdx] */
289
0
            PUT_BITS_UEV(ps_bitstrm,
290
0
                         ps_hrd_params->au4_bit_rate_value_minus1[i],
291
0
                         return_status, "bit_rate_value_minus1[SchedSelIdx]");
292
293
            /* cpb_size_value_minus1[SchedSelIdx] */
294
0
            PUT_BITS_UEV(ps_bitstrm,
295
0
                         ps_hrd_params->au4_cpb_size_value_minus1[i],
296
0
                         return_status, "cpb_size_value_minus1[SchedSelIdx]");
297
298
            /* cbr_flag[SchedSelIdx] */
299
0
            PUT_BITS(ps_bitstrm, ps_hrd_params->au1_cbr_flag[i], 1,
300
0
                     return_status, "cbr_flag[SchedSelIdx]");
301
0
        }
302
303
        /* initial_cpb_removal_delay_length_minus1 */
304
0
        PUT_BITS(ps_bitstrm,
305
0
                 ps_hrd_params->u1_initial_cpb_removal_delay_length_minus1, 5,
306
0
                 return_status, "initial_cpb_removal_delay_length_minus1");
307
308
        /* cpb_removal_delay_length_minus1 */
309
0
        PUT_BITS(ps_bitstrm, ps_hrd_params->u1_cpb_removal_delay_length_minus1,
310
0
                 5, return_status, "cpb_removal_delay_length_minus1");
311
312
        /* dpb_output_delay_length_minus1 */
313
0
        PUT_BITS(ps_bitstrm, ps_hrd_params->u1_dpb_output_delay_length_minus1,
314
0
                 5, return_status, "dpb_output_delay_length_minus1");
315
316
        /* time_offset_length */
317
0
        PUT_BITS(ps_bitstrm, ps_hrd_params->u1_time_offset_length, 5,
318
0
                 return_status, "time_offset_length");
319
0
    }
320
321
    /* vcl_hrd_parameters_present_flag */
322
4.87k
    PUT_BITS(ps_bitstrm, ps_vui->u1_vcl_hrd_parameters_present_flag, 1,
323
4.87k
             return_status, "vcl_hrd_parameters_present_flag");
324
325
4.87k
    if(ps_vui->u1_vcl_hrd_parameters_present_flag)
326
0
    {
327
0
        hrd_params_t * ps_hrd_params = &ps_vui->s_vcl_hrd_parameters;
328
0
        WORD32 i;
329
        /* cpb_cnt_minus1 */
330
0
        PUT_BITS_UEV(ps_bitstrm, ps_hrd_params->u1_cpb_cnt_minus1,
331
0
                     return_status, "cpb_cnt_minus1");
332
333
        /* bit_rate_scale */
334
0
        PUT_BITS(ps_bitstrm, ps_hrd_params->u4_bit_rate_scale, 4, return_status,
335
0
                 "bit_rate_scale");
336
337
        /* cpb_size_scale */
338
0
        PUT_BITS(ps_bitstrm, ps_hrd_params->u4_cpb_size_scale, 4, return_status,
339
0
                 "cpb_size_scale");
340
0
        for(i = 0; i < ps_hrd_params->u1_cpb_cnt_minus1; i++)
341
0
        {
342
            /* bit_rate_value_minus1[SchedSelIdx] */
343
0
            PUT_BITS_UEV(ps_bitstrm,
344
0
                         ps_hrd_params->au4_bit_rate_value_minus1[i],
345
0
                         return_status, "bit_rate_value_minus1[SchedSelIdx]");
346
347
            /* cpb_size_value_minus1[SchedSelIdx] */
348
0
            PUT_BITS_UEV(ps_bitstrm,
349
0
                         ps_hrd_params->au4_cpb_size_value_minus1[i],
350
0
                         return_status, "cpb_size_value_minus1[SchedSelIdx]");
351
352
            /* cbr_flag[SchedSelIdx] */
353
0
            PUT_BITS(ps_bitstrm, ps_hrd_params->au1_cbr_flag[i], 1,
354
0
                     return_status, "cbr_flag[SchedSelIdx]");
355
0
        }
356
357
        /* initial_cpb_removal_delay_length_minus1 */
358
0
        PUT_BITS(ps_bitstrm,
359
0
                 ps_hrd_params->u1_initial_cpb_removal_delay_length_minus1, 5,
360
0
                 return_status, "initial_cpb_removal_delay_length_minus1");
361
362
        /* cpb_removal_delay_length_minus1 */
363
0
        PUT_BITS(ps_bitstrm, ps_hrd_params->u1_cpb_removal_delay_length_minus1,
364
0
                 5, return_status, "cpb_removal_delay_length_minus1");
365
366
        /* dpb_output_delay_length_minus1 */
367
0
        PUT_BITS(ps_bitstrm, ps_hrd_params->u1_dpb_output_delay_length_minus1,
368
0
                 5, return_status, "dpb_output_delay_length_minus1");
369
370
        /* time_offset_length */
371
0
        PUT_BITS(ps_bitstrm, ps_hrd_params->u1_time_offset_length, 5,
372
0
                 return_status, "time_offset_length");
373
0
    }
374
375
4.87k
    if(ps_vui->u1_nal_hrd_parameters_present_flag
376
4.87k
                    || ps_vui->u1_vcl_hrd_parameters_present_flag)
377
0
    {
378
        /* low_delay_hrd_flag */
379
0
        PUT_BITS(ps_bitstrm, ps_vui->u1_low_delay_hrd_flag, 1, return_status,
380
0
                 "low_delay_hrd_flag");
381
0
    }
382
    /* pic_struct_present_flag */
383
4.87k
    PUT_BITS(ps_bitstrm, ps_vui->u1_pic_struct_present_flag, 1, return_status,
384
4.87k
             "pic_struct_present_flag");
385
386
    /* bitstream_restriction_flag */
387
4.87k
    PUT_BITS(ps_bitstrm, ps_vui->u1_bitstream_restriction_flag, 1,
388
4.87k
             return_status, "bitstream_restriction_flag");
389
390
4.87k
    if(ps_vui->u1_bitstream_restriction_flag == 1)
391
4.87k
    {
392
        /* motion_vectors_over_pic_boundaries_flag */
393
4.87k
        PUT_BITS(ps_bitstrm, ps_vui->u1_motion_vectors_over_pic_boundaries_flag,
394
4.87k
                 1, return_status, "motion_vectors_over_pic_boundaries_flag");
395
396
        /* max_bytes_per_pic_denom */
397
4.87k
        PUT_BITS_UEV(ps_bitstrm, ps_vui->u1_max_bytes_per_pic_denom,
398
4.87k
                     return_status, "max_bytes_per_pic_denom");
399
400
        /* max_bits_per_mb_denom */
401
4.87k
        PUT_BITS_UEV(ps_bitstrm, ps_vui->u1_max_bits_per_mb_denom,
402
4.87k
                     return_status, "max_bits_per_mb_denom");
403
404
        /* log2_max_mv_length_horizontal */
405
4.87k
        PUT_BITS_UEV(ps_bitstrm, ps_vui->u1_log2_max_mv_length_horizontal,
406
4.87k
                     return_status, "log2_max_mv_length_horizontal");
407
408
        /* log2_max_mv_length_vertical */
409
4.87k
        PUT_BITS_UEV(ps_bitstrm, ps_vui->u1_log2_max_mv_length_vertical,
410
4.87k
                     return_status, "log2_max_mv_length_vertical");
411
412
        /* max_num_reorder_frames */
413
4.87k
        PUT_BITS_UEV(ps_bitstrm, ps_vui->u1_num_reorder_frames, return_status,
414
4.87k
                     "max_num_reorder_frames");
415
416
        /* max_dec_frame_buffering */
417
4.87k
        PUT_BITS_UEV(ps_bitstrm, ps_vui->u1_max_dec_frame_buffering,
418
4.87k
                     return_status, "max_dec_frame_buffering");
419
4.87k
    }
420
421
4.87k
    return return_status;
422
4.87k
}
423
424
/**
425
******************************************************************************
426
*
427
* @brief Generates SPS (Sequence Parameter Set)
428
*
429
* @par   Description
430
*  This function generates Sequence Parameter Set header as per the spec
431
*
432
* @param[in]   ps_bitstrm
433
*  pointer to bitstream context (handle)
434
*
435
* @param[in]   ps_sps
436
*  pointer to structure containing SPS data
437
*
438
* @param[in]   ps_vui
439
*  pointer to structure containing VUI data
440
*
441
* @return      success or failure error code
442
*
443
******************************************************************************
444
*/
445
WORD32 ih264e_generate_sps(bitstrm_t *ps_bitstrm, sps_t *ps_sps, vui_t *ps_vui)
446
4.87k
{
447
4.87k
    WORD32 return_status = IH264E_SUCCESS;
448
4.87k
    WORD32 i;
449
4.87k
    WORD8  i1_nal_unit_type = 7;
450
4.87k
    WORD8  i1_nal_ref_idc = 3;
451
452
    /* Insert Start Code */
453
4.87k
    return_status = ih264e_put_nal_start_code_prefix(ps_bitstrm, 1);
454
4.87k
    if(return_status != IH264E_SUCCESS)
455
0
    {
456
0
        return return_status;
457
0
    }
458
    /* Insert Nal Unit Header */
459
4.87k
    return_status = ih264e_generate_nal_unit_header(ps_bitstrm, i1_nal_unit_type, i1_nal_ref_idc);
460
4.87k
    if(return_status != IH264E_SUCCESS)
461
0
    {
462
0
        return return_status;
463
0
    }
464
    /* profile_idc */
465
4.87k
    PUT_BITS(ps_bitstrm, ps_sps->u1_profile_idc, 8, return_status, "profile_idc");
466
467
    /* constrained_set_flags */
468
4.87k
    PUT_BITS(ps_bitstrm, ps_sps->u1_constraint_set0_flag, 1, return_status, "constrained_set0_flag");
469
4.87k
    PUT_BITS(ps_bitstrm, ps_sps->u1_constraint_set1_flag, 1, return_status, "constrained_set1_flag");
470
4.87k
    PUT_BITS(ps_bitstrm, ps_sps->u1_constraint_set2_flag, 1, return_status, "constrained_set2_flag");
471
4.87k
    PUT_BITS(ps_bitstrm, ps_sps->u1_constraint_set3_flag, 1, return_status, "constrained_set3_flag");
472
473
    /* reserved_zero_four_bits */
474
4.87k
    PUT_BITS(ps_bitstrm, 0, 4, return_status, "reserved_zero_four_bits");
475
476
    /* level_idc */
477
4.87k
    PUT_BITS(ps_bitstrm, ps_sps->u1_level_idc, 8, return_status, "level_idc");
478
479
    /* seq_parameter_set_id */
480
4.87k
    PUT_BITS_UEV(ps_bitstrm, ps_sps->u1_sps_id, return_status, "seq_parameter_set_id");
481
482
4.87k
    if (ps_sps->u1_profile_idc >= IH264_PROFILE_HIGH)
483
0
    {
484
        /* chroma_format_idc */
485
0
        PUT_BITS_UEV(ps_bitstrm, ps_sps->u1_chroma_format_idc, return_status, "chroma_format_idc");
486
487
0
        if (ps_sps->u1_chroma_format_idc == CHROMA_FMT_IDC_YUV444)
488
0
        {
489
            /* i1_residual_colour_transform_flag */
490
0
            PUT_BITS(ps_bitstrm, ps_sps->i1_residual_colour_transform_flag, 1, return_status, "i1_residual_colour_transform_flag");
491
0
        }
492
493
        /* bit_depth_luma_minus8 */
494
0
        PUT_BITS_UEV(ps_bitstrm, (ps_sps->i1_bit_depth_luma - 8), return_status, "bit_depth_luma_minus8");
495
496
        /* bit_depth_chroma_minus8 */
497
0
        PUT_BITS_UEV(ps_bitstrm, (ps_sps->i1_bit_depth_chroma - 8), return_status, "bit_depth_chroma_minus8");
498
499
        /* qpprime_y_zero_transform_bypass_flag */
500
0
        PUT_BITS(ps_bitstrm, ps_sps->i1_qpprime_y_zero_transform_bypass_flag, 1, return_status, "qpprime_y_zero_transform_bypass_flag");
501
502
        /* seq_scaling_matrix_present_flag */
503
0
        PUT_BITS(ps_bitstrm, ps_sps->i1_seq_scaling_matrix_present_flag, 1, return_status, "seq_scaling_matrix_present_flag");
504
505
        /* seq_scaling_list */
506
0
        if (ps_sps->i1_seq_scaling_matrix_present_flag)
507
0
        {
508
            /* TODO_LATER: Will be enabled once scaling list support is added */
509
0
        }
510
0
    }
511
512
    /* log2_max_frame_num_minus4 */
513
4.87k
    PUT_BITS_UEV(ps_bitstrm, (ps_sps->i1_log2_max_frame_num - 4), return_status, "log2_max_frame_num_minus4");
514
515
    /* pic_order_cnt_type */
516
4.87k
    PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_pic_order_cnt_type, return_status, "pic_order_cnt_type");
517
518
4.87k
    if (ps_sps->i1_pic_order_cnt_type == 0)
519
2.86k
    {
520
        /* log2_max_pic_order_cnt_lsb_minus4 */
521
2.86k
        PUT_BITS_UEV(ps_bitstrm, (ps_sps->i1_log2_max_pic_order_cnt_lsb - 4), return_status, "log2_max_pic_order_cnt_lsb_minus4");
522
2.86k
    }
523
2.01k
    else if (ps_sps->i1_pic_order_cnt_type == 1)
524
0
    {
525
        /* delta_pic_order_always_zero_flag */
526
0
        PUT_BITS(ps_bitstrm, ps_sps->i1_delta_pic_order_always_zero_flag, 1, return_status, "delta_pic_order_always_zero_flag");
527
528
        /* offset_for_non_ref_pic */
529
0
        PUT_BITS_SEV(ps_bitstrm, ps_sps->i4_offset_for_non_ref_pic, return_status, "offset_for_non_ref_pic");
530
531
        /* offset_for_top_to_bottom_field */
532
0
        PUT_BITS_SEV(ps_bitstrm, ps_sps->i4_offset_for_top_to_bottom_field, return_status, "offset_for_top_to_bottom_field");
533
534
        /* num_ref_frames_in_pic_order_cnt_cycle */
535
0
        PUT_BITS_UEV(ps_bitstrm, ps_sps->u1_num_ref_frames_in_pic_order_cnt_cycle, return_status, "num_ref_frames_in_pic_order_cnt_cycle");
536
537
        /* Offset for ref frame */
538
0
        for (i=0; i<ps_sps->u1_num_ref_frames_in_pic_order_cnt_cycle; i++)
539
0
        {
540
            /* offset_for_ref_frame */
541
0
            PUT_BITS_SEV(ps_bitstrm, ps_sps->ai4_offset_for_ref_frame[i], return_status, "offset_for_ref_frame");
542
0
        }
543
0
    }
544
545
    /* num_ref_frames */
546
4.87k
    PUT_BITS_UEV(ps_bitstrm, ps_sps->u1_max_num_ref_frames, return_status, "num_ref_frames");
547
548
    /* gaps_in_frame_num_value_allowed_flag */
549
4.87k
    PUT_BITS(ps_bitstrm, ps_sps->i1_gaps_in_frame_num_value_allowed_flag, 1, return_status, "gaps_in_frame_num_value_allowed_flag");
550
551
    /* pic_width_in_mbs_minus1 */
552
4.87k
    PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_width_in_mbs_minus1, return_status, "pic_width_in_mbs_minus1");
553
554
    /* pic_height_in_map_units_minus1 */
555
4.87k
    PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_height_in_map_units_minus1, return_status, "pic_height_in_map_units_minus1");
556
557
    /* frame_mbs_only_flag */
558
4.87k
    PUT_BITS(ps_bitstrm, ps_sps->i1_frame_mbs_only_flag, 1, return_status, "frame_mbs_only_flag");
559
560
4.87k
    if (!ps_sps->i1_frame_mbs_only_flag)
561
0
    {
562
        /* mb_adaptive_frame_field_flag */
563
0
        PUT_BITS(ps_bitstrm, ps_sps->i1_mb_adaptive_frame_field_flag, 1, return_status, "mb_adaptive_frame_field_flag");
564
0
    }
565
566
    /* direct_8x8_inference_flag */
567
4.87k
    PUT_BITS(ps_bitstrm, ps_sps->i1_direct_8x8_inference_flag, 1, return_status, "direct_8x8_inference_flag");
568
569
    /* frame_cropping_flag */
570
4.87k
    PUT_BITS(ps_bitstrm, ps_sps->i1_frame_cropping_flag, 1, return_status, "frame_cropping_flag");
571
572
4.87k
    if (ps_sps->i1_frame_cropping_flag)
573
3.44k
    {
574
        /* frame_crop_left_offset */
575
3.44k
        PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_frame_crop_left_offset, return_status, "frame_crop_left_offset");
576
577
        /* frame_crop_right_offset */
578
3.44k
        PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_frame_crop_right_offset, return_status, "frame_crop_right_offset");
579
580
        /* frame_crop_top_offset */
581
3.44k
        PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_frame_crop_top_offset, return_status, "frame_crop_top_offset");
582
583
        /* frame_crop_bottom_offset */
584
3.44k
        PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_frame_crop_bottom_offset, return_status, "frame_crop_bottom_offset");
585
3.44k
    }
586
587
    /* vui_parameters_present_flag */
588
4.87k
    PUT_BITS(ps_bitstrm, ps_sps->i1_vui_parameters_present_flag, 1, return_status, "vui_parameters_present_flag");
589
590
4.87k
    if (ps_sps->i1_vui_parameters_present_flag)
591
4.87k
    {
592
4.87k
        /* Add vui parameters to the bitstream */;
593
4.87k
        return_status = ih264e_generate_vui(ps_bitstrm, ps_vui);
594
4.87k
        if(return_status != IH264E_SUCCESS)
595
0
        {
596
0
            return return_status;
597
0
        }
598
4.87k
    }
599
600
    /* rbsp trailing bits */
601
4.87k
    return_status = ih264e_put_rbsp_trailing_bits(ps_bitstrm);
602
603
4.87k
    return return_status;
604
4.87k
}
605
606
/**
607
******************************************************************************
608
*
609
* @brief Generates PPS (Picture Parameter Set)
610
*
611
* @par   Description
612
*  Generate Picture Parameter Set as per Section 7.3.2.2
613
*
614
* @param[in]   ps_bitstrm
615
*  pointer to bitstream context (handle)
616
*
617
* @param[in]   ps_pps
618
*  pointer to structure containing PPS data
619
*
620
* @return      success or failure error code
621
*
622
******************************************************************************
623
*/
624
WORD32 ih264e_generate_pps(bitstrm_t *ps_bitstrm, pps_t *ps_pps, sps_t *ps_sps)
625
4.87k
{
626
4.87k
    WORD32 return_status = IH264E_SUCCESS;
627
628
    /* Insert the NAL start code */
629
4.87k
    return_status = ih264e_put_nal_start_code_prefix(ps_bitstrm, 1);
630
4.87k
    if(return_status != IH264E_SUCCESS)
631
0
    {
632
0
        return return_status;
633
0
    }
634
635
    /* Insert Nal Unit Header */
636
4.87k
    PUT_BITS(ps_bitstrm, NAL_PPS_FIRST_BYTE, 8, return_status, "pps_header");
637
638
    /* pic_parameter_set_id */
639
4.87k
    PUT_BITS_UEV(ps_bitstrm, ps_pps->u1_pps_id, return_status, "pic_parameter_set_id");
640
641
    /* seq_parameter_set_id */
642
4.87k
    PUT_BITS_UEV(ps_bitstrm, ps_pps->u1_sps_id, return_status, "seq_parameter_set_id");
643
644
    /* Entropy coding : 0-VLC; 1 - CABAC */
645
4.87k
    PUT_BITS(ps_bitstrm, ps_pps->u1_entropy_coding_mode_flag, 1, return_status, "Entropy coding : 0-VLC; 1 - CABAC");
646
647
    /* Pic order present flag */
648
4.87k
    PUT_BITS(ps_bitstrm, ps_pps->u1_pic_order_present_flag, 1, return_status, "Pic order present flag");
649
650
    /* Number of slice groups */
651
4.87k
    PUT_BITS_UEV(ps_bitstrm, ps_pps->u1_num_slice_groups - 1, return_status, "Number of slice groups");
652
653
4.87k
    if (ps_pps->u1_num_slice_groups > 1)
654
0
    {
655
        /* TODO_LATER: Currently the number of slice groups minus 1 is 0.
656
         * If this is not the case, we have to add Slice group map type to the bit stream*/
657
0
    }
658
659
    /* num_ref_idx_l0_default_active_minus1 */
660
4.87k
    PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_num_ref_idx_l0_default_active - 1, return_status, "num_ref_idx_l0_default_active_minus1");
661
662
    /* num_ref_idx_l1_default_active_minus1 */
663
4.87k
    PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_num_ref_idx_l1_default_active - 1, return_status, "num_ref_idx_l1_default_active_minus1");
664
665
    /* weighted_pred_flag */
666
4.87k
    PUT_BITS(ps_bitstrm, ps_pps->i1_weighted_pred_flag, 1, return_status, "weighted_pred_flag");
667
668
    /* weighted_bipred_flag */
669
4.87k
    PUT_BITS(ps_bitstrm, ps_pps->i1_weighted_bipred_idc, 2, return_status, "weighted_bipred_idc");
670
671
    /* pic_init_qp_minus26 */
672
4.87k
    PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_pic_init_qp - 26, return_status, "pic_init_qp_minus26");
673
674
    /* pic_init_qs_minus26 */
675
4.87k
    PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_pic_init_qs - 26, return_status, "pic_init_qs_minus26");
676
677
    /* chroma_qp_index_offset */
678
4.87k
    PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_chroma_qp_index_offset, return_status, "chroma_qp_index_offset");
679
680
    /* deblocking_filter_control_present_flag */
681
4.87k
    PUT_BITS(ps_bitstrm, ps_pps->i1_deblocking_filter_control_present_flag, 1, return_status, "deblocking_filter_control_present_flag");
682
683
    /* constrained_intra_pred_flag */
684
4.87k
    PUT_BITS(ps_bitstrm, ps_pps->i1_constrained_intra_pred_flag, 1, return_status, "constrained_intra_pred_flag");
685
686
    /*redundant_pic_cnt_present_flag */
687
4.87k
    PUT_BITS(ps_bitstrm, ps_pps->i1_redundant_pic_cnt_present_flag, 1, return_status, "redundant_pic_cnt_present_flag");
688
689
4.87k
    if (ps_sps->u1_profile_idc >= IH264_PROFILE_HIGH)
690
0
    {
691
        /* transform_8x8_mode_flag */
692
0
        PUT_BITS(ps_bitstrm, ps_pps->i1_transform_8x8_mode_flag, 1, return_status, "transform_8x8_mode_flag");
693
694
        /* pic_scaling_matrix_present_flag */
695
0
        PUT_BITS(ps_bitstrm, ps_pps->i1_pic_scaling_matrix_present_flag, 1, return_status, "pic_scaling_matrix_present_flag");
696
697
0
        if(ps_pps->i1_pic_scaling_matrix_present_flag)
698
0
        {
699
            /* TODO_LATER: Will be enabled once scaling list support is added */
700
0
        }
701
702
        /* Second chroma QP offset */
703
0
        PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_second_chroma_qp_index_offset, return_status, "Second chroma QP offset");
704
0
    }
705
706
4.87k
    return_status = ih264e_put_rbsp_trailing_bits(ps_bitstrm);
707
708
4.87k
    return return_status;
709
4.87k
}
710
711
/**
712
******************************************************************************
713
*
714
* @brief Generates SEI (Supplemental Enhancement Information)
715
*
716
* @par   Description
717
*  This function generates Supplemental Enhancement Information header as per the spec
718
*
719
* @param[in]   ps_bitstrm
720
*  pointer to bitstream context (handle)
721
*
722
* @param[in]   ps_sei
723
*  pointer to structure containing SEI data
724
*
725
* @return      success or failure error code
726
*
727
******************************************************************************
728
*/
729
IH264E_ERROR_T ih264e_generate_sei(bitstrm_t *ps_bitstrm, sei_params_t *ps_sei,
730
                                                        UWORD32 u4_insert_per_idr)
731
27.6k
{
732
27.6k
    WORD32 return_status = IH264E_SUCCESS;
733
27.6k
    WORD8  i1_nal_unit_type = NAL_SEI;
734
27.6k
    WORD8  i1_nal_ref_idc = 0;
735
736
    /* Insert Start Code */
737
27.6k
    return_status = ih264e_put_nal_start_code_prefix(ps_bitstrm, 1);
738
27.6k
    if(return_status != IH264E_SUCCESS)
739
0
    {
740
0
        return return_status;
741
0
    }
742
743
    /* Insert Nal Unit Header */
744
27.6k
    return_status = ih264e_generate_nal_unit_header(ps_bitstrm,
745
27.6k
                                                    i1_nal_unit_type, i1_nal_ref_idc);
746
27.6k
    if(return_status != IH264E_SUCCESS)
747
0
    {
748
0
        return return_status;
749
0
    }
750
    /* Mastering Display Color SEI */
751
27.6k
    if(1 == ps_sei->u1_sei_mdcv_params_present_flag && u4_insert_per_idr)
752
7.92k
    {
753
7.92k
        return_status = ih264e_put_sei_msg(IH264_SEI_MASTERING_DISP_COL_VOL,
754
7.92k
                                            ps_sei, ps_bitstrm);
755
7.92k
        if(return_status != IH264E_SUCCESS)
756
0
        {
757
0
            return return_status;
758
0
        }
759
7.92k
    }
760
761
    /* Content Light Level Information*/
762
27.6k
    if(1 == ps_sei->u1_sei_cll_params_present_flag && u4_insert_per_idr)
763
7.04k
    {
764
7.04k
        return_status = ih264e_put_sei_msg(IH264_SEI_CONTENT_LIGHT_LEVEL_DATA,
765
7.04k
                                            ps_sei, ps_bitstrm);
766
7.04k
        if(return_status != IH264E_SUCCESS)
767
0
        {
768
0
            return return_status;
769
0
        }
770
7.04k
    }
771
772
    /* Ambient viewing environment SEI */
773
27.6k
    if(1 == ps_sei->u1_sei_ave_params_present_flag && u4_insert_per_idr)
774
7.88k
    {
775
7.88k
        return_status = ih264e_put_sei_msg(IH264_SEI_AMBIENT_VIEWING_ENVIRONMENT,
776
7.88k
                                            ps_sei, ps_bitstrm);
777
7.88k
        if(return_status != IH264E_SUCCESS)
778
0
        {
779
0
            return return_status;
780
0
        }
781
7.88k
    }
782
783
    /* Content color volume Information*/
784
27.6k
    if(1 == ps_sei->u1_sei_ccv_params_present_flag)
785
11.5k
    {
786
11.5k
        return_status = ih264e_put_sei_msg(IH264_SEI_CONTENT_COLOR_VOLUME,
787
11.5k
                                            ps_sei, ps_bitstrm);
788
11.5k
        if(return_status != IH264E_SUCCESS)
789
0
        {
790
0
            return return_status;
791
0
        }
792
11.5k
    }
793
794
    /* Shutter Interval Information*/
795
27.6k
    if(1 == ps_sei->u1_sei_sii_params_present_flag)
796
14.9k
    {
797
14.9k
        return_status = ih264e_put_sei_msg(IH264_SEI_SHUTTER_INTERVAL_INFO, ps_sei, ps_bitstrm);
798
14.9k
        if(return_status != IH264E_SUCCESS)
799
0
        {
800
0
            return return_status;
801
0
        }
802
14.9k
    }
803
804
    /* rbsp trailing bits */
805
27.6k
    return_status = ih264e_put_rbsp_trailing_bits(ps_bitstrm);
806
807
27.6k
    return return_status;
808
27.6k
}
809
810
/**
811
******************************************************************************
812
*
813
* @brief Generates Slice Header
814
*
815
* @par   Description
816
*  Generate Slice Header as per Section 7.3.5.1
817
*
818
* @param[inout]   ps_bitstrm
819
*  pointer to bitstream context for generating slice header
820
*
821
* @param[in]   ps_slice_hdr
822
*  pointer to slice header params
823
*
824
* @param[in]   ps_pps
825
*  pointer to pps params referred by slice
826
*
827
* @param[in]   ps_sps
828
*  pointer to sps params referred by slice
829
*
830
* @param[out]   ps_dup_bit_strm_ent_offset
831
*  Bitstream struct to store bitstream state
832
*
833
* @param[out]   pu4_first_slice_start_offset
834
*  first slice offset is returned
835
*
836
* @return      success or failure error code
837
*
838
******************************************************************************
839
*/
840
WORD32 ih264e_generate_slice_header(bitstrm_t *ps_bitstrm,
841
                                    slice_header_t *ps_slice_hdr,
842
                                    pps_t *ps_pps,
843
                                    sps_t *ps_sps)
844
44.1k
{
845
846
44.1k
    WORD32 return_status = IH264E_SUCCESS;
847
848
    /* Insert start code */
849
44.1k
    return_status = ih264e_put_nal_start_code_prefix(ps_bitstrm, 1);
850
44.1k
    if(return_status != IH264E_SUCCESS)
851
0
    {
852
0
        return return_status;
853
0
    }
854
    /* Insert Nal Unit Header */
855
44.1k
    return_status = ih264e_generate_nal_unit_header(ps_bitstrm, ps_slice_hdr->i1_nal_unit_type, ps_slice_hdr->i1_nal_unit_idc);
856
44.1k
    if(return_status != IH264E_SUCCESS)
857
0
    {
858
0
        return return_status;
859
0
    }
860
    /* first_mb_in_slice */
861
44.1k
    PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u2_first_mb_in_slice, return_status, "first_mb_in_slice");
862
863
    /* slice_type */
864
44.1k
    PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u1_slice_type, return_status, "slice_type");
865
866
    /* pic_parameter_set_id */
867
44.1k
    PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u1_pps_id, return_status, "pic_parameter_set_id");
868
869
    /* frame_num */
870
44.1k
    PUT_BITS(ps_bitstrm, ps_slice_hdr->i4_frame_num, ps_sps->i1_log2_max_frame_num, return_status, "frame_num");
871
872
44.1k
    if (!ps_sps->i1_frame_mbs_only_flag)
873
0
    {
874
        /* field_pic_flag */
875
0
        PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_field_pic_flag, 1, return_status, "field_pic_flag");
876
877
0
        if(ps_slice_hdr->i1_field_pic_flag)
878
0
        {
879
            /* bottom_field_flag */
880
0
            PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_bottom_field_flag, 1, return_status, "bottom_field_flag");
881
0
        }
882
0
    }
883
884
44.1k
    if (ps_slice_hdr->i1_nal_unit_type == 5)
885
9.93k
    {
886
        /* u2_idr_pic_id */
887
9.93k
        PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u2_idr_pic_id, return_status, "u2_idr_pic_id");
888
9.93k
    }
889
890
44.1k
    if (ps_sps->i1_pic_order_cnt_type == 0)
891
28.9k
    {
892
        /* pic_order_cnt_lsb */
893
28.9k
        PUT_BITS(ps_bitstrm, ps_slice_hdr->i4_pic_order_cnt_lsb, ps_sps->i1_log2_max_pic_order_cnt_lsb, return_status, "pic_order_cnt_lsb");
894
895
28.9k
        if(ps_pps->u1_pic_order_present_flag && !ps_slice_hdr->i1_field_pic_flag)
896
0
        {
897
            /* delta_pic_order_cnt_bottom */
898
0
            PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i4_delta_pic_order_cnt_bottom, return_status, "delta_pic_order_cnt_bottom");
899
0
        }
900
28.9k
    }
901
902
44.1k
    if (ps_sps->i1_pic_order_cnt_type == 1 && !ps_sps->i1_delta_pic_order_always_zero_flag)
903
0
    {
904
        /* delta_pic_order_cnt[0] */
905
0
        PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->ai4_delta_pic_order_cnt[0], return_status, "delta_pic_order_cnt[0]");
906
907
0
        if (ps_pps->u1_pic_order_present_flag && !ps_slice_hdr->i1_field_pic_flag)
908
0
        {
909
            /* delta_pic_order_cnt[1] */
910
0
            PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->ai4_delta_pic_order_cnt[1], return_status, "delta_pic_order_cnt[1]");
911
0
        }
912
0
    }
913
914
44.1k
    if (ps_pps->i1_redundant_pic_cnt_present_flag)
915
0
    {
916
        /* redundant_pic_cnt */
917
0
        PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u1_redundant_pic_cnt, return_status, "redundant_pic_cnt");
918
0
    }
919
920
44.1k
    if (ps_slice_hdr->u1_slice_type == BSLICE)
921
15.6k
    {
922
        /* direct_spatial_mv_pred_flag */
923
15.6k
        PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_direct_spatial_mv_pred_flag, 1, return_status, "direct_spatial_mv_pred_flag");
924
15.6k
    }
925
926
44.1k
    if (ps_slice_hdr->u1_slice_type == PSLICE || ps_slice_hdr->u1_slice_type == SPSLICE || ps_slice_hdr->u1_slice_type == BSLICE)
927
32.0k
    {
928
        /* num_ref_idx_active_override_flag */
929
32.0k
        PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_num_ref_idx_active_override_flag, 1, return_status, "num_ref_idx_active_override_flag");
930
931
32.0k
        if (ps_slice_hdr->u1_num_ref_idx_active_override_flag)
932
0
        {
933
            /* num_ref_idx_l0_active_minus1 */
934
0
            PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_num_ref_idx_l0_active - 1, return_status, "num_ref_idx_l0_active_minus1");
935
936
0
            if (ps_slice_hdr->u1_slice_type == BSLICE)
937
0
            {
938
                /* num_ref_idx_l1_active_minus1 */
939
0
                PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_num_ref_idx_l1_active - 1, return_status, "num_ref_idx_l1_active_minus1");
940
0
            }
941
0
        }
942
32.0k
    }
943
944
    /* ref_idx_reordering */
945
    /* TODO: ref_idx_reordering */
946
44.1k
    if ((ps_slice_hdr->u1_slice_type != ISLICE) && (ps_slice_hdr->u1_slice_type != SISLICE))
947
32.0k
    {
948
        /* ref_pic_list_reordering_flag_l0 */
949
32.0k
        PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_ref_idx_reordering_flag_l0, 1, return_status, "ref_pic_list_reordering_flag_l0");
950
951
32.0k
        if (ps_slice_hdr->u1_ref_idx_reordering_flag_l0)
952
0
        {
953
954
0
        }
955
32.0k
    }
956
957
44.1k
    if (ps_slice_hdr->u1_slice_type == BSLICE)
958
15.6k
    {
959
        /* ref_pic_list_reordering_flag_l1 */
960
15.6k
        PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_ref_idx_reordering_flag_l1, 1, return_status, "ref_pic_list_reordering_flag_l1");
961
962
15.6k
        if (ps_slice_hdr->u1_ref_idx_reordering_flag_l1)
963
0
        {
964
965
0
        }
966
15.6k
    }
967
968
44.1k
    if ((ps_pps->i1_weighted_pred_flag &&
969
44.1k
                    (ps_slice_hdr->u1_slice_type == PSLICE || ps_slice_hdr->u1_slice_type == SPSLICE)) ||
970
44.1k
                    (ps_slice_hdr->u1_slice_type == BSLICE && ps_pps->i1_weighted_bipred_idc == 1))
971
0
    {
972
        /* TODO_LATER: Currently there is no support for weighted prediction.
973
         This needs to be updated when the support is added */
974
0
    }
975
976
44.1k
    if (ps_slice_hdr->i1_nal_unit_idc != 0)
977
28.4k
    {
978
28.4k
        if (ps_slice_hdr->i1_nal_unit_type == 5)
979
9.93k
        {
980
            /* no_output_of_prior_pics_flag  */
981
9.93k
            PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_no_output_of_prior_pics_flag , 1, return_status, "no_output_of_prior_pics_flag ");
982
983
            /* long_term_reference_flag  */
984
9.93k
            PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_long_term_reference_flag , 1, return_status, "long_term_reference_flag ");
985
9.93k
        }
986
18.5k
        else
987
18.5k
        {
988
            /* adaptive_ref_pic_marking_mode_flag  */
989
18.5k
            PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_adaptive_ref_pic_marking_mode_flag , 1, return_status, "adaptive_ref_pic_marking_mode_flag ");
990
991
18.5k
            if (ps_slice_hdr->u1_adaptive_ref_pic_marking_mode_flag)
992
0
            {
993
                /* TODO: if the reference picture marking mode is adaptive
994
                 add these fields in the bit-stream */
995
0
            }
996
18.5k
        }
997
28.4k
    }
998
999
44.1k
    if (ps_slice_hdr->u1_entropy_coding_mode_flag && ps_slice_hdr->u1_slice_type != ISLICE &&
1000
44.1k
                    ps_slice_hdr->u1_slice_type != SISLICE)
1001
10.1k
    {
1002
        /* cabac_init_idc */
1003
10.1k
        PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_cabac_init_idc, return_status, "cabac_init_idc");
1004
10.1k
    }
1005
1006
    /* slice_qp_delta */
1007
44.1k
    PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_slice_qp - ps_pps->i1_pic_init_qp, return_status, "slice_qp_delta");
1008
1009
44.1k
    if (ps_slice_hdr->u1_slice_type == SPSLICE || ps_slice_hdr->u1_slice_type == SISLICE)
1010
0
    {
1011
0
        if (ps_slice_hdr->u1_slice_type == SPSLICE)
1012
0
        {
1013
            /* sp_for_switch_flag */
1014
0
            PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_sp_for_switch_flag , 1, return_status, "sp_for_switch_flag");
1015
0
        }
1016
        /* slice_qs_delta */
1017
0
        PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->u1_slice_qs - ps_pps->i1_pic_init_qs, return_status, "slice_qs_delta");
1018
0
    }
1019
1020
44.1k
    if (ps_pps->i1_deblocking_filter_control_present_flag)
1021
44.1k
    {
1022
        /* disable_deblocking_filter_idc */
1023
44.1k
        PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u1_disable_deblocking_filter_idc, return_status, "disable_deblocking_filter_idc");
1024
1025
44.1k
        if(ps_slice_hdr->u1_disable_deblocking_filter_idc != 1)
1026
30.1k
        {
1027
            /* slice_alpha_c0_offset_div2 */
1028
30.1k
            PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_slice_alpha_c0_offset_div2, return_status, "slice_alpha_c0_offset_div2");
1029
1030
            /* slice_beta_offset_div2 */
1031
30.1k
            PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_slice_beta_offset_div2, return_status, "slice_beta_offset_div2");
1032
30.1k
        }
1033
44.1k
    }
1034
1035
44.1k
    if (ps_slice_hdr->u1_num_slice_groups_minus1 > 0 &&
1036
44.1k
                    ps_pps->u1_slice_group_map_type >= 3 &&
1037
44.1k
                    ps_pps->u1_slice_group_map_type <= 5)
1038
0
    {
1039
        /* slice_group_change_cycle */
1040
        /* TODO_LATER: Currently the number of slice groups minus 1 is 0.
1041
         * If this is not the case, we have to add Slice group map type to the bit stream */
1042
0
    }
1043
1044
44.1k
    return return_status;
1045
44.1k
}
1046
1047
/**
1048
******************************************************************************
1049
*
1050
* @brief Populates VUI structure
1051
*
1052
* @par   Description
1053
*  Populates VUI structure for its use in header generation
1054
*
1055
* @param[in]   ps_codec
1056
*  pointer to encoder context
1057
*
1058
* @return      success or failure error code
1059
*
1060
******************************************************************************
1061
*/
1062
IH264E_ERROR_T ih264e_populate_vui(codec_t *ps_codec)
1063
4.87k
{
1064
    /* vui params */
1065
4.87k
    vui_t *ps_vui = &ps_codec->s_cfg.s_vui;
1066
1067
    /* active sps params */
1068
4.87k
    sps_t *ps_sps = ps_codec->ps_sps_base + ps_codec->i4_sps_id;
1069
1070
4.87k
    ps_vui->u1_nal_hrd_parameters_present_flag = 0;
1071
1072
4.87k
    ps_vui->u1_vcl_hrd_parameters_present_flag = 0;
1073
1074
4.87k
    ps_vui->u1_bitstream_restriction_flag = 1;
1075
1076
4.87k
    ps_vui->u1_motion_vectors_over_pic_boundaries_flag = 1;
1077
1078
4.87k
    ps_vui->u1_max_bytes_per_pic_denom = 0;
1079
1080
4.87k
    ps_vui->u1_max_bits_per_mb_denom = 0;
1081
1082
4.87k
    ps_vui->u1_log2_max_mv_length_horizontal = 16;
1083
1084
4.87k
    ps_vui->u1_log2_max_mv_length_vertical = 16;
1085
1086
4.87k
    if (ps_codec->s_cfg.u4_num_bframes == 0)
1087
2.01k
    {
1088
2.01k
        ps_vui->u1_num_reorder_frames = 0;
1089
2.01k
    }
1090
2.86k
    else
1091
2.86k
    {
1092
2.86k
        ps_vui->u1_num_reorder_frames = 1;
1093
2.86k
    }
1094
1095
4.87k
    ps_vui->u1_max_dec_frame_buffering = ps_sps->u1_max_num_ref_frames;
1096
1097
4.87k
    return IH264E_SUCCESS;
1098
4.87k
}
1099
1100
/**
1101
******************************************************************************
1102
*
1103
* @brief Populates sps structure
1104
*
1105
* @par   Description
1106
*  Populates sps structure for its use in header generation
1107
*
1108
* @param[in]   ps_codec
1109
*  pointer to encoder context
1110
*
1111
* @param[out]  ps_sps
1112
*  pointer to sps params that needs to be populated
1113
*
1114
* @return      success or failure error code
1115
*
1116
******************************************************************************
1117
*/
1118
IH264E_ERROR_T ih264e_populate_sps(codec_t *ps_codec, sps_t *ps_sps)
1119
4.87k
{
1120
    /* active config parameters */
1121
4.87k
    cfg_params_t    *ps_cfg = &(ps_codec->s_cfg);
1122
1123
//    /* level */
1124
//    IH264_LEVEL_T   level_idc;
1125
1126
    /* error_status */
1127
4.87k
    IH264E_ERROR_T i4_err_code = IH264E_FAIL;
1128
1129
    /* profile */
1130
    /*
1131
     * Baseline profile supports, 8 bits per sample, 4:2:0 format, CAVLC.
1132
     * B frames are not allowed. Further, Flexible mb ordering, Redundant slices, Arbitrary slice ordering are supported.
1133
     * The constrained baseline profile is baseline profile minus ASO, FMO and redundant slices.
1134
     * To the constrained baseline profile if we add support for B slices, support for encoding interlaced frames,
1135
     * support for weighted prediction and introduce CABAC entropy coding then we have Main Profile.
1136
     */
1137
4.87k
    if ((ps_cfg->u4_num_bframes) || (ps_cfg->e_content_type != IV_PROGRESSIVE) ||
1138
4.87k
        (ps_cfg->u4_entropy_coding_mode == CABAC) || (ps_cfg->u4_weighted_prediction))
1139
3.61k
    {
1140
3.61k
        ps_sps->u1_profile_idc = IH264_PROFILE_MAIN;
1141
3.61k
    }
1142
1.26k
    else
1143
1.26k
    {
1144
1.26k
        ps_sps->u1_profile_idc = IH264_PROFILE_BASELINE;
1145
1.26k
    }
1146
1147
    /* level */
1148
4.87k
    ps_sps->u1_level_idc = MAX(ps_cfg->u4_max_level,
1149
4.87k
                               (UWORD32)ih264e_get_min_level(ps_cfg->u4_max_wd, ps_cfg->u4_max_ht));
1150
1151
    /* constrained flags */
1152
    /*
1153
     * baseline profile automatically implies set 0 flag
1154
     */
1155
4.87k
    ps_sps->u1_constraint_set0_flag = (ps_sps->u1_profile_idc == IH264_PROFILE_BASELINE);
1156
    /*
1157
     * main profile automatically implies set 1 flag
1158
     * Although the encoder says it supports Baseline profile it actually supports constrained
1159
     * baseline profile as ASO, FMO and redundant slices are not supported
1160
     */
1161
4.87k
    ps_sps->u1_constraint_set1_flag = (ps_sps->u1_profile_idc <= IH264_PROFILE_MAIN);
1162
    /*
1163
     * extended profile is not supported
1164
     */
1165
4.87k
    ps_sps->u1_constraint_set2_flag = 0x00;
1166
    /*
1167
     * level 1b or level 11
1168
     */
1169
4.87k
    if (ps_sps->u1_level_idc == IH264_LEVEL_1B)
1170
0
    {
1171
0
        ps_sps->u1_constraint_set3_flag = 0;
1172
0
        ps_sps->u1_level_idc = IH264_LEVEL_11;
1173
0
    }
1174
4.87k
    else
1175
4.87k
    {
1176
4.87k
        ps_sps->u1_constraint_set3_flag = 0;
1177
4.87k
    }
1178
1179
    /* active sps id */
1180
4.87k
    ps_sps->u1_sps_id = ps_codec->i4_sps_id;
1181
1182
4.87k
    if (ps_sps->u1_profile_idc >= IH264_PROFILE_HIGH)
1183
0
    {
1184
        /* chroma format idc */
1185
0
        ps_sps->u1_chroma_format_idc = CHROMA_FMT_IDC_YUV420;
1186
1187
        /* residual_colour_transform_flag */
1188
0
        ps_sps->i1_residual_colour_transform_flag = 0;
1189
1190
        /* luma bit depth 8 */
1191
0
        ps_sps->i1_bit_depth_luma = 8;
1192
1193
        /* chroma bit depth 8 */
1194
0
        ps_sps->i1_bit_depth_chroma = 8;
1195
1196
        /* qpprime_y_zero_transform_bypass_flag */
1197
0
        ps_sps->i1_qpprime_y_zero_transform_bypass_flag = 0;
1198
1199
        /* seq_scaling_matrix_present_flag */
1200
0
        ps_sps->i1_seq_scaling_matrix_present_flag = 0;
1201
1202
0
        if (ps_sps->i1_seq_scaling_matrix_present_flag)
1203
0
        {
1204
            /* TODO_LATER: Will be enabled once scaling list support is added */
1205
0
        }
1206
0
    }
1207
1208
    /* log2_max_frame_num_minus4 */
1209
4.87k
    ps_sps->i1_log2_max_frame_num = 16;
1210
1211
    /* pic_order_cnt_type */
1212
4.87k
    ps_sps->i1_pic_order_cnt_type = 2;
1213
1214
4.87k
    if (ps_codec->i4_non_ref_frames_in_stream)
1215
2.86k
    {
1216
2.86k
        ps_sps->i1_pic_order_cnt_type = 0;
1217
2.86k
    }
1218
1219
    /* log2_max_pic_order_cnt_lsb_minus4 */
1220
4.87k
    ps_sps->i1_log2_max_pic_order_cnt_lsb = 8;
1221
1222
    /* TODO : add support for other poc types */
1223
4.87k
    if (ps_sps->i1_pic_order_cnt_type == 0)
1224
2.86k
    {
1225
1226
2.86k
    }
1227
2.01k
    else if (ps_sps->i1_pic_order_cnt_type == 1)
1228
0
    {
1229
1230
0
    }
1231
1232
    /* num_ref_frames */
1233
    /* TODO : Should we have a flexible num ref frames */
1234
4.87k
    if (ps_codec->s_cfg.u4_num_bframes > 0)
1235
2.86k
    {
1236
2.86k
        ps_sps->u1_max_num_ref_frames = 2;
1237
2.86k
    }
1238
2.01k
    else
1239
2.01k
    {
1240
2.01k
        ps_sps->u1_max_num_ref_frames = 1;
1241
2.01k
    }
1242
1243
    /* gaps_in_frame_num_value_allowed_flag */
1244
4.87k
    ps_sps->i1_gaps_in_frame_num_value_allowed_flag = 0;
1245
1246
    /* pic width in mb - 1 */
1247
4.87k
    ps_sps->i2_pic_width_in_mbs_minus1 = ps_cfg->i4_wd_mbs - 1;
1248
1249
    /* pic height in mb - 1 */
1250
4.87k
    ps_sps->i2_pic_height_in_map_units_minus1 = ps_cfg->i4_ht_mbs - 1;;
1251
1252
    /* frame_mbs_only_flag, no support for interlace encoding */
1253
4.87k
    ps_sps->i1_frame_mbs_only_flag = 1;
1254
1255
    /* mb_adaptive_frame_field_flag */
1256
4.87k
    if (ps_sps->i1_frame_mbs_only_flag == 0)
1257
0
    {
1258
0
        ps_sps->i1_mb_adaptive_frame_field_flag = 0;
1259
0
    }
1260
1261
    /* direct_8x8_inference_flag */
1262
4.87k
    if (ps_sps->u1_level_idc < IH264_LEVEL_30)
1263
0
    {
1264
0
        ps_sps->i1_direct_8x8_inference_flag = 0;
1265
0
    }
1266
4.87k
    else
1267
4.87k
    {
1268
4.87k
        ps_sps->i1_direct_8x8_inference_flag = 1;
1269
4.87k
    }
1270
1271
1272
    /* cropping params */
1273
    /*NOTE : Cropping values depend on the chroma format
1274
     * For our case ,decoder interprets the cropping values as 2*num pixels
1275
     * Hence the difference in the disp width and width must be halved before sending
1276
     * to get the expected results
1277
     */
1278
4.87k
    ps_sps->i1_frame_cropping_flag      = 0;
1279
4.87k
    ps_sps->i2_frame_crop_left_offset   = 0;
1280
4.87k
    ps_sps->i2_frame_crop_right_offset  = (ps_codec->s_cfg.u4_wd - ps_codec->s_cfg.u4_disp_wd)>>1;
1281
4.87k
    ps_sps->i2_frame_crop_top_offset    = 0;
1282
4.87k
    ps_sps->i2_frame_crop_bottom_offset = (ps_codec->s_cfg.u4_ht - ps_codec->s_cfg.u4_disp_ht)>>1;
1283
1284
4.87k
    if (ps_sps->i2_frame_crop_left_offset    ||
1285
4.87k
                    ps_sps->i2_frame_crop_right_offset   ||
1286
4.87k
                    ps_sps->i2_frame_crop_top_offset     ||
1287
4.87k
                    ps_sps->i2_frame_crop_bottom_offset)
1288
3.44k
    {
1289
3.44k
        ps_sps->i1_frame_cropping_flag      = 1;
1290
3.44k
    }
1291
1292
    /* vui params */
1293
4.87k
    ps_sps->i1_vui_parameters_present_flag = 1;
1294
1295
4.87k
    if (ps_sps->i1_vui_parameters_present_flag)
1296
4.87k
    {
1297
        /* populate vui params */
1298
4.87k
        ih264e_populate_vui(ps_codec);
1299
4.87k
    }
1300
1301
4.87k
    return i4_err_code;
1302
4.87k
}
1303
1304
/**
1305
******************************************************************************
1306
*
1307
* @brief Populates pps structure
1308
*
1309
* @par   Description
1310
*  Populates pps structure for its use in header generation
1311
*
1312
* @param[in]   ps_codec
1313
*  pointer to encoder context
1314
*
1315
* @param[out]  ps_pps
1316
*  pointer to pps params that needs to be populated
1317
*
1318
* @return      success or failure error code
1319
*
1320
******************************************************************************
1321
*/
1322
IH264E_ERROR_T ih264e_populate_pps(codec_t *ps_codec, pps_t *ps_pps)
1323
4.87k
{
1324
    /* active config parameters */
1325
4.87k
    cfg_params_t    *ps_cfg = &(ps_codec->s_cfg);
1326
1327
    /* seq_parameter_set_id */
1328
4.87k
    ps_pps->u1_sps_id = ps_codec->i4_sps_id;
1329
1330
    /* pic_parameter_set_id */
1331
4.87k
    ps_pps->u1_pps_id = ps_codec->i4_pps_id;
1332
1333
    /* entropy_coding_mode */
1334
4.87k
    ps_pps->u1_entropy_coding_mode_flag = ps_cfg->u4_entropy_coding_mode;
1335
1336
    /* pic_order_present_flag is unset if we don't have feilds */
1337
4.87k
    ps_pps->u1_pic_order_present_flag = 0;
1338
1339
    /* Currently number of slice groups supported are 1 */
1340
4.87k
    ps_pps->u1_num_slice_groups = 1;
1341
1342
4.87k
    if (ps_pps->u1_num_slice_groups - 1)
1343
0
    {
1344
        /* TODO_LATER: Currently the number of slice groups minus 1 is 0.
1345
         * If this is not the case, we have to add Slice group map type to the bit stream*/
1346
0
    }
1347
1348
    /* number of reference frames for list 0 */
1349
    /* FIXME : fix this hard coded value */
1350
4.87k
    ps_pps->i1_num_ref_idx_l0_default_active = 1;
1351
1352
    /* number of reference frames for list 1 */
1353
4.87k
    ps_pps->i1_num_ref_idx_l1_default_active = 1;
1354
1355
    /* weighted prediction for now is disabled */
1356
4.87k
    ps_pps->i1_weighted_pred_flag = 0;
1357
4.87k
    ps_pps->i1_weighted_bipred_idc = 0;
1358
1359
    /* The intent is to not signal qp from pps. Rather send the same in slice headers */
1360
4.87k
    ps_pps->i1_pic_init_qp = 0;
1361
1362
    /* The intent is to not signal qp from pps. Rather send the same in slice headers */
1363
4.87k
    ps_pps->i1_pic_init_qs = 0;
1364
1365
    /* The intent is to not signal qp from pps. Rather send the same in slice headers */
1366
4.87k
    ps_pps->i1_chroma_qp_index_offset = 0;
1367
1368
    /* deblocking filter flags present in slice header */
1369
4.87k
    ps_pps->i1_deblocking_filter_control_present_flag = 1;
1370
1371
    /* constrained intra prediction */
1372
4.87k
    ps_pps->i1_constrained_intra_pred_flag = ps_cfg->u4_constrained_intra_pred;
1373
1374
    /* sending redundant slices is not supported for now */
1375
4.87k
    ps_pps->i1_redundant_pic_cnt_present_flag = 0;
1376
1377
4.87k
    ps_pps->u1_slice_group_map_type = 0;
1378
1379
4.87k
    return IH264E_SUCCESS;
1380
4.87k
}
1381
1382
/**
1383
******************************************************************************
1384
*
1385
* @brief Populates slice header structure
1386
*
1387
* @par   Description
1388
*  Populates slice header structure for its use in header generation
1389
*
1390
* @param[in]  ps_proc
1391
*  pointer to proc context
1392
*
1393
* @param[out]  ps_slice_hdr
1394
*  pointer to slice header structure that needs to be populated
1395
*
1396
* @param[in]  ps_pps
1397
*  pointer to pps params structure referred by the slice
1398
*
1399
* @param[in]   ps_sps
1400
*  pointer to sps params referred by the pps
1401
*
1402
* @return      success or failure error code
1403
*
1404
******************************************************************************
1405
*/
1406
WORD32 ih264e_populate_slice_header(process_ctxt_t *ps_proc,
1407
                                    slice_header_t *ps_slice_hdr,
1408
                                    pps_t *ps_pps,
1409
                                    sps_t *ps_sps)
1410
44.1k
{
1411
    /* entropy context */
1412
44.1k
    entropy_ctxt_t *ps_entropy = &ps_proc->s_entropy;
1413
1414
44.1k
    codec_t *ps_codec = ps_proc->ps_codec;
1415
1416
44.1k
    if (ps_proc->ps_codec->u4_is_curr_frm_ref)
1417
28.4k
    {
1418
28.4k
        ps_slice_hdr->i1_nal_unit_idc = 3;
1419
28.4k
    }
1420
15.6k
    else
1421
15.6k
    {
1422
15.6k
        ps_slice_hdr->i1_nal_unit_idc = 0;
1423
15.6k
    }
1424
1425
    /* start mb address */
1426
44.1k
    ps_slice_hdr->u2_first_mb_in_slice = ps_entropy->i4_mb_start_add;
1427
1428
    /* slice type */
1429
44.1k
    ps_slice_hdr->u1_slice_type = ps_proc->i4_slice_type;
1430
1431
    /* pic_parameter_set_id */
1432
44.1k
    ps_slice_hdr->u1_pps_id = ps_pps->u1_pps_id;
1433
1434
    /* Separate color plane flag is 0,
1435
     * hence the syntax element color_plane_id not included */
1436
1437
    /* frame num */
1438
44.1k
    ps_slice_hdr->i4_frame_num = ps_proc->i4_frame_num;
1439
1440
    /* frame_mbs_only_flag, no support for interlace encoding */
1441
44.1k
    if (!ps_sps->i1_frame_mbs_only_flag)
1442
0
    {
1443
0
        ps_slice_hdr->i1_field_pic_flag = 0;
1444
1445
0
        if (ps_slice_hdr->i1_field_pic_flag)
1446
0
        {
1447
0
            ps_slice_hdr->i1_bottom_field_flag = 0;
1448
0
        }
1449
0
    }
1450
1451
    /* idr pic id */
1452
44.1k
    if (ps_proc->u4_is_idr)
1453
9.93k
    {
1454
9.93k
        ps_slice_hdr->u2_idr_pic_id = ps_proc->u4_idr_pic_id;
1455
9.93k
        ps_slice_hdr->i1_nal_unit_type = 5;
1456
9.93k
    }
1457
34.1k
    else
1458
34.1k
    {
1459
34.1k
        ps_slice_hdr->i1_nal_unit_type = 1;
1460
34.1k
    }
1461
1462
44.1k
    if (ps_sps->i1_pic_order_cnt_type == 0)
1463
28.9k
    {
1464
1465
28.9k
        WORD32 i4_poc;
1466
28.9k
        i4_poc = ps_codec->i4_poc;
1467
28.9k
        i4_poc %= (1 << ps_sps->i1_log2_max_pic_order_cnt_lsb);
1468
28.9k
        ps_slice_hdr->i4_pic_order_cnt_lsb = i4_poc;
1469
28.9k
    }
1470
    /* TODO add support for poc type 1 */
1471
15.1k
    else if (ps_sps->i1_pic_order_cnt_type == 1)
1472
0
    {
1473
1474
0
    }
1475
1476
1477
    /*
1478
     * redundant slices are not currently supported.
1479
     * Hence the syntax element redundant slice cnt is not initialized
1480
     */
1481
44.1k
    if (ps_pps->i1_redundant_pic_cnt_present_flag)
1482
0
    {
1483
1484
0
    }
1485
1486
    /* direct spatial mv pred flag */
1487
44.1k
    if (ps_proc->i4_slice_type == BSLICE)
1488
15.6k
    {
1489
15.6k
        ps_slice_hdr->u1_direct_spatial_mv_pred_flag = 1;
1490
15.6k
    }
1491
1492
44.1k
    if (ps_proc->i4_slice_type == PSLICE || ps_proc->i4_slice_type == SPSLICE || ps_proc->i4_slice_type == BSLICE)
1493
32.0k
    {
1494
        /* num_ref_idx_active_override_flag */
1495
32.0k
        ps_slice_hdr->u1_num_ref_idx_active_override_flag = 0;
1496
1497
32.0k
        if (ps_slice_hdr->u1_num_ref_idx_active_override_flag)
1498
0
        {
1499
            /* num_ref_idx_l0_active_minus1 */
1500
1501
0
            if (ps_proc->i4_slice_type == BSLICE)
1502
0
            {
1503
                /* num_ref_idx_l1_active_minus1 */
1504
1505
0
            }
1506
0
        }
1507
32.0k
    }
1508
1509
    /* ref_idx_reordering */
1510
    /* TODO: ref_idx_reordering */
1511
44.1k
    if ((ps_proc->i4_slice_type != ISLICE) && (ps_proc->i4_slice_type != SISLICE))
1512
32.0k
    {
1513
        /* ref_pic_list_reordering_flag_l0 */
1514
32.0k
        ps_slice_hdr->u1_ref_idx_reordering_flag_l0 = 0;
1515
1516
32.0k
        if (ps_slice_hdr->u1_ref_idx_reordering_flag_l0)
1517
0
        {
1518
1519
0
        }
1520
1521
        /* ref_pic_list_reordering_flag_l1 */
1522
32.0k
        ps_slice_hdr->u1_ref_idx_reordering_flag_l1 = 0;
1523
1524
32.0k
        if (ps_slice_hdr->u1_ref_idx_reordering_flag_l1)
1525
0
        {
1526
1527
0
        }
1528
32.0k
    }
1529
1530
1531
    /* Currently we do not support weighted pred */
1532
    /* ps_slice_hdr->u1_weighted_bipred_idc = 0; */
1533
1534
44.1k
    if ((ps_pps->i1_weighted_pred_flag &&
1535
44.1k
                    (ps_proc->i4_slice_type == PSLICE || ps_proc->i4_slice_type == SPSLICE)) ||
1536
44.1k
                    (ps_proc->i4_slice_type == BSLICE && ps_pps->i1_weighted_bipred_idc == 1))
1537
0
    {
1538
        /* TODO_LATER: Currently there is no support for weighted prediction.
1539
             This needs to be updated when the support is added */
1540
0
    }
1541
1542
44.1k
    if (ps_slice_hdr->i1_nal_unit_idc != 0)
1543
28.4k
    {
1544
28.4k
        if (ps_slice_hdr->i1_nal_unit_type == 5)
1545
9.93k
        {
1546
            /* no_output_of_prior_pics_flag  */
1547
9.93k
            ps_slice_hdr->u1_no_output_of_prior_pics_flag = 0;
1548
1549
            /* long_term_reference_flag  */
1550
9.93k
            ps_slice_hdr->u1_long_term_reference_flag = 0;
1551
9.93k
        }
1552
18.5k
        else
1553
18.5k
        {
1554
            /* adaptive_ref_pic_marking_mode_flag  */
1555
18.5k
            ps_slice_hdr->u1_adaptive_ref_pic_marking_mode_flag = 0;
1556
1557
18.5k
            if (ps_slice_hdr->u1_adaptive_ref_pic_marking_mode_flag)
1558
0
            {
1559
                /* TODO: if the reference picture marking mode is adaptive
1560
                     add these fields in the bit-stream */
1561
0
            }
1562
18.5k
        }
1563
28.4k
    }
1564
1565
    /* entropy coding mode flag */
1566
44.1k
    ps_slice_hdr->u1_entropy_coding_mode_flag = ps_entropy->u1_entropy_coding_mode_flag;
1567
1568
44.1k
    if (ps_slice_hdr->u1_entropy_coding_mode_flag && ps_proc->i4_slice_type != ISLICE &&
1569
44.1k
                    ps_proc->i4_slice_type != SISLICE)
1570
10.1k
    {
1571
        /* cabac_init_idc */
1572
10.1k
    }
1573
1574
    /* slice qp */
1575
44.1k
    ps_slice_hdr->i1_slice_qp = ps_proc->u4_frame_qp;
1576
1577
44.1k
    if (ps_proc->i4_slice_type == SPSLICE || ps_proc->i4_slice_type == SISLICE)
1578
0
    {
1579
0
        if (ps_proc->i4_slice_type == SPSLICE)
1580
0
        {
1581
            /* sp_for_switch_flag */
1582
0
        }
1583
        /* slice_qs_delta */
1584
0
    }
1585
1586
44.1k
    if (ps_pps->i1_deblocking_filter_control_present_flag)
1587
44.1k
    {
1588
        /* disable_deblocking_filter_idc */
1589
44.1k
        ps_slice_hdr->u1_disable_deblocking_filter_idc = ps_proc->u4_disable_deblock_level;
1590
1591
44.1k
        if (ps_slice_hdr->u1_disable_deblocking_filter_idc != 1)
1592
30.1k
        {
1593
            /* slice_alpha_c0_offset_div2 */
1594
30.1k
            ps_slice_hdr->i1_slice_alpha_c0_offset_div2 = 0;
1595
1596
            /* slice_beta_offset_div2 */
1597
30.1k
            ps_slice_hdr->i1_slice_beta_offset_div2 = 0;
1598
30.1k
        }
1599
44.1k
    }
1600
44.1k
    ps_slice_hdr->u1_num_slice_groups_minus1 = 0;
1601
44.1k
    if(ps_slice_hdr->u1_num_slice_groups_minus1 > 0 &&
1602
44.1k
        ps_pps->u1_slice_group_map_type >= 3 &&
1603
44.1k
        ps_pps->u1_slice_group_map_type <= 5)
1604
0
    {
1605
        /* slice_group_change_cycle */
1606
        /* TODO_LATER: Currently the number of slice groups minus 1 is 0.
1607
         * If this is not the case, we have to add Slice group map type to the bit stream */
1608
0
    }
1609
1610
44.1k
    ps_slice_hdr->i1_cabac_init_idc = CABAC_INIT_IDC;
1611
1612
44.1k
    return IH264E_SUCCESS;
1613
44.1k
}
1614
1615
/**
1616
******************************************************************************
1617
*
1618
* @brief inserts FILLER Nal Unit.
1619
*
1620
* @par   Description
1621
*  In constant bit rate rc mode, when the bits generated by the codec is
1622
*  underflowing the target bit rate, the encoder library inserts filler nal unit.
1623
*
1624
* @param[in]    ps_bitstrm
1625
*  pointer to bitstream context (handle)
1626
*
1627
* @param[in]    insert_fill_bytes
1628
*  Number of fill bytes to be inserted
1629
*
1630
* @return      success or failure error code
1631
*
1632
******************************************************************************
1633
*/
1634
IH264E_ERROR_T ih264e_add_filler_nal_unit(bitstrm_t *ps_bitstrm,
1635
                                          WORD32 insert_fill_bytes)
1636
1.68k
{
1637
1.68k
    WORD32  i4_num_words_to_fill, i4_words_filled;
1638
1639
1.68k
    IH264E_ERROR_T return_status = IH264E_SUCCESS;
1640
1641
    /* Insert the NAL start code */
1642
1.68k
    return_status = ih264e_put_nal_start_code_prefix(ps_bitstrm, 1);
1643
1.68k
    if(return_status != IH264E_SUCCESS)
1644
2
    {
1645
2
        return return_status;
1646
2
    }
1647
1648
1.67k
    if (ps_bitstrm->u4_strm_buf_offset + insert_fill_bytes >= ps_bitstrm->u4_max_strm_size)
1649
269
    {
1650
269
        return (IH264E_BITSTREAM_BUFFER_OVERFLOW);
1651
269
    }
1652
1653
    /* Insert Nal Unit Header */
1654
1.40k
    PUT_BITS(ps_bitstrm, NAL_FILLER_FIRST_BYTE, 8, return_status, "filler_header");
1655
1656
1.40k
    PUT_BITS(ps_bitstrm, 0xFFFFFF, 24, return_status, "fill bytes");
1657
1658
    /* Initializing Variables                           */
1659
1.40k
    i4_words_filled    = 1;
1660
1661
    /****************************************************/
1662
    /* Flooring the number of bytes for be stuffed to   */
1663
    /* WORD unit                                        */
1664
    /****************************************************/
1665
1.40k
    i4_num_words_to_fill = (insert_fill_bytes >> 2);
1666
1667
    /****************************************************/
1668
    /* Reducing already 4 bytes filled. In case stuffing*/
1669
    /* is <= 4 bytes, we are actually not stuffing      */
1670
    /* anything                                         */
1671
    /****************************************************/
1672
1.40k
    i4_num_words_to_fill -= i4_words_filled;
1673
1674
13.8M
    while (i4_num_words_to_fill > 0)
1675
13.8M
    {
1676
        /* Insert Nal Unit Header */
1677
13.8M
        PUT_BITS(ps_bitstrm, 0xFFFFFFFF, 32, return_status, "fill bytes");
1678
1679
13.8M
        i4_num_words_to_fill-- ;
1680
13.8M
    }
1681
1682
1.40k
    return_status = ih264e_put_rbsp_trailing_bits(ps_bitstrm);
1683
1684
1.40k
    return return_status;
1685
1.40k
}
1686