annotate ffmpeg/libavfilter/vf_aspect.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * Copyright (c) 2010 Bobby Bingham
yading@10 3 *
yading@10 4 * This file is part of FFmpeg.
yading@10 5 *
yading@10 6 * FFmpeg is free software; you can redistribute it and/or
yading@10 7 * modify it under the terms of the GNU Lesser General Public
yading@10 8 * License as published by the Free Software Foundation; either
yading@10 9 * version 2.1 of the License, or (at your option) any later version.
yading@10 10 *
yading@10 11 * FFmpeg is distributed in the hope that it will be useful,
yading@10 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 14 * Lesser General Public License for more details.
yading@10 15 *
yading@10 16 * You should have received a copy of the GNU Lesser General Public
yading@10 17 * License along with FFmpeg; if not, write to the Free Software
yading@10 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 19 */
yading@10 20
yading@10 21 /**
yading@10 22 * @file
yading@10 23 * aspect ratio modification video filters
yading@10 24 */
yading@10 25
yading@10 26 #include <float.h>
yading@10 27
yading@10 28 #include "libavutil/common.h"
yading@10 29 #include "libavutil/eval.h"
yading@10 30 #include "libavutil/mathematics.h"
yading@10 31 #include "libavutil/opt.h"
yading@10 32 #include "libavutil/parseutils.h"
yading@10 33
yading@10 34 #include "avfilter.h"
yading@10 35 #include "internal.h"
yading@10 36 #include "video.h"
yading@10 37
yading@10 38 typedef struct {
yading@10 39 const AVClass *class;
yading@10 40 AVRational aspect;
yading@10 41 int max;
yading@10 42 #if FF_API_OLD_FILTER_OPTS
yading@10 43 float aspect_den;
yading@10 44 #endif
yading@10 45 char *ratio_str;
yading@10 46 } AspectContext;
yading@10 47
yading@10 48 static av_cold int init(AVFilterContext *ctx)
yading@10 49 {
yading@10 50 AspectContext *s = ctx->priv;
yading@10 51 int ret;
yading@10 52
yading@10 53 #if FF_API_OLD_FILTER_OPTS
yading@10 54 if (s->ratio_str && s->aspect_den > 0) {
yading@10 55 double num;
yading@10 56 av_log(ctx, AV_LOG_WARNING,
yading@10 57 "num:den syntax is deprecated, please use num/den or named options instead\n");
yading@10 58 ret = av_expr_parse_and_eval(&num, s->ratio_str, NULL, NULL,
yading@10 59 NULL, NULL, NULL, NULL, NULL, 0, ctx);
yading@10 60 if (ret < 0) {
yading@10 61 av_log(ctx, AV_LOG_ERROR, "Unable to parse ratio numerator \"%s\"\n", s->ratio_str);
yading@10 62 return AVERROR(EINVAL);
yading@10 63 }
yading@10 64 s->aspect = av_d2q(num / s->aspect_den, s->max);
yading@10 65 } else
yading@10 66 #endif
yading@10 67 if (s->ratio_str) {
yading@10 68 ret = av_parse_ratio(&s->aspect, s->ratio_str, s->max, 0, ctx);
yading@10 69 if (ret < 0 || s->aspect.num < 0 || s->aspect.den <= 0) {
yading@10 70 av_log(ctx, AV_LOG_ERROR,
yading@10 71 "Invalid string '%s' for aspect ratio\n", s->ratio_str);
yading@10 72 return AVERROR(EINVAL);
yading@10 73 }
yading@10 74 }
yading@10 75 return 0;
yading@10 76 }
yading@10 77
yading@10 78 static int filter_frame(AVFilterLink *link, AVFrame *frame)
yading@10 79 {
yading@10 80 AspectContext *aspect = link->dst->priv;
yading@10 81
yading@10 82 frame->sample_aspect_ratio = aspect->aspect;
yading@10 83 return ff_filter_frame(link->dst->outputs[0], frame);
yading@10 84 }
yading@10 85
yading@10 86 #define OFFSET(x) offsetof(AspectContext, x)
yading@10 87 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
yading@10 88
yading@10 89 static inline void compute_dar(AVRational *dar, AVRational sar, int w, int h)
yading@10 90 {
yading@10 91 if (sar.num && sar.den) {
yading@10 92 av_reduce(&dar->num, &dar->den, sar.num * w, sar.den * h, INT_MAX);
yading@10 93 } else {
yading@10 94 av_reduce(&dar->num, &dar->den, w, h, INT_MAX);
yading@10 95 }
yading@10 96 }
yading@10 97
yading@10 98 #if CONFIG_SETDAR_FILTER
yading@10 99
yading@10 100 static int setdar_config_props(AVFilterLink *inlink)
yading@10 101 {
yading@10 102 AspectContext *aspect = inlink->dst->priv;
yading@10 103 AVRational dar = aspect->aspect, old_dar;
yading@10 104 AVRational old_sar = inlink->sample_aspect_ratio;
yading@10 105
yading@10 106 if (aspect->aspect.num && aspect->aspect.den) {
yading@10 107 av_reduce(&aspect->aspect.num, &aspect->aspect.den,
yading@10 108 aspect->aspect.num * inlink->h,
yading@10 109 aspect->aspect.den * inlink->w, INT_MAX);
yading@10 110 inlink->sample_aspect_ratio = aspect->aspect;
yading@10 111 } else {
yading@10 112 inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
yading@10 113 dar = (AVRational){ inlink->w, inlink->h };
yading@10 114 }
yading@10 115
yading@10 116 compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
yading@10 117 av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d dar:%d/%d sar:%d/%d -> dar:%d/%d sar:%d/%d\n",
yading@10 118 inlink->w, inlink->h, old_dar.num, old_dar.den, old_sar.num, old_sar.den,
yading@10 119 dar.num, dar.den, inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
yading@10 120
yading@10 121 return 0;
yading@10 122 }
yading@10 123
yading@10 124 static const AVOption setdar_options[] = {
yading@10 125 { "dar", "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
yading@10 126 { "ratio", "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
yading@10 127 { "r", "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
yading@10 128 #if FF_API_OLD_FILTER_OPTS
yading@10 129 { "dar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
yading@10 130 #endif
yading@10 131 { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
yading@10 132 { NULL }
yading@10 133 };
yading@10 134
yading@10 135 AVFILTER_DEFINE_CLASS(setdar);
yading@10 136
yading@10 137 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
yading@10 138 {
yading@10 139 .name = "default",
yading@10 140 .type = AVMEDIA_TYPE_VIDEO,
yading@10 141 .config_props = setdar_config_props,
yading@10 142 .get_video_buffer = ff_null_get_video_buffer,
yading@10 143 .filter_frame = filter_frame,
yading@10 144 },
yading@10 145 { NULL }
yading@10 146 };
yading@10 147
yading@10 148 static const AVFilterPad avfilter_vf_setdar_outputs[] = {
yading@10 149 {
yading@10 150 .name = "default",
yading@10 151 .type = AVMEDIA_TYPE_VIDEO,
yading@10 152 },
yading@10 153 { NULL }
yading@10 154 };
yading@10 155
yading@10 156 AVFilter avfilter_vf_setdar = {
yading@10 157 .name = "setdar",
yading@10 158 .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
yading@10 159 .init = init,
yading@10 160 .priv_size = sizeof(AspectContext),
yading@10 161 .priv_class = &setdar_class,
yading@10 162
yading@10 163 .inputs = avfilter_vf_setdar_inputs,
yading@10 164
yading@10 165 .outputs = avfilter_vf_setdar_outputs,
yading@10 166 };
yading@10 167
yading@10 168 #endif /* CONFIG_SETDAR_FILTER */
yading@10 169
yading@10 170 #if CONFIG_SETSAR_FILTER
yading@10 171
yading@10 172 static int setsar_config_props(AVFilterLink *inlink)
yading@10 173 {
yading@10 174 AspectContext *aspect = inlink->dst->priv;
yading@10 175 AVRational old_sar = inlink->sample_aspect_ratio;
yading@10 176 AVRational old_dar, dar;
yading@10 177
yading@10 178 inlink->sample_aspect_ratio = aspect->aspect;
yading@10 179
yading@10 180 compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
yading@10 181 compute_dar(&dar, aspect->aspect, inlink->w, inlink->h);
yading@10 182 av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d sar:%d/%d dar:%d/%d -> sar:%d/%d dar:%d/%d\n",
yading@10 183 inlink->w, inlink->h, old_sar.num, old_sar.den, old_dar.num, old_dar.den,
yading@10 184 inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den, dar.num, dar.den);
yading@10 185
yading@10 186 return 0;
yading@10 187 }
yading@10 188
yading@10 189 static const AVOption setsar_options[] = {
yading@10 190 { "sar", "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
yading@10 191 { "ratio", "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
yading@10 192 { "r", "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
yading@10 193 #if FF_API_OLD_FILTER_OPTS
yading@10 194 { "sar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
yading@10 195 #endif
yading@10 196 { "max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
yading@10 197 { NULL }
yading@10 198 };
yading@10 199
yading@10 200 AVFILTER_DEFINE_CLASS(setsar);
yading@10 201
yading@10 202 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
yading@10 203 {
yading@10 204 .name = "default",
yading@10 205 .type = AVMEDIA_TYPE_VIDEO,
yading@10 206 .config_props = setsar_config_props,
yading@10 207 .get_video_buffer = ff_null_get_video_buffer,
yading@10 208 .filter_frame = filter_frame,
yading@10 209 },
yading@10 210 { NULL }
yading@10 211 };
yading@10 212
yading@10 213 static const AVFilterPad avfilter_vf_setsar_outputs[] = {
yading@10 214 {
yading@10 215 .name = "default",
yading@10 216 .type = AVMEDIA_TYPE_VIDEO,
yading@10 217 },
yading@10 218 { NULL }
yading@10 219 };
yading@10 220
yading@10 221 AVFilter avfilter_vf_setsar = {
yading@10 222 .name = "setsar",
yading@10 223 .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
yading@10 224 .init = init,
yading@10 225 .priv_size = sizeof(AspectContext),
yading@10 226 .priv_class = &setsar_class,
yading@10 227
yading@10 228 .inputs = avfilter_vf_setsar_inputs,
yading@10 229
yading@10 230 .outputs = avfilter_vf_setsar_outputs,
yading@10 231 };
yading@10 232
yading@10 233 #endif /* CONFIG_SETSAR_FILTER */