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