[DVDBurn] fix up title list layout and change some more strings (add #316)
[enigma2.git] / lib / dvb / decoder.cpp
index 511f91de1f81720a6f714dcc9d086cfb98cd85b0..ef8dadc387bdcb59b306ab573d83ebe69e836b31 100644 (file)
 #define dmxPesFilterParams dmx_pes_filter_params
 #define DMX_PES_VIDEO0 DMX_PES_VIDEO
 #define DMX_PES_AUDIO0 DMX_PES_AUDIO
 #define dmxPesFilterParams dmx_pes_filter_params
 #define DMX_PES_VIDEO0 DMX_PES_VIDEO
 #define DMX_PES_AUDIO0 DMX_PES_AUDIO
+#define DMX_PES_PCR0 DMX_PES_PCR
+#define DMX_PES_TELETEXT0 DMX_PES_TELETEXT
 #define DMX_PES_VIDEO1 DMX_PES_VIDEO
 #define DMX_PES_AUDIO1 DMX_PES_AUDIO
 #define DMX_PES_VIDEO1 DMX_PES_VIDEO
 #define DMX_PES_AUDIO1 DMX_PES_AUDIO
+#define DMX_PES_PCR1 DMX_PES_PCR
+#define DMX_PES_TELETEXT1 DMX_PES_TELETEXT
 #include <ost/dmx.h>
 #include <ost/video.h>
 #include <ost/audio.h>
 #include <ost/dmx.h>
 #include <ost/video.h>
 #include <ost/audio.h>
@@ -39,7 +43,7 @@
 DEFINE_REF(eDVBAudio);
 
 eDVBAudio::eDVBAudio(eDVBDemux *demux, int dev)
 DEFINE_REF(eDVBAudio);
 
 eDVBAudio::eDVBAudio(eDVBDemux *demux, int dev)
-       :m_demux(demux), m_dev(dev), m_is_freezed(0)
+       :m_demux(demux), m_dev(dev)
 {
        char filename[128];
 #if HAVE_DVB_API_VERSION < 3
 {
        char filename[128];
 #if HAVE_DVB_API_VERSION < 3
@@ -187,20 +191,26 @@ int eDVBAudio::startPid(int pid, int type)
        case aAC3:
                bypass = 0;
                break;
        case aAC3:
                bypass = 0;
                break;
-               /*
        case aDTS:
                bypass = 2;
                break;
        case aDTS:
                bypass = 2;
                break;
-               */
+       case aAAC:
+               bypass = 8;
+               break;
+       case aAACHE:
+               bypass = 9;
+               break;
+       case aLPCM:
+               bypass = 6;
+               break;
        }
 
        }
 
-       eDebugNoNewLine("AUDIO_SET_BYPASS - ");
+       eDebugNoNewLine("AUDIO_SET_BYPASS(%d) - ", bypass);
        if (::ioctl(m_fd, AUDIO_SET_BYPASS_MODE, bypass) < 0)
                eDebug("failed (%m)");
        else
                eDebug("ok");
        if (::ioctl(m_fd, AUDIO_SET_BYPASS_MODE, bypass) < 0)
                eDebug("failed (%m)");
        else
                eDebug("ok");
-       freeze();
-
+       freeze();  // why freeze here?!? this is a problem when only a pid change is requested... because of the unfreeze logic in Decoder::setState
        eDebugNoNewLine("AUDIO_PLAY - ");
        if (::ioctl(m_fd, AUDIO_PLAY) < 0)
                eDebug("failed (%m)");
        eDebugNoNewLine("AUDIO_PLAY - ");
        if (::ioctl(m_fd, AUDIO_PLAY) < 0)
                eDebug("failed (%m)");
@@ -212,9 +222,6 @@ int eDVBAudio::startPid(int pid, int type)
 
 void eDVBAudio::stop()
 {
 
 void eDVBAudio::stop()
 {
-#if HAVE_DVB_API_VERSION > 2
-       flush();
-#endif
        eDebugNoNewLine("AUDIO_STOP - ");
        if (::ioctl(m_fd, AUDIO_STOP) < 0)
                eDebug("failed (%m)");
        eDebugNoNewLine("AUDIO_STOP - ");
        if (::ioctl(m_fd, AUDIO_STOP) < 0)
                eDebug("failed (%m)");
@@ -240,28 +247,20 @@ void eDVBAudio::flush()
 
 void eDVBAudio::freeze()
 {
 
 void eDVBAudio::freeze()
 {
-       if (!m_is_freezed)
-       {
-               eDebugNoNewLine("AUDIO_PAUSE - ");
-               if (::ioctl(m_fd, AUDIO_PAUSE) < 0)
-                       eDebug("failed (%m)");
-               else
-                       eDebug("ok");
-               m_is_freezed=1;
-       }
+       eDebugNoNewLine("AUDIO_PAUSE - ");
+       if (::ioctl(m_fd, AUDIO_PAUSE) < 0)
+               eDebug("failed (%m)");
+       else
+               eDebug("ok");
 }
 
 void eDVBAudio::unfreeze()
 {
 }
 
 void eDVBAudio::unfreeze()
 {
-       if (m_is_freezed)
-       {
-               eDebugNoNewLine("AUDIO_CONTINUE - ");
-               if (::ioctl(m_fd, AUDIO_CONTINUE) < 0)
-                       eDebug("failed (%m)");
-               else
-                       eDebug("ok");
-               m_is_freezed=0;
-       }
+       eDebugNoNewLine("AUDIO_CONTINUE - ");
+       if (::ioctl(m_fd, AUDIO_CONTINUE) < 0)
+               eDebug("failed (%m)");
+       else
+               eDebug("ok");
 }
 
 void eDVBAudio::setChannel(int channel)
 }
 
 void eDVBAudio::setChannel(int channel)
@@ -282,15 +281,14 @@ void eDVBAudio::setChannel(int channel)
 
 int eDVBAudio::getPTS(pts_t &now)
 {
 
 int eDVBAudio::getPTS(pts_t &now)
 {
-       eDebugNoNewLine("AUDIO_GET_PTS - ");
        if (::ioctl(m_fd, AUDIO_GET_PTS, &now) < 0)
        if (::ioctl(m_fd, AUDIO_GET_PTS, &now) < 0)
-               eDebug("failed (%m)");
+               eDebug("AUDIO_GET_PTS failed (%m)");
        return 0;
 }
 
 eDVBAudio::~eDVBAudio()
 {
        return 0;
 }
 
 eDVBAudio::~eDVBAudio()
 {
-       unfreeze();
+       unfreeze();  // why unfreeze here... but not unfreeze video in ~eDVBVideo ?!?
        if (m_fd >= 0)
                ::close(m_fd);
        if (m_fd_demux >= 0)
        if (m_fd >= 0)
                ::close(m_fd);
        if (m_fd_demux >= 0)
@@ -300,7 +298,8 @@ eDVBAudio::~eDVBAudio()
 DEFINE_REF(eDVBVideo);
 
 eDVBVideo::eDVBVideo(eDVBDemux *demux, int dev)
 DEFINE_REF(eDVBVideo);
 
 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)
+       : m_demux(demux), m_dev(dev),
+       m_width(-1), m_height(-1), m_framerate(-1), m_aspect(-1), m_progressive(-1)
 {
        char filename[128];
 #if HAVE_DVB_API_VERSION < 3
 {
        char filename[128];
 #if HAVE_DVB_API_VERSION < 3
@@ -316,7 +315,7 @@ eDVBVideo::eDVBVideo(eDVBDemux *demux, int dev)
                eWarning("%s: %m", filename);
        else
        {
                eWarning("%s: %m", filename);
        else
        {
-               m_sn = new eSocketNotifier(eApp, m_fd, eSocketNotifier::Priority);
+               m_sn = eSocketNotifier::create(eApp, m_fd, eSocketNotifier::Priority);
                CONNECT(m_sn->activated, eDVBVideo::video_event);
        }
        eDebug("Video Device: %s", filename);
                CONNECT(m_sn->activated, eDVBVideo::video_event);
        }
        eDebug("Video Device: %s", filename);
@@ -334,6 +333,10 @@ eDVBVideo::eDVBVideo(eDVBDemux *demux, int dev)
 // not finally values i think.. !!
 #define VIDEO_STREAMTYPE_MPEG2 0
 #define VIDEO_STREAMTYPE_MPEG4_H264 1
 // not finally values i think.. !!
 #define VIDEO_STREAMTYPE_MPEG2 0
 #define VIDEO_STREAMTYPE_MPEG4_H264 1
+#define VIDEO_STREAMTYPE_VC1 3
+#define VIDEO_STREAMTYPE_MPEG4_Part2 4
+#define VIDEO_STREAMTYPE_VC1_SM 5
+#define VIDEO_STREAMTYPE_MPEG1 6
 
 #if HAVE_DVB_API_VERSION < 3
 int eDVBVideo::setPid(int pid)
 
 #if HAVE_DVB_API_VERSION < 3
 int eDVBVideo::setPid(int pid)
@@ -395,13 +398,36 @@ int eDVBVideo::stopPid()
 #else
 int eDVBVideo::startPid(int pid, int type)
 {
 #else
 int eDVBVideo::startPid(int pid, int type)
 {
+       int streamtype = VIDEO_STREAMTYPE_MPEG2;
+
        if ((m_fd < 0) || (m_fd_demux < 0))
                return -1;
        dmx_pes_filter_params pes;
 
        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)
+       switch(type)
+       {
+       default:
+       case MPEG2:
+               break;
+       case MPEG4_H264:
+               streamtype = VIDEO_STREAMTYPE_MPEG4_H264;
+               break;
+       case MPEG1:
+               streamtype = VIDEO_STREAMTYPE_MPEG1;
+               break;
+       case MPEG4_Part2:
+               streamtype = VIDEO_STREAMTYPE_MPEG4_Part2;
+               break;
+       case VC1:
+               streamtype = VIDEO_STREAMTYPE_VC1;
+               break;
+       case VC1_SM:
+               streamtype = VIDEO_STREAMTYPE_VC1_SM;
+               break;
+       }
+
+       eDebugNoNewLine("VIDEO_SET_STREAMTYPE %d - ", streamtype);
+       if (::ioctl(m_fd, VIDEO_SET_STREAMTYPE, streamtype) < 0)
                eDebug("failed (%m)");
        else
                eDebug("ok");
                eDebug("failed (%m)");
        else
                eDebug("ok");
@@ -425,7 +451,7 @@ int eDVBVideo::startPid(int pid, int type)
                return -errno;
        }
        eDebug("ok");
                return -errno;
        }
        eDebug("ok");
-       freeze();
+       freeze();  // why freeze here?!? this is a problem when only a pid change is requested... because of the unfreeze logic in Decoder::setState
        eDebugNoNewLine("VIDEO_PLAY - ");
        if (::ioctl(m_fd, VIDEO_PLAY) < 0)
                eDebug("failed (%m)");
        eDebugNoNewLine("VIDEO_PLAY - ");
        if (::ioctl(m_fd, VIDEO_PLAY) < 0)
                eDebug("failed (%m)");
@@ -462,34 +488,25 @@ void eDVBVideo::flush()
 
 void eDVBVideo::freeze()
 {
 
 void eDVBVideo::freeze()
 {
-       if (!m_is_freezed)
-       {
-               eDebugNoNewLine("VIDEO_FREEZE - ");
-               if (::ioctl(m_fd, VIDEO_FREEZE) < 0)
-                       eDebug("failed (%m)");
-               else
-                       eDebug("ok");
-               m_is_freezed=1;
-       }
+       eDebugNoNewLine("VIDEO_FREEZE - ");
+       if (::ioctl(m_fd, VIDEO_FREEZE) < 0)
+               eDebug("failed (%m)");
+       else
+               eDebug("ok");
 }
 
 void eDVBVideo::unfreeze()
 {
 }
 
 void eDVBVideo::unfreeze()
 {
-       if (m_is_freezed)
-       {
-               eDebugNoNewLine("VIDEO_CONTINUE - ");
-               if (::ioctl(m_fd, VIDEO_CONTINUE) < 0)
-                       eDebug("failed (%m)");
-               else
-                       eDebug("ok");
-               m_is_freezed=0;
-       }
+       eDebugNoNewLine("VIDEO_CONTINUE - ");
+       if (::ioctl(m_fd, VIDEO_CONTINUE) < 0)
+               eDebug("failed (%m)");
+       else
+               eDebug("ok");
 }
 
 int eDVBVideo::setSlowMotion(int repeat)
 {
 }
 
 int eDVBVideo::setSlowMotion(int repeat)
 {
-       eDebugNoNewLine("VIDEO_SLOWMOTION - ");
-       m_is_slow_motion = repeat;
+       eDebugNoNewLine("VIDEO_SLOWMOTION(%d) - ", repeat);
        int ret = ::ioctl(m_fd, VIDEO_SLOWMOTION, repeat);
        if (ret < 0)
                eDebug("failed(%m)");
        int ret = ::ioctl(m_fd, VIDEO_SLOWMOTION, repeat);
        if (ret < 0)
                eDebug("failed(%m)");
@@ -500,8 +517,7 @@ int eDVBVideo::setSlowMotion(int repeat)
 
 int eDVBVideo::setFastForward(int skip)
 {
 
 int eDVBVideo::setFastForward(int skip)
 {
-       eDebugNoNewLine("VIDEO_FAST_FORWARD - ");
-       m_is_fast_forward = skip;
+       eDebugNoNewLine("VIDEO_FAST_FORWARD(%d) - ", skip);
        int ret = ::ioctl(m_fd, VIDEO_FAST_FORWARD, skip);
        if (ret < 0)
                eDebug("failed(%m)");
        int ret = ::ioctl(m_fd, VIDEO_FAST_FORWARD, skip);
        if (ret < 0)
                eDebug("failed(%m)");
@@ -528,13 +544,6 @@ int eDVBVideo::getPTS(pts_t &now)
 
 eDVBVideo::~eDVBVideo()
 {
 
 eDVBVideo::~eDVBVideo()
 {
-       if (m_sn)
-               delete m_sn;
-       if (m_is_slow_motion)
-               setSlowMotion(0);
-       if (m_is_fast_forward)
-               setFastForward(0);
-       unfreeze();
        if (m_fd >= 0)
                ::close(m_fd);
        if (m_fd_demux >= 0)
        if (m_fd >= 0)
                ::close(m_fd);
        if (m_fd_demux >= 0)
@@ -559,9 +568,23 @@ void eDVBVideo::video_event(int)
                {
                        struct iTSMPEGDecoder::videoEvent event;
                        event.type = iTSMPEGDecoder::videoEvent::eventSizeChanged;
                {
                        struct iTSMPEGDecoder::videoEvent event;
                        event.type = iTSMPEGDecoder::videoEvent::eventSizeChanged;
-                       event.aspect = evt.u.size.aspect_ratio;
-                       event.height = evt.u.size.h;
-                       event.width = evt.u.size.w;
+                       m_aspect = event.aspect = evt.u.size.aspect_ratio == 0 ? 2 : 3;  // convert dvb api to etsi
+                       m_height = event.height = evt.u.size.h;
+                       m_width = 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;
+                       m_framerate = 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;
+                       m_progressive = event.progressive = evt.u.frame_rate;
                        /* emit */ m_event(event);
                }
                else
                        /* emit */ m_event(event);
                }
                else
@@ -578,9 +601,96 @@ RESULT eDVBVideo::connectEvent(const Slot1<void, struct iTSMPEGDecoder::videoEve
        return 0;
 }
 
        return 0;
 }
 
+static int readMpegProc(char *str, int decoder)
+{
+       int val = -1;
+       char tmp[64];
+       sprintf(tmp, "/proc/stb/vmpeg/%d/%s", decoder, str);
+       FILE *f = fopen(tmp, "r");
+       if (f)
+       {
+               fscanf(f, "%x", &val);
+               fclose(f);
+       }
+       return val;
+}
+
+static int readApiSize(int fd, int &xres, int &yres, int &aspect)
+{
+#if HAVE_DVB_API_VERSION >= 3
+       video_size_t size;
+       if (!::ioctl(fd, VIDEO_GET_SIZE, &size))
+       {
+               xres = size.w;
+               yres = size.h;
+               aspect = size.aspect_ratio == 0 ? 2 : 3;  // convert dvb api to etsi
+               return 0;
+       }
+//     eDebug("VIDEO_GET_SIZE failed (%m)");
+#endif
+       return -1;
+}
+
+static int readApiFrameRate(int fd, int &framerate)
+{
+#if HAVE_DVB_API_VERSION >= 3
+       unsigned int frate;
+       if (!::ioctl(fd, VIDEO_GET_FRAME_RATE, &frate))
+       {
+               framerate = frate;      
+               return 0;
+       }
+//     eDebug("VIDEO_GET_FRAME_RATE failed (%m)");
+#endif
+       return -1;
+}
+
+int eDVBVideo::getWidth()
+{
+       if (m_width == -1)
+               readApiSize(m_fd, m_width, m_height, m_aspect);
+       if (m_width == -1)
+               m_width = readMpegProc("xres", m_dev);
+       return m_width;
+}
+
+int eDVBVideo::getHeight()
+{
+       if (m_height == -1)
+               readApiSize(m_fd, m_width, m_height, m_aspect);
+       if (m_height == -1)
+               m_height = readMpegProc("yres", m_dev);
+       return m_height;
+}
+
+int eDVBVideo::getAspect()
+{
+       if (m_aspect == -1)
+               readApiSize(m_fd, m_width, m_height, m_aspect);
+       if (m_aspect == -1)
+               m_aspect = readMpegProc("aspect", m_dev);
+       return m_aspect;
+}
+
+int eDVBVideo::getProgressive()
+{
+       if (m_progressive == -1)
+               m_progressive = readMpegProc("progressive", m_dev);
+       return m_progressive;
+}
+
+int eDVBVideo::getFrameRate()
+{
+       if (m_framerate == -1)
+               readApiFrameRate(m_fd, m_framerate);
+       if (m_framerate == -1)
+               m_framerate = readMpegProc("framerate", m_dev);
+       return m_framerate;
+}
+
 DEFINE_REF(eDVBPCR);
 
 DEFINE_REF(eDVBPCR);
 
-eDVBPCR::eDVBPCR(eDVBDemux *demux): m_demux(demux)
+eDVBPCR::eDVBPCR(eDVBDemux *demux, int dev): m_demux(demux), m_dev(dev)
 {
        char filename[128];
 #if HAVE_DVB_API_VERSION < 3
 {
        char filename[128];
 #if HAVE_DVB_API_VERSION < 3
@@ -639,7 +749,7 @@ int eDVBPCR::startPid(int pid)
        pes.pid      = pid;
        pes.input    = DMX_IN_FRONTEND;
        pes.output   = DMX_OUT_DECODER;
        pes.pid      = pid;
        pes.input    = DMX_IN_FRONTEND;
        pes.output   = DMX_OUT_DECODER;
-       pes.pes_type = DMX_PES_PCR;
+       pes.pes_type = m_dev ? DMX_PES_PCR1 : DMX_PES_PCR0; /* FIXME */
        pes.flags    = 0;
        eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - pcr - ", pid);
        if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
        pes.flags    = 0;
        eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - pcr - ", pid);
        if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
@@ -676,7 +786,8 @@ eDVBPCR::~eDVBPCR()
 
 DEFINE_REF(eDVBTText);
 
 
 DEFINE_REF(eDVBTText);
 
-eDVBTText::eDVBTText(eDVBDemux *demux): m_demux(demux)
+eDVBTText::eDVBTText(eDVBDemux *demux, int dev)
+    :m_demux(demux), m_dev(dev)
 {
        char filename[128];
 #if HAVE_DVB_API_VERSION < 3
 {
        char filename[128];
 #if HAVE_DVB_API_VERSION < 3
@@ -698,7 +809,7 @@ int eDVBTText::startPid(int pid)
        pes.pid      = pid;
        pes.input    = DMX_IN_FRONTEND;
        pes.output   = DMX_OUT_DECODER;
        pes.pid      = pid;
        pes.input    = DMX_IN_FRONTEND;
        pes.output   = DMX_OUT_DECODER;
-       pes.pes_type = DMX_PES_TELETEXT;
+       pes.pes_type = m_dev ? DMX_PES_TELETEXT1 : DMX_PES_TELETEXT0; // FIXME
        pes.flags    = 0;
 
        eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - ttx - ", pid);
        pes.flags    = 0;
 
        eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - ttx - ", pid);
@@ -708,7 +819,7 @@ int eDVBTText::startPid(int pid)
                return -errno;
        }
        eDebug("ok");
                return -errno;
        }
        eDebug("ok");
-       eDebugNoNewLine("DEMUX_START - pcr - ");
+       eDebugNoNewLine("DEMUX_START - ttx - ");
        if (::ioctl(m_fd_demux, DMX_START) < 0)
        {
                eDebug("failed(%m)");
        if (::ioctl(m_fd_demux, DMX_START) < 0)
        {
                eDebug("failed(%m)");
@@ -739,16 +850,19 @@ int eTSMPEGDecoder::setState()
 {
        int res = 0;
 
 {
        int res = 0;
 
-       int noaudio = m_is_sm || m_is_ff || m_is_trickmode;
+       int noaudio = (m_state != statePlay) && (m_state != statePause);
        int nott = noaudio; /* actually same conditions */
 
        if ((noaudio && m_audio) || (!m_audio && !noaudio))
        int nott = noaudio; /* actually same conditions */
 
        if ((noaudio && m_audio) || (!m_audio && !noaudio))
-               m_changed |= changeAudio;
+               m_changed |= changeAudio | changeState;
 
        if ((nott && m_text) || (!m_text && !nott))
 
        if ((nott && m_text) || (!m_text && !nott))
-               m_changed |= changeText;
+               m_changed |= changeText | changeState;
+
+       const char *decoder_states[] = {"stop", "pause", "play", "decoderfastforward", "trickmode", "slowmotion"};
+       eDebug("decoder state: %s, vpid=%d, apid=%d", decoder_states[m_state], m_vpid, m_apid);
 
 
-       bool changed = !!m_changed;
+       int changed = m_changed;
 #if HAVE_DVB_API_VERSION < 3
        bool checkAVSync = m_changed & (changeAudio|changeVideo|changePCR);
        if (m_changed & changeAudio && m_audio)
 #if HAVE_DVB_API_VERSION < 3
        bool checkAVSync = m_changed & (changeAudio|changeVideo|changePCR);
        if (m_changed & changeAudio && m_audio)
@@ -786,7 +900,7 @@ int eTSMPEGDecoder::setState()
        }
        if (m_changed & changePCR)
        {
        }
        if (m_changed & changePCR)
        {
-               m_pcr = new eDVBPCR(m_demux);
+               m_pcr = new eDVBPCR(m_demux, m_decoder);
                if (m_pcr->setPid(m_pcrpid))
                        res = -1;
        }
                if (m_pcr->setPid(m_pcrpid))
                        res = -1;
        }
@@ -825,37 +939,40 @@ int eTSMPEGDecoder::setState()
                if (m_pcr)
                        m_pcr->stop();
                m_pcr = 0;
                if (m_pcr)
                        m_pcr->stop();
                m_pcr = 0;
-               if ((m_pcrpid >= 0) && (m_pcrpid < 0x1FFF))
-               {
-                       m_pcr = new eDVBPCR(m_demux);
-                       if (m_pcr->startPid(m_pcrpid))
-                               res = -1;
-               }
-               m_changed &= ~changePCR;
        }
        if (m_changed & changeVideo)
        {
        }
        if (m_changed & changeVideo)
        {
-               eDebug("VIDEO CHANGED (to %04x)", m_vpid);
                if (m_video)
                {
                        m_video->stop();
                        m_video = 0;
                        m_video_event_conn = 0;
                }
                if (m_video)
                {
                        m_video->stop();
                        m_video = 0;
                        m_video_event_conn = 0;
                }
-               if ((m_vpid >= 0) && (m_vpid < 0x1FFF))
-               {
-                       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))
-                               res = -1;
-               }
-               m_changed &= ~changeVideo;
        }
        if (m_changed & changeAudio)
        {
                if (m_audio)
                        m_audio->stop();
                m_audio = 0;
        }
        if (m_changed & changeAudio)
        {
                if (m_audio)
                        m_audio->stop();
                m_audio = 0;
+       }
+       if (m_changed & changeText)
+       {
+               if (m_text)
+                       m_text->stop();
+               m_text = 0;
+       }
+       if (m_changed & changePCR)
+       {
+               if ((m_pcrpid >= 0) && (m_pcrpid < 0x1FFF))
+               {
+                       m_pcr = new eDVBPCR(m_demux, m_decoder);
+                       if (m_pcr->startPid(m_pcrpid))
+                               res = -1;
+               }
+               m_changed &= ~changePCR;
+       }
+       if (m_changed & changeAudio)
+       {
                if ((m_apid >= 0) && (m_apid < 0x1FFF) && !noaudio)
                {
                        m_audio = new eDVBAudio(m_demux, m_decoder);
                if ((m_apid >= 0) && (m_apid < 0x1FFF) && !noaudio)
                {
                        m_audio = new eDVBAudio(m_demux, m_decoder);
@@ -864,20 +981,61 @@ int eTSMPEGDecoder::setState()
                }
                m_changed &= ~changeAudio;
        }
                }
                m_changed &= ~changeAudio;
        }
+       if (m_changed & changeVideo)
+       {
+               if ((m_vpid >= 0) && (m_vpid < 0x1FFF))
+               {
+                       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))
+                               res = -1;
+               }
+               m_changed &= ~changeVideo;
+       }
        if (m_changed & changeText)
        {
        if (m_changed & changeText)
        {
-               if (m_text)
-                       m_text->stop();
-               m_text = 0;
                if ((m_textpid >= 0) && (m_textpid < 0x1FFF) && !nott)
                {
                if ((m_textpid >= 0) && (m_textpid < 0x1FFF) && !nott)
                {
-                       m_text = new eDVBTText(m_demux);
+                       m_text = new eDVBTText(m_demux, m_decoder);
                        if (m_text->startPid(m_textpid))
                                res = -1;
                }
                m_changed &= ~changeText;
        }
 #endif
                        if (m_text->startPid(m_textpid))
                                res = -1;
                }
                m_changed &= ~changeText;
        }
 #endif
+
+       if (changed & (changeState|changeVideo|changeAudio))
+       {
+                                       /* play, slowmotion, fast-forward */
+               int state_table[6][4] = 
+                       {
+                               /* [stateStop] =                 */ {0, 0, 0},
+                               /* [statePause] =                */ {0, 0, 0},
+                               /* [statePlay] =                 */ {1, 0, 0},
+                               /* [stateDecoderFastForward] =   */ {1, 0, m_ff_sm_ratio},
+                               /* [stateHighspeedFastForward] = */ {1, 0, 1},
+                               /* [stateSlowMotion] =           */ {1, m_ff_sm_ratio, 0}
+                       };
+               int *s = state_table[m_state];
+               if (changed & (changeState|changeVideo) && m_video)
+               {
+                       m_video->setSlowMotion(s[1]);
+                       m_video->setFastForward(s[2]);
+                       if (s[0])
+                               m_video->unfreeze();
+                       else
+                               m_video->freeze();
+               }
+               if (changed & (changeState|changeAudio) && m_audio)
+               {
+                       if (s[0])
+                               m_audio->unfreeze();
+                       else
+                               m_audio->freeze();
+               }
+               m_changed &= ~changeState;
+       }
+
        if (changed && !m_video && m_audio && m_radio_pic.length())
                showSinglePic(m_radio_pic.c_str());
 
        if (changed && !m_video && m_audio && m_radio_pic.length())
                showSinglePic(m_radio_pic.c_str());
 
@@ -887,9 +1045,9 @@ int eTSMPEGDecoder::setState()
 int eTSMPEGDecoder::m_pcm_delay=-1,
        eTSMPEGDecoder::m_ac3_delay=-1;
 
 int eTSMPEGDecoder::m_pcm_delay=-1,
        eTSMPEGDecoder::m_ac3_delay=-1;
 
-RESULT eTSMPEGDecoder::setPCMDelay(int delay)
+RESULT eTSMPEGDecoder::setHwPCMDelay(int delay)
 {
 {
-       if (m_decoder == 0 && delay != m_pcm_delay )
+       if (delay != m_pcm_delay )
        {
                FILE *fp = fopen("/proc/stb/audio/audio_delay_pcm", "w");
                if (fp)
        {
                FILE *fp = fopen("/proc/stb/audio/audio_delay_pcm", "w");
                if (fp)
@@ -903,9 +1061,9 @@ RESULT eTSMPEGDecoder::setPCMDelay(int delay)
        return -1;
 }
 
        return -1;
 }
 
-RESULT eTSMPEGDecoder::setAC3Delay(int delay)
+RESULT eTSMPEGDecoder::setHwAC3Delay(int delay)
 {
 {
-       if ( m_decoder == 0 && delay != m_ac3_delay )
+       if ( delay != m_ac3_delay )
        {
                FILE *fp = fopen("/proc/stb/audio/audio_delay_bitstream", "w");
                if (fp)
        {
                FILE *fp = fopen("/proc/stb/audio/audio_delay_bitstream", "w");
                if (fp)
@@ -919,12 +1077,25 @@ RESULT eTSMPEGDecoder::setAC3Delay(int delay)
        return -1;
 }
 
        return -1;
 }
 
+
+RESULT eTSMPEGDecoder::setPCMDelay(int delay)
+{
+       return m_decoder == 0 ? setHwPCMDelay(delay) : -1;
+}
+
+RESULT eTSMPEGDecoder::setAC3Delay(int delay)
+{
+       return m_decoder == 0 ? setHwAC3Delay(delay) : -1;
+}
+
 eTSMPEGDecoder::eTSMPEGDecoder(eDVBDemux *demux, int decoder)
 eTSMPEGDecoder::eTSMPEGDecoder(eDVBDemux *demux, int decoder)
-       :m_demux(demux), m_changed(0), m_decoder(decoder), m_video_clip_fd(-1), m_showSinglePicTimer(eApp)
+       : m_demux(demux), 
+               m_vpid(-1), m_vtype(-1), m_apid(-1), m_atype(-1), m_pcrpid(-1), m_textpid(-1),
+               m_changed(0), m_decoder(decoder), m_video_clip_fd(-1), m_showSinglePicTimer(eTimer::create(eApp))
 {
        demux->connectEvent(slot(*this, &eTSMPEGDecoder::demux_event), m_demux_event_conn);
 {
        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;
+       CONNECT(m_showSinglePicTimer->timeout, eTSMPEGDecoder::finishShowSinglePic);
+       m_state = stateStop;
 }
 
 eTSMPEGDecoder::~eTSMPEGDecoder()
 }
 
 eTSMPEGDecoder::~eTSMPEGDecoder()
@@ -937,7 +1108,7 @@ eTSMPEGDecoder::~eTSMPEGDecoder()
 
 RESULT eTSMPEGDecoder::setVideoPID(int vpid, int type)
 {
 
 RESULT eTSMPEGDecoder::setVideoPID(int vpid, int type)
 {
-       if (m_vpid != vpid)
+       if ((m_vpid != vpid) || (m_vtype != type))
        {
                m_changed |= changeVideo;
                m_vpid = vpid;
        {
                m_changed |= changeVideo;
                m_vpid = vpid;
@@ -1006,80 +1177,66 @@ RESULT eTSMPEGDecoder::setSyncMaster(int who)
        return -1;
 }
 
        return -1;
 }
 
-RESULT eTSMPEGDecoder::start()
-{
-       RESULT r;
-       r = setState();
-       if (r)
-               return r;
-       return unfreeze();
-}
-
-       /* preroll is start in freezed mode. */
-RESULT eTSMPEGDecoder::preroll()
+RESULT eTSMPEGDecoder::set()
 {
        return setState();
 }
 
 {
        return setState();
 }
 
-RESULT eTSMPEGDecoder::freeze(int cont)
-{
-       if (m_video)
-               m_video->freeze();
-
-       if (m_audio)
-               m_audio->freeze();
-
-       return 0;
-}
-
-RESULT eTSMPEGDecoder::unfreeze()
-{
-       if (m_video)
-               m_video->unfreeze();
-
-       if (m_audio)
-               m_audio->unfreeze();
-
-       return 0;
-}
-
-RESULT eTSMPEGDecoder::setSinglePictureMode(int when)
+RESULT eTSMPEGDecoder::play()
 {
 {
-       return -1;
+       if (m_state == statePlay)
+       {
+               if (!m_changed)
+                       return 0;
+       } else  
+       {
+               m_state = statePlay;
+               m_changed |= changeState;
+       }
+       return setState();
 }
 
 }
 
-RESULT eTSMPEGDecoder::setPictureSkipMode(int what)
+RESULT eTSMPEGDecoder::pause()
 {
 {
-       return -1;
+       if (m_state == statePause)
+               return 0;
+       m_state = statePause;
+       m_changed |= changeState;
+       return setState();
 }
 
 RESULT eTSMPEGDecoder::setFastForward(int frames_to_skip)
 {
 }
 
 RESULT eTSMPEGDecoder::setFastForward(int frames_to_skip)
 {
-       m_is_ff = frames_to_skip != 0;
+       if ((m_state == stateDecoderFastForward) && (m_ff_sm_ratio == frames_to_skip))
+               return 0;
 
 
-       setState();
+       m_state = stateDecoderFastForward;
+       m_ff_sm_ratio = frames_to_skip;
+       m_changed |= changeState;
+       return setState();
 
 
-       if (m_video)
-               return m_video->setFastForward(frames_to_skip);
-       else
-               return -1;
+//             return m_video->setFastForward(frames_to_skip);
 }
 
 RESULT eTSMPEGDecoder::setSlowMotion(int repeat)
 {
 }
 
 RESULT eTSMPEGDecoder::setSlowMotion(int repeat)
 {
-       m_is_sm = repeat != 0;
+       if ((m_state == stateSlowMotion) && (m_ff_sm_ratio == repeat))
+               return 0;
 
 
-       setState();
-
-       if (m_video)
-               return m_video->setSlowMotion(repeat);
-       else
-               return -1;
+       m_state = stateSlowMotion;
+       m_ff_sm_ratio = repeat;
+       m_changed |= changeState;
+       return setState();
 }
 
 }
 
-RESULT eTSMPEGDecoder::setZoom(int what)
+RESULT eTSMPEGDecoder::setTrickmode()
 {
 {
-       return -1;
+       if (m_state == stateTrickmode)
+               return 0;
+
+       m_state = stateTrickmode;
+       m_changed |= changeState;
+       return setState();
 }
 
 RESULT eTSMPEGDecoder::flush()
 }
 
 RESULT eTSMPEGDecoder::flush()
@@ -1103,13 +1260,6 @@ void eTSMPEGDecoder::demux_event(int event)
        }
 }
 
        }
 }
 
-RESULT eTSMPEGDecoder::setTrickmode(int what)
-{
-       m_is_trickmode = what;
-       setState();
-       return 0;
-}
-
 RESULT eTSMPEGDecoder::getPTS(int what, pts_t &pts)
 {
        if (what == 0) /* auto */
 RESULT eTSMPEGDecoder::getPTS(int what, pts_t &pts)
 {
        if (what == 0) /* auto */
@@ -1146,7 +1296,7 @@ RESULT eTSMPEGDecoder::showSinglePic(const char *filename)
        {
                eDebug("showSinglePic %s", filename);
                int f = open(filename, O_RDONLY);
        {
                eDebug("showSinglePic %s", filename);
                int f = open(filename, O_RDONLY);
-               if (f)
+               if (f >= 0)
                {
                        struct stat s;
                        fstat(f, &s);
                {
                        struct stat s;
                        fstat(f, &s);
@@ -1160,25 +1310,30 @@ RESULT eTSMPEGDecoder::showSinglePic(const char *filename)
                                unsigned char seq_end[] = { 0x00, 0x00, 0x01, 0xB7 };
                                unsigned char iframe[s.st_size];
                                unsigned char stuffing[8192];
                                unsigned char seq_end[] = { 0x00, 0x00, 0x01, 0xB7 };
                                unsigned char iframe[s.st_size];
                                unsigned char stuffing[8192];
+                               int streamtype = VIDEO_STREAMTYPE_MPEG2;
                                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)");
                                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(m_video_clip_fd, VIDEO_SET_STREAMTYPE, streamtype) < 0)
+                                       eDebug("VIDEO_SET_STREAMTYPE failed(%m)");
                                if (ioctl(m_video_clip_fd, VIDEO_PLAY) < 0)
                                        eDebug("VIDEO_PLAY failed (%m)");
                                if (ioctl(m_video_clip_fd, VIDEO_PLAY) < 0)
                                        eDebug("VIDEO_PLAY failed (%m)");
-                               if (::ioctl(m_video_clip_fd, VIDEO_CONTINUE) < 0)
+                               if (ioctl(m_video_clip_fd, VIDEO_CONTINUE) < 0)
                                        eDebug("video: VIDEO_CONTINUE: %m");
                                        eDebug("video: VIDEO_CONTINUE: %m");
-                               if (::ioctl(m_video_clip_fd, VIDEO_CLEAR_BUFFER) < 0)
+                               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));
                                        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));
+                               else
+                                       iframe[4] = iframe[5] = 0x00;
                                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);
                                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);
+                               m_showSinglePicTimer->start(150, true);
                        }
                        close(f);
                }
                        }
                        close(f);
                }
@@ -1219,3 +1374,38 @@ void eTSMPEGDecoder::video_event(struct videoEvent event)
 {
        /* emit */ m_video_event(event);
 }
 {
        /* emit */ m_video_event(event);
 }
+
+int eTSMPEGDecoder::getVideoWidth()
+{
+       if (m_video)
+               return m_video->getWidth();
+       return -1;
+}
+
+int eTSMPEGDecoder::getVideoHeight()
+{
+       if (m_video)
+               return m_video->getHeight();
+       return -1;
+}
+
+int eTSMPEGDecoder::getVideoProgressive()
+{
+       if (m_video)
+               return m_video->getProgressive();
+       return -1;
+}
+
+int eTSMPEGDecoder::getVideoFrameRate()
+{
+       if (m_video)
+               return m_video->getFrameRate();
+       return -1;
+}
+
+int eTSMPEGDecoder::getVideoAspect()
+{
+       if (m_video)
+               return m_video->getAspect();
+       return -1;
+}