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.

308 lines
9.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/common.h"
  26. #include "libavutil/dict.h"
  27. #include "libavutil/mathematics.h"
  28. #include "libavutil/opt.h"
  29. #include "libavresample/avresample.h"
  30. #include "audio.h"
  31. #include "avfilter.h"
  32. #include "formats.h"
  33. #include "internal.h"
  34. typedef struct ResampleContext {
  35. AVAudioResampleContext *avr;
  36. AVDictionary *options;
  37. int64_t next_pts;
  38. /* set by filter_frame() to signal an output frame to request_frame() */
  39. int got_output;
  40. } ResampleContext;
  41. static av_cold int init(AVFilterContext *ctx, const char *args)
  42. {
  43. ResampleContext *s = ctx->priv;
  44. if (args) {
  45. int ret = av_dict_parse_string(&s->options, args, "=", ":", 0);
  46. if (ret < 0) {
  47. av_log(ctx, AV_LOG_ERROR, "error setting option string: %s\n", args);
  48. return ret;
  49. }
  50. /* do not allow the user to override basic format options */
  51. av_dict_set(&s->options, "in_channel_layout", NULL, 0);
  52. av_dict_set(&s->options, "out_channel_layout", NULL, 0);
  53. av_dict_set(&s->options, "in_sample_fmt", NULL, 0);
  54. av_dict_set(&s->options, "out_sample_fmt", NULL, 0);
  55. av_dict_set(&s->options, "in_sample_rate", NULL, 0);
  56. av_dict_set(&s->options, "out_sample_rate", NULL, 0);
  57. }
  58. return 0;
  59. }
  60. static av_cold void uninit(AVFilterContext *ctx)
  61. {
  62. ResampleContext *s = ctx->priv;
  63. if (s->avr) {
  64. avresample_close(s->avr);
  65. avresample_free(&s->avr);
  66. }
  67. av_dict_free(&s->options);
  68. }
  69. static int query_formats(AVFilterContext *ctx)
  70. {
  71. AVFilterLink *inlink = ctx->inputs[0];
  72. AVFilterLink *outlink = ctx->outputs[0];
  73. AVFilterFormats *in_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
  74. AVFilterFormats *out_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
  75. AVFilterFormats *in_samplerates = ff_all_samplerates();
  76. AVFilterFormats *out_samplerates = ff_all_samplerates();
  77. AVFilterChannelLayouts *in_layouts = ff_all_channel_layouts();
  78. AVFilterChannelLayouts *out_layouts = ff_all_channel_layouts();
  79. ff_formats_ref(in_formats, &inlink->out_formats);
  80. ff_formats_ref(out_formats, &outlink->in_formats);
  81. ff_formats_ref(in_samplerates, &inlink->out_samplerates);
  82. ff_formats_ref(out_samplerates, &outlink->in_samplerates);
  83. ff_channel_layouts_ref(in_layouts, &inlink->out_channel_layouts);
  84. ff_channel_layouts_ref(out_layouts, &outlink->in_channel_layouts);
  85. return 0;
  86. }
  87. static int config_output(AVFilterLink *outlink)
  88. {
  89. AVFilterContext *ctx = outlink->src;
  90. AVFilterLink *inlink = ctx->inputs[0];
  91. ResampleContext *s = ctx->priv;
  92. char buf1[64], buf2[64];
  93. int ret;
  94. if (s->avr) {
  95. avresample_close(s->avr);
  96. avresample_free(&s->avr);
  97. }
  98. if (inlink->channel_layout == outlink->channel_layout &&
  99. inlink->sample_rate == outlink->sample_rate &&
  100. (inlink->format == outlink->format ||
  101. (av_get_channel_layout_nb_channels(inlink->channel_layout) == 1 &&
  102. av_get_channel_layout_nb_channels(outlink->channel_layout) == 1 &&
  103. av_get_planar_sample_fmt(inlink->format) ==
  104. av_get_planar_sample_fmt(outlink->format))))
  105. return 0;
  106. if (!(s->avr = avresample_alloc_context()))
  107. return AVERROR(ENOMEM);
  108. if (s->options) {
  109. AVDictionaryEntry *e = NULL;
  110. while ((e = av_dict_get(s->options, "", e, AV_DICT_IGNORE_SUFFIX)))
  111. av_log(ctx, AV_LOG_VERBOSE, "lavr option: %s=%s\n", e->key, e->value);
  112. av_opt_set_dict(s->avr, &s->options);
  113. }
  114. av_opt_set_int(s->avr, "in_channel_layout", inlink ->channel_layout, 0);
  115. av_opt_set_int(s->avr, "out_channel_layout", outlink->channel_layout, 0);
  116. av_opt_set_int(s->avr, "in_sample_fmt", inlink ->format, 0);
  117. av_opt_set_int(s->avr, "out_sample_fmt", outlink->format, 0);
  118. av_opt_set_int(s->avr, "in_sample_rate", inlink ->sample_rate, 0);
  119. av_opt_set_int(s->avr, "out_sample_rate", outlink->sample_rate, 0);
  120. if ((ret = avresample_open(s->avr)) < 0)
  121. return ret;
  122. outlink->time_base = (AVRational){ 1, outlink->sample_rate };
  123. s->next_pts = AV_NOPTS_VALUE;
  124. av_get_channel_layout_string(buf1, sizeof(buf1),
  125. -1, inlink ->channel_layout);
  126. av_get_channel_layout_string(buf2, sizeof(buf2),
  127. -1, outlink->channel_layout);
  128. av_log(ctx, AV_LOG_VERBOSE,
  129. "fmt:%s srate:%d cl:%s -> fmt:%s srate:%d cl:%s\n",
  130. av_get_sample_fmt_name(inlink ->format), inlink ->sample_rate, buf1,
  131. av_get_sample_fmt_name(outlink->format), outlink->sample_rate, buf2);
  132. return 0;
  133. }
  134. static int request_frame(AVFilterLink *outlink)
  135. {
  136. AVFilterContext *ctx = outlink->src;
  137. ResampleContext *s = ctx->priv;
  138. int ret = 0;
  139. s->got_output = 0;
  140. while (ret >= 0 && !s->got_output)
  141. ret = ff_request_frame(ctx->inputs[0]);
  142. /* flush the lavr delay buffer */
  143. if (ret == AVERROR_EOF && s->avr) {
  144. AVFilterBufferRef *buf;
  145. int nb_samples = av_rescale_rnd(avresample_get_delay(s->avr),
  146. outlink->sample_rate,
  147. ctx->inputs[0]->sample_rate,
  148. AV_ROUND_UP);
  149. if (!nb_samples)
  150. return ret;
  151. buf = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
  152. if (!buf)
  153. return AVERROR(ENOMEM);
  154. ret = avresample_convert(s->avr, buf->extended_data,
  155. buf->linesize[0], nb_samples,
  156. NULL, 0, 0);
  157. if (ret <= 0) {
  158. avfilter_unref_buffer(buf);
  159. return (ret == 0) ? AVERROR_EOF : ret;
  160. }
  161. buf->pts = s->next_pts;
  162. return ff_filter_frame(outlink, buf);
  163. }
  164. return ret;
  165. }
  166. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
  167. {
  168. AVFilterContext *ctx = inlink->dst;
  169. ResampleContext *s = ctx->priv;
  170. AVFilterLink *outlink = ctx->outputs[0];
  171. int ret;
  172. if (s->avr) {
  173. AVFilterBufferRef *buf_out;
  174. int delay, nb_samples;
  175. /* maximum possible samples lavr can output */
  176. delay = avresample_get_delay(s->avr);
  177. nb_samples = av_rescale_rnd(buf->audio->nb_samples + delay,
  178. outlink->sample_rate, inlink->sample_rate,
  179. AV_ROUND_UP);
  180. buf_out = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
  181. if (!buf_out) {
  182. ret = AVERROR(ENOMEM);
  183. goto fail;
  184. }
  185. ret = avresample_convert(s->avr, buf_out->extended_data,
  186. buf_out->linesize[0], nb_samples,
  187. buf->extended_data, buf->linesize[0],
  188. buf->audio->nb_samples);
  189. if (ret <= 0) {
  190. avfilter_unref_buffer(buf_out);
  191. if (ret < 0)
  192. goto fail;
  193. }
  194. av_assert0(!avresample_available(s->avr));
  195. if (s->next_pts == AV_NOPTS_VALUE) {
  196. if (buf->pts == AV_NOPTS_VALUE) {
  197. av_log(ctx, AV_LOG_WARNING, "First timestamp is missing, "
  198. "assuming 0.\n");
  199. s->next_pts = 0;
  200. } else
  201. s->next_pts = av_rescale_q(buf->pts, inlink->time_base,
  202. outlink->time_base);
  203. }
  204. if (ret > 0) {
  205. buf_out->audio->nb_samples = ret;
  206. if (buf->pts != AV_NOPTS_VALUE) {
  207. buf_out->pts = av_rescale_q(buf->pts, inlink->time_base,
  208. outlink->time_base) -
  209. av_rescale(delay, outlink->sample_rate,
  210. inlink->sample_rate);
  211. } else
  212. buf_out->pts = s->next_pts;
  213. s->next_pts = buf_out->pts + buf_out->audio->nb_samples;
  214. ret = ff_filter_frame(outlink, buf_out);
  215. s->got_output = 1;
  216. }
  217. fail:
  218. avfilter_unref_buffer(buf);
  219. } else {
  220. buf->format = outlink->format;
  221. ret = ff_filter_frame(outlink, buf);
  222. s->got_output = 1;
  223. }
  224. return ret;
  225. }
  226. static const AVFilterPad avfilter_af_resample_inputs[] = {
  227. {
  228. .name = "default",
  229. .type = AVMEDIA_TYPE_AUDIO,
  230. .filter_frame = filter_frame,
  231. .min_perms = AV_PERM_READ
  232. },
  233. { NULL }
  234. };
  235. static const AVFilterPad avfilter_af_resample_outputs[] = {
  236. {
  237. .name = "default",
  238. .type = AVMEDIA_TYPE_AUDIO,
  239. .config_props = config_output,
  240. .request_frame = request_frame
  241. },
  242. { NULL }
  243. };
  244. AVFilter avfilter_af_resample = {
  245. .name = "resample",
  246. .description = NULL_IF_CONFIG_SMALL("Audio resampling and conversion."),
  247. .priv_size = sizeof(ResampleContext),
  248. .init = init,
  249. .uninit = uninit,
  250. .query_formats = query_formats,
  251. .inputs = avfilter_af_resample_inputs,
  252. .outputs = avfilter_af_resample_outputs,
  253. };