1 /* AVI muxer plugin for GStreamer
2 * Copyright (C) 2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
3 * (C) 2006 Mark Nauwelaerts <manauw@skynet.be>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * - the old avimuxer (by Wim Taymans)
23 * - xawtv's aviwriter (by Gerd Knorr)
24 * - mjpegtools' avilib (by Rainer Johanni)
25 * - openDML large-AVI docs
29 * SECTION:element-avimux
31 * Muxes raw or compressed audio and/or video streams into an AVI file.
34 * <title>Example launch lines</title>
35 * <para>(write everything in one line, without the backslash characters)</para>
37 * gst-launch videotestsrc num-buffers=250 \
38 * ! 'video/x-raw-yuv,format=(fourcc)I420,width=320,height=240,framerate=(fraction)25/1' \
40 * audiotestsrc num-buffers=440 ! audioconvert \
41 * ! 'audio/x-raw-int,rate=44100,channels=2' ! queue ! mux. \
42 * avimux name=mux ! filesink location=test.avi
43 * ]| This will create an .AVI file containing an uncompressed video stream
44 * with a test picture and an uncompressed audio stream containing a
47 * gst-launch videotestsrc num-buffers=250 \
48 * ! 'video/x-raw-yuv,format=(fourcc)I420,width=320,height=240,framerate=(fraction)25/1' \
49 * ! xvidenc ! queue ! mux. \
50 * audiotestsrc num-buffers=440 ! audioconvert ! 'audio/x-raw-int,rate=44100,channels=2' \
51 * ! lame ! queue ! mux. \
52 * avimux name=mux ! filesink location=test.avi
53 * ]| This will create an .AVI file containing the same test video and sound
54 * as above, only that both streams will be compressed this time. This will
55 * only work if you have the necessary encoder elements installed of course.
63 #include "gst/gst-i18n-plugin.h"
67 #include <gst/video/video.h>
69 #include "gstavimux.h"
71 GST_DEBUG_CATEGORY_STATIC (avimux_debug);
72 #define GST_CAT_DEFAULT avimux_debug
80 #define DEFAULT_BIGFILE TRUE
82 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
85 GST_STATIC_CAPS ("video/x-msvideo")
88 static GstStaticPadTemplate video_sink_factory =
89 GST_STATIC_PAD_TEMPLATE ("video_%d",
92 GST_STATIC_CAPS ("video/x-raw-yuv, "
93 "format = (fourcc) { YUY2, I420 }, "
94 "width = (int) [ 16, 4096 ], "
95 "height = (int) [ 16, 4096 ], "
96 "framerate = (fraction) [ 0, MAX ]; "
98 "width = (int) [ 16, 4096 ], "
99 "height = (int) [ 16, 4096 ], "
100 "framerate = (fraction) [ 0, MAX ]; "
102 "width = (int) [ 16, 4096 ], "
103 "height = (int) [ 16, 4096 ], "
104 "framerate = (fraction) [ 0, MAX ], "
105 "divxversion = (int) [ 3, 5 ]; "
107 "width = (int) [ 16, 4096 ], "
108 "height = (int) [ 16, 4096 ], "
109 "framerate = (fraction) [ 0, MAX ]; "
111 "width = (int) [ 16, 4096 ], "
112 "height = (int) [ 16, 4096 ], "
113 "framerate = (fraction) [ 0, MAX ]; "
115 "width = (int) [ 16, 4096 ], "
116 "height = (int) [ 16, 4096 ], "
117 "framerate = (fraction) [ 0, MAX ], "
118 "msmpegversion = (int) [ 41, 43 ]; "
120 "width = (int) [ 16, 4096 ], "
121 "height = (int) [ 16, 4096 ], "
122 "framerate = (fraction) [ 0, MAX ], "
123 "mpegversion = (int) { 1, 2, 4}, "
124 "systemstream = (boolean) FALSE; "
126 "width = (int) [ 16, 4096 ], "
127 "height = (int) [ 16, 4096 ], "
128 "framerate = (fraction) [ 0, MAX ]; "
130 "width = (int) [ 16, 4096 ], "
131 "height = (int) [ 16, 4096 ], "
132 "framerate = (fraction) [ 0, MAX ]; "
134 "width = (int) 720, "
135 "height = (int) { 576, 480 }, "
136 "framerate = (fraction) [ 0, MAX ], "
137 "systemstream = (boolean) FALSE; "
139 "width = (int) [ 16, 4096 ], "
140 "height = (int) [ 16, 4096 ], " "framerate = (fraction) [ 0, MAX ];"
142 "width = (int) [ 16, 4096 ], "
143 "height = (int) [ 16, 4096 ], " "framerate = (fraction) [ 0, MAX ];"
145 "width = (int) [ 16, 4096 ], "
146 "height = (int) [ 16, 4096 ], " "framerate = (fraction) [ 0, MAX ], "
147 "wmvversion = (int) [ 1, 3];"
149 "width = (int) [ 1, 2147483647 ], "
150 "height = (int) [ 1, 2147483647 ], "
151 "framerate = (fraction) [ 0, MAX ]")
154 static GstStaticPadTemplate audio_sink_factory =
155 GST_STATIC_PAD_TEMPLATE ("audio_%d",
158 GST_STATIC_CAPS ("audio/x-raw-int, "
159 "endianness = (int) LITTLE_ENDIAN, "
160 "signed = (boolean) { TRUE, FALSE }, "
161 "width = (int) { 8, 16 }, "
162 "depth = (int) { 8, 16 }, "
163 "rate = (int) [ 1000, 96000 ], "
164 "channels = (int) [ 1, 2 ]; "
166 "mpegversion = (int) 1, "
167 "layer = (int) [ 1, 3 ], "
168 "rate = (int) [ 1000, 96000 ], " "channels = (int) [ 1, 2 ]; "
170 "mpegversion = (int) 4, "
171 "stream-format = (string) raw, "
172 "rate = (int) [ 1000, 96000 ], " "channels = (int) [ 1, 2 ]; "
173 /*#if 0 VC6 doesn't support #if here ...
175 "rate = (int) [ 1000, 96000 ], " "channels = (int) [ 1, 2 ]; "
178 "rate = (int) [ 1000, 96000 ], " "channels = (int) [ 1, 2 ]; "
180 "rate = (int) [ 1000, 48000 ], " "channels = (int) [ 1, 2 ]; "
182 "rate = (int) [ 1000, 48000 ], " "channels = (int) [ 1, 2 ]; "
184 "rate = (int) [ 1000, 96000 ], " "channels = (int) [ 1, 2 ], "
185 "wmaversion = (int) [ 1, 2 ] ")
188 static void gst_avi_mux_base_init (gpointer g_class);
189 static void gst_avi_mux_class_init (GstAviMuxClass * klass);
190 static void gst_avi_mux_init (GstAviMux * avimux);
191 static void gst_avi_mux_pad_reset (GstAviPad * avipad, gboolean free);
193 static GstFlowReturn gst_avi_mux_collect_pads (GstCollectPads * pads,
195 static gboolean gst_avi_mux_handle_event (GstPad * pad, GstEvent * event);
196 static GstPad *gst_avi_mux_request_new_pad (GstElement * element,
197 GstPadTemplate * templ, const gchar * name);
198 static void gst_avi_mux_release_pad (GstElement * element, GstPad * pad);
199 static void gst_avi_mux_set_property (GObject * object,
200 guint prop_id, const GValue * value, GParamSpec * pspec);
201 static void gst_avi_mux_get_property (GObject * object,
202 guint prop_id, GValue * value, GParamSpec * pspec);
203 static GstStateChangeReturn gst_avi_mux_change_state (GstElement * element,
204 GstStateChange transition);
206 static GstElementClass *parent_class = NULL;
209 gst_avi_mux_get_type (void)
211 static GType avimux_type = 0;
214 static const GTypeInfo avimux_info = {
215 sizeof (GstAviMuxClass),
216 gst_avi_mux_base_init,
218 (GClassInitFunc) gst_avi_mux_class_init,
223 (GInstanceInitFunc) gst_avi_mux_init,
225 static const GInterfaceInfo tag_setter_info = {
232 g_type_register_static (GST_TYPE_ELEMENT, "GstAviMux", &avimux_info, 0);
233 g_type_add_interface_static (avimux_type, GST_TYPE_TAG_SETTER,
240 gst_avi_mux_base_init (gpointer g_class)
242 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
244 gst_element_class_add_pad_template (element_class,
245 gst_static_pad_template_get (&src_factory));
246 gst_element_class_add_pad_template (element_class,
247 gst_static_pad_template_get (&audio_sink_factory));
248 gst_element_class_add_pad_template (element_class,
249 gst_static_pad_template_get (&video_sink_factory));
251 gst_element_class_set_details_simple (element_class, "Avi muxer",
253 "Muxes audio and video into an avi stream",
254 "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
256 GST_DEBUG_CATEGORY_INIT (avimux_debug, "avimux", 0, "Muxer for AVI streams");
260 gst_avi_mux_finalize (GObject * object)
262 GstAviMux *mux = GST_AVI_MUX (object);
265 /* completely free each sinkpad */
266 node = mux->sinkpads;
268 GstAviPad *avipad = (GstAviPad *) node->data;
272 gst_avi_mux_pad_reset (avipad, TRUE);
275 g_slist_free (mux->sinkpads);
276 mux->sinkpads = NULL;
281 gst_object_unref (mux->collect);
283 G_OBJECT_CLASS (parent_class)->finalize (object);
287 gst_avi_mux_class_init (GstAviMuxClass * klass)
289 GObjectClass *gobject_class;
290 GstElementClass *gstelement_class;
292 gobject_class = (GObjectClass *) klass;
293 gstelement_class = (GstElementClass *) klass;
295 parent_class = g_type_class_peek_parent (klass);
297 gobject_class->get_property = gst_avi_mux_get_property;
298 gobject_class->set_property = gst_avi_mux_set_property;
299 gobject_class->finalize = gst_avi_mux_finalize;
301 g_object_class_install_property (gobject_class, ARG_BIGFILE,
302 g_param_spec_boolean ("bigfile", "Bigfile Support (>2GB)",
303 "Support for openDML-2.0 (big) AVI files", DEFAULT_BIGFILE,
306 gstelement_class->request_new_pad =
307 GST_DEBUG_FUNCPTR (gst_avi_mux_request_new_pad);
308 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_avi_mux_release_pad);
309 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_avi_mux_change_state);
312 /* reset pad to initial state
313 * free - if true, release all, not only stream related, data */
315 gst_avi_mux_pad_reset (GstAviPad * avipad, gboolean free)
318 memset (&(avipad->hdr), 0, sizeof (gst_riff_strh));
320 memset (&(avipad->idx[0]), 0, sizeof (avipad->idx));
323 g_free (avipad->tag);
325 g_free (avipad->idx_tag);
326 avipad->idx_tag = NULL;
329 if (avipad->is_video) {
330 GstAviVideoPad *vidpad = (GstAviVideoPad *) avipad;
332 avipad->hdr.type = GST_MAKE_FOURCC ('v', 'i', 'd', 's');
333 if (vidpad->vids_codec_data) {
334 gst_buffer_unref (vidpad->vids_codec_data);
335 vidpad->vids_codec_data = NULL;
338 if (vidpad->prepend_buffer) {
339 gst_buffer_unref (vidpad->prepend_buffer);
340 vidpad->prepend_buffer = NULL;
343 memset (&(vidpad->vids), 0, sizeof (gst_riff_strf_vids));
344 memset (&(vidpad->vprp), 0, sizeof (gst_riff_vprp));
346 GstAviAudioPad *audpad = (GstAviAudioPad *) avipad;
350 avipad->hdr.type = GST_MAKE_FOURCC ('a', 'u', 'd', 's');
351 if (audpad->auds_codec_data) {
352 gst_buffer_unref (audpad->auds_codec_data);
353 audpad->auds_codec_data = NULL;
356 memset (&(audpad->auds), 0, sizeof (gst_riff_strf_auds));
361 gst_avi_mux_reset (GstAviMux * avimux)
363 GSList *node, *newlist = NULL;
365 /* free and reset each sinkpad */
366 node = avimux->sinkpads;
368 GstAviPad *avipad = (GstAviPad *) node->data;
372 gst_avi_mux_pad_reset (avipad, FALSE);
373 /* if this pad has collectdata, keep it, otherwise dump it completely */
375 newlist = g_slist_append (newlist, avipad);
377 gst_avi_mux_pad_reset (avipad, TRUE);
382 /* free the old list of sinkpads, only keep the real collecting ones */
383 g_slist_free (avimux->sinkpads);
384 avimux->sinkpads = newlist;
387 avimux->num_frames = 0;
388 memset (&(avimux->avi_hdr), 0, sizeof (gst_riff_avih));
389 avimux->avi_hdr.max_bps = 10000000;
390 avimux->codec_data_size = 0;
392 if (avimux->tags_snap) {
393 gst_tag_list_free (avimux->tags_snap);
394 avimux->tags_snap = NULL;
397 g_free (avimux->idx);
401 avimux->write_header = TRUE;
404 gst_tag_setter_reset_tags (GST_TAG_SETTER (avimux));
408 gst_avi_mux_init (GstAviMux * avimux)
410 avimux->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
411 gst_pad_use_fixed_caps (avimux->srcpad);
412 gst_element_add_pad (GST_ELEMENT (avimux), avimux->srcpad);
415 avimux->enable_large_avi = DEFAULT_BIGFILE;
417 avimux->collect = gst_collect_pads_new ();
418 gst_collect_pads_set_function (avimux->collect,
419 (GstCollectPadsFunction) (GST_DEBUG_FUNCPTR (gst_avi_mux_collect_pads)),
422 /* set to clean state */
423 gst_avi_mux_reset (avimux);
427 gst_avi_mux_vidsink_set_caps (GstPad * pad, GstCaps * vscaps)
430 GstAviVideoPad *avipad;
431 GstAviCollectData *collect_pad;
432 GstStructure *structure;
433 const gchar *mimetype;
434 const GValue *fps, *par;
435 const GValue *codec_data;
438 gboolean codec_data_in_headers = TRUE;
440 avimux = GST_AVI_MUX (gst_pad_get_parent (pad));
442 /* find stream data */
443 collect_pad = (GstAviCollectData *) gst_pad_get_element_private (pad);
444 g_assert (collect_pad);
445 avipad = (GstAviVideoPad *) collect_pad->avipad;
447 g_assert (avipad->parent.is_video);
448 g_assert (avipad->parent.hdr.type == GST_MAKE_FOURCC ('v', 'i', 'd', 's'));
450 GST_DEBUG_OBJECT (avimux, "%s:%s, caps=%" GST_PTR_FORMAT,
451 GST_DEBUG_PAD_NAME (pad), vscaps);
453 structure = gst_caps_get_structure (vscaps, 0);
454 mimetype = gst_structure_get_name (structure);
457 avipad->vids.size = sizeof (gst_riff_strf_vids);
458 avipad->vids.planes = 1;
459 if (!gst_structure_get_int (structure, "width", &width) ||
460 !gst_structure_get_int (structure, "height", &height)) {
464 avipad->vids.width = width;
465 avipad->vids.height = height;
467 fps = gst_structure_get_value (structure, "framerate");
468 if (fps == NULL || !GST_VALUE_HOLDS_FRACTION (fps))
471 avipad->parent.hdr.rate = gst_value_get_fraction_numerator (fps);
472 avipad->parent.hdr.scale = gst_value_get_fraction_denominator (fps);
474 /* (pixel) aspect ratio data, if any */
475 par = gst_structure_get_value (structure, "pixel-aspect-ratio");
476 /* only use video properties header if there is non-trivial aspect info */
477 if (par && GST_VALUE_HOLDS_FRACTION (par) &&
478 ((par_n = gst_value_get_fraction_numerator (par)) !=
479 (par_d = gst_value_get_fraction_denominator (par)))) {
480 GValue to_ratio = { 0, };
481 guint ratio_n, ratio_d;
483 /* some fraction voodoo to obtain simplest possible ratio */
484 g_value_init (&to_ratio, GST_TYPE_FRACTION);
485 gst_value_set_fraction (&to_ratio, width * par_n, height * par_d);
486 ratio_n = gst_value_get_fraction_numerator (&to_ratio);
487 ratio_d = gst_value_get_fraction_denominator (&to_ratio);
488 GST_DEBUG_OBJECT (avimux, "generating vprp data with aspect ratio %d/%d",
491 avipad->vprp.vert_rate = avipad->parent.hdr.rate / avipad->parent.hdr.scale;
492 avipad->vprp.hor_t_total = width;
493 avipad->vprp.vert_lines = height;
494 avipad->vprp.aspect = (ratio_n) << 16 | (ratio_d & 0xffff);
495 avipad->vprp.width = width;
496 avipad->vprp.height = height;
497 avipad->vprp.fields = 1;
498 avipad->vprp.field_info[0].compressed_bm_height = height;
499 avipad->vprp.field_info[0].compressed_bm_width = width;
500 avipad->vprp.field_info[0].valid_bm_height = height;
501 avipad->vprp.field_info[0].valid_bm_width = width;
504 if (!strcmp (mimetype, "video/x-raw-yuv")) {
507 gst_structure_get_fourcc (structure, "format", &format);
508 avipad->vids.compression = format;
510 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
511 avipad->vids.bit_cnt = 16;
513 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
514 avipad->vids.bit_cnt = 12;
518 avipad->vids.bit_cnt = 24;
519 avipad->vids.compression = 0;
522 if (!strcmp (mimetype, "video/x-huffyuv")) {
523 avipad->vids.compression = GST_MAKE_FOURCC ('H', 'F', 'Y', 'U');
524 } else if (!strcmp (mimetype, "image/jpeg")) {
525 avipad->vids.compression = GST_MAKE_FOURCC ('M', 'J', 'P', 'G');
526 } else if (!strcmp (mimetype, "video/x-divx")) {
529 gst_structure_get_int (structure, "divxversion", &divxversion);
530 switch (divxversion) {
532 avipad->vids.compression = GST_MAKE_FOURCC ('D', 'I', 'V', '3');
535 avipad->vids.compression = GST_MAKE_FOURCC ('D', 'I', 'V', 'X');
538 avipad->vids.compression = GST_MAKE_FOURCC ('D', 'X', '5', '0');
541 } else if (!strcmp (mimetype, "video/x-xvid")) {
542 avipad->vids.compression = GST_MAKE_FOURCC ('X', 'V', 'I', 'D');
543 } else if (!strcmp (mimetype, "video/x-3ivx")) {
544 avipad->vids.compression = GST_MAKE_FOURCC ('3', 'I', 'V', '2');
545 } else if (gst_structure_has_name (structure, "video/x-msmpeg")) {
548 gst_structure_get_int (structure, "msmpegversion", &msmpegversion);
549 switch (msmpegversion) {
551 avipad->vids.compression = GST_MAKE_FOURCC ('M', 'P', 'G', '4');
554 avipad->vids.compression = GST_MAKE_FOURCC ('M', 'P', '4', '2');
557 avipad->vids.compression = GST_MAKE_FOURCC ('M', 'P', '4', '3');
560 GST_INFO ("unhandled msmpegversion : %d, fall back to fourcc=MPEG",
562 avipad->vids.compression = GST_MAKE_FOURCC ('M', 'P', 'E', 'G');
565 } else if (!strcmp (mimetype, "video/x-dv")) {
566 avipad->vids.compression = GST_MAKE_FOURCC ('D', 'V', 'S', 'D');
567 } else if (!strcmp (mimetype, "video/x-h263")) {
568 avipad->vids.compression = GST_MAKE_FOURCC ('H', '2', '6', '3');
569 } else if (!strcmp (mimetype, "video/x-h264")) {
570 avipad->vids.compression = GST_MAKE_FOURCC ('H', '2', '6', '4');
571 } else if (!strcmp (mimetype, "video/mpeg")) {
574 gst_structure_get_int (structure, "mpegversion", &mpegversion);
576 switch (mpegversion) {
578 avipad->vids.compression = GST_MAKE_FOURCC ('M', 'P', 'G', '2');
581 /* mplayer/ffmpeg might not work with DIVX, but with FMP4 */
582 avipad->vids.compression = GST_MAKE_FOURCC ('D', 'I', 'V', 'X');
584 /* DIVX/XVID in AVI store the codec_data chunk as part of the
585 first data buffer. So for this case, we prepend the codec_data
586 blob (if any) to that first buffer */
587 codec_data_in_headers = FALSE;
590 GST_INFO ("unhandled mpegversion : %d, fall back to fourcc=MPEG",
592 avipad->vids.compression = GST_MAKE_FOURCC ('M', 'P', 'E', 'G');
595 } else if (!strcmp (mimetype, "video/x-dirac")) {
596 avipad->vids.compression = GST_MAKE_FOURCC ('d', 'r', 'a', 'c');
597 } else if (!strcmp (mimetype, "video/x-wmv")) {
600 if (gst_structure_get_int (structure, "wmvversion", &wmvversion)) {
601 switch (wmvversion) {
603 avipad->vids.compression = GST_MAKE_FOURCC ('W', 'M', 'V', '1');
606 avipad->vids.compression = GST_MAKE_FOURCC ('W', 'M', 'V', '2');
609 avipad->vids.compression = GST_MAKE_FOURCC ('W', 'M', 'V', '3');
614 } else if (!strcmp (mimetype, "image/x-jpc")) {
615 avipad->vids.compression = GST_MAKE_FOURCC ('M', 'J', '2', 'C');
618 if (!avipad->vids.compression)
622 /* codec initialization data, if any */
623 codec_data = gst_structure_get_value (structure, "codec_data");
625 if (codec_data_in_headers) {
626 avipad->vids_codec_data = gst_value_get_buffer (codec_data);
627 gst_buffer_ref (avipad->vids_codec_data);
628 /* keep global track of size */
629 avimux->codec_data_size += GST_BUFFER_SIZE (avipad->vids_codec_data);
631 avipad->prepend_buffer =
632 gst_buffer_ref (gst_value_get_buffer (codec_data));
636 avipad->parent.hdr.fcc_handler = avipad->vids.compression;
637 avipad->vids.image_size = avipad->vids.height * avipad->vids.width;
638 /* hm, maybe why avi only handles one stream well ... */
639 avimux->avi_hdr.width = avipad->vids.width;
640 avimux->avi_hdr.height = avipad->vids.height;
641 avimux->avi_hdr.us_frame = 1000000. * avipad->parent.hdr.scale /
642 avipad->parent.hdr.rate;
644 gst_object_unref (avimux);
649 GST_WARNING_OBJECT (avimux, "refused caps %" GST_PTR_FORMAT, vscaps);
650 gst_object_unref (avimux);
656 gst_avi_mux_audsink_scan_mpeg_audio (GstAviMux * avimux, GstAviPad * avipad,
667 data = GST_BUFFER_DATA (buffer);
668 size = GST_BUFFER_SIZE (buffer);
673 header = GST_READ_UINT32_BE (data);
675 if ((header & 0xffe00000) != 0xffe00000)
678 /* thanks go to mp3parse */
679 if (header & (1 << 20)) {
680 lsf = (header & (1 << 19)) ? 0 : 1;
687 version = 1 + lsf + mpg25;
688 layer = 4 - ((header >> 17) & 0x3);
690 /* see http://www.codeproject.com/audio/MPEGAudioInfo.asp */
695 else if (version == 1) {
698 /* MPEG-2 or "2.5" */
702 if (G_UNLIKELY (avipad->hdr.scale <= 1))
703 avipad->hdr.scale = spf;
704 else if (G_UNLIKELY (avipad->hdr.scale != spf)) {
705 GST_WARNING_OBJECT (avimux, "input mpeg audio has varying frame size");
714 GST_WARNING_OBJECT (avimux, "input mpeg audio is not parsed");
719 GST_WARNING_OBJECT (avimux, "falling back to CBR muxing");
720 avipad->hdr.scale = 1;
721 /* no need to check further */
728 gst_avi_mux_audsink_set_fields (GstAviMux * avimux, GstAviAudioPad * avipad)
730 if (avipad->parent.hdr.scale > 1) {
731 /* vbr case: fixed duration per frame/chunk */
732 avipad->parent.hdr.rate = avipad->auds.rate;
733 avipad->parent.hdr.samplesize = 0;
734 /* FIXME ?? some rumours say this should be largest audio chunk size */
735 avipad->auds.blockalign = avipad->parent.hdr.scale;
737 /* by spec, hdr.rate is av_bps related, is calculated that way in stop_file,
738 * and reduces to sample rate in PCM like cases */
739 avipad->parent.hdr.rate = avipad->auds.av_bps / avipad->auds.blockalign;
740 avipad->parent.hdr.samplesize = avipad->auds.blockalign;
741 avipad->parent.hdr.scale = 1;
746 gst_avi_mux_audsink_set_caps (GstPad * pad, GstCaps * vscaps)
749 GstAviAudioPad *avipad;
750 GstAviCollectData *collect_pad;
751 GstStructure *structure;
752 const gchar *mimetype;
753 const GValue *codec_data;
756 avimux = GST_AVI_MUX (gst_pad_get_parent (pad));
758 /* find stream data */
759 collect_pad = (GstAviCollectData *) gst_pad_get_element_private (pad);
760 g_assert (collect_pad);
761 avipad = (GstAviAudioPad *) collect_pad->avipad;
763 g_assert (!avipad->parent.is_video);
764 g_assert (avipad->parent.hdr.type == GST_MAKE_FOURCC ('a', 'u', 'd', 's'));
766 GST_DEBUG_OBJECT (avimux, "%s:%s, caps=%" GST_PTR_FORMAT,
767 GST_DEBUG_PAD_NAME (pad), vscaps);
769 structure = gst_caps_get_structure (vscaps, 0);
770 mimetype = gst_structure_get_name (structure);
772 /* we want these for all */
773 if (!gst_structure_get_int (structure, "channels", &channels) ||
774 !gst_structure_get_int (structure, "rate", &rate)) {
778 avipad->auds.channels = channels;
779 avipad->auds.rate = rate;
781 /* codec initialization data, if any */
782 codec_data = gst_structure_get_value (structure, "codec_data");
784 avipad->auds_codec_data = gst_value_get_buffer (codec_data);
785 gst_buffer_ref (avipad->auds_codec_data);
786 /* keep global track of size */
787 avimux->codec_data_size += GST_BUFFER_SIZE (avipad->auds_codec_data);
790 if (!strcmp (mimetype, "audio/x-raw-int")) {
794 avipad->auds.format = GST_RIFF_WAVE_FORMAT_PCM;
796 if (!gst_structure_get_int (structure, "width", &width) ||
797 !gst_structure_get_int (structure, "depth", &depth) ||
798 !gst_structure_get_boolean (structure, "signed", &signedness)) {
799 GST_DEBUG_OBJECT (avimux,
800 "broken caps, width/depth/signed field missing");
804 /* no clear place to put different values for these while keeping to spec */
805 if (width != depth) {
806 GST_DEBUG_OBJECT (avimux, "width must be same as depth!");
810 /* because that's the way the caps will be recreated from riff data */
811 if ((width == 8 && signedness) || (width == 16 && !signedness)) {
812 GST_DEBUG_OBJECT (avimux,
813 "8-bit PCM must be unsigned, 16-bit PCM signed");
817 avipad->auds.blockalign = width;
818 avipad->auds.size = (width == 8) ? 8 : depth;
820 /* set some more info straight */
821 avipad->auds.blockalign /= 8;
822 avipad->auds.blockalign *= avipad->auds.channels;
823 avipad->auds.av_bps = avipad->auds.blockalign * avipad->auds.rate;
825 avipad->auds.format = 0;
826 /* set some defaults */
827 avipad->auds.blockalign = 1;
828 avipad->auds.av_bps = 0;
829 avipad->auds.size = 16;
831 if (!strcmp (mimetype, "audio/mpeg")) {
834 gst_structure_get_int (structure, "mpegversion", &mpegversion);
835 switch (mpegversion) {
838 gboolean parsed = FALSE;
840 gst_structure_get_int (structure, "layer", &layer);
841 gst_structure_get_boolean (structure, "parsed", &parsed);
844 avipad->auds.format = GST_RIFF_WAVE_FORMAT_MPEGL3;
848 avipad->auds.format = GST_RIFF_WAVE_FORMAT_MPEGL12;
852 /* treat as VBR, should also cover CBR case;
853 * setup hook to parse frame header and determine spf */
854 avipad->parent.hook = gst_avi_mux_audsink_scan_mpeg_audio;
856 GST_WARNING_OBJECT (avimux, "unparsed MPEG audio input (?), "
863 GstBuffer *codec_data_buf = avipad->auds_codec_data;
864 const gchar *stream_format;
867 stream_format = gst_structure_get_string (structure, "stream-format");
869 if (strcmp (stream_format, "raw") != 0) {
870 GST_WARNING_OBJECT (avimux, "AAC's stream format '%s' is not "
871 "supported, please use 'raw'", stream_format);
875 GST_WARNING_OBJECT (avimux, "AAC's stream-format not specified, "
879 /* vbr case needs some special handling */
880 if (!codec_data_buf || GST_BUFFER_SIZE (codec_data_buf) < 2) {
881 GST_WARNING_OBJECT (avimux, "no (valid) codec_data for AAC audio");
884 avipad->auds.format = GST_RIFF_WAVE_FORMAT_AAC;
885 /* need to determine frame length */
886 codec = GST_READ_UINT16_BE (GST_BUFFER_DATA (codec_data_buf));
887 avipad->parent.hdr.scale = (codec & 0x4) ? 960 : 1024;
891 } else if (!strcmp (mimetype, "audio/x-vorbis")) {
892 avipad->auds.format = GST_RIFF_WAVE_FORMAT_VORBIS3;
893 } else if (!strcmp (mimetype, "audio/x-ac3")) {
894 avipad->auds.format = GST_RIFF_WAVE_FORMAT_A52;
895 } else if (!strcmp (mimetype, "audio/x-alaw")) {
896 avipad->auds.format = GST_RIFF_WAVE_FORMAT_ALAW;
897 avipad->auds.size = 8;
898 avipad->auds.blockalign = avipad->auds.channels;
899 avipad->auds.av_bps = avipad->auds.blockalign * avipad->auds.rate;
900 } else if (!strcmp (mimetype, "audio/x-mulaw")) {
901 avipad->auds.format = GST_RIFF_WAVE_FORMAT_MULAW;
902 avipad->auds.size = 8;
903 avipad->auds.blockalign = avipad->auds.channels;
904 avipad->auds.av_bps = avipad->auds.blockalign * avipad->auds.rate;
905 } else if (!strcmp (mimetype, "audio/x-wma")) {
910 if (gst_structure_get_int (structure, "wmaversion", &version)) {
913 avipad->auds.format = GST_RIFF_WAVE_FORMAT_WMAV1;
916 avipad->auds.format = GST_RIFF_WAVE_FORMAT_WMAV2;
923 if (avipad->auds.format != 0) {
924 if (gst_structure_get_int (structure, "block_align", &block_align)) {
925 avipad->auds.blockalign = block_align;
927 if (gst_structure_get_int (structure, "bitrate", &bitrate)) {
928 avipad->auds.av_bps = bitrate / 8;
934 if (!avipad->auds.format)
937 gst_avi_mux_audsink_set_fields (avimux, avipad);
939 gst_object_unref (avimux);
944 GST_WARNING_OBJECT (avimux, "refused caps %" GST_PTR_FORMAT, vscaps);
945 gst_object_unref (avimux);
952 gst_avi_mux_request_new_pad (GstElement * element,
953 GstPadTemplate * templ, const gchar * req_name)
958 GstElementClass *klass;
960 g_return_val_if_fail (templ != NULL, NULL);
962 if (templ->direction != GST_PAD_SINK)
963 goto wrong_direction;
965 g_return_val_if_fail (GST_IS_AVI_MUX (element), NULL);
966 avimux = GST_AVI_MUX (element);
968 if (!avimux->write_header)
971 klass = GST_ELEMENT_GET_CLASS (element);
973 if (templ == gst_element_class_get_pad_template (klass, "audio_%d")) {
977 name = g_strdup_printf ("audio_%02d", avimux->audio_pads);
978 GST_DEBUG_OBJECT (avimux, "adding new pad: %s", name);
979 newpad = gst_pad_new_from_template (templ, name);
981 gst_pad_set_setcaps_function (newpad,
982 GST_DEBUG_FUNCPTR (gst_avi_mux_audsink_set_caps));
984 /* init pad specific data */
985 avipad = g_malloc0 (sizeof (GstAviAudioPad));
986 avipad->is_video = FALSE;
987 avipad->hdr.type = GST_MAKE_FOURCC ('a', 'u', 'd', 's');
988 avimux->audio_pads++;
989 /* audio goes last */
990 avimux->sinkpads = g_slist_append (avimux->sinkpads, avipad);
991 } else if (templ == gst_element_class_get_pad_template (klass, "video_%d")) {
992 /* though streams are pretty generic and relatively self-contained,
993 * some video info goes in a single avi header -and therefore mux struct-
994 * so video restricted to one stream */
995 if (avimux->video_pads > 0)
998 GST_DEBUG_OBJECT (avimux, "adding new pad: video_00");
999 newpad = gst_pad_new_from_template (templ, "video_00");
1000 gst_pad_set_setcaps_function (newpad,
1001 GST_DEBUG_FUNCPTR (gst_avi_mux_vidsink_set_caps));
1002 avipad = g_malloc0 (sizeof (GstAviVideoPad));
1004 /* init pad specific data */
1005 avipad->is_video = TRUE;
1006 avipad->hdr.type = GST_MAKE_FOURCC ('v', 'i', 'd', 's');
1007 avimux->video_pads++;
1008 /* video goes first */
1009 avimux->sinkpads = g_slist_prepend (avimux->sinkpads, avipad);
1011 goto wrong_template;
1013 avipad->collect = gst_collect_pads_add_pad (avimux->collect,
1014 newpad, sizeof (GstAviCollectData));
1015 ((GstAviCollectData *) (avipad->collect))->avipad = avipad;
1016 /* FIXME: hacked way to override/extend the event function of
1017 * GstCollectPads; because it sets its own event function giving the
1018 * element no access to events */
1019 avimux->collect_event = (GstPadEventFunction) GST_PAD_EVENTFUNC (newpad);
1020 gst_pad_set_event_function (newpad,
1021 GST_DEBUG_FUNCPTR (gst_avi_mux_handle_event));
1023 gst_element_add_pad (element, newpad);
1030 g_warning ("avimux: request pad that is not a SINK pad\n");
1035 g_warning ("avimux: request pad cannot be added after streaming started\n");
1040 g_warning ("avimux: this is not our template!\n");
1046 gst_avi_mux_release_pad (GstElement * element, GstPad * pad)
1048 GstAviMux *avimux = GST_AVI_MUX (element);
1051 node = avimux->sinkpads;
1053 GstAviPad *avipad = (GstAviPad *) node->data;
1055 if (avipad->collect->pad == pad) {
1056 /* pad count should not be adjusted,
1057 * as it also represent number of streams present */
1058 avipad->collect = NULL;
1059 GST_DEBUG_OBJECT (avimux, "removed pad '%s'", GST_PAD_NAME (pad));
1060 gst_collect_pads_remove_pad (avimux->collect, pad);
1061 gst_element_remove_pad (element, pad);
1062 /* if not started yet, we can remove any sign this pad ever existed */
1063 /* in this case _start will take care of the real pad count */
1064 if (avimux->write_header) {
1065 avimux->sinkpads = g_slist_remove (avimux->sinkpads, avipad);
1066 gst_avi_mux_pad_reset (avipad, TRUE);
1075 g_warning ("Unknown pad %s", GST_PAD_NAME (pad));
1078 /* maybe some of these functions should be moved to riff.h? */
1080 /* DISCLAIMER: this function is fairly ugly. So be it (i.e. it makes the rest easier)
1081 * so is this struct */
1083 typedef struct _GstMarkedBuffer
1090 gst_avi_mux_write_tag (const GstTagList * list, const gchar * tag,
1099 GST_RIFF_INFO_IARL, GST_TAG_LOCATION}, {
1100 GST_RIFF_INFO_IART, GST_TAG_ARTIST}, {
1101 GST_RIFF_INFO_ICMT, GST_TAG_COMMENT}, {
1102 GST_RIFF_INFO_ICOP, GST_TAG_COPYRIGHT}, {
1103 GST_RIFF_INFO_ICRD, GST_TAG_DATE}, {
1104 GST_RIFF_INFO_IGNR, GST_TAG_GENRE}, {
1105 GST_RIFF_INFO_IKEY, GST_TAG_KEYWORDS}, {
1106 GST_RIFF_INFO_INAM, GST_TAG_TITLE}, {
1107 GST_RIFF_INFO_ISFT, GST_TAG_ENCODER}, {
1108 GST_RIFF_INFO_ISRC, GST_TAG_ISRC}, {
1112 GstBuffer *buf = ((GstMarkedBuffer *) data)->buffer;
1113 guint *highmark = ((GstMarkedBuffer *) data)->highmark;
1114 guint8 *buffdata = GST_BUFFER_DATA (buf) + *highmark;
1117 for (n = 0; rifftags[n].fcc != 0; n++) {
1118 if (!strcmp (rifftags[n].tag, tag) &&
1119 gst_tag_list_get_string (list, tag, &str) && str) {
1124 if (GST_BUFFER_SIZE (buf) >= *highmark + 8 + plen) {
1125 GST_WRITE_UINT32_LE (buffdata, rifftags[n].fcc);
1126 GST_WRITE_UINT32_LE (buffdata + 4, len + 1);
1127 memcpy (buffdata + 8, str, len);
1128 buffdata[8 + len] = 0;
1129 *highmark += 8 + plen;
1130 GST_DEBUG ("writing tag in buffer %p, highmark at %d", buf, *highmark);
1138 #define ODML_SUPERINDEX_SIZE \
1139 (32 + GST_AVI_SUPERINDEX_COUNT * sizeof (gst_avi_superindex_entry))
1142 gst_avi_mux_riff_get_avi_header (GstAviMux * avimux)
1144 const GstTagList *tags;
1150 /* pointer to list size field */
1151 guint8 *riff_size, *hdrl_size;
1154 GST_DEBUG_OBJECT (avimux, "creating avi header, data_size %u, idx_size %u",
1155 avimux->data_size, avimux->idx_size);
1157 if (avimux->tags_snap)
1158 tags = avimux->tags_snap;
1160 /* need to make snapshot of current state of tags to ensure the same set
1161 * is used next time around during header rewrite at the end */
1162 tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (avimux));
1164 tags = avimux->tags_snap = gst_tag_list_copy (tags);
1167 /* that should be the strlen of all tags + header sizes
1168 * not all of tags end up in a avi, still this is a good estimate
1170 gchar *str = gst_structure_to_string (tags);
1171 size += strlen (str) + 8 * gst_structure_n_fields (tags);
1175 /* allocate the buffer, starting with some wild/safe upper bound */
1176 size += avimux->codec_data_size + 100 + sizeof (gst_riff_avih)
1177 + (g_slist_length (avimux->sinkpads) * (100 + sizeof (gst_riff_strh_full)
1178 + sizeof (gst_riff_strf_vids)
1179 + sizeof (gst_riff_vprp)
1180 + sizeof (gst_riff_vprp_video_field_desc) * 2
1181 + sizeof (gst_riff_strf_auds) + 2 + ODML_SUPERINDEX_SIZE));
1182 buffer = gst_buffer_new_and_alloc (size);
1183 buffdata = GST_BUFFER_DATA (buffer);
1185 GST_DEBUG_OBJECT (avimux, "creating buffer %p, size %d, highmark at 0",
1186 buffer, GST_BUFFER_SIZE (buffer));
1188 /* avi header metadata */
1189 memcpy (buffdata + 0, "RIFF", 4);
1190 /* fill in RIFF size later */
1191 riff_size = buffdata + 4;
1192 memcpy (buffdata + 8, "AVI ", 4);
1193 memcpy (buffdata + 12, "LIST", 4);
1194 /* fill in header size later */
1195 hdrl_size = buffdata + 16;
1196 memcpy (buffdata + 20, "hdrl", 4);
1197 memcpy (buffdata + 24, "avih", 4);
1198 GST_WRITE_UINT32_LE (buffdata + 28, sizeof (gst_riff_avih));
1202 /* the AVI header itself */
1203 GST_WRITE_UINT32_LE (buffdata + 0, avimux->avi_hdr.us_frame);
1204 GST_WRITE_UINT32_LE (buffdata + 4, avimux->avi_hdr.max_bps);
1205 GST_WRITE_UINT32_LE (buffdata + 8, avimux->avi_hdr.pad_gran);
1206 GST_WRITE_UINT32_LE (buffdata + 12, avimux->avi_hdr.flags);
1207 GST_WRITE_UINT32_LE (buffdata + 16, avimux->avi_hdr.tot_frames);
1208 GST_WRITE_UINT32_LE (buffdata + 20, avimux->avi_hdr.init_frames);
1209 GST_WRITE_UINT32_LE (buffdata + 24, avimux->avi_hdr.streams);
1210 GST_WRITE_UINT32_LE (buffdata + 28, avimux->avi_hdr.bufsize);
1211 GST_WRITE_UINT32_LE (buffdata + 32, avimux->avi_hdr.width);
1212 GST_WRITE_UINT32_LE (buffdata + 36, avimux->avi_hdr.height);
1213 GST_WRITE_UINT32_LE (buffdata + 40, avimux->avi_hdr.scale);
1214 GST_WRITE_UINT32_LE (buffdata + 44, avimux->avi_hdr.rate);
1215 GST_WRITE_UINT32_LE (buffdata + 48, avimux->avi_hdr.start);
1216 GST_WRITE_UINT32_LE (buffdata + 52, avimux->avi_hdr.length);
1221 node = avimux->sinkpads;
1223 GstAviPad *avipad = (GstAviPad *) node->data;
1224 GstAviVideoPad *vidpad = (GstAviVideoPad *) avipad;
1225 GstAviAudioPad *audpad = (GstAviAudioPad *) avipad;
1226 guint codec_size = 0, strl_size = 0, vprp_size = 0;
1228 if (avipad->is_video) {
1229 if (vidpad->vids_codec_data)
1230 codec_size = GST_BUFFER_SIZE (vidpad->vids_codec_data);
1231 strl_size = sizeof (gst_riff_strh_full) + sizeof (gst_riff_strf_vids)
1232 + GST_ROUND_UP_2 (codec_size) + 4 * 5 + ODML_SUPERINDEX_SIZE;
1233 if (vidpad->vprp.aspect) {
1234 /* let's be on the safe side */
1235 vidpad->vprp.fields = MIN (vidpad->vprp.fields,
1236 GST_RIFF_VPRP_VIDEO_FIELDS);
1237 vprp_size = G_STRUCT_OFFSET (gst_riff_vprp, field_info)
1238 + (vidpad->vprp.fields * sizeof (gst_riff_vprp_video_field_desc));
1239 strl_size += 4 * 2 + vprp_size;
1242 if (audpad->auds_codec_data)
1243 codec_size = GST_BUFFER_SIZE (audpad->auds_codec_data);
1244 /* +2 is codec_size field, not part of gst_riff_strf_auds */
1245 strl_size = sizeof (gst_riff_strh_full) + sizeof (gst_riff_strf_auds) + 2
1246 + GST_ROUND_UP_2 (codec_size) + 4 * 5 + ODML_SUPERINDEX_SIZE;
1249 /* stream list metadata */
1250 memcpy (buffdata + 0, "LIST", 4);
1251 GST_WRITE_UINT32_LE (buffdata + 4, strl_size);
1252 memcpy (buffdata + 8, "strl", 4);
1253 /* generic header */
1254 memcpy (buffdata + 12, "strh", 4);
1255 GST_WRITE_UINT32_LE (buffdata + 16, sizeof (gst_riff_strh_full));
1256 /* the actual header */
1257 GST_WRITE_UINT32_LE (buffdata + 20, avipad->hdr.type);
1258 GST_WRITE_UINT32_LE (buffdata + 24, avipad->hdr.fcc_handler);
1259 GST_WRITE_UINT32_LE (buffdata + 28, avipad->hdr.flags);
1260 GST_WRITE_UINT32_LE (buffdata + 32, avipad->hdr.priority);
1261 GST_WRITE_UINT32_LE (buffdata + 36, avipad->hdr.init_frames);
1262 GST_WRITE_UINT32_LE (buffdata + 40, avipad->hdr.scale);
1263 GST_WRITE_UINT32_LE (buffdata + 44, avipad->hdr.rate);
1264 GST_WRITE_UINT32_LE (buffdata + 48, avipad->hdr.start);
1265 GST_WRITE_UINT32_LE (buffdata + 52, avipad->hdr.length);
1266 GST_WRITE_UINT32_LE (buffdata + 56, avipad->hdr.bufsize);
1267 GST_WRITE_UINT32_LE (buffdata + 60, avipad->hdr.quality);
1268 GST_WRITE_UINT32_LE (buffdata + 64, avipad->hdr.samplesize);
1269 GST_WRITE_UINT16_LE (buffdata + 68, 0);
1270 GST_WRITE_UINT16_LE (buffdata + 70, 0);
1271 GST_WRITE_UINT16_LE (buffdata + 72, 0);
1272 GST_WRITE_UINT16_LE (buffdata + 74, 0);
1276 if (avipad->is_video) {
1277 /* the video header */
1278 memcpy (buffdata + 0, "strf", 4);
1279 GST_WRITE_UINT32_LE (buffdata + 4,
1280 sizeof (gst_riff_strf_vids) + codec_size);
1281 /* the actual header */
1282 GST_WRITE_UINT32_LE (buffdata + 8, vidpad->vids.size + codec_size);
1283 GST_WRITE_UINT32_LE (buffdata + 12, vidpad->vids.width);
1284 GST_WRITE_UINT32_LE (buffdata + 16, vidpad->vids.height);
1285 GST_WRITE_UINT16_LE (buffdata + 20, vidpad->vids.planes);
1286 GST_WRITE_UINT16_LE (buffdata + 22, vidpad->vids.bit_cnt);
1287 GST_WRITE_UINT32_LE (buffdata + 24, vidpad->vids.compression);
1288 GST_WRITE_UINT32_LE (buffdata + 28, vidpad->vids.image_size);
1289 GST_WRITE_UINT32_LE (buffdata + 32, vidpad->vids.xpels_meter);
1290 GST_WRITE_UINT32_LE (buffdata + 36, vidpad->vids.ypels_meter);
1291 GST_WRITE_UINT32_LE (buffdata + 40, vidpad->vids.num_colors);
1292 GST_WRITE_UINT32_LE (buffdata + 44, vidpad->vids.imp_colors);
1296 /* include codec data, if any */
1298 memcpy (buffdata, GST_BUFFER_DATA (vidpad->vids_codec_data),
1301 buffdata += codec_size;
1302 highmark += codec_size;
1305 if (highmark & 0x1) {
1310 /* add video property data, mainly for aspect ratio, if any */
1314 /* the vprp header */
1315 memcpy (buffdata + 0, "vprp", 4);
1316 GST_WRITE_UINT32_LE (buffdata + 4, vprp_size);
1317 /* the actual data */
1318 GST_WRITE_UINT32_LE (buffdata + 8, vidpad->vprp.format_token);
1319 GST_WRITE_UINT32_LE (buffdata + 12, vidpad->vprp.standard);
1320 GST_WRITE_UINT32_LE (buffdata + 16, vidpad->vprp.vert_rate);
1321 GST_WRITE_UINT32_LE (buffdata + 20, vidpad->vprp.hor_t_total);
1322 GST_WRITE_UINT32_LE (buffdata + 24, vidpad->vprp.vert_lines);
1323 GST_WRITE_UINT32_LE (buffdata + 28, vidpad->vprp.aspect);
1324 GST_WRITE_UINT32_LE (buffdata + 32, vidpad->vprp.width);
1325 GST_WRITE_UINT32_LE (buffdata + 36, vidpad->vprp.height);
1326 GST_WRITE_UINT32_LE (buffdata + 40, vidpad->vprp.fields);
1329 for (f = 0; f < vidpad->vprp.fields; ++f) {
1330 gst_riff_vprp_video_field_desc *fd;
1332 fd = &(vidpad->vprp.field_info[f]);
1333 GST_WRITE_UINT32_LE (buffdata + 0, fd->compressed_bm_height);
1334 GST_WRITE_UINT32_LE (buffdata + 4, fd->compressed_bm_width);
1335 GST_WRITE_UINT32_LE (buffdata + 8, fd->valid_bm_height);
1336 GST_WRITE_UINT32_LE (buffdata + 12, fd->valid_bm_width);
1337 GST_WRITE_UINT32_LE (buffdata + 16, fd->valid_bm_x_offset);
1338 GST_WRITE_UINT32_LE (buffdata + 20, fd->valid_bm_y_offset);
1339 GST_WRITE_UINT32_LE (buffdata + 24, fd->video_x_t_offset);
1340 GST_WRITE_UINT32_LE (buffdata + 28, fd->video_y_start);
1346 /* the audio header */
1347 memcpy (buffdata + 0, "strf", 4);
1348 GST_WRITE_UINT32_LE (buffdata + 4,
1349 sizeof (gst_riff_strf_auds) + 2 + codec_size);
1350 /* the actual header */
1351 GST_WRITE_UINT16_LE (buffdata + 8, audpad->auds.format);
1352 GST_WRITE_UINT16_LE (buffdata + 10, audpad->auds.channels);
1353 GST_WRITE_UINT32_LE (buffdata + 12, audpad->auds.rate);
1354 GST_WRITE_UINT32_LE (buffdata + 16, audpad->auds.av_bps);
1355 GST_WRITE_UINT16_LE (buffdata + 20, audpad->auds.blockalign);
1356 GST_WRITE_UINT16_LE (buffdata + 22, audpad->auds.size);
1357 GST_WRITE_UINT16_LE (buffdata + 24, codec_size);
1361 /* include codec data, if any */
1363 memcpy (buffdata, GST_BUFFER_DATA (audpad->auds_codec_data),
1366 buffdata += codec_size;
1367 highmark += codec_size;
1370 if (highmark & 0x1) {
1376 /* odml superindex chunk */
1377 if (avipad->idx_index > 0)
1378 memcpy (buffdata, "indx", 4);
1380 memcpy (buffdata, "JUNK", 4);
1381 GST_WRITE_UINT32_LE (buffdata + 4, ODML_SUPERINDEX_SIZE - 8); /* chunk size */
1382 GST_WRITE_UINT16_LE (buffdata + 8, 4); /* bytes per entry */
1383 buffdata[10] = 0; /* index subtype */
1384 buffdata[11] = GST_AVI_INDEX_OF_INDEXES; /* index type */
1385 GST_WRITE_UINT32_LE (buffdata + 12, avipad->idx_index); /* entries in use */
1386 memcpy (buffdata + 16, avipad->tag, 4); /* stream id */
1387 GST_WRITE_UINT32_LE (buffdata + 20, 0); /* reserved */
1388 GST_WRITE_UINT32_LE (buffdata + 24, 0); /* reserved */
1389 GST_WRITE_UINT32_LE (buffdata + 28, 0); /* reserved */
1390 memcpy (buffdata + 32, avipad->idx,
1391 GST_AVI_SUPERINDEX_COUNT * sizeof (gst_avi_superindex_entry));
1392 buffdata += ODML_SUPERINDEX_SIZE;
1393 highmark += ODML_SUPERINDEX_SIZE;
1398 if (avimux->video_pads > 0) {
1400 memcpy (buffdata + 0, "LIST", 4);
1401 GST_WRITE_UINT32_LE (buffdata + 4, sizeof (guint32) + 4 * 3);
1402 memcpy (buffdata + 8, "odml", 4);
1403 memcpy (buffdata + 12, "dmlh", 4);
1404 GST_WRITE_UINT32_LE (buffdata + 16, sizeof (guint32));
1405 GST_WRITE_UINT32_LE (buffdata + 20, avimux->total_frames);
1410 GST_WRITE_UINT32_LE (hdrl_size, (guint32) (buffdata - hdrl_size) - 4);
1416 GstMarkedBuffer data = { &highmark, buffer };
1418 memcpy (buffdata + 0, "LIST", 4);
1419 ptr = buffdata + 4; /* fill in later */
1420 startsize = highmark + 4;
1421 memcpy (buffdata + 8, "INFO", 4);
1425 /* 12 bytes is needed for data header */
1426 GST_BUFFER_SIZE (buffer) -= 12;
1427 gst_tag_list_foreach (tags, gst_avi_mux_write_tag, &data);
1428 GST_BUFFER_SIZE (buffer) += 12;
1429 buffdata = GST_BUFFER_DATA (buffer) + highmark;
1431 if (highmark - startsize - 4 == 4) {
1432 /* no tags writen, remove the empty INFO LIST as it is useless
1433 * and prevents playback in vlc */
1435 buffdata = GST_BUFFER_DATA (buffer) + highmark;
1436 /* no need to erase the writen data, it will be overwriten anyway */
1438 /* update list size */
1439 GST_WRITE_UINT32_LE (ptr, highmark - startsize - 4);
1443 /* avi data header */
1444 memcpy (buffdata + 0, "LIST", 4);
1445 GST_WRITE_UINT32_LE (buffdata + 4, avimux->data_size);
1446 memcpy (buffdata + 8, "movi", 4);
1450 /* finally we can fill in the RIFF size */
1451 /* note that riff only counts the first avi chunk */
1452 GST_WRITE_UINT32_LE (riff_size, (guint32) (buffdata - riff_size - 4) /* header and movi tags */
1453 +avimux->idx_size + avimux->data_size - 4); /* movi data and index */
1455 { /* only the part that is filled in actually makes up the header
1456 * unref the parent as we only need this part from now on */
1457 GstBuffer *subbuffer = gst_buffer_create_sub (buffer, 0, highmark);
1459 gst_buffer_unref (buffer);
1465 gst_avi_mux_riff_get_avix_header (guint32 datax_size)
1470 buffer = gst_buffer_new_and_alloc (24);
1471 buffdata = GST_BUFFER_DATA (buffer);
1473 memcpy (buffdata + 0, "RIFF", 4);
1474 GST_WRITE_UINT32_LE (buffdata + 4, datax_size + 3 * 4);
1475 memcpy (buffdata + 8, "AVIX", 4);
1476 memcpy (buffdata + 12, "LIST", 4);
1477 GST_WRITE_UINT32_LE (buffdata + 16, datax_size);
1478 memcpy (buffdata + 20, "movi", 4);
1483 static inline GstBuffer *
1484 gst_avi_mux_riff_get_header (GstAviPad * avipad, guint32 video_frame_size)
1489 buffer = gst_buffer_new_and_alloc (8);
1490 buffdata = GST_BUFFER_DATA (buffer);
1491 memcpy (buffdata + 0, avipad->tag, 4);
1492 GST_WRITE_UINT32_LE (buffdata + 4, video_frame_size);
1497 /* write an odml index chunk in the movi list */
1498 static GstFlowReturn
1499 gst_avi_mux_write_avix_index (GstAviMux * avimux, GstAviPad * avipad,
1500 gchar * code, gchar * chunk, gst_avi_superindex_entry * super_index,
1501 gint * super_index_count)
1505 guint8 *buffdata, *data;
1506 gst_riff_index_entry *entry;
1508 guint32 size, entry_count;
1509 gboolean is_pcm = FALSE;
1510 guint32 pcm_samples = 0;
1512 /* check if it is pcm */
1513 if (avipad && !avipad->is_video) {
1514 GstAviAudioPad *audiopad = (GstAviAudioPad *) avipad;
1515 if (audiopad->auds.format == GST_RIFF_WAVE_FORMAT_PCM) {
1516 pcm_samples = audiopad->samples;
1521 /* allocate the maximum possible */
1522 buffer = gst_buffer_new_and_alloc (32 + 8 * avimux->idx_index);
1523 buffdata = GST_BUFFER_DATA (buffer);
1525 /* general index chunk info */
1526 memcpy (buffdata + 0, chunk, 4); /* chunk id */
1527 GST_WRITE_UINT32_LE (buffdata + 4, 0); /* chunk size; fill later */
1528 GST_WRITE_UINT16_LE (buffdata + 8, 2); /* index entry is 2 words */
1529 buffdata[10] = 0; /* index subtype */
1530 buffdata[11] = GST_AVI_INDEX_OF_CHUNKS; /* index type: AVI_INDEX_OF_CHUNKS */
1531 GST_WRITE_UINT32_LE (buffdata + 12, 0); /* entries in use; fill later */
1532 memcpy (buffdata + 16, code, 4); /* stream to which index refers */
1533 GST_WRITE_UINT64_LE (buffdata + 20, avimux->avix_start); /* base offset */
1534 GST_WRITE_UINT32_LE (buffdata + 28, 0); /* reserved */
1537 /* now the actual index entries */
1538 i = avimux->idx_index;
1539 entry = avimux->idx;
1541 if (memcmp (&entry->id, code, 4) == 0) {
1542 /* enter relative offset to the data (!) */
1543 GST_WRITE_UINT32_LE (buffdata, GUINT32_FROM_LE (entry->offset) + 8);
1544 /* msb is set if not (!) keyframe */
1545 GST_WRITE_UINT32_LE (buffdata + 4, GUINT32_FROM_LE (entry->size)
1546 | (GUINT32_FROM_LE (entry->flags)
1547 & GST_RIFF_IF_KEYFRAME ? 0 : 1U << 31));
1554 /* ok, now we know the size and no of entries, fill in where needed */
1555 data = GST_BUFFER_DATA (buffer);
1556 GST_BUFFER_SIZE (buffer) = size = buffdata - data;
1557 GST_WRITE_UINT32_LE (data + 4, size - 8);
1558 entry_count = (size - 32) / 8;
1559 GST_WRITE_UINT32_LE (data + 12, entry_count);
1561 /* decorate and send */
1562 gst_buffer_set_caps (buffer, GST_PAD_CAPS (avimux->srcpad));
1563 if ((res = gst_pad_push (avimux->srcpad, buffer)) != GST_FLOW_OK)
1566 /* keep track of this in superindex (if room) ... */
1567 if (*super_index_count < GST_AVI_SUPERINDEX_COUNT) {
1568 i = *super_index_count;
1569 super_index[i].offset = GUINT64_TO_LE (avimux->total_data);
1570 super_index[i].size = GUINT32_TO_LE (size);
1572 super_index[i].duration = GUINT32_TO_LE (pcm_samples);
1574 super_index[i].duration = GUINT32_TO_LE (entry_count);
1576 (*super_index_count)++;
1578 GST_WARNING_OBJECT (avimux, "No more room in superindex of stream %s",
1581 /* ... and in size */
1582 avimux->total_data += size;
1583 if (avimux->is_bigfile)
1584 avimux->datax_size += size;
1586 avimux->data_size += size;
1591 /* some other usable functions (thankyou xawtv ;-) ) */
1594 gst_avi_mux_add_index (GstAviMux * avimux, GstAviPad * avipad, guint32 flags,
1597 gchar *code = avipad->tag;
1598 if (avimux->idx_index == avimux->idx_count) {
1599 avimux->idx_count += 256;
1601 g_realloc (avimux->idx,
1602 avimux->idx_count * sizeof (gst_riff_index_entry));
1605 /* in case of pcm audio, we need to count the number of samples for
1606 * putting in the indx entries */
1607 if (!avipad->is_video) {
1608 GstAviAudioPad *audiopad = (GstAviAudioPad *) avipad;
1609 if (audiopad->auds.format == GST_RIFF_WAVE_FORMAT_PCM) {
1610 audiopad->samples += size / audiopad->auds.blockalign;
1614 memcpy (&(avimux->idx[avimux->idx_index].id), code, 4);
1615 avimux->idx[avimux->idx_index].flags = GUINT32_TO_LE (flags);
1616 avimux->idx[avimux->idx_index].offset = GUINT32_TO_LE (avimux->idx_offset);
1617 avimux->idx[avimux->idx_index].size = GUINT32_TO_LE (size);
1618 avimux->idx_index++;
1621 static GstFlowReturn
1622 gst_avi_mux_write_index (GstAviMux * avimux)
1628 buffer = gst_buffer_new_and_alloc (8);
1629 buffdata = GST_BUFFER_DATA (buffer);
1630 memcpy (buffdata + 0, "idx1", 4);
1631 GST_WRITE_UINT32_LE (buffdata + 4,
1632 avimux->idx_index * sizeof (gst_riff_index_entry));
1634 gst_buffer_set_caps (buffer, GST_PAD_CAPS (avimux->srcpad));
1635 res = gst_pad_push (avimux->srcpad, buffer);
1636 if (res != GST_FLOW_OK)
1639 buffer = gst_buffer_new ();
1640 GST_BUFFER_SIZE (buffer) = avimux->idx_index * sizeof (gst_riff_index_entry);
1641 GST_BUFFER_DATA (buffer) = (guint8 *) avimux->idx;
1642 GST_BUFFER_MALLOCDATA (buffer) = GST_BUFFER_DATA (buffer);
1643 avimux->idx = NULL; /* will be free()'ed by gst_buffer_unref() */
1644 avimux->total_data += GST_BUFFER_SIZE (buffer) + 8;
1646 gst_buffer_set_caps (buffer, GST_PAD_CAPS (avimux->srcpad));
1647 res = gst_pad_push (avimux->srcpad, buffer);
1648 if (res != GST_FLOW_OK)
1651 avimux->idx_size += avimux->idx_index * sizeof (gst_riff_index_entry) + 8;
1654 avimux->avi_hdr.flags |= GST_RIFF_AVIH_HASINDEX;
1658 static GstFlowReturn
1659 gst_avi_mux_bigfile (GstAviMux * avimux, gboolean last)
1661 GstFlowReturn res = GST_FLOW_OK;
1666 /* first some odml standard index chunks in the movi list */
1667 node = avimux->sinkpads;
1669 GstAviPad *avipad = (GstAviPad *) node->data;
1673 res = gst_avi_mux_write_avix_index (avimux, avipad, avipad->tag,
1674 avipad->idx_tag, avipad->idx, &avipad->idx_index);
1675 if (res != GST_FLOW_OK)
1679 if (avimux->is_bigfile) {
1681 event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
1682 avimux->avix_start, GST_CLOCK_TIME_NONE, avimux->avix_start);
1683 /* if the event succeeds */
1684 gst_pad_push_event (avimux->srcpad, event);
1686 /* rewrite AVIX header */
1687 header = gst_avi_mux_riff_get_avix_header (avimux->datax_size);
1688 gst_buffer_set_caps (header, GST_PAD_CAPS (avimux->srcpad));
1689 res = gst_pad_push (avimux->srcpad, header);
1691 /* go back to current location, at least try */
1692 event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
1693 avimux->total_data, GST_CLOCK_TIME_NONE, avimux->total_data);
1694 gst_pad_push_event (avimux->srcpad, event);
1696 if (res != GST_FLOW_OK)
1698 } else { /* write a standard index in the first riff chunk */
1699 res = gst_avi_mux_write_index (avimux);
1700 /* the index data/buffer is freed by pushing it */
1701 avimux->idx_count = 0;
1702 if (res != GST_FLOW_OK)
1706 avimux->avix_start = avimux->total_data;
1711 avimux->is_bigfile = TRUE;
1712 avimux->numx_frames = 0;
1713 avimux->datax_size = 4; /* movi tag */
1714 avimux->idx_index = 0;
1715 node = avimux->sinkpads;
1717 GstAviPad *avipad = (GstAviPad *) node->data;
1719 if (!avipad->is_video) {
1720 GstAviAudioPad *audiopad = (GstAviAudioPad *) avipad;
1721 audiopad->samples = 0;
1725 header = gst_avi_mux_riff_get_avix_header (0);
1726 avimux->total_data += GST_BUFFER_SIZE (header);
1727 /* avix_start is used as base offset for the odml index chunk */
1728 avimux->idx_offset = avimux->total_data - avimux->avix_start;
1729 gst_buffer_set_caps (header, GST_PAD_CAPS (avimux->srcpad));
1730 return gst_pad_push (avimux->srcpad, header);
1733 /* enough header blabla now, let's go on to actually writing the headers */
1735 static GstFlowReturn
1736 gst_avi_mux_start_file (GstAviMux * avimux)
1742 avimux->total_data = 0;
1743 avimux->total_frames = 0;
1744 avimux->data_size = 4; /* movi tag */
1745 avimux->datax_size = 0;
1746 avimux->num_frames = 0;
1747 avimux->numx_frames = 0;
1748 avimux->avix_start = 0;
1750 avimux->idx_index = 0;
1751 avimux->idx_offset = 0; /* see 10 lines below */
1752 avimux->idx_size = 0;
1753 avimux->idx_count = 0;
1757 avimux->write_header = FALSE;
1758 avimux->restart = FALSE;
1760 /* init streams, see what we've got */
1761 node = avimux->sinkpads;
1762 avimux->audio_pads = avimux->video_pads = 0;
1764 GstAviPad *avipad = (GstAviPad *) node->data;
1768 if (!avipad->is_video) {
1769 /* audio stream numbers must start at 1 iff there is a video stream 0;
1770 * request_pad inserts video pad at head of list, so this test suffices */
1771 if (avimux->video_pads)
1772 avimux->audio_pads++;
1773 avipad->tag = g_strdup_printf ("%02uwb", avimux->audio_pads);
1774 avipad->idx_tag = g_strdup_printf ("ix%02u", avimux->audio_pads);
1775 if (!avimux->video_pads)
1776 avimux->audio_pads++;
1778 avipad->tag = g_strdup_printf ("%02udb", avimux->video_pads);
1779 avipad->idx_tag = g_strdup_printf ("ix%02u", avimux->video_pads++);
1783 /* let downstream know we think in BYTES and expect to do seeking later on */
1784 gst_pad_push_event (avimux->srcpad,
1785 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES, 0, -1, 0));
1788 avimux->avi_hdr.streams = g_slist_length (avimux->sinkpads);
1789 avimux->is_bigfile = FALSE;
1791 header = gst_avi_mux_riff_get_avi_header (avimux);
1792 avimux->total_data += GST_BUFFER_SIZE (header);
1794 gst_buffer_set_caps (header, GST_PAD_CAPS (avimux->srcpad));
1795 res = gst_pad_push (avimux->srcpad, header);
1797 avimux->idx_offset = avimux->total_data;
1802 static GstFlowReturn
1803 gst_avi_mux_stop_file (GstAviMux * avimux)
1805 GstFlowReturn res = GST_FLOW_OK;
1810 /* if bigfile, rewrite header, else write indexes */
1811 /* don't bail out at once if error, still try to re-write header */
1812 if (avimux->video_pads > 0) {
1813 if (avimux->is_bigfile) {
1814 res = gst_avi_mux_bigfile (avimux, TRUE);
1816 res = gst_avi_mux_write_index (avimux);
1820 /* we do our best to make it interleaved at least ... */
1821 if (avimux->audio_pads > 0 && avimux->video_pads > 0)
1822 avimux->avi_hdr.flags |= GST_RIFF_AVIH_ISINTERLEAVED;
1824 /* set rate and everything having to do with that */
1825 avimux->avi_hdr.max_bps = 0;
1826 node = avimux->sinkpads;
1828 GstAviPad *avipad = (GstAviPad *) node->data;
1832 if (!avipad->is_video) {
1833 GstAviAudioPad *audpad = (GstAviAudioPad *) avipad;
1835 /* calculate bps if needed */
1836 if (!audpad->auds.av_bps) {
1837 if (audpad->audio_time) {
1838 audpad->auds.av_bps =
1839 (GST_SECOND * audpad->audio_size) / audpad->audio_time;
1840 /* round bps to nearest multiple of 8;
1841 * which is much more likely to be the (cbr) bitrate in use;
1842 * which in turn results in better timestamp calculation on playback */
1843 audpad->auds.av_bps = GST_ROUND_UP_8 (audpad->auds.av_bps - 4);
1845 GST_ELEMENT_WARNING (avimux, STREAM, MUX,
1846 (_("No or invalid input audio, AVI stream will be corrupt.")),
1848 audpad->auds.av_bps = 0;
1851 gst_avi_mux_audsink_set_fields (avimux, audpad);
1852 avimux->avi_hdr.max_bps += audpad->auds.av_bps;
1853 avipad->hdr.length = gst_util_uint64_scale (audpad->audio_time,
1854 avipad->hdr.rate, avipad->hdr.scale * GST_SECOND);
1856 GstAviVideoPad *vidpad = (GstAviVideoPad *) avipad;
1858 avimux->avi_hdr.max_bps += ((vidpad->vids.bit_cnt + 7) / 8) *
1859 (1000000. / avimux->avi_hdr.us_frame) * vidpad->vids.image_size;
1860 avipad->hdr.length = avimux->total_frames;
1864 /* statistics/total_frames/... */
1865 avimux->avi_hdr.tot_frames = avimux->num_frames;
1867 /* seek and rewrite the header */
1868 header = gst_avi_mux_riff_get_avi_header (avimux);
1869 event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
1870 0, GST_CLOCK_TIME_NONE, 0);
1871 gst_pad_push_event (avimux->srcpad, event);
1873 gst_buffer_set_caps (header, GST_PAD_CAPS (avimux->srcpad));
1874 /* the first error survives */
1875 if (res == GST_FLOW_OK)
1876 res = gst_pad_push (avimux->srcpad, header);
1878 gst_pad_push (avimux->srcpad, header);
1880 event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
1881 avimux->total_data, GST_CLOCK_TIME_NONE, avimux->total_data);
1882 gst_pad_push_event (avimux->srcpad, event);
1884 avimux->write_header = TRUE;
1889 static GstFlowReturn
1890 gst_avi_mux_restart_file (GstAviMux * avimux)
1894 if ((res = gst_avi_mux_stop_file (avimux)) != GST_FLOW_OK)
1897 gst_pad_push_event (avimux->srcpad, gst_event_new_eos ());
1899 return gst_avi_mux_start_file (avimux);
1902 /* handle events (search) */
1904 gst_avi_mux_handle_event (GstPad * pad, GstEvent * event)
1909 avimux = GST_AVI_MUX (gst_pad_get_parent (pad));
1911 switch (GST_EVENT_TYPE (event)) {
1912 case GST_EVENT_TAG:{
1914 GstTagSetter *setter = GST_TAG_SETTER (avimux);
1915 const GstTagMergeMode mode = gst_tag_setter_get_tag_merge_mode (setter);
1917 gst_event_parse_tag (event, &list);
1918 gst_tag_setter_merge_tags (setter, list, mode);
1925 /* now GstCollectPads can take care of the rest, e.g. EOS */
1926 ret = avimux->collect_event (pad, event);
1928 gst_object_unref (avimux);
1933 /* send extra 'padding' data */
1934 static GstFlowReturn
1935 gst_avi_mux_send_pad_data (GstAviMux * avimux, gulong num_bytes)
1939 buffer = gst_buffer_new_and_alloc (num_bytes);
1940 memset (GST_BUFFER_DATA (buffer), 0, num_bytes);
1941 gst_buffer_set_caps (buffer, GST_PAD_CAPS (avimux->srcpad));
1942 return gst_pad_push (avimux->srcpad, buffer);
1946 static GstFlowReturn
1947 gst_avi_mux_do_buffer (GstAviMux * avimux, GstAviPad * avipad)
1950 GstBuffer *data, *header;
1951 gulong total_size, pad_bytes = 0;
1954 data = gst_collect_pads_pop (avimux->collect, avipad->collect);
1956 /* Prepend a special buffer to the first one for some formats */
1957 if (avipad->is_video) {
1958 GstAviVideoPad *vidpad = (GstAviVideoPad *) avipad;
1960 if (vidpad->prepend_buffer) {
1961 GstBuffer *newdata = gst_buffer_merge (vidpad->prepend_buffer, data);
1962 gst_buffer_copy_metadata (newdata, data, GST_BUFFER_COPY_TIMESTAMPS);
1963 gst_buffer_unref (data);
1964 gst_buffer_unref (vidpad->prepend_buffer);
1967 vidpad->prepend_buffer = NULL;
1971 if (avimux->restart) {
1972 if ((res = gst_avi_mux_restart_file (avimux)) != GST_FLOW_OK)
1976 /* need to restart or start a next avix chunk ? */
1977 if ((avimux->is_bigfile ? avimux->datax_size : avimux->data_size) +
1978 GST_BUFFER_SIZE (data) > GST_AVI_MAX_SIZE) {
1979 if (avimux->enable_large_avi) {
1980 if ((res = gst_avi_mux_bigfile (avimux, FALSE)) != GST_FLOW_OK)
1983 if ((res = gst_avi_mux_restart_file (avimux)) != GST_FLOW_OK)
1988 /* get header and record some stats */
1989 if (GST_BUFFER_SIZE (data) & 1) {
1990 pad_bytes = 2 - (GST_BUFFER_SIZE (data) & 1);
1992 header = gst_avi_mux_riff_get_header (avipad, GST_BUFFER_SIZE (data));
1993 total_size = GST_BUFFER_SIZE (header) + GST_BUFFER_SIZE (data) + pad_bytes;
1995 if (avimux->is_bigfile) {
1996 avimux->datax_size += total_size;
1998 avimux->data_size += total_size;
2001 if (G_UNLIKELY (avipad->hook))
2002 avipad->hook (avimux, avipad, data);
2004 /* the suggested buffer size is the max frame size */
2005 if (avipad->hdr.bufsize < GST_BUFFER_SIZE (data))
2006 avipad->hdr.bufsize = GST_BUFFER_SIZE (data);
2008 if (avipad->is_video) {
2009 avimux->total_frames++;
2011 if (avimux->is_bigfile) {
2012 avimux->numx_frames++;
2014 avimux->num_frames++;
2018 if (!GST_BUFFER_FLAG_IS_SET (data, GST_BUFFER_FLAG_DELTA_UNIT))
2021 GstAviAudioPad *audpad = (GstAviAudioPad *) avipad;
2024 audpad->audio_size += GST_BUFFER_SIZE (data);
2025 audpad->audio_time += GST_BUFFER_DURATION (data);
2028 gst_avi_mux_add_index (avimux, avipad, flags, GST_BUFFER_SIZE (data));
2030 /* prepare buffers for sending */
2031 gst_buffer_set_caps (header, GST_PAD_CAPS (avimux->srcpad));
2032 data = gst_buffer_make_metadata_writable (data);
2033 gst_buffer_set_caps (data, GST_PAD_CAPS (avimux->srcpad));
2035 GST_LOG_OBJECT (avimux, "pushing buffers: head, data");
2037 if ((res = gst_pad_push (avimux->srcpad, header)) != GST_FLOW_OK)
2039 if ((res = gst_pad_push (avimux->srcpad, data)) != GST_FLOW_OK)
2043 if ((res = gst_avi_mux_send_pad_data (avimux, pad_bytes)) != GST_FLOW_OK)
2047 /* if any push above fails, we're in trouble with file consistency anyway */
2048 avimux->total_data += total_size;
2049 avimux->idx_offset += total_size;
2054 /* pick the oldest buffer from the pads and push it */
2055 static GstFlowReturn
2056 gst_avi_mux_do_one_buffer (GstAviMux * avimux)
2058 GstAviPad *avipad, *best_pad;
2061 GstClockTime time, best_time, delay;
2063 node = avimux->sinkpads;
2065 best_time = GST_CLOCK_TIME_NONE;
2066 for (; node; node = node->next) {
2067 avipad = (GstAviPad *) node->data;
2069 if (!avipad->collect)
2072 buffer = gst_collect_pads_peek (avimux->collect, avipad->collect);
2075 time = GST_BUFFER_TIMESTAMP (buffer);
2076 gst_buffer_unref (buffer);
2078 delay = avipad->is_video ? GST_SECOND / 2 : 0;
2080 /* invalid timestamp buffers pass first,
2081 * these are probably initialization buffers */
2082 if (best_pad == NULL || !GST_CLOCK_TIME_IS_VALID (time)
2083 || (GST_CLOCK_TIME_IS_VALID (best_time) && time + delay < best_time)) {
2085 best_time = time + delay;
2090 GST_LOG_OBJECT (avimux, "selected pad %s with time %" GST_TIME_FORMAT,
2091 GST_PAD_NAME (best_pad->collect->pad), GST_TIME_ARGS (best_time));
2093 return gst_avi_mux_do_buffer (avimux, best_pad);
2095 /* simply finish off the file and send EOS */
2096 gst_avi_mux_stop_file (avimux);
2097 gst_pad_push_event (avimux->srcpad, gst_event_new_eos ());
2098 return GST_FLOW_UNEXPECTED;
2102 static GstFlowReturn
2103 gst_avi_mux_collect_pads (GstCollectPads * pads, GstAviMux * avimux)
2107 if (G_UNLIKELY (avimux->write_header)) {
2108 if ((res = gst_avi_mux_start_file (avimux)) != GST_FLOW_OK)
2112 return gst_avi_mux_do_one_buffer (avimux);
2117 gst_avi_mux_get_property (GObject * object,
2118 guint prop_id, GValue * value, GParamSpec * pspec)
2122 avimux = GST_AVI_MUX (object);
2126 g_value_set_boolean (value, avimux->enable_large_avi);
2129 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2135 gst_avi_mux_set_property (GObject * object,
2136 guint prop_id, const GValue * value, GParamSpec * pspec)
2140 avimux = GST_AVI_MUX (object);
2144 avimux->enable_large_avi = g_value_get_boolean (value);
2147 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2152 static GstStateChangeReturn
2153 gst_avi_mux_change_state (GstElement * element, GstStateChange transition)
2156 GstStateChangeReturn ret;
2158 avimux = GST_AVI_MUX (element);
2160 switch (transition) {
2161 case GST_STATE_CHANGE_READY_TO_PAUSED:
2162 gst_collect_pads_start (avimux->collect);
2164 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2166 case GST_STATE_CHANGE_PAUSED_TO_READY:
2167 gst_collect_pads_stop (avimux->collect);
2173 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2174 if (ret == GST_STATE_CHANGE_FAILURE)
2177 switch (transition) {
2178 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2180 case GST_STATE_CHANGE_PAUSED_TO_READY:
2181 gst_avi_mux_reset (avimux);
2183 case GST_STATE_CHANGE_READY_TO_NULL: