From 5c5f45ba7371e7d174d963635f3cbbbe37476a48 Mon Sep 17 00:00:00 2001 From: kingslay Date: Sat, 9 Dec 2023 23:23:28 +0800 Subject: [PATCH] add hw_device_ctx --- FFmpegKit | 2 +- .../KSPlayer/MEPlayer/AVFFmpegExtension.swift | 30 ++++++++++--------- Sources/KSPlayer/MEPlayer/Filter.swift | 7 +++++ 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/FFmpegKit b/FFmpegKit index cf3f7383f..70e2ae2e5 160000 --- a/FFmpegKit +++ b/FFmpegKit @@ -1 +1 @@ -Subproject commit cf3f7383f7e2ce9b7af8acc34b07e8388b89d1d0 +Subproject commit 70e2ae2e5f4634202ab4cb9b7ed86b85df89eedc diff --git a/Sources/KSPlayer/MEPlayer/AVFFmpegExtension.swift b/Sources/KSPlayer/MEPlayer/AVFFmpegExtension.swift index 64c904a5c..5395c46b2 100644 --- a/Sources/KSPlayer/MEPlayer/AVFFmpegExtension.swift +++ b/Sources/KSPlayer/MEPlayer/AVFFmpegExtension.swift @@ -28,20 +28,22 @@ extension UnsafeMutablePointer where Pointee == AVCodecContext { if deviceCtx == nil { break } - var framesCtx = av_hwframe_ctx_alloc(deviceCtx) - if let framesCtx { - let framesCtxData = UnsafeMutableRawPointer(framesCtx.pointee.data) - .bindMemory(to: AVHWFramesContext.self, capacity: 1) - framesCtxData.pointee.format = AV_PIX_FMT_VIDEOTOOLBOX - framesCtxData.pointee.sw_format = ctx.pointee.pix_fmt.bestPixelFormat - framesCtxData.pointee.width = ctx.pointee.width - framesCtxData.pointee.height = ctx.pointee.height - } - if av_hwframe_ctx_init(framesCtx) != 0 { - av_buffer_unref(&framesCtx) - break - } - ctx.pointee.hw_frames_ctx = framesCtx + // 只要有hw_device_ctx就可以了。不需要hw_frames_ctx + ctx.pointee.hw_device_ctx = deviceCtx +// var framesCtx = av_hwframe_ctx_alloc(deviceCtx) +// if let framesCtx { +// let framesCtxData = UnsafeMutableRawPointer(framesCtx.pointee.data) +// .bindMemory(to: AVHWFramesContext.self, capacity: 1) +// framesCtxData.pointee.format = AV_PIX_FMT_VIDEOTOOLBOX +// framesCtxData.pointee.sw_format = ctx.pointee.pix_fmt.bestPixelFormat +// framesCtxData.pointee.width = ctx.pointee.width +// framesCtxData.pointee.height = ctx.pointee.height +// } +// if av_hwframe_ctx_init(framesCtx) != 0 { +// av_buffer_unref(&framesCtx) +// break +// } +// ctx.pointee.hw_frames_ctx = framesCtx return fmt[i] } i += 1 diff --git a/Sources/KSPlayer/MEPlayer/Filter.swift b/Sources/KSPlayer/MEPlayer/Filter.swift index 07c144ee4..18ea4b17a 100644 --- a/Sources/KSPlayer/MEPlayer/Filter.swift +++ b/Sources/KSPlayer/MEPlayer/Filter.swift @@ -52,6 +52,13 @@ class MEFilter { guard ret >= 0 else { return false } ret = avfilter_link(bufferSrcContext, 0, inputs.pointee.filter_ctx, UInt32(inputs.pointee.pad_idx)) guard ret >= 0 else { return false } + if let ctx = params.hw_frames_ctx { + let framesCtxData = UnsafeMutableRawPointer(ctx.pointee.data).bindMemory(to: AVHWFramesContext.self, capacity: 1) + inputs.pointee.filter_ctx.pointee.hw_device_ctx = framesCtxData.pointee.device_ref +// outputs.pointee.filter_ctx.pointee.hw_device_ctx = framesCtxData.pointee.device_ref +// bufferSrcContext?.pointee.hw_device_ctx = framesCtxData.pointee.device_ref +// bufferSinkContext?.pointee.hw_device_ctx = framesCtxData.pointee.device_ref + } ret = avfilter_graph_config(graph, nil) guard ret >= 0 else { return false } return true