]> git.sesse.net Git - ffmpeg/blob - libavcodec/hevc_sei.c
Merge commit 'a553c6a347d3d28d7ee44c3df3d5c4ee780dba23'
[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 "hevc.h"
26 #include "golomb.h"
27
28 static void decode_nal_sei_decoded_picture_hash(HEVCContext *s,
29                                                 int payload_size)
30 {
31     int cIdx, i;
32     uint8_t hash_type;
33     //uint16_t picture_crc;
34     //uint32_t picture_checksum;
35     GetBitContext *gb = &s->HEVClc->gb;
36     hash_type = get_bits(gb, 8);
37
38     for (cIdx = 0; cIdx < 3/*((s->sps->chroma_format_idc == 0) ? 1 : 3)*/; cIdx++) {
39         if (hash_type == 0) {
40             s->is_md5 = 1;
41             for (i = 0; i < 16; i++)
42                 s->md5[cIdx][i] = get_bits(gb, 8);
43         } else if (hash_type == 1) {
44             // picture_crc = get_bits(gb, 16);
45             skip_bits(gb, 16);
46         } else if (hash_type == 2) {
47             // picture_checksum = get_bits(gb, 32);
48             skip_bits(gb, 32);
49         }
50     }
51 }
52
53 static void decode_nal_sei_frame_packing_arrangement(HEVCContext *s)
54 {
55     GetBitContext *gb = &s->HEVClc->gb;
56     int cancel, type, quincunx;
57
58     get_ue_golomb(gb);                  // frame_packing_arrangement_id
59     cancel = get_bits1(gb);             // frame_packing_cancel_flag
60     if (cancel == 0) {
61         type     = get_bits(gb, 7);     // frame_packing_arrangement_type
62         quincunx = get_bits1(gb);       // quincunx_sampling_flag
63         skip_bits(gb, 6);               // content_interpretation_type
64
65         // the following skips spatial_flipping_flag frame0_flipped_flag
66         // field_views_flag current_frame_is_frame0_flag
67         // frame0_self_contained_flag frame1_self_contained_flag
68         skip_bits(gb, 6);
69
70         if (quincunx == 0 && type != 5)
71             skip_bits(gb, 16);  // frame[01]_grid_position_[xy]
72         skip_bits(gb, 8);       // frame_packing_arrangement_reserved_byte
73         skip_bits1(gb);         // frame_packing_arrangement_persistance_flag
74     }
75     skip_bits1(gb);             // upsampled_aspect_ratio_flag
76 }
77
78 static int decode_pic_timing(HEVCContext *s)
79 {
80     GetBitContext *gb = &s->HEVClc->gb;
81     HEVCSPS *sps;
82
83     if (!s->sps_list[s->active_seq_parameter_set_id])
84         return(AVERROR(ENOMEM));
85     sps = (HEVCSPS*)s->sps_list[s->active_seq_parameter_set_id]->data;
86
87     if (sps->vui.frame_field_info_present_flag) {
88         int pic_struct = get_bits(gb, 4);
89         s->picture_struct = AV_PICTURE_STRUCTURE_UNKNOWN;
90         if (pic_struct == 2) {
91             av_log(s->avctx, AV_LOG_DEBUG, "BOTTOM Field\n");
92             s->picture_struct = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
93         } else if (pic_struct == 1) {
94             av_log(s->avctx, AV_LOG_DEBUG, "TOP Field\n");
95             s->picture_struct = AV_PICTURE_STRUCTURE_TOP_FIELD;
96         }
97         get_bits(gb, 2);                   // source_scan_type
98         get_bits(gb, 1);                   // duplicate_flag
99     }
100     return 1;
101 }
102
103 static int active_parameter_sets(HEVCContext *s)
104 {
105     GetBitContext *gb = &s->HEVClc->gb;
106     int num_sps_ids_minus1;
107     int i;
108     unsigned active_seq_parameter_set_id;
109
110     get_bits(gb, 4); // active_video_parameter_set_id
111     get_bits(gb, 1); // self_contained_cvs_flag
112     get_bits(gb, 1); // num_sps_ids_minus1
113     num_sps_ids_minus1 = get_ue_golomb_long(gb); // num_sps_ids_minus1
114
115     active_seq_parameter_set_id = get_ue_golomb_long(gb);
116     if (active_seq_parameter_set_id >= MAX_SPS_COUNT) {
117         av_log(s->avctx, AV_LOG_ERROR, "active_parameter_set_id %d invalid\n", active_seq_parameter_set_id);
118         return AVERROR_INVALIDDATA;
119     }
120     s->active_seq_parameter_set_id = active_seq_parameter_set_id;
121
122     for (i = 1; i <= num_sps_ids_minus1; i++)
123         get_ue_golomb_long(gb); // active_seq_parameter_set_id[i]
124
125     return 0;
126 }
127
128 static int decode_nal_sei_message(HEVCContext *s)
129 {
130     GetBitContext *gb = &s->HEVClc->gb;
131
132     int payload_type = 0;
133     int payload_size = 0;
134     int byte = 0xFF;
135     av_log(s->avctx, AV_LOG_DEBUG, "Decoding SEI\n");
136
137     while (byte == 0xFF) {
138         byte          = get_bits(gb, 8);
139         payload_type += byte;
140     }
141     byte = 0xFF;
142     while (byte == 0xFF) {
143         byte          = get_bits(gb, 8);
144         payload_size += byte;
145     }
146     if (s->nal_unit_type == NAL_SEI_PREFIX) {
147         if (payload_type == 256 /*&& s->decode_checksum_sei*/) {
148             decode_nal_sei_decoded_picture_hash(s, payload_size);
149             return 1;
150         } else if (payload_type == 45) {
151             decode_nal_sei_frame_packing_arrangement(s);
152             return 1;
153         } else if (payload_type == 1){
154             int ret = decode_pic_timing(s);
155             av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", payload_type);
156             skip_bits(gb, 8 * payload_size);
157             return ret;
158         } else if (payload_type == 129){
159             active_parameter_sets(s);
160             av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", payload_type);
161             return 1;
162         } else {
163             av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", payload_type);
164             skip_bits(gb, 8*payload_size);
165             return 1;
166         }
167     } else { /* nal_unit_type == NAL_SEI_SUFFIX */
168         if (payload_type == 132 /* && s->decode_checksum_sei */)
169             decode_nal_sei_decoded_picture_hash(s, payload_size);
170         else {
171             av_log(s->avctx, AV_LOG_DEBUG, "Skipped SUFFIX SEI %d\n", payload_type);
172             skip_bits(gb, 8 * payload_size);
173         }
174         return 1;
175     }
176 }
177
178 static int more_rbsp_data(GetBitContext *gb)
179 {
180     return get_bits_left(gb) > 0 && show_bits(gb, 8) != 0x80;
181 }
182
183 int ff_hevc_decode_nal_sei(HEVCContext *s)
184 {
185     int ret;
186
187     do {
188         ret = decode_nal_sei_message(s);
189         if (ret < 0)
190             return(AVERROR(ENOMEM));
191     } while (more_rbsp_data(&s->HEVClc->gb));
192     return 1;
193 }