forked from ScanNet/ScanNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoder.mm
More file actions
125 lines (104 loc) · 4 KB
/
Encoder.mm
File metadata and controls
125 lines (104 loc) · 4 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
//
// Encoder.mm
// Scanner
//
// Created by Toan Vuong on 11/21/15.
// Copyright © 2015 Toan Vuong. All rights reserved.
//
// Sampled from:
// https://github.com/manishganvir/iOS-h264Hw-Toolbox/blob/master/VTToolbox/VTToolbox/H264HwEncoderImpl.m
//
#include <stdexcept>
#include <sstream>
#include "Encoder.h"
Encoder::Encoder(const EncoderConfig &encoderConfig)
: m_compressionSessionRef(NULL)
, m_encoderConfig(encoderConfig)
, m_cb(NULL)
, m_userData(NULL)
, m_frameCount(0)
{
NSDictionary *sourceImageAttributes =
@{
(id)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange),
(id)kCVPixelBufferWidthKey: @(m_encoderConfig.getWidth()),
(id)kCVPixelBufferHeightKey: @(m_encoderConfig.getHeight())
};
CMVideoCodecType format = m_encoderConfig.getFormat() == EncoderConfig::H264
? kCMVideoCodecType_H264
: kCMVideoCodecType_JPEG;
OSStatus result = VTCompressionSessionCreate(kCFAllocatorDefault,
m_encoderConfig.getWidth(),
m_encoderConfig.getHeight(),
format,
NULL,
(__bridge CFDictionaryRef)sourceImageAttributes,
NULL,
EncodedFrameAvailable,
this,
&m_compressionSessionRef);
CFDictionaryRef sessionProperties = m_encoderConfig.getProperties();
result = VTSessionSetProperties(m_compressionSessionRef, sessionProperties);
if (result != noErr)
{
throw std::runtime_error("Failed to set VTSession properties");
}
VTCompressionSessionPrepareToEncodeFrames(m_compressionSessionRef);
}
Encoder::~Encoder()
{
if (m_compressionSessionRef)
{
VTCompressionSessionInvalidate(m_compressionSessionRef);
CFRelease(m_compressionSessionRef);
}
}
void Encoder::registerEncodedFrameCallback(EncodedFrameCallback cb, void *userData)
{
m_cb = cb;
m_userData = userData;
}
void Encoder::encodeFrame(STColorFrame *frame)
{
CVImageBufferRef imageBuffer = (CVImageBufferRef)CMSampleBufferGetImageBuffer(frame.sampleBuffer);
CMTime pts = CMSampleBufferGetPresentationTimeStamp(frame.sampleBuffer);
m_frameCount++;
VTEncodeInfoFlags encodeInfoFlags = 0;
OSStatus status = VTCompressionSessionEncodeFrame(m_compressionSessionRef,
imageBuffer,
pts,
kCMTimeInvalid,
NULL,
NULL, // Do we want to pass a frame ref? Probably un-needed
&encodeInfoFlags);
if (status != 0)
{
std::stringstream ss;
ss << "Error! " << status;
throw std::runtime_error(ss.str());
}
}
void Encoder::_encodedFrame(CMSampleBufferRef sampleBuffer)
{
if (!m_cb)
{
throw std::runtime_error("Must register a callback before encoding");
}
m_cb(sampleBuffer, m_userData);
}
void Encoder::EncodedFrameAvailable(void *outputCallbackRefCon,
void *sourceFrameRefCon,
OSStatus status,
VTEncodeInfoFlags infoFlags,
CMSampleBufferRef sampleBuffer)
{
if (status != noErr)
{
NSLog(@"Encode error: %d", (int) status);
}
Encoder *encoder = static_cast<Encoder *>(outputCallbackRefCon);
if (encoder)
{
encoder->_encodedFrame(sampleBuffer);
}
}