00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "libavutil/common.h"
00027 #include "libavutil/opt.h"
00028 #include "libavutil/mathematics.h"
00029 #include "libavutil/parseutils.h"
00030 #include "avfilter.h"
00031 #include "internal.h"
00032 #include "video.h"
00033
00034 typedef struct {
00035 const AVClass *class;
00036 AVRational ratio;
00037 char *ratio_str;
00038 int max;
00039 } AspectContext;
00040
00041 #define OFFSET(x) offsetof(AspectContext, x)
00042 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
00043
00044 static const AVOption options[] = {
00045 {"max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
00046 {"ratio", "set ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
00047 {"r", "set ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
00048 {NULL}
00049 };
00050
00051 static av_cold int init(AVFilterContext *ctx, const char *args, const AVClass *class)
00052 {
00053 AspectContext *aspect = ctx->priv;
00054 static const char *shorthand[] = { "ratio", "max", NULL };
00055 char c;
00056 int ret;
00057 AVRational q;
00058
00059 aspect->class = class;
00060 av_opt_set_defaults(aspect);
00061
00062 if (sscanf(args, "%d:%d%c", &q.num, &q.den, &c) == 2) {
00063 aspect->ratio_str = av_strdup(args);
00064 av_log(ctx, AV_LOG_WARNING,
00065 "num:den syntax is deprecated, please use num/den or named options instead\n");
00066 } else if ((ret = av_opt_set_from_string(aspect, args, shorthand, "=", ":")) < 0) {
00067 return ret;
00068 }
00069
00070 if (aspect->ratio_str) {
00071 ret = av_parse_ratio(&aspect->ratio, aspect->ratio_str, aspect->max, 0, ctx);
00072 if (ret < 0 || aspect->ratio.num < 0 || aspect->ratio.den <= 0) {
00073 av_log(ctx, AV_LOG_ERROR,
00074 "Invalid string '%s' for aspect ratio\n", args);
00075 return AVERROR(EINVAL);
00076 }
00077 }
00078
00079 av_log(ctx, AV_LOG_VERBOSE, "a:%d/%d\n", aspect->ratio.num, aspect->ratio.den);
00080 return 0;
00081 }
00082
00083 static int filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
00084 {
00085 AspectContext *aspect = link->dst->priv;
00086
00087 frame->video->sample_aspect_ratio = aspect->ratio;
00088 return ff_filter_frame(link->dst->outputs[0], frame);
00089 }
00090
00091 static av_cold void uninit(AVFilterContext *ctx)
00092 {
00093 AspectContext *aspect = ctx->priv;
00094
00095 av_opt_free(aspect);
00096 }
00097
00098 #if CONFIG_SETDAR_FILTER
00099
00100 #define setdar_options options
00101 AVFILTER_DEFINE_CLASS(setdar);
00102
00103 static av_cold int setdar_init(AVFilterContext *ctx, const char *args)
00104 {
00105 return init(ctx, args, &setdar_class);
00106 }
00107
00108 static int setdar_config_props(AVFilterLink *inlink)
00109 {
00110 AspectContext *aspect = inlink->dst->priv;
00111 AVRational dar = aspect->ratio;
00112
00113 av_reduce(&aspect->ratio.num, &aspect->ratio.den,
00114 aspect->ratio.num * inlink->h,
00115 aspect->ratio.den * inlink->w, 100);
00116
00117 av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
00118 inlink->w, inlink->h, dar.num, dar.den, aspect->ratio.num, aspect->ratio.den);
00119
00120 inlink->sample_aspect_ratio = aspect->ratio;
00121
00122 return 0;
00123 }
00124
00125 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
00126 {
00127 .name = "default",
00128 .type = AVMEDIA_TYPE_VIDEO,
00129 .config_props = setdar_config_props,
00130 .get_video_buffer = ff_null_get_video_buffer,
00131 .filter_frame = filter_frame,
00132 },
00133 { NULL }
00134 };
00135
00136 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
00137 {
00138 .name = "default",
00139 .type = AVMEDIA_TYPE_VIDEO,
00140 },
00141 { NULL }
00142 };
00143
00144 AVFilter avfilter_vf_setdar = {
00145 .name = "setdar",
00146 .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
00147
00148 .init = setdar_init,
00149 .uninit = uninit,
00150
00151 .priv_size = sizeof(AspectContext),
00152
00153 .inputs = avfilter_vf_setdar_inputs,
00154
00155 .outputs = avfilter_vf_setdar_outputs,
00156 .priv_class = &setdar_class,
00157 };
00158
00159 #endif
00160
00161 #if CONFIG_SETSAR_FILTER
00162
00163 #define setsar_options options
00164 AVFILTER_DEFINE_CLASS(setsar);
00165
00166 static av_cold int setsar_init(AVFilterContext *ctx, const char *args)
00167 {
00168 return init(ctx, args, &setsar_class);
00169 }
00170
00171 static int setsar_config_props(AVFilterLink *inlink)
00172 {
00173 AspectContext *aspect = inlink->dst->priv;
00174
00175 inlink->sample_aspect_ratio = aspect->ratio;
00176
00177 return 0;
00178 }
00179
00180 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
00181 {
00182 .name = "default",
00183 .type = AVMEDIA_TYPE_VIDEO,
00184 .config_props = setsar_config_props,
00185 .get_video_buffer = ff_null_get_video_buffer,
00186 .filter_frame = filter_frame,
00187 },
00188 { NULL }
00189 };
00190
00191 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
00192 {
00193 .name = "default",
00194 .type = AVMEDIA_TYPE_VIDEO,
00195 },
00196 { NULL }
00197 };
00198
00199 AVFilter avfilter_vf_setsar = {
00200 .name = "setsar",
00201 .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
00202
00203 .init = setsar_init,
00204 .uninit = uninit,
00205
00206 .priv_size = sizeof(AspectContext),
00207
00208 .inputs = avfilter_vf_setsar_inputs,
00209
00210 .outputs = avfilter_vf_setsar_outputs,
00211 .priv_class = &setsar_class,
00212 };
00213
00214 #endif