gstavimux.c 41.1 KB
Newer Older
1
2
/* AVI muxer plugin for GStreamer
 * Copyright (C) 2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

20
21
22
23
24
25
26
/* based on:
 * - the old avimuxer (by Wim Taymans)
 * - xawtv's aviwriter (by Gerd Knorr)
 * - mjpegtools' avilib (by Rainer Johanni)
 * - openDML large-AVI docs
 */

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
27

28
29
30
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
31

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
32
#include "gst/gst-i18n-plugin.h"
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
33
34
35
#include <stdlib.h>
#include <string.h>

36
37
#include <gst/video/video.h>

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
38
39
#include "gstavimux.h"

40
41
42
43
44
45
#ifndef LE_FROM_GUINT16
#define LE_FROM_GUINT16 GUINT16_FROM_LE
#endif
#ifndef LE_FROM_GUINT32
#define LE_FROM_GUINT32 GUINT32_FROM_LE
#endif
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
46
47

/* AviMux signals and args */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
48
49
enum
{
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
50
51
52
53
  /* FILL ME */
  LAST_SIGNAL
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
54
55
enum
{
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
56
  ARG_0,
57
  ARG_BIGFILE
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
58
59
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
60
61
62
63
64
65
static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-msvideo")
    );

David Schleef's avatar
David Schleef committed
66
static GstStaticPadTemplate video_sink_factory =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
67
68
69
70
    GST_STATIC_PAD_TEMPLATE ("video_%d",
    GST_PAD_SINK,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS ("video/x-raw-yuv, "
71
72
        "format = (fourcc) { YUY2, I420 }, "
        "width = (int) [ 16, 4096 ], "
Ronald S. Bultje's avatar
Ronald S. Bultje committed
73
        "height = (int) [ 16, 4096 ], "
74
        "framerate = (fraction) [ 0, MAX ]; "
75
        "image/jpeg, "
76
        "width = (int) [ 16, 4096 ], "
Ronald S. Bultje's avatar
Ronald S. Bultje committed
77
        "height = (int) [ 16, 4096 ], "
78
        "framerate = (fraction) [ 0, MAX ]; "
79
80
81
        "video/x-divx, "
        "width = (int) [ 16, 4096 ], "
        "height = (int) [ 16, 4096 ], "
82
        "framerate = (fraction) [ 0, MAX ], "
83
84
85
        "divxversion = (int) [ 3, 5 ]; "
        "video/x-xvid, "
        "width = (int) [ 16, 4096 ], "
Ronald S. Bultje's avatar
Ronald S. Bultje committed
86
        "height = (int) [ 16, 4096 ], "
87
        "framerate = (fraction) [ 0, MAX ]; "
88
89
        "video/x-3ivx, "
        "width = (int) [ 16, 4096 ], "
Ronald S. Bultje's avatar
Ronald S. Bultje committed
90
        "height = (int) [ 16, 4096 ], "
91
        "framerate = (fraction) [ 0, MAX ]; "
92
93
94
        "video/x-msmpeg, "
        "width = (int) [ 16, 4096 ], "
        "height = (int) [ 16, 4096 ], "
95
        "framerate = (fraction) [ 0, MAX ], "
96
97
98
99
        "msmpegversion = (int) [ 41, 43 ]; "
        "video/mpeg, "
        "width = (int) [ 16, 4096 ], "
        "height = (int) [ 16, 4096 ], "
100
        "framerate = (fraction) [ 0, MAX ], "
101
102
103
104
        "mpegversion = (int) 1, "
        "systemstream = (boolean) FALSE; "
        "video/x-h263, "
        "width = (int) [ 16, 4096 ], "
Ronald S. Bultje's avatar
Ronald S. Bultje committed
105
        "height = (int) [ 16, 4096 ], "
106
        "framerate = (fraction) [ 0, MAX ]; "
107
108
109
        "video/x-h264, "
        "width = (int) [ 16, 4096 ], "
        "height = (int) [ 16, 4096 ], "
110
        "framerate = (fraction) [ 0, MAX ]; "
111
112
113
        "video/x-dv, "
        "width = (int) 720, "
        "height = (int) { 576, 480 }, "
114
        "framerate = (fraction) [ 0, MAX ], "
115
116
        "systemstream = (boolean) FALSE; "
        "video/x-huffyuv, "
Ronald S. Bultje's avatar
Ronald S. Bultje committed
117
        "width = (int) [ 16, 4096 ], "
118
        "height = (int) [ 16, 4096 ], " "framerate = (fraction) [ 0, MAX ]")
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
119
120
    );

David Schleef's avatar
David Schleef committed
121
static GstStaticPadTemplate audio_sink_factory =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
122
123
124
125
    GST_STATIC_PAD_TEMPLATE ("audio_%d",
    GST_PAD_SINK,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS ("audio/x-raw-int, "
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
        "endianness = (int) LITTLE_ENDIAN, "
        "signed = (boolean) { TRUE, FALSE }, "
        "width = (int) { 8, 16 }, "
        "depth = (int) { 8, 16 }, "
        "rate = (int) [ 1000, 96000 ], "
        "channels = (int) [ 1, 2 ]; "
        "audio/mpeg, "
        "mpegversion = (int) 1, "
        "layer = (int) [ 1, 3 ], "
        "rate = (int) [ 1000, 96000 ], "
        "channels = (int) [ 1, 2 ]; "
        "audio/x-vorbis, "
        "rate = (int) [ 1000, 96000 ], "
        "channels = (int) [ 1, 2 ]; "
        "audio/x-ac3, "
        "rate = (int) [ 1000, 96000 ], " "channels = (int) [ 1, 2 ]")
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
142
143
144
145
146
147
148
149
150
151
152
    );


static void gst_avimux_base_init (gpointer g_class);
static void gst_avimux_class_init (GstAviMuxClass * klass);
static void gst_avimux_init (GstAviMux * avimux);

static void gst_avimux_loop (GstElement * element);
static gboolean gst_avimux_handle_event (GstPad * pad, GstEvent * event);
static GstPad *gst_avimux_request_new_pad (GstElement * element,
    GstPadTemplate * templ, const gchar * name);
153
static void gst_avimux_release_pad (GstElement * element, GstPad * pad);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
154
155
156
157
static void gst_avimux_set_property (GObject * object,
    guint prop_id, const GValue * value, GParamSpec * pspec);
static void gst_avimux_get_property (GObject * object,
    guint prop_id, GValue * value, GParamSpec * pspec);
158
159
static GstStateChangeReturn gst_avimux_change_state (GstElement * element,
    GstStateChange transition);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
160
161

static GstElementClass *parent_class = NULL;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
162

163
/*static guint gst_avimux_signals[LAST_SIGNAL] = { 0 }; */
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
164
165

GType
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
166
gst_avimux_get_type (void)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
167
168
169
170
171
{
  static GType avimux_type = 0;

  if (!avimux_type) {
    static const GTypeInfo avimux_info = {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
172
      sizeof (GstAviMuxClass),
173
      gst_avimux_base_init,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
174
      NULL,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
175
      (GClassInitFunc) gst_avimux_class_init,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
176
177
      NULL,
      NULL,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
178
      sizeof (GstAviMux),
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
179
      0,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
180
      (GInstanceInitFunc) gst_avimux_init,
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
181
    };
182
183
184
185
186
    static const GInterfaceInfo tag_setter_info = {
      NULL,
      NULL,
      NULL
    };
187

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
188
    avimux_type =
189
        g_type_register_static (GST_TYPE_ELEMENT, "GstAviMux", &avimux_info, 0);
190
191
    g_type_add_interface_static (avimux_type, GST_TYPE_TAG_SETTER,
        &tag_setter_info);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
192
193
194
195
  }
  return avimux_type;
}

196
197
198
199
static void
gst_avimux_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
200
201
202
203
204
  static GstElementDetails gst_avimux_details =
      GST_ELEMENT_DETAILS ("Avi multiplexer",
      "Codec/Muxer",
      "Muxes audio and video into an avi stream",
      "Ronald Bultje <rbultje@ronald.bitfreak.net>");
205
206

