audio.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) Stefano Sabatini | stefasab at gmail.com
  3. * Copyright (c) S.N. Hemanth Meenakshisundaram | smeenaks at ucsd.edu
  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. #include "libavutil/avassert.h"
  22. #include "libavutil/channel_layout.h"
  23. #include "libavutil/common.h"
  24. #include "audio.h"
  25. #include "avfilter.h"
  26. #include "internal.h"
  27. #define BUFFER_ALIGN 0
  28. AVFrame *ff_null_get_audio_buffer(AVFilterLink *link, int nb_samples)
  29. {
  30. return ff_get_audio_buffer(link->dst->outputs[0], nb_samples);
  31. }
  32. AVFrame *ff_default_get_audio_buffer(AVFilterLink *link, int nb_samples)
  33. {
  34. AVFrame *frame = NULL;
  35. int channels = link->channels;
  36. av_assert0(channels == av_get_channel_layout_nb_channels(link->channel_layout) || !av_get_channel_layout_nb_channels(link->channel_layout));
  37. if (!link->frame_pool) {
  38. link->frame_pool = ff_frame_pool_audio_init(av_buffer_allocz, channels,
  39. nb_samples, link->format, BUFFER_ALIGN);
  40. if (!link->frame_pool)
  41. return NULL;
  42. } else {
  43. int pool_channels = 0;
  44. int pool_nb_samples = 0;
  45. int pool_align = 0;
  46. enum AVSampleFormat pool_format = AV_SAMPLE_FMT_NONE;
  47. if (ff_frame_pool_get_audio_config(link->frame_pool,
  48. &pool_channels, &pool_nb_samples,
  49. &pool_format, &pool_align) < 0) {
  50. return NULL;
  51. }
  52. if (pool_channels != channels || pool_nb_samples < nb_samples ||
  53. pool_format != link->format || pool_align != BUFFER_ALIGN) {
  54. ff_frame_pool_uninit((FFFramePool **)&link->frame_pool);
  55. link->frame_pool = ff_frame_pool_audio_init(av_buffer_allocz, channels,
  56. nb_samples, link->format, BUFFER_ALIGN);
  57. if (!link->frame_pool)
  58. return NULL;
  59. }
  60. }
  61. frame = ff_frame_pool_get(link->frame_pool);
  62. if (!frame)
  63. return NULL;
  64. frame->nb_samples = nb_samples;
  65. frame->channel_layout = link->channel_layout;
  66. frame->sample_rate = link->sample_rate;
  67. av_samples_set_silence(frame->extended_data, 0, nb_samples, channels, link->format);
  68. return frame;
  69. }
  70. AVFrame *ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
  71. {
  72. AVFrame *ret = NULL;
  73. if (link->dstpad->get_audio_buffer)
  74. ret = link->dstpad->get_audio_buffer(link, nb_samples);
  75. if (!ret)
  76. ret = ff_default_get_audio_buffer(link, nb_samples);
  77. return ret;
  78. }