af_aconvert.c 17 KB

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