2 * HEVC Supplementary Enhancement Information messages
4 * Copyright (C) 2012 - 2013 Guillaume Martres
5 * Copyright (C) 2012 - 2013 Gildas Cocherel
6 * Copyright (C) 2013 Vittorio Giovara
8 * This file is part of FFmpeg.
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 static void decode_nal_sei_decoded_picture_hash(HEVCContext *s)
32 //uint16_t picture_crc;
33 //uint32_t picture_checksum;
34 GetBitContext *gb = &s->HEVClc->gb;
35 hash_type = get_bits(gb, 8);
37 for (cIdx = 0; cIdx < 3/*((s->sps->chroma_format_idc == 0) ? 1 : 3)*/; cIdx++) {
40 for (i = 0; i < 16; i++)
41 s->md5[cIdx][i] = get_bits(gb, 8);
42 } else if (hash_type == 1) {
43 // picture_crc = get_bits(gb, 16);
45 } else if (hash_type == 2) {
46 // picture_checksum = get_bits(gb, 32);
52 static void decode_nal_sei_frame_packing_arrangement(HEVCContext *s)
54 GetBitContext *gb = &s->HEVClc->gb;
60 get_ue_golomb(gb); // frame_packing_arrangement_id
61 cancel = get_bits1(gb); // frame_packing_cancel_flag
63 type = get_bits(gb, 7); // frame_packing_arrangement_type
64 quincunx = get_bits1(gb); // quincunx_sampling_flag
65 content = get_bits(gb, 6); // content_interpretation_type
67 // the following skips spatial_flipping_flag frame0_flipped_flag
68 // field_views_flag current_frame_is_frame0_flag
69 // frame0_self_contained_flag frame1_self_contained_flag
72 if (quincunx == 0 && type != 5)
73 skip_bits(gb, 16); // frame[01]_grid_position_[xy]
74 skip_bits(gb, 8); // frame_packing_arrangement_reserved_byte
75 skip_bits1(gb); // frame_packing_arrangement_persistance_flag
77 skip_bits1(gb); // upsampled_aspect_ratio_flag
79 s->sei_frame_packing_present = (cancel == 0);
80 s->frame_packing_arrangement_type = type;
81 s->content_interpretation_type = content;
82 s->quincunx_subsampling = quincunx;
85 static int decode_pic_timing(HEVCContext *s)
87 GetBitContext *gb = &s->HEVClc->gb;
90 if (!s->sps_list[s->active_seq_parameter_set_id])
91 return(AVERROR(ENOMEM));
92 sps = (HEVCSPS*)s->sps_list[s->active_seq_parameter_set_id]->data;
94 if (sps->vui.frame_field_info_present_flag) {
95 int pic_struct = get_bits(gb, 4);
96 s->picture_struct = AV_PICTURE_STRUCTURE_UNKNOWN;
97 if (pic_struct == 2) {
98 av_log(s->avctx, AV_LOG_DEBUG, "BOTTOM Field\n");
99 s->picture_struct = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
100 } else if (pic_struct == 1) {
101 av_log(s->avctx, AV_LOG_DEBUG, "TOP Field\n");
102 s->picture_struct = AV_PICTURE_STRUCTURE_TOP_FIELD;
104 get_bits(gb, 2); // source_scan_type
105 get_bits(gb, 1); // duplicate_flag
110 static int active_parameter_sets(HEVCContext *s)
112 GetBitContext *gb = &s->HEVClc->gb;
113 int num_sps_ids_minus1;
115 unsigned active_seq_parameter_set_id;
117 get_bits(gb, 4); // active_video_parameter_set_id
118 get_bits(gb, 1); // self_contained_cvs_flag
119 get_bits(gb, 1); // num_sps_ids_minus1
120 num_sps_ids_minus1 = get_ue_golomb_long(gb); // num_sps_ids_minus1
122 active_seq_parameter_set_id = get_ue_golomb_long(gb);
123 if (active_seq_parameter_set_id >= MAX_SPS_COUNT) {
124 av_log(s->avctx, AV_LOG_ERROR, "active_parameter_set_id %d invalid\n", active_seq_parameter_set_id);
125 return AVERROR_INVALIDDATA;
127 s->active_seq_parameter_set_id = active_seq_parameter_set_id;
129 for (i = 1; i <= num_sps_ids_minus1; i++)
130 get_ue_golomb_long(gb); // active_seq_parameter_set_id[i]
135 static int decode_nal_sei_message(HEVCContext *s)
137 GetBitContext *gb = &s->HEVClc->gb;
139 int payload_type = 0;
140 int payload_size = 0;
142 av_log(s->avctx, AV_LOG_DEBUG, "Decoding SEI\n");
144 while (byte == 0xFF) {
145 byte = get_bits(gb, 8);
146 payload_type += byte;
149 while (byte == 0xFF) {
150 byte = get_bits(gb, 8);
151 payload_size += byte;
153 if (s->nal_unit_type == NAL_SEI_PREFIX) {
154 if (payload_type == 256 /*&& s->decode_checksum_sei*/) {
155 decode_nal_sei_decoded_picture_hash(s);
157 } else if (payload_type == 45) {
158 decode_nal_sei_frame_packing_arrangement(s);
160 } else if (payload_type == 1){
161 int ret = decode_pic_timing(s);
162 av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", payload_type);
163 skip_bits(gb, 8 * payload_size);
165 } else if (payload_type == 129){
166 active_parameter_sets(s);
167 av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", payload_type);
170 av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", payload_type);
171 skip_bits(gb, 8*payload_size);
174 } else { /* nal_unit_type == NAL_SEI_SUFFIX */
175 if (payload_type == 132 /* && s->decode_checksum_sei */)
176 decode_nal_sei_decoded_picture_hash(s);
178 av_log(s->avctx, AV_LOG_DEBUG, "Skipped SUFFIX SEI %d\n", payload_type);
179 skip_bits(gb, 8 * payload_size);
185 static int more_rbsp_data(GetBitContext *gb)
187 return get_bits_left(gb) > 0 && show_bits(gb, 8) != 0x80;
190 int ff_hevc_decode_nal_sei(HEVCContext *s)
195 ret = decode_nal_sei_message(s);
197 return(AVERROR(ENOMEM));
198 } while (more_rbsp_data(&s->HEVClc->gb));