2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * <2002> Wim Taymans <wim.taymans@chello.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.
24 #include "gstcdxaparse.h"
26 #define MAKE_FOUR_CC(a,b,c,d) ( ((guint32)a) | (((guint32)b)<< 8) | \
27 ((guint32)c)<<16 | (((guint32)d)<<24) )
31 #define GST_RIFF_TAG_RIFF MAKE_FOUR_CC('R','I','F','F')
32 #define GST_RIFF_RIFF_CDXA MAKE_FOUR_CC('C','D','X','A')
35 #define GST_RIFF_TAG_fmt MAKE_FOUR_CC('f','m','t',' ')
36 #define GST_RIFF_TAG_data MAKE_FOUR_CC('d','a','t','a')
39 /* elementfactory information */
40 static GstElementDetails gst_cdxa_parse_details = {
43 "Parse a .dat file (VCD) into raw mpeg1",
45 "Wim Taymans <wim.taymans@tvd.be>",
49 static GstCaps* cdxa_typefind (GstBuffer *buf, gpointer private);
51 /* typefactory for 'cdxa' */
52 static GstTypeDefinition cdxadefinition = {
53 "cdxaparse_video/avi",
59 /* CDXAParse signals and args */
70 GST_PADTEMPLATE_FACTORY (sink_templ,
77 "format", GST_PROPS_STRING ("CDXA")
81 GST_PADTEMPLATE_FACTORY (src_templ,
88 "mpegversion", GST_PROPS_INT (1),
89 "systemstream", GST_PROPS_BOOLEAN (TRUE)
93 static void gst_cdxa_parse_class_init (GstCDXAParseClass *klass);
94 static void gst_cdxa_parse_init (GstCDXAParse *cdxa_parse);
96 static void gst_cdxa_parse_loop (GstElement *element);
98 static GstElementStateReturn
99 gst_cdxa_parse_change_state (GstElement *element);
102 static GstElementClass *parent_class = NULL;
103 /*static guint gst_cdxa_parse_signals[LAST_SIGNAL] = { 0 }; */
106 gst_cdxa_parse_get_type(void)
108 static GType cdxa_parse_type = 0;
110 if (!cdxa_parse_type) {
111 static const GTypeInfo cdxa_parse_info = {
112 sizeof(GstCDXAParseClass),
115 (GClassInitFunc)gst_cdxa_parse_class_init,
118 sizeof(GstCDXAParse),
120 (GInstanceInitFunc)gst_cdxa_parse_init,
122 cdxa_parse_type = g_type_register_static(GST_TYPE_ELEMENT, "GstCDXAParse", &cdxa_parse_info, 0);
124 return cdxa_parse_type;
128 gst_cdxa_parse_class_init (GstCDXAParseClass *klass)
130 GObjectClass *gobject_class;
131 GstElementClass *gstelement_class;
133 gobject_class = (GObjectClass*)klass;
134 gstelement_class = (GstElementClass*)klass;
136 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
138 gstelement_class->change_state = gst_cdxa_parse_change_state;
142 gst_cdxa_parse_init (GstCDXAParse *cdxa_parse)
146 GST_FLAG_SET (cdxa_parse, GST_ELEMENT_EVENT_AWARE);
148 cdxa_parse->sinkpad = gst_pad_new_from_template (
149 GST_PADTEMPLATE_GET (sink_templ), "sink");
150 gst_element_add_pad (GST_ELEMENT (cdxa_parse), cdxa_parse->sinkpad);
152 cdxa_parse->srcpad = gst_pad_new_from_template (
153 GST_PADTEMPLATE_GET (src_templ), "src");
154 gst_element_add_pad (GST_ELEMENT (cdxa_parse), cdxa_parse->srcpad);
156 gst_element_set_loop_function (GST_ELEMENT (cdxa_parse), gst_cdxa_parse_loop);
161 cdxa_typefind (GstBuffer *buf,
164 gchar *data = GST_BUFFER_DATA (buf);
167 GST_DEBUG (0,"cdxa_parse: typefind");
169 if (GUINT32_FROM_LE (((guint32 *)data)[0]) != GST_RIFF_TAG_RIFF)
171 if (GUINT32_FROM_LE (((guint32 *)data)[2]) != GST_RIFF_RIFF_CDXA)
174 new = GST_CAPS_NEW ("cdxa_typefind",
176 "RIFF", GST_PROPS_STRING ("CDXA"));
182 gst_cdxa_parse_handle_event (GstCDXAParse *cdxa_parse)
188 gst_bytestream_get_status (cdxa_parse->bs, &remaining, &event);
190 type = event? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
194 gst_pad_event_default (cdxa_parse->sinkpad, event);
197 g_warning ("seek event\n");
199 case GST_EVENT_FLUSH:
200 g_warning ("flush event\n");
202 case GST_EVENT_DISCONTINUOUS:
203 g_warning ("discont event\n");
206 g_warning ("unhandled event %d\n", type);
215 CDXA starts with the following header:
217 ! RIFF:4 ! size:4 ! "CDXA" ! "fmt " ! size:4 ! (size+1)&~1 bytes of crap !
218 ! "data" ! data_size:4 ! (data_size/2352) sectors...
232 A sectors is 2352 bytes long and is composed of:
234 ! sync ! header ! subheader ! data ... ! edc !
235 ! 12 bytes ! 4 bytes ! 8 bytes ! 2324 bytes ! 4 bytes !
236 !-------------------------------------------------------!
238 We parse the data out of it and send it to the srcpad.
242 gst_cdxa_parse_loop (GstElement *element)
244 GstCDXAParse *cdxa_parse;
245 CDXAParseHeader *header;
247 g_return_if_fail (element != NULL);
248 g_return_if_fail (GST_IS_CDXA_PARSE (element));
250 cdxa_parse = GST_CDXA_PARSE (element);
252 if (cdxa_parse->state == CDXA_PARSE_HEADER) {
256 header = (CDXAParseHeader *) gst_bytestream_peek_bytes (cdxa_parse->bs, 20);
260 cdxa_parse->riff_size = GUINT32_FROM_LE (header->riff_size);
261 fmt_size = (GUINT32_FROM_LE (header->fmt_size) + 1)&~1;
263 /* flush the header + fmt_size bytes + 4 bytes "data" */
264 if (!gst_bytestream_flush (cdxa_parse->bs, 20 + fmt_size + 4))
267 /* get the data size */
268 buf = gst_bytestream_peek_bytes (cdxa_parse->bs, 4);
271 cdxa_parse->data_size = GUINT32_FROM_LE (*((guint32 *)buf));
273 /* flush the data size */
274 if (!gst_bytestream_flush (cdxa_parse->bs, 4))
277 if (cdxa_parse->data_size % CDXA_SECTOR_SIZE)
278 g_warning ("cdxa_parse: size not multiple of %d bytes", CDXA_SECTOR_SIZE);
280 cdxa_parse->sectors = cdxa_parse->data_size / CDXA_SECTOR_SIZE;
282 cdxa_parse->state = CDXA_PARSE_DATA;
288 buf = gst_bytestream_read (cdxa_parse->bs, CDXA_SECTOR_SIZE);
290 gst_cdxa_parse_handle_event (cdxa_parse);
294 outbuf = gst_buffer_create_sub (buf, 24, CDXA_DATA_SIZE);
295 gst_buffer_unref (buf);
297 gst_pad_push (cdxa_parse->srcpad, outbuf);
301 static GstElementStateReturn
302 gst_cdxa_parse_change_state (GstElement *element)
304 GstCDXAParse *cdxa_parse = GST_CDXA_PARSE (element);
306 switch (GST_STATE_TRANSITION (element)) {
307 case GST_STATE_NULL_TO_READY:
309 case GST_STATE_READY_TO_PAUSED:
310 cdxa_parse->state = CDXA_PARSE_HEADER;
311 cdxa_parse->bs = gst_bytestream_new (cdxa_parse->sinkpad);
313 case GST_STATE_PAUSED_TO_PLAYING:
315 case GST_STATE_PLAYING_TO_PAUSED:
317 case GST_STATE_PAUSED_TO_READY:
318 gst_bytestream_destroy (cdxa_parse->bs);
320 case GST_STATE_READY_TO_NULL:
326 parent_class->change_state (element);
328 return GST_STATE_SUCCESS;
332 plugin_init (GModule *module, GstPlugin *plugin)
334 GstElementFactory *factory;
335 GstTypeFactory *type;
337 /* this filter needs the riff parser */
338 if (!gst_library_load ("gstbytestream")) {
339 gst_info("cdxaparse: could not load support library: 'gstbytestream'\n");
343 /* create an elementfactory for the cdxa_parse element */
344 factory = gst_elementfactory_new ("cdxaparse", GST_TYPE_CDXA_PARSE,
345 &gst_cdxa_parse_details);
346 g_return_val_if_fail (factory != NULL, FALSE);
348 gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (src_templ));
349 gst_elementfactory_add_padtemplate (factory, GST_PADTEMPLATE_GET (sink_templ));
351 type = gst_typefactory_new (&cdxadefinition);
352 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (type));
354 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
359 GstPluginDesc plugin_desc = {