af_pan.c 13 KB

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