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.

545 lines
17KB

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