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.

182 lines
6.5KB

  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. int avfilter_ref_get_channels(AVFilterBufferRef *ref)
  28. {
  29. return ref->audio ? ref->audio->channels : 0;
  30. }
  31. AVFilterBufferRef *ff_null_get_audio_buffer(AVFilterLink *link, int perms,
  32. int nb_samples)
  33. {
  34. return ff_get_audio_buffer(link->dst->outputs[0], perms, nb_samples);
  35. }
  36. AVFilterBufferRef *ff_default_get_audio_buffer(AVFilterLink *link, int perms,
  37. int nb_samples)
  38. {
  39. AVFilterBufferRef *samplesref = NULL;
  40. uint8_t **data;
  41. int planar = av_sample_fmt_is_planar(link->format);
  42. int nb_channels = link->channels;
  43. int planes = planar ? nb_channels : 1;
  44. int linesize;
  45. int full_perms = AV_PERM_READ | AV_PERM_WRITE | AV_PERM_PRESERVE |
  46. AV_PERM_REUSE | AV_PERM_REUSE2 | AV_PERM_ALIGN;
  47. av_assert1(!(perms & ~(full_perms | AV_PERM_NEG_LINESIZES)));
  48. if (!(data = av_mallocz(sizeof(*data) * planes)))
  49. goto fail;
  50. if (av_samples_alloc(data, &linesize, nb_channels, nb_samples, link->format, 0) < 0)
  51. goto fail;
  52. samplesref = avfilter_get_audio_buffer_ref_from_arrays_channels(
  53. data, linesize, full_perms, nb_samples, link->format,
  54. link->channels, link->channel_layout);
  55. if (!samplesref)
  56. goto fail;
  57. samplesref->audio->sample_rate = link->sample_rate;
  58. av_freep(&data);
  59. fail:
  60. if (data)
  61. av_freep(&data[0]);
  62. av_freep(&data);
  63. return samplesref;
  64. }
  65. AVFilterBufferRef *ff_get_audio_buffer(AVFilterLink *link, int perms,
  66. int nb_samples)
  67. {
  68. AVFilterBufferRef *ret = NULL;
  69. if (link->dstpad->get_audio_buffer)
  70. ret = link->dstpad->get_audio_buffer(link, perms, nb_samples);
  71. if (!ret)
  72. ret = ff_default_get_audio_buffer(link, perms, nb_samples);
  73. if (ret)
  74. ret->type = AVMEDIA_TYPE_AUDIO;
  75. return ret;
  76. }
  77. AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays_channels(uint8_t **data,
  78. int linesize,
  79. int perms,
  80. int nb_samples,
  81. enum AVSampleFormat sample_fmt,
  82. int channels,
  83. uint64_t channel_layout)
  84. {
  85. int planes;
  86. AVFilterBuffer *samples = av_mallocz(sizeof(*samples));
  87. AVFilterBufferRef *samplesref = av_mallocz(sizeof(*samplesref));
  88. if (!samples || !samplesref)
  89. goto fail;
  90. av_assert0(channels);
  91. av_assert0(channel_layout == 0 ||
  92. channels == av_get_channel_layout_nb_channels(channel_layout));
  93. samplesref->buf = samples;
  94. samplesref->buf->free = ff_avfilter_default_free_buffer;
  95. if (!(samplesref->audio = av_mallocz(sizeof(*samplesref->audio))))
  96. goto fail;
  97. samplesref->audio->nb_samples = nb_samples;
  98. samplesref->audio->channel_layout = channel_layout;
  99. samplesref->audio->channels = channels;
  100. planes = av_sample_fmt_is_planar(sample_fmt) ? channels : 1;
  101. /* make sure the buffer gets read permission or it's useless for output */
  102. samplesref->perms = perms | AV_PERM_READ;
  103. samples->refcount = 1;
  104. samplesref->type = AVMEDIA_TYPE_AUDIO;
  105. samplesref->format = sample_fmt;
  106. memcpy(samples->data, data,
  107. FFMIN(FF_ARRAY_ELEMS(samples->data), planes)*sizeof(samples->data[0]));
  108. memcpy(samplesref->data, samples->data, sizeof(samples->data));
  109. samples->linesize[0] = samplesref->linesize[0] = linesize;
  110. if (planes > FF_ARRAY_ELEMS(samples->data)) {
  111. samples-> extended_data = av_mallocz(sizeof(*samples->extended_data) *
  112. planes);
  113. samplesref->extended_data = av_mallocz(sizeof(*samplesref->extended_data) *
  114. planes);
  115. if (!samples->extended_data || !samplesref->extended_data)
  116. goto fail;
  117. memcpy(samples-> extended_data, data, sizeof(*data)*planes);
  118. memcpy(samplesref->extended_data, data, sizeof(*data)*planes);
  119. } else {
  120. samples->extended_data = samples->data;
  121. samplesref->extended_data = samplesref->data;
  122. }
  123. samplesref->pts = AV_NOPTS_VALUE;
  124. return samplesref;
  125. fail:
  126. if (samples && samples->extended_data != samples->data)
  127. av_freep(&samples->extended_data);
  128. if (samplesref) {
  129. av_freep(&samplesref->audio);
  130. if (samplesref->extended_data != samplesref->data)
  131. av_freep(&samplesref->extended_data);
  132. }
  133. av_freep(&samplesref);
  134. av_freep(&samples);
  135. return NULL;
  136. }
  137. AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
  138. int linesize,int perms,
  139. int nb_samples,
  140. enum AVSampleFormat sample_fmt,
  141. uint64_t channel_layout)
  142. {
  143. int channels = av_get_channel_layout_nb_channels(channel_layout);
  144. return avfilter_get_audio_buffer_ref_from_arrays_channels(data, linesize, perms,
  145. nb_samples, sample_fmt,
  146. channels, channel_layout);
  147. }