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,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_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 avimux->auds.format = GST_RIFF_WAVE_FORMAT_PCM;
366 avimux->auds.channels = gst_caps_get_int (caps, "channels");
367 avimux->auds.rate = gst_caps_get_int (caps, "rate");
368 avimux->auds.av_bps = gst_caps_get_int (caps, "width") * avimux->auds.rate *
369 avimux->auds.channels / 8;
370 avimux->auds.blockalign = gst_caps_get_int (caps, "width") * avimux->auds.channels/8;
371 avimux->auds.size = gst_caps_get_int (caps, "depth");
374 else if (!strcmp (mimetype, "audio/mp3"))
376 /* we don't need to do anything here, compressed mp3 contains it all */
377 avimux->auds.format = gst_caps_get_int(caps, "layer")==3?
378 GST_RIFF_WAVE_FORMAT_MPEGL3:GST_RIFF_WAVE_FORMAT_MPEGL12;
382 return GST_PAD_CONNECT_REFUSED;
385 return GST_PAD_CONNECT_OK;
389 gst_avimux_request_new_pad (GstElement *element,
390 GstPadTemplate *templ,
391 const gchar *req_name)
397 g_return_val_if_fail (templ != NULL, NULL);
399 if (templ->direction != GST_PAD_SINK) {
400 g_warning ("avimux: request pad that is not a SINK pad\n");
404 g_return_val_if_fail (GST_IS_AVIMUX (element), NULL);
406 avimux = GST_AVIMUX (element);
408 if (templ == GST_PADTEMPLATE_GET (audio_sink_factory)) {
409 g_return_val_if_fail(avimux->num_audio_pads == 0 /*< MAX_NUM_AUDIO_PADS*/, NULL);
410 name = g_strdup_printf ("audio_%02d", avimux->num_audio_pads);
411 newpad = gst_pad_new_from_template (templ, name);
412 gst_pad_set_element_private (newpad, GINT_TO_POINTER (avimux->num_audio_pads));
414 avimux->audiosinkpad[avimux->num_audio_pads] = newpad;
415 avimux->num_audio_pads++;
417 else if (templ == GST_PADTEMPLATE_GET (video_sink_factory)) {
418 g_return_val_if_fail(avimux->num_video_pads == 0 /*< MAX_NUM_VIDEO_PADS*/, NULL);
419 name = g_strdup_printf ("video_%02d", avimux->num_video_pads);
420 newpad = gst_pad_new_from_template (templ, name);
421 gst_pad_set_element_private (newpad, GINT_TO_POINTER (avimux->num_video_pads));
423 avimux->videosinkpad[avimux->num_video_pads] = newpad;
424 avimux->num_video_pads++;
427 g_warning ("avimux: this is not our template!\n");
431 gst_pad_set_chain_function (newpad, gst_avimux_chain);
432 gst_pad_set_connect_function (newpad, gst_avimux_sinkconnect);
433 gst_element_add_pad (element, newpad);
438 /* maybe some of these functions should be moved to riff.h? */
440 /* DISCLAIMER: this function is ugly. So be it (i.e. it makes the rest easier) */
443 gst_avimux_riff_get_avi_header (GstAviMux *avimux)
450 buffer = gst_buffer_new();
452 /* first, let's see what actually needs to be in the buffer */
453 GST_BUFFER_SIZE(buffer) = 0;
454 GST_BUFFER_SIZE(buffer) += 32 + sizeof(gst_riff_avih); /* avi header */
455 if (avimux->num_video_pads)
456 { /* we have video */
457 GST_BUFFER_SIZE(buffer) += 28 + sizeof(gst_riff_strh) + sizeof(gst_riff_strf_vids); /* vid hdr */
458 GST_BUFFER_SIZE(buffer) += 24; /* odml header */
460 if (avimux->num_audio_pads)
461 { /* we have audio */
462 GST_BUFFER_SIZE(buffer) += 28 + sizeof(gst_riff_strh) + sizeof(gst_riff_strf_auds); /* aud hdr */
464 /* this is the "riff size" */
465 avimux->header_size = GST_BUFFER_SIZE(buffer);
466 GST_BUFFER_SIZE(buffer) += 12; /* avi data header */
468 /* allocate the buffer */
469 buffdata = GST_BUFFER_DATA(buffer) = g_malloc(GST_BUFFER_SIZE(buffer));
471 /* avi header metadata */
472 memcpy(buffdata, "RIFF", 4); buffdata += 4;
473 temp32 = LE_FROM_GUINT32(avimux->header_size + avimux->idx_size + avimux->data_size);
474 memcpy(buffdata, &temp32, 4); buffdata += 4;
475 memcpy(buffdata, "AVI ", 4); buffdata += 4;
476 memcpy(buffdata, "LIST", 4); buffdata += 4;
477 temp32 = LE_FROM_GUINT32(avimux->header_size - 4*5);
478 memcpy(buffdata, &temp32, 4); buffdata += 4;
479 memcpy(buffdata, "hdrl", 4); buffdata += 4;
480 memcpy(buffdata, "avih", 4); buffdata += 4;
481 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_avih));
482 memcpy(buffdata, &temp32, 4); buffdata += 4;
483 /* the AVI header itself */
484 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.us_frame);
485 memcpy(buffdata, &temp32, 4); buffdata += 4;
486 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.max_bps);
487 memcpy(buffdata, &temp32, 4); buffdata += 4;
488 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.pad_gran);
489 memcpy(buffdata, &temp32, 4); buffdata += 4;
490 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.flags);
491 memcpy(buffdata, &temp32, 4); buffdata += 4;
492 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.tot_frames);
493 memcpy(buffdata, &temp32, 4); buffdata += 4;
494 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.init_frames);
495 memcpy(buffdata, &temp32, 4); buffdata += 4;
496 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.streams);
497 memcpy(buffdata, &temp32, 4); buffdata += 4;
498 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.bufsize);
499 memcpy(buffdata, &temp32, 4); buffdata += 4;
500 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.width);
501 memcpy(buffdata, &temp32, 4); buffdata += 4;
502 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.height);
503 memcpy(buffdata, &temp32, 4); buffdata += 4;
504 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.scale);
505 memcpy(buffdata, &temp32, 4); buffdata += 4;
506 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.rate);
507 memcpy(buffdata, &temp32, 4); buffdata += 4;
508 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.start);
509 memcpy(buffdata, &temp32, 4); buffdata += 4;
510 temp32 = LE_FROM_GUINT32(avimux->avi_hdr.length);
511 memcpy(buffdata, &temp32, 4); buffdata += 4;
513 if (avimux->num_video_pads)
515 /* video header metadata */
516 memcpy(buffdata, "LIST", 4); buffdata += 4;
517 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh) + sizeof(gst_riff_strf_vids) + 4*5);
518 memcpy(buffdata, &temp32, 4); buffdata += 4;
519 memcpy(buffdata, "strl", 4); buffdata += 4;
521 memcpy(buffdata, "strh", 4); buffdata += 4;
522 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh));
523 memcpy(buffdata, &temp32, 4); buffdata += 4;
524 /* the actual header */
525 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.type);
526 memcpy(buffdata, &temp32, 4); buffdata += 4;
527 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.fcc_handler);
528 memcpy(buffdata, &temp32, 4); buffdata += 4;
529 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.flags);
530 memcpy(buffdata, &temp32, 4); buffdata += 4;
531 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.priority);
532 memcpy(buffdata, &temp32, 4); buffdata += 4;
533 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.init_frames);
534 memcpy(buffdata, &temp32, 4); buffdata += 4;
535 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.scale);
536 memcpy(buffdata, &temp32, 4); buffdata += 4;
537 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.rate);
538 memcpy(buffdata, &temp32, 4); buffdata += 4;
539 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.start);
540 memcpy(buffdata, &temp32, 4); buffdata += 4;
541 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.length);
542 memcpy(buffdata, &temp32, 4); buffdata += 4;
543 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.bufsize);
544 memcpy(buffdata, &temp32, 4); buffdata += 4;
545 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.quality);
546 memcpy(buffdata, &temp32, 4); buffdata += 4;
547 temp32 = LE_FROM_GUINT32(avimux->vids_hdr.samplesize);
548 memcpy(buffdata, &temp32, 4); buffdata += 4;
549 /* the video header */
550 memcpy(buffdata, "strf", 4); buffdata += 4;
551 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strf_vids));
552 memcpy(buffdata, &temp32, 4); buffdata += 4;
553 /* the actual header */
554 temp32 = LE_FROM_GUINT32(avimux->vids.size);
555 memcpy(buffdata, &temp32, 4); buffdata += 4;
556 temp32 = LE_FROM_GUINT32(avimux->vids.width);
557 memcpy(buffdata, &temp32, 4); buffdata += 4;
558 temp32 = LE_FROM_GUINT32(avimux->vids.height);
559 memcpy(buffdata, &temp32, 4); buffdata += 4;
560 temp16 = LE_FROM_GUINT16(avimux->vids.planes);
561 memcpy(buffdata, &temp16, 2); buffdata += 2;
562 temp16 = LE_FROM_GUINT16(avimux->vids.bit_cnt);
563 memcpy(buffdata, &temp16, 2); buffdata += 2;
564 temp32 = LE_FROM_GUINT32(avimux->vids.compression);
565 memcpy(buffdata, &temp32, 4); buffdata += 4;
566 temp32 = LE_FROM_GUINT32(avimux->vids.image_size);
567 memcpy(buffdata, &temp32, 4); buffdata += 4;
568 temp32 = LE_FROM_GUINT32(avimux->vids.xpels_meter);
569 memcpy(buffdata, &temp32, 4); buffdata += 4;
570 temp32 = LE_FROM_GUINT32(avimux->vids.ypels_meter);
571 memcpy(buffdata, &temp32, 4); buffdata += 4;
572 temp32 = LE_FROM_GUINT32(avimux->vids.num_colors);
573 memcpy(buffdata, &temp32, 4); buffdata += 4;
574 temp32 = LE_FROM_GUINT32(avimux->vids.imp_colors);
575 memcpy(buffdata, &temp32, 4); buffdata += 4;
578 if (avimux->num_audio_pads)
581 memcpy(buffdata, "LIST", 4); buffdata += 4;
582 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh) + sizeof(gst_riff_strf_auds) + 4*5);
583 memcpy(buffdata, &temp32, 4); buffdata += 4;
584 memcpy(buffdata, "strl", 4); buffdata += 4;
586 memcpy(buffdata, "strh", 4); buffdata += 4;
587 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh));
588 memcpy(buffdata, &temp32, 4); buffdata += 4;
589 /* the actual header */
590 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.type);
591 memcpy(buffdata, &temp32, 4); buffdata += 4;
592 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.fcc_handler);
593 memcpy(buffdata, &temp32, 4); buffdata += 4;
594 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.flags);
595 memcpy(buffdata, &temp32, 4); buffdata += 4;
596 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.priority);
597 memcpy(buffdata, &temp32, 4); buffdata += 4;
598 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.init_frames);
599 memcpy(buffdata, &temp32, 4); buffdata += 4;
600 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.scale);
601 memcpy(buffdata, &temp32, 4); buffdata += 4;
602 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.rate);
603 memcpy(buffdata, &temp32, 4); buffdata += 4;
604 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.start);
605 memcpy(buffdata, &temp32, 4); buffdata += 4;
606 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.length);
607 memcpy(buffdata, &temp32, 4); buffdata += 4;
608 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.bufsize);
609 memcpy(buffdata, &temp32, 4); buffdata += 4;
610 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.quality);
611 memcpy(buffdata, &temp32, 4); buffdata += 4;
612 temp32 = LE_FROM_GUINT32(avimux->auds_hdr.samplesize);
613 memcpy(buffdata, &temp32, 4); buffdata += 4;
614 /* the audio header */
615 memcpy(buffdata, "strf", 4); buffdata += 4;
616 temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strf_vids));
617 memcpy(buffdata, &temp32, 4); buffdata += 4;
618 /* the actual header */
619 temp16 = LE_FROM_GUINT16(avimux->auds.format);
620 memcpy(buffdata, &temp16, 2); buffdata += 2;
621 temp16 = LE_FROM_GUINT16(avimux->auds.channels);
622 memcpy(buffdata, &temp16, 2); buffdata += 2;
623 temp32 = LE_FROM_GUINT32(avimux->auds.rate);
624 memcpy(buffdata, &temp32, 4); buffdata += 4;
625 temp32 = LE_FROM_GUINT32(avimux->auds.av_bps);
626 memcpy(buffdata, &temp32, 4); buffdata += 4;
627 temp16 = LE_FROM_GUINT16(avimux->auds.blockalign);
628 memcpy(buffdata, &temp16, 2); buffdata += 2;
629 temp16 = LE_FROM_GUINT16(avimux->auds.size);
630 memcpy(buffdata, &temp16, 2); buffdata += 2;
633 if (avimux->num_video_pads)
636 memcpy(buffdata, "LIST", 4); buffdata += 4;
637 temp32 = LE_FROM_GUINT32(sizeof(guint32)+4*3);
638 memcpy(buffdata, &temp32, 4); buffdata += 4;
639 memcpy(buffdata, "odml", 4); buffdata += 4;
640 memcpy(buffdata, "dmlh", 4); buffdata += 4;
641 temp32 = LE_FROM_GUINT32(sizeof(guint32));
642 memcpy(buffdata, &temp32, 4); buffdata += 4;
643 temp32 = LE_FROM_GUINT32(avimux->total_frames);
644 memcpy(buffdata, &temp32, 4); buffdata += 4;
647 /* avi data header */
648 memcpy(buffdata, "LIST", 4); buffdata += 4;
649 temp32 = LE_FROM_GUINT32(avimux->data_size);
650 memcpy(buffdata, &temp32, 4); buffdata += 4;
651 memcpy(buffdata, "movi", 4);
657 gst_avimux_riff_get_avix_header (guint32 datax_size)
663 buffer = gst_buffer_new();
664 GST_BUFFER_SIZE(buffer) = 24;
665 buffdata = GST_BUFFER_DATA(buffer) = g_malloc(GST_BUFFER_SIZE(buffer));
667 memcpy(buffdata, "LIST", 4); buffdata += 4;
668 temp32 = LE_FROM_GUINT32(datax_size+4*4);
669 memcpy(buffdata, &temp32, 4); buffdata += 4;
670 memcpy(buffdata, "AVIX", 4); buffdata += 4;
671 memcpy(buffdata, "LIST", 4); buffdata += 4;
672 temp32 = LE_FROM_GUINT32(datax_size);
673 memcpy(buffdata, &temp32, 4); buffdata += 4;
674 memcpy(buffdata, "movi", 4);
680 gst_avimux_riff_get_video_header (guint32 video_frame_size)
685 buffer = gst_buffer_new();
686 GST_BUFFER_DATA(buffer) = g_malloc(8);
687 GST_BUFFER_SIZE(buffer) = 8;
688 memcpy(GST_BUFFER_DATA(buffer), "00db", 4);
689 temp32 = LE_FROM_GUINT32(video_frame_size);
690 memcpy(GST_BUFFER_DATA(buffer)+4, &temp32, 4);
696 gst_avimux_riff_get_audio_header (guint32 audio_sample_size)
701 buffer = gst_buffer_new();
702 GST_BUFFER_DATA(buffer) = g_malloc(8);
703 GST_BUFFER_SIZE(buffer) = 8;
704 memcpy(GST_BUFFER_DATA(buffer), "01wb", 4);
705 temp32 = LE_FROM_GUINT32(audio_sample_size);
706 memcpy(GST_BUFFER_DATA(buffer)+4, &temp32, 4);
711 /* some other usable functions (thankyou xawtv ;-) ) */
714 gst_avimux_add_index (GstAviMux *avimux, guint32 fourcc, guint32 flags, guint32 size)
718 if (avimux->idx_index == avimux->idx_count)
720 avimux->idx_count += 256;
721 avimux->idx = realloc(avimux->idx, avimux->idx_count*sizeof(gst_riff_index_entry));
723 temp32 = LE_FROM_GUINT32(fourcc);
724 memcpy(&(avimux->idx[avimux->idx_index].id), &temp32, 4);
725 avimux->idx[avimux->idx_index].flags = LE_FROM_GUINT32(flags);
726 avimux->idx[avimux->idx_index].offset = LE_FROM_GUINT32(avimux->idx_offset-avimux->header_size-8);
727 avimux->idx[avimux->idx_index].size = LE_FROM_GUINT32(size);
729 avimux->idx_offset += size + sizeof(gst_riff_index_entry);
733 gst_avimux_write_index (GstAviMux *avimux)
738 buffer = gst_buffer_new();
739 GST_BUFFER_SIZE(buffer) = 8;
740 GST_BUFFER_DATA(buffer) = g_malloc(8);
741 memcpy(GST_BUFFER_DATA(buffer), "idx1", 4);
742 temp32 = LE_FROM_GUINT32(avimux->idx_index * sizeof(gst_riff_index_entry));
743 memcpy(GST_BUFFER_DATA(buffer)+4, &temp32, 4);
744 gst_pad_push(avimux->srcpad, buffer);
746 buffer = gst_buffer_new();
747 GST_BUFFER_SIZE(buffer) = avimux->idx_index * sizeof(gst_riff_index_entry);
748 GST_BUFFER_DATA(buffer) = (unsigned char*) avimux->idx;
749 avimux->idx = NULL; /* will be free()'ed by gst_buffer_unref() */
750 avimux->total_data += GST_BUFFER_SIZE(buffer);
751 gst_pad_push(avimux->srcpad, buffer);
753 avimux->idx_size += avimux->idx_index * sizeof(gst_riff_index_entry) + 8;
756 avimux->avi_hdr.flags |= GST_RIFF_AVIH_HASINDEX;
760 gst_avimux_bigfile(GstAviMux *avimux, gboolean last)
765 if (avimux->is_bigfile)
768 event = gst_event_new_seek(GST_SEEK_BYTEOFFSET_SET, avimux->avix_start, TRUE);
769 gst_pad_send_event(avimux->srcpad, event);
771 /* rewrite AVIX header */
772 header = gst_avimux_riff_get_avix_header(avimux->datax_size);
773 gst_pad_push(avimux->srcpad, header);
775 /* go back to current location */
776 event = gst_event_new_seek(GST_SEEK_BYTEOFFSET_SET, avimux->total_data, TRUE);
777 gst_pad_send_event(avimux->srcpad, event);
779 avimux->avix_start = avimux->total_data;
784 avimux->is_bigfile = TRUE;
785 avimux->numx_frames = 0;
786 avimux->datax_size = 0;
788 header = gst_avimux_riff_get_avix_header(0);
789 avimux->total_data += GST_BUFFER_SIZE(header);
790 gst_pad_push(avimux->srcpad, header);
793 /* enough header blabla now, let's go on to actually writing the headers */
796 gst_avimux_start_file (GstAviMux *avimux)
800 avimux->total_data = 0;
801 avimux->total_frames = 0;
802 avimux->data_size = 4; /* ? */
803 avimux->datax_size = 0;
804 avimux->num_frames = 0;
805 avimux->numx_frames = 0;
806 avimux->audio_size = 0;
808 avimux->idx_index = 0;
809 avimux->idx_offset = avimux->header_size + 12;
810 avimux->idx_size = 0;
811 avimux->idx_count = 0;
815 avimux->avi_hdr.streams = avimux->num_video_pads + avimux->num_audio_pads;
816 avimux->is_bigfile = FALSE;
818 header = gst_avimux_riff_get_avi_header(avimux);
819 avimux->total_data += GST_BUFFER_SIZE(header);
820 gst_pad_push(avimux->srcpad, header);
822 avimux->write_header = FALSE;
823 avimux->restart = FALSE;
827 gst_avimux_stop_file (GstAviMux *avimux)
832 /* if bigfile, rewrite header, else write indexes */
833 if (avimux->num_video_pads)
835 if (avimux->is_bigfile)
837 gst_avimux_bigfile(avimux, TRUE);
838 avimux->idx_size = 0;
842 gst_avimux_write_index(avimux);
846 /* statistics/total_frames/... */
847 avimux->avi_hdr.tot_frames = avimux->num_frames;
848 if (avimux->num_video_pads)
849 avimux->vids_hdr.length = avimux->num_frames;
850 if (avimux->num_audio_pads)
851 avimux->auds_hdr.length = avimux->audio_size/avimux->auds_hdr.scale;
853 /* TODO: fps calculation!! */
854 avimux->avi_hdr.us_frame = 1000000/avimux->framerate;
855 avimux->avi_hdr.max_bps = 0;
856 if (avimux->num_audio_pads)
857 avimux->avi_hdr.max_bps += avimux->auds.av_bps;
858 if (avimux->num_video_pads)
859 avimux->avi_hdr.max_bps += avimux->vids.bit_cnt/8 * avimux->framerate;
861 /* seek and rewrite the header */
862 header = gst_avimux_riff_get_avi_header(avimux);
863 event = gst_event_new_seek(GST_SEEK_BYTEOFFSET_SET, 0, TRUE);
864 gst_pad_send_event(avimux->srcpad, event);
865 gst_pad_push(avimux->srcpad, header);
867 avimux->write_header = TRUE;
871 gst_avimux_restart_file (GstAviMux *avimux)
875 gst_avimux_stop_file(avimux);
877 event = gst_event_new(GST_EVENT_NEW_MEDIA);
878 gst_pad_send_event(avimux->srcpad, event);
880 gst_avimux_start_file(avimux);
883 /* handle events (search) */
885 gst_avimux_handle_event (GstPad *pad, GstEvent *event)
890 avimux = GST_AVIMUX (gst_pad_get_parent (pad));
892 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
895 case GST_EVENT_NEW_MEDIA:
896 avimux->restart = TRUE;
906 gst_avimux_chain (GstPad *pad, GstBuffer *buf)
910 const gchar *padname = gst_pad_get_name (pad);
912 g_return_if_fail (pad != NULL);
913 g_return_if_fail (GST_IS_PAD (pad));
914 g_return_if_fail (buf != NULL);
915 g_return_if_fail (GST_BUFFER_DATA (buf) != NULL);
917 avimux = GST_AVIMUX (gst_pad_get_parent (pad));
919 if (GST_IS_EVENT(buf))
921 gst_avimux_handle_event(pad, GST_EVENT(buf));
926 if (avimux->write_header)
927 gst_avimux_start_file(avimux);
929 if (strncmp(padname, "audio_", 6) == 0)
931 /* write a audio header + index entry */
932 newbuf = gst_avimux_riff_get_audio_header(GST_BUFFER_SIZE(buf));
933 avimux->total_data += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
935 if (avimux->is_bigfile)
937 avimux->datax_size += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
941 avimux->data_size += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
942 avimux->audio_size += GST_BUFFER_SIZE(buf);
943 gst_avimux_add_index(avimux, avimux->auds.format, 0x0, GST_BUFFER_SIZE(buf));
946 gst_pad_push(avimux->srcpad, newbuf);
948 else if (strncmp(padname, "video_", 6) == 0)
951 gst_avimux_restart_file(avimux);
953 /* write a video header + index entry */
954 GST_BUFFER_SIZE(buf) = (GST_BUFFER_SIZE(buf)+3)&~3;
956 if ((avimux->is_bigfile?avimux->datax_size:avimux->data_size)+GST_BUFFER_SIZE(buf)>1024*1024*2000)
958 if (avimux->enable_large_avi)
959 gst_avimux_bigfile(avimux, FALSE);
961 gst_avimux_restart_file(avimux);
964 newbuf = gst_avimux_riff_get_video_header(GST_BUFFER_SIZE(buf));
965 avimux->total_data += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
966 avimux->total_frames++;
968 if (avimux->is_bigfile)
970 avimux->datax_size += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
971 avimux->numx_frames++;
975 avimux->data_size += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
976 avimux->num_frames++;
977 gst_avimux_add_index(avimux, avimux->vids.compression, 0x12, GST_BUFFER_SIZE(buf));
980 gst_pad_push(avimux->srcpad, newbuf);
984 g_warning("Unknown padname \'%s\'\n", padname);
989 gst_pad_push(avimux->srcpad, buf);
993 gst_avimux_get_property (GObject *object,
1000 /* it's not null if we got it, but it might not be ours */
1001 g_return_if_fail(GST_IS_AVIMUX(object));
1002 avimux = GST_AVIMUX(object);
1007 g_value_set_boolean(value, avimux->enable_large_avi);
1010 g_value_set_double(value, avimux->framerate);
1013 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1019 gst_avimux_set_property (GObject *object,
1021 const GValue *value,
1026 /* it's not null if we got it, but it might not be ours */
1027 g_return_if_fail(GST_IS_AVIMUX(object));
1028 avimux = GST_AVIMUX(object);
1033 avimux->enable_large_avi = g_value_get_boolean(value);
1036 avimux->framerate = g_value_get_double(value);
1039 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1044 static GstElementStateReturn
1045 gst_avimux_change_state (GstElement *element)
1048 gint transition = GST_STATE_TRANSITION (element);
1050 /* TODO: timer (for fps calculations) */
1052 g_return_val_if_fail(GST_IS_AVIMUX(element), GST_STATE_FAILURE);
1054 avimux = GST_AVIMUX(element);
1056 switch (transition) {
1057 case GST_STATE_READY_TO_PAUSED:
1058 /*gst_avimux_start_file(avimux);*/
1060 case GST_STATE_PAUSED_TO_PLAYING:
1062 case GST_STATE_PLAYING_TO_PAUSED:
1063 gst_avimux_stop_file(avimux);
1065 case GST_STATE_PAUSED_TO_READY:
1066 /*gst_avimux_stop_file(avimux);*/
1070 if (GST_ELEMENT_CLASS (parent_class)->change_state)
1071 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
1073 return GST_STATE_SUCCESS;
1077 plugin_init (GModule *module, GstPlugin *plugin)
1079 GstElementFactory *factory;
1081 /* this filter needs the riff parser */
1083 if (!gst_library_load ("gstriff")) {
1084 gst_info ("avimux: could not load support library: 'gstriff'\n");
1089 /* create an elementfactory for the avimux element */
1090 factory = gst_elementfactory_new ("avimux", GST_TYPE_AVIMUX,
1091 &gst_avimux_details);
1092 g_return_val_if_fail (factory != NULL, FALSE);
1094 gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (src_factory));
1095 gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (audio_sink_factory));
1096 gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (video_sink_factory));
1098 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
1103 GstPluginDesc plugin_desc = {