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