From patchwork Sun May 26 04:15:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Li X-Patchwork-Id: 13300 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 CDA05447509 for ; Sun, 26 May 2019 07:15:52 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BCAAC689AFF; Sun, 26 May 2019 07:15:52 +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 711E06801E0 for ; Sun, 26 May 2019 07:15:44 +0300 (EEST) Received: by mail-pg1-f194.google.com with SMTP id h2so4151808pgg.1 for ; Sat, 25 May 2019 21:15:44 -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=wyViCHY3iTea4NWN78nLgKpylx6udfMpL+mXTc6brqg=; b=ifNGV7RotCXa8IjoyfBeTPY9SryW7ANfG6mk+QtZ/D9Nh0MOvLcBXzs7cqEvJWMmws 7FkanbOtNAUCWUINhq8eL4fzaGh4FqrZn0uYI2JPHMJyjGjf55ZuhxYQ8uEWQExXaUkc RoVVZIVGhhKBUPvWUot9hUyrh5ElmvfiCGh5+YI+GcklIsLXFXZ/IO59EV6CVGeFmlBv IHe+kDHVZ4txTVx+fIoU1ZIX0Jq7zhL5VlNmOAdUboNMPdP8n6HoZLdKUIWBVD78LVoH CMx/uGQzZ0vDJsCjEIwhliSd9ir+bZuABsYo2ISJld7YDLxNKX30nCp1j3UPTwX6PZiY 7tFw== 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=wyViCHY3iTea4NWN78nLgKpylx6udfMpL+mXTc6brqg=; b=ZWYodAbwYgSQ41u6ltQxW7yMJM9srpNNdxH+J0UXos+nTq/ZtBMiZqfebldoKzYHtQ JUkV6pLqkQbcFEGuEzSdjiuZ51dqW+gb00HBimUcIhicmI9eFB1PKHJXRdHnINUX76N0 cYlB+UJ1IErJ8KmbXhKgdydJW5iJDXCq9ukFOm4QozC8jrxu7ONEv7EyHUwzOROe2Kd5 7cIqW1w5Xymp89v5WVEY2cbDZ5OkBDVfbXb0NWawED90DmLfPZYYAmUqVp9uJoCCl2ie FJtOEYooRUPlV4XREDcudX72KAkCn2SvRVhVWh8s98bCk0E+zn6cpdDUKwssQPX+UTl6 0Hrw== X-Gm-Message-State: APjAAAUghtbuP7Tj/1xPrAsrmSY4Q6uSB9gyujfV5ECb9nF9g3PtFSzY sBGslioGuuqE2aUpBpVueyx/3JdY X-Google-Smtp-Source: APXvYqxHS7CiTX5EyA7cFUkWhq0wJMHmuCrL3rVPaeARchBhsCR2+HZty3f2edMHEiNMTc1VDtxXaw== X-Received: by 2002:a63:5608:: with SMTP id k8mr115834949pgb.393.1558844142450; Sat, 25 May 2019 21:15:42 -0700 (PDT) Received: from jun-ubuntu.corp.microsoft.com ([2001:4898:80e8:b:44c2:ff80:3faf:dfc5]) by smtp.googlemail.com with ESMTPSA id v39sm13858459pjb.3.2019.05.25.21.15.41 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 25 May 2019 21:15:41 -0700 (PDT) From: Jun Li To: ffmpeg-devel@ffmpeg.org Date: Sat, 25 May 2019 21:15:34 -0700 Message-Id: <20190526041534.23469-2-junli1026@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190526041534.23469-1-junli1026@gmail.com> References: <20190526041534.23469-1-junli1026@gmail.com> Subject: [FFmpeg-devel] [PATCH v2 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 | 3 ++- fftools/ffmpeg.h | 3 ++- fftools/ffmpeg_filter.c | 35 ++++++++++++++++++++++++++--------- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 01f04103cf..da4c19c782 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -2142,7 +2142,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 || + ifilter->orientation != get_frame_orientation(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..41dcf89692 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) + orientation = atoi(entry->value); + return orientation; +} + static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter, AVFilterInOut *in) { @@ -808,20 +820,24 @@ 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) { + char filter_args[64]; + + 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) { // skip 0 (not set) and 1 (orientaion 'Normal') + snprintf(filter_args, sizeof(filter_args), "orientation=%i", ifilter->orientation); + ret = insert_filter(&last_filter, &pad_idx, "transpose", filter_args); + } + } 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=3"); } else if (fabs(theta - 270) < 1.0) { ret = insert_filter(&last_filter, &pad_idx, "transpose", "cclock"); } else if (fabs(theta) > 1.0) { - char rotate_buf[64]; - snprintf(rotate_buf, sizeof(rotate_buf), "%f*PI/180", theta); - ret = insert_filter(&last_filter, &pad_idx, "rotate", rotate_buf); + snprintf(filter_args, sizeof(filter_args), "%f*PI/180", theta); + ret = insert_filter(&last_filter, &pad_idx, "rotate", filter_args); } if (ret < 0) return ret; @@ -1191,6 +1207,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;