summaryrefslogtreecommitdiffstats
path: root/media/base/video_frame_impl_unittest.cc
blob: a6a1a6acf1626af5b4d9e164b48b14657b2f4937 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/base/buffers.h"
#include "media/base/mock_media_filters.h"
#include "media/base/video_frame_impl.h"
#include "media/base/yuv_convert.h"
#include "testing/gtest/include/gtest/gtest.h"

using media::VideoFrameImpl;
using media::VideoSurface;

namespace {

// Given a |yv12_frame| this method converts the YV12 frame to RGBA and
// makes sure that all the pixels of the RBG frame equal |expect_rgb_color|.
void ExpectFrameColor(media::VideoFrame* yv12_frame, uint32 expect_rgb_color) {
  // On linux and mac builds if you directly compare using EXPECT_EQ and use
  // the VideoSurface::kNumxxxPlanes constants, it generates an error when
  // linking.  These are declared so that we can compare against locals.
  const size_t expect_yuv_planes = VideoSurface::kNumYUVPlanes;
  const size_t expect_rgb_planes = VideoSurface::kNumRGBPlanes;

  VideoSurface yuv_surface;
  EXPECT_TRUE(yv12_frame->Lock(&yuv_surface));
  EXPECT_EQ(yuv_surface.format, VideoSurface::YV12);
  EXPECT_EQ(yuv_surface.planes, expect_yuv_planes);
  EXPECT_EQ(yuv_surface.strides[VideoSurface::kUPlane],
            yuv_surface.strides[VideoSurface::kVPlane]);

  scoped_refptr<media::VideoFrame> rgb_frame;
  media::VideoFrameImpl::CreateFrame(VideoSurface::RGBA,
                                     yuv_surface.width,
                                     yuv_surface.height,
                                     yv12_frame->GetTimestamp(),
                                     yv12_frame->GetDuration(),
                                     &rgb_frame);
  media::VideoSurface rgb_surface;
  EXPECT_TRUE(rgb_frame->Lock(&rgb_surface));
  EXPECT_EQ(rgb_surface.width, yuv_surface.width);
  EXPECT_EQ(rgb_surface.height, yuv_surface.height);
  EXPECT_EQ(rgb_surface.planes, expect_rgb_planes);

  media::ConvertYUVToRGB32(yuv_surface.data[VideoSurface::kYPlane],
                           yuv_surface.data[VideoSurface::kUPlane],
                           yuv_surface.data[VideoSurface::kVPlane],
                           rgb_surface.data[VideoSurface::kRGBPlane],
                           rgb_surface.width,
                           rgb_surface.height,
                           yuv_surface.strides[VideoSurface::kYPlane],
                           yuv_surface.strides[VideoSurface::kUPlane],
                           rgb_surface.strides[VideoSurface::kRGBPlane],
                           media::YV12);

  for (size_t row = 0; row < rgb_surface.height; ++row) {
    uint32* rgb_row_data = reinterpret_cast<uint32*>(
        rgb_surface.data[VideoSurface::kRGBPlane] +
        (rgb_surface.strides[VideoSurface::kRGBPlane] * row));
    for (size_t col = 0; col < rgb_surface.width; ++col) {
      EXPECT_EQ(rgb_row_data[col], expect_rgb_color);
    }
  }
  rgb_frame->Unlock();
  yv12_frame->Unlock();
}

}  // namespace


TEST(VideoFrameImpl, Basic) {
  const size_t kWidth = 64;
  const size_t kHeight = 48;
  const base::TimeDelta kTimestampA = base::TimeDelta::FromMicroseconds(1337);
  const base::TimeDelta kDurationA = base::TimeDelta::FromMicroseconds(1667);
  const base::TimeDelta kTimestampB = base::TimeDelta::FromMicroseconds(1234);
  const base::TimeDelta kDurationB = base::TimeDelta::FromMicroseconds(5678);

  // Create a YV12 Video Frame.
  scoped_refptr<media::VideoFrame> frame;
  media::VideoFrameImpl::CreateFrame(media::VideoSurface::YV12, kWidth, kHeight,
                                     kTimestampA, kDurationA, &frame);
  ASSERT_TRUE(frame);

  // Test StreamSample implementation.
  EXPECT_TRUE(kTimestampA == frame->GetTimestamp());
  EXPECT_TRUE(kDurationA == frame->GetDuration());
  EXPECT_FALSE(frame->IsEndOfStream());
  EXPECT_FALSE(frame->IsDiscontinuous());
  frame->SetTimestamp(kTimestampB);
  frame->SetDuration(kDurationB);
  EXPECT_TRUE(kTimestampB == frame->GetTimestamp());
  EXPECT_TRUE(kDurationB == frame->GetDuration());
  EXPECT_FALSE(frame->IsEndOfStream());
  frame->SetDiscontinuous(true);
  EXPECT_TRUE(frame->IsDiscontinuous());
  frame->SetDiscontinuous(false);
  EXPECT_FALSE(frame->IsDiscontinuous());

  // Test VideoFrame implementation.
  media::old_mocks::MockVideoDecoder::InitializeYV12Frame(frame, 0.0f);
  ExpectFrameColor(frame, 0xFF000000);
  media::old_mocks::MockVideoDecoder::InitializeYV12Frame(frame, 1.0f);
  ExpectFrameColor(frame, 0xFFFFFFFF);

  // Test an empty frame.
  media::VideoFrameImpl::CreateEmptyFrame(&frame);
  EXPECT_TRUE(frame->IsEndOfStream());
}