Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 <stdio.h> | |
| 11 #include <stdlib.h> | |
| 12 #include <string.h> | |
| 13 #include <iostream> | |
| 14 #include <numeric> | |
| 15 #include <vector> | |
| 16 | |
| 17 #include "webrtc/tools/frame_analyzer/reference_less_video_analysis_lib.h" | |
| 18 #include "webrtc/tools/frame_analyzer/video_quality_analysis.h" | |
| 19 #include "third_party/libvpx/source/libvpx/y4minput.h" | |
|
phoglund
2016/11/30 12:41:01
Oh, so you could just include the file. Nice! Sorr
| |
| 20 | |
| 21 #define STATS_LINE_LENGTH 28 | |
| 22 #define PSNR_FREEZE_THRESHOLD 47 | |
| 23 #define SSIM_FREEZE_THRESHOLD .999 | |
| 24 | |
| 25 void get_height_width_fps(int *height, int *width, int *fps, | |
| 26 const char* video_file) { | |
| 27 FILE* input_file = fopen(video_file, "rb"); | |
| 28 y4m_input y4m; | |
| 29 y4m_input_open(&y4m, input_file, NULL, 0, 0); | |
|
charujain
2016/11/30 09:58:23
We are using this function since this internally c
| |
| 30 | |
| 31 *width = y4m.pic_w; | |
| 32 *height = y4m.pic_h; | |
| 33 *fps = y4m.fps_n; | |
| 34 printf("Height: %d Width: %d fps:%d \n", *height, *width, *fps); | |
| 35 fclose(input_file); | |
| 36 } | |
| 37 | |
| 38 bool frozen_frame(std::vector<double> psnr_per_frame, | |
| 39 std::vector<double> ssim_per_frame, size_t frame) { | |
| 40 if (psnr_per_frame[frame] >= PSNR_FREEZE_THRESHOLD || | |
| 41 ssim_per_frame[frame] >= SSIM_FREEZE_THRESHOLD) | |
| 42 return true; | |
| 43 return false; | |
| 44 } | |
| 45 | |
| 46 std::vector<int> find_frame_clusters(std::vector<double> psnr_per_frame, | |
| 47 std::vector<double> ssim_per_frame) { | |
| 48 std::vector<int> identical_frame_clusters; | |
| 49 int num_frozen = 0; | |
| 50 size_t total_no_of_frames = psnr_per_frame.size(); | |
| 51 | |
| 52 for (size_t each_frame = 0; each_frame < total_no_of_frames; each_frame++) { | |
| 53 if (frozen_frame(psnr_per_frame, ssim_per_frame, each_frame)) { | |
| 54 num_frozen++; | |
| 55 } else if (num_frozen > 0) { | |
| 56 // Not frozen anymore. | |
| 57 identical_frame_clusters.push_back(num_frozen); | |
| 58 num_frozen = 0; | |
| 59 } | |
| 60 } | |
| 61 return identical_frame_clusters; | |
| 62 } | |
| 63 | |
| 64 void print_freezing_metrics(std::vector<double> psnr_per_frame, | |
| 65 std::vector<double> ssim_per_frame) { | |
| 66 /* | |
| 67 * Prints the different metrics mainly: | |
| 68 * 1) Identical frame number, PSNR and SSIM values. | |
| 69 * 2) Length of continuous frozen frames. | |
| 70 * 3) Max length of continuous freezed frames. | |
| 71 * 4) No of unique frames found. | |
| 72 * 5) Total different identical frames found. | |
| 73 * | |
| 74 * Sample output: | |
| 75 * Printing metrics for file: /src/webrtc/tools/test_3.y4m | |
| 76 ============================= | |
| 77 Total number of frames received: 74 | |
| 78 Total identical frames: 5 | |
| 79 Number of unique frames: 69 | |
| 80 Printing Identical Frames: | |
| 81 Frame Number: 29 PSNR: 48.000000 SSIM: 0.999618 | |
| 82 Frame Number: 30 PSNR: 48.000000 SSIM: 0.999898 | |
| 83 Frame Number: 60 PSNR: 48.000000 SSIM: 0.999564 | |
| 84 Frame Number: 64 PSNR: 48.000000 SSIM: 0.999651 | |
| 85 Frame Number: 69 PSNR: 48.000000 SSIM: 0.999684 | |
| 86 Print identical frame which appears in clusters : | |
| 87 2 1 1 1 | |
| 88 * | |
| 89 */ | |
| 90 size_t total_no_of_frames = psnr_per_frame.size(); | |
| 91 std::vector<int> identical_frame_clusters = find_frame_clusters( | |
| 92 psnr_per_frame, ssim_per_frame); | |
| 93 int total_identical_frames = std::accumulate( | |
| 94 identical_frame_clusters.begin(), identical_frame_clusters.end(), 0); | |
| 95 size_t unique_frames = total_no_of_frames - total_identical_frames; | |
| 96 | |
| 97 printf("Total number of frames received: %zu\n", total_no_of_frames); | |
| 98 printf("Total identical frames: %d\n", total_identical_frames); | |
| 99 printf("Number of unique frames: %zu\n", unique_frames); | |
| 100 | |
| 101 printf("Printing Identical Frames: \n"); | |
| 102 for (size_t frame = 0; frame < total_no_of_frames; frame++) { | |
| 103 if (frozen_frame(psnr_per_frame, ssim_per_frame, frame)) { | |
| 104 printf(" Frame Number: %zu PSNR: %f SSIM: %f \n", frame, | |
| 105 psnr_per_frame[frame], ssim_per_frame[frame]); | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 printf("Print identical frame which appears in clusters : \n"); | |
| 110 for (int cluster = 0; | |
| 111 cluster < static_cast<int>(identical_frame_clusters.size()); cluster++) | |
| 112 printf("%d ", identical_frame_clusters[cluster]); | |
| 113 printf("\n"); | |
| 114 } | |
| 115 | |
| 116 void compute_metrics(std::string video_file_name, | |
| 117 std::vector<double>* psnr_per_frame, | |
| 118 std::vector<double>* ssim_per_frame) { | |
| 119 int height = 0, width = 0, fps = 0; | |
| 120 get_height_width_fps(&height, &width, &fps, video_file_name.c_str()); | |
| 121 | |
| 122 int no_of_frames = 0; | |
| 123 int size = webrtc::test::GetI420FrameSize(width, height); | |
| 124 | |
| 125 // Allocate buffers for test and reference frames. | |
| 126 uint8_t* current_frame = new uint8_t[size]; | |
| 127 uint8_t* next_frame = new uint8_t[size]; | |
| 128 | |
| 129 while (true) { | |
| 130 if (!(webrtc::test::ExtractFrameFromY4mFile (video_file_name.c_str(), | |
| 131 width, height, | |
| 132 no_of_frames, | |
| 133 current_frame))) { | |
| 134 break; | |
| 135 } | |
| 136 | |
| 137 if (!(webrtc::test::ExtractFrameFromY4mFile (video_file_name.c_str(), | |
| 138 width, height, | |
| 139 no_of_frames + 1, | |
| 140 next_frame))) { | |
| 141 break; | |
| 142 } | |
| 143 | |
| 144 double result_psnr = webrtc::test::CalculateMetrics(webrtc::test::kPSNR, | |
| 145 current_frame, | |
| 146 next_frame, | |
| 147 width, height); | |
| 148 double result_ssim = webrtc::test::CalculateMetrics(webrtc::test::kSSIM, | |
| 149 current_frame, | |
| 150 next_frame, | |
| 151 width, height); | |
| 152 | |
| 153 psnr_per_frame->push_back(result_psnr); | |
| 154 ssim_per_frame->push_back(result_ssim); | |
| 155 no_of_frames++; | |
| 156 } | |
| 157 // Cleanup. | |
| 158 delete[] current_frame; | |
| 159 delete[] next_frame; | |
| 160 } | |
| 161 | |
| 162 bool check_file_extension(std::string video_file_name) { | |
| 163 if (video_file_name.substr(video_file_name.length()-3, 3) != "y4m") { | |
| 164 printf("Only y4m video file format is supported. Given: %s", | |
| 165 video_file_name.c_str()); | |
| 166 return false; | |
| 167 } | |
| 168 return true; | |
| 169 } | |
| 170 | |
| 171 int run_analysis(std::string video_file) { | |
| 172 std::vector<double> psnr_per_frame; | |
| 173 std::vector<double> ssim_per_frame; | |
| 174 if (check_file_extension(video_file)) { | |
| 175 compute_metrics(video_file, &psnr_per_frame, &ssim_per_frame); | |
| 176 } else { | |
| 177 return -1; | |
| 178 } | |
| 179 printf("=============================\n"); | |
| 180 printf("Printing metrics for file: %s\n", video_file.c_str()); | |
| 181 printf("=============================\n"); | |
| 182 print_freezing_metrics(psnr_per_frame, ssim_per_frame); | |
| 183 return 0; | |
| 184 } | |
| OLD | NEW |