Libav 0.7.1
|
00001 /* 00002 * Copyright (c) 2010 Bobby Bingham 00003 00004 * This file is part of Libav. 00005 * 00006 * Libav 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 * Libav 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 Libav; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 */ 00020 00026 #include "avfilter.h" 00027 00028 typedef struct { 00029 AVRational aspect; 00030 } AspectContext; 00031 00032 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) 00033 { 00034 AspectContext *aspect = ctx->priv; 00035 double ratio; 00036 int64_t gcd; 00037 char c = 0; 00038 00039 if (args) { 00040 if (sscanf(args, "%d:%d%c", &aspect->aspect.num, &aspect->aspect.den, &c) != 2) 00041 if (sscanf(args, "%lf%c", &ratio, &c) == 1) 00042 aspect->aspect = av_d2q(ratio, 100); 00043 00044 if (c || aspect->aspect.num <= 0 || aspect->aspect.den <= 0) { 00045 av_log(ctx, AV_LOG_ERROR, 00046 "Invalid string '%s' for aspect ratio.\n", args); 00047 return AVERROR(EINVAL); 00048 } 00049 00050 gcd = av_gcd(FFABS(aspect->aspect.num), FFABS(aspect->aspect.den)); 00051 if (gcd) { 00052 aspect->aspect.num /= gcd; 00053 aspect->aspect.den /= gcd; 00054 } 00055 } 00056 00057 if (aspect->aspect.den == 0) 00058 aspect->aspect = (AVRational) {0, 1}; 00059 00060 av_log(ctx, AV_LOG_INFO, "a:%d/%d\n", aspect->aspect.num, aspect->aspect.den); 00061 return 0; 00062 } 00063 00064 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref) 00065 { 00066 AspectContext *aspect = link->dst->priv; 00067 00068 picref->video->pixel_aspect = aspect->aspect; 00069 avfilter_start_frame(link->dst->outputs[0], picref); 00070 } 00071 00072 #if CONFIG_SETDAR_FILTER 00073 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */ 00074 static int setdar_config_props(AVFilterLink *inlink) 00075 { 00076 AspectContext *aspect = inlink->dst->priv; 00077 AVRational dar = aspect->aspect; 00078 00079 av_reduce(&aspect->aspect.num, &aspect->aspect.den, 00080 aspect->aspect.num * inlink->h, 00081 aspect->aspect.den * inlink->w, 100); 00082 00083 av_log(inlink->dst, AV_LOG_INFO, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n", 00084 inlink->w, inlink->h, dar.num, dar.den, aspect->aspect.num, aspect->aspect.den); 00085 00086 inlink->sample_aspect_ratio = aspect->aspect; 00087 00088 return 0; 00089 } 00090 00091 AVFilter avfilter_vf_setdar = { 00092 .name = "setdar", 00093 .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."), 00094 00095 .init = init, 00096 00097 .priv_size = sizeof(AspectContext), 00098 00099 .inputs = (AVFilterPad[]) {{ .name = "default", 00100 .type = AVMEDIA_TYPE_VIDEO, 00101 .config_props = setdar_config_props, 00102 .get_video_buffer = avfilter_null_get_video_buffer, 00103 .start_frame = start_frame, 00104 .end_frame = avfilter_null_end_frame }, 00105 { .name = NULL}}, 00106 00107 .outputs = (AVFilterPad[]) {{ .name = "default", 00108 .type = AVMEDIA_TYPE_VIDEO, }, 00109 { .name = NULL}}, 00110 }; 00111 #endif /* CONFIG_SETDAR_FILTER */ 00112 00113 #if CONFIG_SETSAR_FILTER 00114 /* for setdar filter, convert from frame aspect ratio to pixel aspect ratio */ 00115 static int setsar_config_props(AVFilterLink *inlink) 00116 { 00117 AspectContext *aspect = inlink->dst->priv; 00118 00119 inlink->sample_aspect_ratio = aspect->aspect; 00120 00121 return 0; 00122 } 00123 00124 AVFilter avfilter_vf_setsar = { 00125 .name = "setsar", 00126 .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."), 00127 00128 .init = init, 00129 00130 .priv_size = sizeof(AspectContext), 00131 00132 .inputs = (AVFilterPad[]) {{ .name = "default", 00133 .type = AVMEDIA_TYPE_VIDEO, 00134 .config_props = setsar_config_props, 00135 .get_video_buffer = avfilter_null_get_video_buffer, 00136 .start_frame = start_frame, 00137 .end_frame = avfilter_null_end_frame }, 00138 { .name = NULL}}, 00139 00140 .outputs = (AVFilterPad[]) {{ .name = "default", 00141 .type = AVMEDIA_TYPE_VIDEO, }, 00142 { .name = NULL}}, 00143 }; 00144 #endif /* CONFIG_SETSAR_FILTER */ 00145