  gst_element_class_add_pad_template (element_class,
David Schleef's avatar
David Schleef committed
207
      gst_static_pad_template_get (&src_factory));
208
  gst_element_class_add_pad_template (element_class,
David Schleef's avatar
David Schleef committed
209
      gst_static_pad_template_get (&audio_sink_factory));
210
  gst_element_class_add_pad_template (element_class,
David Schleef's avatar
David Schleef committed
211
      gst_static_pad_template_get (&video_sink_factory));
212
213
214
215

  gst_element_class_set_details (element_class, &gst_avimux_details);
}

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
216
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
217
gst_avimux_class_init (GstAviMuxClass * klass)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
218
219
220
221
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
222
223
  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
224
225
226

  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
227
228
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BIGFILE,
      g_param_spec_boolean ("bigfile", "Bigfile Support",
229
          "Support for openDML-2.0 (big) AVI files", 0, G_PARAM_READWRITE));
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
230
231

  gstelement_class->request_new_pad = gst_avimux_request_new_pad;
232
  gstelement_class->release_pad = gst_avimux_release_pad;
233
234
235
236
237

  gstelement_class->change_state = gst_avimux_change_state;

  gstelement_class->get_property = gst_avimux_get_property;
  gstelement_class->set_property = gst_avimux_set_property;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
238
239
}

240
static const GstEventMask *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
241
gst_avimux_get_event_masks (GstPad * pad)
242
{
243
  static const GstEventMask gst_avimux_sink_event_masks[] = {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
244
245
    {GST_EVENT_EOS, 0},
    {0,}
246
  };
247

248
  return gst_avimux_sink_event_masks;
249
250
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
251
252
static void
gst_avimux_init (GstAviMux * avimux)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
253
{
254
255
  GstElementClass *klass = GST_ELEMENT_GET_CLASS (avimux);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
256
257
  avimux->srcpad =
      gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
258
          "src"), "src");
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
259
260
  gst_element_add_pad (GST_ELEMENT (avimux), avimux->srcpad);

261
  GST_OBJECT_FLAG_SET (GST_ELEMENT (avimux), GST_ELEMENT_EVENT_AWARE);
262

263
264
265
266
267
268
269
  avimux->audiosinkpad = NULL;
  avimux->audio_pad_connected = FALSE;
  avimux->videosinkpad = NULL;
  avimux->video_pad_connected = FALSE;

  avimux->audio_buffer_queue = NULL;
  avimux->video_buffer_queue = NULL;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
270

271
272
273
  avimux->num_frames = 0;

  /* audio/video/AVI header initialisation */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
274
275
276
277
278
279
  memset (&(avimux->avi_hdr), 0, sizeof (gst_riff_avih));
  memset (&(avimux->vids_hdr), 0, sizeof (gst_riff_strh));
  memset (&(avimux->vids), 0, sizeof (gst_riff_strf_vids));
  memset (&(avimux->auds_hdr), 0, sizeof (gst_riff_strh));
  memset (&(avimux->auds), 0, sizeof (gst_riff_strf_auds));
  avimux->vids_hdr.type = GST_MAKE_FOURCC ('v', 'i', 'd', 's');
280
  avimux->vids_hdr.rate = 1;
281
  avimux->avi_hdr.max_bps = 10000000;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
282
  avimux->auds_hdr.type = GST_MAKE_FOURCC ('a', 'u', 'd', 's');
283
284
  avimux->vids_hdr.quality = 0xFFFFFFFF;
  avimux->auds_hdr.quality = 0xFFFFFFFF;
285
  avimux->tags = NULL;
286
287
288
289
290
291

  avimux->idx = NULL;

  avimux->write_header = TRUE;

  avimux->enable_large_avi = TRUE;
292

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
293
  gst_element_set_loop_function (GST_ELEMENT (avimux), gst_avimux_loop);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
