[Patch] Scale filter should use multiples of 2
Hey, Attached is a small patch to make the vf_scale filter always produce multiples of 2 when the -1 option is used for either width or height to keep the aspect ratio. This lets you use it properly with some encoders, like libx264, that would otherwise fail, e.g: ffmpeg -i myfile.mp4 -vf scale=320:-1 -vcodec libx264 -vpre default -crf 21 test.mp4 ... [scale @ 0xa43c580]w:390 h:240 fmt:yuv420p -> w:320 h:197 fmt:yuv420p flags:0x2 ... [libx264 @ 0xa43b200]width or height not divisible by 2 (320x197) ... Error while opening encoder for output stream #0.0 - maybe incorrect parameters such as bit_rate, rate, width or height After the patch is applied it scaled to 320x198 and works perfectly. Take care, -- Daniel G. Taylor http://programmer-art.org/
On 6/29/10 12:02 PM, Daniel G. Taylor wrote:
Hey,
Attached is a small patch to make the vf_scale filter always produce multiples of 2 when the -1 option is used for either width or height to keep the aspect ratio. This lets you use it properly with some encoders, like libx264, that would otherwise fail, e.g:
This depends on the output pixel format. For 420, indeed it would be useful. -- Baptiste COUDURIER Key fingerprint 8D77134D20CC9220201FC5DB0AC9325C5C1ABAAA FFmpeg maintainer http://www.ffmpeg.org
On date Tuesday 2010-06-29 12:56:17 -0700, Baptiste Coudurier encoded:
On 6/29/10 12:02 PM, Daniel G. Taylor wrote:
Hey,
Attached is a small patch to make the vf_scale filter always produce multiples of 2 when the -1 option is used for either width or height to keep the aspect ratio. This lets you use it properly with some encoders, like libx264, that would otherwise fail, e.g:
This depends on the output pixel format. For 420, indeed it would be useful.
Odd-size rescaling was a feature explicitely requested by Michael. I'm not against adding an option to make it only issues even-sized image though. A yet better solution would be to make the output size parametric (IIRC there is already a patch by Baptiste for that in the ML), and now that the eval API is public there is no excuse for not working on it. Regards. -- FFmpeg = Fostering & Fierce Magical Power Extravagant Gorilla
On Wed, Jun 30, 2010 at 01:04:36AM +0200, Stefano Sabatini wrote:
On date Tuesday 2010-06-29 12:56:17 -0700, Baptiste Coudurier encoded:
On 6/29/10 12:02 PM, Daniel G. Taylor wrote:
Hey,
Attached is a small patch to make the vf_scale filter always produce multiples of 2 when the -1 option is used for either width or height to keep the aspect ratio. This lets you use it properly with some encoders, like libx264, that would otherwise fail, e.g:
This depends on the output pixel format. For 420, indeed it would be useful.
Odd-size rescaling was a feature explicitely requested by Michael. I'm not against adding an option to make it only issues even-sized image though.
I insisted possibly on it being supported (that is important) but doing it by default with 420 seems odd [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Into a blind darkness they enter who follow after the Ignorance, they as if into a greater darkness enter who devote themselves to the Knowledge alone. -- Isha Upanishad
On 06/29/2010 08:16 PM, Michael Niedermayer wrote:
On Wed, Jun 30, 2010 at 01:04:36AM +0200, Stefano Sabatini wrote:
On date Tuesday 2010-06-29 12:56:17 -0700, Baptiste Coudurier encoded:
On 6/29/10 12:02 PM, Daniel G. Taylor wrote:
Hey,
Attached is a small patch to make the vf_scale filter always produce multiples of 2 when the -1 option is used for either width or height to keep the aspect ratio. This lets you use it properly with some encoders, like libx264, that would otherwise fail, e.g:
This depends on the output pixel format. For 420, indeed it would be useful.
Odd-size rescaling was a feature explicitely requested by Michael. I'm not against adding an option to make it only issues even-sized image though.
I insisted possibly on it being supported (that is important) but doing it by default with 420 seems odd
What about attached patch? It changes the behavior so that a width or height of -1 remains the same as it is now while adding the ability to use other negative values to mean "scale proportionately but clip to the closest multiple of X," so e.g. scale=320:-1 => 320x197 scale=320:-2 => 320x198 scale=320:-8 => 320x200 scale=320:-16 => 320x192 This can be useful for other codecs that wish to have dimensions as multiples of 2, 8, 16, etc. In the future we could even auto-insert a scaler to do this when needed by the chosen vcodec. Thoughts? I tried to use FFALIGN but it seems to always round up. Take care, -- Daniel G. Taylor http://programmer-art.org/
On date Wednesday 2010-06-30 13:21:03 -0400, Daniel G. Taylor encoded:
On 06/29/2010 08:16 PM, Michael Niedermayer wrote:
On Wed, Jun 30, 2010 at 01:04:36AM +0200, Stefano Sabatini wrote:
On date Tuesday 2010-06-29 12:56:17 -0700, Baptiste Coudurier encoded:
On 6/29/10 12:02 PM, Daniel G. Taylor wrote:
Hey,
Attached is a small patch to make the vf_scale filter always produce multiples of 2 when the -1 option is used for either width or height to keep the aspect ratio. This lets you use it properly with some encoders, like libx264, that would otherwise fail, e.g:
This depends on the output pixel format. For 420, indeed it would be useful.
Odd-size rescaling was a feature explicitely requested by Michael. I'm not against adding an option to make it only issues even-sized image though.
I insisted possibly on it being supported (that is important) but doing it by default with 420 seems odd
What about attached patch? It changes the behavior so that a width or height of -1 remains the same as it is now while adding the ability to use other negative values to mean "scale proportionately but clip to the closest multiple of X," so e.g.
scale=320:-1 => 320x197 scale=320:-2 => 320x198 scale=320:-8 => 320x200 scale=320:-16 => 320x192
This can be useful for other codecs that wish to have dimensions as multiples of 2, 8, 16, etc. In the future we could even auto-insert a scaler to do this when needed by the chosen vcodec.
Thoughts? I tried to use FFALIGN but it seems to always round up.
Take care, -- Daniel G. Taylor http://programmer-art.org/
Index: libavfilter/vf_scale.c =================================================================== --- libavfilter/vf_scale.c (revision 23891) +++ libavfilter/vf_scale.c (working copy) @@ -33,7 +33,7 @@ /** * New dimensions. Special values are: * 0 = original width/height - * -1 = keep original aspect + * -x = keep original aspect and make mod-x, e.g. -16 for multiples of 16
Just a question, couldn't this be achieved using parametric values for w:h? I don't want to add code and complexity when all this may be accomplished by a more general mechanism. For example we may have a = w / h w'/h' = a = w/h h' = w' / a scale=OUT_W:mod(OUT_W/a, 2) Regards. -- FFmpeg = Fiendish and Fostering Mean Purposeless Elastic Glue
On Thu, Jul 01, 2010 at 01:40:55AM +0200, Stefano Sabatini wrote: [...]
scale=OUT_W:mod(OUT_W/a, 2)
x%2 will not produce what you expect you want something like: OUT_W:round(OUT_H, 2) [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Those who are too smart to engage in politics are punished by being governed by those who are dumber. -- Plato
On 06/30/2010 07:40 PM, Stefano Sabatini wrote:
Index: libavfilter/vf_scale.c =================================================================== --- libavfilter/vf_scale.c (revision 23891) +++ libavfilter/vf_scale.c (working copy) @@ -33,7 +33,7 @@ /** * New dimensions. Special values are: * 0 = original width/height - * -1 = keep original aspect + * -x = keep original aspect and make mod-x, e.g. -16 for multiples of 16
Just a question, couldn't this be achieved using parametric values for w:h?
I don't want to add code and complexity when all this may be accomplished by a more general mechanism.
For example we may have a = w / h w'/h' = a = w/h h' = w' / a
scale=OUT_W:mod(OUT_W/a, 2)
I suppose that will work (and Michael is right about it being round() instead of mod), but doesn't having to parse such values make this incredibly more complex? Are there utilities in libav* to make parsing that easy? Also, what other use cases are there for such a system? We can round to the nearest multiple, but what else would it do that warrants such a system? Basically, I don't know how I can do this easily, and writing a bunch of code doing it a stupid way seems like a waste of time, so any advice would be appreciated if we do want to go this way. Take care, -- Daniel G. Taylor http://programmer-art.org/
On date Thursday 2010-07-01 10:00:18 -0400, Daniel G. Taylor encoded:
On 06/30/2010 07:40 PM, Stefano Sabatini wrote:
Index: libavfilter/vf_scale.c =================================================================== --- libavfilter/vf_scale.c (revision 23891) +++ libavfilter/vf_scale.c (working copy) @@ -33,7 +33,7 @@ /** * New dimensions. Special values are: * 0 = original width/height - * -1 = keep original aspect + * -x = keep original aspect and make mod-x, e.g. -16 for multiples of 16
Just a question, couldn't this be achieved using parametric values for w:h?
I don't want to add code and complexity when all this may be accomplished by a more general mechanism.
For example we may have a = w / h w'/h' = a = w/h h' = w' / a
scale=OUT_W:mod(OUT_W/a, 2)
I suppose that will work (and Michael is right about it being round() instead of mod), but doesn't having to parse such values make this incredibly more complex? Are there utilities in libav* to make parsing that easy?
Also, what other use cases are there for such a system? We can round to the nearest multiple, but what else would it do that warrants such a system?
For example you may to parametrically change the size: # double size scale=2*w:2*h # decrease size by 50% scale=w/2:h/2 # invert w/h sizes scale=h:w # seek for greek harmony scale=h*PHI:h
Basically, I don't know how I can do this easily, and writing a bunch of code doing it a stupid way seems like a waste of time, so any advice would be appreciated if we do want to go this way.
libavutil/eval.h, check how it is done in the libavfilter-soc overlay and setpts filters. This requires to add eval evalutation to the scale filter, check the recent patch by Baptiste on which I commented yesterday. Regards. -- FFmpeg = Fundamentalist & Faithless Maxi Ponderous Exploitable Gymnast
On Thu, Jul 01, 2010 at 04:27:33PM +0200, Stefano Sabatini wrote:
On date Thursday 2010-07-01 10:00:18 -0400, Daniel G. Taylor encoded:
On 06/30/2010 07:40 PM, Stefano Sabatini wrote:
Index: libavfilter/vf_scale.c =================================================================== --- libavfilter/vf_scale.c (revision 23891) +++ libavfilter/vf_scale.c (working copy) @@ -33,7 +33,7 @@ /** * New dimensions. Special values are: * 0 = original width/height - * -1 = keep original aspect + * -x = keep original aspect and make mod-x, e.g. -16 for multiples of 16
Just a question, couldn't this be achieved using parametric values for w:h?
I don't want to add code and complexity when all this may be accomplished by a more general mechanism.
For example we may have a = w / h w'/h' = a = w/h h' = w' / a
scale=OUT_W:mod(OUT_W/a, 2)
I suppose that will work (and Michael is right about it being round() instead of mod), but doesn't having to parse such values make this incredibly more complex? Are there utilities in libav* to make parsing that easy?
Also, what other use cases are there for such a system? We can round to the nearest multiple, but what else would it do that warrants such a system?
For example you may to parametrically change the size: # double size scale=2*w:2*h # decrease size by 50% scale=w/2:h/2 # invert w/h sizes scale=h:w # seek for greek harmony scale=h*PHI:h
include time and all kinds of odd effects become possible [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB The bravest are surely those who have the clearest vision of what is before them, glory and danger alike, and yet notwithstanding go out to meet it. -- Thucydides
On date Thursday 2010-07-01 23:28:03 +0200, Michael Niedermayer encoded:
On Thu, Jul 01, 2010 at 04:27:33PM +0200, Stefano Sabatini wrote:
On date Thursday 2010-07-01 10:00:18 -0400, Daniel G. Taylor encoded: [...]
Also, what other use cases are there for such a system? We can round to the nearest multiple, but what else would it do that warrants such a system?
For example you may to parametrically change the size: # double size scale=2*w:2*h # decrease size by 50% scale=w/2:h/2 # invert w/h sizes scale=h:w # seek for greek harmony scale=h*PHI:h
include time and all kinds of odd effects become possible
This would require scale context re-initialization and filter graph re-configuration, it's a little complicated but would be definitively interesting. Regards. -- FFmpeg = Fast & Furious Meaningless Peaceful Enhancing Gadget
On 07/01/2010 10:27 AM, Stefano Sabatini wrote:
Basically, I don't know how I can do this easily, and writing a bunch of code doing it a stupid way seems like a waste of time, so any advice would be appreciated if we do want to go this way.
libavutil/eval.h, check how it is done in the libavfilter-soc overlay and setpts filters. This requires to add eval evalutation to the scale filter, check the recent patch by Baptiste on which I commented yesterday.
Thanks for the help here Stefano. I've run into a bit of a snag I'd say... See attached patch to reproduce what I'm running into, but basically: * av_parse_expr and such use a comma as a delimiter for function arguments * avfilter's graph parsing uses a comma as a delimiter for filters Because of that it means anytime you put a comma into the filter arguments it thinks you are passing in the next filter. If we are going to allow functions and such as filter arguments it might be a good idea to change the syntax for specifying filters, maybe using a pipe character like "|" or something, e.g. -vf "scale|unsharp|...|crop". Attached patch works for stuff like -vf "scale=320:240" or -vf "scale=W/2:H/2" and such but if you try -vf "scale=W:round(H, 16)" you'll immediately see the issue. Thoughts? Take care, -- Daniel G. Taylor http://programmer-art.org/
On date Wednesday 2010-07-07 15:53:16 -0400, Daniel G. Taylor encoded:
On 07/01/2010 10:27 AM, Stefano Sabatini wrote:
Basically, I don't know how I can do this easily, and writing a bunch of code doing it a stupid way seems like a waste of time, so any advice would be appreciated if we do want to go this way.
libavutil/eval.h, check how it is done in the libavfilter-soc overlay and setpts filters. This requires to add eval evalutation to the scale filter, check the recent patch by Baptiste on which I commented yesterday.
Thanks for the help here Stefano.
I've run into a bit of a snag I'd say... See attached patch to reproduce what I'm running into, but basically:
* av_parse_expr and such use a comma as a delimiter for function arguments * avfilter's graph parsing uses a comma as a delimiter for filters
Because of that it means anytime you put a comma into the filter arguments it thinks you are passing in the next filter. If we are going to allow functions and such as filter arguments it might be a good idea to change the syntax for specifying filters, maybe using a pipe character like "|" or something, e.g. -vf "scale|unsharp|...|crop".
Attached patch works for stuff like -vf "scale=320:240" or -vf "scale=W/2:H/2" and such but if you try -vf "scale=W:round(H, 16)" you'll immediately see the issue.
Thoughts?
Possible solutions: 1) change the character currently used, which is ",", and which is very likely to appear in expressions inside the filter args. '|' is an option, note that we didn't choose '|' in the first place because it's a special sh character, for example this doesn't work in sh: -vf scale|unsharp|crop you need: -vf "scale|unsharp|crop" For me this doesn't look like a big issue, but we should wart the users in the docs. The requirement is that the choosen character cannot compare inside the filter args. 2) make the separator definable by the user, for example we could have something like: -vfd "%" -vf "scale % unsharp % ... % crop". 3) Implement an escaping mechanism. Since at the first level of escaping there is just one special character, we may use '\' to escape the ',', for example: scale="W:round(H\, 16), unsharp, crop" The problem with this approach is that combined with the shell escaping mechanism this can easily became an escaping hell. So I tend to prefer the option 2, and use by default the value "|" for the separator.
Take care, -- Daniel G. Taylor http://programmer-art.org/
Index: libavfilter/vf_scale.c =================================================================== --- libavfilter/vf_scale.c (revision 24077) +++ libavfilter/vf_scale.c (working copy) @@ -1,5 +1,6 @@ /* * copyright (c) 2007 Bobby Bingham + * copyright (c) 2010 Daniel G. Taylor <dan at programmer-art.org> * * This file is part of FFmpeg. * @@ -24,9 +25,41 @@ */
#include "avfilter.h" +#include "libavutil/eval.h" +#include "libavutil/mem.h" #include "libavutil/pixdesc.h" #include "libswscale/swscale.h"
+static const char *const_names[]={ + "W", + "H", + NULL +};
While at it add also some nice constant, PI, E, and convince Michael that PHI is cool to have in FFmpeg ;-).
+ +enum PosOfValue { + POV_W, + POV_H, + POV_NULL +};
PosOfValue => poor name, you choose a bad example (setpts rather than overlay ;-))
+ +static inline double round_clip(void *ctx, double arg1, double arg2) +{ + int orig = (int)arg1; + int clip = (int)arg2; + + return orig -= (orig % clip < clip / 2) ? orig % clip : -(clip - (orig % clip)); +} + +static const char *func2_names[]={ + "round", + NULL +}; + +static double (* const *funcs2[])(void *, double, double)={ + (void *)round_clip, + NULL +}; + typedef struct { struct SwsContext *sws; ///< software scaler context
@@ -41,27 +74,44 @@ int hsub, vsub; ///< chroma subsampling int slice_y; ///< top of current output slice int input_is_pal; ///< set to 1 if the input format is paletted
+ AVExpr *wexpr; ///< width expression parser + AVExpr *hexpr; ///< height expression parser
is not a parser, I'd say just "expression", also w_expr/h_expr is more readable IMO
+ double const_values[POV_NULL+1]; ///< expression parsing constants } ScaleContext;
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) { ScaleContext *scale = ctx->priv; const char *p; + char *cp, *width, *height; + int ret; + char *save_ptr = NULL;
scale->flags = SWS_BILINEAR;
if (args){ - sscanf(args, "%d:%d", &scale->w, &scale->h); + cp = av_strdup(args); + width = strtok_r(cp, ":", &save_ptr); + height = strtok_r(NULL, ":", &save_ptr); p= strstr(args,"flags="); if(p) scale->flags= strtoul(p+6, NULL, 0); }
why not use sscanf ("%255[^:]:%255[^:]") ? Looks simpler. Also this is leaking cp, and you're forgetting to assign the default expression to w and h. As for strtok_r, since it is not C99 I believe we may need to add a check for that in the configure (I didn't know that strtok was not re-entrant, is that really true?).
- - /* sanity check params */ - if (scale->w < -1 || scale->h < -1) { - av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n"); - return -1; + + ret = av_parse_expr(&scale->wexpr, args ? width : "W", + const_names, NULL, NULL, func2_names, funcs2, 0, ctx); + if (ret < 0) { + av_log(ctx, AV_LOG_ERROR, + "Error while parsing width expression '%s'\n", width); + return ret; } - if (scale->w == -1 && scale->h == -1) - scale->w = scale->h = 0; + + ret = av_parse_expr(&scale->hexpr, args ? height : "H", + const_names, NULL, NULL, func2_names, funcs2, 0, ctx); + if (ret < 0) { + av_log(ctx, AV_LOG_ERROR, + "Error while parsing height expression '%s'\n", height); + return ret; + }
return 0; } @@ -109,6 +159,12 @@ AVFilterLink *inlink = outlink->src->inputs[0]; ScaleContext *scale = ctx->priv; int64_t w, h; + + scale->const_values[POV_W] = inlink->w; + scale->const_values[POV_H] = inlink->h; +
+ scale->w = av_eval_expr(scale->wexpr, scale->const_values, scale); + scale->h = av_eval_expr(scale->hexpr, scale->const_values, scale);
Maybe we should add a check here: !is_nan(av_eval_expr(scale->w_expr, ...))
if (!(w = scale->w)) w = inlink->w;
This can be removed once you assign the default expression. Thanks, regards, -- FFmpeg = Fierce & Freak Meaningless Powered Embarassing God
On Wed, Jul 07, 2010 at 11:02:01PM +0200, Stefano Sabatini wrote:
On date Wednesday 2010-07-07 15:53:16 -0400, Daniel G. Taylor encoded:
On 07/01/2010 10:27 AM, Stefano Sabatini wrote:
Basically, I don't know how I can do this easily, and writing a bunch of code doing it a stupid way seems like a waste of time, so any advice would be appreciated if we do want to go this way.
libavutil/eval.h, check how it is done in the libavfilter-soc overlay and setpts filters. This requires to add eval evalutation to the scale filter, check the recent patch by Baptiste on which I commented yesterday.
Thanks for the help here Stefano.
I've run into a bit of a snag I'd say... See attached patch to reproduce what I'm running into, but basically:
* av_parse_expr and such use a comma as a delimiter for function arguments * avfilter's graph parsing uses a comma as a delimiter for filters
Because of that it means anytime you put a comma into the filter arguments it thinks you are passing in the next filter. If we are going to allow functions and such as filter arguments it might be a good idea to change the syntax for specifying filters, maybe using a pipe character like "|" or something, e.g. -vf "scale|unsharp|...|crop".
Attached patch works for stuff like -vf "scale=320:240" or -vf "scale=W/2:H/2" and such but if you try -vf "scale=W:round(H, 16)" you'll immediately see the issue.
Thoughts?
Possible solutions:
1) change the character currently used, which is ",", and which is very likely to appear in expressions inside the filter args.
'|' is an option, note that we didn't choose '|' in the first place because it's a special sh character, for example this doesn't work in sh: -vf scale|unsharp|crop
you need: -vf "scale|unsharp|crop"
For me this doesn't look like a big issue, but we should wart the users in the docs.
The requirement is that the choosen character cannot compare inside the filter args.
2) make the separator definable by the user, for example we could have something like: -vfd "%" -vf "scale % unsharp % ... % crop".
3) Implement an escaping mechanism. Since at the first level of escaping there is just one special character, we may use '\' to escape the ',', for example:
scale="W:round(H\, 16), unsharp, crop"
The problem with this approach is that combined with the shell escaping mechanism this can easily became an escaping hell.
So I tend to prefer the option 2, and use by default the value "|" for the separator.
how do you pass a string to a filter? you need escaping anyway a eval expression is just a string ... [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB It is not what we do, but why we do it that matters.
On date Wednesday 2010-07-07 23:14:42 +0200, Michael Niedermayer encoded:
On Wed, Jul 07, 2010 at 11:02:01PM +0200, Stefano Sabatini wrote:
On date Wednesday 2010-07-07 15:53:16 -0400, Daniel G. Taylor encoded:
On 07/01/2010 10:27 AM, Stefano Sabatini wrote:
Basically, I don't know how I can do this easily, and writing a bunch of code doing it a stupid way seems like a waste of time, so any advice would be appreciated if we do want to go this way.
libavutil/eval.h, check how it is done in the libavfilter-soc overlay and setpts filters. This requires to add eval evalutation to the scale filter, check the recent patch by Baptiste on which I commented yesterday.
Thanks for the help here Stefano.
I've run into a bit of a snag I'd say... See attached patch to reproduce what I'm running into, but basically:
* av_parse_expr and such use a comma as a delimiter for function arguments * avfilter's graph parsing uses a comma as a delimiter for filters
Because of that it means anytime you put a comma into the filter arguments it thinks you are passing in the next filter. If we are going to allow functions and such as filter arguments it might be a good idea to change the syntax for specifying filters, maybe using a pipe character like "|" or something, e.g. -vf "scale|unsharp|...|crop".
Attached patch works for stuff like -vf "scale=320:240" or -vf "scale=W/2:H/2" and such but if you try -vf "scale=W:round(H, 16)" you'll immediately see the issue.
Thoughts?
Possible solutions:
1) change the character currently used, which is ",", and which is very likely to appear in expressions inside the filter args.
'|' is an option, note that we didn't choose '|' in the first place because it's a special sh character, for example this doesn't work in sh: -vf scale|unsharp|crop
you need: -vf "scale|unsharp|crop"
For me this doesn't look like a big issue, but we should wart the users in the docs.
The requirement is that the choosen character cannot compare inside the filter args.
2) make the separator definable by the user, for example we could have something like: -vfd "%" -vf "scale % unsharp % ... % crop".
3) Implement an escaping mechanism. Since at the first level of escaping there is just one special character, we may use '\' to escape the ',', for example:
scale="W:round(H\, 16), unsharp, crop"
The problem with this approach is that combined with the shell escaping mechanism this can easily became an escaping hell.
So I tend to prefer the option 2, and use by default the value "|" for the separator.
how do you pass a string to a filter? you need escaping anyway a eval expression is just a string ...
Ehm, after further inspection of the code I discovered that the graph parser *already* supports escaping (through the av_get_token() function). So for the particular case above: "scale=W:round(H\, 16)" or even: 'scale=W:round(H, 16)' Regards. -- FFmpeg = Fast Fundamental Most Puritan Epic Ghost
On 07/21/2010 08:04 PM, Stefano Sabatini wrote:
Ehm, after further inspection of the code I discovered that the graph parser *already* supports escaping (through the av_get_token() function).
So for the particular case above: "scale=W:round(H\, 16)"
or even: 'scale=W:round(H, 16)'
Okay, awesome. I've confirmed that works, along with stuff like: ffmpeg -i in.mp4 -vf "scale=PI*100:round(PI*100/A\, 16)" out.mp4 I've also fixed all the comments from the other mail, and as you can see above added PI and E. Updated patch is attached. There are now two issues I'm a little stuck with and would like your thoughts on: * With zero arguments it crashes. It appears 'flags=0x...' is magically appended to args and I have no idea where and what is going on. Can one of you C string parsing gurus take a look at the best way to parse the args here? * The special case of -1 can't work within the expression parser, so if you want to round e.g. scale=320:-1 you now would have to do "scale=320:round(320/H\, 16)". Any way to fix this or is it okay this way? Take care, -- Daniel G. Taylor http://programmer-art.org/
On date Thursday 2010-07-22 11:33:10 -0400, Daniel G. Taylor encoded:
On 07/21/2010 08:04 PM, Stefano Sabatini wrote:
Ehm, after further inspection of the code I discovered that the graph parser *already* supports escaping (through the av_get_token() function).
So for the particular case above: "scale=W:round(H\, 16)"
or even: 'scale=W:round(H, 16)'
Okay, awesome. I've confirmed that works, along with stuff like:
ffmpeg -i in.mp4 -vf "scale=PI*100:round(PI*100/A\, 16)" out.mp4
I've also fixed all the comments from the other mail, and as you can see above added PI and E. Updated patch is attached. There are now two issues I'm a little stuck with and would like your thoughts on:
* With zero arguments it crashes. It appears 'flags=0x...' is magically appended to args and I have no idea where and what is going on. Can one of you C string parsing gurus take a look at the best way to parse the args here?
This is automatically added by ffmpeg or the graphparser, that's an hack we use to propagate the scaling flags to use. I suppose we may fix that *requiring* a syntax of the kind: W:H:FLAGS rather than try to intercept the flags= string. Anyway I'd rather expect the filter to issue a configuration error, further debugging information is required.
* The special case of -1 can't work within the expression parser, so if you want to round e.g. scale=320:-1 you now would have to do "scale=320:round(320/H\, 16)". Any way to fix this or is it okay this way?
We may keep the interpretation of -1 as a special value, but also using W2/H1 looks fine to me (and imho less surprising for a new user).
Take care, -- Daniel G. Taylor http://programmer-art.org/
Index: libavfilter/vf_scale.c =================================================================== --- libavfilter/vf_scale.c (revision 24077) +++ libavfilter/vf_scale.c (working copy) @@ -1,5 +1,6 @@ /* * copyright (c) 2007 Bobby Bingham + * copyright (c) 2010 Daniel G. Taylor <dan at programmer-art.org> * * This file is part of FFmpeg. * @@ -24,44 +25,91 @@ */
#include "avfilter.h" +#include "libavutil/eval.h" #include "libavutil/pixdesc.h" #include "libswscale/swscale.h"
+static const char *var_names[]={ + "PI", + "E", + "W", + "H", + "A", + NULL +}; + +enum var_name { + PI, + E, + W, + H, + A, + VARS_NB +};
alphabetical order, avoid potential bugs.
+ +static inline double round_clip(void *ctx, double arg1, double arg2) +{ + int orig = (int)arg1; + int clip = (int)arg2; + + return orig -= (orig % clip < clip / 2) ? orig % clip : -(clip - (orig % clip)); +} +
+static const char *func2_names[]={
nit: _=_{
+ "round", + NULL +}; +
+static double (* const *funcs2[])(void *, double, double)={ + (void *)round_clip, + NULL +}; + typedef struct { struct SwsContext *sws; ///< software scaler context
- /** - * New dimensions. Special values are: - * 0 = original width/height - * -1 = keep original aspect - */ - int w, h; - unsigned int flags; ///sws flags + int w, h; ///< new dimensions + unsigned int flags; ///< sws flags
int hsub, vsub; ///< chroma subsampling int slice_y; ///< top of current output slice int input_is_pal; ///< set to 1 if the input format is paletted + + AVExpr *w_expr; ///< width expression + AVExpr *h_expr; ///< height expression + double var_values[VARS_NB+1]; ///< expression parsing constants } ScaleContext;
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) { ScaleContext *scale = ctx->priv; const char *p; + char width[255] = "W", height[255] = "H"; + int ret; + char *save_ptr = NULL;
scale->flags = SWS_BILINEAR;
if (args){ - sscanf(args, "%d:%d", &scale->w, &scale->h); + sscanf(args, "%255[^:]:%255[^:]", &width, &height); p= strstr(args,"flags="); if(p) scale->flags= strtoul(p+6, NULL, 0); }
this fails if you have the string: flags=.. appended to args. I believe the best solution would be to change the scale syntax to W:H:FLAGS, thoughts?
- - /* sanity check params */ - if (scale->w < -1 || scale->h < -1) { - av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n"); - return -1; +
+ ret = av_parse_expr(&scale->w_expr, width, var_names, NULL, NULL, + func2_names, funcs2, 0, ctx); + if (ret < 0) { + av_log(ctx, AV_LOG_ERROR, + "Error while parsing width expression '%s'\n", width); + return ret; } - if (scale->w == -1 && scale->h == -1) - scale->w = scale->h = 0; + + ret = av_parse_expr(&scale->h_expr, height, var_names, NULL, NULL, + func2_names, funcs2, 0, ctx); + if (ret < 0) { + av_log(ctx, AV_LOG_ERROR, + "Error while parsing height expression '%s'\n", height); + return ret; + }
code duplication, look at how it's done in the libavfilter soc overlay filter to avoid it.
return 0; } @@ -109,11 +157,19 @@ AVFilterLink *inlink = outlink->src->inputs[0]; ScaleContext *scale = ctx->priv; int64_t w, h;
+ + scale->var_values[PI] = M_PI; + scale->var_values[E] = M_E; + scale->var_values[W] = inlink->w; + scale->var_values[H] = inlink->h; + scale->var_values[A] = (float) inlink->w / inlink->h;
vertical align.
+ + scale->w = av_eval_expr(scale->w_expr, scale->var_values, scale); + scale->h = av_eval_expr(scale->h_expr, scale->var_values, scale);
BTW in order to avoid to parse and eval as two separate operations as you need to evaluate the expression just once, you may just use here av_parse_and_eval_expr(), that may simplify the code a bit.
- if (!(w = scale->w)) - w = inlink->w; - if (!(h = scale->h)) - h = inlink->h; + w = scale->w; + h = scale->h; + if (w == -1) w = av_rescale(h, inlink->w, inlink->h); if (h == -1)
Regards. -- FFmpeg = Furious and Fundamental Multipurpose Programmable Elaborated Gigant
On Thu, Jul 22, 2010 at 11:33:10AM -0400, Daniel G. Taylor wrote: [...]
+static inline double round_clip(void *ctx, double arg1, double arg2) +{ + int orig = (int)arg1; + int clip = (int)arg2; + + return orig -= (orig % clip < clip / 2) ? orig % clip : -(clip - (orig % clip)); +}
trailing whitespace and missing documentation [...]
@@ -109,11 +157,19 @@ AVFilterLink *inlink = outlink->src->inputs[0]; ScaleContext *scale = ctx->priv; int64_t w, h; + + scale->var_values[PI] = M_PI; + scale->var_values[E] = M_E; + scale->var_values[W] = inlink->w; + scale->var_values[H] = inlink->h; + scale->var_values[A] = (float) inlink->w / inlink->h; + + scale->w = av_eval_expr(scale->w_expr, scale->var_values, scale); + scale->h = av_eval_expr(scale->h_expr, scale->var_values, scale);
the scale->w and scale->h values should be available to teh expressions so that each can refer to the other [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB it is not once nor twice but times without number that the same ideas make their appearance in the world. -- Aristotle
participants (4)
-
baptiste.coudurier@gmail.com -
dan@programmer-art.org -
michaelni@gmx.at -
stefano.sabatini-lala@poste.it