af_resample.c 11 KB

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