2 * Copyright (C) <2011> Intel Corporation
3 * Copyright (C) <2011> Collabora Ltd.
4 * Copyright (C) <2011> Thibault Saunier <thibault.saunier@collabora.com>
6 * Some bits C-c,C-v'ed and s/4/3 from h264parse and videoparsers/h264parse.c:
7 * Copyright (C) <2010> Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>
8 * Copyright (C) <2010> Collabora Multimedia
9 * Copyright (C) <2010> Nokia Corporation
11 * (C) 2005 Michal Benes <michal.benes@itonis.tv>
12 * (C) 2008 Wim Taymans <wim.taymans@gmail.com>
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Library General Public
16 * License as published by the Free Software Foundation; either
17 * version 2 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with this library; if not, write to the
26 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
27 * Boston, MA 02110-1301, USA.
31 * SECTION:gsth264parser
32 * @title: GstH264Parser
33 * @short_description: Convenience library for h264 video
36 * It offers bitstream parsing in both AVC (length-prefixed) and Annex B
37 * (0x000001 start code prefix) format. To identify a NAL unit in a bitstream
38 * and parse its headers, first call:
40 * * #gst_h264_parser_identify_nalu to identify a NAL unit in an Annex B type bitstream
42 * * #gst_h264_parser_identify_nalu_avc to identify a NAL unit in an AVC type bitstream
44 * The following functions are then available for parsing the structure of the
45 * #GstH264NalUnit, depending on the #GstH264NalUnitType:
47 * * From #GST_H264_NAL_SLICE to #GST_H264_NAL_SLICE_IDR: #gst_h264_parser_parse_slice_hdr
49 * * #GST_H264_NAL_SEI: #gst_h264_parser_parse_sei
51 * * #GST_H264_NAL_SPS: #gst_h264_parser_parse_sps
53 * * #GST_H264_NAL_PPS: #gst_h264_parser_parse_pps
55 * * Any other: #gst_h264_parser_parse_nal
57 * One of these functions *must* be called on every NAL unit in the bitstream,
58 * in order to keep the internal structures of the #GstH264NalParser up to
59 * date. It is legal to call #gst_h264_parser_parse_nal on NAL units of any
60 * type, if no special parsing of the current NAL unit is required by the
63 * For more details about the structures, look at the ITU-T H.264 and ISO/IEC 14496-10 – MPEG-4
64 * Part 10 specifications, available at:
66 * * ITU-T H.264: http://www.itu.int/rec/T-REC-H.264
68 * * ISO/IEC 14496-10: http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=56538
77 #include "gsth264parser.h"
79 #include <gst/base/gstbytereader.h>
80 #include <gst/base/gstbitreader.h>
83 #ifndef GST_DISABLE_GST_DEBUG
84 #define GST_CAT_DEFAULT gst_h264_debug_category_get()
85 static GstDebugCategory *
86 gst_h264_debug_category_get (void)
88 static gsize cat_gonce = 0;
90 if (g_once_init_enter (&cat_gonce)) {
91 GstDebugCategory *cat = NULL;
93 GST_DEBUG_CATEGORY_INIT (cat, "codecparsers_h264", 0, "h264 parse library");
95 g_once_init_leave (&cat_gonce, (gsize) cat);
98 return (GstDebugCategory *) cat_gonce;
100 #endif /* GST_DISABLE_GST_DEBUG */
102 /**** Default scaling_lists according to Table 7-2 *****/
103 static const guint8 default_4x4_intra[16] = {
104 6, 13, 13, 20, 20, 20, 28, 28, 28, 28, 32, 32,
108 static const guint8 default_4x4_inter[16] = {
109 10, 14, 14, 20, 20, 20, 24, 24, 24, 24, 27, 27,
113 static const guint8 default_8x8_intra[64] = {
114 6, 10, 10, 13, 11, 13, 16, 16, 16, 16, 18, 18,
115 18, 18, 18, 23, 23, 23, 23, 23, 23, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27,
116 27, 27, 27, 27, 27, 29, 29, 29, 29, 29, 29, 29, 31, 31, 31, 31, 31, 31, 33,
117 33, 33, 33, 33, 36, 36, 36, 36, 38, 38, 38, 40, 40, 42
120 static const guint8 default_8x8_inter[64] = {
121 9, 13, 13, 15, 13, 15, 17, 17, 17, 17, 19, 19,
122 19, 19, 19, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 24, 24, 24,
123 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27, 27, 27, 28,
124 28, 28, 28, 28, 30, 30, 30, 30, 32, 32, 32, 33, 33, 35
127 static const guint8 zigzag_8x8[64] = {
128 0, 1, 8, 16, 9, 2, 3, 10,
129 17, 24, 32, 25, 18, 11, 4, 5,
130 12, 19, 26, 33, 40, 48, 41, 34,
131 27, 20, 13, 6, 7, 14, 21, 28,
132 35, 42, 49, 56, 57, 50, 43, 36,
133 29, 22, 15, 23, 30, 37, 44, 51,
134 58, 59, 52, 45, 38, 31, 39, 46,
135 53, 60, 61, 54, 47, 55, 62, 63
138 static const guint8 zigzag_4x4[16] = {
150 /* Table E-1 - Meaning of sample aspect ratio indicator (1..16) */
151 static const PAR aspect_ratios[17] = {
172 #define EXTENDED_SAR 255
175 gst_h264_parser_get_sps (GstH264NalParser * nalparser, guint8 sps_id)
179 sps = &nalparser->sps[sps_id];
188 gst_h264_parser_get_pps (GstH264NalParser * nalparser, guint8 pps_id)
192 pps = &nalparser->pps[pps_id];
201 gst_h264_parse_nalu_header (GstH264NalUnit * nalu)
203 guint8 *data = nalu->data + nalu->offset;
204 guint8 svc_extension_flag;
210 nalu->type = (data[0] & 0x1f);
211 nalu->ref_idc = (data[0] & 0x60) >> 5;
212 nalu->idr_pic_flag = (nalu->type == 5 ? 1 : 0);
213 nalu->header_bytes = 1;
215 nalu->extension_type = GST_H264_NAL_EXTENSION_NONE;
217 switch (nalu->type) {
218 case GST_H264_NAL_PREFIX_UNIT:
219 case GST_H264_NAL_SLICE_EXT:
222 gst_bit_reader_init (&br, nalu->data + nalu->offset + nalu->header_bytes,
223 nalu->size - nalu->header_bytes);
225 svc_extension_flag = gst_bit_reader_get_bits_uint8_unchecked (&br, 1);
226 if (svc_extension_flag) { /* SVC */
228 nalu->extension_type = GST_H264_NAL_EXTENSION_SVC;
231 GstH264NalUnitExtensionMVC *const mvc = &nalu->extension.mvc;
233 nalu->extension_type = GST_H264_NAL_EXTENSION_MVC;
234 mvc->non_idr_flag = gst_bit_reader_get_bits_uint8_unchecked (&br, 1);
235 mvc->priority_id = gst_bit_reader_get_bits_uint8_unchecked (&br, 6);
236 mvc->view_id = gst_bit_reader_get_bits_uint16_unchecked (&br, 10);
237 mvc->temporal_id = gst_bit_reader_get_bits_uint8_unchecked (&br, 3);
238 mvc->anchor_pic_flag = gst_bit_reader_get_bits_uint8_unchecked (&br, 1);
239 mvc->inter_view_flag = gst_bit_reader_get_bits_uint8_unchecked (&br, 1);
241 /* Update IdrPicFlag (H.7.4.1.1) */
242 nalu->idr_pic_flag = !mvc->non_idr_flag;
244 nalu->header_bytes += 3;
250 GST_DEBUG ("Nal type %u, ref_idc %u", nalu->type, nalu->ref_idc);
256 * @dst_pps: The destination #GstH264PPS to copy into
257 * @src_pps: The source #GstH264PPS to copy from
259 * Copies @src_pps into @dst_pps.
261 * Returns: %TRUE if everything went fine, %FALSE otherwise
264 gst_h264_pps_copy (GstH264PPS * dst_pps, const GstH264PPS * src_pps)
266 g_return_val_if_fail (dst_pps != NULL, FALSE);
267 g_return_val_if_fail (src_pps != NULL, FALSE);
269 gst_h264_pps_clear (dst_pps);
273 if (src_pps->slice_group_id)
274 dst_pps->slice_group_id = g_memdup (src_pps->slice_group_id,
275 src_pps->pic_size_in_map_units_minus1 + 1);
280 /* Copy MVC-specific data for subset SPS header */
282 gst_h264_sps_mvc_copy (GstH264SPS * dst_sps, const GstH264SPS * src_sps)
284 GstH264SPSExtMVC *const dst_mvc = &dst_sps->extension.mvc;
285 const GstH264SPSExtMVC *const src_mvc = &src_sps->extension.mvc;
288 g_assert (dst_sps->extension_type == GST_H264_NAL_EXTENSION_MVC);
290 dst_mvc->num_views_minus1 = src_mvc->num_views_minus1;
291 dst_mvc->view = g_new0 (GstH264SPSExtMVCView, dst_mvc->num_views_minus1 + 1);
295 dst_mvc->view[0].view_id = src_mvc->view[0].view_id;
297 for (i = 1; i <= dst_mvc->num_views_minus1; i++) {
298 GstH264SPSExtMVCView *const dst_view = &dst_mvc->view[i];
299 const GstH264SPSExtMVCView *const src_view = &src_mvc->view[i];
301 dst_view->view_id = src_view->view_id;
303 dst_view->num_anchor_refs_l0 = src_view->num_anchor_refs_l0;
304 for (j = 0; j < dst_view->num_anchor_refs_l0; j++)
305 dst_view->anchor_ref_l0[j] = src_view->anchor_ref_l0[j];
307 dst_view->num_anchor_refs_l1 = src_view->num_anchor_refs_l1;
308 for (j = 0; j < dst_view->num_anchor_refs_l1; j++)
309 dst_view->anchor_ref_l1[j] = src_view->anchor_ref_l1[j];
311 dst_view->num_non_anchor_refs_l0 = src_view->num_non_anchor_refs_l0;
312 for (j = 0; j < dst_view->num_non_anchor_refs_l0; j++)
313 dst_view->non_anchor_ref_l0[j] = src_view->non_anchor_ref_l0[j];
315 dst_view->num_non_anchor_refs_l1 = src_view->num_non_anchor_refs_l1;
316 for (j = 0; j < dst_view->num_non_anchor_refs_l1; j++)
317 dst_view->non_anchor_ref_l1[j] = src_view->non_anchor_ref_l1[j];
320 dst_mvc->num_level_values_signalled_minus1 =
321 src_mvc->num_level_values_signalled_minus1;
322 dst_mvc->level_value = g_new0 (GstH264SPSExtMVCLevelValue,
323 dst_mvc->num_level_values_signalled_minus1 + 1);
324 if (!dst_mvc->level_value)
327 for (i = 0; i <= dst_mvc->num_level_values_signalled_minus1; i++) {
328 GstH264SPSExtMVCLevelValue *const dst_value = &dst_mvc->level_value[i];
329 const GstH264SPSExtMVCLevelValue *const src_value =
330 &src_mvc->level_value[i];
332 dst_value->level_idc = src_value->level_idc;
334 dst_value->num_applicable_ops_minus1 = src_value->num_applicable_ops_minus1;
335 dst_value->applicable_op = g_new0 (GstH264SPSExtMVCLevelValueOp,
336 dst_value->num_applicable_ops_minus1 + 1);
337 if (!dst_value->applicable_op)
340 for (j = 0; j <= dst_value->num_applicable_ops_minus1; j++) {
341 GstH264SPSExtMVCLevelValueOp *const dst_op = &dst_value->applicable_op[j];
342 const GstH264SPSExtMVCLevelValueOp *const src_op =
343 &src_value->applicable_op[j];
345 dst_op->temporal_id = src_op->temporal_id;
346 dst_op->num_target_views_minus1 = src_op->num_target_views_minus1;
347 dst_op->target_view_id =
348 g_new (guint16, dst_op->num_target_views_minus1 + 1);
349 if (!dst_op->target_view_id)
352 for (k = 0; k <= dst_op->num_target_views_minus1; k++)
353 dst_op->target_view_id[k] = src_op->target_view_id[k];
354 dst_op->num_views_minus1 = src_op->num_views_minus1;
362 * @dst_sps: The destination #GstH264SPS to copy into
363 * @src_sps: The source #GstH264SPS to copy from
365 * Copies @src_sps into @dst_sps.
367 * Returns: %TRUE if everything went fine, %FALSE otherwise
370 gst_h264_sps_copy (GstH264SPS * dst_sps, const GstH264SPS * src_sps)
372 g_return_val_if_fail (dst_sps != NULL, FALSE);
373 g_return_val_if_fail (src_sps != NULL, FALSE);
375 gst_h264_sps_clear (dst_sps);
379 switch (dst_sps->extension_type) {
380 case GST_H264_NAL_EXTENSION_MVC:
381 if (!gst_h264_sps_mvc_copy (dst_sps, src_sps))
388 /****** Parsing functions *****/
391 gst_h264_parse_hrd_parameters (GstH264HRDParams * hrd, NalReader * nr)
395 GST_DEBUG ("parsing \"HRD Parameters\"");
397 READ_UE_MAX (nr, hrd->cpb_cnt_minus1, 31);
398 READ_UINT8 (nr, hrd->bit_rate_scale, 4);
399 READ_UINT8 (nr, hrd->cpb_size_scale, 4);
401 for (sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1; sched_sel_idx++) {
402 READ_UE (nr, hrd->bit_rate_value_minus1[sched_sel_idx]);
403 READ_UE (nr, hrd->cpb_size_value_minus1[sched_sel_idx]);
404 READ_UINT8 (nr, hrd->cbr_flag[sched_sel_idx], 1);
407 READ_UINT8 (nr, hrd->initial_cpb_removal_delay_length_minus1, 5);
408 READ_UINT8 (nr, hrd->cpb_removal_delay_length_minus1, 5);
409 READ_UINT8 (nr, hrd->dpb_output_delay_length_minus1, 5);
410 READ_UINT8 (nr, hrd->time_offset_length, 5);
415 GST_WARNING ("error parsing \"HRD Parameters\"");
420 gst_h264_parse_vui_parameters (GstH264SPS * sps, NalReader * nr)
422 GstH264VUIParams *vui = &sps->vui_parameters;
424 GST_DEBUG ("parsing \"VUI Parameters\"");
426 /* set default values for fields that might not be present in the bitstream
427 and have valid defaults */
428 vui->video_format = 5;
429 vui->colour_primaries = 2;
430 vui->transfer_characteristics = 2;
431 vui->matrix_coefficients = 2;
433 READ_UINT8 (nr, vui->aspect_ratio_info_present_flag, 1);
434 if (vui->aspect_ratio_info_present_flag) {
435 READ_UINT8 (nr, vui->aspect_ratio_idc, 8);
436 if (vui->aspect_ratio_idc == EXTENDED_SAR) {
437 READ_UINT16 (nr, vui->sar_width, 16);
438 READ_UINT16 (nr, vui->sar_height, 16);
439 vui->par_n = vui->sar_width;
440 vui->par_d = vui->sar_height;
441 } else if (vui->aspect_ratio_idc <= 16) {
442 vui->par_n = aspect_ratios[vui->aspect_ratio_idc].par_n;
443 vui->par_d = aspect_ratios[vui->aspect_ratio_idc].par_d;
447 READ_UINT8 (nr, vui->overscan_info_present_flag, 1);
448 if (vui->overscan_info_present_flag)
449 READ_UINT8 (nr, vui->overscan_appropriate_flag, 1);
451 READ_UINT8 (nr, vui->video_signal_type_present_flag, 1);
452 if (vui->video_signal_type_present_flag) {
454 READ_UINT8 (nr, vui->video_format, 3);
455 READ_UINT8 (nr, vui->video_full_range_flag, 1);
456 READ_UINT8 (nr, vui->colour_description_present_flag, 1);
457 if (vui->colour_description_present_flag) {
458 READ_UINT8 (nr, vui->colour_primaries, 8);
459 READ_UINT8 (nr, vui->transfer_characteristics, 8);
460 READ_UINT8 (nr, vui->matrix_coefficients, 8);
464 READ_UINT8 (nr, vui->chroma_loc_info_present_flag, 1);
465 if (vui->chroma_loc_info_present_flag) {
466 READ_UE_MAX (nr, vui->chroma_sample_loc_type_top_field, 5);
467 READ_UE_MAX (nr, vui->chroma_sample_loc_type_bottom_field, 5);
470 READ_UINT8 (nr, vui->timing_info_present_flag, 1);
471 if (vui->timing_info_present_flag) {
472 READ_UINT32 (nr, vui->num_units_in_tick, 32);
473 if (vui->num_units_in_tick == 0)
474 GST_WARNING ("num_units_in_tick = 0 detected in stream "
475 "(incompliant to H.264 E.2.1).");
477 READ_UINT32 (nr, vui->time_scale, 32);
478 if (vui->time_scale == 0)
479 GST_WARNING ("time_scale = 0 detected in stream "
480 "(incompliant to H.264 E.2.1).");
482 READ_UINT8 (nr, vui->fixed_frame_rate_flag, 1);
485 READ_UINT8 (nr, vui->nal_hrd_parameters_present_flag, 1);
486 if (vui->nal_hrd_parameters_present_flag) {
487 if (!gst_h264_parse_hrd_parameters (&vui->nal_hrd_parameters, nr))
491 READ_UINT8 (nr, vui->vcl_hrd_parameters_present_flag, 1);
492 if (vui->vcl_hrd_parameters_present_flag) {
493 if (!gst_h264_parse_hrd_parameters (&vui->vcl_hrd_parameters, nr))
497 if (vui->nal_hrd_parameters_present_flag ||
498 vui->vcl_hrd_parameters_present_flag)
499 READ_UINT8 (nr, vui->low_delay_hrd_flag, 1);
501 READ_UINT8 (nr, vui->pic_struct_present_flag, 1);
502 READ_UINT8 (nr, vui->bitstream_restriction_flag, 1);
503 if (vui->bitstream_restriction_flag) {
504 READ_UINT8 (nr, vui->motion_vectors_over_pic_boundaries_flag, 1);
505 READ_UE (nr, vui->max_bytes_per_pic_denom);
506 READ_UE_MAX (nr, vui->max_bits_per_mb_denom, 16);
507 READ_UE_MAX (nr, vui->log2_max_mv_length_horizontal, 16);
508 READ_UE_MAX (nr, vui->log2_max_mv_length_vertical, 16);
509 READ_UE (nr, vui->num_reorder_frames);
510 READ_UE (nr, vui->max_dec_frame_buffering);
516 GST_WARNING ("error parsing \"VUI Parameters\"");
521 gst_h264_parser_parse_scaling_list (NalReader * nr,
522 guint8 scaling_lists_4x4[6][16], guint8 scaling_lists_8x8[6][64],
523 const guint8 fallback_4x4_inter[16], const guint8 fallback_4x4_intra[16],
524 const guint8 fallback_8x8_inter[64], const guint8 fallback_8x8_intra[64],
529 static const guint8 *default_lists[12] = {
530 default_4x4_intra, default_4x4_intra, default_4x4_intra,
531 default_4x4_inter, default_4x4_inter, default_4x4_inter,
532 default_8x8_intra, default_8x8_inter,
533 default_8x8_intra, default_8x8_inter,
534 default_8x8_intra, default_8x8_inter
537 GST_DEBUG ("parsing scaling lists");
539 for (i = 0; i < 12; i++) {
540 gboolean use_default = FALSE;
543 guint8 scaling_list_present_flag;
545 READ_UINT8 (nr, scaling_list_present_flag, 1);
546 if (scaling_list_present_flag) {
547 guint8 *scaling_list;
550 guint8 last_scale, next_scale;
553 scaling_list = scaling_lists_4x4[i];
556 scaling_list = scaling_lists_8x8[i - 6];
562 for (j = 0; j < size; j++) {
563 if (next_scale != 0) {
566 READ_SE (nr, delta_scale);
567 next_scale = (last_scale + delta_scale) & 0xff;
569 if (j == 0 && next_scale == 0) {
570 /* Use default scaling lists (7.4.2.1.1.1) */
571 memcpy (scaling_list, default_lists[i], size);
574 last_scale = scaling_list[j] =
575 (next_scale == 0) ? last_scale : next_scale;
585 memcpy (scaling_lists_4x4[0], fallback_4x4_intra, 16);
588 memcpy (scaling_lists_4x4[1], scaling_lists_4x4[0], 16);
591 memcpy (scaling_lists_4x4[2], scaling_lists_4x4[1], 16);
594 memcpy (scaling_lists_4x4[3], fallback_4x4_inter, 16);
597 memcpy (scaling_lists_4x4[4], scaling_lists_4x4[3], 16);
600 memcpy (scaling_lists_4x4[5], scaling_lists_4x4[4], 16);
603 memcpy (scaling_lists_8x8[0], fallback_8x8_intra, 64);
606 memcpy (scaling_lists_8x8[1], fallback_8x8_inter, 64);
609 memcpy (scaling_lists_8x8[2], scaling_lists_8x8[0], 64);
612 memcpy (scaling_lists_8x8[3], scaling_lists_8x8[1], 64);
615 memcpy (scaling_lists_8x8[4], scaling_lists_8x8[2], 64);
618 memcpy (scaling_lists_8x8[5], scaling_lists_8x8[3], 64);
630 GST_WARNING ("error parsing scaling lists");
635 slice_parse_ref_pic_list_modification_1 (GstH264SliceHdr * slice,
636 NalReader * nr, guint list, gboolean is_mvc)
638 GstH264RefPicListModification *entries;
639 guint8 *ref_pic_list_modification_flag, *n_ref_pic_list_modification;
640 guint32 modification_of_pic_nums_idc;
645 entries = slice->ref_pic_list_modification_l0;
646 max_entries = G_N_ELEMENTS (slice->ref_pic_list_modification_l0);
647 ref_pic_list_modification_flag = &slice->ref_pic_list_modification_flag_l0;
648 n_ref_pic_list_modification = &slice->n_ref_pic_list_modification_l0;
650 entries = slice->ref_pic_list_modification_l1;
651 max_entries = G_N_ELEMENTS (slice->ref_pic_list_modification_l1);
652 ref_pic_list_modification_flag = &slice->ref_pic_list_modification_flag_l1;
653 n_ref_pic_list_modification = &slice->n_ref_pic_list_modification_l1;
656 READ_UINT8 (nr, *ref_pic_list_modification_flag, 1);
657 if (*ref_pic_list_modification_flag) {
659 READ_UE (nr, modification_of_pic_nums_idc);
660 if (modification_of_pic_nums_idc == 0 ||
661 modification_of_pic_nums_idc == 1) {
662 READ_UE_MAX (nr, entries[i].value.abs_diff_pic_num_minus1,
663 slice->max_pic_num - 1);
664 } else if (modification_of_pic_nums_idc == 2) {
665 READ_UE (nr, entries[i].value.long_term_pic_num);
666 } else if (is_mvc && (modification_of_pic_nums_idc == 4 ||
667 modification_of_pic_nums_idc == 5)) {
668 READ_UE (nr, entries[i].value.abs_diff_view_idx_minus1);
670 entries[i++].modification_of_pic_nums_idc = modification_of_pic_nums_idc;
671 if (modification_of_pic_nums_idc == 3)
673 if (i >= max_entries)
677 *n_ref_pic_list_modification = i;
681 GST_WARNING ("error parsing \"Reference picture list %u modification\"",
687 slice_parse_ref_pic_list_modification (GstH264SliceHdr * slice, NalReader * nr,
690 if (!GST_H264_IS_I_SLICE (slice) && !GST_H264_IS_SI_SLICE (slice)) {
691 if (!slice_parse_ref_pic_list_modification_1 (slice, nr, 0, is_mvc))
695 if (GST_H264_IS_B_SLICE (slice)) {
696 if (!slice_parse_ref_pic_list_modification_1 (slice, nr, 1, is_mvc))
703 gst_h264_slice_parse_dec_ref_pic_marking (GstH264SliceHdr * slice,
704 GstH264NalUnit * nalu, NalReader * nr)
706 GstH264DecRefPicMarking *dec_ref_pic_m;
709 GST_DEBUG ("parsing \"Decoded reference picture marking\"");
711 start_pos = nal_reader_get_pos (nr);
713 dec_ref_pic_m = &slice->dec_ref_pic_marking;
715 if (nalu->idr_pic_flag) {
716 READ_UINT8 (nr, dec_ref_pic_m->no_output_of_prior_pics_flag, 1);
717 READ_UINT8 (nr, dec_ref_pic_m->long_term_reference_flag, 1);
719 READ_UINT8 (nr, dec_ref_pic_m->adaptive_ref_pic_marking_mode_flag, 1);
720 if (dec_ref_pic_m->adaptive_ref_pic_marking_mode_flag) {
721 guint32 mem_mgmt_ctrl_op;
722 GstH264RefPicMarking *refpicmarking;
724 dec_ref_pic_m->n_ref_pic_marking = 0;
727 &dec_ref_pic_m->ref_pic_marking[dec_ref_pic_m->n_ref_pic_marking];
729 READ_UE (nr, mem_mgmt_ctrl_op);
730 if (mem_mgmt_ctrl_op == 0)
733 refpicmarking->memory_management_control_operation = mem_mgmt_ctrl_op;
735 if (mem_mgmt_ctrl_op == 1 || mem_mgmt_ctrl_op == 3)
736 READ_UE (nr, refpicmarking->difference_of_pic_nums_minus1);
738 if (mem_mgmt_ctrl_op == 2)
739 READ_UE (nr, refpicmarking->long_term_pic_num);
741 if (mem_mgmt_ctrl_op == 3 || mem_mgmt_ctrl_op == 6)
742 READ_UE (nr, refpicmarking->long_term_frame_idx);
744 if (mem_mgmt_ctrl_op == 4)
745 READ_UE (nr, refpicmarking->max_long_term_frame_idx_plus1);
747 dec_ref_pic_m->n_ref_pic_marking++;
752 dec_ref_pic_m->bit_size = nal_reader_get_pos (nr) - start_pos;
757 GST_WARNING ("error parsing \"Decoded reference picture marking\"");
762 gst_h264_slice_parse_pred_weight_table (GstH264SliceHdr * slice,
763 NalReader * nr, guint8 chroma_array_type)
765 GstH264PredWeightTable *p;
766 gint16 default_luma_weight, default_chroma_weight;
769 GST_DEBUG ("parsing \"Prediction weight table\"");
771 p = &slice->pred_weight_table;
773 READ_UE_MAX (nr, p->luma_log2_weight_denom, 7);
774 /* set default values */
775 default_luma_weight = 1 << p->luma_log2_weight_denom;
776 for (i = 0; i < G_N_ELEMENTS (p->luma_weight_l0); i++)
777 p->luma_weight_l0[i] = default_luma_weight;
778 if (GST_H264_IS_B_SLICE (slice)) {
779 for (i = 0; i < G_N_ELEMENTS (p->luma_weight_l1); i++)
780 p->luma_weight_l1[i] = default_luma_weight;
783 if (chroma_array_type != 0) {
784 READ_UE_MAX (nr, p->chroma_log2_weight_denom, 7);
785 /* set default values */
786 default_chroma_weight = 1 << p->chroma_log2_weight_denom;
787 for (i = 0; i < G_N_ELEMENTS (p->chroma_weight_l0); i++) {
788 p->chroma_weight_l0[i][0] = default_chroma_weight;
789 p->chroma_weight_l0[i][1] = default_chroma_weight;
791 if (GST_H264_IS_B_SLICE (slice)) {
792 for (i = 0; i < G_N_ELEMENTS (p->chroma_weight_l1); i++) {
793 p->chroma_weight_l1[i][0] = default_chroma_weight;
794 p->chroma_weight_l1[i][1] = default_chroma_weight;
799 for (i = 0; i <= slice->num_ref_idx_l0_active_minus1; i++) {
800 guint8 luma_weight_l0_flag;
802 READ_UINT8 (nr, luma_weight_l0_flag, 1);
803 if (luma_weight_l0_flag) {
804 READ_SE_ALLOWED (nr, p->luma_weight_l0[i], -128, 127);
805 READ_SE_ALLOWED (nr, p->luma_offset_l0[i], -128, 127);
807 if (chroma_array_type != 0) {
808 guint8 chroma_weight_l0_flag;
811 READ_UINT8 (nr, chroma_weight_l0_flag, 1);
812 if (chroma_weight_l0_flag) {
813 for (j = 0; j < 2; j++) {
814 READ_SE_ALLOWED (nr, p->chroma_weight_l0[i][j], -128, 127);
815 READ_SE_ALLOWED (nr, p->chroma_offset_l0[i][j], -128, 127);
821 if (GST_H264_IS_B_SLICE (slice)) {
822 for (i = 0; i <= slice->num_ref_idx_l1_active_minus1; i++) {
823 guint8 luma_weight_l1_flag;
825 READ_UINT8 (nr, luma_weight_l1_flag, 1);
826 if (luma_weight_l1_flag) {
827 READ_SE_ALLOWED (nr, p->luma_weight_l1[i], -128, 127);
828 READ_SE_ALLOWED (nr, p->luma_offset_l1[i], -128, 127);
830 if (chroma_array_type != 0) {
831 guint8 chroma_weight_l1_flag;
834 READ_UINT8 (nr, chroma_weight_l1_flag, 1);
835 if (chroma_weight_l1_flag) {
836 for (j = 0; j < 2; j++) {
837 READ_SE_ALLOWED (nr, p->chroma_weight_l1[i][j], -128, 127);
838 READ_SE_ALLOWED (nr, p->chroma_offset_l1[i][j], -128, 127);
848 GST_WARNING ("error parsing \"Prediction weight table\"");
852 static GstH264ParserResult
853 gst_h264_parser_parse_buffering_period (GstH264NalParser * nalparser,
854 GstH264BufferingPeriod * per, NalReader * nr)
859 GST_DEBUG ("parsing \"Buffering period\"");
861 READ_UE_MAX (nr, sps_id, GST_H264_MAX_SPS_COUNT - 1);
862 sps = gst_h264_parser_get_sps (nalparser, sps_id);
864 GST_WARNING ("couldn't find associated sequence parameter set with id: %d",
866 return GST_H264_PARSER_BROKEN_LINK;
870 if (sps->vui_parameters_present_flag) {
871 GstH264VUIParams *vui = &sps->vui_parameters;
873 if (vui->nal_hrd_parameters_present_flag) {
874 GstH264HRDParams *hrd = &vui->nal_hrd_parameters;
875 const guint8 nbits = hrd->initial_cpb_removal_delay_length_minus1 + 1;
876 guint8 sched_sel_idx;
878 for (sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1;
880 READ_UINT32 (nr, per->nal_initial_cpb_removal_delay[sched_sel_idx],
883 per->nal_initial_cpb_removal_delay_offset[sched_sel_idx], nbits);
887 if (vui->vcl_hrd_parameters_present_flag) {
888 GstH264HRDParams *hrd = &vui->vcl_hrd_parameters;
889 const guint8 nbits = hrd->initial_cpb_removal_delay_length_minus1 + 1;
890 guint8 sched_sel_idx;
892 for (sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1;
894 READ_UINT32 (nr, per->vcl_initial_cpb_removal_delay[sched_sel_idx],
897 per->vcl_initial_cpb_removal_delay_offset[sched_sel_idx], nbits);
902 return GST_H264_PARSER_OK;
905 GST_WARNING ("error parsing \"Buffering period\"");
906 return GST_H264_PARSER_ERROR;
910 gst_h264_parse_clock_timestamp (GstH264ClockTimestamp * tim,
911 GstH264VUIParams * vui, NalReader * nr)
913 guint8 full_timestamp_flag;
914 guint8 time_offset_length;
916 GST_DEBUG ("parsing \"Clock timestamp\"");
919 tim->time_offset = 0;
921 READ_UINT8 (nr, tim->ct_type, 2);
922 READ_UINT8 (nr, tim->nuit_field_based_flag, 1);
923 READ_UINT8 (nr, tim->counting_type, 5);
924 READ_UINT8 (nr, full_timestamp_flag, 1);
925 READ_UINT8 (nr, tim->discontinuity_flag, 1);
926 READ_UINT8 (nr, tim->cnt_dropped_flag, 1);
927 READ_UINT8 (nr, tim->n_frames, 8);
929 if (full_timestamp_flag) {
930 tim->seconds_flag = TRUE;
931 READ_UINT8 (nr, tim->seconds_value, 6);
933 tim->minutes_flag = TRUE;
934 READ_UINT8 (nr, tim->minutes_value, 6);
936 tim->hours_flag = TRUE;
937 READ_UINT8 (nr, tim->hours_value, 5);
939 READ_UINT8 (nr, tim->seconds_flag, 1);
940 if (tim->seconds_flag) {
941 READ_UINT8 (nr, tim->seconds_value, 6);
942 READ_UINT8 (nr, tim->minutes_flag, 1);
943 if (tim->minutes_flag) {
944 READ_UINT8 (nr, tim->minutes_value, 6);
945 READ_UINT8 (nr, tim->hours_flag, 1);
947 READ_UINT8 (nr, tim->hours_value, 5);
952 time_offset_length = 24;
953 if (vui->nal_hrd_parameters_present_flag)
954 time_offset_length = vui->nal_hrd_parameters.time_offset_length;
955 else if (vui->vcl_hrd_parameters_present_flag)
956 time_offset_length = vui->vcl_hrd_parameters.time_offset_length;
958 if (time_offset_length > 0)
959 READ_UINT32 (nr, tim->time_offset, time_offset_length);
964 GST_WARNING ("error parsing \"Clock timestamp\"");
968 static GstH264ParserResult
969 gst_h264_parser_parse_pic_timing (GstH264NalParser * nalparser,
970 GstH264PicTiming * tim, NalReader * nr)
972 GstH264ParserResult error = GST_H264_PARSER_ERROR;
973 gboolean CpbDpbDelaysPresentFlag = FALSE;
974 gboolean pic_struct_present_flag = FALSE;
976 GST_DEBUG ("parsing \"Picture timing\"");
977 if (!nalparser->last_sps || !nalparser->last_sps->valid) {
978 GST_WARNING ("didn't get the associated sequence parameter set for the "
979 "current access unit");
980 error = GST_H264_PARSER_BROKEN_LINK;
984 if (nalparser->last_sps->vui_parameters_present_flag) {
985 GstH264VUIParams *vui = &nalparser->last_sps->vui_parameters;
987 CpbDpbDelaysPresentFlag = vui->nal_hrd_parameters_present_flag
988 || vui->vcl_hrd_parameters_present_flag;
989 tim->pic_struct_present_flag = pic_struct_present_flag =
990 vui->pic_struct_present_flag;
992 if (vui->nal_hrd_parameters_present_flag) {
993 READ_UINT32 (nr, tim->cpb_removal_delay,
994 vui->nal_hrd_parameters.cpb_removal_delay_length_minus1 + 1);
995 READ_UINT32 (nr, tim->dpb_output_delay,
996 vui->nal_hrd_parameters.dpb_output_delay_length_minus1 + 1);
997 } else if (vui->vcl_hrd_parameters_present_flag) {
998 READ_UINT32 (nr, tim->cpb_removal_delay,
999 vui->vcl_hrd_parameters.cpb_removal_delay_length_minus1 + 1);
1000 READ_UINT32 (nr, tim->dpb_output_delay,
1001 vui->vcl_hrd_parameters.dpb_output_delay_length_minus1 + 1);
1004 if (pic_struct_present_flag) {
1005 const guint8 num_clock_ts_table[9] = {
1006 1, 1, 1, 2, 2, 3, 3, 2, 3
1008 guint8 num_clock_num_ts;
1011 READ_UINT8 (nr, tim->pic_struct, 4);
1012 CHECK_ALLOWED ((gint8) tim->pic_struct, 0, 8);
1014 num_clock_num_ts = num_clock_ts_table[tim->pic_struct];
1015 for (i = 0; i < num_clock_num_ts; i++) {
1016 READ_UINT8 (nr, tim->clock_timestamp_flag[i], 1);
1017 if (tim->clock_timestamp_flag[i]) {
1018 if (!gst_h264_parse_clock_timestamp (&tim->clock_timestamp[i], vui,
1026 if (!CpbDpbDelaysPresentFlag && !pic_struct_present_flag) {
1028 ("Invalid pic_timing SEI NAL with neither CpbDpbDelays nor pic_struct");
1029 return GST_H264_PARSER_BROKEN_DATA;
1032 return GST_H264_PARSER_OK;
1035 GST_WARNING ("error parsing \"Picture timing\"");
1039 static GstH264ParserResult
1040 gst_h264_parser_parse_registered_user_data (GstH264NalParser * nalparser,
1041 GstH264RegisteredUserData * rud, NalReader * nr, guint payload_size)
1043 guint8 *data = NULL;
1049 if (payload_size < 2)
1050 return GST_H264_PARSER_ERROR;
1052 READ_UINT8 (nr, rud->country_code, 8);
1055 if (rud->country_code == 0xFF) {
1056 READ_UINT8 (nr, rud->country_code_extension, 8);
1059 rud->country_code_extension = 0;
1062 if (payload_size < 8)
1063 return GST_H264_PARSER_ERROR;
1065 data = g_malloc (payload_size);
1066 for (i = 0; i < payload_size / 8; ++i) {
1067 READ_UINT8 (nr, data[i], 8);
1070 GST_MEMDUMP ("SEI user data", data, payload_size / 8);
1073 rud->size = payload_size;
1074 return GST_H264_PARSER_OK;
1078 GST_WARNING ("error parsing \"Registered User Data\"");
1080 return GST_H264_PARSER_ERROR;
1084 static GstH264ParserResult
1085 gst_h264_parser_parse_recovery_point (GstH264NalParser * nalparser,
1086 GstH264RecoveryPoint * rp, NalReader * nr)
1088 GstH264SPS *const sps = nalparser->last_sps;
1090 GST_DEBUG ("parsing \"Recovery point\"");
1091 if (!sps || !sps->valid) {
1092 GST_WARNING ("didn't get the associated sequence paramater set for the "
1093 "current access unit");
1097 READ_UE_MAX (nr, rp->recovery_frame_cnt, sps->max_frame_num - 1);
1098 READ_UINT8 (nr, rp->exact_match_flag, 1);
1099 READ_UINT8 (nr, rp->broken_link_flag, 1);
1100 READ_UINT8 (nr, rp->changing_slice_group_idc, 2);
1102 return GST_H264_PARSER_OK;
1105 GST_WARNING ("error parsing \"Recovery point\"");
1106 return GST_H264_PARSER_ERROR;
1109 /* Parse SEI stereo_video_info() message */
1110 static GstH264ParserResult
1111 gst_h264_parser_parse_stereo_video_info (GstH264NalParser * nalparser,
1112 GstH264StereoVideoInfo * info, NalReader * nr)
1114 GST_DEBUG ("parsing \"Stereo Video info\"");
1116 READ_UINT8 (nr, info->field_views_flag, 1);
1117 if (info->field_views_flag) {
1118 READ_UINT8 (nr, info->top_field_is_left_view_flag, 1);
1120 READ_UINT8 (nr, info->current_frame_is_left_view_flag, 1);
1121 READ_UINT8 (nr, info->next_frame_is_second_view_flag, 1);
1123 READ_UINT8 (nr, info->left_view_self_contained_flag, 1);
1124 READ_UINT8 (nr, info->right_view_self_contained_flag, 1);
1126 return GST_H264_PARSER_OK;
1129 GST_WARNING ("error parsing \"Stereo Video info\"");
1130 return GST_H264_PARSER_ERROR;
1133 /* Parse SEI frame_packing_arrangement() message */
1134 static GstH264ParserResult
1135 gst_h264_parser_parse_frame_packing (GstH264NalParser * nalparser,
1136 GstH264FramePacking * frame_packing, NalReader * nr, guint payload_size)
1138 guint8 frame_packing_extension_flag;
1141 GST_DEBUG ("parsing \"Frame Packing Arrangement\"");
1143 start_pos = nal_reader_get_pos (nr);
1144 READ_UE (nr, frame_packing->frame_packing_id);
1145 READ_UINT8 (nr, frame_packing->frame_packing_cancel_flag, 1);
1147 if (!frame_packing->frame_packing_cancel_flag) {
1148 READ_UINT8 (nr, frame_packing->frame_packing_type, 7);
1149 READ_UINT8 (nr, frame_packing->quincunx_sampling_flag, 1);
1150 READ_UINT8 (nr, frame_packing->content_interpretation_type, 6);
1151 READ_UINT8 (nr, frame_packing->spatial_flipping_flag, 1);
1152 READ_UINT8 (nr, frame_packing->frame0_flipped_flag, 1);
1153 READ_UINT8 (nr, frame_packing->field_views_flag, 1);
1154 READ_UINT8 (nr, frame_packing->current_frame_is_frame0_flag, 1);
1155 READ_UINT8 (nr, frame_packing->frame0_self_contained_flag, 1);
1156 READ_UINT8 (nr, frame_packing->frame1_self_contained_flag, 1);
1158 if (!frame_packing->quincunx_sampling_flag &&
1159 frame_packing->frame_packing_type !=
1160 GST_H264_FRAME_PACKING_TEMPORAL_INTERLEAVING) {
1161 READ_UINT8 (nr, frame_packing->frame0_grid_position_x, 4);
1162 READ_UINT8 (nr, frame_packing->frame0_grid_position_y, 4);
1163 READ_UINT8 (nr, frame_packing->frame1_grid_position_x, 4);
1164 READ_UINT8 (nr, frame_packing->frame1_grid_position_y, 4);
1167 /* Skip frame_packing_arrangement_reserved_byte */
1168 if (!nal_reader_skip (nr, 8))
1171 READ_UE_MAX (nr, frame_packing->frame_packing_repetition_period, 16384);
1174 READ_UINT8 (nr, frame_packing_extension_flag, 1);
1176 /* All data that follows within a frame packing arrangement SEI message
1177 after the value 1 for frame_packing_arrangement_extension_flag shall
1178 be ignored (D.2.25) */
1179 if (frame_packing_extension_flag) {
1180 nal_reader_skip_long (nr,
1181 payload_size - (nal_reader_get_pos (nr) - start_pos));
1184 return GST_H264_PARSER_OK;
1187 GST_WARNING ("error parsing \"Frame Packing Arrangement\"");
1188 return GST_H264_PARSER_ERROR;
1191 static GstH264ParserResult
1192 gst_h264_parser_parse_sei_message (GstH264NalParser * nalparser,
1193 NalReader * nr, GstH264SEIMessage * sei)
1195 guint32 payloadSize;
1196 guint8 payload_type_byte, payload_size_byte;
1197 guint remaining, payload_size, next;
1198 GstH264ParserResult res;
1200 GST_DEBUG ("parsing \"SEI message\"");
1202 memset (sei, 0, sizeof (*sei));
1205 READ_UINT8 (nr, payload_type_byte, 8);
1206 sei->payloadType += payload_type_byte;
1207 } while (payload_type_byte == 0xff);
1211 READ_UINT8 (nr, payload_size_byte, 8);
1212 payloadSize += payload_size_byte;
1214 while (payload_size_byte == 0xff);
1216 remaining = nal_reader_get_remaining (nr);
1217 payload_size = payloadSize * 8 < remaining ? payloadSize * 8 : remaining;
1218 next = nal_reader_get_pos (nr) + payload_size;
1220 GST_DEBUG ("SEI message received: payloadType %u, payloadSize = %u bits",
1221 sei->payloadType, payload_size);
1223 switch (sei->payloadType) {
1224 case GST_H264_SEI_BUF_PERIOD:
1225 /* size not set; might depend on emulation_prevention_three_byte */
1226 res = gst_h264_parser_parse_buffering_period (nalparser,
1227 &sei->payload.buffering_period, nr);
1229 case GST_H264_SEI_PIC_TIMING:
1230 /* size not set; might depend on emulation_prevention_three_byte */
1231 res = gst_h264_parser_parse_pic_timing (nalparser,
1232 &sei->payload.pic_timing, nr);
1234 case GST_H264_SEI_REGISTERED_USER_DATA:
1235 res = gst_h264_parser_parse_registered_user_data (nalparser,
1236 &sei->payload.registered_user_data, nr, payload_size);
1238 case GST_H264_SEI_RECOVERY_POINT:
1239 res = gst_h264_parser_parse_recovery_point (nalparser,
1240 &sei->payload.recovery_point, nr);
1242 case GST_H264_SEI_STEREO_VIDEO_INFO:
1243 res = gst_h264_parser_parse_stereo_video_info (nalparser,
1244 &sei->payload.stereo_video_info, nr);
1246 case GST_H264_SEI_FRAME_PACKING:
1247 res = gst_h264_parser_parse_frame_packing (nalparser,
1248 &sei->payload.frame_packing, nr, payload_size);
1251 /* Just consume payloadSize bytes, which does not account for
1252 emulation prevention bytes */
1253 if (!nal_reader_skip_long (nr, payload_size))
1255 res = GST_H264_PARSER_OK;
1259 /* When SEI message doesn't end at byte boundary,
1260 * check remaining bits fit the specification.
1262 if (!nal_reader_is_byte_aligned (nr)) {
1263 guint8 bit_equal_to_one;
1264 READ_UINT8 (nr, bit_equal_to_one, 1);
1265 if (!bit_equal_to_one)
1266 GST_WARNING ("Bit non equal to one.");
1268 while (!nal_reader_is_byte_aligned (nr)) {
1269 guint8 bit_equal_to_zero;
1270 READ_UINT8 (nr, bit_equal_to_zero, 1);
1271 if (bit_equal_to_zero)
1272 GST_WARNING ("Bit non equal to zero.");
1276 /* Always make sure all the advertised SEI bits
1277 * were consumed during parsing */
1278 if (next > nal_reader_get_pos (nr)) {
1279 GST_LOG ("Skipping %u unused SEI bits", next - nal_reader_get_pos (nr));
1281 if (!nal_reader_skip_long (nr, next - nal_reader_get_pos (nr)))
1288 GST_WARNING ("error parsing \"Sei message\"");
1289 return GST_H264_PARSER_ERROR;
1292 /******** API *************/
1295 * gst_h264_nal_parser_new:
1297 * Creates a new #GstH264NalParser. It should be freed with
1298 * gst_h264_nal_parser_free after use.
1300 * Returns: a new #GstH264NalParser
1303 gst_h264_nal_parser_new (void)
1305 GstH264NalParser *nalparser;
1307 nalparser = g_slice_new0 (GstH264NalParser);
1313 * gst_h264_nal_parser_free:
1314 * @nalparser: the #GstH264NalParser to free
1316 * Frees @nalparser and sets it to %NULL
1319 gst_h264_nal_parser_free (GstH264NalParser * nalparser)
1323 for (i = 0; i < GST_H264_MAX_SPS_COUNT; i++)
1324 gst_h264_sps_clear (&nalparser->sps[i]);
1325 for (i = 0; i < GST_H264_MAX_PPS_COUNT; i++)
1326 gst_h264_pps_clear (&nalparser->pps[i]);
1327 g_slice_free (GstH264NalParser, nalparser);
1333 * gst_h264_parser_identify_nalu_unchecked:
1334 * @nalparser: a #GstH264NalParser
1335 * @data: The data to parse
1336 * @offset: the offset from which to parse @data
1337 * @size: the size of @data
1338 * @nalu: The #GstH264NalUnit where to store parsed nal headers
1340 * Parses @data and fills @nalu from the next nalu data from @data.
1342 * This differs from @gst_h264_parser_identify_nalu in that it doesn't
1343 * check whether the packet is complete or not.
1345 * Note: Only use this function if you already know the provided @data
1346 * is a complete NALU, else use @gst_h264_parser_identify_nalu.
1348 * Returns: a #GstH264ParserResult
1351 gst_h264_parser_identify_nalu_unchecked (GstH264NalParser * nalparser,
1352 const guint8 * data, guint offset, gsize size, GstH264NalUnit * nalu)
1356 memset (nalu, 0, sizeof (*nalu));
1358 if (size < offset + 4) {
1359 GST_DEBUG ("Can't parse, buffer has too small size %" G_GSIZE_FORMAT
1360 ", offset %u", size, offset);
1361 return GST_H264_PARSER_ERROR;
1364 off1 = scan_for_start_codes (data + offset, size - offset);
1367 GST_DEBUG ("No start code prefix in this buffer");
1368 return GST_H264_PARSER_NO_NAL;
1371 if (offset + off1 == size - 1) {
1372 GST_DEBUG ("Missing data to identify nal unit");
1374 return GST_H264_PARSER_ERROR;
1377 nalu->sc_offset = offset + off1;
1380 nalu->offset = offset + off1 + 3;
1381 nalu->data = (guint8 *) data;
1382 nalu->size = size - nalu->offset;
1384 if (!gst_h264_parse_nalu_header (nalu)) {
1385 GST_WARNING ("error parsing \"NAL unit header\"");
1387 return GST_H264_PARSER_BROKEN_DATA;
1392 /* sc might have 2 or 3 0-bytes */
1393 if (nalu->sc_offset > 0 && data[nalu->sc_offset - 1] == 00
1394 && (nalu->type == GST_H264_NAL_SPS || nalu->type == GST_H264_NAL_PPS
1395 || nalu->type == GST_H264_NAL_AU_DELIMITER))
1398 if (nalu->type == GST_H264_NAL_SEQ_END ||
1399 nalu->type == GST_H264_NAL_STREAM_END) {
1400 GST_DEBUG ("end-of-seq or end-of-stream nal found");
1402 return GST_H264_PARSER_OK;
1405 return GST_H264_PARSER_OK;
1409 * gst_h264_parser_identify_nalu:
1410 * @nalparser: a #GstH264NalParser
1411 * @data: The data to parse, containing an Annex B coded NAL unit
1412 * @offset: the offset in @data from which to parse the NAL unit
1413 * @size: the size of @data
1414 * @nalu: The #GstH264NalUnit to store the identified NAL unit in
1416 * Parses the headers of an Annex B coded NAL unit from @data and puts the
1417 * result into @nalu.
1419 * Returns: a #GstH264ParserResult
1422 gst_h264_parser_identify_nalu (GstH264NalParser * nalparser,
1423 const guint8 * data, guint offset, gsize size, GstH264NalUnit * nalu)
1425 GstH264ParserResult res;
1429 gst_h264_parser_identify_nalu_unchecked (nalparser, data, offset, size,
1432 if (res != GST_H264_PARSER_OK)
1435 /* The two NALs are exactly 1 byte size and are placed at the end of an AU,
1436 * there is no need to wait for the following */
1437 if (nalu->type == GST_H264_NAL_SEQ_END ||
1438 nalu->type == GST_H264_NAL_STREAM_END)
1441 off2 = scan_for_start_codes (data + nalu->offset, size - nalu->offset);
1443 GST_DEBUG ("Nal start %d, No end found", nalu->offset);
1445 return GST_H264_PARSER_NO_NAL_END;
1448 /* Mini performance improvement:
1449 * We could have a way to store how many 0s were skipped to avoid
1450 * parsing them again on the next NAL */
1451 while (off2 > 0 && data[nalu->offset + off2 - 1] == 00)
1456 return GST_H264_PARSER_BROKEN_DATA;
1458 GST_DEBUG ("Complete nal found. Off: %d, Size: %d", nalu->offset, nalu->size);
1466 * gst_h264_parser_identify_nalu_avc:
1467 * @nalparser: a #GstH264NalParser
1468 * @data: The data to parse, containing an AVC coded NAL unit
1469 * @offset: the offset in @data from which to parse the NAL unit
1470 * @size: the size of @data
1471 * @nal_length_size: the size in bytes of the AVC nal length prefix.
1472 * @nalu: The #GstH264NalUnit to store the identified NAL unit in
1474 * Parses the headers of an AVC coded NAL unit from @data and puts the result
1477 * Returns: a #GstH264ParserResult
1480 gst_h264_parser_identify_nalu_avc (GstH264NalParser * nalparser,
1481 const guint8 * data, guint offset, gsize size, guint8 nal_length_size,
1482 GstH264NalUnit * nalu)
1486 memset (nalu, 0, sizeof (*nalu));
1488 if (size < offset + nal_length_size) {
1489 GST_DEBUG ("Can't parse, buffer has too small size %" G_GSIZE_FORMAT
1490 ", offset %u", size, offset);
1491 return GST_H264_PARSER_ERROR;
1494 size = size - offset;
1495 gst_bit_reader_init (&br, data + offset, size);
1497 nalu->size = gst_bit_reader_get_bits_uint32_unchecked (&br,
1498 nal_length_size * 8);
1499 nalu->sc_offset = offset;
1500 nalu->offset = offset + nal_length_size;
1502 if (size < nalu->size + nal_length_size) {
1505 return GST_H264_PARSER_NO_NAL_END;
1508 nalu->data = (guint8 *) data;
1510 if (!gst_h264_parse_nalu_header (nalu)) {
1511 GST_WARNING ("error parsing \"NAL unit header\"");
1513 return GST_H264_PARSER_BROKEN_DATA;
1518 return GST_H264_PARSER_OK;
1522 * gst_h264_parser_parse_nal:
1523 * @nalparser: a #GstH264NalParser
1524 * @nalu: The #GstH264NalUnit to parse
1526 * This function should be called in the case one doesn't need to
1527 * parse a specific structure. It is necessary to do so to make
1528 * sure @nalparser is up to date.
1530 * Returns: a #GstH264ParserResult
1533 gst_h264_parser_parse_nal (GstH264NalParser * nalparser, GstH264NalUnit * nalu)
1538 switch (nalu->type) {
1539 case GST_H264_NAL_SPS:
1540 return gst_h264_parser_parse_sps (nalparser, nalu, &sps);
1542 case GST_H264_NAL_PPS:
1543 return gst_h264_parser_parse_pps (nalparser, nalu, &pps);
1546 return GST_H264_PARSER_OK;
1550 * gst_h264_parser_parse_sps:
1551 * @nalparser: a #GstH264NalParser
1552 * @nalu: The #GST_H264_NAL_SPS #GstH264NalUnit to parse
1553 * @sps: The #GstH264SPS to fill.
1555 * Parses @nalu containing a Sequence Parameter Set, and fills @sps.
1557 * Returns: a #GstH264ParserResult
1560 gst_h264_parser_parse_sps (GstH264NalParser * nalparser, GstH264NalUnit * nalu,
1563 GstH264ParserResult res = gst_h264_parse_sps (nalu, sps);
1565 if (res == GST_H264_PARSER_OK) {
1566 GST_DEBUG ("adding sequence parameter set with id: %d to array", sps->id);
1568 if (!gst_h264_sps_copy (&nalparser->sps[sps->id], sps))
1569 return GST_H264_PARSER_ERROR;
1570 nalparser->last_sps = &nalparser->sps[sps->id];
1575 /* Parse seq_parameter_set_data() */
1577 gst_h264_parse_sps_data (NalReader * nr, GstH264SPS * sps)
1580 guint subwc[] = { 1, 2, 2, 1 };
1581 guint subhc[] = { 1, 2, 1, 1 };
1583 memset (sps, 0, sizeof (*sps));
1585 /* set default values for fields that might not be present in the bitstream
1586 and have valid defaults */
1587 sps->extension_type = GST_H264_NAL_EXTENSION_NONE;
1588 sps->chroma_format_idc = 1;
1589 memset (sps->scaling_lists_4x4, 16, 96);
1590 memset (sps->scaling_lists_8x8, 16, 384);
1592 READ_UINT8 (nr, sps->profile_idc, 8);
1593 READ_UINT8 (nr, sps->constraint_set0_flag, 1);
1594 READ_UINT8 (nr, sps->constraint_set1_flag, 1);
1595 READ_UINT8 (nr, sps->constraint_set2_flag, 1);
1596 READ_UINT8 (nr, sps->constraint_set3_flag, 1);
1597 READ_UINT8 (nr, sps->constraint_set4_flag, 1);
1598 READ_UINT8 (nr, sps->constraint_set5_flag, 1);
1600 /* skip reserved_zero_2bits */
1601 if (!nal_reader_skip (nr, 2))
1604 READ_UINT8 (nr, sps->level_idc, 8);
1606 READ_UE_MAX (nr, sps->id, GST_H264_MAX_SPS_COUNT - 1);
1608 if (sps->profile_idc == 100 || sps->profile_idc == 110 ||
1609 sps->profile_idc == 122 || sps->profile_idc == 244 ||
1610 sps->profile_idc == 44 || sps->profile_idc == 83 ||
1611 sps->profile_idc == 86 || sps->profile_idc == 118 ||
1612 sps->profile_idc == 128 || sps->profile_idc == 138 ||
1613 sps->profile_idc == 139 || sps->profile_idc == 134 ||
1614 sps->profile_idc == 135) {
1615 READ_UE_MAX (nr, sps->chroma_format_idc, 3);
1616 if (sps->chroma_format_idc == 3)
1617 READ_UINT8 (nr, sps->separate_colour_plane_flag, 1);
1619 READ_UE_MAX (nr, sps->bit_depth_luma_minus8, 6);
1620 READ_UE_MAX (nr, sps->bit_depth_chroma_minus8, 6);
1621 READ_UINT8 (nr, sps->qpprime_y_zero_transform_bypass_flag, 1);
1623 READ_UINT8 (nr, sps->scaling_matrix_present_flag, 1);
1624 if (sps->scaling_matrix_present_flag) {
1627 n_lists = (sps->chroma_format_idc != 3) ? 8 : 12;
1628 if (!gst_h264_parser_parse_scaling_list (nr,
1629 sps->scaling_lists_4x4, sps->scaling_lists_8x8,
1630 default_4x4_inter, default_4x4_intra,
1631 default_8x8_inter, default_8x8_intra, n_lists))
1636 READ_UE_MAX (nr, sps->log2_max_frame_num_minus4, 12);
1638 sps->max_frame_num = 1 << (sps->log2_max_frame_num_minus4 + 4);
1640 READ_UE_MAX (nr, sps->pic_order_cnt_type, 2);
1641 if (sps->pic_order_cnt_type == 0) {
1642 READ_UE_MAX (nr, sps->log2_max_pic_order_cnt_lsb_minus4, 12);
1643 } else if (sps->pic_order_cnt_type == 1) {
1646 READ_UINT8 (nr, sps->delta_pic_order_always_zero_flag, 1);
1647 READ_SE (nr, sps->offset_for_non_ref_pic);
1648 READ_SE (nr, sps->offset_for_top_to_bottom_field);
1649 READ_UE_MAX (nr, sps->num_ref_frames_in_pic_order_cnt_cycle, 255);
1651 for (i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; i++)
1652 READ_SE (nr, sps->offset_for_ref_frame[i]);
1655 READ_UE (nr, sps->num_ref_frames);
1656 READ_UINT8 (nr, sps->gaps_in_frame_num_value_allowed_flag, 1);
1657 READ_UE (nr, sps->pic_width_in_mbs_minus1);
1658 READ_UE (nr, sps->pic_height_in_map_units_minus1);
1659 READ_UINT8 (nr, sps->frame_mbs_only_flag, 1);
1661 if (!sps->frame_mbs_only_flag)
1662 READ_UINT8 (nr, sps->mb_adaptive_frame_field_flag, 1);
1664 READ_UINT8 (nr, sps->direct_8x8_inference_flag, 1);
1665 READ_UINT8 (nr, sps->frame_cropping_flag, 1);
1666 if (sps->frame_cropping_flag) {
1667 READ_UE (nr, sps->frame_crop_left_offset);
1668 READ_UE (nr, sps->frame_crop_right_offset);
1669 READ_UE (nr, sps->frame_crop_top_offset);
1670 READ_UE (nr, sps->frame_crop_bottom_offset);
1673 READ_UINT8 (nr, sps->vui_parameters_present_flag, 1);
1674 if (sps->vui_parameters_present_flag)
1675 if (!gst_h264_parse_vui_parameters (sps, nr))
1678 /* calculate ChromaArrayType */
1679 if (!sps->separate_colour_plane_flag)
1680 sps->chroma_array_type = sps->chroma_format_idc;
1682 /* Calculate width and height */
1683 width = (sps->pic_width_in_mbs_minus1 + 1);
1685 height = (sps->pic_height_in_map_units_minus1 + 1);
1686 height *= 16 * (2 - sps->frame_mbs_only_flag);
1687 GST_LOG ("initial width=%d, height=%d", width, height);
1688 if (width < 0 || height < 0) {
1689 GST_WARNING ("invalid width/height in SPS");
1694 sps->height = height;
1696 if (sps->frame_cropping_flag) {
1697 const guint crop_unit_x = subwc[sps->chroma_format_idc];
1698 const guint crop_unit_y =
1699 subhc[sps->chroma_format_idc] * (2 - sps->frame_mbs_only_flag);
1701 width -= (sps->frame_crop_left_offset + sps->frame_crop_right_offset)
1703 height -= (sps->frame_crop_top_offset + sps->frame_crop_bottom_offset)
1706 sps->crop_rect_width = width;
1707 sps->crop_rect_height = height;
1708 sps->crop_rect_x = sps->frame_crop_left_offset * crop_unit_x;
1709 sps->crop_rect_y = sps->frame_crop_top_offset * crop_unit_y;
1711 GST_LOG ("crop_rectangle x=%u y=%u width=%u, height=%u", sps->crop_rect_x,
1712 sps->crop_rect_y, width, height);
1715 sps->fps_num_removed = 0;
1716 sps->fps_den_removed = 1;
1724 /* Parse subset_seq_parameter_set() data for MVC */
1726 gst_h264_parse_sps_mvc_data (NalReader * nr, GstH264SPS * sps)
1728 GstH264SPSExtMVC *const mvc = &sps->extension.mvc;
1729 guint8 bit_equal_to_one;
1732 READ_UINT8 (nr, bit_equal_to_one, 1);
1733 if (!bit_equal_to_one)
1736 sps->extension_type = GST_H264_NAL_EXTENSION_MVC;
1738 READ_UE_MAX (nr, mvc->num_views_minus1, GST_H264_MAX_VIEW_COUNT - 1);
1740 mvc->view = g_new0 (GstH264SPSExtMVCView, mvc->num_views_minus1 + 1);
1742 goto error_allocation_failed;
1744 for (i = 0; i <= mvc->num_views_minus1; i++)
1745 READ_UE_MAX (nr, mvc->view[i].view_id, GST_H264_MAX_VIEW_ID);
1747 for (i = 1; i <= mvc->num_views_minus1; i++) {
1748 /* for RefPicList0 */
1749 READ_UE_MAX (nr, mvc->view[i].num_anchor_refs_l0, 15);
1750 for (j = 0; j < mvc->view[i].num_anchor_refs_l0; j++) {
1751 READ_UE_MAX (nr, mvc->view[i].anchor_ref_l0[j], GST_H264_MAX_VIEW_ID);
1754 /* for RefPicList1 */
1755 READ_UE_MAX (nr, mvc->view[i].num_anchor_refs_l1, 15);
1756 for (j = 0; j < mvc->view[i].num_anchor_refs_l1; j++) {
1757 READ_UE_MAX (nr, mvc->view[i].anchor_ref_l1[j], GST_H264_MAX_VIEW_ID);
1761 for (i = 1; i <= mvc->num_views_minus1; i++) {
1762 /* for RefPicList0 */
1763 READ_UE_MAX (nr, mvc->view[i].num_non_anchor_refs_l0, 15);
1764 for (j = 0; j < mvc->view[i].num_non_anchor_refs_l0; j++) {
1765 READ_UE_MAX (nr, mvc->view[i].non_anchor_ref_l0[j], GST_H264_MAX_VIEW_ID);
1768 /* for RefPicList1 */
1769 READ_UE_MAX (nr, mvc->view[i].num_non_anchor_refs_l1, 15);
1770 for (j = 0; j < mvc->view[i].num_non_anchor_refs_l1; j++) {
1771 READ_UE_MAX (nr, mvc->view[i].non_anchor_ref_l1[j], GST_H264_MAX_VIEW_ID);
1775 READ_UE_MAX (nr, mvc->num_level_values_signalled_minus1, 63);
1778 g_new0 (GstH264SPSExtMVCLevelValue,
1779 mvc->num_level_values_signalled_minus1 + 1);
1780 if (!mvc->level_value)
1781 goto error_allocation_failed;
1783 for (i = 0; i <= mvc->num_level_values_signalled_minus1; i++) {
1784 GstH264SPSExtMVCLevelValue *const level_value = &mvc->level_value[i];
1786 READ_UINT8 (nr, level_value->level_idc, 8);
1788 READ_UE_MAX (nr, level_value->num_applicable_ops_minus1, 1023);
1789 level_value->applicable_op =
1790 g_new0 (GstH264SPSExtMVCLevelValueOp,
1791 level_value->num_applicable_ops_minus1 + 1);
1792 if (!level_value->applicable_op)
1793 goto error_allocation_failed;
1795 for (j = 0; j <= level_value->num_applicable_ops_minus1; j++) {
1796 GstH264SPSExtMVCLevelValueOp *const op = &level_value->applicable_op[j];
1798 READ_UINT8 (nr, op->temporal_id, 3);
1800 READ_UE_MAX (nr, op->num_target_views_minus1, 1023);
1801 op->target_view_id = g_new (guint16, op->num_target_views_minus1 + 1);
1802 if (!op->target_view_id)
1803 goto error_allocation_failed;
1805 for (k = 0; k <= op->num_target_views_minus1; k++)
1806 READ_UE_MAX (nr, op->target_view_id[k], GST_H264_MAX_VIEW_ID);
1807 READ_UE_MAX (nr, op->num_views_minus1, 1023);
1812 error_allocation_failed:
1813 GST_WARNING ("failed to allocate memory");
1814 gst_h264_sps_clear (sps);
1818 gst_h264_sps_clear (sps);
1823 * gst_h264_parse_sps:
1824 * @nalu: The #GST_H264_NAL_SPS #GstH264NalUnit to parse
1825 * @sps: The #GstH264SPS to fill.
1827 * Parses @data, and fills the @sps structure.
1829 * Returns: a #GstH264ParserResult
1832 gst_h264_parse_sps (GstH264NalUnit * nalu, GstH264SPS * sps)
1836 GST_DEBUG ("parsing SPS");
1838 nal_reader_init (&nr, nalu->data + nalu->offset + nalu->header_bytes,
1839 nalu->size - nalu->header_bytes);
1841 if (!gst_h264_parse_sps_data (&nr, sps))
1846 return GST_H264_PARSER_OK;
1849 GST_WARNING ("error parsing \"Sequence parameter set\"");
1851 return GST_H264_PARSER_ERROR;
1855 * gst_h264_parser_parse_subset_sps:
1856 * @nalparser: a #GstH264NalParser
1857 * @nalu: The #GST_H264_NAL_SUBSET_SPS #GstH264NalUnit to parse
1858 * @sps: The #GstH264SPS to fill.
1860 * Parses @data, and fills in the @sps structure.
1862 * This function fully parses @data and allocates all the necessary
1863 * data structures needed for MVC extensions. The resulting @sps
1864 * structure shall be deallocated with gst_h264_sps_clear() when it is
1867 * Note: if the caller doesn't need any of the MVC-specific data, then
1868 * gst_h264_parser_parse_sps() is more efficient because those extra
1869 * syntax elements are not parsed and no extra memory is allocated.
1871 * Returns: a #GstH264ParserResult
1876 gst_h264_parser_parse_subset_sps (GstH264NalParser * nalparser,
1877 GstH264NalUnit * nalu, GstH264SPS * sps)
1879 GstH264ParserResult res;
1881 res = gst_h264_parse_subset_sps (nalu, sps);
1882 if (res == GST_H264_PARSER_OK) {
1883 GST_DEBUG ("adding sequence parameter set with id: %d to array", sps->id);
1885 if (!gst_h264_sps_copy (&nalparser->sps[sps->id], sps)) {
1886 gst_h264_sps_clear (sps);
1887 return GST_H264_PARSER_ERROR;
1889 nalparser->last_sps = &nalparser->sps[sps->id];
1895 * gst_h264_parse_subset_sps:
1896 * @nalu: The #GST_H264_NAL_SUBSET_SPS #GstH264NalUnit to parse
1897 * @sps: The #GstH264SPS to fill.
1899 * Parses @data, and fills in the @sps structure.
1901 * This function fully parses @data and allocates all the necessary
1902 * data structures needed for MVC extensions. The resulting @sps
1903 * structure shall be deallocated with gst_h264_sps_clear() when it is
1906 * Note: if the caller doesn't need any of the MVC-specific data, then
1907 * gst_h264_parser_parse_sps() is more efficient because those extra
1908 * syntax elements are not parsed and no extra memory is allocated.
1910 * Returns: a #GstH264ParserResult
1915 gst_h264_parse_subset_sps (GstH264NalUnit * nalu, GstH264SPS * sps)
1919 GST_DEBUG ("parsing Subset SPS");
1921 nal_reader_init (&nr, nalu->data + nalu->offset + nalu->header_bytes,
1922 nalu->size - nalu->header_bytes);
1924 if (!gst_h264_parse_sps_data (&nr, sps))
1927 if (sps->profile_idc == GST_H264_PROFILE_MULTIVIEW_HIGH ||
1928 sps->profile_idc == GST_H264_PROFILE_STEREO_HIGH) {
1929 if (!gst_h264_parse_sps_mvc_data (&nr, sps))
1934 return GST_H264_PARSER_OK;
1937 GST_WARNING ("error parsing \"Subset sequence parameter set\"");
1938 gst_h264_sps_clear (sps);
1940 return GST_H264_PARSER_ERROR;
1944 * gst_h264_parse_pps:
1945 * @nalparser: a #GstH264NalParser
1946 * @nalu: The #GST_H264_NAL_PPS #GstH264NalUnit to parse
1947 * @pps: The #GstH264PPS to fill.
1949 * Parses @data, and fills the @pps structure.
1951 * The resulting @pps data structure shall be deallocated with the
1952 * gst_h264_pps_clear() function when it is no longer needed, or prior
1953 * to parsing a new PPS NAL unit.
1955 * Returns: a #GstH264ParserResult
1958 gst_h264_parse_pps (GstH264NalParser * nalparser, GstH264NalUnit * nalu,
1966 GST_DEBUG ("parsing PPS");
1968 nal_reader_init (&nr, nalu->data + nalu->offset + nalu->header_bytes,
1969 nalu->size - nalu->header_bytes);
1971 memset (pps, 0, sizeof (*pps));
1973 READ_UE_MAX (&nr, pps->id, GST_H264_MAX_PPS_COUNT - 1);
1974 READ_UE_MAX (&nr, sps_id, GST_H264_MAX_SPS_COUNT - 1);
1976 sps = gst_h264_parser_get_sps (nalparser, sps_id);
1978 GST_WARNING ("couldn't find associated sequence parameter set with id: %d",
1980 return GST_H264_PARSER_BROKEN_LINK;
1982 pps->sequence = sps;
1983 qp_bd_offset = 6 * (sps->bit_depth_luma_minus8 +
1984 sps->separate_colour_plane_flag);
1986 /* set default values for fields that might not be present in the bitstream
1987 and have valid defaults */
1988 memcpy (&pps->scaling_lists_4x4, &sps->scaling_lists_4x4, 96);
1989 memcpy (&pps->scaling_lists_8x8, &sps->scaling_lists_8x8, 384);
1991 READ_UINT8 (&nr, pps->entropy_coding_mode_flag, 1);
1992 READ_UINT8 (&nr, pps->pic_order_present_flag, 1);
1993 READ_UE_MAX (&nr, pps->num_slice_groups_minus1, 7);
1994 if (pps->num_slice_groups_minus1 > 0) {
1995 READ_UE_MAX (&nr, pps->slice_group_map_type, 6);
1997 if (pps->slice_group_map_type == 0) {
2000 for (i = 0; i <= pps->num_slice_groups_minus1; i++)
2001 READ_UE (&nr, pps->run_length_minus1[i]);
2002 } else if (pps->slice_group_map_type == 2) {
2005 for (i = 0; i < pps->num_slice_groups_minus1; i++) {
2006 READ_UE (&nr, pps->top_left[i]);
2007 READ_UE (&nr, pps->bottom_right[i]);
2009 } else if (pps->slice_group_map_type >= 3 && pps->slice_group_map_type <= 5) {
2010 READ_UINT8 (&nr, pps->slice_group_change_direction_flag, 1);
2011 READ_UE (&nr, pps->slice_group_change_rate_minus1);
2012 } else if (pps->slice_group_map_type == 6) {
2016 READ_UE (&nr, pps->pic_size_in_map_units_minus1);
2017 bits = g_bit_storage (pps->num_slice_groups_minus1);
2019 pps->slice_group_id =
2020 g_new (guint8, pps->pic_size_in_map_units_minus1 + 1);
2021 for (i = 0; i <= pps->pic_size_in_map_units_minus1; i++)
2022 READ_UINT8 (&nr, pps->slice_group_id[i], bits);
2026 READ_UE_MAX (&nr, pps->num_ref_idx_l0_active_minus1, 31);
2027 READ_UE_MAX (&nr, pps->num_ref_idx_l1_active_minus1, 31);
2028 READ_UINT8 (&nr, pps->weighted_pred_flag, 1);
2029 READ_UINT8 (&nr, pps->weighted_bipred_idc, 2);
2030 READ_SE_ALLOWED (&nr, pps->pic_init_qp_minus26, -(26 + qp_bd_offset), 25);
2031 READ_SE_ALLOWED (&nr, pps->pic_init_qs_minus26, -26, 25);
2032 READ_SE_ALLOWED (&nr, pps->chroma_qp_index_offset, -12, 12);
2033 pps->second_chroma_qp_index_offset = pps->chroma_qp_index_offset;
2034 READ_UINT8 (&nr, pps->deblocking_filter_control_present_flag, 1);
2035 READ_UINT8 (&nr, pps->constrained_intra_pred_flag, 1);
2036 READ_UINT8 (&nr, pps->redundant_pic_cnt_present_flag, 1);
2038 if (!nal_reader_has_more_data (&nr))
2041 READ_UINT8 (&nr, pps->transform_8x8_mode_flag, 1);
2043 READ_UINT8 (&nr, pps->pic_scaling_matrix_present_flag, 1);
2044 if (pps->pic_scaling_matrix_present_flag) {
2047 n_lists = 6 + ((sps->chroma_format_idc != 3) ? 2 : 6) *
2048 pps->transform_8x8_mode_flag;
2050 if (sps->scaling_matrix_present_flag) {
2051 if (!gst_h264_parser_parse_scaling_list (&nr,
2052 pps->scaling_lists_4x4, pps->scaling_lists_8x8,
2053 sps->scaling_lists_4x4[3], sps->scaling_lists_4x4[0],
2054 sps->scaling_lists_8x8[3], sps->scaling_lists_8x8[0], n_lists))
2057 if (!gst_h264_parser_parse_scaling_list (&nr,
2058 pps->scaling_lists_4x4, pps->scaling_lists_8x8,
2059 default_4x4_inter, default_4x4_intra,
2060 default_8x8_inter, default_8x8_intra, n_lists))
2065 READ_SE_ALLOWED (&nr, pps->second_chroma_qp_index_offset, -12, 12);
2069 return GST_H264_PARSER_OK;
2072 GST_WARNING ("error parsing \"Picture parameter set\"");
2074 gst_h264_pps_clear (pps);
2075 return GST_H264_PARSER_ERROR;
2079 * gst_h264_parser_parse_pps:
2080 * @nalparser: a #GstH264NalParser
2081 * @nalu: The #GST_H264_NAL_PPS #GstH264NalUnit to parse
2082 * @pps: The #GstH264PPS to fill.
2084 * Parses @nalu containing a Picture Parameter Set, and fills @pps.
2086 * The resulting @pps data structure must be deallocated by the caller using
2087 * gst_h264_pps_clear().
2089 * Returns: a #GstH264ParserResult
2092 gst_h264_parser_parse_pps (GstH264NalParser * nalparser,
2093 GstH264NalUnit * nalu, GstH264PPS * pps)
2095 GstH264ParserResult res = gst_h264_parse_pps (nalparser, nalu, pps);
2097 if (res == GST_H264_PARSER_OK) {
2098 GST_DEBUG ("adding picture parameter set with id: %d to array", pps->id);
2100 if (!gst_h264_pps_copy (&nalparser->pps[pps->id], pps))
2101 return GST_H264_PARSER_ERROR;
2102 nalparser->last_pps = &nalparser->pps[pps->id];
2109 * gst_h264_pps_clear:
2110 * @pps: The #GstH264PPS to free
2112 * Clears all @pps internal resources.
2117 gst_h264_pps_clear (GstH264PPS * pps)
2119 g_return_if_fail (pps != NULL);
2121 g_free (pps->slice_group_id);
2122 pps->slice_group_id = NULL;
2126 * gst_h264_parser_parse_slice_hdr:
2127 * @nalparser: a #GstH264NalParser
2128 * @nalu: The #GST_H264_NAL_SLICE to #GST_H264_NAL_SLICE_IDR #GstH264NalUnit to parse
2129 * @slice: The #GstH264SliceHdr to fill.
2130 * @parse_pred_weight_table: Whether to parse the pred_weight_table or not
2131 * @parse_dec_ref_pic_marking: Whether to parse the dec_ref_pic_marking or not
2133 * Parses @nalu containing a coded slice, and fills @slice.
2135 * Returns: a #GstH264ParserResult
2138 gst_h264_parser_parse_slice_hdr (GstH264NalParser * nalparser,
2139 GstH264NalUnit * nalu, GstH264SliceHdr * slice,
2140 gboolean parse_pred_weight_table, gboolean parse_dec_ref_pic_marking)
2148 memset (slice, 0, sizeof (*slice));
2151 GST_DEBUG ("Invalid Nal Unit");
2152 return GST_H264_PARSER_ERROR;
2155 nal_reader_init (&nr, nalu->data + nalu->offset + nalu->header_bytes,
2156 nalu->size - nalu->header_bytes);
2158 READ_UE (&nr, slice->first_mb_in_slice);
2159 READ_UE (&nr, slice->type);
2161 GST_DEBUG ("parsing \"Slice header\", slice type %u", slice->type);
2163 READ_UE_MAX (&nr, pps_id, GST_H264_MAX_PPS_COUNT - 1);
2164 pps = gst_h264_parser_get_pps (nalparser, pps_id);
2167 GST_WARNING ("couldn't find associated picture parameter set with id: %d",
2170 return GST_H264_PARSER_BROKEN_LINK;
2174 sps = pps->sequence;
2176 GST_WARNING ("couldn't find associated sequence parameter set with id: %d",
2178 return GST_H264_PARSER_BROKEN_LINK;
2181 /* Check we can actually parse this slice (AVC, MVC headers only) */
2182 if (sps->extension_type && sps->extension_type != GST_H264_NAL_EXTENSION_MVC) {
2183 GST_WARNING ("failed to parse unsupported slice header");
2184 return GST_H264_PARSER_BROKEN_DATA;
2187 /* set default values for fields that might not be present in the bitstream
2188 and have valid defaults */
2189 slice->num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_active_minus1;
2190 slice->num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_active_minus1;
2192 if (sps->separate_colour_plane_flag)
2193 READ_UINT8 (&nr, slice->colour_plane_id, 2);
2195 READ_UINT16 (&nr, slice->frame_num, sps->log2_max_frame_num_minus4 + 4);
2197 if (!sps->frame_mbs_only_flag) {
2198 READ_UINT8 (&nr, slice->field_pic_flag, 1);
2199 if (slice->field_pic_flag)
2200 READ_UINT8 (&nr, slice->bottom_field_flag, 1);
2203 /* calculate MaxPicNum */
2204 if (slice->field_pic_flag)
2205 slice->max_pic_num = 2 * sps->max_frame_num;
2207 slice->max_pic_num = sps->max_frame_num;
2209 if (nalu->idr_pic_flag)
2210 READ_UE_MAX (&nr, slice->idr_pic_id, G_MAXUINT16);
2212 start_pos = nal_reader_get_pos (&nr);
2214 if (sps->pic_order_cnt_type == 0) {
2215 READ_UINT16 (&nr, slice->pic_order_cnt_lsb,
2216 sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
2218 if (pps->pic_order_present_flag && !slice->field_pic_flag)
2219 READ_SE (&nr, slice->delta_pic_order_cnt_bottom);
2222 if (sps->pic_order_cnt_type == 1 && !sps->delta_pic_order_always_zero_flag) {
2223 READ_SE (&nr, slice->delta_pic_order_cnt[0]);
2224 if (pps->pic_order_present_flag && !slice->field_pic_flag)
2225 READ_SE (&nr, slice->delta_pic_order_cnt[1]);
2228 slice->pic_order_cnt_bit_size = nal_reader_get_pos (&nr) - start_pos;
2230 if (pps->redundant_pic_cnt_present_flag)
2231 READ_UE_MAX (&nr, slice->redundant_pic_cnt, G_MAXINT8);
2233 if (GST_H264_IS_B_SLICE (slice))
2234 READ_UINT8 (&nr, slice->direct_spatial_mv_pred_flag, 1);
2236 if (GST_H264_IS_P_SLICE (slice) || GST_H264_IS_SP_SLICE (slice) ||
2237 GST_H264_IS_B_SLICE (slice)) {
2238 READ_UINT8 (&nr, slice->num_ref_idx_active_override_flag, 1);
2239 if (slice->num_ref_idx_active_override_flag) {
2240 READ_UE_MAX (&nr, slice->num_ref_idx_l0_active_minus1, 31);
2242 if (GST_H264_IS_B_SLICE (slice))
2243 READ_UE_MAX (&nr, slice->num_ref_idx_l1_active_minus1, 31);
2247 if (!slice_parse_ref_pic_list_modification (slice, &nr,
2248 GST_H264_IS_MVC_NALU (nalu)))
2251 if ((pps->weighted_pred_flag && (GST_H264_IS_P_SLICE (slice)
2252 || GST_H264_IS_SP_SLICE (slice)))
2253 || (pps->weighted_bipred_idc == 1 && GST_H264_IS_B_SLICE (slice))) {
2254 if (!gst_h264_slice_parse_pred_weight_table (slice, &nr,
2255 sps->chroma_array_type))
2259 if (nalu->ref_idc != 0) {
2260 if (!gst_h264_slice_parse_dec_ref_pic_marking (slice, nalu, &nr))
2264 if (pps->entropy_coding_mode_flag && !GST_H264_IS_I_SLICE (slice) &&
2265 !GST_H264_IS_SI_SLICE (slice))
2266 READ_UE_MAX (&nr, slice->cabac_init_idc, 2);
2268 READ_SE_ALLOWED (&nr, slice->slice_qp_delta, -87, 77);
2270 if (GST_H264_IS_SP_SLICE (slice) || GST_H264_IS_SI_SLICE (slice)) {
2271 if (GST_H264_IS_SP_SLICE (slice))
2272 READ_UINT8 (&nr, slice->sp_for_switch_flag, 1);
2273 READ_SE_ALLOWED (&nr, slice->slice_qs_delta, -51, 51);
2276 if (pps->deblocking_filter_control_present_flag) {
2277 READ_UE_MAX (&nr, slice->disable_deblocking_filter_idc, 2);
2278 if (slice->disable_deblocking_filter_idc != 1) {
2279 READ_SE_ALLOWED (&nr, slice->slice_alpha_c0_offset_div2, -6, 6);
2280 READ_SE_ALLOWED (&nr, slice->slice_beta_offset_div2, -6, 6);
2284 if (pps->num_slice_groups_minus1 > 0 &&
2285 pps->slice_group_map_type >= 3 && pps->slice_group_map_type <= 5) {
2286 /* Ceil(Log2(PicSizeInMapUnits / SliceGroupChangeRate + 1)) [7-33] */
2287 guint32 PicWidthInMbs = sps->pic_width_in_mbs_minus1 + 1;
2288 guint32 PicHeightInMapUnits = sps->pic_height_in_map_units_minus1 + 1;
2289 guint32 PicSizeInMapUnits = PicWidthInMbs * PicHeightInMapUnits;
2290 guint32 SliceGroupChangeRate = pps->slice_group_change_rate_minus1 + 1;
2291 const guint n = ceil_log2 (PicSizeInMapUnits / SliceGroupChangeRate + 1);
2292 READ_UINT16 (&nr, slice->slice_group_change_cycle, n);
2295 slice->header_size = nal_reader_get_pos (&nr);
2296 slice->n_emulation_prevention_bytes = nal_reader_get_epb_count (&nr);
2298 return GST_H264_PARSER_OK;
2301 GST_WARNING ("error parsing \"Slice header\"");
2302 return GST_H264_PARSER_ERROR;
2305 /* Free MVC-specific data from subset SPS header */
2307 gst_h264_sps_mvc_clear (GstH264SPS * sps)
2309 GstH264SPSExtMVC *const mvc = &sps->extension.mvc;
2312 g_assert (sps->extension_type == GST_H264_NAL_EXTENSION_MVC);
2317 for (i = 0; i <= mvc->num_level_values_signalled_minus1; i++) {
2318 GstH264SPSExtMVCLevelValue *const level_value = &mvc->level_value[i];
2320 for (j = 0; j <= level_value->num_applicable_ops_minus1; j++) {
2321 g_free (level_value->applicable_op[j].target_view_id);
2322 level_value->applicable_op[j].target_view_id = NULL;
2324 g_free (level_value->applicable_op);
2325 level_value->applicable_op = NULL;
2327 g_free (mvc->level_value);
2328 mvc->level_value = NULL;
2330 /* All meaningful MVC info are now gone, just pretend to be a
2331 * standard AVC struct now */
2332 sps->extension_type = GST_H264_NAL_EXTENSION_NONE;
2336 * gst_h264_sps_clear:
2337 * @sps: The #GstH264SPS to free
2339 * Clears all @sps internal resources.
2344 gst_h264_sps_clear (GstH264SPS * sps)
2346 g_return_if_fail (sps != NULL);
2348 switch (sps->extension_type) {
2349 case GST_H264_NAL_EXTENSION_MVC:
2350 gst_h264_sps_mvc_clear (sps);
2356 h264_sei_message_clear (GstH264SEIMessage * sei_msg)
2358 switch (sei_msg->payloadType) {
2359 case GST_H264_SEI_REGISTERED_USER_DATA:{
2360 GstH264RegisteredUserData *rud = &sei_msg->payload.registered_user_data;
2362 g_free ((guint8 *) rud->data);
2372 * gst_h264_parser_parse_sei:
2373 * @nalparser: a #GstH264NalParser
2374 * @nalu: The #GST_H264_NAL_SEI #GstH264NalUnit to parse
2375 * @messages: The GArray of #GstH264SEIMessage to fill. The caller must free it when done.
2377 * Parses @nalu containing one or more Supplementary Enhancement Information messages,
2378 * and allocates and fills the @messages array.
2380 * Returns: a #GstH264ParserResult
2383 gst_h264_parser_parse_sei (GstH264NalParser * nalparser, GstH264NalUnit * nalu,
2387 GstH264SEIMessage sei;
2388 GstH264ParserResult res;
2390 GST_DEBUG ("parsing SEI nal");
2391 nal_reader_init (&nr, nalu->data + nalu->offset + nalu->header_bytes,
2392 nalu->size - nalu->header_bytes);
2393 *messages = g_array_new (FALSE, FALSE, sizeof (GstH264SEIMessage));
2394 g_array_set_clear_func (*messages, (GDestroyNotify) h264_sei_message_clear);
2397 res = gst_h264_parser_parse_sei_message (nalparser, &nr, &sei);
2398 if (res == GST_H264_PARSER_OK)
2399 g_array_append_val (*messages, sei);
2402 } while (nal_reader_has_more_data (&nr));
2408 * gst_h264_quant_matrix_8x8_get_zigzag_from_raster:
2409 * @out_quant: (out): The resulting quantization matrix
2410 * @quant: The source quantization matrix
2412 * Converts quantization matrix @quant from raster scan order to
2413 * zigzag scan order and store the resulting factors into @out_quant.
2415 * Note: it is an error to pass the same table in both @quant and
2416 * @out_quant arguments.
2421 gst_h264_quant_matrix_8x8_get_zigzag_from_raster (guint8 out_quant[64],
2422 const guint8 quant[64])
2426 g_return_if_fail (out_quant != quant);
2428 for (i = 0; i < 64; i++)
2429 out_quant[i] = quant[zigzag_8x8[i]];
2433 * gst_h264_quant_matrix_8x8_get_raster_from_zigzag:
2434 * @out_quant: (out): The resulting quantization matrix
2435 * @quant: The source quantization matrix
2437 * Converts quantization matrix @quant from zigzag scan order to
2438 * raster scan order and store the resulting factors into @out_quant.
2440 * Note: it is an error to pass the same table in both @quant and
2441 * @out_quant arguments.
2446 gst_h264_quant_matrix_8x8_get_raster_from_zigzag (guint8 out_quant[64],
2447 const guint8 quant[64])
2451 g_return_if_fail (out_quant != quant);
2453 for (i = 0; i < 64; i++)
2454 out_quant[zigzag_8x8[i]] = quant[i];
2458 * gst_h264_quant_matrix_4x4_get_zigzag_from_raster:
2459 * @out_quant: (out): The resulting quantization matrix
2460 * @quant: The source quantization matrix
2462 * Converts quantization matrix @quant from raster scan order to
2463 * zigzag scan order and store the resulting factors into @out_quant.
2465 * Note: it is an error to pass the same table in both @quant and
2466 * @out_quant arguments.
2471 gst_h264_quant_matrix_4x4_get_zigzag_from_raster (guint8 out_quant[16],
2472 const guint8 quant[16])
2476 g_return_if_fail (out_quant != quant);
2478 for (i = 0; i < 16; i++)
2479 out_quant[i] = quant[zigzag_4x4[i]];
2483 * gst_h264_quant_matrix_4x4_get_raster_from_zigzag:
2484 * @out_quant: (out): The resulting quantization matrix
2485 * @quant: The source quantization matrix
2487 * Converts quantization matrix @quant from zigzag scan order to
2488 * raster scan order and store the resulting factors into @out_quant.
2490 * Note: it is an error to pass the same table in both @quant and
2491 * @out_quant arguments.
2496 gst_h264_quant_matrix_4x4_get_raster_from_zigzag (guint8 out_quant[16],
2497 const guint8 quant[16])
2501 g_return_if_fail (out_quant != quant);
2503 for (i = 0; i < 16; i++)
2504 out_quant[zigzag_4x4[i]] = quant[i];
2508 * gst_h264_video_calculate_framerate:
2509 * @sps: Current Sequence Parameter Set
2510 * @field_pic_flag: Current @field_pic_flag, obtained from latest slice header
2511 * @pic_struct: @pic_struct value if available, 0 otherwise
2512 * @fps_num: (out): The resulting fps numerator
2513 * @fps_den: (out): The resulting fps denominator
2515 * Calculate framerate of a video sequence using @sps VUI information,
2516 * @field_pic_flag from a slice header and @pic_struct from #GstH264PicTiming SEI
2519 * If framerate is variable or can't be determined, @fps_num will be set to 0
2520 * and @fps_den to 1.
2523 gst_h264_video_calculate_framerate (const GstH264SPS * sps,
2524 guint field_pic_flag, guint pic_struct, gint * fps_num, gint * fps_den)
2529 /* To calculate framerate, we use this formula:
2531 * fps = ----------------- x --------------- x ------------------------
2532 * num_units_in_tick DeltaTfiDivisor (field_pic_flag ? 2 : 1)
2534 * See H264 specification E2.1 for more details.
2538 if (sps->vui_parameters_present_flag) {
2539 const GstH264VUIParams *vui = &sps->vui_parameters;
2540 if (vui->timing_info_present_flag) {
2541 int delta_tfi_divisor = 1;
2542 num = vui->time_scale;
2543 den = vui->num_units_in_tick;
2545 if (vui->pic_struct_present_flag) {
2546 switch (pic_struct) {
2549 delta_tfi_divisor = 1;
2554 delta_tfi_divisor = 2;
2558 delta_tfi_divisor = 3;
2561 delta_tfi_divisor = 4;
2564 delta_tfi_divisor = 6;
2568 delta_tfi_divisor = field_pic_flag ? 1 : 2;
2570 den *= delta_tfi_divisor;
2572 /* Picture is two fields ? */
2573 den *= (field_pic_flag ? 2 : 1);