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.

173 lines
6.1KB

  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 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. frame->nb_samples = nb_samples;
  45. frame->format = link->format;
  46. av_frame_set_channels(frame, link->channels);
  47. frame->channel_layout = link->channel_layout;
  48. frame->sample_rate = link->sample_rate;
  49. ret = av_frame_get_buffer(frame, 0);
  50. if (ret < 0) {
  51. av_frame_free(&frame);
  52. return NULL;
  53. }
  54. av_samples_set_silence(frame->extended_data, 0, nb_samples, channels,
  55. link->format);
  56. return frame;
  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. FF_DISABLE_DEPRECATION_WARNINGS
  69. AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays_channels(uint8_t **data,
  70. int linesize,int perms,
  71. int nb_samples,
  72. enum AVSampleFormat sample_fmt,
  73. int channels,
  74. uint64_t channel_layout)
  75. {
  76. int planes;
  77. AVFilterBuffer *samples = av_mallocz(sizeof(*samples));
  78. AVFilterBufferRef *samplesref = av_mallocz(sizeof(*samplesref));
  79. if (!samples || !samplesref)
  80. goto fail;
  81. av_assert0(channels);
  82. av_assert0(channel_layout == 0 ||
  83. channels == av_get_channel_layout_nb_channels(channel_layout));
  84. samplesref->buf = samples;
  85. samplesref->buf->free = ff_avfilter_default_free_buffer;
  86. if (!(samplesref->audio = av_mallocz(sizeof(*samplesref->audio))))
  87. goto fail;
  88. samplesref->audio->nb_samples = nb_samples;
  89. samplesref->audio->channel_layout = channel_layout;
  90. samplesref->audio->channels = channels;
  91. planes = av_sample_fmt_is_planar(sample_fmt) ? channels : 1;
  92. /* make sure the buffer gets read permission or it's useless for output */
  93. samplesref->perms = perms | AV_PERM_READ;
  94. samples->refcount = 1;
  95. samplesref->type = AVMEDIA_TYPE_AUDIO;
  96. samplesref->format = sample_fmt;
  97. memcpy(samples->data, data,
  98. FFMIN(FF_ARRAY_ELEMS(samples->data), planes)*sizeof(samples->data[0]));
  99. memcpy(samplesref->data, samples->data, sizeof(samples->data));
  100. samples->linesize[0] = samplesref->linesize[0] = linesize;
  101. if (planes > FF_ARRAY_ELEMS(samples->data)) {
  102. samples-> extended_data = av_mallocz_array(sizeof(*samples->extended_data),
  103. planes);
  104. samplesref->extended_data = av_mallocz_array(sizeof(*samplesref->extended_data),
  105. planes);
  106. if (!samples->extended_data || !samplesref->extended_data)
  107. goto fail;
  108. memcpy(samples-> extended_data, data, sizeof(*data)*planes);
  109. memcpy(samplesref->extended_data, data, sizeof(*data)*planes);
  110. } else {
  111. samples->extended_data = samples->data;
  112. samplesref->extended_data = samplesref->data;
  113. }
  114. samplesref->pts = AV_NOPTS_VALUE;
  115. return samplesref;
  116. fail:
  117. if (samples && samples->extended_data != samples->data)
  118. av_freep(&samples->extended_data);
  119. if (samplesref) {
  120. av_freep(&samplesref->audio);
  121. if (samplesref->extended_data != samplesref->data)
  122. av_freep(&samplesref->extended_data);
  123. }
  124. av_freep(&samplesref);
  125. av_freep(&samples);
  126. return NULL;
  127. }
  128. AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
  129. int linesize,int perms,
  130. int nb_samples,
  131. enum AVSampleFormat sample_fmt,
  132. uint64_t channel_layout)
  133. {
  134. int channels = av_get_channel_layout_nb_channels(channel_layout);
  135. return avfilter_get_audio_buffer_ref_from_arrays_channels(data, linesize, perms,
  136. nb_samples, sample_fmt,
  137. channels, channel_layout);
  138. }
  139. FF_ENABLE_DEPRECATION_WARNINGS
  140. #endif