af_pan.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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/avstring.h"
  30. #include "libavutil/channel_layout.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. goto fail;
  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. ret = AVERROR(EINVAL);
  116. goto fail;
  117. }
  118. if (named) {
  119. if (!((pan->out_channel_layout >> out_ch_id) & 1)) {
  120. av_log(ctx, AV_LOG_ERROR,
  121. "Channel \"%.8s\" does not exist in the chosen layout\n", arg0);
  122. ret = AVERROR(EINVAL);
  123. goto fail;
  124. }
  125. /* get the channel number in the output channel layout:
  126. * out_channel_layout & ((1 << out_ch_id) - 1) are all the
  127. * channels that come before out_ch_id,
  128. * so their count is the index of out_ch_id */
  129. out_ch_id = av_get_channel_layout_nb_channels(pan->out_channel_layout & (((int64_t)1 << out_ch_id) - 1));
  130. }
  131. if (out_ch_id < 0 || out_ch_id >= pan->nb_output_channels) {
  132. av_log(ctx, AV_LOG_ERROR,
  133. "Invalid out channel name \"%.8s\"\n", arg0);
  134. ret = AVERROR(EINVAL);
  135. goto fail;
  136. }
  137. skip_spaces(&arg);
  138. if (*arg == '=') {
  139. arg++;
  140. } else if (*arg == '<') {
  141. pan->need_renorm |= (int64_t)1 << out_ch_id;
  142. arg++;
  143. } else {
  144. av_log(ctx, AV_LOG_ERROR,
  145. "Syntax error after channel name in \"%.8s\"\n", arg0);
  146. ret = AVERROR(EINVAL);
  147. goto fail;
  148. }
  149. /* gains */
  150. while (1) {
  151. gain = 1;
  152. if (sscanf(arg, "%lf%n *%n", &gain, &len, &len))
  153. arg += len;
  154. if (parse_channel_name(&arg, &in_ch_id, &named)){
  155. av_log(ctx, AV_LOG_ERROR,
  156. "Expected in channel name, got \"%.8s\"\n", arg);
  157. ret = AVERROR(EINVAL);
  158. goto fail;
  159. }
  160. nb_in_channels[named]++;
  161. if (nb_in_channels[!named]) {
  162. av_log(ctx, AV_LOG_ERROR,
  163. "Can not mix named and numbered channels\n");
  164. ret = AVERROR(EINVAL);
  165. goto fail;
  166. }
  167. pan->gain[out_ch_id][in_ch_id] = gain;
  168. skip_spaces(&arg);
  169. if (!*arg)
  170. break;
  171. if (*arg != '+') {
  172. av_log(ctx, AV_LOG_ERROR, "Syntax error near \"%.8s\"\n", arg);
  173. ret = AVERROR(EINVAL);
  174. goto fail;
  175. }
  176. arg++;
  177. }
  178. }
  179. pan->need_renumber = !!nb_in_channels[1];
  180. ret = 0;
  181. fail:
  182. av_free(args);
  183. return ret;
  184. }
  185. static int are_gains_pure(const PanContext *pan)
  186. {
  187. int i, j;
  188. for (i = 0; i < MAX_CHANNELS; i++) {
  189. int nb_gain = 0;
  190. for (j = 0; j < MAX_CHANNELS; j++) {
  191. double gain = pan->gain[i][j];
  192. /* channel mapping is effective only if 0% or 100% of a channel is
  193. * selected... */
  194. if (gain != 0. && gain != 1.)
  195. return 0;
  196. /* ...and if the output channel is only composed of one input */
  197. if (gain && nb_gain++)
  198. return 0;
  199. }
  200. }
  201. return 1;
  202. }
  203. static int query_formats(AVFilterContext *ctx)
  204. {
  205. PanContext *pan = ctx->priv;
  206. AVFilterLink *inlink = ctx->inputs[0];
  207. AVFilterLink *outlink = ctx->outputs[0];
  208. AVFilterFormats *formats = NULL;
  209. AVFilterChannelLayouts *layouts;
  210. pan->pure_gains = are_gains_pure(pan);
  211. /* libswr supports any sample and packing formats */
  212. ff_set_common_formats(ctx, ff_all_formats(AVMEDIA_TYPE_AUDIO));
  213. formats = ff_all_samplerates();
  214. if (!formats)
  215. return AVERROR(ENOMEM);
  216. ff_set_common_samplerates(ctx, formats);
  217. // inlink supports any channel layout
  218. layouts = ff_all_channel_layouts();
  219. ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
  220. // outlink supports only requested output channel layout
  221. layouts = NULL;
  222. ff_add_channel_layout(&layouts, pan->out_channel_layout);
  223. ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
  224. return 0;
  225. }
  226. static int config_props(AVFilterLink *link)
  227. {
  228. AVFilterContext *ctx = link->dst;
  229. PanContext *pan = ctx->priv;
  230. char buf[1024], *cur;
  231. int i, j, k, r;
  232. double t;
  233. pan->nb_input_channels = av_get_channel_layout_nb_channels(link->channel_layout);
  234. if (pan->need_renumber) {
  235. // input channels were given by their name: renumber them
  236. for (i = j = 0; i < MAX_CHANNELS; i++) {
  237. if ((link->channel_layout >> i) & 1) {
  238. for (k = 0; k < pan->nb_output_channels; k++)
  239. pan->gain[k][j] = pan->gain[k][i];
  240. j++;
  241. }
  242. }
  243. }
  244. // sanity check; can't be done in query_formats since the inlink
  245. // channel layout is unknown at that time
  246. if (pan->nb_input_channels > SWR_CH_MAX ||
  247. pan->nb_output_channels > SWR_CH_MAX) {
  248. av_log(ctx, AV_LOG_ERROR,
  249. "libswresample support a maximum of %d channels. "
  250. "Feel free to ask for a higher limit.\n", SWR_CH_MAX);
  251. return AVERROR_PATCHWELCOME;
  252. }
  253. // init libswresample context
  254. pan->swr = swr_alloc_set_opts(pan->swr,
  255. pan->out_channel_layout, link->format, link->sample_rate,
  256. link->channel_layout, link->format, link->sample_rate,
  257. 0, ctx);
  258. if (!pan->swr)
  259. return AVERROR(ENOMEM);
  260. // gains are pure, init the channel mapping
  261. if (pan->pure_gains) {
  262. // get channel map from the pure gains
  263. for (i = 0; i < pan->nb_output_channels; i++) {
  264. int ch_id = -1;
  265. for (j = 0; j < pan->nb_input_channels; j++) {
  266. if (pan->gain[i][j]) {
  267. ch_id = j;
  268. break;
  269. }
  270. }
  271. pan->channel_map[i] = ch_id;
  272. }
  273. av_opt_set_int(pan->swr, "icl", pan->out_channel_layout, 0);
  274. av_opt_set_int(pan->swr, "uch", pan->nb_output_channels, 0);
  275. swr_set_channel_mapping(pan->swr, pan->channel_map);
  276. } else {
  277. // renormalize
  278. for (i = 0; i < pan->nb_output_channels; i++) {
  279. if (!((pan->need_renorm >> i) & 1))
  280. continue;
  281. t = 0;
  282. for (j = 0; j < pan->nb_input_channels; j++)
  283. t += pan->gain[i][j];
  284. if (t > -1E-5 && t < 1E-5) {
  285. // t is almost 0 but not exactly, this is probably a mistake
  286. if (t)
  287. av_log(ctx, AV_LOG_WARNING,
  288. "Degenerate coefficients while renormalizing\n");
  289. continue;
  290. }
  291. for (j = 0; j < pan->nb_input_channels; j++)
  292. pan->gain[i][j] /= t;
  293. }
  294. av_opt_set_int(pan->swr, "icl", link->channel_layout, 0);
  295. av_opt_set_int(pan->swr, "ocl", pan->out_channel_layout, 0);
  296. swr_set_matrix(pan->swr, pan->gain[0], pan->gain[1] - pan->gain[0]);
  297. }
  298. r = swr_init(pan->swr);
  299. if (r < 0)
  300. return r;
  301. // summary
  302. for (i = 0; i < pan->nb_output_channels; i++) {
  303. cur = buf;
  304. for (j = 0; j < pan->nb_input_channels; j++) {
  305. r = snprintf(cur, buf + sizeof(buf) - cur, "%s%.3g i%d",
  306. j ? " + " : "", pan->gain[i][j], j);
  307. cur += FFMIN(buf + sizeof(buf) - cur, r);
  308. }
  309. av_log(ctx, AV_LOG_VERBOSE, "o%d = %s\n", i, buf);
  310. }
  311. // add channel mapping summary if possible
  312. if (pan->pure_gains) {
  313. av_log(ctx, AV_LOG_INFO, "Pure channel mapping detected:");
  314. for (i = 0; i < pan->nb_output_channels; i++)
  315. if (pan->channel_map[i] < 0)
  316. av_log(ctx, AV_LOG_INFO, " M");
  317. else
  318. av_log(ctx, AV_LOG_INFO, " %d", pan->channel_map[i]);
  319. av_log(ctx, AV_LOG_INFO, "\n");
  320. return 0;
  321. }
  322. return 0;
  323. }
  324. static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  325. {
  326. int ret;
  327. int n = insamples->audio->nb_samples;
  328. AVFilterLink *const outlink = inlink->dst->outputs[0];
  329. AVFilterBufferRef *outsamples = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n);
  330. PanContext *pan = inlink->dst->priv;
  331. swr_convert(pan->swr, outsamples->data, n, (void *)insamples->data, n);
  332. avfilter_copy_buffer_ref_props(outsamples, insamples);
  333. outsamples->audio->channel_layout = outlink->channel_layout;
  334. outsamples->audio->channels = outlink->channels;
  335. ret = ff_filter_frame(outlink, outsamples);
  336. avfilter_unref_buffer(insamples);
  337. return ret;
  338. }
  339. static av_cold void uninit(AVFilterContext *ctx)
  340. {
  341. PanContext *pan = ctx->priv;
  342. swr_free(&pan->swr);
  343. }
  344. static const AVFilterPad pan_inputs[] = {
  345. {
  346. .name = "default",
  347. .type = AVMEDIA_TYPE_AUDIO,
  348. .config_props = config_props,
  349. .filter_frame = filter_frame,
  350. .min_perms = AV_PERM_READ,
  351. },
  352. { NULL }
  353. };
  354. static const AVFilterPad pan_outputs[] = {
  355. {
  356. .name = "default",
  357. .type = AVMEDIA_TYPE_AUDIO,
  358. },
  359. { NULL }
  360. };
  361. AVFilter avfilter_af_pan = {
  362. .name = "pan",
  363. .description = NULL_IF_CONFIG_SMALL("Remix channels with coefficients (panning)."),
  364. .priv_size = sizeof(PanContext),
  365. .init = init,
  366. .uninit = uninit,
  367. .query_formats = query_formats,
  368. .inputs = pan_inputs,
  369. .outputs = pan_outputs,
  370. };