From patchwork Sun Jun 9 21:28:35 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Li X-Patchwork-Id: 13479 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 6347A4450ED for ; Mon, 10 Jun 2019 00:34:17 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 456DA68AAAD; Mon, 10 Jun 2019 00:34:17 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 55ED068A0DF for ; Mon, 10 Jun 2019 00:34:11 +0300 (EEST) Received: by mail-pf1-f170.google.com with SMTP id i189so4061012pfg.10 for ; Sun, 09 Jun 2019 14:34: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:in-reply-to:references; bh=WMgLzHdM9EmVh3hGH8Eq9guGx9QrQPYy5WjsS2J9SvY=; b=Hp/t12YA3ZVvdXx6zc2ij5sgeKyFkBW2I/5whalJOU48E7ZnyUrrx0ZDD8XScIY+Zz c/WYUoaWNreXX/QxAuJa/J0n6PtznjReCxqZ/w8oRIoEiaP3nfs0sc4HIvvQVp9xpRYE YdacGgk2KF3cfeEE5uh1FJWX6D5H6KTmc2f6lwWRR2A3xu39CR1kgnMhci9GP2siX++l 3WIYRcxydvCsSDFt/G14ZxvDfm8RYBnmQ9Ceir/LsEGTSniD1471bIjOTIhIszeExvKh nwemcbVe38+suwrKHfcI98tysGDxx9Gmm8p7Sz4y4InX/GI3hbx8QgWcGTFryqzG1zQm yP7w== 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=WMgLzHdM9EmVh3hGH8Eq9guGx9QrQPYy5WjsS2J9SvY=; b=azQ7H7UwMwZiZS/8lM5BpGfmCiktNDx1OMqwOSIl/4cex8xjTW9rzWEhB0HGA5AEyf SjL9LqSjv9ruEcey0xXadF5pRMTOIrKrlzzD90UUvAgq/LzSWeaPhfbTyt/66L12gzR+ gP2HVPfGGCo7l2EwS6v03SaUiPIz9Jin3ewKng3FxO6beFoljL9QRlTTqm8DSwQ1xdsK xxqPjQkMd0USnOjkpV3efzp7XmQBlWSpHyhoYCXHkxUk2b7IKzSW5849PwekM7168ALq GKfXM16vL0XTCWOH3wiSfRhr/eXFsVVONPYEGziLgNVdKiZj2SFmmOUKv1DZw0/GSbDv RjTg== X-Gm-Message-State: APjAAAUISxeeS4ApJlxBe8M2TvwVK2QPNq1LYGWqjgD/WStiatwV2bQC YyepWq5WuER8kXwe1Qtnbcj26OrD X-Google-Smtp-Source: APXvYqyid4tqYb8zOPvj7fIKCf0xEclLLzP/QP4x+5mbz6/+wPJ23hGTe9hfObY7FgfCP3UkFVgh+g== X-Received: by 2002:aa7:8b17:: with SMTP id f23mr35482759pfd.194.1560115722873; Sun, 09 Jun 2019 14:28:42 -0700 (PDT) Received: from jun-ubuntu.corp.microsoft.com ([2001:4898:80e8:a:5d6f:404d:4506:60b]) by smtp.googlemail.com with ESMTPSA id m5sm11359664pgc.84.2019.06.09.14.28.42 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 09 Jun 2019 14:28:42 -0700 (PDT) From: Jun Li To: ffmpeg-devel@ffmpeg.org Date: Sun, 9 Jun 2019 14:28:35 -0700 Message-Id: <20190609212835.17650-2-junli1026@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190609212835.17650-1-junli1026@gmail.com> References: <20190609212835.17650-1-junli1026@gmail.com> Subject: [FFmpeg-devel] [PATCH v10 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 | 5 +++-- fftools/ffmpeg.h | 8 ++++++++ fftools/ffmpeg_filter.c | 40 +++++++++++++++++++++++++++++++++++----- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 01f04103cf..bc0cece59d 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -2141,8 +2141,9 @@ static int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame) ifilter->channel_layout != frame->channel_layout; break; case AVMEDIA_TYPE_VIDEO: - need_reinit |= ifilter->width != frame->width || - ifilter->height != frame->height; + need_reinit |= ifilter->width != frame->width || + ifilter->height != frame->height || + ifilter->orientation != get_frame_orientation(frame); break; } diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 7b6f802082..7324813ce3 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -232,6 +232,12 @@ typedef struct OptionsContext { int nb_enc_time_bases; } OptionsContext; +enum OrientationType { + ORIENTATION_NONE, + ORIENTATION_AUTO_FLIP, + ORIENTATION_AUTO_TRANSPOSE +}; + typedef struct InputFilter { AVFilterContext *filter; struct InputStream *ist; @@ -245,6 +251,7 @@ typedef struct InputFilter { int format; int width, height; + enum OrientationType orientation; AVRational sample_aspect_ratio; int sample_rate; @@ -649,6 +656,7 @@ int init_complex_filtergraph(FilterGraph *fg); void sub2video_update(InputStream *ist, AVSubtitle *sub); int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame); +enum OrientationType 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..eebb624116 100644 --- a/fftools/ffmpeg_filter.c +++ b/fftools/ffmpeg_filter.c @@ -743,6 +743,32 @@ static int sub2video_prepare(InputStream *ist, InputFilter *ifilter) return 0; } +enum OrientationType 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); + + // exif defines orientation in range [1, 8] + if (orientation > 8 || orientation < 1) { + if (entry && entry->value) { + av_log(NULL, AV_LOG_WARNING, + "Invalid frame orientation: %s, skip it.\n", entry->value); + } + return ORIENTATION_NONE; + } else if (orientation == 1) { + return ORIENTATION_NONE; + } else if (orientation <= 4) { + return ORIENTATION_AUTO_FLIP; + } else { + return ORIENTATION_AUTO_TRANSPOSE; + } +} + static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter, AVFilterInOut *in) { @@ -809,13 +835,16 @@ 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 == ORIENTATION_AUTO_FLIP) { + ret = insert_filter(&last_filter, &pad_idx, "transpose", "orientation=auto_flip"); + } else if (ifilter->orientation == ORIENTATION_AUTO_TRANSPOSE) { + 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 +1220,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;