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.

306 lines
9.7KB

  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. AVFrame *frame;
  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. frame = ff_get_audio_buffer(outlink, nb_samples);
  152. if (!frame)
  153. return AVERROR(ENOMEM);
  154. ret = avresample_convert(s->avr, frame->extended_data,
  155. frame->linesize[0], nb_samples,
  156. NULL, 0, 0);
  157. if (ret <= 0) {
  158. av_frame_free(&frame);
  159. return (ret == 0) ? AVERROR_EOF : ret;
  160. }
  161. frame->pts = s->next_pts;
  162. return ff_filter_frame(outlink, frame);
  163. }
  164. return ret;
  165. }
  166. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  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. AVFrame *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(in->nb_samples + delay,
  178. outlink->sample_rate, inlink->sample_rate,
  179. AV_ROUND_UP);
  180. out = ff_get_audio_buffer(outlink, nb_samples);
  181. if (!out) {
  182. ret = AVERROR(ENOMEM);
  183. goto fail;
  184. }
  185. ret = avresample_convert(s->avr, out->extended_data, out->linesize[0],
  186. nb_samples, in->extended_data, in->linesize[0],
  187. in->nb_samples);
  188. if (ret <= 0) {
  189. av_frame_free(&out);
  190. if (ret < 0)
  191. goto fail;
  192. }
  193. av_assert0(!avresample_available(s->avr));
  194. if (s->next_pts == AV_NOPTS_VALUE) {
  195. if (in->pts == AV_NOPTS_VALUE) {
  196. av_log(ctx, AV_LOG_WARNING, "First timestamp is missing, "
  197. "assuming 0.\n");
  198. s->next_pts = 0;
  199. } else
  200. s->next_pts = av_rescale_q(in->pts, inlink->time_base,
  201. outlink->time_base);
  202. }
  203. if (ret > 0) {
  204. out->nb_samples = ret;
  205. if (in->pts != AV_NOPTS_VALUE) {
  206. out->pts = av_rescale_q(in->pts, inlink->time_base,
  207. outlink->time_base) -
  208. av_rescale(delay, outlink->sample_rate,
  209. inlink->sample_rate);
  210. } else
  211. out->pts = s->next_pts;
  212. s->next_pts = out->pts + out->nb_samples;
  213. ret = ff_filter_frame(outlink, out);
  214. s->got_output = 1;
  215. }
  216. fail:
  217. av_frame_free(&in);
  218. } else {
  219. in->format = outlink->format;
  220. ret = ff_filter_frame(outlink, in);
  221. s->got_output = 1;
  222. }
  223. return ret;
  224. }
  225. static const AVFilterPad avfilter_af_resample_inputs[] = {
  226. {
  227. .name = "default",
  228. .type = AVMEDIA_TYPE_AUDIO,
  229. .filter_frame = filter_frame,
  230. },
  231. { NULL }
  232. };
  233. static const AVFilterPad avfilter_af_resample_outputs[] = {
  234. {
  235. .name = "default",
  236. .type = AVMEDIA_TYPE_AUDIO,
  237. .config_props = config_output,
  238. .request_frame = request_frame
  239. },
  240. { NULL }
  241. };
  242. AVFilter avfilter_af_resample = {
  243. .name = "resample",
  244. .description = NULL_IF_CONFIG_SMALL("Audio resampling and conversion."),
  245. .priv_size = sizeof(ResampleContext),
  246. .init = init,
  247. .uninit = uninit,
  248. .query_formats = query_formats,
  249. .inputs = avfilter_af_resample_inputs,
  250. .outputs = avfilter_af_resample_outputs,
  251. };