OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/video_coding/codecs/test/stats.h" | 11 #include "webrtc/modules/video_coding/codecs/test/stats.h" |
12 | 12 |
13 #include <stdio.h> | 13 #include <stdio.h> |
14 | 14 |
15 #include <algorithm> // min_element, max_element | 15 #include <algorithm> |
16 | 16 |
17 #include "webrtc/rtc_base/checks.h" | 17 #include "webrtc/rtc_base/checks.h" |
18 #include "webrtc/rtc_base/format_macros.h" | 18 #include "webrtc/rtc_base/format_macros.h" |
19 | 19 |
20 namespace webrtc { | 20 namespace webrtc { |
21 namespace test { | 21 namespace test { |
| 22 |
22 namespace { | 23 namespace { |
| 24 |
23 bool LessForEncodeTime(const FrameStatistic& s1, const FrameStatistic& s2) { | 25 bool LessForEncodeTime(const FrameStatistic& s1, const FrameStatistic& s2) { |
24 return s1.encode_time_in_us < s2.encode_time_in_us; | 26 RTC_DCHECK_NE(s1.frame_number, s2.frame_number); |
| 27 return s1.encode_time_us < s2.encode_time_us; |
25 } | 28 } |
26 | 29 |
27 bool LessForDecodeTime(const FrameStatistic& s1, const FrameStatistic& s2) { | 30 bool LessForDecodeTime(const FrameStatistic& s1, const FrameStatistic& s2) { |
28 return s1.decode_time_in_us < s2.decode_time_in_us; | 31 RTC_DCHECK_NE(s1.frame_number, s2.frame_number); |
| 32 return s1.decode_time_us < s2.decode_time_us; |
29 } | 33 } |
30 | 34 |
31 bool LessForEncodedSize(const FrameStatistic& s1, const FrameStatistic& s2) { | 35 bool LessForEncodedSize(const FrameStatistic& s1, const FrameStatistic& s2) { |
32 return s1.encoded_frame_length_in_bytes < s2.encoded_frame_length_in_bytes; | 36 RTC_DCHECK_NE(s1.frame_number, s2.frame_number); |
| 37 return s1.encoded_frame_size_bytes < s2.encoded_frame_size_bytes; |
33 } | 38 } |
34 | 39 |
35 bool LessForBitRate(const FrameStatistic& s1, const FrameStatistic& s2) { | 40 bool LessForBitRate(const FrameStatistic& s1, const FrameStatistic& s2) { |
36 return s1.bit_rate_in_kbps < s2.bit_rate_in_kbps; | 41 RTC_DCHECK_NE(s1.frame_number, s2.frame_number); |
| 42 return s1.bitrate_kbps < s2.bitrate_kbps; |
37 } | 43 } |
| 44 |
38 } // namespace | 45 } // namespace |
39 | 46 |
40 Stats::Stats() {} | 47 FrameStatistic* Stats::AddFrame() { |
41 | 48 // We don't expect more frames than what can be stored in an int. |
42 Stats::~Stats() {} | 49 stats_.emplace_back(static_cast<int>(stats_.size())); |
43 | 50 return &stats_.back(); |
44 FrameStatistic& Stats::NewFrame(int frame_number) { | |
45 RTC_DCHECK_GE(frame_number, 0); | |
46 FrameStatistic stat; | |
47 stat.frame_number = frame_number; | |
48 stats_.push_back(stat); | |
49 return stats_[frame_number]; | |
50 } | 51 } |
51 | 52 |
52 void Stats::PrintSummary() { | 53 FrameStatistic* Stats::GetFrame(int frame_number) { |
| 54 RTC_CHECK_GE(frame_number, 0); |
| 55 RTC_CHECK_LT(frame_number, stats_.size()); |
| 56 return &stats_[frame_number]; |
| 57 } |
| 58 |
| 59 size_t Stats::size() const { |
| 60 return stats_.size(); |
| 61 } |
| 62 |
| 63 void Stats::PrintSummary() const { |
53 if (stats_.empty()) { | 64 if (stats_.empty()) { |
54 printf("No frame statistics have been logged yet.\n"); | 65 printf("No frame statistics have been logged yet.\n"); |
55 return; | 66 return; |
56 } | 67 } |
57 | 68 |
58 // Calculate min, max, average and total encoding time. | 69 // Calculate min, max, average and total encoding time. |
59 int total_encoding_time_in_us = 0; | 70 int total_encoding_time_us = 0; |
60 int total_decoding_time_in_us = 0; | 71 int total_decoding_time_us = 0; |
61 int total_qp = 0; | 72 size_t total_encoded_frame_size_bytes = 0; |
62 int total_qp_count = 0; | 73 size_t total_encoded_key_frame_size_bytes = 0; |
63 size_t total_encoded_frames_lengths = 0; | 74 size_t total_encoded_delta_frame_size_bytes = 0; |
64 size_t total_encoded_key_frames_lengths = 0; | 75 size_t num_key_frames = 0; |
65 size_t total_encoded_nonkey_frames_lengths = 0; | 76 size_t num_delta_frames = 0; |
66 size_t num_keyframes = 0; | |
67 size_t num_nonkeyframes = 0; | |
68 | 77 |
69 for (const FrameStatistic& stat : stats_) { | 78 for (const FrameStatistic& stat : stats_) { |
70 total_encoding_time_in_us += stat.encode_time_in_us; | 79 total_encoding_time_us += stat.encode_time_us; |
71 total_decoding_time_in_us += stat.decode_time_in_us; | 80 total_decoding_time_us += stat.decode_time_us; |
72 total_encoded_frames_lengths += stat.encoded_frame_length_in_bytes; | 81 total_encoded_frame_size_bytes += stat.encoded_frame_size_bytes; |
73 if (stat.frame_type == webrtc::kVideoFrameKey) { | 82 if (stat.frame_type == webrtc::kVideoFrameKey) { |
74 total_encoded_key_frames_lengths += stat.encoded_frame_length_in_bytes; | 83 total_encoded_key_frame_size_bytes += stat.encoded_frame_size_bytes; |
75 ++num_keyframes; | 84 ++num_key_frames; |
76 } else { | 85 } else { |
77 total_encoded_nonkey_frames_lengths += stat.encoded_frame_length_in_bytes; | 86 total_encoded_delta_frame_size_bytes += stat.encoded_frame_size_bytes; |
78 ++num_nonkeyframes; | 87 ++num_delta_frames; |
79 } | |
80 if (stat.qp >= 0) { | |
81 total_qp += stat.qp; | |
82 ++total_qp_count; | |
83 } | 88 } |
84 } | 89 } |
85 | 90 |
86 // Encoding stats. | 91 // Encoding stats. |
87 printf("Encoding time:\n"); | 92 printf("Encoding time:\n"); |
88 FrameStatisticsIterator frame; | 93 auto frame_it = |
89 frame = std::min_element(stats_.begin(), stats_.end(), LessForEncodeTime); | 94 std::min_element(stats_.begin(), stats_.end(), LessForEncodeTime); |
90 printf(" Min : %7d us (frame %d)\n", frame->encode_time_in_us, | 95 printf(" Min : %7d us (frame %d)\n", frame_it->encode_time_us, |
91 frame->frame_number); | 96 frame_it->frame_number); |
92 frame = std::max_element(stats_.begin(), stats_.end(), LessForEncodeTime); | 97 frame_it = std::max_element(stats_.begin(), stats_.end(), LessForEncodeTime); |
93 printf(" Max : %7d us (frame %d)\n", frame->encode_time_in_us, | 98 printf(" Max : %7d us (frame %d)\n", frame_it->encode_time_us, |
94 frame->frame_number); | 99 frame_it->frame_number); |
95 printf(" Average : %7d us\n", | 100 printf(" Average : %7d us\n", |
96 static_cast<int>(total_encoding_time_in_us / stats_.size())); | 101 static_cast<int>(total_encoding_time_us / stats_.size())); |
97 | 102 |
98 // Decoding stats. | 103 // Decoding stats. |
99 printf("Decoding time:\n"); | 104 printf("Decoding time:\n"); |
100 // Only consider successfully decoded frames (packet loss may cause failures). | 105 // Only consider successfully decoded frames (packet loss may cause failures). |
101 std::vector<FrameStatistic> decoded_frames; | 106 std::vector<FrameStatistic> decoded_frames; |
102 for (const FrameStatistic& stat : stats_) { | 107 for (const FrameStatistic& stat : stats_) { |
103 if (stat.decoding_successful) { | 108 if (stat.decoding_successful) { |
104 decoded_frames.push_back(stat); | 109 decoded_frames.push_back(stat); |
105 } | 110 } |
106 } | 111 } |
107 if (decoded_frames.empty()) { | 112 if (decoded_frames.empty()) { |
108 printf("No successfully decoded frames exist in this statistics.\n"); | 113 printf("No successfully decoded frames exist in this statistics.\n"); |
109 } else { | 114 } else { |
110 frame = std::min_element(decoded_frames.begin(), decoded_frames.end(), | 115 frame_it = std::min_element(decoded_frames.begin(), decoded_frames.end(), |
111 LessForDecodeTime); | 116 LessForDecodeTime); |
112 printf(" Min : %7d us (frame %d)\n", frame->decode_time_in_us, | 117 printf(" Min : %7d us (frame %d)\n", frame_it->decode_time_us, |
113 frame->frame_number); | 118 frame_it->frame_number); |
114 frame = std::max_element(decoded_frames.begin(), decoded_frames.end(), | 119 frame_it = std::max_element(decoded_frames.begin(), decoded_frames.end(), |
115 LessForDecodeTime); | 120 LessForDecodeTime); |
116 printf(" Max : %7d us (frame %d)\n", frame->decode_time_in_us, | 121 printf(" Max : %7d us (frame %d)\n", frame_it->decode_time_us, |
117 frame->frame_number); | 122 frame_it->frame_number); |
118 printf(" Average : %7d us\n", | 123 printf(" Average : %7d us\n", |
119 static_cast<int>(total_decoding_time_in_us / decoded_frames.size())); | 124 static_cast<int>(total_decoding_time_us / decoded_frames.size())); |
120 printf(" Failures: %d frames failed to decode.\n", | 125 printf(" Failures: %d frames failed to decode.\n", |
121 static_cast<int>(stats_.size() - decoded_frames.size())); | 126 static_cast<int>(stats_.size() - decoded_frames.size())); |
122 } | 127 } |
123 | 128 |
124 // Frame size stats. | 129 // Frame size stats. |
125 printf("Frame sizes:\n"); | 130 printf("Frame sizes:\n"); |
126 frame = std::min_element(stats_.begin(), stats_.end(), LessForEncodedSize); | 131 frame_it = std::min_element(stats_.begin(), stats_.end(), LessForEncodedSize); |
127 printf(" Min : %7" PRIuS " bytes (frame %d)\n", | 132 printf(" Min : %7" PRIuS " bytes (frame %d)\n", |
128 frame->encoded_frame_length_in_bytes, frame->frame_number); | 133 frame_it->encoded_frame_size_bytes, frame_it->frame_number); |
129 frame = std::max_element(stats_.begin(), stats_.end(), LessForEncodedSize); | 134 frame_it = std::max_element(stats_.begin(), stats_.end(), LessForEncodedSize); |
130 printf(" Max : %7" PRIuS " bytes (frame %d)\n", | 135 printf(" Max : %7" PRIuS " bytes (frame %d)\n", |
131 frame->encoded_frame_length_in_bytes, frame->frame_number); | 136 frame_it->encoded_frame_size_bytes, frame_it->frame_number); |
132 printf(" Average : %7" PRIuS " bytes\n", | 137 printf(" Average : %7" PRIuS " bytes\n", |
133 total_encoded_frames_lengths / stats_.size()); | 138 total_encoded_frame_size_bytes / stats_.size()); |
134 if (num_keyframes > 0) { | 139 if (num_key_frames > 0) { |
135 printf(" Average key frame size : %7" PRIuS " bytes (%" PRIuS | 140 printf(" Average key frame size : %7" PRIuS " bytes (%" PRIuS |
136 " keyframes)\n", | 141 " keyframes)\n", |
137 total_encoded_key_frames_lengths / num_keyframes, num_keyframes); | 142 total_encoded_key_frame_size_bytes / num_key_frames, num_key_frames); |
138 } | 143 } |
139 if (num_nonkeyframes > 0) { | 144 if (num_delta_frames > 0) { |
140 printf(" Average non-key frame size: %7" PRIuS " bytes (%" PRIuS | 145 printf(" Average non-key frame size: %7" PRIuS " bytes (%" PRIuS |
141 " frames)\n", | 146 " frames)\n", |
142 total_encoded_nonkey_frames_lengths / num_nonkeyframes, | 147 total_encoded_delta_frame_size_bytes / num_delta_frames, |
143 num_nonkeyframes); | 148 num_delta_frames); |
144 } | 149 } |
145 | 150 |
146 // Bitrate stats. | 151 // Bitrate stats. |
147 printf("Bitrates:\n"); | 152 printf("Bitrates:\n"); |
148 frame = std::min_element(stats_.begin(), stats_.end(), LessForBitRate); | 153 frame_it = std::min_element(stats_.begin(), stats_.end(), LessForBitRate); |
149 printf(" Min bitrate: %7d kbps (frame %d)\n", frame->bit_rate_in_kbps, | 154 printf(" Min bitrate: %7d kbps (frame %d)\n", frame_it->bitrate_kbps, |
150 frame->frame_number); | 155 frame_it->frame_number); |
151 frame = std::max_element(stats_.begin(), stats_.end(), LessForBitRate); | 156 frame_it = std::max_element(stats_.begin(), stats_.end(), LessForBitRate); |
152 printf(" Max bitrate: %7d kbps (frame %d)\n", frame->bit_rate_in_kbps, | 157 printf(" Max bitrate: %7d kbps (frame %d)\n", frame_it->bitrate_kbps, |
153 frame->frame_number); | 158 frame_it->frame_number); |
154 | 159 |
155 printf("\n"); | 160 printf("\n"); |
156 printf("Total encoding time : %7d ms.\n", total_encoding_time_in_us / 1000); | 161 printf("Total encoding time : %7d ms.\n", total_encoding_time_us / 1000); |
157 printf("Total decoding time : %7d ms.\n", total_decoding_time_in_us / 1000); | 162 printf("Total decoding time : %7d ms.\n", total_decoding_time_us / 1000); |
158 printf("Total processing time: %7d ms.\n", | 163 printf("Total processing time: %7d ms.\n", |
159 (total_encoding_time_in_us + total_decoding_time_in_us) / 1000); | 164 (total_encoding_time_us + total_decoding_time_us) / 1000); |
160 | 165 |
| 166 // QP stats. |
| 167 int total_qp = 0; |
| 168 int total_qp_count = 0; |
| 169 for (const FrameStatistic& stat : stats_) { |
| 170 if (stat.qp >= 0) { |
| 171 total_qp += stat.qp; |
| 172 ++total_qp_count; |
| 173 } |
| 174 } |
161 int avg_qp = (total_qp_count > 0) ? (total_qp / total_qp_count) : -1; | 175 int avg_qp = (total_qp_count > 0) ? (total_qp / total_qp_count) : -1; |
162 printf("Average QP: %d\n", avg_qp); | 176 printf("Average QP: %d\n", avg_qp); |
163 } | 177 } |
164 | 178 |
165 } // namespace test | 179 } // namespace test |
166 } // namespace webrtc | 180 } // namespace webrtc |
OLD | NEW |