2 * Copyright (C) 2003 Martin Soto <martinsoto@users.sourceforge.net>
4 * dxr3videosink.c: Video sink for em8300 based cards.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
29 #include <sys/ioctl.h>
31 #include <linux/em8300.h>
33 #include <gst/gst-i18n-plugin.h>
36 #include "dxr3videosink.h"
37 #include "dxr3marshal.h"
39 #include "dxr3common.h"
41 /* Dxr3VideoSink signals and args */
53 /* Possible states for the MPEG start code scanner. */
56 SCAN_STATE_WAITING, /* Waiting for a code. */
57 SCAN_STATE_0, /* 0 seen. */
58 SCAN_STATE_00, /* 00 seen. */
59 SCAN_STATE_001 /* 001 seen. */
62 /* Possible states for the MPEG sequence parser. */
65 PARSE_STATE_WAITING, /* Waiting for the start of a sequence. */
66 PARSE_STATE_START, /* Start of sequence seen. */
67 PARSE_STATE_PICTURE /* Picture start seen. */
71 /* Relevant mpeg start codes. */
72 #define START_CODE_PICTURE 0x00
73 #define START_CODE_SEQUENCE_HEADER 0xB3
74 #define START_CODE_SEQUENCE_END 0xB7
76 static GstStaticPadTemplate dxr3videosink_sink_factory =
77 GST_STATIC_PAD_TEMPLATE ("sink",
80 GST_STATIC_CAPS ("video/mpeg, "
81 "mpegversion = (int) { 1, 2 }, " "systemstream = (boolean) FALSE"
82 /* width/height/framerate omitted, we don't
83 * need a parsed stream */
88 static void dxr3videosink_class_init (Dxr3VideoSinkClass * klass);
89 static void dxr3videosink_base_init (Dxr3VideoSinkClass * klass);
90 static void dxr3videosink_init (Dxr3VideoSink * dxr3videosink);
92 static void dxr3videosink_set_property (GObject * object,
93 guint prop_id, const GValue * value, GParamSpec * pspec);
94 static void dxr3videosink_get_property (GObject * object,
95 guint prop_id, GValue * value, GParamSpec * pspec);
97 static gboolean dxr3videosink_open (Dxr3VideoSink * sink);
98 static void dxr3videosink_close (Dxr3VideoSink * sink);
99 static void dxr3videosink_set_clock (GstElement * element, GstClock * clock);
101 static void dxr3videosink_reset_parser (Dxr3VideoSink * sink);
102 static int dxr3videosink_next_start_code (Dxr3VideoSink * sink);
103 static void dxr3videosink_discard_data (Dxr3VideoSink * sink, guint cut);
104 static void dxr3videosink_write_data (Dxr3VideoSink * sink, guint cut);
105 static void dxr3videosink_parse_data (Dxr3VideoSink * sink);
107 static gboolean dxr3videosink_handle_event (GstPad * pad, GstEvent * event);
108 static void dxr3videosink_chain (GstPad * pad, GstData * _data);
110 static GstStateChangeReturn dxr3videosink_change_state (GstElement * element,
111 GstStateChange transition);
113 /* static void dxr3videosink_wait (Dxr3VideoSink *sink, */
114 /* GstClockTime time); */
115 static int dxr3videosink_mvcommand (Dxr3VideoSink * sink, int command);
117 static void dxr3videosink_flushed (Dxr3VideoSink * sink);
119 static GstElementClass *parent_class = NULL;
120 static guint dxr3videosink_signals[LAST_SIGNAL] = { 0 };
124 dxr3videosink_get_type (void)
126 static GType dxr3videosink_type = 0;
128 if (!dxr3videosink_type) {
129 static const GTypeInfo dxr3videosink_info = {
130 sizeof (Dxr3VideoSinkClass),
131 (GBaseInitFunc) dxr3videosink_base_init,
133 (GClassInitFunc) dxr3videosink_class_init,
136 sizeof (Dxr3VideoSink),
138 (GInstanceInitFunc) dxr3videosink_init,
141 dxr3videosink_type = g_type_register_static (GST_TYPE_ELEMENT,
142 "Dxr3VideoSink", &dxr3videosink_info, 0);
145 return dxr3videosink_type;
150 dxr3videosink_base_init (Dxr3VideoSinkClass * klass)
152 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
154 gst_element_class_add_pad_template (element_class,
155 gst_static_pad_template_get (&dxr3videosink_sink_factory));
156 gst_element_class_set_details_simple (element_class,
157 "dxr3/Hollywood+ mpeg decoder board video element", "Sink/Video",
158 "Feeds MPEG2 video to Sigma Designs em8300 based boards",
159 "Martin Soto <martinsoto@users.sourceforge.net>");
163 dxr3videosink_class_init (Dxr3VideoSinkClass * klass)
165 GObjectClass *gobject_class;
166 GstElementClass *gstelement_class;
168 gobject_class = (GObjectClass *) klass;
169 gstelement_class = (GstElementClass *) klass;
171 parent_class = g_type_class_peek_parent (klass);
173 dxr3videosink_signals[SIGNAL_FLUSHED] =
174 g_signal_new ("flushed", G_TYPE_FROM_CLASS (klass),
176 G_STRUCT_OFFSET (Dxr3VideoSinkClass, flushed),
177 NULL, NULL, dxr3_marshal_VOID__VOID, G_TYPE_NONE, 0);
179 klass->flushed = dxr3videosink_flushed;
181 gobject_class->set_property = dxr3videosink_set_property;
182 gobject_class->get_property = dxr3videosink_get_property;
184 gstelement_class->change_state = dxr3videosink_change_state;
185 gstelement_class->set_clock = dxr3videosink_set_clock;
190 dxr3videosink_init (Dxr3VideoSink * sink)
194 pad = gst_pad_new_from_static_template (&dxr3videosink_sink_factory, "sink");
195 gst_element_add_pad (GST_ELEMENT (sink), pad);
196 gst_pad_set_chain_function (pad, dxr3videosink_chain);
198 GST_OBJECT_FLAG_SET (GST_ELEMENT (sink), GST_ELEMENT_EVENT_AWARE);
200 sink->card_number = 0;
202 sink->video_filename = NULL;
204 sink->control_filename = NULL;
205 sink->control_fd = -1;
209 sink->last_ts = GST_CLOCK_TIME_NONE;
211 sink->cur_buf = NULL;
212 dxr3videosink_reset_parser (sink);
217 dxr3videosink_set_property (GObject * object, guint prop_id,
218 const GValue * value, GParamSpec * pspec)
222 sink = DXR3VIDEOSINK (object);
232 dxr3videosink_get_property (GObject * object, guint prop_id,
233 GValue * value, GParamSpec * pspec)
237 g_return_if_fail (GST_IS_DXR3VIDEOSINK (object));
239 sink = DXR3VIDEOSINK (object);
243 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
250 dxr3videosink_open (Dxr3VideoSink * sink)
252 g_return_val_if_fail (!GST_OBJECT_FLAG_IS_SET (sink, DXR3VIDEOSINK_OPEN),
255 /* Compute the name of the video device file. */
256 sink->video_filename = g_strdup_printf ("/dev/em8300_mv-%d",
259 sink->video_fd = open (sink->video_filename, O_WRONLY);
260 if (sink->video_fd < 0) {
261 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
262 (_("Could not open video device \"%s\" for writing."),
263 sink->video_filename), GST_ERROR_SYSTEM);
267 /* Open the control device. */
268 sink->control_filename = g_strdup_printf ("/dev/em8300-%d",
271 sink->control_fd = open (sink->control_filename, O_WRONLY);
272 if (sink->control_fd < 0) {
273 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
274 (_("Could not open control device \"%s\" for writing."),
275 sink->control_filename), GST_ERROR_SYSTEM);
279 GST_OBJECT_FLAG_SET (sink, DXR3VIDEOSINK_OPEN);
286 dxr3videosink_close (Dxr3VideoSink * sink)
288 g_return_if_fail (GST_OBJECT_FLAG_IS_SET (sink, DXR3VIDEOSINK_OPEN));
290 if (close (sink->video_fd) != 0) {
291 GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
292 (_("Could not close video device \"%s\"."), sink->video_filename),
297 if (close (sink->control_fd) != 0) {
298 GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
299 (_("Could not close control device \"%s\"."), sink->control_filename),
304 GST_OBJECT_FLAG_UNSET (sink, DXR3VIDEOSINK_OPEN);
306 free (sink->video_filename);
307 sink->video_filename = NULL;
312 dxr3videosink_set_clock (GstElement * element, GstClock * clock)
314 Dxr3VideoSink *src = DXR3VIDEOSINK (element);
321 dxr3videosink_reset_parser (Dxr3VideoSink * sink)
323 if (sink->cur_buf != NULL) {
324 gst_buffer_unref (sink->cur_buf);
325 sink->cur_buf = NULL;
327 sink->cur_ts = GST_CLOCK_TIME_NONE;
329 sink->scan_state = SCAN_STATE_WAITING;
332 sink->parse_state = PARSE_STATE_WAITING;
337 dxr3videosink_next_start_code (Dxr3VideoSink * sink)
341 g_return_val_if_fail (sink->cur_buf != NULL, -1);
343 while (sink->scan_pos < GST_BUFFER_SIZE (sink->cur_buf)) {
344 c = (GST_BUFFER_DATA (sink->cur_buf))[sink->scan_pos];
346 switch (sink->scan_state) {
347 case SCAN_STATE_WAITING:
349 sink->scan_state = SCAN_STATE_0;
354 sink->scan_state = SCAN_STATE_00;
356 sink->scan_state = SCAN_STATE_WAITING;
361 sink->scan_state = SCAN_STATE_001;
362 } else if (c != 0x00) {
363 sink->scan_state = SCAN_STATE_WAITING;
368 sink->scan_state = SCAN_STATE_WAITING;
380 dxr3videosink_discard_data (Dxr3VideoSink * sink, guint cut)
385 g_return_if_fail (sink->cur_buf != NULL);
386 g_assert (cut <= sink->scan_pos);
388 size = sink->scan_pos - cut;
390 g_return_if_fail (size <= GST_BUFFER_SIZE (sink->cur_buf));
392 if (GST_BUFFER_SIZE (sink->cur_buf) == size) {
393 gst_buffer_unref (sink->cur_buf);
394 sink->cur_buf = NULL;
396 sub = gst_buffer_create_sub (sink->cur_buf, size,
397 GST_BUFFER_SIZE (sink->cur_buf)
399 gst_buffer_unref (sink->cur_buf);
403 sink->scan_state = SCAN_STATE_WAITING;
404 sink->scan_pos = cut;
406 sink->cur_ts = GST_CLOCK_TIME_NONE;
411 dxr3videosink_write_data (Dxr3VideoSink * sink, guint cut)
416 g_return_if_fail (sink->cur_buf != NULL);
418 if (GST_OBJECT_FLAG_IS_SET (sink, DXR3VIDEOSINK_OPEN)) {
419 if (sink->cur_ts != GST_CLOCK_TIME_NONE) {
422 /* fprintf (stderr, "------ Video Time %.04f\n", */
423 /* (double) sink->cur_ts / GST_SECOND); */
425 pts = (guint) GSTTIME_TO_MPEGTIME (sink->cur_ts);
426 ioctl (sink->video_fd, EM8300_IOCTL_VIDEO_SETPTS, &pts);
427 sink->cur_ts = GST_CLOCK_TIME_NONE;
430 data = GST_BUFFER_DATA (sink->cur_buf);
431 size = sink->scan_pos - cut;
433 g_assert (size <= GST_BUFFER_SIZE (sink->cur_buf));
435 /* We should always write data that corresponds to whole MPEG
436 video sintactical elements. They should always start with an
438 g_assert (size >= 4 && data[0] == 0 && data[1] == 0 && data[2] == 1);
441 written = write (sink->video_fd, data, size);
443 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE,
444 (_("Could not write to device \"%s\"."), sink->video_filename),
448 size = size - written;
449 data = data + written;
453 dxr3videosink_discard_data (sink, cut);
458 dxr3videosink_parse_data (Dxr3VideoSink * sink)
462 /* Timestamp handling assumes that timestamps are associated to
463 sequence starts. This seems to be the case, at least for
466 code = dxr3videosink_next_start_code (sink);
468 switch (sink->parse_state) {
470 case PARSE_STATE_WAITING:
471 if (code == START_CODE_SEQUENCE_HEADER) {
472 dxr3videosink_discard_data (sink, 4);
473 sink->parse_state = PARSE_STATE_START;
474 sink->cur_ts = sink->last_ts;
478 case PARSE_STATE_START:
480 case START_CODE_SEQUENCE_HEADER:
481 dxr3videosink_discard_data (sink, 4);
482 sink->cur_ts = sink->last_ts;
484 case START_CODE_SEQUENCE_END:
485 dxr3videosink_discard_data (sink, 0);
486 sink->parse_state = PARSE_STATE_WAITING;
488 case START_CODE_PICTURE:
489 sink->parse_state = PARSE_STATE_PICTURE;
494 case PARSE_STATE_PICTURE:
496 case START_CODE_SEQUENCE_HEADER:
497 dxr3videosink_write_data (sink, 4);
498 sink->parse_state = PARSE_STATE_START;
499 sink->cur_ts = sink->last_ts;
501 case START_CODE_SEQUENCE_END:
502 dxr3videosink_write_data (sink, 0);
503 sink->parse_state = PARSE_STATE_WAITING;
505 case START_CODE_PICTURE:
506 dxr3videosink_write_data (sink, 4);
513 code = dxr3videosink_next_start_code (sink);
516 if (sink->parse_state == PARSE_STATE_WAITING) {
517 dxr3videosink_discard_data (sink, 0);
523 dxr3videosink_handle_event (GstPad * pad, GstEvent * event)
528 sink = DXR3VIDEOSINK (gst_pad_get_parent (pad));
530 type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;
533 case GST_EVENT_EMPTY:
534 //fprintf (stderr, "++++++ Video empty event\n");
536 /* FIXME: Handle this with a discontinuity or something. */
537 /* Write an MPEG2 sequence end code, to ensure that the card
538 actually displays the last picture. Apparently some DVDs are
539 encoded without proper sequence end codes. */
540 static guint8 sec[4] = { 0x00, 0x00, 0x01, 0xb7 };
542 if (sink->cur_buf != NULL) {
543 dxr3videosink_write_data (sink, 0);
546 write (sink->video_fd, &sec, 4);
550 case GST_EVENT_DISCONTINUOUS:
551 //fprintf (stderr, "++++++ Video discont event\n");
555 unsigned cur_scr, mpeg_scr, diff;
557 has_time = gst_event_discont_get_value (event, GST_FORMAT_TIME, &time);
559 /* fprintf (stderr, "^^^^^^ Discontinuous event has time %.4f\n", */
560 /* (double) time / GST_SECOND); */
562 /* If the SCR in the card is way off, fix it. */
563 ioctl (sink->control_fd, EM8300_IOCTL_SCR_GET, &cur_scr);
564 mpeg_scr = MPEGTIME_TO_DXRTIME (GSTTIME_TO_MPEGTIME (time));
566 diff = cur_scr > mpeg_scr ? cur_scr - mpeg_scr : mpeg_scr - cur_scr;
570 /* fprintf (stderr, "====== Adjusting SCR from video\n"); */
572 ioctl (sink->control_fd, EM8300_IOCTL_SCR_SET, &zero);
573 ioctl (sink->control_fd, EM8300_IOCTL_SCR_SET, &mpeg_scr);
576 /* fprintf (stderr, "^^^^^^ Discontinuous event has no time\n"); */
581 case GST_EVENT_FLUSH:
582 dxr3videosink_reset_parser (sink);
586 gst_pad_event_default (pad, event);
595 dxr3videosink_chain (GstPad * pad, GstData * _data)
597 GstBuffer *buf = GST_BUFFER (_data);
601 g_return_if_fail (pad != NULL);
602 g_return_if_fail (GST_IS_PAD (pad));
603 g_return_if_fail (buf != NULL);
605 sink = DXR3VIDEOSINK (gst_pad_get_parent (pad));
607 if (GST_IS_EVENT (buf)) {
608 dxr3videosink_handle_event (pad, GST_EVENT (buf));
612 /* fprintf (stderr, "^^^^^^ Video block\n"); */
614 if (sink->cur_buf == NULL) {
617 merged = gst_buffer_merge (sink->cur_buf, buf);
618 gst_buffer_unref (sink->cur_buf);
619 gst_buffer_unref (buf);
620 sink->cur_buf = merged;
623 sink->last_ts = GST_BUFFER_TIMESTAMP (buf);
625 dxr3videosink_parse_data (sink);
629 static GstStateChangeReturn
630 dxr3videosink_change_state (GstElement * element, GstStateChange transition)
632 g_return_val_if_fail (GST_IS_DXR3VIDEOSINK (element),
633 GST_STATE_CHANGE_FAILURE);
635 switch (transition) {
636 case GST_STATE_CHANGE_NULL_TO_READY:
637 if (!GST_OBJECT_FLAG_IS_SET (element, DXR3VIDEOSINK_OPEN)) {
638 if (!dxr3videosink_open (DXR3VIDEOSINK (element))) {
639 return GST_STATE_CHANGE_FAILURE;
643 case GST_STATE_CHANGE_READY_TO_PAUSED:
644 dxr3videosink_mvcommand (DXR3VIDEOSINK (element), MVCOMMAND_PAUSE);
646 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
647 dxr3videosink_mvcommand (DXR3VIDEOSINK (element), MVCOMMAND_START);
649 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
650 dxr3videosink_mvcommand (DXR3VIDEOSINK (element), MVCOMMAND_PAUSE);
652 case GST_STATE_CHANGE_PAUSED_TO_READY:
653 dxr3videosink_mvcommand (DXR3VIDEOSINK (element), MVCOMMAND_STOP);
655 case GST_STATE_CHANGE_READY_TO_NULL:
656 if (GST_OBJECT_FLAG_IS_SET (element, DXR3VIDEOSINK_OPEN)) {
657 dxr3videosink_close (DXR3VIDEOSINK (element));
662 if (GST_ELEMENT_CLASS (parent_class)->change_state) {
663 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
666 return GST_STATE_CHANGE_SUCCESS;
671 * dxr3videosink_wait:
673 * Make the sink wait the specified amount of time.
676 dxr3videosink_wait (Dxr3VideoSink * sink, GstClockTime time)
679 GstClockTimeDiff jitter;
681 GstClockTime current_time = gst_clock_get_time (sink->clock);
683 id = gst_clock_new_single_shot_id (sink->clock, current_time + time);
684 ret = gst_clock_id_wait (id, &jitter);
685 gst_clock_id_free (id);
690 * dxr3videosink_mvcommand
692 * Send an MVCOMMAND to the card.
695 dxr3videosink_mvcommand (Dxr3VideoSink * sink, int command)
697 em8300_register_t regs;
699 regs.microcode_register = 1;
703 return ioctl (sink->control_fd, EM8300_IOCTL_WRITEREG, ®s);
708 * dxr3videosink_flushed:
710 * Default do nothing implementation for the "flushed" signal. The
711 * "flushed" signal will be fired right after flushing the hardware
712 * queues due to a received flush event
715 dxr3videosink_flushed (Dxr3VideoSink * sink)