../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
200 / 298
67.1%
Branches:
67 / 120
55.8%
Functions:
12 / 14
85.7%

libavcodec/apv_decode.c
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 <stdatomic.h>
20
21 #include "libavutil/attributes.h"
22 #include "libavutil/avassert.h"
23 #include "libavutil/mastering_display_metadata.h"
24 #include "libavutil/mem_internal.h"
25 #include "libavutil/pixdesc.h"
26 #include "libavutil/thread.h"
27
28 #include "apv.h"
29 #include "apv_decode.h"
30 #include "avcodec.h"
31 #include "cbs_apv.h"
32 #include "codec_internal.h"
33 #include "decode.h"
34 #include "internal.h"
35 #include "thread.h"
36 #include "hwconfig.h"
37 #include "hwaccel_internal.h"
38 #include "config_components.h"
39
40 static const enum AVPixelFormat apv_format_table[5][4] = {
41 { AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16 },
42 { AV_PIX_FMT_NONE, AV_PIX_FMT_NONE, AV_PIX_FMT_NONE, AV_PIX_FMT_NONE }, // 4:2:0 is not valid.
43 { AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16 },
44 { AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16 },
45 { AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_NONE, AV_PIX_FMT_YUVA444P16 },
46 };
47
48 APVVLCLUT ff_apv_decode_lut;
49
50 10 static enum AVPixelFormat get_pixel_format(AVCodecContext *avctx,
51 enum AVPixelFormat pix_fmt)
52 {
53 10 enum AVPixelFormat pix_fmts[] = {
54 #if CONFIG_APV_VULKAN_HWACCEL
55 AV_PIX_FMT_VULKAN,
56 #endif
57 pix_fmt,
58 AV_PIX_FMT_NONE,
59 };
60
61 10 return ff_get_format(avctx, pix_fmts);
62 }
63
64 16 static int apv_decode_check_format(AVCodecContext *avctx,
65 const APVRawFrameHeader *header)
66 {
67 int err, bit_depth;
68 enum AVPixelFormat pix_fmt;
69 16 int coded_width = FFALIGN(header->frame_info.frame_width, 16);
70 16 int coded_height = FFALIGN(header->frame_info.frame_height, 16);
71 16 APVDecodeContext *apv = avctx->priv_data;
72
73 16 avctx->profile = header->frame_info.profile_idc;
74 16 avctx->level = header->frame_info.level_idc;
75
76 16 bit_depth = header->frame_info.bit_depth_minus8 + 8;
77 av_assert1(bit_depth >= 10 && bit_depth <= 16); // checked by CBS
78
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 16 times.
16 if (bit_depth % 2) {
79 ✗ avpriv_request_sample(avctx, "Bit depth %d", bit_depth);
80 ✗ return AVERROR_PATCHWELCOME;
81 }
82
83 16 pix_fmt = apv_format_table[header->frame_info.chroma_format_idc][(bit_depth - 10) >> 1];
84
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 16 times.
16 if (pix_fmt == AV_PIX_FMT_NONE) {
85 ✗ avpriv_request_sample(avctx, "YUVA444P14");
86 ✗ return AVERROR_PATCHWELCOME;
87 }
88
89
2/2
✓ Branch 8 → 9 taken 10 times.
✓ Branch 8 → 10 taken 6 times.
16 if (avctx->coded_width != coded_width ||
90
2/2
✓ Branch 9 → 10 taken 4 times.
✓ Branch 9 → 13 taken 6 times.
10 avctx->coded_height != coded_height) {
91 10 err = ff_set_dimensions(avctx, coded_width, coded_height);
92
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 10 times.
10 if (err < 0) {
93 // Unsupported frame size.
94 ✗ return err;
95 }
96 }
97
98 16 avctx->width = header->frame_info.frame_width;
99 16 avctx->height = header->frame_info.frame_height;
100
101
2/2
✓ Branch 13 → 14 taken 10 times.
✓ Branch 13 → 18 taken 6 times.
16 if (pix_fmt != apv->pix_fmt) {
102 10 apv->pix_fmt = pix_fmt;
103
104 10 err = get_pixel_format(avctx, pix_fmt);
105
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 10 times.
10 if (err < 0)
106 ✗ return err;
107
108 10 avctx->pix_fmt = err;
109 }
110
111 16 avctx->sample_aspect_ratio = (AVRational){ 1, 1 };
112
113 16 avctx->color_primaries = header->color_primaries;
114 16 avctx->color_trc = header->transfer_characteristics;
115 16 avctx->colorspace = header->matrix_coefficients;
116 32 avctx->color_range = header->full_range_flag ? AVCOL_RANGE_JPEG
117
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 16 times.
16 : AVCOL_RANGE_MPEG;
118 16 avctx->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
119
120 16 avctx->refs = 0;
121 16 avctx->has_b_frames = 0;
122
123 16 return 0;
124 }
125
126 static const CodedBitstreamUnitType apv_decompose_unit_types[] = {
127 APV_PBU_PRIMARY_FRAME,
128 APV_PBU_METADATA,
129 };
130
131 static AVOnce apv_entropy_once = AV_ONCE_INIT;
132
133 7 static av_cold void apv_entropy_build_decode_lut(void)
134 {
135 7 ff_apv_entropy_build_decode_lut(&ff_apv_decode_lut);
136 7 }
137
138 10 static av_cold int apv_decode_init(AVCodecContext *avctx)
139 {
140 10 APVDecodeContext *apv = avctx->priv_data;
141 int err;
142
143 10 apv->pix_fmt = AV_PIX_FMT_NONE;
144
145 10 ff_thread_once(&apv_entropy_once, apv_entropy_build_decode_lut);
146
147 10 err = ff_cbs_init(&apv->cbc, AV_CODEC_ID_APV, avctx);
148
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 10 times.
10 if (err < 0)
149 ✗ return err;
150
151 10 apv->cbc->decompose_unit_types =
152 apv_decompose_unit_types;
153 10 apv->cbc->nb_decompose_unit_types =
154 FF_ARRAY_ELEMS(apv_decompose_unit_types);
155
156 // Extradata could be set here, but is ignored by the decoder.
157
158 10 apv->pkt = avctx->internal->in_pkt;
159 10 ff_apv_dsp_init(&apv->dsp);
160
161 10 atomic_init(&apv->tile_errors, 0);
162
163 10 return 0;
164 }
165
166 ✗ static av_cold void apv_decode_flush(AVCodecContext *avctx)
167 {
168 ✗ APVDecodeContext *apv = avctx->priv_data;
169
170 ✗ apv->nb_unit = 0;
171 ✗ av_packet_unref(apv->pkt);
172 ✗ ff_cbs_fragment_reset(&apv->au);
173 ✗ ff_cbs_flush(apv->cbc);
174 ✗ }
175
176 10 static av_cold int apv_decode_close(AVCodecContext *avctx)
177 {
178 10 APVDecodeContext *apv = avctx->priv_data;
179
180 10 ff_cbs_fragment_free(&apv->au);
181 10 ff_cbs_close(&apv->cbc);
182
183 10 return 0;
184 }
185
186 14400 static int apv_decode_block(AVCodecContext *avctx,
187 void *output,
188 ptrdiff_t pitch,
189 GetBitContext *gbc,
190 APVEntropyState *entropy_state,
191 const void *constants)
192 {
193 14400 APVDecodeContext *apv = avctx->priv_data;
194 int err;
195
196 // Aligned for SIMD use in transquant.
197 14400 LOCAL_ALIGNED_32(int16_t, coeff, [APV_ENTROPY_COEFF_SIZE]);
198 // Zero only the used entries, padding entries may be written but
199 // are never read.
200 14400 memset(coeff, 0, 64 * sizeof(int16_t));
201
202 14400 err = ff_apv_entropy_decode_block(coeff, gbc, entropy_state);
203
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 14400 times.
14400 if (err < 0)
204 ✗ return err;
205
206 14400 apv->dsp.decode_transquant(output, pitch, coeff, constants);
207
208 14400 return 0;
209 }
210
211 42 static int apv_decode_tile_component(AVCodecContext *avctx, void *data,
212 int job, int thread)
213 {
214 42 APVRawFrame *input = data;
215 42 APVDecodeContext *apv = avctx->priv_data;
216 42 const APVDerivedTileInfo *tile_info = &apv->tile_info;
217 const AVPixFmtDescriptor *pix_fmt_desc =
218 42 av_pix_fmt_desc_get(apv->pix_fmt);
219 42 int nb_components = pix_fmt_desc->nb_components;
220
221 42 int tile_index = job / nb_components;
222 42 int comp_index = job % nb_components;
223
224
2/2
✓ Branch 3 → 4 taken 24 times.
✓ Branch 3 → 5 taken 18 times.
42 int sub_w_shift = comp_index == 0 ? 0 : pix_fmt_desc->log2_chroma_w;
225
2/2
✓ Branch 6 → 7 taken 24 times.
✓ Branch 6 → 8 taken 18 times.
42 int sub_h_shift = comp_index == 0 ? 0 : pix_fmt_desc->log2_chroma_h;
226
227 42 APVRawTile *tile = &input->tile[tile_index];
228
229 42 int tile_y = tile_index / tile_info->tile_cols;
230 42 int tile_x = tile_index % tile_info->tile_cols;
231
232 42 int tile_start_x = tile_info->col_starts[tile_x];
233 42 int tile_start_y = tile_info->row_starts[tile_y];
234
235 42 int tile_width = tile_info->col_starts[tile_x + 1] - tile_start_x;
236 42 int tile_height = tile_info->row_starts[tile_y + 1] - tile_start_y;
237
238 42 int tile_mb_width = tile_width / APV_MB_WIDTH;
239 42 int tile_mb_height = tile_height / APV_MB_HEIGHT;
240
241 42 int blk_mb_width = 2 >> sub_w_shift;
242 42 int blk_mb_height = 2 >> sub_h_shift;
243
244 42 LOCAL_ALIGNED_32(uint8_t, constants, [APV_DSP_CONSTANTS_SIZE]);
245
246 GetBitContext gbc;
247
248 42 APVEntropyState entropy_state = {
249 .log_ctx = avctx,
250 .decode_lut = &ff_apv_decode_lut,
251 .prev_dc = 0,
252 .prev_k_dc = 5,
253 .prev_k_level = 0,
254 };
255
256 int err;
257
258 42 err = init_get_bits8(&gbc, tile->tile_data[comp_index],
259 42 tile->tile_header.tile_data_size[comp_index]);
260
1/2
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 42 times.
42 if (err < 0)
261 ✗ goto fail;
262
263 // Combine the bitstream quantisation matrix with the qp scaling
264 // in advance. (Including qp_shift as well would overflow 16 bits.)
265 // Fix the row ordering at the same time. Everything else which
266 // is constant for the tile component is derived from this by the
267 // DSP code.
268 {
269 static const uint8_t apv_level_scale[6] = { 40, 45, 51, 57, 64, 71 };
270 42 int qp = tile->tile_header.tile_qp[comp_index];
271 42 int level_scale = apv_level_scale[qp % 6];
272 42 int bit_depth = input->frame_header.frame_info.bit_depth_minus8 + 8;
273 int16_t qmatrix_scaled[64];
274
275
2/2
✓ Branch 17 → 13 taken 336 times.
✓ Branch 17 → 18 taken 42 times.
378 for (int y = 0; y < 8; y++) {
276
2/2
✓ Branch 15 → 14 taken 2688 times.
✓ Branch 15 → 16 taken 336 times.
3024 for (int x = 0; x < 8; x++)
277 2688 qmatrix_scaled[y * 8 + x] = level_scale *
278 2688 input->frame_header.quantization_matrix.q_matrix[comp_index][x][y];
279 }
280
281 42 apv->dsp.prepare_constants(constants, qmatrix_scaled,
282 bit_depth, qp / 6);
283 }
284
285
2/2
✓ Branch 33 → 20 taken 504 times.
✓ Branch 33 → 34 taken 42 times.
546 for (int mb_y = 0; mb_y < tile_mb_height; mb_y++) {
286
2/2
✓ Branch 31 → 21 taken 5040 times.
✓ Branch 31 → 32 taken 504 times.
5544 for (int mb_x = 0; mb_x < tile_mb_width; mb_x++) {
287
2/2
✓ Branch 29 → 22 taken 10080 times.
✓ Branch 29 → 30 taken 5040 times.
15120 for (int blk_y = 0; blk_y < blk_mb_height; blk_y++) {
288
2/2
✓ Branch 27 → 23 taken 14400 times.
✓ Branch 27 → 28 taken 10080 times.
24480 for (int blk_x = 0; blk_x < blk_mb_width; blk_x++) {
289 14400 int frame_y = (tile_start_y +
290 14400 APV_MB_HEIGHT * mb_y +
291 14400 APV_TR_SIZE * blk_y) >> sub_h_shift;
292 14400 int frame_x = (tile_start_x +
293 14400 APV_MB_WIDTH * mb_x +
294 14400 APV_TR_SIZE * blk_x) >> sub_w_shift;
295
296 14400 ptrdiff_t frame_pitch = apv->output_frame->linesize[comp_index];
297 14400 uint8_t *block_start = apv->output_frame->data[comp_index] +
298 14400 frame_y * frame_pitch + 2 * frame_x;
299
300 14400 err = apv_decode_block(avctx,
301 block_start, frame_pitch,
302 &gbc, &entropy_state,
303 constants);
304
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 14400 times.
14400 if (err < 0) {
305 // Error in block decode means entropy desync,
306 // so this is not recoverable.
307 ✗ goto fail;
308 }
309 }
310 }
311 }
312 }
313
314 42 av_log(avctx, AV_LOG_TRACE,
315 "Decoded tile %d component %d: %dx%d MBs starting at (%d,%d)\n",
316 tile_index, comp_index, tile_mb_width, tile_mb_height,
317 tile_start_x, tile_start_y);
318
319 42 return 0;
320
321 ✗ fail:
322 ✗ av_log(avctx, AV_LOG_VERBOSE,
323 "Decode error in tile %d component %d.\n",
324 tile_index, comp_index);
325 ✗ atomic_fetch_add_explicit(&apv->tile_errors, 1, memory_order_relaxed);
326 ✗ return err;
327 }
328
329 9 static void apv_derive_tile_info(APVDerivedTileInfo *ti,
330 const APVRawFrameHeader *fh)
331 {
332 9 int frame_width_in_mbs = (fh->frame_info.frame_width + (APV_MB_WIDTH - 1)) >> 4;
333 9 int frame_height_in_mbs = (fh->frame_info.frame_height + (APV_MB_HEIGHT - 1)) >> 4;
334 int start_mb, i;
335
336 9 start_mb = 0;
337
2/2
✓ Branch 4 → 3 taken 18 times.
✓ Branch 4 → 5 taken 9 times.
27 for (i = 0; start_mb < frame_width_in_mbs; i++) {
338 18 ti->col_starts[i] = start_mb * APV_MB_WIDTH;
339 18 start_mb += fh->tile_info.tile_width_in_mbs;
340 }
341 9 ti->col_starts[i] = frame_width_in_mbs * APV_MB_WIDTH;
342 9 ti->tile_cols = i;
343
344 9 start_mb = 0;
345
2/2
✓ Branch 7 → 6 taken 9 times.
✓ Branch 7 → 8 taken 9 times.
18 for (i = 0; start_mb < frame_height_in_mbs; i++) {
346 9 ti->row_starts[i] = start_mb * APV_MB_HEIGHT;
347 9 start_mb += fh->tile_info.tile_height_in_mbs;
348 }
349 9 ti->row_starts[i] = frame_height_in_mbs * APV_MB_HEIGHT;
350 9 ti->tile_rows = i;
351
352 9 ti->num_tiles = ti->tile_cols * ti->tile_rows;
353 9 }
354
355 16 static int apv_decode(AVCodecContext *avctx, AVFrame *output,
356 APVRawFrame *input)
357 {
358 16 APVDecodeContext *apv = avctx->priv_data;
359 16 const AVPixFmtDescriptor *desc = NULL;
360 16 APVDerivedTileInfo *tile_info = &apv->tile_info;
361 int err, job_count;
362
363 16 err = apv_decode_check_format(avctx, &input->frame_header);
364
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 16 times.
16 if (err < 0) {
365 ✗ av_log(avctx, AV_LOG_ERROR, "Unsupported format parameters.\n");
366 ✗ return err;
367 }
368
369
2/2
✓ Branch 6 → 7 taken 7 times.
✓ Branch 6 → 8 taken 9 times.
16 if (avctx->skip_frame == AVDISCARD_ALL)
370 7 return 0;
371
372 9 desc = av_pix_fmt_desc_get(apv->pix_fmt);
373
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 12 taken 9 times.
9 av_assert0(desc);
374
375 9 err = ff_thread_get_buffer(avctx, output, 0);
376
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 9 times.
9 if (err < 0)
377 ✗ return err;
378
379 9 apv->cur_raw_frame = input;
380 9 apv->output_frame = output;
381 9 atomic_store_explicit(&apv->tile_errors, 0, memory_order_relaxed);
382
383 9 apv_derive_tile_info(tile_info, &input->frame_header);
384
385 9 err = ff_hwaccel_frame_priv_alloc(avctx, &apv->hwaccel_picture_private);
386
1/2
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 19 taken 9 times.
9 if (err < 0)
387 ✗ return err;
388
389 9 ff_thread_finish_setup(avctx);
390
391
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 38 taken 9 times.
9 if (avctx->hwaccel) {
392 ✗ const FFHWAccel *hwaccel = ffhwaccel(avctx->hwaccel);
393
394 ✗ err = hwaccel->start_frame(avctx, apv->pkt->buf,
395 ✗ apv->pkt->data, apv->pkt->size);
396
0/2
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 25 not taken.
✗ if (err < 0)
397 ✗ return err;
398
399
0/2
✗ Branch 33 → 26 not taken.
✗ Branch 33 → 34 not taken.
✗ for (int j = 0; j < desc->nb_components; j++) {
400
0/2
✗ Branch 31 → 27 not taken.
✗ Branch 31 → 32 not taken.
✗ for (int i = 0; i < tile_info->num_tiles; i++) {
401 ✗ APVRawTile *tile = &input->tile[i];
402 ✗ err = hwaccel->decode_slice(avctx, tile->tile_data[j],
403 tile->tile_header.tile_data_size[j]);
404
0/2
✗ Branch 28 → 29 not taken.
✗ Branch 28 → 30 not taken.
✗ if (err < 0)
405 ✗ return err;
406 }
407 }
408
409 ✗ err = hwaccel->end_frame(avctx);
410
0/2
✗ Branch 35 → 36 not taken.
✗ Branch 35 → 37 not taken.
✗ if (err < 0)
411 ✗ return err;
412
413 ✗ av_refstruct_unref(&apv->hwaccel_picture_private);
414 } else {
415 // Each component within a tile is independent of every other,
416 // so we can decode all in parallel.
417 9 job_count = tile_info->num_tiles * desc->nb_components;
418
419 9 avctx->execute2(avctx, apv_decode_tile_component,
420 input, NULL, job_count);
421
422 9 err = atomic_load_explicit(&apv->tile_errors, memory_order_relaxed);
423
1/2
✗ Branch 39 → 40 not taken.
✓ Branch 39 → 44 taken 9 times.
9 if (err > 0) {
424 ✗ av_log(avctx, AV_LOG_ERROR,
425 "Decode errors in %d tile components.\n", err);
426
0/2
✗ Branch 41 → 42 not taken.
✗ Branch 41 → 43 not taken.
✗ if (avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) {
427 // Output the frame anyway.
428 ✗ output->flags |= AV_FRAME_FLAG_CORRUPT;
429 } else {
430 ✗ return AVERROR_INVALIDDATA;
431 }
432 }
433 }
434
435 9 return 0;
436 }
437
438 16 static int apv_decode_metadata(AVCodecContext *avctx, AVFrame *frame,
439 const APVRawMetadata *md)
440 {
441 int err;
442
443
2/2
✓ Branch 27 → 3 taken 32 times.
✓ Branch 27 → 28 taken 16 times.
48 for (int i = 0; i < md->metadata_count; i++) {
444 32 const APVRawMetadataPayload *pl = &md->payloads[i];
445
446
1/3
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 19 not taken.
✓ Branch 3 → 25 taken 32 times.
32 switch (pl->payload_type) {
447 ✗ case APV_METADATA_MDCV:
448 {
449 ✗ const APVRawMetadataMDCV *mdcv = &pl->mdcv;
450 AVMasteringDisplayMetadata *mdm;
451
452 ✗ err = ff_decode_mastering_display_new(avctx, frame, &mdm);
453
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ if (err < 0)
454 ✗ return err;
455
456
0/2
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 18 not taken.
✗ if (mdm) {
457
0/2
✗ Branch 12 → 9 not taken.
✗ Branch 12 → 13 not taken.
✗ for (int j = 0; j < 3; j++) {
458 ✗ mdm->display_primaries[j][0] =
459 ✗ av_make_q(mdcv->primary_chromaticity_x[j], 1 << 16);
460 ✗ mdm->display_primaries[j][1] =
461 ✗ av_make_q(mdcv->primary_chromaticity_y[j], 1 << 16);
462 }
463
464 ✗ mdm->white_point[0] =
465 ✗ av_make_q(mdcv->white_point_chromaticity_x, 1 << 16);
466 ✗ mdm->white_point[1] =
467 ✗ av_make_q(mdcv->white_point_chromaticity_y, 1 << 16);
468
469 ✗ mdm->max_luminance =
470 ✗ av_make_q(mdcv->max_mastering_luminance, 1 << 8);
471 ✗ mdm->min_luminance =
472 ✗ av_make_q(mdcv->min_mastering_luminance, 1 << 14);
473
474 ✗ mdm->has_primaries = 1;
475 ✗ mdm->has_luminance = 1;
476 }
477 }
478 ✗ break;
479 ✗ case APV_METADATA_CLL:
480 {
481 ✗ const APVRawMetadataCLL *cll = &pl->cll;
482 AVContentLightMetadata *clm;
483
484 ✗ err = ff_decode_content_light_new(avctx, frame, &clm);
485
0/2
✗ Branch 20 → 21 not taken.
✗ Branch 20 → 22 not taken.
✗ if (err < 0)
486 ✗ return err;
487
488
0/2
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 24 not taken.
✗ if (clm) {
489 ✗ clm->MaxCLL = cll->max_cll;
490 ✗ clm->MaxFALL = cll->max_fall;
491 }
492 }
493 ✗ break;
494 32 default:
495 // Ignore other types of metadata.
496 32 break;
497 }
498 }
499
500 16 return 0;
501 }
502
503 32 static int apv_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
504 {
505 32 APVDecodeContext *apv = avctx->priv_data;
506 32 CodedBitstreamFragment *au = &apv->au;
507 int i, err;
508
509
2/2
✓ Branch 20 → 3 taken 32 times.
✓ Branch 20 → 21 taken 16 times.
48 for (i = apv->nb_unit; i < au->nb_units; i++) {
510 32 CodedBitstreamUnit *pbu = &au->units[i];
511
512
2/5
✓ Branch 3 → 4 taken 16 times.
✓ Branch 3 → 6 taken 16 times.
✗ Branch 3 → 8 not taken.
✗ Branch 3 → 13 not taken.
✗ Branch 3 → 14 not taken.
32 switch (pbu->type) {
513 16 case APV_PBU_PRIMARY_FRAME:
514 16 err = apv_decode(avctx, frame, pbu->content);
515 16 frame->pkt_dts = apv->pkt->dts;
516 16 i++;
517 16 goto end;
518 16 case APV_PBU_METADATA:
519 16 apv_decode_metadata(avctx, frame, pbu->content);
520 16 break;
521 ✗ case APV_PBU_NON_PRIMARY_FRAME:
522 case APV_PBU_PREVIEW_FRAME:
523 case APV_PBU_DEPTH_FRAME:
524 case APV_PBU_ALPHA_FRAME:
525
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 12 not taken.
✗ if (!avctx->internal->is_copy &&
526
0/2
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 12 not taken.
✗ !apv->warned_additional_frames) {
527 ✗ av_log(avctx, AV_LOG_WARNING,
528 "Stream contains additional non-primary frames "
529 "which will be ignored by the decoder.\n");
530 ✗ apv->warned_additional_frames = 1;
531 }
532 ✗ break;
533 ✗ case APV_PBU_ACCESS_UNIT_INFORMATION:
534 case APV_PBU_FILLER:
535 // Not relevant to the decoder.
536 ✗ break;
537 ✗ default:
538
0/2
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 18 not taken.
✗ if (!avctx->internal->is_copy &&
539
0/2
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 18 not taken.
✗ !apv->warned_unknown_pbu_types) {
540 ✗ av_log(avctx, AV_LOG_WARNING,
541 "Stream contains PBUs with unknown types "
542 "which will be ignored by the decoder.\n");
543 ✗ apv->warned_unknown_pbu_types = 1;
544 }
545 ✗ break;
546 }
547 }
548
549 16 err = AVERROR(EAGAIN);
550 32 end:
551
1/2
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 25 taken 32 times.
32 av_assert0(i <= apv->au.nb_units);
552 32 apv->nb_unit = i;
553
554
5/6
✓ Branch 25 → 26 taken 16 times.
✓ Branch 25 → 27 taken 16 times.
✓ Branch 26 → 27 taken 16 times.
✗ Branch 26 → 28 not taken.
✓ Branch 27 → 28 taken 16 times.
✓ Branch 27 → 31 taken 16 times.
32 if ((err < 0 && err != AVERROR(EAGAIN)) || apv->au.nb_units == i) {
555 16 av_packet_unref(apv->pkt);
556 16 ff_cbs_fragment_reset(&apv->au);
557 16 apv->nb_unit = 0;
558 }
559
4/4
✓ Branch 31 → 32 taken 16 times.
✓ Branch 31 → 34 taken 16 times.
✓ Branch 32 → 33 taken 7 times.
✓ Branch 32 → 34 taken 9 times.
32 if (!err && !frame->buf[0])
560 7 err = AVERROR(EAGAIN);
561
562 32 return err;
563 }
564
565 35 static int apv_receive_frame(AVCodecContext *avctx, AVFrame *frame)
566 {
567 35 APVDecodeContext *apv = avctx->priv_data;
568 int err;
569
570 do {
571
2/2
✓ Branch 3 → 4 taken 42 times.
✓ Branch 3 → 14 taken 16 times.
58 if (!apv->au.nb_units) {
572 42 err = ff_decode_get_packet(avctx, apv->pkt);
573
2/2
✓ Branch 5 → 6 taken 26 times.
✓ Branch 5 → 7 taken 16 times.
42 if (err < 0)
574 26 return err;
575
576 16 err = ff_cbs_read_packet(apv->cbc, &apv->au, apv->pkt);
577
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 13 taken 16 times.
16 if (err < 0) {
578 ✗ ff_cbs_fragment_reset(&apv->au);
579 ✗ av_packet_unref(apv->pkt);
580 ✗ av_log(avctx, AV_LOG_ERROR, "Failed to read packet.\n");
581 ✗ return err;
582 }
583
584 16 apv->nb_unit = 0;
585 16 av_log(avctx, AV_LOG_DEBUG, "Total PBUs on this packet: %d.\n",
586 apv->au.nb_units);
587 }
588
589 32 err = apv_receive_frame_internal(avctx, frame);
590
2/2
✓ Branch 15 → 16 taken 23 times.
✓ Branch 15 → 17 taken 9 times.
32 } while (err == AVERROR(EAGAIN));
591
592 9 return err;
593 }
594
595 #if HAVE_THREADS
596 ✗ static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
597 {
598 ✗ APVDecodeContext *asrc = src->priv_data;
599 ✗ APVDecodeContext *adst = dst->priv_data;
600
601 ✗ adst->pix_fmt = asrc->pix_fmt;
602
603 ✗ return 0;
604 }
605 #endif
606
607 const FFCodec ff_apv_decoder = {
608 .p.name = "apv",
609 CODEC_LONG_NAME("Advanced Professional Video"),
610 .p.type = AVMEDIA_TYPE_VIDEO,
611 .p.id = AV_CODEC_ID_APV,
612 .priv_data_size = sizeof(APVDecodeContext),
613 .init = apv_decode_init,
614 .flush = apv_decode_flush,
615 .close = apv_decode_close,
616 FF_CODEC_RECEIVE_FRAME_CB(apv_receive_frame),
617 UPDATE_THREAD_CONTEXT(update_thread_context),
618 .p.capabilities = AV_CODEC_CAP_DR1 |
619 AV_CODEC_CAP_SLICE_THREADS |
620 AV_CODEC_CAP_FRAME_THREADS,
621 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
622 .hw_configs = (const AVCodecHWConfigInternal *const []) {
623 #if CONFIG_APV_VULKAN_HWACCEL
624 HWACCEL_VULKAN(apv),
625 #endif
626 NULL
627 },
628 };
629