libavfilter/vf_aspect.c
3922deb5
 /*
  * Copyright (c) 2010 Bobby Bingham
 
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
ba87f080
  * @file
cef4b74b
  * aspect ratio modification video filters
3922deb5
  */
 
 #include "avfilter.h"
 
 typedef struct {
     AVRational aspect;
 } AspectContext;
 
 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
 {
     AspectContext *aspect = ctx->priv;
     double  ratio;
     int64_t gcd;
7de19a32
     char c = 0;
3922deb5
 
bdb47f3a
     if (args) {
7de19a32
         if (sscanf(args, "%d:%d%c", &aspect->aspect.num, &aspect->aspect.den, &c) != 2)
             if (sscanf(args, "%lf%c", &ratio, &c) == 1)
                 aspect->aspect = av_d2q(ratio, 100);
 
         if (c || aspect->aspect.num <= 0 || aspect->aspect.den <= 0) {
             av_log(ctx, AV_LOG_ERROR,
                    "Invalid string '%s' for aspect ratio.\n", args);
             return AVERROR(EINVAL);
         }
 
b7be04f3
         gcd = av_gcd(FFABS(aspect->aspect.num), FFABS(aspect->aspect.den));
         if (gcd) {
             aspect->aspect.num /= gcd;
             aspect->aspect.den /= gcd;
         }
3922deb5
     }
 
bdb47f3a
     if (aspect->aspect.den == 0)
3922deb5
         aspect->aspect = (AVRational) {0, 1};
 
eee0ef5e
     av_log(ctx, AV_LOG_INFO, "a:%d/%d\n", aspect->aspect.num, aspect->aspect.den);
3922deb5
     return 0;
 }
 
ecc8dada
 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
3922deb5
 {
     AspectContext *aspect = link->dst->priv;
 
35fe66ab
     picref->video->sample_aspect_ratio = aspect->aspect;
3922deb5
     avfilter_start_frame(link->dst->outputs[0], picref);
 }
 
2fb21bf4
 #if CONFIG_SETDAR_FILTER
 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
 static int setdar_config_props(AVFilterLink *inlink)
3922deb5
 {
     AspectContext *aspect = inlink->dst->priv;
eee0ef5e
     AVRational dar = aspect->aspect;
3922deb5
 
     av_reduce(&aspect->aspect.num, &aspect->aspect.den,
                aspect->aspect.num * inlink->h,
                aspect->aspect.den * inlink->w, 100);
 
3ea031b3
     av_log(inlink->dst, AV_LOG_INFO, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
6fd2b8bd
            inlink->w, inlink->h, dar.num, dar.den, aspect->aspect.num, aspect->aspect.den);
14b171cd
 
     inlink->sample_aspect_ratio = aspect->aspect;
 
3922deb5
     return 0;
 }
 
2fb21bf4
 AVFilter avfilter_vf_setdar = {
     .name      = "setdar",
     .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
3922deb5
 
     .init      = init,
 
     .priv_size = sizeof(AspectContext),
 
     .inputs    = (AVFilterPad[]) {{ .name             = "default",
72415b2a
                                     .type             = AVMEDIA_TYPE_VIDEO,
2fb21bf4
                                     .config_props     = setdar_config_props,
d313e17a
                                     .get_video_buffer = avfilter_null_get_video_buffer,
3922deb5
                                     .start_frame      = start_frame,
d313e17a
                                     .end_frame        = avfilter_null_end_frame },
3922deb5
                                   { .name = NULL}},
 
     .outputs   = (AVFilterPad[]) {{ .name             = "default",
72415b2a
                                     .type             = AVMEDIA_TYPE_VIDEO, },
3922deb5
                                   { .name = NULL}},
 };
2fb21bf4
 #endif /* CONFIG_SETDAR_FILTER */
3922deb5
 
2fb21bf4
 #if CONFIG_SETSAR_FILTER
14b171cd
 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
 static int setsar_config_props(AVFilterLink *inlink)
 {
     AspectContext *aspect = inlink->dst->priv;
 
     inlink->sample_aspect_ratio = aspect->aspect;
 
     return 0;
 }
 
2fb21bf4
 AVFilter avfilter_vf_setsar = {
     .name      = "setsar",
     .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
3922deb5
 
     .init      = init,
 
     .priv_size = sizeof(AspectContext),
 
     .inputs    = (AVFilterPad[]) {{ .name             = "default",
72415b2a
                                     .type             = AVMEDIA_TYPE_VIDEO,
14b171cd
                                     .config_props     = setsar_config_props,
d313e17a
                                     .get_video_buffer = avfilter_null_get_video_buffer,
3922deb5
                                     .start_frame      = start_frame,
d313e17a
                                     .end_frame        = avfilter_null_end_frame },
3922deb5
                                   { .name = NULL}},
 
     .outputs   = (AVFilterPad[]) {{ .name             = "default",
72415b2a
                                     .type             = AVMEDIA_TYPE_VIDEO, },
3922deb5
                                   { .name = NULL}},
 };
2fb21bf4
 #endif /* CONFIG_SETSAR_FILTER */
3922deb5