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