/
mythcommflagplayer.cpp
224 lines (189 loc) · 6.52 KB
/
mythcommflagplayer.cpp
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
#include <iostream>
using namespace std;
#include <QRunnable>
#include "mythcommflagplayer.h"
#include "mthreadpool.h"
#include "mythlogging.h"
class RebuildSaver : public QRunnable
{
public:
RebuildSaver(DecoderBase *d, uint64_t f, uint64_t l)
: m_decoder(d), m_first(f), m_last(l)
{
QMutexLocker locker(&s_lock);
s_cnt[d]++;
}
virtual void run(void)
{
m_decoder->SavePositionMapDelta(m_first, m_last);
QMutexLocker locker(&s_lock);
s_cnt[m_decoder]--;
if (!s_cnt[m_decoder])
s_wait.wakeAll();
}
static uint GetCount(DecoderBase *d)
{
QMutexLocker locker(&s_lock);
return s_cnt[d];
}
static void Wait(DecoderBase *d)
{
QMutexLocker locker(&s_lock);
if (!s_cnt[d])
return;
while (s_wait.wait(&s_lock))
{
if (!s_cnt[d])
return;
}
}
private:
DecoderBase *m_decoder;
uint64_t m_first;
uint64_t m_last;
static QMutex s_lock;
static QWaitCondition s_wait;
static QMap<DecoderBase*,uint> s_cnt;
};
QMutex RebuildSaver::s_lock;
QWaitCondition RebuildSaver::s_wait;
QMap<DecoderBase*,uint> RebuildSaver::s_cnt;
bool MythCommFlagPlayer::RebuildSeekTable(
bool showPercentage, StatusCallback cb, void* cbData)
{
int percentage = 0;
uint64_t myFramesPlayed = 0, pmap_first = 0, pmap_last = 0;
killdecoder = false;
framesPlayed = 0;
// clear out any existing seektables
player_ctx->LockPlayingInfo(__FILE__, __LINE__);
if (player_ctx->playingInfo)
{
player_ctx->playingInfo->ClearPositionMap(MARK_KEYFRAME);
player_ctx->playingInfo->ClearPositionMap(MARK_GOP_START);
player_ctx->playingInfo->ClearPositionMap(MARK_GOP_BYFRAME);
player_ctx->playingInfo->ClearPositionMap(MARK_DURATION_MS);
}
player_ctx->UnlockPlayingInfo(__FILE__, __LINE__);
if (OpenFile() < 0)
return false;
SetPlaying(true);
if (!InitVideo())
{
LOG(VB_GENERAL, LOG_ERR,
"RebuildSeekTable unable to initialize video");
SetPlaying(false);
return false;
}
ClearAfterSeek();
int save_timeout = 1001;
MythTimer flagTime, ui_timer, inuse_timer, save_timer;
flagTime.start();
ui_timer.start();
inuse_timer.start();
save_timer.start();
DecoderGetFrame(kDecodeNothing,true);
decoder->TrackTotalDuration(true);
if (showPercentage)
cout << "\r \r" << flush;
int prevperc = -1;
bool usingIframes = false;
while (GetEof() == kEofStateNone)
{
if (inuse_timer.elapsed() > 2534)
{
inuse_timer.restart();
player_ctx->LockPlayingInfo(__FILE__, __LINE__);
if (player_ctx->playingInfo)
player_ctx->playingInfo->UpdateInUseMark();
player_ctx->UnlockPlayingInfo(__FILE__, __LINE__);
}
if (save_timer.elapsed() > save_timeout)
{
// Give DB some breathing room if it gets far behind..
if (myFramesPlayed - pmap_last > 5000)
usleep(200 * 1000);
// If we're already saving, just save a larger block next time..
if (RebuildSaver::GetCount(decoder) < 1)
{
pmap_last = myFramesPlayed;
MThreadPool::globalInstance()->start(
new RebuildSaver(decoder, pmap_first, pmap_last),
"RebuildSaver");
pmap_first = pmap_last + 1;
}
save_timer.restart();
}
if (ui_timer.elapsed() > 98)
{
ui_timer.restart();
if (totalFrames)
{
float elapsed = flagTime.elapsed() * 0.001f;
int flagFPS = (elapsed > 0.0f) ?
(int)(myFramesPlayed / elapsed) : 0;
percentage = myFramesPlayed * 100 / totalFrames;
if (cb)
(*cb)(percentage, cbData);
if (showPercentage)
{
QString str = QString("\r%1%/%2fps \r")
.arg(percentage,3).arg(flagFPS,5);
cout << qPrintable(str) << flush;
}
else if (percentage % 10 == 0 && prevperc != percentage)
{
prevperc = percentage;
LOG(VB_GENERAL, LOG_INFO, QString("Progress %1% @ %2fps")
.arg(percentage,3).arg(flagFPS,5));
}
}
else
{
if (showPercentage)
{
QString str = QString("\r%1 \r").arg(myFramesPlayed,6);
cout << qPrintable(str) << flush;
}
else if (myFramesPlayed % 1000 == 0)
{
LOG(VB_GENERAL, LOG_INFO, QString("Frames processed %1")
.arg(myFramesPlayed));
}
}
}
if (DecoderGetFrame(kDecodeNothing,true))
myFramesPlayed = decoder->GetFramesRead();
// H.264 recordings from an HD-PVR contain IDR keyframes,
// which are the only valid cut points for lossless cuts.
// However, DVB-S h.264 recordings may lack IDR keyframes, in
// which case we need to allow non-IDR I-frames. If we get
// far enough into the rebuild without having created any
// seektable entries, we can assume it is because of the IDR
// keyframe setting, and so we rewind and allow h.264 non-IDR
// I-frames to be treated as keyframes.
uint64_t frames = decoder->GetFramesRead();
if (!usingIframes &&
(GetEof() != kEofStateNone || (frames > 1000 && frames < 1100)) &&
!decoder->HasPositionMap())
{
cout << "No I-frames found, rewinding..." << endl;
decoder->DoRewind(0);
decoder->Reset(true, true, true);
pmap_first = pmap_last = myFramesPlayed = 0;
decoder->SetIdrOnlyKeyframes(false);
usingIframes = true;
}
}
if (showPercentage)
cout << "\r \r" << flush;
SaveTotalDuration();
SaveTotalFrames();
SetPlaying(false);
killdecoder = true;
MThreadPool::globalInstance()->start(
new RebuildSaver(decoder, pmap_first, myFramesPlayed),
"RebuildSaver");
RebuildSaver::Wait(decoder);
return true;
}