FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/sei.c
Date: 2024-10-27 21:33:06
Exec Total Coverage
Lines: 110 166 66.3%
Functions: 9 9 100.0%
Branches: 56 100 56.0%

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 7047 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 7047 hash_type = bytestream2_get_byte(gb);
38
39
2/2
✓ Branch 0 taken 21141 times.
✓ Branch 1 taken 7047 times.
28188 for (cIdx = 0; cIdx < 3/*((s->sps->chroma_format_idc == 0) ? 1 : 3)*/; cIdx++) {
40
2/2
✓ Branch 0 taken 20091 times.
✓ Branch 1 taken 1050 times.
21141 if (hash_type == 0) {
41 20091 s->is_md5 = 1;
42 20091 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 7047 return 0;
50 }
51
52 1394 static int decode_nal_sei_pic_timing(HEVCSEI *s, GetBitContext *gb,
53 const HEVCParamSets *ps, void *logctx)
54 {
55 1394 HEVCSEIPictureTiming *h = &s->picture_timing;
56 1394 const HEVCSPS *sps = ps->sps_list[s->active_seq_parameter_set_id];
57
58
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 1328 times.
1394 if (!sps)
59 66 return AVERROR_INVALIDDATA;
60
61
2/2
✓ Branch 0 taken 727 times.
✓ Branch 1 taken 601 times.
1328 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 1328 return 0;
80 }
81
82 35 static int decode_nal_sei_active_parameter_sets(HEVCSEI *s, GetBitContext *gb, void *logctx)
83 {
84 int num_sps_ids_minus1;
85 unsigned active_seq_parameter_set_id;
86
87 35 get_bits(gb, 4); // active_video_parameter_set_id
88 35 get_bits(gb, 1); // self_contained_cvs_flag
89 35 get_bits(gb, 1); // num_sps_ids_minus1
90 35 num_sps_ids_minus1 = get_ue_golomb_long(gb); // num_sps_ids_minus1
91
92
2/4
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 35 times.
35 if (num_sps_ids_minus1 < 0 || num_sps_ids_minus1 > 15) {
93 av_log(logctx, AV_LOG_ERROR, "num_sps_ids_minus1 %d invalid\n", num_sps_ids_minus1);
94 return AVERROR_INVALIDDATA;
95 }
96
97 35 active_seq_parameter_set_id = get_ue_golomb_long(gb);
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (active_seq_parameter_set_id >= HEVC_MAX_SPS_COUNT) {
99 av_log(logctx, AV_LOG_ERROR, "active_parameter_set_id %d invalid\n", active_seq_parameter_set_id);
100 return AVERROR_INVALIDDATA;
101 }
102 35 s->active_seq_parameter_set_id = active_seq_parameter_set_id;
103
104 35 return 0;
105 }
106
107 136 static int decode_nal_sei_timecode(HEVCSEITimeCode *s, GetBitContext *gb)
108 {
109 136 s->num_clock_ts = get_bits(gb, 2);
110
111
2/2
✓ Branch 0 taken 136 times.
✓ Branch 1 taken 136 times.
272 for (int i = 0; i < s->num_clock_ts; i++) {
112 136 s->clock_timestamp_flag[i] = get_bits(gb, 1);
113
114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
136 if (s->clock_timestamp_flag[i]) {
115 s->units_field_based_flag[i] = get_bits(gb, 1);
116 s->counting_type[i] = get_bits(gb, 5);
117 s->full_timestamp_flag[i] = get_bits(gb, 1);
118 s->discontinuity_flag[i] = get_bits(gb, 1);
119 s->cnt_dropped_flag[i] = get_bits(gb, 1);
120
121 s->n_frames[i] = get_bits(gb, 9);
122
123 if (s->full_timestamp_flag[i]) {
124 s->seconds_value[i] = av_clip(get_bits(gb, 6), 0, 59);
125 s->minutes_value[i] = av_clip(get_bits(gb, 6), 0, 59);
126 s->hours_value[i] = av_clip(get_bits(gb, 5), 0, 23);
127 } else {
128 s->seconds_flag[i] = get_bits(gb, 1);
129 if (s->seconds_flag[i]) {
130 s->seconds_value[i] = av_clip(get_bits(gb, 6), 0, 59);
131 s->minutes_flag[i] = get_bits(gb, 1);
132 if (s->minutes_flag[i]) {
133 s->minutes_value[i] = av_clip(get_bits(gb, 6), 0, 59);
134 s->hours_flag[i] = get_bits(gb, 1);
135 if (s->hours_flag[i]) {
136 s->hours_value[i] = av_clip(get_bits(gb, 5), 0, 23);
137 }
138 }
139 }
140 }
141
142 s->time_offset_length[i] = get_bits(gb, 5);
143 if (s->time_offset_length[i] > 0) {
144 s->time_offset_value[i] = get_bits_long(gb, s->time_offset_length[i]);
145 }
146 }
147 }
148
149 136 s->present = 1;
150 136 return 0;
151 }
152
153 4 static int decode_nal_sei_3d_reference_displays_info(HEVCSEITDRDI *s, GetBitContext *gb)
154 {
155 4 s->prec_ref_display_width = get_ue_golomb(gb);
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (s->prec_ref_display_width > 31)
157 return AVERROR_INVALIDDATA;
158 4 s->ref_viewing_distance_flag = get_bits1(gb);
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (s->ref_viewing_distance_flag) {
160 s->prec_ref_viewing_dist = get_ue_golomb(gb);
161 if (s->prec_ref_viewing_dist > 31)
162 return AVERROR_INVALIDDATA;
163 }
164 4 s->num_ref_displays = get_ue_golomb(gb);
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (s->num_ref_displays > 31)
166 return AVERROR_INVALIDDATA;
167 4 s->num_ref_displays += 1;
168
169
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 for (int i = 0; i < s->num_ref_displays; i++) {
170 int length;
171 4 s->left_view_id[i] = get_ue_golomb(gb);
172 4 s->right_view_id[i] = get_ue_golomb(gb);
173 4 s->exponent_ref_display_width[i] = get_bits(gb, 6);
174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (s->exponent_ref_display_width[i] > 62)
175 return AVERROR_INVALIDDATA;
176
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 else if (!s->exponent_ref_display_width[i])
177
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 length = FFMAX(0, (int)s->prec_ref_display_width - 30);
178 else
179 length = FFMAX(0, (int)s->exponent_ref_display_width[i] +
180 (int)s->prec_ref_display_width - 31);
181 4 s->mantissa_ref_display_width[i] = get_bits_long(gb, length);
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (s->ref_viewing_distance_flag) {
183 s->exponent_ref_viewing_distance[i] = get_bits(gb, 6);
184 if (s->exponent_ref_viewing_distance[i] > 62)
185 return AVERROR_INVALIDDATA;
186 else if (!s->exponent_ref_viewing_distance[i])
187 length = FFMAX(0, (int)s->prec_ref_viewing_dist - 30);
188 else
189 length = FFMAX(0, (int)s->exponent_ref_viewing_distance[i] +
190 (int)s->prec_ref_viewing_dist - 31);
191 s->mantissa_ref_viewing_distance[i] = get_bits_long(gb, length);
192 }
193 4 s->additional_shift_present_flag[i] = get_bits1(gb);
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (s->additional_shift_present_flag[i]) {
195 s->num_sample_shift[i] = get_bits(gb, 10);
196 if (s->num_sample_shift[i] > 1023)
197 return AVERROR_INVALIDDATA;
198 s->num_sample_shift[i] -= 512;
199 }
200 }
201 4 s->three_dimensional_reference_displays_extension_flag = get_bits1(gb);
202
203 4 return 0;
204 }
205
206 1878 static int decode_nal_sei_prefix(GetBitContext *gb, GetByteContext *gbyte,
207 void *logctx, HEVCSEI *s,
208 const HEVCParamSets *ps, int type)
209 {
210
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1394 times.
✓ Branch 2 taken 35 times.
✓ Branch 3 taken 136 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 309 times.
1878 switch (type) {
211 case 256: // Mismatched value from HM 8.1
212 return decode_nal_sei_decoded_picture_hash(&s->picture_hash, gbyte);
213 1394 case SEI_TYPE_PIC_TIMING:
214 1394 return decode_nal_sei_pic_timing(s, gb, ps, logctx);
215 35 case SEI_TYPE_ACTIVE_PARAMETER_SETS:
216 35 return decode_nal_sei_active_parameter_sets(s, gb, logctx);
217 136 case SEI_TYPE_TIME_CODE:
218 136 return decode_nal_sei_timecode(&s->timecode, gb);
219 4 case SEI_TYPE_THREE_DIMENSIONAL_REFERENCE_DISPLAYS_INFO:
220 4 return decode_nal_sei_3d_reference_displays_info(&s->tdrdi, gb);
221 309 default: {
222 309 int ret = ff_h2645_sei_message_decode(&s->common, type, AV_CODEC_ID_HEVC,
223 gb, gbyte, logctx);
224
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 204 times.
309 if (ret == FF_H2645_SEI_MESSAGE_UNHANDLED)
225 105 av_log(logctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
226 309 return ret;
227 }
228 }
229 }
230
231 7047 static int decode_nal_sei_suffix(GetBitContext *gb, GetByteContext *gbyte,
232 void *logctx, HEVCSEI *s, int type)
233 {
234
1/2
✓ Branch 0 taken 7047 times.
✗ Branch 1 not taken.
7047 switch (type) {
235 7047 case SEI_TYPE_DECODED_PICTURE_HASH:
236 7047 return decode_nal_sei_decoded_picture_hash(&s->picture_hash, gbyte);
237 default:
238 av_log(logctx, AV_LOG_DEBUG, "Skipped SUFFIX SEI %d\n", type);
239 return 0;
240 }
241 }
242
243 8925 static int decode_nal_sei_message(GetByteContext *gb, void *logctx, HEVCSEI *s,
244 const HEVCParamSets *ps, int nal_unit_type)
245 {
246 GetByteContext message_gbyte;
247 GetBitContext message_gb;
248 8925 int payload_type = 0;
249 8925 int payload_size = 0;
250 8925 int byte = 0xFF;
251 av_unused int ret;
252 8925 av_log(logctx, AV_LOG_DEBUG, "Decoding SEI\n");
253
254
2/2
✓ Branch 0 taken 8925 times.
✓ Branch 1 taken 8925 times.
17850 while (byte == 0xFF) {
255
2/4
✓ Branch 1 taken 8925 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 8925 times.
8925 if (bytestream2_get_bytes_left(gb) < 2 || payload_type > INT_MAX - 255)
256 return AVERROR_INVALIDDATA;
257 8925 byte = bytestream2_get_byteu(gb);
258 8925 payload_type += byte;
259 }
260 8925 byte = 0xFF;
261
2/2
✓ Branch 0 taken 9019 times.
✓ Branch 1 taken 8925 times.
17944 while (byte == 0xFF) {
262
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9019 times.
9019 if (bytestream2_get_bytes_left(gb) < 1 + payload_size)
263 return AVERROR_INVALIDDATA;
264 9019 byte = bytestream2_get_byteu(gb);
265 9019 payload_size += byte;
266 }
267
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8925 times.
8925 if (bytestream2_get_bytes_left(gb) < payload_size)
268 return AVERROR_INVALIDDATA;
269 8925 bytestream2_init(&message_gbyte, gb->buffer, payload_size);
270 8925 ret = init_get_bits8(&message_gb, gb->buffer, payload_size);
271 av_assert1(ret >= 0);
272 8925 bytestream2_skipu(gb, payload_size);
273
2/2
✓ Branch 0 taken 1878 times.
✓ Branch 1 taken 7047 times.
8925 if (nal_unit_type == HEVC_NAL_SEI_PREFIX) {
274 1878 return decode_nal_sei_prefix(&message_gb, &message_gbyte,
275 logctx, s, ps, payload_type);
276 } else { /* nal_unit_type == NAL_SEI_SUFFIX */
277 7047 return decode_nal_sei_suffix(&message_gb, &message_gbyte,
278 logctx, s, payload_type);
279 }
280 }
281
282 8913 int ff_hevc_decode_nal_sei(GetBitContext *gb, void *logctx, HEVCSEI *s,
283 const HEVCParamSets *ps, enum HEVCNALUnitType type)
284 {
285 GetByteContext gbyte;
286 int ret;
287
288 av_assert1((get_bits_count(gb) % 8) == 0);
289 8913 bytestream2_init(&gbyte, gb->buffer + get_bits_count(gb) / 8,
290 8913 get_bits_left(gb) / 8);
291
292 do {
293 8925 ret = decode_nal_sei_message(&gbyte, logctx, s, ps, type);
294
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 8859 times.
8925 if (ret < 0)
295 66 return ret;
296
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 8847 times.
8859 } while (bytestream2_get_bytes_left(&gbyte) > 0);
297 8847 return 1;
298 }
299