FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/cbs_vp9.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 154 193 79.8%
Functions: 10 11 90.9%
Branches: 89 150 59.3%

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/avassert.h"
20
21 #include "cbs.h"
22 #include "cbs_internal.h"
23 #include "cbs_vp9.h"
24
25
26 50 static int cbs_vp9_read_s(CodedBitstreamContext *ctx, GetBitContext *gbc,
27 int width, const char *name,
28 const int *subscripts, int32_t *write_to)
29 {
30 uint32_t magnitude;
31 int sign;
32 int32_t value;
33
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 CBS_TRACE_READ_START();
35
36
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
50 if (get_bits_left(gbc) < width + 1) {
37 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid signed value at "
38 "%s: bitstream ended.\n", name);
39 return AVERROR_INVALIDDATA;
40 }
41
42 50 magnitude = get_bits(gbc, width);
43 50 sign = get_bits1(gbc);
44
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 18 times.
50 value = sign ? -(int32_t)magnitude : magnitude;
45
46
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
50 CBS_TRACE_READ_END();
47
48 50 *write_to = value;
49 50 return 0;
50 }
51
52 50 static int cbs_vp9_write_s(CodedBitstreamContext *ctx, PutBitContext *pbc,
53 int width, const char *name,
54 const int *subscripts, int32_t value)
55 {
56 uint32_t magnitude;
57 int sign;
58
59
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 CBS_TRACE_WRITE_START();
60
61
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
50 if (put_bits_left(pbc) < width + 1)
62 return AVERROR(ENOSPC);
63
64 50 sign = value < 0;
65
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 18 times.
50 magnitude = sign ? -value : value;
66
67 50 put_bits(pbc, width, magnitude);
68 50 put_bits(pbc, 1, sign);
69
70
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 50 times.
50 CBS_TRACE_WRITE_END();
71
72 50 return 0;
73 }
74
75 362 static int cbs_vp9_read_increment(CodedBitstreamContext *ctx, GetBitContext *gbc,
76 uint32_t range_min, uint32_t range_max,
77 const char *name, uint32_t *write_to)
78 {
79 uint32_t value;
80
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 362 times.
362 CBS_TRACE_READ_START();
82
83
2/4
✓ Branch 0 taken 362 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 362 times.
362 av_assert0(range_min <= range_max && range_max - range_min < 32);
84
85
2/2
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 179 times.
366 for (value = range_min; value < range_max;) {
86
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 187 times.
187 if (get_bits_left(gbc) < 1) {
87 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid increment value at "
88 "%s: bitstream ended.\n", name);
89 return AVERROR_INVALIDDATA;
90 }
91
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 183 times.
187 if (get_bits1(gbc))
92 4 ++value;
93 else
94 183 break;
95 }
96
97
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 362 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
362 CBS_TRACE_READ_END_NO_SUBSCRIPTS();
98
99 362 *write_to = value;
100 362 return 0;
101 }
102
103 362 static int cbs_vp9_write_increment(CodedBitstreamContext *ctx, PutBitContext *pbc,
104 uint32_t range_min, uint32_t range_max,
105 const char *name, uint32_t value)
106 {
107 int len;
108
109
1/2
✓ Branch 0 taken 362 times.
✗ Branch 1 not taken.
362 CBS_TRACE_WRITE_START();
110
111
2/4
✓ Branch 0 taken 362 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 362 times.
362 av_assert0(range_min <= range_max && range_max - range_min < 8);
112
2/4
✓ Branch 0 taken 362 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 362 times.
362 if (value < range_min || value > range_max) {
113 av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
114 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
115 name, value, range_min, range_max);
116 return AVERROR_INVALIDDATA;
117 }
118
119
2/2
✓ Branch 0 taken 179 times.
✓ Branch 1 taken 183 times.
362 if (value == range_max)
120 179 len = range_max - range_min;
121 else
122 183 len = value - range_min + 1;
123
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 362 times.
362 if (put_bits_left(pbc) < len)
124 return AVERROR(ENOSPC);
125
126
2/2
✓ Branch 0 taken 183 times.
✓ Branch 1 taken 179 times.
362 if (len > 0)
127 183 put_bits(pbc, len, (1 << len) - 1 - (value != range_max));
128
129
2/4
✓ Branch 0 taken 362 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 362 times.
362 CBS_TRACE_WRITE_END_NO_SUBSCRIPTS();
130
131 362 return 0;
132 }
133
134 27 static int cbs_vp9_read_le(CodedBitstreamContext *ctx, GetBitContext *gbc,
135 int width, const char *name,
136 const int *subscripts, uint32_t *write_to)
137 {
138 uint32_t value;
139 int b;
140
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 CBS_TRACE_READ_START();
142
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 av_assert0(width % 8 == 0);
144
145
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
27 if (get_bits_left(gbc) < width) {
146 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid le value at "
147 "%s: bitstream ended.\n", name);
148 return AVERROR_INVALIDDATA;
149 }
150
151 27 value = 0;
152
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 27 times.
81 for (b = 0; b < width; b += 8)
153 54 value |= get_bits(gbc, 8) << b;
154
155
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
27 CBS_TRACE_READ_END();
156
157 27 *write_to = value;
158 27 return 0;
159 }
160
161 27 static int cbs_vp9_write_le(CodedBitstreamContext *ctx, PutBitContext *pbc,
162 int width, const char *name,
163 const int *subscripts, uint32_t value)
164 {
165 int b;
166
167
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 CBS_TRACE_WRITE_START();
168
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 av_assert0(width % 8 == 0);
170
171
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
27 if (put_bits_left(pbc) < width)
172 return AVERROR(ENOSPC);
173
174
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 27 times.
81 for (b = 0; b < width; b += 8)
175 54 put_bits(pbc, 8, value >> b & 0xff);
176
177
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 27 times.
27 CBS_TRACE_WRITE_END();
178
179 27 return 0;
180 }
181
182 #define HEADER(name) do { \
183 ff_cbs_trace_header(ctx, name); \
184 } while (0)
185
186 #define CHECK(call) do { \
187 err = (call); \
188 if (err < 0) \
189 return err; \
190 } while (0)
191
192 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
193 #define FUNC_VP9(rw, name) FUNC_NAME(rw, vp9, name)
194 #define FUNC(name) FUNC_VP9(READWRITE, name)
195
196 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
197
198 #define s(width, name) \
199 xs(width, name, current->name, 0, )
200 #define fs(width, name, subs, ...) \
201 xf(width, name, current->name, subs, __VA_ARGS__)
202 #define ss(width, name, subs, ...) \
203 xs(width, name, current->name, subs, __VA_ARGS__)
204
205 #define READ
206 #define READWRITE read
207 #define RWContext GetBitContext
208
209 #define f(width, name) do { \
210 uint32_t value; \
211 CHECK(ff_cbs_read_simple_unsigned(ctx, rw, width, #name, \
212 &value)); \
213 current->name = value; \
214 } while (0)
215 #define xf(width, name, var, subs, ...) do { \
216 uint32_t value; \
217 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
218 SUBSCRIPTS(subs, __VA_ARGS__), \
219 &value, 0, (1 << width) - 1)); \
220 var = value; \
221 } while (0)
222 #define xs(width, name, var, subs, ...) do { \
223 int32_t value; \
224 CHECK(cbs_vp9_read_s(ctx, rw, width, #name, \
225 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
226 var = value; \
227 } while (0)
228
229
230 #define increment(name, min, max) do { \
231 uint32_t value; \
232 CHECK(cbs_vp9_read_increment(ctx, rw, min, max, #name, &value)); \
233 current->name = value; \
234 } while (0)
235
236 #define fle(width, name, subs, ...) do { \
237 CHECK(cbs_vp9_read_le(ctx, rw, width, #name, \
238 SUBSCRIPTS(subs, __VA_ARGS__), &current->name)); \
239 } while (0)
240
241 #define delta_q(name) do { \
242 uint8_t delta_coded; \
243 int8_t delta_q; \
244 xf(1, name.delta_coded, delta_coded, 0, ); \
245 if (delta_coded) \
246 xs(4, name.delta_q, delta_q, 0, ); \
247 else \
248 delta_q = 0; \
249 current->name = delta_q; \
250 } while (0)
251
252 #define prob(name, subs, ...) do { \
253 uint8_t prob_coded; \
254 uint8_t prob; \
255 xf(1, name.prob_coded, prob_coded, subs, __VA_ARGS__); \
256 if (prob_coded) \
257 xf(8, name.prob, prob, subs, __VA_ARGS__); \
258 else \
259 prob = 255; \
260 current->name = prob; \
261 } while (0)
262
263 #define fixed(width, name, value) do { \
264 av_unused uint32_t fixed_value; \
265 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
266 0, &fixed_value, value, value)); \
267 } while (0)
268
269 #define infer(name, value) do { \
270 current->name = value; \
271 } while (0)
272
273 #define byte_alignment(rw) (get_bits_count(rw) % 8)
274
275 #include "cbs_vp9_syntax_template.c"
276
277 #undef READ
278 #undef READWRITE
279 #undef RWContext
280 #undef f
281 #undef xf
282 #undef xs
283 #undef increment
284 #undef fle
285 #undef delta_q
286 #undef prob
287 #undef fixed
288 #undef infer
289 #undef byte_alignment
290
291
292 #define WRITE
293 #define READWRITE write
294 #define RWContext PutBitContext
295
296 #define f(width, name) do { \
297 CHECK(ff_cbs_write_simple_unsigned(ctx, rw, width, #name, \
298 current->name)); \
299 } while (0)
300 #define xf(width, name, var, subs, ...) do { \
301 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
302 SUBSCRIPTS(subs, __VA_ARGS__), \
303 var, 0, (1 << width) - 1)); \
304 } while (0)
305 #define xs(width, name, var, subs, ...) do { \
306 CHECK(cbs_vp9_write_s(ctx, rw, width, #name, \
307 SUBSCRIPTS(subs, __VA_ARGS__), var)); \
308 } while (0)
309
310 #define increment(name, min, max) do { \
311 CHECK(cbs_vp9_write_increment(ctx, rw, min, max, #name, current->name)); \
312 } while (0)
313
314 #define fle(width, name, subs, ...) do { \
315 CHECK(cbs_vp9_write_le(ctx, rw, width, #name, \
316 SUBSCRIPTS(subs, __VA_ARGS__), current->name)); \
317 } while (0)
318
319 #define delta_q(name) do { \
320 xf(1, name.delta_coded, !!current->name, 0, ); \
321 if (current->name) \
322 xs(4, name.delta_q, current->name, 0, ); \
323 } while (0)
324
325 #define prob(name, subs, ...) do { \
326 xf(1, name.prob_coded, current->name != 255, subs, __VA_ARGS__); \
327 if (current->name != 255) \
328 xf(8, name.prob, current->name, subs, __VA_ARGS__); \
329 } while (0)
330
331 #define fixed(width, name, value) do { \
332 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
333 0, value, value, value)); \
334 } while (0)
335
336 #define infer(name, value) do { \
337 if (current->name != (value)) { \
338 av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
339 "%s does not match inferred value: " \
340 "%"PRId64", but should be %"PRId64".\n", \
341 #name, (int64_t)current->name, (int64_t)(value)); \
342 } \
343 } while (0)
344
345 #define byte_alignment(rw) (put_bits_count(rw) % 8)
346
347 #include "cbs_vp9_syntax_template.c"
348
349 #undef WRITE
350 #undef READWRITE
351 #undef RWContext
352 #undef f
353 #undef xf
354 #undef xs
355 #undef increment
356 #undef fle
357 #undef delta_q
358 #undef prob
359 #undef fixed
360 #undef infer
361 #undef byte_alignment
362
363
364 183 static int cbs_vp9_split_fragment(CodedBitstreamContext *ctx,
365 CodedBitstreamFragment *frag,
366 int header)
367 {
368 uint8_t superframe_header;
369 int err;
370
371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 183 times.
183 if (frag->data_size == 0)
372 return AVERROR_INVALIDDATA;
373
374 // Last byte in the packet.
375 183 superframe_header = frag->data[frag->data_size - 1];
376
377
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 170 times.
183 if ((superframe_header & 0xe0) == 0xc0) {
378 VP9RawSuperframeIndex sfi;
379 GetBitContext gbc;
380 size_t index_size, pos;
381 int i;
382
383 13 index_size = 2 + (((superframe_header & 0x18) >> 3) + 1) *
384 13 ((superframe_header & 0x07) + 1);
385
386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (index_size > frag->data_size)
387 return AVERROR_INVALIDDATA;
388
389 13 err = init_get_bits(&gbc, frag->data + frag->data_size - index_size,
390 8 * index_size);
391
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (err < 0)
392 return err;
393
394 13 err = cbs_vp9_read_superframe_index(ctx, &gbc, &sfi);
395
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (err < 0)
396 return err;
397
398 13 pos = 0;
399
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 13 times.
40 for (i = 0; i <= sfi.frames_in_superframe_minus_1; i++) {
400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (pos + sfi.frame_sizes[i] + index_size > frag->data_size) {
401 av_log(ctx->log_ctx, AV_LOG_ERROR, "Frame %d too large "
402 "in superframe: %"PRIu32" bytes.\n",
403 i, sfi.frame_sizes[i]);
404 return AVERROR_INVALIDDATA;
405 }
406
407 27 err = ff_cbs_append_unit_data(frag, 0,
408 27 frag->data + pos,
409 27 sfi.frame_sizes[i],
410 frag->data_ref);
411
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (err < 0)
412 return err;
413
414 27 pos += sfi.frame_sizes[i];
415 }
416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (pos + index_size != frag->data_size) {
417 av_log(ctx->log_ctx, AV_LOG_WARNING, "Extra padding at "
418 "end of superframe: %"SIZE_SPECIFIER" bytes.\n",
419 frag->data_size - (pos + index_size));
420 }
421
422 13 return 0;
423
424 } else {
425 170 err = ff_cbs_append_unit_data(frag, 0,
426 frag->data, frag->data_size,
427 frag->data_ref);
428
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 170 times.
170 if (err < 0)
429 return err;
430 }
431
432 170 return 0;
433 }
434
435 197 static int cbs_vp9_read_unit(CodedBitstreamContext *ctx,
436 CodedBitstreamUnit *unit)
437 {
438 VP9RawFrame *frame;
439 GetBitContext gbc;
440 int err, pos;
441
442 197 err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 197 times.
197 if (err < 0)
444 return err;
445
446 197 err = ff_cbs_alloc_unit_content(ctx, unit);
447
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 197 times.
197 if (err < 0)
448 return err;
449 197 frame = unit->content;
450
451 197 err = cbs_vp9_read_frame(ctx, &gbc, frame);
452
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 197 times.
197 if (err < 0)
453 return err;
454
455 197 pos = get_bits_count(&gbc);
456
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 197 times.
197 av_assert0(pos % 8 == 0);
457 197 pos /= 8;
458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 197 times.
197 av_assert0(pos <= unit->data_size);
459
460
2/2
✓ Branch 0 taken 181 times.
✓ Branch 1 taken 16 times.
197 if (pos == unit->data_size) {
461 // No data (e.g. a show-existing-frame frame).
462 } else {
463 181 frame->data_ref = av_buffer_ref(unit->data_ref);
464
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 181 times.
181 if (!frame->data_ref)
465 return AVERROR(ENOMEM);
466
467 181 frame->data = unit->data + pos;
468 181 frame->data_size = unit->data_size - pos;
469 }
470
471 197 return 0;
472 }
473
474 197 static int cbs_vp9_write_unit(CodedBitstreamContext *ctx,
475 CodedBitstreamUnit *unit,
476 PutBitContext *pbc)
477 {
478 197 VP9RawFrame *frame = unit->content;
479 int err;
480
481 197 err = cbs_vp9_write_frame(ctx, pbc, frame);
482
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 197 times.
197 if (err < 0)
483 return err;
484
485 // Frame must be byte-aligned.
486
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 197 times.
197 av_assert0(put_bits_count(pbc) % 8 == 0);
487
488
2/2
✓ Branch 0 taken 181 times.
✓ Branch 1 taken 16 times.
197 if (frame->data) {
489
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 181 times.
181 if (frame->data_size > put_bits_left(pbc) / 8)
490 return AVERROR(ENOSPC);
491
492 181 flush_put_bits(pbc);
493 181 memcpy(put_bits_ptr(pbc), frame->data, frame->data_size);
494 181 skip_put_bytes(pbc, frame->data_size);
495 }
496
497 197 return 0;
498 }
499
500 183 static int cbs_vp9_assemble_fragment(CodedBitstreamContext *ctx,
501 CodedBitstreamFragment *frag)
502 {
503 int err;
504
505
2/2
✓ Branch 0 taken 170 times.
✓ Branch 1 taken 13 times.
183 if (frag->nb_units == 1) {
506 // Output is just the content of the single frame.
507
508 170 CodedBitstreamUnit *frame = &frag->units[0];
509
510 170 frag->data_ref = av_buffer_ref(frame->data_ref);
511
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 170 times.
170 if (!frag->data_ref)
512 return AVERROR(ENOMEM);
513
514 170 frag->data = frame->data;
515 170 frag->data_size = frame->data_size;
516
517 } else {
518 // Build superframe out of frames.
519
520 VP9RawSuperframeIndex sfi;
521 PutBitContext pbc;
522 AVBufferRef *ref;
523 uint8_t *data;
524 size_t size, max, pos;
525 int i, size_len;
526
527
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (frag->nb_units > 8) {
528 av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many frames to "
529 "make superframe: %d.\n", frag->nb_units);
530 return AVERROR(EINVAL);
531 }
532
533 13 max = 0;
534
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 13 times.
40 for (i = 0; i < frag->nb_units; i++)
535
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 13 times.
27 if (max < frag->units[i].data_size)
536 14 max = frag->units[i].data_size;
537
538
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (max < 2)
539 size_len = 1;
540 else
541 13 size_len = av_log2(max) / 8 + 1;
542
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 av_assert0(size_len <= 4);
543
544 13 sfi.superframe_marker = VP9_SUPERFRAME_MARKER;
545 13 sfi.bytes_per_framesize_minus_1 = size_len - 1;
546 13 sfi.frames_in_superframe_minus_1 = frag->nb_units - 1;
547
548 13 size = 2;
549
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 13 times.
40 for (i = 0; i < frag->nb_units; i++) {
550 27 size += size_len + frag->units[i].data_size;
551 27 sfi.frame_sizes[i] = frag->units[i].data_size;
552 }
553
554 13 ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
555
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (!ref)
556 return AVERROR(ENOMEM);
557 13 data = ref->data;
558 13 memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
559
560 13 pos = 0;
561
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 13 times.
40 for (i = 0; i < frag->nb_units; i++) {
562
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 av_assert0(size - pos > frag->units[i].data_size);
563 27 memcpy(data + pos, frag->units[i].data,
564 27 frag->units[i].data_size);
565 27 pos += frag->units[i].data_size;
566 }
567
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 av_assert0(size - pos == 2 + frag->nb_units * size_len);
568
569 13 init_put_bits(&pbc, data + pos, size - pos);
570
571 13 err = cbs_vp9_write_superframe_index(ctx, &pbc, &sfi);
572
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (err < 0) {
573 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to write "
574 "superframe index.\n");
575 av_buffer_unref(&ref);
576 return err;
577 }
578
579
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
13 av_assert0(put_bits_left(&pbc) == 0);
580 13 flush_put_bits(&pbc);
581
582 13 frag->data_ref = ref;
583 13 frag->data = data;
584 13 frag->data_size = size;
585 }
586
587 183 return 0;
588 }
589
590 static void cbs_vp9_flush(CodedBitstreamContext *ctx)
591 {
592 CodedBitstreamVP9Context *vp9 = ctx->priv_data;
593
594 memset(vp9->ref, 0, sizeof(vp9->ref));
595 }
596
597 static const CodedBitstreamUnitTypeDescriptor cbs_vp9_unit_types[] = {
598 CBS_UNIT_TYPE_INTERNAL_REF(0, VP9RawFrame, data),
599 CBS_UNIT_TYPE_END_OF_LIST
600 };
601
602 const CodedBitstreamType ff_cbs_type_vp9 = {
603 .codec_id = AV_CODEC_ID_VP9,
604
605 .priv_data_size = sizeof(CodedBitstreamVP9Context),
606
607 .unit_types = cbs_vp9_unit_types,
608
609 .split_fragment = &cbs_vp9_split_fragment,
610 .read_unit = &cbs_vp9_read_unit,
611 .write_unit = &cbs_vp9_write_unit,
612
613 .flush = &cbs_vp9_flush,
614
615 .assemble_fragment = &cbs_vp9_assemble_fragment,
616 };
617