Removing sstream from Profile code, and pretty large refactor to support this effort. All unit tests now passing.

This commit is contained in:
Jonathan Thomas
2025-05-30 17:23:48 -05:00
parent 9b650bab97
commit b2b28e5cc6
2 changed files with 77 additions and 80 deletions

View File

@@ -77,47 +77,47 @@ Profile::Profile(std::string path) {
info.description = value;
}
else if (setting == "frame_rate_num") {
std::stringstream(value) >> value_int;
value_int = std::stoi(value);
info.fps.num = value_int;
}
else if (setting == "frame_rate_den") {
std::stringstream(value) >> value_int;
value_int = std::stoi(value);
info.fps.den = value_int;
}
else if (setting == "width") {
std::stringstream(value) >> value_int;
value_int = std::stoi(value);
info.width = value_int;
}
else if (setting == "height") {
std::stringstream(value) >> value_int;
value_int = std::stoi(value);
info.height = value_int;
}
else if (setting == "progressive") {
std::stringstream(value) >> value_int;
value_int = std::stoi(value);
info.interlaced_frame = !(bool)value_int;
}
else if (setting == "sample_aspect_num") {
std::stringstream(value) >> value_int;
value_int = std::stoi(value);
info.pixel_ratio.num = value_int;
}
else if (setting == "sample_aspect_den") {
std::stringstream(value) >> value_int;
value_int = std::stoi(value);
info.pixel_ratio.den = value_int;
}
else if (setting == "display_aspect_num") {
std::stringstream(value) >> value_int;
value_int = std::stoi(value);
info.display_ratio.num = value_int;
}
else if (setting == "display_aspect_den") {
std::stringstream(value) >> value_int;
value_int = std::stoi(value);
info.display_ratio.den = value_int;
}
else if (setting == "colorspace") {
std::stringstream(value) >> value_int;
value_int = std::stoi(value);
info.pixel_format = value_int;
}
else if (setting == "spherical") {
std::stringstream(value) >> value_int;
value_int = std::stoi(value);
info.spherical = (bool)value_int;
}
}
@@ -141,101 +141,99 @@ Profile::Profile(std::string path) {
// Return a formatted FPS
std::string Profile::formattedFPS(bool include_decimal) {
// Format FPS to use 2 decimals (if needed)
float fps = info.fps.ToFloat();
std::stringstream fps_string;
if (info.fps.den == 1) {
// For example: 24.0 will become 24
fps_string << std::fixed << std::setprecision(0) << fps;
} else {
// For example: 29.97002997 will become 29.97
fps_string << std::fixed << std::setprecision(2) << fps;
// Remove decimal place using QString (for convenience)
if (!include_decimal) {
QString fps_qstring = QString::fromStdString(fps_string.str());
fps_qstring.replace(".", "");
fps_string.str(fps_qstring.toStdString());
if (!include_decimal) {
int fps_code = 0;
if (info.fps.den == 1) {
// Exact integer FPS (e.g. 24 → 0024)
fps_code = info.fps.num;
} else {
// Fractional FPS, scale by 100 (e.g. 29.97 → 2997)
fps_code = static_cast<int>((info.fps.num * 100.0) / info.fps.den + 0.5);
}
char buffer[5];
std::snprintf(buffer, sizeof(buffer), "%04d", fps_code);
return std::string(buffer);
}
return fps_string.str();
// Human-readable version for display
float fps = info.fps.ToFloat();
if (std::fabs(fps - std::round(fps)) < 0.01) {
return std::to_string(static_cast<int>(std::round(fps)));
}
char buffer[16];
std::snprintf(buffer, sizeof(buffer), "%.2f", fps);
return std::string(buffer);
}
// Return a unique key of this profile (01920x1080i2997_16-09)
std::string Profile::Key() {
std::stringstream output;
std::string progressive_str = "p";
if (info.interlaced_frame) {
progressive_str = "i";
}
std::string fps_string = formattedFPS(false);
output << std::setfill('0') << std::setw(5) << info.width << std::setfill('\0') << "x";
output << std::setfill('0') << std::setw(4) << info.height << std::setfill('\0') << progressive_str;
output << std::setfill('0') << std::setw(4) << fps_string << std::setfill('\0') << "_";
output << std::setfill('0') << std::setw(2) << info.display_ratio.num << std::setfill('\0') << "-";
output << std::setfill('0') << std::setw(2) << info.display_ratio.den << std::setfill('\0');
std::string raw_fps = formattedFPS(false);
// Add spherical indicator
if (info.spherical) {
output << "_360";
}
// Pad FPS string to 4 characters with leading zeros
std::string fps_padded = std::string(4 - raw_fps.length(), '0') + raw_fps;
return output.str();
char buffer[64];
std::snprintf(buffer, sizeof(buffer), "%05dx%04d%s%s_%02d-%02d",
info.width,
info.height,
info.interlaced_frame ? "i" : "p",
fps_padded.c_str(),
info.display_ratio.num,
info.display_ratio.den
);
std::string result(buffer);
if (info.spherical)
result += "_360";
return result;
}
// Return the name of this profile (1920x1080p29.97)
std::string Profile::ShortName() {
std::stringstream output;
std::string progressive_str = "p";
if (info.interlaced_frame) {
progressive_str = "i";
}
std::string progressive_str = info.interlaced_frame ? "i" : "p";
std::string fps_string = formattedFPS(true);
output << info.width << "x" << info.height << progressive_str << fps_string;
std::string result = std::to_string(info.width) + "x" + std::to_string(info.height) + progressive_str + fps_string;
// Add 360° indicator for spherical videos
if (info.spherical) {
output << " 360°";
}
return output.str();
if (info.spherical)
result += " 360°";
return result;
}
// Return a longer format name (1920x1080p @ 29.97 fps (16:9))
std::string Profile::LongName() {
std::stringstream output;
std::string progressive_str = "p";
if (info.interlaced_frame) {
progressive_str = "i";
}
std::string progressive_str = info.interlaced_frame ? "i" : "p";
std::string fps_string = formattedFPS(true);
output << info.width << "x" << info.height << progressive_str << " @ " << fps_string
<< " fps (" << info.display_ratio.num << ":" << info.display_ratio.den << ")";
std::string result = std::to_string(info.width) + "x" + std::to_string(info.height) +
progressive_str + " @ " + fps_string +
" fps (" + std::to_string(info.display_ratio.num) + ":" +
std::to_string(info.display_ratio.den) + ")";
// Add 360° indicator for spherical videos
if (info.spherical) {
output << " 360°";
}
return output.str();
if (info.spherical)
result += " 360°";
return result;
}
// Return a longer format name (1920x1080p @ 29.97 fps (16:9) HD 1080i 29.97 fps)
std::string Profile::LongNameWithDesc() {
std::stringstream output;
std::string progressive_str = "p";
if (info.interlaced_frame) {
progressive_str = "i";
}
std::string progressive_str = info.interlaced_frame ? "i" : "p";
std::string fps_string = formattedFPS(true);
output << info.width << "x" << info.height << progressive_str << " @ " << fps_string
<< " fps (" << info.display_ratio.num << ":" << info.display_ratio.den << ")";
// Add 360° indicator for spherical videos
if (info.spherical) {
output << " 360°";
}
std::string result = std::to_string(info.width) + "x" + std::to_string(info.height) +
progressive_str + " @ " + fps_string +
" fps (" + std::to_string(info.display_ratio.num) + ":" +
std::to_string(info.display_ratio.den) + ")";
output << " " << info.description;
return output.str();
if (info.spherical)
result += " 360°";
if (!info.description.empty())
result += " " + info.description;
return result;
}
// Save profile to file system

View File

@@ -15,7 +15,6 @@
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>
#include <fstream>
#include <QtCore/QString>