blob: b4b30a12682b23425e19fd71becf235325900b4f [file] [log] [blame]
magjed712338e2017-05-11 05:11:57 -07001/*
2 * Copyright (c) 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
11#include "webrtc/api/video/video_frame_buffer.h"
12
13#include "libyuv/convert_from.h"
14#include "webrtc/api/video/i420_buffer.h"
15#include "webrtc/base/checks.h"
16
17namespace webrtc {
18
19namespace {
20
21// TODO(magjed): Remove this class. It is only used for providing a default
22// implementation of ToI420() until external clients are updated. ToI420() will
23// then be made pure virtual. This adapter adapts a VideoFrameBuffer (which is
24// expected to be in I420 format) to the PlanarYuvBuffer interface. The reason
25// this is needed is because of the return type mismatch in NativeToI420Buffer
26// (returns VideoFrameBuffer) vs ToI420 (returns PlanarYuvBuffer).
27class PlanarYuvBufferAdapter : public PlanarYuvBuffer {
28 public:
29 explicit PlanarYuvBufferAdapter(rtc::scoped_refptr<VideoFrameBuffer> buffer)
30 : buffer_(buffer) {}
31
32 Type type() const override { return Type::kI420; }
33
34 int width() const override { return buffer_->width(); }
35 int height() const override { return buffer_->height(); }
36
37 const uint8_t* DataY() const override { return buffer_->DataY(); }
38 const uint8_t* DataU() const override { return buffer_->DataU(); }
39 const uint8_t* DataV() const override { return buffer_->DataV(); }
40
41 int StrideY() const override { return buffer_->StrideY(); }
42 int StrideU() const override { return buffer_->StrideU(); }
43 int StrideV() const override { return buffer_->StrideV(); }
44
45 private:
46 rtc::scoped_refptr<VideoFrameBuffer> buffer_;
47};
48
49} // namespace
50
51// TODO(magjed): The default implementations in VideoFrameBuffer are provided in
52// order to support the deprecated interface until external clients are updated.
53// Remove once done.
54VideoFrameBuffer::Type VideoFrameBuffer::type() const {
55 return native_handle() ? Type::kNative : Type::kI420;
56}
57
58const uint8_t* VideoFrameBuffer::DataY() const {
59 return const_cast<VideoFrameBuffer*>(this)->GetI420()->DataY();
60}
61
62const uint8_t* VideoFrameBuffer::DataU() const {
63 return const_cast<VideoFrameBuffer*>(this)->GetI420()->DataU();
64}
65
66const uint8_t* VideoFrameBuffer::DataV() const {
67 return const_cast<VideoFrameBuffer*>(this)->GetI420()->DataV();
68}
69
70// Returns the number of bytes between successive rows for a given plane.
71int VideoFrameBuffer::StrideY() const {
72 return const_cast<VideoFrameBuffer*>(this)->GetI420()->StrideY();
73}
74
75int VideoFrameBuffer::StrideU() const {
76 return const_cast<VideoFrameBuffer*>(this)->GetI420()->StrideU();
77}
78
79int VideoFrameBuffer::StrideV() const {
80 return const_cast<VideoFrameBuffer*>(this)->GetI420()->StrideV();
81}
82
83void* VideoFrameBuffer::native_handle() const {
84 RTC_DCHECK(type() != Type::kNative);
85 return nullptr;
86}
87
88rtc::scoped_refptr<VideoFrameBuffer> VideoFrameBuffer::NativeToI420Buffer() {
89 return ToI420();
90}
91
92rtc::scoped_refptr<PlanarYuvBuffer> VideoFrameBuffer::ToI420() {
93 return new rtc::RefCountedObject<PlanarYuvBufferAdapter>(
94 NativeToI420Buffer());
95}
96
97rtc::scoped_refptr<PlanarYuvBuffer> VideoFrameBuffer::GetI420() {
98 RTC_CHECK(type() == Type::kI420);
99 // TODO(magjed): static_cast to PlanarYuvBuffer instead once external clients
100 // are updated.
101 return new rtc::RefCountedObject<PlanarYuvBufferAdapter>(this);
102}
103
104rtc::scoped_refptr<PlanarYuvBuffer> VideoFrameBuffer::GetI444() {
105 RTC_CHECK(type() == Type::kI444);
106 return static_cast<PlanarYuvBuffer*>(this);
107}
108
109rtc::scoped_refptr<PlanarYuvBuffer> PlanarYuvBuffer::ToI420() {
110 switch (type()) {
111 case Type::kI420:
112 return this;
113 case Type::kI444: {
114 rtc::scoped_refptr<I420Buffer> i420_buffer =
115 I420Buffer::Create(width(), height());
116 libyuv::I420ToI444(DataY(), StrideY(), DataU(), StrideU(), DataV(),
117 StrideV(), i420_buffer->MutableDataY(),
118 i420_buffer->StrideY(), i420_buffer->MutableDataU(),
119 i420_buffer->StrideU(), i420_buffer->MutableDataV(),
120 i420_buffer->StrideV(), width(), height());
121 return i420_buffer;
122 }
123 default:
124 RTC_NOTREACHED();
125 return nullptr;
126 }
127}
128
129int PlanarYuvBuffer::ChromaWidth() const {
130 switch (type()) {
131 case Type::kI420:
132 return (width() + 1) / 2;
133 case Type::kI444:
134 return width();
135 default:
136 RTC_NOTREACHED();
137 return 0;
138 }
139}
140
141int PlanarYuvBuffer::ChromaHeight() const {
142 switch (type()) {
143 case Type::kI420:
144 return (height() + 1) / 2;
145 case Type::kI444:
146 return height();
147 default:
148 RTC_NOTREACHED();
149 return 0;
150 }
151}
152
153} // namespace webrtc