fix no more working teletext / ac3
[enigma2.git] / lib / dvb / decoder.cpp
index 544a41f0bde9438759480107aaf2d6170075cb4f..71b3f39e055d916d2d6c570133b89c04a508b673 100644 (file)
@@ -26,6 +26,8 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 #include <errno.h>
 
        /* these are quite new... */
@@ -36,7 +38,8 @@
 
 DEFINE_REF(eDVBAudio);
 
-eDVBAudio::eDVBAudio(eDVBDemux *demux, int dev): m_demux(demux), m_dev(dev)
+eDVBAudio::eDVBAudio(eDVBDemux *demux, int dev)
+       :m_demux(demux), m_dev(dev), m_is_freezed(0)
 {
        char filename[128];
 #if HAVE_DVB_API_VERSION < 3
@@ -57,10 +60,32 @@ eDVBAudio::eDVBAudio(eDVBDemux *demux, int dev): m_demux(demux), m_dev(dev)
                eWarning("%s: %m", filename);
 }
 
-int eDVBAudio::startPid(int pid, int type)
+#if HAVE_DVB_API_VERSION < 3
+int eDVBAudio::setPid(int pid, int type)
 {
        if ((m_fd < 0) || (m_fd_demux < 0))
                return -1;
+
+       int bypass = 0;
+
+       switch (type)
+       {
+       case aMPEG:
+               bypass = 1;
+               break;
+       case aAC3:
+               bypass = 0;
+               break;
+               /*
+       case aDTS:
+               bypass = 2;
+               break;
+               */
+       }
+
+       if (::ioctl(m_fd, AUDIO_SET_BYPASS_MODE, bypass) < 0)
+               eDebug("failed (%m)");
+
        dmx_pes_filter_params pes;
 
        pes.pid      = pid;
@@ -68,17 +93,90 @@ int eDVBAudio::startPid(int pid, int type)
        pes.output   = DMX_OUT_DECODER;
        pes.pes_type = m_dev ? DMX_PES_AUDIO1 : DMX_PES_AUDIO0; /* FIXME */
        pes.flags    = 0;
+       eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - audio - ", pid);
        if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
        {
-               eWarning("audio: DMX_SET_PES_FILTER: %m");
+               eDebug("failed (%m)");
                return -errno;
        }
+       eDebug("ok");
+
+       return 0;
+}
+
+int eDVBAudio::startPid()
+{
+       eDebugNoNewLine("DEMUX_START - audio - ");
        if (::ioctl(m_fd_demux, DMX_START) < 0)
        {
-               eWarning("audio: DMX_START: %m");
+               eDebug("failed (%m)");
                return -errno;
        }
+       eDebug("ok");
+       return 0;
+}
 
+int eDVBAudio::start()
+{
+       eDebugNoNewLine("AUDIO_PLAY - ");
+       if (::ioctl(m_fd, AUDIO_PLAY) < 0)
+       {
+               eDebug("failed (%m)");
+               return -errno;
+       }
+       eDebug("ok");
+       return 0;
+}
+
+int eDVBAudio::stopPid()
+{
+       eDebugNoNewLine("DEMUX_STOP - audio - ");
+       if (::ioctl(m_fd_demux, DMX_STOP) < 0)
+       {
+               eDebug("failed (%m)");
+               return -errno;
+       }
+       eDebug("ok");
+       return 0;
+}
+
+int eDVBAudio::setAVSync(int val)
+{
+       eDebugNoNewLine("AUDIO_SET_AV_SYNC - ");
+       if (::ioctl(m_fd, AUDIO_SET_AV_SYNC, val) < 0)
+       {
+               eDebug("failed (%m)");
+               return -errno;
+       }
+       eDebug("ok");
+       return 0;
+}
+#else
+int eDVBAudio::startPid(int pid, int type)
+{
+       if ((m_fd < 0) || (m_fd_demux < 0))
+               return -1;
+       dmx_pes_filter_params pes;
+
+       pes.pid      = pid;
+       pes.input    = DMX_IN_FRONTEND;
+       pes.output   = DMX_OUT_DECODER;
+       pes.pes_type = m_dev ? DMX_PES_AUDIO1 : DMX_PES_AUDIO0; /* FIXME */
+       pes.flags    = 0;
+       eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - audio - ", pid);
+       if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
+       {
+               eDebug("failed (%m)");
+               return -errno;
+       }
+       eDebug("ok");
+       eDebugNoNewLine("DEMUX_START - audio - ");
+       if (::ioctl(m_fd_demux, DMX_START) < 0)
+       {
+               eDebug("failed (%m)");
+               return -errno;
+       }
+       eDebug("ok");
        int bypass = 0;
 
        switch (type)
@@ -96,50 +194,74 @@ int eDVBAudio::startPid(int pid, int type)
                */
        }
 
+       eDebugNoNewLine("AUDIO_SET_BYPASS - ");
        if (::ioctl(m_fd, AUDIO_SET_BYPASS_MODE, bypass) < 0)
-               eWarning("audio: AUDIO_SET_BYPASS_MODE: %m");
-
+               eDebug("failed (%m)");
+       else
+               eDebug("ok");
        freeze();
 
+       eDebugNoNewLine("AUDIO_PLAY - ");
        if (::ioctl(m_fd, AUDIO_PLAY) < 0)
-               eWarning("audio: AUDIO_PLAY: %m");
+               eDebug("failed (%m)");
+       else
+               eDebug("ok");
        return 0;
 }
+#endif
 
 void eDVBAudio::stop()
 {
+#if HAVE_DVB_API_VERSION > 2
+       flush();
+#endif
+       eDebugNoNewLine("AUDIO_STOP - ");
        if (::ioctl(m_fd, AUDIO_STOP) < 0)
-               eWarning("audio: AUDIO_STOP: %m");
+               eDebug("failed (%m)");
+       else
+               eDebug("ok");
 #if HAVE_DVB_API_VERSION > 2
+       eDebugNoNewLine("DEMUX_STOP - audio - ");
        if (::ioctl(m_fd_demux, DMX_STOP) < 0)
-               eWarning("audio: DMX_STOP: %m");
+               eDebug("failed (%m)");
+       else
+               eDebug("ok");
 #endif
 }
 
-#if HAVE_DVB_API_VERSION < 3
-void eDVBAudio::stopPid()
-{
-       if (::ioctl(m_fd_demux, DMX_STOP) < 0)
-               eWarning("audio: DMX_STOP: %m");
-}
-#endif
-
 void eDVBAudio::flush()
 {
+       eDebugNoNewLine("AUDIO_CLEAR_BUFFER - ");
        if (::ioctl(m_fd, AUDIO_CLEAR_BUFFER) < 0)
-               eDebug("audio: AUDIO_CLEAR_BUFFER: %m");
+               eDebug("failed (%m)");
+       else
+               eDebug("ok");
 }
 
 void eDVBAudio::freeze()
 {
-       if (::ioctl(m_fd, AUDIO_PAUSE) < 0)
-               eDebug("video: AUDIO_PAUSE: %m");
+       if (!m_is_freezed)
+       {
+               eDebugNoNewLine("AUDIO_PAUSE - ");
+               if (::ioctl(m_fd, AUDIO_PAUSE) < 0)
+                       eDebug("failed (%m)");
+               else
+                       eDebug("ok");
+               m_is_freezed=1;
+       }
 }
 
 void eDVBAudio::unfreeze()
 {
-       if (::ioctl(m_fd, AUDIO_CONTINUE) < 0)
-               eDebug("video: AUDIO_CONTINUE: %m");
+       if (m_is_freezed)
+       {
+               eDebugNoNewLine("AUDIO_CONTINUE - ");
+               if (::ioctl(m_fd, AUDIO_CONTINUE) < 0)
+                       eDebug("failed (%m)");
+               else
+                       eDebug("ok");
+               m_is_freezed=0;
+       }
 }
 
 void eDVBAudio::setChannel(int channel)
@@ -151,13 +273,18 @@ void eDVBAudio::setChannel(int channel)
        case aMonoRight: val = AUDIO_MONO_RIGHT; break;
        default: break;
        }
+       eDebugNoNewLine("AUDIO_CHANNEL_SELECT(%d) - ", val);
        if (::ioctl(m_fd, AUDIO_CHANNEL_SELECT, val) < 0)
-               eDebug("video: AUDIO_CHANNEL_SELECT: %m");
+               eDebug("failed (%m)");
+       else
+               eDebug("ok");
 }
 
 int eDVBAudio::getPTS(pts_t &now)
 {
-       return ::ioctl(m_fd, AUDIO_GET_PTS, &now);
+       if (::ioctl(m_fd, AUDIO_GET_PTS, &now) < 0)
+               eDebug("AUDIO_GET_PTS failed (%m)");
+       return 0;
 }
 
 eDVBAudio::~eDVBAudio()
