kjellander@webrtc.org | 35a1756 | 2011-10-06 06:44:54 +0000 | [diff] [blame^] | 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 | #include "stats.h" |
| 11 | |
| 12 | #include <algorithm> // min_element, max_element |
| 13 | #include <cassert> |
| 14 | #include <cstdio> |
| 15 | |
| 16 | #include "util.h" |
| 17 | |
| 18 | namespace webrtc { |
| 19 | namespace test { |
| 20 | |
| 21 | Stats::Stats() { |
| 22 | } |
| 23 | |
| 24 | Stats::~Stats() { |
| 25 | } |
| 26 | |
| 27 | bool LessForEncodeTime(const FrameStatistic& s1, const FrameStatistic& s2) { |
| 28 | return s1.encode_time_in_us < s2.encode_time_in_us; |
| 29 | } |
| 30 | |
| 31 | bool LessForDecodeTime(const FrameStatistic& s1, const FrameStatistic& s2) { |
| 32 | return s1.decode_time_in_us < s2.decode_time_in_us; |
| 33 | } |
| 34 | |
| 35 | bool LessForEncodedSize(const FrameStatistic& s1, const FrameStatistic& s2) { |
| 36 | return s1.encoded_frame_length_in_bytes < s2.encoded_frame_length_in_bytes; |
| 37 | } |
| 38 | |
| 39 | bool LessForBitRate(const FrameStatistic& s1, const FrameStatistic& s2) { |
| 40 | return s1.bit_rate_in_kbps < s2.bit_rate_in_kbps; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | FrameStatistic& Stats::NewFrame(int frame_number) { |
| 45 | assert(frame_number >= 0); |
| 46 | FrameStatistic stat; |
| 47 | stat.frame_number = frame_number; |
| 48 | stats_.push_back(stat); |
| 49 | return stats_[frame_number]; |
| 50 | } |
| 51 | |
| 52 | void Stats::PrintSummary() { |
| 53 | log("Processing summary:\n"); |
| 54 | if (stats_.size() == 0) { |
| 55 | log("No frame statistics have been logged yet.\n"); |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | // Calculate min, max, average and total encoding time |
| 60 | int total_encoding_time_in_us = 0; |
| 61 | int total_decoding_time_in_us = 0; |
| 62 | int total_encoded_frames_lengths = 0; |
| 63 | int total_encoded_key_frames_lengths = 0; |
| 64 | int total_encoded_nonkey_frames_lengths = 0; |
| 65 | int nbr_keyframes = 0; |
| 66 | int nbr_nonkeyframes = 0; |
| 67 | |
| 68 | for (FrameStatisticsIterator it = stats_.begin(); |
| 69 | it != stats_.end(); ++it) { |
| 70 | total_encoding_time_in_us += it->encode_time_in_us; |
| 71 | total_decoding_time_in_us += it->decode_time_in_us; |
| 72 | total_encoded_frames_lengths += it->encoded_frame_length_in_bytes; |
| 73 | if (it->frame_type == webrtc::kKeyFrame) { |
| 74 | total_encoded_key_frames_lengths += it->encoded_frame_length_in_bytes; |
| 75 | nbr_keyframes++; |
| 76 | } else { |
| 77 | total_encoded_nonkey_frames_lengths += it->encoded_frame_length_in_bytes; |
| 78 | nbr_nonkeyframes++; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | FrameStatisticsIterator frame; |
| 83 | |
| 84 | // ENCODING |
| 85 | log("Encoding time:\n"); |
| 86 | frame = min_element(stats_.begin(), |
| 87 | stats_.end(), LessForEncodeTime); |
| 88 | log(" Min : %7d us (frame %d)\n", |
| 89 | frame->encode_time_in_us, frame->frame_number); |
| 90 | |
| 91 | frame = max_element(stats_.begin(), |
| 92 | stats_.end(), LessForEncodeTime); |
| 93 | log(" Max : %7d us (frame %d)\n", |
| 94 | frame->encode_time_in_us, frame->frame_number); |
| 95 | |
| 96 | log(" Average : %7d us\n", |
| 97 | total_encoding_time_in_us / stats_.size()); |
| 98 | |
| 99 | // DECODING |
| 100 | log("Decoding time:\n"); |
| 101 | // only consider frames that were successfully decoded (packet loss may cause |
| 102 | // failures) |
| 103 | std::vector<FrameStatistic> decoded_frames; |
| 104 | for (std::vector<FrameStatistic>::iterator it = stats_.begin(); |
| 105 | it != stats_.end(); ++it) { |
| 106 | if (it->decoding_successful) { |
| 107 | decoded_frames.push_back(*it); |
| 108 | } |
| 109 | } |
| 110 | if (decoded_frames.size() == 0) { |
| 111 | printf("No successfully decoded frames exist in this statistics."); |
| 112 | } else { |
| 113 | frame = min_element(decoded_frames.begin(), |
| 114 | decoded_frames.end(), LessForDecodeTime); |
| 115 | log(" Min : %7d us (frame %d)\n", |
| 116 | frame->decode_time_in_us, frame->frame_number); |
| 117 | |
| 118 | frame = max_element(decoded_frames.begin(), |
| 119 | decoded_frames.end(), LessForDecodeTime); |
| 120 | log(" Max : %7d us (frame %d)\n", |
| 121 | frame->decode_time_in_us, frame->frame_number); |
| 122 | |
| 123 | log(" Average : %7d us\n", |
| 124 | total_decoding_time_in_us / decoded_frames.size()); |
| 125 | log(" Failures: %d frames failed to decode.\n", |
| 126 | (stats_.size() - decoded_frames.size())); |
| 127 | } |
| 128 | |
| 129 | // SIZE |
| 130 | log("Frame sizes:\n"); |
| 131 | frame = min_element(stats_.begin(), |
| 132 | stats_.end(), LessForEncodedSize); |
| 133 | log(" Min : %7d bytes (frame %d)\n", |
| 134 | frame->encoded_frame_length_in_bytes, frame->frame_number); |
| 135 | |
| 136 | frame = max_element(stats_.begin(), |
| 137 | stats_.end(), LessForEncodedSize); |
| 138 | log(" Max : %7d bytes (frame %d)\n", |
| 139 | frame->encoded_frame_length_in_bytes, frame->frame_number); |
| 140 | |
| 141 | log(" Average : %7d bytes\n", |
| 142 | total_encoded_frames_lengths / stats_.size()); |
| 143 | if (nbr_keyframes > 0) { |
| 144 | log(" Average key frame size : %7d bytes (%d keyframes)\n", |
| 145 | total_encoded_key_frames_lengths / nbr_keyframes, |
| 146 | nbr_keyframes); |
| 147 | } |
| 148 | if (nbr_nonkeyframes > 0) { |
| 149 | log(" Average non-key frame size: %7d bytes (%d frames)\n", |
| 150 | total_encoded_nonkey_frames_lengths / nbr_nonkeyframes, |
| 151 | nbr_nonkeyframes); |
| 152 | } |
| 153 | |
| 154 | // BIT RATE |
| 155 | log("Bit rates:\n"); |
| 156 | frame = min_element(stats_.begin(), |
| 157 | stats_.end(), LessForBitRate); |
| 158 | log(" Min bit rate: %7d kbps (frame %d)\n", |
| 159 | frame->bit_rate_in_kbps, frame->frame_number); |
| 160 | |
| 161 | frame = max_element(stats_.begin(), |
| 162 | stats_.end(), LessForBitRate); |
| 163 | log(" Max bit rate: %7d kbps (frame %d)\n", |
| 164 | frame->bit_rate_in_kbps, frame->frame_number); |
| 165 | |
| 166 | log("\n"); |
| 167 | log("Total encoding time : %7d ms.\n", |
| 168 | total_encoding_time_in_us / 1000); |
| 169 | log("Total decoding time : %7d ms.\n", |
| 170 | total_decoding_time_in_us / 1000); |
| 171 | log("Total processing time: %7d ms.\n", |
| 172 | (total_encoding_time_in_us + total_decoding_time_in_us) / 1000); |
| 173 | } |
| 174 | |
| 175 | } // namespace test |
| 176 | } // namespace webrtc |