af_pan.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Copyright (c) 2002 Anders Johansson <ajh@atri.curtin.edu.au>
  3. * Copyright (c) 2011 Clément Bœsch <ubitux@gmail.com>
  4. * Copyright (c) 2011 Nicolas George <nicolas.george@normalesup.org>
  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
  16. * GNU 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. * Audio panning filter (channels mixing)
  25. * Original code written by Anders Johansson for MPlayer,
  26. * reimplemented for FFmpeg.
  27. */
  28. #include <stdio.h>
  29. #include "libavutil/audioconvert.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/opt.h"
  32. #include "libswresample/swresample.h"
  33. #include "audio.h"
  34. #include "avfilter.h"
  35. #include "formats.h"
  36. #include "internal.h"
  37. #define MAX_CHANNELS 63
  38. typedef struct PanContext {
  39. int64_t out_channel_layout;
  40. double gain[MAX_CHANNELS][MAX_CHANNELS];
  41. int64_t need_renorm;
  42. int need_renumber;
  43. int nb_input_channels;
  44. int nb_output_channels;
  45. int pure_gains;
  46. /* channel mapping specific */
  47. int channel_map[SWR_CH_MAX];
  48. struct SwrContext *swr;
  49. } PanContext;
  50. static int parse_channel_name(char **arg, int *rchannel, int *rnamed)
  51. {
  52. char buf[8];
  53. int len, i, channel_id = 0;
  54. int64_t layout, layout0;
  55. /* try to parse a channel name, e.g. "FL" */
  56. if (sscanf(*arg, "%7[A-Z]%n", buf, &len)) {
  57. layout0 = layout = av_get_channel_layout(buf);
  58. /* channel_id <- first set bit in layout */
  59. for (i = 32; i > 0; i >>= 1) {
  60. if (layout >= (int64_t)1 << i) {
  61. channel_id += i;
  62. layout >>= i;
  63. }
  64. }
  65. /* reject layouts that are not a single channel */
  66. if (channel_id >= MAX_CHANNELS || layout0 != (int64_t)1 << channel_id)
  67. return AVERROR(EINVAL);
  68. *rchannel = channel_id;
  69. *rnamed = 1;
  70. *arg += len;
  71. return 0;
  72. }
  73. /* try to parse a channel number, e.g. "c2" */
  74. if (sscanf(*arg, "c%d%n", &channel_id, &len) &&
  75. channel_id >= 0 && channel_id < MAX_CHANNELS) {
  76. *rchannel = channel_id;
  77. *rnamed = 0;
  78. *arg += len;
  79. return 0;
  80. }
  81. return AVERROR(EINVAL);
  82. }
  83. static void skip_spaces(char **arg)
  84. {
  85. int len = 0;
  86. sscanf(*arg, " %n", &len);
  87. *arg += len;
  88. }
  89. static av_cold int init(AVFilterContext *ctx, const char *args0)
  90. {
  91. PanContext *const pan = ctx->priv;
  92. char *arg, *arg0, *tokenizer, *args = av_strdup(args0);
  93. int out_ch_id, in_ch_id, len, named, ret;
  94. int nb_in_channels[2] = { 0, 0 }; // number of unnamed and named input channels
  95. double gain;
  96. if (!args0) {
  97. av_log(ctx, AV_LOG_ERROR,
  98. "pan filter needs a channel layout and a set "
  99. "of channels definitions as parameter\n");
  100. return AVERROR(EINVAL);
  101. }
  102. if (!args)
  103. return AVERROR(ENOMEM);
  104. arg = av_strtok(args, ":", &tokenizer);
  105. ret = ff_parse_channel_layout(&pan->out_channel_layout, arg, ctx);
  106. if (ret < 0)
  107. return ret;
  108. pan->nb_output_channels = av_get_channel_layout_nb_channels(pan->out_channel_layout);
  109. /* parse channel specifications */
  110. while ((arg = arg0 = av_strtok(NULL, ":", &tokenizer))) {
  111. /* channel name */
  112. if (parse_channel_name(&arg, &out_ch_id, &named)) {
  113. av_log(ctx, AV_LOG_ERROR,
  114. "Expected out channel name, got \"%.8s\"\n", arg);
  115. return AVERROR(EINVAL);
  116. }
  117. if (named) {
  118. if (!((pan->out_channel_layout >> out_ch_id) & 1)) {
  119. av_log(ctx, AV_LOG_ERROR,
  120. "Channel \"%.8s\" does not exist in the chosen layout\n", arg0);
  121. return AVERROR(EINVAL);
  122. }
  123. /* get the channel number in the output channel layout:
  124. * out_channel_layout & ((1 << out_ch_id) - 1) are all the
  125. * channels that come before out_ch_id,
  126. * so their count is the index of out_ch_id */
  127. out_ch_id = av_get_channel_layout_nb_channels(pan->out_channel_layout & (((int64_t)1 << out_ch_id) - 1));
  128. }
  129. if (out_ch_id < 0 || out_ch_id >= pan->nb_output_channels) {
  130. av_log(ctx, AV_LOG_ERROR,
  131. "Invalid out channel name \"%.8s\"\n", arg0);
  132. return AVERROR(EINVAL);
  133. }
  134. skip_spaces(&arg);
  135. if (*arg == '=') {
  136. arg++;
  137. } else if (*arg == '<') {
  138. pan->need_renorm |= (int64_t)1 << out_ch_id;
  139. arg++;
  140. } else {
  141. av_log(ctx, AV_LOG_ERROR,
  142. "Syntax error after channel name in \"%.8s\"\n", arg0);
  143. return AVERROR(EINVAL);
  144. }
  145. /* gains */
  146. while (1) {
  147. gain = 1;
  148. if (sscanf(arg, "%lf%n *%n", &gain, &len, &len))
  149. arg += len;
  150. if (parse_channel_name(&arg, &in_ch_id, &named)){
  151. av_log(ctx, AV_LOG_ERROR,
  152. "Expected in channel name, got \"%.8s\"\n", arg);
  153. return AVERROR(EINVAL);
  154. }
  155. nb_in_channels[named]++;
  156. if (nb_in_channels[!named]) {
  157. av_log(ctx, AV_LOG_ERROR,
  158. "Can not mix named and numbered channels\n");
  159. return AVERROR(EINVAL);
  160. }
  161. pan->gain[out_ch_id][in_ch_id] = gain;
  162. skip_spaces(&arg);
  163. if (!*arg)
  164. break;
  165. if (*arg != '+') {
  166. av_log(ctx, AV_LOG_ERROR, "Syntax error near \"%.8s\"\n", arg);
  167. return AVERROR(EINVAL);
  168. }
  169. arg++;
  170. }
  171. }
  172. pan->need_renumber = !!nb_in_channels[1];
  173. av_free(args);
  174. return 0;
  175. }
  176. static int are_gains_pure(const PanContext *pan)
  177. {
  178. int i, j;
  179. for (i = 0; i < MAX_CHANNELS; i++) {
  180. int nb_gain = 0;
  181. for (j = 0; j < MAX_CHANNELS; j++) {
  182. double gain = pan->gain[i][j];
  183. /* channel mapping is effective only if 0% or 100% of a channel is
  184. * selected... */
  185. if (gain != 0. && gain != 1.)
  186. return 0;
  187. /* ...and if the output channel is only composed of one input */
  188. if (gain && nb_gain++)
  189. return 0;
  190. }
  191. }
  192. return 1;
  193. }
  194. static int query_formats(AVFilterContext *ctx)
  195. {
  196. PanContext *pan = ctx->priv;
  197. AVFilterLink *inlink = ctx->inputs[0];
  198. AVFilterLink *outlink = ctx->outputs[0];
  199. AVFilterFormats *formats = NULL;
  200. AVFilterChannelLayouts *layouts;
  201. pan->pure_gains = are_gains_pure(pan);
  202. /* libswr supports any sample and packing formats */
  203. ff_set_common_formats(ctx, ff_all_formats(AVMEDIA_TYPE_AUDIO));
  204. formats = ff_all_samplerates();
  205. if (!formats)
  206. return AVERROR(ENOMEM);
  207. ff_set_common_samplerates(ctx, formats);
  208. // inlink supports any channel layout
  209. layouts = ff_all_channel_layouts();
  210. ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
  211. // outlink supports only requested output channel layout
  212. layouts = NULL;
  213. ff_add_channel_layout(&layouts, pan->out_channel_layout);
  214. ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
  215. return 0;
  216. }
  217. static int config_props(AVFilterLink *link)
  218. {
  219. AVFilterContext *ctx = link->dst;
  220. PanContext *pan = ctx->priv;
  221. char buf[1024], *cur;
  222. int i, j, k, r;
  223. double t;
  224. pan->nb_input_channels = av_get_channel_layout_nb_channels(link->channel_layout);
  225. if (pan->need_renumber) {
  226. // input channels were given by their name: renumber them
  227. for (i = j = 0; i < MAX_CHANNELS; i++) {
  228. if ((link->channel_layout >> i) & 1) {
  229. for (k = 0; k < pan->nb_output_channels; k++)
  230. pan->gain[k][j] = pan->gain[k][i];
  231. j++;
  232. }
  233. }
  234. }
  235. // sanity check; can't be done in query_formats since the inlink
  236. // channel layout is unknown at that time
  237. if (pan->nb_input_channels > SWR_CH_MAX ||
  238. pan->nb_output_channels > SWR_CH_MAX) {
  239. av_log(ctx, AV_LOG_ERROR,
  240. "libswresample support a maximum of %d channels. "
  241. "Feel free to ask for a higher limit.\n", SWR_CH_MAX);
  242. return AVERROR_PATCHWELCOME;
  243. }
  244. // init libswresample context
  245. pan->swr = swr_alloc_set_opts(pan->swr,
  246. pan->out_channel_layout, link->format, link->sample_rate,
  247. link->channel_layout, link->format, link->sample_rate,
  248. 0, ctx);
  249. if (!pan->swr)
  250. return AVERROR(ENOMEM);
  251. // gains are pure, init the channel mapping
  252. if (pan->pure_gains) {
  253. // get channel map from the pure gains
  254. for (i = 0; i < pan->nb_output_channels; i++) {
  255. int ch_id = -1;
  256. for (j = 0; j < pan->nb_input_channels; j++) {
  257. if (pan->gain[i][j]) {
  258. ch_id = j;
  259. break;
  260. }
  261. }
  262. pan->channel_map[i] = ch_id;
  263. }
  264. av_opt_set_int(pan->swr, "icl", pan->out_channel_layout, 0);
  265. av_opt_set_int(pan->swr, "uch", pan->nb_output_channels, 0);
  266. swr_set_channel_mapping(pan->swr, pan->channel_map);
  267. } else {
  268. // renormalize
  269. for (i = 0; i < pan->nb_output_channels; i++) {
  270. if (!((pan->need_renorm >> i) & 1))
  271. continue;
  272. t = 0;
  273. for (j = 0; j < pan->nb_input_channels; j++)
  274. t += pan->gain[i][j];
  275. if (t > -1E-5 && t < 1E-5) {
  276. // t is almost 0 but not exactly, this is probably a mistake
  277. if (t)
  278. av_log(ctx, AV_LOG_WARNING,
  279. "Degenerate coefficients while renormalizing\n");
  280. continue;
  281. }
  282. for (j = 0; j < pan->nb_input_channels; j++)
  283. pan->gain[i][j] /= t;
  284. }
  285. av_opt_set_int(pan->swr, "icl", link->channel_layout, 0);
  286. av_opt_set_int(pan->swr, "ocl", pan->out_channel_layout, 0);
  287. swr_set_matrix(pan->swr, pan->gain[0], pan->gain[1] - pan->gain[0]);
  288. }
  289. r = swr_init(pan->swr);
  290. if (r < 0)
  291. return r;
  292. // summary
  293. for (i = 0; i < pan->nb_output_channels; i++) {
  294. cur = buf;
  295. for (j = 0; j < pan->nb_input_channels; j++) {
  296. r = snprintf(cur, buf + sizeof(buf) - cur, "%s%.3g i%d",
  297. j ? " + " : "", pan->gain[i][j], j);
  298. cur += FFMIN(buf + sizeof(buf) - cur, r);
  299. }
  300. av_log(ctx, AV_LOG_VERBOSE, "o%d = %s\n", i, buf);
  301. }
  302. // add channel mapping summary if possible
  303. if (pan->pure_gains) {
  304. av_log(ctx, AV_LOG_INFO, "Pure channel mapping detected:");
  305. for (i = 0; i < pan->nb_output_channels; i++)
  306. if (pan->channel_map[i] < 0)
  307. av_log(ctx, AV_LOG_INFO, " M");
  308. else
  309. av_log(ctx, AV_LOG_INFO, " %d", pan->channel_map[i]);
  310. av_log(ctx, AV_LOG_INFO, "\n");
  311. return 0;
  312. }
  313. return 0;
  314. }
  315. static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  316. {
  317. int ret;
  318. int n = insamples->audio->nb_samples;
  319. AVFilterLink *const outlink = inlink->dst->outputs[0];
  320. AVFilterBufferRef *outsamples = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n);
  321. PanContext *pan = inlink->dst->priv;
  322. swr_convert(pan->swr, outsamples->data, n, (void *)insamples->data, n);
  323. avfilter_copy_buffer_ref_props(outsamples, insamples);
  324. outsamples->audio->channel_layout = outlink->channel_layout;
  325. ret = ff_filter_samples(outlink, outsamples);
  326. avfilter_unref_buffer(insamples);
  327. return ret;
  328. }
  329. static av_cold void uninit(AVFilterContext *ctx)
  330. {
  331. PanContext *pan = ctx->priv;
  332. swr_free(&pan->swr);
  333. }
  334. AVFilter avfilter_af_pan = {
  335. .name = "pan",
  336. .description = NULL_IF_CONFIG_SMALL("Remix channels with coefficients (panning)."),
  337. .priv_size = sizeof(PanContext),
  338. .init = init,
  339. .uninit = uninit,
  340. .query_formats = query_formats,
  341. .inputs = (const AVFilterPad[]) {
  342. { .name = "default",
  343. .type = AVMEDIA_TYPE_AUDIO,
  344. .config_props = config_props,
  345. .filter_samples = filter_samples,
  346. .min_perms = AV_PERM_READ, },
  347. { .name = NULL}
  348. },
  349. .outputs = (const AVFilterPad[]) {
  350. { .name = "default",
  351. .type = AVMEDIA_TYPE_AUDIO, },
  352. { .name = NULL}
  353. },
  354. };