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 "avfilter.h"
  33. #define MAX_CHANNELS 63
  34. typedef struct PanContext {
  35. int64_t out_channel_layout;
  36. double gain[MAX_CHANNELS][MAX_CHANNELS];
  37. int64_t need_renorm;
  38. int need_renumber;
  39. int nb_input_channels;
  40. int nb_output_channels;
  41. int pure_gains;
  42. /* channel mapping specific */
  43. int channel_map[SWR_CH_MAX];
  44. struct SwrContext *swr;
  45. } PanContext;
  46. static int parse_channel_name(char **arg, int *rchannel, int *rnamed)
  47. {
  48. char buf[8];
  49. int len, i, channel_id = 0;
  50. int64_t layout, layout0;
  51. /* try to parse a channel name, e.g. "FL" */
  52. if (sscanf(*arg, " %7[A-Z] %n", buf, &len)) {
  53. layout0 = layout = av_get_channel_layout(buf);
  54. /* channel_id <- first set bit in layout */
  55. for (i = 32; i > 0; i >>= 1) {
  56. if (layout >= (int64_t)1 << i) {
  57. channel_id += i;
  58. layout >>= i;
  59. }
  60. }
  61. /* reject layouts that are not a single channel */
  62. if (channel_id >= MAX_CHANNELS || layout0 != (int64_t)1 << channel_id)
  63. return AVERROR(EINVAL);
  64. *rchannel = channel_id;
  65. *rnamed = 1;
  66. *arg += len;
  67. return 0;
  68. }
  69. /* try to parse a channel number, e.g. "c2" */
  70. if (sscanf(*arg, " c%d %n", &channel_id, &len) &&
  71. channel_id >= 0 && channel_id < MAX_CHANNELS) {
  72. *rchannel = channel_id;
  73. *rnamed = 0;
  74. *arg += len;
  75. return 0;
  76. }
  77. return AVERROR(EINVAL);
  78. }
  79. static void skip_spaces(char **arg)
  80. {
  81. int len = 0;
  82. sscanf(*arg, " %n", &len);
  83. *arg += len;
  84. }
  85. static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
  86. {
  87. PanContext *const pan = ctx->priv;
  88. char *arg, *arg0, *tokenizer, *args = av_strdup(args0);
  89. int out_ch_id, in_ch_id, len, named;
  90. int nb_in_channels[2] = { 0, 0 }; // number of unnamed and named input channels
  91. double gain;
  92. if (!args0) {
  93. av_log(ctx, AV_LOG_ERROR,
  94. "pan filter needs a channel layout and a set "
  95. "of channels definitions as parameter\n");
  96. return AVERROR(EINVAL);
  97. }
  98. if (!args)
  99. return AVERROR(ENOMEM);
  100. arg = av_strtok(args, ":", &tokenizer);
  101. pan->out_channel_layout = av_get_channel_layout(arg);
  102. if (!pan->out_channel_layout) {
  103. av_log(ctx, AV_LOG_ERROR, "Unknown channel layout \"%s\"\n", arg);
  104. return AVERROR(EINVAL);
  105. }
  106. pan->nb_output_channels = av_get_channel_layout_nb_channels(pan->out_channel_layout);
  107. /* parse channel specifications */
  108. while ((arg = arg0 = av_strtok(NULL, ":", &tokenizer))) {
  109. /* channel name */
  110. if (parse_channel_name(&arg, &out_ch_id, &named)) {
  111. av_log(ctx, AV_LOG_ERROR,
  112. "Expected out channel name, got \"%.8s\"\n", arg);
  113. return AVERROR(EINVAL);
  114. }
  115. if (named) {
  116. if (!((pan->out_channel_layout >> out_ch_id) & 1)) {
  117. av_log(ctx, AV_LOG_ERROR,
  118. "Channel \"%.8s\" does not exist in the chosen layout\n", arg0);
  119. return AVERROR(EINVAL);
  120. }
  121. /* get the channel number in the output channel layout:
  122. * out_channel_layout & ((1 << out_ch_id) - 1) are all the
  123. * channels that come before out_ch_id,
  124. * so their count is the index of out_ch_id */
  125. out_ch_id = av_get_channel_layout_nb_channels(pan->out_channel_layout & (((int64_t)1 << out_ch_id) - 1));
  126. }
  127. if (out_ch_id < 0 || out_ch_id >= pan->nb_output_channels) {
  128. av_log(ctx, AV_LOG_ERROR,
  129. "Invalid out channel name \"%.8s\"\n", arg0);
  130. return AVERROR(EINVAL);
  131. }
  132. if (*arg == '=') {
  133. arg++;
  134. } else if (*arg == '<') {
  135. pan->need_renorm |= (int64_t)1 << out_ch_id;
  136. arg++;
  137. } else {
  138. av_log(ctx, AV_LOG_ERROR,
  139. "Syntax error after channel name in \"%.8s\"\n", arg0);
  140. return AVERROR(EINVAL);
  141. }
  142. /* gains */
  143. while (1) {
  144. gain = 1;
  145. if (sscanf(arg, " %lf %n* %n", &gain, &len, &len))
  146. arg += len;
  147. if (parse_channel_name(&arg, &in_ch_id, &named)){
  148. av_log(ctx, AV_LOG_ERROR,
  149. "Expected in channel name, got \"%.8s\"\n", arg);
  150. return AVERROR(EINVAL);
  151. }
  152. nb_in_channels[named]++;
  153. if (nb_in_channels[!named]) {
  154. av_log(ctx, AV_LOG_ERROR,
  155. "Can not mix named and numbered channels\n");
  156. return AVERROR(EINVAL);
  157. }
  158. pan->gain[out_ch_id][in_ch_id] = gain;
  159. if (!*arg)
  160. break;
  161. if (*arg != '+') {
  162. av_log(ctx, AV_LOG_ERROR, "Syntax error near \"%.8s\"\n", arg);
  163. return AVERROR(EINVAL);
  164. }
  165. arg++;
  166. skip_spaces(&arg);
  167. }
  168. }
  169. pan->need_renumber = !!nb_in_channels[1];
  170. av_free(args);
  171. return 0;
  172. }
  173. static int are_gains_pure(const PanContext *pan)
  174. {
  175. int i, j;
  176. for (i = 0; i < MAX_CHANNELS; i++) {
  177. int nb_gain = 0;
  178. for (j = 0; j < MAX_CHANNELS; j++) {
  179. double gain = pan->gain[i][j];
  180. /* channel mapping is effective only if 0% or 100% of a channel is
  181. * selected... */
  182. if (gain != 0. && gain != 1.)
  183. return 0;
  184. /* ...and if the output channel is only composed of one input */
  185. if (gain && nb_gain++)
  186. return 0;
  187. }
  188. }
  189. return 1;
  190. }
  191. static int query_formats(AVFilterContext *ctx)
  192. {
  193. PanContext *pan = ctx->priv;
  194. AVFilterLink *inlink = ctx->inputs[0];
  195. AVFilterLink *outlink = ctx->outputs[0];
  196. AVFilterFormats *formats;
  197. pan->pure_gains = are_gains_pure(pan);
  198. /* libswr supports any sample and packing formats */
  199. avfilter_set_common_sample_formats(ctx, avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO));
  200. avfilter_set_common_packing_formats(ctx, avfilter_make_all_packing_formats());
  201. // inlink supports any channel layout
  202. formats = avfilter_make_all_channel_layouts();
  203. avfilter_formats_ref(formats, &inlink->out_chlayouts);
  204. // outlink supports only requested output channel layout
  205. formats = NULL;
  206. avfilter_add_format(&formats, pan->out_channel_layout);
  207. avfilter_formats_ref(formats, &outlink->in_chlayouts);
  208. return 0;
  209. }
  210. static int config_props(AVFilterLink *link)
  211. {
  212. AVFilterContext *ctx = link->dst;
  213. PanContext *pan = ctx->priv;
  214. char buf[1024], *cur;
  215. int i, j, k, r;
  216. double t;
  217. pan->nb_input_channels = av_get_channel_layout_nb_channels(link->channel_layout);
  218. if (pan->need_renumber) {
  219. // input channels were given by their name: renumber them
  220. for (i = j = 0; i < MAX_CHANNELS; i++) {
  221. if ((link->channel_layout >> i) & 1) {
  222. for (k = 0; k < pan->nb_output_channels; k++)
  223. pan->gain[k][j] = pan->gain[k][i];
  224. j++;
  225. }
  226. }
  227. }
  228. // sanity check; can't be done in query_formats since the inlink
  229. // channel layout is unknown at that time
  230. if (pan->nb_input_channels > SWR_CH_MAX ||
  231. pan->nb_output_channels > SWR_CH_MAX) {
  232. av_log(ctx, AV_LOG_ERROR,
  233. "libswresample support a maximum of %d channels. "
  234. "Feel free to ask for a higher limit.\n", SWR_CH_MAX);
  235. return AVERROR_PATCHWELCOME;
  236. }
  237. // init libswresample context
  238. pan->swr = swr_alloc_set_opts(pan->swr,
  239. pan->out_channel_layout, link->format, link->sample_rate,
  240. link->channel_layout, link->format, link->sample_rate,
  241. 0, ctx);
  242. if (!pan->swr)
  243. return AVERROR(ENOMEM);
  244. // gains are pure, init the channel mapping
  245. if (pan->pure_gains) {
  246. // get channel map from the pure gains
  247. for (i = 0; i < pan->nb_output_channels; i++) {
  248. int ch_id = -1;
  249. for (j = 0; j < pan->nb_input_channels; j++) {
  250. if (pan->gain[i][j]) {
  251. ch_id = j;
  252. break;
  253. }
  254. }
  255. pan->channel_map[i] = ch_id;
  256. }
  257. av_opt_set_int(pan->swr, "icl", pan->out_channel_layout, 0);
  258. av_opt_set_int(pan->swr, "uch", pan->nb_output_channels, 0);
  259. swr_set_channel_mapping(pan->swr, pan->channel_map);
  260. } else {
  261. // renormalize
  262. for (i = 0; i < pan->nb_output_channels; i++) {
  263. if (!((pan->need_renorm >> i) & 1))
  264. continue;
  265. t = 0;
  266. for (j = 0; j < pan->nb_input_channels; j++)
  267. t += pan->gain[i][j];
  268. if (t > -1E-5 && t < 1E-5) {
  269. // t is almost 0 but not exactly, this is probably a mistake
  270. if (t)
  271. av_log(ctx, AV_LOG_WARNING,
  272. "Degenerate coefficients while renormalizing\n");
  273. continue;
  274. }
  275. for (j = 0; j < pan->nb_input_channels; j++)
  276. pan->gain[i][j] /= t;
  277. }
  278. av_opt_set_int(pan->swr, "icl", link->channel_layout, 0);
  279. av_opt_set_int(pan->swr, "ocl", pan->out_channel_layout, 0);
  280. swr_set_matrix(pan->swr, pan->gain[0], pan->gain[1] - pan->gain[0]);
  281. }
  282. r = swr_init(pan->swr);
  283. if (r < 0)
  284. return r;
  285. // summary
  286. for (i = 0; i < pan->nb_output_channels; i++) {
  287. cur = buf;
  288. for (j = 0; j < pan->nb_input_channels; j++) {
  289. r = snprintf(cur, buf + sizeof(buf) - cur, "%s%.3g i%d",
  290. j ? " + " : "", pan->gain[i][j], j);
  291. cur += FFMIN(buf + sizeof(buf) - cur, r);
  292. }
  293. av_log(ctx, AV_LOG_INFO, "o%d = %s\n", i, buf);
  294. }
  295. // add channel mapping summary if possible
  296. if (pan->pure_gains) {
  297. av_log(ctx, AV_LOG_INFO, "Pure channel mapping detected:");
  298. for (i = 0; i < pan->nb_output_channels; i++)
  299. if (pan->channel_map[i] < 0)
  300. av_log(ctx, AV_LOG_INFO, " M");
  301. else
  302. av_log(ctx, AV_LOG_INFO, " %d", pan->channel_map[i]);
  303. av_log(ctx, AV_LOG_INFO, "\n");
  304. return 0;
  305. }
  306. return 0;
  307. }
  308. static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  309. {
  310. int n = insamples->audio->nb_samples;
  311. AVFilterLink *const outlink = inlink->dst->outputs[0];
  312. AVFilterBufferRef *outsamples = avfilter_get_audio_buffer(outlink, AV_PERM_WRITE, n);
  313. PanContext *pan = inlink->dst->priv;
  314. swr_convert(pan->swr, outsamples->data, n, (void *)insamples->data, n);
  315. avfilter_copy_buffer_ref_props(outsamples, insamples);
  316. outsamples->audio->channel_layout = outlink->channel_layout;
  317. outsamples->audio->planar = outlink->planar;
  318. avfilter_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. };