Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Side by Side Diff: webrtc/sdk/objc/Framework/Classes/VideoToolbox/RTCVideoDecoderH264.mm

Issue 2987413002: ObjC: Implement HW codecs in ObjC instead of C++ (Closed)
Patch Set: Rebase against https://codereview.webrtc.org/2992233002 Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 * 9 *
10 */ 10 */
11 11
12 #include "webrtc/sdk/objc/Framework/Classes/VideoToolbox/decoder.h" 12 #import "WebRTC/RTCVideoCodecH264.h"
13 13
14 #include <memory> 14 #import <VideoToolbox/VideoToolbox.h>
15 15
16 #include "libyuv/convert.h" 16 #include "webrtc/modules/video_coding/include/video_error_codes.h"
17 #include "webrtc/api/video/video_frame.h"
18 #include "webrtc/common_video/include/video_frame.h"
19 #include "webrtc/rtc_base/checks.h" 17 #include "webrtc/rtc_base/checks.h"
20 #include "webrtc/rtc_base/logging.h" 18 #include "webrtc/rtc_base/logging.h"
21 #include "webrtc/sdk/objc/Framework/Classes/Video/objc_frame_buffer.h" 19 #include "webrtc/rtc_base/timeutils.h"
22 #include "webrtc/sdk/objc/Framework/Classes/VideoToolbox/nalu_rewriter.h" 20 #include "webrtc/sdk/objc/Framework/Classes/VideoToolbox/nalu_rewriter.h"
23 21
22 #import "WebRTC/RTCVideoFrame.h"
24 #import "WebRTC/RTCVideoFrameBuffer.h" 23 #import "WebRTC/RTCVideoFrameBuffer.h"
24 #import "helpers.h"
25 25
26 #if defined(WEBRTC_IOS) 26 #if defined(WEBRTC_IOS)
27 #import "Common/RTCUIApplicationStatusObserver.h" 27 #import "Common/RTCUIApplicationStatusObserver.h"
28 #endif 28 #endif
29 29
30 namespace webrtc {
31 namespace {
32
33 static const int64_t kMsPerSec = 1000;
34
35 // Convenience function for creating a dictionary.
36 inline CFDictionaryRef CreateCFDictionary(CFTypeRef* keys,
37 CFTypeRef* values,
38 size_t size) {
39 return CFDictionaryCreate(nullptr, keys, values, size,
40 &kCFTypeDictionaryKeyCallBacks,
41 &kCFTypeDictionaryValueCallBacks);
42 }
43
44 // Struct that we pass to the decoder per frame to decode. We receive it again 30 // Struct that we pass to the decoder per frame to decode. We receive it again
45 // in the decoder callback. 31 // in the decoder callback.
46 struct FrameDecodeParams { 32 struct RTCFrameDecodeParams {
47 FrameDecodeParams(DecodedImageCallback* cb, int64_t ts) 33 RTCFrameDecodeParams(RTCVideoDecoderCallback cb, int64_t ts) : callback(cb), t imestamp(ts) {}
48 : callback(cb), timestamp(ts) {} 34 RTCVideoDecoderCallback callback;
49 DecodedImageCallback* callback;
50 int64_t timestamp; 35 int64_t timestamp;
51 }; 36 };
52 37
53 // This is the callback function that VideoToolbox calls when decode is 38 // This is the callback function that VideoToolbox calls when decode is
54 // complete. 39 // complete.
55 void VTDecompressionOutputCallback(void* decoder, 40 void decompressionOutputCallback(void *decoder,
56 void* params, 41 void *params,
57 OSStatus status, 42 OSStatus status,
58 VTDecodeInfoFlags info_flags, 43 VTDecodeInfoFlags infoFlags,
59 CVImageBufferRef image_buffer, 44 CVImageBufferRef imageBuffer,
60 CMTime timestamp, 45 CMTime timestamp,
61 CMTime duration) { 46 CMTime duration) {
62 std::unique_ptr<FrameDecodeParams> decode_params( 47 std::unique_ptr<RTCFrameDecodeParams> decodeParams(
63 reinterpret_cast<FrameDecodeParams*>(params)); 48 reinterpret_cast<RTCFrameDecodeParams *>(params));
64 if (status != noErr) { 49 if (status != noErr) {
65 LOG(LS_ERROR) << "Failed to decode frame. Status: " << status; 50 LOG(LS_ERROR) << "Failed to decode frame. Status: " << status;
66 return; 51 return;
67 } 52 }
68 // TODO(tkchin): Handle CVO properly. 53 // TODO(tkchin): Handle CVO properly.
69 rtc::scoped_refptr<VideoFrameBuffer> buffer = new rtc::RefCountedObject<ObjCFr ameBuffer>( 54 RTCCVPixelBuffer *frameBuffer = [[RTCCVPixelBuffer alloc] initWithPixelBuffer: imageBuffer];
70 [[RTCCVPixelBuffer alloc] initWithPixelBuffer:image_buffer]); 55 RTCVideoFrame *decodedFrame =
71 VideoFrame decoded_frame(buffer, decode_params->timestamp, 56 [[RTCVideoFrame alloc] initWithBuffer:frameBuffer
72 CMTimeGetSeconds(timestamp) * kMsPerSec, 57 rotation:RTCVideoRotation_0
73 kVideoRotation_0); 58 timeStampNs:CMTimeGetSeconds(timestamp) * rtc::k NumNanosecsPerSec];
74 decode_params->callback->Decoded(decoded_frame); 59 decodedFrame.timeStamp = decodeParams->timestamp;
60 decodeParams->callback(decodedFrame);
75 } 61 }
76 62
77 } // namespace 63 // Decoder.
78 64 @implementation RTCVideoDecoderH264 {
79 H264VideoToolboxDecoder::H264VideoToolboxDecoder() 65 CMVideoFormatDescriptionRef _videoFormat;
80 : callback_(nullptr), video_format_(nullptr), decompression_session_(nullptr ) {} 66 VTDecompressionSessionRef _decompressionSession;
81 67 RTCVideoDecoderCallback _callback;
82 H264VideoToolboxDecoder::~H264VideoToolboxDecoder() {
83 DestroyDecompressionSession();
84 SetVideoFormat(nullptr);
85 } 68 }
86 69
87 int H264VideoToolboxDecoder::InitDecode(const VideoCodec* video_codec, 70 - (void)dealloc {
88 int number_of_cores) { 71 [self destroyDecompressionSession];
72 [self setVideoFormat:nullptr];
73 }
74
75 - (NSInteger)startDecodeWithSettings:(RTCVideoEncoderSettings *)settings
76 numberOfCores:(int)numberOfCores {
89 return WEBRTC_VIDEO_CODEC_OK; 77 return WEBRTC_VIDEO_CODEC_OK;
90 } 78 }
91 79
92 int H264VideoToolboxDecoder::Decode( 80 - (NSInteger)decode:(RTCEncodedImage *)inputImage
93 const EncodedImage& input_image, 81 missingFrames:(BOOL)missingFrames
94 bool missing_frames, 82 fragmentationHeader:(RTCRtpFragmentationHeader *)fragmentationHeader
95 const RTPFragmentationHeader* fragmentation, 83 codecSpecificInfo:(__nullable id<RTCCodecSpecificInfo>)info
96 const CodecSpecificInfo* codec_specific_info, 84 renderTimeMs:(int64_t)renderTimeMs {
97 int64_t render_time_ms) { 85 RTC_DCHECK(inputImage.buffer);
98 RTC_DCHECK(input_image._buffer);
99 86
100 #if defined(WEBRTC_IOS) 87 #if defined(WEBRTC_IOS)
101 if (![[RTCUIApplicationStatusObserver sharedInstance] isApplicationActive]) { 88 if (![[RTCUIApplicationStatusObserver sharedInstance] isApplicationActive]) {
102 // Ignore all decode requests when app isn't active. In this state, the 89 // Ignore all decode requests when app isn't active. In this state, the
103 // hardware decoder has been invalidated by the OS. 90 // hardware decoder has been invalidated by the OS.
104 // Reset video format so that we won't process frames until the next 91 // Reset video format so that we won't process frames until the next
105 // keyframe. 92 // keyframe.
106 SetVideoFormat(nullptr); 93 [self setVideoFormat:nullptr];
107 return WEBRTC_VIDEO_CODEC_NO_OUTPUT; 94 return WEBRTC_VIDEO_CODEC_NO_OUTPUT;
108 } 95 }
109 #endif 96 #endif
110 CMVideoFormatDescriptionRef input_format = nullptr; 97 CMVideoFormatDescriptionRef inputFormat = nullptr;
111 if (H264AnnexBBufferHasVideoFormatDescription(input_image._buffer, 98 if (webrtc::H264AnnexBBufferHasVideoFormatDescription((uint8_t *)inputImage.bu ffer.bytes,
112 input_image._length)) { 99 inputImage.buffer.length )) {
113 input_format = CreateVideoFormatDescription(input_image._buffer, 100 inputFormat = webrtc::CreateVideoFormatDescription((uint8_t *)inputImage.buf fer.bytes,
114 input_image._length); 101 inputImage.buffer.length) ;
115 if (input_format) { 102 if (inputFormat) {
116 // Check if the video format has changed, and reinitialize decoder if 103 // Check if the video format has changed, and reinitialize decoder if
117 // needed. 104 // needed.
118 if (!CMFormatDescriptionEqual(input_format, video_format_)) { 105 if (!CMFormatDescriptionEqual(inputFormat, _videoFormat)) {
119 SetVideoFormat(input_format); 106 [self setVideoFormat:inputFormat];
120 ResetDecompressionSession(); 107 [self resetDecompressionSession];
121 } 108 }
122 CFRelease(input_format); 109 CFRelease(inputFormat);
123 } 110 }
124 } 111 }
125 if (!video_format_) { 112 if (!_videoFormat) {
126 // We received a frame but we don't have format information so we can't 113 // We received a frame but we don't have format information so we can't
127 // decode it. 114 // decode it.
128 // This can happen after backgrounding. We need to wait for the next 115 // This can happen after backgrounding. We need to wait for the next
129 // sps/pps before we can resume so we request a keyframe by returning an 116 // sps/pps before we can resume so we request a keyframe by returning an
130 // error. 117 // error.
131 LOG(LS_WARNING) << "Missing video format. Frame with sps/pps required."; 118 LOG(LS_WARNING) << "Missing video format. Frame with sps/pps required.";
132 return WEBRTC_VIDEO_CODEC_ERROR; 119 return WEBRTC_VIDEO_CODEC_ERROR;
133 } 120 }
134 CMSampleBufferRef sample_buffer = nullptr; 121 CMSampleBufferRef sampleBuffer = nullptr;
135 if (!H264AnnexBBufferToCMSampleBuffer(input_image._buffer, 122 if (!webrtc::H264AnnexBBufferToCMSampleBuffer((uint8_t *)inputImage.buffer.byt es,
136 input_image._length, video_format_, 123 inputImage.buffer.length,
137 &sample_buffer)) { 124 _videoFormat,
125 &sampleBuffer)) {
138 return WEBRTC_VIDEO_CODEC_ERROR; 126 return WEBRTC_VIDEO_CODEC_ERROR;
139 } 127 }
140 RTC_DCHECK(sample_buffer); 128 RTC_DCHECK(sampleBuffer);
141 VTDecodeFrameFlags decode_flags = 129 VTDecodeFrameFlags decodeFlags = kVTDecodeFrame_EnableAsynchronousDecompressio n;
142 kVTDecodeFrame_EnableAsynchronousDecompression; 130 std::unique_ptr<RTCFrameDecodeParams> frameDecodeParams;
143 std::unique_ptr<FrameDecodeParams> frame_decode_params; 131 frameDecodeParams.reset(new RTCFrameDecodeParams(_callback, inputImage.timeSta mp));
144 frame_decode_params.reset(
145 new FrameDecodeParams(callback_, input_image._timeStamp));
146 OSStatus status = VTDecompressionSessionDecodeFrame( 132 OSStatus status = VTDecompressionSessionDecodeFrame(
147 decompression_session_, sample_buffer, decode_flags, 133 _decompressionSession, sampleBuffer, decodeFlags, frameDecodeParams.releas e(), nullptr);
148 frame_decode_params.release(), nullptr);
149 #if defined(WEBRTC_IOS) 134 #if defined(WEBRTC_IOS)
150 // Re-initialize the decoder if we have an invalid session while the app is 135 // Re-initialize the decoder if we have an invalid session while the app is
151 // active and retry the decode request. 136 // active and retry the decode request.
152 if (status == kVTInvalidSessionErr && 137 if (status == kVTInvalidSessionErr && [self resetDecompressionSession] == WEBR TC_VIDEO_CODEC_OK) {
153 ResetDecompressionSession() == WEBRTC_VIDEO_CODEC_OK) { 138 frameDecodeParams.reset(new RTCFrameDecodeParams(_callback, inputImage.timeS tamp));
154 frame_decode_params.reset(
155 new FrameDecodeParams(callback_, input_image._timeStamp));
156 status = VTDecompressionSessionDecodeFrame( 139 status = VTDecompressionSessionDecodeFrame(
157 decompression_session_, sample_buffer, decode_flags, 140 _decompressionSession, sampleBuffer, decodeFlags, frameDecodeParams.rele ase(), nullptr);
158 frame_decode_params.release(), nullptr);
159 } 141 }
160 #endif 142 #endif
161 CFRelease(sample_buffer); 143 CFRelease(sampleBuffer);
162 if (status != noErr) { 144 if (status != noErr) {
163 LOG(LS_ERROR) << "Failed to decode frame with code: " << status; 145 LOG(LS_ERROR) << "Failed to decode frame with code: " << status;
164 return WEBRTC_VIDEO_CODEC_ERROR; 146 return WEBRTC_VIDEO_CODEC_ERROR;
165 } 147 }
166 return WEBRTC_VIDEO_CODEC_OK; 148 return WEBRTC_VIDEO_CODEC_OK;
167 } 149 }
168 150
169 int H264VideoToolboxDecoder::RegisterDecodeCompleteCallback( 151 - (void)setCallback:(RTCVideoDecoderCallback)callback {
170 DecodedImageCallback* callback) { 152 _callback = callback;
171 RTC_DCHECK(!callback_); 153 }
172 callback_ = callback; 154
155 - (NSInteger)releaseDecoder {
156 // Need to invalidate the session so that callbacks no longer occur and it
157 // is safe to null out the callback.
158 [self destroyDecompressionSession];
159 [self setVideoFormat:nullptr];
160 _callback = nullptr;
173 return WEBRTC_VIDEO_CODEC_OK; 161 return WEBRTC_VIDEO_CODEC_OK;
174 } 162 }
175 163
176 int H264VideoToolboxDecoder::Release() { 164 #pragma mark - Private
177 // Need to invalidate the session so that callbacks no longer occur and it
178 // is safe to null out the callback.
179 DestroyDecompressionSession();
180 SetVideoFormat(nullptr);
181 callback_ = nullptr;
182 return WEBRTC_VIDEO_CODEC_OK;
183 }
184 165
185 int H264VideoToolboxDecoder::ResetDecompressionSession() { 166 - (int)resetDecompressionSession {
186 DestroyDecompressionSession(); 167 [self destroyDecompressionSession];
187 168
188 // Need to wait for the first SPS to initialize decoder. 169 // Need to wait for the first SPS to initialize decoder.
189 if (!video_format_) { 170 if (!_videoFormat) {
190 return WEBRTC_VIDEO_CODEC_OK; 171 return WEBRTC_VIDEO_CODEC_OK;
191 } 172 }
192 173
193 // Set keys for OpenGL and IOSurface compatibilty, which makes the encoder 174 // Set keys for OpenGL and IOSurface compatibilty, which makes the encoder
194 // create pixel buffers with GPU backed memory. The intent here is to pass 175 // create pixel buffers with GPU backed memory. The intent here is to pass
195 // the pixel buffers directly so we avoid a texture upload later during 176 // the pixel buffers directly so we avoid a texture upload later during
196 // rendering. This currently is moot because we are converting back to an 177 // rendering. This currently is moot because we are converting back to an
197 // I420 frame after decode, but eventually we will be able to plumb 178 // I420 frame after decode, but eventually we will be able to plumb
198 // CVPixelBuffers directly to the renderer. 179 // CVPixelBuffers directly to the renderer.
199 // TODO(tkchin): Maybe only set OpenGL/IOSurface keys if we know that that 180 // TODO(tkchin): Maybe only set OpenGL/IOSurface keys if we know that that
200 // we can pass CVPixelBuffers as native handles in decoder output. 181 // we can pass CVPixelBuffers as native handles in decoder output.
201 static size_t const attributes_size = 3; 182 static size_t const attributesSize = 3;
202 CFTypeRef keys[attributes_size] = { 183 CFTypeRef keys[attributesSize] = {
203 #if defined(WEBRTC_IOS) 184 #if defined(WEBRTC_IOS)
204 kCVPixelBufferOpenGLESCompatibilityKey, 185 kCVPixelBufferOpenGLESCompatibilityKey,
205 #elif defined(WEBRTC_MAC) 186 #elif defined(WEBRTC_MAC)
206 kCVPixelBufferOpenGLCompatibilityKey, 187 kCVPixelBufferOpenGLCompatibilityKey,
207 #endif 188 #endif
208 kCVPixelBufferIOSurfacePropertiesKey, 189 kCVPixelBufferIOSurfacePropertiesKey,
209 kCVPixelBufferPixelFormatTypeKey 190 kCVPixelBufferPixelFormatTypeKey
210 }; 191 };
211 CFDictionaryRef io_surface_value = CreateCFDictionary(nullptr, nullptr, 0); 192 CFDictionaryRef ioSurfaceValue = CreateCFTypeDictionary(nullptr, nullptr, 0);
212 int64_t nv12type = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange; 193 int64_t nv12type = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange;
213 CFNumberRef pixel_format = 194 CFNumberRef pixelFormat = CFNumberCreate(nullptr, kCFNumberLongType, &nv12type );
214 CFNumberCreate(nullptr, kCFNumberLongType, &nv12type); 195 CFTypeRef values[attributesSize] = {kCFBooleanTrue, ioSurfaceValue, pixelForma t};
215 CFTypeRef values[attributes_size] = {kCFBooleanTrue, io_surface_value, 196 CFDictionaryRef attributes = CreateCFTypeDictionary(keys, values, attributesSi ze);
216 pixel_format}; 197 if (ioSurfaceValue) {
217 CFDictionaryRef attributes = 198 CFRelease(ioSurfaceValue);
218 CreateCFDictionary(keys, values, attributes_size); 199 ioSurfaceValue = nullptr;
219 if (io_surface_value) {
220 CFRelease(io_surface_value);
221 io_surface_value = nullptr;
222 } 200 }
223 if (pixel_format) { 201 if (pixelFormat) {
224 CFRelease(pixel_format); 202 CFRelease(pixelFormat);
225 pixel_format = nullptr; 203 pixelFormat = nullptr;
226 } 204 }
227 VTDecompressionOutputCallbackRecord record = { 205 VTDecompressionOutputCallbackRecord record = {
228 VTDecompressionOutputCallback, this, 206 decompressionOutputCallback, nullptr,
229 }; 207 };
230 OSStatus status = 208 OSStatus status = VTDecompressionSessionCreate(
231 VTDecompressionSessionCreate(nullptr, video_format_, nullptr, attributes, 209 nullptr, _videoFormat, nullptr, attributes, &record, &_decompressionSessio n);
232 &record, &decompression_session_);
233 CFRelease(attributes); 210 CFRelease(attributes);
234 if (status != noErr) { 211 if (status != noErr) {
235 DestroyDecompressionSession(); 212 [self destroyDecompressionSession];
236 return WEBRTC_VIDEO_CODEC_ERROR; 213 return WEBRTC_VIDEO_CODEC_ERROR;
237 } 214 }
238 ConfigureDecompressionSession(); 215 [self configureDecompressionSession];
239 216
240 return WEBRTC_VIDEO_CODEC_OK; 217 return WEBRTC_VIDEO_CODEC_OK;
241 } 218 }
242 219
243 void H264VideoToolboxDecoder::ConfigureDecompressionSession() { 220 - (void)configureDecompressionSession {
244 RTC_DCHECK(decompression_session_); 221 RTC_DCHECK(_decompressionSession);
245 #if defined(WEBRTC_IOS) 222 #if defined(WEBRTC_IOS)
246 VTSessionSetProperty(decompression_session_, 223 VTSessionSetProperty(_decompressionSession, kVTDecompressionPropertyKey_RealTi me, kCFBooleanTrue);
247 kVTDecompressionPropertyKey_RealTime, kCFBooleanTrue);
248 #endif 224 #endif
249 } 225 }
250 226
251 void H264VideoToolboxDecoder::DestroyDecompressionSession() { 227 - (void)destroyDecompressionSession {
252 if (decompression_session_) { 228 if (_decompressionSession) {
253 VTDecompressionSessionInvalidate(decompression_session_); 229 VTDecompressionSessionInvalidate(_decompressionSession);
254 CFRelease(decompression_session_); 230 CFRelease(_decompressionSession);
255 decompression_session_ = nullptr; 231 _decompressionSession = nullptr;
256 } 232 }
257 } 233 }
258 234
259 void H264VideoToolboxDecoder::SetVideoFormat( 235 - (void)setVideoFormat:(CMVideoFormatDescriptionRef)videoFormat {
260 CMVideoFormatDescriptionRef video_format) { 236 if (_videoFormat == videoFormat) {
261 if (video_format_ == video_format) {
262 return; 237 return;
263 } 238 }
264 if (video_format_) { 239 if (_videoFormat) {
265 CFRelease(video_format_); 240 CFRelease(_videoFormat);
266 } 241 }
267 video_format_ = video_format; 242 _videoFormat = videoFormat;
268 if (video_format_) { 243 if (_videoFormat) {
269 CFRetain(video_format_); 244 CFRetain(_videoFormat);
270 } 245 }
271 } 246 }
272 247
273 const char* H264VideoToolboxDecoder::ImplementationName() const { 248 - (NSString *)implementationName {
274 return "VideoToolbox"; 249 return @"VideoToolbox";
275 } 250 }
276 251
277 } // namespace webrtc 252 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698