Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +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_encoder.h" |
| 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | |
| 15 | #include "absl/types/optional.h" |
Erik Språng | 4529fbc | 2018-10-12 10:30:31 +0200 | [diff] [blame] | 16 | #include "api/video_codecs/vp8_temporal_layers.h" |
Elad Alon | cde8ab2 | 2019-03-20 11:56:20 +0100 | [diff] [blame^] | 17 | #include "api/video_codecs/vp8_temporal_layers_factory.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 18 | #include "common_types.h" // NOLINT(build/include) |
| 19 | #include "modules/video_coding/codecs/interface/common_constants.h" |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 20 | #include "modules/video_coding/include/video_codec_interface.h" |
| 21 | #include "modules/video_coding/include/video_error_codes.h" |
| 22 | #include "modules/video_coding/utility/simulcast_utility.h" |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 23 | |
Per Kjellander | 841c912 | 2018-10-04 18:40:28 +0200 | [diff] [blame] | 24 | namespace { |
| 25 | |
| 26 | // Write width and height to the payload the same way as the real encoder does. |
| 27 | // It requires that |payload| has a size of at least kMinPayLoadHeaderLength. |
| 28 | void WriteFakeVp8(unsigned char* payload, |
| 29 | int width, |
| 30 | int height, |
| 31 | bool key_frame) { |
| 32 | payload[0] = key_frame ? 0 : 0x01; |
| 33 | |
| 34 | if (key_frame) { |
| 35 | payload[9] = (height & 0x3F00) >> 8; |
| 36 | payload[8] = (height & 0x00FF); |
| 37 | |
| 38 | payload[7] = (width & 0x3F00) >> 8; |
| 39 | payload[6] = (width & 0x00FF); |
| 40 | } |
| 41 | } |
| 42 | } // namespace |
| 43 | |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 44 | namespace webrtc { |
| 45 | |
| 46 | namespace test { |
| 47 | |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 48 | FakeVP8Encoder::FakeVP8Encoder(Clock* clock) : FakeEncoder(clock) { |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 49 | sequence_checker_.Detach(); |
| 50 | } |
| 51 | |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 52 | int32_t FakeVP8Encoder::InitEncode(const VideoCodec* config, |
| 53 | int32_t number_of_cores, |
| 54 | size_t max_payload_size) { |
| 55 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 56 | auto result = |
| 57 | FakeEncoder::InitEncode(config, number_of_cores, max_payload_size); |
| 58 | if (result != WEBRTC_VIDEO_CODEC_OK) { |
| 59 | return result; |
| 60 | } |
| 61 | |
Elad Alon | cde8ab2 | 2019-03-20 11:56:20 +0100 | [diff] [blame^] | 62 | Vp8TemporalLayersFactory factory; |
| 63 | frame_buffer_controller_ = factory.Create(*config); |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 64 | |
| 65 | return WEBRTC_VIDEO_CODEC_OK; |
| 66 | } |
| 67 | |
| 68 | int32_t FakeVP8Encoder::Release() { |
| 69 | auto result = FakeEncoder::Release(); |
| 70 | sequence_checker_.Detach(); |
| 71 | return result; |
| 72 | } |
| 73 | |
Erik Språng | 59021ba | 2018-10-03 11:05:16 +0200 | [diff] [blame] | 74 | void FakeVP8Encoder::PopulateCodecSpecific(CodecSpecificInfo* codec_specific, |
| 75 | size_t size_bytes, |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 76 | VideoFrameType frame_type, |
Erik Språng | 59021ba | 2018-10-03 11:05:16 +0200 | [diff] [blame] | 77 | int stream_idx, |
| 78 | uint32_t timestamp) { |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 79 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 80 | codec_specific->codecType = kVideoCodecVP8; |
philipel | 9df3353 | 2019-03-04 16:37:50 +0100 | [diff] [blame] | 81 | codec_specific->codecSpecific.VP8.keyIdx = kNoKeyIdx; |
| 82 | codec_specific->codecSpecific.VP8.nonReference = false; |
Elad Alon | cde8ab2 | 2019-03-20 11:56:20 +0100 | [diff] [blame^] | 83 | frame_buffer_controller_->OnEncodeDone(stream_idx, timestamp, size_bytes, |
| 84 | frame_type == kVideoFrameKey, -1, |
| 85 | codec_specific); |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 88 | std::unique_ptr<RTPFragmentationHeader> FakeVP8Encoder::EncodeHook( |
| 89 | EncodedImage* encoded_image, |
| 90 | CodecSpecificInfo* codec_specific) { |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 91 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 92 | uint8_t stream_idx = encoded_image->SpatialIndex().value_or(0); |
Elad Alon | cde8ab2 | 2019-03-20 11:56:20 +0100 | [diff] [blame^] | 93 | frame_buffer_controller_->UpdateLayerConfig(stream_idx, |
| 94 | encoded_image->Timestamp()); |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 95 | PopulateCodecSpecific(codec_specific, encoded_image->size(), |
| 96 | encoded_image->_frameType, stream_idx, |
| 97 | encoded_image->Timestamp()); |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 98 | |
Per Kjellander | 841c912 | 2018-10-04 18:40:28 +0200 | [diff] [blame] | 99 | // Write width and height to the payload the same way as the real encoder |
| 100 | // does. |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 101 | WriteFakeVp8(encoded_image->data(), encoded_image->_encodedWidth, |
| 102 | encoded_image->_encodedHeight, |
| 103 | encoded_image->_frameType == kVideoFrameKey); |
| 104 | return nullptr; |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Erik Språng | 86336a5 | 2018-11-15 15:38:05 +0100 | [diff] [blame] | 107 | VideoEncoder::EncoderInfo FakeVP8Encoder::GetEncoderInfo() const { |
| 108 | EncoderInfo info; |
| 109 | info.implementation_name = "FakeVp8Encoder"; |
| 110 | return info; |
| 111 | } |
| 112 | |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 113 | } // namespace test |
| 114 | } // namespace webrtc |