* commit 'b384e031daeb1ac612620985e3e5377bc587559c':
lavfi: add volume filter
Conflicts:
Changelog
libavfilter/Makefile
libavfilter/af_volume.c
libavfilter/version.h
Merged-by: Michael Niedermayer <michaelni@gmx.at>
... | ... |
@@ -701,96 +701,6 @@ tolerance in @file{silence.mp3}: |
701 | 701 |
ffmpeg -f lavfi -i amovie=silence.mp3,silencedetect=noise=0.0001 -f null - |
702 | 702 |
@end example |
703 | 703 |
|
704 |
-@section volume |
|
705 |
- |
|
706 |
-Adjust the input audio volume. |
|
707 |
- |
|
708 |
-The filter accepts exactly one parameter @var{vol}, which expresses |
|
709 |
-how the audio volume will be increased or decreased. |
|
710 |
- |
|
711 |
-Output values are clipped to the maximum value. |
|
712 |
- |
|
713 |
-If @var{vol} is expressed as a decimal number, the output audio |
|
714 |
-volume is given by the relation: |
|
715 |
-@example |
|
716 |
-@var{output_volume} = @var{vol} * @var{input_volume} |
|
717 |
-@end example |
|
718 |
- |
|
719 |
-If @var{vol} is expressed as a decimal number followed by the string |
|
720 |
-"dB", the value represents the requested change in decibels of the |
|
721 |
-input audio power, and the output audio volume is given by the |
|
722 |
-relation: |
|
723 |
-@example |
|
724 |
-@var{output_volume} = 10^(@var{vol}/20) * @var{input_volume} |
|
725 |
-@end example |
|
726 |
- |
|
727 |
-Otherwise @var{vol} is considered an expression and its evaluated |
|
728 |
-value is used for computing the output audio volume according to the |
|
729 |
-first relation. |
|
730 |
- |
|
731 |
-Default value for @var{vol} is 1.0. |
|
732 |
- |
|
733 |
-@subsection Examples |
|
734 |
- |
|
735 |
-@itemize |
|
736 |
-@item |
|
737 |
-Half the input audio volume: |
|
738 |
-@example |
|
739 |
-volume=0.5 |
|
740 |
-@end example |
|
741 |
- |
|
742 |
-The above example is equivalent to: |
|
743 |
-@example |
|
744 |
-volume=1/2 |
|
745 |
-@end example |
|
746 |
- |
|
747 |
-@item |
|
748 |
-Decrease input audio power by 12 decibels: |
|
749 |
-@example |
|
750 |
-volume=-12dB |
|
751 |
-@end example |
|
752 |
-@end itemize |
|
753 |
- |
|
754 |
-@section volumedetect |
|
755 |
- |
|
756 |
-Detect the volume of the input video. |
|
757 |
- |
|
758 |
-The filter has no parameters. The input is not modified. Statistics about |
|
759 |
-the volume will be printed in the log when the input stream end is reached. |
|
760 |
- |
|
761 |
-In particular it will show the mean volume (root mean square), maximum |
|
762 |
-volume (on a per-sample basis), and the beginning of an histogram of the |
|
763 |
-registered volume values (from the maximum value to a cumulated 1/1000 of |
|
764 |
-the samples). |
|
765 |
- |
|
766 |
-All volumes are in decibels relative to the maximum PCM value. |
|
767 |
- |
|
768 |
-Here is an excerpt of the output: |
|
769 |
-@example |
|
770 |
-[Parsed_volumedetect_0 @ 0xa23120] mean_volume: -27 dB |
|
771 |
-[Parsed_volumedetect_0 @ 0xa23120] max_volume: -4 dB |
|
772 |
-[Parsed_volumedetect_0 @ 0xa23120] histogram_4db: 6 |
|
773 |
-[Parsed_volumedetect_0 @ 0xa23120] histogram_5db: 62 |
|
774 |
-[Parsed_volumedetect_0 @ 0xa23120] histogram_6db: 286 |
|
775 |
-[Parsed_volumedetect_0 @ 0xa23120] histogram_7db: 1042 |
|
776 |
-[Parsed_volumedetect_0 @ 0xa23120] histogram_8db: 2551 |
|
777 |
-[Parsed_volumedetect_0 @ 0xa23120] histogram_9db: 4609 |
|
778 |
-[Parsed_volumedetect_0 @ 0xa23120] histogram_10db: 8409 |
|
779 |
-@end example |
|
780 |
- |
|
781 |
-It means that: |
|
782 |
-@itemize |
|
783 |
-@item |
|
784 |
-The mean square energy is approximately -27 dB, or 10^-2.7. |
|
785 |
-@item |
|
786 |
-The largest sample is at -4 dB, or more precisely between -4 dB and -5 dB. |
|
787 |
-@item |
|
788 |
-There are 6 samples at -4 dB, 62 at -5 dB, 286 at -6 dB, etc. |
|
789 |
-@end itemize |
|
790 |
- |
|
791 |
-In other words, raising the volume by +4 dB does not cause any clipping, |
|
792 |
-raising it by +5 dB causes clipping for 6 samples, etc. |
|
793 |
- |
|
794 | 704 |
@section asyncts |
795 | 705 |
Synchronize audio data with timestamps by squeezing/stretching it and/or |
796 | 706 |
dropping samples/adding silence when needed. |
... | ... |
@@ -919,6 +829,149 @@ out |
919 | 919 |
Convert the audio sample format, sample rate and channel layout. This filter is |
920 | 920 |
not meant to be used directly. |
921 | 921 |
|
922 |
+@section volume |
|
923 |
+ |
|
924 |
+Adjust the input audio volume. |
|
925 |
+ |
|
926 |
+The filter accepts exactly one parameter @var{vol}, which expresses |
|
927 |
+how the audio volume will be increased or decreased. |
|
928 |
+ |
|
929 |
+Output values are clipped to the maximum value. |
|
930 |
+ |
|
931 |
+If @var{vol} is expressed as a decimal number, the output audio |
|
932 |
+volume is given by the relation: |
|
933 |
+@example |
|
934 |
+@var{output_volume} = @var{vol} * @var{input_volume} |
|
935 |
+@end example |
|
936 |
+ |
|
937 |
+If @var{vol} is expressed as a decimal number followed by the string |
|
938 |
+"dB", the value represents the requested change in decibels of the |
|
939 |
+input audio power, and the output audio volume is given by the |
|
940 |
+relation: |
|
941 |
+@example |
|
942 |
+@var{output_volume} = 10^(@var{vol}/20) * @var{input_volume} |
|
943 |
+@end example |
|
944 |
+ |
|
945 |
+Otherwise @var{vol} is considered an expression and its evaluated |
|
946 |
+value is used for computing the output audio volume according to the |
|
947 |
+first relation. |
|
948 |
+ |
|
949 |
+Default value for @var{vol} is 1.0. |
|
950 |
+ |
|
951 |
+@subsection Examples |
|
952 |
+ |
|
953 |
+@itemize |
|
954 |
+@item |
|
955 |
+Half the input audio volume: |
|
956 |
+@example |
|
957 |
+volume=0.5 |
|
958 |
+@end example |
|
959 |
+ |
|
960 |
+The above example is equivalent to: |
|
961 |
+@example |
|
962 |
+volume=1/2 |
|
963 |
+@end example |
|
964 |
+ |
|
965 |
+@item |
|
966 |
+Decrease input audio power by 12 decibels: |
|
967 |
+@example |
|
968 |
+volume=-12dB |
|
969 |
+@end example |
|
970 |
+@end itemize |
|
971 |
+ |
|
972 |
+@section volumedetect |
|
973 |
+ |
|
974 |
+Detect the volume of the input video. |
|
975 |
+ |
|
976 |
+The filter has no parameters. The input is not modified. Statistics about |
|
977 |
+the volume will be printed in the log when the input stream end is reached. |
|
978 |
+ |
|
979 |
+In particular it will show the mean volume (root mean square), maximum |
|
980 |
+volume (on a per-sample basis), and the beginning of an histogram of the |
|
981 |
+registered volume values (from the maximum value to a cumulated 1/1000 of |
|
982 |
+the samples). |
|
983 |
+ |
|
984 |
+All volumes are in decibels relative to the maximum PCM value. |
|
985 |
+ |
|
986 |
+Here is an excerpt of the output: |
|
987 |
+@example |
|
988 |
+[Parsed_volumedetect_0 @ 0xa23120] mean_volume: -27 dB |
|
989 |
+[Parsed_volumedetect_0 @ 0xa23120] max_volume: -4 dB |
|
990 |
+[Parsed_volumedetect_0 @ 0xa23120] histogram_4db: 6 |
|
991 |
+[Parsed_volumedetect_0 @ 0xa23120] histogram_5db: 62 |
|
992 |
+[Parsed_volumedetect_0 @ 0xa23120] histogram_6db: 286 |
|
993 |
+[Parsed_volumedetect_0 @ 0xa23120] histogram_7db: 1042 |
|
994 |
+[Parsed_volumedetect_0 @ 0xa23120] histogram_8db: 2551 |
|
995 |
+[Parsed_volumedetect_0 @ 0xa23120] histogram_9db: 4609 |
|
996 |
+[Parsed_volumedetect_0 @ 0xa23120] histogram_10db: 8409 |
|
997 |
+@end example |
|
998 |
+ |
|
999 |
+It means that: |
|
1000 |
+@itemize |
|
1001 |
+@item |
|
1002 |
+The mean square energy is approximately -27 dB, or 10^-2.7. |
|
1003 |
+@item |
|
1004 |
+The largest sample is at -4 dB, or more precisely between -4 dB and -5 dB. |
|
1005 |
+@item |
|
1006 |
+There are 6 samples at -4 dB, 62 at -5 dB, 286 at -6 dB, etc. |
|
1007 |
+@end itemize |
|
1008 |
+ |
|
1009 |
+In other words, raising the volume by +4 dB does not cause any clipping, |
|
1010 |
+raising it by +5 dB causes clipping for 6 samples, etc. |
|
1011 |
+ |
|
1012 |
+@section volume_justin |
|
1013 |
+ |
|
1014 |
+Adjust the input audio volume. |
|
1015 |
+ |
|
1016 |
+The filter accepts the following named parameters: |
|
1017 |
+@table @option |
|
1018 |
+ |
|
1019 |
+@item volume |
|
1020 |
+Expresses how the audio volume will be increased or decreased. |
|
1021 |
+ |
|
1022 |
+Output values are clipped to the maximum value. |
|
1023 |
+ |
|
1024 |
+The output audio volume is given by the relation: |
|
1025 |
+@example |
|
1026 |
+@var{output_volume} = @var{volume} * @var{input_volume} |
|
1027 |
+@end example |
|
1028 |
+ |
|
1029 |
+Default value for @var{volume} is 1.0. |
|
1030 |
+ |
|
1031 |
+@item precision |
|
1032 |
+Mathematical precision. |
|
1033 |
+ |
|
1034 |
+This determines which input sample formats will be allowed, which affects the |
|
1035 |
+precision of the volume scaling. |
|
1036 |
+ |
|
1037 |
+@table @option |
|
1038 |
+@item fixed |
|
1039 |
+8-bit fixed-point; limits input sample format to U8, S16, and S32. |
|
1040 |
+@item float |
|
1041 |
+32-bit floating-point; limits input sample format to FLT. (default) |
|
1042 |
+@item double |
|
1043 |
+64-bit floating-point; limits input sample format to DBL. |
|
1044 |
+@end table |
|
1045 |
+@end table |
|
1046 |
+ |
|
1047 |
+@subsection Examples |
|
1048 |
+ |
|
1049 |
+@itemize |
|
1050 |
+@item |
|
1051 |
+Halve the input audio volume: |
|
1052 |
+@example |
|
1053 |
+volume_justin=volume=0.5 |
|
1054 |
+volume_justin=volume=1/2 |
|
1055 |
+volume_justin=volume=-6.0206dB |
|
1056 |
+@end example |
|
1057 |
+ |
|
1058 |
+@item |
|
1059 |
+Increase input audio power by 6 decibels using fixed-point precision: |
|
1060 |
+@example |
|
1061 |
+volume_justin=volume=6dB:precision=fixed |
|
1062 |
+@end example |
|
1063 |
+@end itemize |
|
1064 |
+ |
|
922 | 1065 |
@c man end AUDIO FILTERS |
923 | 1066 |
|
924 | 1067 |
@chapter Audio Sources |
... | ... |
@@ -72,6 +72,7 @@ OBJS-$(CONFIG_PAN_FILTER) += af_pan.o |
72 | 72 |
OBJS-$(CONFIG_RESAMPLE_FILTER) += af_resample.o |
73 | 73 |
OBJS-$(CONFIG_SILENCEDETECT_FILTER) += af_silencedetect.o |
74 | 74 |
OBJS-$(CONFIG_VOLUME_FILTER) += af_volume.o |
75 |
+OBJS-$(CONFIG_VOLUME_JUSTIN_FILTER) += af_volume_justin.o |
|
75 | 76 |
OBJS-$(CONFIG_VOLUMEDETECT_FILTER) += af_volumedetect.o |
76 | 77 |
|
77 | 78 |
OBJS-$(CONFIG_AEVALSRC_FILTER) += asrc_aevalsrc.o |
78 | 79 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,53 @@ |
0 |
+/* |
|
1 |
+ * This file is part of FFmpeg. |
|
2 |
+ * |
|
3 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
4 |
+ * modify it under the terms of the GNU Lesser General Public |
|
5 |
+ * License as published by the Free Software Foundation; either |
|
6 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
7 |
+ * |
|
8 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
9 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
11 |
+ * Lesser General Public License for more details. |
|
12 |
+ * |
|
13 |
+ * You should have received a copy of the GNU Lesser General Public |
|
14 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
15 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
16 |
+ */ |
|
17 |
+ |
|
18 |
+/** |
|
19 |
+ * @file |
|
20 |
+ * audio volume filter |
|
21 |
+ */ |
|
22 |
+ |
|
23 |
+#ifndef AVFILTER_AF_VOLUME_H |
|
24 |
+#define AVFILTER_AF_VOLUME_H |
|
25 |
+ |
|
26 |
+#include "libavutil/common.h" |
|
27 |
+#include "libavutil/float_dsp.h" |
|
28 |
+#include "libavutil/opt.h" |
|
29 |
+#include "libavutil/samplefmt.h" |
|
30 |
+ |
|
31 |
+enum PrecisionType { |
|
32 |
+ PRECISION_FIXED = 0, |
|
33 |
+ PRECISION_FLOAT, |
|
34 |
+ PRECISION_DOUBLE, |
|
35 |
+}; |
|
36 |
+ |
|
37 |
+typedef struct VolumeContext { |
|
38 |
+ const AVClass *class; |
|
39 |
+ AVFloatDSPContext fdsp; |
|
40 |
+ enum PrecisionType precision; |
|
41 |
+ double volume; |
|
42 |
+ int volume_i; |
|
43 |
+ int channels; |
|
44 |
+ int planes; |
|
45 |
+ enum AVSampleFormat sample_fmt; |
|
46 |
+ |
|
47 |
+ void (*scale_samples)(uint8_t *dst, const uint8_t *src, int nb_samples, |
|
48 |
+ int volume); |
|
49 |
+ int samples_align; |
|
50 |
+} VolumeContext; |
|
51 |
+ |
|
52 |
+#endif /* AVFILTER_AF_VOLUME_H */ |
0 | 53 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,314 @@ |
0 |
+/* |
|
1 |
+ * Copyright (c) 2011 Stefano Sabatini |
|
2 |
+ * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com> |
|
3 |
+ * |
|
4 |
+ * This file is part of FFmpeg. |
|
5 |
+ * |
|
6 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
7 |
+ * modify it under the terms of the GNU Lesser General Public |
|
8 |
+ * License as published by the Free Software Foundation; either |
|
9 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
10 |
+ * |
|
11 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
12 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
+ * Lesser General Public License for more details. |
|
15 |
+ * |
|
16 |
+ * You should have received a copy of the GNU Lesser General Public |
|
17 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
18 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
19 |
+ */ |
|
20 |
+ |
|
21 |
+/** |
|
22 |
+ * @file |
|
23 |
+ * audio volume filter |
|
24 |
+ */ |
|
25 |
+ |
|
26 |
+#include "libavutil/audioconvert.h" |
|
27 |
+#include "libavutil/common.h" |
|
28 |
+#include "libavutil/eval.h" |
|
29 |
+#include "libavutil/float_dsp.h" |
|
30 |
+#include "libavutil/opt.h" |
|
31 |
+#include "audio.h" |
|
32 |
+#include "avfilter.h" |
|
33 |
+#include "formats.h" |
|
34 |
+#include "internal.h" |
|
35 |
+#include "af_volume.h" |
|
36 |
+ |
|
37 |
+static const char *precision_str[] = { |
|
38 |
+ "fixed", "float", "double" |
|
39 |
+}; |
|
40 |
+ |
|
41 |
+#define OFFSET(x) offsetof(VolumeContext, x) |
|
42 |
+#define A AV_OPT_FLAG_AUDIO_PARAM |
|
43 |
+ |
|
44 |
+static const AVOption options[] = { |
|
45 |
+ { "volume", "Volume adjustment.", |
|
46 |
+ OFFSET(volume), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 }, 0, 0x7fffff, A }, |
|
47 |
+ { "precision", "Mathematical precision.", |
|
48 |
+ OFFSET(precision), AV_OPT_TYPE_INT, { .i64 = PRECISION_FLOAT }, PRECISION_FIXED, PRECISION_DOUBLE, A, "precision" }, |
|
49 |
+ { "fixed", "8-bit fixed-point.", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FIXED }, INT_MIN, INT_MAX, A, "precision" }, |
|
50 |
+ { "float", "32-bit floating-point.", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_FLOAT }, INT_MIN, INT_MAX, A, "precision" }, |
|
51 |
+ { "double", "64-bit floating-point.", 0, AV_OPT_TYPE_CONST, { .i64 = PRECISION_DOUBLE }, INT_MIN, INT_MAX, A, "precision" }, |
|
52 |
+ { NULL }, |
|
53 |
+}; |
|
54 |
+ |
|
55 |
+static const AVClass volume_class = { |
|
56 |
+ .class_name = "volume filter", |
|
57 |
+ .item_name = av_default_item_name, |
|
58 |
+ .option = options, |
|
59 |
+ .version = LIBAVUTIL_VERSION_INT, |
|
60 |
+}; |
|
61 |
+ |
|
62 |
+static av_cold int init(AVFilterContext *ctx, const char *args) |
|
63 |
+{ |
|
64 |
+ VolumeContext *vol = ctx->priv; |
|
65 |
+ int ret; |
|
66 |
+ |
|
67 |
+ vol->class = &volume_class; |
|
68 |
+ av_opt_set_defaults(vol); |
|
69 |
+ |
|
70 |
+ if ((ret = av_set_options_string(vol, args, "=", ":")) < 0) { |
|
71 |
+ av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args); |
|
72 |
+ return ret; |
|
73 |
+ } |
|
74 |
+ |
|
75 |
+ if (vol->precision == PRECISION_FIXED) { |
|
76 |
+ vol->volume_i = (int)(vol->volume * 256 + 0.5); |
|
77 |
+ vol->volume = vol->volume_i / 256.0; |
|
78 |
+ av_log(ctx, AV_LOG_VERBOSE, "volume:(%d/256)(%f)(%1.2fdB) precision:fixed\n", |
|
79 |
+ vol->volume_i, vol->volume, 20.0*log(vol->volume)/M_LN10); |
|
80 |
+ } else { |
|
81 |
+ av_log(ctx, AV_LOG_VERBOSE, "volume:(%f)(%1.2fdB) precision:%s\n", |
|
82 |
+ vol->volume, 20.0*log(vol->volume)/M_LN10, |
|
83 |
+ precision_str[vol->precision]); |
|
84 |
+ } |
|
85 |
+ |
|
86 |
+ av_opt_free(vol); |
|
87 |
+ return ret; |
|
88 |
+} |
|
89 |
+ |
|
90 |
+static int query_formats(AVFilterContext *ctx) |
|
91 |
+{ |
|
92 |
+ VolumeContext *vol = ctx->priv; |
|
93 |
+ AVFilterFormats *formats = NULL; |
|
94 |
+ AVFilterChannelLayouts *layouts; |
|
95 |
+ static const enum AVSampleFormat sample_fmts[][7] = { |
|
96 |
+ /* PRECISION_FIXED */ |
|
97 |
+ { |
|
98 |
+ AV_SAMPLE_FMT_U8, |
|
99 |
+ AV_SAMPLE_FMT_U8P, |
|
100 |
+ AV_SAMPLE_FMT_S16, |
|
101 |
+ AV_SAMPLE_FMT_S16P, |
|
102 |
+ AV_SAMPLE_FMT_S32, |
|
103 |
+ AV_SAMPLE_FMT_S32P, |
|
104 |
+ AV_SAMPLE_FMT_NONE |
|
105 |
+ }, |
|
106 |
+ /* PRECISION_FLOAT */ |
|
107 |
+ { |
|
108 |
+ AV_SAMPLE_FMT_FLT, |
|
109 |
+ AV_SAMPLE_FMT_FLTP, |
|
110 |
+ AV_SAMPLE_FMT_NONE |
|
111 |
+ }, |
|
112 |
+ /* PRECISION_DOUBLE */ |
|
113 |
+ { |
|
114 |
+ AV_SAMPLE_FMT_DBL, |
|
115 |
+ AV_SAMPLE_FMT_DBLP, |
|
116 |
+ AV_SAMPLE_FMT_NONE |
|
117 |
+ } |
|
118 |
+ }; |
|
119 |
+ |
|
120 |
+ layouts = ff_all_channel_layouts(); |
|
121 |
+ if (!layouts) |
|
122 |
+ return AVERROR(ENOMEM); |
|
123 |
+ ff_set_common_channel_layouts(ctx, layouts); |
|
124 |
+ |
|
125 |
+ formats = ff_make_format_list(sample_fmts[vol->precision]); |
|
126 |
+ if (!formats) |
|
127 |
+ return AVERROR(ENOMEM); |
|
128 |
+ ff_set_common_formats(ctx, formats); |
|
129 |
+ |
|
130 |
+ formats = ff_all_samplerates(); |
|
131 |
+ if (!formats) |
|
132 |
+ return AVERROR(ENOMEM); |
|
133 |
+ ff_set_common_samplerates(ctx, formats); |
|
134 |
+ |
|
135 |
+ return 0; |
|
136 |
+} |
|
137 |
+ |
|
138 |
+static inline void scale_samples_u8(uint8_t *dst, const uint8_t *src, |
|
139 |
+ int nb_samples, int volume) |
|
140 |
+{ |
|
141 |
+ int i; |
|
142 |
+ for (i = 0; i < nb_samples; i++) |
|
143 |
+ dst[i] = av_clip_uint8(((((int64_t)src[i] - 128) * volume + 128) >> 8) + 128); |
|
144 |
+} |
|
145 |
+ |
|
146 |
+static inline void scale_samples_u8_small(uint8_t *dst, const uint8_t *src, |
|
147 |
+ int nb_samples, int volume) |
|
148 |
+{ |
|
149 |
+ int i; |
|
150 |
+ for (i = 0; i < nb_samples; i++) |
|
151 |
+ dst[i] = av_clip_uint8((((src[i] - 128) * volume + 128) >> 8) + 128); |
|
152 |
+} |
|
153 |
+ |
|
154 |
+static inline void scale_samples_s16(uint8_t *dst, const uint8_t *src, |
|
155 |
+ int nb_samples, int volume) |
|
156 |
+{ |
|
157 |
+ int i; |
|
158 |
+ int16_t *smp_dst = (int16_t *)dst; |
|
159 |
+ const int16_t *smp_src = (const int16_t *)src; |
|
160 |
+ for (i = 0; i < nb_samples; i++) |
|
161 |
+ smp_dst[i] = av_clip_int16(((int64_t)smp_src[i] * volume + 128) >> 8); |
|
162 |
+} |
|
163 |
+ |
|
164 |
+static inline void scale_samples_s16_small(uint8_t *dst, const uint8_t *src, |
|
165 |
+ int nb_samples, int volume) |
|
166 |
+{ |
|
167 |
+ int i; |
|
168 |
+ int16_t *smp_dst = (int16_t *)dst; |
|
169 |
+ const int16_t *smp_src = (const int16_t *)src; |
|
170 |
+ for (i = 0; i < nb_samples; i++) |
|
171 |
+ smp_dst[i] = av_clip_int16((smp_src[i] * volume + 128) >> 8); |
|
172 |
+} |
|
173 |
+ |
|
174 |
+static inline void scale_samples_s32(uint8_t *dst, const uint8_t *src, |
|
175 |
+ int nb_samples, int volume) |
|
176 |
+{ |
|
177 |
+ int i; |
|
178 |
+ int32_t *smp_dst = (int32_t *)dst; |
|
179 |
+ const int32_t *smp_src = (const int32_t *)src; |
|
180 |
+ for (i = 0; i < nb_samples; i++) |
|
181 |
+ smp_dst[i] = av_clipl_int32((((int64_t)smp_src[i] * volume + 128) >> 8)); |
|
182 |
+} |
|
183 |
+ |
|
184 |
+ |
|
185 |
+ |
|
186 |
+static void volume_init(VolumeContext *vol) |
|
187 |
+{ |
|
188 |
+ vol->samples_align = 1; |
|
189 |
+ |
|
190 |
+ switch (av_get_packed_sample_fmt(vol->sample_fmt)) { |
|
191 |
+ case AV_SAMPLE_FMT_U8: |
|
192 |
+ if (vol->volume_i < 0x1000000) |
|
193 |
+ vol->scale_samples = scale_samples_u8_small; |
|
194 |
+ else |
|
195 |
+ vol->scale_samples = scale_samples_u8; |
|
196 |
+ break; |
|
197 |
+ case AV_SAMPLE_FMT_S16: |
|
198 |
+ if (vol->volume_i < 0x10000) |
|
199 |
+ vol->scale_samples = scale_samples_s16_small; |
|
200 |
+ else |
|
201 |
+ vol->scale_samples = scale_samples_s16; |
|
202 |
+ break; |
|
203 |
+ case AV_SAMPLE_FMT_S32: |
|
204 |
+ vol->scale_samples = scale_samples_s32; |
|
205 |
+ break; |
|
206 |
+ case AV_SAMPLE_FMT_FLT: |
|
207 |
+ avpriv_float_dsp_init(&vol->fdsp, 0); |
|
208 |
+ vol->samples_align = 4; |
|
209 |
+ break; |
|
210 |
+ case AV_SAMPLE_FMT_DBL: |
|
211 |
+ avpriv_float_dsp_init(&vol->fdsp, 0); |
|
212 |
+ vol->samples_align = 8; |
|
213 |
+ break; |
|
214 |
+ } |
|
215 |
+} |
|
216 |
+ |
|
217 |
+static int config_output(AVFilterLink *outlink) |
|
218 |
+{ |
|
219 |
+ AVFilterContext *ctx = outlink->src; |
|
220 |
+ VolumeContext *vol = ctx->priv; |
|
221 |
+ AVFilterLink *inlink = ctx->inputs[0]; |
|
222 |
+ |
|
223 |
+ vol->sample_fmt = inlink->format; |
|
224 |
+ vol->channels = av_get_channel_layout_nb_channels(inlink->channel_layout); |
|
225 |
+ vol->planes = av_sample_fmt_is_planar(inlink->format) ? vol->channels : 1; |
|
226 |
+ |
|
227 |
+ volume_init(vol); |
|
228 |
+ |
|
229 |
+ return 0; |
|
230 |
+} |
|
231 |
+ |
|
232 |
+static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf) |
|
233 |
+{ |
|
234 |
+ VolumeContext *vol = inlink->dst->priv; |
|
235 |
+ AVFilterLink *outlink = inlink->dst->outputs[0]; |
|
236 |
+ int nb_samples = buf->audio->nb_samples; |
|
237 |
+ AVFilterBufferRef *out_buf; |
|
238 |
+ |
|
239 |
+ if (vol->volume == 1.0 || vol->volume_i == 256) |
|
240 |
+ return ff_filter_frame(outlink, buf); |
|
241 |
+ |
|
242 |
+ /* do volume scaling in-place if input buffer is writable */ |
|
243 |
+ if (buf->perms & AV_PERM_WRITE) { |
|
244 |
+ out_buf = buf; |
|
245 |
+ } else { |
|
246 |
+ out_buf = ff_get_audio_buffer(inlink, AV_PERM_WRITE, nb_samples); |
|
247 |
+ if (!out_buf) |
|
248 |
+ return AVERROR(ENOMEM); |
|
249 |
+ out_buf->pts = buf->pts; |
|
250 |
+ } |
|
251 |
+ |
|
252 |
+ if (vol->precision != PRECISION_FIXED || vol->volume_i > 0) { |
|
253 |
+ int p, plane_samples; |
|
254 |
+ |
|
255 |
+ if (av_sample_fmt_is_planar(buf->format)) |
|
256 |
+ plane_samples = FFALIGN(nb_samples, vol->samples_align); |
|
257 |
+ else |
|
258 |
+ plane_samples = FFALIGN(nb_samples * vol->channels, vol->samples_align); |
|
259 |
+ |
|
260 |
+ if (vol->precision == PRECISION_FIXED) { |
|
261 |
+ for (p = 0; p < vol->planes; p++) { |
|
262 |
+ vol->scale_samples(out_buf->extended_data[p], |
|
263 |
+ buf->extended_data[p], plane_samples, |
|
264 |
+ vol->volume_i); |
|
265 |
+ } |
|
266 |
+ } else if (av_get_packed_sample_fmt(vol->sample_fmt) == AV_SAMPLE_FMT_FLT) { |
|
267 |
+ for (p = 0; p < vol->planes; p++) { |
|
268 |
+ vol->fdsp.vector_fmul_scalar((float *)out_buf->extended_data[p], |
|
269 |
+ (const float *)buf->extended_data[p], |
|
270 |
+ vol->volume, plane_samples); |
|
271 |
+ } |
|
272 |
+ } else { |
|
273 |
+ for (p = 0; p < vol->planes; p++) { |
|
274 |
+ vol->fdsp.vector_dmul_scalar((double *)out_buf->extended_data[p], |
|
275 |
+ (const double *)buf->extended_data[p], |
|
276 |
+ vol->volume, plane_samples); |
|
277 |
+ } |
|
278 |
+ } |
|
279 |
+ } |
|
280 |
+ |
|
281 |
+ if (buf != out_buf) |
|
282 |
+ avfilter_unref_buffer(buf); |
|
283 |
+ |
|
284 |
+ return ff_filter_frame(outlink, out_buf); |
|
285 |
+} |
|
286 |
+ |
|
287 |
+static const AVFilterPad avfilter_af_volume_inputs[] = { |
|
288 |
+ { |
|
289 |
+ .name = "default", |
|
290 |
+ .type = AVMEDIA_TYPE_AUDIO, |
|
291 |
+ .filter_frame = filter_frame, |
|
292 |
+ }, |
|
293 |
+ { NULL } |
|
294 |
+}; |
|
295 |
+ |
|
296 |
+static const AVFilterPad avfilter_af_volume_outputs[] = { |
|
297 |
+ { |
|
298 |
+ .name = "default", |
|
299 |
+ .type = AVMEDIA_TYPE_AUDIO, |
|
300 |
+ .config_props = config_output, |
|
301 |
+ }, |
|
302 |
+ { NULL } |
|
303 |
+}; |
|
304 |
+ |
|
305 |
+AVFilter avfilter_af_volume_justin = { |
|
306 |
+ .name = "volume_justin", |
|
307 |
+ .description = NULL_IF_CONFIG_SMALL("Change input volume."), |
|
308 |
+ .query_formats = query_formats, |
|
309 |
+ .priv_size = sizeof(VolumeContext), |
|
310 |
+ .init = init, |
|
311 |
+ .inputs = avfilter_af_volume_inputs, |
|
312 |
+ .outputs = avfilter_af_volume_outputs, |
|
313 |
+}; |
... | ... |
@@ -61,10 +61,11 @@ void avfilter_register_all(void) |
61 | 61 |
REGISTER_FILTER (EBUR128, ebur128, af); |
62 | 62 |
REGISTER_FILTER (JOIN, join, af); |
63 | 63 |
REGISTER_FILTER (PAN, pan, af); |
64 |
+ REGISTER_FILTER (RESAMPLE, resample, af); |
|
64 | 65 |
REGISTER_FILTER (SILENCEDETECT, silencedetect, af); |
65 | 66 |
REGISTER_FILTER (VOLUME, volume, af); |
67 |
+ REGISTER_FILTER (VOLUME_JUSTIN, volume_justin, af); |
|
66 | 68 |
REGISTER_FILTER (VOLUMEDETECT,volumedetect,af); |
67 |
- REGISTER_FILTER (RESAMPLE, resample, af); |
|
68 | 69 |
|
69 | 70 |
REGISTER_FILTER (AEVALSRC, aevalsrc, asrc); |
70 | 71 |
REGISTER_FILTER (ANULLSRC, anullsrc, asrc); |
... | ... |
@@ -29,7 +29,7 @@ |
29 | 29 |
#include "libavutil/avutil.h" |
30 | 30 |
|
31 | 31 |
#define LIBAVFILTER_VERSION_MAJOR 3 |
32 |
-#define LIBAVFILTER_VERSION_MINOR 24 |
|
32 |
+#define LIBAVFILTER_VERSION_MINOR 25 |
|
33 | 33 |
#define LIBAVFILTER_VERSION_MICRO 100 |
34 | 34 |
|
35 | 35 |
#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ |