ndk_video_encode_accelerator_tests.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <algorithm>
  5. #include <map>
  6. #include <vector>
  7. #include "base/android/build_info.h"
  8. #include "base/logging.h"
  9. #include "base/memory/ptr_util.h"
  10. #include "base/run_loop.h"
  11. #include "base/test/task_environment.h"
  12. #include "media/base/bitstream_buffer.h"
  13. #include "media/base/media_util.h"
  14. #include "media/base/test_helpers.h"
  15. #include "media/base/video_codecs.h"
  16. #include "media/base/video_frame.h"
  17. #include "media/base/video_util.h"
  18. #include "media/gpu/android/ndk_video_encode_accelerator.h"
  19. #include "media/video/fake_gpu_memory_buffer.h"
  20. #include "testing/gtest/include/gtest/gtest.h"
  21. #include "third_party/abseil-cpp/absl/types/optional.h"
  22. #include "third_party/libyuv/include/libyuv.h"
  23. #include "third_party/libyuv/include/libyuv/convert_from.h"
  24. using testing::Return;
  25. namespace media {
  26. struct VideoParams {
  27. VideoCodecProfile profile;
  28. VideoPixelFormat pixel_format;
  29. };
  30. class NdkVideoEncoderAcceleratorTest
  31. : public ::testing::TestWithParam<VideoParams>,
  32. public VideoEncodeAccelerator::Client {
  33. public:
  34. void SetUp() override {
  35. if (!NdkVideoEncodeAccelerator::IsSupported())
  36. GTEST_SKIP() << "Not supported Android version";
  37. auto args = GetParam();
  38. profile_ = args.profile;
  39. codec_ = VideoCodecProfileToVideoCodec(profile_);
  40. pixel_format_ = args.pixel_format;
  41. auto profiles = MakeNdkAccelerator()->GetSupportedProfiles();
  42. bool codec_supported =
  43. std::any_of(profiles.begin(), profiles.end(),
  44. [this](VideoEncodeAccelerator::SupportedProfile p) {
  45. return p.profile == profile_;
  46. });
  47. if (!codec_supported) {
  48. GTEST_SKIP() << "Device doesn't have hw encoder for: "
  49. << GetProfileName(profile_);
  50. }
  51. }
  52. void TearDown() override {}
  53. // Implementation for VEA::Client
  54. void RequireBitstreamBuffers(unsigned int input_count,
  55. const gfx::Size& input_coded_size,
  56. size_t output_buffer_size) override {
  57. output_buffer_size_ = output_buffer_size;
  58. input_buffer_size_ =
  59. VideoFrame::AllocationSize(PIXEL_FORMAT_I420, input_coded_size);
  60. SendNewBuffer();
  61. if (!OnRequireBuffer())
  62. loop_.Quit();
  63. }
  64. void BitstreamBufferReady(int32_t bitstream_buffer_id,
  65. const BitstreamBufferMetadata& metadata) override {
  66. outputs_.push_back({bitstream_buffer_id, metadata});
  67. SendNewBuffer();
  68. if (!OnBufferReady())
  69. loop_.Quit();
  70. }
  71. void NotifyError(VideoEncodeAccelerator::Error error) override {
  72. error_ = error;
  73. if (!OnError())
  74. loop_.Quit();
  75. }
  76. MOCK_METHOD(bool, OnRequireBuffer, ());
  77. MOCK_METHOD(bool, OnBufferReady, ());
  78. MOCK_METHOD(bool, OnError, ());
  79. protected:
  80. void SendNewBuffer() {
  81. auto buffer = output_pool_->MaybeAllocateBuffer(output_buffer_size_);
  82. if (!buffer) {
  83. FAIL() << "Can't allocate memory buffer";
  84. }
  85. const base::UnsafeSharedMemoryRegion& region = buffer->GetRegion();
  86. auto mapping = region.Map();
  87. memset(mapping.memory(), 0, mapping.size());
  88. auto id = ++last_buffer_id_;
  89. accelerator_->UseOutputBitstreamBuffer(
  90. BitstreamBuffer(id, region.Duplicate(), region.GetSize()));
  91. id_to_buffer_[id] = std::move(buffer);
  92. }
  93. scoped_refptr<VideoFrame> CreateI420Frame(gfx::Size size,
  94. uint32_t color,
  95. base::TimeDelta timestamp) {
  96. auto frame = VideoFrame::CreateFrame(PIXEL_FORMAT_I420, size,
  97. gfx::Rect(size), size, timestamp);
  98. auto y = color & 0xFF;
  99. auto u = (color >> 8) & 0xFF;
  100. auto v = (color >> 16) & 0xFF;
  101. libyuv::I420Rect(
  102. frame->data(VideoFrame::kYPlane), frame->stride(VideoFrame::kYPlane),
  103. frame->data(VideoFrame::kUPlane), frame->stride(VideoFrame::kUPlane),
  104. frame->data(VideoFrame::kVPlane), frame->stride(VideoFrame::kVPlane),
  105. 0, // left
  106. 0, // top
  107. frame->visible_rect().width(), // right
  108. frame->visible_rect().height(), // bottom
  109. y, // Y color
  110. u, // U color
  111. v); // V color
  112. return frame;
  113. }
  114. scoped_refptr<VideoFrame> CreateNV12Frame(gfx::Size size,
  115. uint32_t color,
  116. base::TimeDelta timestamp) {
  117. auto i420_frame = CreateI420Frame(size, color, timestamp);
  118. auto nv12_frame = VideoFrame::CreateFrame(PIXEL_FORMAT_NV12, size,
  119. gfx::Rect(size), size, timestamp);
  120. auto status = ConvertAndScaleFrame(*i420_frame, *nv12_frame, resize_buff_);
  121. EXPECT_TRUE(status.is_ok());
  122. return nv12_frame;
  123. }
  124. scoped_refptr<VideoFrame> CreateRGBFrame(gfx::Size size,
  125. uint32_t color,
  126. base::TimeDelta timestamp) {
  127. auto frame = VideoFrame::CreateFrame(PIXEL_FORMAT_XRGB, size,
  128. gfx::Rect(size), size, timestamp);
  129. libyuv::ARGBRect(frame->data(VideoFrame::kARGBPlane),
  130. frame->stride(VideoFrame::kARGBPlane),
  131. 0, // left
  132. 0, // top
  133. frame->visible_rect().width(), // right
  134. frame->visible_rect().height(), // bottom
  135. color);
  136. return frame;
  137. }
  138. scoped_refptr<VideoFrame> CreateFrame(gfx::Size size,
  139. VideoPixelFormat format,
  140. base::TimeDelta timestamp,
  141. uint32_t color = 0x964050) {
  142. switch (format) {
  143. case PIXEL_FORMAT_I420:
  144. return CreateI420Frame(size, color, timestamp);
  145. case PIXEL_FORMAT_NV12:
  146. return CreateNV12Frame(size, color, timestamp);
  147. case PIXEL_FORMAT_XRGB:
  148. return CreateRGBFrame(size, color, timestamp);
  149. default:
  150. EXPECT_TRUE(false) << "not supported pixel format";
  151. return nullptr;
  152. }
  153. }
  154. VideoEncodeAccelerator::Config GetDefaultConfig() {
  155. gfx::Size frame_size(640, 480);
  156. uint32_t framerate = 30;
  157. auto bitrate = Bitrate::ConstantBitrate(1000000u);
  158. return VideoEncodeAccelerator::Config(pixel_format_, frame_size, profile_,
  159. bitrate, framerate, 1000);
  160. }
  161. void Run() { loop_.Run(); }
  162. std::unique_ptr<NullMediaLog> NullLog() {
  163. return std::make_unique<NullMediaLog>();
  164. }
  165. std::unique_ptr<VideoEncodeAccelerator> MakeNdkAccelerator() {
  166. auto runner = task_environment_.GetMainThreadTaskRunner();
  167. return base::WrapUnique<VideoEncodeAccelerator>(
  168. new NdkVideoEncodeAccelerator(runner));
  169. }
  170. VideoCodec codec_;
  171. VideoCodecProfile profile_;
  172. VideoPixelFormat pixel_format_;
  173. base::test::TaskEnvironment task_environment_;
  174. base::RunLoop loop_;
  175. std::unique_ptr<VideoEncodeAccelerator> accelerator_;
  176. size_t output_buffer_size_ = 0;
  177. scoped_refptr<base::UnsafeSharedMemoryPool> output_pool_ =
  178. base::MakeRefCounted<base::UnsafeSharedMemoryPool>();
  179. std::map<int32_t, std::unique_ptr<base::UnsafeSharedMemoryPool::Handle>>
  180. id_to_buffer_;
  181. struct Output {
  182. int32_t id;
  183. BitstreamBufferMetadata md;
  184. };
  185. std::vector<Output> outputs_;
  186. absl::optional<VideoEncodeAccelerator::Error> error_;
  187. size_t input_buffer_size_ = 0;
  188. int32_t last_buffer_id_ = 0;
  189. std::vector<uint8_t> resize_buff_;
  190. };
  191. TEST_P(NdkVideoEncoderAcceleratorTest, InitializeAndDestroy) {
  192. auto config = GetDefaultConfig();
  193. accelerator_ = MakeNdkAccelerator();
  194. EXPECT_CALL(*this, OnRequireBuffer()).WillOnce(Return(false));
  195. bool result = accelerator_->Initialize(config, this, NullLog());
  196. ASSERT_TRUE(result);
  197. Run();
  198. EXPECT_GE(id_to_buffer_.size(), 1u);
  199. accelerator_.reset();
  200. EXPECT_FALSE(error_.has_value());
  201. }
  202. TEST_P(NdkVideoEncoderAcceleratorTest, HandleEncodingError) {
  203. auto config = GetDefaultConfig();
  204. accelerator_ = MakeNdkAccelerator();
  205. EXPECT_CALL(*this, OnRequireBuffer()).WillOnce(Return(true));
  206. EXPECT_CALL(*this, OnError()).WillOnce(Return(false));
  207. bool result = accelerator_->Initialize(config, this, NullLog());
  208. ASSERT_TRUE(result);
  209. auto size = config.input_visible_size;
  210. // A frame with unsupported pixel format works as a way to induce a error.
  211. auto frame = VideoFrame::CreateFrame(PIXEL_FORMAT_NV21, size, gfx::Rect(size),
  212. size, {});
  213. accelerator_->Encode(frame, true);
  214. Run();
  215. EXPECT_EQ(outputs_.size(), 0u);
  216. EXPECT_TRUE(error_.has_value());
  217. }
  218. TEST_P(NdkVideoEncoderAcceleratorTest, EncodeSeveralFrames) {
  219. const size_t total_frames_count = 10;
  220. const size_t key_frame_index = 7;
  221. auto config = GetDefaultConfig();
  222. accelerator_ = MakeNdkAccelerator();
  223. EXPECT_CALL(*this, OnRequireBuffer()).WillRepeatedly(Return(true));
  224. EXPECT_CALL(*this, OnBufferReady()).WillRepeatedly([this]() {
  225. if (outputs_.size() < total_frames_count)
  226. return true;
  227. return false;
  228. });
  229. bool result = accelerator_->Initialize(config, this, NullLog());
  230. ASSERT_TRUE(result);
  231. uint32_t color = 0x964050;
  232. auto duration = base::Milliseconds(16);
  233. for (auto frame_index = 0u; frame_index < total_frames_count; frame_index++) {
  234. auto timestamp = frame_index * duration;
  235. auto frame =
  236. CreateFrame(config.input_visible_size, pixel_format_, timestamp, color);
  237. color = (color << 1) + frame_index;
  238. bool key_frame = (frame_index == key_frame_index);
  239. accelerator_->Encode(frame, key_frame);
  240. }
  241. Run();
  242. EXPECT_FALSE(error_.has_value());
  243. EXPECT_GE(outputs_.size(), total_frames_count);
  244. // Here we'd like to test that an output with at `key_frame_index`
  245. // has a keyframe flag set to true, but because MediaCodec
  246. // is unreliable in inserting keyframes at our request we can't test
  247. // for it. In practice it usually works, just not always.
  248. for (auto& output : outputs_) {
  249. auto& mapping = id_to_buffer_[output.id]->GetMapping();
  250. EXPECT_GE(mapping.size(), output.md.payload_size_bytes);
  251. EXPECT_GT(output.md.payload_size_bytes, 0u);
  252. auto span = mapping.GetMemoryAsSpan<uint8_t>();
  253. bool found_not_zero =
  254. std::any_of(span.begin(), span.end(), [](uint8_t x) { return x != 0; });
  255. EXPECT_TRUE(found_not_zero);
  256. }
  257. }
  258. std::string PrintTestParams(const testing::TestParamInfo<VideoParams>& info) {
  259. auto result = GetProfileName(info.param.profile) + "__" +
  260. VideoPixelFormatToString(info.param.pixel_format);
  261. // GTest doesn't like spaces, but profile names have spaces, so we need
  262. // to replace them with underscores.
  263. std::replace(result.begin(), result.end(), ' ', '_');
  264. return result;
  265. }
  266. VideoParams kParams[] = {
  267. {VP8PROFILE_MIN, PIXEL_FORMAT_I420},
  268. {VP8PROFILE_MIN, PIXEL_FORMAT_NV12},
  269. {H264PROFILE_BASELINE, PIXEL_FORMAT_I420},
  270. {H264PROFILE_BASELINE, PIXEL_FORMAT_NV12},
  271. };
  272. INSTANTIATE_TEST_SUITE_P(AllNdkEncoderTests,
  273. NdkVideoEncoderAcceleratorTest,
  274. ::testing::ValuesIn(kParams),
  275. PrintTestParams);
  276. } // namespace media