From patchwork Sat May 25 00:04:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Li X-Patchwork-Id: 13283 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 E4AA1449366 for ; Sat, 25 May 2019 03:11:17 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C97F768A15A; Sat, 25 May 2019 03:11:17 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg1-f194.google.com (mail-pg1-f194.google.com [209.85.215.194]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 963C168A1C3 for ; Sat, 25 May 2019 03:11:11 +0300 (EEST) Received: by mail-pg1-f194.google.com with SMTP id w22so5868062pgi.6 for ; Fri, 24 May 2019 17:11:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=Jnb8oOIkLnMZFF9lC/m3iWsY0uveV2XFNNO954OYwlc=; b=W6lo3y1OKpnqNse9br9Br+NJ/LzH/p7pDmnf15F05MSJJ4UlYulatr5U+6BnwcXPjm nLbb42IQawWyiA92TFqecU1BDbzv0rTqItgd2OiUbfVpCQcg5VQJX6rfmQFVhsi0XhT2 doZylCn+vF5vhbYsHCYWtJ0BtgVc3PfklHOCDrzcUufDBPNWU3He313ze1PCTdc9HzgO 0vVUQaeIFJpXllSUMuMzFeWJdG9rHRuHWtt44seBpq7BUXdCz2Y6Zsr7e2JENzmvD0Sk /P0Y7YGAjwHirVhTq/oyJKYIktcTwx8ijDvPfuzK1TacIPs0zClMC/uXNSG2hgFCfBRn XEow== 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; bh=Jnb8oOIkLnMZFF9lC/m3iWsY0uveV2XFNNO954OYwlc=; b=A1xVl1vrm8XYRCwXLM+16HReKttvw7svxbHU1Zzwj4e8bljGe82OojrHzFpGmWrmCM rJuBI90yNpHk4Cez15ZqIsISoIyovjEbo7ZebsKT82fNq9lkFwBy5QMi5zRPy8XU0Ryn L0jLUb7GvilzMJVdc5rM49oGOZka+VhB4v3FovrOFUZhJuNopLtRwWg++2vGUXNLMZx0 6OcADYjUop7WmeMc30gWHYWh1wDBpzCZ+hhu4SCS4qlHgep74XZOQR/n4xCA4KjIs3JP RCLLUnZEc/CWfvkKmQA9hX0mI1ozU+Q8rk0cj+3ZcLDEq0IB9nnIbnB5GcTSV52kNwdS jySw== X-Gm-Message-State: APjAAAVge7PFcj2W3Q9pbHH4AVPr4NGyMLI6axLUR/sAFFMysJ+kWsIc lkcel/8ZqhLbiHhn7gZQxrnP7nPS X-Google-Smtp-Source: APXvYqxFOZeBGqXQ+QXgy7oBfa4ULzFxEiu49f7TtZHZPkxyS5iR8KFy1AG+FxrYfokMfNsdVhNWvA== X-Received: by 2002:a63:e54d:: with SMTP id z13mr13653571pgj.132.1558742679343; Fri, 24 May 2019 17:04:39 -0700 (PDT) Received: from jun-ubuntu.corp.microsoft.com ([2001:4898:80e8:9:311a:6c69:a9e0:a9ae]) by smtp.googlemail.com with ESMTPSA id h62sm3404738pgc.77.2019.05.24.17.04.38 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 24 May 2019 17:04:38 -0700 (PDT) From: Jun Li To: ffmpeg-devel@ffmpeg.org Date: Fri, 24 May 2019 17:04:26 -0700 Message-Id: <20190525000427.25546-1-junli1026@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH v1 1/2] lavf/vf_transpose: add exif orientation support 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" Add exif orientation support and expose an option. --- libavfilter/vf_transpose.c | 258 +++++++++++++++++++++++++++++-------- 1 file changed, 207 insertions(+), 51 deletions(-) diff --git a/libavfilter/vf_transpose.c b/libavfilter/vf_transpose.c index dd54947bd9..4aebfb9ee4 100644 --- a/libavfilter/vf_transpose.c +++ b/libavfilter/vf_transpose.c @@ -46,6 +46,9 @@ typedef struct TransVtable { void (*transpose_block)(uint8_t *src, ptrdiff_t src_linesize, uint8_t *dst, ptrdiff_t dst_linesize, int w, int h); + void (*copyline_block)(uint8_t *src, ptrdiff_t src_linesize, + uint8_t *dst, ptrdiff_t dst_linesize, + int line_dir, int w, int h); } TransVtable; typedef struct TransContext { @@ -56,7 +59,7 @@ typedef struct TransContext { int passthrough; ///< PassthroughType, landscape passthrough mode enabled int dir; ///< TransposeDir - + int orientation; ///< Orientation TransVtable vtables[4]; } TransContext; @@ -182,6 +185,105 @@ static void transpose_8x8_64_c(uint8_t *src, ptrdiff_t src_linesize, transpose_block_64_c(src, src_linesize, dst, dst_linesize, 8, 8); } + +static void copyline_block_8(uint8_t *src, ptrdiff_t src_linesize, + uint8_t *dst, ptrdiff_t dst_linesize, + int line_dir, int w, int h) +{ + int x, y, i; + for (y = 0; y < h; y++, dst += dst_linesize, src += src_linesize) { + for (x = 0; x < w; x ++) { + i = line_dir < 0 ? w-x-1 : x; + dst[i] = src[x]; + } + } +} + +static void copyline_block_16(uint8_t *src, ptrdiff_t src_linesize, + uint8_t *dst, ptrdiff_t dst_linesize, + int line_dir, int w, int h) +{ + int x, y, i; + for (y = 0; y < h; y++, dst += dst_linesize, src += src_linesize) { + for (x = 0; x < w; x ++) { + i = line_dir < 0 ? w-x-1 : x; + *((uint16_t *)(dst + 2*i)) = *((uint16_t *)(src + 2*x)); + } + } +} + +static void copyline_block_24(uint8_t *src, ptrdiff_t src_linesize, + uint8_t *dst, ptrdiff_t dst_linesize, + int line_dir, int w, int h) +{ + int x, y, i; + for (y = 0; y < h; y++, dst += dst_linesize, src += src_linesize) { + for (x = 0; x < w; x++) { + int32_t v = AV_RB24(src + 3*x); + i = line_dir < 0 ? w-x-1 : x; + AV_WB24(dst + 3*i, v); + } + } +} + +static void copyline_block_32(uint8_t *src, ptrdiff_t src_linesize, + uint8_t *dst, ptrdiff_t dst_linesize, + int line_dir, int w, int h) +{ + int x, y, i; + for (y = 0; y < h; y++, dst += dst_linesize, src += src_linesize) { + for (x = 0; x < w; x ++) { + i = line_dir < 0 ? w-x-1 : x; + *((uint32_t *)(dst + 4*i)) = *((uint32_t *)(src + 4*x)); + } + } +} + +static void copyline_block_48(uint8_t *src, ptrdiff_t src_linesize, + uint8_t *dst, ptrdiff_t dst_linesize, + int line_dir, int w, int h) +{ + int x, y, i; + for (y = 0; y < h; y++, dst += dst_linesize, src += src_linesize) { + for (x = 0; x < w; x++) { + int64_t v = AV_RB48(src + 6*x); + i = line_dir < 0 ? w-x-1 : x; + AV_WB48(dst + 6*i, v); + } + } +} + +static void copyline_block_64(uint8_t *src, ptrdiff_t src_linesize, + uint8_t *dst, ptrdiff_t dst_linesize, + int line_dir, int w, int h) +{ + int x, y, i; + for (y = 0; y < h; y++, dst += dst_linesize, src += src_linesize) { + for (x = 0; x < w; x ++) { + i = line_dir < 0 ? w-x-1 : x; + *((uint64_t *)(dst + 8*i)) = *((uint64_t *)(src + 8*x)); + } + } +} + +static void set_outlink_width_height(AVFilterLink *inlink, AVFilterLink *outlink, int transpose) +{ + if (transpose) { + outlink->w = inlink->h; + outlink->h = inlink->w; + + if (inlink->sample_aspect_ratio.num) + outlink->sample_aspect_ratio = av_div_q((AVRational) { 1, 1 }, + inlink->sample_aspect_ratio); + else + outlink->sample_aspect_ratio = inlink->sample_aspect_ratio; + } else { + outlink->w = inlink->w; + outlink->h = inlink->h; + outlink->sample_aspect_ratio = inlink->sample_aspect_ratio; + } +} + static int config_props_output(AVFilterLink *outlink) { AVFilterContext *ctx = outlink->src; @@ -213,33 +315,44 @@ static int config_props_output(AVFilterLink *outlink) av_assert0(desc_in->nb_components == desc_out->nb_components); - av_image_fill_max_pixsteps(s->pixsteps, NULL, desc_out); + + if (s->orientation && s->orientation < 5) { + outlink->h = inlink->h; + outlink->w = inlink->w; + outlink->sample_aspect_ratio = inlink->sample_aspect_ratio; + } else { + outlink->w = inlink->h; + outlink->h = inlink->w; - outlink->w = inlink->h; - outlink->h = inlink->w; - - if (inlink->sample_aspect_ratio.num) - outlink->sample_aspect_ratio = av_div_q((AVRational) { 1, 1 }, - inlink->sample_aspect_ratio); - else - outlink->sample_aspect_ratio = inlink->sample_aspect_ratio; + if (inlink->sample_aspect_ratio.num) + outlink->sample_aspect_ratio = av_div_q((AVRational) { 1, 1 }, + inlink->sample_aspect_ratio); + else + outlink->sample_aspect_ratio = inlink->sample_aspect_ratio; + } for (int i = 0; i < 4; i++) { TransVtable *v = &s->vtables[i]; switch (s->pixsteps[i]) { case 1: v->transpose_block = transpose_block_8_c; - v->transpose_8x8 = transpose_8x8_8_c; break; + v->transpose_8x8 = transpose_8x8_8_c; + v->copyline_block = copyline_block_8; break; case 2: v->transpose_block = transpose_block_16_c; - v->transpose_8x8 = transpose_8x8_16_c; break; + v->transpose_8x8 = transpose_8x8_16_c; + v->copyline_block = copyline_block_16; break; case 3: v->transpose_block = transpose_block_24_c; - v->transpose_8x8 = transpose_8x8_24_c; break; + v->transpose_8x8 = transpose_8x8_24_c; + v->copyline_block = copyline_block_24; break; case 4: v->transpose_block = transpose_block_32_c; - v->transpose_8x8 = transpose_8x8_32_c; break; + v->transpose_8x8 = transpose_8x8_32_c; + v->copyline_block = copyline_block_32; break; case 6: v->transpose_block = transpose_block_48_c; - v->transpose_8x8 = transpose_8x8_48_c; break; + v->transpose_8x8 = transpose_8x8_48_c; + v->copyline_block = copyline_block_48; break; case 8: v->transpose_block = transpose_block_64_c; - v->transpose_8x8 = transpose_8x8_64_c; break; + v->transpose_8x8 = transpose_8x8_64_c; + v->copyline_block = copyline_block_64; break; } } @@ -285,42 +398,80 @@ static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, uint8_t *dst, *src; int dstlinesize, srclinesize; int x, y; + int dir = s->dir; TransVtable *v = &s->vtables[plane]; + + if (s->orientation > 0 && s->orientation < 5) { + int line_dir = 1; + dstlinesize = out->linesize[plane]; + dst = out->data[plane] + start * dstlinesize; + srclinesize = in->linesize[plane]; + src = in->data[plane] + start * srclinesize; + + switch (s->orientation) { + case 2: // mirror horizontal + line_dir = -1; + break; + case 3: // rotate 180 + dst = out->data[plane] + (outh - start - 1) * dstlinesize; + line_dir = -1; + dstlinesize *= -1; + break; + case 4: // mirror vertical + dst = out->data[plane] + (outh - start - 1) * dstlinesize; + dstlinesize *= -1; + break; + default: + break; + } + v->copyline_block(src, srclinesize, dst, dstlinesize, line_dir, outw, end - start); + } else { + dstlinesize = out->linesize[plane]; + dst = out->data[plane] + start * dstlinesize; + src = in->data[plane]; + srclinesize = in->linesize[plane]; + switch (s->orientation) { + case 5: // mirror horizontal and rotate 270 CW + dir = 0; break; + case 6: // rotate 90 CW + dir = 1; break; + case 7: // mirror horizontal and rotate 90 CW + dir = 3; break; + case 8: // rotate 270 CW + dir = 2; break; + default: break; + } - dstlinesize = out->linesize[plane]; - dst = out->data[plane] + start * dstlinesize; - src = in->data[plane]; - srclinesize = in->linesize[plane]; - - if (s->dir & 1) { - src += in->linesize[plane] * (inh - 1); - srclinesize *= -1; - } + if (dir & 1) { + src += in->linesize[plane] * (inh - 1); + srclinesize *= -1; + } - if (s->dir & 2) { - dst = out->data[plane] + dstlinesize * (outh - start - 1); - dstlinesize *= -1; - } + if (dir & 2) { + dst = out->data[plane] + dstlinesize * (outh - start - 1); + dstlinesize *= -1; + } - for (y = start; y < end - 7; y += 8) { - for (x = 0; x < outw - 7; x += 8) { - v->transpose_8x8(src + x * srclinesize + y * pixstep, - srclinesize, - dst + (y - start) * dstlinesize + x * pixstep, - dstlinesize); + for (y = start; y < end - 7; y += 8) { + for (x = 0; x < outw - 7; x += 8) { + v->transpose_8x8(src + x * srclinesize + y * pixstep, + srclinesize, + dst + (y - start) * dstlinesize + x * pixstep, + dstlinesize); + } + if (outw - x > 0 && end - y > 0) + v->transpose_block(src + x * srclinesize + y * pixstep, + srclinesize, + dst + (y - start) * dstlinesize + x * pixstep, + dstlinesize, outw - x, end - y); } - if (outw - x > 0 && end - y > 0) - v->transpose_block(src + x * srclinesize + y * pixstep, - srclinesize, - dst + (y - start) * dstlinesize + x * pixstep, - dstlinesize, outw - x, end - y); - } - if (end - y > 0) - v->transpose_block(src + 0 * srclinesize + y * pixstep, - srclinesize, - dst + (y - start) * dstlinesize + 0 * pixstep, - dstlinesize, outw, end - y); + if (end - y > 0) + v->transpose_block(src + 0 * srclinesize + y * pixstep, + srclinesize, + dst + (y - start) * dstlinesize + 0 * pixstep, + dstlinesize, outw, end - y); + } } return 0; @@ -334,7 +485,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) ThreadData td; AVFrame *out; - if (s->passthrough) + if (s->passthrough || s->orientation == 1) return ff_filter_frame(outlink, in); out = ff_get_video_buffer(outlink, outlink->w, outlink->h); @@ -347,8 +498,13 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) if (in->sample_aspect_ratio.num == 0) { out->sample_aspect_ratio = in->sample_aspect_ratio; } else { - out->sample_aspect_ratio.num = in->sample_aspect_ratio.den; - out->sample_aspect_ratio.den = in->sample_aspect_ratio.num; + if (s->orientation > 0 && s->orientation < 5) { + out->sample_aspect_ratio.num = in->sample_aspect_ratio.num; + out->sample_aspect_ratio.den = in->sample_aspect_ratio.den; + } else { + out->sample_aspect_ratio.num = in->sample_aspect_ratio.den; + out->sample_aspect_ratio.den = in->sample_aspect_ratio.num; + } } td.in = in, td.out = out; @@ -366,13 +522,13 @@ static const AVOption transpose_options[] = { { "clock", "rotate clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK }, .flags=FLAGS, .unit = "dir" }, { "cclock", "rotate counter-clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK }, .flags=FLAGS, .unit = "dir" }, { "clock_flip", "rotate clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP }, .flags=FLAGS, .unit = "dir" }, - + { "passthrough", "do not apply transposition if the input matches the specified geometry", OFFSET(passthrough), AV_OPT_TYPE_INT, {.i64=TRANSPOSE_PT_TYPE_NONE}, 0, INT_MAX, FLAGS, "passthrough" }, { "none", "always apply transposition", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_NONE}, INT_MIN, INT_MAX, FLAGS, "passthrough" }, { "portrait", "preserve portrait geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_PORTRAIT}, INT_MIN, INT_MAX, FLAGS, "passthrough" }, { "landscape", "preserve landscape geometry", 0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_LANDSCAPE}, INT_MIN, INT_MAX, FLAGS, "passthrough" }, - + { "orientation", "set exif orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 8, FLAGS, "orientation" }, { NULL } };