00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/avstring.h"
00028 #include "libavutil/opt.h"
00029 #include "libavutil/samplefmt.h"
00030 #include "libavutil/avassert.h"
00031 #include "libswresample/swresample.h"
00032 #include "avfilter.h"
00033 #include "audio.h"
00034 #include "internal.h"
00035
00036 typedef struct {
00037 double ratio;
00038 struct SwrContext *swr;
00039 int64_t next_pts;
00040 int req_fullfilled;
00041 } AResampleContext;
00042
00043 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00044 {
00045 AResampleContext *aresample = ctx->priv;
00046 int ret = 0;
00047 char *argd = av_strdup(args);
00048
00049 aresample->next_pts = AV_NOPTS_VALUE;
00050 aresample->swr = swr_alloc();
00051 if (!aresample->swr)
00052 return AVERROR(ENOMEM);
00053
00054 if (args) {
00055 char *ptr=argd, *token;
00056
00057 while(token = av_strtok(ptr, ":", &ptr)) {
00058 char *value;
00059 av_strtok(token, "=", &value);
00060
00061 if(value) {
00062 if((ret=av_opt_set(aresample->swr, token, value, 0)) < 0)
00063 goto end;
00064 } else {
00065 int out_rate;
00066 if ((ret = ff_parse_sample_rate(&out_rate, token, ctx)) < 0)
00067 goto end;
00068 if((ret = av_opt_set_int(aresample->swr, "osr", out_rate, 0)) < 0)
00069 goto end;
00070 }
00071 }
00072 }
00073 end:
00074 av_free(argd);
00075 return ret;
00076 }
00077
00078 static av_cold void uninit(AVFilterContext *ctx)
00079 {
00080 AResampleContext *aresample = ctx->priv;
00081 swr_free(&aresample->swr);
00082 }
00083
00084 static int query_formats(AVFilterContext *ctx)
00085 {
00086 AResampleContext *aresample = ctx->priv;
00087 int out_rate = av_get_int(aresample->swr, "osr", NULL);
00088 uint64_t out_layout = av_get_int(aresample->swr, "ocl", NULL);
00089 enum AVSampleFormat out_format = av_get_int(aresample->swr, "osf", NULL);
00090
00091 AVFilterLink *inlink = ctx->inputs[0];
00092 AVFilterLink *outlink = ctx->outputs[0];
00093
00094 AVFilterFormats *in_formats = avfilter_all_formats(AVMEDIA_TYPE_AUDIO);
00095 AVFilterFormats *out_formats;
00096 AVFilterFormats *in_samplerates = ff_all_samplerates();
00097 AVFilterFormats *out_samplerates;
00098 AVFilterChannelLayouts *in_layouts = ff_all_channel_layouts();
00099 AVFilterChannelLayouts *out_layouts;
00100
00101 avfilter_formats_ref (in_formats, &inlink->out_formats);
00102 avfilter_formats_ref (in_samplerates, &inlink->out_samplerates);
00103 ff_channel_layouts_ref(in_layouts, &inlink->out_channel_layouts);
00104
00105 if(out_rate > 0) {
00106 out_samplerates = avfilter_make_format_list((int[]){ out_rate, -1 });
00107 } else {
00108 out_samplerates = ff_all_samplerates();
00109 }
00110 avfilter_formats_ref(out_samplerates, &outlink->in_samplerates);
00111
00112 if(out_format != AV_SAMPLE_FMT_NONE) {
00113 out_formats = avfilter_make_format_list((int[]){ out_format, -1 });
00114 } else
00115 out_formats = avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO);
00116 avfilter_formats_ref(out_formats, &outlink->in_formats);
00117
00118 if(out_layout) {
00119 out_layouts = avfilter_make_format64_list((int64_t[]){ out_layout, -1 });
00120 } else
00121 out_layouts = ff_all_channel_layouts();
00122 ff_channel_layouts_ref(out_layouts, &outlink->in_channel_layouts);
00123
00124 return 0;
00125 }
00126
00127
00128 static int config_output(AVFilterLink *outlink)
00129 {
00130 int ret;
00131 AVFilterContext *ctx = outlink->src;
00132 AVFilterLink *inlink = ctx->inputs[0];
00133 AResampleContext *aresample = ctx->priv;
00134 int out_rate;
00135 uint64_t out_layout;
00136 enum AVSampleFormat out_format;
00137 char inchl_buf[128], outchl_buf[128];
00138
00139 aresample->swr = swr_alloc_set_opts(aresample->swr,
00140 outlink->channel_layout, outlink->format, outlink->sample_rate,
00141 inlink->channel_layout, inlink->format, inlink->sample_rate,
00142 0, ctx);
00143 if (!aresample->swr)
00144 return AVERROR(ENOMEM);
00145
00146 ret = swr_init(aresample->swr);
00147 if (ret < 0)
00148 return ret;
00149
00150 out_rate = av_get_int(aresample->swr, "osr", NULL);
00151 out_layout = av_get_int(aresample->swr, "ocl", NULL);
00152 out_format = av_get_int(aresample->swr, "osf", NULL);
00153 outlink->time_base = (AVRational) {1, out_rate};
00154
00155 av_assert0(outlink->sample_rate == out_rate);
00156 av_assert0(outlink->channel_layout == out_layout);
00157 av_assert0(outlink->format == out_format);
00158
00159 aresample->ratio = (double)outlink->sample_rate / inlink->sample_rate;
00160
00161 av_get_channel_layout_string(inchl_buf, sizeof(inchl_buf), -1, inlink ->channel_layout);
00162 av_get_channel_layout_string(outchl_buf, sizeof(outchl_buf), -1, outlink->channel_layout);
00163 av_log(ctx, AV_LOG_INFO, "chl:%s fmt:%s r:%"PRId64"Hz -> chl:%s fmt:%s r:%"PRId64"Hz\n",
00164 inchl_buf, av_get_sample_fmt_name(inlink->format), inlink->sample_rate,
00165 outchl_buf, av_get_sample_fmt_name(outlink->format), outlink->sample_rate);
00166 return 0;
00167 }
00168
00169 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
00170 {
00171 AResampleContext *aresample = inlink->dst->priv;
00172 const int n_in = insamplesref->audio->nb_samples;
00173 int n_out = n_in * aresample->ratio * 2 ;
00174 AVFilterLink *const outlink = inlink->dst->outputs[0];
00175 AVFilterBufferRef *outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n_out);
00176
00177
00178 avfilter_copy_buffer_ref_props(outsamplesref, insamplesref);
00179 outsamplesref->format = outlink->format;
00180 outsamplesref->audio->channel_layout = outlink->channel_layout;
00181 outsamplesref->audio->sample_rate = outlink->sample_rate;
00182
00183 if(insamplesref->pts != AV_NOPTS_VALUE) {
00184 int64_t inpts = av_rescale(insamplesref->pts, inlink->time_base.num * (int64_t)outlink->sample_rate * inlink->sample_rate, inlink->time_base.den);
00185 int64_t outpts= swr_next_pts(aresample->swr, inpts);
00186 aresample->next_pts =
00187 outsamplesref->pts = (outpts + inlink->sample_rate/2) / inlink->sample_rate;
00188 } else {
00189 outsamplesref->pts = AV_NOPTS_VALUE;
00190 }
00191
00192 n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out,
00193 (void *)insamplesref->extended_data, n_in);
00194 if (n_out <= 0) {
00195 avfilter_unref_buffer(outsamplesref);
00196 avfilter_unref_buffer(insamplesref);
00197 return;
00198 }
00199
00200 outsamplesref->audio->sample_rate = outlink->sample_rate;
00201 outsamplesref->audio->nb_samples = n_out;
00202
00203 ff_filter_samples(outlink, outsamplesref);
00204 aresample->req_fullfilled= 1;
00205 avfilter_unref_buffer(insamplesref);
00206 }
00207
00208 static int request_frame(AVFilterLink *outlink)
00209 {
00210 AVFilterContext *ctx = outlink->src;
00211 AResampleContext *aresample = ctx->priv;
00212 AVFilterLink *const inlink = outlink->src->inputs[0];
00213 int ret;
00214
00215 aresample->req_fullfilled = 0;
00216 do{
00217 ret = avfilter_request_frame(ctx->inputs[0]);
00218 }while(!aresample->req_fullfilled && ret>=0);
00219
00220 if (ret == AVERROR_EOF) {
00221 AVFilterBufferRef *outsamplesref;
00222 int n_out = 4096;
00223
00224 outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n_out);
00225 if (!outsamplesref)
00226 return AVERROR(ENOMEM);
00227 n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out, 0, 0);
00228 if (n_out <= 0) {
00229 avfilter_unref_buffer(outsamplesref);
00230 return (n_out == 0) ? AVERROR_EOF : n_out;
00231 }
00232
00233 outsamplesref->audio->sample_rate = outlink->sample_rate;
00234 outsamplesref->audio->nb_samples = n_out;
00235 #if 0
00236 outsamplesref->pts = aresample->next_pts;
00237 if(aresample->next_pts != AV_NOPTS_VALUE)
00238 aresample->next_pts += av_rescale_q(n_out, (AVRational){1 ,outlink->sample_rate}, outlink->time_base);
00239 #else
00240 outsamplesref->pts = (swr_next_pts(aresample->swr, INT64_MIN) + inlink->sample_rate/2) / inlink->sample_rate;
00241 #endif
00242
00243 ff_filter_samples(outlink, outsamplesref);
00244 return 0;
00245 }
00246 return ret;
00247 }
00248
00249 AVFilter avfilter_af_aresample = {
00250 .name = "aresample",
00251 .description = NULL_IF_CONFIG_SMALL("Resample audio data."),
00252 .init = init,
00253 .uninit = uninit,
00254 .query_formats = query_formats,
00255 .priv_size = sizeof(AResampleContext),
00256
00257 .inputs = (const AVFilterPad[]) {{ .name = "default",
00258 .type = AVMEDIA_TYPE_AUDIO,
00259 .filter_samples = filter_samples,
00260 .min_perms = AV_PERM_READ, },
00261 { .name = NULL}},
00262 .outputs = (const AVFilterPad[]) {{ .name = "default",
00263 .config_props = config_output,
00264 .request_frame = request_frame,
00265 .type = AVMEDIA_TYPE_AUDIO, },
00266 { .name = NULL}},
00267 };