00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00025 #include "libavutil/avassert.h"
00026 #include "libavutil/avstring.h"
00027 #include "libavutil/common.h"
00028 #include "libavutil/mathematics.h"
00029 #include "libavutil/opt.h"
00030
00031 #include "libavresample/avresample.h"
00032
00033 #include "audio.h"
00034 #include "avfilter.h"
00035 #include "formats.h"
00036 #include "internal.h"
00037
00038 typedef struct ResampleContext {
00039 AVAudioResampleContext *avr;
00040
00041 int64_t next_pts;
00042
00043
00044 int got_output;
00045 } ResampleContext;
00046
00047 static av_cold void uninit(AVFilterContext *ctx)
00048 {
00049 ResampleContext *s = ctx->priv;
00050
00051 if (s->avr) {
00052 avresample_close(s->avr);
00053 avresample_free(&s->avr);
00054 }
00055 }
00056
00057 static int query_formats(AVFilterContext *ctx)
00058 {
00059 AVFilterLink *inlink = ctx->inputs[0];
00060 AVFilterLink *outlink = ctx->outputs[0];
00061
00062 AVFilterFormats *in_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
00063 AVFilterFormats *out_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
00064 AVFilterFormats *in_samplerates = ff_all_samplerates();
00065 AVFilterFormats *out_samplerates = ff_all_samplerates();
00066 AVFilterChannelLayouts *in_layouts = ff_all_channel_layouts();
00067 AVFilterChannelLayouts *out_layouts = ff_all_channel_layouts();
00068
00069 ff_formats_ref(in_formats, &inlink->out_formats);
00070 ff_formats_ref(out_formats, &outlink->in_formats);
00071
00072 ff_formats_ref(in_samplerates, &inlink->out_samplerates);
00073 ff_formats_ref(out_samplerates, &outlink->in_samplerates);
00074
00075 ff_channel_layouts_ref(in_layouts, &inlink->out_channel_layouts);
00076 ff_channel_layouts_ref(out_layouts, &outlink->in_channel_layouts);
00077
00078 return 0;
00079 }
00080
00081 static int config_output(AVFilterLink *outlink)
00082 {
00083 AVFilterContext *ctx = outlink->src;
00084 AVFilterLink *inlink = ctx->inputs[0];
00085 ResampleContext *s = ctx->priv;
00086 char buf1[64], buf2[64];
00087 int ret;
00088
00089 if (s->avr) {
00090 avresample_close(s->avr);
00091 avresample_free(&s->avr);
00092 }
00093
00094 if (inlink->channel_layout == outlink->channel_layout &&
00095 inlink->sample_rate == outlink->sample_rate &&
00096 inlink->format == outlink->format)
00097 return 0;
00098
00099 if (!(s->avr = avresample_alloc_context()))
00100 return AVERROR(ENOMEM);
00101
00102 av_opt_set_int(s->avr, "in_channel_layout", inlink ->channel_layout, 0);
00103 av_opt_set_int(s->avr, "out_channel_layout", outlink->channel_layout, 0);
00104 av_opt_set_int(s->avr, "in_sample_fmt", inlink ->format, 0);
00105 av_opt_set_int(s->avr, "out_sample_fmt", outlink->format, 0);
00106 av_opt_set_int(s->avr, "in_sample_rate", inlink ->sample_rate, 0);
00107 av_opt_set_int(s->avr, "out_sample_rate", outlink->sample_rate, 0);
00108
00109 if ((ret = avresample_open(s->avr)) < 0)
00110 return ret;
00111
00112 outlink->time_base = (AVRational){ 1, outlink->sample_rate };
00113 s->next_pts = AV_NOPTS_VALUE;
00114
00115 av_get_channel_layout_string(buf1, sizeof(buf1),
00116 -1, inlink ->channel_layout);
00117 av_get_channel_layout_string(buf2, sizeof(buf2),
00118 -1, outlink->channel_layout);
00119 av_log(ctx, AV_LOG_VERBOSE,
00120 "fmt:%s srate:%d cl:%s -> fmt:%s srate:%d cl:%s\n",
00121 av_get_sample_fmt_name(inlink ->format), inlink ->sample_rate, buf1,
00122 av_get_sample_fmt_name(outlink->format), outlink->sample_rate, buf2);
00123
00124 return 0;
00125 }
00126
00127 static int request_frame(AVFilterLink *outlink)
00128 {
00129 AVFilterContext *ctx = outlink->src;
00130 ResampleContext *s = ctx->priv;
00131 int ret = 0;
00132
00133 s->got_output = 0;
00134 while (ret >= 0 && !s->got_output)
00135 ret = ff_request_frame(ctx->inputs[0]);
00136
00137
00138 if (ret == AVERROR_EOF && s->avr) {
00139 AVFilterBufferRef *buf;
00140 int nb_samples = av_rescale_rnd(avresample_get_delay(s->avr),
00141 outlink->sample_rate,
00142 ctx->inputs[0]->sample_rate,
00143 AV_ROUND_UP);
00144
00145 if (!nb_samples)
00146 return ret;
00147
00148 buf = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
00149 if (!buf)
00150 return AVERROR(ENOMEM);
00151
00152 ret = avresample_convert(s->avr, (void**)buf->extended_data,
00153 buf->linesize[0], nb_samples,
00154 NULL, 0, 0);
00155 if (ret <= 0) {
00156 avfilter_unref_buffer(buf);
00157 return (ret == 0) ? AVERROR_EOF : ret;
00158 }
00159
00160 buf->pts = s->next_pts;
00161 return ff_filter_samples(outlink, buf);
00162 }
00163 return ret;
00164 }
00165
00166 static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *buf)
00167 {
00168 AVFilterContext *ctx = inlink->dst;
00169 ResampleContext *s = ctx->priv;
00170 AVFilterLink *outlink = ctx->outputs[0];
00171 int ret;
00172
00173 if (s->avr) {
00174 AVFilterBufferRef *buf_out;
00175 int delay, nb_samples;
00176
00177
00178 delay = avresample_get_delay(s->avr);
00179 nb_samples = av_rescale_rnd(buf->audio->nb_samples + delay,
00180 outlink->sample_rate, inlink->sample_rate,
00181 AV_ROUND_UP);
00182
00183 buf_out = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
00184 if (!buf_out) {
00185 ret = AVERROR(ENOMEM);
00186 goto fail;
00187 }
00188
00189 ret = avresample_convert(s->avr, (void**)buf_out->extended_data,
00190 buf_out->linesize[0], nb_samples,
00191 (void**)buf->extended_data, buf->linesize[0],
00192 buf->audio->nb_samples);
00193 if (ret < 0) {
00194 avfilter_unref_buffer(buf_out);
00195 goto fail;
00196 }
00197
00198 av_assert0(!avresample_available(s->avr));
00199
00200 if (s->next_pts == AV_NOPTS_VALUE) {
00201 if (buf->pts == AV_NOPTS_VALUE) {
00202 av_log(ctx, AV_LOG_WARNING, "First timestamp is missing, "
00203 "assuming 0.\n");
00204 s->next_pts = 0;
00205 } else
00206 s->next_pts = av_rescale_q(buf->pts, inlink->time_base,
00207 outlink->time_base);
00208 }
00209
00210 if (ret > 0) {
00211 buf_out->audio->nb_samples = ret;
00212 if (buf->pts != AV_NOPTS_VALUE) {
00213 buf_out->pts = av_rescale_q(buf->pts, inlink->time_base,
00214 outlink->time_base) -
00215 av_rescale(delay, outlink->sample_rate,
00216 inlink->sample_rate);
00217 } else
00218 buf_out->pts = s->next_pts;
00219
00220 s->next_pts = buf_out->pts + buf_out->audio->nb_samples;
00221
00222 ret = ff_filter_samples(outlink, buf_out);
00223 s->got_output = 1;
00224 }
00225
00226 fail:
00227 avfilter_unref_buffer(buf);
00228 } else {
00229 ret = ff_filter_samples(outlink, buf);
00230 s->got_output = 1;
00231 }
00232
00233 return ret;
00234 }
00235
00236 AVFilter avfilter_af_resample = {
00237 .name = "resample",
00238 .description = NULL_IF_CONFIG_SMALL("Audio resampling and conversion."),
00239 .priv_size = sizeof(ResampleContext),
00240
00241 .uninit = uninit,
00242 .query_formats = query_formats,
00243
00244 .inputs = (const AVFilterPad[]) {{ .name = "default",
00245 .type = AVMEDIA_TYPE_AUDIO,
00246 .filter_samples = filter_samples,
00247 .min_perms = AV_PERM_READ },
00248 { .name = NULL}},
00249 .outputs = (const AVFilterPad[]) {{ .name = "default",
00250 .type = AVMEDIA_TYPE_AUDIO,
00251 .config_props = config_output,
00252 .request_frame = request_frame },
00253 { .name = NULL}},
00254 };