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