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
13 #include <stddef.h>
14
15 #include "absl/types/optional.h"
16 #include "api/scoped_refptr.h"
17 #include "api/video/i420_buffer.h"
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"
22 #include "rtc_base/time_utils.h"
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.
ParseFakeVp8(const unsigned char * data,int * width,int * height)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
FakeVp8Decoder()40 FakeVp8Decoder::FakeVp8Decoder() : callback_(nullptr), width_(0), height_(0) {}
41
InitDecode(const VideoCodec * config,int32_t number_of_cores)42 int32_t FakeVp8Decoder::InitDecode(const VideoCodec* config,
43 int32_t number_of_cores) {
44 return WEBRTC_VIDEO_CODEC_OK;
45 }
46
Decode(const EncodedImage & input,bool missing_frames,int64_t render_time_ms)47 int32_t FakeVp8Decoder::Decode(const EncodedImage& input,
48 bool missing_frames,
49 int64_t render_time_ms) {
50 constexpr size_t kMinPayLoadHeaderLength = 10;
51 if (input.size() < kMinPayLoadHeaderLength) {
52 return WEBRTC_VIDEO_CODEC_ERROR;
53 }
54 ParseFakeVp8(input.data(), &width_, &height_);
55
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();
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
RegisterDecodeCompleteCallback(DecodedImageCallback * callback)71 int32_t FakeVp8Decoder::RegisterDecodeCompleteCallback(
72 DecodedImageCallback* callback) {
73 callback_ = callback;
74 return WEBRTC_VIDEO_CODEC_OK;
75 }
76
Release()77 int32_t FakeVp8Decoder::Release() {
78 return WEBRTC_VIDEO_CODEC_OK;
79 }
80
81 const char* FakeVp8Decoder::kImplementationName = "fake_vp8_decoder";
ImplementationName() const82 const char* FakeVp8Decoder::ImplementationName() const {
83 return kImplementationName;
84 }
85
86 } // namespace test
87 } // namespace webrtc
88