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.

273 lines
8.8KB

  1. /*
  2. *
  3. * This file is part of Libav.
  4. *
  5. * Libav is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * Libav is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with Libav; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /**
  20. * @file
  21. * sample format and channel layout conversion audio filter
  22. */
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/mathematics.h"
  27. #include "libavutil/opt.h"
  28. #include "libavresample/avresample.h"
  29. #include "audio.h"
  30. #include "avfilter.h"
  31. #include "formats.h"
  32. #include "internal.h"
  33. typedef struct ResampleContext {
  34. AVAudioResampleContext *avr;
  35. int64_t next_pts;
  36. /* set by filter_frame() to signal an output frame to request_frame() */
  37. int got_output;
  38. } ResampleContext;
  39. static av_cold void uninit(AVFilterContext *ctx)
  40. {
  41. ResampleContext *s = ctx->priv;
  42. if (s->avr) {
  43. avresample_close(s->avr);
  44. avresample_free(&s->avr);
  45. }
  46. }
  47. static int query_formats(AVFilterContext *ctx)
  48. {
  49. AVFilterLink *inlink = ctx->inputs[0];
  50. AVFilterLink *outlink = ctx->outputs[0];
  51. AVFilterFormats *in_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
  52. AVFilterFormats *out_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
  53. AVFilterFormats *in_samplerates = ff_all_samplerates();
  54. AVFilterFormats *out_samplerates = ff_all_samplerates();
  55. AVFilterChannelLayouts *in_layouts = ff_all_channel_layouts();
  56. AVFilterChannelLayouts *out_layouts = ff_all_channel_layouts();
  57. ff_formats_ref(in_formats, &inlink->out_formats);
  58. ff_formats_ref(out_formats, &outlink->in_formats);
  59. ff_formats_ref(in_samplerates, &inlink->out_samplerates);
  60. ff_formats_ref(out_samplerates, &outlink->in_samplerates);
  61. ff_channel_layouts_ref(in_layouts, &inlink->out_channel_layouts);
  62. ff_channel_layouts_ref(out_layouts, &outlink->in_channel_layouts);
  63. return 0;
  64. }
  65. static int config_output(AVFilterLink *outlink)
  66. {
  67. AVFilterContext *ctx = outlink->src;
  68. AVFilterLink *inlink = ctx->inputs[0];
  69. ResampleContext *s = ctx->priv;
  70. char buf1[64], buf2[64];
  71. int ret;
  72. if (s->avr) {
  73. avresample_close(s->avr);
  74. avresample_free(&s->avr);
  75. }
  76. if (inlink->channel_layout == outlink->channel_layout &&
  77. inlink->sample_rate == outlink->sample_rate &&
  78. (inlink->format == outlink->format ||
  79. (av_get_channel_layout_nb_channels(inlink->channel_layout) == 1 &&
  80. av_get_channel_layout_nb_channels(outlink->channel_layout) == 1 &&
  81. av_get_planar_sample_fmt(inlink->format) ==
  82. av_get_planar_sample_fmt(outlink->format))))
  83. return 0;
  84. if (!(s->avr = avresample_alloc_context()))
  85. return AVERROR(ENOMEM);
  86. av_opt_set_int(s->avr, "in_channel_layout", inlink ->channel_layout, 0);
  87. av_opt_set_int(s->avr, "out_channel_layout", outlink->channel_layout, 0);
  88. av_opt_set_int(s->avr, "in_sample_fmt", inlink ->format, 0);
  89. av_opt_set_int(s->avr, "out_sample_fmt", outlink->format, 0);
  90. av_opt_set_int(s->avr, "in_sample_rate", inlink ->sample_rate, 0);
  91. av_opt_set_int(s->avr, "out_sample_rate", outlink->sample_rate, 0);
  92. if ((ret = avresample_open(s->avr)) < 0)
  93. return ret;
  94. outlink->time_base = (AVRational){ 1, outlink->sample_rate };
  95. s->next_pts = AV_NOPTS_VALUE;
  96. av_get_channel_layout_string(buf1, sizeof(buf1),
  97. -1, inlink ->channel_layout);
  98. av_get_channel_layout_string(buf2, sizeof(buf2),
  99. -1, outlink->channel_layout);
  100. av_log(ctx, AV_LOG_VERBOSE,
  101. "fmt:%s srate:%d cl:%s -> fmt:%s srate:%d cl:%s\n",
  102. av_get_sample_fmt_name(inlink ->format), inlink ->sample_rate, buf1,
  103. av_get_sample_fmt_name(outlink->format), outlink->sample_rate, buf2);
  104. return 0;
  105. }
  106. static int request_frame(AVFilterLink *outlink)
  107. {
  108. AVFilterContext *ctx = outlink->src;
  109. ResampleContext *s = ctx->priv;
  110. int ret = 0;
  111. s->got_output = 0;
  112. while (ret >= 0 && !s->got_output)
  113. ret = ff_request_frame(ctx->inputs[0]);
  114. /* flush the lavr delay buffer */
  115. if (ret == AVERROR_EOF && s->avr) {
  116. AVFilterBufferRef *buf;
  117. int nb_samples = av_rescale_rnd(avresample_get_delay(s->avr),
  118. outlink->sample_rate,
  119. ctx->inputs[0]->sample_rate,
  120. AV_ROUND_UP);
  121. if (!nb_samples)
  122. return ret;
  123. buf = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
  124. if (!buf)
  125. return AVERROR(ENOMEM);
  126. ret = avresample_convert(s->avr, buf->extended_data,
  127. buf->linesize[0], nb_samples,
  128. NULL, 0, 0);
  129. if (ret <= 0) {
  130. avfilter_unref_buffer(buf);
  131. return (ret == 0) ? AVERROR_EOF : ret;
  132. }
  133. buf->pts = s->next_pts;
  134. return ff_filter_frame(outlink, buf);
  135. }
  136. return ret;
  137. }
  138. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
  139. {
  140. AVFilterContext *ctx = inlink->dst;
  141. ResampleContext *s = ctx->priv;
  142. AVFilterLink *outlink = ctx->outputs[0];
  143. int ret;
  144. if (s->avr) {
  145. AVFilterBufferRef *buf_out;
  146. int delay, nb_samples;
  147. /* maximum possible samples lavr can output */
  148. delay = avresample_get_delay(s->avr);
  149. nb_samples = av_rescale_rnd(buf->audio->nb_samples + delay,
  150. outlink->sample_rate, inlink->sample_rate,
  151. AV_ROUND_UP);
  152. buf_out = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
  153. if (!buf_out) {
  154. ret = AVERROR(ENOMEM);
  155. goto fail;
  156. }
  157. ret = avresample_convert(s->avr, buf_out->extended_data,
  158. buf_out->linesize[0], nb_samples,
  159. buf->extended_data, buf->linesize[0],
  160. buf->audio->nb_samples);
  161. if (ret <= 0) {
  162. avfilter_unref_buffer(buf_out);
  163. if (ret < 0)
  164. goto fail;
  165. }
  166. av_assert0(!avresample_available(s->avr));
  167. if (s->next_pts == AV_NOPTS_VALUE) {
  168. if (buf->pts == AV_NOPTS_VALUE) {
  169. av_log(ctx, AV_LOG_WARNING, "First timestamp is missing, "
  170. "assuming 0.\n");
  171. s->next_pts = 0;
  172. } else
  173. s->next_pts = av_rescale_q(buf->pts, inlink->time_base,
  174. outlink->time_base);
  175. }
  176. if (ret > 0) {
  177. buf_out->audio->nb_samples = ret;
  178. if (buf->pts != AV_NOPTS_VALUE) {
  179. buf_out->pts = av_rescale_q(buf->pts, inlink->time_base,
  180. outlink->time_base) -
  181. av_rescale(delay, outlink->sample_rate,
  182. inlink->sample_rate);
  183. } else
  184. buf_out->pts = s->next_pts;
  185. s->next_pts = buf_out->pts + buf_out->audio->nb_samples;
  186. ret = ff_filter_frame(outlink, buf_out);
  187. s->got_output = 1;
  188. }
  189. fail:
  190. avfilter_unref_buffer(buf);
  191. } else {
  192. buf->format = outlink->format;
  193. ret = ff_filter_frame(outlink, buf);
  194. s->got_output = 1;
  195. }
  196. return ret;
  197. }
  198. static const AVFilterPad avfilter_af_resample_inputs[] = {
  199. {
  200. .name = "default",
  201. .type = AVMEDIA_TYPE_AUDIO,
  202. .filter_frame = filter_frame,
  203. .min_perms = AV_PERM_READ
  204. },
  205. { NULL }
  206. };
  207. static const AVFilterPad avfilter_af_resample_outputs[] = {
  208. {
  209. .name = "default",
  210. .type = AVMEDIA_TYPE_AUDIO,
  211. .config_props = config_output,
  212. .request_frame = request_frame
  213. },
  214. { NULL }
  215. };
  216. AVFilter avfilter_af_resample = {
  217. .name = "resample",
  218. .description = NULL_IF_CONFIG_SMALL("Audio resampling and conversion."),
  219. .priv_size = sizeof(ResampleContext),
  220. .uninit = uninit,
  221. .query_formats = query_formats,
  222. .inputs = avfilter_af_resample_inputs,
  223. .outputs = avfilter_af_resample_outputs,
  224. };