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.

520 lines
16KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * Audio join filter
  21. *
  22. * Join multiple audio inputs as different channels in
  23. * a single output
  24. */
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/common.h"
  28. #include "libavutil/opt.h"
  29. #include "audio.h"
  30. #include "avfilter.h"
  31. #include "formats.h"
  32. #include "internal.h"
  33. typedef struct ChannelMap {
  34. int input; ///< input stream index
  35. int in_channel_idx; ///< index of in_channel in the input stream data
  36. uint64_t in_channel; ///< layout describing the input channel
  37. uint64_t out_channel; ///< layout describing the output channel
  38. } ChannelMap;
  39. typedef struct JoinContext {
  40. const AVClass *class;
  41. int inputs;
  42. char *map;
  43. char *channel_layout_str;
  44. uint64_t channel_layout;
  45. int nb_channels;
  46. ChannelMap *channels;
  47. /**
  48. * Temporary storage for input frames, until we get one on each input.
  49. */
  50. AVFrame **input_frames;
  51. /**
  52. * Temporary storage for buffer references, for assembling the output frame.
  53. */
  54. AVBufferRef **buffers;
  55. } JoinContext;
  56. #define OFFSET(x) offsetof(JoinContext, x)
  57. #define A AV_OPT_FLAG_AUDIO_PARAM
  58. #define F AV_OPT_FLAG_FILTERING_PARAM
  59. static const AVOption join_options[] = {
  60. { "inputs", "Number of input streams.", OFFSET(inputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, A|F },
  61. { "channel_layout", "Channel layout of the "
  62. "output stream.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, {.str = "stereo"}, 0, 0, A|F },
  63. { "map", "A comma-separated list of channels maps in the format "
  64. "'input_stream.input_channel-output_channel.",
  65. OFFSET(map), AV_OPT_TYPE_STRING, .flags = A|F },
  66. { NULL }
  67. };
  68. AVFILTER_DEFINE_CLASS(join);
  69. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  70. {
  71. AVFilterContext *ctx = link->dst;
  72. JoinContext *s = ctx->priv;
  73. int i;
  74. for (i = 0; i < ctx->nb_inputs; i++)
  75. if (link == ctx->inputs[i])
  76. break;
  77. av_assert0(i < ctx->nb_inputs);
  78. av_assert0(!s->input_frames[i]);
  79. s->input_frames[i] = frame;
  80. return 0;
  81. }
  82. static int parse_maps(AVFilterContext *ctx)
  83. {
  84. JoinContext *s = ctx->priv;
  85. char separator = '|';
  86. char *cur = s->map;
  87. #if FF_API_OLD_FILTER_OPTS
  88. if (cur && strchr(cur, ',')) {
  89. av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use '|' to "
  90. "separate the mappings.\n");
  91. separator = ',';
  92. }
  93. #endif
  94. while (cur && *cur) {
  95. char *sep, *next, *p;
  96. uint64_t in_channel = 0, out_channel = 0;
  97. int input_idx, out_ch_idx, in_ch_idx;
  98. next = strchr(cur, separator);
  99. if (next)
  100. *next++ = 0;
  101. /* split the map into input and output parts */
  102. if (!(sep = strchr(cur, '-'))) {
  103. av_log(ctx, AV_LOG_ERROR, "Missing separator '-' in channel "
  104. "map '%s'\n", cur);
  105. return AVERROR(EINVAL);
  106. }
  107. *sep++ = 0;
  108. #define PARSE_CHANNEL(str, var, inout) \
  109. if (!(var = av_get_channel_layout(str))) { \
  110. av_log(ctx, AV_LOG_ERROR, "Invalid " inout " channel: %s.\n", str);\
  111. return AVERROR(EINVAL); \
  112. } \
  113. if (av_get_channel_layout_nb_channels(var) != 1) { \
  114. av_log(ctx, AV_LOG_ERROR, "Channel map describes more than one " \
  115. inout " channel.\n"); \
  116. return AVERROR(EINVAL); \
  117. }
  118. /* parse output channel */
  119. PARSE_CHANNEL(sep, out_channel, "output");
  120. if (!(out_channel & s->channel_layout)) {
  121. av_log(ctx, AV_LOG_ERROR, "Output channel '%s' is not present in "
  122. "requested channel layout.\n", sep);
  123. return AVERROR(EINVAL);
  124. }
  125. out_ch_idx = av_get_channel_layout_channel_index(s->channel_layout,
  126. out_channel);
  127. if (s->channels[out_ch_idx].input >= 0) {
  128. av_log(ctx, AV_LOG_ERROR, "Multiple maps for output channel "
  129. "'%s'.\n", sep);
  130. return AVERROR(EINVAL);
  131. }
  132. /* parse input channel */
  133. input_idx = strtol(cur, &cur, 0);
  134. if (input_idx < 0 || input_idx >= s->inputs) {
  135. av_log(ctx, AV_LOG_ERROR, "Invalid input stream index: %d.\n",
  136. input_idx);
  137. return AVERROR(EINVAL);
  138. }
  139. if (*cur)
  140. cur++;
  141. in_ch_idx = strtol(cur, &p, 0);
  142. if (p == cur) {
  143. /* channel specifier is not a number,
  144. * try to parse as channel name */
  145. PARSE_CHANNEL(cur, in_channel, "input");
  146. }
  147. s->channels[out_ch_idx].input = input_idx;
  148. if (in_channel)
  149. s->channels[out_ch_idx].in_channel = in_channel;
  150. else
  151. s->channels[out_ch_idx].in_channel_idx = in_ch_idx;
  152. cur = next;
  153. }
  154. return 0;
  155. }
  156. static av_cold int join_init(AVFilterContext *ctx)
  157. {
  158. JoinContext *s = ctx->priv;
  159. int ret, i;
  160. if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
  161. av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
  162. s->channel_layout_str);
  163. return AVERROR(EINVAL);
  164. }
  165. s->nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
  166. s->channels = av_mallocz(sizeof(*s->channels) * s->nb_channels);
  167. s->buffers = av_mallocz(sizeof(*s->buffers) * s->nb_channels);
  168. s->input_frames = av_mallocz(sizeof(*s->input_frames) * s->inputs);
  169. if (!s->channels || !s->buffers|| !s->input_frames)
  170. return AVERROR(ENOMEM);
  171. for (i = 0; i < s->nb_channels; i++) {
  172. s->channels[i].out_channel = av_channel_layout_extract_channel(s->channel_layout, i);
  173. s->channels[i].input = -1;
  174. }
  175. if ((ret = parse_maps(ctx)) < 0)
  176. return ret;
  177. for (i = 0; i < s->inputs; i++) {
  178. char name[32];
  179. AVFilterPad pad = { 0 };
  180. snprintf(name, sizeof(name), "input%d", i);
  181. pad.type = AVMEDIA_TYPE_AUDIO;
  182. pad.name = av_strdup(name);
  183. pad.filter_frame = filter_frame;
  184. pad.needs_fifo = 1;
  185. ff_insert_inpad(ctx, i, &pad);
  186. }
  187. return 0;
  188. }
  189. static av_cold void join_uninit(AVFilterContext *ctx)
  190. {
  191. JoinContext *s = ctx->priv;
  192. int i;
  193. for (i = 0; i < ctx->nb_inputs; i++) {
  194. av_freep(&ctx->input_pads[i].name);
  195. av_frame_free(&s->input_frames[i]);
  196. }
  197. av_freep(&s->channels);
  198. av_freep(&s->buffers);
  199. av_freep(&s->input_frames);
  200. }
  201. static int join_query_formats(AVFilterContext *ctx)
  202. {
  203. JoinContext *s = ctx->priv;
  204. AVFilterChannelLayouts *layouts = NULL;
  205. int i;
  206. ff_add_channel_layout(&layouts, s->channel_layout);
  207. ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
  208. for (i = 0; i < ctx->nb_inputs; i++) {
  209. layouts = ff_all_channel_layouts();
  210. if (!layouts)
  211. return AVERROR(ENOMEM);
  212. ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts);
  213. }
  214. ff_set_common_formats (ctx, ff_planar_sample_fmts());
  215. ff_set_common_samplerates(ctx, ff_all_samplerates());
  216. return 0;
  217. }
  218. static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch,
  219. uint64_t *inputs)
  220. {
  221. int i;
  222. for (i = 0; i < ctx->nb_inputs; i++) {
  223. AVFilterLink *link = ctx->inputs[i];
  224. if (ch->out_channel & link->channel_layout &&
  225. !(ch->out_channel & inputs[i])) {
  226. ch->input = i;
  227. ch->in_channel = ch->out_channel;
  228. inputs[i] |= ch->out_channel;
  229. return;
  230. }
  231. }
  232. }
  233. static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch,
  234. uint64_t *inputs)
  235. {
  236. int i;
  237. for (i = 0; i < ctx->nb_inputs; i++) {
  238. AVFilterLink *link = ctx->inputs[i];
  239. if ((inputs[i] & link->channel_layout) != link->channel_layout) {
  240. uint64_t unused = link->channel_layout & ~inputs[i];
  241. ch->input = i;
  242. ch->in_channel = av_channel_layout_extract_channel(unused, 0);
  243. inputs[i] |= ch->in_channel;
  244. return;
  245. }
  246. }
  247. }
  248. static int join_config_output(AVFilterLink *outlink)
  249. {
  250. AVFilterContext *ctx = outlink->src;
  251. JoinContext *s = ctx->priv;
  252. uint64_t *inputs; // nth element tracks which channels are used from nth input
  253. int i, ret = 0;
  254. /* initialize inputs to user-specified mappings */
  255. if (!(inputs = av_mallocz(sizeof(*inputs) * ctx->nb_inputs)))
  256. return AVERROR(ENOMEM);
  257. for (i = 0; i < s->nb_channels; i++) {
  258. ChannelMap *ch = &s->channels[i];
  259. AVFilterLink *inlink;
  260. if (ch->input < 0)
  261. continue;
  262. inlink = ctx->inputs[ch->input];
  263. if (!ch->in_channel)
  264. ch->in_channel = av_channel_layout_extract_channel(inlink->channel_layout,
  265. ch->in_channel_idx);
  266. if (!(ch->in_channel & inlink->channel_layout)) {
  267. av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in "
  268. "input stream #%d.\n", av_get_channel_name(ch->in_channel),
  269. ch->input);
  270. ret = AVERROR(EINVAL);
  271. goto fail;
  272. }
  273. inputs[ch->input] |= ch->in_channel;
  274. }
  275. /* guess channel maps when not explicitly defined */
  276. /* first try unused matching channels */
  277. for (i = 0; i < s->nb_channels; i++) {
  278. ChannelMap *ch = &s->channels[i];
  279. if (ch->input < 0)
  280. guess_map_matching(ctx, ch, inputs);
  281. }
  282. /* if the above failed, try to find _any_ unused input channel */
  283. for (i = 0; i < s->nb_channels; i++) {
  284. ChannelMap *ch = &s->channels[i];
  285. if (ch->input < 0)
  286. guess_map_any(ctx, ch, inputs);
  287. if (ch->input < 0) {
  288. av_log(ctx, AV_LOG_ERROR, "Could not find input channel for "
  289. "output channel '%s'.\n",
  290. av_get_channel_name(ch->out_channel));
  291. goto fail;
  292. }
  293. ch->in_channel_idx = av_get_channel_layout_channel_index(ctx->inputs[ch->input]->channel_layout,
  294. ch->in_channel);
  295. }
  296. /* print mappings */
  297. av_log(ctx, AV_LOG_VERBOSE, "mappings: ");
  298. for (i = 0; i < s->nb_channels; i++) {
  299. ChannelMap *ch = &s->channels[i];
  300. av_log(ctx, AV_LOG_VERBOSE, "%d.%s => %s ", ch->input,
  301. av_get_channel_name(ch->in_channel),
  302. av_get_channel_name(ch->out_channel));
  303. }
  304. av_log(ctx, AV_LOG_VERBOSE, "\n");
  305. for (i = 0; i < ctx->nb_inputs; i++) {
  306. if (!inputs[i])
  307. av_log(ctx, AV_LOG_WARNING, "No channels are used from input "
  308. "stream %d.\n", i);
  309. }
  310. fail:
  311. av_freep(&inputs);
  312. return ret;
  313. }
  314. static int join_request_frame(AVFilterLink *outlink)
  315. {
  316. AVFilterContext *ctx = outlink->src;
  317. JoinContext *s = ctx->priv;
  318. AVFrame *frame;
  319. int linesize = INT_MAX;
  320. int nb_samples = 0;
  321. int nb_buffers = 0;
  322. int i, j, ret;
  323. /* get a frame on each input */
  324. for (i = 0; i < ctx->nb_inputs; i++) {
  325. AVFilterLink *inlink = ctx->inputs[i];
  326. if (!s->input_frames[i] &&
  327. (ret = ff_request_frame(inlink)) < 0)
  328. return ret;
  329. /* request the same number of samples on all inputs */
  330. if (i == 0) {
  331. nb_samples = s->input_frames[0]->nb_samples;
  332. for (j = 1; !i && j < ctx->nb_inputs; j++)
  333. ctx->inputs[j]->request_samples = nb_samples;
  334. }
  335. }
  336. /* setup the output frame */
  337. frame = av_frame_alloc();
  338. if (!frame)
  339. return AVERROR(ENOMEM);
  340. if (s->nb_channels > FF_ARRAY_ELEMS(frame->data)) {
  341. frame->extended_data = av_mallocz(s->nb_channels *
  342. sizeof(*frame->extended_data));
  343. if (!frame->extended_data) {
  344. ret = AVERROR(ENOMEM);
  345. goto fail;
  346. }
  347. }
  348. /* copy the data pointers */
  349. for (i = 0; i < s->nb_channels; i++) {
  350. ChannelMap *ch = &s->channels[i];
  351. AVFrame *cur = s->input_frames[ch->input];
  352. AVBufferRef *buf;
  353. frame->extended_data[i] = cur->extended_data[ch->in_channel_idx];
  354. linesize = FFMIN(linesize, cur->linesize[0]);
  355. /* add the buffer where this plan is stored to the list if it's
  356. * not already there */
  357. buf = av_frame_get_plane_buffer(cur, ch->in_channel_idx);
  358. if (!buf) {
  359. ret = AVERROR(EINVAL);
  360. goto fail;
  361. }
  362. for (j = 0; j < nb_buffers; j++)
  363. if (s->buffers[j]->buffer == buf->buffer)
  364. break;
  365. if (j == i)
  366. s->buffers[nb_buffers++] = buf;
  367. }
  368. /* create references to the buffers we copied to output */
  369. if (nb_buffers > FF_ARRAY_ELEMS(frame->buf)) {
  370. frame->nb_extended_buf = nb_buffers - FF_ARRAY_ELEMS(frame->buf);
  371. frame->extended_buf = av_mallocz(sizeof(*frame->extended_buf) *
  372. frame->nb_extended_buf);
  373. if (!frame->extended_buf) {
  374. frame->nb_extended_buf = 0;
  375. ret = AVERROR(ENOMEM);
  376. goto fail;
  377. }
  378. }
  379. for (i = 0; i < FFMIN(FF_ARRAY_ELEMS(frame->buf), nb_buffers); i++) {
  380. frame->buf[i] = av_buffer_ref(s->buffers[i]);
  381. if (!frame->buf[i]) {
  382. ret = AVERROR(ENOMEM);
  383. goto fail;
  384. }
  385. }
  386. for (i = 0; i < frame->nb_extended_buf; i++) {
  387. frame->extended_buf[i] = av_buffer_ref(s->buffers[i +
  388. FF_ARRAY_ELEMS(frame->buf)]);
  389. if (!frame->extended_buf[i]) {
  390. ret = AVERROR(ENOMEM);
  391. goto fail;
  392. }
  393. }
  394. frame->nb_samples = nb_samples;
  395. frame->channel_layout = outlink->channel_layout;
  396. av_frame_set_channels(frame, outlink->channels);
  397. frame->sample_rate = outlink->sample_rate;
  398. frame->format = outlink->format;
  399. frame->pts = s->input_frames[0]->pts;
  400. frame->linesize[0] = linesize;
  401. if (frame->data != frame->extended_data) {
  402. memcpy(frame->data, frame->extended_data, sizeof(*frame->data) *
  403. FFMIN(FF_ARRAY_ELEMS(frame->data), s->nb_channels));
  404. }
  405. ret = ff_filter_frame(outlink, frame);
  406. for (i = 0; i < ctx->nb_inputs; i++)
  407. av_frame_free(&s->input_frames[i]);
  408. return ret;
  409. fail:
  410. av_frame_free(&frame);
  411. return ret;
  412. }
  413. static const AVFilterPad avfilter_af_join_outputs[] = {
  414. {
  415. .name = "default",
  416. .type = AVMEDIA_TYPE_AUDIO,
  417. .config_props = join_config_output,
  418. .request_frame = join_request_frame,
  419. },
  420. { NULL }
  421. };
  422. AVFilter ff_af_join = {
  423. .name = "join",
  424. .description = NULL_IF_CONFIG_SMALL("Join multiple audio streams into "
  425. "multi-channel output."),
  426. .priv_size = sizeof(JoinContext),
  427. .priv_class = &join_class,
  428. .init = join_init,
  429. .uninit = join_uninit,
  430. .query_formats = join_query_formats,
  431. .inputs = NULL,
  432. .outputs = avfilter_af_join_outputs,
  433. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  434. };