00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/audio_fifo.h"
00028 #include "libavutil/audioconvert.h"
00029 #include "libavutil/avassert.h"
00030 #include "libavutil/opt.h"
00031 #include "avfilter.h"
00032 #include "audio.h"
00033 #include "internal.h"
00034 #include "formats.h"
00035
00036 typedef struct {
00037 const AVClass *class;
00038 int nb_out_samples;
00039 AVAudioFifo *fifo;
00040 int64_t next_out_pts;
00041 int req_fullfilled;
00042 int pad;
00043 } ASNSContext;
00044
00045 #define OFFSET(x) offsetof(ASNSContext, x)
00046 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
00047
00048 static const AVOption asetnsamples_options[] = {
00049 { "pad", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
00050 { "p", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
00051 { "nb_out_samples", "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
00052 { "n", "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
00053 { NULL }
00054 };
00055
00056 AVFILTER_DEFINE_CLASS(asetnsamples);
00057
00058 static av_cold int init(AVFilterContext *ctx, const char *args)
00059 {
00060 ASNSContext *asns = ctx->priv;
00061 int err;
00062
00063 asns->class = &asetnsamples_class;
00064 av_opt_set_defaults(asns);
00065
00066 if ((err = av_set_options_string(asns, args, "=", ":")) < 0)
00067 return err;
00068
00069 asns->next_out_pts = AV_NOPTS_VALUE;
00070 av_log(ctx, AV_LOG_VERBOSE, "nb_out_samples:%d pad:%d\n", asns->nb_out_samples, asns->pad);
00071
00072 return 0;
00073 }
00074
00075 static av_cold void uninit(AVFilterContext *ctx)
00076 {
00077 ASNSContext *asns = ctx->priv;
00078 av_audio_fifo_free(asns->fifo);
00079 }
00080
00081 static int config_props_output(AVFilterLink *outlink)
00082 {
00083 ASNSContext *asns = outlink->src->priv;
00084 int nb_channels = av_get_channel_layout_nb_channels(outlink->channel_layout);
00085
00086 asns->fifo = av_audio_fifo_alloc(outlink->format, nb_channels, asns->nb_out_samples);
00087 if (!asns->fifo)
00088 return AVERROR(ENOMEM);
00089
00090 return 0;
00091 }
00092
00093 static int push_samples(AVFilterLink *outlink)
00094 {
00095 ASNSContext *asns = outlink->src->priv;
00096 AVFilterBufferRef *outsamples = NULL;
00097 int nb_out_samples, nb_pad_samples;
00098
00099 if (asns->pad) {
00100 nb_out_samples = av_audio_fifo_size(asns->fifo) ? asns->nb_out_samples : 0;
00101 nb_pad_samples = nb_out_samples - FFMIN(nb_out_samples, av_audio_fifo_size(asns->fifo));
00102 } else {
00103 nb_out_samples = FFMIN(asns->nb_out_samples, av_audio_fifo_size(asns->fifo));
00104 nb_pad_samples = 0;
00105 }
00106
00107 if (!nb_out_samples)
00108 return 0;
00109
00110 outsamples = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_out_samples);
00111 av_assert0(outsamples);
00112
00113 av_audio_fifo_read(asns->fifo,
00114 (void **)outsamples->extended_data, nb_out_samples);
00115
00116 if (nb_pad_samples)
00117 av_samples_set_silence(outsamples->extended_data, nb_out_samples - nb_pad_samples,
00118 nb_pad_samples, av_get_channel_layout_nb_channels(outlink->channel_layout),
00119 outlink->format);
00120 outsamples->audio->nb_samples = nb_out_samples;
00121 outsamples->audio->channel_layout = outlink->channel_layout;
00122 outsamples->audio->sample_rate = outlink->sample_rate;
00123 outsamples->pts = asns->next_out_pts;
00124
00125 if (asns->next_out_pts != AV_NOPTS_VALUE)
00126 asns->next_out_pts += nb_out_samples;
00127
00128 ff_filter_samples(outlink, outsamples);
00129 asns->req_fullfilled = 1;
00130 return nb_out_samples;
00131 }
00132
00133 static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
00134 {
00135 AVFilterContext *ctx = inlink->dst;
00136 ASNSContext *asns = ctx->priv;
00137 AVFilterLink *outlink = ctx->outputs[0];
00138 int ret;
00139 int nb_samples = insamples->audio->nb_samples;
00140
00141 if (av_audio_fifo_space(asns->fifo) < nb_samples) {
00142 av_log(ctx, AV_LOG_DEBUG, "No space for %d samples, stretching audio fifo\n", nb_samples);
00143 ret = av_audio_fifo_realloc(asns->fifo, av_audio_fifo_size(asns->fifo) + nb_samples);
00144 if (ret < 0) {
00145 av_log(ctx, AV_LOG_ERROR,
00146 "Stretching audio fifo failed, discarded %d samples\n", nb_samples);
00147 return -1;
00148 }
00149 }
00150 av_audio_fifo_write(asns->fifo, (void **)insamples->extended_data, nb_samples);
00151 if (asns->next_out_pts == AV_NOPTS_VALUE)
00152 asns->next_out_pts = insamples->pts;
00153 avfilter_unref_buffer(insamples);
00154
00155 while (av_audio_fifo_size(asns->fifo) >= asns->nb_out_samples)
00156 push_samples(outlink);
00157 return 0;
00158 }
00159
00160 static int request_frame(AVFilterLink *outlink)
00161 {
00162 ASNSContext *asns = outlink->src->priv;
00163 AVFilterLink *inlink = outlink->src->inputs[0];
00164 int ret;
00165
00166 asns->req_fullfilled = 0;
00167 do {
00168 ret = ff_request_frame(inlink);
00169 } while (!asns->req_fullfilled && ret >= 0);
00170
00171 if (ret == AVERROR_EOF)
00172 while (push_samples(outlink))
00173 ;
00174
00175 return ret;
00176 }
00177
00178 AVFilter avfilter_af_asetnsamples = {
00179 .name = "asetnsamples",
00180 .description = NULL_IF_CONFIG_SMALL("Set the number of samples for each output audio frames."),
00181 .priv_size = sizeof(ASNSContext),
00182 .init = init,
00183 .uninit = uninit,
00184
00185 .inputs = (const AVFilterPad[]) {
00186 {
00187 .name = "default",
00188 .type = AVMEDIA_TYPE_AUDIO,
00189 .filter_samples = filter_samples,
00190 .min_perms = AV_PERM_READ|AV_PERM_WRITE
00191 },
00192 { .name = NULL }
00193 },
00194
00195 .outputs = (const AVFilterPad[]) {
00196 {
00197 .name = "default",
00198 .type = AVMEDIA_TYPE_AUDIO,
00199 .request_frame = request_frame,
00200 .config_props = config_props_output,
00201 },
00202 { .name = NULL }
00203 },
00204 .priv_class = &asetnsamples_class,
00205 };