Skip to content

Commit

Permalink
avcodec/qsv: polling free synchronization
Browse files Browse the repository at this point in the history
synchronization by sync point after DEVICE_BUSY

Fixes: CPU usage on AVC decode cases (18% -> 9%)
  • Loading branch information
andreyor committed Sep 23, 2019
1 parent 53d31e9 commit 21014bc
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 8 deletions.
17 changes: 17 additions & 0 deletions libavcodec/qsv.c
Expand Up @@ -32,6 +32,7 @@
#include "libavutil/hwcontext_qsv.h"
#include "libavutil/imgutils.h"
#include "libavutil/avassert.h"
#include "libavutil/time.h"

#include "avcodec.h"
#include "qsv_internal.h"
Expand Down Expand Up @@ -758,3 +759,19 @@ int ff_qsv_init_session_frames(AVCodecContext *avctx, mfxSession *psession,
*psession = session;
return 0;
}

void ff_qsv_handle_device_busy(mfxSession *session, mfxSyncPoint *sync, mfxStatus *ret, unsigned sleep)
{
int sync_ret;

if (*sync) {
sync_ret = MFXVideoCORE_SyncOperation(*session, *sync, MFX_INFINITE);
if (sync_ret == MFX_ERR_NONE) {
*sync = NULL;
} else {
*ret = MFX_ERR_ABORTED;
}
} else {
av_usleep(sleep);
}
}
2 changes: 2 additions & 0 deletions libavcodec/qsv_internal.h
Expand Up @@ -111,4 +111,6 @@ int ff_qsv_init_session_frames(AVCodecContext *avctx, mfxSession *session,

int ff_qsv_find_surface_idx(QSVFramesContext *ctx, QSVFrame *frame);

void ff_qsv_handle_device_busy(mfxSession *session, mfxSyncPoint *sync, mfxStatus *ret, unsigned sleep);

#endif /* AVCODEC_QSV_INTERNAL_H */
12 changes: 8 additions & 4 deletions libavcodec/qsvdec.c
Expand Up @@ -404,8 +404,12 @@ static int qsv_decode(AVCodecContext *avctx, QSVContext *q,

ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
insurf, &outsurf, sync);

if (ret == MFX_ERR_NONE)
q->last_dec_sync = *sync;

if (ret == MFX_WRN_DEVICE_BUSY)
av_usleep(500);
ff_qsv_handle_device_busy(&q->session, &q->last_dec_sync, &ret, 500);

} while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);

Expand Down Expand Up @@ -457,9 +461,9 @@ static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
out_frame->queued = 0;

if (avctx->pix_fmt != AV_PIX_FMT_QSV) {
do {
ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000);
} while (ret == MFX_WRN_IN_EXECUTION);
ret = MFXVideoCORE_SyncOperation(q->session, *sync, MFX_INFINITE);
if (ret < 0)
return ret;
}

av_freep(&sync);
Expand Down
2 changes: 2 additions & 0 deletions libavcodec/qsvdec.h
Expand Up @@ -70,6 +70,8 @@ typedef struct QSVContext {

mfxExtBuffer **ext_buffers;
int nb_ext_buffers;

mfxSyncPoint last_dec_sync;
} QSVContext;

extern const AVCodecHWConfigInternal *ff_qsv_hw_configs[];
Expand Down
13 changes: 9 additions & 4 deletions libavcodec/qsvenc.c
Expand Up @@ -1366,8 +1366,13 @@ static int encode_frame(AVCodecContext *avctx, QSVEncContext *q,

do {
ret = MFXVideoENCODE_EncodeFrameAsync(q->session, enc_ctrl, surf, bs, sync);

if (ret == MFX_ERR_NONE)
q->last_enc_sync = *sync;

if (ret == MFX_WRN_DEVICE_BUSY)
av_usleep(500);
ff_qsv_handle_device_busy(&q->session, &q->last_enc_sync, &ret, 500);

} while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_WRN_IN_EXECUTION);

if (ret > 0)
Expand Down Expand Up @@ -1433,9 +1438,9 @@ int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q,
av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
av_fifo_generic_read(q->async_fifo, &bs, sizeof(bs), NULL);

do {
ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000);
} while (ret == MFX_WRN_IN_EXECUTION);
ret = MFXVideoCORE_SyncOperation(q->session, *sync, MFX_INFINITE);
if (ret < 0)
return ret;

new_pkt.dts = av_rescale_q(bs->DecodeTimeStamp, (AVRational){1, 90000}, avctx->time_base);
new_pkt.pts = av_rescale_q(bs->TimeStamp, (AVRational){1, 90000}, avctx->time_base);
Expand Down
2 changes: 2 additions & 0 deletions libavcodec/qsvenc.h
Expand Up @@ -185,6 +185,8 @@ typedef struct QSVEncContext {
char *load_plugins;
SetEncodeCtrlCB *set_encode_ctrl_cb;
int forced_idr;

mfxSyncPoint last_enc_sync;
} QSVEncContext;

int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q);
Expand Down

0 comments on commit 21014bc

Please sign in to comment.