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.

226 lines
7.9KB

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