]> git.sesse.net Git - ffmpeg/blob - libavcodec/hevc_sei.c
Merge commit '043f46f5741e1a5caedf55d788e1a72aae3b7605'
[ffmpeg] / libavcodec / hevc_sei.c
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 FFmpeg.
9  *
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.
14  *
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.
19  *
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
23  */
24
25 #include "golomb.h"
26 #include "hevc.h"
27
28 enum 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
56 static int decode_nal_sei_decoded_picture_hash(HEVCContext *s)
57 {
58     int cIdx, i;
59     uint8_t hash_type;
60     //uint16_t picture_crc;
61     //uint32_t picture_checksum;
62     GetBitContext *gb = &s->HEVClc->gb;
63     hash_type = get_bits(gb, 8);
64
65     for (cIdx = 0; cIdx < 3/*((s->sps->chroma_format_idc == 0) ? 1 : 3)*/; cIdx++) {
66         if (hash_type == 0) {
67             s->is_md5 = 1;
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);
72             skip_bits(gb, 16);
73         } else if (hash_type == 2) {
74             // picture_checksum = get_bits_long(gb, 32);
75             skip_bits(gb, 32);
76         }
77     }
78     return 0;
79 }
80
81 static int decode_nal_sei_frame_packing_arrangement(HEVCContext *s)
82 {
83     GetBitContext *gb = &s->HEVClc->gb;
84
85     get_ue_golomb(gb);                  // frame_packing_arrangement_id
86     s->sei_frame_packing_present = !get_bits1(gb);
87
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);
92
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
96         skip_bits(gb, 6);
97
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
102     }
103     skip_bits1(gb);             // upsampled_aspect_ratio_flag
104     return 0;
105 }
106
107 static int decode_nal_sei_display_orientation(HEVCContext *s)
108 {
109     GetBitContext *gb = &s->HEVClc->gb;
110
111     s->sei_display_orientation_present = !get_bits1(gb);
112
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
116
117         s->sei_anticlockwise_rotation = get_bits(gb, 16);
118         skip_bits1(gb);     // display_orientation_persistence_flag
119     }
120
121     return 0;
122 }
123
124 static int decode_pic_timing(HEVCContext *s)
125 {
126     GetBitContext *gb = &s->HEVClc->gb;
127     HEVCSPS *sps;
128
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;
132
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;
142         }
143         get_bits(gb, 2);                   // source_scan_type
144         get_bits(gb, 1);                   // duplicate_flag
145     }
146     return 1;
147 }
148
149 static int active_parameter_sets(HEVCContext *s)
150 {
151     GetBitContext *gb = &s->HEVClc->gb;
152     int num_sps_ids_minus1;
153     int i;
154     unsigned active_seq_parameter_set_id;
155
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
160
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;
164     }
165
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;
170     }
171     s->active_seq_parameter_set_id = active_seq_parameter_set_id;
172
173     for (i = 1; i <= num_sps_ids_minus1; i++)
174         get_ue_golomb_long(gb); // active_seq_parameter_set_id[i]
175
176     return 0;
177 }
178
179 static int decode_nal_sei_message(HEVCContext *s)
180 {
181     GetBitContext *gb = &s->HEVClc->gb;
182
183     int payload_type = 0;
184     int payload_size = 0;
185     int byte = 0xFF;
186     av_log(s->avctx, AV_LOG_DEBUG, "Decoding SEI\n");
187
188     while (byte == 0xFF) {
189         byte          = get_bits(gb, 8);
190         payload_type += byte;
191     }
192     byte = 0xFF;
193     while (byte == 0xFF) {
194         byte          = get_bits(gb, 8);
195         payload_size += byte;
196     }
197     if (s->nal_unit_type == NAL_SEI_PREFIX) {
198         switch (payload_type) {
199         case 256:  // Mismatched value from HM 8.1
200             return decode_nal_sei_decoded_picture_hash(s);
201         case SEI_TYPE_FRAME_PACKING:
202             return decode_nal_sei_frame_packing_arrangement(s);
203         case SEI_TYPE_DISPLAY_ORIENTATION:
204             return decode_nal_sei_display_orientation(s);
205         case SEI_TYPE_PICTURE_TIMING:
206             {
207                 int ret = decode_pic_timing(s);
208                 av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", payload_type);
209                 skip_bits(gb, 8 * payload_size);
210                 return ret;
211             }
212         case SEI_TYPE_ACTIVE_PARAMETER_SETS:
213             active_parameter_sets(s);
214             av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", payload_type);
215             return 0;
216         default:
217             av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", payload_type);
218             skip_bits(gb, 8 * payload_size);
219             return 0;
220         }
221     } else { /* nal_unit_type == NAL_SEI_SUFFIX */
222         switch (payload_type) {
223         case SEI_TYPE_DECODED_PICTURE_HASH:
224             return decode_nal_sei_decoded_picture_hash(s);
225         default:
226             av_log(s->avctx, AV_LOG_DEBUG, "Skipped SUFFIX SEI %d\n", payload_type);
227             skip_bits(gb, 8 * payload_size);
228             return 0;
229         }
230     }
231     return 1;
232 }
233
234 static int more_rbsp_data(GetBitContext *gb)
235 {
236     return get_bits_left(gb) > 0 && show_bits(gb, 8) != 0x80;
237 }
238
239 int ff_hevc_decode_nal_sei(HEVCContext *s)
240 {
241     int ret;
242
243     do {
244         ret = decode_nal_sei_message(s);
245         if (ret < 0)
246             return(AVERROR(ENOMEM));
247     } while (more_rbsp_data(&s->HEVClc->gb));
248     return 1;
249 }