From patchwork Wed May 29 05:07:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Li X-Patchwork-Id: 13322 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 58B0D44989B for ; Wed, 29 May 2019 08:07:49 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4951A688372; Wed, 29 May 2019 08:07:49 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 79996680998 for ; Wed, 29 May 2019 08:07:42 +0300 (EEST) Received: by mail-pl1-f180.google.com with SMTP id f97so526017plb.5 for ; Tue, 28 May 2019 22:07:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=xgpnUcBuzALxHRjbV5NgQ/KQTGe7WjJF12wUQjtup6c=; b=Jgc4bN6gYeoezir8u5Vc0/k72VkBt/+5Ci+ZLjpRnDa7is6O8dMqwyiIclUQiEQ6Mh fApqKwd4Yud6Da5PxnBaMBusBGLUDzlvIim9tzb13lddH8TFfTvMs0Bu5wGDNCvhAvGM 08AjgmwlvqswAWWA9PHJe4JQ3EEWU/b98/u6gPIzo8C8fbti0ph6pvssoEjHM3lUewAY L6z96wtCs8YOq9Xmix9tM+P3xg0KUUb74v/Tsj+mTfnH1fLUkYp867vnJDJ1Bi0Xc9IX m0pO6eBwPm7v4HJVFjdcswazqFlZv7eUPDt9msgsjJzebZ88YElXziA0PeV3/mrWpOKc 4m6A== 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=xgpnUcBuzALxHRjbV5NgQ/KQTGe7WjJF12wUQjtup6c=; b=VYgwxA1IHImkU/8CW/nt9BnIHJ1Jmk+NEpY3VeL0fqscgvMKDmJY6VvLvldmisPbkM 9lb3ag/qS2p24r5Jw8QHnCoktjC5QuOAJSqQY1dePQ4X22wCIRK/kqQYQ7/Ma548kE1t 1RXSoj3G+GOdm/VNAdI2/hDfDaFlrJKjhMd5NeG+CAwz19iUE5GoQFbEkNnU4LbU0VAJ 7fyvHwhfB0EwKsRbPXTJ3SmitNWB+PjNt2glChIg8ukolLm6qYNiy4RnTLyr0h3OURWV /1cgX5K9Is5Vw66RXsdIZ6O76GLGkqV5fD+r/B2cpL0JUBXCyHKLWrEdibygys8tnjFj 4e3Q== X-Gm-Message-State: APjAAAX++9UmH0wGKVI9Cdt9fUlHNLtjV1h2XLro7O1DpJJzai6PL+LS isR4OB1qyCcPd1608RFMCsabtB9m X-Google-Smtp-Source: APXvYqxswSmPgCgxITqXpSKA8OyOH9auNl/9WH2Nl6Uuc83M7NgI0r497382gKnnAiaCLkoeppA90Q== X-Received: by 2002:a17:902:b590:: with SMTP id a16mr9461676pls.168.1559106460507; Tue, 28 May 2019 22:07:40 -0700 (PDT) Received: from jun-ubuntu.corp.microsoft.com ([2001:4898:80e8:3:f7ce:c45e:a7ce:5d49]) by smtp.googlemail.com with ESMTPSA id g12sm11023135pfq.1.2019.05.28.22.07.39 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 28 May 2019 22:07:40 -0700 (PDT) From: Jun Li To: ffmpeg-devel@ffmpeg.org Date: Tue, 28 May 2019 22:07:33 -0700 Message-Id: <20190529050733.11348-2-junli1026@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190529050733.11348-1-junli1026@gmail.com> References: <20190529050733.11348-1-junli1026@gmail.com> Subject: [FFmpeg-devel] [PATCH v5 2/2] fftools/ffmpeg: add exif orientation support per frame's metadata 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" Fix #6945 Rotate or/and flip frame according to frame's metadata orientation --- fftools/ffmpeg.c | 16 +++++++++++++++- fftools/ffmpeg.h | 3 ++- fftools/ffmpeg_filter.c | 28 +++++++++++++++++++++++----- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 01f04103cf..2f4229a9d0 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -2126,6 +2126,19 @@ static int ifilter_has_all_input_formats(FilterGraph *fg) return 1; } +static int orientation_need_update(InputFilter *ifilter, AVFrame *frame) +{ + int orientaion = get_frame_orientation(frame); + int filterst = ifilter->orientation <= 1 ? 0 : // not set + ifilter->orientation <= 4 ? 1 : // auto flip + 2; // auto transpose + int framest = orientaion <= 1 ? 0 : // not set + orientaion <= 4 ? 1 : // auto flip + 2; // auto transpose + + return filterst != framest; +} + static int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame) { FilterGraph *fg = ifilter->graph; @@ -2142,7 +2155,8 @@ static int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame) break; case AVMEDIA_TYPE_VIDEO: need_reinit |= ifilter->width != frame->width || - ifilter->height != frame->height; + ifilter->height != frame->height || + orientation_need_update(ifilter, frame); break; } diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index eb1eaf6363..54532ef0eb 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -244,7 +244,7 @@ typedef struct InputFilter { // parameters configured for this input int format; - int width, height; + int width, height, orientation; AVRational sample_aspect_ratio; int sample_rate; @@ -649,6 +649,7 @@ int init_complex_filtergraph(FilterGraph *fg); void sub2video_update(InputStream *ist, AVSubtitle *sub); int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame); +int get_frame_orientation(const AVFrame* frame); int ffmpeg_parse_options(int argc, char **argv); diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c index 72838de1e2..1fcadb1871 100644 --- a/fftools/ffmpeg_filter.c +++ b/fftools/ffmpeg_filter.c @@ -743,6 +743,18 @@ static int sub2video_prepare(InputStream *ist, InputFilter *ifilter) return 0; } +int get_frame_orientation(const AVFrame *frame) +{ + AVDictionaryEntry *entry = NULL; + int orientation = 0; + + // read exif orientation data + entry = av_dict_get(frame->metadata, "Orientation", NULL, 0); + if (entry && entry->value) + orientation = atoi(entry->value); + return orientation; +} + static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter, AVFilterInOut *in) { @@ -809,13 +821,18 @@ static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter, if (ist->autorotate) { double theta = get_rotation(ist->st); - if (fabs(theta - 90) < 1.0) { + if (fabs(theta) < 1.0) { // no rotation info in stream meta + if (ifilter->orientation < 0 || ifilter->orientation > 8) { + av_log(NULL, AV_LOG_ERROR, "Invalid frame orientation: %i\n", ifilter->orientation); + } else if (ifilter->orientation > 1 && ifilter->orientation <= 4) { // skip 0 (not set) and 1 (orientaion 'Normal') + ret = insert_filter(&last_filter, &pad_idx, "transpose", "orientation=auto_flip"); + } else if (ifilter->orientation > 4) { + ret = insert_filter(&last_filter, &pad_idx, "transpose", "orientation=auto_transpose"); + } + } else if (fabs(theta - 90) < 1.0) { ret = insert_filter(&last_filter, &pad_idx, "transpose", "clock"); } else if (fabs(theta - 180) < 1.0) { - ret = insert_filter(&last_filter, &pad_idx, "hflip", NULL); - if (ret < 0) - return ret; - ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL); + ret = insert_filter(&last_filter, &pad_idx, "transpose", "orientation=rotate180"); } else if (fabs(theta - 270) < 1.0) { ret = insert_filter(&last_filter, &pad_idx, "transpose", "cclock"); } else if (fabs(theta) > 1.0) { @@ -1191,6 +1208,7 @@ int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame) ifilter->width = frame->width; ifilter->height = frame->height; ifilter->sample_aspect_ratio = frame->sample_aspect_ratio; + ifilter->orientation = get_frame_orientation(frame); ifilter->sample_rate = frame->sample_rate; ifilter->channels = frame->channels;