-
Notifications
You must be signed in to change notification settings - Fork 1
/
FrameQueue.cpp
executable file
·197 lines (171 loc) · 4.66 KB
/
FrameQueue.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
/*
* Copyright 1993-2017 NVIDIA Corporation. All rights reserved.
*
* Please refer to the NVIDIA end user license agreement (EULA) associated
* with this source code for terms and conditions that govern your use of
* this software. Any use, reproduction, disclosure, or distribution of
* this software and related documentation outside the terms of the EULA
* is strictly prohibited.
*
*/
#include "FrameQueue.h"
#include <stdio.h>
#include <assert.h>
FrameQueue::FrameQueue(CUvideoctxlock ctxLock): hEvent_(0)
, nReadPosition_(0), nWritePosition_(0), nFramesInQueue_(0)
, bEndOfDecode_(0), m_ctxLock(ctxLock)
{
#ifdef _WIN32
hEvent_ = CreateEvent(NULL, false, false, NULL);
InitializeCriticalSection(&oCriticalSection_);
#else
pthread_mutex_init(&oCriticalSection_, NULL);
#endif
memset((void*)aIsFrameInUse_, 0, cnMaximumSize * sizeof(int));
}
FrameQueue::~FrameQueue()
{
#ifdef _WIN32
DeleteCriticalSection(&oCriticalSection_);
CloseHandle(hEvent_);
#else
pthread_mutex_destroy(&oCriticalSection_);
#endif
}
void
FrameQueue::waitForQueueUpdate()
{
#ifdef _WIN32
WaitForSingleObject(hEvent_, 10);
#endif
}
void
FrameQueue::enter_CS(CRITICAL_SECTION *pCS)
{
#ifdef _WIN32
EnterCriticalSection(pCS);
#else
pthread_mutex_lock(pCS);
#endif
}
void
FrameQueue::leave_CS(CRITICAL_SECTION *pCS)
{
#ifdef _WIN32
LeaveCriticalSection(pCS);
#else
pthread_mutex_unlock(pCS);
#endif
}
void
FrameQueue::set_event(HANDLE event)
{
#ifdef _WIN32
SetEvent(event);
#endif
}
void
FrameQueue::reset_event(HANDLE event)
{
#ifdef _WIN32
ResetEvent(event);
#endif
}
bool
FrameQueue::isInUse(int nPictureIndex)
const
{
assert(nPictureIndex >= 0);
assert(nPictureIndex < (int)cnMaximumSize);
return (0 != aIsFrameInUse_[nPictureIndex]);
}
bool
FrameQueue::isEndOfDecode()
const
{
return (0 != bEndOfDecode_);
}
void
FrameQueue::endDecode()
{
bEndOfDecode_ = true;
signalStatusChange(); // Signal for the display thread
}
// Spins until frame becomes available or decoding
// gets canceled.
// If the requested frame is available the method returns true.
// If decoding was interupted before the requested frame becomes
// available, the method returns false.
bool
FrameQueue::waitUntilFrameAvailable(int nPictureIndex)
{
while (isInUse(nPictureIndex))
{
Sleep(1); // Decoder is getting too far ahead from display
if (isEndOfDecode())
return false;
}
return true;
}
void
FrameQueue::signalStatusChange()
{
set_event(hEvent_);
}
CUVIDFrameQueue::CUVIDFrameQueue(CUvideoctxlock ctxLock): FrameQueue(ctxLock)
{
memset(aDisplayQueue_, 0, cnMaximumSize * sizeof(CUVIDPARSERDISPINFO));
}
CUVIDFrameQueue::~CUVIDFrameQueue()
{}
void
CUVIDFrameQueue::enqueue(const void * pData)
{
// Mark the frame as 'in-use' so we don't re-use it for decoding until it is no longer needed
// for display
const CUVIDPARSERDISPINFO* pPicParams = (const CUVIDPARSERDISPINFO*)(pData);
aIsFrameInUse_[pPicParams->picture_index] = true;
// Wait until we have a free entry in the display queue (should never block if we have enough entries)
do
{
bool bPlacedFrame = false;
enter_CS(&oCriticalSection_);
if (nFramesInQueue_ < (int)FrameQueue::cnMaximumSize)
{
int iWritePosition = (nReadPosition_ + nFramesInQueue_) % cnMaximumSize;
aDisplayQueue_[iWritePosition] = *pPicParams;
nFramesInQueue_++;
bPlacedFrame = true;
}
leave_CS(&oCriticalSection_);
if (bPlacedFrame) // Done
break;
Sleep(1); // Wait a bit
} while (!bEndOfDecode_);
signalStatusChange(); // Signal for the display thread
}
// if no valid picture can be return the pic-info's picture_index will
// be -1.
bool
CUVIDFrameQueue::dequeue(void* pData)
{
CUVIDPARSERDISPINFO* pDisplayInfo = (CUVIDPARSERDISPINFO*)(pData);
pDisplayInfo->picture_index = -1;
bool bHaveNewFrame = false;
enter_CS(&oCriticalSection_);
if (nFramesInQueue_ > 0)
{
int iEntry = nReadPosition_;
*pDisplayInfo = aDisplayQueue_[iEntry];
nReadPosition_ = (iEntry+1) % cnMaximumSize;
nFramesInQueue_--;
bHaveNewFrame = true;
}
leave_CS(&oCriticalSection_);
return bHaveNewFrame;
}
void
CUVIDFrameQueue::releaseFrame(const void * pPicParams) {
const CUVIDPARSERDISPINFO* pInfo = (const CUVIDPARSERDISPINFO*)(pPicParams);
aIsFrameInUse_[pInfo->picture_index] = false;
}