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.

141 lines
4.6KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/channel_layout.h"
  19. #include "libavutil/common.h"
  20. #include "audio.h"
  21. #include "avfilter.h"
  22. #include "internal.h"
  23. AVFrame *ff_null_get_audio_buffer(AVFilterLink *link, int nb_samples)
  24. {
  25. return ff_get_audio_buffer(link->dst->outputs[0], nb_samples);
  26. }
  27. AVFrame *ff_default_get_audio_buffer(AVFilterLink *link, int nb_samples)
  28. {
  29. AVFrame *frame = av_frame_alloc();
  30. int channels = av_get_channel_layout_nb_channels(link->channel_layout);
  31. int ret;
  32. if (!frame)
  33. return NULL;
  34. frame->nb_samples = nb_samples;
  35. frame->format = link->format;
  36. frame->channel_layout = link->channel_layout;
  37. frame->sample_rate = link->sample_rate;
  38. ret = av_frame_get_buffer(frame, 0);
  39. if (ret < 0) {
  40. av_frame_free(&frame);
  41. return NULL;
  42. }
  43. av_samples_set_silence(frame->extended_data, 0, nb_samples, channels,
  44. link->format);
  45. return frame;
  46. }
  47. AVFrame *ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
  48. {
  49. AVFrame *ret = NULL;
  50. if (link->dstpad->get_audio_buffer)
  51. ret = link->dstpad->get_audio_buffer(link, nb_samples);
  52. if (!ret)
  53. ret = ff_default_get_audio_buffer(link, nb_samples);
  54. return ret;
  55. }
  56. #if FF_API_AVFILTERBUFFER
  57. AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
  58. int linesize,int perms,
  59. int nb_samples,
  60. enum AVSampleFormat sample_fmt,
  61. uint64_t channel_layout)
  62. {
  63. int planes;
  64. AVFilterBuffer *samples = av_mallocz(sizeof(*samples));
  65. AVFilterBufferRef *samplesref = av_mallocz(sizeof(*samplesref));
  66. if (!samples || !samplesref)
  67. goto fail;
  68. samplesref->buf = samples;
  69. samplesref->buf->free = ff_avfilter_default_free_buffer;
  70. if (!(samplesref->audio = av_mallocz(sizeof(*samplesref->audio))))
  71. goto fail;
  72. samplesref->audio->nb_samples = nb_samples;
  73. samplesref->audio->channel_layout = channel_layout;
  74. samplesref->audio->planar = av_sample_fmt_is_planar(sample_fmt);
  75. planes = samplesref->audio->planar ? av_get_channel_layout_nb_channels(channel_layout) : 1;
  76. /* make sure the buffer gets read permission or it's useless for output */
  77. samplesref->perms = perms | AV_PERM_READ;
  78. samples->refcount = 1;
  79. samplesref->type = AVMEDIA_TYPE_AUDIO;
  80. samplesref->format = sample_fmt;
  81. memcpy(samples->data, data,
  82. FFMIN(FF_ARRAY_ELEMS(samples->data), planes)*sizeof(samples->data[0]));
  83. memcpy(samplesref->data, samples->data, sizeof(samples->data));
  84. samples->linesize[0] = samplesref->linesize[0] = linesize;
  85. if (planes > FF_ARRAY_ELEMS(samples->data)) {
  86. samples-> extended_data = av_mallocz(sizeof(*samples->extended_data) *
  87. planes);
  88. samplesref->extended_data = av_mallocz(sizeof(*samplesref->extended_data) *
  89. planes);
  90. if (!samples->extended_data || !samplesref->extended_data)
  91. goto fail;
  92. memcpy(samples-> extended_data, data, sizeof(*data)*planes);
  93. memcpy(samplesref->extended_data, data, sizeof(*data)*planes);
  94. } else {
  95. samples->extended_data = samples->data;
  96. samplesref->extended_data = samplesref->data;
  97. }
  98. samplesref->pts = AV_NOPTS_VALUE;
  99. return samplesref;
  100. fail:
  101. if (samples && samples->extended_data != samples->data)
  102. av_freep(&samples->extended_data);
  103. if (samplesref) {
  104. av_freep(&samplesref->audio);
  105. if (samplesref->extended_data != samplesref->data)
  106. av_freep(&samplesref->extended_data);
  107. }
  108. av_freep(&samplesref);
  109. av_freep(&samples);
  110. return NULL;
  111. }
  112. #endif