blob: b2ba15885ff4a0102bfca62e6199a858ca7ac375 [file] [log] [blame]
andrew@webrtc.orgb015cbe2012-10-22 18:19:231/*
2 * Copyright (c) 2012 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
pbos@webrtc.org026d1ce2013-06-04 09:02:3711#include "webrtc/common_video/interface/i420_video_frame.h"
andrew@webrtc.orgb015cbe2012-10-22 18:19:2312
wuchengli@chromium.org1bdf1862014-05-28 07:00:5113#include <string.h>
14
andrew@webrtc.orgb015cbe2012-10-22 18:19:2315#include <algorithm> // swap
16
17namespace webrtc {
18
19I420VideoFrame::I420VideoFrame()
20 : width_(0),
21 height_(0),
22 timestamp_(0),
wu@webrtc.org9d107692014-04-15 17:46:3323 ntp_time_ms_(0),
guoweis@webrtc.orgfd7343a2015-02-11 18:37:5424 render_time_ms_(0),
25 rotation_(kVideoRotation_0) {
26}
andrew@webrtc.orgb015cbe2012-10-22 18:19:2327
28I420VideoFrame::~I420VideoFrame() {}
29
30int I420VideoFrame::CreateEmptyFrame(int width, int height,
31 int stride_y, int stride_u, int stride_v) {
32 if (CheckDimensions(width, height, stride_y, stride_u, stride_v) < 0)
33 return -1;
34 int size_y = stride_y * height;
35 int half_height = (height + 1) / 2;
36 int size_u = stride_u * half_height;
37 int size_v = stride_v * half_height;
38 width_ = width;
39 height_ = height;
40 y_plane_.CreateEmptyPlane(size_y, stride_y, size_y);
41 u_plane_.CreateEmptyPlane(size_u, stride_u, size_u);
42 v_plane_.CreateEmptyPlane(size_v, stride_v, size_v);
mikhal@webrtc.org3bbed742012-10-24 18:33:0443 // Creating empty frame - reset all values.
44 timestamp_ = 0;
wu@webrtc.org9d107692014-04-15 17:46:3345 ntp_time_ms_ = 0;
mikhal@webrtc.org3bbed742012-10-24 18:33:0446 render_time_ms_ = 0;
guoweis@webrtc.orgfd7343a2015-02-11 18:37:5447 rotation_ = kVideoRotation_0;
andrew@webrtc.orgb015cbe2012-10-22 18:19:2348 return 0;
49}
50
51int I420VideoFrame::CreateFrame(int size_y, const uint8_t* buffer_y,
52 int size_u, const uint8_t* buffer_u,
53 int size_v, const uint8_t* buffer_v,
54 int width, int height,
55 int stride_y, int stride_u, int stride_v) {
guoweis@webrtc.orgfd7343a2015-02-11 18:37:5456 return CreateFrame(size_y, buffer_y, size_u, buffer_u, size_v, buffer_v,
57 width, height, stride_y, stride_u, stride_v,
58 kVideoRotation_0);
59}
60
61int I420VideoFrame::CreateFrame(int size_y,
62 const uint8_t* buffer_y,
63 int size_u,
64 const uint8_t* buffer_u,
65 int size_v,
66 const uint8_t* buffer_v,
67 int width,
68 int height,
69 int stride_y,
70 int stride_u,
71 int stride_v,
72 VideoRotation rotation) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:2373 if (size_y < 1 || size_u < 1 || size_v < 1)
74 return -1;
75 if (CheckDimensions(width, height, stride_y, stride_u, stride_v) < 0)
76 return -1;
77 y_plane_.Copy(size_y, stride_y, buffer_y);
78 u_plane_.Copy(size_u, stride_u, buffer_u);
79 v_plane_.Copy(size_v, stride_v, buffer_v);
80 width_ = width;
81 height_ = height;
guoweis@webrtc.orgfd7343a2015-02-11 18:37:5482 rotation_ = rotation;
andrew@webrtc.orgb015cbe2012-10-22 18:19:2383 return 0;
84}
85
86int I420VideoFrame::CopyFrame(const I420VideoFrame& videoFrame) {
87 int ret = CreateFrame(videoFrame.allocated_size(kYPlane),
88 videoFrame.buffer(kYPlane),
89 videoFrame.allocated_size(kUPlane),
90 videoFrame.buffer(kUPlane),
91 videoFrame.allocated_size(kVPlane),
92 videoFrame.buffer(kVPlane),
93 videoFrame.width_, videoFrame.height_,
94 videoFrame.stride(kYPlane), videoFrame.stride(kUPlane),
95 videoFrame.stride(kVPlane));
96 if (ret < 0)
97 return ret;
98 timestamp_ = videoFrame.timestamp_;
wu@webrtc.org9d107692014-04-15 17:46:3399 ntp_time_ms_ = videoFrame.ntp_time_ms_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23100 render_time_ms_ = videoFrame.render_time_ms_;
guoweis@webrtc.orgfd7343a2015-02-11 18:37:54101 rotation_ = videoFrame.rotation_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23102 return 0;
103}
104
wuchengli@chromium.org1bdf1862014-05-28 07:00:51105I420VideoFrame* I420VideoFrame::CloneFrame() const {
kwiberg@webrtc.orgdeb9dae2015-02-26 14:34:55106 rtc::scoped_ptr<I420VideoFrame> new_frame(new I420VideoFrame());
wuchengli@chromium.org1bdf1862014-05-28 07:00:51107 if (new_frame->CopyFrame(*this) == -1) {
108 // CopyFrame failed.
109 return NULL;
110 }
111 return new_frame.release();
112}
113
andrew@webrtc.orgb015cbe2012-10-22 18:19:23114void I420VideoFrame::SwapFrame(I420VideoFrame* videoFrame) {
115 y_plane_.Swap(videoFrame->y_plane_);
116 u_plane_.Swap(videoFrame->u_plane_);
117 v_plane_.Swap(videoFrame->v_plane_);
118 std::swap(width_, videoFrame->width_);
119 std::swap(height_, videoFrame->height_);
120 std::swap(timestamp_, videoFrame->timestamp_);
wu@webrtc.org9d107692014-04-15 17:46:33121 std::swap(ntp_time_ms_, videoFrame->ntp_time_ms_);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23122 std::swap(render_time_ms_, videoFrame->render_time_ms_);
guoweis@webrtc.orgfd7343a2015-02-11 18:37:54123 std::swap(rotation_, videoFrame->rotation_);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23124}
125
126uint8_t* I420VideoFrame::buffer(PlaneType type) {
127 Plane* plane_ptr = GetPlane(type);
128 if (plane_ptr)
129 return plane_ptr->buffer();
130 return NULL;
131}
132
133const uint8_t* I420VideoFrame::buffer(PlaneType type) const {
134 const Plane* plane_ptr = GetPlane(type);
135 if (plane_ptr)
136 return plane_ptr->buffer();
137 return NULL;
138}
139
140int I420VideoFrame::allocated_size(PlaneType type) const {
141 const Plane* plane_ptr = GetPlane(type);
142 if (plane_ptr)
143 return plane_ptr->allocated_size();
144 return -1;
145}
146
147int I420VideoFrame::stride(PlaneType type) const {
148 const Plane* plane_ptr = GetPlane(type);
149 if (plane_ptr)
150 return plane_ptr->stride();
151 return -1;
152}
153
mikhal@webrtc.org3bbed742012-10-24 18:33:04154bool I420VideoFrame::IsZeroSize() const {
magjed@webrtc.org48c809f2015-02-19 15:34:55155 return width() == 0 || height() == 0;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23156}
157
wu@webrtc.orgea7b33e2013-08-05 20:36:57158void* I420VideoFrame::native_handle() const { return NULL; }
159
andrew@webrtc.orgb015cbe2012-10-22 18:19:23160int I420VideoFrame::CheckDimensions(int width, int height,
161 int stride_y, int stride_u, int stride_v) {
162 int half_width = (width + 1) / 2;
163 if (width < 1 || height < 1 ||
164 stride_y < width || stride_u < half_width || stride_v < half_width)
165 return -1;
166 return 0;
167}
168
169const Plane* I420VideoFrame::GetPlane(PlaneType type) const {
170 switch (type) {
171 case kYPlane :
172 return &y_plane_;
173 case kUPlane :
174 return &u_plane_;
175 case kVPlane :
176 return &v_plane_;
177 default:
178 assert(false);
179 }
180 return NULL;
181}
182
183Plane* I420VideoFrame::GetPlane(PlaneType type) {
184 switch (type) {
185 case kYPlane :
186 return &y_plane_;
187 case kUPlane :
188 return &u_plane_;
189 case kVPlane :
190 return &v_plane_;
191 default:
192 assert(false);
193 }
194 return NULL;
195}
196
andrew@webrtc.orgb015cbe2012-10-22 18:19:23197} // namespace webrtc