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.

384 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. #define MAX_CHANNELS 63
  35. typedef struct PanContext {
  36. int64_t out_channel_layout;
  37. double gain[MAX_CHANNELS][MAX_CHANNELS];
  38. int64_t need_renorm;
  39. int need_renumber;
  40. int nb_input_channels;
  41. int nb_output_channels;
  42. int pure_gains;
  43. /* channel mapping specific */
  44. int channel_map[SWR_CH_MAX];
  45. struct SwrContext *swr;
  46. } PanContext;
  47. static int parse_channel_name(char **arg, int *rchannel, int *rnamed)
  48. {
  49. char buf[8];
  50. int len, i, channel_id = 0;
  51. int64_t layout, layout0;
  52. /* try to parse a channel name, e.g. "FL" */
  53. if (sscanf(*arg, " %7[A-Z] %n", buf, &len)) {
  54. layout0 = layout = av_get_channel_layout(buf);
  55. /* channel_id <- first set bit in layout */
  56. for (i = 32; i > 0; i >>= 1) {
  57. if (layout >= (int64_t)1 << i) {
  58. channel_id += i;
  59. layout >>= i;
  60. }
  61. }
  62. /* reject layouts that are not a single channel */
  63. if (channel_id >= MAX_CHANNELS || layout0 != (int64_t)1 << channel_id)
  64. return AVERROR(EINVAL);
  65. *rchannel = channel_id;
  66. *rnamed = 1;
  67. *arg += len;
  68. return 0;
  69. }
  70. /* try to parse a channel number, e.g. "c2" */
  71. if (sscanf(*arg, " c%d %n", &channel_id, &len) &&
  72. channel_id >= 0 && channel_id < MAX_CHANNELS) {
  73. *rchannel = channel_id;
  74. *rnamed = 0;
  75. *arg += len;
  76. return 0;
  77. }
  78. return AVERROR(EINVAL);
  79. }
  80. static void skip_spaces(char **arg)
  81. {
  82. int len = 0;
  83. sscanf(*arg, " %n", &len);
  84. *arg += len;
  85. }
  86. static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
  87. {
  88. PanContext *const pan = ctx->priv;
  89. char *arg, *arg0, *tokenizer, *args = av_strdup(args0);
  90. int out_ch_id, in_ch_id, len, named;
  91. int nb_in_channels[2] = { 0, 0 }; // number of unnamed and named input channels
  92. double gain;
  93. if (!args0) {
  94. av_log(ctx, AV_LOG_ERROR,
  95. "pan filter needs a channel layout and a set "
  96. "of channels definitions as parameter\n");
  97. return AVERROR(EINVAL);
  98. }
  99. if (!args)
  100. return AVERROR(ENOMEM);
  101. arg = av_strtok(args, ":", &tokenizer);
  102. pan->out_channel_layout = av_get_channel_layout(arg);
  103. if (!pan->out_channel_layout) {
  104. av_log(ctx, AV_LOG_ERROR, "Unknown channel layout \"%s\"\n", arg);
  105. return AVERROR(EINVAL);
  106. }
  107. pan->nb_output_channels = av_get_channel_layout_nb_channels(pan->out_channel_layout);
  108. /* parse channel specifications */
  109. while ((arg = arg0 = av_strtok(NULL, ":", &tokenizer))) {
  110. /* channel name */
  111. if (parse_channel_name(&arg, &out_ch_id, &named)) {
  112. av_log(ctx, AV_LOG_ERROR,
  113. "Expected out channel name, got \"%.8s\"\n", arg);
  114. return AVERROR(EINVAL);
  115. }
  116. if (named) {
  117. if (!((pan->out_channel_layout >> out_ch_id) & 1)) {
  118. av_log(ctx, AV_LOG_ERROR,
  119. "Channel \"%.8s\" does not exist in the chosen layout\n", arg0);
  120. return AVERROR(EINVAL);
  121. }
  122. /* get the channel number in the output channel layout:
  123. * out_channel_layout & ((1 << out_ch_id) - 1) are all the
  124. * channels that come before out_ch_id,
  125. * so their count is the index of out_ch_id */
  126. out_ch_id = av_get_channel_layout_nb_channels(pan->out_channel_layout & (((int64_t)1 << out_ch_id) - 1));
  127. }
  128. if (out_ch_id < 0 || out_ch_id >= pan->nb_output_channels) {
  129. av_log(ctx, AV_LOG_ERROR,
  130. "Invalid out channel name \"%.8s\"\n", arg0);
  131. return AVERROR(EINVAL);
  132. }
  133. if (*arg == '=') {
  134. arg++;
  135. } else if (*arg == '<') {
  136. pan->need_renorm |= (int64_t)1 << out_ch_id;
  137. arg++;
  138. } else {
  139. av_log(ctx, AV_LOG_ERROR,
  140. "Syntax error after channel name in \"%.8s\"\n", arg0);
  141. return AVERROR(EINVAL);
  142. }
  143. /* gains */
  144. while (1) {
  145. gain = 1;
  146. if (sscanf(arg, " %lf %n* %n", &gain, &len, &len))
  147. arg += len;
  148. if (parse_channel_name(&arg, &in_ch_id, &named)){
  149. av_log(ctx, AV_LOG_ERROR,
  150. "Expected in channel name, got \"%.8s\"\n", arg);
  151. return AVERROR(EINVAL);
  152. }
  153. nb_in_channels[named]++;
  154. if (nb_in_channels[!named]) {
  155. av_log(ctx, AV_LOG_ERROR,
  156. "Can not mix named and numbered channels\n");
  157. return AVERROR(EINVAL);
  158. }
  159. pan->gain[out_ch_id][in_ch_id] = gain;
  160. if (!*arg)
  161. break;
  162. if (*arg != '+') {
  163. av_log(ctx, AV_LOG_ERROR, "Syntax error near \"%.8s\"\n", arg);
  164. return AVERROR(EINVAL);
  165. }
  166. arg++;
  167. skip_spaces(&arg);
  168. }
  169. }
  170. pan->need_renumber = !!nb_in_channels[1];
  171. av_free(args);
  172. return 0;
  173. }
  174. static int are_gains_pure(const PanContext *pan)
  175. {
  176. int i, j;
  177. for (i = 0; i < MAX_CHANNELS; i++) {
  178. int nb_gain = 0;
  179. for (j = 0; j < MAX_CHANNELS; j++) {
  180. double gain = pan->gain[i][j];
  181. /* channel mapping is effective only if 0% or 100% of a channel is
  182. * selected... */
  183. if (gain != 0. && gain != 1.)
  184. return 0;
  185. /* ...and if the output channel is only composed of one input */
  186. if (gain && nb_gain++)
  187. return 0;
  188. }
  189. }
  190. return 1;
  191. }
  192. static int query_formats(AVFilterContext *ctx)
  193. {
  194. PanContext *pan = ctx->priv;
  195. AVFilterLink *inlink = ctx->inputs[0];
  196. AVFilterLink *outlink = ctx->outputs[0];
  197. AVFilterFormats *formats;
  198. pan->pure_gains = are_gains_pure(pan);
  199. /* libswr supports any sample and packing formats */
  200. avfilter_set_common_sample_formats(ctx, avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO));
  201. avfilter_set_common_packing_formats(ctx, avfilter_make_all_packing_formats());
  202. // inlink supports any channel layout
  203. formats = avfilter_make_all_channel_layouts();
  204. avfilter_formats_ref(formats, &inlink->out_chlayouts);
  205. // outlink supports only requested output channel layout
  206. formats = NULL;
  207. avfilter_add_format(&formats, pan->out_channel_layout);
  208. avfilter_formats_ref(formats, &outlink->in_chlayouts);
  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. outsamples->audio->planar = outlink->planar;
  319. ff_filter_samples(outlink, outsamples);
  320. avfilter_unref_buffer(insamples);
  321. }
  322. static av_cold void uninit(AVFilterContext *ctx)
  323. {
  324. PanContext *pan = ctx->priv;
  325. swr_free(&pan->swr);
  326. }
  327. AVFilter avfilter_af_pan = {
  328. .name = "pan",
  329. .description = NULL_IF_CONFIG_SMALL("Remix channels with coefficients (panning)."),
  330. .priv_size = sizeof(PanContext),
  331. .init = init,
  332. .uninit = uninit,
  333. .query_formats = query_formats,
  334. .inputs = (const AVFilterPad[]) {
  335. { .name = "default",
  336. .type = AVMEDIA_TYPE_AUDIO,
  337. .config_props = config_props,
  338. .filter_samples = filter_samples,
  339. .min_perms = AV_PERM_READ, },
  340. { .name = NULL}
  341. },
  342. .outputs = (const AVFilterPad[]) {
  343. { .name = "default",
  344. .type = AVMEDIA_TYPE_AUDIO, },
  345. { .name = NULL}
  346. },
  347. };