xref: /aosp_15_r20/external/webrtc/modules/video_coding/codecs/h264/test/h264_impl_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2017 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 <stdint.h>
12 
13 #include <memory>
14 
15 #include "absl/types/optional.h"
16 #include "api/video/color_space.h"
17 #include "api/video/encoded_image.h"
18 #include "api/video/video_frame.h"
19 #include "api/video_codecs/video_codec.h"
20 #include "api/video_codecs/video_decoder.h"
21 #include "api/video_codecs/video_encoder.h"
22 #include "common_video/libyuv/include/webrtc_libyuv.h"
23 #include "media/base/codec.h"
24 #include "media/base/media_constants.h"
25 #include "modules/video_coding/codecs/h264/include/h264.h"
26 #include "modules/video_coding/codecs/test/video_codec_unittest.h"
27 #include "modules/video_coding/include/video_codec_interface.h"
28 #include "modules/video_coding/include/video_error_codes.h"
29 #include "test/gtest.h"
30 #include "test/video_codec_settings.h"
31 
32 namespace webrtc {
33 
34 class TestH264Impl : public VideoCodecUnitTest {
35  protected:
CreateEncoder()36   std::unique_ptr<VideoEncoder> CreateEncoder() override {
37     return H264Encoder::Create(cricket::VideoCodec(cricket::kH264CodecName));
38   }
39 
CreateDecoder()40   std::unique_ptr<VideoDecoder> CreateDecoder() override {
41     return H264Decoder::Create();
42   }
43 
ModifyCodecSettings(VideoCodec * codec_settings)44   void ModifyCodecSettings(VideoCodec* codec_settings) override {
45     webrtc::test::CodecSettings(kVideoCodecH264, codec_settings);
46   }
47 };
48 
49 #ifdef WEBRTC_USE_H264
50 #define MAYBE_EncodeDecode EncodeDecode
51 #define MAYBE_DecodedQpEqualsEncodedQp DecodedQpEqualsEncodedQp
52 #else
53 #define MAYBE_EncodeDecode DISABLED_EncodeDecode
54 #define MAYBE_DecodedQpEqualsEncodedQp DISABLED_DecodedQpEqualsEncodedQp
55 #endif
56 
TEST_F(TestH264Impl,MAYBE_EncodeDecode)57 TEST_F(TestH264Impl, MAYBE_EncodeDecode) {
58   VideoFrame input_frame = NextInputFrame();
59   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(input_frame, nullptr));
60   EncodedImage encoded_frame;
61   CodecSpecificInfo codec_specific_info;
62   ASSERT_TRUE(WaitForEncodedFrame(&encoded_frame, &codec_specific_info));
63   // First frame should be a key frame.
64   encoded_frame._frameType = VideoFrameType::kVideoFrameKey;
65   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, decoder_->Decode(encoded_frame, false, 0));
66   std::unique_ptr<VideoFrame> decoded_frame;
67   absl::optional<uint8_t> decoded_qp;
68   ASSERT_TRUE(WaitForDecodedFrame(&decoded_frame, &decoded_qp));
69   ASSERT_TRUE(decoded_frame);
70   EXPECT_GT(I420PSNR(&input_frame, decoded_frame.get()), 36);
71 
72   const ColorSpace color_space = *decoded_frame->color_space();
73   EXPECT_EQ(ColorSpace::PrimaryID::kUnspecified, color_space.primaries());
74   EXPECT_EQ(ColorSpace::TransferID::kUnspecified, color_space.transfer());
75   EXPECT_EQ(ColorSpace::MatrixID::kUnspecified, color_space.matrix());
76   EXPECT_EQ(ColorSpace::RangeID::kInvalid, color_space.range());
77   EXPECT_EQ(ColorSpace::ChromaSiting::kUnspecified,
78             color_space.chroma_siting_horizontal());
79   EXPECT_EQ(ColorSpace::ChromaSiting::kUnspecified,
80             color_space.chroma_siting_vertical());
81 }
82 
TEST_F(TestH264Impl,MAYBE_DecodedQpEqualsEncodedQp)83 TEST_F(TestH264Impl, MAYBE_DecodedQpEqualsEncodedQp) {
84   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(NextInputFrame(), nullptr));
85   EncodedImage encoded_frame;
86   CodecSpecificInfo codec_specific_info;
87   ASSERT_TRUE(WaitForEncodedFrame(&encoded_frame, &codec_specific_info));
88   // First frame should be a key frame.
89   encoded_frame._frameType = VideoFrameType::kVideoFrameKey;
90   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, decoder_->Decode(encoded_frame, false, 0));
91   std::unique_ptr<VideoFrame> decoded_frame;
92   absl::optional<uint8_t> decoded_qp;
93   ASSERT_TRUE(WaitForDecodedFrame(&decoded_frame, &decoded_qp));
94   ASSERT_TRUE(decoded_frame);
95   ASSERT_TRUE(decoded_qp);
96   EXPECT_EQ(encoded_frame.qp_, *decoded_qp);
97 }
98 
99 }  // namespace webrtc
100