[FFmpeg-devel] [PATCH v1 2/2] fftools/ffmpeg: add exif orientation support per frame's metadata

Jun Li junli1026 at gmail.com
Sat May 25 03:04:27 EEST 2019


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 | 19 ++++++++++++++++++-
 3 files changed, 22 insertions(+), 3 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..b230dafdc9 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 = 1; // orientation indicates 'Normal' 
+    
+    // read exif orientation data
+    entry = av_dict_get(frame->metadata, "Orientation", NULL, 0);
+    if (entry)
+        orientation = atoi(entry->value);
+    return orientation > 8 ? 1 : orientation;
+}
+
 static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
                                         AVFilterInOut *in)
 {
@@ -809,7 +821,11 @@ 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
+            char transpose_args[32];
+            snprintf(transpose_args, sizeof(transpose_args), "orientation=%i", ifilter->orientation);
+            ret = insert_filter(&last_filter, &pad_idx, "transpose", transpose_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);
@@ -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;
-- 
2.17.1



More information about the ffmpeg-devel mailing list