af_resample.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. int64_t next_in_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. s->next_in_pts = AV_NOPTS_VALUE;
  129. av_get_channel_layout_string(buf1, sizeof(buf1),
  130. -1, inlink ->channel_layout);
  131. av_get_channel_layout_string(buf2, sizeof(buf2),
  132. -1, outlink->channel_layout);
  133. av_log(ctx, AV_LOG_VERBOSE,
  134. "fmt:%s srate:%d cl:%s -> fmt:%s srate:%d cl:%s\n",
  135. av_get_sample_fmt_name(inlink ->format), inlink ->sample_rate, buf1,
  136. av_get_sample_fmt_name(outlink->format), outlink->sample_rate, buf2);
  137. return 0;
  138. }
  139. static int request_frame(AVFilterLink *outlink)
  140. {
  141. AVFilterContext *ctx = outlink->src;
  142. ResampleContext *s = ctx->priv;
  143. int ret = 0;
  144. s->got_output = 0;
  145. while (ret >= 0 && !s->got_output)
  146. ret = ff_request_frame(ctx->inputs[0]);
  147. /* flush the lavr delay buffer */
  148. if (ret == AVERROR_EOF && s->avr) {
  149. AVFrame *frame;
  150. int nb_samples = av_rescale_rnd(avresample_get_delay(s->avr),
  151. outlink->sample_rate,
  152. ctx->inputs[0]->sample_rate,
  153. AV_ROUND_UP);
  154. if (!nb_samples)
  155. return ret;
  156. frame = ff_get_audio_buffer(outlink, nb_samples);
  157. if (!frame)
  158. return AVERROR(ENOMEM);
  159. ret = avresample_convert(s->avr, frame->extended_data,
  160. frame->linesize[0], nb_samples,
  161. NULL, 0, 0);
  162. if (ret <= 0) {
  163. av_frame_free(&frame);
  164. return (ret == 0) ? AVERROR_EOF : ret;
  165. }
  166. frame->pts = s->next_pts;
  167. return ff_filter_frame(outlink, frame);
  168. }
  169. return ret;
  170. }
  171. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  172. {
  173. AVFilterContext *ctx = inlink->dst;
  174. ResampleContext *s = ctx->priv;
  175. AVFilterLink *outlink = ctx->outputs[0];
  176. int ret;
  177. if (s->avr) {
  178. AVFrame *out;
  179. int delay, nb_samples;
  180. /* maximum possible samples lavr can output */
  181. delay = avresample_get_delay(s->avr);
  182. nb_samples = av_rescale_rnd(in->nb_samples + delay,
  183. outlink->sample_rate, inlink->sample_rate,
  184. AV_ROUND_UP);
  185. out = ff_get_audio_buffer(outlink, nb_samples);
  186. if (!out) {
  187. ret = AVERROR(ENOMEM);
  188. goto fail;
  189. }
  190. ret = avresample_convert(s->avr, out->extended_data, out->linesize[0],
  191. nb_samples, in->extended_data, in->linesize[0],
  192. in->nb_samples);
  193. if (ret <= 0) {
  194. av_frame_free(&out);
  195. if (ret < 0)
  196. goto fail;
  197. }
  198. av_assert0(!avresample_available(s->avr));
  199. if (s->next_pts == AV_NOPTS_VALUE) {
  200. if (in->pts == AV_NOPTS_VALUE) {
  201. av_log(ctx, AV_LOG_WARNING, "First timestamp is missing, "
  202. "assuming 0.\n");
  203. s->next_pts = 0;
  204. } else
  205. s->next_pts = av_rescale_q(in->pts, inlink->time_base,
  206. outlink->time_base);
  207. }
  208. if (ret > 0) {
  209. out->nb_samples = ret;
  210. ret = av_frame_copy_props(out, in);
  211. if (ret < 0) {
  212. av_frame_free(&out);
  213. goto fail;
  214. }
  215. out->sample_rate = outlink->sample_rate;
  216. /* Only convert in->pts if there is a discontinuous jump.
  217. This ensures that out->pts tracks the number of samples actually
  218. output by the resampler in the absence of such a jump.
  219. Otherwise, the rounding in av_rescale_q() and av_rescale()
  220. causes off-by-1 errors. */
  221. if (in->pts != AV_NOPTS_VALUE && in->pts != s->next_in_pts) {
  222. out->pts = av_rescale_q(in->pts, inlink->time_base,
  223. outlink->time_base) -
  224. av_rescale(delay, outlink->sample_rate,
  225. inlink->sample_rate);
  226. } else
  227. out->pts = s->next_pts;
  228. s->next_pts = out->pts + out->nb_samples;
  229. s->next_in_pts = in->pts + in->nb_samples;
  230. ret = ff_filter_frame(outlink, out);
  231. s->got_output = 1;
  232. }
  233. fail:
  234. av_frame_free(&in);
  235. } else {
  236. in->format = outlink->format;
  237. ret = ff_filter_frame(outlink, in);
  238. s->got_output = 1;
  239. }
  240. return ret;
  241. }
  242. static const AVClass *resample_child_class_next(const AVClass *prev)
  243. {
  244. return prev ? NULL : avresample_get_class();
  245. }
  246. static void *resample_child_next(void *obj, void *prev)
  247. {
  248. ResampleContext *s = obj;
  249. return prev ? NULL : s->avr;
  250. }
  251. static const AVClass resample_class = {
  252. .class_name = "resample",
  253. .item_name = av_default_item_name,
  254. .version = LIBAVUTIL_VERSION_INT,
  255. .child_class_next = resample_child_class_next,
  256. .child_next = resample_child_next,
  257. };
  258. static const AVFilterPad avfilter_af_resample_inputs[] = {
  259. {
  260. .name = "default",
  261. .type = AVMEDIA_TYPE_AUDIO,
  262. .filter_frame = filter_frame,
  263. },
  264. { NULL }
  265. };
  266. static const AVFilterPad avfilter_af_resample_outputs[] = {
  267. {
  268. .name = "default",
  269. .type = AVMEDIA_TYPE_AUDIO,
  270. .config_props = config_output,
  271. .request_frame = request_frame
  272. },
  273. { NULL }
  274. };
  275. AVFilter ff_af_resample = {
  276. .name = "resample",
  277. .description = NULL_IF_CONFIG_SMALL("Audio resampling and conversion."),
  278. .priv_size = sizeof(ResampleContext),
  279. .priv_class = &resample_class,
  280. .init_dict = init,
  281. .uninit = uninit,
  282. .query_formats = query_formats,
  283. .inputs = avfilter_af_resample_inputs,
  284. .outputs = avfilter_af_resample_outputs,
  285. };