3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Minitube. If not, see <http://www.gnu.org/licenses/>.
21 #include "videodefinition.h"
24 const int kEmptyDefinitionCode = -1;
26 template <typename T, T (VideoDefinition::*Getter)() const>
27 const VideoDefinition &getDefinitionForImpl(T matchValue) {
28 const auto &defs = VideoDefinition::getDefinitions();
30 for (auto i = defs.rbegin(); i != defs.rend(); ++i) {
31 if ((*i.*Getter)() == matchValue) return *i;
34 for (const VideoDefinition &def : defs) {
35 if ((def.*Getter)() == matchValue) return def;
38 static const VideoDefinition kEmptyDefinition(QString(), kEmptyDefinitionCode);
39 return kEmptyDefinition;
43 const QVector<VideoDefinition> &VideoDefinition::getDefinitions() {
44 // List preferred equivalent format last:
45 // algo selects the last format with same name first
46 static const QVector<VideoDefinition> definitions = {
47 VideoDefinition("240p", 242), VideoDefinition("240p", 133),
48 VideoDefinition("360p", 243), VideoDefinition("360p", 396),
49 VideoDefinition("360p", 18, true), VideoDefinition("480p", 244),
50 VideoDefinition("480p", 135), VideoDefinition("720p", 247),
51 VideoDefinition("720p", 136), VideoDefinition("720p", 22, true),
52 VideoDefinition("1080p", 248), VideoDefinition("1080p", 137),
53 VideoDefinition("1440p", 271), VideoDefinition("2160p", 313),
58 const QVector<QString> &VideoDefinition::getDefinitionNames() {
59 static const QVector<QString> names = {"480p", "720p", "1080p", "1440p", "2160p"};
63 const VideoDefinition &VideoDefinition::forName(const QString &name) {
64 return getDefinitionForImpl<const QString &, &VideoDefinition::getName>(name);
67 const VideoDefinition &VideoDefinition::forCode(int code) {
68 return getDefinitionForImpl<int, &VideoDefinition::getCode>(code);
71 VideoDefinition::VideoDefinition(const QString &name, int code, bool hasAudioStream)
72 : name(name), code(code), hasAudioStream(hasAudioStream) {}
74 bool VideoDefinition::isEmpty() const {
75 return code == kEmptyDefinitionCode && name.isEmpty();