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.

254 lines
8.6KB

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