add h264 422 decoding

Bug: webrtc:13826
Change-Id: Ic7296be69157a9aaf5f139a18fdb011b90f4caa1
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/255380
Reviewed-by: Niels Moller <nisse@webrtc.org>
Reviewed-by: Stefan Holmer <stefan@webrtc.org>
Reviewed-by: Ilya Nikolaevskiy <ilnik@webrtc.org>
Reviewed-by: Magnus Flodman <mflodman@webrtc.org>
Commit-Queue: Ilya Nikolaevskiy <ilnik@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#36337}
This commit is contained in:
Sergio Garcia Murillo 2022-03-25 09:04:09 +01:00 committed by WebRTC LUCI CQ
parent d8654cf636
commit b63536f5d3
14 changed files with 884 additions and 67 deletions

View File

@ -44,6 +44,8 @@ rtc_library("video_frame") {
sources = [
"i420_buffer.cc",
"i420_buffer.h",
"i422_buffer.cc",
"i422_buffer.h",
"i444_buffer.cc",
"i444_buffer.h",
"nv12_buffer.cc",

View File

@ -18,6 +18,10 @@ specific_include_rules = {
"+rtc_base/memory/aligned_malloc.h",
],
"i422_buffer\.h": [
"+rtc_base/memory/aligned_malloc.h",
],
"i444_buffer\.h": [
"+rtc_base/memory/aligned_malloc.h",
],

348
api/video/i422_buffer.cc Normal file
View File

@ -0,0 +1,348 @@
/*
* Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "api/video/i422_buffer.h"
#include <string.h>
#include <algorithm>
#include <utility>
#include "api/video/i420_buffer.h"
#include "rtc_base/checks.h"
#include "rtc_base/ref_counted_object.h"
#include "third_party/libyuv/include/libyuv/convert.h"
#include "third_party/libyuv/include/libyuv/planar_functions.h"
#include "third_party/libyuv/include/libyuv/scale.h"
// Aligning pointer to 64 bytes for improved performance, e.g. use SIMD.
static const int kBufferAlignment = 64;
namespace webrtc {
namespace {
int I422DataSize(int height, int stride_y, int stride_u, int stride_v) {
return stride_y * height + stride_u * height + stride_v * height;
}
int I422Rotate(const uint8_t* src_y,
int src_stride_y,
const uint8_t* src_u,
int src_stride_u,
const uint8_t* src_v,
int src_stride_v,
uint8_t* dst_y,
int dst_stride_y,
uint8_t* dst_u,
int dst_stride_u,
uint8_t* dst_v,
int dst_stride_v,
int width,
int height,
enum libyuv::RotationMode mode) {
int halfwidth = (width + 1) >> 1;
int halfheight = (height + 1) >> 1;
if (!src_y || !src_u || !src_v || width <= 0 || height == 0 || !dst_y ||
!dst_u || !dst_v) {
return -1;
}
// Negative height means invert the image.
if (height < 0) {
height = -height;
src_y = src_y + (height - 1) * src_stride_y;
src_u = src_u + (height - 1) * src_stride_u;
src_v = src_v + (height - 1) * src_stride_v;
src_stride_y = -src_stride_y;
src_stride_u = -src_stride_u;
src_stride_v = -src_stride_v;
}
switch (mode) {
case libyuv::kRotate0:
// copy frame
libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width,
height);
libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth,
height);
libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth,
height);
return 0;
case libyuv::kRotate90:
// We need to rotate and rescale, we use plane Y as temporal storage.
libyuv::RotatePlane90(src_u, src_stride_u, dst_y, height, halfwidth,
height);
libyuv::ScalePlane(dst_y, height, height, halfwidth, dst_u, halfheight,
halfheight, width, libyuv::kFilterBilinear);
libyuv::RotatePlane90(src_v, src_stride_v, dst_y, height, halfwidth,
height);
libyuv::ScalePlane(dst_y, height, height, halfwidth, dst_v, halfheight,
halfheight, width, libyuv::kFilterLinear);
libyuv::RotatePlane90(src_y, src_stride_y, dst_y, dst_stride_y, width,
height);
return 0;
case libyuv::kRotate270:
// We need to rotate and rescale, we use plane Y as temporal storage.
libyuv::RotatePlane270(src_u, src_stride_u, dst_y, height, halfwidth,
height);
libyuv::ScalePlane(dst_y, height, height, halfwidth, dst_u, halfheight,
halfheight, width, libyuv::kFilterBilinear);
libyuv::RotatePlane270(src_v, src_stride_v, dst_y, height, halfwidth,
height);
libyuv::ScalePlane(dst_y, height, height, halfwidth, dst_v, halfheight,
halfheight, width, libyuv::kFilterLinear);
libyuv::RotatePlane270(src_y, src_stride_y, dst_y, dst_stride_y, width,
height);
return 0;
case libyuv::kRotate180:
libyuv::RotatePlane180(src_y, src_stride_y, dst_y, dst_stride_y, width,
height);
libyuv::RotatePlane180(src_u, src_stride_u, dst_u, dst_stride_u,
halfwidth, height);
libyuv::RotatePlane180(src_v, src_stride_v, dst_v, dst_stride_v,
halfwidth, height);
return 0;
default:
break;
}
return -1;
}
int I422Scale(const uint8_t* src_y,
int src_stride_y,
const uint8_t* src_u,
int src_stride_u,
const uint8_t* src_v,
int src_stride_v,
int src_width,
int src_height,
uint8_t* dst_y,
int dst_stride_y,
uint8_t* dst_u,
int dst_stride_u,
uint8_t* dst_v,
int dst_stride_v,
int dst_width,
int dst_height,
enum libyuv::FilterMode filtering) {
if (!src_y || !src_u || !src_v || src_width <= 0 || src_height == 0 ||
src_width > 32768 || src_height > 32768 || !dst_y || !dst_u || !dst_v ||
dst_width <= 0 || dst_height <= 0) {
return -1;
}
int src_halfwidth = (src_width + 1) >> 1;
int dst_halfwidth = (dst_width + 1) >> 1;
libyuv::ScalePlane(src_y, src_stride_y, src_width, src_height, dst_y,
dst_stride_y, dst_width, dst_height, filtering);
libyuv::ScalePlane(src_u, src_stride_u, src_halfwidth, src_height, dst_u,
dst_stride_u, dst_halfwidth, dst_height, filtering);
libyuv::ScalePlane(src_v, src_stride_v, src_halfwidth, src_height, dst_v,
dst_stride_v, dst_halfwidth, dst_height, filtering);
return 0;
}
} // namespace
I422Buffer::I422Buffer(int width, int height)
: I422Buffer(width, height, width, (width + 1) / 2, (width + 1) / 2) {}
I422Buffer::I422Buffer(int width,
int height,
int stride_y,
int stride_u,
int stride_v)
: width_(width),
height_(height),
stride_y_(stride_y),
stride_u_(stride_u),
stride_v_(stride_v),
data_(static_cast<uint8_t*>(
AlignedMalloc(I422DataSize(height, stride_y, stride_u, stride_v),
kBufferAlignment))) {
RTC_DCHECK_GT(width, 0);
RTC_DCHECK_GT(height, 0);
RTC_DCHECK_GE(stride_y, width);
RTC_DCHECK_GE(stride_u, (width + 1) / 2);
RTC_DCHECK_GE(stride_v, (width + 1) / 2);
}
I422Buffer::~I422Buffer() {}
// static
rtc::scoped_refptr<I422Buffer> I422Buffer::Create(int width, int height) {
return rtc::make_ref_counted<I422Buffer>(width, height);
}
// static
rtc::scoped_refptr<I422Buffer> I422Buffer::Create(int width,
int height,
int stride_y,
int stride_u,
int stride_v) {
return rtc::make_ref_counted<I422Buffer>(width, height, stride_y, stride_u,
stride_v);
}
// static
rtc::scoped_refptr<I422Buffer> I422Buffer::Copy(
const I422BufferInterface& source) {
return Copy(source.width(), source.height(), source.DataY(), source.StrideY(),
source.DataU(), source.StrideU(), source.DataV(),
source.StrideV());
}
// static
rtc::scoped_refptr<I422Buffer> I422Buffer::Copy(
const I420BufferInterface& source) {
const int width = source.width();
const int height = source.height();
rtc::scoped_refptr<I422Buffer> buffer = Create(width, height);
RTC_CHECK_EQ(
0, libyuv::I420ToI422(
source.DataY(), source.StrideY(), source.DataU(), source.StrideU(),
source.DataV(), source.StrideV(), buffer->MutableDataY(),
buffer->StrideY(), buffer->MutableDataU(), buffer->StrideU(),
buffer->MutableDataV(), buffer->StrideV(), width, height));
return buffer;
}
// static
rtc::scoped_refptr<I422Buffer> I422Buffer::Copy(int width,
int height,
const uint8_t* data_y,
int stride_y,
const uint8_t* data_u,
int stride_u,
const uint8_t* data_v,
int stride_v) {
// Note: May use different strides than the input data.
rtc::scoped_refptr<I422Buffer> buffer = Create(width, height);
RTC_CHECK_EQ(0, libyuv::I422Copy(data_y, stride_y, data_u, stride_u, data_v,
stride_v, buffer->MutableDataY(),
buffer->StrideY(), buffer->MutableDataU(),
buffer->StrideU(), buffer->MutableDataV(),
buffer->StrideV(), width, height));
return buffer;
}
// static
rtc::scoped_refptr<I422Buffer> I422Buffer::Rotate(
const I422BufferInterface& src,
VideoRotation rotation) {
RTC_CHECK(src.DataY());
RTC_CHECK(src.DataU());
RTC_CHECK(src.DataV());
int rotated_width = src.width();
int rotated_height = src.height();
if (rotation == webrtc::kVideoRotation_90 ||
rotation == webrtc::kVideoRotation_270) {
std::swap(rotated_width, rotated_height);
}
rtc::scoped_refptr<webrtc::I422Buffer> buffer =
I422Buffer::Create(rotated_width, rotated_height);
RTC_CHECK_EQ(
0,
I422Rotate(src.DataY(), src.StrideY(), src.DataU(), src.StrideU(),
src.DataV(), src.StrideV(), buffer->MutableDataY(),
buffer->StrideY(), buffer->MutableDataU(), buffer->StrideU(),
buffer->MutableDataV(), buffer->StrideV(), src.width(),
src.height(), static_cast<libyuv::RotationMode>(rotation)));
return buffer;
}
rtc::scoped_refptr<I420BufferInterface> I422Buffer::ToI420() {
rtc::scoped_refptr<I420Buffer> i420_buffer =
I420Buffer::Create(width(), height());
libyuv::I422ToI420(DataY(), StrideY(), DataU(), StrideU(), DataV(), StrideV(),
i420_buffer->MutableDataY(), i420_buffer->StrideY(),
i420_buffer->MutableDataU(), i420_buffer->StrideU(),
i420_buffer->MutableDataV(), i420_buffer->StrideV(),
width(), height());
return i420_buffer;
}
void I422Buffer::InitializeData() {
memset(data_.get(), 0,
I422DataSize(height_, stride_y_, stride_u_, stride_v_));
}
int I422Buffer::width() const {
return width_;
}
int I422Buffer::height() const {
return height_;
}
const uint8_t* I422Buffer::DataY() const {
return data_.get();
}
const uint8_t* I422Buffer::DataU() const {
return data_.get() + stride_y_ * height_;
}
const uint8_t* I422Buffer::DataV() const {
return data_.get() + stride_y_ * height_ + stride_u_ * height_;
}
int I422Buffer::StrideY() const {
return stride_y_;
}
int I422Buffer::StrideU() const {
return stride_u_;
}
int I422Buffer::StrideV() const {
return stride_v_;
}
uint8_t* I422Buffer::MutableDataY() {
return const_cast<uint8_t*>(DataY());
}
uint8_t* I422Buffer::MutableDataU() {
return const_cast<uint8_t*>(DataU());
}
uint8_t* I422Buffer::MutableDataV() {
return const_cast<uint8_t*>(DataV());
}
void I422Buffer::CropAndScaleFrom(const I422BufferInterface& src,
int offset_x,
int offset_y,
int crop_width,
int crop_height) {
RTC_CHECK_LE(crop_width, src.width());
RTC_CHECK_LE(crop_height, src.height());
RTC_CHECK_LE(crop_width + offset_x, src.width());
RTC_CHECK_LE(crop_height + offset_y, src.height());
RTC_CHECK_GE(offset_x, 0);
RTC_CHECK_GE(offset_y, 0);
// Make sure offset is even so that u/v plane becomes aligned.
const int uv_offset_x = offset_x / 2;
const int uv_offset_y = offset_y;
offset_x = uv_offset_x * 2;
const uint8_t* y_plane = src.DataY() + src.StrideY() * offset_y + offset_x;
const uint8_t* u_plane =
src.DataU() + src.StrideU() * uv_offset_y + uv_offset_x;
const uint8_t* v_plane =
src.DataV() + src.StrideV() * uv_offset_y + uv_offset_x;
int res = I422Scale(y_plane, src.StrideY(), u_plane, src.StrideU(), v_plane,
src.StrideV(), crop_width, crop_height, MutableDataY(),
StrideY(), MutableDataU(), StrideU(), MutableDataV(),
StrideV(), width(), height(), libyuv::kFilterBox);
RTC_DCHECK_EQ(res, 0);
}
} // namespace webrtc

114
api/video/i422_buffer.h Normal file
View File

@ -0,0 +1,114 @@
/*
* Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef API_VIDEO_I422_BUFFER_H_
#define API_VIDEO_I422_BUFFER_H_
#include <stdint.h>
#include <memory>
#include "api/scoped_refptr.h"
#include "api/video/video_frame_buffer.h"
#include "api/video/video_rotation.h"
#include "rtc_base/memory/aligned_malloc.h"
#include "rtc_base/system/rtc_export.h"
namespace webrtc {
// Plain I422 buffer in standard memory.
class RTC_EXPORT I422Buffer : public I422BufferInterface {
public:
static rtc::scoped_refptr<I422Buffer> Create(int width, int height);
static rtc::scoped_refptr<I422Buffer> Create(int width,
int height,
int stride_y,
int stride_u,
int stride_v);
// Create a new buffer and copy the pixel data.
static rtc::scoped_refptr<I422Buffer> Copy(const I422BufferInterface& buffer);
/// Convert and put I420 buffer into a new buffer.
static rtc::scoped_refptr<I422Buffer> Copy(const I420BufferInterface& buffer);
static rtc::scoped_refptr<I422Buffer> Copy(int width,
int height,
const uint8_t* data_y,
int stride_y,
const uint8_t* data_u,
int stride_u,
const uint8_t* data_v,
int stride_v);
// Returns a rotated copy of `src`.
static rtc::scoped_refptr<I422Buffer> Rotate(const I422BufferInterface& src,
VideoRotation rotation);
rtc::scoped_refptr<I420BufferInterface> ToI420() final;
const I420BufferInterface* GetI420() const final { return nullptr; }
// Sets the buffer to all black.
static void SetBlack(I422Buffer* buffer);
// Sets all three planes to all zeros. Used to work around for
// quirks in memory checkers
// (https://bugs.chromium.org/p/libyuv/issues/detail?id=377) and
// ffmpeg (http://crbug.com/390941).
// TODO(nisse): Deprecated. Should be deleted if/when those issues
// are resolved in a better way. Or in the mean time, use SetBlack.
void InitializeData();
int width() const override;
int height() const override;
const uint8_t* DataY() const override;
const uint8_t* DataU() const override;
const uint8_t* DataV() const override;
int StrideY() const override;
int StrideU() const override;
int StrideV() const override;
uint8_t* MutableDataY();
uint8_t* MutableDataU();
uint8_t* MutableDataV();
// Scale the cropped area of `src` to the size of `this` buffer, and
// write the result into `this`.
void CropAndScaleFrom(const I422BufferInterface& src,
int offset_x,
int offset_y,
int crop_width,
int crop_height);
// The common case of a center crop, when needed to adjust the
// aspect ratio without distorting the image.
void CropAndScaleFrom(const I422BufferInterface& src);
// Scale all of `src` to the size of `this` buffer, with no cropping.
void ScaleFrom(const I422BufferInterface& src);
protected:
I422Buffer(int width, int height);
I422Buffer(int width, int height, int stride_y, int stride_u, int stride_v);
~I422Buffer() override;
private:
const int width_;
const int height_;
const int stride_y_;
const int stride_u_;
const int stride_v_;
const std::unique_ptr<uint8_t, AlignedFreeDeleter> data_;
};
} // namespace webrtc
#endif // API_VIDEO_I422_BUFFER_H_

View File

@ -12,6 +12,7 @@ rtc_library("rtc_api_video_unittests") {
testonly = true
sources = [
"color_space_unittest.cc",
"i422_buffer_unittest.cc",
"i444_buffer_unittest.cc",
"nv12_buffer_unittest.cc",
"video_adaptation_counters_unittest.cc",

View File

@ -0,0 +1,128 @@
/*
* Copyright (c) 2022 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "api/video/i422_buffer.h"
#include "api/video/i420_buffer.h"
#include "test/frame_utils.h"
#include "test/gmock.h"
#include "test/gtest.h"
namespace webrtc {
namespace {
int GetY(rtc::scoped_refptr<I422BufferInterface> buf, int col, int row) {
return buf->DataY()[row * buf->StrideY() + col];
}
int GetU(rtc::scoped_refptr<I422BufferInterface> buf, int col, int row) {
return buf->DataU()[row * buf->StrideU() + col];
}
int GetV(rtc::scoped_refptr<I422BufferInterface> buf, int col, int row) {
return buf->DataV()[row * buf->StrideV() + col];
}
void FillI422Buffer(rtc::scoped_refptr<I422Buffer> buf) {
const uint8_t Y = 1;
const uint8_t U = 2;
const uint8_t V = 3;
for (int row = 0; row < buf->height(); ++row) {
for (int col = 0; col < buf->width(); ++col) {
buf->MutableDataY()[row * buf->StrideY() + col] = Y;
}
}
for (int row = 0; row < buf->ChromaHeight(); ++row) {
for (int col = 0; col < buf->ChromaWidth(); ++col) {
buf->MutableDataU()[row * buf->StrideU() + col] = U;
buf->MutableDataV()[row * buf->StrideV() + col] = V;
}
}
}
} // namespace
TEST(I422BufferTest, InitialData) {
constexpr int stride = 3;
constexpr int halfstride = (stride + 1) >> 1;
constexpr int width = 3;
constexpr int halfwidth = (width + 1) >> 1;
constexpr int height = 3;
rtc::scoped_refptr<I422Buffer> i422_buffer(I422Buffer::Create(width, height));
EXPECT_EQ(width, i422_buffer->width());
EXPECT_EQ(height, i422_buffer->height());
EXPECT_EQ(stride, i422_buffer->StrideY());
EXPECT_EQ(halfstride, i422_buffer->StrideU());
EXPECT_EQ(halfstride, i422_buffer->StrideV());
EXPECT_EQ(halfwidth, i422_buffer->ChromaWidth());
EXPECT_EQ(height, i422_buffer->ChromaHeight());
}
TEST(I422BufferTest, ReadPixels) {
constexpr int width = 3;
constexpr int halfwidth = (width + 1) >> 1;
constexpr int height = 3;
rtc::scoped_refptr<I422Buffer> i422_buffer(I422Buffer::Create(width, height));
// Y = 1, U = 2, V = 3.
FillI422Buffer(i422_buffer);
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
EXPECT_EQ(1, GetY(i422_buffer, col, row));
}
}
for (int row = 0; row < height; row++) {
for (int col = 0; col < halfwidth; col++) {
EXPECT_EQ(2, GetU(i422_buffer, col, row));
EXPECT_EQ(3, GetV(i422_buffer, col, row));
}
}
}
TEST(I422BufferTest, ToI420) {
constexpr int width = 3;
constexpr int halfwidth = (width + 1) >> 1;
constexpr int height = 3;
constexpr int size = width * height;
constexpr int halfsize = (width + 1) / 2 * height;
constexpr int quartersize = (width + 1) / 2 * (height + 1) / 2;
rtc::scoped_refptr<I420Buffer> reference(I420Buffer::Create(width, height));
memset(reference->MutableDataY(), 8, size);
memset(reference->MutableDataU(), 4, quartersize);
memset(reference->MutableDataV(), 2, quartersize);
rtc::scoped_refptr<I422Buffer> i422_buffer(I422Buffer::Create(width, height));
// Convert the reference buffer to I422.
memset(i422_buffer->MutableDataY(), 8, size);
memset(i422_buffer->MutableDataU(), 4, halfsize);
memset(i422_buffer->MutableDataV(), 2, halfsize);
// Confirm YUV values are as expected.
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
EXPECT_EQ(8, GetY(i422_buffer, col, row));
}
}
for (int row = 0; row < height; row++) {
for (int col = 0; col < halfwidth; col++) {
EXPECT_EQ(4, GetU(i422_buffer, col, row));
EXPECT_EQ(2, GetV(i422_buffer, col, row));
}
}
rtc::scoped_refptr<I420BufferInterface> i420_buffer(i422_buffer->ToI420());
EXPECT_EQ(height, i420_buffer->height());
EXPECT_EQ(width, i420_buffer->width());
EXPECT_TRUE(test::FrameBufsEqual(reference, i420_buffer));
}
} // namespace webrtc

View File

@ -11,6 +11,7 @@
#include "api/video/video_frame_buffer.h"
#include "api/video/i420_buffer.h"
#include "api/video/i422_buffer.h"
#include "api/video/i444_buffer.h"
#include "api/video/nv12_buffer.h"
#include "rtc_base/checks.h"
@ -47,6 +48,11 @@ const I444BufferInterface* VideoFrameBuffer::GetI444() const {
return static_cast<const I444BufferInterface*>(this);
}
const I422BufferInterface* VideoFrameBuffer::GetI422() const {
RTC_CHECK(type() == Type::kI422);
return static_cast<const I422BufferInterface*>(this);
}
const I010BufferInterface* VideoFrameBuffer::GetI010() const {
RTC_CHECK(type() == Type::kI010);
return static_cast<const I010BufferInterface*>(this);
@ -77,6 +83,8 @@ const char* VideoFrameBufferTypeToString(VideoFrameBuffer::Type type) {
return "kI420A";
case VideoFrameBuffer::Type::kI444:
return "kI444";
case VideoFrameBuffer::Type::kI422:
return "kI422";
case VideoFrameBuffer::Type::kI010:
return "kI010";
case VideoFrameBuffer::Type::kNV12:
@ -131,6 +139,31 @@ rtc::scoped_refptr<VideoFrameBuffer> I444BufferInterface::CropAndScale(
return result;
}
VideoFrameBuffer::Type I422BufferInterface::type() const {
return Type::kI422;
}
int I422BufferInterface::ChromaWidth() const {
return (width() + 1) / 2;
}
int I422BufferInterface::ChromaHeight() const {
return height();
}
rtc::scoped_refptr<VideoFrameBuffer> I422BufferInterface::CropAndScale(
int offset_x,
int offset_y,
int crop_width,
int crop_height,
int scaled_width,
int scaled_height) {
rtc::scoped_refptr<I422Buffer> result =
I422Buffer::Create(scaled_width, scaled_height);
result->CropAndScaleFrom(*this, offset_x, offset_y, crop_width, crop_height);
return result;
}
VideoFrameBuffer::Type I010BufferInterface::type() const {
return Type::kI010;
}

View File

@ -22,6 +22,7 @@ namespace webrtc {
class I420BufferInterface;
class I420ABufferInterface;
class I422BufferInterface;
class I444BufferInterface;
class I010BufferInterface;
class NV12BufferInterface;
@ -52,6 +53,7 @@ class RTC_EXPORT VideoFrameBuffer : public rtc::RefCountInterface {
kNative,
kI420,
kI420A,
kI422,
kI444,
kI010,
kNV12,
@ -104,6 +106,7 @@ class RTC_EXPORT VideoFrameBuffer : public rtc::RefCountInterface {
// These functions should only be called if type() is of the correct type.
// Calling with a different type will result in a crash.
const I420ABufferInterface* GetI420A() const;
const I422BufferInterface* GetI422() const;
const I444BufferInterface* GetI444() const;
const I010BufferInterface* GetI010() const;
const NV12BufferInterface* GetNV12() const;
@ -140,7 +143,7 @@ class PlanarYuvBuffer : public VideoFrameBuffer {
};
// This interface represents 8-bit color depth formats: Type::kI420,
// Type::kI420A and Type::kI444.
// Type::kI420A, Type::kI422 and Type::kI444.
class PlanarYuv8Buffer : public PlanarYuvBuffer {
public:
// Returns pointer to the pixel data for a given plane. The memory is owned by
@ -177,6 +180,26 @@ class RTC_EXPORT I420ABufferInterface : public I420BufferInterface {
~I420ABufferInterface() override {}
};
// Represents Type::kI422, 4:2:2 planar with 8 bits per pixel.
class I422BufferInterface : public PlanarYuv8Buffer {
public:
Type type() const final;
int ChromaWidth() const final;
int ChromaHeight() const final;
rtc::scoped_refptr<VideoFrameBuffer> CropAndScale(int offset_x,
int offset_y,
int crop_width,
int crop_height,
int scaled_width,
int scaled_height) override;
protected:
~I422BufferInterface() override {}
};
// Represents Type::kI444, 4:4:4 planar with 8 bits per pixel.
class I444BufferInterface : public PlanarYuv8Buffer {
public:
Type type() const final;

View File

@ -31,6 +31,17 @@ rtc::scoped_refptr<I420BufferInterface> WrapI420Buffer(
int v_stride,
std::function<void()> no_longer_used);
rtc::scoped_refptr<I422BufferInterface> WrapI422Buffer(
int width,
int height,
const uint8_t* y_plane,
int y_stride,
const uint8_t* u_plane,
int u_stride,
const uint8_t* v_plane,
int v_stride,
std::function<void()> no_longer_used);
rtc::scoped_refptr<I444BufferInterface> WrapI444Buffer(
int width,
int height,

View File

@ -17,6 +17,7 @@
#include "api/scoped_refptr.h"
#include "api/video/i420_buffer.h"
#include "api/video/i422_buffer.h"
#include "api/video/i444_buffer.h"
#include "api/video/nv12_buffer.h"
#include "rtc_base/race_checker.h"
@ -44,6 +45,7 @@ class VideoFrameBufferPool {
// and there are less than `max_number_of_buffers` pending, a buffer is
// created. Returns null otherwise.
rtc::scoped_refptr<I420Buffer> CreateI420Buffer(int width, int height);
rtc::scoped_refptr<I422Buffer> CreateI422Buffer(int width, int height);
rtc::scoped_refptr<I444Buffer> CreateI444Buffer(int width, int height);
rtc::scoped_refptr<NV12Buffer> CreateNV12Buffer(int width, int height);

View File

@ -124,6 +124,22 @@ rtc::scoped_refptr<I420BufferInterface> I444BufferBase::ToI420() {
return i420_buffer;
}
class I422BufferBase : public I422BufferInterface {
public:
rtc::scoped_refptr<I420BufferInterface> ToI420() final;
};
rtc::scoped_refptr<I420BufferInterface> I422BufferBase::ToI420() {
rtc::scoped_refptr<I420Buffer> i420_buffer =
I420Buffer::Create(width(), height());
libyuv::I422ToI420(DataY(), StrideY(), DataU(), StrideU(), DataV(), StrideV(),
i420_buffer->MutableDataY(), i420_buffer->StrideY(),
i420_buffer->MutableDataU(), i420_buffer->StrideU(),
i420_buffer->MutableDataV(), i420_buffer->StrideV(),
width(), height());
return i420_buffer;
}
// Template to implement a wrapped buffer for a PlanarYuv16BBuffer.
template <typename Base>
class WrappedYuv16BBuffer : public Base {
@ -231,6 +247,22 @@ rtc::scoped_refptr<I420ABufferInterface> WrapI420ABuffer(
v_stride, a_plane, a_stride, no_longer_used));
}
rtc::scoped_refptr<I422BufferInterface> WrapI422Buffer(
int width,
int height,
const uint8_t* y_plane,
int y_stride,
const uint8_t* u_plane,
int u_stride,
const uint8_t* v_plane,
int v_stride,
std::function<void()> no_longer_used) {
return rtc::scoped_refptr<I422BufferBase>(
rtc::make_ref_counted<WrappedYuvBuffer<I422BufferBase>>(
width, height, y_plane, y_stride, u_plane, u_stride, v_plane,
v_stride, no_longer_used));
}
rtc::scoped_refptr<I444BufferInterface> WrapI444Buffer(
int width,
int height,
@ -262,6 +294,9 @@ rtc::scoped_refptr<PlanarYuvBuffer> WrapYuvBuffer(
case VideoFrameBuffer::Type::kI420:
return WrapI420Buffer(width, height, y_plane, y_stride, u_plane, u_stride,
v_plane, v_stride, no_longer_used);
case VideoFrameBuffer::Type::kI422:
return WrapI422Buffer(width, height, y_plane, y_stride, u_plane, u_stride,
v_plane, v_stride, no_longer_used);
case VideoFrameBuffer::Type::kI444:
return WrapI444Buffer(width, height, y_plane, y_stride, u_plane, u_stride,
v_plane, v_stride, no_longer_used);

View File

@ -31,6 +31,10 @@ bool HasOneRef(const rtc::scoped_refptr<VideoFrameBuffer>& buffer) {
return static_cast<rtc::RefCountedObject<I444Buffer>*>(buffer.get())
->HasOneRef();
}
case VideoFrameBuffer::Type::kI422: {
return static_cast<rtc::RefCountedObject<I422Buffer>*>(buffer.get())
->HasOneRef();
}
case VideoFrameBuffer::Type::kNV12: {
return static_cast<rtc::RefCountedObject<NV12Buffer>*>(buffer.get())
->HasOneRef();
@ -152,6 +156,37 @@ rtc::scoped_refptr<I444Buffer> VideoFrameBufferPool::CreateI444Buffer(
return buffer;
}
rtc::scoped_refptr<I422Buffer> VideoFrameBufferPool::CreateI422Buffer(
int width,
int height) {
RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
rtc::scoped_refptr<VideoFrameBuffer> existing_buffer =
GetExistingBuffer(width, height, VideoFrameBuffer::Type::kI422);
if (existing_buffer) {
// Cast is safe because the only way kI422 buffer is created is
// in the same function below, where |RefCountedObject<I422Buffer>|
// is created.
rtc::RefCountedObject<I422Buffer>* raw_buffer =
static_cast<rtc::RefCountedObject<I422Buffer>*>(existing_buffer.get());
// Creates a new scoped_refptr, which is also pointing to the same
// RefCountedObject as buffer, increasing ref count.
return rtc::scoped_refptr<I422Buffer>(raw_buffer);
}
if (buffers_.size() >= max_number_of_buffers_)
return nullptr;
// Allocate new buffer.
rtc::scoped_refptr<I422Buffer> buffer =
rtc::make_ref_counted<I422Buffer>(width, height);
if (zero_initialize_)
buffer->InitializeData();
buffers_.push_back(buffer);
return buffer;
}
rtc::scoped_refptr<NV12Buffer> VideoFrameBufferPool::CreateNV12Buffer(
int width,
int height) {

View File

@ -15,6 +15,7 @@
#include "api/video/i010_buffer.h"
#include "api/video/i420_buffer.h"
#include "api/video/i422_buffer.h"
#include "api/video/i444_buffer.h"
#include "api/video/nv12_buffer.h"
#include "rtc_base/time_utils.h"
@ -26,6 +27,28 @@ namespace webrtc {
namespace {
struct SubSampling {
int x;
int y;
};
SubSampling SubSamplingForType(VideoFrameBuffer::Type type) {
switch (type) {
case VideoFrameBuffer::Type::kI420:
return {.x = 2, .y = 2};
case VideoFrameBuffer::Type::kI420A:
return {.x = 2, .y = 2};
case VideoFrameBuffer::Type::kI422:
return {.x = 2, .y = 1};
case VideoFrameBuffer::Type::kI444:
return {.x = 1, .y = 1};
case VideoFrameBuffer::Type::kI010:
return {.x = 2, .y = 2};
default:
return {};
}
}
// Helper function to create a buffer and fill it with a gradient for
// PlanarYuvBuffer based buffers.
template <class T>
@ -86,7 +109,8 @@ void CheckCrop(const T& frame,
double rel_height) {
int width = frame.width();
int height = frame.height();
int plane_divider = frame.type() == VideoFrameBuffer::Type::kI444 ? 1 : 2;
SubSampling plane_divider = SubSamplingForType(frame.type());
// Check that pixel values in the corners match the gradient used
// for initialization.
@ -102,12 +126,12 @@ void CheckCrop(const T& frame,
EXPECT_NEAR(frame.DataY()[x + y * frame.StrideY()] / 256.0,
(orig_x + orig_y) / 2, 0.02);
EXPECT_NEAR(frame.DataU()[x / plane_divider +
(y / plane_divider) * frame.StrideU()] /
EXPECT_NEAR(frame.DataU()[x / plane_divider.x +
(y / plane_divider.y) * frame.StrideU()] /
256.0,
orig_x, 0.02);
EXPECT_NEAR(frame.DataV()[x / plane_divider +
(y / plane_divider) * frame.StrideV()] /
EXPECT_NEAR(frame.DataV()[x / plane_divider.x +
(y / plane_divider.y) * frame.StrideV()] /
256.0,
orig_y, 0.02);
}
@ -141,18 +165,19 @@ void CheckRotate(int width,
} colors[] = {{0, 0, 0}, {127, 255, 0}, {255, 255, 255}, {127, 0, 255}};
int corner_offset = static_cast<int>(rotation) / 90;
int plane_divider = rotated.type() == VideoFrameBuffer::Type::kI444 ? 1 : 2;
SubSampling plane_divider = SubSamplingForType(rotated.type());
for (int i = 0; i < 4; i++) {
int j = (i + corner_offset) % 4;
int x = corners[j].x * (rotated_width - 1);
int y = corners[j].y * (rotated_height - 1);
EXPECT_EQ(colors[i].y, rotated.DataY()[x + y * rotated.StrideY()]);
EXPECT_EQ(colors[i].u,
rotated.DataU()[(x / plane_divider) +
(y / plane_divider) * rotated.StrideU()]);
rotated.DataU()[(x / plane_divider.x) +
(y / plane_divider.y) * rotated.StrideU()]);
EXPECT_EQ(colors[i].v,
rotated.DataV()[(x / plane_divider) +
(y / plane_divider) * rotated.StrideV()]);
rotated.DataV()[(x / plane_divider.x) +
(y / plane_divider.y) * rotated.StrideV()]);
}
}
@ -262,6 +287,9 @@ rtc::scoped_refptr<T> CreateAndFillBuffer() {
if (buf->type() == VideoFrameBuffer::Type::kI444) {
memset(buf->MutableDataU(), 2, 200);
memset(buf->MutableDataV(), 3, 200);
} else if (buf->type() == VideoFrameBuffer::Type::kI422) {
memset(buf->MutableDataU(), 2, 100);
memset(buf->MutableDataV(), 3, 100);
} else {
memset(buf->MutableDataU(), 2, 50);
memset(buf->MutableDataV(), 3, 50);
@ -340,7 +368,8 @@ REGISTER_TYPED_TEST_SUITE_P(TestPlanarYuvBuffer,
CropYNotCenter,
CropAndScale16x9);
using TestTypesAll = ::testing::Types<I420Buffer, I010Buffer, I444Buffer>;
using TestTypesAll =
::testing::Types<I420Buffer, I010Buffer, I444Buffer, I422Buffer>;
INSTANTIATE_TYPED_TEST_SUITE_P(All, TestPlanarYuvBuffer, TestTypesAll);
template <class T>
@ -382,7 +411,8 @@ TYPED_TEST_P(TestPlanarYuvBufferRotate, Rotates) {
REGISTER_TYPED_TEST_SUITE_P(TestPlanarYuvBufferRotate, Rotates);
using TestTypesRotate = ::testing::Types<I420Buffer, I010Buffer, I444Buffer>;
using TestTypesRotate =
::testing::Types<I420Buffer, I010Buffer, I444Buffer, I422Buffer>;
INSTANTIATE_TYPED_TEST_SUITE_P(Rotate,
TestPlanarYuvBufferRotate,
TestTypesRotate);

View File

@ -41,10 +41,9 @@ namespace webrtc {
namespace {
constexpr std::array<AVPixelFormat, 2> kPixelFormatsDefault = {
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV444P};
constexpr std::array<AVPixelFormat, 2> kPixelFormatsFullRange = {
AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ444P};
constexpr std::array<AVPixelFormat, 6> kPixelFormatsSupported = {
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P};
const size_t kYPlaneIndex = 0;
const size_t kUPlaneIndex = 1;
const size_t kVPlaneIndex = 2;
@ -78,17 +77,11 @@ int H264DecoderImpl::AVGetBuffer2(AVCodecContext* context,
// Necessary capability to be allowed to provide our own buffers.
RTC_DCHECK(context->codec->capabilities | AV_CODEC_CAP_DR1);
// Limited or full range YUV420 or YUV444 is expected.
auto pixelFormatDefault = std::find_if(
kPixelFormatsDefault.begin(), kPixelFormatsDefault.end(),
[context](AVPixelFormat format) { return context->pix_fmt == format; });
auto pixelFormatFullRange = std::find_if(
kPixelFormatsFullRange.begin(), kPixelFormatsFullRange.end(),
auto pixelFormatSupported = std::find_if(
kPixelFormatsSupported.begin(), kPixelFormatsSupported.end(),
[context](AVPixelFormat format) { return context->pix_fmt == format; });
// Limited or full range YUV420 is expected.
RTC_CHECK(pixelFormatDefault != kPixelFormatsDefault.end() ||
pixelFormatFullRange != kPixelFormatsFullRange.end());
RTC_CHECK(pixelFormatSupported != kPixelFormatsSupported.end());
// `av_frame->width` and `av_frame->height` are set by FFmpeg. These are the
// actual image's dimensions and may be different from `context->width` and
@ -125,6 +118,7 @@ int H264DecoderImpl::AVGetBuffer2(AVCodecContext* context,
rtc::scoped_refptr<PlanarYuv8Buffer> frame_buffer;
rtc::scoped_refptr<I444Buffer> i444_buffer;
rtc::scoped_refptr<I420Buffer> i420_buffer;
rtc::scoped_refptr<I422Buffer> i422_buffer;
switch (context->pix_fmt) {
case AV_PIX_FMT_YUV420P:
case AV_PIX_FMT_YUVJ420P:
@ -153,6 +147,19 @@ int H264DecoderImpl::AVGetBuffer2(AVCodecContext* context,
av_frame->linesize[kVPlaneIndex] = i444_buffer->StrideV();
frame_buffer = i444_buffer;
break;
case AV_PIX_FMT_YUV422P:
case AV_PIX_FMT_YUVJ422P:
i422_buffer =
decoder->ffmpeg_buffer_pool_.CreateI422Buffer(width, height);
// Set `av_frame` members as required by FFmpeg.
av_frame->data[kYPlaneIndex] = i422_buffer->MutableDataY();
av_frame->linesize[kYPlaneIndex] = i422_buffer->StrideY();
av_frame->data[kUPlaneIndex] = i422_buffer->MutableDataU();
av_frame->linesize[kUPlaneIndex] = i422_buffer->StrideU();
av_frame->data[kVPlaneIndex] = i422_buffer->MutableDataV();
av_frame->linesize[kVPlaneIndex] = i422_buffer->StrideV();
frame_buffer = i422_buffer;
break;
default:
RTC_LOG(LS_ERROR) << "Unsupported buffer type " << context->pix_fmt
<< ". Check supported supported pixel formats!";
@ -363,9 +370,12 @@ int32_t H264DecoderImpl::Decode(const EncodedImage& input_image,
case VideoFrameBuffer::Type::kI444:
planar_yuv8_buffer = frame_buffer->GetI444();
break;
case VideoFrameBuffer::Type::kI422:
planar_yuv8_buffer = frame_buffer->GetI422();
break;
default:
// If this code is changed to allow other video frame buffer type,
// make sure that the code below which wraps I420/I444 buffer and
// make sure that the code below which wraps I420/I422/I444 buffer and
// code which converts to NV12 is changed
// to work with new video frame buffer type
@ -400,22 +410,40 @@ int32_t H264DecoderImpl::Decode(const EncodedImage& input_image,
2);
rtc::scoped_refptr<webrtc::VideoFrameBuffer> cropped_buffer;
if (video_frame_buffer_type == VideoFrameBuffer::Type::kI420) {
cropped_buffer = WrapI420Buffer(
av_frame_->width, av_frame_->height, av_frame_->data[kYPlaneIndex],
av_frame_->linesize[kYPlaneIndex], av_frame_->data[kUPlaneIndex],
av_frame_->linesize[kUPlaneIndex], av_frame_->data[kVPlaneIndex],
av_frame_->linesize[kVPlaneIndex],
// To keep reference alive.
[frame_buffer] {});
} else {
cropped_buffer = WrapI444Buffer(
av_frame_->width, av_frame_->height, av_frame_->data[kYPlaneIndex],
av_frame_->linesize[kYPlaneIndex], av_frame_->data[kUPlaneIndex],
av_frame_->linesize[kUPlaneIndex], av_frame_->data[kVPlaneIndex],
av_frame_->linesize[kVPlaneIndex],
// To keep reference alive.
[frame_buffer] {});
switch (video_frame_buffer_type) {
case VideoFrameBuffer::Type::kI420:
cropped_buffer = WrapI420Buffer(
av_frame_->width, av_frame_->height, av_frame_->data[kYPlaneIndex],
av_frame_->linesize[kYPlaneIndex], av_frame_->data[kUPlaneIndex],
av_frame_->linesize[kUPlaneIndex], av_frame_->data[kVPlaneIndex],
av_frame_->linesize[kVPlaneIndex],
// To keep reference alive.
[frame_buffer] {});
break;
case VideoFrameBuffer::Type::kI444:
cropped_buffer = WrapI444Buffer(
av_frame_->width, av_frame_->height, av_frame_->data[kYPlaneIndex],
av_frame_->linesize[kYPlaneIndex], av_frame_->data[kUPlaneIndex],
av_frame_->linesize[kUPlaneIndex], av_frame_->data[kVPlaneIndex],
av_frame_->linesize[kVPlaneIndex],
// To keep reference alive.
[frame_buffer] {});
break;
case VideoFrameBuffer::Type::kI422:
cropped_buffer = WrapI422Buffer(
av_frame_->width, av_frame_->height, av_frame_->data[kYPlaneIndex],
av_frame_->linesize[kYPlaneIndex], av_frame_->data[kUPlaneIndex],
av_frame_->linesize[kUPlaneIndex], av_frame_->data[kVPlaneIndex],
av_frame_->linesize[kVPlaneIndex],
// To keep reference alive.
[frame_buffer] {});
break;
default:
RTC_LOG(LS_ERROR) << "frame_buffer type: "
<< static_cast<int32_t>(video_frame_buffer_type)
<< " is not supported!";
ReportError();
return WEBRTC_VIDEO_CODEC_ERROR;
}
if (preferred_output_format_ == VideoFrameBuffer::Type::kNV12) {
@ -423,30 +451,53 @@ int32_t H264DecoderImpl::Decode(const EncodedImage& input_image,
cropped_buffer->width(), cropped_buffer->height());
const PlanarYuv8Buffer* cropped_planar_yuv8_buffer = nullptr;
if (video_frame_buffer_type == VideoFrameBuffer::Type::kI420) {
cropped_planar_yuv8_buffer = cropped_buffer->GetI420();
libyuv::I420ToNV12(cropped_planar_yuv8_buffer->DataY(),
cropped_planar_yuv8_buffer->StrideY(),
cropped_planar_yuv8_buffer->DataU(),
cropped_planar_yuv8_buffer->StrideU(),
cropped_planar_yuv8_buffer->DataV(),
cropped_planar_yuv8_buffer->StrideV(),
nv12_buffer->MutableDataY(), nv12_buffer->StrideY(),
nv12_buffer->MutableDataUV(), nv12_buffer->StrideUV(),
planar_yuv8_buffer->width(),
planar_yuv8_buffer->height());
} else {
cropped_planar_yuv8_buffer = cropped_buffer->GetI444();
libyuv::I444ToNV12(cropped_planar_yuv8_buffer->DataY(),
cropped_planar_yuv8_buffer->StrideY(),
cropped_planar_yuv8_buffer->DataU(),
cropped_planar_yuv8_buffer->StrideU(),
cropped_planar_yuv8_buffer->DataV(),
cropped_planar_yuv8_buffer->StrideV(),
nv12_buffer->MutableDataY(), nv12_buffer->StrideY(),
nv12_buffer->MutableDataUV(), nv12_buffer->StrideUV(),
planar_yuv8_buffer->width(),
planar_yuv8_buffer->height());
switch (video_frame_buffer_type) {
case VideoFrameBuffer::Type::kI420:
cropped_planar_yuv8_buffer = cropped_buffer->GetI420();
libyuv::I420ToNV12(cropped_planar_yuv8_buffer->DataY(),
cropped_planar_yuv8_buffer->StrideY(),
cropped_planar_yuv8_buffer->DataU(),
cropped_planar_yuv8_buffer->StrideU(),
cropped_planar_yuv8_buffer->DataV(),
cropped_planar_yuv8_buffer->StrideV(),
nv12_buffer->MutableDataY(), nv12_buffer->StrideY(),
nv12_buffer->MutableDataUV(),
nv12_buffer->StrideUV(), planar_yuv8_buffer->width(),
planar_yuv8_buffer->height());
break;
case VideoFrameBuffer::Type::kI444:
cropped_planar_yuv8_buffer = cropped_buffer->GetI444();
libyuv::I444ToNV12(cropped_planar_yuv8_buffer->DataY(),
cropped_planar_yuv8_buffer->StrideY(),
cropped_planar_yuv8_buffer->DataU(),
cropped_planar_yuv8_buffer->StrideU(),
cropped_planar_yuv8_buffer->DataV(),
cropped_planar_yuv8_buffer->StrideV(),
nv12_buffer->MutableDataY(), nv12_buffer->StrideY(),
nv12_buffer->MutableDataUV(),
nv12_buffer->StrideUV(), planar_yuv8_buffer->width(),
planar_yuv8_buffer->height());
break;
case VideoFrameBuffer::Type::kI422:
cropped_planar_yuv8_buffer = cropped_buffer->GetI422();
// Swap src_u and src_v to implement I422ToNV12.
libyuv::I422ToNV21(cropped_planar_yuv8_buffer->DataY(),
cropped_planar_yuv8_buffer->StrideY(),
cropped_planar_yuv8_buffer->DataV(),
cropped_planar_yuv8_buffer->StrideV(),
cropped_planar_yuv8_buffer->DataU(),
cropped_planar_yuv8_buffer->StrideU(),
nv12_buffer->MutableDataY(), nv12_buffer->StrideY(),
nv12_buffer->MutableDataUV(),
nv12_buffer->StrideUV(), planar_yuv8_buffer->width(),
planar_yuv8_buffer->height());
break;
default:
RTC_LOG(LS_ERROR) << "frame_buffer type: "
<< static_cast<int32_t>(video_frame_buffer_type)
<< " is not supported!";
ReportError();
return WEBRTC_VIDEO_CODEC_ERROR;
}
cropped_buffer = nv12_buffer;