2013-07-05 01:28:43 +02:00
|
|
|
// Copyright (c) 2013, Thomas Goyne <plorkyeran@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
//
|
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
#include "resolution_resampler.h"
|
|
|
|
|
|
|
|
#include "ass_dialogue.h"
|
|
|
|
#include "ass_file.h"
|
|
|
|
#include "ass_style.h"
|
2013-07-31 05:31:10 +02:00
|
|
|
#include "utils.h"
|
2013-07-05 01:28:43 +02:00
|
|
|
|
|
|
|
#include <libaegisub/of_type_adaptor.h>
|
|
|
|
#include <libaegisub/split.h>
|
2013-07-31 05:31:10 +02:00
|
|
|
#include <libaegisub/util.h>
|
2014-05-20 01:47:54 +02:00
|
|
|
#include <libaegisub/ycbcr_conv.h>
|
2013-07-05 01:28:43 +02:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2014-05-16 20:46:07 +02:00
|
|
|
#include <cmath>
|
2014-05-23 00:40:16 +02:00
|
|
|
#include <wx/intl.h>
|
2013-07-05 01:28:43 +02:00
|
|
|
|
|
|
|
enum {
|
|
|
|
LEFT = 0,
|
|
|
|
RIGHT = 1,
|
|
|
|
TOP = 2,
|
|
|
|
BOTTOM = 3
|
|
|
|
};
|
|
|
|
|
2014-05-20 01:47:54 +02:00
|
|
|
static const std::string names[] = {
|
|
|
|
"None",
|
|
|
|
"TV.601", "PC.601",
|
|
|
|
"TV.709", "PC.709",
|
|
|
|
"TV.FCC", "PC.FCC",
|
|
|
|
"TV.240M", "PC.240M"
|
|
|
|
};
|
|
|
|
|
|
|
|
YCbCrMatrix MatrixFromString(std::string const& str) {
|
|
|
|
if (str.empty()) return YCbCrMatrix::tv_601;
|
|
|
|
auto pos = std::find(std::begin(names), std::end(names), str);
|
|
|
|
if (pos == std::end(names))
|
|
|
|
return YCbCrMatrix::rgb;
|
|
|
|
return static_cast<YCbCrMatrix>(std::distance(std::begin(names), pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MatrixToString(YCbCrMatrix mat) {
|
|
|
|
return names[static_cast<int>(mat)];
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> MatrixNames() {
|
|
|
|
return {std::begin(names), std::end(names)};
|
|
|
|
}
|
|
|
|
|
2013-07-05 01:28:43 +02:00
|
|
|
namespace {
|
|
|
|
std::string transform_drawing(std::string const& drawing, int shift_x, int shift_y, double scale_x, double scale_y) {
|
|
|
|
bool is_x = true;
|
|
|
|
std::string final;
|
|
|
|
final.reserve(drawing.size());
|
|
|
|
|
|
|
|
for (auto const& cur : agi::Split(drawing, ' ')) {
|
2013-07-31 05:31:10 +02:00
|
|
|
double val;
|
|
|
|
if (agi::util::try_parse(agi::str(cur), &val)) {
|
2013-07-05 01:28:43 +02:00
|
|
|
if (is_x)
|
2013-07-31 05:31:10 +02:00
|
|
|
val = (val + shift_x) * scale_x;
|
2013-07-05 01:28:43 +02:00
|
|
|
else
|
2013-07-31 05:31:10 +02:00
|
|
|
val = (val + shift_y) * scale_y;
|
2014-05-16 20:46:07 +02:00
|
|
|
val = round(val * 8) / 8.0; // round to eighth-pixels
|
2013-07-31 05:31:10 +02:00
|
|
|
final += float_to_string(val);
|
2013-07-05 01:28:43 +02:00
|
|
|
final += ' ';
|
2013-07-31 05:31:10 +02:00
|
|
|
is_x = !is_x;
|
2013-07-05 01:28:43 +02:00
|
|
|
}
|
|
|
|
else if (cur.size() == 1) {
|
|
|
|
char c = tolower(cur[0]);
|
|
|
|
if (c == 'm' || c == 'n' || c == 'l' || c == 'b' || c == 's' || c == 'p' || c == 'c') {
|
|
|
|
is_x = true;
|
|
|
|
final += c;
|
|
|
|
final += ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-23 18:54:39 +01:00
|
|
|
if (final.size())
|
|
|
|
final.pop_back();
|
2013-07-05 01:28:43 +02:00
|
|
|
return final;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct resample_state {
|
|
|
|
const int *margin;
|
|
|
|
double rx;
|
|
|
|
double ry;
|
|
|
|
double ar;
|
2014-05-20 01:47:54 +02:00
|
|
|
agi::ycbcr_converter conv;
|
|
|
|
bool convert_colors;
|
2013-07-05 01:28:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void resample_tags(std::string const& name, AssOverrideParameter *cur, void *ud) {
|
|
|
|
resample_state *state = static_cast<resample_state *>(ud);
|
|
|
|
|
|
|
|
double resizer = 1.0;
|
|
|
|
int shift = 0;
|
|
|
|
|
|
|
|
switch (cur->classification) {
|
|
|
|
case AssParameterClass::ABSOLUTE_SIZE:
|
|
|
|
resizer = state->ry;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AssParameterClass::ABSOLUTE_POS_X:
|
|
|
|
resizer = state->rx;
|
|
|
|
shift = state->margin[LEFT];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AssParameterClass::ABSOLUTE_POS_Y:
|
|
|
|
resizer = state->ry;
|
|
|
|
shift = state->margin[TOP];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AssParameterClass::RELATIVE_SIZE_X:
|
|
|
|
resizer = state->ar;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AssParameterClass::RELATIVE_SIZE_Y:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AssParameterClass::DRAWING: {
|
|
|
|
cur->Set(transform_drawing(
|
|
|
|
cur->Get<std::string>(),
|
|
|
|
state->margin[LEFT], state->margin[TOP], state->rx, state->ry));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-20 01:47:54 +02:00
|
|
|
case AssParameterClass::COLOR:
|
|
|
|
if (state->convert_colors)
|
|
|
|
cur->Set<std::string>(state->conv.rgb_to_rgb(agi::Color{cur->Get<std::string>()}).GetAssOverrideFormatted());
|
|
|
|
return;
|
|
|
|
|
2013-07-05 01:28:43 +02:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
VariableDataType curType = cur->GetType();
|
|
|
|
if (curType == VariableDataType::FLOAT)
|
|
|
|
cur->Set((cur->Get<double>() + shift) * resizer);
|
|
|
|
else if (curType == VariableDataType::INT)
|
|
|
|
cur->Set<int>((cur->Get<int>() + shift) * resizer + 0.5);
|
|
|
|
}
|
|
|
|
|
2014-03-07 19:58:51 +01:00
|
|
|
void resample_line(resample_state *state, AssDialogue &diag) {
|
|
|
|
if (diag.Comment && (boost::starts_with(diag.Effect.get(), "template") || boost::starts_with(diag.Effect.get(), "code")))
|
|
|
|
return;
|
2013-07-05 01:28:43 +02:00
|
|
|
|
2014-04-14 19:58:46 +02:00
|
|
|
auto blocks = diag.ParseTags();
|
2013-07-05 01:28:43 +02:00
|
|
|
|
2014-03-07 19:58:51 +01:00
|
|
|
for (auto block : blocks | agi::of_type<AssDialogueBlockOverride>())
|
|
|
|
block->ProcessParameters(resample_tags, state);
|
2013-07-05 01:28:43 +02:00
|
|
|
|
2014-03-07 19:58:51 +01:00
|
|
|
for (auto drawing : blocks | agi::of_type<AssDialogueBlockDrawing>())
|
2014-05-16 19:54:00 +02:00
|
|
|
drawing->text = transform_drawing(drawing->text, 0, 0, state->rx / state->ar, state->ry);
|
2013-07-05 01:28:43 +02:00
|
|
|
|
2014-05-16 20:59:38 +02:00
|
|
|
for (size_t i = 0; i < 3; ++i) {
|
|
|
|
if (diag.Margin[i])
|
|
|
|
diag.Margin[i] = int((diag.Margin[i] + state->margin[i]) * (i < 2 ? state->rx : state->ry) + 0.5);
|
|
|
|
}
|
2014-03-07 19:58:51 +01:00
|
|
|
|
|
|
|
diag.UpdateText(blocks);
|
|
|
|
}
|
|
|
|
|
|
|
|
void resample_style(resample_state *state, AssStyle &style) {
|
|
|
|
style.fontsize = int(style.fontsize * state->ry + 0.5);
|
|
|
|
style.outline_w *= state->ry;
|
|
|
|
style.shadow_w *= state->ry;
|
|
|
|
style.spacing *= state->rx;
|
|
|
|
style.scalex *= state->ar;
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
style.Margin[i] = int((style.Margin[i] + state->margin[i]) * (i < 2 ? state->rx : state->ry) + 0.5);
|
2014-05-20 01:47:54 +02:00
|
|
|
if (state->convert_colors) {
|
|
|
|
style.primary = state->conv.rgb_to_rgb(style.primary);
|
|
|
|
style.secondary = state->conv.rgb_to_rgb(style.secondary);
|
|
|
|
style.outline = state->conv.rgb_to_rgb(style.outline);
|
|
|
|
style.shadow = state->conv.rgb_to_rgb(style.shadow);
|
|
|
|
}
|
2014-03-07 19:58:51 +01:00
|
|
|
style.UpdateData();
|
2013-07-05 01:28:43 +02:00
|
|
|
}
|
2014-05-20 01:47:54 +02:00
|
|
|
|
|
|
|
agi::ycbcr_matrix matrix(YCbCrMatrix mat) {
|
|
|
|
switch (mat) {
|
|
|
|
case YCbCrMatrix::rgb: return agi::ycbcr_matrix::bt601;
|
|
|
|
case YCbCrMatrix::tv_601: case YCbCrMatrix::pc_601: return agi::ycbcr_matrix::bt601;
|
|
|
|
case YCbCrMatrix::tv_709: case YCbCrMatrix::pc_709: return agi::ycbcr_matrix::bt709;
|
|
|
|
case YCbCrMatrix::tv_fcc: case YCbCrMatrix::pc_fcc: return agi::ycbcr_matrix::fcc;
|
|
|
|
case YCbCrMatrix::tv_240m: case YCbCrMatrix::pc_240m: return agi::ycbcr_matrix::smpte_240m;
|
|
|
|
}
|
2014-05-29 14:57:27 +02:00
|
|
|
throw agi::InternalError("Invalid matrix");
|
2014-05-20 01:47:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
agi::ycbcr_range range(YCbCrMatrix mat) {
|
|
|
|
switch (mat) {
|
|
|
|
case YCbCrMatrix::rgb:
|
|
|
|
case YCbCrMatrix::tv_601:
|
|
|
|
case YCbCrMatrix::tv_709:
|
|
|
|
case YCbCrMatrix::tv_fcc:
|
|
|
|
case YCbCrMatrix::tv_240m:
|
|
|
|
return agi::ycbcr_range::tv;
|
|
|
|
case YCbCrMatrix::pc_601:
|
|
|
|
case YCbCrMatrix::pc_709:
|
|
|
|
case YCbCrMatrix::pc_fcc:
|
|
|
|
case YCbCrMatrix::pc_240m:
|
|
|
|
return agi::ycbcr_range::pc;
|
|
|
|
}
|
2014-05-29 14:57:27 +02:00
|
|
|
throw agi::InternalError("Invalid matrix");
|
2014-05-20 01:47:54 +02:00
|
|
|
}
|
2013-07-05 01:28:43 +02:00
|
|
|
}
|
|
|
|
|
2013-09-08 17:03:55 +02:00
|
|
|
void ResampleResolution(AssFile *ass, ResampleSettings settings) {
|
2014-05-16 18:20:30 +02:00
|
|
|
auto horizontal_stretch = 1.0;
|
|
|
|
auto old_ar = double(settings.source_x) / settings.source_y;
|
|
|
|
auto new_ar = double(settings.dest_x) / settings.dest_y;
|
|
|
|
bool border_horizontally = new_ar > old_ar;
|
|
|
|
// Don't convert aspect ratio if it's very close to correct
|
|
|
|
// (for reference, 848x480 <-> 1280x720 is .006)
|
|
|
|
if (abs(old_ar - new_ar) / new_ar > .01) {
|
|
|
|
switch (settings.ar_mode) {
|
|
|
|
case ResampleARMode::RemoveBorder:
|
|
|
|
border_horizontally = !border_horizontally;
|
|
|
|
case ResampleARMode::AddBorder:
|
|
|
|
if (border_horizontally) // Wider/Shorter
|
|
|
|
settings.margin[LEFT] = settings.margin[RIGHT] = (settings.source_y * new_ar - settings.source_x) / 2;
|
|
|
|
else // Taller/Narrower
|
|
|
|
settings.margin[TOP] = settings.margin[BOTTOM] = (settings.source_x / new_ar - settings.source_y) / 2;
|
|
|
|
break;
|
|
|
|
case ResampleARMode::Stretch:
|
|
|
|
horizontal_stretch = new_ar / old_ar;
|
|
|
|
break;
|
|
|
|
case ResampleARMode::Manual:
|
|
|
|
old_ar =
|
|
|
|
double(settings.source_x + settings.margin[LEFT] + settings.margin[RIGHT]) /
|
|
|
|
double(settings.source_y + settings.margin[TOP] + settings.margin[BOTTOM]);
|
|
|
|
|
|
|
|
if (abs(old_ar - new_ar) / new_ar > .01)
|
|
|
|
horizontal_stretch = new_ar / old_ar;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-05 01:28:43 +02:00
|
|
|
// Add margins to original resolution
|
2013-09-08 17:03:55 +02:00
|
|
|
settings.source_x += settings.margin[LEFT] + settings.margin[RIGHT];
|
|
|
|
settings.source_y += settings.margin[TOP] + settings.margin[BOTTOM];
|
2013-07-05 01:28:43 +02:00
|
|
|
|
2014-05-20 01:47:54 +02:00
|
|
|
bool resample_colors =
|
|
|
|
settings.source_matrix != settings.dest_matrix &&
|
|
|
|
settings.source_matrix != YCbCrMatrix::rgb &&
|
|
|
|
settings.dest_matrix != YCbCrMatrix::rgb;
|
|
|
|
|
2013-07-05 01:28:43 +02:00
|
|
|
resample_state state = {
|
|
|
|
settings.margin,
|
2013-09-08 17:03:55 +02:00
|
|
|
double(settings.dest_x) / double(settings.source_x),
|
|
|
|
double(settings.dest_y) / double(settings.source_y),
|
2014-05-20 01:47:54 +02:00
|
|
|
horizontal_stretch,
|
|
|
|
agi::ycbcr_converter{
|
|
|
|
matrix(settings.source_matrix),
|
|
|
|
range(settings.source_matrix),
|
|
|
|
matrix(settings.dest_matrix),
|
|
|
|
range(settings.dest_matrix),
|
|
|
|
},
|
|
|
|
resample_colors
|
2013-07-05 01:28:43 +02:00
|
|
|
};
|
|
|
|
|
2014-03-07 18:02:24 +01:00
|
|
|
for (auto& line : ass->Styles)
|
2014-03-07 19:58:51 +01:00
|
|
|
resample_style(&state, line);
|
2014-03-07 18:02:24 +01:00
|
|
|
for (auto& line : ass->Events)
|
2013-07-05 01:28:43 +02:00
|
|
|
resample_line(&state, line);
|
|
|
|
|
2013-09-08 17:03:55 +02:00
|
|
|
ass->SetScriptInfo("PlayResX", std::to_string(settings.dest_x));
|
|
|
|
ass->SetScriptInfo("PlayResY", std::to_string(settings.dest_y));
|
2014-05-20 01:47:54 +02:00
|
|
|
ass->SetScriptInfo("YCbCr Matrix", MatrixToString(settings.dest_matrix));
|
2013-07-05 01:28:43 +02:00
|
|
|
|
|
|
|
ass->Commit(_("resolution resampling"), AssFile::COMMIT_SCRIPTINFO | AssFile::COMMIT_DIAG_FULL);
|
|
|
|
}
|