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.

542 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 < ctx->nb_inputs; i++) {
  177. av_freep(&ctx->input_pads[i].name);
  178. av_frame_free(&s->input_frames[i]);
  179. }
  180. av_freep(&s->channels);
  181. av_freep(&s->buffers);
  182. av_freep(&s->input_frames);
  183. }
  184. static int join_query_formats(AVFilterContext *ctx)
  185. {
  186. JoinContext *s = ctx->priv;
  187. AVFilterChannelLayouts *layouts = NULL;
  188. int i, ret;
  189. if ((ret = ff_add_channel_layout(&layouts, s->channel_layout)) < 0 ||
  190. (ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
  191. return ret;
  192. for (i = 0; i < ctx->nb_inputs; i++) {
  193. layouts = ff_all_channel_layouts();
  194. if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
  195. return ret;
  196. }
  197. if ((ret = ff_set_common_formats(ctx, ff_planar_sample_fmts())) < 0 ||
  198. (ret = ff_set_common_samplerates(ctx, ff_all_samplerates())) < 0)
  199. return ret;
  200. return 0;
  201. }
  202. static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch,
  203. uint64_t *inputs)
  204. {
  205. int i;
  206. for (i = 0; i < ctx->nb_inputs; i++) {
  207. AVFilterLink *link = ctx->inputs[i];
  208. if (ch->out_channel & link->channel_layout &&
  209. !(ch->out_channel & inputs[i])) {
  210. ch->input = i;
  211. ch->in_channel = ch->out_channel;
  212. inputs[i] |= ch->out_channel;
  213. return;
  214. }
  215. }
  216. }
  217. static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch,
  218. uint64_t *inputs)
  219. {
  220. int i;
  221. for (i = 0; i < ctx->nb_inputs; i++) {
  222. AVFilterLink *link = ctx->inputs[i];
  223. if ((inputs[i] & link->channel_layout) != link->channel_layout) {
  224. uint64_t unused = link->channel_layout & ~inputs[i];
  225. ch->input = i;
  226. ch->in_channel = av_channel_layout_extract_channel(unused, 0);
  227. inputs[i] |= ch->in_channel;
  228. return;
  229. }
  230. }
  231. }
  232. static int join_config_output(AVFilterLink *outlink)
  233. {
  234. AVFilterContext *ctx = outlink->src;
  235. JoinContext *s = ctx->priv;
  236. uint64_t *inputs; // nth element tracks which channels are used from nth input
  237. int i, ret = 0;
  238. /* initialize inputs to user-specified mappings */
  239. if (!(inputs = av_mallocz_array(ctx->nb_inputs, sizeof(*inputs))))
  240. return AVERROR(ENOMEM);
  241. for (i = 0; i < s->nb_channels; i++) {
  242. ChannelMap *ch = &s->channels[i];
  243. AVFilterLink *inlink;
  244. if (ch->input < 0)
  245. continue;
  246. inlink = ctx->inputs[ch->input];
  247. if (!ch->in_channel)
  248. ch->in_channel = av_channel_layout_extract_channel(inlink->channel_layout,
  249. ch->in_channel_idx);
  250. if (!(ch->in_channel & inlink->channel_layout)) {
  251. av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in "
  252. "input stream #%d.\n", av_get_channel_name(ch->in_channel),
  253. ch->input);
  254. ret = AVERROR(EINVAL);
  255. goto fail;
  256. }
  257. inputs[ch->input] |= ch->in_channel;
  258. }
  259. /* guess channel maps when not explicitly defined */
  260. /* first try unused matching channels */
  261. for (i = 0; i < s->nb_channels; i++) {
  262. ChannelMap *ch = &s->channels[i];
  263. if (ch->input < 0)
  264. guess_map_matching(ctx, ch, inputs);
  265. }
  266. /* if the above failed, try to find _any_ unused input channel */
  267. for (i = 0; i < s->nb_channels; i++) {
  268. ChannelMap *ch = &s->channels[i];
  269. if (ch->input < 0)
  270. guess_map_any(ctx, ch, inputs);
  271. if (ch->input < 0) {
  272. av_log(ctx, AV_LOG_ERROR, "Could not find input channel for "
  273. "output channel '%s'.\n",
  274. av_get_channel_name(ch->out_channel));
  275. goto fail;
  276. }
  277. ch->in_channel_idx = av_get_channel_layout_channel_index(ctx->inputs[ch->input]->channel_layout,
  278. ch->in_channel);
  279. }
  280. /* print mappings */
  281. av_log(ctx, AV_LOG_VERBOSE, "mappings: ");
  282. for (i = 0; i < s->nb_channels; i++) {
  283. ChannelMap *ch = &s->channels[i];
  284. av_log(ctx, AV_LOG_VERBOSE, "%d.%s => %s ", ch->input,
  285. av_get_channel_name(ch->in_channel),
  286. av_get_channel_name(ch->out_channel));
  287. }
  288. av_log(ctx, AV_LOG_VERBOSE, "\n");
  289. for (i = 0; i < ctx->nb_inputs; i++) {
  290. if (!inputs[i])
  291. av_log(ctx, AV_LOG_WARNING, "No channels are used from input "
  292. "stream %d.\n", i);
  293. }
  294. fail:
  295. av_freep(&inputs);
  296. return ret;
  297. }
  298. static int try_push_frame(AVFilterContext *ctx)
  299. {
  300. AVFilterLink *outlink = ctx->outputs[0];
  301. JoinContext *s = ctx->priv;
  302. AVFrame *frame;
  303. int linesize = INT_MAX;
  304. int nb_samples = INT_MAX;
  305. int nb_buffers = 0;
  306. int i, j, ret;
  307. for (i = 0; i < ctx->nb_inputs; i++) {
  308. if (!s->input_frames[i])
  309. return 0;
  310. nb_samples = FFMIN(nb_samples, s->input_frames[i]->nb_samples);
  311. }
  312. if (!nb_samples)
  313. return 0;
  314. /* setup the output frame */
  315. frame = av_frame_alloc();
  316. if (!frame)
  317. return AVERROR(ENOMEM);
  318. if (s->nb_channels > FF_ARRAY_ELEMS(frame->data)) {
  319. frame->extended_data = av_mallocz_array(s->nb_channels,
  320. sizeof(*frame->extended_data));
  321. if (!frame->extended_data) {
  322. ret = AVERROR(ENOMEM);
  323. goto fail;
  324. }
  325. }
  326. /* copy the data pointers */
  327. for (i = 0; i < s->nb_channels; i++) {
  328. ChannelMap *ch = &s->channels[i];
  329. AVFrame *cur = s->input_frames[ch->input];
  330. AVBufferRef *buf;
  331. frame->extended_data[i] = cur->extended_data[ch->in_channel_idx];
  332. linesize = FFMIN(linesize, cur->linesize[0]);
  333. /* add the buffer where this plan is stored to the list if it's
  334. * not already there */
  335. buf = av_frame_get_plane_buffer(cur, ch->in_channel_idx);
  336. if (!buf) {
  337. ret = AVERROR(EINVAL);
  338. goto fail;
  339. }
  340. for (j = 0; j < nb_buffers; j++)
  341. if (s->buffers[j]->buffer == buf->buffer)
  342. break;
  343. if (j == i)
  344. s->buffers[nb_buffers++] = buf;
  345. }
  346. /* create references to the buffers we copied to output */
  347. if (nb_buffers > FF_ARRAY_ELEMS(frame->buf)) {
  348. frame->nb_extended_buf = nb_buffers - FF_ARRAY_ELEMS(frame->buf);
  349. frame->extended_buf = av_mallocz_array(frame->nb_extended_buf,
  350. sizeof(*frame->extended_buf));
  351. if (!frame->extended_buf) {
  352. frame->nb_extended_buf = 0;
  353. ret = AVERROR(ENOMEM);
  354. goto fail;
  355. }
  356. }
  357. for (i = 0; i < FFMIN(FF_ARRAY_ELEMS(frame->buf), nb_buffers); i++) {
  358. frame->buf[i] = av_buffer_ref(s->buffers[i]);
  359. if (!frame->buf[i]) {
  360. ret = AVERROR(ENOMEM);
  361. goto fail;
  362. }
  363. }
  364. for (i = 0; i < frame->nb_extended_buf; i++) {
  365. frame->extended_buf[i] = av_buffer_ref(s->buffers[i +
  366. FF_ARRAY_ELEMS(frame->buf)]);
  367. if (!frame->extended_buf[i]) {
  368. ret = AVERROR(ENOMEM);
  369. goto fail;
  370. }
  371. }
  372. frame->nb_samples = nb_samples;
  373. frame->channel_layout = outlink->channel_layout;
  374. frame->channels = outlink->channels;
  375. frame->sample_rate = outlink->sample_rate;
  376. frame->format = outlink->format;
  377. frame->pts = s->input_frames[0]->pts;
  378. frame->linesize[0] = linesize;
  379. if (frame->data != frame->extended_data) {
  380. memcpy(frame->data, frame->extended_data, sizeof(*frame->data) *
  381. FFMIN(FF_ARRAY_ELEMS(frame->data), s->nb_channels));
  382. }
  383. ret = ff_filter_frame(outlink, frame);
  384. for (i = 0; i < ctx->nb_inputs; i++)
  385. av_frame_free(&s->input_frames[i]);
  386. return ret;
  387. fail:
  388. av_frame_free(&frame);
  389. return ret;
  390. }
  391. static int activate(AVFilterContext *ctx)
  392. {
  393. JoinContext *s = ctx->priv;
  394. int i, ret, status;
  395. int nb_samples = 0;
  396. int64_t pts;
  397. FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
  398. if (!s->input_frames[0]) {
  399. ret = ff_inlink_consume_frame(ctx->inputs[0], &s->input_frames[0]);
  400. if (ret < 0) {
  401. return ret;
  402. } else if (ff_inlink_acknowledge_status(ctx->inputs[0], &status, &pts)) {
  403. ff_outlink_set_status(ctx->outputs[0], status, pts);
  404. return 0;
  405. } else {
  406. if (ff_outlink_frame_wanted(ctx->outputs[0]) && !s->input_frames[0]) {
  407. ff_inlink_request_frame(ctx->inputs[0]);
  408. return 0;
  409. }
  410. }
  411. if (!s->input_frames[0]) {
  412. return 0;
  413. }
  414. }
  415. nb_samples = s->input_frames[0]->nb_samples;
  416. for (i = 1; i < ctx->nb_inputs && nb_samples > 0; i++) {
  417. if (s->input_frames[i])
  418. continue;
  419. if (ff_inlink_check_available_samples(ctx->inputs[i], nb_samples) > 0) {
  420. ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->input_frames[i]);
  421. if (ret < 0) {
  422. return ret;
  423. } else if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
  424. ff_outlink_set_status(ctx->outputs[0], status, pts);
  425. return 0;
  426. }
  427. } else {
  428. if (ff_outlink_frame_wanted(ctx->outputs[0])) {
  429. ff_inlink_request_frame(ctx->inputs[i]);
  430. return 0;
  431. }
  432. }
  433. }
  434. return try_push_frame(ctx);
  435. }
  436. static const AVFilterPad avfilter_af_join_outputs[] = {
  437. {
  438. .name = "default",
  439. .type = AVMEDIA_TYPE_AUDIO,
  440. .config_props = join_config_output,
  441. },
  442. { NULL }
  443. };
  444. AVFilter ff_af_join = {
  445. .name = "join",
  446. .description = NULL_IF_CONFIG_SMALL("Join multiple audio streams into "
  447. "multi-channel output."),
  448. .priv_size = sizeof(JoinContext),
  449. .priv_class = &join_class,
  450. .init = join_init,
  451. .uninit = join_uninit,
  452. .activate = activate,
  453. .query_formats = join_query_formats,
  454. .inputs = NULL,
  455. .outputs = avfilter_af_join_outputs,
  456. .flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
  457. };