From patchwork Tue Jan 31 19:14:56 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aman Karmani X-Patchwork-Id: 2382 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp2091904vsb; Tue, 31 Jan 2017 11:20:48 -0800 (PST) X-Received: by 10.28.22.146 with SMTP id 140mr18990888wmw.22.1485890448315; Tue, 31 Jan 2017 11:20:48 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id l187si1101385wml.150.2017.01.31.11.20.47; Tue, 31 Jan 2017 11:20:48 -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=@tmm1-net.20150623.gappssmtp.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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C468768A593; Tue, 31 Jan 2017 21:20:42 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf0-f196.google.com (mail-pf0-f196.google.com [209.85.192.196]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 206B568A572 for ; Tue, 31 Jan 2017 21:20:35 +0200 (EET) Received: by mail-pf0-f196.google.com with SMTP id 19so29290187pfo.3 for ; Tue, 31 Jan 2017 11:20:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tmm1-net.20150623.gappssmtp.com; s=20150623; h=sender:from:to:cc:subject:date:message-id; bh=TOvoffY9wQDLDKZyOk9LaWAzWBcFdV4quPy6adHmPI8=; b=zirMSUApL+aMD6R616GAFY4wv7C7gUuLfjz0tC7qIeY0hhftatT8/93yC3ZPKeYDuO 18+eeERwKKlOmA6PX731ES85YdeBC1X4u6tW/jCScBuvvH1BoWE34/5NVw9Tb1vOlu1M 66ACfIaJZaG6QzhwKkCZfPCjMDvy3wVfR4vZlP5shQnB0J0usUH2E0gz6rCh5q6I47Pl UMe2DbG2H6URteOWo0znuH7sLOWZSiHi3RtUET+KvHEu3bmPEjti6UVrlkDfxxlNR4yJ MKH+2WcOZ7qfNvH/j8jh6STmq8uIutpqxXKqjdmnnrz1wtgQPSxIbhB8iyIK78MeW7NB p/NQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id; bh=TOvoffY9wQDLDKZyOk9LaWAzWBcFdV4quPy6adHmPI8=; b=a+mGyQNdlFzLG1Fu31095Mfvobj9p2HFpe2Xg2MdxfG4mqfkswGeioHIIsIJQAanWM DZ5+7nWawsqlW1M2EBIM7k6nc//JzL2m1C0GFgU+BB1Ox0uPhj3fdBCG/DRi10SG3ByP nryRw6U67QEDfVjPFxSv4FEMJlBeonTFmOkhENRstPPsQVfDyJ8hH80/vlB1oTUFnw9z WqQDZ36KwzZYbOdb67CsgYFf98fO8GCjqOSqEOiMgEGqGcWBewlNUux4camOUALQRXdv w29WPUWI3XoFry9aiHDBhPGtLhqZj3XxnR+G8wHmU3jUV1IZaZUGz2Ns7zSKXBzRpa/e mVvQ== X-Gm-Message-State: AIkVDXI0QN98E2MrSToR72rnBZ+ye9CqtsB3jWMW1G1ByDWoBI1vwswtQU4R3msWW+IgUQ== X-Received: by 10.99.134.199 with SMTP id x190mr32550383pgd.68.1485890103813; Tue, 31 Jan 2017 11:15:03 -0800 (PST) Received: from tmm1-macbook.local.net (c-73-252-174-83.hsd1.ca.comcast.net. [73.252.174.83]) by smtp.gmail.com with ESMTPSA id j128sm43100635pfg.73.2017.01.31.11.15.02 (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 31 Jan 2017 11:15:03 -0800 (PST) From: Aman Gupta To: ffmpeg-devel@ffmpeg.org Date: Tue, 31 Jan 2017 11:14:56 -0800 Message-Id: <20170131191456.59393-1-ffmpeg@tmm1.net> X-Mailer: git-send-email 2.10.1 (Apple Git-78) Subject: [FFmpeg-devel] [PATCH] avfilter/scale_vaapi: add support for basic height/width expressions 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: Aman Gupta MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Aman Gupta Copied directly from vf_scale.c. Implements the same expression logic, however not all the variables were copied over. This patch was sufficient for my particular use-case `scale_vaapi=-2:min(ih\,720)`, but perhaps it makes sense to add support for the remaining variables and pull out shared code into a new vf_scale_common.c? --- libavfilter/vf_scale_vaapi.c | 98 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 95 insertions(+), 3 deletions(-) diff --git a/libavfilter/vf_scale_vaapi.c b/libavfilter/vf_scale_vaapi.c index d1cb246..0d1e1b3 100644 --- a/libavfilter/vf_scale_vaapi.c +++ b/libavfilter/vf_scale_vaapi.c @@ -22,6 +22,7 @@ #include #include "libavutil/avassert.h" +#include "libavutil/eval.h" #include "libavutil/hwcontext.h" #include "libavutil/hwcontext_vaapi.h" #include "libavutil/mem.h" @@ -32,6 +33,22 @@ #include "formats.h" #include "internal.h" +static const char *const var_names[] = { + "in_w", "iw", + "in_h", "ih", + "out_w", "ow", + "out_h", "oh", + NULL +}; + +enum var_name { + VAR_IN_W, VAR_IW, + VAR_IN_H, VAR_IH, + VAR_OUT_W, VAR_OW, + VAR_OUT_H, VAR_OH, + VARS_NB +}; + typedef struct ScaleVAAPIContext { const AVClass *class; @@ -50,9 +67,21 @@ typedef struct ScaleVAAPIContext { char *output_format_string; enum AVPixelFormat output_format; + + /** + * New dimensions. Special values are: + * 0 = original width/height + * -1 = keep original aspect + * -N = try to keep aspect but make sure it is divisible by N + */ + int w, h; + + char *w_expr; ///< width expression string + char *h_expr; ///< height expression string + + /* computed width/height */ int output_width; int output_height; - } ScaleVAAPIContext; @@ -118,6 +147,14 @@ static int scale_vaapi_config_output(AVFilterLink *outlink) VAStatus vas; int err, i; + AVFilterLink *inlink = outlink->src->inputs[0]; + ScaleVAAPIContext *scale = ctx; + int64_t w, h; + double var_values[VARS_NB], res; + char *expr; + int ret; + int factor_w, factor_h; + scale_vaapi_pipeline_uninit(ctx); ctx->device_ref = av_buffer_ref(ctx->input_frames->device_ref); @@ -162,6 +199,61 @@ static int scale_vaapi_config_output(AVFilterLink *outlink) } } + var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w; + var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h; + var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN; + var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN; + + /* evaluate width and height */ + av_expr_parse_and_eval(&res, (expr = scale->w_expr), + var_names, var_values, + NULL, NULL, NULL, NULL, NULL, 0, ctx); + scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res; + if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr), + var_names, var_values, + NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) + goto fail; + scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res; + /* evaluate again the width, as it may depend on the output height */ + if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr), + var_names, var_values, + NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) + goto fail; + scale->w = res; + + w = scale->w; + h = scale->h; + + /* Check if it is requested that the result has to be divisible by a some + * factor (w or h = -n with n being the factor). */ + factor_w = 1; + factor_h = 1; + if (w < -1) { + factor_w = -w; + } + if (h < -1) { + factor_h = -h; + } + + if (w < 0 && h < 0) + scale->w = scale->h = 0; + + if (!(w = scale->w)) + w = inlink->w; + if (!(h = scale->h)) + h = inlink->h; + + /* Make sure that the result is divisible by the factor we determined + * earlier. If no factor was set, it is nothing will happen as the default + * factor is 1 */ + if (w < 0) + w = av_rescale(h, inlink->w, inlink->h * factor_w) * factor_w; + if (h < 0) + h = av_rescale(w, inlink->h, inlink->w * factor_h) * factor_h; + + ctx->output_width = w; + ctx->output_height = h; + if (ctx->output_width < constraints->min_width || ctx->output_height < constraints->min_height || ctx->output_width > constraints->max_width || @@ -414,9 +506,9 @@ static av_cold void scale_vaapi_uninit(AVFilterContext *avctx) #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM) static const AVOption scale_vaapi_options[] = { { "w", "Output video width", - OFFSET(output_width), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, .flags = FLAGS }, + OFFSET(w_expr), AV_OPT_TYPE_STRING, .flags = FLAGS }, { "h", "Output video height", - OFFSET(output_height), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, .flags = FLAGS }, + OFFSET(h_expr), AV_OPT_TYPE_STRING, .flags = FLAGS }, { "format", "Output video format (software format of hardware frames)", OFFSET(output_format_string), AV_OPT_TYPE_STRING, .flags = FLAGS }, { NULL },