From patchwork Sun Nov 19 19:52:26 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philip Langdale X-Patchwork-Id: 6201 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.161.94 with SMTP id m30csp3111879jah; Sun, 19 Nov 2017 11:53:00 -0800 (PST) X-Google-Smtp-Source: AGs4zMZSBHD94imRa/PXGSdiTbpG5c+p7RnlGeAXQO/S6iwjA+3SlnupNiTq3qxHeZ/pZzr7Ocol X-Received: by 10.28.217.144 with SMTP id q138mr9033309wmg.9.1511121180035; Sun, 19 Nov 2017 11:53:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511121179; cv=none; d=google.com; s=arc-20160816; b=miBseT+56V3wNvxbnnW8vWvF+3PDBBgoyN5OpbYxS6tgmjT3aoTXu01uVZ+2+W2CdB zHfkTgbHTlS8GDTnseKKM5W9Nu/84Mgu406O6Wab90tNv2jQ4x+TLhhsF2n1L4FUSdlP T25LzF5e5ABpfPo2mSEf2CDpaY8a6ejxPzp0V3DAb9rNJGIvxv3iu9T6mp1cJNiRLq60 QpV3fV4foVzbabBmHEwdRz5z8dPEdrjApUvsDhZv95IXMWzTYHWwuH0W/MXrdnpulCdY UD5q5ih8EMgtlMVUGy0D89U2yN6SCKnsIGUYUqLPo5UviE308MG0ytj3yM9rzzH8TEGC cjMw== 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:cc: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=H2NVrP147ogHXcRW5wtO6UZetmRSxuMSyHSvvLu4EF8=; b=GAzywqdj4Pz8a8NkTYje0wxgbDk/qmugx76lB9sNPxRdSCHMof8EB1jiDpX4hrdi/z 9SpxzMg0s6u2MoGxewXhRXdM8tqLZfV01FsZjzOuJv7Qi/C1j6qdxp3za1i5yYhFGru4 FWQ/Tmv1fbjBKX3YlSeogjmnO7Ieg+TxEjZqdGsjFvdUaikXCEblmZsXHGHIYkQ+McH1 QC2m30/JP9OJVw5F1aj8zxknS3djW1iiznveQ5FBFRYi4N0C9c/kacC3lpcA3rZV3nUc NoOviaPclF50YsbOe4ooQPX4DHmbXXmo59D/34qdWgggGGf/H+QQ5hIu4owv+wlQXNu/ e03A== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@overt.org header.s=mail header.b=uq70DJND; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id i75si6723672wri.188.2017.11.19.11.52.59; Sun, 19 Nov 2017 11:52:59 -0800 (PST) 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=@overt.org header.s=mail header.b=uq70DJND; 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 F0693689AE2; Sun, 19 Nov 2017 21:52:32 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-it0-f99.google.com (mail-it0-f99.google.com [209.85.214.99]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4C4CB6882C9 for ; Sun, 19 Nov 2017 21:52:25 +0200 (EET) Received: by mail-it0-f99.google.com with SMTP id 187so7366036iti.1 for ; Sun, 19 Nov 2017 11:52:42 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references; bh=M3zblw3NtwMQBcMGa016R/OkwsR6YHCwDNX+jl9ely8=; b=MjU8rD2iE7+0NKJDUrpfF7iIFP8B70F+Mt7Qc9lEC+/uJg5egvZim1JpT422FyBszv epO+FrwYLOX+EZdWgjc5C59+fY2sTfcm5L6n73Vavi8VlMtnSCXDb4ZYMRL5ZXy7J4xn dhKKnem8ZsVRoWYoroKRP6O1RTa58/05eDwAblciBdUAATFznapqaC2+L17v4h0hdHEh +0eZdDjsYx2TMeUeiLCAHFAXWHLOtf6ynKh8QsykO4axsJsIk4L2Iuu54ZDGvjyYWKTh OAYuR6qp9HRkKUN61w317GkDCcKOHVBvvCnUXKdOXxGzBe54s9EsdKG/hsCCa4ksK6a8 Ebtw== X-Gm-Message-State: AJaThX5/Fd0rPzb83FSvpx77D1P4CJ4o4Erv/on+CC/h/ZBwpLXAmSyN 8uXAj31c6Jietsrj+i/WZGjen0Fn8Xg9aJALzGbnx5RKriEfQw== X-Received: by 10.36.40.207 with SMTP id h198mr16587144ith.95.1511121161184; Sun, 19 Nov 2017 11:52:41 -0800 (PST) Received: from mail.overt.org (155.208.178.107.bc.googleusercontent.com. [107.178.208.155]) by smtp-relay.gmail.com with ESMTPS id 67sm2890523ita.0.2017.11.19.11.52.41 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 19 Nov 2017 11:52:41 -0800 (PST) X-Relaying-Domain: gapps.overt.org Received: from authenticated-user (mail.overt.org [107.178.208.155]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.overt.org (Postfix) with ESMTPSA id A5159627DD; Sun, 19 Nov 2017 19:52:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=overt.org; s=mail; t=1511121160; bh=Ymjs4s/UpM/acQKe4CeLxD5kt7WI2iBY1Mx8rcDBZPc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uq70DJND3JWaZcsRk5g+4/uU+fttY3OrLVEaFn7xtI/8zvWQD2yBlIcJmRFOcpiwu 2n2HtpEYcqYOj1OkaXyQmzdiTdLp3XNmhIlU7p9iDNB8kSrglCDtux9ALWebd+5aZZ py00L+uOVFFiiNl4MRVphP0MHO5G6bb9BukkIZvyDFXyFqiGIkyG93RTuN4MW+tOG8 rePLXrJJNhvb5LKfdh+0tEzFty6ByLw/dSjrmsP2j9Y8/m+z+byX5oCLywvCKnxwPs DzzCQjRizN+76ObbmN5zmYF7UvLZDzzgYjE1BvFOw+q55mCcGc9zGN7qxkXwVFWc81 bjWSEmDkv+ltA== From: Philip Langdale To: ffmpeg-devel@ffmpeg.org Date: Sun, 19 Nov 2017 11:52:26 -0800 Message-Id: <20171119195228.25719-2-philipl@overt.org> In-Reply-To: <20171119195228.25719-1-philipl@overt.org> References: <20171119195228.25719-1-philipl@overt.org> Subject: [FFmpeg-devel] [PATCH 1/3] avcodec: Refactor common nvdec hwaccel logic 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 Cc: Philip Langdale MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" The 'simple' hwaccels (not h.264 and hevc) all use the same bitstream management and reference lookup logic so let's refactor all that into common functions. I verified that casting a signed int -1 to unsigned char produces 255 according to the C language specification. Signed-off-by: Philip Langdale --- libavcodec/nvdec.c | 46 +++++++++++++++++++++++++++++++++++++++ libavcodec/nvdec.h | 4 ++++ libavcodec/nvdec_mpeg12.c | 53 ++++----------------------------------------- libavcodec/nvdec_vc1.c | 55 ++++++----------------------------------------- libavcodec/nvdec_vp9.c | 53 +++++---------------------------------------- 5 files changed, 65 insertions(+), 146 deletions(-) diff --git a/libavcodec/nvdec.c b/libavcodec/nvdec.c index 3d62840e9f..97ff605f0f 100644 --- a/libavcodec/nvdec.c +++ b/libavcodec/nvdec.c @@ -475,6 +475,36 @@ finish: return ret; } +int ff_nvdec_simple_end_frame(AVCodecContext *avctx) +{ + NVDECContext *ctx = avctx->internal->hwaccel_priv_data; + int ret = ff_nvdec_end_frame(avctx); + ctx->bitstream = NULL; + return ret; +} + +int ff_nvdec_simple_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, + uint32_t size) +{ + NVDECContext *ctx = avctx->internal->hwaccel_priv_data; + void *tmp; + + tmp = av_fast_realloc(ctx->slice_offsets, &ctx->slice_offsets_allocated, + (ctx->nb_slices + 1) * sizeof(*ctx->slice_offsets)); + if (!tmp) + return AVERROR(ENOMEM); + ctx->slice_offsets = tmp; + + if (!ctx->bitstream) + ctx->bitstream = (uint8_t*)buffer; + + ctx->slice_offsets[ctx->nb_slices] = buffer - ctx->bitstream; + ctx->bitstream_len += size; + ctx->nb_slices++; + + return 0; +} + int ff_nvdec_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx, int dpb_size) @@ -520,3 +550,19 @@ int ff_nvdec_frame_params(AVCodecContext *avctx, return 0; } + +int ff_nvdec_get_ref_idx(AVFrame *frame) +{ + FrameDecodeData *fdd; + NVDECFrame *cf; + + if (!frame || !frame->private_ref) + return -1; + + fdd = (FrameDecodeData*)frame->private_ref->data; + cf = (NVDECFrame*)fdd->hwaccel_priv; + if (!cf) + return -1; + + return cf->idx; +} diff --git a/libavcodec/nvdec.h b/libavcodec/nvdec.h index 14d29ee94b..90578d5a1c 100644 --- a/libavcodec/nvdec.h +++ b/libavcodec/nvdec.h @@ -58,8 +58,12 @@ int ff_nvdec_decode_init(AVCodecContext *avctx); int ff_nvdec_decode_uninit(AVCodecContext *avctx); int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame); int ff_nvdec_end_frame(AVCodecContext *avctx); +int ff_nvdec_simple_end_frame(AVCodecContext *avctx); +int ff_nvdec_simple_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, + uint32_t size); int ff_nvdec_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx, int dpb_size); +int ff_nvdec_get_ref_idx(AVFrame *frame); #endif /* AVCODEC_NVDEC_H */ diff --git a/libavcodec/nvdec_mpeg12.c b/libavcodec/nvdec_mpeg12.c index 127e843d85..db9cebeddd 100644 --- a/libavcodec/nvdec_mpeg12.c +++ b/libavcodec/nvdec_mpeg12.c @@ -25,22 +25,6 @@ #include "nvdec.h" #include "decode.h" -static int get_ref_idx(AVFrame *frame) -{ - FrameDecodeData *fdd; - NVDECFrame *cf; - - if (!frame || !frame->private_ref) - return -1; - - fdd = (FrameDecodeData*)frame->private_ref->data; - cf = (NVDECFrame*)fdd->hwaccel_priv; - if (!cf) - return -1; - - return cf->idx; -} - static int nvdec_mpeg12_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size) { MpegEncContext *s = avctx->priv_data; @@ -71,8 +55,8 @@ static int nvdec_mpeg12_start_frame(AVCodecContext *avctx, const uint8_t *buffer s->pict_type == AV_PICTURE_TYPE_P, .CodecSpecific.mpeg2 = { - .ForwardRefIdx = get_ref_idx(s->last_picture.f), - .BackwardRefIdx = get_ref_idx(s->next_picture.f), + .ForwardRefIdx = ff_nvdec_get_ref_idx(s->last_picture.f), + .BackwardRefIdx = ff_nvdec_get_ref_idx(s->next_picture.f), .picture_coding_type = s->pict_type, .full_pel_forward_vector = s->full_pel[0], @@ -99,35 +83,6 @@ static int nvdec_mpeg12_start_frame(AVCodecContext *avctx, const uint8_t *buffer return 0; } -static int nvdec_mpeg12_end_frame(AVCodecContext *avctx) -{ - NVDECContext *ctx = avctx->internal->hwaccel_priv_data; - int ret = ff_nvdec_end_frame(avctx); - ctx->bitstream = NULL; - return ret; -} - -static int nvdec_mpeg12_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size) -{ - NVDECContext *ctx = avctx->internal->hwaccel_priv_data; - void *tmp; - - tmp = av_fast_realloc(ctx->slice_offsets, &ctx->slice_offsets_allocated, - (ctx->nb_slices + 1) * sizeof(*ctx->slice_offsets)); - if (!tmp) - return AVERROR(ENOMEM); - ctx->slice_offsets = tmp; - - if (!ctx->bitstream) - ctx->bitstream = (uint8_t*)buffer; - - ctx->slice_offsets[ctx->nb_slices] = buffer - ctx->bitstream; - ctx->bitstream_len += size; - ctx->nb_slices++; - - return 0; -} - static int nvdec_mpeg12_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx) { @@ -142,8 +97,8 @@ AVHWAccel ff_mpeg2_nvdec_hwaccel = { .id = AV_CODEC_ID_MPEG2VIDEO, .pix_fmt = AV_PIX_FMT_CUDA, .start_frame = nvdec_mpeg12_start_frame, - .end_frame = nvdec_mpeg12_end_frame, - .decode_slice = nvdec_mpeg12_decode_slice, + .end_frame = ff_nvdec_simple_end_frame, + .decode_slice = ff_nvdec_simple_decode_slice, .frame_params = nvdec_mpeg12_frame_params, .init = ff_nvdec_decode_init, .uninit = ff_nvdec_decode_uninit, diff --git a/libavcodec/nvdec_vc1.c b/libavcodec/nvdec_vc1.c index 588a5b9d07..c04b153a86 100644 --- a/libavcodec/nvdec_vc1.c +++ b/libavcodec/nvdec_vc1.c @@ -25,20 +25,6 @@ #include "decode.h" #include "vc1.h" -static int get_ref_idx(AVFrame *frame) -{ - FrameDecodeData *fdd; - NVDECFrame *cf; - - if (!frame || !frame->private_ref) - return -1; - - fdd = (FrameDecodeData*)frame->private_ref->data; - cf = (NVDECFrame*)fdd->hwaccel_priv; - - return cf->idx; -} - static int nvdec_vc1_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size) { VC1Context *v = avctx->priv_data; @@ -73,8 +59,8 @@ static int nvdec_vc1_start_frame(AVCodecContext *avctx, const uint8_t *buffer, u s->pict_type == AV_PICTURE_TYPE_P, .CodecSpecific.vc1 = { - .ForwardRefIdx = get_ref_idx(s->last_picture.f), - .BackwardRefIdx = get_ref_idx(s->next_picture.f), + .ForwardRefIdx = ff_nvdec_get_ref_idx(s->last_picture.f), + .BackwardRefIdx = ff_nvdec_get_ref_idx(s->next_picture.f), .FrameWidth = cur_frame->width, .FrameHeight = cur_frame->height, @@ -117,35 +103,6 @@ static int nvdec_vc1_start_frame(AVCodecContext *avctx, const uint8_t *buffer, u return 0; } -static int nvdec_vc1_end_frame(AVCodecContext *avctx) -{ - NVDECContext *ctx = avctx->internal->hwaccel_priv_data; - int ret = ff_nvdec_end_frame(avctx); - ctx->bitstream = NULL; - return ret; -} - -static int nvdec_vc1_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size) -{ - NVDECContext *ctx = avctx->internal->hwaccel_priv_data; - void *tmp; - - tmp = av_fast_realloc(ctx->slice_offsets, &ctx->slice_offsets_allocated, - (ctx->nb_slices + 1) * sizeof(*ctx->slice_offsets)); - if (!tmp) - return AVERROR(ENOMEM); - ctx->slice_offsets = tmp; - - if (!ctx->bitstream) - ctx->bitstream = (uint8_t*)buffer; - - ctx->slice_offsets[ctx->nb_slices] = buffer - ctx->bitstream; - ctx->bitstream_len += size; - ctx->nb_slices++; - - return 0; -} - static int nvdec_vc1_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx) { @@ -159,8 +116,8 @@ AVHWAccel ff_vc1_nvdec_hwaccel = { .id = AV_CODEC_ID_VC1, .pix_fmt = AV_PIX_FMT_CUDA, .start_frame = nvdec_vc1_start_frame, - .end_frame = nvdec_vc1_end_frame, - .decode_slice = nvdec_vc1_decode_slice, + .end_frame = ff_nvdec_simple_end_frame, + .decode_slice = ff_nvdec_simple_decode_slice, .frame_params = nvdec_vc1_frame_params, .init = ff_nvdec_decode_init, .uninit = ff_nvdec_decode_uninit, @@ -174,8 +131,8 @@ AVHWAccel ff_wmv3_nvdec_hwaccel = { .id = AV_CODEC_ID_WMV3, .pix_fmt = AV_PIX_FMT_CUDA, .start_frame = nvdec_vc1_start_frame, - .end_frame = nvdec_vc1_end_frame, - .decode_slice = nvdec_vc1_decode_slice, + .end_frame = ff_nvdec_simple_end_frame, + .decode_slice = ff_nvdec_simple_decode_slice, .frame_params = nvdec_vc1_frame_params, .init = ff_nvdec_decode_init, .uninit = ff_nvdec_decode_uninit, diff --git a/libavcodec/nvdec_vp9.c b/libavcodec/nvdec_vp9.c index 06c13e6f03..ed06d91416 100644 --- a/libavcodec/nvdec_vp9.c +++ b/libavcodec/nvdec_vp9.c @@ -28,20 +28,6 @@ #include "internal.h" #include "vp9shared.h" -static unsigned char get_ref_idx(AVFrame *frame) -{ - FrameDecodeData *fdd; - NVDECFrame *cf; - - if (!frame || !frame->private_ref) - return 255; - - fdd = (FrameDecodeData*)frame->private_ref->data; - cf = (NVDECFrame*)fdd->hwaccel_priv; - - return cf->idx; -} - static int nvdec_vp9_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size) { VP9SharedContext *h = avctx->priv_data; @@ -72,9 +58,9 @@ static int nvdec_vp9_start_frame(AVCodecContext *avctx, const uint8_t *buffer, u .width = cur_frame->width, .height = cur_frame->height, - .LastRefIdx = get_ref_idx(h->refs[h->h.refidx[0]].f), - .GoldenRefIdx = get_ref_idx(h->refs[h->h.refidx[1]].f), - .AltRefIdx = get_ref_idx(h->refs[h->h.refidx[2]].f), + .LastRefIdx = ff_nvdec_get_ref_idx(h->refs[h->h.refidx[0]].f), + .GoldenRefIdx = ff_nvdec_get_ref_idx(h->refs[h->h.refidx[1]].f), + .AltRefIdx = ff_nvdec_get_ref_idx(h->refs[h->h.refidx[2]].f), .profile = h->h.profile, .frameContextIdx = h->h.framectxid, @@ -176,35 +162,6 @@ static int nvdec_vp9_start_frame(AVCodecContext *avctx, const uint8_t *buffer, u return 0; } -static int nvdec_vp9_end_frame(AVCodecContext *avctx) -{ - NVDECContext *ctx = avctx->internal->hwaccel_priv_data; - int ret = ff_nvdec_end_frame(avctx); - ctx->bitstream = NULL; - return ret; -} - -static int nvdec_vp9_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size) -{ - NVDECContext *ctx = avctx->internal->hwaccel_priv_data; - void *tmp; - - tmp = av_fast_realloc(ctx->slice_offsets, &ctx->slice_offsets_allocated, - (ctx->nb_slices + 1) * sizeof(*ctx->slice_offsets)); - if (!tmp) - return AVERROR(ENOMEM); - ctx->slice_offsets = tmp; - - if (!ctx->bitstream) - ctx->bitstream = (uint8_t*)buffer; - - ctx->slice_offsets[ctx->nb_slices] = buffer - ctx->bitstream; - ctx->bitstream_len += size; - ctx->nb_slices++; - - return 0; -} - static int nvdec_vp9_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx) { @@ -218,8 +175,8 @@ AVHWAccel ff_vp9_nvdec_hwaccel = { .id = AV_CODEC_ID_VP9, .pix_fmt = AV_PIX_FMT_CUDA, .start_frame = nvdec_vp9_start_frame, - .end_frame = nvdec_vp9_end_frame, - .decode_slice = nvdec_vp9_decode_slice, + .end_frame = ff_nvdec_simple_end_frame, + .decode_slice = ff_nvdec_simple_decode_slice, .frame_params = nvdec_vp9_frame_params, .init = ff_nvdec_decode_init, .uninit = ff_nvdec_decode_uninit,