| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2011 Stefano Sabatini | ||
| 3 | * Copyright (c) 2011 Mina Nagy Zaki | ||
| 4 | * | ||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 7 | * FFmpeg is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with FFmpeg; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | /** | ||
| 23 | * @file | ||
| 24 | * resampling audio filter | ||
| 25 | */ | ||
| 26 | |||
| 27 | #include "libavutil/avstring.h" | ||
| 28 | #include "libavutil/channel_layout.h" | ||
| 29 | #include "libavutil/downmix_info.h" | ||
| 30 | #include "libavutil/opt.h" | ||
| 31 | #include "libavutil/samplefmt.h" | ||
| 32 | #include "libavutil/avassert.h" | ||
| 33 | #include "libswresample/swresample.h" | ||
| 34 | #include "avfilter.h" | ||
| 35 | #include "audio.h" | ||
| 36 | #include "filters.h" | ||
| 37 | #include "formats.h" | ||
| 38 | |||
| 39 | typedef struct AResampleContext { | ||
| 40 | const AVClass *class; | ||
| 41 | int sample_rate_arg; | ||
| 42 | double ratio; | ||
| 43 | struct SwrContext *swr; | ||
| 44 | int64_t next_pts; | ||
| 45 | int more_data; | ||
| 46 | } AResampleContext; | ||
| 47 | |||
| 48 | 2399 | static av_cold int preinit(AVFilterContext *ctx) | |
| 49 | { | ||
| 50 | 2399 | AResampleContext *aresample = ctx->priv; | |
| 51 | |||
| 52 | 2399 | aresample->next_pts = AV_NOPTS_VALUE; | |
| 53 | 2399 | aresample->swr = swr_alloc(); | |
| 54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2399 times.
|
2399 | if (!aresample->swr) |
| 55 | ✗ | return AVERROR(ENOMEM); | |
| 56 | |||
| 57 | 2399 | return 0; | |
| 58 | } | ||
| 59 | |||
| 60 | 2399 | static av_cold void uninit(AVFilterContext *ctx) | |
| 61 | { | ||
| 62 | 2399 | AResampleContext *aresample = ctx->priv; | |
| 63 | 2399 | swr_free(&aresample->swr); | |
| 64 | 2399 | } | |
| 65 | |||
| 66 | 1534 | static int query_formats(const AVFilterContext *ctx, | |
| 67 | AVFilterFormatsConfig **cfg_in, | ||
| 68 | AVFilterFormatsConfig **cfg_out) | ||
| 69 | { | ||
| 70 | 1534 | const AResampleContext *aresample = ctx->priv; | |
| 71 | enum AVSampleFormat out_format; | ||
| 72 | 1534 | AVChannelLayout out_layout = { 0 }; | |
| 73 | int64_t out_rate; | ||
| 74 | |||
| 75 | AVFilterFormats *in_formats, *out_formats; | ||
| 76 | AVFilterFormats *in_samplerates, *out_samplerates; | ||
| 77 | AVFilterChannelLayouts *in_layouts, *out_layouts; | ||
| 78 | int ret; | ||
| 79 | |||
| 80 |
2/2✓ Branch 0 taken 640 times.
✓ Branch 1 taken 894 times.
|
1534 | if (aresample->sample_rate_arg > 0) |
| 81 | 640 | av_opt_set_int(aresample->swr, "osr", aresample->sample_rate_arg, 0); | |
| 82 | 1534 | av_opt_get_sample_fmt(aresample->swr, "osf", 0, &out_format); | |
| 83 | 1534 | av_opt_get_int(aresample->swr, "osr", 0, &out_rate); | |
| 84 | |||
| 85 | 1534 | in_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO); | |
| 86 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1534 times.
|
1534 | if ((ret = ff_formats_ref(in_formats, &cfg_in[0]->formats)) < 0) |
| 87 | ✗ | return ret; | |
| 88 | |||
| 89 | 1534 | in_samplerates = ff_all_samplerates(); | |
| 90 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1534 times.
|
1534 | if ((ret = ff_formats_ref(in_samplerates, &cfg_in[0]->samplerates)) < 0) |
| 91 | ✗ | return ret; | |
| 92 | |||
| 93 | 1534 | in_layouts = ff_all_channel_counts(); | |
| 94 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1534 times.
|
1534 | if ((ret = ff_channel_layouts_ref(in_layouts, &cfg_in[0]->channel_layouts)) < 0) |
| 95 | ✗ | return ret; | |
| 96 | |||
| 97 |
2/2✓ Branch 0 taken 640 times.
✓ Branch 1 taken 894 times.
|
1534 | if(out_rate > 0) { |
| 98 | 640 | int ratelist[] = { out_rate, -1 }; | |
| 99 | 640 | out_samplerates = ff_make_format_list(ratelist); | |
| 100 | } else { | ||
| 101 | 894 | out_samplerates = ff_all_samplerates(); | |
| 102 | } | ||
| 103 | |||
| 104 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1534 times.
|
1534 | if ((ret = ff_formats_ref(out_samplerates, &cfg_out[0]->samplerates)) < 0) |
| 105 | ✗ | return ret; | |
| 106 | |||
| 107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1534 times.
|
1534 | if(out_format != AV_SAMPLE_FMT_NONE) { |
| 108 | ✗ | enum AVSampleFormat formatlist[] = { out_format, AV_SAMPLE_FMT_NONE }; | |
| 109 | ✗ | out_formats = ff_make_sample_format_list(formatlist); | |
| 110 | } else | ||
| 111 | 1534 | out_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO); | |
| 112 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1534 times.
|
1534 | if ((ret = ff_formats_ref(out_formats, &cfg_out[0]->formats)) < 0) |
| 113 | ✗ | return ret; | |
| 114 | |||
| 115 | 1534 | av_opt_get_chlayout(aresample->swr, "ochl", 0, &out_layout); | |
| 116 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1534 times.
|
1534 | if (av_channel_layout_check(&out_layout)) { |
| 117 | ✗ | const AVChannelLayout layout_list[] = { out_layout, { 0 } }; | |
| 118 | ✗ | out_layouts = ff_make_channel_layout_list(layout_list); | |
| 119 | } else | ||
| 120 | 1534 | out_layouts = ff_all_channel_counts(); | |
| 121 | 1534 | av_channel_layout_uninit(&out_layout); | |
| 122 | |||
| 123 | 1534 | return ff_channel_layouts_ref(out_layouts, &cfg_out[0]->channel_layouts); | |
| 124 | } | ||
| 125 | |||
| 126 | #define SWR_CH_MAX 64 | ||
| 127 | |||
| 128 | 1534 | static int config_output(AVFilterLink *outlink) | |
| 129 | { | ||
| 130 | int ret; | ||
| 131 | 1534 | AVFilterContext *ctx = outlink->src; | |
| 132 | 1534 | AVFilterLink *inlink = ctx->inputs[0]; | |
| 133 | 1534 | AResampleContext *aresample = ctx->priv; | |
| 134 | 1534 | AVChannelLayout out_layout = { 0 }; | |
| 135 | int64_t out_rate; | ||
| 136 | const AVFrameSideData *sd; | ||
| 137 | enum AVSampleFormat out_format; | ||
| 138 | 1534 | enum AVMatrixEncoding matrix_encoding = AV_MATRIX_ENCODING_NONE; | |
| 139 | char inchl_buf[128], outchl_buf[128]; | ||
| 140 | |||
| 141 | 1534 | ret = swr_alloc_set_opts2(&aresample->swr, | |
| 142 | 1534 | &outlink->ch_layout, outlink->format, outlink->sample_rate, | |
| 143 | 1534 | &inlink->ch_layout, inlink->format, inlink->sample_rate, | |
| 144 | 0, ctx); | ||
| 145 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1534 times.
|
1534 | if (ret < 0) |
| 146 | ✗ | return ret; | |
| 147 | |||
| 148 | 1534 | sd = av_frame_side_data_get(inlink->side_data, inlink->nb_side_data, | |
| 149 | AV_FRAME_DATA_DOWNMIX_MATRIX); | ||
| 150 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1530 times.
|
1534 | if (sd) { |
| 151 | 4 | const AVDownmixMatrix *dm = (AVDownmixMatrix *)sd->data; | |
| 152 | |||
| 153 |
3/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
|
8 | if (inlink->ch_layout.nb_channels == dm->in_ch_count && |
| 154 | 4 | !av_channel_layout_compare(&outlink->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO)) { | |
| 155 | 2 | swr_set_matrix(aresample->swr, | |
| 156 | 2 | (double *)((uint8_t *)dm + dm->coeffs_offset), | |
| 157 | 2 | dm->in_ch_count); | |
| 158 | |||
| 159 |
1/3✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | switch (dm->downmix_type) { |
| 160 | ✗ | case AV_DOWNMIX_TYPE_LTRT: | |
| 161 | ✗ | matrix_encoding = AV_MATRIX_ENCODING_DOLBY; | |
| 162 | ✗ | break; | |
| 163 | ✗ | case AV_DOWNMIX_TYPE_DPLII: | |
| 164 | ✗ | matrix_encoding = AV_MATRIX_ENCODING_DPLII; | |
| 165 | ✗ | break; | |
| 166 | 2 | default: | |
| 167 | 2 | break; | |
| 168 | } | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | 1534 | sd = av_frame_side_data_get(inlink->side_data, inlink->nb_side_data, | |
| 173 | AV_FRAME_DATA_DOWNMIX_INFO); | ||
| 174 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1529 times.
|
1534 | if (sd) { |
| 175 | 5 | const AVDownmixInfo *di = (AVDownmixInfo *)sd->data; | |
| 176 | double center_mix_level, surround_mix_level; | ||
| 177 | |||
| 178 |
3/3✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
|
5 | switch (di->preferred_downmix_type) { |
| 179 | 1 | case AV_DOWNMIX_TYPE_LTRT: | |
| 180 | 1 | matrix_encoding = AV_MATRIX_ENCODING_DOLBY; | |
| 181 | 1 | center_mix_level = di->center_mix_level_ltrt; | |
| 182 | 1 | surround_mix_level = di->surround_mix_level_ltrt; | |
| 183 | 1 | break; | |
| 184 | 1 | case AV_DOWNMIX_TYPE_DPLII: | |
| 185 | 1 | matrix_encoding = AV_MATRIX_ENCODING_DPLII; | |
| 186 | 1 | center_mix_level = di->center_mix_level_ltrt; | |
| 187 | 1 | surround_mix_level = di->surround_mix_level_ltrt; | |
| 188 | 1 | break; | |
| 189 | 3 | default: | |
| 190 | 3 | center_mix_level = di->center_mix_level; | |
| 191 | 3 | surround_mix_level = di->surround_mix_level; | |
| 192 | 3 | break; | |
| 193 | } | ||
| 194 | |||
| 195 | // Don't use Dolby Surround compatible coeffs when not downmixing to stereo | ||
| 196 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | if (av_channel_layout_compare(&outlink->ch_layout, |
| 197 | 5 | &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO)) { | |
| 198 | 5 | matrix_encoding = AV_MATRIX_ENCODING_NONE; | |
| 199 | 5 | center_mix_level = di->center_mix_level; | |
| 200 | 5 | surround_mix_level = di->surround_mix_level; | |
| 201 | } | ||
| 202 | |||
| 203 | 5 | av_log(ctx, AV_LOG_VERBOSE, "Mix levels: center %f - " | |
| 204 | "surround %f - lfe %f.\n", | ||
| 205 | 5 | center_mix_level, surround_mix_level, di->lfe_mix_level); | |
| 206 | |||
| 207 | 5 | av_opt_set_double(aresample->swr, "clev", center_mix_level, 0); | |
| 208 | 5 | av_opt_set_double(aresample->swr, "slev", surround_mix_level, 0); | |
| 209 | 5 | av_opt_set_double(aresample->swr, "lfe_mix_level", di->lfe_mix_level, 0); | |
| 210 | 5 | av_opt_set_int(aresample->swr, "matrix_encoding", matrix_encoding, 0); | |
| 211 | } | ||
| 212 | |||
| 213 |
2/2✓ Branch 1 taken 15 times.
✓ Branch 2 taken 1519 times.
|
1534 | if (av_channel_layout_compare(&outlink->ch_layout, &inlink->ch_layout)) { |
| 214 | 15 | av_frame_side_data_remove_by_props(&outlink->side_data, &outlink->nb_side_data, | |
| 215 | AV_SIDE_DATA_PROP_CHANNEL_DEPENDENT); | ||
| 216 | |||
| 217 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (matrix_encoding != AV_MATRIX_ENCODING_NONE) { |
| 218 | ✗ | AVFrameSideData *side_data = av_frame_side_data_new(&outlink->side_data, &outlink->nb_side_data, | |
| 219 | AV_FRAME_DATA_MATRIXENCODING, | ||
| 220 | sizeof(matrix_encoding), 0); | ||
| 221 | |||
| 222 | ✗ | if (!side_data) | |
| 223 | ✗ | return AVERROR(ENOMEM); | |
| 224 | |||
| 225 | ✗ | *(enum AVMatrixEncoding *)side_data->data = matrix_encoding; | |
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | 1534 | ret = swr_init(aresample->swr); | |
| 230 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1534 times.
|
1534 | if (ret < 0) |
| 231 | ✗ | return ret; | |
| 232 | |||
| 233 | 1534 | av_opt_get_int(aresample->swr, "osr", 0, &out_rate); | |
| 234 | 1534 | av_opt_get_chlayout(aresample->swr, "ochl", 0, &out_layout); | |
| 235 | 1534 | av_opt_get_sample_fmt(aresample->swr, "osf", 0, &out_format); | |
| 236 | 1534 | outlink->time_base = (AVRational) {1, out_rate}; | |
| 237 | |||
| 238 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1534 times.
|
1534 | av_assert0(outlink->sample_rate == out_rate); |
| 239 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1534 times.
|
1534 | av_assert0(!av_channel_layout_compare(&outlink->ch_layout, &out_layout)); |
| 240 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1534 times.
|
1534 | av_assert0(outlink->format == out_format); |
| 241 | |||
| 242 | 1534 | av_channel_layout_uninit(&out_layout); | |
| 243 | |||
| 244 | 1534 | aresample->ratio = (double)outlink->sample_rate / inlink->sample_rate; | |
| 245 | |||
| 246 | 1534 | av_channel_layout_describe(&inlink ->ch_layout, inchl_buf, sizeof(inchl_buf)); | |
| 247 | 1534 | av_channel_layout_describe(&outlink->ch_layout, outchl_buf, sizeof(outchl_buf)); | |
| 248 | |||
| 249 | 1534 | av_log(ctx, AV_LOG_VERBOSE, "ch:%d chl:%s fmt:%s r:%dHz -> ch:%d chl:%s fmt:%s r:%dHz\n", | |
| 250 | 1534 | inlink ->ch_layout.nb_channels, inchl_buf, av_get_sample_fmt_name(inlink->format), inlink->sample_rate, | |
| 251 | 1534 | outlink->ch_layout.nb_channels, outchl_buf, av_get_sample_fmt_name(outlink->format), outlink->sample_rate); | |
| 252 | 1534 | return 0; | |
| 253 | } | ||
| 254 | |||
| 255 | 263783 | static int filter_frame(AVFilterLink *inlink, AVFrame *insamplesref, AVFrame **outsamplesref_ret) | |
| 256 | { | ||
| 257 | 263783 | AVFilterContext *ctx = inlink->dst; | |
| 258 | 263783 | AResampleContext *aresample = ctx->priv; | |
| 259 | 263783 | const int n_in = insamplesref->nb_samples; | |
| 260 | int64_t delay; | ||
| 261 | 263783 | int n_out = n_in * aresample->ratio + 32; | |
| 262 | 263783 | AVFilterLink *const outlink = inlink->dst->outputs[0]; | |
| 263 | AVFrame *outsamplesref; | ||
| 264 | int ret; | ||
| 265 | |||
| 266 | 263783 | *outsamplesref_ret = NULL; | |
| 267 | 263783 | delay = swr_get_delay(aresample->swr, outlink->sample_rate); | |
| 268 |
2/2✓ Branch 0 taken 7957 times.
✓ Branch 1 taken 255826 times.
|
263783 | if (delay > 0) |
| 269 | 7957 | n_out += FFMIN(delay, FFMAX(4096, n_out)); | |
| 270 | |||
| 271 | 263783 | outsamplesref = ff_get_audio_buffer(outlink, n_out); | |
| 272 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 263783 times.
|
263783 | if (!outsamplesref) |
| 273 | ✗ | return AVERROR(ENOMEM); | |
| 274 | |||
| 275 | 263783 | av_frame_copy_props(outsamplesref, insamplesref); | |
| 276 | 263783 | outsamplesref->format = outlink->format; | |
| 277 | 263783 | ret = av_channel_layout_copy(&outsamplesref->ch_layout, &outlink->ch_layout); | |
| 278 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 263783 times.
|
263783 | if (ret < 0) { |
| 279 | ✗ | av_frame_free(&outsamplesref); | |
| 280 | ✗ | return ret; | |
| 281 | } | ||
| 282 | 263783 | outsamplesref->sample_rate = outlink->sample_rate; | |
| 283 | |||
| 284 |
2/2✓ Branch 1 taken 543 times.
✓ Branch 2 taken 263240 times.
|
263783 | if (av_channel_layout_compare(&outsamplesref->ch_layout, &insamplesref->ch_layout)) |
| 285 | 543 | av_frame_side_data_remove_by_props(&outsamplesref->side_data, &outsamplesref->nb_side_data, | |
| 286 | AV_SIDE_DATA_PROP_CHANNEL_DEPENDENT); | ||
| 287 | |||
| 288 |
1/2✓ Branch 0 taken 263783 times.
✗ Branch 1 not taken.
|
263783 | if(insamplesref->pts != AV_NOPTS_VALUE) { |
| 289 | 263783 | int64_t inpts = av_rescale(insamplesref->pts, inlink->time_base.num * (int64_t)outlink->sample_rate * inlink->sample_rate, inlink->time_base.den); | |
| 290 | 263783 | int64_t outpts= swr_next_pts(aresample->swr, inpts); | |
| 291 | 263783 | aresample->next_pts = | |
| 292 |
2/2✓ Branch 0 taken 263699 times.
✓ Branch 1 taken 84 times.
|
263783 | outsamplesref->pts = ROUNDED_DIV(outpts, inlink->sample_rate); |
| 293 | } else { | ||
| 294 | ✗ | outsamplesref->pts = AV_NOPTS_VALUE; | |
| 295 | } | ||
| 296 | 263783 | n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out, | |
| 297 | 263783 | (void *)insamplesref->extended_data, n_in); | |
| 298 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 263783 times.
|
263783 | if (n_out <= 0) { |
| 299 | ✗ | av_frame_free(&outsamplesref); | |
| 300 | ✗ | return 0; | |
| 301 | } | ||
| 302 | |||
| 303 | 263783 | aresample->more_data = outsamplesref->nb_samples == n_out; // Indicate that there is probably more data in our buffers | |
| 304 | |||
| 305 | 263783 | outsamplesref->nb_samples = n_out; | |
| 306 | |||
| 307 | 263783 | *outsamplesref_ret = outsamplesref; | |
| 308 | 263783 | return 1; | |
| 309 | } | ||
| 310 | |||
| 311 | 2076 | static int flush_frame(AVFilterLink *outlink, int final, AVFrame **outsamplesref_ret) | |
| 312 | { | ||
| 313 | 2076 | AVFilterContext *ctx = outlink->src; | |
| 314 | 2076 | AResampleContext *aresample = ctx->priv; | |
| 315 | 2076 | AVFilterLink *const inlink = outlink->src->inputs[0]; | |
| 316 | AVFrame *outsamplesref; | ||
| 317 | 2076 | int n_out = 4096; | |
| 318 | int64_t pts; | ||
| 319 | |||
| 320 | 2076 | outsamplesref = ff_get_audio_buffer(outlink, n_out); | |
| 321 | 2076 | *outsamplesref_ret = outsamplesref; | |
| 322 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2076 times.
|
2076 | if (!outsamplesref) |
| 323 | ✗ | return AVERROR(ENOMEM); | |
| 324 | |||
| 325 | 2076 | pts = swr_next_pts(aresample->swr, INT64_MIN); | |
| 326 |
1/2✓ Branch 0 taken 2076 times.
✗ Branch 1 not taken.
|
2076 | pts = ROUNDED_DIV(pts, inlink->sample_rate); |
| 327 | |||
| 328 |
2/2✓ Branch 0 taken 127 times.
✓ Branch 1 taken 1949 times.
|
2076 | n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out, final ? NULL : (void*)outsamplesref->extended_data, 0); |
| 329 |
2/2✓ Branch 0 taken 1315 times.
✓ Branch 1 taken 761 times.
|
2076 | if (n_out <= 0) { |
| 330 | 1315 | av_frame_free(&outsamplesref); | |
| 331 | 1315 | return n_out; | |
| 332 | } | ||
| 333 | |||
| 334 | 761 | outsamplesref->sample_rate = outlink->sample_rate; | |
| 335 | 761 | outsamplesref->nb_samples = n_out; | |
| 336 | |||
| 337 | 761 | outsamplesref->pts = pts; | |
| 338 | |||
| 339 | 761 | return 1; | |
| 340 | } | ||
| 341 | |||
| 342 | 531891 | static int activate(AVFilterContext *ctx) | |
| 343 | { | ||
| 344 | 531891 | AVFilterLink *inlink = ctx->inputs[0]; | |
| 345 | 531891 | AVFilterLink *outlink = ctx->outputs[0]; | |
| 346 | 531891 | AResampleContext *aresample = ctx->priv; | |
| 347 | AVFrame *frame; | ||
| 348 | 531891 | int ret = 0, status; | |
| 349 | int64_t pts; | ||
| 350 | |||
| 351 |
2/2✓ Branch 1 taken 1140 times.
✓ Branch 2 taken 530751 times.
|
531891 | FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); |
| 352 | |||
| 353 | // First try to get data from the internal buffers | ||
| 354 |
2/2✓ Branch 0 taken 127 times.
✓ Branch 1 taken 530624 times.
|
530751 | if (aresample->more_data) { |
| 355 | AVFrame *outsamplesref; | ||
| 356 | |||
| 357 | 127 | ret = flush_frame(outlink, 0, &outsamplesref); | |
| 358 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 127 times.
|
127 | if (ret < 0) |
| 359 | 123 | return ret; | |
| 360 |
2/2✓ Branch 0 taken 123 times.
✓ Branch 1 taken 4 times.
|
127 | if (ret > 0) |
| 361 | 123 | return ff_filter_frame(outlink, outsamplesref); | |
| 362 | } | ||
| 363 | 530628 | aresample->more_data = 0; | |
| 364 | |||
| 365 | // Then consume frames from inlink | ||
| 366 |
2/2✓ Branch 1 taken 263783 times.
✓ Branch 2 taken 266845 times.
|
530628 | while ((ret = ff_inlink_consume_frame(inlink, &frame))) { |
| 367 | AVFrame *outsamplesref; | ||
| 368 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 263783 times.
|
263783 | if (ret < 0) |
| 369 | 263783 | return ret; | |
| 370 | |||
| 371 | 263783 | ret = filter_frame(inlink, frame, &outsamplesref); | |
| 372 | 263783 | av_frame_free(&frame); | |
| 373 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 263783 times.
|
263783 | if (ret < 0) |
| 374 | ✗ | return ret; | |
| 375 |
1/2✓ Branch 0 taken 263783 times.
✗ Branch 1 not taken.
|
263783 | if (ret > 0) |
| 376 | 263783 | return ff_filter_frame(outlink, outsamplesref); | |
| 377 | } | ||
| 378 | |||
| 379 | // If we hit the end flush | ||
| 380 |
2/2✓ Branch 1 taken 1949 times.
✓ Branch 2 taken 264896 times.
|
266845 | if (ff_inlink_acknowledge_status(inlink, &status, &pts)) { |
| 381 | AVFrame *outsamplesref; | ||
| 382 | |||
| 383 | 1949 | ret = flush_frame(outlink, 1, &outsamplesref); | |
| 384 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1949 times.
|
1949 | if (ret < 0) |
| 385 | ✗ | return ret; | |
| 386 |
2/2✓ Branch 0 taken 638 times.
✓ Branch 1 taken 1311 times.
|
1949 | if (ret > 0) |
| 387 | 638 | return ff_filter_frame(outlink, outsamplesref); | |
| 388 | 1311 | ff_outlink_set_status(outlink, status, aresample->next_pts); | |
| 389 | 1311 | return 0; | |
| 390 | } | ||
| 391 | |||
| 392 | // If not, request more data from the input | ||
| 393 |
1/2✓ Branch 1 taken 264896 times.
✗ Branch 2 not taken.
|
264896 | FF_FILTER_FORWARD_WANTED(outlink, inlink); |
| 394 | |||
| 395 | ✗ | return FFERROR_NOT_READY; | |
| 396 | } | ||
| 397 | |||
| 398 | ✗ | static const AVClass *resample_child_class_iterate(void **iter) | |
| 399 | { | ||
| 400 | ✗ | const AVClass *c = *iter ? NULL : swr_get_class(); | |
| 401 | ✗ | *iter = (void*)(uintptr_t)c; | |
| 402 | ✗ | return c; | |
| 403 | } | ||
| 404 | |||
| 405 | 6693 | static void *resample_child_next(void *obj, void *prev) | |
| 406 | { | ||
| 407 | 6693 | AResampleContext *s = obj; | |
| 408 |
2/2✓ Branch 0 taken 5413 times.
✓ Branch 1 taken 1280 times.
|
6693 | return prev ? NULL : s->swr; |
| 409 | } | ||
| 410 | |||
| 411 | #define OFFSET(x) offsetof(AResampleContext, x) | ||
| 412 | #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM | ||
| 413 | |||
| 414 | static const AVOption options[] = { | ||
| 415 | {"sample_rate", NULL, OFFSET(sample_rate_arg), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS }, | ||
| 416 | {NULL} | ||
| 417 | }; | ||
| 418 | |||
| 419 | static const AVClass aresample_class = { | ||
| 420 | .class_name = "aresample", | ||
| 421 | .item_name = av_default_item_name, | ||
| 422 | .option = options, | ||
| 423 | .version = LIBAVUTIL_VERSION_INT, | ||
| 424 | .child_class_iterate = resample_child_class_iterate, | ||
| 425 | .child_next = resample_child_next, | ||
| 426 | }; | ||
| 427 | |||
| 428 | static const AVFilterPad aresample_outputs[] = { | ||
| 429 | { | ||
| 430 | .name = "default", | ||
| 431 | .config_props = config_output, | ||
| 432 | .type = AVMEDIA_TYPE_AUDIO, | ||
| 433 | }, | ||
| 434 | }; | ||
| 435 | |||
| 436 | const FFFilter ff_af_aresample = { | ||
| 437 | .p.name = "aresample", | ||
| 438 | .p.description = NULL_IF_CONFIG_SMALL("Resample audio data."), | ||
| 439 | .p.priv_class = &aresample_class, | ||
| 440 | .preinit = preinit, | ||
| 441 | .activate = activate, | ||
| 442 | .uninit = uninit, | ||
| 443 | .priv_size = sizeof(AResampleContext), | ||
| 444 | FILTER_INPUTS(ff_audio_default_filterpad), | ||
| 445 | FILTER_OUTPUTS(aresample_outputs), | ||
| 446 | FILTER_QUERY_FUNC2(query_formats), | ||
| 447 | }; | ||
| 448 |