This repository has been archived by the owner on Dec 19, 2022. It is now read-only.
forked from reufer/rpihddevice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.c
424 lines (355 loc) · 10.8 KB
/
setup.c
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
/*
* rpihddevice - VDR HD output device for Raspberry Pi
* Copyright (C) 2014, 2015, 2016 Thomas Reufer
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "setup.h"
#include "display.h"
#include "ovgosd.h"
#include <vdr/tools.h>
#include <vdr/menuitems.h>
#include <getopt.h>
#include <bcm_host.h>
#include "interface/vchiq_arm/vchiq_if.h"
#include "interface/vmcs_host/vc_tvservice.h"
/* ------------------------------------------------------------------------- */
class cRpiSetupPage : public cMenuSetupPage
{
public:
cRpiSetupPage(
cRpiSetup::AudioParameters audio,
cRpiSetup::VideoParameters video,
cRpiSetup::OsdParameters osd) :
m_audio(audio),
m_video(video),
m_osd(osd)
{
m_audioPort[0] = tr("analog");
m_audioPort[1] = tr("HDMI");
m_audioFormat[0] = tr("pass through");
m_audioFormat[1] = tr("multi channel PCM");
m_audioFormat[2] = tr("stereo PCM");
m_videoFraming[0] = tr("box");
m_videoFraming[1] = tr("crop");
m_videoFraming[2] = tr("stretch");
m_videoResolution[0] = tr("default");
m_videoResolution[1] = tr("follow video");
m_videoResolution[2] = "720x480 (4:3)";
m_videoResolution[3] = "720x480 (16:9)";
m_videoResolution[4] = "720x576 (4:3)";
m_videoResolution[5] = "720x576 (16:9)";
m_videoResolution[6] = "1280x720";
m_videoResolution[7] = "1920x1080";
m_videoFrameRate[0] = tr("default");
m_videoFrameRate[1] = tr("follow video");
m_videoFrameRate[2] = "24p";
m_videoFrameRate[3] = "25p";
m_videoFrameRate[4] = "30p";
m_videoFrameRate[5] = "50i";
m_videoFrameRate[6] = "50p";
m_videoFrameRate[7] = "60i";
m_videoFrameRate[8] = "60p";
m_useAdvancedDeinterlacer[0] = trVDR("no");
m_useAdvancedDeinterlacer[1] = tr("for SD video only");
m_useAdvancedDeinterlacer[2] = tr("always");
Setup();
}
eOSState ProcessKey(eKeys Key)
{
int newAudioPort = m_audio.port;
eOSState state = cMenuSetupPage::ProcessKey(Key);
if (Key != kNone)
{
if (newAudioPort != m_audio.port)
Setup();
}
return state;
}
protected:
virtual void Store(void)
{
SetupStore("AudioPort", m_audio.port);
SetupStore("AudioFormat", m_audio.format);
SetupStore("VideoFraming", m_video.framing);
SetupStore("Resolution", m_video.resolution);
SetupStore("FrameRate", m_video.frameRate);
SetupStore("AdvancedDeinterlacer", m_video.advancedDeinterlacer);
SetupStore("AcceleratedOsd", m_osd.accelerated);
cRpiSetup::GetInstance()->Set(m_audio, m_video, m_osd);
}
private:
void Setup(void)
{
int current = Current();
Clear();
if (!cRpiDisplay::IsFixedMode())
{
Add(new cMenuEditStraItem(
tr("Resolution"), &m_video.resolution, 8, m_videoResolution));
Add(new cMenuEditStraItem(
tr("Frame Rate"), &m_video.frameRate, 9, m_videoFrameRate));
}
if (cRpiDisplay::IsProgressive())
Add(new cMenuEditStraItem(
tr("Use Advanced Deinterlacer"),
&m_video.advancedDeinterlacer, 3,
m_useAdvancedDeinterlacer));
Add(new cMenuEditStraItem(
tr("Video Framing"), &m_video.framing, 3, m_videoFraming));
Add(new cMenuEditStraItem(
tr("Audio Port"), &m_audio.port, 2, m_audioPort));
if (m_audio.port == 1)
{
Add(new cMenuEditStraItem(tr("Digital Audio Format"),
&m_audio.format, 3, m_audioFormat));
}
Add(new cMenuEditBoolItem(
tr("Use GPU accelerated OSD"), &m_osd.accelerated));
SetCurrent(Get(current));
Display();
}
cRpiSetup::AudioParameters m_audio;
cRpiSetup::VideoParameters m_video;
cRpiSetup::OsdParameters m_osd;
const char *m_audioPort[2];
const char *m_audioFormat[3];
const char *m_videoFraming[3];
const char *m_videoResolution[8];
const char *m_videoFrameRate[9];
const char *m_useAdvancedDeinterlacer[3];
};
/* ------------------------------------------------------------------------- */
cRpiSetup* cRpiSetup::s_instance = 0;
cRpiSetup* cRpiSetup::GetInstance(void)
{
if (!s_instance)
s_instance = new cRpiSetup();
return s_instance;
}
void cRpiSetup::DropInstance(void)
{
delete s_instance;
s_instance = 0;
bcm_host_deinit();
}
bool cRpiSetup::HwInit(void)
{
cRpiSetup* instance = GetInstance();
if (!instance)
return false;
bcm_host_init();
if (!vc_gencmd_send("codec_enabled MPG2"))
{
char buffer[1024];
if (!vc_gencmd_read_response(buffer, sizeof(buffer)))
{
if (!strcasecmp(buffer,"MPG2=enabled"))
GetInstance()->m_mpeg2Enabled = true;
}
}
int width, height;
if (!cRpiDisplay::GetSize(width, height))
{
ILOG("HwInit() done, display size is %dx%d", width, height);
}
else
ELOG("failed to get video port information!");
return true;
}
void cRpiSetup::SetAudioSetupChangedCallback(void (*callback)(void*), void* data)
{
GetInstance()->m_onAudioSetupChanged = callback;
GetInstance()->m_onAudioSetupChangedData = data;
}
void cRpiSetup::SetVideoSetupChangedCallback(void (*callback)(void*), void* data)
{
GetInstance()->m_onVideoSetupChanged = callback;
GetInstance()->m_onVideoSetupChangedData = data;
}
bool cRpiSetup::IsAudioFormatSupported(cAudioCodec::eCodec codec,
int channels, int samplingRate)
{
// MPEG-1 layer 2 audio pass-through not supported by audio render
// and AAC audio pass-through not yet working
if (codec == cAudioCodec::eMPG || codec == cAudioCodec::eAAC)
return false;
if (channels < 2 || channels > 6)
return false;
switch (GetAudioFormat())
{
case cAudioFormat::ePassThrough:
return (vc_tv_hdmi_audio_supported(
codec == cAudioCodec::eMPG ? EDID_AudioFormat_eMPEG1 :
codec == cAudioCodec::eAC3 ? EDID_AudioFormat_eAC3 :
codec == cAudioCodec::eEAC3 ? EDID_AudioFormat_eEAC3 :
codec == cAudioCodec::eAAC ? EDID_AudioFormat_eAAC :
codec == cAudioCodec::eDTS ? EDID_AudioFormat_eDTS :
EDID_AudioFormat_ePCM, channels,
samplingRate == 32000 ? EDID_AudioSampleRate_e32KHz :
samplingRate == 44100 ? EDID_AudioSampleRate_e44KHz :
samplingRate == 88200 ? EDID_AudioSampleRate_e88KHz :
samplingRate == 96000 ? EDID_AudioSampleRate_e96KHz :
samplingRate == 176000 ? EDID_AudioSampleRate_e176KHz :
samplingRate == 192000 ? EDID_AudioSampleRate_e192KHz :
EDID_AudioSampleRate_e48KHz,
EDID_AudioSampleSize_16bit) == 0);
case cAudioFormat::eMultiChannelPCM:
return codec == cAudioCodec::ePCM;
default:
case cAudioFormat::eStereoPCM:
return codec == cAudioCodec::ePCM && channels == 2;
}
}
void cRpiSetup::SetHDMIChannelMapping(bool passthrough, int channels)
{
char command[80], response[80];
sprintf(command, "hdmi_stream_channels %d", passthrough ? 1 : 0);
vc_gencmd(response, sizeof(response), command);
uint32_t channel_map = 0;
if (!passthrough && channels > 0 && channels <= 6)
{
const unsigned char ch_mapping[6][8] =
{
{ 0, 0, 0, 0, 0, 0, 0, 0 }, // not supported
{ 1, 2, 0, 0, 0, 0, 0, 0 }, // 2.0
{ 1, 2, 4, 0, 0, 0, 0, 0 }, // 2.1
{ 0, 0, 0, 0, 0, 0, 0, 0 }, // not supported
{ 0, 0, 0, 0, 0, 0, 0, 0 }, // not supported
{ 1, 2, 4, 3, 5, 6, 0, 0 }, // 5.1
};
// speaker layout according CEA 861, Table 28: Audio InfoFrame, byte 4
const unsigned char cea_map[] =
{
0xff, // not supported
0x00, // 2.0
0x01, // 2.1
0xff, // not supported
0xff, // not supported
0x0b // 5.1
};
for (int ch = 0; ch < channels; ch++)
if (ch_mapping[channels - 1][ch])
channel_map |= (ch_mapping[channels - 1][ch] - 1) << (3 * ch);
channel_map |= cea_map[channels - 1] << 24;
}
sprintf(command, "hdmi_channel_map 0x%08x", channel_map);
vc_gencmd(response, sizeof(response), command);
}
cMenuSetupPage* cRpiSetup::GetSetupPage(void)
{
return new cRpiSetupPage(m_audio, m_video, m_osd);
}
bool cRpiSetup::Parse(const char *name, const char *value)
{
if (!strcasecmp(name, "AudioPort"))
m_audio.port = atoi(value);
else if (!strcasecmp(name, "AudioFormat"))
m_audio.format = atoi(value);
else if (!strcasecmp(name, "VideoFraming"))
m_video.framing = atoi(value);
else if (!strcasecmp(name, "Resolution"))
m_video.resolution = atoi(value);
else if (!strcasecmp(name, "FrameRate"))
m_video.frameRate = atoi(value);
else if (!strcasecmp(name, "AdvancedDeinterlacer"))
m_video.advancedDeinterlacer = atoi(value);
else if (!strcasecmp(name, "AcceleratedOsd"))
m_osd.accelerated = atoi(value);
else return false;
return true;
}
void cRpiSetup::Set(AudioParameters audio, VideoParameters video,
OsdParameters osd)
{
if (audio != m_audio)
{
m_audio = audio;
if (m_onAudioSetupChanged)
m_onAudioSetupChanged(m_onAudioSetupChangedData);
}
if (video != m_video)
{
m_video = video;
if (m_onVideoSetupChanged)
m_onVideoSetupChanged(m_onVideoSetupChangedData);
}
if (osd != m_osd)
{
m_osd = osd;
cRpiOsdProvider::ResetOsd(false);
}
}
bool cRpiSetup::ProcessArgs(int argc, char *argv[])
{
const int cDisplayOpt = 0x100;
static struct option long_options[] = {
{ "disable-osd", no_argument, NULL, 'd' },
{ "display", required_argument, NULL, cDisplayOpt },
{ "video-layer", required_argument, NULL, 'v' },
{ "osd-layer", required_argument, NULL, 'o' },
{ 0, 0, 0, 0 }
};
int c;
while ((c = getopt_long(argc, argv, "do:v:", long_options, NULL)) != -1)
{
switch (c)
{
case 'd':
m_plugin.hasOsd = false;
break;
case 'o':
m_plugin.osdLayer = atoi(optarg);
break;
case 'v':
m_plugin.videoLayer = atoi(optarg);
break;
case cDisplayOpt:
{
int d = atoi(optarg);
switch (d)
{
case 0:
case 4:
case 5:
case 6:
m_plugin.display = d;
break;
default:
ELOG("invalid device id (%d), using default display!", d);
break;
}
}
break;
default:
return false;
}
}
DBG("dispmanx layers: video=%d, osd=%d (%s), display=%d",
m_plugin.videoLayer, m_plugin.osdLayer,
m_plugin.hasOsd ? "enabled" : "disabled", m_plugin.display);
return true;
}
const char *cRpiSetup::CommandLineHelp(void)
{
return " -d, --disable-osd disable OSD\n"
" -v, --video-layer dispmanx layer for video (default 0)\n"
" -o, --osd-layer dispmanx layer for OSD (default 2)\n"
" --display display used for output:\n"
" 0: default display (default)\n"
" 4: LCD\n"
" 5: TV/HDMI\n"
" 6: non-default display\n";
}