294
295
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
296
static GstPadLinkReturn
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
297
gst_avimux_vidsinkconnect (GstPad * pad, const GstCaps * vscaps)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
298
299
{
  GstAviMux *avimux;
David Schleef's avatar
David Schleef committed
300
  GstStructure *structure;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
301
  const gchar *mimetype;
302
  const GValue *fps;
David Schleef's avatar
David Schleef committed
303
  gboolean ret;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
304
305
306

  avimux = GST_AVIMUX (gst_pad_get_parent (pad));

307
  GST_DEBUG ("avimux: video sinkconnect triggered on %s",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
308
      gst_pad_get_name (pad));
309

David Schleef's avatar
David Schleef committed
310
311
  structure = gst_caps_get_structure (vscaps, 0);
  mimetype = gst_structure_get_name (structure);
312

David Schleef's avatar
David Schleef committed
313
  /* global */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
314
315
  avimux->vids.size = sizeof (gst_riff_strf_vids);
  avimux->vids.planes = 1;
David Schleef's avatar
David Schleef committed
316
317
  ret = gst_structure_get_int (structure, "width", &avimux->vids.width);
  ret &= gst_structure_get_int (structure, "height", &avimux->vids.height);
318
319
  fps = gst_structure_get_value (structure, "framerate");
  ret &= (fps != NULL && GST_VALUE_HOLDS_FRACTION (fps));
320
321
  if (!ret) {
    gst_object_unref (avimux);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
322
    return GST_PAD_LINK_REFUSED;
323
  }
David Schleef's avatar
David Schleef committed
324

325
326
  avimux->vids_hdr.rate = gst_value_get_fraction_numerator (fps);
  avimux->vids_hdr.scale = gst_value_get_fraction_denominator (fps);
David Schleef's avatar
David Schleef committed
327
328
329
330
331
332

  if (!strcmp (mimetype, "video/x-raw-yuv")) {
    guint32 format;

    gst_structure_get_fourcc (structure, "format", &format);
    avimux->vids.compression = format;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
333
334
    switch (format) {
      case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
335
336
        avimux->vids.bit_cnt = 16;
        break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
337
      case GST_MAKE_FOURCC ('I', '4', '2', '0'):
338
339
        avimux->vids.bit_cnt = 12;
        break;
David Schleef's avatar
David Schleef committed
340
341
342
343
344
345
346
    }
  } else {
    avimux->vids.bit_cnt = 24;
    avimux->vids.compression = 0;

    /* find format */
    if (!strcmp (mimetype, "video/x-huffyuv")) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
347
      avimux->vids.compression = GST_MAKE_FOURCC ('H', 'F', 'Y', 'U');
348
    } else if (!strcmp (mimetype, "image/jpeg")) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
349
      avimux->vids.compression = GST_MAKE_FOURCC ('M', 'J', 'P', 'G');
David Schleef's avatar
David Schleef committed
350
351
    } else if (!strcmp (mimetype, "video/x-divx")) {
      gint divxversion;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
352

David Schleef's avatar
David Schleef committed
353
354
      gst_structure_get_int (structure, "divxversion", &divxversion);
      switch (divxversion) {
355
356
357
358
359
360
361
362
363
        case 3:
          avimux->vids.compression = GST_MAKE_FOURCC ('D', 'I', 'V', '3');
          break;
        case 4:
          avimux->vids.compression = GST_MAKE_FOURCC ('D', 'I', 'V', 'X');
          break;
        case 5:
          avimux->vids.compression = GST_MAKE_FOURCC ('D', 'X', '5', '0');
          break;
364
      }
David Schleef's avatar
David Schleef committed
365
    } else if (!strcmp (mimetype, "video/x-xvid")) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
366
      avimux->vids.compression = GST_MAKE_FOURCC ('X', 'V', 'I', 'D');
David Schleef's avatar
David Schleef committed
367
    } else if (!strcmp (mimetype, "video/x-3ivx")) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
368
      avimux->vids.compression = GST_MAKE_FOURCC ('3', 'I', 'V', '2');
David Schleef's avatar
David Schleef committed
369
370
    } else if (!strcmp (mimetype, "video/x-msmpeg")) {
      gint msmpegversion;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
371

David Schleef's avatar
David Schleef committed
372
373
      gst_structure_get_int (structure, "msmpegversion", &msmpegversion);
      switch (msmpegversion) {
374
375
376
377
378
379
380
381
382
        case 41:
          avimux->vids.compression = GST_MAKE_FOURCC ('M', 'P', 'G', '4');
          break;
        case 42:
          avimux->vids.compression = GST_MAKE_FOURCC ('M', 'P', '4', '2');
          break;
        case 43:
          avimux->vids.compression = GST_MAKE_FOURCC ('M', 'P', '4', '3');
          break;
383
      }
David Schleef's avatar
David Schleef committed
384
    } else if (!strcmp (mimetype, "video/x-dv")) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
385
      avimux->vids.compression = GST_MAKE_FOURCC ('D', 'V', 'S', 'D');
David Schleef's avatar
David Schleef committed
386
    } else if (!strcmp (mimetype, "video/x-h263")) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
387
      avimux->vids.compression = GST_MAKE_FOURCC ('H', '2', '6', '3');
David Schleef's avatar
David Schleef committed
388
    } else if (!strcmp (mimetype, "video/mpeg")) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
389
      avimux->vids.compression = GST_MAKE_FOURCC ('M', 'P', 'E', 'G');
David Schleef's avatar
David Schleef committed
390
    }
391

David Schleef's avatar
David Schleef committed
392
    if (!avimux->vids.compression) {
393
      gst_object_unref (avimux);
David Schleef's avatar
David Schleef committed
394
      return GST_PAD_LINK_DELAYED;
395
    }
396
397
398
399
400
401
402
  }

  avimux->vids_hdr.fcc_handler = avimux->vids.compression;
  avimux->vids.image_size = avimux->vids.height * avimux->vids.width;
  avimux->avi_hdr.width = avimux->vids.width;
  avimux->avi_hdr.height = avimux->vids.height;
  avimux->avi_hdr.us_frame = avimux->vids_hdr.scale;
403
404
405

  gst_object_unref (avimux);

