• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavfilter/vf_aspect.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010 Bobby Bingham
00003 
00004  * This file is part of FFmpeg.
00005  *
00006  * FFmpeg is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * FFmpeg is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with FFmpeg; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00026 #include "libavutil/mathematics.h"
00027 #include "avfilter.h"
00028 
00029 typedef struct {
00030     AVRational aspect;
00031 } AspectContext;
00032 
00033 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00034 {
00035     AspectContext *aspect = ctx->priv;
00036     double  ratio;
00037     int64_t gcd;
00038     char c = 0;
00039 
00040     if (args) {
00041         if (sscanf(args, "%d:%d%c", &aspect->aspect.num, &aspect->aspect.den, &c) != 2)
00042             if (sscanf(args, "%lf%c", &ratio, &c) == 1)
00043                 aspect->aspect = av_d2q(ratio, 100);
00044 
00045         if (c || aspect->aspect.num <= 0 || aspect->aspect.den <= 0) {
00046             av_log(ctx, AV_LOG_ERROR,
00047                    "Invalid string '%s' for aspect ratio.\n", args);
00048             return AVERROR(EINVAL);
00049         }
00050 
00051         gcd = av_gcd(FFABS(aspect->aspect.num), FFABS(aspect->aspect.den));
00052         if (gcd) {
00053             aspect->aspect.num /= gcd;
00054             aspect->aspect.den /= gcd;
00055         }
00056     }
00057 
00058     if (aspect->aspect.den == 0)
00059         aspect->aspect = (AVRational) {0, 1};
00060 
00061     av_log(ctx, AV_LOG_INFO, "a:%d/%d\n", aspect->aspect.num, aspect->aspect.den);
00062     return 0;
00063 }
00064 
00065 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
00066 {
00067     AspectContext *aspect = link->dst->priv;
00068 
00069     picref->video->sample_aspect_ratio = aspect->aspect;
00070     avfilter_start_frame(link->dst->outputs[0], picref);
00071 }
00072 
00073 #if CONFIG_SETDAR_FILTER
00074 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
00075 static int setdar_config_props(AVFilterLink *inlink)
00076 {
00077     AspectContext *aspect = inlink->dst->priv;
00078     AVRational dar = aspect->aspect;
00079 
00080     av_reduce(&aspect->aspect.num, &aspect->aspect.den,
00081                aspect->aspect.num * inlink->h,
00082                aspect->aspect.den * inlink->w, 100);
00083 
00084     av_log(inlink->dst, AV_LOG_INFO, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
00085            inlink->w, inlink->h, dar.num, dar.den, aspect->aspect.num, aspect->aspect.den);
00086 
00087     inlink->sample_aspect_ratio = aspect->aspect;
00088 
00089     return 0;
00090 }
00091 
00092 AVFilter avfilter_vf_setdar = {
00093     .name      = "setdar",
00094     .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
00095 
00096     .init      = init,
00097 
00098     .priv_size = sizeof(AspectContext),
00099 
00100     .inputs    = (const AVFilterPad[]) {{ .name       = "default",
00101                                     .type             = AVMEDIA_TYPE_VIDEO,
00102                                     .config_props     = setdar_config_props,
00103                                     .get_video_buffer = avfilter_null_get_video_buffer,
00104                                     .start_frame      = start_frame,
00105                                     .end_frame        = avfilter_null_end_frame },
00106                                   { .name = NULL}},
00107 
00108     .outputs   = (const AVFilterPad[]) {{ .name       = "default",
00109                                     .type             = AVMEDIA_TYPE_VIDEO, },
00110                                   { .name = NULL}},
00111 };
00112 #endif /* CONFIG_SETDAR_FILTER */
00113 
00114 #if CONFIG_SETSAR_FILTER
00115 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */
00116 static int setsar_config_props(AVFilterLink *inlink)
00117 {
00118     AspectContext *aspect = inlink->dst->priv;
00119 
00120     inlink->sample_aspect_ratio = aspect->aspect;
00121 
00122     return 0;
00123 }
00124 
00125 AVFilter avfilter_vf_setsar = {
00126     .name      = "setsar",
00127     .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
00128 
00129     .init      = init,
00130 
00131     .priv_size = sizeof(AspectContext),
00132 
00133     .inputs    = (const AVFilterPad[]) {{ .name       = "default",
00134                                     .type             = AVMEDIA_TYPE_VIDEO,
00135                                     .config_props     = setsar_config_props,
00136                                     .get_video_buffer = avfilter_null_get_video_buffer,
00137                                     .start_frame      = start_frame,
00138                                     .end_frame        = avfilter_null_end_frame },
00139                                   { .name = NULL}},
00140 
00141     .outputs   = (const AVFilterPad[]) {{ .name       = "default",
00142                                     .type             = AVMEDIA_TYPE_VIDEO, },
00143                                   { .name = NULL}},
00144 };
00145 #endif /* CONFIG_SETSAR_FILTER */
00146 
Generated on Fri Feb 1 2013 14:34:49 for FFmpeg by doxygen 1.7.1