From patchwork Wed Aug 22 23:44:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 10071 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:12c4:0:0:0:0:0 with SMTP id 65-v6csp1442061jap; Wed, 22 Aug 2018 16:47:52 -0700 (PDT) X-Google-Smtp-Source: ANB0VdaTRcP/umDJIkUaRq5ZuStAI1kpVmpnIVCbVsaLc51foFmIK9n5glLOVRpFymD5stcbR9K0 X-Received: by 2002:adf:9281:: with SMTP id 1-v6mr8167818wrn.69.1534981672812; Wed, 22 Aug 2018 16:47:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534981672; cv=none; d=google.com; s=arc-20160816; b=kVlKe+nwYuYv1LLdc2ZCy3LCpHFHCyyiMWxP4jxhNCwyxY864WO+IlqoHYseprhEml lnAU5LzRBIYtIcXMWNS1qJLFkAC8zi9RaQgAnS38rWDPcO1Klb+vKkt5wcrqRnC7vmmW dYGU4VGlswOHApu3Tr7uxVpO7WR9mwWwnAbpCk2KPcy9nCpi92IJ3TBF6qQRIKQiU0AT SkjkHv9yicqTqIKjQ7qLgzCm/DI5wIKD1kNiEz53cjpF/Ybd2l4EGKyf6crahGYNUfmC ZzcksgW/DIiKVZtg1sqHixIN7JS42PbuNVNHUVQs9pcYKytVj0+LvXe7bM0Eq+FDslSI Ypgw== 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=AsKmDsMsE1dhV88diyOih+SG24vVOvFU+l0gZQRd4b4=; b=s/sf6JZ5fUuBcIIrnUfbvyVc07TRX6/gTt7nYEjXpgHeeqEWxeTqdG5bIqERn08hVC lhR5lf4OMfjE0MyLGD6tOKLzYoNzwPA36d6t+Bb5u0imerz3Gw6/lZkD8vJ21E9/BZHu fRf3c4QkL+nzKRC6h7r3AV203BpXeqWkTXOCFrgzrIVwW3OgfujRWUJUY+2i4Xcz8hXX ayGJFIZNT04yTScvI08SsJJJ8NtSrC+jJdXqQ6HgosbcRyJCTH5w9lJO25k4kuf48bqK rH7pOpckq5c12+F4kLLsgURvY8FYW1Ry0tbEVLxNMVvq/tYX5PuLqx8fYY1AkXa062H2 2AiQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@jkqxz-net.20150623.gappssmtp.com header.s=20150623 header.b=XjmFXjAR; 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 t126-v6si2233193wmb.80.2018.08.22.16.47.52; Wed, 22 Aug 2018 16:47:52 -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=@jkqxz-net.20150623.gappssmtp.com header.s=20150623 header.b=XjmFXjAR; 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 60E63689B9B; Thu, 23 Aug 2018 02:45:48 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f45.google.com (mail-wm0-f45.google.com [74.125.82.45]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 19598689EAA for ; Thu, 23 Aug 2018 02:45:45 +0300 (EEST) Received: by mail-wm0-f45.google.com with SMTP id s12-v6so3476760wmc.0 for ; Wed, 22 Aug 2018 16:45:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jkqxz-net.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references; bh=0NU7fUrZ3PPlJ9DgaLjRHKHuRtiWUZeBdWnpIos0P5U=; b=XjmFXjARsZTuOp+LtyvwifYr9/iCcbd/8dU0u9VDdyWxaswNcq1ZHNUgcSvjsxytuU nnIDHfLcdcHuGLGlLYKT3cMVolkN6WaTXAvHWp3LXN7aDwFuukglPup5ko2MUQffrUzr Q+K5f1zqaSToOhz8jp6rHvSw9WZZdgNVUViHS+btSrlu+0tHt59DYkBFJuHGPUItlaig 5YzqG8099H0XxvWTkMrqnvOmzjH62Wrkzexy9EE+0l9HT+eT1voNAyvUywp39MdX+3Va jh8UGueL0eVF1VgylC0RzMjplJi4rpgFMNnLmICS6VuMhRQRLf+fhGLVgd/Y4TfFG5P8 muIA== 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=0NU7fUrZ3PPlJ9DgaLjRHKHuRtiWUZeBdWnpIos0P5U=; b=Gzp8hZ8I0teBa53jGDeYK3k2N4Jc2BeCQ7YZVRnTzlpBUOpDmRGCDV5/Wz04v6fkQP ZEeLIl55755xJZl9TbrdVWhz8bC1g9KjYpLdI1PyqBDpiyijD78bwoc07zMzpL4w1TvE 3TRntpoRRvHpC7Q2nNQeE3f3A328Pbzqj6VuyZlMTGLnKkZQKkMhQvS9JSyfzL2jh7tX UuZY2XN/TWJJKgOIMpIaIS9gS1uUEVB5Lguylw26enrj47qUPSLigV7EzqqzfDcFn3Dy yExy6NNdFkVjUF9fcBPr+Uykew+8gi8NkU4XJCEhjDGQMe5Ox1b1juQuAyLwmkjhWnOZ QHHw== X-Gm-Message-State: APzg51BIqORjBQXQIMLtsL/zntOKqDxokuBpUDo68l0Z+pn1/8dSzXEH gWaKx3DsEG4TpJant1YoqovvcLMNbaI= X-Received: by 2002:a1c:ef0f:: with SMTP id n15-v6mr3437418wmh.116.1534981546401; Wed, 22 Aug 2018 16:45:46 -0700 (PDT) Received: from rywe.jkqxz.net (cpc91242-cmbg18-2-0-cust650.5-4.cable.virginm.net. [82.8.130.139]) by smtp.gmail.com with ESMTPSA id x82-v6sm8184537wmd.11.2018.08.22.16.45.45 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 22 Aug 2018 16:45:45 -0700 (PDT) From: Mark Thompson To: ffmpeg-devel@ffmpeg.org Date: Thu, 23 Aug 2018 00:44:58 +0100 Message-Id: <20180822234514.10571-26-sw@jkqxz.net> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180822234514.10571-1-sw@jkqxz.net> References: <20180822234514.10571-1-sw@jkqxz.net> Subject: [FFmpeg-devel] [PATCH v3 25/41] hwcontext_vaapi: Improve format mapping 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" Give the entries in the VAAPI format map table an explicit type and add functions to do the necessary lookups. Add another field to this table indicating whether the chroma planes are swapped (as in YV12), and use that rather than explicit comparisons where swapping is needed. --- libavutil/hwcontext_vaapi.c | 133 ++++++++++++++++++++---------------- 1 file changed, 76 insertions(+), 57 deletions(-) diff --git a/libavutil/hwcontext_vaapi.c b/libavutil/hwcontext_vaapi.c index 4088a96be4..8624369bb9 100644 --- a/libavutil/hwcontext_vaapi.c +++ b/libavutil/hwcontext_vaapi.c @@ -87,57 +87,82 @@ typedef struct VAAPIMapping { int flags; } VAAPIMapping; -#define MAP(va, rt, av) { \ +typedef struct VAAPIFormat { + unsigned int fourcc; + unsigned int rt_format; + enum AVPixelFormat pix_fmt; + int chroma_planes_swapped; +} VAAPIFormatDescriptor; + +#define MAP(va, rt, av, swap_uv) { \ VA_FOURCC_ ## va, \ VA_RT_FORMAT_ ## rt, \ - AV_PIX_FMT_ ## av \ + AV_PIX_FMT_ ## av, \ + swap_uv, \ } // The map fourcc <-> pix_fmt isn't bijective because of the annoying U/V // plane swap cases. The frame handling below tries to hide these. -static const struct { - unsigned int fourcc; - unsigned int rt_format; - enum AVPixelFormat pix_fmt; -} vaapi_format_map[] = { - MAP(NV12, YUV420, NV12), - MAP(YV12, YUV420, YUV420P), // With U/V planes swapped. - MAP(IYUV, YUV420, YUV420P), +static const VAAPIFormatDescriptor vaapi_format_map[] = { + MAP(NV12, YUV420, NV12, 0), #ifdef VA_FOURCC_I420 - MAP(I420, YUV420, YUV420P), + MAP(I420, YUV420, YUV420P, 0), #endif + MAP(YV12, YUV420, YUV420P, 1), + MAP(IYUV, YUV420, YUV420P, 0), + MAP(422H, YUV422, YUV422P, 0), #ifdef VA_FOURCC_YV16 - MAP(YV16, YUV422, YUV422P), // With U/V planes swapped. + MAP(YV16, YUV422, YUV422P, 1), #endif - MAP(422H, YUV422, YUV422P), - MAP(UYVY, YUV422, UYVY422), - MAP(YUY2, YUV422, YUYV422), - MAP(411P, YUV411, YUV411P), - MAP(422V, YUV422, YUV440P), - MAP(444P, YUV444, YUV444P), - MAP(Y800, YUV400, GRAY8), + MAP(UYVY, YUV422, UYVY422, 0), + MAP(YUY2, YUV422, YUYV422, 0), + MAP(411P, YUV411, YUV411P, 0), + MAP(422V, YUV422, YUV440P, 0), + MAP(444P, YUV444, YUV444P, 0), + MAP(Y800, YUV400, GRAY8, 0), #ifdef VA_FOURCC_P010 - MAP(P010, YUV420_10BPP, P010), + MAP(P010, YUV420_10BPP, P010, 0), #endif - MAP(BGRA, RGB32, BGRA), - MAP(BGRX, RGB32, BGR0), - MAP(RGBA, RGB32, RGBA), - MAP(RGBX, RGB32, RGB0), + MAP(BGRA, RGB32, BGRA, 0), + MAP(BGRX, RGB32, BGR0, 0), + MAP(RGBA, RGB32, RGBA, 0), + MAP(RGBX, RGB32, RGB0, 0), #ifdef VA_FOURCC_ABGR - MAP(ABGR, RGB32, ABGR), - MAP(XBGR, RGB32, 0BGR), + MAP(ABGR, RGB32, ABGR, 0), + MAP(XBGR, RGB32, 0BGR, 0), #endif - MAP(ARGB, RGB32, ARGB), - MAP(XRGB, RGB32, 0RGB), + MAP(ARGB, RGB32, ARGB, 0), + MAP(XRGB, RGB32, 0RGB, 0), }; #undef MAP -static enum AVPixelFormat vaapi_pix_fmt_from_fourcc(unsigned int fourcc) +static const VAAPIFormatDescriptor * + vaapi_format_from_fourcc(unsigned int fourcc) { int i; for (i = 0; i < FF_ARRAY_ELEMS(vaapi_format_map); i++) if (vaapi_format_map[i].fourcc == fourcc) - return vaapi_format_map[i].pix_fmt; - return AV_PIX_FMT_NONE; + return &vaapi_format_map[i]; + return NULL; +} + +static const VAAPIFormatDescriptor * + vaapi_format_from_pix_fmt(enum AVPixelFormat pix_fmt) +{ + int i; + for (i = 0; i < FF_ARRAY_ELEMS(vaapi_format_map); i++) + if (vaapi_format_map[i].pix_fmt == pix_fmt) + return &vaapi_format_map[i]; + return NULL; +} + +static enum AVPixelFormat vaapi_pix_fmt_from_fourcc(unsigned int fourcc) +{ + const VAAPIFormatDescriptor *desc; + desc = vaapi_format_from_fourcc(fourcc); + if (desc) + return desc->pix_fmt; + else + return AV_PIX_FMT_NONE; } static int vaapi_get_image_format(AVHWDeviceContext *hwdev, @@ -461,22 +486,16 @@ static int vaapi_frames_init(AVHWFramesContext *hwfc) AVVAAPIFramesContext *avfc = hwfc->hwctx; VAAPIFramesContext *ctx = hwfc->internal->priv; AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx; + const VAAPIFormatDescriptor *desc; VAImageFormat *expected_format; AVBufferRef *test_surface = NULL; VASurfaceID test_surface_id; VAImage test_image; VAStatus vas; int err, i; - unsigned int fourcc, rt_format; - for (i = 0; i < FF_ARRAY_ELEMS(vaapi_format_map); i++) { - if (vaapi_format_map[i].pix_fmt == hwfc->sw_format) { - fourcc = vaapi_format_map[i].fourcc; - rt_format = vaapi_format_map[i].rt_format; - break; - } - } - if (i >= FF_ARRAY_ELEMS(vaapi_format_map)) { + desc = vaapi_format_from_pix_fmt(hwfc->sw_format); + if (!desc) { av_log(hwfc, AV_LOG_ERROR, "Unsupported format: %s.\n", av_get_pix_fmt_name(hwfc->sw_format)); return AVERROR(EINVAL); @@ -517,7 +536,7 @@ static int vaapi_frames_init(AVHWFramesContext *hwfc) .type = VASurfaceAttribPixelFormat, .flags = VA_SURFACE_ATTRIB_SETTABLE, .value.type = VAGenericValueTypeInteger, - .value.value.i = fourcc, + .value.value.i = desc->fourcc, }; } av_assert0(i == ctx->nb_attributes); @@ -526,7 +545,7 @@ static int vaapi_frames_init(AVHWFramesContext *hwfc) ctx->nb_attributes = 0; } - ctx->rt_format = rt_format; + ctx->rt_format = desc->rt_format; if (hwfc->initial_pool_size > 0) { // This pool will be usable as a render target, so we need to store @@ -716,6 +735,7 @@ static int vaapi_map_frame(AVHWFramesContext *hwfc, AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx; VAAPIFramesContext *ctx = hwfc->internal->priv; VASurfaceID surface_id; + const VAAPIFormatDescriptor *desc; VAImageFormat *image_format; VAAPIMapping *map; VAStatus vas; @@ -824,11 +844,9 @@ static int vaapi_map_frame(AVHWFramesContext *hwfc, dst->data[i] = (uint8_t*)address + map->image.offsets[i]; dst->linesize[i] = map->image.pitches[i]; } - if ( -#ifdef VA_FOURCC_YV16 - map->image.format.fourcc == VA_FOURCC_YV16 || -#endif - map->image.format.fourcc == VA_FOURCC_YV12) { + + desc = vaapi_format_from_fourcc(map->image.format.fourcc); + if (desc && desc->chroma_planes_swapped) { // Chroma planes are YVU rather than YUV, so swap them. FFSWAP(uint8_t*, dst->data[1], dst->data[2]); } @@ -981,9 +999,10 @@ static int vaapi_map_from_drm(AVHWFramesContext *src_fc, AVFrame *dst, (AVHWFramesContext*)dst->hw_frames_ctx->data; AVVAAPIDeviceContext *dst_dev = dst_fc->device_ctx->hwctx; const AVDRMFrameDescriptor *desc; + const VAAPIFormatDescriptor *format_desc; VASurfaceID surface_id; VAStatus vas; - uint32_t va_fourcc, va_rt_format; + uint32_t va_fourcc; int err, i, j, k; unsigned long buffer_handle; @@ -1034,14 +1053,8 @@ static int vaapi_map_from_drm(AVHWFramesContext *src_fc, AVFrame *dst, av_log(dst_fc, AV_LOG_DEBUG, "Map DRM object %d to VAAPI as " "%08x.\n", desc->objects[0].fd, va_fourcc); - for (i = 0; i < FF_ARRAY_ELEMS(vaapi_format_map); i++) { - if (vaapi_format_map[i].fourcc == va_fourcc) { - va_rt_format = vaapi_format_map[i].rt_format; - break; - } - } - - av_assert0(i < FF_ARRAY_ELEMS(vaapi_format_map)); + format_desc = vaapi_format_from_fourcc(va_fourcc); + av_assert0(format_desc); buffer_handle = desc->objects[0].fd; buffer_desc.pixel_format = va_fourcc; @@ -1062,7 +1075,13 @@ static int vaapi_map_from_drm(AVHWFramesContext *src_fc, AVFrame *dst, } buffer_desc.num_planes = k; - vas = vaCreateSurfaces(dst_dev->display, va_rt_format, + if (format_desc->chroma_planes_swapped && + buffer_desc.num_planes == 3) { + FFSWAP(uint32_t, buffer_desc.pitches[1], buffer_desc.pitches[2]); + FFSWAP(uint32_t, buffer_desc.offsets[1], buffer_desc.offsets[2]); + } + + vas = vaCreateSurfaces(dst_dev->display, format_desc->rt_format, src->width, src->height, &surface_id, 1, attrs, FF_ARRAY_ELEMS(attrs));