From patchwork Tue Jun 11 05:38:43 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Wang X-Patchwork-Id: 13494 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id BB991448E62 for ; Tue, 11 Jun 2019 08:39:20 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A4EA368A758; Tue, 11 Jun 2019 08:39:20 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CB79268A569 for ; Tue, 11 Jun 2019 08:39:14 +0300 (EEST) Received: by mail-pl1-f196.google.com with SMTP id bi6so4193520plb.12 for ; Mon, 10 Jun 2019 22:39:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UraQkaf+5SIkTdiK0xH8FchI42uXoj2Vj9HQqL2d/9c=; b=Kw0zM53NC+jAgOMu9Nq3ta39amJRsGQYtfmyYaKVV4DqVF6Hi29GXPJssWiDb63YD1 8UkorJvTmuF4wepJ5ZMxJr4X/NZmNoBozIDP2xp17AyE8zz59G6EBLmJJNr1CYs8502N dq08uNSJsPkIgsfx6HvYiR/6nnRJTJIAyZdJCU6vO6rd5g/vC4XfqtXMYh5/gti8PBoY xTjR5TYTRHE+pzm12+pzGV6CRmAdXiVW0J0SSixRI8F944apB4HbNWxEKpIvKO0JMwzA gB40jEcoUfQqpZ7YB7yLYl2n0Dcl1QgeI/yCd2czj30bHbHT5ggmiQ0UCObipBv5BTnk c2Ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UraQkaf+5SIkTdiK0xH8FchI42uXoj2Vj9HQqL2d/9c=; b=VcW7FpAm78deOAnepiP8NW0s3x7KartLIC8+0JxKIJVvOeKn8FG1oU2GuuTtos7MDa A1YU9j0aZXCnv+5E5oopKBlltvqhv1RUxaFyjb0K3q/QCbiWLJrFFpv4YEEqIfmqWqfa Gofd4MF6NGZoDZuaMnU+ZEauHBBTiTsYremgOeYtxtaMiMrd3VyDvSkeBuNzZJzST6RU oqHTRE7OjsFuT5malqmJsrdL6y1S5NxsARKs6ILKRmuiiGLLHD79h1GU4NyaxQtElXMO Ci4b3uHGcrjFDfrVaBdX/52Yy7ZQltDUSu5Gh0L6ajG0jrRDQEOKXXHS3FS8ykH+gFA8 zb8w== X-Gm-Message-State: APjAAAUv+lC7E5U4Oo77b9qiUE7abwB8iYNpQGrh6h9/QqZD2LQxQFZm KPydjDCj7YZInivfTH3aZ9mQvHyTZ2c= X-Google-Smtp-Source: APXvYqxN/Y2o5v05obRQ55cXTMHn6bvWF0VrTRWITnOvZ9BNuKd+Bbs/VDJ0jAN00HHWkOQcsST5cw== X-Received: by 2002:a17:902:a986:: with SMTP id bh6mr72513202plb.100.1560231553060; Mon, 10 Jun 2019 22:39:13 -0700 (PDT) Received: from localhost.localdomain ([47.90.99.151]) by smtp.gmail.com with ESMTPSA id 5sm12260079pfh.109.2019.06.10.22.39.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 10 Jun 2019 22:39:12 -0700 (PDT) From: lance.lmwang@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Tue, 11 Jun 2019 13:38:43 +0800 Message-Id: <20190611053844.21321-2-lance.lmwang@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190611053844.21321-1-lance.lmwang@gmail.com> References: <20190606083056.10791-1-lance.lmwang@gmail.com> <20190611053844.21321-1-lance.lmwang@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 2/3] libavfilter/vf_cover_rect: support for cover image with more pixel format and different width and height 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: Limin Wang Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Limin Wang Signed-off-by: Limin Wang --- libavfilter/vf_cover_rect.c | 49 +++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/libavfilter/vf_cover_rect.c b/libavfilter/vf_cover_rect.c index 898debf09d..22389b79d2 100644 --- a/libavfilter/vf_cover_rect.c +++ b/libavfilter/vf_cover_rect.c @@ -28,6 +28,7 @@ #include "internal.h" #include "lavfutils.h" +#include "lswsutils.h" enum mode { MODE_COVER, @@ -40,6 +41,7 @@ typedef struct CoverContext { int mode; char *cover_filename; AVFrame *cover_frame; + AVFrame *match_frame; int width, height; } CoverContext; @@ -71,21 +73,21 @@ static int config_input(AVFilterLink *inlink) return 0; } -static void cover_rect(CoverContext *cover, AVFrame *in, int offx, int offy) +static void cover_rect(AVFrame *cover_frame, AVFrame *in, int offx, int offy) { int x, y, p; for (p = 0; p < 3; p++) { uint8_t *data = in->data[p] + (offx>>!!p) + (offy>>!!p) * in->linesize[p]; - const uint8_t *src = cover->cover_frame->data[p]; - int w = AV_CEIL_RSHIFT(cover->cover_frame->width , !!p); - int h = AV_CEIL_RSHIFT(cover->cover_frame->height, !!p); + const uint8_t *src = cover_frame->data[p]; + int w = AV_CEIL_RSHIFT(cover_frame->width , !!p); + int h = AV_CEIL_RSHIFT(cover_frame->height, !!p); for (y = 0; y < h; y++) { for (x = 0; x < w; x++) { data[x] = src[x]; } data += in->linesize[p]; - src += cover->cover_frame->linesize[p]; + src += cover_frame->linesize[p]; } } } @@ -138,7 +140,10 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) CoverContext *cover = ctx->priv; AVDictionaryEntry *ex, *ey, *ew, *eh; int x = -1, y = -1, w = -1, h = -1; + enum AVPixelFormat in_format; char *xendptr = NULL, *yendptr = NULL, *wendptr = NULL, *hendptr = NULL; + AVFrame *cover_frame = NULL; + int ret; ex = av_dict_get(in->metadata, "lavfi.rect.x", NULL, AV_DICT_MATCH_CASE); ey = av_dict_get(in->metadata, "lavfi.rect.y", NULL, AV_DICT_MATCH_CASE); @@ -167,13 +172,30 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) } w = FFMIN(w, in->width - x); h = FFMIN(h, in->height - y); + in_format = in->format; if (w > in->width || h > in->height || w <= 0 || h <= 0) return AVERROR(EINVAL); if (cover->cover_frame) { - if (w != cover->cover_frame->width || h != cover->cover_frame->height) - return AVERROR(EINVAL); + if (w != cover->cover_frame->width || h != cover->cover_frame->height || + in_format != cover->cover_frame->format) { + if (!cover->match_frame && !(cover->match_frame = av_frame_alloc())) + return AVERROR(EINVAL); + + if ((ret = ff_scale_image(cover->match_frame->data, cover->match_frame->linesize, + w, h, in_format, cover->cover_frame->data, cover->cover_frame->linesize, + cover->cover_frame->width, cover->cover_frame->height, + cover->cover_frame->format, ctx)) < 0) + return AVERROR(EINVAL); + + cover->match_frame->width = w; + cover->match_frame->height = h; + cover->match_frame->format = in_format; + + cover_frame = cover->match_frame; + } else + cover_frame = cover->cover_frame; } cover->width = w; @@ -187,8 +209,12 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) if (cover->mode == MODE_BLUR) { blur (cover, in, x, y); } else { - cover_rect(cover, in, x, y); + cover_rect(cover_frame, in, x, y); + + if (cover->match_frame) + av_freep(&cover->match_frame->data[0]); } + return ff_filter_frame(ctx->outputs[0], in); } @@ -199,6 +225,8 @@ static av_cold void uninit(AVFilterContext *ctx) if (cover->cover_frame) av_freep(&cover->cover_frame->data[0]); av_frame_free(&cover->cover_frame); + + av_frame_free(&cover->match_frame); } static av_cold int init(AVFilterContext *ctx) @@ -220,11 +248,6 @@ static av_cold int init(AVFilterContext *ctx) &cover->cover_frame->width, &cover->cover_frame->height, &cover->cover_frame->format, cover->cover_filename, ctx)) < 0) return ret; - - if (cover->cover_frame->format != AV_PIX_FMT_YUV420P && cover->cover_frame->format != AV_PIX_FMT_YUVJ420P) { - av_log(ctx, AV_LOG_ERROR, "cover image is not a YUV420 image\n"); - return AVERROR(EINVAL); - } } return 0;