[FFmpeg-devel] [PATCH 1/2] vf_scale: add force_original_aspect_ratio

Timothy Gu timothygu99 at gmail.com
Sat Aug 3 03:42:07 CEST 2013


---
 doc/filters.texi       | 15 +++++++++++++++
 libavfilter/vf_scale.c | 16 ++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/doc/filters.texi b/doc/filters.texi
index cd59057..d78c4ca 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -6373,6 +6373,21 @@ Full range (0-255 in case of 8bit luma)
 "Mpeg" range (16-235 in case of 8bit luma)
 @end table
 
+ at item force_original_aspect_ratio
+Enable decreasing or increasing output video width or height if necessary to
+keep the original aspect ratio. If set to 1, it will automatically decrease;
+if set to 2, it will increase if needed.
+
+One useful instance of this option is that when you know a specific device's
+maximum allowed resolution, you can use this to limit the output video to
+that, while retaining the aspect ratio. For example, device A allows
+1280x720 playback, and your video is 1920x800. Using this option (set it to
+1) and specifying 1280x720 to the command line makes the output 1280x533.
+
+Please note that this is a different thing than specifying -1 for @option{w}
+or @option{h}, you still need to specify the output resolution for this option
+to work.
+
 @end table
 
 The values of the @var{w} and @var{h} options are expressions
diff --git a/libavfilter/vf_scale.c b/libavfilter/vf_scale.c
index 034da5e..21c7244 100644
--- a/libavfilter/vf_scale.c
+++ b/libavfilter/vf_scale.c
@@ -101,6 +101,8 @@ typedef struct {
     int out_v_chr_pos;
     int in_h_chr_pos;
     int in_v_chr_pos;
+
+    int force_original_aspect_ratio;
 } ScaleContext;
 
 static av_cold int init(AVFilterContext *ctx)
@@ -274,6 +276,19 @@ static int config_props(AVFilterLink *outlink)
     if (h == -1)
         h = av_rescale(w, inlink->h, inlink->w);
 
+    if (scale->force_original_aspect_ratio) {
+        int tmp_w = av_rescale(h, inlink->w, inlink->h);
+        int tmp_h = av_rescale(w, inlink->h, inlink->w);
+
+        if (scale->force_original_aspect_ratio == 1) {
+             w = FFMIN(tmp_w, w);
+             h = FFMIN(tmp_h, h);
+        } else {
+             w = FFMAX(tmp_w, w);
+             h = FFMAX(tmp_h, h);
+        }
+    }
+
     if (w > INT_MAX || h > INT_MAX ||
         (h * inlink->w) > INT_MAX  ||
         (w * inlink->h) > INT_MAX)
@@ -501,6 +516,7 @@ static const AVOption scale_options[] = {
     { "in_h_chr_pos",   "input horizontal chroma position in luma grid/256", OFFSET(in_h_chr_pos), AV_OPT_TYPE_INT, { .i64 = -1}, -1, 512, FLAGS },
     { "out_v_chr_pos",   "output vertical chroma position in luma grid/256"  , OFFSET(out_v_chr_pos), AV_OPT_TYPE_INT, { .i64 = -1}, -1, 512, FLAGS },
     { "out_h_chr_pos",   "output horizontal chroma position in luma grid/256", OFFSET(out_h_chr_pos), AV_OPT_TYPE_INT, { .i64 = -1}, -1, 512, FLAGS },
+    { "force_original_aspect_ratio", "decrease or increase w/h if necessary to keep the original AR", OFFSET(force_original_aspect_ratio), AV_OPT_TYPE_INT, { .i64 = 0}, 0, 2, FLAGS },
     { NULL },
 };
 
-- 
1.8.1.2



More information about the ffmpeg-devel mailing list