static HRESULT d3d_query_init(struct d3d_query *query, struct d3d_device *device, const D3D11_QUERY_DESC *desc, BOOL predicate) { HRESULT hr; static const enum wined3d_query_type query_type_map[] = { /* D3D11_QUERY_EVENT */ WINED3D_QUERY_TYPE_EVENT, /* D3D11_QUERY_OCCLUSION */ WINED3D_QUERY_TYPE_OCCLUSION, /* D3D11_QUERY_TIMESTAMP */ WINED3D_QUERY_TYPE_TIMESTAMP, /* D3D11_QUERY_TIMESTAMP_DISJOINT */ WINED3D_QUERY_TYPE_TIMESTAMP_DISJOINT, /* D3D11_QUERY_PIPELINE_STATISTICS */ WINED3D_QUERY_TYPE_PIPELINE_STATISTICS, /* D3D11_QUERY_OCCLUSION_PREDICATE */ WINED3D_QUERY_TYPE_OCCLUSION, /* D3D11_QUERY_SO_STATISTICS */ WINED3D_QUERY_TYPE_SO_STATISTICS, /* D3D11_QUERY_SO_OVERFLOW_PREDICATE */ WINED3D_QUERY_TYPE_SO_OVERFLOW, /* D3D11_QUERY_SO_STATISTICS_STREAM0 */ WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM0, /* D3D11_QUERY_SO_OVERFLOW_PREDICATE_STREAM0 */ WINED3D_QUERY_TYPE_SO_OVERFLOW_STREAM0, /* D3D11_QUERY_SO_STATISTICS_STREAM1 */ WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM1, /* D3D11_QUERY_SO_OVERFLOW_PREDICATE_STREAM1 */ WINED3D_QUERY_TYPE_SO_OVERFLOW_STREAM1, /* D3D11_QUERY_SO_STATISTICS_STREAM2 */ WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM2, /* D3D11_QUERY_SO_OVERFLOW_PREDICATE_STREAM2 */ WINED3D_QUERY_TYPE_SO_OVERFLOW_STREAM2, /* D3D11_QUERY_SO_STATISTICS_STREAM3 */ WINED3D_QUERY_TYPE_SO_STATISTICS_STREAM3, /* D3D11_QUERY_SO_OVERFLOW_PREDICATE_STREAM3 */ WINED3D_QUERY_TYPE_SO_OVERFLOW_STREAM3, }; if (desc->Query >= ARRAY_SIZE(query_type_map)) { FIXME("Unhandled query type %#x.\n", desc->Query); return E_INVALIDARG; } if (desc->MiscFlags) FIXME("Ignoring MiscFlags %#x.\n", desc->MiscFlags); query->ID3D11Query_iface.lpVtbl = &d3d11_query_vtbl; query->ID3D10Query_iface.lpVtbl = &d3d10_query_vtbl; query->refcount = 1; query->desc = *desc; wined3d_mutex_lock(); wined3d_private_store_init(&query->private_store); if (FAILED(hr = wined3d_query_create(device->wined3d_device, query_type_map[desc->Query], query, &d3d_query_wined3d_parent_ops, &query->wined3d_query))) { WARN("Failed to create wined3d query, hr %#x.\n", hr); wined3d_private_store_cleanup(&query->private_store); wined3d_mutex_unlock(); return hr; } wined3d_mutex_unlock(); query->predicate = predicate; query->device = &device->ID3D11Device_iface; ID3D11Device_AddRef(query->device); return S_OK; }
static void STDMETHODCALLTYPE d3d11_buffer_GetDevice(ID3D11Buffer *iface, ID3D11Device **device) { struct d3d_buffer *buffer = impl_from_ID3D11Buffer(iface); TRACE("iface %p, device %p.\n", iface, device); *device = (ID3D11Device *)buffer->device; ID3D11Device_AddRef(*device); }
static void STDMETHODCALLTYPE d3d11_texture3d_GetDevice(ID3D11Texture3D *iface, ID3D11Device **device) { struct d3d_texture3d *texture = impl_from_ID3D11Texture3D(iface); TRACE("iface %p, device %p.\n", iface, device); *device = texture->device; ID3D11Device_AddRef(*device); }
static void STDMETHODCALLTYPE d3d11_query_GetDevice(ID3D11Query *iface, ID3D11Device **device) { struct d3d_query *query = impl_from_ID3D11Query(iface); TRACE("iface %p, device %p.\n", iface, device); *device = query->device; ID3D11Device_AddRef(*device); }
static void STDMETHODCALLTYPE d3d11_sampler_state_GetDevice(ID3D11SamplerState *iface, ID3D11Device **device) { struct d3d_sampler_state *state = impl_from_ID3D11SamplerState(iface); TRACE("iface %p, device %p.\n", iface, device); *device = state->device; ID3D11Device_AddRef(*device); }
static void STDMETHODCALLTYPE d3d11_pixel_shader_GetDevice(ID3D11PixelShader *iface, ID3D11Device **device) { struct d3d_pixel_shader *shader = impl_from_ID3D11PixelShader(iface); TRACE("iface %p, device %p.\n", iface, device); *device = shader->device; ID3D11Device_AddRef(*device); }
HRESULT d3d_sampler_state_init(struct d3d_sampler_state *state, struct d3d_device *device, const D3D11_SAMPLER_DESC *desc) { struct wined3d_sampler_desc wined3d_desc; HRESULT hr; state->ID3D11SamplerState_iface.lpVtbl = &d3d11_sampler_state_vtbl; state->ID3D10SamplerState_iface.lpVtbl = &d3d10_sampler_state_vtbl; state->refcount = 1; wined3d_mutex_lock(); wined3d_private_store_init(&state->private_store); state->desc = *desc; wined3d_desc.address_u = wined3d_texture_address_from_d3d11(desc->AddressU); wined3d_desc.address_v = wined3d_texture_address_from_d3d11(desc->AddressV); wined3d_desc.address_w = wined3d_texture_address_from_d3d11(desc->AddressW); memcpy(wined3d_desc.border_color, desc->BorderColor, sizeof(wined3d_desc.border_color)); wined3d_desc.mag_filter = wined3d_texture_filter_mag_from_d3d11(desc->Filter); wined3d_desc.min_filter = wined3d_texture_filter_min_from_d3d11(desc->Filter); wined3d_desc.mip_filter = wined3d_texture_filter_mip_from_d3d11(desc->Filter); wined3d_desc.lod_bias = desc->MipLODBias; wined3d_desc.min_lod = desc->MinLOD; wined3d_desc.max_lod = desc->MaxLOD; wined3d_desc.max_anisotropy = D3D11_DECODE_IS_ANISOTROPIC_FILTER(desc->Filter) ? desc->MaxAnisotropy : 1; wined3d_desc.compare = wined3d_texture_compare_from_d3d11(desc->Filter); wined3d_desc.comparison_func = wined3d_cmp_func_from_d3d11(desc->ComparisonFunc); wined3d_desc.srgb_decode = FALSE; if (FAILED(hr = wined3d_sampler_create(device->wined3d_device, &wined3d_desc, state, &state->wined3d_sampler))) { WARN("Failed to create wined3d sampler, hr %#x.\n", hr); wined3d_private_store_cleanup(&state->private_store); wined3d_mutex_unlock(); return hr; } if (wine_rb_put(&device->sampler_states, desc, &state->entry) == -1) { ERR("Failed to insert sampler state entry.\n"); wined3d_sampler_decref(state->wined3d_sampler); wined3d_private_store_cleanup(&state->private_store); wined3d_mutex_unlock(); return E_FAIL; } wined3d_mutex_unlock(); state->device = &device->ID3D11Device_iface; ID3D11Device_AddRef(state->device); return S_OK; }
static HRESULT d3d_geometry_shader_init(struct d3d_geometry_shader *shader, struct d3d_device *device, const void *byte_code, SIZE_T byte_code_length) { struct wined3d_shader_signature output_signature; struct wined3d_shader_signature input_signature; struct d3d_shader_info shader_info; struct wined3d_shader_desc desc; HRESULT hr; shader->ID3D11GeometryShader_iface.lpVtbl = &d3d11_geometry_shader_vtbl; shader->ID3D10GeometryShader_iface.lpVtbl = &d3d10_geometry_shader_vtbl; shader->refcount = 1; wined3d_mutex_lock(); wined3d_private_store_init(&shader->private_store); shader_info.input_signature = &input_signature; shader_info.output_signature = &output_signature; if (FAILED(hr = shader_extract_from_dxbc(byte_code, byte_code_length, &shader_info))) { ERR("Failed to extract shader, hr %#x.\n", hr); wined3d_private_store_cleanup(&shader->private_store); wined3d_mutex_unlock(); return hr; } desc.byte_code = shader_info.shader_code; desc.input_signature = &input_signature; desc.output_signature = &output_signature; desc.max_version = 4; hr = wined3d_shader_create_gs(device->wined3d_device, &desc, shader, &d3d_geometry_shader_wined3d_parent_ops, &shader->wined3d_shader); shader_free_signature(&input_signature); shader_free_signature(&output_signature); if (FAILED(hr)) { WARN("Failed to create wined3d geometry shader, hr %#x.\n", hr); wined3d_private_store_cleanup(&shader->private_store); wined3d_mutex_unlock(); return E_INVALIDARG; } wined3d_mutex_unlock(); shader->device = &device->ID3D11Device_iface; ID3D11Device_AddRef(shader->device); return S_OK; }
static ULONG STDMETHODCALLTYPE d3d11_query_AddRef(ID3D11Query *iface) { struct d3d_query *query = impl_from_ID3D11Query(iface); ULONG refcount = InterlockedIncrement(&query->refcount); TRACE("%p increasing refcount to %u.\n", query, refcount); if (refcount == 1) { ID3D11Device_AddRef(query->device); wined3d_mutex_lock(); wined3d_query_incref(query->wined3d_query); wined3d_mutex_unlock(); } return refcount; }
static ULONG STDMETHODCALLTYPE d3d11_texture2d_AddRef(ID3D11Texture2D *iface) { struct d3d_texture2d *texture = impl_from_ID3D11Texture2D(iface); ULONG refcount = InterlockedIncrement(&texture->refcount); TRACE("%p increasing refcount to %u.\n", texture, refcount); if (refcount == 1) { ID3D11Device_AddRef(texture->device); wined3d_mutex_lock(); wined3d_texture_incref(texture->wined3d_texture); wined3d_mutex_unlock(); } return refcount; }
static HRESULT d3d_texture3d_init(struct d3d_texture3d *texture, struct d3d_device *device, const D3D11_TEXTURE3D_DESC *desc, const D3D11_SUBRESOURCE_DATA *data) { struct wined3d_resource_desc wined3d_desc; unsigned int levels; HRESULT hr; texture->ID3D11Texture3D_iface.lpVtbl = &d3d11_texture3d_vtbl; texture->ID3D10Texture3D_iface.lpVtbl = &d3d10_texture3d_vtbl; texture->refcount = 1; wined3d_mutex_lock(); wined3d_private_store_init(&texture->private_store); texture->desc = *desc; wined3d_desc.resource_type = WINED3D_RTYPE_TEXTURE_3D; wined3d_desc.format = wined3dformat_from_dxgi_format(desc->Format); wined3d_desc.multisample_type = WINED3D_MULTISAMPLE_NONE; wined3d_desc.multisample_quality = 0; wined3d_desc.usage = wined3d_usage_from_d3d11(desc->BindFlags, desc->Usage); wined3d_desc.pool = WINED3D_POOL_DEFAULT; wined3d_desc.width = desc->Width; wined3d_desc.height = desc->Height; wined3d_desc.depth = desc->Depth; wined3d_desc.size = 0; levels = desc->MipLevels ? desc->MipLevels : wined3d_log2i(max(max(desc->Width, desc->Height), desc->Depth)) + 1; if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &wined3d_desc, 1, levels, 0, (struct wined3d_sub_resource_data *)data, texture, &d3d_texture3d_wined3d_parent_ops, &texture->wined3d_texture))) { WARN("Failed to create wined3d texture, hr %#x.\n", hr); wined3d_private_store_cleanup(&texture->private_store); wined3d_mutex_unlock(); if (hr == WINED3DERR_INVALIDCALL) hr = E_INVALIDARG; return hr; } wined3d_mutex_unlock(); texture->desc.MipLevels = levels; texture->device = &device->ID3D11Device_iface; ID3D11Device_AddRef(texture->device); return S_OK; }
static ULONG STDMETHODCALLTYPE d3d11_buffer_AddRef(ID3D11Buffer *iface) { struct d3d_buffer *buffer = impl_from_ID3D11Buffer(iface); ULONG refcount = InterlockedIncrement(&buffer->refcount); TRACE("%p increasing refcount to %u.\n", buffer, refcount); if (refcount == 1) { ID3D11Device_AddRef(buffer->device); wined3d_mutex_lock(); wined3d_buffer_incref(buffer->wined3d_buffer); wined3d_mutex_unlock(); } return refcount; }
static HRESULT d3d_buffer_init(struct d3d_buffer *buffer, struct d3d_device *device, const D3D11_BUFFER_DESC *desc, const D3D11_SUBRESOURCE_DATA *data) { struct wined3d_buffer_desc wined3d_desc; HRESULT hr; buffer->ID3D11Buffer_iface.lpVtbl = &d3d11_buffer_vtbl; buffer->ID3D10Buffer_iface.lpVtbl = &d3d10_buffer_vtbl; buffer->refcount = 1; wined3d_mutex_lock(); wined3d_private_store_init(&buffer->private_store); buffer->desc = *desc; wined3d_desc.byte_width = desc->ByteWidth; wined3d_desc.usage = wined3d_usage_from_d3d11(0, desc->Usage); wined3d_desc.bind_flags = desc->BindFlags; wined3d_desc.cpu_access_flags = desc->CPUAccessFlags; wined3d_desc.misc_flags = desc->MiscFlags; if (desc->StructureByteStride) FIXME("Ignoring structure byte stride %u.\n", desc->StructureByteStride); if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &wined3d_desc, (const struct wined3d_sub_resource_data *)data, buffer, &d3d10_buffer_wined3d_parent_ops, &buffer->wined3d_buffer))) { WARN("Failed to create wined3d buffer, hr %#x.\n", hr); wined3d_private_store_cleanup(&buffer->private_store); wined3d_mutex_unlock(); return hr; } wined3d_mutex_unlock(); buffer->device = &device->ID3D11Device_iface; ID3D11Device_AddRef(buffer->device); return S_OK; }
HRESULT d3d_blend_state_init(struct d3d_blend_state *state, struct d3d_device *device, const D3D11_BLEND_DESC *desc) { state->ID3D11BlendState_iface.lpVtbl = &d3d11_blend_state_vtbl; state->ID3D10BlendState1_iface.lpVtbl = &d3d10_blend_state_vtbl; state->refcount = 1; wined3d_mutex_lock(); wined3d_private_store_init(&state->private_store); state->desc = *desc; if (wine_rb_put(&device->blend_states, desc, &state->entry) == -1) { ERR("Failed to insert blend state entry.\n"); wined3d_private_store_cleanup(&state->private_store); wined3d_mutex_unlock(); return E_FAIL; } wined3d_mutex_unlock(); state->device = &device->ID3D11Device_iface; ID3D11Device_AddRef(state->device); return S_OK; }
HRESULT d3d_depthstencil_state_init(struct d3d_depthstencil_state *state, struct d3d_device *device, const D3D11_DEPTH_STENCIL_DESC *desc) { state->ID3D11DepthStencilState_iface.lpVtbl = &d3d11_depthstencil_state_vtbl; state->ID3D10DepthStencilState_iface.lpVtbl = &d3d10_depthstencil_state_vtbl; state->refcount = 1; wined3d_mutex_lock(); wined3d_private_store_init(&state->private_store); state->desc = *desc; if (wine_rb_put(&device->depthstencil_states, desc, &state->entry) == -1) { ERR("Failed to insert depthstencil state entry.\n"); wined3d_private_store_cleanup(&state->private_store); wined3d_mutex_unlock(); return E_FAIL; } wined3d_mutex_unlock(); state->device = &device->ID3D11Device_iface; ID3D11Device_AddRef(state->device); return S_OK; }
HRESULT d3d_rasterizer_state_init(struct d3d_rasterizer_state *state, struct d3d_device *device, const D3D11_RASTERIZER_DESC *desc) { state->ID3D11RasterizerState_iface.lpVtbl = &d3d11_rasterizer_state_vtbl; state->ID3D10RasterizerState_iface.lpVtbl = &d3d10_rasterizer_state_vtbl; state->refcount = 1; wined3d_mutex_lock(); wined3d_private_store_init(&state->private_store); state->desc = *desc; if (wine_rb_put(&device->rasterizer_states, desc, &state->entry) == -1) { ERR("Failed to insert rasterizer state entry.\n"); wined3d_private_store_cleanup(&state->private_store); wined3d_mutex_unlock(); return E_FAIL; } wined3d_mutex_unlock(); state->device = &device->ID3D11Device_iface; ID3D11Device_AddRef(state->device); return S_OK; }
static int recreate_video_proc(struct mp_filter *vf) { struct priv *p = vf->priv; HRESULT hr; destroy_video_proc(vf); D3D11_VIDEO_PROCESSOR_CONTENT_DESC vpdesc = { .InputFrameFormat = p->d3d_frame_format, .InputWidth = p->c_w, .InputHeight = p->c_h, .OutputWidth = p->params.w, .OutputHeight = p->params.h, }; hr = ID3D11VideoDevice_CreateVideoProcessorEnumerator(p->video_dev, &vpdesc, &p->vp_enum); if (FAILED(hr)) goto fail; D3D11_VIDEO_PROCESSOR_CAPS caps; hr = ID3D11VideoProcessorEnumerator_GetVideoProcessorCaps(p->vp_enum, &caps); if (FAILED(hr)) goto fail; MP_VERBOSE(vf, "Found %d rate conversion caps. Looking for caps=0x%x.\n", (int)caps.RateConversionCapsCount, p->opts->mode); int rindex = -1; for (int n = 0; n < caps.RateConversionCapsCount; n++) { D3D11_VIDEO_PROCESSOR_RATE_CONVERSION_CAPS rcaps; hr = ID3D11VideoProcessorEnumerator_GetVideoProcessorRateConversionCaps (p->vp_enum, n, &rcaps); if (FAILED(hr)) goto fail; MP_VERBOSE(vf, " - %d: 0x%08x\n", n, (unsigned)rcaps.ProcessorCaps); if (rcaps.ProcessorCaps & p->opts->mode) { MP_VERBOSE(vf, " (matching)\n"); if (rindex < 0) rindex = n; } } if (rindex < 0) { MP_WARN(vf, "No fitting video processor found, picking #0.\n"); rindex = 0; } // TOOD: so, how do we select which rate conversion mode the processor uses? hr = ID3D11VideoDevice_CreateVideoProcessor(p->video_dev, p->vp_enum, rindex, &p->video_proc); if (FAILED(hr)) { MP_ERR(vf, "Failed to create D3D11 video processor.\n"); goto fail; } // Note: libavcodec does not support cropping left/top with hwaccel. RECT src_rc = { .right = p->params.w, .bottom = p->params.h, }; ID3D11VideoContext_VideoProcessorSetStreamSourceRect(p->video_ctx, p->video_proc, 0, TRUE, &src_rc); // This is supposed to stop drivers from f*****g up the video quality. ID3D11VideoContext_VideoProcessorSetStreamAutoProcessingMode(p->video_ctx, p->video_proc, 0, FALSE); ID3D11VideoContext_VideoProcessorSetStreamOutputRate(p->video_ctx, p->video_proc, 0, D3D11_VIDEO_PROCESSOR_OUTPUT_RATE_NORMAL, FALSE, 0); D3D11_VIDEO_PROCESSOR_COLOR_SPACE csp = { .YCbCr_Matrix = p->params.color.space != MP_CSP_BT_601, .Nominal_Range = p->params.color.levels == MP_CSP_LEVELS_TV ? 1 : 2, }; ID3D11VideoContext_VideoProcessorSetStreamColorSpace(p->video_ctx, p->video_proc, 0, &csp); if (p->out_rgb) { if (p->params.color.space != MP_CSP_BT_601 && p->params.color.space != MP_CSP_BT_709) { MP_WARN(vf, "Unsupported video colorspace (%s/%s). Consider " "disabling hardware decoding, or using " "--hwdec=d3d11va-copy to get correct output.\n", m_opt_choice_str(mp_csp_names, p->params.color.space), m_opt_choice_str(mp_csp_levels_names, p->params.color.levels)); } } else { ID3D11VideoContext_VideoProcessorSetOutputColorSpace(p->video_ctx, p->video_proc, &csp); } return 0; fail: destroy_video_proc(vf); return -1; } static struct mp_image *render(struct mp_filter *vf) { struct priv *p = vf->priv; int res = -1; HRESULT hr; ID3D11VideoProcessorInputView *in_view = NULL; ID3D11VideoProcessorOutputView *out_view = NULL; struct mp_image *in = NULL, *out = NULL; out = mp_image_pool_get(p->pool, IMGFMT_D3D11, p->params.w, p->params.h); if (!out) { MP_WARN(vf, "failed to allocate frame\n"); goto cleanup; } ID3D11Texture2D *d3d_out_tex = (void *)out->planes[0]; in = mp_refqueue_get(p->queue, 0); if (!in) goto cleanup; ID3D11Texture2D *d3d_tex = (void *)in->planes[0]; int d3d_subindex = (intptr_t)in->planes[1]; mp_image_copy_attributes(out, in); D3D11_VIDEO_FRAME_FORMAT d3d_frame_format; if (!mp_refqueue_should_deint(p->queue)) { d3d_frame_format = D3D11_VIDEO_FRAME_FORMAT_PROGRESSIVE; } else if (mp_refqueue_top_field_first(p->queue)) { d3d_frame_format = D3D11_VIDEO_FRAME_FORMAT_INTERLACED_TOP_FIELD_FIRST; } else { d3d_frame_format = D3D11_VIDEO_FRAME_FORMAT_INTERLACED_BOTTOM_FIELD_FIRST; } D3D11_TEXTURE2D_DESC texdesc; ID3D11Texture2D_GetDesc(d3d_tex, &texdesc); if (!p->video_proc || p->c_w != texdesc.Width || p->c_h != texdesc.Height || p->d3d_frame_format != d3d_frame_format) { p->c_w = texdesc.Width; p->c_h = texdesc.Height; p->d3d_frame_format = d3d_frame_format; if (recreate_video_proc(vf) < 0) goto cleanup; } if (!mp_refqueue_should_deint(p->queue)) { d3d_frame_format = D3D11_VIDEO_FRAME_FORMAT_PROGRESSIVE; } else if (mp_refqueue_is_top_field(p->queue)) { d3d_frame_format = D3D11_VIDEO_FRAME_FORMAT_INTERLACED_TOP_FIELD_FIRST; } else { d3d_frame_format = D3D11_VIDEO_FRAME_FORMAT_INTERLACED_BOTTOM_FIELD_FIRST; } ID3D11VideoContext_VideoProcessorSetStreamFrameFormat(p->video_ctx, p->video_proc, 0, d3d_frame_format); D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC indesc = { .ViewDimension = D3D11_VPIV_DIMENSION_TEXTURE2D, .Texture2D = { .ArraySlice = d3d_subindex, }, }; hr = ID3D11VideoDevice_CreateVideoProcessorInputView(p->video_dev, (ID3D11Resource *)d3d_tex, p->vp_enum, &indesc, &in_view); if (FAILED(hr)) { MP_ERR(vf, "Could not create ID3D11VideoProcessorInputView\n"); goto cleanup; } D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC outdesc = { .ViewDimension = D3D11_VPOV_DIMENSION_TEXTURE2D, }; hr = ID3D11VideoDevice_CreateVideoProcessorOutputView(p->video_dev, (ID3D11Resource *)d3d_out_tex, p->vp_enum, &outdesc, &out_view); if (FAILED(hr)) { MP_ERR(vf, "Could not create ID3D11VideoProcessorOutputView\n"); goto cleanup; } D3D11_VIDEO_PROCESSOR_STREAM stream = { .Enable = TRUE, .pInputSurface = in_view, }; int frame = mp_refqueue_is_second_field(p->queue); hr = ID3D11VideoContext_VideoProcessorBlt(p->video_ctx, p->video_proc, out_view, frame, 1, &stream); if (FAILED(hr)) { MP_ERR(vf, "VideoProcessorBlt failed.\n"); goto cleanup; } res = 0; cleanup: if (in_view) ID3D11VideoProcessorInputView_Release(in_view); if (out_view) ID3D11VideoProcessorOutputView_Release(out_view); if (res < 0) TA_FREEP(&out); return out; } static bool vo_supports(struct priv *p, int subfmt) { for (int n = 0; p->vo_formats && p->vo_formats[n]; n++) { if (p->vo_formats[n] == subfmt) return true; } return false; } static void vf_d3d11vpp_process(struct mp_filter *vf) { struct priv *p = vf->priv; struct mp_image *in_fmt = mp_refqueue_execute_reinit(p->queue); if (in_fmt) { mp_image_pool_clear(p->pool); destroy_video_proc(vf); p->params = in_fmt->params; p->out_params = p->params; if (vo_supports(p, IMGFMT_NV12)) { p->out_params.hw_subfmt = IMGFMT_NV12; p->out_format = DXGI_FORMAT_NV12; p->out_shared = false; p->out_rgb = false; } else { p->out_params.hw_subfmt = IMGFMT_RGB0; p->out_format = DXGI_FORMAT_B8G8R8A8_UNORM; p->out_shared = true; p->out_rgb = true; } p->out_params.hw_flags = 0; p->require_filtering = p->params.hw_subfmt != p->out_params.hw_subfmt; } if (!mp_refqueue_can_output(p->queue)) return; if (!mp_refqueue_should_deint(p->queue) && !p->require_filtering) { // no filtering struct mp_image *in = mp_image_new_ref(mp_refqueue_get(p->queue, 0)); if (!in) { mp_filter_internal_mark_failed(vf); return; } mp_refqueue_write_out_pin(p->queue, in); } else { mp_refqueue_write_out_pin(p->queue, render(vf)); } } static void uninit(struct mp_filter *vf) { struct priv *p = vf->priv; destroy_video_proc(vf); flush_frames(vf); talloc_free(p->queue); talloc_free(p->pool); if (p->video_ctx) ID3D11VideoContext_Release(p->video_ctx); if (p->video_dev) ID3D11VideoDevice_Release(p->video_dev); if (p->device_ctx) ID3D11DeviceContext_Release(p->device_ctx); if (p->vo_dev) ID3D11Device_Release(p->vo_dev); } static const struct mp_filter_info vf_d3d11vpp_filter = { .name = "d3d11vpp", .process = vf_d3d11vpp_process, .reset = flush_frames, .destroy = uninit, .priv_size = sizeof(struct priv), }; static struct mp_filter *vf_d3d11vpp_create(struct mp_filter *parent, void *options) { struct mp_filter *f = mp_filter_create(parent, &vf_d3d11vpp_filter); if (!f) { talloc_free(options); return NULL; } mp_filter_add_pin(f, MP_PIN_IN, "in"); mp_filter_add_pin(f, MP_PIN_OUT, "out"); struct priv *p = f->priv; p->opts = talloc_steal(p, options); // Special path for vf_d3d11_create_outconv(): disable all processing except // possibly surface format conversions. if (!p->opts) { static const struct opts opts = {0}; p->opts = (struct opts *)&opts; } p->queue = mp_refqueue_alloc(f); struct mp_stream_info *info = mp_filter_find_stream_info(f); if (!info || !info->hwdec_devs) goto fail; hwdec_devices_request_all(info->hwdec_devs); struct mp_hwdec_ctx *hwctx = hwdec_devices_get_by_lavc(info->hwdec_devs, AV_HWDEVICE_TYPE_D3D11VA); if (!hwctx || !hwctx->av_device_ref) goto fail; AVHWDeviceContext *avhwctx = (void *)hwctx->av_device_ref->data; AVD3D11VADeviceContext *d3dctx = avhwctx->hwctx; p->vo_dev = d3dctx->device; ID3D11Device_AddRef(p->vo_dev); p->vo_formats = hwctx->supported_formats; HRESULT hr; hr = ID3D11Device_QueryInterface(p->vo_dev, &IID_ID3D11VideoDevice, (void **)&p->video_dev); if (FAILED(hr)) goto fail; ID3D11Device_GetImmediateContext(p->vo_dev, &p->device_ctx); if (!p->device_ctx) goto fail; hr = ID3D11DeviceContext_QueryInterface(p->device_ctx, &IID_ID3D11VideoContext, (void **)&p->video_ctx); if (FAILED(hr)) goto fail; p->pool = mp_image_pool_new(f); mp_image_pool_set_allocator(p->pool, alloc_pool, f); mp_image_pool_set_lru(p->pool); mp_refqueue_add_in_format(p->queue, IMGFMT_D3D11, 0); mp_refqueue_set_refs(p->queue, 0, 0); mp_refqueue_set_mode(p->queue, (p->opts->deint_enabled ? MP_MODE_DEINT : 0) | MP_MODE_OUTPUT_FIELDS | (p->opts->interlaced_only ? MP_MODE_INTERLACED_ONLY : 0)); return f; fail: talloc_free(f); return NULL; } #define OPT_BASE_STRUCT struct opts static const m_option_t vf_opts_fields[] = { OPT_FLAG("deint", deint_enabled, 0), OPT_FLAG("interlaced-only", interlaced_only, 0), OPT_CHOICE("mode", mode, 0, ({"blend", D3D11_VIDEO_PROCESSOR_PROCESSOR_CAPS_DEINTERLACE_BLEND}, {"bob", D3D11_VIDEO_PROCESSOR_PROCESSOR_CAPS_DEINTERLACE_BOB}, {"adaptive", D3D11_VIDEO_PROCESSOR_PROCESSOR_CAPS_DEINTERLACE_ADAPTIVE}, {"mocomp", D3D11_VIDEO_PROCESSOR_PROCESSOR_CAPS_DEINTERLACE_MOTION_COMPENSATION}, {"ivctc", D3D11_VIDEO_PROCESSOR_PROCESSOR_CAPS_INVERSE_TELECINE},
static HRESULT d3d_texture2d_init(struct d3d_texture2d *texture, struct d3d_device *device, const D3D11_TEXTURE2D_DESC *desc, const D3D11_SUBRESOURCE_DATA *data) { struct wined3d_resource_desc wined3d_desc; unsigned int levels; HRESULT hr; texture->ID3D11Texture2D_iface.lpVtbl = &d3d11_texture2d_vtbl; texture->ID3D10Texture2D_iface.lpVtbl = &d3d10_texture2d_vtbl; texture->refcount = 1; wined3d_mutex_lock(); wined3d_private_store_init(&texture->private_store); texture->desc = *desc; if (desc->ArraySize != 1) FIXME("Array textures not implemented.\n"); if (desc->SampleDesc.Count > 1) FIXME("Multisampled textures not implemented.\n"); wined3d_desc.resource_type = WINED3D_RTYPE_TEXTURE; wined3d_desc.format = wined3dformat_from_dxgi_format(desc->Format); wined3d_desc.multisample_type = desc->SampleDesc.Count > 1 ? desc->SampleDesc.Count : WINED3D_MULTISAMPLE_NONE; wined3d_desc.multisample_quality = desc->SampleDesc.Quality; wined3d_desc.usage = wined3d_usage_from_d3d11(desc->BindFlags, desc->Usage); wined3d_desc.pool = WINED3D_POOL_DEFAULT; wined3d_desc.width = desc->Width; wined3d_desc.height = desc->Height; wined3d_desc.depth = 1; wined3d_desc.size = 0; levels = desc->MipLevels ? desc->MipLevels : wined3d_log2i(max(desc->Width, desc->Height)) + 1; if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &wined3d_desc, levels, 0, (struct wined3d_sub_resource_data *)data, texture, &d3d_texture2d_wined3d_parent_ops, &texture->wined3d_texture))) { WARN("Failed to create wined3d texture, hr %#x.\n", hr); wined3d_private_store_cleanup(&texture->private_store); wined3d_mutex_unlock(); return hr; } texture->desc.MipLevels = levels; if (desc->MipLevels == 1 && desc->ArraySize == 1) { IWineDXGIDevice *wine_device; if (FAILED(hr = ID3D10Device1_QueryInterface(&device->ID3D10Device1_iface, &IID_IWineDXGIDevice, (void **)&wine_device))) { ERR("Device should implement IWineDXGIDevice.\n"); wined3d_texture_decref(texture->wined3d_texture); wined3d_mutex_unlock(); return E_FAIL; } hr = IWineDXGIDevice_create_surface(wine_device, wined3d_texture_get_resource(texture->wined3d_texture), 0, NULL, (IUnknown *)&texture->ID3D10Texture2D_iface, (void **)&texture->dxgi_surface); IWineDXGIDevice_Release(wine_device); if (FAILED(hr)) { ERR("Failed to create DXGI surface, returning %#x\n", hr); texture->dxgi_surface = NULL; wined3d_texture_decref(texture->wined3d_texture); wined3d_mutex_unlock(); return hr; } } wined3d_mutex_unlock(); texture->device = &device->ID3D11Device_iface; ID3D11Device_AddRef(texture->device); return S_OK; }
static int recreate_video_proc(struct vf_instance *vf) { struct vf_priv_s *p = vf->priv; HRESULT hr; destroy_video_proc(vf); D3D11_VIDEO_PROCESSOR_CONTENT_DESC vpdesc = { .InputFrameFormat = p->d3d_frame_format, .InputWidth = p->c_w, .InputHeight = p->c_h, .OutputWidth = p->params.w, .OutputHeight = p->params.h, }; hr = ID3D11VideoDevice_CreateVideoProcessorEnumerator(p->video_dev, &vpdesc, &p->vp_enum); if (FAILED(hr)) goto fail; D3D11_VIDEO_PROCESSOR_CAPS caps; hr = ID3D11VideoProcessorEnumerator_GetVideoProcessorCaps(p->vp_enum, &caps); if (FAILED(hr)) goto fail; MP_VERBOSE(vf, "Found %d rate conversion caps. Looking for caps=0x%x.\n", (int)caps.RateConversionCapsCount, p->mode); int rindex = -1; for (int n = 0; n < caps.RateConversionCapsCount; n++) { D3D11_VIDEO_PROCESSOR_RATE_CONVERSION_CAPS rcaps; hr = ID3D11VideoProcessorEnumerator_GetVideoProcessorRateConversionCaps (p->vp_enum, n, &rcaps); if (FAILED(hr)) goto fail; MP_VERBOSE(vf, " - %d: 0x%08x\n", n, (unsigned)rcaps.ProcessorCaps); if (rcaps.ProcessorCaps & p->mode) { MP_VERBOSE(vf, " (matching)\n"); if (rindex < 0) rindex = n; } } if (rindex < 0) { MP_WARN(vf, "No fitting video processor found, picking #0.\n"); rindex = 0; } // TOOD: so, how do we select which rate conversion mode the processor uses? hr = ID3D11VideoDevice_CreateVideoProcessor(p->video_dev, p->vp_enum, rindex, &p->video_proc); if (FAILED(hr)) { MP_ERR(vf, "Failed to create D3D11 video processor.\n"); goto fail; } // Note: libavcodec does not support cropping left/top with hwaccel. RECT src_rc = { .right = p->params.w, .bottom = p->params.h, }; ID3D11VideoContext_VideoProcessorSetStreamSourceRect(p->video_ctx, p->video_proc, 0, TRUE, &src_rc); // This is supposed to stop drivers from f*****g up the video quality. ID3D11VideoContext_VideoProcessorSetStreamAutoProcessingMode(p->video_ctx, p->video_proc, 0, FALSE); ID3D11VideoContext_VideoProcessorSetStreamOutputRate(p->video_ctx, p->video_proc, 0, D3D11_VIDEO_PROCESSOR_OUTPUT_RATE_NORMAL, FALSE, 0); D3D11_VIDEO_PROCESSOR_COLOR_SPACE csp = { .YCbCr_Matrix = p->params.color.space != MP_CSP_BT_601, .Nominal_Range = p->params.color.levels == MP_CSP_LEVELS_TV ? 1 : 2, }; ID3D11VideoContext_VideoProcessorSetStreamColorSpace(p->video_ctx, p->video_proc, 0, &csp); if (p->out_rgb) { if (p->params.color.space != MP_CSP_BT_601 && p->params.color.space != MP_CSP_BT_709) { MP_WARN(vf, "Unsupported video colorspace (%s/%s). Consider " "disabling hardware decoding, or using " "--hwdec=d3d11va-copy to get correct output.\n", m_opt_choice_str(mp_csp_names, p->params.color.space), m_opt_choice_str(mp_csp_levels_names, p->params.color.levels)); } } else { ID3D11VideoContext_VideoProcessorSetOutputColorSpace(p->video_ctx, p->video_proc, &csp); } return 0; fail: destroy_video_proc(vf); return -1; } static int render(struct vf_instance *vf) { struct vf_priv_s *p = vf->priv; int res = -1; HRESULT hr; ID3D11VideoProcessorInputView *in_view = NULL; ID3D11VideoProcessorOutputView *out_view = NULL; struct mp_image *in = NULL, *out = NULL; out = mp_image_pool_get(p->pool, p->out_params.imgfmt, p->params.w, p->params.h); if (!out) goto cleanup; ID3D11Texture2D *d3d_out_tex = (void *)out->planes[1]; in = mp_refqueue_get(p->queue, 0); if (!in) goto cleanup; ID3D11Texture2D *d3d_tex = (void *)in->planes[1]; int d3d_subindex = (intptr_t)in->planes[2]; mp_image_copy_attributes(out, in); D3D11_VIDEO_FRAME_FORMAT d3d_frame_format; if (!mp_refqueue_should_deint(p->queue)) { d3d_frame_format = D3D11_VIDEO_FRAME_FORMAT_PROGRESSIVE; } else if (mp_refqueue_top_field_first(p->queue)) { d3d_frame_format = D3D11_VIDEO_FRAME_FORMAT_INTERLACED_TOP_FIELD_FIRST; } else { d3d_frame_format = D3D11_VIDEO_FRAME_FORMAT_INTERLACED_BOTTOM_FIELD_FIRST; } D3D11_TEXTURE2D_DESC texdesc; ID3D11Texture2D_GetDesc(d3d_tex, &texdesc); if (!p->video_proc || p->c_w != texdesc.Width || p->c_h != texdesc.Height || p->d3d_frame_format != d3d_frame_format) { p->c_w = texdesc.Width; p->c_h = texdesc.Height; p->d3d_frame_format = d3d_frame_format; if (recreate_video_proc(vf) < 0) goto cleanup; } if (!mp_refqueue_should_deint(p->queue)) { d3d_frame_format = D3D11_VIDEO_FRAME_FORMAT_PROGRESSIVE; } else if (mp_refqueue_is_top_field(p->queue)) { d3d_frame_format = D3D11_VIDEO_FRAME_FORMAT_INTERLACED_TOP_FIELD_FIRST; } else { d3d_frame_format = D3D11_VIDEO_FRAME_FORMAT_INTERLACED_BOTTOM_FIELD_FIRST; } ID3D11VideoContext_VideoProcessorSetStreamFrameFormat(p->video_ctx, p->video_proc, 0, d3d_frame_format); D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC indesc = { .ViewDimension = D3D11_VPIV_DIMENSION_TEXTURE2D, .Texture2D = { .ArraySlice = d3d_subindex, }, }; hr = ID3D11VideoDevice_CreateVideoProcessorInputView(p->video_dev, (ID3D11Resource *)d3d_tex, p->vp_enum, &indesc, &in_view); if (FAILED(hr)) { MP_ERR(vf, "Could not create ID3D11VideoProcessorInputView\n"); goto cleanup; } D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC outdesc = { .ViewDimension = D3D11_VPOV_DIMENSION_TEXTURE2D, }; hr = ID3D11VideoDevice_CreateVideoProcessorOutputView(p->video_dev, (ID3D11Resource *)d3d_out_tex, p->vp_enum, &outdesc, &out_view); if (FAILED(hr)) goto cleanup; D3D11_VIDEO_PROCESSOR_STREAM stream = { .Enable = TRUE, .pInputSurface = in_view, }; int frame = mp_refqueue_is_second_field(p->queue); hr = ID3D11VideoContext_VideoProcessorBlt(p->video_ctx, p->video_proc, out_view, frame, 1, &stream); if (FAILED(hr)) { MP_ERR(vf, "VideoProcessorBlt failed.\n"); goto cleanup; } res = 0; cleanup: if (in_view) ID3D11VideoProcessorInputView_Release(in_view); if (out_view) ID3D11VideoProcessorOutputView_Release(out_view); if (res >= 0) { vf_add_output_frame(vf, out); } else { talloc_free(out); } mp_refqueue_next_field(p->queue); return res; } static int filter_out(struct vf_instance *vf) { struct vf_priv_s *p = vf->priv; if (!mp_refqueue_has_output(p->queue)) return 0; // no filtering if (!mp_refqueue_should_deint(p->queue) && !p->require_filtering) { struct mp_image *in = mp_image_new_ref(mp_refqueue_get(p->queue, 0)); if (!in) return -1; mp_image_set_params(in, &p->out_params); vf_add_output_frame(vf, in); mp_refqueue_next(p->queue); return 0; } return render(vf); } static int reconfig(struct vf_instance *vf, struct mp_image_params *in, struct mp_image_params *out) { struct vf_priv_s *p = vf->priv; flush_frames(vf); talloc_free(p->pool); p->pool = NULL; destroy_video_proc(vf); *out = *in; if (vf_next_query_format(vf, IMGFMT_D3D11VA) || vf_next_query_format(vf, IMGFMT_D3D11NV12)) { out->imgfmt = vf_next_query_format(vf, IMGFMT_D3D11VA) ? IMGFMT_D3D11VA : IMGFMT_D3D11NV12; out->hw_subfmt = IMGFMT_NV12; p->out_format = DXGI_FORMAT_NV12; p->out_shared = false; p->out_rgb = false; } else { out->imgfmt = IMGFMT_D3D11RGB; out->hw_subfmt = IMGFMT_RGB0; p->out_format = DXGI_FORMAT_B8G8R8A8_UNORM; p->out_shared = true; p->out_rgb = true; } p->require_filtering = in->hw_subfmt != out->hw_subfmt; p->params = *in; p->out_params = *out; p->pool = mp_image_pool_new(20); mp_image_pool_set_allocator(p->pool, alloc_pool, vf); mp_image_pool_set_lru(p->pool); return 0; } static void uninit(struct vf_instance *vf) { struct vf_priv_s *p = vf->priv; destroy_video_proc(vf); flush_frames(vf); mp_refqueue_free(p->queue); talloc_free(p->pool); if (p->video_ctx) ID3D11VideoContext_Release(p->video_ctx); if (p->video_dev) ID3D11VideoDevice_Release(p->video_dev); if (p->device_ctx) ID3D11DeviceContext_Release(p->device_ctx); if (p->vo_dev) ID3D11Device_Release(p->vo_dev); } static int query_format(struct vf_instance *vf, unsigned int imgfmt) { if (imgfmt == IMGFMT_D3D11VA || imgfmt == IMGFMT_D3D11NV12 || imgfmt == IMGFMT_D3D11RGB) { return vf_next_query_format(vf, IMGFMT_D3D11VA) || vf_next_query_format(vf, IMGFMT_D3D11NV12) || vf_next_query_format(vf, IMGFMT_D3D11RGB); } return 0; } static bool test_conversion(int in, int out) { return (in == IMGFMT_D3D11VA || in == IMGFMT_D3D11NV12 || in == IMGFMT_D3D11RGB) && (out == IMGFMT_D3D11VA || out == IMGFMT_D3D11NV12 || out == IMGFMT_D3D11RGB); } static int control(struct vf_instance *vf, int request, void* data) { struct vf_priv_s *p = vf->priv; switch (request){ case VFCTRL_GET_DEINTERLACE: *(int*)data = !!p->deint_enabled; return true; case VFCTRL_SET_DEINTERLACE: p->deint_enabled = !!*(int*)data; return true; case VFCTRL_SEEK_RESET: flush_frames(vf); return true; default: return CONTROL_UNKNOWN; } } static int vf_open(vf_instance_t *vf) { struct vf_priv_s *p = vf->priv; vf->reconfig = reconfig; vf->filter_ext = filter_ext; vf->filter_out = filter_out; vf->query_format = query_format; vf->uninit = uninit; vf->control = control; p->queue = mp_refqueue_alloc(); p->vo_dev = hwdec_devices_load(vf->hwdec_devs, HWDEC_D3D11VA); if (!p->vo_dev) return 0; ID3D11Device_AddRef(p->vo_dev); HRESULT hr; hr = ID3D11Device_QueryInterface(p->vo_dev, &IID_ID3D11VideoDevice, (void **)&p->video_dev); if (FAILED(hr)) goto fail; ID3D11Device_GetImmediateContext(p->vo_dev, &p->device_ctx); if (!p->device_ctx) goto fail; hr = ID3D11DeviceContext_QueryInterface(p->device_ctx, &IID_ID3D11VideoContext, (void **)&p->video_ctx); if (FAILED(hr)) goto fail; return 1; fail: uninit(vf); return 0; } #define OPT_BASE_STRUCT struct vf_priv_s static const m_option_t vf_opts_fields[] = { OPT_FLAG("deint", deint_enabled, 0), OPT_FLAG("interlaced-only", interlaced_only, 0), OPT_CHOICE("mode", mode, 0, ({"blend", D3D11_VIDEO_PROCESSOR_PROCESSOR_CAPS_DEINTERLACE_BLEND}, {"bob", D3D11_VIDEO_PROCESSOR_PROCESSOR_CAPS_DEINTERLACE_BOB}, {"adaptive", D3D11_VIDEO_PROCESSOR_PROCESSOR_CAPS_DEINTERLACE_ADAPTIVE}, {"mocomp", D3D11_VIDEO_PROCESSOR_PROCESSOR_CAPS_DEINTERLACE_MOTION_COMPENSATION},
static int d3d11va_init_decoder(struct lavc_ctx *s, int w, int h) { HRESULT hr; int ret = -1; struct priv *p = s->hwdec_priv; TA_FREEP(&p->decoder); ID3D11Texture2D *texture = NULL; void *tmp = talloc_new(NULL); UINT n_guids = ID3D11VideoDevice_GetVideoDecoderProfileCount(p->video_dev); GUID *device_guids = talloc_array(tmp, GUID, n_guids); for (UINT i = 0; i < n_guids; i++) { GUID *guid = &device_guids[i]; hr = ID3D11VideoDevice_GetVideoDecoderProfile(p->video_dev, i, guid); if (FAILED(hr)) { MP_ERR(p, "Failed to get VideoDecoderProfile %d: %s\n", i, mp_HRESULT_to_str(hr)); goto done; } dump_decoder_info(s, guid); } struct d3d_decoder_fmt fmt = d3d_select_decoder_mode(s, device_guids, n_guids, d3d11_formats, MP_ARRAY_SIZE(d3d11_formats), d3d11_format_supported); if (!fmt.format) { MP_ERR(p, "Failed to find a suitable decoder\n"); goto done; } struct d3d11va_decoder *decoder = talloc_zero(tmp, struct d3d11va_decoder); talloc_set_destructor(decoder, d3d11va_destroy_decoder); decoder->mpfmt_decoded = fmt.format->mpfmt; int n_surfaces = hwdec_get_max_refs(s) + ADDITIONAL_SURFACES; int w_align = w, h_align = h; d3d_surface_align(s, &w_align, &h_align); D3D11_TEXTURE2D_DESC tex_desc = { .Width = w_align, .Height = h_align, .MipLevels = 1, .Format = fmt.format->dxfmt, .SampleDesc.Count = 1, .MiscFlags = 0, .ArraySize = n_surfaces, .Usage = D3D11_USAGE_DEFAULT, .BindFlags = D3D11_BIND_DECODER | D3D11_BIND_SHADER_RESOURCE, .CPUAccessFlags = 0, }; hr = ID3D11Device_CreateTexture2D(p->device, &tex_desc, NULL, &texture); if (FAILED(hr)) { MP_ERR(p, "Failed to create Direct3D11 texture with %d surfaces: %s\n", n_surfaces, mp_HRESULT_to_str(hr)); goto done; } if (s->hwdec->type == HWDEC_D3D11VA_COPY) { // create staging texture shared with the CPU with mostly the same // parameters as the above decoder-bound texture ID3D11Texture2D_GetDesc(texture, &tex_desc); tex_desc.MipLevels = 1; tex_desc.MiscFlags = 0; tex_desc.ArraySize = 1; tex_desc.Usage = D3D11_USAGE_STAGING; tex_desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; tex_desc.BindFlags = 0; hr = ID3D11Device_CreateTexture2D(p->device, &tex_desc, NULL, &decoder->staging); if (FAILED(hr)) { MP_ERR(p, "Failed to create staging texture: %s\n", mp_HRESULT_to_str(hr)); goto done; } } // pool to hold the mp_image wrapped surfaces decoder->pool = talloc_steal(decoder, mp_image_pool_new(n_surfaces)); // array of the same surfaces (needed by ffmpeg) ID3D11VideoDecoderOutputView **surfaces = talloc_array_ptrtype(decoder->pool, surfaces, n_surfaces); D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC view_desc = { .DecodeProfile = *fmt.guid, .ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D, }; for (int i = 0; i < n_surfaces; i++) { ID3D11VideoDecoderOutputView **surface = &surfaces[i]; view_desc.Texture2D.ArraySlice = i; hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( p->video_dev, (ID3D11Resource *)texture, &view_desc, surface); if (FAILED(hr)) { MP_ERR(p, "Failed getting decoder output view %d: %s\n", i, mp_HRESULT_to_str(hr)); goto done; } struct mp_image *img = d3d11va_new_ref(*surface, w, h); ID3D11VideoDecoderOutputView_Release(*surface); // transferred to img if (!img) { MP_ERR(p, "Failed to create D3D11VA image %d\n", i); goto done; } mp_image_pool_add(decoder->pool, img); // transferred to pool } D3D11_VIDEO_DECODER_DESC decoder_desc = { .Guid = *fmt.guid, .SampleWidth = w, .SampleHeight = h, .OutputFormat = fmt.format->dxfmt, }; UINT n_cfg; hr = ID3D11VideoDevice_GetVideoDecoderConfigCount(p->video_dev, &decoder_desc, &n_cfg); if (FAILED(hr)) { MP_ERR(p, "Failed to get number of decoder configurations: %s)", mp_HRESULT_to_str(hr)); goto done; } // pick the config with the highest score D3D11_VIDEO_DECODER_CONFIG *decoder_config = talloc_zero(decoder, D3D11_VIDEO_DECODER_CONFIG); unsigned max_score = 0; for (UINT i = 0; i < n_cfg; i++) { D3D11_VIDEO_DECODER_CONFIG cfg; hr = ID3D11VideoDevice_GetVideoDecoderConfig(p->video_dev, &decoder_desc, i, &cfg); if (FAILED(hr)) { MP_ERR(p, "Failed to get decoder config %d: %s\n", i, mp_HRESULT_to_str(hr)); goto done; } unsigned score = d3d_decoder_config_score( s, &cfg.guidConfigBitstreamEncryption, cfg.ConfigBitstreamRaw); if (score > max_score) { max_score = score; *decoder_config = cfg; } } if (!max_score) { MP_ERR(p, "Failed to find a suitable decoder configuration\n"); goto done; } hr = ID3D11VideoDevice_CreateVideoDecoder(p->video_dev, &decoder_desc, decoder_config, &decoder->decoder); if (FAILED(hr)) { MP_ERR(p, "Failed to create video decoder: %s\n", mp_HRESULT_to_str(hr)); goto done; } struct AVD3D11VAContext *avd3d11va_ctx = s->avctx->hwaccel_context; avd3d11va_ctx->decoder = decoder->decoder; avd3d11va_ctx->video_context = p->video_ctx; avd3d11va_ctx->cfg = decoder_config; avd3d11va_ctx->surface_count = n_surfaces; avd3d11va_ctx->surface = surfaces; avd3d11va_ctx->workaround = is_clearvideo(fmt.guid) ? FF_DXVA2_WORKAROUND_INTEL_CLEARVIDEO : 0; p->decoder = talloc_steal(NULL, decoder); ret = 0; done: // still referenced by pool images / surfaces if (texture) ID3D11Texture2D_Release(texture); talloc_free(tmp); return ret; } static void destroy_device(struct lavc_ctx *s) { struct priv *p = s->hwdec_priv; if (p->device) ID3D11Device_Release(p->device); if (p->device_ctx) ID3D11DeviceContext_Release(p->device_ctx); } static bool create_device(struct lavc_ctx *s, BOOL thread_safe) { HRESULT hr; struct priv *p = s->hwdec_priv; d3d_load_dlls(); if (!d3d11_dll) { MP_ERR(p, "Failed to load D3D11 library\n"); return false; } PFN_D3D11_CREATE_DEVICE CreateDevice = (void *)GetProcAddress(d3d11_dll, "D3D11CreateDevice"); if (!CreateDevice) { MP_ERR(p, "Failed to get D3D11CreateDevice symbol from DLL: %s\n", mp_LastError_to_str()); return false; } hr = CreateDevice(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, D3D11_CREATE_DEVICE_VIDEO_SUPPORT, NULL, 0, D3D11_SDK_VERSION, &p->device, NULL, &p->device_ctx); if (FAILED(hr)) { MP_ERR(p, "Failed to create D3D11 Device: %s\n", mp_HRESULT_to_str(hr)); return false; } ID3D10Multithread *multithread; hr = ID3D11Device_QueryInterface(p->device, &IID_ID3D10Multithread, (void **)&multithread); if (FAILED(hr)) { MP_ERR(p, "Failed to get Multithread interface: %s\n", mp_HRESULT_to_str(hr)); return false; } ID3D10Multithread_SetMultithreadProtected(multithread, thread_safe); ID3D10Multithread_Release(multithread); return true; } static void d3d11va_uninit(struct lavc_ctx *s) { struct priv *p = s->hwdec_priv; if (!p) return; talloc_free(p->decoder); av_freep(&s->avctx->hwaccel_context); if (p->video_dev) ID3D11VideoDevice_Release(p->video_dev); if (p->video_ctx) ID3D11VideoContext_Release(p->video_ctx); destroy_device(s); TA_FREEP(&s->hwdec_priv); } static int d3d11va_init(struct lavc_ctx *s) { HRESULT hr; struct priv *p = talloc_zero(NULL, struct priv); if (!p) return -1; s->hwdec_priv = p; p->log = mp_log_new(s, s->log, "d3d11va"); if (s->hwdec->type == HWDEC_D3D11VA_COPY) { mp_check_gpu_memcpy(p->log, NULL); p->sw_pool = talloc_steal(p, mp_image_pool_new(17)); } p->device = hwdec_devices_load(s->hwdec_devs, s->hwdec->type); if (p->device) { ID3D11Device_AddRef(p->device); ID3D11Device_GetImmediateContext(p->device, &p->device_ctx); if (!p->device_ctx) goto fail; MP_VERBOSE(p, "Using VO-supplied device %p.\n", p->device); } else if (s->hwdec->type == HWDEC_D3D11VA) { MP_ERR(p, "No Direct3D device provided for native d3d11 decoding\n"); goto fail; } else { if (!create_device(s, FALSE)) goto fail; } hr = ID3D11DeviceContext_QueryInterface(p->device_ctx, &IID_ID3D11VideoContext, (void **)&p->video_ctx); if (FAILED(hr)) { MP_ERR(p, "Failed to get VideoContext interface: %s\n", mp_HRESULT_to_str(hr)); goto fail; } hr = ID3D11Device_QueryInterface(p->device, &IID_ID3D11VideoDevice, (void **)&p->video_dev); if (FAILED(hr)) { MP_ERR(p, "Failed to get VideoDevice interface. %s\n", mp_HRESULT_to_str(hr)); goto fail; } s->avctx->hwaccel_context = av_d3d11va_alloc_context(); if (!s->avctx->hwaccel_context) { MP_ERR(p, "Failed to allocate hwaccel_context\n"); goto fail; } return 0; fail: d3d11va_uninit(s); return -1; } static int d3d11va_probe(struct lavc_ctx *ctx, struct vd_lavc_hwdec *hwdec, const char *codec) { // d3d11va-copy can do without external context; dxva2 requires it. if (hwdec->type != HWDEC_D3D11VA_COPY) { if (!hwdec_devices_load(ctx->hwdec_devs, HWDEC_D3D11VA)) return HWDEC_ERR_NO_CTX; } return d3d_probe_codec(codec); } const struct vd_lavc_hwdec mp_vd_lavc_d3d11va = { .type = HWDEC_D3D11VA, .image_format = IMGFMT_D3D11VA, .probe = d3d11va_probe, .init = d3d11va_init, .uninit = d3d11va_uninit, .init_decoder = d3d11va_init_decoder, .allocate_image = d3d11va_allocate_image, .process_image = d3d11va_update_image_attribs, }; const struct vd_lavc_hwdec mp_vd_lavc_d3d11va_copy = { .type = HWDEC_D3D11VA_COPY, .copying = true, .image_format = IMGFMT_D3D11VA, .probe = d3d11va_probe, .init = d3d11va_init, .uninit = d3d11va_uninit, .init_decoder = d3d11va_init_decoder, .allocate_image = d3d11va_allocate_image, .process_image = d3d11va_retrieve_image, .delay_queue = HWDEC_DELAY_QUEUE_COUNT, };
/** * It creates a Direct3D device usable for decoding */ static int D3dCreateDevice(vlc_va_t *va) { directx_sys_t *dx_sys = &va->sys->dx_sys; HRESULT hr; if (dx_sys->d3ddev && va->sys->d3dctx) { msg_Dbg(va, "Reusing Direct3D11 device"); ID3D11DeviceContext_AddRef(va->sys->d3dctx); ID3D11Device_AddRef(dx_sys->d3ddev); return VLC_SUCCESS; } /* */ PFN_D3D11_CREATE_DEVICE pf_CreateDevice; pf_CreateDevice = (void *)GetProcAddress(dx_sys->hdecoder_dll, "D3D11CreateDevice"); if (!pf_CreateDevice) { msg_Err(va, "Cannot locate reference to D3D11CreateDevice ABI in DLL"); return VLC_EGENERIC; } UINT creationFlags = D3D11_CREATE_DEVICE_VIDEO_SUPPORT; #if !defined(NDEBUG) //&& defined(_MSC_VER) creationFlags |= D3D11_CREATE_DEVICE_DEBUG; #endif /* */ ID3D11Device *d3ddev; ID3D11DeviceContext *d3dctx; hr = pf_CreateDevice(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, creationFlags, NULL, 0, D3D11_SDK_VERSION, &d3ddev, NULL, &d3dctx); if (FAILED(hr)) { msg_Err(va, "D3D11CreateDevice failed. (hr=0x%lX)", hr); return VLC_EGENERIC; } dx_sys->d3ddev = (IUnknown*) d3ddev; va->sys->d3dctx = d3dctx; ID3D11VideoContext *d3dvidctx = NULL; hr = ID3D11DeviceContext_QueryInterface(d3dctx, &IID_ID3D11VideoContext, (void **)&d3dvidctx); if (FAILED(hr)) { msg_Err(va, "Could not Query ID3D11VideoDevice Interface. (hr=0x%lX)", hr); return VLC_EGENERIC; } va->sys->d3dvidctx = d3dvidctx; #if !defined(NDEBUG) && defined(HAVE_DXGIDEBUG_H) HRESULT (WINAPI * pf_DXGIGetDebugInterface)(const GUID *riid, void **ppDebug); if (va->sys->dxgidebug_dll) { pf_DXGIGetDebugInterface = (void *)GetProcAddress(va->sys->dxgidebug_dll, "DXGIGetDebugInterface"); if (pf_DXGIGetDebugInterface) { IDXGIDebug *pDXGIDebug = NULL; hr = pf_DXGIGetDebugInterface(&IID_IDXGIDebug, (void**)&pDXGIDebug); if (SUCCEEDED(hr) && pDXGIDebug) { hr = IDXGIDebug_ReportLiveObjects(pDXGIDebug, DXGI_DEBUG_ALL, DXGI_DEBUG_RLO_ALL); } } } #endif return VLC_SUCCESS; }
static HRESULT d3d_buffer_init(struct d3d_buffer *buffer, struct d3d_device *device, const D3D11_BUFFER_DESC *desc, const D3D11_SUBRESOURCE_DATA *data) { struct wined3d_buffer_desc wined3d_desc; HRESULT hr; buffer->ID3D11Buffer_iface.lpVtbl = &d3d11_buffer_vtbl; buffer->ID3D10Buffer_iface.lpVtbl = &d3d10_buffer_vtbl; buffer->refcount = 1; wined3d_mutex_lock(); wined3d_private_store_init(&buffer->private_store); buffer->desc = *desc; if (buffer->desc.MiscFlags & D3D11_RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS) { if (buffer->desc.MiscFlags & D3D11_RESOURCE_MISC_BUFFER_STRUCTURED) { WARN("Raw and structure buffers are mutually exclusive.\n"); return E_INVALIDARG; } if (!(buffer->desc.BindFlags & (D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS))) { WARN("Invalid bind flags %#x for raw buffer.\n", buffer->desc.BindFlags); return E_INVALIDARG; } } if (buffer->desc.MiscFlags & D3D11_RESOURCE_MISC_BUFFER_STRUCTURED) { if (!buffer->desc.StructureByteStride || buffer->desc.StructureByteStride % 4) { WARN("Invalid structure byte stride %u.\n", buffer->desc.StructureByteStride); return E_INVALIDARG; } if (buffer->desc.ByteWidth % buffer->desc.StructureByteStride) { WARN("Byte width %u is not divisible by structure byte stride %u.\n", buffer->desc.ByteWidth, buffer->desc.StructureByteStride); return E_INVALIDARG; } } else { buffer->desc.StructureByteStride = 0; } wined3d_desc.byte_width = buffer->desc.ByteWidth; wined3d_desc.usage = wined3d_usage_from_d3d11(0, buffer->desc.Usage); wined3d_desc.bind_flags = buffer->desc.BindFlags; wined3d_desc.cpu_access_flags = buffer->desc.CPUAccessFlags; wined3d_desc.misc_flags = buffer->desc.MiscFlags; if (buffer->desc.StructureByteStride) FIXME("Ignoring structure byte stride %u.\n", buffer->desc.StructureByteStride); if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &wined3d_desc, (const struct wined3d_sub_resource_data *)data, buffer, &d3d_buffer_wined3d_parent_ops, &buffer->wined3d_buffer))) { WARN("Failed to create wined3d buffer, hr %#x.\n", hr); wined3d_private_store_cleanup(&buffer->private_store); wined3d_mutex_unlock(); return hr; } wined3d_mutex_unlock(); buffer->device = &device->ID3D11Device_iface; ID3D11Device_AddRef(buffer->device); return S_OK; }