]> git.sesse.net Git - ffmpeg/blob - libavfilter/split.c
Merge commit 'b522000e9b2ca36fe5b2751096b9a5f5ed8f87e6'
[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 <stdio.h>
27
28 #include "libavutil/internal.h"
29 #include "libavutil/mem.h"
30 #include "avfilter.h"
31 #include "audio.h"
32 #include "internal.h"
33 #include "video.h"
34
35 static int split_init(AVFilterContext *ctx, const char *args)
36 {
37     int i, nb_outputs = 2;
38
39     if (args) {
40         nb_outputs = strtol(args, NULL, 0);
41         if (nb_outputs <= 0) {
42             av_log(ctx, AV_LOG_ERROR, "Invalid number of outputs specified: %d.\n",
43                    nb_outputs);
44             return AVERROR(EINVAL);
45         }
46     }
47
48     for (i = 0; i < nb_outputs; i++) {
49         char name[32];
50         AVFilterPad pad = { 0 };
51
52         snprintf(name, sizeof(name), "output%d", i);
53         pad.type = ctx->filter->inputs[0].type;
54         pad.name = av_strdup(name);
55         pad.rej_perms = AV_PERM_WRITE;
56
57         ff_insert_outpad(ctx, i, &pad);
58     }
59
60     return 0;
61 }
62
63 static void split_uninit(AVFilterContext *ctx)
64 {
65     int i;
66
67     for (i = 0; i < ctx->nb_outputs; i++)
68         av_freep(&ctx->output_pads[i].name);
69 }
70
71 static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
72 {
73     AVFilterContext *ctx = inlink->dst;
74     int i, ret = AVERROR_EOF;
75
76     for (i = 0; i < ctx->nb_outputs; i++) {
77         AVFilterBufferRef *buf_out;
78
79         if (ctx->outputs[i]->closed)
80             continue;
81         buf_out = avfilter_ref_buffer(picref, ~AV_PERM_WRITE);
82         if (!buf_out)
83             return AVERROR(ENOMEM);
84
85         ret = ff_start_frame(ctx->outputs[i], buf_out);
86         if (ret < 0)
87             break;
88     }
89     return ret;
90 }
91
92 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
93 {
94     AVFilterContext *ctx = inlink->dst;
95     int i, ret = AVERROR_EOF;
96
97     for (i = 0; i < ctx->nb_outputs; i++) {
98         if (ctx->outputs[i]->closed)
99             continue;
100         ret = ff_draw_slice(ctx->outputs[i], y, h, slice_dir);
101         if (ret < 0)
102             break;
103     }
104     return ret;
105 }
106
107 static int end_frame(AVFilterLink *inlink)
108 {
109     AVFilterContext *ctx = inlink->dst;
110     int i, ret = AVERROR_EOF;
111
112     for (i = 0; i < ctx->nb_outputs; i++) {
113         if (ctx->outputs[i]->closed)
114             continue;
115         ret = ff_end_frame(ctx->outputs[i]);
116         if (ret < 0)
117             break;
118     }
119     return ret;
120 }
121
122 AVFilter avfilter_vf_split = {
123     .name      = "split",
124     .description = NULL_IF_CONFIG_SMALL("Pass on the input video to N outputs."),
125
126     .init   = split_init,
127     .uninit = split_uninit,
128
129     .inputs    = (const AVFilterPad[]) {{ .name            = "default",
130                                           .type            = AVMEDIA_TYPE_VIDEO,
131                                           .get_video_buffer= ff_null_get_video_buffer,
132                                           .start_frame     = start_frame,
133                                           .draw_slice      = draw_slice,
134                                           .end_frame       = end_frame, },
135                                         { .name = NULL}},
136     .outputs   = NULL,
137 };
138
139 static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
140 {
141     AVFilterContext *ctx = inlink->dst;
142     int i, ret = 0;
143
144     for (i = 0; i < ctx->nb_outputs; i++) {
145         AVFilterBufferRef *buf_out = avfilter_ref_buffer(samplesref,
146                                                          ~AV_PERM_WRITE);
147         if (!buf_out) {
148             ret = AVERROR(ENOMEM);
149             break;
150         }
151
152         ret = ff_filter_samples(inlink->dst->outputs[i], buf_out);
153         if (ret < 0)
154             break;
155     }
156     avfilter_unref_buffer(samplesref);
157     return ret;
158 }
159
160 AVFilter avfilter_af_asplit = {
161     .name        = "asplit",
162     .description = NULL_IF_CONFIG_SMALL("Pass on the audio input to N audio outputs."),
163
164     .init   = split_init,
165     .uninit = split_uninit,
166
167     .inputs  = (const AVFilterPad[]) {{ .name             = "default",
168                                         .type             = AVMEDIA_TYPE_AUDIO,
169                                         .get_audio_buffer = ff_null_get_audio_buffer,
170                                         .filter_samples   = filter_samples },
171                                       { .name = NULL }},
172     .outputs = NULL,
173 };