]> git.vomp.tv Git - vompclient.git/blob - dvbsubtitles.cc
Fix segfault in subs unpause
[vompclient.git] / dvbsubtitles.cc
1 /*
2     Copyright 2008 Mark Calderbank, 2020 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP.  If not, see <https://www.gnu.org/licenses/>.
18 */
19 /*  Sections of code and general methods adopted from VDR version 1.6.0
20  *  dvbsubtitle.c (GPL version 2 or later)
21  *  Original author: Marco Schluessler <marco@lordzodiac.de>
22  *  With some input from the "subtitle plugin"
23  *  by Pekka Virtanen <pekka.virtanen@sci.fi>
24 */
25
26 /*  Reference ETSI EN 300 743 V1.3.1 (2006-11) */
27
28
29 #define DVBSDEBUG 0
30
31
32 #ifdef DVBSDEBUG
33 #include <stdio.h>
34 #endif
35
36 #include "defines.h"
37 #include "demuxer.h"
38 #include "osdreceiver.h"
39 #include "video.h"
40 #include "log.h"
41
42 #include "dvbsubtitles.h"
43
44 DVBSubtitleCLUT::DVBSubtitleCLUT()
45 : version(0xFF),
46   palette2(2),
47   palette4(4),
48   palette8(8)
49 {
50   for (int i = 0; i < 4; ++i) palette2.setColour(i, defaultPalette2[i]);
51   for (int i = 0; i < 16; ++i) palette4.setColour(i, defaultPalette4[i]);
52   for (int i = 0; i < 256; ++i) palette8.setColour(i, defaultPalette8[i]);
53 }
54
55 const ULONG DVBSubtitleCLUT::defaultPalette2[4] = {
56   0x00000000, 0xFFFFFFFF, 0xFF000000, 0xFF808080
57 };
58
59 const ULONG DVBSubtitleCLUT::defaultPalette4[16] = {
60   0x00000000, 0xFFFF0000, 0xFF00FF00, 0xFFFFFF00,
61   0xFF0000FF, 0xFFFF00FF, 0xFF00FFFF, 0xFFFFFFFF,
62   0xFF000000, 0xFF800000, 0xFF008000, 0xFF808000,
63   0xFF000080, 0xFF800080, 0xFF008080, 0xFF808080
64 };
65
66 const ULONG DVBSubtitleCLUT::defaultPalette8[256] = {
67   0x00000000, 0x40FF0000, 0x4000FF00, 0x40FFFF00, // 0000 0000
68   0x400000FF, 0x40FF00FF, 0x4000FFFF, 0x40FFFFFF, // 0000 0100
69   0x80000000, 0x80550000, 0x80005500, 0x80555500, // 0000 1000
70   0x80000055, 0x80550055, 0x80005555, 0x80555555, // 0000 1100
71   0xFFAA0000, 0xFFFF0000, 0xFFAA5500, 0xFFFF5500, // 0001 0000
72   0xFFAA0055, 0xFFFF0055, 0xFFAA5555, 0xFFFF5555, // 0001 0100
73   0x80AA0000, 0x80FF0000, 0x80AA5500, 0x80FF5500, // 0001 1000
74   0x80AA0055, 0x80FF0055, 0x80AA5555, 0x80FF5555, // 0001 1100
75   0xFF00AA00, 0xFF55AA00, 0xFF00FF00, 0xFF55FF00, // 0010 0000
76   0xFF00AA55, 0xFF55AA55, 0xFF00FF55, 0xFF55FF55, // 0010 0100
77   0x8000AA00, 0x8055AA00, 0x8000FF00, 0x8055FF00, // 0010 1000
78   0x8000AA55, 0x8055AA55, 0x8000FF55, 0x8055FF55, // 0010 1100
79   0xFFAAAA00, 0xFFFFAA00, 0xFFAAFF00, 0xFFFFFF00, // 0011 0000
80   0xFFAAAA55, 0xFFFFAA55, 0xFFAAFF55, 0xFFFFFF55, // 0011 0100
81   0x80AAAA00, 0x80FFAA00, 0x80AAFF00, 0x80FFFF00, // 0011 1000
82   0x80AAAA55, 0x80FFAA55, 0x80AAFF55, 0x80FFFF55, // 0011 1100
83   0xFF0000AA, 0xFF5500AA, 0xFF0055AA, 0xFF5555AA, // 0100 0000
84   0xFF0000FF, 0xFF5500FF, 0xFF0055FF, 0xFF5555FF, // 0100 0100
85   0x800000AA, 0x805500AA, 0x800055AA, 0x805555AA, // 0100 1000
86   0x800000FF, 0x805500FF, 0x800055FF, 0x805555FF, // 0100 1100
87   0xFFAA00AA, 0xFFFF00AA, 0xFFAA55AA, 0xFFFF55AA, // 0101 0000
88   0xFFAA00FF, 0xFFFF00FF, 0xFFAA55FF, 0xFFFF55FF, // 0101 0100
89   0x80AA00AA, 0x80FF00AA, 0x80AA55AA, 0x80FF55AA, // 0101 1000
90   0x80AA00FF, 0x80FF00FF, 0x80AA55FF, 0x80FF55FF, // 0101 1100
91   0xFF00AAAA, 0xFF55AAAA, 0xFF00FFAA, 0xFF55FFAA, // 0110 0000
92   0xFF00AAFF, 0xFF55AAFF, 0xFF00FFFF, 0xFF55FFFF, // 0110 0100
93   0x8000AAAA, 0x8055AAAA, 0x8000FFAA, 0x8055FFAA, // 0110 1000
94   0x8000AAFF, 0x8055AAFF, 0x8000FFFF, 0x8055FFFF, // 0110 1100
95   0xFFAAAAAA, 0xFFFFAAAA, 0xFFAAFFAA, 0xFFFFFFAA, // 0111 0000
96   0xFFAAAAFF, 0xFFFFAAFF, 0xFFAAFFFF, 0xFFFFFFFF, // 0111 0100
97   0x80AAAAAA, 0x80FFAAAA, 0x80AAFFAA, 0x80FFFFAA, // 0111 1000
98   0x80AAAAFF, 0x80FFAAFF, 0x80AAFFFF, 0x80FFFFFF, // 0111 1100
99   0xFF808080, 0xFFAA8080, 0xFF80AA80, 0xFFAAAA80, // 1000 0000
100   0xFF8080AA, 0xFFAA80AA, 0xFF80AAAA, 0xFFAAAAAA, // 1000 0100
101   0xFF000000, 0xFF2A0000, 0xFF002A00, 0xFF2A2A00, // 1000 1000
102   0xFF00002A, 0xFF2A002A, 0xFF002A2A, 0xFF2A2A2A, // 1000 1100
103   0xFFD58080, 0xFFFF8080, 0xFFD5AA80, 0xFFFFAA80, // 1001 0000
104   0xFFD580AA, 0xFFFF80AA, 0xFFD5AAAA, 0xFFFFAAAA, // 1001 0100
105   0xFF550000, 0xFF7F0000, 0xFF552A00, 0xFF7F2A00, // 1001 1000
106   0xFF55002A, 0xFF7F002A, 0xFF552A2A, 0xFF7F2A2A, // 1001 1100
107   0xFF80D580, 0xFFAAD580, 0xFF80FF80, 0xFFAAFF80, // 1010 0000
108   0xFF80D5AA, 0xFFAAD5AA, 0xFF80FFAA, 0xFFAAFFAA, // 1010 0100
109   0xFF005500, 0xFF2A5500, 0xFF007F00, 0xFF2A7F00, // 1010 1000
110   0xFF00552A, 0xFF2A552A, 0xFF007F2A, 0xFF2A7F2A, // 1010 1100
111   0xFFD5D580, 0xFFFFD580, 0xFFD5FF80, 0xFFFFFF80, // 1011 0000
112   0xFFD5D5AA, 0xFFFFD5AA, 0xFFD5FFAA, 0xFFFFFFAA, // 1011 0100
113   0xFF555500, 0xFF7F5500, 0xFF557F00, 0xFF7F7F00, // 1011 1000
114   0xFF55552A, 0xFF7F552A, 0xFF557F2A, 0xFF7F7F2A, // 1011 1100
115   0xFF8080D5, 0xFFAA80D5, 0xFF80AAD5, 0xFFAAAAD5, // 1100 0000
116   0xFF8080FF, 0xFFAA80FF, 0xFF80AAFF, 0xFFAAAAFF, // 1100 0100
117   0xFF000055, 0xFF2A0055, 0xFF002A55, 0xFF2A2A55, // 1100 1000
118   0xFF00007F, 0xFF2A007F, 0xFF002A7F, 0xFF2A2A7F, // 1100 1100
119   0xFFD580D5, 0xFFFF80D5, 0xFFD5AAD5, 0xFFFFAAD5, // 1101 0000
120   0xFFD580FF, 0xFFFF80FF, 0xFFD5AAFF, 0xFFFFAAFF, // 1101 0100
121   0xFF550055, 0xFF7F0055, 0xFF552A55, 0xFF7F2A55, // 1101 1000
122   0xFF55007F, 0xFF7F007F, 0xFF552A7F, 0xFF7F2A7F, // 1101 1100
123   0xFF80D5D5, 0xFFAAD5D5, 0xFF80FFD5, 0xFFAAFFD5, // 1110 0000
124   0xFF80D5FF, 0xFFAAD5FF, 0xFF80FFFF, 0xFFAAFFFF, // 1110 0100
125   0xFF005555, 0xFF2A5555, 0xFF007F55, 0xFF2A7F55, // 1110 1000
126   0xFF00557F, 0xFF2A557F, 0xFF007F7F, 0xFF2A7F7F, // 1110 1100
127   0xFFD5D5D5, 0xFFFFD5D5, 0xFFD5FFD5, 0xFFFFFFD5, // 1111 0000
128   0xFFD5D5FF, 0xFFFFD5FF, 0xFFD5FFFF, 0xFFFFFFFF, // 1111 0100
129   0xFF555555, 0xFF7F5555, 0xFF557F55, 0xFF7F7F55, // 1111 1000
130   0xFF55557F, 0xFF7F557F, 0xFF557F7F, 0xFF7F7F7F, // 1111 1100
131 };
132
133 void DVBSubtitleCLUT::setEntry(int bpp, UINT index, UCHAR Y, UCHAR Cr, UCHAR Cb, UCHAR T)
134 {
135   UCHAR Y2 = Y;
136   UCHAR A;
137   if (Y2 == 0) { Y2 = 16; A = 0; } else A = 255 - T;
138   switch (bpp)
139   {
140     case 2: palette2.setYCrCbA(index, Y2, Cr, Cb, A); break;
141     case 4: palette4.setYCrCbA(index, Y2, Cr, Cb, A); break;
142     case 8: palette8.setYCrCbA(index, Y2, Cr, Cb, A); break;
143   }
144 }
145
146 const Palette& DVBSubtitleCLUT::getPalette(int bpp) const
147 {
148   switch (bpp)
149   {
150     case 2: return palette2; break;
151     case 4: return palette4; break;
152     default: return palette8; break;
153   }
154 }
155
156 DVBSubtitleRegion::DVBSubtitleRegion()
157 : version(0xFF),
158   level(0),
159   CLUTid(0)
160 {}
161
162 void DVBSubtitleRegion::setDepth(UINT d)
163 {
164   if (d < 4) palette.setBpp(1 << d);
165 }
166
167 DVBSubtitlePage::DVBSubtitlePage()
168 : pts(0),
169   dirty(false),
170   version(0xFF),
171   timeout(0),
172   state(0),
173   serviceAcquired(false)
174 {}
175
176 void DVBSubtitlePage::setState(UCHAR s)
177 {
178   state = s;
179   if (state == 1) // Acquisition point - page refresh
180   if (!serviceAcquired) // Service not acquired lets treat this as mode change
181      state=2;
182   if (state == 2) // Mode change - new epoch
183   {
184     serviceAcquired = true;
185     dirty = true;
186     regions.clear();
187     currentDisplay.clear();
188     pendingDisplay.clear();
189     CLUTs.clear();
190   }
191 }
192
193 void DVBSubtitlePage::updateRegionPalettes(const DVBSubtitleCLUT& clut)
194 {
195   RegionMap::iterator region;
196   for (region = regions.begin(); region != regions.end(); ++region)
197     if (region->second.CLUTid == clut.CLUTid)
198       region->second.palette = clut.getPalette(region->second.palette.getBpp());
199 }
200
201 DVBSubtitleDisplayDefinition::DVBSubtitleDisplayDefinition()
202 {
203         version=0xFF;
204         displaywindow=false;
205 }
206
207 class DVBSubtitleObject
208 {
209   private:
210     UINT objectID;
211     UCHAR nonModColourFlag;
212     struct RegionRef
213     {
214       DVBSubtitleRegion* region;
215       UINT horizontalPosition;
216       UINT verticalPosition;
217     };
218     typedef std::vector<struct RegionRef> UseList;
219     UseList useList;
220     UCHAR map2to4[4];
221     UCHAR map2to8[4];
222     UCHAR map4to8[16];
223     void initMapTables();
224     UCHAR* currentMapTable;
225     UINT bitPos;
226     void drawLine(const RegionRef& use, UINT x, UINT y, UCHAR colour, UINT len);
227     UCHAR get2Bits(const UCHAR* data, UINT& index);
228     UCHAR get4Bits(const UCHAR* data, UINT& index);
229     bool decode2BppCodeString(const UCHAR* data, UINT& index, const struct RegionRef& use, UINT& x, UINT y);
230     bool decode4BppCodeString(const UCHAR* data, UINT& index, const struct RegionRef& use, UINT& x, UINT y);
231     bool decode8BppCodeString(const UCHAR* data, UINT& index, const struct RegionRef& use, UINT& x, UINT y);
232   public:
233     DVBSubtitleObject(UINT id = 0);
234     void setNonModColourFlag(UCHAR flag) { nonModColourFlag = flag; }
235     void findWhereUsed(DVBSubtitlePage&);
236     void decodeSubBlock(const UCHAR* data, UINT length, bool even);
237 };
238
239 DVBSubtitleObject::DVBSubtitleObject(UINT id)
240 : objectID(id),
241   nonModColourFlag(0),
242   currentMapTable(NULL),
243   bitPos(0)
244 {
245   initMapTables();
246 }
247
248 void DVBSubtitleObject::initMapTables()
249 {
250   map2to4[0] = 0x0; map2to4[1] = 0x7; map2to4[2] = 0x8; map2to4[3] = 0xF;
251   map2to8[0] = 0x00; map2to8[1] = 0x77; map2to8[2] = 0x88; map2to8[3] = 0xFF;
252   map4to8[0] = 0x00; map4to8[1] = 0x11; map4to8[2] = 0x22; map4to8[3] = 0x33;
253   map4to8[4] = 0x44; map4to8[5] = 0x55; map4to8[6] = 0x66; map4to8[7] = 0x77;
254   map4to8[8] = 0x88; map4to8[9] = 0x99; map4to8[10]= 0xAA; map4to8[11]= 0xBB;
255   map4to8[12]= 0xCC; map4to8[13]= 0xDD; map4to8[14]= 0xEE; map4to8[15]= 0xFF;
256 }
257
258 void DVBSubtitleObject::findWhereUsed(DVBSubtitlePage& page)
259 {
260   useList.clear();
261   DVBSubtitlePage::RegionMap::iterator i;
262   for (i = page.regions.begin(); i != page.regions.end(); ++i)
263   {
264     DVBSubtitleRegion::ObjectList::iterator j;
265     for (j = i->second.objects.begin(); j != i->second.objects.end(); ++j)
266     {
267       if (j->objectID == objectID)
268       {
269         struct RegionRef usage;
270         usage.region = &(i->second);
271         usage.horizontalPosition = j->horizontalPosition;
272         usage.verticalPosition = j->verticalPosition;
273         useList.push_back(usage);
274       }
275     }
276   }
277 }
278
279 void DVBSubtitleObject::drawLine(const RegionRef& use, UINT x, UINT y,
280                                  UCHAR colour, UINT len)
281 {
282   if (nonModColourFlag && colour == 1) return;
283   x += use.horizontalPosition;
284   y += use.verticalPosition;
285   for (UINT pos = x; pos < x + len; ++pos)
286     use.region->setIndex(pos, y, colour);
287 }
288
289 UCHAR DVBSubtitleObject::get2Bits(const UCHAR* data, UINT& index)
290 {
291   UCHAR result = data[index];
292   if (!bitPos)
293   {
294     index++;
295     bitPos = 8;
296   }
297   bitPos -= 2;
298   return (result >> bitPos) & 0x03;
299 }
300
301 UCHAR DVBSubtitleObject::get4Bits(const UCHAR* data, UINT& index)
302 {
303   UCHAR result = data[index];
304   if (!bitPos)
305   {
306     index++;
307     bitPos = 4;
308   }
309   else
310   {
311     result >>= 4;
312     bitPos -= 4;
313   }
314   return result & 0x0F;
315 }
316
317 bool DVBSubtitleObject::decode2BppCodeString(const UCHAR* data, UINT& index, const struct RegionRef& use, UINT& x, UINT y)
318 {
319   UINT rl = 0, colour = 0;
320   UCHAR code = get2Bits(data, index);
321   if (code)
322   {
323     colour = code;
324     rl = 1;
325   }
326   else
327   {
328     code = get2Bits(data, index);
329     if (code & 2) // Switch 1
330     {
331       rl = ((code & 1) << 2) + get2Bits(data, index) + 3;
332       colour = get2Bits(data, index);
333     }
334     else if (code & 1) rl = 1; // Colour 0
335     else
336     {
337       code = get2Bits(data, index);
338       switch (code)
339       {
340         case 0:
341           return false; // end of 2-bit/pixel_code_string
342         case 1:
343           rl = 2; // Colour 0
344           break;
345         case 2:
346           rl = (get2Bits(data, index) << 2) + get2Bits(data, index) + 12;
347           colour = get2Bits(data, index);
348           break;
349         case 3:
350           rl = (get2Bits(data, index) << 6) + (get2Bits(data, index) << 4) +
351                (get2Bits(data, index) << 2) + get2Bits(data, index) + 29;
352           colour = get2Bits(data, index);
353           break;
354       }
355     }
356   }
357   drawLine(use, x, y, colour, rl);
358   x += rl;
359   return true;
360 }
361
362 bool DVBSubtitleObject::decode4BppCodeString(const UCHAR* data, UINT& index, const struct RegionRef& use, UINT& x, UINT y)
363 {
364   UINT rl = 0, colour = 0;
365   UCHAR code = get4Bits(data, index);
366   if (code)
367   {
368     colour = code;
369     rl = 1;
370   }
371   else
372   {
373     code = get4Bits(data, index);
374     if (code & 8) // Switch 1
375     {
376       if (code & 4) // Switch 2
377       {
378         switch (code & 3) // Switch 3
379         {
380           case 0: // Colour 0
381             rl = 1;
382             break;
383           case 1: // Colour 0
384             rl = 2;
385             break;
386           case 2:
387             rl = get4Bits(data, index) + 9;
388             colour = get4Bits(data, index);
389             break;
390           case 3:
391             rl = (get4Bits(data, index) << 4) + get4Bits(data, index) + 25;
392             colour = get4Bits(data, index);
393             break;
394         }
395       }
396       else
397       {
398         rl = (code & 3) + 4;
399         colour = get4Bits(data, index);
400       }
401     }
402     else
403     {
404       if (!code) return false; // end of 4-bit/pixel_code_string
405       rl = code + 2; // Colour 0
406     }
407   }
408   drawLine(use, x, y, colour, rl);
409   x += rl;
410   return true;
411 }
412
413 bool DVBSubtitleObject::decode8BppCodeString(const UCHAR* data, UINT& index, const struct RegionRef& use, UINT& x, UINT y)
414 {
415   UINT rl = 0, colour = 0;
416   UCHAR code = data[index++];
417   if (code)
418   {
419     colour = code;
420     rl = 1;
421   }
422   else
423   {
424     code = data[index++];
425     rl = code & 0x7F;
426     if (code & 0x80)
427       colour = data[index++];
428     else if (!rl)
429       return false; // else colour 0
430   }
431   drawLine(use, x, y, colour, rl);
432   x += rl;
433   return true;
434 }
435
436 void DVBSubtitleObject::decodeSubBlock(const UCHAR* data, UINT length, bool even)
437 {
438   UseList::const_iterator use;
439   for (use = useList.begin(); use != useList.end(); ++use)
440   {
441     UINT x = 0; UINT y = even ? 0 : 1; UINT index = 0;
442     while (index < length)
443     {
444       switch (data[index++])
445       {
446         case 0x10:
447           switch (use->region->palette.getBpp())
448           {
449             case 8: currentMapTable = map2to8; break;
450             case 4: currentMapTable = map2to4; break;
451             default: currentMapTable = NULL;
452           }
453           bitPos = 8;
454           while (decode2BppCodeString(data, index, *use,x,y) && index < length);
455           if (!bitPos) index++;
456           break;
457         case 0x11:
458           if (use->region->palette.getBpp() == 8)
459             currentMapTable = map4to8;
460           else
461             currentMapTable = NULL;
462           bitPos = 4;
463           while (decode4BppCodeString(data, index, *use,x,y) && index < length);
464           if (!bitPos) index++;
465           break;
466         case 0x12:
467           while (decode8BppCodeString(data, index, *use,x,y) && index < length);
468           break;
469         case 0x20:
470           map2to4[0] = data[index] >> 4;
471           map2to4[1] = data[index++] & 0x0F;
472           map2to4[2] = data[index] >> 4;
473           map2to4[3] = data[index++] & 0x0F;
474           break;
475         case 0x21:
476           for (int i = 0; i < 4; i++) map2to8[i] = data[index++];
477           break;
478         case 0x22:
479           for (int i = 0; i < 16; i++) map4to8[i] = data[index++];
480           break;
481         case 0xF0:
482           x = 0; y += 2;
483           break;
484       }
485     }
486   }
487 }
488
489 static ULLONG PTSDifference(ULLONG pts1, ULLONG pts2)
490 {
491   // Assume pts1, pts2 < 2^33; calculate pts1 - pts2
492   if (pts1 > pts2)
493     return pts1 - pts2;
494   else
495     return (1LL<<33) + pts1 - pts2;
496 }
497
498 DVBSubtitles::DVBSubtitles(OSDReceiver* tosd)
499 : osd(tosd),
500   pageOnDisplay(65536),
501   running(false),
502   showing(false)
503 {
504 }
505
506 void DVBSubtitles::put(const PESPacket& packet)
507 {
508   // Player object play feed thread comes here, via the demuxer
509   // input_mutex only used in put() here, start(), stop() and threadMethod()
510   // So, roll old Thread::mutex/cond into input_mutex?
511
512   input_mutex.lock();
513   if (running)
514   {
515     if (packet.getPTS() != PESPacket::PTS_INVALID)
516     {
517       worklist.push_back(packet);
518       signalRecalcWLTO = true;
519         Log::getInstance()->log("DVBSubs", Log::DEBUG, "Received packet: PTS: %llu, size: %u, type: %u", packet.getPTS(), packet.getSize(), packet.getPacketType());
520
521
522       dvbsCond.notify_one();
523     }
524     else
525     {
526         Log::getInstance()->log("DVBSubs", Log::DEBUG, "PUT DROPPING INVALID PACKET: PTS: %llu, size: %u, type: %u", packet.getPTS(), packet.getSize(), packet.getPacketType());
527     }
528   }
529   input_mutex.unlock();
530 }
531
532 bool DVBSubtitles::decodePacket(const PESPacket& packet)
533 {
534   const UCHAR* data = packet.getData();
535   UINT packetSize = packet.getSize();
536   if (packetSize < 9) return false;
537   UINT segmentOffset = data[8] + 11;
538   while (packetSize > segmentOffset)
539   {
540     if (data[segmentOffset] == 0xFF) return true; // 'End of PES' marker
541     if (data[segmentOffset] != 0x0F) return false; // Out of sync
542     if (packetSize <= segmentOffset + 5) return false; // Packet truncated
543     UCHAR segmentType = data[segmentOffset + 1];
544     UINT pageID = (data[segmentOffset + 2] << 8)
545                  + data[segmentOffset + 3];
546     UINT segmentLength = (data[segmentOffset + 4] << 8)
547                         + data[segmentOffset + 5];
548     if (pageOnDisplay == 65536) pageOnDisplay = pageID;
549     if (pageOnDisplay != pageID) return false;
550     if (packetSize <= segmentOffset + 5 + segmentLength) return false;
551     const UCHAR* segmentData = &data[segmentOffset + 6];
552     pages[pageID]; // Create this page if it doesn't exist
553     PageMap::iterator pageEntry = pages.find(pageID);
554     if (pageEntry == pages.end()) return false;
555     DVBSubtitlePage& page = pageEntry->second;
556     if (packet.getPTS() != PESPacket::PTS_INVALID) page.pts = packet.getPTS();
557     switch (segmentType)
558     {
559     case 0x10: // Page composition
560     {
561       if (segmentLength < 2) break;
562       page.setState((segmentData[1] & 0x0C) >> 2);
563       if (!page.serviceAcquired) break;
564       UCHAR pageVersion = (segmentData[1] & 0xF0) >> 4;
565       if (pageVersion == page.version) break; // No update
566       page.version = pageVersion;
567       page.timeout = segmentData[0];
568       page.pendingDisplay.clear();
569       UINT parsePos = 2;
570       while (segmentLength > parsePos + 5)
571       {
572         UCHAR regionID = segmentData[parsePos];
573         page.regions[regionID]; // Create the region if it doesn't exist
574         DVBSubtitlePage::Coords coords;
575         coords.x = (segmentData[parsePos + 2] << 8) +
576                     segmentData[parsePos + 3];
577         coords.y = (segmentData[parsePos + 4] << 8) +
578                     segmentData[parsePos + 5];
579         page.pendingDisplay[regionID] = coords;
580         parsePos += 6;
581       }
582       break;
583     }
584     case 0x11: // Region composition
585     {
586       if (segmentLength < 10) break;
587       UCHAR regionID = segmentData[0];
588       page.regions[regionID]; // Create the region if it doesn't exist
589       DVBSubtitlePage::RegionMap::iterator regionEntry;
590       regionEntry = page.regions.find(regionID);
591       if (regionEntry == page.regions.end()) return false;
592       DVBSubtitleRegion& region = regionEntry->second;
593       UCHAR regionVersion = (segmentData[1] & 0xF0) >> 4;
594       if (regionVersion == region.version) break; // No update
595       region.version = regionVersion;
596       bool regionFillFlag = (segmentData[1] & 0x08) >> 3;
597       UINT regionWidth  = (segmentData[2] << 8) + segmentData[3];
598       UINT regionHeight = (segmentData[4] << 8) + segmentData[5];
599       region.setSize(regionWidth, regionHeight);
600       region.level = (segmentData[6] & 0xE0) >> 5;
601       region.setDepth((segmentData[6] & 0x1C) >> 2);
602       region.CLUTid = segmentData[7];
603       page.CLUTs[region.CLUTid]; // Create the CLUT if it doesn't exist
604       DVBSubtitlePage::CLUTMap::iterator clut;
605       clut = page.CLUTs.find(region.CLUTid);
606       if (clut == page.CLUTs.end()) return false;
607       region.palette = clut->second.getPalette(region.palette.getBpp());
608       if (regionFillFlag) switch (region.palette.getBpp())
609       {
610         case 2: region.setAllIndices((segmentData[9] & 0x0C) >> 2); break;
611         case 4: region.setAllIndices((segmentData[9] & 0xF0) >> 4); break;
612         case 8: region.setAllIndices(segmentData[8]); break;
613       }
614       region.objects.clear();
615       UINT parsePos = 10;
616       while (segmentLength > parsePos + 5)
617       {
618         UINT objectID = (segmentData[parsePos] << 8) +
619                          segmentData[parsePos + 1];
620         DVBSubtitleRegion::ObjectRef objref;
621         objref.objectID = objectID;
622         UINT objectType = (segmentData[parsePos + 2] & 0xC0) >> 6;
623         objref.horizontalPosition = ((segmentData[parsePos + 2] & 0x0F) << 8) +
624                                       segmentData[parsePos + 3];
625         objref.verticalPosition = ((segmentData[parsePos + 4] & 0x0F) << 8) +
626                                     segmentData[parsePos + 5];
627         if (objectType == 0) region.objects.push_back(objref);
628         parsePos += 6;
629       }
630       break; // case 0x11
631     }
632     case 0x12: // CLUT definition
633     {
634       if (segmentLength < 2) break;
635       UCHAR CLUTid = segmentData[0];
636       DVBSubtitlePage::CLUTMap::iterator clutEntry;
637       clutEntry = page.CLUTs.find(CLUTid);
638       if (clutEntry == page.CLUTs.end()) return false;
639       DVBSubtitleCLUT& clut = clutEntry->second;
640       clut.CLUTid = CLUTid;
641       UCHAR clutVersion = (segmentData[1] & 0xF0) >> 4;
642       if (clutVersion == clut.version) break; // No update
643       clut.version = clutVersion;
644       UINT parsePos = 2;
645       while (segmentLength > parsePos + 3)
646       {
647         UCHAR clutEntryID = segmentData[parsePos];
648         bool fullRangeFlag = segmentData[parsePos + 1] & 0x01;
649         UCHAR Y, Cr, Cb, T;
650         if (fullRangeFlag)
651         {
652           if (segmentLength <= parsePos + 5) break;
653           Y  = segmentData[parsePos + 2];
654           Cr = segmentData[parsePos + 3];
655           Cb = segmentData[parsePos + 4];
656           T  = segmentData[parsePos + 5];
657         }
658         else
659         {
660           Y   =  segmentData[parsePos + 2] & 0xFC;
661           Cr  = (segmentData[parsePos + 2] & 0x03) << 6;
662           Cr |= (segmentData[parsePos + 3] & 0xC0) >> 2;
663           Cb  = (segmentData[parsePos + 3] & 0x3C) << 2;
664           T   = (segmentData[parsePos + 3] & 0x03) << 6;
665         }
666         UCHAR entryFlags = segmentData[parsePos + 1];
667         if (entryFlags & 0x80) clut.setEntry(2, clutEntryID, Y,Cr,Cb,T);
668         if (entryFlags & 0x40) clut.setEntry(4, clutEntryID, Y,Cr,Cb,T);
669         if (entryFlags & 0x20) clut.setEntry(8, clutEntryID, Y,Cr,Cb,T);
670         parsePos += fullRangeFlag ? 6 : 4;
671       }
672       page.updateRegionPalettes(clut);
673       break; // case 0x12
674     }
675     case 0x13: // Object data
676     {
677       if (segmentLength < 3) break;
678       UINT objectID = (segmentData[0] << 8) + segmentData[1];
679       DVBSubtitleObject object(objectID);
680       object.findWhereUsed(page);
681       UCHAR codingMethod = (segmentData[2] & 0x0C) >> 2;
682       object.setNonModColourFlag(segmentData[2] & 0x01);
683       if (codingMethod == 0x00) // Coding of pixels
684       {
685         if (segmentLength < 7) break;
686         UINT topFieldLen     = (segmentData[3] << 8) + segmentData[4];
687         UINT bottomFieldLen  = (segmentData[5] << 8) + segmentData[6];
688         if (segmentLength < 7 + topFieldLen + bottomFieldLen) break;
689         object.decodeSubBlock(segmentData + 7, topFieldLen, true);
690         if (bottomFieldLen)
691           object.decodeSubBlock(segmentData + 7 + topFieldLen,
692                                 bottomFieldLen, false);
693         else
694           object.decodeSubBlock(segmentData + 7, topFieldLen, false);
695       }
696       else if (codingMethod == 0x01) // Coded as a string of characters
697         ; // TODO
698       break;
699     }
700     case 0x14: {// Display definition
701         if (segmentLength<5) break;
702         UINT ddsversion=(segmentData[0]&0xf0)>>4;
703         if (ddsversion==dds.version) break; // no update ncessary
704         dds.version=ddsversion;
705         dds.displaywindow=!(!(segmentData[0]&0x08));
706         dds.framewidth=(segmentData[1] << 8) + segmentData[2];
707         dds.frameheight=(segmentData[3] << 8) + segmentData[4];
708         if (segmentLength<13) break;
709         if (dds.displaywindow) {
710                 dds.windowx=(segmentData[4] << 8) + segmentData[5];
711                 dds.windoww=(segmentData[6] << 8) + segmentData[7]-dds.windowx;
712                 dds.windowy=(segmentData[8] << 8) + segmentData[9];
713                 dds.windowh=(segmentData[10] << 8) + segmentData[11]-dds.windowy;
714         } else {
715                 dds.windowx=0;
716                 dds.windowy=0;
717                 dds.windoww=dds.framewidth;
718                 dds.windowh=dds.frameheight;
719         }
720       break;
721     }
722     case 0x80: // End of display set
723       finishPage(page);
724       page.dirty = false;
725       page.currentDisplay = page.pendingDisplay;
726       break;
727     }
728     segmentOffset += (segmentLength + 6);
729   }
730   return false; // Fall through from while loop: we ran out of data
731 }
732
733 void DVBSubtitles::finishPage(const DVBSubtitlePage& page)
734 {
735   if (page.dirty && !osdMenuShowing) 
736   {
737     osd->clearOSD();
738   }
739
740   DVBSubtitlePage::DisplayMap::const_iterator i,j;
741   for (i = page.currentDisplay.begin(); i != page.currentDisplay.end(); ++i)
742   { // For each currently displayed region, if the region has been
743     // moved or removed, blank out its previous screen position
744     j = page.pendingDisplay.find(i->first);
745     if (j == page.pendingDisplay.end() ||
746         j->second.x != i->second.x ||
747         j->second.y != i->second.y)
748     {
749       DVBSubtitlePage::RegionMap::const_iterator region_iter;
750       region_iter = page.regions.find(i->first);
751       if (region_iter == page.regions.end()) continue;
752       Log::getInstance()->log("DVBSubs", Log::DEBUG, "Clear region %d", i->first);
753       if (!osdMenuShowing) osd->clearOSDArea(i->second.x, i->second.y,
754            region_iter->second.getWidth(), region_iter->second.getHeight(),dds);
755     }
756   }
757
758   for (i = page.pendingDisplay.begin(); i != page.pendingDisplay.end(); ++i)
759   { // Display each pending region
760     DVBSubtitlePage::RegionMap::const_iterator region_iter;
761     region_iter = page.regions.find(i->first);
762     if (region_iter == page.regions.end()) continue;
763     Log::getInstance()->log("DVBSubs", Log::DEBUG, "Display region %d", i->first);
764     if (!osdMenuShowing) osd->drawOSDBitmap(i->second.x, i->second.y,
765          region_iter->second,dds);
766   }
767 // after displaying regions set the page timeout timer
768
769   subtitleTimeoutPoint = std::chrono::system_clock::now() + std::chrono::seconds(page.timeout);
770   subtitleTimeoutPointActive = true;
771
772   Log::getInstance()->log("DVBSubs", Log::DEBUG, "SubtitleTimeout %d", page.timeout);
773 }
774
775 void DVBSubtitles::start()
776 {
777   Log::getInstance()->log("DVBSubs", Log::DEBUG, "subs start");
778
779 #ifdef DVBSDEBUG
780
781   DBG = fopen("dfifo", "a");
782
783   fprintf(DBG, "\033[H\033[2J");
784   fflush(DBG);
785
786 #endif
787
788   input_mutex.lock();
789
790   dds=DVBSubtitleDisplayDefinition();
791   running = true;
792
793   dvbsThread = std::thread([this] { threadMethod(); });
794
795   input_mutex.unlock();
796 }
797
798 void DVBSubtitles::stop()
799 {
800   Log::getInstance()->log("DVBSubs", Log::DEBUG, "subs stop");
801
802   input_mutex.lock();
803   if (!running)
804   {
805     Log::getInstance()->log("DVBSubs", Log::ERR, "STOP called, already dead!");
806     input_mutex.unlock();
807     return;
808   }
809
810   running = false; // disables put()
811   signalStop = true;
812   dvbsCond.notify_one();
813   input_mutex.unlock();
814   dvbsThread.join();
815
816   worklist.clear();
817   input_mutex.unlock();
818
819   clearDisplayedPages();
820
821
822 #ifdef DVBSDEBUG
823   fclose(DBG);
824 #endif
825 }
826
827 void DVBSubtitles::show()
828 {
829   Log::getInstance()->log("DVBSubs", Log::DEBUG, "subs show");
830
831   output_mutex.lock();
832   showing = true;
833   output_mutex.unlock();
834 }
835
836 void DVBSubtitles::hide()
837 {
838   Log::getInstance()->log("DVBSubs", Log::DEBUG, "subs hide");
839
840   showing = false;
841   clearDisplayedPages();
842 }
843
844 void DVBSubtitles::clearDisplayedPages()
845 {
846   output_mutex.lock();
847
848   PageMap::const_iterator pageEntry = pages.find(pageOnDisplay);
849   if (pageEntry != pages.end())
850   {
851     const DVBSubtitlePage& page = pageEntry->second;
852     DVBSubtitlePage::DisplayMap::const_iterator i;
853     for (i = page.currentDisplay.begin(); i != page.currentDisplay.end(); ++i)
854     {
855       DVBSubtitlePage::RegionMap::const_iterator region_iter;
856       region_iter = page.regions.find(i->first);
857       if (region_iter == page.regions.end()) continue;
858       if (!osdMenuShowing) osd->clearOSDArea(i->second.x, i->second.y,
859            region_iter->second.getWidth(), region_iter->second.getHeight(),dds);
860     }
861   }
862   pages.clear();
863   pageOnDisplay = 65536;
864
865   output_mutex.unlock();
866 }
867
868 void DVBSubtitles::pause()
869 {
870   Log::getInstance()->log("DVBSubs", Log::DEBUG, "subs pause");
871
872   input_mutex.lock();
873   if (!running)
874   {
875     Log::getInstance()->log("DVBSubs", Log::ERR, "pause called, already dead!");
876     input_mutex.unlock();
877     return;
878   }
879
880   signalPause = true;
881   dvbsCond.notify_one();
882   input_mutex.unlock();
883 }
884
885 void DVBSubtitles::unPause()
886 {
887   Log::getInstance()->log("DVBSubs", Log::DEBUG, "subs unpause");
888
889   input_mutex.lock();
890   if (!running)
891   {
892     Log::getInstance()->log("DVBSubs", Log::ERR, "pause called, already dead!");
893     input_mutex.unlock();
894     return;
895   }
896
897   signalRecalcWLTO = true;
898   dvbsCond.notify_one();
899   input_mutex.unlock();
900 }
901
902 #if WIN32
903 // FIXME win pragma
904 #pragma warning(disable : 4146)
905 #endif
906
907 void DVBSubtitles::threadMethod()
908 {
909   std::unique_lock<std::mutex> ul(input_mutex);  // Lock here, force start() to return before this does anything
910
911   bool waitExpireST{};
912   bool waitExpireWL{};
913
914   std::chrono::time_point<std::chrono::system_clock> worklistTimeoutPoint;
915   bool worklistTimeoutPointActive{};
916
917   while(1)
918   {
919 #ifdef DVBSDEBUG
920
921     // TEMP DEBUG - re-enable nowPTS below when this goes
922     ULLONG nowPTS = Video::getInstance()->getCurrentTimestamp();
923     fprintf(DBG, "\033[H\033[2J");
924     fprintf(DBG, "Now: %s %llu\n", tp2str(std::chrono::system_clock::now()).c_str(), Video::getInstance()->getCurrentTimestamp());
925     for (auto p : worklist)
926     {
927       fprintf(DBG, "packet PTS %llu SIZE %i\tTIME %s\n", p.getPTS(), p.getSize(),
928         tp2str(std::chrono::system_clock::now() + std::chrono::milliseconds(PTSDifference(p.getPTS(), nowPTS) / 90)).c_str());
929     }
930     fflush(DBG);
931
932 #endif
933
934
935     if (signalStop)
936     {
937       Log::getInstance()->log("DVBSubs", Log::INFO, "Thread exiting");
938       signalStop = false;
939       return;
940     }
941     else if (signalPause)
942     {
943       signalPause = false;
944       subtitleTimeoutPointActive = false;
945       worklistTimeoutPointActive = false;
946     }
947     else if (signalRecalcWLTO) // once per incoming packet, and other times
948     {
949       signalRecalcWLTO = false;
950 #ifndef DVBSDEBUG
951       ULLONG nowPTS = Video::getInstance()->getCurrentTimestamp();
952 #endif
953
954       if (nowPTS == 0)
955       {
956         // Video is not started yet. DVBSub packet has come in. Don't set worklistTimeoutPoint,
957         // just store the packet and allow next signal to start things off
958         Log::getInstance()->log("DVBSubs", Log::DEBUG, "signalRecalcWLTO but Video PTS == 0");
959       }
960       else if (worklist.size()) // It is possible to be called to recalc when there are no packets
961       {
962         worklistTimeoutPointActive = true;
963         Log::getInstance()->log("DVBSubs", Log::DEBUG, "Calc: Num packets available: %i", worklist.size());
964
965         ULLONG pktPTS = worklist.front().getPTS();
966         ULLONG diff = PTSDifference(pktPTS, nowPTS);
967         diff /= 90; // convert diff to ms (PTS difference is in 1/90000s)
968         if (diff < 60 * 1000)
969         {
970           worklistTimeoutPoint = std::chrono::system_clock::now() + std::chrono::milliseconds(diff);
971           Log::getInstance()->log("DVBSubs", Log::DEBUG, "Calc'd new worklistTimeoutPoint");
972         }
973         else
974         {
975           // We have a problem. An action so far in the future should have
976           // been culled. Probably the action is already due and PTSDifference
977           // wrapped around. Therefore we sleep for a minimal time instead.
978
979           // FIXME check if this still works
980           worklistTimeoutPoint = std::chrono::system_clock::now();
981           Log::getInstance()->log("DVBSubs", Log::DEBUG, "Problem packet");
982         }
983       }
984     }
985     else if (waitExpireST) // do real work - subtitletimeout
986     {
987       Log::getInstance()->log("DVBSubs", Log::DEBUG, "Subtitle timeout occurred");
988       waitExpireST = false;
989       subtitleTimeoutPointActive = false;
990       output_mutex.lock();
991       if (showing && !osdMenuShowing) osd->clearOSD();
992       output_mutex.unlock();
993     }
994     else if (waitExpireWL) // do real work - worklist - could be multiple packets to do
995     {
996       Log::getInstance()->log("DVBSubs", Log::DEBUG, "Something to do");
997
998       waitExpireWL = false;
999       worklistTimeoutPointActive = false;
1000
1001       ULLONG nowPTS = Video::getInstance()->getCurrentTimestamp();
1002
1003       // Guaranteed to be at least one packet in the worklist
1004       PESPacket packet = worklist.front();
1005       ULLONG pktPTS = worklist.front().getPTS();
1006
1007       while(1)
1008       {
1009         worklist.pop_front();
1010
1011         output_mutex.lock();
1012         if (showing) decodePacket(packet);
1013         output_mutex.unlock();
1014
1015         // Repeat this loop only if there is another packet and it's the same PTS as this one
1016         // Not sure this ever happens.
1017         if (worklist.size() && (worklist.front().getPTS() == pktPTS))
1018         {
1019           packet = worklist.front();
1020           continue;
1021         }
1022         else
1023         {
1024           break;
1025         }
1026       }
1027
1028       if (worklist.size())
1029       {
1030         signalRecalcWLTO = true;
1031         continue; // restart the loop and force a recalc
1032       }
1033
1034
1035         /*
1036
1037
1038         if (PTSDifference(nowPTS, pktPTS) < 2*90000     // need this? we should be here exactly on time
1039          || PTSDifference(pktPTS, nowPTS) < 4000)
1040         { // It is due for processing or discarding.
1041           worklist.pop_front();
1042
1043           output_mutex.lock();
1044           if (showing) decodePacket(packet);
1045           output_mutex.unlock();
1046         }
1047         else if (PTSDifference(pktPTS, nowPTS) >= 60*90000)
1048         { // Seems like a bad or very old entry. Get rid of it.
1049           finished = false;
1050           worklist.pop_front();
1051         }
1052 */
1053
1054
1055       // Loop has exited. Either worklist is now empty or the next packet doesn't match the PTSDifference ranges
1056     }
1057     else
1058     {
1059       // Spurious?
1060     }
1061
1062     // Either:
1063     // 1. sleep until timeout
1064     // 2. sleep until next worklist
1065     // 3. sleep until signalled
1066
1067     if (!subtitleTimeoutPointActive && !worklistTimeoutPointActive)
1068     {
1069       Log::getInstance()->log("DVBSubs", Log::DEBUG, "No WL or STT. Infinite wait");
1070       dvbsCond.wait(ul); // Wait until signalled
1071     }
1072     else if (subtitleTimeoutPointActive)
1073     {
1074       if (worklistTimeoutPointActive && (worklistTimeoutPoint < subtitleTimeoutPoint))
1075       {
1076         Log::getInstance()->log("DVBSubs", Log::DEBUG, "WaitFor: WL");
1077         if (dvbsCond.wait_until(ul, worklistTimeoutPoint) == std::cv_status::timeout) waitExpireWL = true;
1078       }
1079       else
1080       {
1081         Log::getInstance()->log("DVBSubs", Log::DEBUG, "WaitFor: ST");
1082         if (dvbsCond.wait_until(ul, subtitleTimeoutPoint) == std::cv_status::timeout) waitExpireST = true;
1083       }
1084     }
1085     else
1086     {
1087       Log::getInstance()->log("DVBSubs", Log::DEBUG, "WaitFor: WL");
1088       if (dvbsCond.wait_until(ul, worklistTimeoutPoint) == std::cv_status::timeout) waitExpireWL = true;
1089     }
1090   }
1091 /*
1092
1093   struct timespec sleeptime;
1094   sleeptime.tv_sec = 0;
1095   sleeptime.tv_nsec = 0;
1096   uint64_t wakeup = 0;
1097   timeout_clear=false;
1098   while (1)
1099   {
1100     if (subtitleTimeoutPointActive && (subtitleTimeoutPoint < std::chrono::system_clock::now())) // do we have a subtitle timeout
1101     {
1102       subtitleTimeoutPointActive = false;
1103
1104
1105 //    if (SubtitleTimeout.TimedOut())  // do we have a subtitle timeout
1106 //    {
1107       output_mutex.lock();
1108       if (showing && !osdMenuShowing) {
1109           if (!timeout_clear) {
1110                   osd->clearOSD();      // if we have the timeout, lets clear the OSD
1111                   timeout_clear=true;
1112           }
1113       }
1114       output_mutex.unlock();
1115     }
1116     else if (showing)                   // if not lets check when will we have it
1117     {
1118       // Get milliseconds until the timeout
1119       wakeup = std::chrono::duration_cast<std::chrono::milliseconds>(subtitleTimeoutPoint - std::chrono::system_clock::now()).count();
1120               Log::getInstance()->log("DVBSubs", Log::DEBUG, "A1: %lu", wakeup);
1121
1122       //                 now - begin
1123        wakeup = -(SubtitleTimeout.Elapsed());
1124
1125               Log::getInstance()->log("DVBSubs", Log::DEBUG, "A2: %lu", wakeup);
1126
1127        if (wakeup > 0 && sleeptime.tv_nsec == 0 && sleeptime.tv_sec == 0)
1128        // We are not done, we still have a Subtitle Timeout!
1129        {
1130          sleeptime.tv_sec = (int)(wakeup / 1000);
1131          sleeptime.tv_nsec = (long)(wakeup % 1000) * 10000L / 1000L * 100000L;
1132          timeout_clear=false;
1133        }
1134     }
1135
1136
1137     //if (dvbsThreadStop) return;
1138     threadCheckExit();
1139
1140
1141     threadLock();
1142     if (!threadNudged)
1143     { // We have done the current work and no more has arrived. Sleep.
1144       if (sleeptime.tv_sec == 0 && sleeptime.tv_nsec == 0)
1145       {
1146         Log::getInstance()->log("DVBSubs", Log::DEBUG, "Sleeping until nudged.");
1147         threadWaitForSignal();
1148       }
1149       else
1150       {
1151         Log::getInstance()->log("DVBSubs", Log::DEBUG, "Sleeping for %d and %d", sleeptime.tv_sec, sleeptime.tv_nsec);
1152         struct timespec targetTime;
1153
1154                 getClockRealTime(&targetTime);
1155
1156         targetTime.tv_nsec += sleeptime.tv_nsec;
1157         if (targetTime.tv_nsec > 999999999)
1158         {
1159           targetTime.tv_sec += 1;
1160           targetTime.tv_nsec -= 1000000000;
1161         }
1162         targetTime.tv_sec += sleeptime.tv_sec;
1163         threadWaitForSignalTimed(&targetTime);
1164       }
1165     }
1166     threadNudged = false;
1167     threadUnlock();
1168     bool finished = false;
1169     ULLONG nowPTS, pktPTS;
1170     int wait;
1171     while (!finished)
1172     {
1173       threadCheckExit();
1174       finished = true;
1175       pktPTS = PESPacket::PTS_INVALID;
1176       input_mutex.lock();
1177       if (!worklist.empty())
1178         pktPTS = worklist.front().getPTS();
1179       input_mutex.unlock();
1180       if (pktPTS != PESPacket::PTS_INVALID)
1181       { // An entry exists in the work list
1182         nowPTS = Video::getInstance()->getCurrentTimestamp();
1183         if (PTSDifference(nowPTS, pktPTS) < 2*90000 ||
1184             PTSDifference(pktPTS, nowPTS) < 4000)
1185         { // It is due for processing or discarding.
1186           finished = false;
1187           input_mutex.lock();
1188           if (!worklist.empty())
1189           {
1190             PESPacket packet = worklist.front();
1191             worklist.pop_front();
1192             input_mutex.unlock();
1193             output_mutex.lock();
1194             if (showing) decodePacket(packet);
1195             output_mutex.unlock();
1196           }
1197           else input_mutex.unlock();
1198         }
1199         else if (PTSDifference(pktPTS, nowPTS) >= 60*90000)
1200         { // Seems like a bad or very old entry. Get rid of it.
1201           finished = false;
1202           input_mutex.lock();
1203           if (!worklist.empty()) worklist.pop_front();
1204           input_mutex.unlock();
1205         }
1206       }
1207     }
1208     // Calculate how long to sleep for
1209     wait = SUBTITLE_TIMEOUT_MS; // Max sleep while checking subtitle timeout 
1210     sleeptime.tv_sec = 0;
1211     sleeptime.tv_nsec = 0;
1212     if (pktPTS != PESPacket::PTS_INVALID)
1213     { // A future action exists
1214       nowPTS = Video::getInstance()->getCurrentTimestamp();
1215       ULLONG diff = PTSDifference(pktPTS, nowPTS);
1216       diff /= 90; // convert diff in ms as PTS difference in 1/90000s
1217       if (diff < 60 * 1000)
1218       {
1219         if (diff < (ULLONG)wait)
1220           wait = (int)diff;
1221         sleeptime.tv_nsec = (long)(wait % 1000) * 10000L / 1000L * 100000L;
1222       }
1223       else
1224         { // We have a problem. An action so far in the future should have
1225         // been culled. Probably the action is already due and PTSDifference
1226         // wrapped around. Therefore we sleep for a minimal time instead.
1227         sleeptime.tv_nsec = 1;
1228       }
1229     }
1230   }
1231
1232   */
1233 }
1234
1235 void DVBSubtitles::setOSDMenuVisibility(bool visible)
1236 {
1237   output_mutex.lock();
1238   osdMenuShowing = visible;
1239   output_mutex.unlock();
1240 }
1241