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.

269 lines
9.6KB

  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. 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. samplesref->audio->sample_rate = link->sample_rate;
  54. av_freep(&data);
  55. fail:
  56. if (data)
  57. av_freep(&data[0]);
  58. av_freep(&data);
  59. return samplesref;
  60. }
  61. AVFilterBufferRef *ff_get_audio_buffer(AVFilterLink *link, int perms,
  62. int nb_samples)
  63. {
  64. AVFilterBufferRef *ret = NULL;
  65. if (link->dstpad->get_audio_buffer)
  66. ret = link->dstpad->get_audio_buffer(link, perms, nb_samples);
  67. if (!ret)
  68. ret = ff_default_get_audio_buffer(link, perms, nb_samples);
  69. if (ret)
  70. ret->type = AVMEDIA_TYPE_AUDIO;
  71. return ret;
  72. }
  73. AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
  74. int linesize,int perms,
  75. int nb_samples,
  76. enum AVSampleFormat sample_fmt,
  77. uint64_t channel_layout)
  78. {
  79. int planes;
  80. AVFilterBuffer *samples = av_mallocz(sizeof(*samples));
  81. AVFilterBufferRef *samplesref = av_mallocz(sizeof(*samplesref));
  82. if (!samples || !samplesref)
  83. goto fail;
  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. planes = av_sample_fmt_is_planar(sample_fmt) ?
  91. av_get_channel_layout_nb_channels(channel_layout) : 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(sizeof(*samples->extended_data) *
  103. planes);
  104. samplesref->extended_data = av_mallocz(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. static int default_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
  129. {
  130. return ff_filter_frame(link->dst->outputs[0], frame);
  131. }
  132. int ff_filter_samples_framed(AVFilterLink *link, AVFilterBufferRef *samplesref)
  133. {
  134. int (*filter_frame)(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_frame); ff_tlog_link(NULL, link, 1);
  141. if (link->closed) {
  142. avfilter_unref_buffer(samplesref);
  143. return AVERROR_EOF;
  144. }
  145. if (!(filter_frame = dst->filter_frame))
  146. filter_frame = default_filter_frame;
  147. av_assert1((samplesref->perms & src->min_perms) == src->min_perms);
  148. samplesref->perms &= ~ src->rej_perms;
  149. /* prepare to copy the samples if the buffer has insufficient permissions */
  150. if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
  151. dst->rej_perms & samplesref->perms) {
  152. av_log(link->dst, AV_LOG_DEBUG,
  153. "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
  154. samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
  155. buf_out = ff_default_get_audio_buffer(link, dst->min_perms,
  156. samplesref->audio->nb_samples);
  157. if (!buf_out) {
  158. avfilter_unref_buffer(samplesref);
  159. return AVERROR(ENOMEM);
  160. }
  161. buf_out->pts = samplesref->pts;
  162. buf_out->audio->sample_rate = samplesref->audio->sample_rate;
  163. /* Copy actual data into new samples buffer */
  164. av_samples_copy(buf_out->extended_data, samplesref->extended_data,
  165. 0, 0, samplesref->audio->nb_samples,
  166. av_get_channel_layout_nb_channels(link->channel_layout),
  167. link->format);
  168. avfilter_unref_buffer(samplesref);
  169. } else
  170. buf_out = samplesref;
  171. link->cur_buf = buf_out;
  172. pts = buf_out->pts;
  173. ret = filter_frame(link, buf_out);
  174. ff_update_link_current_pts(link, pts);
  175. return ret;
  176. }
  177. int ff_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
  178. {
  179. int insamples = samplesref->audio->nb_samples, inpos = 0, nb_samples;
  180. AVFilterBufferRef *pbuf = link->partial_buf;
  181. int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
  182. int ret = 0;
  183. av_assert1(samplesref->format == link->format);
  184. av_assert1(samplesref->audio->channel_layout == link->channel_layout);
  185. av_assert1(samplesref->audio->sample_rate == link->sample_rate);
  186. if (!link->min_samples ||
  187. (!pbuf &&
  188. insamples >= link->min_samples && insamples <= link->max_samples)) {
  189. return ff_filter_samples_framed(link, samplesref);
  190. }
  191. /* Handle framing (min_samples, max_samples) */
  192. while (insamples) {
  193. if (!pbuf) {
  194. AVRational samples_tb = { 1, link->sample_rate };
  195. int perms = link->dstpad->min_perms | AV_PERM_WRITE;
  196. pbuf = ff_get_audio_buffer(link, perms, link->partial_buf_size);
  197. if (!pbuf) {
  198. av_log(link->dst, AV_LOG_WARNING,
  199. "Samples dropped due to memory allocation failure.\n");
  200. return 0;
  201. }
  202. avfilter_copy_buffer_ref_props(pbuf, samplesref);
  203. pbuf->pts = samplesref->pts +
  204. av_rescale_q(inpos, samples_tb, link->time_base);
  205. pbuf->audio->nb_samples = 0;
  206. }
  207. nb_samples = FFMIN(insamples,
  208. link->partial_buf_size - pbuf->audio->nb_samples);
  209. av_samples_copy(pbuf->extended_data, samplesref->extended_data,
  210. pbuf->audio->nb_samples, inpos,
  211. nb_samples, nb_channels, link->format);
  212. inpos += nb_samples;
  213. insamples -= nb_samples;
  214. pbuf->audio->nb_samples += nb_samples;
  215. if (pbuf->audio->nb_samples >= link->min_samples) {
  216. ret = ff_filter_samples_framed(link, pbuf);
  217. pbuf = NULL;
  218. }
  219. }
  220. avfilter_unref_buffer(samplesref);
  221. link->partial_buf = pbuf;
  222. return ret;
  223. }