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.

259 lines
9.3KB

  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 "libavutil/common.h"
  24. #include "audio.h"
  25. #include "avfilter.h"
  26. #include "internal.h"
  27. AVFilterBufferRef *ff_null_get_audio_buffer(AVFilterLink *link, int perms,
  28. int nb_samples)
  29. {
  30. return ff_get_audio_buffer(link->dst->outputs[0], perms, nb_samples);
  31. }
  32. AVFilterBufferRef *ff_default_get_audio_buffer(AVFilterLink *link, int perms,
  33. int nb_samples)
  34. {
  35. AVFilterBufferRef *samplesref = NULL;
  36. uint8_t **data;
  37. int planar = av_sample_fmt_is_planar(link->format);
  38. int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
  39. int planes = planar ? nb_channels : 1;
  40. int linesize;
  41. int full_perms = AV_PERM_READ | AV_PERM_WRITE | AV_PERM_PRESERVE |
  42. AV_PERM_REUSE | AV_PERM_REUSE2 | AV_PERM_ALIGN;
  43. av_assert1(!(perms & ~(full_perms | AV_PERM_NEG_LINESIZES)));
  44. if (!(data = av_mallocz(sizeof(*data) * planes)))
  45. goto fail;
  46. if (av_samples_alloc(data, &linesize, nb_channels, nb_samples, link->format, 0) < 0)
  47. goto fail;
  48. samplesref = avfilter_get_audio_buffer_ref_from_arrays(data, linesize, full_perms,
  49. nb_samples, link->format,
  50. link->channel_layout);
  51. if (!samplesref)
  52. goto fail;
  53. av_freep(&data);
  54. fail:
  55. if (data)
  56. av_freep(&data[0]);
  57. av_freep(&data);
  58. return samplesref;
  59. }
  60. AVFilterBufferRef *ff_get_audio_buffer(AVFilterLink *link, int perms,
  61. int nb_samples)
  62. {
  63. AVFilterBufferRef *ret = NULL;
  64. if (link->dstpad->get_audio_buffer)
  65. ret = link->dstpad->get_audio_buffer(link, perms, nb_samples);
  66. if (!ret)
  67. ret = ff_default_get_audio_buffer(link, perms, nb_samples);
  68. if (ret)
  69. ret->type = AVMEDIA_TYPE_AUDIO;
  70. return ret;
  71. }
  72. AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
  73. int linesize,int perms,
  74. int nb_samples,
  75. enum AVSampleFormat sample_fmt,
  76. uint64_t channel_layout)
  77. {
  78. int planes;
  79. AVFilterBuffer *samples = av_mallocz(sizeof(*samples));
  80. AVFilterBufferRef *samplesref = av_mallocz(sizeof(*samplesref));
  81. if (!samples || !samplesref)
  82. goto fail;
  83. samplesref->buf = samples;
  84. samplesref->buf->free = ff_avfilter_default_free_buffer;
  85. if (!(samplesref->audio = av_mallocz(sizeof(*samplesref->audio))))
  86. goto fail;
  87. samplesref->audio->nb_samples = nb_samples;
  88. samplesref->audio->channel_layout = channel_layout;
  89. planes = av_sample_fmt_is_planar(sample_fmt) ?
  90. av_get_channel_layout_nb_channels(channel_layout) : 1;
  91. /* make sure the buffer gets read permission or it's useless for output */
  92. samplesref->perms = perms | AV_PERM_READ;
  93. samples->refcount = 1;
  94. samplesref->type = AVMEDIA_TYPE_AUDIO;
  95. samplesref->format = sample_fmt;
  96. memcpy(samples->data, data,
  97. FFMIN(FF_ARRAY_ELEMS(samples->data), planes)*sizeof(samples->data[0]));
  98. memcpy(samplesref->data, samples->data, sizeof(samples->data));
  99. samples->linesize[0] = samplesref->linesize[0] = linesize;
  100. if (planes > FF_ARRAY_ELEMS(samples->data)) {
  101. samples-> extended_data = av_mallocz(sizeof(*samples->extended_data) *
  102. planes);
  103. samplesref->extended_data = av_mallocz(sizeof(*samplesref->extended_data) *
  104. planes);
  105. if (!samples->extended_data || !samplesref->extended_data)
  106. goto fail;
  107. memcpy(samples-> extended_data, data, sizeof(*data)*planes);
  108. memcpy(samplesref->extended_data, data, sizeof(*data)*planes);
  109. } else {
  110. samples->extended_data = samples->data;
  111. samplesref->extended_data = samplesref->data;
  112. }
  113. samplesref->pts = AV_NOPTS_VALUE;
  114. return samplesref;
  115. fail:
  116. if (samples && samples->extended_data != samples->data)
  117. av_freep(&samples->extended_data);
  118. if (samplesref) {
  119. av_freep(&samplesref->audio);
  120. if (samplesref->extended_data != samplesref->data)
  121. av_freep(&samplesref->extended_data);
  122. }
  123. av_freep(&samplesref);
  124. av_freep(&samples);
  125. return NULL;
  126. }
  127. static int default_filter_samples(AVFilterLink *link,
  128. AVFilterBufferRef *samplesref)
  129. {
  130. return ff_filter_samples(link->dst->outputs[0], samplesref);
  131. }
  132. int ff_filter_samples_framed(AVFilterLink *link, AVFilterBufferRef *samplesref)
  133. {
  134. int (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
  135. AVFilterPad *src = link->srcpad;
  136. AVFilterPad *dst = link->dstpad;
  137. int64_t pts;
  138. AVFilterBufferRef *buf_out;
  139. int ret;
  140. FF_TPRINTF_START(NULL, filter_samples); ff_tlog_link(NULL, link, 1);
  141. if (!(filter_samples = dst->filter_samples))
  142. filter_samples = default_filter_samples;
  143. av_assert1((samplesref->perms & src->min_perms) == src->min_perms);
  144. samplesref->perms &= ~ src->rej_perms;
  145. /* prepare to copy the samples if the buffer has insufficient permissions */
  146. if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
  147. dst->rej_perms & samplesref->perms) {
  148. av_log(link->dst, AV_LOG_DEBUG,
  149. "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
  150. samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
  151. buf_out = ff_default_get_audio_buffer(link, dst->min_perms,
  152. samplesref->audio->nb_samples);
  153. if (!buf_out) {
  154. avfilter_unref_buffer(samplesref);
  155. return AVERROR(ENOMEM);
  156. }
  157. buf_out->pts = samplesref->pts;
  158. buf_out->audio->sample_rate = samplesref->audio->sample_rate;
  159. /* Copy actual data into new samples buffer */
  160. av_samples_copy(buf_out->extended_data, samplesref->extended_data,
  161. 0, 0, samplesref->audio->nb_samples,
  162. av_get_channel_layout_nb_channels(link->channel_layout),
  163. link->format);
  164. avfilter_unref_buffer(samplesref);
  165. } else
  166. buf_out = samplesref;
  167. link->cur_buf = buf_out;
  168. pts = buf_out->pts;
  169. ret = filter_samples(link, buf_out);
  170. ff_update_link_current_pts(link, pts);
  171. return ret;
  172. }
  173. int ff_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  174. {
  175. int insamples = samplesref->audio->nb_samples, inpos = 0, nb_samples;
  176. AVFilterBufferRef *pbuf = link->partial_buf;
  177. int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
  178. int ret = 0;
  179. if (!link->min_samples ||
  180. (!pbuf &&
  181. insamples >= link->min_samples && insamples <= link->max_samples)) {
  182. return ff_filter_samples_framed(link, samplesref);
  183. }
  184. /* Handle framing (min_samples, max_samples) */
  185. while (insamples) {
  186. if (!pbuf) {
  187. AVRational samples_tb = { 1, link->sample_rate };
  188. int perms = link->dstpad->min_perms | AV_PERM_WRITE;
  189. pbuf = ff_get_audio_buffer(link, perms, link->partial_buf_size);
  190. if (!pbuf) {
  191. av_log(link->dst, AV_LOG_WARNING,
  192. "Samples dropped due to memory allocation failure.\n");
  193. return 0;
  194. }
  195. avfilter_copy_buffer_ref_props(pbuf, samplesref);
  196. pbuf->pts = samplesref->pts +
  197. av_rescale_q(inpos, samples_tb, link->time_base);
  198. pbuf->audio->nb_samples = 0;
  199. }
  200. nb_samples = FFMIN(insamples,
  201. link->partial_buf_size - pbuf->audio->nb_samples);
  202. av_samples_copy(pbuf->extended_data, samplesref->extended_data,
  203. pbuf->audio->nb_samples, inpos,
  204. nb_samples, nb_channels, link->format);
  205. inpos += nb_samples;
  206. insamples -= nb_samples;
  207. pbuf->audio->nb_samples += nb_samples;
  208. if (pbuf->audio->nb_samples >= link->min_samples) {
  209. ret = ff_filter_samples_framed(link, pbuf);
  210. pbuf = NULL;
  211. }
  212. }
  213. avfilter_unref_buffer(samplesref);
  214. link->partial_buf = pbuf;
  215. return ret;
  216. }