1
1
/*
2
- * Copyright (c) 2010, 2014 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2010, 2023 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
23
23
* questions.
24
24
*/
25
25
26
+ // For UINT_MAX, we cannot use GLib here, since it is shared code between
27
+ // GStreamer and AVFoundation.
28
+ #include < limits.h>
29
+
26
30
#include " VideoFrame.h"
27
31
#include < Common/VSMemory.h>
28
32
29
33
// *************************************************************************************************
30
34
// ********** class CVideoFrame
31
35
// *************************************************************************************************
32
36
CVideoFrame::CVideoFrame ()
33
- : m_iWidth (0 ),
34
- m_iHeight (0 ),
35
- m_iEncodedWidth (0 ),
36
- m_iEncodedHeight (0 ),
37
+ : m_uiWidth (0 ),
38
+ m_uiHeight (0 ),
39
+ m_uiEncodedWidth (0 ),
40
+ m_uiEncodedHeight (0 ),
37
41
m_typeFrame(UNKNOWN),
38
42
m_bHasAlpha(false ),
39
43
m_dTime(0.0 ),
40
- m_FrameDirty(false ),
41
- m_iPlaneCount(1 )
44
+ m_FrameDirty(false )
42
45
{
43
- m_piPlaneStrides[0 ] = m_piPlaneStrides[1 ] = m_piPlaneStrides[2 ] = m_piPlaneStrides[3 ] = 0 ;
44
- m_pulPlaneSize[0 ] = m_pulPlaneSize[1 ] = m_pulPlaneSize[2 ] = m_pulPlaneSize[3 ] = 0 ;
45
- m_pvPlaneData[0 ] = m_pvPlaneData[1 ] = m_pvPlaneData[2 ] = m_pvPlaneData[3 ] = NULL ;
46
+ Reset ();
46
47
}
47
48
48
49
CVideoFrame::~CVideoFrame ()
49
50
{
50
51
}
51
52
52
- int CVideoFrame::GetWidth ()
53
+ unsigned int CVideoFrame::GetWidth ()
53
54
{
54
- return m_iWidth ;
55
+ return m_uiWidth ;
55
56
}
56
57
57
- int CVideoFrame::GetHeight ()
58
+ unsigned int CVideoFrame::GetHeight ()
58
59
{
59
- return m_iHeight ;
60
+ return m_uiHeight ;
60
61
}
61
62
62
- int CVideoFrame::GetEncodedWidth ()
63
+ unsigned int CVideoFrame::GetEncodedWidth ()
63
64
{
64
- return m_iEncodedWidth ;
65
+ return m_uiEncodedWidth ;
65
66
}
66
67
67
- int CVideoFrame::GetEncodedHeight ()
68
+ unsigned int CVideoFrame::GetEncodedHeight ()
68
69
{
69
- return m_iEncodedHeight ;
70
+ return m_uiEncodedHeight ;
70
71
}
71
72
72
73
CVideoFrame::FrameType CVideoFrame::GetType ()
@@ -84,31 +85,41 @@ double CVideoFrame::GetTime()
84
85
return m_dTime;
85
86
}
86
87
87
- int CVideoFrame::GetPlaneCount ()
88
+ unsigned int CVideoFrame::GetPlaneCount ()
88
89
{
89
- return m_iPlaneCount;
90
+ return m_uiPlaneCount;
91
+ }
92
+
93
+ void CVideoFrame::SetPlaneCount (unsigned int count)
94
+ {
95
+ if (count <= MAX_PLANE_COUNT) {
96
+ m_uiPlaneCount = count;
97
+ } else {
98
+ // Should never happen
99
+ m_uiPlaneCount = MAX_PLANE_COUNT;
100
+ }
90
101
}
91
102
92
- void * CVideoFrame::GetDataForPlane (int planeIndex)
103
+ void * CVideoFrame::GetDataForPlane (unsigned int planeIndex)
93
104
{
94
- if (planeIndex < 4 && planeIndex >= 0 ) {
105
+ if (planeIndex < MAX_PLANE_COUNT ) {
95
106
return m_pvPlaneData[planeIndex];
96
107
}
97
108
return NULL ;
98
109
}
99
110
100
- unsigned long CVideoFrame::GetSizeForPlane (int planeIndex)
111
+ unsigned long CVideoFrame::GetSizeForPlane (unsigned int planeIndex)
101
112
{
102
- if (planeIndex < 4 && planeIndex >= 0 ) {
113
+ if (planeIndex < MAX_PLANE_COUNT ) {
103
114
return m_pulPlaneSize[planeIndex];
104
115
}
105
116
return 0 ;
106
117
}
107
118
108
- int CVideoFrame::GetStrideForPlane (int planeIndex)
119
+ unsigned int CVideoFrame::GetStrideForPlane (unsigned int planeIndex)
109
120
{
110
- if (planeIndex < 4 && planeIndex >= 0 ) {
111
- return m_piPlaneStrides [planeIndex];
121
+ if (planeIndex < MAX_PLANE_COUNT ) {
122
+ return m_puiPlaneStrides [planeIndex];
112
123
}
113
124
return 0 ;
114
125
}
@@ -118,12 +129,22 @@ CVideoFrame *CVideoFrame::ConvertToFormat(FrameType type)
118
129
return NULL ;
119
130
}
120
131
121
- void CVideoFrame::SwapPlanes (int aa, int bb)
132
+ void CVideoFrame::Reset ()
133
+ {
134
+ m_uiPlaneCount = 0 ;
135
+ for (int i = 0 ; i < MAX_PLANE_COUNT; i++) {
136
+ m_puiPlaneStrides[i] = 0 ;
137
+ m_pulPlaneSize[i] = 0 ;
138
+ m_pvPlaneData[i] = NULL ;
139
+ }
140
+ }
141
+
142
+ void CVideoFrame::SwapPlanes (unsigned int aa, unsigned int bb)
122
143
{
123
- if (aa != bb && aa >= 0 && aa < m_iPlaneCount && bb >= 0 && bb < m_iPlaneCount ) {
124
- int stride = m_piPlaneStrides [aa];
125
- m_piPlaneStrides [aa] = m_piPlaneStrides [bb];
126
- m_piPlaneStrides [bb] = stride;
144
+ if (aa != bb && aa < m_uiPlaneCount && bb < m_uiPlaneCount ) {
145
+ unsigned int stride = m_puiPlaneStrides [aa];
146
+ m_puiPlaneStrides [aa] = m_puiPlaneStrides [bb];
147
+ m_puiPlaneStrides [bb] = stride;
127
148
128
149
unsigned long size = m_pulPlaneSize[aa];
129
150
m_pulPlaneSize[aa] = m_pulPlaneSize[bb];
@@ -134,3 +155,53 @@ void CVideoFrame::SwapPlanes(int aa, int bb)
134
155
m_pvPlaneData[bb] = vptr;
135
156
}
136
157
}
158
+
159
+ unsigned long CVideoFrame::CalcSize (unsigned int a, unsigned int b, bool *pbValid)
160
+ {
161
+ if (pbValid == NULL || *(pbValid) == false ) {
162
+ return 0 ;
163
+ }
164
+
165
+ if (b > 0 && a <= (UINT_MAX / b)) {
166
+ return (a * b);
167
+ }
168
+
169
+ *(pbValid) = false ;
170
+ return 0 ;
171
+ }
172
+
173
+ unsigned long CVideoFrame::AddSize (unsigned long a, unsigned long b, bool *pbValid)
174
+ {
175
+ if (pbValid == NULL || *(pbValid) == false ) {
176
+ return 0 ;
177
+ }
178
+
179
+ // unsigned long can be 32-bit or 64-bit, make sure it is no more then UINT_MAX
180
+ if (a <= UINT_MAX && b <= UINT_MAX && a <= (UINT_MAX - b)) {
181
+ return (a + b);
182
+ }
183
+
184
+ *(pbValid) = false ;
185
+ return 0 ;
186
+ }
187
+
188
+ void * CVideoFrame::CalcPlanePointer (intptr_t baseAddress, unsigned int offset,
189
+ unsigned long planeSize, unsigned long baseSize,
190
+ bool *pbValid)
191
+ {
192
+ if (pbValid == NULL || *(pbValid) == false ) {
193
+ return NULL ;
194
+ }
195
+
196
+ // We will read planeSize bytes from baseAddress starting with offset, so
197
+ // make sure we do not read pass baseSize.
198
+ unsigned long endOfPlane = AddSize (offset, planeSize, pbValid);
199
+ if (*(pbValid)) { // Make sure AddSize() did not failed.
200
+ if (endOfPlane <= baseSize) {
201
+ return (void *)(baseAddress + offset);
202
+ }
203
+ }
204
+
205
+ *(pbValid) = false ;
206
+ return NULL ;
207
+ }
0 commit comments