mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-23 22:55:00 +00:00
Temp
This commit is contained in:
@@ -5,10 +5,17 @@ import CryptoUtils
|
||||
import ManagedFile
|
||||
import Compression
|
||||
|
||||
private func alignUp(size: Int, align: Int) -> Int {
|
||||
precondition(((align - 1) & align) == 0, "Align must be a power of two")
|
||||
|
||||
let alignmentMask = align - 1
|
||||
return (size + alignmentMask) & ~alignmentMask
|
||||
}
|
||||
|
||||
public final class AnimationCacheItemFrame {
|
||||
public enum RequestedFormat {
|
||||
case rgba
|
||||
case yuva
|
||||
case yuva(bytesPerRow: Int)
|
||||
}
|
||||
|
||||
public final class Plane {
|
||||
@@ -279,7 +286,7 @@ private final class AnimationCacheItemWriterImpl: AnimationCacheItemWriter {
|
||||
} else {
|
||||
isFirstFrame = true
|
||||
|
||||
surface = ImageARGB(width: width, height: height)
|
||||
surface = ImageARGB(width: width, height: height, bytesPerRow: alignUp(size: width * 4, align: 32))
|
||||
self.currentSurface = surface
|
||||
}
|
||||
|
||||
@@ -292,7 +299,7 @@ private final class AnimationCacheItemWriterImpl: AnimationCacheItemWriter {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
yuvaSurface = ImageYUVA420(width: width, height: height)
|
||||
yuvaSurface = ImageYUVA420(width: width, height: height, bytesPerRow: nil)
|
||||
self.currentYUVASurface = yuvaSurface
|
||||
}
|
||||
|
||||
@@ -484,7 +491,7 @@ private final class AnimationCacheItemAccessor {
|
||||
private let durationMapping: [Double]
|
||||
private let totalDuration: Double
|
||||
|
||||
private var currentYUVASurface: ImageYUVA420
|
||||
private var currentYUVASurface: ImageYUVA420?
|
||||
private var currentDctData: DctData
|
||||
private var currentDctCoefficients: DctCoefficientsYUVA420
|
||||
|
||||
@@ -506,7 +513,6 @@ private final class AnimationCacheItemAccessor {
|
||||
self.durationMapping = durationMapping
|
||||
self.totalDuration = totalDuration
|
||||
|
||||
self.currentYUVASurface = ImageYUVA420(width: width, height: height)
|
||||
self.currentDctData = DctData(quality: dctQuality)
|
||||
self.currentDctCoefficients = DctCoefficientsYUVA420(width: width, height: height)
|
||||
}
|
||||
@@ -544,43 +550,53 @@ private final class AnimationCacheItemAccessor {
|
||||
frameDataOffset += dctPlane.data.count
|
||||
}
|
||||
|
||||
self.currentDctCoefficients.idct(dctData: self.currentDctData, target: self.currentYUVASurface)
|
||||
let yuvaSurface: ImageYUVA420
|
||||
switch requestedFormat {
|
||||
case .rgba:
|
||||
if let currentYUVASurface = self.currentYUVASurface {
|
||||
yuvaSurface = currentYUVASurface
|
||||
} else {
|
||||
yuvaSurface = ImageYUVA420(width: self.currentDctCoefficients.yPlane.width, height: self.currentDctCoefficients.yPlane.height, bytesPerRow: nil)
|
||||
}
|
||||
case let .yuva(preferredBytesPerRow):
|
||||
yuvaSurface = ImageYUVA420(width: self.currentDctCoefficients.yPlane.width, height: self.currentDctCoefficients.yPlane.height, bytesPerRow: preferredBytesPerRow)
|
||||
}
|
||||
|
||||
self.currentDctCoefficients.idct(dctData: self.currentDctData, target: yuvaSurface)
|
||||
|
||||
switch requestedFormat {
|
||||
case .rgba:
|
||||
let currentSurface = ImageARGB(width: self.currentYUVASurface.yPlane.width, height: self.currentYUVASurface.yPlane.height)
|
||||
self.currentYUVASurface.toARGB(target: currentSurface)
|
||||
let currentSurface = ImageARGB(width: yuvaSurface.yPlane.width, height: yuvaSurface.yPlane.height, bytesPerRow: alignUp(size: yuvaSurface.yPlane.width * 4, align: 32))
|
||||
yuvaSurface.toARGB(target: currentSurface)
|
||||
self.currentYUVASurface = yuvaSurface
|
||||
|
||||
return AnimationCacheItemFrame(format: .rgba(data: currentSurface.argbPlane.data, width: currentSurface.argbPlane.width, height: currentSurface.argbPlane.height, bytesPerRow: currentSurface.argbPlane.bytesPerRow), duration: frameInfo.duration)
|
||||
case .yuva:
|
||||
let currentYUVASurface = self.currentYUVASurface
|
||||
self.currentYUVASurface = ImageYUVA420(width: currentYUVASurface.yPlane.width, height: currentYUVASurface.yPlane.height)
|
||||
|
||||
return AnimationCacheItemFrame(
|
||||
format: .yuva(
|
||||
y: AnimationCacheItemFrame.Plane(
|
||||
data: currentYUVASurface.yPlane.data,
|
||||
width: currentYUVASurface.yPlane.width,
|
||||
height: currentYUVASurface.yPlane.height,
|
||||
bytesPerRow: currentYUVASurface.yPlane.bytesPerRow
|
||||
data: yuvaSurface.yPlane.data,
|
||||
width: yuvaSurface.yPlane.width,
|
||||
height: yuvaSurface.yPlane.height,
|
||||
bytesPerRow: yuvaSurface.yPlane.bytesPerRow
|
||||
),
|
||||
u: AnimationCacheItemFrame.Plane(
|
||||
data: currentYUVASurface.uPlane.data,
|
||||
width: currentYUVASurface.uPlane.width,
|
||||
height: currentYUVASurface.uPlane.height,
|
||||
bytesPerRow: currentYUVASurface.uPlane.bytesPerRow
|
||||
data: yuvaSurface.uPlane.data,
|
||||
width: yuvaSurface.uPlane.width,
|
||||
height: yuvaSurface.uPlane.height,
|
||||
bytesPerRow: yuvaSurface.uPlane.bytesPerRow
|
||||
),
|
||||
v: AnimationCacheItemFrame.Plane(
|
||||
data: currentYUVASurface.vPlane.data,
|
||||
width: currentYUVASurface.vPlane.width,
|
||||
height: currentYUVASurface.vPlane.height,
|
||||
bytesPerRow: currentYUVASurface.vPlane.bytesPerRow
|
||||
data: yuvaSurface.vPlane.data,
|
||||
width: yuvaSurface.vPlane.width,
|
||||
height: yuvaSurface.vPlane.height,
|
||||
bytesPerRow: yuvaSurface.vPlane.bytesPerRow
|
||||
),
|
||||
a: AnimationCacheItemFrame.Plane(
|
||||
data: currentYUVASurface.aPlane.data,
|
||||
width: currentYUVASurface.aPlane.width,
|
||||
height: currentYUVASurface.aPlane.height,
|
||||
bytesPerRow: currentYUVASurface.aPlane.bytesPerRow
|
||||
data: yuvaSurface.aPlane.data,
|
||||
width: yuvaSurface.aPlane.width,
|
||||
height: yuvaSurface.aPlane.height,
|
||||
bytesPerRow: yuvaSurface.aPlane.bytesPerRow
|
||||
)
|
||||
),
|
||||
duration: frameInfo.duration
|
||||
|
||||
@@ -9,20 +9,20 @@ final class ImagePlane {
|
||||
let components: Int
|
||||
var data: Data
|
||||
|
||||
init(width: Int, height: Int, components: Int) {
|
||||
init(width: Int, height: Int, components: Int, bytesPerRow: Int?) {
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.bytesPerRow = width * components
|
||||
self.bytesPerRow = bytesPerRow ?? (width * components)
|
||||
self.components = components
|
||||
self.data = Data(count: width * components * height)
|
||||
self.data = Data(count: self.bytesPerRow * height)
|
||||
}
|
||||
}
|
||||
|
||||
final class ImageARGB {
|
||||
let argbPlane: ImagePlane
|
||||
|
||||
init(width: Int, height: Int) {
|
||||
self.argbPlane = ImagePlane(width: width, height: height, components: 4)
|
||||
init(width: Int, height: Int, bytesPerRow: Int?) {
|
||||
self.argbPlane = ImagePlane(width: width, height: height, components: 4, bytesPerRow: bytesPerRow)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,11 +32,11 @@ final class ImageYUVA420 {
|
||||
let vPlane: ImagePlane
|
||||
let aPlane: ImagePlane
|
||||
|
||||
init(width: Int, height: Int) {
|
||||
self.yPlane = ImagePlane(width: width, height: height, components: 1)
|
||||
self.uPlane = ImagePlane(width: width / 2, height: height / 2, components: 1)
|
||||
self.vPlane = ImagePlane(width: width / 2, height: height / 2, components: 1)
|
||||
self.aPlane = ImagePlane(width: width, height: height, components: 1)
|
||||
init(width: Int, height: Int, bytesPerRow: Int?) {
|
||||
self.yPlane = ImagePlane(width: width, height: height, components: 1, bytesPerRow: bytesPerRow)
|
||||
self.uPlane = ImagePlane(width: width / 2, height: height / 2, components: 1, bytesPerRow: bytesPerRow)
|
||||
self.vPlane = ImagePlane(width: width / 2, height: height / 2, components: 1, bytesPerRow: bytesPerRow)
|
||||
self.aPlane = ImagePlane(width: width, height: height, components: 1, bytesPerRow: bytesPerRow)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,8 +92,8 @@ extension ImageARGB {
|
||||
}
|
||||
}
|
||||
|
||||
func toYUVA420() -> ImageYUVA420 {
|
||||
let resultImage = ImageYUVA420(width: self.argbPlane.width, height: self.argbPlane.height)
|
||||
func toYUVA420(bytesPerRow: Int?) -> ImageYUVA420 {
|
||||
let resultImage = ImageYUVA420(width: self.argbPlane.width, height: self.argbPlane.height, bytesPerRow: bytesPerRow)
|
||||
self.toYUVA420(target: resultImage)
|
||||
return resultImage
|
||||
}
|
||||
@@ -125,8 +125,8 @@ extension ImageYUVA420 {
|
||||
}
|
||||
}
|
||||
|
||||
func toARGB() -> ImageARGB {
|
||||
let resultImage = ImageARGB(width: self.yPlane.width, height: self.yPlane.height)
|
||||
func toARGB(bytesPerRow: Int?) -> ImageARGB {
|
||||
let resultImage = ImageARGB(width: self.yPlane.width, height: self.yPlane.height, bytesPerRow: bytesPerRow)
|
||||
self.toARGB(target: resultImage)
|
||||
return resultImage
|
||||
}
|
||||
@@ -215,14 +215,14 @@ extension DctCoefficientsYUVA420 {
|
||||
targetPlane.data.withUnsafeMutableBytes { bytes in
|
||||
let pixels = bytes.baseAddress!.assumingMemoryBound(to: UInt8.self)
|
||||
|
||||
dctData.dct.inverse(withCoefficients: coefficients, pixels: pixels, width: sourcePlane.width, height: sourcePlane.height, coefficientsPerRow: targetPlane.width, bytesPerRow: targetPlane.width)
|
||||
dctData.dct.inverse(withCoefficients: coefficients, pixels: pixels, width: sourcePlane.width, height: sourcePlane.height, coefficientsPerRow: targetPlane.width, bytesPerRow: targetPlane.bytesPerRow)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func idct(dctData: DctData) -> ImageYUVA420 {
|
||||
let resultImage = ImageYUVA420(width: self.yPlane.width, height: self.yPlane.height)
|
||||
func idct(dctData: DctData, bytesPerRow: Int?) -> ImageYUVA420 {
|
||||
let resultImage = ImageYUVA420(width: self.yPlane.width, height: self.yPlane.height, bytesPerRow: bytesPerRow)
|
||||
self.idct(dctData: dctData, target: resultImage)
|
||||
return resultImage
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user