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.

276 lines
9.9KB

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