avi needs 4 byte padding
[platform/upstream/gst-plugins-good.git] / gst / avi / gstavimux.c
1 /* AVI muxer plugin for GStreamer
2  * Copyright (C) 2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /* based on:
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
25  */
26
27
28 #include <config.h>
29
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include <gst/video/video.h>
34
35 #include "gstavimux.h"
36
37 #ifndef LE_FROM_GUINT16
38 #define LE_FROM_GUINT16 GUINT16_FROM_LE
39 #endif
40 #ifndef LE_FROM_GUINT32
41 #define LE_FROM_GUINT32 GUINT32_FROM_LE
42 #endif
43
44 /* elementfactory information */
45 static GstElementDetails 
46 gst_avimux_details = 
47 {
48   "Avi multiplexer",
49   "Codec/Muxer",
50   "LGPL",
51   "Muxes audio and video into an avi stream",
52   VERSION,
53   "Ronald Bultje <rbultje@ronald.bitfreak.net>",
54   "(C) 2002",
55 };
56
57 /* AviMux signals and args */
58 enum {
59   /* FILL ME */
60   LAST_SIGNAL
61 };
62
63 enum {
64   ARG_0,
65   ARG_BIGFILE,
66 };
67
68 GST_PAD_TEMPLATE_FACTORY (src_factory,
69   "src",
70   GST_PAD_SRC,
71   GST_PAD_ALWAYS,
72   GST_CAPS_NEW (
73     "avimux_src_video",
74     "video/avi",
75     NULL
76   )
77 )
78     
79 GST_PAD_TEMPLATE_FACTORY (video_sink_factory,
80   "video_%d",
81   GST_PAD_SINK,
82   GST_PAD_REQUEST,
83   GST_CAPS_NEW (
84     "avimux_sink_video",
85     "video/avi",
86       "format",   GST_PROPS_STRING ("strf_vids")
87   ),
88   GST_CAPS_NEW (
89     "avimux_sink_video",
90     "video/raw",
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'))
95                 ),
96       "width",  GST_PROPS_INT_RANGE (16, 4096),
97       "height", GST_PROPS_INT_RANGE (16, 4096)
98   ),
99   GST_CAPS_NEW (
100     "avimux_sink_video",
101     "video/raw",
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(
106                   GST_PROPS_INT(16),
107                   GST_PROPS_INT(16),
108                   GST_PROPS_INT(24),
109                   GST_PROPS_INT(32)
110                 ),
111       "bpp",    GST_PROPS_LIST(
112                   GST_PROPS_INT(15),
113                   GST_PROPS_INT(16),
114                   GST_PROPS_INT(24),
115                   GST_PROPS_INT(32)
116                 )
117   ),
118   GST_CAPS_NEW (
119     "avimux_sink_video",
120     "video/jpeg",
121       "width",  GST_PROPS_INT_RANGE (16, 4096),
122       "height", GST_PROPS_INT_RANGE (16, 4096)
123   )
124 )
125     
126 GST_PAD_TEMPLATE_FACTORY (audio_sink_factory,
127   "audio_%d",
128   GST_PAD_SINK,
129   GST_PAD_REQUEST,
130   GST_CAPS_NEW (
131     "avimux_sink_audio",
132     "video/avi",
133       "format",   GST_PROPS_STRING ("strf_auds")
134   ),
135   GST_CAPS_NEW (
136     "avimux_sink_audio",
137     "audio/raw",
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)
144                           ),
145       "width",            GST_PROPS_LIST (
146                             GST_PROPS_INT (8),
147                             GST_PROPS_INT (16)
148                           ),
149       "depth",            GST_PROPS_LIST (
150                             GST_PROPS_INT (8),
151                             GST_PROPS_INT (16)
152                           ),
153       "rate",             GST_PROPS_INT_RANGE (1000, 48000),
154       "channels",         GST_PROPS_INT_RANGE (1, 2)
155   ),
156   GST_CAPS_NEW (
157     "avimux_sink_audio",
158     "audio/mp3",
159       NULL
160   )
161 )
162     
163
164 static void     gst_avimux_class_init                (GstAviMuxClass *klass);
165 static void     gst_avimux_init                      (GstAviMux      *avimux);
166
167 static void     gst_avimux_loop                      (GstElement     *element);
168 static gboolean gst_avimux_handle_event              (GstPad         *pad,
169                                                       GstEvent       *event);
170 static GstPad*  gst_avimux_request_new_pad           (GstElement     *element,
171                                                       GstPadTemplate *templ,
172                                                       const gchar    *name);
173 static void     gst_avimux_set_property              (GObject        *object,
174                                                       guint           prop_id,
175                                                       const GValue   *value,
176                                                       GParamSpec     *pspec);
177 static void     gst_avimux_get_property              (GObject        *object,
178                                                       guint           prop_id,
179                                                       GValue         *value,
180                                                       GParamSpec     *pspec);
181 static GstElementStateReturn gst_avimux_change_state (GstElement     *element);
182
183 static GstElementClass *parent_class = NULL;
184 /*static guint gst_avimux_signals[LAST_SIGNAL] = { 0 }; */
185
186 GType
187 gst_avimux_get_type (void) 
188 {
189   static GType avimux_type = 0;
190
191   if (!avimux_type) {
192     static const GTypeInfo avimux_info = {
193       sizeof(GstAviMuxClass),      
194       NULL,
195       NULL,
196       (GClassInitFunc)gst_avimux_class_init,
197       NULL,
198       NULL,
199       sizeof(GstAviMux),
200       0,
201       (GInstanceInitFunc)gst_avimux_init,
202     };
203     avimux_type = g_type_register_static(GST_TYPE_ELEMENT, "GstAviMux", &avimux_info, 0);
204   }
205   return avimux_type;
206 }
207
208 static void
209 gst_avimux_class_init (GstAviMuxClass *klass) 
210 {
211   GObjectClass *gobject_class;
212   GstElementClass *gstelement_class;
213
214   gobject_class = (GObjectClass*)klass;
215   gstelement_class = (GstElementClass*)klass;
216
217   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
218
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));
222
223   gstelement_class->request_new_pad = gst_avimux_request_new_pad;
224
225   gstelement_class->change_state = gst_avimux_change_state;
226
227   gstelement_class->get_property = gst_avimux_get_property;
228   gstelement_class->set_property = gst_avimux_set_property;
229 }
230
231 static const GstEventMask *
232 gst_avimux_get_event_masks (GstPad *pad)
233 {
234   static const GstEventMask gst_avimux_src_event_masks[] = {
235     { GST_EVENT_NEW_MEDIA, 0 },
236     { 0, }
237   };
238   static const GstEventMask gst_avimux_sink_event_masks[] = {
239     { GST_EVENT_EOS, 0 },
240     { 0, }
241   };
242
243   if (GST_PAD_IS_SRC(pad))
244     return gst_avimux_src_event_masks;
245   else
246     return gst_avimux_sink_event_masks;
247 }
248
249 static void 
250 gst_avimux_init (GstAviMux *avimux) 
251 {
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);
255
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);
259
260   avimux->audiosinkpad = NULL;
261   avimux->audio_pad_connected = FALSE;
262   avimux->videosinkpad = NULL;
263   avimux->video_pad_connected = FALSE;
264
265   avimux->audio_buffer_queue = NULL;
266   avimux->video_buffer_queue = NULL;
267
268   avimux->num_frames = 0;
269
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');
279
280   avimux->idx = NULL;
281
282   avimux->write_header = TRUE;
283
284   avimux->enable_large_avi = TRUE;
285
286   avimux->framerate = 0;
287
288   gst_element_set_loop_function(GST_ELEMENT(avimux), gst_avimux_loop);
289 }
290
291 static GstPadConnectReturn
292 gst_avimux_sinkconnect (GstPad *pad, GstCaps *vscaps)
293 {
294   GstAviMux *avimux;
295   GstCaps *caps;
296
297   avimux = GST_AVIMUX (gst_pad_get_parent (pad));
298
299   /* we are not going to act on variable caps */
300   if (!GST_CAPS_IS_FIXED (vscaps))
301     return GST_PAD_CONNECT_DELAYED;
302
303   GST_DEBUG (0, "avimux: sinkconnect triggered on %s", gst_pad_get_name (pad));
304
305   for (caps = vscaps; caps != NULL; caps = vscaps = vscaps->next)
306   {
307     const gchar* mimetype = gst_caps_get_mime(caps);
308
309     if (!strcmp (mimetype, "video/avi"))
310     {
311       const gchar* format;
312       
313       gst_caps_get_string (caps, "format", &format);
314
315       if (!strncmp (format, "strf_vids", 9)) {
316         avimux->vids.size        = sizeof(gst_riff_strf_vids);
317         gst_caps_get (caps,
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,
328                       NULL);
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;
332         goto done;
333       }
334       else if (!strncmp (format, "strf_auds", 9)) {
335         gst_caps_get (caps,
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,
342                       NULL);
343         avimux->auds_hdr.samplesize = avimux->auds_hdr.scale = avimux->auds.blockalign;
344         avimux->auds_hdr.rate = avimux->auds.av_bps;
345         goto done;
346       }
347     }
348     else if (!strcmp (mimetype, "video/raw"))
349     {
350       guint32 format;
351       gint temp;
352
353       gst_caps_get_fourcc_int (caps, "format", &format);
354       switch (format)
355       {
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;
364           switch (format)
365           {
366             case GST_MAKE_FOURCC('Y','U','Y','2'):
367               avimux->vids.bit_cnt     = 16; /* YUY2 */
368               break;
369             case GST_MAKE_FOURCC('R','G','B',' '):
370               gst_caps_get_int (caps, "bpp", &temp); /* RGB */
371               avimux->vids.bit_cnt = temp;
372               break;
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 */
376               break;
377           }
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;
383           goto done;
384         default:
385           break;
386       }
387     }
388     else if (!strcmp (mimetype, "video/jpeg"))
389     {
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;
399       goto done;
400     }
401     else if (!strcmp (mimetype, "audio/raw"))
402     {
403       gint width;
404
405       avimux->auds.format      = GST_RIFF_WAVE_FORMAT_PCM;
406       gst_caps_get (caps, "channels",   &avimux->auds.channels,
407                           "rate",       &avimux->auds.rate,
408                           "width",      &width,
409                           "depth",      &avimux->auds.size,
410                           NULL);
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;
413       goto done;
414     }
415     else if (!strcmp (mimetype, "audio/mp3"))
416     {
417       gint layer;
418
419       gst_caps_get_int(caps, "layer", &layer);
420
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);
425       goto done;
426     }
427   }
428   return GST_PAD_CONNECT_REFUSED;
429
430 done:
431   return GST_PAD_CONNECT_OK;
432 }
433
434 static void
435 gst_avimux_pad_connect (GstPad   *pad,
436                         GstPad   *peer,
437                         gpointer  data)
438 {
439   GstAviMux *avimux = GST_AVIMUX(data);
440   const gchar *padname = gst_pad_get_name (pad);
441
442   if (pad == avimux->audiosinkpad)
443   {
444     avimux->audio_pad_connected = TRUE;
445   }
446   else if (pad == avimux->videosinkpad)
447   {
448     avimux->video_pad_connected = TRUE;
449   }
450   else
451   {
452     g_warning("Unknown padname '%s'", padname);
453     return;
454   }
455
456   GST_DEBUG(GST_CAT_PLUGIN_INFO, "pad '%s' connected", padname);
457 }
458
459 static void
460 gst_avimux_pad_disconnect (GstPad   *pad,
461                            GstPad   *peer,
462                            gpointer  data)
463 {
464   GstAviMux *avimux = GST_AVIMUX(data);
465   const gchar *padname = gst_pad_get_name (pad);
466
467   if (pad == avimux->audiosinkpad)
468   {
469     avimux->audio_pad_connected = FALSE;
470     avimux->audiosinkpad = NULL;
471   }
472   else if (pad == avimux->videosinkpad)
473   {
474     avimux->video_pad_connected = FALSE;
475     avimux->videosinkpad = NULL;
476   }
477   else
478   {
479     g_warning("Unknown padname '%s'", padname);
480     return;
481   }
482
483   GST_DEBUG(GST_CAT_PLUGIN_INFO, "pad '%s' disconnected", padname);
484
485   /* is this allowed? */
486   gst_pad_destroy(pad);
487 }
488
489 static GstPad*
490 gst_avimux_request_new_pad (GstElement     *element,
491                             GstPadTemplate *templ,
492                             const gchar    *req_name)
493 {
494   GstAviMux *avimux;
495   GstPad *newpad;
496
497   g_return_val_if_fail (templ != NULL, NULL);
498
499   if (templ->direction != GST_PAD_SINK) {
500     g_warning ("avimux: request pad that is not a SINK pad\n");
501     return NULL;
502   }
503
504   g_return_val_if_fail (GST_IS_AVIMUX (element), NULL);
505
506   avimux = GST_AVIMUX (element);
507
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;
512   }
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;
517   }
518   else {
519     g_warning ("avimux: this is not our template!\n");
520     return NULL;
521   }
522
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);
531   
532   return newpad;
533 }
534
535 /* maybe some of these functions should be moved to riff.h? */
536
537 /* DISCLAIMER: this function is ugly. So be it (i.e. it makes the rest easier) */
538
539 static GstBuffer *
540 gst_avimux_riff_get_avi_header (GstAviMux *avimux)
541 {
542   GstBuffer *buffer;
543   guint8 *buffdata;
544   guint16 temp16;
545   guint32 temp32;
546
547   buffer = gst_buffer_new();
548
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 */
556   }
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 */
560   }
561   /* this is the "riff size" */
562   avimux->header_size = GST_BUFFER_SIZE(buffer);
563   GST_BUFFER_SIZE(buffer) += 12; /* avi data header */
564
565   /* allocate the buffer */
566   buffdata = GST_BUFFER_DATA(buffer) = g_malloc(GST_BUFFER_SIZE(buffer));
567
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;
609
610   if (avimux->video_pad_connected)
611   {
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;
617     /* generic header */
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;
673   }
674
675   if (avimux->audio_pad_connected)
676   {
677     /* audio header */
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;
682     /* generic header */
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;
728   }
729
730   if (avimux->video_pad_connected)
731   {
732     /* odml header */
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;
742   }
743
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);
749
750   return buffer;
751 }
752
753 static GstBuffer *
754 gst_avimux_riff_get_avix_header (guint32 datax_size)
755 {
756   GstBuffer *buffer;
757   guint8 *buffdata;
758   guint32 temp32;
759
760   buffer = gst_buffer_new();
761   GST_BUFFER_SIZE(buffer) = 24;
762   buffdata = GST_BUFFER_DATA(buffer) = g_malloc(GST_BUFFER_SIZE(buffer));
763
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);
772
773   return buffer;
774 }
775
776 static GstBuffer *
777 gst_avimux_riff_get_video_header (guint32 video_frame_size)
778 {
779   GstBuffer *buffer;
780   guint32 temp32;
781
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);
788
789   return buffer;
790 }
791
792 static GstBuffer *
793 gst_avimux_riff_get_audio_header (guint32 audio_sample_size)
794 {
795   GstBuffer *buffer;
796   guint32 temp32;
797
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);
804
805   return buffer;
806 }
807
808 /* some other usable functions (thankyou xawtv ;-) ) */
809
810 static void
811 gst_avimux_add_index (GstAviMux *avimux, guchar *code, guint32 flags, guint32 size)
812 {
813   if (avimux->idx_index == avimux->idx_count)
814   {
815     avimux->idx_count += 256;
816     avimux->idx = realloc(avimux->idx, avimux->idx_count*sizeof(gst_riff_index_entry));
817   }
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);
822   avimux->idx_index++;
823   avimux->idx_offset += size;
824 }
825
826 static void
827 gst_avimux_write_index (GstAviMux *avimux)
828 {
829   GstBuffer *buffer;
830   guint32 temp32;
831
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);
839
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);
846
847   avimux->idx_size += avimux->idx_index * sizeof(gst_riff_index_entry) + 8;
848
849   /* update header */
850   avimux->avi_hdr.flags |= GST_RIFF_AVIH_HASINDEX;
851 }
852
853 static void
854 gst_avimux_bigfile(GstAviMux *avimux, gboolean last)
855 {
856   GstBuffer *header;
857   GstEvent *event;
858     
859   if (avimux->is_bigfile)
860   {
861     /* sarch back */
862     event = gst_event_new_seek (GST_FORMAT_BYTES | 
863                                 GST_SEEK_METHOD_SET | 
864                                 GST_SEEK_FLAG_FLUSH, 
865                                 avimux->avix_start);
866     /* if the event succeeds */
867     if (gst_pad_send_event(GST_PAD_PEER(avimux->srcpad), event)) {
868
869       /* rewrite AVIX header */
870       header = gst_avimux_riff_get_avix_header(avimux->datax_size);
871       gst_pad_push(avimux->srcpad, header);
872
873       /* go back to current location */
874       event = gst_event_new_seek (GST_FORMAT_BYTES | 
875                                   GST_SEEK_METHOD_SET | 
876                                   GST_SEEK_FLAG_FLUSH, 
877                                   avimux->total_data);
878       gst_pad_send_event(GST_PAD_PEER(avimux->srcpad), event);
879     }
880   }
881   avimux->avix_start = avimux->total_data;
882
883   if (last)
884     return;
885
886   avimux->is_bigfile = TRUE;
887   avimux->numx_frames = 0;
888   avimux->datax_size = 0;
889
890   header = gst_avimux_riff_get_avix_header(0);
891   avimux->total_data += GST_BUFFER_SIZE(header);
892   gst_pad_push(avimux->srcpad, header);
893 }
894
895 /* enough header blabla now, let's go on to actually writing the headers */
896
897 static void
898 gst_avimux_start_file (GstAviMux *avimux)
899 {
900   GstBuffer *header;
901
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;
910
911   avimux->idx_index = 0;
912   avimux->idx_offset = 0; /* see 10 lines below */
913   avimux->idx_size = 0;
914   avimux->idx_count = 0;
915   avimux->idx = NULL;
916
917   /* header */
918   avimux->avi_hdr.streams = avimux->video_pad_connected?1:0 + avimux->audio_pad_connected?1:0;
919   avimux->is_bigfile = FALSE;
920
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);
925
926   avimux->write_header = FALSE;
927   avimux->restart = FALSE;
928 }
929
930 static void
931 gst_avimux_stop_file (GstAviMux *avimux)
932 {
933   GstEvent *event;
934   GstBuffer *header;
935
936   /* if bigfile, rewrite header, else write indexes */
937   if (avimux->video_pad_connected)
938   {
939     if (avimux->is_bigfile)
940     {
941       gst_avimux_bigfile(avimux, TRUE);
942       avimux->idx_size = 0;
943     }
944     else
945     {
946       gst_avimux_write_index(avimux);
947     }
948   }
949
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)
955   {
956     if (avimux->auds_hdr.scale)
957       avimux->auds_hdr.length = avimux->audio_size/avimux->auds_hdr.scale;
958     else
959       avimux->auds_hdr.length = 0; /* urm...? FIXME! ;-) */
960   }
961
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) *
969                                 avimux->framerate *
970                                 avimux->vids.image_size;
971
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);
979
980   avimux->write_header = TRUE;
981 }
982
983 static void
984 gst_avimux_restart_file (GstAviMux *avimux)
985 {
986   GstEvent *event;
987
988   gst_avimux_stop_file(avimux);
989
990   event = gst_event_new(GST_EVENT_NEW_MEDIA);
991   gst_pad_send_event(avimux->srcpad, event);
992
993   gst_avimux_start_file(avimux);
994 }
995
996 /* handle events (search) */
997 static gboolean
998 gst_avimux_handle_event (GstPad *pad, GstEvent *event)
999 {
1000   GstAviMux *avimux;
1001   GstEventType type;
1002
1003   avimux = GST_AVIMUX (gst_pad_get_parent (pad));
1004   
1005   type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
1006
1007   switch (type) {
1008     case GST_EVENT_NEW_MEDIA:
1009       avimux->restart = TRUE;
1010       break;
1011     case GST_EVENT_EOS:
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;
1017       else
1018         g_warning("Unknown pad for EOS!");
1019       break;
1020     default:
1021       break;
1022   }
1023
1024   return TRUE;
1025 }
1026
1027
1028 /* fill the internal queue for each available pad */
1029 static void
1030 gst_avimux_fill_queue (GstAviMux *avimux)
1031 {
1032   GstBuffer *buffer;
1033
1034   if (!avimux->audio_buffer_queue &&
1035        GST_PAD_IS_USABLE(avimux->audiosinkpad) &&
1036       !avimux->audio_pad_eos)
1037   {
1038     while (1)
1039     {
1040       buffer = gst_pad_pull(avimux->audiosinkpad);
1041       if (GST_IS_EVENT(buffer))
1042       {
1043         gst_avimux_handle_event(avimux->audiosinkpad, GST_EVENT(buffer));
1044       }
1045       else
1046       {
1047         avimux->audio_buffer_queue = buffer;
1048         break;
1049       }
1050     }
1051   }
1052
1053   if (!avimux->video_buffer_queue &&
1054        GST_PAD_IS_USABLE(avimux->videosinkpad) &&
1055       !avimux->video_pad_eos)
1056   {
1057     while (1)
1058     {
1059       buffer = gst_pad_pull(avimux->videosinkpad);
1060       if (GST_IS_EVENT(buffer))
1061       {
1062         gst_avimux_handle_event(avimux->videosinkpad, GST_EVENT(buffer));
1063       }
1064       else
1065       {
1066         avimux->video_buffer_queue = buffer;
1067         if (avimux->framerate < 0)
1068           avimux->framerate = gst_video_frame_rate(GST_PAD_PEER(avimux->videosinkpad));
1069         break;
1070       }
1071     }
1072   }
1073 }
1074
1075
1076 /* send extra 'padding' data */
1077 static void
1078 gst_avimux_send_pad_data (GstAviMux *avimux,
1079                           gulong     num_bytes)
1080 {
1081   GstBuffer *buffer;
1082
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);
1087
1088   gst_pad_push(avimux->srcpad, buffer);
1089 }
1090
1091 /* do audio buffer */
1092 static void
1093 gst_avimux_do_audio_buffer (GstAviMux *avimux)
1094 {
1095   GstBuffer *data = avimux->audio_buffer_queue, *header;
1096   gulong total_size, pad_bytes;
1097
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;
1102
1103   if (avimux->is_bigfile)
1104   {
1105     avimux->datax_size += total_size;
1106   }
1107   else
1108   {
1109     avimux->data_size += total_size;
1110     avimux->audio_size += GST_BUFFER_SIZE(data);
1111     gst_avimux_add_index(avimux, "01wb", 0x0, total_size);
1112   }
1113
1114   gst_pad_push(avimux->srcpad, header);
1115   pad_bytes = ((GST_BUFFER_SIZE(data)+3)&~3) - GST_BUFFER_SIZE(data);
1116   if (pad_bytes)
1117     if (GST_BUFFER_MAXSIZE(data) >= GST_BUFFER_SIZE(data) + pad_bytes)
1118     {
1119       GST_BUFFER_SIZE(data) += pad_bytes;
1120       pad_bytes = 0;
1121     }
1122   gst_pad_push(avimux->srcpad, data);
1123   if (pad_bytes)
1124     gst_avimux_send_pad_data(avimux, pad_bytes);
1125   avimux->audio_buffer_queue = NULL;
1126 }
1127
1128
1129 /* do video buffer */
1130 static void
1131 gst_avimux_do_video_buffer (GstAviMux *avimux)
1132 {
1133   GstBuffer *data = avimux->video_buffer_queue, *header;
1134   gulong total_size, pad_bytes;
1135
1136   if (avimux->restart)
1137     gst_avimux_restart_file(avimux);
1138
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)
1141   {
1142     if (avimux->enable_large_avi)
1143       gst_avimux_bigfile(avimux, FALSE);
1144     else
1145       gst_avimux_restart_file(avimux);
1146   }
1147
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++;
1152
1153   if (avimux->is_bigfile)
1154   {
1155     avimux->datax_size += total_size;
1156     avimux->numx_frames++;
1157   }
1158   else
1159   {
1160     avimux->data_size += total_size;
1161     avimux->num_frames++;
1162     gst_avimux_add_index(avimux, "00db", 0x12, total_size);
1163   }
1164
1165   gst_pad_push(avimux->srcpad, header);
1166   pad_bytes = ((GST_BUFFER_SIZE(data)+3)&~3) - GST_BUFFER_SIZE(data);
1167   if (pad_bytes)
1168     if (GST_BUFFER_MAXSIZE(data) >= GST_BUFFER_SIZE(data) + pad_bytes)
1169     {
1170       GST_BUFFER_SIZE(data) += pad_bytes;
1171       pad_bytes = 0;
1172     }
1173   gst_pad_push(avimux->srcpad, data);
1174   if (pad_bytes)
1175     gst_avimux_send_pad_data(avimux, pad_bytes);
1176   avimux->video_buffer_queue = NULL;
1177 }
1178
1179
1180 /* take the oldest buffer in our internal queue and push-it */
1181 static gboolean
1182 gst_avimux_do_one_buffer (GstAviMux *avimux)
1183 {
1184   if (avimux->video_buffer_queue &&
1185       avimux->audio_buffer_queue)
1186   {
1187     if (GST_BUFFER_TIMESTAMP(avimux->video_buffer_queue) <=
1188         GST_BUFFER_TIMESTAMP(avimux->audio_buffer_queue))
1189       gst_avimux_do_video_buffer(avimux);
1190     else
1191       gst_avimux_do_audio_buffer(avimux);
1192   }
1193   else if (avimux->video_buffer_queue ||
1194            avimux->audio_buffer_queue)
1195   {
1196     if (avimux->video_buffer_queue)
1197       gst_avimux_do_video_buffer(avimux);
1198     else
1199       gst_avimux_do_audio_buffer(avimux);
1200   }
1201   else
1202     return FALSE;
1203
1204   return TRUE;
1205 }
1206
1207
1208 static void
1209 gst_avimux_loop (GstElement *element)
1210 {
1211   GstAviMux *avimux;
1212
1213   avimux = GST_AVIMUX(element);
1214
1215   /* first fill queue (some elements only set caps when
1216    * flowing data), then write header */
1217   gst_avimux_fill_queue(avimux);
1218   
1219   if (avimux->write_header)
1220     gst_avimux_start_file(avimux);
1221
1222   gst_avimux_do_one_buffer(avimux);
1223 }
1224
1225 static void
1226 gst_avimux_get_property (GObject    *object,
1227                          guint      prop_id,
1228                          GValue     *value,
1229                          GParamSpec *pspec)
1230 {
1231   GstAviMux *avimux;
1232
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);
1236
1237   switch (prop_id)
1238   {
1239     case ARG_BIGFILE:
1240       g_value_set_boolean(value, avimux->enable_large_avi);
1241       break;
1242     default:
1243       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1244       break;
1245   }
1246 }
1247
1248 static void
1249 gst_avimux_set_property (GObject      *object,
1250                          guint         prop_id,
1251                          const GValue *value,
1252                          GParamSpec   *pspec)
1253 {
1254   GstAviMux *avimux;
1255
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);
1259
1260   switch (prop_id)
1261   {
1262     case ARG_BIGFILE:
1263       avimux->enable_large_avi = g_value_get_boolean(value);
1264       break;
1265     default:
1266       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1267       break;
1268   }
1269 }
1270
1271 static GstElementStateReturn
1272 gst_avimux_change_state (GstElement *element)
1273 {
1274   GstAviMux *avimux;
1275   gint transition = GST_STATE_TRANSITION (element);
1276
1277   g_return_val_if_fail(GST_IS_AVIMUX(element), GST_STATE_FAILURE);
1278   
1279   avimux = GST_AVIMUX(element);
1280
1281   switch (transition) {
1282     case GST_STATE_READY_TO_PAUSED:
1283       break;
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;
1287       break;
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);
1292       break;
1293     case GST_STATE_PAUSED_TO_READY:
1294       break;
1295   }
1296
1297   if (GST_ELEMENT_CLASS (parent_class)->change_state)
1298     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
1299
1300   return GST_STATE_SUCCESS;
1301 }
1302
1303 static gboolean
1304 plugin_init (GModule *module, GstPlugin *plugin)
1305 {
1306   GstElementFactory *factory;
1307
1308   if (!gst_library_load("gstvideo"))
1309     return FALSE;
1310
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);
1315
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));
1319   
1320   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
1321
1322   return TRUE;
1323 }
1324
1325 GstPluginDesc plugin_desc = {
1326   GST_VERSION_MAJOR,
1327   GST_VERSION_MINOR,
1328   "avimux",
1329   plugin_init
1330 };