| Line | Branch | Exec | Source | 
|---|---|---|---|
| 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 | |||
| 28 | #include "libavutil/ambient_viewing_environment.h" | ||
| 29 | #include "libavutil/buffer.h" | ||
| 30 | #include "libavutil/display.h" | ||
| 31 | #include "libavutil/hdr_dynamic_metadata.h" | ||
| 32 | #include "libavutil/film_grain_params.h" | ||
| 33 | #include "libavutil/mastering_display_metadata.h" | ||
| 34 | #include "libavutil/mem.h" | ||
| 35 | #include "libavutil/refstruct.h" | ||
| 36 | #include "libavutil/stereo3d.h" | ||
| 37 | |||
| 38 | #include "atsc_a53.h" | ||
| 39 | #include "avcodec.h" | ||
| 40 | #include "decode.h" | ||
| 41 | #include "dynamic_hdr_vivid.h" | ||
| 42 | #include "get_bits.h" | ||
| 43 | #include "golomb.h" | ||
| 44 | #include "h2645_sei.h" | ||
| 45 | #include "itut35.h" | ||
| 46 | |||
| 47 | #define IS_H264(codec_id) (CONFIG_H264_SEI && (CONFIG_HEVC_SEI || CONFIG_VVC_SEI ) ? codec_id == AV_CODEC_ID_H264 : CONFIG_H264_SEI) | ||
| 48 | #define IS_HEVC(codec_id) (CONFIG_HEVC_SEI && (CONFIG_H264_SEI || CONFIG_VVC_SEI ) ? codec_id == AV_CODEC_ID_HEVC : CONFIG_HEVC_SEI) | ||
| 49 | #define IS_VVC(codec_id) (CONFIG_VVC_SEI && (CONFIG_H264_SEI || CONFIG_HEVC_SEI) ? codec_id == AV_CODEC_ID_VVC : CONFIG_VVC_SEI ) | ||
| 50 | |||
| 51 | #if CONFIG_HEVC_SEI | ||
| 52 | 11 | static int decode_registered_user_data_dynamic_hdr_plus(HEVCSEIDynamicHDRPlus *s, | |
| 53 | GetByteContext *gb) | ||
| 54 | { | ||
| 55 | size_t meta_size; | ||
| 56 | int err; | ||
| 57 | 11 | AVDynamicHDRPlus *metadata = av_dynamic_hdr_plus_alloc(&meta_size); | |
| 58 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 11 times. 
         | 
      11 | if (!metadata) | 
| 59 | ✗ | return AVERROR(ENOMEM); | |
| 60 | |||
| 61 | 11 | err = av_dynamic_hdr_plus_from_t35(metadata, gb->buffer, | |
| 62 | 11 | bytestream2_get_bytes_left(gb)); | |
| 63 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 11 times. 
         | 
      11 | if (err < 0) { | 
| 64 | ✗ | av_free(metadata); | |
| 65 | ✗ | return err; | |
| 66 | } | ||
| 67 | |||
| 68 | 11 | av_buffer_unref(&s->info); | |
| 69 | 11 | s->info = av_buffer_create((uint8_t *)metadata, meta_size, NULL, NULL, 0); | |
| 70 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 11 times. 
         | 
      11 | if (!s->info) { | 
| 71 | ✗ | av_free(metadata); | |
| 72 | ✗ | return AVERROR(ENOMEM); | |
| 73 | } | ||
| 74 | |||
| 75 | 11 | return 0; | |
| 76 | } | ||
| 77 | |||
| 78 | 3 | static int decode_registered_user_data_dynamic_hdr_vivid(HEVCSEIDynamicHDRVivid *s, | |
| 79 | GetByteContext *gb) | ||
| 80 | { | ||
| 81 | size_t meta_size; | ||
| 82 | int err; | ||
| 83 | 3 | AVDynamicHDRVivid *metadata = av_dynamic_hdr_vivid_alloc(&meta_size); | |
| 84 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 3 times. 
         | 
      3 | if (!metadata) | 
| 85 | ✗ | return AVERROR(ENOMEM); | |
| 86 | |||
| 87 | 3 | err = ff_parse_itu_t_t35_to_dynamic_hdr_vivid(metadata, | |
| 88 | gb->buffer, bytestream2_get_bytes_left(gb)); | ||
| 89 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 3 times. 
         | 
      3 | if (err < 0) { | 
| 90 | ✗ | av_free(metadata); | |
| 91 | ✗ | return err; | |
| 92 | } | ||
| 93 | |||
| 94 | 3 | av_buffer_unref(&s->info); | |
| 95 | 3 | s->info = av_buffer_create((uint8_t *)metadata, meta_size, NULL, NULL, 0); | |
| 96 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 3 times. 
         | 
      3 | if (!s->info) { | 
| 97 | ✗ | av_free(metadata); | |
| 98 | ✗ | return AVERROR(ENOMEM); | |
| 99 | } | ||
| 100 | |||
| 101 | 3 | return 0; | |
| 102 | } | ||
| 103 | #endif | ||
| 104 | |||
| 105 | ✗ | static int decode_registered_user_data_lcevc(HEVCSEILCEVC *s, | |
| 106 | GetByteContext *gb) | ||
| 107 | { | ||
| 108 | ✗ | int size = bytestream2_get_bytes_left(gb); | |
| 109 | |||
| 110 | ✗ | av_buffer_unref(&s->info); | |
| 111 | ✗ | s->info = av_buffer_alloc(size); | |
| 112 | ✗ | if (!s->info) | |
| 113 | ✗ | return AVERROR(ENOMEM); | |
| 114 | |||
| 115 | ✗ | bytestream2_get_bufferu(gb, s->info->data, size); | |
| 116 | ✗ | return 0; | |
| 117 | } | ||
| 118 | |||
| 119 | 1169 | static int decode_registered_user_data_afd(H2645SEIAFD *h, GetByteContext *gb) | |
| 120 | { | ||
| 121 | int flag; | ||
| 122 | |||
| 123 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 1169 times. 
         | 
      1169 | if (bytestream2_get_bytes_left(gb) <= 0) | 
| 124 | ✗ | return AVERROR_INVALIDDATA; | |
| 125 | |||
| 126 | 1169 | flag = !!(bytestream2_get_byteu(gb) & 0x40); // active_format_flag | |
| 127 | |||
| 128 | 
        1/2✓ Branch 0 taken 1169 times. 
          ✗ Branch 1 not taken. 
         | 
      1169 | if (flag) { | 
| 129 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 1169 times. 
         | 
      1169 | if (bytestream2_get_bytes_left(gb) <= 0) | 
| 130 | ✗ | return AVERROR_INVALIDDATA; | |
| 131 | 1169 | h->active_format_description = bytestream2_get_byteu(gb) & 0xF; | |
| 132 | 1169 | h->present = 1; | |
| 133 | } | ||
| 134 | |||
| 135 | 1169 | return 0; | |
| 136 | } | ||
| 137 | |||
| 138 | 6 | static int decode_registered_user_data_closed_caption(H2645SEIA53Caption *h, | |
| 139 | GetByteContext *gb) | ||
| 140 | { | ||
| 141 | 6 | return ff_parse_a53_cc(&h->buf_ref, gb->buffer, | |
| 142 | bytestream2_get_bytes_left(gb)); | ||
| 143 | } | ||
| 144 | |||
| 145 | 1189 | static int decode_registered_user_data(H2645SEI *h, GetByteContext *gb, | |
| 146 | enum AVCodecID codec_id, void *logctx) | ||
| 147 | { | ||
| 148 | int country_code, provider_code; | ||
| 149 | |||
| 150 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 1189 times. 
         | 
      1189 | if (bytestream2_get_bytes_left(gb) < 3) | 
| 151 | ✗ | return AVERROR_INVALIDDATA; | |
| 152 | |||
| 153 | 1189 | country_code = bytestream2_get_byteu(gb); // itu_t_t35_country_code | |
| 154 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 1189 times. 
         | 
      1189 | if (country_code == 0xFF) { | 
| 155 | ✗ | if (bytestream2_get_bytes_left(gb) < 3) | |
| 156 | ✗ | return AVERROR_INVALIDDATA; | |
| 157 | |||
| 158 | ✗ | bytestream2_skipu(gb, 1); // itu_t_t35_country_code_extension_byte | |
| 159 | } | ||
| 160 | |||
| 161 | /* itu_t_t35_payload_byte follows */ | ||
| 162 | 1189 | provider_code = bytestream2_get_be16u(gb); | |
| 163 | |||
| 164 | 
        4/4✓ Branch 0 taken 1186 times. 
          ✓ Branch 1 taken 3 times. 
          ✓ Branch 2 taken 1175 times. 
          ✓ Branch 3 taken 11 times. 
         | 
      1189 | if (country_code == ITU_T_T35_COUNTRY_CODE_US && provider_code == ITU_T_T35_PROVIDER_CODE_ATSC) { | 
| 165 | uint32_t user_identifier; | ||
| 166 | |||
| 167 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 1175 times. 
         | 
      1175 | if (bytestream2_get_bytes_left(gb) < 4) | 
| 168 | ✗ | return AVERROR_INVALIDDATA; | |
| 169 | |||
| 170 | 1175 | user_identifier = bytestream2_get_be32u(gb); | |
| 171 | 
        2/3✓ Branch 0 taken 1169 times. 
          ✓ Branch 1 taken 6 times. 
          ✗ Branch 2 not taken. 
         | 
      1175 | switch (user_identifier) { | 
| 172 | 1169 | case MKBETAG('D', 'T', 'G', '1'): // afd_data | |
| 173 | 1169 | return decode_registered_user_data_afd(&h->afd, gb); | |
| 174 | 6 | case MKBETAG('G', 'A', '9', '4'): // closed captions | |
| 175 | 6 | return decode_registered_user_data_closed_caption(&h->a53_caption, gb); | |
| 176 | ✗ | default: | |
| 177 | ✗ | av_log(logctx, AV_LOG_VERBOSE, | |
| 178 | "Unsupported User Data Registered ITU-T T35 SEI message (atsc user_identifier = 0x%04x)\n", | ||
| 179 | user_identifier); | ||
| 180 | ✗ | break; | |
| 181 | } | ||
| 182 | 
        1/4✗ Branch 0 not taken. 
          ✓ Branch 1 taken 14 times. 
          ✗ Branch 2 not taken. 
          ✗ Branch 3 not taken. 
         | 
      14 | } else if (country_code == ITU_T_T35_COUNTRY_CODE_UK && provider_code == ITU_T_T35_PROVIDER_CODE_VNOVA) { | 
| 183 | ✗ | if (bytestream2_get_bytes_left(gb) < 2) | |
| 184 | ✗ | return AVERROR_INVALIDDATA; | |
| 185 | |||
| 186 | ✗ | bytestream2_skipu(gb, 1); // user_data_type_code | |
| 187 | ✗ | return decode_registered_user_data_lcevc(&h->lcevc, gb); | |
| 188 | } | ||
| 189 | #if CONFIG_HEVC_SEI | ||
| 190 | 
        3/4✓ Branch 0 taken 3 times. 
          ✓ Branch 1 taken 11 times. 
          ✓ Branch 2 taken 3 times. 
          ✗ Branch 3 not taken. 
         | 
      14 | else if (country_code == ITU_T_T35_COUNTRY_CODE_CN && provider_code == ITU_T_T35_PROVIDER_CODE_HDR_VIVID) { | 
| 191 | 3 | const uint16_t cuva_provider_oriented_code = 0x0005; | |
| 192 | uint16_t provider_oriented_code; | ||
| 193 | |||
| 194 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 3 times. 
         | 
      3 | if (!IS_HEVC(codec_id)) | 
| 195 | ✗ | goto unsupported_provider_code; | |
| 196 | |||
| 197 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 3 times. 
         | 
      3 | if (bytestream2_get_bytes_left(gb) < 2) | 
| 198 | ✗ | return AVERROR_INVALIDDATA; | |
| 199 | |||
| 200 | 3 | provider_oriented_code = bytestream2_get_be16u(gb); | |
| 201 | 
        1/2✓ Branch 0 taken 3 times. 
          ✗ Branch 1 not taken. 
         | 
      3 | if (provider_oriented_code == cuva_provider_oriented_code) { | 
| 202 | 3 | return decode_registered_user_data_dynamic_hdr_vivid(&h->dynamic_hdr_vivid, gb); | |
| 203 | } | ||
| 204 | 
        2/4✓ Branch 0 taken 11 times. 
          ✗ Branch 1 not taken. 
          ✓ Branch 2 taken 11 times. 
          ✗ Branch 3 not taken. 
         | 
      11 | } else if(country_code == ITU_T_T35_COUNTRY_CODE_US && provider_code == ITU_T_T35_PROVIDER_CODE_SAMSUNG) { | 
| 205 | // A/341 Amendment - 2094-40 | ||
| 206 | 11 | const uint16_t smpte2094_40_provider_oriented_code = 0x0001; | |
| 207 | 11 | const uint8_t smpte2094_40_application_identifier = 0x04; | |
| 208 | uint16_t provider_oriented_code; | ||
| 209 | uint8_t application_identifier; | ||
| 210 | |||
| 211 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 11 times. 
         | 
      11 | if (!IS_HEVC(codec_id)) | 
| 212 | ✗ | goto unsupported_provider_code; | |
| 213 | |||
| 214 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 11 times. 
         | 
      11 | if (bytestream2_get_bytes_left(gb) < 3) | 
| 215 | ✗ | return AVERROR_INVALIDDATA; | |
| 216 | |||
| 217 | 11 | provider_oriented_code = bytestream2_get_be16u(gb); | |
| 218 | 11 | application_identifier = bytestream2_get_byteu(gb); | |
| 219 | 
        2/4✓ Branch 0 taken 11 times. 
          ✗ Branch 1 not taken. 
          ✓ Branch 2 taken 11 times. 
          ✗ Branch 3 not taken. 
         | 
      11 | if (provider_oriented_code == smpte2094_40_provider_oriented_code && | 
| 220 | application_identifier == smpte2094_40_application_identifier) { | ||
| 221 | 11 | return decode_registered_user_data_dynamic_hdr_plus(&h->dynamic_hdr_plus, gb); | |
| 222 | } | ||
| 223 | ✗ | } else if (country_code == ITU_T_T35_COUNTRY_CODE_US && provider_code == ITU_T_T35_PROVIDER_CODE_AOM) { | |
| 224 | ✗ | const uint16_t aom_grain_provider_oriented_code = 0x0001; | |
| 225 | uint16_t provider_oriented_code; | ||
| 226 | |||
| 227 | ✗ | if (!IS_HEVC(codec_id)) | |
| 228 | ✗ | goto unsupported_provider_code; | |
| 229 | |||
| 230 | ✗ | if (bytestream2_get_bytes_left(gb) < 2) | |
| 231 | ✗ | return AVERROR_INVALIDDATA; | |
| 232 | |||
| 233 | ✗ | provider_oriented_code = bytestream2_get_byteu(gb); | |
| 234 | ✗ | if (provider_oriented_code == aom_grain_provider_oriented_code) { | |
| 235 | ✗ | return ff_aom_parse_film_grain_sets(&h->aom_film_grain, | |
| 236 | gb->buffer, | ||
| 237 | bytestream2_get_bytes_left(gb)); | ||
| 238 | } | ||
| 239 | } | ||
| 240 | #endif | ||
| 241 | else { | ||
| 242 | ✗ | unsupported_provider_code: | |
| 243 | ✗ | av_log(logctx, AV_LOG_VERBOSE, | |
| 244 | "Unsupported User Data Registered ITU-T T35 SEI message (country_code = %d, provider_code = %d)\n", | ||
| 245 | country_code, provider_code); | ||
| 246 | } | ||
| 247 | |||
| 248 | ✗ | return 0; | |
| 249 | } | ||
| 250 | |||
| 251 | 624 | static int decode_unregistered_user_data(H2645SEIUnregistered *h, | |
| 252 | GetByteContext *gb, | ||
| 253 | enum AVCodecID codec_id) | ||
| 254 | { | ||
| 255 | uint8_t *user_data; | ||
| 256 | 624 | int size = bytestream2_get_bytes_left(gb); | |
| 257 | AVBufferRef *buf_ref, **tmp; | ||
| 258 | |||
| 259 | 
        2/4✓ Branch 0 taken 624 times. 
          ✗ Branch 1 not taken. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 624 times. 
         | 
      624 | if (size < 16 || size >= INT_MAX - 1) | 
| 260 | ✗ | return AVERROR_INVALIDDATA; | |
| 261 | |||
| 262 | 624 | tmp = av_realloc_array(h->buf_ref, h->nb_buf_ref + 1, sizeof(*h->buf_ref)); | |
| 263 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 624 times. 
         | 
      624 | if (!tmp) | 
| 264 | ✗ | return AVERROR(ENOMEM); | |
| 265 | 624 | h->buf_ref = tmp; | |
| 266 | |||
| 267 | 624 | buf_ref = av_buffer_alloc(size + 1); | |
| 268 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 624 times. 
         | 
      624 | if (!buf_ref) | 
| 269 | ✗ | return AVERROR(ENOMEM); | |
| 270 | 624 | user_data = buf_ref->data; | |
| 271 | |||
| 272 | 624 | bytestream2_get_bufferu(gb, user_data, size); | |
| 273 | 624 | user_data[size] = 0; | |
| 274 | 624 | buf_ref->size = size; | |
| 275 | 624 | h->buf_ref[h->nb_buf_ref++] = buf_ref; | |
| 276 | |||
| 277 | 
        2/2✓ Branch 0 taken 589 times. 
          ✓ Branch 1 taken 35 times. 
         | 
      624 | if (IS_H264(codec_id)) { | 
| 278 | int e, build; | ||
| 279 | 589 | e = sscanf(user_data + 16, "x264 - core %d", &build); | |
| 280 | 
        4/4✓ Branch 0 taken 162 times. 
          ✓ Branch 1 taken 427 times. 
          ✓ Branch 2 taken 159 times. 
          ✓ Branch 3 taken 3 times. 
         | 
      589 | if (e == 1 && build > 0) | 
| 281 | 159 | h->x264_build = build; | |
| 282 | 
        3/6✓ Branch 0 taken 162 times. 
          ✓ Branch 1 taken 427 times. 
          ✗ Branch 2 not taken. 
          ✓ Branch 3 taken 162 times. 
          ✗ Branch 4 not taken. 
          ✗ Branch 5 not taken. 
         | 
      589 | if (e == 1 && build == 1 && !strncmp(user_data+16, "x264 - core 0000", 16)) | 
| 283 | ✗ | h->x264_build = 67; | |
| 284 | } | ||
| 285 | |||
| 286 | 624 | return 0; | |
| 287 | } | ||
| 288 | |||
| 289 | 2 | static int decode_display_orientation(H2645SEIDisplayOrientation *h, | |
| 290 | GetBitContext *gb) | ||
| 291 | { | ||
| 292 | 2 | h->present = !get_bits1(gb); // display_orientation_cancel_flag | |
| 293 | |||
| 294 | 
        1/2✓ Branch 0 taken 2 times. 
          ✗ Branch 1 not taken. 
         | 
      2 | if (h->present) { | 
| 295 | 2 | h->hflip = get_bits1(gb); // hor_flip | |
| 296 | 2 | h->vflip = get_bits1(gb); // ver_flip | |
| 297 | |||
| 298 | 2 | h->anticlockwise_rotation = get_bits(gb, 16); | |
| 299 | // This is followed by display_orientation_repetition_period | ||
| 300 | // and display_orientation_extension_flag for H.264 | ||
| 301 | // and by display_orientation_persistence_flag for HEVC. | ||
| 302 | } | ||
| 303 | |||
| 304 | 2 | return 0; | |
| 305 | } | ||
| 306 | |||
| 307 | ✗ | static int decode_frame_packing_arrangement(H2645SEIFramePacking *h, | |
| 308 | GetBitContext *gb, | ||
| 309 | enum AVCodecID codec_id) | ||
| 310 | { | ||
| 311 | ✗ | h->arrangement_id = get_ue_golomb_long(gb); | |
| 312 | ✗ | h->arrangement_cancel_flag = get_bits1(gb); | |
| 313 | ✗ | h->present = !h->arrangement_cancel_flag; | |
| 314 | |||
| 315 | ✗ | if (h->present) { | |
| 316 | ✗ | h->arrangement_type = get_bits(gb, 7); | |
| 317 | ✗ | h->quincunx_sampling_flag = get_bits1(gb); | |
| 318 | ✗ | h->content_interpretation_type = get_bits(gb, 6); | |
| 319 | |||
| 320 | // spatial_flipping_flag, frame0_flipped_flag, field_views_flag | ||
| 321 | ✗ | skip_bits(gb, 3); | |
| 322 | ✗ | h->current_frame_is_frame0_flag = get_bits1(gb); | |
| 323 | // frame0_self_contained_flag, frame1_self_contained_flag | ||
| 324 | ✗ | skip_bits(gb, 2); | |
| 325 | |||
| 326 | ✗ | if (!h->quincunx_sampling_flag && h->arrangement_type != 5) | |
| 327 | ✗ | skip_bits(gb, 16); // frame[01]_grid_position_[xy] | |
| 328 | ✗ | skip_bits(gb, 8); // frame_packing_arrangement_reserved_byte | |
| 329 | ✗ | if (IS_H264(codec_id)) | |
| 330 | ✗ | h->arrangement_repetition_period = get_ue_golomb_long(gb); | |
| 331 | else | ||
| 332 | ✗ | skip_bits1(gb); // frame_packing_arrangement_persistence_flag | |
| 333 | } | ||
| 334 | // H.264: frame_packing_arrangement_extension_flag, | ||
| 335 | // HEVC: upsampled_aspect_ratio_flag | ||
| 336 | ✗ | skip_bits1(gb); | |
| 337 | |||
| 338 | ✗ | return 0; | |
| 339 | } | ||
| 340 | |||
| 341 | ✗ | static int decode_alternative_transfer(H2645SEIAlternativeTransfer *s, | |
| 342 | GetByteContext *gb) | ||
| 343 | { | ||
| 344 | ✗ | if (bytestream2_get_bytes_left(gb) < 1) | |
| 345 | ✗ | return AVERROR_INVALIDDATA; | |
| 346 | |||
| 347 | ✗ | s->present = 1; | |
| 348 | ✗ | s->preferred_transfer_characteristics = bytestream2_get_byteu(gb); | |
| 349 | |||
| 350 | ✗ | return 0; | |
| 351 | } | ||
| 352 | |||
| 353 | 9 | static int decode_ambient_viewing_environment(H2645SEIAmbientViewingEnvironment *s, | |
| 354 | GetByteContext *gb) | ||
| 355 | { | ||
| 356 | static const uint16_t max_ambient_light_value = 50000; | ||
| 357 | |||
| 358 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 9 times. 
         | 
      9 | if (bytestream2_get_bytes_left(gb) < 8) | 
| 359 | ✗ | return AVERROR_INVALIDDATA; | |
| 360 | |||
| 361 | 9 | s->ambient_illuminance = bytestream2_get_be32u(gb); | |
| 362 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 9 times. 
         | 
      9 | if (!s->ambient_illuminance) | 
| 363 | ✗ | return AVERROR_INVALIDDATA; | |
| 364 | |||
| 365 | 9 | s->ambient_light_x = bytestream2_get_be16u(gb); | |
| 366 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 9 times. 
         | 
      9 | if (s->ambient_light_x > max_ambient_light_value) | 
| 367 | ✗ | return AVERROR_INVALIDDATA; | |
| 368 | |||
| 369 | 9 | s->ambient_light_y = bytestream2_get_be16u(gb); | |
| 370 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 9 times. 
         | 
      9 | if (s->ambient_light_y > max_ambient_light_value) | 
| 371 | ✗ | return AVERROR_INVALIDDATA; | |
| 372 | |||
| 373 | 9 | s->present = 1; | |
| 374 | |||
| 375 | 9 | return 0; | |
| 376 | } | ||
| 377 | |||
| 378 | ✗ | static int decode_film_grain_characteristics(H2645SEIFilmGrainCharacteristics *h, | |
| 379 | enum AVCodecID codec_id, GetBitContext *gb) | ||
| 380 | { | ||
| 381 | ✗ | h->present = !get_bits1(gb); // film_grain_characteristics_cancel_flag | |
| 382 | |||
| 383 | ✗ | if (h->present) { | |
| 384 | ✗ | memset(h, 0, sizeof(*h)); | |
| 385 | ✗ | h->model_id = get_bits(gb, 2); | |
| 386 | ✗ | h->separate_colour_description_present_flag = get_bits1(gb); | |
| 387 | ✗ | if (h->separate_colour_description_present_flag) { | |
| 388 | ✗ | h->bit_depth_luma = get_bits(gb, 3) + 8; | |
| 389 | ✗ | h->bit_depth_chroma = get_bits(gb, 3) + 8; | |
| 390 | ✗ | h->full_range = get_bits1(gb); | |
| 391 | ✗ | h->color_primaries = get_bits(gb, 8); | |
| 392 | ✗ | h->transfer_characteristics = get_bits(gb, 8); | |
| 393 | ✗ | h->matrix_coeffs = get_bits(gb, 8); | |
| 394 | } | ||
| 395 | ✗ | h->blending_mode_id = get_bits(gb, 2); | |
| 396 | ✗ | h->log2_scale_factor = get_bits(gb, 4); | |
| 397 | ✗ | for (int c = 0; c < 3; c++) | |
| 398 | ✗ | h->comp_model_present_flag[c] = get_bits1(gb); | |
| 399 | ✗ | for (int c = 0; c < 3; c++) { | |
| 400 | ✗ | if (h->comp_model_present_flag[c]) { | |
| 401 | ✗ | h->num_intensity_intervals[c] = get_bits(gb, 8) + 1; | |
| 402 | ✗ | h->num_model_values[c] = get_bits(gb, 3) + 1; | |
| 403 | ✗ | if (h->num_model_values[c] > 6) | |
| 404 | ✗ | return AVERROR_INVALIDDATA; | |
| 405 | ✗ | for (int i = 0; i < h->num_intensity_intervals[c]; i++) { | |
| 406 | ✗ | h->intensity_interval_lower_bound[c][i] = get_bits(gb, 8); | |
| 407 | ✗ | h->intensity_interval_upper_bound[c][i] = get_bits(gb, 8); | |
| 408 | ✗ | for (int j = 0; j < h->num_model_values[c]; j++) | |
| 409 | ✗ | h->comp_model_value[c][i][j] = get_se_golomb_long(gb); | |
| 410 | } | ||
| 411 | } | ||
| 412 | } | ||
| 413 | ✗ | if (!IS_H264(codec_id)) | |
| 414 | ✗ | h->persistence_flag = get_bits1(gb); | |
| 415 | else | ||
| 416 | ✗ | h->repetition_period = get_ue_golomb_long(gb); | |
| 417 | |||
| 418 | ✗ | h->present = 1; | |
| 419 | } | ||
| 420 | |||
| 421 | ✗ | return 0; | |
| 422 | } | ||
| 423 | |||
| 424 | 23 | static int decode_nal_sei_mastering_display_info(H2645SEIMasteringDisplay *s, | |
| 425 | GetByteContext *gb) | ||
| 426 | { | ||
| 427 | int i; | ||
| 428 | |||
| 429 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 23 times. 
         | 
      23 | if (bytestream2_get_bytes_left(gb) < 24) | 
| 430 | ✗ | return AVERROR_INVALIDDATA; | |
| 431 | |||
| 432 | // Mastering primaries | ||
| 433 | 
        2/2✓ Branch 0 taken 69 times. 
          ✓ Branch 1 taken 23 times. 
         | 
      92 | for (i = 0; i < 3; i++) { | 
| 434 | 69 | s->display_primaries[i][0] = bytestream2_get_be16u(gb); | |
| 435 | 69 | s->display_primaries[i][1] = bytestream2_get_be16u(gb); | |
| 436 | } | ||
| 437 | // White point (x, y) | ||
| 438 | 23 | s->white_point[0] = bytestream2_get_be16u(gb); | |
| 439 | 23 | s->white_point[1] = bytestream2_get_be16u(gb); | |
| 440 | |||
| 441 | // Max and min luminance of mastering display | ||
| 442 | 23 | s->max_luminance = bytestream2_get_be32u(gb); | |
| 443 | 23 | s->min_luminance = bytestream2_get_be32u(gb); | |
| 444 | |||
| 445 | // As this SEI message comes before the first frame that references it, | ||
| 446 | // initialize the flag to 2 and decrement on IRAP access unit so it | ||
| 447 | // persists for the coded video sequence (e.g., between two IRAPs) | ||
| 448 | 23 | s->present = 2; | |
| 449 | |||
| 450 | 23 | return 0; | |
| 451 | } | ||
| 452 | |||
| 453 | 14 | static int decode_nal_sei_content_light_info(H2645SEIContentLight *s, | |
| 454 | GetByteContext *gb) | ||
| 455 | { | ||
| 456 | 
        1/2✗ Branch 1 not taken. 
          ✓ Branch 2 taken 14 times. 
         | 
      14 | if (bytestream2_get_bytes_left(gb) < 4) | 
| 457 | ✗ | return AVERROR_INVALIDDATA; | |
| 458 | |||
| 459 | // Max and average light levels | ||
| 460 | 14 | s->max_content_light_level = bytestream2_get_be16u(gb); | |
| 461 | 14 | s->max_pic_average_light_level = bytestream2_get_be16u(gb); | |
| 462 | // As this SEI message comes before the first frame that references it, | ||
| 463 | // initialize the flag to 2 and decrement on IRAP access unit so it | ||
| 464 | // persists for the coded video sequence (e.g., between two IRAPs) | ||
| 465 | 14 | s->present = 2; | |
| 466 | |||
| 467 | 14 | return 0; | |
| 468 | } | ||
| 469 | |||
| 470 | 2245 | int ff_h2645_sei_message_decode(H2645SEI *h, enum SEIType type, | |
| 471 | enum AVCodecID codec_id, GetBitContext *gb, | ||
| 472 | GetByteContext *gbyte, void *logctx) | ||
| 473 | { | ||
| 474 | 
        7/10✓ Branch 0 taken 1189 times. 
          ✓ Branch 1 taken 624 times. 
          ✓ Branch 2 taken 2 times. 
          ✗ Branch 3 not taken. 
          ✗ Branch 4 not taken. 
          ✗ Branch 5 not taken. 
          ✓ Branch 6 taken 9 times. 
          ✓ Branch 7 taken 23 times. 
          ✓ Branch 8 taken 14 times. 
          ✓ Branch 9 taken 384 times. 
         | 
      2245 | switch (type) { | 
| 475 | 1189 | case SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35: | |
| 476 | 1189 | return decode_registered_user_data(h, gbyte, codec_id, logctx); | |
| 477 | 624 | case SEI_TYPE_USER_DATA_UNREGISTERED: | |
| 478 | 624 | return decode_unregistered_user_data(&h->unregistered, gbyte, codec_id); | |
| 479 | 2 | case SEI_TYPE_DISPLAY_ORIENTATION: | |
| 480 | 2 | return decode_display_orientation(&h->display_orientation, gb); | |
| 481 | ✗ | case SEI_TYPE_FILM_GRAIN_CHARACTERISTICS: | |
| 482 | ✗ | av_refstruct_unref(&h->film_grain_characteristics); | |
| 483 | ✗ | h->film_grain_characteristics = av_refstruct_allocz(sizeof(*h->film_grain_characteristics)); | |
| 484 | ✗ | if (!h->film_grain_characteristics) | |
| 485 | ✗ | return AVERROR(ENOMEM); | |
| 486 | ✗ | return decode_film_grain_characteristics(h->film_grain_characteristics, codec_id, gb); | |
| 487 | ✗ | case SEI_TYPE_FRAME_PACKING_ARRANGEMENT: | |
| 488 | ✗ | return decode_frame_packing_arrangement(&h->frame_packing, gb, codec_id); | |
| 489 | ✗ | case SEI_TYPE_ALTERNATIVE_TRANSFER_CHARACTERISTICS: | |
| 490 | ✗ | return decode_alternative_transfer(&h->alternative_transfer, gbyte); | |
| 491 | 9 | case SEI_TYPE_AMBIENT_VIEWING_ENVIRONMENT: | |
| 492 | 9 | return decode_ambient_viewing_environment(&h->ambient_viewing_environment, | |
| 493 | gbyte); | ||
| 494 | 23 | case SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME: | |
| 495 | 23 | return decode_nal_sei_mastering_display_info(&h->mastering_display, | |
| 496 | gbyte); | ||
| 497 | 14 | case SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO: | |
| 498 | 14 | return decode_nal_sei_content_light_info(&h->content_light, gbyte); | |
| 499 | 384 | default: | |
| 500 | 384 | return FF_H2645_SEI_MESSAGE_UNHANDLED; | |
| 501 | } | ||
| 502 | } | ||
| 503 | |||
| 504 | 1062 | int ff_h2645_sei_ctx_replace(H2645SEI *dst, const H2645SEI *src) | |
| 505 | { | ||
| 506 | 1062 | int ret = av_buffer_replace(&dst->a53_caption.buf_ref, | |
| 507 | 1062 | src->a53_caption.buf_ref); | |
| 508 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 1062 times. 
         | 
      1062 | if (ret < 0) | 
| 509 | ✗ | return ret; | |
| 510 | |||
| 511 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 1062 times. 
         | 
      1062 | for (unsigned i = 0; i < dst->unregistered.nb_buf_ref; i++) | 
| 512 | ✗ | av_buffer_unref(&dst->unregistered.buf_ref[i]); | |
| 513 | 1062 | dst->unregistered.nb_buf_ref = 0; | |
| 514 | |||
| 515 | 1062 | ret = av_buffer_replace(&dst->lcevc.info, src->lcevc.info); | |
| 516 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 1062 times. 
         | 
      1062 | if (ret < 0) | 
| 517 | ✗ | return ret; | |
| 518 | |||
| 519 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 1062 times. 
         | 
      1062 | if (src->unregistered.nb_buf_ref) { | 
| 520 | ✗ | ret = av_reallocp_array(&dst->unregistered.buf_ref, | |
| 521 | ✗ | src->unregistered.nb_buf_ref, | |
| 522 | sizeof(*dst->unregistered.buf_ref)); | ||
| 523 | ✗ | if (ret < 0) | |
| 524 | ✗ | return ret; | |
| 525 | |||
| 526 | ✗ | for (unsigned i = 0; i < src->unregistered.nb_buf_ref; i++) { | |
| 527 | ✗ | dst->unregistered.buf_ref[i] = av_buffer_ref(src->unregistered.buf_ref[i]); | |
| 528 | ✗ | if (!dst->unregistered.buf_ref[i]) | |
| 529 | ✗ | return AVERROR(ENOMEM); | |
| 530 | ✗ | dst->unregistered.nb_buf_ref++; | |
| 531 | } | ||
| 532 | } | ||
| 533 | |||
| 534 | 
        2/2✓ Branch 0 taken 8496 times. 
          ✓ Branch 1 taken 1062 times. 
         | 
      9558 | for (unsigned i = 0; i < FF_ARRAY_ELEMS(dst->aom_film_grain.sets); i++) { | 
| 535 | 8496 | ret = av_buffer_replace(&dst->aom_film_grain.sets[i], | |
| 536 | 8496 | src->aom_film_grain.sets[i]); | |
| 537 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 8496 times. 
         | 
      8496 | if (ret < 0) | 
| 538 | ✗ | return ret; | |
| 539 | } | ||
| 540 | 1062 | dst->aom_film_grain.enable = src->aom_film_grain.enable; | |
| 541 | |||
| 542 | 1062 | dst->ambient_viewing_environment = src->ambient_viewing_environment; | |
| 543 | 1062 | dst->mastering_display = src->mastering_display; | |
| 544 | 1062 | dst->content_light = src->content_light; | |
| 545 | |||
| 546 | 1062 | av_refstruct_replace(&dst->film_grain_characteristics, | |
| 547 | 1062 | src->film_grain_characteristics); | |
| 548 | |||
| 549 | 1062 | return 0; | |
| 550 | } | ||
| 551 | |||
| 552 | ✗ | static int is_frame_packing_type_valid(SEIFpaType type, enum AVCodecID codec_id) | |
| 553 | { | ||
| 554 | ✗ | if (IS_H264(codec_id)) | |
| 555 | ✗ | return type <= SEI_FPA_H264_TYPE_2D && | |
| 556 | type >= SEI_FPA_H264_TYPE_CHECKERBOARD; | ||
| 557 | else | ||
| 558 | ✗ | return type <= SEI_FPA_TYPE_INTERLEAVE_TEMPORAL && | |
| 559 | type >= SEI_FPA_TYPE_SIDE_BY_SIDE; | ||
| 560 | } | ||
| 561 | |||
| 562 | 36654 | static int h2645_sei_to_side_data(AVCodecContext *avctx, H2645SEI *sei, | |
| 563 | AVFrameSideData ***sd, int *nb_sd) | ||
| 564 | { | ||
| 565 | int ret; | ||
| 566 | |||
| 567 | 
        2/2✓ Branch 0 taken 315 times. 
          ✓ Branch 1 taken 36654 times. 
         | 
      36969 | for (unsigned i = 0; i < sei->unregistered.nb_buf_ref; i++) { | 
| 568 | 315 | H2645SEIUnregistered *unreg = &sei->unregistered; | |
| 569 | |||
| 570 | 
        1/2✓ Branch 0 taken 315 times. 
          ✗ Branch 1 not taken. 
         | 
      315 | if (unreg->buf_ref[i]) { | 
| 571 | AVFrameSideData *entry = | ||
| 572 | 315 | av_frame_side_data_add(sd, nb_sd, AV_FRAME_DATA_SEI_UNREGISTERED, | |
| 573 | 315 | &unreg->buf_ref[i], 0); | |
| 574 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 315 times. 
         | 
      315 | if (!entry) | 
| 575 | ✗ | av_buffer_unref(&unreg->buf_ref[i]); | |
| 576 | } | ||
| 577 | } | ||
| 578 | 36654 | sei->unregistered.nb_buf_ref = 0; | |
| 579 | |||
| 580 | 
        2/2✓ Branch 0 taken 10 times. 
          ✓ Branch 1 taken 36644 times. 
         | 
      36654 | if (sei->ambient_viewing_environment.present) { | 
| 581 | 10 | H2645SEIAmbientViewingEnvironment *env = &sei->ambient_viewing_environment; | |
| 582 | AVBufferRef *buf; | ||
| 583 | size_t size; | ||
| 584 | |||
| 585 | AVAmbientViewingEnvironment *dst_env = | ||
| 586 | 10 | av_ambient_viewing_environment_alloc(&size); | |
| 587 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 10 times. 
         | 
      10 | if (!dst_env) | 
| 588 | ✗ | return AVERROR(ENOMEM); | |
| 589 | |||
| 590 | 10 | buf = av_buffer_create((uint8_t *)dst_env, size, NULL, NULL, 0); | |
| 591 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 10 times. 
         | 
      10 | if (!buf) { | 
| 592 | ✗ | av_free(dst_env); | |
| 593 | ✗ | return AVERROR(ENOMEM); | |
| 594 | } | ||
| 595 | |||
| 596 | 10 | ret = ff_frame_new_side_data_from_buf_ext(avctx, sd, nb_sd, | |
| 597 | AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT, &buf); | ||
| 598 | |||
| 599 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 10 times. 
         | 
      10 | if (ret < 0) | 
| 600 | ✗ | return ret; | |
| 601 | |||
| 602 | 10 | dst_env->ambient_illuminance = av_make_q(env->ambient_illuminance, 10000); | |
| 603 | 10 | dst_env->ambient_light_x = av_make_q(env->ambient_light_x, 50000); | |
| 604 | 10 | dst_env->ambient_light_y = av_make_q(env->ambient_light_y, 50000); | |
| 605 | } | ||
| 606 | |||
| 607 | 
        2/2✓ Branch 0 taken 35 times. 
          ✓ Branch 1 taken 36619 times. 
         | 
      36654 | if (sei->mastering_display.present) { | 
| 608 | // HEVC uses a g,b,r ordering, which we convert to a more natural r,g,b | ||
| 609 | 35 | const int mapping[3] = {2, 0, 1}; | |
| 610 | 35 | const int chroma_den = 50000; | |
| 611 | 35 | const int luma_den = 10000; | |
| 612 | int i; | ||
| 613 | AVMasteringDisplayMetadata *metadata; | ||
| 614 | |||
| 615 | 35 | ret = ff_decode_mastering_display_new_ext(avctx, sd, nb_sd, &metadata); | |
| 616 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 35 times. 
         | 
      35 | if (ret < 0) | 
| 617 | ✗ | return ret; | |
| 618 | |||
| 619 | 
        2/2✓ Branch 0 taken 25 times. 
          ✓ Branch 1 taken 10 times. 
         | 
      35 | if (metadata) { | 
| 620 | 25 | metadata->has_luminance = 1; | |
| 621 | 25 | metadata->has_primaries = 1; | |
| 622 | |||
| 623 | 
        2/2✓ Branch 0 taken 75 times. 
          ✓ Branch 1 taken 25 times. 
         | 
      100 | for (i = 0; i < 3; i++) { | 
| 624 | 75 | const int j = mapping[i]; | |
| 625 | 75 | metadata->display_primaries[i][0].num = sei->mastering_display.display_primaries[j][0]; | |
| 626 | 75 | metadata->display_primaries[i][0].den = chroma_den; | |
| 627 | 
        1/2✓ Branch 0 taken 75 times. 
          ✗ Branch 1 not taken. 
         | 
      150 | metadata->has_primaries &= sei->mastering_display.display_primaries[j][0] >= 5 && | 
| 628 | 
        1/2✓ Branch 0 taken 75 times. 
          ✗ Branch 1 not taken. 
         | 
      75 | sei->mastering_display.display_primaries[j][0] <= 37000; | 
| 629 | |||
| 630 | 75 | metadata->display_primaries[i][1].num = sei->mastering_display.display_primaries[j][1]; | |
| 631 | 75 | metadata->display_primaries[i][1].den = chroma_den; | |
| 632 | 
        1/2✓ Branch 0 taken 75 times. 
          ✗ Branch 1 not taken. 
         | 
      150 | metadata->has_primaries &= sei->mastering_display.display_primaries[j][1] >= 5 && | 
| 633 | 
        1/2✓ Branch 0 taken 75 times. 
          ✗ Branch 1 not taken. 
         | 
      75 | sei->mastering_display.display_primaries[j][1] <= 42000; | 
| 634 | } | ||
| 635 | 25 | metadata->white_point[0].num = sei->mastering_display.white_point[0]; | |
| 636 | 25 | metadata->white_point[0].den = chroma_den; | |
| 637 | 
        1/2✓ Branch 0 taken 25 times. 
          ✗ Branch 1 not taken. 
         | 
      50 | metadata->has_primaries &= sei->mastering_display.white_point[0] >= 5 && | 
| 638 | 
        1/2✓ Branch 0 taken 25 times. 
          ✗ Branch 1 not taken. 
         | 
      25 | sei->mastering_display.white_point[0] <= 37000; | 
| 639 | |||
| 640 | 25 | metadata->white_point[1].num = sei->mastering_display.white_point[1]; | |
| 641 | 25 | metadata->white_point[1].den = chroma_den; | |
| 642 | 
        1/2✓ Branch 0 taken 25 times. 
          ✗ Branch 1 not taken. 
         | 
      50 | metadata->has_primaries &= sei->mastering_display.white_point[1] >= 5 && | 
| 643 | 
        1/2✓ Branch 0 taken 25 times. 
          ✗ Branch 1 not taken. 
         | 
      25 | sei->mastering_display.white_point[1] <= 42000; | 
| 644 | |||
| 645 | 25 | metadata->max_luminance.num = sei->mastering_display.max_luminance; | |
| 646 | 25 | metadata->max_luminance.den = luma_den; | |
| 647 | 
        1/2✓ Branch 0 taken 25 times. 
          ✗ Branch 1 not taken. 
         | 
      50 | metadata->has_luminance &= sei->mastering_display.max_luminance >= 50000 && | 
| 648 | 
        2/2✓ Branch 0 taken 24 times. 
          ✓ Branch 1 taken 1 times. 
         | 
      25 | sei->mastering_display.max_luminance <= 100000000; | 
| 649 | |||
| 650 | 25 | metadata->min_luminance.num = sei->mastering_display.min_luminance; | |
| 651 | 25 | metadata->min_luminance.den = luma_den; | |
| 652 | 
        1/2✓ Branch 0 taken 25 times. 
          ✗ Branch 1 not taken. 
         | 
      50 | metadata->has_luminance &= sei->mastering_display.min_luminance <= 50000 && | 
| 653 | 25 | sei->mastering_display.min_luminance < | |
| 654 | 
        1/2✓ Branch 0 taken 25 times. 
          ✗ Branch 1 not taken. 
         | 
      25 | sei->mastering_display.max_luminance; | 
| 655 | |||
| 656 | /* Real (blu-ray) releases in the wild come with minimum luminance | ||
| 657 | * values of 0.000 cd/m2, so permit this edge case */ | ||
| 658 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 25 times. 
         | 
      25 | if (avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) | 
| 659 | ✗ | metadata->has_luminance &= sei->mastering_display.min_luminance >= 1; | |
| 660 | |||
| 661 | 
        3/4✓ Branch 0 taken 1 times. 
          ✓ Branch 1 taken 24 times. 
          ✓ Branch 2 taken 1 times. 
          ✗ Branch 3 not taken. 
         | 
      25 | if (metadata->has_luminance || metadata->has_primaries) | 
| 662 | 25 | av_log(avctx, AV_LOG_DEBUG, "Mastering Display Metadata:\n"); | |
| 663 | 
        1/2✓ Branch 0 taken 25 times. 
          ✗ Branch 1 not taken. 
         | 
      25 | if (metadata->has_primaries) { | 
| 664 | 25 | av_log(avctx, AV_LOG_DEBUG, | |
| 665 | "r(%5.4f,%5.4f) g(%5.4f,%5.4f) b(%5.4f %5.4f) wp(%5.4f, %5.4f)\n", | ||
| 666 | 25 | av_q2d(metadata->display_primaries[0][0]), | |
| 667 | 25 | av_q2d(metadata->display_primaries[0][1]), | |
| 668 | 25 | av_q2d(metadata->display_primaries[1][0]), | |
| 669 | 25 | av_q2d(metadata->display_primaries[1][1]), | |
| 670 | 25 | av_q2d(metadata->display_primaries[2][0]), | |
| 671 | 25 | av_q2d(metadata->display_primaries[2][1]), | |
| 672 | 25 | av_q2d(metadata->white_point[0]), av_q2d(metadata->white_point[1])); | |
| 673 | } | ||
| 674 | 
        2/2✓ Branch 0 taken 24 times. 
          ✓ Branch 1 taken 1 times. 
         | 
      25 | if (metadata->has_luminance) { | 
| 675 | 24 | av_log(avctx, AV_LOG_DEBUG, | |
| 676 | "min_luminance=%f, max_luminance=%f\n", | ||
| 677 | 24 | av_q2d(metadata->min_luminance), av_q2d(metadata->max_luminance)); | |
| 678 | } | ||
| 679 | } | ||
| 680 | } | ||
| 681 | |||
| 682 | 
        2/2✓ Branch 0 taken 8 times. 
          ✓ Branch 1 taken 36646 times. 
         | 
      36654 | if (sei->content_light.present) { | 
| 683 | AVContentLightMetadata *metadata; | ||
| 684 | |||
| 685 | 8 | ret = ff_decode_content_light_new_ext(avctx, sd, nb_sd, &metadata); | |
| 686 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 8 times. 
         | 
      8 | if (ret < 0) | 
| 687 | ✗ | return ret; | |
| 688 | |||
| 689 | 
        1/2✓ Branch 0 taken 8 times. 
          ✗ Branch 1 not taken. 
         | 
      8 | if (metadata) { | 
| 690 | 8 | metadata->MaxCLL = sei->content_light.max_content_light_level; | |
| 691 | 8 | metadata->MaxFALL = sei->content_light.max_pic_average_light_level; | |
| 692 | |||
| 693 | 8 | av_log(avctx, AV_LOG_DEBUG, "Content Light Level Metadata:\n"); | |
| 694 | 8 | av_log(avctx, AV_LOG_DEBUG, "MaxCLL=%d, MaxFALL=%d\n", | |
| 695 | 8 | metadata->MaxCLL, metadata->MaxFALL); | |
| 696 | } | ||
| 697 | } | ||
| 698 | |||
| 699 | 36654 | return 0; | |
| 700 | } | ||
| 701 | |||
| 702 | 36358 | int ff_h2645_sei_to_frame(AVFrame *frame, H2645SEI *sei, | |
| 703 | enum AVCodecID codec_id, | ||
| 704 | AVCodecContext *avctx, const H2645VUI *vui, | ||
| 705 | unsigned bit_depth_luma, unsigned bit_depth_chroma, | ||
| 706 | int seed) | ||
| 707 | { | ||
| 708 | 36358 | H2645SEIFramePacking *fp = &sei->frame_packing; | |
| 709 | int ret; | ||
| 710 | |||
| 711 | 
        1/4✗ Branch 0 not taken. 
          ✓ Branch 1 taken 36358 times. 
          ✗ Branch 2 not taken. 
          ✗ Branch 3 not taken. 
         | 
      36358 | if (fp->present && | 
| 712 | ✗ | is_frame_packing_type_valid(fp->arrangement_type, codec_id) && | |
| 713 | ✗ | fp->content_interpretation_type > 0 && | |
| 714 | ✗ | fp->content_interpretation_type < 3) { | |
| 715 | ✗ | AVStereo3D *stereo = av_stereo3d_create_side_data(frame); | |
| 716 | |||
| 717 | ✗ | if (!stereo) | |
| 718 | ✗ | return AVERROR(ENOMEM); | |
| 719 | |||
| 720 | ✗ | switch (fp->arrangement_type) { | |
| 721 | #if CONFIG_H264_SEI | ||
| 722 | ✗ | case SEI_FPA_H264_TYPE_CHECKERBOARD: | |
| 723 | ✗ | stereo->type = AV_STEREO3D_CHECKERBOARD; | |
| 724 | ✗ | break; | |
| 725 | ✗ | case SEI_FPA_H264_TYPE_INTERLEAVE_COLUMN: | |
| 726 | ✗ | stereo->type = AV_STEREO3D_COLUMNS; | |
| 727 | ✗ | break; | |
| 728 | ✗ | case SEI_FPA_H264_TYPE_INTERLEAVE_ROW: | |
| 729 | ✗ | stereo->type = AV_STEREO3D_LINES; | |
| 730 | ✗ | break; | |
| 731 | #endif | ||
| 732 | ✗ | case SEI_FPA_TYPE_SIDE_BY_SIDE: | |
| 733 | ✗ | if (fp->quincunx_sampling_flag) | |
| 734 | ✗ | stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX; | |
| 735 | else | ||
| 736 | ✗ | stereo->type = AV_STEREO3D_SIDEBYSIDE; | |
| 737 | ✗ | break; | |
| 738 | ✗ | case SEI_FPA_TYPE_TOP_BOTTOM: | |
| 739 | ✗ | stereo->type = AV_STEREO3D_TOPBOTTOM; | |
| 740 | ✗ | break; | |
| 741 | ✗ | case SEI_FPA_TYPE_INTERLEAVE_TEMPORAL: | |
| 742 | ✗ | stereo->type = AV_STEREO3D_FRAMESEQUENCE; | |
| 743 | ✗ | break; | |
| 744 | #if CONFIG_H264_SEI | ||
| 745 | ✗ | case SEI_FPA_H264_TYPE_2D: | |
| 746 | ✗ | stereo->type = AV_STEREO3D_2D; | |
| 747 | ✗ | break; | |
| 748 | #endif | ||
| 749 | } | ||
| 750 | |||
| 751 | ✗ | if (fp->content_interpretation_type == 2) | |
| 752 | ✗ | stereo->flags = AV_STEREO3D_FLAG_INVERT; | |
| 753 | |||
| 754 | ✗ | if (fp->arrangement_type == SEI_FPA_TYPE_INTERLEAVE_TEMPORAL) { | |
| 755 | ✗ | if (fp->current_frame_is_frame0_flag) | |
| 756 | ✗ | stereo->view = AV_STEREO3D_VIEW_LEFT; | |
| 757 | else | ||
| 758 | ✗ | stereo->view = AV_STEREO3D_VIEW_RIGHT; | |
| 759 | } | ||
| 760 | } | ||
| 761 | |||
| 762 | 
        2/2✓ Branch 0 taken 1 times. 
          ✓ Branch 1 taken 36357 times. 
         | 
      36358 | if (sei->display_orientation.present && | 
| 763 | 
        1/2✓ Branch 0 taken 1 times. 
          ✗ Branch 1 not taken. 
         | 
      1 | (sei->display_orientation.anticlockwise_rotation || | 
| 764 | 
        1/2✓ Branch 0 taken 1 times. 
          ✗ Branch 1 not taken. 
         | 
      1 | sei->display_orientation.hflip || | 
| 765 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 1 times. 
         | 
      1 | sei->display_orientation.vflip)) { | 
| 766 | ✗ | H2645SEIDisplayOrientation *o = &sei->display_orientation; | |
| 767 | ✗ | double angle = o->anticlockwise_rotation * 360 / (double) (1 << 16); | |
| 768 | ✗ | AVFrameSideData *rotation = av_frame_new_side_data(frame, | |
| 769 | AV_FRAME_DATA_DISPLAYMATRIX, | ||
| 770 | sizeof(int32_t) * 9); | ||
| 771 | ✗ | if (!rotation) | |
| 772 | ✗ | return AVERROR(ENOMEM); | |
| 773 | |||
| 774 | /* av_display_rotation_set() expects the angle in the clockwise | ||
| 775 | * direction, hence the first minus. | ||
| 776 | * The below code applies the flips after the rotation, yet | ||
| 777 | * the H.2645 specs require flipping to be applied first. | ||
| 778 | * Because of R O(phi) = O(-phi) R (where R is flipping around | ||
| 779 | * an arbitatry axis and O(phi) is the proper rotation by phi) | ||
| 780 | * we can create display matrices as desired by negating | ||
| 781 | * the degree once for every flip applied. */ | ||
| 782 | ✗ | angle = -angle * (1 - 2 * !!o->hflip) * (1 - 2 * !!o->vflip); | |
| 783 | ✗ | av_display_rotation_set((int32_t *)rotation->data, angle); | |
| 784 | ✗ | av_display_matrix_flip((int32_t *)rotation->data, | |
| 785 | o->hflip, o->vflip); | ||
| 786 | } | ||
| 787 | |||
| 788 | 
        2/2✓ Branch 0 taken 1 times. 
          ✓ Branch 1 taken 36357 times. 
         | 
      36358 | if (sei->a53_caption.buf_ref) { | 
| 789 | 1 | H2645SEIA53Caption *a53 = &sei->a53_caption; | |
| 790 | 1 | AVFrameSideData *sd = av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_A53_CC, a53->buf_ref); | |
| 791 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 1 times. 
         | 
      1 | if (!sd) | 
| 792 | ✗ | av_buffer_unref(&a53->buf_ref); | |
| 793 | 1 | a53->buf_ref = NULL; | |
| 794 | #if FF_API_CODEC_PROPS | ||
| 795 | FF_DISABLE_DEPRECATION_WARNINGS | ||
| 796 | 1 | avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS; | |
| 797 | FF_ENABLE_DEPRECATION_WARNINGS | ||
| 798 | #endif | ||
| 799 | } | ||
| 800 | |||
| 801 | 36358 | ret = h2645_sei_to_side_data(avctx, sei, &frame->side_data, &frame->nb_side_data); | |
| 802 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 36358 times. 
         | 
      36358 | if (ret < 0) | 
