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