From patchwork Wed Jun 7 00:53:43 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Zhao X-Patchwork-Id: 3861 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.10.2 with SMTP id 2csp2118245vsk; Tue, 6 Jun 2017 17:59:43 -0700 (PDT) X-Received: by 10.223.130.151 with SMTP id 23mr19696618wrc.16.1496797183233; Tue, 06 Jun 2017 17:59:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496797183; cv=none; d=google.com; s=arc-20160816; b=KToxyfyYZUYhXXqBoQh6SPCW4UDiZZWKA03zV80dFaQuRnibSB3dSvd4/CYIutJzHa K7VYMcP631nazCgJ7oxe3UpNaGLOZ8xMmPcgaVtVgHXGdMyiz4ibC7JgcQdeSQN6bTYC VlBJXzOhajoP+g3l9ssCn+Eyro7TBD91knLJBByARBPBWHMEsSp4e7DEadeiT/of6AdR cp7RPfwyK3WsGHDfDtrNsXSgTI/Ie182/3n7h4fQmKMfV8332g3b4CWzYHTs9TgJhYwN 8R9jf0KQISdcVTKWyrrYsi9GCg8nsldjL986GIrGiTAXbjWuiRPxE3M1tsYDIKugobg4 Z6Lw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:content-disposition:content-language :mime-version:user-agent:date:message-id:from:to:dkim-signature :delivered-to:arc-authentication-results; bh=n/vNhFSQwc4sq+Cy1MrW7YjdhOmKEmNVmVUAtbtdtgM=; b=RNjxAdjnf36tmtTNJB5PQ3C+nCEEQr+NKs1MXJH+aQso7Oecz/Dl44j4A0oCLK2Js1 VfNETc1nMbismw4kx5AyRGTsgI42a0O2+31IZRYacU9+gaeRBOpJXuKterD/X+NcvIef prI8rTnuC1JjO0zCQfPP8Nwl7aAJDehOzWTv8HttXcN4sRU1ApdubkKkm/coTQCtSfZL hoML5mqoAWHXxcON2DwkcsyzGaNUAu+zA1IRAajY4tXXFOUK/wn3iZm+f/1ArUwP6HAN qpN+FQGqlf2C4YIf4m4j1dh81ss1lb/v/FhJHnlgyB5GFtUc0S/3RautEyKITLa0DEFg vOTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 197si500215wmo.18.2017.06.06.17.59.42; Tue, 06 Jun 2017 17:59:43 -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; dkim=neutral (body hash did not verify) header.i=@gmail.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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 14676689DFB; Wed, 7 Jun 2017 03:59:32 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg0-f46.google.com (mail-pg0-f46.google.com [74.125.83.46]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E5FBD6899C1 for ; Wed, 7 Jun 2017 03:59:25 +0300 (EEST) Received: by mail-pg0-f46.google.com with SMTP id f185so39943569pgc.0 for ; Tue, 06 Jun 2017 17:59:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:message-id:date:user-agent:mime-version :content-language; bh=RsqMvX0l/N9UEFA04F7XJjYdGlOojnFxaCK4yaTsTkQ=; b=VhCD0SghGvMsvN2aQ+YI1J1qxBCl5A51e5CbFw9j6MLjsJgvtMTur3aqPrc2do79pr bwQ+iJ95vevrKCcH202DA0ljrXw/mn42kcYm9kmUO7Kv75MmKKTtxaF102Plfdgc90Ja JgISaBpiDwSoPJaTlrnV7AD31jAHCqlWfzqG2x6KjVp+6ZfjGVDIpd2Y8swVUCX94d62 dvQ7/ufxHR9et7N4jiSpQatuG2MYrwhZHBt7eyn9lp3ihUu7FdqN6AYhckh3Fbrpr0Ku niKIGpko/Zus1s0h/MrB9zsv013E85jEoMKtrvr/tedOjFmsySx61lxbQVd+0FiPDcJU WHzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language; bh=RsqMvX0l/N9UEFA04F7XJjYdGlOojnFxaCK4yaTsTkQ=; b=SoLF4O0kcQSNnkgogWUDbTozuJrca/iaC5MCyqgWCzZ5WJz9KYMCJBGNOBCBoeQZig dj0gtPRZ7/Mgt9AWwHDtv6O4QRmdTGbkQ1pjco5rEeo0lDDQx4BdbcVF5tAWKMRDOQWe dh0SOvCWO6fHi+Z7ftt4sWgMZWfXhQ+rgZdTJm7E7ZHhbg6wGmRxXM1b1NVNL4tl18j4 /ptlNaPDhASuWgIu8qSwKqcIbSyE8B/boZoaNjePp/qOaCu37SFE+smXUvRjHLN/QWhS q0yFhRduq76lYAzM+u/EcdlJh27i0KohBGZMgwcDgUja0g6zxJZ4evroHh0m6wNQKnug jjCg== X-Gm-Message-State: AODbwcBEV18qJuCj+YoGhcK2hcvj0ZqlHHK5ehERGAmwNbTo9zDieIvd nofHSvRpwckA0g== X-Received: by 10.99.161.25 with SMTP id b25mr29268515pgf.26.1496796826999; Tue, 06 Jun 2017 17:53:46 -0700 (PDT) Received: from [10.239.204.30] (fmdmzpr04-ext.fm.intel.com. [192.55.54.39]) by smtp.gmail.com with ESMTPSA id r14sm104032pfe.9.2017.06.06.17.53.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Jun 2017 17:53:45 -0700 (PDT) To: FFmpeg development discussions and patches , Mark Thompson From: Jun Zhao Message-ID: Date: Wed, 7 Jun 2017 08:53:43 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.1.1 MIME-Version: 1.0 Content-Language: en-US Content-Disposition: attachment; filename*0="0001-lavc-vaapi_encode_h26-45-respect-slices-in-h26-45-va.pa"; filename*1="tch" X-Content-Filtered-By: Mailman/MimeDel 2.1.20 Subject: [FFmpeg-devel] [PATCH] lavc/vaapi_encode_h26[45]: respect "slices" option in h26[45] vaapi encoder. 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 5c88956e36e7318cf1d1b7c41a9d4108fcf9d0a5 Mon Sep 17 00:00:00 2001 From: Jun Zhao Date: Fri, 12 May 2017 08:30:43 +0800 Subject: [PATCH] lavc/vaapi_encode_h26[45]: respect "slices" in h26[45] vaapi encoder. Enable multi-slice support in AVC/HEVC vaapi encoder. Signed-off-by: Wang, Yi A Signed-off-by: Jun Zhao --- libavcodec/vaapi_encode.c | 36 ++++++++++++++++++++++++++++++++---- libavcodec/vaapi_encode.h | 9 +++++++-- libavcodec/vaapi_encode_h264.c | 24 ++++++++++++++++++------ libavcodec/vaapi_encode_h265.c | 28 ++++++++++++++++++++++------ 4 files changed, 79 insertions(+), 18 deletions(-) diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c index 7e9c00f51d..14a3fba7b1 100644 --- a/libavcodec/vaapi_encode.c +++ b/libavcodec/vaapi_encode.c @@ -36,13 +36,18 @@ static int vaapi_encode_make_packed_header(AVCodecContext *avctx, VAAPIEncodeContext *ctx = avctx->priv_data; VAStatus vas; VABufferID param_buffer, data_buffer; + VABufferID *tmp; VAEncPackedHeaderParameterBuffer params = { .type = type, .bit_length = bit_len, .has_emulation_bytes = 1, }; - av_assert0(pic->nb_param_buffers + 2 <= MAX_PARAM_BUFFERS); + tmp = av_realloc_array(pic->param_buffers, sizeof(*tmp), (pic->nb_param_buffers + 2)); + if (!tmp) { + return AVERROR(ENOMEM); + } + pic->param_buffers = tmp; vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context, VAEncPackedHeaderParameterBufferType, @@ -77,9 +82,14 @@ static int vaapi_encode_make_param_buffer(AVCodecContext *avctx, { VAAPIEncodeContext *ctx = avctx->priv_data; VAStatus vas; + VABufferID *tmp; VABufferID buffer; - av_assert0(pic->nb_param_buffers + 1 <= MAX_PARAM_BUFFERS); + tmp = av_realloc_array(pic->param_buffers, sizeof(*tmp), (pic->nb_param_buffers + 1)); + if (!tmp) { + return AVERROR(ENOMEM); + } + pic->param_buffers = tmp; vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context, type, len, 1, data, &buffer); @@ -122,6 +132,8 @@ static int vaapi_encode_wait(AVCodecContext *avctx, // Input is definitely finished with now. av_frame_free(&pic->input_image); + av_freep(&pic->param_buffers); + pic->encode_complete = 1; return 0; } @@ -313,7 +325,10 @@ static int vaapi_encode_issue(AVCodecContext *avctx, } } - av_assert0(pic->nb_slices <= MAX_PICTURE_SLICES); + pic->slices = (VAAPIEncodeSlice **)av_malloc(sizeof(VAAPIEncodeSlice *) * pic->nb_slices); + if (pic->slices == NULL) + goto fail; + for (i = 0; i < pic->nb_slices; i++) { slice = av_mallocz(sizeof(*slice)); if (!slice) { @@ -322,7 +337,6 @@ static int vaapi_encode_issue(AVCodecContext *avctx, } slice->index = i; pic->slices[i] = slice; - if (ctx->codec->slice_params_size > 0) { slice->codec_slice_params = av_mallocz(ctx->codec->slice_params_size); if (!slice->codec_slice_params) { @@ -427,6 +441,8 @@ fail: vaDestroyBuffer(ctx->hwctx->display, pic->param_buffers[i]); fail_at_end: av_freep(&pic->codec_picture_params); + av_freep(&pic->param_buffers); + av_freep(&pic->slices); av_frame_free(&pic->recon_image); return err; } @@ -542,6 +558,8 @@ static int vaapi_encode_free(AVCodecContext *avctx, av_frame_free(&pic->input_image); av_frame_free(&pic->recon_image); + av_freep(&pic->param_buffers); + av_freep(&pic->slices); // Output buffer should already be destroyed. av_assert0(pic->output_buffer == VA_INVALID_ID); @@ -949,6 +967,7 @@ static av_cold int vaapi_encode_config_attributes(AVCodecContext *avctx) { VAConfigAttribRTFormat }, { VAConfigAttribRateControl }, { VAConfigAttribEncMaxRefFrames }, + { VAConfigAttribEncMaxSlices }, { VAConfigAttribEncPackedHeaders }, }; @@ -1079,6 +1098,15 @@ static av_cold int vaapi_encode_config_attributes(AVCodecContext *avctx) } } break; + case VAConfigAttribEncMaxSlices: + if (avctx->slices > attr[i].value) { + av_log(avctx, AV_LOG_ERROR, "Slices per frame more than %#x " + "is not supported.\n", attr[i].value); + err = AVERROR(EINVAL); + goto fail; + } + ctx->multi_slices_available = 1; + break; case VAConfigAttribEncPackedHeaders: if (ctx->va_packed_headers & ~attr[i].value) { // This isn't fatal, but packed headers are always diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h index 0edf27e4cb..4afe4fa103 100644 --- a/libavcodec/vaapi_encode.h +++ b/libavcodec/vaapi_encode.h @@ -73,7 +73,7 @@ typedef struct VAAPIEncodePicture { VASurfaceID recon_surface; int nb_param_buffers; - VABufferID param_buffers[MAX_PARAM_BUFFERS]; + VABufferID *param_buffers; AVBufferRef *output_buffer_ref; VABufferID output_buffer; @@ -85,7 +85,10 @@ typedef struct VAAPIEncodePicture { struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES]; int nb_slices; - VAAPIEncodeSlice *slices[MAX_PICTURE_SLICES]; + VAAPIEncodeSlice **slices; + int slice_of_mbs; + int slice_mod_mbs; + int last_mb_index; } VAAPIEncodePicture; typedef struct VAAPIEncodeContext { @@ -105,6 +108,8 @@ typedef struct VAAPIEncodeContext { // Supported packed headers (initially the desired set, modified // later to what is actually supported). unsigned int va_packed_headers; + // Supported multi-slices per frame + int multi_slices_available; // The required size of surfaces. This is probably the input // size (AVCodecContext.width|height) aligned up to whatever diff --git a/libavcodec/vaapi_encode_h264.c b/libavcodec/vaapi_encode_h264.c index 92e29554ed..f325346433 100644 --- a/libavcodec/vaapi_encode_h264.c +++ b/libavcodec/vaapi_encode_h264.c @@ -1002,7 +1002,16 @@ static int vaapi_encode_h264_init_picture_params(AVCodecContext *avctx, vpic->pic_fields.bits.idr_pic_flag = (pic->type == PICTURE_TYPE_IDR); vpic->pic_fields.bits.reference_pic_flag = (pic->type != PICTURE_TYPE_B); - pic->nb_slices = 1; + if (ctx->multi_slices_available) + avctx->slices = av_clip(avctx->slices, 1, priv->mb_height); + else + avctx->slices = 1; + + pic->nb_slices = avctx->slices; + + pic->slice_of_mbs = (priv->mb_width * priv->mb_height) / pic->nb_slices; + pic->slice_mod_mbs = (priv->mb_width * priv->mb_height) % pic->nb_slices; + pic->last_mb_index = 0; return 0; } @@ -1052,15 +1061,18 @@ static int vaapi_encode_h264_init_slice_params(AVCodecContext *avctx, av_assert0(0 && "invalid picture type"); } - // Only one slice per frame. - vslice->macroblock_address = 0; - vslice->num_macroblocks = priv->mb_width * priv->mb_height; + vslice->macroblock_address = pic->last_mb_index; + vslice->num_macroblocks = pic->slice_of_mbs + (pic->slice_mod_mbs > 0 ? 1 : 0); + if (pic->slice_mod_mbs > 0) + pic->slice_mod_mbs --; + pic->last_mb_index += vslice->num_macroblocks; vslice->macroblock_info = VA_INVALID_ID; vslice->pic_parameter_set_id = vpic->pic_parameter_set_id; - vslice->idr_pic_id = priv->idr_pic_count++; - + vslice->idr_pic_id = priv->idr_pic_count; + if (pic->last_mb_index == priv->mb_width * priv->mb_height) + priv->idr_pic_count ++; vslice->pic_order_cnt_lsb = (pic->display_order - priv->last_idr_frame) & ((1 << (4 + vseq->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4)) - 1); diff --git a/libavcodec/vaapi_encode_h265.c b/libavcodec/vaapi_encode_h265.c index 6e008b7b9c..e930026184 100644 --- a/libavcodec/vaapi_encode_h265.c +++ b/libavcodec/vaapi_encode_h265.c @@ -1025,7 +1025,15 @@ static int vaapi_encode_h265_init_picture_params(AVCodecContext *avctx, av_assert0(0 && "invalid picture type"); } - pic->nb_slices = 1; + if (ctx->multi_slices_available) + avctx->slices = av_clip(avctx->slices, 1, priv->ctu_height); + else + avctx->slices = 1; + + pic->nb_slices = avctx->slices; + pic->slice_of_mbs = (priv->ctu_width * priv->ctu_height) / pic->nb_slices; + pic->slice_mod_mbs = (priv->ctu_width * priv->ctu_height) % pic->nb_slices; + pic->last_mb_index = 0; return 0; } @@ -1048,9 +1056,13 @@ static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx, pslice = slice->priv_data; mslice = &pslice->misc_slice_params; - // Currently we only support one slice per frame. - vslice->slice_segment_address = 0; - vslice->num_ctu_in_slice = priv->ctu_width * priv->ctu_height; + vslice->slice_segment_address = pic->last_mb_index; + mslice->slice_segment_address = pic->last_mb_index; + vslice->num_ctu_in_slice = pic->slice_of_mbs + (pic->slice_mod_mbs > 0 ? 1 : 0); + + if (pic->slice_mod_mbs > 0) + pic->slice_mod_mbs --; + pic->last_mb_index += vslice->num_ctu_in_slice; switch (pic->type) { case PICTURE_TYPE_IDR: @@ -1104,9 +1116,13 @@ static int vaapi_encode_h265_init_slice_params(AVCodecContext *avctx, else vslice->slice_qp_delta = priv->fixed_qp_idr - vpic->pic_init_qp; - vslice->slice_fields.bits.last_slice_of_pic_flag = 1; + if (pic->last_mb_index == priv->ctu_width * priv->ctu_height) + vslice->slice_fields.bits.last_slice_of_pic_flag = 1; - mslice->first_slice_segment_in_pic_flag = 1; + if (vslice->slice_segment_address == 0) + mslice->first_slice_segment_in_pic_flag = 1; + else + mslice->first_slice_segment_in_pic_flag = 0; if (pic->type == PICTURE_TYPE_IDR) { // No reference pictures.