@@ -171,11 +298,15 @@ eDVBAudio::~eDVBAudio()
 
 DEFINE_REF(eDVBVideo);
 
-eDVBVideo::eDVBVideo(eDVBDemux *demux, int dev): m_demux(demux), m_dev(dev)
+eDVBVideo::eDVBVideo(eDVBDemux *demux, int dev)
+       :m_demux(demux), m_dev(dev), m_is_slow_motion(0), m_is_fast_forward(0), m_is_freezed(0)
 {
        char filename[128];
 #if HAVE_DVB_API_VERSION < 3
        sprintf(filename, "/dev/dvb/card%d/video%d", demux->adapter, dev);
+       m_fd_video = ::open("/dev/video", O_RDWR);
+       if (m_fd_video < 0)
+               eWarning("/dev/video: %m");
 #else
        sprintf(filename, "/dev/dvb/adapter%d/video%d", demux->adapter, dev);
 #endif
@@ -203,94 +334,195 @@ eDVBVideo::eDVBVideo(eDVBDemux *demux, int dev): m_demux(demux), m_dev(dev)
 #define VIDEO_STREAMTYPE_MPEG2 0
 #define VIDEO_STREAMTYPE_MPEG4_H264 1
 
+#if HAVE_DVB_API_VERSION < 3
+int eDVBVideo::setPid(int pid)
+{
+       if ((m_fd < 0) || (m_fd_demux < 0))
+               return -1;
+       dmx_pes_filter_params pes;
+
+       pes.pid      = pid;
+       pes.input    = DMX_IN_FRONTEND;
+       pes.output   = DMX_OUT_DECODER;
+       pes.pes_type = m_dev ? DMX_PES_VIDEO1 : DMX_PES_VIDEO0; /* FIXME */
+       pes.flags    = 0;
+       eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - video - ", pid);
+       if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
+       {
+               eDebug("failed (%m)");
+               return -errno;
+       }
+       eDebug("ok");
+       return 0;
+}
+
+int eDVBVideo::startPid()
+{
+       eDebugNoNewLine("DEMUX_START - video - ");
+       if (::ioctl(m_fd_demux, DMX_START) < 0)
+       {
+               eDebug("failed (%m)");
+               return -errno;
+       }
+       eDebug("ok");
+       return 0;
+}
+
+int eDVBVideo::start()
+{
+       eDebugNoNewLine("VIDEO_PLAY - ");
+       if (::ioctl(m_fd, VIDEO_PLAY) < 0)
+       {
+               eDebug("failed (%m)");
+               return -errno;
+       }
+       eDebug("ok");
+       return 0;
+}
+
+int eDVBVideo::stopPid()
+{
+       eDebugNoNewLine("DEMUX_STOP - video - ");
+       if (::ioctl(m_fd_demux, DMX_STOP) < 0)
+       {
+               eDebug("failed (%m)");
+               return -errno;
+       }
+       eDebug("ok");
+       return 0;
+}
+#else
 int eDVBVideo::startPid(int pid, int type)
 {
        if ((m_fd < 0) || (m_fd_demux < 0))
                return -1;
        dmx_pes_filter_params pes;
 
+       eDebugNoNewLine("VIDEO_SET_STREAMTYPE %d - ",type == MPEG4_H264 ? VIDEO_STREAMTYPE_MPEG4_H264 : VIDEO_STREAMTYPE_MPEG2);
        if (::ioctl(m_fd, VIDEO_SET_STREAMTYPE,
                type == MPEG4_H264 ? VIDEO_STREAMTYPE_MPEG4_H264 : VIDEO_STREAMTYPE_MPEG2) < 0)
-               eWarning("video: VIDEO_SET_STREAMTYPE: %m");
+               eDebug("failed (%m)");
+       else
+               eDebug("ok");
 
        pes.pid      = pid;
        pes.input    = DMX_IN_FRONTEND;
        pes.output   = DMX_OUT_DECODER;
        pes.pes_type = m_dev ? DMX_PES_VIDEO1 : DMX_PES_VIDEO0; /* FIXME */
        pes.flags    = 0;
+       eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - video - ", pid);
        if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
        {
-               eWarning("video: DMX_SET_PES_FILTER: %m");
+               eDebug("failed (%m)");
                return -errno;
        }
+       eDebug("ok");
+       eDebugNoNewLine("DEMUX_START - video - ");
        if (::ioctl(m_fd_demux, DMX_START) < 0)
        {
-               eWarning("video: DMX_START: %m");
+               eDebug("failed (%m)");
                return -errno;
        }
-
-       eDebug("FREEZE.\n");
+       eDebug("ok");
        freeze();
-
+       eDebugNoNewLine("VIDEO_PLAY - ");
        if (::ioctl(m_fd, VIDEO_PLAY) < 0)
-               eWarning("video: VIDEO_PLAY: %m");
+               eDebug("failed (%m)");
+       else
+               eDebug("ok");
        return 0;
 }
+#endif
 
 void eDVBVideo::stop()
 {
 #if HAVE_DVB_API_VERSION > 2
+       eDebugNoNewLine("DEMUX_STOP - video - ");
        if (::ioctl(m_fd_demux, DMX_STOP) < 0)
-               eWarning("video: DMX_STOP: %m");
+               eDebug("failed (%m)");
+       else
+               eDebug("ok");
 #endif
-       eDebug("VIDEO_STOP");
+       eDebugNoNewLine("VIDEO_STOP - ");
        if (::ioctl(m_fd, VIDEO_STOP, 1) < 0)
-               eWarning("video: VIDEO_STOP: %m");
-}
-
-#if HAVE_DVB_API_VERSION < 3
-void eDVBVideo::stopPid()
-{
-       if (::ioctl(m_fd_demux, DMX_STOP) < 0)
-               eWarning("video: DMX_STOP: %m");
+               eDebug("failed (%m)");
+       else
+               eDebug("ok");
 }
-#endif
 
 void eDVBVideo::flush()
 {
+       eDebugNoNewLine("VIDEO_CLEAR_BUFFER - ");
        if (::ioctl(m_fd, VIDEO_CLEAR_BUFFER) < 0)
-               eDebug("video: VIDEO_CLEAR_BUFFER: %m");
+               eDebug("failed (%m)");
+       else
+               eDebug("ok");
 }
 
 void eDVBVideo::freeze()
 {
-       eDebug("VIDEO_FREEZE");
-       if (::ioctl(m_fd, VIDEO_FREEZE) < 0)
-               eDebug("video: VIDEO_FREEZE: %m");
+       if (!m_is_freezed)
+       {
+               eDebugNoNewLine("VIDEO_FREEZE - ");
+               if (::ioctl(m_fd, VIDEO_FREEZE) < 0)
+                       eDebug("failed (%m)");
+               else
+                       eDebug("ok");
+               m_is_freezed=1;
+       }
 }
 
 void eDVBVideo::unfreeze()
 {
-       eDebug("VIDEO_CONTINUE");
-       if (::ioctl(m_fd, VIDEO_CONTINUE) < 0)
-               eDebug("video: VIDEO_CONTINUE: %m");
+       if (m_is_freezed)
+       {
+               eDebugNoNewLine("VIDEO_CONTINUE - ");
+               if (::ioctl(m_fd, VIDEO_CONTINUE) < 0)
+                       eDebug("failed (%m)");
+               else
+                       eDebug("ok");
+               m_is_freezed=0;
+       }
 }
 
 int eDVBVideo::setSlowMotion(int repeat)
 {
+       eDebugNoNewLine("VIDEO_SLOWMOTION - ");
        m_is_slow_motion = repeat;
-       return ::ioctl(m_fd, VIDEO_SLOWMOTION, repeat);
+       int ret = ::ioctl(m_fd, VIDEO_SLOWMOTION, repeat);
+       if (ret < 0)
+               eDebug("failed(%m)");
+       else
+               eDebug("ok");
+       return ret;
 }
 
 int eDVBVideo::setFastForward(int skip)
 {
+       eDebugNoNewLine("VIDEO_FAST_FORWARD - ");
        m_is_fast_forward = skip;
-       return ::ioctl(m_fd, VIDEO_FAST_FORWARD, skip);
+       int ret = ::ioctl(m_fd, VIDEO_FAST_FORWARD, skip);
+       if (ret < 0)
+               eDebug("failed(%m)");
+       else
+               eDebug("ok");
+       return ret;
 }
 
 int eDVBVideo::getPTS(pts_t &now)
 {
-       return ::ioctl(m_fd, VIDEO_GET_PTS, &now);
+#if HAVE_DVB_API_VERSION < 3
+       #define VIDEO_GET_PTS_OLD           _IOR('o', 1, unsigned int*)
+       unsigned int pts;
+       int ret = ::ioctl(m_fd_video, VIDEO_GET_PTS_OLD, &pts);
+       now = pts;
+       now *= 2;
+#else
+       int ret = ::ioctl(m_fd, VIDEO_GET_PTS, &now);
+#endif
+       if (ret < 0)
+               eDebug("VIDEO_GET_PTS failed(%m)");
+       return ret;
 }
 
 eDVBVideo::~eDVBVideo()
@@ -306,16 +538,22 @@ eDVBVideo::~eDVBVideo()
                ::close(m_fd);
        if (m_fd_demux >= 0)
                ::close(m_fd_demux);
+#if HAVE_DVB_API_VERSION < 3
+       if (m_fd_video >= 0)
+               ::close(m_fd_video);
+#endif
 }
 
 void eDVBVideo::video_event(int)
 {
 #if HAVE_DVB_API_VERSION >= 3
        struct video_event evt;
+       eDebugNoNewLine("VIDEO_GET_EVENT - ");
        if (::ioctl(m_fd, VIDEO_GET_EVENT, &evt) < 0)
-               eDebug("VIDEO_GET_EVENT failed(%m)");
+               eDebug("failed (%m)");
        else
        {
+               eDebug("ok");
                if (evt.type == VIDEO_EVENT_SIZE_CHANGED)
                {
                        struct iTSMPEGDecoder::videoEvent event;
@@ -325,6 +563,20 @@ void eDVBVideo::video_event(int)
                        event.width = evt.u.size.w;
                        /* emit */ m_event(event);
                }
+               else if (evt.type == VIDEO_EVENT_FRAME_RATE_CHANGED)
+               {
+                       struct iTSMPEGDecoder::videoEvent event;
+                       event.type = iTSMPEGDecoder::videoEvent::eventFrameRateChanged;
+                       event.framerate = evt.u.frame_rate;
+                       /* emit */ m_event(event);
+               }
+               else if (evt.type == 16 /*VIDEO_EVENT_PROGRESSIVE_CHANGED*/)
+               {
+                       struct iTSMPEGDecoder::videoEvent event;
+                       event.type = iTSMPEGDecoder::videoEvent::eventProgressiveChanged;
+                       event.progressive = evt.u.frame_rate;
+                       /* emit */ m_event(event);
+               }
                else
                        eDebug("unhandled DVBAPI Video Event %d", evt.type);
        }
@@ -354,6 +606,43 @@ eDVBPCR::eDVBPCR(eDVBDemux *demux): m_demux(demux)
                eWarning("%s: %m", filename);
 }
 
