2012-05-28 08:28:58 +03:00
|
|
|
/*
|
2013-08-16 00:12:51 +03:00
|
|
|
* This file is part of FFmpeg.
|
2012-05-28 08:28:58 +03:00
|
|
|
*
|
2013-08-16 00:12:51 +03:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2012-05-28 08:28:58 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2013-08-16 00:12:51 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2012-05-28 08:28:58 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2013-08-16 00:12:51 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2012-05-28 08:28:58 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Audio join filter
|
|
|
|
*
|
|
|
|
* Join multiple audio inputs as different channels in
|
|
|
|
* a single output
|
|
|
|
*/
|
|
|
|
|
2021-08-31 16:03:14 +02:00
|
|
|
#include "libavutil/avassert.h"
|
2019-09-30 16:45:31 +02:00
|
|
|
#include "libavutil/avstring.h"
|
2012-11-10 18:00:00 +03:00
|
|
|
#include "libavutil/channel_layout.h"
|
2012-08-06 16:49:32 +03:00
|
|
|
#include "libavutil/common.h"
|
2012-05-28 08:28:58 +03:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
|
|
|
|
#include "audio.h"
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "formats.h"
|
2017-10-27 17:59:29 +02:00
|
|
|
#include "filters.h"
|
2012-05-28 08:28:58 +03:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
typedef struct ChannelMap {
|
2021-08-31 16:03:14 +02:00
|
|
|
int input; ///< input stream index
|
|
|
|
int in_channel_idx; ///< index of in_channel in the input stream data
|
|
|
|
enum AVChannel in_channel;
|
|
|
|
enum AVChannel out_channel;
|
2012-05-28 08:28:58 +03:00
|
|
|
} ChannelMap;
|
|
|
|
|
|
|
|
typedef struct JoinContext {
|
|
|
|
const AVClass *class;
|
|
|
|
|
|
|
|
int inputs;
|
|
|
|
char *map;
|
2021-08-31 16:03:14 +02:00
|
|
|
AVChannelLayout ch_layout;
|
2012-05-28 08:28:58 +03:00
|
|
|
|
2021-08-10 20:56:11 +02:00
|
|
|
int64_t eof_pts;
|
2023-05-30 00:43:08 +02:00
|
|
|
int eof;
|
2021-08-10 20:56:11 +02:00
|
|
|
|
2012-05-28 08:28:58 +03:00
|
|
|
ChannelMap *channels;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Temporary storage for input frames, until we get one on each input.
|
|
|
|
*/
|
2012-11-28 10:41:07 +03:00
|
|
|
AVFrame **input_frames;
|
2012-05-28 08:28:58 +03:00
|
|
|
|
|
|
|
/**
|
2012-11-28 10:41:07 +03:00
|
|
|
* Temporary storage for buffer references, for assembling the output frame.
|
2012-05-28 08:28:58 +03:00
|
|
|
*/
|
2012-11-28 10:41:07 +03:00
|
|
|
AVBufferRef **buffers;
|
2012-05-28 08:28:58 +03:00
|
|
|
} JoinContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(JoinContext, x)
|
|
|
|
#define A AV_OPT_FLAG_AUDIO_PARAM
|
2012-08-13 14:40:01 +03:00
|
|
|
#define F AV_OPT_FLAG_FILTERING_PARAM
|
2012-05-28 08:28:58 +03:00
|
|
|
static const AVOption join_options[] = {
|
2012-09-05 15:26:01 +03:00
|
|
|
{ "inputs", "Number of input streams.", OFFSET(inputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, A|F },
|
2012-05-28 08:28:58 +03:00
|
|
|
{ "channel_layout", "Channel layout of the "
|
2023-11-19 20:35:43 +02:00
|
|
|
"output stream.", OFFSET(ch_layout), AV_OPT_TYPE_CHLAYOUT, {.str = "stereo"}, 0, 0, A|F },
|
2012-05-28 08:28:58 +03:00
|
|
|
{ "map", "A comma-separated list of channels maps in the format "
|
|
|
|
"'input_stream.input_channel-output_channel.",
|
2012-08-13 14:40:01 +03:00
|
|
|
OFFSET(map), AV_OPT_TYPE_STRING, .flags = A|F },
|
2013-09-07 15:13:50 +03:00
|
|
|
{ NULL }
|
2012-05-28 08:28:58 +03:00
|
|
|
};
|
|
|
|
|
2021-08-31 16:03:14 +02:00
|
|
|
#define MAP_SEPARATOR '|'
|
|
|
|
|
2013-05-27 14:10:39 +03:00
|
|
|
AVFILTER_DEFINE_CLASS(join);
|
2012-05-28 08:28:58 +03:00
|
|
|
|
|
|
|
static int parse_maps(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
JoinContext *s = ctx->priv;
|
|
|
|
char *cur = s->map;
|
|
|
|
|
|
|
|
while (cur && *cur) {
|
2021-08-31 16:03:14 +02:00
|
|
|
ChannelMap *map;
|
2012-05-28 08:28:58 +03:00
|
|
|
char *sep, *next, *p;
|
2021-08-31 16:03:14 +02:00
|
|
|
int input_idx, out_ch_idx;
|
2012-05-28 08:28:58 +03:00
|
|
|
|
2021-08-31 16:03:14 +02:00
|
|
|
next = strchr(cur, MAP_SEPARATOR);
|
2012-05-28 08:28:58 +03:00
|
|
|
if (next)
|
|
|
|
*next++ = 0;
|
|
|
|
|
|
|
|
/* split the map into input and output parts */
|
|
|
|
if (!(sep = strchr(cur, '-'))) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Missing separator '-' in channel "
|
|
|
|
"map '%s'\n", cur);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
*sep++ = 0;
|
|
|
|
|
|
|
|
/* parse output channel */
|
2021-08-31 16:03:14 +02:00
|
|
|
out_ch_idx = av_channel_layout_index_from_string(&s->ch_layout, sep);
|
|
|
|
if (out_ch_idx < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Invalid output channel: %s.\n", sep);
|
2012-05-28 08:28:58 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2021-08-31 16:03:14 +02:00
|
|
|
map = &s->channels[out_ch_idx];
|
|
|
|
|
|
|
|
if (map->input >= 0) {
|
2012-05-28 08:28:58 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Multiple maps for output channel "
|
|
|
|
"'%s'.\n", sep);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse input channel */
|
|
|
|
input_idx = strtol(cur, &cur, 0);
|
|
|
|
if (input_idx < 0 || input_idx >= s->inputs) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Invalid input stream index: %d.\n",
|
|
|
|
input_idx);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*cur)
|
|
|
|
cur++;
|
|
|
|
|
2021-08-31 16:03:14 +02:00
|
|
|
map->input = input_idx;
|
|
|
|
map->in_channel = AV_CHAN_NONE;
|
|
|
|
map->in_channel_idx = strtol(cur, &p, 0);
|
2012-05-28 08:28:58 +03:00
|
|
|
if (p == cur) {
|
2021-08-31 16:03:14 +02:00
|
|
|
/* channel specifier is not a number, handle as channel name */
|
|
|
|
map->in_channel = av_channel_from_string(cur);
|
|
|
|
if (map->in_channel < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Invalid input channel: %s.\n", cur);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
} else if (map->in_channel_idx < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Invalid input channel index: %d\n", map->in_channel_idx);
|
|
|
|
return AVERROR(EINVAL);
|
2012-05-28 08:28:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cur = next;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-30 19:30:02 +03:00
|
|
|
static av_cold int join_init(AVFilterContext *ctx)
|
2012-05-28 08:28:58 +03:00
|
|
|
{
|
|
|
|
JoinContext *s = ctx->priv;
|
|
|
|
int ret, i;
|
|
|
|
|
2021-08-31 16:03:14 +02:00
|
|
|
s->channels = av_calloc(s->ch_layout.nb_channels, sizeof(*s->channels));
|
|
|
|
s->buffers = av_calloc(s->ch_layout.nb_channels, sizeof(*s->buffers));
|
2021-09-14 21:31:53 +02:00
|
|
|
s->input_frames = av_calloc(s->inputs, sizeof(*s->input_frames));
|
2013-10-07 18:08:27 +03:00
|
|
|
if (!s->channels || !s->buffers|| !s->input_frames)
|
|
|
|
return AVERROR(ENOMEM);
|
2012-05-28 08:28:58 +03:00
|
|
|
|
2021-08-31 16:03:14 +02:00
|
|
|
for (i = 0; i < s->ch_layout.nb_channels; i++) {
|
|
|
|
s->channels[i].out_channel = av_channel_layout_channel_from_index(&s->ch_layout, i);
|
|
|
|
s->channels[i].input = -1;
|
|
|
|
s->channels[i].in_channel_idx = -1;
|
|
|
|
s->channels[i].in_channel = AV_CHAN_NONE;
|
2012-05-28 08:28:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = parse_maps(ctx)) < 0)
|
2013-10-07 18:08:27 +03:00
|
|
|
return ret;
|
2012-05-28 08:28:58 +03:00
|
|
|
|
|
|
|
for (i = 0; i < s->inputs; i++) {
|
|
|
|
AVFilterPad pad = { 0 };
|
|
|
|
|
2019-09-30 16:52:52 +02:00
|
|
|
pad.type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
pad.name = av_asprintf("input%d", i);
|
2015-01-06 11:42:59 +02:00
|
|
|
if (!pad.name)
|
|
|
|
return AVERROR(ENOMEM);
|
2012-05-28 08:28:58 +03:00
|
|
|
|
2021-08-11 22:02:44 +02:00
|
|
|
if ((ret = ff_append_inpad_free_name(ctx, &pad)) < 0)
|
2017-08-25 09:49:01 +02:00
|
|
|
return ret;
|
2012-05-28 08:28:58 +03:00
|
|
|
}
|
|
|
|
|
2013-10-07 18:08:27 +03:00
|
|
|
return 0;
|
2012-05-28 08:28:58 +03:00
|
|
|
}
|
|
|
|
|
2013-04-30 19:30:02 +03:00
|
|
|
static av_cold void join_uninit(AVFilterContext *ctx)
|
2012-05-28 08:28:58 +03:00
|
|
|
{
|
|
|
|
JoinContext *s = ctx->priv;
|
|
|
|
int i;
|
|
|
|
|
2019-10-21 16:03:22 +02:00
|
|
|
for (i = 0; i < s->inputs && s->input_frames; i++) {
|
|
|
|
av_frame_free(&s->input_frames[i]);
|
|
|
|
}
|
|
|
|
|
2012-05-28 08:28:58 +03:00
|
|
|
av_freep(&s->channels);
|
2012-11-28 10:41:07 +03:00
|
|
|
av_freep(&s->buffers);
|
2012-05-28 08:28:58 +03:00
|
|
|
av_freep(&s->input_frames);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int join_query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
JoinContext *s = ctx->priv;
|
|
|
|
AVFilterChannelLayouts *layouts = NULL;
|
2015-10-05 05:39:25 +02:00
|
|
|
int i, ret;
|
2012-05-28 08:28:58 +03:00
|
|
|
|
2021-08-31 16:03:14 +02:00
|
|
|
if ((ret = ff_add_channel_layout(&layouts, &s->ch_layout)) < 0 ||
|
2020-08-24 12:16:34 +02:00
|
|
|
(ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->incfg.channel_layouts)) < 0)
|
2015-10-05 05:39:25 +02:00
|
|
|
return ret;
|
2012-05-28 08:28:58 +03:00
|
|
|
|
2014-09-26 18:52:09 +03:00
|
|
|
for (i = 0; i < ctx->nb_inputs; i++) {
|
|
|
|
layouts = ff_all_channel_layouts();
|
2020-08-24 12:16:34 +02:00
|
|
|
if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->outcfg.channel_layouts)) < 0)
|
2015-10-05 05:39:25 +02:00
|
|
|
return ret;
|
2014-09-26 18:52:09 +03:00
|
|
|
}
|
2012-05-28 08:28:58 +03:00
|
|
|
|
2015-10-05 05:39:25 +02:00
|
|
|
if ((ret = ff_set_common_formats(ctx, ff_planar_sample_fmts())) < 0 ||
|
2021-08-10 01:25:31 +02:00
|
|
|
(ret = ff_set_common_all_samplerates(ctx)) < 0)
|
2015-10-05 05:39:25 +02:00
|
|
|
return ret;
|
2012-05-28 08:28:58 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-31 16:03:14 +02:00
|
|
|
typedef struct ChannelList {
|
|
|
|
enum AVChannel *ch;
|
|
|
|
int nb_ch;
|
|
|
|
} ChannelList;
|
|
|
|
|
|
|
|
static enum AVChannel channel_list_pop(ChannelList *chl, int idx)
|
|
|
|
{
|
|
|
|
enum AVChannel ret = chl->ch[idx];
|
2022-03-15 16:58:35 +02:00
|
|
|
memmove(chl->ch + idx, chl->ch + idx + 1,
|
|
|
|
(chl->nb_ch - idx - 1) * sizeof(*chl->ch));
|
2021-08-31 16:03:14 +02:00
|
|
|
chl->nb_ch--;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If ch is present in chl, remove it from the list and return it.
|
|
|
|
* Otherwise return AV_CHAN_NONE.
|
|
|
|
*/
|
|
|
|
static enum AVChannel channel_list_pop_ch(ChannelList *chl, enum AVChannel ch)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < chl->nb_ch; i++)
|
|
|
|
if (chl->ch[i] == ch)
|
|
|
|
return channel_list_pop(chl, i);
|
|
|
|
return AV_CHAN_NONE;
|
|
|
|
}
|
|
|
|
|
2012-05-28 08:28:58 +03:00
|
|
|
static void guess_map_matching(AVFilterContext *ctx, ChannelMap *ch,
|
2021-08-31 16:03:14 +02:00
|
|
|
ChannelList *inputs)
|
2012-05-28 08:28:58 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ctx->nb_inputs; i++) {
|
2021-08-31 16:03:14 +02:00
|
|
|
if (channel_list_pop_ch(&inputs[i], ch->out_channel) != AV_CHAN_NONE) {
|
2012-05-28 08:28:58 +03:00
|
|
|
ch->input = i;
|
|
|
|
ch->in_channel = ch->out_channel;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void guess_map_any(AVFilterContext *ctx, ChannelMap *ch,
|
2021-08-31 16:03:14 +02:00
|
|
|
ChannelList *inputs)
|
2012-05-28 08:28:58 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ctx->nb_inputs; i++) {
|
2021-08-31 16:03:14 +02:00
|
|
|
if (inputs[i].nb_ch) {
|
2012-05-28 08:28:58 +03:00
|
|
|
ch->input = i;
|
2021-08-31 16:03:14 +02:00
|
|
|
ch->in_channel = channel_list_pop(&inputs[i], 0);
|
2012-05-28 08:28:58 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int join_config_output(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
JoinContext *s = ctx->priv;
|
2021-08-31 16:03:14 +02:00
|
|
|
// unused channels from each input
|
|
|
|
ChannelList *inputs_unused;
|
|
|
|
char inbuf[64], outbuf[64];
|
2012-05-28 08:28:58 +03:00
|
|
|
int i, ret = 0;
|
|
|
|
|
2021-08-31 16:03:14 +02:00
|
|
|
/* initialize unused channel list for each input */
|
|
|
|
inputs_unused = av_calloc(ctx->nb_inputs, sizeof(*inputs_unused));
|
|
|
|
if (!inputs_unused)
|
2012-05-28 08:28:58 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2021-08-31 16:03:14 +02:00
|
|
|
for (i = 0; i < ctx->nb_inputs; i++) {
|
|
|
|
AVFilterLink *inlink = ctx->inputs[i];
|
|
|
|
AVChannelLayout *chl = &inlink->ch_layout;
|
|
|
|
ChannelList *iu = &inputs_unused[i];
|
|
|
|
|
|
|
|
iu->nb_ch = chl->nb_channels;
|
|
|
|
iu->ch = av_malloc_array(iu->nb_ch, sizeof(*iu->ch));
|
|
|
|
if (!iu->ch) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int ch_idx = 0; ch_idx < iu->nb_ch; ch_idx++) {
|
|
|
|
iu->ch[ch_idx] = av_channel_layout_channel_from_index(chl, ch_idx);
|
|
|
|
if (iu->ch[ch_idx] < 0) {
|
|
|
|
/* no channel ordering information in this input,
|
|
|
|
* so don't auto-map from it */
|
|
|
|
iu->nb_ch = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* process user-specified maps */
|
|
|
|
for (i = 0; i < s->ch_layout.nb_channels; i++) {
|
2012-05-28 08:28:58 +03:00
|
|
|
ChannelMap *ch = &s->channels[i];
|
|
|
|
AVFilterLink *inlink;
|
2021-08-31 16:03:14 +02:00
|
|
|
AVChannelLayout *ichl;
|
|
|
|
ChannelList *iu;
|
2012-05-28 08:28:58 +03:00
|
|
|
|
|
|
|
if (ch->input < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
inlink = ctx->inputs[ch->input];
|
2021-08-31 16:03:14 +02:00
|
|
|
ichl = &inlink->ch_layout;
|
|
|
|
iu = &inputs_unused[ch->input];
|
|
|
|
|
|
|
|
/* get the index for the channels defined by name */
|
|
|
|
if (ch->in_channel != AV_CHAN_NONE) {
|
|
|
|
ch->in_channel_idx = av_channel_layout_index_from_channel(ichl, ch->in_channel);
|
|
|
|
if (ch->in_channel_idx < 0) {
|
|
|
|
av_channel_name(inbuf, sizeof(inbuf), ch->in_channel);
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Requested channel %s is not present in "
|
|
|
|
"input stream #%d.\n", inbuf,
|
|
|
|
ch->input);
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
2012-05-28 08:28:58 +03:00
|
|
|
|
2021-08-31 16:03:14 +02:00
|
|
|
/* make sure channels specified by index actually exist */
|
|
|
|
if (ch->in_channel_idx >= ichl->nb_channels) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Requested channel with index %d is not "
|
|
|
|
"present in input stream #%d.\n", ch->in_channel_idx, ch->input);
|
2012-05-28 08:28:58 +03:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2021-08-31 16:03:14 +02:00
|
|
|
channel_list_pop_ch(iu, av_channel_layout_channel_from_index(ichl, ch->in_channel_idx));
|
2012-05-28 08:28:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* guess channel maps when not explicitly defined */
|
|
|
|
/* first try unused matching channels */
|
2021-08-31 16:03:14 +02:00
|
|
|
for (i = 0; i < s->ch_layout.nb_channels; i++) {
|
2012-05-28 08:28:58 +03:00
|
|
|
ChannelMap *ch = &s->channels[i];
|
|
|
|
|
|
|
|
if (ch->input < 0)
|
2021-08-31 16:03:14 +02:00
|
|
|
guess_map_matching(ctx, ch, inputs_unused);
|
2012-05-28 08:28:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if the above failed, try to find _any_ unused input channel */
|
2021-08-31 16:03:14 +02:00
|
|
|
for (i = 0; i < s->ch_layout.nb_channels; i++) {
|
2012-05-28 08:28:58 +03:00
|
|
|
ChannelMap *ch = &s->channels[i];
|
|
|
|
|
|
|
|
if (ch->input < 0)
|
2021-08-31 16:03:14 +02:00
|
|
|
guess_map_any(ctx, ch, inputs_unused);
|
2012-05-28 08:28:58 +03:00
|
|
|
|
|
|
|
if (ch->input < 0) {
|
2021-08-31 16:03:14 +02:00
|
|
|
av_channel_name(outbuf, sizeof(outbuf), ch->out_channel);
|
2012-05-28 08:28:58 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Could not find input channel for "
|
|
|
|
"output channel '%s'.\n",
|
2021-08-31 16:03:14 +02:00
|
|
|
outbuf);
|
|
|
|
ret = AVERROR(EINVAL);
|
2012-05-28 08:28:58 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2021-08-31 16:03:14 +02:00
|
|
|
if (ch->in_channel != AV_CHAN_NONE) {
|
|
|
|
ch->in_channel_idx = av_channel_layout_index_from_channel(
|
|
|
|
&ctx->inputs[ch->input]->ch_layout, ch->in_channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
av_assert0(ch->in_channel_idx >= 0);
|
2012-05-28 08:28:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* print mappings */
|
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "mappings: ");
|
2021-08-31 16:03:14 +02:00
|
|
|
for (i = 0; i < s->ch_layout.nb_channels; i++) {
|
2012-05-28 08:28:58 +03:00
|
|
|
ChannelMap *ch = &s->channels[i];
|
2021-08-31 16:03:14 +02:00
|
|
|
AVFilterLink *inlink = ctx->inputs[ch->input];
|
|
|
|
AVChannelLayout *ichl = &inlink->ch_layout;
|
|
|
|
enum AVChannel in_ch = av_channel_layout_channel_from_index(
|
|
|
|
ichl, ch->in_channel_idx);
|
|
|
|
|
|
|
|
av_channel_name(inbuf, sizeof(inbuf), in_ch);
|
|
|
|
av_channel_name(outbuf, sizeof(outbuf), ch->out_channel);
|
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "%d.%s(%d) => %s(%d) ", ch->input,
|
|
|
|
inbuf, ch->in_channel_idx,
|
|
|
|
outbuf, i);
|
2012-05-28 08:28:58 +03:00
|
|
|
}
|
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "\n");
|
|
|
|
|
|
|
|
for (i = 0; i < ctx->nb_inputs; i++) {
|
2021-08-31 16:03:14 +02:00
|
|
|
if (inputs_unused[i].nb_ch == ctx->inputs[i]->ch_layout.nb_channels)
|
2012-05-28 08:28:58 +03:00
|
|
|
av_log(ctx, AV_LOG_WARNING, "No channels are used from input "
|
|
|
|
"stream %d.\n", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
2021-08-31 16:03:14 +02:00
|
|
|
for (i = 0; i < ctx->nb_inputs; i++)
|
|
|
|
av_freep(&inputs_unused[i].ch);
|
|
|
|
av_freep(&inputs_unused);
|
2012-05-28 08:28:58 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-22 09:56:11 +02:00
|
|
|
static int try_push_frame(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
JoinContext *s = ctx->priv;
|
|
|
|
AVFrame *frame;
|
|
|
|
int linesize = INT_MAX;
|
|
|
|
int nb_samples = INT_MAX;
|
|
|
|
int nb_buffers = 0;
|
|
|
|
int i, j, ret;
|
2012-05-28 08:28:58 +03:00
|
|
|
|
2015-10-22 09:56:11 +02:00
|
|
|
for (i = 0; i < ctx->nb_inputs; i++) {
|
2021-08-10 20:56:11 +02:00
|
|
|
if (!s->input_frames[i]) {
|
|
|
|
nb_samples = 0;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
nb_samples = FFMIN(nb_samples, s->input_frames[i]->nb_samples);
|
|
|
|
}
|
2012-05-28 08:28:58 +03:00
|
|
|
}
|
2017-10-27 17:59:29 +02:00
|
|
|
if (!nb_samples)
|
2021-08-10 20:56:11 +02:00
|
|
|
goto eof;
|
2012-05-28 08:28:58 +03:00
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
/* setup the output frame */
|
|
|
|
frame = av_frame_alloc();
|
|
|
|
if (!frame)
|
|
|
|
return AVERROR(ENOMEM);
|
2021-08-31 16:03:14 +02:00
|
|
|
if (s->ch_layout.nb_channels > FF_ARRAY_ELEMS(frame->data)) {
|
|
|
|
frame->extended_data = av_calloc(s->ch_layout.nb_channels,
|
2012-11-28 10:41:07 +03:00
|
|
|
sizeof(*frame->extended_data));
|
|
|
|
if (!frame->extended_data) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy the data pointers */
|
2021-08-31 16:03:14 +02:00
|
|
|
for (i = 0; i < s->ch_layout.nb_channels; i++) {
|
2012-05-28 08:28:58 +03:00
|
|
|
ChannelMap *ch = &s->channels[i];
|
2012-11-28 10:41:07 +03:00
|
|
|
AVFrame *cur = s->input_frames[ch->input];
|
|
|
|
AVBufferRef *buf;
|
2012-05-28 08:28:58 +03:00
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
frame->extended_data[i] = cur->extended_data[ch->in_channel_idx];
|
|
|
|
linesize = FFMIN(linesize, cur->linesize[0]);
|
2012-05-28 08:28:58 +03:00
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
/* add the buffer where this plan is stored to the list if it's
|
|
|
|
* not already there */
|
|
|
|
buf = av_frame_get_plane_buffer(cur, ch->in_channel_idx);
|
|
|
|
if (!buf) {
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
for (j = 0; j < nb_buffers; j++)
|
|
|
|
if (s->buffers[j]->buffer == buf->buffer)
|
|
|
|
break;
|
|
|
|
if (j == i)
|
|
|
|
s->buffers[nb_buffers++] = buf;
|
|
|
|
}
|
2012-05-28 08:28:58 +03:00
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
/* create references to the buffers we copied to output */
|
|
|
|
if (nb_buffers > FF_ARRAY_ELEMS(frame->buf)) {
|
|
|
|
frame->nb_extended_buf = nb_buffers - FF_ARRAY_ELEMS(frame->buf);
|
2021-09-14 21:31:53 +02:00
|
|
|
frame->extended_buf = av_calloc(frame->nb_extended_buf,
|
|
|
|
sizeof(*frame->extended_buf));
|
2012-11-28 10:41:07 +03:00
|
|
|
if (!frame->extended_buf) {
|
|
|
|
frame->nb_extended_buf = 0;
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < FFMIN(FF_ARRAY_ELEMS(frame->buf), nb_buffers); i++) {
|
|
|
|
frame->buf[i] = av_buffer_ref(s->buffers[i]);
|
|
|
|
if (!frame->buf[i]) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < frame->nb_extended_buf; i++) {
|
|
|
|
frame->extended_buf[i] = av_buffer_ref(s->buffers[i +
|
|
|
|
FF_ARRAY_ELEMS(frame->buf)]);
|
|
|
|
if (!frame->extended_buf[i]) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
2012-05-28 08:28:58 +03:00
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
frame->nb_samples = nb_samples;
|
2023-05-05 16:07:07 +02:00
|
|
|
frame->duration = av_rescale_q(frame->nb_samples,
|
|
|
|
av_make_q(1, outlink->sample_rate),
|
|
|
|
outlink->time_base);
|
|
|
|
|
2021-08-31 16:03:14 +02:00
|
|
|
#if FF_API_OLD_CHANNEL_LAYOUT
|
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
2012-11-28 10:41:07 +03:00
|
|
|
frame->channel_layout = outlink->channel_layout;
|
2021-08-31 16:03:14 +02:00
|
|
|
frame->channels = outlink->ch_layout.nb_channels;
|
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
|
|
|
#endif
|
|
|
|
if ((ret = av_channel_layout_copy(&frame->ch_layout, &outlink->ch_layout)) < 0)
|
2023-05-10 13:50:13 +02:00
|
|
|
goto fail;
|
2012-11-28 10:41:07 +03:00
|
|
|
frame->sample_rate = outlink->sample_rate;
|
2014-08-01 23:27:13 +03:00
|
|
|
frame->format = outlink->format;
|
2012-11-28 10:41:07 +03:00
|
|
|
frame->pts = s->input_frames[0]->pts;
|
|
|
|
frame->linesize[0] = linesize;
|
|
|
|
if (frame->data != frame->extended_data) {
|
|
|
|
memcpy(frame->data, frame->extended_data, sizeof(*frame->data) *
|
2021-08-31 16:03:14 +02:00
|
|
|
FFMIN(FF_ARRAY_ELEMS(frame->data), s->ch_layout.nb_channels));
|
2012-11-28 10:41:07 +03:00
|
|
|
}
|
2012-05-28 08:28:58 +03:00
|
|
|
|
2021-08-10 20:56:11 +02:00
|
|
|
s->eof_pts = frame->pts + av_rescale_q(frame->nb_samples,
|
|
|
|
av_make_q(1, outlink->sample_rate),
|
|
|
|
outlink->time_base);
|
2012-11-28 10:41:07 +03:00
|
|
|
ret = ff_filter_frame(outlink, frame);
|
2012-05-28 08:28:58 +03:00
|
|
|
|
2013-03-13 22:43:35 +03:00
|
|
|
for (i = 0; i < ctx->nb_inputs; i++)
|
|
|
|
av_frame_free(&s->input_frames[i]);
|
2012-05-28 08:28:58 +03:00
|
|
|
|
2012-07-02 21:13:40 +03:00
|
|
|
return ret;
|
2012-05-28 08:28:58 +03:00
|
|
|
|
|
|
|
fail:
|
2012-11-28 10:41:07 +03:00
|
|
|
av_frame_free(&frame);
|
|
|
|
return ret;
|
2021-08-10 20:56:11 +02:00
|
|
|
eof:
|
|
|
|
for (i = 0; i < ctx->nb_inputs; i++) {
|
2023-05-30 00:43:08 +02:00
|
|
|
if (s->eof &&
|
2021-08-10 20:56:11 +02:00
|
|
|
ff_inlink_queued_samples(ctx->inputs[i]) <= 0 &&
|
|
|
|
!s->input_frames[i]) {
|
|
|
|
ff_outlink_set_status(outlink, AVERROR_EOF, s->eof_pts);
|
2023-05-30 00:43:08 +02:00
|
|
|
break;
|
2021-08-10 20:56:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2012-05-28 08:28:58 +03:00
|
|
|
}
|
|
|
|
|
2017-10-27 17:59:29 +02:00
|
|
|
static int activate(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
JoinContext *s = ctx->priv;
|
|
|
|
int i, ret, status;
|
|
|
|
int nb_samples = 0;
|
|
|
|
int64_t pts;
|
|
|
|
|
2018-05-05 21:24:41 +02:00
|
|
|
FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
|
|
|
|
|
2017-10-27 17:59:29 +02:00
|
|
|
if (!s->input_frames[0]) {
|
|
|
|
ret = ff_inlink_consume_frame(ctx->inputs[0], &s->input_frames[0]);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
2021-08-10 20:56:11 +02:00
|
|
|
} else if (ret == 0 && ff_inlink_acknowledge_status(ctx->inputs[0], &status, &pts)) {
|
2023-05-30 00:43:08 +02:00
|
|
|
s->eof |= status == AVERROR_EOF;
|
2017-10-27 17:59:29 +02:00
|
|
|
}
|
2021-08-10 20:56:11 +02:00
|
|
|
|
2023-05-30 00:43:08 +02:00
|
|
|
if (!s->eof && !s->input_frames[0] && ff_outlink_frame_wanted(ctx->outputs[0])) {
|
2021-08-10 20:56:11 +02:00
|
|
|
ff_inlink_request_frame(ctx->inputs[0]);
|
2018-02-03 01:11:04 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2017-10-27 17:59:29 +02:00
|
|
|
}
|
|
|
|
|
2022-03-31 00:46:47 +02:00
|
|
|
if (s->input_frames[0])
|
|
|
|
nb_samples = s->input_frames[0]->nb_samples;
|
2017-10-27 17:59:29 +02:00
|
|
|
|
|
|
|
for (i = 1; i < ctx->nb_inputs && nb_samples > 0; i++) {
|
|
|
|
if (s->input_frames[i])
|
|
|
|
continue;
|
2021-08-10 20:56:11 +02:00
|
|
|
ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->input_frames[i]);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
} else if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
|
2023-05-30 00:43:08 +02:00
|
|
|
s->eof |= status == AVERROR_EOF;
|
2021-08-10 20:56:11 +02:00
|
|
|
}
|
2017-10-27 17:59:29 +02:00
|
|
|
|
2023-05-30 00:43:08 +02:00
|
|
|
if (!s->eof && !s->input_frames[i]) {
|
2021-08-10 20:56:11 +02:00
|
|
|
ff_inlink_request_frame(ctx->inputs[i]);
|
|
|
|
return 0;
|
2017-10-27 17:59:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return try_push_frame(ctx);
|
|
|
|
}
|
|
|
|
|
2012-07-24 16:14:01 +03:00
|
|
|
static const AVFilterPad avfilter_af_join_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.config_props = join_config_output,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_af_join = {
|
2012-05-28 08:28:58 +03:00
|
|
|
.name = "join",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Join multiple audio streams into "
|
2013-04-14 21:58:31 +03:00
|
|
|
"multi-channel output."),
|
2012-05-28 08:28:58 +03:00
|
|
|
.priv_size = sizeof(JoinContext),
|
2013-02-25 23:21:29 +03:00
|
|
|
.priv_class = &join_class,
|
2012-05-28 08:28:58 +03:00
|
|
|
.init = join_init,
|
|
|
|
.uninit = join_uninit,
|
2017-10-27 17:59:29 +02:00
|
|
|
.activate = activate,
|
2013-09-07 15:13:50 +03:00
|
|
|
.inputs = NULL,
|
2021-08-12 13:05:31 +02:00
|
|
|
FILTER_OUTPUTS(avfilter_af_join_outputs),
|
2021-09-27 12:07:35 +02:00
|
|
|
FILTER_QUERY_FUNC(join_query_formats),
|
2013-09-07 15:13:50 +03:00
|
|
|
.flags = AVFILTER_FLAG_DYNAMIC_INPUTS,
|
2012-05-28 08:28:58 +03:00
|
|
|
};
|