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.

331 lines
10KB

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