Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 1 | /* |
2 | * Copyright 2015 The WebRTC project authors. All Rights Reserved. | ||||
3 | * | ||||
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 | ||||
6 | * tree. An additional intellectual property rights grant can be found | ||||
7 | * in the file PATENTS. All contributing project authors may | ||||
8 | * be found in the AUTHORS file in the root of the source tree. | ||||
9 | */ | ||||
10 | |||||
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 11 | #import "RTCVideoFrame+Private.h" |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 12 | |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 13 | #include <memory> |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 14 | |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame^] | 15 | #include "webrtc/common_video/rotation.h" |
16 | |||||
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 17 | @implementation RTCVideoFrame { |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame^] | 18 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> _videoBuffer; |
19 | webrtc::VideoRotation _rotation; | ||||
20 | int64_t _timeStampNs; | ||||
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 21 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> _i420Buffer; |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 22 | } |
23 | |||||
24 | - (size_t)width { | ||||
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame^] | 25 | return _videoBuffer->width(); |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 26 | } |
27 | |||||
28 | - (size_t)height { | ||||
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame^] | 29 | return _videoBuffer->height(); |
30 | } | ||||
31 | |||||
32 | - (int)rotation { | ||||
33 | return static_cast<int>(_rotation); | ||||
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 34 | } |
35 | |||||
nisse | 71a0c2f | 2016-04-04 00:57:29 -0700 | [diff] [blame] | 36 | // TODO(nisse): chromaWidth and chromaHeight are used only in |
37 | // RTCOpenGLVideoRenderer.mm. Update, and then delete these | ||||
38 | // properties. | ||||
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 39 | - (size_t)chromaWidth { |
nisse | 71a0c2f | 2016-04-04 00:57:29 -0700 | [diff] [blame] | 40 | return (self.width + 1) / 2; |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 41 | } |
42 | |||||
43 | - (size_t)chromaHeight { | ||||
nisse | 71a0c2f | 2016-04-04 00:57:29 -0700 | [diff] [blame] | 44 | return (self.height + 1) / 2; |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 45 | } |
46 | |||||
47 | - (const uint8_t *)yPlane { | ||||
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 48 | if (!self.i420Buffer) { |
49 | return nullptr; | ||||
50 | } | ||||
nisse | ca6d5d1 | 2016-06-17 05:03:04 -0700 | [diff] [blame] | 51 | return self.i420Buffer->DataY(); |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 52 | } |
53 | |||||
54 | - (const uint8_t *)uPlane { | ||||
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 55 | if (!self.i420Buffer) { |
56 | return nullptr; | ||||
57 | } | ||||
nisse | ca6d5d1 | 2016-06-17 05:03:04 -0700 | [diff] [blame] | 58 | return self.i420Buffer->DataU(); |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 59 | } |
60 | |||||
61 | - (const uint8_t *)vPlane { | ||||
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 62 | if (!self.i420Buffer) { |
63 | return nullptr; | ||||
64 | } | ||||
nisse | ca6d5d1 | 2016-06-17 05:03:04 -0700 | [diff] [blame] | 65 | return self.i420Buffer->DataV(); |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 66 | } |
67 | |||||
68 | - (int32_t)yPitch { | ||||
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 69 | if (!self.i420Buffer) { |
70 | return 0; | ||||
71 | } | ||||
nisse | ca6d5d1 | 2016-06-17 05:03:04 -0700 | [diff] [blame] | 72 | return self.i420Buffer->StrideY(); |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 73 | } |
74 | |||||
75 | - (int32_t)uPitch { | ||||
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 76 | if (!self.i420Buffer) { |
77 | return 0; | ||||
78 | } | ||||
nisse | ca6d5d1 | 2016-06-17 05:03:04 -0700 | [diff] [blame] | 79 | return self.i420Buffer->StrideU(); |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 80 | } |
81 | |||||
82 | - (int32_t)vPitch { | ||||
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 83 | if (!self.i420Buffer) { |
84 | return 0; | ||||
85 | } | ||||
nisse | ca6d5d1 | 2016-06-17 05:03:04 -0700 | [diff] [blame] | 86 | return self.i420Buffer->StrideV(); |
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 87 | } |
88 | |||||
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame^] | 89 | - (int64_t)timeStampNs { |
90 | return _timeStampNs; | ||||
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 91 | } |
92 | |||||
93 | - (CVPixelBufferRef)nativeHandle { | ||||
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame^] | 94 | return static_cast<CVPixelBufferRef>(_videoBuffer->native_handle()); |
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 95 | } |
96 | |||||
97 | - (void)convertBufferIfNeeded { | ||||
98 | if (!_i420Buffer) { | ||||
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame^] | 99 | _i420Buffer = _videoBuffer->native_handle() |
100 | ? _videoBuffer->NativeToI420Buffer() | ||||
101 | : _videoBuffer; | ||||
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 102 | } |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 103 | } |
104 | |||||
105 | #pragma mark - Private | ||||
106 | |||||
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame^] | 107 | - (instancetype)initWithVideoBuffer: |
108 | (rtc::scoped_refptr<webrtc::VideoFrameBuffer>)videoBuffer | ||||
109 | rotation:(webrtc::VideoRotation)rotation | ||||
110 | timeStampNs:(int64_t)timeStampNs { | ||||
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 111 | if (self = [super init]) { |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame^] | 112 | _videoBuffer = videoBuffer; |
113 | _rotation = rotation; | ||||
114 | _timeStampNs = timeStampNs; | ||||
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 115 | } |
116 | return self; | ||||
117 | } | ||||
118 | |||||
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 119 | - (rtc::scoped_refptr<webrtc::VideoFrameBuffer>)i420Buffer { |
120 | [self convertBufferIfNeeded]; | ||||
121 | return _i420Buffer; | ||||
122 | } | ||||
123 | |||||
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 124 | @end |