summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/api/test/video_codec_stats.h
blob: 80f82878485f292083200448392516804e5efc62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 *  Copyright (c) 2023 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef API_TEST_VIDEO_CODEC_STATS_H_
#define API_TEST_VIDEO_CODEC_STATS_H_

#include <map>
#include <string>
#include <vector>

#include "absl/types/optional.h"
#include "api/numerics/samples_stats_counter.h"
#include "api/test/metrics/metric.h"
#include "api/test/metrics/metrics_logger.h"
#include "api/units/data_rate.h"
#include "api/units/data_size.h"
#include "api/units/frequency.h"

namespace webrtc {
namespace test {

// Interface for encoded and/or decoded video frame and stream statistics.
class VideoCodecStats {
 public:
  // Filter for slicing frames.
  struct Filter {
    absl::optional<int> first_frame;
    absl::optional<int> last_frame;
    absl::optional<int> spatial_idx;
    absl::optional<int> temporal_idx;
  };

  struct Frame {
    int frame_num = 0;
    uint32_t timestamp_rtp = 0;

    int spatial_idx = 0;
    int temporal_idx = 0;

    int width = 0;
    int height = 0;
    DataSize frame_size = DataSize::Zero();
    bool keyframe = false;
    absl::optional<int> qp;
    absl::optional<int> base_spatial_idx;

    Timestamp encode_start = Timestamp::Zero();
    TimeDelta encode_time = TimeDelta::Zero();
    Timestamp decode_start = Timestamp::Zero();
    TimeDelta decode_time = TimeDelta::Zero();

    struct Psnr {
      double y = 0.0;
      double u = 0.0;
      double v = 0.0;
    };
    absl::optional<Psnr> psnr;

    absl::optional<DataRate> target_bitrate;
    absl::optional<Frequency> target_framerate;

    bool encoded = false;
    bool decoded = false;
  };

  struct Stream {
    SamplesStatsCounter width;
    SamplesStatsCounter height;
    SamplesStatsCounter frame_size_bytes;
    SamplesStatsCounter keyframe;
    SamplesStatsCounter qp;

    SamplesStatsCounter encode_time_ms;
    SamplesStatsCounter decode_time_ms;

    SamplesStatsCounter target_bitrate_kbps;
    SamplesStatsCounter target_framerate_fps;

    SamplesStatsCounter encoded_bitrate_kbps;
    SamplesStatsCounter encoded_framerate_fps;

    SamplesStatsCounter bitrate_mismatch_pct;
    SamplesStatsCounter framerate_mismatch_pct;

    SamplesStatsCounter transmission_time_ms;

    struct Psnr {
      SamplesStatsCounter y;
      SamplesStatsCounter u;
      SamplesStatsCounter v;
    } psnr;

    // Logs `Stream` metrics to provided `MetricsLogger`.
    void LogMetrics(MetricsLogger* logger,
                    std::string test_case_name,
                    std::map<std::string, std::string> metadata = {}) const;
  };

  virtual ~VideoCodecStats() = default;

  // Returns frames from interval, spatial and temporal layer specified by given
  // `filter`.
  virtual std::vector<Frame> Slice(
      absl::optional<Filter> filter = absl::nullopt) const = 0;

  // Returns video statistics aggregated for given `frames`.
  virtual Stream Aggregate(const std::vector<Frame>& frames) const = 0;
};

}  // namespace test
}  // namespace webrtc

#endif  // API_TEST_VIDEO_CODEC_STATS_H_