Skip to content

Commit

Permalink
qsv: remove globals
Browse files Browse the repository at this point in the history
  • Loading branch information
galinart authored and sr55 committed Jul 20, 2020
1 parent 8cfd3a2 commit cf4c834
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 126 deletions.
12 changes: 6 additions & 6 deletions libhb/avfilter.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

#if HB_PROJECT_FEATURE_QSV
#include "handbrake/qsv_common.h"
extern int qsv_filters_are_enabled;
extern HBQSVFramesContext hb_dec_qsv_frames_ctx;
#endif

static int avfilter_init(hb_filter_object_t * filter, hb_filter_init_t * init);
Expand Down Expand Up @@ -242,13 +240,15 @@ static hb_buffer_t* filterFrame( hb_filter_private_t * pv, hb_buffer_t * in )
#if HB_PROJECT_FEATURE_QSV
mfxFrameSurface1 *surface = NULL;
// We need to keep surface pointer because hb_avfilter_add_buf set it to 0 after in ffmpeg call
if (qsv_filters_are_enabled && in && in->qsv_details.frame)
int use_qsv_filters = (pv->input.job && pv->input.job->qsv.ctx &&
pv->input.job->qsv.ctx->qsv_filters_are_enabled && in && in->qsv_details.frame) ? 1 : 0;
if (use_qsv_filters)
{
surface = (mfxFrameSurface1 *)in->qsv_details.frame->data[3];
}
else
{
av_frame_is_not_null= 0;
av_frame_is_not_null = 0;
}
#endif
if (av_frame_is_not_null)
Expand All @@ -262,9 +262,9 @@ static hb_buffer_t* filterFrame( hb_filter_private_t * pv, hb_buffer_t * in )
buf = hb_avfilter_get_buf(pv->graph);
}
#if HB_PROJECT_FEATURE_QSV
if (qsv_filters_are_enabled && surface)
if (use_qsv_filters && surface)
{
hb_qsv_release_surface_from_pool_by_surface_pointer(&hb_dec_qsv_frames_ctx, surface);
hb_qsv_release_surface_from_pool_by_surface_pointer(pv->input.job->qsv.ctx->hb_dec_qsv_frames_ctx, surface);
}
#endif
// Delay one frame so we can set the stop time of the output buffer
Expand Down
26 changes: 12 additions & 14 deletions libhb/cropscale.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
#include "handbrake/qsv_common.h"
#include "libavutil/hwcontext_qsv.h"
#include "libavutil/hwcontext.h"
extern int qsv_filters_are_enabled;
extern int num_cpu_filters;
HBQSVFramesContext hb_vpp_qsv_frames_ctx;
#endif

