split.c 4.01 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright (c) 2007 Bobby Bingham
 *
 * This file is part of Libav.
 *
 * Libav is free software; you can redistribute it and/or
 * 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.
 *
 * Libav is distributed in the hope that it will be useful,
 * 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
 * License along with Libav; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
23
 * audio and video splitter
24 25
 */

26 27
#include <stdio.h>

28
#include "libavutil/attributes.h"
29 30
#include "libavutil/internal.h"
#include "libavutil/mem.h"
31 32
#include "libavutil/opt.h"

33
#include "avfilter.h"
34
#include "audio.h"
35
#include "internal.h"
36
#include "video.h"
37

38 39 40 41 42
typedef struct SplitContext {
    const AVClass *class;
    int nb_outputs;
} SplitContext;

43
static av_cold int split_init(AVFilterContext *ctx)
44
{
45 46
    SplitContext *s = ctx->priv;
    int i;
47

48
    for (i = 0; i < s->nb_outputs; i++) {
49 50 51 52
        char name[32];
        AVFilterPad pad = { 0 };

        snprintf(name, sizeof(name), "output%d", i);
53
        pad.type = ctx->filter->inputs[0].type;
54 55
        pad.name = av_strdup(name);

56
        ff_insert_outpad(ctx, i, &pad);
57 58 59 60 61
    }

    return 0;
}

62
static av_cold void split_uninit(AVFilterContext *ctx)
63 64 65
{
    int i;

66
    for (i = 0; i < ctx->nb_outputs; i++)
67 68 69
        av_freep(&ctx->output_pads[i].name);
}

70
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
71
{
72
    AVFilterContext *ctx = inlink->dst;
73
    int i, ret = 0;
74

75
    for (i = 0; i < ctx->nb_outputs; i++) {
76
        AVFrame *buf_out = av_frame_clone(frame);
77 78
        if (!buf_out) {
            ret = AVERROR(ENOMEM);
79
            break;
80
        }
81

82
        ret = ff_filter_frame(ctx->outputs[i], buf_out);
83 84 85
        if (ret < 0)
            break;
    }
86
    av_frame_free(&frame);
87
    return ret;
88 89
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
#define OFFSET(x) offsetof(SplitContext, x)
#define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM
static const AVOption options[] = {
    { "outputs", "Number of outputs", OFFSET(nb_outputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, FLAGS },
    { NULL },
};

static const AVClass split_class = {
    .class_name = "split",
    .item_name  = av_default_item_name,
    .option     = options,
    .version    = LIBAVUTIL_VERSION_INT,
};

static const AVClass asplit_class = {
    .class_name = "asplit",
    .item_name  = av_default_item_name,
    .option     = options,
    .version    = LIBAVUTIL_VERSION_INT,
};

111 112 113 114 115
static const AVFilterPad avfilter_vf_split_inputs[] = {
    {
        .name             = "default",
        .type             = AVMEDIA_TYPE_VIDEO,
        .get_video_buffer = ff_null_get_video_buffer,
116
        .filter_frame     = filter_frame,
117 118 119 120
    },
    { NULL }
};

121
AVFilter ff_vf_split = {
122
    .name      = "split",
123
    .description = NULL_IF_CONFIG_SMALL("Pass on the input to N video outputs."),
124

125 126 127
    .priv_size  = sizeof(SplitContext),
    .priv_class = &split_class,

128 129 130
    .init   = split_init,
    .uninit = split_uninit,

131
    .inputs    = avfilter_vf_split_inputs,
132
    .outputs   = NULL,
133 134

    .flags     = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
135
};
136

137 138 139 140 141
static const AVFilterPad avfilter_af_asplit_inputs[] = {
    {
        .name             = "default",
        .type             = AVMEDIA_TYPE_AUDIO,
        .get_audio_buffer = ff_null_get_audio_buffer,
142
        .filter_frame     = filter_frame,
143 144 145 146
    },
    { NULL }
};

147
AVFilter ff_af_asplit = {
148 149 150
    .name        = "asplit",
    .description = NULL_IF_CONFIG_SMALL("Pass on the audio input to N audio outputs."),

151 152 153
    .priv_size  = sizeof(SplitContext),
    .priv_class = &asplit_class,

154 155 156
    .init   = split_init,
    .uninit = split_uninit,

157
    .inputs  = avfilter_af_asplit_inputs,
158
    .outputs = NULL,
159 160

    .flags   = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
161
};