From ec0b206fee166975fdb3667dee22da0ce50a1107 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Wed, 12 Nov 2014 02:22:05 -0800 Subject: [PATCH] Qt: Add ability to manually specify and lock video aspect ratio --- src/platform/qt/VideoView.cpp | 52 +++++++++++++++++++++++++++++++++-- src/platform/qt/VideoView.h | 3 ++ src/platform/qt/VideoView.ui | 31 +++++++++++++++------ 3 files changed, 75 insertions(+), 11 deletions(-) diff --git a/src/platform/qt/VideoView.cpp b/src/platform/qt/VideoView.cpp index 6e4a56ba9..da68f60f5 100644 --- a/src/platform/qt/VideoView.cpp +++ b/src/platform/qt/VideoView.cpp @@ -82,6 +82,9 @@ VideoView::VideoView(QWidget* parent) connect(m_ui.width, SIGNAL(valueChanged(int)), this, SLOT(setWidth(int))); connect(m_ui.height, SIGNAL(valueChanged(int)), this, SLOT(setHeight(int))); + connect(m_ui.wratio, SIGNAL(valueChanged(int)), this, SLOT(setAspectWidth(int))); + connect(m_ui.hratio, SIGNAL(valueChanged(int)), this, SLOT(setAspectHeight(int))); + connect(m_ui.showAdvanced, SIGNAL(clicked(bool)), this, SLOT(showAdvanced(bool))); FFmpegEncoderInit(&m_encoder); @@ -253,8 +256,8 @@ void VideoView::setVideoBitrate(int br, bool manual) { void VideoView::setWidth(int width, bool manual) { m_width = width; + updateAspectRatio(width, 0); FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height); - validateSettings(); if (manual) { uncheckIncompatible(); } @@ -262,8 +265,24 @@ void VideoView::setWidth(int width, bool manual) { void VideoView::setHeight(int height, bool manual) { m_height = height; + updateAspectRatio(0, height); + FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height); + if (manual) { + uncheckIncompatible(); + } +} + +void VideoView::setAspectWidth(int, bool manual) { + updateAspectRatio(0, m_height, true); + FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height); + if (manual) { + uncheckIncompatible(); + } +} + +void VideoView::setAspectHeight(int, bool manual) { + updateAspectRatio(m_width, 0, true); FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height); - validateSettings(); if (manual) { uncheckIncompatible(); } @@ -307,6 +326,35 @@ bool VideoView::validateSettings() { return valid; } +void VideoView::updateAspectRatio(int width, int height, bool force) { + if (m_ui.lockRatio->isChecked() || force) { + if (width) { + height = m_ui.hratio->value() * width / m_ui.wratio->value(); + } else if (height) { + width = m_ui.wratio->value() * height / m_ui.hratio->value(); + } + + m_width = width; + m_height = height; + safelySet(m_ui.width, m_width); + safelySet(m_ui.height, m_height); + } else { + int w = m_width; + int h = m_height; + // Get greatest common divisor + while (w != 0) { + int temp = h % w; + h = w; + w = temp; + } + int gcd = h; + w = m_width / gcd; + h = m_height / gcd; + safelySet(m_ui.wratio, w); + safelySet(m_ui.hratio, h); + } +} + void VideoView::uncheckIncompatible() { Preset current = { .container = m_container, diff --git a/src/platform/qt/VideoView.h b/src/platform/qt/VideoView.h index 633a052bb..a2b008f43 100644 --- a/src/platform/qt/VideoView.h +++ b/src/platform/qt/VideoView.h @@ -54,6 +54,8 @@ private slots: void setWidth(int, bool manual = true); void setHeight(int, bool manual = true); + void setAspectWidth(int, bool manual = true); + void setAspectHeight(int, bool manual = true); void showAdvanced(bool); @@ -61,6 +63,7 @@ private slots: private: bool validateSettings(); + void updateAspectRatio(int width, int height, bool force = false); static QString sanitizeCodec(const QString&, const QMap& mapping); static void safelyCheck(QAbstractButton*, bool set = true); static void safelySet(QSpinBox*, int value); diff --git a/src/platform/qt/VideoView.ui b/src/platform/qt/VideoView.ui index 685a630d1..98bfb4034 100644 --- a/src/platform/qt/VideoView.ui +++ b/src/platform/qt/VideoView.ui @@ -431,12 +431,12 @@ - - false - 1 + + 9999 + 2 @@ -444,12 +444,12 @@ - - false - 1 + + 9999 + 3 @@ -457,9 +457,6 @@ - - false - Lock aspect ratio @@ -496,9 +493,25 @@ start stop selectFile + presetHQ + presetYoutube + presetWebM + presetLossless + preset1080 + preset720 + preset480 + preset160 container video audio + vbr + abr + width + height + wratio + hratio + lockRatio + showAdvanced