| 1 | ; /* |
| 2 | * Copyright (c) 2011 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 | #ifndef WEBRTC_MODULES_VIDEO_CODING_CODECS_TEST_FRAWEWORK_BENCHMARK_H_ |
| 12 | #define WEBRTC_MODULES_VIDEO_CODING_CODECS_TEST_FRAWEWORK_BENCHMARK_H_ |
| 13 | |
| 14 | #include <cstdlib> |
| 15 | #include <fstream> |
| 16 | #include <list> |
| 17 | #include <queue> |
| 18 | #include <string> |
| 19 | |
| 20 | #include "cpu/cpu_linux.h" |
| 21 | #include "api/environment/environment_factory.h" |
| 22 | #include "modules/include/module_common_types.h" |
| 23 | #include "modules/video_coding/include/video_codec_interface.h" |
| 24 | #include "rtc_base/synchronization/mutex.h" |
| 25 | #include "system_wrappers/include/clock.h" |
| 26 | |
| 27 | |
| 28 | class VideoSource; |
| 29 | class Benchmark; |
| 30 | |
| 31 | // feedback signal to encoder |
| 32 | struct fbSignal { |
| 33 | fbSignal(int d, uint8_t pid) : delay(d), id(pid) {}; |
| 34 | int delay; |
| 35 | uint8_t id; |
| 36 | }; |
| 37 | |
| 38 | class FrameQueueTuple { |
| 39 | public: |
| 40 | FrameQueueTuple(webrtc::VideoFrame* frame, |
| 41 | const webrtc::CodecSpecificInfo* codecSpecificInfo = NULL) |
| 42 | : _frame(frame), _codecSpecificInfo(codecSpecificInfo) {}; |
| 43 | ~FrameQueueTuple(); |
| 44 | webrtc::VideoFrame* _frame; |
| 45 | const webrtc::CodecSpecificInfo* _codecSpecificInfo; |
| 46 | }; |
| 47 | |
| 48 | class FrameQueue { |
| 49 | public: |
| 50 | FrameQueue() {} |
| 51 | |
| 52 | ~FrameQueue() {} |
| 53 | |
| 54 | void PushFrame(webrtc::VideoFrame* frame, |
| 55 | webrtc::CodecSpecificInfo* codecSpecificInfo = NULL); |
| 56 | FrameQueueTuple* PopFrame(); |
| 57 | bool Empty(); |
| 58 | |
| 59 | private: |
| 60 | webrtc::Mutex _queueRWLock; |
| 61 | std::queue<FrameQueueTuple*> _frameBufferQueue; |
| 62 | }; |
| 63 | |
| 64 | class VideoEncodeCompleteCallback : public webrtc::EncodedImageCallback { |
| 65 | public: |
| 66 | VideoEncodeCompleteCallback(FILE* encodedFile, |
| 67 | FrameQueue* frameQueue, |
| 68 | Benchmark& test) |
| 69 | : _encodedFile(encodedFile), |
| 70 | _frameQueue(frameQueue), |
| 71 | _test(test), |
| 72 | _encodedBytes(0) {} |
| 73 | |
| 74 | webrtc::EncodedImageCallback::Result OnEncodedImage( |
| 75 | const webrtc::EncodedImage& encoded_image, |
| 76 | const webrtc::CodecSpecificInfo* codec_specific_info) override; |
| 77 | |
| 78 | uint32_t EncodedBytes(); |
| 79 | |
| 80 | private: |
| 81 | FILE* _encodedFile; |
| 82 | FrameQueue* _frameQueue; |
| 83 | Benchmark& _test; |
| 84 | uint32_t _encodedBytes; |
| 85 | }; |
| 86 | |
| 87 | class Benchmark { |
| 88 | public: |
| 89 | friend class VideoEncodeCompleteCallback; |
| 90 | |
| 91 | public: |
| 92 | Benchmark(); |
| 93 | virtual void Perform(); |
| 94 | virtual bool IsSupported() = 0; |
| 95 | |
| 96 | protected: |
| 97 | Benchmark(std::string name, std::string description); |
| 98 | Benchmark(std::string name, |
| 99 | std::string description, |
| 100 | std::string resultsFileName, |
| 101 | std::string codecName); |
| 102 | virtual webrtc::VideoEncoder* GetNewEncoder(webrtc::Environment &env) = 0; |
| 103 | virtual void PerformNormalTest(); |
| 104 | virtual void CodecSpecific_InitBitrate(); |
| 105 | static const char* GetMagicStr() { return "#!benchmark1.0"; } |
| 106 | |
| 107 | double ActualBitRate(int nFrames) { |
| 108 | return 8.0 * _sumEncBytes / (nFrames / _inst.maxFramerate); |
| 109 | } |
| 110 | |
| 111 | webrtc::CodecSpecificInfo* CopyCodecSpecificInfo( |
| 112 | const webrtc::CodecSpecificInfo* codecSpecificInfo) const; |
| 113 | |
| 114 | bool Encode(); |
| 115 | |
| 116 | void Setup(); |
| 117 | |
| 118 | void Teardown(); |
| 119 | |
| 120 | void CodecSettings(int width, |
| 121 | int height, |
| 122 | uint32_t frameRate /*=30*/, |
| 123 | uint32_t bitRate /*=0*/) { |
| 124 | if (bitRate > 0) { |
| 125 | _bitRate = bitRate; |
| 126 | } else if (_bitRate == 0) { |
| 127 | _bitRate = 600; |
| 128 | } |
| 129 | _inst.codecType = webrtc::kVideoCodecH264; |
| 130 | _inst.maxFramerate = (unsigned char)frameRate; |
| 131 | _inst.minBitrate = (unsigned char)frameRate; |
| 132 | _inst.startBitrate = (int)_bitRate; |
| 133 | _inst.maxBitrate = 8000; |
| 134 | _inst.width = width; |
| 135 | _inst.height = height; |
| 136 | _inst.numberOfSimulcastStreams = 1; |
| 137 | _inst.simulcastStream[0].width = width; |
| 138 | _inst.simulcastStream[0].height = height; |
| 139 | _inst.simulcastStream[0].maxBitrate = 8000; |
| 140 | _inst.simulcastStream[0].minBitrate = _bitRate; |
| 141 | _inst.simulcastStream[0].targetBitrate = _bitRate; |
| 142 | _inst.simulcastStream[0].maxFramerate = frameRate; |
| 143 | _inst.simulcastStream[0].active = true; |
| 144 | _inst.SetScalabilityMode(webrtc::ScalabilityMode::kL1T1); |
| 145 | _inst.mode = webrtc::VideoCodecMode::kRealtimeVideo; |
| 146 | _inst.qpMax = 56; |
| 147 | _inst.SetFrameDropEnabled(true); |
| 148 | } |
| 149 | |
| 150 | double tGetTime() { |
| 151 | // return time in sec |
| 152 | return ((double)(webrtc::Clock::GetRealTimeClock()->TimeInMilliseconds()) / |
| 153 | 1000); |
| 154 | } |
| 155 | |
| 156 | virtual webrtc::CodecSpecificInfo* CreateEncoderSpecificInfo() const { |
| 157 | return NULL; |
| 158 | }; |
| 159 | |
| 160 | void UpdateEncodedBytes(int encodedBytes) { _sumEncBytes += encodedBytes; } |
| 161 | |
| 162 | const VideoSource* _target; |
| 163 | std::string _resultsFileName; |
| 164 | std::ofstream _results; |
| 165 | std::string _name; |
| 166 | std::string _description; |
| 167 | std::string _codecName; |
| 168 | std::string _inname; |
| 169 | std::string _outname; |
| 170 | webrtc::VideoEncoder* _encoder; |
| 171 | //webrtc::VideoDecoder* _decoder; |
| 172 | uint32_t _bitRate; |
| 173 | bool _appendNext = false; |
| 174 | int _framecnt; |
| 175 | int _encFrameCnt; |
| 176 | double _totalEncodeTime; |
| 177 | double _totalDecodeTime; |
| 178 | double _decodeCompleteTime; |
| 179 | double _encodeCompleteTime; |
| 180 | double _totalEncodePipeTime; |
| 181 | double _totalDecodePipeTime; |
| 182 | webrtc::VideoCodec _inst; |
| 183 | int _sumEncBytes; |
| 184 | |
| 185 | unsigned int _lengthSourceFrame = 0; |
| 186 | unsigned char* _sourceBuffer = nullptr; |
| 187 | |
| 188 | FILE* _encodedFile = nullptr; |
| 189 | unsigned int _lengthEncFrame = 0; |
| 190 | FrameQueueTuple* _frameToDecode = nullptr; |
| 191 | |
| 192 | FILE* _sourceFile = nullptr; |
| 193 | FILE* _decodedFile = nullptr; |
| 194 | |
| 195 | bool _hasReceivedPLI = false; |
| 196 | bool _waitForKey = false; |
| 197 | std::map<uint32_t, double> _encodeTimes; |
| 198 | std::map<uint32_t, double> _decodeTimes; |
| 199 | |
| 200 | bool _missingFrames = false; |
| 201 | std::list<fbSignal> _signalSLI; |
| 202 | int _rttFrames = 0; |
| 203 | mutable bool _hasReceivedSLI = false; |
| 204 | mutable bool _hasReceivedRPSI = false; |
| 205 | uint8_t _pictureIdSLI = 0; |
| 206 | uint16_t _pictureIdRPSI = 0; |
| 207 | uint64_t _lastDecRefPictureId = 0; |
| 208 | uint64_t _lastDecPictureId = 0; |
| 209 | std::list<fbSignal> _signalPLI; |
| 210 | webrtc::CpuWrapper* _cpu; |
| 211 | webrtc::Environment _env; |
| 212 | }; |
| 213 | |
| 214 | #endif // WEBRTC_MODULES_VIDEO_CODING_CODECS_TEST_FRAWEWORK_BENCHMARK_H_ |