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
29 SEI_TYPE_BUFFERING_PERIOD = 0,
30 SEI_TYPE_PICTURE_TIMING = 1,
31 SEI_TYPE_PAN_SCAN_RECT = 2,
32 SEI_TYPE_FILLER_PAYLOAD = 3,
33 SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35 = 4,
34 SEI_TYPE_USER_DATA_UNREGISTERED = 5,
35 SEI_TYPE_RECOVERY_POINT = 6,
36 SEI_TYPE_SCENE_INFO = 9,
37 SEI_TYPE_FULL_FRAME_SNAPSHOT = 15,
38 SEI_TYPE_PROGRESSIVE_REFINEMENT_SEGMENT_START = 16,
39 SEI_TYPE_PROGRESSIVE_REFINEMENT_SEGMENT_END = 17,
40 SEI_TYPE_FILM_GRAIN_CHARACTERISTICS = 19,
41 SEI_TYPE_POST_FILTER_HINT = 22,
42 SEI_TYPE_TONE_MAPPING_INFO = 23,
43 SEI_TYPE_FRAME_PACKING = 45,
44 SEI_TYPE_DISPLAY_ORIENTATION = 47,
45 SEI_TYPE_SOP_DESCRIPTION = 128,
46 SEI_TYPE_ACTIVE_PARAMETER_SETS = 129,
47 SEI_TYPE_DECODING_UNIT_INFO = 130,
48 SEI_TYPE_TEMPORAL_LEVEL0_INDEX = 131,
49 SEI_TYPE_DECODED_PICTURE_HASH = 132,
50 SEI_TYPE_SCALABLE_NESTING = 133,
51 SEI_TYPE_REGION_REFRESH_INFO = 134,
52 SEI_TYPE_MASTERING_DISPLAY_INFO = 137,
53 SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO = 144,
56 static int decode_nal_sei_decoded_picture_hash(HEVCContext *s)
60 //uint16_t picture_crc;
61 //uint32_t picture_checksum;
62 GetBitContext *gb = &s->HEVClc->gb;
63 hash_type = get_bits(gb, 8);
65 for (cIdx = 0; cIdx < 3/*((s->sps->chroma_format_idc == 0) ? 1 : 3)*/; cIdx++) {
68 for (i = 0; i < 16; i++)
69 s->md5[cIdx][i] = get_bits(gb, 8);
70 } else if (hash_type == 1) {
71 // picture_crc = get_bits(gb, 16);
73 } else if (hash_type == 2) {
74 // picture_checksum = get_bits_long(gb, 32);
81 static int decode_nal_sei_frame_packing_arrangement(HEVCContext *s)
83 GetBitContext *gb = &s->HEVClc->gb;
85 get_ue_golomb(gb); // frame_packing_arrangement_id
86 s->sei_frame_packing_present = !get_bits1(gb);
88 if (s->sei_frame_packing_present) {
89 s->frame_packing_arrangement_type = get_bits(gb, 7);
90 s->quincunx_subsampling = get_bits1(gb);
91 s->content_interpretation_type = get_bits(gb, 6);
93 // the following skips spatial_flipping_flag frame0_flipped_flag
94 // field_views_flag current_frame_is_frame0_flag
95 // frame0_self_contained_flag frame1_self_contained_flag
98 if (!s->quincunx_subsampling && s->frame_packing_arrangement_type != 5)
99 skip_bits(gb, 16); // frame[01]_grid_position_[xy]
100 skip_bits(gb, 8); // frame_packing_arrangement_reserved_byte
101 skip_bits1(gb); // frame_packing_arrangement_persistance_flag
103 skip_bits1(gb); // upsampled_aspect_ratio_flag
107 static int decode_nal_sei_display_orientation(HEVCContext *s)
109 GetBitContext *gb = &s->HEVClc->gb;
111 s->sei_display_orientation_present = !get_bits1(gb);
113 if (s->sei_display_orientation_present) {
114 s->sei_hflip = get_bits1(gb); // hor_flip
115 s->sei_vflip = get_bits1(gb); // ver_flip
117 s->sei_anticlockwise_rotation = get_bits(gb, 16);
118 skip_bits1(gb); // display_orientation_persistence_flag
124 static int decode_pic_timing(HEVCContext *s)
126 GetBitContext *gb = &s->HEVClc->gb;
129 if (!s->ps.sps_list[s->active_seq_parameter_set_id])
130 return(AVERROR(ENOMEM));
131 sps = (HEVCSPS*)s->ps.sps_list[s->active_seq_parameter_set_id]->data;
133 if (sps->vui.frame_field_info_present_flag) {
134 int pic_struct = get_bits(gb, 4);
135 s->picture_struct = AV_PICTURE_STRUCTURE_UNKNOWN;
136 if (pic_struct == 2) {
137 av_log(s->avctx, AV_LOG_DEBUG, "BOTTOM Field\n");
138 s->picture_struct = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
139 } else if (pic_struct == 1) {
140 av_log(s->avctx, AV_LOG_DEBUG, "TOP Field\n");
141 s->picture_struct = AV_PICTURE_STRUCTURE_TOP_FIELD;
143 get_bits(gb, 2); // source_scan_type
144 get_bits(gb, 1); // duplicate_flag
149 static int active_parameter_sets(HEVCContext *s)
151 GetBitContext *gb = &s->HEVClc->gb;
152 int num_sps_ids_minus1;
154 unsigned active_seq_parameter_set_id;
156 get_bits(gb, 4); // active_video_parameter_set_id
157 get_bits(gb, 1); // self_contained_cvs_flag
158 get_bits(gb, 1); // num_sps_ids_minus1
159 num_sps_ids_minus1 = get_ue_golomb_long(gb); // num_sps_ids_minus1
161 if (num_sps_ids_minus1 < 0 || num_sps_ids_minus1 > 15) {
162 av_log(s->avctx, AV_LOG_ERROR, "num_sps_ids_minus1 %d invalid\n", num_sps_ids_minus1);
163 return AVERROR_INVALIDDATA;
166 active_seq_parameter_set_id = get_ue_golomb_long(gb);
167 if (active_seq_parameter_set_id >= MAX_SPS_COUNT) {
168 av_log(s->avctx, AV_LOG_ERROR, "active_parameter_set_id %d invalid\n", active_seq_parameter_set_id);
169 return AVERROR_INVALIDDATA;
171 s->active_seq_parameter_set_id = active_seq_parameter_set_id;
173 for (i = 1; i <= num_sps_ids_minus1; i++)
174 get_ue_golomb_long(gb); // active_seq_parameter_set_id[i]
179 static int decode_nal_sei_prefix(HEVCContext *s, int type, int size)
181 GetBitContext *gb = &s->HEVClc->gb;
184 case 256: // Mismatched value from HM 8.1
185 return decode_nal_sei_decoded_picture_hash(s);
186 case SEI_TYPE_FRAME_PACKING:
187 return decode_nal_sei_frame_packing_arrangement(s);
188 case SEI_TYPE_DISPLAY_ORIENTATION:
189 return decode_nal_sei_display_orientation(s);
190 case SEI_TYPE_PICTURE_TIMING:
192 int ret = decode_pic_timing(s);
193 av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
194 skip_bits(gb, 8 * size);
197 case SEI_TYPE_ACTIVE_PARAMETER_SETS:
198 active_parameter_sets(s);
199 av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
202 av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
203 skip_bits_long(gb, 8 * size);
208 static int decode_nal_sei_suffix(HEVCContext *s, int type, int size)
210 GetBitContext *gb = &s->HEVClc->gb;
213 case SEI_TYPE_DECODED_PICTURE_HASH:
214 return decode_nal_sei_decoded_picture_hash(s);
216 av_log(s->avctx, AV_LOG_DEBUG, "Skipped SUFFIX SEI %d\n", type);
217 skip_bits_long(gb, 8 * size);
222 static int decode_nal_sei_message(HEVCContext *s)
224 GetBitContext *gb = &s->HEVClc->gb;
226 int payload_type = 0;
227 int payload_size = 0;
229 av_log(s->avctx, AV_LOG_DEBUG, "Decoding SEI\n");
231 while (byte == 0xFF) {
232 byte = get_bits(gb, 8);
233 payload_type += byte;
236 while (byte == 0xFF) {
237 byte = get_bits(gb, 8);
238 payload_size += byte;
240 if (s->nal_unit_type == NAL_SEI_PREFIX) {
241 return decode_nal_sei_prefix(s, payload_type, payload_size);
242 } else { /* nal_unit_type == NAL_SEI_SUFFIX */
243 return decode_nal_sei_suffix(s, payload_type, payload_size);
248 static int more_rbsp_data(GetBitContext *gb)
250 return get_bits_left(gb) > 0 && show_bits(gb, 8) != 0x80;
253 int ff_hevc_decode_nal_sei(HEVCContext *s)
258 ret = decode_nal_sei_message(s);
260 return(AVERROR(ENOMEM));
261 } while (more_rbsp_data(&s->HEVClc->gb));