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/attributes.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/internal.h"
31 #include "libavutil/mem.h"
32 #include "libavutil/opt.h"
33
34 #include "avfilter.h"
35 #include "audio.h"
36 #include "filters.h"
37 #include "formats.h"
38 #include "internal.h"
39 #include "video.h"
40
41 typedef struct SplitContext {
42 const AVClass *class;
43 int nb_outputs;
44 } SplitContext;
45
split_init(AVFilterContext * ctx)46 static av_cold int split_init(AVFilterContext *ctx)
47 {
48 SplitContext *s = ctx->priv;
49 int i, ret;
50
51 for (i = 0; i < s->nb_outputs; i++) {
52 AVFilterPad pad = { 0 };
53
54 pad.type = ctx->filter->inputs[0].type;
55 pad.name = av_asprintf("output%d", i);
56 if (!pad.name)
57 return AVERROR(ENOMEM);
58
59 if ((ret = ff_append_outpad_free_name(ctx, &pad)) < 0)
60 return ret;
61 }
62
63 return 0;
64 }
65
activate(AVFilterContext * ctx)66 static int activate(AVFilterContext *ctx)
67 {
68 AVFilterLink *inlink = ctx->inputs[0];
69 AVFrame *in;
70 int status, ret;
71 int64_t pts;
72
73 for (int i = 0; i < ctx->nb_outputs; i++) {
74 FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[i], ctx);
75 }
76
77 ret = ff_inlink_consume_frame(inlink, &in);
78 if (ret < 0)
79 return ret;
80 if (ret > 0) {
81 for (int i = 0; i < ctx->nb_outputs; i++) {
82 AVFrame *buf_out;
83
84 if (ff_outlink_get_status(ctx->outputs[i]))
85 continue;
86 buf_out = av_frame_clone(in);
87 if (!buf_out) {
88 ret = AVERROR(ENOMEM);
89 break;
90 }
91
92 ret = ff_filter_frame(ctx->outputs[i], buf_out);
93 if (ret < 0)
94 break;
95 }
96
97 av_frame_free(&in);
98 if (ret < 0)
99 return ret;
100 }
101
102 if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
103 for (int i = 0; i < ctx->nb_outputs; i++) {
104 if (ff_outlink_get_status(ctx->outputs[i]))
105 continue;
106 ff_outlink_set_status(ctx->outputs[i], status, pts);
107 }
108 return 0;
109 }
110
111 for (int i = 0; i < ctx->nb_outputs; i++) {
112 if (ff_outlink_get_status(ctx->outputs[i]))
113 continue;
114
115 if (ff_outlink_frame_wanted(ctx->outputs[i])) {
116 ff_inlink_request_frame(inlink);
117 return 0;
118 }
119 }
120
121 return FFERROR_NOT_READY;
122 }
123
124 #define OFFSET(x) offsetof(SplitContext, x)
125 #define FLAGS (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
126 static const AVOption options[] = {
127 { "outputs", "set number of outputs", OFFSET(nb_outputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, FLAGS },
128 { NULL }
129 };
130
131 AVFILTER_DEFINE_CLASS_EXT(split, "(a)split", options);
132
133 static const AVFilterPad avfilter_vf_split_inputs[] = {
134 {
135 .name = "default",
136 .type = AVMEDIA_TYPE_VIDEO,
137 },
138 };
139
140 const AVFilter ff_vf_split = {
141 .name = "split",
142 .description = NULL_IF_CONFIG_SMALL("Pass on the input to N video outputs."),
143 .priv_size = sizeof(SplitContext),
144 .priv_class = &split_class,
145 .init = split_init,
146 .activate = activate,
147 FILTER_INPUTS(avfilter_vf_split_inputs),
148 .outputs = NULL,
149 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS | AVFILTER_FLAG_METADATA_ONLY,
150 };
151
152 static const AVFilterPad avfilter_af_asplit_inputs[] = {
153 {
154 .name = "default",
155 .type = AVMEDIA_TYPE_AUDIO,
156 },
157 };
158
159 const AVFilter ff_af_asplit = {
160 .name = "asplit",
161 .description = NULL_IF_CONFIG_SMALL("Pass on the audio input to N audio outputs."),
162 .priv_class = &split_class,
163 .priv_size = sizeof(SplitContext),
164 .init = split_init,
165 .activate = activate,
166 FILTER_INPUTS(avfilter_af_asplit_inputs),
167 .outputs = NULL,
168 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS | AVFILTER_FLAG_METADATA_ONLY,
169 };
170