blob: 31d9f3166d72fc62549b2391270a1a751317a1ad [file] [log] [blame]
kjellander@webrtc.org35a17562011-10-06 06:44:54 +00001/*
leozwang@webrtc.orgdb2de5b2012-03-05 19:53:24 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
kjellander@webrtc.org35a17562011-10-06 06:44:54 +00003 *
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 */
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000010
pbos@webrtc.orga4407322013-07-16 12:32:05 +000011#include "webrtc/modules/video_coding/codecs/test/stats.h"
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <assert.h>
14#include <stdio.h>
15
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000016#include <algorithm> // min_element, max_element
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000017
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000018#include "webrtc/base/format_macros.h"
19
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000020namespace webrtc {
21namespace test {
22
pbos@webrtc.org7f7162a2013-07-30 15:18:31 +000023FrameStatistic::FrameStatistic()
24 : encoding_successful(false),
25 decoding_successful(false),
26 encode_return_code(0),
27 decode_return_code(0),
28 encode_time_in_us(0),
29 decode_time_in_us(0),
asaperssonabc00802017-02-23 01:33:04 -080030 qp(-1),
pbos@webrtc.org7f7162a2013-07-30 15:18:31 +000031 frame_number(0),
32 packets_dropped(0),
33 total_packets(0),
34 bit_rate_in_kbps(0),
35 encoded_frame_length_in_bytes(0),
Peter Boström49e196a2015-10-23 15:58:18 +020036 frame_type(kVideoFrameDelta) {}
pbos@webrtc.org7f7162a2013-07-30 15:18:31 +000037
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000038Stats::Stats() {}
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000039
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000040Stats::~Stats() {}
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000041
42bool LessForEncodeTime(const FrameStatistic& s1, const FrameStatistic& s2) {
philipelcce46fc2015-12-21 03:04:49 -080043 return s1.encode_time_in_us < s2.encode_time_in_us;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000044}
45
46bool LessForDecodeTime(const FrameStatistic& s1, const FrameStatistic& s2) {
philipelcce46fc2015-12-21 03:04:49 -080047 return s1.decode_time_in_us < s2.decode_time_in_us;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000048}
49
50bool LessForEncodedSize(const FrameStatistic& s1, const FrameStatistic& s2) {
philipelcce46fc2015-12-21 03:04:49 -080051 return s1.encoded_frame_length_in_bytes < s2.encoded_frame_length_in_bytes;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000052}
53
54bool LessForBitRate(const FrameStatistic& s1, const FrameStatistic& s2) {
philipelcce46fc2015-12-21 03:04:49 -080055 return s1.bit_rate_in_kbps < s2.bit_rate_in_kbps;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000056}
57
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000058FrameStatistic& Stats::NewFrame(int frame_number) {
59 assert(frame_number >= 0);
60 FrameStatistic stat;
61 stat.frame_number = frame_number;
62 stats_.push_back(stat);
63 return stats_[frame_number];
64}
65
66void Stats::PrintSummary() {
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000067 printf("Processing summary:\n");
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000068 if (stats_.size() == 0) {
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000069 printf("No frame statistics have been logged yet.\n");
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000070 return;
71 }
72
73 // Calculate min, max, average and total encoding time
74 int total_encoding_time_in_us = 0;
75 int total_decoding_time_in_us = 0;
asaperssonabc00802017-02-23 01:33:04 -080076 int total_qp = 0;
77 int total_qp_count = 0;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000078 size_t total_encoded_frames_lengths = 0;
79 size_t total_encoded_key_frames_lengths = 0;
80 size_t total_encoded_nonkey_frames_lengths = 0;
81 size_t nbr_keyframes = 0;
82 size_t nbr_nonkeyframes = 0;
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000083
philipelcce46fc2015-12-21 03:04:49 -080084 for (FrameStatisticsIterator it = stats_.begin(); it != stats_.end(); ++it) {
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000085 total_encoding_time_in_us += it->encode_time_in_us;
86 total_decoding_time_in_us += it->decode_time_in_us;
87 total_encoded_frames_lengths += it->encoded_frame_length_in_bytes;
Peter Boström49e196a2015-10-23 15:58:18 +020088 if (it->frame_type == webrtc::kVideoFrameKey) {
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000089 total_encoded_key_frames_lengths += it->encoded_frame_length_in_bytes;
90 nbr_keyframes++;
91 } else {
92 total_encoded_nonkey_frames_lengths += it->encoded_frame_length_in_bytes;
93 nbr_nonkeyframes++;
94 }
asaperssonabc00802017-02-23 01:33:04 -080095 if (it->qp >= 0) {
96 total_qp += it->qp;
97 ++total_qp_count;
98 }
kjellander@webrtc.org35a17562011-10-06 06:44:54 +000099 }
100
101 FrameStatisticsIterator frame;
102
103 // ENCODING
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000104 printf("Encoding time:\n");
philipelcce46fc2015-12-21 03:04:49 -0800105 frame = std::min_element(stats_.begin(), stats_.end(), LessForEncodeTime);
106 printf(" Min : %7d us (frame %d)\n", frame->encode_time_in_us,
107 frame->frame_number);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000108
philipelcce46fc2015-12-21 03:04:49 -0800109 frame = std::max_element(stats_.begin(), stats_.end(), LessForEncodeTime);
110 printf(" Max : %7d us (frame %d)\n", frame->encode_time_in_us,
111 frame->frame_number);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000112
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000113 printf(" Average : %7d us\n",
kjellander@webrtc.org7de6e102011-12-08 08:39:13 +0000114 static_cast<int>(total_encoding_time_in_us / stats_.size()));
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000115
116 // DECODING
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000117 printf("Decoding time:\n");
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000118 // only consider frames that were successfully decoded (packet loss may cause
119 // failures)
120 std::vector<FrameStatistic> decoded_frames;
121 for (std::vector<FrameStatistic>::iterator it = stats_.begin();
philipelcce46fc2015-12-21 03:04:49 -0800122 it != stats_.end(); ++it) {
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000123 if (it->decoding_successful) {
124 decoded_frames.push_back(*it);
125 }
126 }
127 if (decoded_frames.size() == 0) {
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000128 printf("No successfully decoded frames exist in this statistics.\n");
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000129 } else {
philipelcce46fc2015-12-21 03:04:49 -0800130 frame = std::min_element(decoded_frames.begin(), decoded_frames.end(),
131 LessForDecodeTime);
132 printf(" Min : %7d us (frame %d)\n", frame->decode_time_in_us,
133 frame->frame_number);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000134
philipelcce46fc2015-12-21 03:04:49 -0800135 frame = std::max_element(decoded_frames.begin(), decoded_frames.end(),
136 LessForDecodeTime);
137 printf(" Max : %7d us (frame %d)\n", frame->decode_time_in_us,
138 frame->frame_number);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000139
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000140 printf(" Average : %7d us\n",
kjellander@webrtc.org7de6e102011-12-08 08:39:13 +0000141 static_cast<int>(total_decoding_time_in_us / decoded_frames.size()));
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000142 printf(" Failures: %d frames failed to decode.\n",
kjellander@webrtc.org7de6e102011-12-08 08:39:13 +0000143 static_cast<int>(stats_.size() - decoded_frames.size()));
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000144 }
145
146 // SIZE
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000147 printf("Frame sizes:\n");
philipelcce46fc2015-12-21 03:04:49 -0800148 frame = std::min_element(stats_.begin(), stats_.end(), LessForEncodedSize);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000149 printf(" Min : %7" PRIuS " bytes (frame %d)\n",
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000150 frame->encoded_frame_length_in_bytes, frame->frame_number);
151
philipelcce46fc2015-12-21 03:04:49 -0800152 frame = std::max_element(stats_.begin(), stats_.end(), LessForEncodedSize);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000153 printf(" Max : %7" PRIuS " bytes (frame %d)\n",
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000154 frame->encoded_frame_length_in_bytes, frame->frame_number);
155
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000156 printf(" Average : %7" PRIuS " bytes\n",
157 total_encoded_frames_lengths / stats_.size());
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000158 if (nbr_keyframes > 0) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000159 printf(" Average key frame size : %7" PRIuS " bytes (%" PRIuS
160 " keyframes)\n",
161 total_encoded_key_frames_lengths / nbr_keyframes, nbr_keyframes);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000162 }
163 if (nbr_nonkeyframes > 0) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000164 printf(" Average non-key frame size: %7" PRIuS " bytes (%" PRIuS
165 " frames)\n",
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000166 total_encoded_nonkey_frames_lengths / nbr_nonkeyframes,
167 nbr_nonkeyframes);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000168 }
169
170 // BIT RATE
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000171 printf("Bit rates:\n");
philipelcce46fc2015-12-21 03:04:49 -0800172 frame = std::min_element(stats_.begin(), stats_.end(), LessForBitRate);
173 printf(" Min bit rate: %7d kbps (frame %d)\n", frame->bit_rate_in_kbps,
174 frame->frame_number);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000175
philipelcce46fc2015-12-21 03:04:49 -0800176 frame = std::max_element(stats_.begin(), stats_.end(), LessForBitRate);
177 printf(" Max bit rate: %7d kbps (frame %d)\n", frame->bit_rate_in_kbps,
178 frame->frame_number);
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000179
asaperssonabc00802017-02-23 01:33:04 -0800180 int avg_qp = (total_qp_count > 0) ? (total_qp / total_qp_count) : -1;
181 printf("Average QP: %d\n", avg_qp);
182
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000183 printf("\n");
philipelcce46fc2015-12-21 03:04:49 -0800184 printf("Total encoding time : %7d ms.\n", total_encoding_time_in_us / 1000);
185 printf("Total decoding time : %7d ms.\n", total_decoding_time_in_us / 1000);
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000186 printf("Total processing time: %7d ms.\n",
kjellander@webrtc.org35a17562011-10-06 06:44:54 +0000187 (total_encoding_time_in_us + total_decoding_time_in_us) / 1000);
188}
189
190} // namespace test
191} // namespace webrtc