blob: 865bca7983607a53ddc87aca65ac1daa9bd9f525 [file] [log] [blame]
pbos@webrtc.org0181b5f2013-09-09 08:26:30 +00001/*
2 * Copyright (c) 2013 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "test/fake_decoder.h"
pbos@webrtc.org0181b5f2013-09-09 08:26:30 +000012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <string.h>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "api/video/i420_buffer.h"
Yves Gerey3e707812018-11-28 16:47:49 +010016#include "api/video/video_frame.h"
17#include "api/video/video_frame_buffer.h"
18#include "api/video/video_rotation.h"
19#include "modules/video_coding/include/video_error_codes.h"
20#include "rtc_base/checks.h"
21#include "rtc_base/scoped_ref_ptr.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/time_utils.h"
pbos@webrtc.org0181b5f2013-09-09 08:26:30 +000023
24namespace webrtc {
25namespace test {
26
Ilya Nikolaevskiyb0588e62018-08-27 14:12:27 +020027namespace {
28const int kDefaultWidth = 320;
29const int kDefaultHeight = 180;
30} // namespace
31
philipel0e075722018-04-05 13:04:42 +020032FakeDecoder::FakeDecoder()
Ilya Nikolaevskiyb0588e62018-08-27 14:12:27 +020033 : callback_(NULL), width_(kDefaultWidth), height_(kDefaultHeight) {}
pbos@webrtc.org0181b5f2013-09-09 08:26:30 +000034
35int32_t FakeDecoder::InitDecode(const VideoCodec* config,
36 int32_t number_of_cores) {
pbos@webrtc.org0181b5f2013-09-09 08:26:30 +000037 return WEBRTC_VIDEO_CODEC_OK;
38}
39
40int32_t FakeDecoder::Decode(const EncodedImage& input,
41 bool missing_frames,
pbos@webrtc.org0181b5f2013-09-09 08:26:30 +000042 const CodecSpecificInfo* codec_specific_info,
43 int64_t render_time_ms) {
philipel0e075722018-04-05 13:04:42 +020044 if (input._encodedWidth > 0 && input._encodedHeight > 0) {
45 width_ = input._encodedWidth;
46 height_ = input._encodedHeight;
47 }
48
Artem Titov1ebfb6a2019-01-03 23:49:37 +010049 VideoFrame frame =
50 VideoFrame::Builder()
51 .set_video_frame_buffer(I420Buffer::Create(width_, height_))
52 .set_rotation(webrtc::kVideoRotation_0)
53 .set_timestamp_ms(render_time_ms)
54 .build();
Niels Möller23775882018-08-16 10:24:12 +020055 frame.set_timestamp(input.Timestamp());
nissef122a852016-10-04 23:27:30 -070056 frame.set_ntp_time_ms(input.ntp_time_ms_);
pbos@webrtc.org0181b5f2013-09-09 08:26:30 +000057
nissef122a852016-10-04 23:27:30 -070058 callback_->Decoded(frame);
pbos@webrtc.org0181b5f2013-09-09 08:26:30 +000059
60 return WEBRTC_VIDEO_CODEC_OK;
61}
62
63int32_t FakeDecoder::RegisterDecodeCompleteCallback(
64 DecodedImageCallback* callback) {
65 callback_ = callback;
66 return WEBRTC_VIDEO_CODEC_OK;
67}
68
69int32_t FakeDecoder::Release() {
70 return WEBRTC_VIDEO_CODEC_OK;
71}
Peter Boströmb7d9a972015-12-18 16:01:11 +010072
Peter Boströmb7d9a972015-12-18 16:01:11 +010073const char* FakeDecoder::kImplementationName = "fake_decoder";
74const char* FakeDecoder::ImplementationName() const {
75 return kImplementationName;
76}
77
stefan@webrtc.org79c33592014-08-06 09:24:53 +000078int32_t FakeH264Decoder::Decode(const EncodedImage& input,
79 bool missing_frames,
stefan@webrtc.org79c33592014-08-06 09:24:53 +000080 const CodecSpecificInfo* codec_specific_info,
81 int64_t render_time_ms) {
82 uint8_t value = 0;
Niels Möller77536a22019-01-15 08:50:01 +010083 for (size_t i = 0; i < input.size(); ++i) {
stefan@webrtc.org79c33592014-08-06 09:24:53 +000084 uint8_t kStartCode[] = {0, 0, 0, 1};
Niels Möller77536a22019-01-15 08:50:01 +010085 if (i < input.size() - sizeof(kStartCode) &&
Niels Möller24871e42019-01-17 11:31:13 +010086 !memcmp(&input.data()[i], kStartCode, sizeof(kStartCode))) {
stefan@webrtc.org79c33592014-08-06 09:24:53 +000087 i += sizeof(kStartCode) + 1; // Skip start code and NAL header.
88 }
Niels Möller24871e42019-01-17 11:31:13 +010089 if (input.data()[i] != value) {
90 RTC_CHECK_EQ(value, input.data()[i])
stefan@webrtc.org79c33592014-08-06 09:24:53 +000091 << "Bitstream mismatch between sender and receiver.";
92 return -1;
93 }
94 ++value;
95 }
Yves Gerey665174f2018-06-19 15:03:05 +020096 return FakeDecoder::Decode(input, missing_frames, codec_specific_info,
stefan@webrtc.org79c33592014-08-06 09:24:53 +000097 render_time_ms);
98}
99
pbos@webrtc.org0181b5f2013-09-09 08:26:30 +0000100} // namespace test
101} // namespace webrtc