af_pan.c 14 KB

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