From patchwork Tue Nov 14 19:47:23 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 6068 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.161.94 with SMTP id m30csp3722259jah; Tue, 14 Nov 2017 11:49:37 -0800 (PST) X-Google-Smtp-Source: AGs4zMbgLwWsbm8SLSm+X7NfEVyAvNpt/AxHww4l9GAu2o+nUkqwPLznqizDj7uw+ilfYluIMUcO X-Received: by 10.223.166.103 with SMTP id k94mr11169005wrc.22.1510688977068; Tue, 14 Nov 2017 11:49:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510688977; cv=none; d=google.com; s=arc-20160816; b=i48bDTpCIT/947RPNTMLA3HF60v8ytmOObLmbitT8qTdpVbyk7hpoaaeiWvtHfNIf4 HSkATJLBCkyytWOcfXNSbJ7LdVC4l+etd5ap6lPEBfQ2oCUEPtHOoQPHiCkiZEvgmpof cSukSvJ+6Z/xgAZViSn/gP02J9k+GjT32BHEXhKi65yvr9ekWz9wIfNm8ICC2F4C8iJt +xlSzxOncbjqb4jLpr1GtLHZuK7yazcRERzg4/tJbKKrR+rDSoEDjLtKUwXJ5xpboGPG y/bplIz2U7HZmGGO/x3hZ8+mLaOpclFBslRMey3F5V6Ck9DZRWg9r+KxdFCteLe6jz7e LNsg== 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=+/i8cyEyobGewtIlarb6sZa3mgrK/SFt9qRb5CNcIZ4=; b=Wleg3oW9qxPt4beDzEXKKehOSxyFMxAw436MCT1QUPm9ayXycoObV5q7ORvk7ZY6b0 7S0SCfDglVvQfeo0dZOIsRou6Ef7RXWVcMNXFxipdL2fn1k42YyhPh/sTPx/QIBwRZYX DmxvllnWrEsL+kEdO2nrlXlYoFEdnCKdSCnspNowjpf90qLNCUYL0kSKEqqIbkOwxCeS cBuj1sXoqO/nCmqbGVKV+m6OHrk4e2wZtsRzmxit47JNsOUaYyT6xtAkOtGAB8y99KOA mmdIAFEJt3W1598BM1jnx6Vj81Txy93orPHvPC3tdFxSb71MS2nntnzknYbheHsXroId 2Qvw== 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=STVKM487; 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 w83si280005wma.41.2017.11.14.11.49.36; Tue, 14 Nov 2017 11:49:37 -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=@jkqxz-net.20150623.gappssmtp.com header.s=20150623 header.b=STVKM487; 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 B793268A1C3; Tue, 14 Nov 2017 21:47:37 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr0-f173.google.com (mail-wr0-f173.google.com [209.85.128.173]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5B0DC68A151 for ; Tue, 14 Nov 2017 21:47:28 +0200 (EET) Received: by mail-wr0-f173.google.com with SMTP id y42so18529681wrd.3 for ; Tue, 14 Nov 2017 11:47:43 -0800 (PST) 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=7cMweCZB/nJPFKFC4UJC3PxVFwE+xli5/p+2DoUbd6c=; b=STVKM487lCuYBqo6AucsFy7x5azrt58UJ9HpHWbmKeOCDt2J/TwBKn0OAmVQsrY1K1 KZ4m7SRKHnfViakDc+RNS2Z+0VuynfbHdt9SLA2076Qzcs9jlPop94CJgchYddwawIoj 1uhvSUnYYUceRlZ5RbL/3FuWbt5uVU7OmrXZFSRLimC0sNMUlDOCbDErd9rPevNppxp9 0G9lANoXHkZqUMfnF0BhzD1zQ1ISQ04Glu+8EY+DRZOtWDrmZkOVS5ak+k7+XPev0Lx2 SEp57mX0zXfaqlgDTFLmx9BUdtG8XR31ZSWPgHhcr1S06AiKNzP1Z6igYl7cwiDgELjF 2VNg== 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=7cMweCZB/nJPFKFC4UJC3PxVFwE+xli5/p+2DoUbd6c=; b=FaQG7fZpoB9/XMSvENkLNjJ6TqnUZ+Ku/6fcHJaeizI5AVWyLKyJn4uFcc365GQRQd ElsRD4rWQcUxGuI8S/CCtdmPsWM4I5tKpUR/uOE+shL59v0yKvwCICcfncZjVg7NdWey HG0Ff1lRs00+6IOXx29kPuo7pUya5FqttY64Rzspn+b6whLX/0GQSgaU4icAWejzP8Xj DO9MHurFgUqCRzC6GHXjgCC3QjI70WUu9BU2UmncExhWSdgVZ7c3AruaZ7IlQDTAbrwF EX+d89hTedrlZLDAu6MD0Ln7NfxBB3Bdke+pnMEJCOkFPweczeGdC0Njh6WF4MhXrU4q /NKg== X-Gm-Message-State: AJaThX6goZl+Za0At5ZrD8WjlY7GWKRzrfFYlQGB58SnIR2ISzmdj5ZJ hLN/TXmV41yG94QRVzz92fimCeNi X-Received: by 10.223.170.143 with SMTP id h15mr11963814wrc.49.1510688862812; Tue, 14 Nov 2017 11:47:42 -0800 (PST) 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 v35sm38938226wrc.13.2017.11.14.11.47.41 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 14 Nov 2017 11:47:41 -0800 (PST) From: Mark Thompson To: ffmpeg-devel@ffmpeg.org Date: Tue, 14 Nov 2017 19:47:23 +0000 Message-Id: <20171114194730.11052-9-sw@jkqxz.net> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171114194730.11052-1-sw@jkqxz.net> References: <20171114194730.11052-1-sw@jkqxz.net> Subject: [FFmpeg-devel] [PATCH 08/15] lavfi: Add some common code for OpenCL filtering 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" --- libavfilter/opencl.c | 251 +++++++++++++++++++++++++++++++++++++++++++++++++++ libavfilter/opencl.h | 87 ++++++++++++++++++ 2 files changed, 338 insertions(+) create mode 100644 libavfilter/opencl.c create mode 100644 libavfilter/opencl.h diff --git a/libavfilter/opencl.c b/libavfilter/opencl.c new file mode 100644 index 0000000000..005ad089e2 --- /dev/null +++ b/libavfilter/opencl.c @@ -0,0 +1,251 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include + +#include "libavutil/hwcontext.h" +#include "libavutil/hwcontext_opencl.h" +#include "libavutil/mem.h" + +#include "avfilter.h" +#include "formats.h" +#include "opencl.h" + +int ff_opencl_filter_query_formats(AVFilterContext *avctx) +{ + const static enum AVPixelFormat pix_fmts[] = { + AV_PIX_FMT_OPENCL, + AV_PIX_FMT_NONE, + }; + AVFilterFormats *formats; + + formats = ff_make_format_list(pix_fmts); + if (!formats) + return AVERROR(ENOMEM); + + return ff_set_common_formats(avctx, formats); +} + +int ff_opencl_filter_config_input(AVFilterLink *inlink) +{ + AVFilterContext *avctx = inlink->dst; + OpenCLFilterContext *ctx = avctx->priv; + AVHWFramesContext *input_frames; + + if (!inlink->hw_frames_ctx) { + av_log(avctx, AV_LOG_ERROR, "OpenCL filtering requires a " + "hardware frames context on the input.\n"); + return AVERROR(EINVAL); + } + + // Extract the device and default output format from the first input. + if (avctx->inputs[0] != inlink) + return 0; + + input_frames = (AVHWFramesContext*)inlink->hw_frames_ctx->data; + + if (input_frames->format != AV_PIX_FMT_OPENCL) + return AVERROR(EINVAL); + + ctx->device_ref = av_buffer_ref(input_frames->device_ref); + if (!ctx->device_ref) + return AVERROR(ENOMEM); + ctx->device = input_frames->device_ctx; + ctx->hwctx = ctx->device->hwctx; + + // Default output parameters match input parameters. + if (ctx->output_format == AV_PIX_FMT_NONE) + ctx->output_format = input_frames->sw_format; + if (!ctx->output_width) + ctx->output_width = inlink->w; + if (!ctx->output_height) + ctx->output_height = inlink->h; + + return 0; +} + +int ff_opencl_filter_config_output(AVFilterLink *outlink) +{ + AVFilterContext *avctx = outlink->src; + OpenCLFilterContext *ctx = avctx->priv; + AVBufferRef *output_frames_ref = NULL; + AVHWFramesContext *output_frames; + int err; + + av_buffer_unref(&outlink->hw_frames_ctx); + + output_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref); + if (!output_frames_ref) { + err = AVERROR(ENOMEM); + goto fail; + } + output_frames = (AVHWFramesContext*)output_frames_ref->data; + + output_frames->format = AV_PIX_FMT_OPENCL; + output_frames->sw_format = ctx->output_format; + output_frames->width = ctx->output_width; + output_frames->height = ctx->output_height; + + err = av_hwframe_ctx_init(output_frames_ref); + if (err < 0) { + av_log(avctx, AV_LOG_ERROR, "Failed to initialise output " + "frames: %d.\n", err); + goto fail; + } + + outlink->hw_frames_ctx = output_frames_ref; + outlink->w = ctx->output_width; + outlink->h = ctx->output_height; + + return 0; +fail: + av_buffer_unref(&output_frames_ref); + return err; +} + +int ff_opencl_filter_init(AVFilterContext *avctx) +{ + OpenCLFilterContext *ctx = avctx->priv; + + ctx->output_format = AV_PIX_FMT_NONE; + + return 0; +} + +void ff_opencl_filter_uninit(AVFilterContext *avctx) +{ + OpenCLFilterContext *ctx = avctx->priv; + cl_int cle; + + if (ctx->program) { + cle = clReleaseProgram(ctx->program); + if (cle != CL_SUCCESS) + av_log(avctx, AV_LOG_ERROR, "Failed to release " + "program: %d.\n", cle); + } + + av_buffer_unref(&ctx->device_ref); +} + +int ff_opencl_filter_load_program(AVFilterContext *avctx, + const char **program_source_array, + int nb_strings) +{ + OpenCLFilterContext *ctx = avctx->priv; + cl_int cle; + + ctx->program = clCreateProgramWithSource(ctx->hwctx->context, nb_strings, + program_source_array, + NULL, &cle); + if (!ctx->program) { + av_log(avctx, AV_LOG_ERROR, "Failed to create program: %d.\n", cle); + return AVERROR(EIO); + } + + cle = clBuildProgram(ctx->program, 1, &ctx->hwctx->device_id, + NULL, NULL, NULL); + if (cle != CL_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, "Failed to build program: %d.\n", cle); + + if (cle == CL_BUILD_PROGRAM_FAILURE) { + char *log; + size_t log_length; + + clGetProgramBuildInfo(ctx->program, ctx->hwctx->device_id, + CL_PROGRAM_BUILD_LOG, 0, NULL, &log_length); + + log = av_malloc(log_length); + if (log) { + cle = clGetProgramBuildInfo(ctx->program, + ctx->hwctx->device_id, + CL_PROGRAM_BUILD_LOG, + log_length, log, NULL); + if (cle == CL_SUCCESS) + av_log(avctx, AV_LOG_ERROR, "Build log:\n%s\n", log); + } + + av_free(log); + } + + clReleaseProgram(ctx->program); + ctx->program = NULL; + return AVERROR(EIO); + } + + return 0; +} + +int ff_opencl_filter_load_program_from_file(AVFilterContext *avctx, + const char *filename) +{ + FILE *file; + char *src = NULL; + size_t pos, len, rb; + const char *src_const; + int err; + + file = fopen(filename, "r"); + if (!file) { + av_log(avctx, AV_LOG_ERROR, "Unable to open program " + "source file \"%s\".\n", filename); + return AVERROR(ENOENT); + } + + len = 1 << 16; + pos = 0; + + err = av_reallocp(&src, len); + if (err < 0) + goto fail; + + err = snprintf(src, len, "#line 1 \"%s\"\n", filename); + if (err < 0) { + err = AVERROR(errno); + goto fail; + } + if (err > len / 2) { + err = AVERROR(EINVAL); + goto fail; + } + pos = err; + + while (1) { + rb = fread(src + pos, 1, len - pos - 1, file); + if (rb == 0 && ferror(file)) { + err = AVERROR(EIO); + goto fail; + } + pos += rb; + if (pos < len) + break; + len <<= 1; + err = av_reallocp(&src, len); + if (err < 0) + goto fail; + } + src[pos] = 0; + + src_const = src; + + err = ff_opencl_filter_load_program(avctx, &src_const, 1); +fail: + fclose(file); + av_freep(&src); + return err; +} diff --git a/libavfilter/opencl.h b/libavfilter/opencl.h new file mode 100644 index 0000000000..4d740c18ab --- /dev/null +++ b/libavfilter/opencl.h @@ -0,0 +1,87 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVFILTER_OPENCL_H +#define AVFILTER_OPENCL_H + +#include "libavutil/buffer.h" +#include "libavutil/hwcontext.h" +#include "libavutil/hwcontext_opencl.h" +#include "libavutil/pixfmt.h" + +#include "avfilter.h" + +typedef struct OpenCLFilterContext { + const AVClass *class; + + AVBufferRef *device_ref; + AVHWDeviceContext *device; + AVOpenCLDeviceContext *hwctx; + + cl_program program; + + enum AVPixelFormat output_format; + int output_width; + int output_height; +} OpenCLFilterContext; + +/** + * Return that all inputs and outputs support only AV_PIX_FMT_OPENCL. + */ +int ff_opencl_filter_query_formats(AVFilterContext *avctx); + +/** + * Check that the input link contains a suitable hardware frames + * context and extract the device from it. + */ +int ff_opencl_filter_config_input(AVFilterLink *inlink); + +/** + * Create a suitable hardware frames context for the output. + */ +int ff_opencl_filter_config_output(AVFilterLink *outlink); + +/** + * Initialise an OpenCL filter context. + */ +int ff_opencl_filter_init(AVFilterContext *avctx); + +/** + * Uninitialise an OpenCL filter context. + */ +void ff_opencl_filter_uninit(AVFilterContext *avctx); + +/** + * Load a new OpenCL program from strings in memory. + * + * Creates a new program and compiles it for the current device. + * Will log any build errors if compilation fails. + */ +int ff_opencl_filter_load_program(AVFilterContext *avctx, + const char **program_source_array, + int nb_strings); + +/** + * Load a new OpenCL program from a file. + * + * Same as ff_opencl_filter_load_program(), but from a file. + */ +int ff_opencl_filter_load_program_from_file(AVFilterContext *avctx, + const char *filename); + +#endif /* AVFILTER_OPENCL_H */