Metal学习笔记(六) -- Metal渲染视频

除了渲染摄像头采集数据,我们还可以通过Metal渲染视频文件。不同的是,视频文件经过编码,并且采用的是YUV颜色空间,所以除了解码,我们还需要矩阵将YUV转化为RGB颜色空间。

基本思路

采用AVFoundationAVAssetReader解码视频文件获得CMSampleBufferRef,再通过CoreVideo转化得到MTLTexture对象(YUV),最后将MTLTexture和YUV转RGB的矩阵传入Metal,完成渲染。

思维导图.png

视频解码

关于AVAssetReader,可以通过苹果官方文档得知,它一个用来获得视频数据的工具类。

AVAssetReader lets you:

  • Read raw un-decoded media samples directly from storage, obtain samples decoded into renderable forms.
  • Mix multiple audio tracks of the asset and compose multiple video tracks by using AVAssetReaderAudioMixOutput and AVAssetReaderVideoCompositionOutput.

The AVAssetReader pipelines are multithreaded internally. After you initiate reading with initWithAsset:error:, a reader loads and processes a reasonable amount of sample data ahead of use so that retrieval operations such as copyNextSampleBuffer (AVAssetReaderOutput) can have very low latency. AVAssetReader is not intended for use with real-time sources, and its performance is not guaranteed for real-time operations.

由于本文不对音频做探究,所以只获取视频轨道数据。

@implementation LJAssetReader {
    AVAssetReaderTrackOutput *readerVideoTrackOutput;
    AVAssetReader *assetReader;
    NSURL *videoUrl;
    NSLock *lock;
}

- (instancetype)initWithUrl:(NSURL *)url {
    if (self = [super init]) {
        videoUrl = url;
        lock = [[NSLock alloc] init];
        [self setupAsset];
    }
    return self;
}

- (void)setupAsset {
    NSDictionary *inputOption = @{AVURLAssetPreferPreciseDurationAndTimingKey: @(YES)};
    
    AVURLAsset *inputAsset = [[AVURLAsset alloc] initWithURL:videoUrl options:inputOption];
    __weak typeof(self) weakSelf = self;
    
    NSString *tracks = @"tracks";
    
    [inputAsset loadValuesAsynchronouslyForKeys:@[tracks] completionHandler:^{
        __strong typeof(self) strongSelf = weakSelf;
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSError *error = nil;
            AVKeyValueStatus trackStatus = [inputAsset statusOfValueForKey:@"tracks" error:&error];
            if (trackStatus != AVKeyValueStatusLoaded) {
                NSLog(@"error:%@", error);
                return;
            }
            
            [weakSelf processWithAsset:inputAsset];
            
            
        });
    }];
}

- (void)processWithAsset:(AVAsset *)asset {
    [lock lock];
    NSLog(@"processWithAsset");
    
    NSError *error = nil;
    
    assetReader = [AVAssetReader assetReaderWithAsset:asset error:&error];
    
    NSMutableDictionary *outputSettings = [NSMutableDictionary dictionary];
    [outputSettings setObject:@(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange) forKey:(id)kCVPixelBufferPixelFormatTypeKey];
    
    readerVideoTrackOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] firstObject] outputSettings:outputSettings];
    
    readerVideoTrackOutput.alwaysCopiesSampleData = NO;
    
    [assetReader addOutput:readerVideoTrackOutput];
    
    if ([assetReader startReading] == NO) {
        NSLog(@"error reading");
    }
    
    [lock unlock];
}

- (CMSampleBufferRef)readBuffer {
    [lock lock];
    
    CMSampleBufferRef sampleBuffer = nil;
    
    if (readerVideoTrackOutput) {
        sampleBuffer = [readerVideoTrackOutput copyNextSampleBuffer];
    }
    
    if (assetReader && assetReader.status == AVAssetReaderStatusCompleted) {
        NSLog(@"customInit");
        
        readerVideoTrackOutput = nil;
        assetReader = nil;
        
        [self setupAsset];
    }
    
    [lock unlock];
    
    return  sampleBuffer;
}

@end

AVAssetReader的使用步骤为,将AVURLAsset作为AVAssetReader的输入源获取视频源数据,再通过AVAssetReaderTrackOutput作为AVAssetReader的输出端口并通过copyNextSampleBuffer获得CMSampleBufferRef

需要注意的是AVAssetReaderTrackOutput的输出设置里将输出格式设置为kCVPixelFormatType_420YpCbCr8BiPlanarFullRange,则表示输出采用的是4:2:0的YUV颜色空间格式,并且采用的是双平面,即Y通道一个平面,UV通道一个平面,颜色范围为更多的FullRange,这个设置至关重要,关系着Metal获取纹素的计算方式。

Metal配置

关于Metal的配置,这里就不再赘述,直接上代码。

- (void)setupMetal {
    _mtkView = [[MTKView alloc] initWithFrame:self.view.bounds device:MTLCreateSystemDefaultDevice()];
    
    if (!_mtkView.device) {
        NSLog(@"not device");
        return;
    }
    
    [self.view addSubview:_mtkView];
    
    _mtkView.delegate = self;
    
    self.viewportSize = (vector_uint2){self.mtkView.drawableSize.width, self.mtkView.drawableSize.height};
}

