]> git.sesse.net Git - ffmpeg/blob - libavfilter/split.c
83ad765081329b56a562760eef27543318225390
[ffmpeg] / libavfilter / split.c
1 /*
2  * Copyright (c) 2007 Bobby Bingham
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * audio and video splitter
24  */
25
26 #include "avfilter.h"
27 #include "audio.h"
28
29 static int split_init(AVFilterContext *ctx, const char *args, void *opaque)
30 {
31     int i, nb_outputs = 2;
32
33     if (args) {
34         nb_outputs = strtol(args, NULL, 0);
35         if (nb_outputs <= 0) {
36             av_log(ctx, AV_LOG_ERROR, "Invalid number of outputs specified: %d.\n",
37                    nb_outputs);
38             return AVERROR(EINVAL);
39         }
40     }
41
42     for (i = 0; i < nb_outputs; i++) {
43         char name[32];
44         AVFilterPad pad = { 0 };
45
46         snprintf(name, sizeof(name), "output%d", i);
47         pad.type = ctx->filter->inputs[0].type;
48         pad.name = av_strdup(name);
49
50         avfilter_insert_outpad(ctx, i, &pad);
51     }
52
53     return 0;
54 }
55
56 static void split_uninit(AVFilterContext *ctx)
57 {
58     int i;
59
60     for (i = 0; i < ctx->output_count; i++)
61         av_freep(&ctx->output_pads[i].name);
62 }
63
64 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
65 {
66     AVFilterContext *ctx = inlink->dst;
67     int i;
68
69     for (i = 0; i < ctx->output_count; i++)
70         avfilter_start_frame(ctx->outputs[i],
71                              avfilter_ref_buffer(picref, ~AV_PERM_WRITE));
72 }
73
74 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
75 {
76     AVFilterContext *ctx = inlink->dst;
77     int i;
78
79     for (i = 0; i < ctx->output_count; i++)
80         avfilter_draw_slice(ctx->outputs[i], y, h, slice_dir);
81 }
82
83 static void end_frame(AVFilterLink *inlink)
84 {
85     AVFilterContext *ctx = inlink->dst;
86     int i;
87
88     for (i = 0; i < ctx->output_count; i++)
89         avfilter_end_frame(ctx->outputs[i]);
90
91     avfilter_unref_buffer(inlink->cur_buf);
92 }
93
94 AVFilter avfilter_vf_split = {
95     .name      = "split",
96     .description = NULL_IF_CONFIG_SMALL("Pass on the input to two outputs."),
97
98     .init   = split_init,
99     .uninit = split_uninit,
100
101     .inputs    = (AVFilterPad[]) {{ .name            = "default",
102                                     .type            = AVMEDIA_TYPE_VIDEO,
103                                     .get_video_buffer= avfilter_null_get_video_buffer,
104                                     .start_frame     = start_frame,
105                                     .draw_slice      = draw_slice,
106                                     .end_frame       = end_frame, },
107                                   { .name = NULL}},
108     .outputs   = (AVFilterPad[]) {{ .name = NULL}},
109 };
110
111 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
112 {
113     AVFilterContext *ctx = inlink->dst;
114     int i;
115
116     for (i = 0; i < ctx->output_count; i++)
117         ff_filter_samples(inlink->dst->outputs[i],
118                           avfilter_ref_buffer(samplesref, ~AV_PERM_WRITE));
119 }
120
121 AVFilter avfilter_af_asplit = {
122     .name        = "asplit",
123     .description = NULL_IF_CONFIG_SMALL("Pass on the audio input to N audio outputs."),
124
125     .init   = split_init,
126     .uninit = split_uninit,
127
128     .inputs  = (const AVFilterPad[]) {{ .name             = "default",
129                                         .type             = AVMEDIA_TYPE_AUDIO,
130                                         .get_audio_buffer = ff_null_get_audio_buffer,
131                                         .filter_samples   = filter_samples },
132                                       { .name = NULL }},
133     .outputs = (const AVFilterPad[]) {{ .name = NULL }},
134 };