1 /*
2 * Common H.264 and HEVC Supplementary Enhancement Information messages
3 *
4 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * Copyright (C) 2012 - 2013 Guillaume Martres
6 * Copyright (C) 2012 - 2013 Gildas Cocherel
7 * Copyright (C) 2013 Vittorio Giovara
8 *
9 * This file is part of FFmpeg.
10 *
11 * FFmpeg is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * FFmpeg is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with FFmpeg; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include "config_components.h"
27
33
41
42 #define IS_H264(codec_id) (CONFIG_H264_SEI && CONFIG_HEVC_SEI ? codec_id == AV_CODEC_ID_H264 : CONFIG_H264_SEI)
43 #define IS_HEVC(codec_id) (CONFIG_H264_SEI && CONFIG_HEVC_SEI ? codec_id == AV_CODEC_ID_HEVC : CONFIG_HEVC_SEI)
44
45 #if CONFIG_HEVC_SEI
48 {
49 size_t meta_size;
50 int err;
52 if (!metadata)
54
57 if (err < 0) {
59 return err;
60 }
61
67 }
68
69 return 0;
70 }
71
74 {
75 size_t meta_size;
76 int err;
78 if (!metadata)
80
83 if (err < 0) {
85 return err;
86 }
87
93 }
94
95 return 0;
96 }
97 #endif
98
100 {
102
105
106 flag = !!(bytestream2_get_byteu(gb) & 0x40);
// active_format_flag
107
111 h->active_format_description = bytestream2_get_byteu(gb) & 0xF;
113 }
114
115 return 0;
116 }
117
120 {
123 }
124
127 {
128 int country_code, provider_code;
129
132
133 country_code = bytestream2_get_byteu(gb); // itu_t_t35_country_code
134 if (country_code == 0xFF) {
137
139 }
140
141 if (country_code != 0xB5 && country_code != 0x26) { // usa_country_code and cn_country_code
143 "Unsupported User Data Registered ITU-T T35 SEI message (country_code = %d)\n",
144 country_code);
145 return 0;
146 }
147
148 /* itu_t_t35_payload_byte follows */
149 provider_code = bytestream2_get_be16u(gb);
150
151 switch (provider_code) {
152 case 0x31: { // atsc_provider_code
153 uint32_t user_identifier;
154
157
158 user_identifier = bytestream2_get_be32u(gb);
159 switch (user_identifier) {
160 case MKBETAG(
'D',
'T',
'G',
'1'):
// afd_data
162 case MKBETAG(
'G',
'A',
'9',
'4'):
// closed captions
164 default:
166 "Unsupported User Data Registered ITU-T T35 SEI message (atsc user_identifier = 0x%04x)\n",
167 user_identifier);
168 break;
169 }
170 break;
171 }
172 #if CONFIG_HEVC_SEI
173 case 0x04: { // cuva_provider_code
174 const uint16_t cuva_provider_oriented_code = 0x0005;
175 uint16_t provider_oriented_code;
176
178 goto unsupported_provider_code;
179
182
183 provider_oriented_code = bytestream2_get_be16u(gb);
184 if (provider_oriented_code == cuva_provider_oriented_code) {
185 return decode_registered_user_data_dynamic_hdr_vivid(&
h->dynamic_hdr_vivid, gb);
186 }
187 break;
188 }
189 case 0x3C: { // smpte_provider_code
190 // A/341 Amendment - 2094-40
191 const uint16_t smpte2094_40_provider_oriented_code = 0x0001;
192 const uint8_t smpte2094_40_application_identifier = 0x04;
193 uint16_t provider_oriented_code;
194 uint8_t application_identifier;
195
197 goto unsupported_provider_code;
198
201
202 provider_oriented_code = bytestream2_get_be16u(gb);
203 application_identifier = bytestream2_get_byteu(gb);
204 if (provider_oriented_code == smpte2094_40_provider_oriented_code &&
205 application_identifier == smpte2094_40_application_identifier) {
206 return decode_registered_user_data_dynamic_hdr_plus(&
h->dynamic_hdr_plus, gb);
207 }
208 break;
209 }
210 unsupported_provider_code:
211 #endif
212 default:
214 "Unsupported User Data Registered ITU-T T35 SEI message (provider_code = %d)\n",
215 provider_code);
216 break;
217 }
218
219 return 0;
220 }
221
225 {
229
230 if (size < 16 || size >= INT_MAX - 1)
232
237
239 if (!buf_ref)
242
246 h->buf_ref[
h->nb_buf_ref++] = buf_ref;
247
249 int e, build;
250 e = sscanf(
user_data + 16,
"x264 - core %d", &build);
251 if (e == 1 && build > 0)
252 h->x264_build = build;
253 if (e == 1 && build == 1 && !strncmp(
user_data+16,
"x264 - core 0000", 16))
255 }
256
257 return 0;
258 }
259
262 {
263 h->present = !
get_bits1(gb);
// display_orientation_cancel_flag
264
268
269 h->anticlockwise_rotation =
get_bits(gb, 16);
270 // This is followed by display_orientation_repetition_period
271 // and display_orientation_extension_flag for H.264
272 // and by display_orientation_persistence_flag for HEVC.
273 }
274
275 return 0;
276 }
277
281 {
284 h->present = !
h->arrangement_cancel_flag;
285
289 h->content_interpretation_type =
get_bits(gb, 6);
290
291 // spatial_flipping_flag, frame0_flipped_flag, field_views_flag
293 h->current_frame_is_frame0_flag =
get_bits1(gb);
294 // frame0_self_contained_flag, frame1_self_contained_flag
296
297 if (!
h->quincunx_sampling_flag &&
h->arrangement_type != 5)
298 skip_bits(gb, 16);
// frame[01]_grid_position_[xy]
299 skip_bits(gb, 8);
// frame_packing_arrangement_reserved_byte
302 else
303 skip_bits1(gb);
// frame_packing_arrangement_persistence_flag
304 }
305 // H.264: frame_packing_arrangement_extension_flag,
306 // HEVC: upsampled_aspect_ratio_flag
308
309 return 0;
310 }
311
314 {
317
319 s->preferred_transfer_characteristics = bytestream2_get_byteu(gb);
320
321 return 0;
322 }
323
326 {
327 static const uint16_t max_ambient_light_value = 50000;
328
331
332 s->ambient_illuminance = bytestream2_get_be32u(gb);
333 if (!
s->ambient_illuminance)
335
336 s->ambient_light_x = bytestream2_get_be16u(gb);
337 if (
s->ambient_light_x > max_ambient_light_value)
339
340 s->ambient_light_y = bytestream2_get_be16u(gb);
341 if (
s->ambient_light_y > max_ambient_light_value)
343
345
346 return 0;
347 }
348
351 {
352 h->present = !
get_bits1(gb);
// film_grain_characteristics_cancel_flag
353
355 memset(
h, 0,
sizeof(*
h));
357 h->separate_colour_description_present_flag =
get_bits1(gb);
358 if (
h->separate_colour_description_present_flag) {
360 h->bit_depth_chroma =
get_bits(gb, 3) + 8;
363 h->transfer_characteristics =
get_bits(gb, 8);
365 }
368 for (
int c = 0;
c < 3;
c++)
370 for (
int c = 0;
c < 3;
c++) {
371 if (
h->comp_model_present_flag[
c]) {
372 h->num_intensity_intervals[
c] =
get_bits(gb, 8) + 1;
374 if (
h->num_model_values[
c] > 6)
376 for (
int i = 0;
i <
h->num_intensity_intervals[
c];
i++) {
377 h->intensity_interval_lower_bound[
c][
i] =
get_bits(gb, 8);
378 h->intensity_interval_upper_bound[
c][
i] =
get_bits(gb, 8);
379 for (
int j = 0; j <
h->num_model_values[
c]; j++)
381 }
382 }
383 }
386 else
388
390 }
391
392 return 0;
393 }
394
398 {
414 gbyte);
415 default:
417 }
418 }
419
421 {
423 src->a53_caption.buf_ref);
426
430
431 if (
src->unregistered.nb_buf_ref) {
433 src->unregistered.nb_buf_ref,
437
438 for (
unsigned i = 0;
i <
src->unregistered.nb_buf_ref;
i++) {
443 }
444 }
445
446 return 0;
447 }
448
450 {
454 else
457 }
458
462 unsigned bit_depth_luma, unsigned bit_depth_chroma,
464 {
466
469 fp->content_interpretation_type > 0 &&
470 fp->content_interpretation_type < 3) {
472
473 if (!stereo)
475
476 switch (
fp->arrangement_type) {
477 #if CONFIG_H264_SEI
480 break;
483 break;
486 break;
487 #endif
489 if (
fp->quincunx_sampling_flag)
491 else
493 break;
496 break;
499 break;
500 #if CONFIG_H264_SEI
503 break;
504 #endif
505 }
506
507 if (
fp->content_interpretation_type == 2)
509
511 if (
fp->current_frame_is_frame0_flag)
513 else
515 }
516 }
517
518 if (
sei->display_orientation.present &&
519 (
sei->display_orientation.anticlockwise_rotation ||
520 sei->display_orientation.hflip ||
521 sei->display_orientation.vflip)) {
527 if (!rotation)
529
530 /* av_display_rotation_set() expects the angle in the clockwise
531 * direction, hence the first minus.
532 * The below code applies the flips after the rotation, yet
533 * the H.2645 specs require flipping to be applied first.
534 * Because of R O(phi) = O(-phi) R (where R is flipping around
535 * an arbitatry axis and O(phi) is the proper rotation by phi)
536 * we can create display matrices as desired by negating
537 * the degree once for every flip applied. */
538 angle = -angle * (1 - 2 * !!o->
hflip) * (1 - 2 * !!o->
vflip);
542 }
543
544 if (
sei->a53_caption.buf_ref) {
547 if (!sd)
550 if (avctx)
552 }
553
554 for (
unsigned i = 0;
i <
sei->unregistered.nb_buf_ref;
i++) {
556
561 if (!sd)
564 }
565 }
566 sei->unregistered.nb_buf_ref = 0;
567
568 if (
sei->afd.present) {
570 sizeof(uint8_t));
571
572 if (sd) {
573 *sd->
data =
sei->afd.active_format_description;
574 sei->afd.present = 0;
575 }
576 }
577
578 if (
sei->film_grain_characteristics.present) {
582
583 if (!fgp)
585
588
590
599 } else {
604 else
610 } else {
614 }
615 }
618
631
634 else
636
637 if (avctx)
639 }
640
641 if (
sei->ambient_viewing_environment.present) {
643 &
sei->ambient_viewing_environment;
644
647 if (!dst_env)
649
653 }
654
655 return 0;
656 }
657
659 {
661
662 for (
unsigned i = 0;
i <
s->unregistered.nb_buf_ref;
i++)
664 s->unregistered.nb_buf_ref = 0;
668
669 s->ambient_viewing_environment.present = 0;
670 }