- (void)setupPipeline {
    id<MTLLibrary> defaultLibrary = [self.mtkView.device newDefaultLibrary];
    
    id<MTLFunction> vertexFunction = [defaultLibrary newFunctionWithName:@"vertexShader"];
    id<MTLFunction> fragmentFunction = [defaultLibrary newFunctionWithName:@"fragmentShader"];
    
    MTLRenderPipelineDescriptor *pipelineDesc = [[MTLRenderPipelineDescriptor alloc] init];
    pipelineDesc.label = @"my pipeline desc";
    pipelineDesc.vertexFunction = vertexFunction;
    pipelineDesc.fragmentFunction = fragmentFunction;
    pipelineDesc.colorAttachments[0].pixelFormat = self.mtkView.colorPixelFormat;
    
    NSError *error = nil;
    _pipeline = [self.mtkView.device newRenderPipelineStateWithDescriptor:pipelineDesc error:&error];
    
    if (error) {
        NSLog(@"pipeline create error: %@", error.localizedDescription);
        return;
    }
    
    _commandQueue = [self.mtkView.device newCommandQueue];
}

而Metal的片元着色器函数则需要传入两个纹理(Y通道纹理和UV通道纹理)和一个转化矩阵,代码如下:

#include <metal_stdlib>
#import "LJShaderTypes.h"
using namespace metal;

typedef struct
{
    float4 clipSpacePosition [[position]];
    float2 textureCoord;
} RasteizerData;

vertex RasteizerData
vertexShader(uint vertexID [[vertex_id]],
             constant LJVertex *vertexArray [[buffer(LJVertexInputIndexVertices)]])
{
    RasteizerData out;
    out.clipSpacePosition = vertexArray[vertexID].position;
    out.textureCoord = vertexArray[vertexID].textureCoord;
    return out;
}

fragment float4 fragmentShader(RasteizerData input [[stage_in]],
                               texture2d<float> textureY [[texture(LJFragmentTextureIndexTextureY)]],
                               texture2d<float> textureUV [[texture(LJFragmentTextureIndexTextureUV)]],
                               constant LJConvertMatrix *convertMatrix [[buffer(LJFragmentBufferIndexMatrix)]])
{
    
    constexpr sampler textureSampler(mag_filter::linear, min_filter::linear);
    float3 yuv = float3(textureY.sample(textureSampler, input.textureCoord).r, textureUV.sample(textureSampler, input.textureCoord).rg);
    float3 rgb = convertMatrix->matrix * (yuv + convertMatrix->offset);
    
    return  float4(rgb, 1.0);
}

附上Metal和app共有文件的代码

#ifndef LJShaderTypes_h
#define LJShaderTypes_h

#include <simd/simd.h>

typedef struct {
    vector_float4 position;
    vector_float2 textureCoord;
}LJVertex;

typedef struct {
    matrix_float3x3  matrix;
    vector_float3 offset;
}LJConvertMatrix;

typedef enum {
    LJVertexInputIndexVertices = 0,
}LJVertexInputIndex;

typedef enum {
    LJFragmentBufferIndexMatrix = 0,
}LJFragmentBufferIndex;

typedef enum {
    LJFragmentTextureIndexTextureY = 0,
    LJFragmentTextureIndexTextureUV = 1,
}LJFragmentTextureIndex;

#endif /* LJShaderTypes_h */

准备顶点和转换矩阵

- (void)setupVertices {
    static const LJVertex quardVertices[] = {
        { {  1.0, -1.0, 0.0, 1.0 },  { 1.f, 1.f } },
        { { -1.0, -1.0, 0.0, 1.0 },  { 0.f, 1.f } },
        { { -1.0,  1.0, 0.0, 1.0 },  { 0.f, 0.f } },
        
        { {  1.0, -1.0, 0.0, 1.0 },  { 1.f, 1.f } },
        { { -1.0,  1.0, 0.0, 1.0 },  { 0.f, 0.f } },
        { {  1.0,  1.0, 0.0, 1.0 },  { 1.f, 0.f } },
    };
    
    _vertices = [self.mtkView.device newBufferWithBytes:quardVertices length:sizeof(quardVertices) options:MTLResourceStorageModeShared];
    
    _numVertices = sizeof(quardVertices) / sizeof(LJVertex);
}

- (void)setupMatrix {
    //1.转化矩阵
     // BT.601, which is the standard for SDTV.
     matrix_float3x3 kColorConversion601DefaultMatrix = (matrix_float3x3){
         (simd_float3){1.164,  1.164, 1.164},
         (simd_float3){0.0, -0.392, 2.017},
         (simd_float3){1.596, -0.813,   0.0},
     };
     
     // BT.601 full range
     matrix_float3x3 kColorConversion601FullRangeMatrix = (matrix_float3x3){
         (simd_float3){1.0,    1.0,    1.0},
         (simd_float3){0.0,    -0.343, 1.765},
         (simd_float3){1.4,    -0.711, 0.0},
     };
    
     // BT.709, which is the standard for HDTV.
     matrix_float3x3 kColorConversion709DefaultMatrix[] = {
         (simd_float3){1.164,  1.164, 1.164},
         (simd_float3){0.0, -0.213, 2.112},
         (simd_float3){1.793, -0.533,   0.0},
     };
     //2.偏移量
    vector_float3 kColorConversion601FullRangeOffset = (vector_float3){ -(16.0/255.0), -0.5, -0.5};
    
    LJConvertMatrix matrix;
    
    matrix.matrix = kColorConversion601FullRangeMatrix;
    
    matrix.offset = kColorConversion601FullRangeOffset;
    
    _convertMatrix = [self.mtkView.device newBufferWithBytes:&matrix length:sizeof(matrix) options:MTLResourceStorageModeShared];
}

