From patchwork Sun May 7 17:40:29 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Levinson X-Patchwork-Id: 3600 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.3.129 with SMTP id 123csp526455vsd; Sun, 7 May 2017 10:40:41 -0700 (PDT) X-Received: by 10.28.228.212 with SMTP id b203mr4548405wmh.119.1494178841246; Sun, 07 May 2017 10:40:41 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 201si11176132wmj.65.2017.05.07.10.40.40; Sun, 07 May 2017 10:40:41 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E4C4868828A; Sun, 7 May 2017 20:40:32 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from white.spiritone.com (white.spiritone.com [216.99.193.38]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4F08A680384 for ; Sun, 7 May 2017 20:40:26 +0300 (EEST) Received: from [10.253.196.185] (unknown [96.89.110.217]) by white.spiritone.com (Postfix) with ESMTPSA id 5E7B473403EC for ; Sun, 7 May 2017 10:40:31 -0700 (PDT) To: FFmpeg development discussions and patches From: Aaron Levinson Message-ID: <931c8692-303e-7fd3-8479-4cff4a9affe2@aracnet.com> Date: Sun, 7 May 2017 10:40:29 -0700 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/2] qsvenc: Make sure the interlaced encoding works X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From 30eb78bac7bd92b0c085ba6873341e8319072acc Mon Sep 17 00:00:00 2001 From: Aaron Levinson Date: Fri, 5 May 2017 21:31:30 -0700 Subject: [PATCH 2/2] qsvenc: Make sure the interlaced encoding works Purpose: qsvenc: make sure that interlaced encoding works. Also, reduce the vertical alignment constraint when possible to reduce memory usage. Note: Most of this code used to be present in ffmpeg and was eliminated in revision 1f26a23 on Oct. 31, 2016 (qsv: Merge libav implementation, at https://github.com/FFmpeg/FFmpeg/commit/1f26a231bb065276cd80ce02957c759f3197 edfa#diff-7d84a34d58597bb7aa4b8239dca1f9f8). Already applied to libav. Reviewed-by: Luca Barbato (cherry picked from commit 8fd8f91e47f33cd82371a97ac81afc476144964f) Signed-off-by: Mark Thompson Signed-off-by: Aaron Levinson --- libavcodec/qsvenc.c | 29 +++++++++++++++++++++++------ libavcodec/qsvenc.h | 1 + 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c index 68d4f5edd0..57bc83a47f 100644 --- a/libavcodec/qsvenc.c +++ b/libavcodec/qsvenc.c @@ -358,8 +358,6 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q) return AVERROR_BUG; q->param.mfx.CodecId = ret; - q->width_align = avctx->codec_id == AV_CODEC_ID_HEVC ? 32 : 16; - if (avctx->level > 0) q->param.mfx.CodecLevel = avctx->level; @@ -381,20 +379,39 @@ static int init_video_param(AVCodecContext *avctx, QSVEncContext *q) ff_qsv_map_pixfmt(sw_format, &q->param.mfx.FrameInfo.FourCC); - q->param.mfx.FrameInfo.Width = FFALIGN(avctx->width, q->width_align); - q->param.mfx.FrameInfo.Height = FFALIGN(avctx->height, 32); q->param.mfx.FrameInfo.CropX = 0; q->param.mfx.FrameInfo.CropY = 0; q->param.mfx.FrameInfo.CropW = avctx->width; q->param.mfx.FrameInfo.CropH = avctx->height; q->param.mfx.FrameInfo.AspectRatioW = avctx->sample_aspect_ratio.num; q->param.mfx.FrameInfo.AspectRatioH = avctx->sample_aspect_ratio.den; - q->param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE; q->param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420; q->param.mfx.FrameInfo.BitDepthLuma = desc->comp[0].depth; q->param.mfx.FrameInfo.BitDepthChroma = desc->comp[0].depth; q->param.mfx.FrameInfo.Shift = desc->comp[0].depth > 8; + // TODO: detect version of MFX--if the minor version is greater than + // or equal to 19, then can use the same alignment settings as H.264 + // for HEVC + q->width_align = avctx->codec_id == AV_CODEC_ID_HEVC ? 32 : 16; + q->param.mfx.FrameInfo.Width = FFALIGN(avctx->width, q->width_align); + + if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) { + // it is important that PicStruct be setup correctly from the + // start--otherwise, encoding doesn't work and results in a bunch + // of incompatible video parameter errors + q->param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_TFF; + // height alignment always must be 32 for interlaced video + q->height_align = 32; + } else { + q->param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE; + // for progressive video, the height should be aligned to 16 for + // H.264. For HEVC, depending on the version of MFX, it should be + // either 32 or 16. The lower number is better if possible. + q->height_align = avctx->codec_id == AV_CODEC_ID_HEVC ? 32 : 16; + } + q->param.mfx.FrameInfo.Height = FFALIGN(avctx->height, q->height_align); + if (avctx->hw_frames_ctx) { AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data; AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx; @@ -898,7 +915,7 @@ static int submit_frame(QSVEncContext *q, const AVFrame *frame, } else { /* make a copy if the input is not padded as libmfx requires */ if (frame->height & 31 || frame->linesize[0] & (q->width_align - 1)) { - qf->frame->height = FFALIGN(frame->height, 32); + qf->frame->height = FFALIGN(frame->height, q->height_align); qf->frame->width = FFALIGN(frame->width, q->width_align); ret = ff_get_buffer(q->avctx, qf->frame, AV_GET_BUFFER_FLAG_REF); diff --git a/libavcodec/qsvenc.h b/libavcodec/qsvenc.h index 361d9333d8..12e3444b75 100644 --- a/libavcodec/qsvenc.h +++ b/libavcodec/qsvenc.h @@ -84,6 +84,7 @@ typedef struct QSVEncContext { int packet_size; int width_align; + int height_align; mfxVideoParam param; mfxFrameAllocRequest req;