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.

383 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 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/opt.h"
  31. #include "libswresample/swresample.h"
  32. #include "audio.h"
  33. #include "avfilter.h"
  34. #include "formats.h"
  35. #define MAX_CHANNELS 63
  36. typedef struct PanContext {
  37. int64_t out_channel_layout;
  38. double gain[MAX_CHANNELS][MAX_CHANNELS];
  39. int64_t need_renorm;
  40. int need_renumber;
  41. int nb_input_channels;
  42. int nb_output_channels;
  43. int pure_gains;
  44. /* channel mapping specific */
  45. int channel_map[SWR_CH_MAX];
  46. struct SwrContext *swr;
  47. } PanContext;
  48. static int parse_channel_name(char **arg, int *rchannel, int *rnamed)
  49. {
  50. char buf[8];
  51. int len, i, channel_id = 0;
  52. int64_t layout, layout0;
  53. /* try to parse a channel name, e.g. "FL" */
  54. if (sscanf(*arg, " %7[A-Z] %n", buf, &len)) {
  55. layout0 = layout = av_get_channel_layout(buf);
  56. /* channel_id <- first set bit in layout */
  57. for (i = 32; i > 0; i >>= 1) {
  58. if (layout >= (int64_t)1 << i) {
  59. channel_id += i;
  60. layout >>= i;
  61. }
  62. }
  63. /* reject layouts that are not a single channel */
  64. if (channel_id >= MAX_CHANNELS || layout0 != (int64_t)1 << channel_id)
  65. return AVERROR(EINVAL);
  66. *rchannel = channel_id;
  67. *rnamed = 1;
  68. *arg += len;
  69. return 0;
  70. }
  71. /* try to parse a channel number, e.g. "c2" */
  72. if (sscanf(*arg, " c%d %n", &channel_id, &len) &&
  73. channel_id >= 0 && channel_id < MAX_CHANNELS) {
  74. *rchannel = channel_id;
  75. *rnamed = 0;
  76. *arg += len;
  77. return 0;
  78. }
  79. return AVERROR(EINVAL);
  80. }
  81. static void skip_spaces(char **arg)
  82. {
  83. int len = 0;
  84. sscanf(*arg, " %n", &len);
  85. *arg += len;
  86. }
  87. static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
  88. {
  89. PanContext *const pan = ctx->priv;
  90. char *arg, *arg0, *tokenizer, *args = av_strdup(args0);
  91. int out_ch_id, in_ch_id, len, named;
  92. int nb_in_channels[2] = { 0, 0 }; // number of unnamed and named input channels
  93. double gain;
  94. if (!args0) {
  95. av_log(ctx, AV_LOG_ERROR,
  96. "pan filter needs a channel layout and a set "
  97. "of channels definitions as parameter\n");
  98. return AVERROR(EINVAL);
  99. }
  100. if (!args)
  101. return AVERROR(ENOMEM);
  102. arg = av_strtok(args, ":", &tokenizer);
  103. pan->out_channel_layout = av_get_channel_layout(arg);
  104. if (!pan->out_channel_layout) {
  105. av_log(ctx, AV_LOG_ERROR, "Unknown channel layout \"%s\"\n", arg);
  106. return AVERROR(EINVAL);
  107. }
  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. AVFilterChannelLayouts *layouts;
  199. pan->pure_gains = are_gains_pure(pan);
  200. /* libswr supports any sample and packing formats */
  201. avfilter_set_common_sample_formats(ctx, avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO));
  202. // inlink supports any channel layout
  203. layouts = ff_all_channel_layouts();
  204. ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
  205. // outlink supports only requested output channel layout
  206. layouts = NULL;
  207. ff_add_channel_layout(&layouts, pan->out_channel_layout);
  208. ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
  209. return 0;
  210. }
  211. static int config_props(AVFilterLink *link)
  212. {
  213. AVFilterContext *ctx = link->dst;
  214. PanContext *pan = ctx->priv;
  215. char buf[1024], *cur;
  216. int i, j, k, r;
  217. double t;
  218. pan->nb_input_channels = av_get_channel_layout_nb_channels(link->channel_layout);
  219. if (pan->need_renumber) {
  220. // input channels were given by their name: renumber them
  221. for (i = j = 0; i < MAX_CHANNELS; i++) {
  222. if ((link->channel_layout >> i) & 1) {
  223. for (k = 0; k < pan->nb_output_channels; k++)
  224. pan->gain[k][j] = pan->gain[k][i];
  225. j++;
  226. }
  227. }
  228. }
  229. // sanity check; can't be done in query_formats since the inlink
  230. // channel layout is unknown at that time
  231. if (pan->nb_input_channels > SWR_CH_MAX ||
  232. pan->nb_output_channels > SWR_CH_MAX) {
  233. av_log(ctx, AV_LOG_ERROR,
  234. "libswresample support a maximum of %d channels. "
  235. "Feel free to ask for a higher limit.\n", SWR_CH_MAX);
  236. return AVERROR_PATCHWELCOME;
  237. }
  238. // init libswresample context
  239. pan->swr = swr_alloc_set_opts(pan->swr,
  240. pan->out_channel_layout, link->format, link->sample_rate,
  241. link->channel_layout, link->format, link->sample_rate,
  242. 0, ctx);
  243. if (!pan->swr)
  244. return AVERROR(ENOMEM);
  245. // gains are pure, init the channel mapping
  246. if (pan->pure_gains) {
  247. // get channel map from the pure gains
  248. for (i = 0; i < pan->nb_output_channels; i++) {
  249. int ch_id = -1;
  250. for (j = 0; j < pan->nb_input_channels; j++) {
  251. if (pan->gain[i][j]) {
  252. ch_id = j;
  253. break;
  254. }
  255. }
  256. pan->channel_map[i] = ch_id;
  257. }
  258. av_opt_set_int(pan->swr, "icl", pan->out_channel_layout, 0);
  259. av_opt_set_int(pan->swr, "uch", pan->nb_output_channels, 0);
  260. swr_set_channel_mapping(pan->swr, pan->channel_map);
  261. } else {
  262. // renormalize
  263. for (i = 0; i < pan->nb_output_channels; i++) {
  264. if (!((pan->need_renorm >> i) & 1))
  265. continue;
  266. t = 0;
  267. for (j = 0; j < pan->nb_input_channels; j++)
  268. t += pan->gain[i][j];
  269. if (t > -1E-5 && t < 1E-5) {
  270. // t is almost 0 but not exactly, this is probably a mistake
  271. if (t)
  272. av_log(ctx, AV_LOG_WARNING,
  273. "Degenerate coefficients while renormalizing\n");
  274. continue;
  275. }
  276. for (j = 0; j < pan->nb_input_channels; j++)
  277. pan->gain[i][j] /= t;
  278. }
  279. av_opt_set_int(pan->swr, "icl", link->channel_layout, 0);
  280. av_opt_set_int(pan->swr, "ocl", pan->out_channel_layout, 0);
  281. swr_set_matrix(pan->swr, pan->gain[0], pan->gain[1] - pan->gain[0]);
  282. }
  283. r = swr_init(pan->swr);
  284. if (r < 0)
  285. return r;
  286. // summary
  287. for (i = 0; i < pan->nb_output_channels; i++) {
  288. cur = buf;
  289. for (j = 0; j < pan->nb_input_channels; j++) {
  290. r = snprintf(cur, buf + sizeof(buf) - cur, "%s%.3g i%d",
  291. j ? " + " : "", pan->gain[i][j], j);
  292. cur += FFMIN(buf + sizeof(buf) - cur, r);
  293. }
  294. av_log(ctx, AV_LOG_INFO, "o%d = %s\n", i, buf);
  295. }
  296. // add channel mapping summary if possible
  297. if (pan->pure_gains) {
  298. av_log(ctx, AV_LOG_INFO, "Pure channel mapping detected:");
  299. for (i = 0; i < pan->nb_output_channels; i++)
  300. if (pan->channel_map[i] < 0)
  301. av_log(ctx, AV_LOG_INFO, " M");
  302. else
  303. av_log(ctx, AV_LOG_INFO, " %d", pan->channel_map[i]);
  304. av_log(ctx, AV_LOG_INFO, "\n");
  305. return 0;
  306. }
  307. return 0;
  308. }
  309. static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  310. {
  311. int n = insamples->audio->nb_samples;
  312. AVFilterLink *const outlink = inlink->dst->outputs[0];
  313. AVFilterBufferRef *outsamples = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n);
  314. PanContext *pan = inlink->dst->priv;
  315. swr_convert(pan->swr, outsamples->data, n, (void *)insamples->data, n);
  316. avfilter_copy_buffer_ref_props(outsamples, insamples);
  317. outsamples->audio->channel_layout = outlink->channel_layout;
  318. ff_filter_samples(outlink, outsamples);
  319. avfilter_unref_buffer(insamples);
  320. }
  321. static av_cold void uninit(AVFilterContext *ctx)
  322. {
  323. PanContext *pan = ctx->priv;
  324. swr_free(&pan->swr);
  325. }
  326. AVFilter avfilter_af_pan = {
  327. .name = "pan",
  328. .description = NULL_IF_CONFIG_SMALL("Remix channels with coefficients (panning)."),
  329. .priv_size = sizeof(PanContext),
  330. .init = init,
  331. .uninit = uninit,
  332. .query_formats = query_formats,
  333. .inputs = (const AVFilterPad[]) {
  334. { .name = "default",
  335. .type = AVMEDIA_TYPE_AUDIO,
  336. .config_props = config_props,
  337. .filter_samples = filter_samples,
  338. .min_perms = AV_PERM_READ, },
  339. { .name = NULL}
  340. },
  341. .outputs = (const AVFilterPad[]) {
  342. { .name = "default",
  343. .type = AVMEDIA_TYPE_AUDIO, },
  344. { .name = NULL}
  345. },
  346. };