blob: faaa554259334da4923b44d62951cd51552d0519 [file] [log] [blame]
Per Kjellander841c9122018-10-04 18:40:28 +02001/*
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 Gerey3e707812018-11-28 16:47:49 +010013#include <stddef.h>
14
15#include "absl/types/optional.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010016#include "api/scoped_refptr.h"
Per Kjellander841c9122018-10-04 18:40:28 +020017#include "api/video/i420_buffer.h"
Yves Gerey3e707812018-11-28 16:47:49 +010018#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 Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/time_utils.h"
Per Kjellander841c9122018-10-04 18:40:28 +020023
24namespace webrtc {
25namespace test {
26
27namespace {
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.
31void 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
40FakeVp8Decoder::FakeVp8Decoder() : callback_(nullptr), width_(0), height_(0) {}
41
42int32_t FakeVp8Decoder::InitDecode(const VideoCodec* config,
43 int32_t number_of_cores) {
44 return WEBRTC_VIDEO_CODEC_OK;
45}
46
47int32_t FakeVp8Decoder::Decode(const EncodedImage& input,
48 bool missing_frames,
Per Kjellander841c9122018-10-04 18:40:28 +020049 int64_t render_time_ms) {
50 constexpr size_t kMinPayLoadHeaderLength = 10;
Niels Möller77536a22019-01-15 08:50:01 +010051 if (input.size() < kMinPayLoadHeaderLength) {
Per Kjellander841c9122018-10-04 18:40:28 +020052 return WEBRTC_VIDEO_CODEC_ERROR;
53 }
Niels Möller24871e42019-01-17 11:31:13 +010054 ParseFakeVp8(input.data(), &width_, &height_);
Per Kjellander841c9122018-10-04 18:40:28 +020055
Artem Titov1ebfb6a2019-01-03 23:49:37 +010056 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 Kjellander841c9122018-10-04 18:40:28 +020062 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
71int32_t FakeVp8Decoder::RegisterDecodeCompleteCallback(
72 DecodedImageCallback* callback) {
73 callback_ = callback;
74 return WEBRTC_VIDEO_CODEC_OK;
75}
76
77int32_t FakeVp8Decoder::Release() {
78 return WEBRTC_VIDEO_CODEC_OK;
79}
80
81const char* FakeVp8Decoder::kImplementationName = "fake_vp8_decoder";
82const char* FakeVp8Decoder::ImplementationName() const {
83 return kImplementationName;
84}
85
86} // namespace test
87} // namespace webrtc