[FFmpeg-devel] [PATCH 1/5] lavfi: add internal functions for parsing format arguments

Stefano Sabatini stefano.sabatini-lala at poste.it
Mon Aug 8 11:36:49 CEST 2011


On date Monday 2011-08-08 11:11:45 +0300, Mina Nagy Zaki encoded:
> ---
>  libavfilter/formats.c  |   55 ++++++++++++++++++++++++++++++++++++++++++++++++
>  libavfilter/internal.h |   38 +++++++++++++++++++++++++++++++++
>  2 files changed, 93 insertions(+), 0 deletions(-)
> 
> diff --git a/libavfilter/formats.c b/libavfilter/formats.c
> index 214718b..53d24c0 100644
> --- a/libavfilter/formats.c
> +++ b/libavfilter/formats.c
> @@ -19,6 +19,7 @@
>   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
>   */
>  
> +#include "libavutil/eval.h"
>  #include "libavutil/pixdesc.h"
>  #include "libavutil/audioconvert.h"
>  #include "avfilter.h"
> @@ -233,3 +234,57 @@ void avfilter_formats_changeref(AVFilterFormats **oldref,
>      }
>  }
>  
> +/* internal functions for parsing audio format arguments */
> +
> +int ff_parse_sample_format(char *arg, void *log_ctx)
> +{
> +    char *tail;
> +    int sample_fmt = av_get_sample_fmt(arg);
> +    if (sample_fmt == AV_SAMPLE_FMT_NONE) {
> +        sample_fmt = strtol(arg, &tail, 0);
> +        if (*tail || (unsigned)sample_fmt >= AV_SAMPLE_FMT_NB) {
> +            av_log(log_ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
> +            return -1;
> +        }
> +    }
> +    return sample_fmt;
> +}
> +
> +int ff_parse_sample_rate(char *arg, void *log_ctx)
> +{
> +    char *tail;
> +    double sample_rate = av_strtod(arg, &tail);
> +    if (*tail || sample_rate < 1 || (int)sample_rate != sample_rate) {
> +        av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
> +        return -1;
> +    }
> +    return sample_rate;
> +}
> +
> +int64_t ff_parse_channel_layout(char *arg, void *log_ctx)
> +{
> +    char *tail;
> +    int64_t chlayout = av_get_channel_layout(arg);
> +    if (chlayout <= 0) {
> +        chlayout = strtol(arg, &tail, 0);
> +        if (*tail || chlayout <= 0) {
> +            av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
> +            return -1;
> +        }
> +    }
> +    return chlayout;
> +}
> +
> +int ff_parse_packing_format(char *arg, void *log_ctx)
> +{
> +    char *tail;
> +    int planar = strtol(arg, &tail, 10);
> +    if (*tail) {
> +        planar = (strcmp(arg, "packed") != 0);
> +    } else if (planar != 0 && planar != 1) {
> +        av_log(log_ctx, AV_LOG_ERROR, "Invalid packing format '%s'\n", arg);
> +        return -1;
> +    }
> +    return planar;
> +}

s/return -1/return AVERROR(EINVAL)/

> +
> diff --git a/libavfilter/internal.h b/libavfilter/internal.h
> index 7537565..b0a252c 100644
> --- a/libavfilter/internal.h
> +++ b/libavfilter/internal.h
> @@ -61,4 +61,42 @@ void ff_avfilter_default_free_buffer(AVFilterBuffer *buf);
>  /** Tell is a format is contained in the provided list terminated by -1. */
>  int ff_fmt_is_in(int fmt, const int *fmts);
>  
> +/* Functions to parse audio format arguments */
> +
> +/**
> + * Parse a sample rate.
> + *
> + * @param arg string to parse
> + * @param log_ctx log ctx

> + * @return sample rate or -1 on error

@return sample rate or a negative AVERROR code in case of error

more general, and you can reuse the issued error code

[...]

Looks fine otherwise.
-- 
FFmpeg = Fantastic Fostering Mythic Patchable Elastic Game


More information about the ffmpeg-devel mailing list