diff mbox series

[FFmpeg-devel,v2] libavutil/hwcontext_qsv: Align width and heigh when download qsv frame

Message ID 20220402115939.993541-1-wenbin.chen@intel.com
State New
Headers show
Series [FFmpeg-devel,v2] libavutil/hwcontext_qsv: Align width and heigh when download qsv frame | expand

Commit Message

Wenbin Chen April 2, 2022, 11:59 a.m. UTC
The width and height for qsv frame to download need to be
aligned with 16. Add the alignment operation.
Now the following command works:
ffmpeg -hwaccel qsv -f rawvideo -s 1920x1080 -pix_fmt yuv420p -i \
input.yuv -vf "hwupload=extra_hw_frames=16,format=qsv,hwdownload, \
format=nv12" -f null -

Signed-off-by: Wenbin Chen <wenbin.chen@intel.com>
---
 libavutil/hwcontext_qsv.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
diff mbox series

Patch

diff --git a/libavutil/hwcontext_qsv.c b/libavutil/hwcontext_qsv.c
index 95f8071abe..fe11e215a8 100644
--- a/libavutil/hwcontext_qsv.c
+++ b/libavutil/hwcontext_qsv.c
@@ -1063,6 +1063,38 @@  static int qsv_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst,
     if (ret < 0)
         return ret;
 
+    /* According to MSDK spec for mfxframeinfo, "Width must be a multiple of 16.
+     * Height must be a multiple of 16 for progressive frame sequence and a
+     * multiple of 32 otherwise.", so allign all frames to 16 before downloading. */
+    if (dst->height & 15 || dst->linesize[0] & 15) {
+        AVFrame *tmp_frame;
+        tmp_frame = av_frame_alloc();
+        if (!tmp_frame)
+            return AVERROR(ENOMEM);
+        ret = av_frame_ref(tmp_frame, dst);
+        if (ret < 0) {
+            av_frame_free(&tmp_frame);
+            return ret;
+        }
+        av_frame_unref(dst);
+
+        dst->width  = FFALIGN(tmp_frame->linesize[0], 16);
+        dst->height = FFALIGN(tmp_frame->height, 16);
+        dst->format = tmp_frame->format;
+        ret = av_frame_get_buffer(dst, 0);
+        if (ret < 0) {
+            av_frame_free(&tmp_frame);
+            return ret;
+        }
+
+        dst->width = tmp_frame->width;
+        dst->height = tmp_frame->height;
+        ret = av_frame_copy_props(dst, tmp_frame);
+        av_frame_free(&tmp_frame);
+        if (ret < 0)
+            return ret;
+    }
+
     if (!s->session_download) {
         if (s->child_frames_ref)
             return qsv_transfer_data_child(ctx, dst, src);