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_PAD_TEMPLATE_FACTORY (src_factory,
79 GST_PAD_TEMPLATE_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_PAD_TEMPLATE_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,25.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_PAD_TEMPLATE_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", 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"))
297 gst_caps_get_string (caps, "format", &format);
299 if (!strncmp (format, "strf_vids", 9)) {
300 avimux->vids.size = sizeof(gst_riff_strf_vids);
302 "width", &avimux->vids.width,
303 "height", &avimux->vids.height,
304 "planes", &avimux->vids.planes,
305 "bit_cnt", &avimux->vids.bit_cnt,
306 "compression", &avimux->vids.compression,
307 "image_size", &avimux->vids.image_size,
308 "xpels_meter", &avimux->vids.xpels_meter,
309 "ypels_meter", &avimux->vids.ypels_meter,
310 "num_colors", &avimux->vids.num_colors,
311 "imp_colors", &avimux->vids.imp_colors,
314 else if (!strncmp (format, "strf_auds", 9)) {
316 "format", &avimux->auds.format,
317 "channels", &avimux->auds.channels,
318 "rate", &avimux->auds.rate,
319 "av_bps", &avimux->auds.av_bps,
320 "blockalign", &avimux->auds.blockalign,
321 "size", &avimux->auds.size,
326 else if (!strcmp (mimetype, "video/raw"))
331 gst_caps_get_fourcc_int (caps, "format", &format);
334 case GST_MAKE_FOURCC('Y','U','Y','2'):
335 case GST_MAKE_FOURCC('I','4','2','0'):
336 case GST_MAKE_FOURCC('Y','4','1','P'):
337 case GST_MAKE_FOURCC('R','G','B',' '):
338 avimux->vids.size = sizeof(gst_riff_strf_vids);
339 gst_caps_get (caps, "width", &avimux->vids.width,
340 "height", &avimux->vids.height, NULL);
341 avimux->vids.planes = 1;
344 case GST_MAKE_FOURCC('Y','U','Y','2'):
345 avimux->vids.bit_cnt = 16; /* YUY2 */
347 case GST_MAKE_FOURCC('R','G','B',' '):
348 gst_caps_get_int (caps, "bpp", &temp); /* RGB */
349 avimux->vids.bit_cnt = temp;
351 case GST_MAKE_FOURCC('Y','4','1','P'):
352 case GST_MAKE_FOURCC('I','4','2','0'):
353 avimux->vids.bit_cnt = 12; /* Y41P or I420 */
356 gst_caps_get_fourcc_int(caps, "format", &avimux->vids.compression);
357 avimux->vids.image_size = avimux->vids.height * avimux->vids.width;
363 else if (!strcmp (mimetype, "video/jpeg"))
365 avimux->vids.size = sizeof(gst_riff_strf_vids);
366 gst_caps_get (caps, "width", &avimux->vids.width,
367 "height", &avimux->vids.height, NULL);
368 avimux->vids.planes = 1;
369 avimux->vids.bit_cnt = 24;
370 avimux->vids.compression = GST_MAKE_FOURCC('M','J','P','G');
371 avimux->vids.image_size = avimux->vids.height * avimux->vids.width;
374 else if (!strcmp (mimetype, "audio/raw"))
378 avimux->auds.format = GST_RIFF_WAVE_FORMAT_PCM;
379 gst_caps_get (caps, "channels", &avimux->auds.channels,
380 "rate", &avimux->auds.rate,
382 "depth", &avimux->auds.size,
384 avimux->auds.av_bps = width * avimux->auds.rate * avimux->auds.channels / 8;
385 avimux->auds.blockalign = width * avimux->auds.channels/8;
388 else if (!strcmp (mimetype, "audio/mp3"))
392 gst_caps_get_int(caps, "layer", &layer);
394 /* we don't need to do anything here, compressed mp3 contains it all */
395 avimux->auds.format = (layer == 3?
396 GST_RIFF_WAVE_FORMAT_MPEGL3 :
397 GST_RIFF_WAVE_FORMAT_MPEGL12);
401 return GST_PAD_CONNECT_REFUSED;
404 return GST_PAD_CONNECT_OK;
408 gst_avimux_request_new_pad (GstElement *element,
409 GstPadTemplate *templ,
410 const gchar *req_name)
416 g_return_val_if_fail (templ != NULL, NULL);
418 if (templ->direction != GST_PAD_SINK) {
419 g_warning ("avimux: request pad that is not a SINK pad\n");
423 g_return_val_if_fail (GST_IS_AVIMUX (element), NULL);
425 avimux = GST_AVIMUX (element);
427 if (templ == GST_PAD_TEMPLATE_GET (audio_sink_factory)) {
428 g_return_val_if_fail(avimux->num_audio_pads == 0 /*< MAX_NUM_AUDIO_PADS*/, NULL);
429 name = g_strdup_printf ("audio_%02d", avimux->num_audio_pads);
430 newpad = gst_pad_new_from_template (templ, name);
431 gst_pad_set_element_private (newpad, GINT_TO_POINTER (avimux->num_audio_pads));
433 avimux->audiosinkpad[avimux->num_audio_pads] = newpad;
434 avimux->num_audio_pads++;
436 else if (templ == GST_PAD_TEMPLATE_GET (video_sink_factory)) {
437 g_return_val_if_fail(avimux->num_video_pads == 0 /*< MAX_NUM_VIDEO_PADS*/, NULL);
438 name = g_strdup_printf ("video_%02d", avimux->num_video_pads);
439 newpad = gst_pad_new_from_template (templ, name);
440 gst_pad_set_element_private (newpad, GINT_TO_POINTER (avimux->num_video_pads));
442 avimux->videosinkpad[avimux->num_video_pads] = newpad;
443 avimux->num_video_pads++;
446 g_warning ("avimux: this is not our template!\n");
450 gst_pad_set_chain_function (newpad, gst_avimux_chain);
451 gst_pad_set_connect_function (newpad, gst_avimux_sinkconnect);
452 gst_element_add_pad (element, newpad);
457 /* maybe some of these functions should be moved to riff.h? */
459 /* DISCLAIMER: this function is ugly. So be it (i.e. it makes the rest easier) */
462 gst_avimux_riff_get_avi_header (GstAviMux *avimux)
469 buffer = gst_buffer_new();
471 /* first, let's see what actually needs to be in the buffer */
472 GST_BUFFER_SIZE(buffer) = 0;
473 GST_BUFFER_SIZE(buffer) += 32 + sizeof(gst_riff_avih); /* avi header */
474 if (avimux->num_video_pads)
475 { /* we have video */
476 GST_BUFFER_SIZE(buffer) += 28 + sizeof(gst_riff_strh) + sizeof(gst_riff_strf_vids); /* vid hdr */
477 GST_BUFFER_SIZE(buffer) += 24; /* odml header */
479 if (avimux->num_audio_pads)
480 { /* we have audio */
481 GST_BUFFER_SIZE(buffer) += 28 + sizeof(gst_riff_strh) + sizeof(gst_riff_strf_auds); /* aud hdr */
483 /* this is the "riff size" */
484 avimux->header_size = GST_BUFFER_SIZE(buffer);
485 GST_BUFFER_SIZE(buffer) += 12; /* avi data header */
487 /* allocate the buffer */
488 buffdata = GST_BUFFER_DATA(buffer) = g_malloc(GST_BUFFER_SIZE(buffer));
490 /* avi header metadata */
491 memcpy(buffdata, "RIFF", 4); buffdata += 4;
492 temp32 = LE_FROM_GUINT32(avimux->header_size + avimux->idx_size + avimux->data_size);
493 memcpy(buffdata, &temp32, 4); buffdata += 4;
494 memcpy(buffdata, "AVI ", 4); buffdata += 4;
495 memcpy(buffdata, "LIST", 4); buffdata += 4;
496 temp32 = LE_FROM_GUINT32(avimux->header_size - 4*5);
497 memcpy(buffdata, &temp32, 4); buffdata += 4;
498 memcpy(buffdata, "hdrl", 4); buffdata += 4;
499 memcpy(buffdata, "avih", 4); buffdata += 4;
500 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_avih));
501 memcpy(buffdata, &temp32, 4); buffdata += 4;
502 /* the AVI header itself */
503 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.us_frame);
504 memcpy(buffdata, &temp32, 4); buffdata += 4;
505 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.max_bps);
506 memcpy(buffdata, &temp32, 4); buffdata += 4;
507 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.pad_gran);
508 memcpy(buffdata, &temp32, 4); buffdata += 4;
509 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.flags);
510 memcpy(buffdata, &temp32, 4); buffdata += 4;
511 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.tot_frames);
512 memcpy(buffdata, &temp32, 4); buffdata += 4;
513 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.init_frames);
514 memcpy(buffdata, &temp32, 4); buffdata += 4;
515 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.streams);
516 memcpy(buffdata, &temp32, 4); buffdata += 4;
517 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.bufsize);
518 memcpy(buffdata, &temp32, 4); buffdata += 4;
519 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.width);
520 memcpy(buffdata, &temp32, 4); buffdata += 4;
521 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.height);
522 memcpy(buffdata, &temp32, 4); buffdata += 4;
523 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.scale);
524 memcpy(buffdata, &temp32, 4); buffdata += 4;
525 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.rate);
526 memcpy(buffdata, &temp32, 4); buffdata += 4;
527 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.start);
528 memcpy(buffdata, &temp32, 4); buffdata += 4;
529 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.length);
530 memcpy(buffdata, &temp32, 4); buffdata += 4;
532 if (avimux->num_video_pads)
534 /* video header metadata */
535 memcpy(buffdata, "LIST", 4); buffdata += 4;
536 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh) + sizeof(gst_riff_strf_vids) + 4*5);
537 memcpy(buffdata, &temp32, 4); buffdata += 4;
538 memcpy(buffdata, "strl", 4); buffdata += 4;
540 memcpy(buffdata, "strh", 4); buffdata += 4;
541 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh));
542 memcpy(buffdata, &temp32, 4); buffdata += 4;
543 /* the actual header */
544 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.type);
545 memcpy(buffdata, &temp32, 4); buffdata += 4;
546 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.fcc_handler);
547 memcpy(buffdata, &temp32, 4); buffdata += 4;
548 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.flags);
549 memcpy(buffdata, &temp32, 4); buffdata += 4;
550 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.priority);
551 memcpy(buffdata, &temp32, 4); buffdata += 4;
552 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.init_frames);
553 memcpy(buffdata, &temp32, 4); buffdata += 4;
554 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.scale);
555 memcpy(buffdata, &temp32, 4); buffdata += 4;
556 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.rate);
557 memcpy(buffdata, &temp32, 4); buffdata += 4;
558 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.start);
559 memcpy(buffdata, &temp32, 4); buffdata += 4;
560 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.length);
561 memcpy(buffdata, &temp32, 4); buffdata += 4;
562 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.bufsize);
563 memcpy(buffdata, &temp32, 4); buffdata += 4;
564 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.quality);
565 memcpy(buffdata, &temp32, 4); buffdata += 4;
566 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.samplesize);
567 memcpy(buffdata, &temp32, 4); buffdata += 4;
568 /* the video header */
569 memcpy(buffdata, "strf", 4); buffdata += 4;
570 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strf_vids));
571 memcpy(buffdata, &temp32, 4); buffdata += 4;
572 /* the actual header */
573 temp32 = LE_FROM_GUINT32(avimux->vids.size);
574 memcpy(buffdata, &temp32, 4); buffdata += 4;
575 temp32 = LE_FROM_GUINT32(avimux->vids.width);
576 memcpy(buffdata, &temp32, 4); buffdata += 4;
577 temp32 = LE_FROM_GUINT32(avimux->vids.height);
578 memcpy(buffdata, &temp32, 4); buffdata += 4;
579 temp16 = LE_FROM_GUINT16(avimux->vids.planes);
580 memcpy(buffdata, &temp16, 2); buffdata += 2;
581 temp16 = LE_FROM_GUINT16(avimux->vids.bit_cnt);
582 memcpy(buffdata, &temp16, 2); buffdata += 2;
583 temp32 = LE_FROM_GUINT32(avimux->vids.compression);
584 memcpy(buffdata, &temp32, 4); buffdata += 4;
585 temp32 = LE_FROM_GUINT32(avimux->vids.image_size);
586 memcpy(buffdata, &temp32, 4); buffdata += 4;
587 temp32 = LE_FROM_GUINT32(avimux->vids.xpels_meter);
588 memcpy(buffdata, &temp32, 4); buffdata += 4;
589 temp32 = LE_FROM_GUINT32(avimux->vids.ypels_meter);
590 memcpy(buffdata, &temp32, 4); buffdata += 4;
591 temp32 = LE_FROM_GUINT32(avimux->vids.num_colors);
592 memcpy(buffdata, &temp32, 4); buffdata += 4;
593 temp32 = LE_FROM_GUINT32(avimux->vids.imp_colors);
594 memcpy(buffdata, &temp32, 4); buffdata += 4;
597 if (avimux->num_audio_pads)
600 memcpy(buffdata, "LIST", 4); buffdata += 4;
601 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh) + sizeof(gst_riff_strf_auds) + 4*5);
602 memcpy(buffdata, &temp32, 4); buffdata += 4;
603 memcpy(buffdata, "strl", 4); buffdata += 4;
605 memcpy(buffdata, "strh", 4); buffdata += 4;
606 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh));
607 memcpy(buffdata, &temp32, 4); buffdata += 4;
608 /* the actual header */
609 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.type);
610 memcpy(buffdata, &temp32, 4); buffdata += 4;
611 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.fcc_handler);
612 memcpy(buffdata, &temp32, 4); buffdata += 4;
613 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.flags);
614 memcpy(buffdata, &temp32, 4); buffdata += 4;
615 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.priority);
616 memcpy(buffdata, &temp32, 4); buffdata += 4;
617 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.init_frames);
618 memcpy(buffdata, &temp32, 4); buffdata += 4;
619 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.scale);
620 memcpy(buffdata, &temp32, 4); buffdata += 4;
621 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.rate);
622 memcpy(buffdata, &temp32, 4); buffdata += 4;
623 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.start);
624 memcpy(buffdata, &temp32, 4); buffdata += 4;
625 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.length);
626 memcpy(buffdata, &temp32, 4); buffdata += 4;
627 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.bufsize);
628 memcpy(buffdata, &temp32, 4); buffdata += 4;
629 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.quality);
630 memcpy(buffdata, &temp32, 4); buffdata += 4;
631 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.samplesize);
632 memcpy(buffdata, &temp32, 4); buffdata += 4;
633 /* the audio header */
634 memcpy(buffdata, "strf", 4); buffdata += 4;
635 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strf_vids));
636 memcpy(buffdata, &temp32, 4); buffdata += 4;
637 /* the actual header */
638 temp16 = LE_FROM_GUINT16(avimux->auds.format);
639 memcpy(buffdata, &temp16, 2); buffdata += 2;
640 temp16 = LE_FROM_GUINT16(avimux->auds.channels);
641 memcpy(buffdata, &temp16, 2); buffdata += 2;
642 temp32 = LE_FROM_GUINT32(avimux->auds.rate);
643 memcpy(buffdata, &temp32, 4); buffdata += 4;
644 temp32 = LE_FROM_GUINT32(avimux->auds.av_bps);
645 memcpy(buffdata, &temp32, 4); buffdata += 4;
646 temp16 = LE_FROM_GUINT16(avimux->auds.blockalign);
647 memcpy(buffdata, &temp16, 2); buffdata += 2;
648 temp16 = LE_FROM_GUINT16(avimux->auds.size);
649 memcpy(buffdata, &temp16, 2); buffdata += 2;
652 if (avimux->num_video_pads)
655 memcpy(buffdata, "LIST", 4); buffdata += 4;
656 temp32 = LE_FROM_GUINT32(sizeof(guint32)+4*3);
657 memcpy(buffdata, &temp32, 4); buffdata += 4;
658 memcpy(buffdata, "odml", 4); buffdata += 4;
659 memcpy(buffdata, "dmlh", 4); buffdata += 4;
660 temp32 = LE_FROM_GUINT32(sizeof(guint32));
661 memcpy(buffdata, &temp32, 4); buffdata += 4;
662 temp32 = LE_FROM_GUINT32(avimux->total_frames);
663 memcpy(buffdata, &temp32, 4); buffdata += 4;
666 /* avi data header */
667 memcpy(buffdata, "LIST", 4); buffdata += 4;
668 temp32 = LE_FROM_GUINT32(avimux->data_size);
669 memcpy(buffdata, &temp32, 4); buffdata += 4;
670 memcpy(buffdata, "movi", 4);
676 gst_avimux_riff_get_avix_header (guint32 datax_size)
682 buffer = gst_buffer_new();
683 GST_BUFFER_SIZE(buffer) = 24;
684 buffdata = GST_BUFFER_DATA(buffer) = g_malloc(GST_BUFFER_SIZE(buffer));
686 memcpy(buffdata, "LIST", 4); buffdata += 4;
687 temp32 = LE_FROM_GUINT32(datax_size+4*4);
688 memcpy(buffdata, &temp32, 4); buffdata += 4;
689 memcpy(buffdata, "AVIX", 4); buffdata += 4;
690 memcpy(buffdata, "LIST", 4); buffdata += 4;
691 temp32 = LE_FROM_GUINT32(datax_size);
692 memcpy(buffdata, &temp32, 4); buffdata += 4;
693 memcpy(buffdata, "movi", 4);
699 gst_avimux_riff_get_video_header (guint32 video_frame_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), "00db", 4);
708 temp32 = LE_FROM_GUINT32(video_frame_size);
709 memcpy(GST_BUFFER_DATA(buffer)+4, &temp32, 4);
715 gst_avimux_riff_get_audio_header (guint32 audio_sample_size)
720 buffer = gst_buffer_new();
721 GST_BUFFER_DATA(buffer) = g_malloc(8);
722 GST_BUFFER_SIZE(buffer) = 8;
723 memcpy(GST_BUFFER_DATA(buffer), "01wb", 4);
724 temp32 = LE_FROM_GUINT32(audio_sample_size);
725 memcpy(GST_BUFFER_DATA(buffer)+4, &temp32, 4);
730 /* some other usable functions (thankyou xawtv ;-) ) */
733 gst_avimux_add_index (GstAviMux *avimux, guint32 fourcc, guint32 flags, guint32 size)
737 if (avimux->idx_index == avimux->idx_count)
739 avimux->idx_count += 256;
740 avimux->idx = realloc(avimux->idx, avimux->idx_count*sizeof(gst_riff_index_entry));
742 temp32 = LE_FROM_GUINT32(fourcc);
743 memcpy(&(avimux->idx[avimux->idx_index].id), &temp32, 4);
744 avimux->idx[avimux->idx_index].flags = LE_FROM_GUINT32(flags);
745 avimux->idx[avimux->idx_index].offset = LE_FROM_GUINT32(avimux->idx_offset-avimux->header_size-8);
746 avimux->idx[avimux->idx_index].size = LE_FROM_GUINT32(size);
748 avimux->idx_offset += size + sizeof(gst_riff_index_entry);
752 gst_avimux_write_index (GstAviMux *avimux)
757 buffer = gst_buffer_new();
758 GST_BUFFER_SIZE(buffer) = 8;
759 GST_BUFFER_DATA(buffer) = g_malloc(8);
760 memcpy(GST_BUFFER_DATA(buffer), "idx1", 4);
761 temp32 = LE_FROM_GUINT32(avimux->idx_index * sizeof(gst_riff_index_entry));
762 memcpy(GST_BUFFER_DATA(buffer)+4, &temp32, 4);
763 gst_pad_push(avimux->srcpad, buffer);
765 buffer = gst_buffer_new();
766 GST_BUFFER_SIZE(buffer) = avimux->idx_index * sizeof(gst_riff_index_entry);
767 GST_BUFFER_DATA(buffer) = (unsigned char*) avimux->idx;
768 avimux->idx = NULL; /* will be free()'ed by gst_buffer_unref() */
769 avimux->total_data += GST_BUFFER_SIZE(buffer);
770 gst_pad_push(avimux->srcpad, buffer);
772 avimux->idx_size += avimux->idx_index * sizeof(gst_riff_index_entry) + 8;
775 avimux->avi_hdr.flags |= GST_RIFF_AVIH_HASINDEX;
779 gst_avimux_bigfile(GstAviMux *avimux, gboolean last)
784 if (avimux->is_bigfile)
787 event = gst_event_new_seek (GST_FORMAT_BYTES |
788 GST_SEEK_METHOD_SET |
791 /* if the event succeeds */
792 if (gst_pad_send_event(avimux->srcpad, event)) {
794 /* rewrite AVIX header */
795 header = gst_avimux_riff_get_avix_header(avimux->datax_size);
796 gst_pad_push(avimux->srcpad, header);
798 /* go back to current location */
799 event = gst_event_new_seek (GST_FORMAT_BYTES |
800 GST_SEEK_METHOD_SET |
803 gst_pad_send_event(avimux->srcpad, event);
806 avimux->avix_start = avimux->total_data;
811 avimux->is_bigfile = TRUE;
812 avimux->numx_frames = 0;
813 avimux->datax_size = 0;
815 header = gst_avimux_riff_get_avix_header(0);
816 avimux->total_data += GST_BUFFER_SIZE(header);
817 gst_pad_push(avimux->srcpad, header);
820 /* enough header blabla now, let's go on to actually writing the headers */
823 gst_avimux_start_file (GstAviMux *avimux)
827 avimux->total_data = 0;
828 avimux->total_frames = 0;
829 avimux->data_size = 4; /* ? */
830 avimux->datax_size = 0;
831 avimux->num_frames = 0;
832 avimux->numx_frames = 0;
833 avimux->audio_size = 0;
835 avimux->idx_index = 0;
836 avimux->idx_offset = avimux->header_size + 12;
837 avimux->idx_size = 0;
838 avimux->idx_count = 0;
842 avimux->avi_hdr.streams = avimux->num_video_pads + avimux->num_audio_pads;
843 avimux->is_bigfile = FALSE;
845 header = gst_avimux_riff_get_avi_header(avimux);
846 avimux->total_data += GST_BUFFER_SIZE(header);
847 gst_pad_push(avimux->srcpad, header);
849 avimux->write_header = FALSE;
850 avimux->restart = FALSE;
854 gst_avimux_stop_file (GstAviMux *avimux)
859 /* if bigfile, rewrite header, else write indexes */
860 if (avimux->num_video_pads)
862 if (avimux->is_bigfile)
864 gst_avimux_bigfile(avimux, TRUE);
865 avimux->idx_size = 0;
869 gst_avimux_write_index(avimux);
873 /* statistics/total_frames/... */
874 avimux->avi_hdr.tot_frames = avimux->num_frames;
875 if (avimux->num_video_pads)
876 avimux->vids_hdr.length = avimux->num_frames;
877 if (avimux->num_audio_pads)
878 avimux->auds_hdr.length = avimux->audio_size/avimux->auds_hdr.scale;
880 /* TODO: fps calculation!! */
881 avimux->avi_hdr.us_frame = 1000000/avimux->framerate;
882 avimux->avi_hdr.max_bps = 0;
883 if (avimux->num_audio_pads)
884 avimux->avi_hdr.max_bps += avimux->auds.av_bps;
885 if (avimux->num_video_pads)
886 avimux->avi_hdr.max_bps += avimux->vids.bit_cnt/8 * avimux->framerate;
888 /* seek and rewrite the header */
889 header = gst_avimux_riff_get_avi_header(avimux);
890 event = gst_event_new_seek (GST_FORMAT_BYTES |
891 GST_SEEK_METHOD_SET |
892 GST_SEEK_FLAG_FLUSH, 0);
893 gst_pad_send_event(avimux->srcpad, event);
894 gst_pad_push(avimux->srcpad, header);
896 avimux->write_header = TRUE;
900 gst_avimux_restart_file (GstAviMux *avimux)
904 gst_avimux_stop_file(avimux);
906 event = gst_event_new(GST_EVENT_NEW_MEDIA);
907 gst_pad_send_event(avimux->srcpad, event);
909 gst_avimux_start_file(avimux);
912 /* handle events (search) */
914 gst_avimux_handle_event (GstPad *pad, GstEvent *event)
919 avimux = GST_AVIMUX (gst_pad_get_parent (pad));
921 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
924 case GST_EVENT_NEW_MEDIA:
925 avimux->restart = TRUE;
935 gst_avimux_chain (GstPad *pad, GstBuffer *buf)
939 const gchar *padname = gst_pad_get_name (pad);
941 g_return_if_fail (pad != NULL);
942 g_return_if_fail (GST_IS_PAD (pad));
943 g_return_if_fail (buf != NULL);
944 g_return_if_fail (GST_BUFFER_DATA (buf) != NULL);
946 avimux = GST_AVIMUX (gst_pad_get_parent (pad));
948 if (GST_IS_EVENT(buf))
950 gst_avimux_handle_event(pad, GST_EVENT(buf));
955 if (avimux->write_header)
956 gst_avimux_start_file(avimux);
958 if (strncmp(padname, "audio_", 6) == 0)
960 /* write a audio header + index entry */
961 newbuf = gst_avimux_riff_get_audio_header(GST_BUFFER_SIZE(buf));
962 avimux->total_data += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
964 if (avimux->is_bigfile)
966 avimux->datax_size += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
970 avimux->data_size += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
971 avimux->audio_size += GST_BUFFER_SIZE(buf);
972 gst_avimux_add_index(avimux, avimux->auds.format, 0x0, GST_BUFFER_SIZE(buf));
975 gst_pad_push(avimux->srcpad, newbuf);
977 else if (strncmp(padname, "video_", 6) == 0)
980 gst_avimux_restart_file(avimux);
982 /* write a video header + index entry */
983 GST_BUFFER_SIZE(buf) = (GST_BUFFER_SIZE(buf)+3)&~3;
985 if ((avimux->is_bigfile?avimux->datax_size:avimux->data_size)+GST_BUFFER_SIZE(buf)>1024*1024*2000)
987 if (avimux->enable_large_avi)
988 gst_avimux_bigfile(avimux, FALSE);
990 gst_avimux_restart_file(avimux);
993 newbuf = gst_avimux_riff_get_video_header(GST_BUFFER_SIZE(buf));
994 avimux->total_data += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
995 avimux->total_frames++;
997 if (avimux->is_bigfile)
999 avimux->datax_size += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
1000 avimux->numx_frames++;
1004 avimux->data_size += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
1005 avimux->num_frames++;
1006 gst_avimux_add_index(avimux, avimux->vids.compression, 0x12, GST_BUFFER_SIZE(buf));
1009 gst_pad_push(avimux->srcpad, newbuf);
1013 g_warning("Unknown padname \'%s\'\n", padname);
1018 gst_pad_push(avimux->srcpad, buf);
1022 gst_avimux_get_property (GObject *object,
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 g_value_set_boolean(value, avimux->enable_large_avi);
1039 g_value_set_double(value, avimux->framerate);
1042 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1048 gst_avimux_set_property (GObject *object,
1050 const GValue *value,
1055 /* it's not null if we got it, but it might not be ours */
1056 g_return_if_fail(GST_IS_AVIMUX(object));
1057 avimux = GST_AVIMUX(object);
1062 avimux->enable_large_avi = g_value_get_boolean(value);
1065 avimux->framerate = g_value_get_double(value);
1068 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1073 static GstElementStateReturn
1074 gst_avimux_change_state (GstElement *element)
1077 gint transition = GST_STATE_TRANSITION (element);
1079 /* TODO: timer (for fps calculations) */
1081 g_return_val_if_fail(GST_IS_AVIMUX(element), GST_STATE_FAILURE);
1083 avimux = GST_AVIMUX(element);
1085 switch (transition) {
1086 case GST_STATE_READY_TO_PAUSED:
1087 /*gst_avimux_start_file(avimux);*/
1089 case GST_STATE_PAUSED_TO_PLAYING:
1091 case GST_STATE_PLAYING_TO_PAUSED:
1092 gst_avimux_stop_file(avimux);
1094 case GST_STATE_PAUSED_TO_READY:
1095 /*gst_avimux_stop_file(avimux);*/
1099 if (GST_ELEMENT_CLASS (parent_class)->change_state)
1100 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
1102 return GST_STATE_SUCCESS;
1106 plugin_init (GModule *module, GstPlugin *plugin)
1108 GstElementFactory *factory;
1110 /* this filter needs the riff parser */
1112 if (!gst_library_load ("gstriff")) {
1113 gst_info ("avimux: could not load support library: 'gstriff'\n");
1118 /* create an elementfactory for the avimux element */
1119 factory = gst_element_factory_new ("avimux", GST_TYPE_AVIMUX,
1120 &gst_avimux_details);
1121 g_return_val_if_fail (factory != NULL, FALSE);
1123 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (src_factory));
1124 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (audio_sink_factory));
1125 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (video_sink_factory));
1127 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
1132 GstPluginDesc plugin_desc = {