You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

445 lines
14KB

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