iOS Swift VideoToolBox распаковывает кадр EXC_BAD_ADDRESS ошибка - PullRequest
0 голосов
/ 11 апреля 2020

Я пытался преобразовать CMSampleBuffer в Данные через inte rnet и создать его копию. Я уже сжал CMSampleBuffer захвата камеры VTCompressionSession с h264. Теперь я создал CMSampleBuffer, создал VTDecompressionSession, но есть ошибка памяти, когда в VTDecompressionSessionDecodeFrame () Я не знаю, как исправить. Я видел много примеров h264 на inte rnet, но все они использовали устаревший язык Objective- C. Я использую Xcode 11.3 Swift 5.1 и развернул на iOS устройствах, которые работают iOS 12.1.

    var sps,pps:UnsafePointer<UInt8>?
    var spsSize=0,ppsSize:Int=0
    var parameterSetCount=0
    var nalUnitHeaderLength:Int32=0
    CMVideoFormatDescriptionGetH264ParameterSetAtIndex(CMSampleBufferGetFormatDescription(encodedSamples.last!)!, parameterSetIndex: 0, parameterSetPointerOut: &sps, parameterSetSizeOut: &spsSize, parameterSetCountOut: &parameterSetCount, nalUnitHeaderLengthOut: &nalUnitHeaderLength)
    CMVideoFormatDescriptionGetH264ParameterSetAtIndex(CMSampleBufferGetFormatDescription(encodedSamples.last!)!, parameterSetIndex: 1, parameterSetPointerOut: &pps, parameterSetSizeOut: &ppsSize, parameterSetCountOut: &parameterSetCount, nalUnitHeaderLengthOut: &nalUnitHeaderLength)
    let dataBuffer:CMBlockBuffer={
        var buffer:CMBlockBuffer?
        let bufferData:Data={
            let blockBuffer=CMSampleBufferGetDataBuffer(encodedSamples.last!)!
            var totalLength:Int=0
            var data:UnsafeMutablePointer<Int8>?
            CMBlockBufferGetDataPointer(blockBuffer, atOffset: 0, lengthAtOffsetOut: nil, totalLengthOut: &totalLength, dataPointerOut: &data)
            return Data(bytes: data!,count:totalLength)
        }()
        var bbd=Data()
        bbd.append(bufferData)
        let status=CMBlockBufferCreateWithMemoryBlock(allocator: kCFAllocatorDefault,memoryBlock: &bbd, blockLength: bbd.count,blockAllocator: kCFAllocatorDefault,customBlockSource: nil, offsetToData: 0, dataLength: bbd.count,flags: 0, blockBufferOut: &buffer)
        if status != kCMBlockBufferNoErr{print(status)}
        return buffer!
    }()
    let formatDes:CMFormatDescription={
        let dataParamArray = [sps!,pps!]
        let parameterSetPointers = UnsafePointer<UnsafePointer<UInt8>>(dataParamArray)
        var formatDescription:CMFormatDescription?
        CMVideoFormatDescriptionCreateFromH264ParameterSets(allocator: kCFAllocatorDefault, parameterSetCount: parameterSetCount, parameterSetPointers: parameterSetPointers, parameterSetSizes: UnsafePointer<Int>([spsSize,ppsSize]), nalUnitHeaderLength: nalUnitHeaderLength, formatDescriptionOut: &formatDescription)
        return formatDescription!
    }()
    var timingInfo:CMSampleTimingInfo={
        var t=CMSampleTimingInfo()
        CMSampleBufferGetSampleTimingInfoArray(encodedSamples.last!, entryCount: 1, arrayToFill: &t, entriesNeededOut: nil)
        return t
    }()
    var sampleSize=CMBlockBufferGetDataLength(dataBuffer)
    var sampleBuffer:CMSampleBuffer!
    let status=CMSampleBufferCreate(allocator: kCFAllocatorDefault, dataBuffer: dataBuffer, dataReady: true, makeDataReadyCallback: nil, refcon: nil, formatDescription: formatDes, sampleCount: 1, sampleTimingEntryCount: 1, sampleTimingArray: &timingInfo, sampleSizeEntryCount: 1, sampleSizeArray: &sampleSize, sampleBufferOut: &sampleBuffer)
    var attachmentsArray=CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, createIfNecessary: true) as! Array<Dictionary<String, Any>>
    attachmentsArray[0]=[kCMSampleAttachmentKey_DependsOnOthers as String:false]
    if status != noErr{print(status)}
    if #available(iOS 9, *){
        var session:VTDecompressionSession?
        VTDecompressionSessionCreate(allocator: kCFAllocatorDefault, formatDescription: formatDes, decoderSpecification: nil, imageBufferAttributes: [kCVPixelBufferOpenGLESCompatibilityKey:true,kCVPixelBufferPixelFormatTypeKey:kCVPixelFormatType_32BGRA] as CFDictionary, outputCallback: nil, decompressionSessionOut: &session)
        print(VTDecompressionSessionCanAcceptFormatDescription(session!, formatDescription: formatDes))
        var info=VTDecodeInfoFlags()
        let flags = VTDecodeFrameFlags._EnableAsynchronousDecompression
        let status=VTDecompressionSessionDecodeFrame(session!, sampleBuffer: sampleBuffer, flags: flags, infoFlagsOut: &info){status,infoFlags,imageBuffer,presentationTimeStamp,presentationDuration in
            print(imageBuffer!)
        }//EXC_BAD_ADDRESS HERE!!
        print(status,info)
        VTDecompressionSessionInvalidate(session!)
    }

1 Ответ

0 голосов
/ 14 апреля 2020

Я нахожу проблему. CMBlockBufferCreateWithMemoryBlock () параметр memoryBlock не может использовать '& Data' в качестве указателя .

let dataBuffer:CMBlockBuffer={
    var buffer:CMBlockBuffer?
    let bufferData:Data={
        let blockBuffer=CMSampleBufferGetDataBuffer(encodedSamples.last!)!
        var totalLength:Int=0
        var data:UnsafeMutablePointer<Int8>?
        CMBlockBufferGetDataPointer(blockBuffer, atOffset: 0, lengthAtOffsetOut: nil, totalLengthOut: &totalLength, dataPointerOut: &data)
        return Data(bytes: data!,count:totalLength)
    }()
    var bbd=Data()
    bbd.append(bufferData)
    let status=CMBlockBufferCreateWithMemoryBlock(allocator: kCFAllocatorDefault,memoryBlock: UnsafeMutableRawPointer(mutating: (bbd as NSData).bytes), blockLength: bbd.count,blockAllocator: kCFAllocatorDefault,customBlockSource: nil, offsetToData: 0, dataLength: bbd.count,flags: 0, blockBufferOut: &buffer)
    if status != kCMBlockBufferNoErr{print(status)}
    return buffer!
}()

Это редко встречающаяся проблема в языке Swift. После того, как я попытался напечатать данные из созданного CMBlockBuffer, я наконец решил, используя UnsafeMutableRawPointer ((данные как NSData) .bytes) .

...