summaryrefslogtreecommitdiff
path: root/dxr3device.c
blob: e68ef4a23ed7021edf84ccd2870425952dbf216f (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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/*
 *           _                 _             _                 _          _____
 * __   ____| |_ __      _ __ | |_   _  __ _(_)_ __         __| |_  ___ _|___ /
 * \ \ / / _` | '__|____| '_ \| | | | |/ _` | | '_ \ _____ / _` \ \/ / '__||_ \
 *  \ V / (_| | | |_____| |_) | | |_| | (_| | | | | |_____| (_| |>  <| |  ___) |
 *   \_/ \__,_|_|       | .__/|_|\__,_|\__, |_|_| |_|      \__,_/_/\_\_| |____/
 *                      |_|            |___/
 *
 * Copyright (C) 2002-2004 Kai Möller
 * Copyright (C) 2004-2010 Christian Gmeiner
 *
 * This file is part of vdr-plugin-dxr3.
 *
 * vdr-plugin-dxr3 is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation version 2.
 *
 * vdr-plugin-dxr3 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with dxr3-plugin.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <sys/ioctl.h>

#include "dxr3device.h"
#include "dxr3tools.h"
#include "dxr3osd.h"
#include "dxr3audio-alsa.h"
#include "dxr3audio-pa.h"
#include "dxr3pesframe.h"
#include "settings.h"

static const char *DEV_DXR3_OSD   = "_sp";
static const char *DEV_DXR3_VIDEO = "_mv";
static const char *DEV_DXR3_CONT  = "";

static const int TIMESTAMPS_PREBUFFER = 4500;    // 50ms

cDxr3Device::cDxr3Device() : spuDecoder(NULL), pluginOn(true), vPts(0), scrSet(false), aspectRatio(EM8300_ASPECTRATIO_4_3)
{
    claimDevices();

    switch (cSettings::instance()->audioDriver()) {
    case ALSA:
        isyslog("[dxr3-device] using alsa audio driver");
        audio = new cAudioAlsa();
        break;
#ifdef PULSEAUDIO
    case PA:
        isyslog("[dxr3-device] using PulseAudio audio driver");
        audio = new cAudioPA();
        break;
#endif
    }

    audio->openDevice();
    decoder = new cDecoder();

    // register observer
    cSettings::instance()->registerObserver(this);
}

cDxr3Device::~cDxr3Device()
{
    audio->releaseDevice();
    delete audio;
    delete decoder;

    releaseDevices();

    if (spuDecoder)
        delete spuDecoder;
}

cDxr3Device *cDxr3Device::instance()
{
    if (!inst) {
        inst = new cDxr3Device();
    }

    return inst;
}

// init static inst with NULL
cDxr3Device *cDxr3Device::inst = NULL;

int cDxr3Device::Dxr3Open(const char *name, int mode, bool report_error)
{
    const char *filename = *cDxr3Name(name, cSettings::instance()->card());
    int fd = open(filename, mode);

    if (report_error && (fd < 0)) {
        LOG_ERROR_STR(filename);
    }
    return fd;
}

void cDxr3Device::MakePrimaryDevice(bool On)
{
#if VDRVERSNUM >= 10711
    cDevice::MakePrimaryDevice(On);
#endif

    if (On) {
        new cDxr3OsdProvider();
    }
}

bool cDxr3Device::HasDecoder() const
{
    return true;
}

bool cDxr3Device::CanReplay() const
{
    return true;
}

bool cDxr3Device::SetPlayMode(ePlayMode PlayMode)
{
    cMutexLock l(&mutex);

    dsyslog("[dxr3-device] setting playmode %d", PlayMode);
    uint32_t val;

    switch (PlayMode) {
    case pmNone:
        audio->setEnabled(false);
        audio->flush();
        scrSet = false;

        // reset pts values
        vPts = 0;
        aPts = 0;

        // here we use some magic
        // set the scr into future so that the firmware/hardware
        // clears the buffers.
        CHECK(ioctl(fdControl, EM8300_IOCTL_SCR_GET, &val));
        val += TIMESTAMPS_PREBUFFER * 4;
        CHECK(ioctl(fdControl, EM8300_IOCTL_SCR_SET, &val));

        playBlackFrame(val + TIMESTAMPS_PREBUFFER);
        break;

    case pmAudioVideo:
        audio->setEnabled(true);
        break;

    default:
        dsyslog("[dxr3-device] playmode not supported");
        return false;
    }

    return true;
}

int64_t cDxr3Device::GetSTC()
{
    return (vPts << 1);
}

void cDxr3Device::TrickSpeed(int Speed)
{
    dsyslog("dxr3: device: tricspeed: %d", Speed);
}

void cDxr3Device::Clear()
{
    dsyslog("[dxr3-device] clear");
    uint32_t val = 0;

    // here we use some magic
    // set the scr into future so that the firmware/hardware
    // clears the buffers.
    CHECK(ioctl(fdControl, EM8300_IOCTL_SCR_GET, &val));
    val += TIMESTAMPS_PREBUFFER * 4;
    CHECK(ioctl(fdControl, EM8300_IOCTL_SCR_SET, &val));

    audio->flush();
    cDevice::Clear();
}

void cDxr3Device::Play()
{
    dsyslog("[dxr3-device] play");
}

void cDxr3Device::Freeze()
{
    dsyslog("[dxr3-device] freeze");
}

void cDxr3Device::Mute()
{
    audio->mute();
    cDevice::Mute();
}

void cDxr3Device::StillPicture(const uchar *Data, int Length)
{
    dsyslog("[dxr3-device] stillpciture");
    cDxr3PesFrame frame;

    // we need to check if Data points to a pes
    // frame or to non-pes data. This could be the
    // case for the radio-Plugin, which points to an
    // elementary stream.
    if (!Data || Length < TS_SIZE)
       return;

    if (Data[0] == 0x47) {
       // TS data
       cDevice::StillPicture(Data, Length);
    } else if (frame.parse(Data, Length)) {
        // PES data
    } else {
        // unknown data
        dsyslog("[dxr3-device] StillPicture: TODO %08x %08x %08x", Data[0], Data[1], Data[2]);
    }
}

bool cDxr3Device::Poll(cPoller &Poller, int TimeoutMs)
{
    Poller.Add(fdVideo, true);
    audio->poll(Poller);
    return Poller.Poll(TimeoutMs);
}

int cDxr3Device::PlayVideo(const uchar *Data, int Length)
{
    cMutexLock l(&mutex);

    cDxr3PesFrame frame;
    if (!frame.parse(Data, Length)) {
    	// if parsing failed, ignore frame
    	return Length;
    }

    uint32_t pts = frame.pts();

    if (pts == 0) {
        pts = vPts;
    } else {
        vPts = pts;
    }

    if (scrSet) {
        playVideoFrame(&frame, vPts);
    }

    return Length;
}

int cDxr3Device::PlayAudio(const uchar *Data, int Length, uchar Id)
{
    cMutexLock l(&mutex);

    cDxr3PesFrame frame;
    if (!frame.parse(Data, Length)) {
    	// if parsing failed, ignore frame
    	return Length;
    }

    uint32_t pts = frame.pts();

    if (pts == 0) {
        pts = aPts;
    } else {
        aPts = pts;
    }

    if (!scrSet && aPts != 0) {
        setScr(aPts);
        scrSet = true;
    }

    bool isAc3 = ((Id & 0xF0) == 0x80) || Id == 0xbd;

    if (!isAc3) {
        decoder->decode(&frame, audio);
    } else {
        decoder->ac3dts(&frame, audio);
    }

    return Length;
}

#if VDRVERSNUM >= 10710
void cDxr3Device::GetVideoSize(int &Width, int &Height, double &VideoAspect)
{
    uint32_t aspect;
    CHECK(ioctl(fdControl, EM8300_IOCTL_GET_ASPECTRATIO, &aspect));

    if (aspect == EM8300_ASPECTRATIO_4_3) {
        VideoAspect = 4.0 / 3.0;
    } else {
        VideoAspect = 16.0 / 9.0;
    }

    Width = horizontal;
    Height = vertical;
}
#endif

void cDxr3Device::SetVideoFormat(bool VideoFormat16_9)
{
    int ratio = EM8300_ASPECTRATIO_4_3;
    if (VideoFormat16_9) {
        ratio = EM8300_ASPECTRATIO_16_9;
    }

    int aspect;

    if (cSettings::instance()->forceLetterBox()) {
        ratio = EM8300_ASPECTRATIO_16_9;
    }

    if (Setup.VideoFormat) {
        aspect = EM8300_ASPECTRATIO_4_3;
#ifdef EM8300_IOCTL_SET_WSS
        if (cSettings::Instance().GetUseWSS()) {
            int wssmode;
            if (ratio == EM8300_ASPECTRATIO_16_9) {
                wssmode = EM8300_WSS_16_9;
            } else {
                wssmode = EM8300_WSS_OFF;
            }

            CHECK(ioctl(fdControl, EM8300_IOCTL_SET_WSS, &wssmode));
        }
#endif
    } else {
        aspect = ratio;
    }

    CHECK(ioctl(fdControl, EM8300_IOCTL_SET_ASPECTRATIO, &aspect));

    SetVideoDisplayFormat(eVideoDisplayFormat(Setup.VideoDisplayFormat));
}

void cDxr3Device::SetVolumeDevice(int Volume)
{
    audio->setVolume(Volume);
}

void cDxr3Device::SetAudioChannelDevice(int AudioChannel)
{
    audio->setAudioChannel(AudioChannel);
}

int cDxr3Device::GetAudioChannelDevice()
{
    return audio->getAudioChannel();
}

void cDxr3Device::SetDigitalAudioDevice(bool on)
{
    audio->setDigitalAudio(on);
}

cSpuDecoder *cDxr3Device::GetSpuDecoder()
{
    if (!spuDecoder && IsPrimaryDevice()) {
        spuDecoder = new cDxr3SpuDecoder();
    }
    return spuDecoder;
}

void cDxr3Device::turnPlugin(bool on)
{
    // we have support for a very unique thing.
    // it is possible to release the current
    // used dxr3 device and make it so usable
    // for other programs.
    // and the unique thing is that VDR can run
    // without interrupting it.


    if (on) {

        // check if we already at 'on' state
        if (pluginOn) {
            return;
        }

        // get full control over device
        claimDevices();
        audio->openDevice();

        // enable pes packet processing
        pluginOn = true;

    } else {

        // check if we already at 'off' state
        if (!pluginOn) {
            return;
        }

        // clear buffer

        // release device and give control to somebody else
        Tools::WriteInfoToOsd(tr("DXR3: releasing devices"));
        releaseDevices();
        audio->releaseDevice();

        // disable pes packet processing
        pluginOn = false;
    }
}

void cDxr3Device::dimension(uint32_t &hor, uint32_t &ver)
{
    hor = horizontal;
    ver = vertical;
}

void cDxr3Device::setPalette(uint8_t *pal)
{
    CHECK(ioctl(fdSpu, EM8300_IOCTL_SPU_SETPALETTE, pal));
}

void cDxr3Device::writeSpu(const uint8_t* data, int length)
{
    WriteAllOrNothing(fdSpu, data, length, 1000, 10);
}

void cDxr3Device::setButton(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, uint32_t palette)
{
    em8300_button_t button;

    button.color = palette >> 16;
    button.contrast = palette & 0xFFFF;
    button.top = sy;
    button.bottom = ey;
    button.left = sx;
    button.right = ex;

    CHECK(ioctl(fdSpu, EM8300_IOCTL_SPU_BUTTON, &button));
}

void cDxr3Device::clearButton()
{
    em8300_button_t button;

    // todo: why are there 1s and 2s - memset 0
    button.color = 0;
    button.contrast = 0;
    button.top = 1;
    button.bottom = 2;
    button.left = 1;
    button.right = 2;

    CHECK(ioctl(fdSpu, EM8300_IOCTL_SPU_BUTTON, &button))
}

void cDxr3Device::settingsChange(SettingsChange change)
{
    if (change == BCS) {
        // update bcs value
        bcs.brightness = cSettings::instance()->brightness();
        bcs.contrast = cSettings::instance()->contrast();
        bcs.saturation = cSettings::instance()->saturation();

        CHECK(ioctl(fdControl, EM8300_IOCTL_SETBCS, &bcs));
    }
}

void cDxr3Device::claimDevices()
{
    // open control stream
    fdControl = Dxr3Open(DEV_DXR3_CONT, O_WRONLY | O_SYNC);
    if (fdControl < 0) {
        esyslog("dxr3: please verify that the em8300 modules are loaded");
        exit(1);
    }

    uploadFirmware();

    ///< open multimedia streams
    fdVideo = Dxr3Open(DEV_DXR3_VIDEO, O_WRONLY | O_SYNC);
    fdSpu = Dxr3Open(DEV_DXR3_OSD, O_WRONLY | O_SYNC);

    // everything ok?
    if (fdVideo < 0 || fdSpu < 0) {
        esyslog("dxr3: fatal: unable to open some em8300 devices");
        exit(1);
    }

    // get bcs values from driver
    CHECK(ioctl(fdControl, EM8300_IOCTL_GETBCS, &bcs));

    setPlayMode();
    playBlackFrame(TIMESTAMPS_PREBUFFER);
}

void cDxr3Device::releaseDevices()
{
    close(fdControl);
    close(fdVideo);
    close(fdSpu);

    fdControl = fdVideo = fdSpu = -1;
}

void cDxr3Device::uploadFirmware()
{
    if (!cSettings::instance()->loadFirmware()) {
        return;
    }

    dsyslog("[dxr3-interface] loading firmware");

    // try to open it
    // MICROCODE comes from makefile
    int UCODE = open(MICROCODE, O_RDONLY);

    if (UCODE < 0) {
        esyslog("dxr3: fatal: unable to open microcode file %s", MICROCODE);
        exit(1);
    }

    struct stat s;
    if (fstat(UCODE, &s ) <0) {
        esyslog("dxr3: fatal: unable to fstat microcode file %s", MICROCODE);
        exit(1);
    }

    // read microcode
    em8300_microcode_t em8300_microcode;
    em8300_microcode.ucode = new char[s.st_size];
    if (em8300_microcode.ucode == NULL) {
        esyslog("dxr3: fatal: unable to malloc() space for microcode");
        exit(1);
    }

    if (read(UCODE,em8300_microcode.ucode,s.st_size) < 1) {
        esyslog("dxr3: fatal: unable to read microcode file %s", MICROCODE);
        // free memory to avoid memory leak
        delete[] (char*) em8300_microcode.ucode;
        exit(1);
    }

    close(UCODE);
    em8300_microcode.ucode_size = s.st_size;

    // upload it
    if( ioctl(fdControl, EM8300_IOCTL_INIT, &em8300_microcode) == -1) {
        esyslog("dxr3: fatal: microcode upload failed: %m");
        // free memory to avoid memory leak
        delete [] (char*) em8300_microcode.ucode;
        exit(1);
    }

    // free memory to avoid memory leak
    delete [] (char*) em8300_microcode.ucode;
}

void cDxr3Device::setPlayMode()
{
    uint32_t val = 0;
    CHECK(ioctl(fdControl, EM8300_IOCTL_SCR_SET, &val));

    int ioval = EM8300_PLAYMODE_PLAY;
    CHECK(ioctl(fdControl, EM8300_IOCTL_SET_PLAYMODE, &ioval));
    writeRegister(0, MVCOMMAND_SYNC);
}

void cDxr3Device::playVideoFrame(cDxr3PesFrame *frame, uint32_t pts)
{
    if (pts > 0) {
        uint32_t val = pts - offset;
        CHECK(ioctl(fdVideo, EM8300_IOCTL_VIDEO_SETPTS, &val));
    }

    // apply aspect ratio
    uint32_t ratio = frame->aspectRatio();

    if (aspectRatio != ratio) {
    	if (ratio == EM8300_ASPECTRATIO_16_9)
    		SetVideoFormat(true);
    	else
    		SetVideoFormat(false);

    	aspectRatio = ratio;
    }

    // write video data
    const uint8_t *data = frame->payload();
    uint32_t len = frame->payloadSize();

    WriteAllOrNothing(fdVideo, data, len, 1000, 10);
}

void cDxr3Device::playBlackFrame(uint32_t pts)
{
    extern char blackframe[];
    extern int blackframeLength;

    CHECK(ioctl(fdVideo, EM8300_IOCTL_VIDEO_SETPTS, &pts));

    for (int i = 0; i < 3; i++) {
        WriteAllOrNothing(fdVideo, (const uchar*)blackframe, blackframeLength, 1000, 10);
    }

    horizontal = 720;
    vertical = 576;
}

void cDxr3Device::writeRegister(int reg, int value)
{
    em8300_register_t regs;

    regs.microcode_register = 1;
    regs.reg = reg;
    regs.val = value;

    CHECK(ioctl(fdControl, EM8300_IOCTL_WRITEREG, &regs));
}

void cDxr3Device::setScr(uint32_t val)
{
    uint32_t scr;
    CHECK(ioctl(fdControl, EM8300_IOCTL_SCR_GET, &scr));
    offset = val - scr - TIMESTAMPS_PREBUFFER;
}

uint32_t cDxr3Device::getScr()
{
    uint32_t scr;
    CHECK(ioctl(fdControl, EM8300_IOCTL_SCR_GET, &scr));
    scr += offset;

    return scr;
}

// Local variables:
// mode: c++
// c-file-style: "stroustrup"
// c-file-offsets: ((inline-open . 0))
// tab-width: 4;
// indent-tabs-mode: nil
// End: