blob: 65f3d5ad411b5b1def1fd92227d1d88803950e61 [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
35 strideY:(int)strideY
36 strideU:(int)strideU
37 strideV:(int)strideV {
38 if (self = [super init]) {
39 _i420Buffer = webrtc::I420Buffer::Create(width, height, strideY, strideU, strideV);
40 }
41
42 return self;
43}
44
45- (instancetype)initWithFrameBuffer:(rtc::scoped_refptr<webrtc::I420BufferInterface>)i420Buffer {
46 if (self = [super init]) {
47 _i420Buffer = i420Buffer;
48 }
49
50 return self;
51}
52
53- (int)width {
54 return _i420Buffer->width();
55}
56
57- (int)height {
58 return _i420Buffer->height();
59}
60
61- (int)strideY {
62 return _i420Buffer->StrideY();
63}
64
65- (int)strideU {
66 return _i420Buffer->StrideU();
67}
68
69- (int)strideV {
70 return _i420Buffer->StrideV();
71}
72
73- (int)chromaWidth {
74 return _i420Buffer->ChromaWidth();
75}
76
77- (int)chromaHeight {
78 return _i420Buffer->ChromaHeight();
79}
80
81- (const uint8_t *)dataY {
82 return _i420Buffer->DataY();
83}
84
85- (const uint8_t *)dataU {
86 return _i420Buffer->DataU();
87}
88
89- (const uint8_t *)dataV {
90 return _i420Buffer->DataV();
91}
92
93- (id<RTCI420Buffer>)toI420 {
94 return self;
95}
96
Anders Carlsson498644e2018-04-05 13:07:39 +020097#pragma mark - Private
98
Anders Carlssonfe9d8172018-04-03 11:40:39 +020099- (rtc::scoped_refptr<webrtc::I420BufferInterface>)nativeI420Buffer {
100 return _i420Buffer;
101}
102
Anders Carlsson498644e2018-04-05 13:07:39 +0200103#pragma mark - Debugging
104
105#if !defined(NDEBUG) && defined(WEBRTC_IOS)
106- (id)debugQuickLookObject {
107 UIGraphicsBeginImageContext(CGSizeMake(_i420Buffer->width(), _i420Buffer->height()));
108 CGContextRef c = UIGraphicsGetCurrentContext();
109 uint8_t *ctxData = (uint8_t *)CGBitmapContextGetData(c);
110
111 libyuv::I420ToARGB(_i420Buffer->DataY(),
112 _i420Buffer->StrideY(),
113 _i420Buffer->DataU(),
114 _i420Buffer->StrideU(),
115 _i420Buffer->DataV(),
116 _i420Buffer->StrideV(),
117 ctxData,
118 _i420Buffer->width() * 4,
119 _i420Buffer->width(),
120 _i420Buffer->height());
121
122 UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
123 UIGraphicsEndImageContext();
124
125 return image;
126}
127#endif
128
Anders Carlssone5960ce2017-06-22 15:26:30 +0200129@end
130
Anders Carlsson498644e2018-04-05 13:07:39 +0200131#pragma mark -
132
Anders Carlssone5960ce2017-06-22 15:26:30 +0200133@implementation RTCMutableI420Buffer
134
135- (uint8_t *)mutableDataY {
136 return static_cast<webrtc::I420Buffer *>(_i420Buffer.get())->MutableDataY();
137}
138
139- (uint8_t *)mutableDataU {
140 return static_cast<webrtc::I420Buffer *>(_i420Buffer.get())->MutableDataU();
141}
142
143- (uint8_t *)mutableDataV {
144 return static_cast<webrtc::I420Buffer *>(_i420Buffer.get())->MutableDataV();
145}
146
147@end