| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * H.26L/H.264/AVC/JVT/14496-10/... SEI decoding | ||
| 3 | * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at> | ||
| 4 | * | ||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 7 | * FFmpeg is free software; you can redistribute it and/or | ||
| 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 | * | ||
| 12 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 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 | ||
| 18 | * License along with FFmpeg; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | /** | ||
| 23 | * @file | ||
| 24 | * H.264 / AVC / MPEG-4 part10 SEI decoding. | ||
| 25 | * @author Michael Niedermayer <michaelni@gmx.at> | ||
| 26 | */ | ||
| 27 | |||
| 28 | #include <limits.h> | ||
| 29 | #include <stdio.h> | ||
| 30 | #include <string.h> | ||
| 31 | #include "libavutil/error.h" | ||
| 32 | #include "libavutil/log.h" | ||
| 33 | #include "libavutil/macros.h" | ||
| 34 | #include "libavutil/mem.h" | ||
| 35 | #include "bytestream.h" | ||
| 36 | #include "get_bits.h" | ||
| 37 | #include "golomb.h" | ||
| 38 | #include "h264_ps.h" | ||
| 39 | #include "h264_sei.h" | ||
| 40 | #include "sei.h" | ||
| 41 | |||
| 42 | #define AVERROR_PS_NOT_FOUND FFERRTAG(0xF8,'?','P','S') | ||
| 43 | |||
| 44 | static const uint8_t sei_num_clock_ts_table[9] = { | ||
| 45 | 1, 1, 1, 2, 2, 3, 3, 2, 3 | ||
| 46 | }; | ||
| 47 | |||
| 48 | 61859 | void ff_h264_sei_uninit(H264SEIContext *h) | |
| 49 | { | ||
| 50 | 61859 | h->recovery_point.recovery_frame_cnt = -1; | |
| 51 | |||
| 52 | 61859 | h->picture_timing.dpb_output_delay = 0; | |
| 53 | 61859 | h->picture_timing.cpb_removal_delay = -1; | |
| 54 | |||
| 55 | 61859 | h->picture_timing.present = 0; | |
| 56 | 61859 | h->buffering_period.present = 0; | |
| 57 | 61859 | h->common.frame_packing.present = 0; | |
| 58 | 61859 | h->common.display_orientation.present = 0; | |
| 59 | 61859 | h->common.afd.present = 0; | |
| 60 | |||
| 61 | 61859 | ff_h2645_sei_reset(&h->common); | |
| 62 | 61859 | } | |
| 63 | |||
| 64 | 4417 | int ff_h264_sei_process_picture_timing(H264SEIPictureTiming *h, const SPS *sps, | |
| 65 | void *logctx) | ||
| 66 | { | ||
| 67 | GetBitContext gb; | ||
| 68 | av_unused int ret; | ||
| 69 | |||
| 70 | 4417 | ret = init_get_bits8(&gb, h->payload, h->payload_size_bytes); | |
| 71 | av_assert1(ret >= 0); | ||
| 72 | |||
| 73 |
2/2✓ Branch 0 taken 1946 times.
✓ Branch 1 taken 2471 times.
|
4417 | if (sps->nal_hrd_parameters_present_flag || |
| 74 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1946 times.
|
1946 | sps->vcl_hrd_parameters_present_flag) { |
| 75 | 2471 | h->cpb_removal_delay = get_bits_long(&gb, sps->cpb_removal_delay_length); | |
| 76 | 2471 | h->dpb_output_delay = get_bits_long(&gb, sps->dpb_output_delay_length); | |
| 77 | } | ||
| 78 |
2/2✓ Branch 0 taken 4397 times.
✓ Branch 1 taken 20 times.
|
4417 | if (sps->pic_struct_present_flag) { |
| 79 | unsigned int i, num_clock_ts; | ||
| 80 | |||
| 81 | 4397 | h->pic_struct = get_bits(&gb, 4); | |
| 82 | 4397 | h->ct_type = 0; | |
| 83 | |||
| 84 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4397 times.
|
4397 | if (h->pic_struct > H264_SEI_PIC_STRUCT_FRAME_TRIPLING) |
| 85 | ✗ | return AVERROR_INVALIDDATA; | |
| 86 | |||
| 87 | 4397 | num_clock_ts = sei_num_clock_ts_table[h->pic_struct]; | |
| 88 | 4397 | h->timecode_cnt = 0; | |
| 89 |
2/2✓ Branch 0 taken 7183 times.
✓ Branch 1 taken 4397 times.
|
11580 | for (i = 0; i < num_clock_ts; i++) { |
| 90 |
2/2✓ Branch 1 taken 1748 times.
✓ Branch 2 taken 5435 times.
|
7183 | if (get_bits(&gb, 1)) { /* clock_timestamp_flag */ |
| 91 | 1748 | H264SEITimeCode *tc = &h->timecode[h->timecode_cnt++]; | |
| 92 | unsigned int full_timestamp_flag; | ||
| 93 | unsigned int counting_type, cnt_dropped_flag; | ||
| 94 | 1748 | h->ct_type |= 1 << get_bits(&gb, 2); | |
| 95 | 1748 | skip_bits(&gb, 1); /* nuit_field_based_flag */ | |
| 96 | 1748 | counting_type = get_bits(&gb, 5); /* counting_type */ | |
| 97 | 1748 | full_timestamp_flag = get_bits(&gb, 1); | |
| 98 | 1748 | skip_bits(&gb, 1); /* discontinuity_flag */ | |
| 99 | 1748 | cnt_dropped_flag = get_bits(&gb, 1); /* cnt_dropped_flag */ | |
| 100 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 1748 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
1748 | if (cnt_dropped_flag && counting_type > 1 && counting_type < 7) |
| 101 | ✗ | tc->dropframe = 1; | |
| 102 | 1748 | tc->frame = get_bits(&gb, 8); /* n_frames */ | |
| 103 |
2/2✓ Branch 0 taken 1202 times.
✓ Branch 1 taken 546 times.
|
1748 | if (full_timestamp_flag) { |
| 104 | 1202 | tc->full = 1; | |
| 105 | 1202 | tc->seconds = get_bits(&gb, 6); /* seconds_value 0..59 */ | |
| 106 | 1202 | tc->minutes = get_bits(&gb, 6); /* minutes_value 0..59 */ | |
| 107 | 1202 | tc->hours = get_bits(&gb, 5); /* hours_value 0..23 */ | |
| 108 | } else { | ||
| 109 | 546 | tc->seconds = tc->minutes = tc->hours = tc->full = 0; | |
| 110 |
2/2✓ Branch 1 taken 86 times.
✓ Branch 2 taken 460 times.
|
546 | if (get_bits(&gb, 1)) { /* seconds_flag */ |
| 111 | 86 | tc->seconds = get_bits(&gb, 6); | |
| 112 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
|
86 | if (get_bits(&gb, 1)) { /* minutes_flag */ |
| 113 | ✗ | tc->minutes = get_bits(&gb, 6); | |
| 114 | ✗ | if (get_bits(&gb, 1)) /* hours_flag */ | |
| 115 | ✗ | tc->hours = get_bits(&gb, 5); | |
| 116 | } | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 |
1/2✓ Branch 0 taken 1748 times.
✗ Branch 1 not taken.
|
1748 | if (sps->time_offset_length > 0) |
| 121 | 1748 | skip_bits(&gb, | |
| 122 | 1748 | sps->time_offset_length); /* time_offset */ | |
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | 4397 | av_log(logctx, AV_LOG_DEBUG, "ct_type:%X pic_struct:%d\n", | |
| 127 | 4397 | h->ct_type, h->pic_struct); | |
| 128 | } | ||
| 129 | |||
| 130 | 4417 | return 0; | |
| 131 | } | ||
| 132 | |||
| 133 | 5547 | static int decode_picture_timing(H264SEIPictureTiming *h, GetByteContext *gb, | |
| 134 | void *logctx) | ||
| 135 | { | ||
| 136 | 5547 | int size = bytestream2_get_bytes_left(gb); | |
| 137 | |||
| 138 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5547 times.
|
5547 | if (size > sizeof(h->payload)) { |
| 139 | ✗ | av_log(logctx, AV_LOG_ERROR, "Picture timing SEI payload too large\n"); | |
| 140 | ✗ | return AVERROR_INVALIDDATA; | |
| 141 | } | ||
| 142 | 5547 | bytestream2_get_bufferu(gb, h->payload, size); | |
| 143 | |||
| 144 | 5547 | h->payload_size_bytes = size; | |
| 145 | |||
| 146 | 5547 | h->present = 1; | |
| 147 | 5547 | return 0; | |
| 148 | } | ||
| 149 | |||
| 150 | 286 | static int decode_recovery_point(H264SEIRecoveryPoint *h, GetBitContext *gb, void *logctx) | |
| 151 | { | ||
| 152 | 286 | unsigned recovery_frame_cnt = get_ue_golomb_long(gb); | |
| 153 | |||
| 154 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 286 times.
|
286 | if (recovery_frame_cnt >= (1<<MAX_LOG2_MAX_FRAME_NUM)) { |
| 155 | ✗ | av_log(logctx, AV_LOG_ERROR, "recovery_frame_cnt %u is out of range\n", recovery_frame_cnt); | |
| 156 | ✗ | return AVERROR_INVALIDDATA; | |
| 157 | } | ||
| 158 | |||
| 159 | 286 | h->recovery_frame_cnt = recovery_frame_cnt; | |
| 160 | /* 1b exact_match_flag, | ||
| 161 | * 1b broken_link_flag, | ||
| 162 | * 2b changing_slice_group_idc */ | ||
| 163 | 286 | skip_bits(gb, 4); | |
| 164 | |||
| 165 | 286 | return 0; | |
| 166 | } | ||
| 167 | |||
| 168 | 1225 | static int decode_buffering_period(H264SEIBufferingPeriod *h, GetBitContext *gb, | |
| 169 | const H264ParamSets *ps, void *logctx) | ||
| 170 | { | ||
| 171 | unsigned int sps_id; | ||
| 172 | int sched_sel_idx; | ||
| 173 | const SPS *sps; | ||
| 174 | |||
| 175 | 1225 | sps_id = get_ue_golomb_31(gb); | |
| 176 |
2/4✓ Branch 0 taken 1225 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1225 times.
|
1225 | if (sps_id > 31 || !ps->sps_list[sps_id]) { |
| 177 | ✗ | av_log(logctx, AV_LOG_ERROR, | |
| 178 | "non-existing SPS %d referenced in buffering period\n", sps_id); | ||
| 179 | ✗ | return sps_id > 31 ? AVERROR_INVALIDDATA : AVERROR_PS_NOT_FOUND; | |
| 180 | } | ||
| 181 | 1225 | sps = ps->sps_list[sps_id]; | |
| 182 | |||
| 183 | // NOTE: This is really so duplicated in the standard... See H.264, D.1.1 | ||
| 184 |
2/2✓ Branch 0 taken 603 times.
✓ Branch 1 taken 622 times.
|
1225 | if (sps->nal_hrd_parameters_present_flag) { |
| 185 |
2/2✓ Branch 0 taken 603 times.
✓ Branch 1 taken 603 times.
|
1206 | for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) { |
| 186 | 603 | h->initial_cpb_removal_delay[sched_sel_idx] = | |
| 187 | 603 | get_bits_long(gb, sps->initial_cpb_removal_delay_length); | |
| 188 | // initial_cpb_removal_delay_offset | ||
| 189 | 603 | skip_bits(gb, sps->initial_cpb_removal_delay_length); | |
| 190 | } | ||
| 191 | } | ||
| 192 |
2/2✓ Branch 0 taken 352 times.
✓ Branch 1 taken 873 times.
|
1225 | if (sps->vcl_hrd_parameters_present_flag) { |
| 193 |
2/2✓ Branch 0 taken 352 times.
✓ Branch 1 taken 352 times.
|
704 | for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) { |
| 194 | 352 | h->initial_cpb_removal_delay[sched_sel_idx] = | |
| 195 | 352 | get_bits_long(gb, sps->initial_cpb_removal_delay_length); | |
| 196 | // initial_cpb_removal_delay_offset | ||
| 197 | 352 | skip_bits(gb, sps->initial_cpb_removal_delay_length); | |
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | 1225 | h->present = 1; | |
| 202 | 1225 | return 0; | |
| 203 | } | ||
| 204 | |||
| 205 | ✗ | static int decode_green_metadata(H264SEIGreenMetaData *h, GetByteContext *gb) | |
| 206 | { | ||
| 207 | ✗ | h->green_metadata_type = bytestream2_get_byte(gb); | |
| 208 | |||
| 209 | ✗ | if (h->green_metadata_type == 0) { | |
| 210 | ✗ | h->period_type = bytestream2_get_byte(gb); | |
| 211 | |||
| 212 | ✗ | if (h->period_type == 2) | |
| 213 | ✗ | h->num_seconds = bytestream2_get_be16(gb); | |
| 214 | ✗ | else if (h->period_type == 3) | |
| 215 | ✗ | h->num_pictures = bytestream2_get_be16(gb); | |
| 216 | |||
| 217 | ✗ | h->percent_non_zero_macroblocks = bytestream2_get_byte(gb); | |
| 218 | ✗ | h->percent_intra_coded_macroblocks = bytestream2_get_byte(gb); | |
| 219 | ✗ | h->percent_six_tap_filtering = bytestream2_get_byte(gb); | |
| 220 | ✗ | h->percent_alpha_point_deblocking_instance = bytestream2_get_byte(gb); | |
| 221 | |||
| 222 | ✗ | } else if (h->green_metadata_type == 1) { | |
| 223 | ✗ | h->xsd_metric_type = bytestream2_get_byte(gb); | |
| 224 | ✗ | h->xsd_metric_value = bytestream2_get_be16(gb); | |
| 225 | } | ||
| 226 | |||
| 227 | ✗ | return 0; | |
| 228 | } | ||
| 229 | |||
| 230 | 7117 | int ff_h264_sei_decode(H264SEIContext *h, GetBitContext *gb, | |
| 231 | const H264ParamSets *ps, void *logctx) | ||
| 232 | { | ||
| 233 | GetByteContext gbyte; | ||
| 234 | 7117 | int master_ret = 0; | |
| 235 | |||
| 236 | av_assert1((get_bits_count(gb) % 8) == 0); | ||
| 237 | 7117 | bytestream2_init(&gbyte, gb->buffer + get_bits_count(gb) / 8, | |
| 238 | 7117 | get_bits_left(gb) / 8); | |
| 239 | |||
| 240 |
4/4✓ Branch 1 taken 9044 times.
✓ Branch 2 taken 7094 times.
✓ Branch 4 taken 9028 times.
✓ Branch 5 taken 16 times.
|
16138 | while (bytestream2_get_bytes_left(&gbyte) > 2 && bytestream2_peek_ne16(&gbyte)) { |
| 241 | GetByteContext gbyte_payload; | ||
| 242 | GetBitContext gb_payload; | ||
| 243 | 9028 | int type = 0; | |
| 244 | 9028 | unsigned size = 0; | |
| 245 | 9028 | int ret = 0; | |
| 246 | |||
| 247 | do { | ||
| 248 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9052 times.
|
9052 | if (bytestream2_get_bytes_left(&gbyte) <= 0) |
| 249 | 7 | return AVERROR_INVALIDDATA; | |
| 250 | 9052 | type += bytestream2_peek_byteu(&gbyte); | |
| 251 |
2/2✓ Branch 1 taken 24 times.
✓ Branch 2 taken 9028 times.
|
9052 | } while (bytestream2_get_byteu(&gbyte) == 255); |
| 252 | |||
| 253 | do { | ||
| 254 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9348 times.
|
9348 | if (bytestream2_get_bytes_left(&gbyte) <= 0) |
| 255 | ✗ | return AVERROR_INVALIDDATA; | |
| 256 | 9348 | size += bytestream2_peek_byteu(&gbyte); | |
| 257 |
2/2✓ Branch 1 taken 320 times.
✓ Branch 2 taken 9028 times.
|
9348 | } while (bytestream2_get_byteu(&gbyte) == 255); |
| 258 | |||
| 259 |
2/2✓ Branch 1 taken 7 times.
✓ Branch 2 taken 9021 times.
|
9028 | if (size > bytestream2_get_bytes_left(&gbyte)) { |
| 260 | 7 | av_log(logctx, AV_LOG_ERROR, "SEI type %d size %d truncated at %d\n", | |
| 261 | type, size, bytestream2_get_bytes_left(&gbyte)); | ||
| 262 | 7 | return AVERROR_INVALIDDATA; | |
| 263 | } | ||
| 264 | |||
| 265 | 9021 | bytestream2_init (&gbyte_payload, gbyte.buffer, size); | |
| 266 | 9021 | ret = init_get_bits8(&gb_payload, gbyte.buffer, size); | |
| 267 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9021 times.
|
9021 | if (ret < 0) |
| 268 | ✗ | return ret; | |
| 269 | |||
| 270 |
4/5✓ Branch 0 taken 5547 times.
✓ Branch 1 taken 286 times.
✓ Branch 2 taken 1225 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1963 times.
|
9021 | switch (type) { |
| 271 | 5547 | case SEI_TYPE_PIC_TIMING: // Picture timing SEI | |
| 272 | 5547 | ret = decode_picture_timing(&h->picture_timing, &gbyte_payload, logctx); | |
| 273 | 5547 | break; | |
| 274 | 286 | case SEI_TYPE_RECOVERY_POINT: | |
| 275 | 286 | ret = decode_recovery_point(&h->recovery_point, &gb_payload, logctx); | |
| 276 | 286 | break; | |
| 277 | 1225 | case SEI_TYPE_BUFFERING_PERIOD: | |
| 278 | 1225 | ret = decode_buffering_period(&h->buffering_period, &gb_payload, ps, logctx); | |
| 279 | 1225 | break; | |
| 280 | ✗ | case SEI_TYPE_GREEN_METADATA: | |
| 281 | ✗ | ret = decode_green_metadata(&h->green_metadata, &gbyte_payload); | |
| 282 | ✗ | break; | |
| 283 | 1963 | default: | |
| 284 | 1963 | ret = ff_h2645_sei_message_decode(&h->common, type, AV_CODEC_ID_H264, | |
| 285 | &gb_payload, &gbyte_payload, logctx); | ||
| 286 |
2/2✓ Branch 0 taken 328 times.
✓ Branch 1 taken 1635 times.
|
1963 | if (ret == FF_H2645_SEI_MESSAGE_UNHANDLED) |
| 287 | 328 | av_log(logctx, AV_LOG_DEBUG, "unknown SEI type %d\n", type); | |
| 288 | } | ||
| 289 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 9021 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
9021 | if (ret < 0 && ret != AVERROR_PS_NOT_FOUND) |
| 290 | ✗ | return ret; | |
| 291 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9021 times.
|
9021 | if (ret < 0) |
| 292 | ✗ | master_ret = ret; | |
| 293 | |||
| 294 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 9018 times.
|
9021 | if (get_bits_left(&gb_payload) < 0) { |
| 295 | 3 | av_log(logctx, AV_LOG_WARNING, "SEI type %d overread by %d bits\n", | |
| 296 | 3 | type, -get_bits_left(&gb_payload)); | |
| 297 | } | ||
| 298 | |||
| 299 | 9021 | bytestream2_skipu(&gbyte, size); | |
| 300 | } | ||
| 301 | |||
| 302 | 7110 | return master_ret; | |
| 303 | } | ||
| 304 | |||
| 305 | 24764 | const char *ff_h264_sei_stereo_mode(const H2645SEIFramePacking *h) | |
| 306 | { | ||
| 307 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24764 times.
|
24764 | if (h->arrangement_cancel_flag == 0) { |
| 308 | ✗ | switch (h->arrangement_type) { | |
| 309 | ✗ | case SEI_FPA_H264_TYPE_CHECKERBOARD: | |
| 310 | ✗ | if (h->content_interpretation_type == 2) | |
| 311 | ✗ | return "checkerboard_rl"; | |
| 312 | else | ||
| 313 | ✗ | return "checkerboard_lr"; | |
| 314 | ✗ | case SEI_FPA_H264_TYPE_INTERLEAVE_COLUMN: | |
| 315 | ✗ | if (h->content_interpretation_type == 2) | |
| 316 | ✗ | return "col_interleaved_rl"; | |
| 317 | else | ||
| 318 | ✗ | return "col_interleaved_lr"; | |
| 319 | ✗ | case SEI_FPA_H264_TYPE_INTERLEAVE_ROW: | |
| 320 | ✗ | if (h->content_interpretation_type == 2) | |
| 321 | ✗ | return "row_interleaved_rl"; | |
| 322 | else | ||
| 323 | ✗ | return "row_interleaved_lr"; | |
| 324 | ✗ | case SEI_FPA_TYPE_SIDE_BY_SIDE: | |
| 325 | ✗ | if (h->content_interpretation_type == 2) | |
| 326 | ✗ | return "right_left"; | |
| 327 | else | ||
| 328 | ✗ | return "left_right"; | |
| 329 | ✗ | case SEI_FPA_TYPE_TOP_BOTTOM: | |
| 330 | ✗ | if (h->content_interpretation_type == 2) | |
| 331 | ✗ | return "bottom_top"; | |
| 332 | else | ||
| 333 | ✗ | return "top_bottom"; | |
| 334 | ✗ | case SEI_FPA_TYPE_INTERLEAVE_TEMPORAL: | |
| 335 | ✗ | if (h->content_interpretation_type == 2) | |
| 336 | ✗ | return "block_rl"; | |
| 337 | else | ||
| 338 | ✗ | return "block_lr"; | |
| 339 | ✗ | case SEI_FPA_H264_TYPE_2D: | |
| 340 | default: | ||
| 341 | ✗ | return "mono"; | |
| 342 | } | ||
| 343 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24764 times.
|
24764 | } else if (h->arrangement_cancel_flag == 1) { |
| 344 | ✗ | return "mono"; | |
| 345 | } else { | ||
| 346 | 24764 | return NULL; | |
| 347 | } | ||
| 348 | } | ||
| 349 |