2 * Copyright (C) <1999> Erik Walthinsen <omega@temple-baptist.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 /* #define GST_DEBUG_ENABLED */
24 #include "gstavidemux.h"
27 /* elementfactory information */
28 static GstElementDetails gst_avi_demux_details = {
31 "Parse a .avi file into audio and video",
33 "Erik Walthinsen <omega@cse.ogi.edu>\n"
34 "Wim Taymans <wim.taymans@tvd.be>",
38 static GstCaps* avi_typefind (GstBuffer *buf, gpointer private);
40 /* typefactory for 'avi' */
41 static GstTypeDefinition avidefinition = {
48 /* AviDemux signals and args */
62 GST_PADTEMPLATE_FACTORY (sink_templ,
69 "RIFF", GST_PROPS_STRING ("AVI")
73 GST_PADTEMPLATE_FACTORY (src_video_templ,
80 "format", GST_PROPS_LIST (
81 GST_PROPS_STRING ("strf_vids"),
82 GST_PROPS_STRING ("strf_iavs")
87 GST_PADTEMPLATE_FACTORY (src_audio_templ,
94 "format", GST_PROPS_STRING ("strf_auds")
98 static void gst_avi_demux_class_init (GstAviDemuxClass *klass);
99 static void gst_avi_demux_init (GstAviDemux *avi_demux);
101 static void gst_avi_demux_loop (GstElement *element);
103 static GstElementStateReturn
104 gst_avi_demux_change_state (GstElement *element);
106 static void gst_avi_demux_get_property (GObject *object, guint prop_id,
107 GValue *value, GParamSpec *pspec);
110 static GstElementClass *parent_class = NULL;
111 /*static guint gst_avi_demux_signals[LAST_SIGNAL] = { 0 }; */
114 gst_avi_demux_get_type(void)
116 static GType avi_demux_type = 0;
118 if (!avi_demux_type) {
119 static const GTypeInfo avi_demux_info = {
120 sizeof(GstAviDemuxClass),
123 (GClassInitFunc)gst_avi_demux_class_init,
128 (GInstanceInitFunc)gst_avi_demux_init,
130 avi_demux_type = g_type_register_static(GST_TYPE_ELEMENT, "GstAviDemux", &avi_demux_info, 0);
132 return avi_demux_type;
136 gst_avi_demux_class_init (GstAviDemuxClass *klass)
138 GObjectClass *gobject_class;
139 GstElementClass *gstelement_class;
141 gobject_class = (GObjectClass*)klass;
142 gstelement_class = (GstElementClass*)klass;
144 g_object_class_install_property (G_OBJECT_CLASS(klass), ARG_BITRATE,
145 g_param_spec_long ("bitrate","bitrate","bitrate",
146 G_MINLONG, G_MAXLONG, 0, G_PARAM_READABLE)); /* CHECKME */
147 g_object_class_install_property (G_OBJECT_CLASS(klass), ARG_MEDIA_TIME,
148 g_param_spec_long ("media_time","media_time","media_time",
149 G_MINLONG, G_MAXLONG, 0, G_PARAM_READABLE)); /* CHECKME */
150 g_object_class_install_property (G_OBJECT_CLASS(klass), ARG_CURRENT_TIME,
151 g_param_spec_long ("current_time","current_time","current_time",
152 G_MINLONG, G_MAXLONG, 0, G_PARAM_READABLE)); /* CHECKME */
154 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
156 gobject_class->get_property = gst_avi_demux_get_property;
158 gstelement_class->change_state = gst_avi_demux_change_state;
162 gst_avi_demux_init (GstAviDemux *avi_demux)
166 GST_FLAG_SET (avi_demux, GST_ELEMENT_EVENT_AWARE);
168 avi_demux->sinkpad = gst_pad_new_from_template (
169 GST_PADTEMPLATE_GET (sink_templ), "sink");
170 gst_element_add_pad (GST_ELEMENT (avi_demux), avi_demux->sinkpad);
172 gst_element_set_loop_function (GST_ELEMENT (avi_demux), gst_avi_demux_loop);
174 avi_demux->state = GST_AVI_DEMUX_UNKNOWN;
175 avi_demux->num_audio_pads = 0;
176 avi_demux->num_video_pads = 0;
177 avi_demux->next_time = 0;
178 avi_demux->flags = 0;
179 avi_demux->index_entries = NULL;
180 avi_demux->index_size = 0;
181 avi_demux->resync_offset = 0;
183 /*GST_FLAG_SET( GST_OBJECT (avi_demux), GST_ELEMENT_NO_SEEK); */
185 for(i=0; i<GST_AVI_DEMUX_MAX_AUDIO_PADS; i++)
186 avi_demux->audio_pad[i] = NULL;
188 for(i=0; i<GST_AVI_DEMUX_MAX_VIDEO_PADS; i++)
189 avi_demux->video_pad[i] = NULL;
194 avi_typefind (GstBuffer *buf,
197 gchar *data = GST_BUFFER_DATA (buf);
200 GST_DEBUG (0,"avi_demux: typefind\n");
202 if (GUINT32_FROM_LE (((guint32 *)data)[0]) != GST_RIFF_TAG_RIFF)
204 if (GUINT32_FROM_LE (((guint32 *)data)[2]) != GST_RIFF_RIFF_AVI)
207 new = GST_CAPS_NEW ("avi_typefind",
209 "RIFF", GST_PROPS_STRING ("AVI"));
215 gst_avi_demux_avih (GstAviDemux *avi_demux)
218 GstByteStream *bs = avi_demux->bs;
220 avih = (gst_riff_avih *) gst_bytestream_peek_bytes (bs, sizeof (gst_riff_avih));
222 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: avih tag found");
223 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: us_frame %d", GUINT32_FROM_LE (avih->us_frame));
224 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: max_bps %d", GUINT32_FROM_LE (avih->max_bps));
225 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: pad_gran %d", GUINT32_FROM_LE (avih->pad_gran));
226 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: flags 0x%08x", GUINT32_FROM_LE (avih->flags));
227 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: tot_frames %d", GUINT32_FROM_LE (avih->tot_frames));
228 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: init_frames %d", GUINT32_FROM_LE (avih->init_frames));
229 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: streams %d", GUINT32_FROM_LE (avih->streams));
230 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: bufsize %d", GUINT32_FROM_LE (avih->bufsize));
231 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: width %d", GUINT32_FROM_LE (avih->width));
232 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: height %d", GUINT32_FROM_LE (avih->height));
233 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: scale %d", GUINT32_FROM_LE (avih->scale));
234 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: rate %d", GUINT32_FROM_LE (avih->rate));
235 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: start %d", GUINT32_FROM_LE (avih->start));
236 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: length %d", GUINT32_FROM_LE (avih->length));
238 avi_demux->time_interval = GUINT32_FROM_LE (avih->us_frame);
239 avi_demux->tot_frames = GUINT32_FROM_LE (avih->tot_frames);
240 avi_demux->flags = GUINT32_FROM_LE (avih->flags);
248 gst_avi_demux_strh (GstAviDemux *avi_demux)
251 GstByteStream *bs = avi_demux->bs;
253 strh = (gst_riff_strh *) gst_bytestream_peek_bytes (bs, sizeof (gst_riff_strh));
255 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: strh tag found");
256 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: type 0x%08x (%s)",
257 GUINT32_FROM_LE (strh->type), gst_riff_id_to_fourcc (strh->type));
258 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: fcc_handler 0x%08x (%s)",
259 GUINT32_FROM_LE (strh->fcc_handler), gst_riff_id_to_fourcc (strh->fcc_handler));
260 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: flags 0x%08x", GUINT32_FROM_LE (strh->flags));
261 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: priority %d", GUINT32_FROM_LE (strh->priority));
262 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: init_frames %d", GUINT32_FROM_LE (strh->init_frames));
263 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: scale %d", GUINT32_FROM_LE (strh->scale));
264 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: rate %d", GUINT32_FROM_LE (strh->rate));
265 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: start %d", GUINT32_FROM_LE (strh->start));
266 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: length %d", GUINT32_FROM_LE (strh->length));
267 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: bufsize %d", GUINT32_FROM_LE (strh->bufsize));
268 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: quality %d", GUINT32_FROM_LE (strh->quality));
269 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: samplesize %d", GUINT32_FROM_LE (strh->samplesize));
271 avi_demux->fcc_type = GUINT32_FROM_LE (strh->type);
279 gst_avi_demux_strf_vids (GstAviDemux *avi_demux)
281 gst_riff_strf_vids *strf;
283 GstByteStream *bs = avi_demux->bs;
285 strf = (gst_riff_strf_vids *) gst_bytestream_peek_bytes (bs, sizeof (gst_riff_strf_vids));
287 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: strf tag found in context vids");
288 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: size %d", GUINT32_FROM_LE (strf->size));
289 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: width %d", GUINT32_FROM_LE (strf->width));
290 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: height %d", GUINT32_FROM_LE (strf->height));
291 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: planes %d", GUINT16_FROM_LE (strf->planes));
292 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: bit_cnt %d", GUINT16_FROM_LE (strf->bit_cnt));
293 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: compression 0x%08x (%s)",
294 GUINT32_FROM_LE (strf->compression), gst_riff_id_to_fourcc (strf->compression));
295 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: image_size %d", GUINT32_FROM_LE (strf->image_size));
296 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: xpels_meter %d", GUINT32_FROM_LE (strf->xpels_meter));
297 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: ypels_meter %d", GUINT32_FROM_LE (strf->ypels_meter));
298 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: num_colors %d", GUINT32_FROM_LE (strf->num_colors));
299 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: imp_colors %d", GUINT32_FROM_LE (strf->imp_colors));
301 srcpad = gst_pad_new_from_template (
302 GST_PADTEMPLATE_GET (src_video_templ), g_strdup_printf ("video_%02d",
303 avi_demux->num_video_pads));
305 gst_pad_try_set_caps (srcpad,
309 "format", GST_PROPS_STRING ("strf_vids"),
310 "size", GST_PROPS_INT (GUINT32_FROM_LE (strf->size)),
311 "width", GST_PROPS_INT (GUINT32_FROM_LE (strf->width)),
312 "height", GST_PROPS_INT (GUINT32_FROM_LE (strf->height)),
313 "planes", GST_PROPS_INT (GUINT16_FROM_LE (strf->planes)),
314 "bit_cnt", GST_PROPS_INT (GUINT16_FROM_LE (strf->bit_cnt)),
315 "compression", GST_PROPS_FOURCC (GUINT32_FROM_LE (strf->compression)),
316 "image_size", GST_PROPS_INT (GUINT32_FROM_LE (strf->image_size)),
317 "xpels_meter", GST_PROPS_INT (GUINT32_FROM_LE (strf->xpels_meter)),
318 "ypels_meter", GST_PROPS_INT (GUINT32_FROM_LE (strf->ypels_meter)),
319 "num_colors", GST_PROPS_INT (GUINT32_FROM_LE (strf->num_colors)),
320 "imp_colors", GST_PROPS_INT (GUINT32_FROM_LE (strf->imp_colors))
323 avi_demux->video_pad[avi_demux->num_video_pads++] = srcpad;
324 gst_element_add_pad (GST_ELEMENT (avi_demux), srcpad);
328 gst_avi_demux_strf_auds (GstAviDemux *avi_demux)
330 gst_riff_strf_auds *strf;
332 GstByteStream *bs = avi_demux->bs;
334 strf = (gst_riff_strf_auds *) gst_bytestream_peek_bytes (bs, sizeof (gst_riff_strf_auds));
336 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: strf tag found in context auds");
337 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: format %d", GUINT16_FROM_LE (strf->format));
338 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: channels %d", GUINT16_FROM_LE (strf->channels));
339 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: rate %d", GUINT32_FROM_LE (strf->rate));
340 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: av_bps %d", GUINT32_FROM_LE (strf->av_bps));
341 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: blockalign %d", GUINT16_FROM_LE (strf->blockalign));
342 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: size %d", GUINT16_FROM_LE (strf->size));
344 srcpad = gst_pad_new_from_template (
345 GST_PADTEMPLATE_GET (src_audio_templ), g_strdup_printf ("audio_%02d",
346 avi_demux->num_audio_pads));
348 gst_pad_try_set_caps (srcpad,
352 "format", GST_PROPS_STRING ("strf_auds"),
353 "fmt", GST_PROPS_INT (GUINT16_FROM_LE (strf->format)),
354 "channels", GST_PROPS_INT (GUINT16_FROM_LE (strf->channels)),
355 "rate", GST_PROPS_INT (GUINT32_FROM_LE (strf->rate)),
356 "av_bps", GST_PROPS_INT (GUINT32_FROM_LE (strf->av_bps)),
357 "blockalign", GST_PROPS_INT (GUINT16_FROM_LE (strf->blockalign)),
358 "size", GST_PROPS_INT (GUINT16_FROM_LE (strf->size))
361 avi_demux->audio_pad[avi_demux->num_audio_pads++] = srcpad;
362 gst_element_add_pad (GST_ELEMENT (avi_demux), srcpad);
366 gst_avi_demux_strf_iavs (GstAviDemux *avi_demux)
368 gst_riff_strf_iavs *strf;
370 GstByteStream *bs = avi_demux->bs;
372 strf = (gst_riff_strf_iavs *) gst_bytestream_peek_bytes (bs, sizeof (gst_riff_strf_iavs));
374 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: strf tag found in context iavs");
375 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: DVAAuxSrc %08x", GUINT32_FROM_LE (strf->DVAAuxSrc));
376 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: DVAAuxCtl %08x", GUINT32_FROM_LE (strf->DVAAuxCtl));
377 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: DVAAuxSrc1 %08x", GUINT32_FROM_LE (strf->DVAAuxSrc1));
378 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: DVAAuxCtl1 %08x", GUINT32_FROM_LE (strf->DVAAuxCtl1));
379 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: DVVAuxSrc %08x", GUINT32_FROM_LE (strf->DVVAuxSrc));
380 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: DVVAuxCtl %08x", GUINT32_FROM_LE (strf->DVVAuxCtl));
381 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: DVReserved1 %08x", GUINT32_FROM_LE (strf->DVReserved1));
382 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux: DVReserved2 %08x", GUINT32_FROM_LE (strf->DVReserved2));
384 srcpad = gst_pad_new_from_template (
385 GST_PADTEMPLATE_GET (src_video_templ), g_strdup_printf ("video_%02d",
386 avi_demux->num_video_pads));
388 gst_pad_try_set_caps (srcpad,
392 "format", GST_PROPS_STRING ("strf_iavs"),
393 "DVAAuxSrc", GST_PROPS_INT (GUINT32_FROM_LE (strf->DVAAuxSrc)),
394 "DVAAuxCtl", GST_PROPS_INT (GUINT32_FROM_LE (strf->DVAAuxCtl)),
395 "DVAAuxSrc1", GST_PROPS_INT (GUINT32_FROM_LE (strf->DVAAuxSrc1)),
396 "DVAAuxCtl1", GST_PROPS_INT (GUINT32_FROM_LE (strf->DVAAuxCtl1)),
397 "DVVAuxSrc", GST_PROPS_INT (GUINT32_FROM_LE (strf->DVVAuxSrc)),
398 "DVVAuxCtl", GST_PROPS_INT (GUINT32_FROM_LE (strf->DVVAuxCtl)),
399 "DVReserved1", GST_PROPS_INT (GUINT32_FROM_LE (strf->DVReserved1)),
400 "DVReserved2", GST_PROPS_INT (GUINT32_FROM_LE (strf->DVReserved2))
403 avi_demux->video_pad[avi_demux->num_video_pads++] = srcpad;
404 gst_element_add_pad (GST_ELEMENT (avi_demux), srcpad);
408 gst_avidemux_parse_index (GstAviDemux *avi_demux,
414 buf = gst_pad_pullregion (avi_demux->sinkpad, GST_REGION_OFFSET_LEN, offset, 8);
416 if (!buf || GST_BUFFER_OFFSET (buf) != offset || GST_BUFFER_SIZE (buf) != 8) {
417 GST_INFO (GST_CAT_PLUGIN_INFO, "avidemux: could not get index");
421 if (gst_riff_fourcc_to_id (GST_BUFFER_DATA (buf)) != GST_RIFF_TAG_idx1) {
422 GST_INFO (GST_CAT_PLUGIN_INFO, "avidemux: no index found");
426 index_size = GUINT32_FROM_LE(*(guint32 *)(GST_BUFFER_DATA (buf) + 4));
428 buf = gst_pad_pullregion(avi_demux->sinkpad, GST_REGION_OFFSET_LEN, offset+8, index_size);
430 avi_demux->index_size = index_size/sizeof(gst_riff_index_entry);
432 GST_INFO (GST_CAT_PLUGIN_INFO, "avidemux: index size %lu", avi_demux->index_size);
434 avi_demux->index_entries = g_malloc (GST_BUFFER_SIZE (buf));
435 memcpy (avi_demux->index_entries, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
437 buf = gst_pad_pullregion(avi_demux->sinkpad, GST_REGION_OFFSET_LEN, avi_demux->index_offset, 0);
441 gst_avidemux_forall_pads (GstAviDemux *avi_demux, GFunc func, gpointer user_data)
446 for(i=0; i<GST_AVI_DEMUX_MAX_AUDIO_PADS; i++) {
447 pad = avi_demux->audio_pad[i];
448 if (pad && GST_PAD_IS_CONNECTED (pad)) {
449 (*func) (pad, user_data);
453 for(i=0; i<GST_AVI_DEMUX_MAX_VIDEO_PADS; i++) {
454 pad = avi_demux->video_pad[i];
455 if (pad && GST_PAD_IS_CONNECTED (pad)) {
456 (*func) (pad, user_data);
462 gst_avidemux_handle_event (GstAviDemux *avi_demux)
468 gst_bytestream_get_status (avi_demux->bs, &remaining, &event);
470 type = event? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
474 gst_pad_event_default (avi_demux->sinkpad, event);
477 g_warning ("seek event\n");
479 case GST_EVENT_FLUSH:
480 g_warning ("flush event\n");
482 case GST_EVENT_DISCONTINUOUS:
483 g_warning ("discont event\n");
486 g_warning ("unhandled event %d\n", type);
493 static inline gboolean
494 gst_avidemux_read_chunk (GstAviDemux *avi_demux, guint32 *id, guint32 *size)
496 gst_riff_chunk *chunk;
497 GstByteStream *bs = avi_demux->bs;
499 chunk = (gst_riff_chunk *) gst_bytestream_peek_bytes (bs, sizeof (gst_riff_chunk));
501 *id = GUINT32_FROM_LE (chunk->id);
502 *size = GUINT32_FROM_LE (chunk->size);
504 gst_bytestream_flush (bs, sizeof (gst_riff_chunk));
508 return gst_avidemux_handle_event (avi_demux);
512 gst_avidemux_process_chunk (GstAviDemux *avi_demux, guint64 *filepos,
514 gint rec_depth, guint32 *chunksize)
517 GstByteStream *bs = avi_demux->bs;
519 if (!gst_avidemux_read_chunk (avi_demux, &chunkid, chunksize)) {
520 g_print (" ***** Error reading chunk at filepos 0x%08llx\n", *filepos);
523 if (desired_tag) { /* do we have to test identity? */
524 if (desired_tag != chunkid) {
525 g_print ("\n\n *** Error: Expected chunk '%08x', found '%08x'\n",
526 desired_tag, chunkid);
531 GST_INFO (GST_CAT_PLUGIN_INFO, "chunkid %s, size %08x, filepos %08llx",
532 gst_riff_id_to_fourcc (chunkid), *chunksize, *filepos);
534 *filepos += (sizeof (guint32) + sizeof (guint32));
537 case GST_RIFF_TAG_RIFF:
538 case GST_RIFF_TAG_LIST:
541 guint32 subchunksize = 0; /* size of a read subchunk */
543 /* flush the form type */
544 if (!gst_bytestream_flush (bs, sizeof (guint32)))
547 datashowed = sizeof (guint32); /* we showed the form type */
548 *filepos += datashowed; /* for the rest of the routine */
550 while (datashowed < *chunksize) { /* while not showed all: */
552 /* recurse for subchunks of RIFF and LIST chunks: */
553 if (!gst_avidemux_process_chunk (avi_demux, filepos, 0,
554 rec_depth + 1, &subchunksize))
557 subchunksize = ((subchunksize + 1) & ~1);
559 datashowed += (sizeof (guint32) + sizeof (guint32) + subchunksize);
560 *filepos += subchunksize;
562 if (datashowed != *chunksize) {
563 g_warning ("error parsing AVI");
567 case GST_RIFF_TAG_avih:
568 gst_avi_demux_avih (avi_demux);
570 case GST_RIFF_TAG_strh:
572 gst_avi_demux_strh (avi_demux);
575 case GST_RIFF_TAG_strf:
576 switch (avi_demux->fcc_type) {
577 case GST_RIFF_FCC_vids:
578 gst_avi_demux_strf_vids (avi_demux);
580 case GST_RIFF_FCC_auds:
581 gst_avi_demux_strf_auds (avi_demux);
583 case GST_RIFF_FCC_iavs:
584 gst_avi_demux_strf_iavs (avi_demux);
586 case GST_RIFF_FCC_pads:
587 case GST_RIFF_FCC_txts:
589 GST_INFO (GST_CAT_PLUGIN_INFO, "gst_avi_demux_chain: strh type %s not supported", gst_riff_id_to_fourcc (avi_demux->fcc_type));
597 GST_DEBUG (0,"gst_avi_demux_chain: tag found %08x size %08x\n",
598 chunkid, *chunksize);
600 if (GST_PAD_IS_CONNECTED (avi_demux->video_pad[0])) {
604 buf = gst_bytestream_peek (bs, *chunksize);
607 buf = gst_buffer_new ();
609 GST_BUFFER_TIMESTAMP (buf) = avi_demux->next_time;
610 avi_demux->next_time += avi_demux->time_interval;
612 if (avi_demux->video_need_flush[0]) {
613 /* FIXME, do some flush event here */
614 avi_demux->video_need_flush[0] = FALSE;
617 GST_DEBUG (0,"gst_avi_demux_chain: send video buffer %08x\n", *chunksize);
618 gst_pad_push(avi_demux->video_pad[0], buf);
619 GST_DEBUG (0,"gst_avi_demux_chain: sent video buffer %08x %p\n",
620 *chunksize, &avi_demux->video_pad[0]);
621 avi_demux->current_frame++;
623 *chunksize = (*chunksize + 1) & ~1;
628 GST_DEBUG (0,"gst_avi_demux_chain: tag found %08x size %08x\n",
629 chunkid, *chunksize);
631 if (GST_PAD_IS_CONNECTED (avi_demux->audio_pad[0])) {
635 buf = gst_bytestream_peek (bs, *chunksize);
638 buf = gst_buffer_new ();
641 if (avi_demux->audio_need_flush[0]) {
642 GST_DEBUG (0,"audio flush\n");
643 avi_demux->audio_need_flush[0] = FALSE;
644 /* FIXME, do some flush event here */
647 GST_DEBUG (0,"gst_avi_demux_chain: send audio buffer %08x\n", *chunksize);
648 gst_pad_push (avi_demux->audio_pad[0], buf);
649 GST_DEBUG (0,"gst_avi_demux_chain: sent audio buffer %08x\n", *chunksize);
651 *chunksize = (*chunksize + 1) & ~1;
655 GST_DEBUG (0, " ***** unknown chunkid %08x (%s)\n", chunkid, gst_riff_id_to_fourcc (chunkid));
656 *chunksize = (*chunksize + 1) & ~1;
659 GST_INFO (GST_CAT_PLUGIN_INFO, "chunkid %s, flush %08x, filepos %08llx",
660 gst_riff_id_to_fourcc (chunkid), *chunksize, *filepos);
662 if (!gst_bytestream_flush (bs, *chunksize)) {
663 return gst_avidemux_handle_event (avi_demux);
667 /* we are running in an infinite loop, we need to _yield
668 * from time to time */
669 gst_element_yield (GST_ELEMENT (avi_demux));
675 gst_avi_demux_loop (GstElement *element)
677 GstAviDemux *avi_demux;
681 g_return_if_fail (element != NULL);
682 g_return_if_fail (GST_IS_AVI_DEMUX (element));
684 avi_demux = GST_AVI_DEMUX (element);
686 /* this is basically an infinite loop */
687 if (!gst_avidemux_process_chunk (avi_demux, &filepos, GST_RIFF_TAG_RIFF, 0, &chunksize)) {
688 gst_element_error (element, "This doesn't appear to be an AVI file");
692 static GstElementStateReturn
693 gst_avi_demux_change_state (GstElement *element)
695 GstAviDemux *avi_demux = GST_AVI_DEMUX (element);
697 switch (GST_STATE_TRANSITION (element)) {
698 case GST_STATE_NULL_TO_READY:
700 case GST_STATE_READY_TO_PAUSED:
701 avi_demux->bs = gst_bytestream_new (avi_demux->sinkpad);
703 case GST_STATE_PAUSED_TO_PLAYING:
705 case GST_STATE_PLAYING_TO_PAUSED:
707 case GST_STATE_PAUSED_TO_READY:
708 gst_bytestream_destroy (avi_demux->bs);
710 case GST_STATE_READY_TO_NULL:
716 parent_class->change_state (element);
718 return GST_STATE_SUCCESS;
722 gst_avi_demux_get_property (GObject *object, guint prop_id, GValue *value,
727 g_return_if_fail (GST_IS_AVI_DEMUX (object));
729 src = GST_AVI_DEMUX (object);
735 g_value_set_long (value, (src->tot_frames * src->time_interval) / 1000000);
737 case ARG_CURRENT_TIME:
738 g_value_set_long (value, (src->current_frame * src->time_interval) / 1000000);
746 plugin_init (GModule *module, GstPlugin *plugin)
748 GstElementFactory *factory;
749 GstTypeFactory *type;
751 /* this filter needs the riff parser */
752 if (!gst_library_load ("gstbytestream")) {
753 gst_info("avidemux: could not load support library: 'gstbytestream'\n");
756 if (!gst_library_load ("gstriff")) {
757 gst_info("avidemux: could not load support library: 'gstriff'\n");
761 /* create an elementfactory for the avi_demux element */
762 factory = gst_elementfactory_new ("avidemux",GST_TYPE_AVI_DEMUX,
763 &gst_avi_demux_details);
764 g_return_val_if_fail (factory != NULL, FALSE);
766 gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (src_audio_templ));
767 gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (src_video_templ));
768 gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (sink_templ));
770 type = gst_typefactory_new (&avidefinition);
771 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (type));
773 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
778 GstPluginDesc plugin_desc = {