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.

342 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. AVDictionaryEntry *e = NULL;
  115. while ((e = av_dict_get(s->options, "", e, AV_DICT_IGNORE_SUFFIX)))
  116. av_log(ctx, AV_LOG_VERBOSE, "lavr option: %s=%s\n", e->key, e->value);
  117. av_opt_set_dict(s->avr, &s->options);
  118. }
  119. av_opt_set_int(s->avr, "in_channel_layout", inlink ->channel_layout, 0);
  120. av_opt_set_int(s->avr, "out_channel_layout", outlink->channel_layout, 0);
  121. av_opt_set_int(s->avr, "in_sample_fmt", inlink ->format, 0);
  122. av_opt_set_int(s->avr, "out_sample_fmt", outlink->format, 0);
  123. av_opt_set_int(s->avr, "in_sample_rate", inlink ->sample_rate, 0);
  124. av_opt_set_int(s->avr, "out_sample_rate", outlink->sample_rate, 0);
  125. if ((ret = avresample_open(s->avr)) < 0)
  126. return ret;
  127. outlink->time_base = (AVRational){ 1, outlink->sample_rate };
  128. s->next_pts = AV_NOPTS_VALUE;
  129. s->next_in_pts = AV_NOPTS_VALUE;
  130. av_get_channel_layout_string(buf1, sizeof(buf1),
  131. -1, inlink ->channel_layout);
  132. av_get_channel_layout_string(buf2, sizeof(buf2),
  133. -1, outlink->channel_layout);
  134. av_log(ctx, AV_LOG_VERBOSE,
  135. "fmt:%s srate:%d cl:%s -> fmt:%s srate:%d cl:%s\n",
  136. av_get_sample_fmt_name(inlink ->format), inlink ->sample_rate, buf1,
  137. av_get_sample_fmt_name(outlink->format), outlink->sample_rate, buf2);
  138. return 0;
  139. }
  140. static int request_frame(AVFilterLink *outlink)
  141. {
  142. AVFilterContext *ctx = outlink->src;
  143. ResampleContext *s = ctx->priv;
  144. int ret = 0;
  145. s->got_output = 0;
  146. while (ret >= 0 && !s->got_output)
  147. ret = ff_request_frame(ctx->inputs[0]);
  148. /* flush the lavr delay buffer */
  149. if (ret == AVERROR_EOF && s->avr) {
  150. AVFrame *frame;
  151. int nb_samples = avresample_get_out_samples(s->avr, 0);
  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 = avresample_get_out_samples(s->avr, in->nb_samples);
  181. out = ff_get_audio_buffer(outlink, nb_samples);
  182. if (!out) {
  183. ret = AVERROR(ENOMEM);
  184. goto fail;
  185. }
  186. ret = avresample_convert(s->avr, out->extended_data, out->linesize[0],
  187. nb_samples, in->extended_data, in->linesize[0],
  188. in->nb_samples);
  189. if (ret <= 0) {
  190. av_frame_free(&out);
  191. if (ret < 0)
  192. goto fail;
  193. }
  194. av_assert0(!avresample_available(s->avr));
  195. if (s->next_pts == AV_NOPTS_VALUE) {
  196. if (in->pts == AV_NOPTS_VALUE) {
  197. av_log(ctx, AV_LOG_WARNING, "First timestamp is missing, "
  198. "assuming 0.\n");
  199. s->next_pts = 0;
  200. } else
  201. s->next_pts = av_rescale_q(in->pts, inlink->time_base,
  202. outlink->time_base);
  203. }
  204. if (ret > 0) {
  205. out->nb_samples = ret;
  206. ret = av_frame_copy_props(out, in);
  207. if (ret < 0) {
  208. av_frame_free(&out);
  209. goto fail;
  210. }
  211. out->sample_rate = outlink->sample_rate;
  212. /* Only convert in->pts if there is a discontinuous jump.
  213. This ensures that out->pts tracks the number of samples actually
  214. output by the resampler in the absence of such a jump.
  215. Otherwise, the rounding in av_rescale_q() and av_rescale()
  216. causes off-by-1 errors. */
  217. if (in->pts != AV_NOPTS_VALUE && in->pts != s->next_in_pts) {
  218. out->pts = av_rescale_q(in->pts, inlink->time_base,
  219. outlink->time_base) -
  220. av_rescale(delay, outlink->sample_rate,
  221. inlink->sample_rate);
  222. } else
  223. out->pts = s->next_pts;
  224. s->next_pts = out->pts + out->nb_samples;
  225. s->next_in_pts = in->pts + in->nb_samples;
  226. ret = ff_filter_frame(outlink, out);
  227. s->got_output = 1;
  228. }
  229. fail:
  230. av_frame_free(&in);
  231. } else {
  232. in->format = outlink->format;
  233. ret = ff_filter_frame(outlink, in);
  234. s->got_output = 1;
  235. }
  236. return ret;
  237. }
  238. static const AVClass *resample_child_class_next(const AVClass *prev)
  239. {
  240. return prev ? NULL : avresample_get_class();
  241. }
  242. static void *resample_child_next(void *obj, void *prev)
  243. {
  244. ResampleContext *s = obj;
  245. return prev ? NULL : s->avr;
  246. }
  247. static const AVClass resample_class = {
  248. .class_name = "resample",
  249. .item_name = av_default_item_name,
  250. .version = LIBAVUTIL_VERSION_INT,
  251. .child_class_next = resample_child_class_next,
  252. .child_next = resample_child_next,
  253. };
  254. static const AVFilterPad avfilter_af_resample_inputs[] = {
  255. {
  256. .name = "default",
  257. .type = AVMEDIA_TYPE_AUDIO,
  258. .filter_frame = filter_frame,
  259. },
  260. { NULL }
  261. };
  262. static const AVFilterPad avfilter_af_resample_outputs[] = {
  263. {
  264. .name = "default",
  265. .type = AVMEDIA_TYPE_AUDIO,
  266. .config_props = config_output,
  267. .request_frame = request_frame
  268. },
  269. { NULL }
  270. };
  271. AVFilter ff_af_resample = {
  272. .name = "resample",
  273. .description = NULL_IF_CONFIG_SMALL("Audio resampling and conversion."),
  274. .priv_size = sizeof(ResampleContext),
  275. .priv_class = &resample_class,
  276. .init_dict = init,
  277. .uninit = uninit,
  278. .query_formats = query_formats,
  279. .inputs = avfilter_af_resample_inputs,
  280. .outputs = avfilter_af_resample_outputs,
  281. };