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 <gst/video/video.h>
35 #include "gstavimux.h"
37 #ifndef LE_FROM_GUINT16
38 #define LE_FROM_GUINT16 GUINT16_FROM_LE
40 #ifndef LE_FROM_GUINT32
41 #define LE_FROM_GUINT32 GUINT32_FROM_LE
44 /* elementfactory information */
45 static GstElementDetails
51 "Muxes audio and video into an avi stream",
53 "Ronald Bultje <rbultje@ronald.bitfreak.net>",
57 /* 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 (1000, 48000),
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_loop (GstElement *element);
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;
231 static const GstEventMask *
232 gst_avimux_get_event_masks (GstPad *pad)
234 static const GstEventMask gst_avimux_src_event_masks[] = {
235 { GST_EVENT_NEW_MEDIA, 0 },
238 static const GstEventMask gst_avimux_sink_event_masks[] = {
239 { GST_EVENT_EOS, 0 },
243 if (GST_PAD_IS_SRC(pad))
244 return gst_avimux_src_event_masks;
246 return gst_avimux_sink_event_masks;
250 gst_avimux_init (GstAviMux *avimux)
252 avimux->srcpad = gst_pad_new_from_template (
253 GST_PAD_TEMPLATE_GET (src_factory), "src");
254 gst_element_add_pad (GST_ELEMENT (avimux), avimux->srcpad);
256 GST_FLAG_SET (GST_ELEMENT(avimux), GST_ELEMENT_EVENT_AWARE);
257 gst_pad_set_event_function(avimux->srcpad, gst_avimux_handle_event);
258 gst_pad_set_event_mask_function(avimux->srcpad, gst_avimux_get_event_masks);
260 avimux->audiosinkpad = NULL;
261 avimux->audio_pad_connected = FALSE;
262 avimux->videosinkpad = NULL;
263 avimux->video_pad_connected = FALSE;
265 avimux->audio_buffer_queue = NULL;
266 avimux->video_buffer_queue = NULL;
268 avimux->num_frames = 0;
270 /* audio/video/AVI header initialisation */
271 memset(&(avimux->avi_hdr),0,sizeof(gst_riff_avih));
272 memset(&(avimux->vids_hdr),0,sizeof(gst_riff_strh));
273 memset(&(avimux->vids),0,sizeof(gst_riff_strf_vids));
274 memset(&(avimux->auds_hdr),0,sizeof(gst_riff_strh));
275 memset(&(avimux->auds),0,sizeof(gst_riff_strf_auds));
276 avimux->vids_hdr.type = GST_MAKE_FOURCC('v','i','d','s');
277 avimux->vids_hdr.rate = 1000000;
278 avimux->auds_hdr.type = GST_MAKE_FOURCC('a','u','d','s');
282 avimux->write_header = TRUE;
284 avimux->enable_large_avi = TRUE;
286 avimux->framerate = 0;
288 gst_element_set_loop_function(GST_ELEMENT(avimux), gst_avimux_loop);
291 static GstPadConnectReturn
292 gst_avimux_sinkconnect (GstPad *pad, GstCaps *vscaps)
297 avimux = GST_AVIMUX (gst_pad_get_parent (pad));
299 /* we are not going to act on variable caps */
300 if (!GST_CAPS_IS_FIXED (vscaps))
301 return GST_PAD_CONNECT_DELAYED;
303 GST_DEBUG (0, "avimux: sinkconnect triggered on %s", gst_pad_get_name (pad));
305 for (caps = vscaps; caps != NULL; caps = vscaps = vscaps->next)
307 const gchar* mimetype = gst_caps_get_mime(caps);
309 if (!strcmp (mimetype, "video/avi"))
313 gst_caps_get_string (caps, "format", &format);
315 if (!strncmp (format, "strf_vids", 9)) {
316 avimux->vids.size = sizeof(gst_riff_strf_vids);
318 "width", &avimux->vids.width,
319 "height", &avimux->vids.height,
320 "planes", &avimux->vids.planes,
321 "bit_cnt", &avimux->vids.bit_cnt,
322 "compression", &avimux->vids.compression,
323 "image_size", &avimux->vids.image_size,
324 "xpels_meter", &avimux->vids.xpels_meter,
325 "ypels_meter", &avimux->vids.ypels_meter,
326 "num_colors", &avimux->vids.num_colors,
327 "imp_colors", &avimux->vids.imp_colors,
329 avimux->vids_hdr.fcc_handler = avimux->vids.compression;
330 avimux->avi_hdr.width = avimux->vids.width;
331 avimux->avi_hdr.height = avimux->vids.height;
334 else if (!strncmp (format, "strf_auds", 9)) {
336 "format", &avimux->auds.format,
337 "channels", &avimux->auds.channels,
338 "rate", &avimux->auds.rate,
339 "av_bps", &avimux->auds.av_bps,
340 "blockalign", &avimux->auds.blockalign,
341 "size", &avimux->auds.size,
343 avimux->auds_hdr.samplesize = avimux->auds_hdr.scale = avimux->auds.blockalign;
344 avimux->auds_hdr.rate = avimux->auds.av_bps;
348 else if (!strcmp (mimetype, "video/raw"))
353 gst_caps_get_fourcc_int (caps, "format", &format);
356 case GST_MAKE_FOURCC('Y','U','Y','2'):
357 case GST_MAKE_FOURCC('I','4','2','0'):
358 case GST_MAKE_FOURCC('Y','4','1','P'):
359 case GST_MAKE_FOURCC('R','G','B',' '):
360 avimux->vids.size = sizeof(gst_riff_strf_vids);
361 gst_caps_get (caps, "width", &avimux->vids.width,
362 "height", &avimux->vids.height, NULL);
363 avimux->vids.planes = 1;
366 case GST_MAKE_FOURCC('Y','U','Y','2'):
367 avimux->vids.bit_cnt = 16; /* YUY2 */
369 case GST_MAKE_FOURCC('R','G','B',' '):
370 gst_caps_get_int (caps, "bpp", &temp); /* RGB */
371 avimux->vids.bit_cnt = temp;
373 case GST_MAKE_FOURCC('Y','4','1','P'):
374 case GST_MAKE_FOURCC('I','4','2','0'):
375 avimux->vids.bit_cnt = 12; /* Y41P or I420 */
378 gst_caps_get_fourcc_int(caps, "format", &avimux->vids.compression);
379 avimux->vids_hdr.fcc_handler = avimux->vids.compression;
380 avimux->vids.image_size = avimux->vids.height * avimux->vids.width;
381 avimux->avi_hdr.width = avimux->vids.width;
382 avimux->avi_hdr.height = avimux->vids.height;
388 else if (!strcmp (mimetype, "video/jpeg"))
390 avimux->vids.size = sizeof(gst_riff_strf_vids);
391 gst_caps_get (caps, "width", &avimux->vids.width,
392 "height", &avimux->vids.height, NULL);
393 avimux->vids.planes = 1;
394 avimux->vids.bit_cnt = 24;
395 avimux->vids_hdr.fcc_handler = avimux->vids.compression = GST_MAKE_FOURCC('M','J','P','G');
396 avimux->avi_hdr.width = avimux->vids.width;
397 avimux->avi_hdr.height = avimux->vids.height;
398 avimux->vids.image_size = avimux->vids.height * avimux->vids.width;
401 else if (!strcmp (mimetype, "audio/raw"))
405 avimux->auds.format = GST_RIFF_WAVE_FORMAT_PCM;
406 gst_caps_get (caps, "channels", &avimux->auds.channels,
407 "rate", &avimux->auds.rate,
409 "depth", &avimux->auds.size,
411 avimux->auds_hdr.rate = avimux->auds.av_bps = width * avimux->auds.rate * avimux->auds.channels / 8;
412 avimux->auds_hdr.samplesize = avimux->auds_hdr.scale = avimux->auds.blockalign = width * avimux->auds.channels/8;
415 else if (!strcmp (mimetype, "audio/mp3"))
419 gst_caps_get_int(caps, "layer", &layer);
421 /* we don't need to do anything here, compressed mp3 contains it all */
422 avimux->auds.format = (layer == 3?
423 GST_RIFF_WAVE_FORMAT_MPEGL3 :
424 GST_RIFF_WAVE_FORMAT_MPEGL12);
428 return GST_PAD_CONNECT_REFUSED;
431 return GST_PAD_CONNECT_OK;
435 gst_avimux_pad_connect (GstPad *pad,
439 GstAviMux *avimux = GST_AVIMUX(data);
440 const gchar *padname = gst_pad_get_name (pad);
442 if (pad == avimux->audiosinkpad)
444 avimux->audio_pad_connected = TRUE;
446 else if (pad == avimux->videosinkpad)
448 avimux->video_pad_connected = TRUE;
452 g_warning("Unknown padname '%s'", padname);
456 GST_DEBUG(GST_CAT_PLUGIN_INFO, "pad '%s' connected", padname);
460 gst_avimux_pad_disconnect (GstPad *pad,
464 GstAviMux *avimux = GST_AVIMUX(data);
465 const gchar *padname = gst_pad_get_name (pad);
467 if (pad == avimux->audiosinkpad)
469 avimux->audio_pad_connected = FALSE;
470 avimux->audiosinkpad = NULL;
472 else if (pad == avimux->videosinkpad)
474 avimux->video_pad_connected = FALSE;
475 avimux->videosinkpad = NULL;
479 g_warning("Unknown padname '%s'", padname);
483 GST_DEBUG(GST_CAT_PLUGIN_INFO, "pad '%s' disconnected", padname);
485 /* is this allowed? */
486 gst_pad_destroy(pad);
490 gst_avimux_request_new_pad (GstElement *element,
491 GstPadTemplate *templ,
492 const gchar *req_name)
497 g_return_val_if_fail (templ != NULL, NULL);
499 if (templ->direction != GST_PAD_SINK) {
500 g_warning ("avimux: request pad that is not a SINK pad\n");
504 g_return_val_if_fail (GST_IS_AVIMUX (element), NULL);
506 avimux = GST_AVIMUX (element);
508 if (templ == GST_PAD_TEMPLATE_GET (audio_sink_factory)) {
509 g_return_val_if_fail(avimux->audiosinkpad == NULL, NULL);
510 newpad = gst_pad_new_from_template (templ, "audio_00");
511 avimux->audiosinkpad = newpad;
513 else if (templ == GST_PAD_TEMPLATE_GET (video_sink_factory)) {
514 g_return_val_if_fail(avimux->videosinkpad == NULL, NULL);
515 newpad = gst_pad_new_from_template (templ, "video_00");
516 avimux->videosinkpad = newpad;
519 g_warning ("avimux: this is not our template!\n");
523 g_signal_connect(newpad, "connected",
524 G_CALLBACK(gst_avimux_pad_connect), (gpointer)avimux);
525 g_signal_connect(newpad, "disconnected",
526 G_CALLBACK(gst_avimux_pad_disconnect), (gpointer)avimux);
527 gst_pad_set_connect_function (newpad, gst_avimux_sinkconnect);
528 gst_element_add_pad (element, newpad);
529 gst_pad_set_event_function(newpad, gst_avimux_handle_event);
530 gst_pad_set_event_mask_function(newpad, gst_avimux_get_event_masks);
535 /* maybe some of these functions should be moved to riff.h? */
537 /* DISCLAIMER: this function is ugly. So be it (i.e. it makes the rest easier) */
540 gst_avimux_riff_get_avi_header (GstAviMux *avimux)
547 buffer = gst_buffer_new();
549 /* first, let's see what actually needs to be in the buffer */
550 GST_BUFFER_SIZE(buffer) = 0;
551 GST_BUFFER_SIZE(buffer) += 32 + sizeof(gst_riff_avih); /* avi header */
552 if (avimux->video_pad_connected)
553 { /* we have video */
554 GST_BUFFER_SIZE(buffer) += 28 + sizeof(gst_riff_strh) + sizeof(gst_riff_strf_vids); /* vid hdr */
555 GST_BUFFER_SIZE(buffer) += 24; /* odml header */
557 if (avimux->audio_pad_connected)
558 { /* we have audio */
559 GST_BUFFER_SIZE(buffer) += 28 + sizeof(gst_riff_strh) + sizeof(gst_riff_strf_auds); /* aud hdr */
561 /* this is the "riff size" */
562 avimux->header_size = GST_BUFFER_SIZE(buffer);
563 GST_BUFFER_SIZE(buffer) += 12; /* avi data header */
565 /* allocate the buffer */
566 buffdata = GST_BUFFER_DATA(buffer) = g_malloc(GST_BUFFER_SIZE(buffer));
568 /* avi header metadata */
569 memcpy(buffdata, "RIFF", 4); buffdata += 4;
570 temp32 = LE_FROM_GUINT32(avimux->header_size + avimux->idx_size + avimux->data_size);
571 memcpy(buffdata, &temp32, 4); buffdata += 4;
572 memcpy(buffdata, "AVI ", 4); buffdata += 4;
573 memcpy(buffdata, "LIST", 4); buffdata += 4;
574 temp32 = LE_FROM_GUINT32(avimux->header_size - 4*5);
575 memcpy(buffdata, &temp32, 4); buffdata += 4;
576 memcpy(buffdata, "hdrl", 4); buffdata += 4;
577 memcpy(buffdata, "avih", 4); buffdata += 4;
578 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_avih));
579 memcpy(buffdata, &temp32, 4); buffdata += 4;
580 /* the AVI header itself */
581 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.us_frame);
582 memcpy(buffdata, &temp32, 4); buffdata += 4;
583 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.max_bps);
584 memcpy(buffdata, &temp32, 4); buffdata += 4;
585 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.pad_gran);
586 memcpy(buffdata, &temp32, 4); buffdata += 4;
587 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.flags);
588 memcpy(buffdata, &temp32, 4); buffdata += 4;
589 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.tot_frames);
590 memcpy(buffdata, &temp32, 4); buffdata += 4;
591 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.init_frames);
592 memcpy(buffdata, &temp32, 4); buffdata += 4;
593 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.streams);
594 memcpy(buffdata, &temp32, 4); buffdata += 4;
595 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.bufsize);
596 memcpy(buffdata, &temp32, 4); buffdata += 4;
597 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.width);
598 memcpy(buffdata, &temp32, 4); buffdata += 4;
599 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.height);
600 memcpy(buffdata, &temp32, 4); buffdata += 4;
601 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.scale);
602 memcpy(buffdata, &temp32, 4); buffdata += 4;
603 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.rate);
604 memcpy(buffdata, &temp32, 4); buffdata += 4;
605 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.start);
606 memcpy(buffdata, &temp32, 4); buffdata += 4;
607 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.length);
608 memcpy(buffdata, &temp32, 4); buffdata += 4;
610 if (avimux->video_pad_connected)
612 /* video header metadata */
613 memcpy(buffdata, "LIST", 4); buffdata += 4;
614 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh) + sizeof(gst_riff_strf_vids) + 4*5);
615 memcpy(buffdata, &temp32, 4); buffdata += 4;
616 memcpy(buffdata, "strl", 4); buffdata += 4;
618 memcpy(buffdata, "strh", 4); buffdata += 4;
619 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh));
620 memcpy(buffdata, &temp32, 4); buffdata += 4;
621 /* the actual header */
622 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.type);
623 memcpy(buffdata, &temp32, 4); buffdata += 4;
624 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.fcc_handler);
625 memcpy(buffdata, &temp32, 4); buffdata += 4;
626 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.flags);
627 memcpy(buffdata, &temp32, 4); buffdata += 4;
628 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.priority);
629 memcpy(buffdata, &temp32, 4); buffdata += 4;
630 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.init_frames);
631 memcpy(buffdata, &temp32, 4); buffdata += 4;
632 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.scale);
633 memcpy(buffdata, &temp32, 4); buffdata += 4;
634 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.rate);
635 memcpy(buffdata, &temp32, 4); buffdata += 4;
636 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.start);
637 memcpy(buffdata, &temp32, 4); buffdata += 4;
638 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.length);
639 memcpy(buffdata, &temp32, 4); buffdata += 4;
640 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.bufsize);
641 memcpy(buffdata, &temp32, 4); buffdata += 4;
642 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.quality);
643 memcpy(buffdata, &temp32, 4); buffdata += 4;
644 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.samplesize);
645 memcpy(buffdata, &temp32, 4); buffdata += 4;
646 /* the video header */
647 memcpy(buffdata, "strf", 4); buffdata += 4;
648 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strf_vids));
649 memcpy(buffdata, &temp32, 4); buffdata += 4;
650 /* the actual header */
651 temp32 = LE_FROM_GUINT32(avimux->vids.size);
652 memcpy(buffdata, &temp32, 4); buffdata += 4;
653 temp32 = LE_FROM_GUINT32(avimux->vids.width);
654 memcpy(buffdata, &temp32, 4); buffdata += 4;
655 temp32 = LE_FROM_GUINT32(avimux->vids.height);
656 memcpy(buffdata, &temp32, 4); buffdata += 4;
657 temp16 = LE_FROM_GUINT16(avimux->vids.planes);
658 memcpy(buffdata, &temp16, 2); buffdata += 2;
659 temp16 = LE_FROM_GUINT16(avimux->vids.bit_cnt);
660 memcpy(buffdata, &temp16, 2); buffdata += 2;
661 temp32 = LE_FROM_GUINT32(avimux->vids.compression);
662 memcpy(buffdata, &temp32, 4); buffdata += 4;
663 temp32 = LE_FROM_GUINT32(avimux->vids.image_size);
664 memcpy(buffdata, &temp32, 4); buffdata += 4;
665 temp32 = LE_FROM_GUINT32(avimux->vids.xpels_meter);
666 memcpy(buffdata, &temp32, 4); buffdata += 4;
667 temp32 = LE_FROM_GUINT32(avimux->vids.ypels_meter);
668 memcpy(buffdata, &temp32, 4); buffdata += 4;
669 temp32 = LE_FROM_GUINT32(avimux->vids.num_colors);
670 memcpy(buffdata, &temp32, 4); buffdata += 4;
671 temp32 = LE_FROM_GUINT32(avimux->vids.imp_colors);
672 memcpy(buffdata, &temp32, 4); buffdata += 4;
675 if (avimux->audio_pad_connected)
678 memcpy(buffdata, "LIST", 4); buffdata += 4;
679 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh) + sizeof(gst_riff_strf_auds) + 4*5);
680 memcpy(buffdata, &temp32, 4); buffdata += 4;
681 memcpy(buffdata, "strl", 4); buffdata += 4;
683 memcpy(buffdata, "strh", 4); buffdata += 4;
684 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh));
685 memcpy(buffdata, &temp32, 4); buffdata += 4;
686 /* the actual header */
687 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.type);
688 memcpy(buffdata, &temp32, 4); buffdata += 4;
689 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.fcc_handler);
690 memcpy(buffdata, &temp32, 4); buffdata += 4;
691 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.flags);
692 memcpy(buffdata, &temp32, 4); buffdata += 4;
693 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.priority);
694 memcpy(buffdata, &temp32, 4); buffdata += 4;
695 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.init_frames);
696 memcpy(buffdata, &temp32, 4); buffdata += 4;
697 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.scale);
698 memcpy(buffdata, &temp32, 4); buffdata += 4;
699 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.rate);
700 memcpy(buffdata, &temp32, 4); buffdata += 4;
701 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.start);
702 memcpy(buffdata, &temp32, 4); buffdata += 4;
703 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.length);
704 memcpy(buffdata, &temp32, 4); buffdata += 4;
705 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.bufsize);
706 memcpy(buffdata, &temp32, 4); buffdata += 4;
707 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.quality);
708 memcpy(buffdata, &temp32, 4); buffdata += 4;
709 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.samplesize);
710 memcpy(buffdata, &temp32, 4); buffdata += 4;
711 /* the audio header */
712 memcpy(buffdata, "strf", 4); buffdata += 4;
713 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strf_auds));
714 memcpy(buffdata, &temp32, 4); buffdata += 4;
715 /* the actual header */
716 temp16 = LE_FROM_GUINT16(avimux->auds.format);
717 memcpy(buffdata, &temp16, 2); buffdata += 2;
718 temp16 = LE_FROM_GUINT16(avimux->auds.channels);
719 memcpy(buffdata, &temp16, 2); buffdata += 2;
720 temp32 = LE_FROM_GUINT32(avimux->auds.rate);
721 memcpy(buffdata, &temp32, 4); buffdata += 4;
722 temp32 = LE_FROM_GUINT32(avimux->auds.av_bps);
723 memcpy(buffdata, &temp32, 4); buffdata += 4;
724 temp16 = LE_FROM_GUINT16(avimux->auds.blockalign);
725 memcpy(buffdata, &temp16, 2); buffdata += 2;
726 temp16 = LE_FROM_GUINT16(avimux->auds.size);
727 memcpy(buffdata, &temp16, 2); buffdata += 2;
730 if (avimux->video_pad_connected)
733 memcpy(buffdata, "LIST", 4); buffdata += 4;
734 temp32 = LE_FROM_GUINT32(sizeof(guint32)+4*3);
735 memcpy(buffdata, &temp32, 4); buffdata += 4;
736 memcpy(buffdata, "odml", 4); buffdata += 4;
737 memcpy(buffdata, "dmlh", 4); buffdata += 4;
738 temp32 = LE_FROM_GUINT32(sizeof(guint32));
739 memcpy(buffdata, &temp32, 4); buffdata += 4;
740 temp32 = LE_FROM_GUINT32(avimux->total_frames);
741 memcpy(buffdata, &temp32, 4); buffdata += 4;
744 /* avi data header */
745 memcpy(buffdata, "LIST", 4); buffdata += 4;
746 temp32 = LE_FROM_GUINT32(avimux->data_size);
747 memcpy(buffdata, &temp32, 4); buffdata += 4;
748 memcpy(buffdata, "movi", 4);
754 gst_avimux_riff_get_avix_header (guint32 datax_size)
760 buffer = gst_buffer_new();
761 GST_BUFFER_SIZE(buffer) = 24;
762 buffdata = GST_BUFFER_DATA(buffer) = g_malloc(GST_BUFFER_SIZE(buffer));
764 memcpy(buffdata, "LIST", 4); buffdata += 4;
765 temp32 = LE_FROM_GUINT32(datax_size+4*4);
766 memcpy(buffdata, &temp32, 4); buffdata += 4;
767 memcpy(buffdata, "AVIX", 4); buffdata += 4;
768 memcpy(buffdata, "LIST", 4); buffdata += 4;
769 temp32 = LE_FROM_GUINT32(datax_size);
770 memcpy(buffdata, &temp32, 4); buffdata += 4;
771 memcpy(buffdata, "movi", 4);
777 gst_avimux_riff_get_video_header (guint32 video_frame_size)
782 buffer = gst_buffer_new();
783 GST_BUFFER_DATA(buffer) = g_malloc(8);
784 GST_BUFFER_SIZE(buffer) = 8;
785 memcpy(GST_BUFFER_DATA(buffer), "00db", 4);
786 temp32 = LE_FROM_GUINT32(video_frame_size);
787 memcpy(GST_BUFFER_DATA(buffer)+4, &temp32, 4);
793 gst_avimux_riff_get_audio_header (guint32 audio_sample_size)
798 buffer = gst_buffer_new();
799 GST_BUFFER_DATA(buffer) = g_malloc(8);
800 GST_BUFFER_SIZE(buffer) = 8;
801 memcpy(GST_BUFFER_DATA(buffer), "01wb", 4);
802 temp32 = LE_FROM_GUINT32(audio_sample_size);
803 memcpy(GST_BUFFER_DATA(buffer)+4, &temp32, 4);
808 /* some other usable functions (thankyou xawtv ;-) ) */
811 gst_avimux_add_index (GstAviMux *avimux, guchar *code, guint32 flags, guint32 size)
813 if (avimux->idx_index == avimux->idx_count)
815 avimux->idx_count += 256;
816 avimux->idx = realloc(avimux->idx, avimux->idx_count*sizeof(gst_riff_index_entry));
818 memcpy(&(avimux->idx[avimux->idx_index].id), code, 4);
819 avimux->idx[avimux->idx_index].flags = LE_FROM_GUINT32(flags);
820 avimux->idx[avimux->idx_index].offset = LE_FROM_GUINT32(avimux->idx_offset);
821 avimux->idx[avimux->idx_index].size = LE_FROM_GUINT32(size);
823 avimux->idx_offset += size;
827 gst_avimux_write_index (GstAviMux *avimux)
832 buffer = gst_buffer_new();
833 GST_BUFFER_SIZE(buffer) = 8;
834 GST_BUFFER_DATA(buffer) = g_malloc(8);
835 memcpy(GST_BUFFER_DATA(buffer), "idx1", 4);
836 temp32 = LE_FROM_GUINT32(avimux->idx_index * sizeof(gst_riff_index_entry));
837 memcpy(GST_BUFFER_DATA(buffer)+4, &temp32, 4);
838 gst_pad_push(avimux->srcpad, buffer);
840 buffer = gst_buffer_new();
841 GST_BUFFER_SIZE(buffer) = avimux->idx_index * sizeof(gst_riff_index_entry);
842 GST_BUFFER_DATA(buffer) = (unsigned char*) avimux->idx;
843 avimux->idx = NULL; /* will be free()'ed by gst_buffer_unref() */
844 avimux->total_data += GST_BUFFER_SIZE(buffer);
845 gst_pad_push(avimux->srcpad, buffer);
847 avimux->idx_size += avimux->idx_index * sizeof(gst_riff_index_entry) + 8;
850 avimux->avi_hdr.flags |= GST_RIFF_AVIH_HASINDEX;
854 gst_avimux_bigfile(GstAviMux *avimux, gboolean last)
859 if (avimux->is_bigfile)
862 event = gst_event_new_seek (GST_FORMAT_BYTES |
863 GST_SEEK_METHOD_SET |
866 /* if the event succeeds */
867 if (gst_pad_send_event(GST_PAD_PEER(avimux->srcpad), event)) {
869 /* rewrite AVIX header */
870 header = gst_avimux_riff_get_avix_header(avimux->datax_size);
871 gst_pad_push(avimux->srcpad, header);
873 /* go back to current location */
874 event = gst_event_new_seek (GST_FORMAT_BYTES |
875 GST_SEEK_METHOD_SET |
878 gst_pad_send_event(GST_PAD_PEER(avimux->srcpad), event);
881 avimux->avix_start = avimux->total_data;
886 avimux->is_bigfile = TRUE;
887 avimux->numx_frames = 0;
888 avimux->datax_size = 0;
890 header = gst_avimux_riff_get_avix_header(0);
891 avimux->total_data += GST_BUFFER_SIZE(header);
892 gst_pad_push(avimux->srcpad, header);
895 /* enough header blabla now, let's go on to actually writing the headers */
898 gst_avimux_start_file (GstAviMux *avimux)
902 avimux->total_data = 0;
903 avimux->total_frames = 0;
904 avimux->data_size = 4; /* ? */
905 avimux->datax_size = 0;
906 avimux->num_frames = 0;
907 avimux->numx_frames = 0;
908 avimux->audio_size = 0;
909 avimux->avix_start = 0;
911 avimux->idx_index = 0;
912 avimux->idx_offset = 0; /* see 10 lines below */
913 avimux->idx_size = 0;
914 avimux->idx_count = 0;
918 avimux->avi_hdr.streams = avimux->video_pad_connected?1:0 + avimux->audio_pad_connected?1:0;
919 avimux->is_bigfile = FALSE;
921 header = gst_avimux_riff_get_avi_header(avimux);
922 avimux->idx_offset = avimux->header_size + 12;
923 avimux->total_data += GST_BUFFER_SIZE(header);
924 gst_pad_push(avimux->srcpad, header);
926 avimux->write_header = FALSE;
927 avimux->restart = FALSE;
931 gst_avimux_stop_file (GstAviMux *avimux)
936 /* if bigfile, rewrite header, else write indexes */
937 if (avimux->video_pad_connected)
939 if (avimux->is_bigfile)
941 gst_avimux_bigfile(avimux, TRUE);
942 avimux->idx_size = 0;
946 gst_avimux_write_index(avimux);
950 /* statistics/total_frames/... */
951 avimux->avi_hdr.tot_frames = avimux->num_frames;
952 if (avimux->video_pad_connected)
953 avimux->vids_hdr.length = avimux->num_frames;
954 if (avimux->audio_pad_connected)
956 if (avimux->auds_hdr.scale)
957 avimux->auds_hdr.length = avimux->audio_size/avimux->auds_hdr.scale;
959 avimux->auds_hdr.length = 0; /* urm...? FIXME! ;-) */
962 /* set rate and everything having to do with that */
963 avimux->avi_hdr.us_frame = avimux->vids_hdr.scale = 1000000/avimux->framerate;
964 avimux->avi_hdr.max_bps = 0;
965 if (avimux->audio_pad_connected)
966 avimux->avi_hdr.max_bps += avimux->auds.av_bps;
967 if (avimux->video_pad_connected)
968 avimux->avi_hdr.max_bps += ((avimux->vids.bit_cnt+7)/8) *
970 avimux->vids.image_size;
972 /* seek and rewrite the header */
973 header = gst_avimux_riff_get_avi_header(avimux);
974 event = gst_event_new_seek (GST_FORMAT_BYTES |
975 GST_SEEK_METHOD_SET |
976 GST_SEEK_FLAG_FLUSH, 0);
977 gst_pad_send_event(GST_PAD_PEER(avimux->srcpad), event);
978 gst_pad_push(avimux->srcpad, header);
980 avimux->write_header = TRUE;
984 gst_avimux_restart_file (GstAviMux *avimux)
988 gst_avimux_stop_file(avimux);
990 event = gst_event_new(GST_EVENT_NEW_MEDIA);
991 gst_pad_send_event(avimux->srcpad, event);
993 gst_avimux_start_file(avimux);
996 /* handle events (search) */
998 gst_avimux_handle_event (GstPad *pad, GstEvent *event)
1003 avimux = GST_AVIMUX (gst_pad_get_parent (pad));
1005 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
1008 case GST_EVENT_NEW_MEDIA:
1009 avimux->restart = TRUE;
1012 /* is this allright? */
1013 if (pad == avimux->videosinkpad)
1014 avimux->video_pad_eos = TRUE;
1015 else if (pad == avimux->audiosinkpad)
1016 avimux->audio_pad_eos = TRUE;
1018 g_warning("Unknown pad for EOS!");
1028 /* fill the internal queue for each available pad */
1030 gst_avimux_fill_queue (GstAviMux *avimux)
1034 if (!avimux->audio_buffer_queue &&
1035 GST_PAD_IS_USABLE(avimux->audiosinkpad) &&
1036 !avimux->audio_pad_eos)
1040 buffer = gst_pad_pull(avimux->audiosinkpad);
1041 if (GST_IS_EVENT(buffer))
1043 gst_avimux_handle_event(avimux->audiosinkpad, GST_EVENT(buffer));
1047 avimux->audio_buffer_queue = buffer;
1053 if (!avimux->video_buffer_queue &&
1054 GST_PAD_IS_USABLE(avimux->videosinkpad) &&
1055 !avimux->video_pad_eos)
1059 buffer = gst_pad_pull(avimux->videosinkpad);
1060 if (GST_IS_EVENT(buffer))
1062 gst_avimux_handle_event(avimux->videosinkpad, GST_EVENT(buffer));
1066 avimux->video_buffer_queue = buffer;
1067 if (avimux->framerate < 0)
1068 avimux->framerate = gst_video_frame_rate(GST_PAD_PEER(avimux->videosinkpad));
1076 /* send extra 'padding' data */
1078 gst_avimux_send_pad_data (GstAviMux *avimux,
1083 buffer = gst_buffer_new();
1084 GST_BUFFER_SIZE(buffer) = num_bytes;
1085 GST_BUFFER_DATA(buffer) = g_malloc(num_bytes);
1086 memset(GST_BUFFER_DATA(buffer), 0, num_bytes);
1088 gst_pad_push(avimux->srcpad, buffer);
1091 /* do audio buffer */
1093 gst_avimux_do_audio_buffer (GstAviMux *avimux)
1095 GstBuffer *data = avimux->audio_buffer_queue, *header;
1096 gulong total_size, pad_bytes;
1098 /* write a audio header + index entry */
1099 header = gst_avimux_riff_get_audio_header((GST_BUFFER_SIZE(data)+3)&~3);
1100 total_size = GST_BUFFER_SIZE(header) + GST_BUFFER_SIZE(data);
1101 avimux->total_data += total_size;
1103 if (avimux->is_bigfile)
1105 avimux->datax_size += total_size;
1109 avimux->data_size += total_size;
1110 avimux->audio_size += GST_BUFFER_SIZE(data);
1111 gst_avimux_add_index(avimux, "01wb", 0x0, total_size);
1114 gst_pad_push(avimux->srcpad, header);
1115 pad_bytes = ((GST_BUFFER_SIZE(data)+3)&~3) - GST_BUFFER_SIZE(data);
1117 if (GST_BUFFER_MAXSIZE(data) >= GST_BUFFER_SIZE(data) + pad_bytes)
1119 GST_BUFFER_SIZE(data) += pad_bytes;
1122 gst_pad_push(avimux->srcpad, data);
1124 gst_avimux_send_pad_data(avimux, pad_bytes);
1125 avimux->audio_buffer_queue = NULL;
1129 /* do video buffer */
1131 gst_avimux_do_video_buffer (GstAviMux *avimux)
1133 GstBuffer *data = avimux->video_buffer_queue, *header;
1134 gulong total_size, pad_bytes;
1136 if (avimux->restart)
1137 gst_avimux_restart_file(avimux);
1139 /* write a video header + index entry */
1140 if ((avimux->is_bigfile?avimux->datax_size:avimux->data_size)+GST_BUFFER_SIZE(data)>1024*1024*2000)
1142 if (avimux->enable_large_avi)
1143 gst_avimux_bigfile(avimux, FALSE);
1145 gst_avimux_restart_file(avimux);
1148 header = gst_avimux_riff_get_video_header((GST_BUFFER_SIZE(data)+3)&~3);
1149 total_size = GST_BUFFER_SIZE(header) + GST_BUFFER_SIZE(data);
1150 avimux->total_data += total_size;
1151 avimux->total_frames++;
1153 if (avimux->is_bigfile)
1155 avimux->datax_size += total_size;
1156 avimux->numx_frames++;
1160 avimux->data_size += total_size;
1161 avimux->num_frames++;
1162 gst_avimux_add_index(avimux, "00db", 0x12, total_size);
1165 gst_pad_push(avimux->srcpad, header);
1166 pad_bytes = ((GST_BUFFER_SIZE(data)+3)&~3) - GST_BUFFER_SIZE(data);
1168 if (GST_BUFFER_MAXSIZE(data) >= GST_BUFFER_SIZE(data) + pad_bytes)
1170 GST_BUFFER_SIZE(data) += pad_bytes;
1173 gst_pad_push(avimux->srcpad, data);
1175 gst_avimux_send_pad_data(avimux, pad_bytes);
1176 avimux->video_buffer_queue = NULL;
1180 /* take the oldest buffer in our internal queue and push-it */
1182 gst_avimux_do_one_buffer (GstAviMux *avimux)
1184 if (avimux->video_buffer_queue &&
1185 avimux->audio_buffer_queue)
1187 if (GST_BUFFER_TIMESTAMP(avimux->video_buffer_queue) <=
1188 GST_BUFFER_TIMESTAMP(avimux->audio_buffer_queue))
1189 gst_avimux_do_video_buffer(avimux);
1191 gst_avimux_do_audio_buffer(avimux);
1193 else if (avimux->video_buffer_queue ||
1194 avimux->audio_buffer_queue)
1196 if (avimux->video_buffer_queue)
1197 gst_avimux_do_video_buffer(avimux);
1199 gst_avimux_do_audio_buffer(avimux);
1209 gst_avimux_loop (GstElement *element)
1213 avimux = GST_AVIMUX(element);
1215 /* first fill queue (some elements only set caps when
1216 * flowing data), then write header */
1217 gst_avimux_fill_queue(avimux);
1219 if (avimux->write_header)
1220 gst_avimux_start_file(avimux);
1222 gst_avimux_do_one_buffer(avimux);
1226 gst_avimux_get_property (GObject *object,
1233 /* it's not null if we got it, but it might not be ours */
1234 g_return_if_fail(GST_IS_AVIMUX(object));
1235 avimux = GST_AVIMUX(object);
1240 g_value_set_boolean(value, avimux->enable_large_avi);
1243 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1249 gst_avimux_set_property (GObject *object,
1251 const GValue *value,
1256 /* it's not null if we got it, but it might not be ours */
1257 g_return_if_fail(GST_IS_AVIMUX(object));
1258 avimux = GST_AVIMUX(object);
1263 avimux->enable_large_avi = g_value_get_boolean(value);
1266 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1271 static GstElementStateReturn
1272 gst_avimux_change_state (GstElement *element)
1275 gint transition = GST_STATE_TRANSITION (element);
1277 g_return_val_if_fail(GST_IS_AVIMUX(element), GST_STATE_FAILURE);
1279 avimux = GST_AVIMUX(element);
1281 switch (transition) {
1282 case GST_STATE_READY_TO_PAUSED:
1284 case GST_STATE_PAUSED_TO_PLAYING:
1285 avimux->framerate = -1; /* means that we fill it in later */
1286 avimux->video_pad_eos = avimux->audio_pad_eos = FALSE;
1288 case GST_STATE_PLAYING_TO_PAUSED:
1289 /* this function returns TRUE while it handles buffers */
1290 while (gst_avimux_do_one_buffer(avimux));
1291 gst_avimux_stop_file(avimux);
1293 case GST_STATE_PAUSED_TO_READY:
1297 if (GST_ELEMENT_CLASS (parent_class)->change_state)
1298 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
1300 return GST_STATE_SUCCESS;
1304 plugin_init (GModule *module, GstPlugin *plugin)
1306 GstElementFactory *factory;
1308 if (!gst_library_load("gstvideo"))
1311 /* create an elementfactory for the avimux element */
1312 factory = gst_element_factory_new ("avimux", GST_TYPE_AVIMUX,
1313 &gst_avimux_details);
1314 g_return_val_if_fail (factory != NULL, FALSE);
1316 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (src_factory));
1317 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (audio_sink_factory));
1318 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (video_sink_factory));
1320 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
1325 GstPluginDesc plugin_desc = {