-
Notifications
You must be signed in to change notification settings - Fork 43
/
ECVDependentVideoStorage.m
executable file
·287 lines (231 loc) · 6.88 KB
/
ECVDependentVideoStorage.m
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
279
280
281
282
283
284
285
286
287
/* Copyright (c) 2009-2010, Ben Trask
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY BEN TRASK ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL BEN TRASK BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
#import "ECVDependentVideoStorage.h"
// Models
#import "ECVVideoFormat.h"
// Other Sources
#import "ECVReadWriteLock.h"
#define ECVDependentBufferCount 16
@interface ECVDependentPixelBuffer : ECVMutablePixelBuffer
{
@private
ECVDependentVideoStorage *_videoStorage;
NSUInteger _bufferIndex;
}
- (id)initWithVideoStorage:(ECVDependentVideoStorage *)storage bufferIndex:(NSUInteger)i;
- (NSUInteger)bufferIndex;
@end
@interface ECVDependentVideoFrame : ECVVideoFrame
{
@private
ECVReadWriteLock *_lock;
NSUInteger _bufferIndex;
}
- (id)initWithVideoStorage:(ECVVideoStorage *)storage bufferIndex:(NSUInteger)i;
- (void)removeFromStorageIfPossible;
@end
@interface ECVDependentVideoFrame(Private)
- (BOOL)_removeFrame:(ECVVideoFrame *)frame;
@end
@implementation ECVDependentVideoStorage
#pragma mark -ECVDependentVideoStorage
- (NSUInteger)numberOfBuffers
{
return _numberOfBuffers;
}
- (void *)allBufferBytes
{
return [_allBufferData mutableBytes];
}
- (void *)bytesAtIndex:(NSUInteger)i
{
return [_allBufferData mutableBytes] + [self bufferSize] * i;
}
#pragma mark -ECVDependentVideoFrame(Private)
- (BOOL)_removeFrame:(ECVVideoFrame *)frame
{
if(!frame) return NO;
[self lock];
NSUInteger const i = [_frames indexOfObjectIdenticalTo:frame];
BOOL drop = NSNotFound != i;
if(drop) {
[[frame retain] autorelease];
[_unusedBufferIndexes addIndex:[frame bufferIndex]];
[_frames removeObjectAtIndex:i];
}
[self unlock];
return drop;
}
#pragma mark -ECVVideoStorage
- (id)initWithVideoFormat:(ECVVideoFormat *const)videoFormat deinterlacingMode:(Class const)mode pixelFormat:(OSType const)pixelFormat
{
if((self = [super initWithVideoFormat:videoFormat deinterlacingMode:mode pixelFormat:pixelFormat])) {
_frames = [[NSMutableArray alloc] initWithCapacity:ECVDependentBufferCount];
_numberOfBuffers = ECVDependentBufferCount;
_allBufferData = [[NSMutableData alloc] initWithLength:_numberOfBuffers * [self bufferSize]];
_unusedBufferIndexes = [[NSMutableIndexSet alloc] initWithIndexesInRange:NSMakeRange(0, _numberOfBuffers)];
}
return self;
}
#pragma mark -ECVVideoStorage(ECVAbstract)
- (ECVVideoFrame *)currentFrame
{
[self lock];
ECVVideoFrame *const frame = [[[_frames lastObject] retain] autorelease];
[self unlock];
return frame;
}
#pragma mark -
- (ECVMutablePixelBuffer *)nextBuffer
{
NSUInteger i = [_unusedBufferIndexes firstIndex];
if(NSNotFound == i) {
NSUInteger const frameCount = [_frames count];
NSUInteger const frameGroupSize = [[self videoFormat] frameGroupSize];
NSUInteger const framesToKeep = ((frameCount - 1) % frameGroupSize) + 1;
[[_frames subarrayWithRange:NSMakeRange(0, frameCount - framesToKeep)] makeObjectsPerformSelector:@selector(removeFromStorageIfPossible)];
i = [_unusedBufferIndexes firstIndex];
if(NSNotFound == i) return nil;
}
[_unusedBufferIndexes removeIndex:i];
return [[[ECVDependentPixelBuffer alloc] initWithVideoStorage:self bufferIndex:i] autorelease];
}
- (ECVVideoFrame *)finishedFrameWithFinishedBuffer:(id)buffer
{
ECVVideoFrame *const frame = [[[ECVDependentVideoFrame alloc] initWithVideoStorage:self bufferIndex:[buffer bufferIndex]] autorelease];
[_frames addObject:frame];
return frame;
}
#pragma mark -
- (void)empty
{
// We probably don't need to worry about the frames themselves because this should only happen while paused.
[_unusedBufferIndexes release];
_unusedBufferIndexes = [[NSMutableIndexSet alloc] initWithIndexesInRange:NSMakeRange(0, _numberOfBuffers)];
}
#pragma mark -NSObject
- (void)dealloc
{
[_frames release];
[_allBufferData release];
[_unusedBufferIndexes release];
[super dealloc];
}
@end
@implementation ECVDependentPixelBuffer
#pragma mark -ECVDependentPixelBuffer
- (id)initWithVideoStorage:(ECVDependentVideoStorage *)storage bufferIndex:(NSUInteger)i
{
if((self = [super init])) {
_videoStorage = storage;
_bufferIndex = i;
}
return self;
}
- (NSUInteger)bufferIndex
{
return _bufferIndex;
}
#pragma mark -ECVMutablePixelBuffer(ECVAbstract)
- (void *)mutableBytes
{
return [_videoStorage bytesAtIndex:_bufferIndex];
}
#pragma mark -ECVPixelBuffer(ECVAbstract)
- (ECVIntegerSize)pixelSize
{
return [[_videoStorage videoFormat] frameSize];
}
- (size_t)bytesPerRow
{
return [_videoStorage bytesPerRow];
}
- (OSType)pixelFormat
{
return [_videoStorage pixelFormat];
}
#pragma mark -
- (void const *)bytes
{
return [_videoStorage bytesAtIndex:_bufferIndex];
}
- (NSRange)validRange
{
return NSMakeRange(0, [_videoStorage bufferSize]);
}
#pragma mark -ECVPixelBuffer(ECVAbstract) <NSLocking>
- (void)lock{}
- (void)unlock{}
@end
@implementation ECVDependentVideoFrame
#pragma mark -ECVDependentVideoFrame
- (id)initWithVideoStorage:(ECVVideoStorage *)storage bufferIndex:(NSUInteger)i
{
if((self = [super initWithVideoStorage:storage])) {
_lock = [[ECVReadWriteLock alloc] init];
_bufferIndex = i;
}
return self;
}
- (void)removeFromStorageIfPossible
{
NSAssert([self hasBytes], @"Frame not in storage to begin with.");
if(![_lock tryWriteLock]) return;
if([[self videoStorage] _removeFrame:self]) _bufferIndex = NSNotFound;
[_lock unlock];
}
#pragma mark -ECVVideoFrame(ECVAbstract)
- (void const *)bytes
{
return [[self videoStorage] bytesAtIndex:_bufferIndex];
}
#pragma mark -
- (BOOL)hasBytes
{
return NSNotFound != _bufferIndex;
}
- (BOOL)lockIfHasBytes
{
[self lock];
if([self hasBytes]) return YES;
[self unlock];
return NO;
}
#pragma mark -ECVVideoFrame(ECVAbstract) <NSLocking>
- (void)lock
{
[_lock readLock];
}
- (void)unlock
{
[_lock unlock];
}
#pragma mark -ECVVideoFrame(ECVDependentVideoFrame)
- (NSUInteger)bufferIndex
{
return _bufferIndex;
}
#pragma mark -NSObject
- (void)dealloc
{
[_lock release];
[super dealloc];
}
@end