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 */
68 GST_PADTEMPLATE_FACTORY (src_factory,
79 GST_PADTEMPLATE_FACTORY (video_sink_factory,
86 "format", GST_PROPS_STRING ("strf_vids")
91 "format", GST_PROPS_LIST (
92 GST_PROPS_FOURCC (GST_MAKE_FOURCC('Y','U','Y','2')),
93 GST_PROPS_FOURCC (GST_MAKE_FOURCC('I','4','2','0')),
94 GST_PROPS_FOURCC (GST_MAKE_FOURCC('Y','4','1','P'))
96 "width", GST_PROPS_INT_RANGE (16, 4096),
97 "height", GST_PROPS_INT_RANGE (16, 4096)
102 "format", GST_PROPS_FOURCC (GST_MAKE_FOURCC('R','G','B',' ')),
103 "width", GST_PROPS_INT_RANGE (16, 4096),
104 "height", GST_PROPS_INT_RANGE (16, 4096),
105 "depth", GST_PROPS_LIST(
111 "bpp", GST_PROPS_LIST(
121 "width", GST_PROPS_INT_RANGE (16, 4096),
122 "height", GST_PROPS_INT_RANGE (16, 4096)
126 GST_PADTEMPLATE_FACTORY (audio_sink_factory,
133 "format", GST_PROPS_STRING ("strf_auds")
138 "format", GST_PROPS_STRING ("int"),
139 "law", GST_PROPS_INT (0),
140 "endianness", GST_PROPS_INT (G_BYTE_ORDER),
141 "signed", GST_PROPS_LIST (
142 GST_PROPS_BOOLEAN (TRUE),
143 GST_PROPS_BOOLEAN (FALSE)
145 "width", GST_PROPS_LIST (
149 "depth", GST_PROPS_LIST (
153 "rate", GST_PROPS_INT_RANGE (11025, 44100),
154 "channels", GST_PROPS_INT_RANGE (1, 2)
164 static void gst_avimux_class_init (GstAviMuxClass *klass);
165 static void gst_avimux_init (GstAviMux *avimux);
167 static void gst_avimux_chain (GstPad *pad,
169 static gboolean gst_avimux_handle_event (GstPad *pad,
171 static GstPad* gst_avimux_request_new_pad (GstElement *element,
172 GstPadTemplate *templ,
174 static void gst_avimux_set_property (GObject *object,
178 static void gst_avimux_get_property (GObject *object,
182 static GstElementStateReturn gst_avimux_change_state (GstElement *element);
184 static GstElementClass *parent_class = NULL;
185 /*static guint gst_avimux_signals[LAST_SIGNAL] = { 0 }; */
188 gst_avimux_get_type (void)
190 static GType avimux_type = 0;
193 static const GTypeInfo avimux_info = {
194 sizeof(GstAviMuxClass),
197 (GClassInitFunc)gst_avimux_class_init,
202 (GInstanceInitFunc)gst_avimux_init,
204 avimux_type = g_type_register_static(GST_TYPE_ELEMENT, "GstAviMux", &avimux_info, 0);
210 gst_avimux_class_init (GstAviMuxClass *klass)
212 GObjectClass *gobject_class;
213 GstElementClass *gstelement_class;
215 gobject_class = (GObjectClass*)klass;
216 gstelement_class = (GstElementClass*)klass;
218 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
220 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_BIGFILE,
221 g_param_spec_boolean("bigfile","Bigfile Support","Whether to capture large or small AVI files",
222 0,G_PARAM_READWRITE));
224 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_FRAMERATE,
225 g_param_spec_double("framerate","Framerate","Frames/sec",
226 G_MINDOUBLE,G_MAXDOUBLE,0,G_PARAM_READWRITE));
228 gstelement_class->request_new_pad = gst_avimux_request_new_pad;
230 gstelement_class->change_state = gst_avimux_change_state;
232 gstelement_class->get_property = gst_avimux_get_property;
233 gstelement_class->set_property = gst_avimux_set_property;
237 gst_avimux_init (GstAviMux *avimux)
240 avimux->srcpad = gst_pad_new_from_template (
241 GST_PADTEMPLATE_GET (src_factory), "src");
242 gst_element_add_pad (GST_ELEMENT (avimux), avimux->srcpad);
244 GST_FLAG_SET (GST_ELEMENT(avimux), GST_ELEMENT_EVENT_AWARE);
245 gst_pad_set_event_function(avimux->srcpad, gst_avimux_handle_event);
247 for (i=0;i<MAX_NUM_AUDIO_PADS;i++)
248 avimux->audiosinkpad[i] = NULL;
249 avimux->num_audio_pads = 0;
250 for (i=0;i<MAX_NUM_VIDEO_PADS;i++)
251 avimux->videosinkpad[i] = NULL;
252 avimux->num_video_pads = 0;
254 avimux->num_frames = 0;
256 /* audio/video/AVI header initialisation */
257 memset(&(avimux->avi_hdr),0,sizeof(gst_riff_avih));
258 memset(&(avimux->vids_hdr),0,sizeof(gst_riff_strh));
259 memset(&(avimux->vids),0,sizeof(gst_riff_strf_vids));
260 memset(&(avimux->auds_hdr),0,sizeof(gst_riff_strh));
261 memset(&(avimux->auds),0,sizeof(gst_riff_strf_auds));
262 avimux->vids_hdr.type = GST_MAKE_FOURCC('v','i','d','s');
263 avimux->vids_hdr.rate = 1000000;
264 avimux->auds_hdr.type = GST_MAKE_FOURCC('a','u','d','s');
268 avimux->write_header = TRUE;
270 avimux->enable_large_avi = TRUE;
272 avimux->framerate = 25.;
275 static GstPadConnectReturn
276 gst_avimux_sinkconnect (GstPad *pad, GstCaps *vscaps)
281 avimux = GST_AVIMUX (gst_pad_get_parent (pad));
283 /* we are not going to act on variable caps */
284 if (!GST_CAPS_IS_FIXED (vscaps))
285 return GST_PAD_CONNECT_DELAYED;
287 GST_DEBUG (0, "avimux: sinkconnect triggered on %s\n", gst_pad_get_name (pad));
289 for (caps = vscaps; caps != NULL; caps = vscaps = vscaps->next)
291 const gchar* mimetype = gst_caps_get_mime(caps);
293 if (!strcmp (mimetype, "video/avi"))
295 const gchar* format = gst_caps_get_string(caps, "format");
297 if (!strncmp (format, "strf_vids", 9)) {
298 avimux->vids.size = sizeof(gst_riff_strf_vids);
299 avimux->vids.width = gst_caps_get_int (caps, "width");
300 avimux->vids.height = gst_caps_get_int (caps, "height");
301 avimux->vids.planes = gst_caps_get_int (caps, "planes");
302 avimux->vids.bit_cnt = gst_caps_get_int (caps, "bit_cnt");
303 avimux->vids.compression = gst_caps_get_fourcc_int (caps, "compression");
304 avimux->vids.image_size = gst_caps_get_int (caps, "image_size");
305 avimux->vids.xpels_meter = gst_caps_get_int (caps, "xpels_meter");
306 avimux->vids.ypels_meter = gst_caps_get_int (caps, "ypels_meter");
307 avimux->vids.num_colors = gst_caps_get_int (caps, "num_colors");
308 avimux->vids.imp_colors = gst_caps_get_int (caps, "imp_colors");
310 else if (!strncmp (format, "strf_auds", 9)) {
311 avimux->auds.format = gst_caps_get_int (caps, "format");
312 avimux->auds.channels = gst_caps_get_int (caps, "channels");
313 avimux->auds.rate = gst_caps_get_int (caps, "rate");
314 avimux->auds.av_bps = gst_caps_get_int (caps, "av_bps");
315 avimux->auds.blockalign = gst_caps_get_int (caps, "blockalign");
316 avimux->auds.size = gst_caps_get_int (caps, "size");
320 else if (!strcmp (mimetype, "video/raw"))
322 switch (gst_caps_get_fourcc_int(caps, "format"))
324 case GST_MAKE_FOURCC('Y','U','Y','2'):
325 case GST_MAKE_FOURCC('I','4','2','0'):
326 case GST_MAKE_FOURCC('Y','4','1','P'):
327 case GST_MAKE_FOURCC('R','G','B',' '):
328 avimux->vids.size = sizeof(gst_riff_strf_vids);
329 avimux->vids.width = gst_caps_get_int (caps, "width");
330 avimux->vids.height = gst_caps_get_int (caps, "height");
331 avimux->vids.planes = 1;
332 switch (gst_caps_get_fourcc_int(caps, "format"))
334 case GST_MAKE_FOURCC('Y','U','Y','2'):
335 avimux->vids.bit_cnt = 16; /* YUY2 */
337 case GST_MAKE_FOURCC('R','G','B',' '):
338 avimux->vids.bit_cnt = gst_caps_get_fourcc_int(caps, "bpp"); /* RGB */
340 case GST_MAKE_FOURCC('Y','4','1','P'):
341 case GST_MAKE_FOURCC('I','4','2','0'):
342 avimux->vids.bit_cnt = 12; /* Y41P or I420 */
345 avimux->vids.compression = gst_caps_get_fourcc_int(caps, "format");
346 avimux->vids.image_size = avimux->vids.height * avimux->vids.width;
352 else if (!strcmp (mimetype, "video/jpeg"))
354 avimux->vids.size = sizeof(gst_riff_strf_vids);
355 avimux->vids.width = gst_caps_get_int (caps, "width");
356 avimux->vids.height = gst_caps_get_int (caps, "height");
357 avimux->vids.planes = 1;
358 avimux->vids.bit_cnt = 24;
359 avimux->vids.compression = GST_MAKE_FOURCC('M','J','P','G');
360 avimux->vids.image_size = avimux->vids.height * avimux->vids.width;
363 else if (!strcmp (mimetype, "audio/raw"))
365 printf("WE HAVE AUDIO\n");
366 avimux->auds.format = GST_RIFF_WAVE_FORMAT_PCM;
367 avimux->auds.channels = gst_caps_get_int (caps, "channels");
368 avimux->auds.rate = gst_caps_get_int (caps, "rate");
369 avimux->auds.av_bps = gst_caps_get_int (caps, "width") * avimux->auds.rate *
370 avimux->auds.channels / 8;
371 avimux->auds.blockalign = gst_caps_get_int (caps, "width") * avimux->auds.channels/8;
372 avimux->auds.size = gst_caps_get_int (caps, "depth");
375 else if (!strcmp (mimetype, "audio/mp3"))
377 /* we don't need to do anything here, compressed mp3 contains it all */
378 avimux->auds.format = gst_caps_get_int(caps, "layer")==3?
379 GST_RIFF_WAVE_FORMAT_MPEGL3:GST_RIFF_WAVE_FORMAT_MPEGL12;
383 return GST_PAD_CONNECT_REFUSED;
386 return GST_PAD_CONNECT_OK;
390 gst_avimux_request_new_pad (GstElement *element,
391 GstPadTemplate *templ,
392 const gchar *req_name)
398 g_return_val_if_fail (templ != NULL, NULL);
400 if (templ->direction != GST_PAD_SINK) {
401 g_warning ("avimux: request pad that is not a SINK pad\n");
405 g_return_val_if_fail (GST_IS_AVIMUX (element), NULL);
407 avimux = GST_AVIMUX (element);
409 if (templ == GST_PADTEMPLATE_GET (audio_sink_factory)) {
410 printf("AUDIOPAD REQUESTED\n");
411 g_return_val_if_fail(avimux->num_audio_pads == 0 /*< MAX_NUM_AUDIO_PADS*/, NULL);
412 name = g_strdup_printf ("audio_%02d", avimux->num_audio_pads);
413 newpad = gst_pad_new_from_template (templ, name);
414 gst_pad_set_element_private (newpad, GINT_TO_POINTER (avimux->num_audio_pads));
416 avimux->audiosinkpad[avimux->num_audio_pads] = newpad;
417 avimux->num_audio_pads++;
419 else if (templ == GST_PADTEMPLATE_GET (video_sink_factory)) {
420 printf("VIDEOPAD %d REQUESTED\n", avimux->num_video_pads);
421 g_return_val_if_fail(avimux->num_video_pads == 0 /*< MAX_NUM_VIDEO_PADS*/, NULL);
422 name = g_strdup_printf ("video_%02d", avimux->num_video_pads);
423 newpad = gst_pad_new_from_template (templ, name);
424 gst_pad_set_element_private (newpad, GINT_TO_POINTER (avimux->num_video_pads));
426 avimux->videosinkpad[avimux->num_video_pads] = newpad;
427 avimux->num_video_pads++;
430 g_warning ("avimux: this is not our template!\n");
434 gst_pad_set_chain_function (newpad, gst_avimux_chain);
435 gst_pad_set_connect_function (newpad, gst_avimux_sinkconnect);
436 gst_element_add_pad (element, newpad);
441 /* maybe some of these functions should be moved to riff.h? */
443 /* DISCLAIMER: this function is ugly. So be it (i.e. it makes the rest easier) */
446 gst_avimux_riff_get_avi_header (GstAviMux *avimux)
453 buffer = gst_buffer_new();
455 /* first, let's see what actually needs to be in the buffer */
456 GST_BUFFER_SIZE(buffer) = 0;
457 GST_BUFFER_SIZE(buffer) += 32 + sizeof(gst_riff_avih); /* avi header */
458 if (avimux->num_video_pads)
459 { /* we have video */
460 GST_BUFFER_SIZE(buffer) += 28 + sizeof(gst_riff_strh) + sizeof(gst_riff_strf_vids); /* vid hdr */
461 GST_BUFFER_SIZE(buffer) += 24; /* odml header */
463 if (avimux->num_audio_pads)
464 { /* we have audio */
465 GST_BUFFER_SIZE(buffer) += 28 + sizeof(gst_riff_strh) + sizeof(gst_riff_strf_auds); /* aud hdr */
467 /* this is the "riff size" */
468 avimux->header_size = GST_BUFFER_SIZE(buffer);
469 GST_BUFFER_SIZE(buffer) += 12; /* avi data header */
471 /* allocate the buffer */
472 buffdata = GST_BUFFER_DATA(buffer) = g_malloc(GST_BUFFER_SIZE(buffer));
474 /* avi header metadata */
475 memcpy(buffdata, "RIFF", 4); buffdata += 4;
476 temp32 = LE_FROM_GUINT32(avimux->header_size + avimux->idx_size + avimux->data_size);
477 memcpy(buffdata, &temp32, 4); buffdata += 4;
478 memcpy(buffdata, "AVI ", 4); buffdata += 4;
479 memcpy(buffdata, "LIST", 4); buffdata += 4;
480 temp32 = LE_FROM_GUINT32(avimux->header_size - 4*5);
481 memcpy(buffdata, &temp32, 4); buffdata += 4;
482 memcpy(buffdata, "hdrl", 4); buffdata += 4;
483 memcpy(buffdata, "avih", 4); buffdata += 4;
484 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_avih));
485 memcpy(buffdata, &temp32, 4); buffdata += 4;
486 /* the AVI header itself */
487 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.us_frame);
488 memcpy(buffdata, &temp32, 4); buffdata += 4;
489 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.max_bps);
490 memcpy(buffdata, &temp32, 4); buffdata += 4;
491 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.pad_gran);
492 memcpy(buffdata, &temp32, 4); buffdata += 4;
493 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.flags);
494 memcpy(buffdata, &temp32, 4); buffdata += 4;
495 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.tot_frames);
496 memcpy(buffdata, &temp32, 4); buffdata += 4;
497 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.init_frames);
498 memcpy(buffdata, &temp32, 4); buffdata += 4;
499 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.streams);
500 memcpy(buffdata, &temp32, 4); buffdata += 4;
501 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.bufsize);
502 memcpy(buffdata, &temp32, 4); buffdata += 4;
503 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.width);
504 memcpy(buffdata, &temp32, 4); buffdata += 4;
505 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.height);
506 memcpy(buffdata, &temp32, 4); buffdata += 4;
507 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.scale);
508 memcpy(buffdata, &temp32, 4); buffdata += 4;
509 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.rate);
510 memcpy(buffdata, &temp32, 4); buffdata += 4;
511 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.start);
512 memcpy(buffdata, &temp32, 4); buffdata += 4;
513 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.length);
514 memcpy(buffdata, &temp32, 4); buffdata += 4;
516 if (avimux->num_video_pads)
518 /* video header metadata */
519 memcpy(buffdata, "LIST", 4); buffdata += 4;
520 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh) + sizeof(gst_riff_strf_vids) + 4*5);
521 memcpy(buffdata, &temp32, 4); buffdata += 4;
522 memcpy(buffdata, "strl", 4); buffdata += 4;
524 memcpy(buffdata, "strh", 4); buffdata += 4;
525 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh));
526 memcpy(buffdata, &temp32, 4); buffdata += 4;
527 /* the actual header */
528 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.type);
529 memcpy(buffdata, &temp32, 4); buffdata += 4;
530 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.fcc_handler);
531 memcpy(buffdata, &temp32, 4); buffdata += 4;
532 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.flags);
533 memcpy(buffdata, &temp32, 4); buffdata += 4;
534 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.priority);
535 memcpy(buffdata, &temp32, 4); buffdata += 4;
536 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.init_frames);
537 memcpy(buffdata, &temp32, 4); buffdata += 4;
538 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.scale);
539 memcpy(buffdata, &temp32, 4); buffdata += 4;
540 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.rate);
541 memcpy(buffdata, &temp32, 4); buffdata += 4;
542 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.start);
543 memcpy(buffdata, &temp32, 4); buffdata += 4;
544 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.length);
545 memcpy(buffdata, &temp32, 4); buffdata += 4;
546 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.bufsize);
547 memcpy(buffdata, &temp32, 4); buffdata += 4;
548 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.quality);
549 memcpy(buffdata, &temp32, 4); buffdata += 4;
550 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.samplesize);
551 memcpy(buffdata, &temp32, 4); buffdata += 4;
552 /* the video header */
553 memcpy(buffdata, "strf", 4); buffdata += 4;
554 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strf_vids));
555 memcpy(buffdata, &temp32, 4); buffdata += 4;
556 /* the actual header */
557 temp32 = LE_FROM_GUINT32(avimux->vids.size);
558 memcpy(buffdata, &temp32, 4); buffdata += 4;
559 temp32 = LE_FROM_GUINT32(avimux->vids.width);
560 memcpy(buffdata, &temp32, 4); buffdata += 4;
561 temp32 = LE_FROM_GUINT32(avimux->vids.height);
562 memcpy(buffdata, &temp32, 4); buffdata += 4;
563 temp16 = LE_FROM_GUINT16(avimux->vids.planes);
564 memcpy(buffdata, &temp16, 2); buffdata += 2;
565 temp16 = LE_FROM_GUINT16(avimux->vids.bit_cnt);
566 memcpy(buffdata, &temp16, 2); buffdata += 2;
567 temp32 = LE_FROM_GUINT32(avimux->vids.compression);
568 memcpy(buffdata, &temp32, 4); buffdata += 4;
569 temp32 = LE_FROM_GUINT32(avimux->vids.image_size);
570 memcpy(buffdata, &temp32, 4); buffdata += 4;
571 temp32 = LE_FROM_GUINT32(avimux->vids.xpels_meter);
572 memcpy(buffdata, &temp32, 4); buffdata += 4;
573 temp32 = LE_FROM_GUINT32(avimux->vids.ypels_meter);
574 memcpy(buffdata, &temp32, 4); buffdata += 4;
575 temp32 = LE_FROM_GUINT32(avimux->vids.num_colors);
576 memcpy(buffdata, &temp32, 4); buffdata += 4;
577 temp32 = LE_FROM_GUINT32(avimux->vids.imp_colors);
578 memcpy(buffdata, &temp32, 4); buffdata += 4;
581 if (avimux->num_audio_pads)
584 memcpy(buffdata, "LIST", 4); buffdata += 4;
585 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh) + sizeof(gst_riff_strf_auds) + 4*5);
586 memcpy(buffdata, &temp32, 4); buffdata += 4;
587 memcpy(buffdata, "strl", 4); buffdata += 4;
589 memcpy(buffdata, "strh", 4); buffdata += 4;
590 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh));
591 memcpy(buffdata, &temp32, 4); buffdata += 4;
592 /* the actual header */
593 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.type);
594 memcpy(buffdata, &temp32, 4); buffdata += 4;
595 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.fcc_handler);
596 memcpy(buffdata, &temp32, 4); buffdata += 4;
597 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.flags);
598 memcpy(buffdata, &temp32, 4); buffdata += 4;
599 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.priority);
600 memcpy(buffdata, &temp32, 4); buffdata += 4;
601 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.init_frames);
602 memcpy(buffdata, &temp32, 4); buffdata += 4;
603 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.scale);
604 memcpy(buffdata, &temp32, 4); buffdata += 4;
605 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.rate);
606 memcpy(buffdata, &temp32, 4); buffdata += 4;
607 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.start);
608 memcpy(buffdata, &temp32, 4); buffdata += 4;
609 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.length);
610 memcpy(buffdata, &temp32, 4); buffdata += 4;
611 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.bufsize);
612 memcpy(buffdata, &temp32, 4); buffdata += 4;
613 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.quality);
614 memcpy(buffdata, &temp32, 4); buffdata += 4;
615 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.samplesize);
616 memcpy(buffdata, &temp32, 4); buffdata += 4;
617 /* the audio header */
618 memcpy(buffdata, "strf", 4); buffdata += 4;
619 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strf_vids));
620 memcpy(buffdata, &temp32, 4); buffdata += 4;
621 /* the actual header */
622 temp16 = LE_FROM_GUINT16(avimux->auds.format);
623 memcpy(buffdata, &temp16, 2); buffdata += 2;
624 temp16 = LE_FROM_GUINT16(avimux->auds.channels);
625 memcpy(buffdata, &temp16, 2); buffdata += 2;
626 temp32 = LE_FROM_GUINT32(avimux->auds.rate);
627 memcpy(buffdata, &temp32, 4); buffdata += 4;
628 temp32 = LE_FROM_GUINT32(avimux->auds.av_bps);
629 memcpy(buffdata, &temp32, 4); buffdata += 4;
630 temp16 = LE_FROM_GUINT16(avimux->auds.blockalign);
631 memcpy(buffdata, &temp16, 2); buffdata += 2;
632 temp16 = LE_FROM_GUINT16(avimux->auds.size);
633 memcpy(buffdata, &temp16, 2); buffdata += 2;
636 if (avimux->num_video_pads)
639 memcpy(buffdata, "LIST", 4); buffdata += 4;
640 temp32 = LE_FROM_GUINT32(sizeof(guint32)+4*3);
641 memcpy(buffdata, &temp32, 4); buffdata += 4;
642 memcpy(buffdata, "odml", 4); buffdata += 4;
643 memcpy(buffdata, "dmlh", 4); buffdata += 4;
644 temp32 = LE_FROM_GUINT32(sizeof(guint32));
645 memcpy(buffdata, &temp32, 4); buffdata += 4;
646 temp32 = LE_FROM_GUINT32(avimux->total_frames);
647 memcpy(buffdata, &temp32, 4); buffdata += 4;
650 /* avi data header */
651 memcpy(buffdata, "LIST", 4); buffdata += 4;
652 temp32 = LE_FROM_GUINT32(avimux->data_size);
653 memcpy(buffdata, &temp32, 4); buffdata += 4;
654 memcpy(buffdata, "movi", 4);
660 gst_avimux_riff_get_avix_header (guint32 datax_size)
666 buffer = gst_buffer_new();
667 GST_BUFFER_SIZE(buffer) = 24;
668 buffdata = GST_BUFFER_DATA(buffer) = g_malloc(GST_BUFFER_SIZE(buffer));
670 memcpy(buffdata, "LIST", 4); buffdata += 4;
671 temp32 = LE_FROM_GUINT32(datax_size+4*4);
672 memcpy(buffdata, &temp32, 4); buffdata += 4;
673 memcpy(buffdata, "AVIX", 4); buffdata += 4;
674 memcpy(buffdata, "LIST", 4); buffdata += 4;
675 temp32 = LE_FROM_GUINT32(datax_size);
676 memcpy(buffdata, &temp32, 4); buffdata += 4;
677 memcpy(buffdata, "movi", 4);
683 gst_avimux_riff_get_video_header (guint32 video_frame_size)
688 buffer = gst_buffer_new();
689 GST_BUFFER_DATA(buffer) = g_malloc(8);
690 GST_BUFFER_SIZE(buffer) = 8;
691 memcpy(GST_BUFFER_DATA(buffer), "00db", 4);
692 temp32 = LE_FROM_GUINT32(video_frame_size);
693 memcpy(GST_BUFFER_DATA(buffer)+4, &temp32, 4);
699 gst_avimux_riff_get_audio_header (guint32 audio_sample_size)
704 buffer = gst_buffer_new();
705 GST_BUFFER_DATA(buffer) = g_malloc(8);
706 GST_BUFFER_SIZE(buffer) = 8;
707 memcpy(GST_BUFFER_DATA(buffer), "01wb", 4);
708 temp32 = LE_FROM_GUINT32(audio_sample_size);
709 memcpy(GST_BUFFER_DATA(buffer)+4, &temp32, 4);
714 /* some other usable functions (thankyou xawtv ;-) ) */
717 gst_avimux_add_index (GstAviMux *avimux, guint32 fourcc, guint32 flags, guint32 size)
721 if (avimux->idx_index == avimux->idx_count)
723 avimux->idx_count += 256;
724 avimux->idx = realloc(avimux->idx, avimux->idx_count*sizeof(gst_riff_index_entry));
726 temp32 = LE_FROM_GUINT32(fourcc);
727 memcpy(&(avimux->idx[avimux->idx_index].id), &temp32, 4);
728 avimux->idx[avimux->idx_index].flags = LE_FROM_GUINT32(flags);
729 avimux->idx[avimux->idx_index].offset = LE_FROM_GUINT32(avimux->idx_offset-avimux->header_size-8);
730 avimux->idx[avimux->idx_index].size = LE_FROM_GUINT32(size);
732 avimux->idx_offset += size + sizeof(gst_riff_index_entry);
736 gst_avimux_write_index (GstAviMux *avimux)
741 buffer = gst_buffer_new();
742 GST_BUFFER_SIZE(buffer) = 8;
743 GST_BUFFER_DATA(buffer) = g_malloc(8);
744 memcpy(GST_BUFFER_DATA(buffer), "idx1", 4);
745 temp32 = LE_FROM_GUINT32(avimux->idx_index * sizeof(gst_riff_index_entry));
746 memcpy(GST_BUFFER_DATA(buffer)+4, &temp32, 4);
747 gst_pad_push(avimux->srcpad, buffer);
749 buffer = gst_buffer_new();
750 GST_BUFFER_SIZE(buffer) = avimux->idx_index * sizeof(gst_riff_index_entry);
751 GST_BUFFER_DATA(buffer) = (unsigned char*) avimux->idx;
752 avimux->idx = NULL; /* will be free()'ed by gst_buffer_unref() */
753 avimux->total_data += GST_BUFFER_SIZE(buffer);
754 gst_pad_push(avimux->srcpad, buffer);
756 avimux->idx_size += avimux->idx_index * sizeof(gst_riff_index_entry) + 8;
759 avimux->avi_hdr.flags |= GST_RIFF_AVIH_HASINDEX;
763 gst_avimux_bigfile(GstAviMux *avimux, gboolean last)
768 if (avimux->is_bigfile)
771 event = gst_event_new_seek(GST_SEEK_BYTEOFFSET_SET, avimux->avix_start, TRUE);
772 gst_pad_send_event(avimux->srcpad, event);
774 /* rewrite AVIX header */
775 header = gst_avimux_riff_get_avix_header(avimux->datax_size);
776 gst_pad_push(avimux->srcpad, header);
778 /* go back to current location */
779 event = gst_event_new_seek(GST_SEEK_BYTEOFFSET_SET, avimux->total_data, TRUE);
780 gst_pad_send_event(avimux->srcpad, event);
782 avimux->avix_start = avimux->total_data;
787 avimux->is_bigfile = TRUE;
788 avimux->numx_frames = 0;
789 avimux->datax_size = 0;
791 header = gst_avimux_riff_get_avix_header(0);
792 avimux->total_data += GST_BUFFER_SIZE(header);
793 gst_pad_push(avimux->srcpad, header);
796 /* enough header blabla now, let's go on to actually writing the headers */
799 gst_avimux_start_file (GstAviMux *avimux)
803 avimux->total_data = 0;
804 avimux->total_frames = 0;
805 avimux->data_size = 4; /* ? */
806 avimux->datax_size = 0;
807 avimux->num_frames = 0;
808 avimux->numx_frames = 0;
809 avimux->audio_size = 0;
811 avimux->idx_index = 0;
812 avimux->idx_offset = avimux->header_size + 12;
813 avimux->idx_size = 0;
814 avimux->idx_count = 0;
818 avimux->avi_hdr.streams = avimux->num_video_pads + avimux->num_audio_pads;
819 avimux->is_bigfile = FALSE;
821 header = gst_avimux_riff_get_avi_header(avimux);
822 avimux->total_data += GST_BUFFER_SIZE(header);
823 gst_pad_push(avimux->srcpad, header);
825 avimux->write_header = FALSE;
826 avimux->restart = FALSE;
830 gst_avimux_stop_file (GstAviMux *avimux)
835 /* if bigfile, rewrite header, else write indexes */
836 if (avimux->num_video_pads)
838 if (avimux->is_bigfile)
840 gst_avimux_bigfile(avimux, TRUE);
841 avimux->idx_size = 0;
845 gst_avimux_write_index(avimux);
849 /* statistics/total_frames/... */
850 avimux->avi_hdr.tot_frames = avimux->num_frames;
851 if (avimux->num_video_pads)
852 avimux->vids_hdr.length = avimux->num_frames;
853 if (avimux->num_audio_pads)
854 avimux->auds_hdr.length = avimux->audio_size/avimux->auds_hdr.scale;
856 /* TODO: fps calculation!! */
857 avimux->avi_hdr.us_frame = 1000000/avimux->framerate;
858 avimux->avi_hdr.max_bps = 0;
859 if (avimux->num_audio_pads)
860 avimux->avi_hdr.max_bps += avimux->auds.av_bps;
861 if (avimux->num_video_pads)
862 avimux->avi_hdr.max_bps += avimux->vids.bit_cnt/8 * avimux->framerate;
864 /* seek and rewrite the header */
865 header = gst_avimux_riff_get_avi_header(avimux);
866 event = gst_event_new_seek(GST_SEEK_BYTEOFFSET_SET, 0, TRUE);
867 gst_pad_send_event(avimux->srcpad, event);
868 gst_pad_push(avimux->srcpad, header);
870 avimux->write_header = TRUE;
874 gst_avimux_restart_file (GstAviMux *avimux)
878 gst_avimux_stop_file(avimux);
880 event = gst_event_new(GST_EVENT_NEW_MEDIA);
881 gst_pad_send_event(avimux->srcpad, event);
883 gst_avimux_start_file(avimux);
886 /* handle events (search) */
888 gst_avimux_handle_event (GstPad *pad, GstEvent *event)
893 avimux = GST_AVIMUX (gst_pad_get_parent (pad));
895 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
898 case GST_EVENT_NEW_MEDIA:
899 avimux->restart = TRUE;
909 gst_avimux_chain (GstPad *pad, GstBuffer *buf)
913 const gchar *padname = gst_pad_get_name (pad);
915 g_return_if_fail (pad != NULL);
916 g_return_if_fail (GST_IS_PAD (pad));
917 g_return_if_fail (buf != NULL);
918 g_return_if_fail (GST_BUFFER_DATA (buf) != NULL);
920 avimux = GST_AVIMUX (gst_pad_get_parent (pad));
922 if (GST_IS_EVENT(buf))
924 gst_avimux_handle_event(pad, GST_EVENT(buf));
929 if (avimux->write_header)
930 gst_avimux_start_file(avimux);
932 if (strncmp(padname, "audio_", 6) == 0)
934 /* write a audio header + index entry */
935 newbuf = gst_avimux_riff_get_audio_header(GST_BUFFER_SIZE(buf));
936 avimux->total_data += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
938 if (avimux->is_bigfile)
940 avimux->datax_size += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
944 avimux->data_size += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
945 avimux->audio_size += GST_BUFFER_SIZE(buf);
946 gst_avimux_add_index(avimux, avimux->auds.format, 0x0, GST_BUFFER_SIZE(buf));
949 gst_pad_push(avimux->srcpad, newbuf);
951 else if (strncmp(padname, "video_", 6) == 0)
954 gst_avimux_restart_file(avimux);
956 /* write a video header + index entry */
957 GST_BUFFER_SIZE(buf) = (GST_BUFFER_SIZE(buf)+3)&~3;
959 if ((avimux->is_bigfile?avimux->datax_size:avimux->data_size)+GST_BUFFER_SIZE(buf)>1024*1024*2000)
961 if (avimux->enable_large_avi)
962 gst_avimux_bigfile(avimux, FALSE);
964 gst_avimux_restart_file(avimux);
967 newbuf = gst_avimux_riff_get_video_header(GST_BUFFER_SIZE(buf));
968 avimux->total_data += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
969 avimux->total_frames++;
971 if (avimux->is_bigfile)
973 avimux->datax_size += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
974 avimux->numx_frames++;
978 avimux->data_size += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
979 avimux->num_frames++;
980 gst_avimux_add_index(avimux, avimux->vids.compression, 0x12, GST_BUFFER_SIZE(buf));
983 gst_pad_push(avimux->srcpad, newbuf);
987 g_warning("Unknown padname \'%s\'\n", padname);
992 gst_pad_push(avimux->srcpad, buf);
996 gst_avimux_get_property (GObject *object,
1003 /* it's not null if we got it, but it might not be ours */
1004 g_return_if_fail(GST_IS_AVIMUX(object));
1005 avimux = GST_AVIMUX(object);
1010 g_value_set_boolean(value, avimux->enable_large_avi);
1013 g_value_set_double(value, avimux->framerate);
1016 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1022 gst_avimux_set_property (GObject *object,
1024 const GValue *value,
1029 /* it's not null if we got it, but it might not be ours */
1030 g_return_if_fail(GST_IS_AVIMUX(object));
1031 avimux = GST_AVIMUX(object);
1036 avimux->enable_large_avi = g_value_get_boolean(value);
1039 avimux->framerate = g_value_get_double(value);
1042 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1047 static GstElementStateReturn
1048 gst_avimux_change_state (GstElement *element)
1051 gint transition = GST_STATE_TRANSITION (element);
1053 /* TODO: timer (for fps calculations) */
1055 g_return_val_if_fail(GST_IS_AVIMUX(element), GST_STATE_FAILURE);
1057 avimux = GST_AVIMUX(element);
1059 switch (transition) {
1060 case GST_STATE_READY_TO_PAUSED:
1061 /*gst_avimux_start_file(avimux);*/
1063 case GST_STATE_PAUSED_TO_PLAYING:
1065 case GST_STATE_PLAYING_TO_PAUSED:
1066 gst_avimux_stop_file(avimux);
1068 case GST_STATE_PAUSED_TO_READY:
1069 /*gst_avimux_stop_file(avimux);*/
1073 if (GST_ELEMENT_CLASS (parent_class)->change_state)
1074 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
1076 return GST_STATE_SUCCESS;
1080 plugin_init (GModule *module, GstPlugin *plugin)
1082 GstElementFactory *factory;
1084 /* this filter needs the riff parser */
1086 if (!gst_library_load ("gstriff")) {
1087 gst_info ("avimux: could not load support library: 'gstriff'\n");
1092 /* create an elementfactory for the avimux element */
1093 factory = gst_elementfactory_new ("avimux", GST_TYPE_AVIMUX,
1094 &gst_avimux_details);
1095 g_return_val_if_fail (factory != NULL, FALSE);
1097 gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (src_factory));
1098 gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (audio_sink_factory));
1099 gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (video_sink_factory));
1101 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
1106 GstPluginDesc plugin_desc = {