blob: 3d0538e572ce93ef299e9787d70e492ffb7b47dc [file] [log] [blame]
Anders Carlssone5960ce2017-06-22 15:26:30 +02001/*
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 Carlsson498644e2018-04-05 13:07:39 +020011#import "RTCI420Buffer+Private.h"
Anders Carlssone5960ce2017-06-22 15:26:30 +020012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "api/video/i420_buffer.h"
Anders Carlssone5960ce2017-06-22 15:26:30 +020014
Anders Carlsson498644e2018-04-05 13:07:39 +020015#if !defined(NDEBUG) && defined(WEBRTC_IOS)
16#import <UIKit/UIKit.h>
17#include "third_party/libyuv/include/libyuv.h"
18#endif
19
Anders Carlssone5960ce2017-06-22 15:26:30 +020020@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) Slatala0b71c292018-04-18 12:42:30 -070035 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 Carlssone5960ce2017-06-22 15:26:30 +020047 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 Carlsson498644e2018-04-05 13:07:39 +0200109#pragma mark - Private
110
Anders Carlssonfe9d8172018-04-03 11:40:39 +0200111- (rtc::scoped_refptr<webrtc::I420BufferInterface>)nativeI420Buffer {
112 return _i420Buffer;
113}
114
Anders Carlsson498644e2018-04-05 13:07:39 +0200115#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 Carlssone5960ce2017-06-22 15:26:30 +0200141@end
142
Anders Carlsson498644e2018-04-05 13:07:39 +0200143#pragma mark -
144
Anders Carlssone5960ce2017-06-22 15:26:30 +0200145@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