af_pan.c 10 KB

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