1b02f411b2e83474c6b709291db63f44bf3904e2
[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 "gstavimux.h"
34
35 #ifndef LE_FROM_GUINT16
36 #define LE_FROM_GUINT16 GUINT16_FROM_LE
37 #endif
38
39 #ifndef LE_FROM_GUINT32
40 #define LE_FROM_GUINT32 GUINT32_FROM_LE
41 #endif
42
43
44 /* elementfactory information */
45 static GstElementDetails 
46 gst_avimux_details = 
47 {
48   ".avi mux",
49   "Mux/Video",
50   "Muxes audio and video into an avi stream",
51   VERSION,
52   "Ronald Bultje <rbultje@ronald.bitfreak.net>",
53   "(C) 2002",
54 };
55
56 /* AviMux signals and args */
57 enum {
58   /* FILL ME */
59   LAST_SIGNAL
60 };
61
62 enum {
63   ARG_0,
64   ARG_BIGFILE,
65 };
66
67 GST_PADTEMPLATE_FACTORY (src_factory,
68   "src",
69   GST_PAD_SRC,
70   GST_PAD_ALWAYS,
71   GST_CAPS_NEW (
72     "avimux_src_video",
73     "video/avi",
74     NULL
75   )
76 )
77     
78 GST_PADTEMPLATE_FACTORY (video_sink_factory,
79   "video_%02d",
80   GST_PAD_SINK,
81   GST_PAD_REQUEST,
82   GST_CAPS_NEW (
83     "avimux_sink_video",
84     "video/avi",
85       "format",   GST_PROPS_STRING ("strf_vids")
86   ),
87   GST_CAPS_NEW (
88     "avimux_sink_video",
89     "video/raw",
90       "format", GST_PROPS_LIST (
91                   GST_PROPS_FOURCC (GST_MAKE_FOURCC('Y','U','Y','2')),
92                   GST_PROPS_FOURCC (GST_MAKE_FOURCC('I','4','2','0')),
93                   GST_PROPS_FOURCC (GST_MAKE_FOURCC('Y','4','1','P'))
94                 ),
95       "width",  GST_PROPS_INT_RANGE (16, 4096),
96       "height", GST_PROPS_INT_RANGE (16, 4096)
97   ),
98   GST_CAPS_NEW (
99     "avimux_sink_video",
100     "video/raw",
101       "format", GST_PROPS_FOURCC (GST_MAKE_FOURCC('R','G','B',' ')),
102       "width",  GST_PROPS_INT_RANGE (16, 4096),
103       "height", GST_PROPS_INT_RANGE (16, 4096),
104       "depth",  GST_PROPS_LIST(
105                   GST_PROPS_INT(16),
106                   GST_PROPS_INT(16),
107                   GST_PROPS_INT(24),
108                   GST_PROPS_INT(32)
109                 ),
110       "bpp",    GST_PROPS_LIST(
111                   GST_PROPS_INT(15),
112                   GST_PROPS_INT(16),
113                   GST_PROPS_INT(24),
114                   GST_PROPS_INT(32)
115                 )
116   ),
117   GST_CAPS_NEW (
118     "avimux_sink_video",
119     "video/jpeg",
120       "width",  GST_PROPS_INT_RANGE (16, 4096),
121       "height", GST_PROPS_INT_RANGE (16, 4096)
122   )
123 )
124     
125 GST_PADTEMPLATE_FACTORY (audio_sink_factory,
126   "audio_%02d",
127   GST_PAD_SINK,
128   GST_PAD_REQUEST,
129   GST_CAPS_NEW (
130     "avimux_sink_audio",
131     "video/avi",
132       "format",   GST_PROPS_STRING ("strf_auds")
133   ),
134   GST_CAPS_NEW (
135     "avimux_sink_audio",
136     "audio/raw",
137       "format",           GST_PROPS_STRING ("int"),
138       "law",              GST_PROPS_INT (0),
139       "endianness",       GST_PROPS_INT (G_BYTE_ORDER),
140       "signed",           GST_PROPS_LIST (
141                             GST_PROPS_BOOLEAN (TRUE),
142                             GST_PROPS_BOOLEAN (FALSE)
143                           ),
144       "width",            GST_PROPS_LIST (
145                             GST_PROPS_INT (8),
146                             GST_PROPS_INT (16)
147                           ),
148       "depth",            GST_PROPS_LIST (
149                             GST_PROPS_INT (8),
150                             GST_PROPS_INT (16)
151                           ),
152       "rate",             GST_PROPS_INT_RANGE (11025, 44100),
153       "channels",         GST_PROPS_INT_RANGE (1, 2)
154   ),
155   GST_CAPS_NEW (
156     "avimux_sink_audio",
157     "audio/mp3",
158       NULL
159   )
160 )
161     
162
163 static void     gst_avimux_class_init                (GstAviMuxClass *klass);
164 static void     gst_avimux_init                      (GstAviMux      *avimux);
165
166 static void     gst_avimux_chain                     (GstPad         *pad,
167                                                       GstBuffer      *buf);
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 void 
232 gst_avimux_init (GstAviMux *avimux) 
233 {
234   gint i;
235   avimux->srcpad = gst_pad_new_from_template (
236                   GST_PADTEMPLATE_GET (src_factory), "src");
237   gst_element_add_pad (GST_ELEMENT (avimux), avimux->srcpad);
238
239   GST_FLAG_SET (GST_ELEMENT(avimux), GST_ELEMENT_EVENT_AWARE);
240   gst_pad_set_event_function(avimux->srcpad, gst_avimux_handle_event);
241
242   for (i=0;i<MAX_NUM_AUDIO_PADS;i++)
243     avimux->audiosinkpad[i] = NULL;
244   avimux->num_audio_pads = 0;
245   for (i=0;i<MAX_NUM_VIDEO_PADS;i++)
246     avimux->videosinkpad[i] = NULL;
247   avimux->num_video_pads = 0;
248
249   avimux->num_frames = 0;
250
251   /* audio/video/AVI header initialisation */
252   memset(&(avimux->avi_hdr),0,sizeof(gst_riff_avih));
253   memset(&(avimux->vids_hdr),0,sizeof(gst_riff_strh));
254   memset(&(avimux->vids),0,sizeof(gst_riff_strf_vids));
255   memset(&(avimux->auds_hdr),0,sizeof(gst_riff_strh));
256   memset(&(avimux->auds),0,sizeof(gst_riff_strf_auds));
257   avimux->vids_hdr.type = GST_MAKE_FOURCC('v','i','d','s');
258   avimux->vids_hdr.rate = 1000000;
259   avimux->auds_hdr.type = GST_MAKE_FOURCC('a','u','d','s');
260
261   avimux->idx = NULL;
262
263   avimux->write_header = TRUE;
264
265   avimux->enable_large_avi = TRUE;
266 }
267
268 static GstPadConnectReturn
269 gst_avimux_sinkconnect (GstPad *pad, GstCaps *vscaps)
270 {
271   GstAviMux *avimux;
272   GstCaps *caps;
273
274   avimux = GST_AVIMUX (gst_pad_get_parent (pad));
275
276   /* we are not going to act on variable caps */
277   if (!GST_CAPS_IS_FIXED (vscaps))
278     return GST_PAD_CONNECT_DELAYED;
279
280   GST_DEBUG (0, "avimux: sinkconnect triggered on %s\n", gst_pad_get_name (pad));
281
282   for (caps = vscaps; caps != NULL; caps = vscaps = vscaps->next)
283   {
284     const gchar* mimetype = gst_caps_get_mime(caps);
285
286     if (!strcmp (mimetype, "video/avi"))
287     {
288       const gchar* format = gst_caps_get_string(caps, "format");
289
290       if (!strncmp (format, "strf_vids", 9)) {
291         avimux->vids.size        = sizeof(gst_riff_strf_vids);
292         avimux->vids.width       = gst_caps_get_int (caps, "width");
293         avimux->vids.height      = gst_caps_get_int (caps, "height");
294         avimux->vids.planes      = gst_caps_get_int (caps, "planes");
295         avimux->vids.bit_cnt     = gst_caps_get_int (caps, "bit_cnt");
296         avimux->vids.compression = gst_caps_get_fourcc_int (caps, "compression");
297         avimux->vids.image_size  = gst_caps_get_int (caps, "image_size");
298         avimux->vids.xpels_meter = gst_caps_get_int (caps, "xpels_meter");
299         avimux->vids.ypels_meter = gst_caps_get_int (caps, "ypels_meter");
300         avimux->vids.num_colors  = gst_caps_get_int (caps, "num_colors");
301         avimux->vids.imp_colors  = gst_caps_get_int (caps, "imp_colors");
302       }
303       else if (!strncmp (format, "strf_auds", 9)) {
304         avimux->auds.format      = gst_caps_get_int (caps, "format");
305         avimux->auds.channels    = gst_caps_get_int (caps, "channels");
306         avimux->auds.rate        = gst_caps_get_int (caps, "rate");
307         avimux->auds.av_bps      = gst_caps_get_int (caps, "av_bps");
308         avimux->auds.blockalign  = gst_caps_get_int (caps, "blockalign");
309         avimux->auds.size        = gst_caps_get_int (caps, "size");
310       }
311       goto done;
312     }
313     else if (!strcmp (mimetype, "video/raw"))
314     {
315       switch (gst_caps_get_fourcc_int(caps, "format"))
316       {
317         case GST_MAKE_FOURCC('Y','U','Y','2'):
318         case GST_MAKE_FOURCC('I','4','2','0'):
319         case GST_MAKE_FOURCC('Y','4','1','P'):
320         case GST_MAKE_FOURCC('R','G','B',' '):
321           avimux->vids.size        = sizeof(gst_riff_strf_vids);
322           avimux->vids.width       = gst_caps_get_int (caps, "width");
323           avimux->vids.height      = gst_caps_get_int (caps, "height");
324           avimux->vids.planes      = 1;
325           switch (gst_caps_get_fourcc_int(caps, "format"))
326           {
327             case GST_MAKE_FOURCC('Y','U','Y','2'):
328               avimux->vids.bit_cnt     = 16; /* YUY2 */
329               break;
330             case GST_MAKE_FOURCC('R','G','B',' '):
331               avimux->vids.bit_cnt     = gst_caps_get_fourcc_int(caps, "bpp"); /* RGB */
332               break;
333             case GST_MAKE_FOURCC('Y','4','1','P'):
334             case GST_MAKE_FOURCC('I','4','2','0'):
335               avimux->vids.bit_cnt     = 12; /* Y41P or I420 */
336               break;
337           }
338           avimux->vids.compression = gst_caps_get_fourcc_int(caps, "format");
339           avimux->vids.image_size  = avimux->vids.height * avimux->vids.width;
340           goto done;
341         default:
342           break;
343       }
344     }
345     else if (!strcmp (mimetype, "video/jpeg"))
346     {
347       avimux->vids.size        = sizeof(gst_riff_strf_vids);
348       avimux->vids.width       = gst_caps_get_int (caps, "width");
349       avimux->vids.height      = gst_caps_get_int (caps, "height");
350       avimux->vids.planes      = 1;
351       avimux->vids.bit_cnt     = 24;
352       avimux->vids.compression = GST_MAKE_FOURCC('M','J','P','G');
353       avimux->vids.image_size  = avimux->vids.height * avimux->vids.width;
354       goto done;
355     }
356     else if (!strcmp (mimetype, "audio/raw"))
357     {
358 printf("WE HAVE AUDIO\n");
359       avimux->auds.format      = GST_RIFF_WAVE_FORMAT_PCM;
360       avimux->auds.channels    = gst_caps_get_int (caps, "channels");
361       avimux->auds.rate        = gst_caps_get_int (caps, "rate");
362       avimux->auds.av_bps      = gst_caps_get_int (caps, "width") * avimux->auds.rate *
363                                                avimux->auds.channels / 8;
364       avimux->auds.blockalign  = gst_caps_get_int (caps, "width") * avimux->auds.channels/8;
365       avimux->auds.size        = gst_caps_get_int (caps, "depth");
366       goto done;
367     }
368     else if (!strcmp (mimetype, "audio/mp3"))
369     {
370       /* we don't need to do anything here, compressed mp3 contains it all */
371       avimux->auds.format      = gst_caps_get_int(caps, "layer")==3?
372                                    GST_RIFF_WAVE_FORMAT_MPEGL3:GST_RIFF_WAVE_FORMAT_MPEGL12;
373       goto done;
374     }
375   }
376   return GST_PAD_CONNECT_REFUSED;
377
378 done:
379   return GST_PAD_CONNECT_OK;
380 }
381
382 static GstPad*
383 gst_avimux_request_new_pad (GstElement     *element,
384                             GstPadTemplate *templ,
385                             const gchar    *req_name)
386 {
387   GstAviMux *avimux;
388   gchar *name = NULL;
389   GstPad *newpad;
390   
391   g_return_val_if_fail (templ != NULL, NULL);
392
393   if (templ->direction != GST_PAD_SINK) {
394     g_warning ("avimux: request pad that is not a SINK pad\n");
395     return NULL;
396   }
397
398   g_return_val_if_fail (GST_IS_AVIMUX (element), NULL);
399
400   avimux = GST_AVIMUX (element);
401
402   if (templ == GST_PADTEMPLATE_GET (audio_sink_factory)) {
403 printf("AUDIOPAD REQUESTED\n");
404     g_return_val_if_fail(avimux->num_audio_pads == 0 /*< MAX_NUM_AUDIO_PADS*/, NULL);
405     name = g_strdup_printf ("audio_%02d", avimux->num_audio_pads);
406     newpad = gst_pad_new_from_template (templ, name);
407     gst_pad_set_element_private (newpad, GINT_TO_POINTER (avimux->num_audio_pads));
408
409     avimux->audiosinkpad[avimux->num_audio_pads] = newpad;
410     avimux->num_audio_pads++;
411   }
412   else if (templ == GST_PADTEMPLATE_GET (video_sink_factory)) {
413 printf("VIDEOPAD %d REQUESTED\n", avimux->num_video_pads);
414     g_return_val_if_fail(avimux->num_video_pads == 0 /*< MAX_NUM_VIDEO_PADS*/, NULL);
415     name = g_strdup_printf ("video_%02d", avimux->num_video_pads);
416     newpad = gst_pad_new_from_template (templ, name);
417     gst_pad_set_element_private (newpad, GINT_TO_POINTER (avimux->num_video_pads));
418
419     avimux->videosinkpad[avimux->num_video_pads] = newpad;
420     avimux->num_video_pads++;
421   }
422   else {
423     g_warning ("avimux: this is not our template!\n");
424     return NULL;
425   }
426
427   gst_pad_set_chain_function (newpad, gst_avimux_chain);
428   gst_pad_set_connect_function (newpad, gst_avimux_sinkconnect);
429   gst_element_add_pad (element, newpad);
430   
431   return newpad;
432 }
433
434 /* maybe some of these functions should be moved to riff.h? */
435
436 /* DISCLAIMER: this function is ugly. So be it (i.e. it makes the rest easier) */
437
438 static GstBuffer *
439 gst_avimux_riff_get_avi_header (GstAviMux *avimux)
440 {
441   GstBuffer *buffer;
442   guint8 *buffdata;
443   guint16 temp16;
444   guint32 temp32;
445
446   buffer = gst_buffer_new();
447
448   /* first, let's see what actually needs to be in the buffer */
449   GST_BUFFER_SIZE(buffer) = 0;
450   GST_BUFFER_SIZE(buffer) += 32 + sizeof(gst_riff_avih); /* avi header */
451   if (avimux->num_video_pads)
452   { /* we have video */
453     GST_BUFFER_SIZE(buffer) += 28 + sizeof(gst_riff_strh) + sizeof(gst_riff_strf_vids); /* vid hdr */
454     GST_BUFFER_SIZE(buffer) += 24; /* odml header */
455   }
456   if (avimux->num_audio_pads)
457   { /* we have audio */
458     GST_BUFFER_SIZE(buffer) += 28 + sizeof(gst_riff_strh) + sizeof(gst_riff_strf_auds); /* aud hdr */
459   }
460   /* this is the "riff size" */
461   avimux->header_size = GST_BUFFER_SIZE(buffer);
462   GST_BUFFER_SIZE(buffer) += 12; /* avi data header */
463
464   /* allocate the buffer */
465   buffdata = GST_BUFFER_DATA(buffer) = g_malloc(GST_BUFFER_SIZE(buffer));
466
467   /* avi header metadata */
468   memcpy(buffdata, "RIFF", 4); buffdata += 4;
469   temp32 = LE_FROM_GUINT32(avimux->header_size + avimux->idx_size + avimux->data_size);
470   memcpy(buffdata, &temp32, 4); buffdata += 4;
471   memcpy(buffdata, "AVI ", 4); buffdata += 4;
472   memcpy(buffdata, "LIST", 4); buffdata += 4;
473   temp32 = LE_FROM_GUINT32(avimux->header_size - 4*5);
474   memcpy(buffdata, &temp32, 4); buffdata += 4;
475   memcpy(buffdata, "hdrl", 4); buffdata += 4;
476   memcpy(buffdata, "avih", 4); buffdata += 4;
477   temp32 = LE_FROM_GUINT32(sizeof(gst_riff_avih));
478   memcpy(buffdata, &temp32, 4); buffdata += 4;
479   /* the AVI header itself */
480   temp32 = LE_FROM_GUINT32(avimux->avi_hdr.us_frame);
481   memcpy(buffdata, &temp32, 4); buffdata += 4;
482   temp32 = LE_FROM_GUINT32(avimux->avi_hdr.max_bps);
483   memcpy(buffdata, &temp32, 4); buffdata += 4;
484   temp32 = LE_FROM_GUINT32(avimux->avi_hdr.pad_gran);
485   memcpy(buffdata, &temp32, 4); buffdata += 4;
486   temp32 = LE_FROM_GUINT32(avimux->avi_hdr.flags);
487   memcpy(buffdata, &temp32, 4); buffdata += 4;
488   temp32 = LE_FROM_GUINT32(avimux->avi_hdr.tot_frames);
489   memcpy(buffdata, &temp32, 4); buffdata += 4;
490   temp32 = LE_FROM_GUINT32(avimux->avi_hdr.init_frames);
491   memcpy(buffdata, &temp32, 4); buffdata += 4;
492   temp32 = LE_FROM_GUINT32(avimux->avi_hdr.streams);
493   memcpy(buffdata, &temp32, 4); buffdata += 4;
494   temp32 = LE_FROM_GUINT32(avimux->avi_hdr.bufsize);
495   memcpy(buffdata, &temp32, 4); buffdata += 4;
496   temp32 = LE_FROM_GUINT32(avimux->avi_hdr.width);
497   memcpy(buffdata, &temp32, 4); buffdata += 4;
498   temp32 = LE_FROM_GUINT32(avimux->avi_hdr.height);
499   memcpy(buffdata, &temp32, 4); buffdata += 4;
500   temp32 = LE_FROM_GUINT32(avimux->avi_hdr.scale);
501   memcpy(buffdata, &temp32, 4); buffdata += 4;
502   temp32 = LE_FROM_GUINT32(avimux->avi_hdr.rate);
503   memcpy(buffdata, &temp32, 4); buffdata += 4;
504   temp32 = LE_FROM_GUINT32(avimux->avi_hdr.start);
505   memcpy(buffdata, &temp32, 4); buffdata += 4;
506   temp32 = LE_FROM_GUINT32(avimux->avi_hdr.length);
507   memcpy(buffdata, &temp32, 4); buffdata += 4;
508
509   if (avimux->num_video_pads)
510   {
511     /* video header metadata */
512     memcpy(buffdata, "LIST", 4); buffdata += 4;
513     temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh) + sizeof(gst_riff_strf_vids) + 4*5);
514     memcpy(buffdata, &temp32, 4); buffdata += 4;
515     memcpy(buffdata, "strl", 4); buffdata += 4;
516     /* generic header */
517     memcpy(buffdata, "strh", 4); buffdata += 4;
518     temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh));
519     memcpy(buffdata, &temp32, 4); buffdata += 4;
520     /* the actual header */
521     temp32 = LE_FROM_GUINT32(avimux->vids_hdr.type);
522     memcpy(buffdata, &temp32, 4); buffdata += 4;
523     temp32 = LE_FROM_GUINT32(avimux->vids_hdr.fcc_handler);
524     memcpy(buffdata, &temp32, 4); buffdata += 4;
525     temp32 = LE_FROM_GUINT32(avimux->vids_hdr.flags);
526     memcpy(buffdata, &temp32, 4); buffdata += 4;
527     temp32 = LE_FROM_GUINT32(avimux->vids_hdr.priority);
528     memcpy(buffdata, &temp32, 4); buffdata += 4;
529     temp32 = LE_FROM_GUINT32(avimux->vids_hdr.init_frames);
530     memcpy(buffdata, &temp32, 4); buffdata += 4;
531     temp32 = LE_FROM_GUINT32(avimux->vids_hdr.scale);
532     memcpy(buffdata, &temp32, 4); buffdata += 4;
533     temp32 = LE_FROM_GUINT32(avimux->vids_hdr.rate);
534     memcpy(buffdata, &temp32, 4); buffdata += 4;
535     temp32 = LE_FROM_GUINT32(avimux->vids_hdr.start);
536     memcpy(buffdata, &temp32, 4); buffdata += 4;
537     temp32 = LE_FROM_GUINT32(avimux->vids_hdr.length);
538     memcpy(buffdata, &temp32, 4); buffdata += 4;
539     temp32 = LE_FROM_GUINT32(avimux->vids_hdr.bufsize);
540     memcpy(buffdata, &temp32, 4); buffdata += 4;
541     temp32 = LE_FROM_GUINT32(avimux->vids_hdr.quality);
542     memcpy(buffdata, &temp32, 4); buffdata += 4;
543     temp32 = LE_FROM_GUINT32(avimux->vids_hdr.samplesize);
544     memcpy(buffdata, &temp32, 4); buffdata += 4;
545     /* the video header */
546     memcpy(buffdata, "strf", 4); buffdata += 4;
547     temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strf_vids));
548     memcpy(buffdata, &temp32, 4); buffdata += 4;
549     /* the actual header */
550     temp32 = LE_FROM_GUINT32(avimux->vids.size);
551     memcpy(buffdata, &temp32, 4); buffdata += 4;
552     temp32 = LE_FROM_GUINT32(avimux->vids.width);
553     memcpy(buffdata, &temp32, 4); buffdata += 4;
554     temp32 = LE_FROM_GUINT32(avimux->vids.height);
555     memcpy(buffdata, &temp32, 4); buffdata += 4;
556     temp16 = LE_FROM_GUINT16(avimux->vids.planes);
557     memcpy(buffdata, &temp16, 2); buffdata += 2;
558     temp16 = LE_FROM_GUINT16(avimux->vids.bit_cnt);
559     memcpy(buffdata, &temp16, 2); buffdata += 2;
560     temp32 = LE_FROM_GUINT32(avimux->vids.compression);
561     memcpy(buffdata, &temp32, 4); buffdata += 4;
562     temp32 = LE_FROM_GUINT32(avimux->vids.image_size);
563     memcpy(buffdata, &temp32, 4); buffdata += 4;
564     temp32 = LE_FROM_GUINT32(avimux->vids.xpels_meter);
565     memcpy(buffdata, &temp32, 4); buffdata += 4;
566     temp32 = LE_FROM_GUINT32(avimux->vids.ypels_meter);
567     memcpy(buffdata, &temp32, 4); buffdata += 4;
568     temp32 = LE_FROM_GUINT32(avimux->vids.num_colors);
569     memcpy(buffdata, &temp32, 4); buffdata += 4;
570     temp32 = LE_FROM_GUINT32(avimux->vids.imp_colors);
571     memcpy(buffdata, &temp32, 4); buffdata += 4;
572   }
573
574   if (avimux->num_audio_pads)
575   {
576     /* audio header */
577     memcpy(buffdata, "LIST", 4); buffdata += 4;
578     temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh) + sizeof(gst_riff_strf_auds) + 4*5);
579     memcpy(buffdata, &temp32, 4); buffdata += 4;
580     memcpy(buffdata, "strl", 4); buffdata += 4;
581     /* generic header */
582     memcpy(buffdata, "strh", 4); buffdata += 4;
583     temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strh));
584     memcpy(buffdata, &temp32, 4); buffdata += 4;
585     /* the actual header */
586     temp32 = LE_FROM_GUINT32(avimux->auds_hdr.type);
587     memcpy(buffdata, &temp32, 4); buffdata += 4;
588     temp32 = LE_FROM_GUINT32(avimux->auds_hdr.fcc_handler);
589     memcpy(buffdata, &temp32, 4); buffdata += 4;
590     temp32 = LE_FROM_GUINT32(avimux->auds_hdr.flags);
591     memcpy(buffdata, &temp32, 4); buffdata += 4;
592     temp32 = LE_FROM_GUINT32(avimux->auds_hdr.priority);
593     memcpy(buffdata, &temp32, 4); buffdata += 4;
594     temp32 = LE_FROM_GUINT32(avimux->auds_hdr.init_frames);
595     memcpy(buffdata, &temp32, 4); buffdata += 4;
596     temp32 = LE_FROM_GUINT32(avimux->auds_hdr.scale);
597     memcpy(buffdata, &temp32, 4); buffdata += 4;
598     temp32 = LE_FROM_GUINT32(avimux->auds_hdr.rate);
599     memcpy(buffdata, &temp32, 4); buffdata += 4;
600     temp32 = LE_FROM_GUINT32(avimux->auds_hdr.start);
601     memcpy(buffdata, &temp32, 4); buffdata += 4;
602     temp32 = LE_FROM_GUINT32(avimux->auds_hdr.length);
603     memcpy(buffdata, &temp32, 4); buffdata += 4;
604     temp32 = LE_FROM_GUINT32(avimux->auds_hdr.bufsize);
605     memcpy(buffdata, &temp32, 4); buffdata += 4;
606     temp32 = LE_FROM_GUINT32(avimux->auds_hdr.quality);
607     memcpy(buffdata, &temp32, 4); buffdata += 4;
608     temp32 = LE_FROM_GUINT32(avimux->auds_hdr.samplesize);
609     memcpy(buffdata, &temp32, 4); buffdata += 4;
610     /* the audio header */
611     memcpy(buffdata, "strf", 4); buffdata += 4;
612     temp32 = LE_FROM_GUINT32(sizeof(gst_riff_strf_vids));
613     memcpy(buffdata, &temp32, 4); buffdata += 4;
614     /* the actual header */
615     temp16 = LE_FROM_GUINT16(avimux->auds.format);
616     memcpy(buffdata, &temp16, 2); buffdata += 2;
617     temp16 = LE_FROM_GUINT16(avimux->auds.channels);
618     memcpy(buffdata, &temp16, 2); buffdata += 2;
619     temp32 = LE_FROM_GUINT32(avimux->auds.rate);
620     memcpy(buffdata, &temp32, 4); buffdata += 4;
621     temp32 = LE_FROM_GUINT32(avimux->auds.av_bps);
622     memcpy(buffdata, &temp32, 4); buffdata += 4;
623     temp16 = LE_FROM_GUINT16(avimux->auds.blockalign);
624     memcpy(buffdata, &temp16, 2); buffdata += 2;
625     temp16 = LE_FROM_GUINT16(avimux->auds.size);
626     memcpy(buffdata, &temp16, 2); buffdata += 2;
627   }
628
629   if (avimux->num_video_pads)
630   {
631     /* odml header */
632     memcpy(buffdata, "LIST", 4); buffdata += 4;
633     temp32 = LE_FROM_GUINT32(sizeof(guint32)+4*3);
634     memcpy(buffdata, &temp32, 4); buffdata += 4;
635     memcpy(buffdata, "odml", 4); buffdata += 4;
636     memcpy(buffdata, "dmlh", 4); buffdata += 4;
637     temp32 = LE_FROM_GUINT32(sizeof(guint32));
638     memcpy(buffdata, &temp32, 4); buffdata += 4;
639     temp32 = LE_FROM_GUINT32(avimux->total_frames);
640     memcpy(buffdata, &temp32, 4); buffdata += 4;
641   }
642
643   /* avi data header */
644   memcpy(buffdata, "LIST", 4); buffdata += 4;
645   temp32 = LE_FROM_GUINT32(avimux->data_size);
646   memcpy(buffdata, &temp32, 4); buffdata += 4;
647   memcpy(buffdata, "movi", 4);
648
649   return buffer;
650 }
651
652 static GstBuffer *
653 gst_avimux_riff_get_avix_header (guint32 datax_size)
654 {
655   GstBuffer *buffer;
656   guint8 *buffdata;
657   guint32 temp32;
658
659   buffer = gst_buffer_new();
660   GST_BUFFER_SIZE(buffer) = 24;
661   buffdata = GST_BUFFER_DATA(buffer) = g_malloc(GST_BUFFER_SIZE(buffer));
662
663   memcpy(buffdata, "LIST", 4); buffdata += 4;
664   temp32 = LE_FROM_GUINT32(datax_size+4*4);
665   memcpy(buffdata, &temp32, 4); buffdata += 4;
666   memcpy(buffdata, "AVIX", 4); buffdata += 4;
667   memcpy(buffdata, "LIST", 4); buffdata += 4;
668   temp32 = LE_FROM_GUINT32(datax_size);
669   memcpy(buffdata, &temp32, 4); buffdata += 4;
670   memcpy(buffdata, "movi", 4);
671
672   return buffer;
673 }
674
675 static GstBuffer *
676 gst_avimux_riff_get_video_header (guint32 video_frame_size)
677 {
678   GstBuffer *buffer;
679   guint32 temp32;
680
681   buffer = gst_buffer_new();
682   GST_BUFFER_DATA(buffer) = g_malloc(8);
683   GST_BUFFER_SIZE(buffer) = 8;
684   memcpy(GST_BUFFER_DATA(buffer), "00db", 4);
685   temp32 = LE_FROM_GUINT32(video_frame_size);
686   memcpy(GST_BUFFER_DATA(buffer)+4, &temp32, 4);
687
688   return buffer;
689 }
690
691 static GstBuffer *
692 gst_avimux_riff_get_audio_header (guint32 audio_sample_size)
693 {
694   GstBuffer *buffer;
695   guint32 temp32;
696
697   buffer = gst_buffer_new();
698   GST_BUFFER_DATA(buffer) = g_malloc(8);
699   GST_BUFFER_SIZE(buffer) = 8;
700   memcpy(GST_BUFFER_DATA(buffer), "01wb", 4);
701   temp32 = LE_FROM_GUINT32(audio_sample_size);
702   memcpy(GST_BUFFER_DATA(buffer)+4, &temp32, 4);
703
704   return buffer;
705 }
706
707 /* some other usable functions (thankyou xawtv ;-) ) */
708
709 static void
710 gst_avimux_add_index (GstAviMux *avimux, guint32 fourcc, guint32 flags, guint32 size)
711 {
712   guint32 temp32;
713   
714   if (avimux->idx_index == avimux->idx_count)
715   {
716     avimux->idx_count += 256;
717     avimux->idx = realloc(avimux->idx, avimux->idx_count*sizeof(gst_riff_index_entry));
718   }
719   temp32 = LE_FROM_GUINT32(fourcc);
720   memcpy(&(avimux->idx[avimux->idx_index].id), &temp32, 4);
721   avimux->idx[avimux->idx_index].flags = LE_FROM_GUINT32(flags);
722   avimux->idx[avimux->idx_index].offset = LE_FROM_GUINT32(avimux->idx_offset-avimux->header_size-8);
723   avimux->idx[avimux->idx_index].size = LE_FROM_GUINT32(size);
724   avimux->idx_index++;
725   avimux->idx_offset += size + sizeof(gst_riff_index_entry);
726 }
727
728 static void
729 gst_avimux_write_index (GstAviMux *avimux)
730 {
731   GstBuffer *buffer;
732   guint32 temp32;
733
734   buffer = gst_buffer_new();
735   GST_BUFFER_SIZE(buffer) = 8;
736   GST_BUFFER_DATA(buffer) = g_malloc(8);
737   memcpy(GST_BUFFER_DATA(buffer), "idx1", 4);
738   temp32 = LE_FROM_GUINT32(avimux->idx_index * sizeof(gst_riff_index_entry)); 
739   memcpy(GST_BUFFER_DATA(buffer)+4, &temp32, 4);
740   gst_pad_push(avimux->srcpad, buffer);
741
742   buffer = gst_buffer_new();
743   GST_BUFFER_SIZE(buffer) = avimux->idx_index * sizeof(gst_riff_index_entry);
744   GST_BUFFER_DATA(buffer) = (unsigned char*) avimux->idx;
745   avimux->idx = NULL; /* will be free()'ed by gst_buffer_unref() */
746   avimux->total_data += GST_BUFFER_SIZE(buffer);
747   gst_pad_push(avimux->srcpad, buffer);
748
749   avimux->idx_size += avimux->idx_index * sizeof(gst_riff_index_entry) + 8;
750
751   /* update header */
752   avimux->avi_hdr.flags |= GST_RIFF_AVIH_HASINDEX;
753 }
754
755 static void
756 gst_avimux_bigfile(GstAviMux *avimux, gboolean last)
757 {
758   GstBuffer *header;
759   GstEvent *event;
760     
761   if (avimux->is_bigfile)
762   {
763     /* sarch back */
764     event = gst_event_new_seek(GST_SEEK_BYTEOFFSET_SET, avimux->avix_start, TRUE);
765     gst_pad_send_event(avimux->srcpad, event);
766
767     /* rewrite AVIX header */
768     header = gst_avimux_riff_get_avix_header(avimux->datax_size);
769     gst_pad_push(avimux->srcpad, header);
770
771     /* go back to current location */
772     event = gst_event_new_seek(GST_SEEK_BYTEOFFSET_SET, avimux->total_data, TRUE);
773     gst_pad_send_event(avimux->srcpad, event);
774   }
775   avimux->avix_start = avimux->total_data;
776
777   if (last)
778     return;
779
780   avimux->is_bigfile = TRUE;
781   avimux->numx_frames = 0;
782   avimux->datax_size = 0;
783
784   header = gst_avimux_riff_get_avix_header(0);
785   avimux->total_data += GST_BUFFER_SIZE(header);
786   gst_pad_push(avimux->srcpad, header);
787 }
788
789 /* enough header blabla now, let's go on to actually writing the headers */
790
791 static void
792 gst_avimux_start_file (GstAviMux *avimux)
793 {
794   GstBuffer *header;
795
796   avimux->total_data = 0;
797   avimux->total_frames = 0;
798   avimux->data_size = 4; /* ? */
799   avimux->datax_size = 0;
800   avimux->num_frames = 0;
801   avimux->numx_frames = 0;
802   avimux->audio_size = 0;
803
804   avimux->idx_index = 0;
805   avimux->idx_offset = avimux->header_size + 12;
806   avimux->idx_size = 0;
807   avimux->idx_count = 0;
808   avimux->idx = NULL;
809
810   /* header */
811   avimux->avi_hdr.streams = avimux->num_video_pads + avimux->num_audio_pads;
812   avimux->is_bigfile = FALSE;
813
814   header = gst_avimux_riff_get_avi_header(avimux);
815   avimux->total_data += GST_BUFFER_SIZE(header);
816   gst_pad_push(avimux->srcpad, header);
817
818   avimux->write_header = FALSE;
819   avimux->restart = FALSE;
820 }
821
822 static void
823 gst_avimux_stop_file (GstAviMux *avimux)
824 {
825   GstEvent *event;
826   GstBuffer *header;
827   gfloat fps = 25.0;
828
829   /* if bigfile, rewrite header, else write indexes */
830   if (avimux->num_video_pads)
831   {
832     if (avimux->is_bigfile)
833     {
834       gst_avimux_bigfile(avimux, TRUE);
835       avimux->idx_size = 0;
836     }
837     else
838     {
839       gst_avimux_write_index(avimux);
840     }
841   }
842
843   /* statistics/total_frames/... */
844   avimux->avi_hdr.tot_frames = avimux->num_frames;
845   if (avimux->num_video_pads)
846     avimux->vids_hdr.length = avimux->num_frames;
847   if (avimux->num_audio_pads)
848     avimux->auds_hdr.length = avimux->audio_size/avimux->auds_hdr.scale;
849
850   /* TODO: fps calculation!! */
851   avimux->avi_hdr.us_frame = 1000000/fps;
852   avimux->avi_hdr.max_bps = 0;
853   if (avimux->num_audio_pads)
854     avimux->avi_hdr.max_bps += avimux->auds.av_bps;
855   if (avimux->num_video_pads)
856     avimux->avi_hdr.max_bps += avimux->vids.bit_cnt/8 * fps;
857
858   /* seek and rewrite the header */
859   header = gst_avimux_riff_get_avi_header(avimux);
860   event = gst_event_new_seek(GST_SEEK_BYTEOFFSET_SET, 0, TRUE);
861   gst_pad_send_event(avimux->srcpad, event);
862   gst_pad_push(avimux->srcpad, header);
863
864   avimux->write_header = TRUE;
865 }
866
867 static void
868 gst_avimux_restart_file (GstAviMux *avimux)
869 {
870   GstEvent *event;
871
872   gst_avimux_stop_file(avimux);
873
874   event = gst_event_new(GST_EVENT_NEW_MEDIA);
875   gst_pad_send_event(avimux->srcpad, event);
876
877   gst_avimux_start_file(avimux);
878 }
879
880 /* handle events (search) */
881 static gboolean
882 gst_avimux_handle_event (GstPad *pad, GstEvent *event)
883 {
884   GstAviMux *avimux;
885   GstEventType type;
886
887   avimux = GST_AVIMUX (gst_pad_get_parent (pad));
888   
889   type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
890
891   switch (type) {
892     case GST_EVENT_NEW_MEDIA:
893       avimux->restart = TRUE;
894       break;
895     default:
896       break;
897   }
898
899   return TRUE;
900 }
901
902 static void
903 gst_avimux_chain (GstPad *pad, GstBuffer *buf)
904 {
905   GstAviMux *avimux;
906   GstBuffer *newbuf;
907   const gchar *padname = gst_pad_get_name (pad);
908
909   g_return_if_fail (pad != NULL);
910   g_return_if_fail (GST_IS_PAD (pad));
911   g_return_if_fail (buf != NULL);
912   g_return_if_fail (GST_BUFFER_DATA (buf) != NULL);
913
914   avimux = GST_AVIMUX (gst_pad_get_parent (pad));
915   
916   if (GST_IS_EVENT(buf))
917   {
918     gst_avimux_handle_event(pad, GST_EVENT(buf));
919     return;
920   }
921
922
923   if (avimux->write_header)
924     gst_avimux_start_file(avimux);
925
926   if (strncmp(padname, "audio_", 6) == 0)
927   {
928 printf("AUDIODATA GIVEN\n");
929     /* write a audio header + index entry */
930     newbuf = gst_avimux_riff_get_audio_header(GST_BUFFER_SIZE(buf));
931     avimux->total_data += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
932
933     if (avimux->is_bigfile)
934     {
935       avimux->datax_size += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
936     }
937     else
938     {
939       avimux->data_size += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
940       avimux->audio_size += GST_BUFFER_SIZE(buf);
941       gst_avimux_add_index(avimux, avimux->auds.format, 0x0, GST_BUFFER_SIZE(buf));
942     }
943
944     gst_pad_push(avimux->srcpad, newbuf);
945   }
946   else if (strncmp(padname, "video_", 6) == 0)
947   {
948     if (avimux->restart)
949       gst_avimux_restart_file(avimux);
950
951     /* write a video header + index entry */
952     GST_BUFFER_SIZE(buf) = (GST_BUFFER_SIZE(buf)+3)&~3;
953
954     if ((avimux->is_bigfile?avimux->datax_size:avimux->data_size)+GST_BUFFER_SIZE(buf)>1024*1024*2000)
955     {
956       if (avimux->enable_large_avi)
957         gst_avimux_bigfile(avimux, FALSE);
958       else
959         gst_avimux_restart_file(avimux);
960     }
961
962     newbuf = gst_avimux_riff_get_video_header(GST_BUFFER_SIZE(buf));
963     avimux->total_data += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
964     avimux->total_frames++;
965
966     if (avimux->is_bigfile)
967     {
968       avimux->datax_size += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
969       avimux->numx_frames++;
970     }
971     else
972     {
973       avimux->data_size += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
974       avimux->num_frames++;
975       gst_avimux_add_index(avimux, avimux->vids.compression, 0x12, GST_BUFFER_SIZE(buf));
976     }
977
978     gst_pad_push(avimux->srcpad, newbuf);
979   }
980   else
981   {
982     g_warning("Unknown padname \'%s\'\n", padname);
983     return;
984   }
985
986   /* data */
987   gst_pad_push(avimux->srcpad, buf);
988 }
989
990 static void
991 gst_avimux_get_property (GObject    *object,
992                          guint      prop_id,
993                          GValue     *value,
994                          GParamSpec *pspec)
995 {
996   GstAviMux *avimux;
997
998   /* it's not null if we got it, but it might not be ours */
999   g_return_if_fail(GST_IS_AVIMUX(object));
1000   avimux = GST_AVIMUX(object);
1001
1002   switch (prop_id)
1003   {
1004     case ARG_BIGFILE:
1005       g_value_set_boolean(value, avimux->enable_large_avi);
1006       break;
1007     default:
1008       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1009       break;
1010   }
1011 }
1012
1013 static void
1014 gst_avimux_set_property (GObject      *object,
1015                          guint         prop_id,
1016                          const GValue *value,
1017                          GParamSpec   *pspec)
1018 {
1019   GstAviMux *avimux;
1020
1021   /* it's not null if we got it, but it might not be ours */
1022   g_return_if_fail(GST_IS_AVIMUX(object));
1023   avimux = GST_AVIMUX(object);
1024
1025   switch (prop_id)
1026   {
1027     case ARG_BIGFILE:
1028       avimux->enable_large_avi = g_value_get_boolean(value);
1029       break;
1030     default:
1031       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1032       break;
1033   }
1034 }
1035
1036 static GstElementStateReturn
1037 gst_avimux_change_state (GstElement *element)
1038 {
1039   GstAviMux *avimux;
1040   gint transition = GST_STATE_TRANSITION (element);
1041
1042   /* TODO: timer (for fps calculations) */
1043
1044   g_return_val_if_fail(GST_IS_AVIMUX(element), GST_STATE_FAILURE);
1045   
1046   avimux = GST_AVIMUX(element);
1047
1048   switch (transition) {
1049     case GST_STATE_READY_TO_PAUSED:
1050       /*gst_avimux_start_file(avimux);*/
1051       break;
1052     case GST_STATE_PAUSED_TO_PLAYING:
1053       break;
1054     case GST_STATE_PLAYING_TO_PAUSED:
1055       gst_avimux_stop_file(avimux);
1056       break;
1057     case GST_STATE_PAUSED_TO_READY:
1058       /*gst_avimux_stop_file(avimux);*/
1059       break;
1060   }
1061
1062   if (GST_ELEMENT_CLASS (parent_class)->change_state)
1063     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
1064
1065   return GST_STATE_SUCCESS;
1066 }
1067
1068 static gboolean
1069 plugin_init (GModule *module, GstPlugin *plugin)
1070 {
1071   GstElementFactory *factory;
1072
1073   /* this filter needs the riff parser */
1074 #if 0
1075   if (!gst_library_load ("gstriff")) {
1076     gst_info ("avimux: could not load support library: 'gstriff'\n");
1077     return FALSE;
1078   }
1079 #endif
1080
1081   /* create an elementfactory for the avimux element */
1082   factory = gst_elementfactory_new ("avimux", GST_TYPE_AVIMUX,
1083                                     &gst_avimux_details);
1084   g_return_val_if_fail (factory != NULL, FALSE);
1085
1086   gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (src_factory));
1087   gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (audio_sink_factory));
1088   gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (video_sink_factory));
1089   
1090   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
1091
1092   return TRUE;
1093 }
1094
1095 GstPluginDesc plugin_desc = {
1096   GST_VERSION_MAJOR,
1097   GST_VERSION_MINOR,
1098   "avimux",
1099   plugin_init
1100 };