-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkxPrioFrame.cpp
278 lines (257 loc) · 8.2 KB
/
mkxPrioFrame.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
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
/****************************************************************************
** libmatroska : parse Matroska files, see http://www.matroska.org/
**
** <file/class description>
**
** Copyright (C) 2002-2003 Steve Lhomme. All rights reserved.
**
** This file may be distributed under the terms of the Q Public License
** as defined by Trolltech AS of Norway and appearing in the file
** LICENSE.QPL included in the packaging of this file.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** Licensees holding an other license may use this file in accordance with
** the Agreement provided with the Software.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.matroska.org/license/qpl/ for QPL licensing information.
** See http://www.matroska.org/license/gpl/ for GPL licensing information.
**
** Contact [email protected] if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
/*!
\file
\version \$Id: mkxPrioFrame.cpp,v 1.8 2003/08/01 21:15:47 robux4 Exp $
\author Steve Lhomme <robux4 @ users.sf.net>
*/
/*=====================================================================
If we have in input the data in coding order from matroska, we need
to be able to tell which frame is next in the stream.
In input we may have IBP frames in that order :
I1 P2 P5 B3 B4 P6 I7
In output we need to be able to give the frames in this order :
I1 P2 B3 B4 P5 P6 I7
======================================================================*/
#include <streams.h>
#include "mkxPrioFrame.h"
MkxPrioCache::MkxPrioCache(UINT aMinCache, UINT aMaxWriteThreshold,
UINT aMaxReadThreshold, UINT aMinWriteThreshold)
:MinCache(aMinCache)
,MinWriteThreshold(aMaxWriteThreshold)
,MaxWriteThreshold(aMaxReadThreshold)
,MaxReadThreshold(aMinWriteThreshold)
,Ending(FALSE)
,Flushing(FALSE)
,mWriteUnBlock(TRUE)
,bWriteCanBlock(TRUE)
,mReadUnBlock(TRUE)
{
}
MkxPrioCache::~MkxPrioCache()
{
}
void MkxPrioCache::SetMaxReadThreshold(UINT Threshold)
{
Lock();
MaxReadThreshold = Threshold;
Unlock();
}
void MkxPrioCache::SetMaxWriteThreshold(UINT Threshold)
{
Lock();
MaxWriteThreshold = Threshold;
Unlock();
}
void MkxPrioCache::SetMinWriteThreshold(UINT Threshold)
{
Lock();
MinWriteThreshold = Threshold;
Unlock();
}
void MkxPrioCache::DisableWriteBlock()
{
NOTE1("MkxPrioCache::WriteBlock Disable (0x%08x)", this);
bWriteCanBlock = FALSE;
mWriteUnBlock.Set();
}
void MkxPrioCache::EnableWriteBlock()
{
NOTE1("MkxPrioCache::WriteBlock Enable (0x%08x)", this);
bWriteCanBlock = TRUE;
}
void MkxPrioCache::Reset()
{
NOTE("MkxPrioCache::Reset");
mWriteUnBlock.Reset();
mReadUnBlock.Reset();
Ending = FALSE;
Flushing = FALSE;
NOTE1("MkxPrioCache::WriteBlock Reset (0x%08x)", this);
bWriteCanBlock = TRUE;
}
void MkxPrioCache::EndOfStream()
{
NOTE("MkxPrioCache::EndOfStream");
Ending = TRUE;
mReadUnBlock.Set();
}
void MkxPrioCache::SetPauseMode(bool bPauseMode)
{
NOTE1("MkxPrioCache::SetPauseMode %d", bPauseMode);
}
void MkxPrioCache::PushBlock(KaxBlockGroup & aBlock)
{
///< \todo delete an element that has been "displayed"
MkxPrioBuffer & EltToAdd = *(new MkxPrioBuffer(aBlock));
MkxPrioKey & EltKey = *(new MkxPrioKey(EltToAdd));
Lock();
while (MaxWriteThreshold <= DisplayOrderList.size() && bWriteCanBlock) {
mWriteUnBlock.Reset();
NOTE1("MkxPrioCache::PushBlock blocking (0x%08x)", this);
Unlock();
mWriteUnBlock.Wait();
Lock();
NOTE1("MkxPrioCache::PushBlock UNblocking WRITE (0x%08x)", this);
}
DisplayOrderList.insert( std::make_pair(EltKey, &EltToAdd) );
CodingOrderList.push_back( &EltToAdd );
NOTE4("MkxPrioCache::PushBlock (0x%08x) 0x%08x (%d/%d)", this, &aBlock, DisplayOrderList.size(), MaxWriteThreshold);
if (DisplayOrderList.size() >= MaxReadThreshold && !mReadUnBlock.Check()) {
NOTE1("MkxPrioCache::PushBlock UNblocking READ (0x%08x)", this);
mReadUnBlock.Set(); // unblock the reader
}
Unlock();
}
KaxBlockGroup * MkxPrioCache::GetFrontBlock()
{
KaxBlockGroup * result = NULL;
Lock();
NOTE1("MkxPrioCache::GetFrontBlock (0x%08x)", this);
while (result == NULL) {
for (size_t i=0;i<CodingOrderList.size(); i++)
{
MkxPrioBuffer *tst = CodingOrderList[i];
// find the first frame not displayed in coding order
if (!CodingOrderList[i]->Displayed) {
// CodingOrderList[i].Displayed = true;
result = CodingOrderList[i]->Block;
CodingOrderList[i]->CanDelete = false;
NOTE1("MkxPrioCache::GetFrontBlock CanDelete NOT 0x%08x", result);
break;
}
}
if (Ending || Flushing)
break; // leave the while
if (result == NULL) {
// block reading until something occurs
mReadUnBlock.Reset();
NOTE1("MkxPrioCache::GetFrontBlock blocked (0x%08x)", this);
Unlock();
mReadUnBlock.Wait();
NOTE1("MkxPrioCache::GetFrontBlock UNblocked (0x%08x) AAA", this);
Lock();
NOTE1("MkxPrioCache::GetFrontBlock UNblocked (0x%08x)", this);
}
if (Ending || Flushing)
break; // leave the while
}
NOTE1("MkxPrioCache::GetFrontBlock 0x%08x", result);
Unlock();
return result;
}
KaxBlockGroup * MkxPrioCache::GetNextInTimeOrder(KaxBlockGroup & aBlock)
{
KaxBlockGroup * result = NULL;
// find the right buffer to output
MkxPrioBuffer TmpBuf(aBlock);
MkxPrioKey TmpKey(TmpBuf);
Lock();
std::map<MkxPrioKey, MkxPrioBuffer*>::iterator & TheElt = DisplayOrderList.find( TmpKey );
if (!Flushing && !Ending && TheElt != DisplayOrderList.end()) {
TheElt++;
if (TheElt != DisplayOrderList.end()) {
result = (*TheElt).second->Block;
NOTE1("MkxPrioCache::GetNextInTimeOrder found 0x%08x", result);
}
}
Unlock();
return result;
}
void MkxPrioCache::UsedBlock(KaxBlockGroup & aBlock)
{
Lock();
MkxPrioBuffer TmpBuf(aBlock);
MkxPrioKey TmpKey(TmpBuf);
std::map<MkxPrioKey, MkxPrioBuffer*>::iterator & TheElt = DisplayOrderList.find( TmpKey );
if (TheElt != DisplayOrderList.end()) {
MkxPrioBuffer *tst = (*TheElt).second;
tst->CanDelete = true;
NOTE1("MkxPrioCache::PushBlock CanDelete (0x%08x)", (*TheElt).second->Block);
(*TheElt).second->Displayed = true;
KaxBlock & tmpBlk = GetChild<KaxBlock>(*(*TheElt).second->Block);
tmpBlk.ReleaseFrames();
StripList();
// Unblock writing if needed+possible
if (MinWriteThreshold >= DisplayOrderList.size() && !mWriteUnBlock.Check()) {
NOTE1("MkxPrioCache::PushBlock Unblocking (0x%08x)", this);
mWriteUnBlock.Set();
}
}
Unlock();
}
void MkxPrioCache::StartFlush()
{
Lock();
NOTE2("MkxPrioCache::StartFlush (0x%08x) start %d", this, DisplayOrderList.size());
Flushing = TRUE;
mReadUnBlock.Set(); // in case it's blocked, deblock it
Unlock();
}
void MkxPrioCache::EndFlush()
{
Lock();
NOTE2("MkxPrioCache::EndFlush (0x%08x) start %d", this, DisplayOrderList.size());
std::map<MkxPrioKey, MkxPrioBuffer*>::iterator & TheElt = DisplayOrderList.begin();
for (; TheElt != DisplayOrderList.end(); TheElt++) {
if ((*TheElt).second->CanDelete) {
KaxBlockGroup *tmp = (*TheElt).second->Block;
//(*TheElt).second->Block = NULL;
//NOTE1("MkxPrioCache::Flush delete 0x%08x", tmp);
delete tmp;
}
//NOTE("MkxPrioCache::Flush delete done");
}
DisplayOrderList.clear();
CodingOrderList.clear();
NOTE2("MkxPrioCache::EndFlush (0x%08x) done %d", this, DisplayOrderList.size());
Unlock();
}
/*!
\todo handle MinCache & ReferencePriorities (for forward frames)
*/
void MkxPrioCache::StripList()
{
NOTE2("MkxPrioCache::StripList %d/%d", DisplayOrderList.size(), CodingOrderList.size());
// only delete elements that are displayed and not "needed in the cache" according to the MinCache
std::map<MkxPrioKey, MkxPrioBuffer*>::iterator TheElt = DisplayOrderList.begin();
for (; TheElt != DisplayOrderList.end(); ) {
if ((*TheElt).second->Displayed && (*TheElt).second->CanDelete) {
KaxBlockGroup *tmp = (*TheElt).second->Block;
//(*TheElt).second->Block = NULL;
NOTE1("MkxPrioCache::StripList delete 0x%08x", tmp);
TheElt = DisplayOrderList.erase(TheElt);
delete tmp;
} else {
TheElt++;
}
}
NOTE1("MkxPrioCache::StripList done %d", DisplayOrderList.size());
}