Skip to content

Commit 1a7ef1f

Browse files
Hnoo112233Commit bot
authored andcommitted
Reland of Optimize Android NV12 capture (patchset #1 id:1 of https://codereview.webrtc.org/2327893002/ )
Reason for revert: Import breakage has been fixed. Original issue's description: > Revert of Optimize Android NV12 capture (patchset #2 id:20001 of https://codereview.webrtc.org/2317443003/ ) > > Reason for revert: > Import breakage in g3. > > Original issue's description: > > Optimize Android NV12 capture > > > > This CL optimizes the Android capture NV12 -> I420 + scaling code. For > > example, when the input is 1280x720 and we adapt to 640x360, this CL: > > - Reduces conversion time from 3.37 ms to 1.46 ms. > > - Reduces memory footprint by 1 MB. > > > > BUG=webrtc:6319 > > > > Committed: https://crrev.com/36d38cbb153e19bdc3c62a750aba6889da40aac2 > > Cr-Commit-Position: refs/heads/master@{#14167} > > TBR=sakal@webrtc.org > # Not skipping CQ checks because original CL landed more than 1 days ago. > BUG=webrtc:6319 TBR=sakal@webrtc.org # Not skipping CQ checks because original CL landed more than 1 days ago. BUG=webrtc:6319 Review-Url: https://codereview.webrtc.org/2332213011 Cr-Commit-Position: refs/heads/master@{#14273}
1 parent 7fd9286 commit 1a7ef1f

4 files changed

Lines changed: 107 additions & 29 deletions

File tree

webrtc/api/androidvideotracksource.cc

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#include <utility>
1414

15+
#include "third_party/libyuv/include/libyuv/rotate.h"
16+
1517
namespace webrtc {
1618

1719
AndroidVideoTrackSource::AndroidVideoTrackSource(rtc::Thread* signaling_thread,
@@ -106,42 +108,51 @@ void AndroidVideoTrackSource::OnByteBufferFrameCaptured(const void* frame_data,
106108
return;
107109
}
108110

109-
int rotated_width = crop_width;
110-
int rotated_height = crop_height;
111-
112-
rtc::CritScope lock(&apply_rotation_crit_);
113-
if (apply_rotation_ && (rotation == 90 || rotation == 270)) {
114-
std::swap(adapted_width, adapted_height);
115-
std::swap(rotated_width, rotated_height);
116-
}
117-
118-
rtc::scoped_refptr<webrtc::I420Buffer> buffer =
119-
pre_scale_pool_.CreateBuffer(rotated_width, rotated_height);
120-
121111
const uint8_t* y_plane = static_cast<const uint8_t*>(frame_data);
122112
const uint8_t* uv_plane = y_plane + width * height;
123-
int uv_width = (width + 1) / 2;
113+
const int uv_width = (width + 1) / 2;
124114

125115
RTC_CHECK_GE(length, width * height + 2 * uv_width * ((height + 1) / 2));
126116

127117
// Can only crop at even pixels.
128118
crop_x &= ~1;
129119
crop_y &= ~1;
120+
// Crop just by modifying pointers.
121+
y_plane += width * crop_y + crop_x;
122+
uv_plane += uv_width * crop_y + crop_x;
123+
124+
rtc::scoped_refptr<webrtc::I420Buffer> buffer =
125+
buffer_pool_.CreateBuffer(adapted_width, adapted_height);
130126

131-
libyuv::NV12ToI420Rotate(
132-
y_plane + width * crop_y + crop_x, width,
133-
uv_plane + uv_width * crop_y + crop_x, width, buffer->MutableDataY(),
134-
buffer->StrideY(),
127+
nv12toi420_scaler_.NV12ToI420Scale(
128+
y_plane, width,
129+
uv_plane, uv_width * 2,
130+
crop_width, crop_height,
131+
buffer->MutableDataY(), buffer->StrideY(),
135132
// Swap U and V, since we have NV21, not NV12.
136-
buffer->MutableDataV(), buffer->StrideV(), buffer->MutableDataU(),
137-
buffer->StrideU(), crop_width, crop_height,
138-
static_cast<libyuv::RotationMode>(apply_rotation_ ? rotation : 0));
139-
140-
if (adapted_width != buffer->width() || adapted_height != buffer->height()) {
141-
rtc::scoped_refptr<webrtc::I420Buffer> scaled_buffer(
142-
post_scale_pool_.CreateBuffer(adapted_width, adapted_height));
143-
scaled_buffer->ScaleFrom(buffer);
144-
buffer = scaled_buffer;
133+
buffer->MutableDataV(), buffer->StrideV(),
134+
buffer->MutableDataU(), buffer->StrideU(),
135+
buffer->width(), buffer->height());
136+
137+
// Applying rotation is only supported for legacy reasons, and the performance
138+
// for this path is not critical.
139+
rtc::CritScope lock(&apply_rotation_crit_);
140+
if (apply_rotation_ && rotation != 0) {
141+
rtc::scoped_refptr<I420Buffer> rotated_buffer =
142+
rotation == 180 ? I420Buffer::Create(buffer->width(), buffer->height())
143+
: I420Buffer::Create(buffer->height(), buffer->width());
144+
145+
libyuv::I420Rotate(
146+
buffer->DataY(), buffer->StrideY(),
147+
buffer->DataU(), buffer->StrideU(),
148+
buffer->DataV(), buffer->StrideV(),
149+
rotated_buffer->MutableDataY(), rotated_buffer->StrideY(),
150+
rotated_buffer->MutableDataU(), rotated_buffer->StrideU(),
151+
rotated_buffer->MutableDataV(), rotated_buffer->StrideV(),
152+
buffer->width(), buffer->height(),
153+
static_cast<libyuv::RotationMode>(rotation));
154+
155+
buffer = rotated_buffer;
145156
}
146157

147158
OnFrame(cricket::WebRtcVideoFrame(

webrtc/api/androidvideotracksource.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
#include "webrtc/base/thread_checker.h"
2121
#include "webrtc/base/timestampaligner.h"
2222
#include "webrtc/common_video/include/i420_buffer_pool.h"
23+
#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
2324
#include "webrtc/media/base/videoadapter.h"
2425
#include "webrtc/media/base/videobroadcaster.h"
2526
#include "webrtc/media/base/videosinkinterface.h"
26-
#include "third_party/libyuv/include/libyuv/convert.h"
2727

2828
namespace webrtc {
2929

@@ -92,8 +92,8 @@ class AndroidVideoTrackSource : public Notifier<VideoTrackSourceInterface> {
9292
cricket::VideoAdapter video_adapter_;
9393
rtc::CriticalSection apply_rotation_crit_;
9494
bool apply_rotation_ GUARDED_BY(apply_rotation_crit_);
95-
webrtc::I420BufferPool pre_scale_pool_;
96-
webrtc::I420BufferPool post_scale_pool_;
95+
webrtc::NV12ToI420Scaler nv12toi420_scaler_;
96+
webrtc::I420BufferPool buffer_pool_;
9797
rtc::scoped_refptr<webrtc_jni::SurfaceTextureHelper> surface_texture_helper_;
9898
const bool is_screencast_;
9999

webrtc/common_video/libyuv/include/webrtc_libyuv.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define WEBRTC_COMMON_VIDEO_LIBYUV_INCLUDE_WEBRTC_LIBYUV_H_
1717

1818
#include <stdio.h>
19+
#include <vector>
1920

2021
#include "webrtc/common_types.h" // RawVideoTypes.
2122
#include "webrtc/common_video/rotation.h"
@@ -123,6 +124,22 @@ double I420PSNR(const VideoFrame* ref_frame, const VideoFrame* test_frame);
123124
// Compute SSIM for an I420 frame (all planes).
124125
double I420SSIM(const VideoFrame* ref_frame, const VideoFrame* test_frame);
125126

127+
// Helper class for directly converting and scaling NV12 to I420. The Y-plane
128+
// will be scaled directly to the I420 destination, which makes this faster
129+
// than separate NV12->I420 + I420->I420 scaling.
130+
class NV12ToI420Scaler {
131+
public:
132+
void NV12ToI420Scale(const uint8_t* src_y, int src_stride_y,
133+
const uint8_t* src_uv, int src_stride_uv,
134+
int src_width, int src_height,
135+
uint8_t* dst_y, int dst_stride_y,
136+
uint8_t* dst_u, int dst_stride_u,
137+
uint8_t* dst_v, int dst_stride_v,
138+
int dst_width, int dst_height);
139+
private:
140+
std::vector<uint8_t> tmp_uv_planes_;
141+
};
142+
126143
} // namespace webrtc
127144

128145
#endif // WEBRTC_COMMON_VIDEO_LIBYUV_INCLUDE_WEBRTC_LIBYUV_H_

webrtc/common_video/libyuv/webrtc_libyuv.cc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,4 +341,54 @@ double I420SSIM(const VideoFrame* ref_frame, const VideoFrame* test_frame) {
341341
test_frame->video_frame_buffer()->StrideV(),
342342
test_frame->width(), test_frame->height());
343343
}
344+
345+
void NV12ToI420Scaler::NV12ToI420Scale(
346+
const uint8_t* src_y, int src_stride_y,
347+
const uint8_t* src_uv, int src_stride_uv,
348+
int src_width, int src_height,
349+
uint8_t* dst_y, int dst_stride_y,
350+
uint8_t* dst_u, int dst_stride_u,
351+
uint8_t* dst_v, int dst_stride_v,
352+
int dst_width, int dst_height) {
353+
if (src_width == dst_width && src_height == dst_height) {
354+
// No scaling.
355+
tmp_uv_planes_.clear();
356+
tmp_uv_planes_.shrink_to_fit();
357+
libyuv::NV12ToI420(
358+
src_y, src_stride_y,
359+
src_uv, src_stride_uv,
360+
dst_y, dst_stride_y,
361+
dst_u, dst_stride_u,
362+
dst_v, dst_stride_v,
363+
src_width, src_height);
364+
return;
365+
}
366+
367+
// Scaling.
368+
// Allocate temporary memory for spitting UV planes.
369+
const int src_uv_width = (src_width + 1) / 2;
370+
const int src_uv_height = (src_height + 1) / 2;
371+
tmp_uv_planes_.resize(src_uv_width * src_uv_height * 2);
372+
tmp_uv_planes_.shrink_to_fit();
373+
374+
// Split source UV plane into separate U and V plane using the temporary data.
375+
uint8_t* const src_u = tmp_uv_planes_.data();
376+
uint8_t* const src_v = tmp_uv_planes_.data() + src_uv_width * src_uv_height;
377+
libyuv::SplitUVPlane(src_uv, src_stride_uv,
378+
src_u, src_uv_width,
379+
src_v, src_uv_width,
380+
src_uv_width, src_uv_height);
381+
382+
// Scale the planes into the destination.
383+
libyuv::I420Scale(src_y, src_stride_y,
384+
src_u, src_uv_width,
385+
src_v, src_uv_width,
386+
src_width, src_height,
387+
dst_y, dst_stride_y,
388+
dst_u, dst_stride_u,
389+
dst_v, dst_stride_v,
390+
dst_width, dst_height,
391+
libyuv::kFilterBox);
392+
}
393+
344394
} // namespace webrtc

0 commit comments

Comments
 (0)