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
|
/*
* See the README file for copyright information and how to reach the author.
*
* $Id$
*/
#include "omxdevice.h"
#include "omx.h"
#include "audio.h"
#include "setup.h"
#include <vdr/remux.h>
#include <vdr/tools.h>
#include <string.h>
cOmxDevice::cOmxDevice(void (*onPrimaryDevice)(void)) :
cDevice(),
m_onPrimaryDevice(onPrimaryDevice),
m_omx(new cOmx()),
m_audio(new cAudioDecoder(m_omx)),
m_mutex(new cMutex()),
m_state(eNone),
m_audioId(0)
{
}
cOmxDevice::~cOmxDevice()
{
delete m_omx;
delete m_audio;
delete m_mutex;
}
int cOmxDevice::Init(void)
{
if (m_omx->Init() < 0)
{
esyslog("rpihddevice: failed to initialize OMX!");
return -1;
}
if (m_audio->Init() < 0)
{
esyslog("rpihddevice: failed to initialize audio!");
return -1;
}
return 0;
}
int cOmxDevice::DeInit(void)
{
if (m_audio->DeInit() < 0)
{
esyslog("rpihddevice: failed to deinitialize audio!");
return -1;
}
if (m_omx->DeInit() < 0)
{
esyslog("rpihddevice: failed to deinitialize OMX!");
return -1;
}
return 0;
}
void cOmxDevice::GetOsdSize(int &Width, int &Height, double &PixelAspect)
{
cRpiSetup::GetDisplaySize(Width, Height, PixelAspect);
}
bool cOmxDevice::CanReplay(void) const
{
dsyslog("rpihddevice: CanReplay");
// video codec de-initialization done
return (m_state == eNone);
}
bool cOmxDevice::SetPlayMode(ePlayMode PlayMode)
{
// in case we were in some trick mode
m_omx->SetClockScale(1.0f);
dsyslog("rpihddevice: SetPlayMode(%s)",
PlayMode == pmNone ? "none" :
PlayMode == pmAudioVideo ? "Audio/Video" :
PlayMode == pmAudioOnly ? "Audio only" :
PlayMode == pmAudioOnlyBlack ? "Audio only, black" :
PlayMode == pmVideoOnly ? "Video only" :
"unsupported");
switch (PlayMode)
{
case pmNone:
m_mutex->Lock();
if (HasVideo())
m_omx->FlushVideo(true);
if (HasAudio())
{
m_audio->Reset();
m_omx->FlushAudio();
}
m_omx->Stop();
SetState(eNone);
m_mutex->Unlock();
break;
case pmAudioVideo:
case pmAudioOnly:
case pmAudioOnlyBlack:
case pmVideoOnly:
break;
}
return true;
}
int cOmxDevice::PlayAudio(const uchar *Data, int Length, uchar Id)
{
m_mutex->Lock();
int ret = Length;
// if first packet is audio, we assume audio only
if (State() == eNone)
SetState(eAudioOnly);
if (State() == eAudioOnly || State() == eAudioVideo)
{
if (m_audio->Poll())
{
if (m_audioId != Id)
{
m_audioId = Id;
m_audio->Reset();
}
//dsyslog("A %llu", PesGetPts(Data));
if (!m_audio->WriteData(Data + PesPayloadOffset(Data),
PesLength(Data) - PesPayloadOffset(Data),
PesHasPts(Data) ? PesGetPts(Data) : 0))
esyslog("rpihddevice: failed to write data to audio decoder!");
}
else
ret = 0;
}
m_mutex->Unlock();
return ret;
}
int cOmxDevice::PlayVideo(const uchar *Data, int Length)
{
m_mutex->Lock();
int ret = Length;
if (State() == eNone)
SetState(eStartingVideo);
if (State() == eStartingVideo)
{
cVideoCodec::eCodec codec = ParseVideoCodec(Data, Length);
if (codec != cVideoCodec::eInvalid)
{
if (cRpiSetup::IsVideoCodecSupported(codec))
{
m_omx->SetVideoCodec(codec);
SetState(eAudioVideo);
dsyslog("rpihddevice: set video codec to %s",
cVideoCodec::Str(codec));
}
else
{
SetState(eAudioOnly);
esyslog("rpihddevice: %s video codec not supported!",
cVideoCodec::Str(codec));
}
}
}
if (State() == eVideoOnly || State() == eAudioVideo)
{
int64_t pts = PesHasPts(Data) ? PesGetPts(Data) : 0;
OMX_BUFFERHEADERTYPE *buf = m_omx->GetVideoBuffer(pts);
if (buf)
{
//dsyslog("V %llu", PesGetPts(Data));
const uchar *payload = Data + PesPayloadOffset(Data);
int length = PesLength(Data) - PesPayloadOffset(Data);
if (length <= buf->nAllocLen)
{
memcpy(buf->pBuffer, payload, length);
buf->nFilledLen = length;
}
else
esyslog("rpihddevice: video packet too long for video buffer!");
if (!m_omx->EmptyVideoBuffer(buf))
{
ret = 0;
esyslog("rpihddevice: failed to pass buffer to video decoder!");
}
}
}
m_mutex->Unlock();
return ret;
}
void cOmxDevice::SetState(eState state)
{
switch (state)
{
case eNone:
m_omx->SetClockState(cOmx::eClockStateStop);
break;
case eStartingVideo:
break;
case eAudioOnly:
m_omx->SetClockState(cOmx::eClockStateWaitForAudio);
break;
case eVideoOnly:
m_omx->SetClockState(cOmx::eClockStateWaitForVideo);
break;
case eAudioVideo:
m_omx->SetClockState(cOmx::eClockStateWaitForAudioVideo);
break;
}
m_state = state;
}
int64_t cOmxDevice::GetSTC(void)
{
//dsyslog("S %llu", m_omx->GetSTC());
return m_omx->GetSTC();
}
void cOmxDevice::Play(void)
{
dsyslog("rpihddevice: Play()");
m_omx->SetClockScale(1.0f);
cDevice::Play();
}
void cOmxDevice::Freeze(void)
{
dsyslog("rpihddevice: Freeze()");
m_omx->SetClockScale(0.0f);
cDevice::Freeze();
}
void cOmxDevice::TrickSpeed(int Speed)
{
dsyslog("rpihddevice: TrickSpeed(%d)", Speed);
}
bool cOmxDevice::Flush(int TimeoutMs)
{
dsyslog("rpihddevice: Flush()");
return true;
}
void cOmxDevice::Clear(void)
{
m_mutex->Lock();
dsyslog("rpihddevice: Clear()");
m_omx->SetClockScale(1.0f);
m_omx->SetMediaTime(0);
if (HasAudio())
{
m_audio->Reset();
m_omx->FlushAudio();
}
if (HasVideo())
m_omx->FlushVideo(false);
m_mutex->Unlock();
cDevice::Clear();
}
void cOmxDevice::SetVolumeDevice(int Volume)
{
m_omx->SetVolume(Volume);
}
bool cOmxDevice::Poll(cPoller &Poller, int TimeoutMs)
{
cTimeMs time;
time.Set();
while (!m_omx->PollVideoBuffers() || !m_audio->Poll())
{
if (time.Elapsed() >= TimeoutMs)
return false;
cCondWait::SleepMs(5);
}
return true;
}
void cOmxDevice::MakePrimaryDevice(bool On)
{
if (On && m_onPrimaryDevice)
m_onPrimaryDevice();
cDevice::MakePrimaryDevice(On);
}
cVideoCodec::eCodec cOmxDevice::ParseVideoCodec(const uchar *data, int length)
{
if (!PesHasPts(data))
return cVideoCodec::eInvalid;
if (PesLength(data) - PesPayloadOffset(data) < 6)
return cVideoCodec::eInvalid;
const uchar *p = data + PesPayloadOffset(data);
for (int i = 0; i < 5; i++)
{
// find start code prefix - should be right at the beginning of payload
if ((!p[i] && !p[i + 1] && p[i + 2] == 0x01))
{
if (p[i + 3] == 0xb3) // sequence header
return cVideoCodec::eMPEG2;
else if (p[i + 3] == 0x09) // slice
{
switch (p[i + 4] >> 5)
{
case 0: case 3: case 5: // I frame
return cVideoCodec::eH264;
case 1: case 4: case 6: // P frame
case 2: case 7: // B frame
default:
return cVideoCodec::eInvalid;
}
}
return cVideoCodec::eInvalid;
}
}
return cVideoCodec::eInvalid;
}
|