+#if HAVE_DVB_API_VERSION < 3
+int eDVBPCR::setPid(int pid)
+{
+       if (m_fd_demux < 0)
+               return -1;
+       dmx_pes_filter_params pes;
+
+       pes.pid      = pid;
+       pes.input    = DMX_IN_FRONTEND;
+       pes.output   = DMX_OUT_DECODER;
+       pes.pes_type = DMX_PES_PCR;
+       pes.flags    = 0;
+
+       eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - pcr - ", pid);
+       if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
+       {
+               eDebug("failed (%m)");
+               return -errno;
+       }
+       eDebug("ok");
+       return 0;
+}
+
+int eDVBPCR::startPid()
+{
+       if (m_fd_demux < 0)
+               return -1;
+       eDebugNoNewLine("DEMUX_START - pcr - ");
+       if (::ioctl(m_fd_demux, DMX_START) < 0)
+       {
+               eDebug("failed (%m)");
+               return -errno;
+       }
+       eDebug("ok");
+       return 0;
+}
+#else
 int eDVBPCR::startPid(int pid)
 {
        if (m_fd_demux < 0)
@@ -365,23 +654,31 @@ int eDVBPCR::startPid(int pid)
        pes.output   = DMX_OUT_DECODER;
        pes.pes_type = DMX_PES_PCR;
        pes.flags    = 0;
+       eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - pcr - ", pid);
        if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
        {
-               eWarning("video: DMX_SET_PES_FILTER: %m");
+               eDebug("failed (%m)");
                return -errno;
        }
+       eDebug("ok");
+       eDebugNoNewLine("DEMUX_START - pcr - ");
        if (::ioctl(m_fd_demux, DMX_START) < 0)
        {
-               eWarning("video: DMX_START: %m");
+               eDebug("failed (%m)");
                return -errno;
        }
+       eDebug("ok");
        return 0;
 }
