Skip to content

Instantly share code, notes, and snippets.

@ryanmarin
Created October 30, 2018 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanmarin/a4bb8d3d2241b827cff4611e3deca503 to your computer and use it in GitHub Desktop.
Save ryanmarin/a4bb8d3d2241b827cff4611e3deca503 to your computer and use it in GitHub Desktop.
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
char *res, int res_len, int flags)
{
InputSwapContext *inputswap = ctx->priv;
int ret = AVERROR(ENOSYS);
if (!strcmp(cmd, "swap")) {
int nb_streams = 1, i;
char name[16];
char default_streams[16], *stream_specs, *spec, *cursor;
AVStream *st;
/* Close old input */
for (i = 0; i < ctx->nb_outputs; i++) {
av_freep(&ctx->output_pads[i].name);
if (inputswap->st[i].st)
avcodec_free_context(&inputswap->st[i].codec_ctx);
}
av_freep(&inputswap->st);
av_freep(&inputswap->out_index);
if (inputswap->format_ctx)
avformat_close_input(&inputswap->format_ctx);
/* Open new input */
stream_specs = inputswap->stream_specs;
if (!stream_specs) {
snprintf(default_streams, sizeof(default_streams), "d%c%d",
!strcmp(ctx->filter->name, "ainputswap") ? 'a' : 'v',
inputswap->stream_index);
stream_specs = default_streams;
}
for (cursor = stream_specs; *cursor; cursor++)
if (*cursor == '+')
nb_streams++;
inputswap->format_ctx = NULL;
ret = avformat_open_input(&inputswap->format_ctx, args, NULL, NULL);
av_log(ctx, AV_LOG_WARNING, "Open input %d", ret);
ret = avformat_find_stream_info(inputswap->format_ctx, NULL);
av_log(ctx, AV_LOG_WARNING, "stream info %d", ret);
for (i = 0; i < inputswap->format_ctx->nb_streams; i++)
inputswap->format_ctx->streams[i]->discard = AVDISCARD_ALL;
inputswap->st = av_calloc(1, sizeof(*inputswap->st));
for (i = 0; i < nb_streams; i++) {
spec = av_strtok(stream_specs, "+", &cursor);
if (!spec)
return AVERROR_BUG;
stream_specs = NULL;
st = find_stream(ctx, inputswap->format_ctx, spec);
if (!st)
return AVERROR(EINVAL);
st->discard = AVDISCARD_DEFAULT;
inputswap->st[i].st = st;
inputswap->max_stream_index = FFMAX(inputswap->max_stream_index, st->index);
inputswap->st[i].discontinuity_threshold =
av_rescale_q(inputswap->discontinuity_threshold, AV_TIME_BASE_Q, st->time_base);
}
inputswap->out_index = av_calloc(inputswap->max_stream_index + 1,
sizeof(*inputswap->out_index));
for (i = 0; i <= inputswap->max_stream_index; i++)
inputswap->out_index[i] = -1;
for (i = 0; i < nb_streams; i++) {
AVFilterPad pad = { 0 };
inputswap->out_index[inputswap->st[i].st->index] = i;
snprintf(name, sizeof(name), "out%d", i);
pad.type = inputswap->st[i].st->codecpar->codec_type;
pad.name = av_strdup(name);
if (!pad.name)
return AVERROR(ENOMEM);
pad.config_props = inputswap_config_output_props;
pad.request_frame = inputswap_request_frame;
if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
av_freep(&pad.name);
return ret;
}
if ( inputswap->st[i].st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
!inputswap->st[i].st->codecpar->channel_layout) {
ret = guess_channel_layout(&inputswap->st[i], i, ctx);
if (ret < 0)
return ret;
}
ret = open_stream(ctx, &inputswap->st[i]);
if (ret < 0)
return ret;
}
return 0;
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment