1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "vp9dec.h"
#include "vulkan_decode.h"
const FFVulkanDecodeDescriptor ff_vk_dec_vp9_desc = {
.codec_id = AV_CODEC_ID_VP9,
.decode_extension = FF_VK_EXT_VIDEO_DECODE_VP9,
.queue_flags = VK_QUEUE_VIDEO_DECODE_BIT_KHR,
.decode_op = VK_VIDEO_CODEC_OPERATION_DECODE_VP9_BIT_KHR,
.ext_props = {
.extensionName = VK_STD_VULKAN_VIDEO_CODEC_VP9_DECODE_EXTENSION_NAME,
.specVersion = VK_STD_VULKAN_VIDEO_CODEC_VP9_DECODE_SPEC_VERSION,
},
};
typedef struct VP9VulkanDecodePicture {
FFVulkanDecodePicture vp;
/* TODO: investigate if this can be removed to make decoding completely
* independent. */
FFVulkanDecodeContext *dec;
/* Current picture */
StdVideoVP9ColorConfig color_config;
StdVideoVP9Segmentation segmentation;
StdVideoVP9LoopFilter loop_filter;
StdVideoDecodeVP9PictureInfo std_pic_info;
VkVideoDecodeVP9PictureInfoKHR vp9_pic_info;
const VP9Frame *ref_src[8];
uint8_t frame_id_set;
uint8_t frame_id;
uint8_t ref_frame_sign_bias_mask;
} VP9VulkanDecodePicture;
static int vk_vp9_fill_pict(AVCodecContext *avctx, const VP9Frame **ref_src,
VkVideoReferenceSlotInfoKHR *ref_slot, /* Main structure */
VkVideoPictureResourceInfoKHR *ref, /* Goes in ^ */
const VP9Frame *pic, int is_current)
{
FFVulkanDecodeContext *dec = avctx->internal->hwaccel_priv_data;
FFVulkanDecodeShared *ctx = dec->shared_ctx;
VP9VulkanDecodePicture *hp = pic->hwaccel_picture_private;
FFVulkanDecodePicture *vkpic = &hp->vp;
int err = ff_vk_decode_prepare_frame(dec, pic->tf.f, vkpic, is_current,
dec->dedicated_dpb);
if (err < 0)
return err;
*ref = (VkVideoPictureResourceInfoKHR) {
.sType = VK_STRUCTURE_TYPE_VIDEO_PICTURE_RESOURCE_INFO_KHR,
.codedOffset = (VkOffset2D){ 0, 0 },
.codedExtent = (VkExtent2D){ pic->tf.f->width, pic->tf.f->height },
.baseArrayLayer = (dec->dedicated_dpb && ctx->common.layered_dpb) ?
hp->frame_id : 0,
.imageViewBinding = vkpic->view.ref[0],
};
*ref_slot = (VkVideoReferenceSlotInfoKHR) {
.sType = VK_STRUCTURE_TYPE_VIDEO_REFERENCE_SLOT_INFO_KHR,
.slotIndex = hp->frame_id,
.pPictureResource = ref,
};
if (ref_src)
*ref_src = pic;
return 0;
}
static enum StdVideoVP9InterpolationFilter remap_interp(uint8_t is_filter_switchable,
uint8_t raw_interpolation_filter_type)
{
static const enum StdVideoVP9InterpolationFilter remap[] = {
STD_VIDEO_VP9_INTERPOLATION_FILTER_EIGHTTAP_SMOOTH,
STD_VIDEO_VP9_INTERPOLATION_FILTER_EIGHTTAP,
STD_VIDEO_VP9_INTERPOLATION_FILTER_EIGHTTAP_SHARP,
STD_VIDEO_VP9_INTERPOLATION_FILTER_BILINEAR,
};
if (is_filter_switchable)
return STD_VIDEO_VP9_INTERPOLATION_FILTER_SWITCHABLE;
return remap[raw_interpolation_filter_type];
}
static int vk_vp9_start_frame(AVCodecContext *avctx,
av_unused const AVBufferRef *buffer_ref,
av_unused const uint8_t *buffer,
av_unused uint32_t size)
{
int err;
int ref_count = 0;
const VP9Context *priv = avctx->priv_data;
const CodedBitstreamVP9Context *vp9 = priv->cbc->priv_data;
const VP9SharedContext *s = &priv->s;
uint32_t frame_id_alloc_mask = 0;
const VP9Frame *pic = &s->frames[CUR_FRAME];
const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
FFVulkanDecodeContext *dec = avctx->internal->hwaccel_priv_data;
uint8_t profile = (pic->frame_header->profile_high_bit << 1) | pic->frame_header->profile_low_bit;
VP9VulkanDecodePicture *ap = pic->hwaccel_picture_private;
FFVulkanDecodePicture *vp = &ap->vp;
/* Use the current frame_ids in ref_frames[] to decide occupied frame_ids */
for (int i = 0; i < STD_VIDEO_VP9_NUM_REF_FRAMES; i++) {
const VP9VulkanDecodePicture* rp = s->ref_frames[i].hwaccel_picture_private;
if (rp)
frame_id_alloc_mask |= 1 << rp->frame_id;
}
if (!ap->frame_id_set) {
unsigned slot_idx = 0;
for (unsigned i = 0; i < 32; i++) {
if (!(frame_id_alloc_mask & (1 << i))) {
slot_idx = i;
break;
}
}
ap->frame_id = slot_idx;
ap->frame_id_set = 1;
frame_id_alloc_mask |= (1 << slot_idx);
}
for (int i = 0; i < STD_VIDEO_VP9_REFS_PER_FRAME; i++) {
const int idx = pic->frame_header->ref_frame_idx[i];
const VP9Frame *ref_frame = &s->ref_frames[idx];
VP9VulkanDecodePicture *hp = ref_frame->hwaccel_picture_private;
int found = 0;
if (!ref_frame->tf.f)
continue;
for (int j = 0; j < ref_count; j++) {
if (vp->ref_slots[j].slotIndex == hp->frame_id) {
found = 1;
break;
}
}
if (found)
continue;
err = vk_vp9_fill_pict(avctx, &ap->ref_src[ref_count],
&vp->ref_slots[ref_count], &vp->refs[ref_count],
ref_frame, 0);
if (err < 0)
return err;
ref_count++;
}
err = vk_vp9_fill_pict(avctx, NULL, &vp->ref_slot, &vp->ref,
pic, 1);
if (err < 0)
return err;
ap->loop_filter = (StdVideoVP9LoopFilter) {
.flags = (StdVideoVP9LoopFilterFlags) {
.loop_filter_delta_enabled = pic->frame_header->loop_filter_delta_enabled,
.loop_filter_delta_update = pic->frame_header->loop_filter_delta_update,
},
.loop_filter_level = pic->frame_header->loop_filter_level,
.loop_filter_sharpness = pic->frame_header->loop_filter_sharpness,
.update_ref_delta = 0x0,
.update_mode_delta = 0x0,
};
for (int i = 0; i < STD_VIDEO_VP9_MAX_REF_FRAMES; i++) {
ap->loop_filter.loop_filter_ref_deltas[i] = vp9->loop_filter_ref_deltas[i];
ap->loop_filter.update_ref_delta |= pic->frame_header->update_ref_delta[i];
}
for (int i = 0; i < STD_VIDEO_VP9_LOOP_FILTER_ADJUSTMENTS; i++) {
ap->loop_filter.loop_filter_mode_deltas[i] = vp9->loop_filter_mode_deltas[i];
ap->loop_filter.update_mode_delta |= pic->frame_header->update_mode_delta[i];
}
ap->segmentation = (StdVideoVP9Segmentation) {
.flags = (StdVideoVP9SegmentationFlags) {
.segmentation_update_map = pic->frame_header->segmentation_update_map,
.segmentation_temporal_update = pic->frame_header->segmentation_temporal_update,
.segmentation_update_data = pic->frame_header->segmentation_update_data,
.segmentation_abs_or_delta_update = pic->frame_header->segmentation_abs_or_delta_update,
},
};
for (int i = 0; i < STD_VIDEO_VP9_MAX_SEGMENTATION_TREE_PROBS; i++)
ap->segmentation.segmentation_tree_probs[i] = vp9->segmentation_tree_probs[i];
for (int i = 0; i < STD_VIDEO_VP9_MAX_SEGMENTATION_PRED_PROB; i++)
ap->segmentation.segmentation_pred_prob[i] = vp9->segmentation_pred_prob[i];
for (int i = 0; i < STD_VIDEO_VP9_MAX_SEGMENTS; i++) {
ap->segmentation.FeatureEnabled[i] = 0x0;
for (int j = 0; j < STD_VIDEO_VP9_SEG_LVL_MAX; j++) {
ap->segmentation.FeatureEnabled[i] |= vp9->feature_enabled[i][j] << j;
ap->segmentation.FeatureData[i][j] = vp9->feature_sign[i][j] ?
-vp9->feature_value[i][j] :
+vp9->feature_value[i][j];
}
}
ap->color_config = (StdVideoVP9ColorConfig) {
.flags = (StdVideoVP9ColorConfigFlags) {
.color_range = pic->frame_header->color_range,
},
.BitDepth = profile < 2 ? 8 :
pic->frame_header->ten_or_twelve_bit ? 12 : 10,
.subsampling_x = pixdesc->log2_chroma_w,
.subsampling_y = pixdesc->log2_chroma_h,
.color_space = pic->frame_header->color_space,
};
ap->std_pic_info = (StdVideoDecodeVP9PictureInfo) {
.flags = (StdVideoDecodeVP9PictureInfoFlags) {
.error_resilient_mode = pic->frame_header->error_resilient_mode,
.intra_only = pic->frame_header->intra_only,
.allow_high_precision_mv = pic->frame_header->allow_high_precision_mv,
.refresh_frame_context = pic->frame_header->refresh_frame_context,
.frame_parallel_decoding_mode = pic->frame_header->frame_parallel_decoding_mode,
.segmentation_enabled = pic->frame_header->segmentation_enabled,
.show_frame = !s->h.invisible,
.UsePrevFrameMvs = s->h.use_last_frame_mvs,
},
.profile = profile,
.frame_type = pic->frame_header->frame_type,
.frame_context_idx = pic->frame_header->frame_context_idx,
.reset_frame_context = pic->frame_header->reset_frame_context,
.refresh_frame_flags = pic->frame_header->refresh_frame_flags,
.ref_frame_sign_bias_mask = 0x0,
.interpolation_filter = remap_interp(pic->frame_header->is_filter_switchable,
pic->frame_header->raw_interpolation_filter_type),
.base_q_idx = pic->frame_header->base_q_idx,
.delta_q_y_dc = pic->frame_header->delta_q_y_dc,
.delta_q_uv_dc = pic->frame_header->delta_q_uv_dc,
.delta_q_uv_ac = pic->frame_header->delta_q_uv_ac,
.tile_cols_log2 = pic->frame_header->tile_cols_log2,
.tile_rows_log2 = pic->frame_header->tile_rows_log2,
/* Reserved */
.pColorConfig = &ap->color_config,
.pLoopFilter = &ap->loop_filter,
.pSegmentation = &ap->segmentation,
};
for (int i = VP9_LAST_FRAME; i <= VP9_ALTREF_FRAME; i++)
ap->std_pic_info.ref_frame_sign_bias_mask |= pic->frame_header->ref_frame_sign_bias[i] << i;
ap->vp9_pic_info = (VkVideoDecodeVP9PictureInfoKHR) {
.sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_VP9_PICTURE_INFO_KHR,
.pStdPictureInfo = &ap->std_pic_info,
.uncompressedHeaderOffset = 0,
.compressedHeaderOffset = s->h.uncompressed_header_size,
.tilesOffset = s->h.uncompressed_header_size +
s->h.compressed_header_size,
};
for (int i = 0; i < STD_VIDEO_VP9_REFS_PER_FRAME; i++) {
const int idx = pic->frame_header->ref_frame_idx[i];
const VP9Frame *ref_frame = &s->ref_frames[idx];
VP9VulkanDecodePicture *hp = ref_frame->hwaccel_picture_private;
if (!ref_frame->tf.f)
ap->vp9_pic_info.referenceNameSlotIndices[i] = -1;
else
ap->vp9_pic_info.referenceNameSlotIndices[i] = hp->frame_id;
}
vp->decode_info = (VkVideoDecodeInfoKHR) {
.sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_INFO_KHR,
.pNext = &ap->vp9_pic_info,
.flags = 0x0,
.pSetupReferenceSlot = &vp->ref_slot,
.referenceSlotCount = ref_count,
.pReferenceSlots = vp->ref_slots,
.dstPictureResource = (VkVideoPictureResourceInfoKHR) {
.sType = VK_STRUCTURE_TYPE_VIDEO_PICTURE_RESOURCE_INFO_KHR,
.codedOffset = (VkOffset2D){ 0, 0 },
.codedExtent = (VkExtent2D){ pic->tf.f->width, pic->tf.f->height },
.baseArrayLayer = 0,
.imageViewBinding = vp->view.out[0],
},
};
ap->dec = dec;
return 0;
}
static int vk_vp9_decode_slice(AVCodecContext *avctx,
const uint8_t *data,
uint32_t size)
{
int err;
const VP9SharedContext *s = avctx->priv_data;
VP9VulkanDecodePicture *ap = s->frames[CUR_FRAME].hwaccel_picture_private;
FFVulkanDecodePicture *vp = &ap->vp;
err = ff_vk_decode_add_slice(avctx, vp, data, size, 0, NULL, NULL);
if (err < 0)
return err;
return 0;
}
static int vk_vp9_end_frame(AVCodecContext *avctx)
{
const VP9SharedContext *s = avctx->priv_data;
const VP9Frame *pic = &s->frames[CUR_FRAME];
VP9VulkanDecodePicture *ap = pic->hwaccel_picture_private;
FFVulkanDecodePicture *vp = &ap->vp;
FFVulkanDecodePicture *rvp[STD_VIDEO_VP9_REFS_PER_FRAME] = { 0 };
AVFrame *rav[STD_VIDEO_VP9_REFS_PER_FRAME] = { 0 };
for (int i = 0; i < vp->decode_info.referenceSlotCount; i++) {
const VP9Frame *rp = ap->ref_src[i];
VP9VulkanDecodePicture *rhp = rp->hwaccel_picture_private;
rvp[i] = &rhp->vp;
rav[i] = ap->ref_src[i]->tf.f;
}
av_log(avctx, AV_LOG_VERBOSE, "Decoding frame, %zu bytes\n",
vp->slices_size);
return ff_vk_decode_frame(avctx, pic->tf.f, vp, rav, rvp);
}
static void vk_vp9_free_frame_priv(AVRefStructOpaque _hwctx, void *data)
{
AVHWDeviceContext *hwctx = _hwctx.nc;
VP9VulkanDecodePicture *ap = data;
/* Free frame resources, this also destroys the session parameters. */
ff_vk_decode_free_frame(hwctx, &ap->vp);
}
const FFHWAccel ff_vp9_vulkan_hwaccel = {
.p.name = "vp9_vulkan",
.p.type = AVMEDIA_TYPE_VIDEO,
.p.id = AV_CODEC_ID_VP9,
.p.pix_fmt = AV_PIX_FMT_VULKAN,
.start_frame = &vk_vp9_start_frame,
.decode_slice = &vk_vp9_decode_slice,
.end_frame = &vk_vp9_end_frame,
.free_frame_priv = &vk_vp9_free_frame_priv,
.frame_priv_data_size = sizeof(VP9VulkanDecodePicture),
.init = &ff_vk_decode_init,
.update_thread_context = &ff_vk_update_thread_context,
.uninit = &ff_vk_decode_uninit,
.frame_params = &ff_vk_frame_params,
.priv_data_size = sizeof(FFVulkanDecodeContext),
.caps_internal = HWACCEL_CAP_ASYNC_SAFE,
};
|