Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --json-format argument to print the results into the std output #1

Draft
wants to merge 3 commits into
base: stable
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
build
.vscode
.vscode
cmake-build-*/
34 changes: 34 additions & 0 deletions source/apps/common/ResultToJson.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include "lib/vcaLib.h"
#include <iostream>
#include <nlohmann/json.hpp>

[[maybe_unused]] void to_json(nlohmann::json &j, const vca_frame_results &frameResults)
{
j = nlohmann::json{
{"averageBrightness", frameResults.averageBrightness},
{"averageEnergy", frameResults.averageEnergy},
{"sad", frameResults.sad},
{"averageU", frameResults.averageU},
{"averageV", frameResults.averageV},
{"energyU", frameResults.energyU},
{"energyV", frameResults.energyV},
{"epsilon", frameResults.epsilon},
{"poc", frameResults.poc},
{"isNewShot", frameResults.isNewShot}};
}

[[maybe_unused]] void from_json(const nlohmann::json &j, vca_frame_results &frameResults)
{
j.at("averageBrightness").get_to(frameResults.averageBrightness);
j.at("averageEnergy").get_to(frameResults.averageEnergy);
j.at("sad").get_to(frameResults.sad);
j.at("averageU").get_to(frameResults.averageU);
j.at("averageV").get_to(frameResults.averageV);
j.at("energyU").get_to(frameResults.energyU);
j.at("energyV").get_to(frameResults.energyV);
j.at("epsilon").get_to(frameResults.epsilon);
j.at("poc").get_to(frameResults.poc);
j.at("isNewShot").get_to(frameResults.isNewShot);
}
42 changes: 30 additions & 12 deletions source/apps/vca/vca.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "vcacli.h"

#include <common/ResultToJson.h>
#include <common/input/Y4MInput.h>
#include <common/input/YUVInput.h>
#include <common/stats/YUViewStatsFile.h>
Expand Down Expand Up @@ -67,7 +68,7 @@ void printStatus(uint32_t frameNum, unsigned framesToBeAnalyzed, bool printSumma
prevUpdateTime = now;

auto elapsedAbsMs = std::chrono::duration_cast<std::chrono::milliseconds>(now - startTime)
.count();
.count();
double fps = elapsedAbsMs > 0 ? frameNum * 1000. / elapsedAbsMs : 0;

if (printSummary)
Expand Down Expand Up @@ -116,6 +117,8 @@ struct CLIOptions

vca_param vcaParam;
vca_shot_detection_param shotDetectParam;

bool jsonFormat{};
};

struct Result
Expand Down Expand Up @@ -180,7 +183,7 @@ std::optional<CLIOptions> parseCLIOptions(int argc, char **argv)
if (c != 63)
vca_log(LogLevel::Warning,
"internal error: short option " + std::string(1, c)
+ " has no long option");
+ " has no long option");
return {};
}
}
Expand All @@ -201,6 +204,8 @@ std::optional<CLIOptions> parseCLIOptions(int argc, char **argv)
options.vcaParam.enableChroma = false;
else if (name == "y4m")
options.openAsY4m = true;
else if (name == "json-format")
options.jsonFormat = true;
else
{
auto arg = std::string(optarg);
Expand Down Expand Up @@ -288,7 +293,7 @@ bool checkOptions(CLIOptions options)
{
vca_log(LogLevel::Error,
"Invalid block size (" + std::to_string(options.vcaParam.blockSize)
+ ") provided. Valid values are 8, 16 and 32.");
+ ") provided. Valid values are 8, 16 and 32.");
return false;
}

Expand All @@ -309,26 +314,27 @@ void logOptions(const CLIOptions &options)
vca_log(LogLevel::Info, " Complexity csv: "s + options.complexityCSVFilename);
vca_log(LogLevel::Info, " Shot csv: "s + options.shotCSVFilename);
vca_log(LogLevel::Info, " YUView stats file: "s + options.yuviewStatsFilename);
vca_log(LogLevel::Info, " Results as Json: "s + (options.jsonFormat ? "True"s : "False"s));
}

