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.

424 lines
13KB

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