FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cbs_h2645.c
Date: 2026-04-24 19:58:39
Exec Total Coverage
Lines: 147 187 78.6%
Functions: 9 9 100.0%
Branches: 108 164 65.9%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "libavutil/attributes.h"
20 #include "libavutil/avassert.h"
21 #include "libavutil/mem.h"
22
23 #include "bytestream.h"
24 #include "cbs.h"
25 #include "cbs_internal.h"
26 #include "cbs_h2645.h"
27 #include "h264.h"
28 #include "h2645_parse.h"
29 #include "vvc.h"
30
31 #include "hevc/hevc.h"
32
33 19 int ff_cbs_h2645_payload_extension_present(GetBitContext *gbc, uint32_t payload_size,
34 int cur_pos)
35 {
36 19 int bits_left = payload_size * 8 - cur_pos;
37
4/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 9 times.
24 return (bits_left > 0 &&
38
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 (bits_left > 7 || show_bits(gbc, bits_left) & MAX_UINT_BITS(bits_left - 1)));
39 }
40
41 351368 int ff_cbs_read_ue_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
42 const char *name, const int *subscripts,
43 uint32_t *write_to,
44 uint32_t range_min, uint32_t range_max)
45 {
46 uint32_t leading_bits, value;
47 int max_length, leading_zeroes;
48
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 351368 times.
351368 CBS_TRACE_READ_START();
50
51
2/2
✓ Branch 1 taken 11664 times.
✓ Branch 2 taken 339704 times.
351368 max_length = FFMIN(get_bits_left(gbc), 32);
52
53
1/2
✓ Branch 0 taken 351368 times.
✗ Branch 1 not taken.
351368 leading_bits = max_length ? show_bits_long(gbc, max_length) : 0;
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 351368 times.
351368 if (leading_bits == 0) {
55 if (max_length >= 32) {
56 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
57 "%s: more than 31 zeroes.\n", name);
58 return AVERROR_INVALIDDATA;
59 } else {
60 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
61 "%s: bitstream ended.\n", name);
62 return AVERROR_INVALIDDATA;
63 }
64 }
65
66 351368 leading_zeroes = max_length - 1 - av_log2(leading_bits);
67 351368 skip_bits_long(gbc, leading_zeroes);
68
69
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 351368 times.
351368 if (get_bits_left(gbc) < leading_zeroes + 1) {
70 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
71 "%s: bitstream ended.\n", name);
72 return AVERROR_INVALIDDATA;
73 }
74
75 351368 value = get_bits_long(gbc, leading_zeroes + 1) - 1;
76
77
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 351368 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
351368 CBS_TRACE_READ_END();
78
79
2/4
✓ Branch 0 taken 351368 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 351368 times.
351368 if (value < range_min || value > range_max) {
80 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
81 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
82 name, value, range_min, range_max);
83 return AVERROR_INVALIDDATA;
84 }
85
86 351368 *write_to = value;
87 351368 return 0;
88 }
89
90 167982 int ff_cbs_read_se_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
91 const char *name, const int *subscripts,
92 int32_t *write_to,
93 int32_t range_min, int32_t range_max)
94 {
95 uint32_t leading_bits, unsigned_value;
96 int max_length, leading_zeroes;
97 int32_t value;
98
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 167982 times.
167982 CBS_TRACE_READ_START();
100
101
2/2
✓ Branch 1 taken 10043 times.
✓ Branch 2 taken 157939 times.
167982 max_length = FFMIN(get_bits_left(gbc), 32);
102
103
1/2
✓ Branch 0 taken 167982 times.
✗ Branch 1 not taken.
167982 leading_bits = max_length ? show_bits_long(gbc, max_length) : 0;
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 167982 times.
167982 if (leading_bits == 0) {
105 if (max_length >= 32) {
106 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
107 "%s: more than 31 zeroes.\n", name);
108 return AVERROR_INVALIDDATA;
109 } else {
110 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
111 "%s: bitstream ended.\n", name);
112 return AVERROR_INVALIDDATA;
113 }
114 }
115
116 167982 leading_zeroes = max_length - 1 - av_log2(leading_bits);
117 167982 skip_bits_long(gbc, leading_zeroes);
118
119
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 167982 times.
167982 if (get_bits_left(gbc) < leading_zeroes + 1) {
120 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
121 "%s: bitstream ended.\n", name);
122 return AVERROR_INVALIDDATA;
123 }
124
125 167982 unsigned_value = get_bits_long(gbc, leading_zeroes + 1);
126
127
2/2
✓ Branch 0 taken 90235 times.
✓ Branch 1 taken 77747 times.
167982 if (unsigned_value & 1)
128 90235 value = -(int32_t)(unsigned_value / 2);
129 else
130 77747 value = unsigned_value / 2;
131
132
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 167982 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
167982 CBS_TRACE_READ_END();
133
134
2/4
✓ Branch 0 taken 167982 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 167982 times.
167982 if (value < range_min || value > range_max) {
135 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
136 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
137 name, value, range_min, range_max);
138 return AVERROR_INVALIDDATA;
139 }
140
141 167982 *write_to = value;
142 167982 return 0;
143 }
144
145 155423 int ff_cbs_write_ue_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
146 const char *name, const int *subscripts,
147 uint32_t value,
148 uint32_t range_min, uint32_t range_max)
149 {
150 int len;
151
152
2/2
✓ Branch 0 taken 152354 times.
✓ Branch 1 taken 3069 times.
155423 CBS_TRACE_WRITE_START();
153
154
2/4
✓ Branch 0 taken 155423 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 155423 times.
155423 if (value < range_min || value > range_max) {
155 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
156 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
157 name, value, range_min, range_max);
158 return AVERROR_INVALIDDATA;
159 }
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 155423 times.
155423 av_assert0(value != UINT32_MAX);
161
162 155423 len = av_log2(value + 1);
163
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 155423 times.
155423 if (put_bits_left(pbc) < 2 * len + 1)
164 return AVERROR(ENOSPC);
165
166 155423 put_bits(pbc, len, 0);
167
1/2
✓ Branch 0 taken 155423 times.
✗ Branch 1 not taken.
155423 if (len + 1 < 32)
168 155423 put_bits(pbc, len + 1, value + 1);
169 else
170 put_bits32(pbc, value + 1);
171
172
3/4
✓ Branch 0 taken 152354 times.
✓ Branch 1 taken 3069 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 152354 times.
155423 CBS_TRACE_WRITE_END();
173
174 155423 return 0;
175 }
176
177 84109 int ff_cbs_write_se_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
178 const char *name, const int *subscripts,
179 int32_t value,
180 int32_t range_min, int32_t range_max)
181 {
182 int len;
183 uint32_t uvalue;
184
185
2/2
✓ Branch 0 taken 83539 times.
✓ Branch 1 taken 570 times.
84109 CBS_TRACE_WRITE_START();
186
187
2/4
✓ Branch 0 taken 84109 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 84109 times.
84109 if (value < range_min || value > range_max) {
188 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
189 "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
190 name, value, range_min, range_max);
191 return AVERROR_INVALIDDATA;
192 }
193
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84109 times.
84109 av_assert0(value != INT32_MIN);
194
195
2/2
✓ Branch 0 taken 19190 times.
✓ Branch 1 taken 64919 times.
84109 if (value == 0)
196 19190 uvalue = 0;
197
2/2
✓ Branch 0 taken 36580 times.
✓ Branch 1 taken 28339 times.
64919 else if (value > 0)
198 36580 uvalue = 2 * (uint32_t)value - 1;
199 else
200 28339 uvalue = 2 * (uint32_t)-value;
201
202 84109 len = av_log2(uvalue + 1);
203
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 84109 times.
84109 if (put_bits_left(pbc) < 2 * len + 1)
204 return AVERROR(ENOSPC);
205
206 84109 put_bits(pbc, len, 0);
207
1/2
✓ Branch 0 taken 84109 times.
✗ Branch 1 not taken.
84109 if (len + 1 < 32)
208 84109 put_bits(pbc, len + 1, uvalue + 1);
209 else
210 put_bits32(pbc, uvalue + 1);
211
212
3/4
✓ Branch 0 taken 83539 times.
✓ Branch 1 taken 570 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 83539 times.
84109 CBS_TRACE_WRITE_END();
213
214 84109 return 0;
215 }
216
217 30232 int ff_cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
218 {
219 30232 int bits_left = get_bits_left(gbc);
220
2/2
✓ Branch 0 taken 22633 times.
✓ Branch 1 taken 7599 times.
30232 if (bits_left > 8)
221 22633 return 1;
222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7599 times.
7599 if (bits_left == 0)
223 return 0;
224
2/2
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 7588 times.
7599 if (show_bits(gbc, bits_left) & MAX_UINT_BITS(bits_left - 1))
225 11 return 1;
226 7588 return 0;
227 }
228
229 12845 int ff_cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx,
230 CodedBitstreamFragment *frag,
231 const H2645Packet *packet)
232 {
233 int err, i;
234
235
2/2
✓ Branch 0 taken 39388 times.
✓ Branch 1 taken 12845 times.
52233 for (i = 0; i < packet->nb_nals; i++) {
236 39388 const H2645NAL *nal = &packet->nals[i];
237 AVBufferRef *ref;
238 39388 size_t size = nal->size;
239 39388 enum AVCodecID codec_id = ctx->codec->codec_id;
240
241
3/4
✓ Branch 0 taken 9960 times.
✓ Branch 1 taken 29428 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9960 times.
39388 if (codec_id == AV_CODEC_ID_HEVC && nal->nuh_layer_id > 0 &&
242 (nal->type < HEVC_NAL_VPS || nal->type > HEVC_NAL_PPS))
243 continue;
244
245 // Remove trailing zeroes.
246
3/4
✓ Branch 0 taken 48755 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9367 times.
✓ Branch 3 taken 39388 times.
48755 while (size > 0 && nal->data[size - 1] == 0)
247 9367 --size;
248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39388 times.
39388 if (size == 0) {
249 av_log(ctx->log_ctx, AV_LOG_VERBOSE, "Discarding empty 0 NAL unit\n");
250 continue;
251 }
252
253 78776 ref = (nal->data == nal->raw_data) ? frag->data_ref
254
2/2
✓ Branch 0 taken 37740 times.
✓ Branch 1 taken 1648 times.
39388 : packet->rbsp.rbsp_buffer_ref;
255
256 39388 err = ff_cbs_append_unit_data(frag, nal->type,
257 39388 (uint8_t*)nal->data, size, ref);
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39388 times.
39388 if (err < 0)
259 return err;
260 }
261
262 12845 return 0;
263 }
264
265 14273 int ff_cbs_h2645_write_slice_data(CodedBitstreamContext *ctx,
266 PutBitContext *pbc, const uint8_t *data,
267 size_t data_size, int data_bit_start)
268 {
269 14273 size_t rest = data_size - (data_bit_start + 7) / 8;
270 14273 const uint8_t *pos = data + data_bit_start / 8;
271
272
2/4
✓ Branch 0 taken 14273 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14273 times.
14273 av_assert0(data_bit_start >= 0 &&
273 data_size > data_bit_start / 8);
274
275
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14273 times.
14273 if (data_size * 8 + 8 > put_bits_left(pbc))
276 return AVERROR(ENOSPC);
277
278
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 14272 times.
14273 if (!rest)
279 1 goto rbsp_stop_one_bit;
280
281 // First copy the remaining bits of the first byte
282 // The above check ensures that we do not accidentally
283 // copy beyond the rbsp_stop_one_bit.
284
2/2
✓ Branch 0 taken 3729 times.
✓ Branch 1 taken 10543 times.
14272 if (data_bit_start % 8)
285 3729 put_bits(pbc, 8 - data_bit_start % 8,
286 3729 *pos++ & MAX_UINT_BITS(8 - data_bit_start % 8));
287
288
2/2
✓ Branch 1 taken 14163 times.
✓ Branch 2 taken 109 times.
14272 if (put_bits_count(pbc) % 8 == 0) {
289 // If the writer is aligned at this point,
290 // memcpy can be used to improve performance.
291 // This happens normally for CABAC.
292 14163 flush_put_bits(pbc);
293 14163 memcpy(put_bits_ptr(pbc), pos, rest);
294 14163 skip_put_bytes(pbc, rest);
295 } else {
296 // If not, we have to copy manually.
297 // rbsp_stop_one_bit forces us to special-case
298 // the last byte.
299 uint8_t temp;
300 int i;
301
302
2/2
✓ Branch 0 taken 98762 times.
✓ Branch 1 taken 109 times.
98871 for (; rest > 4; rest -= 4, pos += 4)
303 98762 put_bits32(pbc, AV_RB32(pos));
304
305
2/2
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 109 times.
274 for (; rest > 1; rest--, pos++)
306 165 put_bits(pbc, 8, *pos);
307
308 109 rbsp_stop_one_bit:
309
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 1 times.
110 temp = rest ? *pos : *pos & MAX_UINT_BITS(8 - data_bit_start % 8);
310
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 av_assert0(temp);
312 110 i = ff_ctz(*pos);
313 110 temp = temp >> i;
314
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 1 times.
110 i = rest ? (8 - i) : (8 - i - data_bit_start % 8);
315 110 put_bits(pbc, i, temp);
316
2/2
✓ Branch 1 taken 92 times.
✓ Branch 2 taken 18 times.
110 if (put_bits_count(pbc) % 8)
317 92 put_bits(pbc, 8 - put_bits_count(pbc) % 8, 0);
318 }
319
320 14273 return 0;
321 }
322
323 7328 int ff_cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
324 CodedBitstreamFragment *frag)
325 {
326 uint8_t *data;
327 size_t max_size, dp, sp;
328 int err, i, zero_run;
329
330
2/2
✓ Branch 0 taken 21877 times.
✓ Branch 1 taken 7328 times.
29205 for (i = 0; i < frag->nb_units; i++) {
331 // Data should already all have been written when we get here.
332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21877 times.
21877 av_assert0(frag->units[i].data);
333 }
334
335 7328 max_size = 0;
336
2/2
✓ Branch 0 taken 21877 times.
✓ Branch 1 taken 7328 times.
29205 for (i = 0; i < frag->nb_units; i++) {
337 // Start code + content with worst-case emulation prevention.
338 21877 max_size += 4 + frag->units[i].data_size * 3 / 2;
339 }
340
341 7328 data = av_realloc(NULL, max_size + AV_INPUT_BUFFER_PADDING_SIZE);
342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7328 times.
7328 if (!data)
343 return AVERROR(ENOMEM);
344
345 7328 dp = 0;
346
2/2
✓ Branch 0 taken 21877 times.
✓ Branch 1 taken 7328 times.
29205 for (i = 0; i < frag->nb_units; i++) {
347 21877 CodedBitstreamUnit *unit = &frag->units[i];
348
349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21877 times.
21877 if (unit->data_bit_padding > 0) {
350 if (i < frag->nb_units - 1)
351 av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
352 "unaligned padding on non-final NAL unit.\n");
353 else
354 frag->data_bit_padding = unit->data_bit_padding;
355 }
356
357
2/2
✓ Branch 1 taken 8350 times.
✓ Branch 2 taken 13527 times.
21877 if (ff_h2645_unit_requires_zero_byte(ctx->codec->codec_id, unit->type, i)) {
358 // zero_byte
359 8350 data[dp++] = 0;
360 }
361 // start_code_prefix_one_3bytes
362 21877 data[dp++] = 0;
363 21877 data[dp++] = 0;
364 21877 data[dp++] = 1;
365
366 21877 zero_run = 0;
367
2/2
✓ Branch 0 taken 19230787 times.
✓ Branch 1 taken 21877 times.
19252664 for (sp = 0; sp < unit->data_size; sp++) {
368
2/2
✓ Branch 0 taken 19223304 times.
✓ Branch 1 taken 7483 times.
19230787 if (zero_run < 2) {
369
2/2
✓ Branch 0 taken 157971 times.
✓ Branch 1 taken 19065333 times.
19223304 if (unit->data[sp] == 0)
370 157971 ++zero_run;
371 else
372 19065333 zero_run = 0;
373 } else {
374
2/2
✓ Branch 0 taken 1325 times.
✓ Branch 1 taken 6158 times.
7483 if ((unit->data[sp] & ~3) == 0) {
375 // emulation_prevention_three_byte
376 1325 data[dp++] = 3;
377 }
378 7483 zero_run = unit->data[sp] == 0;
379 }
380 19230787 data[dp++] = unit->data[sp];
381 }
382 }
383
384
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7328 times.
7328 av_assert0(dp <= max_size);
385 7328 err = av_reallocp(&data, dp + AV_INPUT_BUFFER_PADDING_SIZE);
386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7328 times.
7328 if (err)
387 return err;
388 7328 memset(data + dp, 0, AV_INPUT_BUFFER_PADDING_SIZE);
389
390 7328 frag->data_ref = av_buffer_create(data, dp + AV_INPUT_BUFFER_PADDING_SIZE,
391 NULL, NULL, 0);
392
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7328 times.
7328 if (!frag->data_ref) {
393 av_freep(&data);
394 return AVERROR(ENOMEM);
395 }
396
397 7328 frag->data = data;
398 7328 frag->data_size = dp;
399
400 7328 return 0;
401 }
402