hevc: Use switch instead of if-nests in decode_nal_sei_message
[libav.git] / libavcodec / hevc_sei.c
CommitLineData
064698d3
GM
1/*
2 * HEVC Supplementary Enhancement Information messages
3 *
4 * Copyright (C) 2012 - 2013 Guillaume Martres
5 * Copyright (C) 2012 - 2013 Gildas Cocherel
6 * Copyright (C) 2013 Vittorio Giovara
7 *
8 * This file is part of Libav.
9 *
10 * Libav 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.
14 *
15 * Libav 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.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with Libav; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#include "golomb.h"
26#include "hevc.h"
27
2cd841c0
LB
28enum HEVC_SEI_TYPE {
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,
54};
55
043f46f5 56static int decode_nal_sei_decoded_picture_hash(HEVCContext *s)
064698d3
GM
57{
58 int cIdx, i;
59 GetBitContext *gb = &s->HEVClc.gb;
60 uint8_t hash_type = get_bits(gb, 8);
61
62 for (cIdx = 0; cIdx < 3; cIdx++) {
63 if (hash_type == 0) {
64 s->is_md5 = 1;
65 for (i = 0; i < 16; i++)
66 s->md5[cIdx][i] = get_bits(gb, 8);
67 } else if (hash_type == 1) {
68 // picture_crc = get_bits(gb, 16);
69 skip_bits(gb, 16);
70 } else if (hash_type == 2) {
71 // picture_checksum = get_bits(gb, 32);
72 skip_bits(gb, 32);
73 }
74 }
043f46f5 75 return 0;
064698d3
GM
76}
77
043f46f5 78static int decode_nal_sei_frame_packing_arrangement(HEVCContext *s)
064698d3 79{
acb77dff 80 GetBitContext *gb = &s->HEVClc.gb;
064698d3
GM
81
82 get_ue_golomb(gb); // frame_packing_arrangement_id
3a149e23
LB
83 s->sei_frame_packing_present = !get_bits1(gb);
84
85 if (s->sei_frame_packing_present) {
86 s->frame_packing_arrangement_type = get_bits(gb, 7);
87 s->quincunx_subsampling = get_bits1(gb);
88 s->content_interpretation_type = get_bits(gb, 6);
064698d3
GM
89
90 // the following skips spatial_flipping_flag frame0_flipped_flag
91 // field_views_flag current_frame_is_frame0_flag
92 // frame0_self_contained_flag frame1_self_contained_flag
93 skip_bits(gb, 6);
94
3a149e23 95 if (!s->quincunx_subsampling && s->frame_packing_arrangement_type != 5)
064698d3
GM
96 skip_bits(gb, 16); // frame[01]_grid_position_[xy]
97 skip_bits(gb, 8); // frame_packing_arrangement_reserved_byte
98 skip_bits1(gb); // frame_packing_arrangement_persistance_flag
99 }
100 skip_bits1(gb); // upsampled_aspect_ratio_flag
043f46f5 101 return 0;
064698d3
GM
102}
103
043f46f5 104static int decode_nal_sei_display_orientation(HEVCContext *s)
0569a7e0
VG
105{
106 GetBitContext *gb = &s->HEVClc.gb;
107
108 s->sei_display_orientation_present = !get_bits1(gb);
109
110 if (s->sei_display_orientation_present) {
111 s->sei_hflip = get_bits1(gb); // hor_flip
112 s->sei_vflip = get_bits1(gb); // ver_flip
113
114 s->sei_anticlockwise_rotation = get_bits(gb, 16);
115 skip_bits1(gb); // display_orientation_persistence_flag
116 }
043f46f5
LB
117
118 return 0;
0569a7e0
VG
119}
120
064698d3
GM
121static int decode_nal_sei_message(HEVCContext *s)
122{
123 GetBitContext *gb = &s->HEVClc.gb;
124
125 int payload_type = 0;
126 int payload_size = 0;
127 int byte = 0xFF;
128 av_log(s->avctx, AV_LOG_DEBUG, "Decoding SEI\n");
129
130 while (byte == 0xFF) {
131 byte = get_bits(gb, 8);
132 payload_type += byte;
133 }
134 byte = 0xFF;
135 while (byte == 0xFF) {
136 byte = get_bits(gb, 8);
137 payload_size += byte;
138 }
139 if (s->nal_unit_type == NAL_SEI_PREFIX) {
043f46f5
LB
140 switch (payload_type) {
141 case 256: // Mismatched value from HM 8.1
142 return decode_nal_sei_decoded_picture_hash(s);
143 case SEI_TYPE_FRAME_PACKING:
144 return decode_nal_sei_frame_packing_arrangement(s);
145 case SEI_TYPE_DISPLAY_ORIENTATION:
146 return decode_nal_sei_display_orientation(s);
147 default:
064698d3
GM
148 av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", payload_type);
149 skip_bits(gb, 8 * payload_size);
043f46f5 150 return 0;
064698d3
GM
151 }
152 } else { /* nal_unit_type == NAL_SEI_SUFFIX */
043f46f5
LB
153 switch (payload_type) {
154 case SEI_TYPE_DECODED_PICTURE_HASH:
155 return decode_nal_sei_decoded_picture_hash(s);
156 default:
064698d3
GM
157 av_log(s->avctx, AV_LOG_DEBUG, "Skipped SUFFIX SEI %d\n", payload_type);
158 skip_bits(gb, 8 * payload_size);
043f46f5 159 return 0;
064698d3
GM
160 }
161 }
162 return 0;
163}
164
165static int more_rbsp_data(GetBitContext *gb)
166{
167 return get_bits_left(gb) > 0 && show_bits(gb, 8) != 0x80;
168}
169
170int ff_hevc_decode_nal_sei(HEVCContext *s)
171{
172 do {
173 decode_nal_sei_message(s);
174 } while (more_rbsp_data(&s->HEVClc.gb));
175 return 0;
176}