7

我正在尝试在 iOS 上播放视频 (MP4/H.263),但得到的结果非常模糊。这是初始化资产读取的代码:

mTextureHandle = [self createTexture:CGSizeMake(400,400)];

NSURL * url = [NSURL fileURLWithPath:file];    
mAsset = [[AVURLAsset alloc] initWithURL:url options:NULL];

NSArray * tracks = [mAsset tracksWithMediaType:AVMediaTypeVideo];

mTrack = [tracks objectAtIndex:0];

NSLog(@"Tracks: %i", [tracks count]);

NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
NSDictionary * settings = [[NSDictionary alloc] initWithObjectsAndKeys:value, key, nil];

mOutput = [[AVAssetReaderTrackOutput alloc] 
            initWithTrack:mTrack outputSettings:settings];


mReader = [[AVAssetReader alloc] initWithAsset:mAsset error:nil];
[mReader addOutput:mOutput];

对于读者 init 来说这么多,现在是实际的纹理:

CMSampleBufferRef sampleBuffer = [mOutput copyNextSampleBuffer];    
CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress( pixelBuffer, 0 );

glBindTexture(GL_TEXTURE_2D, mTextureHandle);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 600, 400, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, CVPixelBufferGetBaseAddress( pixelBuffer ));    
CVPixelBufferUnlockBaseAddress( pixelBuffer, 0 );    
CFRelease(sampleBuffer);

一切正常......除了渲染图像看起来像这样; 切片和歪斜?

在此处输入图像描述

我什至尝试查看AVAssetTrack的首选转换矩阵,但无济于事,因为它总是返回CGAffineTransformIdentity.

旁注:如果我将源切换到相机,图像会渲染得很好。我错过了一些减压步骤吗?这不应该由资产阅读器处理吗?

谢谢!

代码:https ://github.com/shaded-enmity/objcpp-opengl-video

4

1 回答 1

6

我认为 CMSampleBuffer 出于性能原因使用填充,因此您需要为纹理设置正确的宽度。

尝试设置纹理的宽度: CVPixelBufferGetBytesPerRow(pixelBuffer) / 4 (如果您的视频格式每像素使用 4 个字节,则更改其他)

于 2011-10-01T13:44:36.927 回答