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.

313 lines
10KB

  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/audioconvert.h"
  30. #include "libavutil/avstring.h"
  31. #include "avfilter.h"
  32. #include "internal.h"
  33. #define MAX_CHANNELS 63
  34. typedef struct {
  35. int64_t out_channel_layout;
  36. union {
  37. double d[MAX_CHANNELS][MAX_CHANNELS];
  38. // i is 1:7:8 fixed-point, i.e. in [-128*256; +128*256[
  39. int i[MAX_CHANNELS][MAX_CHANNELS];
  40. } gain;
  41. int64_t need_renorm;
  42. int need_renumber;
  43. int nb_input_channels;
  44. int nb_output_channels;
  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;
  50. int64_t layout, layout0;
  51. if (sscanf(*arg, " %7[A-Z] %n", buf, &len)) {
  52. layout0 = layout = av_get_channel_layout(buf);
  53. for (i = 32; i > 0; i >>= 1) {
  54. if (layout >= (int64_t)1 << i) {
  55. channel_id += i;
  56. layout >>= i;
  57. }
  58. }
  59. if (channel_id >= MAX_CHANNELS || layout0 != (int64_t)1 << channel_id)
  60. return AVERROR(EINVAL);
  61. *rchannel = channel_id;
  62. *rnamed = 1;
  63. *arg += len;
  64. return 0;
  65. }
  66. if (sscanf(*arg, " c%d %n", &channel_id, &len) &&
  67. channel_id >= 0 && channel_id < MAX_CHANNELS) {
  68. *rchannel = channel_id;
  69. *rnamed = 0;
  70. *arg += len;
  71. return 0;
  72. }
  73. return AVERROR(EINVAL);
  74. }
  75. static void skip_spaces(char **arg)
  76. {
  77. int len = 0;
  78. sscanf(*arg, " %n", &len);
  79. *arg += len;
  80. }
  81. static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
  82. {
  83. PanContext *const pan = ctx->priv;
  84. char *arg, *arg0, *tokenizer, *args = av_strdup(args0);
  85. int out_ch_id, in_ch_id, len, named;
  86. int nb_in_channels[2] = { 0, 0 }; // number of unnamed and named input channels
  87. double gain;
  88. if (!args0) {
  89. av_log(ctx, AV_LOG_ERROR,
  90. "pan filter needs a channel layout and a set "
  91. "of channels definitions as parameter\n");
  92. return AVERROR(EINVAL);
  93. }
  94. if (!args)
  95. return AVERROR(ENOMEM);
  96. arg = av_strtok(args, ":", &tokenizer);
  97. pan->out_channel_layout = av_get_channel_layout(arg);
  98. if (!pan->out_channel_layout) {
  99. av_log(ctx, AV_LOG_ERROR, "Unknown channel layout \"%s\"\n", arg);
  100. return AVERROR(EINVAL);
  101. }
  102. pan->nb_output_channels = av_get_channel_layout_nb_channels(pan->out_channel_layout);
  103. /* parse channel specifications */
  104. while ((arg = arg0 = av_strtok(NULL, ":", &tokenizer))) {
  105. /* channel name */
  106. if (parse_channel_name(&arg, &out_ch_id, &named)) {
  107. av_log(ctx, AV_LOG_ERROR,
  108. "Expected out channel name, got \"%.8s\"\n", arg);
  109. return AVERROR(EINVAL);
  110. }
  111. if (named) {
  112. if (!((pan->out_channel_layout >> out_ch_id) & 1)) {
  113. av_log(ctx, AV_LOG_ERROR,
  114. "Channel \"%.8s\" does not exist in the chosen layout\n", arg0);
  115. return AVERROR(EINVAL);
  116. }
  117. /* get the channel number in the output channel layout:
  118. * out_channel_layout & ((1 << out_ch_id) - 1) are all the
  119. * channels that come before out_ch_id,
  120. * so their count is the index of out_ch_id */
  121. out_ch_id = av_get_channel_layout_nb_channels(pan->out_channel_layout & (((int64_t)1 << out_ch_id) - 1));
  122. }
  123. if (out_ch_id < 0 || out_ch_id >= pan->nb_output_channels) {
  124. av_log(ctx, AV_LOG_ERROR,
  125. "Invalid out channel name \"%.8s\"\n", arg0);
  126. return AVERROR(EINVAL);
  127. }
  128. if (*arg == '=') {
  129. arg++;
  130. } else if (*arg == '<') {
  131. pan->need_renorm |= (int64_t)1 << out_ch_id;
  132. arg++;
  133. } else {
  134. av_log(ctx, AV_LOG_ERROR,
  135. "Syntax error after channel name in \"%.8s\"\n", arg0);
  136. return AVERROR(EINVAL);
  137. }
  138. /* gains */
  139. while (1) {
  140. gain = 1;
  141. if (sscanf(arg, " %lf %n* %n", &gain, &len, &len))
  142. arg += len;
  143. if (parse_channel_name(&arg, &in_ch_id, &named)){
  144. av_log(ctx, AV_LOG_ERROR,
  145. "Expected in channel name, got \"%.8s\"\n", arg);
  146. return AVERROR(EINVAL);
  147. }
  148. nb_in_channels[named]++;
  149. if (nb_in_channels[!named]) {
  150. av_log(ctx, AV_LOG_ERROR,
  151. "Can not mix named and numbered channels\n");
  152. return AVERROR(EINVAL);
  153. }
  154. pan->gain.d[out_ch_id][in_ch_id] = gain;
  155. if (!*arg)
  156. break;
  157. if (*arg != '+') {
  158. av_log(ctx, AV_LOG_ERROR, "Syntax error near \"%.8s\"\n", arg);
  159. return AVERROR(EINVAL);
  160. }
  161. arg++;
  162. skip_spaces(&arg);
  163. }
  164. }
  165. pan->need_renumber = !!nb_in_channels[1];
  166. av_free(args);
  167. return 0;
  168. }
  169. static int query_formats(AVFilterContext *ctx)
  170. {
  171. PanContext *pan = ctx->priv;
  172. AVFilterLink *inlink = ctx->inputs[0];
  173. AVFilterLink *outlink = ctx->outputs[0];
  174. AVFilterFormats *formats;
  175. const enum AVSampleFormat sample_fmts[] = {AV_SAMPLE_FMT_S16, -1};
  176. const int packing_fmts[] = {AVFILTER_PACKED, -1};
  177. avfilter_set_common_sample_formats (ctx, avfilter_make_format_list(sample_fmts));
  178. avfilter_set_common_packing_formats(ctx, avfilter_make_format_list(packing_fmts));
  179. // inlink supports any channel layout
  180. formats = avfilter_make_all_channel_layouts();
  181. avfilter_formats_ref(formats, &inlink->out_chlayouts);
  182. // outlink supports only requested output channel layout
  183. formats = NULL;
  184. avfilter_add_format(&formats, pan->out_channel_layout);
  185. avfilter_formats_ref(formats, &outlink->in_chlayouts);
  186. return 0;
  187. }
  188. static int config_props(AVFilterLink *link)
  189. {
  190. AVFilterContext *ctx = link->dst;
  191. PanContext *pan = ctx->priv;
  192. char buf[1024], *cur;
  193. int i, j, k, r;
  194. double t;
  195. pan->nb_input_channels = av_get_channel_layout_nb_channels(link->channel_layout);
  196. if (pan->need_renumber) {
  197. // input channels were given by their name: renumber them
  198. for (i = j = 0; i < MAX_CHANNELS; i++) {
  199. if ((link->channel_layout >> i) & 1) {
  200. for (k = 0; k < pan->nb_output_channels; k++)
  201. pan->gain.d[k][j] = pan->gain.d[k][i];
  202. j++;
  203. }
  204. }
  205. }
  206. // renormalize
  207. for (i = 0; i < pan->nb_output_channels; i++) {
  208. if (!((pan->need_renorm >> i) & 1))
  209. continue;
  210. t = 0;
  211. for (j = 0; j < pan->nb_input_channels; j++)
  212. t += pan->gain.d[i][j];
  213. if (t > -1E-5 && t < 1E-5) {
  214. // t is almost 0 but not exactly, this is probably a mistake
  215. if (t)
  216. av_log(ctx, AV_LOG_WARNING,
  217. "Degenerate coefficients while renormalizing\n");
  218. continue;
  219. }
  220. for (j = 0; j < pan->nb_input_channels; j++)
  221. pan->gain.d[i][j] /= t;
  222. }
  223. // summary
  224. for (i = 0; i < pan->nb_output_channels; i++) {
  225. cur = buf;
  226. for (j = 0; j < pan->nb_input_channels; j++) {
  227. r = snprintf(cur, buf + sizeof(buf) - cur, "%s%.3g i%d",
  228. j ? " + " : "", pan->gain.d[i][j], j);
  229. cur += FFMIN(buf + sizeof(buf) - cur, r);
  230. }
  231. av_log(ctx, AV_LOG_INFO, "o%d = %s\n", i, buf);
  232. }
  233. // convert to integer
  234. for (i = 0; i < pan->nb_output_channels; i++) {
  235. for (j = 0; j < pan->nb_input_channels; j++) {
  236. if (pan->gain.d[i][j] < -128 || pan->gain.d[i][j] > 128)
  237. av_log(ctx, AV_LOG_WARNING,
  238. "Gain #%d->#%d too large, clamped\n", j, i);
  239. pan->gain.i[i][j] = av_clipf(pan->gain.d[i][j], -128, 128) * 256.0;
  240. }
  241. }
  242. return 0;
  243. }
  244. static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
  245. {
  246. PanContext *const pan = inlink->dst->priv;
  247. int i, o, n = insamples->audio->nb_samples;
  248. /* input */
  249. const int16_t *in = (int16_t *)insamples->data[0];
  250. const int16_t *in_end = in + n * pan->nb_input_channels;
  251. /* output */
  252. AVFilterLink *const outlink = inlink->dst->outputs[0];
  253. AVFilterBufferRef *outsamples = avfilter_get_audio_buffer(outlink, AV_PERM_WRITE, n);
  254. int16_t *out = (int16_t *)outsamples->data[0];
  255. for (; in < in_end; in += pan->nb_input_channels) {
  256. for (o = 0; o < pan->nb_output_channels; o++) {
  257. int v = 0;
  258. for (i = 0; i < pan->nb_input_channels; i++)
  259. v += pan->gain.i[o][i] * in[i];
  260. *(out++) = v >> 8;
  261. }
  262. }
  263. avfilter_filter_samples(outlink, outsamples);
  264. avfilter_unref_buffer(insamples);
  265. }
  266. AVFilter avfilter_af_pan = {
  267. .name = "pan",
  268. .description = NULL_IF_CONFIG_SMALL("Remix channels with coefficients (panning)."),
  269. .priv_size = sizeof(PanContext),
  270. .init = init,
  271. .query_formats = query_formats,
  272. .inputs = (const AVFilterPad[]) {
  273. { .name = "default",
  274. .type = AVMEDIA_TYPE_AUDIO,
  275. .config_props = config_props,
  276. .filter_samples = filter_samples,
  277. .min_perms = AV_PERM_READ, },
  278. { .name = NULL}
  279. },
  280. .outputs = (const AVFilterPad[]) {
  281. { .name = "default",
  282. .type = AVMEDIA_TYPE_AUDIO, },
  283. { .name = NULL}
  284. },
  285. };