better readable code
[enigma2.git] / lib / dvb / decoder.cpp
index 0e6f86d31bd982017e8c85a1f35c887637a86b7a..6b4a7f25ba2021f34d03e6d20c44c7d373cab223 100644 (file)
@@ -33,7 +33,7 @@
 
 DEFINE_REF(eDVBAudio);
 
-eDVBAudio::eDVBAudio(eDVBDemux *demux, int dev): m_demux(demux)
+eDVBAudio::eDVBAudio(eDVBDemux *demux, int dev): m_demux(demux), m_dev(dev)
 {
        char filename[128];
 #if HAVE_DVB_API_VERSION < 3
@@ -63,7 +63,7 @@ int eDVBAudio::startPid(int pid, int type)
        pes.pid      = pid;
        pes.input    = DMX_IN_FRONTEND;
        pes.output   = DMX_OUT_DECODER;
-       pes.pes_type = DMX_PES_AUDIO;  // DMX_PES_AUDIO0
+       pes.pes_type = m_dev ? DMX_PES_AUDIO1 : DMX_PES_AUDIO0; /* FIXME */
        pes.flags    = 0;
        if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
        {
@@ -137,6 +137,19 @@ void eDVBAudio::unfreeze()
                eDebug("video: AUDIO_CONTINUE: %m");
 }
 
+void eDVBAudio::setChannel(int channel)
+{
+       int val = AUDIO_STEREO;
+       switch (channel)
+       {
+       case aMonoLeft: val = AUDIO_MONO_LEFT; break;
+       case aMonoRight: val = AUDIO_MONO_RIGHT; break;
+       default: break;
+       }
+       if (::ioctl(m_fd, AUDIO_CHANNEL_SELECT, val) < 0)
+               eDebug("video: AUDIO_CHANNEL_SELECT: %m");
+}
+
 int eDVBAudio::getPTS(pts_t &now)
 {
        return ::ioctl(m_fd, AUDIO_GET_PTS, &now);
@@ -152,7 +165,7 @@ eDVBAudio::~eDVBAudio()
 
 DEFINE_REF(eDVBVideo);
 
-eDVBVideo::eDVBVideo(eDVBDemux *demux, int dev): m_demux(demux)
+eDVBVideo::eDVBVideo(eDVBDemux *demux, int dev): m_demux(demux), m_dev(dev)
 {
        char filename[128];
 #if HAVE_DVB_API_VERSION < 3
@@ -163,6 +176,8 @@ eDVBVideo::eDVBVideo(eDVBDemux *demux, int dev): m_demux(demux)
        m_fd = ::open(filename, O_RDWR);
        if (m_fd < 0)
                eWarning("%s: %m", filename);
+       
+       eDebug("Video Device: %s", filename);
 #if HAVE_DVB_API_VERSION < 3
        sprintf(filename, "/dev/dvb/card%d/demux%d", demux->adapter, demux->demux);
 #else
@@ -171,18 +186,27 @@ eDVBVideo::eDVBVideo(eDVBDemux *demux, int dev): m_demux(demux)
        m_fd_demux = ::open(filename, O_RDWR);
        if (m_fd_demux < 0)
                eWarning("%s: %m", filename);
+       eDebug("demux device: %s", filename);
 }
-       
-int eDVBVideo::startPid(int pid)
+
+// not finally values i think.. !!
+#define VIDEO_STREAMTYPE_MPEG2 0
+#define VIDEO_STREAMTYPE_MPEG4_H264 1
+
+int eDVBVideo::startPid(int pid, int type)
 {      
        if ((m_fd < 0) || (m_fd_demux < 0))
                return -1;
        dmx_pes_filter_params pes;
 
+       if (::ioctl(m_fd, VIDEO_SET_STREAMTYPE,
+               type == MPEG4_H264 ? VIDEO_STREAMTYPE_MPEG4_H264 : VIDEO_STREAMTYPE_MPEG2) < 0)
+               eWarning("video: VIDEO_SET_STREAMTYPE: %m");
+
        pes.pid      = pid;
        pes.input    = DMX_IN_FRONTEND;
        pes.output   = DMX_OUT_DECODER;
-       pes.pes_type = DMX_PES_VIDEO;
+       pes.pes_type = m_dev ? DMX_PES_VIDEO1 : DMX_PES_VIDEO0; /* FIXME */
        pes.flags    = 0;
        if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
        {
@@ -375,13 +399,14 @@ int eTSMPEGDecoder::setState()
        
        int noaudio = m_is_sm || m_is_ff || m_is_trickmode;
        int nott = noaudio; /* actually same conditions */
-
+       
        if ((noaudio && m_audio) || (!m_audio && !noaudio))
                m_changed |= changeAudio;
        
        if ((nott && m_text) || (!m_text && !nott))
                m_changed |= changeText;
-       
+
+       bool changed = !!m_changed;
 #if HAVE_DVB_API_VERSION < 3
        if (m_changed & changeAudio && m_audio)
                m_audio->stopPid();
@@ -414,7 +439,7 @@ int eTSMPEGDecoder::setState()
        }
        if (m_changed & changeVideo)
        {
-               m_video = new eDVBVideo(m_demux, 0);
+               m_video = new eDVBVideo(m_demux, m_decoder);
                if (m_video->startPid(m_vpid))
                {
                        eWarning("video: startpid failed!");
@@ -424,7 +449,7 @@ int eTSMPEGDecoder::setState()
        }
        if (m_changed & changeAudio)
        {
-               m_audio = new eDVBAudio(m_demux, 0);
+               m_audio = new eDVBAudio(m_demux, m_decoder);
                if (m_audio->startPid(m_apid, m_atype))
                {
                        eWarning("audio: startpid failed!");
@@ -453,7 +478,7 @@ int eTSMPEGDecoder::setState()
        {
                eDebug("VIDEO CHANGED (to %04x)", m_vpid);
                if (m_video)
-               {       
+               {
                        eDebug("STOP");
                        m_video->stop();
                }
@@ -461,8 +486,8 @@ int eTSMPEGDecoder::setState()
                if ((m_vpid >= 0) && (m_vpid < 0x1FFF))
                {
                        eDebug("new video");
-                       m_video = new eDVBVideo(m_demux, 0);
-                       if (m_video->startPid(m_vpid))
+                       m_video = new eDVBVideo(m_demux, m_decoder);
+                       if (m_video->startPid(m_vpid, m_vtype))
                        {
                                eWarning("video: startpid failed!");
                                res = -1;
@@ -477,7 +502,7 @@ int eTSMPEGDecoder::setState()
                m_audio = 0;
                if ((m_apid >= 0) && (m_apid < 0x1FFF) && !noaudio)
                {
-                       m_audio = new eDVBAudio(m_demux, 0);
+                       m_audio = new eDVBAudio(m_demux, m_decoder);
                        if (m_audio->startPid(m_apid, m_atype))
                        {
                                eWarning("audio: startpid failed!");
@@ -503,10 +528,44 @@ int eTSMPEGDecoder::setState()
                m_changed &= ~changeText;
        }
 #endif
+       if (changed && !m_video && m_audio && m_radio_pic.length())
+               showSinglePic(m_radio_pic.c_str());
+
        return res;
 }
 
-eTSMPEGDecoder::eTSMPEGDecoder(eDVBDemux *demux, int decoder): m_demux(demux), m_changed(0)
+int eTSMPEGDecoder::m_pcm_delay=-1,
+       eTSMPEGDecoder::m_ac3_delay=-1;
+
+RESULT eTSMPEGDecoder::setPCMDelay(int delay)
+{
+       if (m_decoder == 0 && delay != m_pcm_delay )
+       {
+               FILE *fp = fopen("/proc/stb/audio/audio_delay_pcm", "w");
+               if (fp)
+               {
+                       fprintf(fp, "%x", delay*90);
+                       fclose(fp);
+                       m_pcm_delay = delay;
+               }
+       }
+}
+
+RESULT eTSMPEGDecoder::setAC3Delay(int delay)
+{
+       if ( m_decoder == 0 && delay != m_ac3_delay )
+       {
+               FILE *fp = fopen("/proc/stb/audio/audio_delay_bitstream", "w");
+               if (fp)
+               {
+                       fprintf(fp, "%x", delay*90);
+                       fclose(fp);
+                       m_ac3_delay = delay;
+               }
+       }
+}
+
+eTSMPEGDecoder::eTSMPEGDecoder(eDVBDemux *demux, int decoder): m_demux(demux), m_changed(0), m_decoder(decoder)
 {
        demux->connectEvent(slot(*this, &eTSMPEGDecoder::demux_event), m_demux_event);
        m_is_ff = m_is_sm = m_is_trickmode = 0;
@@ -519,12 +578,13 @@ eTSMPEGDecoder::~eTSMPEGDecoder()
        setState();
 }
 
-RESULT eTSMPEGDecoder::setVideoPID(int vpid)
+RESULT eTSMPEGDecoder::setVideoPID(int vpid, int type)
 {
        if (m_vpid != vpid)
        {
                m_changed |= changeVideo;
                m_vpid = vpid;
+               m_vtype = type;
        }
        return 0;
 }
@@ -540,6 +600,30 @@ RESULT eTSMPEGDecoder::setAudioPID(int apid, int type)
        return 0;
 }
 
+int eTSMPEGDecoder::m_audio_channel = -1;
+
+RESULT eTSMPEGDecoder::setAudioChannel(int channel)
+{
+       if (channel == -1)
+               channel = ac_stereo;
+       if (m_decoder == 0 && m_audio_channel != channel)
+       {
+               if (m_audio)
+               {
+                       m_audio->setChannel(channel);
+                       m_audio_channel=channel;
+               }
+               else
+                       eDebug("eTSMPEGDecoder::setAudioChannel but no audio decoder exist");
+       }
+       return 0;
+}
+
+int eTSMPEGDecoder::getAudioChannel()
+{
+       return m_audio_channel == -1 ? ac_stereo : m_audio_channel;
+}
+
 RESULT eTSMPEGDecoder::setSyncPCR(int pcrpid)
 {
        if (m_pcrpid != pcrpid)
@@ -682,3 +766,80 @@ RESULT eTSMPEGDecoder::getPTS(int what, pts_t &pts)
 
        return -1;
 }
+
+RESULT eTSMPEGDecoder::setRadioPic(const std::string &filename)
+{
+       m_radio_pic = filename;
+}
+
+RESULT eTSMPEGDecoder::showSinglePic(const char *filename)
+{
+       if (m_decoder == 0)
+       {
+               FILE *f = fopen(filename, "r");
+               if (f)
+               {
+                       int vfd = open("/dev/dvb/adapter0/video0", O_RDWR);
+                       if (vfd > 0)
+                       {
+                               fseek(f, 0, SEEK_END);
+                               int length = ftell(f);
+                               unsigned char *buffer = new unsigned char[length*3+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)
+                                       eDebug("VIDEO_SELECT_SOURCE MEMORY failed (%m)");
+                               if (ioctl(vfd, VIDEO_PLAY) < 0)
+                                       eDebug("VIDEO_PLAY failed (%m)");
+                               int cnt=0;
+                               int pos=0;
+                               while(cnt<3)
+                               {
+                                       int rd;
+                                       fseek(f, 0, SEEK_SET);
+                                       while(1)
+                                       {
+                                               if (!cnt)
+                                               {
+                                                       buffer[pos++]=0;
+                                                       buffer[pos++]=0;
+                                                       buffer[pos++]=1;
+                                                       buffer[pos++]=0xE0;
+                                                       buffer[pos++]=(length*3)>>8;
+                                                       buffer[pos++]=(length*3)&0xFF;
+                                                       buffer[pos++]=0x80;
+                                                       buffer[pos++]=0;
+                                                       buffer[pos++]=0;
+                                               }
+                                               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;
+                       }
+                       fclose(f);
+               }
+               else
+               {
+                       eDebug("couldnt open %s", filename);
+                       return -1;
+               }
+       }
+       else
+       {
+               eDebug("only show single pics on first decoder");
+               return -1;
+       }
+       return 0;
+}