1 /* AVI muxer plugin for GStreamer
2 * Copyright (C) 2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
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.
21 * - the old avimuxer (by Wim Taymans)
22 * - xawtv's aviwriter (by Gerd Knorr)
23 * - mjpegtools' avilib (by Rainer Johanni)
24 * - openDML large-AVI docs
33 #include "gstavimux.h"
35 #ifndef LE_FROM_GUINT16
36 #define LE_FROM_GUINT16 GUINT16_FROM_LE
39 #ifndef LE_FROM_GUINT32
40 #define LE_FROM_GUINT32 GUINT32_FROM_LE
44 /* elementfactory information */
45 static GstElementDetails
50 "Muxes audio and video into an avi stream",
52 "Ronald Bultje <rbultje@ronald.bitfreak.net>",
56 /* AviMux signals and args */
67 GST_PADTEMPLATE_FACTORY (src_factory,
78 GST_PADTEMPLATE_FACTORY (video_sink_factory,
85 "format", GST_PROPS_STRING ("strf_vids")
90 "format", GST_PROPS_LIST (
91 GST_PROPS_FOURCC (GST_MAKE_FOURCC('Y','U','Y','2')),
92 GST_PROPS_FOURCC (GST_MAKE_FOURCC('I','4','2','0')),
93 GST_PROPS_FOURCC (GST_MAKE_FOURCC('Y','4','1','P'))
95 "width", GST_PROPS_INT_RANGE (16, 4096),
96 "height", GST_PROPS_INT_RANGE (16, 4096)
101 "format", GST_PROPS_FOURCC (GST_MAKE_FOURCC('R','G','B',' ')),
102 "width", GST_PROPS_INT_RANGE (16, 4096),
103 "height", GST_PROPS_INT_RANGE (16, 4096),
104 "depth", GST_PROPS_LIST(
110 "bpp", GST_PROPS_LIST(
120 "width", GST_PROPS_INT_RANGE (16, 4096),
121 "height", GST_PROPS_INT_RANGE (16, 4096)
125 GST_PADTEMPLATE_FACTORY (audio_sink_factory,
132 "format", GST_PROPS_STRING ("strf_auds")
137 "format", GST_PROPS_STRING ("int"),
138 "law", GST_PROPS_INT (0),
139 "endianness", GST_PROPS_INT (G_BYTE_ORDER),
140 "signed", GST_PROPS_LIST (
141 GST_PROPS_BOOLEAN (TRUE),
142 GST_PROPS_BOOLEAN (FALSE)
144 "width", GST_PROPS_LIST (
148 "depth", GST_PROPS_LIST (
152 "rate", GST_PROPS_INT_RANGE (11025, 44100),
153 "channels", GST_PROPS_INT_RANGE (1, 2)
163 static void gst_avimux_class_init (GstAviMuxClass *klass);
164 static void gst_avimux_init (GstAviMux *avimux);
166 static void gst_avimux_chain (GstPad *pad,
168 static gboolean gst_avimux_handle_event (GstPad *pad,
170 static GstPad* gst_avimux_request_new_pad (GstElement *element,
171 GstPadTemplate *templ,
173 static void gst_avimux_set_property (GObject *object,
177 static void gst_avimux_get_property (GObject *object,
181 static GstElementStateReturn gst_avimux_change_state (GstElement *element);
183 static GstElementClass *parent_class = NULL;
184 /*static guint gst_avimux_signals[LAST_SIGNAL] = { 0 }; */
187 gst_avimux_get_type (void)
189 static GType avimux_type = 0;
192 static const GTypeInfo avimux_info = {
193 sizeof(GstAviMuxClass),
196 (GClassInitFunc)gst_avimux_class_init,
201 (GInstanceInitFunc)gst_avimux_init,
203 avimux_type = g_type_register_static(GST_TYPE_ELEMENT, "GstAviMux", &avimux_info, 0);
209 gst_avimux_class_init (GstAviMuxClass *klass)
211 GObjectClass *gobject_class;
212 GstElementClass *gstelement_class;
214 gobject_class = (GObjectClass*)klass;
215 gstelement_class = (GstElementClass*)klass;
217 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
219 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_BIGFILE,
220 g_param_spec_boolean("bigfile","Bigfile Support","Whether to capture large or small AVI files",
221 0,G_PARAM_READWRITE));
223 gstelement_class->request_new_pad = gst_avimux_request_new_pad;
225 gstelement_class->change_state = gst_avimux_change_state;
227 gstelement_class->get_property = gst_avimux_get_property;
228 gstelement_class->set_property = gst_avimux_set_property;
232 gst_avimux_init (GstAviMux *avimux)
235 avimux->srcpad = gst_pad_new_from_template (
236 GST_PADTEMPLATE_GET (src_factory), "src");
237 gst_element_add_pad (GST_ELEMENT (avimux), avimux->srcpad);
239 GST_FLAG_SET (GST_ELEMENT(avimux), GST_ELEMENT_EVENT_AWARE);
240 gst_pad_set_event_function(avimux->srcpad, gst_avimux_handle_event);
242 for (i=0;i<MAX_NUM_AUDIO_PADS;i++)
243 avimux->audiosinkpad[i] = NULL;
244 avimux->num_audio_pads = 0;
245 for (i=0;i<MAX_NUM_VIDEO_PADS;i++)
246 avimux->videosinkpad[i] = NULL;
247 avimux->num_video_pads = 0;
249 avimux->num_frames = 0;
251 /* audio/video/AVI header initialisation */
252 memset(&(avimux->avi_hdr),0,sizeof(gst_riff_avih));
253 memset(&(avimux->vids_hdr),0,sizeof(gst_riff_strh));
254 memset(&(avimux->vids),0,sizeof(gst_riff_strf_vids));
255 memset(&(avimux->auds_hdr),0,sizeof(gst_riff_strh));
256 memset(&(avimux->auds),0,sizeof(gst_riff_strf_auds));
257 avimux->vids_hdr.type = GST_MAKE_FOURCC('v','i','d','s');
258 avimux->vids_hdr.rate = 1000000;
259 avimux->auds_hdr.type = GST_MAKE_FOURCC('a','u','d','s');
263 avimux->write_header = TRUE;
265 avimux->enable_large_avi = TRUE;
268 static GstPadConnectReturn
269 gst_avimux_sinkconnect (GstPad *pad, GstCaps *vscaps)
274 avimux = GST_AVIMUX (gst_pad_get_parent (pad));
276 /* we are not going to act on variable caps */
277 if (!GST_CAPS_IS_FIXED (vscaps))
278 return GST_PAD_CONNECT_DELAYED;
280 GST_DEBUG (0, "avimux: sinkconnect triggered on %s\n", gst_pad_get_name (pad));
282 for (caps = vscaps; caps != NULL; caps = vscaps = vscaps->next)
284 const gchar* mimetype = gst_caps_get_mime(caps);
286 if (!strcmp (mimetype, "video/avi"))
288 const gchar* format = gst_caps_get_string(caps, "format");
290 if (!strncmp (format, "strf_vids", 9)) {
291 avimux->vids.size = sizeof(gst_riff_strf_vids);
292 avimux->vids.width = gst_caps_get_int (caps, "width");
293 avimux->vids.height = gst_caps_get_int (caps, "height");
294 avimux->vids.planes = gst_caps_get_int (caps, "planes");
295 avimux->vids.bit_cnt = gst_caps_get_int (caps, "bit_cnt");
296 avimux->vids.compression = gst_caps_get_fourcc_int (caps, "compression");
297 avimux->vids.image_size = gst_caps_get_int (caps, "image_size");
298 avimux->vids.xpels_meter = gst_caps_get_int (caps, "xpels_meter");
299 avimux->vids.ypels_meter = gst_caps_get_int (caps, "ypels_meter");
300 avimux->vids.num_colors = gst_caps_get_int (caps, "num_colors");
301 avimux->vids.imp_colors = gst_caps_get_int (caps, "imp_colors");
303 else if (!strncmp (format, "strf_auds", 9)) {
304 avimux->auds.format = gst_caps_get_int (caps, "format");
305 avimux->auds.channels = gst_caps_get_int (caps, "channels");
306 avimux->auds.rate = gst_caps_get_int (caps, "rate");
307 avimux->auds.av_bps = gst_caps_get_int (caps, "av_bps");
308 avimux->auds.blockalign = gst_caps_get_int (caps, "blockalign");
309 avimux->auds.size = gst_caps_get_int (caps, "size");
313 else if (!strcmp (mimetype, "video/raw"))
315 switch (gst_caps_get_fourcc_int(caps, "format"))
317 case GST_MAKE_FOURCC('Y','U','Y','2'):
318 case GST_MAKE_FOURCC('I','4','2','0'):
319 case GST_MAKE_FOURCC('Y','4','1','P'):
320 case GST_MAKE_FOURCC('R','G','B',' '):
321 avimux->vids.size = sizeof(gst_riff_strf_vids);
322 avimux->vids.width = gst_caps_get_int (caps, "width");
323 avimux->vids.height = gst_caps_get_int (caps, "height");
324 avimux->vids.planes = 1;
325 switch (gst_caps_get_fourcc_int(caps, "format"))
327 case GST_MAKE_FOURCC('Y','U','Y','2'):
328 avimux->vids.bit_cnt = 16; /* YUY2 */
330 case GST_MAKE_FOURCC('R','G','B',' '):
331 avimux->vids.bit_cnt = gst_caps_get_fourcc_int(caps, "bpp"); /* RGB */
333 case GST_MAKE_FOURCC('Y','4','1','P'):
334 case GST_MAKE_FOURCC('I','4','2','0'):
335 avimux->vids.bit_cnt = 12; /* Y41P or I420 */
338 avimux->vids.compression = gst_caps_get_fourcc_int(caps, "format");
339 avimux->vids.image_size = avimux->vids.height * avimux->vids.width;
345 else if (!strcmp (mimetype, "video/jpeg"))
347 avimux->vids.size = sizeof(gst_riff_strf_vids);
348 avimux->vids.width = gst_caps_get_int (caps, "width");
349 avimux->vids.height = gst_caps_get_int (caps, "height");
350 avimux->vids.planes = 1;
351 avimux->vids.bit_cnt = 24;
352 avimux->vids.compression = GST_MAKE_FOURCC('M','J','P','G');
353 avimux->vids.image_size = avimux->vids.height * avimux->vids.width;
356 else if (!strcmp (mimetype, "audio/raw"))
358 printf("WE HAVE AUDIO\n");
359 avimux->auds.format = GST_RIFF_WAVE_FORMAT_PCM;
360 avimux->auds.channels = gst_caps_get_int (caps, "channels");
361 avimux->auds.rate = gst_caps_get_int (caps, "rate");
362 avimux->auds.av_bps = gst_caps_get_int (caps, "width") * avimux->auds.rate *
363 avimux->auds.channels / 8;
364 avimux->auds.blockalign = gst_caps_get_int (caps, "width") * avimux->auds.channels/8;
365 avimux->auds.size = gst_caps_get_int (caps, "depth");
368 else if (!strcmp (mimetype, "audio/mp3"))
370 /* we don't need to do anything here, compressed mp3 contains it all */
371 avimux->auds.format = gst_caps_get_int(caps, "layer")==3?
372 GST_RIFF_WAVE_FORMAT_MPEGL3:GST_RIFF_WAVE_FORMAT_MPEGL12;
376 return GST_PAD_CONNECT_REFUSED;
379 return GST_PAD_CONNECT_OK;
383 gst_avimux_request_new_pad (GstElement *element,
384 GstPadTemplate *templ,
385 const gchar *req_name)
391 g_return_val_if_fail (templ != NULL, NULL);
393 if (templ->direction != GST_PAD_SINK) {
394 g_warning ("avimux: request pad that is not a SINK pad\n");
398 g_return_val_if_fail (GST_IS_AVIMUX (element), NULL);
400 avimux = GST_AVIMUX (element);
402 if (templ == GST_PADTEMPLATE_GET (audio_sink_factory)) {
403 printf("AUDIOPAD REQUESTED\n");
404 g_return_val_if_fail(avimux->num_audio_pads == 0 /*< MAX_NUM_AUDIO_PADS*/, NULL);
405 name = g_strdup_printf ("audio_%02d", avimux->num_audio_pads);
406 newpad = gst_pad_new_from_template (templ, name);
407 gst_pad_set_element_private (newpad, GINT_TO_POINTER (avimux->num_audio_pads));
409 avimux->audiosinkpad[avimux->num_audio_pads] = newpad;
410 avimux->num_audio_pads++;
412 else if (templ == GST_PADTEMPLATE_GET (video_sink_factory)) {
413 printf("VIDEOPAD %d REQUESTED\n", avimux->num_video_pads);
414 g_return_val_if_fail(avimux->num_video_pads == 0 /*< MAX_NUM_VIDEO_PADS*/, NULL);
415 name = g_strdup_printf ("video_%02d", avimux->num_video_pads);
416 newpad = gst_pad_new_from_template (templ, name);
417 gst_pad_set_element_private (newpad, GINT_TO_POINTER (avimux->num_video_pads));
419 avimux->videosinkpad[avimux->num_video_pads] = newpad;
420 avimux->num_video_pads++;
423 g_warning ("avimux: this is not our template!\n");
427 gst_pad_set_chain_function (newpad, gst_avimux_chain);
428 gst_pad_set_connect_function (newpad, gst_avimux_sinkconnect);
429 gst_element_add_pad (element, newpad);
434 /* maybe some of these functions should be moved to riff.h? */
436 /* DISCLAIMER: this function is ugly. So be it (i.e. it makes the rest easier) */
439 gst_avimux_riff_get_avi_header (GstAviMux *avimux)
446 buffer = gst_buffer_new();
448 /* first, let's see what actually needs to be in the buffer */
449 GST_BUFFER_SIZE(buffer) = 0;
450 GST_BUFFER_SIZE(buffer) += 32 + sizeof(gst_riff_avih); /* avi header */
451 if (avimux->num_video_pads)
452 { /* we have video */
453 GST_BUFFER_SIZE(buffer) += 28 + sizeof(gst_riff_strh) + sizeof(gst_riff_strf_vids); /* vid hdr */
454 GST_BUFFER_SIZE(buffer) += 24; /* odml header */
456 if (avimux->num_audio_pads)
457 { /* we have audio */
458 GST_BUFFER_SIZE(buffer) += 28 + sizeof(gst_riff_strh) + sizeof(gst_riff_strf_auds); /* aud hdr */
460 /* this is the "riff size" */
461 avimux->header_size = GST_BUFFER_SIZE(buffer);
462 GST_BUFFER_SIZE(buffer) += 12; /* avi data header */
464 /* allocate the buffer */
465 buffdata = GST_BUFFER_DATA(buffer) = g_malloc(GST_BUFFER_SIZE(buffer));
467 /* avi header metadata */
468 memcpy(buffdata, "RIFF", 4); buffdata += 4;
469 temp32 = LE_FROM_GUINT32(avimux->header_size + avimux->idx_size + avimux->data_size);
470 memcpy(buffdata, &temp32, 4); buffdata += 4;
471 memcpy(buffdata, "AVI ", 4); buffdata += 4;
472 memcpy(buffdata, "LIST", 4); buffdata += 4;
473 temp32 = LE_FROM_GUINT32(avimux->header_size - 4*5);
474 memcpy(buffdata, &temp32, 4); buffdata += 4;
475 memcpy(buffdata, "hdrl", 4); buffdata += 4;
476 memcpy(buffdata, "avih", 4); buffdata += 4;
477 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_avih));
478 memcpy(buffdata, &temp32, 4); buffdata += 4;
479 /* the AVI header itself */
480 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.us_frame);
481 memcpy(buffdata, &temp32, 4); buffdata += 4;
482 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.max_bps);
483 memcpy(buffdata, &temp32, 4); buffdata += 4;
484 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.pad_gran);
485 memcpy(buffdata, &temp32, 4); buffdata += 4;
486 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.flags);
487 memcpy(buffdata, &temp32, 4); buffdata += 4;
488 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.tot_frames);
489 memcpy(buffdata, &temp32, 4); buffdata += 4;
490 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.init_frames);
491 memcpy(buffdata, &temp32, 4); buffdata += 4;
492 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.streams);
493 memcpy(buffdata, &temp32, 4); buffdata += 4;
494 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.bufsize);
495 memcpy(buffdata, &temp32, 4); buffdata += 4;
496 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.width);
497 memcpy(buffdata, &temp32, 4); buffdata += 4;
498 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.height);
499 memcpy(buffdata, &temp32, 4); buffdata += 4;
500 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.scale);
501 memcpy(buffdata, &temp32, 4); buffdata += 4;
502 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.rate);
503 memcpy(buffdata, &temp32, 4); buffdata += 4;
504 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.start);
505 memcpy(buffdata, &temp32, 4); buffdata += 4;
506 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.length);
507 memcpy(buffdata, &temp32, 4); buffdata += 4;
509 if (avimux->num_video_pads)
511 /* video header metadata */
512 memcpy(buffdata, "LIST", 4); buffdata += 4;
513 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh) + sizeof(gst_riff_strf_vids) + 4*5);
514 memcpy(buffdata, &temp32, 4); buffdata += 4;
515 memcpy(buffdata, "strl", 4); buffdata += 4;
517 memcpy(buffdata, "strh", 4); buffdata += 4;
518 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh));
519 memcpy(buffdata, &temp32, 4); buffdata += 4;
520 /* the actual header */
521 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.type);
522 memcpy(buffdata, &temp32, 4); buffdata += 4;
523 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.fcc_handler);
524 memcpy(buffdata, &temp32, 4); buffdata += 4;
525 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.flags);
526 memcpy(buffdata, &temp32, 4); buffdata += 4;
527 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.priority);
528 memcpy(buffdata, &temp32, 4); buffdata += 4;
529 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.init_frames);
530 memcpy(buffdata, &temp32, 4); buffdata += 4;
531 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.scale);
532 memcpy(buffdata, &temp32, 4); buffdata += 4;
533 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.rate);
534 memcpy(buffdata, &temp32, 4); buffdata += 4;
535 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.start);
536 memcpy(buffdata, &temp32, 4); buffdata += 4;
537 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.length);
538 memcpy(buffdata, &temp32, 4); buffdata += 4;
539 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.bufsize);
540 memcpy(buffdata, &temp32, 4); buffdata += 4;
541 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.quality);
542 memcpy(buffdata, &temp32, 4); buffdata += 4;
543 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.samplesize);
544 memcpy(buffdata, &temp32, 4); buffdata += 4;
545 /* the video header */
546 memcpy(buffdata, "strf", 4); buffdata += 4;
547 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strf_vids));
548 memcpy(buffdata, &temp32, 4); buffdata += 4;
549 /* the actual header */
550 temp32 = LE_FROM_GUINT32(avimux->vids.size);
551 memcpy(buffdata, &temp32, 4); buffdata += 4;
552 temp32 = LE_FROM_GUINT32(avimux->vids.width);
553 memcpy(buffdata, &temp32, 4); buffdata += 4;
554 temp32 = LE_FROM_GUINT32(avimux->vids.height);
555 memcpy(buffdata, &temp32, 4); buffdata += 4;
556 temp16 = LE_FROM_GUINT16(avimux->vids.planes);
557 memcpy(buffdata, &temp16, 2); buffdata += 2;
558 temp16 = LE_FROM_GUINT16(avimux->vids.bit_cnt);
559 memcpy(buffdata, &temp16, 2); buffdata += 2;
560 temp32 = LE_FROM_GUINT32(avimux->vids.compression);
561 memcpy(buffdata, &temp32, 4); buffdata += 4;
562 temp32 = LE_FROM_GUINT32(avimux->vids.image_size);
563 memcpy(buffdata, &temp32, 4); buffdata += 4;
564 temp32 = LE_FROM_GUINT32(avimux->vids.xpels_meter);
565 memcpy(buffdata, &temp32, 4); buffdata += 4;
566 temp32 = LE_FROM_GUINT32(avimux->vids.ypels_meter);
567 memcpy(buffdata, &temp32, 4); buffdata += 4;
568 temp32 = LE_FROM_GUINT32(avimux->vids.num_colors);
569 memcpy(buffdata, &temp32, 4); buffdata += 4;
570 temp32 = LE_FROM_GUINT32(avimux->vids.imp_colors);
571 memcpy(buffdata, &temp32, 4); buffdata += 4;
574 if (avimux->num_audio_pads)
577 memcpy(buffdata, "LIST", 4); buffdata += 4;
578 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh) + sizeof(gst_riff_strf_auds) + 4*5);
579 memcpy(buffdata, &temp32, 4); buffdata += 4;
580 memcpy(buffdata, "strl", 4); buffdata += 4;
582 memcpy(buffdata, "strh", 4); buffdata += 4;
583 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh));
584 memcpy(buffdata, &temp32, 4); buffdata += 4;
585 /* the actual header */
586 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.type);
587 memcpy(buffdata, &temp32, 4); buffdata += 4;
588 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.fcc_handler);
589 memcpy(buffdata, &temp32, 4); buffdata += 4;
590 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.flags);
591 memcpy(buffdata, &temp32, 4); buffdata += 4;
592 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.priority);
593 memcpy(buffdata, &temp32, 4); buffdata += 4;
594 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.init_frames);
595 memcpy(buffdata, &temp32, 4); buffdata += 4;
596 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.scale);
597 memcpy(buffdata, &temp32, 4); buffdata += 4;
598 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.rate);
599 memcpy(buffdata, &temp32, 4); buffdata += 4;
600 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.start);
601 memcpy(buffdata, &temp32, 4); buffdata += 4;
602 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.length);
603 memcpy(buffdata, &temp32, 4); buffdata += 4;
604 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.bufsize);
605 memcpy(buffdata, &temp32, 4); buffdata += 4;
606 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.quality);
607 memcpy(buffdata, &temp32, 4); buffdata += 4;
608 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.samplesize);
609 memcpy(buffdata, &temp32, 4); buffdata += 4;
610 /* the audio header */
611 memcpy(buffdata, "strf", 4); buffdata += 4;
612 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strf_vids));
613 memcpy(buffdata, &temp32, 4); buffdata += 4;
614 /* the actual header */
615 temp16 = LE_FROM_GUINT16(avimux->auds.format);
616 memcpy(buffdata, &temp16, 2); buffdata += 2;
617 temp16 = LE_FROM_GUINT16(avimux->auds.channels);
618 memcpy(buffdata, &temp16, 2); buffdata += 2;
619 temp32 = LE_FROM_GUINT32(avimux->auds.rate);
620 memcpy(buffdata, &temp32, 4); buffdata += 4;
621 temp32 = LE_FROM_GUINT32(avimux->auds.av_bps);
622 memcpy(buffdata, &temp32, 4); buffdata += 4;
623 temp16 = LE_FROM_GUINT16(avimux->auds.blockalign);
624 memcpy(buffdata, &temp16, 2); buffdata += 2;
625 temp16 = LE_FROM_GUINT16(avimux->auds.size);
626 memcpy(buffdata, &temp16, 2); buffdata += 2;
629 if (avimux->num_video_pads)
632 memcpy(buffdata, "LIST", 4); buffdata += 4;
633 temp32 = LE_FROM_GUINT32(sizeof(guint32)+4*3);
634 memcpy(buffdata, &temp32, 4); buffdata += 4;
635 memcpy(buffdata, "odml", 4); buffdata += 4;
636 memcpy(buffdata, "dmlh", 4); buffdata += 4;
637 temp32 = LE_FROM_GUINT32(sizeof(guint32));
638 memcpy(buffdata, &temp32, 4); buffdata += 4;
639 temp32 = LE_FROM_GUINT32(avimux->total_frames);
640 memcpy(buffdata, &temp32, 4); buffdata += 4;
643 /* avi data header */
644 memcpy(buffdata, "LIST", 4); buffdata += 4;
645 temp32 = LE_FROM_GUINT32(avimux->data_size);
646 memcpy(buffdata, &temp32, 4); buffdata += 4;
647 memcpy(buffdata, "movi", 4);
653 gst_avimux_riff_get_avix_header (guint32 datax_size)
659 buffer = gst_buffer_new();
660 GST_BUFFER_SIZE(buffer) = 24;
661 buffdata = GST_BUFFER_DATA(buffer) = g_malloc(GST_BUFFER_SIZE(buffer));
663 memcpy(buffdata, "LIST", 4); buffdata += 4;
664 temp32 = LE_FROM_GUINT32(datax_size+4*4);
665 memcpy(buffdata, &temp32, 4); buffdata += 4;
666 memcpy(buffdata, "AVIX", 4); buffdata += 4;
667 memcpy(buffdata, "LIST", 4); buffdata += 4;
668 temp32 = LE_FROM_GUINT32(datax_size);
669 memcpy(buffdata, &temp32, 4); buffdata += 4;
670 memcpy(buffdata, "movi", 4);
676 gst_avimux_riff_get_video_header (guint32 video_frame_size)
681 buffer = gst_buffer_new();
682 GST_BUFFER_DATA(buffer) = g_malloc(8);
683 GST_BUFFER_SIZE(buffer) = 8;
684 memcpy(GST_BUFFER_DATA(buffer), "00db", 4);
685 temp32 = LE_FROM_GUINT32(video_frame_size);
686 memcpy(GST_BUFFER_DATA(buffer)+4, &temp32, 4);
692 gst_avimux_riff_get_audio_header (guint32 audio_sample_size)
697 buffer = gst_buffer_new();
698 GST_BUFFER_DATA(buffer) = g_malloc(8);
699 GST_BUFFER_SIZE(buffer) = 8;
700 memcpy(GST_BUFFER_DATA(buffer), "01wb", 4);
701 temp32 = LE_FROM_GUINT32(audio_sample_size);
702 memcpy(GST_BUFFER_DATA(buffer)+4, &temp32, 4);
707 /* some other usable functions (thankyou xawtv ;-) ) */
710 gst_avimux_add_index (GstAviMux *avimux, guint32 fourcc, guint32 flags, guint32 size)
714 if (avimux->idx_index == avimux->idx_count)
716 avimux->idx_count += 256;
717 avimux->idx = realloc(avimux->idx, avimux->idx_count*sizeof(gst_riff_index_entry));
719 temp32 = LE_FROM_GUINT32(fourcc);
720 memcpy(&(avimux->idx[avimux->idx_index].id), &temp32, 4);
721 avimux->idx[avimux->idx_index].flags = LE_FROM_GUINT32(flags);
722 avimux->idx[avimux->idx_index].offset = LE_FROM_GUINT32(avimux->idx_offset-avimux->header_size-8);
723 avimux->idx[avimux->idx_index].size = LE_FROM_GUINT32(size);
725 avimux->idx_offset += size + sizeof(gst_riff_index_entry);
729 gst_avimux_write_index (GstAviMux *avimux)
734 buffer = gst_buffer_new();
735 GST_BUFFER_SIZE(buffer) = 8;
736 GST_BUFFER_DATA(buffer) = g_malloc(8);
737 memcpy(GST_BUFFER_DATA(buffer), "idx1", 4);
738 temp32 = LE_FROM_GUINT32(avimux->idx_index * sizeof(gst_riff_index_entry));
739 memcpy(GST_BUFFER_DATA(buffer)+4, &temp32, 4);
740 gst_pad_push(avimux->srcpad, buffer);
742 buffer = gst_buffer_new();
743 GST_BUFFER_SIZE(buffer) = avimux->idx_index * sizeof(gst_riff_index_entry);
744 GST_BUFFER_DATA(buffer) = (unsigned char*) avimux->idx;
745 avimux->idx = NULL; /* will be free()'ed by gst_buffer_unref() */
746 avimux->total_data += GST_BUFFER_SIZE(buffer);
747 gst_pad_push(avimux->srcpad, buffer);
749 avimux->idx_size += avimux->idx_index * sizeof(gst_riff_index_entry) + 8;
752 avimux->avi_hdr.flags |= GST_RIFF_AVIH_HASINDEX;
756 gst_avimux_bigfile(GstAviMux *avimux, gboolean last)
761 if (avimux->is_bigfile)
764 event = gst_event_new_seek(GST_SEEK_BYTEOFFSET_SET, avimux->avix_start, TRUE);
765 gst_pad_send_event(avimux->srcpad, event);
767 /* rewrite AVIX header */
768 header = gst_avimux_riff_get_avix_header(avimux->datax_size);
769 gst_pad_push(avimux->srcpad, header);
771 /* go back to current location */
772 event = gst_event_new_seek(GST_SEEK_BYTEOFFSET_SET, avimux->total_data, TRUE);
773 gst_pad_send_event(avimux->srcpad, event);
775 avimux->avix_start = avimux->total_data;
780 avimux->is_bigfile = TRUE;
781 avimux->numx_frames = 0;
782 avimux->datax_size = 0;
784 header = gst_avimux_riff_get_avix_header(0);
785 avimux->total_data += GST_BUFFER_SIZE(header);
786 gst_pad_push(avimux->srcpad, header);
789 /* enough header blabla now, let's go on to actually writing the headers */
792 gst_avimux_start_file (GstAviMux *avimux)
796 avimux->total_data = 0;
797 avimux->total_frames = 0;
798 avimux->data_size = 4; /* ? */
799 avimux->datax_size = 0;
800 avimux->num_frames = 0;
801 avimux->numx_frames = 0;
802 avimux->audio_size = 0;
804 avimux->idx_index = 0;
805 avimux->idx_offset = avimux->header_size + 12;
806 avimux->idx_size = 0;
807 avimux->idx_count = 0;
811 avimux->avi_hdr.streams = avimux->num_video_pads + avimux->num_audio_pads;
812 avimux->is_bigfile = FALSE;
814 header = gst_avimux_riff_get_avi_header(avimux);
815 avimux->total_data += GST_BUFFER_SIZE(header);
816 gst_pad_push(avimux->srcpad, header);
818 avimux->write_header = FALSE;
819 avimux->restart = FALSE;
823 gst_avimux_stop_file (GstAviMux *avimux)
829 /* if bigfile, rewrite header, else write indexes */
830 if (avimux->num_video_pads)
832 if (avimux->is_bigfile)
834 gst_avimux_bigfile(avimux, TRUE);
835 avimux->idx_size = 0;
839 gst_avimux_write_index(avimux);
843 /* statistics/total_frames/... */
844 avimux->avi_hdr.tot_frames = avimux->num_frames;
845 if (avimux->num_video_pads)
846 avimux->vids_hdr.length = avimux->num_frames;
847 if (avimux->num_audio_pads)
848 avimux->auds_hdr.length = avimux->audio_size/avimux->auds_hdr.scale;
850 /* TODO: fps calculation!! */
851 avimux->avi_hdr.us_frame = 1000000/fps;
852 avimux->avi_hdr.max_bps = 0;
853 if (avimux->num_audio_pads)
854 avimux->avi_hdr.max_bps += avimux->auds.av_bps;
855 if (avimux->num_video_pads)
856 avimux->avi_hdr.max_bps += avimux->vids.bit_cnt/8 * fps;
858 /* seek and rewrite the header */
859 header = gst_avimux_riff_get_avi_header(avimux);
860 event = gst_event_new_seek(GST_SEEK_BYTEOFFSET_SET, 0, TRUE);
861 gst_pad_send_event(avimux->srcpad, event);
862 gst_pad_push(avimux->srcpad, header);
864 avimux->write_header = TRUE;
868 gst_avimux_restart_file (GstAviMux *avimux)
872 gst_avimux_stop_file(avimux);
874 event = gst_event_new(GST_EVENT_NEW_MEDIA);
875 gst_pad_send_event(avimux->srcpad, event);
877 gst_avimux_start_file(avimux);
880 /* handle events (search) */
882 gst_avimux_handle_event (GstPad *pad, GstEvent *event)
887 avimux = GST_AVIMUX (gst_pad_get_parent (pad));
889 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
892 case GST_EVENT_NEW_MEDIA:
893 avimux->restart = TRUE;
903 gst_avimux_chain (GstPad *pad, GstBuffer *buf)
907 const gchar *padname = gst_pad_get_name (pad);
909 g_return_if_fail (pad != NULL);
910 g_return_if_fail (GST_IS_PAD (pad));
911 g_return_if_fail (buf != NULL);
912 g_return_if_fail (GST_BUFFER_DATA (buf) != NULL);
914 avimux = GST_AVIMUX (gst_pad_get_parent (pad));
916 if (GST_IS_EVENT(buf))
918 gst_avimux_handle_event(pad, GST_EVENT(buf));
923 if (avimux->write_header)
924 gst_avimux_start_file(avimux);
926 if (strncmp(padname, "audio_", 6) == 0)
928 printf("AUDIODATA GIVEN\n");
929 /* write a audio header + index entry */
930 newbuf = gst_avimux_riff_get_audio_header(GST_BUFFER_SIZE(buf));
931 avimux->total_data += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
933 if (avimux->is_bigfile)
935 avimux->datax_size += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
939 avimux->data_size += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
940 avimux->audio_size += GST_BUFFER_SIZE(buf);
941 gst_avimux_add_index(avimux, avimux->auds.format, 0x0, GST_BUFFER_SIZE(buf));
944 gst_pad_push(avimux->srcpad, newbuf);
946 else if (strncmp(padname, "video_", 6) == 0)
949 gst_avimux_restart_file(avimux);
951 /* write a video header + index entry */
952 GST_BUFFER_SIZE(buf) = (GST_BUFFER_SIZE(buf)+3)&~3;
954 if ((avimux->is_bigfile?avimux->datax_size:avimux->data_size)+GST_BUFFER_SIZE(buf)>1024*1024*2000)
956 if (avimux->enable_large_avi)
957 gst_avimux_bigfile(avimux, FALSE);
959 gst_avimux_restart_file(avimux);
962 newbuf = gst_avimux_riff_get_video_header(GST_BUFFER_SIZE(buf));
963 avimux->total_data += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
964 avimux->total_frames++;
966 if (avimux->is_bigfile)
968 avimux->datax_size += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
969 avimux->numx_frames++;
973 avimux->data_size += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
974 avimux->num_frames++;
975 gst_avimux_add_index(avimux, avimux->vids.compression, 0x12, GST_BUFFER_SIZE(buf));
978 gst_pad_push(avimux->srcpad, newbuf);
982 g_warning("Unknown padname \'%s\'\n", padname);
987 gst_pad_push(avimux->srcpad, buf);
991 gst_avimux_get_property (GObject *object,
998 /* it's not null if we got it, but it might not be ours */
999 g_return_if_fail(GST_IS_AVIMUX(object));
1000 avimux = GST_AVIMUX(object);
1005 g_value_set_boolean(value, avimux->enable_large_avi);
1008 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1014 gst_avimux_set_property (GObject *object,
1016 const GValue *value,
1021 /* it's not null if we got it, but it might not be ours */
1022 g_return_if_fail(GST_IS_AVIMUX(object));
1023 avimux = GST_AVIMUX(object);
1028 avimux->enable_large_avi = g_value_get_boolean(value);
1031 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1036 static GstElementStateReturn
1037 gst_avimux_change_state (GstElement *element)
1040 gint transition = GST_STATE_TRANSITION (element);
1042 /* TODO: timer (for fps calculations) */
1044 g_return_val_if_fail(GST_IS_AVIMUX(element), GST_STATE_FAILURE);
1046 avimux = GST_AVIMUX(element);
1048 switch (transition) {
1049 case GST_STATE_READY_TO_PAUSED:
1050 /*gst_avimux_start_file(avimux);*/
1052 case GST_STATE_PAUSED_TO_PLAYING:
1054 case GST_STATE_PLAYING_TO_PAUSED:
1055 gst_avimux_stop_file(avimux);
1057 case GST_STATE_PAUSED_TO_READY:
1058 /*gst_avimux_stop_file(avimux);*/
1062 if (GST_ELEMENT_CLASS (parent_class)->change_state)
1063 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
1065 return GST_STATE_SUCCESS;
1069 plugin_init (GModule *module, GstPlugin *plugin)
1071 GstElementFactory *factory;
1073 /* this filter needs the riff parser */
1075 if (!gst_library_load ("gstriff")) {
1076 gst_info ("avimux: could not load support library: 'gstriff'\n");
1081 /* create an elementfactory for the avimux element */
1082 factory = gst_elementfactory_new ("avimux", GST_TYPE_AVIMUX,
1083 &gst_avimux_details);
1084 g_return_val_if_fail (factory != NULL, FALSE);
1086 gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (src_factory));
1087 gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (audio_sink_factory));
1088 gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (video_sink_factory));
1090 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
1095 GstPluginDesc plugin_desc = {