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 /* rbsp trailing bits */
790 return_status = ih264e_put_rbsp_trailing_bits(ps_bitstrm);
791
792 return return_status;
793 }
794
795 /**
796 ******************************************************************************
797 *
798 * @brief Generates Slice Header
799 *
800 * @par Description
801 * Generate Slice Header as per Section 7.3.5.1
802 *
803 * @param[inout] ps_bitstrm
804 * pointer to bitstream context for generating slice header
805 *
806 * @param[in] ps_slice_hdr
807 * pointer to slice header params
808 *
809 * @param[in] ps_pps
810 * pointer to pps params referred by slice
811 *
812 * @param[in] ps_sps
813 * pointer to sps params referred by slice
814 *
815 * @param[out] ps_dup_bit_strm_ent_offset
816 * Bitstream struct to store bitstream state
817 *
818 * @param[out] pu4_first_slice_start_offset
819 * first slice offset is returned
820 *
821 * @return success or failure error code
822 *
823 ******************************************************************************
824 */
ih264e_generate_slice_header(bitstrm_t * ps_bitstrm,slice_header_t * ps_slice_hdr,pps_t * ps_pps,sps_t * ps_sps)825 WORD32 ih264e_generate_slice_header(bitstrm_t *ps_bitstrm,
826 slice_header_t *ps_slice_hdr,
827 pps_t *ps_pps,
828 sps_t *ps_sps)
829 {
830
831 WORD32 return_status = IH264E_SUCCESS;
832
833 /* Insert start code */
834 return_status = ih264e_put_nal_start_code_prefix(ps_bitstrm, 1);
835 if(return_status != IH264E_SUCCESS)
836 {
837 return return_status;
838 }
839 /* Insert Nal Unit Header */
840 return_status = ih264e_generate_nal_unit_header(ps_bitstrm, ps_slice_hdr->i1_nal_unit_type, ps_slice_hdr->i1_nal_unit_idc);
841 if(return_status != IH264E_SUCCESS)
842 {
843 return return_status;
844 }
845 /* first_mb_in_slice */
846 PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u2_first_mb_in_slice, return_status, "first_mb_in_slice");
847
848 /* slice_type */
849 PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u1_slice_type, return_status, "slice_type");
850
851 /* pic_parameter_set_id */
852 PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u1_pps_id, return_status, "pic_parameter_set_id");
853
854 /* frame_num */
855 PUT_BITS(ps_bitstrm, ps_slice_hdr->i4_frame_num, ps_sps->i1_log2_max_frame_num, return_status, "frame_num");
856
857 if (!ps_sps->i1_frame_mbs_only_flag)
858 {
859 /* field_pic_flag */
860 PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_field_pic_flag, 1, return_status, "field_pic_flag");
861
862 if(ps_slice_hdr->i1_field_pic_flag)
863 {
864 /* bottom_field_flag */
865 PUT_BITS(ps_bitstrm, ps_slice_hdr->i1_bottom_field_flag, 1, return_status, "bottom_field_flag");
866 }
867 }
868
869 if (ps_slice_hdr->i1_nal_unit_type == 5)
870 {
871 /* u2_idr_pic_id */
872 PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u2_idr_pic_id, return_status, "u2_idr_pic_id");
873 }
874
875 if (ps_sps->i1_pic_order_cnt_type == 0)
876 {
877 /* pic_order_cnt_lsb */
878 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");
879
880 if(ps_pps->u1_pic_order_present_flag && !ps_slice_hdr->i1_field_pic_flag)
881 {
882 /* delta_pic_order_cnt_bottom */
883 PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i4_delta_pic_order_cnt_bottom, return_status, "delta_pic_order_cnt_bottom");
884 }
885 }
886
887 if (ps_sps->i1_pic_order_cnt_type == 1 && !ps_sps->i1_delta_pic_order_always_zero_flag)
888 {
889 /* delta_pic_order_cnt[0] */
890 PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->ai4_delta_pic_order_cnt[0], return_status, "delta_pic_order_cnt[0]");
891
892 if (ps_pps->u1_pic_order_present_flag && !ps_slice_hdr->i1_field_pic_flag)
893 {
894 /* delta_pic_order_cnt[1] */
895 PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->ai4_delta_pic_order_cnt[1], return_status, "delta_pic_order_cnt[1]");
896 }
897 }
898
899 if (ps_pps->i1_redundant_pic_cnt_present_flag)
900 {
901 /* redundant_pic_cnt */
902 PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u1_redundant_pic_cnt, return_status, "redundant_pic_cnt");
903 }
904
905 if (ps_slice_hdr->u1_slice_type == BSLICE)
906 {
907 /* direct_spatial_mv_pred_flag */
908 PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_direct_spatial_mv_pred_flag, 1, return_status, "direct_spatial_mv_pred_flag");
909 }
910
911 if (ps_slice_hdr->u1_slice_type == PSLICE || ps_slice_hdr->u1_slice_type == SPSLICE || ps_slice_hdr->u1_slice_type == BSLICE)
912 {
913 /* num_ref_idx_active_override_flag */
914 PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_num_ref_idx_active_override_flag, 1, return_status, "num_ref_idx_active_override_flag");
915
916 if (ps_slice_hdr->u1_num_ref_idx_active_override_flag)
917 {
918 /* num_ref_idx_l0_active_minus1 */
919 PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_num_ref_idx_l0_active - 1, return_status, "num_ref_idx_l0_active_minus1");
920
921 if (ps_slice_hdr->u1_slice_type == BSLICE)
922 {
923 /* num_ref_idx_l1_active_minus1 */
924 PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_num_ref_idx_l1_active - 1, return_status, "num_ref_idx_l1_active_minus1");
925 }
926 }
927 }
928
929 /* ref_idx_reordering */
930 /* TODO: ref_idx_reordering */
931 if ((ps_slice_hdr->u1_slice_type != ISLICE) && (ps_slice_hdr->u1_slice_type != SISLICE))
932 {
933 /* ref_pic_list_reordering_flag_l0 */
934 PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_ref_idx_reordering_flag_l0, 1, return_status, "ref_pic_list_reordering_flag_l0");
935
936 if (ps_slice_hdr->u1_ref_idx_reordering_flag_l0)
937 {
938
939 }
940 }
941
942 if (ps_slice_hdr->u1_slice_type == BSLICE)
943 {
944 /* ref_pic_list_reordering_flag_l1 */
945 PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_ref_idx_reordering_flag_l1, 1, return_status, "ref_pic_list_reordering_flag_l1");
946
947 if (ps_slice_hdr->u1_ref_idx_reordering_flag_l1)
948 {
949
950 }
951 }
952
953 if ((ps_pps->i1_weighted_pred_flag &&
954 (ps_slice_hdr->u1_slice_type == PSLICE || ps_slice_hdr->u1_slice_type == SPSLICE)) ||
955 (ps_slice_hdr->u1_slice_type == BSLICE && ps_pps->i1_weighted_bipred_idc == 1))
956 {
957 /* TODO_LATER: Currently there is no support for weighted prediction.
958 This needs to be updated when the support is added */
959 }
960
961 if (ps_slice_hdr->i1_nal_unit_idc != 0)
962 {
963 if (ps_slice_hdr->i1_nal_unit_type == 5)
964 {
965 /* no_output_of_prior_pics_flag */
966 PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_no_output_of_prior_pics_flag , 1, return_status, "no_output_of_prior_pics_flag ");
967
968 /* long_term_reference_flag */
969 PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_long_term_reference_flag , 1, return_status, "long_term_reference_flag ");
970 }
971 else
972 {
973 /* adaptive_ref_pic_marking_mode_flag */
974 PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_adaptive_ref_pic_marking_mode_flag , 1, return_status, "adaptive_ref_pic_marking_mode_flag ");
975
976 if (ps_slice_hdr->u1_adaptive_ref_pic_marking_mode_flag)
977 {
978 /* TODO: if the reference picture marking mode is adaptive
979 add these fields in the bit-stream */
980 }
981 }
982 }
983
984 if (ps_slice_hdr->u1_entropy_coding_mode_flag && ps_slice_hdr->u1_slice_type != ISLICE &&
985 ps_slice_hdr->u1_slice_type != SISLICE)
986 {
987 /* cabac_init_idc */
988 PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->i1_cabac_init_idc, return_status, "cabac_init_idc");
989 }
990
991 /* slice_qp_delta */
992 PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_slice_qp - ps_pps->i1_pic_init_qp, return_status, "slice_qp_delta");
993
994 if (ps_slice_hdr->u1_slice_type == SPSLICE || ps_slice_hdr->u1_slice_type == SISLICE)
995 {
996 if (ps_slice_hdr->u1_slice_type == SPSLICE)
997 {
998 /* sp_for_switch_flag */
999 PUT_BITS(ps_bitstrm, ps_slice_hdr->u1_sp_for_switch_flag , 1, return_status, "sp_for_switch_flag");
1000 }
1001 /* slice_qs_delta */
1002 PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->u1_slice_qs - ps_pps->i1_pic_init_qs, return_status, "slice_qs_delta");
1003 }
1004
1005 if (ps_pps->i1_deblocking_filter_control_present_flag)
1006 {
1007 /* disable_deblocking_filter_idc */
1008 PUT_BITS_UEV(ps_bitstrm, ps_slice_hdr->u1_disable_deblocking_filter_idc, return_status, "disable_deblocking_filter_idc");
1009
1010 if(ps_slice_hdr->u1_disable_deblocking_filter_idc != 1)
1011 {
1012 /* slice_alpha_c0_offset_div2 */
1013 PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_slice_alpha_c0_offset_div2, return_status, "slice_alpha_c0_offset_div2");
1014
1015 /* slice_beta_offset_div2 */
1016 PUT_BITS_SEV(ps_bitstrm, ps_slice_hdr->i1_slice_beta_offset_div2, return_status, "slice_beta_offset_div2");
1017 }
1018 }
1019
1020 if (ps_slice_hdr->u1_num_slice_groups_minus1 > 0 &&
1021 ps_pps->u1_slice_group_map_type >= 3 &&
1022 ps_pps->u1_slice_group_map_type <= 5)
1023 {
1024 /* slice_group_change_cycle */
1025 /* TODO_LATER: Currently the number of slice groups minus 1 is 0.
1026 * If this is not the case, we have to add Slice group map type to the bit stream */
1027 }
1028
1029 return return_status;
1030 }
1031
1032 /**
1033 ******************************************************************************
1034 *
1035 * @brief Populates VUI structure
1036 *
1037 * @par Description
1038 * Populates VUI structure for its use in header generation
1039 *
1040 * @param[in] ps_codec
1041 * pointer to encoder context
1042 *
1043 * @return success or failure error code
1044 *
1045 ******************************************************************************
1046 */
ih264e_populate_vui(codec_t * ps_codec)1047 IH264E_ERROR_T ih264e_populate_vui(codec_t *ps_codec)
1048 {
1049
1050 vui_t *ps_vui = &ps_codec->s_cfg.s_vui;
1051 sps_t *ps_sps = ps_codec->ps_sps_base + ps_codec->i4_sps_id;
1052
1053
1054 ps_vui->u1_nal_hrd_parameters_present_flag = 0;
1055 ps_vui->u1_vcl_hrd_parameters_present_flag = 0;
1056
1057 ps_vui->u1_bitstream_restriction_flag = 1;
1058 ps_vui->u1_motion_vectors_over_pic_boundaries_flag = 1;
1059 ps_vui->u1_max_bytes_per_pic_denom = 0;
1060 ps_vui->u1_max_bits_per_mb_denom = 0;
1061 ps_vui->u1_log2_max_mv_length_horizontal = 16;
1062 ps_vui->u1_log2_max_mv_length_vertical = 16;
1063
1064 if(ps_codec->s_cfg.u4_num_bframes == 0)
1065 {
1066 ps_vui->u1_num_reorder_frames = 0;
1067 }
1068 else
1069 {
1070 ps_vui->u1_num_reorder_frames = 1;
1071 }
1072
1073 ps_vui->u1_max_dec_frame_buffering = ps_sps->u1_max_num_ref_frames;
1074
1075
1076 return 0;
1077 }
1078
1079
1080
1081 /**
1082 ******************************************************************************
1083 *
1084 * @brief Populates sps structure
1085 *
1086 * @par Description
1087 * Populates sps structure for its use in header generation
1088 *
1089 * @param[in] ps_codec
1090 * pointer to encoder context
1091 *
1092 * @param[out] ps_sps
1093 * pointer to sps params that needs to be populated
1094 *
1095 * @return success or failure error code
1096 *
1097 ******************************************************************************
1098 */
ih264e_populate_sps(codec_t * ps_codec,sps_t * ps_sps)1099 IH264E_ERROR_T ih264e_populate_sps(codec_t *ps_codec, sps_t *ps_sps)
1100 {
1101 /* active config parameters */
1102 cfg_params_t *ps_cfg = &(ps_codec->s_cfg);
1103
1104 // /* level */
1105 // IH264_LEVEL_T level_idc;
1106
1107 /* error_status */
1108 IH264E_ERROR_T i4_err_code = IH264E_FAIL;
1109
1110 /* profile */
1111 /*
1112 * Baseline profile supports, 8 bits per sample, 4:2:0 format, CAVLC.
1113 * B frames are not allowed. Further, Flexible mb ordering, Redundant slices, Arbitrary slice ordering are supported.
1114 * The constrained baseline profile is baseline profile minus ASO, FMO and redundant slices.
1115 * To the constrained baseline profile if we add support for B slices, support for encoding interlaced frames,
1116 * support for weighted prediction and introduce CABAC entropy coding then we have Main Profile.
1117 */
1118 if ((ps_cfg->u4_num_bframes) || (ps_cfg->e_content_type != IV_PROGRESSIVE) ||
1119 (ps_cfg->u4_entropy_coding_mode == CABAC) || (ps_cfg->u4_weighted_prediction))
1120 {
1121 ps_sps->u1_profile_idc = IH264_PROFILE_MAIN;
1122 }
1123 else
1124 {
1125 ps_sps->u1_profile_idc = IH264_PROFILE_BASELINE;
1126 }
1127
1128 /* level */
1129 ps_sps->u1_level_idc = MAX(ps_cfg->u4_max_level,
1130 (UWORD32)ih264e_get_min_level(ps_cfg->u4_max_wd, ps_cfg->u4_max_ht));
1131
1132 /* constrained flags */
1133 /*
1134 * baseline profile automatically implies set 0 flag
1135 */
1136 ps_sps->u1_constraint_set0_flag = (ps_sps->u1_profile_idc == IH264_PROFILE_BASELINE);
1137 /*
1138 * main profile automatically implies set 1 flag
1139 * Although the encoder says it supports Baseline profile it actually supports constrained
1140 * baseline profile as ASO, FMO and redundant slices are not supported
1141 */
1142 ps_sps->u1_constraint_set1_flag = (ps_sps->u1_profile_idc <= IH264_PROFILE_MAIN);
1143 /*
1144 * extended profile is not supported
1145 */
1146 ps_sps->u1_constraint_set2_flag = 0x00;
1147 /*
1148 * level 1b or level 11
1149 */
1150 if (ps_sps->u1_level_idc == IH264_LEVEL_1B)
1151 {
1152 ps_sps->u1_constraint_set3_flag = 0;
1153 ps_sps->u1_level_idc = IH264_LEVEL_11;
1154 }
1155 else
1156 {
1157 ps_sps->u1_constraint_set3_flag = 0;
1158 }
1159
1160 /* active sps id */
1161 ps_sps->u1_sps_id = ps_codec->i4_sps_id;
1162
1163 if (ps_sps->u1_profile_idc >= IH264_PROFILE_HIGH)
1164 {
1165 /* chroma format idc */
1166 ps_sps->u1_chroma_format_idc = CHROMA_FMT_IDC_YUV420;
1167
1168 /* residual_colour_transform_flag */
1169 ps_sps->i1_residual_colour_transform_flag = 0;
1170
1171 /* luma bit depth 8 */
1172 ps_sps->i1_bit_depth_luma = 8;
1173
1174 /* chroma bit depth 8 */
1175 ps_sps->i1_bit_depth_chroma = 8;
1176
1177 /* qpprime_y_zero_transform_bypass_flag */
1178 ps_sps->i1_qpprime_y_zero_transform_bypass_flag = 0;
1179
1180 /* seq_scaling_matrix_present_flag */
1181 ps_sps->i1_seq_scaling_matrix_present_flag = 0;
1182
1183 if (ps_sps->i1_seq_scaling_matrix_present_flag)
1184 {
1185 /* TODO_LATER: Will be enabled once scaling list support is added */
1186 }
1187 }
1188
1189 /* log2_max_frame_num_minus4 */
1190 ps_sps->i1_log2_max_frame_num = 16;
1191
1192 /* pic_order_cnt_type */
1193 ps_sps->i1_pic_order_cnt_type = 2;
1194
1195 if (ps_codec->i4_non_ref_frames_in_stream)
1196 {
1197 ps_sps->i1_pic_order_cnt_type = 0;
1198 }
1199
1200 /* log2_max_pic_order_cnt_lsb_minus4 */
1201 ps_sps->i1_log2_max_pic_order_cnt_lsb = 8;
1202
1203 /* TODO : add support for other poc types */
1204 if (ps_sps->i1_pic_order_cnt_type == 0)
1205 {
1206
1207 }
1208 else if (ps_sps->i1_pic_order_cnt_type == 1)
1209 {
1210
1211 }
1212
1213 /* num_ref_frames */
1214 /* TODO : Should we have a flexible num ref frames */
1215 if (ps_codec->s_cfg.u4_num_bframes > 0)
1216 {
1217 ps_sps->u1_max_num_ref_frames = 2;
1218 }
1219 else
1220 {
1221 ps_sps->u1_max_num_ref_frames = 1;
1222 }
1223
1224 /* gaps_in_frame_num_value_allowed_flag */
1225 ps_sps->i1_gaps_in_frame_num_value_allowed_flag = 0;
1226
1227 /* pic width in mb - 1 */
1228 ps_sps->i2_pic_width_in_mbs_minus1 = ps_cfg->i4_wd_mbs - 1;
1229
1230 /* pic height in mb - 1 */
1231 ps_sps->i2_pic_height_in_map_units_minus1 = ps_cfg->i4_ht_mbs - 1;;
1232
1233 /* frame_mbs_only_flag, no support for interlace encoding */
1234 ps_sps->i1_frame_mbs_only_flag = 1;
1235
1236 /* mb_adaptive_frame_field_flag */
1237 if (ps_sps->i1_frame_mbs_only_flag == 0)
1238 {
1239 ps_sps->i1_mb_adaptive_frame_field_flag = 0;
1240 }
1241
1242 /* direct_8x8_inference_flag */
1243 if (ps_sps->u1_level_idc < IH264_LEVEL_30)
1244 {
1245 ps_sps->i1_direct_8x8_inference_flag = 0;
1246 }
1247 else
1248 {
1249 ps_sps->i1_direct_8x8_inference_flag = 1;
1250 }
1251
1252 /* cropping params */
1253 /*NOTE : Cropping values depend on the chroma format
1254 * For our case ,decoder interprets the cropping values as 2*num pixels
1255 * Hence the difference in the disp width and width must be halved before sending
1256 * to get the expected results
1257 */
1258 ps_sps->i1_frame_cropping_flag = 0;
1259 ps_sps->i2_frame_crop_left_offset = 0;
1260 ps_sps->i2_frame_crop_right_offset = (ps_codec->s_cfg.u4_wd - ps_codec->s_cfg.u4_disp_wd)>>1;
1261 ps_sps->i2_frame_crop_top_offset = 0;
1262 ps_sps->i2_frame_crop_bottom_offset = (ps_codec->s_cfg.u4_ht - ps_codec->s_cfg.u4_disp_ht)>>1;
1263
1264 if (ps_sps->i2_frame_crop_left_offset ||
1265 ps_sps->i2_frame_crop_right_offset ||
1266 ps_sps->i2_frame_crop_top_offset ||
1267 ps_sps->i2_frame_crop_bottom_offset)
1268 {
1269 ps_sps->i1_frame_cropping_flag = 1;
1270 }
1271
1272 /* vui params */
1273 ps_sps->i1_vui_parameters_present_flag = 1;
1274
1275 if (ps_sps->i1_vui_parameters_present_flag)
1276 {
1277 /* populate vui params */
1278 ih264e_populate_vui(ps_codec);
1279 }
1280
1281 return i4_err_code;
1282 }
1283
1284 /**
1285 ******************************************************************************
1286 *
1287 * @brief Populates pps structure
1288 *
1289 * @par Description
1290 * Populates pps structure for its use in header generation
1291 *
1292 * @param[in] ps_codec
1293 * pointer to encoder context
1294 *
1295 * @param[out] ps_pps
1296 * pointer to pps params that needs to be populated
1297 *
1298 * @return success or failure error code
1299 *
1300 ******************************************************************************
1301 */
ih264e_populate_pps(codec_t * ps_codec,pps_t * ps_pps)1302 IH264E_ERROR_T ih264e_populate_pps(codec_t *ps_codec, pps_t *ps_pps)
1303 {
1304 /* active config parameters */
1305 cfg_params_t *ps_cfg = &(ps_codec->s_cfg);
1306
1307 /* seq_parameter_set_id */
1308 ps_pps->u1_sps_id = ps_codec->i4_sps_id;
1309
1310 /* pic_parameter_set_id */
1311 ps_pps->u1_pps_id = ps_codec->i4_pps_id;
1312
1313 /* entropy_coding_mode */
1314 ps_pps->u1_entropy_coding_mode_flag = ps_cfg->u4_entropy_coding_mode;
1315
1316 /* pic_order_present_flag is unset if we don't have feilds */
1317 ps_pps->u1_pic_order_present_flag = 0;
1318
1319 /* Currently number of slice groups supported are 1 */
1320 ps_pps->u1_num_slice_groups = 1;
1321
1322 if (ps_pps->u1_num_slice_groups - 1)
1323 {
1324 /* TODO_LATER: Currently the number of slice groups minus 1 is 0.
1325 * If this is not the case, we have to add Slice group map type to the bit stream*/
1326 }
1327
1328 /* number of reference frames for list 0 */
1329 /* FIXME : fix this hard coded value */
1330 ps_pps->i1_num_ref_idx_l0_default_active = 1;
1331
1332 /* number of reference frames for list 1 */
1333 ps_pps->i1_num_ref_idx_l1_default_active = 1;
1334
1335 /* weighted prediction for now is disabled */
1336 ps_pps->i1_weighted_pred_flag = 0;
1337 ps_pps->i1_weighted_bipred_idc = 0;
1338
1339 /* The intent is to not signal qp from pps. Rather send the same in slice headers */
1340 ps_pps->i1_pic_init_qp = 0;
1341
1342 /* The intent is to not signal qp from pps. Rather send the same in slice headers */
1343 ps_pps->i1_pic_init_qs = 0;
1344
1345 /* The intent is to not signal qp from pps. Rather send the same in slice headers */
1346 ps_pps->i1_chroma_qp_index_offset = 0;
1347
1348 /* deblocking filter flags present in slice header */
1349 ps_pps->i1_deblocking_filter_control_present_flag = 1;
1350
1351 /* constrained intra prediction */
1352 ps_pps->i1_constrained_intra_pred_flag = ps_cfg->u4_constrained_intra_pred;
1353
1354 /* sending redundant slices is not supported for now */
1355 ps_pps->i1_redundant_pic_cnt_present_flag = 0;
1356
1357 ps_pps->u1_slice_group_map_type = 0;
1358 return IH264E_SUCCESS;
1359 }
1360
1361 /**
1362 ******************************************************************************
1363 *
1364 * @brief Populates slice header structure
1365 *
1366 * @par Description
1367 * Populates slice header structure for its use in header generation
1368 *
1369 * @param[in] ps_proc
1370 * pointer to proc context
1371 *
1372 * @param[out] ps_slice_hdr
1373 * pointer to slice header structure that needs to be populated
1374 *
1375 * @param[in] ps_pps
1376 * pointer to pps params structure referred by the slice
1377 *
1378 * @param[in] ps_sps
1379 * pointer to sps params referred by the pps
1380 *
1381 * @return success or failure error code
1382 *
1383 ******************************************************************************
1384 */
ih264e_populate_slice_header(process_ctxt_t * ps_proc,slice_header_t * ps_slice_hdr,pps_t * ps_pps,sps_t * ps_sps)1385 WORD32 ih264e_populate_slice_header(process_ctxt_t *ps_proc,
1386 slice_header_t *ps_slice_hdr,
1387 pps_t *ps_pps,
1388 sps_t *ps_sps)
1389 {
1390 /* entropy context */
1391 entropy_ctxt_t *ps_entropy = &ps_proc->s_entropy;
1392
1393 codec_t *ps_codec = ps_proc->ps_codec;
1394
1395 if (ps_proc->ps_codec->u4_is_curr_frm_ref)
1396 {
1397 ps_slice_hdr->i1_nal_unit_idc = 3;
1398 }
1399 else
1400 {
1401 ps_slice_hdr->i1_nal_unit_idc = 0;
1402 }
1403
1404 /* start mb address */
1405 ps_slice_hdr->u2_first_mb_in_slice = ps_entropy->i4_mb_start_add;
1406
1407 /* slice type */
1408 ps_slice_hdr->u1_slice_type = ps_proc->i4_slice_type;
1409
1410 /* pic_parameter_set_id */
1411 ps_slice_hdr->u1_pps_id = ps_pps->u1_pps_id;
1412
1413 /* Separate color plane flag is 0,
1414 * hence the syntax element color_plane_id not included */
1415
1416 /* frame num */
1417 ps_slice_hdr->i4_frame_num = ps_proc->i4_frame_num;
1418
1419 /* frame_mbs_only_flag, no support for interlace encoding */
1420 if (!ps_sps->i1_frame_mbs_only_flag)
1421 {
1422 ps_slice_hdr->i1_field_pic_flag = 0;
1423
1424 if (ps_slice_hdr->i1_field_pic_flag)
1425 {
1426 ps_slice_hdr->i1_bottom_field_flag = 0;
1427 }
1428 }
1429
1430 /* idr pic id */
1431 if (ps_proc->u4_is_idr)
1432 {
1433 ps_slice_hdr->u2_idr_pic_id = ps_proc->u4_idr_pic_id;
1434 ps_slice_hdr->i1_nal_unit_type = 5;
1435 }
1436 else
1437 {
1438 ps_slice_hdr->i1_nal_unit_type = 1;
1439 }
1440
1441 if (ps_sps->i1_pic_order_cnt_type == 0)
1442 {
1443
1444 WORD32 i4_poc;
1445 i4_poc = ps_codec->i4_poc;
1446 i4_poc %= (1 << ps_sps->i1_log2_max_pic_order_cnt_lsb);
1447 ps_slice_hdr->i4_pic_order_cnt_lsb = i4_poc;
1448 }
1449 /* TODO add support for poc type 1 */
1450 else if (ps_sps->i1_pic_order_cnt_type == 1)
1451 {
1452
1453 }
1454
1455
1456 /*
1457 * redundant slices are not currently supported.
1458 * Hence the syntax element redundant slice cnt is not initialized
1459 */
1460 if (ps_pps->i1_redundant_pic_cnt_present_flag)
1461 {
1462
1463 }
1464
1465 /* direct spatial mv pred flag */
1466 if (ps_proc->i4_slice_type == BSLICE)
1467 {
1468 ps_slice_hdr->u1_direct_spatial_mv_pred_flag = 1;
1469 }
1470
1471 if (ps_proc->i4_slice_type == PSLICE || ps_proc->i4_slice_type == SPSLICE || ps_proc->i4_slice_type == BSLICE)
1472 {
1473 /* num_ref_idx_active_override_flag */
1474 ps_slice_hdr->u1_num_ref_idx_active_override_flag = 0;
1475
1476 if (ps_slice_hdr->u1_num_ref_idx_active_override_flag)
1477 {
1478 /* num_ref_idx_l0_active_minus1 */
1479
1480 if (ps_proc->i4_slice_type == BSLICE)
1481 {
1482 /* num_ref_idx_l1_active_minus1 */
1483
1484 }
1485 }
1486 }
1487
1488 /* ref_idx_reordering */
1489 /* TODO: ref_idx_reordering */
1490 if ((ps_proc->i4_slice_type != ISLICE) && (ps_proc->i4_slice_type != SISLICE))
1491 {
1492 /* ref_pic_list_reordering_flag_l0 */
1493 ps_slice_hdr->u1_ref_idx_reordering_flag_l0 = 0;
1494
1495 if (ps_slice_hdr->u1_ref_idx_reordering_flag_l0)
1496 {
1497
1498 }
1499
1500 /* ref_pic_list_reordering_flag_l1 */
1501 ps_slice_hdr->u1_ref_idx_reordering_flag_l1 = 0;
1502
1503 if (ps_slice_hdr->u1_ref_idx_reordering_flag_l1)
1504 {
1505
1506 }
1507 }
1508
1509
1510 /* Currently we do not support weighted pred */
1511 /* ps_slice_hdr->u1_weighted_bipred_idc = 0; */
1512
1513 if ((ps_pps->i1_weighted_pred_flag &&
1514 (ps_proc->i4_slice_type == PSLICE || ps_proc->i4_slice_type == SPSLICE)) ||
1515 (ps_proc->i4_slice_type == BSLICE && ps_pps->i1_weighted_bipred_idc == 1))
1516 {
1517 /* TODO_LATER: Currently there is no support for weighted prediction.
1518 This needs to be updated when the support is added */
1519 }
1520
1521 if (ps_slice_hdr->i1_nal_unit_idc != 0)
1522 {
1523 if (ps_slice_hdr->i1_nal_unit_type == 5)
1524 {
1525 /* no_output_of_prior_pics_flag */
1526 ps_slice_hdr->u1_no_output_of_prior_pics_flag = 0;
1527
1528 /* long_term_reference_flag */
1529 ps_slice_hdr->u1_long_term_reference_flag = 0;
1530 }
1531 else
1532 {
1533 /* adaptive_ref_pic_marking_mode_flag */
1534 ps_slice_hdr->u1_adaptive_ref_pic_marking_mode_flag = 0;
1535
1536 if (ps_slice_hdr->u1_adaptive_ref_pic_marking_mode_flag)
1537 {
1538 /* TODO: if the reference picture marking mode is adaptive
1539 add these fields in the bit-stream */
1540 }
1541 }
1542 }
1543
1544 /* entropy coding mode flag */
1545 ps_slice_hdr->u1_entropy_coding_mode_flag = ps_entropy->u1_entropy_coding_mode_flag;
1546
1547 if (ps_slice_hdr->u1_entropy_coding_mode_flag && ps_proc->i4_slice_type != ISLICE &&
1548 ps_proc->i4_slice_type != SISLICE)
1549 {
1550 /* cabac_init_idc */
1551 }
1552
1553 /* slice qp */
1554 ps_slice_hdr->i1_slice_qp = ps_proc->u4_frame_qp;
1555
1556 if (ps_proc->i4_slice_type == SPSLICE || ps_proc->i4_slice_type == SISLICE)
1557 {
1558 if (ps_proc->i4_slice_type == SPSLICE)
1559 {
1560 /* sp_for_switch_flag */
1561 }
1562 /* slice_qs_delta */
1563 }
1564
1565 if (ps_pps->i1_deblocking_filter_control_present_flag)
1566 {
1567 /* disable_deblocking_filter_idc */
1568 ps_slice_hdr->u1_disable_deblocking_filter_idc = ps_proc->u4_disable_deblock_level;
1569
1570 if (ps_slice_hdr->u1_disable_deblocking_filter_idc != 1)
1571 {
1572 /* slice_alpha_c0_offset_div2 */
1573 ps_slice_hdr->i1_slice_alpha_c0_offset_div2 = 0;
1574
1575 /* slice_beta_offset_div2 */
1576 ps_slice_hdr->i1_slice_beta_offset_div2 = 0;
1577 }
1578 }
1579 ps_slice_hdr->u1_num_slice_groups_minus1 = 0;
1580 if(ps_slice_hdr->u1_num_slice_groups_minus1 > 0 &&
1581 ps_pps->u1_slice_group_map_type >= 3 &&
1582 ps_pps->u1_slice_group_map_type <= 5)
1583 {
1584 /* slice_group_change_cycle */
1585 /* TODO_LATER: Currently the number of slice groups minus 1 is 0.
1586 * If this is not the case, we have to add Slice group map type to the bit stream */
1587 }
1588
1589 ps_slice_hdr->i1_cabac_init_idc = CABAC_INIT_IDC;
1590
1591 return IH264E_SUCCESS;
1592 }
1593
1594 /**
1595 ******************************************************************************
1596 *
1597 * @brief inserts FILLER Nal Unit.
1598 *
1599 * @par Description
1600 * In constant bit rate rc mode, when the bits generated by the codec is
1601 * underflowing the target bit rate, the encoder library inserts filler nal unit.
1602 *
1603 * @param[in] ps_bitstrm
1604 * pointer to bitstream context (handle)
1605 *
1606 * @param[in] insert_fill_bytes
1607 * Number of fill bytes to be inserted
1608 *
1609 * @return success or failure error code
1610 *
1611 ******************************************************************************
1612 */
ih264e_add_filler_nal_unit(bitstrm_t * ps_bitstrm,WORD32 insert_fill_bytes)1613 IH264E_ERROR_T ih264e_add_filler_nal_unit(bitstrm_t *ps_bitstrm,
1614 WORD32 insert_fill_bytes)
1615 {
1616 WORD32 i4_num_words_to_fill, i4_words_filled;
1617
1618 IH264E_ERROR_T return_status = IH264E_SUCCESS;
1619
1620 /* Insert the NAL start code */
1621 return_status = ih264e_put_nal_start_code_prefix(ps_bitstrm, 1);
1622 if(return_status != IH264E_SUCCESS)
1623 {
1624 return return_status;
1625 }
1626
1627 if (ps_bitstrm->u4_strm_buf_offset + insert_fill_bytes >= ps_bitstrm->u4_max_strm_size)
1628 {
1629 return (IH264E_BITSTREAM_BUFFER_OVERFLOW);
1630 }
1631
1632 /* Insert Nal Unit Header */
1633 PUT_BITS(ps_bitstrm, NAL_FILLER_FIRST_BYTE, 8, return_status, "filler_header");
1634
1635 PUT_BITS(ps_bitstrm, 0xFFFFFF, 24, return_status, "fill bytes");
1636
1637 /* Initializing Variables */
1638 i4_words_filled = 1;
1639
1640 /****************************************************/
1641 /* Flooring the number of bytes for be stuffed to */
1642 /* WORD unit */
1643 /****************************************************/
1644 i4_num_words_to_fill = (insert_fill_bytes >> 2);
1645
1646 /****************************************************/
1647 /* Reducing already 4 bytes filled. In case stuffing*/
1648 /* is <= 4 bytes, we are actually not stuffing */
1649 /* anything */
1650 /****************************************************/
1651 i4_num_words_to_fill -= i4_words_filled;
1652
1653 while (i4_num_words_to_fill > 0)
1654 {
1655 /* Insert Nal Unit Header */
1656 PUT_BITS(ps_bitstrm, 0xFFFFFFFF, 32, return_status, "fill bytes");
1657
1658 i4_num_words_to_fill-- ;
1659 }
1660
1661 return_status = ih264e_put_rbsp_trailing_bits(ps_bitstrm);
1662
1663 return return_status;
1664 }
1665
1666