FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
af_asetnsamples.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Andrey Utkin
3  * Copyright (c) 2012 Stefano Sabatini
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  * Filter that changes number of samples on single output operation
25  */
26 
27 #include "libavutil/audio_fifo.h"
28 #include "libavutil/avassert.h"
30 #include "libavutil/opt.h"
31 #include "avfilter.h"
32 #include "audio.h"
33 #include "internal.h"
34 #include "formats.h"
35 
36 typedef struct {
37  const AVClass *class;
38  int nb_out_samples; ///< how many samples to output
39  AVAudioFifo *fifo; ///< samples are queued here
40  int64_t next_out_pts;
42  int pad;
43 } ASNSContext;
44 
45 #define OFFSET(x) offsetof(ASNSContext, x)
46 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
47 
48 static const AVOption asetnsamples_options[] = {
49 { "pad", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
50 { "p", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
51 { "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 },
52 { "n", "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
53 { NULL }
54 };
55 
56 AVFILTER_DEFINE_CLASS(asetnsamples);
57 
58 static av_cold int init(AVFilterContext *ctx, const char *args)
59 {
60  ASNSContext *asns = ctx->priv;
61  int err;
62 
63  asns->class = &asetnsamples_class;
64  av_opt_set_defaults(asns);
65 
66  if ((err = av_set_options_string(asns, args, "=", ":")) < 0)
67  return err;
68 
70  av_log(ctx, AV_LOG_VERBOSE, "nb_out_samples:%d pad:%d\n", asns->nb_out_samples, asns->pad);
71 
72  return 0;
73 }
74 
75 static av_cold void uninit(AVFilterContext *ctx)
76 {
77  ASNSContext *asns = ctx->priv;
78  av_audio_fifo_free(asns->fifo);
79 }
80 
81 static int config_props_output(AVFilterLink *outlink)
82 {
83  ASNSContext *asns = outlink->src->priv;
85 
86  asns->fifo = av_audio_fifo_alloc(outlink->format, nb_channels, asns->nb_out_samples);
87  if (!asns->fifo)
88  return AVERROR(ENOMEM);
89 
90  return 0;
91 }
92 
93 static int push_samples(AVFilterLink *outlink)
94 {
95  ASNSContext *asns = outlink->src->priv;
96  AVFilterBufferRef *outsamples = NULL;
97  int nb_out_samples, nb_pad_samples;
98 
99  if (asns->pad) {
100  nb_out_samples = av_audio_fifo_size(asns->fifo) ? asns->nb_out_samples : 0;
101  nb_pad_samples = nb_out_samples - FFMIN(nb_out_samples, av_audio_fifo_size(asns->fifo));
102  } else {
103  nb_out_samples = FFMIN(asns->nb_out_samples, av_audio_fifo_size(asns->fifo));
104  nb_pad_samples = 0;
105  }
106 
107  if (!nb_out_samples)
108  return 0;
109 
110  outsamples = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_out_samples);
111  av_assert0(outsamples);
112 
113  av_audio_fifo_read(asns->fifo,
114  (void **)outsamples->extended_data, nb_out_samples);
115 
116  if (nb_pad_samples)
117  av_samples_set_silence(outsamples->extended_data, nb_out_samples - nb_pad_samples,
118  nb_pad_samples, av_get_channel_layout_nb_channels(outlink->channel_layout),
119  outlink->format);
120  outsamples->audio->nb_samples = nb_out_samples;
121  outsamples->audio->channel_layout = outlink->channel_layout;
122  outsamples->audio->sample_rate = outlink->sample_rate;
123  outsamples->pts = asns->next_out_pts;
124 
125  if (asns->next_out_pts != AV_NOPTS_VALUE)
126  asns->next_out_pts += nb_out_samples;
127 
128  ff_filter_frame(outlink, outsamples);
129  asns->req_fullfilled = 1;
130  return nb_out_samples;
131 }
132 
133 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamples)
134 {
135  AVFilterContext *ctx = inlink->dst;
136  ASNSContext *asns = ctx->priv;
137  AVFilterLink *outlink = ctx->outputs[0];
138  int ret;
139  int nb_samples = insamples->audio->nb_samples;
140 
141  if (av_audio_fifo_space(asns->fifo) < nb_samples) {
142  av_log(ctx, AV_LOG_DEBUG, "No space for %d samples, stretching audio fifo\n", nb_samples);
143  ret = av_audio_fifo_realloc(asns->fifo, av_audio_fifo_size(asns->fifo) + nb_samples);
144  if (ret < 0) {
145  av_log(ctx, AV_LOG_ERROR,
146  "Stretching audio fifo failed, discarded %d samples\n", nb_samples);
147  return -1;
148  }
149  }
150  av_audio_fifo_write(asns->fifo, (void **)insamples->extended_data, nb_samples);
151  if (asns->next_out_pts == AV_NOPTS_VALUE)
152  asns->next_out_pts = insamples->pts;
153  avfilter_unref_buffer(insamples);
154 
155  while (av_audio_fifo_size(asns->fifo) >= asns->nb_out_samples)
156  push_samples(outlink);
157  return 0;
158 }
159 
160 static int request_frame(AVFilterLink *outlink)
161 {
162  ASNSContext *asns = outlink->src->priv;
163  AVFilterLink *inlink = outlink->src->inputs[0];
164  int ret;
165 
166  asns->req_fullfilled = 0;
167  do {
168  ret = ff_request_frame(inlink);
169  } while (!asns->req_fullfilled && ret >= 0);
170 
171  if (ret == AVERROR_EOF)
172  while (push_samples(outlink))
173  ;
174 
175  return ret;
176 }
177 
179  {
180  .name = "default",
181  .type = AVMEDIA_TYPE_AUDIO,
182  .filter_frame = filter_frame,
183  .min_perms = AV_PERM_READ | AV_PERM_WRITE,
184  },
185  { NULL }
186 };
187 
189  {
190  .name = "default",
191  .type = AVMEDIA_TYPE_AUDIO,
192  .request_frame = request_frame,
193  .config_props = config_props_output,
194  },
195  { NULL }
196 };
197 
199  .name = "asetnsamples",
200  .description = NULL_IF_CONFIG_SMALL("Set the number of samples for each output audio frames."),
201  .priv_size = sizeof(ASNSContext),
202  .init = init,
203  .uninit = uninit,
204  .inputs = asetnsamples_inputs,
205  .outputs = asetnsamples_outputs,
206  .priv_class = &asetnsamples_class,
207 };