406
407
  return GST_PAD_LINK_OK;
}
Wim Taymans's avatar
Wim Taymans committed
408

409
static GstPadLinkReturn
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
410
gst_avimux_audsinkconnect (GstPad * pad, const GstCaps * vscaps)
411
412
{
  GstAviMux *avimux;
David Schleef's avatar
David Schleef committed
413
  GstStructure *structure;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
414
  const gchar *mimetype;
David Schleef's avatar
David Schleef committed
415
  int i;
416
417
418
419

  avimux = GST_AVIMUX (gst_pad_get_parent (pad));

  GST_DEBUG ("avimux: audio sinkconnect triggered on %s",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
420
      gst_pad_get_name (pad));
421

David Schleef's avatar
David Schleef committed
422
423
  structure = gst_caps_get_structure (vscaps, 0);
  mimetype = gst_structure_get_name (structure);
424

David Schleef's avatar
David Schleef committed
425
426
427
428
429
  /* we want these for all */
  gst_structure_get_int (structure, "channels", &i);
  avimux->auds.channels = i;
  gst_structure_get_int (structure, "rate", &i);
  avimux->auds.rate = i;
430

David Schleef's avatar
David Schleef committed
431
  if (!strcmp (mimetype, "audio/x-raw-int")) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
432
    avimux->auds.format = GST_RIFF_WAVE_FORMAT_PCM;
433

David Schleef's avatar
David Schleef committed
434
435
436
437
    gst_structure_get_int (structure, "width", &i);
    avimux->auds.blockalign = i;
    gst_structure_get_int (structure, "depth", &i);
    avimux->auds.size = i;
438

David Schleef's avatar
David Schleef committed
439
440
441
442
443
    /* set some more info straight */
    avimux->auds.blockalign /= 8;
    avimux->auds.blockalign *= avimux->auds.channels;
    avimux->auds.av_bps = avimux->auds.blockalign * avimux->auds.rate;
  } else if (!strcmp (mimetype, "audio/mpeg") ||
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
444
445
      !strcmp (mimetype, "audio/x-vorbis") ||
      !strcmp (mimetype, "audio/x-ac3")) {
David Schleef's avatar
David Schleef committed
446
447
448
449
    avimux->auds.format = 0;

    if (!strcmp (mimetype, "audio/mpeg")) {
      gint layer = 3;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
450

David Schleef's avatar
David Schleef committed
451
452
      gst_structure_get_int (structure, "layer", &layer);
      switch (layer) {
453
454
455
456
457
458
459
        case 3:
          avimux->auds.format = GST_RIFF_WAVE_FORMAT_MPEGL3;
          break;
        case 1:
        case 2:
          avimux->auds.format = GST_RIFF_WAVE_FORMAT_MPEGL12;
          break;
460
      }
David Schleef's avatar
David Schleef committed
461
462
463
464
465
    } else if (!strcmp (mimetype, "audio/x-vorbis")) {
      avimux->auds.format = GST_RIFF_WAVE_FORMAT_VORBIS3;
    } else if (!strcmp (mimetype, "audio/x-ac3")) {
      avimux->auds.format = GST_RIFF_WAVE_FORMAT_A52;
    }
466

David Schleef's avatar
David Schleef committed
467
468
469
    avimux->auds.blockalign = 1;
    avimux->auds.av_bps = 0;
    avimux->auds.size = 16;
470

David Schleef's avatar
David Schleef committed
471
    if (!avimux->auds.format) {
472
      gst_object_unref (avimux);
David Schleef's avatar
David Schleef committed
473
      return GST_PAD_LINK_REFUSED;
474
    }
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
475
476
  }

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
477
  avimux->auds_hdr.rate = avimux->auds.blockalign * avimux->auds.rate;
478
  avimux->auds_hdr.samplesize = avimux->auds.blockalign;
479
  avimux->auds_hdr.scale = 1;
480
481
482

  gst_object_unref (avimux);

483
  return GST_PAD_LINK_OK;
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
484
485
}

486
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
487
gst_avimux_pad_link (GstPad * pad, GstPad * peer, gpointer data)
488
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
489
  GstAviMux *avimux = GST_AVIMUX (data);
490
491
  const gchar *padname = gst_pad_get_name (pad);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
492
  if (pad == avimux->audiosinkpad) {
493
    avimux->audio_pad_connected = TRUE;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
494
  } else if (pad == avimux->videosinkpad) {
495
    avimux->video_pad_connected = TRUE;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
496
497
  } else {
    g_warning ("Unknown padname '%s'", padname);
498
499
500
    return;
  }

501
  GST_DEBUG ("pad '%s' connected", padname);
502
503
504
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
505
gst_avimux_pad_unlink (GstPad * pad, GstPad * peer, gpointer data)
506
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
507
  GstAviMux *avimux = GST_AVIMUX (data);
508
509
  const gchar *padname = gst_pad_get_name (pad);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
510
  if (pad == avimux->audiosinkpad) {
511
    avimux->audio_pad_connected = FALSE;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
512
  } else if (pad == avimux->videosinkpad) {
513
    avimux->video_pad_connected = FALSE;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
514
515
  } else {
    g_warning ("Unknown padname '%s'", padname);
516
517
518
    return;
  }

519
  GST_DEBUG ("pad '%s' unlinked", padname);
520
521
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
522
523
524
static GstPad *
gst_avimux_request_new_pad (GstElement * element,
    GstPadTemplate * templ, const gchar * req_name)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
