You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
6.5KB

  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. * @file
  23. * Filter that changes number of samples on single output operation
  24. */
  25. #include "libavutil/audio_fifo.h"
  26. #include "libavutil/audioconvert.h"
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/opt.h"
  29. #include "avfilter.h"
  30. #include "audio.h"
  31. #include "internal.h"
  32. #include "formats.h"
  33. typedef struct {
  34. const AVClass *class;
  35. int nb_out_samples; ///< how many samples to output
  36. AVAudioFifo *fifo; ///< samples are queued here
  37. int64_t next_out_pts;
  38. int req_fullfilled;
  39. int pad;
  40. } ASNSContext;
  41. #define OFFSET(x) offsetof(ASNSContext, x)
  42. static const AVOption asetnsamples_options[] = {
  43. { "pad", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
  44. { "p", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
  45. { "nb_out_samples", "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.dbl=1024}, 1, INT_MAX },
  46. { "n", "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.dbl=1024}, 1, INT_MAX },
  47. { NULL }
  48. };
  49. AVFILTER_DEFINE_CLASS(asetnsamples);
  50. static av_cold int init(AVFilterContext *ctx, const char *args)
  51. {
  52. ASNSContext *asns = ctx->priv;
  53. int err;
  54. asns->class = &asetnsamples_class;
  55. av_opt_set_defaults(asns);
  56. if ((err = av_set_options_string(asns, args, "=", ":")) < 0) {
  57. av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  58. return err;
  59. }
  60. asns->next_out_pts = AV_NOPTS_VALUE;
  61. av_log(ctx, AV_LOG_VERBOSE, "nb_out_samples:%d pad:%d\n", asns->nb_out_samples, asns->pad);
  62. return 0;
  63. }
  64. static av_cold void uninit(AVFilterContext *ctx)
  65. {
  66. ASNSContext *asns = ctx->priv;
  67. av_audio_fifo_free(asns->fifo);
  68. }
  69. static int config_props_output(AVFilterLink *outlink)
  70. {
  71. ASNSContext *asns = outlink->src->priv;
  72. int nb_channels = av_get_channel_layout_nb_channels(outlink->channel_layout);
  73. asns->fifo = av_audio_fifo_alloc(outlink->format, nb_channels, asns->nb_out_samples);
  74. if (!asns->fifo)
  75. return AVERROR(ENOMEM);
  76. return 0;
  77. }
  78. static int push_samples(AVFilterLink *outlink)
  79. {
  80. ASNSContext *asns = outlink->src->priv;
  81. AVFilterBufferRef *outsamples = NULL;
  82. int nb_out_samples, nb_pad_samples;
  83. if (asns->pad) {
  84. nb_out_samples = av_audio_fifo_size(asns->fifo) ? asns->nb_out_samples : 0;
  85. nb_pad_samples = nb_out_samples - FFMIN(nb_out_samples, av_audio_fifo_size(asns->fifo));
  86. } else {
  87. nb_out_samples = FFMIN(asns->nb_out_samples, av_audio_fifo_size(asns->fifo));
  88. nb_pad_samples = 0;
  89. }
  90. if (!nb_out_samples)
  91. return 0;
  92. outsamples = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_out_samples);
  93. av_assert0(outsamples);
  94. av_audio_fifo_read(asns->fifo,
  95. (void **)outsamples->extended_data, nb_out_samples);
  96. if (nb_pad_samples)
  97. av_samples_set_silence(outsamples->extended_data, nb_out_samples - nb_pad_samples,
  98. nb_pad_samples, av_get_channel_layout_nb_channels(outlink->channel_layout),
  99. outlink->format);
  100. outsamples->audio->nb_samples = nb_out_samples;
  101. outsamples->audio->channel_layout = outlink->channel_layout;
  102. outsamples->audio->sample_rate = outlink->sample_rate;
  103. outsamples->pts = asns->next_out_pts;
  104. if (asns->next_out_pts != AV_NOPTS_VALUE)
  105. asns->next_out_pts += nb_out_samples;
  106. ff_filter_samples(outlink, outsamples);
  107. asns->req_fullfilled = 1;
  108. return nb_out_samples;
  109. }
  110. static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  111. {
  112. AVFilterContext *ctx = inlink->dst;
  113. ASNSContext *asns = ctx->priv;
  114. AVFilterLink *outlink = ctx->outputs[0];
  115. int ret;
  116. int nb_samples = insamples->audio->nb_samples;
  117. if (av_audio_fifo_space(asns->fifo) < nb_samples) {
  118. av_log(ctx, AV_LOG_DEBUG, "No space for %d samples, stretching audio fifo\n", nb_samples);
  119. ret = av_audio_fifo_realloc(asns->fifo, av_audio_fifo_size(asns->fifo) + nb_samples);
  120. if (ret < 0) {
  121. av_log(ctx, AV_LOG_ERROR,
  122. "Stretching audio fifo failed, discarded %d samples\n", nb_samples);
  123. return -1;
  124. }
  125. }
  126. av_audio_fifo_write(asns->fifo, (void **)insamples->extended_data, nb_samples);
  127. if (asns->next_out_pts == AV_NOPTS_VALUE)
  128. asns->next_out_pts = insamples->pts;
  129. avfilter_unref_buffer(insamples);
  130. if (av_audio_fifo_size(asns->fifo) >= asns->nb_out_samples)
  131. push_samples(outlink);
  132. return 0;
  133. }
  134. static int request_frame(AVFilterLink *outlink)
  135. {
  136. ASNSContext *asns = outlink->src->priv;
  137. AVFilterLink *inlink = outlink->src->inputs[0];
  138. int ret;
  139. asns->req_fullfilled = 0;
  140. do {
  141. ret = ff_request_frame(inlink);
  142. } while (!asns->req_fullfilled && ret >= 0);
  143. if (ret == AVERROR_EOF)
  144. while (push_samples(outlink))
  145. ;
  146. return ret;
  147. }
  148. AVFilter avfilter_af_asetnsamples = {
  149. .name = "asetnsamples",
  150. .description = NULL_IF_CONFIG_SMALL("Set the number of samples for each output audio frames."),
  151. .priv_size = sizeof(ASNSContext),
  152. .init = init,
  153. .uninit = uninit,
  154. .inputs = (const AVFilterPad[]) {
  155. {
  156. .name = "default",
  157. .type = AVMEDIA_TYPE_AUDIO,
  158. .filter_samples = filter_samples,
  159. .min_perms = AV_PERM_READ|AV_PERM_WRITE
  160. },
  161. { .name = NULL }
  162. },
  163. .outputs = (const AVFilterPad[]) {
  164. {
  165. .name = "default",
  166. .type = AVMEDIA_TYPE_AUDIO,
  167. .request_frame = request_frame,
  168. .config_props = config_props_output,
  169. },
  170. { .name = NULL }
  171. },
  172. };