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