3 * Copyright (C) 2009 Carl-Anton Ingmarsson <ca.ingmarsson@gmail.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 the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #include "gstnalreader.h"
25 #include "gsth264parser.h"
27 /* default scaling_lists according to Table 7-2 */
28 const guint8 default_4x4_intra[16] =
29 { 6, 13, 13, 20, 20, 20, 28, 28, 28, 28, 32, 32,
33 const guint8 default_4x4_inter[16] =
34 { 10, 14, 14, 20, 20, 20, 24, 24, 24, 24, 27, 27,
38 const guint8 default_8x8_intra[64] =
39 { 6, 10, 10, 13, 11, 13, 16, 16, 16, 16, 18, 18,
40 18, 18, 18, 23, 23, 23, 23, 23, 23, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27,
41 27, 27, 27, 27, 27, 29, 29, 29, 29, 29, 29, 29, 31, 31, 31, 31, 31, 31, 33,
42 33, 33, 33, 33, 36, 36, 36, 36, 38, 38, 38, 40, 40, 42
45 const guint8 default_8x8_inter[64] =
46 { 9, 13, 13, 15, 13, 15, 17, 17, 17, 17, 19, 19,
47 19, 19, 19, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 24, 24, 24,
48 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27, 27, 27, 28,
49 28, 28, 28, 28, 30, 30, 30, 30, 32, 32, 32, 33, 33, 35
52 const guint8 zigzag_8x8[64] = {
53 0, 1, 8, 16, 9, 2, 3, 10,
54 17, 24, 32, 25, 18, 11, 4, 5,
55 12, 19, 26, 33, 40, 48, 41, 34,
56 27, 20, 13, 6, 7, 14, 21, 28,
57 35, 42, 49, 56, 57, 50, 43, 36,
58 29, 22, 15, 23, 30, 37, 44, 51,
59 58, 59, 52, 45, 38, 31, 39, 46,
60 53, 60, 61, 54, 47, 55, 62, 63
63 const guint8 zigzag_4x4[16] = {
70 #define CHECK_ALLOWED(val, min, max) { \
71 if (val < min || val > max) { \
72 GST_WARNING ("value not in allowed range. value: %d, range %d-%d", \
78 #define READ_UINT8(reader, val, nbits) { \
79 if (!gst_nal_reader_get_bits_uint8 (reader, &val, nbits)) { \
80 GST_WARNING ("failed to read uint8, nbits: %d", nbits); \
85 #define READ_UINT16(reader, val, nbits) { \
86 if (!gst_nal_reader_get_bits_uint16 (reader, &val, nbits)) { \
87 GST_WARNING ("failed to read uint16, nbits: %d", nbits); \
92 #define READ_UINT32(reader, val, nbits) { \
93 if (!gst_nal_reader_get_bits_uint32 (reader, &val, nbits)) { \
94 GST_WARNING ("failed to read uint32, nbits: %d", nbits); \
99 #define READ_UINT64(reader, val, nbits) { \
100 if (!gst_nal_reader_get_bits_uint64 (reader, &val, nbits)) { \
101 GST_WARNING ("failed to read uint32, nbits: %d", nbits); \
106 #define READ_UE(reader, val) { \
107 if (!gst_nal_reader_get_ue (reader, &val)) { \
108 GST_WARNING ("failed to read UE"); \
113 #define READ_UE_ALLOWED(reader, val, min, max) { \
115 READ_UE (reader, tmp); \
116 CHECK_ALLOWED (tmp, min, max); \
120 #define READ_SE(reader, val) { \
121 if (!gst_nal_reader_get_se (reader, &val)) { \
122 GST_WARNING ("failed to read SE"); \
127 #define READ_SE_ALLOWED(reader, val, min, max) { \
129 READ_SE (reader, tmp); \
130 CHECK_ALLOWED (tmp, min, max); \
134 GST_DEBUG_CATEGORY_STATIC (h264parser_debug);
135 #define GST_CAT_DEFAULT h264parser_debug
138 GST_DEBUG_CATEGORY_INIT (h264parser_debug, "h264parser", 0, \
141 G_DEFINE_TYPE_WITH_CODE (GstH264Parser, gst_h264_parser, G_TYPE_OBJECT,
145 gst_h264_sequence_free (void *data)
147 g_slice_free (GstH264Sequence, data);
151 gst_h264_parse_hrd_parameters (GstH264HRDParameters * hrd,
152 GstNalReader * reader)
156 GST_DEBUG ("parsing \"HRD Parameters\"");
158 READ_UE_ALLOWED (reader, hrd->cpb_cnt_minus1, 0, 31);
159 READ_UINT8 (reader, hrd->bit_rate_scale, 4);
160 READ_UINT8 (reader, hrd->cpb_size_scale, 4);
162 for (SchedSelIdx = 0; SchedSelIdx <= hrd->cpb_cnt_minus1; SchedSelIdx++) {
163 READ_UE (reader, hrd->bit_rate_value_minus1[SchedSelIdx]);
164 READ_UE (reader, hrd->cpb_size_value_minus1[SchedSelIdx]);
167 READ_UINT8 (reader, hrd->initial_cpb_removal_delay_length_minus1, 5);
168 READ_UINT8 (reader, hrd->cpb_removal_delay_length_minus1, 5);
169 READ_UINT8 (reader, hrd->dpb_output_delay_length_minus1, 5);
170 READ_UINT8 (reader, hrd->time_offset_length, 5);
175 GST_WARNING ("error parsing \"HRD Parameters\"");
181 gst_h264_parse_vui_parameters (GstH264VUIParameters * vui,
182 GstNalReader * reader)
184 guint8 aspect_ratio_info_present_flag;
185 guint8 video_signal_type_present_flag;
186 guint8 chroma_loc_info_present_flag;
188 GST_DEBUG ("parsing \"VUI Parameters\"");
190 /* set default values for fields that might not be present in the bitstream
191 and have valid defaults */
192 vui->aspect_ratio_idc = 0;
193 vui->video_format = 5;
194 vui->video_full_range_flag = 0;
195 vui->colour_primaries = 2;
196 vui->transfer_characteristics = 2;
197 vui->matrix_coefficients = 2;
198 vui->chroma_sample_loc_type_top_field = 0;
199 vui->chroma_sample_loc_type_bottom_field = 0;
200 vui->low_delay_hrd_flag = 0;
202 READ_UINT8 (reader, aspect_ratio_info_present_flag, 1);
203 if (aspect_ratio_info_present_flag) {
204 READ_UINT8 (reader, vui->aspect_ratio_idc, 8);
205 if (vui->aspect_ratio_idc == 255) {
206 READ_UINT16 (reader, vui->sar_width, 16);
207 READ_UINT16 (reader, vui->sar_height, 16);
211 READ_UINT8 (reader, vui->overscan_info_present_flag, 1);
212 if (vui->overscan_info_present_flag)
213 READ_UINT8 (reader, vui->overscan_appropriate_flag, 1);
215 READ_UINT8 (reader, video_signal_type_present_flag, 1);
216 if (video_signal_type_present_flag) {
217 guint8 colour_description_present_flag;
219 READ_UINT8 (reader, vui->video_format, 3);
220 READ_UINT8 (reader, vui->video_full_range_flag, 1);
221 READ_UINT8 (reader, colour_description_present_flag, 1);
222 if (colour_description_present_flag) {
223 READ_UINT8 (reader, vui->colour_primaries, 8);
224 READ_UINT8 (reader, vui->transfer_characteristics, 8);
225 READ_UINT8 (reader, vui->matrix_coefficients, 8);
229 READ_UINT8 (reader, chroma_loc_info_present_flag, 1);
230 if (chroma_loc_info_present_flag) {
231 READ_UE_ALLOWED (reader, vui->chroma_sample_loc_type_top_field, 0, 5);
232 READ_UE_ALLOWED (reader, vui->chroma_sample_loc_type_bottom_field, 0, 5);
235 READ_UINT8 (reader, vui->timing_info_present_flag, 1);
236 if (vui->timing_info_present_flag) {
237 READ_UINT32 (reader, vui->num_units_in_tick, 32);
238 if (vui->num_units_in_tick == 0)
240 ("num_units_in_tick = 0 detected in stream (incompliant to H.264 E.2.1).");
242 READ_UINT32 (reader, vui->time_scale, 32);
243 if (vui->time_scale == 0)
245 ("time_scale = 0 detected in stream (incompliant to H.264 E.2.1).");
247 READ_UINT8 (reader, vui->fixed_frame_rate_flag, 1);
250 READ_UINT8 (reader, vui->nal_hrd_parameters_present_flag, 1);
251 if (vui->nal_hrd_parameters_present_flag) {
252 if (!gst_h264_parse_hrd_parameters (&vui->nal_hrd_parameters, reader))
256 READ_UINT8 (reader, vui->vcl_hrd_parameters_present_flag, 1);
257 if (vui->vcl_hrd_parameters_present_flag) {
258 if (!gst_h264_parse_hrd_parameters (&vui->vcl_hrd_parameters, reader))
262 if (vui->nal_hrd_parameters_present_flag ||
263 vui->vcl_hrd_parameters_present_flag)
264 READ_UINT8 (reader, vui->low_delay_hrd_flag, 1);
266 READ_UINT8 (reader, vui->pic_struct_present_flag, 1);
271 GST_WARNING ("error parsing \"VUI Parameters\"");
276 gst_h264_parser_parse_scaling_list (GstNalReader * reader,
277 guint8 scaling_lists_4x4[6][16], guint8 scaling_lists_8x8[6][64],
278 const guint8 fallback_4x4_inter[16], const guint8 fallback_4x4_intra[16],
279 const guint8 fallback_8x8_inter[64], const guint8 fallback_8x8_intra[64],
284 GST_DEBUG ("parsing scaling lists");
286 for (i = 0; i < 12; i++) {
287 gboolean use_default = FALSE;
290 guint8 scaling_list_present_flag;
292 READ_UINT8 (reader, scaling_list_present_flag, 1);
293 if (scaling_list_present_flag) {
294 guint8 *scaling_list;
298 guint8 last_scale, next_scale;
301 scaling_list = scaling_lists_4x4[i];
305 scaling_list = scaling_lists_8x8[i - 6];
312 for (j = 0; j < size; j++) {
313 if (next_scale != 0) {
316 READ_SE (reader, delta_scale);
317 next_scale = (last_scale + delta_scale) & 0xff;
319 if (j == 0 && next_scale == 0) {
323 last_scale = scaling_list[scan[j]] =
324 (next_scale == 0) ? last_scale : next_scale;
334 memcpy (scaling_lists_4x4[0], fallback_4x4_intra, 16);
337 memcpy (scaling_lists_4x4[1], scaling_lists_4x4[0], 16);
340 memcpy (scaling_lists_4x4[2], scaling_lists_4x4[1], 16);
343 memcpy (scaling_lists_4x4[3], fallback_4x4_inter, 16);
346 memcpy (scaling_lists_4x4[4], scaling_lists_4x4[3], 16);
349 memcpy (scaling_lists_4x4[5], scaling_lists_4x4[4], 16);
352 memcpy (scaling_lists_8x8[0], fallback_8x8_intra, 64);
355 memcpy (scaling_lists_8x8[1], fallback_8x8_inter, 64);
358 memcpy (scaling_lists_8x8[2], scaling_lists_8x8[0], 64);
361 memcpy (scaling_lists_8x8[3], scaling_lists_8x8[1], 64);
364 memcpy (scaling_lists_8x8[4], scaling_lists_8x8[2], 64);
367 memcpy (scaling_lists_8x8[5], scaling_lists_8x8[3], 64);
380 GST_WARNING ("error parsing scaling lists");
385 gst_h264_parser_parse_sequence (GstH264Parser * parser, guint8 * data,
388 GstNalReader reader = GST_NAL_READER_INIT (data, size);
389 GstH264Sequence *seq;
390 guint8 frame_cropping_flag;
392 g_return_val_if_fail (GST_IS_H264_PARSER (parser), NULL);
393 g_return_val_if_fail (data != NULL, NULL);
394 g_return_val_if_fail (size > 0, NULL);
396 GST_DEBUG ("parsing \"Sequence parameter set\"");
398 seq = g_slice_new (GstH264Sequence);
400 /* set default values for fields that might not be present in the bitstream
401 and have valid defaults */
402 seq->chroma_format_idc = 1;
403 seq->separate_colour_plane_flag = 0;
404 seq->bit_depth_luma_minus8 = 0;
405 seq->bit_depth_chroma_minus8 = 0;
406 memset (seq->scaling_lists_4x4, 16, 96);
407 memset (seq->scaling_lists_8x8, 16, 384);
408 seq->mb_adaptive_frame_field_flag = 0;
409 seq->frame_crop_left_offset = 0;
410 seq->frame_crop_right_offset = 0;
411 seq->frame_crop_top_offset = 0;
412 seq->frame_crop_bottom_offset = 0;
414 READ_UINT8 (&reader, seq->profile_idc, 8);
415 READ_UINT8 (&reader, seq->constraint_set0_flag, 1);
416 READ_UINT8 (&reader, seq->constraint_set1_flag, 1);
417 READ_UINT8 (&reader, seq->constraint_set2_flag, 1);
418 READ_UINT8 (&reader, seq->constraint_set3_flag, 1);
420 /* skip reserved_zero_4bits */
421 if (!gst_nal_reader_skip (&reader, 4))
424 READ_UINT8 (&reader, seq->level_idc, 8);
426 READ_UE_ALLOWED (&reader, seq->id, 0, 31);
428 if (seq->profile_idc == 100 || seq->profile_idc == 110 ||
429 seq->profile_idc == 122 || seq->profile_idc == 244 ||
430 seq->profile_idc == 44 || seq->profile_idc == 83 ||
431 seq->profile_idc == 86) {
432 READ_UE_ALLOWED (&reader, seq->chroma_format_idc, 0, 3);
433 if (seq->chroma_format_idc == 3)
434 READ_UINT8 (&reader, seq->separate_colour_plane_flag, 1);
436 READ_UE_ALLOWED (&reader, seq->bit_depth_luma_minus8, 0, 6);
437 READ_UE_ALLOWED (&reader, seq->bit_depth_chroma_minus8, 0, 6);
438 READ_UINT8 (&reader, seq->qpprime_y_zero_transform_bypass_flag, 1);
440 READ_UINT8 (&reader, seq->scaling_matrix_present_flag, 1);
441 if (seq->scaling_matrix_present_flag) {
444 n_lists = (seq->chroma_format_idc != 3) ? 8 : 12;
445 if (!gst_h264_parser_parse_scaling_list (&reader,
446 seq->scaling_lists_4x4, seq->scaling_lists_8x8,
447 default_4x4_inter, default_4x4_intra,
448 default_8x8_inter, default_8x8_intra, n_lists))
453 READ_UE_ALLOWED (&reader, seq->log2_max_frame_num_minus4, 0, 12);
454 /* calculate MaxFrameNum */
455 seq->MaxFrameNum = 1 << (seq->log2_max_frame_num_minus4 + 4);
457 READ_UE_ALLOWED (&reader, seq->pic_order_cnt_type, 0, 2);
458 if (seq->pic_order_cnt_type == 0) {
459 READ_UE_ALLOWED (&reader, seq->log2_max_pic_order_cnt_lsb_minus4, 0, 12);
460 } else if (seq->pic_order_cnt_type == 1) {
463 READ_UINT8 (&reader, seq->delta_pic_order_always_zero_flag, 1);
464 READ_SE (&reader, seq->offset_for_non_ref_pic);
465 READ_SE (&reader, seq->offset_for_top_to_bottom_field);
466 READ_UE_ALLOWED (&reader, seq->num_ref_frames_in_pic_order_cnt_cycle, 0,
468 for (i = 0; i < seq->num_ref_frames_in_pic_order_cnt_cycle; i++)
469 READ_SE (&reader, seq->offset_for_ref_frame[i]);
472 READ_UE (&reader, seq->num_ref_frames);
473 READ_UINT8 (&reader, seq->gaps_in_frame_num_value_allowed_flag, 1);
474 READ_UE (&reader, seq->pic_width_in_mbs_minus1);
475 READ_UE (&reader, seq->pic_height_in_map_units_minus1);
476 READ_UINT8 (&reader, seq->frame_mbs_only_flag, 1);
478 if (!seq->frame_mbs_only_flag)
479 READ_UINT8 (&reader, seq->mb_adaptive_frame_field_flag, 1);
481 READ_UINT8 (&reader, seq->direct_8x8_inference_flag, 1);
482 READ_UINT8 (&reader, frame_cropping_flag, 1);
483 if (frame_cropping_flag) {
484 READ_UE (&reader, seq->frame_crop_left_offset);
485 READ_UE (&reader, seq->frame_crop_right_offset);
486 READ_UE (&reader, seq->frame_crop_top_offset);
487 READ_UE (&reader, seq->frame_crop_bottom_offset);
490 READ_UINT8 (&reader, seq->vui_parameters_present_flag, 1);
491 if (seq->vui_parameters_present_flag) {
492 if (!gst_h264_parse_vui_parameters (&seq->vui_parameters, &reader))
496 /* calculate ChromaArrayType */
497 if (seq->separate_colour_plane_flag)
498 seq->ChromaArrayType = 0;
500 seq->ChromaArrayType = seq->chroma_format_idc;
502 GST_DEBUG ("adding sequence parameter set with id: %d to hash table",
504 g_hash_table_replace (parser->sequences, &seq->id, seq);
508 GST_WARNING ("error parsing \"Sequence parameter set\"");
510 gst_h264_sequence_free (seq);
515 gst_h264_picture_free (void *data)
517 GstH264Picture *pic = (GstH264Picture *) data;
519 if (pic->slice_group_id)
520 g_free (pic->slice_group_id);
522 g_slice_free (GstH264Picture, data);
526 gst_h264_parser_more_data (GstNalReader * reader)
530 remaining = gst_nal_reader_get_remaining (reader);
534 if (remaining <= 8) {
535 guint8 rbsp_stop_one_bit;
537 if (!gst_nal_reader_peek_bits_uint8 (reader, &rbsp_stop_one_bit, 1))
540 if (rbsp_stop_one_bit == 1) {
546 if (!gst_nal_reader_peek_bits_uint8 (reader, &zero_bits, remaining))
549 if ((zero_bits - (1 << (remaining - 1))) == 0)
558 gst_h264_parser_parse_picture (GstH264Parser * parser, guint8 * data,
561 GstNalReader reader = GST_NAL_READER_INIT (data, size);
563 gint seq_parameter_set_id;
564 GstH264Sequence *seq;
565 guint8 pic_scaling_matrix_present_flag;
567 g_return_val_if_fail (GST_IS_H264_PARSER (parser), NULL);
568 g_return_val_if_fail (data != NULL, NULL);
569 g_return_val_if_fail (size > 0, NULL);
571 GST_DEBUG ("parsing \"Picture parameter set\"");
573 pic = g_slice_new (GstH264Picture);
575 READ_UE_ALLOWED (&reader, pic->id, 0, 255);
576 READ_UE_ALLOWED (&reader, seq_parameter_set_id, 0, 31);
577 seq = g_hash_table_lookup (parser->sequences, &seq_parameter_set_id);
579 GST_WARNING ("couldn't find associated sequence parameter set with id: %d",
580 seq_parameter_set_id);
585 /* set default values for fields that might not be present in the bitstream
586 and have valid defaults */
587 pic->slice_group_id = NULL;
588 pic->transform_8x8_mode_flag = 0;
589 memcpy (&pic->scaling_lists_4x4, &seq->scaling_lists_4x4, 96);
590 memcpy (&pic->scaling_lists_8x8, &seq->scaling_lists_8x8, 384);
592 READ_UINT8 (&reader, pic->entropy_coding_mode_flag, 1);
593 READ_UINT8 (&reader, pic->pic_order_present_flag, 1);
594 READ_UE_ALLOWED (&reader, pic->num_slice_groups_minus1, 0, 7);
595 if (pic->num_slice_groups_minus1 > 0) {
596 READ_UE_ALLOWED (&reader, pic->slice_group_map_type, 0, 6);
597 if (pic->slice_group_map_type == 0) {
600 for (i = 0; i <= pic->num_slice_groups_minus1; i++)
601 READ_UE (&reader, pic->run_length_minus1[i]);
602 } else if (pic->slice_group_map_type == 2) {
605 for (i = 0; i <= pic->num_slice_groups_minus1; i++) {
606 READ_UE (&reader, pic->top_left[i]);
607 READ_UE (&reader, pic->bottom_right[i]);
609 } else if (pic->slice_group_map_type >= 3 && pic->slice_group_map_type <= 5) {
610 READ_UINT8 (&reader, pic->slice_group_change_direction_flag, 1);
611 READ_UE (&reader, pic->slice_group_change_rate_minus1);
612 } else if (pic->slice_group_map_type == 6) {
616 READ_UE (&reader, pic->pic_size_in_map_units_minus1);
617 bits = g_bit_storage (pic->num_slice_groups_minus1);
619 pic->slice_group_id =
620 g_new (guint8, pic->pic_size_in_map_units_minus1 + 1);
621 for (i = 0; i <= pic->pic_size_in_map_units_minus1; i++)
622 READ_UINT8 (&reader, pic->slice_group_id[i], bits);
626 READ_UE_ALLOWED (&reader, pic->num_ref_idx_l0_active_minus1, 0, 31);
627 READ_UE_ALLOWED (&reader, pic->num_ref_idx_l1_active_minus1, 0, 31);
628 READ_UINT8 (&reader, pic->weighted_pred_flag, 1);
629 READ_UINT8 (&reader, pic->weighted_bipred_idc, 2);
630 READ_SE_ALLOWED (&reader, pic->pic_init_qp_minus26, -26, 25);
631 READ_SE_ALLOWED (&reader, pic->pic_init_qs_minus26, -26, 25);
632 READ_SE_ALLOWED (&reader, pic->chroma_qp_index_offset, -12, 12);
633 pic->second_chroma_qp_index_offset = pic->chroma_qp_index_offset;
634 READ_UINT8 (&reader, pic->deblocking_filter_control_present_flag, 1);
635 READ_UINT8 (&reader, pic->constrained_intra_pred_flag, 1);
636 READ_UINT8 (&reader, pic->redundant_pic_cnt_present_flag, 1);
638 if (!gst_h264_parser_more_data (&reader))
641 READ_UINT8 (&reader, pic->transform_8x8_mode_flag, 1);
643 READ_UINT8 (&reader, pic_scaling_matrix_present_flag, 1);
644 if (pic_scaling_matrix_present_flag) {
647 n_lists = 6 + ((seq->chroma_format_idc != 3) ? 2 : 6) *
648 pic->transform_8x8_mode_flag;
650 if (seq->scaling_matrix_present_flag) {
651 if (!gst_h264_parser_parse_scaling_list (&reader,
652 pic->scaling_lists_4x4, pic->scaling_lists_8x8,
653 seq->scaling_lists_4x4[0], seq->scaling_lists_4x4[3],
654 seq->scaling_lists_8x8[0], seq->scaling_lists_8x8[3], n_lists))
657 if (!gst_h264_parser_parse_scaling_list (&reader,
658 pic->scaling_lists_4x4, pic->scaling_lists_8x8,
659 default_4x4_inter, default_4x4_intra,
660 default_8x8_inter, default_8x8_intra, n_lists))
665 READ_SE_ALLOWED (&reader, pic->second_chroma_qp_index_offset, -12, 12);
668 GST_DEBUG ("adding picture parameter set with id: %d to hash table", pic->id);
669 g_hash_table_replace (parser->pictures, &pic->id, pic);
673 GST_WARNING ("error parsing \"Picture parameter set\"");
675 gst_h264_picture_free (pic);
680 gst_h264_slice_parse_pred_weight_table (GstH264Slice * slice,
681 GstNalReader * reader,
682 const GstH264Sequence * seq, const GstH264Picture * pic)
684 GstH264PredWeightTable *p;
687 GST_DEBUG ("parsing \"Prediction weight table\"");
689 p = &slice->pred_weight_table;
691 READ_UE_ALLOWED (reader, p->luma_log2_weight_denom, 0, 7);
692 /* set default values */
693 memset (p->luma_weight_l0, 1 << p->luma_log2_weight_denom, 32);
694 memset (p->luma_offset_l0, 0, 32);
696 if (seq->ChromaArrayType != 0) {
697 READ_UE_ALLOWED (reader, p->chroma_log2_weight_denom, 0, 7);
698 /* set default values */
699 memset (p->chroma_weight_l0, 1 << p->chroma_log2_weight_denom, 64);
700 memset (p->chroma_offset_l0, 0, 64);
703 for (i = 0; i <= slice->num_ref_idx_l0_active_minus1; i++) {
704 guint8 luma_weight_l0_flag;
706 READ_UINT8 (reader, luma_weight_l0_flag, 1);
707 if (luma_weight_l0_flag) {
708 READ_SE_ALLOWED (reader, p->luma_weight_l0[i], -128, 127);
709 READ_SE_ALLOWED (reader, p->luma_offset_l0[i], -128, 127);
711 if (seq->ChromaArrayType != 0) {
712 guint8 chroma_weight_l0_flag;
715 READ_UINT8 (reader, chroma_weight_l0_flag, 1);
716 if (chroma_weight_l0_flag) {
717 for (j = 0; j < 2; j++) {
718 READ_SE_ALLOWED (reader, p->chroma_weight_l0[i][j], -128, 127);
719 READ_SE_ALLOWED (reader, p->chroma_offset_l0[i][j], -128, 127);
725 if (GST_H264_IS_B_SLICE (slice->type)) {
726 for (i = 0; i <= slice->num_ref_idx_l1_active_minus1; i++) {
727 guint8 luma_weight_l1_flag;
729 READ_UINT8 (reader, luma_weight_l1_flag, 1);
730 if (luma_weight_l1_flag) {
731 READ_SE_ALLOWED (reader, p->luma_weight_l1[i], -128, 127);
732 READ_SE_ALLOWED (reader, p->luma_offset_l1[i], -128, 127);
734 if (seq->ChromaArrayType != 0) {
735 guint8 chroma_weight_l1_flag;
738 READ_UINT8 (reader, chroma_weight_l1_flag, 1);
739 if (chroma_weight_l1_flag) {
740 for (j = 0; j < 2; j++) {
741 READ_SE_ALLOWED (reader, p->chroma_weight_l1[i][j], -128, 127);
742 READ_SE_ALLOWED (reader, p->chroma_offset_l1[i][j], -128, 127);
752 GST_WARNING ("error parsing \"Prediction weight table\"");
757 gst_h264_slice_parse_ref_pic_list_reordering (GstH264Slice * slice,
758 GstNalReader * reader)
760 GST_DEBUG ("parsing \"Reference picture list reordering\"");
762 if (!GST_H264_IS_I_SLICE (slice->type) && !GST_H264_IS_SI_SLICE (slice->type)) {
763 guint8 ref_pic_list_reordering_flag_l0;
764 guint8 reordering_of_pic_nums_idc;
766 READ_UINT8 (reader, ref_pic_list_reordering_flag_l0, 1);
767 if (ref_pic_list_reordering_flag_l0)
769 READ_UE_ALLOWED (reader, reordering_of_pic_nums_idc, 0, 3);
770 if (reordering_of_pic_nums_idc == 0 || reordering_of_pic_nums_idc == 1) {
771 guint32 abs_diff_pic_num_minus1 G_GNUC_UNUSED;
773 READ_UE_ALLOWED (reader, abs_diff_pic_num_minus1, 0,
774 slice->MaxPicNum - 1);
775 } else if (reordering_of_pic_nums_idc == 2) {
776 guint32 long_term_pic_num;
778 READ_UE (reader, long_term_pic_num);
780 } while (reordering_of_pic_nums_idc != 3);
783 if (GST_H264_IS_B_SLICE (slice->type)) {
784 guint8 ref_pic_list_reordering_flag_l1;
785 guint8 reordering_of_pic_nums_idc;
787 READ_UINT8 (reader, ref_pic_list_reordering_flag_l1, 1);
788 if (ref_pic_list_reordering_flag_l1)
790 READ_UE_ALLOWED (reader, reordering_of_pic_nums_idc, 0, 3);
791 if (reordering_of_pic_nums_idc == 0 || reordering_of_pic_nums_idc == 1) {
792 guint32 abs_diff_num_minus1;
793 READ_UE (reader, abs_diff_num_minus1);
794 } else if (reordering_of_pic_nums_idc == 2) {
795 guint32 long_term_pic_num;
797 READ_UE (reader, long_term_pic_num);
799 } while (reordering_of_pic_nums_idc != 3);
805 GST_WARNING ("error parsing \"Reference picture list reordering\"");
810 gst_h264_slice_parse_dec_ref_pic_marking (GstH264Slice * slice,
811 GstNalReader * reader)
813 GstH264DecRefPicMarking *m;
815 GST_DEBUG ("parsing \"Decoded reference picture marking\"");
817 m = &slice->dec_ref_pic_marking;
819 if (slice->nal_unit.IdrPicFlag) {
820 READ_UINT8 (reader, m->no_output_of_prior_pics_flag, 1);
821 READ_UINT8 (reader, m->long_term_reference_flag, 1);
823 READ_UINT8 (reader, m->adaptive_ref_pic_marking_mode_flag, 1);
824 if (m->adaptive_ref_pic_marking_mode_flag) {
825 guint8 memory_management_control_operation;
827 m->n_ref_pic_marking = 0;
829 READ_UE_ALLOWED (reader, memory_management_control_operation, 0, 6);
830 if (memory_management_control_operation == 0)
833 m->ref_pic_marking[m->
834 n_ref_pic_marking].memory_management_control_operation =
835 memory_management_control_operation;
837 if (memory_management_control_operation == 1 ||
838 memory_management_control_operation == 3)
840 m->ref_pic_marking[m->
841 n_ref_pic_marking].difference_of_pic_nums_minus1);
843 if (memory_management_control_operation == 2)
845 m->ref_pic_marking[m->n_ref_pic_marking].long_term_pic_num);
847 if (memory_management_control_operation == 3 ||
848 memory_management_control_operation == 6)
850 m->ref_pic_marking[m->n_ref_pic_marking].long_term_frame_idx);
852 if (memory_management_control_operation == 4)
854 m->ref_pic_marking[m->
855 n_ref_pic_marking].max_long_term_frame_idx_plus1);
857 m->n_ref_pic_marking++;
865 GST_WARNING ("error parsing \"Decoded reference picture marking\"");
870 gst_h264_parser_parse_slice_header (GstH264Parser * parser,
871 GstH264Slice * slice, guint8 * data, guint size, GstNalUnit nal_unit)
873 GstNalReader reader = GST_NAL_READER_INIT (data, size);
874 gint pic_parameter_set_id;
876 GstH264Sequence *seq;
878 g_return_val_if_fail (GST_IS_H264_PARSER (parser), FALSE);
879 g_return_val_if_fail (slice != NULL, FALSE);
880 g_return_val_if_fail (data != NULL, FALSE);
881 g_return_val_if_fail (size > 0, FALSE);
883 GST_DEBUG ("parsing \"Slice header\"");
885 memcpy (&slice->nal_unit, &nal_unit, sizeof (GstNalUnit));
887 READ_UE (&reader, slice->first_mb_in_slice);
888 READ_UE (&reader, slice->type);
890 READ_UE_ALLOWED (&reader, pic_parameter_set_id, 0, 255);
891 pic = g_hash_table_lookup (parser->pictures, &pic_parameter_set_id);
893 GST_WARNING ("couldn't find associated picture parameter set with id: %d",
894 pic_parameter_set_id);
897 slice->picture = pic;
900 /* set default values for fields that might not be present in the bitstream
901 and have valid defaults */
902 slice->field_pic_flag = 0;
903 slice->bottom_field_flag = 0;
904 slice->delta_pic_order_cnt_bottom = 0;
905 slice->delta_pic_order_cnt[0] = 0;
906 slice->delta_pic_order_cnt[1] = 0;
907 slice->redundant_pic_cnt = 0;
908 slice->num_ref_idx_l0_active_minus1 = pic->num_ref_idx_l0_active_minus1;
909 slice->num_ref_idx_l1_active_minus1 = pic->num_ref_idx_l1_active_minus1;
911 if (seq->separate_colour_plane_flag)
912 READ_UINT8 (&reader, slice->colour_plane_id, 2);
914 READ_UINT16 (&reader, slice->frame_num, seq->log2_max_frame_num_minus4 + 4);
916 if (!seq->frame_mbs_only_flag) {
917 READ_UINT8 (&reader, slice->field_pic_flag, 1);
918 if (slice->field_pic_flag)
919 READ_UINT8 (&reader, slice->bottom_field_flag, 1);
922 /* calculate MaxPicNum */
923 if (slice->field_pic_flag)
924 slice->MaxPicNum = seq->MaxFrameNum;
926 slice->MaxPicNum = 2 * seq->MaxFrameNum;
928 if (nal_unit.type == 5)
929 READ_UE_ALLOWED (&reader, slice->idr_pic_id, 0, 65535);
931 if (seq->pic_order_cnt_type == 0) {
932 READ_UINT16 (&reader, slice->pic_order_cnt_lsb,
933 seq->log2_max_pic_order_cnt_lsb_minus4 + 4);
934 if (pic->pic_order_present_flag && !slice->field_pic_flag)
935 READ_SE (&reader, slice->delta_pic_order_cnt_bottom);
938 if (seq->pic_order_cnt_type == 1 && !seq->delta_pic_order_always_zero_flag) {
939 READ_SE (&reader, slice->delta_pic_order_cnt[0]);
940 if (pic->pic_order_present_flag && !slice->field_pic_flag)
941 READ_SE (&reader, slice->delta_pic_order_cnt[1]);
944 if (pic->redundant_pic_cnt_present_flag)
945 READ_UE_ALLOWED (&reader, slice->redundant_pic_cnt, 0, 127);
947 if (GST_H264_IS_B_SLICE (slice->type))
948 READ_UINT8 (&reader, slice->direct_spatial_mv_pred_flag, 1);
950 if (GST_H264_IS_P_SLICE (slice->type) ||
951 GST_H264_IS_SP_SLICE (slice->type) || GST_H264_IS_B_SLICE (slice->type)) {
952 guint8 num_ref_idx_active_override_flag;
954 READ_UINT8 (&reader, num_ref_idx_active_override_flag, 1);
955 if (num_ref_idx_active_override_flag) {
956 READ_UE_ALLOWED (&reader, slice->num_ref_idx_l0_active_minus1, 0, 31);
958 if (GST_H264_IS_B_SLICE (slice->type))
959 READ_UE_ALLOWED (&reader, slice->num_ref_idx_l1_active_minus1, 0, 31);
963 if (!gst_h264_slice_parse_ref_pic_list_reordering (slice, &reader))
966 if ((pic->weighted_pred_flag && (GST_H264_IS_P_SLICE (slice->type) ||
967 GST_H264_IS_SP_SLICE (slice->type)))
968 || (pic->weighted_bipred_idc == 1 && GST_H264_IS_B_SLICE (slice->type))) {
969 if (!gst_h264_slice_parse_pred_weight_table (slice, &reader, seq, pic))
973 if (nal_unit.ref_idc != 0) {
974 if (!gst_h264_slice_parse_dec_ref_pic_marking (slice, &reader))
981 GST_WARNING ("error parsing \"Slice header\"");
986 gst_h264_parser_parse_buffering_period (GstH264Parser * parser,
987 GstH264BufferingPeriod * per, guint8 * data, guint size)
989 GstNalReader reader = GST_NAL_READER_INIT (data, size);
991 GstH264Sequence *seq;
992 guint8 seq_parameter_set_id;
994 GST_DEBUG ("parsing \"Buffering period\"");
996 READ_UE_ALLOWED (&reader, seq_parameter_set_id, 0, 31);
997 seq = g_hash_table_lookup (parser->sequences, &seq_parameter_set_id);
999 GST_WARNING ("couldn't find associated sequence parameter set with id: %d",
1000 seq_parameter_set_id);
1005 if (seq->vui_parameters_present_flag) {
1006 GstH264VUIParameters *vui = &seq->vui_parameters;
1008 if (vui->nal_hrd_parameters_present_flag) {
1009 GstH264HRDParameters *hrd = &vui->nal_hrd_parameters;
1012 for (SchedSelIdx = 0; SchedSelIdx <= hrd->cpb_cnt_minus1; SchedSelIdx++) {
1013 READ_UINT8 (&reader, per->nal_initial_cpb_removal_delay[SchedSelIdx],
1015 READ_UINT8 (&reader,
1016 per->nal_initial_cpb_removal_delay_offset[SchedSelIdx], 5);
1020 if (vui->vcl_hrd_parameters_present_flag) {
1021 GstH264HRDParameters *hrd = &vui->vcl_hrd_parameters;
1024 for (SchedSelIdx = 0; SchedSelIdx <= hrd->cpb_cnt_minus1; SchedSelIdx++) {
1025 READ_UINT8 (&reader, per->vcl_initial_cpb_removal_delay[SchedSelIdx],
1027 READ_UINT8 (&reader,
1028 per->vcl_initial_cpb_removal_delay_offset[SchedSelIdx], 5);
1036 GST_WARNING ("error parsing \"Buffering period\"");
1041 gst_h264_parse_clock_timestamp (GstH264ClockTimestamp * tim,
1042 GstH264VUIParameters * vui, GstNalReader * reader)
1044 guint8 full_timestamp_flag;
1045 guint8 time_offset_length;
1047 GST_DEBUG ("parsing \"Clock timestamp\"");
1050 tim->time_offset = 0;
1052 READ_UINT8 (reader, tim->ct_type, 2);
1053 READ_UINT8 (reader, tim->nuit_field_based_flag, 1);
1054 READ_UINT8 (reader, tim->counting_type, 5);
1055 READ_UINT8 (reader, full_timestamp_flag, 1);
1056 READ_UINT8 (reader, tim->discontinuity_flag, 1);
1057 READ_UINT8 (reader, tim->cnt_dropped_flag, 1);
1058 READ_UINT8 (reader, tim->n_frames, 8);
1060 if (full_timestamp_flag) {
1061 tim->seconds_flag = TRUE;
1062 READ_UINT8 (reader, tim->seconds_value, 6);
1064 tim->minutes_flag = TRUE;
1065 READ_UINT8 (reader, tim->minutes_value, 6);
1067 tim->hours_flag = TRUE;
1068 READ_UINT8 (reader, tim->hours_value, 5);
1070 READ_UINT8 (reader, tim->seconds_flag, 1);
1071 if (tim->seconds_flag) {
1072 READ_UINT8 (reader, tim->seconds_value, 6);
1073 READ_UINT8 (reader, tim->minutes_flag, 1);
1074 if (tim->minutes_flag) {
1075 READ_UINT8 (reader, tim->minutes_value, 6);
1076 READ_UINT8 (reader, tim->hours_flag, 1);
1077 if (tim->hours_flag)
1078 READ_UINT8 (reader, tim->hours_value, 5);
1083 time_offset_length = 0;
1084 if (vui->nal_hrd_parameters_present_flag)
1085 time_offset_length = vui->nal_hrd_parameters.time_offset_length;
1086 else if (vui->vcl_hrd_parameters_present_flag)
1087 time_offset_length = vui->vcl_hrd_parameters.time_offset_length;
1089 if (time_offset_length > 0)
1090 READ_UINT32 (reader, tim->time_offset, time_offset_length);
1093 GST_WARNING ("error parsing \"Clock timestamp\"");
1098 gst_h264_parser_parse_pic_timing (GstH264Parser * parser, GstH264Sequence * seq,
1099 GstH264PicTiming * tim, guint8 * data, guint size)
1101 GstNalReader reader = GST_NAL_READER_INIT (data, size);
1103 GST_DEBUG ("parsing \"Picture timing\"");
1106 GST_WARNING ("didn't get the associated sequence paramater set for the "
1107 "current access unit");
1111 /* default values */
1112 memset (tim->clock_timestamp_flag, 0, 3);
1114 if (seq->vui_parameters_present_flag) {
1115 GstH264VUIParameters *vui = &seq->vui_parameters;
1117 if (vui->nal_hrd_parameters_present_flag) {
1118 READ_UINT8 (&reader, tim->cpb_removal_delay,
1119 vui->nal_hrd_parameters.cpb_removal_delay_length_minus1 + 1);
1120 READ_UINT8 (&reader, tim->dpb_output_delay,
1121 vui->nal_hrd_parameters.dpb_output_delay_length_minus1 + 1);
1122 } else if (vui->nal_hrd_parameters_present_flag) {
1123 READ_UINT8 (&reader, tim->cpb_removal_delay,
1124 vui->vcl_hrd_parameters.cpb_removal_delay_length_minus1 + 1);
1125 READ_UINT8 (&reader, tim->dpb_output_delay,
1126 vui->vcl_hrd_parameters.dpb_output_delay_length_minus1 + 1);
1129 if (vui->pic_struct_present_flag) {
1130 const guint8 num_clock_ts_table[9] = {
1131 1, 1, 1, 2, 2, 3, 3, 2, 3
1136 READ_UINT8 (&reader, tim->pic_struct, 4);
1137 CHECK_ALLOWED (tim->pic_struct, 0, 8);
1139 NumClockTs = num_clock_ts_table[tim->pic_struct];
1140 for (i = 0; i < NumClockTs; i++) {
1141 READ_UINT8 (&reader, tim->clock_timestamp_flag[i], 1);
1142 if (tim->clock_timestamp_flag[i]) {
1143 if (!gst_h264_parse_clock_timestamp (&tim->clock_timestamp[i], vui,
1154 GST_WARNING ("error parsing \"Picture timing\"");
1159 gst_h264_parser_parse_sei_message (GstH264Parser * parser,
1160 GstH264Sequence * seq, GstH264SEIMessage * sei, guint8 * data, guint size)
1162 GstNalReader reader;
1164 guint32 payloadSize;
1165 guint8 payload_type_byte, payload_size_byte;
1167 guint8 *payload_data;
1168 guint remaining, payload_size;
1171 g_return_val_if_fail (GST_IS_H264_PARSER (parser), FALSE);
1172 g_return_val_if_fail (sei != NULL, FALSE);
1173 g_return_val_if_fail (data != NULL, FALSE);
1174 g_return_val_if_fail (size > 0, FALSE);
1176 GST_DEBUG ("parsing \"Sei message\"");
1178 gst_nal_reader_init (&reader, data, size);
1180 sei->payloadType = 0;
1182 READ_UINT8 (&reader, payload_type_byte, 8);
1183 sei->payloadType += payload_type_byte;
1185 while (payload_type_byte == 0xff);
1189 READ_UINT8 (&reader, payload_size_byte, 8);
1190 payloadSize += payload_size_byte;
1192 while (payload_size_byte == 0xff);
1194 payload_data = data + gst_nal_reader_get_pos (&reader) * 8;
1195 remaining = gst_nal_reader_get_remaining (&reader) * 8;
1196 payload_size = payloadSize < remaining ? payloadSize : remaining;
1198 if (sei->payloadType == 0)
1200 gst_h264_parser_parse_buffering_period (parser,
1201 &sei->buffering_period, payload_data, payload_size);
1202 else if (sei->payloadType == 1)
1203 res = gst_h264_parser_parse_pic_timing (parser, seq, &sei->pic_timing,
1204 payload_data, payload_size);
1211 GST_WARNING ("error parsing \"Sei message\"");
1215 #undef CHECK_ALLOWED
1221 #undef READ_UE_ALLOWED
1223 #undef READ_SE_ALLOWED
1226 gst_h264_parser_init (GstH264Parser * object)
1228 GstH264Parser *parser = GST_H264_PARSER (object);
1230 parser->sequences = g_hash_table_new_full (g_int_hash, g_int_equal, NULL,
1231 gst_h264_sequence_free);
1232 parser->pictures = g_hash_table_new_full (g_int_hash, g_int_equal, NULL,
1233 gst_h264_picture_free);
1237 gst_h264_parser_finalize (GObject * object)
1239 GstH264Parser *parser = GST_H264_PARSER (object);
1241 g_hash_table_destroy (parser->sequences);
1242 g_hash_table_destroy (parser->pictures);
1244 G_OBJECT_CLASS (gst_h264_parser_parent_class)->finalize (object);
1248 gst_h264_parser_class_init (GstH264ParserClass * klass)
1250 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1252 object_class->finalize = gst_h264_parser_finalize;