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.

231 lines
8.1KB

  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/mathematics.h"
  26. #include "libavutil/opt.h"
  27. #include "libavresample/avresample.h"
  28. #include "audio.h"
  29. #include "avfilter.h"
  30. #include "formats.h"
  31. #include "internal.h"
  32. typedef struct ResampleContext {
  33. AVAudioResampleContext *avr;
  34. int64_t next_pts;
  35. } ResampleContext;
  36. static av_cold void uninit(AVFilterContext *ctx)
  37. {
  38. ResampleContext *s = ctx->priv;
  39. if (s->avr) {
  40. avresample_close(s->avr);
  41. avresample_free(&s->avr);
  42. }
  43. }
  44. static int query_formats(AVFilterContext *ctx)
  45. {
  46. AVFilterLink *inlink = ctx->inputs[0];
  47. AVFilterLink *outlink = ctx->outputs[0];
  48. AVFilterFormats *in_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
  49. AVFilterFormats *out_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
  50. AVFilterFormats *in_samplerates = ff_all_samplerates();
  51. AVFilterFormats *out_samplerates = ff_all_samplerates();
  52. AVFilterChannelLayouts *in_layouts = ff_all_channel_layouts();
  53. AVFilterChannelLayouts *out_layouts = ff_all_channel_layouts();
  54. ff_formats_ref(in_formats, &inlink->out_formats);
  55. ff_formats_ref(out_formats, &outlink->in_formats);
  56. ff_formats_ref(in_samplerates, &inlink->out_samplerates);
  57. ff_formats_ref(out_samplerates, &outlink->in_samplerates);
  58. ff_channel_layouts_ref(in_layouts, &inlink->out_channel_layouts);
  59. ff_channel_layouts_ref(out_layouts, &outlink->in_channel_layouts);
  60. return 0;
  61. }
  62. static int config_output(AVFilterLink *outlink)
  63. {
  64. AVFilterContext *ctx = outlink->src;
  65. AVFilterLink *inlink = ctx->inputs[0];
  66. ResampleContext *s = ctx->priv;
  67. char buf1[64], buf2[64];
  68. int ret;
  69. if (s->avr) {
  70. avresample_close(s->avr);
  71. avresample_free(&s->avr);
  72. }
  73. if (inlink->channel_layout == outlink->channel_layout &&
  74. inlink->sample_rate == outlink->sample_rate &&
  75. inlink->format == outlink->format)
  76. return 0;
  77. if (!(s->avr = avresample_alloc_context()))
  78. return AVERROR(ENOMEM);
  79. av_opt_set_int(s->avr, "in_channel_layout", inlink ->channel_layout, 0);
  80. av_opt_set_int(s->avr, "out_channel_layout", outlink->channel_layout, 0);
  81. av_opt_set_int(s->avr, "in_sample_fmt", inlink ->format, 0);
  82. av_opt_set_int(s->avr, "out_sample_fmt", outlink->format, 0);
  83. av_opt_set_int(s->avr, "in_sample_rate", inlink ->sample_rate, 0);
  84. av_opt_set_int(s->avr, "out_sample_rate", outlink->sample_rate, 0);
  85. if ((ret = avresample_open(s->avr)) < 0)
  86. return ret;
  87. outlink->time_base = (AVRational){ 1, outlink->sample_rate };
  88. s->next_pts = AV_NOPTS_VALUE;
  89. av_get_channel_layout_string(buf1, sizeof(buf1),
  90. -1, inlink ->channel_layout);
  91. av_get_channel_layout_string(buf2, sizeof(buf2),
  92. -1, outlink->channel_layout);
  93. av_log(ctx, AV_LOG_VERBOSE,
  94. "fmt:%s srate:%d cl:%s -> fmt:%s srate:%d cl:%s\n",
  95. av_get_sample_fmt_name(inlink ->format), inlink ->sample_rate, buf1,
  96. av_get_sample_fmt_name(outlink->format), outlink->sample_rate, buf2);
  97. return 0;
  98. }
  99. static int request_frame(AVFilterLink *outlink)
  100. {
  101. AVFilterContext *ctx = outlink->src;
  102. ResampleContext *s = ctx->priv;
  103. int ret = ff_request_frame(ctx->inputs[0]);
  104. /* flush the lavr delay buffer */
  105. if (ret == AVERROR_EOF && s->avr) {
  106. AVFilterBufferRef *buf;
  107. int nb_samples = av_rescale_rnd(avresample_get_delay(s->avr),
  108. outlink->sample_rate,
  109. ctx->inputs[0]->sample_rate,
  110. AV_ROUND_UP);
  111. if (!nb_samples)
  112. return ret;
  113. buf = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
  114. if (!buf)
  115. return AVERROR(ENOMEM);
  116. ret = avresample_convert(s->avr, (void**)buf->extended_data,
  117. buf->linesize[0], nb_samples,
  118. NULL, 0, 0);
  119. if (ret <= 0) {
  120. avfilter_unref_buffer(buf);
  121. return (ret == 0) ? AVERROR_EOF : ret;
  122. }
  123. buf->pts = s->next_pts;
  124. ff_filter_samples(outlink, buf);
  125. return 0;
  126. }
  127. return ret;
  128. }
  129. static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *buf)
  130. {
  131. AVFilterContext *ctx = inlink->dst;
  132. ResampleContext *s = ctx->priv;
  133. AVFilterLink *outlink = ctx->outputs[0];
  134. if (s->avr) {
  135. AVFilterBufferRef *buf_out;
  136. int delay, nb_samples, ret;
  137. /* maximum possible samples lavr can output */
  138. delay = avresample_get_delay(s->avr);
  139. nb_samples = av_rescale_rnd(buf->audio->nb_samples + delay,
  140. outlink->sample_rate, inlink->sample_rate,
  141. AV_ROUND_UP);
  142. buf_out = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
  143. ret = avresample_convert(s->avr, (void**)buf_out->extended_data,
  144. buf_out->linesize[0], nb_samples,
  145. (void**)buf->extended_data, buf->linesize[0],
  146. buf->audio->nb_samples);
  147. av_assert0(!avresample_available(s->avr));
  148. if (s->next_pts == AV_NOPTS_VALUE) {
  149. if (buf->pts == AV_NOPTS_VALUE) {
  150. av_log(ctx, AV_LOG_WARNING, "First timestamp is missing, "
  151. "assuming 0.\n");
  152. s->next_pts = 0;
  153. } else
  154. s->next_pts = av_rescale_q(buf->pts, inlink->time_base,
  155. outlink->time_base);
  156. }
  157. if (ret > 0) {
  158. buf_out->audio->nb_samples = ret;
  159. if (buf->pts != AV_NOPTS_VALUE) {
  160. buf_out->pts = av_rescale_q(buf->pts, inlink->time_base,
  161. outlink->time_base) -
  162. av_rescale(delay, outlink->sample_rate,
  163. inlink->sample_rate);
  164. } else
  165. buf_out->pts = s->next_pts;
  166. s->next_pts = buf_out->pts + buf_out->audio->nb_samples;
  167. ff_filter_samples(outlink, buf_out);
  168. }
  169. avfilter_unref_buffer(buf);
  170. } else
  171. ff_filter_samples(outlink, buf);
  172. }
  173. AVFilter avfilter_af_resample = {
  174. .name = "resample",
  175. .description = NULL_IF_CONFIG_SMALL("Audio resampling and conversion."),
  176. .priv_size = sizeof(ResampleContext),
  177. .uninit = uninit,
  178. .query_formats = query_formats,
  179. .inputs = (const AVFilterPad[]) {{ .name = "default",
  180. .type = AVMEDIA_TYPE_AUDIO,
  181. .filter_samples = filter_samples,
  182. .min_perms = AV_PERM_READ },
  183. { .name = NULL}},
  184. .outputs = (const AVFilterPad[]) {{ .name = "default",
  185. .type = AVMEDIA_TYPE_AUDIO,
  186. .config_props = config_output,
  187. .request_frame = request_frame },
  188. { .name = NULL}},
  189. };