void logResult(const Result &result, const vca_frame *frame, const unsigned resultsCounter)
{
if (result.result.poc != frame->stats.poc)
vca_log(LogLevel::Warning,
"The poc of the returned data (" + std::to_string(result.result.poc)
+ ") does not match the expected next frames POC ("
+ std::to_string(frame->stats.poc) + ").");
+ ") does not match the expected next frames POC ("
+ std::to_string(frame->stats.poc) + ").");
if (result.result.poc != resultsCounter)
vca_log(LogLevel::Warning,
"The poc of the returned data (" + std::to_string(result.result.poc)
+ ") does not match the expected results counter ("
+ std::to_string(resultsCounter) + ").");
+ ") does not match the expected results counter ("
+ std::to_string(resultsCounter) + ").");

vca_log(LogLevel::Debug,
"Got results POC " + std::to_string(result.result.poc) + "averageBrightness "
+ std::to_string(result.result.averageBrightness) + " averageEnergy "
+ std::to_string(result.result.averageEnergy) + " sad "
+ std::to_string(result.result.sad));
+ std::to_string(result.result.averageBrightness) + " averageEnergy "
+ std::to_string(result.result.averageEnergy) + " sad "
+ std::to_string(result.result.sad));
}

void writeComplexityStatsToFile(const Result &result, std::ofstream &file, bool enableChroma)
Expand Down Expand Up @@ -607,6 +613,12 @@ int main(int argc, char **argv)

logResult(result, processedFrame->getFrame(), resultsCounter);

if (options.jsonFormat)
{
nlohmann::json j = result.result;
std::cout << j << std::endl;
}

frameRecycling.push(std::move(processedFrame));
resultsCounter++;
}
Expand Down Expand Up @@ -636,13 +648,19 @@ int main(int argc, char **argv)

logResult(result, processedFrame->getFrame(), resultsCounter);

if (options.jsonFormat)
{
nlohmann::json j = result.result;
std::cout << j << std::endl;
}

resultsCounter++;
}

vca_analyzer_close(analyzer);
printStatus(resultsCounter, pushedFrames, true);

if (!options.shotCSVFilename.empty())
if (options.shotCSVFilename.empty())
{
if (options.shotDetectParam.fps == 0.0)
options.shotDetectParam.fps = inputFile->getFPS();
Expand All @@ -667,7 +685,7 @@ int main(int argc, char **argv)
[](auto frame) { return frame.isNewShot; });
vca_log(LogLevel::Info,
"Performed shot detection for " + std::to_string(shotDetectFrames.size())
+ " frames. Detected " + std::to_string(nrShots) + " shots.");
+ " frames. Detected " + std::to_string(nrShots) + " shots.");
}

return 0;
Expand Down
2 changes: 2 additions & 0 deletions source/apps/vca/vcacli.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ static const struct option long_options[] = {{"help", no_argument, NULL, 'h'},
{"no-chroma", no_argument, NULL, 0},
{"input", required_argument, NULL, 0},
{"y4m", no_argument, NULL, 0},
{"json-format", no_argument, NULL, 0},
{"input-depth", required_argument, NULL, 0},
{"input-res", required_argument, NULL, 0},
{"input-csp", required_argument, NULL, 0},
Expand Down Expand Up @@ -79,6 +80,7 @@ static void showHelp()
printf(" --yuview-stats <filename> Write the per block results (energy, sad) to a stats "
"file\n");
printf(" that can be visualized using YUView.\n");
printf(" --json-format Print VCA results as Json. Default: Disabled\n");
printf("\nOperation Options:\n");
printf(" --no-simd Disable SIMD. Default: Enabled\n");
printf(" --no-chroma Disable chroma. Default: Enabled\n");
Expand Down
2 changes: 1 addition & 1 deletion source/lib/vcaLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ struct vca_frame_results
bool isNewShot{};

// An increasing counter that is incremented with each call to 'vca_analyzer_push'.
// So with this one can double check that the results are recieved in the right order.
// So with this one can double-check that the results are received in the right order.
unsigned jobID{};
};

Expand Down