blob: ebfc6650b2a1a7b31dc668e9bf99270fabd52f0c [file] [log] [blame]
charujain0f01c7f2016-12-02 13:00:001/*
2 * Copyright (c) 2016 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 */
Jonas Olssona4d87372019-07-05 17:08:3310#include "rtc_tools/frame_analyzer/reference_less_video_analysis_lib.h"
11
charujain0f01c7f2016-12-02 13:00:0012#include <stdio.h>
Jonas Olssona4d87372019-07-05 17:08:3313
charujain0f01c7f2016-12-02 13:00:0014#include <numeric>
15#include <vector>
16
Yves Gerey3e707812018-11-28 15:47:4917#include "api/video/video_frame_buffer.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "rtc_tools/frame_analyzer/video_quality_analysis.h"
charujain0f01c7f2016-12-02 13:00:0019
20#define STATS_LINE_LENGTH 28
21#define PSNR_FREEZE_THRESHOLD 47
22#define SSIM_FREEZE_THRESHOLD .999
23
24#if defined(_WIN32) || defined(_WIN64)
25#define strtok_r strtok_s
26#endif
27
charujain0f01c7f2016-12-02 13:00:0028bool frozen_frame(std::vector<double> psnr_per_frame,
Yves Gerey665174f2018-06-19 13:03:0529 std::vector<double> ssim_per_frame,
30 size_t frame) {
charujain0f01c7f2016-12-02 13:00:0031 if (psnr_per_frame[frame] >= PSNR_FREEZE_THRESHOLD ||
32 ssim_per_frame[frame] >= SSIM_FREEZE_THRESHOLD)
33 return true;
34 return false;
35}
36
Yves Gerey665174f2018-06-19 13:03:0537std::vector<int> find_frame_clusters(
38 const std::vector<double>& psnr_per_frame,
39 const std::vector<double>& ssim_per_frame) {
charujain0f01c7f2016-12-02 13:00:0040 std::vector<int> identical_frame_clusters;
41 int num_frozen = 0;
42 size_t total_no_of_frames = psnr_per_frame.size();
43
44 for (size_t each_frame = 0; each_frame < total_no_of_frames; each_frame++) {
Yves Gerey665174f2018-06-19 13:03:0545 if (frozen_frame(psnr_per_frame, ssim_per_frame, each_frame)) {
46 num_frozen++;
47 } else if (num_frozen > 0) {
48 // Not frozen anymore.
49 identical_frame_clusters.push_back(num_frozen);
50 num_frozen = 0;
51 }
charujain0f01c7f2016-12-02 13:00:0052 }
53 return identical_frame_clusters;
54}
55
56void print_freezing_metrics(const std::vector<double>& psnr_per_frame,
57 const std::vector<double>& ssim_per_frame) {
58 /*
59 * Prints the different metrics mainly:
60 * 1) Identical frame number, PSNR and SSIM values.
61 * 2) Length of continuous frozen frames.
62 * 3) Max length of continuous freezed frames.
63 * 4) No of unique frames found.
64 * 5) Total different identical frames found.
65 *
66 * Sample output:
Mirko Bonadei92ea95e2017-09-15 04:47:3167 * Printing metrics for file: /src/rtc_tools/test_3.y4m
charujain0f01c7f2016-12-02 13:00:0068 =============================
69 Total number of frames received: 74
70 Total identical frames: 5
71 Number of unique frames: 69
72 Printing Identical Frames:
73 Frame Number: 29 PSNR: 48.000000 SSIM: 0.999618
74 Frame Number: 30 PSNR: 48.000000 SSIM: 0.999898
75 Frame Number: 60 PSNR: 48.000000 SSIM: 0.999564
76 Frame Number: 64 PSNR: 48.000000 SSIM: 0.999651
77 Frame Number: 69 PSNR: 48.000000 SSIM: 0.999684
78 Print identical frame which appears in clusters :
79 2 1 1 1
80 *
81 */
82 size_t total_no_of_frames = psnr_per_frame.size();
Yves Gerey665174f2018-06-19 13:03:0583 std::vector<int> identical_frame_clusters =
84 find_frame_clusters(psnr_per_frame, ssim_per_frame);
charujain0f01c7f2016-12-02 13:00:0085 int total_identical_frames = std::accumulate(
86 identical_frame_clusters.begin(), identical_frame_clusters.end(), 0);
87 size_t unique_frames = total_no_of_frames - total_identical_frames;
88
89 printf("Total number of frames received: %zu\n", total_no_of_frames);
90 printf("Total identical frames: %d\n", total_identical_frames);
91 printf("Number of unique frames: %zu\n", unique_frames);
92
93 printf("Printing Identical Frames: \n");
94 for (size_t frame = 0; frame < total_no_of_frames; frame++) {
95 if (frozen_frame(psnr_per_frame, ssim_per_frame, frame)) {
96 printf(" Frame Number: %zu PSNR: %f SSIM: %f \n", frame,
97 psnr_per_frame[frame], ssim_per_frame[frame]);
98 }
99 }
100
101 printf("Print identical frame which appears in clusters : \n");
102 for (int cluster = 0;
Yves Gerey665174f2018-06-19 13:03:05103 cluster < static_cast<int>(identical_frame_clusters.size()); cluster++)
charujain0f01c7f2016-12-02 13:00:00104 printf("%d ", identical_frame_clusters[cluster]);
105 printf("\n");
106}
107
Magnus Jedvert10e829a2018-09-05 08:46:18108void compute_metrics(const rtc::scoped_refptr<webrtc::test::Video>& video,
charujain0f01c7f2016-12-02 13:00:00109 std::vector<double>* psnr_per_frame,
110 std::vector<double>* ssim_per_frame) {
Magnus Jedvert10e829a2018-09-05 08:46:18111 for (size_t i = 0; i < video->number_of_frames() - 1; ++i) {
112 const rtc::scoped_refptr<webrtc::I420BufferInterface> current_frame =
113 video->GetFrame(i);
114 const rtc::scoped_refptr<webrtc::I420BufferInterface> next_frame =
115 video->GetFrame(i + 1);
116 double result_psnr = webrtc::test::Psnr(current_frame, next_frame);
117 double result_ssim = webrtc::test::Ssim(current_frame, next_frame);
charujain0f01c7f2016-12-02 13:00:00118
119 psnr_per_frame->push_back(result_psnr);
120 ssim_per_frame->push_back(result_ssim);
charujain0f01c7f2016-12-02 13:00:00121 }
charujain0f01c7f2016-12-02 13:00:00122}
123
124int run_analysis(const std::string& video_file) {
125 std::vector<double> psnr_per_frame;
126 std::vector<double> ssim_per_frame;
Magnus Jedvert10e829a2018-09-05 08:46:18127 rtc::scoped_refptr<webrtc::test::Video> video =
128 webrtc::test::OpenY4mFile(video_file);
129 if (video) {
130 compute_metrics(video, &psnr_per_frame, &ssim_per_frame);
charujain0f01c7f2016-12-02 13:00:00131 } else {
132 return -1;
133 }
134 printf("=============================\n");
135 printf("Printing metrics for file: %s\n", video_file.c_str());
136 printf("=============================\n");
137 print_freezing_metrics(psnr_per_frame, ssim_per_frame);
138 return 0;
139}