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