525
526
527
{
  GstAviMux *avimux;
  GstPad *newpad;
528
  GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
529

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
530
531
532
533
534
535
536
537
538
539
540
  g_return_val_if_fail (templ != NULL, NULL);

  if (templ->direction != GST_PAD_SINK) {
    g_warning ("avimux: request pad that is not a SINK pad\n");
    return NULL;
  }

  g_return_val_if_fail (GST_IS_AVIMUX (element), NULL);

  avimux = GST_AVIMUX (element);

541
  if (templ == gst_element_class_get_pad_template (klass, "audio_%d")) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
542
    g_return_val_if_fail (avimux->audiosinkpad == NULL, NULL);
543
    newpad = gst_pad_new_from_template (templ, "audio_00");
544
    gst_pad_set_link_function (newpad, gst_avimux_audsinkconnect);
545
    avimux->audiosinkpad = newpad;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
546
547
  } else if (templ == gst_element_class_get_pad_template (klass, "video_%d")) {
    g_return_val_if_fail (avimux->videosinkpad == NULL, NULL);
548
    newpad = gst_pad_new_from_template (templ, "video_00");
549
    gst_pad_set_link_function (newpad, gst_avimux_vidsinkconnect);
550
    avimux->videosinkpad = newpad;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
551
  } else {
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
552
553
554
555
    g_warning ("avimux: this is not our template!\n");
    return NULL;
  }

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
556
557
558
559
  g_signal_connect (newpad, "linked",
      G_CALLBACK (gst_avimux_pad_link), (gpointer) avimux);
  g_signal_connect (newpad, "unlinked",
      G_CALLBACK (gst_avimux_pad_unlink), (gpointer) avimux);
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
560
  gst_element_add_pad (element, newpad);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
561
562
  gst_pad_set_event_mask_function (newpad, gst_avimux_get_event_masks);

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
563
564
565
  return newpad;
}

566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
static void
gst_avimux_release_pad (GstElement * element, GstPad * pad)
{
  GstAviMux *avimux = GST_AVIMUX (element);

  if (pad == avimux->videosinkpad) {
    avimux->videosinkpad = NULL;
  } else if (pad == avimux->audiosinkpad) {
    avimux->audiosinkpad = NULL;
  } else {
    g_warning ("Unknown pad %s", gst_pad_get_name (pad));
    return;
  }

  GST_DEBUG ("Removed pad '%s'", gst_pad_get_name (pad));
  gst_element_remove_pad (element, pad);
}

584
585
586
587
/* maybe some of these functions should be moved to riff.h? */

/* DISCLAIMER: this function is ugly. So be it (i.e. it makes the rest easier) */

588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
static void
gst_avimux_write_tag (const GstTagList * list, const gchar * tag, gpointer data)
{
  const struct
  {
    guint32 fcc;
    gchar *tag;
  } rifftags[] = {
    {
    GST_RIFF_INFO_ICMT, GST_TAG_COMMENT}, {
    GST_RIFF_INFO_INAM, GST_TAG_TITLE}, {
    GST_RIFF_INFO_ISFT, GST_TAG_ENCODER}, {
    GST_RIFF_INFO_IGNR, GST_TAG_GENRE}, {
    GST_RIFF_INFO_ICOP, GST_TAG_COPYRIGHT}, {
    GST_RIFF_INFO_IART, GST_TAG_ARTIST}, {
    GST_RIFF_INFO_IARL, GST_TAG_LOCATION}, {
    0, NULL}
  };
606
  gint n, len, plen;
607
  GstBuffer *buf = data;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
608
  guint8 *buffdata = GST_BUFFER_DATA (buf) + GST_BUFFER_SIZE (buf);
609
610
611
612
613
614
  gchar *str;

  for (n = 0; rifftags[n].fcc != 0; n++) {
    if (!strcmp (rifftags[n].tag, tag) &&
        gst_tag_list_get_string (list, tag, &str)) {
      len = strlen (str);
615
616
617
618
      plen = len + 1;
      if (plen & 1)
        plen++;
      if (GST_BUFFER_MAXSIZE (buf) >= GST_BUFFER_SIZE (buf) + 8 + plen) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
619
620
621
622
        GST_WRITE_UINT32_LE (buffdata, rifftags[n].fcc);
        GST_WRITE_UINT32_LE (buffdata + 4, len + 1);
        memcpy (buffdata + 8, str, len);
        buffdata[8 + len] = 0;
623
        GST_BUFFER_SIZE (buf) += 8 + plen;
624
      }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
625
      break;
626
627
628
629
    }
  }
}

630
static GstBuffer *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
631
gst_avimux_riff_get_avi_header (GstAviMux * avimux)
632
{
633
634
  GstTagList *tags;
  const GstTagList *iface_tags;
635
636
  GstBuffer *buffer;
  guint8 *buffdata;
637
  guint size = 0;
638
639

  /* first, let's see what actually needs to be in the buffer */
640
  size += 32 + sizeof (gst_riff_avih);  /* avi header */
641
  if (avimux->video_pad_connected) {    /* we have video */
642
643
    size += 28 + sizeof (gst_riff_strh) + sizeof (gst_riff_strf_vids);  /* vid hdr */
    size += 24;                 /* odml header */
644
  }
645
  if (avimux->audio_pad_connected) {    /* we have audio */
646
    size += 28 + sizeof (gst_riff_strh) + sizeof (gst_riff_strf_auds);  /* aud hdr */
647
648
  }
  /* this is the "riff size" */
649
650
651
652
  avimux->header_size = size;
  size += 12;                   /* avi data header */

  /* tags */
653
  iface_tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (avimux));
654
655
656
657
658
659
660
661
662
663
664
665
666
  if (iface_tags || avimux->tags) {
    size += 1024;
    if (iface_tags && avimux->tags) {
      tags = gst_tag_list_merge (iface_tags, avimux->tags,
          GST_TAG_MERGE_APPEND);
    } else if (iface_tags) {
      tags = gst_tag_list_copy (iface_tags);
    } else {
      tags = gst_tag_list_copy (avimux->tags);
    }
  } else {
    tags = NULL;
  }
667
668

  /* allocate the buffer */
669
670
671
  buffer = gst_buffer_new_and_alloc (size);
  buffdata = GST_BUFFER_DATA (buffer);
  GST_BUFFER_SIZE (buffer) = 0;
672
673

  /* avi header metadata */
