FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264_sei.c
Date: 2026-06-06 18:10:07
Exec Total Coverage
Lines: 123 185 66.5%
Functions: 7 8 87.5%
Branches: 54 108 50.0%

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 62546 void ff_h264_sei_uninit(H264SEIContext *h)
49 {
50 62546 h->recovery_point.recovery_frame_cnt = -1;
51
52 62546 h->picture_timing.dpb_output_delay = 0;
53 62546 h->picture_timing.cpb_removal_delay = -1;
54
55 62546 h->picture_timing.present = 0;
56 62546 h->buffering_period.present = 0;
57 62546 h->common.frame_packing.present = 0;
58 62546 h->common.display_orientation.present = 0;
59
60 62546 ff_h2645_sei_reset(&h->common);
61 62546 }
62
63 4623 int ff_h264_sei_process_picture_timing(H264SEIPictureTiming *h, const SPS *sps,
64 void *logctx)
65 {
66 GetBitContext gb;
67 av_unused int ret;
68
69 4623 ret = init_get_bits8(&gb, h->payload, h->payload_size_bytes);
70 av_assert1(ret >= 0);
71
72
2/2
✓ Branch 0 taken 1946 times.
✓ Branch 1 taken 2677 times.
4623 if (sps->nal_hrd_parameters_present_flag ||
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1946 times.
1946 sps->vcl_hrd_parameters_present_flag) {
74 2677 h->cpb_removal_delay = get_bits_long(&gb, sps->cpb_removal_delay_length);
75 2677 h->dpb_output_delay = get_bits_long(&gb, sps->dpb_output_delay_length);
76 }
77
2/2
✓ Branch 0 taken 4603 times.
✓ Branch 1 taken 20 times.
4623 if (sps->pic_struct_present_flag) {
78 unsigned int i, num_clock_ts;
79
80 4603 h->pic_struct = get_bits(&gb, 4);
81 4603 h->ct_type = 0;
82
83
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4603 times.
4603 if (h->pic_struct > H264_SEI_PIC_STRUCT_FRAME_TRIPLING)
84 return AVERROR_INVALIDDATA;
85
86 4603 num_clock_ts = sei_num_clock_ts_table[h->pic_struct];
87 4603 h->timecode_cnt = 0;
88
2/2
✓ Branch 0 taken 7389 times.
✓ Branch 1 taken 4603 times.
11992 for (i = 0; i < num_clock_ts; i++) {
89
2/2
✓ Branch 1 taken 1748 times.
✓ Branch 2 taken 5641 times.
7389 if (get_bits(&gb, 1)) { /* clock_timestamp_flag */
90 1748 H264SEITimeCode *tc = &h->timecode[h->timecode_cnt++];
91 unsigned int full_timestamp_flag;
92 unsigned int counting_type, cnt_dropped_flag;
93 1748 h->ct_type |= 1 << get_bits(&gb, 2);
94 1748 skip_bits(&gb, 1); /* nuit_field_based_flag */
95 1748 counting_type = get_bits(&gb, 5); /* counting_type */
96 1748 full_timestamp_flag = get_bits(&gb, 1);
97 1748 skip_bits(&gb, 1); /* discontinuity_flag */
98 1748 cnt_dropped_flag = get_bits(&gb, 1); /* cnt_dropped_flag */
99
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)
100 tc->dropframe = 1;
101 1748 tc->frame = get_bits(&gb, 8); /* n_frames */
102
2/2
✓ Branch 0 taken 1202 times.
✓ Branch 1 taken 546 times.
1748 if (full_timestamp_flag) {
103 1202 tc->full = 1;
104 1202 tc->seconds = get_bits(&gb, 6); /* seconds_value 0..59 */
105 1202 tc->minutes = get_bits(&gb, 6); /* minutes_value 0..59 */
106 1202 tc->hours = get_bits(&gb, 5); /* hours_value 0..23 */
107 } else {
108 546 tc->seconds = tc->minutes = tc->hours = tc->full = 0;
109
2/2
✓ Branch 1 taken 86 times.
✓ Branch 2 taken 460 times.
546 if (get_bits(&gb, 1)) { /* seconds_flag */
110 86 tc->seconds = get_bits(&gb, 6);
111
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
86 if (get_bits(&gb, 1)) { /* minutes_flag */
112 tc->minutes = get_bits(&gb, 6);
113 if (get_bits(&gb, 1)) /* hours_flag */
114 tc->hours = get_bits(&gb, 5);
115 }
116 }
117 }
118
119
1/2
✓ Branch 0 taken 1748 times.
✗ Branch 1 not taken.
1748 if (sps->time_offset_length > 0)
120 1748 skip_bits(&gb,
121 1748 sps->time_offset_length); /* time_offset */
122 }
123 }
124
125 4603 av_log(logctx, AV_LOG_DEBUG, "ct_type:%X pic_struct:%d\n",
126 4603 h->ct_type, h->pic_struct);
127 }
128
129 4623 return 0;
130 }
131
132 5753 static int decode_picture_timing(H264SEIPictureTiming *h, GetByteContext *gb,
133 void *logctx)
134 {
135 5753 int size = bytestream2_get_bytes_left(gb);
136
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5753 times.
5753 if (size > sizeof(h->payload)) {
138 av_log(logctx, AV_LOG_ERROR, "Picture timing SEI payload too large\n");
139 return AVERROR_INVALIDDATA;
140 }
141 5753 bytestream2_get_bufferu(gb, h->payload, size);
142
143 5753 h->payload_size_bytes = size;
144
145 5753 h->present = 1;
146 5753 return 0;
147 }
148
149 286 static int decode_recovery_point(H264SEIRecoveryPoint *h, GetBitContext *gb, void *logctx)
150 {
151 286 unsigned recovery_frame_cnt = get_ue_golomb_long(gb);
152
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 286 times.
286 if (recovery_frame_cnt >= (1<<MAX_LOG2_MAX_FRAME_NUM)) {
154 av_log(logctx, AV_LOG_ERROR, "recovery_frame_cnt %u is out of range\n", recovery_frame_cnt);
155 return AVERROR_INVALIDDATA;
156 }
157
158 286 h->recovery_frame_cnt = recovery_frame_cnt;
159 /* 1b exact_match_flag,
160 * 1b broken_link_flag,
161 * 2b changing_slice_group_idc */
162 286 skip_bits(gb, 4);
163
164 286 return 0;
165 }
166
167 1331 static int decode_buffering_period(H264SEIBufferingPeriod *h, GetBitContext *gb,
168 const H264ParamSets *ps, void *logctx)
169 {
170 unsigned int sps_id;
171 int sched_sel_idx;
172 const SPS *sps;
173
174 1331 sps_id = get_ue_golomb_31(gb);
175
2/4
✓ Branch 0 taken 1331 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1331 times.
1331 if (sps_id > 31 || !ps->sps_list[sps_id]) {
176 av_log(logctx, AV_LOG_ERROR,
177 "non-existing SPS %d referenced in buffering period\n", sps_id);
178 return sps_id > 31 ? AVERROR_INVALIDDATA : AVERROR_PS_NOT_FOUND;
179 }
180 1331 sps = ps->sps_list[sps_id];
181
182 // NOTE: This is really so duplicated in the standard... See H.264, D.1.1
183
2/2
✓ Branch 0 taken 709 times.
✓ Branch 1 taken 622 times.
1331 if (sps->nal_hrd_parameters_present_flag) {
184
2/2
✓ Branch 0 taken 709 times.
✓ Branch 1 taken 709 times.
1418 for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
185 709 h->initial_cpb_removal_delay[sched_sel_idx] =
186 709 get_bits_long(gb, sps->initial_cpb_removal_delay_length);
187 // initial_cpb_removal_delay_offset
188 709 skip_bits(gb, sps->initial_cpb_removal_delay_length);
189 }
190 }
191
2/2
✓ Branch 0 taken 352 times.
✓ Branch 1 taken 979 times.
1331 if (sps->vcl_hrd_parameters_present_flag) {
192
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++) {
193 352 h->initial_cpb_removal_delay[sched_sel_idx] =
194 352 get_bits_long(gb, sps->initial_cpb_removal_delay_length);
195 // initial_cpb_removal_delay_offset
196 352 skip_bits(gb, sps->initial_cpb_removal_delay_length);
197 }
198 }
199
200 1331 h->present = 1;
201 1331 return 0;
202 }
203
204 static int decode_green_metadata(H264SEIGreenMetaData *h, GetByteContext *gb)
205 {
206 h->green_metadata_type = bytestream2_get_byte(gb);
207
208 if (h->green_metadata_type == 0) {
209 h->period_type = bytestream2_get_byte(gb);
210
211 if (h->period_type == 2)
212 h->num_seconds = bytestream2_get_be16(gb);
213 else if (h->period_type == 3)
214 h->num_pictures = bytestream2_get_be16(gb);
215
216 h->percent_non_zero_macroblocks = bytestream2_get_byte(gb);
217 h->percent_intra_coded_macroblocks = bytestream2_get_byte(gb);
218 h->percent_six_tap_filtering = bytestream2_get_byte(gb);
219 h->percent_alpha_point_deblocking_instance = bytestream2_get_byte(gb);
220
221 } else if (h->green_metadata_type == 1) {
222 h->xsd_metric_type = bytestream2_get_byte(gb);
223 h->xsd_metric_value = bytestream2_get_be16(gb);
224 }
225
226 return 0;
227 }
228
229 7592 int ff_h264_sei_decode(H264SEIContext *h, GetBitContext *gb,
230 const H264ParamSets *ps, void *logctx)
231 {
232 GetByteContext gbyte;
233 7592 int master_ret = 0;
234
235 av_assert1((get_bits_count(gb) % 8) == 0);
236 7592 bytestream2_init(&gbyte, gb->buffer + get_bits_count(gb) / 8,
237 7592 get_bits_left(gb) / 8);
238
239
4/4
✓ Branch 1 taken 9519 times.
✓ Branch 2 taken 7569 times.
✓ Branch 4 taken 9503 times.
✓ Branch 5 taken 16 times.
17088 while (bytestream2_get_bytes_left(&gbyte) > 2 && bytestream2_peek_ne16(&gbyte)) {
240 GetByteContext gbyte_payload;
241 GetBitContext gb_payload;
242 9503 int type = 0;
243 9503 unsigned size = 0;
244 9503 int ret = 0;
245
246 do {
247
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9527 times.
9527 if (bytestream2_get_bytes_left(&gbyte) <= 0)
248 7 return AVERROR_INVALIDDATA;
249 9527 type += bytestream2_peek_byteu(&gbyte);
250
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 9503 times.
9527 } while (bytestream2_get_byteu(&gbyte) == 255);
251
252 do {
253
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9945 times.
9945 if (bytestream2_get_bytes_left(&gbyte) <= 0)
254 return AVERROR_INVALIDDATA;
255 9945 size += bytestream2_peek_byteu(&gbyte);
256
2/2
✓ Branch 1 taken 442 times.
✓ Branch 2 taken 9503 times.
9945 } while (bytestream2_get_byteu(&gbyte) == 255);
257
258
2/2
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 9496 times.
9503 if (size > bytestream2_get_bytes_left(&gbyte)) {
259 7 av_log(logctx, AV_LOG_ERROR, "SEI type %d size %d truncated at %d\n",
260 type, size, bytestream2_get_bytes_left(&gbyte));
261 7 return AVERROR_INVALIDDATA;
262 }
263
264 9496 bytestream2_init (&gbyte_payload, gbyte.buffer, size);
265 9496 ret = init_get_bits8(&gb_payload, gbyte.buffer, size);
266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9496 times.
9496 if (ret < 0)
267 return ret;
268
269
4/5
✓ Branch 0 taken 5753 times.
✓ Branch 1 taken 286 times.
✓ Branch 2 taken 1331 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2126 times.
9496 switch (type) {
270 5753 case SEI_TYPE_PIC_TIMING: // Picture timing SEI
271 5753 ret = decode_picture_timing(&h->picture_timing, &gbyte_payload, logctx);
272 5753 break;
273 286 case SEI_TYPE_RECOVERY_POINT:
274 286 ret = decode_recovery_point(&h->recovery_point, &gb_payload, logctx);
275 286 break;
276 1331 case SEI_TYPE_BUFFERING_PERIOD:
277 1331 ret = decode_buffering_period(&h->buffering_period, &gb_payload, ps, logctx);
278 1331 break;
279 case SEI_TYPE_GREEN_METADATA:
280 ret = decode_green_metadata(&h->green_metadata, &gbyte_payload);
281 break;
282 2126 default:
283 2126 ret = ff_h2645_sei_message_decode(&h->common, type, AV_CODEC_ID_H264,
284 &gb_payload, &gbyte_payload, logctx);
285
2/2
✓ Branch 0 taken 328 times.
✓ Branch 1 taken 1798 times.
2126 if (ret == FF_H2645_SEI_MESSAGE_UNHANDLED)
286 328 av_log(logctx, AV_LOG_DEBUG, "unknown SEI type %d\n", type);
287 }
288
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 9496 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
9496 if (ret < 0 && ret != AVERROR_PS_NOT_FOUND)
289 return ret;
290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9496 times.
9496 if (ret < 0)
291 master_ret = ret;
292
293
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 9493 times.
9496 if (get_bits_left(&gb_payload) < 0) {
294 3 av_log(logctx, AV_LOG_WARNING, "SEI type %d overread by %d bits\n",
295 3 type, -get_bits_left(&gb_payload));
296 }
297
298 9496 bytestream2_skipu(&gbyte, size);
299 }
300
301 7585 return master_ret;
302 }
303
304 24989 const char *ff_h264_sei_stereo_mode(const H2645SEIFramePacking *h)
305 {
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24989 times.
24989 if (h->arrangement_cancel_flag == 0) {
307 switch (h->arrangement_type) {
308 case SEI_FPA_H264_TYPE_CHECKERBOARD:
309 if (h->content_interpretation_type == 2)
310 return "checkerboard_rl";
311 else
312 return "checkerboard_lr";
313 case SEI_FPA_H264_TYPE_INTERLEAVE_COLUMN:
314 if (h->content_interpretation_type == 2)
315 return "col_interleaved_rl";
316 else
317 return "col_interleaved_lr";
318 case SEI_FPA_H264_TYPE_INTERLEAVE_ROW:
319 if (h->content_interpretation_type == 2)
320 return "row_interleaved_rl";
321 else
322 return "row_interleaved_lr";
323 case SEI_FPA_TYPE_SIDE_BY_SIDE:
324 if (h->content_interpretation_type == 2)
325 return "right_left";
326 else
327 return "left_right";
328 case SEI_FPA_TYPE_TOP_BOTTOM:
329 if (h->content_interpretation_type == 2)
330 return "bottom_top";
331 else
332 return "top_bottom";
333 case SEI_FPA_TYPE_INTERLEAVE_TEMPORAL:
334 if (h->content_interpretation_type == 2)
335 return "block_rl";
336 else
337 return "block_lr";
338 case SEI_FPA_H264_TYPE_2D:
339 default:
340 return "mono";
341 }
342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24989 times.
24989 } else if (h->arrangement_cancel_flag == 1) {
343 return "mono";
344 } else {
345 24989 return NULL;
346 }
347 }
348