b48fe8582a634adadd12a2b6d30656bce807a85a
[platform/upstream/gstreamer.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   ARG_FRAMERATE,
66 };
67
68 GST_PADTEMPLATE_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_PADTEMPLATE_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_PADTEMPLATE_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 (11025, 44100),
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_chain                     (GstPad         *pad,
168                                                       GstBuffer      *buf);
169 static gboolean gst_avimux_handle_event              (GstPad         *pad,
170                                                       GstEvent       *event);
171 static GstPad*  gst_avimux_request_new_pad           (GstElement     *element,
172                                                       GstPadTemplate *templ,
173                                                       const gchar    *name);
174 static void     gst_avimux_set_property              (GObject        *object,
175                                                       guint           prop_id,
176                                                       const GValue   *value,
177                                                       GParamSpec     *pspec);
178 static void     gst_avimux_get_property              (GObject        *object,
179                                                       guint           prop_id,
180                                                       GValue         *value,
181                                                       GParamSpec     *pspec);
182 static GstElementStateReturn gst_avimux_change_state (GstElement     *element);
183
184 static GstElementClass *parent_class = NULL;
185 /*static guint gst_avimux_signals[LAST_SIGNAL] = { 0 }; */
186
187 GType
188 gst_avimux_get_type (void) 
189 {
190   static GType avimux_type = 0;
191
192   if (!avimux_type) {
193     static const GTypeInfo avimux_info = {
194       sizeof(GstAviMuxClass),      
195       NULL,
196       NULL,
197       (GClassInitFunc)gst_avimux_class_init,
198       NULL,
199       NULL,
200       sizeof(GstAviMux),
201       0,
202       (GInstanceInitFunc)gst_avimux_init,
203     };
204     avimux_type = g_type_register_static(GST_TYPE_ELEMENT, "GstAviMux", &avimux_info, 0);
205   }
206   return avimux_type;
207 }
208
209 static void
210 gst_avimux_class_init (GstAviMuxClass *klass) 
211 {
212   GObjectClass *gobject_class;
213   GstElementClass *gstelement_class;
214
215   gobject_class = (GObjectClass*)klass;
216   gstelement_class = (GstElementClass*)klass;
217
218   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
219
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));
223
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));
227
228   gstelement_class->request_new_pad = gst_avimux_request_new_pad;
229
230   gstelement_class->change_state = gst_avimux_change_state;
231
232   gstelement_class->get_property = gst_avimux_get_property;
233   gstelement_class->set_property = gst_avimux_set_property;
234 }
235
236 static void 
237 gst_avimux_init (GstAviMux *avimux) 
238 {
239   gint i;
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);
243
244   GST_FLAG_SET (GST_ELEMENT(avimux), GST_ELEMENT_EVENT_AWARE);
245   gst_pad_set_event_function(avimux->srcpad, gst_avimux_handle_event);
246
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;
253
254   avimux->num_frames = 0;
255
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');
265
266   avimux->idx = NULL;
267
268   avimux->write_header = TRUE;
269
270   avimux->enable_large_avi = TRUE;
271
272   avimux->framerate = 25.;
273 }
274
275 static GstPadConnectReturn
276 gst_avimux_sinkconnect (GstPad *pad, GstCaps *vscaps)
277 {
278   GstAviMux *avimux;
279   GstCaps *caps;
280
281   avimux = GST_AVIMUX (gst_pad_get_parent (pad));
282
283   /* we are not going to act on variable caps */
284   if (!GST_CAPS_IS_FIXED (vscaps))
285     return GST_PAD_CONNECT_DELAYED;
286
287   GST_DEBUG (0, "avimux: sinkconnect triggered on %s\n", gst_pad_get_name (pad));
288
289   for (caps = vscaps; caps != NULL; caps = vscaps = vscaps->next)
290   {
291     const gchar* mimetype = gst_caps_get_mime(caps);
292
293     if (!strcmp (mimetype, "video/avi"))
294     {
295       const gchar* format = gst_caps_get_string(caps, "format");
296
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");
309       }
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");
317       }
318       goto done;
319     }
320     else if (!strcmp (mimetype, "video/raw"))
321     {
322       switch (gst_caps_get_fourcc_int(caps, "format"))
323       {
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"))
333           {
334             case GST_MAKE_FOURCC('Y','U','Y','2'):
335               avimux->vids.bit_cnt     = 16; /* YUY2 */
336               break;
337             case GST_MAKE_FOURCC('R','G','B',' '):
338               avimux->vids.bit_cnt     = gst_caps_get_fourcc_int(caps, "bpp"); /* RGB */
339               break;
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 */
343               break;
344           }
345           avimux->vids.compression = gst_caps_get_fourcc_int(caps, "format");
346           avimux->vids.image_size  = avimux->vids.height * avimux->vids.width;
347           goto done;
348         default:
349           break;
350       }
351     }
352     else if (!strcmp (mimetype, "video/jpeg"))
353     {
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;
361       goto done;
362     }
363     else if (!strcmp (mimetype, "audio/raw"))
364     {
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");
372       goto done;
373     }
374     else if (!strcmp (mimetype, "audio/mp3"))
375     {
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;
379       goto done;
380     }
381   }
382   return GST_PAD_CONNECT_REFUSED;
383
384 done:
385   return GST_PAD_CONNECT_OK;
386 }
387
388 static GstPad*
389 gst_avimux_request_new_pad (GstElement     *element,
390                             GstPadTemplate *templ,
391                             const gchar    *req_name)
392 {
393   GstAviMux *avimux;
394   gchar *name = NULL;
395   GstPad *newpad;
396
397   g_return_val_if_fail (templ != NULL, NULL);
398
399   if (templ->direction != GST_PAD_SINK) {
400     g_warning ("avimux: request pad that is not a SINK pad\n");
401     return NULL;
402   }
403
404   g_return_val_if_fail (GST_IS_AVIMUX (element), NULL);
405
406   avimux = GST_AVIMUX (element);
407
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));
413
414     avimux->audiosinkpad[avimux->num_audio_pads] = newpad;
415     avimux->num_audio_pads++;
416   }
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));
422
423     avimux->videosinkpad[avimux->num_video_pads] = newpad;
424     avimux->num_video_pads++;
425   }
426   else {
427     g_warning ("avimux: this is not our template!\n");
428     return NULL;
429   }
430
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);
434   
435   return newpad;
436 }
437
438 /* maybe some of these functions should be moved to riff.h? */
439
440 /* DISCLAIMER: this function is ugly. So be it (i.e. it makes the rest easier) */
441
442 static GstBuffer *
443 gst_avimux_riff_get_avi_header (GstAviMux *avimux)
444 {
445   GstBuffer *buffer;
446   guint8 *buffdata;
447   guint16 temp16;
448   guint32 temp32;
449
450   buffer = gst_buffer_new();
451
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 */
459   }
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 */
463   }
464   /* this is the "riff size" */
465   avimux->header_size = GST_BUFFER_SIZE(buffer);
466   GST_BUFFER_SIZE(buffer) += 12; /* avi data header */
467
468   /* allocate the buffer */
469   buffdata = GST_BUFFER_DATA(buffer) = g_malloc(GST_BUFFER_SIZE(buffer));
470
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;
512
513   if (avimux->num_video_pads)
514   {
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;
520     /* generic header */
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;
576   }
577
578   if (avimux->num_audio_pads)
579   {
580     /* audio header */
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;
585     /* generic header */
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;
631   }
632
633   if (avimux->num_video_pads)
634   {
635     /* odml header */
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;
645   }
646
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);
652
653   return buffer;
654 }
655
656 static GstBuffer *
657 gst_avimux_riff_get_avix_header (guint32 datax_size)
658 {
659   GstBuffer *buffer;
660   guint8 *buffdata;
661   guint32 temp32;
662
663   buffer = gst_buffer_new();
664   GST_BUFFER_SIZE(buffer) = 24;
665   buffdata = GST_BUFFER_DATA(buffer) = g_malloc(GST_BUFFER_SIZE(buffer));
666
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);
675
676   return buffer;
677 }
678
679 static GstBuffer *
680 gst_avimux_riff_get_video_header (guint32 video_frame_size)
681 {
682   GstBuffer *buffer;
683   guint32 temp32;
684
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);
691
692   return buffer;
693 }
694
695 static GstBuffer *
696 gst_avimux_riff_get_audio_header (guint32 audio_sample_size)
697 {
698   GstBuffer *buffer;
699   guint32 temp32;
700
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);
707
708   return buffer;
709 }
710
711 /* some other usable functions (thankyou xawtv ;-) ) */
712
713 static void
714 gst_avimux_add_index (GstAviMux *avimux, guint32 fourcc, guint32 flags, guint32 size)
715 {
716   guint32 temp32;
717   
718   if (avimux->idx_index == avimux->idx_count)
719   {
720     avimux->idx_count += 256;
721     avimux->idx = realloc(avimux->idx, avimux->idx_count*sizeof(gst_riff_index_entry));
722   }
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);
728   avimux->idx_index++;
729   avimux->idx_offset += size + sizeof(gst_riff_index_entry);
730 }
731
732 static void
733 gst_avimux_write_index (GstAviMux *avimux)
734 {
735   GstBuffer *buffer;
736   guint32 temp32;
737
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);
745
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);
752
753   avimux->idx_size += avimux->idx_index * sizeof(gst_riff_index_entry) + 8;
754
755   /* update header */
756   avimux->avi_hdr.flags |= GST_RIFF_AVIH_HASINDEX;
757 }
758
759 static void
760 gst_avimux_bigfile(GstAviMux *avimux, gboolean last)
761 {
762   GstBuffer *header;
763   GstEvent *event;
764     
765   if (avimux->is_bigfile)
766   {
767     /* sarch back */
768     event = gst_event_new_seek(GST_SEEK_BYTEOFFSET_SET, avimux->avix_start, TRUE);
769     gst_pad_send_event(avimux->srcpad, event);
770
771     /* rewrite AVIX header */
772     header = gst_avimux_riff_get_avix_header(avimux->datax_size);
773     gst_pad_push(avimux->srcpad, header);
774
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);
778   }
779   avimux->avix_start = avimux->total_data;
780
781   if (last)
782     return;
783
784   avimux->is_bigfile = TRUE;
785   avimux->numx_frames = 0;
786   avimux->datax_size = 0;
787
788   header = gst_avimux_riff_get_avix_header(0);
789   avimux->total_data += GST_BUFFER_SIZE(header);
790   gst_pad_push(avimux->srcpad, header);
791 }
792
793 /* enough header blabla now, let's go on to actually writing the headers */
794
795 static void
796 gst_avimux_start_file (GstAviMux *avimux)
797 {
798   GstBuffer *header;
799
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;
807
808   avimux->idx_index = 0;
809   avimux->idx_offset = avimux->header_size + 12;
810   avimux->idx_size = 0;
811   avimux->idx_count = 0;
812   avimux->idx = NULL;
813
814   /* header */
815   avimux->avi_hdr.streams = avimux->num_video_pads + avimux->num_audio_pads;
816   avimux->is_bigfile = FALSE;
817
818   header = gst_avimux_riff_get_avi_header(avimux);
819   avimux->total_data += GST_BUFFER_SIZE(header);
820   gst_pad_push(avimux->srcpad, header);
821
822   avimux->write_header = FALSE;
823   avimux->restart = FALSE;
824 }
825
826 static void
827 gst_avimux_stop_file (GstAviMux *avimux)
828 {
829   GstEvent *event;
830   GstBuffer *header;
831
832   /* if bigfile, rewrite header, else write indexes */
833   if (avimux->num_video_pads)
834   {
835     if (avimux->is_bigfile)
836     {
837       gst_avimux_bigfile(avimux, TRUE);
838       avimux->idx_size = 0;
839     }
840     else
841     {
842       gst_avimux_write_index(avimux);
843     }
844   }
845
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;
852
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;
860
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);
866
867   avimux->write_header = TRUE;
868 }
869
870 static void
871 gst_avimux_restart_file (GstAviMux *avimux)
872 {
873   GstEvent *event;
874
875   gst_avimux_stop_file(avimux);
876
877   event = gst_event_new(GST_EVENT_NEW_MEDIA);
878   gst_pad_send_event(avimux->srcpad, event);
879
880   gst_avimux_start_file(avimux);
881 }
882
883 /* handle events (search) */
884 static gboolean
885 gst_avimux_handle_event (GstPad *pad, GstEvent *event)
886 {
887   GstAviMux *avimux;
888   GstEventType type;
889
890   avimux = GST_AVIMUX (gst_pad_get_parent (pad));
891   
892   type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
893
894   switch (type) {
895     case GST_EVENT_NEW_MEDIA:
896       avimux->restart = TRUE;
897       break;
898     default:
899       break;
900   }
901
902   return TRUE;
903 }
904
905 static void
906 gst_avimux_chain (GstPad *pad, GstBuffer *buf)
907 {
908   GstAviMux *avimux;
909   GstBuffer *newbuf;
910   const gchar *padname = gst_pad_get_name (pad);
911
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);
916
917   avimux = GST_AVIMUX (gst_pad_get_parent (pad));
918   
919   if (GST_IS_EVENT(buf))
920   {
921     gst_avimux_handle_event(pad, GST_EVENT(buf));
922     return;
923   }
924
925
926   if (avimux->write_header)
927     gst_avimux_start_file(avimux);
928
929   if (strncmp(padname, "audio_", 6) == 0)
930   {
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);
934
935     if (avimux->is_bigfile)
936     {
937       avimux->datax_size += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
938     }
939     else
940     {
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));
944     }
945
946     gst_pad_push(avimux->srcpad, newbuf);
947   }
948   else if (strncmp(padname, "video_", 6) == 0)
949   {
950     if (avimux->restart)
951       gst_avimux_restart_file(avimux);
952
953     /* write a video header + index entry */
954     GST_BUFFER_SIZE(buf) = (GST_BUFFER_SIZE(buf)+3)&~3;
955
956     if ((avimux->is_bigfile?avimux->datax_size:avimux->data_size)+GST_BUFFER_SIZE(buf)>1024*1024*2000)
957     {
958       if (avimux->enable_large_avi)
959         gst_avimux_bigfile(avimux, FALSE);
960       else
961         gst_avimux_restart_file(avimux);
962     }
963
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++;
967
968     if (avimux->is_bigfile)
969     {
970       avimux->datax_size += GST_BUFFER_SIZE(newbuf) + GST_BUFFER_SIZE(buf);
971       avimux->numx_frames++;
972     }
973     else
974     {
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));
978     }
979
980     gst_pad_push(avimux->srcpad, newbuf);
981   }
982   else
983   {
984     g_warning("Unknown padname \'%s\'\n", padname);
985     return;
986   }
987
988   /* data */
989   gst_pad_push(avimux->srcpad, buf);
990 }
991
992 static void
993 gst_avimux_get_property (GObject    *object,
994                          guint      prop_id,
995                          GValue     *value,
996                          GParamSpec *pspec)
997 {
998   GstAviMux *avimux;
999
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);
1003
1004   switch (prop_id)
1005   {
1006     case ARG_BIGFILE:
1007       g_value_set_boolean(value, avimux->enable_large_avi);
1008       break;
1009     case ARG_FRAMERATE:
1010       g_value_set_double(value, avimux->framerate);
1011       break;
1012     default:
1013       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1014       break;
1015   }
1016 }
1017
1018 static void
1019 gst_avimux_set_property (GObject      *object,
1020                          guint         prop_id,
1021                          const GValue *value,
1022                          GParamSpec   *pspec)
1023 {
1024   GstAviMux *avimux;
1025
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);
1029
1030   switch (prop_id)
1031   {
1032     case ARG_BIGFILE:
1033       avimux->enable_large_avi = g_value_get_boolean(value);
1034       break;
1035     case ARG_FRAMERATE:
1036       avimux->framerate = g_value_get_double(value);
1037       break;
1038     default:
1039       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1040       break;
1041   }
1042 }
1043
1044 static GstElementStateReturn
1045 gst_avimux_change_state (GstElement *element)
1046 {
1047   GstAviMux *avimux;
1048   gint transition = GST_STATE_TRANSITION (element);
1049
1050   /* TODO: timer (for fps calculations) */
1051
1052   g_return_val_if_fail(GST_IS_AVIMUX(element), GST_STATE_FAILURE);
1053   
1054   avimux = GST_AVIMUX(element);
1055
1056   switch (transition) {
1057     case GST_STATE_READY_TO_PAUSED:
1058       /*gst_avimux_start_file(avimux);*/
1059       break;
1060     case GST_STATE_PAUSED_TO_PLAYING:
1061       break;
1062     case GST_STATE_PLAYING_TO_PAUSED:
1063       gst_avimux_stop_file(avimux);
1064       break;
1065     case GST_STATE_PAUSED_TO_READY:
1066       /*gst_avimux_stop_file(avimux);*/
1067       break;
1068   }
1069
1070   if (GST_ELEMENT_CLASS (parent_class)->change_state)
1071     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
1072
1073   return GST_STATE_SUCCESS;
1074 }
1075
1076 static gboolean
1077 plugin_init (GModule *module, GstPlugin *plugin)
1078 {
1079   GstElementFactory *factory;
1080
1081   /* this filter needs the riff parser */
1082 #if 0
1083   if (!gst_library_load ("gstriff")) {
1084     gst_info ("avimux: could not load support library: 'gstriff'\n");
1085     return FALSE;
1086   }
1087 #endif
1088
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);
1093
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));
1097   
1098   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
1099
1100   return TRUE;
1101 }
1102
1103 GstPluginDesc plugin_desc = {
1104   GST_VERSION_MAJOR,
1105   GST_VERSION_MINOR,
1106   "avimux",
1107   plugin_init
1108 };