af_resample.c 12 KB

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