ih264e_encode_header.c revision 3749f6f435e79624f72841e866245d84195551cd
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_sps()
35*  - ih264e_generate_pps()
36*  - ih264e_generate_slice_header()
37*  - ih264e_get_level()
38*  - ih264e_populate_sps()
39*  - ih264e_populate_pps()
40*  - ih264e_populate_slice_header()
41*  - ih264e_add_filler_nal_unit()
42*
43* @remarks
44*  None
45*
46*******************************************************************************
47*/
48
49/*****************************************************************************/
50/* File Includes                                                             */
51/*****************************************************************************/
52
53/* System include files */
54#include <stdio.h>
55#include <stddef.h>
56#include <stdlib.h>
57#include <string.h>
58#include <assert.h>
59
60/* User Include Files */
61#include "ih264_typedefs.h"
62#include "iv2.h"
63#include "ive2.h"
64#include "ih264e.h"
65#include "ithread.h"
66#include "ih264e_config.h"
67#include "ih264e_trace.h"
68#include "ih264e_error.h"
69#include "ih264e_bitstream.h"
70#include "ih264_debug.h"
71#include "ih264_defs.h"
72#include "ime_distortion_metrics.h"
73#include "ime_defs.h"
74#include "ime_structs.h"
75#include "ih264_error.h"
76#include "ih264_structs.h"
77#include "ih264_trans_quant_itrans_iquant.h"
78#include "ih264_inter_pred_filters.h"
79#include "ih264_mem_fns.h"
80#include "ih264_padding.h"
81#include "ih264_intra_pred_filters.h"
82#include "ih264_deblk_edge_filters.h"
83#include "ih264_cabac_tables.h"
84#include "ih264e_defs.h"
85#include "irc_cntrl_param.h"
86#include "irc_frame_info_collector.h"
87#include "ih264e_rate_control.h"
88#include "ih264e_cabac_structs.h"
89#include "ih264e_structs.h"
90#include "ih264e_encode_header.h"
91#include "ih264_common_tables.h"
92#include "ih264_macros.h"
93
94
95/*****************************************************************************/
96/* Function Definitions                                                      */
97/*****************************************************************************/
98
99/**
100******************************************************************************
101*
102* @brief Generate nal unit header in the stream as per section 7.4.1
103*
104* @par   Description
105*  Inserts Nal unit header syntax as per section 7.4.1
106*
107* @param[inout]   ps_bitstrm
108*  pointer to bitstream context (handle)
109*
110* @param[in]   nal_unit_type
111*  nal type to be inserted
112*
113* @param[in]   nal_ref_idc
114*  nal ref idc to be inserted
115*
116* @return      success or failure error code
117*
118******************************************************************************
119*/
120static WORD32 ih264e_generate_nal_unit_header(bitstrm_t *ps_bitstrm,
121                                              WORD32 nal_unit_type,
122                                              WORD32 nal_ref_idc)
123{
124    WORD32 return_status = IH264E_SUCCESS;
125
126    /* sanity checks */
127    ASSERT((nal_unit_type > 0) && (nal_unit_type < 32));
128
129    /* forbidden_zero_bit + nal_ref_idc + nal_unit_type */
130    PUT_BITS(ps_bitstrm,
131             ((nal_ref_idc << 5) + nal_unit_type),
132             (1+2+5), /*1 forbidden zero bit + 2 nal_ref_idc + 5 nal_unit_type */
133             return_status,
134             "nal_unit_header");
135
136    return(return_status);
137}
138
139/**
140******************************************************************************
141*
142* @brief Generates SPS (Sequence Parameter Set)
143*
144* @par   Description
145*  This function generates Sequence Parameter Set header as per the spec
146*
147* @param[in]   ps_bitstrm
148*  pointer to bitstream context (handle)
149*
150* @param[in]   ps_sps
151*  pointer to structure containing SPS data
152*
153* @return      success or failure error code
154*
155******************************************************************************
156*/
157WORD32 ih264e_generate_sps(bitstrm_t *ps_bitstrm, sps_t *ps_sps)
158{
159    WORD32 return_status = IH264E_SUCCESS;
160    WORD32 i;
161    WORD8  i1_nal_unit_type = 7;
162    WORD8  i1_nal_ref_idc = 3;
163
164    /* Insert Start Code */
165    return_status |= ih264e_put_nal_start_code_prefix(ps_bitstrm, 1);
166
167    /* Insert Nal Unit Header */
168    return_status |= ih264e_generate_nal_unit_header(ps_bitstrm, i1_nal_unit_type, i1_nal_ref_idc);
169
170    /* profile_idc */
171    PUT_BITS(ps_bitstrm, ps_sps->u1_profile_idc, 8, return_status, "profile_idc");
172
173    /* constrained_set_flags */
174    PUT_BITS(ps_bitstrm, ps_sps->u1_constraint_set0_flag, 1, return_status, "constrained_set0_flag");
175    PUT_BITS(ps_bitstrm, ps_sps->u1_constraint_set1_flag, 1, return_status, "constrained_set1_flag");
176    PUT_BITS(ps_bitstrm, ps_sps->u1_constraint_set2_flag, 1, return_status, "constrained_set2_flag");
177    PUT_BITS(ps_bitstrm, ps_sps->u1_constraint_set3_flag, 1, return_status, "constrained_set3_flag");
178
179    /* reserved_zero_four_bits */
180    PUT_BITS(ps_bitstrm, 0, 4, return_status, "reserved_zero_four_bits");
181
182    /* level_idc */
183    PUT_BITS(ps_bitstrm, ps_sps->u1_level_idc, 8, return_status, "level_idc");
184
185    /* seq_parameter_set_id */
186    PUT_BITS_UEV(ps_bitstrm, ps_sps->u1_sps_id, return_status, "seq_parameter_set_id");
187
188    if (ps_sps->u1_profile_idc >= IH264_PROFILE_HIGH)
189    {
190        /* chroma_format_idc */
191        PUT_BITS_UEV(ps_bitstrm, ps_sps->u1_chroma_format_idc, return_status, "chroma_format_idc");
192
193        if (ps_sps->u1_chroma_format_idc == CHROMA_FMT_IDC_YUV444)
194        {
195            /* i1_residual_colour_transform_flag */
196            PUT_BITS(ps_bitstrm, ps_sps->i1_residual_colour_transform_flag, 1, return_status, "i1_residual_colour_transform_flag");
197        }
198
199        /* bit_depth_luma_minus8 */
200        PUT_BITS_UEV(ps_bitstrm, (ps_sps->i1_bit_depth_luma - 8), return_status, "bit_depth_luma_minus8");
201
202        /* bit_depth_chroma_minus8 */
203        PUT_BITS_UEV(ps_bitstrm, (ps_sps->i1_bit_depth_chroma - 8), return_status, "bit_depth_chroma_minus8");
204
205        /* qpprime_y_zero_transform_bypass_flag */
206        PUT_BITS(ps_bitstrm, ps_sps->i1_qpprime_y_zero_transform_bypass_flag, 1, return_status, "qpprime_y_zero_transform_bypass_flag");
207
208        /* seq_scaling_matrix_present_flag */
209        PUT_BITS(ps_bitstrm, ps_sps->i1_seq_scaling_matrix_present_flag, 1, return_status, "seq_scaling_matrix_present_flag");
210
211        /* seq_scaling_list */
212        if (ps_sps->i1_seq_scaling_matrix_present_flag)
213        {
214            /* TODO_LATER: Will be enabled once scaling list support is added */
215        }
216    }
217
218    /* log2_max_frame_num_minus4 */
219    PUT_BITS_UEV(ps_bitstrm, (ps_sps->i1_log2_max_frame_num - 4), return_status, "log2_max_frame_num_minus4");
220
221    /* pic_order_cnt_type */
222    PUT_BITS_UEV(ps_bitstrm, ps_sps->i1_pic_order_cnt_type, return_status, "pic_order_cnt_type");
223
224    if (ps_sps->i1_pic_order_cnt_type == 0)
225    {
226        /* log2_max_pic_order_cnt_lsb_minus4 */
227        PUT_BITS_UEV(ps_bitstrm, (ps_sps->i1_log2_max_pic_order_cnt_lsb - 4), return_status, "log2_max_pic_order_cnt_lsb_minus4");
228    }
229    else if (ps_sps->i1_pic_order_cnt_type == 1)
230    {
231        /* delta_pic_order_always_zero_flag */
232        PUT_BITS(ps_bitstrm, ps_sps->i1_delta_pic_order_always_zero_flag, 1, return_status, "delta_pic_order_always_zero_flag");
233
234        /* offset_for_non_ref_pic */
235        PUT_BITS_SEV(ps_bitstrm, ps_sps->i4_offset_for_non_ref_pic, return_status, "offset_for_non_ref_pic");
236
237        /* offset_for_top_to_bottom_field */
238        PUT_BITS_SEV(ps_bitstrm, ps_sps->i4_offset_for_top_to_bottom_field, return_status, "offset_for_top_to_bottom_field");
239
240        /* num_ref_frames_in_pic_order_cnt_cycle */
241        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");
242
243        /* Offset for ref frame */
244        for (i=0; i<ps_sps->u1_num_ref_frames_in_pic_order_cnt_cycle; i++)
245        {
246            /* offset_for_ref_frame */
247            PUT_BITS_SEV(ps_bitstrm, ps_sps->ai4_offset_for_ref_frame[i], return_status, "offset_for_ref_frame");
248        }
249    }
250
251    /* num_ref_frames */
252    PUT_BITS_UEV(ps_bitstrm, ps_sps->u1_max_num_ref_frames, return_status, "num_ref_frames");
253
254    /* gaps_in_frame_num_value_allowed_flag */
255    PUT_BITS(ps_bitstrm, ps_sps->i1_gaps_in_frame_num_value_allowed_flag, 1, return_status, "gaps_in_frame_num_value_allowed_flag");
256
257    /* pic_width_in_mbs_minus1 */
258    PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_width_in_mbs_minus1, return_status, "pic_width_in_mbs_minus1");
259
260    /* pic_height_in_map_units_minus1 */
261    PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_pic_height_in_map_units_minus1, return_status, "pic_height_in_map_units_minus1");
262
263    /* frame_mbs_only_flag */
264    PUT_BITS(ps_bitstrm, ps_sps->i1_frame_mbs_only_flag, 1, return_status, "frame_mbs_only_flag");
265
266    if (!ps_sps->i1_frame_mbs_only_flag)
267    {
268        /* mb_adaptive_frame_field_flag */
269        PUT_BITS(ps_bitstrm, ps_sps->i1_mb_adaptive_frame_field_flag, 1, return_status, "mb_adaptive_frame_field_flag");
270    }
271
272    /* direct_8x8_inference_flag */
273    PUT_BITS(ps_bitstrm, ps_sps->i1_direct_8x8_inference_flag, 1, return_status, "direct_8x8_inference_flag");
274
275    /* frame_cropping_flag */
276    PUT_BITS(ps_bitstrm, ps_sps->i1_frame_cropping_flag, 1, return_status, "frame_cropping_flag");
277
278    if (ps_sps->i1_frame_cropping_flag)
279    {
280        /* frame_crop_left_offset */
281        PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_frame_crop_left_offset, return_status, "frame_crop_left_offset");
282
283        /* frame_crop_right_offset */
284        PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_frame_crop_right_offset, return_status, "frame_crop_right_offset");
285
286        /* frame_crop_top_offset */
287        PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_frame_crop_top_offset, return_status, "frame_crop_top_offset");
288
289        /* frame_crop_bottom_offset */
290        PUT_BITS_UEV(ps_bitstrm, ps_sps->i2_frame_crop_bottom_offset, return_status, "frame_crop_bottom_offset");
291    }
292
293    /* vui_parameters_present_flag */
294    PUT_BITS(ps_bitstrm, ps_sps->i1_vui_parameters_present_flag, 1, return_status, "vui_parameters_present_flag");
295
296    if (ps_sps->i1_vui_parameters_present_flag)
297    {
298        /* Add vui parameters to the bitstream */;
299    }
300
301    /* rbsp trailing bits */
302    return_status |= ih264e_put_rbsp_trailing_bits(ps_bitstrm);
303
304    return return_status;
305}
306
307/**
308******************************************************************************
309*
310* @brief Generates PPS (Picture Parameter Set)
311*
312* @par   Description
313*  Generate Picture Parameter Set as per Section 7.3.2.2
314*
315* @param[in]   ps_bitstrm
316*  pointer to bitstream context (handle)
317*
318* @param[in]   ps_pps
319*  pointer to structure containing PPS data
320*
321* @return      success or failure error code
322*
323******************************************************************************
324*/
325WORD32 ih264e_generate_pps(bitstrm_t *ps_bitstrm, pps_t *ps_pps, sps_t *ps_sps)
326{
327    WORD32 return_status = IH264E_SUCCESS;
328
329    /* Insert the NAL start code */
330    return_status |= ih264e_put_nal_start_code_prefix(ps_bitstrm, 1);
331
332    /* Insert Nal Unit Header */
333    PUT_BITS(ps_bitstrm, NAL_PPS_FIRST_BYTE, 8, return_status, "pps_header");
334
335    /* pic_parameter_set_id */
336    PUT_BITS_UEV(ps_bitstrm, ps_pps->u1_pps_id, return_status, "pic_parameter_set_id");
337
338    /* seq_parameter_set_id */
339    PUT_BITS_UEV(ps_bitstrm, ps_pps->u1_sps_id, return_status, "seq_parameter_set_id");
340
341    /* Entropy coding : 0-VLC; 1 - CABAC */
342    PUT_BITS(ps_bitstrm, ps_pps->u1_entropy_coding_mode_flag, 1, return_status, "Entropy coding : 0-VLC; 1 - CABAC");
343
344    /* Pic order present flag */
345    PUT_BITS(ps_bitstrm, ps_pps->u1_pic_order_present_flag, 1, return_status, "Pic order present flag");
346
347    /* Number of slice groups */
348    PUT_BITS_UEV(ps_bitstrm, ps_pps->u1_num_slice_groups - 1, return_status, "Number of slice groups");
349
350    if (ps_pps->u1_num_slice_groups > 1)
351    {
352        /* TODO_LATER: Currently the number of slice groups minus 1 is 0.
353         * If this is not the case, we have to add Slice group map type to the bit stream*/
354    }
355
356    /* num_ref_idx_l0_default_active_minus1 */
357    PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_num_ref_idx_l0_default_active - 1, return_status, "num_ref_idx_l0_default_active_minus1");
358
359    /* num_ref_idx_l1_default_active_minus1 */
360    PUT_BITS_UEV(ps_bitstrm, ps_pps->i1_num_ref_idx_l1_default_active - 1, return_status, "num_ref_idx_l1_default_active_minus1");
361
362    /* weighted_pred_flag */
363    PUT_BITS(ps_bitstrm, ps_pps->i1_weighted_pred_flag, 1, return_status, "weighted_pred_flag");
364
365    /* weighted_bipred_flag */
366    PUT_BITS(ps_bitstrm, ps_pps->i1_weighted_bipred_idc, 2, return_status, "weighted_bipred_idc");
367
368    /* pic_init_qp_minus26 */
369    PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_pic_init_qp - 26, return_status, "pic_init_qp_minus26");
370
371    /* pic_init_qs_minus26 */
372    PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_pic_init_qs - 26, return_status, "pic_init_qs_minus26");
373
374    /* chroma_qp_index_offset */
375    PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_chroma_qp_index_offset, return_status, "chroma_qp_index_offset");
376
377    /* deblocking_filter_control_present_flag */
378    PUT_BITS(ps_bitstrm, ps_pps->i1_deblocking_filter_control_present_flag, 1, return_status, "deblocking_filter_control_present_flag");
379
380    /* constrained_intra_pred_flag */
381    PUT_BITS(ps_bitstrm, ps_pps->i1_constrained_intra_pred_flag, 1, return_status, "constrained_intra_pred_flag");
382
383    /*redundant_pic_cnt_present_flag */
384    PUT_BITS(ps_bitstrm, ps_pps->i1_redundant_pic_cnt_present_flag, 1, return_status, "redundant_pic_cnt_present_flag");
385
386    if (ps_sps->u1_profile_idc >= IH264_PROFILE_HIGH)
387    {
388        /* transform_8x8_mode_flag */
389        PUT_BITS(ps_bitstrm, ps_pps->i1_transform_8x8_mode_flag, 1, return_status, "transform_8x8_mode_flag");
390
391        /* pic_scaling_matrix_present_flag */
392        PUT_BITS(ps_bitstrm, ps_pps->i1_pic_scaling_matrix_present_flag, 1, return_status, "pic_scaling_matrix_present_flag");
393
394        if(ps_pps->i1_pic_scaling_matrix_present_flag)
395        {
396            /* TODO_LATER: Will be enabled once scaling list support is added */
397        }
398
399        /* Second chroma QP offset */
400        PUT_BITS_SEV(ps_bitstrm, ps_pps->i1_second_chroma_qp_index_offset, return_status, "Second chroma QP offset");
401    }
402
403    return_status |= ih264e_put_rbsp_trailing_bits(ps_bitstrm);
404
405    return return_status;
406}
407
408/**
409******************************************************************************
410*
411* @brief Generates Slice Header
412*
413* @par   Description
414*  Generate Slice Header as per Section 7.3.5.1
415*
416* @param[inout]   ps_bitstrm
417*  pointer to bitstream context for generating slice header
418*
419* @param[in]   ps_slice_hdr
420*  pointer to slice header params
421*
422* @param[in]   ps_pps
423*  pointer to pps params referred by slice
424*
425* @param[in]   ps_sps
426*  pointer to sps params referred by slice
427*
428* @param[out]   ps_dup_bit_strm_ent_offset
429*  Bitstream struct to store bitstream state
430*
431* @param[out]   pu4_first_slice_start_offset
432*  first slice offset is returned
433*
434* @return      success or failure error code
435*
436******************************************************************************
437*/
438WORD32 ih264e_generate_slice_header(bitstrm_t *ps_bitstrm,
439                                    slice_header_t *ps_slice_hdr,
440                                    pps_t *ps_pps,
441                                    sps_t *ps_sps)
442{
443
444    WORD32 return_status = IH264E_SUCCESS;
445
446    /* Insert start code */
447    return_status |= ih264e_put_nal_start_code_prefix(ps_bitstrm, 1);
448
449    /* Insert Nal Unit Header */
450    return_status |= ih264e_generate_nal_unit_header(ps_bitstrm, ps_slice_hdr->i1_nal_unit_type, ps_slice_hdr->i1_nal_unit_idc);
451
452    /* first_mb_in_slice */
453    PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u2_first_mb_in_slice, return_status, "first_mb_in_slice");
454
455    /* slice_type */
456    PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u1_slice_type, return_status, "slice_type");
457
458    /* pic_parameter_set_id */
459    PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u1_pps_id, return_status, "pic_parameter_set_id");
460
461    /* frame_num */
462    PUT_BITS(ps_bitstrm, ps_slice_hdr->i4_frame_num, ps_sps->i1_log2_max_frame_num, return_status, "frame_num");
463
464    if (!ps_sps->i1_frame_mbs_only_flag)
465    {
466        /* field_pic_flag */
467        PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_field_pic_flag, 1, return_status, "field_pic_flag");
468
469        if(ps_slice_hdr->i1_field_pic_flag)
470        {
471            /* bottom_field_flag */
472            PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_bottom_field_flag, 1, return_status, "bottom_field_flag");
473        }
474    }
475
476    if (ps_slice_hdr->i1_nal_unit_type == 5)
477    {
478        /* u2_idr_pic_id */
479        PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u2_idr_pic_id, return_status, "u2_idr_pic_id");
480    }
481
482    if (ps_sps->i1_pic_order_cnt_type == 0)
483    {
484        /* pic_order_cnt_lsb */
485        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");
486
487        if(ps_pps->u1_pic_order_present_flag && !ps_slice_hdr->i1_field_pic_flag)
488        {
489            /* delta_pic_order_cnt_bottom */
490            PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i4_delta_pic_order_cnt_bottom, return_status, "delta_pic_order_cnt_bottom");
491        }
492    }
493
494    if (ps_sps->i1_pic_order_cnt_type == 1 && !ps_sps->i1_delta_pic_order_always_zero_flag)
495    {
496        /* delta_pic_order_cnt[0] */
497        PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->ai4_delta_pic_order_cnt[0], return_status, "delta_pic_order_cnt[0]");
498
499        if (ps_pps->u1_pic_order_present_flag && !ps_slice_hdr->i1_field_pic_flag)
500        {
501            /* delta_pic_order_cnt[1] */
502            PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->ai4_delta_pic_order_cnt[1], return_status, "delta_pic_order_cnt[1]");
503        }
504    }
505
506    if (ps_pps->i1_redundant_pic_cnt_present_flag)
507    {
508        /* redundant_pic_cnt */
509        PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u1_redundant_pic_cnt, return_status, "redundant_pic_cnt");
510    }
511
512    if (ps_slice_hdr->u1_slice_type == BSLICE)
513    {
514        /* direct_spatial_mv_pred_flag */
515        PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_direct_spatial_mv_pred_flag, 1, return_status, "direct_spatial_mv_pred_flag");
516    }
517
518    if (ps_slice_hdr->u1_slice_type == PSLICE || ps_slice_hdr->u1_slice_type == SPSLICE || ps_slice_hdr->u1_slice_type == BSLICE)
519    {
520        /* num_ref_idx_active_override_flag */
521        PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_num_ref_idx_active_override_flag, 1, return_status, "num_ref_idx_active_override_flag");
522
523        if (ps_slice_hdr->u1_num_ref_idx_active_override_flag)
524        {
525            /* num_ref_idx_l0_active_minus1 */
526            PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_num_ref_idx_l0_active - 1, return_status, "num_ref_idx_l0_active_minus1");
527
528            if (ps_slice_hdr->u1_slice_type == BSLICE)
529            {
530                /* num_ref_idx_l1_active_minus1 */
531                PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_num_ref_idx_l1_active - 1, return_status, "num_ref_idx_l1_active_minus1");
532            }
533        }
534    }
535
536    /* ref_idx_reordering */
537    /* TODO: ref_idx_reordering */
538    if ((ps_slice_hdr->u1_slice_type != ISLICE) && (ps_slice_hdr->u1_slice_type != SISLICE))
539    {
540        /* ref_pic_list_reordering_flag_l0 */
541        PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_ref_idx_reordering_flag_l0, 1, return_status, "ref_pic_list_reordering_flag_l0");
542
543        if (ps_slice_hdr->u1_ref_idx_reordering_flag_l0)
544        {
545
546        }
547    }
548
549    if (ps_slice_hdr->u1_slice_type == BSLICE)
550    {
551        /* ref_pic_list_reordering_flag_l1 */
552        PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_ref_idx_reordering_flag_l1, 1, return_status, "ref_pic_list_reordering_flag_l1");
553
554        if (ps_slice_hdr->u1_ref_idx_reordering_flag_l1)
555        {
556
557        }
558    }
559
560    if ((ps_pps->i1_weighted_pred_flag &&
561                    (ps_slice_hdr->u1_slice_type == PSLICE || ps_slice_hdr->u1_slice_type == SPSLICE)) ||
562                    (ps_pps->i1_weighted_bipred_idc == 1 && ps_slice_hdr->u1_slice_type == BSLICE))
563    {
564        /* TODO_LATER: Currently there is no support for weighted prediction.
565         This needs to be updated when the support is added */
566    }
567
568    if (ps_slice_hdr->i1_nal_unit_idc != 0)
569    {
570        if (ps_slice_hdr->i1_nal_unit_type == 5)
571        {
572            /* no_output_of_prior_pics_flag  */
573            PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_no_output_of_prior_pics_flag , 1, return_status, "no_output_of_prior_pics_flag ");
574
575            /* long_term_reference_flag  */
576            PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_long_term_reference_flag , 1, return_status, "long_term_reference_flag ");
577        }
578        else
579        {
580            /* adaptive_ref_pic_marking_mode_flag  */
581            PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_adaptive_ref_pic_marking_mode_flag , 1, return_status, "adaptive_ref_pic_marking_mode_flag ");
582
583            if (ps_slice_hdr->u1_adaptive_ref_pic_marking_mode_flag)
584            {
585                /* TODO: if the reference picture marking mode is adaptive
586                 add these fields in the bit-stream */
587            }
588        }
589    }
590
591    if (ps_slice_hdr->u1_entropy_coding_mode_flag && ps_slice_hdr->u1_slice_type != ISLICE &&
592                    ps_slice_hdr->u1_slice_type != SISLICE)
593    {
594        /* cabac_init_idc */
595        PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_cabac_init_idc, return_status, "cabac_init_idc");
596    }
597
598    /* slice_qp_delta */
599    PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_slice_qp - ps_pps->i1_pic_init_qp, return_status, "slice_qp_delta");
600
601    if (ps_slice_hdr->u1_slice_type == SPSLICE || ps_slice_hdr->u1_slice_type == SISLICE)
602    {
603        if (ps_slice_hdr->u1_slice_type == SPSLICE)
604        {
605            /* sp_for_switch_flag */
606            PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_sp_for_switch_flag , 1, return_status, "sp_for_switch_flag");
607        }
608        /* slice_qs_delta */
609        PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->u1_slice_qs - ps_pps->i1_pic_init_qs, return_status, "slice_qs_delta");
610    }
611
612    if (ps_pps->i1_deblocking_filter_control_present_flag)
613    {
614        /* disable_deblocking_filter_idc */
615        PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u1_disable_deblocking_filter_idc, return_status, "disable_deblocking_filter_idc");
616
617        if(ps_slice_hdr->u1_disable_deblocking_filter_idc != 1)
618        {
619            /* slice_alpha_c0_offset_div2 */
620            PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_slice_alpha_c0_offset_div2, return_status, "slice_alpha_c0_offset_div2");
621
622            /* slice_beta_offset_div2 */
623            PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_slice_beta_offset_div2, return_status, "slice_beta_offset_div2");
624        }
625    }
626
627    if (ps_slice_hdr->u1_num_slice_groups_minus1 > 0 &&
628                    ps_pps->u1_slice_group_map_type >= 3 &&
629                    ps_pps->u1_slice_group_map_type <= 5)
630    {
631        /* slice_group_change_cycle */
632        /* TODO_LATER: Currently the number of slice groups minus 1 is 0.
633         * If this is not the case, we have to add Slice group map type to the bit stream */
634    }
635
636    return return_status;
637}
638
639
640
641/**
642******************************************************************************
643*
644* @brief Populates sps structure
645*
646* @par   Description
647*  Populates sps structure for its use in header generation
648*
649* @param[in]   ps_codec
650*  pointer to encoder context
651*
652* @param[out]  ps_sps
653*  pointer to sps params that needs to be populated
654*
655* @return      success or failure error code
656*
657******************************************************************************
658*/
659IH264E_ERROR_T ih264e_populate_sps(codec_t *ps_codec, sps_t *ps_sps)
660{
661    /* active config parameters */
662    cfg_params_t    *ps_cfg = &(ps_codec->s_cfg);
663
664//    /* level */
665//    IH264_LEVEL_T   level_idc;
666
667    /* error_status */
668    IH264E_ERROR_T i4_err_code = IH264E_FAIL;
669
670    /* profile */
671    /*
672     * Baseline profile supports, 8 bits per sample, 4:2:0 format, CAVLC.
673     * B frames are not allowed. Further, Flexible mb ordering, Redundant slices, Arbitrary slice ordering are supported.
674     * The constrained baseline profile is baseline profile minus ASO, FMO and redundant slices.
675     * To the constrained baseline profile if we add support for B slices, support for encoding interlaced frames,
676     * support for weighted prediction and introduce CABAC entropy coding then we have Main Profile.
677     */
678    if ((ps_cfg->u4_num_bframes) || (ps_cfg->e_content_type != IV_PROGRESSIVE) ||
679        (ps_cfg->u4_entropy_coding_mode == CABAC) || (ps_cfg->u4_weighted_prediction))
680    {
681        ps_sps->u1_profile_idc = IH264_PROFILE_MAIN;
682    }
683    else
684    {
685        ps_sps->u1_profile_idc = IH264_PROFILE_BASELINE;
686    }
687
688    /* level */
689    ps_sps->u1_level_idc = ps_cfg->u4_max_level;
690//    i4_err_code = ih264e_get_level(ps_cfg, &level_idc);
691//    if (i4_err_code == IH264E_SUCCESS)
692//    {
693//        ps_sps->u1_level_idc = level_idc;
694//
695//    }
696//    else
697//    {
698//        return i4_err_code;
699//    }
700
701    /* constrained flags */
702    /*
703     * baseline profile automatically implies set 0 flag
704     */
705    ps_sps->u1_constraint_set0_flag = (ps_sps->u1_profile_idc == IH264_PROFILE_BASELINE);
706    /*
707     * main profile automatically implies set 1 flag
708     * Although the encoder says it supports Baseline profile it actually supports constrained
709     * baseline profile as ASO, FMO and redundant slices are not supported
710     */
711    ps_sps->u1_constraint_set1_flag = (ps_sps->u1_profile_idc <= IH264_PROFILE_MAIN);
712    /*
713     * extended profile is not supported
714     */
715    ps_sps->u1_constraint_set2_flag = 0x00;
716    /*
717     * level 1b or level 11
718     */
719    if (ps_sps->u1_level_idc == IH264_LEVEL_1B)
720    {
721        ps_sps->u1_constraint_set3_flag = 0;
722        ps_sps->u1_level_idc = IH264_LEVEL_11;
723    }
724    else
725    {
726        ps_sps->u1_constraint_set3_flag = 0;
727    }
728
729    /* active sps id */
730    ps_sps->u1_sps_id = ps_codec->i4_sps_id;
731
732    if (ps_sps->u1_profile_idc >= IH264_PROFILE_HIGH)
733    {
734        /* chroma format idc */
735        ps_sps->u1_chroma_format_idc = CHROMA_FMT_IDC_YUV420;
736
737        /* residual_colour_transform_flag */
738        ps_sps->i1_residual_colour_transform_flag = 0;
739
740        /* luma bit depth 8 */
741        ps_sps->i1_bit_depth_luma = 8;
742
743        /* chroma bit depth 8 */
744        ps_sps->i1_bit_depth_chroma = 8;
745
746        /* qpprime_y_zero_transform_bypass_flag */
747        ps_sps->i1_qpprime_y_zero_transform_bypass_flag = 0;
748
749        /* seq_scaling_matrix_present_flag */
750        ps_sps->i1_seq_scaling_matrix_present_flag = 0;
751
752        if (ps_sps->i1_seq_scaling_matrix_present_flag)
753        {
754            /* TODO_LATER: Will be enabled once scaling list support is added */
755        }
756    }
757
758    /* log2_max_frame_num_minus4 */
759    ps_sps->i1_log2_max_frame_num = 16;
760
761    /* pic_order_cnt_type */
762    ps_sps->i1_pic_order_cnt_type = 2;
763
764    if (ps_codec->i4_non_ref_frames_in_stream)
765    {
766        ps_sps->i1_pic_order_cnt_type = 0;
767    }
768
769    /* log2_max_pic_order_cnt_lsb_minus4 */
770    ps_sps->i1_log2_max_pic_order_cnt_lsb = 8;
771
772    /* TODO : add support for other poc types */
773    if (ps_sps->i1_pic_order_cnt_type == 0)
774    {
775
776    }
777    else if (ps_sps->i1_pic_order_cnt_type == 1)
778    {
779
780    }
781
782    /* num_ref_frames */
783    /* TODO : Should we have a flexible num ref frames */
784    if (ps_codec->s_cfg.u4_num_bframes > 0)
785    {
786        ps_sps->u1_max_num_ref_frames = 2;
787    }
788    else
789    {
790        ps_sps->u1_max_num_ref_frames = 1;
791    }
792
793    /* gaps_in_frame_num_value_allowed_flag */
794    ps_sps->i1_gaps_in_frame_num_value_allowed_flag = 0;
795
796    /* pic width in mb - 1 */
797    ps_sps->i2_pic_width_in_mbs_minus1 = ps_cfg->i4_wd_mbs - 1;
798
799    /* pic height in mb - 1 */
800    ps_sps->i2_pic_height_in_map_units_minus1 = ps_cfg->i4_ht_mbs - 1;;
801
802    /* frame_mbs_only_flag, no support for interlace encoding */
803    ps_sps->i1_frame_mbs_only_flag = 1;
804
805    /* mb_adaptive_frame_field_flag */
806    if (ps_sps->i1_frame_mbs_only_flag == 0)
807    {
808        ps_sps->i1_mb_adaptive_frame_field_flag = 0;
809    }
810
811    /* direct_8x8_inference_flag */
812    ps_sps->i1_direct_8x8_inference_flag = 0;
813
814    /* cropping params */
815    /*NOTE : Cropping values depend on the chroma format
816     * For our case ,decoder interprets the cropping values as 2*num pixels
817     * Hence the difference in the disp width and width must be halved before sending
818     * to get the expected results
819     */
820    ps_sps->i1_frame_cropping_flag      = 0;
821    ps_sps->i2_frame_crop_left_offset   = 0;
822    ps_sps->i2_frame_crop_right_offset  = (ps_codec->s_cfg.u4_wd - ps_codec->s_cfg.u4_disp_wd)>>1;
823    ps_sps->i2_frame_crop_top_offset    = 0;
824    ps_sps->i2_frame_crop_bottom_offset = (ps_codec->s_cfg.u4_ht - ps_codec->s_cfg.u4_disp_ht)>>1;
825
826    if (ps_sps->i2_frame_crop_left_offset    ||
827                    ps_sps->i2_frame_crop_right_offset   ||
828                    ps_sps->i2_frame_crop_top_offset     ||
829                    ps_sps->i2_frame_crop_bottom_offset)
830    {
831        ps_sps->i1_frame_cropping_flag      = 1;
832    }
833
834    /* vui params */
835    ps_sps->i1_vui_parameters_present_flag = 0;
836
837    if (ps_sps->i1_vui_parameters_present_flag)
838    {
839        /* populate vui params */
840    }
841
842    return i4_err_code;
843}
844
845/**
846******************************************************************************
847*
848* @brief Populates pps structure
849*
850* @par   Description
851*  Populates pps structure for its use in header generation
852*
853* @param[in]   ps_codec
854*  pointer to encoder context
855*
856* @param[out]  ps_pps
857*  pointer to pps params that needs to be populated
858*
859* @return      success or failure error code
860*
861******************************************************************************
862*/
863IH264E_ERROR_T ih264e_populate_pps(codec_t *ps_codec, pps_t *ps_pps)
864{
865    /* active config parameters */
866    cfg_params_t    *ps_cfg = &(ps_codec->s_cfg);
867
868    /* seq_parameter_set_id */
869    ps_pps->u1_sps_id = ps_codec->i4_sps_id;
870
871    /* pic_parameter_set_id */
872    ps_pps->u1_pps_id = ps_codec->i4_pps_id;
873
874    /* entropy_coding_mode */
875    ps_pps->u1_entropy_coding_mode_flag = ps_cfg->u4_entropy_coding_mode;
876
877    /* pic_order_present_flag is unset if we don't have feilds */
878    ps_pps->u1_pic_order_present_flag = 0;
879
880    /* Currently number of slice groups supported are 1 */
881    ps_pps->u1_num_slice_groups = 1;
882
883    if (ps_pps->u1_num_slice_groups - 1)
884    {
885        /* TODO_LATER: Currently the number of slice groups minus 1 is 0.
886         * If this is not the case, we have to add Slice group map type to the bit stream*/
887    }
888
889    /* number of reference frames for list 0 */
890    /* FIXME : fix this hard coded value */
891    ps_pps->i1_num_ref_idx_l0_default_active = 1;
892
893    /* number of reference frames for list 1 */
894    ps_pps->i1_num_ref_idx_l1_default_active = 1;
895
896    /* weighted prediction for now is disabled */
897    ps_pps->i1_weighted_pred_flag = 0;
898    ps_pps->i1_weighted_bipred_idc = 0;
899
900    /* The intent is to not signal qp from pps. Rather send the same in slice headers */
901    ps_pps->i1_pic_init_qp = 0;
902
903    /* The intent is to not signal qp from pps. Rather send the same in slice headers */
904    ps_pps->i1_pic_init_qs = 0;
905
906    /* The intent is to not signal qp from pps. Rather send the same in slice headers */
907    ps_pps->i1_chroma_qp_index_offset = 0;
908
909    /* deblocking filter flags present in slice header */
910    ps_pps->i1_deblocking_filter_control_present_flag = 1;
911
912    /* constrained intra prediction */
913    ps_pps->i1_constrained_intra_pred_flag = ps_cfg->u4_constrained_intra_pred;
914
915    /* sending redundant slices is not supported for now */
916    ps_pps->i1_redundant_pic_cnt_present_flag = 0;
917
918    ps_pps->u1_slice_group_map_type = 0;
919    return IH264E_SUCCESS;
920}
921
922/**
923******************************************************************************
924*
925* @brief Populates slice header structure
926*
927* @par   Description
928*  Populates slice header structure for its use in header generation
929*
930* @param[in]  ps_proc
931*  pointer to proc context
932*
933* @param[out]  ps_slice_hdr
934*  pointer to slice header structure that needs to be populated
935*
936* @param[in]  ps_pps
937*  pointer to pps params structure referred by the slice
938*
939* @param[in]   ps_sps
940*  pointer to sps params referred by the pps
941*
942* @return      success or failure error code
943*
944******************************************************************************
945*/
946WORD32 ih264e_populate_slice_header(process_ctxt_t *ps_proc,
947                                    slice_header_t *ps_slice_hdr,
948                                    pps_t *ps_pps,
949                                    sps_t *ps_sps)
950{
951    /* entropy context */
952    entropy_ctxt_t *ps_entropy = &ps_proc->s_entropy;
953
954    codec_t *ps_codec = ps_proc->ps_codec;
955
956    if (ps_proc->ps_codec->u4_is_curr_frm_ref)
957    {
958        ps_slice_hdr->i1_nal_unit_idc = 3;
959    }
960    else
961    {
962        ps_slice_hdr->i1_nal_unit_idc = 0;
963    }
964
965    /* start mb address */
966    ps_slice_hdr->u2_first_mb_in_slice = ps_entropy->i4_mb_start_add;
967
968    /* slice type */
969    ps_slice_hdr->u1_slice_type = ps_proc->i4_slice_type;
970
971    /* pic_parameter_set_id */
972    ps_slice_hdr->u1_pps_id = ps_pps->u1_pps_id;
973
974    /* Separate color plane flag is 0,
975     * hence the syntax element color_plane_id not included */
976
977    /* frame num */
978    ps_slice_hdr->i4_frame_num = ps_proc->i4_frame_num;
979
980    /* frame_mbs_only_flag, no support for interlace encoding */
981    if (!ps_sps->i1_frame_mbs_only_flag)
982    {
983        ps_slice_hdr->i1_field_pic_flag = 0;
984
985        if (ps_slice_hdr->i1_field_pic_flag)
986        {
987            ps_slice_hdr->i1_bottom_field_flag = 0;
988        }
989    }
990
991    /* idr pic id */
992    if (ps_proc->u4_is_idr)
993    {
994        ps_slice_hdr->u2_idr_pic_id = ps_proc->u4_idr_pic_id;
995        ps_slice_hdr->i1_nal_unit_type = 5;
996    }
997    else
998    {
999        ps_slice_hdr->i1_nal_unit_type = 1;
1000    }
1001
1002    if (ps_sps->i1_pic_order_cnt_type == 0)
1003    {
1004
1005        WORD32 i4_poc;
1006        i4_poc = ps_codec->i4_poc;
1007        i4_poc %= (1 << ps_sps->i1_log2_max_pic_order_cnt_lsb);
1008        ps_slice_hdr->i4_pic_order_cnt_lsb = i4_poc;
1009    }
1010    /* TODO add support for poc type 1 */
1011    else if (ps_sps->i1_pic_order_cnt_type == 1)
1012    {
1013
1014    }
1015
1016
1017    /*
1018     * redundant slices are not currently supported.
1019     * Hence the syntax element redundant slice cnt is not initialized
1020     */
1021    if (ps_pps->i1_redundant_pic_cnt_present_flag)
1022    {
1023
1024    }
1025
1026    /* direct spatial mv pred flag */
1027    if (ps_proc->i4_slice_type == BSLICE)
1028    {
1029        ps_slice_hdr->u1_direct_spatial_mv_pred_flag = 1;
1030    }
1031
1032    if (ps_proc->i4_slice_type == PSLICE || ps_proc->i4_slice_type == SPSLICE || ps_proc->i4_slice_type == BSLICE)
1033    {
1034        /* num_ref_idx_active_override_flag */
1035        ps_slice_hdr->u1_num_ref_idx_active_override_flag = 0;
1036
1037        if (ps_slice_hdr->u1_num_ref_idx_active_override_flag)
1038        {
1039            /* num_ref_idx_l0_active_minus1 */
1040
1041            if (ps_proc->i4_slice_type == BSLICE)
1042            {
1043                /* num_ref_idx_l1_active_minus1 */
1044
1045            }
1046        }
1047    }
1048
1049    /* ref_idx_reordering */
1050    /* TODO: ref_idx_reordering */
1051    if ((ps_proc->i4_slice_type != ISLICE) && (ps_proc->i4_slice_type != SISLICE))
1052    {
1053        /* ref_pic_list_reordering_flag_l0 */
1054        ps_slice_hdr->u1_ref_idx_reordering_flag_l0 = 0;
1055
1056        if (ps_slice_hdr->u1_ref_idx_reordering_flag_l0)
1057        {
1058
1059        }
1060
1061        /* ref_pic_list_reordering_flag_l1 */
1062        ps_slice_hdr->u1_ref_idx_reordering_flag_l1 = 0;
1063
1064        if (ps_slice_hdr->u1_ref_idx_reordering_flag_l1)
1065        {
1066
1067        }
1068    }
1069
1070
1071    /* Currently we do not support weighted pred */
1072    /* ps_slice_hdr->u1_weighted_bipred_idc = 0; */
1073
1074    if ((ps_pps->i1_weighted_pred_flag &&
1075                    (ps_proc->i4_slice_type == PSLICE || ps_proc->i4_slice_type == SPSLICE)) ||
1076                    (ps_pps->i1_weighted_bipred_idc == 1 && ps_proc->i4_slice_type == BSLICE))
1077    {
1078        /* TODO_LATER: Currently there is no support for weighted prediction.
1079             This needs to be updated when the support is added */
1080    }
1081
1082    if (ps_slice_hdr->i1_nal_unit_idc != 0)
1083    {
1084        if (ps_slice_hdr->i1_nal_unit_type == 5)
1085        {
1086            /* no_output_of_prior_pics_flag  */
1087            ps_slice_hdr->u1_no_output_of_prior_pics_flag = 0;
1088
1089            /* long_term_reference_flag  */
1090            ps_slice_hdr->u1_long_term_reference_flag = 0;
1091        }
1092        else
1093        {
1094            /* adaptive_ref_pic_marking_mode_flag  */
1095            ps_slice_hdr->u1_adaptive_ref_pic_marking_mode_flag = 0;
1096
1097            if (ps_slice_hdr->u1_adaptive_ref_pic_marking_mode_flag)
1098            {
1099                /* TODO: if the reference picture marking mode is adaptive
1100                     add these fields in the bit-stream */
1101            }
1102        }
1103    }
1104
1105    /* entropy coding mode flag */
1106    ps_slice_hdr->u1_entropy_coding_mode_flag = ps_entropy->u1_entropy_coding_mode_flag;
1107
1108    if (ps_slice_hdr->u1_entropy_coding_mode_flag && ps_proc->i4_slice_type != ISLICE &&
1109                    ps_proc->i4_slice_type != SISLICE)
1110    {
1111        /* cabac_init_idc */
1112    }
1113
1114    /* slice qp */
1115    ps_slice_hdr->i1_slice_qp = ps_proc->u4_frame_qp;
1116
1117    if (ps_proc->i4_slice_type == SPSLICE || ps_proc->i4_slice_type == SISLICE)
1118    {
1119        if (ps_proc->i4_slice_type == SPSLICE)
1120        {
1121            /* sp_for_switch_flag */
1122        }
1123        /* slice_qs_delta */
1124    }
1125
1126    if (ps_pps->i1_deblocking_filter_control_present_flag)
1127    {
1128        /* disable_deblocking_filter_idc */
1129        ps_slice_hdr->u1_disable_deblocking_filter_idc = ps_proc->u4_disable_deblock_level;
1130
1131        if (ps_slice_hdr->u1_disable_deblocking_filter_idc != 1)
1132        {
1133            /* slice_alpha_c0_offset_div2 */
1134            ps_slice_hdr->i1_slice_alpha_c0_offset_div2 = 0;
1135
1136            /* slice_beta_offset_div2 */
1137            ps_slice_hdr->i1_slice_beta_offset_div2 = 0;
1138        }
1139    }
1140    ps_slice_hdr->u1_num_slice_groups_minus1 = 0;
1141    if(ps_slice_hdr->u1_num_slice_groups_minus1 > 0 &&
1142        ps_pps->u1_slice_group_map_type >= 3 &&
1143        ps_pps->u1_slice_group_map_type <= 5)
1144    {
1145        /* slice_group_change_cycle */
1146        /* TODO_LATER: Currently the number of slice groups minus 1 is 0.
1147         * If this is not the case, we have to add Slice group map type to the bit stream */
1148    }
1149
1150    ps_slice_hdr->i1_cabac_init_idc = CABAC_INIT_IDC;
1151
1152    return IH264E_SUCCESS;
1153}
1154
1155/**
1156******************************************************************************
1157*
1158* @brief inserts FILLER Nal Unit.
1159*
1160* @par   Description
1161*  In constant bit rate rc mode, when the bits generated by the codec is
1162*  underflowing the target bit rate, the encoder library inserts filler nal unit.
1163*
1164* @param[in]    ps_bitstrm
1165*  pointer to bitstream context (handle)
1166*
1167* @param[in]    insert_fill_bytes
1168*  Number of fill bytes to be inserted
1169*
1170* @return      success or failure error code
1171*
1172******************************************************************************
1173*/
1174IH264E_ERROR_T ih264e_add_filler_nal_unit(bitstrm_t *ps_bitstrm,
1175                                          WORD32 insert_fill_bytes)
1176{
1177    WORD32  i4_num_words_to_fill, i4_words_filled;
1178
1179    IH264E_ERROR_T return_status = IH264E_SUCCESS;
1180
1181    /* Insert the NAL start code */
1182    return_status |= ih264e_put_nal_start_code_prefix(ps_bitstrm, 1);
1183
1184    if (ps_bitstrm->u4_strm_buf_offset + insert_fill_bytes >= ps_bitstrm->u4_max_strm_size)
1185    {
1186        return (IH264E_BITSTREAM_BUFFER_OVERFLOW);
1187    }
1188
1189    /* Insert Nal Unit Header */
1190    PUT_BITS(ps_bitstrm, NAL_FILLER_FIRST_BYTE, 8, return_status, "filler_header");
1191
1192    PUT_BITS(ps_bitstrm, 0xFFFFFF, 24, return_status, "fill bytes");
1193
1194    /* Initializing Variables                           */
1195    i4_words_filled    = 1;
1196
1197    /****************************************************/
1198    /* Flooring the number of bytes for be stuffed to   */
1199    /* WORD unit                                        */
1200    /****************************************************/
1201    i4_num_words_to_fill = (insert_fill_bytes >> 2);
1202
1203    /****************************************************/
1204    /* Reducing already 4 bytes filled. In case stuffing*/
1205    /* is <= 4 bytes, we are actually not stuffing      */
1206    /* anything                                         */
1207    /****************************************************/
1208    i4_num_words_to_fill -= i4_words_filled;
1209
1210    while (i4_num_words_to_fill > 0)
1211    {
1212        /* Insert Nal Unit Header */
1213        PUT_BITS(ps_bitstrm, 0xFFFFFFFF, 32, return_status, "fill bytes");
1214
1215        i4_num_words_to_fill-- ;
1216    }
1217
1218    return_status |= ih264e_put_rbsp_trailing_bits(ps_bitstrm);
1219
1220    return return_status;
1221}
1222
1223