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.

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