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.

204 lines
6.4KB

  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. return err;
  58. asns->next_out_pts = AV_NOPTS_VALUE;
  59. av_log(ctx, AV_LOG_VERBOSE, "nb_out_samples:%d pad:%d\n", asns->nb_out_samples, asns->pad);
  60. return 0;
  61. }
  62. static av_cold void uninit(AVFilterContext *ctx)
  63. {
  64. ASNSContext *asns = ctx->priv;
  65. av_audio_fifo_free(asns->fifo);
  66. }
  67. static int config_props_output(AVFilterLink *outlink)
  68. {
  69. ASNSContext *asns = outlink->src->priv;
  70. int nb_channels = av_get_channel_layout_nb_channels(outlink->channel_layout);
  71. asns->fifo = av_audio_fifo_alloc(outlink->format, nb_channels, asns->nb_out_samples);
  72. if (!asns->fifo)
  73. return AVERROR(ENOMEM);
  74. return 0;
  75. }
  76. static int push_samples(AVFilterLink *outlink)
  77. {
  78. ASNSContext *asns = outlink->src->priv;
  79. AVFilterBufferRef *outsamples = NULL;
  80. int nb_out_samples, nb_pad_samples;
  81. if (asns->pad) {
  82. nb_out_samples = av_audio_fifo_size(asns->fifo) ? asns->nb_out_samples : 0;
  83. nb_pad_samples = nb_out_samples - FFMIN(nb_out_samples, av_audio_fifo_size(asns->fifo));
  84. } else {
  85. nb_out_samples = FFMIN(asns->nb_out_samples, av_audio_fifo_size(asns->fifo));
  86. nb_pad_samples = 0;
  87. }
  88. if (!nb_out_samples)
  89. return 0;
  90. outsamples = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_out_samples);
  91. av_assert0(outsamples);
  92. av_audio_fifo_read(asns->fifo,
  93. (void **)outsamples->extended_data, nb_out_samples);
  94. if (nb_pad_samples)
  95. av_samples_set_silence(outsamples->extended_data, nb_out_samples - nb_pad_samples,
  96. nb_pad_samples, av_get_channel_layout_nb_channels(outlink->channel_layout),
  97. outlink->format);
  98. outsamples->audio->nb_samples = nb_out_samples;
  99. outsamples->audio->channel_layout = outlink->channel_layout;
  100. outsamples->audio->sample_rate = outlink->sample_rate;
  101. outsamples->pts = asns->next_out_pts;
  102. if (asns->next_out_pts != AV_NOPTS_VALUE)
  103. asns->next_out_pts += nb_out_samples;
  104. ff_filter_samples(outlink, outsamples);
  105. asns->req_fullfilled = 1;
  106. return nb_out_samples;
  107. }
  108. static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  109. {
  110. AVFilterContext *ctx = inlink->dst;
  111. ASNSContext *asns = ctx->priv;
  112. AVFilterLink *outlink = ctx->outputs[0];
  113. int ret;
  114. int nb_samples = insamples->audio->nb_samples;
  115. if (av_audio_fifo_space(asns->fifo) < nb_samples) {
  116. av_log(ctx, AV_LOG_DEBUG, "No space for %d samples, stretching audio fifo\n", nb_samples);
  117. ret = av_audio_fifo_realloc(asns->fifo, av_audio_fifo_size(asns->fifo) + nb_samples);
  118. if (ret < 0) {
  119. av_log(ctx, AV_LOG_ERROR,
  120. "Stretching audio fifo failed, discarded %d samples\n", nb_samples);
  121. return -1;
  122. }
  123. }
  124. av_audio_fifo_write(asns->fifo, (void **)insamples->extended_data, nb_samples);
  125. if (asns->next_out_pts == AV_NOPTS_VALUE)
  126. asns->next_out_pts = insamples->pts;
  127. avfilter_unref_buffer(insamples);
  128. if (av_audio_fifo_size(asns->fifo) >= asns->nb_out_samples)
  129. push_samples(outlink);
  130. return 0;
  131. }
  132. static int request_frame(AVFilterLink *outlink)
  133. {
  134. ASNSContext *asns = outlink->src->priv;
  135. AVFilterLink *inlink = outlink->src->inputs[0];
  136. int ret;
  137. asns->req_fullfilled = 0;
  138. do {
  139. ret = ff_request_frame(inlink);
  140. } while (!asns->req_fullfilled && ret >= 0);
  141. if (ret == AVERROR_EOF)
  142. while (push_samples(outlink))
  143. ;
  144. return ret;
  145. }
  146. AVFilter avfilter_af_asetnsamples = {
  147. .name = "asetnsamples",
  148. .description = NULL_IF_CONFIG_SMALL("Set the number of samples for each output audio frames."),
  149. .priv_size = sizeof(ASNSContext),
  150. .init = init,
  151. .uninit = uninit,
  152. .inputs = (const AVFilterPad[]) {
  153. {
  154. .name = "default",
  155. .type = AVMEDIA_TYPE_AUDIO,
  156. .filter_samples = filter_samples,
  157. .min_perms = AV_PERM_READ|AV_PERM_WRITE
  158. },
  159. { .name = NULL }
  160. },
  161. .outputs = (const AVFilterPad[]) {
  162. {
  163. .name = "default",
  164. .type = AVMEDIA_TYPE_AUDIO,
  165. .request_frame = request_frame,
  166. .config_props = config_props_output,
  167. },
  168. { .name = NULL }
  169. },
  170. };