674
675
676
677
678
679
680
681
682
683
684
685
  memcpy (buffdata + 0, "RIFF", 4);
  GST_WRITE_UINT32_LE (buffdata + 4,
      avimux->header_size + avimux->idx_size + avimux->data_size);
  memcpy (buffdata + 8, "AVI ", 4);
  memcpy (buffdata + 12, "LIST", 4);
  GST_WRITE_UINT32_LE (buffdata + 16, avimux->header_size - 4 * 5);
  memcpy (buffdata + 20, "hdrl", 4);
  memcpy (buffdata + 24, "avih", 4);
  GST_WRITE_UINT32_LE (buffdata + 28, sizeof (gst_riff_avih));
  buffdata += 32;
  GST_BUFFER_SIZE (buffer) += 32;

686
  /* the AVI header itself */
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
  GST_WRITE_UINT32_LE (buffdata + 0, avimux->avi_hdr.us_frame);
  GST_WRITE_UINT32_LE (buffdata + 4, avimux->avi_hdr.max_bps);
  GST_WRITE_UINT32_LE (buffdata + 8, avimux->avi_hdr.pad_gran);
  GST_WRITE_UINT32_LE (buffdata + 12, avimux->avi_hdr.flags);
  GST_WRITE_UINT32_LE (buffdata + 16, avimux->avi_hdr.tot_frames);
  GST_WRITE_UINT32_LE (buffdata + 20, avimux->avi_hdr.init_frames);
  GST_WRITE_UINT32_LE (buffdata + 24, avimux->avi_hdr.streams);
  GST_WRITE_UINT32_LE (buffdata + 28, avimux->avi_hdr.bufsize);
  GST_WRITE_UINT32_LE (buffdata + 32, avimux->avi_hdr.width);
  GST_WRITE_UINT32_LE (buffdata + 36, avimux->avi_hdr.height);
  GST_WRITE_UINT32_LE (buffdata + 40, avimux->avi_hdr.scale);
  GST_WRITE_UINT32_LE (buffdata + 44, avimux->avi_hdr.rate);
  GST_WRITE_UINT32_LE (buffdata + 48, avimux->avi_hdr.start);
  GST_WRITE_UINT32_LE (buffdata + 52, avimux->avi_hdr.length);
  buffdata += 56;
  GST_BUFFER_SIZE (buffer) += 56;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
703
704

  if (avimux->video_pad_connected) {
705
    /* video header metadata */
706
707
708
709
    memcpy (buffdata + 0, "LIST", 4);
    GST_WRITE_UINT32_LE (buffdata + 4,
        sizeof (gst_riff_strh) + sizeof (gst_riff_strf_vids) + 4 * 5);
    memcpy (buffdata + 8, "strl", 4);
710
    /* generic header */
711
712
    memcpy (buffdata + 12, "strh", 4);
    GST_WRITE_UINT32_LE (buffdata + 16, sizeof (gst_riff_strh));
713
    /* the actual header */
714
715
716
717
718
719
720
721
722
723
724
725
    GST_WRITE_UINT32_LE (buffdata + 20, avimux->vids_hdr.type);
    GST_WRITE_UINT32_LE (buffdata + 24, avimux->vids_hdr.fcc_handler);
    GST_WRITE_UINT32_LE (buffdata + 28, avimux->vids_hdr.flags);
    GST_WRITE_UINT32_LE (buffdata + 32, avimux->vids_hdr.priority);
    GST_WRITE_UINT32_LE (buffdata + 36, avimux->vids_hdr.init_frames);
    GST_WRITE_UINT32_LE (buffdata + 40, avimux->vids_hdr.scale);
    GST_WRITE_UINT32_LE (buffdata + 44, avimux->vids_hdr.rate);
    GST_WRITE_UINT32_LE (buffdata + 48, avimux->vids_hdr.start);
    GST_WRITE_UINT32_LE (buffdata + 52, avimux->vids_hdr.length);
    GST_WRITE_UINT32_LE (buffdata + 56, avimux->vids_hdr.bufsize);
    GST_WRITE_UINT32_LE (buffdata + 60, avimux->vids_hdr.quality);
    GST_WRITE_UINT32_LE (buffdata + 64, avimux->vids_hdr.samplesize);
726
    /* the video header */
727
728
    memcpy (buffdata + 68, "strf", 4);
    GST_WRITE_UINT32_LE (buffdata + 72, sizeof (gst_riff_strf_vids));
729
    /* the actual header */
730
731
732
733
734
735
736
737
738
739
740
741
742
    GST_WRITE_UINT32_LE (buffdata + 76, avimux->vids.size);
    GST_WRITE_UINT32_LE (buffdata + 80, avimux->vids.width);
    GST_WRITE_UINT32_LE (buffdata + 84, avimux->vids.height);
    GST_WRITE_UINT16_LE (buffdata + 88, avimux->vids.planes);
    GST_WRITE_UINT16_LE (buffdata + 90, avimux->vids.bit_cnt);
    GST_WRITE_UINT32_LE (buffdata + 92, avimux->vids.compression);
    GST_WRITE_UINT32_LE (buffdata + 96, avimux->vids.image_size);
    GST_WRITE_UINT32_LE (buffdata + 100, avimux->vids.xpels_meter);
    GST_WRITE_UINT32_LE (buffdata + 104, avimux->vids.ypels_meter);
    GST_WRITE_UINT32_LE (buffdata + 108, avimux->vids.num_colors);
    GST_WRITE_UINT32_LE (buffdata + 112, avimux->vids.imp_colors);
    buffdata += 116;
    GST_BUFFER_SIZE (buffer) += 116;
743
  }
