af_aconvert.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * Copyright (c) 2010 S.N. Hemanth Meenakshisundaram <smeenaks@ucsd.edu>
  3. * Copyright (c) 2011 Stefano Sabatini
  4. * Copyright (c) 2011 Mina Nagy Zaki
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * sample format and channel layout conversion audio filter
  25. * based on code in libavcodec/resample.c by Fabrice Bellard and
  26. * libavcodec/audioconvert.c by Michael Niedermayer
  27. */
  28. #include "libavutil/audioconvert.h"
  29. #include "libavutil/avstring.h"
  30. #include "libavcodec/audioconvert.h"
  31. #include "avfilter.h"
  32. #include "internal.h"
  33. typedef struct {
  34. enum AVSampleFormat out_sample_fmt, in_sample_fmt; ///< in/out sample formats
  35. int64_t out_chlayout, in_chlayout; ///< in/out channel layout
  36. int out_nb_channels, in_nb_channels; ///< number of in/output channels
  37. enum AVFilterPacking out_packing_fmt, in_packing_fmt; ///< output packing format
  38. int max_nb_samples; ///< maximum number of buffered samples
  39. AVFilterBufferRef *mix_samplesref; ///< rematrixed buffer
  40. AVFilterBufferRef *out_samplesref; ///< output buffer after required conversions
  41. uint8_t *in_mix[8], *out_mix[8]; ///< input/output for rematrixing functions
  42. uint8_t *packed_data[8]; ///< pointers for packing conversion
  43. int out_strides[8], in_strides[8]; ///< input/output strides for av_audio_convert
  44. uint8_t **in_conv, **out_conv; ///< input/output for av_audio_convert
  45. AVAudioConvert *audioconvert_ctx; ///< context for conversion to output sample format
  46. void (*convert_chlayout)(); ///< function to do the requested rematrixing
  47. } AConvertContext;
  48. #define REMATRIX_FUNC_SIG(NAME) static void REMATRIX_FUNC_NAME(NAME) \
  49. (FMT_TYPE *outp[], FMT_TYPE *inp[], int nb_samples, AConvertContext *aconvert)
  50. #define FMT_TYPE uint8_t
  51. #define REMATRIX_FUNC_NAME(NAME) NAME ## _u8
  52. #include "af_aconvert_rematrix.c"
  53. #define FMT_TYPE int16_t
  54. #define REMATRIX_FUNC_NAME(NAME) NAME ## _s16
  55. #include "af_aconvert_rematrix.c"
  56. #define FMT_TYPE int32_t
  57. #define REMATRIX_FUNC_NAME(NAME) NAME ## _s32
  58. #include "af_aconvert_rematrix.c"
  59. #define FLOATING
  60. #define FMT_TYPE float
  61. #define REMATRIX_FUNC_NAME(NAME) NAME ## _flt
  62. #include "af_aconvert_rematrix.c"
  63. #define FMT_TYPE double
  64. #define REMATRIX_FUNC_NAME(NAME) NAME ## _dbl
  65. #include "af_aconvert_rematrix.c"
  66. #define FMT_TYPE uint8_t
  67. #define REMATRIX_FUNC_NAME(NAME) NAME
  68. REMATRIX_FUNC_SIG(stereo_remix_planar)
  69. {
  70. int size = av_get_bytes_per_sample(aconvert->in_sample_fmt) * nb_samples;
  71. memcpy(outp[0], inp[0], size);
  72. memcpy(outp[1], inp[aconvert->in_nb_channels == 1 ? 0 : 1], size);
  73. }
  74. #define REGISTER_FUNC_PACKING(INCHLAYOUT, OUTCHLAYOUT, FUNC, PACKING) \
  75. {INCHLAYOUT, OUTCHLAYOUT, PACKING, AV_SAMPLE_FMT_U8, FUNC##_u8}, \
  76. {INCHLAYOUT, OUTCHLAYOUT, PACKING, AV_SAMPLE_FMT_S16, FUNC##_s16}, \
  77. {INCHLAYOUT, OUTCHLAYOUT, PACKING, AV_SAMPLE_FMT_S32, FUNC##_s32}, \
  78. {INCHLAYOUT, OUTCHLAYOUT, PACKING, AV_SAMPLE_FMT_FLT, FUNC##_flt}, \
  79. {INCHLAYOUT, OUTCHLAYOUT, PACKING, AV_SAMPLE_FMT_DBL, FUNC##_dbl},
  80. #define REGISTER_FUNC(INCHLAYOUT, OUTCHLAYOUT, FUNC) \
  81. REGISTER_FUNC_PACKING(INCHLAYOUT, OUTCHLAYOUT, FUNC##_packed, AVFILTER_PACKED) \
  82. REGISTER_FUNC_PACKING(INCHLAYOUT, OUTCHLAYOUT, FUNC##_planar, AVFILTER_PLANAR)
  83. static const struct RematrixFunctionInfo {
  84. int64_t in_chlayout, out_chlayout;
  85. int planar, sfmt;
  86. void (*func)();
  87. } rematrix_funcs[] = {
  88. REGISTER_FUNC (AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_5POINT1, stereo_to_surround_5p1)
  89. REGISTER_FUNC (AV_CH_LAYOUT_5POINT1, AV_CH_LAYOUT_STEREO, surround_5p1_to_stereo)
  90. REGISTER_FUNC_PACKING(AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_MONO, stereo_to_mono_packed, AVFILTER_PACKED)
  91. REGISTER_FUNC_PACKING(AV_CH_LAYOUT_MONO, AV_CH_LAYOUT_STEREO, mono_to_stereo_packed, AVFILTER_PACKED)
  92. REGISTER_FUNC (0, AV_CH_LAYOUT_MONO, mono_downmix)
  93. REGISTER_FUNC_PACKING(0, AV_CH_LAYOUT_STEREO, stereo_downmix_packed, AVFILTER_PACKED)
  94. // This function works for all sample formats
  95. {0, AV_CH_LAYOUT_STEREO, AVFILTER_PLANAR, -1, stereo_remix_planar}
  96. };
  97. static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
  98. {
  99. AConvertContext *aconvert = ctx->priv;
  100. char *arg, *ptr = NULL;
  101. int ret = 0;
  102. char *args = av_strdup(args0);
  103. aconvert->out_sample_fmt = AV_SAMPLE_FMT_NONE;
  104. aconvert->out_chlayout = 0;
  105. aconvert->out_packing_fmt = -1;
  106. if ((arg = av_strtok(args, ":", &ptr)) && strcmp(arg, "auto")) {
  107. if ((ret = ff_parse_sample_format(&aconvert->out_sample_fmt, arg, ctx)) < 0)
  108. goto end;
  109. }
  110. if ((arg = av_strtok(NULL, ":", &ptr)) && strcmp(arg, "auto")) {
  111. if ((ret = ff_parse_channel_layout(&aconvert->out_chlayout, arg, ctx)) < 0)
  112. goto end;
  113. }
  114. if ((arg = av_strtok(NULL, ":", &ptr)) && strcmp(arg, "auto")) {
  115. if ((ret = ff_parse_packing_format((int *)&aconvert->out_packing_fmt, arg, ctx)) < 0)
  116. goto end;
  117. }
  118. end:
  119. av_freep(&args);
  120. return ret;
  121. }
  122. static av_cold void uninit(AVFilterContext *ctx)
  123. {
  124. AConvertContext *aconvert = ctx->priv;
  125. avfilter_unref_buffer(aconvert->mix_samplesref);
  126. avfilter_unref_buffer(aconvert->out_samplesref);
  127. if (aconvert->audioconvert_ctx)
  128. av_audio_convert_free(aconvert->audioconvert_ctx);
  129. }
  130. static int query_formats(AVFilterContext *ctx)
  131. {
  132. AVFilterFormats *formats = NULL;
  133. AConvertContext *aconvert = ctx->priv;
  134. AVFilterLink *inlink = ctx->inputs[0];
  135. AVFilterLink *outlink = ctx->outputs[0];
  136. avfilter_formats_ref(avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO),
  137. &inlink->out_formats);
  138. if (aconvert->out_sample_fmt != AV_SAMPLE_FMT_NONE) {
  139. formats = NULL;
  140. avfilter_add_format(&formats, aconvert->out_sample_fmt);
  141. avfilter_formats_ref(formats, &outlink->in_formats);
  142. } else
  143. avfilter_formats_ref(avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO),
  144. &outlink->in_formats);
  145. avfilter_formats_ref(avfilter_make_all_channel_layouts(),
  146. &inlink->out_chlayouts);
  147. if (aconvert->out_chlayout != 0) {
  148. formats = NULL;
  149. avfilter_add_format(&formats, aconvert->out_chlayout);
  150. avfilter_formats_ref(formats, &outlink->in_chlayouts);
  151. } else
  152. avfilter_formats_ref(avfilter_make_all_channel_layouts(),
  153. &outlink->in_chlayouts);
  154. avfilter_formats_ref(avfilter_make_all_packing_formats(),
  155. &inlink->out_packing);
  156. if (aconvert->out_packing_fmt != -1) {
  157. formats = NULL;
  158. avfilter_add_format(&formats, aconvert->out_packing_fmt);
  159. avfilter_formats_ref(formats, &outlink->in_packing);
  160. } else
  161. avfilter_formats_ref(avfilter_make_all_packing_formats(),
  162. &outlink->in_packing);
  163. return 0;
  164. }
  165. static int config_output(AVFilterLink *outlink)
  166. {
  167. AVFilterLink *inlink = outlink->src->inputs[0];
  168. AConvertContext *aconvert = outlink->src->priv;
  169. char buf1[64], buf2[64];
  170. aconvert->in_sample_fmt = inlink->format;
  171. aconvert->in_packing_fmt = inlink->planar;
  172. if (aconvert->out_packing_fmt == -1)
  173. aconvert->out_packing_fmt = outlink->planar;
  174. aconvert->in_chlayout = inlink->channel_layout;
  175. aconvert->in_nb_channels =
  176. av_get_channel_layout_nb_channels(inlink->channel_layout);
  177. /* if not specified in args, use the format and layout of the output */
  178. if (aconvert->out_sample_fmt == AV_SAMPLE_FMT_NONE)
  179. aconvert->out_sample_fmt = outlink->format;
  180. if (aconvert->out_chlayout == 0)
  181. aconvert->out_chlayout = outlink->channel_layout;
  182. aconvert->out_nb_channels =
  183. av_get_channel_layout_nb_channels(outlink->channel_layout);
  184. av_get_channel_layout_string(buf1, sizeof(buf1),
  185. -1, inlink ->channel_layout);
  186. av_get_channel_layout_string(buf2, sizeof(buf2),
  187. -1, outlink->channel_layout);
  188. av_log(outlink->src, AV_LOG_INFO,
  189. "fmt:%s cl:%s planar:%i -> fmt:%s cl:%s planar:%i\n",
  190. av_get_sample_fmt_name(inlink ->format), buf1, inlink ->planar,
  191. av_get_sample_fmt_name(outlink->format), buf2, outlink->planar);
  192. /* compute which channel layout conversion to use */
  193. if (inlink->channel_layout != outlink->channel_layout) {
  194. int i;
  195. for (i = 0; i < sizeof(rematrix_funcs); i++) {
  196. const struct RematrixFunctionInfo *f = &rematrix_funcs[i];
  197. if ((f->in_chlayout == 0 || f->in_chlayout == inlink ->channel_layout) &&
  198. (f->out_chlayout == 0 || f->out_chlayout == outlink->channel_layout) &&
  199. (f->planar == -1 || f->planar == inlink->planar) &&
  200. (f->sfmt == -1 || f->sfmt == inlink->format)
  201. ) {
  202. aconvert->convert_chlayout = f->func;
  203. break;
  204. }
  205. }
  206. if (!aconvert->convert_chlayout) {
  207. av_log(outlink->src, AV_LOG_ERROR,
  208. "Unsupported channel layout conversion '%s -> %s' requested!\n",
  209. buf1, buf2);
  210. return AVERROR(EINVAL);
  211. }
  212. }
  213. return 0;
  214. }
  215. static int init_buffers(AVFilterLink *inlink, int nb_samples)
  216. {
  217. AConvertContext *aconvert = inlink->dst->priv;
  218. AVFilterLink * const outlink = inlink->dst->outputs[0];
  219. int i, packed_stride = 0;
  220. const unsigned
  221. packing_conv = inlink->planar != outlink->planar &&
  222. aconvert->out_nb_channels != 1,
  223. format_conv = inlink->format != outlink->format;
  224. int nb_channels = aconvert->out_nb_channels;
  225. uninit(inlink->dst);
  226. aconvert->max_nb_samples = nb_samples;
  227. if (aconvert->convert_chlayout) {
  228. /* allocate buffer for storing intermediary mixing samplesref */
  229. uint8_t *data[8];
  230. int linesize[8];
  231. int nb_channels = av_get_channel_layout_nb_channels(outlink->channel_layout);
  232. if (av_samples_alloc(data, linesize, nb_channels, nb_samples,
  233. inlink->format, 16) < 0)
  234. goto fail_no_mem;
  235. aconvert->mix_samplesref =
  236. avfilter_get_audio_buffer_ref_from_arrays(data, linesize, AV_PERM_WRITE,
  237. nb_samples, inlink->format,
  238. outlink->channel_layout,
  239. inlink->planar);
  240. if (!aconvert->mix_samplesref)
  241. goto fail_no_mem;
  242. }
  243. // if there's a format/packing conversion we need an audio_convert context
  244. if (format_conv || packing_conv) {
  245. aconvert->out_samplesref =
  246. avfilter_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
  247. if (!aconvert->out_samplesref)
  248. goto fail_no_mem;
  249. aconvert->in_strides [0] = av_get_bytes_per_sample(inlink ->format);
  250. aconvert->out_strides[0] = av_get_bytes_per_sample(outlink->format);
  251. aconvert->out_conv = aconvert->out_samplesref->data;
  252. if (aconvert->mix_samplesref)
  253. aconvert->in_conv = aconvert->mix_samplesref->data;
  254. if (packing_conv) {
  255. // packed -> planar
  256. if (outlink->planar == AVFILTER_PLANAR) {
  257. if (aconvert->mix_samplesref)
  258. aconvert->packed_data[0] = aconvert->mix_samplesref->data[0];
  259. aconvert->in_conv = aconvert->packed_data;
  260. packed_stride = aconvert->in_strides[0];
  261. aconvert->in_strides[0] *= nb_channels;
  262. // planar -> packed
  263. } else {
  264. aconvert->packed_data[0] = aconvert->out_samplesref->data[0];
  265. aconvert->out_conv = aconvert->packed_data;
  266. packed_stride = aconvert->out_strides[0];
  267. aconvert->out_strides[0] *= nb_channels;
  268. }
  269. } else if (outlink->planar == AVFILTER_PACKED) {
  270. /* If there's no packing conversion, and the stream is packed
  271. * then we treat the entire stream as one big channel
  272. */
  273. nb_channels = 1;
  274. }
  275. for (i = 1; i < nb_channels; i++) {
  276. aconvert->packed_data[i] = aconvert->packed_data[i-1] + packed_stride;
  277. aconvert->in_strides[i] = aconvert->in_strides[0];
  278. aconvert->out_strides[i] = aconvert->out_strides[0];
  279. }
  280. aconvert->audioconvert_ctx =
  281. av_audio_convert_alloc(outlink->format, nb_channels,
  282. inlink->format, nb_channels, NULL, 0);
  283. if (!aconvert->audioconvert_ctx)
  284. goto fail_no_mem;
  285. }
  286. return 0;
  287. fail_no_mem:
  288. av_log(inlink->dst, AV_LOG_ERROR, "Could not allocate memory.\n");
  289. return AVERROR(ENOMEM);
  290. }
  291. static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
  292. {
  293. AConvertContext *aconvert = inlink->dst->priv;
  294. AVFilterBufferRef *curbuf = insamplesref;
  295. AVFilterLink * const outlink = inlink->dst->outputs[0];
  296. int chan_mult;
  297. /* in/reinint the internal buffers if this is the first buffer
  298. * provided or it is needed to use a bigger one */
  299. if (!aconvert->max_nb_samples ||
  300. (curbuf->audio->nb_samples > aconvert->max_nb_samples))
  301. if (init_buffers(inlink, curbuf->audio->nb_samples) < 0) {
  302. av_log(inlink->dst, AV_LOG_ERROR, "Could not initialize buffers.\n");
  303. return;
  304. }
  305. /* if channel mixing is required */
  306. if (aconvert->mix_samplesref) {
  307. memcpy(aconvert->in_mix, curbuf->data, sizeof(aconvert->in_mix));
  308. memcpy(aconvert->out_mix, aconvert->mix_samplesref->data, sizeof(aconvert->out_mix));
  309. aconvert->convert_chlayout(aconvert->out_mix,
  310. aconvert->in_mix,
  311. curbuf->audio->nb_samples,
  312. aconvert);
  313. curbuf = aconvert->mix_samplesref;
  314. }
  315. if (aconvert->audioconvert_ctx) {
  316. if (!aconvert->mix_samplesref) {
  317. if (aconvert->in_conv == aconvert->packed_data) {
  318. int i, packed_stride = av_get_bytes_per_sample(inlink->format);
  319. aconvert->packed_data[0] = curbuf->data[0];
  320. for (i = 1; i < aconvert->out_nb_channels; i++)
  321. aconvert->packed_data[i] = aconvert->packed_data[i-1] + packed_stride;
  322. } else {
  323. aconvert->in_conv = curbuf->data;
  324. }
  325. }
  326. chan_mult = inlink->planar == outlink->planar && inlink->planar == 0 ?
  327. aconvert->out_nb_channels : 1;
  328. av_audio_convert(aconvert->audioconvert_ctx,
  329. (void * const *) aconvert->out_conv,
  330. aconvert->out_strides,
  331. (const void * const *) aconvert->in_conv,
  332. aconvert->in_strides,
  333. curbuf->audio->nb_samples * chan_mult);
  334. curbuf = aconvert->out_samplesref;
  335. }
  336. avfilter_copy_buffer_ref_props(curbuf, insamplesref);
  337. curbuf->audio->channel_layout = outlink->channel_layout;
  338. curbuf->audio->planar = outlink->planar;
  339. avfilter_filter_samples(inlink->dst->outputs[0],
  340. avfilter_ref_buffer(curbuf, ~0));
  341. avfilter_unref_buffer(insamplesref);
  342. }
  343. AVFilter avfilter_af_aconvert = {
  344. .name = "aconvert",
  345. .description = NULL_IF_CONFIG_SMALL("Convert the input audio to sample_fmt:channel_layout:packed_fmt."),
  346. .priv_size = sizeof(AConvertContext),
  347. .init = init,
  348. .uninit = uninit,
  349. .query_formats = query_formats,
  350. .inputs = (const AVFilterPad[]) {{ .name = "default",
  351. .type = AVMEDIA_TYPE_AUDIO,
  352. .filter_samples = filter_samples,
  353. .min_perms = AV_PERM_READ, },
  354. { .name = NULL}},
  355. .outputs = (const AVFilterPad[]) {{ .name = "default",
  356. .type = AVMEDIA_TYPE_AUDIO,
  357. .config_props = config_output, },
  358. { .name = NULL}},
  359. };