summaryrefslogtreecommitdiff
path: root/player-image.c
blob: fa90174e3ebe49c39d3b9e2191102046d5a86b1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
 * Image plugin to VDR (C++)
 *
 * (C) 2004-2011 Andreas Brachold    <vdr07 at deltab.de>
 * based on (C) 2003 Kai Tobias Burwieck <kai-at-burwieck.net>
 *
 * This code is distributed under the terms and conditions of the
 * GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
 *
 */

#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <signal.h>
#include <wait.h>

#include "player-image.h"
#include "control-image.h"
#include "setup-image.h"
#include "data-image.h"
#include "image.h"
#include "list.h"
#include <vdr/i18n.h>

#include "libimage/pnm.h"
#include "libimage/xpm.h"


const char *g_szConvertScript = "imageplugin.sh";


//----------cImagePlayer-------------




cImagePlayer::cImagePlayer(cSlideShow *pCurSlideShow)
: cStillImagePlayer(
 ((ImageSetup.m_bLiveAudio != 0)?pmAudioVideo:pmVideoOnly)
,((ImageSetup.m_bUseDeviceStillPicture != 0)?true:false)
)
, m_bConvertRunning(false)
, m_szError(NULL)
{
  theSlideShow.Assign(pCurSlideShow);
}

bool cImagePlayer::GetIndex(int &nCurrent, int &nTotal, bool /*bSnapToIFrame*/)
{
  // Notify other status-plugins with one picture as one second
  nCurrent = theSlideShow.ImageCurrent(); 
  // Notify other status-plugins count of picture as totalcount of seconds
  nTotal = theSlideShow.ImageTotal();

  return true;
}

cImagePlayer::~cImagePlayer()
{
  // Remove Slideshow
  theSlideShow.Shutdown();
}


void cImagePlayer::Activate(bool bOn)
{
  if(bOn) {
    if(theSlideShow.GetImage())
    {
      cStillImagePlayer::Activate(bOn);
    }
  }
  else
    cStillImagePlayer::Activate(false);
}


bool cImagePlayer::NextImage(int nOffset)
{
  return theSlideShow.NextImage(nOffset);
}



bool cImagePlayer::PrevImage(int nOffset)
{
  return theSlideShow.PrevImage(nOffset);
}


bool cImagePlayer::GotoImage(unsigned int nNewPictureIndex)
{
  return theSlideShow.GotoImage(nNewPictureIndex);
}


bool cImagePlayer::Convert(const char *szChange)
{
  cImageData* pImage = theSlideShow.GetImage();
  if(pImage)
  {
    cShellWrapper* pCmd = new cShellWrapper;
    pCmd->bClearBackground = true;  
    pCmd->nOffLeft = m_StillImage.GetBorderWidth();
    pCmd->nOffTop = m_StillImage.GetBorderHeight();
    pCmd->nWidth = UseWidth();
    pCmd->nHeight = UseHeight();
  
    // Build image_convert.sh "source.jpg" "/tmp/image/dest.pnm" 720 576 0 0 0 0 original
    pCmd->szPNM = strdup(pImage->NamePNM());
      
    asprintf(&pCmd->szCmd, "%s \"%s\" \"%s\" %d %d %d %d %d %s", 
        g_szConvertScript,
        pImage->Name(), 
        pCmd->szPNM,
        pCmd->nWidth,
        pCmd->nHeight,
        0,
        0,
        0,
        szChange ? szChange : "");
  
    Exec(pCmd);
    return true;
  }
  return false;
}



bool cImagePlayer::ConvertJump(int nOffset)
{
  register unsigned int w,h;
  const unsigned int MAX_BILDER = 9;
  cImageData* pImage[MAX_BILDER];
  for (w = 0; w < MAX_BILDER; ++w)
    pImage[w] = NULL;
  int nBilder = theSlideShow.GetJumpNames(nOffset,pImage,MAX_BILDER);
  if(nBilder > 0 
    && pImage[0]) {
      
    unsigned int nMatrix = (nBilder < 5) ? 2 : 3;
  
    for (h = 0; h < nMatrix; ++h) 
      for (w = 0; w < nMatrix && pImage[(h*nMatrix)+w]; ++w) 
      {
        cShellWrapper* pCmd = new cShellWrapper;
      
        pCmd->bClearBackground = (w == 0 && h == 0);  
        pCmd->nWidth = UseWidth();
        pCmd->nHeight = UseHeight();
        pCmd->nWidth /= nMatrix;
        pCmd->nHeight /= nMatrix;
        pCmd->nOffLeft = (pCmd->nWidth * w) +  m_StillImage.GetBorderWidth();
        pCmd->nOffTop =  (pCmd->nHeight * h) + m_StillImage.GetBorderHeight();
      
        // Build image_convert.sh "source.jpg" "/tmp/image/source.jpg-i.pnm" 256 192 0 0 0 0 original
        pCmd->szPNM = strdup(pImage[(h*nMatrix)+w]->NameIndex());

      
    
        asprintf(&pCmd->szCmd, "%s \"%s\" \"%s\" %d %d %d %d %d %s", 
            g_szConvertScript,
            pImage[(h*nMatrix)+w]->Name(), 
            pCmd->szPNM,
            pCmd->nWidth,
            pCmd->nHeight,
            0,
            0,
            0,
            /*szChange ? szChange : */"");
        pCmd->szNumber = '0'+((h*nMatrix)+w)+1;

        Exec(pCmd);
      }
      return true;
  }
  return false;
}


bool cImagePlayer::ConvertZoom(const char *szChange, int nZoomFaktor,
			       int nLeftPos, int nTopPos)
{
  cImageData* pImage = theSlideShow.GetImage();
  if(pImage)
  {
    cShellWrapper* pCmd = new cShellWrapper;
    pCmd->bClearBackground = true;  
    pCmd->nOffLeft = m_StillImage.GetBorderWidth() + (nLeftPos>0?0:(nLeftPos*-1));
    pCmd->nOffTop = m_StillImage.GetBorderHeight() + (nTopPos>0?0:(nTopPos*-1));
    pCmd->nWidth = UseWidth();
    pCmd->nHeight = UseHeight();
  
    // Build image_convert.sh "source.jpg" "/tmp/image/dest.pnm" 720 576 0 0 0 0 original
    pCmd->szPNM = strdup(pImage->NameZoom());
      
    asprintf(&pCmd->szCmd, "%s \"%s\" \"%s\" %d %d %d %d %d %s", 
        g_szConvertScript,
        pImage->Name(), 
        pCmd->szPNM,
        pCmd->nWidth,
        pCmd->nHeight,
        nZoomFaktor,
        nLeftPos>0?nLeftPos:0,
        nTopPos>0?nTopPos:0,
        szChange ? szChange : "");
  
    Exec(pCmd);
    return true;
  }
  return false;
}


void cImagePlayer::LoadImage(cShellWrapper* pShell)
{
    cPNM pnmImage;
    bool bSuccess = false;
    register unsigned int nHeight = m_StillImage.GetHeight();
    register unsigned int nWidth = m_StillImage.GetWidth();
    register unsigned int nOffLeft = 0;
    register unsigned int nOffTop = 0;
    errno = 0;
    if(!pShell || pShell->bClearBackground)
      m_StillImage.ClearRGBMem();

    if(pShell && pShell->szPNM)
    {  
      nHeight = std::min(nHeight,pShell->nHeight);
      nWidth = std::min(nWidth,pShell->nWidth);
      nOffLeft = pShell->nOffLeft;
      nOffTop = pShell->nOffTop;
  
      FILE *f=fopen(pShell->szPNM, "r");
      if(f)
      {
        xel* pRow = NULL;
        register unsigned int w;
        register unsigned int h;
      
        if(pnmImage.readHeader(f))
        {
          register unsigned int nColorDepth = pnmImage.GetColorDepth();
        
          if(pnmImage.GetWidth() < nWidth)
            nOffLeft +=  (nWidth - pnmImage.GetWidth()) / 2;
    
          if(pnmImage.GetHeight() < nHeight)
            nOffTop +=  (nHeight - pnmImage.GetHeight()) / 2;
  
  
          for(h = 0;
              h < pnmImage.GetHeight() 
              && h < nHeight 
              && h+nOffTop < m_StillImage.GetHeight();
              ++h)
            {  
              if(!pnmImage.allocrow(&pRow) 
                ||!pnmImage.readrow(f, pRow) )
               break;
  
              xel* pP = pRow;
              for(w = 0;w < pnmImage.GetWidth() 
                 && w < nWidth
                 && w+nOffLeft < m_StillImage.GetWidth();
                 ++w,++pP)
              {  
                uint8_t* pImageRGB = m_StillImage.GetRGBMem() + 
                          ((((h+nOffTop)*m_StillImage.GetWidth())+w+nOffLeft)*3);
            
                if(nColorDepth == 0xFF) // normal 8-bit at any canal (24 Bit) colordepth
                {  
                  *(pImageRGB + 0) = (uint8_t) PPM_GETR(*pP);
                  *(pImageRGB + 1) = (uint8_t) PPM_GETG(*pP);
                  *(pImageRGB + 2) = (uint8_t) PPM_GETB(*pP);
                }
                else if(nColorDepth == 1) // black/white image
                {  
                  *(pImageRGB + 0) = (uint8_t) PPM_GETR(*pP)==0?0x00:0xFF;
                  *(pImageRGB + 1) = (uint8_t) PPM_GETG(*pP)==0?0x00:0xFF;
                  *(pImageRGB + 2) = (uint8_t) PPM_GETB(*pP)==0?0x00:0xFF;
                }
                else // Adjust other colordepth to 8bit
                {  
                  *(pImageRGB + 0) = (uint8_t) (PPM_GETR(*pP)*255 / pnmImage.GetColorDepth()) & 0xFF;
                  *(pImageRGB + 1) = (uint8_t) (PPM_GETG(*pP)*255 / pnmImage.GetColorDepth()) & 0xFF;
                  *(pImageRGB + 2) = (uint8_t) (PPM_GETB(*pP)*255 / pnmImage.GetColorDepth()) & 0xFF;
                }
              }
              pnmImage.freerow(pRow);
              pRow = NULL;
            }
          if(pRow)
            pnmImage.freerow((char*)pRow);
          else
          {
            if(pShell->szNumber && ImageSetup.m_bShowNumbers)
              cXPM::Overlay(pShell->szNumber,m_StillImage.GetRGBMem(),
                  m_StillImage.GetWidth(),m_StillImage.GetHeight(),
                  cXPM::TopRight,nOffLeft,nOffTop,pnmImage.GetWidth(),pnmImage.GetHeight());
  
            bSuccess = true;
          }
        }
        fclose(f);
        if (ImageSetup.m_bRemoveImmediately) {
            cImageData::Unlink (pShell->szPNM);
        }
      }
    }
  if(!bSuccess) {
  
    // Merge Errorimage with Encoder-Memory
    if(pShell && pShell->szNumber)
      cXPM::Overlay('s',m_StillImage.GetRGBMem(),
          m_StillImage.GetWidth(),m_StillImage.GetHeight(),
          cXPM::Center,pShell->nOffLeft,pShell->nOffTop,pShell->nWidth,pShell->nHeight);
    else
      cXPM::Error(m_StillImage.GetRGBMem(),
        m_StillImage.GetWidth(),m_StillImage.GetHeight());
    
    // Build Error, depends PNM Errormsg or system messages
    char szErr[128];
    szErr[0] = '\0';

    if(pnmImage.GetError())
      strncpy(szErr,pnmImage.GetError(),sizeof(szErr));
    else if(errno) {  
        int nErr = errno;
        szErr[sizeof(szErr)-1] = '\0';
        if(0 != strerror_r(nErr,szErr,sizeof(szErr)-1)) {
            szErr[0] = '\0';
        } 
    }

    // Make Syslog entry
    if(pShell && pShell->szPNM && szErr[0] != '\0') 
      esyslog("imageplugin: Error until read %s : '%s'", pShell->szPNM, szErr);
    else if(pShell && pShell->szPNM) 
      esyslog("imageplugin: Error until read %s", pShell->szPNM);
    else  
      esyslog("imageplugin: Error until read image %s",(szErr[0] != '\0')?szErr:"");
    
    { // Copy Errormessage forward to OSD Thread
      cMutexLock lock(&m_MutexErr);
        if(m_szError)
          free(m_szError);
        if(szErr[0] != '\0')
          asprintf(&m_szError, "%s : %s", tr("Image couldn't load"),szErr);
        else
          m_szError = strdup(tr("Image couldn't load"));
    }
  }
}

////////////////////////////////////////////////////////////////////////////////
/** void cImagePlayer::ExecFailed()
Show a precompiled Image if Exec can't find a converted Image
@param  - char* szErr
@return - nothing */
void cImagePlayer::ExecFailed(cShellWrapper* pShell,const char* szErr)
{
    if(!pShell || pShell->bClearBackground)
      m_StillImage.ClearRGBMem();
    
    // Merge Errorimage with Encoder-Memory
    if(pShell && pShell->szNumber)
      cXPM::Overlay('s',m_StillImage.GetRGBMem(),
          m_StillImage.GetWidth(),m_StillImage.GetHeight(),
          cXPM::Center,pShell->nOffLeft,pShell->nOffTop,pShell->nWidth,pShell->nHeight);
    else
      cXPM::Error(m_StillImage.GetRGBMem(),
        m_StillImage.GetWidth(),m_StillImage.GetHeight());


  // Store Message for OSD-Thread
  {
    cMutexLock lock(&m_MutexErr);
      if(m_szError)
        free(m_szError);
      m_szError = strdup(szErr);
  }
}

////////////////////////////////////////////////////////////////////////////////
/** void cImagePlayer::ErrorMsg()
ThreadSafe Method to show messages from Worker thread,
this functions is called only from cImageControl::ProcessKey(kNone)
@return - nothing */
void cImagePlayer::ErrorMsg()
{
  char* szErr = NULL;
  {
    cMutexLock lock(&m_MutexErr);
    szErr = m_szError;
    m_szError = NULL;
  }
  if(szErr)
  {  
    OSD_ErrorMsg(szErr);
    free(szErr);
  }
}

const char* cImagePlayer::FileName(void) const
{ 
  cImageData* pImage = theSlideShow.GetImage();
  return pImage?pImage->Name():NULL;
}

void cImagePlayer::Exec(cShellWrapper* pCmd)
{
  if(pCmd->szPNM)
    m_bConvertRunning = true;
  if(pCmd) {
    cMutexLock lock(&m_Mutex);
    if(!m_queue.add(pCmd))
      delete pCmd;          //Queue full or Thread is dead     
  }
}

bool cImagePlayer::Worker(bool bDoIt)
{
  bool bQueueEmpty;
  cShellWrapper *pShell = NULL;
  
  { // Protect the queue ++
    cMutexLock lock(&m_Mutex);
    if(bDoIt && !m_queue.empty()) {
      pShell = *m_queue.begin();
      m_queue.erase(m_queue.begin());
    }
    bQueueEmpty = m_queue.empty();
  } // ++

  if(!pShell) 
  {
    m_bConvertRunning = m_StillImage.EncodeRequired();
    return bQueueEmpty;
  }  

  if(pShell->szCmd) {
    //dsyslog("imageplugin: executing script '%s'", pShell->szCmd);
    
    ImageSetup.SetEnv();
    if(0 == SystemExec(pShell->szCmd))
    {  
      if(pShell->szPNM) {
          LoadImage(pShell);
          m_StillImage.EncodeRequired(true);
      }
    }    
    else
    {
      esyslog("imageplugin: script execution failed '%s'", pShell->szCmd);
      if(pShell->szPNM) {
        ExecFailed(pShell,tr("Script execution failed"));        
        m_StillImage.EncodeRequired(true);
      }
    }   
  } 
  delete pShell;
  return bQueueEmpty;
}