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 * @short_description: Convenience library for h264 video
35 * It offers you bitstream parsing in AVC mode or not. To identify Nals in a bitstream and
36 * parse its headers, you should call:
39 * #gst_h264_parser_identify_nalu to identify the following nalu in not AVC bitstreams
42 * #gst_h264_parser_identify_nalu_avc to identify the nalu in AVC bitstreams
46 * Then, depending on the #GstH264NalUnitType of the newly parsed #GstH264NalUnit, you should
47 * call the differents functions to parse the structure:
50 * From #GST_H264_NAL_SLICE to #GST_H264_NAL_SLICE_IDR: #gst_h264_parser_parse_slice_hdr
53 * #GST_H264_NAL_SEI: #gst_h264_parser_parse_sei
56 * #GST_H264_NAL_SPS: #gst_h264_parser_parse_sps
59 * #GST_H264_NAL_PPS: #gst_h264_parser_parse_pps
62 * Any other: #gst_h264_parser_parse_nal
66 * Note: You should always call gst_h264_parser_parse_nal if you don't actually need
67 * #GstH264NalUnitType to be parsed for your personnal use, in order to guarantee that the
68 * #GstH264NalParser is always up to date.
70 * For more details about the structures, look at the ITU-T H.264 and ISO/IEC 14496-10 – MPEG-4
71 * Part 10 specifications, you can download them from:
75 * ITU-T H.264: http://www.itu.int/rec/T-REC-H.264
78 * ISO/IEC 14496-10: http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=56538
87 #include "gsth264parser.h"
89 #include <gst/base/gstbytereader.h>
90 #include <gst/base/gstbitreader.h>
93 GST_DEBUG_CATEGORY (h264_parser_debug);
94 #define GST_CAT_DEFAULT h264_parser_debug
96 /**** Default scaling_lists according to Table 7-2 *****/
97 static const guint8 default_4x4_intra[16] = {
98 6, 13, 13, 20, 20, 20, 28, 28, 28, 28, 32, 32,
102 static const guint8 default_4x4_inter[16] = {
103 10, 14, 14, 20, 20, 20, 24, 24, 24, 24, 27, 27,
107 static const guint8 default_8x8_intra[64] = {
108 6, 10, 10, 13, 11, 13, 16, 16, 16, 16, 18, 18,
109 18, 18, 18, 23, 23, 23, 23, 23, 23, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27,
110 27, 27, 27, 27, 27, 29, 29, 29, 29, 29, 29, 29, 31, 31, 31, 31, 31, 31, 33,
111 33, 33, 33, 33, 36, 36, 36, 36, 38, 38, 38, 40, 40, 42
114 static const guint8 default_8x8_inter[64] = {
115 9, 13, 13, 15, 13, 15, 17, 17, 17, 17, 19, 19,
116 19, 19, 19, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 24, 24, 24,
117 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27, 27, 27, 28,
118 28, 28, 28, 28, 30, 30, 30, 30, 32, 32, 32, 33, 33, 35
121 static const guint8 zigzag_8x8[64] = {
122 0, 1, 8, 16, 9, 2, 3, 10,
123 17, 24, 32, 25, 18, 11, 4, 5,
124 12, 19, 26, 33, 40, 48, 41, 34,
125 27, 20, 13, 6, 7, 14, 21, 28,
126 35, 42, 49, 56, 57, 50, 43, 36,
127 29, 22, 15, 23, 30, 37, 44, 51,
128 58, 59, 52, 45, 38, 31, 39, 46,
129 53, 60, 61, 54, 47, 55, 62, 63
132 static const guint8 zigzag_4x4[16] = {
144 /* Table E-1 - Meaning of sample aspect ratio indicator (1..16) */
145 static PAR aspect_ratios[17] = {
165 /* Compute Ceil(Log2(v)) */
166 /* Derived from branchless code for integer log2(v) from:
167 <http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog> */
169 ceil_log2 (guint32 v)
174 r = (v > 0xFFFF) << 4;
176 shift = (v > 0xFF) << 3;
179 shift = (v > 0xF) << 2;
182 shift = (v > 0x3) << 1;
189 /****** Nal parser ******/
196 guint n_epb; /* Number of emulation prevention bytes */
197 guint byte; /* Byte position */
198 guint bits_in_cache; /* bitpos in the cache of next bit */
200 guint64 cache; /* cached bytes */
204 nal_reader_init (NalReader * nr, const guint8 * data, guint size)
211 nr->bits_in_cache = 0;
212 /* fill with something other than 0 to detect emulation prevention bytes */
213 nr->first_byte = 0xff;
217 static inline gboolean
218 nal_reader_read (NalReader * nr, guint nbits)
220 if (G_UNLIKELY (nr->byte * 8 + (nbits - nr->bits_in_cache) > nr->size * 8)) {
221 GST_DEBUG ("Can not read %u bits, bits in cache %u, Byte * 8 %u, size in "
222 "bits %u", nbits, nr->bits_in_cache, nr->byte * 8, nr->size * 8);
226 while (nr->bits_in_cache < nbits) {
228 gboolean check_three_byte;
230 check_three_byte = TRUE;
232 if (G_UNLIKELY (nr->byte >= nr->size))
235 byte = nr->data[nr->byte++];
237 /* check if the byte is a emulation_prevention_three_byte */
238 if (check_three_byte && byte == 0x03 && nr->first_byte == 0x00 &&
239 ((nr->cache & 0xff) == 0)) {
240 /* next byte goes unconditionally to the cache, even if it's 0x03 */
241 check_three_byte = FALSE;
245 nr->cache = (nr->cache << 8) | nr->first_byte;
246 nr->first_byte = byte;
247 nr->bits_in_cache += 8;
253 static inline gboolean
254 nal_reader_skip (NalReader * nr, guint nbits)
256 if (G_UNLIKELY (!nal_reader_read (nr, nbits)))
259 nr->bits_in_cache -= nbits;
264 static inline gboolean
265 nal_reader_skip_to_byte (NalReader * nr)
267 if (nr->bits_in_cache == 0) {
268 if (G_LIKELY ((nr->size - nr->byte) > 0))
274 nr->bits_in_cache = 0;
280 nal_reader_get_pos (const NalReader * nr)
282 return nr->byte * 8 - nr->bits_in_cache;
286 nal_reader_get_remaining (const NalReader * nr)
288 return (nr->size - nr->byte) * 8 + nr->bits_in_cache;
292 nal_reader_get_epb_count (const NalReader * nr)
297 #define GST_NAL_READER_READ_BITS(bits) \
299 nal_reader_get_bits_uint##bits (NalReader *nr, guint##bits *val, guint nbits) \
303 if (!nal_reader_read (nr, nbits)) \
306 /* bring the required bits down and truncate */ \
307 shift = nr->bits_in_cache - nbits; \
308 *val = nr->first_byte >> shift; \
310 *val |= nr->cache << (8 - shift); \
311 /* mask out required bits */ \
313 *val &= ((guint##bits)1 << nbits) - 1; \
315 nr->bits_in_cache = shift; \
320 GST_NAL_READER_READ_BITS (8);
321 GST_NAL_READER_READ_BITS (16);
322 GST_NAL_READER_READ_BITS (32);
325 nal_reader_get_ue (NalReader * nr, guint32 * val)
331 if (G_UNLIKELY (!nal_reader_get_bits_uint8 (nr, &bit, 1))) {
339 ((!nal_reader_get_bits_uint8 (nr, &bit, 1)))
343 if (G_UNLIKELY (i > 32))
346 if (G_UNLIKELY (!nal_reader_get_bits_uint32 (nr, &value, i)))
349 *val = (1 << i) - 1 + value;
354 static inline gboolean
355 nal_reader_get_se (NalReader * nr, gint32 * val)
359 if (G_UNLIKELY (!nal_reader_get_ue (nr, &value)))
363 *val = (value / 2) + 1;
370 #define CHECK_ALLOWED(val, min, max) { \
371 if (val < min || val > max) { \
372 GST_WARNING ("value not in allowed range. value: %d, range %d-%d", \
378 #define READ_UINT8(nr, val, nbits) { \
379 if (!nal_reader_get_bits_uint8 (nr, &val, nbits)) { \
380 GST_WARNING ("failed to read uint8, nbits: %d", nbits); \
385 #define READ_UINT16(nr, val, nbits) { \
386 if (!nal_reader_get_bits_uint16 (nr, &val, nbits)) { \
387 GST_WARNING ("failed to read uint16, nbits: %d", nbits); \
392 #define READ_UINT32(nr, val, nbits) { \
393 if (!nal_reader_get_bits_uint32 (nr, &val, nbits)) { \
394 GST_WARNING ("failed to read uint32, nbits: %d", nbits); \
399 #define READ_UINT64(nr, val, nbits) { \
400 if (!nal_reader_get_bits_uint64 (nr, &val, nbits)) { \
401 GST_WARNING ("failed to read uint32, nbits: %d", nbits); \
406 #define READ_UE(nr, val) { \
407 if (!nal_reader_get_ue (nr, &val)) { \
408 GST_WARNING ("failed to read UE"); \
413 #define READ_UE_ALLOWED(nr, val, min, max) { \
416 CHECK_ALLOWED (tmp, min, max); \
420 #define READ_SE(nr, val) { \
421 if (!nal_reader_get_se (nr, &val)) { \
422 GST_WARNING ("failed to read SE"); \
427 #define READ_SE_ALLOWED(nr, val, min, max) { \
430 CHECK_ALLOWED (tmp, min, max); \
434 /*********** end of nal parser ***************/
437 #define EXTENDED_SAR 255
440 gst_h264_parser_get_sps (GstH264NalParser * nalparser, guint8 sps_id)
444 sps = &nalparser->sps[sps_id];
453 gst_h264_parser_get_pps (GstH264NalParser * nalparser, guint8 pps_id)
457 pps = &nalparser->pps[pps_id];
466 set_nalu_datas (GstH264NalUnit * nalu)
468 guint8 *data = nalu->data + nalu->offset;
470 nalu->type = (data[0] & 0x1f);
471 nalu->ref_idc = (data[0] & 0x60) >> 5;
472 nalu->idr_pic_flag = (nalu->type == 5 ? 1 : 0);
474 GST_DEBUG ("Nal type %u, ref_idc %u", nalu->type, nalu->ref_idc);
478 scan_for_start_codes (const guint8 * data, guint size)
481 gst_byte_reader_init (&br, data, size);
483 /* NALU not empty, so we can at least expect 1 (even 2) bytes following sc */
484 return gst_byte_reader_masked_scan_uint32 (&br, 0xffffff00, 0x00000100,
489 gst_h264_parser_more_data (NalReader * nr)
492 guint remaining, nbits;
493 guint8 rbsp_stop_one_bit, zero_bits;
495 remaining = nal_reader_get_remaining (nr);
502 if (!nal_reader_get_bits_uint8 (nr, &rbsp_stop_one_bit, 1))
504 if (!rbsp_stop_one_bit)
507 nbits = --remaining % 8;
508 while (remaining > 0) {
509 if (!nal_reader_get_bits_uint8 (nr, &zero_bits, nbits))
519 /****** Parsing functions *****/
522 gst_h264_parse_hrd_parameters (GstH264HRDParams * hrd, NalReader * nr)
526 GST_DEBUG ("parsing \"HRD Parameters\"");
528 READ_UE_ALLOWED (nr, hrd->cpb_cnt_minus1, 0, 31);
529 READ_UINT8 (nr, hrd->bit_rate_scale, 4);
530 READ_UINT8 (nr, hrd->cpb_size_scale, 4);
532 for (sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1; sched_sel_idx++) {
533 READ_UE (nr, hrd->bit_rate_value_minus1[sched_sel_idx]);
534 READ_UE (nr, hrd->cpb_size_value_minus1[sched_sel_idx]);
535 READ_UINT8 (nr, hrd->cbr_flag[sched_sel_idx], 1);
538 READ_UINT8 (nr, hrd->initial_cpb_removal_delay_length_minus1, 5);
539 READ_UINT8 (nr, hrd->cpb_removal_delay_length_minus1, 5);
540 READ_UINT8 (nr, hrd->dpb_output_delay_length_minus1, 5);
541 READ_UINT8 (nr, hrd->time_offset_length, 5);
546 GST_WARNING ("error parsing \"HRD Parameters\"");
551 gst_h264_parse_vui_parameters (GstH264SPS * sps, NalReader * nr)
553 GstH264VUIParams *vui = &sps->vui_parameters;
555 GST_DEBUG ("parsing \"VUI Parameters\"");
557 /* set default values for fields that might not be present in the bitstream
558 and have valid defaults */
559 vui->aspect_ratio_idc = 0;
560 vui->video_format = 5;
561 vui->video_full_range_flag = 0;
562 vui->colour_primaries = 2;
563 vui->transfer_characteristics = 2;
564 vui->matrix_coefficients = 2;
565 vui->chroma_sample_loc_type_top_field = 0;
566 vui->chroma_sample_loc_type_bottom_field = 0;
567 vui->low_delay_hrd_flag = 0;
571 READ_UINT8 (nr, vui->aspect_ratio_info_present_flag, 1);
572 if (vui->aspect_ratio_info_present_flag) {
573 READ_UINT8 (nr, vui->aspect_ratio_idc, 8);
574 if (vui->aspect_ratio_idc == EXTENDED_SAR) {
575 READ_UINT16 (nr, vui->sar_width, 16);
576 READ_UINT16 (nr, vui->sar_height, 16);
577 vui->par_n = vui->sar_width;
578 vui->par_d = vui->sar_height;
579 } else if (vui->aspect_ratio_idc <= 16) {
580 vui->par_n = aspect_ratios[vui->aspect_ratio_idc].par_n;
581 vui->par_d = aspect_ratios[vui->aspect_ratio_idc].par_d;
585 READ_UINT8 (nr, vui->overscan_info_present_flag, 1);
586 if (vui->overscan_info_present_flag)
587 READ_UINT8 (nr, vui->overscan_appropriate_flag, 1);
589 READ_UINT8 (nr, vui->video_signal_type_present_flag, 1);
590 if (vui->video_signal_type_present_flag) {
592 READ_UINT8 (nr, vui->video_format, 3);
593 READ_UINT8 (nr, vui->video_full_range_flag, 1);
594 READ_UINT8 (nr, vui->colour_description_present_flag, 1);
595 if (vui->colour_description_present_flag) {
596 READ_UINT8 (nr, vui->colour_primaries, 8);
597 READ_UINT8 (nr, vui->transfer_characteristics, 8);
598 READ_UINT8 (nr, vui->matrix_coefficients, 8);
602 READ_UINT8 (nr, vui->chroma_loc_info_present_flag, 1);
603 if (vui->chroma_loc_info_present_flag) {
604 READ_UE_ALLOWED (nr, vui->chroma_sample_loc_type_top_field, 0, 5);
605 READ_UE_ALLOWED (nr, vui->chroma_sample_loc_type_bottom_field, 0, 5);
608 READ_UINT8 (nr, vui->timing_info_present_flag, 1);
609 if (vui->timing_info_present_flag) {
610 READ_UINT32 (nr, vui->num_units_in_tick, 32);
611 if (vui->num_units_in_tick == 0)
612 GST_WARNING ("num_units_in_tick = 0 detected in stream "
613 "(incompliant to H.264 E.2.1).");
615 READ_UINT32 (nr, vui->time_scale, 32);
616 if (vui->time_scale == 0)
617 GST_WARNING ("time_scale = 0 detected in stream "
618 "(incompliant to H.264 E.2.1).");
620 READ_UINT8 (nr, vui->fixed_frame_rate_flag, 1);
623 READ_UINT8 (nr, vui->nal_hrd_parameters_present_flag, 1);
624 if (vui->nal_hrd_parameters_present_flag) {
625 if (!gst_h264_parse_hrd_parameters (&vui->nal_hrd_parameters, nr))
629 READ_UINT8 (nr, vui->vcl_hrd_parameters_present_flag, 1);
630 if (vui->vcl_hrd_parameters_present_flag) {
631 if (!gst_h264_parse_hrd_parameters (&vui->vcl_hrd_parameters, nr))
635 if (vui->nal_hrd_parameters_present_flag ||
636 vui->vcl_hrd_parameters_present_flag)
637 READ_UINT8 (nr, vui->low_delay_hrd_flag, 1);
639 READ_UINT8 (nr, vui->pic_struct_present_flag, 1);
640 READ_UINT8 (nr, vui->bitstream_restriction_flag, 1);
641 if (vui->bitstream_restriction_flag) {
642 READ_UINT8 (nr, vui->motion_vectors_over_pic_boundaries_flag, 1);
643 READ_UE (nr, vui->max_bytes_per_pic_denom);
644 READ_UE_ALLOWED (nr, vui->max_bits_per_mb_denom, 0, 16);
645 READ_UE_ALLOWED (nr, vui->log2_max_mv_length_horizontal, 0, 16);
646 READ_UE_ALLOWED (nr, vui->log2_max_mv_length_vertical, 0, 16);
647 READ_UE (nr, vui->num_reorder_frames);
648 READ_UE (nr, vui->max_dec_frame_buffering);
654 GST_WARNING ("error parsing \"VUI Parameters\"");
659 gst_h264_parser_parse_scaling_list (NalReader * nr,
660 guint8 scaling_lists_4x4[6][16], guint8 scaling_lists_8x8[6][64],
661 const guint8 fallback_4x4_inter[16], const guint8 fallback_4x4_intra[16],
662 const guint8 fallback_8x8_inter[64], const guint8 fallback_8x8_intra[64],
667 GST_DEBUG ("parsing scaling lists");
669 for (i = 0; i < 12; i++) {
670 gboolean use_default = FALSE;
673 guint8 scaling_list_present_flag;
675 READ_UINT8 (nr, scaling_list_present_flag, 1);
676 if (scaling_list_present_flag) {
677 guint8 *scaling_list;
681 guint8 last_scale, next_scale;
684 scaling_list = scaling_lists_4x4[i];
688 scaling_list = scaling_lists_8x8[i - 6];
695 for (j = 0; j < size; j++) {
696 if (next_scale != 0) {
699 READ_SE (nr, delta_scale);
700 next_scale = (last_scale + delta_scale) & 0xff;
702 if (j == 0 && next_scale == 0) {
706 last_scale = scaling_list[scan[j]] =
707 (next_scale == 0) ? last_scale : next_scale;
717 memcpy (scaling_lists_4x4[0], fallback_4x4_intra, 16);
720 memcpy (scaling_lists_4x4[1], scaling_lists_4x4[0], 16);
723 memcpy (scaling_lists_4x4[2], scaling_lists_4x4[1], 16);
726 memcpy (scaling_lists_4x4[3], fallback_4x4_inter, 16);
729 memcpy (scaling_lists_4x4[4], scaling_lists_4x4[3], 16);
732 memcpy (scaling_lists_4x4[5], scaling_lists_4x4[4], 16);
735 memcpy (scaling_lists_8x8[0], fallback_8x8_intra, 64);
738 memcpy (scaling_lists_8x8[1], fallback_8x8_inter, 64);
741 memcpy (scaling_lists_8x8[2], scaling_lists_8x8[0], 64);
744 memcpy (scaling_lists_8x8[3], scaling_lists_8x8[1], 64);
747 memcpy (scaling_lists_8x8[4], scaling_lists_8x8[2], 64);
750 memcpy (scaling_lists_8x8[5], scaling_lists_8x8[3], 64);
762 GST_WARNING ("error parsing scaling lists");
767 slice_parse_ref_pic_list_modification_1 (GstH264SliceHdr * slice,
768 NalReader * nr, guint list)
770 GstH264RefPicListModification *entries;
771 guint8 *ref_pic_list_modification_flag, *n_ref_pic_list_modification;
772 guint32 modification_of_pic_nums_idc;
776 entries = slice->ref_pic_list_modification_l0;
777 ref_pic_list_modification_flag = &slice->ref_pic_list_modification_flag_l0;
778 n_ref_pic_list_modification = &slice->n_ref_pic_list_modification_l0;
780 entries = slice->ref_pic_list_modification_l1;
781 ref_pic_list_modification_flag = &slice->ref_pic_list_modification_flag_l1;
782 n_ref_pic_list_modification = &slice->n_ref_pic_list_modification_l1;
785 READ_UINT8 (nr, *ref_pic_list_modification_flag, 1);
786 if (*ref_pic_list_modification_flag) {
788 READ_UE (nr, modification_of_pic_nums_idc);
789 if (modification_of_pic_nums_idc == 3)
791 if (modification_of_pic_nums_idc == 0 ||
792 modification_of_pic_nums_idc == 1) {
793 READ_UE_ALLOWED (nr, entries[i].value.abs_diff_pic_num_minus1, 0,
794 slice->max_pic_num - 1);
795 } else if (modification_of_pic_nums_idc == 2) {
796 READ_UE (nr, entries[i].value.long_term_pic_num);
798 entries[i++].modification_of_pic_nums_idc = modification_of_pic_nums_idc;
801 *n_ref_pic_list_modification = i;
805 GST_WARNING ("error parsing \"Reference picture list %u modification\"",
811 slice_parse_ref_pic_list_modification (GstH264SliceHdr * slice, NalReader * nr)
813 if (!GST_H264_IS_I_SLICE (slice) && !GST_H264_IS_SI_SLICE (slice)) {
814 if (!slice_parse_ref_pic_list_modification_1 (slice, nr, 0))
818 if (GST_H264_IS_B_SLICE (slice)) {
819 if (!slice_parse_ref_pic_list_modification_1 (slice, nr, 1))
826 gst_h264_slice_parse_dec_ref_pic_marking (GstH264SliceHdr * slice,
827 GstH264NalUnit * nalu, NalReader * nr)
829 GstH264DecRefPicMarking *dec_ref_pic_m;
831 GST_DEBUG ("parsing \"Decoded reference picture marking\"");
833 dec_ref_pic_m = &slice->dec_ref_pic_marking;
835 if (nalu->idr_pic_flag) {
836 READ_UINT8 (nr, dec_ref_pic_m->no_output_of_prior_pics_flag, 1);
837 READ_UINT8 (nr, dec_ref_pic_m->long_term_reference_flag, 1);
839 READ_UINT8 (nr, dec_ref_pic_m->adaptive_ref_pic_marking_mode_flag, 1);
840 if (dec_ref_pic_m->adaptive_ref_pic_marking_mode_flag) {
841 guint32 mem_mgmt_ctrl_op;
842 GstH264RefPicMarking *refpicmarking;
844 dec_ref_pic_m->n_ref_pic_marking = 0;
847 &dec_ref_pic_m->ref_pic_marking[dec_ref_pic_m->n_ref_pic_marking];
849 READ_UE (nr, mem_mgmt_ctrl_op);
850 if (mem_mgmt_ctrl_op == 0)
853 refpicmarking->memory_management_control_operation = mem_mgmt_ctrl_op;
855 if (mem_mgmt_ctrl_op == 1 || mem_mgmt_ctrl_op == 3)
856 READ_UE (nr, refpicmarking->difference_of_pic_nums_minus1);
858 if (mem_mgmt_ctrl_op == 2)
859 READ_UE (nr, refpicmarking->long_term_pic_num);
861 if (mem_mgmt_ctrl_op == 3 || mem_mgmt_ctrl_op == 6)
862 READ_UE (nr, refpicmarking->long_term_frame_idx);
864 if (mem_mgmt_ctrl_op == 4)
865 READ_UE (nr, refpicmarking->max_long_term_frame_idx_plus1);
867 dec_ref_pic_m->n_ref_pic_marking++;
875 GST_WARNING ("error parsing \"Decoded reference picture marking\"");
880 gst_h264_slice_parse_pred_weight_table (GstH264SliceHdr * slice,
881 NalReader * nr, guint8 chroma_array_type)
883 GstH264PredWeightTable *p;
884 gint16 default_luma_weight, default_chroma_weight;
887 GST_DEBUG ("parsing \"Prediction weight table\"");
889 p = &slice->pred_weight_table;
891 READ_UE_ALLOWED (nr, p->luma_log2_weight_denom, 0, 7);
892 /* set default values */
893 default_luma_weight = 1 << p->luma_log2_weight_denom;
894 for (i = 0; i < G_N_ELEMENTS (p->luma_weight_l0); i++)
895 p->luma_weight_l0[i] = default_luma_weight;
896 memset (p->luma_offset_l0, 0, sizeof (p->luma_offset_l0));
897 if (GST_H264_IS_B_SLICE (slice)) {
898 for (i = 0; i < G_N_ELEMENTS (p->luma_weight_l1); i++)
899 p->luma_weight_l1[i] = default_luma_weight;
900 memset (p->luma_offset_l1, 0, sizeof (p->luma_offset_l1));
903 if (chroma_array_type != 0) {
904 READ_UE_ALLOWED (nr, p->chroma_log2_weight_denom, 0, 7);
905 /* set default values */
906 default_chroma_weight = 1 << p->chroma_log2_weight_denom;
907 for (i = 0; i < G_N_ELEMENTS (p->chroma_weight_l0); i++) {
908 p->chroma_weight_l0[i][0] = default_chroma_weight;
909 p->chroma_weight_l0[i][1] = default_chroma_weight;
911 memset (p->chroma_offset_l0, 0, sizeof (p->chroma_offset_l0));
912 if (GST_H264_IS_B_SLICE (slice)) {
913 for (i = 0; i < G_N_ELEMENTS (p->chroma_weight_l1); i++) {
914 p->chroma_weight_l1[i][0] = default_chroma_weight;
915 p->chroma_weight_l1[i][1] = default_chroma_weight;
917 memset (p->chroma_offset_l1, 0, sizeof (p->chroma_offset_l1));
921 for (i = 0; i <= slice->num_ref_idx_l0_active_minus1; i++) {
922 guint8 luma_weight_l0_flag;
924 READ_UINT8 (nr, luma_weight_l0_flag, 1);
925 if (luma_weight_l0_flag) {
926 READ_SE_ALLOWED (nr, p->luma_weight_l0[i], -128, 127);
927 READ_SE_ALLOWED (nr, p->luma_offset_l0[i], -128, 127);
929 if (chroma_array_type != 0) {
930 guint8 chroma_weight_l0_flag;
933 READ_UINT8 (nr, chroma_weight_l0_flag, 1);
934 if (chroma_weight_l0_flag) {
935 for (j = 0; j < 2; j++) {
936 READ_SE_ALLOWED (nr, p->chroma_weight_l0[i][j], -128, 127);
937 READ_SE_ALLOWED (nr, p->chroma_offset_l0[i][j], -128, 127);
943 if (GST_H264_IS_B_SLICE (slice)) {
944 for (i = 0; i <= slice->num_ref_idx_l1_active_minus1; i++) {
945 guint8 luma_weight_l1_flag;
947 READ_UINT8 (nr, luma_weight_l1_flag, 1);
948 if (luma_weight_l1_flag) {
949 READ_SE_ALLOWED (nr, p->luma_weight_l1[i], -128, 127);
950 READ_SE_ALLOWED (nr, p->luma_offset_l1[i], -128, 127);
952 if (chroma_array_type != 0) {
953 guint8 chroma_weight_l1_flag;
956 READ_UINT8 (nr, chroma_weight_l1_flag, 1);
957 if (chroma_weight_l1_flag) {
958 for (j = 0; j < 2; j++) {
959 READ_SE_ALLOWED (nr, p->chroma_weight_l1[i][j], -128, 127);
960 READ_SE_ALLOWED (nr, p->chroma_offset_l1[i][j], -128, 127);
970 GST_WARNING ("error parsing \"Prediction weight table\"");
975 gst_h264_parser_parse_buffering_period (GstH264NalParser * nalparser,
976 GstH264BufferingPeriod * per, NalReader * nr)
981 GST_DEBUG ("parsing \"Buffering period\"");
983 READ_UE_ALLOWED (nr, sps_id, 0, GST_H264_MAX_SPS_COUNT - 1);
984 sps = gst_h264_parser_get_sps (nalparser, sps_id);
986 GST_WARNING ("couldn't find associated sequence parameter set with id: %d",
988 return GST_H264_PARSER_BROKEN_LINK;
992 if (sps->vui_parameters_present_flag) {
993 GstH264VUIParams *vui = &sps->vui_parameters;
995 if (vui->nal_hrd_parameters_present_flag) {
996 GstH264HRDParams *hrd = &vui->nal_hrd_parameters;
997 guint8 sched_sel_idx;
999 for (sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1;
1001 READ_UINT8 (nr, per->nal_initial_cpb_removal_delay[sched_sel_idx], 5);
1003 per->nal_initial_cpb_removal_delay_offset[sched_sel_idx], 5);
1007 if (vui->vcl_hrd_parameters_present_flag) {
1008 GstH264HRDParams *hrd = &vui->vcl_hrd_parameters;
1009 guint8 sched_sel_idx;
1011 for (sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1;
1013 READ_UINT8 (nr, per->vcl_initial_cpb_removal_delay[sched_sel_idx], 5);
1015 per->vcl_initial_cpb_removal_delay_offset[sched_sel_idx], 5);
1020 return GST_H264_PARSER_OK;
1023 GST_WARNING ("error parsing \"Buffering period\"");
1024 return GST_H264_PARSER_ERROR;
1028 gst_h264_parse_clock_timestamp (GstH264ClockTimestamp * tim,
1029 GstH264VUIParams * vui, NalReader * nr)
1031 guint8 full_timestamp_flag;
1032 guint8 time_offset_length;
1034 GST_DEBUG ("parsing \"Clock timestamp\"");
1037 tim->time_offset = 0;
1039 READ_UINT8 (nr, tim->ct_type, 2);
1040 READ_UINT8 (nr, tim->nuit_field_based_flag, 1);
1041 READ_UINT8 (nr, tim->counting_type, 5);
1042 READ_UINT8 (nr, full_timestamp_flag, 1);
1043 READ_UINT8 (nr, tim->discontinuity_flag, 1);
1044 READ_UINT8 (nr, tim->cnt_dropped_flag, 1);
1045 READ_UINT8 (nr, tim->n_frames, 8);
1047 if (full_timestamp_flag) {
1048 tim->seconds_flag = TRUE;
1049 READ_UINT8 (nr, tim->seconds_value, 6);
1051 tim->minutes_flag = TRUE;
1052 READ_UINT8 (nr, tim->minutes_value, 6);
1054 tim->hours_flag = TRUE;
1055 READ_UINT8 (nr, tim->hours_value, 5);
1057 READ_UINT8 (nr, tim->seconds_flag, 1);
1058 if (tim->seconds_flag) {
1059 READ_UINT8 (nr, tim->seconds_value, 6);
1060 READ_UINT8 (nr, tim->minutes_flag, 1);
1061 if (tim->minutes_flag) {
1062 READ_UINT8 (nr, tim->minutes_value, 6);
1063 READ_UINT8 (nr, tim->hours_flag, 1);
1064 if (tim->hours_flag)
1065 READ_UINT8 (nr, tim->hours_value, 5);
1070 time_offset_length = 0;
1071 if (vui->nal_hrd_parameters_present_flag)
1072 time_offset_length = vui->nal_hrd_parameters.time_offset_length;
1073 else if (vui->vcl_hrd_parameters_present_flag)
1074 time_offset_length = vui->vcl_hrd_parameters.time_offset_length;
1076 if (time_offset_length > 0)
1077 READ_UINT32 (nr, tim->time_offset, time_offset_length);
1082 GST_WARNING ("error parsing \"Clock timestamp\"");
1087 gst_h264_parser_parse_pic_timing (GstH264NalParser * nalparser,
1088 GstH264PicTiming * tim, NalReader * nr)
1090 GST_DEBUG ("parsing \"Picture timing\"");
1091 if (!nalparser->last_sps || !nalparser->last_sps->valid) {
1092 GST_WARNING ("didn't get the associated sequence paramater set for the "
1093 "current access unit");
1097 /* default values */
1098 memset (tim->clock_timestamp_flag, 0, 3);
1100 if (nalparser->last_sps->vui_parameters_present_flag) {
1101 GstH264VUIParams *vui = &nalparser->last_sps->vui_parameters;
1103 if (vui->nal_hrd_parameters_present_flag) {
1104 READ_UINT32 (nr, tim->cpb_removal_delay,
1105 vui->nal_hrd_parameters.cpb_removal_delay_length_minus1 + 1);
1106 READ_UINT32 (nr, tim->dpb_output_delay,
1107 vui->nal_hrd_parameters.dpb_output_delay_length_minus1 + 1);
1108 } else if (vui->nal_hrd_parameters_present_flag) {
1109 READ_UINT32 (nr, tim->cpb_removal_delay,
1110 vui->vcl_hrd_parameters.cpb_removal_delay_length_minus1 + 1);
1111 READ_UINT32 (nr, tim->dpb_output_delay,
1112 vui->vcl_hrd_parameters.dpb_output_delay_length_minus1 + 1);
1115 if (vui->pic_struct_present_flag) {
1116 const guint8 num_clock_ts_table[9] = {
1117 1, 1, 1, 2, 2, 3, 3, 2, 3
1119 guint8 num_clock_num_ts;
1122 tim->pic_struct_present_flag = TRUE;
1123 READ_UINT8 (nr, tim->pic_struct, 4);
1124 CHECK_ALLOWED ((gint8) tim->pic_struct, 0, 8);
1126 num_clock_num_ts = num_clock_ts_table[tim->pic_struct];
1127 for (i = 0; i < num_clock_num_ts; i++) {
1128 READ_UINT8 (nr, tim->clock_timestamp_flag[i], 1);
1129 if (tim->clock_timestamp_flag[i]) {
1130 if (!gst_h264_parse_clock_timestamp (&tim->clock_timestamp[i], vui,
1138 return GST_H264_PARSER_OK;
1141 GST_WARNING ("error parsing \"Picture timing\"");
1142 return GST_H264_PARSER_ERROR;
1145 /******** API *************/
1148 * gst_h264_nal_parser_new:
1150 * Creates a new #GstH264NalParser. It should be freed with
1151 * gst_h264_nal_parser_free after use.
1153 * Returns: a new #GstH264NalParser
1156 gst_h264_nal_parser_new (void)
1158 GstH264NalParser *nalparser;
1160 nalparser = g_slice_new0 (GstH264NalParser);
1161 GST_DEBUG_CATEGORY_INIT (h264_parser_debug, "codecparsers_h264", 0,
1162 "h264 parser library");
1168 * gst_h264_nal_parser_free:
1169 * @nalparser: the #GstH264NalParser to free
1171 * Frees @nalparser and sets it to %NULL
1174 gst_h264_nal_parser_free (GstH264NalParser * nalparser)
1176 g_slice_free (GstH264NalParser, nalparser);
1182 * gst_h264_parser_identify_nalu_unchecked:
1183 * @nalparser: a #GstH264NalParser
1184 * @data: The data to parse
1185 * @offset: the offset from which to parse @data
1186 * @size: the size of @data
1187 * @nalu: The #GstH264NalUnit where to store parsed nal headers
1189 * Parses @data and fills @nalu from the next nalu data from @data.
1191 * This differs from @gst_h264_parser_identify_nalu in that it doesn't
1192 * check whether the packet is complete or not.
1194 * Note: Only use this function if you already know the provided @data
1195 * is a complete NALU, else use @gst_h264_parser_identify_nalu.
1197 * Returns: a #GstH264ParserResult
1200 gst_h264_parser_identify_nalu_unchecked (GstH264NalParser * nalparser,
1201 const guint8 * data, guint offset, gsize size, GstH264NalUnit * nalu)
1205 if (size < offset + 4) {
1206 GST_DEBUG ("Can't parse, buffer has too small size %" G_GSIZE_FORMAT
1207 ", offset %u", size, offset);
1208 return GST_H264_PARSER_ERROR;
1211 off1 = scan_for_start_codes (data + offset, size - offset);
1214 GST_DEBUG ("No start code prefix in this buffer");
1215 return GST_H264_PARSER_NO_NAL;
1218 if (offset + off1 == size - 1) {
1219 GST_DEBUG ("Missing data to identify nal unit");
1221 return GST_H264_PARSER_ERROR;
1225 nalu->sc_offset = offset + off1;
1227 /* sc might have 2 or 3 0-bytes */
1228 if (nalu->sc_offset > 0 && data[nalu->sc_offset - 1] == 00)
1231 nalu->offset = offset + off1 + 3;
1232 nalu->data = (guint8 *) data;
1234 set_nalu_datas (nalu);
1236 if (nalu->type == GST_H264_NAL_SEQ_END ||
1237 nalu->type == GST_H264_NAL_STREAM_END) {
1238 GST_DEBUG ("end-of-seq or end-of-stream nal found");
1240 return GST_H264_PARSER_OK;
1243 nalu->size = size - nalu->offset;
1245 return GST_H264_PARSER_OK;
1249 * gst_h264_parser_identify_nalu:
1250 * @nalparser: a #GstH264NalParser
1251 * @data: The data to parse
1252 * @offset: the offset from which to parse @data
1253 * @size: the size of @data
1254 * @nalu: The #GstH264NalUnit where to store parsed nal headers
1256 * Parses @data and fills @nalu from the next nalu data from @data
1258 * Returns: a #GstH264ParserResult
1261 gst_h264_parser_identify_nalu (GstH264NalParser * nalparser,
1262 const guint8 * data, guint offset, gsize size, GstH264NalUnit * nalu)
1264 GstH264ParserResult res;
1268 gst_h264_parser_identify_nalu_unchecked (nalparser, data, offset, size,
1271 if (res != GST_H264_PARSER_OK || nalu->size == 0)
1274 off2 = scan_for_start_codes (data + nalu->offset, size - nalu->offset);
1276 GST_DEBUG ("Nal start %d, No end found", nalu->offset);
1278 return GST_H264_PARSER_NO_NAL_END;
1281 if (off2 > 0 && data[nalu->offset + off2 - 1] == 00)
1286 return GST_H264_PARSER_BROKEN_DATA;
1288 GST_DEBUG ("Complete nal found. Off: %d, Size: %d", nalu->offset, nalu->size);
1296 * gst_h264_parser_identify_nalu_avc:
1297 * @nalparser: a #GstH264NalParser
1298 * @data: The data to parse, must be the beging of the Nal unit
1299 * @offset: the offset from which to parse @data
1300 * @size: the size of @data
1301 * @nal_length_size: the size in bytes of the AVC nal length prefix.
1302 * @nalu: The #GstH264NalUnit where to store parsed nal headers
1304 * Parses @data and sets @nalu.
1306 * Returns: a #GstH264ParserResult
1309 gst_h264_parser_identify_nalu_avc (GstH264NalParser * nalparser,
1310 const guint8 * data, guint offset, gsize size, guint8 nal_length_size,
1311 GstH264NalUnit * nalu)
1315 if (size < offset + nal_length_size) {
1316 GST_DEBUG ("Can't parse, buffer has too small size %" G_GSIZE_FORMAT
1317 ", offset %u", size, offset);
1318 return GST_H264_PARSER_ERROR;
1321 size = size - offset;
1322 gst_bit_reader_init (&br, data + offset, size);
1324 nalu->size = gst_bit_reader_get_bits_uint32_unchecked (&br,
1325 nal_length_size * 8);
1326 nalu->sc_offset = offset;
1327 nalu->offset = offset + nal_length_size;
1329 if (size < nalu->size + nal_length_size) {
1332 return GST_H264_PARSER_NO_NAL_END;
1335 nalu->data = (guint8 *) data;
1337 set_nalu_datas (nalu);
1340 return GST_H264_PARSER_BROKEN_DATA;
1344 return GST_H264_PARSER_OK;
1348 * gst_h264_parser_parse_nal:
1349 * @nalparser: a #GstH264NalParser
1350 * @nalu: The #GstH264NalUnit to parse
1352 * This function should be called in the case one doesn't need to
1353 * parse a specific structure. It is necessary to do so to make
1354 * sure @nalparser is up to date.
1356 * Returns: a #GstH264ParserResult
1359 gst_h264_parser_parse_nal (GstH264NalParser * nalparser, GstH264NalUnit * nalu)
1364 switch (nalu->type) {
1365 case GST_H264_NAL_SPS:
1366 return gst_h264_parser_parse_sps (nalparser, nalu, &sps, FALSE);
1368 case GST_H264_NAL_PPS:
1369 return gst_h264_parser_parse_pps (nalparser, nalu, &pps);
1372 return GST_H264_PARSER_OK;
1376 * gst_h264_parser_parse_sps:
1377 * @nalparser: a #GstH264NalParser
1378 * @nalu: The #GST_H264_NAL_SPS #GstH264NalUnit to parse
1379 * @sps: The #GstH264SPS to fill.
1380 * @parse_vui_params: Whether to parse the vui_params or not
1382 * Parses @data, and fills the @sps structure.
1384 * Returns: a #GstH264ParserResult
1387 gst_h264_parser_parse_sps (GstH264NalParser * nalparser, GstH264NalUnit * nalu,
1388 GstH264SPS * sps, gboolean parse_vui_params)
1390 GstH264ParserResult res = gst_h264_parse_sps (nalu, sps, parse_vui_params);
1392 if (res == GST_H264_PARSER_OK) {
1393 GST_DEBUG ("adding sequence parameter set with id: %d to array", sps->id);
1395 nalparser->sps[sps->id] = *sps;
1396 nalparser->last_sps = &nalparser->sps[sps->id];
1405 * gst_h264_parse_sps:
1406 * @nalu: The #GST_H264_NAL_SPS #GstH264NalUnit to parse
1407 * @sps: The #GstH264SPS to fill.
1408 * @parse_vui_params: Whether to parse the vui_params or not
1410 * Parses @data, and fills the @sps structure.
1412 * Returns: a #GstH264ParserResult
1415 gst_h264_parse_sps (GstH264NalUnit * nalu, GstH264SPS * sps,
1416 gboolean parse_vui_params)
1420 guint8 frame_cropping_flag;
1421 guint subwc[] = { 1, 2, 2, 1 };
1422 guint subhc[] = { 1, 2, 1, 1 };
1423 GstH264VUIParams *vui = NULL;
1425 GST_DEBUG ("parsing SPS");
1426 nal_reader_init (&nr, nalu->data + nalu->offset + 1, nalu->size - 1);
1428 /* set default values for fields that might not be present in the bitstream
1429 and have valid defaults */
1430 sps->chroma_format_idc = 1;
1431 sps->separate_colour_plane_flag = 0;
1432 sps->bit_depth_luma_minus8 = 0;
1433 sps->bit_depth_chroma_minus8 = 0;
1434 memset (sps->scaling_lists_4x4, 16, 96);
1435 memset (sps->scaling_lists_8x8, 16, 384);
1436 sps->mb_adaptive_frame_field_flag = 0;
1437 sps->frame_crop_left_offset = 0;
1438 sps->frame_crop_right_offset = 0;
1439 sps->frame_crop_top_offset = 0;
1440 sps->frame_crop_bottom_offset = 0;
1441 sps->delta_pic_order_always_zero_flag = 0;
1443 READ_UINT8 (&nr, sps->profile_idc, 8);
1444 READ_UINT8 (&nr, sps->constraint_set0_flag, 1);
1445 READ_UINT8 (&nr, sps->constraint_set1_flag, 1);
1446 READ_UINT8 (&nr, sps->constraint_set2_flag, 1);
1447 READ_UINT8 (&nr, sps->constraint_set3_flag, 1);
1449 /* skip reserved_zero_4bits */
1450 if (!nal_reader_skip (&nr, 4))
1453 READ_UINT8 (&nr, sps->level_idc, 8);
1455 READ_UE_ALLOWED (&nr, sps->id, 0, GST_H264_MAX_SPS_COUNT - 1);
1457 if (sps->profile_idc == 100 || sps->profile_idc == 110 ||
1458 sps->profile_idc == 122 || sps->profile_idc == 244 ||
1459 sps->profile_idc == 44 || sps->profile_idc == 83 ||
1460 sps->profile_idc == 86) {
1461 READ_UE_ALLOWED (&nr, sps->chroma_format_idc, 0, 3);
1462 if (sps->chroma_format_idc == 3)
1463 READ_UINT8 (&nr, sps->separate_colour_plane_flag, 1);
1465 READ_UE_ALLOWED (&nr, sps->bit_depth_luma_minus8, 0, 6);
1466 READ_UE_ALLOWED (&nr, sps->bit_depth_chroma_minus8, 0, 6);
1467 READ_UINT8 (&nr, sps->qpprime_y_zero_transform_bypass_flag, 1);
1469 READ_UINT8 (&nr, sps->scaling_matrix_present_flag, 1);
1470 if (sps->scaling_matrix_present_flag) {
1473 n_lists = (sps->chroma_format_idc != 3) ? 8 : 12;
1474 if (!gst_h264_parser_parse_scaling_list (&nr,
1475 sps->scaling_lists_4x4, sps->scaling_lists_8x8,
1476 default_4x4_inter, default_4x4_intra,
1477 default_8x8_inter, default_8x8_intra, n_lists))
1482 READ_UE_ALLOWED (&nr, sps->log2_max_frame_num_minus4, 0, 12);
1484 sps->max_frame_num = 1 << (sps->log2_max_frame_num_minus4 + 4);
1486 READ_UE_ALLOWED (&nr, sps->pic_order_cnt_type, 0, 2);
1487 if (sps->pic_order_cnt_type == 0) {
1488 READ_UE_ALLOWED (&nr, sps->log2_max_pic_order_cnt_lsb_minus4, 0, 12);
1489 } else if (sps->pic_order_cnt_type == 1) {
1492 READ_UINT8 (&nr, sps->delta_pic_order_always_zero_flag, 1);
1493 READ_SE (&nr, sps->offset_for_non_ref_pic);
1494 READ_SE (&nr, sps->offset_for_top_to_bottom_field);
1495 READ_UE_ALLOWED (&nr, sps->num_ref_frames_in_pic_order_cnt_cycle, 0, 255);
1497 for (i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; i++)
1498 READ_SE (&nr, sps->offset_for_ref_frame[i]);
1501 READ_UE (&nr, sps->num_ref_frames);
1502 READ_UINT8 (&nr, sps->gaps_in_frame_num_value_allowed_flag, 1);
1503 READ_UE (&nr, sps->pic_width_in_mbs_minus1);
1504 READ_UE (&nr, sps->pic_height_in_map_units_minus1);
1505 READ_UINT8 (&nr, sps->frame_mbs_only_flag, 1);
1507 if (!sps->frame_mbs_only_flag)
1508 READ_UINT8 (&nr, sps->mb_adaptive_frame_field_flag, 1);
1510 READ_UINT8 (&nr, sps->direct_8x8_inference_flag, 1);
1511 READ_UINT8 (&nr, frame_cropping_flag, 1);
1512 if (frame_cropping_flag) {
1513 READ_UE (&nr, sps->frame_crop_left_offset);
1514 READ_UE (&nr, sps->frame_crop_right_offset);
1515 READ_UE (&nr, sps->frame_crop_top_offset);
1516 READ_UE (&nr, sps->frame_crop_bottom_offset);
1519 READ_UINT8 (&nr, sps->vui_parameters_present_flag, 1);
1520 if (sps->vui_parameters_present_flag && parse_vui_params) {
1521 if (!gst_h264_parse_vui_parameters (sps, &nr))
1523 vui = &sps->vui_parameters;
1526 /* calculate ChromaArrayType */
1527 if (sps->separate_colour_plane_flag)
1528 sps->chroma_array_type = 0;
1530 sps->chroma_array_type = sps->chroma_format_idc;
1532 /* Calculate width and height */
1533 width = (sps->pic_width_in_mbs_minus1 + 1);
1535 height = (sps->pic_height_in_map_units_minus1 + 1);
1536 height *= 16 * (2 - sps->frame_mbs_only_flag);
1537 GST_LOG ("initial width=%d, height=%d", width, height);
1539 width -= (sps->frame_crop_left_offset + sps->frame_crop_right_offset)
1540 * subwc[sps->chroma_format_idc];
1541 height -= (sps->frame_crop_top_offset + sps->frame_crop_bottom_offset
1542 * subhc[sps->chroma_format_idc] * (2 - sps->frame_mbs_only_flag));
1543 if (width < 0 || height < 0) {
1544 GST_WARNING ("invalid width/height in SPS");
1547 GST_LOG ("final width=%u, height=%u", width, height);
1549 sps->height = height;
1554 if (vui && vui->timing_info_present_flag) {
1555 /* derive framerate */
1556 /* FIXME verify / also handle other cases */
1557 GST_LOG ("Framerate: %u %u %u %u", parse_vui_params,
1558 vui->fixed_frame_rate_flag, sps->frame_mbs_only_flag,
1559 vui->pic_struct_present_flag);
1561 if (parse_vui_params && vui->fixed_frame_rate_flag &&
1562 sps->frame_mbs_only_flag && !vui->pic_struct_present_flag) {
1563 sps->fps_num = vui->time_scale;
1564 sps->fps_den = vui->num_units_in_tick;
1565 /* picture is a frame = 2 fields */
1567 GST_LOG ("framerate %d/%d", sps->fps_num, sps->fps_den);
1570 GST_LOG ("No VUI, unknown framerate");
1575 return GST_H264_PARSER_OK;
1578 GST_WARNING ("error parsing \"Sequence parameter set\"");
1580 return GST_H264_PARSER_ERROR;
1584 * gst_h264_parse_pps:
1585 * @nalparser: a #GstH264NalParser
1586 * @nalu: The #GST_H264_NAL_PPS #GstH264NalUnit to parse
1587 * @pps: The #GstH264PPS to fill.
1589 * Parses @data, and fills the @pps structure.
1591 * Returns: a #GstH264ParserResult
1594 gst_h264_parse_pps (GstH264NalParser * nalparser, GstH264NalUnit * nalu,
1600 guint8 pic_scaling_matrix_present_flag;
1603 GST_DEBUG ("parsing PPS");
1605 nal_reader_init (&nr, nalu->data + nalu->offset + 1, nalu->size - 1);
1607 READ_UE_ALLOWED (&nr, pps->id, 0, GST_H264_MAX_PPS_COUNT - 1);
1608 READ_UE_ALLOWED (&nr, sps_id, 0, GST_H264_MAX_SPS_COUNT - 1);
1610 sps = gst_h264_parser_get_sps (nalparser, sps_id);
1612 GST_WARNING ("couldn't find associated sequence parameter set with id: %d",
1614 return GST_H264_PARSER_BROKEN_LINK;
1616 pps->sequence = sps;
1617 qp_bd_offset = 6 * (sps->bit_depth_luma_minus8 +
1618 sps->separate_colour_plane_flag);
1620 /* set default values for fields that might not be present in the bitstream
1621 and have valid defaults */
1622 pps->slice_group_id = NULL;
1623 pps->transform_8x8_mode_flag = 0;
1624 memcpy (&pps->scaling_lists_4x4, &sps->scaling_lists_4x4, 96);
1625 memcpy (&pps->scaling_lists_8x8, &sps->scaling_lists_8x8, 384);
1627 READ_UINT8 (&nr, pps->entropy_coding_mode_flag, 1);
1628 READ_UINT8 (&nr, pps->pic_order_present_flag, 1);
1629 READ_UE_ALLOWED (&nr, pps->num_slice_groups_minus1, 0, 7);
1630 if (pps->num_slice_groups_minus1 > 0) {
1631 READ_UE_ALLOWED (&nr, pps->slice_group_map_type, 0, 6);
1633 if (pps->slice_group_map_type == 0) {
1636 for (i = 0; i <= pps->num_slice_groups_minus1; i++)
1637 READ_UE (&nr, pps->run_length_minus1[i]);
1638 } else if (pps->slice_group_map_type == 2) {
1641 for (i = 0; i <= pps->num_slice_groups_minus1; i++) {
1642 READ_UE (&nr, pps->top_left[i]);
1643 READ_UE (&nr, pps->bottom_right[i]);
1645 } else if (pps->slice_group_map_type >= 3 && pps->slice_group_map_type <= 5) {
1646 READ_UINT8 (&nr, pps->slice_group_change_direction_flag, 1);
1647 READ_UE (&nr, pps->slice_group_change_rate_minus1);
1648 } else if (pps->slice_group_map_type == 6) {
1652 READ_UE (&nr, pps->pic_size_in_map_units_minus1);
1653 bits = g_bit_storage (pps->num_slice_groups_minus1);
1655 pps->slice_group_id =
1656 g_new (guint8, pps->pic_size_in_map_units_minus1 + 1);
1657 for (i = 0; i <= pps->pic_size_in_map_units_minus1; i++)
1658 READ_UINT8 (&nr, pps->slice_group_id[i], bits);
1662 READ_UE_ALLOWED (&nr, pps->num_ref_idx_l0_active_minus1, 0, 31);
1663 READ_UE_ALLOWED (&nr, pps->num_ref_idx_l1_active_minus1, 0, 31);
1664 READ_UINT8 (&nr, pps->weighted_pred_flag, 1);
1665 READ_UINT8 (&nr, pps->weighted_bipred_idc, 2);
1666 READ_SE_ALLOWED (&nr, pps->pic_init_qp_minus26, -(26 + qp_bd_offset), 25);
1667 READ_SE_ALLOWED (&nr, pps->pic_init_qs_minus26, -26, 25);
1668 READ_SE_ALLOWED (&nr, pps->chroma_qp_index_offset, -12, 12);
1669 pps->second_chroma_qp_index_offset = pps->chroma_qp_index_offset;
1670 READ_UINT8 (&nr, pps->deblocking_filter_control_present_flag, 1);
1671 READ_UINT8 (&nr, pps->constrained_intra_pred_flag, 1);
1672 READ_UINT8 (&nr, pps->redundant_pic_cnt_present_flag, 1);
1674 if (!gst_h264_parser_more_data (&nr))
1677 READ_UINT8 (&nr, pps->transform_8x8_mode_flag, 1);
1679 READ_UINT8 (&nr, pic_scaling_matrix_present_flag, 1);
1680 if (pic_scaling_matrix_present_flag) {
1683 n_lists = 6 + ((sps->chroma_format_idc != 3) ? 2 : 6) *
1684 pps->transform_8x8_mode_flag;
1686 if (sps->scaling_matrix_present_flag) {
1687 if (!gst_h264_parser_parse_scaling_list (&nr,
1688 pps->scaling_lists_4x4, pps->scaling_lists_8x8,
1689 sps->scaling_lists_4x4[0], sps->scaling_lists_4x4[3],
1690 sps->scaling_lists_8x8[0], sps->scaling_lists_8x8[3], n_lists))
1693 if (!gst_h264_parser_parse_scaling_list (&nr,
1694 pps->scaling_lists_4x4, pps->scaling_lists_8x8,
1695 default_4x4_inter, default_4x4_intra,
1696 default_8x8_inter, default_8x8_intra, n_lists))
1701 READ_SE_ALLOWED (&nr, pps->second_chroma_qp_index_offset, -12, 12);
1705 return GST_H264_PARSER_OK;
1708 GST_WARNING ("error parsing \"Picture parameter set\"");
1710 return GST_H264_PARSER_ERROR;
1714 * gst_h264_parser_parse_pps:
1715 * @nalparser: a #GstH264NalParser
1716 * @nalu: The #GST_H264_NAL_PPS #GstH264NalUnit to parse
1717 * @pps: The #GstH264PPS to fill.
1719 * Parses @data, and fills the @pps structure.
1721 * Returns: a #GstH264ParserResult
1724 gst_h264_parser_parse_pps (GstH264NalParser * nalparser,
1725 GstH264NalUnit * nalu, GstH264PPS * pps)
1727 GstH264ParserResult res = gst_h264_parse_pps (nalparser, nalu, pps);
1729 if (res == GST_H264_PARSER_OK) {
1730 GST_DEBUG ("adding picture parameter set with id: %d to array", pps->id);
1732 nalparser->pps[pps->id] = *pps;
1733 nalparser->last_pps = &nalparser->pps[pps->id];
1740 * gst_h264_parser_parse_slice_hdr:
1741 * @nalparser: a #GstH264NalParser
1742 * @nalu: The #GST_H264_NAL_SLICE #GstH264NalUnit to parse
1743 * @slice: The #GstH264SliceHdr to fill.
1744 * @parse_pred_weight_table: Whether to parse the pred_weight_table or not
1745 * @parse_dec_ref_pic_marking: Whether to parse the dec_ref_pic_marking or not
1747 * Parses @data, and fills the @slice structure.
1749 * Returns: a #GstH264ParserResult
1752 gst_h264_parser_parse_slice_hdr (GstH264NalParser * nalparser,
1753 GstH264NalUnit * nalu, GstH264SliceHdr * slice,
1754 gboolean parse_pred_weight_table, gboolean parse_dec_ref_pic_marking)
1762 GST_DEBUG ("Invalid Nal Unit");
1763 return GST_H264_PARSER_ERROR;
1767 nal_reader_init (&nr, nalu->data + nalu->offset + 1, nalu->size - 1);
1769 READ_UE (&nr, slice->first_mb_in_slice);
1770 READ_UE (&nr, slice->type);
1772 GST_DEBUG ("parsing \"Slice header\", slice type %u", slice->type);
1774 READ_UE_ALLOWED (&nr, pps_id, 0, GST_H264_MAX_PPS_COUNT - 1);
1775 pps = gst_h264_parser_get_pps (nalparser, pps_id);
1778 GST_WARNING ("couldn't find associated picture parameter set with id: %d",
1781 return GST_H264_PARSER_BROKEN_LINK;
1785 sps = pps->sequence;
1787 GST_WARNING ("couldn't find associated sequence parameter set with id: %d",
1789 return GST_H264_PARSER_BROKEN_LINK;
1792 /* set default values for fields that might not be present in the bitstream
1793 and have valid defaults */
1794 slice->field_pic_flag = 0;
1795 slice->bottom_field_flag = 0;
1796 slice->delta_pic_order_cnt_bottom = 0;
1797 slice->delta_pic_order_cnt[0] = 0;
1798 slice->delta_pic_order_cnt[1] = 0;
1799 slice->redundant_pic_cnt = 0;
1800 slice->num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_active_minus1;
1801 slice->num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_active_minus1;
1802 slice->disable_deblocking_filter_idc = 0;
1803 slice->slice_alpha_c0_offset_div2 = 0;
1805 if (sps->separate_colour_plane_flag)
1806 READ_UINT8 (&nr, slice->colour_plane_id, 2);
1808 READ_UINT16 (&nr, slice->frame_num, sps->log2_max_frame_num_minus4 + 4);
1810 if (!sps->frame_mbs_only_flag) {
1811 READ_UINT8 (&nr, slice->field_pic_flag, 1);
1812 if (slice->field_pic_flag)
1813 READ_UINT8 (&nr, slice->bottom_field_flag, 1);
1816 /* calculate MaxPicNum */
1817 if (slice->field_pic_flag)
1818 slice->max_pic_num = sps->max_frame_num;
1820 slice->max_pic_num = 2 * sps->max_frame_num;
1822 if (nalu->type == 5)
1823 READ_UE_ALLOWED (&nr, slice->idr_pic_id, 0, G_MAXUINT16);
1825 if (sps->pic_order_cnt_type == 0) {
1826 READ_UINT16 (&nr, slice->pic_order_cnt_lsb,
1827 sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
1829 if (pps->pic_order_present_flag && !slice->field_pic_flag)
1830 READ_SE (&nr, slice->delta_pic_order_cnt_bottom);
1833 if (sps->pic_order_cnt_type == 1 && !sps->delta_pic_order_always_zero_flag) {
1834 READ_SE (&nr, slice->delta_pic_order_cnt[0]);
1835 if (pps->pic_order_present_flag && !slice->field_pic_flag)
1836 READ_SE (&nr, slice->delta_pic_order_cnt[1]);
1839 if (pps->redundant_pic_cnt_present_flag)
1840 READ_UE_ALLOWED (&nr, slice->redundant_pic_cnt, 0, G_MAXINT8);
1842 if (GST_H264_IS_B_SLICE (slice))
1843 READ_UINT8 (&nr, slice->direct_spatial_mv_pred_flag, 1);
1845 if (GST_H264_IS_P_SLICE (slice) || GST_H264_IS_SP_SLICE (slice) ||
1846 GST_H264_IS_B_SLICE (slice)) {
1847 guint8 num_ref_idx_active_override_flag;
1849 READ_UINT8 (&nr, num_ref_idx_active_override_flag, 1);
1850 if (num_ref_idx_active_override_flag) {
1851 READ_UE_ALLOWED (&nr, slice->num_ref_idx_l0_active_minus1, 0, 31);
1853 if (GST_H264_IS_B_SLICE (slice))
1854 READ_UE_ALLOWED (&nr, slice->num_ref_idx_l1_active_minus1, 0, 31);
1858 if (!slice_parse_ref_pic_list_modification (slice, &nr))
1861 if ((pps->weighted_pred_flag && (GST_H264_IS_P_SLICE (slice)
1862 || GST_H264_IS_SP_SLICE (slice)))
1863 || (pps->weighted_bipred_idc == 1 && GST_H264_IS_B_SLICE (slice))) {
1864 if (!gst_h264_slice_parse_pred_weight_table (slice, &nr,
1865 sps->chroma_array_type))
1869 if (nalu->ref_idc != 0) {
1870 if (!gst_h264_slice_parse_dec_ref_pic_marking (slice, nalu, &nr))
1874 if (pps->entropy_coding_mode_flag && !GST_H264_IS_I_SLICE (slice) &&
1875 !GST_H264_IS_SI_SLICE (slice))
1876 READ_UE_ALLOWED (&nr, slice->cabac_init_idc, 0, 2);
1878 READ_SE_ALLOWED (&nr, slice->slice_qp_delta, -87, 77);
1880 if (GST_H264_IS_SP_SLICE (slice) || GST_H264_IS_SI_SLICE (slice)) {
1881 guint8 sp_for_switch_flag;
1883 if (GST_H264_IS_SP_SLICE (slice))
1884 READ_UINT8 (&nr, sp_for_switch_flag, 1);
1885 READ_SE_ALLOWED (&nr, slice->slice_qs_delta, -51, 51);
1888 if (pps->deblocking_filter_control_present_flag) {
1889 READ_UE_ALLOWED (&nr, slice->disable_deblocking_filter_idc, 0, 2);
1890 if (slice->disable_deblocking_filter_idc != 1) {
1891 READ_SE_ALLOWED (&nr, slice->slice_alpha_c0_offset_div2, -6, 6);
1892 READ_SE_ALLOWED (&nr, slice->slice_beta_offset_div2, -6, 6);
1896 if (pps->num_slice_groups_minus1 > 0 &&
1897 pps->slice_group_map_type >= 3 && pps->slice_group_map_type <= 5) {
1898 /* Ceil(Log2(PicSizeInMapUnits / SliceGroupChangeRate + 1)) [7-33] */
1899 guint32 PicWidthInMbs = sps->pic_width_in_mbs_minus1 + 1;
1900 guint32 PicHeightInMapUnits = sps->pic_height_in_map_units_minus1 + 1;
1901 guint32 PicSizeInMapUnits = PicWidthInMbs * PicHeightInMapUnits;
1902 guint32 SliceGroupChangeRate = pps->slice_group_change_rate_minus1 + 1;
1903 const guint n = ceil_log2 (PicSizeInMapUnits / SliceGroupChangeRate + 1);
1904 READ_UINT16 (&nr, slice->slice_group_change_cycle, n);
1907 slice->header_size = nal_reader_get_pos (&nr);
1908 slice->n_emulation_prevention_bytes = nal_reader_get_epb_count (&nr);
1910 return GST_H264_PARSER_OK;
1913 GST_WARNING ("error parsing \"Slice header\"");
1914 return GST_H264_PARSER_ERROR;
1918 * gst_h264_parser_parse_sei:
1919 * @nalparser: a #GstH264NalParser
1920 * @nalu: The #GST_H264_NAL_SEI #GstH264NalUnit to parse
1921 * @sei: The #GstH264SEIMessage to fill.
1923 * Parses @data, and fills the @sei structures.
1925 * Returns: a #GstH264ParserResult
1928 gst_h264_parser_parse_sei (GstH264NalParser * nalparser, GstH264NalUnit * nalu,
1929 GstH264SEIMessage * sei)
1933 guint32 payloadSize;
1934 guint8 payload_type_byte, payload_size_byte;
1935 guint remaining, payload_size;
1938 GST_DEBUG ("parsing \"Sei message\"");
1940 nal_reader_init (&nr, nalu->data + nalu->offset + 1, nalu->size - 1);
1943 memset (sei, 0, sizeof (*sei));
1945 sei->payloadType = 0;
1947 READ_UINT8 (&nr, payload_type_byte, 8);
1948 sei->payloadType += payload_type_byte;
1949 } while (payload_type_byte == 0xff);
1953 READ_UINT8 (&nr, payload_size_byte, 8);
1954 payloadSize += payload_size_byte;
1956 while (payload_size_byte == 0xff);
1958 remaining = nal_reader_get_remaining (&nr) * 8;
1959 payload_size = payloadSize < remaining ? payloadSize : remaining;
1961 GST_DEBUG ("SEI message received: payloadType %u, payloadSize = %u bytes",
1962 sei->payloadType, payload_size);
1964 if (sei->payloadType == GST_H264_SEI_BUF_PERIOD) {
1965 /* size not set; might depend on emulation_prevention_three_byte */
1966 res = gst_h264_parser_parse_buffering_period (nalparser,
1967 &sei->payload.buffering_period, &nr);
1968 } else if (sei->payloadType == GST_H264_SEI_PIC_TIMING) {
1969 /* size not set; might depend on emulation_prevention_three_byte */
1970 res = gst_h264_parser_parse_pic_timing (nalparser,
1971 &sei->payload.pic_timing, &nr);
1973 res = GST_H264_PARSER_OK;
1978 GST_WARNING ("error parsing \"Sei message\"");
1979 return GST_H264_PARSER_ERROR;