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.

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