744

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
745
  if (avimux->audio_pad_connected) {
746
    /* audio header */
747
748
749
750
    memcpy (buffdata + 0, "LIST", 4);
    GST_WRITE_UINT32_LE (buffdata + 4,
        sizeof (gst_riff_strh) + sizeof (gst_riff_strf_auds) + 4 * 5);
    memcpy (buffdata + 8, "strl", 4);
751
    /* generic header */
752
753
    memcpy (buffdata + 12, "strh", 4);
    GST_WRITE_UINT32_LE (buffdata + 16, sizeof (gst_riff_strh));
754
    /* the actual header */
755
756
757
758
759
760
761
762
763
764
765
766
    GST_WRITE_UINT32_LE (buffdata + 20, avimux->auds_hdr.type);
    GST_WRITE_UINT32_LE (buffdata + 24, avimux->auds_hdr.fcc_handler);
    GST_WRITE_UINT32_LE (buffdata + 28, avimux->auds_hdr.flags);
    GST_WRITE_UINT32_LE (buffdata + 32, avimux->auds_hdr.priority);
    GST_WRITE_UINT32_LE (buffdata + 36, avimux->auds_hdr.init_frames);
    GST_WRITE_UINT32_LE (buffdata + 40, avimux->auds_hdr.scale);
    GST_WRITE_UINT32_LE (buffdata + 44, avimux->auds_hdr.rate);
    GST_WRITE_UINT32_LE (buffdata + 48, avimux->auds_hdr.start);
    GST_WRITE_UINT32_LE (buffdata + 52, avimux->auds_hdr.length);
    GST_WRITE_UINT32_LE (buffdata + 56, avimux->auds_hdr.bufsize);
    GST_WRITE_UINT32_LE (buffdata + 60, avimux->auds_hdr.quality);
    GST_WRITE_UINT32_LE (buffdata + 64, avimux->auds_hdr.samplesize);
767
    /* the audio header */
768
769
    memcpy (buffdata + 68, "strf", 4);
    GST_WRITE_UINT32_LE (buffdata + 72, sizeof (gst_riff_strf_auds));
770
    /* the actual header */
771
772
773
774
775
776
777
778
    GST_WRITE_UINT16_LE (buffdata + 76, avimux->auds.format);
    GST_WRITE_UINT16_LE (buffdata + 78, avimux->auds.channels);
    GST_WRITE_UINT32_LE (buffdata + 80, avimux->auds.rate);
    GST_WRITE_UINT32_LE (buffdata + 84, avimux->auds.av_bps);
    GST_WRITE_UINT16_LE (buffdata + 88, avimux->auds.blockalign);
    GST_WRITE_UINT16_LE (buffdata + 90, avimux->auds.size);
    buffdata += 92;
    GST_BUFFER_SIZE (buffer) += 92;
779
  }
780

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
781
  if (avimux->video_pad_connected) {
782
    /* odml header */
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
    memcpy (buffdata + 0, "LIST", 4);
    GST_WRITE_UINT32_LE (buffdata + 4, sizeof (guint32) + 4 * 3);
    memcpy (buffdata + 8, "odml", 4);
    memcpy (buffdata + 12, "dmlh", 4);
    GST_WRITE_UINT32_LE (buffdata + 16, sizeof (guint32));
    GST_WRITE_UINT32_LE (buffdata + 20, avimux->total_frames);
    buffdata += 24;
    GST_BUFFER_SIZE (buffer) += 24;
  }

  /* tags */
  if (tags) {
    guint8 *ptr;
    guint startsize;

    memcpy (buffdata + 0, "LIST", 4);
    ptr = buffdata + 4;         /* fill in later */
    startsize = GST_BUFFER_SIZE (buffer) + 4;
    memcpy (buffdata + 8, "INFO", 4);
    buffdata += 12;
    GST_BUFFER_SIZE (buffer) += 12;

    /* 12 bytes is needed for data header */
    GST_BUFFER_MAXSIZE (buffer) -= 12;
    gst_tag_list_foreach (tags, gst_avimux_write_tag, buffer);
    gst_tag_list_free (tags);
    GST_BUFFER_MAXSIZE (buffer) += 12;
    buffdata = GST_BUFFER_DATA (buffer) + GST_BUFFER_SIZE (buffer);

    /* update list size */
Ronald S. Bultje's avatar
Ronald S. Bultje committed
813
    GST_WRITE_UINT32_LE (ptr, GST_BUFFER_SIZE (buffer) - startsize - 4);
814
  }
815
816

  /* avi data header */
817
818
  memcpy (buffdata + 0, "LIST", 4);
  GST_WRITE_UINT32_LE (buffdata + 4, avimux->data_size);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
819
  memcpy (buffdata + 8, "movi", 4);
820
821
  buffdata += 12;
  GST_BUFFER_SIZE (buffer) += 12;
822
823
824
825
826
827
828
829
830
831

  return buffer;
}

static GstBuffer *
gst_avimux_riff_get_avix_header (guint32 datax_size)
{
  GstBuffer *buffer;
  guint8 *buffdata;

832
833
834
835
836
837
838
839
840
  buffer = gst_buffer_new_and_alloc (24);
  buffdata = GST_BUFFER_DATA (buffer);

  memcpy (buffdata + 0, "LIST", 4);
  GST_WRITE_UINT32_LE (buffdata + 4, datax_size + 4 * 4);
  memcpy (buffdata + 8, "AVIX", 4);
  memcpy (buffdata + 12, "LIST", 4);
  GST_WRITE_UINT32_LE (buffdata + 16, datax_size);
  memcpy (buffdata + 20, "movi", 4);
841
842
843
844
845
846
847
848

  return buffer;
}

static GstBuffer *
gst_avimux_riff_get_video_header (guint32 video_frame_size)
{
  GstBuffer *buffer;
849
  guint8 *buffdata;
850

851
852
853
854
  buffer = gst_buffer_new_and_alloc (8);
  buffdata = GST_BUFFER_DATA (buffer);
  memcpy (buffdata + 0, "00db", 4);
  GST_WRITE_UINT32_LE (buffdata + 4, video_frame_size);
855
856
857
858
859
860
861
862

  return buffer;
}

static GstBuffer *
gst_avimux_riff_get_audio_header (guint32 audio_sample_size)
{
  GstBuffer *buffer;
863
  guint8 *buffdata;
864

865
866
867
868
  buffer = gst_buffer_new_and_alloc (8);
  buffdata = GST_BUFFER_DATA (buffer);
  memcpy (buffdata + 0, "01wb", 4);
  GST_WRITE_UINT32_LE (buffdata + 4, audio_sample_size);
869
870
871
872
873
874

  return buffer;
}

