Skip to content

Commit

Permalink
[MovieInput]fix MovieInput decode issue
Browse files Browse the repository at this point in the history
Using "CVOpenGLESTextureCacheCreateTextureFromImage" function to
generating "luminanceTexture" and "chrominanceTexture". This could
avoid some byte-aligning issue
  • Loading branch information
lyb0307 committed Jul 27, 2017
1 parent 2683a19 commit ac3526f
Showing 1 changed file with 45 additions and 8 deletions.
53 changes: 45 additions & 8 deletions framework/Source/iOS/MovieInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,54 @@ public class MovieInput: ImageSource {

let startTime = CFAbsoluteTimeGetCurrent()

let luminanceFramebuffer = sharedImageProcessingContext.framebufferCache.requestFramebufferWithProperties(orientation:.portrait, size:GLSize(width:GLint(bufferWidth), height:GLint(bufferHeight)), textureOnly:true)
luminanceFramebuffer.lock()
var luminanceGLTexture: CVOpenGLESTexture?

glActiveTexture(GLenum(GL_TEXTURE0))
glBindTexture(GLenum(GL_TEXTURE_2D), luminanceFramebuffer.texture)
glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_LUMINANCE, GLsizei(bufferWidth), GLsizei(bufferHeight), 0, GLenum(GL_LUMINANCE), GLenum(GL_UNSIGNED_BYTE), CVPixelBufferGetBaseAddressOfPlane(movieFrame, 0))

let chrominanceFramebuffer = sharedImageProcessingContext.framebufferCache.requestFramebufferWithProperties(orientation:.portrait, size:GLSize(width:GLint(bufferWidth), height:GLint(bufferHeight)), textureOnly:true)
chrominanceFramebuffer.lock()
let luminanceGLTextureResult = CVOpenGLESTextureCacheCreateTextureFromImage(kCFAllocatorDefault, sharedImageProcessingContext.coreVideoTextureCache, movieFrame, nil, GLenum(GL_TEXTURE_2D), GL_LUMINANCE, GLsizei(bufferWidth), GLsizei(bufferHeight), GLenum(GL_LUMINANCE), GLenum(GL_UNSIGNED_BYTE), 0, &luminanceGLTexture)

assert(luminanceGLTextureResult == kCVReturnSuccess && luminanceGLTexture != nil)

let luminanceTexture = CVOpenGLESTextureGetName(luminanceGLTexture!)

glBindTexture(GLenum(GL_TEXTURE_2D), luminanceTexture)
glTexParameterf(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_WRAP_S), GLfloat(GL_CLAMP_TO_EDGE));
glTexParameterf(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_WRAP_T), GLfloat(GL_CLAMP_TO_EDGE));

let luminanceFramebuffer: Framebuffer
do {
luminanceFramebuffer = try Framebuffer(context: sharedImageProcessingContext, orientation: .portrait, size: GLSize(width:GLint(bufferWidth), height:GLint(bufferHeight)), textureOnly: true, overriddenTexture: luminanceTexture)
} catch {
fatalError("Could not create a framebuffer of the size (\(bufferWidth), \(bufferHeight)), error: \(error)")
}

luminanceFramebuffer.cache = sharedImageProcessingContext.framebufferCache
luminanceFramebuffer.lock()


var chrominanceGLTexture: CVOpenGLESTexture?

glActiveTexture(GLenum(GL_TEXTURE1))
glBindTexture(GLenum(GL_TEXTURE_2D), chrominanceFramebuffer.texture)
glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_LUMINANCE_ALPHA, GLsizei(bufferWidth / 2), GLsizei(bufferHeight / 2), 0, GLenum(GL_LUMINANCE_ALPHA), GLenum(GL_UNSIGNED_BYTE), CVPixelBufferGetBaseAddressOfPlane(movieFrame, 1))

let chrominanceGLTextureResult = CVOpenGLESTextureCacheCreateTextureFromImage(kCFAllocatorDefault, sharedImageProcessingContext.coreVideoTextureCache, movieFrame, nil, GLenum(GL_TEXTURE_2D), GL_LUMINANCE_ALPHA, GLsizei(bufferWidth / 2), GLsizei(bufferHeight / 2), GLenum(GL_LUMINANCE_ALPHA), GLenum(GL_UNSIGNED_BYTE), 1, &chrominanceGLTexture)

assert(chrominanceGLTextureResult == kCVReturnSuccess && chrominanceGLTexture != nil)

let chrominanceTexture = CVOpenGLESTextureGetName(chrominanceGLTexture!)

glBindTexture(GLenum(GL_TEXTURE_2D), chrominanceTexture)
glTexParameterf(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_WRAP_S), GLfloat(GL_CLAMP_TO_EDGE));
glTexParameterf(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_WRAP_T), GLfloat(GL_CLAMP_TO_EDGE));

let chrominanceFramebuffer: Framebuffer
do {
chrominanceFramebuffer = try Framebuffer(context: sharedImageProcessingContext, orientation: .portrait, size: GLSize(width:GLint(bufferWidth), height:GLint(bufferHeight)), textureOnly: true, overriddenTexture: chrominanceTexture)
} catch {
fatalError("Could not create a framebuffer of the size (\(bufferWidth), \(bufferHeight)), error: \(error)")
}

chrominanceFramebuffer.cache = sharedImageProcessingContext.framebufferCache
chrominanceFramebuffer.lock()

let movieFramebuffer = sharedImageProcessingContext.framebufferCache.requestFramebufferWithProperties(orientation:.portrait, size:GLSize(width:GLint(bufferWidth), height:GLint(bufferHeight)), textureOnly:false)

Expand Down

0 comments on commit ac3526f

Please sign in to comment.