From patchwork Thu Sep 21 13:52:49 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steve Lhomme X-Patchwork-Id: 5223 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.36.26 with SMTP id f26csp1977502jaa; Thu, 21 Sep 2017 06:58:18 -0700 (PDT) X-Received: by 10.28.151.74 with SMTP id z71mr1202520wmd.131.1506002298590; Thu, 21 Sep 2017 06:58:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1506002298; cv=none; d=google.com; s=arc-20160816; b=zYDkDr5Bes9ymEZoJ7XGUBjYY/1TPvDCXGzseCl7z6U0z3E5mlzjRbrLO6N1HUgxBZ BHuLV4n0mdRY/1oairlAZ0nYh6NFK2Xi9mOImFypgClcN2M5YfCvFb1HKlo5Qw2HOQ59 hYShJWdQDfTRq30UWlM6rkgEcn48F8hn4z/D45vDYfoC1Not7fRwiuSrqoZ8Kbt9NMs+ 4uR5IYfjLEH72b7IRWXehXKiNsog6HZ3RCuB7Ivqwmhwngfho7mWs2wOGobnkfBzUVj5 02err5BHnNADYGQPnK2fHt9LarEHwInV2XwXaLiAaxdWAnWn3zaSaXKF+1vm1hn8rTTN hV7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=ET+0HMicPI9LNQ1aJAXtO90FcscKVunLVBDi4yxWbxI=; b=pUIpU2eW9rRM0++C4PmQzwbnORzPZ3s6OcSNGnTR2Aok5lJI4AxsqiZMJfvzoQtN/m HUrmwNK0GYYNFVZkTFIwxBnH2UDSqO7Ywh4jfW0peXNKxpAwckSuzFta5PQGn5jgqNpb BQr9kn2VnJqwXKZ0yC9j9Ovr0aDASZilYHKtq2CO6nJkDsr84moCIdZ1CeMm9nQWbWRt VhBdd5Bc0BujkuxDUgSSkAsu0yF/f+IC/2I+yJRYBsIbRtpcTBmlKYc3KBqyAGsKf5pt XaYnobUkJ6vtgoV/QlDEfLe68vdMZNWt+DOSOnrXJ3mvBOCGo8z068UqNKDJLFBjW1g6 oB7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=Cs2yaEQX; 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 e44si1242628wre.413.2017.09.21.06.58.18; Thu, 21 Sep 2017 06:58:18 -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 header.s=20161025 header.b=Cs2yaEQX; 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 742F66883B4; Thu, 21 Sep 2017 16:58:07 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f66.google.com (mail-wm0-f66.google.com [74.125.82.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id EB95468826D for ; Thu, 21 Sep 2017 16:58:00 +0300 (EEST) Received: by mail-wm0-f66.google.com with SMTP id e64so5325462wmi.2 for ; Thu, 21 Sep 2017 06:58:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=YeC53MQiJtsZWNV3vfB7YEKpCSv29nB/coXU/+zrOgY=; b=Cs2yaEQX6KSn5Iz3TIsMtIoFXm3r9EgV1cQiqPZgOSh9l/RHhx5JqlEA2BAYOfR+Ts RGYvpKqDlHJ3zsZSnucLKrCBPDYx2HymIGzat5wXf8PuuiVameXOqs+SZtuXBGLpQOnG Oacz9/8VUm+dXJ+OTTW1YPYgI5P7poKnE7X6VfAZnUMP7ZD/ZXMxVJuBFynHhA99Yacq 34i9+sKYU6Jy9fJsTmqV2E+YpzklWMPV07S0e5NKF768hhPshTkoobMKjnTfptnW2Th/ ET1aAR4ixgM/o6u1eusC9YgXWDxFEdSV2B1pvk1WIQIU6Zy0eRnrVejtxtxkDiEwcIjk YU7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=YeC53MQiJtsZWNV3vfB7YEKpCSv29nB/coXU/+zrOgY=; b=tckks9Ty75q02hFtOtuKG5Vh/AuDg6CFCcl3t5sRt5CGtdqC96oAm5PcqtjLhep8k7 ZgajTebQMozlrfJwZW5yu/EktdxECjBHLtwBVF97UaZVbo8G0bUSMZyYYpYVSsXcGbG5 pD6PMUYVPSBXaQMCdVCtXP508VQcgS7Gh/CJQx5NJx3lHk37O2XbLbcVInG7fPYBRf2W h2u7eEJ/ujndSk3f79mcoZqI54XdFgCIEP58boFGyYFg/XZdRaYGL5wW2AZ+PI7xtq55 bvatS/9wmOKkLk5YO2pQKNG9L5i/momgQ9dfN2tGE9N4FMQmHs8NYWHG3WjidBWfVuCI zdfA== X-Gm-Message-State: AHPjjUg+UKNZ+TNcNc2IVlF8FMFFqJwzJERU/7hFv2tQBkLj3YNgJ5zi Q3MO7D45k9JkkPjdyKFw9WEx/L4= X-Google-Smtp-Source: AOwi7QDSr1wD2L2iLZc8UuxnlEeV80oK51LIl8O+qW/fYZbiKsV4X72pil0zfc7fGQktiSTYAa7R9g== X-Received: by 10.28.13.76 with SMTP id 73mr1055326wmn.101.1506001981034; Thu, 21 Sep 2017 06:53:01 -0700 (PDT) Received: from localhost.localdomain (2a01cb0c000ea9000095b705df317edf.ipv6.abo.wanadoo.fr. [2a01:cb0c:e:a900:95:b705:df31:7edf]) by smtp.gmail.com with ESMTPSA id l15sm2363096wmd.23.2017.09.21.06.52.59 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 21 Sep 2017 06:52:59 -0700 (PDT) From: Steve Lhomme To: ffmpeg-devel@ffmpeg.org Date: Thu, 21 Sep 2017 15:52:49 +0200 Message-Id: <20170921135250.716-3-robux4@gmail.com> X-Mailer: git-send-email 2.12.1 In-Reply-To: <20170921135250.716-1-robux4@gmail.com> References: <20170921135250.716-1-robux4@gmail.com> Subject: [FFmpeg-devel] [PATCH 3/4] qsvenc: merge the output fifo elements into a structure 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" This is cleaner to read and less error prone. --- libavcodec/qsvenc.c | 117 +++++++++++++++++++++++++--------------------------- 1 file changed, 56 insertions(+), 61 deletions(-) diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c index 772336b719..bbf72e54b0 100644 --- a/libavcodec/qsvenc.c +++ b/libavcodec/qsvenc.c @@ -69,6 +69,13 @@ static const struct { #endif }; +struct QSVpacket +{ + AVPacket new_pkt; + mfxBitstream *bs; + mfxSyncPoint *syncp; +}; + static const char *print_profile(mfxU16 profile) { int i; @@ -727,8 +734,7 @@ int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q) q->param.AsyncDepth = q->async_depth; - q->async_fifo = av_fifo_alloc((1 + q->async_depth) * - (sizeof(AVPacket) + sizeof(mfxSyncPoint*) + sizeof(mfxBitstream*))); + q->async_fifo = av_fifo_alloc((1 + q->async_depth) * sizeof(struct QSVpacket)); if (!q->async_fifo) return AVERROR(ENOMEM); @@ -979,14 +985,19 @@ static void print_interlace_msg(AVCodecContext *avctx, QSVEncContext *q) } } +static void qsv_packet_release(struct QSVpacket *qsv_pkt) +{ + av_packet_unref(&qsv_pkt->new_pkt); + av_freep(qsv_pkt->bs); + av_freep(qsv_pkt->syncp); +} + static int encode_frame(AVCodecContext *avctx, QSVEncContext *q, const AVFrame *frame) { - AVPacket new_pkt = { 0 }; - mfxBitstream *bs; + struct QSVpacket qsv_pkt = { 0 }; mfxFrameSurface1 *surf = NULL; - mfxSyncPoint *sync = NULL; QSVFrame *qsv_frame = NULL; mfxEncodeCtrl* enc_ctrl = NULL; mfxStatus ret; @@ -1003,33 +1014,33 @@ static int encode_frame(AVCodecContext *avctx, QSVEncContext *q, enc_ctrl = &qsv_frame->enc_ctrl; } - ret = av_new_packet(&new_pkt, q->packet_size); + ret = av_new_packet(&qsv_pkt.new_pkt, q->packet_size); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "Error allocating the output packet\n"); return ret; } - bs = av_mallocz(sizeof(*bs)); - if (!bs) { - av_packet_unref(&new_pkt); + qsv_pkt.bs = av_mallocz(sizeof(*qsv_pkt.bs)); + if (!qsv_pkt.bs) { + av_packet_unref(&qsv_pkt.new_pkt); return AVERROR(ENOMEM); } - bs->Data = new_pkt.data; - bs->MaxLength = new_pkt.size; + qsv_pkt.bs->Data = qsv_pkt.new_pkt.data; + qsv_pkt.bs->MaxLength = qsv_pkt.new_pkt.size; if (q->set_encode_ctrl_cb) { q->set_encode_ctrl_cb(avctx, frame, &qsv_frame->enc_ctrl); } - sync = av_mallocz(sizeof(*sync)); - if (!sync) { - av_freep(&bs); - av_packet_unref(&new_pkt); + qsv_pkt.syncp = av_mallocz(sizeof(*qsv_pkt.syncp)); + if (!qsv_pkt.syncp) { + av_freep(&qsv_pkt.bs); + av_packet_unref(&qsv_pkt.new_pkt); return AVERROR(ENOMEM); } do { - ret = MFXVideoENCODE_EncodeFrameAsync(q->session, enc_ctrl, surf, bs, sync); + ret = MFXVideoENCODE_EncodeFrameAsync(q->session, enc_ctrl, surf, qsv_pkt.bs, qsv_pkt.syncp); if (ret == MFX_WRN_DEVICE_BUSY) av_usleep(500); } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_WRN_IN_EXECUTION); @@ -1038,9 +1049,7 @@ static int encode_frame(AVCodecContext *avctx, QSVEncContext *q, ff_qsv_print_warning(avctx, ret, "Warning during encoding"); if (ret < 0) { - av_packet_unref(&new_pkt); - av_freep(&bs); - av_freep(&sync); + qsv_packet_release(&qsv_pkt); return (ret == MFX_ERR_MORE_DATA) ? 0 : ff_qsv_print_error(avctx, ret, "Error during encoding"); } @@ -1048,14 +1057,10 @@ static int encode_frame(AVCodecContext *avctx, QSVEncContext *q, if (ret == MFX_WRN_INCOMPATIBLE_VIDEO_PARAM && frame->interlaced_frame) print_interlace_msg(avctx, q); - if (*sync) { - av_fifo_generic_write(q->async_fifo, &new_pkt, sizeof(new_pkt), NULL); - av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL); - av_fifo_generic_write(q->async_fifo, &bs, sizeof(bs), NULL); + if (*qsv_pkt.syncp) { + av_fifo_generic_write(q->async_fifo, &qsv_pkt, sizeof(qsv_pkt), NULL); } else { - av_freep(&sync); - av_packet_unref(&new_pkt); - av_freep(&bs); + qsv_packet_release(&qsv_pkt); } return 0; @@ -1072,57 +1077,53 @@ int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q, if (!av_fifo_space(q->async_fifo) || (!frame && av_fifo_size(q->async_fifo))) { - AVPacket new_pkt; - mfxBitstream *bs; - mfxSyncPoint *sync; + struct QSVpacket qsv_pkt; - av_fifo_generic_read(q->async_fifo, &new_pkt, sizeof(new_pkt), NULL); - av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL); - av_fifo_generic_read(q->async_fifo, &bs, sizeof(bs), NULL); + av_fifo_generic_read(q->async_fifo, &qsv_pkt, sizeof(qsv_pkt), NULL); do { - ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000); + ret = MFXVideoCORE_SyncOperation(q->session, *qsv_pkt.syncp, 1000); } while (ret == MFX_WRN_IN_EXECUTION); - 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); - new_pkt.size = bs->DataLength; + qsv_pkt.new_pkt.dts = av_rescale_q(qsv_pkt.bs->DecodeTimeStamp, (AVRational){1, 90000}, avctx->time_base); + qsv_pkt.new_pkt.pts = av_rescale_q(qsv_pkt.bs->TimeStamp, (AVRational){1, 90000}, avctx->time_base); + qsv_pkt.new_pkt.size = qsv_pkt.bs->DataLength; - if (bs->FrameType & MFX_FRAMETYPE_IDR || - bs->FrameType & MFX_FRAMETYPE_xIDR) - new_pkt.flags |= AV_PKT_FLAG_KEY; + if (qsv_pkt.bs->FrameType & MFX_FRAMETYPE_IDR || + qsv_pkt.bs->FrameType & MFX_FRAMETYPE_xIDR) + qsv_pkt.new_pkt.flags |= AV_PKT_FLAG_KEY; #if FF_API_CODED_FRAME FF_DISABLE_DEPRECATION_WARNINGS - if (bs->FrameType & MFX_FRAMETYPE_I || bs->FrameType & MFX_FRAMETYPE_xI) + if (qsv_pkt.bs->FrameType & MFX_FRAMETYPE_I || qsv_pkt.bs->FrameType & MFX_FRAMETYPE_xI) avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I; - else if (bs->FrameType & MFX_FRAMETYPE_P || bs->FrameType & MFX_FRAMETYPE_xP) + else if (qsv_pkt.bs->FrameType & MFX_FRAMETYPE_P || qsv_pkt.bs->FrameType & MFX_FRAMETYPE_xP) avctx->coded_frame->pict_type = AV_PICTURE_TYPE_P; - else if (bs->FrameType & MFX_FRAMETYPE_B || bs->FrameType & MFX_FRAMETYPE_xB) + else if (qsv_pkt.bs->FrameType & MFX_FRAMETYPE_B || qsv_pkt.bs->FrameType & MFX_FRAMETYPE_xB) avctx->coded_frame->pict_type = AV_PICTURE_TYPE_B; FF_ENABLE_DEPRECATION_WARNINGS #endif - av_freep(&bs); - av_freep(&sync); + av_freep(&qsv_pkt.bs); + av_freep(&qsv_pkt.syncp); if (pkt->data) { - if (pkt->size < new_pkt.size) { + if (pkt->size < qsv_pkt.new_pkt.size) { av_log(avctx, AV_LOG_ERROR, "Submitted buffer not large enough: %d < %d\n", - pkt->size, new_pkt.size); - av_packet_unref(&new_pkt); + pkt->size, qsv_pkt.new_pkt.size); + av_packet_unref(&qsv_pkt.new_pkt); return AVERROR(EINVAL); } - memcpy(pkt->data, new_pkt.data, new_pkt.size); - pkt->size = new_pkt.size; + memcpy(pkt->data, qsv_pkt.new_pkt.data, qsv_pkt.new_pkt.size); + pkt->size = qsv_pkt.new_pkt.size; - ret = av_packet_copy_props(pkt, &new_pkt); - av_packet_unref(&new_pkt); + ret = av_packet_copy_props(pkt, &qsv_pkt.new_pkt); + av_packet_unref(&qsv_pkt.new_pkt); if (ret < 0) return ret; } else - *pkt = new_pkt; + *pkt = qsv_pkt.new_pkt; *got_packet = 1; } @@ -1154,17 +1155,11 @@ int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q) } while (q->async_fifo && av_fifo_size(q->async_fifo)) { - AVPacket pkt; - mfxSyncPoint *sync; - mfxBitstream *bs; + struct QSVpacket qsv_pkt; - av_fifo_generic_read(q->async_fifo, &pkt, sizeof(pkt), NULL); - av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL); - av_fifo_generic_read(q->async_fifo, &bs, sizeof(bs), NULL); + av_fifo_generic_read(q->async_fifo, &qsv_pkt, sizeof(qsv_pkt), NULL); - av_freep(&sync); - av_freep(&bs); - av_packet_unref(&pkt); + qsv_packet_release(&qsv_pkt); } av_fifo_free(q->async_fifo); q->async_fifo = NULL;