+#endif
 
 void eDVBPCR::stop()
 {
+       eDebugNoNewLine("DEMUX_STOP - pcr - ");
        if (::ioctl(m_fd_demux, DMX_STOP) < 0)
-               eWarning("video: DMX_STOP: %m");
+               eDebug("failed(%m)");
+       else
+               eDebug("ok");
 }
 
 eDVBPCR::~eDVBPCR()
@@ -416,23 +713,31 @@ int eDVBTText::startPid(int pid)
        pes.output   = DMX_OUT_DECODER;
        pes.pes_type = DMX_PES_TELETEXT;
        pes.flags    = 0;
+
+       eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - ttx - ", pid);
        if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
        {
-               eWarning("video: DMX_SET_PES_FILTER: %m");
+               eDebug("failed(%m)");
                return -errno;
        }
+       eDebug("ok");
+       eDebugNoNewLine("DEMUX_START - pcr - ");
        if (::ioctl(m_fd_demux, DMX_START) < 0)
        {
-               eWarning("video: DMX_START: %m");
+               eDebug("failed(%m)");
                return -errno;
        }
+       eDebug("ok");
        return 0;
 }
 
 void eDVBTText::stop()
 {
+       eDebugNoNewLine("DEMUX_STOP - ttx - ");
        if (::ioctl(m_fd_demux, DMX_STOP) < 0)
-               eWarning("video: DMX_STOP: %m");
+               eDebug("failed(%m)");
+       else
+               eDebug("ok");
 }
 
 eDVBTText::~eDVBTText()
