forked from samiamwork/Movist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMovieLayer_FFMPEG.m
202 lines (176 loc) · 4.75 KB
/
MMovieLayer_FFMPEG.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
//
// MMovieLayer_FFMPEG.m
// Movist
//
// Created by Nur Monson on 7/29/12.
//
//
#import "MMovieLayer_FFMPEG.h"
#import "MMovie_FFMPEG.h"
#import <OpenGL/gl.h>
@interface MMovieLayer_FFMPEG ()
- (void)configure;
- (CVReturn)updateImage:(const CVTimeStamp*)timeStamp;
@end
@implementation MMovieLayer_FFMPEG
- (id)init
{
if((self = [super init]))
{
[self setOpaque:YES];
_configured = NO;
CGColorRef blue = CGColorCreateGenericRGB(0.0, 0.5, 1.0, 1.0);
self.backgroundColor = blue;
CGColorRelease(blue);
self.asynchronous = NO;
[self setNeedsDisplayOnBoundsChange:YES];
}
return self;
}
- (void)dealloc
{
CVOpenGLTextureRelease(_image);
[_ciContext release];
[self setMovie:nil];
[super dealloc];
}
- (void)setMovie:(MMovie_FFmpeg*)newMovie
{
if(newMovie == _movie)
return;
[[NSNotificationCenter defaultCenter] removeObserver:self name:MMovieCurrentTimeNotification object:_movie];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MMovieRateChangeNotification object:_movie];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[_movie release];
_movie = [newMovie retain];
if(_movie != nil)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(currentTimeChanged:) name:MMovieCurrentTimeNotification object:_movie];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackRateChanged:) name:MMovieRateChangeNotification object:_movie];
}
if([_movie rate] > 0.0)
{
self.asynchronous = YES;
}
else
{
self.asynchronous = NO;
}
_movieNeedsGLContext = YES;
}
- (MMovie*)movie
{
return _movie;
}
- (void)playbackRateChanged:(NSNotification*)theNotification
{
if([_movie rate] == 0.0)
{
dispatch_async(dispatch_get_main_queue(), ^{
self.asynchronous = NO;
});
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
self.asynchronous = YES;
});
}
}
- (void)currentTimeChanged:(NSNotification*)theNotification
{
if([_movie rate] == 0.0)
{
dispatch_async(dispatch_get_main_queue(), ^{
[self setNeedsDisplay];
});
}
}
- (void)reshape
{
// //TRACE(@"%s", __PRETTY_FUNCTION__);
// [_drawLock lock];
//
// NSRect bounds = [self bounds];
// glViewport(0, 0, bounds.size.width, bounds.size.height);
//
// glMatrixMode(GL_PROJECTION);
// glLoadIdentity();
// glOrtho(0, bounds.size.width, 0, bounds.size.height, -1.0f, 1.0f);
//
// glMatrixMode(GL_MODELVIEW);
// glLoadIdentity();
//
// [_drawLock unlock];
}
- (CGDirectDisplayID)currentDisplayID
{
NSDictionary* deviceDesc = [[self.view.window screen] deviceDescription];
NSNumber* screenNumber = [deviceDesc objectForKey:@"NSScreenNumber"];
return (CGDirectDisplayID)[screenNumber intValue];
}
- (void)configure
{
// TODO: put this in copyCGLContextForPixelFormat:
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
(id)colorSpace, kCIContextOutputColorSpace,
(id)colorSpace, kCIContextWorkingColorSpace, nil];
_ciContext = [[CIContext contextWithCGLContext:[self.openGLContext CGLContextObj]
pixelFormat:[self.openGLPixelFormat CGLPixelFormatObj]
colorSpace:colorSpace
options:dict] retain];
CGColorSpaceRelease(colorSpace);
//GLint swapInterval = 1;
//[[self openGLContext] setValues:&swapInterval forParameter:NSOpenGLCPSwapInterval];
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
_configured = YES;
}
- (CVReturn)updateImage:(const CVTimeStamp*)timeStamp
{
// if([_drawLock tryLock])
{
CVOpenGLTextureRef image = [_movie nextImage:timeStamp];
if(image)
{
CVOpenGLTextureRelease(_image);
_image = image;
}
// [_drawLock unlock];
}
return kCVReturnSuccess;
}
- (void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
{
// [_drawLock lock];
if(!_configured)
{
[self configure];
}
if(_movieNeedsGLContext)
{
NSError* error;
if(![_movie setOpenGLContext:self.openGLContext pixelFormat:self.openGLPixelFormat error:&error])
{
// TODO: return an bool FALSE and the NSError object
NSLog(@"error: %@", error);
}
_movieNeedsGLContext = NO;
}
// since we're async and not using displaylink callbacks
[self updateImage:timeStamp];
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, self.bounds.size.width, 0, self.bounds.size.height, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if(_image)
{
CIImage* img = [CIImage imageWithCVImageBuffer:_image];
[_ciContext drawImage:img inRect:self.bounds fromRect:CVImageBufferGetCleanRect(_image)];
}
// [[self openGLContext] flushBuffer];
// [_drawLock unlock];
}
@end