h264: factor out calculating the POC count into a separate file
[libav.git] / libavcodec / h264_sei.c
CommitLineData
9c095463
MN
1/*
2 * H.26L/H.264/AVC/JVT/14496-10/... sei decoding
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 *
2912e87a 5 * This file is part of Libav.
9c095463 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
9c095463
MN
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
9c095463
MN
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
9c095463
MN
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
ba87f080 23 * @file
9c095463
MN
24 * H.264 / AVC / MPEG4 part10 sei decoding.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
9c095463 28#include "avcodec.h"
9c095463 29#include "golomb.h"
066ad249
VG
30#include "h264.h"
31#include "internal.h"
9c095463 32
b1841217
VG
33static const uint8_t sei_num_clock_ts_table[9] = {
34 1, 1, 1, 2, 2, 3, 3, 2, 3
9c095463
MN
35};
36
b1841217
VG
37void ff_h264_reset_sei(H264Context *h)
38{
9c095463
MN
39 h->sei_recovery_frame_cnt = -1;
40 h->sei_dpb_output_delay = 0;
41 h->sei_cpb_removal_delay = -1;
42 h->sei_buffering_period_present = 0;
5b10ef72 43 h->sei_frame_packing_present = 0;
18e3d61e 44 h->sei_display_orientation_present = 0;
271ce76d 45 h->sei_reguserdata_afd_present = 0;
b0017579
KK
46
47 h->a53_caption_size = 0;
48 av_freep(&h->a53_caption);
9c095463
MN
49}
50
b1841217
VG
51static int decode_picture_timing(H264Context *h)
52{
3176217c
AK
53 const SPS *sps = h->ps.sps;
54
55 if (!sps)
56 return AVERROR_INVALIDDATA;
57
58 if (sps->nal_hrd_parameters_present_flag ||
59 sps->vcl_hrd_parameters_present_flag) {
b1841217 60 h->sei_cpb_removal_delay = get_bits(&h->gb,
3176217c 61 sps->cpb_removal_delay_length);
b1841217 62 h->sei_dpb_output_delay = get_bits(&h->gb,
3176217c 63 sps->dpb_output_delay_length);
9c095463 64 }
3176217c 65 if (sps->pic_struct_present_flag) {
9c095463 66 unsigned int i, num_clock_ts;
b1841217 67
2c541554 68 h->sei_pic_struct = get_bits(&h->gb, 4);
9c095463
MN
69 h->sei_ct_type = 0;
70
71 if (h->sei_pic_struct > SEI_PIC_STRUCT_FRAME_TRIPLING)
0d8b943d 72 return AVERROR_INVALIDDATA;
9c095463
MN
73
74 num_clock_ts = sei_num_clock_ts_table[h->sei_pic_struct];
75
b1841217
VG
76 for (i = 0; i < num_clock_ts; i++) {
77 if (get_bits(&h->gb, 1)) { /* clock_timestamp_flag */
9c095463 78 unsigned int full_timestamp_flag;
b1841217
VG
79
80 h->sei_ct_type |= 1 << get_bits(&h->gb, 2);
2c541554
AK
81 skip_bits(&h->gb, 1); /* nuit_field_based_flag */
82 skip_bits(&h->gb, 5); /* counting_type */
83 full_timestamp_flag = get_bits(&h->gb, 1);
84 skip_bits(&h->gb, 1); /* discontinuity_flag */
85 skip_bits(&h->gb, 1); /* cnt_dropped_flag */
86 skip_bits(&h->gb, 8); /* n_frames */
b1841217 87 if (full_timestamp_flag) {
2c541554
AK
88 skip_bits(&h->gb, 6); /* seconds_value 0..59 */
89 skip_bits(&h->gb, 6); /* minutes_value 0..59 */
90 skip_bits(&h->gb, 5); /* hours_value 0..23 */
b1841217
VG
91 } else {
92 if (get_bits(&h->gb, 1)) { /* seconds_flag */
2c541554 93 skip_bits(&h->gb, 6); /* seconds_value range 0..59 */
b1841217 94 if (get_bits(&h->gb, 1)) { /* minutes_flag */
2c541554 95 skip_bits(&h->gb, 6); /* minutes_value 0..59 */
b1841217 96 if (get_bits(&h->gb, 1)) /* hours_flag */
2c541554 97 skip_bits(&h->gb, 5); /* hours_value 0..23 */
9c095463
MN
98 }
99 }
100 }
3176217c 101 if (sps->time_offset_length > 0)
b1841217 102 skip_bits(&h->gb,
3176217c 103 sps->time_offset_length); /* time_offset */
9c095463
MN
104 }
105 }
106
b1841217
VG
107 if (h->avctx->debug & FF_DEBUG_PICT_INFO)
108 av_log(h->avctx, AV_LOG_DEBUG, "ct_type:%X pic_struct:%d\n",
109 h->sei_ct_type, h->sei_pic_struct);
9c095463
MN
110 }
111 return 0;
112}
113
80ea6611
DH
114static int decode_registered_user_data_afd(H264Context *h, int size)
115{
116 int flag;
117
118 if (size-- < 1)
119 return AVERROR_INVALIDDATA;
120 skip_bits(&h->gb, 1); // 0
121 flag = get_bits(&h->gb, 1); // active_format_flag
122 skip_bits(&h->gb, 6); // reserved
123
124 if (flag) {
125 if (size-- < 1)
126 return AVERROR_INVALIDDATA;
127 skip_bits(&h->gb, 4); // reserved
128 h->active_format_description = get_bits(&h->gb, 4);
129 h->sei_reguserdata_afd_present = 1;
130 }
131
132 return 0;
133}
134
135static int decode_registered_user_data_closed_caption(H264Context *h, int size)
136{
137 int flag;
138 int user_data_type_code;
139 int cc_count;
140
141 if (size < 3)
142 return AVERROR(EINVAL);
143
144 user_data_type_code = get_bits(&h->gb, 8);
145 if (user_data_type_code == 0x3) {
146 skip_bits(&h->gb, 1); // reserved
147
148 flag = get_bits(&h->gb, 1); // process_cc_data_flag
149 if (flag) {
150 skip_bits(&h->gb, 1); // zero bit
151 cc_count = get_bits(&h->gb, 5);
152 skip_bits(&h->gb, 8); // reserved
153 size -= 2;
154
155 if (cc_count && size >= cc_count * 3) {
156 const uint64_t new_size = (h->a53_caption_size + cc_count
157 * UINT64_C(3));
158 int i, ret;
159
160 if (new_size > INT_MAX)
161 return AVERROR(EINVAL);
162
163 /* Allow merging of the cc data from two fields. */
164 ret = av_reallocp(&h->a53_caption, new_size);
165 if (ret < 0)
166 return ret;
167
168 for (i = 0; i < cc_count; i++) {
169 h->a53_caption[h->a53_caption_size++] = get_bits(&h->gb, 8);
170 h->a53_caption[h->a53_caption_size++] = get_bits(&h->gb, 8);
171 h->a53_caption[h->a53_caption_size++] = get_bits(&h->gb, 8);
172 }
173
174 skip_bits(&h->gb, 8); // marker_bits
175 }
176 }
f9ab4fe1
JH
177 } else {
178 int i;
61d8fa2a 179 avpriv_request_sample(h->avctx, "Subtitles with data type 0x%02x",
f9ab4fe1
JH
180 user_data_type_code);
181 for (i = 0; i < size - 1; i++)
182 skip_bits(&h->gb, 8);
80ea6611
DH
183 }
184
185 return 0;
186}
187
271ce76d
VG
188static int decode_registered_user_data(H264Context *h, int size)
189{
190 uint32_t country_code;
191 uint32_t user_identifier;
271ce76d
VG
192
193 if (size < 7)
194 return AVERROR_INVALIDDATA;
195 size -= 7;
196
197 country_code = get_bits(&h->gb, 8); // itu_t_t35_country_code
198 if (country_code == 0xFF) {
199 skip_bits(&h->gb, 8); // itu_t_t35_country_code_extension_byte
200 size--;
201 }
202
203 /* itu_t_t35_payload_byte follows */
204 skip_bits(&h->gb, 8); // terminal provider code
205 skip_bits(&h->gb, 8); // terminal provider oriented code
206 user_identifier = get_bits_long(&h->gb, 32);
207
208 switch (user_identifier) {
209 case MKBETAG('D', 'T', 'G', '1'): // afd_data
80ea6611 210 return decode_registered_user_data_afd(h, size);
b0017579 211 case MKBETAG('G', 'A', '9', '4'): // closed captions
80ea6611 212 return decode_registered_user_data_closed_caption(h, size);
271ce76d
VG
213 default:
214 skip_bits(&h->gb, size * 8);
215 break;
216 }
217
218 return 0;
219}
220
b1841217
VG
221static int decode_unregistered_user_data(H264Context *h, int size)
222{
b4a82e74 223 uint8_t *user_data;
9c095463
MN
224 int e, build, i;
225
b4a82e74 226 if (size < 16 || size >= INT_MAX - 16)
0d8b943d 227 return AVERROR_INVALIDDATA;
9c095463 228
b4a82e74
VG
229 user_data = av_malloc(16 + size + 1);
230 if (!user_data)
231 return AVERROR(ENOMEM);
232
233 for (i = 0; i < size + 16; i++)
b1841217 234 user_data[i] = get_bits(&h->gb, 8);
9c095463 235
b1841217 236 user_data[i] = 0;
4c7fd58f 237 e = sscanf(user_data + 16, "x264 - core %d", &build);
b1841217
VG
238 if (e == 1 && build > 0)
239 h->x264_build = build;
9c095463 240
8487987b 241 if (strlen(user_data + 16) > 0)
b1841217 242 av_log(h->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data + 16);
9c095463 243
b4a82e74 244 av_free(user_data);
9c095463
MN
245 return 0;
246}
247
b1841217
VG
248static int decode_recovery_point(H264Context *h)
249{
2c541554 250 h->sei_recovery_frame_cnt = get_ue_golomb(&h->gb);
b1841217
VG
251
252 /* 1b exact_match_flag,
253 * 1b broken_link_flag,
254 * 2b changing_slice_group_idc */
255 skip_bits(&h->gb, 4);
9c095463
MN
256
257 return 0;
258}
259
b1841217
VG
260static int decode_buffering_period(H264Context *h)
261{
9c095463
MN
262 unsigned int sps_id;
263 int sched_sel_idx;
264 SPS *sps;
265
2c541554 266 sps_id = get_ue_golomb_31(&h->gb);
3176217c 267 if (sps_id > 31 || !h->ps.sps_list[sps_id]) {
b1841217
VG
268 av_log(h->avctx, AV_LOG_ERROR,
269 "non-existing SPS %d referenced in buffering period\n", sps_id);
0d8b943d 270 return AVERROR_INVALIDDATA;
9c095463 271 }
3176217c 272 sps = (SPS*)h->ps.sps_list[sps_id]->data;
9c095463
MN
273
274 // NOTE: This is really so duplicated in the standard... See H.264, D.1.1
275 if (sps->nal_hrd_parameters_present_flag) {
276 for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
b1841217
VG
277 h->initial_cpb_removal_delay[sched_sel_idx] =
278 get_bits(&h->gb, sps->initial_cpb_removal_delay_length);
279 // initial_cpb_removal_delay_offset
280 skip_bits(&h->gb, sps->initial_cpb_removal_delay_length);
9c095463
MN
281 }
282 }
283 if (sps->vcl_hrd_parameters_present_flag) {
284 for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
b1841217
VG
285 h->initial_cpb_removal_delay[sched_sel_idx] =
286 get_bits(&h->gb, sps->initial_cpb_removal_delay_length);
287 // initial_cpb_removal_delay_offset
288 skip_bits(&h->gb, sps->initial_cpb_removal_delay_length);
9c095463
MN
289 }
290 }
291
292 h->sei_buffering_period_present = 1;
293 return 0;
294}
295
5b10ef72
VG
296static int decode_frame_packing_arrangement(H264Context *h)
297{
5b10ef72 298 get_ue_golomb(&h->gb); // frame_packing_arrangement_id
3d9fc21e
VG
299 h->sei_frame_packing_present = !get_bits1(&h->gb);
300
301 if (h->sei_frame_packing_present) {
302 h->frame_packing_arrangement_type = get_bits(&h->gb, 7);
303 h->quincunx_subsampling = get_bits1(&h->gb);
304 h->content_interpretation_type = get_bits(&h->gb, 6);
5b10ef72
VG
305
306 // the following skips: spatial_flipping_flag, frame0_flipped_flag,
307 // field_views_flag, current_frame_is_frame0_flag,
308 // frame0_self_contained_flag, frame1_self_contained_flag
309 skip_bits(&h->gb, 6);
310
3d9fc21e 311 if (!h->quincunx_subsampling && h->frame_packing_arrangement_type != 5)
5b10ef72
VG
312 skip_bits(&h->gb, 16); // frame[01]_grid_position_[xy]
313 skip_bits(&h->gb, 8); // frame_packing_arrangement_reserved_byte
314 get_ue_golomb(&h->gb); // frame_packing_arrangement_repetition_period
315 }
316 skip_bits1(&h->gb); // frame_packing_arrangement_extension_flag
317
5b10ef72
VG
318 return 0;
319}
320
18e3d61e
VG
321static int decode_display_orientation(H264Context *h)
322{
323 h->sei_display_orientation_present = !get_bits1(&h->gb);
324
325 if (h->sei_display_orientation_present) {
326 h->sei_hflip = get_bits1(&h->gb); // hor_flip
327 h->sei_vflip = get_bits1(&h->gb); // ver_flip
328
329 h->sei_anticlockwise_rotation = get_bits(&h->gb, 16);
330 get_ue_golomb(&h->gb); // display_orientation_repetition_period
331 skip_bits1(&h->gb); // display_orientation_extension_flag
332 }
333
334 return 0;
335}
336
b1841217
VG
337int ff_h264_decode_sei(H264Context *h)
338{
2c541554 339 while (get_bits_left(&h->gb) > 16) {
b1841217
VG
340 int size = 0;
341 int type = 0;
0d8b943d 342 int ret = 0;
7ab551f9 343 int last = 0;
9c095463 344
7ab551f9
VG
345 while (get_bits_left(&h->gb) >= 8 &&
346 (last = get_bits(&h->gb, 8)) == 255) {
347 type += 255;
348 }
349 type += last;
9c095463 350
7ab551f9
VG
351 last = 0;
352 while (get_bits_left(&h->gb) >= 8 &&
353 (last = get_bits(&h->gb, 8)) == 255) {
354 size += 255;
355 }
356 size += last;
9c095463 357
84df6d6a
MN
358 if (size > get_bits_left(&h->gb) / 8) {
359 av_log(h->avctx, AV_LOG_ERROR, "SEI type %d truncated at %d\n",
360 type, get_bits_left(&h->gb));
361 return AVERROR_INVALIDDATA;
362 }
363
b1841217 364 switch (type) {
9c095463 365 case SEI_TYPE_PIC_TIMING: // Picture timing SEI
0d8b943d 366 ret = decode_picture_timing(h);
9c095463 367 break;
271ce76d
VG
368 case SEI_TYPE_USER_DATA_REGISTERED:
369 ret = decode_registered_user_data(h, size);
370 break;
9c095463 371 case SEI_TYPE_USER_DATA_UNREGISTERED:
0d8b943d 372 ret = decode_unregistered_user_data(h, size);
9c095463
MN
373 break;
374 case SEI_TYPE_RECOVERY_POINT:
0d8b943d 375 ret = decode_recovery_point(h);
9c095463 376 break;
304e916a 377 case SEI_TYPE_BUFFERING_PERIOD:
0d8b943d 378 ret = decode_buffering_period(h);
9c095463 379 break;
5b10ef72
VG
380 case SEI_TYPE_FRAME_PACKING:
381 ret = decode_frame_packing_arrangement(h);
5b10ef72 382 break;
18e3d61e
VG
383 case SEI_TYPE_DISPLAY_ORIENTATION:
384 ret = decode_display_orientation(h);
18e3d61e 385 break;
9c095463 386 default:
26179964 387 av_log(h->avctx, AV_LOG_DEBUG, "unknown SEI type %d\n", type);
b1841217 388 skip_bits(&h->gb, 8 * size);
9c095463 389 }
0bfab80a
VG
390 if (ret < 0)
391 return ret;
9c095463 392
b1841217 393 // FIXME check bits here
2c541554 394 align_get_bits(&h->gb);
9c095463
MN
395 }
396
397 return 0;
398}