audio.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/audioconvert.h"
  23. #include "audio.h"
  24. #include "avfilter.h"
  25. #include "internal.h"
  26. AVFilterBufferRef *ff_null_get_audio_buffer(AVFilterLink *link, int perms,
  27. int nb_samples)
  28. {
  29. return ff_get_audio_buffer(link->dst->outputs[0], perms, nb_samples);
  30. }
  31. AVFilterBufferRef *ff_default_get_audio_buffer(AVFilterLink *link, int perms,
  32. int nb_samples)
  33. {
  34. AVFilterBufferRef *samplesref = NULL;
  35. uint8_t **data;
  36. int planar = av_sample_fmt_is_planar(link->format);
  37. int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
  38. int planes = planar ? nb_channels : 1;
  39. int linesize;
  40. if (!(data = av_mallocz(sizeof(*data) * planes)))
  41. goto fail;
  42. if (av_samples_alloc(data, &linesize, nb_channels, nb_samples, link->format, 0) < 0)
  43. goto fail;
  44. samplesref = avfilter_get_audio_buffer_ref_from_arrays(data, linesize, perms,
  45. nb_samples, link->format,
  46. link->channel_layout);
  47. if (!samplesref)
  48. goto fail;
  49. av_freep(&data);
  50. fail:
  51. if (data)
  52. av_freep(&data[0]);
  53. av_freep(&data);
  54. return samplesref;
  55. }
  56. AVFilterBufferRef *ff_get_audio_buffer(AVFilterLink *link, int perms,
  57. int nb_samples)
  58. {
  59. AVFilterBufferRef *ret = NULL;
  60. if (link->dstpad->get_audio_buffer)
  61. ret = link->dstpad->get_audio_buffer(link, perms, nb_samples);
  62. if (!ret)
  63. ret = ff_default_get_audio_buffer(link, perms, nb_samples);
  64. if (ret)
  65. ret->type = AVMEDIA_TYPE_AUDIO;
  66. return ret;
  67. }
  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. planes = av_sample_fmt_is_planar(sample_fmt) ?
  86. 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. void ff_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  124. {
  125. ff_filter_samples(link->dst->outputs[0], samplesref);
  126. }
  127. /* FIXME: samplesref is same as link->cur_buf. Need to consider removing the redundant parameter. */
  128. void ff_default_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
  129. {
  130. AVFilterLink *outlink = NULL;
  131. if (inlink->dst->output_count)
  132. outlink = inlink->dst->outputs[0];
  133. if (outlink) {
  134. outlink->out_buf = ff_default_get_audio_buffer(inlink, AV_PERM_WRITE,
  135. samplesref->audio->nb_samples);
  136. outlink->out_buf->pts = samplesref->pts;
  137. outlink->out_buf->audio->sample_rate = samplesref->audio->sample_rate;
  138. ff_filter_samples(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
  139. avfilter_unref_buffer(outlink->out_buf);
  140. outlink->out_buf = NULL;
  141. }
  142. avfilter_unref_buffer(samplesref);
  143. inlink->cur_buf = NULL;
  144. }
  145. void ff_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  146. {
  147. void (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
  148. AVFilterPad *dst = link->dstpad;
  149. int64_t pts;
  150. FF_DPRINTF_START(NULL, filter_samples); ff_dlog_link(NULL, link, 1);
  151. if (!(filter_samples = dst->filter_samples))
  152. filter_samples = ff_default_filter_samples;
  153. /* prepare to copy the samples if the buffer has insufficient permissions */
  154. if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
  155. dst->rej_perms & samplesref->perms) {
  156. int i, planar = av_sample_fmt_is_planar(samplesref->format);
  157. int planes = !planar ? 1:
  158. av_get_channel_layout_nb_channels(samplesref->audio->channel_layout);
  159. av_log(link->dst, AV_LOG_DEBUG,
  160. "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
  161. samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
  162. link->cur_buf = ff_default_get_audio_buffer(link, dst->min_perms,
  163. samplesref->audio->nb_samples);
  164. link->cur_buf->pts = samplesref->pts;
  165. link->cur_buf->audio->sample_rate = samplesref->audio->sample_rate;
  166. /* Copy actual data into new samples buffer */
  167. for (i = 0; i < 8 && samplesref->data[i]; i++)
  168. memcpy(link->cur_buf->data[i], samplesref->data[i], samplesref->linesize[0]);
  169. for (i = 0; i < planes; i++)
  170. memcpy(link->cur_buf->extended_data[i], samplesref->extended_data[i], samplesref->linesize[0]);
  171. avfilter_unref_buffer(samplesref);
  172. } else
  173. link->cur_buf = samplesref;
  174. pts = link->cur_buf->pts;
  175. filter_samples(link, link->cur_buf);
  176. ff_update_link_current_pts(link, pts);
  177. }