FrameQueue.cpp
4.39 KB
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
/*
* Copyright 1993-2015 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 <assert.h>
#define ENABLE_DEBUG_OUT 0
#if ENABLE_DEBUG_OUT
#include <iostream>
#define dbgprintf(x) printf x
#else
#define dbgprintf(x)
#endif
FrameQueue::FrameQueue():
nReadPosition_(0)
, nFramesInQueue_(0)
, bEndOfDecode_(0)
{
//#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
InitializeCriticalSection(&oCriticalSection_);
//#endif
memset(aDisplayQueue_, 0, cnMaximumSize * sizeof(CUVIDPARSERDISPINFO));
memset((void *)aIsFrameInUse_, 0, cnMaximumSize * sizeof(int));
}
#include <stdio.h>
FrameQueue::~FrameQueue()
{
if ( nFramesInQueue_ > 0 )
{
printf( "%d\n", nFramesInQueue_ );
}
//#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
DeleteCriticalSection(&oCriticalSection_);
//#endif
}
void
FrameQueue::enter_CS(CRITICAL_SECTION *pCS)
{
//#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
EnterCriticalSection(pCS);
//#endif
}
void
FrameQueue::leave_CS(CRITICAL_SECTION *pCS)
{
//#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
LeaveCriticalSection(pCS);
//#endif
}
/*
void
FrameQueue::set_event(HANDLE event)
{
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
SetEvent(event);
#endif
}
void
FrameQueue::reset_event(HANDLE event)
{
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
ResetEvent(event);
#endif
}
*/
void
FrameQueue::enqueue(const CUVIDPARSERDISPINFO *pPicParams)
{
// Mark the frame as 'in-use' so we don't re-use it for decoding until it is no longer needed
// for display
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_);
}
// if no valid picture can be return the pic-info's picture_index will
// be -1.
bool
FrameQueue::dequeue(CUVIDPARSERDISPINFO *pDisplayInfo)
{
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
FrameQueue::releaseFrame(const CUVIDPARSERDISPINFO *pPicParams)
{
aIsFrameInUse_[pPicParams->picture_index] = false;
}
bool
FrameQueue::isInUse(int nPictureIndex)
const
{
assert(nPictureIndex >= 0);
assert(nPictureIndex < (int)cnMaximumSize);
return (0 != aIsFrameInUse_[nPictureIndex]);
}
bool
FrameQueue::isDecodeFinished()
const
{
return (nFramesInQueue_ == 0 && 0 != bEndOfDecode_);
}
void
FrameQueue::endDecode()
{
bEndOfDecode_ = true;
}
// 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 (0 != bEndOfDecode_)
{
return false;
}
}
return true;
}