From c06b891c2ba423ee561ba7eedf3699cdcc980878 Mon Sep 17 00:00:00 2001 From: jojo61 Date: Fri, 31 Jan 2020 12:28:18 +0100 Subject: [PATCH] Final fix for memory leak --- codec.c | 5 ++--- drm.c | 32 ++++++++++++++++++++++++++++++++ video.c | 14 ++++++++++---- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/codec.c b/codec.c index 2af9a9b..6789b75 100644 --- a/codec.c +++ b/codec.c @@ -310,7 +310,7 @@ void CodecVideoOpen(VideoDecoder * decoder, int codec_id) deint = 2; #endif #if defined VAAPI && !defined RASPI - decoder->VideoCtx->extra_hw_frames = 8; // VIDEO_SURFACES_MAX +1 + // decoder->VideoCtx->extra_hw_frames = 8; // VIDEO_SURFACES_MAX +1 if (video_codec->capabilities & (AV_CODEC_CAP_AUTO_THREADS)) { Debug(3, "codec: auto threads enabled"); // decoder->VideoCtx->thread_count = 0; @@ -516,7 +516,6 @@ void CodecVideoDecode(VideoDecoder * decoder, const AVPacket * avpkt) *pkt = *avpkt; // use copy ret = avcodec_send_packet(video_ctx, pkt); if (ret < 0) { - Debug(4, "codec: sending video packet failed"); return; } @@ -524,7 +523,7 @@ void CodecVideoDecode(VideoDecoder * decoder, const AVPacket * avpkt) usleep(1000); ret = 0; - while (ret >= 0) { + while (ret >= 0 && CuvidTestSurfaces()) { frame = av_frame_alloc(); ret = avcodec_receive_frame(video_ctx, frame); diff --git a/drm.c b/drm.c index 9b918b0..41f2303 100644 --- a/drm.c +++ b/drm.c @@ -183,6 +183,38 @@ static int SetPropertyRequest(drmModeAtomicReqPtr ModeReq, int fd_drm, return drmModeAtomicAddProperty(ModeReq, objectID, id, value); } +static void CuvidSetVideoMode(void); +void set_video_mode(int width, int height) +{ + drmModeConnector *connector; + drmModeModeInfo *mode; + int ii; + if (height != 1080 && height != 2160) + return; + connector = drmModeGetConnector(render->fd_drm, render->connector_id); + for (ii = 0; ii < connector->count_modes; ii++) { + mode = &connector->modes[ii]; + printf("Mode %d %dx%d Rate %d\n",ii,mode->hdisplay,mode->vdisplay,mode->vrefresh); + if (width == mode->hdisplay && + height == mode->vdisplay && + mode->vrefresh == DRMRefresh && + render->mode.hdisplay != width && + render->mode.vdisplay != height && + !(mode->flags & DRM_MODE_FLAG_INTERLACE)) { + memcpy(&render->mode, mode, sizeof(drmModeModeInfo)); + VideoWindowWidth = mode->hdisplay; + VideoWindowHeight = mode->vdisplay; + eglDestroySurface (eglDisplay, eglSurface); + EglCheck(); + gbm_surface_destroy (gbm.surface); + InitBo(render->bpp); + CuvidSetVideoMode(); + Debug(3,"Set new mode %d:%d\n",mode->hdisplay,mode->vdisplay); + break; + } + } +} + static int FindDevice(VideoRender * render) { drmVersion *version; diff --git a/video.c b/video.c index e0f8d5c..f37e64c 100644 --- a/video.c +++ b/video.c @@ -2412,7 +2412,11 @@ void createTextureDst(CuvidDecoder * decoder, int anz, unsigned int size_x, unsi Debug(3, "video: create %d Textures Format %s w %d h %d \n", anz, PixFmt == AV_PIX_FMT_NV12 ? "NV12" : "P010", size_x, size_y); - + +#ifdef USE_DRM + //set_video_mode(size_x,size_y); // switch Mode here (highly experimental) +#endif + #ifdef CUVID glXMakeCurrent(XlibDisplay, VideoWindow, glxSharedContext); GlxCheck(); @@ -2780,9 +2784,11 @@ static int init_generic_hwaccel(CuvidDecoder * decoder, enum AVPixelFormat hw_fm new_fctx->width != old_fctx->width || new_fctx->height != old_fctx->height || new_fctx->initial_pool_size != old_fctx->initial_pool_size) { - Debug(3,"delete old cache"); - av_buffer_unref(&decoder->cached_hw_frames_ctx); - } + Debug(3,"delete old cache"); + if (decoder->filter_graph) + avfilter_graph_free(&decoder->filter_graph); + av_buffer_unref(&decoder->cached_hw_frames_ctx); + } } if (!decoder->cached_hw_frames_ctx) {