@@ -458,6 +763,7 @@ int eTSMPEGDecoder::setState()
 
        bool changed = !!m_changed;
 #if HAVE_DVB_API_VERSION < 3
+       bool checkAVSync = m_changed & (changeAudio|changeVideo|changePCR);
        if (m_changed & changeAudio && m_audio)
                m_audio->stopPid();
        if (m_changed & changeVideo && m_video)
@@ -466,47 +772,64 @@ int eTSMPEGDecoder::setState()
        {
                m_pcr->stop();
                m_pcr=0;
+               if (!(m_pcrpid >= 0 && m_pcrpid < 0x1ff))
+                       m_changed &= ~changePCR;
        }
        if (m_changed & changeAudio && m_audio)
        {
                m_audio->stop();
                m_audio=0;
+               if (!(m_apid >= 0 && m_apid < 0x1ff))
+                       m_changed &= ~changeAudio;
        }
        if (m_changed & changeVideo && m_video)
        {
                m_video->stop();
                m_video=0;
                m_video_event_conn=0;
+               if (!(m_vpid >= 0 && m_vpid < 0x1ff))
+                       m_changed &= ~changeVideo;
+       }
+       if (m_changed & changeVideo)
+       {
+               m_video = new eDVBVideo(m_demux, m_decoder);
+               m_video->connectEvent(slot(*this, &eTSMPEGDecoder::video_event), m_video_event_conn);
+               if (m_video->setPid(m_vpid))
+                       res -1;
        }
        if (m_changed & changePCR)
        {
                m_pcr = new eDVBPCR(m_demux);
-               if (m_pcr->startPid(m_pcrpid))
-               {
-                       eWarning("pcr: startpid failed!");
+               if (m_pcr->setPid(m_pcrpid))
+                       res = -1;
+       }
+       if (m_changed & changeAudio)
+       {
+               m_audio = new eDVBAudio(m_demux, m_decoder);
+               if (m_audio->setPid(m_apid, m_atype))
+                       res = -1;
+       }
+       if (m_changed & changePCR)
+       {
+               if (m_pcr->startPid())
                        res = -1;
-               }
                m_changed &= ~changePCR;
        }
+       else if (checkAVSync && m_audio && m_video)
+       {
+               if (m_audio->setAVSync(1))
+                       res = -1;
+       }
        if (m_changed & changeVideo)
        {
-               m_video = new eDVBVideo(m_demux, m_decoder);
-               m_video->connectEvent(slot(*this, &eTSMPEGDecoder::video_event), m_video_event_conn);
-               if (m_video->startPid(m_vpid))
-               {
-                       eWarning("video: startpid failed!");
+               if (m_video->startPid() || m_video->start())
                        res = -1;
-               }
                m_changed &= ~changeVideo;
        }
        if (m_changed & changeAudio)
        {
-               m_audio = new eDVBAudio(m_demux, m_decoder);
-               if (m_audio->startPid(m_apid, m_atype))
-               {
-                       eWarning("audio: startpid failed!");
+               if (m_audio->start() || m_audio->startPid())
                        res = -1;
-               }
                m_changed &= ~changeAudio;
        }
 #else