static int crop_scale_init(hb_filter_object_t * filter,
Expand Down Expand Up @@ -95,12 +92,13 @@ static int crop_scale_init(hb_filter_object_t * filter, hb_filter_init_t * init)
hb_dict_t * avsettings = hb_dict_init();

#if HB_PROJECT_FEATURE_QSV
if (qsv_filters_are_enabled)
int use_qsv_filters = (init->job && init->job->qsv.ctx && init->job->qsv.ctx->qsv_filters_are_enabled) ? 1 : 0;
if (use_qsv_filters)
{
hb_dict_set_int(avsettings, "w", width);
hb_dict_set_int(avsettings, "h", height);
hb_dict_set(avfilter, "scale_qsv", avsettings);
int result = hb_create_ffmpeg_pool(width, height, AV_PIX_FMT_NV12, HB_POOL_SURFACE_SIZE, 0, &hb_vpp_qsv_frames_ctx.hw_frames_ctx);
int result = hb_create_ffmpeg_pool(width, height, AV_PIX_FMT_NV12, HB_POOL_SURFACE_SIZE, 0, &init->job->qsv.ctx->hb_vpp_qsv_frames_ctx->hw_frames_ctx);
if (result < 0)
{
hb_error("hb_create_ffmpeg_pool vpp allocation failed");
Expand All @@ -111,19 +109,19 @@ static int crop_scale_init(hb_filter_object_t * filter, hb_filter_init_t * init)
AVQSVFramesContext *frames_hwctx;
AVBufferRef *hw_frames_ctx;

hw_frames_ctx = hb_vpp_qsv_frames_ctx.hw_frames_ctx;
hw_frames_ctx = init->job->qsv.ctx->hb_vpp_qsv_frames_ctx->hw_frames_ctx;
frames_ctx = (AVHWFramesContext*)hw_frames_ctx->data;
frames_hwctx = frames_ctx->hwctx;
hb_vpp_qsv_frames_ctx.input_texture = frames_hwctx->texture;
init->job->qsv.ctx->hb_vpp_qsv_frames_ctx->input_texture = frames_hwctx->texture;

/* allocate the memory ids for the external frames */
av_buffer_unref(&hb_vpp_qsv_frames_ctx.mids_buf);
hb_vpp_qsv_frames_ctx.mids_buf = hb_qsv_create_mids(hb_vpp_qsv_frames_ctx.hw_frames_ctx);
if (!hb_vpp_qsv_frames_ctx.mids_buf)
av_buffer_unref(&init->job->qsv.ctx->hb_vpp_qsv_frames_ctx->mids_buf);
init->job->qsv.ctx->hb_vpp_qsv_frames_ctx->mids_buf = hb_qsv_create_mids(init->job->qsv.ctx->hb_vpp_qsv_frames_ctx->hw_frames_ctx);
if (!init->job->qsv.ctx->hb_vpp_qsv_frames_ctx->mids_buf)
return AVERROR(ENOMEM);
hb_vpp_qsv_frames_ctx.mids = (QSVMid*)hb_vpp_qsv_frames_ctx.mids_buf->data;
hb_vpp_qsv_frames_ctx.nb_mids = frames_hwctx->nb_surfaces;
memset(hb_vpp_qsv_frames_ctx.pool, 0, hb_vpp_qsv_frames_ctx.nb_mids * sizeof(hb_vpp_qsv_frames_ctx.pool[0]));
init->job->qsv.ctx->hb_vpp_qsv_frames_ctx->mids = (QSVMid*)init->job->qsv.ctx->hb_vpp_qsv_frames_ctx->mids_buf->data;
init->job->qsv.ctx->hb_vpp_qsv_frames_ctx->nb_mids = frames_hwctx->nb_surfaces;
memset(init->job->qsv.ctx->hb_vpp_qsv_frames_ctx->pool, 0, init->job->qsv.ctx->hb_vpp_qsv_frames_ctx->nb_mids * sizeof(init->job->qsv.ctx->hb_vpp_qsv_frames_ctx->pool[0]));
}
else
#endif
Expand Down Expand Up @@ -171,7 +169,7 @@ static int crop_scale_init(hb_filter_object_t * filter, hb_filter_init_t * init)
avsettings = hb_dict_init();

#if HB_PROJECT_FEATURE_QSV
if (!qsv_filters_are_enabled)
if (!use_qsv_filters)
#endif
{
// TODO: Support other pix formats
Expand Down
18 changes: 14 additions & 4 deletions libhb/decavcodec.c
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ static hb_buffer_t *copy_frame( hb_work_private_t *pv )
if (pv->qsv.decode &&
pv->qsv.config.io_pattern == MFX_IOPATTERN_OUT_VIDEO_MEMORY)
{
out = hb_qsv_copy_frame(&hb_dec_qsv_frames_ctx, pv->frame, pv->job->qsv.ctx, 0);
out = hb_qsv_copy_frame(pv->job, pv->frame, 0);
}
else
#endif
Expand Down Expand Up @@ -1176,8 +1176,7 @@ int reinit_video_filters(hb_work_private_t * pv)
{
settings = hb_dict_init();
#if HB_PROJECT_FEATURE_QSV
if (pv->qsv.decode &&
pv->qsv.config.io_pattern == MFX_IOPATTERN_OUT_VIDEO_MEMORY)
if (pv->job && pv->job->qsv.ctx && pv->job->qsv.ctx->qsv_filters_are_enabled)
{
hb_dict_set(settings, "w", hb_value_int(orig_width));
hb_dict_set(settings, "h", hb_value_int(orig_height));
Expand Down Expand Up @@ -1222,6 +1221,7 @@ int reinit_video_filters(hb_work_private_t * pv)
}
}

filter_init.job = pv->job;
filter_init.pix_fmt = pv->frame->format;
filter_init.geometry.width = pv->frame->width;
filter_init.geometry.height = pv->frame->height;
Expand Down Expand Up @@ -1416,7 +1416,17 @@ static int decavcodecvInit( hb_work_object_t * w, hb_job_t * job )
hb_error( "decavcodecvInit: qsv ctx alloc failed" );
return 1;
}
hb_qsv_add_context_usage(pv->job->qsv.ctx, 0);
pv->job->qsv.ctx->hb_dec_qsv_frames_ctx = av_mallocz(sizeof(HBQSVFramesContext));
if(!pv->job->qsv.ctx->hb_dec_qsv_frames_ctx)
{
hb_error( "sanitize_qsv: HBQSVFramesContext dec alloc failed" );
return 1;
}
}
hb_qsv_add_context_usage(pv->job->qsv.ctx, 0);

if (!pv->job->qsv.ctx->dec_space)
{
pv->job->qsv.ctx->dec_space = av_mallocz(sizeof(hb_qsv_space));
if(!pv->job->qsv.ctx->dec_space)
{
Expand Down
33 changes: 15 additions & 18 deletions libhb/enc_qsv.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "libavutil/hwcontext.h"
#include <mfx/mfxvideo.h>

extern int qsv_filters_are_enabled;
extern AVBufferRef *hb_hw_device_ctx;
HBQSVFramesContext hb_dec_qsv_frames_ctx;
extern HBQSVFramesContext hb_vpp_qsv_frames_ctx;

/*
* The frame info struct remembers information about each frame across calls to
Expand Down Expand Up @@ -797,17 +794,17 @@ int qsv_enc_init(hb_work_private_t *pv)
// reuse parent session
qsv->mfx_session = parent_session;
mfxFrameAllocator frame_allocator = {
.pthis = &hb_dec_qsv_frames_ctx,
.pthis = pv->job->qsv.ctx->hb_dec_qsv_frames_ctx,
.Alloc = hb_qsv_frame_alloc,
.Lock = hb_qsv_frame_lock,
.Unlock = hb_qsv_frame_unlock,
.GetHDL = hb_qsv_frame_get_hdl,
.Free = hb_qsv_frame_free,
};

if (qsv_filters_are_enabled)
if (pv->job->qsv.ctx->qsv_filters_are_enabled)
{
frame_allocator.pthis = &hb_vpp_qsv_frames_ctx;
frame_allocator.pthis = pv->job->qsv.ctx->hb_vpp_qsv_frames_ctx;
}

err = MFXVideoCORE_SetFrameAllocator(qsv->mfx_session, &frame_allocator);
Expand Down Expand Up @@ -1771,6 +1768,8 @@ void encqsvClose(hb_work_object_t *w)
hb_qsv_unload_plugins(&pv->loaded_plugins, qsv_ctx->mfx_session, version);
}

hb_qsv_uninit_enc(pv->job);

/* QSV context cleanup and MFXClose */
hb_qsv_context_clean(qsv_ctx,hb_qsv_full_path_is_enabled(pv->job));

Expand Down Expand Up @@ -1822,8 +1821,6 @@ void encqsvClose(hb_work_object_t *w)
av_freep(&qsv_ctx);
}
}

hb_qsv_uninit_enc();
}

