lavfi: move tracing utilities definition from video.c to avfilter.c
[ffmpeg.git] / libavfilter / vf_aspect.c
index cdb5390..685c96f 100644 (file)
  * aspect ratio modification video filters
  */
 
+#include "libavutil/mathematics.h"
+#include "libavutil/parseutils.h"
 #include "avfilter.h"
+#include "video.h"
 
 typedef struct {
-    AVRational aspect;
+    AVRational ratio;
 } AspectContext;
 
 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
 {
     AspectContext *aspect = ctx->priv;
-    double  ratio;
-    int64_t gcd;
+    aspect->ratio = (AVRational) {0, 1};
 
     if (args) {
-        if (sscanf(args, "%d:%d", &aspect->aspect.num, &aspect->aspect.den) < 2) {
-            if (sscanf(args, "%lf", &ratio) < 1)
-                return -1;
-            aspect->aspect = av_d2q(ratio, 100);
-        } else {
-            gcd = av_gcd(FFABS(aspect->aspect.num), FFABS(aspect->aspect.den));
-            if (gcd) {
-                aspect->aspect.num /= gcd;
-                aspect->aspect.den /= gcd;
-            }
+        if (av_parse_ratio(&aspect->ratio, args, 100, 0, ctx) < 0 ||
+            aspect->ratio.num < 0 || aspect->ratio.den <= 0) {
+            av_log(ctx, AV_LOG_ERROR,
+                   "Invalid string '%s' for aspect ratio.\n", args);
+            return AVERROR(EINVAL);
         }
     }
 
-    if (aspect->aspect.den == 0)
-        aspect->aspect = (AVRational) {0, 1};
-
+    av_log(ctx, AV_LOG_INFO, "a:%d/%d\n", aspect->ratio.num, aspect->ratio.den);
     return 0;
 }
 
@@ -59,64 +54,78 @@ static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
 {
     AspectContext *aspect = link->dst->priv;
 
-    picref->video->pixel_aspect = aspect->aspect;
+    picref->video->sample_aspect_ratio = aspect->ratio;
     avfilter_start_frame(link->dst->outputs[0], picref);
 }
 
-#if CONFIG_ASPECT_FILTER
-/* for aspect filter, convert from frame aspect ratio to pixel aspect ratio */
-static int frameaspect_config_props(AVFilterLink *inlink)
+#if CONFIG_SETDAR_FILTER
+static int setdar_config_props(AVFilterLink *inlink)
 {
     AspectContext *aspect = inlink->dst->priv;
+    AVRational dar = aspect->ratio;
+
+    av_reduce(&aspect->ratio.num, &aspect->ratio.den,
+               aspect->ratio.num * inlink->h,
+               aspect->ratio.den * inlink->w, 100);
+
+    av_log(inlink->dst, AV_LOG_INFO, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
+           inlink->w, inlink->h, dar.num, dar.den, aspect->ratio.num, aspect->ratio.den);
 
-    av_reduce(&aspect->aspect.num, &aspect->aspect.den,
-               aspect->aspect.num * inlink->h,
-               aspect->aspect.den * inlink->w, 100);
+    inlink->sample_aspect_ratio = aspect->ratio;
 
     return 0;
 }
 
-AVFilter avfilter_vf_aspect = {
-    .name      = "aspect",
-    .description = NULL_IF_CONFIG_SMALL("Set the frame aspect ratio."),
+AVFilter avfilter_vf_setdar = {
+    .name      = "setdar",
+    .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
 
     .init      = init,
 
     .priv_size = sizeof(AspectContext),
 
-    .inputs    = (AVFilterPad[]) {{ .name             = "default",
+    .inputs    = (const AVFilterPad[]) {{ .name       = "default",
                                     .type             = AVMEDIA_TYPE_VIDEO,
-                                    .config_props     = frameaspect_config_props,
-                                    .get_video_buffer = avfilter_null_get_video_buffer,
+                                    .config_props     = setdar_config_props,
+                                    .get_video_buffer = ff_null_get_video_buffer,
                                     .start_frame      = start_frame,
-                                    .end_frame        = avfilter_null_end_frame },
+                                    .end_frame        = ff_null_end_frame },
                                   { .name = NULL}},
 
-    .outputs   = (AVFilterPad[]) {{ .name             = "default",
+    .outputs   = (const AVFilterPad[]) {{ .name       = "default",
                                     .type             = AVMEDIA_TYPE_VIDEO, },
                                   { .name = NULL}},
 };
-#endif /* CONFIG_ASPECT_FILTER */
+#endif /* CONFIG_SETDAR_FILTER */
+
+#if CONFIG_SETSAR_FILTER
+static int setsar_config_props(AVFilterLink *inlink)
+{
+    AspectContext *aspect = inlink->dst->priv;
 
-#if CONFIG_PIXELASPECT_FILTER
-AVFilter avfilter_vf_pixelaspect = {
-    .name      = "pixelaspect",
-    .description = NULL_IF_CONFIG_SMALL("Set the pixel aspect ratio."),
+    inlink->sample_aspect_ratio = aspect->ratio;
+
+    return 0;
+}
+
+AVFilter avfilter_vf_setsar = {
+    .name      = "setsar",
+    .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
 
     .init      = init,
 
     .priv_size = sizeof(AspectContext),
 
-    .inputs    = (AVFilterPad[]) {{ .name             = "default",
+    .inputs    = (const AVFilterPad[]) {{ .name       = "default",
                                     .type             = AVMEDIA_TYPE_VIDEO,
-                                    .get_video_buffer = avfilter_null_get_video_buffer,
+                                    .config_props     = setsar_config_props,
+                                    .get_video_buffer = ff_null_get_video_buffer,
                                     .start_frame      = start_frame,
-                                    .end_frame        = avfilter_null_end_frame },
+                                    .end_frame        = ff_null_end_frame },
                                   { .name = NULL}},
 
-    .outputs   = (AVFilterPad[]) {{ .name             = "default",
+    .outputs   = (const AVFilterPad[]) {{ .name       = "default",
                                     .type             = AVMEDIA_TYPE_VIDEO, },
                                   { .name = NULL}},
 };
-#endif /* CONFIG_PIXELASPECT_FILTER */
-
+#endif /* CONFIG_SETSAR_FILTER */