tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nv12_buffer_unittest.cc (4012B)


      1 /*
      2 *  Copyright (c) 2020 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 "api/video/nv12_buffer.h"
     12 
     13 #include <cstdint>
     14 #include <cstring>
     15 
     16 #include "api/scoped_refptr.h"
     17 #include "api/video/i420_buffer.h"
     18 #include "api/video/video_frame_buffer.h"
     19 #include "test/frame_utils.h"
     20 #include "test/gtest.h"
     21 
     22 namespace webrtc {
     23 
     24 namespace {
     25 int GetY(scoped_refptr<NV12BufferInterface> buf, int col, int row) {
     26  return buf->DataY()[row * buf->StrideY() + col];
     27 }
     28 
     29 int GetU(scoped_refptr<NV12BufferInterface> buf, int col, int row) {
     30  return buf->DataUV()[(row / 2) * buf->StrideUV() + (col / 2) * 2];
     31 }
     32 
     33 int GetV(scoped_refptr<NV12BufferInterface> buf, int col, int row) {
     34  return buf->DataUV()[(row / 2) * buf->StrideUV() + (col / 2) * 2 + 1];
     35 }
     36 
     37 void FillNV12Buffer(scoped_refptr<NV12Buffer> buf) {
     38  const uint8_t Y = 1;
     39  const uint8_t U = 2;
     40  const uint8_t V = 3;
     41  for (int row = 0; row < buf->height(); ++row) {
     42    for (int col = 0; col < buf->width(); ++col) {
     43      buf->MutableDataY()[row * buf->StrideY() + col] = Y;
     44    }
     45  }
     46  // Fill interleaving UV values.
     47  for (int row = 0; row < buf->ChromaHeight(); row++) {
     48    for (int col = 0; col < buf->StrideUV(); col += 2) {
     49      int uv_index = row * buf->StrideUV() + col;
     50      buf->MutableDataUV()[uv_index] = U;
     51      buf->MutableDataUV()[uv_index + 1] = V;
     52    }
     53  }
     54 }
     55 
     56 }  // namespace
     57 
     58 TEST(NV12BufferTest, InitialData) {
     59  constexpr int stride_y = 3;
     60  constexpr int stride_uv = 4;
     61  constexpr int width = 3;
     62  constexpr int height = 3;
     63 
     64  scoped_refptr<NV12Buffer> nv12_buffer(NV12Buffer::Create(width, height));
     65  EXPECT_EQ(width, nv12_buffer->width());
     66  EXPECT_EQ(height, nv12_buffer->height());
     67  EXPECT_EQ(stride_y, nv12_buffer->StrideY());
     68  EXPECT_EQ(stride_uv, nv12_buffer->StrideUV());
     69  EXPECT_EQ(2, nv12_buffer->ChromaWidth());
     70  EXPECT_EQ(2, nv12_buffer->ChromaHeight());
     71 }
     72 
     73 TEST(NV12BufferTest, ReadPixels) {
     74  constexpr int width = 3;
     75  constexpr int height = 3;
     76 
     77  scoped_refptr<NV12Buffer> nv12_buffer(NV12Buffer::Create(width, height));
     78  // Y = 1, U = 2, V = 3.
     79  FillNV12Buffer(nv12_buffer);
     80  for (int row = 0; row < height; row++) {
     81    for (int col = 0; col < width; col++) {
     82      EXPECT_EQ(1, GetY(nv12_buffer, col, row));
     83      EXPECT_EQ(2, GetU(nv12_buffer, col, row));
     84      EXPECT_EQ(3, GetV(nv12_buffer, col, row));
     85    }
     86  }
     87 }
     88 
     89 TEST(NV12BufferTest, ToI420) {
     90  constexpr int width = 3;
     91  constexpr int height = 3;
     92  constexpr int size_y = width * height;
     93  constexpr int size_u = (width + 1) / 2 * (height + 1) / 2;
     94  constexpr int size_v = (width + 1) / 2 * (height + 1) / 2;
     95  scoped_refptr<I420Buffer> reference(I420Buffer::Create(width, height));
     96  memset(reference->MutableDataY(), 8, size_y);
     97  memset(reference->MutableDataU(), 4, size_u);
     98  memset(reference->MutableDataV(), 2, size_v);
     99 
    100  scoped_refptr<NV12Buffer> nv12_buffer(NV12Buffer::Create(width, height));
    101  // Convert the reference buffer to NV12.
    102  memset(nv12_buffer->MutableDataY(), 8, size_y);
    103  // Interleaving u/v values.
    104  for (int i = 0; i < size_u + size_v; i += 2) {
    105    nv12_buffer->MutableDataUV()[i] = 4;
    106    nv12_buffer->MutableDataUV()[i + 1] = 2;
    107  }
    108  // Confirm YUV values are as expected.
    109  for (int row = 0; row < height; row++) {
    110    for (int col = 0; col < width; col++) {
    111      EXPECT_EQ(8, GetY(nv12_buffer, col, row));
    112      EXPECT_EQ(4, GetU(nv12_buffer, col, row));
    113      EXPECT_EQ(2, GetV(nv12_buffer, col, row));
    114    }
    115  }
    116 
    117  scoped_refptr<I420BufferInterface> i420_buffer(nv12_buffer->ToI420());
    118  EXPECT_EQ(height, i420_buffer->height());
    119  EXPECT_EQ(width, i420_buffer->width());
    120  EXPECT_TRUE(test::FrameBufsEqual(reference, i420_buffer));
    121 }
    122 
    123 }  // namespace webrtc