• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
10 #include "rtc_tools/frame_analyzer/reference_less_video_analysis_lib.h"
11 
12 #include <stdio.h>
13 
14 #include <numeric>
15 #include <vector>
16 
17 #include "api/video/video_frame_buffer.h"
18 #include "rtc_tools/frame_analyzer/video_quality_analysis.h"
19 
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 
frozen_frame(std::vector<double> psnr_per_frame,std::vector<double> ssim_per_frame,size_t frame)28 bool frozen_frame(std::vector<double> psnr_per_frame,
29                   std::vector<double> ssim_per_frame,
30                   size_t frame) {
31   if (psnr_per_frame[frame] >= PSNR_FREEZE_THRESHOLD ||
32       ssim_per_frame[frame] >= SSIM_FREEZE_THRESHOLD)
33     return true;
34   return false;
35 }
36 
find_frame_clusters(const std::vector<double> & psnr_per_frame,const std::vector<double> & ssim_per_frame)37 std::vector<int> find_frame_clusters(
38     const std::vector<double>& psnr_per_frame,
39     const std::vector<double>& ssim_per_frame) {
40   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++) {
45     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     }
52   }
53   return identical_frame_clusters;
54 }
55 
print_freezing_metrics(const std::vector<double> & psnr_per_frame,const std::vector<double> & ssim_per_frame)56 void 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:
67    *  Printing metrics for file: /src/rtc_tools/test_3.y4m
68       =============================
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();
83   std::vector<int> identical_frame_clusters =
84       find_frame_clusters(psnr_per_frame, ssim_per_frame);
85   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;
103        cluster < static_cast<int>(identical_frame_clusters.size()); cluster++)
104     printf("%d ", identical_frame_clusters[cluster]);
105   printf("\n");
106 }
107 
compute_metrics(const rtc::scoped_refptr<webrtc::test::Video> & video,std::vector<double> * psnr_per_frame,std::vector<double> * ssim_per_frame)108 void compute_metrics(const rtc::scoped_refptr<webrtc::test::Video>& video,
109                      std::vector<double>* psnr_per_frame,
110                      std::vector<double>* ssim_per_frame) {
111   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);
118 
119     psnr_per_frame->push_back(result_psnr);
120     ssim_per_frame->push_back(result_ssim);
121   }
122 }
123 
run_analysis(const std::string & video_file)124 int run_analysis(const std::string& video_file) {
125   std::vector<double> psnr_per_frame;
126   std::vector<double> ssim_per_frame;
127   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);
131   } 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 }
140