@@ -519,10 +842,7 @@ int eTSMPEGDecoder::setState()
                {
                        m_pcr = new eDVBPCR(m_demux);
                        if (m_pcr->startPid(m_pcrpid))
-                       {
-                               eWarning("pcr: startpid failed!");
                                res = -1;
-                       }
                }
                m_changed &= ~changePCR;
        }
@@ -531,21 +851,16 @@ int eTSMPEGDecoder::setState()
                eDebug("VIDEO CHANGED (to %04x)", m_vpid);
                if (m_video)
                {
-                       eDebug("STOP");
                        m_video->stop();
                        m_video = 0;
                        m_video_event_conn = 0;
                }
                if ((m_vpid >= 0) && (m_vpid < 0x1FFF))
                {
-                       eDebug("new video");
                        m_video = new eDVBVideo(m_demux, m_decoder);
                        m_video->connectEvent(slot(*this, &eTSMPEGDecoder::video_event), m_video_event_conn);
                        if (m_video->startPid(m_vpid, m_vtype))
-                       {
-                               eWarning("video: startpid failed!");
                                res = -1;
-                       }
                }
                m_changed &= ~changeVideo;
        }
@@ -558,10 +873,7 @@ int eTSMPEGDecoder::setState()
                {
                        m_audio = new eDVBAudio(m_demux, m_decoder);
                        if (m_audio->startPid(m_apid, m_atype))
-                       {
-                               eWarning("audio: startpid failed!");
                                res = -1;
-                       }
                }
                m_changed &= ~changeAudio;
        }
@@ -574,10 +886,7 @@ int eTSMPEGDecoder::setState()
                {
                        m_text = new eDVBTText(m_demux);
                        if (m_text->startPid(m_textpid))
-                       {
-                               eWarning("text: startpid failed!");
                                res = -1;
-                       }
                }
                m_changed &= ~changeText;
        }
@@ -623,15 +932,18 @@ RESULT eTSMPEGDecoder::setAC3Delay(int delay)
        return -1;
 }
 
-eTSMPEGDecoder::eTSMPEGDecoder(eDVBDemux *demux, int decoder): m_demux(demux), m_changed(0), m_decoder(decoder)
+eTSMPEGDecoder::eTSMPEGDecoder(eDVBDemux *demux, int decoder)
+       :m_demux(demux), m_changed(0), m_decoder(decoder), m_video_clip_fd(-1), m_showSinglePicTimer(eApp)
 {
        demux->connectEvent(slot(*this, &eTSMPEGDecoder::demux_event), m_demux_event_conn);
+       CONNECT(m_showSinglePicTimer.timeout, eTSMPEGDecoder::finishShowSinglePic);
        m_is_ff = m_is_sm = m_is_trickmode = 0;
 }
 
 eTSMPEGDecoder::~eTSMPEGDecoder()
 {
-       m_vpid = m_apid = m_pcrpid = pidNone;
+       finishShowSinglePic();
+       m_vpid = m_apid = m_pcrpid = m_textpid = pidNone;
        m_changed = -1;
        setState();
 }
