-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathH264Decoder.swift
More file actions
213 lines (170 loc) · 8.52 KB
/
H264Decoder.swift
File metadata and controls
213 lines (170 loc) · 8.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// VideoFrameDecoder.swift
// ImprovedCubeCamera
//
// Created by PHILIP SHEN on 3/19/18.
// Copyright © 2018 PHILIP SHEN. All rights reserved.
//
import Foundation
import VideoToolbox
typealias FrameData = Array<UInt8>
protocol VideoFrameDecoderDelegate {
func receivedDisplayableFrame(_ frame: CVPixelBuffer)
}
class VideoFrameDecoder {
static var delegate: VideoFrameDecoderDelegate?
private var formatDesc: CMVideoFormatDescription?
private var decompressionSession: VTDecompressionSession?
/*
Ideally, whoever calls this function would be returned the displayable frame.
Still figuring that one out. In the meantime this stuff works.
*/
func interpretRawFrameData(_ frameData: inout FrameData) {
var naluType = frameData[4] & 0x1F
if naluType != 7 && formatDesc == nil { return }
// Replace start code with the size
var frameSize = CFSwapInt32HostToBig(UInt32(frameData.count - 4))
memcpy(&frameData, &frameSize, 4)
// The start indices for nested packets. Default to 0.
var ppsStartIndex = 0
var frameStartIndex = 0
var sps: Array<UInt8>?
var pps: Array<UInt8>?
/*
Generally, SPS, PPS, and IDR frames from the camera will come packaged together
while B/P frames will come individually. For the sake of flexibility this code
does not reflect this bitstream format specifically.
*/
// SPS parameters
if naluType == 7 {
print("===== NALU type SPS")
for i in 4..<40 {
if frameData[i] == 0 && frameData[i+1] == 0 && frameData[i+2] == 0 && frameData[i+3] == 1 {
ppsStartIndex = i // Includes the start header
sps = Array(frameData[4..<i])
// Set naluType to the nested packet's NALU type
naluType = frameData[i + 4] & 0x1F
break
}
}
}
// PPS parameters
if naluType == 8 {
print("===== NALU type PPS")
for i in ppsStartIndex+4..<ppsStartIndex+34 {
if frameData[i] == 0 && frameData[i+1] == 0 && frameData[i+2] == 0 && frameData[i+3] == 1 {
frameStartIndex = i
pps = Array(frameData[ppsStartIndex+4..<i])
// Set naluType to the nested packet's NALU type
naluType = frameData[i+4] & 0x1F
break
}
}
if !createFormatDescription(sps: sps!, pps: pps!) {
print("===== ===== Failed to create formatDesc")
return
}
if !createDecompressionSession() {
print("===== ===== Failed to create decompressionSession")
return
}
}
if (naluType == 1 || naluType == 5) && decompressionSession != nil {
print("===== NALU type \(naluType)")
// If this is successful, the callback will be called
// The callback will send the decoded, decompressed frame to the delegate
decodeFrameData(Array(frameData[frameStartIndex...]))
}
}
private func decodeFrameData(_ frameData: FrameData) {
let bufferPointer = UnsafeMutablePointer<UInt8>(mutating: frameData)
// Replace the start code with the size of the NALU
var frameSize = CFSwapInt32HostToBig(UInt32(frameData.count - 4))
memcpy(bufferPointer, &frameSize, 4)
var outputBuffer: CVPixelBuffer?
var blockBuffer: CMBlockBuffer?
var status = CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault,
bufferPointer,
frameData.count,
kCFAllocatorNull,
nil, 0, frameData.count,
0, &blockBuffer)
if status != kCMBlockBufferNoErr { return }
var sampleBuffer: CMSampleBuffer?
let sampleSizeArray = [frameData.count]
status = CMSampleBufferCreateReady(kCFAllocatorDefault,
blockBuffer,
formatDesc,
1, 0, nil,
1, sampleSizeArray,
&sampleBuffer)
if let buffer = sampleBuffer, status == kCMBlockBufferNoErr {
let attachments: CFArray? = CMSampleBufferGetSampleAttachmentsArray(buffer, true)
if let attachmentsArray = attachments {
let dic = unsafeBitCast(CFArrayGetValueAtIndex(attachmentsArray, 0), to: CFMutableDictionary.self)
CFDictionarySetValue(dic,
Unmanaged.passUnretained(kCMSampleAttachmentKey_DisplayImmediately).toOpaque(),
Unmanaged.passUnretained(kCFBooleanTrue).toOpaque())
// Decompress
var flagOut = VTDecodeInfoFlags(rawValue: 0)
status = VTDecompressionSessionDecodeFrame(decompressionSession!, buffer,
[], &outputBuffer, &flagOut)
/* The "CMSampleBuffer" can be returned here and passed to an AVSampleBufferDisplayLayer.
I tried it and the picture was ugly. Instead I decompress with VideoToolbox and then
display the resultant CVPixelLayer. Looks great.
*/
}
}
}
private func createFormatDescription(sps: [UInt8], pps: [UInt8]) -> Bool {
formatDesc = nil
let pointerSPS = UnsafePointer<UInt8>(sps)
let pointerPPS = UnsafePointer<UInt8>(pps)
let dataParamArray = [pointerSPS, pointerPPS]
let parameterSetPointers = UnsafePointer<UnsafePointer<UInt8>>(dataParamArray)
let sizeParamArray = [sps.count, pps.count]
let parameterSetSizes = UnsafePointer<Int>(sizeParamArray)
let status = CMVideoFormatDescriptionCreateFromH264ParameterSets(kCFAllocatorDefault, 2, parameterSetPointers, parameterSetSizes, 4, &formatDesc)
return status == noErr
}
private func createDecompressionSession() -> Bool {
guard let desc = formatDesc else { return false }
if let session = decompressionSession {
VTDecompressionSessionInvalidate(session)
decompressionSession = nil
}
let decoderParameters = NSMutableDictionary()
let destinationPixelBufferAttributes = NSMutableDictionary()
var outputCallback = VTDecompressionOutputCallbackRecord()
outputCallback.decompressionOutputCallback = callback
outputCallback.decompressionOutputRefCon = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())
let status = VTDecompressionSessionCreate(kCFAllocatorDefault,
desc, decoderParameters,
destinationPixelBufferAttributes,&outputCallback,
&decompressionSession)
if status == noErr {
return true
} else {
return false
}
}
private var callback: VTDecompressionOutputCallback = {(
decompressionOutputRefCon: UnsafeMutableRawPointer?,
sourceFrameRefCon: UnsafeMutableRawPointer?,
status: OSStatus,
infoFlags: VTDecodeInfoFlags,
imageBuffer: CVPixelBuffer?,
presentationTimeStamp: CMTime,
duration: CMTime) in
let decoder: VideoFrameDecoder = Unmanaged<VideoFrameDecoder>.fromOpaque(decompressionOutputRefCon!).takeUnretainedValue()
if imageBuffer != nil && status == noErr {
print("===== Image successfully decompressed")
decoder.imageDecompressed(image: imageBuffer!)
} else {
print("===== Failed to decompress. VT Error \(status)")
}
}
private func imageDecompressed(image: CVPixelBuffer) {
VideoFrameDecoder.delegate?.receivedDisplayableFrame(image)
}
}