forked from wjhwsh/VideoPlayer-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathES2Renderer.m
477 lines (386 loc) · 14.1 KB
/
ES2Renderer.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//
// ES2Renderer.m
// FFmpegPlayTest
//
// Created by Jack on 11/2/12.
// Copyright (c) 2012 Jack. All rights reserved.
//
#import "ES2Renderer.h"
#import <OpenGLES/ES2/gl.h>
#import <OpenGLES/ES2/glext.h>
#define kGLShaderNameRGB @"rgb_render"
#define kGLShaderNameYUV @"yuv_render"
#define kGLShaderAttributePosition @"position"
#define kGLShaderAttributeTexCoords @"texCoords"
#define kGLShaderUniformSampler0 @"sampler0"
#define kGLShaderUniformSampler1 @"sampler1"
#define kGLShaderUniformSampler2 @"sampler2"
#define kGLShaderUniformMvp @"viewProjectionMatrix"
#pragma mark -
@implementation ES2Renderer
//------------------------------------------------------------------------------
- (id) init
{
self = [super init];
if (self) {
context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if (!context || ![EAGLContext setCurrentContext:context]) {
self = nil;
return self;
}
glGenFramebuffers(1, &defaultFrameBuffer);
glGenRenderbuffers(1, &colorRenderBuffer);
/*
glGenRenderbuffers(1, &depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER,
GL_DEPTH_COMPONENT16,
backingWidth,
backingHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,
GL_DEPTH_ATTACHMENT,
GL_RENDERBUFFER,
depthBuffer);
*/
glBindFramebuffer(GL_FRAMEBUFFER, defaultFrameBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderBuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_RENDERBUFFER,
colorRenderBuffer);
/// We dont need to check framebuffer completeness here because we havent bind
/// colorbUffer to backingStorage (an EADrawable obj) yet.
NSLog(@"ES2Renderer created");
}
return self;
}
//------------------------------------------------------------------------------
- (void) dealloc
{
if (defaultFrameBuffer) {
glDeleteFramebuffers(1, &defaultFrameBuffer);
defaultFrameBuffer = 0;
}
if (colorRenderBuffer) {
glDeleteRenderbuffers(1, &colorRenderBuffer);
colorRenderBuffer = 0;
}
if(frameTextures) {
glDeleteTextures(3, frameTextures);
}
// if(shader) {
// [shader release];
// }
shader = nil;
if ([EAGLContext currentContext] == context) {
[EAGLContext setCurrentContext:nil];
}
context = nil;
[super dealloc];
}
#pragma mark Utilities
- (NSString*) shaderNameOfPixelType: (int) pixelType
{
/// [Jack] test direct render YUV
return kGLShaderNameYUV;
// return kGLShaderNameRGB;
/// TODO: detect appropriate shader base on pixeltype
// if ( /* it was rgb format*/) {
// // return RGB
// }
}
//------------------------------------------------------------------------------
- (BOOL) setupTextureRGBWidth: (int) texW height: (int) texH
{
if (!shader) {
shader = [[GLShader alloc] initWithFileName: kGLShaderNameRGB
attributes:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:0], kGLShaderAttributePosition,
[NSNumber numberWithInt:1], kGLShaderAttributeTexCoords, nil]
uniforms:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:0], kGLShaderUniformSampler0,
[NSNumber numberWithInt:0], kGLShaderUniformMvp,nil]];
}
if (!shader) {
return NO;
}
/// Create texture
if(frameTextures[0])
glDeleteTextures(1, &frameTextures[0]);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &frameTextures[0]);
glBindTexture(GL_TEXTURE_2D, frameTextures[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
// Create texture space, the videop pictures will be rendered as subtexture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texW, texH, 0, GL_RGB,
GL_UNSIGNED_SHORT_5_6_5, NULL);
glUseProgram(shader.program);
glUniformMatrix4fv([shader getUniform:kGLShaderUniformMvp], 1, FALSE, (GLfloat*)&mvp.m[0]);
glUniform1i([shader getUniform:kGLShaderUniformSampler0], 0);
glVertexAttribPointer([shader getAttribute:kGLShaderAttributePosition], 2, GL_FLOAT, 0, 0, verts);
glEnableVertexAttribArray([shader getAttribute:kGLShaderAttributePosition]);
glVertexAttribPointer([shader getAttribute:kGLShaderAttributeTexCoords], 2, GL_FLOAT, 0, 0, texCoords);
glEnableVertexAttribArray([shader getAttribute:kGLShaderAttributeTexCoords]);
return YES;
}
//------------------------------------------------------------------------------
- (void) renderRGBPicture: (VideoPicture*) picture
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, frameTextures[0]);
// OpenGL loads textures lazily so accessing the buffer is deferred until
// draw; notify the movie player that we're done with the texture after glDrawArrays.
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
mFrameW, mFrameH,
GL_RGB,
[_videoSource pixelFormat],
[picture pdata]);
}
//------------------------------------------------------------------------------
- (BOOL) setupTextureYUVWidth: (int) texW height: (int) texH
{
if (!shader) {
shader = [[GLShader alloc] initWithFileName: kGLShaderNameYUV
attributes:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:0], kGLShaderAttributePosition,
[NSNumber numberWithInt:1], kGLShaderAttributeTexCoords, nil]
uniforms:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:0], kGLShaderUniformSampler0,
[NSNumber numberWithInt:0], kGLShaderUniformSampler1,
[NSNumber numberWithInt:0], kGLShaderUniformSampler2,
[NSNumber numberWithInt:0], kGLShaderUniformMvp,nil]];
}
NSUInteger widths[3] = {texW, texW/2, texW/2};
NSUInteger heights[3] = {texH, texH/2, texH/2};
for (int i=0; i < 3; ++i) {
if (frameTextures[i]) glDeleteTextures(1, &frameTextures[i]);
glGenTextures(1, &frameTextures[i]);
glBindTexture(GL_TEXTURE_2D, frameTextures[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// This is necessary for non-power-of-two textures
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_LUMINANCE,
widths[i],
heights[i],
0,
GL_LUMINANCE,
GL_UNSIGNED_BYTE,
NULL);
}
glUseProgram(shader.program);
glUniformMatrix4fv([shader getUniform:kGLShaderUniformMvp], 1, FALSE, (GLfloat*)&mvp.m[0]);
glUniform1i([shader getUniform:kGLShaderUniformSampler0], 0);
glUniform1i([shader getUniform:kGLShaderUniformSampler1], 1);
glUniform1i([shader getUniform:kGLShaderUniformSampler2], 2);
glVertexAttribPointer([shader getAttribute:kGLShaderAttributePosition], 2, GL_FLOAT, 0, 0, verts);
glEnableVertexAttribArray([shader getAttribute:kGLShaderAttributePosition]);
glVertexAttribPointer([shader getAttribute:kGLShaderAttributeTexCoords], 2, GL_FLOAT, 0, 0, texCoords);
glEnableVertexAttribArray([shader getAttribute:kGLShaderAttributeTexCoords]);
return TRUE;
}
//------------------------------------------------------------------------------
- (void) renderYUVPicture: (VideoPicture*) picture
{
// Y data
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,frameTextures[0]);
glTexSubImage2D(
GL_TEXTURE_2D,
0,
0,
0,
mFrameW, // source width
mFrameH, // source height
GL_LUMINANCE,
GL_UNSIGNED_BYTE,
[picture yData]);
// U data
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, frameTextures[1]);
glTexSubImage2D(
GL_TEXTURE_2D,
0,
0,
0,
mFrameW / 2, // source width
mFrameH / 2, // source height
GL_LUMINANCE,
GL_UNSIGNED_BYTE,
[picture uData]);
// V data
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, frameTextures[2]);
glTexSubImage2D(
GL_TEXTURE_2D,
0,
0,
0,
mFrameW / 2, // source width
mFrameH / 2, // source height
GL_LUMINANCE,
GL_UNSIGNED_BYTE,
[picture vData]);
}
//------------------------------------------------------------------------------
//- (BOOL) resizeRenderOutput:(id<EAGLDrawable>) layer
//{
// // Allocate color buffer backing based on the current layer size
// glBindRenderbuffer(GL_RENDERBUFFER, colorRenderBuffer);
// [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:layer];
// glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
// glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);
//
// if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
// {
// NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
// return FALSE;
// }
// return YES;
//}
//------------------------------------------------------------------------------
#pragma mark Renderer Protocol
@synthesize delegate = _delegate;
@synthesize videoScreen = _videoScreen;
@synthesize videoSource = _videoSource;
#ifndef next_powerof2
#define next_powerof2(x) \
x--;\
x |= x >> 1;\
x |= x >> 2;\
x |= x >> 4;\
x |= x >> 8;\
x |= x >> 16;\
x++;
#endif // !next_powerof2
- (BOOL) prepareTexture
{
if (!_videoSource || !_videoScreen) {
return FALSE;
}
// Make videoScreen as output
/// [Jack] This part should be separated from prepareTexture method because we dont need
/// to re-bind output if only video source changes.
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderBuffer);
[context renderbufferStorage:GL_RENDERBUFFER fromDrawable:[_videoScreen viewPort]];
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
return FALSE;
}
/// TODO: validate video source frame size and video screen frame size
/// before any calculation (prevent devide-by-zero)
/// TODO: take scaling mode into account for texture calculation
int scalingMode = [_videoScreen scalingMode];
int pixelType = [_videoSource pixelFormat];
int texW, texH, frameW, frameH;
texW = [_videoSource videoFrameSize].width;
texH = [_videoSource videoFrameSize].height;
frameW = texW;
frameH = texH;
// Adjust size for texture to be "Power Of Two"
next_powerof2(texH);
next_powerof2(texW);
float videoAspect = (float) frameW / (float) frameH;
float backHeight = backingHeight;
float backWidth = backingWidth;
float screenAspect = backWidth / backHeight;
float minX=-1.f, minY=-1.f, maxX=1.f, maxY=1.f;
float scale;
//This calculate the scaling for AspectFit
if(videoAspect >= screenAspect)
{
// Aspect ratio will retain width.
scale = (float)backWidth / (float) frameW;
maxY = ((float)frameH * scale) / (float) backHeight ;
minY = -maxY;
}
else
{
// Retain height.
scale = (float) backHeight / (float) frameW;
maxX = ((float) frameW * scale) / (float) backWidth;
minX = -maxX;
}
verts[0] = minX;
verts[1] = minY;
verts[2] = maxX;
verts[3] = minY;
verts[4] = minX;
verts[5] = maxY;
verts[6] = maxX;
verts[7] = maxY;
float s = (float) frameW / (float) texW;
float t = (float) frameH / (float) texH;
texCoords[0] = s;
texCoords[1] = 0.f;
texCoords[2] = 0.f;
texCoords[3] = 0.f;
texCoords[4] = s;
texCoords[5] = t;
texCoords[6] = 0;
texCoords[7] = t;
mFrameH = frameH;
mFrameW = frameW;
mTexH = texH;
mTexW = texW;
maxS = s;
maxT = t;
matSetPerspective(&proj, -1, 1, 1,-1, -1, 1);
// Just supporting one rotation direction, landscape left. Rotate Z by 90 degrees.
// matSetRotZ(&rot,M_PI_2);
matSetRotZ(&rot,M_PI);
matMul(&mvp, &rot, &proj);
NSString* shaderName = [self shaderNameOfPixelType:pixelType];
if ([shaderName isEqualToString:kGLShaderNameRGB]) {
[self setupTextureRGBWidth:texW height:texH];
}else{
[self setupTextureYUVWidth:texW height:texH];
}
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
return NO;
}
return YES;
}
//------------------------------------------------------------------------------
- (BOOL) prepareTextureForVideoForSource: (id<VideoScreenSource>) videoSource
screen: (id<VideoScreen>)viewScreen
{
[self setVideoSource:videoSource];
[self setVideoScreen:viewScreen];
return [self prepareTexture];
}
//------------------------------------------------------------------------------
- (void) render:(VideoPicture *)picture
{
// TODO: need to check if render is correctly settup
[EAGLContext setCurrentContext:context];
glBindFramebuffer(GL_FRAMEBUFFER, defaultFrameBuffer);
glViewport(0, 0, backingWidth, backingHeight);
glClearColor(0.3f, 0.2f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if ([[self shaderNameOfPixelType:[_videoSource pixelFormat]] isEqualToString:kGLShaderNameYUV]) {
[self renderYUVPicture: (VideoPicture*) picture];
}else {
[self renderRGBPicture: (VideoPicture*) picture];
}
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
if ([_delegate respondsToSelector:@selector(finishFrameByRenderer:)]) {
[_delegate finishFrameByRenderer:self];
}
glBindRenderbuffer(GL_RENDERBUFFER, colorRenderBuffer);
[context presentRenderbuffer:GL_RENDERBUFFER];
}
@end