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.

328 lines
10KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * sample format and channel layout conversion audio filter
  21. */
  22. #include "libavutil/avassert.h"
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/common.h"
  25. #include "libavutil/dict.h"
  26. #include "libavutil/mathematics.h"
  27. #include "libavutil/opt.h"
  28. #include "libavresample/avresample.h"
  29. #include "audio.h"
  30. #include "avfilter.h"
  31. #include "formats.h"
  32. #include "internal.h"
  33. typedef struct ResampleContext {
  34. const AVClass *class;
  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, AVDictionary **opts)
  42. {
  43. ResampleContext *s = ctx->priv;
  44. const AVClass *avr_class = avresample_get_class();
  45. AVDictionaryEntry *e = NULL;
  46. while ((e = av_dict_get(*opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
  47. if (av_opt_find(&avr_class, e->key, NULL, 0,
  48. AV_OPT_SEARCH_FAKE_OBJ | AV_OPT_SEARCH_CHILDREN))
  49. av_dict_set(&s->options, e->key, e->value, 0);
  50. }
  51. e = NULL;
  52. while ((e = av_dict_get(s->options, "", e, AV_DICT_IGNORE_SUFFIX)))
  53. av_dict_set(opts, e->key, NULL, 0);
  54. /* do not allow the user to override basic format options */
  55. av_dict_set(&s->options, "in_channel_layout", NULL, 0);
  56. av_dict_set(&s->options, "out_channel_layout", NULL, 0);
  57. av_dict_set(&s->options, "in_sample_fmt", NULL, 0);
  58. av_dict_set(&s->options, "out_sample_fmt", NULL, 0);
  59. av_dict_set(&s->options, "in_sample_rate", NULL, 0);
  60. av_dict_set(&s->options, "out_sample_rate", NULL, 0);
  61. return 0;
  62. }
  63. static av_cold void uninit(AVFilterContext *ctx)
  64. {
  65. ResampleContext *s = ctx->priv;
  66. if (s->avr) {
  67. avresample_close(s->avr);
  68. avresample_free(&s->avr);
  69. }
  70. av_dict_free(&s->options);
  71. }
  72. static int query_formats(AVFilterContext *ctx)
  73. {
  74. AVFilterLink *inlink = ctx->inputs[0];
  75. AVFilterLink *outlink = ctx->outputs[0];
  76. AVFilterFormats *in_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
  77. AVFilterFormats *out_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
  78. AVFilterFormats *in_samplerates = ff_all_samplerates();
  79. AVFilterFormats *out_samplerates = ff_all_samplerates();
  80. AVFilterChannelLayouts *in_layouts = ff_all_channel_layouts();
  81. AVFilterChannelLayouts *out_layouts = ff_all_channel_layouts();
  82. ff_formats_ref(in_formats, &inlink->out_formats);
  83. ff_formats_ref(out_formats, &outlink->in_formats);
  84. ff_formats_ref(in_samplerates, &inlink->out_samplerates);
  85. ff_formats_ref(out_samplerates, &outlink->in_samplerates);
  86. ff_channel_layouts_ref(in_layouts, &inlink->out_channel_layouts);
  87. ff_channel_layouts_ref(out_layouts, &outlink->in_channel_layouts);
  88. return 0;
  89. }
  90. static int config_output(AVFilterLink *outlink)
  91. {
  92. AVFilterContext *ctx = outlink->src;
  93. AVFilterLink *inlink = ctx->inputs[0];
  94. ResampleContext *s = ctx->priv;
  95. char buf1[64], buf2[64];
  96. int ret;
  97. if (s->avr) {
  98. avresample_close(s->avr);
  99. avresample_free(&s->avr);
  100. }
  101. if (inlink->channel_layout == outlink->channel_layout &&
  102. inlink->sample_rate == outlink->sample_rate &&
  103. (inlink->format == outlink->format ||
  104. (av_get_channel_layout_nb_channels(inlink->channel_layout) == 1 &&
  105. av_get_channel_layout_nb_channels(outlink->channel_layout) == 1 &&
  106. av_get_planar_sample_fmt(inlink->format) ==
  107. av_get_planar_sample_fmt(outlink->format))))
  108. return 0;
  109. if (!(s->avr = avresample_alloc_context()))
  110. return AVERROR(ENOMEM);
  111. if (s->options) {
  112. AVDictionaryEntry *e = NULL;
  113. while ((e = av_dict_get(s->options, "", e, AV_DICT_IGNORE_SUFFIX)))
  114. av_log(ctx, AV_LOG_VERBOSE, "lavr option: %s=%s\n", e->key, e->value);
  115. av_opt_set_dict(s->avr, &s->options);
  116. }
  117. av_opt_set_int(s->avr, "in_channel_layout", inlink ->channel_layout, 0);
  118. av_opt_set_int(s->avr, "out_channel_layout", outlink->channel_layout, 0);
  119. av_opt_set_int(s->avr, "in_sample_fmt", inlink ->format, 0);
  120. av_opt_set_int(s->avr, "out_sample_fmt", outlink->format, 0);
  121. av_opt_set_int(s->avr, "in_sample_rate", inlink ->sample_rate, 0);
  122. av_opt_set_int(s->avr, "out_sample_rate", outlink->sample_rate, 0);
  123. if ((ret = avresample_open(s->avr)) < 0)
  124. return ret;
  125. outlink->time_base = (AVRational){ 1, outlink->sample_rate };
  126. s->next_pts = AV_NOPTS_VALUE;
  127. av_get_channel_layout_string(buf1, sizeof(buf1),
  128. -1, inlink ->channel_layout);
  129. av_get_channel_layout_string(buf2, sizeof(buf2),
  130. -1, outlink->channel_layout);
  131. av_log(ctx, AV_LOG_VERBOSE,
  132. "fmt:%s srate:%d cl:%s -> fmt:%s srate:%d cl:%s\n",
  133. av_get_sample_fmt_name(inlink ->format), inlink ->sample_rate, buf1,
  134. av_get_sample_fmt_name(outlink->format), outlink->sample_rate, buf2);
  135. return 0;
  136. }
  137. static int request_frame(AVFilterLink *outlink)
  138. {
  139. AVFilterContext *ctx = outlink->src;
  140. ResampleContext *s = ctx->priv;
  141. int ret = 0;
  142. s->got_output = 0;
  143. while (ret >= 0 && !s->got_output)
  144. ret = ff_request_frame(ctx->inputs[0]);
  145. /* flush the lavr delay buffer */
  146. if (ret == AVERROR_EOF && s->avr) {
  147. AVFrame *frame;
  148. int nb_samples = av_rescale_rnd(avresample_get_delay(s->avr),
  149. outlink->sample_rate,
  150. ctx->inputs[0]->sample_rate,
  151. AV_ROUND_UP);
  152. if (!nb_samples)
  153. return ret;
  154. frame = ff_get_audio_buffer(outlink, nb_samples);
  155. if (!frame)
  156. return AVERROR(ENOMEM);
  157. ret = avresample_convert(s->avr, frame->extended_data,
  158. frame->linesize[0], nb_samples,
  159. NULL, 0, 0);
  160. if (ret <= 0) {
  161. av_frame_free(&frame);
  162. return (ret == 0) ? AVERROR_EOF : ret;
  163. }
  164. frame->pts = s->next_pts;
  165. return ff_filter_frame(outlink, frame);
  166. }
  167. return ret;
  168. }
  169. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  170. {
  171. AVFilterContext *ctx = inlink->dst;
  172. ResampleContext *s = ctx->priv;
  173. AVFilterLink *outlink = ctx->outputs[0];
  174. int ret;
  175. if (s->avr) {
  176. AVFrame *out;
  177. int delay, nb_samples;
  178. /* maximum possible samples lavr can output */
  179. delay = avresample_get_delay(s->avr);
  180. nb_samples = av_rescale_rnd(in->nb_samples + delay,
  181. outlink->sample_rate, inlink->sample_rate,
  182. AV_ROUND_UP);
  183. out = ff_get_audio_buffer(outlink, nb_samples);
  184. if (!out) {
  185. ret = AVERROR(ENOMEM);
  186. goto fail;
  187. }
  188. ret = avresample_convert(s->avr, out->extended_data, out->linesize[0],
  189. nb_samples, in->extended_data, in->linesize[0],
  190. in->nb_samples);
  191. if (ret <= 0) {
  192. av_frame_free(&out);
  193. if (ret < 0)
  194. goto fail;
  195. }
  196. av_assert0(!avresample_available(s->avr));
  197. if (s->next_pts == AV_NOPTS_VALUE) {
  198. if (in->pts == AV_NOPTS_VALUE) {
  199. av_log(ctx, AV_LOG_WARNING, "First timestamp is missing, "
  200. "assuming 0.\n");
  201. s->next_pts = 0;
  202. } else
  203. s->next_pts = av_rescale_q(in->pts, inlink->time_base,
  204. outlink->time_base);
  205. }
  206. if (ret > 0) {
  207. out->nb_samples = ret;
  208. if (in->pts != AV_NOPTS_VALUE) {
  209. out->pts = av_rescale_q(in->pts, inlink->time_base,
  210. outlink->time_base) -
  211. av_rescale(delay, outlink->sample_rate,
  212. inlink->sample_rate);
  213. } else
  214. out->pts = s->next_pts;
  215. s->next_pts = out->pts + out->nb_samples;
  216. ret = ff_filter_frame(outlink, out);
  217. s->got_output = 1;
  218. }
  219. fail:
  220. av_frame_free(&in);
  221. } else {
  222. in->format = outlink->format;
  223. ret = ff_filter_frame(outlink, in);
  224. s->got_output = 1;
  225. }
  226. return ret;
  227. }
  228. static const AVClass *resample_child_class_next(const AVClass *prev)
  229. {
  230. return prev ? NULL : avresample_get_class();
  231. }
  232. static void *resample_child_next(void *obj, void *prev)
  233. {
  234. ResampleContext *s = obj;
  235. return prev ? NULL : s->avr;
  236. }
  237. static const AVClass resample_class = {
  238. .class_name = "resample",
  239. .item_name = av_default_item_name,
  240. .version = LIBAVUTIL_VERSION_INT,
  241. .child_class_next = resample_child_class_next,
  242. .child_next = resample_child_next,
  243. };
  244. static const AVFilterPad avfilter_af_resample_inputs[] = {
  245. {
  246. .name = "default",
  247. .type = AVMEDIA_TYPE_AUDIO,
  248. .filter_frame = filter_frame,
  249. },
  250. { NULL }
  251. };
  252. static const AVFilterPad avfilter_af_resample_outputs[] = {
  253. {
  254. .name = "default",
  255. .type = AVMEDIA_TYPE_AUDIO,
  256. .config_props = config_output,
  257. .request_frame = request_frame
  258. },
  259. { NULL }
  260. };
  261. AVFilter ff_af_resample = {
  262. .name = "resample",
  263. .description = NULL_IF_CONFIG_SMALL("Audio resampling and conversion."),
  264. .priv_size = sizeof(ResampleContext),
  265. .priv_class = &resample_class,
  266. .init_dict = init,
  267. .uninit = uninit,
  268. .query_formats = query_formats,
  269. .inputs = avfilter_af_resample_inputs,
  270. .outputs = avfilter_af_resample_outputs,
  271. };