try to fix EOS event handling
[gst-plugin-dvbmediasink.git] / src / gstdvbvideosink.c
1 /* GStreamer DVB Media Sink
2  * Copyright 2006 Felix Domke <tmbinc@elitedvb.net>
3  * based on code by:
4  * Copyright 2005 Thomas Vander Stichele <thomas@apestaart.org>
5  * Copyright 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
6  * 
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  * Alternatively, the contents of this file may be used under the
26  * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
27  * which case the following provisions apply instead of the ones
28  * mentioned above:
29  *
30  * This library is free software; you can redistribute it and/or
31  * modify it under the terms of the GNU Library General Public
32  * License as published by the Free Software Foundation; either
33  * version 2 of the License, or (at your option) any later version.
34  *
35  * This library is distributed in the hope that it will be useful,
36  * but WITHOUT ANY WARRANTY; without even the implied warranty of
37  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
38  * Library General Public License for more details.
39  *
40  * You should have received a copy of the GNU Library General Public
41  * License along with this library; if not, write to the
42  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
43  * Boston, MA 02111-1307, USA.
44  */
45
46 /**
47  * SECTION:element-plugin
48  *
49  * <refsect2>
50  * <title>Example launch line</title>
51  * <para>
52  * <programlisting>
53  * gst-launch -v -m audiotestsrc ! plugin ! fakesink silent=TRUE
54  * </programlisting>
55  * </para>
56  * </refsect2>
57  */
58
59 #ifdef HAVE_CONFIG_H
60 #include <config.h>
61 #endif
62 #include <unistd.h>
63 #include <stdint.h>
64 #include <sys/ioctl.h>
65 #include <sys/socket.h>
66 #include <linux/dvb/video.h>
67 #include <fcntl.h>
68 #include <poll.h>
69 #include <string.h>
70
71 #include <gst/gst.h>
72
73 #include "gstdvbvideosink.h"
74 #include "gstdvbsink-marshal.h"
75
76 #ifndef VIDEO_GET_PTS
77 #define VIDEO_GET_PTS              _IOR('o', 57, gint64)
78 #endif
79
80 #ifdef PACK_UNPACKED_XVID_DIVX5_BITSTREAM
81 struct bitstream
82 {
83         guint8 *data;
84         guint8 last;
85         int avail;
86 };
87
88 void bitstream_init(struct bitstream *bit, const void *buffer, gboolean wr)
89 {
90         bit->data = (guint8*) buffer;
91         if (wr) {
92                 bit->avail = 0;
93                 bit->last = 0;
94         }
95         else {
96                 bit->avail = 8;
97                 bit->last = *bit->data++;
98         }
99 }
100
101 unsigned long bitstream_get(struct bitstream *bit, int bits)
102 {
103         unsigned long res=0;
104         while (bits)
105         {
106                 unsigned int d=bits;
107                 if (!bit->avail) {
108                         bit->last = *bit->data++;
109                         bit->avail = 8;
110                 }
111                 if (d > bit->avail)
112                         d=bit->avail;
113                 res<<=d;
114                 res|=(bit->last>>(bit->avail-d))&~(-1<<d);
115                 bit->avail-=d;
116                 bits-=d;
117         }
118         return res;
119 }
120
121 void bitstream_put(struct bitstream *bit, unsigned long val, int bits)
122 {
123         while (bits)
124         {
125                 bit->last |= ((val & (1 << (bits-1))) ? 1 : 0) << (7 - bit->avail);
126                 if (++bit->avail == 8) {
127                         *bit->data = bit->last;
128                         ++bit->data;
129                         bit->last = 0;
130                         bit->avail = 0;
131                 }
132                 --bits;
133         }
134 }
135 #endif
136
137 /* We add a control socket as in fdsrc to make it shutdown quickly when it's blocking on the fd.
138  * Poll is used to determine when the fd is ready for use. When the element state is changed,
139  * it happens from another thread while fdsink is poll'ing on the fd. The state-change thread 
140  * sends a control message, so fdsink wakes up and changes state immediately otherwise
141  * it would stay blocked until it receives some data. */
142
143 /* the poll call is also performed on the control sockets, that way
144  * we can send special commands to unblock the poll call */
145 #define CONTROL_STOP            'S'                     /* stop the poll call */
146 #define CONTROL_SOCKETS(sink)   sink->control_sock
147 #define WRITE_SOCKET(sink)      sink->control_sock[1]
148 #define READ_SOCKET(sink)       sink->control_sock[0]
149
150 #define SEND_COMMAND(sink, command)                     \
151 G_STMT_START {                                          \
152         unsigned char c; c = command;                   \
153         write (WRITE_SOCKET(sink), &c, 1);              \
154 } G_STMT_END
155
156 #define READ_COMMAND(sink, command, res)                \
157 G_STMT_START {                                          \
158         res = read(READ_SOCKET(sink), &command, 1);     \
159 } G_STMT_END
160
161 GST_DEBUG_CATEGORY_STATIC (dvbvideosink_debug);
162 #define GST_CAT_DEFAULT dvbvideosink_debug
163
164 #define COMMON_VIDEO_CAPS \
165   "width = (int) [ 16, 4096 ], " \
166   "height = (int) [ 16, 4096 ], " \
167   "framerate = (fraction) [ 0, MAX ]"
168
169 #define MPEG4V2_LIMITED_CAPS \
170   "width = (int) [ 16, 800 ], " \
171   "height = (int) [ 16, 600 ], " \
172   "framerate = (fraction) [ 0, MAX ]"
173
174 enum
175 {
176         SIGNAL_GET_DECODER_TIME,
177         LAST_SIGNAL
178 };
179
180 static guint gst_dvb_videosink_signals[LAST_SIGNAL] = { 0 };
181
182 static GstStaticPadTemplate sink_factory_bcm7400 =
183 GST_STATIC_PAD_TEMPLATE (
184         "sink",
185         GST_PAD_SINK,
186         GST_PAD_ALWAYS,
187         GST_STATIC_CAPS (
188                 "video/mpeg, "
189                 "mpegversion = (int) { 1, 2, 4 }, "
190                 "systemstream = (boolean) false, "
191         COMMON_VIDEO_CAPS "; "
192                 "video/x-h264, "
193         COMMON_VIDEO_CAPS "; "
194                 "video/x-h263, "
195         COMMON_VIDEO_CAPS "; "
196                 "video/x-msmpeg, "
197         MPEG4V2_LIMITED_CAPS ", mspegversion = (int) 43; "
198                 "video/x-divx, "
199         MPEG4V2_LIMITED_CAPS ", divxversion = (int) [ 3, 5 ]; "
200                 "video/x-xvid, "
201         MPEG4V2_LIMITED_CAPS "; "
202                 "video/x-3ivx, "
203         MPEG4V2_LIMITED_CAPS "; ")
204 );
205
206 static GstStaticPadTemplate sink_factory_bcm7405 =
207 GST_STATIC_PAD_TEMPLATE (
208         "sink",
209         GST_PAD_SINK,
210         GST_PAD_ALWAYS,
211         GST_STATIC_CAPS (
212                 "video/mpeg, "
213                 "mpegversion = (int) { 1, 2, 4 }, "
214                 "systemstream = (boolean) false, "
215         COMMON_VIDEO_CAPS "; "
216                 "video/x-msmpeg, "
217         COMMON_VIDEO_CAPS ", mspegversion = (int) 43; "
218                 "video/x-h264, "
219         COMMON_VIDEO_CAPS "; "
220                 "video/x-h263, "
221         COMMON_VIDEO_CAPS "; "
222                 "video/x-divx, "
223         COMMON_VIDEO_CAPS ", divxversion = (int) [ 3, 5 ]; "
224                 "video/x-xvid, "
225         COMMON_VIDEO_CAPS "; "
226                 "video/x-3ivx, "
227         COMMON_VIDEO_CAPS "; ")
228 );
229
230
231 static GstStaticPadTemplate sink_factory_bcm7401 =
232 GST_STATIC_PAD_TEMPLATE (
233         "sink",
234         GST_PAD_SINK,
235         GST_PAD_ALWAYS,
236         GST_STATIC_CAPS (
237                 "video/mpeg, "
238                 "mpegversion = (int) { 1, 2, 4 }, "
239                 "systemstream = (boolean) false, "
240         COMMON_VIDEO_CAPS "; "
241                 "video/x-h264, "
242         COMMON_VIDEO_CAPS "; "
243                 "video/x-h263, "
244         COMMON_VIDEO_CAPS "; ")
245 );
246
247 static GstStaticPadTemplate sink_factory_ati_xilleon =
248 GST_STATIC_PAD_TEMPLATE (
249         "sink",
250         GST_PAD_SINK,
251         GST_PAD_ALWAYS,
252         GST_STATIC_CAPS (
253                 "video/mpeg, "
254                 "mpegversion = (int) { 1, 2 }, "
255                 "systemstream = (boolean) false, "
256         COMMON_VIDEO_CAPS "; ")
257 );
258
259 #define DEBUG_INIT(bla) \
260         GST_DEBUG_CATEGORY_INIT (dvbvideosink_debug, "dvbvideosink", 0, "dvbvideosink element");
261
262 GST_BOILERPLATE_FULL (GstDVBVideoSink, gst_dvbvideosink, GstBaseSink,
263         GST_TYPE_BASE_SINK, DEBUG_INIT);
264
265 static gboolean gst_dvbvideosink_start (GstBaseSink * sink);
266 static gboolean gst_dvbvideosink_stop (GstBaseSink * sink);
267 static gboolean gst_dvbvideosink_event (GstBaseSink * sink, GstEvent * event);
268 static GstFlowReturn gst_dvbvideosink_render (GstBaseSink * sink, GstBuffer * buffer);
269 static gboolean gst_dvbvideosink_set_caps (GstBaseSink * sink, GstCaps * caps);
270 static gboolean gst_dvbvideosink_unlock (GstBaseSink * basesink);
271 static gboolean gst_dvbvideosink_unlock_stop (GstBaseSink * basesink);
272 static void gst_dvbvideosink_dispose (GObject * object);
273 static GstStateChangeReturn gst_dvbvideosink_change_state (GstElement * element, GstStateChange transition);
274 static gint64 gst_dvbvideosink_get_decoder_time (GstDVBVideoSink *self);
275
276 typedef enum { DM7025, DM800, DM8000, DM500HD, DM800SE } hardware_type_t;
277
278 static hardware_type_t hwtype;
279
280 static void
281 gst_dvbvideosink_base_init (gpointer klass)
282 {
283         static GstElementDetails element_details = {
284                 "A DVB video sink",
285                 "Generic/DVBVideoSink",
286                 "Output video PES / ES into a DVB video device for hardware playback",
287                 "Felix Domke <tmbinc@elitedvb.net>"
288         };
289         GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
290
291         int fd = open("/proc/stb/info/model", O_RDONLY);
292         if ( fd > 0 )
293         {
294                 gchar string[8] = { 0, };
295                 ssize_t rd = read(fd, string, 7);
296                 if ( rd >= 5 )
297                 {
298                         if ( !strncasecmp(string, "DM7025", 6) ) {
299                                 hwtype = DM7025;
300                                 GST_INFO ("model is DM7025... set ati xilleon caps");
301                         } else if ( !strncasecmp(string, "DM500HD", 7) ) {
302                                 hwtype = DM500HD;
303                                 GST_INFO ("model is DM500HD... set bcm7405 caps");
304                                 gst_element_class_add_pad_template (element_class,
305                                         gst_static_pad_template_get (&sink_factory_bcm7405));
306                         } else if ( !strncasecmp(string, "DM800SE", 7) ) {
307                                 hwtype = DM800SE;
308                                 GST_INFO ("model is DM800SE... set bcm7405 caps");
309                                 gst_element_class_add_pad_template (element_class,
310                                         gst_static_pad_template_get (&sink_factory_bcm7405));
311                         } else if ( !strncasecmp(string, "DM8000", 6) ) {
312                                 hwtype = DM8000;
313                                 GST_INFO ("model is DM8000... set bcm7400 caps");
314                                 gst_element_class_add_pad_template (element_class,
315                                         gst_static_pad_template_get (&sink_factory_bcm7400));
316                         } else if ( !strncasecmp(string, "DM800", 5) ) {
317                                 hwtype = DM800;
318                                 GST_INFO ("model is DM800 set bcm7401 caps");
319                                 gst_element_class_add_pad_template (element_class,
320                                         gst_static_pad_template_get (&sink_factory_bcm7401));
321                         }
322                 }
323                 close(fd);
324         }
325
326         if (hwtype == DM7025) {
327                 gst_element_class_add_pad_template (element_class,
328                         gst_static_pad_template_get (&sink_factory_ati_xilleon));
329         }
330
331         gst_element_class_set_details (element_class, &element_details);
332 }
333
334 /* initialize the plugin's class */
335 static void
336 gst_dvbvideosink_class_init (GstDVBVideoSinkClass *klass)
337 {
338         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
339         GstBaseSinkClass *gstbasesink_class = GST_BASE_SINK_CLASS (klass);
340         GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
341
342         gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_dvbvideosink_dispose);
343
344         gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_dvbvideosink_start);
345         gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_dvbvideosink_stop);
346         gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_dvbvideosink_render);
347         gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_dvbvideosink_event);
348         gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (gst_dvbvideosink_unlock);
349         gstbasesink_class->unlock_stop = GST_DEBUG_FUNCPTR (gst_dvbvideosink_unlock_stop);
350         gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_dvbvideosink_set_caps);
351
352         element_class->change_state = GST_DEBUG_FUNCPTR (gst_dvbvideosink_change_state);
353
354         gst_dvb_videosink_signals[SIGNAL_GET_DECODER_TIME] =
355                 g_signal_new ("get-decoder-time",
356                 G_TYPE_FROM_CLASS (klass),
357                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
358                 G_STRUCT_OFFSET (GstDVBVideoSinkClass, get_decoder_time),
359                 NULL, NULL, gst_dvbsink_marshal_INT64__VOID, G_TYPE_INT64, 0);
360
361         klass->get_decoder_time = gst_dvbvideosink_get_decoder_time;
362 }
363
364 #define H264_BUFFER_SIZE (64*1024+2048)
365
366 /* initialize the new element
367  * instantiate pads and add them to element
368  * set functions
369  * initialize structure
370  */
371 static void
372 gst_dvbvideosink_init (GstDVBVideoSink *klass, GstDVBVideoSinkClass * gclass)
373 {
374         FILE *f = fopen("/proc/stb/vmpeg/0/fallback_framerate", "r");
375         klass->dec_running = FALSE;
376         klass->must_send_header = 1;
377         klass->h264_buffer = NULL;
378         klass->h264_nal_len_size = 0;
379         klass->codec_data = NULL;
380         klass->codec_type = CT_H264;
381 #ifdef PACK_UNPACKED_XVID_DIVX5_BITSTREAM
382         klass->must_pack_bitstream = 0;
383         klass->num_non_keyframes = 0;
384         klass->prev_frame = NULL;
385 #endif
386         klass->no_write = 0;
387         klass->queue = NULL;
388         klass->fd = -1;
389
390         if (f) {
391                 fgets(klass->saved_fallback_framerate, 16, f);
392                 fclose(f);
393         }
394
395         gst_base_sink_set_sync(GST_BASE_SINK (klass), FALSE);
396         gst_base_sink_set_async_enabled(GST_BASE_SINK (klass), TRUE);
397 }
398
399 static void gst_dvbvideosink_dispose (GObject * object)
400 {
401         GstDVBVideoSink *self = GST_DVBVIDEOSINK (object);
402         GstState state, pending;
403         GST_DEBUG_OBJECT(self, "dispose");
404
405 // hack for decodebin2 bug.. decodebin2 tries to dispose ..but doesnt set state to NULL when it is READY
406         switch(gst_element_get_state(GST_ELEMENT(object), &state, &pending, GST_CLOCK_TIME_NONE))
407         {
408         case GST_STATE_CHANGE_SUCCESS:
409                 GST_DEBUG_OBJECT(self, "success");
410                 if (state != GST_STATE_NULL) {
411                         GST_DEBUG_OBJECT(self, "state %d in dispose.. set it to NULL (decodebin2 bug?)", state);
412                         if (gst_element_set_state(GST_ELEMENT(object), GST_STATE_NULL) == GST_STATE_CHANGE_ASYNC) {
413                                 GST_DEBUG_OBJECT(self, "set state returned async... wait!");
414                                 gst_element_get_state(GST_ELEMENT(object), &state, &pending, GST_CLOCK_TIME_NONE);
415                         }
416                 }
417                 break;
418         case GST_STATE_CHANGE_ASYNC:
419                 GST_DEBUG_OBJECT(self, "async");
420                 break;
421         case GST_STATE_CHANGE_FAILURE:
422                 GST_DEBUG_OBJECT(self, "failure");
423                 break;
424         case GST_STATE_CHANGE_NO_PREROLL:
425                 GST_DEBUG_OBJECT(self, "no preroll");
426                 break;
427         default:
428                 break;
429         }
430 // hack end
431
432         GST_DEBUG_OBJECT(self, "state in dispose %d, pending %d", state, pending);
433
434         G_OBJECT_CLASS (parent_class)->dispose (object);
435 }
436
437 static gint64 gst_dvbvideosink_get_decoder_time (GstDVBVideoSink *self)
438 {
439         if (self->dec_running) {
440                 gint64 cur = 0;
441                 static gint64 last_pos = 0;
442
443                 ioctl(self->fd, VIDEO_GET_PTS, &cur);
444
445                 /* workaround until driver fixed */
446                 if (cur)
447                         last_pos = cur;
448                 else
449                         cur = last_pos;
450
451                 cur *= 11111;
452
453                 return cur;
454         }
455         return GST_CLOCK_TIME_NONE;
456 }
457
458 static gboolean gst_dvbvideosink_unlock (GstBaseSink * basesink)
459 {
460         GstDVBVideoSink *self = GST_DVBVIDEOSINK (basesink);
461         GST_OBJECT_LOCK(self);
462         self->no_write |= 2;
463         GST_OBJECT_UNLOCK(self);
464         SEND_COMMAND (self, CONTROL_STOP);
465         GST_DEBUG_OBJECT (basesink, "unlock");
466         return TRUE;
467 }
468
469 static gboolean gst_dvbvideosink_unlock_stop (GstBaseSink * basesink)
470 {
471         GstDVBVideoSink *self = GST_DVBVIDEOSINK (basesink);
472         GST_OBJECT_LOCK(self);
473         self->no_write &= ~2;
474         GST_OBJECT_UNLOCK(self);
475         GST_DEBUG_OBJECT (basesink, "unlock_stop");
476         return TRUE;
477 }
478
479 void queue_push(queue_entry_t **queue_base, guint8 *data, size_t len)
480 {
481         queue_entry_t *entry = malloc(sizeof(queue_entry_t)+len);
482         queue_entry_t *last = *queue_base;
483         guint8 *d = (guint8*)(entry+1);
484         memcpy(d, data, len);
485         entry->bytes = len;
486         entry->offset = 0;
487         if (!last)
488                 *queue_base = entry;
489         else {
490                 while(last->next)
491                         last = last->next;
492                 last->next = entry;
493         }
494         entry->next = NULL;
495 }
496
497 void queue_pop(queue_entry_t **queue_base)
498 {
499         queue_entry_t *base = *queue_base;
500         *queue_base = base->next;
501         free(base);
502 }
503
504 int queue_front(queue_entry_t **queue_base, guint8 **data, size_t *bytes)
505 {
506         if (!*queue_base) {
507                 *bytes = 0;
508                 *data = 0;
509         }
510         else {
511                 queue_entry_t *entry = *queue_base;
512                 *bytes = entry->bytes - entry->offset;
513                 *data = ((guint8*)(entry+1))+entry->offset;
514         }
515         return *bytes;
516 }
517
518 static gboolean
519 gst_dvbvideosink_event (GstBaseSink * sink, GstEvent * event)
520 {
521         GstDVBVideoSink *self = GST_DVBVIDEOSINK (sink);
522         GST_DEBUG_OBJECT (self, "EVENT %s", gst_event_type_get_name(GST_EVENT_TYPE (event)));
523         int ret=TRUE;
524
525         switch (GST_EVENT_TYPE (event)) {
526         case GST_EVENT_FLUSH_START:
527                 GST_OBJECT_LOCK(self);
528                 self->no_write |= 1;
529                 GST_OBJECT_UNLOCK(self);
530                 SEND_COMMAND (self, CONTROL_STOP);
531                 break;
532         case GST_EVENT_FLUSH_STOP:
533                 ioctl(self->fd, VIDEO_CLEAR_BUFFER);
534                 GST_OBJECT_LOCK(self);
535                 self->no_write &= ~1;
536                 self->must_send_header = 1;
537                 if (hwtype == DM7025)
538                         ++self->must_send_header;  // we must send the sequence header twice on dm7025... 
539                 while (self->queue)
540                         queue_pop(&self->queue);
541                 GST_OBJECT_UNLOCK(self);
542                 break;
543         case GST_EVENT_EOS:
544         {
545                 struct pollfd pfd[2];
546                 int retval;
547
548                 pfd[0].fd = READ_SOCKET(self);
549                 pfd[0].events = POLLIN;
550                 pfd[1].fd = self->fd;
551                 pfd[1].events = POLLIN;
552
553                 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
554                 while (1) {
555                         retval = poll(pfd, 2, 250);
556                         if (retval < 0) {
557                                 perror("poll in EVENT_EOS");
558                                 ret=FALSE;
559                                 break;
560                         }
561
562                         if (pfd[0].revents & POLLIN) {
563                                 GST_DEBUG_OBJECT (self, "wait EOS aborted!!\n");
564                                 ret=FALSE;
565                                 break;
566                         }
567
568                         if (pfd[1].revents & POLLIN) {
569                                 GST_DEBUG_OBJECT (self, "got buffer empty from driver!\n");
570                                 break;
571                         }
572
573                         if (sink->flushing) {
574                                 GST_DEBUG_OBJECT (self, "wait EOS flushing!!\n");
575                                 ret=FALSE;
576                                 break;
577                         }
578                 }
579                 GST_PAD_PREROLL_LOCK (sink->sinkpad);
580
581                 break;
582         }
583         case GST_EVENT_NEWSEGMENT:{
584                 GstFormat fmt;
585                 gboolean update;
586                 gdouble rate, applied_rate;
587                 gint64 cur, stop, time;
588                 int skip = 0, repeat = 0, ret;
589                 gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate, &fmt, &cur, &stop, &time);
590                 GST_DEBUG_OBJECT (self, "GST_EVENT_NEWSEGMENT rate=%f applied_rate=%f\n", rate, applied_rate);
591                 
592                 if (fmt == GST_FORMAT_TIME)
593                 {       
594                         if ( rate > 1 )
595                                 skip = (int) rate;
596                         else if ( rate < 1 )
597                                 repeat = 1.0/rate;
598
599                         ret = ioctl(self->fd, VIDEO_SLOWMOTION, repeat);
600                         ret = ioctl(self->fd, VIDEO_FAST_FORWARD, skip);
601 //                      gst_segment_set_newsegment_full (&dec->segment, update, rate, applied_rate, dformat, cur, stop, time);
602                 }
603                 break;
604         }
605
606         default:
607                 break;
608         }
609
610         return ret;
611 }
612
613 #define ASYNC_WRITE(data, len) do { \
614                 switch(AsyncWrite(sink, self, data, len)) { \
615                 case -1: goto poll_error; \
616                 case -3: goto write_error; \
617                 default: break; \
618                 } \
619         } while(0)
620
621 static int AsyncWrite(GstBaseSink * sink, GstDVBVideoSink *self, unsigned char *data, unsigned int len)
622 {
623         unsigned int written=0;
624         struct pollfd pfd[2];
625
626         pfd[0].fd = READ_SOCKET(self);
627         pfd[0].events = POLLIN;
628         pfd[1].fd = self->fd;
629         pfd[1].events = POLLOUT | POLLPRI;
630
631         do {
632 loop_start:
633                 if (self->no_write & 1) {
634                         GST_DEBUG_OBJECT (self, "skip %d bytes", len - written);
635                         break;
636                 }
637                 else if (self->no_write & 6) {
638                         // directly push to queue
639                         GST_OBJECT_LOCK(self);
640                         queue_push(&self->queue, data + written, len - written);
641                         GST_OBJECT_UNLOCK(self);
642                         GST_DEBUG_OBJECT (self, "pushed %d bytes to queue", len - written);
643                         break;
644                 }
645                 else
646                         GST_LOG_OBJECT (self, "going into poll, have %d bytes to write", len - written);
647                 if (poll(pfd, 2, -1) == -1) {
648                         if (errno == EINTR)
649                                 continue;
650                         return -1;
651                 }
652                 if (pfd[0].revents & POLLIN) {
653                         /* read all stop commands */
654                         while (TRUE) {
655                                 gchar command;
656                                 int res;
657                                 READ_COMMAND (self, command, res);
658                                 if (res < 0) {
659                                         GST_DEBUG_OBJECT (self, "no more commands");
660                                         /* no more commands */
661                                         goto loop_start;
662                                 }
663                         }
664                 }
665                 if (pfd[1].revents & POLLPRI) {
666                         GstStructure *s;
667                         GstMessage *msg;
668                         struct video_event evt;
669                         if (ioctl(self->fd, VIDEO_GET_EVENT, &evt) < 0)
670                                 g_warning ("failed to ioctl VIDEO_GET_EVENT!");
671                         else {
672                                 GST_INFO_OBJECT (self, "VIDEO_EVENT %d", evt.type);
673                                 if (evt.type == VIDEO_EVENT_SIZE_CHANGED) {
674                                         s = gst_structure_new ("eventSizeChanged",
675                                                 "aspect_ratio", G_TYPE_INT, evt.u.size.aspect_ratio == 0 ? 2 : 3,
676                                                 "width", G_TYPE_INT, evt.u.size.w,
677                                                 "height", G_TYPE_INT, evt.u.size.h, NULL);
678                                         msg = gst_message_new_element (GST_OBJECT (sink), s);
679                                         gst_element_post_message (GST_ELEMENT (sink), msg);
680                                 } else if (evt.type == VIDEO_EVENT_FRAME_RATE_CHANGED) {
681                                         s = gst_structure_new ("eventFrameRateChanged",
682                                                 "frame_rate", G_TYPE_INT, evt.u.frame_rate, NULL);
683                                         msg = gst_message_new_element (GST_OBJECT (sink), s);
684                                         gst_element_post_message (GST_ELEMENT (sink), msg);
685                                 } else if (evt.type == 16 /*VIDEO_EVENT_PROGRESSIVE_CHANGED*/) {
686                                         s = gst_structure_new ("eventProgressiveChanged",
687                                                 "progressive", G_TYPE_INT, evt.u.frame_rate, NULL);
688                                         msg = gst_message_new_element (GST_OBJECT (sink), s);
689                                         gst_element_post_message (GST_ELEMENT (sink), msg);
690                                 } else
691                                         g_warning ("unhandled DVBAPI Video Event %d", evt.type);
692                         }
693                 }
694                 if (pfd[1].revents & POLLOUT) {
695                         size_t queue_entry_size;
696                         guint8 *queue_data;
697                         GST_OBJECT_LOCK(self);
698                         if (queue_front(&self->queue, &queue_data, &queue_entry_size)) {
699                                 int wr = write(self->fd, queue_data, queue_entry_size);
700                                 if (wr < 0) {
701                                         switch (errno) {
702                                                 case EINTR:
703                                                 case EAGAIN:
704                                                         break;
705                                                 default:
706                                                         GST_OBJECT_UNLOCK(self);
707                                                         return -3;
708                                         }
709                                 }
710                                 else if (wr == queue_entry_size) {
711                                         queue_pop(&self->queue);
712                                         GST_DEBUG_OBJECT (self, "written %d queue bytes... pop entry", wr);
713                                 }
714                                 else {
715                                         self->queue->offset += wr;
716                                         GST_DEBUG_OBJECT (self, "written %d queue bytes... update offset", wr);
717                                 }
718                                 GST_OBJECT_UNLOCK(self);
719                                 continue;
720                         }
721                         GST_OBJECT_UNLOCK(self);
722                         int wr = write(self->fd, data+written, len - written);
723                         if (wr < 0) {
724                                 switch (errno) {
725                                         case EINTR:
726                                         case EAGAIN:
727                                                 continue;
728                                         default:
729                                                 return -3;
730                                 }
731                         }
732                         written += wr;
733                 }
734         } while (written != len);
735
736         return 0;
737 }
738
739 static GstFlowReturn
740 gst_dvbvideosink_render (GstBaseSink * sink, GstBuffer * buffer)
741 {
742         GstDVBVideoSink *self = GST_DVBVIDEOSINK (sink);
743         unsigned char *data = GST_BUFFER_DATA(buffer);
744         unsigned int data_len = GST_BUFFER_SIZE (buffer);
745         guint8 pes_header[2048];
746         unsigned int pes_header_len=0;
747         unsigned int payload_len=0;
748 //      int i=0;
749
750 #ifdef PACK_UNPACKED_XVID_DIVX5_BITSTREAM
751         gboolean commit_prev_frame_data = FALSE,
752                         cache_prev_frame = FALSE;
753 #endif
754
755 //      for (;i < (data_len > 0xF ? 0xF : data_len); ++i)
756 //              printf("%02x ", data[i]);
757 //      printf("%d bytes\n", data_len);
758 //      printf("timestamp: %08llx\n", (long long)GST_BUFFER_TIMESTAMP(buffer));
759
760         if (self->fd < 0)
761                 return GST_FLOW_OK;
762
763 #ifdef PACK_UNPACKED_XVID_DIVX5_BITSTREAM
764         if (self->must_pack_bitstream == 1) {
765                 cache_prev_frame = TRUE;
766                 unsigned int pos = 0;
767                 while(pos < data_len) {
768                         if (data[pos++])
769                                 continue;
770                         if (data[pos++])
771                                 continue;
772                         while (!data[pos])
773                                 ++pos;
774                         if (data[pos++] != 1)
775                                 continue;
776                         if ((data[pos++] & 0xF0) == 0x20) { // we need time_inc_res
777                                 gboolean low_delay=FALSE;
778                                 unsigned int ver_id = 1, shape=0, time_inc_res=0, tmp=0;
779                                 struct bitstream bit;
780                                 bitstream_init(&bit, data+pos, 0);
781                                 bitstream_get(&bit, 9);
782                                 if (bitstream_get(&bit, 1)) {
783                                         ver_id = bitstream_get(&bit, 4); // ver_id
784                                         bitstream_get(&bit, 3);
785                                 }
786                                 if ((tmp=bitstream_get(&bit, 4)) == 15) { // Custom Aspect Ration
787                                         bitstream_get(&bit, 8); // skip AR width
788                                         bitstream_get(&bit, 8); // skip AR height
789                                 }
790                                 if (bitstream_get(&bit, 1)) {
791                                         bitstream_get(&bit, 2);
792                                         low_delay = bitstream_get(&bit, 1) ? TRUE : FALSE;
793                                         if (bitstream_get(&bit, 1)) {
794                                                 bitstream_get(&bit, 32);
795                                                 bitstream_get(&bit, 32);
796                                                 bitstream_get(&bit, 15);
797                                         }
798                                 }
799                                 shape = bitstream_get(&bit, 2);
800                                 if (ver_id != 1 && shape == 3 /* Grayscale */)
801                                         bitstream_get(&bit, 4);
802                                 bitstream_get(&bit, 1);
803                                 time_inc_res = bitstream_get(&bit, 16);
804                                 self->time_inc_bits = 0;
805                                 while (time_inc_res) { // count bits
806                                         ++self->time_inc_bits;
807                                         time_inc_res >>= 1;
808                                 }
809 //                              printf("%d time_inc_bits\n", self->time_inc_bits);
810                         }
811                 }
812         }
813
814         if (self->must_pack_bitstream == 1) {
815                 int tmp1, tmp2;
816                 unsigned char c1, c2;
817                 unsigned int pos = 0;
818                 while(pos < data_len) {
819                         if (data[pos++])
820                                 continue;
821                         if (data[pos++])
822                                 continue;
823                         while (!data[pos])
824                                 ++pos;
825                         if (data[pos++] != 1)
826                                 continue;
827                         if (data[pos++] != 0xB2)
828                                 continue;
829                         if (data_len - pos < 13)
830                                 break;
831                         if (sscanf((char*)data+pos, "DivX%d%c%d%cp", &tmp1, &c1, &tmp2, &c2) == 4 && (c1 == 'b' || c1 == 'B') && (c2 == 'p' || c2 == 'P')) {
832                                 GST_INFO_OBJECT (self, "%s seen... already packed!", (char*)data+pos);
833                                 self->must_pack_bitstream = 0;
834                         }
835 //                      if (self->must_pack_bitstream)
836 //                              printf("pack needed\n");
837 //                      else
838 //                              printf("no pack needed\n");
839                 }
840         }
841 #endif
842
843         pes_header[0] = 0;
844         pes_header[1] = 0;
845         pes_header[2] = 1;
846         pes_header[3] = 0xE0;
847
848                 /* do we have a timestamp? */
849         if (GST_BUFFER_TIMESTAMP(buffer) != GST_CLOCK_TIME_NONE) {
850                 unsigned long long pts = GST_BUFFER_TIMESTAMP(buffer) * 9LL / 100000 /* convert ns to 90kHz */;
851
852                 pes_header[6] = 0x80;
853                 pes_header[7] = 0x80;
854                 
855                 pes_header[8] = 5;
856                 
857                 pes_header[9] =  0x21 | ((pts >> 29) & 0xE);
858                 pes_header[10] = pts >> 22;
859                 pes_header[11] = 0x01 | ((pts >> 14) & 0xFE);
860                 pes_header[12] = pts >> 7;
861                 pes_header[13] = 0x01 | ((pts << 1) & 0xFE);
862
863                 pes_header_len = 14;
864
865                 if (self->codec_data) {
866                         switch (self->codec_type) { // we must always resend the codec data before every seq header on dm8k
867                         case CT_MPEG4_PART2:
868                         case CT_DIVX4:
869                                 if (data[0] == 0xb3 || !memcmp(data, "\x00\x00\x01\xb3", 4))
870                                         self->must_send_header = 1;
871                         default:
872                                 break;
873                         }
874                         if (self->must_send_header) {
875                                 if (self->codec_type != CT_MPEG1 && self->codec_type != CT_MPEG2 && (self->codec_type != CT_DIVX4 || data[3] == 0x00)) {
876                                         unsigned char *codec_data = GST_BUFFER_DATA (self->codec_data);
877                                         unsigned int codec_data_len = GST_BUFFER_SIZE (self->codec_data);
878                                         if (self->codec_type == CT_DIVX311)
879                                                 ASYNC_WRITE(codec_data, codec_data_len);
880                                         else {
881                                                 memcpy(pes_header+pes_header_len, codec_data, codec_data_len);
882                                                 pes_header_len += codec_data_len;
883                                         }
884                                         self->must_send_header = 0;
885                                 }
886                         }
887                         if (self->codec_type == CT_H264) {  // MKV stuff
888                                 unsigned int pos = 0;
889                                 if (self->h264_nal_len_size == 4) {
890                                         while(TRUE) {
891                                                 unsigned int pack_len = (data[pos] << 24) | (data[pos+1] << 16) | (data[pos+2] << 8) | data[pos+3];
892 //                                              printf("patch %02x %02x %02x %02x\n",
893 //                                                      data[pos],
894 //                                                      data[pos + 1],
895 //                                                      data[pos + 2],
896 //                                                      data[pos + 3]);
897                                                 memcpy(data+pos, "\x00\x00\x00\x01", 4);
898 //                                              printf("pos %d, (%d) >= %d\n", pos, pos+pack_len, data_len);
899                                                 pos += 4;
900                                                 if ((pos + pack_len) >= data_len)
901                                                         break;
902                                                 pos += pack_len;
903                                         }
904                                 }
905                                 else if (self->h264_nal_len_size == 3) {
906                                         while(TRUE) {
907                                                 unsigned int pack_len = (data[pos] << 16) | (data[pos+1] << 8) | data[pos+2];
908 //                                              printf("patch %02x %02x %02x\n",
909 //                                                      data[pos],
910 //                                                      data[pos + 1],
911 //                                                      data[pos + 2]);
912                                                 memcpy(data+pos, "\x00\x00\x01", 3);
913 //                                              printf("pos %d, (%d) >= %d\n", pos, pos+pack_len, data_len);
914                                                 pos += 3;
915                                                 if ((pos + pack_len) >= data_len)
916                                                         break;
917                                                 pos += pack_len;
918                                         }
919                                 }
920                                 else {
921                                         unsigned char *dest = GST_BUFFER_DATA (self->h264_buffer);
922                                         unsigned int dest_pos = 0;
923                                         while(TRUE) {
924                                                 unsigned int pack_len = self->h264_nal_len_size == 2 ? (data[pos] << 8) | data[pos+1] : data[pos];
925                                                 if (dest_pos + pack_len <= H264_BUFFER_SIZE) {
926                                                         memcpy(dest+dest_pos, "\x00\x00\x01", 3);
927                                                         dest_pos += 3;
928                                                         pos += self->h264_nal_len_size;
929                                                         memcpy(dest+dest_pos, data+pos, pack_len);
930                                                         dest_pos += pack_len;
931                                                         if ((pos + pack_len) >= data_len)
932                                                                 break;
933                                                         pos += pack_len;
934                                                 }
935                                                 else {
936                                                         g_error("BUG!!!!!!!! H264 buffer to small skip video data!!.. please report!\n");
937                                                         break;
938                                                 }
939                                         }
940                                         data = dest;
941                                         data_len = dest_pos;
942                                 }
943                         }
944                         else if (self->codec_type == CT_MPEG4_PART2) {
945                                 if (data[0] || data[1] || data[2] != 1) {
946                                         memcpy(pes_header+pes_header_len, "\x00\x00\x01", 3);
947                                         pes_header_len += 3;
948                                 }
949                         }
950                         else if (self->codec_type == CT_DIVX311) {
951                                 if (data[0] || data[1] || data[2] != 1 || data[3] != 0xb6) {
952                                         memcpy(pes_header+pes_header_len, "\x00\x00\x01\xb6", 4);
953                                         pes_header_len += 4;
954                                 }
955                         }
956                 }
957         }
958         else {
959 //              printf("no timestamp!\n");
960                 pes_header[6] = 0x80;
961                 pes_header[7] = 0x00;
962                 pes_header[8] = 0;
963                 pes_header_len = 9;
964         }
965
966 #ifdef PACK_UNPACKED_XVID_DIVX5_BITSTREAM
967         if (self->must_pack_bitstream == 1) {
968                 unsigned int pos = 0;
969                 gboolean i_frame = FALSE;
970 //              gboolean s_frame = FALSE;
971                 while(pos < data_len) {
972                         if (data[pos++])
973                                 continue;
974                         if (data[pos++])
975                                 continue;
976                         while (!data[pos])
977                                 ++pos;
978                         if (data[pos++] != 1)
979                                 continue;
980                         if (data[pos++] != 0xB6)
981                                 continue;
982                         switch ((data[pos] & 0xC0) >> 6) {
983                                 case 0: // I-Frame
984 //                                      printf("I ");
985                                         cache_prev_frame = FALSE;
986                                         i_frame = TRUE;
987                                 case 1: // P-Frame
988 //                                      if (!i_frame)
989 //                                              printf("P ");
990                                         if (self->prev_frame != buffer) {
991                                                 struct bitstream bit;
992                                                 gboolean store_frame=FALSE;
993                                                 if (self->prev_frame) {
994                                                         if (!self->num_non_keyframes) {
995 //                                                              printf("no non keyframes...immediate commit prev frame\n");
996                                                                 GstFlowReturn ret = gst_dvbvideosink_render(sink, self->prev_frame);
997                                                                 gst_buffer_unref(self->prev_frame);
998                                                                 self->prev_frame = NULL;
999                                                                 if (ret != GST_FLOW_OK)
1000                                                                         return ret;
1001                                                                 store_frame = TRUE;
1002                                                         }
1003                                                         else {
1004 //                                                              int i=-4;
1005                                                                 pes_header[pes_header_len++] = 0;
1006                                                                 pes_header[pes_header_len++] = 0;
1007                                                                 pes_header[pes_header_len++] = 1;
1008                                                                 pes_header[pes_header_len++] = 0xB6;
1009                                                                 bitstream_init(&bit, pes_header+pes_header_len, 1);
1010                                                                 bitstream_put(&bit, 1, 2);
1011                                                                 bitstream_put(&bit, 0, 1);
1012                                                                 bitstream_put(&bit, 1, 1);
1013                                                                 bitstream_put(&bit, self->time_inc, self->time_inc_bits);
1014                                                                 bitstream_put(&bit, 1, 1);
1015                                                                 bitstream_put(&bit, 0, 1);
1016                                                                 bitstream_put(&bit, 0x7F >> bit.avail, 8 - bit.avail);
1017 //                                                              printf(" insert pack frame %d non keyframes, time_inc %d, time_inc_bits %d -",
1018 //                                                                      self->num_non_keyframes, self->time_inc, self->time_inc_bits);
1019 //                                                              for (; i < (bit.data - (pes_header+pes_header_len)); ++i)
1020 //                                                                      printf(" %02x", pes_header[pes_header_len+i]);
1021 //                                                              printf("\nset data_len to 0!\n");
1022                                                                 data_len = 0;
1023                                                                 pes_header_len += bit.data - (pes_header+pes_header_len);
1024                                                                 cache_prev_frame = TRUE;
1025                                                         }
1026                                                 }
1027                                                 else if (!i_frame)
1028                                                         store_frame = TRUE;
1029
1030                                                 self->num_non_keyframes=0;
1031
1032                                                 // extract time_inc
1033                                                 bitstream_init(&bit, data+pos, 0);
1034                                                 bitstream_get(&bit, 2); // skip coding_type
1035                                                 while(bitstream_get(&bit, 1));
1036                                                 bitstream_get(&bit, 1);
1037                                                 self->time_inc = bitstream_get(&bit, self->time_inc_bits);
1038 //                                              printf("\ntime_inc is %d\n", self->time_inc);
1039
1040                                                 if (store_frame) {
1041 //                                                      printf("store frame\n");
1042                                                         self->prev_frame = buffer;
1043                                                         gst_buffer_ref (buffer);
1044                                                         return GST_FLOW_OK;
1045                                                 }
1046                                         }
1047                                         else {
1048                                                 cache_prev_frame = FALSE;
1049 //                                              printf(" I/P Frame without non key frame(s)!!\n");
1050                                         }
1051                                         break;
1052                                 case 3: // S-Frame
1053 //                                      printf("S ");
1054 //                                      s_frame = TRUE;
1055                                 case 2: // B-Frame
1056 //                                      if (!s_frame)
1057 //                                              printf("B ");
1058                                         if (++self->num_non_keyframes == 1 && self->prev_frame) {
1059 //                                              printf("send grouped with prev P!\n");
1060                                                 commit_prev_frame_data = TRUE;
1061                                         }
1062                                         break;
1063                                 case 4: // N-Frame
1064                                 default:
1065                                         g_warning("unhandled divx5/xvid frame type %d\n", (data[pos] & 0xC0) >> 6);
1066                                         break;
1067                         }
1068                 }
1069 //              printf("\n");
1070         }
1071 #endif
1072
1073         payload_len = data_len + pes_header_len - 6;
1074
1075 #ifdef PACK_UNPACKED_XVID_DIVX5_BITSTREAM
1076         if (self->prev_frame && self->prev_frame != buffer) {
1077                 unsigned long long pts = GST_BUFFER_TIMESTAMP(self->prev_frame) * 9LL / 100000 /* convert ns to 90kHz */;
1078 //              printf("use prev timestamp: %08llx\n", (long long)GST_BUFFER_TIMESTAMP(self->prev_frame));
1079
1080                 pes_header[9] =  0x21 | ((pts >> 29) & 0xE);
1081                 pes_header[10] = pts >> 22;
1082                 pes_header[11] = 0x01 | ((pts >> 14) & 0xFE);
1083                 pes_header[12] = pts >> 7;
1084                 pes_header[13] = 0x01 | ((pts << 1) & 0xFE);
1085         }
1086
1087         if (commit_prev_frame_data)
1088                 payload_len += GST_BUFFER_SIZE (self->prev_frame);
1089 #endif
1090
1091         if (self->codec_type == CT_MPEG2 || self->codec_type == CT_MPEG1) {
1092                 if (!self->codec_data && data_len > 3 && !data[0] && !data[1] && data[2] == 1 && data[3] == 0xb3) { // sequence header?
1093                         gboolean ok = TRUE;
1094                         unsigned int pos = 4;
1095                         unsigned int sheader_data_len=0;
1096                         while(pos < data_len && ok) {
1097                                 if ( pos >= data_len )
1098                                         break;
1099                                 pos+=7;
1100                                 if ( pos >=data_len )
1101                                         break;
1102                                 sheader_data_len=12;
1103                                 if ( data[pos] & 2 ) { // intra matrix avail?
1104                                         pos+=64;
1105                                         if ( pos >=data_len )
1106                                                 break;
1107                                         sheader_data_len+=64;
1108                                 }
1109                                 if ( data[pos] & 1 ) { // non intra matrix avail?
1110                                         pos+=64;
1111                                         if ( pos >=data_len )
1112                                                 break;
1113                                         sheader_data_len+=64;
1114                                 }
1115                                 pos+=1;
1116                                 if ( pos+3 >=data_len )
1117                                         break;
1118                                 // extended start code
1119                                 if ( !data[pos] && !data[pos+1] && data[pos+2] == 1 && data[pos+3] == 0xB5 ) {
1120                                         pos+=3;
1121                                         sheader_data_len+=3;
1122                                         do {
1123                                                 pos+=1;
1124                                                 ++sheader_data_len;
1125                                                 if (pos+2 > data_len)
1126                                                         goto leave;
1127                                         }
1128                                         while( data[pos] || data[pos+1] || data[pos+2] != 1 );
1129                                 }
1130                                 if ( pos+3 >=data_len )
1131                                         break;
1132                                 // private data
1133                                 if ( !data[pos] && !data[pos+1] && data[pos+2] && data[pos+3] == 0xB2 ) {
1134                                         pos+=3;
1135                                         sheader_data_len+=3;
1136                                         do {
1137                                                 pos+=1;
1138                                                 ++sheader_data_len;
1139                                                 if (pos+2 > data_len)
1140                                                         goto leave;
1141                                         }
1142                                         while( data[pos] || data[pos+1] || data[pos+2] != 1 );
1143                                 }
1144                                 self->codec_data = gst_buffer_new_and_alloc(sheader_data_len);
1145                                 memcpy(GST_BUFFER_DATA(self->codec_data), data+pos-sheader_data_len, sheader_data_len);
1146                                 self->must_send_header = 0;
1147 leave:
1148                                 ok = FALSE;
1149                         }
1150                 }
1151                 else if (self->codec_data && self->must_send_header) {
1152                         unsigned char *codec_data = GST_BUFFER_DATA (self->codec_data);
1153                         unsigned int codec_data_len = GST_BUFFER_SIZE (self->codec_data);
1154                         int pos = 0;
1155                         while(pos < data_len) {
1156                                 if ( data[pos++] )
1157                                         continue;
1158                                 if ( data[pos++] )
1159                                         continue;
1160                                 while ( !data[pos] )
1161                                         pos++;
1162                                 if ( data[pos++] != 1 )
1163                                         continue;
1164                                 if ( data[pos++] != 0xb8 ) // group start code
1165                                         continue;
1166                                 pos-=4; // before group start
1167                                 payload_len += codec_data_len;
1168                                 if (payload_len <= 0xFFFF) {
1169                                         pes_header[4] = payload_len >> 8;
1170                                         pes_header[5] = payload_len & 0xFF;
1171                                 }
1172                                 else {
1173                                         pes_header[4] = 0;
1174                                         pes_header[5] = 0;
1175                                 }
1176                                 ASYNC_WRITE(pes_header, pes_header_len);
1177                                 ASYNC_WRITE(data, pos);
1178                                 ASYNC_WRITE(codec_data, codec_data_len);
1179                                 ASYNC_WRITE(data+pos, data_len - pos);
1180                                 --self->must_send_header;
1181                                 return GST_FLOW_OK;
1182                         }
1183                 }
1184         }
1185
1186         if (payload_len <= 0xFFFF) {
1187                 pes_header[4] = payload_len >> 8;
1188                 pes_header[5] = payload_len & 0xFF;
1189         }
1190         else {
1191                 pes_header[4] = 0;
1192                 pes_header[5] = 0;
1193         }
1194
1195         ASYNC_WRITE(pes_header, pes_header_len);
1196
1197 #ifdef PACK_UNPACKED_XVID_DIVX5_BITSTREAM
1198         if (commit_prev_frame_data) {
1199 //              printf("commit prev frame data\n");
1200                 ASYNC_WRITE(GST_BUFFER_DATA (self->prev_frame), GST_BUFFER_SIZE (self->prev_frame));
1201         }
1202
1203         if (self->prev_frame && self->prev_frame != buffer) {
1204 //              printf("unref prev_frame buffer\n");
1205                 gst_buffer_unref(self->prev_frame);
1206                 self->prev_frame = NULL;
1207         }
1208
1209         if (cache_prev_frame) {
1210 //              printf("cache prev frame\n");
1211                 gst_buffer_ref(buffer);
1212                 self->prev_frame = buffer;
1213         }
1214 #endif
1215         ASYNC_WRITE(data, data_len);
1216
1217         return GST_FLOW_OK;
1218 poll_error:
1219         {
1220                 GST_ELEMENT_ERROR (self, RESOURCE, READ, (NULL),
1221                                 ("poll on file descriptor: %s.", g_strerror (errno)));
1222                 GST_WARNING_OBJECT (self, "Error during poll");
1223                 return GST_FLOW_ERROR;
1224         }
1225 write_error:
1226         {
1227                 GST_ELEMENT_ERROR (self, RESOURCE, READ, (NULL),
1228                                 ("write on file descriptor: %s.", g_strerror (errno)));
1229                 GST_WARNING_OBJECT (self, "Error during write");
1230                 return GST_FLOW_ERROR;
1231         }
1232         self->must_send_header = 1;
1233         if (hwtype == DM7025)
1234                 ++self->must_send_header;  // we must send the sequence header twice on dm7025...
1235 }
1236
1237 static gboolean 
1238 gst_dvbvideosink_set_caps (GstBaseSink * basesink, GstCaps * caps)
1239 {
1240         GstDVBVideoSink *self = GST_DVBVIDEOSINK (basesink);
1241         GstStructure *structure = gst_caps_get_structure (caps, 0);
1242         const char *mimetype = gst_structure_get_name (structure);
1243         int streamtype = -1;
1244
1245         if (!strcmp (mimetype, "video/mpeg")) {
1246                 gint mpegversion;
1247                 gst_structure_get_int (structure, "mpegversion", &mpegversion);
1248                 switch (mpegversion) {
1249                         case 1:
1250                                 streamtype = 6;
1251                                 self->codec_type = CT_MPEG1;
1252                                 GST_INFO_OBJECT (self, "MIMETYPE video/mpeg1 -> VIDEO_SET_STREAMTYPE, 6");
1253                         break;
1254                         case 2:
1255                                 streamtype = 0;
1256                                 self->codec_type = CT_MPEG2;
1257                                 GST_INFO_OBJECT (self, "MIMETYPE video/mpeg2 -> VIDEO_SET_STREAMTYPE, 0");
1258                         break;
1259                         case 4:
1260                         {
1261                                 const GValue *codec_data = gst_structure_get_value (structure, "codec_data");
1262                                 if (codec_data) {
1263                                         GST_INFO_OBJECT (self, "MPEG4 have codec data");
1264                                         self->codec_data = gst_value_get_buffer (codec_data);
1265                                         self->codec_type = CT_MPEG4_PART2;
1266                                         gst_buffer_ref (self->codec_data);
1267                                 }
1268                                 streamtype = 4;
1269                                 GST_INFO_OBJECT (self, "MIMETYPE video/mpeg4 -> VIDEO_SET_STREAMTYPE, 4");
1270                         }
1271                         break;
1272                         default:
1273                                 GST_ELEMENT_ERROR (self, STREAM, FORMAT, (NULL), ("unhandled mpeg version %i", mpegversion));
1274                         break;
1275                 }
1276         } else if (!strcmp (mimetype, "video/x-3ivx")) {
1277                 const GValue *codec_data = gst_structure_get_value (structure, "codec_data");
1278                 if (codec_data) {
1279                         GST_INFO_OBJECT (self, "have 3ivx codec... handle as CT_MPEG4_PART2");
1280                         self->codec_data = gst_value_get_buffer (codec_data);
1281                         self->codec_type = CT_MPEG4_PART2;
1282                         gst_buffer_ref (self->codec_data);
1283                 }
1284                 streamtype = 4;
1285                 GST_INFO_OBJECT (self, "MIMETYPE video/x-3ivx -> VIDEO_SET_STREAMTYPE, 4");
1286         } else if (!strcmp (mimetype, "video/x-h264")) {
1287                 const GValue *cd_data = gst_structure_get_value (structure, "codec_data");
1288                 streamtype = 1;
1289                 if (cd_data) {
1290                         unsigned char tmp[2048];
1291                         unsigned int tmp_len = 0;
1292                         GstBuffer *codec_data = gst_value_get_buffer (cd_data);
1293                         unsigned char *data = GST_BUFFER_DATA (codec_data);
1294                         unsigned int cd_len = GST_BUFFER_SIZE (codec_data);
1295                         unsigned int cd_pos = 0;
1296                         GST_INFO_OBJECT (self, "H264 have codec data..!");
1297                         if (cd_len > 7 && data[0] == 1) {
1298                                 unsigned short len = (data[6] << 8) | data[7];
1299 //                              printf("2 %d bytes\n", len);
1300                                 if (cd_len >= (len + 8)) {
1301                                         unsigned int i=0;
1302                                         uint8_t profile_num[] = { 66, 77, 88, 100 };
1303                                         uint8_t profile_cmp[2] = { 0x67, 0x00 };
1304                                         const char *profile_str[] = { "baseline", "main", "extended", "high" };
1305 //                                      printf("3\n");
1306                                         memcpy(tmp, "\x00\x00\x00\x01", 4);
1307                                         tmp_len += 4;
1308 //                                      printf("header part1 ");
1309 //                                      for (i = 0; i < len; ++i)
1310 //                                              printf("%02x ", data[8+i]);
1311 //                                      printf("\n");
1312                                         memcpy(tmp+tmp_len, data+8, len);
1313                                         for (i = 0; i < 4; ++i) {
1314                                                 profile_cmp[1] = profile_num[i];
1315                                                 if (!memcmp(tmp+tmp_len, profile_cmp, 2)) {
1316                                                         uint8_t level_org = tmp[tmp_len+3];
1317                                                         if (level_org > 0x29) {
1318                                                                 GST_INFO_OBJECT (self, "H264 %s profile@%d.%d patched down to 4.1!", profile_str[i], level_org / 10 , level_org % 10);
1319                                                                 tmp[tmp_len+3] = 0x29; // level 4.1
1320                                                         }
1321                                                         else
1322                                                                 GST_INFO_OBJECT (self, "H264 %s profile@%d.%d", profile_str[i], level_org / 10 , level_org % 10);
1323                                                         break;
1324                                                 }
1325                                         }
1326                                         tmp_len += len;
1327                                         cd_pos = 8 + len;
1328                                         if (cd_len > (cd_pos + 2)) {
1329                                                 len = (data[cd_pos+1] << 8) | data[cd_pos+2];
1330 //                                              printf("4 %d bytes\n", len);
1331                                                 cd_pos += 3;
1332                                                 if (cd_len >= (cd_pos+len)) {
1333 //                                                      printf("codec data ok!\n");
1334                                                         memcpy(tmp+tmp_len, "\x00\x00\x00\x01", 4);
1335                                                         tmp_len += 4;
1336 //                                                      printf("header part2 %02x %02x %02x %02x ... %d bytes\n", data[cd_pos], data[cd_pos+1], data[cd_pos+2], data[cd_pos+3], len);
1337                                                         memcpy(tmp+tmp_len, data+cd_pos, len);
1338                                                         tmp_len += len;
1339                                                         self->codec_data = gst_buffer_new_and_alloc(tmp_len);
1340                                                         memcpy(GST_BUFFER_DATA(self->codec_data), tmp, tmp_len);
1341                                                         self->h264_nal_len_size = (data[4] & 0x03) + 1;
1342                                                         if (self->h264_nal_len_size < 3)
1343                                                                 self->h264_buffer = gst_buffer_new_and_alloc(H264_BUFFER_SIZE);
1344                                                 }
1345                                                 else
1346                                                         GST_WARNING_OBJECT (self, "codec_data to short(4)");
1347                                         }
1348                                         else
1349                                                 GST_WARNING_OBJECT (self, "codec_data to short(3)");
1350                                 }
1351                                 else
1352                                         GST_WARNING_OBJECT (self, "codec_data to short(2)");
1353                         }
1354                         else if (cd_len <= 7)
1355                                 GST_WARNING_OBJECT (self, "codec_data to short(1)");
1356                         else
1357                                 GST_WARNING_OBJECT (self, "wrong avcC version %d!", data[0]);
1358                 }
1359                 else
1360                         self->h264_nal_len_size = 0;
1361                 GST_INFO_OBJECT (self, "MIMETYPE video/x-h264 VIDEO_SET_STREAMTYPE, 1");
1362         } else if (!strcmp (mimetype, "video/x-h263")) {
1363                 streamtype = 2;
1364                 GST_INFO_OBJECT (self, "MIMETYPE video/x-h263 VIDEO_SET_STREAMTYPE, 2");
1365         } else if (!strcmp (mimetype, "video/x-xvid")) {
1366                 streamtype = 10;
1367 #ifdef PACK_UNPACKED_XVID_DIVX5_BITSTREAM
1368                 self->must_pack_bitstream = 1;
1369 #endif
1370                 GST_INFO_OBJECT (self, "MIMETYPE video/x-xvid -> VIDEO_SET_STREAMTYPE, 10");
1371         } else if (!strcmp (mimetype, "video/x-divx") || !strcmp (mimetype, "video/x-msmpeg")) {
1372                 gint divxversion = -1;
1373                 if (!gst_structure_get_int (structure, "divxversion", &divxversion) && !gst_structure_get_int (structure, "msmpegversion", &divxversion))
1374                         ;
1375                 switch (divxversion) {
1376                         case 3:
1377                         case 43:
1378                         {
1379                                 #define B_GET_BITS(w,e,b)  (((w)>>(b))&(((unsigned)(-1))>>((sizeof(unsigned))*8-(e+1-b))))
1380                                 #define B_SET_BITS(name,v,e,b)  (((unsigned)(v))<<(b))
1381                                 static const guint8 brcm_divx311_sequence_header[] = {
1382                                         0x00, 0x00, 0x01, 0xE0, 0x00, 0x34, 0x80, 0x80, // PES HEADER
1383                                         0x05, 0x2F, 0xFF, 0xFF, 0xFF, 0xFF, 
1384                                         0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x20, /* 0 .. 7 */
1385                                         0x08, 0xC8, 0x0D, 0x40, 0x00, 0x53, 0x88, 0x40, /* 8 .. 15 */
1386                                         0x0C, 0x40, 0x01, 0x90, 0x00, 0x97, 0x53, 0x0A, /* 16 .. 24 */
1387                                         0x00, 0x00, 0x00, 0x00,
1388                                         0x30, 0x7F, 0x00, 0x00, 0x01, 0xB2, 0x44, 0x69, /* 0 .. 7 */
1389                                         0x76, 0x58, 0x33, 0x31, 0x31, 0x41, 0x4E, 0x44  /* 8 .. 15 */
1390                                 };
1391                                 self->codec_data = gst_buffer_new_and_alloc(63);
1392                                 guint8 *data = GST_BUFFER_DATA(self->codec_data);
1393                                 gint height, width;
1394                                 gst_structure_get_int (structure, "height", &height);
1395                                 gst_structure_get_int (structure, "width", &width);
1396                                 memcpy(data, brcm_divx311_sequence_header, 63);
1397                                 data += 38;
1398                                 data[0] = B_GET_BITS(width,11,4);
1399                                 data[1] = B_SET_BITS("width [3..0]", B_GET_BITS(width,3,0), 7, 4) |
1400                                         B_SET_BITS("'10'", 0x02, 3, 2) |
1401                                         B_SET_BITS("height [11..10]", B_GET_BITS(height,11,10), 1, 0);
1402                                 data[2] = B_GET_BITS(height,9,2);
1403                                 data[3]= B_SET_BITS("height [1.0]", B_GET_BITS(height,1,0), 7, 6) |
1404                                         B_SET_BITS("'100000'", 0x20, 5, 0);
1405                                 streamtype = 13;
1406                                 self->codec_type = CT_DIVX311;
1407                                 GST_INFO_OBJECT (self, "MIMETYPE video/x-divx vers. 3 -> VIDEO_SET_STREAMTYPE, 13");
1408                         }
1409                         break;
1410                         case 4:
1411                                 streamtype = 14;
1412                                 self->codec_type = CT_DIVX4;
1413                                 self->codec_data = gst_buffer_new_and_alloc(12);
1414                                 guint8 *data = GST_BUFFER_DATA(self->codec_data);
1415                                 memcpy(data, "\x00\x00\x01\xb2\x44\x69\x76\x58\x34\x41\x4e\x44", 12);
1416                                 GST_INFO_OBJECT (self, "MIMETYPE video/x-divx vers. 4 -> VIDEO_SET_STREAMTYPE, 14");
1417                         break;
1418                         case 6:
1419                         case 5:
1420                                 streamtype = 15;
1421 #ifdef PACK_UNPACKED_XVID_DIVX5_BITSTREAM
1422                                 self->must_pack_bitstream = 1;
1423 #endif
1424                                 GST_INFO_OBJECT (self, "MIMETYPE video/x-divx vers. 5 -> VIDEO_SET_STREAMTYPE, 15");
1425                         break;
1426                         default:
1427                                 GST_ELEMENT_ERROR (self, STREAM, FORMAT, (NULL), ("unhandled divx version %i", divxversion));
1428                         break;
1429                 }
1430         }
1431         if (streamtype != -1) {
1432                 gint numerator, denominator;
1433                 if (gst_structure_get_fraction (structure, "framerate", &numerator, &denominator)) {
1434                         FILE *f = fopen("/proc/stb/vmpeg/0/fallback_framerate", "w");
1435                         if (f) {
1436                                 int valid_framerates[] = { 23976, 24000, 25000, 29970, 30000, 50000, 59940, 60000 };
1437                                 int framerate = (int)(((double)numerator * 1000) / denominator);
1438                                 int diff = 60000;
1439                                 int best = 0;
1440                                 int i = 0;
1441                                 for (; i < 7; ++i) {
1442                                         int ndiff = abs(framerate - valid_framerates[i]);
1443                                         if (ndiff < diff) {
1444                                                 diff = ndiff;
1445                                                 best = i;
1446                                         }
1447                                 }
1448                                 fprintf(f, "%d", valid_framerates[best]);
1449                                 fclose(f);
1450                         }
1451                 }
1452                 if (self->dec_running) {
1453                         ioctl(self->fd, VIDEO_STOP, 0);
1454                         self->dec_running = FALSE;
1455                 }
1456                 if (ioctl(self->fd, VIDEO_SET_STREAMTYPE, streamtype) < 0 )
1457                         if ( streamtype != 0 && streamtype != 6 )
1458                                 GST_ELEMENT_ERROR (self, STREAM, CODEC_NOT_FOUND, (NULL), ("hardware decoder can't handle streamtype %i", streamtype));
1459                 ioctl(self->fd, VIDEO_PLAY);
1460                 self->dec_running = TRUE;
1461         } else
1462                 GST_ELEMENT_ERROR (self, STREAM, TYPE_NOT_FOUND, (NULL), ("unimplemented stream type %s", mimetype));
1463
1464         return TRUE;
1465 }
1466
1467 static int readMpegProc(char *str, int decoder)
1468 {
1469         int val = -1;
1470         char tmp[64];
1471         sprintf(tmp, "/proc/stb/vmpeg/%d/%s", decoder, str);
1472         FILE *f = fopen(tmp, "r");
1473         if (f)
1474         {
1475                 fscanf(f, "%x", &val);
1476                 fclose(f);
1477         }
1478         return val;
1479 }
1480
1481 static int readApiSize(int fd, int *xres, int *yres, int *aspect)
1482 {
1483         video_size_t size;
1484         if (!ioctl(fd, VIDEO_GET_SIZE, &size))
1485         {
1486                 *xres = size.w;
1487                 *yres = size.h;
1488                 *aspect = size.aspect_ratio == 0 ? 2 : 3;  // convert dvb api to etsi
1489                 return 0;
1490         }
1491         return -1;
1492 }
1493
1494 static int readApiFrameRate(int fd, int *framerate)
1495 {
1496         unsigned int frate;
1497         if (!ioctl(fd, VIDEO_GET_FRAME_RATE, &frate))
1498         {
1499                 *framerate = frate;
1500                 return 0;
1501         }
1502         return -1;
1503 }
1504
1505 static gboolean
1506 gst_dvbvideosink_start (GstBaseSink * basesink)
1507 {
1508         GstDVBVideoSink *self = GST_DVBVIDEOSINK (basesink);
1509         gint control_sock[2];
1510
1511         GST_DEBUG_OBJECT (self, "start");
1512
1513
1514         if (socketpair(PF_UNIX, SOCK_STREAM, 0, control_sock) < 0) {
1515                 perror("socketpair");
1516                 goto socket_pair;
1517         }
1518
1519         READ_SOCKET (self) = control_sock[0];
1520         WRITE_SOCKET (self) = control_sock[1];
1521
1522         fcntl (READ_SOCKET (self), F_SETFL, O_NONBLOCK);
1523         fcntl (WRITE_SOCKET (self), F_SETFL, O_NONBLOCK);
1524
1525         return TRUE;
1526         /* ERRORS */
1527 socket_pair:
1528         {
1529                 GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ_WRITE, (NULL),
1530                                 GST_ERROR_SYSTEM);
1531                 return FALSE;
1532         }
1533 }
1534
1535 static gboolean
1536 gst_dvbvideosink_stop (GstBaseSink * basesink)
1537 {
1538         GstDVBVideoSink *self = GST_DVBVIDEOSINK (basesink);
1539         FILE *f = fopen("/proc/stb/vmpeg/0/fallback_framerate", "w");
1540         GST_DEBUG_OBJECT (self, "stop");
1541         if (self->fd >= 0)
1542         {
1543                 if (self->dec_running) {
1544                         ioctl(self->fd, VIDEO_STOP);
1545                         self->dec_running = FALSE;
1546                 }
1547                 ioctl(self->fd, VIDEO_SLOWMOTION, 0);
1548                 ioctl(self->fd, VIDEO_FAST_FORWARD, 0);
1549                 ioctl(self->fd, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_DEMUX);
1550                 close(self->fd);
1551         }
1552
1553         if (self->codec_data)
1554                 gst_buffer_unref(self->codec_data);
1555
1556         if (self->h264_buffer)
1557                 gst_buffer_unref(self->h264_buffer);
1558
1559 #ifdef PACK_UNPACKED_XVID_DIVX5_BITSTREAM
1560         if (self->prev_frame)
1561                 gst_buffer_unref(self->prev_frame);
1562 #endif
1563
1564         while(self->queue)
1565                 queue_pop(&self->queue);
1566
1567         if (f) {
1568                 fputs(self->saved_fallback_framerate, f);
1569                 fclose(f);
1570         }
1571
1572         close (READ_SOCKET (self));
1573         close (WRITE_SOCKET (self));
1574         READ_SOCKET (self) = -1;
1575         WRITE_SOCKET (self) = -1;
1576
1577         return TRUE;
1578 }
1579
1580 static GstStateChangeReturn
1581 gst_dvbvideosink_change_state (GstElement * element, GstStateChange transition)
1582 {
1583         GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1584         GstDVBVideoSink *self = GST_DVBVIDEOSINK (element);
1585
1586         switch (transition) {
1587         case GST_STATE_CHANGE_NULL_TO_READY:
1588                 GST_DEBUG_OBJECT (self,"GST_STATE_CHANGE_NULL_TO_READY");
1589                 break;
1590         case GST_STATE_CHANGE_READY_TO_PAUSED:
1591                 GST_DEBUG_OBJECT (self,"GST_STATE_CHANGE_READY_TO_PAUSED");
1592
1593                 self->fd = open("/dev/dvb/adapter0/video0", O_RDWR|O_NONBLOCK);
1594 //              self->fd = open("/dump.pes", O_RDWR|O_CREAT|O_TRUNC, 0555);
1595
1596                 GST_OBJECT_LOCK(self);
1597                 self->no_write |= 4;
1598                 GST_OBJECT_UNLOCK(self);
1599
1600                 if (self->fd >= 0) {
1601                         GstStructure *s = 0;
1602                         GstMessage *msg = 0;
1603                         int aspect = -1, width = -1, height = -1, framerate = -1,
1604                                 progressive = readMpegProc("progressive", 0);
1605
1606                         if (readApiSize(self->fd, &width, &height, &aspect) == -1) {
1607                                 aspect = readMpegProc("aspect", 0);
1608                                 width = readMpegProc("xres", 0);
1609                                 height = readMpegProc("yres", 0);
1610                         } else
1611                                 aspect = aspect == 0 ? 2 : 3; // dvb api to etsi
1612                         if (readApiFrameRate(self->fd, &framerate) == -1)
1613                                 framerate = readMpegProc("framerate", 0);
1614
1615                         s = gst_structure_new ("eventSizeAvail",
1616                                 "aspect_ratio", G_TYPE_INT, aspect == 0 ? 2 : 3,
1617                                 "width", G_TYPE_INT, width,
1618                                 "height", G_TYPE_INT, height, NULL);
1619                         msg = gst_message_new_element (GST_OBJECT (element), s);
1620                         gst_element_post_message (GST_ELEMENT (element), msg);
1621                         s = gst_structure_new ("eventFrameRateAvail",
1622                                 "frame_rate", G_TYPE_INT, framerate, NULL);
1623                         msg = gst_message_new_element (GST_OBJECT (element), s);
1624                         gst_element_post_message (GST_ELEMENT (element), msg);
1625                         s = gst_structure_new ("eventProgressiveAvail",
1626                                 "progressive", G_TYPE_INT, progressive, NULL);
1627                         msg = gst_message_new_element (GST_OBJECT (element), s);
1628                         gst_element_post_message (GST_ELEMENT (element), msg);
1629                         ioctl(self->fd, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_MEMORY);
1630                         ioctl(self->fd, VIDEO_FREEZE);
1631                 }
1632                 break;
1633         case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1634                 GST_DEBUG_OBJECT (self,"GST_STATE_CHANGE_PAUSED_TO_PLAYING");
1635                 ioctl(self->fd, VIDEO_CONTINUE);
1636                 GST_OBJECT_LOCK(self);
1637                 self->no_write &= ~4;
1638                 GST_OBJECT_UNLOCK(self);
1639                 break;
1640         default:
1641                 break;
1642         }
1643
1644         ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1645
1646         switch (transition) {
1647         case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1648                 GST_DEBUG_OBJECT (self,"GST_STATE_CHANGE_PLAYING_TO_PAUSED");
1649                 GST_OBJECT_LOCK(self);
1650                 self->no_write |= 4;
1651                 GST_OBJECT_UNLOCK(self);
1652                 ioctl(self->fd, VIDEO_FREEZE);
1653                 SEND_COMMAND (self, CONTROL_STOP);
1654                 break;
1655         case GST_STATE_CHANGE_PAUSED_TO_READY:
1656                 GST_DEBUG_OBJECT (self,"GST_STATE_CHANGE_PAUSED_TO_READY");
1657                 break;
1658         case GST_STATE_CHANGE_READY_TO_NULL:
1659                 GST_DEBUG_OBJECT (self,"GST_STATE_CHANGE_READY_TO_NULL");
1660                 break;
1661         default:
1662                 break;
1663         }
1664
1665         return ret;
1666 }
1667
1668 /* entry point to initialize the plug-in
1669  * initialize the plug-in itself
1670  * register the element factories and pad templates
1671  * register the features
1672  *
1673  * exchange the string 'plugin' with your elemnt name
1674  */
1675 static gboolean
1676 plugin_init (GstPlugin *plugin)
1677 {
1678         return gst_element_register (plugin, "dvbvideosink",
1679                                                  GST_RANK_PRIMARY,
1680                                                  GST_TYPE_DVBVIDEOSINK);
1681 }
1682
1683 /* this is the structure that gstreamer looks for to register plugins
1684  *
1685  * exchange the strings 'plugin' and 'Template plugin' with you plugin name and
1686  * description
1687  */
1688 GST_PLUGIN_DEFINE (
1689         GST_VERSION_MAJOR,
1690         GST_VERSION_MINOR,
1691         "dvb_video_out",
1692         "DVB Video Output",
1693         plugin_init,
1694         VERSION,
1695         "LGPL",
1696         "GStreamer",
1697         "http://gstreamer.net/"
1698 )