@@ -845,59 +1157,43 @@ RESULT eTSMPEGDecoder::showSinglePic(const char *filename)
 {
        if (m_decoder == 0)
        {
-               FILE *f = fopen(filename, "r");
-               if (f)
+               eDebug("showSinglePic %s", filename);
+               int f = open(filename, O_RDONLY);
+               if (f >= 0)
                {
-                       int vfd = open("/dev/dvb/adapter0/video0", O_RDWR);
-                       if (vfd > 0)
+                       struct stat s;
+                       fstat(f, &s);
+                       if (m_video_clip_fd == -1)
+                               m_video_clip_fd = open("/dev/dvb/adapter0/video0", O_WRONLY|O_NONBLOCK);
+                       if (m_video_clip_fd >= 0)
                        {
-                               fseek(f, 0, SEEK_END);
-                               int length = ftell(f);
-                               unsigned char *buffer = new unsigned char[length*2+9];
-                               if (ioctl(vfd, VIDEO_FAST_FORWARD, 1) < 0)
-                                       eDebug("VIDEO_FAST_FORWARD failed (%m)");
-                               if (ioctl(vfd, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_MEMORY) < 0)
+                               bool seq_end_avail = false;
+                               size_t pos=0;
+                               unsigned char pes_header[] = { 0x00, 0x00, 0x01, 0xE0, 0x00, 0x00, 0x80, 0x00, 0x00 };
+                               unsigned char seq_end[] = { 0x00, 0x00, 0x01, 0xB7 };
+                               unsigned char iframe[s.st_size];
+                               unsigned char stuffing[8192];
+                               memset(stuffing, 0, 8192);
+                               read(f, iframe, s.st_size);
+                               if (ioctl(m_video_clip_fd, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_MEMORY) < 0)
                                        eDebug("VIDEO_SELECT_SOURCE MEMORY failed (%m)");
-                               if (ioctl(vfd, VIDEO_PLAY) < 0)
+                               if (ioctl(m_video_clip_fd, VIDEO_PLAY) < 0)
                                        eDebug("VIDEO_PLAY failed (%m)");
-                               int cnt=0;
-                               int pos=0;
-                               while(cnt<2)
-                               {
-                                       int rd;
-                                       fseek(f, 0, SEEK_SET);
-                                       if (!cnt)
-                                       {
-                                               buffer[pos++]=0;
-                                               buffer[pos++]=0;
-                                               buffer[pos++]=1;
-                                               buffer[pos++]=0xE0;
-                                               buffer[pos++]=(length*2)>>8;
-                                               buffer[pos++]=(length*2)&0xFF;
-                                               buffer[pos++]=0x80;
-                                               buffer[pos++]=0;
-                                               buffer[pos++]=0;
-                                       }
-                                       while(1)
-                                       {
-                                               rd = fread(buffer+pos, 1, length, f);
-                                               if (rd > 0)
-                                                       pos += rd;
-                                               else
-                                                       break;
-                                       }
-                                       ++cnt;
-                               }
-                               write(vfd, buffer, pos);
-                               usleep(75000);  // i dont like this.. but i dont have a better solution :(
-                               if (ioctl(vfd, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_DEMUX) < 0)
-                                       eDebug("VIDEO_SELECT_SOURCE DEMUX failed (%m)");
-                               if (ioctl(vfd, VIDEO_FAST_FORWARD, 0) < 0)
-                                       eDebug("VIDEO_FAST_FORWARD failed (%m)");
-                               close(vfd);
-                               delete [] buffer;
+                               if (::ioctl(m_video_clip_fd, VIDEO_CONTINUE) < 0)
+                                       eDebug("video: VIDEO_CONTINUE: %m");
+                               if (::ioctl(m_video_clip_fd, VIDEO_CLEAR_BUFFER) < 0)
+                                       eDebug("video: VIDEO_CLEAR_BUFFER: %m");
+                               while(pos <= (s.st_size-4) && !(seq_end_avail = (!iframe[pos] && !iframe[pos+1] && iframe[pos+2] == 1 && iframe[pos+3] == 0xB7)))
+                                       ++pos;
+                               if ((iframe[3] >> 4) != 0xE) // no pes header
+                                       write(m_video_clip_fd, pes_header, sizeof(pes_header));
+                               write(m_video_clip_fd, iframe, s.st_size);
+                               if (!seq_end_avail)
+                                       write(m_video_clip_fd, seq_end, sizeof(seq_end));
+                               write(m_video_clip_fd, stuffing, 8192);
+                               m_showSinglePicTimer.start(150, true);
                        }
-                       fclose(f);
+                       close(f);
                }
                else
                {
@@ -913,6 +1209,19 @@ RESULT eTSMPEGDecoder::showSinglePic(const char *filename)
        return 0;
 }
 
+void eTSMPEGDecoder::finishShowSinglePic()
+{
+       if (m_video_clip_fd >= 0)
+       {
+               if (ioctl(m_video_clip_fd, VIDEO_STOP, 0) < 0)
+                       eDebug("VIDEO_STOP failed (%m)");
+               if (ioctl(m_video_clip_fd, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_DEMUX) < 0)
+                               eDebug("VIDEO_SELECT_SOURCE DEMUX failed (%m)");
+               close(m_video_clip_fd);
+               m_video_clip_fd = -1;
+       }
+}
+
 RESULT eTSMPEGDecoder::connectVideoEvent(const Slot1<void, struct videoEvent> &event, ePtr<eConnection> &conn)
 {
        conn = new eConnection(this, m_video_event.connect(event));