| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2018 Paul B Mahol | ||
| 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 | #include "libavutil/channel_layout.h" | ||
| 22 | #include "libavutil/common.h" | ||
| 23 | #include "libavutil/float_dsp.h" | ||
| 24 | #include "libavutil/mem.h" | ||
| 25 | |||
| 26 | #include "audio.h" | ||
| 27 | #include "avfilter.h" | ||
| 28 | #include "filters.h" | ||
| 29 | |||
| 30 | typedef struct AudioMultiplyContext { | ||
| 31 | const AVClass *class; | ||
| 32 | |||
| 33 | AVFrame *frames[2]; | ||
| 34 | int planes; | ||
| 35 | int channels; | ||
| 36 | int samples_align; | ||
| 37 | |||
| 38 | AVFloatDSPContext *fdsp; | ||
| 39 | } AudioMultiplyContext; | ||
| 40 | |||
| 41 | ✗ | static int activate(AVFilterContext *ctx) | |
| 42 | { | ||
| 43 | ✗ | AudioMultiplyContext *s = ctx->priv; | |
| 44 | int i, ret, status; | ||
| 45 | int nb_samples; | ||
| 46 | int64_t pts; | ||
| 47 | |||
| 48 | ✗ | FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx); | |
| 49 | |||
| 50 | ✗ | nb_samples = FFMIN(ff_inlink_queued_samples(ctx->inputs[0]), | |
| 51 | ff_inlink_queued_samples(ctx->inputs[1])); | ||
| 52 | ✗ | for (i = 0; i < ctx->nb_inputs && nb_samples > 0; i++) { | |
| 53 | ✗ | if (s->frames[i]) | |
| 54 | ✗ | continue; | |
| 55 | |||
| 56 | ✗ | if (ff_inlink_check_available_samples(ctx->inputs[i], nb_samples) > 0) { | |
| 57 | ✗ | ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->frames[i]); | |
| 58 | ✗ | if (ret < 0) | |
| 59 | ✗ | return ret; | |
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | ✗ | if (s->frames[0] && s->frames[1]) { | |
| 64 | AVFrame *out; | ||
| 65 | int plane_samples; | ||
| 66 | |||
| 67 | ✗ | if (av_sample_fmt_is_planar(ctx->inputs[0]->format)) | |
| 68 | ✗ | plane_samples = FFALIGN(s->frames[0]->nb_samples, s->samples_align); | |
| 69 | else | ||
| 70 | ✗ | plane_samples = FFALIGN(s->frames[0]->nb_samples * s->channels, s->samples_align); | |
| 71 | |||
| 72 | ✗ | out = ff_get_audio_buffer(ctx->outputs[0], s->frames[0]->nb_samples); | |
| 73 | ✗ | if (!out) | |
| 74 | ✗ | return AVERROR(ENOMEM); | |
| 75 | |||
| 76 | ✗ | out->pts = s->frames[0]->pts; | |
| 77 | ✗ | out->duration = s->frames[0]->duration; | |
| 78 | |||
| 79 | ✗ | if (av_get_packed_sample_fmt(ctx->inputs[0]->format) == AV_SAMPLE_FMT_FLT) { | |
| 80 | ✗ | for (i = 0; i < s->planes; i++) { | |
| 81 | ✗ | s->fdsp->vector_fmul((float *)out->extended_data[i], | |
| 82 | ✗ | (const float *)s->frames[0]->extended_data[i], | |
| 83 | ✗ | (const float *)s->frames[1]->extended_data[i], | |
| 84 | plane_samples); | ||
| 85 | } | ||
| 86 | } else { | ||
| 87 | ✗ | for (i = 0; i < s->planes; i++) { | |
| 88 | ✗ | s->fdsp->vector_dmul((double *)out->extended_data[i], | |
| 89 | ✗ | (const double *)s->frames[0]->extended_data[i], | |
| 90 | ✗ | (const double *)s->frames[1]->extended_data[i], | |
| 91 | plane_samples); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | ✗ | av_frame_free(&s->frames[0]); | |
| 96 | ✗ | av_frame_free(&s->frames[1]); | |
| 97 | |||
| 98 | ✗ | ret = ff_filter_frame(ctx->outputs[0], out); | |
| 99 | ✗ | if (ret < 0) | |
| 100 | ✗ | return ret; | |
| 101 | } | ||
| 102 | |||
| 103 | ✗ | if (!nb_samples) { | |
| 104 | ✗ | for (i = 0; i < 2; i++) { | |
| 105 | ✗ | if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) { | |
| 106 | ✗ | ff_outlink_set_status(ctx->outputs[0], status, pts); | |
| 107 | ✗ | return 0; | |
| 108 | } | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | ✗ | if (ff_outlink_frame_wanted(ctx->outputs[0])) { | |
| 113 | ✗ | for (i = 0; i < 2; i++) { | |
| 114 | ✗ | if (s->frames[i] || ff_inlink_queued_samples(ctx->inputs[i]) > 0) | |
| 115 | ✗ | continue; | |
| 116 | ✗ | ff_inlink_request_frame(ctx->inputs[i]); | |
| 117 | ✗ | return 0; | |
| 118 | } | ||
| 119 | } | ||
| 120 | ✗ | return 0; | |
| 121 | } | ||
| 122 | |||
| 123 | ✗ | static int config_output(AVFilterLink *outlink) | |
| 124 | { | ||
| 125 | ✗ | AVFilterContext *ctx = outlink->src; | |
| 126 | ✗ | AudioMultiplyContext *s = ctx->priv; | |
| 127 | ✗ | AVFilterLink *inlink = ctx->inputs[0]; | |
| 128 | |||
| 129 | ✗ | s->channels = inlink->ch_layout.nb_channels; | |
| 130 | ✗ | s->planes = av_sample_fmt_is_planar(inlink->format) ? inlink->ch_layout.nb_channels : 1; | |
| 131 | ✗ | s->samples_align = 16; | |
| 132 | |||
| 133 | ✗ | return 0; | |
| 134 | } | ||
| 135 | |||
| 136 | ✗ | static av_cold int init(AVFilterContext *ctx) | |
| 137 | { | ||
| 138 | ✗ | AudioMultiplyContext *s = ctx->priv; | |
| 139 | |||
| 140 | ✗ | s->fdsp = avpriv_float_dsp_alloc(0); | |
| 141 | ✗ | if (!s->fdsp) | |
| 142 | ✗ | return AVERROR(ENOMEM); | |
| 143 | |||
| 144 | ✗ | return 0; | |
| 145 | } | ||
| 146 | |||
| 147 | ✗ | static av_cold void uninit(AVFilterContext *ctx) | |
| 148 | { | ||
| 149 | ✗ | AudioMultiplyContext *s = ctx->priv; | |
| 150 | ✗ | av_freep(&s->fdsp); | |
| 151 | ✗ | } | |
| 152 | |||
| 153 | static const AVFilterPad inputs[] = { | ||
| 154 | { | ||
| 155 | .name = "multiply0", | ||
| 156 | .type = AVMEDIA_TYPE_AUDIO, | ||
| 157 | }, | ||
| 158 | { | ||
| 159 | .name = "multiply1", | ||
| 160 | .type = AVMEDIA_TYPE_AUDIO, | ||
| 161 | }, | ||
| 162 | }; | ||
| 163 | |||
| 164 | static const AVFilterPad outputs[] = { | ||
| 165 | { | ||
| 166 | .name = "default", | ||
| 167 | .type = AVMEDIA_TYPE_AUDIO, | ||
| 168 | .config_props = config_output, | ||
| 169 | }, | ||
| 170 | }; | ||
| 171 | |||
| 172 | const FFFilter ff_af_amultiply = { | ||
| 173 | .p.name = "amultiply", | ||
| 174 | .p.description = NULL_IF_CONFIG_SMALL("Multiply two audio streams."), | ||
| 175 | .priv_size = sizeof(AudioMultiplyContext), | ||
| 176 | .init = init, | ||
| 177 | .uninit = uninit, | ||
| 178 | .activate = activate, | ||
| 179 | FILTER_INPUTS(inputs), | ||
| 180 | FILTER_OUTPUTS(outputs), | ||
| 181 | FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP, | ||
| 182 | AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP), | ||
| 183 | }; | ||
| 184 |