YUV转RGB的矩阵有3种,这里采用了BT.601 full range

开始渲染

- (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size {
    _viewportSize = (vector_uint2){size.width, size.height};
}

- (void)drawInMTKView:(MTKView *)view {
    id<MTLCommandBuffer> commandBuffer = [self.commandQueue commandBuffer];
    commandBuffer.label = @"my commadn buffer";
    
    MTLRenderPassDescriptor *renderPassDesc = view.currentRenderPassDescriptor;
    
    CMSampleBufferRef sampleBuffer = [self.reader readBuffer];
    
    if (renderPassDesc && sampleBuffer) {
        renderPassDesc.colorAttachments[0].clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1.0);
        
        id<MTLRenderCommandEncoder> commandEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDesc];
        
        [commandEncoder setRenderPipelineState:self.pipeline];
        
        [commandEncoder setViewport:(MTLViewport){0.0, 0.0, self.viewportSize.x, self.viewportSize.y, -1.0, 1.0}];
        
        [commandEncoder setVertexBuffer:self.vertices offset:0 atIndex:LJVertexInputIndexVertices];
        
        [self setupTextureWithEncoder:commandEncoder buffer:sampleBuffer];
        
        [commandEncoder setFragmentBuffer:self.convertMatrix offset:0 atIndex:LJFragmentBufferIndexMatrix];
        
        [commandEncoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:self.numVertices];
        
        [commandEncoder endEncoding];
        
        [commandBuffer presentDrawable:view.currentDrawable];
    }
    
    [commandBuffer commit];
}

这部分代码只是常规的渲染,关键点在于setupTextureWithEncoder:buffer:,代码如下

- (void)setupTextureWithEncoder:(id<MTLRenderCommandEncoder>)encoder buffer:(CMSampleBufferRef)samplerBuffer {

    CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(samplerBuffer);
    
    id<MTLTexture> textureY = nil;
    id<MTLTexture> textureUV = nil;
    
    {
        size_t width  = CVPixelBufferGetWidthOfPlane(pixelBuffer, 0);
        size_t height = CVPixelBufferGetHeightOfPlane(pixelBuffer, 0);
        
        MTLPixelFormat pixelFormat = MTLPixelFormatR8Unorm;
        
        CVMetalTextureRef temTexture = nil;
        
        CVReturn status = CVMetalTextureCacheCreateTextureFromImage(NULL, self.textureCache, pixelBuffer, NULL, pixelFormat, width, height, 0, &temTexture);
        
        if (status == kCVReturnSuccess) {
            textureY = CVMetalTextureGetTexture(temTexture);
            
            CFRelease(temTexture);
        }
    }
    
    {
        size_t width = CVPixelBufferGetWidthOfPlane(pixelBuffer, 1);
        size_t height = CVPixelBufferGetHeightOfPlane(pixelBuffer, 1);
        MTLPixelFormat pixelFormat = MTLPixelFormatRG8Unorm;
        CVMetalTextureRef tmpTexture = NULL;
        CVReturn status = CVMetalTextureCacheCreateTextureFromImage(NULL, self.textureCache, pixelBuffer, NULL, pixelFormat, width, height, 1, &tmpTexture);
        if (status == kCVReturnSuccess) {
            textureUV = CVMetalTextureGetTexture(tmpTexture);
            CFRelease(tmpTexture);
        }
    }
    
    if (textureY != nil && textureUV != nil) {
        [encoder setFragmentTexture:textureY atIndex:LJFragmentTextureIndexTextureY];
        [encoder setFragmentTexture:textureUV atIndex:LJFragmentTextureIndexTextureUV];
    }
    
    CFRelease(samplerBuffer);
    
}

因为在前面我们设置视频流输出格式为kCVPixelFormatType_420YpCbCr8BiPlanarFullRange,所以CVPixelBufferRef有两个平面,我们可以通过CVMetalTextureCacheCreateTextureFromImage函数将planeIndex参数设置为0或1获取不同平面的纹理,另外因为YUV是4:2:0的关系,所以两个平面的宽高并不一致(Y平面的宽高是UV平面宽高的2倍),我们需要使用CVPixelBufferGetWidthOfPlane获取不同平面的宽高。

最后附上demo代码

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,589评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,615评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,933评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,976评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,999评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,775评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,474评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,359评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,854评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,007评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,146评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,826评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,484评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,029评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,153评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,420评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,107评论 2 356