2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * <2000> Daniel Fischer <dan@f3c.com>
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.
23 #include "gstdv1394src.h"
25 #define N_BUFFERS_IN_POOL 3
27 #define PAL_FRAMESIZE 144000
28 #define NTSC_FRAMESIZE 120000
30 /* Filter signals and args */
43 static GstElementDetails gst_dv1394src_details = {
44 "Firewire (1394) DV Source",
46 "Source for DV video data from firewire port",
47 "Erik Walthinsen <omega@temple-baptist.com>\n"
48 "Daniel Fischer <dan@f3c.com>",
52 static GstPadTemplate*
53 gst_dv1394src_factory (void)
55 static GstPadTemplate *template = NULL;
58 template = gst_pad_template_new (
67 "format", GST_PROPS_LIST (
68 GST_PROPS_STRING ("NTSC"),
69 GST_PROPS_STRING ("PAL")
80 static void gst_dv1394src_base_init (gpointer g_class);
81 static void gst_dv1394src_class_init (GstDV1394SrcClass *klass);
82 static void gst_dv1394src_init (GstDV1394Src *filter);
84 static void gst_dv1394src_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
85 static void gst_dv1394src_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
87 static GstElementStateReturn gst_dv1394src_change_state (GstElement *element);
89 static GstData * gst_dv1394src_get (GstPad *pad);
91 static GstElementClass *parent_class = NULL;
92 /*static guint gst_filter_signals[LAST_SIGNAL] = { 0 }; */
95 gst_dv1394src_get_type(void) {
96 static GType gst_dv1394src_type = 0;
98 if (!gst_dv1394src_type) {
99 static const GTypeInfo gst_dv1394src_info = {
100 sizeof(GstDV1394Src),
101 gst_dv1394src_base_init,
103 (GClassInitFunc)gst_dv1394src_class_init,
106 sizeof(GstDV1394Src),
108 (GInstanceInitFunc)gst_dv1394src_init,
110 gst_dv1394src_type = g_type_register_static(GST_TYPE_ELEMENT, "DV1394Src", &gst_dv1394src_info, 0);
112 return gst_dv1394src_type;
116 gst_dv1394src_base_init (gpointer g_class)
118 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
120 gst_element_class_set_details (element_class, &gst_dv1394src_details);
124 gst_dv1394src_class_init (GstDV1394SrcClass *klass)
126 GObjectClass *gobject_class;
127 GstElementClass *gstelement_class;
129 gobject_class = (GObjectClass*)klass;
130 gstelement_class = (GstElementClass*)klass;
132 g_object_class_install_property( G_OBJECT_CLASS(klass), ARG_CONSECUTIVE,
133 g_param_spec_int("consecutive","consecutive frames","send n consecutive frames after skipping",
134 1, G_MAXINT,1,G_PARAM_READWRITE));
135 g_object_class_install_property( G_OBJECT_CLASS(klass), ARG_SKIP,
136 g_param_spec_int("skip","skip frames","skip n frames",
137 0, G_MAXINT,1,G_PARAM_READWRITE));
138 g_object_class_install_property( G_OBJECT_CLASS(klass), ARG_DROP_INCOMPLETE,
139 g_param_spec_boolean("drop_incomplete","drop_incomplete","drop incomplete frames",
140 TRUE, G_PARAM_READWRITE));
142 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
144 gobject_class->set_property = gst_dv1394src_set_property;
145 gobject_class->get_property = gst_dv1394src_get_property;
147 gstelement_class->change_state = gst_dv1394src_change_state;
151 gst_dv1394src_init (GstDV1394Src *dv1394src)
153 dv1394src->srcpad = gst_pad_new ("src", GST_PAD_SRC);
154 gst_pad_set_get_function (dv1394src->srcpad, gst_dv1394src_get);
155 gst_element_add_pad (GST_ELEMENT (dv1394src), dv1394src->srcpad);
159 dv1394src->channel = 63;
161 dv1394src->consecutive = 1;
163 dv1394src->drop_incomplete = TRUE;
165 /* initialized when first header received */
166 dv1394src->frameSize=0;
167 dv1394src->pool = NULL;
169 dv1394src->buf = NULL;
170 dv1394src->frame = NULL;
171 dv1394src->frameSequence = 0;
175 gst_dv1394src_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
177 GstDV1394Src *filter;
179 /* it's not null if we got it, but it might not be ours */
180 g_return_if_fail(GST_IS_DV1394SRC(object));
181 filter = GST_DV1394SRC(object);
185 filter->skip = g_value_get_int(value);
187 case ARG_CONSECUTIVE:
188 filter->consecutive = g_value_get_int(value);
190 case ARG_DROP_INCOMPLETE:
191 filter->drop_incomplete = g_value_get_boolean(value);
199 gst_dv1394src_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
201 GstDV1394Src *filter;
203 /* it's not null if we got it, but it might not be ours */
204 g_return_if_fail(GST_IS_DV1394SRC(object));
205 filter = GST_DV1394SRC(object);
209 g_value_set_int( value, filter->skip );
211 case ARG_CONSECUTIVE:
212 g_value_set_int( value, filter->consecutive );
214 case ARG_DROP_INCOMPLETE:
215 g_value_set_boolean( value, filter->drop_incomplete );
218 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
224 int gst_dv1394src_iso_receive(raw1394handle_t handle,int channel,size_t len,quadlet_t *data) {
225 GstDV1394Src *dv1394src = GST_DV1394SRC (raw1394_get_userdata(handle));
229 the following code taken from kino-0.51 (Dan Dennedy/Charles Yates)
231 unsigned char *p = (unsigned char*) & data[3];
232 int section_type = p[0] >> 5; /* section type is in bits 5 - 7 */
233 int dif_sequence = p[1] >> 4; /* dif sequence number is in bits 4 - 7 */
234 int dif_block = p[2];
236 /* if we are at the beginning of a frame,
237 we set buf=frame, and get a new buffer from pool for frame
240 if (section_type == 0 && dif_sequence == 0) { // dif header
242 if( dv1394src->pool == NULL ) {
243 // figure format (NTSC/PAL)
246 dv1394src->frameSize = PAL_FRAMESIZE;
247 GST_DEBUG ("PAL data");
248 if (gst_pad_try_set_caps (dv1394src->srcpad,
249 GST_CAPS_NEW ( "dv1394src", "video/dv",
250 "format", GST_PROPS_STRING("PAL"),
253 gst_element_error (GST_ELEMENT(dv1394src), "Could not set source caps for PAL");
258 dv1394src->frameSize = NTSC_FRAMESIZE;
259 GST_DEBUG ("NTSC data [untested] - please report success/failure to <dan@f3c.com>");
260 if (gst_pad_try_set_caps (dv1394src->srcpad,
261 GST_CAPS_NEW ( "dv1394src", "video/dv",
262 "format", GST_PROPS_STRING ("NTSC"),
265 gst_element_error (GST_ELEMENT(dv1394src), "Could not set source caps for NTSC");
270 dv1394src->pool = gst_buffer_pool_get_default( dv1394src->frameSize, N_BUFFERS_IN_POOL );
271 if (dv1394src->pool == NULL) {
272 gst_element_error (GST_ELEMENT(dv1394src), "gst_buffer_pool_get_default returned NULL");
276 // drop last frame when not complete
277 if( !dv1394src->drop_incomplete || dv1394src->bytesInFrame == dv1394src->frameSize ) {
278 dv1394src->buf = dv1394src->frame;
280 GST_INFO_OBJECT (GST_ELEMENT(dv1394src), "incomplete frame dropped");
282 dv1394src->frame = NULL;
284 dv1394src->frameSequence++;
286 if( dv1394src->frameSequence % (dv1394src->skip+dv1394src->consecutive) < dv1394src->consecutive ) {
287 if( dv1394src->pool ) dv1394src->frame = gst_buffer_new_from_pool( dv1394src->pool, 0, dv1394src->frameSize );
288 dv1394src->bytesInFrame = 0;
292 if (dv1394src->frame != NULL) {
293 void *data = GST_BUFFER_DATA( dv1394src->frame );
296 switch (section_type) {
297 case 0: /* 1 Header block */
298 /* p[3] |= 0x80; // hack to force PAL data */
299 memcpy(data + dif_sequence * 150 * 80, p, 480);
302 case 1: /* 2 Subcode blocks */
303 memcpy(data + dif_sequence * 150 * 80 + (1 + dif_block) * 80, p, 480);
306 case 2: /* 3 VAUX blocks */
307 memcpy(data + dif_sequence * 150 * 80 + (3 + dif_block) * 80, p, 480);
310 case 3: /* 9 Audio blocks interleaved with video */
311 memcpy(data + dif_sequence * 150 * 80 + (6 + dif_block * 16) * 80, p, 480);
314 case 4: /* 135 Video blocks interleaved with audio */
315 memcpy(data + dif_sequence * 150 * 80 + (7 + (dif_block / 15) + dif_block) * 80, p, 480);
318 default: /* we canĀ“t handle any other data */
321 dv1394src->bytesInFrame += 480;
329 int gst_dv1394src_bus_reset(raw1394handle_t handle,
330 unsigned int generation) {
331 GST_INFO_OBJECT (GST_DV1394SRC(raw1394_get_userdata(handle)),"have bus reset");
336 gst_dv1394src_get (GstPad *pad)
338 GstDV1394Src *dv1394src = GST_DV1394SRC (GST_PAD_PARENT(pad));
340 dv1394src->buf = NULL;
341 while (dv1394src->buf == NULL)
342 raw1394_loop_iterate(dv1394src->handle);
344 return GST_DATA(dv1394src->buf);
347 static GstElementStateReturn
348 gst_dv1394src_change_state (GstElement *element)
350 GstDV1394Src *dv1394src;
352 g_return_val_if_fail (GST_IS_DV1394SRC (element), GST_STATE_FAILURE);
353 dv1394src = GST_DV1394SRC (element);
355 switch (GST_STATE_TRANSITION (element)) {
356 case GST_STATE_NULL_TO_READY:
357 if ((dv1394src->handle = raw1394_new_handle()) == NULL) {
358 GST_INFO_OBJECT (dv1394src,"can't get raw1394 handle");
359 return GST_STATE_FAILURE;
361 raw1394_set_userdata(dv1394src->handle,dv1394src);
362 dv1394src->numcards = raw1394_get_port_info(dv1394src->handle,dv1394src->pinfo,16);
363 if (dv1394src->numcards == 0) {
364 GST_INFO_OBJECT (dv1394src,"no cards available for raw1394");
365 return GST_STATE_FAILURE;
367 if (dv1394src->pinfo[dv1394src->card].nodes <= 1) {
368 GST_INFO_OBJECT (dv1394src,"there are no nodes on the 1394 bus");
369 return GST_STATE_FAILURE;
371 if (raw1394_set_port(dv1394src->handle,dv1394src->port) < 0) {
372 GST_INFO_OBJECT (dv1394src,"can't set 1394 port %d",dv1394src->port);
373 return GST_STATE_FAILURE;
375 raw1394_set_iso_handler(dv1394src->handle,dv1394src->channel,gst_dv1394src_iso_receive);
376 raw1394_set_bus_reset_handler(dv1394src->handle,gst_dv1394src_bus_reset);
377 dv1394src->started = FALSE;
378 GST_DEBUG ("successfully opened up 1394 connection");
380 case GST_STATE_PAUSED_TO_PLAYING:
381 if (raw1394_start_iso_rcv(dv1394src->handle,dv1394src->channel) < 0) {
382 GST_INFO_OBJECT (dv1394src,"can't start 1394 iso receive");
383 return GST_STATE_FAILURE;
386 case GST_STATE_PLAYING_TO_PAUSED:
387 raw1394_stop_iso_rcv(dv1394src->handle, dv1394src->channel);
389 case GST_STATE_READY_TO_NULL:
390 if (dv1394src->pool != NULL) {
391 gst_buffer_pool_unref(dv1394src->pool);
392 dv1394src->pool = NULL;
394 raw1394_destroy_handle(dv1394src->handle);
400 /* if we haven't failed already, give the parent class a chance to ;-) */
401 if (GST_ELEMENT_CLASS (parent_class)->change_state)
402 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
404 return GST_STATE_SUCCESS;