]> git.sur5r.net Git - minitube/blob - src/videodefinition.cpp
Upload 3.9.3-2 to unstable
[minitube] / src / videodefinition.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
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.
10
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.
15
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/>.
18
19 $END_LICENSE */
20
21 #include "videodefinition.h"
22
23 namespace {
24 const int kEmptyDefinitionCode = -1;
25
26 template <typename T, T (VideoDefinition::*Getter)() const>
27 const VideoDefinition &getDefinitionForImpl(T matchValue) {
28     const auto &defs = VideoDefinition::getDefinitions();
29
30     for (auto i = defs.rbegin(); i != defs.rend(); ++i) {
31         if ((*i.*Getter)() == matchValue) return *i;
32     }
33     /*
34     for (const VideoDefinition &def : defs) {
35         if ((def.*Getter)() == matchValue) return def;
36     }
37     */
38     static const VideoDefinition kEmptyDefinition(QString(), kEmptyDefinitionCode);
39     return kEmptyDefinition;
40 }
41 } // namespace
42
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),
54     };
55     return definitions;
56 }
57
58 const QVector<QString> &VideoDefinition::getDefinitionNames() {
59     static const QVector<QString> names = {"480p", "720p", "1080p", "1440p", "2160p"};
60     return names;
61 }
62
63 const VideoDefinition &VideoDefinition::forName(const QString &name) {
64     return getDefinitionForImpl<const QString &, &VideoDefinition::getName>(name);
65 }
66
67 const VideoDefinition &VideoDefinition::forCode(int code) {
68     return getDefinitionForImpl<int, &VideoDefinition::getCode>(code);
69 }
70
71 VideoDefinition::VideoDefinition(const QString &name, int code, bool hasAudioStream)
72     : name(name), code(code), hasAudioStream(hasAudioStream) {}
73
74 bool VideoDefinition::isEmpty() const {
75     return code == kEmptyDefinitionCode && name.isEmpty();
76 }