]> git.vomp.tv Git - vompclient.git/blob - osdvector.cc
Add channel status icons
[vompclient.git] / osdvector.cc
1 /*
2     Copyright 2012 Marten Richter
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, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21 #include "osdvector.h"
22 #include "surfacevector.h"
23 #include "vdr.h"
24 #include "vdrresponsepacket.h"
25 #include "command.h"
26 #include "message.h"
27
28 // The next section is activated, if the magick++ PictureReader is provided, it should be available for many POSIX platforms
29 #ifdef PICTURE_DECODER_MAGICK
30 #include <Magick++.h>
31
32 using namespace Magick;
33
34 class MagickDecoder: public OsdVector::PictureDecoder {
35 public:
36         MagickDecoder(OsdVector::PictureReader* treader): OsdVector::PictureDecoder(treader) {pictInfValid=false;};
37
38         unsigned char *decodePicture(LoadIndex index, unsigned char * buffer, unsigned int length, bool freemem);
39
40     bool getDecodedPicture( struct OsdVector::PictureInfo& pict_inf);
41
42     void freeReference(void * ref);
43
44 protected:
45     OsdVector::PictureInfo pictInf;
46         bool pictInfValid;
47 };
48
49 unsigned char * MagickDecoder::decodePicture(LoadIndex index, unsigned char * buffer, unsigned int length, bool freemem)
50 {
51         if (pictInfValid) return buffer; // does support only one image at a Time;
52         Image magicimage;
53         Blob *imageblob = new Blob();
54         Blob myblob;
55         try{
56                 Log::getInstance()->log("MagickDecoder", Log::DEBUG, "decodePicture");
57
58                 if (freemem) myblob.updateNoCopy(buffer,length,Blob::MallocAllocator);
59                 else myblob.update(buffer,length);
60                 magicimage.read(myblob);
61
62                 magicimage.write(imageblob,"RGBA");
63
64         }catch( Exception &error_ )
65         {
66                 Log::getInstance()->log("MagickDecoder", Log::DEBUG, "Libmagick: %s",error_.what());
67                 delete imageblob;
68                 Log::getInstance()->log("MagickDecoder", Log::DEBUG, "Libmagick: error mark2");
69                 unsigned char* newbuffer=(unsigned char*) malloc(length);
70                 memcpy(newbuffer,myblob.data(),length);
71                 return newbuffer;
72         }
73         pictInf.reference = (void*) imageblob;
74         pictInf.width = magicimage.columns();
75         pictInf.height = magicimage.rows();
76         pictInf.image = imageblob->data();
77         pictInf.decoder = this;
78         pictInf.type = OsdVector::PictureInfo::RGBAMemBlock;
79         pictInf.lindex = index;
80         pictInfValid = true;
81
82
83
84         // I can handle everything, so the return value is always true
85         return NULL;
86 }
87 void MagickDecoder::freeReference(void * ref)
88 {
89         Blob *todelete = (Blob*) ref;
90         delete todelete;
91 }
92
93 bool MagickDecoder::getDecodedPicture(struct OsdVector::PictureInfo& pict_inf)
94 {
95         if (!pictInfValid) return false;
96         pict_inf=pictInf;
97         pictInfValid = false;
98         return true;
99 }
100
101
102 #endif
103
104 OsdVector::OsdVector()
105 {
106         setlocale(LC_CTYPE,"C.UTF-8");
107 #ifdef PICTURE_DECODER_MAGICK
108         reader.addDecoder(new MagickDecoder(&reader));
109 #endif
110
111 }
112
113 OsdVector::~OsdVector()
114 {
115 }
116
117
118 int OsdVector::getFD()
119 {
120         return 0;
121 }
122
123 void OsdVector::screenShot(const char* fileName)
124 {
125         //Do nothing, if no libmagick is there
126 #ifdef PICTURE_DECODER_MAGICK
127         int width,height;
128         getRealScreenSize(width,height);
129         size_t length=width*height*4;
130         void *mem=malloc(length);
131
132         try {
133                 Blob myblob;
134                 if (!screenShot(mem,width,height,true)) {
135                         Log::getInstance()->log("OsdVector", Log::DEBUG, "Screenshot failed!");
136                         free(mem);
137                         return;
138                 }
139                 myblob.updateNoCopy(mem,length,Blob::MallocAllocator);
140                 Image image(myblob,Geometry(width,height),8,"RGBA");
141                 image.write(fileName);
142         }catch( Exception &error_ )
143         {
144                 Log::getInstance()->log("MagickEncoder", Log::DEBUG, "Libmagick: %s",error_.what());
145
146         }
147
148 #endif
149 }
150
151 Surface * OsdVector::createNewSurface()
152 {
153         return new SurfaceVector(this);
154 }
155
156
157 void OsdVector::Blank()
158 {
159         // do nothing? remove this one?
160 }
161
162 int OsdVector::restore()
163 {
164         // First clear the contents of all registered surfaces
165         surfaces_mutex.Lock();
166
167         //Now go through all surfaces and draw them
168         list<SurfaceCommands>::iterator curdraw=scommands.begin();
169         while (curdraw!=scommands.end()) {
170                 (*curdraw).commands.clear();
171                 curdraw++;
172         }
173         //also clear all handles, they are now invalid, no need to release them
174         images_ref.clear();
175         monobitmaps.clear();
176         //jpegs.clear();
177         styles.clear();
178         styles_ref.clear();
179         palettepics.clear();
180
181         tvmedias.clear();
182         loadindex_ref.clear();
183         tvmedias_load.clear();
184         tvmedias_load_inv.clear();
185         tvmedias_loaded.clear();
186
187         surfaces_mutex.Unlock();
188         return 1;
189 }
190
191 void OsdVector::drawSurfaces()
192 {
193         surfaces_mutex.Lock();
194         list<SurfaceCommands*> todraw; //First figure out if a surfaces is below another surface
195         list<SurfaceCommands>::iterator itty1=scommands.begin();
196         while (itty1!=scommands.end()) {
197                 list<SurfaceCommands>::iterator itty2=itty1;
198                 itty2++;
199                 bool hidden=false;
200                 while (itty2!=scommands.end()) {
201                         SurfaceCommands & ref1=*itty1;
202                         SurfaceCommands & ref2=*itty2;
203                         if (ref1.x>=ref2.x && ref1.y>=ref2.y
204                                         && (ref1.x+ref1.w) <= (ref2.x+ref2.w)
205                                         && (ref1.y+ref1.h) <= (ref2.y+ref2.h) ) {
206                                 hidden=true;
207                                 break;
208                         }
209                         itty2++;
210                 }
211                 if (!hidden) { // we are not hidden, perfect
212                         todraw.push_back(&(*itty1));
213                 }
214                 itty1++;
215         }
216         int swidth,sheight;
217         getScreenSize(swidth,sheight);
218         //Now go through all surfaces and draw them
219         list<SurfaceCommands*>::iterator curdraw=todraw.begin();
220         while (curdraw!=todraw.end()) {
221                 drawSetTrans(*(*curdraw));
222                 list<SVGCommand>::iterator commands=(*(*curdraw)).commands.begin();
223                 list<SVGCommand>::iterator end=(*(*curdraw)).commands.end();
224                 while (commands!=end) {
225                         // update any images loaded in the mean time
226                         if ((*commands).instr==DrawImageLoading) {
227                                 LoadIndex loadindex=(*commands).target.loadindex;
228                                 if (tvmedias_loaded.find(loadindex)!=tvmedias_loaded.end()) {
229                                         (*commands).instr=DrawImage;
230                                         (*commands).target.image=tvmedias_loaded[loadindex];;
231                                         incImageRef((*commands).target.image);
232                                         removeLoadIndexRef(loadindex);
233                                 }
234
235                         }
236                         // Now check if the command is on screen!
237                         if  (!(*commands).Outside(0,0,swidth,sheight)) {
238                                 executeDrawCommand(*commands);
239                         }
240                         commands++;
241                 }
242                 curdraw++;
243         }
244
245         surfaces_mutex.Unlock();
246 }
247
248 void OsdVector::updateOrAddSurface(const SurfaceVector *surf,float x,float y,float height,float width,
249                         list<SVGCommand>& commands)
250 {
251         surfaces_mutex.Lock();
252         //First determine it is already in our system
253         list<SurfaceCommands>::iterator itty=scommands.begin();
254         while (itty!=scommands.end()) {
255                 if ((*itty).surf==surf) {
256                         //decrease the references
257                         dereferenceSVGCommand((*itty).commands);
258                         break;
259                 }
260                 itty++;
261         }
262         // if not insert it
263         if (itty==scommands.end()) {
264                 SurfaceCommands new_sc;
265                 new_sc.surf=surf;
266                 new_sc.x=x;
267                 new_sc.y=y;
268                 new_sc.w=width;
269                 new_sc.h=height;
270                 itty=scommands.insert(itty,new_sc);
271         }
272         // update any images loaded in the mean time
273         list<SVGCommand>::iterator ilitty=commands.begin();
274
275         while (ilitty!=commands.end())
276         {
277                 if ((*ilitty).instr==DrawImageLoading) {
278                         LoadIndex loadindex=(*ilitty).target.loadindex;
279                         if (tvmedias_loaded.find(loadindex)!=tvmedias_loaded.end()) {
280
281                                 (*ilitty).instr=DrawImage;
282                                 (*ilitty).target.image=tvmedias_loaded[loadindex];
283                                 incImageRef((*ilitty).target.image);
284                                 removeLoadIndexRef(loadindex);
285                         }
286                 }
287                 ilitty++;
288         }
289
290
291         // then clear and copy
292         (*itty).commands.clear();
293         (*itty).commands=commands;
294         //increase the references
295         referenceSVGCommand((*itty).commands);
296         cleanupOrphanedRefs();
297
298         surfaces_mutex.Unlock();
299 }
300
301 void OsdVector::removeSurface(const SurfaceVector *surf)
302 {
303         surfaces_mutex.Lock();
304         //First determine it is already in our system
305         list<SurfaceCommands>::iterator itty=scommands.begin();
306         while (itty!=scommands.end()) {
307                 if ((*itty).surf==surf) {
308                         dereferenceSVGCommand((*itty).commands);
309                         (*itty).commands.clear();
310                         scommands.erase(itty);
311                         break;
312                 }
313                 itty++;
314         }
315         surfaces_mutex.Unlock();
316
317 }
318
319 void OsdVector::dereferenceSVGCommand(list<SVGCommand>& commands )
320 {
321
322         list<SVGCommand>::iterator sitty = commands.begin();
323         while (sitty != commands.end()) {
324                 removeStyleRef((*sitty).getRef());
325                 ImageIndex ii = (*sitty).getImageIndex();
326                 if (ii) removeImageRef(ii);
327                 LoadIndex li=(*sitty).getLoadIndex();
328                 if (li) removeLoadIndexRef(li);
329                 sitty++;
330         }
331 }
332
333 void OsdVector::referenceSVGCommand(list<SVGCommand>& commands )
334 {
335         list<SVGCommand>::iterator sitty=commands.begin();
336         while (sitty!=commands.end())
337         {
338                 incStyleRef((*sitty).getRef());
339                 ImageIndex ii=(*sitty).getImageIndex();
340                 if (ii) incImageRef(ii);
341                 LoadIndex li=(*sitty).getLoadIndex();
342                 if (li) incLoadIndexRef(li);
343                 sitty++;
344         }
345 }
346
347
348 void OsdVector::incImageRef(ImageIndex index)
349 {
350         if (images_ref.find(index)==images_ref.end()) {
351                 images_ref[index]=1;
352         } else {
353                 images_ref[index]++;
354         }
355 }
356
357 void OsdVector::removeImageRef(const ImageIndex ref)
358 {
359         images_ref[ref]--;
360 }
361
362 int OsdVector::getLoadIndexRef(LoadIndex index)
363 {
364         surfaces_mutex.Lock();
365         if (loadindex_ref.find(index)==loadindex_ref.end()) {
366                 return -1;
367         } else {
368                 return loadindex_ref[index];
369         }
370         surfaces_mutex.Unlock();
371 }
372
373 void OsdVector::incLoadIndexRef(LoadIndex index)
374 {
375         if (loadindex_ref.find(index)==loadindex_ref.end()) {
376                 loadindex_ref[index]=1;
377         } else {
378                 loadindex_ref[index]++;
379         }
380 }
381
382 void OsdVector::removeLoadIndexRef(const LoadIndex ref)
383 {
384         loadindex_ref[ref]--;
385         if (loadindex_ref[ref]==0) {
386                 //now check, if it is already loaded
387                 map<LoadIndex,ImageIndex>::iterator itty=tvmedias_loaded.find(ref);
388                 if ( itty != tvmedias_loaded.end()) {
389                         removeImageRef((*itty).second); // remove lock
390                 }
391                 tvmedias_loaded.erase(ref);
392                 tvmedias_load.erase(tvmedias_load_inv[ref]);
393                 tvmedias_load_inv.erase(ref);
394
395                 reader.invalidateLoadIndex(ref);
396
397
398         }
399 }
400
401
402
403 void OsdVector::cleanupOrphanedRefs()
404 { // Do some garbage collection
405
406         map<void *,ImageIndex>::iterator mitty=monobitmaps.begin();
407         while (mitty!=monobitmaps.end()) {
408                 map<ImageIndex,int>::iterator curitty=images_ref.find((*mitty).second);
409                 int count=(*curitty).second;
410                 if (count==0) {
411                         ImageIndex ref=(*curitty).first;
412                         monobitmaps.erase(mitty++);
413                         images_ref.erase(curitty++);
414                         destroyImageRef(ref);
415                 } else ++mitty;
416         }
417
418         /*map<string,ImageIndex>::iterator jitty=jpegs.begin();
419         while (jitty!=jpegs.end()) {
420                 map<ImageIndex,int>::iterator curitty=images_ref.find((*jitty).second);
421                 int count=(*curitty).second;
422                 if (count==0) {
423                         ImageIndex ref=(*curitty).first;
424                         jpegs.erase(jitty++);
425                         images_ref.erase(curitty++);
426                         destroyImageRef(ref);
427                 } else ++jitty;
428         }*/
429
430         map<TVMediaInfo,ImageIndex>::iterator titty=tvmedias.begin();
431         while (titty!=tvmedias.end()) {
432                 map<ImageIndex,int>::iterator curitty=images_ref.find((*titty).second);
433                 int count=(*curitty).second;
434                 if (count==0) {
435                         ImageIndex ref=(*curitty).first;
436                         tvmedias.erase(titty++);
437                         images_ref.erase(curitty);
438                         destroyImageRef(ref);
439                 } else ++titty;
440         }
441
442
443         map<TVMediaInfo,LoadIndex>::iterator litty=tvmedias_load.begin();
444         while (litty!=tvmedias_load.end()) {
445                 map<LoadIndex,int>::iterator curitty=loadindex_ref.find((*litty).second);
446                 int count=(*curitty).second;
447                 if (count==0) {
448                         tvmedias_load_inv.erase((*litty).second);
449                         tvmedias_loaded.erase((*litty).second);
450                         tvmedias_load.erase(litty++);
451                 } else ++litty;
452         }
453
454         list<ImageIndex>::iterator pitty=palettepics.begin();
455         while (pitty!=palettepics.end()) {
456                 map<ImageIndex,int>::iterator curitty=images_ref.find((*pitty));
457                 int count=(*curitty).second;
458                 if (count==0) {
459                         ImageIndex ref=(*curitty).first;
460                         palettepics.erase(pitty++);
461                         images_ref.erase(curitty++);
462                         destroyImageRef(ref);
463                 } else ++pitty;
464         }
465
466
467         map<pair<Colour*,unsigned int>,unsigned int>::iterator sitty=styles.begin();
468         while (sitty!=styles.end()) {
469                 map<unsigned int,int>::iterator curitty=styles_ref.find((*sitty).second);
470                 int count=(*curitty).second;
471                 if (count==0) {
472                         unsigned int ref=(*curitty).first;
473                         styles.erase(sitty++);
474                         styles_ref.erase(curitty++);
475                         destroyStyleRef(ref);
476
477                 } else ++sitty;
478
479         }
480 }
481
482
483 int OsdVector::getImageRef(ImageIndex index)
484 {
485         surfaces_mutex.Lock();
486         if (images_ref.find(index)==images_ref.end()) {
487                 return -1;
488         } else {
489                 return images_ref[index];
490         }
491         surfaces_mutex.Unlock();
492 }
493
494 void OsdVector::incStyleRef(unsigned int index)
495 {
496         if (styles_ref.find(index)==styles_ref.end()) {
497                 styles_ref[index]=1;
498         } else {
499                 styles_ref[index]++;
500         }
501 }
502
503 void OsdVector::removeStyleRef(unsigned int index)
504 {
505         styles_ref[index]--;
506 }
507
508 unsigned int OsdVector::getStyleRef(const DrawStyle &c)
509 {
510         surfaces_mutex.Lock();
511         unsigned int style_handle=0;
512         if (styles.find(pair<Colour*,unsigned int>((Colour*)&c,c.rgba()))==styles.end())
513         {
514                 surfaces_mutex.Unlock();
515                 style_handle=createStyleRef(c);
516                 surfaces_mutex.Lock();
517                 styles[pair<Colour*,unsigned int>((Colour*)&c,c.rgba())]=style_handle;
518         } else {
519                 style_handle=styles[pair<Colour*,unsigned int>((Colour*)&c,c.rgba())];
520                 //Now check if the handle is valid
521                 if (styles_ref.find(style_handle)==styles_ref.end()) {
522                         //invalid handle recreate
523                         surfaces_mutex.Unlock();
524                         style_handle=createStyleRef(c);
525                         surfaces_mutex.Lock();
526                         styles[pair<Colour*,unsigned int>((Colour*)&c,c.rgba())]=style_handle;
527                 }
528         }
529         incStyleRef(style_handle);
530         surfaces_mutex.Unlock();
531         return style_handle;
532 }
533
534 unsigned int OsdVector::getColorRef(const Colour &c)
535 {
536         surfaces_mutex.Lock();
537         unsigned int style_handle=0;
538         if (styles.find(pair<Colour*,unsigned int>((Colour*)&c,c.rgba()))==styles.end())
539         {
540                 surfaces_mutex.Unlock();
541                 style_handle=createColorRef(c);
542                 surfaces_mutex.Lock();
543                 styles[pair<Colour*,unsigned int>((Colour*)&c,c.rgba())]=style_handle;
544         } else {
545                 style_handle=styles[pair<Colour*,unsigned int>((Colour*)&c,c.rgba())];
546                 if (styles_ref.find(style_handle)==styles_ref.end()) {
547                         //invalid handle recreate
548                         surfaces_mutex.Unlock();
549                         style_handle=createColorRef(c);
550                         surfaces_mutex.Lock();
551                         styles[pair<Colour*,unsigned int>((Colour*)&c,c.rgba())]=style_handle;
552                 }
553         }
554         incStyleRef(style_handle);
555         surfaces_mutex.Unlock();
556         return style_handle;
557 }
558
559 int OsdVector::getStyleRef(unsigned int index)
560 {
561         if (styles_ref.find(index)==styles_ref.end()) {
562                 return -1;
563         } else {
564                 return styles_ref[index];
565         }
566 }
567
568 LoadIndex OsdVector::getTVMediaRef(TVMediaInfo& tvmedia, ImageIndex& image)
569 {
570         ImageIndex image_handle=0;
571         LoadIndex loadindex=0;
572         surfaces_mutex.Lock();
573         if (tvmedias.find(tvmedia)==tvmedias.end())
574         {
575                 loadindex=loadTVMedia(tvmedia);
576         } else {
577                 image_handle=tvmedias[tvmedia];
578                 if (images_ref.find(image_handle)==images_ref.end()) {
579                         //invalid handle recreate
580                         loadindex=loadTVMedia(tvmedia);
581                         image_handle=0;
582                 } else {
583                         incImageRef(image_handle);
584                 }
585         }
586         /*tvmedias[tvmedia]=createTVMedia(tvmedia,width,height);
587         incImageRef(image_handle);*/
588         image=image_handle;
589         surfaces_mutex.Unlock();
590         return loadindex;
591 }
592
593 LoadIndex OsdVector::loadTVMedia(TVMediaInfo& tvmedia)
594 {
595         LoadIndex index=0;
596         if (tvmedias_load.find(tvmedia)==tvmedias_load.end())
597         {
598                 switch (tvmedia.getType()) {
599                 case 3:
600                         index=VDR::getInstance()->loadTVMediaRecThumb(tvmedia);
601                         break;
602                 case 4: {
603                         index = ((long long) tvmedia.getPrimaryID()) << 32LL;
604                         reader.addStaticImage(tvmedia.getPrimaryID());
605                 } break;
606                 case 5:
607                         index=VDR::getInstance()->loadTVMediaEventThumb(tvmedia);
608                 break;
609                 case 6:
610                         index=VDR::getInstance()->loadChannelLogo(tvmedia);
611                 break;
612                 default:
613                         index=VDR::getInstance()->loadTVMedia(tvmedia);
614                         break;
615                 }
616
617                 tvmedias_load[tvmedia]=index;
618                 tvmedias_load_inv[index]=tvmedia;
619         } else {
620
621                 index=tvmedias_load[tvmedia];
622         }
623
624         incLoadIndexRef(index);
625
626         return index;
627 }
628
629
630
631 void OsdVector::informPicture(LoadIndex index, ImageIndex imageIndex)
632 {
633         //Beware for thread safety
634         ImageIndex image_index=0;
635
636         Log::getInstance()->log("OsdVector", Log::DEBUG, "TVMedia Picture for request id %llx arrived %d",index, imageIndex);
637         surfaces_mutex.Lock();
638         TVMediaInfo tvmedia=tvmedias_load_inv[index];
639         if (imageIndex) {
640                 image_index=tvmedias[tvmedia]=imageIndex;
641                 tvmedias_loaded[index]=image_index;
642                 if (loadindex_ref.find(index)==loadindex_ref.end()) {
643                         // we do not want the picture anymore . Really...
644                         // fill images_ref in to not irritate the garbage collector
645                         if (images_ref.find(index)==images_ref.end()) {
646                                 images_ref[image_index]=0;
647                         }
648                 } else {
649
650                         incImageRef(image_index); // hold one index until all loadings refs are gone;
651                 }
652         }
653         surfaces_mutex.Unlock();
654
655
656 }
657
658
659
660
661 /*
662 ImageIndex OsdVector::getJpegRef(const char* fileName, int *width,int *height)
663 {
664         ImageIndex image_handle=0;
665         if (jpegs.find(fileName)==jpegs.end())
666         {
667                 image_handle=jpegs[fileName]=createJpeg(fileName,width,height);
668         } else {
669                 image_handle=jpegs[fileName];
670                 *width=0;
671                 *height=0;
672                 if (images_ref.find(image_handle)==images_ref.end()) {
673                         //invalid handle recreate
674                         image_handle=jpegs[fileName]=createJpeg(fileName,width,height);
675                 }
676         }
677         incImageRef(image_handle);
678         return image_handle;
679 }
680 */
681
682 ImageIndex OsdVector::getMonoBitmapRef(void *base,int width,int height)
683 {
684         ImageIndex image_handle;
685         surfaces_mutex.Lock();
686         if (monobitmaps.find(base)==monobitmaps.end())
687         {
688                 surfaces_mutex.Unlock();
689                 image_handle=createMonoBitmap(base,width,height);
690                 surfaces_mutex.Lock();
691                 monobitmaps[base]=image_handle;
692         } else {
693                 image_handle=monobitmaps[base];
694                 if (images_ref.find(image_handle)==images_ref.end()) {
695                         //invalid handle recreate
696                         surfaces_mutex.Unlock();
697                         image_handle=createMonoBitmap(base,width,height);
698                         surfaces_mutex.Lock();
699                         monobitmaps[base]=image_handle;
700                 }
701         }
702         incImageRef(image_handle);
703         surfaces_mutex.Unlock();
704         return image_handle;
705 }
706
707 ImageIndex  OsdVector::getImagePalette(int width,int height,const unsigned char *image_data,const unsigned int*palette_data)
708 {
709         ImageIndex image_handle;
710         image_handle=createImagePalette(width,height,image_data,palette_data);
711         surfaces_mutex.Lock();
712         palettepics.push_back(image_handle);
713         incImageRef(image_handle);
714         surfaces_mutex.Unlock();
715         return image_handle;
716 }
717
718 OsdVector::PictureReader::~PictureReader()
719 {
720         decoders_lock.Lock();
721         while ( decoders.size()) {
722                 PictureDecoder* dec=decoders.front();
723                 decoders.pop_front();
724                 delete dec;
725         }
726
727         decoders_lock.Unlock();
728 }
729
730 void OsdVector::PictureReader::init()
731 {
732         threadStart();
733 }
734
735 void OsdVector::PictureReader::shutdown()
736 {
737         threadStop();
738
739
740 }
741
742 void OsdVector::PictureReader::addDecoder(PictureDecoder* decoder)
743 {
744         decoders_lock.Lock();
745         decoder->init();
746         decoders.push_front(decoder);
747         decoders_lock.Unlock();
748 }
749
750 void OsdVector::PictureReader::removeDecoder(PictureDecoder* decoder)
751 {
752         decoders_lock.Lock();
753         std::list<PictureDecoder*>::iterator itty=decoders.begin();
754         while (itty!=decoders.end()) {
755                 if ((*itty) == decoder)
756                 {
757                         decoders.erase(itty);
758                         break;
759                 }
760                 itty++;
761         }
762         Log::getInstance()->log("OsdVector", Log::DEBUG, "removeDecoder");
763         decoder->shutdown();
764         delete decoder;
765         decoders_lock.Unlock();
766 }
767
768 void OsdVector::PictureReader::threadMethod()
769 {
770         OsdVector *osdvector = dynamic_cast<OsdVector*>(Osd::getInstance());
771         Log::getInstance()->log("OsdVector", Log::DEBUG, "TVMedia Start Picture Reader");
772         while (true) {
773                 if (!threadIsActive()) {
774                         Log::getInstance()->log("OsdVector", Log::DEBUG, "TVMedia End Picture Reader");
775                         threadCheckExit();
776                 }
777
778                 bool todos=true;
779                 while (todos)
780                 {
781                         todos=false;
782                         PictureInfo pictinf;
783                         decoders_lock.Lock();
784                         std::list<PictureDecoder*>::iterator itty=decoders.begin();
785
786                         while (itty!=decoders.end()) {
787                                 if ((*itty)->getDecodedPicture(pictinf)) {
788                                         todos = true;
789                                         osdvector->createPicture(pictinf);
790                                 }
791
792                                 itty++;
793                         }
794                         if (processReceivedPictures())
795                         {
796                                 todos = true;
797                         }
798
799                         decoders_lock.Unlock();
800                 }
801                 //Log::getInstance()->log("OsdVector", Log::DEBUG, "TVMedia Sleep Picture Reader");
802
803                 struct timespec target_time;
804                 clock_gettime(CLOCK_REALTIME,&target_time);
805                 target_time.tv_nsec+=1000000LL*10;
806                 if (target_time.tv_nsec>999999999) {
807                         target_time.tv_nsec-=1000000000L;
808                         target_time.tv_sec+=1;
809                 }
810                 threadLock();
811                 threadWaitForSignalTimed(&target_time);
812                 threadUnlock();
813                 //Log::getInstance()->log("OsdVector", Log::DEBUG, "TVMedia Sleep end Picture Reader");
814         }
815 }
816
817 void OsdVector::PictureReader::threadPostStopCleanup()
818 {
819
820 }
821
822 void OsdVector::PictureReader::invalidateLoadIndex(LoadIndex index)
823 {
824         pict_lock_incoming.Lock();
825         invalid_loadindex.insert(index);
826         pict_lock_incoming.Unlock();
827 }
828
829 void OsdVector::PictureReader::receivePicture(VDR_ResponsePacket *vresp)
830 {
831         pict_lock_incoming.Lock();
832         pict_incoming.push(vresp);
833         pict_lock_incoming.Unlock();
834         threadSignal();
835 }
836
837
838 void OsdVector::PictureReader::addStaticImage(unsigned int id)
839 {
840         pict_lock_incoming.Lock();
841         pict_incoming_static.push(id);
842         invalid_loadindex.erase(((long long) id) << 32LL);
843         pict_lock_incoming.Unlock();
844         threadSignal();
845 }
846
847 bool OsdVector::PictureReader::processReceivedPictures()
848 {
849         bool decoded = false;
850         bool valid = true;
851         pict_lock_incoming.Lock();
852     if (pict_incoming.size()) {
853                 VDR_ResponsePacket *vresp=pict_incoming.front();
854                 pict_incoming.pop();
855                 set<LoadIndex>::iterator setpos = invalid_loadindex.find(vresp->getStreamID());
856                 if (setpos != invalid_loadindex.end()) {
857                         valid = false;
858                         invalid_loadindex.erase(setpos);
859                 }
860                 pict_lock_incoming.Unlock();
861                 if (!valid) { // we do not want it anymore skip it;
862                         delete vresp;
863                         return true;
864                 }
865         //       Log::getInstance()->log("OsdVector", Log::DEBUG, "TVMedia Pictures arrived VDR %x %d %d",
866         //                       vresp->getStreamID(),vresp->getUserDataLength(),vresp->getFlag());
867                 if (vresp->getFlag() != 2) {
868                         UCHAR *userdata=vresp->getUserData();
869                         ULONG length=vresp->getUserDataLength();
870                         std::list<PictureDecoder*>::iterator itty=decoders.begin();
871                         while (itty!=decoders.end()) {
872                                 userdata=(*itty)->decodePicture(vresp->getStreamID(), userdata, length);
873                                 if (!userdata){
874                                         decoded=true;
875                                         break;
876                                 }
877                                 itty++;
878                         }
879                         if (!decoded && userdata ){
880                                 free(userdata);
881                         }
882                 }
883                 //else  osd->informPicture(vresp->getStreamID(), 0);
884                 delete vresp;
885         } else if (pict_incoming_static.size()){
886                 unsigned int static_id = pict_incoming_static.front();
887                 pict_incoming_static.pop();
888                 set<LoadIndex>::iterator setpos = invalid_loadindex.find(((long long) static_id) << 32LL);
889                 if (setpos != invalid_loadindex.end()) {
890                         valid = false;
891                         invalid_loadindex.erase(setpos);
892                 }
893                 pict_lock_incoming.Unlock();
894                 if (!valid) { // we do not want it anymore skip it;
895                         return true;
896                 }
897
898                 UCHAR *userdata;
899                 ULONG length;
900                 if (((OsdVector*)Osd::getInstance())->getStaticImageData(static_id, &userdata, &length))
901                 {
902                         std::list<PictureDecoder*>::iterator itty=decoders.begin();
903                         while (itty!=decoders.end()) {
904                                 if (!(*itty)->decodePicture(((long long) static_id) << 32LL,userdata, length, false)){
905                                         decoded=true;
906                                         break;
907                                 }
908                                 itty++;
909                         }
910                 }
911         } else {
912                 pict_lock_incoming.Unlock();
913         }
914
915
916     if (pict_incoming.size() || pict_incoming_static.size()) return true;
917         return decoded;
918 }