fix debug output
[enigma2.git] / lib / dvb / decoder.cpp
1 #include <lib/base/ebase.h>
2 #include <lib/base/eerror.h>
3 #include <lib/dvb/decoder.h>
4 #if HAVE_DVB_API_VERSION < 3 
5 #define audioStatus audio_status
6 #define videoStatus video_status
7 #define pesType pes_type
8 #define playState play_state
9 #define audioStreamSource_t audio_stream_source_t
10 #define videoStreamSource_t video_stream_source_t
11 #define streamSource stream_source
12 #define dmxPesFilterParams dmx_pes_filter_params
13 #define DMX_PES_VIDEO0 DMX_PES_VIDEO
14 #define DMX_PES_AUDIO0 DMX_PES_AUDIO
15 #define DMX_PES_VIDEO1 DMX_PES_VIDEO
16 #define DMX_PES_AUDIO1 DMX_PES_AUDIO
17 #include <ost/dmx.h>
18 #include <ost/video.h>
19 #include <ost/audio.h>
20 #else
21 #include <linux/dvb/audio.h>
22 #include <linux/dvb/video.h>
23 #include <linux/dvb/dmx.h>
24 #endif
25
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sys/ioctl.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <errno.h>
32
33         /* these are quite new... */
34 #ifndef AUDIO_GET_PTS
35 #define AUDIO_GET_PTS              _IOR('o', 19, __u64)
36 #define VIDEO_GET_PTS              _IOR('o', 57, __u64)
37 #endif
38
39 DEFINE_REF(eDVBAudio);
40
41 eDVBAudio::eDVBAudio(eDVBDemux *demux, int dev)
42         :m_demux(demux), m_dev(dev)
43 {
44         char filename[128];
45 #if HAVE_DVB_API_VERSION < 3
46         sprintf(filename, "/dev/dvb/card%d/audio%d", demux->adapter, dev);
47 #else
48         sprintf(filename, "/dev/dvb/adapter%d/audio%d", demux->adapter, dev);
49 #endif
50         m_fd = ::open(filename, O_RDWR);
51         if (m_fd < 0)
52                 eWarning("%s: %m", filename);
53 #if HAVE_DVB_API_VERSION < 3
54         sprintf(filename, "/dev/dvb/card%d/demux%d", demux->adapter, demux->demux);
55 #else
56         sprintf(filename, "/dev/dvb/adapter%d/demux%d", demux->adapter, demux->demux);
57 #endif
58         m_fd_demux = ::open(filename, O_RDWR);
59         if (m_fd_demux < 0)
60                 eWarning("%s: %m", filename);
61 }
62
63 #if HAVE_DVB_API_VERSION < 3
64 int eDVBAudio::setPid(int pid, int type)
65 {
66         if ((m_fd < 0) || (m_fd_demux < 0))
67                 return -1;
68
69         int bypass = 0;
70
71         switch (type)
72         {
73         case aMPEG:
74                 bypass = 1;
75                 break;
76         case aAC3:
77                 bypass = 0;
78                 break;
79                 /*
80         case aDTS:
81                 bypass = 2;
82                 break;
83                 */
84         }
85
86         if (::ioctl(m_fd, AUDIO_SET_BYPASS_MODE, bypass) < 0)
87                 eDebug("failed (%m)");
88
89         dmx_pes_filter_params pes;
90
91         pes.pid      = pid;
92         pes.input    = DMX_IN_FRONTEND;
93         pes.output   = DMX_OUT_DECODER;
94         pes.pes_type = m_dev ? DMX_PES_AUDIO1 : DMX_PES_AUDIO0; /* FIXME */
95         pes.flags    = 0;
96         eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - audio - ", pid);
97         if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
98         {
99                 eDebug("failed (%m)");
100                 return -errno;
101         }
102         eDebug("ok");
103
104         return 0;
105 }
106
107 int eDVBAudio::startPid()
108 {
109         eDebugNoNewLine("DEMUX_START - audio - ");
110         if (::ioctl(m_fd_demux, DMX_START) < 0)
111         {
112                 eDebug("failed (%m)");
113                 return -errno;
114         }
115         eDebug("ok");
116         return 0;
117 }
118
119 int eDVBAudio::start()
120 {
121         eDebugNoNewLine("AUDIO_PLAY - ");
122         if (::ioctl(m_fd, AUDIO_PLAY) < 0)
123         {
124                 eDebug("failed (%m)");
125                 return -errno;
126         }
127         eDebug("ok");
128         return 0;
129 }
130
131 int eDVBAudio::stopPid()
132 {
133         eDebugNoNewLine("DEMUX_STOP - audio - ");
134         if (::ioctl(m_fd_demux, DMX_STOP) < 0)
135         {
136                 eDebug("failed (%m)");
137                 return -errno;
138         }
139         eDebug("ok");
140         return 0;
141 }
142
143 int eDVBAudio::setAVSync(int val)
144 {
145         eDebugNoNewLine("AUDIO_SET_AV_SYNC - ");
146         if (::ioctl(m_fd, AUDIO_SET_AV_SYNC, val) < 0)
147         {
148                 eDebug("failed (%m)");
149                 return -errno;
150         }
151         eDebug("ok");
152         return 0;
153 }
154 #else
155 int eDVBAudio::startPid(int pid, int type)
156 {
157         if ((m_fd < 0) || (m_fd_demux < 0))
158                 return -1;
159         dmx_pes_filter_params pes;
160
161         pes.pid      = pid;
162         pes.input    = DMX_IN_FRONTEND;
163         pes.output   = DMX_OUT_DECODER;
164         pes.pes_type = m_dev ? DMX_PES_AUDIO1 : DMX_PES_AUDIO0; /* FIXME */
165         pes.flags    = 0;
166         eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - audio - ", pid);
167         if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
168         {
169                 eDebug("failed (%m)");
170                 return -errno;
171         }
172         eDebug("ok");
173         eDebugNoNewLine("DEMUX_START - audio - ");
174         if (::ioctl(m_fd_demux, DMX_START) < 0)
175         {
176                 eDebug("failed (%m)");
177                 return -errno;
178         }
179         eDebug("ok");
180         int bypass = 0;
181
182         switch (type)
183         {
184         case aMPEG:
185                 bypass = 1;
186                 break;
187         case aAC3:
188                 bypass = 0;
189                 break;
190         case aDTS:
191                 bypass = 2;
192                 break;
193         case aAAC:
194                 bypass = 8;
195                 break;
196         case aAACHE:
197                 bypass = 9;
198                 break;
199         }
200
201         eDebugNoNewLine("AUDIO_SET_BYPASS(%d) - ", bypass);
202         if (::ioctl(m_fd, AUDIO_SET_BYPASS_MODE, bypass) < 0)
203                 eDebug("failed (%m)");
204         else
205                 eDebug("ok");
206         freeze();  // why freeze here?!? this is a problem when only a pid change is requested... because of the unfreeze logic in Decoder::setState
207         eDebugNoNewLine("AUDIO_PLAY - ");
208         if (::ioctl(m_fd, AUDIO_PLAY) < 0)
209                 eDebug("failed (%m)");
210         else
211                 eDebug("ok");
212         return 0;
213 }
214 #endif
215
216 void eDVBAudio::stop()
217 {
218 #if HAVE_DVB_API_VERSION > 2
219         flush();
220 #endif
221         eDebugNoNewLine("AUDIO_STOP - ");
222         if (::ioctl(m_fd, AUDIO_STOP) < 0)
223                 eDebug("failed (%m)");
224         else
225                 eDebug("ok");
226 #if HAVE_DVB_API_VERSION > 2
227         eDebugNoNewLine("DEMUX_STOP - audio - ");
228         if (::ioctl(m_fd_demux, DMX_STOP) < 0)
229                 eDebug("failed (%m)");
230         else
231                 eDebug("ok");
232 #endif
233 }
234
235 void eDVBAudio::flush()
236 {
237         eDebugNoNewLine("AUDIO_CLEAR_BUFFER - ");
238         if (::ioctl(m_fd, AUDIO_CLEAR_BUFFER) < 0)
239                 eDebug("failed (%m)");
240         else
241                 eDebug("ok");
242 }
243
244 void eDVBAudio::freeze()
245 {
246         eDebugNoNewLine("AUDIO_PAUSE - ");
247         if (::ioctl(m_fd, AUDIO_PAUSE) < 0)
248                 eDebug("failed (%m)");
249         else
250                 eDebug("ok");
251 }
252
253 void eDVBAudio::unfreeze()
254 {
255         eDebugNoNewLine("AUDIO_CONTINUE - ");
256         if (::ioctl(m_fd, AUDIO_CONTINUE) < 0)
257                 eDebug("failed (%m)");
258         else
259                 eDebug("ok");
260 }
261
262 void eDVBAudio::setChannel(int channel)
263 {
264         int val = AUDIO_STEREO;
265         switch (channel)
266         {
267         case aMonoLeft: val = AUDIO_MONO_LEFT; break;
268         case aMonoRight: val = AUDIO_MONO_RIGHT; break;
269         default: break;
270         }
271         eDebugNoNewLine("AUDIO_CHANNEL_SELECT(%d) - ", val);
272         if (::ioctl(m_fd, AUDIO_CHANNEL_SELECT, val) < 0)
273                 eDebug("failed (%m)");
274         else
275                 eDebug("ok");
276 }
277
278 int eDVBAudio::getPTS(pts_t &now)
279 {
280         if (::ioctl(m_fd, AUDIO_GET_PTS, &now) < 0)
281                 eDebug("AUDIO_GET_PTS failed (%m)");
282         return 0;
283 }
284
285 eDVBAudio::~eDVBAudio()
286 {
287         unfreeze();  // why unfreeze here... but not unfreeze video in ~eDVBVideo ?!?
288         if (m_fd >= 0)
289                 ::close(m_fd);
290         if (m_fd_demux >= 0)
291                 ::close(m_fd_demux);
292 }
293
294 DEFINE_REF(eDVBVideo);
295
296 eDVBVideo::eDVBVideo(eDVBDemux *demux, int dev)
297         : m_demux(demux), m_dev(dev),
298         m_width(-1), m_height(-1), m_framerate(-1), m_aspect(-1), m_progressive(-1)
299 {
300         char filename[128];
301 #if HAVE_DVB_API_VERSION < 3
302         sprintf(filename, "/dev/dvb/card%d/video%d", demux->adapter, dev);
303         m_fd_video = ::open("/dev/video", O_RDWR);
304         if (m_fd_video < 0)
305                 eWarning("/dev/video: %m");
306 #else
307         sprintf(filename, "/dev/dvb/adapter%d/video%d", demux->adapter, dev);
308 #endif
309         m_fd = ::open(filename, O_RDWR);
310         if (m_fd < 0)
311                 eWarning("%s: %m", filename);
312         else
313         {
314                 m_sn = eSocketNotifier::create(eApp, m_fd, eSocketNotifier::Priority);
315                 CONNECT(m_sn->activated, eDVBVideo::video_event);
316         }
317         eDebug("Video Device: %s", filename);
318 #if HAVE_DVB_API_VERSION < 3
319         sprintf(filename, "/dev/dvb/card%d/demux%d", demux->adapter, demux->demux);
320 #else
321         sprintf(filename, "/dev/dvb/adapter%d/demux%d", demux->adapter, demux->demux);
322 #endif
323         m_fd_demux = ::open(filename, O_RDWR);
324         if (m_fd_demux < 0)
325                 eWarning("%s: %m", filename);
326         eDebug("demux device: %s", filename);
327 }
328
329 // not finally values i think.. !!
330 #define VIDEO_STREAMTYPE_MPEG2 0
331 #define VIDEO_STREAMTYPE_MPEG4_H264 1
332 #define VIDEO_STREAMTYPE_VC1 3
333 #define VIDEO_STREAMTYPE_MPEG4_Part2 4
334 #define VIDEO_STREAMTYPE_VC1_SM 5
335 #define VIDEO_STREAMTYPE_MPEG1 6
336
337 #if HAVE_DVB_API_VERSION < 3
338 int eDVBVideo::setPid(int pid)
339 {
340         if ((m_fd < 0) || (m_fd_demux < 0))
341                 return -1;
342         dmx_pes_filter_params pes;
343
344         pes.pid      = pid;
345         pes.input    = DMX_IN_FRONTEND;
346         pes.output   = DMX_OUT_DECODER;
347         pes.pes_type = m_dev ? DMX_PES_VIDEO1 : DMX_PES_VIDEO0; /* FIXME */
348         pes.flags    = 0;
349         eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - video - ", pid);
350         if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
351         {
352                 eDebug("failed (%m)");
353                 return -errno;
354         }
355         eDebug("ok");
356         return 0;
357 }
358
359 int eDVBVideo::startPid()
360 {
361         eDebugNoNewLine("DEMUX_START - video - ");
362         if (::ioctl(m_fd_demux, DMX_START) < 0)
363         {
364                 eDebug("failed (%m)");
365                 return -errno;
366         }
367         eDebug("ok");
368         return 0;
369 }
370
371 int eDVBVideo::start()
372 {
373         eDebugNoNewLine("VIDEO_PLAY - ");
374         if (::ioctl(m_fd, VIDEO_PLAY) < 0)
375         {
376                 eDebug("failed (%m)");
377                 return -errno;
378         }
379         eDebug("ok");
380         return 0;
381 }
382
383 int eDVBVideo::stopPid()
384 {
385         eDebugNoNewLine("DEMUX_STOP - video - ");
386         if (::ioctl(m_fd_demux, DMX_STOP) < 0)
387         {
388                 eDebug("failed (%m)");
389                 return -errno;
390         }
391         eDebug("ok");
392         return 0;
393 }
394 #else
395 int eDVBVideo::startPid(int pid, int type)
396 {
397         int streamtype = VIDEO_STREAMTYPE_MPEG2;
398
399         if ((m_fd < 0) || (m_fd_demux < 0))
400                 return -1;
401         dmx_pes_filter_params pes;
402
403         switch(type)
404         {
405         default:
406         case MPEG2:
407                 break;
408         case MPEG4_H264:
409                 streamtype = VIDEO_STREAMTYPE_MPEG4_H264;
410                 break;
411         case MPEG1:
412                 streamtype = VIDEO_STREAMTYPE_MPEG1;
413                 break;
414         case MPEG4_Part2:
415                 streamtype = VIDEO_STREAMTYPE_MPEG4_Part2;
416                 break;
417         case VC1:
418                 streamtype = VIDEO_STREAMTYPE_VC1;
419                 break;
420         case VC1_SM:
421                 streamtype = VIDEO_STREAMTYPE_VC1_SM;
422                 break;
423         }
424
425         eDebugNoNewLine("VIDEO_SET_STREAMTYPE %d - ", streamtype);
426         if (::ioctl(m_fd, VIDEO_SET_STREAMTYPE, streamtype) < 0)
427                 eDebug("failed (%m)");
428         else
429                 eDebug("ok");
430
431         pes.pid      = pid;
432         pes.input    = DMX_IN_FRONTEND;
433         pes.output   = DMX_OUT_DECODER;
434         pes.pes_type = m_dev ? DMX_PES_VIDEO1 : DMX_PES_VIDEO0; /* FIXME */
435         pes.flags    = 0;
436         eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - video - ", pid);
437         if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
438         {
439                 eDebug("failed (%m)");
440                 return -errno;
441         }
442         eDebug("ok");
443         eDebugNoNewLine("DEMUX_START - video - ");
444         if (::ioctl(m_fd_demux, DMX_START) < 0)
445         {
446                 eDebug("failed (%m)");
447                 return -errno;
448         }
449         eDebug("ok");
450         freeze();  // why freeze here?!? this is a problem when only a pid change is requested... because of the unfreeze logic in Decoder::setState
451         eDebugNoNewLine("VIDEO_PLAY - ");
452         if (::ioctl(m_fd, VIDEO_PLAY) < 0)
453                 eDebug("failed (%m)");
454         else
455                 eDebug("ok");
456         return 0;
457 }
458 #endif
459
460 void eDVBVideo::stop()
461 {
462 #if HAVE_DVB_API_VERSION > 2
463         eDebugNoNewLine("DEMUX_STOP - video - ");
464         if (::ioctl(m_fd_demux, DMX_STOP) < 0)
465                 eDebug("failed (%m)");
466         else
467                 eDebug("ok");
468 #endif
469         eDebugNoNewLine("VIDEO_STOP - ");
470         if (::ioctl(m_fd, VIDEO_STOP, 1) < 0)
471                 eDebug("failed (%m)");
472         else
473                 eDebug("ok");
474 }
475
476 void eDVBVideo::flush()
477 {
478         eDebugNoNewLine("VIDEO_CLEAR_BUFFER - ");
479         if (::ioctl(m_fd, VIDEO_CLEAR_BUFFER) < 0)
480                 eDebug("failed (%m)");
481         else
482                 eDebug("ok");
483 }
484
485 void eDVBVideo::freeze()
486 {
487         eDebugNoNewLine("VIDEO_FREEZE - ");
488         if (::ioctl(m_fd, VIDEO_FREEZE) < 0)
489                 eDebug("failed (%m)");
490         else
491                 eDebug("ok");
492 }
493
494 void eDVBVideo::unfreeze()
495 {
496         eDebugNoNewLine("VIDEO_CONTINUE - ");
497         if (::ioctl(m_fd, VIDEO_CONTINUE) < 0)
498                 eDebug("failed (%m)");
499         else
500                 eDebug("ok");
501 }
502
503 int eDVBVideo::setSlowMotion(int repeat)
504 {
505         eDebugNoNewLine("VIDEO_SLOWMOTION(%d) - ", repeat);
506         int ret = ::ioctl(m_fd, VIDEO_SLOWMOTION, repeat);
507         if (ret < 0)
508                 eDebug("failed(%m)");
509         else
510                 eDebug("ok");
511         return ret;
512 }
513
514 int eDVBVideo::setFastForward(int skip)
515 {
516         eDebugNoNewLine("VIDEO_FAST_FORWARD(%d) - ", skip);
517         int ret = ::ioctl(m_fd, VIDEO_FAST_FORWARD, skip);
518         if (ret < 0)
519                 eDebug("failed(%m)");
520         else
521                 eDebug("ok");
522         return ret;
523 }
524
525 int eDVBVideo::getPTS(pts_t &now)
526 {
527 #if HAVE_DVB_API_VERSION < 3
528         #define VIDEO_GET_PTS_OLD           _IOR('o', 1, unsigned int*)
529         unsigned int pts;
530         int ret = ::ioctl(m_fd_video, VIDEO_GET_PTS_OLD, &pts);
531         now = pts;
532         now *= 2;
533 #else
534         int ret = ::ioctl(m_fd, VIDEO_GET_PTS, &now);
535 #endif
536         if (ret < 0)
537                 eDebug("VIDEO_GET_PTS failed(%m)");
538         return ret;
539 }
540
541 eDVBVideo::~eDVBVideo()
542 {
543         if (m_fd >= 0)
544                 ::close(m_fd);
545         if (m_fd_demux >= 0)
546                 ::close(m_fd_demux);
547 #if HAVE_DVB_API_VERSION < 3
548         if (m_fd_video >= 0)
549                 ::close(m_fd_video);
550 #endif
551 }
552
553 void eDVBVideo::video_event(int)
554 {
555 #if HAVE_DVB_API_VERSION >= 3
556         struct video_event evt;
557         eDebugNoNewLine("VIDEO_GET_EVENT - ");
558         if (::ioctl(m_fd, VIDEO_GET_EVENT, &evt) < 0)
559                 eDebug("failed (%m)");
560         else
561         {
562                 eDebug("ok");
563                 if (evt.type == VIDEO_EVENT_SIZE_CHANGED)
564                 {
565                         struct iTSMPEGDecoder::videoEvent event;
566                         event.type = iTSMPEGDecoder::videoEvent::eventSizeChanged;
567                         m_aspect = event.aspect = evt.u.size.aspect_ratio == 0 ? 2 : 3;  // convert dvb api to etsi
568                         m_height = event.height = evt.u.size.h;
569                         m_width = event.width = evt.u.size.w;
570                         /* emit */ m_event(event);
571                 }
572                 else if (evt.type == VIDEO_EVENT_FRAME_RATE_CHANGED)
573                 {
574                         struct iTSMPEGDecoder::videoEvent event;
575                         event.type = iTSMPEGDecoder::videoEvent::eventFrameRateChanged;
576                         m_framerate = event.framerate = evt.u.frame_rate;
577                         /* emit */ m_event(event);
578                 }
579                 else if (evt.type == 16 /*VIDEO_EVENT_PROGRESSIVE_CHANGED*/)
580                 {
581                         struct iTSMPEGDecoder::videoEvent event;
582                         event.type = iTSMPEGDecoder::videoEvent::eventProgressiveChanged;
583                         m_progressive = event.progressive = evt.u.frame_rate;
584                         /* emit */ m_event(event);
585                 }
586                 else
587                         eDebug("unhandled DVBAPI Video Event %d", evt.type);
588         }
589 #else
590 #warning "FIXMEE!! Video Events not implemented for old api"
591 #endif
592 }
593
594 RESULT eDVBVideo::connectEvent(const Slot1<void, struct iTSMPEGDecoder::videoEvent> &event, ePtr<eConnection> &conn)
595 {
596         conn = new eConnection(this, m_event.connect(event));
597         return 0;
598 }
599
600 static int readMpegProc(char *str, int decoder)
601 {
602         int val = -1;
603         char tmp[64];
604         sprintf(tmp, "/proc/stb/vmpeg/%d/%s", decoder, str);
605         FILE *f = fopen(tmp, "r");
606         if (f)
607         {
608                 fscanf(f, "%x", &val);
609                 fclose(f);
610         }
611         return val;
612 }
613
614 static int readApiSize(int fd, int &xres, int &yres, int &aspect)
615 {
616 #if HAVE_DVB_API_VERSION >= 3
617         video_size_t size;
618         if (!::ioctl(fd, VIDEO_GET_SIZE, &size))
619         {
620                 xres = size.w;
621                 yres = size.h;
622                 aspect = size.aspect_ratio == 0 ? 2 : 3;  // convert dvb api to etsi
623                 return 0;
624         }
625 //      eDebug("VIDEO_GET_SIZE failed (%m)");
626 #endif
627         return -1;
628 }
629
630 static int readApiFrameRate(int fd, int &framerate)
631 {
632 #if HAVE_DVB_API_VERSION >= 3
633         unsigned int frate;
634         if (!::ioctl(fd, VIDEO_GET_FRAME_RATE, &frate))
635         {
636                 framerate = frate;      
637                 return 0;
638         }
639 //      eDebug("VIDEO_GET_FRAME_RATE failed (%m)");
640 #endif
641         return -1;
642 }
643
644 int eDVBVideo::getWidth()
645 {
646         if (m_width == -1)
647                 readApiSize(m_fd, m_width, m_height, m_aspect);
648         if (m_width == -1)
649                 m_width = readMpegProc("xres", m_dev);
650         return m_width;
651 }
652
653 int eDVBVideo::getHeight()
654 {
655         if (m_height == -1)
656                 readApiSize(m_fd, m_width, m_height, m_aspect);
657         if (m_height == -1)
658                 m_height = readMpegProc("yres", m_dev);
659         return m_height;
660 }
661
662 int eDVBVideo::getAspect()
663 {
664         if (m_aspect == -1)
665                 readApiSize(m_fd, m_width, m_height, m_aspect);
666         if (m_aspect == -1)
667                 m_aspect = readMpegProc("aspect", m_dev);
668         return m_aspect;
669 }
670
671 int eDVBVideo::getProgressive()
672 {
673         if (m_progressive == -1)
674                 m_progressive = readMpegProc("progressive", m_dev);
675         return m_progressive;
676 }
677
678 int eDVBVideo::getFrameRate()
679 {
680         if (m_framerate == -1)
681                 readApiFrameRate(m_fd, m_framerate);
682         if (m_framerate == -1)
683                 m_framerate = readMpegProc("framerate", m_dev);
684         return m_framerate;
685 }
686
687 DEFINE_REF(eDVBPCR);
688
689 eDVBPCR::eDVBPCR(eDVBDemux *demux): m_demux(demux)
690 {
691         char filename[128];
692 #if HAVE_DVB_API_VERSION < 3
693         sprintf(filename, "/dev/dvb/card%d/demux%d", demux->adapter, demux->demux);
694 #else
695         sprintf(filename, "/dev/dvb/adapter%d/demux%d", demux->adapter, demux->demux);
696 #endif
697         m_fd_demux = ::open(filename, O_RDWR);
698         if (m_fd_demux < 0)
699                 eWarning("%s: %m", filename);
700 }
701
702 #if HAVE_DVB_API_VERSION < 3
703 int eDVBPCR::setPid(int pid)
704 {
705         if (m_fd_demux < 0)
706                 return -1;
707         dmx_pes_filter_params pes;
708
709         pes.pid      = pid;
710         pes.input    = DMX_IN_FRONTEND;
711         pes.output   = DMX_OUT_DECODER;
712         pes.pes_type = DMX_PES_PCR;
713         pes.flags    = 0;
714
715         eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - pcr - ", pid);
716         if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
717         {
718                 eDebug("failed (%m)");
719                 return -errno;
720         }
721         eDebug("ok");
722         return 0;
723 }
724
725 int eDVBPCR::startPid()
726 {
727         if (m_fd_demux < 0)
728                 return -1;
729         eDebugNoNewLine("DEMUX_START - pcr - ");
730         if (::ioctl(m_fd_demux, DMX_START) < 0)
731         {
732                 eDebug("failed (%m)");
733                 return -errno;
734         }
735         eDebug("ok");
736         return 0;
737 }
738 #else
739 int eDVBPCR::startPid(int pid)
740 {
741         if (m_fd_demux < 0)
742                 return -1;
743         dmx_pes_filter_params pes;
744
745         pes.pid      = pid;
746         pes.input    = DMX_IN_FRONTEND;
747         pes.output   = DMX_OUT_DECODER;
748         pes.pes_type = DMX_PES_PCR;
749         pes.flags    = 0;
750         eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - pcr - ", pid);
751         if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
752         {
753                 eDebug("failed (%m)");
754                 return -errno;
755         }
756         eDebug("ok");
757         eDebugNoNewLine("DEMUX_START - pcr - ");
758         if (::ioctl(m_fd_demux, DMX_START) < 0)
759         {
760                 eDebug("failed (%m)");
761                 return -errno;
762         }
763         eDebug("ok");
764         return 0;
765 }
766 #endif
767
768 void eDVBPCR::stop()
769 {
770         eDebugNoNewLine("DEMUX_STOP - pcr - ");
771         if (::ioctl(m_fd_demux, DMX_STOP) < 0)
772                 eDebug("failed(%m)");
773         else
774                 eDebug("ok");
775 }
776
777 eDVBPCR::~eDVBPCR()
778 {
779         if (m_fd_demux >= 0)
780                 ::close(m_fd_demux);
781 }
782
783 DEFINE_REF(eDVBTText);
784
785 eDVBTText::eDVBTText(eDVBDemux *demux): m_demux(demux)
786 {
787         char filename[128];
788 #if HAVE_DVB_API_VERSION < 3
789         sprintf(filename, "/dev/dvb/card%d/demux%d", demux->adapter, demux->demux);
790 #else
791         sprintf(filename, "/dev/dvb/adapter%d/demux%d", demux->adapter, demux->demux);
792 #endif
793         m_fd_demux = ::open(filename, O_RDWR);
794         if (m_fd_demux < 0)
795                 eWarning("%s: %m", filename);
796 }
797
798 int eDVBTText::startPid(int pid)
799 {
800         if (m_fd_demux < 0)
801                 return -1;
802         dmx_pes_filter_params pes;
803
804         pes.pid      = pid;
805         pes.input    = DMX_IN_FRONTEND;
806         pes.output   = DMX_OUT_DECODER;
807         pes.pes_type = DMX_PES_TELETEXT;
808         pes.flags    = 0;
809
810         eDebugNoNewLine("DMX_SET_PES_FILTER(0x%02x) - ttx - ", pid);
811         if (::ioctl(m_fd_demux, DMX_SET_PES_FILTER, &pes) < 0)
812         {
813                 eDebug("failed(%m)");
814                 return -errno;
815         }
816         eDebug("ok");
817         eDebugNoNewLine("DEMUX_START - ttx - ");
818         if (::ioctl(m_fd_demux, DMX_START) < 0)
819         {
820                 eDebug("failed(%m)");
821                 return -errno;
822         }
823         eDebug("ok");
824         return 0;
825 }
826
827 void eDVBTText::stop()
828 {
829         eDebugNoNewLine("DEMUX_STOP - ttx - ");
830         if (::ioctl(m_fd_demux, DMX_STOP) < 0)
831                 eDebug("failed(%m)");
832         else
833                 eDebug("ok");
834 }
835
836 eDVBTText::~eDVBTText()
837 {
838         if (m_fd_demux >= 0)
839                 ::close(m_fd_demux);
840 }
841
842 DEFINE_REF(eTSMPEGDecoder);
843
844 int eTSMPEGDecoder::setState()
845 {
846         int res = 0;
847
848         int noaudio = (m_state != statePlay) && (m_state != statePause);
849         int nott = noaudio; /* actually same conditions */
850
851         if ((noaudio && m_audio) || (!m_audio && !noaudio))
852                 m_changed |= changeAudio | changeState;
853
854         if ((nott && m_text) || (!m_text && !nott))
855                 m_changed |= changeText | changeState;
856
857         const char *decoder_states[] = {"stop", "pause", "play", "decoderfastforward", "trickmode", "slowmotion"};
858         eDebug("decoder state: %s, vpid=%d, apid=%d", decoder_states[m_state], m_vpid, m_apid);
859
860         bool changed = !!m_changed;
861 #if HAVE_DVB_API_VERSION < 3
862         bool checkAVSync = m_changed & (changeAudio|changeVideo|changePCR);
863         if (m_changed & changeAudio && m_audio)
864                 m_audio->stopPid();
865         if (m_changed & changeVideo && m_video)
866                 m_video->stopPid();
867         if (m_changed & changePCR && m_pcr)
868         {
869                 m_pcr->stop();
870                 m_pcr=0;
871                 if (!(m_pcrpid >= 0 && m_pcrpid < 0x1ff))
872                         m_changed &= ~changePCR;
873         }
874         if (m_changed & changeAudio && m_audio)
875         {
876                 m_audio->stop();
877                 m_audio=0;
878                 if (!(m_apid >= 0 && m_apid < 0x1ff))
879                         m_changed &= ~changeAudio;
880         }
881         if (m_changed & changeVideo && m_video)
882         {
883                 m_video->stop();
884                 m_video=0;
885                 m_video_event_conn=0;
886                 if (!(m_vpid >= 0 && m_vpid < 0x1ff))
887                         m_changed &= ~changeVideo;
888         }
889         if (m_changed & changeVideo)
890         {
891                 m_video = new eDVBVideo(m_demux, m_decoder);
892                 m_video->connectEvent(slot(*this, &eTSMPEGDecoder::video_event), m_video_event_conn);
893                 if (m_video->setPid(m_vpid))
894                         res -1;
895         }
896         if (m_changed & changePCR)
897         {
898                 m_pcr = new eDVBPCR(m_demux);
899                 if (m_pcr->setPid(m_pcrpid))
900                         res = -1;
901         }
902         if (m_changed & changeAudio)
903         {
904                 m_audio = new eDVBAudio(m_demux, m_decoder);
905                 if (m_audio->setPid(m_apid, m_atype))
906                         res = -1;
907         }
908         if (m_changed & changePCR)
909         {
910                 if (m_pcr->startPid())
911                         res = -1;
912                 m_changed &= ~changePCR;
913         }
914         else if (checkAVSync && m_audio && m_video)
915         {
916                 if (m_audio->setAVSync(1))
917                         res = -1;
918         }
919         if (m_changed & changeVideo)
920         {
921                 if (m_video->startPid() || m_video->start())
922                         res = -1;
923                 m_changed &= ~changeVideo;
924         }
925         if (m_changed & changeAudio)
926         {
927                 if (m_audio->start() || m_audio->startPid())
928                         res = -1;
929                 m_changed &= ~changeAudio;
930         }
931 #else
932         if (m_changed & changePCR)
933         {
934                 if (m_pcr)
935                         m_pcr->stop();
936                 m_pcr = 0;
937         }
938         if (m_changed & changeVideo)
939         {
940                 if (m_video)
941                 {
942                         m_video->stop();
943                         m_video = 0;
944                         m_video_event_conn = 0;
945                 }
946         }
947         if (m_changed & changeAudio)
948         {
949                 if (m_audio)
950                         m_audio->stop();
951                 m_audio = 0;
952         }
953         if (m_changed & changeText)
954         {
955                 if (m_text)
956                         m_text->stop();
957                 m_text = 0;
958         }
959         if (m_changed & changePCR)
960         {
961                 if ((m_pcrpid >= 0) && (m_pcrpid < 0x1FFF))
962                 {
963                         m_pcr = new eDVBPCR(m_demux);
964                         if (m_pcr->startPid(m_pcrpid))
965                                 res = -1;
966                 }
967                 m_changed &= ~changePCR;
968         }
969         if (m_changed & changeAudio)
970         {
971                 if ((m_apid >= 0) && (m_apid < 0x1FFF) && !noaudio)
972                 {
973                         m_audio = new eDVBAudio(m_demux, m_decoder);
974                         if (m_audio->startPid(m_apid, m_atype))
975                                 res = -1;
976                 }
977                 m_changed &= ~changeAudio;
978         }
979         if (m_changed & changeVideo)
980         {
981                 if ((m_vpid >= 0) && (m_vpid < 0x1FFF))
982                 {
983                         m_video = new eDVBVideo(m_demux, m_decoder);
984                         m_video->connectEvent(slot(*this, &eTSMPEGDecoder::video_event), m_video_event_conn);
985                         if (m_video->startPid(m_vpid, m_vtype))
986                                 res = -1;
987                 }
988                 m_changed &= ~changeVideo;
989         }
990         if (m_changed & changeText)
991         {
992                 if ((m_textpid >= 0) && (m_textpid < 0x1FFF) && !nott)
993                 {
994                         m_text = new eDVBTText(m_demux);
995                         if (m_text->startPid(m_textpid))
996                                 res = -1;
997                 }
998                 m_changed &= ~changeText;
999         }
1000 #endif
1001
1002         if (m_changed & changeState)
1003         {
1004                                         /* play, slowmotion, fast-forward */
1005                 int state_table[6][4] = 
1006                         {
1007                                 /* [stateStop] =                 */ {0, 0, 0},
1008                                 /* [statePause] =                */ {0, 0, 0},
1009                                 /* [statePlay] =                 */ {1, 0, 0},
1010                                 /* [stateDecoderFastForward] =   */ {1, 0, m_ff_sm_ratio},
1011                                 /* [stateHighspeedFastForward] = */ {1, 0, 1},
1012                                 /* [stateSlowMotion] =           */ {1, m_ff_sm_ratio, 0}
1013                         };
1014                 int *s = state_table[m_state];
1015                 if (m_video)
1016                 {
1017                         m_video->setSlowMotion(s[1]);
1018                         m_video->setFastForward(s[2]);
1019                         if (s[0])
1020                                 m_video->unfreeze();
1021                         else
1022                                 m_video->freeze();
1023                 }
1024                 if (m_audio)
1025                 {
1026                         if (s[0])
1027                                 m_audio->unfreeze();
1028                         else
1029                                 m_audio->freeze();
1030                 }
1031                 m_changed &= ~changeState;
1032         }
1033
1034         if (changed && !m_video && m_audio && m_radio_pic.length())
1035                 showSinglePic(m_radio_pic.c_str());
1036
1037         return res;
1038 }
1039
1040 int eTSMPEGDecoder::m_pcm_delay=-1,
1041         eTSMPEGDecoder::m_ac3_delay=-1;
1042
1043 RESULT eTSMPEGDecoder::setPCMDelay(int delay)
1044 {
1045         if (m_decoder == 0 && delay != m_pcm_delay )
1046         {
1047                 FILE *fp = fopen("/proc/stb/audio/audio_delay_pcm", "w");
1048                 if (fp)
1049                 {
1050                         fprintf(fp, "%x", delay*90);
1051                         fclose(fp);
1052                         m_pcm_delay = delay;
1053                         return 0;
1054                 }
1055         }
1056         return -1;
1057 }
1058
1059 RESULT eTSMPEGDecoder::setAC3Delay(int delay)
1060 {
1061         if ( m_decoder == 0 && delay != m_ac3_delay )
1062         {
1063                 FILE *fp = fopen("/proc/stb/audio/audio_delay_bitstream", "w");
1064                 if (fp)
1065                 {
1066                         fprintf(fp, "%x", delay*90);
1067                         fclose(fp);
1068                         m_ac3_delay = delay;
1069                         return 0;
1070                 }
1071         }
1072         return -1;
1073 }
1074
1075 eTSMPEGDecoder::eTSMPEGDecoder(eDVBDemux *demux, int decoder)
1076         : m_demux(demux), 
1077                 m_vpid(-1), m_vtype(-1), m_apid(-1), m_atype(-1), m_pcrpid(-1), m_textpid(-1),
1078                 m_changed(0), m_decoder(decoder), m_video_clip_fd(-1), m_showSinglePicTimer(eTimer::create(eApp))
1079 {
1080         demux->connectEvent(slot(*this, &eTSMPEGDecoder::demux_event), m_demux_event_conn);
1081         CONNECT(m_showSinglePicTimer->timeout, eTSMPEGDecoder::finishShowSinglePic);
1082         m_state = stateStop;
1083 }
1084
1085 eTSMPEGDecoder::~eTSMPEGDecoder()
1086 {
1087         finishShowSinglePic();
1088         m_vpid = m_apid = m_pcrpid = m_textpid = pidNone;
1089         m_changed = -1;
1090         setState();
1091 }
1092
1093 RESULT eTSMPEGDecoder::setVideoPID(int vpid, int type)
1094 {
1095         if ((m_vpid != vpid) || (m_vtype != type))
1096         {
1097                 m_changed |= changeVideo;
1098                 m_vpid = vpid;
1099                 m_vtype = type;
1100         }
1101         return 0;
1102 }
1103
1104 RESULT eTSMPEGDecoder::setAudioPID(int apid, int type)
1105 {
1106         if ((m_apid != apid) || (m_atype != type))
1107         {
1108                 m_changed |= changeAudio;
1109                 m_atype = type;
1110                 m_apid = apid;
1111         }
1112         return 0;
1113 }
1114
1115 int eTSMPEGDecoder::m_audio_channel = -1;
1116
1117 RESULT eTSMPEGDecoder::setAudioChannel(int channel)
1118 {
1119         if (channel == -1)
1120                 channel = ac_stereo;
1121         if (m_decoder == 0 && m_audio_channel != channel)
1122         {
1123                 if (m_audio)
1124                 {
1125                         m_audio->setChannel(channel);
1126                         m_audio_channel=channel;
1127                 }
1128                 else
1129                         eDebug("eTSMPEGDecoder::setAudioChannel but no audio decoder exist");
1130         }
1131         return 0;
1132 }
1133
1134 int eTSMPEGDecoder::getAudioChannel()
1135 {
1136         return m_audio_channel == -1 ? ac_stereo : m_audio_channel;
1137 }
1138
1139 RESULT eTSMPEGDecoder::setSyncPCR(int pcrpid)
1140 {
1141         if (m_pcrpid != pcrpid)
1142         {
1143                 m_changed |= changePCR;
1144                 m_pcrpid = pcrpid;
1145         }
1146         return 0;
1147 }
1148
1149 RESULT eTSMPEGDecoder::setTextPID(int textpid)
1150 {
1151         if (m_textpid != textpid)
1152         {
1153                 m_changed |= changeText;
1154                 m_textpid = textpid;
1155         }
1156         return 0;
1157 }
1158
1159 RESULT eTSMPEGDecoder::setSyncMaster(int who)
1160 {
1161         return -1;
1162 }
1163
1164 RESULT eTSMPEGDecoder::set()
1165 {
1166         return setState();
1167 }
1168
1169 RESULT eTSMPEGDecoder::play()
1170 {
1171         if (m_state == statePlay)
1172         {
1173                 if (!m_changed)
1174                         return 0;
1175         }
1176 //      else  
1177 /* commented out because the changeState is needed to unfreeze decoders in decoder::setState... needed by normal pmt changes
1178 tmbinc please recheck this! */
1179         {
1180                 m_state = statePlay;
1181                 m_changed |= changeState;
1182         }
1183         return setState();
1184 }
1185
1186 RESULT eTSMPEGDecoder::pause()
1187 {
1188         if (m_state == statePause)
1189                 return 0;
1190         m_state = statePause;
1191         m_changed |= changeState;
1192         return setState();
1193 }
1194
1195 RESULT eTSMPEGDecoder::setFastForward(int frames_to_skip)
1196 {
1197         if ((m_state == stateDecoderFastForward) && (m_ff_sm_ratio == frames_to_skip))
1198                 return 0;
1199
1200         m_state = stateDecoderFastForward;
1201         m_ff_sm_ratio = frames_to_skip;
1202         m_changed |= changeState;
1203         return setState();
1204
1205 //              return m_video->setFastForward(frames_to_skip);
1206 }
1207
1208 RESULT eTSMPEGDecoder::setSlowMotion(int repeat)
1209 {
1210         if ((m_state == stateSlowMotion) && (m_ff_sm_ratio == repeat))
1211                 return 0;
1212
1213         m_state = stateSlowMotion;
1214         m_ff_sm_ratio = repeat;
1215         m_changed |= changeState;
1216         return setState();
1217 }
1218
1219 RESULT eTSMPEGDecoder::setTrickmode()
1220 {
1221         if (m_state == stateTrickmode)
1222                 return 0;
1223
1224         m_state = stateTrickmode;
1225         m_changed |= changeState;
1226         return setState();
1227 }
1228
1229 RESULT eTSMPEGDecoder::flush()
1230 {
1231         if (m_audio)
1232                 m_audio->flush();
1233         if (m_video)
1234                 m_video->flush();
1235         return 0;
1236 }
1237
1238 void eTSMPEGDecoder::demux_event(int event)
1239 {
1240         switch (event)
1241         {
1242         case eDVBDemux::evtFlush:
1243                 flush();
1244                 break;
1245         default:
1246                 break;
1247         }
1248 }
1249
1250 RESULT eTSMPEGDecoder::getPTS(int what, pts_t &pts)
1251 {
1252         if (what == 0) /* auto */
1253                 what = m_video ? 1 : 2;
1254
1255         if (what == 1) /* video */
1256         {
1257                 if (m_video)
1258                         return m_video->getPTS(pts);
1259                 else
1260                         return -1;
1261         }
1262
1263         if (what == 2) /* audio */
1264         {
1265                 if (m_audio)
1266                         return m_audio->getPTS(pts);
1267                 else
1268                         return -1;
1269         }
1270
1271         return -1;
1272 }
1273
1274 RESULT eTSMPEGDecoder::setRadioPic(const std::string &filename)
1275 {
1276         m_radio_pic = filename;
1277         return 0;
1278 }
1279
1280 RESULT eTSMPEGDecoder::showSinglePic(const char *filename)
1281 {
1282         if (m_decoder == 0)
1283         {
1284                 eDebug("showSinglePic %s", filename);
1285                 int f = open(filename, O_RDONLY);
1286                 if (f >= 0)
1287                 {
1288                         struct stat s;
1289                         fstat(f, &s);
1290                         if (m_video_clip_fd == -1)
1291                                 m_video_clip_fd = open("/dev/dvb/adapter0/video0", O_WRONLY|O_NONBLOCK);
1292                         if (m_video_clip_fd >= 0)
1293                         {
1294                                 bool seq_end_avail = false;
1295                                 size_t pos=0;
1296                                 unsigned char pes_header[] = { 0x00, 0x00, 0x01, 0xE0, 0x00, 0x00, 0x80, 0x00, 0x00 };
1297                                 unsigned char seq_end[] = { 0x00, 0x00, 0x01, 0xB7 };
1298                                 unsigned char iframe[s.st_size];
1299                                 unsigned char stuffing[8192];
1300                                 int streamtype = VIDEO_STREAMTYPE_MPEG2;
1301                                 memset(stuffing, 0, 8192);
1302                                 read(f, iframe, s.st_size);
1303                                 if (ioctl(m_video_clip_fd, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_MEMORY) < 0)
1304                                         eDebug("VIDEO_SELECT_SOURCE MEMORY failed (%m)");
1305                                 if (ioctl(m_video_clip_fd, VIDEO_SET_STREAMTYPE, streamtype) < 0)
1306                                         eDebug("VIDEO_SET_STREAMTYPE failed(%m)");
1307                                 if (ioctl(m_video_clip_fd, VIDEO_PLAY) < 0)
1308                                         eDebug("VIDEO_PLAY failed (%m)");
1309                                 if (ioctl(m_video_clip_fd, VIDEO_CONTINUE) < 0)
1310                                         eDebug("video: VIDEO_CONTINUE: %m");
1311                                 if (ioctl(m_video_clip_fd, VIDEO_CLEAR_BUFFER) < 0)
1312                                         eDebug("video: VIDEO_CLEAR_BUFFER: %m");
1313                                 while(pos <= (s.st_size-4) && !(seq_end_avail = (!iframe[pos] && !iframe[pos+1] && iframe[pos+2] == 1 && iframe[pos+3] == 0xB7)))
1314                                         ++pos;
1315                                 if ((iframe[3] >> 4) != 0xE) // no pes header
1316                                         write(m_video_clip_fd, pes_header, sizeof(pes_header));
1317                                 else
1318                                         iframe[4] = iframe[5] = 0x00;
1319                                 write(m_video_clip_fd, iframe, s.st_size);
1320                                 if (!seq_end_avail)
1321                                         write(m_video_clip_fd, seq_end, sizeof(seq_end));
1322                                 write(m_video_clip_fd, stuffing, 8192);
1323                                 m_showSinglePicTimer->start(150, true);
1324                         }
1325                         close(f);
1326                 }
1327                 else
1328                 {
1329                         eDebug("couldnt open %s", filename);
1330                         return -1;
1331                 }
1332         }
1333         else
1334         {
1335                 eDebug("only show single pics on first decoder");
1336                 return -1;
1337         }
1338         return 0;
1339 }
1340
1341 void eTSMPEGDecoder::finishShowSinglePic()
1342 {
1343         if (m_video_clip_fd >= 0)
1344         {
1345                 if (ioctl(m_video_clip_fd, VIDEO_STOP, 0) < 0)
1346                         eDebug("VIDEO_STOP failed (%m)");
1347                 if (ioctl(m_video_clip_fd, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_DEMUX) < 0)
1348                                 eDebug("VIDEO_SELECT_SOURCE DEMUX failed (%m)");
1349                 close(m_video_clip_fd);
1350                 m_video_clip_fd = -1;
1351         }
1352 }
1353
1354 RESULT eTSMPEGDecoder::connectVideoEvent(const Slot1<void, struct videoEvent> &event, ePtr<eConnection> &conn)
1355 {
1356         conn = new eConnection(this, m_video_event.connect(event));
1357         return 0;
1358 }
1359
1360 void eTSMPEGDecoder::video_event(struct videoEvent event)
1361 {
1362         /* emit */ m_video_event(event);
1363 }
1364
1365 int eTSMPEGDecoder::getVideoWidth()
1366 {
1367         if (m_video)
1368                 return m_video->getWidth();
1369         return -1;
1370 }
1371
1372 int eTSMPEGDecoder::getVideoHeight()
1373 {
1374         if (m_video)
1375                 return m_video->getHeight();
1376         return -1;
1377 }
1378
1379 int eTSMPEGDecoder::getVideoProgressive()
1380 {
1381         if (m_video)
1382                 return m_video->getProgressive();
1383         return -1;
1384 }
1385
1386 int eTSMPEGDecoder::getVideoFrameRate()
1387 {
1388         if (m_video)
1389                 return m_video->getFrameRate();
1390         return -1;
1391 }
1392
1393 int eTSMPEGDecoder::getVideoAspect()
1394 {
1395         if (m_video)
1396                 return m_video->getAspect();
1397         return -1;
1398 }