if (pv != NULL)
Expand Down Expand Up @@ -2162,13 +2159,13 @@ static int qsv_enc_work(hb_work_private_t *pv,
mfxFrameSurface1 *surface = task->stage->in.p_surface;
if(!pv->is_sys_mem && surface)
{
if (qsv_filters_are_enabled)
if (pv->job->qsv.ctx->qsv_filters_are_enabled)
{
hb_qsv_release_surface_from_pool(&hb_vpp_qsv_frames_ctx, surface->Data.MemId);
hb_qsv_release_surface_from_pool(pv->job->qsv.ctx->hb_vpp_qsv_frames_ctx, surface->Data.MemId);
}
else
{
hb_qsv_release_surface_from_pool(&hb_dec_qsv_frames_ctx, surface->Data.MemId);
hb_qsv_release_surface_from_pool(pv->job->qsv.ctx->hb_dec_qsv_frames_ctx, surface->Data.MemId);
}
}

Expand Down Expand Up @@ -2252,28 +2249,28 @@ int encqsvWork(hb_work_object_t *w, hb_buffer_t **buf_in, hb_buffer_t **buf_out)
if(in->qsv_details.frame)
{
surface = ((mfxFrameSurface1*)in->qsv_details.frame->data[3]);
if (qsv_filters_are_enabled)
if (pv->job->qsv.ctx->qsv_filters_are_enabled)
{
hb_qsv_get_mid_by_surface_from_pool(&hb_vpp_qsv_frames_ctx, surface, &mid);
hb_qsv_get_mid_by_surface_from_pool(pv->job->qsv.ctx->hb_vpp_qsv_frames_ctx, surface, &mid);
}
else
{
hb_qsv_get_mid_by_surface_from_pool(&hb_dec_qsv_frames_ctx, surface, &mid);
hb_qsv_get_mid_by_surface_from_pool(pv->job->qsv.ctx->hb_dec_qsv_frames_ctx, surface, &mid);
}
}
else
{
// Create black buffer in the begining of the encoding, usually first 2 frames
hb_qsv_get_free_surface_from_pool_with_range(&hb_dec_qsv_frames_ctx, HB_POOL_SURFACE_SIZE - HB_POOL_ENCODER_SIZE, HB_POOL_SURFACE_SIZE, &mid, &surface);
hb_qsv_get_free_surface_from_pool_with_range(pv->job->qsv.ctx->hb_dec_qsv_frames_ctx, HB_POOL_SURFACE_SIZE - HB_POOL_ENCODER_SIZE, HB_POOL_SURFACE_SIZE, &mid, &surface);
}

if (qsv_filters_are_enabled)
if (pv->job->qsv.ctx->qsv_filters_are_enabled)
{
hb_qsv_replace_surface_mid(&hb_vpp_qsv_frames_ctx, mid, surface);
hb_qsv_replace_surface_mid(pv->job->qsv.ctx->hb_vpp_qsv_frames_ctx, mid, surface);
}
else
{
hb_qsv_replace_surface_mid(&hb_dec_qsv_frames_ctx, mid, surface);
hb_qsv_replace_surface_mid(pv->job->qsv.ctx->hb_dec_qsv_frames_ctx, mid, surface);
}
#endif
// At this point, enc_qsv takes ownership of the QSV resources
Expand Down
12 changes: 3 additions & 9 deletions libhb/fifo.c
Original file line number Diff line number Diff line change
Expand Up @@ -715,12 +715,6 @@ void hb_buffer_swap_copy( hb_buffer_t *src, hb_buffer_t *dst )
src->alloc = alloc;
}

