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_sei.c
25 *
26 * @brief
27 * This file contains function definitions related to SEI NAL's header encoding
28 *
29 * @author
30 * ittiam
31 *
32 * @par List of Functions:
33 * - ih264e_put_sei_mdcv_params
34 * - ih264e_put_sei_cll_params
35 * - ih264e_put_sei_ave_params
36 * - ih264e_put_sei_ccv_params
37 * - ih264e_put_sei_msg
38 * - ih264e_put_sei_sii_params
39 *
40 * @remarks
41 * None
42 *
43 *******************************************************************************
44 */
45
46 /*****************************************************************************/
47 /* File Includes */
48 /*****************************************************************************/
49
50 /* System include files */
51 #include <stdio.h>
52 #include <stddef.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <assert.h>
56
57 /* User include files */
58 #include "ih264_typedefs.h"
59 #include "iv2.h"
60 #include "ive2.h"
61 #include "ih264_macros.h"
62 #include "ih264_platform_macros.h"
63 #include "ih264e_trace.h"
64 #include "ih264e_error.h"
65 #include "ih264e_bitstream.h"
66 #include "ih264_defs.h"
67 #include "ih264_structs.h"
68 #include "ih264_cabac_tables.h"
69 #include "ih264e_cabac_structs.h"
70 #include "irc_cntrl_param.h"
71 #include "ime_distortion_metrics.h"
72 #include "ime_defs.h"
73 #include "ime_structs.h"
74 #include "ih264e_defs.h"
75 #include "irc_cntrl_param.h"
76 #include "irc_frame_info_collector.h"
77 #include "ih264_trans_quant_itrans_iquant.h"
78 #include "ih264_inter_pred_filters.h"
79 #include "ih264_deblk_edge_filters.h"
80 #include "ih264e_structs.h"
81 #include "ih264e_encode_header.h"
82 #include "ih264e_sei.h"
83
84 /*****************************************************************************/
85 /* Function Definitions */
86 /*****************************************************************************/
87
88 /**
89 ******************************************************************************
90 *
91 * @brief Generates Mastering Display Color Volume (Supplemental Enhancement Information )
92 *
93 * @par Description
94 * Parse Supplemental Enhancement Information
95 *
96 * @param[in] ps_bitstrm
97 * pointer to bitstream context (handle)
98 *
99 * @param[in] ps_sei_mdcv
100 * pointer to structure containing mdcv SEI data
101 *
102 * @return success or failure error code
103 *
104 ******************************************************************************
105 */
ih264e_put_sei_mdcv_params(sei_mdcv_params_t * ps_sei_mdcv,bitstrm_t * ps_bitstrm)106 IH264E_ERROR_T ih264e_put_sei_mdcv_params(sei_mdcv_params_t *ps_sei_mdcv,
107 bitstrm_t *ps_bitstrm)
108 {
109 WORD32 return_status = IH264E_SUCCESS;
110 UWORD8 u1_payload_size = 0;
111 UWORD32 u4_count;
112
113 if(ps_sei_mdcv == NULL)
114 {
115 return IH264E_FAIL;
116 }
117
118 u1_payload_size += (NUM_SEI_MDCV_PRIMARIES * 2); /* display primaries x */
119 u1_payload_size += (NUM_SEI_MDCV_PRIMARIES * 2); /* display primaries y */
120 u1_payload_size += 2; /* white point x */
121 u1_payload_size += 2; /* white point y */
122 u1_payload_size += 4; /* max display mastering luminance */
123 u1_payload_size += 4; /* min display mastering luminance */
124
125 /************************************************************************/
126 /* PayloadSize : This is the size of the payload in bytes */
127 /************************************************************************/
128 PUT_BITS(ps_bitstrm, u1_payload_size, 8, return_status, "u1_payload_size");
129
130 /*******************************************************************************/
131 /* Put the mastering display color volume SEI parameters into the bitstream. */
132 /*******************************************************************************/
133
134 /* display primaries x */
135 for(u4_count = 0; u4_count < NUM_SEI_MDCV_PRIMARIES; u4_count++)
136 {
137 PUT_BITS(ps_bitstrm, ps_sei_mdcv->au2_display_primaries_x[u4_count], 16,
138 return_status, "u2_display_primaries_x");
139
140 PUT_BITS(ps_bitstrm, ps_sei_mdcv->au2_display_primaries_y[u4_count], 16,
141 return_status, "u2_display_primaries_y");
142 }
143
144 /* white point x */
145 PUT_BITS(ps_bitstrm, ps_sei_mdcv->u2_white_point_x, 16, return_status, "u2_white point x");
146
147 /* white point y */
148 PUT_BITS(ps_bitstrm, ps_sei_mdcv->u2_white_point_y, 16, return_status, "u2_white point y");
149
150 /* max display mastering luminance */
151 PUT_BITS(ps_bitstrm, ps_sei_mdcv->u4_max_display_mastering_luminance, 32,
152 return_status, "u4_max_display_mastering_luminance");
153
154 /* min display mastering luminance */
155 PUT_BITS(ps_bitstrm, ps_sei_mdcv->u4_min_display_mastering_luminance, 32,
156 return_status, "u4_max_display_mastering_luminance");
157
158 return (return_status);
159 }
160
161 /**
162 ******************************************************************************
163 *
164 * @brief Stores content light level info in bitstream
165 *
166 * @par Description
167 * Parse Supplemental Enhancement Information
168 *
169 * @param[in] ps_bitstrm
170 * pointer to bitstream context (handle)
171 *
172 * @param[in] ps_sei_cll
173 * Pinter to structure containing cll sei params
174 *
175 * @return success or failure error code
176 *
177 ******************************************************************************
178 */
ih264e_put_sei_cll_params(sei_cll_params_t * ps_sei_cll,bitstrm_t * ps_bitstrm)179 IH264E_ERROR_T ih264e_put_sei_cll_params(sei_cll_params_t *ps_sei_cll,
180 bitstrm_t *ps_bitstrm)
181 {
182 WORD32 return_status = IH264E_SUCCESS;
183 UWORD8 u1_payload_size = 0;
184
185 if(ps_sei_cll == NULL)
186 {
187 return IH264E_FAIL;
188 }
189
190 u1_payload_size += 2; /* max pic average light level */
191 u1_payload_size += 2; /* max content light level */
192
193 /************************************************************************/
194 /* PayloadSize : This is the size of the payload in bytes */
195 /************************************************************************/
196 PUT_BITS(ps_bitstrm, u1_payload_size, 8, return_status, "u1_payload_size");
197
198 PUT_BITS(ps_bitstrm, ps_sei_cll->u2_max_content_light_level, 16,
199 return_status, "u2_max_content_light_level");
200 PUT_BITS(ps_bitstrm, ps_sei_cll->u2_max_pic_average_light_level, 16,
201 return_status, "u2_max_pic_average_light_level");
202
203 return (return_status);
204 }
205
206 /**
207 ******************************************************************************
208 *
209 * @brief Stores ambient viewing environment info in bitstream
210 *
211 * @par Description
212 * Parse Supplemental Enhancement Information
213 *
214 * @param[in] ps_bitstrm
215 * pointer to bitstream context (handle)
216 *
217 * @param[in] ps_sei_ave
218 * pointer to ambient viewing environment info
219 *
220 * @return success or failure error code
221 *
222 ******************************************************************************
223 */
ih264e_put_sei_ave_params(sei_ave_params_t * ps_sei_ave,bitstrm_t * ps_bitstrm)224 IH264E_ERROR_T ih264e_put_sei_ave_params(sei_ave_params_t *ps_sei_ave,
225 bitstrm_t *ps_bitstrm)
226 {
227 WORD32 return_status = IH264E_SUCCESS;
228 UWORD8 u1_payload_size = 0;
229
230 if(ps_sei_ave == NULL)
231 {
232 return IH264E_FAIL;
233 }
234
235 u1_payload_size += 4; /* ambient illuminance */
236 u1_payload_size += 2; /* ambient light x */
237 u1_payload_size += 2; /* ambient light y */
238
239 /************************************************************************/
240 /* PayloadSize : This is the size of the payload in bytes */
241 /************************************************************************/
242 PUT_BITS(ps_bitstrm, u1_payload_size, 8, return_status, "u1_payload_size");
243
244 PUT_BITS(ps_bitstrm, ps_sei_ave->u4_ambient_illuminance, 32,
245 return_status, "u4_ambient_illuminance");
246 PUT_BITS(ps_bitstrm, ps_sei_ave->u2_ambient_light_x, 16,
247 return_status, "u2_ambient_light_x");
248 PUT_BITS(ps_bitstrm, ps_sei_ave->u2_ambient_light_y, 16,
249 return_status, "u2_ambient_light_y");
250
251 return (return_status);
252 }
253
254 /**
255 ******************************************************************************
256 *
257 * @brief Generates Content Color Volume info (Supplemental Enhancement Information )
258 *
259 * @par Description
260 * Parse Supplemental Enhancement Information
261 *
262 * @param[in] ps_bitstrm
263 * pointer to bitstream context (handle)
264 *
265 * @param[in] ps_sei_ccv
266 * pointer to structure containing CCV SEI data
267 *
268 * @return success or failure error code
269 *
270 ******************************************************************************
271 */
ih264e_put_sei_ccv_params(sei_ccv_params_t * ps_sei_ccv,bitstrm_t * ps_bitstrm)272 IH264E_ERROR_T ih264e_put_sei_ccv_params(sei_ccv_params_t *ps_sei_ccv,
273 bitstrm_t *ps_bitstrm)
274 {
275 WORD32 return_status = IH264E_SUCCESS;
276 UWORD16 u2_payload_bits = 0;
277 UWORD8 u1_payload_bytes = 0;
278 UWORD32 u4_count;
279
280 if(ps_sei_ccv == NULL)
281 {
282 return IH264E_FAIL;
283 }
284
285 u2_payload_bits += 1; /* ccv cancel flag */
286 if(0 == (UWORD32)ps_sei_ccv->u1_ccv_cancel_flag)
287 {
288 u2_payload_bits += 1; /* ccv persistence flag */
289 u2_payload_bits += 1; /* ccv primaries present flag */
290 u2_payload_bits += 1; /* ccv min luminance value present flag */
291 u2_payload_bits += 1; /* ccv max luminance value present flag */
292 u2_payload_bits += 1; /* ccv avg luminance value present flag */
293 u2_payload_bits += 2; /* ccv reserved zero 2bits */
294 if(1 == ps_sei_ccv->u1_ccv_primaries_present_flag)
295 {
296 u2_payload_bits += (NUM_SEI_CCV_PRIMARIES * 32); /* ccv primaries x[ c ] */
297 u2_payload_bits += (NUM_SEI_CCV_PRIMARIES * 32); /* ccv primaries y[ c ] */
298 }
299 if(1 == ps_sei_ccv->u1_ccv_min_luminance_value_present_flag)
300 {
301 u2_payload_bits += 32; /* ccv min luminance value */
302 }
303 if(1 == ps_sei_ccv->u1_ccv_max_luminance_value_present_flag)
304 {
305 u2_payload_bits += 32; /* ccv max luminance value */
306 }
307 if(1 == ps_sei_ccv->u1_ccv_avg_luminance_value_present_flag)
308 {
309 u2_payload_bits += 32; /* ccv avg luminance value */
310 }
311 }
312
313 u1_payload_bytes = (UWORD8)((u2_payload_bits + 7) >> 3);
314 /************************************************************************/
315 /* PayloadSize : This is the size of the payload in bytes */
316 /************************************************************************/
317 PUT_BITS(ps_bitstrm, u1_payload_bytes, 8, return_status, "u1_payload_bytes");
318
319 /*******************************************************************************/
320 /* Put the Content Color Volume SEI parameters into the bitstream. */
321 /*******************************************************************************/
322
323 PUT_BITS(ps_bitstrm, ps_sei_ccv->u1_ccv_cancel_flag, 1,
324 return_status, "u1_ccv_cancel_flag");
325
326 if(0 == ps_sei_ccv->u1_ccv_cancel_flag)
327 {
328 PUT_BITS(ps_bitstrm, ps_sei_ccv->u1_ccv_persistence_flag, 1,
329 return_status, "u1_ccv_persistence_flag");
330 PUT_BITS(ps_bitstrm, ps_sei_ccv->u1_ccv_primaries_present_flag, 1,
331 return_status, "u1_ccv_primaries_present_flag");
332 PUT_BITS(ps_bitstrm, ps_sei_ccv->u1_ccv_min_luminance_value_present_flag, 1,
333 return_status, "u1_ccv_min_luminance_value_present_flag");
334 PUT_BITS(ps_bitstrm, ps_sei_ccv->u1_ccv_max_luminance_value_present_flag, 1,
335 return_status, "u1_ccv_max_luminance_value_present_flag");
336 PUT_BITS(ps_bitstrm, ps_sei_ccv->u1_ccv_avg_luminance_value_present_flag, 1,
337 return_status, "u1_ccv_avg_luminance_value_present_flag");
338 PUT_BITS(ps_bitstrm, ps_sei_ccv->u1_ccv_reserved_zero_2bits, 2,
339 return_status, "u1_ccv_reserved_zero_2bits");
340
341 /* ccv primaries */
342 if(1 == ps_sei_ccv->u1_ccv_primaries_present_flag)
343 {
344 for(u4_count = 0; u4_count < NUM_SEI_CCV_PRIMARIES; u4_count++)
345 {
346 PUT_BITS(ps_bitstrm, ps_sei_ccv->ai4_ccv_primaries_x[u4_count], 32,
347 return_status, "i4_ccv_primaries_x");
348 PUT_BITS(ps_bitstrm, ps_sei_ccv->ai4_ccv_primaries_y[u4_count], 32,
349 return_status, "i4_ccv_primaries_y");
350 }
351 }
352
353 if(1 == ps_sei_ccv->u1_ccv_min_luminance_value_present_flag)
354 {
355 PUT_BITS(ps_bitstrm, ps_sei_ccv->u4_ccv_min_luminance_value, 32,
356 return_status, "u4_ccv_min_luminance_value");
357 }
358 if(1 == ps_sei_ccv->u1_ccv_max_luminance_value_present_flag)
359 {
360 PUT_BITS(ps_bitstrm, ps_sei_ccv->u4_ccv_max_luminance_value, 32,
361 return_status, "u4_ccv_max_luminance_value");
362 }
363 if(1 == ps_sei_ccv->u1_ccv_avg_luminance_value_present_flag)
364 {
365 PUT_BITS(ps_bitstrm, ps_sei_ccv->u4_ccv_avg_luminance_value, 32,
366 return_status, "u4_ccv_avg_luminance_value");
367 }
368 }
369
370 return (return_status);
371 }
372
373 /**
374 ******************************************************************************
375 *
376 * @brief Signal shutter interval info in the bitstream
377 *
378 * @par Description
379 * Parse Supplemental Enhancement Information
380 *
381 * @param[in] ps_bitstrm
382 * pointer to bitstream context (handle)
383 *
384 * @param[in] ps_sei_sii
385 * pointer to shutter interval info
386 *
387 * @return success or failure error code
388 *
389 ******************************************************************************
390 */
ih264e_put_sei_sii_params(sei_sii_params_t * ps_sei_sii,bitstrm_t * ps_bitstrm)391 IH264E_ERROR_T ih264e_put_sei_sii_params(sei_sii_params_t *ps_sei_sii, bitstrm_t *ps_bitstrm)
392 {
393 WORD32 return_status = IH264E_SUCCESS;
394 UWORD16 u2_payload_bits = 0;
395 UWORD8 u1_payload_bytes = 0;
396
397 if(ps_sei_sii == NULL)
398 {
399 return IH264E_FAIL;
400 }
401
402 if(0 == ps_sei_sii->u4_sii_sub_layer_idx)
403 {
404 u2_payload_bits += 1; /* shutter interval info present flag */
405
406 if(1 == ps_sei_sii->u1_shutter_interval_info_present_flag)
407 {
408 u2_payload_bits += 32; /* sii time scale */
409 u2_payload_bits += 1; /* fixed shutter interval within cvs flag */
410
411 if(1 == ps_sei_sii->u1_fixed_shutter_interval_within_cvs_flag)
412 {
413 u2_payload_bits += 32; /* sii num units in shutter interval */
414 }
415 else
416 {
417 int sizeofSubLayer;
418 u2_payload_bits += 3; /* sii max sub layers minus1 */
419 sizeofSubLayer =
420 sizeof(ps_sei_sii->au4_sub_layer_num_units_in_shutter_interval) /
421 sizeof(ps_sei_sii->au4_sub_layer_num_units_in_shutter_interval[0]);
422 u2_payload_bits +=
423 32 * sizeofSubLayer; /* sii sub layer num units in shutter interval */
424 }
425 }
426 }
427
428 u1_payload_bytes = (UWORD8) ((u2_payload_bits + 7) >> 3);
429 /************************************************************************/
430 /* PayloadSize : This is the size of the payload in bytes */
431 /************************************************************************/
432 PUT_BITS(ps_bitstrm, u1_payload_bytes, 8, return_status, "u1_payload_bytes");
433
434 /*******************************************************************************/
435 /* Put the Shutter Interval Info SEI parameters into the bitstream. */
436 /*******************************************************************************/
437
438 PUT_BITS_UEV(ps_bitstrm, ps_sei_sii->u4_sii_sub_layer_idx, return_status,
439 "u4_sii_sub_layer_idx");
440
441 if(0 == ps_sei_sii->u4_sii_sub_layer_idx)
442 {
443 PUT_BITS(ps_bitstrm, ps_sei_sii->u1_shutter_interval_info_present_flag, 1, return_status,
444 "u1_shutter_interval_info_present_flag");
445
446 if(1 == ps_sei_sii->u1_shutter_interval_info_present_flag)
447 {
448 PUT_BITS(ps_bitstrm, ps_sei_sii->u4_sii_time_scale, 32, return_status,
449 "u4_sii_time_scale");
450 PUT_BITS(ps_bitstrm, ps_sei_sii->u1_fixed_shutter_interval_within_cvs_flag, 1,
451 return_status, "u1_fixed_shutter_interval_within_cvs_flag");
452
453 if(1 == ps_sei_sii->u1_fixed_shutter_interval_within_cvs_flag)
454 {
455 PUT_BITS(ps_bitstrm, ps_sei_sii->u4_sii_num_units_in_shutter_interval, 32,
456 return_status, "u4_sii_num_units_in_shutter_interval");
457 }
458 else
459 {
460 int i;
461 PUT_BITS(ps_bitstrm, ps_sei_sii->u1_sii_max_sub_layers_minus1, 3, return_status,
462 "u1_sii_max_sub_layers_minus1");
463
464 for(i = 0; i <= ps_sei_sii->u1_sii_max_sub_layers_minus1; i++)
465 {
466 PUT_BITS(ps_bitstrm, ps_sei_sii->au4_sub_layer_num_units_in_shutter_interval[i],
467 32, return_status, "au4_sub_layer_num_units_in_shutter_interval[i]");
468 }
469 }
470 }
471 }
472 return (return_status);
473 }
474
475 /**
476 ******************************************************************************
477 *
478 * @brief Generates SEI (Supplemental Enhancement Information )
479 *
480 * @par Description
481 * Parse Supplemental Enhancement Information
482 *
483 * @param[in] e_payload_type
484 * Determines the type of SEI msg
485 *
486 * @param[in] ps_bitstrm
487 * pointer to bitstream context (handle)
488 *
489 * @param[in] ps_sei_params
490 * pointer to structure containing SEI data
491 * buffer period, recovery point, picture timing
492 *
493 * @return success or failure error code
494 *
495 ******************************************************************************
496 */
ih264e_put_sei_msg(IH264_SEI_TYPE e_payload_type,sei_params_t * ps_sei_params,bitstrm_t * ps_bitstrm)497 IH264E_ERROR_T ih264e_put_sei_msg(IH264_SEI_TYPE e_payload_type,
498 sei_params_t *ps_sei_params,
499 bitstrm_t *ps_bitstrm)
500 {
501 WORD32 return_status = IH264E_SUCCESS;
502
503 /************************************************************************/
504 /* PayloadType : Send in the SEI type in the stream */
505 /************************************************************************/
506
507 UWORD32 u4_payload_type = e_payload_type;
508
509 while(u4_payload_type > 0xFF)
510 {
511 PUT_BITS(ps_bitstrm, 0xFF, 8, return_status, "payload");
512 u4_payload_type -= 0xFF;
513 }
514
515 PUT_BITS(ps_bitstrm, (UWORD32)u4_payload_type, 8, return_status, "e_payload_type");
516
517 switch(e_payload_type)
518 {
519 case IH264_SEI_MASTERING_DISP_COL_VOL :
520 return_status = ih264e_put_sei_mdcv_params(&(ps_sei_params->s_sei_mdcv_params),
521 ps_bitstrm);
522 break;
523
524 case IH264_SEI_CONTENT_LIGHT_LEVEL_DATA :
525 return_status = ih264e_put_sei_cll_params(&(ps_sei_params->s_sei_cll_params),
526 ps_bitstrm);
527 break;
528
529 case IH264_SEI_AMBIENT_VIEWING_ENVIRONMENT :
530 return_status = ih264e_put_sei_ave_params(&(ps_sei_params->s_sei_ave_params),
531 ps_bitstrm);
532 break;
533
534 case IH264_SEI_CONTENT_COLOR_VOLUME :
535 return_status = ih264e_put_sei_ccv_params(&(ps_sei_params->s_sei_ccv_params),
536 ps_bitstrm);
537 break;
538
539 case IH264_SEI_SHUTTER_INTERVAL_INFO:
540 return_status = ih264e_put_sei_sii_params(&(ps_sei_params->s_sei_sii_params), ps_bitstrm);
541
542 break;
543
544 default :
545 return_status = IH264E_FAIL;
546 }
547
548 /* rbsp trailing bits */
549 if((IH264E_SUCCESS == return_status) && (ps_bitstrm->i4_bits_left_in_cw & 0x7))
550 return_status = ih264e_put_rbsp_trailing_bits(ps_bitstrm);
551
552 return(return_status);
553 }
554
555
556