| 803 | ✗ | return ret; | |
| 804 | |||
| 805 | 
        2/2✓ Branch 0 taken 324 times. 
          ✓ Branch 1 taken 36034 times. 
         | 
      36358 | if (sei->afd.present) { | 
| 806 | 324 | AVFrameSideData *sd = av_frame_new_side_data(frame, AV_FRAME_DATA_AFD, | |
| 807 | sizeof(uint8_t)); | ||
| 808 | |||
| 809 | 
        1/2✓ Branch 0 taken 324 times. 
          ✗ Branch 1 not taken. 
         | 
      324 | if (sd) { | 
| 810 | 324 | *sd->data = sei->afd.active_format_description; | |
| 811 | 324 | sei->afd.present = 0; | |
| 812 | } | ||
| 813 | } | ||
| 814 | |||
| 815 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 36358 times. 
         | 
      36358 | if (sei->lcevc.info) { | 
| 816 | ✗ | HEVCSEILCEVC *lcevc = &sei->lcevc; | |
| 817 | ✗ | ret = ff_frame_new_side_data_from_buf(avctx, frame, AV_FRAME_DATA_LCEVC, &lcevc->info); | |
| 818 | ✗ | if (ret < 0) | |
| 819 | ✗ | return ret; | |
| 820 | } | ||
| 821 | |||
| 822 | 
        1/4✗ Branch 0 not taken. 
          ✓ Branch 1 taken 36358 times. 
          ✗ Branch 2 not taken. 
          ✗ Branch 3 not taken. 
         | 
      36358 | if (sei->film_grain_characteristics && sei->film_grain_characteristics->present) { | 
| 823 | ✗ | H2645SEIFilmGrainCharacteristics *fgc = sei->film_grain_characteristics; | |
| 824 | ✗ | AVFilmGrainParams *fgp = av_film_grain_params_create_side_data(frame); | |
| 825 | AVFilmGrainH274Params *h274; | ||
| 826 | |||
| 827 | ✗ | if (!fgp) | |
| 828 | ✗ | return AVERROR(ENOMEM); | |
| 829 | |||
| 830 | ✗ | fgp->type = AV_FILM_GRAIN_PARAMS_H274; | |
| 831 | ✗ | h274 = &fgp->codec.h274; | |
| 832 | |||
| 833 | ✗ | fgp->seed = seed; | |
| 834 | ✗ | fgp->width = frame->width; | |
| 835 | ✗ | fgp->height = frame->height; | |
| 836 | |||
| 837 | /* H.274 mandates film grain be applied to 4:4:4 frames */ | ||
| 838 | ✗ | fgp->subsampling_x = fgp->subsampling_y = 0; | |
| 839 | |||
| 840 | ✗ | h274->model_id = fgc->model_id; | |
| 841 | ✗ | if (IS_VVC(codec_id) || fgc->separate_colour_description_present_flag) { | |
| 842 | ✗ | fgp->bit_depth_luma = fgc->bit_depth_luma; | |
| 843 | ✗ | fgp->bit_depth_chroma = fgc->bit_depth_chroma; | |
| 844 | ✗ | fgp->color_range = fgc->full_range + 1; | |
| 845 | ✗ | fgp->color_primaries = fgc->color_primaries; | |
| 846 | ✗ | fgp->color_trc = fgc->transfer_characteristics; | |
| 847 | ✗ | fgp->color_space = fgc->matrix_coeffs; | |
| 848 | } else { | ||
| 849 | ✗ | fgp->bit_depth_luma = bit_depth_luma; | |
| 850 | ✗ | fgp->bit_depth_chroma = bit_depth_chroma; | |
| 851 | ✗ | if (vui->video_signal_type_present_flag) | |
| 852 | ✗ | fgp->color_range = vui->video_full_range_flag + 1; | |
| 853 | ✗ | if (vui->colour_description_present_flag) { | |
| 854 | ✗ | fgp->color_primaries = vui->colour_primaries; | |
| 855 | ✗ | fgp->color_trc = vui->transfer_characteristics; | |
| 856 | ✗ | fgp->color_space = vui->matrix_coeffs; | |
| 857 | } | ||
| 858 | } | ||
| 859 | ✗ | h274->blending_mode_id = fgc->blending_mode_id; | |
| 860 | ✗ | h274->log2_scale_factor = fgc->log2_scale_factor; | |
| 861 | |||
| 862 | ✗ | memcpy(&h274->component_model_present, &fgc->comp_model_present_flag, | |
| 863 | sizeof(h274->component_model_present)); | ||
| 864 | ✗ | memcpy(&h274->num_intensity_intervals, &fgc->num_intensity_intervals, | |
| 865 | sizeof(h274->num_intensity_intervals)); | ||
| 866 | ✗ | memcpy(&h274->num_model_values, &fgc->num_model_values, | |
| 867 | sizeof(h274->num_model_values)); | ||
| 868 | ✗ | memcpy(&h274->intensity_interval_lower_bound, &fgc->intensity_interval_lower_bound, | |
| 869 | sizeof(h274->intensity_interval_lower_bound)); | ||
| 870 | ✗ | memcpy(&h274->intensity_interval_upper_bound, &fgc->intensity_interval_upper_bound, | |
| 871 | sizeof(h274->intensity_interval_upper_bound)); | ||
| 872 | ✗ | memcpy(&h274->comp_model_value, &fgc->comp_model_value, | |
| 873 | sizeof(h274->comp_model_value)); | ||
| 874 | |||
| 875 | ✗ | if (IS_H264(codec_id)) | |
| 876 | ✗ | fgc->present = !!fgc->repetition_period; | |
| 877 | else | ||
| 878 | ✗ | fgc->present = fgc->persistence_flag; | |
| 879 | |||
| 880 | #if FF_API_CODEC_PROPS | ||
| 881 | FF_DISABLE_DEPRECATION_WARNINGS | ||
| 882 | ✗ | avctx->properties |= FF_CODEC_PROPERTY_FILM_GRAIN; | |
| 883 | FF_ENABLE_DEPRECATION_WARNINGS | ||
| 884 | #endif | ||
| 885 | } | ||
| 886 | |||
| 887 | #if CONFIG_HEVC_SEI | ||
| 888 | 36358 | ret = ff_aom_attach_film_grain_sets(&sei->aom_film_grain, frame); | |
| 889 | 
        1/2✗ Branch 0 not taken. 
          ✓ Branch 1 taken 36358 times. 
         | 
      36358 | if (ret < 0) | 
| 890 | ✗ | return ret; | |
| 891 | #endif | ||
| 892 | |||
| 893 | 36358 | return 0; | |
| 894 | } | ||
| 895 | |||
| 896 | 296 | int ff_h2645_sei_to_context(AVCodecContext *avctx, H2645SEI *sei) | |
| 897 | { | ||
| 898 | 296 | return h2645_sei_to_side_data(avctx, sei, &avctx->decoded_side_data, | |
| 899 | &avctx->nb_decoded_side_data); | ||
| 900 | } | ||
| 901 | |||
| 902 | 75517 | void ff_h2645_sei_reset(H2645SEI *s) | |
| 903 | { | ||
| 904 | 75517 | av_buffer_unref(&s->a53_caption.buf_ref); | |
| 905 | |||
| 906 | 
        2/2✓ Branch 0 taken 309 times. 
          ✓ Branch 1 taken 75517 times. 
         | 
      75826 | for (unsigned i = 0; i < s->unregistered.nb_buf_ref; i++) | 
| 907 | 309 | av_buffer_unref(&s->unregistered.buf_ref[i]); | |
| 908 | 75517 | s->unregistered.nb_buf_ref = 0; | |
| 909 | 75517 | av_freep(&s->unregistered.buf_ref); | |
| 910 | 75517 | av_buffer_unref(&s->dynamic_hdr_plus.info); | |
| 911 | 75517 | av_buffer_unref(&s->dynamic_hdr_vivid.info); | |
| 912 | 75517 | av_buffer_unref(&s->lcevc.info); | |
| 913 | |||
| 914 | 75517 | s->ambient_viewing_environment.present = 0; | |
| 915 | 75517 | s->mastering_display.present = 0; | |
| 916 | 75517 | s->content_light.present = 0; | |
| 917 | |||
| 918 | 75517 | av_refstruct_unref(&s->film_grain_characteristics); | |
| 919 | 75517 | ff_aom_uninit_film_grain_params(&s->aom_film_grain); | |
| 920 | 75517 | } | |
| 921 |