2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
25 #include "gstavimux.h"
29 /* elementfactory information */
30 static GstElementDetails
35 "Encodes audio and video into an avi stream",
37 "Wim Taymans <wim.taymans@chello.be>",
41 /* AviMux signals and args */
52 GST_PADTEMPLATE_FACTORY (src_factory,
63 GST_PADTEMPLATE_FACTORY (video_sink_factory,
70 "format", GST_PROPS_STRING ("strf_vids")
74 GST_PADTEMPLATE_FACTORY (audio_sink_factory,
81 "format", GST_PROPS_STRING ("strf_auds")
86 static void gst_avimux_class_init (GstAviMuxClass *klass);
87 static void gst_avimux_init (GstAviMux *avimux);
89 static void gst_avimux_chain (GstPad *pad, GstBuffer *buf);
90 static GstPad* gst_avimux_request_new_pad (GstElement *element, GstPadTemplate *templ, const gchar *name);
92 static void gst_avimux_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
93 static void gst_avimux_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
96 static GstElementClass *parent_class = NULL;
97 //static guint gst_avimux_signals[LAST_SIGNAL] = { 0 };
100 gst_avimux_get_type (void)
102 static GType avimux_type = 0;
105 static const GTypeInfo avimux_info = {
106 sizeof(GstAviMuxClass),
109 (GClassInitFunc)gst_avimux_class_init,
114 (GInstanceInitFunc)gst_avimux_init,
116 avimux_type = g_type_register_static(GST_TYPE_ELEMENT, "GstAviMux", &avimux_info, 0);
122 gst_avimux_class_init (GstAviMuxClass *klass)
124 GObjectClass *gobject_class;
125 GstElementClass *gstelement_class;
127 gobject_class = (GObjectClass*)klass;
128 gstelement_class = (GstElementClass*)klass;
130 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
132 gobject_class->set_property = gst_avimux_set_property;
133 gobject_class->get_property = gst_avimux_get_property;
135 gstelement_class->request_new_pad = gst_avimux_request_new_pad;
139 gst_avimux_init (GstAviMux *avimux)
141 avimux->srcpad = gst_pad_new_from_template (
142 GST_PADTEMPLATE_GET (src_factory), "src");
143 gst_element_add_pad (GST_ELEMENT (avimux), avimux->srcpad);
145 avimux->state = GST_AVIMUX_INITIAL;
147 avimux->num_audio_pads = 0;
148 avimux->num_video_pads = 0;
149 avimux->next_time = 0;
151 avimux->riff = gst_riff_encoder_new (GST_RIFF_RIFF_AVI);
152 avimux->aviheader = g_malloc0 (sizeof (gst_riff_avih));
155 static GstPadConnectReturn
156 gst_avimux_sinkconnect (GstPad *pad, GstCaps *caps)
159 const gchar* format = gst_caps_get_string (caps, "format");
160 gint padnum = GPOINTER_TO_INT (gst_pad_get_element_private (pad));
162 avimux = GST_AVIMUX (gst_pad_get_parent (pad));
164 GST_DEBUG (0, "avimux: sinkconnect triggered on %s (%d), %s\n", gst_pad_get_name (pad),
167 if (!strncmp (format, "strf_vids", 9)) {
168 gst_riff_strf_vids *strf_vids = g_malloc(sizeof(gst_riff_strf_vids));
170 strf_vids->size = sizeof(gst_riff_strf_vids);
171 strf_vids->width = gst_caps_get_int (caps, "width");
172 strf_vids->height = gst_caps_get_int (caps, "height");;
173 strf_vids->planes = gst_caps_get_int (caps, "planes");;
174 strf_vids->bit_cnt = gst_caps_get_int (caps, "bit_cnt");;
175 strf_vids->compression = gst_caps_get_fourcc_int (caps, "compression");;
176 strf_vids->image_size = gst_caps_get_int (caps, "image_size");;
177 strf_vids->xpels_meter = gst_caps_get_int (caps, "xpels_meter");;
178 strf_vids->ypels_meter = gst_caps_get_int (caps, "ypels_meter");;
179 strf_vids->num_colors = gst_caps_get_int (caps, "num_colors");;
180 strf_vids->imp_colors = gst_caps_get_int (caps, "imp_colors");;
182 avimux->video_header[padnum] = strf_vids;
184 else if (!strncmp (format, "strf_auds", 9)) {
187 return GST_PAD_CONNECT_OK;
191 gst_avimux_request_new_pad (GstElement *element,
192 GstPadTemplate *templ,
193 const gchar *req_name)
199 g_return_val_if_fail (templ != NULL, NULL);
201 if (templ->direction != GST_PAD_SINK) {
202 g_warning ("avimux: request pad that is not a SINK pad\n");
206 g_return_val_if_fail (GST_IS_AVIMUX (element), NULL);
208 avimux = GST_AVIMUX (element);
210 if (templ == GST_PADTEMPLATE_GET (audio_sink_factory)) {
211 name = g_strdup_printf ("audio_%02d", avimux->num_audio_pads);
212 newpad = gst_pad_new_from_template (templ, name);
213 gst_pad_set_element_private (newpad, GINT_TO_POINTER (avimux->num_audio_pads));
215 avimux->audio_pad[avimux->num_audio_pads] = newpad;
216 avimux->num_audio_pads++;
218 else if (templ == GST_PADTEMPLATE_GET (video_sink_factory)) {
219 name = g_strdup_printf ("video_%02d", avimux->num_video_pads);
220 newpad = gst_pad_new_from_template (templ, name);
221 gst_pad_set_element_private (newpad, GINT_TO_POINTER (avimux->num_video_pads));
223 avimux->video_pad[avimux->num_video_pads] = newpad;
224 avimux->num_video_pads++;
227 g_warning ("avimux: this is not our template!\n");
231 gst_pad_set_chain_function (newpad, gst_avimux_chain);
232 gst_pad_set_connect_function (newpad, gst_avimux_sinkconnect);
233 gst_element_add_pad (element, newpad);
239 gst_avimux_make_header (GstAviMux *avimux)
245 avimux->aviheader->us_frame = 40000;
246 avimux->aviheader->streams = avimux->num_video_pads + avimux->num_audio_pads;
247 avimux->aviheader->width = -1;
248 avimux->aviheader->height = -1;
249 gst_riff_encoder_avih(avimux->riff, avimux->aviheader, sizeof(gst_riff_avih));
251 memset(&strh, 0, sizeof(gst_riff_strh));
254 gst_riff_encoder_strh(avimux->riff, GST_RIFF_FCC_vids, &strh, sizeof(gst_riff_strh));
256 for (i=0; i<avimux->num_video_pads; i++) {
257 gst_riff_encoder_strf(avimux->riff, avimux->video_header[i], sizeof(gst_riff_strf_vids));
262 gst_avimux_chain (GstPad *pad, GstBuffer *buf)
267 const gchar *padname;
271 g_return_if_fail (pad != NULL);
272 g_return_if_fail (GST_IS_PAD (pad));
273 g_return_if_fail (buf != NULL);
274 g_return_if_fail (GST_BUFFER_DATA (buf) != NULL);
276 avimux = GST_AVIMUX (gst_pad_get_parent (pad));
278 data = (guchar *)GST_BUFFER_DATA(buf);
279 size = GST_BUFFER_SIZE(buf);
281 switch(avimux->state) {
282 case GST_AVIMUX_INITIAL:
283 GST_DEBUG (0,"gst_avimux_chain: writing header\n");
284 gst_avimux_make_header(avimux);
285 newbuf = gst_riff_encoder_get_and_reset_buffer(avimux->riff);
286 gst_pad_push(avimux->srcpad, newbuf);
287 avimux->state = GST_AVIMUX_MOVI;
288 case GST_AVIMUX_MOVI:
289 padname = gst_pad_get_name (pad);
290 channel = GPOINTER_TO_INT (gst_pad_get_element_private (pad));
292 if (strncmp(padname, "audio_", 6) == 0) {
293 GST_DEBUG (0,"gst_avimux_chain: got audio buffer in from channel %02d %lu\n", channel, size);
294 gst_riff_encoder_chunk(avimux->riff, GST_RIFF_01wb, NULL, size);
295 newbuf = gst_riff_encoder_get_and_reset_buffer(avimux->riff);
296 gst_pad_push(avimux->srcpad, newbuf);
298 else if (strncmp(padname, "video_", 6) == 0) {
299 GST_DEBUG (0,"gst_avimux_chain: got video buffer in from channel %02d %lu\n", channel, size);
300 gst_riff_encoder_chunk(avimux->riff, GST_RIFF_00db, NULL, size);
301 newbuf = gst_riff_encoder_get_and_reset_buffer(avimux->riff);
302 GST_DEBUG (0,"gst_avimux_chain: encoded %u\n", GST_BUFFER_SIZE(newbuf));
303 gst_pad_push(avimux->srcpad, newbuf);
305 GST_BUFFER_SIZE(buf) = (GST_BUFFER_SIZE(buf)+1)&~1;
306 gst_pad_push(avimux->srcpad, buf);
312 //gst_buffer_unref(buf);
316 gst_avimux_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
320 /* it's not null if we got it, but it might not be ours */
321 g_return_if_fail(GST_IS_AVIMUX(object));
322 src = GST_AVIMUX(object);
331 gst_avimux_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
335 /* it's not null if we got it, but it might not be ours */
336 g_return_if_fail(GST_IS_AVIMUX(object));
337 src = GST_AVIMUX(object);
341 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
347 plugin_init (GModule *module, GstPlugin *plugin)
349 GstElementFactory *factory;
351 /* this filter needs the riff parser */
352 if (!gst_library_load ("gstriff")) {
353 gst_info ("avimux: could not load support library: 'gstriff'\n");
357 /* create an elementfactory for the avimux element */
358 factory = gst_elementfactory_new ("avimux", GST_TYPE_AVIMUX,
359 &gst_avimux_details);
360 g_return_val_if_fail (factory != NULL, FALSE);
362 gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (src_factory));
363 gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (audio_sink_factory));
364 gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (video_sink_factory));
366 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
371 GstPluginDesc plugin_desc = {