void dxva2_img_ref_decoder(struct mp_image *mpi, IDirectXVideoDecoder *decoder) { assert(mpi->imgfmt == IMGFMT_DXVA2); struct dxva2_surface *surface = (struct dxva2_surface *)mpi->planes[0]; if (surface->decoder) IDirectXVideoDecoder_Release(surface->decoder); surface->decoder = decoder; IDirectXVideoDecoder_AddRef(surface->decoder); }
static int dxva2_get_buffer(AVCodecContext *s, AVFrame *frame, int flags) { HwAccelContext *hac = s->opaque; DXVA2Context *ctx = hac->hwaccel_ctx; int i, old_unused = -1; LPDIRECT3DSURFACE9 surface; DXVA2SurfaceWrapper *w = NULL; av_assert0(frame->format == AV_PIX_FMT_DXVA2_VLD); for (i = 0; i < ctx->num_surfaces; i++) { surface_info *info = &ctx->surface_infos[i]; if (!info->used && (old_unused == -1 || info->age < ctx->surface_infos[old_unused].age)) old_unused = i; } if (old_unused == -1) { av_log(NULL, AV_LOG_ERROR, "No free DXVA2 surface!\n"); return AVERROR(ENOMEM); } i = old_unused; av_log(NULL, AV_LOG_DEBUG, "dxva2_get_buffer:%d\n",i); surface = ctx->surfaces[i]; w = av_mallocz(sizeof(*w)); if (!w) return AVERROR(ENOMEM); frame->buf[0] = av_buffer_create((uint8_t*)surface, 0, dxva2_release_buffer, w, AV_BUFFER_FLAG_READONLY); if (!frame->buf[0]) { av_free(w); return AVERROR(ENOMEM); } w->ctx = ctx; w->surface = surface; IDirect3DSurface9_AddRef(w->surface); w->decoder = ctx->decoder; IDirectXVideoDecoder_AddRef(w->decoder); ctx->surface_infos[i].used = 1; ctx->surface_infos[i].age = ctx->surface_age++; frame->data[3] = (uint8_t *)surface; return 0; }
struct mp_image *dxva2_new_ref(IDirectXVideoDecoder *decoder, LPDIRECT3DSURFACE9 d3d9_surface, int w, int h) { if (!decoder || !d3d9_surface) return NULL; struct dxva2_surface *surface = talloc_zero(NULL, struct dxva2_surface); // Add additional references to the libraries which might otherwise be freed // before the surface, which is observed to lead to bad behaviour surface->d3dlib = LoadLibrary(L"d3d9.dll"); surface->dxva2lib = LoadLibrary(L"dxva2.dll"); if (!surface->d3dlib || !surface->dxva2lib) goto fail; surface->surface = d3d9_surface; IDirect3DSurface9_AddRef(surface->surface); surface->decoder = decoder; IDirectXVideoDecoder_AddRef(surface->decoder); struct mp_image *mpi = mp_image_new_custom_ref(&(struct mp_image){0},
static struct mp_image *dxva2_new_ref(IDirectXVideoDecoder *decoder, IDirect3DSurface9 *d3d9_surface, int w, int h) { if (!decoder || !d3d9_surface) return NULL; struct dxva2_surface *surface = talloc_zero(NULL, struct dxva2_surface); surface->surface = d3d9_surface; IDirect3DSurface9_AddRef(surface->surface); surface->decoder = decoder; IDirectXVideoDecoder_AddRef(surface->decoder); struct mp_image *mpi = mp_image_new_custom_ref(NULL, surface, dxva2_release_img); if (!mpi) abort(); mp_image_setfmt(mpi, IMGFMT_DXVA2); mp_image_set_size(mpi, w, h); mpi->planes[3] = (void *)surface->surface; return mpi; }
static struct mp_image *dxva2_allocate_image(struct lavc_ctx *s, int img_w, int img_h) { DXVA2Context *ctx = s->hwdec_priv; int i, old_unused = -1; for (i = 0; i < ctx->num_surfaces; i++) { surface_info *info = &ctx->surface_infos[i]; if (!info->used && (old_unused == -1 || info->age < ctx->surface_infos[old_unused].age)) old_unused = i; } if (old_unused == -1) { MP_ERR(ctx, "No free DXVA2 surface!\n"); return NULL; } i = old_unused; DXVA2SurfaceWrapper *w = av_mallocz(sizeof(*w)); if (!w) return NULL; w->ctx = ctx; w->surface = ctx->surfaces[i];; IDirect3DSurface9_AddRef(w->surface); w->decoder = ctx->decoder; IDirectXVideoDecoder_AddRef(w->decoder); ctx->surface_infos[i].used = 1; ctx->surface_infos[i].age = ctx->surface_age++; struct mp_image mpi = {0}; mp_image_setfmt(&mpi, IMGFMT_DXVA2); mp_image_set_size(&mpi, img_w, img_h); mpi.planes[3] = (void *)w->surface; return mp_image_new_custom_ref(&mpi, w, dxva2_release_img); }