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.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/avassert.h"
  27. #include "libavutil/opt.h"
  28. #include "avfilter.h"
  29. #include "audio.h"
  30. #include "internal.h"
  31. #include "formats.h"
  32. typedef struct {
  33. const AVClass *class;
  34. int nb_out_samples; ///< how many samples to output
  35. AVAudioFifo *fifo; ///< samples are queued here
  36. int64_t next_out_pts;
  37. int req_fullfilled;
  38. int pad;
  39. } ASNSContext;
  40. #define OFFSET(x) offsetof(ASNSContext, x)
  41. static const AVOption asetnsamples_options[] = {
  42. { "pad", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
  43. { "p", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
  44. { "nb_out_samples", "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.dbl=1024}, 1, INT_MAX },
  45. { "n", "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.dbl=1024}, 1, INT_MAX },
  46. { NULL }
  47. };
  48. AVFILTER_DEFINE_CLASS(asetnsamples);
  49. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  50. {
  51. ASNSContext *asns = ctx->priv;
  52. int err;
  53. asns->class = &asetnsamples_class;
  54. av_opt_set_defaults(asns);
  55. if ((err = av_set_options_string(asns, args, "=", ":")) < 0) {
  56. av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  57. return err;
  58. }
  59. asns->next_out_pts = AV_NOPTS_VALUE;
  60. av_log(ctx, AV_LOG_INFO, "nb_out_samples:%d pad:%d\n", asns->nb_out_samples, asns->pad);
  61. return 0;
  62. }
  63. static av_cold void uninit(AVFilterContext *ctx)
  64. {
  65. ASNSContext *asns = ctx->priv;
  66. av_audio_fifo_free(asns->fifo);
  67. }
  68. static int config_props_output(AVFilterLink *outlink)
  69. {
  70. ASNSContext *asns = outlink->src->priv;
  71. int nb_channels = av_get_channel_layout_nb_channels(outlink->channel_layout);
  72. asns->fifo = av_audio_fifo_alloc(outlink->format, nb_channels, asns->nb_out_samples);
  73. if (!asns->fifo)
  74. return AVERROR(ENOMEM);
  75. return 0;
  76. }
  77. static int push_samples(AVFilterLink *outlink)
  78. {
  79. ASNSContext *asns = outlink->src->priv;
  80. AVFilterBufferRef *outsamples = NULL;
  81. int nb_out_samples, nb_pad_samples;
  82. if (asns->pad) {
  83. nb_out_samples = av_audio_fifo_size(asns->fifo) ? asns->nb_out_samples : 0;
  84. nb_pad_samples = nb_out_samples - FFMIN(nb_out_samples, av_audio_fifo_size(asns->fifo));
  85. } else {
  86. nb_out_samples = FFMIN(asns->nb_out_samples, av_audio_fifo_size(asns->fifo));
  87. nb_pad_samples = 0;
  88. }
  89. if (!nb_out_samples)
  90. return 0;
  91. outsamples = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_out_samples);
  92. av_assert0(outsamples);
  93. av_audio_fifo_read(asns->fifo,
  94. (void **)outsamples->extended_data, nb_out_samples);
  95. if (nb_pad_samples)
  96. av_samples_set_silence(outsamples->extended_data, nb_out_samples - nb_pad_samples,
  97. nb_pad_samples, av_get_channel_layout_nb_channels(outlink->channel_layout),
  98. outlink->format);
  99. outsamples->audio->nb_samples = nb_out_samples;
  100. outsamples->audio->channel_layout = outlink->channel_layout;
  101. outsamples->audio->sample_rate = outlink->sample_rate;
  102. outsamples->pts = asns->next_out_pts;
  103. if (asns->next_out_pts != AV_NOPTS_VALUE)
  104. asns->next_out_pts += nb_out_samples;
  105. ff_filter_samples(outlink, outsamples);
  106. asns->req_fullfilled = 1;
  107. return nb_out_samples;
  108. }
  109. static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  110. {
  111. AVFilterContext *ctx = inlink->dst;
  112. ASNSContext *asns = ctx->priv;
  113. AVFilterLink *outlink = ctx->outputs[0];
  114. int ret;
  115. int nb_samples = insamples->audio->nb_samples;
  116. if (av_audio_fifo_space(asns->fifo) < nb_samples) {
  117. av_log(ctx, AV_LOG_DEBUG, "No space for %d samples, stretching audio fifo\n", nb_samples);
  118. ret = av_audio_fifo_realloc(asns->fifo, av_audio_fifo_size(asns->fifo) + nb_samples);
  119. if (ret < 0) {
  120. av_log(ctx, AV_LOG_ERROR,
  121. "Stretching audio fifo failed, discarded %d samples\n", nb_samples);
  122. return;
  123. }
  124. }
  125. av_audio_fifo_write(asns->fifo, (void **)insamples->extended_data, nb_samples);
  126. if (asns->next_out_pts == AV_NOPTS_VALUE)
  127. asns->next_out_pts = insamples->pts;
  128. avfilter_unref_buffer(insamples);
  129. if (av_audio_fifo_size(asns->fifo) >= asns->nb_out_samples)
  130. push_samples(outlink);
  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. };