• iOS CMSampleBuffer deep copy


    extension CVPixelBuffer {
        func copy() -> CVPixelBuffer {
            precondition(CFGetTypeID(self) == CVPixelBufferGetTypeID(), "copy() cannot be called on a non-CVPixelBuffer")
            
            var _copy : CVPixelBuffer?
            
    //        var BytesAlignment:Int = 4
    //        let CFBytesAlignment = CFNumberCreate(kCFAllocatorDefault, .intType, &BytesAlignment)!
            let attribute:[String:Any] = [kCVPixelBufferMetalCompatibilityKey as String:kCFBooleanTrue]
    //                                      kCVPixelBufferBytesPerRowAlignmentKey as String:CFBytesAlignment
            
            CVPixelBufferCreate(
                kCFAllocatorDefault,
                CVPixelBufferGetWidth(self),
                CVPixelBufferGetHeight(self),
                CVPixelBufferGetPixelFormatType(self),
                attribute as CFDictionary,
                &_copy)
            
            guard let copy = _copy else { fatalError() }
            
            CVPixelBufferLockBaseAddress(self, CVPixelBufferLockFlags.readOnly)
            CVPixelBufferLockBaseAddress(copy, CVPixelBufferLockFlags(rawValue: 0))
            
            for plane in 0..<CVPixelBufferGetPlaneCount(self) {
                let dest = CVPixelBufferGetBaseAddressOfPlane(copy, plane)
                let source = CVPixelBufferGetBaseAddressOfPlane(self, plane)
                let height = CVPixelBufferGetHeightOfPlane(self, plane)
                let bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(self, plane)
                
                let bytesPerRowDst = CVPixelBufferGetBytesPerRowOfPlane(copy, plane)
                
                for h in 0..<height {
                    memcpy(dest?.advanced(by:h*bytesPerRowDst), source?.advanced(by:h*bytesPerRow),  bytesPerRow)
                }
            }
            
            CVPixelBufferUnlockBaseAddress(copy, CVPixelBufferLockFlags(rawValue: 0))
            CVPixelBufferUnlockBaseAddress(self, CVPixelBufferLockFlags.readOnly)
            
            
            return copy
        }
    }

    https://stackoverflow.com/questions/38335365/pulling-data-from-a-cmsamplebuffer-in-order-to-create-a-deep-copy 

    http://blog.csdn.net/fernandowei/article/details/52180840

  • 相关阅读:
    Azure DevOps Server 2020.1 新增功能 (TFS)
    Azure DevOps Server 2020.1 升级指南 (TFS)
    Azure DevOps Server:如何在Git历史记录中显示中文姓名
    Azure DevOps Server:集中显示所有团队的燃尽图
    MS中adjust hydrogen功能不能使用的问题
    bat对拍
    CSP 201812-4 数据中心(最小瓶颈生成树)
    CSP 202009
    CSP 202012
    牛客练习赛76
  • 原文地址:https://www.cnblogs.com/mlj318/p/7478920.html
Copyright © 2020-2023  润新知