Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
Anders Carlsson | 498644e | 2018-04-05 13:07:39 +0200 | [diff] [blame] | 11 | #import "RTCI420Buffer+Private.h" |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "api/video/i420_buffer.h" |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 14 | |
Anders Carlsson | 498644e | 2018-04-05 13:07:39 +0200 | [diff] [blame] | 15 | #if !defined(NDEBUG) && defined(WEBRTC_IOS) |
| 16 | #import <UIKit/UIKit.h> |
| 17 | #include "third_party/libyuv/include/libyuv.h" |
| 18 | #endif |
| 19 | |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 20 | @implementation RTCI420Buffer { |
| 21 | @protected |
| 22 | rtc::scoped_refptr<webrtc::I420BufferInterface> _i420Buffer; |
| 23 | } |
| 24 | |
| 25 | - (instancetype)initWithWidth:(int)width height:(int)height { |
| 26 | if (self = [super init]) { |
| 27 | _i420Buffer = webrtc::I420Buffer::Create(width, height); |
| 28 | } |
| 29 | |
| 30 | return self; |
| 31 | } |
| 32 | |
| 33 | - (instancetype)initWithWidth:(int)width |
| 34 | height:(int)height |
Piotr (Peter) Slatala | 0b71c29 | 2018-04-18 12:42:30 -0700 | [diff] [blame^] | 35 | dataY:(const uint8_t *)dataY |
| 36 | dataU:(const uint8_t *)dataU |
| 37 | dataV:(const uint8_t *)dataV { |
| 38 | if (self = [super init]) { |
| 39 | _i420Buffer = webrtc::I420Buffer::Copy( |
| 40 | width, height, dataY, width, dataU, (width + 1) / 2, dataV, (width + 1) / 2); |
| 41 | } |
| 42 | return self; |
| 43 | } |
| 44 | |
| 45 | - (instancetype)initWithWidth:(int)width |
| 46 | height:(int)height |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 47 | strideY:(int)strideY |
| 48 | strideU:(int)strideU |
| 49 | strideV:(int)strideV { |
| 50 | if (self = [super init]) { |
| 51 | _i420Buffer = webrtc::I420Buffer::Create(width, height, strideY, strideU, strideV); |
| 52 | } |
| 53 | |
| 54 | return self; |
| 55 | } |
| 56 | |
| 57 | - (instancetype)initWithFrameBuffer:(rtc::scoped_refptr<webrtc::I420BufferInterface>)i420Buffer { |
| 58 | if (self = [super init]) { |
| 59 | _i420Buffer = i420Buffer; |
| 60 | } |
| 61 | |
| 62 | return self; |
| 63 | } |
| 64 | |
| 65 | - (int)width { |
| 66 | return _i420Buffer->width(); |
| 67 | } |
| 68 | |
| 69 | - (int)height { |
| 70 | return _i420Buffer->height(); |
| 71 | } |
| 72 | |
| 73 | - (int)strideY { |
| 74 | return _i420Buffer->StrideY(); |
| 75 | } |
| 76 | |
| 77 | - (int)strideU { |
| 78 | return _i420Buffer->StrideU(); |
| 79 | } |
| 80 | |
| 81 | - (int)strideV { |
| 82 | return _i420Buffer->StrideV(); |
| 83 | } |
| 84 | |
| 85 | - (int)chromaWidth { |
| 86 | return _i420Buffer->ChromaWidth(); |
| 87 | } |
| 88 | |
| 89 | - (int)chromaHeight { |
| 90 | return _i420Buffer->ChromaHeight(); |
| 91 | } |
| 92 | |
| 93 | - (const uint8_t *)dataY { |
| 94 | return _i420Buffer->DataY(); |
| 95 | } |
| 96 | |
| 97 | - (const uint8_t *)dataU { |
| 98 | return _i420Buffer->DataU(); |
| 99 | } |
| 100 | |
| 101 | - (const uint8_t *)dataV { |
| 102 | return _i420Buffer->DataV(); |
| 103 | } |
| 104 | |
| 105 | - (id<RTCI420Buffer>)toI420 { |
| 106 | return self; |
| 107 | } |
| 108 | |
Anders Carlsson | 498644e | 2018-04-05 13:07:39 +0200 | [diff] [blame] | 109 | #pragma mark - Private |
| 110 | |
Anders Carlsson | fe9d817 | 2018-04-03 11:40:39 +0200 | [diff] [blame] | 111 | - (rtc::scoped_refptr<webrtc::I420BufferInterface>)nativeI420Buffer { |
| 112 | return _i420Buffer; |
| 113 | } |
| 114 | |
Anders Carlsson | 498644e | 2018-04-05 13:07:39 +0200 | [diff] [blame] | 115 | #pragma mark - Debugging |
| 116 | |
| 117 | #if !defined(NDEBUG) && defined(WEBRTC_IOS) |
| 118 | - (id)debugQuickLookObject { |
| 119 | UIGraphicsBeginImageContext(CGSizeMake(_i420Buffer->width(), _i420Buffer->height())); |
| 120 | CGContextRef c = UIGraphicsGetCurrentContext(); |
| 121 | uint8_t *ctxData = (uint8_t *)CGBitmapContextGetData(c); |
| 122 | |
| 123 | libyuv::I420ToARGB(_i420Buffer->DataY(), |
| 124 | _i420Buffer->StrideY(), |
| 125 | _i420Buffer->DataU(), |
| 126 | _i420Buffer->StrideU(), |
| 127 | _i420Buffer->DataV(), |
| 128 | _i420Buffer->StrideV(), |
| 129 | ctxData, |
| 130 | _i420Buffer->width() * 4, |
| 131 | _i420Buffer->width(), |
| 132 | _i420Buffer->height()); |
| 133 | |
| 134 | UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); |
| 135 | UIGraphicsEndImageContext(); |
| 136 | |
| 137 | return image; |
| 138 | } |
| 139 | #endif |
| 140 | |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 141 | @end |
| 142 | |
Anders Carlsson | 498644e | 2018-04-05 13:07:39 +0200 | [diff] [blame] | 143 | #pragma mark - |
| 144 | |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 145 | @implementation RTCMutableI420Buffer |
| 146 | |
| 147 | - (uint8_t *)mutableDataY { |
| 148 | return static_cast<webrtc::I420Buffer *>(_i420Buffer.get())->MutableDataY(); |
| 149 | } |
| 150 | |
| 151 | - (uint8_t *)mutableDataU { |
| 152 | return static_cast<webrtc::I420Buffer *>(_i420Buffer.get())->MutableDataU(); |
| 153 | } |
| 154 | |
| 155 | - (uint8_t *)mutableDataV { |
| 156 | return static_cast<webrtc::I420Buffer *>(_i420Buffer.get())->MutableDataV(); |
| 157 | } |
| 158 | |
| 159 | @end |