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.

155 lines
5.1KB

  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 buf_size, ret;
  32. if (!frame)
  33. return NULL;
  34. buf_size = av_samples_get_buffer_size(NULL, channels, nb_samples,
  35. link->format, 0);
  36. if (buf_size < 0)
  37. goto fail;
  38. frame->buf[0] = av_buffer_alloc(buf_size);
  39. if (!frame->buf[0])
  40. goto fail;
  41. frame->nb_samples = nb_samples;
  42. ret = avcodec_fill_audio_frame(frame, channels, link->format,
  43. frame->buf[0]->data, buf_size, 0);
  44. if (ret < 0)
  45. goto fail;
  46. av_samples_set_silence(frame->extended_data, 0, nb_samples, channels,
  47. link->format);
  48. frame->nb_samples = nb_samples;
  49. frame->format = link->format;
  50. frame->channel_layout = link->channel_layout;
  51. frame->sample_rate = link->sample_rate;
  52. return frame;
  53. fail:
  54. av_buffer_unref(&frame->buf[0]);
  55. av_frame_free(&frame);
  56. return NULL;
  57. }
  58. AVFrame *ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
  59. {
  60. AVFrame *ret = NULL;
  61. if (link->dstpad->get_audio_buffer)
  62. ret = link->dstpad->get_audio_buffer(link, nb_samples);
  63. if (!ret)
  64. ret = ff_default_get_audio_buffer(link, nb_samples);
  65. return ret;
  66. }
  67. #if FF_API_AVFILTERBUFFER
  68. AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
  69. int linesize,int perms,
  70. int nb_samples,
  71. enum AVSampleFormat sample_fmt,
  72. uint64_t channel_layout)
  73. {
  74. int planes;
  75. AVFilterBuffer *samples = av_mallocz(sizeof(*samples));
  76. AVFilterBufferRef *samplesref = av_mallocz(sizeof(*samplesref));
  77. if (!samples || !samplesref)
  78. goto fail;
  79. samplesref->buf = samples;
  80. samplesref->buf->free = ff_avfilter_default_free_buffer;
  81. if (!(samplesref->audio = av_mallocz(sizeof(*samplesref->audio))))
  82. goto fail;
  83. samplesref->audio->nb_samples = nb_samples;
  84. samplesref->audio->channel_layout = channel_layout;
  85. samplesref->audio->planar = av_sample_fmt_is_planar(sample_fmt);
  86. planes = samplesref->audio->planar ? av_get_channel_layout_nb_channels(channel_layout) : 1;
  87. /* make sure the buffer gets read permission or it's useless for output */
  88. samplesref->perms = perms | AV_PERM_READ;
  89. samples->refcount = 1;
  90. samplesref->type = AVMEDIA_TYPE_AUDIO;
  91. samplesref->format = sample_fmt;
  92. memcpy(samples->data, data,
  93. FFMIN(FF_ARRAY_ELEMS(samples->data), planes)*sizeof(samples->data[0]));
  94. memcpy(samplesref->data, samples->data, sizeof(samples->data));
  95. samples->linesize[0] = samplesref->linesize[0] = linesize;
  96. if (planes > FF_ARRAY_ELEMS(samples->data)) {
  97. samples-> extended_data = av_mallocz(sizeof(*samples->extended_data) *
  98. planes);
  99. samplesref->extended_data = av_mallocz(sizeof(*samplesref->extended_data) *
  100. planes);
  101. if (!samples->extended_data || !samplesref->extended_data)
  102. goto fail;
  103. memcpy(samples-> extended_data, data, sizeof(*data)*planes);
  104. memcpy(samplesref->extended_data, data, sizeof(*data)*planes);
  105. } else {
  106. samples->extended_data = samples->data;
  107. samplesref->extended_data = samplesref->data;
  108. }
  109. samplesref->pts = AV_NOPTS_VALUE;
  110. return samplesref;
  111. fail:
  112. if (samples && samples->extended_data != samples->data)
  113. av_freep(&samples->extended_data);
  114. if (samplesref) {
  115. av_freep(&samplesref->audio);
  116. if (samplesref->extended_data != samplesref->data)
  117. av_freep(&samplesref->extended_data);
  118. }
  119. av_freep(&samplesref);
  120. av_freep(&samples);
  121. return NULL;
  122. }
  123. #endif