]> git.sesse.net Git - ffmpeg/blob - libavfilter/split.c
libavutil: drop offsetof() fallback definition
[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 #include "internal.h"
29 #include "video.h"
30
31 static int split_init(AVFilterContext *ctx, const char *args)
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 int start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
67 {
68     AVFilterContext *ctx = inlink->dst;
69     int i, ret = 0;
70
71     for (i = 0; i < ctx->nb_outputs; i++) {
72         AVFilterBufferRef *buf_out = avfilter_ref_buffer(picref, ~AV_PERM_WRITE);
73         if (!buf_out)
74             return AVERROR(ENOMEM);
75
76         ret = ff_start_frame(ctx->outputs[i], buf_out);
77         if (ret < 0)
78             break;
79     }
80     return ret;
81 }
82
83 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
84 {
85     AVFilterContext *ctx = inlink->dst;
86     int i, ret = 0;
87
88     for (i = 0; i < ctx->nb_outputs; i++) {
89         ret = ff_draw_slice(ctx->outputs[i], y, h, slice_dir);
90         if (ret < 0)
91             break;
92     }
93     return ret;
94 }
95
96 static int end_frame(AVFilterLink *inlink)
97 {
98     AVFilterContext *ctx = inlink->dst;
99     int i, ret = 0;
100
101     for (i = 0; i < ctx->nb_outputs; i++) {
102         ret = ff_end_frame(ctx->outputs[i]);
103         if (ret < 0)
104             break;
105     }
106     return ret;
107 }
108
109 AVFilter avfilter_vf_split = {
110     .name      = "split",
111     .description = NULL_IF_CONFIG_SMALL("Pass on the input to two outputs."),
112
113     .init   = split_init,
114     .uninit = split_uninit,
115
116     .inputs    = (const AVFilterPad[]) {{ .name            = "default",
117                                           .type            = AVMEDIA_TYPE_VIDEO,
118                                           .get_video_buffer= ff_null_get_video_buffer,
119                                           .start_frame     = start_frame,
120                                           .draw_slice      = draw_slice,
121                                           .end_frame       = end_frame, },
122                                         { .name = NULL}},
123     .outputs   = (const AVFilterPad[]) {{ .name = NULL}},
124 };
125
126 static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
127 {
128     AVFilterContext *ctx = inlink->dst;
129     int i, ret = 0;
130
131     for (i = 0; i < ctx->nb_outputs; i++) {
132         AVFilterBufferRef *buf_out = avfilter_ref_buffer(samplesref,
133                                                          ~AV_PERM_WRITE);
134         if (!buf_out) {
135             ret = AVERROR(ENOMEM);
136             break;
137         }
138
139         ret = ff_filter_samples(inlink->dst->outputs[i], buf_out);
140         if (ret < 0)
141             break;
142     }
143     avfilter_unref_buffer(samplesref);
144     return ret;
145 }
146
147 AVFilter avfilter_af_asplit = {
148     .name        = "asplit",
149     .description = NULL_IF_CONFIG_SMALL("Pass on the audio input to N audio outputs."),
150
151     .init   = split_init,
152     .uninit = split_uninit,
153
154     .inputs  = (const AVFilterPad[]) {{ .name             = "default",
155                                         .type             = AVMEDIA_TYPE_AUDIO,
156                                         .get_audio_buffer = ff_null_get_audio_buffer,
157                                         .filter_samples   = filter_samples },
158                                       { .name = NULL }},
159     .outputs = (const AVFilterPad[]) {{ .name = NULL }},
160 };