]> git.sesse.net Git - ffmpeg/blob - libavfilter/split.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / split.c
1 /*
2  * Copyright (c) 2007 Bobby Bingham
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 #include "video.h"
29
30 static int split_init(AVFilterContext *ctx, const char *args, void *opaque)
31 {
32     int i, nb_outputs = 2;
33
34     if (args) {
35         nb_outputs = strtol(args, NULL, 0);
36         if (nb_outputs <= 0) {
37             av_log(ctx, AV_LOG_ERROR, "Invalid number of outputs specified: %d.\n",
38                    nb_outputs);
39             return AVERROR(EINVAL);
40         }
41     }
42
43     for (i = 0; i < nb_outputs; i++) {
44         char name[32];
45         AVFilterPad pad = { 0 };
46
47         snprintf(name, sizeof(name), "output%d", i);
48         pad.type = ctx->filter->inputs[0].type;
49         pad.name = av_strdup(name);
50
51         avfilter_insert_outpad(ctx, i, &pad);
52     }
53
54     return 0;
55 }
56
57 static void split_uninit(AVFilterContext *ctx)
58 {
59     int i;
60
61     for (i = 0; i < ctx->output_count; i++)
62         av_freep(&ctx->output_pads[i].name);
63 }
64
65 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
66 {
67     AVFilterContext *ctx = inlink->dst;
68     int i;
69
70     for (i = 0; i < ctx->output_count; i++)
71         avfilter_start_frame(ctx->outputs[i],
72                              avfilter_ref_buffer(picref, ~AV_PERM_WRITE));
73 }
74
75 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
76 {
77     AVFilterContext *ctx = inlink->dst;
78     int i;
79
80     for (i = 0; i < ctx->output_count; i++)
81         avfilter_draw_slice(ctx->outputs[i], y, h, slice_dir);
82 }
83
84 static void end_frame(AVFilterLink *inlink)
85 {
86     AVFilterContext *ctx = inlink->dst;
87     int i;
88
89     for (i = 0; i < ctx->output_count; i++)
90         avfilter_end_frame(ctx->outputs[i]);
91
92     avfilter_unref_buffer(inlink->cur_buf);
93 }
94
95 AVFilter avfilter_vf_split = {
96     .name      = "split",
97     .description = NULL_IF_CONFIG_SMALL("Pass on the input to two outputs."),
98
99     .init   = split_init,
100     .uninit = split_uninit,
101
102     .inputs    = (const AVFilterPad[]) {{ .name      = "default",
103                                     .type            = AVMEDIA_TYPE_VIDEO,
104                                     .get_video_buffer= ff_null_get_video_buffer,
105                                     .start_frame     = start_frame,
106                                     .draw_slice      = draw_slice,
107                                     .end_frame       = end_frame, },
108                                   { .name = NULL}},
109     .outputs   = (AVFilterPad[]) {{ .name = NULL}},
110 };
111
112 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
113 {
114     AVFilterContext *ctx = inlink->dst;
115     int i;
116
117     for (i = 0; i < ctx->output_count; i++)
118         ff_filter_samples(inlink->dst->outputs[i],
119                           avfilter_ref_buffer(samplesref, ~AV_PERM_WRITE));
120 }
121
122 AVFilter avfilter_af_asplit = {
123     .name        = "asplit",
124     .description = NULL_IF_CONFIG_SMALL("Pass on the audio input to N audio outputs."),
125
126     .init   = split_init,
127     .uninit = split_uninit,
128
129     .inputs  = (const AVFilterPad[]) {{ .name             = "default",
130                                         .type             = AVMEDIA_TYPE_AUDIO,
131                                         .get_audio_buffer = ff_null_get_audio_buffer,
132                                         .filter_samples   = filter_samples },
133                                       { .name = NULL }},
134     .outputs = (const AVFilterPad[]) {{ .name = NULL }},
135 };