2 * Copyright (C) 2020 Intel Corporation
3 * Author: He Junyan <junyan.he@intel.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the0
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
25 #include "gsth264bitwriter.h"
26 #include <gst/codecparsers/nalutils.h>
27 #include <gst/base/gstbitwriter.h>
29 /******************************** Utils ********************************/
30 #define SIGNED(val) (2 * ABS(val) - ((val) > 0))
32 /* Write an unsigned integer Exp-Golomb-coded syntax element. i.e. ue(v) */
34 _bs_write_ue (GstBitWriter * bs, guint32 value)
36 guint32 size_in_bits = 0;
37 guint32 tmp_value = ++value;
44 && !gst_bit_writer_put_bits_uint32 (bs, 0, size_in_bits - 1))
46 if (!gst_bit_writer_put_bits_uint32 (bs, value, size_in_bits))
51 #define WRITE_BITS_UNCHECK(bw, val, nbits) \
52 (nbits <= 8 ? gst_bit_writer_put_bits_uint8 (bw, val, nbits) : \
53 (nbits <= 16 ? gst_bit_writer_put_bits_uint16 (bw, val, nbits) : \
54 (nbits <= 32 ? gst_bit_writer_put_bits_uint32 (bw, val, nbits) : \
57 #define WRITE_BITS(bw, val, nbits) \
58 if (!WRITE_BITS_UNCHECK (bw, val, nbits)) { \
59 g_warning ("Unsupported bit size: %u", nbits); \
64 #define WRITE_UE_UNCHECK(bw, val) _bs_write_ue (bw, val)
69 #define WRITE_UE(bw, val) \
70 if (!(have_space = WRITE_UE_UNCHECK (bw, val))) \
73 #define WRITE_UE_MAX(bw, val, max) \
74 if ((guint32) val > (max) || !(have_space = WRITE_UE_UNCHECK (bw, val))) \
77 #define WRITE_SE(bw, val) WRITE_UE (bw, SIGNED (val))
79 #define WRITE_SE_RANGE(bw, val, min, max) \
80 if (val > max || val < min || \
81 !(have_space = WRITE_UE_UNCHECK (bw, SIGNED (val)))) \
84 #define WRITE_BYTES_UNCHECK(bw, ptr, nbytes) \
85 gst_bit_writer_put_bytes(bw, ptr, nbytes)
90 #define WRITE_BYTES(bw, ptr, nbytes) \
91 if (!(have_space = WRITE_BYTES_UNCHECK (bw, ptr, nbytes))) \
94 /***************************** End of Utils ****************************/
96 /**** Default scaling_lists according to Table 7-2 *****/
97 static const guint8 default_4x4_intra[16] = {
98 6, 13, 13, 20, 20, 20, 28, 28, 28, 28, 32, 32,
102 static const guint8 default_4x4_inter[16] = {
103 10, 14, 14, 20, 20, 20, 24, 24, 24, 24, 27, 27,
107 static const guint8 default_8x8_intra[64] = {
108 6, 10, 10, 13, 11, 13, 16, 16, 16, 16, 18, 18,
109 18, 18, 18, 23, 23, 23, 23, 23, 23, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27,
110 27, 27, 27, 27, 27, 29, 29, 29, 29, 29, 29, 29, 31, 31, 31, 31, 31, 31, 33,
111 33, 33, 33, 33, 36, 36, 36, 36, 38, 38, 38, 40, 40, 42
114 static const guint8 default_8x8_inter[64] = {
115 9, 13, 13, 15, 13, 15, 17, 17, 17, 17, 19, 19,
116 19, 19, 19, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 24, 24, 24,
117 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27, 27, 27, 28,
118 28, 28, 28, 28, 30, 30, 30, 30, 32, 32, 32, 33, 33, 35
122 _h264_bit_writer_scaling_list (GstBitWriter * bw, gboolean * space,
123 const guint8 scaling_lists_4x4[6][16],
124 const guint8 scaling_lists_8x8[6][64], const guint8 fallback_4x4_inter[16],
125 const guint8 fallback_4x4_intra[16], const guint8 fallback_8x8_inter[64],
126 const guint8 fallback_8x8_intra[64], guint8 n_lists)
128 gboolean have_space = TRUE;
131 const guint8 *default_lists[12] = {
132 fallback_4x4_intra, fallback_4x4_intra, fallback_4x4_intra,
133 fallback_4x4_inter, fallback_4x4_inter, fallback_4x4_inter,
134 fallback_8x8_intra, fallback_8x8_inter,
135 fallback_8x8_intra, fallback_8x8_inter,
136 fallback_8x8_intra, fallback_8x8_inter
139 GST_DEBUG ("writing scaling lists");
141 for (i = 0; i < 12; i++) {
143 guint8 scaling_list_present_flag = FALSE;
144 const guint8 *scaling_list;
148 scaling_list = scaling_lists_4x4[i];
151 scaling_list = scaling_lists_8x8[i - 6];
155 if (memcmp (scaling_list, default_lists[i], size))
156 scaling_list_present_flag = TRUE;
158 WRITE_BITS (bw, scaling_list_present_flag, 1);
159 if (scaling_list_present_flag) {
160 guint8 last_scale, next_scale;
163 for (j = 0; j < size; j++) {
164 last_scale = next_scale = 8;
166 for (j = 0; j < size; j++) {
167 if (next_scale != 0) {
168 delta_scale = (gint8) (scaling_list[j] - last_scale);
170 WRITE_SE (bw, delta_scale);
172 next_scale = scaling_list[j];
174 last_scale = (next_scale == 0) ? last_scale : next_scale;
185 GST_WARNING ("error to write scaling lists");
192 _h264_bit_writer_hrd_parameters (const GstH264HRDParams * hrd,
193 GstBitWriter * bw, gboolean * space)
195 gboolean have_space = TRUE;
198 GST_DEBUG ("writing \"HRD Parameters\"");
200 WRITE_UE_MAX (bw, hrd->cpb_cnt_minus1, 31);
201 WRITE_BITS (bw, hrd->bit_rate_scale, 4);
202 WRITE_BITS (bw, hrd->cpb_size_scale, 4);
204 for (sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1; sched_sel_idx++) {
205 WRITE_UE (bw, hrd->bit_rate_value_minus1[sched_sel_idx]);
206 WRITE_UE (bw, hrd->cpb_size_value_minus1[sched_sel_idx]);
207 WRITE_BITS (bw, hrd->cbr_flag[sched_sel_idx], 1);
210 WRITE_BITS (bw, hrd->initial_cpb_removal_delay_length_minus1, 5);
211 WRITE_BITS (bw, hrd->cpb_removal_delay_length_minus1, 5);
212 WRITE_BITS (bw, hrd->dpb_output_delay_length_minus1, 5);
213 WRITE_BITS (bw, hrd->time_offset_length, 5);
219 GST_WARNING ("error to write \"HRD Parameters\"");
225 #define EXTENDED_SAR 255
228 _h264_bit_writer_vui_parameters (const GstH264SPS * sps, GstBitWriter * bw,
231 gboolean have_space = TRUE;
232 const GstH264VUIParams *vui = &sps->vui_parameters;
234 GST_DEBUG ("writing \"VUI Parameters\"");
236 WRITE_BITS (bw, vui->aspect_ratio_info_present_flag, 1);
237 if (vui->aspect_ratio_info_present_flag) {
238 WRITE_BITS (bw, vui->aspect_ratio_idc, 8);
239 if (vui->aspect_ratio_idc == EXTENDED_SAR) {
240 WRITE_BITS (bw, vui->sar_width, 16);
241 WRITE_BITS (bw, vui->sar_height, 16);
245 WRITE_BITS (bw, vui->overscan_info_present_flag, 1);
246 if (vui->overscan_info_present_flag)
247 WRITE_BITS (bw, vui->overscan_appropriate_flag, 1);
249 WRITE_BITS (bw, vui->video_signal_type_present_flag, 1);
250 if (vui->video_signal_type_present_flag) {
251 WRITE_BITS (bw, vui->video_format, 3);
252 WRITE_BITS (bw, vui->video_full_range_flag, 1);
253 WRITE_BITS (bw, vui->colour_description_present_flag, 1);
254 if (vui->colour_description_present_flag) {
255 WRITE_BITS (bw, vui->colour_primaries, 8);
256 WRITE_BITS (bw, vui->transfer_characteristics, 8);
257 WRITE_BITS (bw, vui->matrix_coefficients, 8);
261 WRITE_BITS (bw, vui->chroma_loc_info_present_flag, 1);
262 if (vui->chroma_loc_info_present_flag) {
263 WRITE_UE_MAX (bw, vui->chroma_sample_loc_type_top_field, 5);
264 WRITE_UE_MAX (bw, vui->chroma_sample_loc_type_bottom_field, 5);
267 WRITE_BITS (bw, vui->timing_info_present_flag, 1);
268 if (vui->timing_info_present_flag) {
269 WRITE_BITS (bw, vui->num_units_in_tick, 32);
270 if (vui->num_units_in_tick == 0)
271 GST_WARNING ("num_units_in_tick = 0 write to stream "
272 "(incompliant to H.264 E.2.1).");
274 WRITE_BITS (bw, vui->time_scale, 32);
275 if (vui->time_scale == 0)
276 GST_WARNING ("time_scale = 0 write to stream "
277 "(incompliant to H.264 E.2.1).");
279 WRITE_BITS (bw, vui->fixed_frame_rate_flag, 1);
282 WRITE_BITS (bw, vui->nal_hrd_parameters_present_flag, 1);
283 if (vui->nal_hrd_parameters_present_flag) {
284 if (!_h264_bit_writer_hrd_parameters (&vui->nal_hrd_parameters, bw,
289 WRITE_BITS (bw, vui->vcl_hrd_parameters_present_flag, 1);
290 if (vui->vcl_hrd_parameters_present_flag) {
291 if (!_h264_bit_writer_hrd_parameters (&vui->vcl_hrd_parameters, bw,
296 if (vui->nal_hrd_parameters_present_flag ||
297 vui->vcl_hrd_parameters_present_flag)
298 WRITE_BITS (bw, vui->low_delay_hrd_flag, 1);
300 WRITE_BITS (bw, vui->pic_struct_present_flag, 1);
301 WRITE_BITS (bw, vui->bitstream_restriction_flag, 1);
302 if (vui->bitstream_restriction_flag) {
303 WRITE_BITS (bw, vui->motion_vectors_over_pic_boundaries_flag, 1);
304 WRITE_UE (bw, vui->max_bytes_per_pic_denom);
305 WRITE_UE_MAX (bw, vui->max_bits_per_mb_denom, 16);
306 WRITE_UE_MAX (bw, vui->log2_max_mv_length_horizontal, 16);
307 WRITE_UE_MAX (bw, vui->log2_max_mv_length_vertical, 16);
308 WRITE_UE (bw, vui->num_reorder_frames);
309 WRITE_UE (bw, vui->max_dec_frame_buffering);
316 GST_WARNING ("error to write \"VUI Parameters\"");
323 _h264_bit_writer_sps (const GstH264SPS * sps, GstBitWriter * bw,
326 gboolean have_space = TRUE;
328 GST_DEBUG ("writing SPS");
330 WRITE_BITS (bw, sps->profile_idc, 8);
331 WRITE_BITS (bw, sps->constraint_set0_flag, 1);
332 WRITE_BITS (bw, sps->constraint_set1_flag, 1);
333 WRITE_BITS (bw, sps->constraint_set2_flag, 1);
334 WRITE_BITS (bw, sps->constraint_set3_flag, 1);
335 WRITE_BITS (bw, sps->constraint_set4_flag, 1);
336 WRITE_BITS (bw, sps->constraint_set5_flag, 1);
337 /* reserved_zero_2bits */
338 WRITE_BITS (bw, 0, 2);
340 WRITE_BITS (bw, sps->level_idc, 8);
342 WRITE_UE_MAX (bw, sps->id, GST_H264_MAX_SPS_COUNT - 1);
344 if (sps->profile_idc == 100 || sps->profile_idc == 110 ||
345 sps->profile_idc == 122 || sps->profile_idc == 244 ||
346 sps->profile_idc == 44 || sps->profile_idc == 83 ||
347 sps->profile_idc == 86 || sps->profile_idc == 118 ||
348 sps->profile_idc == 128 || sps->profile_idc == 138 ||
349 sps->profile_idc == 139 || sps->profile_idc == 134 ||
350 sps->profile_idc == 135) {
351 WRITE_UE_MAX (bw, sps->chroma_format_idc, 3);
352 if (sps->chroma_format_idc == 3)
353 WRITE_BITS (bw, sps->separate_colour_plane_flag, 1);
355 WRITE_UE_MAX (bw, sps->bit_depth_luma_minus8, 6);
356 WRITE_UE_MAX (bw, sps->bit_depth_chroma_minus8, 6);
357 WRITE_BITS (bw, sps->qpprime_y_zero_transform_bypass_flag, 1);
359 WRITE_BITS (bw, sps->scaling_matrix_present_flag, 1);
360 if (sps->scaling_matrix_present_flag) {
363 n_lists = (sps->chroma_format_idc != 3) ? 8 : 12;
364 if (!_h264_bit_writer_scaling_list (bw, &have_space,
365 sps->scaling_lists_4x4, sps->scaling_lists_8x8,
366 default_4x4_inter, default_4x4_intra,
367 default_8x8_inter, default_8x8_intra, n_lists))
372 WRITE_UE_MAX (bw, sps->log2_max_frame_num_minus4, 12);
374 WRITE_UE_MAX (bw, sps->pic_order_cnt_type, 2);
375 if (sps->pic_order_cnt_type == 0) {
376 WRITE_UE_MAX (bw, sps->log2_max_pic_order_cnt_lsb_minus4, 12);
377 } else if (sps->pic_order_cnt_type == 1) {
380 WRITE_BITS (bw, sps->delta_pic_order_always_zero_flag, 1);
381 WRITE_SE (bw, sps->offset_for_non_ref_pic);
382 WRITE_SE (bw, sps->offset_for_top_to_bottom_field);
383 WRITE_UE_MAX (bw, sps->num_ref_frames_in_pic_order_cnt_cycle, 255);
385 for (i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; i++)
386 WRITE_SE (bw, sps->offset_for_ref_frame[i]);
389 WRITE_UE (bw, sps->num_ref_frames);
390 WRITE_BITS (bw, sps->gaps_in_frame_num_value_allowed_flag, 1);
391 WRITE_UE (bw, sps->pic_width_in_mbs_minus1);
392 WRITE_UE (bw, sps->pic_height_in_map_units_minus1);
393 WRITE_BITS (bw, sps->frame_mbs_only_flag, 1);
395 if (!sps->frame_mbs_only_flag)
396 WRITE_BITS (bw, sps->mb_adaptive_frame_field_flag, 1);
398 WRITE_BITS (bw, sps->direct_8x8_inference_flag, 1);
399 WRITE_BITS (bw, sps->frame_cropping_flag, 1);
400 if (sps->frame_cropping_flag) {
401 WRITE_UE (bw, sps->frame_crop_left_offset);
402 WRITE_UE (bw, sps->frame_crop_right_offset);
403 WRITE_UE (bw, sps->frame_crop_top_offset);
404 WRITE_UE (bw, sps->frame_crop_bottom_offset);
407 WRITE_BITS (bw, sps->vui_parameters_present_flag, 1);
408 if (sps->vui_parameters_present_flag)
409 if (!_h264_bit_writer_vui_parameters (sps, bw, &have_space))
416 GST_WARNING ("error to write sps");
423 * gst_h264_bit_writer_sps:
424 * @sps: the sps of #GstH264SPS to write
425 * @start_code: whether adding the nal start code
426 * @data: (out): the bit stream generated by the sps
427 * @size: (inout): the size in bits of the output
429 * Generating the according h264 bit stream by providing the sps.
431 * Returns: a #GstH264BitWriterResult
435 GstH264BitWriterResult
436 gst_h264_bit_writer_sps (const GstH264SPS * sps, gboolean start_code,
437 guint8 * data, gsize * size)
439 gboolean have_space = TRUE;
442 g_return_val_if_fail (sps != NULL, GST_H264_BIT_WRITER_ERROR);
443 g_return_val_if_fail (data != NULL, GST_H264_BIT_WRITER_ERROR);
444 g_return_val_if_fail (size != NULL, GST_H264_BIT_WRITER_ERROR);
445 g_return_val_if_fail (*size > 0, GST_H264_BIT_WRITER_ERROR);
447 gst_bit_writer_init_with_data (&bw, data, *size, FALSE);
450 WRITE_BITS (&bw, 0x00000001, 32);
453 /* forbidden_zero_bit */
454 WRITE_BITS (&bw, 0, 1);
456 WRITE_BITS (&bw, 1, 2);
458 WRITE_BITS (&bw, GST_H264_NAL_SPS, 5);
460 if (!_h264_bit_writer_sps (sps, &bw, &have_space))
464 WRITE_BITS (&bw, 1, 1);
465 if (!gst_bit_writer_align_bytes (&bw, 0)) {
470 *size = gst_bit_writer_get_size (&bw);
471 gst_bit_writer_reset (&bw);
473 return GST_H264_BIT_WRITER_OK;
476 gst_bit_writer_reset (&bw);
479 return have_space ? GST_H264_BIT_WRITER_INVALID_DATA :
480 GST_H264_BIT_WRITER_NO_MORE_SPACE;
484 _h264_bit_writer_pps (const GstH264PPS * pps, GstBitWriter * bw,
487 gboolean have_space = TRUE;
490 GST_DEBUG ("writing SPS");
492 qp_bd_offset = 6 * (pps->sequence->bit_depth_luma_minus8 +
493 pps->sequence->separate_colour_plane_flag);
495 WRITE_UE_MAX (bw, pps->id, GST_H264_MAX_PPS_COUNT - 1);
496 WRITE_UE_MAX (bw, pps->sequence->id, GST_H264_MAX_SPS_COUNT - 1);
498 WRITE_BITS (bw, pps->entropy_coding_mode_flag, 1);
499 WRITE_BITS (bw, pps->pic_order_present_flag, 1);
501 WRITE_UE_MAX (bw, pps->num_slice_groups_minus1, 7);
502 if (pps->num_slice_groups_minus1 > 0) {
503 WRITE_UE_MAX (bw, pps->slice_group_map_type, 6);
505 if (pps->slice_group_map_type == 0) {
508 for (i = 0; i <= pps->num_slice_groups_minus1; i++)
509 WRITE_UE (bw, pps->run_length_minus1[i]);
510 } else if (pps->slice_group_map_type == 2) {
513 for (i = 0; i < pps->num_slice_groups_minus1; i++) {
514 WRITE_UE (bw, pps->top_left[i]);
515 WRITE_UE (bw, pps->bottom_right[i]);
517 } else if (pps->slice_group_map_type >= 3 && pps->slice_group_map_type <= 5) {
518 WRITE_BITS (bw, pps->slice_group_change_direction_flag, 1);
519 WRITE_UE (bw, pps->slice_group_change_rate_minus1);
520 } else if (pps->slice_group_map_type == 6) {
524 WRITE_UE (bw, pps->pic_size_in_map_units_minus1);
525 bits = g_bit_storage (pps->num_slice_groups_minus1);
527 g_assert (pps->slice_group_id);
528 for (i = 0; i <= pps->pic_size_in_map_units_minus1; i++)
529 WRITE_BITS (bw, pps->slice_group_id[i], bits);
533 WRITE_UE_MAX (bw, pps->num_ref_idx_l0_active_minus1, 31);
534 WRITE_UE_MAX (bw, pps->num_ref_idx_l1_active_minus1, 31);
535 WRITE_BITS (bw, pps->weighted_pred_flag, 1);
536 WRITE_BITS (bw, pps->weighted_bipred_idc, 2);
537 WRITE_SE_RANGE (bw, pps->pic_init_qp_minus26, -(26 + qp_bd_offset), 25);
538 WRITE_SE_RANGE (bw, pps->pic_init_qs_minus26, -26, 25);
539 WRITE_SE_RANGE (bw, pps->chroma_qp_index_offset, -12, 12);
541 WRITE_BITS (bw, pps->deblocking_filter_control_present_flag, 1);
542 WRITE_BITS (bw, pps->constrained_intra_pred_flag, 1);
543 WRITE_BITS (bw, pps->redundant_pic_cnt_present_flag, 1);
545 /* A.2.1 Baseline profile, A.2.2 Main profile and
546 A.2.3 Extended profile:
547 The syntax elements transform_8x8_mode_flag,
548 pic_scaling_matrix_present_flag, second_chroma_qp_index_offset
549 shall not be present in picture parameter sets. */
550 if (pps->sequence->profile_idc == 66 ||
551 pps->sequence->profile_idc == 77 || pps->sequence->profile_idc == 88)
554 WRITE_BITS (bw, pps->transform_8x8_mode_flag, 1);
556 WRITE_BITS (bw, pps->pic_scaling_matrix_present_flag, 1);
558 if (pps->pic_scaling_matrix_present_flag) {
561 n_lists = 6 + ((pps->sequence->chroma_format_idc != 3) ? 2 : 6) *
562 pps->transform_8x8_mode_flag;
564 if (pps->sequence->scaling_matrix_present_flag) {
565 if (!_h264_bit_writer_scaling_list (bw, &have_space,
566 pps->scaling_lists_4x4, pps->scaling_lists_8x8,
567 pps->sequence->scaling_lists_4x4[3],
568 pps->sequence->scaling_lists_4x4[0],
569 pps->sequence->scaling_lists_8x8[3],
570 pps->sequence->scaling_lists_8x8[0], n_lists))
573 if (!_h264_bit_writer_scaling_list (bw, &have_space,
574 pps->scaling_lists_4x4, pps->scaling_lists_8x8,
575 default_4x4_inter, default_4x4_intra,
576 default_8x8_inter, default_8x8_intra, n_lists))
581 WRITE_SE_RANGE (bw, ((gint) pps->second_chroma_qp_index_offset), -12, 12);
587 GST_WARNING ("error to write pps");
594 * gst_h264_bit_writer_pps:
595 * @pps: the pps of #GstH264PPS to write
596 * @start_code: whether adding the nal start code
597 * @data: (out): the bit stream generated by the pps
598 * @size: (inout): the size in bits of the output
600 * Generating the according h264 bit stream by providing the pps.
602 * Returns: a #GstH264BitWriterResult
606 GstH264BitWriterResult
607 gst_h264_bit_writer_pps (const GstH264PPS * pps, gboolean start_code,
608 guint8 * data, gsize * size)
610 gboolean have_space = TRUE;
613 g_return_val_if_fail (pps != NULL, GST_H264_BIT_WRITER_ERROR);
614 g_return_val_if_fail (pps->sequence != NULL, GST_H264_BIT_WRITER_ERROR);
615 g_return_val_if_fail (data != NULL, GST_H264_BIT_WRITER_ERROR);
616 g_return_val_if_fail (size != NULL, GST_H264_BIT_WRITER_ERROR);
617 g_return_val_if_fail (*size > 0, GST_H264_BIT_WRITER_ERROR);
619 gst_bit_writer_init_with_data (&bw, data, *size, FALSE);
622 WRITE_BITS (&bw, 0x00000001, 32);
625 /* forbidden_zero_bit */
626 WRITE_BITS (&bw, 0, 1);
628 WRITE_BITS (&bw, 1, 2);
630 WRITE_BITS (&bw, GST_H264_NAL_PPS, 5);
632 if (!_h264_bit_writer_pps (pps, &bw, &have_space))
636 WRITE_BITS (&bw, 1, 1);
637 if (!gst_bit_writer_align_bytes (&bw, 0)) {
642 *size = gst_bit_writer_get_size (&bw);
643 gst_bit_writer_reset (&bw);
644 return GST_H264_BIT_WRITER_OK;
647 gst_bit_writer_reset (&bw);
650 return have_space ? GST_H264_BIT_WRITER_INVALID_DATA :
651 GST_H264_BIT_WRITER_NO_MORE_SPACE;
655 _h264_slice_bit_writer_ref_pic_list_modification_1 (const GstH264SliceHdr *
656 slice, guint list, gboolean is_mvc, GstBitWriter * bw, gboolean * space)
658 gboolean have_space = TRUE;
659 const GstH264RefPicListModification *entries;
660 guint8 ref_pic_list_modification_flag = 0;
664 entries = slice->ref_pic_list_modification_l0;
665 ref_pic_list_modification_flag = slice->ref_pic_list_modification_flag_l0;
667 entries = slice->ref_pic_list_modification_l1;
668 ref_pic_list_modification_flag = slice->ref_pic_list_modification_flag_l1;
671 WRITE_BITS (bw, ref_pic_list_modification_flag, 1);
672 if (ref_pic_list_modification_flag) {
677 WRITE_UE (bw, entries[i].modification_of_pic_nums_idc);
678 if (entries[i].modification_of_pic_nums_idc == 0 ||
679 entries[i].modification_of_pic_nums_idc == 1) {
680 WRITE_UE_MAX (bw, entries[i].value.abs_diff_pic_num_minus1,
681 slice->max_pic_num - 1);
682 } else if (entries[i].modification_of_pic_nums_idc == 2) {
683 WRITE_UE (bw, entries[i].value.long_term_pic_num);
684 } else if (is_mvc && (entries[i].modification_of_pic_nums_idc == 4 ||
685 entries[i].modification_of_pic_nums_idc == 5)) {
686 WRITE_UE (bw, entries[i].value.abs_diff_view_idx_minus1);
688 } while (entries[i++].modification_of_pic_nums_idc != 3);
695 GST_WARNING ("error to write \"Reference picture list %u modification\"",
703 _h264_slice_bit_writer_ref_pic_list_modification (const GstH264SliceHdr *
704 slice, gboolean is_mvc, GstBitWriter * bw, gboolean * space)
706 if (!GST_H264_IS_I_SLICE (slice) && !GST_H264_IS_SI_SLICE (slice)) {
707 if (!_h264_slice_bit_writer_ref_pic_list_modification_1 (slice, 0,
712 if (GST_H264_IS_B_SLICE (slice)) {
713 if (!_h264_slice_bit_writer_ref_pic_list_modification_1 (slice, 1,
723 _h264_slice_bit_writer_pred_weight_table (const GstH264SliceHdr * slice,
724 guint8 chroma_array_type, GstBitWriter * bw, gboolean * space)
726 gboolean have_space = TRUE;
727 const GstH264PredWeightTable *p;
729 gint16 default_luma_weight, default_chroma_weight;
731 GST_DEBUG ("writing \"Prediction weight table\"");
733 p = &slice->pred_weight_table;
734 default_luma_weight = 1 << p->luma_log2_weight_denom;
735 default_chroma_weight = 1 << p->chroma_log2_weight_denom;
737 WRITE_UE_MAX (bw, p->luma_log2_weight_denom, 7);
739 if (chroma_array_type != 0)
740 WRITE_UE_MAX (bw, p->chroma_log2_weight_denom, 7);
742 for (i = 0; i <= slice->num_ref_idx_l0_active_minus1; i++) {
743 guint8 luma_weight_l0_flag = 0;
745 if (p->luma_weight_l0[i] != default_luma_weight ||
746 p->luma_offset_l0[i] != 0)
747 luma_weight_l0_flag = 1;
749 WRITE_BITS (bw, luma_weight_l0_flag, 1);
750 if (luma_weight_l0_flag) {
751 WRITE_SE_RANGE (bw, p->luma_weight_l0[i], -128, 127);
752 WRITE_SE_RANGE (bw, p->luma_offset_l0[i], -128, 127);
754 if (chroma_array_type != 0) {
755 guint8 chroma_weight_l0_flag = 0;
758 for (j = 0; j < 2; j++) {
759 if (p->chroma_weight_l0[i][j] != default_chroma_weight ||
760 p->chroma_offset_l0[i][j] != 0)
761 chroma_weight_l0_flag = 1;
764 WRITE_BITS (bw, chroma_weight_l0_flag, 1);
765 if (chroma_weight_l0_flag) {
766 for (j = 0; j < 2; j++) {
767 WRITE_SE_RANGE (bw, p->chroma_weight_l0[i][j], -128, 127);
768 WRITE_SE_RANGE (bw, p->chroma_offset_l0[i][j], -128, 127);
774 if (GST_H264_IS_B_SLICE (slice)) {
775 for (i = 0; i <= slice->num_ref_idx_l1_active_minus1; i++) {
776 guint8 luma_weight_l1_flag = 0;
778 if (p->luma_weight_l1[i] != default_luma_weight ||
779 p->luma_offset_l1[i] != 0)
780 luma_weight_l1_flag = 1;
782 WRITE_BITS (bw, luma_weight_l1_flag, 1);
783 if (luma_weight_l1_flag) {
784 WRITE_SE_RANGE (bw, p->luma_weight_l1[i], -128, 127);
785 WRITE_SE_RANGE (bw, p->luma_offset_l1[i], -128, 127);
787 if (chroma_array_type != 0) {
788 guint8 chroma_weight_l1_flag = 0;
791 for (j = 0; j < 2; j++) {
792 if (p->chroma_weight_l1[i][j] != default_chroma_weight ||
793 p->chroma_offset_l1[i][j] != 0)
794 chroma_weight_l1_flag = 1;
797 WRITE_BITS (bw, chroma_weight_l1_flag, 1);
798 if (chroma_weight_l1_flag) {
799 for (j = 0; j < 2; j++) {
800 WRITE_SE_RANGE (bw, p->chroma_weight_l1[i][j], -128, 127);
801 WRITE_SE_RANGE (bw, p->chroma_offset_l1[i][j], -128, 127);
812 GST_WARNING ("error to write \"Prediction weight table\"");
819 _h264_bit_writer_slice_dec_ref_pic_marking (const GstH264SliceHdr * slice,
820 guint32 nal_type, GstBitWriter * bw, gboolean * space)
822 gboolean have_space = TRUE;
824 GST_DEBUG ("writing \"Dec Ref Pic Marking\"");
826 if (nal_type == GST_H264_NAL_SLICE_IDR) {
827 WRITE_BITS (bw, slice->dec_ref_pic_marking.no_output_of_prior_pics_flag, 1);
828 WRITE_BITS (bw, slice->dec_ref_pic_marking.long_term_reference_flag, 1);
831 slice->dec_ref_pic_marking.adaptive_ref_pic_marking_mode_flag, 1);
833 if (slice->dec_ref_pic_marking.adaptive_ref_pic_marking_mode_flag) {
834 const GstH264RefPicMarking *refpicmarking;
837 for (i = 0; i < slice->dec_ref_pic_marking.n_ref_pic_marking; i++) {
838 refpicmarking = &slice->dec_ref_pic_marking.ref_pic_marking[i];
841 refpicmarking->memory_management_control_operation, 6);
843 if (refpicmarking->memory_management_control_operation == 0)
846 if (refpicmarking->memory_management_control_operation == 1
847 || refpicmarking->memory_management_control_operation == 3)
848 WRITE_UE (bw, refpicmarking->difference_of_pic_nums_minus1);
850 if (refpicmarking->memory_management_control_operation == 2)
851 WRITE_UE (bw, refpicmarking->long_term_pic_num);
853 if (refpicmarking->memory_management_control_operation == 3
854 || refpicmarking->memory_management_control_operation == 6)
855 WRITE_UE (bw, refpicmarking->long_term_frame_idx);
857 if (refpicmarking->memory_management_control_operation == 4)
858 WRITE_UE (bw, refpicmarking->max_long_term_frame_idx_plus1);
867 GST_WARNING ("error to write \"Dec Ref Pic Marking\"");
874 _h264_bit_writer_slice_hdr (const GstH264SliceHdr * slice, guint32 nal_type,
875 guint32 ext_type, gboolean is_ref, GstBitWriter * bw, gboolean * space)
877 gboolean have_space = TRUE;
879 GST_DEBUG ("writing slice header");
881 WRITE_UE (bw, slice->first_mb_in_slice);
882 WRITE_UE (bw, slice->type);
884 WRITE_UE_MAX (bw, slice->pps->id, GST_H264_MAX_PPS_COUNT - 1);
886 if (slice->pps->sequence->separate_colour_plane_flag)
887 WRITE_BITS (bw, slice->colour_plane_id, 2);
889 WRITE_BITS (bw, slice->frame_num,
890 slice->pps->sequence->log2_max_frame_num_minus4 + 4);
892 if (!slice->pps->sequence->frame_mbs_only_flag) {
893 WRITE_BITS (bw, slice->field_pic_flag, 1);
894 if (slice->field_pic_flag)
895 WRITE_BITS (bw, slice->bottom_field_flag, 1);
898 if (nal_type == GST_H264_NAL_SLICE_IDR)
899 WRITE_UE_MAX (bw, slice->idr_pic_id, G_MAXUINT16);
901 if (slice->pps->sequence->pic_order_cnt_type == 0) {
902 WRITE_BITS (bw, slice->pic_order_cnt_lsb,
903 slice->pps->sequence->log2_max_pic_order_cnt_lsb_minus4 + 4);
905 if (slice->pps->pic_order_present_flag && !slice->field_pic_flag)
906 WRITE_SE (bw, slice->delta_pic_order_cnt_bottom);
909 if (slice->pps->sequence->pic_order_cnt_type == 1
910 && !slice->pps->sequence->delta_pic_order_always_zero_flag) {
911 WRITE_SE (bw, slice->delta_pic_order_cnt[0]);
912 if (slice->pps->pic_order_present_flag && !slice->field_pic_flag)
913 WRITE_SE (bw, slice->delta_pic_order_cnt[1]);
916 if (slice->pps->redundant_pic_cnt_present_flag)
917 WRITE_UE_MAX (bw, slice->redundant_pic_cnt, G_MAXINT8);
919 if (GST_H264_IS_B_SLICE (slice))
920 WRITE_BITS (bw, slice->direct_spatial_mv_pred_flag, 1);
922 if (GST_H264_IS_P_SLICE (slice) || GST_H264_IS_SP_SLICE (slice) ||
923 GST_H264_IS_B_SLICE (slice)) {
924 WRITE_BITS (bw, slice->num_ref_idx_active_override_flag, 1);
925 if (slice->num_ref_idx_active_override_flag) {
926 WRITE_UE_MAX (bw, slice->num_ref_idx_l0_active_minus1, 31);
928 if (GST_H264_IS_B_SLICE (slice))
929 WRITE_UE_MAX (bw, slice->num_ref_idx_l1_active_minus1, 31);
933 if (!_h264_slice_bit_writer_ref_pic_list_modification (slice,
934 ext_type == GST_H264_NAL_EXTENSION_MVC, bw, &have_space))
937 if ((slice->pps->weighted_pred_flag && (GST_H264_IS_P_SLICE (slice)
938 || GST_H264_IS_SP_SLICE (slice)))
939 || (slice->pps->weighted_bipred_idc == 1 && GST_H264_IS_B_SLICE (slice))) {
940 if (!_h264_slice_bit_writer_pred_weight_table (slice,
941 slice->pps->sequence->chroma_array_type, bw, &have_space))
946 if (!_h264_bit_writer_slice_dec_ref_pic_marking (slice, nal_type, bw,
951 if (slice->pps->entropy_coding_mode_flag && !GST_H264_IS_I_SLICE (slice) &&
952 !GST_H264_IS_SI_SLICE (slice))
953 WRITE_UE_MAX (bw, slice->cabac_init_idc, 2);
955 WRITE_SE_RANGE (bw, slice->slice_qp_delta, -87, 77);
957 if (GST_H264_IS_SP_SLICE (slice) || GST_H264_IS_SI_SLICE (slice)) {
958 if (GST_H264_IS_SP_SLICE (slice))
959 WRITE_BITS (bw, slice->sp_for_switch_flag, 1);
961 WRITE_SE_RANGE (bw, slice->slice_qs_delta, -51, 51);
964 if (slice->pps->deblocking_filter_control_present_flag) {
965 WRITE_UE_MAX (bw, slice->disable_deblocking_filter_idc, 2);
966 if (slice->disable_deblocking_filter_idc != 1) {
967 WRITE_SE_RANGE (bw, slice->slice_alpha_c0_offset_div2, -6, 6);
968 WRITE_SE_RANGE (bw, slice->slice_beta_offset_div2, -6, 6);
976 GST_WARNING ("error to write slice header");
983 * gst_h264_bit_writer_slice_hdr:
984 * @slice: the slice header of #GstH264SliceHdr to write
985 * @start_code: whether adding the nal start code
986 * @nal_type: the slice's nal type of #GstH264NalUnitType
987 * @is_ref: whether the slice is a reference
988 * @data: (out): the bit stream generated by the slice header
989 * @size: (inout): the size in bits of the output
991 * Generating the according h264 bit stream by providing the slice header.
993 * Returns: a #GstH264BitWriterResult
997 GstH264BitWriterResult
998 gst_h264_bit_writer_slice_hdr (const GstH264SliceHdr * slice,
999 gboolean start_code, GstH264NalUnitType nal_type, gboolean is_ref,
1000 guint8 * data, gsize * size)
1002 gboolean have_space = TRUE;
1005 g_return_val_if_fail (slice != NULL, GST_H264_BIT_WRITER_ERROR);
1006 g_return_val_if_fail (slice->pps != NULL, GST_H264_BIT_WRITER_ERROR);
1007 g_return_val_if_fail (slice->pps->sequence != NULL,
1008 GST_H264_BIT_WRITER_ERROR);
1009 g_return_val_if_fail (nal_type >= GST_H264_NAL_SLICE
1010 && nal_type <= GST_H264_NAL_SLICE_IDR, GST_H264_BIT_WRITER_ERROR);
1011 g_return_val_if_fail (data != NULL, GST_H264_BIT_WRITER_ERROR);
1012 g_return_val_if_fail (size != NULL, GST_H264_BIT_WRITER_ERROR);
1013 g_return_val_if_fail (*size > 0, GST_H264_BIT_WRITER_ERROR);
1015 if (nal_type == GST_H264_NAL_SLICE_IDR)
1016 g_return_val_if_fail (is_ref, GST_H264_BIT_WRITER_ERROR);
1018 gst_bit_writer_init_with_data (&bw, data, *size, FALSE);
1021 WRITE_BITS (&bw, 0x00000001, 32);
1024 /* forbidden_zero_bit */
1025 WRITE_BITS (&bw, 0, 1);
1026 /* nal_ref_idc, zero for non-reference picture */
1027 WRITE_BITS (&bw, is_ref, 2);
1029 WRITE_BITS (&bw, nal_type, 5);
1031 if (!_h264_bit_writer_slice_hdr (slice, nal_type,
1032 GST_H264_NAL_EXTENSION_NONE, is_ref, &bw, &have_space))
1035 /* We do not add trailing bits here, the slice data should follow it. */
1037 *size = gst_bit_writer_get_size (&bw);
1038 gst_bit_writer_reset (&bw);
1039 return GST_H264_BIT_WRITER_OK;
1042 gst_bit_writer_reset (&bw);
1045 return have_space ? GST_H264_BIT_WRITER_INVALID_DATA :
1046 GST_H264_BIT_WRITER_NO_MORE_SPACE;
1050 _h264_bit_writer_sei_registered_user_data (const GstH264RegisteredUserData *
1051 rud, GstBitWriter * bw, gboolean * space)
1053 gboolean have_space = TRUE;
1055 GST_DEBUG ("Writing \"Registered user data\"");
1057 WRITE_BITS (bw, rud->country_code, 8);
1058 if (rud->country_code == 0xff)
1059 WRITE_BITS (bw, rud->country_code_extension, 8);
1061 WRITE_BYTES (bw, rud->data, rud->size);
1066 error:GST_WARNING ("Failed to write \"Registered user data\"");
1068 *space = have_space;
1073 _h264_bit_writer_sei_frame_packing (const GstH264FramePacking *
1074 frame_packing, GstBitWriter * bw, gboolean * space)
1076 gboolean have_space = TRUE;
1078 GST_DEBUG ("Writing \"Frame packing\"");
1080 WRITE_UE (bw, frame_packing->frame_packing_id);
1081 WRITE_BITS (bw, frame_packing->frame_packing_cancel_flag, 1);
1083 if (!frame_packing->frame_packing_cancel_flag) {
1084 WRITE_BITS (bw, frame_packing->frame_packing_type, 7);
1085 WRITE_BITS (bw, frame_packing->quincunx_sampling_flag, 1);
1086 WRITE_BITS (bw, frame_packing->content_interpretation_type, 6);
1087 WRITE_BITS (bw, frame_packing->spatial_flipping_flag, 1);
1088 WRITE_BITS (bw, frame_packing->frame0_flipped_flag, 1);
1089 WRITE_BITS (bw, frame_packing->field_views_flag, 1);
1090 WRITE_BITS (bw, frame_packing->current_frame_is_frame0_flag, 1);
1091 WRITE_BITS (bw, frame_packing->frame0_self_contained_flag, 1);
1092 WRITE_BITS (bw, frame_packing->frame1_self_contained_flag, 1);
1094 if (!frame_packing->quincunx_sampling_flag &&
1095 frame_packing->frame_packing_type !=
1096 GST_H264_FRAME_PACKING_TEMPORAL_INTERLEAVING) {
1097 WRITE_BITS (bw, frame_packing->frame0_grid_position_x, 4);
1098 WRITE_BITS (bw, frame_packing->frame0_grid_position_y, 4);
1099 WRITE_BITS (bw, frame_packing->frame1_grid_position_x, 4);
1100 WRITE_BITS (bw, frame_packing->frame1_grid_position_y, 4);
1103 /* frame_packing_arrangement_reserved_byte */
1104 WRITE_BITS (bw, 0, 8);
1105 WRITE_UE (bw, frame_packing->frame_packing_repetition_period);
1108 /* frame_packing_arrangement_extension_flag */
1109 WRITE_BITS (bw, 0, 1);
1115 GST_WARNING ("Failed to write \"Frame packing\"");
1117 *space = have_space;
1122 _h264_bit_writer_sei_mastering_display_colour_volume (const
1123 GstH264MasteringDisplayColourVolume * mdcv, GstBitWriter * bw,
1126 gboolean have_space = TRUE;
1129 GST_DEBUG ("Wrtiting \"Mastering display colour volume\"");
1131 for (i = 0; i < 3; i++) {
1132 WRITE_BITS (bw, mdcv->display_primaries_x[i], 16);
1133 WRITE_BITS (bw, mdcv->display_primaries_y[i], 16);
1136 WRITE_BITS (bw, mdcv->white_point_x, 16);
1137 WRITE_BITS (bw, mdcv->white_point_y, 16);
1138 WRITE_BITS (bw, mdcv->max_display_mastering_luminance, 32);
1139 WRITE_BITS (bw, mdcv->min_display_mastering_luminance, 32);
1145 GST_WARNING ("Failed to write \"Mastering display colour volume\"");
1147 *space = have_space;
1152 _h264_bit_writer_sei_content_light_level_info (const
1153 GstH264ContentLightLevel * cll, GstBitWriter * bw, gboolean * space)
1155 gboolean have_space = TRUE;
1157 GST_DEBUG ("Writing \"Content light level\"");
1159 WRITE_BITS (bw, cll->max_content_light_level, 16);
1160 WRITE_BITS (bw, cll->max_pic_average_light_level, 16);
1166 GST_WARNING ("Failed to write \"Content light level\"");
1168 *space = have_space;
1173 _h264_bit_writer_sei_pic_timing (const GstH264PicTiming * tim,
1174 GstBitWriter * bw, gboolean * space)
1176 gboolean have_space = TRUE;
1178 GST_DEBUG ("Writing \"Picture timing\"");
1180 if (tim->CpbDpbDelaysPresentFlag) {
1181 WRITE_BITS (bw, tim->cpb_removal_delay,
1182 tim->cpb_removal_delay_length_minus1 + 1);
1183 WRITE_BITS (bw, tim->dpb_output_delay,
1184 tim->dpb_output_delay_length_minus1 + 1);
1187 if (tim->pic_struct_present_flag) {
1188 const guint8 num_clock_ts_table[9] = {
1189 1, 1, 1, 2, 2, 3, 3, 2, 3
1191 guint8 num_clock_num_ts;
1194 WRITE_BITS (bw, tim->pic_struct, 4);
1196 num_clock_num_ts = num_clock_ts_table[tim->pic_struct];
1197 for (i = 0; i < num_clock_num_ts; i++) {
1198 WRITE_BITS (bw, tim->clock_timestamp_flag[i], 1);
1199 if (tim->clock_timestamp_flag[i]) {
1200 const GstH264ClockTimestamp *timestamp = &tim->clock_timestamp[i];
1202 WRITE_BITS (bw, timestamp->ct_type, 2);
1203 WRITE_BITS (bw, timestamp->nuit_field_based_flag, 1);
1204 WRITE_BITS (bw, timestamp->counting_type, 5);
1205 WRITE_BITS (bw, timestamp->full_timestamp_flag, 1);
1206 WRITE_BITS (bw, timestamp->discontinuity_flag, 1);
1207 WRITE_BITS (bw, timestamp->cnt_dropped_flag, 1);
1208 WRITE_BITS (bw, timestamp->n_frames, 8);
1210 if (timestamp->full_timestamp_flag) {
1211 if (!timestamp->seconds_flag || !timestamp->minutes_flag
1212 || !timestamp->hours_flag)
1215 WRITE_BITS (bw, timestamp->seconds_value, 6);
1216 WRITE_BITS (bw, timestamp->minutes_value, 6);
1217 WRITE_BITS (bw, timestamp->hours_value, 5);
1219 WRITE_BITS (bw, timestamp->seconds_flag, 1);
1220 if (timestamp->seconds_flag) {
1221 WRITE_BITS (bw, timestamp->seconds_value, 6);
1222 WRITE_BITS (bw, timestamp->minutes_flag, 1);
1223 if (timestamp->minutes_flag) {
1224 WRITE_BITS (bw, timestamp->minutes_value, 6);
1225 WRITE_BITS (bw, timestamp->hours_flag, 1);
1226 if (timestamp->hours_flag)
1227 WRITE_BITS (bw, timestamp->hours_value, 5);
1232 if (tim->time_offset_length > 0) {
1233 WRITE_BITS (bw, timestamp->time_offset, tim->time_offset_length);
1243 GST_WARNING ("Failed to write \"Picture timing\"");
1245 *space = have_space;
1250 _h264_bit_writer_sei_buffering_period (const GstH264BufferingPeriod * per,
1251 GstBitWriter * bw, gboolean * space)
1253 gboolean have_space = TRUE;
1255 GST_DEBUG ("Writing \"Buffering period\"");
1260 WRITE_UE_MAX (bw, per->sps->id, GST_H264_MAX_SPS_COUNT - 1);
1262 if (per->sps->vui_parameters_present_flag) {
1263 GstH264VUIParams *vui = &per->sps->vui_parameters;
1265 if (vui->nal_hrd_parameters_present_flag) {
1266 GstH264HRDParams *hrd = &vui->nal_hrd_parameters;
1267 const guint8 nbits = hrd->initial_cpb_removal_delay_length_minus1 + 1;
1268 guint8 sched_sel_idx;
1270 for (sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1;
1272 WRITE_BITS (bw, per->nal_initial_cpb_removal_delay[sched_sel_idx],
1275 per->nal_initial_cpb_removal_delay_offset[sched_sel_idx], nbits);
1279 if (vui->vcl_hrd_parameters_present_flag) {
1280 GstH264HRDParams *hrd = &vui->vcl_hrd_parameters;
1281 const guint8 nbits = hrd->initial_cpb_removal_delay_length_minus1 + 1;
1282 guint8 sched_sel_idx;
1284 for (sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1;
1286 WRITE_BITS (bw, per->vcl_initial_cpb_removal_delay[sched_sel_idx],
1289 per->vcl_initial_cpb_removal_delay_offset[sched_sel_idx], nbits);
1298 GST_WARNING ("Failed to write \"Buffering period\"");
1300 *space = have_space;
1305 _h264_bit_writer_sei_message (const GstH264SEIMessage * msg,
1306 GstBitWriter * bw, gboolean * space)
1308 gboolean have_space = TRUE;
1310 GST_DEBUG ("writing SEI message");
1312 switch (msg->payloadType) {
1313 case GST_H264_SEI_REGISTERED_USER_DATA:
1314 if (!_h264_bit_writer_sei_registered_user_data
1315 (&msg->payload.registered_user_data, bw, &have_space))
1318 case GST_H264_SEI_FRAME_PACKING:
1319 if (!_h264_bit_writer_sei_frame_packing
1320 (&msg->payload.frame_packing, bw, &have_space))
1323 case GST_H264_SEI_MASTERING_DISPLAY_COLOUR_VOLUME:
1324 if (!_h264_bit_writer_sei_mastering_display_colour_volume
1325 (&msg->payload.mastering_display_colour_volume, bw, &have_space))
1328 case GST_H264_SEI_CONTENT_LIGHT_LEVEL:
1329 if (!_h264_bit_writer_sei_content_light_level_info
1330 (&msg->payload.content_light_level, bw, &have_space))
1333 case GST_H264_SEI_PIC_TIMING:
1334 if (!_h264_bit_writer_sei_pic_timing (&msg->payload.pic_timing, bw,
1338 case GST_H264_SEI_BUF_PERIOD:
1339 if (!_h264_bit_writer_sei_buffering_period
1340 (&msg->payload.buffering_period, bw, &have_space))
1347 /* Add trailings. */
1348 WRITE_BITS (bw, 1, 1);
1349 gst_bit_writer_align_bytes_unchecked (bw, 0);
1356 GST_WARNING ("error to write SEI message");
1358 *space = have_space;
1363 * gst_h264_bit_writer_sei:
1364 * @sei_messages: An array of #GstH264SEIMessage to write
1365 * @start_code: whether adding the nal start code
1366 * @data: (out): the bit stream generated by the sei messages
1367 * @size: (inout): the size in bits of the output
1369 * Generating the according h264 bit stream by providing sei messages.
1371 * Returns: a #GstH264BitWriterResult
1375 GstH264BitWriterResult
1376 gst_h264_bit_writer_sei (GArray * sei_messages, gboolean start_code,
1377 guint8 * data, gsize * size)
1379 gboolean have_space = TRUE;
1381 GstBitWriter bw_msg;
1382 GstH264SEIMessage *sei;
1383 gboolean have_written_data = FALSE;
1386 g_return_val_if_fail (sei_messages != NULL, GST_H264_BIT_WRITER_ERROR);
1387 g_return_val_if_fail (data != NULL, GST_H264_BIT_WRITER_ERROR);
1388 g_return_val_if_fail (size != NULL, GST_H264_BIT_WRITER_ERROR);
1389 g_return_val_if_fail (*size > 0, GST_H264_BIT_WRITER_ERROR);
1391 gst_bit_writer_init_with_data (&bw, data, *size, FALSE);
1394 WRITE_BITS (&bw, 0x00000001, 32);
1397 /* forbidden_zero_bit */
1398 WRITE_BITS (&bw, 0, 1);
1399 /* nal_ref_idc, zero for sei nalu */
1400 WRITE_BITS (&bw, 0, 2);
1402 WRITE_BITS (&bw, GST_H264_NAL_SEI, 5);
1404 for (i = 0; i < sei_messages->len; i++) {
1405 guint32 payload_size_data;
1406 guint32 payload_type_data;
1409 gst_bit_writer_init (&bw_msg);
1411 sei = &g_array_index (sei_messages, GstH264SEIMessage, i);
1412 if (!_h264_bit_writer_sei_message (sei, &bw_msg, &have_space))
1415 if (gst_bit_writer_get_size (&bw_msg) == 0) {
1416 GST_FIXME ("Unsupported SEI type %d", sei->payloadType);
1420 have_written_data = TRUE;
1422 g_assert (gst_bit_writer_get_size (&bw_msg) % 8 == 0);
1423 payload_size_data = gst_bit_writer_get_size (&bw_msg) / 8;
1424 payload_type_data = sei->payloadType;
1426 /* write payload type bytes */
1427 while (payload_type_data >= 0xff) {
1428 WRITE_BITS (&bw, 0xff, 8);
1429 payload_type_data -= 0xff;
1431 WRITE_BITS (&bw, payload_type_data, 8);
1433 /* write payload size bytes */
1434 sz = payload_size_data;
1435 while (sz >= 0xff) {
1436 WRITE_BITS (&bw, 0xff, 8);
1439 WRITE_BITS (&bw, sz, 8);
1441 if (payload_size_data > 0)
1442 WRITE_BYTES (&bw, gst_bit_writer_get_data (&bw_msg), payload_size_data);
1444 gst_bit_writer_reset (&bw_msg);
1447 if (!have_written_data) {
1448 GST_WARNING ("No written sei data");
1452 /* Add trailings. */
1453 WRITE_BITS (&bw, 1, 1);
1454 if (!gst_bit_writer_align_bytes (&bw, 0)) {
1459 *size = gst_bit_writer_get_size (&bw);
1460 gst_bit_writer_reset (&bw);
1461 return GST_H264_BIT_WRITER_OK;
1464 gst_bit_writer_reset (&bw);
1467 return have_space ? GST_H264_BIT_WRITER_INVALID_DATA :
1468 GST_H264_BIT_WRITER_NO_MORE_SPACE;
1472 * gst_h264_bit_writer_aud:
1473 * @primary_pic_type: indicate the possible slice types list just
1474 * as the H264 spec defines
1475 * @start_code: whether adding the nal start code
1476 * @data: (out): the bit stream generated by the aud
1477 * @size: (inout): the size in bits of the output
1479 * Generating the according h264 bit stream of an aud.
1481 * Returns: a #GstH264BitWriterResult
1485 GstH264BitWriterResult
1486 gst_h264_bit_writer_aud (guint8 primary_pic_type, gboolean start_code,
1487 guint8 * data, gsize * size)
1489 gboolean have_space = TRUE;
1492 g_return_val_if_fail (primary_pic_type >= 0
1493 && primary_pic_type <= 7, GST_H264_BIT_WRITER_ERROR);
1494 g_return_val_if_fail (data != NULL, GST_H264_BIT_WRITER_ERROR);
1495 g_return_val_if_fail (size != NULL, GST_H264_BIT_WRITER_ERROR);
1496 g_return_val_if_fail (*size > 0, GST_H264_BIT_WRITER_ERROR);
1498 gst_bit_writer_init_with_data (&bw, data, *size, FALSE);
1501 WRITE_BITS (&bw, 0x00000001, 32);
1504 /* forbidden_zero_bit */
1505 WRITE_BITS (&bw, 0, 1);
1507 WRITE_BITS (&bw, 0, 2);
1509 WRITE_BITS (&bw, GST_H264_NAL_AU_DELIMITER, 5);
1511 WRITE_BITS (&bw, primary_pic_type, 3);
1513 /* Add trailings. */
1514 WRITE_BITS (&bw, 1, 1);
1515 if (!gst_bit_writer_align_bytes (&bw, 0)) {
1519 *size = gst_bit_writer_get_size (&bw);
1520 gst_bit_writer_reset (&bw);
1522 return GST_H264_BIT_WRITER_OK;
1525 gst_bit_writer_reset (&bw);
1528 return have_space ? GST_H264_BIT_WRITER_INVALID_DATA :
1529 GST_H264_BIT_WRITER_NO_MORE_SPACE;
1533 * gst_h264_bit_writer_convert_to_nal:
1534 * @nal_prefix_size: the size in bytes for the prefix of a nal, may
1536 * @packetized: whether to write the bit stream in packetized format,
1537 * which does not have the start code but has a @nal_prefix_size bytes'
1538 * size prepending to the real nal data
1539 * @has_startcode: whether the input already has a start code
1540 * @add_trailings: whether to add rbsp trailing bits to make the output
1542 * @raw_data: the input bit stream
1543 * @raw_size: the size in bits of the input bit stream
1544 * @nal_data: (out): the output bit stream converted to a real nal
1545 * @nal_size: (inout): the size in bytes of the output
1547 * Converting a bit stream into a real nal packet. If the bit stream already
1548 * has a start code, it will be replaced by the new one specified by the
1549 * @nal_prefix_size and @packetized. It is assured that the output aligns to
1550 * the byte and the all the emulations are inserted.
1552 * Returns: a #GstH264BitWriterResult
1556 GstH264BitWriterResult
1557 gst_h264_bit_writer_convert_to_nal (guint nal_prefix_size, gboolean packetized,
1558 gboolean has_startcode, gboolean add_trailings, const guint8 * raw_data,
1559 gsize raw_size, guint8 * nal_data, guint32 * nal_size)
1564 gboolean need_more_space = FALSE;
1566 g_return_val_if_fail (
1567 (packetized && nal_prefix_size > 1 && nal_prefix_size < 5) ||
1568 (!packetized && (nal_prefix_size == 3 || nal_prefix_size == 4)),
1569 GST_H264_BIT_WRITER_ERROR);
1570 g_return_val_if_fail (raw_data != NULL, GST_H264_BIT_WRITER_ERROR);
1571 g_return_val_if_fail (raw_size > 0, GST_H264_BIT_WRITER_ERROR);
1572 g_return_val_if_fail (nal_data != NULL, GST_H264_BIT_WRITER_ERROR);
1573 g_return_val_if_fail (nal_size != NULL, GST_H264_BIT_WRITER_ERROR);
1574 g_return_val_if_fail (*nal_size > 0, GST_H264_BIT_WRITER_ERROR);
1576 if (has_startcode) {
1577 /* Skip the start code, the NalWriter will add it automatically. */
1578 if (raw_size >= 4 && raw_data[0] == 0
1579 && raw_data[1] == 0 && raw_data[2] == 0 && raw_data[3] == 0x01) {
1582 } else if (raw_size >= 3 && raw_data[0] == 0 && raw_data[1] == 0
1583 && raw_data[2] == 0x01) {
1587 /* Fail to find the start code. */
1588 g_return_val_if_reached (GST_H264_BIT_WRITER_ERROR);
1592 /* If no RBSP trailing needed, it must align to byte. We assume
1593 that the rbsp trailing bits are already added. */
1595 g_return_val_if_fail (raw_size % 8 == 0, GST_H264_BIT_WRITER_ERROR);
1597 nal_writer_init (&nw, nal_prefix_size, packetized);
1599 if (!nal_writer_put_bytes (&nw, raw_data, raw_size / 8))
1603 guint8 data = *(raw_data + raw_size / 8);
1605 if (!nal_writer_put_bits_uint8 (&nw,
1606 data >> (8 - raw_size % 8), raw_size % 8))
1610 if (add_trailings) {
1611 if (!nal_writer_do_rbsp_trailing_bits (&nw))
1615 data = nal_writer_reset_and_get_data (&nw, &size);
1619 if (size > *nal_size) {
1620 need_more_space = TRUE;
1625 memcpy (nal_data, data, size);
1628 nal_writer_reset (&nw);
1629 return GST_H264_BIT_WRITER_OK;
1632 nal_writer_reset (&nw);
1635 GST_WARNING ("Failed to convert nal data");
1637 return need_more_space ? GST_H264_BIT_WRITER_INVALID_DATA :
1638 GST_H264_BIT_WRITER_NO_MORE_SPACE;