/* some other usable functions (thankyou xawtv ;-) ) */

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
875
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
876
877
gst_avimux_add_index (GstAviMux * avimux, guchar * code, guint32 flags,
    guint32 size)
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
878
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
879
  if (avimux->idx_index == avimux->idx_count) {
880
    avimux->idx_count += 256;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
881
    avimux->idx =
882
883
        realloc (avimux->idx,
        avimux->idx_count * sizeof (gst_riff_index_entry));
884
  }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
885
886
887
888
  memcpy (&(avimux->idx[avimux->idx_index].id), code, 4);
  avimux->idx[avimux->idx_index].flags = LE_FROM_GUINT32 (flags);
  avimux->idx[avimux->idx_index].offset = LE_FROM_GUINT32 (avimux->idx_offset);
  avimux->idx[avimux->idx_index].size = LE_FROM_GUINT32 (size);
889
890
891
892
  avimux->idx_index++;
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
893
gst_avimux_write_index (GstAviMux * avimux)
894
895
{
  GstBuffer *buffer;
896
  guint8 *buffdata;
897

898
899
900
901
902
  buffer = gst_buffer_new_and_alloc (8);
  buffdata = GST_BUFFER_DATA (buffer);
  memcpy (buffdata + 0, "idx1", 4);
  GST_WRITE_UINT32_LE (buffdata + 4,
      avimux->idx_index * sizeof (gst_riff_index_entry));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
903
  gst_pad_push (avimux->srcpad, GST_DATA (buffer));
904

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
905
906
  buffer = gst_buffer_new ();
  GST_BUFFER_SIZE (buffer) = avimux->idx_index * sizeof (gst_riff_index_entry);
907
  GST_BUFFER_DATA (buffer) = (guint8 *) avimux->idx;
908
  avimux->idx = NULL;           /* will be free()'ed by gst_buffer_unref() */
909
  avimux->total_data += GST_BUFFER_SIZE (buffer) + 8;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
910
  gst_pad_push (avimux->srcpad, GST_DATA (buffer));
911

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
912
  avimux->idx_size += avimux->idx_index * sizeof (gst_riff_index_entry) + 8;
913
914
915
916
917
918

  /* update header */
  avimux->avi_hdr.flags |= GST_RIFF_AVIH_HASINDEX;
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
919
gst_avimux_bigfile (GstAviMux * avimux, gboolean last)
920
921
922
{
  GstBuffer *header;
  GstEvent *event;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
923
924

  if (avimux->is_bigfile) {
925
    /* sarch back */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
926
    event = gst_event_new_seek (GST_FORMAT_BYTES |
927
        GST_SEEK_METHOD_SET | GST_SEEK_FLAG_FLUSH, avimux->avix_start);
Wim Taymans's avatar
Wim Taymans committed
928
    /* if the event succeeds */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
929
    gst_pad_push (avimux->srcpad, GST_DATA (event));
930
931

    /* rewrite AVIX header */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
932
933
    header = gst_avimux_riff_get_avix_header (avimux->datax_size);
    gst_pad_push (avimux->srcpad, GST_DATA (header));
934
935

    /* go back to current location */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
936
    event = gst_event_new_seek (GST_FORMAT_BYTES |
937
        GST_SEEK_METHOD_SET | GST_SEEK_FLAG_FLUSH, avimux->total_data);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
938
    gst_pad_push (avimux->srcpad, GST_DATA (event));
939
940
941
942
943
944
945
946
947
948
  }
  avimux->avix_start = avimux->total_data;

  if (last)
    return;

  avimux->is_bigfile = TRUE;
  avimux->numx_frames = 0;
  avimux->datax_size = 0;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
949
950
951
  header = gst_avimux_riff_get_avix_header (0);
  avimux->total_data += GST_BUFFER_SIZE (header);
  gst_pad_push (avimux->srcpad, GST_DATA (header));
952
953
954
955
956
}

/* enough header blabla now, let's go on to actually writing the headers */

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
957
gst_avimux_start_file (GstAviMux * avimux)
958
959
960
961
962
{
  GstBuffer *header;

  avimux->total_data = 0;
  avimux->total_frames = 0;
963
  avimux->data_size = 4;        /* ? */
964
965
966
967
  avimux->datax_size = 0;
  avimux->num_frames = 0;
  avimux->numx_frames = 0;
  avimux->audio_size = 0;
968
  avimux->audio_time = 0;
969
  avimux->avix_start = 0;
970
971

  avimux->idx_index = 0;
972
  avimux->idx_offset = 0;       /* see 10 lines below */
973
974
975
976
  avimux->idx_size = 0;
  avimux->idx_count = 0;
  avimux->idx = NULL;

977
  /* header */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
978
979
980
  avimux->avi_hdr.streams =
      (avimux->video_pad_connected ? 1 : 0) +
      (avimux->audio_pad_connected ? 1 : 0);
981
  avimux->is_bigfile = FALSE;
982

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
983
984
985
  header = gst_avimux_riff_get_avi_header (avimux);
  avimux->total_data += GST_BUFFER_SIZE (header);
  gst_pad_push (avimux->srcpad, GST_DATA (header));
986

987
988
  avimux->idx_offset = avimux->total_data;

989
  avimux->write_header = FALSE;
990
  avimux->restart = FALSE;
991
992
993
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
994
gst_avimux_stop_file (GstAviMux * avimux)
995
996
997
998
999
{
  GstEvent *event;
  GstBuffer *header;

  /* if bigfile, rewrite header, else write indexes */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1000
1001
1002
  if (avimux->video_pad_connected) {
    if (avimux->is_bigfile) {
      gst_avimux_bigfile (avimux, TRUE);
1003
      avimux->idx_size = 0;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1004
1005
    } else {
      gst_avimux_write_index (avimux);
1006
1007
1008
    }
  }

1009
  /* set rate and everything having to do with that */
1010
  avimux->avi_hdr.max_bps = 0;
1011
1012
1013
  if (avimux->audio_pad_connected) {
    /* calculate bps if needed */
    if (!avimux->auds.av_bps) {
1014
      if (avimux->audio_time) {
1015
        avimux->auds.av_bps =