FFmpeg
Loading...
Searching...
No Matches
apv_encode_vulkan.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Lynne <dev@lynne.ee>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <math.h>
22#include <stdlib.h>
23
24#include "libavutil/mem.h"
25#include "libavutil/opt.h"
26#include "libavutil/pixdesc.h"
27#include "libavutil/vulkan.h"
28
29#include "avcodec.h"
30#include "codec_internal.h"
31#include "encode.h"
32#include "hwconfig.h"
33#include "internal.h"
34
35#include "apv.h"
36#include "cbs.h"
37#include "cbs_apv.h"
38
39extern const unsigned char ff_apv_encode_dct_comp_spv_data[];
40extern const unsigned int ff_apv_encode_dct_comp_spv_len;
41
42extern const unsigned char ff_apv_encode_tiles_comp_spv_data[];
43extern const unsigned int ff_apv_encode_tiles_comp_spv_len;
44
45extern const unsigned char ff_seg_gather_comp_spv_data[];
46extern const unsigned int ff_seg_gather_comp_spv_len;
47
48#define APV_DEFAULT_QMAT 16
49#define APV_MAX_NUM_COMP 4
50
51typedef struct DCTPushData {
52 int frame_dim[2];
53 int tile_count[2];
58 float qf[APV_MAX_NUM_COMP]; /* per-component fact/(level_scale*2^qp_shift) */
59 uint8_t qmat[64]; /* quantisation matrix, raster order */
61
62typedef struct EntropyPushData {
63 VkDeviceAddress bytestream;
64 int tile_count[2];
66 uint32_t slot_size;
67 uint32_t comp_base; /* component index this dispatch's z=0 maps to */
68 uint32_t blocks_per_tile; /* uniform coeff stride, in blocks */
69 int frame_mb[2]; /* frame size in MBs (luma basis) */
70 int tile_mb_dim[2]; /* full-tile size in MBs */
71 uint32_t blocks_per_mb; /* blocks per MB of this dispatch's components */
73
74typedef struct CompactPushData {
75 VkDeviceAddress sparse;
76 VkDeviceAddress compacted;
77 uint32_t slot_size;
79
92
93typedef struct VulkanEncodeAPVContext {
94 const AVClass *class;
95
99
101 FFVulkanShader shd_entropy[2]; /* [0] luma-sized, [1] chroma-sized */
103
104 /* Per-frame buffer pools */
110
111 /* DCT/quantize push constants -- encoder-constant, built once at init. */
113
114 /* CBS used to assemble the output packet */
117
119
120 /* Async machinery */
124
125 /* Derived per-encoder state */
126 int frame_mb_x, frame_mb_y; /* MBs in the frame (luma basis) */
128 int tile_mb_w, tile_mb_h; /* MBs per tile (luma basis) */
130 int blocks_per_mb; /* luma; always 4 */
131 int chroma_blocks_per_mb; /* 4 for 4:4:4, 2 for 4:2:2 */
135
140
141 size_t coeffs_size; /* total size of coeffs buffer */
142 size_t bytestream_size; /* total size of bytestream buffer */
143 size_t slot_size; /* per-tile-component bytestream slot size */
144 size_t sizes_size; /* total size of sizes buffer */
145
146 /* User options */
149 int qp_y;
150 int qp_c;
151 int qmatrix; /* APV_QMATRIX_*: quantisation matrix select */
152
153 /* Benchmark knob (env APV_VULKAN_HEADERS_ONLY): the GPU still encodes,
154 * but the tiles are never downloaded and packets carry headers only. */
156
157 /* Benchmark knob (env APV_VULKAN_SKIP_ENTROPY): skip the entropy
158 * dispatch to isolate the DCT pass. Implies headers_only. */
161
162/*
163 * HEVC default 8x8 intra scaling list (ITU-T H.265, Table 7-6): flat through
164 * the low-frequency core, a gentle ramp toward the high-frequency corner.
165 * Raster order; the matrix is symmetric, so APV's [y][x]/[x][y] indexing is
166 * immaterial. APV and HEVC share the "16 = neutral" convention, so the list
167 * transfers without rescaling.
168 */
169static const uint8_t apv_qmat_hevc_intra[64] = {
170 16, 16, 16, 16, 17, 18, 21, 24,
171 16, 16, 16, 16, 17, 19, 22, 25,
172 16, 16, 17, 18, 20, 22, 25, 29,
173 16, 16, 18, 21, 24, 27, 31, 36,
174 17, 17, 20, 24, 30, 35, 41, 47,
175 18, 19, 22, 27, 35, 44, 54, 65,
176 21, 22, 25, 31, 41, 54, 70, 88,
177 24, 25, 29, 36, 47, 65, 88, 115,
178};
179
180enum {
181 APV_QMATRIX_FLAT = 0, /* uniform 16 (the spec default) */
182 APV_QMATRIX_HEVC = 1, /* HEVC default intra scaling list */
183};
184
185/*
186 * The active quantisation-matrix value at raster index i. Both the q_matrix
187 * signalled in the frame header and the encoder's pf table are derived from
188 * this single accessor, so they cannot disagree -- a mismatch would quantise
189 * against a different matrix than the decoder dequantises with.
190 */
191static int apv_qmatrix_value(int qmatrix, int i)
192{
193 return qmatrix == APV_QMATRIX_HEVC ? apv_qmat_hevc_intra[i]
195}
196
197static const uint8_t apv_level_scale[6] = { 40, 45, 51, 57, 64, 71 };
198
200{
201 switch (sw_fmt) {
214 default:
215 return -1;
216 }
217}
218
220{
221 switch (sw_fmt) {
229 default: return -1;
230 }
231}
232
234{
235 int err;
237 FFVulkanShader *shd = &ev->shd_dct;
238
239 SPEC_LIST_CREATE(sl, 1, sizeof(uint32_t))
240 SPEC_LIST_ADD(sl, 16, 32, 4); /* nb_blocks: blocks_per_mb per workgroup */
241
242 ff_vk_shader_load(shd, VK_SHADER_STAGE_COMPUTE_BIT, sl,
243 (uint32_t []) { 8, 4, 1 }, 0);
244
246 VK_SHADER_STAGE_COMPUTE_BIT);
247
248 const FFVulkanDescriptorSetBinding desc_set[] = {
249 {
250 .name = "coeffs_buf",
251 .type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
252 .stages = VK_SHADER_STAGE_COMPUTE_BIT,
253 },
254 {
255 .name = "src",
256 .type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
257 .stages = VK_SHADER_STAGE_COMPUTE_BIT,
259 },
260 };
261 ff_vk_shader_add_descriptor_set(&ev->s, shd, desc_set, 2, 0);
262
263 RET(ff_vk_shader_link(&ev->s, shd,
266
267 RET(ff_vk_shader_register_exec(&ev->s, &ev->exec_pool, shd));
268
269fail:
270 return err;
271}
272
273static int init_entropy_shader(AVCodecContext *avctx, int blocks_per_mb,
274 FFVulkanShader *shd)
275{
276 int err;
278
279 /* One workgroup per tile-component, one invocation per transform block.
280 * Luma and chroma tile-components hold different block counts under
281 * chroma sub-sampling, so each gets a pipeline with its own size. */
282 uint32_t wg = ev->tile_mb_w * ev->tile_mb_h * blocks_per_mb;
283
284 ff_vk_shader_load(shd, VK_SHADER_STAGE_COMPUTE_BIT, NULL,
285 (uint32_t []) { wg, 1, 1 }, 0);
286
288 VK_SHADER_STAGE_COMPUTE_BIT);
289
290 const FFVulkanDescriptorSetBinding desc_set[] = {
291 {
292 .name = "coeffs_buf",
293 .type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
294 .stages = VK_SHADER_STAGE_COMPUTE_BIT,
295 },
296 {
297 .name = "sizes_buf",
298 .type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
299 .stages = VK_SHADER_STAGE_COMPUTE_BIT,
300 },
301 };
302 ff_vk_shader_add_descriptor_set(&ev->s, shd, desc_set, 2, 0);
303
304 RET(ff_vk_shader_link(&ev->s, shd,
307
308 RET(ff_vk_shader_register_exec(&ev->s, &ev->exec_pool, shd));
309
310fail:
311 return err;
312}
313
315{
316 int err;
318 FFVulkanShader *shd = &ev->shd_compact;
319
320 ff_vk_shader_load(shd, VK_SHADER_STAGE_COMPUTE_BIT, NULL,
321 (uint32_t []) { 256, 1, 1 }, 0);
322
324 VK_SHADER_STAGE_COMPUTE_BIT);
325
326 const FFVulkanDescriptorSetBinding desc_set[] = {
327 {
328 .name = "sizes_buf",
329 .type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
330 .stages = VK_SHADER_STAGE_COMPUTE_BIT,
331 },
332 };
333 ff_vk_shader_add_descriptor_set(&ev->s, shd, desc_set, 1, 0);
334
335 RET(ff_vk_shader_link(&ev->s, shd,
338
339 RET(ff_vk_shader_register_exec(&ev->s, &ev->exec_pool, shd));
340
341fail:
342 return err;
343}
344
345/*
346 * The DCT/quantize shader's push constants are entirely encoder-constant:
347 * frame geometry, the per-component quant scale qf, and the quantisation
348 * matrix. Build them once -- nothing here changes between frames.
349 */
351{
354 DCTPushData *pd = &ev->dct_push;
355 const double fact = (double)(1 << (ev->bit_depth - 1));
356
357 pd->frame_dim[0] = avctx->width;
358 pd->frame_dim[1] = avctx->height;
359 pd->tile_count[0] = ev->tile_cols;
360 pd->tile_count[1] = ev->tile_rows;
361 pd->tile_mb_dim[0] = ev->tile_mb_w;
362 pd->tile_mb_dim[1] = ev->tile_mb_h;
363 pd->log2_chroma_sub[0] = desc->log2_chroma_w;
364 pd->log2_chroma_sub[1] = desc->log2_chroma_h;
365 pd->num_comp = ev->num_comp;
366 pd->bit_depth = ev->bit_depth;
367
368 /*
369 * qf[c] = fact / (level_scale * 2^qp_shift). The encoder uses one QP per
370 * component, so this never varies by tile. Component 3 is alpha
371 * (4:4:4:4): full-resolution, so it takes the luma QP.
372 */
373 for (int c = 0; c < APV_MAX_NUM_COMP; c++) {
374 int qp = (c == 0 || c == 3) ? ev->qp_y : ev->qp_c;
375 int level_scale = apv_level_scale[qp % 6];
376 int qp_shift = qp / 6;
377 pd->qf[c] =
378 (float)(fact / ((double)level_scale * (double)(1 << qp_shift)));
379 }
380
381 /*
382 * The 8-bit quantisation matrix. The shader stages it to shared memory
383 * and quantises with 1024 / qmat[i], the reciprocal partner of the
384 * decoder's per-coefficient dequant -- the same matrix that gets
385 * signalled in the frame header.
386 */
387 for (int i = 0; i < 64; i++)
388 pd->qmat[i] = apv_qmatrix_value(ev->qmatrix, i);
389}
390
392 AVFrame *frame)
393{
394 int err = 0;
396 FFVulkanFunctions *vk = &ev->s.vkfn;
398 VkImageView views[AV_NUM_DATA_POINTERS];
399
400 VkImageMemoryBarrier2 img_bar[AV_NUM_DATA_POINTERS];
401 int nb_img_bar = 0;
402 VkBufferMemoryBarrier2 buf_bar[4];
403 int nb_buf_bar = 0;
404
405 FFVkBuffer *coeffs_buf;
406 FFVkBuffer *bytestream_buf;
407
408 FFVkBuffer *gathered_buf = NULL;
409 FFVkBuffer *compacted_buf;
410 FFVkBuffer *sizes_buf;
411
412 /* Allocate per-frame buffers */
414 VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
415 NULL, ev->coeffs_size,
416 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT));
417 coeffs_buf = fd->coeffs_ref;
418
419 /* The entropy shader writes the bitstream here, sparsely -- one
420 * worst-case-sized slot per tile-component. Device-local, so those GPU
421 * writes stay in VRAM and never cross PCIe. */
423 &fd->bytestream_ref,
424 VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
425 VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT,
427 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT));
428 bytestream_buf = fd->bytestream_ref;
429
430 /* The compaction shader gathers the sparse slots into here, contiguous.
431 * Device-local: shader stores over the bus are unreliably slow on some
432 * drivers, so the transfer to the host is left to the copy engine. */
434 &gathered_buf,
435 VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
436 VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
437 VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT,
439 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT));
440
441 /* Copy-engine destination the CPU assembles the packet from.
442 * Host-visible + host-cached so the readback is a fast cached copy. */
444 &fd->compacted_ref,
445 VK_BUFFER_USAGE_TRANSFER_DST_BIT |
446 VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
447 VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT,
449 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
450 VK_MEMORY_PROPERTY_HOST_CACHED_BIT));
451 compacted_buf = fd->compacted_ref;
452
454 VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,
455 NULL, ev->sizes_size,
456 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
457 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
458 VK_MEMORY_PROPERTY_HOST_COHERENT_BIT));
459 sizes_buf = fd->sizes_ref;
460
461 ff_vk_exec_start(&ev->s, exec);
462
467
469 VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
470 VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT));
471
472 RET(ff_vk_create_imageviews(&ev->s, exec, views, frame, FF_VK_REP_INT));
473
474 ff_vk_frame_barrier(&ev->s, exec, frame,
475 img_bar, &nb_img_bar,
476 VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT,
477 VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT,
478 VK_ACCESS_SHADER_READ_BIT,
479 VK_IMAGE_LAYOUT_GENERAL,
480 VK_QUEUE_FAMILY_IGNORED);
481
482 vk->CmdPipelineBarrier2(exec->buf, &(VkDependencyInfo) {
483 .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
484 .pImageMemoryBarriers = img_bar,
485 .imageMemoryBarrierCount = nb_img_bar,
486 });
487 nb_img_bar = 0;
488
489 /* DCT + Quantize pass */
490 {
492 0, 0, 0,
493 coeffs_buf, 0, coeffs_buf->size,
494 VK_FORMAT_UNDEFINED);
495 ff_vk_shader_update_img_array(&ev->s, exec, &ev->shd_dct,
496 frame, views,
497 0, 1,
498 VK_IMAGE_LAYOUT_GENERAL,
499 VK_NULL_HANDLE);
500
501 ff_vk_exec_bind_shader(&ev->s, exec, &ev->shd_dct);
503 VK_SHADER_STAGE_COMPUTE_BIT,
504 0, sizeof(ev->dct_push), &ev->dct_push);
505
506 vk->CmdDispatch(exec->buf,
507 ev->frame_mb_x, ev->frame_mb_y, ev->num_comp);
508 }
509
510 /* Barrier: wait for coeff writes before entropy */
511 ff_vk_buf_barrier(buf_bar[nb_buf_bar++], coeffs_buf,
512 COMPUTE_SHADER_BIT, SHADER_WRITE_BIT, NONE,
513 COMPUTE_SHADER_BIT, SHADER_READ_BIT, NONE,
514 0, coeffs_buf->size);
515
516 vk->CmdPipelineBarrier2(exec->buf, &(VkDependencyInfo) {
517 .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
518 .pBufferMemoryBarriers = buf_bar,
519 .bufferMemoryBarrierCount = nb_buf_bar,
520 });
521 nb_buf_bar = 0;
522
523 /*
524 * Entropy encoding pass. Luma (component 0) and chroma (components
525 * 1..num_comp-1) run as two dispatches: under chroma sub-sampling their
526 * tile-components hold different block counts, hence different workgroup
527 * sizes -- one pipeline each. The two write disjoint memory and need no
528 * barrier between them, so the GPU is free to overlap them.
529 */
530 for (int p = 0; !ev->skip_entropy && p < 2; p++) {
531 FFVulkanShader *shd = &ev->shd_entropy[p];
532 uint32_t z_comps = (p == 0) ? 1 : ev->num_comp - 1;
533
534 if (z_comps == 0)
535 continue; /* 4:0:0 (monochrome) has no chroma components */
536
537 EntropyPushData pd = {
538 .bytestream = bytestream_buf->address,
539 .tile_count = { ev->tile_cols, ev->tile_rows },
540 .num_comp = ev->num_comp,
541 .slot_size = (uint32_t)ev->slot_size,
542 .comp_base = (uint32_t)p,
543 .blocks_per_tile = (uint32_t)ev->tile_mb_w * ev->tile_mb_h *
544 ev->blocks_per_mb,
545 .frame_mb = { ev->frame_mb_x, ev->frame_mb_y },
546 .tile_mb_dim = { ev->tile_mb_w, ev->tile_mb_h },
547 .blocks_per_mb = (uint32_t)(p == 0 ? ev->blocks_per_mb
549 };
550
551 ff_vk_shader_update_desc_buffer(&ev->s, exec, shd, 0, 0, 0,
552 coeffs_buf, 0, coeffs_buf->size,
553 VK_FORMAT_UNDEFINED);
554 ff_vk_shader_update_desc_buffer(&ev->s, exec, shd, 0, 1, 0,
555 sizes_buf, 0, sizes_buf->size,
556 VK_FORMAT_UNDEFINED);
557
558 ff_vk_exec_bind_shader(&ev->s, exec, shd);
559 ff_vk_shader_update_push_const(&ev->s, exec, shd,
560 VK_SHADER_STAGE_COMPUTE_BIT,
561 0, sizeof(pd), &pd);
562
563 vk->CmdDispatch(exec->buf, ev->tile_cols, ev->tile_rows, z_comps);
564 }
565
566 /* Compaction pass: gather the sparse per-tile-component slots into one
567 * contiguous device-local buffer, then read it back with the copy
568 * engine. */
569 if (!ev->headers_only) {
570 ff_vk_buf_barrier(buf_bar[nb_buf_bar++], bytestream_buf,
571 COMPUTE_SHADER_BIT, SHADER_WRITE_BIT, NONE,
572 COMPUTE_SHADER_BIT, SHADER_READ_BIT, NONE,
573 0, bytestream_buf->size);
574 ff_vk_buf_barrier(buf_bar[nb_buf_bar++], sizes_buf,
575 COMPUTE_SHADER_BIT, SHADER_WRITE_BIT, NONE,
576 COMPUTE_SHADER_BIT, SHADER_READ_BIT, NONE,
577 0, sizes_buf->size);
578 vk->CmdPipelineBarrier2(exec->buf, &(VkDependencyInfo) {
579 .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
580 .pBufferMemoryBarriers = buf_bar,
581 .bufferMemoryBarrierCount = nb_buf_bar,
582 });
583 nb_buf_bar = 0;
584
585 CompactPushData pd = {
586 .sparse = bytestream_buf->address,
587 .compacted = gathered_buf->address,
588 .slot_size = (uint32_t)ev->slot_size,
589 };
590
591 ff_vk_shader_update_desc_buffer(&ev->s, exec, &ev->shd_compact,
592 0, 0, 0,
593 sizes_buf, 0, sizes_buf->size,
594 VK_FORMAT_UNDEFINED);
595 ff_vk_exec_bind_shader(&ev->s, exec, &ev->shd_compact);
596 ff_vk_shader_update_push_const(&ev->s, exec, &ev->shd_compact,
597 VK_SHADER_STAGE_COMPUTE_BIT,
598 0, sizeof(pd), &pd);
599
600 vk->CmdDispatch(exec->buf, ev->tile_count * ev->num_comp, 1, 1);
601
602 /* The gathered size is only known once the encode is done, so the
603 * whole buffer is copied; the slots are sized to the entropy coder's
604 * worst case, which keeps this close to the payload size. */
605 ff_vk_buf_barrier(buf_bar[nb_buf_bar++], gathered_buf,
606 COMPUTE_SHADER_BIT, SHADER_WRITE_BIT, NONE,
607 TRANSFER_BIT, TRANSFER_READ_BIT, NONE,
608 0, gathered_buf->size);
609 vk->CmdPipelineBarrier2(exec->buf, &(VkDependencyInfo) {
610 .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
611 .pBufferMemoryBarriers = buf_bar,
612 .bufferMemoryBarrierCount = nb_buf_bar,
613 });
614 nb_buf_bar = 0;
615
616 vk->CmdCopyBuffer(exec->buf, gathered_buf->buf, compacted_buf->buf,
617 1, &(VkBufferCopy) { .size = ev->bytestream_size });
618 }
619
620 ff_vk_exec_move_dep_refstruct(&ev->s, exec, &gathered_buf);
621 err = ff_vk_exec_submit(&ev->s, exec);
622 if (err < 0)
623 goto fail;
624
625 return 0;
626
627fail:
628 av_refstruct_unref(&gathered_buf);
629 ff_vk_exec_discard_deps(&ev->s, exec);
630 return err;
631}
632
634 AVPacket *pkt)
635{
636 int err = 0;
638 FFVulkanFunctions *vk = &ev->s.vkfn;
640 FFVkBuffer *compacted_buf = fd->compacted_ref;
641 FFVkBuffer *sizes_buf = fd->sizes_ref;
642 APVRawFrame *raw_frame = NULL;
643
644 /* Wait for the GPU encode to finish */
645 ff_vk_exec_wait(&ev->s, exec);
646
647 const uint32_t *sizes = NULL;
648 static uint8_t headers_only_tile; /* 1-byte token tile data */
649
650 /* Headers-only benchmark mode never touches the GPU output. */
651 if (!ev->headers_only) {
652 /* Invalidate mapped memory if needed */
653 if (!(compacted_buf->flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)) {
654 VkMappedMemoryRange r = {
655 .sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
656 .memory = compacted_buf->mem,
657 .offset = 0,
658 .size = VK_WHOLE_SIZE,
659 };
660 vk->InvalidateMappedMemoryRanges(ev->s.hwctx->act_dev, 1, &r);
661 }
662 if (!(sizes_buf->flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)) {
663 VkMappedMemoryRange r = {
664 .sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
665 .memory = sizes_buf->mem,
666 .offset = 0,
667 .size = VK_WHOLE_SIZE,
668 };
669 vk->InvalidateMappedMemoryRanges(ev->s.hwctx->act_dev, 1, &r);
670 }
671 sizes = (const uint32_t *)sizes_buf->mapped_mem;
672 }
673
674 /* Allocate the cbs frame structure */
675 raw_frame = av_mallocz(sizeof(*raw_frame));
676 if (!raw_frame)
677 return AVERROR(ENOMEM);
678
680 raw_frame->pbu_header.group_id = 1;
681
682 APVRawFrameHeader *fh = &raw_frame->frame_header;
685 fh->frame_info.band_idc = ev->band_idc;
686 fh->frame_info.frame_width = avctx->width;
687 fh->frame_info.frame_height = avctx->height;
691
693 /* Inferred values when the flag is 0, per the spec. */
694 fh->color_primaries = 2;
696 fh->matrix_coefficients = 2;
697 fh->full_range_flag = 0;
698
699 /* compute_pf_table() builds the encoder's pf scale from the same matrix;
700 * the two must stay in sync. use_q_matrix is only signalled when the
701 * matrix is non-uniform (a flat 16 matrix is the inferred default). */
703 for (int c = 0; c < ev->num_comp; c++)
704 for (int y = 0; y < 8; y++)
705 for (int x = 0; x < 8; x++)
706 fh->quantization_matrix.q_matrix[c][y][x] =
707 apv_qmatrix_value(ev->qmatrix, y * 8 + x);
708
712
713 /* Populate each tile. The compacted buffer holds each tile-component's
714 * data back to back, in (tile, component) order -- the same layout the
715 * gather shader produced. */
716 uint32_t comp_off = 0;
717 for (int t = 0; t < ev->tile_count; t++) {
718 APVRawTile *tile = &raw_frame->tile[t];
719 uint32_t total_tile_data = 0;
720
721 tile->tile_header.tile_header_size =
722 4 + ev->num_comp * (4 + 1) + 1;
723 tile->tile_header.tile_index = t;
724
725 for (int c = 0; c < ev->num_comp; c++) {
726 uint32_t sz;
727 if (ev->headers_only) {
728 /* No readback: one token byte (CBS requires size >= 1). */
729 sz = 1;
730 tile->tile_data[c] = &headers_only_tile;
731 } else {
732 sz = sizes[t * ev->num_comp + c];
733 tile->tile_data[c] = compacted_buf->mapped_mem + comp_off;
734 comp_off += sz;
735 }
736 tile->tile_header.tile_data_size[c] = sz;
737 tile->tile_header.tile_qp[c] =
738 (c == 0 || c == 3) ? ev->qp_y : ev->qp_c;
739 total_tile_data += sz;
740 }
741 tile->tile_header.reserved_zero_8bits = 0;
742 tile->tile_dummy_byte_size = 0;
743 tile->tile_dummy_byte = NULL;
744
745 raw_frame->tile_size[t] =
746 tile->tile_header.tile_header_size + total_tile_data;
747 }
748
749 /* Assemble fragment using cbs_apv */
750 ff_cbs_fragment_reset(&ev->au);
751
752 err = ff_cbs_insert_unit_content(&ev->au, -1, APV_PBU_PRIMARY_FRAME,
753 raw_frame, NULL);
754 if (err < 0) {
755 av_freep(&raw_frame);
756 return err;
757 }
758 /* raw_frame is now owned by the fragment unit */
759 raw_frame = NULL;
760
761 /* Assemble straight into the packet: ff_cbs_write_packet() hands pkt a
762 * reference to CBS's own assembled buffer -- no copy. */
763 err = ff_cbs_write_packet(ev->cbc, pkt, &ev->au);
764 if (err < 0)
765 return err;
766
767 pkt->pts = fd->pts;
768 pkt->dts = fd->pts;
769 pkt->duration = fd->duration;
770 pkt->flags |= AV_PKT_FLAG_KEY; /* APV is all intra */
771
772 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
773 pkt->opaque = fd->frame_opaque;
774 pkt->opaque_ref = fd->frame_opaque_ref;
776 }
777
778 av_log(avctx, AV_LOG_VERBOSE, "Encoded APV frame: %i bytes (%.2f MiB)\n",
779 pkt->size, pkt->size / (1024.0 * 1024.0));
780
785
786 return 0;
787}
788
790 AVPacket *pkt)
791{
792 int err;
795 FFVkExecContext *exec;
796 AVFrame *frame;
797
798 while (1) {
799 exec = ff_vk_exec_get(&ev->s, &ev->exec_pool);
800
801 if (exec->had_submission) {
802 exec->had_submission = 0;
803 ev->in_flight--;
804 return build_packet(avctx, exec, pkt);
805 }
806
807 frame = ev->frame;
808 err = ff_encode_get_frame(avctx, frame);
809 if (err < 0 && err != AVERROR_EOF)
810 return err;
811 else if (err == AVERROR_EOF) {
812 if (!ev->in_flight)
813 return err;
814 continue;
815 }
816
817 fd = exec->opaque;
818 fd->pts = frame->pts;
819 fd->duration = frame->duration;
820 fd->flags = frame->flags;
821 if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
822 fd->frame_opaque = frame->opaque;
823 fd->frame_opaque_ref = frame->opaque_ref;
824 frame->opaque_ref = NULL;
825 }
826
827 err = submit_frame(avctx, exec, frame);
829 if (err < 0)
830 return err;
831
832 ev->in_flight++;
833 if (ev->in_flight < ev->async_depth)
834 return AVERROR(EAGAIN);
835 }
836 return 0;
837}
838
840{
842
843 ff_vk_exec_pool_free(&ev->s, &ev->exec_pool);
844
845 ff_vk_shader_free(&ev->s, &ev->shd_dct);
846 ff_vk_shader_free(&ev->s, &ev->shd_entropy[0]);
847 ff_vk_shader_free(&ev->s, &ev->shd_entropy[1]);
848 ff_vk_shader_free(&ev->s, &ev->shd_compact);
849
850 if (ev->exec_ctx_info) {
851 for (int i = 0; i < ev->async_depth; i++) {
858 }
860 }
861
867
868 ff_cbs_fragment_free(&ev->au);
869 ff_cbs_close(&ev->cbc);
870
871 av_frame_free(&ev->frame);
872 ff_vk_uninit(&ev->s);
873
874 return 0;
875}
876
878{
879 int err;
881 AVHWFramesContext *hwfc;
882
883 if (!avctx->hw_frames_ctx) {
884 av_log(avctx, AV_LOG_ERROR, "An AVHWFramesContext is required.\n");
885 return AVERROR(EINVAL);
886 }
887 hwfc = (AVHWFramesContext *)avctx->hw_frames_ctx->data;
888 ev->sw_format = hwfc->sw_format;
889
892 if (ev->profile_idc < 0 || ev->chroma_format_idc < 0) {
893 av_log(avctx, AV_LOG_ERROR, "Unsupported sw_format %s for APV.\n",
895 return AVERROR(EINVAL);
896 }
897
898 /* All four APV chroma formats are supported -- 4:0:0, 4:2:2, 4:4:4 and
899 * 4:4:4:4. The profile_idc / chroma_format_idc checks above already
900 * reject any pixel format that is not one of them. */
902 ev->bit_depth = desc->comp[0].depth;
903 ev->num_comp = desc->nb_components;
904 ev->blocks_per_mb = 4; /* luma: 16x16 MB -> 4 8x8 blocks */
905 ev->chroma_blocks_per_mb = 4 >> (desc->log2_chroma_w + desc->log2_chroma_h);
906 ev->level_idc = 33; /* placeholder, real value depends on resolution and bitrate */
907 ev->band_idc = 0;
908
909 /* Frame dimensions in macroblocks */
910 ev->frame_mb_x = (avctx->width + APV_MB_WIDTH - 1) / APV_MB_WIDTH;
911 ev->frame_mb_y = (avctx->height + APV_MB_HEIGHT - 1) / APV_MB_HEIGHT;
912
913 /* The 20x20 tile grid cap is structural (fixed-size arrays everywhere);
914 * the spec additionally demands tiles of at least 16x8 MBs. Each
915 * tile-component maps to one entropy workgroup, one invocation per
916 * transform block. */
917 int grid_tw = (ev->frame_mb_x + APV_MAX_TILE_COLS - 1) / APV_MAX_TILE_COLS;
918 int grid_th = (ev->frame_mb_y + APV_MAX_TILE_ROWS - 1) / APV_MAX_TILE_ROWS;
919 int min_tw = FFMAX(APV_MIN_TILE_WIDTH_IN_MBS, grid_tw);
920 int min_th = FFMAX(APV_MIN_TILE_HEIGHT_IN_MBS, grid_th);
921
922 /* tile_w/tile_h pick the tile size in MBs; 0 selects the spec minimum.
923 * An explicit request below the spec minimum is honoured down to the
924 * grid cap -- non-conformant, but more tiles mean shorter (serial)
925 * entropy streams, which is the decode speed lever. */
926 ev->tile_mb_w = ev->tile_w_mbs_opt > 0 ? ev->tile_w_mbs_opt : min_tw;
927 ev->tile_mb_h = ev->tile_h_mbs_opt > 0 ? ev->tile_h_mbs_opt : min_th;
928 ev->tile_mb_w = FFMIN(FFMAX(ev->tile_mb_w, grid_tw), ev->frame_mb_x);
929 ev->tile_mb_h = FFMIN(FFMAX(ev->tile_mb_h, grid_th), ev->frame_mb_y);
932 av_log(avctx, AV_LOG_WARNING,
933 "Tile size %dx%d MBs is below the spec minimum of %dx%d: "
934 "NON-CONFORMANT bitstream, most decoders will reject it.\n",
935 ev->tile_mb_w, ev->tile_mb_h,
937
938 /* Left to default, grow the tile toward 1024 transform blocks (the
939 * entropy workgroup ceiling) while it still divides the frame. Bigger
940 * tiles mean fewer tile-components, which the compaction pass strongly
941 * prefers -- it is the dominant win for throughput. */
942 if (!ev->tile_w_mbs_opt && !ev->tile_h_mbs_opt) {
943 while (ev->tile_mb_w * 2 <= ev->frame_mb_x &&
944 ev->frame_mb_x % (ev->tile_mb_w * 2) == 0 &&
945 (ev->tile_mb_w * 2) * ev->tile_mb_h * ev->blocks_per_mb <= 1024)
946 ev->tile_mb_w *= 2;
947 while (ev->tile_mb_h * 2 <= ev->frame_mb_y &&
948 ev->frame_mb_y % (ev->tile_mb_h * 2) == 0 &&
949 ev->tile_mb_w * (ev->tile_mb_h * 2) * ev->blocks_per_mb <= 1024)
950 ev->tile_mb_h *= 2;
951 }
952
953 /* Ceil division: the rightmost column / bottom row of tiles take the
954 * remainder MBs (spec-legal; the tile grid is closed at the frame edge,
955 * so those tiles may be smaller than the signalled tile size). */
956 ev->tile_cols = (ev->frame_mb_x + ev->tile_mb_w - 1) / ev->tile_mb_w;
957 ev->tile_rows = (ev->frame_mb_y + ev->tile_mb_h - 1) / ev->tile_mb_h;
958 ev->tile_count = ev->tile_cols * ev->tile_rows;
959
960 if (ev->tile_count > APV_MAX_TILE_COUNT) {
961 av_log(avctx, AV_LOG_ERROR, "Too many tiles (%d).\n", ev->tile_count);
962 return AVERROR(EINVAL);
963 }
964
965 /* The entropy shader runs one invocation per block in a tile-component
966 * and its shared buffers are sized for 1024. */
967 if (ev->tile_mb_w * ev->tile_mb_h * ev->blocks_per_mb > 1024) {
968 av_log(avctx, AV_LOG_ERROR,
969 "Tile-component has too many transform blocks (%d > 1024).\n",
970 ev->tile_mb_w * ev->tile_mb_h * ev->blocks_per_mb);
972 }
973
974 /* qp_chroma left at 0 means "use the luma QP". */
975 if (ev->qp_c == 0)
976 ev->qp_c = ev->qp_y;
977
978 /* Validate QP range */
979 int max_qp = 3 + ev->bit_depth * 6;
980 if (ev->qp_y < 0 || ev->qp_y > max_qp || ev->qp_c < 0 || ev->qp_c > max_qp) {
981 av_log(avctx, AV_LOG_ERROR,
982 "QP out of range [0, %d]: qp_y=%d, qp_c=%d.\n",
983 max_qp, ev->qp_y, ev->qp_c);
984 return AVERROR(EINVAL);
985 }
986
987 /* Buffer sizing */
988 size_t blocks_per_tile = (size_t)ev->tile_mb_w * ev->tile_mb_h * ev->blocks_per_mb;
989 ev->coeffs_size = (size_t)ev->tile_count * ev->num_comp *
990 blocks_per_tile * APV_BLK_COEFFS * sizeof(int16_t);
991
992 /* Worst-case per-tile-component bytestream: each coefficient at most ~32 bits.
993 * Round up generously. */
994 ev->slot_size = blocks_per_tile * APV_BLK_COEFFS * 8;
995 ev->slot_size = FFALIGN(ev->slot_size, 64);
996 ev->bytestream_size = (size_t)ev->tile_count * ev->num_comp * ev->slot_size;
997 ev->sizes_size = (size_t)ev->tile_count * ev->num_comp * sizeof(uint32_t);
998
999 av_log(avctx, AV_LOG_VERBOSE,
1000 "APV Vulkan encoder: %dx%d, %d tiles (%dx%d MBs each), "
1001 "qp_y=%d qp_c=%d, coeffs=%zu KiB, bytestream=%zu KiB\n",
1002 avctx->width, avctx->height, ev->tile_count,
1003 ev->tile_mb_w, ev->tile_mb_h, ev->qp_y, ev->qp_c,
1004 ev->coeffs_size / 1024, ev->bytestream_size / 1024);
1005
1006 ev->headers_only = !!getenv("APV_VULKAN_HEADERS_ONLY");
1007 ev->skip_entropy = !!getenv("APV_VULKAN_SKIP_ENTROPY");
1008 if (ev->skip_entropy)
1009 ev->headers_only = 1; /* the bitstream is never produced */
1010 if (ev->headers_only)
1011 av_log(avctx, AV_LOG_WARNING,
1012 "APV_VULKAN_HEADERS_ONLY set: tiles will not be downloaded "
1013 "or assembled; output packets contain headers only.\n");
1014 if (ev->skip_entropy)
1015 av_log(avctx, AV_LOG_WARNING,
1016 "APV_VULKAN_SKIP_ENTROPY set: entropy dispatch skipped "
1017 "(DCT-only benchmark mode).\n");
1018
1019 /* Init Vulkan */
1020 err = ff_vk_init(&ev->s, avctx, NULL, avctx->hw_frames_ctx);
1021 if (err < 0)
1022 return err;
1023
1024 ev->qf = ff_vk_qf_find(&ev->s, VK_QUEUE_COMPUTE_BIT, 0);
1025 if (!ev->qf) {
1026 av_log(avctx, AV_LOG_ERROR, "Device has no compute queues!\n");
1027 return AVERROR(ENOTSUP);
1028 }
1029
1030 err = ff_vk_exec_pool_init(&ev->s, ev->qf, &ev->exec_pool,
1031 ev->async_depth, 0, 0, 0, NULL);
1032 if (err < 0)
1033 return err;
1034
1035 /* Init CBS for assembling output */
1036 err = ff_cbs_init(&ev->cbc, AV_CODEC_ID_APV, avctx);
1037 if (err < 0)
1038 return err;
1039
1040 /* Shaders */
1041 err = init_dct_shader(avctx);
1042 if (err < 0)
1043 return err;
1044 err = init_entropy_shader(avctx, ev->blocks_per_mb, &ev->shd_entropy[0]);
1045 if (err < 0)
1046 return err;
1048 &ev->shd_entropy[1]);
1049 if (err < 0)
1050 return err;
1051 err = init_compact_shader(avctx);
1052 if (err < 0)
1053 return err;
1054
1055 /* The DCT/quantize shader's push constants never change frame to frame;
1056 * build them once. */
1057 build_dct_push_const(avctx);
1058
1059 ev->frame = av_frame_alloc();
1060 if (!ev->frame)
1061 return AVERROR(ENOMEM);
1062
1063 /* Async data pool */
1065 ev->exec_ctx_info = av_calloc(ev->async_depth, sizeof(*ev->exec_ctx_info));
1066 if (!ev->exec_ctx_info)
1067 return AVERROR(ENOMEM);
1068 for (int i = 0; i < ev->async_depth; i++)
1070
1071 return 0;
1072}
1073
1074#define OFFSET(x) offsetof(VulkanEncodeAPVContext, x)
1075#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1077 { "qp", "Quantization parameter (luma)", OFFSET(qp_y),
1078 AV_OPT_TYPE_INT, { .i64 = 22 }, 0, 255, VE },
1079 { "qp_chroma", "Chroma quantization parameter (0 = same as luma qp)", OFFSET(qp_c),
1080 AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 255, VE },
1081 { "qmatrix", "Quantization matrix", OFFSET(qmatrix),
1082 AV_OPT_TYPE_INT, { .i64 = APV_QMATRIX_HEVC }, 0, 1, VE, "qmatrix" },
1083 { "flat", "Uniform matrix, all 16 (APV spec default)", 0,
1084 AV_OPT_TYPE_CONST, { .i64 = APV_QMATRIX_FLAT }, 0, 0, VE, "qmatrix" },
1085 { "hevc", "HEVC default intra scaling list (mild perceptual shaping)", 0,
1086 AV_OPT_TYPE_CONST, { .i64 = APV_QMATRIX_HEVC }, 0, 0, VE, "qmatrix" },
1087 /* The minimum legal tile is 16x8 MBs; the maxima are this encoder's
1088 * ceiling of 1024 transform blocks per tile-component (256 MBs): with
1089 * the other dimension at its minimum, width <= 32 and height <= 16. A
1090 * value of 0 is the sentinel for the adaptive per-frame default. */
1091 { "tile_width", "Tile width in macroblocks (0 = adaptive, auto-sized per frame)", OFFSET(tile_w_mbs_opt),
1092 AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 32, VE },
1093 { "tile_height", "Tile height in macroblocks (0 = adaptive, auto-sized per frame)", OFFSET(tile_h_mbs_opt),
1094 AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 16, VE },
1095 { "async_depth", "Internal parallelization depth", OFFSET(async_depth),
1096 AV_OPT_TYPE_INT, { .i64 = 1 }, 1, INT_MAX, VE },
1097 { NULL }
1098};
1099
1101 { "g", "1" },
1102 { NULL },
1103};
1104
1106 .class_name = "apv_vulkan",
1107 .item_name = av_default_item_name,
1108 .option = vulkan_encode_apv_options,
1109 .version = LIBAVUTIL_VERSION_INT,
1110};
1111
1113 HW_CONFIG_ENCODER_FRAMES(VULKAN, VULKAN),
1114 NULL,
1115};
1116
1118 .p.name = "apv_vulkan",
1119 CODEC_LONG_NAME("Advanced Professional Video (Vulkan)"),
1120 .p.type = AVMEDIA_TYPE_VIDEO,
1121 .p.id = AV_CODEC_ID_APV,
1122 .priv_data_size = sizeof(VulkanEncodeAPVContext),
1125 .close = &vulkan_encode_apv_close,
1126 .p.priv_class = &vulkan_encode_apv_class,
1127 .p.capabilities = AV_CODEC_CAP_DELAY |
1133 .defaults = vulkan_encode_apv_defaults,
1135 .hw_configs = vulkan_encode_apv_hw_configs,
1136 .p.wrapper_name = "vulkan",
1137};
static double fact(double i)
Definition af_aiir.c:935
const FFCodec ff_apv_vulkan_encoder
#define VE
Definition amfenc_av1.c:30
const unsigned int ff_apv_encode_dct_comp_spv_len
static int build_packet(AVCodecContext *avctx, FFVkExecContext *exec, AVPacket *pkt)
const unsigned char ff_apv_encode_tiles_comp_spv_data[]
static int vulkan_encode_apv_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
static const uint8_t apv_qmat_hevc_intra[64]
static av_cold int vulkan_encode_apv_init(AVCodecContext *avctx)
static const AVCodecHWConfigInternal *const vulkan_encode_apv_hw_configs[]
const unsigned char ff_apv_encode_dct_comp_spv_data[]
static const uint8_t apv_level_scale[6]
static int init_dct_shader(AVCodecContext *avctx)
static int init_entropy_shader(AVCodecContext *avctx, int blocks_per_mb, FFVulkanShader *shd)
@ APV_QMATRIX_FLAT
@ APV_QMATRIX_HEVC
static const AVClass vulkan_encode_apv_class
#define APV_DEFAULT_QMAT
static av_cold int vulkan_encode_apv_close(AVCodecContext *avctx)
static void build_dct_push_const(AVCodecContext *avctx)
static const AVOption vulkan_encode_apv_options[]
static const FFCodecDefault vulkan_encode_apv_defaults[]
static int profile_idc_from_pix_fmt(enum AVPixelFormat sw_fmt)
#define OFFSET(x)
static int chroma_format_from_pix_fmt(enum AVPixelFormat sw_fmt)
static int submit_frame(AVCodecContext *avctx, FFVkExecContext *exec, AVFrame *frame)
static int init_compact_shader(AVCodecContext *avctx)
#define APV_MAX_NUM_COMP
const unsigned char ff_seg_gather_comp_spv_data[]
const unsigned int ff_seg_gather_comp_spv_len
static int apv_qmatrix_value(int qmatrix, int i)
const unsigned int ff_apv_encode_tiles_comp_spv_len
Libavcodec external API header.
static int FUNC tile(CodedBitstreamContext *ctx, RWContext *rw, APVRawTile *current, int tile_idx, uint32_t tile_size)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FF_CODEC_CAP_EOF_FLUSH
The encoder has AV_CODEC_CAP_DELAY set, but does not actually have delay - it only wants to be flushe...
#define CODEC_PIXFMTS(...)
#define FF_CODEC_RECEIVE_PACKET_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
Called by encoders to get the next frame for encoding.
Definition encode.c:218
#define AV_NUM_DATA_POINTERS
Definition frame.h:473
#define fail
Definition test.h:479
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_ENCODER_FLUSH
This encoder can be flushed using avcodec_flush_buffers().
Definition codec.h:154
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition codec.h:79
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_HARDWARE
Codec is backed by a hardware implementation.
Definition codec.h:133
#define AV_CODEC_FLAG_COPY_OPAQUE
Definition avcodec.h:279
@ AV_CODEC_ID_APV
Definition codec_id.h:323
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition buffer.c:139
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:496
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define HW_CONFIG_ENCODER_FRAMES(format, device_type_)
Definition hwconfig.h:100
static const int sizes[][2]
Definition img2dec.c:62
#define r
Definition input.c:42
static const int level_scale[2][6]
Definition intra.c:336
@ APV_PBU_PRIMARY_FRAME
Definition apv.h:27
@ APV_CHROMA_FORMAT_422
Definition apv.h:48
@ APV_CHROMA_FORMAT_400
Definition apv.h:47
@ APV_CHROMA_FORMAT_4444
Definition apv.h:50
@ APV_CHROMA_FORMAT_444
Definition apv.h:49
@ APV_MB_HEIGHT
Definition apv.h:41
@ APV_MB_WIDTH
Definition apv.h:40
@ APV_MIN_TILE_WIDTH_IN_MBS
Definition apv.h:73
@ APV_MAX_TILE_COUNT
Definition apv.h:77
@ APV_MAX_TILE_COLS
Definition apv.h:75
@ APV_MIN_TILE_HEIGHT_IN_MBS
Definition apv.h:74
@ APV_MAX_TILE_ROWS
Definition apv.h:76
@ APV_PROFILE_4444_12
Definition apv.h:67
@ APV_PROFILE_444_10
Definition apv.h:64
@ APV_PROFILE_400_10
Definition apv.h:68
@ APV_PROFILE_422_10
Definition apv.h:62
@ APV_PROFILE_422_12
Definition apv.h:63
@ APV_PROFILE_4444_10
Definition apv.h:66
@ APV_PROFILE_444_12
Definition apv.h:65
@ APV_BLK_COEFFS
Definition apv.h:55
common internal api header.
#define av_cold
Definition attributes.h:117
void ff_vk_shader_update_img_array(FFVulkanContext *s, FFVkExecContext *e, FFVulkanShader *shd, AVFrame *f, VkImageView *views, int set, int binding, VkImageLayout layout, VkSampler sampler)
Update a descriptor in a buffer with an image array.
Definition vulkan.c:2542
int ff_vk_shader_load(FFVulkanShader *shd, VkPipelineStageFlags stage, VkSpecializationInfo *spec, uint32_t wg_size[3], uint32_t required_subgroup_size)
Initialize a shader object.
Definition vulkan.c:2070
void ff_vk_shader_add_descriptor_set(FFVulkanContext *s, FFVulkanShader *shd, const FFVulkanDescriptorSetBinding *desc, int nb, int singular)
Add descriptor to a shader.
Definition vulkan.c:2373
void ff_vk_exec_pool_free(FFVulkanContext *s, FFVkExecPool *pool)
Definition vulkan.c:310
int ff_vk_exec_pool_init(FFVulkanContext *s, AVVulkanDeviceQueueFamily *qf, FFVkExecPool *pool, int nb_contexts, int nb_queries, VkQueryType query_type, int query_64bit, const void *query_create_pnext)
Allocates/frees an execution pool.
Definition vulkan.c:357
void ff_vk_exec_wait(FFVulkanContext *s, FFVkExecContext *e)
Definition vulkan.c:590
int ff_vk_shader_add_push_const(FFVulkanShader *shd, int offset, int size, VkShaderStageFlagBits stage)
Add/update push constants for execution.
Definition vulkan.c:1443
void ff_vk_uninit(FFVulkanContext *s)
Frees main context.
Definition vulkan.c:2638
int ff_vk_init(FFVulkanContext *s, void *log_parent, AVBufferRef *device_ref, AVBufferRef *frames_ref)
Initializes the AVClass, in case this context is not used as the main user's context.
Definition vulkan.c:2651
void ff_vk_frame_barrier(FFVulkanContext *s, FFVkExecContext *e, AVFrame *pic, VkImageMemoryBarrier2 *bar, int *nb_bar, VkPipelineStageFlags2 src_stage, VkPipelineStageFlags2 dst_stage, VkAccessFlagBits2 new_access, VkImageLayout new_layout, uint32_t new_qf)
Definition vulkan.c:2027
int ff_vk_exec_start(FFVulkanContext *s, FFVkExecContext *e)
Start/submit/wait an execution.
Definition vulkan.c:599
int ff_vk_create_imageviews(FFVulkanContext *s, FFVkExecContext *e, VkImageView views[AV_NUM_DATA_POINTERS], AVFrame *f, enum FFVkShaderRepFormat rep_fmt)
Create an imageview and add it as a dependency to an execution.
Definition vulkan.c:1958
void ff_vk_shader_free(FFVulkanContext *s, FFVulkanShader *shd)
Free a shader.
Definition vulkan.c:2614
int ff_vk_shader_register_exec(FFVulkanContext *s, FFVkExecPool *pool, FFVulkanShader *shd)
Register a shader with an exec pool.
Definition vulkan.c:2407
FFVkExecContext * ff_vk_exec_get(FFVulkanContext *s, FFVkExecPool *pool)
Retrieve an execution pool.
Definition vulkan.c:571
int ff_vk_exec_submit(FFVulkanContext *s, FFVkExecContext *e)
Definition vulkan.c:881
void ff_vk_exec_bind_shader(FFVulkanContext *s, FFVkExecContext *e, const FFVulkanShader *shd)
Bind a shader.
Definition vulkan.c:2591
void ff_vk_exec_move_dep_refstruct(FFVulkanContext *s, FFVkExecContext *e, void *obj)
Definition vulkan.c:694
int ff_vk_shader_update_desc_buffer(FFVulkanContext *s, FFVkExecContext *e, FFVulkanShader *shd, int set, int bind, int elem, FFVkBuffer *buf, VkDeviceSize offset, VkDeviceSize len, VkFormat fmt)
Update a descriptor in a buffer with a buffer.
Definition vulkan.c:2555
AVVulkanDeviceQueueFamily * ff_vk_qf_find(FFVulkanContext *s, VkQueueFlagBits dev_family, VkVideoCodecOperationFlagBitsKHR vid_ops)
Chooses an appropriate QF.
Definition vulkan.c:297
void ff_vk_exec_add_dep_refstruct(FFVulkanContext *s, FFVkExecContext *e, void *obj)
Execution dependency management.
Definition vulkan.c:687
int ff_vk_get_pooled_buffer(FFVulkanContext *ctx, AVRefStructPool **buf_pool, FFVkBuffer **buf, VkBufferUsageFlags usage, void *create_pNext, size_t size, VkMemoryPropertyFlagBits mem_props)
Initialize a pool and create AVBufferRefs containing FFVkBuffer.
Definition vulkan.c:1256
void ff_vk_exec_discard_deps(FFVulkanContext *s, FFVkExecContext *e)
Definition vulkan.c:636
int ff_vk_shader_link(FFVulkanContext *s, FFVulkanShader *shd, const char *spirv, size_t spirv_len, const char *entrypoint)
Link a shader into an executable.
Definition vulkan.c:2267
int ff_vk_exec_add_dep_frame(FFVulkanContext *s, FFVkExecContext *e, AVFrame *f, VkPipelineStageFlagBits2 wait_stage, VkPipelineStageFlagBits2 signal_stage)
Definition vulkan.c:777
void ff_vk_shader_update_push_const(FFVulkanContext *s, FFVkExecContext *e, FFVulkanShader *shd, VkShaderStageFlagBits stage, int offset, size_t size, void *src)
Update push constant in a shader.
Definition vulkan.c:2581
const char * desc
Definition libsvtav1.c:83
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define FFALIGN(x, a)
Definition macros.h:78
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
AVOptions.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3500
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition pixdesc.c:3380
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:552
#define AV_PIX_FMT_YUVA444P10
Definition pixfmt.h:598
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:550
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:546
#define AV_PIX_FMT_GRAY12
Definition pixfmt.h:526
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_VULKAN
Vulkan hardware images.
Definition pixfmt.h:379
#define AV_PIX_FMT_GRAY10
Definition pixfmt.h:525
#define AV_PIX_FMT_YUVA444P12
Definition pixfmt.h:600
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:548
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition refstruct.c:120
static void av_refstruct_pool_uninit(AVRefStructPool **poolp)
Mark the pool as being available for freeing.
Definition refstruct.h:292
APVRawTileInfo tile_info
Definition cbs_apv.h:80
uint8_t color_primaries
Definition cbs_apv.h:72
uint8_t color_description_present_flag
Definition cbs_apv.h:71
APVRawFrameInfo frame_info
Definition cbs_apv.h:68
APVRawQuantizationMatrix quantization_matrix
Definition cbs_apv.h:78
uint8_t full_range_flag
Definition cbs_apv.h:75
uint8_t matrix_coefficients
Definition cbs_apv.h:74
uint8_t transfer_characteristics
Definition cbs_apv.h:73
uint8_t use_q_matrix
Definition cbs_apv.h:77
uint8_t level_idc
Definition cbs_apv.h:45
uint32_t frame_width
Definition cbs_apv.h:48
uint8_t band_idc
Definition cbs_apv.h:46
uint8_t bit_depth_minus8
Definition cbs_apv.h:51
uint8_t profile_idc
Definition cbs_apv.h:44
uint32_t frame_height
Definition cbs_apv.h:49
uint8_t chroma_format_idc
Definition cbs_apv.h:50
uint8_t capture_time_distance
Definition cbs_apv.h:52
APVRawFrameHeader frame_header
Definition cbs_apv.h:103
APVRawTile tile[APV_MAX_TILE_COUNT]
Definition cbs_apv.h:105
APVRawPBUHeader pbu_header
Definition cbs_apv.h:102
uint32_t tile_size[APV_MAX_TILE_COUNT]
Definition cbs_apv.h:104
uint8_t pbu_type
Definition cbs_apv.h:34
uint16_t group_id
Definition cbs_apv.h:35
uint8_t q_matrix[APV_MAX_NUM_COMP][APV_TR_SIZE][APV_TR_SIZE]
Definition cbs_apv.h:57
uint32_t tile_width_in_mbs
Definition cbs_apv.h:61
uint32_t tile_height_in_mbs
Definition cbs_apv.h:62
uint8_t tile_size_present_in_fh_flag
Definition cbs_apv.h:63
A reference to a data buffer.
Definition buffer.h:82
uint8_t * data
The data buffer.
Definition buffer.h:90
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
int width
picture width / height.
Definition avcodec.h:604
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
Definition avcodec.h:1471
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This struct describes a set or pool of "hardware" frames (i.e.
Definition hwcontext.h:118
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition hwcontext.h:213
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
AVRefStructPool is an API for a thread-safe pool of objects managed via the RefStruct API.
Definition refstruct.c:183
VkDevice act_dev
Active device.
Context structure for coded bitstream operations.
Definition cbs.h:226
Coded bitstream fragment structure, combining one or more units.
Definition cbs.h:129
VkDeviceAddress compacted
VkDeviceAddress sparse
float qf[APV_MAX_NUM_COMP]
uint8_t qmat[64]
VkDeviceAddress bytestream
VkDeviceAddress address
Definition vulkan.h:99
size_t size
Definition vulkan.h:98
VkMemoryPropertyFlagBits flags
Definition vulkan.h:97
VkDeviceMemory mem
Definition vulkan.h:96
uint8_t * mapped_mem
Definition vulkan.h:103
void * opaque
Definition vulkan.h:150
int had_submission
Definition vulkan.h:131
VkCommandBuffer buf
Definition vulkan.h:139
FFVkExecContext * contexts
Definition vulkan.h:269
int pool_size
Definition vulkan.h:274
AVVulkanDeviceContext * hwctx
Definition vulkan.h:329
FFVulkanFunctions vkfn
Definition vulkan.h:294
CodedBitstreamContext * cbc
AVRefStructPool * coeffs_pool
AVRefStructPool * gathered_pool
CodedBitstreamFragment au
AVVulkanDeviceQueueFamily * qf
AVRefStructPool * sizes_pool
FFVulkanShader shd_entropy[2]
AVRefStructPool * compacted_pool
enum AVPixelFormat sw_format
VulkanEncodeAPVFrameData * exec_ctx_info
AVRefStructPool * bytestream_pool
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
#define NONE
Definition vf_drawvg.c:262
static double c[64]
#define ff_vk_buf_barrier(dst, vkb, s_stage, s_access, s_access2, d_stage, d_access, d_access2, offs, bsz)
Definition vulkan.h:562
@ FF_VK_REP_INT
Definition vulkan.h:433
#define RET(x)
Definition vulkan.h:37
#define SPEC_LIST_ADD(name, idx, val_bits, val)
Definition vulkan.h:55
#define SPEC_LIST_CREATE(name, max_length, max_size)
Definition vulkan.h:45