Per Kjellander | 841c912 | 2018-10-04 16:40:28 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 "test/fake_vp8_decoder.h" |
| 12 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 13 | #include <stddef.h> |
| 14 | |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 15 | #include <optional> |
| 16 | |
Mirko Bonadei | d970807 | 2019-01-25 19:26:48 | [diff] [blame] | 17 | #include "api/scoped_refptr.h" |
Per Kjellander | 841c912 | 2018-10-04 16:40:28 | [diff] [blame] | 18 | #include "api/video/i420_buffer.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 19 | #include "api/video/video_frame.h" |
| 20 | #include "api/video/video_frame_buffer.h" |
| 21 | #include "api/video/video_rotation.h" |
| 22 | #include "modules/video_coding/include/video_error_codes.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 23 | #include "rtc_base/time_utils.h" |
Per Kjellander | 841c912 | 2018-10-04 16:40:28 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | namespace test { |
| 27 | |
| 28 | namespace { |
| 29 | // Read width and height from the payload of the frame if it is a key frame the |
| 30 | // same way as the real VP8 decoder. |
| 31 | // FakeEncoder writes width, height and frame type. |
| 32 | void ParseFakeVp8(const unsigned char* data, int* width, int* height) { |
| 33 | bool key_frame = data[0] == 0; |
| 34 | if (key_frame) { |
| 35 | *width = ((data[7] << 8) + data[6]) & 0x3FFF; |
| 36 | *height = ((data[9] << 8) + data[8]) & 0x3FFF; |
| 37 | } |
| 38 | } |
| 39 | } // namespace |
| 40 | |
| 41 | FakeVp8Decoder::FakeVp8Decoder() : callback_(nullptr), width_(0), height_(0) {} |
| 42 | |
Danil Chapovalov | d08930d | 2021-08-12 11:26:55 | [diff] [blame] | 43 | bool FakeVp8Decoder::Configure(const Settings& settings) { |
| 44 | return true; |
Per Kjellander | 841c912 | 2018-10-04 16:40:28 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | int32_t FakeVp8Decoder::Decode(const EncodedImage& input, |
Per Kjellander | 841c912 | 2018-10-04 16:40:28 | [diff] [blame] | 48 | int64_t render_time_ms) { |
| 49 | constexpr size_t kMinPayLoadHeaderLength = 10; |
Niels Möller | 77536a2 | 2019-01-15 07:50:01 | [diff] [blame] | 50 | if (input.size() < kMinPayLoadHeaderLength) { |
Per Kjellander | 841c912 | 2018-10-04 16:40:28 | [diff] [blame] | 51 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 52 | } |
Niels Möller | 24871e4 | 2019-01-17 10:31:13 | [diff] [blame] | 53 | ParseFakeVp8(input.data(), &width_, &height_); |
Per Kjellander | 841c912 | 2018-10-04 16:40:28 | [diff] [blame] | 54 | |
Artem Titov | 1ebfb6a | 2019-01-03 22:49:37 | [diff] [blame] | 55 | VideoFrame frame = |
| 56 | VideoFrame::Builder() |
| 57 | .set_video_frame_buffer(I420Buffer::Create(width_, height_)) |
| 58 | .set_rotation(webrtc::kVideoRotation_0) |
| 59 | .set_timestamp_ms(render_time_ms) |
| 60 | .build(); |
Per K | 0fa9088 | 2024-03-13 08:52:41 | [diff] [blame] | 61 | frame.set_rtp_timestamp(input.RtpTimestamp()); |
Per Kjellander | 841c912 | 2018-10-04 16:40:28 | [diff] [blame] | 62 | frame.set_ntp_time_ms(input.ntp_time_ms_); |
| 63 | |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 64 | callback_->Decoded(frame, /*decode_time_ms=*/std::nullopt, |
| 65 | /*qp=*/std::nullopt); |
Per Kjellander | 841c912 | 2018-10-04 16:40:28 | [diff] [blame] | 66 | |
| 67 | return WEBRTC_VIDEO_CODEC_OK; |
| 68 | } |
| 69 | |
| 70 | int32_t FakeVp8Decoder::RegisterDecodeCompleteCallback( |
| 71 | DecodedImageCallback* callback) { |
| 72 | callback_ = callback; |
| 73 | return WEBRTC_VIDEO_CODEC_OK; |
| 74 | } |
| 75 | |
| 76 | int32_t FakeVp8Decoder::Release() { |
| 77 | return WEBRTC_VIDEO_CODEC_OK; |
| 78 | } |
| 79 | |
Erik Språng | c12f625 | 2021-01-13 20:49:59 | [diff] [blame] | 80 | VideoDecoder::DecoderInfo FakeVp8Decoder::GetDecoderInfo() const { |
| 81 | DecoderInfo info; |
| 82 | info.implementation_name = kImplementationName; |
| 83 | info.is_hardware_accelerated = false; |
| 84 | return info; |
| 85 | } |
| 86 | |
Per Kjellander | 841c912 | 2018-10-04 16:40:28 | [diff] [blame] | 87 | const char* FakeVp8Decoder::ImplementationName() const { |
| 88 | return kImplementationName; |
| 89 | } |
| 90 | |
| 91 | } // namespace test |
| 92 | } // namespace webrtc |