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.

345 lines
11KB

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