[PATCH 1/2] lavu/opt: add AV_OPT_TYPE_DURATION.
Signed-off-by: Nicolas George <nicolas.george@normalesup.org> --- doc/APIchanges | 3 +++ libavutil/opt.c | 30 +++++++++++++++++++++++++++++- libavutil/opt.h | 1 + libavutil/version.h | 2 +- 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/doc/APIchanges b/doc/APIchanges index 2bde666..3f33e03 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -15,6 +15,9 @@ libavutil: 2012-10-22 API changes, most recent first: +2013-03-16 - xxxxxxx - lavu 52.20.100 - opt.h + Add AV_OPT_TYPE_DURATION value to AVOptionType enum. + 2013-03-07 - xxxxxx - lavu 52.18.100 - avstring.h,bprint.h Add av_escape() and av_bprint_escape() API. diff --git a/libavutil/opt.c b/libavutil/opt.c index f91d18b..b4c2dc7 100644 --- a/libavutil/opt.c +++ b/libavutil/opt.c @@ -77,6 +77,7 @@ static int read_number(const AVOption *o, void *dst, double *num, int *den, int6 case AV_OPT_TYPE_PIXEL_FMT: case AV_OPT_TYPE_SAMPLE_FMT: case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0; + case AV_OPT_TYPE_DURATION: case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0; case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0; case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0; @@ -101,6 +102,7 @@ static int write_number(void *obj, const AVOption *o, void *dst, double num, int case AV_OPT_TYPE_PIXEL_FMT: case AV_OPT_TYPE_SAMPLE_FMT: case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break; + case AV_OPT_TYPE_DURATION: case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break; case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break; case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break; @@ -256,7 +258,7 @@ int av_opt_set(void *obj, const char *name, const char *val, int search_flags) return AVERROR_OPTION_NOT_FOUND; if (!val && (o->type != AV_OPT_TYPE_STRING && o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT && - o->type != AV_OPT_TYPE_IMAGE_SIZE)) + o->type != AV_OPT_TYPE_IMAGE_SIZE && o->type != AV_OPT_TYPE_DURATION)) return AVERROR(EINVAL); dst = ((uint8_t*)target_obj) + o->offset; @@ -310,6 +312,15 @@ int av_opt_set(void *obj, const char *name, const char *val, int search_flags) } *(enum AVSampleFormat *)dst = ret; return 0; + case AV_OPT_TYPE_DURATION: + if (!val) { + *(int64_t *)dst = 0; + return 0; + } else { + if ((ret = av_parse_time(dst, val, 1)) < 0) + av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val); + return ret; + } } av_log(obj, AV_LOG_ERROR, "Invalid option type.\n"); @@ -530,6 +541,7 @@ int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val) const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj); uint8_t *bin, buf[128]; int len, i, ret; + int64_t i64; if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST)) return AVERROR_OPTION_NOT_FOUND; @@ -570,6 +582,12 @@ int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val) case AV_OPT_TYPE_SAMPLE_FMT: ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none")); break; + case AV_OPT_TYPE_DURATION: + i64 = *(int64_t *)dst; + ret = snprintf(buf, sizeof(buf), "%"PRIi64"d:%02d:%02d.%06d", + i64 / 3600000000, (int)((i64 / 60000000) % 60), + (int)((i64 / 1000000) % 60), (int)(i64 % 1000000)); + break; default: return AVERROR(EINVAL); } @@ -815,6 +833,9 @@ static void opt_list(void *obj, void *av_log_obj, const char *unit, case AV_OPT_TYPE_SAMPLE_FMT: av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>"); break; + case AV_OPT_TYPE_DURATION: + av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>"); + break; case AV_OPT_TYPE_CONST: default: av_log(av_log_obj, AV_LOG_INFO, "%-12s ", ""); @@ -891,6 +912,7 @@ void av_opt_set_defaults2(void *s, int mask, int flags) case AV_OPT_TYPE_FLAGS: case AV_OPT_TYPE_INT: case AV_OPT_TYPE_INT64: + case AV_OPT_TYPE_DURATION: av_opt_set_int(s, opt->name, opt->default_val.i64, 0); break; case AV_OPT_TYPE_DOUBLE: @@ -1253,6 +1275,7 @@ int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const ch case AV_OPT_TYPE_SAMPLE_FMT: case AV_OPT_TYPE_FLOAT: case AV_OPT_TYPE_DOUBLE: + case AV_OPT_TYPE_DURATION: break; case AV_OPT_TYPE_STRING: range->component_min = 0; @@ -1311,6 +1334,7 @@ typedef struct TestContext int w, h; enum AVPixelFormat pix_fmt; enum AVSampleFormat sample_fmt; + int64_t duration; } TestContext; #define OFFSET(x) offsetof(TestContext, x) @@ -1331,6 +1355,7 @@ static const AVOption test_options[]= { {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,{0}, 0, 0 }, {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, AV_PIX_FMT_NB-1}, {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, {.i64 = AV_SAMPLE_FMT_NONE}, -1, AV_SAMPLE_FMT_NB-1}, +{"duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX}, {NULL}, }; @@ -1381,6 +1406,9 @@ int main(void) "sample_fmt=s16", "sample_fmt=2", "sample_fmt=bogus", + "duration=bogus", + "duration=123.45", + "duration=1\\:23\\:45.67", }; test_ctx.class = &test_class; diff --git a/libavutil/opt.h b/libavutil/opt.h index baf1b82..1c86e6f 100644 --- a/libavutil/opt.h +++ b/libavutil/opt.h @@ -230,6 +230,7 @@ enum AVOptionType{ AV_OPT_TYPE_IMAGE_SIZE = MKBETAG('S','I','Z','E'), ///< offset must point to two consecutive integers AV_OPT_TYPE_PIXEL_FMT = MKBETAG('P','F','M','T'), AV_OPT_TYPE_SAMPLE_FMT = MKBETAG('S','F','M','T'), + AV_OPT_TYPE_DURATION = MKBETAG('D','U','R',' '), #if FF_API_OLD_AVOPTIONS FF_OPT_TYPE_FLAGS = 0, FF_OPT_TYPE_INT, diff --git a/libavutil/version.h b/libavutil/version.h index d95a8a2..1ff9ffe 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -75,7 +75,7 @@ */ #define LIBAVUTIL_VERSION_MAJOR 52 -#define LIBAVUTIL_VERSION_MINOR 19 +#define LIBAVUTIL_VERSION_MINOR 20 #define LIBAVUTIL_VERSION_MICRO 100 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ -- 1.7.10.4
Signed-off-by: Nicolas George <nicolas.george@normalesup.org> --- Changelog | 1 + doc/filters.texi | 51 +++++++++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/asrc_sine.c | 228 ++++++++++++++++++++++++++++++++++++++++++++++ libavfilter/version.h | 2 +- 6 files changed, 283 insertions(+), 1 deletion(-) create mode 100644 libavfilter/asrc_sine.c diff --git a/Changelog b/Changelog index 516b293..958634b 100644 --- a/Changelog +++ b/Changelog @@ -8,6 +8,7 @@ version <next>: or vice versa - support for Monkey's Audio versions from 3.93 - perms and aperms filters +- sine audio filter source version 1.2: diff --git a/doc/filters.texi b/doc/filters.texi index 0dbec8e..de16a2f 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -1653,6 +1653,57 @@ ffplay -f lavfi flite=text='No more be grieved for which that thou hast done.' For more information about libflite, check: @url{http://www.speech.cs.cmu.edu/flite/} +@section sine + +Generate an audio signal made of a sine wave with amplitude 1/8. + +The audio signal is bit-exact. + +It accepts a list of options in the form of @var{key}=@var{value} pairs +separated by ":". If the option name is omitted, the first option is the +frequency and the second option is the beep factor. + +The supported options are: + +@table @option + +@item frequency, f +Set the carrier frequency. Default is 440 Hz. + +@item beep_factor, b +Enable a periodic beep every second with frequency @var{beep_factor} times +the carrier frequency. Default is 0, meaning the beep is disabled. + +@item sample_rate, s +Specify the sample rate, default is 44100. + +@item duration, d +Specify the duration. + +@item samples_per_frame +Set the number of samples per output frame, default is 1024. +@end table + +@subsection Examples + +@itemize + +@item +Generate a simple 440 Hz sine wave: +@example +sine +@end example + +@item +Generate a 220 Hz sine wave with a 880 Hz beep each second, for 5 seconds: +@example +sine=220:4:d=5 +sine=f=220:b=4:d=5 +sine=frequency=220:beep_factor=4:duration=5 +@end example + +@end itemize + @c man end AUDIO SOURCES @chapter Audio Sinks diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 2758c8e..b73569e 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -90,6 +90,7 @@ OBJS-$(CONFIG_VOLUMEDETECT_FILTER) += af_volumedetect.o OBJS-$(CONFIG_AEVALSRC_FILTER) += asrc_aevalsrc.o OBJS-$(CONFIG_ANULLSRC_FILTER) += asrc_anullsrc.o OBJS-$(CONFIG_FLITE_FILTER) += asrc_flite.o +OBJS-$(CONFIG_SINE_FILTER) += asrc_sine.o OBJS-$(CONFIG_ANULLSINK_FILTER) += asink_anullsink.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 086e6c9..45a67e5 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -86,6 +86,7 @@ void avfilter_register_all(void) REGISTER_FILTER(AEVALSRC, aevalsrc, asrc); REGISTER_FILTER(ANULLSRC, anullsrc, asrc); REGISTER_FILTER(FLITE, flite, asrc); + REGISTER_FILTER(SINE, sine, asrc); REGISTER_FILTER(ANULLSINK, anullsink, asink); diff --git a/libavfilter/asrc_sine.c b/libavfilter/asrc_sine.c new file mode 100644 index 0000000..f2dcae9 --- /dev/null +++ b/libavfilter/asrc_sine.c @@ -0,0 +1,228 @@ +/* + * Copyright (c) 2013 Nicolas George + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with FFmpeg; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/avassert.h" +#include "libavutil/channel_layout.h" +#include "libavutil/opt.h" +#include "audio.h" +#include "avfilter.h" +#include "internal.h" + +typedef struct { + const AVClass *class; + double frequency; + double beep_factor; + int samples_per_frame; + int sample_rate; + int64_t duration; + int16_t *sin; + int64_t pts; + uint32_t phi; + uint32_t dphi; + unsigned beep_period; + unsigned beep_index; + unsigned beep_length; + uint32_t phi_beep; + uint32_t dphi_beep; +} SineContext; + +#define CONTEXT SineContext +#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM + +#define OPT_GENERIC(name, field, def, min, max, descr, type, deffield, ...) \ + { name, descr, offsetof(CONTEXT, field), AV_OPT_TYPE_ ## type, \ + { .deffield = def }, min, max, FLAGS, __VA_ARGS__ } + +#define OPT_INT(name, field, def, min, max, descr, ...) \ + OPT_GENERIC(name, field, def, min, max, descr, INT, i64, __VA_ARGS__) + +#define OPT_DBL(name, field, def, min, max, descr, ...) \ + OPT_GENERIC(name, field, def, min, max, descr, DOUBLE, dbl, __VA_ARGS__) + +#define OPT_DUR(name, field, def, min, max, descr, ...) \ + OPT_GENERIC(name, field, def, min, max, descr, DURATION, str, __VA_ARGS__) + +static const AVOption sine_options[]= { + OPT_DBL("frequency", frequency, 440, 0, INFINITY, "set the sine frequency"), + OPT_DBL("f", frequency, 440, 0, INFINITY, "set the sine frequency"), + OPT_DBL("beep_factor", beep_factor, 0, 0, INFINITY, "set the beep fequency factor"), + OPT_DBL("b", beep_factor, 0, 0, INFINITY, "set the beep fequency factor"), + OPT_INT("sample_rate", sample_rate, 44100, 1, INT_MAX, "set the sample rate"), + OPT_INT("r", sample_rate, 44100, 1, INT_MAX, "set the sample rate"), + OPT_DUR("duration", duration, 0, 0, INT64_MAX, "set the audio duration"), + OPT_DUR("d", duration, 0, 0, INT64_MAX, "set the audio duration"), + OPT_INT("samples_per_frame", samples_per_frame, 1024, 0, INT_MAX, "set the number of samples per frame"), + {NULL}, +}; + +AVFILTER_DEFINE_CLASS(sine); + +#define LOG_PERIOD 15 +#define AMPLITUDE 4095 +#define AMPLITUDE_SHIFT 3 + +static void make_sin_table(int16_t *sin) +{ + unsigned half_pi = 1 << (LOG_PERIOD - 2); + unsigned ampls = AMPLITUDE << AMPLITUDE_SHIFT; + uint64_t unit2 = (uint64_t)(ampls * ampls) << 32; + unsigned step, i, c, s, k, new_k, n2; + + /* Principle: if u = exp(i*a1) and v = exp(i*a2), then + exp(i*(a1+a2)/2) = (u+v) / length(u+v) */ + sin[0] = 0; + sin[half_pi] = ampls; + for (step = half_pi; step > 1; step /= 2) { + /* k = (1 << 16) * amplitude / length(u+v) + In exact values, k is constant at a given step */ + k = 0x10000; + for (i = 0; i < half_pi / 2; i += step) { + s = sin[i] + sin[i + step]; + c = sin[half_pi - i] + sin[half_pi - i - step]; + n2 = s * s + c * c; + /* Newton's method to solve n² * k² = unit² */ + while (1) { + new_k = (k + unit2 / ((uint64_t)k * n2) + 1) >> 1; + if (k == new_k) + break; + k = new_k; + } + sin[i + step / 2] = (k * s + 0x7FFF) >> 16; + sin[half_pi - i - step / 2] = (k * c + 0x8000) >> 16; + } + } + /* Unshift amplitude */ + for (i = 0; i <= half_pi; i++) + sin[i] = (sin[i] + (1 << (AMPLITUDE_SHIFT - 1))) >> AMPLITUDE_SHIFT; + /* Use symmetries to fill the other three quarters */ + for (i = 0; i < half_pi; i++) + sin[half_pi * 2 - i] = sin[i]; + for (i = 0; i < 2 * half_pi; i++) + sin[i + 2 * half_pi] = -sin[i]; +} + +static av_cold int init(AVFilterContext *ctx, const char *args) +{ + SineContext *sine = ctx->priv; + static const char *shorthand[] = { "frequency", "beep_factor", NULL }; + int ret; + + sine->class = &sine_class; + av_opt_set_defaults(sine); + + if ((ret = av_opt_set_from_string(sine, args, shorthand, "=", ":")) < 0) + return ret; + if (!(sine->sin = av_malloc(sizeof(*sine->sin) << LOG_PERIOD))) + return AVERROR(ENOMEM); + sine->dphi = ldexp(sine->frequency, 32) / sine->sample_rate + 0.5; + make_sin_table(sine->sin); + + if (sine->beep_factor) { + sine->beep_period = sine->sample_rate; + sine->beep_length = sine->beep_period / 25; + sine->dphi_beep = ldexp(sine->beep_factor * sine->frequency, 32) / + sine->sample_rate + 0.5; + } + + return 0; +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + SineContext *sine = ctx->priv; + + av_freep(&sine->sin); +} + +static av_cold int query_formats(AVFilterContext *ctx) +{ + SineContext *sine = ctx->priv; + static const int64_t chlayouts[] = { AV_CH_LAYOUT_MONO, -1 }; + int sample_rates[] = { sine->sample_rate, -1 }; + static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, + AV_SAMPLE_FMT_NONE }; + + ff_set_common_formats (ctx, ff_make_format_list(sample_fmts)); + ff_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts)); + ff_set_common_samplerates(ctx, ff_make_format_list(sample_rates)); + return 0; +} + +static av_cold int config_props(AVFilterLink *outlink) +{ + SineContext *sine = outlink->src->priv; + sine->duration = av_rescale(sine->duration, sine->sample_rate, AV_TIME_BASE); + return 0; +} + +static int request_frame(AVFilterLink *outlink) +{ + SineContext *sine = outlink->src->priv; + AVFrame *frame; + int i, nb_samples = sine->samples_per_frame; + int16_t *samples; + + if (sine->duration) { + nb_samples = FFMIN(nb_samples, sine->duration - sine->pts); + av_assert1(nb_samples >= 0); + if (!nb_samples) + return AVERROR_EOF; + } + if (!(frame = ff_get_audio_buffer(outlink, nb_samples))) + return AVERROR(ENOMEM); + samples = (int16_t *)frame->data[0]; + + for (i = 0; i < nb_samples; i++) { + samples[i] = sine->sin[sine->phi >> (32 - LOG_PERIOD)]; + sine->phi += sine->dphi; + if (sine->beep_index < sine->beep_length) { + samples[i] += sine->sin[sine->phi_beep >> (32 - LOG_PERIOD)] << 1; + sine->phi_beep += sine->dphi_beep; + } + if (++sine->beep_index == sine->beep_period) + sine->beep_index = 0; + } + + frame->pts = sine->pts; + sine->pts += nb_samples; + return ff_filter_frame(outlink, frame); +} + +static const AVFilterPad sine_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .request_frame = request_frame, + .config_props = config_props, + }, + { NULL } +}; + +AVFilter avfilter_asrc_sine = { + .name = "sine", + .description = NULL_IF_CONFIG_SMALL("Generate sine wave audio signal."), + .query_formats = query_formats, + .init = init, + .uninit = uninit, + .priv_size = sizeof(SineContext), + .inputs = NULL, + .outputs = sine_outputs, + .priv_class = &sine_class, +}; diff --git a/libavfilter/version.h b/libavfilter/version.h index 3ec6f2d..f592fc1 100644 --- a/libavfilter/version.h +++ b/libavfilter/version.h @@ -29,7 +29,7 @@ #include "libavutil/avutil.h" #define LIBAVFILTER_VERSION_MAJOR 3 -#define LIBAVFILTER_VERSION_MINOR 47 +#define LIBAVFILTER_VERSION_MINOR 48 #define LIBAVFILTER_VERSION_MICRO 100 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ -- 1.7.10.4
On date Saturday 2013-03-16 16:44:06 +0100, Nicolas George encoded:
Signed-off-by: Nicolas George <nicolas.george@normalesup.org> --- Changelog | 1 + doc/filters.texi | 51 +++++++++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/asrc_sine.c | 228 ++++++++++++++++++++++++++++++++++++++++++++++ libavfilter/version.h | 2 +- 6 files changed, 283 insertions(+), 1 deletion(-) create mode 100644 libavfilter/asrc_sine.c
diff --git a/Changelog b/Changelog index 516b293..958634b 100644 --- a/Changelog +++ b/Changelog @@ -8,6 +8,7 @@ version <next>: or vice versa - support for Monkey's Audio versions from 3.93 - perms and aperms filters +- sine audio filter source
version 1.2: diff --git a/doc/filters.texi b/doc/filters.texi index 0dbec8e..de16a2f 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -1653,6 +1653,57 @@ ffplay -f lavfi flite=text='No more be grieved for which that thou hast done.' For more information about libflite, check: @url{http://www.speech.cs.cmu.edu/flite/}
+@section sine + +Generate an audio signal made of a sine wave with amplitude 1/8. + +The audio signal is bit-exact. + +It accepts a list of options in the form of @var{key}=@var{value} pairs +separated by ":". If the option name is omitted, the first option is the +frequency and the second option is the beep factor. + +The supported options are: + +@table @option + +@item frequency, f +Set the carrier frequency. Default is 440 Hz. + +@item beep_factor, b +Enable a periodic beep every second with frequency @var{beep_factor} times +the carrier frequency. Default is 0, meaning the beep is disabled. + +@item sample_rate, s +Specify the sample rate, default is 44100. + +@item duration, d +Specify the duration.
... of the generated audio stream.
+ +@item samples_per_frame +Set the number of samples per output frame, default is 1024. +@end table + +@subsection Examples + +@itemize + +@item +Generate a simple 440 Hz sine wave: +@example +sine +@end example + +@item +Generate a 220 Hz sine wave with a 880 Hz beep each second, for 5 seconds: +@example +sine=220:4:d=5 +sine=f=220:b=4:d=5 +sine=frequency=220:beep_factor=4:duration=5 +@end example + +@end itemize + @c man end AUDIO SOURCES
@chapter Audio Sinks diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 2758c8e..b73569e 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -90,6 +90,7 @@ OBJS-$(CONFIG_VOLUMEDETECT_FILTER) += af_volumedetect.o OBJS-$(CONFIG_AEVALSRC_FILTER) += asrc_aevalsrc.o OBJS-$(CONFIG_ANULLSRC_FILTER) += asrc_anullsrc.o OBJS-$(CONFIG_FLITE_FILTER) += asrc_flite.o +OBJS-$(CONFIG_SINE_FILTER) += asrc_sine.o
OBJS-$(CONFIG_ANULLSINK_FILTER) += asink_anullsink.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 086e6c9..45a67e5 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -86,6 +86,7 @@ void avfilter_register_all(void) REGISTER_FILTER(AEVALSRC, aevalsrc, asrc); REGISTER_FILTER(ANULLSRC, anullsrc, asrc); REGISTER_FILTER(FLITE, flite, asrc); + REGISTER_FILTER(SINE, sine, asrc);
REGISTER_FILTER(ANULLSINK, anullsink, asink);
diff --git a/libavfilter/asrc_sine.c b/libavfilter/asrc_sine.c new file mode 100644 index 0000000..f2dcae9 --- /dev/null +++ b/libavfilter/asrc_sine.c @@ -0,0 +1,228 @@ +/* + * Copyright (c) 2013 Nicolas George + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with FFmpeg; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/avassert.h" +#include "libavutil/channel_layout.h" +#include "libavutil/opt.h" +#include "audio.h" +#include "avfilter.h" +#include "internal.h" + +typedef struct { + const AVClass *class; + double frequency; + double beep_factor; + int samples_per_frame; + int sample_rate; + int64_t duration; + int16_t *sin; + int64_t pts;
+ uint32_t phi; + uint32_t dphi;
doxy?
+ unsigned beep_period; + unsigned beep_index; + unsigned beep_length;
+ uint32_t phi_beep; + uint32_t dphi_beep;
doxy?
+} SineContext; + +#define CONTEXT SineContext +#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM +
+#define OPT_GENERIC(name, field, def, min, max, descr, type, deffield, ...) \ + { name, descr, offsetof(CONTEXT, field), AV_OPT_TYPE_ ## type, \ + { .deffield = def }, min, max, FLAGS, __VA_ARGS__ } + +#define OPT_INT(name, field, def, min, max, descr, ...) \ + OPT_GENERIC(name, field, def, min, max, descr, INT, i64, __VA_ARGS__) + +#define OPT_DBL(name, field, def, min, max, descr, ...) \ + OPT_GENERIC(name, field, def, min, max, descr, DOUBLE, dbl, __VA_ARGS__) + +#define OPT_DUR(name, field, def, min, max, descr, ...) \ + OPT_GENERIC(name, field, def, min, max, descr, DURATION, str, __VA_ARGS__)
a bit overkill? But we may move the helpers to an internal header.
+ +static const AVOption sine_options[]= {
nit+++: sine_options[]_= { [...] -- FFmpeg = Fostering and Fantastic Marvellous Peaceful EnGine
Le septidi 27 ventôse, an CCXXI, Stefano Sabatini a écrit :
... of the generated audio stream.
Locally added.
doxy? doxy?
Locally added: uint32_t phi; ///< current phase of the sine (2pi = 1<<32) uint32_t dphi; ///< phase increment between two samples uint32_t phi_beep; ///< current phase of the beep uint32_t dphi_beep; ///< phase increment of the beep
a bit overkill? But we may move the helpers to an internal header.
That is exactly the plan, provided it looks nice (it seems so to me for now) and people like them.
nit+++: sine_options[]_= {
Locally fixed. I do not think sending another patch is required for these trivial changes. Thanks for the review. Regards, -- Nicolas George
On date Saturday 2013-03-16 16:44:06 +0100, Nicolas George encoded:
Signed-off-by: Nicolas George <nicolas.george@normalesup.org> --- Changelog | 1 + doc/filters.texi | 51 +++++++++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/asrc_sine.c | 228 ++++++++++++++++++++++++++++++++++++++++++++++ libavfilter/version.h | 2 +- 6 files changed, 283 insertions(+), 1 deletion(-) create mode 100644 libavfilter/asrc_sine.c
diff --git a/Changelog b/Changelog index 516b293..958634b 100644 --- a/Changelog +++ b/Changelog @@ -8,6 +8,7 @@ version <next>: or vice versa - support for Monkey's Audio versions from 3.93 - perms and aperms filters +- sine audio filter source
version 1.2: diff --git a/doc/filters.texi b/doc/filters.texi index 0dbec8e..de16a2f 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -1653,6 +1653,57 @@ ffplay -f lavfi flite=text='No more be grieved for which that thou hast done.' For more information about libflite, check: @url{http://www.speech.cs.cmu.edu/flite/}
+@section sine + +Generate an audio signal made of a sine wave with amplitude 1/8. + +The audio signal is bit-exact. + +It accepts a list of options in the form of @var{key}=@var{value} pairs +separated by ":". If the option name is omitted, the first option is the +frequency and the second option is the beep factor. + +The supported options are: + +@table @option + +@item frequency, f +Set the carrier frequency. Default is 440 Hz. + +@item beep_factor, b +Enable a periodic beep every second with frequency @var{beep_factor} times +the carrier frequency. Default is 0, meaning the beep is disabled.
A possibly useful option could be beep_duration. [...]
+static void make_sin_table(int16_t *sin) +{ + unsigned half_pi = 1 << (LOG_PERIOD - 2); + unsigned ampls = AMPLITUDE << AMPLITUDE_SHIFT; + uint64_t unit2 = (uint64_t)(ampls * ampls) << 32; + unsigned step, i, c, s, k, new_k, n2; + + /* Principle: if u = exp(i*a1) and v = exp(i*a2), then + exp(i*(a1+a2)/2) = (u+v) / length(u+v) */ + sin[0] = 0; + sin[half_pi] = ampls; + for (step = half_pi; step > 1; step /= 2) { + /* k = (1 << 16) * amplitude / length(u+v) + In exact values, k is constant at a given step */ + k = 0x10000; + for (i = 0; i < half_pi / 2; i += step) { + s = sin[i] + sin[i + step]; + c = sin[half_pi - i] + sin[half_pi - i - step]; + n2 = s * s + c * c; + /* Newton's method to solve n² * k² = unit² */ + while (1) { + new_k = (k + unit2 / ((uint64_t)k * n2) + 1) >> 1; + if (k == new_k) + break; + k = new_k; + } + sin[i + step / 2] = (k * s + 0x7FFF) >> 16; + sin[half_pi - i - step / 2] = (k * c + 0x8000) >> 16; + } + } + /* Unshift amplitude */ + for (i = 0; i <= half_pi; i++) + sin[i] = (sin[i] + (1 << (AMPLITUDE_SHIFT - 1))) >> AMPLITUDE_SHIFT; + /* Use symmetries to fill the other three quarters */ + for (i = 0; i < half_pi; i++) + sin[half_pi * 2 - i] = sin[i]; + for (i = 0; i < 2 * half_pi; i++) + sin[i + 2 * half_pi] = -sin[i]; +}
The hope is that I'll be able to work the math, which is unlikely due to my lack of time, so feel free to commit if you see no comments from me or someone else in the next few days, thanks. -- FFmpeg = Frenzy Freak Mastering Problematic Enhanced Geisha
Le nonidi 29 ventôse, an CCXXI, Stefano Sabatini a écrit :
A possibly useful option could be beep_duration.
And beep_period too, possibly. But I leave it for someone else for now.
The hope is that I'll be able to work the math, which is unlikely due to my lack of time, so feel free to commit if you see no comments from me or someone else in the next few days, thanks.
I will send an updated patch series for reference after Paul's commit on opt and do that. Thanks for the remarks. Regards, -- Nicolas George
Signed-off-by: Nicolas George <nicolas.george@normalesup.org> --- doc/APIchanges | 3 +++ libavutil/opt.c | 31 ++++++++++++++++++++++++++++++- libavutil/opt.h | 1 + libavutil/version.h | 2 +- 4 files changed, 35 insertions(+), 2 deletions(-) I prefer to keep "duration" for accuracy and consistency. diff --git a/doc/APIchanges b/doc/APIchanges index 41552aa..255f914 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -15,6 +15,9 @@ libavutil: 2012-10-22 API changes, most recent first: +2013-03-20 - xxxxxxx - lavu 52.22.100 - opt.h + Add AV_OPT_TYPE_DURATION value to AVOptionType enum. + 2013-03-17 - xxxxxx - lavu 52.20.100 - opt.h Add AV_OPT_TYPE_VIDEO_RATE value to AVOptionType enum. diff --git a/libavutil/opt.c b/libavutil/opt.c index 2d149da..a2cdcb4 100644 --- a/libavutil/opt.c +++ b/libavutil/opt.c @@ -77,6 +77,7 @@ static int read_number(const AVOption *o, void *dst, double *num, int *den, int6 case AV_OPT_TYPE_PIXEL_FMT: case AV_OPT_TYPE_SAMPLE_FMT: case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0; + case AV_OPT_TYPE_DURATION: case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0; case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0; case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0; @@ -101,6 +102,7 @@ static int write_number(void *obj, const AVOption *o, void *dst, double num, int case AV_OPT_TYPE_PIXEL_FMT: case AV_OPT_TYPE_SAMPLE_FMT: case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break; + case AV_OPT_TYPE_DURATION: case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break; case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break; case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break; @@ -256,7 +258,8 @@ int av_opt_set(void *obj, const char *name, const char *val, int search_flags) return AVERROR_OPTION_NOT_FOUND; if (!val && (o->type != AV_OPT_TYPE_STRING && o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT && - o->type != AV_OPT_TYPE_IMAGE_SIZE && o->type != AV_OPT_TYPE_VIDEO_RATE)) + o->type != AV_OPT_TYPE_IMAGE_SIZE && o->type != AV_OPT_TYPE_VIDEO_RATE && + o->type != AV_OPT_TYPE_DURATION)) return AVERROR(EINVAL); dst = ((uint8_t*)target_obj) + o->offset; @@ -319,6 +322,15 @@ int av_opt_set(void *obj, const char *name, const char *val, int search_flags) } *(enum AVSampleFormat *)dst = ret; return 0; + case AV_OPT_TYPE_DURATION: + if (!val) { + *(int64_t *)dst = 0; + return 0; + } else { + if ((ret = av_parse_time(dst, val, 1)) < 0) + av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val); + return ret; + } } av_log(obj, AV_LOG_ERROR, "Invalid option type.\n"); @@ -556,6 +568,7 @@ int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val) const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj); uint8_t *bin, buf[128]; int len, i, ret; + int64_t i64; if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST)) return AVERROR_OPTION_NOT_FOUND; @@ -597,6 +610,12 @@ int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val) case AV_OPT_TYPE_SAMPLE_FMT: ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none")); break; + case AV_OPT_TYPE_DURATION: + i64 = *(int64_t *)dst; + ret = snprintf(buf, sizeof(buf), "%"PRIi64"d:%02d:%02d.%06d", + i64 / 3600000000, (int)((i64 / 60000000) % 60), + (int)((i64 / 1000000) % 60), (int)(i64 % 1000000)); + break; default: return AVERROR(EINVAL); } @@ -861,6 +880,9 @@ static void opt_list(void *obj, void *av_log_obj, const char *unit, case AV_OPT_TYPE_SAMPLE_FMT: av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>"); break; + case AV_OPT_TYPE_DURATION: + av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>"); + break; case AV_OPT_TYPE_CONST: default: av_log(av_log_obj, AV_LOG_INFO, "%-12s ", ""); @@ -937,6 +959,7 @@ void av_opt_set_defaults2(void *s, int mask, int flags) case AV_OPT_TYPE_FLAGS: case AV_OPT_TYPE_INT: case AV_OPT_TYPE_INT64: + case AV_OPT_TYPE_DURATION: av_opt_set_int(s, opt->name, opt->default_val.i64, 0); break; case AV_OPT_TYPE_DOUBLE: @@ -1300,6 +1323,7 @@ int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const ch case AV_OPT_TYPE_SAMPLE_FMT: case AV_OPT_TYPE_FLOAT: case AV_OPT_TYPE_DOUBLE: + case AV_OPT_TYPE_DURATION: break; case AV_OPT_TYPE_STRING: range->component_min = 0; @@ -1365,6 +1389,7 @@ typedef struct TestContext int w, h; enum AVPixelFormat pix_fmt; enum AVSampleFormat sample_fmt; + int64_t duration; } TestContext; #define OFFSET(x) offsetof(TestContext, x) @@ -1386,6 +1411,7 @@ static const AVOption test_options[]= { {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, AV_PIX_FMT_NB-1}, {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, {.i64 = AV_SAMPLE_FMT_NONE}, -1, AV_SAMPLE_FMT_NB-1}, {"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0 }, +{"duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX}, {NULL}, }; @@ -1441,6 +1467,9 @@ int main(void) "video_rate=30000/1001", "video_rate=30/1.001", "video_rate=bogus", + "duration=bogus", + "duration=123.45", + "duration=1\\:23\\:45.67", }; test_ctx.class = &test_class; diff --git a/libavutil/opt.h b/libavutil/opt.h index b36aebe..30f729e 100644 --- a/libavutil/opt.h +++ b/libavutil/opt.h @@ -231,6 +231,7 @@ enum AVOptionType{ AV_OPT_TYPE_PIXEL_FMT = MKBETAG('P','F','M','T'), AV_OPT_TYPE_SAMPLE_FMT = MKBETAG('S','F','M','T'), AV_OPT_TYPE_VIDEO_RATE = MKBETAG('V','R','A','T'), ///< offset must point to AVRational + AV_OPT_TYPE_DURATION = MKBETAG('D','U','R',' '), #if FF_API_OLD_AVOPTIONS FF_OPT_TYPE_FLAGS = 0, FF_OPT_TYPE_INT, diff --git a/libavutil/version.h b/libavutil/version.h index 0619eb9..1c163ca 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -75,7 +75,7 @@ */ #define LIBAVUTIL_VERSION_MAJOR 52 -#define LIBAVUTIL_VERSION_MINOR 21 +#define LIBAVUTIL_VERSION_MINOR 22 #define LIBAVUTIL_VERSION_MICRO 100 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ -- 1.7.10.4
Signed-off-by: Nicolas George <nicolas.george@normalesup.org> --- Changelog | 1 + doc/filters.texi | 51 +++++++++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/asrc_sine.c | 228 ++++++++++++++++++++++++++++++++++++++++++++++ libavfilter/version.h | 4 +- 6 files changed, 284 insertions(+), 2 deletions(-) create mode 100644 libavfilter/asrc_sine.c diff --git a/Changelog b/Changelog index 110e437..8a60d56 100644 --- a/Changelog +++ b/Changelog @@ -10,6 +10,7 @@ version <next>: - perms and aperms filters - audio filtering support in ffplay - 10% faster aac encoding on x86 and MIPS +- sine audio filter source version 1.2: diff --git a/doc/filters.texi b/doc/filters.texi index eb5962b..74a682a 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -1653,6 +1653,57 @@ ffplay -f lavfi flite=text='No more be grieved for which that thou hast done.' For more information about libflite, check: @url{http://www.speech.cs.cmu.edu/flite/} +@section sine + +Generate an audio signal made of a sine wave with amplitude 1/8. + +The audio signal is bit-exact. + +It accepts a list of options in the form of @var{key}=@var{value} pairs +separated by ":". If the option name is omitted, the first option is the +frequency and the second option is the beep factor. + +The supported options are: + +@table @option + +@item frequency, f +Set the carrier frequency. Default is 440 Hz. + +@item beep_factor, b +Enable a periodic beep every second with frequency @var{beep_factor} times +the carrier frequency. Default is 0, meaning the beep is disabled. + +@item sample_rate, s +Specify the sample rate, default is 44100. + +@item duration, d +Specify the duration of the generated audio stream. + +@item samples_per_frame +Set the number of samples per output frame, default is 1024. +@end table + +@subsection Examples + +@itemize + +@item +Generate a simple 440 Hz sine wave: +@example +sine +@end example + +@item +Generate a 220 Hz sine wave with a 880 Hz beep each second, for 5 seconds: +@example +sine=220:4:d=5 +sine=f=220:b=4:d=5 +sine=frequency=220:beep_factor=4:duration=5 +@end example + +@end itemize + @c man end AUDIO SOURCES @chapter Audio Sinks diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 0040a33..690b1cb 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -90,6 +90,7 @@ OBJS-$(CONFIG_VOLUMEDETECT_FILTER) += af_volumedetect.o OBJS-$(CONFIG_AEVALSRC_FILTER) += asrc_aevalsrc.o OBJS-$(CONFIG_ANULLSRC_FILTER) += asrc_anullsrc.o OBJS-$(CONFIG_FLITE_FILTER) += asrc_flite.o +OBJS-$(CONFIG_SINE_FILTER) += asrc_sine.o OBJS-$(CONFIG_ANULLSINK_FILTER) += asink_anullsink.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 086e6c9..45a67e5 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -86,6 +86,7 @@ void avfilter_register_all(void) REGISTER_FILTER(AEVALSRC, aevalsrc, asrc); REGISTER_FILTER(ANULLSRC, anullsrc, asrc); REGISTER_FILTER(FLITE, flite, asrc); + REGISTER_FILTER(SINE, sine, asrc); REGISTER_FILTER(ANULLSINK, anullsink, asink); diff --git a/libavfilter/asrc_sine.c b/libavfilter/asrc_sine.c new file mode 100644 index 0000000..8b406ff --- /dev/null +++ b/libavfilter/asrc_sine.c @@ -0,0 +1,228 @@ +/* + * Copyright (c) 2013 Nicolas George + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with FFmpeg; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/avassert.h" +#include "libavutil/channel_layout.h" +#include "libavutil/opt.h" +#include "audio.h" +#include "avfilter.h" +#include "internal.h" + +typedef struct { + const AVClass *class; + double frequency; + double beep_factor; + int samples_per_frame; + int sample_rate; + int64_t duration; + int16_t *sin; + int64_t pts; + uint32_t phi; ///< current phase of the sine (2pi = 1<<32) + uint32_t dphi; ///< phase increment between two samples + unsigned beep_period; + unsigned beep_index; + unsigned beep_length; + uint32_t phi_beep; ///< current phase of the beep + uint32_t dphi_beep; ///< phase increment of the beep +} SineContext; + +#define CONTEXT SineContext +#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM + +#define OPT_GENERIC(name, field, def, min, max, descr, type, deffield, ...) \ + { name, descr, offsetof(CONTEXT, field), AV_OPT_TYPE_ ## type, \ + { .deffield = def }, min, max, FLAGS, __VA_ARGS__ } + +#define OPT_INT(name, field, def, min, max, descr, ...) \ + OPT_GENERIC(name, field, def, min, max, descr, INT, i64, __VA_ARGS__) + +#define OPT_DBL(name, field, def, min, max, descr, ...) \ + OPT_GENERIC(name, field, def, min, max, descr, DOUBLE, dbl, __VA_ARGS__) + +#define OPT_DUR(name, field, def, min, max, descr, ...) \ + OPT_GENERIC(name, field, def, min, max, descr, DURATION, str, __VA_ARGS__) + +static const AVOption sine_options[] = { + OPT_DBL("frequency", frequency, 440, 0, INFINITY, "set the sine frequency"), + OPT_DBL("f", frequency, 440, 0, INFINITY, "set the sine frequency"), + OPT_DBL("beep_factor", beep_factor, 0, 0, INFINITY, "set the beep fequency factor"), + OPT_DBL("b", beep_factor, 0, 0, INFINITY, "set the beep fequency factor"), + OPT_INT("sample_rate", sample_rate, 44100, 1, INT_MAX, "set the sample rate"), + OPT_INT("r", sample_rate, 44100, 1, INT_MAX, "set the sample rate"), + OPT_DUR("duration", duration, 0, 0, INT64_MAX, "set the audio duration"), + OPT_DUR("d", duration, 0, 0, INT64_MAX, "set the audio duration"), + OPT_INT("samples_per_frame", samples_per_frame, 1024, 0, INT_MAX, "set the number of samples per frame"), + {NULL}, +}; + +AVFILTER_DEFINE_CLASS(sine); + +#define LOG_PERIOD 15 +#define AMPLITUDE 4095 +#define AMPLITUDE_SHIFT 3 + +static void make_sin_table(int16_t *sin) +{ + unsigned half_pi = 1 << (LOG_PERIOD - 2); + unsigned ampls = AMPLITUDE << AMPLITUDE_SHIFT; + uint64_t unit2 = (uint64_t)(ampls * ampls) << 32; + unsigned step, i, c, s, k, new_k, n2; + + /* Principle: if u = exp(i*a1) and v = exp(i*a2), then + exp(i*(a1+a2)/2) = (u+v) / length(u+v) */ + sin[0] = 0; + sin[half_pi] = ampls; + for (step = half_pi; step > 1; step /= 2) { + /* k = (1 << 16) * amplitude / length(u+v) + In exact values, k is constant at a given step */ + k = 0x10000; + for (i = 0; i < half_pi / 2; i += step) { + s = sin[i] + sin[i + step]; + c = sin[half_pi - i] + sin[half_pi - i - step]; + n2 = s * s + c * c; + /* Newton's method to solve n² * k² = unit² */ + while (1) { + new_k = (k + unit2 / ((uint64_t)k * n2) + 1) >> 1; + if (k == new_k) + break; + k = new_k; + } + sin[i + step / 2] = (k * s + 0x7FFF) >> 16; + sin[half_pi - i - step / 2] = (k * c + 0x8000) >> 16; + } + } + /* Unshift amplitude */ + for (i = 0; i <= half_pi; i++) + sin[i] = (sin[i] + (1 << (AMPLITUDE_SHIFT - 1))) >> AMPLITUDE_SHIFT; + /* Use symmetries to fill the other three quarters */ + for (i = 0; i < half_pi; i++) + sin[half_pi * 2 - i] = sin[i]; + for (i = 0; i < 2 * half_pi; i++) + sin[i + 2 * half_pi] = -sin[i]; +} + +static av_cold int init(AVFilterContext *ctx, const char *args) +{ + SineContext *sine = ctx->priv; + static const char *shorthand[] = { "frequency", "beep_factor", NULL }; + int ret; + + sine->class = &sine_class; + av_opt_set_defaults(sine); + + if ((ret = av_opt_set_from_string(sine, args, shorthand, "=", ":")) < 0) + return ret; + if (!(sine->sin = av_malloc(sizeof(*sine->sin) << LOG_PERIOD))) + return AVERROR(ENOMEM); + sine->dphi = ldexp(sine->frequency, 32) / sine->sample_rate + 0.5; + make_sin_table(sine->sin); + + if (sine->beep_factor) { + sine->beep_period = sine->sample_rate; + sine->beep_length = sine->beep_period / 25; + sine->dphi_beep = ldexp(sine->beep_factor * sine->frequency, 32) / + sine->sample_rate + 0.5; + } + + return 0; +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + SineContext *sine = ctx->priv; + + av_freep(&sine->sin); +} + +static av_cold int query_formats(AVFilterContext *ctx) +{ + SineContext *sine = ctx->priv; + static const int64_t chlayouts[] = { AV_CH_LAYOUT_MONO, -1 }; + int sample_rates[] = { sine->sample_rate, -1 }; + static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, + AV_SAMPLE_FMT_NONE }; + + ff_set_common_formats (ctx, ff_make_format_list(sample_fmts)); + ff_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts)); + ff_set_common_samplerates(ctx, ff_make_format_list(sample_rates)); + return 0; +} + +static av_cold int config_props(AVFilterLink *outlink) +{ + SineContext *sine = outlink->src->priv; + sine->duration = av_rescale(sine->duration, sine->sample_rate, AV_TIME_BASE); + return 0; +} + +static int request_frame(AVFilterLink *outlink) +{ + SineContext *sine = outlink->src->priv; + AVFrame *frame; + int i, nb_samples = sine->samples_per_frame; + int16_t *samples; + + if (sine->duration) { + nb_samples = FFMIN(nb_samples, sine->duration - sine->pts); + av_assert1(nb_samples >= 0); + if (!nb_samples) + return AVERROR_EOF; + } + if (!(frame = ff_get_audio_buffer(outlink, nb_samples))) + return AVERROR(ENOMEM); + samples = (int16_t *)frame->data[0]; + + for (i = 0; i < nb_samples; i++) { + samples[i] = sine->sin[sine->phi >> (32 - LOG_PERIOD)]; + sine->phi += sine->dphi; + if (sine->beep_index < sine->beep_length) { + samples[i] += sine->sin[sine->phi_beep >> (32 - LOG_PERIOD)] << 1; + sine->phi_beep += sine->dphi_beep; + } + if (++sine->beep_index == sine->beep_period) + sine->beep_index = 0; + } + + frame->pts = sine->pts; + sine->pts += nb_samples; + return ff_filter_frame(outlink, frame); +} + +static const AVFilterPad sine_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .request_frame = request_frame, + .config_props = config_props, + }, + { NULL } +}; + +AVFilter avfilter_asrc_sine = { + .name = "sine", + .description = NULL_IF_CONFIG_SMALL("Generate sine wave audio signal."), + .query_formats = query_formats, + .init = init, + .uninit = uninit, + .priv_size = sizeof(SineContext), + .inputs = NULL, + .outputs = sine_outputs, + .priv_class = &sine_class, +}; diff --git a/libavfilter/version.h b/libavfilter/version.h index 88a782c..f592fc1 100644 --- a/libavfilter/version.h +++ b/libavfilter/version.h @@ -29,8 +29,8 @@ #include "libavutil/avutil.h" #define LIBAVFILTER_VERSION_MAJOR 3 -#define LIBAVFILTER_VERSION_MINOR 47 -#define LIBAVFILTER_VERSION_MICRO 104 +#define LIBAVFILTER_VERSION_MINOR 48 +#define LIBAVFILTER_VERSION_MICRO 100 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ LIBAVFILTER_VERSION_MINOR, \ -- 1.7.10.4
On 3/20/13, Nicolas George <nicolas.george@normalesup.org> wrote:
Signed-off-by: Nicolas George <nicolas.george@normalesup.org> --- doc/APIchanges | 3 +++ libavutil/opt.c | 31 ++++++++++++++++++++++++++++++- libavutil/opt.h | 1 + libavutil/version.h | 2 +- 4 files changed, 35 insertions(+), 2 deletions(-)
I prefer to keep "duration" for accuracy and consistency.
When you plan to make actually use of this type?
Le sextidi 6 germinal, an CCXXI, Paul B Mahol a écrit :
When you plan to make actually use of this type?
It is already used in asrc_sine. It does not appear on grep because it goes through some macros that are intended to make the options more readable. If it is a success (I believe it is, but I need to check with other options types), I intend to propose to move the macros into a common header and use them for other filters. Moving the other test sources to make use of AV_OPT_TYPE_DURATION, like you did for the frame rates, is a worthy endeavour (and thanks for your work on that) but I did not had time to work on it yet. Regards, -- Nicolas George
On Tue, Mar 26, 2013 at 08:44:57PM +0100, Nicolas George wrote:
Le sextidi 6 germinal, an CCXXI, Paul B Mahol a écrit :
When you plan to make actually use of this type?
It is already used in asrc_sine. It does not appear on grep because it goes through some macros that are intended to make the options more readable. If
I dont think macros that break common grep use are a good idea [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB I am the wisest man alive, for I know one thing, and that is that I know nothing. -- Socrates
Le sextidi 6 germinal, an CCXXI, Michael Niedermayer a écrit :
I dont think macros that break common grep use are a good idea
I agree this is a drawback I had not anticipated, but it should be weighted against the benefits: this macro system makes the options much more readable, IMHO (see its use in asrc_sine.c). I will submit to the majority opinion on that. (Arguably, "grep AV_OPT_TYPE_DURATION" finding only places where AV_OPT_TYPE_DURATION is actually used in the code without being polluted by all the options definitions could be considered a plus; "grep 'OPT.*DUR'" will always find all occurrences.) Regards, -- Nicolas George
On 3/16/13, Nicolas George <nicolas.george@normalesup.org> wrote:
Signed-off-by: Nicolas George <nicolas.george@normalesup.org> --- Changelog | 1 + doc/filters.texi | 51 +++++++++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/asrc_sine.c | 228 ++++++++++++++++++++++++++++++++++++++++++++++ libavfilter/version.h | 2 +- 6 files changed, 283 insertions(+), 1 deletion(-) create mode 100644 libavfilter/asrc_sine.c
What was motivation for this patch? Am I wrong or same functionality is available via aevalsec?
Le nonidi 29 ventôse, an CCXXI, Paul B Mahol a écrit :
What was motivation for this patch?
Am I wrong or same functionality is available via aevalsec?
This version is at least thirty times faster than aevalsec, counting ffmpeg overhead, but that does not matter much (although it will probably be nice when running under valgrind). What matters, and the motivation for writing it, is that except for options parsing, everything is done with integer arithmetic, without arch-dependant rounding errors, and therefore usable in FATE tests. Regards, -- Nicolas George
On date Saturday 2013-03-16 16:44:05 +0100, Nicolas George encoded:
Signed-off-by: Nicolas George <nicolas.george@normalesup.org> --- doc/APIchanges | 3 +++ libavutil/opt.c | 30 +++++++++++++++++++++++++++++- libavutil/opt.h | 1 + libavutil/version.h | 2 +- 4 files changed, 34 insertions(+), 2 deletions(-)
diff --git a/doc/APIchanges b/doc/APIchanges index 2bde666..3f33e03 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -15,6 +15,9 @@ libavutil: 2012-10-22
API changes, most recent first:
+2013-03-16 - xxxxxxx - lavu 52.20.100 - opt.h + Add AV_OPT_TYPE_DURATION value to AVOptionType enum. + 2013-03-07 - xxxxxx - lavu 52.18.100 - avstring.h,bprint.h Add av_escape() and av_bprint_escape() API.
diff --git a/libavutil/opt.c b/libavutil/opt.c index f91d18b..b4c2dc7 100644 --- a/libavutil/opt.c +++ b/libavutil/opt.c @@ -77,6 +77,7 @@ static int read_number(const AVOption *o, void *dst, double *num, int *den, int6 case AV_OPT_TYPE_PIXEL_FMT: case AV_OPT_TYPE_SAMPLE_FMT: case AV_OPT_TYPE_INT: *intnum = *(int *)dst;return 0; + case AV_OPT_TYPE_DURATION: case AV_OPT_TYPE_INT64: *intnum = *(int64_t *)dst;return 0; case AV_OPT_TYPE_FLOAT: *num = *(float *)dst;return 0; case AV_OPT_TYPE_DOUBLE: *num = *(double *)dst;return 0; @@ -101,6 +102,7 @@ static int write_number(void *obj, const AVOption *o, void *dst, double num, int case AV_OPT_TYPE_PIXEL_FMT: case AV_OPT_TYPE_SAMPLE_FMT: case AV_OPT_TYPE_INT: *(int *)dst= llrint(num/den)*intnum; break; + case AV_OPT_TYPE_DURATION: case AV_OPT_TYPE_INT64: *(int64_t *)dst= llrint(num/den)*intnum; break; case AV_OPT_TYPE_FLOAT: *(float *)dst= num*intnum/den; break; case AV_OPT_TYPE_DOUBLE:*(double *)dst= num*intnum/den; break; @@ -256,7 +258,7 @@ int av_opt_set(void *obj, const char *name, const char *val, int search_flags) return AVERROR_OPTION_NOT_FOUND; if (!val && (o->type != AV_OPT_TYPE_STRING && o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT && - o->type != AV_OPT_TYPE_IMAGE_SIZE)) + o->type != AV_OPT_TYPE_IMAGE_SIZE && o->type != AV_OPT_TYPE_DURATION)) return AVERROR(EINVAL);
dst = ((uint8_t*)target_obj) + o->offset; @@ -310,6 +312,15 @@ int av_opt_set(void *obj, const char *name, const char *val, int search_flags) } *(enum AVSampleFormat *)dst = ret; return 0; + case AV_OPT_TYPE_DURATION: + if (!val) { + *(int64_t *)dst = 0; + return 0; + } else { + if ((ret = av_parse_time(dst, val, 1)) < 0) + av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val); + return ret; + } }
av_log(obj, AV_LOG_ERROR, "Invalid option type.\n"); @@ -530,6 +541,7 @@ int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val) const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj); uint8_t *bin, buf[128]; int len, i, ret; + int64_t i64;
if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST)) return AVERROR_OPTION_NOT_FOUND; @@ -570,6 +582,12 @@ int av_opt_get(void *obj, const char *name, int search_flags, uint8_t **out_val) case AV_OPT_TYPE_SAMPLE_FMT: ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none")); break; + case AV_OPT_TYPE_DURATION: + i64 = *(int64_t *)dst; + ret = snprintf(buf, sizeof(buf), "%"PRIi64"d:%02d:%02d.%06d", + i64 / 3600000000, (int)((i64 / 60000000) % 60), + (int)((i64 / 1000000) % 60), (int)(i64 % 1000000)); + break; default: return AVERROR(EINVAL); } @@ -815,6 +833,9 @@ static void opt_list(void *obj, void *av_log_obj, const char *unit, case AV_OPT_TYPE_SAMPLE_FMT: av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>"); break; + case AV_OPT_TYPE_DURATION: + av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>"); + break; case AV_OPT_TYPE_CONST: default: av_log(av_log_obj, AV_LOG_INFO, "%-12s ", ""); @@ -891,6 +912,7 @@ void av_opt_set_defaults2(void *s, int mask, int flags) case AV_OPT_TYPE_FLAGS: case AV_OPT_TYPE_INT: case AV_OPT_TYPE_INT64: + case AV_OPT_TYPE_DURATION: av_opt_set_int(s, opt->name, opt->default_val.i64, 0); break; case AV_OPT_TYPE_DOUBLE: @@ -1253,6 +1275,7 @@ int av_opt_query_ranges_default(AVOptionRanges **ranges_arg, void *obj, const ch case AV_OPT_TYPE_SAMPLE_FMT: case AV_OPT_TYPE_FLOAT: case AV_OPT_TYPE_DOUBLE: + case AV_OPT_TYPE_DURATION: break; case AV_OPT_TYPE_STRING: range->component_min = 0; @@ -1311,6 +1334,7 @@ typedef struct TestContext int w, h; enum AVPixelFormat pix_fmt; enum AVSampleFormat sample_fmt; + int64_t duration; } TestContext;
#define OFFSET(x) offsetof(TestContext, x) @@ -1331,6 +1355,7 @@ static const AVOption test_options[]= { {"size", "set size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,{0}, 0, 0 }, {"pix_fmt", "set pixfmt", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, AV_PIX_FMT_NB-1}, {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, {.i64 = AV_SAMPLE_FMT_NONE}, -1, AV_SAMPLE_FMT_NB-1}, +{"duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX}, {NULL}, };
@@ -1381,6 +1406,9 @@ int main(void) "sample_fmt=s16", "sample_fmt=2", "sample_fmt=bogus", + "duration=bogus", + "duration=123.45", + "duration=1\\:23\\:45.67", };
test_ctx.class = &test_class; diff --git a/libavutil/opt.h b/libavutil/opt.h index baf1b82..1c86e6f 100644 --- a/libavutil/opt.h +++ b/libavutil/opt.h @@ -230,6 +230,7 @@ enum AVOptionType{ AV_OPT_TYPE_IMAGE_SIZE = MKBETAG('S','I','Z','E'), ///< offset must point to two consecutive integers AV_OPT_TYPE_PIXEL_FMT = MKBETAG('P','F','M','T'), AV_OPT_TYPE_SAMPLE_FMT = MKBETAG('S','F','M','T'), + AV_OPT_TYPE_DURATION = MKBETAG('D','U','R',' '), #if FF_API_OLD_AVOPTIONS FF_OPT_TYPE_FLAGS = 0, FF_OPT_TYPE_INT, diff --git a/libavutil/version.h b/libavutil/version.h index d95a8a2..1ff9ffe 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -75,7 +75,7 @@ */
#define LIBAVUTIL_VERSION_MAJOR 52 -#define LIBAVUTIL_VERSION_MINOR 19 +#define LIBAVUTIL_VERSION_MINOR 20 #define LIBAVUTIL_VERSION_MICRO 100
LGTM, same consideration as per the OPT_VIDEO_RATE patch, an av_opt_get/set_duration() may be useful at some point (possibly in the same patch). -- FFmpeg = Freak & Frenzy Mean Plastic Elected God
Le septidi 27 ventôse, an CCXXI, Stefano Sabatini a écrit :
LGTM, same consideration as per the OPT_VIDEO_RATE patch, an av_opt_get/set_duration() may be useful at some point (possibly in the same patch).
Thanks for the review. What added value do you have in mind for av_opt_[gs]_duration(), compared to _int()? AFAIK, from an API point of view, a duration is exactly the same as an int64, and the only point of a special type is the H:MM:SS parsing and pretty-printing. Regards, -- Nicolas George
Hi, On Sun, Mar 17, 2013 at 12:30 PM, Nicolas George <nicolas.george@normalesup.org> wrote:
Le septidi 27 ventôse, an CCXXI, Stefano Sabatini a écrit :
LGTM, same consideration as per the OPT_VIDEO_RATE patch, an av_opt_get/set_duration() may be useful at some point (possibly in the same patch).
Thanks for the review. What added value do you have in mind for av_opt_[gs]_duration(), compared to _int()? AFAIK, from an API point of view, a duration is exactly the same as an int64, and the only point of a special type is the H:MM:SS parsing and pretty-printing.
So why is it called duration? I don't see anything that is duration-specific in this description. Don't you mean "time"? Ronald
Le septidi 27 ventôse, an CCXXI, Ronald S. Bultje a écrit :
So why is it called duration? I don't see anything that is duration-specific in this description. Don't you mean "time"?
It corresponds to this: * @param duration flag which tells how to interpret timestr, if not * zero timestr is interpreted as a duration, otherwise as a date * @return 0 in case of success, a negative value corresponding to an * AVERROR code otherwise */ int av_parse_time(int64_t *timeval, const char *timestr, int duration); This patch uses duration=1 when calling av_parse_time(). Maybe at some point adding AV_OPT_TYPE_DATE or _TIME may be useful, but I had not yet encountered the need. Regards, -- Nicolas George
On date Sunday 2013-03-17 21:31:08 +0100, Nicolas George encoded:
Le septidi 27 ventôse, an CCXXI, Ronald S. Bultje a écrit :
So why is it called duration? I don't see anything that is duration-specific in this description. Don't you mean "time"?
It corresponds to this:
* @param duration flag which tells how to interpret timestr, if not * zero timestr is interpreted as a duration, otherwise as a date * @return 0 in case of success, a negative value corresponding to an * AVERROR code otherwise */ int av_parse_time(int64_t *timeval, const char *timestr, int duration);
This patch uses duration=1 when calling av_parse_time(). Maybe at some point adding AV_OPT_TYPE_DATE or _TIME may be useful, but I had not yet encountered the need.
AV_OPT_TYPE_TIME is fine as well, provided that "time" is not meant as "date", AV_OPT_TYPE_DURATION is IMO acceptable so I let the contributor to decide. Probably: int av_parse_time(int64_t *timeval, const char *timestr, int is_date); was a better choice, but it is too late to change it (and changing the API for that would be rather overkill at this point). -- FFmpeg = Freak & Faithless Monstrous Patchable Egregious Game
participants (5)
-
Michael Niedermayer -
Nicolas George -
Paul B Mahol -
Ronald S. Bultje -
Stefano Sabatini