Skip to content

Commit

Permalink
libhb: improve handling of QSV AVFrames.
Browse files Browse the repository at this point in the history
  • Loading branch information
galad87 committed Aug 5, 2023
1 parent 395676a commit fd36ded
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 125 deletions.
23 changes: 10 additions & 13 deletions libhb/avfilter.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,30 +236,27 @@ static hb_buffer_t* filterFrame( hb_filter_private_t * pv, hb_buffer_t ** buf_in
{
hb_buffer_list_t list;
hb_buffer_t * buf = NULL, * next = NULL;
int av_frame_is_not_null = 1; // TODO: find the reason for empty input av_frame for ffmpeg filters

#if HB_PROJECT_FEATURE_QSV && (defined( _WIN32 ) || defined( __MINGW32__ ))
mfxFrameSurface1 *surface = NULL;
HBQSVFramesContext *frames_ctx = NULL;

if (hb_qsv_hw_filters_are_enabled(pv->input.job) && buf_in != NULL)
{
hb_buffer_t *in = *buf_in;
if (in && in->qsv_details.frame)
hb_buffer_t *in = *buf_in;
AVFrame *frame = (AVFrame *)in->storage;
if (frame)
{
// We need to keep surface pointer because hb_avfilter_add_buf set it to 0 after in ffmpeg call
surface = (mfxFrameSurface1 *)in->qsv_details.frame->data[3];
surface = (mfxFrameSurface1 *)frame->data[3];
frames_ctx = in->qsv_details.qsv_frames_ctx;
}
else
{
av_frame_is_not_null = 0;
}
}
#endif
if (av_frame_is_not_null)
{
hb_avfilter_add_buf(pv->graph, buf_in);
buf = hb_avfilter_get_buf(pv->graph);
}

hb_avfilter_add_buf(pv->graph, buf_in);
buf = hb_avfilter_get_buf(pv->graph);

while (buf != NULL)
{
hb_buffer_list_append(&pv->list, buf);
Expand Down
2 changes: 1 addition & 1 deletion libhb/decavcodec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,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_avframe_to_video_buffer(pv->job, pv->frame, 0);
out = hb_qsv_copy_avframe_to_video_buffer(pv->job, pv->frame, (AVRational){1,1}, 0);
}
else
#endif
Expand Down
11 changes: 7 additions & 4 deletions libhb/enc_qsv.c
Original file line number Diff line number Diff line change
Expand Up @@ -2285,9 +2285,10 @@ int encqsvWork(hb_work_object_t *w, hb_buffer_t **buf_in, hb_buffer_t **buf_out)
else
{
QSVMid *mid = NULL;
if (in->qsv_details.frame && in->qsv_details.frame->data[3])
AVFrame *frame = (AVFrame *)in->storage;
if (frame && frame->data[3])
{
surface = ((mfxFrameSurface1*)in->qsv_details.frame->data[3]);
surface = ((mfxFrameSurface1*)frame->data[3]);
frames_ctx = in->qsv_details.qsv_frames_ctx;
hb_qsv_get_mid_by_surface_from_pool(frames_ctx, surface, &mid);
hb_qsv_replace_surface_mid(frames_ctx, mid, surface);
Expand Down Expand Up @@ -2399,9 +2400,11 @@ int encqsvWork(hb_work_object_t *w, hb_buffer_t **buf_in, hb_buffer_t **buf_out)
goto fail;
}

if (in->qsv_details.frame)
if (in->storage)
{
in->qsv_details.frame->data[3] = 0;
// FIXME: This looks weird
AVFrame *frame = (AVFrame *)in->storage;
frame->data[3] = 0;
}

*buf_out = hb_buffer_list_clear(&pv->encoded_frames);
Expand Down
10 changes: 5 additions & 5 deletions libhb/fifo.c
Original file line number Diff line number Diff line change
Expand Up @@ -939,15 +939,15 @@ void hb_buffer_close( hb_buffer_t ** _b )
#if HB_PROJECT_FEATURE_QSV
// Reclaim QSV resources before dropping the buffer.
// when decoding without QSV, the QSV atom will be NULL.
if(b->qsv_details.frame && b->qsv_details.ctx != NULL)
if (b->storage != NULL && b->qsv_details.ctx != NULL)
{
mfxFrameSurface1 *surface = (mfxFrameSurface1*)b->qsv_details.frame->data[3];
if(surface)
AVFrame *frame = (AVFrame *)b->storage;
mfxFrameSurface1 *surface = (mfxFrameSurface1 *)frame->data[3];
if (surface)
{
hb_qsv_release_surface_from_pool_by_surface_pointer(b->qsv_details.qsv_frames_ctx, surface);
b->qsv_details.frame->data[3] = 0;
frame->data[3] = 0;
}
av_frame_unref(b->qsv_details.frame);
}
if (b->qsv_details.qsv_atom != NULL && b->qsv_details.ctx != NULL)
{
Expand Down
1 change: 0 additions & 1 deletion libhb/handbrake/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ struct hb_buffer_s
struct qsv
{
void * qsv_atom;
AVFrame * frame;
hb_qsv_context * ctx;
HBQSVFramesContext * qsv_frames_ctx;
} qsv_details;
Expand Down
6 changes: 3 additions & 3 deletions libhb/handbrake/qsv_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ int hb_create_ffmpeg_pool(hb_job_t *job, int coded_width, int coded_height, enum
int hb_qsv_hw_filters_are_enabled(hb_job_t *job);
int hb_qsv_full_path_is_enabled(hb_job_t *job);
AVBufferRef *hb_qsv_create_mids(AVBufferRef *hw_frames_ref);
int hb_qsv_attach_surface_to_video_buffer(hb_job_t *job, hb_buffer_t* buf, int is_vpp);
int hb_qsv_copy_video_buffer_to_video_buffer(hb_job_t *job, hb_buffer_t* in, hb_buffer_t* out, int is_vpp);
hb_buffer_t* hb_qsv_copy_avframe_to_video_buffer(hb_job_t *job, AVFrame *frame, int is_vpp);
hb_buffer_t * hb_qsv_copy_video_buffer_to_hw_video_buffer(hb_job_t *job, hb_buffer_t *in, const int is_vpp);
hb_buffer_t * hb_qsv_buffer_dup(hb_job_t *job, hb_buffer_t *in, const int is_vpp);
hb_buffer_t * hb_qsv_copy_avframe_to_video_buffer(hb_job_t *job, AVFrame *frame, AVRational time_base, const 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);
int hb_qsv_get_mid_by_surface_from_pool(HBQSVFramesContext* hb_enc_qsv_frames_ctx, mfxFrameSurface1 *surface, QSVMid **out_mid);
Expand Down
22 changes: 5 additions & 17 deletions libhb/hbavfilter.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,23 +310,12 @@ int hb_avfilter_get_frame(hb_avfilter_graph_t * graph, AVFrame * frame)
int hb_avfilter_add_buf(hb_avfilter_graph_t * graph, hb_buffer_t ** buf_in)
{
int ret;

if (buf_in != NULL && *buf_in != NULL)
{
#if HB_PROJECT_FEATURE_QSV
if (hb_qsv_hw_filters_are_enabled(graph->job))
{
hb_buffer_t *in = *buf_in;

hb_video_buffer_to_avframe(in->qsv_details.frame, buf_in);
ret = hb_avfilter_add_frame(graph, in->qsv_details.frame);
}
else
#endif
{
hb_video_buffer_to_avframe(graph->frame, buf_in);
ret = av_buffersrc_add_frame(graph->input, graph->frame);
av_frame_unref(graph->frame);
}
hb_video_buffer_to_avframe(graph->frame, buf_in);
ret = av_buffersrc_add_frame(graph->input, graph->frame);
av_frame_unref(graph->frame);
}
else
{
Expand All @@ -347,8 +336,7 @@ hb_buffer_t * hb_avfilter_get_buf(hb_avfilter_graph_t * graph)
#if HB_PROJECT_FEATURE_QSV
if (hb_qsv_hw_filters_are_enabled(graph->job))
{
buf = hb_qsv_copy_avframe_to_video_buffer(graph->job, graph->frame, 1);
hb_avframe_set_video_buffer_flags(buf, graph->frame, graph->out_time_base);
buf = hb_qsv_copy_avframe_to_video_buffer(graph->job, graph->frame, graph->out_time_base, 1);
}
else
#endif
Expand Down
Loading

0 comments on commit fd36ded

Please sign in to comment.