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,format=(string)I420,width=320,height=240,framerate=(fraction)25/1' \
40 * audiotestsrc num-buffers=440 ! audioconvert \
41 * ! 'audio/x-raw,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,format=(string)I420,width=320,height=240,framerate=(fraction)25/1' \
49 * ! xvidenc ! queue ! mux. \
50 * audiotestsrc num-buffers=440 ! audioconvert ! 'audio/x-raw,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"
68 #include <gst/video/video.h>
69 #include <gst/audio/audio.h>
70 #include <gst/base/gstbytewriter.h>
72 #include "gstavimux.h"
74 GST_DEBUG_CATEGORY_STATIC (avimux_debug);
75 #define GST_CAT_DEFAULT avimux_debug
83 #define DEFAULT_BIGFILE TRUE
85 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
88 GST_STATIC_CAPS ("video/x-msvideo")
91 static GstStaticPadTemplate video_sink_factory =
92 GST_STATIC_PAD_TEMPLATE ("video_%u",
95 GST_STATIC_CAPS ("video/x-raw, "
96 "format = (string) { YUY2, I420 }, "
97 "width = (int) [ 16, 4096 ], "
98 "height = (int) [ 16, 4096 ], "
99 "framerate = (fraction) [ 0, MAX ]; "
101 "width = (int) [ 16, 4096 ], "
102 "height = (int) [ 16, 4096 ], "
103 "framerate = (fraction) [ 0, MAX ]; "
105 "width = (int) [ 16, 4096 ], "
106 "height = (int) [ 16, 4096 ], "
107 "framerate = (fraction) [ 0, MAX ], "
108 "divxversion = (int) [ 3, 5 ]; "
110 "width = (int) [ 16, 4096 ], "
111 "height = (int) [ 16, 4096 ], "
112 "framerate = (fraction) [ 0, MAX ]; "
114 "width = (int) [ 16, 4096 ], "
115 "height = (int) [ 16, 4096 ], "
116 "framerate = (fraction) [ 0, MAX ]; "
118 "width = (int) [ 16, 4096 ], "
119 "height = (int) [ 16, 4096 ], "
120 "framerate = (fraction) [ 0, MAX ], "
121 "msmpegversion = (int) [ 41, 43 ]; "
123 "width = (int) [ 16, 4096 ], "
124 "height = (int) [ 16, 4096 ], "
125 "framerate = (fraction) [ 0, MAX ], "
126 "mpegversion = (int) { 1, 2, 4}, "
127 "systemstream = (boolean) FALSE; "
129 "width = (int) [ 16, 4096 ], "
130 "height = (int) [ 16, 4096 ], "
131 "framerate = (fraction) [ 0, MAX ]; "
133 "stream-format = (string) byte-stream, "
134 "alignment = (string) au, "
135 "width = (int) [ 16, 4096 ], "
136 "height = (int) [ 16, 4096 ], "
137 "framerate = (fraction) [ 0, MAX ]; "
139 "width = (int) 720, "
140 "height = (int) { 576, 480 }, "
141 "framerate = (fraction) [ 0, MAX ], "
142 "systemstream = (boolean) FALSE; "
144 "width = (int) [ 16, 4096 ], "
145 "height = (int) [ 16, 4096 ], " "framerate = (fraction) [ 0, MAX ];"
147 "width = (int) [ 16, 4096 ], "
148 "height = (int) [ 16, 4096 ], " "framerate = (fraction) [ 0, MAX ], "
149 "wmvversion = (int) [ 1, 3];"
151 "width = (int) [ 1, 2147483647 ], "
152 "height = (int) [ 1, 2147483647 ], "
153 "framerate = (fraction) [ 0, MAX ];"
155 "width = (int) [ 1, 2147483647 ], "
156 "height = (int) [ 1, 2147483647 ], "
157 "framerate = (fraction) [ 0, MAX ]")
160 static GstStaticPadTemplate audio_sink_factory =
161 GST_STATIC_PAD_TEMPLATE ("audio_%u",
164 GST_STATIC_CAPS ("audio/x-raw, "
165 "format = (string) { U8, S16LE }, "
166 "rate = (int) [ 1000, 96000 ], "
167 "channels = (int) [ 1, 2 ]; "
169 "mpegversion = (int) 1, "
170 "layer = (int) [ 1, 3 ], "
171 "rate = (int) [ 1000, 96000 ], " "channels = (int) [ 1, 2 ]; "
173 "mpegversion = (int) 4, "
174 "stream-format = (string) raw, "
175 "rate = (int) [ 1000, 96000 ], " "channels = (int) [ 1, 2 ]; "
176 /*#if 0 VC6 doesn't support #if here ...
178 "rate = (int) [ 1000, 96000 ], " "channels = (int) [ 1, 2 ]; "
181 "rate = (int) [ 1000, 96000 ], " "channels = (int) [ 1, 2 ]; "
183 "rate = (int) [ 1000, 48000 ], " "channels = (int) [ 1, 2 ]; "
185 "rate = (int) [ 1000, 48000 ], " "channels = (int) [ 1, 2 ]; "
187 "rate = (int) [ 1000, 96000 ], " "channels = (int) [ 1, 2 ], "
188 "wmaversion = (int) [ 1, 2 ] ")
191 static void gst_avi_mux_pad_reset (GstAviPad * avipad, gboolean free);
193 static GstFlowReturn gst_avi_mux_collect_pads (GstCollectPads2 * pads,
195 static gboolean gst_avi_mux_handle_event (GstCollectPads2 * pad,
196 GstCollectData2 * data, GstEvent * event, gpointer user_data);
197 static GstPad *gst_avi_mux_request_new_pad (GstElement * element,
198 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
199 static void gst_avi_mux_release_pad (GstElement * element, GstPad * pad);
200 static void gst_avi_mux_set_property (GObject * object,
201 guint prop_id, const GValue * value, GParamSpec * pspec);
202 static void gst_avi_mux_get_property (GObject * object,
203 guint prop_id, GValue * value, GParamSpec * pspec);
204 static GstStateChangeReturn gst_avi_mux_change_state (GstElement * element,
205 GstStateChange transition);
207 #define gst_avi_mux_parent_class parent_class
208 G_DEFINE_TYPE_WITH_CODE (GstAviMux, gst_avi_mux, GST_TYPE_ELEMENT,
209 G_IMPLEMENT_INTERFACE (GST_TYPE_TAG_SETTER, NULL));
212 gst_avi_mux_finalize (GObject * object)
214 GstAviMux *mux = GST_AVI_MUX (object);
217 /* completely free each sinkpad */
218 node = mux->sinkpads;
220 GstAviPad *avipad = (GstAviPad *) node->data;
224 gst_avi_mux_pad_reset (avipad, TRUE);
227 g_slist_free (mux->sinkpads);
228 mux->sinkpads = NULL;
233 gst_object_unref (mux->collect);
235 G_OBJECT_CLASS (parent_class)->finalize (object);
239 gst_avi_mux_class_init (GstAviMuxClass * klass)
241 GObjectClass *gobject_class;
242 GstElementClass *gstelement_class;
244 gobject_class = (GObjectClass *) klass;
245 gstelement_class = (GstElementClass *) klass;
247 GST_DEBUG_CATEGORY_INIT (avimux_debug, "avimux", 0, "Muxer for AVI streams");
249 gobject_class->get_property = gst_avi_mux_get_property;
250 gobject_class->set_property = gst_avi_mux_set_property;
251 gobject_class->finalize = gst_avi_mux_finalize;
253 g_object_class_install_property (gobject_class, ARG_BIGFILE,
254 g_param_spec_boolean ("bigfile", "Bigfile Support (>2GB)",
255 "Support for openDML-2.0 (big) AVI files", DEFAULT_BIGFILE,
256 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
258 gstelement_class->request_new_pad =
259 GST_DEBUG_FUNCPTR (gst_avi_mux_request_new_pad);
260 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_avi_mux_release_pad);
261 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_avi_mux_change_state);
263 gst_element_class_add_pad_template (gstelement_class,
264 gst_static_pad_template_get (&src_factory));
265 gst_element_class_add_pad_template (gstelement_class,
266 gst_static_pad_template_get (&audio_sink_factory));
267 gst_element_class_add_pad_template (gstelement_class,
268 gst_static_pad_template_get (&video_sink_factory));
270 gst_element_class_set_details_simple (gstelement_class, "Avi muxer",
272 "Muxes audio and video into an avi stream",
273 "GStreamer maintainers <gstreamer-devel@lists.sourceforge.net>");
276 /* reset pad to initial state
277 * free - if true, release all, not only stream related, data */
279 gst_avi_mux_pad_reset (GstAviPad * avipad, gboolean free)
282 memset (&(avipad->hdr), 0, sizeof (gst_riff_strh));
284 memset (&(avipad->idx[0]), 0, sizeof (avipad->idx));
287 g_free (avipad->tag);
289 g_free (avipad->idx_tag);
290 avipad->idx_tag = NULL;
293 if (avipad->is_video) {
294 GstAviVideoPad *vidpad = (GstAviVideoPad *) avipad;
296 avipad->hdr.type = GST_MAKE_FOURCC ('v', 'i', 'd', 's');
297 if (vidpad->vids_codec_data) {
298 gst_buffer_unref (vidpad->vids_codec_data);
299 vidpad->vids_codec_data = NULL;
302 if (vidpad->prepend_buffer) {
303 gst_buffer_unref (vidpad->prepend_buffer);
304 vidpad->prepend_buffer = NULL;
307 memset (&(vidpad->vids), 0, sizeof (gst_riff_strf_vids));
308 memset (&(vidpad->vprp), 0, sizeof (gst_riff_vprp));
310 GstAviAudioPad *audpad = (GstAviAudioPad *) avipad;
314 avipad->hdr.type = GST_MAKE_FOURCC ('a', 'u', 'd', 's');
315 if (audpad->auds_codec_data) {
316 gst_buffer_unref (audpad->auds_codec_data);
317 audpad->auds_codec_data = NULL;
320 memset (&(audpad->auds), 0, sizeof (gst_riff_strf_auds));
325 gst_avi_mux_reset (GstAviMux * avimux)
327 GSList *node, *newlist = NULL;
329 /* free and reset each sinkpad */
330 node = avimux->sinkpads;
332 GstAviPad *avipad = (GstAviPad *) node->data;
336 gst_avi_mux_pad_reset (avipad, FALSE);
337 /* if this pad has collectdata, keep it, otherwise dump it completely */
339 newlist = g_slist_append (newlist, avipad);
341 gst_avi_mux_pad_reset (avipad, TRUE);
346 /* free the old list of sinkpads, only keep the real collecting ones */
347 g_slist_free (avimux->sinkpads);
348 avimux->sinkpads = newlist;
351 avimux->num_frames = 0;
352 memset (&(avimux->avi_hdr), 0, sizeof (gst_riff_avih));
353 avimux->avi_hdr.max_bps = 10000000;
354 avimux->codec_data_size = 0;
356 if (avimux->tags_snap) {
357 gst_tag_list_free (avimux->tags_snap);
358 avimux->tags_snap = NULL;
361 g_free (avimux->idx);
365 avimux->write_header = TRUE;
368 gst_tag_setter_reset_tags (GST_TAG_SETTER (avimux));
372 gst_avi_mux_init (GstAviMux * avimux)
374 avimux->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
375 gst_pad_use_fixed_caps (avimux->srcpad);
376 gst_element_add_pad (GST_ELEMENT (avimux), avimux->srcpad);
379 avimux->enable_large_avi = DEFAULT_BIGFILE;
381 avimux->collect = gst_collect_pads2_new ();
382 gst_collect_pads2_set_function (avimux->collect,
383 (GstCollectPads2Function) (GST_DEBUG_FUNCPTR (gst_avi_mux_collect_pads)),
385 gst_collect_pads2_set_event_function (avimux->collect,
386 (GstCollectPads2EventFunction) (GST_DEBUG_FUNCPTR
387 (gst_avi_mux_handle_event)), avimux);
389 /* set to clean state */
390 gst_avi_mux_reset (avimux);
394 gst_avi_mux_vidsink_set_caps (GstPad * pad, GstCaps * vscaps)
397 GstAviVideoPad *avipad;
398 GstAviCollectData *collect_pad;
399 GstStructure *structure;
400 const gchar *mimetype;
401 const GValue *fps, *par;
402 const GValue *codec_data;
405 gboolean codec_data_in_headers = TRUE;
407 avimux = GST_AVI_MUX (gst_pad_get_parent (pad));
409 /* find stream data */
410 collect_pad = (GstAviCollectData *) gst_pad_get_element_private (pad);
411 g_assert (collect_pad);
412 avipad = (GstAviVideoPad *) collect_pad->avipad;
414 g_assert (avipad->parent.is_video);
415 g_assert (avipad->parent.hdr.type == GST_MAKE_FOURCC ('v', 'i', 'd', 's'));
417 GST_DEBUG_OBJECT (avimux, "%s:%s, caps=%" GST_PTR_FORMAT,
418 GST_DEBUG_PAD_NAME (pad), vscaps);
420 structure = gst_caps_get_structure (vscaps, 0);
421 mimetype = gst_structure_get_name (structure);
424 avipad->vids.size = sizeof (gst_riff_strf_vids);
425 avipad->vids.planes = 1;
426 if (!gst_structure_get_int (structure, "width", &width) ||
427 !gst_structure_get_int (structure, "height", &height)) {
431 avipad->vids.width = width;
432 avipad->vids.height = height;
434 fps = gst_structure_get_value (structure, "framerate");
435 if (fps == NULL || !GST_VALUE_HOLDS_FRACTION (fps))
438 avipad->parent.hdr.rate = gst_value_get_fraction_numerator (fps);
439 avipad->parent.hdr.scale = gst_value_get_fraction_denominator (fps);
441 /* (pixel) aspect ratio data, if any */
442 par = gst_structure_get_value (structure, "pixel-aspect-ratio");
443 /* only use video properties header if there is non-trivial aspect info */
444 if (par && GST_VALUE_HOLDS_FRACTION (par) &&
445 ((par_n = gst_value_get_fraction_numerator (par)) !=
446 (par_d = gst_value_get_fraction_denominator (par)))) {
447 GValue to_ratio = { 0, };
448 guint ratio_n, ratio_d;
450 /* some fraction voodoo to obtain simplest possible ratio */
451 g_value_init (&to_ratio, GST_TYPE_FRACTION);
452 gst_value_set_fraction (&to_ratio, width * par_n, height * par_d);
453 ratio_n = gst_value_get_fraction_numerator (&to_ratio);
454 ratio_d = gst_value_get_fraction_denominator (&to_ratio);
455 GST_DEBUG_OBJECT (avimux, "generating vprp data with aspect ratio %d/%d",
458 avipad->vprp.vert_rate = avipad->parent.hdr.rate / avipad->parent.hdr.scale;
459 avipad->vprp.hor_t_total = width;
460 avipad->vprp.vert_lines = height;
461 avipad->vprp.aspect = (ratio_n) << 16 | (ratio_d & 0xffff);
462 avipad->vprp.width = width;
463 avipad->vprp.height = height;
464 avipad->vprp.fields = 1;
465 avipad->vprp.field_info[0].compressed_bm_height = height;
466 avipad->vprp.field_info[0].compressed_bm_width = width;
467 avipad->vprp.field_info[0].valid_bm_height = height;
468 avipad->vprp.field_info[0].valid_bm_width = width;
471 if (!strcmp (mimetype, "video/x-raw")) {
475 format = gst_structure_get_string (structure, "format");
476 fmt = gst_video_format_from_string (format);
479 case GST_VIDEO_FORMAT_YUY2:
480 avipad->vids.compression = GST_MAKE_FOURCC ('Y', 'U', 'Y', '2');
481 avipad->vids.bit_cnt = 16;
483 case GST_VIDEO_FORMAT_I420:
484 avipad->vids.compression = GST_MAKE_FOURCC ('I', '4', '2', '0');
485 avipad->vids.bit_cnt = 12;
491 avipad->vids.bit_cnt = 24;
492 avipad->vids.compression = 0;
495 if (!strcmp (mimetype, "video/x-huffyuv")) {
496 avipad->vids.compression = GST_MAKE_FOURCC ('H', 'F', 'Y', 'U');
497 } else if (!strcmp (mimetype, "image/jpeg")) {
498 avipad->vids.compression = GST_MAKE_FOURCC ('M', 'J', 'P', 'G');
499 } else if (!strcmp (mimetype, "video/x-divx")) {
502 gst_structure_get_int (structure, "divxversion", &divxversion);
503 switch (divxversion) {
505 avipad->vids.compression = GST_MAKE_FOURCC ('D', 'I', 'V', '3');
508 avipad->vids.compression = GST_MAKE_FOURCC ('D', 'I', 'V', 'X');
511 avipad->vids.compression = GST_MAKE_FOURCC ('D', 'X', '5', '0');
514 } else if (!strcmp (mimetype, "video/x-xvid")) {
515 avipad->vids.compression = GST_MAKE_FOURCC ('X', 'V', 'I', 'D');
516 } else if (!strcmp (mimetype, "video/x-3ivx")) {
517 avipad->vids.compression = GST_MAKE_FOURCC ('3', 'I', 'V', '2');
518 } else if (gst_structure_has_name (structure, "video/x-msmpeg")) {
521 gst_structure_get_int (structure, "msmpegversion", &msmpegversion);
522 switch (msmpegversion) {
524 avipad->vids.compression = GST_MAKE_FOURCC ('M', 'P', 'G', '4');
527 avipad->vids.compression = GST_MAKE_FOURCC ('M', 'P', '4', '2');
530 avipad->vids.compression = GST_MAKE_FOURCC ('M', 'P', '4', '3');
533 GST_INFO ("unhandled msmpegversion : %d, fall back to fourcc=MPEG",
535 avipad->vids.compression = GST_MAKE_FOURCC ('M', 'P', 'E', 'G');
538 } else if (!strcmp (mimetype, "video/x-dv")) {
539 avipad->vids.compression = GST_MAKE_FOURCC ('D', 'V', 'S', 'D');
540 } else if (!strcmp (mimetype, "video/x-h263")) {
541 avipad->vids.compression = GST_MAKE_FOURCC ('H', '2', '6', '3');
542 } else if (!strcmp (mimetype, "video/x-h264")) {
543 avipad->vids.compression = GST_MAKE_FOURCC ('H', '2', '6', '4');
544 } else if (!strcmp (mimetype, "video/mpeg")) {
547 gst_structure_get_int (structure, "mpegversion", &mpegversion);
549 switch (mpegversion) {
551 avipad->vids.compression = GST_MAKE_FOURCC ('M', 'P', 'G', '2');
554 /* mplayer/ffmpeg might not work with DIVX, but with FMP4 */
555 avipad->vids.compression = GST_MAKE_FOURCC ('D', 'I', 'V', 'X');
557 /* DIVX/XVID in AVI store the codec_data chunk as part of the
558 first data buffer. So for this case, we prepend the codec_data
559 blob (if any) to that first buffer */
560 codec_data_in_headers = FALSE;
563 GST_INFO ("unhandled mpegversion : %d, fall back to fourcc=MPEG",
565 avipad->vids.compression = GST_MAKE_FOURCC ('M', 'P', 'E', 'G');
568 } else if (!strcmp (mimetype, "video/x-wmv")) {
571 if (gst_structure_get_int (structure, "wmvversion", &wmvversion)) {
572 switch (wmvversion) {
574 avipad->vids.compression = GST_MAKE_FOURCC ('W', 'M', 'V', '1');
577 avipad->vids.compression = GST_MAKE_FOURCC ('W', 'M', 'V', '2');
580 avipad->vids.compression = GST_MAKE_FOURCC ('W', 'M', 'V', '3');
585 } else if (!strcmp (mimetype, "image/x-jpc")) {
586 avipad->vids.compression = GST_MAKE_FOURCC ('M', 'J', '2', 'C');
587 } else if (!strcmp (mimetype, "video/x-vp8")) {
588 avipad->vids.compression = GST_MAKE_FOURCC ('V', 'P', '8', '0');
591 if (!avipad->vids.compression)
595 /* codec initialization data, if any */
596 codec_data = gst_structure_get_value (structure, "codec_data");
598 if (codec_data_in_headers) {
599 avipad->vids_codec_data = gst_value_get_buffer (codec_data);
600 gst_buffer_ref (avipad->vids_codec_data);
601 /* keep global track of size */
602 avimux->codec_data_size += gst_buffer_get_size (avipad->vids_codec_data);
604 avipad->prepend_buffer =
605 gst_buffer_ref (gst_value_get_buffer (codec_data));
609 avipad->parent.hdr.fcc_handler = avipad->vids.compression;
610 avipad->vids.image_size = avipad->vids.height * avipad->vids.width;
611 /* hm, maybe why avi only handles one stream well ... */
612 avimux->avi_hdr.width = avipad->vids.width;
613 avimux->avi_hdr.height = avipad->vids.height;
614 avimux->avi_hdr.us_frame = 1000000. * avipad->parent.hdr.scale /
615 avipad->parent.hdr.rate;
617 gst_object_unref (avimux);
622 GST_WARNING_OBJECT (avimux, "refused caps %" GST_PTR_FORMAT, vscaps);
623 gst_object_unref (avimux);
629 gst_avi_mux_audsink_scan_mpeg_audio (GstAviMux * avimux, GstAviPad * avipad,
639 gst_buffer_map (buffer, &map, GST_MAP_READ);
643 header = GST_READ_UINT32_BE (map.data);
645 if ((header & 0xffe00000) != 0xffe00000)
648 /* thanks go to mp3parse */
649 if (header & (1 << 20)) {
650 lsf = (header & (1 << 19)) ? 0 : 1;
657 version = 1 + lsf + mpg25;
658 layer = 4 - ((header >> 17) & 0x3);
660 /* see http://www.codeproject.com/audio/MPEGAudioInfo.asp */
665 else if (version == 1) {
668 /* MPEG-2 or "2.5" */
672 if (G_UNLIKELY (avipad->hdr.scale <= 1))
673 avipad->hdr.scale = spf;
674 else if (G_UNLIKELY (avipad->hdr.scale != spf)) {
675 GST_WARNING_OBJECT (avimux, "input mpeg audio has varying frame size");
679 gst_buffer_unmap (buffer, &map);
686 GST_WARNING_OBJECT (avimux, "input mpeg audio is not parsed");
691 GST_WARNING_OBJECT (avimux, "falling back to CBR muxing");
692 avipad->hdr.scale = 1;
693 /* no need to check further */
700 gst_avi_mux_audsink_set_fields (GstAviMux * avimux, GstAviAudioPad * avipad)
702 if (avipad->parent.hdr.scale > 1) {
703 /* vbr case: fixed duration per frame/chunk */
704 avipad->parent.hdr.rate = avipad->auds.rate;
705 avipad->parent.hdr.samplesize = 0;
706 /* FIXME ?? some rumours say this should be largest audio chunk size */
707 avipad->auds.blockalign = avipad->parent.hdr.scale;
709 /* by spec, hdr.rate is av_bps related, is calculated that way in stop_file,
710 * and reduces to sample rate in PCM like cases */
711 avipad->parent.hdr.rate = avipad->auds.av_bps / avipad->auds.blockalign;
712 avipad->parent.hdr.samplesize = avipad->auds.blockalign;
713 avipad->parent.hdr.scale = 1;
718 gst_avi_mux_audsink_set_caps (GstPad * pad, GstCaps * vscaps)
721 GstAviAudioPad *avipad;
722 GstAviCollectData *collect_pad;
723 GstStructure *structure;
724 const gchar *mimetype;
725 const GValue *codec_data;
728 avimux = GST_AVI_MUX (gst_pad_get_parent (pad));
730 /* find stream data */
731 collect_pad = (GstAviCollectData *) gst_pad_get_element_private (pad);
732 g_assert (collect_pad);
733 avipad = (GstAviAudioPad *) collect_pad->avipad;
735 g_assert (!avipad->parent.is_video);
736 g_assert (avipad->parent.hdr.type == GST_MAKE_FOURCC ('a', 'u', 'd', 's'));
738 GST_DEBUG_OBJECT (avimux, "%s:%s, caps=%" GST_PTR_FORMAT,
739 GST_DEBUG_PAD_NAME (pad), vscaps);
741 structure = gst_caps_get_structure (vscaps, 0);
742 mimetype = gst_structure_get_name (structure);
744 /* we want these for all */
745 if (!gst_structure_get_int (structure, "channels", &channels) ||
746 !gst_structure_get_int (structure, "rate", &rate)) {
750 avipad->auds.channels = channels;
751 avipad->auds.rate = rate;
753 /* codec initialization data, if any */
754 codec_data = gst_structure_get_value (structure, "codec_data");
756 avipad->auds_codec_data = gst_value_get_buffer (codec_data);
757 gst_buffer_ref (avipad->auds_codec_data);
758 /* keep global track of size */
759 avimux->codec_data_size += gst_buffer_get_size (avipad->auds_codec_data);
762 if (!strcmp (mimetype, "audio/x-raw")) {
766 format = gst_structure_get_string (structure, "format");
767 fmt = gst_audio_format_from_string (format);
770 case GST_AUDIO_FORMAT_U8:
771 avipad->auds.blockalign = 8;
772 avipad->auds.size = 8;
774 case GST_AUDIO_FORMAT_S16:
775 avipad->auds.blockalign = 16;
776 avipad->auds.size = 16;
782 avipad->auds.format = GST_RIFF_WAVE_FORMAT_PCM;
783 /* set some more info straight */
784 avipad->auds.blockalign /= 8;
785 avipad->auds.blockalign *= avipad->auds.channels;
786 avipad->auds.av_bps = avipad->auds.blockalign * avipad->auds.rate;
788 avipad->auds.format = 0;
789 /* set some defaults */
790 avipad->auds.blockalign = 1;
791 avipad->auds.av_bps = 0;
792 avipad->auds.size = 16;
794 if (!strcmp (mimetype, "audio/mpeg")) {
797 gst_structure_get_int (structure, "mpegversion", &mpegversion);
798 switch (mpegversion) {
801 gboolean parsed = FALSE;
803 gst_structure_get_int (structure, "layer", &layer);
804 gst_structure_get_boolean (structure, "parsed", &parsed);
807 avipad->auds.format = GST_RIFF_WAVE_FORMAT_MPEGL3;
811 avipad->auds.format = GST_RIFF_WAVE_FORMAT_MPEGL12;
815 /* treat as VBR, should also cover CBR case;
816 * setup hook to parse frame header and determine spf */
817 avipad->parent.hook = gst_avi_mux_audsink_scan_mpeg_audio;
819 GST_WARNING_OBJECT (avimux, "unparsed MPEG audio input (?), "
826 GstBuffer *codec_data_buf = avipad->auds_codec_data;
827 const gchar *stream_format;
831 stream_format = gst_structure_get_string (structure, "stream-format");
833 if (strcmp (stream_format, "raw") != 0) {
834 GST_WARNING_OBJECT (avimux, "AAC's stream format '%s' is not "
835 "supported, please use 'raw'", stream_format);
839 GST_WARNING_OBJECT (avimux, "AAC's stream-format not specified, "
843 /* vbr case needs some special handling */
844 if (!codec_data_buf || gst_buffer_get_size (codec_data_buf) < 2) {
845 GST_WARNING_OBJECT (avimux, "no (valid) codec_data for AAC audio");
848 avipad->auds.format = GST_RIFF_WAVE_FORMAT_AAC;
849 /* need to determine frame length */
850 gst_buffer_extract (codec_data_buf, 0, data, 2);
851 codec = GST_READ_UINT16_BE (data);
852 avipad->parent.hdr.scale = (codec & 0x4) ? 960 : 1024;
856 } else if (!strcmp (mimetype, "audio/x-vorbis")) {
857 avipad->auds.format = GST_RIFF_WAVE_FORMAT_VORBIS3;
858 } else if (!strcmp (mimetype, "audio/x-ac3")) {
859 avipad->auds.format = GST_RIFF_WAVE_FORMAT_A52;
860 } else if (!strcmp (mimetype, "audio/x-alaw")) {
861 avipad->auds.format = GST_RIFF_WAVE_FORMAT_ALAW;
862 avipad->auds.size = 8;
863 avipad->auds.blockalign = avipad->auds.channels;
864 avipad->auds.av_bps = avipad->auds.blockalign * avipad->auds.rate;
865 } else if (!strcmp (mimetype, "audio/x-mulaw")) {
866 avipad->auds.format = GST_RIFF_WAVE_FORMAT_MULAW;
867 avipad->auds.size = 8;
868 avipad->auds.blockalign = avipad->auds.channels;
869 avipad->auds.av_bps = avipad->auds.blockalign * avipad->auds.rate;
870 } else if (!strcmp (mimetype, "audio/x-wma")) {
875 if (gst_structure_get_int (structure, "wmaversion", &version)) {
878 avipad->auds.format = GST_RIFF_WAVE_FORMAT_WMAV1;
881 avipad->auds.format = GST_RIFF_WAVE_FORMAT_WMAV2;
888 if (avipad->auds.format != 0) {
889 if (gst_structure_get_int (structure, "block_align", &block_align)) {
890 avipad->auds.blockalign = block_align;
892 if (gst_structure_get_int (structure, "bitrate", &bitrate)) {
893 avipad->auds.av_bps = bitrate / 8;
899 if (!avipad->auds.format)
902 avipad->parent.hdr.fcc_handler = avipad->auds.format;
903 gst_avi_mux_audsink_set_fields (avimux, avipad);
905 gst_object_unref (avimux);
910 GST_WARNING_OBJECT (avimux, "refused caps %" GST_PTR_FORMAT, vscaps);
911 gst_object_unref (avimux);
918 gst_avi_mux_request_new_pad (GstElement * element,
919 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
924 GstElementClass *klass;
926 const gchar *pad_name = NULL;
929 g_return_val_if_fail (templ != NULL, NULL);
931 if (templ->direction != GST_PAD_SINK)
932 goto wrong_direction;
934 g_return_val_if_fail (GST_IS_AVI_MUX (element), NULL);
935 avimux = GST_AVI_MUX (element);
937 if (!avimux->write_header)
940 klass = GST_ELEMENT_GET_CLASS (element);
942 if (templ == gst_element_class_get_pad_template (klass, "audio_%u")) {
943 /* don't mix named and unnamed pads, if the pad already exists we fail when
944 * trying to add it */
945 if (req_name != NULL && sscanf (req_name, "audio_%u", &pad_id) == 1) {
948 name = g_strdup_printf ("audio_%u", avimux->audio_pads++);
952 /* init pad specific data */
953 avipad = g_malloc0 (sizeof (GstAviAudioPad));
954 avipad->is_video = FALSE;
955 avipad->hdr.type = GST_MAKE_FOURCC ('a', 'u', 'd', 's');
956 /* audio goes last */
957 avimux->sinkpads = g_slist_append (avimux->sinkpads, avipad);
958 } else if (templ == gst_element_class_get_pad_template (klass, "video_%u")) {
959 /* though streams are pretty generic and relatively self-contained,
960 * some video info goes in a single avi header -and therefore mux struct-
961 * so video restricted to one stream */
962 if (avimux->video_pads > 0)
963 goto too_many_video_pads;
966 pad_name = "video_0";
967 avimux->video_pads++;
969 /* init pad specific data */
970 avipad = g_malloc0 (sizeof (GstAviVideoPad));
971 avipad->is_video = TRUE;
972 avipad->hdr.type = GST_MAKE_FOURCC ('v', 'i', 'd', 's');
973 /* video goes first */
974 avimux->sinkpads = g_slist_prepend (avimux->sinkpads, avipad);
978 newpad = gst_pad_new_from_template (templ, pad_name);
982 avipad->collect = gst_collect_pads2_add_pad (avimux->collect,
983 newpad, sizeof (GstAviCollectData));
984 ((GstAviCollectData *) (avipad->collect))->avipad = avipad;
986 if (!gst_element_add_pad (element, newpad))
989 GST_DEBUG_OBJECT (newpad, "Added new request pad");
996 g_warning ("avimux: request pad that is not a SINK pad\n");
1001 g_warning ("avimux: request pad cannot be added after streaming started\n");
1006 g_warning ("avimux: this is not our template!\n");
1009 too_many_video_pads:
1011 GST_WARNING_OBJECT (avimux, "Can only have one video stream");
1016 GST_WARNING_OBJECT (avimux, "Adding the new pad '%s' failed", pad_name);
1017 gst_object_unref (newpad);
1023 gst_avi_mux_release_pad (GstElement * element, GstPad * pad)
1025 GstAviMux *avimux = GST_AVI_MUX (element);
1028 node = avimux->sinkpads;
1030 GstAviPad *avipad = (GstAviPad *) node->data;
1032 if (avipad->collect->pad == pad) {
1033 /* pad count should not be adjusted,
1034 * as it also represent number of streams present */
1035 avipad->collect = NULL;
1036 GST_DEBUG_OBJECT (avimux, "removed pad '%s'", GST_PAD_NAME (pad));
1037 gst_collect_pads2_remove_pad (avimux->collect, pad);
1038 gst_element_remove_pad (element, pad);
1039 /* if not started yet, we can remove any sign this pad ever existed */
1040 /* in this case _start will take care of the real pad count */
1041 if (avimux->write_header) {
1042 avimux->sinkpads = g_slist_remove (avimux->sinkpads, avipad);
1043 gst_avi_mux_pad_reset (avipad, TRUE);
1052 g_warning ("Unknown pad %s", GST_PAD_NAME (pad));
1056 gst_avi_mux_start_chunk (GstByteWriter * bw, const gchar * tag, guint32 fourcc)
1061 gst_byte_writer_put_data (bw, (const guint8 *) tag, 4);
1063 gst_byte_writer_put_uint32_le (bw, fourcc);
1065 chunk_offset = gst_byte_writer_get_pos (bw);
1066 /* real chunk size comes later */
1067 gst_byte_writer_put_uint32_le (bw, 0);
1069 return chunk_offset;
1073 gst_avi_mux_end_chunk (GstByteWriter * bw, guint chunk_offset)
1077 size = gst_byte_writer_get_pos (bw);
1079 gst_byte_writer_set_pos (bw, chunk_offset);
1080 gst_byte_writer_put_uint32_le (bw, size - chunk_offset - 4);
1081 gst_byte_writer_set_pos (bw, size);
1083 /* arrange for even padding */
1085 gst_byte_writer_put_uint8 (bw, 0);
1088 /* maybe some of these functions should be moved to riff.h? */
1091 gst_avi_mux_write_tag (const GstTagList * list, const gchar * tag,
1100 GST_RIFF_INFO_IARL, GST_TAG_LOCATION}, {
1101 GST_RIFF_INFO_IART, GST_TAG_ARTIST}, {
1102 GST_RIFF_INFO_ICMT, GST_TAG_COMMENT}, {
1103 GST_RIFF_INFO_ICOP, GST_TAG_COPYRIGHT}, {
1104 GST_RIFF_INFO_ICRD, GST_TAG_DATE}, {
1105 GST_RIFF_INFO_IGNR, GST_TAG_GENRE}, {
1106 GST_RIFF_INFO_IKEY, GST_TAG_KEYWORDS}, {
1107 GST_RIFF_INFO_INAM, GST_TAG_TITLE}, {
1108 GST_RIFF_INFO_ISFT, GST_TAG_ENCODER}, {
1109 GST_RIFF_INFO_ISRC, GST_TAG_ISRC}, {
1114 GstByteWriter *bw = data;
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) {
1120 chunk = gst_avi_mux_start_chunk (bw, NULL, rifftags[n].fcc);
1121 gst_byte_writer_put_string (bw, str);
1122 gst_avi_mux_end_chunk (bw, chunk);
1130 gst_avi_mux_riff_get_avi_header (GstAviMux * avimux)
1132 const GstTagList *tags;
1137 guint avih, riff, hdrl;
1140 GST_DEBUG_OBJECT (avimux, "creating avi header, data_size %u, idx_size %u",
1141 avimux->data_size, avimux->idx_size);
1143 if (avimux->tags_snap)
1144 tags = avimux->tags_snap;
1146 /* need to make snapshot of current state of tags to ensure the same set
1147 * is used next time around during header rewrite at the end */
1148 tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (avimux));
1150 tags = avimux->tags_snap = gst_tag_list_copy (tags);
1153 gst_byte_writer_init_with_size (&bw, 1024, FALSE);
1155 /* avi header metadata */
1156 riff = gst_avi_mux_start_chunk (&bw, "RIFF", 0);
1157 gst_byte_writer_put_data (&bw, (guint8 *) "AVI ", 4);
1158 hdrl = gst_avi_mux_start_chunk (&bw, "LIST", 0);
1159 gst_byte_writer_put_data (&bw, (guint8 *) "hdrl", 4);
1161 avih = gst_avi_mux_start_chunk (&bw, "avih", 0);
1162 /* the AVI header itself */
1163 gst_byte_writer_put_uint32_le (&bw, avimux->avi_hdr.us_frame);
1164 gst_byte_writer_put_uint32_le (&bw, avimux->avi_hdr.max_bps);
1165 gst_byte_writer_put_uint32_le (&bw, avimux->avi_hdr.pad_gran);
1166 gst_byte_writer_put_uint32_le (&bw, avimux->avi_hdr.flags);
1167 gst_byte_writer_put_uint32_le (&bw, avimux->avi_hdr.tot_frames);
1168 gst_byte_writer_put_uint32_le (&bw, avimux->avi_hdr.init_frames);
1169 gst_byte_writer_put_uint32_le (&bw, avimux->avi_hdr.streams);
1170 gst_byte_writer_put_uint32_le (&bw, avimux->avi_hdr.bufsize);
1171 gst_byte_writer_put_uint32_le (&bw, avimux->avi_hdr.width);
1172 gst_byte_writer_put_uint32_le (&bw, avimux->avi_hdr.height);
1173 gst_byte_writer_put_uint32_le (&bw, avimux->avi_hdr.scale);
1174 gst_byte_writer_put_uint32_le (&bw, avimux->avi_hdr.rate);
1175 gst_byte_writer_put_uint32_le (&bw, avimux->avi_hdr.start);
1176 gst_byte_writer_put_uint32_le (&bw, avimux->avi_hdr.length);
1177 gst_avi_mux_end_chunk (&bw, avih);
1180 node = avimux->sinkpads;
1182 GstAviPad *avipad = (GstAviPad *) node->data;
1183 GstAviVideoPad *vidpad = (GstAviVideoPad *) avipad;
1184 GstAviAudioPad *audpad = (GstAviAudioPad *) avipad;
1185 gint codec_size = 0;
1186 guint strh, strl, strf, indx;
1188 /* stream list metadata */
1189 strl = gst_avi_mux_start_chunk (&bw, "LIST", 0);
1190 gst_byte_writer_put_data (&bw, (guint8 *) "strl", 4);
1192 /* generic header */
1193 strh = gst_avi_mux_start_chunk (&bw, "strh", 0);
1194 gst_byte_writer_put_uint32_le (&bw, avipad->hdr.type);
1195 gst_byte_writer_put_uint32_le (&bw, avipad->hdr.fcc_handler);
1196 gst_byte_writer_put_uint32_le (&bw, avipad->hdr.flags);
1197 gst_byte_writer_put_uint32_le (&bw, avipad->hdr.priority);
1198 gst_byte_writer_put_uint32_le (&bw, avipad->hdr.init_frames);
1199 gst_byte_writer_put_uint32_le (&bw, avipad->hdr.scale);
1200 gst_byte_writer_put_uint32_le (&bw, avipad->hdr.rate);
1201 gst_byte_writer_put_uint32_le (&bw, avipad->hdr.start);
1202 gst_byte_writer_put_uint32_le (&bw, avipad->hdr.length);
1203 gst_byte_writer_put_uint32_le (&bw, avipad->hdr.bufsize);
1204 gst_byte_writer_put_uint32_le (&bw, avipad->hdr.quality);
1205 gst_byte_writer_put_uint32_le (&bw, avipad->hdr.samplesize);
1206 gst_byte_writer_put_uint16_le (&bw, 0);
1207 gst_byte_writer_put_uint16_le (&bw, 0);
1208 gst_byte_writer_put_uint16_le (&bw, 0);
1209 gst_byte_writer_put_uint16_le (&bw, 0);
1210 gst_avi_mux_end_chunk (&bw, strh);
1212 if (avipad->is_video) {
1213 codec_size = vidpad->vids_codec_data ?
1214 gst_buffer_get_size (vidpad->vids_codec_data) : 0;
1215 /* the video header */
1216 strf = gst_avi_mux_start_chunk (&bw, "strf", 0);
1217 /* the actual header */
1218 gst_byte_writer_put_uint32_le (&bw, vidpad->vids.size + codec_size);
1219 gst_byte_writer_put_uint32_le (&bw, vidpad->vids.width);
1220 gst_byte_writer_put_uint32_le (&bw, vidpad->vids.height);
1221 gst_byte_writer_put_uint16_le (&bw, vidpad->vids.planes);
1222 gst_byte_writer_put_uint16_le (&bw, vidpad->vids.bit_cnt);
1223 gst_byte_writer_put_uint32_le (&bw, vidpad->vids.compression);
1224 gst_byte_writer_put_uint32_le (&bw, vidpad->vids.image_size);
1225 gst_byte_writer_put_uint32_le (&bw, vidpad->vids.xpels_meter);
1226 gst_byte_writer_put_uint32_le (&bw, vidpad->vids.ypels_meter);
1227 gst_byte_writer_put_uint32_le (&bw, vidpad->vids.num_colors);
1228 gst_byte_writer_put_uint32_le (&bw, vidpad->vids.imp_colors);
1229 if (vidpad->vids_codec_data) {
1230 gst_buffer_map (vidpad->vids_codec_data, &map, GST_MAP_READ);
1231 gst_byte_writer_put_data (&bw, map.data, map.size);
1232 gst_buffer_unmap (vidpad->vids_codec_data, &map);
1234 gst_avi_mux_end_chunk (&bw, strf);
1236 /* add video property data, mainly for aspect ratio, if any */
1237 if (vidpad->vprp.aspect) {
1241 /* let's be on the safe side */
1242 vidpad->vprp.fields = MIN (vidpad->vprp.fields,
1243 GST_RIFF_VPRP_VIDEO_FIELDS);
1244 /* the vprp header */
1245 vprp = gst_avi_mux_start_chunk (&bw, "vprp", 0);
1246 /* the actual data */
1247 gst_byte_writer_put_uint32_le (&bw, vidpad->vprp.format_token);
1248 gst_byte_writer_put_uint32_le (&bw, vidpad->vprp.standard);
1249 gst_byte_writer_put_uint32_le (&bw, vidpad->vprp.vert_rate);
1250 gst_byte_writer_put_uint32_le (&bw, vidpad->vprp.hor_t_total);
1251 gst_byte_writer_put_uint32_le (&bw, vidpad->vprp.vert_lines);
1252 gst_byte_writer_put_uint32_le (&bw, vidpad->vprp.aspect);
1253 gst_byte_writer_put_uint32_le (&bw, vidpad->vprp.width);
1254 gst_byte_writer_put_uint32_le (&bw, vidpad->vprp.height);
1255 gst_byte_writer_put_uint32_le (&bw, vidpad->vprp.fields);
1257 for (f = 0; f < vidpad->vprp.fields; ++f) {
1258 gst_riff_vprp_video_field_desc *fd;
1260 fd = &(vidpad->vprp.field_info[f]);
1261 gst_byte_writer_put_uint32_le (&bw, fd->compressed_bm_height);
1262 gst_byte_writer_put_uint32_le (&bw, fd->compressed_bm_width);
1263 gst_byte_writer_put_uint32_le (&bw, fd->valid_bm_height);
1264 gst_byte_writer_put_uint32_le (&bw, fd->valid_bm_width);
1265 gst_byte_writer_put_uint32_le (&bw, fd->valid_bm_x_offset);
1266 gst_byte_writer_put_uint32_le (&bw, fd->valid_bm_y_offset);
1267 gst_byte_writer_put_uint32_le (&bw, fd->video_x_t_offset);
1268 gst_byte_writer_put_uint32_le (&bw, fd->video_y_start);
1270 gst_avi_mux_end_chunk (&bw, vprp);
1273 codec_size = audpad->auds_codec_data ?
1274 gst_buffer_get_size (audpad->auds_codec_data) : 0;
1275 /* the audio header */
1276 strf = gst_avi_mux_start_chunk (&bw, "strf", 0);
1277 /* the actual header */
1278 gst_byte_writer_put_uint16_le (&bw, audpad->auds.format);
1279 gst_byte_writer_put_uint16_le (&bw, audpad->auds.channels);
1280 gst_byte_writer_put_uint32_le (&bw, audpad->auds.rate);
1281 gst_byte_writer_put_uint32_le (&bw, audpad->auds.av_bps);
1282 gst_byte_writer_put_uint16_le (&bw, audpad->auds.blockalign);
1283 gst_byte_writer_put_uint16_le (&bw, audpad->auds.size);
1284 gst_byte_writer_put_uint16_le (&bw, codec_size);
1285 if (audpad->auds_codec_data) {
1286 gst_buffer_map (audpad->auds_codec_data, &map, GST_MAP_READ);
1287 gst_byte_writer_put_data (&bw, map.data, map.size);
1288 gst_buffer_unmap (vidpad->vids_codec_data, &map);
1290 gst_avi_mux_end_chunk (&bw, strf);
1293 /* odml superindex chunk */
1294 if (avipad->idx_index > 0)
1295 indx = gst_avi_mux_start_chunk (&bw, "indx", 0);
1297 indx = gst_avi_mux_start_chunk (&bw, "JUNK", 0);
1298 gst_byte_writer_put_uint16_le (&bw, 4); /* bytes per entry */
1299 gst_byte_writer_put_uint8 (&bw, 0); /* index subtype */
1300 gst_byte_writer_put_uint8 (&bw, GST_AVI_INDEX_OF_INDEXES); /* index type */
1301 gst_byte_writer_put_uint32_le (&bw, avipad->idx_index); /* entries in use */
1302 gst_byte_writer_put_data (&bw, (guint8 *) avipad->tag, 4); /* stream id */
1303 gst_byte_writer_put_uint32_le (&bw, 0); /* reserved */
1304 gst_byte_writer_put_uint32_le (&bw, 0); /* reserved */
1305 gst_byte_writer_put_uint32_le (&bw, 0); /* reserved */
1306 gst_byte_writer_put_data (&bw, (guint8 *) avipad->idx,
1307 GST_AVI_SUPERINDEX_COUNT * sizeof (gst_avi_superindex_entry));
1308 gst_avi_mux_end_chunk (&bw, indx);
1310 /* end strl for this stream */
1311 gst_avi_mux_end_chunk (&bw, strl);
1316 if (avimux->video_pads > 0) {
1319 odml = gst_avi_mux_start_chunk (&bw, "LIST", 0);
1320 gst_byte_writer_put_data (&bw, (guint8 *) "odml", 4);
1321 dmlh = gst_avi_mux_start_chunk (&bw, "dmlh", 0);
1322 gst_byte_writer_put_uint32_le (&bw, avimux->total_frames);
1323 gst_avi_mux_end_chunk (&bw, dmlh);
1324 gst_avi_mux_end_chunk (&bw, odml);
1328 gst_avi_mux_end_chunk (&bw, hdrl);
1334 info = gst_avi_mux_start_chunk (&bw, "LIST", 0);
1335 gst_byte_writer_put_data (&bw, (guint8 *) "INFO", 4);
1337 gst_tag_list_foreach (tags, gst_avi_mux_write_tag, &bw);
1338 if (info + 8 == gst_byte_writer_get_pos (&bw)) {
1339 /* no tags writen, remove the empty INFO LIST as it is useless
1340 * and prevents playback in vlc */
1341 gst_byte_writer_set_pos (&bw, info - 4);
1343 gst_avi_mux_end_chunk (&bw, info);
1348 gst_avi_mux_end_chunk (&bw, riff);
1350 /* avi data header */
1351 gst_byte_writer_put_data (&bw, (guint8 *) "LIST", 4);
1352 gst_byte_writer_put_uint32_le (&bw, avimux->data_size);
1353 gst_byte_writer_put_data (&bw, (guint8 *) "movi", 4);
1355 /* now get the data */
1356 buffer = gst_byte_writer_reset_and_get_buffer (&bw);
1358 /* ... but RIFF includes more than just header */
1359 gst_buffer_map (buffer, &map, GST_MAP_READWRITE);
1360 size = GST_READ_UINT32_LE (map.data + 4);
1361 size += 8 + avimux->data_size + avimux->idx_size;
1362 GST_WRITE_UINT32_LE (map.data + 4, size);
1364 GST_MEMDUMP_OBJECT (avimux, "avi header", map.data, map.size);
1365 gst_buffer_unmap (buffer, &map);
1371 gst_avi_mux_riff_get_avix_header (guint32 datax_size)
1376 buffer = gst_buffer_new_and_alloc (24);
1378 gst_buffer_map (buffer, &map, GST_MAP_WRITE);
1379 memcpy (map.data + 0, "RIFF", 4);
1380 GST_WRITE_UINT32_LE (map.data + 4, datax_size + 3 * 4);
1381 memcpy (map.data + 8, "AVIX", 4);
1382 memcpy (map.data + 12, "LIST", 4);
1383 GST_WRITE_UINT32_LE (map.data + 16, datax_size);
1384 memcpy (map.data + 20, "movi", 4);
1385 gst_buffer_unmap (buffer, &map);
1390 static inline GstBuffer *
1391 gst_avi_mux_riff_get_header (GstAviPad * avipad, guint32 video_frame_size)
1396 buffer = gst_buffer_new_and_alloc (8);
1398 gst_buffer_map (buffer, &map, GST_MAP_WRITE);
1399 memcpy (map.data + 0, avipad->tag, 4);
1400 GST_WRITE_UINT32_LE (map.data + 4, video_frame_size);
1401 gst_buffer_unmap (buffer, &map);
1406 /* write an odml index chunk in the movi list */
1407 static GstFlowReturn
1408 gst_avi_mux_write_avix_index (GstAviMux * avimux, GstAviPad * avipad,
1409 gchar * code, gchar * chunk, gst_avi_superindex_entry * super_index,
1410 gint * super_index_count)
1415 gst_riff_index_entry *entry;
1417 guint32 size, entry_count;
1418 gboolean is_pcm = FALSE;
1419 guint32 pcm_samples = 0;
1422 /* check if it is pcm */
1423 if (avipad && !avipad->is_video) {
1424 GstAviAudioPad *audiopad = (GstAviAudioPad *) avipad;
1425 if (audiopad->auds.format == GST_RIFF_WAVE_FORMAT_PCM) {
1426 pcm_samples = audiopad->samples;
1431 /* allocate the maximum possible */
1432 buffer = gst_buffer_new_and_alloc (32 + 8 * avimux->idx_index);
1434 gst_buffer_map (buffer, &map, GST_MAP_WRITE);
1437 /* general index chunk info */
1438 memcpy (map.data + 0, chunk, 4); /* chunk id */
1439 GST_WRITE_UINT32_LE (map.data + 4, 0); /* chunk size; fill later */
1440 GST_WRITE_UINT16_LE (map.data + 8, 2); /* index entry is 2 words */
1441 map.data[10] = 0; /* index subtype */
1442 map.data[11] = GST_AVI_INDEX_OF_CHUNKS; /* index type: AVI_INDEX_OF_CHUNKS */
1443 GST_WRITE_UINT32_LE (map.data + 12, 0); /* entries in use; fill later */
1444 memcpy (map.data + 16, code, 4); /* stream to which index refers */
1445 GST_WRITE_UINT64_LE (map.data + 20, avimux->avix_start); /* base offset */
1446 GST_WRITE_UINT32_LE (map.data + 28, 0); /* reserved */
1449 /* now the actual index entries */
1450 i = avimux->idx_index;
1451 entry = avimux->idx;
1453 if (memcmp (&entry->id, code, 4) == 0) {
1454 /* enter relative offset to the data (!) */
1455 GST_WRITE_UINT32_LE (map.data, GUINT32_FROM_LE (entry->offset) + 8);
1456 /* msb is set if not (!) keyframe */
1457 GST_WRITE_UINT32_LE (map.data + 4, GUINT32_FROM_LE (entry->size)
1458 | (GUINT32_FROM_LE (entry->flags)
1459 & GST_RIFF_IF_KEYFRAME ? 0 : 1U << 31));
1466 /* ok, now we know the size and no of entries, fill in where needed */
1467 size = map.data - data;
1468 GST_WRITE_UINT32_LE (data + 4, size - 8);
1469 entry_count = (size - 32) / 8;
1470 GST_WRITE_UINT32_LE (data + 12, entry_count);
1471 gst_buffer_unmap (buffer, &map);
1472 gst_buffer_resize (buffer, 0, size);
1475 if ((res = gst_pad_push (avimux->srcpad, buffer)) != GST_FLOW_OK)
1478 /* keep track of this in superindex (if room) ... */
1479 if (*super_index_count < GST_AVI_SUPERINDEX_COUNT) {
1480 i = *super_index_count;
1481 super_index[i].offset = GUINT64_TO_LE (avimux->total_data);
1482 super_index[i].size = GUINT32_TO_LE (size);
1484 super_index[i].duration = GUINT32_TO_LE (pcm_samples);
1486 super_index[i].duration = GUINT32_TO_LE (entry_count);
1488 (*super_index_count)++;
1490 GST_WARNING_OBJECT (avimux, "No more room in superindex of stream %s",
1493 /* ... and in size */
1494 avimux->total_data += size;
1495 if (avimux->is_bigfile)
1496 avimux->datax_size += size;
1498 avimux->data_size += size;
1503 /* some other usable functions (thankyou xawtv ;-) ) */
1506 gst_avi_mux_add_index (GstAviMux * avimux, GstAviPad * avipad, guint32 flags,
1509 gchar *code = avipad->tag;
1510 if (avimux->idx_index == avimux->idx_count) {
1511 avimux->idx_count += 256;
1513 g_realloc (avimux->idx,
1514 avimux->idx_count * sizeof (gst_riff_index_entry));
1517 /* in case of pcm audio, we need to count the number of samples for
1518 * putting in the indx entries */
1519 if (!avipad->is_video) {
1520 GstAviAudioPad *audiopad = (GstAviAudioPad *) avipad;
1521 if (audiopad->auds.format == GST_RIFF_WAVE_FORMAT_PCM) {
1522 audiopad->samples += size / audiopad->auds.blockalign;
1526 memcpy (&(avimux->idx[avimux->idx_index].id), code, 4);
1527 avimux->idx[avimux->idx_index].flags = GUINT32_TO_LE (flags);
1528 avimux->idx[avimux->idx_index].offset = GUINT32_TO_LE (avimux->idx_offset);
1529 avimux->idx[avimux->idx_index].size = GUINT32_TO_LE (size);
1530 avimux->idx_index++;
1533 static GstFlowReturn
1534 gst_avi_mux_write_index (GstAviMux * avimux)
1542 buffer = gst_buffer_new_and_alloc (8);
1544 gst_buffer_map (buffer, &map, GST_MAP_WRITE);
1545 memcpy (map.data + 0, "idx1", 4);
1546 GST_WRITE_UINT32_LE (map.data + 4,
1547 avimux->idx_index * sizeof (gst_riff_index_entry));
1548 gst_buffer_unmap (buffer, &map);
1550 res = gst_pad_push (avimux->srcpad, buffer);
1551 if (res != GST_FLOW_OK)
1554 buffer = gst_buffer_new ();
1556 size = avimux->idx_index * sizeof (gst_riff_index_entry);
1557 data = (guint8 *) avimux->idx;
1558 avimux->idx = NULL; /* will be free()'ed by gst_buffer_unref() */
1560 gst_buffer_take_memory (buffer, -1,
1561 gst_memory_new_wrapped (0, data, g_free, size, 0, size));
1563 avimux->total_data += size + 8;
1565 res = gst_pad_push (avimux->srcpad, buffer);
1566 if (res != GST_FLOW_OK)
1569 avimux->idx_size += avimux->idx_index * sizeof (gst_riff_index_entry) + 8;
1572 avimux->avi_hdr.flags |= GST_RIFF_AVIH_HASINDEX;
1576 static GstFlowReturn
1577 gst_avi_mux_bigfile (GstAviMux * avimux, gboolean last)
1579 GstFlowReturn res = GST_FLOW_OK;
1583 /* first some odml standard index chunks in the movi list */
1584 node = avimux->sinkpads;
1586 GstAviPad *avipad = (GstAviPad *) node->data;
1590 res = gst_avi_mux_write_avix_index (avimux, avipad, avipad->tag,
1591 avipad->idx_tag, avipad->idx, &avipad->idx_index);
1592 if (res != GST_FLOW_OK)
1596 if (avimux->is_bigfile) {
1599 gst_segment_init (&segment, GST_FORMAT_BYTES);
1602 segment.start = avimux->avix_start;
1603 segment.time = avimux->avix_start;
1604 gst_pad_push_event (avimux->srcpad, gst_event_new_segment (&segment));
1606 /* rewrite AVIX header */
1607 header = gst_avi_mux_riff_get_avix_header (avimux->datax_size);
1608 res = gst_pad_push (avimux->srcpad, header);
1610 /* go back to current location, at least try */
1611 segment.start = avimux->total_data;
1612 segment.time = avimux->total_data;
1613 gst_pad_push_event (avimux->srcpad, gst_event_new_segment (&segment));
1615 if (res != GST_FLOW_OK)
1617 } else { /* write a standard index in the first riff chunk */
1618 res = gst_avi_mux_write_index (avimux);
1619 /* the index data/buffer is freed by pushing it */
1620 avimux->idx_count = 0;
1621 if (res != GST_FLOW_OK)
1625 avimux->avix_start = avimux->total_data;
1630 avimux->is_bigfile = TRUE;
1631 avimux->numx_frames = 0;
1632 avimux->datax_size = 4; /* movi tag */
1633 avimux->idx_index = 0;
1634 node = avimux->sinkpads;
1636 GstAviPad *avipad = (GstAviPad *) node->data;
1638 if (!avipad->is_video) {
1639 GstAviAudioPad *audiopad = (GstAviAudioPad *) avipad;
1640 audiopad->samples = 0;
1644 header = gst_avi_mux_riff_get_avix_header (0);
1645 avimux->total_data += gst_buffer_get_size (header);
1646 /* avix_start is used as base offset for the odml index chunk */
1647 avimux->idx_offset = avimux->total_data - avimux->avix_start;
1649 return gst_pad_push (avimux->srcpad, header);
1652 /* enough header blabla now, let's go on to actually writing the headers */
1654 static GstFlowReturn
1655 gst_avi_mux_start_file (GstAviMux * avimux)
1663 avimux->total_data = 0;
1664 avimux->total_frames = 0;
1665 avimux->data_size = 4; /* movi tag */
1666 avimux->datax_size = 0;
1667 avimux->num_frames = 0;
1668 avimux->numx_frames = 0;
1669 avimux->avix_start = 0;
1671 avimux->idx_index = 0;
1672 avimux->idx_offset = 0; /* see 10 lines below */
1673 avimux->idx_size = 0;
1674 avimux->idx_count = 0;
1678 avimux->write_header = FALSE;
1679 avimux->restart = FALSE;
1681 /* init streams, see what we've got */
1682 node = avimux->sinkpads;
1683 avimux->audio_pads = avimux->video_pads = 0;
1685 GstAviPad *avipad = (GstAviPad *) node->data;
1689 if (!avipad->is_video) {
1690 /* audio stream numbers must start at 1 iff there is a video stream 0;
1691 * request_pad inserts video pad at head of list, so this test suffices */
1692 if (avimux->video_pads)
1693 avimux->audio_pads++;
1694 avipad->tag = g_strdup_printf ("%02uwb", avimux->audio_pads);
1695 avipad->idx_tag = g_strdup_printf ("ix%02u", avimux->audio_pads);
1696 if (!avimux->video_pads)
1697 avimux->audio_pads++;
1699 avipad->tag = g_strdup_printf ("%02udb", avimux->video_pads);
1700 avipad->idx_tag = g_strdup_printf ("ix%02u", avimux->video_pads++);
1704 caps = gst_caps_copy (gst_pad_get_pad_template_caps (avimux->srcpad));
1705 gst_pad_set_caps (avimux->srcpad, caps);
1706 gst_caps_unref (caps);
1708 /* let downstream know we think in BYTES and expect to do seeking later on */
1709 gst_segment_init (&segment, GST_FORMAT_BYTES);
1710 gst_pad_push_event (avimux->srcpad, gst_event_new_segment (&segment));
1713 avimux->avi_hdr.streams = g_slist_length (avimux->sinkpads);
1714 avimux->is_bigfile = FALSE;
1716 header = gst_avi_mux_riff_get_avi_header (avimux);
1717 avimux->total_data += gst_buffer_get_size (header);
1719 res = gst_pad_push (avimux->srcpad, header);
1721 avimux->idx_offset = avimux->total_data;
1726 static GstFlowReturn
1727 gst_avi_mux_stop_file (GstAviMux * avimux)
1729 GstFlowReturn res = GST_FLOW_OK;
1734 /* if bigfile, rewrite header, else write indexes */
1735 /* don't bail out at once if error, still try to re-write header */
1736 if (avimux->video_pads > 0) {
1737 if (avimux->is_bigfile) {
1738 res = gst_avi_mux_bigfile (avimux, TRUE);
1740 res = gst_avi_mux_write_index (avimux);
1744 /* we do our best to make it interleaved at least ... */
1745 if (avimux->audio_pads > 0 && avimux->video_pads > 0)
1746 avimux->avi_hdr.flags |= GST_RIFF_AVIH_ISINTERLEAVED;
1748 /* set rate and everything having to do with that */
1749 avimux->avi_hdr.max_bps = 0;
1750 node = avimux->sinkpads;
1752 GstAviPad *avipad = (GstAviPad *) node->data;
1756 if (!avipad->is_video) {
1757 GstAviAudioPad *audpad = (GstAviAudioPad *) avipad;
1759 /* calculate bps if needed */
1760 if (!audpad->auds.av_bps) {
1761 if (audpad->audio_time) {
1762 audpad->auds.av_bps =
1763 (GST_SECOND * audpad->audio_size) / audpad->audio_time;
1764 /* round bps to nearest multiple of 8;
1765 * which is much more likely to be the (cbr) bitrate in use;
1766 * which in turn results in better timestamp calculation on playback */
1767 audpad->auds.av_bps = GST_ROUND_UP_8 (audpad->auds.av_bps - 4);
1769 GST_ELEMENT_WARNING (avimux, STREAM, MUX,
1770 (_("No or invalid input audio, AVI stream will be corrupt.")),
1772 audpad->auds.av_bps = 0;
1775 gst_avi_mux_audsink_set_fields (avimux, audpad);
1776 avimux->avi_hdr.max_bps += audpad->auds.av_bps;
1777 avipad->hdr.length = gst_util_uint64_scale (audpad->audio_time,
1778 avipad->hdr.rate, avipad->hdr.scale * GST_SECOND);
1780 GstAviVideoPad *vidpad = (GstAviVideoPad *) avipad;
1782 avimux->avi_hdr.max_bps += ((vidpad->vids.bit_cnt + 7) / 8) *
1783 (1000000. / avimux->avi_hdr.us_frame) * vidpad->vids.image_size;
1784 avipad->hdr.length = avimux->total_frames;
1788 /* statistics/total_frames/... */
1789 avimux->avi_hdr.tot_frames = avimux->num_frames;
1791 /* seek and rewrite the header */
1792 gst_segment_init (&segment, GST_FORMAT_BYTES);
1793 gst_pad_push_event (avimux->srcpad, gst_event_new_segment (&segment));
1795 /* the first error survives */
1796 header = gst_avi_mux_riff_get_avi_header (avimux);
1797 if (res == GST_FLOW_OK)
1798 res = gst_pad_push (avimux->srcpad, header);
1800 gst_pad_push (avimux->srcpad, header);
1802 segment.start = avimux->total_data;
1803 segment.time = avimux->total_data;
1804 gst_pad_push_event (avimux->srcpad, gst_event_new_segment (&segment));
1806 avimux->write_header = TRUE;
1811 static GstFlowReturn
1812 gst_avi_mux_restart_file (GstAviMux * avimux)
1816 if ((res = gst_avi_mux_stop_file (avimux)) != GST_FLOW_OK)
1819 gst_pad_push_event (avimux->srcpad, gst_event_new_eos ());
1821 return gst_avi_mux_start_file (avimux);
1824 /* handle events (search) */
1826 gst_avi_mux_handle_event (GstCollectPads2 * pads, GstCollectData2 * data,
1827 GstEvent * event, gpointer user_data)
1830 gboolean ret = FALSE;
1832 avimux = GST_AVI_MUX (user_data);
1834 switch (GST_EVENT_TYPE (event)) {
1835 case GST_EVENT_CAPS:
1838 GstAviCollectData *collect_pad;
1839 GstAviVideoPad *avipad;
1841 gst_event_parse_caps (event, &caps);
1843 /* find stream data */
1844 collect_pad = (GstAviCollectData *) data;
1845 g_assert (collect_pad);
1846 avipad = (GstAviVideoPad *) collect_pad->avipad;
1849 if (avipad->parent.is_video) {
1850 ret = gst_avi_mux_vidsink_set_caps (data->pad, caps);
1852 ret = gst_avi_mux_audsink_set_caps (data->pad, caps);
1854 gst_event_unref (event);
1857 case GST_EVENT_TAG:{
1859 GstTagSetter *setter = GST_TAG_SETTER (avimux);
1860 const GstTagMergeMode mode = gst_tag_setter_get_tag_merge_mode (setter);
1862 gst_event_parse_tag (event, &list);
1863 gst_tag_setter_merge_tags (setter, list, mode);
1864 gst_event_unref (event);
1869 case GST_EVENT_SEGMENT:
1870 gst_event_unref (event);
1874 ret = gst_pad_event_default (data->pad, GST_OBJECT (avimux), event);
1881 /* send extra 'padding' data */
1882 static GstFlowReturn
1883 gst_avi_mux_send_pad_data (GstAviMux * avimux, gulong num_bytes)
1887 buffer = gst_buffer_new_and_alloc (num_bytes);
1888 gst_buffer_memset (buffer, 0, 0, num_bytes);
1890 return gst_pad_push (avimux->srcpad, buffer);
1894 static GstFlowReturn
1895 gst_avi_mux_do_buffer (GstAviMux * avimux, GstAviPad * avipad)
1898 GstBuffer *data, *header;
1899 gulong total_size, pad_bytes = 0;
1903 data = gst_collect_pads2_pop (avimux->collect, avipad->collect);
1904 /* arrange downstream running time */
1905 data = gst_buffer_make_writable (data);
1906 GST_BUFFER_TIMESTAMP (data) =
1907 gst_segment_to_running_time (&avipad->collect->segment,
1908 GST_FORMAT_TIME, GST_BUFFER_TIMESTAMP (data));
1910 /* Prepend a special buffer to the first one for some formats */
1911 if (avipad->is_video) {
1912 GstAviVideoPad *vidpad = (GstAviVideoPad *) avipad;
1914 if (vidpad->prepend_buffer) {
1915 GstBuffer *newdata = gst_buffer_merge (vidpad->prepend_buffer, data);
1916 gst_buffer_copy_into (newdata, data, GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
1917 gst_buffer_unref (data);
1918 gst_buffer_unref (vidpad->prepend_buffer);
1921 vidpad->prepend_buffer = NULL;
1925 if (avimux->restart) {
1926 if ((res = gst_avi_mux_restart_file (avimux)) != GST_FLOW_OK)
1930 datasize = gst_buffer_get_size (data);
1932 /* need to restart or start a next avix chunk ? */
1933 if ((avimux->is_bigfile ? avimux->datax_size : avimux->data_size) +
1934 datasize > GST_AVI_MAX_SIZE) {
1935 if (avimux->enable_large_avi) {
1936 if ((res = gst_avi_mux_bigfile (avimux, FALSE)) != GST_FLOW_OK)
1939 if ((res = gst_avi_mux_restart_file (avimux)) != GST_FLOW_OK)
1944 /* get header and record some stats */
1946 pad_bytes = 2 - (datasize & 1);
1948 header = gst_avi_mux_riff_get_header (avipad, datasize);
1949 total_size = gst_buffer_get_size (header) + datasize + pad_bytes;
1951 if (avimux->is_bigfile) {
1952 avimux->datax_size += total_size;
1954 avimux->data_size += total_size;
1957 if (G_UNLIKELY (avipad->hook))
1958 avipad->hook (avimux, avipad, data);
1960 /* the suggested buffer size is the max frame size */
1961 if (avipad->hdr.bufsize < datasize)
1962 avipad->hdr.bufsize = datasize;
1964 if (avipad->is_video) {
1965 avimux->total_frames++;
1967 if (avimux->is_bigfile) {
1968 avimux->numx_frames++;
1970 avimux->num_frames++;
1974 if (!GST_BUFFER_FLAG_IS_SET (data, GST_BUFFER_FLAG_DELTA_UNIT))
1977 GstAviAudioPad *audpad = (GstAviAudioPad *) avipad;
1980 audpad->audio_size += datasize;
1981 audpad->audio_time += GST_BUFFER_DURATION (data);
1984 gst_avi_mux_add_index (avimux, avipad, flags, datasize);
1987 GST_LOG_OBJECT (avimux, "pushing buffers: head, data");
1989 if ((res = gst_pad_push (avimux->srcpad, header)) != GST_FLOW_OK)
1991 if ((res = gst_pad_push (avimux->srcpad, data)) != GST_FLOW_OK)
1995 if ((res = gst_avi_mux_send_pad_data (avimux, pad_bytes)) != GST_FLOW_OK)
1999 /* if any push above fails, we're in trouble with file consistency anyway */
2000 avimux->total_data += total_size;
2001 avimux->idx_offset += total_size;
2006 /* pick the oldest buffer from the pads and push it */
2007 static GstFlowReturn
2008 gst_avi_mux_do_one_buffer (GstAviMux * avimux)
2010 GstAviPad *avipad, *best_pad;
2013 GstClockTime time, best_time, delay;
2015 node = avimux->sinkpads;
2017 best_time = GST_CLOCK_TIME_NONE;
2018 for (; node; node = node->next) {
2019 avipad = (GstAviPad *) node->data;
2021 if (!avipad->collect)
2024 if (!avipad->hdr.fcc_handler)
2025 goto not_negotiated;
2027 buffer = gst_collect_pads2_peek (avimux->collect, avipad->collect);
2030 time = GST_BUFFER_TIMESTAMP (buffer);
2031 gst_buffer_unref (buffer);
2033 /* invalid should pass */
2034 if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (time))) {
2035 time = gst_segment_to_running_time (&avipad->collect->segment,
2036 GST_FORMAT_TIME, time);
2037 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time))) {
2038 GST_DEBUG_OBJECT (avimux, "clipping buffer on pad %s outside segment",
2039 GST_PAD_NAME (avipad->collect->pad));
2040 buffer = gst_collect_pads2_pop (avimux->collect, avipad->collect);
2041 gst_buffer_unref (buffer);
2046 delay = avipad->is_video ? GST_SECOND / 2 : 0;
2048 /* invalid timestamp buffers pass first,
2049 * these are probably initialization buffers */
2050 if (best_pad == NULL || !GST_CLOCK_TIME_IS_VALID (time)
2051 || (GST_CLOCK_TIME_IS_VALID (best_time) && time + delay < best_time)) {
2053 best_time = time + delay;
2058 GST_LOG_OBJECT (avimux, "selected pad %s with time %" GST_TIME_FORMAT,
2059 GST_PAD_NAME (best_pad->collect->pad), GST_TIME_ARGS (best_time));
2061 return gst_avi_mux_do_buffer (avimux, best_pad);
2063 /* simply finish off the file and send EOS */
2064 gst_avi_mux_stop_file (avimux);
2065 gst_pad_push_event (avimux->srcpad, gst_event_new_eos ());
2066 return GST_FLOW_EOS;
2072 GST_ELEMENT_ERROR (avimux, CORE, NEGOTIATION, (NULL),
2073 ("pad %s not negotiated", GST_PAD_NAME (avipad->collect->pad)));
2074 return GST_FLOW_NOT_NEGOTIATED;
2078 static GstFlowReturn
2079 gst_avi_mux_collect_pads (GstCollectPads2 * pads, GstAviMux * avimux)
2083 if (G_UNLIKELY (avimux->write_header)) {
2084 if ((res = gst_avi_mux_start_file (avimux)) != GST_FLOW_OK)
2088 return gst_avi_mux_do_one_buffer (avimux);
2093 gst_avi_mux_get_property (GObject * object,
2094 guint prop_id, GValue * value, GParamSpec * pspec)
2098 avimux = GST_AVI_MUX (object);
2102 g_value_set_boolean (value, avimux->enable_large_avi);
2105 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2111 gst_avi_mux_set_property (GObject * object,
2112 guint prop_id, const GValue * value, GParamSpec * pspec)
2116 avimux = GST_AVI_MUX (object);
2120 avimux->enable_large_avi = g_value_get_boolean (value);
2123 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2128 static GstStateChangeReturn
2129 gst_avi_mux_change_state (GstElement * element, GstStateChange transition)
2132 GstStateChangeReturn ret;
2134 avimux = GST_AVI_MUX (element);
2136 switch (transition) {
2137 case GST_STATE_CHANGE_READY_TO_PAUSED:
2138 gst_collect_pads2_start (avimux->collect);
2140 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2142 case GST_STATE_CHANGE_PAUSED_TO_READY:
2143 gst_collect_pads2_stop (avimux->collect);
2149 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2150 if (ret == GST_STATE_CHANGE_FAILURE)
2153 switch (transition) {
2154 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2156 case GST_STATE_CHANGE_PAUSED_TO_READY:
2157 gst_avi_mux_reset (avimux);
2159 case GST_STATE_CHANGE_READY_TO_NULL: