File: bitstreamout.c

package info (click to toggle)
vdr-plugin-bitstreamout 0.89b-8
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 712 kB
  • ctags: 1,037
  • sloc: ansic: 9,261; sh: 170; makefile: 116
file content (507 lines) | stat: -rw-r--r-- 15,510 bytes parent folder | download | duplicates (2)
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
/*
 * bitstreamout.c:	VDR plugin for redirecting 16bit encoded audio streams
 *			(mainly AC3) received from a DVB card or VDR recording
 *			to S/P-DIF out of a sound card with ALSA (NO decoding!).
 *
 * This program 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; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
 *
 * Copyright (C) 2002-2005 Werner Fink, <werner@suse.de>
 */

#include <stdint.h>
#include <time.h>
#include <getopt.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/rtc.h>
#include <linux/dvb/dmx.h>
#include <vdr/plugin.h>
#include <vdr/interface.h>
#include <vdr/dvbdevice.h>

#include "types.h"
#include "spdif.h"
#include "replay.h"
#include "channel.h"
#include "bitstreamout.h"
#include "ac3.h"
#include "dts.h"
#include "lpcm.h"
#include "mp2.h"
#include "shm_memory_tool.h"

static const char *version	 = VERSION;
static const char *description	 = "bit stream out to S/P-DIF of a sound card";
static const char *mainmenuentry = "Bitstreamout";
static ctrl_t setup = {
    NULL,	// buf
    ((1<<SETUP_ACTIVE)|(1<<SETUP_MP2DITHER)|(1<<SETUP_MP2ENABLE)|(1<<SETUP_CLEAR)),
    {
	0,	// opt.card
	2,	// opt.device
	0,	// opt.delay
	0,	// opt.ldelay
	7,	// opt.mdelay
	0,	// opt.adelay
	false,	// opt.type
	true,	// opt.variable
	false	// opt.mmap
    }
};

#define MAP_MEM		(MAP_SHARED|MAP_NORESERVE|MAP_LOCKED)
#define MAINMENUTIMEOUT	5000 //ms

class cBitStreamOut : public cPlugin {
private:
    cReplayOutSPDif  *ReplayOutSPDif;
    cChannelOutSPDif *ChannelOutSPDif;
    static spdif spdifDev;
    static cBounce * bounce;
    bool onoff;
    bool mp2dec;
    char *SPDIFmute;
    unsigned long rtc;
protected:
    int active;
    int mp2spdif;
    int mp2enable;
public:
    cBitStreamOut(void);
    virtual ~cBitStreamOut(void);
    virtual bool Start(void);
    virtual void Stop(void);
    virtual const char *Version(void);
    virtual const char *Description(void);
    virtual const char *CommandLineHelp(void);
    virtual bool ProcessArgs(int argc, char *argv[]);
    virtual cMenuSetupPage *SetupMenu(void);
    virtual bool SetupParse(const char *Name, const char *Value);
    virtual const char *MainMenuEntry(void);
    virtual cOsdObject *MainMenuAction(void);
};

class cMenuSetupBSO : public cMenuSetupPage {
private:
    virtual void Set(void);
protected:
    static opt_t opt;
    int active;
    int mp2spdif;
    int mp2enable;
    virtual void Store(void);
public:
    cMenuSetupBSO(void) { Set(); };
};

class cDisplayMainMenu : public cMenuSetupPage {
private:
    cChannelOutSPDif *ChannelOutSPDif;
    cReplayOutSPDif  *ReplayOutSPDif;
    char        apidstrs[MAXAPIDS][255];
    const char *apidstrsptr[MAXAPIDS+1];
    int         apidstrnum;
    virtual void Set(void);
protected:
    static opt_t opt;
    int active;
    int mp2spdif;
    int mp2enable;
    int apidstrid;
    virtual void Store(void);
public:
    cDisplayMainMenu(cChannelOutSPDif *COSPDif, cReplayOutSPDif *ROSPDif) :
    ChannelOutSPDif(COSPDif), ReplayOutSPDif(ROSPDif) { Set(); };
};

static const char * mp2out[] = {"Round", "Dither", "Nonlinear"};
static const int    mp2num   = sizeof(mp2out)/sizeof(*mp2out);

// --- cBitStreamOut : VDR interface for redirecting none audio streams ----------------

spdif cBitStreamOut::spdifDev(setup);
cBounce * cBitStreamOut::bounce;

cBitStreamOut::cBitStreamOut(void)
{
    setup.buf = NULL;
    bounce = NULL;
    onoff = false;
    mp2dec = false;
    SPDIFmute = NULL;
    ChannelOutSPDif = NULL;
    ReplayOutSPDif = NULL;
    mp2spdif = 1;			// Default is `Dither'
    rtc = 0;
}

cBitStreamOut::~cBitStreamOut(void)
{
    if (SPDIFmute)
	free(SPDIFmute);
    SPDIFmute = NULL;

    if (bounce)
	delete bounce;
    bounce = NULL;

    if (setup.buf)
	shm_free(setup.buf);
    setup.buf = NULL;

    if (mp2dec)
	mp2.Release();
}

void cBitStreamOut::Stop(void)
{
    set_setup(CLEAR);
    if (ReplayOutSPDif) {
	ReplayOutSPDif->Clear();
#ifdef VDR_DOES_CHECK_OBJECT
	delete ReplayOutSPDif;
	ReplayOutSPDif = NULL;
#endif
    }

    if (ChannelOutSPDif) {
	ChannelOutSPDif->Clear();
	delete ChannelOutSPDif;
	ChannelOutSPDif = NULL;
    }

    if (spdifDev)
	spdifDev.Close();

#if 0
    if (rtc) {
        int fd;
        if ((fd = open ("/dev/rtc", O_RDONLY)) >= 0) {
	    ioctl(fd, RTC_IRQP_SET, rtc);
	    errno = 0;
	    close(fd);
	}

    }
#endif
}

const char *cBitStreamOut::Version(void)
{
    return version;
}

const char *cBitStreamOut::Description(void)
{
    return description;
}

const char *cBitStreamOut::MainMenuEntry(void)
{
    return onoff ? mainmenuentry : NULL;
}

bool cBitStreamOut::Start(void)
{
    setup.buf = (uint_8*)shm_malloc(sizeof(uint_8)*OVERALL_MEM, MAP_MEM);

    if (setup.buf == NULL) {
	esyslog("cBitStreamOut::Start() shm_malloc failed\n");
	goto err;
    }

    if (!(mp2dec = mp2.Initialize())) {
       esyslog("cBitStreamOut::Start() mp2 initialization failed\n");
       goto err;
    }

    if (!(bounce = new cBounce(setup.buf, BOUNCE_MEM)))
	goto err;

    ac3.SetBuffer(setup.buf + SPDIF_START);
    dts.SetBuffer(setup.buf + SPDIF_START);
    pcm.SetBuffer(setup.buf + SPDIF_START);
    mp2.SetBuffer(setup.buf + SPDIF_START);

    if (!(ReplayOutSPDif = new cReplayOutSPDif(spdifDev, setup, bounce, SPDIFmute)))
	goto err;

    if (!(ChannelOutSPDif = new cChannelOutSPDif(spdifDev, setup, bounce, SPDIFmute)))
	goto err;

    if (getuid() == 0) {			// Give maximum speed for ALSA
	int fd;

	if ((fd = open ("/dev/rtc", O_RDONLY)) >= 0) {
	    if (ioctl(fd, RTC_IRQP_READ, &rtc) == 0) {
		unsigned long tmp = 8192;
		ioctl(fd, RTC_IRQP_SET, tmp);
		errno = 0;
	    }
	    close(fd);
	}
    }

    clear_setup(CLEAR);
    return true;
err:
    set_setup(CLEAR);
    Stop();
    return false;
}

const char *cBitStreamOut::CommandLineHelp(void)
{
    return "  -o,        --onoff        enable an control entry in the main menu\n"
	   "  -m script, --mute=script  script for en/dis-able the spdif interface\n";
}

bool cBitStreamOut::ProcessArgs(int argc, char *argv[])
{
    bool ret = true;
    const struct option long_option[] =
    {
	{ "onoff", no_argument,		NULL, 'o' },
	{ "mute",  required_argument,	NULL, 'm' },
	{  NULL,   no_argument,		NULL,  0  },
    };

    int c = 0;

    // Reset all opt variables to their initial values because vdr has its
    // own options already scanned.
    optarg = NULL;
    optind = opterr = optopt = 0;
    while ((c = getopt_long(argc, argv, "om:", long_option, NULL)) > 0) {
	switch (c) {
	case 'o':
	    onoff = true;
	    break;
	case 'm':
	    if (SPDIFmute)
		free(SPDIFmute);
	    if (!(SPDIFmute = strdup(optarg))) {
		esyslog("ERROR: out of memory");
		ret = false;
	    }
	    break;
	default:
	    ret = false;
	    break;
	}
    }
    return ret;
}

cMenuSetupPage *cBitStreamOut::SetupMenu(void)
{
    return new cMenuSetupBSO;
}

bool cBitStreamOut::SetupParse(const char *Name, const char *Value)
{
    bool mp2new = false;
    bool ret = true;
    if      (!strcasecmp(Name, "Card"))       setup.opt.card     = atoi(Value);
    else if (!strcasecmp(Name, "Device"))     setup.opt.device   = atoi(Value);
    else if (!strcasecmp(Name, "Delay"))      setup.opt.delay    = atoi(Value);
    else if (!strcasecmp(Name, "LiveDelay"))  setup.opt.ldelay   = atoi(Value);
    else if (!strcasecmp(Name, "PCMinit"))    setup.opt.mdelay   = atoi(Value);
    else if (!strcasecmp(Name, "Mp2offset"))  setup.opt.adelay   = atoi(Value);
    else if (!strcasecmp(Name, "IEC958"))     setup.opt.type     = atoi(Value);
    else if (!strcasecmp(Name, "VariableIO")) setup.opt.variable = atoi(Value);
    else if (!strcasecmp(Name, "MemoryMap"))  setup.opt.mmap     = atoi(Value);
    else if (!strcasecmp(Name, "Active")) {
	(active    = atoi(Value)) ? set_setup(ACTIVE)    : clear_setup(ACTIVE);
    } else if (!strcasecmp(Name, "Mp2Enable")) {
	(mp2enable = atoi(Value)) ? set_setup(MP2ENABLE) : clear_setup(MP2ENABLE);
    } else if (!strcasecmp(Name, "Mp2Dither") && !mp2new) {
	(mp2spdif = atoi(Value)) ? set_setup(MP2DITHER)  : clear_setup(MP2DITHER);
    } else if (!strcasecmp(Name, "Mp2Out")) {
	mp2new = true;
	for (mp2spdif = 0; mp2spdif < mp2num; mp2spdif++) {
	    if (!strcasecmp(Value, mp2out[mp2spdif]))
		break;
	}
	switch (mp2spdif) {
	default: mp2spdif = 0;
	case 0:  clear_setup(MP2DITHER); clear_setup(MP2SPDIF); break;
	case 1:  set_setup  (MP2DITHER); clear_setup(MP2SPDIF); break;
	case 2:  set_setup  (MP2DITHER); set_setup  (MP2SPDIF); break;
	}
    } else
	ret = false;
    if (setup.opt.mdelay < 4)
	setup.opt.mdelay = 4;

    if (active)
	cDvbDevice::SetTransferModeForDolbyDigital(false);
    else
	cDvbDevice::SetTransferModeForDolbyDigital(true);

    return ret;
}

cOsdObject *cBitStreamOut::MainMenuAction(void)
{
    cOsdObject * ret = NULL;
    if (onoff)
	ret = new cDisplayMainMenu(ChannelOutSPDif, ReplayOutSPDif);
    return ret;
}

// --- cDisplayMainMenu ----------------------------------------------------------------

opt_t cDisplayMainMenu::opt;

void cDisplayMainMenu::Set(void)
{
    char title[255];
    cPlugin * bso = cPluginManager::GetPlugin("bitstreamout");

    SetPlugin(bso);

    memcpy(&opt, &(setup.opt), sizeof(opt_t));
    active    = ((test_setup(ACTIVE))    ? true : false);
    mp2enable = ((test_setup(MP2ENABLE)) ? true : false);
    mp2spdif  = ((test_setup(MP2SPDIF)) ? 2 : ((test_setup(MP2DITHER)) ? 1 : 0));

    snprintf(title, 255, "%s (%s%s)", mainmenuentry,
	     (active) ? "AC3" : "Off", (mp2enable) ? "/MP2" : "");
    SetTitle(title);

    Add(new cMenuEditBoolItem("On/Off",     &(active),       "Off", "On" ));
    Add(new cMenuEditBoolItem("Mp2Enable",  &(mp2enable),    "Off", "On" ));
    Add(new cMenuEditStraItem("Mp2Out",     &(mp2spdif),    mp2num, mp2out));
    Add(new cMenuEditIntItem ("Delay",      &(opt.delay),     0,     32  ));
    Add(new cMenuEditIntItem ("LiveDelay",  &(opt.ldelay),    0,     32  ));
    Add(new cMenuEditIntItem ("PCMinital",  &(opt.mdelay),    4,     12  ));
    Add(new cMenuEditIntItem ("Mp2offset",  &(opt.adelay),    0,     12  ));

    (active)    ? set_setup(ACTIVE)    : clear_setup(ACTIVE);
    (mp2enable) ? set_setup(MP2ENABLE) : clear_setup(MP2ENABLE);
    switch (mp2spdif) {
    default: mp2spdif = 0;
    case 0:  clear_setup(MP2DITHER); clear_setup(MP2SPDIF); break;
    case 1:  set_setup  (MP2DITHER); clear_setup(MP2SPDIF); break;
    case 2:  set_setup  (MP2DITHER); set_setup  (MP2SPDIF); break;
    }

    if (active)
	cDvbDevice::SetTransferModeForDolbyDigital(false);
    else
	cDvbDevice::SetTransferModeForDolbyDigital(true);

    debug("cDisplayMainMenu::Set\n");
}

void cDisplayMainMenu::Store(void)
{
    SetupStore("Delay",      setup.opt.delay    = opt.delay);
    SetupStore("LiveDelay",  setup.opt.ldelay   = opt.ldelay);
    SetupStore("PCMinit",    setup.opt.mdelay   = opt.mdelay);
    SetupStore("Mp2offset",  setup.opt.adelay   = opt.adelay);
    SetupStore("Active",     ((active)    ? true : false));
    SetupStore("Mp2Enable",  ((mp2enable) ? true : false));
    SetupStore("Mp2Out",     mp2out[mp2spdif]);
    (active)    ? set_setup(ACTIVE)    : clear_setup(ACTIVE);
    (mp2enable) ? set_setup(MP2ENABLE) : clear_setup(MP2ENABLE);
    switch (mp2spdif) {
    default:
    case 0: clear_setup(MP2DITHER); clear_setup(MP2SPDIF); break;
    case 1: set_setup  (MP2DITHER); clear_setup(MP2SPDIF); break;
    case 2: clear_setup(MP2DITHER); set_setup  (MP2SPDIF); break;
    }
    set_setup(RESET);

    debug("cDisplayMainMenu::Store set apid idx: %d/%d\n", apidstrid, apidstrnum);

    if (active)
	cDvbDevice::SetTransferModeForDolbyDigital(false);
    else
	cDvbDevice::SetTransferModeForDolbyDigital(true);
    debug("cDisplayMainMenu::Store\n");
}

// --- cMenuSetupBSO -------------------------------------------------------------------

opt_t cMenuSetupBSO::opt;

void cMenuSetupBSO::Set(void)
{
    memcpy(&opt, &(setup.opt), sizeof(opt_t));
    active    = ((test_setup(ACTIVE))    ? true : false);
    mp2enable = ((test_setup(MP2ENABLE)) ? true : false);
    mp2spdif  = ((test_setup(MP2SPDIF)) ? 2 : ((test_setup(MP2DITHER)) ? 1 : 0));
    Add(new cMenuEditBoolItem("On/Off",     &(active),       "Off", "On" ));
    Add(new cMenuEditBoolItem("Mp2Enable",  &(mp2enable),    "Off", "On" ));
    Add(new cMenuEditStraItem("Mp2Out",     &(mp2spdif),    mp2num, mp2out));
    Add(new cMenuEditIntItem ("Card",       &(opt.card),      0,     8-1 ));
    Add(new cMenuEditIntItem ("Device",     &(opt.device),    0,    32-1 ));
    Add(new cMenuEditIntItem ("Delay",      &(opt.delay),     0,     32  ));
    Add(new cMenuEditIntItem ("LiveDelay",  &(opt.ldelay),    0,     32  ));
    Add(new cMenuEditIntItem ("PCMinital",  &(opt.mdelay),    4,     12  ));
    Add(new cMenuEditIntItem ("Mp2offset",  &(opt.adelay),    0,     12  ));
    Add(new cMenuEditBoolItem("IEC958",     &(opt.type),     "Con", "Pro"));
    Add(new cMenuEditBoolItem("VariableIO", &(opt.variable), "No",  "Yes"));
    Add(new cMenuEditBoolItem("MemoryMap",  &(opt.mmap),     "No",  "Yes"));
    (active)    ? set_setup(ACTIVE)    : clear_setup(ACTIVE);
    (mp2enable) ? set_setup(MP2ENABLE) : clear_setup(MP2ENABLE);
    switch (mp2spdif) {
    default:
    case 0: clear_setup(MP2DITHER); clear_setup(MP2SPDIF); break;
    case 1: set_setup  (MP2DITHER); clear_setup(MP2SPDIF); break;
    case 2: clear_setup(MP2DITHER); set_setup  (MP2SPDIF); break;
    }

    if (active)
	cDvbDevice::SetTransferModeForDolbyDigital(false);
    else
	cDvbDevice::SetTransferModeForDolbyDigital(true);
}

void cMenuSetupBSO::Store(void)
{
    SetupStore("Card",       setup.opt.card     = opt.card);
    SetupStore("Device",     setup.opt.device   = opt.device);
    SetupStore("Delay",      setup.opt.delay    = opt.delay);
    SetupStore("LiveDelay",  setup.opt.ldelay   = opt.ldelay);
    SetupStore("PCMinit",    setup.opt.mdelay   = opt.mdelay);
    SetupStore("Mp2offset",  setup.opt.adelay   = opt.adelay);
    SetupStore("IEC958",     setup.opt.type     = opt.type);
    SetupStore("VariableIO", setup.opt.variable = opt.variable);
    SetupStore("MemoryMap",  setup.opt.mmap     = opt.mmap);
    SetupStore("Active",     ((active)    ? true : false));
    SetupStore("Mp2Enable",  ((mp2enable) ? true : false));
    SetupStore("Mp2Out",     mp2out[mp2spdif]);
    (active)    ? set_setup(ACTIVE)    : clear_setup(ACTIVE);
    (mp2enable) ? set_setup(MP2ENABLE) : clear_setup(MP2ENABLE);
    switch (mp2spdif) {
    default:
    case 0: clear_setup(MP2DITHER); clear_setup(MP2SPDIF); break;
    case 1: set_setup  (MP2DITHER); clear_setup(MP2SPDIF); break;
    case 2: clear_setup(MP2DITHER); set_setup  (MP2SPDIF); break;
    }

    if (active)
	cDvbDevice::SetTransferModeForDolbyDigital(false);
    else
	cDvbDevice::SetTransferModeForDolbyDigital(true);

    set_setup(RESET);
}

VDRPLUGINCREATOR(cBitStreamOut);