| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 "bytestream.h" | ||
| 26 | #include "golomb.h" | ||
| 27 | #include "ps.h" | ||
| 28 | #include "sei.h" | ||
| 29 | |||
| 30 | 7049 | static int decode_nal_sei_decoded_picture_hash(HEVCSEIPictureHash *s, | |
| 31 | GetByteContext *gb) | ||
| 32 | { | ||
| 33 | int cIdx; | ||
| 34 | uint8_t hash_type; | ||
| 35 | //uint16_t picture_crc; | ||
| 36 | //uint32_t picture_checksum; | ||
| 37 | 7049 | hash_type = bytestream2_get_byte(gb); | |
| 38 | |||
| 39 |
2/2✓ Branch 0 taken 21147 times.
✓ Branch 1 taken 7049 times.
|
28196 | for (cIdx = 0; cIdx < 3/*((s->sps->chroma_format_idc == 0) ? 1 : 3)*/; cIdx++) { |
| 40 |
2/2✓ Branch 0 taken 20097 times.
✓ Branch 1 taken 1050 times.
|
21147 | if (hash_type == 0) { |
| 41 | 20097 | s->is_md5 = 1; | |
| 42 | 20097 | bytestream2_get_buffer(gb, s->md5[cIdx], sizeof(s->md5[cIdx])); | |
| 43 | } else if (hash_type == 1) { | ||
| 44 | // picture_crc = get_bits(gb, 16); | ||
| 45 | } else if (hash_type == 2) { | ||
| 46 | // picture_checksum = get_bits_long(gb, 32); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | 7049 | return 0; | |
| 50 | } | ||
| 51 | |||
| 52 | 1395 | static int decode_nal_sei_pic_timing(HEVCSEI *s, GetBitContext *gb, | |
| 53 | const HEVCParamSets *ps, void *logctx) | ||
| 54 | { | ||
| 55 | 1395 | HEVCSEIPictureTiming *h = &s->picture_timing; | |
| 56 | 1395 | const HEVCSPS *sps = ps->sps_list[s->active_seq_parameter_set_id]; | |
| 57 | |||
| 58 |
2/2✓ Branch 0 taken 66 times.
✓ Branch 1 taken 1329 times.
|
1395 | if (!sps) |
| 59 | 66 | return AVERROR_INVALIDDATA; | |
| 60 | |||
| 61 |
2/2✓ Branch 0 taken 727 times.
✓ Branch 1 taken 602 times.
|
1329 | if (sps->vui.frame_field_info_present_flag) { |
| 62 | 727 | int pic_struct = get_bits(gb, 4); | |
| 63 | 727 | h->picture_struct = AV_PICTURE_STRUCTURE_UNKNOWN; | |
| 64 |
4/6✓ Branch 0 taken 727 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 717 times.
✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 717 times.
|
727 | if (pic_struct == 2 || pic_struct == 10 || pic_struct == 12) { |
| 65 | 10 | av_log(logctx, AV_LOG_DEBUG, "BOTTOM Field\n"); | |
| 66 | 10 | h->picture_struct = AV_PICTURE_STRUCTURE_BOTTOM_FIELD; | |
| 67 |
4/6✓ Branch 0 taken 717 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 717 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 706 times.
|
717 | } else if (pic_struct == 1 || pic_struct == 9 || pic_struct == 11) { |
| 68 | 11 | av_log(logctx, AV_LOG_DEBUG, "TOP Field\n"); | |
| 69 | 11 | h->picture_struct = AV_PICTURE_STRUCTURE_TOP_FIELD; | |
| 70 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 706 times.
|
706 | } else if (pic_struct == 7) { |
| 71 | ✗ | av_log(logctx, AV_LOG_DEBUG, "Frame/Field Doubling\n"); | |
| 72 | ✗ | h->picture_struct = HEVC_SEI_PIC_STRUCT_FRAME_DOUBLING; | |
| 73 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 706 times.
|
706 | } else if (pic_struct == 8) { |
| 74 | ✗ | av_log(logctx, AV_LOG_DEBUG, "Frame/Field Tripling\n"); | |
| 75 | ✗ | h->picture_struct = HEVC_SEI_PIC_STRUCT_FRAME_TRIPLING; | |
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | 1329 | return 0; | |
| 80 | } | ||
| 81 | |||
| 82 | 53 | static int decode_nal_sei_recovery_point(HEVCSEI *s, GetBitContext *gb) | |
| 83 | { | ||
| 84 | 53 | HEVCSEIRecoveryPoint *rec = &s->recovery_point; | |
| 85 | 53 | int recovery_poc_cnt = get_se_golomb(gb); | |
| 86 | |||
| 87 |
2/4✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 53 times.
|
53 | if (recovery_poc_cnt > INT16_MAX || recovery_poc_cnt < INT16_MIN) |
| 88 | ✗ | return AVERROR_INVALIDDATA; | |
| 89 | 53 | rec->recovery_poc_cnt = recovery_poc_cnt; | |
| 90 | 53 | rec->exact_match_flag = get_bits1(gb); | |
| 91 | 53 | rec->broken_link_flag = get_bits1(gb); | |
| 92 | 53 | rec->has_recovery_poc = 1; | |
| 93 | |||
| 94 | 53 | return 0; | |
| 95 | } | ||
| 96 | |||
| 97 | 36 | static int decode_nal_sei_active_parameter_sets(HEVCSEI *s, GetBitContext *gb, void *logctx) | |
| 98 | { | ||
| 99 | int num_sps_ids_minus1; | ||
| 100 | unsigned active_seq_parameter_set_id; | ||
| 101 | |||
| 102 | 36 | get_bits(gb, 4); // active_video_parameter_set_id | |
| 103 | 36 | get_bits(gb, 1); // self_contained_cvs_flag | |
| 104 | 36 | get_bits(gb, 1); // num_sps_ids_minus1 | |
| 105 | 36 | num_sps_ids_minus1 = get_ue_golomb_long(gb); // num_sps_ids_minus1 | |
| 106 | |||
| 107 |
2/4✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36 times.
|
36 | if (num_sps_ids_minus1 < 0 || num_sps_ids_minus1 > 15) { |
| 108 | ✗ | av_log(logctx, AV_LOG_ERROR, "num_sps_ids_minus1 %d invalid\n", num_sps_ids_minus1); | |
| 109 | ✗ | return AVERROR_INVALIDDATA; | |
| 110 | } | ||
| 111 | |||
| 112 | 36 | active_seq_parameter_set_id = get_ue_golomb_long(gb); | |
| 113 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
|
36 | if (active_seq_parameter_set_id >= HEVC_MAX_SPS_COUNT) { |
| 114 | ✗ | av_log(logctx, AV_LOG_ERROR, "active_parameter_set_id %d invalid\n", active_seq_parameter_set_id); | |
| 115 | ✗ | return AVERROR_INVALIDDATA; | |
| 116 | } | ||
| 117 | 36 | s->active_seq_parameter_set_id = active_seq_parameter_set_id; | |
| 118 | |||
| 119 | 36 | return 0; | |
| 120 | } | ||
| 121 | |||
| 122 | 136 | static int decode_nal_sei_timecode(HEVCSEITimeCode *s, GetBitContext *gb) | |
| 123 | { | ||
| 124 | 136 | s->num_clock_ts = get_bits(gb, 2); | |
| 125 | |||
| 126 |
2/2✓ Branch 0 taken 136 times.
✓ Branch 1 taken 136 times.
|
272 | for (int i = 0; i < s->num_clock_ts; i++) { |
| 127 | 136 | s->clock_timestamp_flag[i] = get_bits(gb, 1); | |
| 128 | |||
| 129 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
|
136 | if (s->clock_timestamp_flag[i]) { |
| 130 | ✗ | s->units_field_based_flag[i] = get_bits(gb, 1); | |
| 131 | ✗ | s->counting_type[i] = get_bits(gb, 5); | |
| 132 | ✗ | s->full_timestamp_flag[i] = get_bits(gb, 1); | |
| 133 | ✗ | s->discontinuity_flag[i] = get_bits(gb, 1); | |
| 134 | ✗ | s->cnt_dropped_flag[i] = get_bits(gb, 1); | |
| 135 | |||
| 136 | ✗ | s->n_frames[i] = get_bits(gb, 9); | |
| 137 | |||
| 138 | ✗ | if (s->full_timestamp_flag[i]) { | |
| 139 | ✗ | s->seconds_value[i] = av_clip(get_bits(gb, 6), 0, 59); | |
| 140 | ✗ | s->minutes_value[i] = av_clip(get_bits(gb, 6), 0, 59); | |
| 141 | ✗ | s->hours_value[i] = av_clip(get_bits(gb, 5), 0, 23); | |
| 142 | } else { | ||
| 143 | ✗ | s->seconds_flag[i] = get_bits(gb, 1); | |
| 144 | ✗ | if (s->seconds_flag[i]) { | |
| 145 | ✗ | s->seconds_value[i] = av_clip(get_bits(gb, 6), 0, 59); | |
| 146 | ✗ | s->minutes_flag[i] = get_bits(gb, 1); | |
| 147 | ✗ | if (s->minutes_flag[i]) { | |
| 148 | ✗ | s->minutes_value[i] = av_clip(get_bits(gb, 6), 0, 59); | |
| 149 | ✗ | s->hours_flag[i] = get_bits(gb, 1); | |
| 150 | ✗ | if (s->hours_flag[i]) { | |
| 151 | ✗ | s->hours_value[i] = av_clip(get_bits(gb, 5), 0, 23); | |
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | ✗ | s->time_offset_length[i] = get_bits(gb, 5); | |
| 158 | ✗ | if (s->time_offset_length[i] > 0) { | |
| 159 | ✗ | s->time_offset_value[i] = get_bits_long(gb, s->time_offset_length[i]); | |
| 160 | } | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | 136 | s->present = 1; | |
| 165 | 136 | return 0; | |
| 166 | } | ||
| 167 | |||
| 168 | 4 | static int decode_nal_sei_3d_reference_displays_info(HEVCSEITDRDI *s, GetBitContext *gb) | |
| 169 | { | ||
| 170 | unsigned num_ref_displays; | ||
| 171 | |||
| 172 | 4 | s->prec_ref_display_width = get_ue_golomb(gb); | |
| 173 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (s->prec_ref_display_width > 31) |
| 174 | ✗ | return AVERROR_INVALIDDATA; | |
| 175 | 4 | s->ref_viewing_distance_flag = get_bits1(gb); | |
| 176 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (s->ref_viewing_distance_flag) { |
| 177 | ✗ | s->prec_ref_viewing_dist = get_ue_golomb(gb); | |
| 178 | ✗ | if (s->prec_ref_viewing_dist > 31) | |
| 179 | ✗ | return AVERROR_INVALIDDATA; | |
| 180 | } | ||
| 181 | 4 | num_ref_displays = get_ue_golomb(gb); | |
| 182 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (num_ref_displays > 31) |
| 183 | ✗ | return AVERROR_INVALIDDATA; | |
| 184 | 4 | s->num_ref_displays = num_ref_displays + 1; | |
| 185 | |||
| 186 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
8 | for (int i = 0; i < s->num_ref_displays; i++) { |
| 187 | int length; | ||
| 188 | 4 | s->left_view_id[i] = get_ue_golomb(gb); | |
| 189 | 4 | s->right_view_id[i] = get_ue_golomb(gb); | |
| 190 | 4 | s->exponent_ref_display_width[i] = get_bits(gb, 6); | |
| 191 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (s->exponent_ref_display_width[i] > 62) |
| 192 | ✗ | return AVERROR_INVALIDDATA; | |
| 193 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | else if (!s->exponent_ref_display_width[i]) |
| 194 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | length = FFMAX(0, (int)s->prec_ref_display_width - 30); |
| 195 | else | ||
| 196 | ✗ | length = FFMAX(0, (int)s->exponent_ref_display_width[i] + | |
| 197 | (int)s->prec_ref_display_width - 31); | ||
| 198 | 4 | s->mantissa_ref_display_width[i] = get_bits64(gb, length); | |
| 199 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (s->ref_viewing_distance_flag) { |
| 200 | ✗ | s->exponent_ref_viewing_distance[i] = get_bits(gb, 6); | |
| 201 | ✗ | if (s->exponent_ref_viewing_distance[i] > 62) | |
| 202 | ✗ | return AVERROR_INVALIDDATA; | |
| 203 | ✗ | else if (!s->exponent_ref_viewing_distance[i]) | |
| 204 | ✗ | length = FFMAX(0, (int)s->prec_ref_viewing_dist - 30); | |
| 205 | else | ||
| 206 | ✗ | length = FFMAX(0, (int)s->exponent_ref_viewing_distance[i] + | |
| 207 | (int)s->prec_ref_viewing_dist - 31); | ||
| 208 | ✗ | s->mantissa_ref_viewing_distance[i] = get_bits64(gb, length); | |
| 209 | } | ||
| 210 | 4 | s->additional_shift_present_flag[i] = get_bits1(gb); | |
| 211 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (s->additional_shift_present_flag[i]) { |
| 212 | ✗ | s->num_sample_shift[i] = get_bits(gb, 10); | |
| 213 | ✗ | if (s->num_sample_shift[i] > 1023) | |
| 214 | ✗ | return AVERROR_INVALIDDATA; | |
| 215 | ✗ | s->num_sample_shift[i] -= 512; | |
| 216 | } | ||
| 217 | } | ||
| 218 | 4 | s->three_dimensional_reference_displays_extension_flag = get_bits1(gb); | |
| 219 | |||
| 220 | 4 | s->present = 1; | |
| 221 | |||
| 222 | 4 | return 0; | |
| 223 | } | ||
| 224 | |||
| 225 | 1909 | static int decode_nal_sei_prefix(GetBitContext *gb, GetByteContext *gbyte, | |
| 226 | void *logctx, HEVCSEI *s, | ||
| 227 | const HEVCParamSets *ps, int type) | ||
| 228 | { | ||
| 229 |
6/7✗ Branch 0 not taken.
✓ Branch 1 taken 1395 times.
✓ Branch 2 taken 53 times.
✓ Branch 3 taken 36 times.
✓ Branch 4 taken 136 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 285 times.
|
1909 | switch (type) { |
| 230 | ✗ | case 256: // Mismatched value from HM 8.1 | |
| 231 | ✗ | return decode_nal_sei_decoded_picture_hash(&s->picture_hash, gbyte); | |
| 232 | 1395 | case SEI_TYPE_PIC_TIMING: | |
| 233 | 1395 | return decode_nal_sei_pic_timing(s, gb, ps, logctx); | |
| 234 | 53 | case SEI_TYPE_RECOVERY_POINT: | |
| 235 | 53 | return decode_nal_sei_recovery_point(s, gb); | |
| 236 | 36 | case SEI_TYPE_ACTIVE_PARAMETER_SETS: | |
| 237 | 36 | return decode_nal_sei_active_parameter_sets(s, gb, logctx); | |
| 238 | 136 | case SEI_TYPE_TIME_CODE: | |
| 239 | 136 | return decode_nal_sei_timecode(&s->timecode, gb); | |
| 240 | 4 | case SEI_TYPE_THREE_DIMENSIONAL_REFERENCE_DISPLAYS_INFO: | |
| 241 | 4 | return decode_nal_sei_3d_reference_displays_info(&s->tdrdi, gb); | |
| 242 | 285 | default: { | |
| 243 | 285 | int ret = ff_h2645_sei_message_decode(&s->common, type, AV_CODEC_ID_HEVC, | |
| 244 | gb, gbyte, logctx); | ||
| 245 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 229 times.
|
285 | if (ret == FF_H2645_SEI_MESSAGE_UNHANDLED) |
| 246 | 56 | av_log(logctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type); | |
| 247 | 285 | return ret; | |
| 248 | } | ||
| 249 | } | ||
| 250 | } | ||
| 251 | |||
| 252 | 7049 | static int decode_nal_sei_suffix(GetBitContext *gb, GetByteContext *gbyte, | |
| 253 | void *logctx, HEVCSEI *s, int type) | ||
| 254 | { | ||
| 255 |
1/2✓ Branch 0 taken 7049 times.
✗ Branch 1 not taken.
|
7049 | switch (type) { |
| 256 | 7049 | case SEI_TYPE_DECODED_PICTURE_HASH: | |
| 257 | 7049 | return decode_nal_sei_decoded_picture_hash(&s->picture_hash, gbyte); | |
| 258 | ✗ | default: | |
| 259 | ✗ | av_log(logctx, AV_LOG_DEBUG, "Skipped SUFFIX SEI %d\n", type); | |
| 260 | ✗ | return 0; | |
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | 8958 | static int decode_nal_sei_message(GetByteContext *gb, void *logctx, HEVCSEI *s, | |
| 265 | const HEVCParamSets *ps, int nal_unit_type) | ||
| 266 | { | ||
| 267 | GetByteContext message_gbyte; | ||
| 268 | GetBitContext message_gb; | ||
| 269 | 8958 | int payload_type = 0; | |
| 270 | 8958 | int payload_size = 0; | |
| 271 | 8958 | int byte = 0xFF; | |
| 272 | av_unused int ret; | ||
| 273 | 8958 | av_log(logctx, AV_LOG_DEBUG, "Decoding SEI\n"); | |
| 274 | |||
| 275 |
2/2✓ Branch 0 taken 8958 times.
✓ Branch 1 taken 8958 times.
|
17916 | while (byte == 0xFF) { |
| 276 |
2/4✓ Branch 1 taken 8958 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 8958 times.
|
8958 | if (bytestream2_get_bytes_left(gb) < 2 || payload_type > INT_MAX - 255) |
| 277 | ✗ | return AVERROR_INVALIDDATA; | |
| 278 | 8958 | byte = bytestream2_get_byteu(gb); | |
| 279 | 8958 | payload_type += byte; | |
| 280 | } | ||
| 281 | 8958 | byte = 0xFF; | |
| 282 |
2/2✓ Branch 0 taken 9115 times.
✓ Branch 1 taken 8958 times.
|
18073 | while (byte == 0xFF) { |
| 283 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9115 times.
|
9115 | if (bytestream2_get_bytes_left(gb) < 1 + payload_size) |
| 284 | ✗ | return AVERROR_INVALIDDATA; | |
| 285 | 9115 | byte = bytestream2_get_byteu(gb); | |
| 286 | 9115 | payload_size += byte; | |
| 287 | } | ||
| 288 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 8958 times.
|
8958 | if (bytestream2_get_bytes_left(gb) < payload_size) |
| 289 | ✗ | return AVERROR_INVALIDDATA; | |
| 290 | 8958 | bytestream2_init(&message_gbyte, gb->buffer, payload_size); | |
| 291 | 8958 | ret = init_get_bits8(&message_gb, gb->buffer, payload_size); | |
| 292 | av_assert1(ret >= 0); | ||
| 293 | 8958 | bytestream2_skipu(gb, payload_size); | |
| 294 |
2/2✓ Branch 0 taken 1909 times.
✓ Branch 1 taken 7049 times.
|
8958 | if (nal_unit_type == HEVC_NAL_SEI_PREFIX) { |
| 295 | 1909 | return decode_nal_sei_prefix(&message_gb, &message_gbyte, | |
| 296 | logctx, s, ps, payload_type); | ||
| 297 | } else { /* nal_unit_type == NAL_SEI_SUFFIX */ | ||
| 298 | 7049 | return decode_nal_sei_suffix(&message_gb, &message_gbyte, | |
| 299 | logctx, s, payload_type); | ||
| 300 | } | ||
| 301 | } | ||
| 302 | |||
| 303 | 8941 | int ff_hevc_decode_nal_sei(GetBitContext *gb, void *logctx, HEVCSEI *s, | |
| 304 | const HEVCParamSets *ps, enum HEVCNALUnitType type) | ||
| 305 | { | ||
| 306 | GetByteContext gbyte; | ||
| 307 | int ret; | ||
| 308 | |||
| 309 | av_assert1((get_bits_count(gb) % 8) == 0); | ||
| 310 | 8941 | bytestream2_init(&gbyte, gb->buffer + get_bits_count(gb) / 8, | |
| 311 | 8941 | get_bits_left(gb) / 8); | |
| 312 | |||
| 313 | do { | ||
| 314 | 8958 | ret = decode_nal_sei_message(&gbyte, logctx, s, ps, type); | |
| 315 |
2/2✓ Branch 0 taken 66 times.
✓ Branch 1 taken 8892 times.
|
8958 | if (ret < 0) |
| 316 | 66 | return ret; | |
| 317 |
2/2✓ Branch 1 taken 17 times.
✓ Branch 2 taken 8875 times.
|
8892 | } while (bytestream2_get_bytes_left(&gbyte) > 0); |
| 318 | 8875 | return 1; | |
| 319 | } | ||
| 320 |