VPCamera3/SwiftProject/SwiftProject/Project/Util/PlayByTransferConvertor.swift
2024-04-19 19:12:40 +08:00

194 lines
8.3 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// PlayByTransferConvertor.swift
// SwiftProject
//
// Created by aaa on 2024/3/12.
//
import Foundation
import AVKit
import VideoToolbox
import CoreImage
import ImageIO
class PlayByTransferConvertor {
func convertVideo(asset:AVAsset, assetOutput:AVAssetReaderTrackOutput,type:SpatialType,time: CMTime)->(CIImage?,CMTime?) {
var newpb:CIImage? = nil
var presentationTime:CMTime? = nil
print("sta.....>>>>>>>thread:\(Thread.current) assetOutput:\(assetOutput) \n\(Date.now.timeIntervalSince1970)")
while let nextSampleBuffer = assetOutput.copyNextSampleBuffer() {
presentationTime = CMSampleBufferGetPresentationTimeStamp(nextSampleBuffer)
print("presentationTime: \(presentationTime) \ntime: \(time)")
if presentationTime! > time {//buffer>time,
print("如果当前获取的buffer的时间>time的时间,则直接返回即可...")
break
}
if presentationTime == time {
guard let taggedBuffers = nextSampleBuffer.taggedBuffers else { break }
let leftEyeBuffer = taggedBuffers.first(where: {
$0.tags.first(matchingCategory: .stereoView) == .stereoView(.leftEye)
})?.buffer
let rightEyeBuffer = taggedBuffers.first(where: {
$0.tags.first(matchingCategory: .stereoView) == .stereoView(.rightEye)
})?.buffer
if let leftEyeBuffer,
let rightEyeBuffer,
case let .pixelBuffer(leftEyePixelBuffer) = leftEyeBuffer,
case let .pixelBuffer(rightEyePixelBuffer) = rightEyeBuffer {
let lciImage = CIImage(cvPixelBuffer: leftEyePixelBuffer)
let rciImage = CIImage(cvPixelBuffer: rightEyePixelBuffer)
let left = UIImage(ciImage: lciImage )
let right = UIImage(ciImage: rciImage )
var cwidth:CGFloat
var cheight:CGFloat
switch type {
case .hsbs:
cwidth = left.size.width
cheight = left.size.height
newpb = joinImages_sbs(left: left, right: right, imgWidth: cwidth, imgHeight:cheight )
break
case .fsbs:
cwidth = left.size.width
cheight = left.size.height
newpb = joinImages_fsbs(left: left, right: right, imgWidth: cwidth, imgHeight: cheight)
break
case .parallelEyes://
newpb = joinImages(leftImage: lciImage, rightImage: rciImage)
break
case .crossedEyes://
newpb = joinImages(leftImage: rciImage, rightImage: lciImage)
break
case .redBlueSolid://
newpb = joinImages_red_blue(lciImage: lciImage, rciImage: rciImage)
break
default:
break
}
}
CMSampleBufferInvalidate(nextSampleBuffer)
break
}
}
print("PlayByTransferConvertor 测试看是否有返回....")
return (newpb,presentationTime)
}
//
func joinImages_red_blue(lciImage:CIImage,rciImage:CIImage) -> CIImage {
//
let redColorMatrix: [CGFloat] = [
0.0, 0.0, 0.0, 0.0, 0.0, //
0.0, 0.0, 0.0, 0.0, 0.0, // 绿
0.0, 0.0, 1.0, 0.0, 0.0, //
0.0, 0.0, 0.0, 1.0, 0.0 //
]
let blueColorMatrix: [CGFloat] = [
1.0, 0.0, 0.0, 0.0, 0.0, //
0.0, 0.0, 0.0, 0.0, 0.0, // 绿
0.0, 0.0, 0.0, 0.0, 0.0, //
0.0, 0.0, 0.0, 1.0, 0.0 //
]
let redFilter = CIFilter(name: "CIColorMatrix")!
redFilter.setValue(lciImage, forKey: kCIInputImageKey)
redFilter.setValue(CIVector(values: redColorMatrix, count: redColorMatrix.count), forKey: "inputRVector")
let blueFilter = CIFilter(name: "CIColorMatrix")!
blueFilter.setValue(rciImage, forKey: kCIInputImageKey)
blueFilter.setValue(CIVector(values: blueColorMatrix, count: blueColorMatrix.count), forKey: "inputBVector")
var lastImg:CIImage? = nil
//
if let redOutputImage = redFilter.outputImage,
let blueOutputImage = blueFilter.outputImage {
let compositeFilter = CIFilter(name: "CIScreenBlendMode")!
compositeFilter.setValue(redOutputImage, forKey: kCIInputImageKey)
compositeFilter.setValue(blueOutputImage, forKey: kCIInputBackgroundImageKey)
let sharpenedFilter = CIFilter(name: "CISharpenLuminance")!
sharpenedFilter.setValue(compositeFilter.outputImage, forKey: kCIInputImageKey)
sharpenedFilter.setValue(2, forKey: kCIInputSharpnessKey)
lastImg = sharpenedFilter.outputImage!
}
return lastImg!
}
// SBS
func joinImages_sbs( left:UIImage, right:UIImage,imgWidth:CGFloat,imgHeight:CGFloat) -> CIImage {
let newImageSize = CGSize(width:imgWidth, height: imgHeight);
UIGraphicsBeginImageContextWithOptions(newImageSize, false, 1);
left.draw(in: CGRect(x:0, y:0, width:imgWidth/2, height:imgHeight))
right.draw(in: CGRect(x:imgWidth/2, y:0, width:imgWidth/2, height:imgHeight))
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext();
let ci = CIImage(cgImage: image.cgImage!)
return ci
}
//FSBS
func joinImages_fsbs( left:UIImage, right:UIImage,imgWidth:CGFloat,imgHeight:CGFloat) -> CIImage {
let newImageSize = CGSize(width:imgWidth, height: imgHeight);//
UIGraphicsBeginImageContextWithOptions(newImageSize, false, 1);
left.draw(in: CGRect(x:0, y:imgHeight/4, width:imgWidth/2, height:imgHeight/2))
right.draw(in: CGRect(x:imgWidth/2, y:imgHeight/4, width:imgWidth/2, height:imgHeight/2))
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext();
let ci = CIImage(cgImage: image.cgImage!)
return ci
}
// OU
func joinImages_ou( left:UIImage, right:UIImage,imgWidth:CGFloat,imgHeight:CGFloat) -> CIImage {
let newImageSize = CGSize(width:imgWidth, height: imgHeight);
UIGraphicsBeginImageContextWithOptions(newImageSize, false, 1);
left.draw(in: CGRect(x:0, y:0, width:imgWidth, height:imgHeight/2))
right.draw(in: CGRect(x:0, y:imgHeight/2, width:imgWidth, height:imgHeight/2))
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext();
let ci = CIImage(cgImage: image.cgImage!)
return ci
}
//
func joinImages( leftImage:CIImage, rightImage:CIImage) -> CIImage {
let left = UIImage(ciImage: leftImage )
let right = UIImage(ciImage: rightImage )
let imageWidth = left.size.width/2 + right.size.width/2
let imageHeight = left.size.height/2
let newImageSize = CGSize(width:imageWidth, height: left.size.height);
UIGraphicsBeginImageContextWithOptions(newImageSize, false, 1);
left.draw(in: CGRect(x:0, y:imageHeight/2, width:imageWidth/2, height:imageHeight))
right.draw(in: CGRect(x:imageWidth/2, y:imageHeight/2, width:imageWidth/2, height:imageHeight))
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext();
let ci = CIImage(cgImage: image.cgImage!)
return ci
}
}