File: videoparams.cpp

package info (click to toggle)
olive-editor 20200620-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 40,228 kB
  • sloc: cpp: 51,932; sh: 56; makefile: 7; xml: 7
file content (120 lines) | stat: -rw-r--r-- 3,207 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/***

  Olive - Non-Linear Video Editor
  Copyright (C) 2019 Olive Team

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.

***/

#include "videoparams.h"

#include <QtMath>

#include "core.h"

OLIVE_NAMESPACE_ENTER

VideoParams::VideoParams() :
  format_(PixelFormat::PIX_FMT_INVALID)
{
}

VideoParams::VideoParams(const int &width, const int &height, const PixelFormat::Format &format, const int& divider) :
  width_(width),
  height_(height),
  format_(format),
  divider_(divider)
{
  calculate_effective_size();
}

VideoParams::VideoParams(const int &width, const int &height, const rational &time_base, const PixelFormat::Format &format, const int &divider) :
  width_(width),
  height_(height),
  time_base_(time_base),
  format_(format),
  divider_(divider)
{
  calculate_effective_size();
}

int VideoParams::generate_auto_divider(qint64 width, qint64 height)
{
  // Arbitrary pixel count (from 640x360)
  const int target_res = 230400;

  qint64 megapixels = width * height;

  double squared_divider = double(megapixels) / double(target_res);
  double divider = qSqrt(squared_divider);

  QList<int> supported_dividers = Core::SupportedDividers();

  if (divider <= supported_dividers.first()) {
    return supported_dividers.first();
  } else if (divider >= supported_dividers.last()) {
    return supported_dividers.last();
  } else {
    for (int i=1; i<supported_dividers.size(); i++) {
      int prev_divider = supported_dividers.at(i-1);
      int next_divider = supported_dividers.at(i);

      if (divider >= prev_divider && divider <= next_divider) {
        double prev_diff = qAbs(prev_divider - divider);
        double next_diff = qAbs(next_divider - divider);

        if (prev_diff < next_diff) {
          return prev_divider;
        } else {
          return next_divider;
        }
      }
    }

    // "Safe" fallback
    return 2;
  }
}

bool VideoParams::operator==(const VideoParams &rhs) const
{
  return width() == rhs.width()
      && height() == rhs.height()
      && time_base() == rhs.time_base()
      && format() == rhs.format()
      && divider() == rhs.divider();
}

bool VideoParams::operator!=(const VideoParams &rhs) const
{
  return !(*this == rhs);
}

void VideoParams::calculate_effective_size()
{
  // Fast rounding up to an even number
  effective_width_ = qCeil(width() / divider_ * 0.5) * 2;
  effective_height_ = qCeil(height() / divider_ * 0.5) * 2;
}

bool VideoParams::is_valid() const
{
  return (width() > 0
          && height() > 0
          && format_ != PixelFormat::PIX_FMT_INVALID
          && format_ != PixelFormat::PIX_FMT_COUNT);
}

OLIVE_NAMESPACE_EXIT