#if HB_PROJECT_FEATURE_QSV
extern HBQSVFramesContext hb_dec_qsv_frames_ctx;
extern HBQSVFramesContext hb_vpp_qsv_frames_ctx;
extern int qsv_filters_are_enabled;
#endif

// Frees the specified buffer list.
void hb_buffer_close( hb_buffer_t ** _b )
{
Expand All @@ -736,13 +730,13 @@ void hb_buffer_close( hb_buffer_t ** _b )
mfxFrameSurface1 *surface = (mfxFrameSurface1*)b->qsv_details.frame->data[3];
if(surface)
{
if(qsv_filters_are_enabled)
if(b->qsv_details.ctx->qsv_filters_are_enabled)
{
hb_qsv_release_surface_from_pool_by_surface_pointer(&hb_dec_qsv_frames_ctx, surface);
hb_qsv_release_surface_from_pool_by_surface_pointer(b->qsv_details.ctx->hb_dec_qsv_frames_ctx, surface);
}
else
{
hb_qsv_release_surface_from_pool(&hb_dec_qsv_frames_ctx, surface->Data.MemId);
hb_qsv_release_surface_from_pool(b->qsv_details.ctx->hb_dec_qsv_frames_ctx, surface->Data.MemId);
}
b->qsv_details.frame->data[3] = 0;
}
Expand Down
5 changes: 3 additions & 2 deletions libhb/handbrake/qsv_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,10 @@ typedef struct HBQSVFramesContext {
/* Full QSV pipeline helpers */
int hb_qsv_init(int coded_width, int coded_height, enum AVPixelFormat sw_pix_fmt, int extra_hw_frames, AVBufferRef **out_hw_frames_ctx);
int hb_create_ffmpeg_pool(int coded_width, int coded_height, enum AVPixelFormat sw_pix_fmt, int pool_size, int extra_hw_frames, AVBufferRef **out_hw_frames_ctx);
void hb_qsv_update_frames_context(hb_job_t *job);
int hb_qsv_full_path_is_enabled(hb_job_t *job);
AVBufferRef *hb_qsv_create_mids(AVBufferRef *hw_frames_ref);
hb_buffer_t* hb_qsv_copy_frame(HBQSVFramesContext* hb_qsv_frames_ctx, AVFrame *frame, hb_qsv_context *qsv_ctx, int is_vpp);
hb_buffer_t* hb_qsv_copy_frame(hb_job_t *job, AVFrame *frame, int is_vpp);
int hb_qsv_get_free_surface_from_pool(HBQSVFramesContext* hb_enc_qsv_frames_ctx, AVFrame* frame, QSVMid** out_mid);
void hb_qsv_get_free_surface_from_pool_with_range(HBQSVFramesContext* hb_enc_qsv_frames_ctx, const int start_index, const int end_index, QSVMid** out_mid, mfxFrameSurface1** out_surface);
void hb_qsv_get_mid_by_surface_from_pool(HBQSVFramesContext* hb_enc_qsv_frames_ctx, mfxFrameSurface1 *surface, QSVMid **out_mid);
Expand All @@ -280,7 +281,7 @@ int hb_qsv_get_buffer(AVCodecContext *s, AVFrame *frame, int flags);
enum AVPixelFormat hb_qsv_get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts);
int hb_qsv_preset_is_zero_copy_enabled(const hb_dict_t *job_dict);
void hb_qsv_uninit_dec(AVCodecContext *s);
void hb_qsv_uninit_enc();
void hb_qsv_uninit_enc(hb_job_t *job);

#endif // __LIBHB__
#endif // HB_PROJECT_FEATURE_QSV
Expand Down
6 changes: 6 additions & 0 deletions libhb/handbrake/qsv_libav.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ typedef struct hb_qsv_space {
mfxMemId *mids;
} hb_qsv_space;

typedef struct HBQSVFramesContext HBQSVFramesContext;

typedef struct hb_qsv_context {
volatile int is_context_active;

Expand Down Expand Up @@ -288,6 +290,10 @@ typedef struct hb_qsv_context {

void *qsv_config;

int num_cpu_filters;
int qsv_filters_are_enabled;
HBQSVFramesContext *hb_dec_qsv_frames_ctx;
HBQSVFramesContext *hb_vpp_qsv_frames_ctx;
} hb_qsv_context;

typedef enum {
Expand Down
15 changes: 8 additions & 7 deletions libhb/hbavfilter.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

#if HB_PROJECT_FEATURE_QSV
#include "handbrake/qsv_common.h"
extern int qsv_filters_are_enabled;
extern HBQSVFramesContext hb_vpp_qsv_frames_ctx;
#endif

struct hb_avfilter_graph_s
Expand All @@ -30,6 +28,7 @@ struct hb_avfilter_graph_s
char * settings;
AVFrame * frame;
AVRational out_time_base;
hb_job_t * job;
};

static AVFilterContext * append_filter( hb_avfilter_graph_t * graph,
Expand Down Expand Up @@ -91,6 +90,7 @@ hb_avfilter_graph_init(hb_value_t * settings, hb_filter_init_t * init)
goto fail;
}

graph->job = init->job;
graph->settings = settings_str;
graph->avgraph = avfilter_graph_alloc();
if (graph->avgraph == NULL)
Expand All @@ -99,7 +99,8 @@ hb_avfilter_graph_init(hb_value_t * settings, hb_filter_init_t * init)
goto fail;
}
#if HB_PROJECT_FEATURE_QSV
if (!qsv_filters_are_enabled)
int use_qsv_filters = (graph->job && graph->job->qsv.ctx && graph->job->qsv.ctx->qsv_filters_are_enabled) ? 1 : 0;
if (!use_qsv_filters)
#endif
{
av_opt_set(graph->avgraph, "scale_sws_opts", "lanczos+accurate_rnd", 0);
Expand All @@ -115,7 +116,7 @@ hb_avfilter_graph_init(hb_value_t * settings, hb_filter_init_t * init)
AVBufferSrcParameters *par = 0;
// Build filter input
#if HB_PROJECT_FEATURE_QSV
if (qsv_filters_are_enabled)
if (use_qsv_filters)
{
par = av_buffersrc_parameters_alloc();
init->pix_fmt = AV_PIX_FMT_QSV;
Expand Down Expand Up @@ -270,7 +271,7 @@ int hb_avfilter_add_buf(hb_avfilter_graph_t * graph, hb_buffer_t * in)
if (in != NULL)
{
#if HB_PROJECT_FEATURE_QSV
if (qsv_filters_are_enabled)
if (graph->job && graph->job->qsv.ctx && graph->job->qsv.ctx->qsv_filters_are_enabled)
{
hb_video_buffer_to_avframe(in->qsv_details.frame, in);
return hb_avfilter_add_frame(graph, in->qsv_details.frame);
Expand All @@ -297,9 +298,9 @@ hb_buffer_t * hb_avfilter_get_buf(hb_avfilter_graph_t * graph)
{
hb_buffer_t * buf;
#if HB_PROJECT_FEATURE_QSV
if (qsv_filters_are_enabled)
if (graph->job && graph->job->qsv.ctx && graph->job->qsv.ctx->qsv_filters_are_enabled)
{
buf = hb_qsv_copy_frame(&hb_vpp_qsv_frames_ctx, graph->frame, 0, 1);
buf = hb_qsv_copy_frame(graph->job, graph->frame, 1);
hb_avframe_set_video_buffer_flags(buf, graph->frame, graph->out_time_base);
}
else
Expand Down
Loading

0 comments on commit cf4c834

Please sign in to comment.