]> git.sesse.net Git - casparcg/blob - core/video_format.cpp
[bluefish_consumer] Install GPF handler for custom thread.
[casparcg] / core / video_format.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG 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 * CasparCG 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 CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "StdAfx.h"
23
24 #include "video_format.h"
25
26 #include <boost/algorithm/string.hpp>
27
28 namespace caspar { namespace core {
29
30         const std::vector<video_format_desc> format_descs = {
31                 { video_format::pal,          720,  576,  1024, 576,  field_mode::upper,       25000, 1000, L"PAL",          { 1920                         } },
32                 { video_format::ntsc,         720,  486,  720,  540,  field_mode::lower,       30000, 1001, L"NTSC",         { 1602, 1601, 1602, 1601, 1602 } },
33                 { video_format::x576p2500,    720,  576,  1024, 576,  field_mode::progressive, 25000, 1000, L"576p2500",     { 1920                         } },
34                 { video_format::x720p2398,    1280, 720,  1280, 720,  field_mode::progressive, 24000, 1001, L"720p2398",     { 2002                         } },
35                 { video_format::x720p2400,    1280, 720,  1280, 720,  field_mode::progressive, 24000, 1000, L"720p2400",     { 2000                         } },
36                 { video_format::x720p2500,    1280, 720,  1280, 720,  field_mode::progressive, 25000, 1000, L"720p2500",     { 1920                         } },
37                 { video_format::x720p5000,    1280, 720,  1280, 720,  field_mode::progressive, 50000, 1000, L"720p5000",     { 960                          } },
38                 { video_format::x720p2997,    1280, 720,  1280, 720,  field_mode::progressive, 30000, 1001, L"720p2997",     { 1602, 1601, 1602, 1601, 1602 } },
39                 { video_format::x720p5994,    1280, 720,  1280, 720,  field_mode::progressive, 60000, 1001, L"720p5994",     { 801,  800,  801,  801,  801  } },
40                 { video_format::x720p3000,    1280, 720,  1280, 720,  field_mode::progressive, 30000, 1000, L"720p3000",     { 1600                         } },
41                 { video_format::x720p6000,    1280, 720,  1280, 720,  field_mode::progressive, 60000, 1000, L"720p6000",     { 800                          } },
42                 { video_format::x1080p2398,   1920, 1080, 1920, 1080, field_mode::progressive, 24000, 1001, L"1080p2398",    { 2002                         } },
43                 { video_format::x1080p2400,   1920, 1080, 1920, 1080, field_mode::progressive, 24000, 1000, L"1080p2400",    { 2000                         } },
44                 { video_format::x1080i5000,   1920, 1080, 1920, 1080, field_mode::upper,       25000, 1000, L"1080i5000",    { 1920                         } },
45                 { video_format::x1080i5994,   1920, 1080, 1920, 1080, field_mode::upper,       30000, 1001, L"1080i5994",    { 1602, 1601, 1602, 1601, 1602 } },
46                 { video_format::x1080i6000,   1920, 1080, 1920, 1080, field_mode::upper,       30000, 1000, L"1080i6000",    { 1600                         } },
47                 { video_format::x1080p2500,   1920, 1080, 1920, 1080, field_mode::progressive, 25000, 1000, L"1080p2500",    { 1920                         } },
48                 { video_format::x1080p2997,   1920, 1080, 1920, 1080, field_mode::progressive, 30000, 1001, L"1080p2997",    { 1602, 1601, 1602, 1601, 1602 } },
49                 { video_format::x1080p3000,   1920, 1080, 1920, 1080, field_mode::progressive, 30000, 1000, L"1080p3000",    { 1600                         } },
50                 { video_format::x1080p5000,   1920, 1080, 1920, 1080, field_mode::progressive, 50000, 1000, L"1080p5000",    { 960                          } },
51                 { video_format::x1080p5994,   1920, 1080, 1920, 1080, field_mode::progressive, 60000, 1001, L"1080p5994",    { 801,  800,  801,  801,  801  } },
52                 { video_format::x1080p6000,   1920, 1080, 1920, 1080, field_mode::progressive, 60000, 1000, L"1080p6000",    { 800                          } },
53                 { video_format::x1556p2398,   2048, 1556, 2048, 1556, field_mode::progressive, 24000, 1001, L"1556p2398",    { 2002                         } },
54                 { video_format::x1556p2400,   2048, 1556, 2048, 1556, field_mode::progressive, 24000, 1000, L"1556p2400",    { 2000                         } },
55                 { video_format::x1556p2500,   2048, 1556, 2048, 1556, field_mode::progressive, 25000, 1000, L"1556p2500",    { 1920                         } },
56                 { video_format::dci1080p2398, 2048, 1080, 2048, 1080, field_mode::progressive, 24000, 1001, L"dci1080p2398", { 2002                         } },
57                 { video_format::dci1080p2400, 2048, 1080, 2048, 1080, field_mode::progressive, 24000, 1000, L"dci1080p2400", { 2000                         } },
58                 { video_format::dci1080p2500, 2048, 1080, 2048, 1080, field_mode::progressive, 25000, 1000, L"dci1080p2500", { 1920                         } },
59                 { video_format::x2160p2398,   3840, 2160, 3840, 2160, field_mode::progressive, 24000, 1001, L"2160p2398",    { 2002                         } },
60                 { video_format::x2160p2400,   3840, 2160, 3840, 2160, field_mode::progressive, 24000, 1000, L"2160p2400",    { 2000                         } },
61                 { video_format::x2160p2500,   3840, 2160, 3840, 2160, field_mode::progressive, 25000, 1000, L"2160p2500",    { 1920                         } },
62                 { video_format::x2160p2997,   3840, 2160, 3840, 2160, field_mode::progressive, 30000, 1001, L"2160p2997",    { 1602, 1601, 1602, 1601, 1602 } },
63                 { video_format::x2160p3000,   3840, 2160, 3840, 2160, field_mode::progressive, 30000, 1000, L"2160p3000",    { 1600                         } },
64                 { video_format::x2160p5000,   3840, 2160, 3840, 2160, field_mode::progressive, 50000, 1000, L"2160p5000",    { 960                          } },
65                 { video_format::x2160p5994,   3840, 2160, 3840, 2160, field_mode::progressive, 60000, 1001, L"2160p5994",    { 801,  800,  801,  801,  801  } },
66                 { video_format::x2160p6000,   3840, 2160, 3840, 2160, field_mode::progressive, 60000, 1000, L"2160p6000",    { 800                          } },
67                 { video_format::dci2160p2398, 4096, 2160, 4096, 2160, field_mode::progressive, 24000, 1001, L"dci2160p2398", { 2002                         } },
68                 { video_format::dci2160p2400, 4096, 2160, 4096, 2160, field_mode::progressive, 24000, 1000, L"dci2160p2400", { 2000                         } },
69                 { video_format::dci2160p2500, 4096, 2160, 4096, 2160, field_mode::progressive, 25000, 1000, L"dci2160p2500", { 1920                         } },
70                 { video_format::invalid,      0,    0,    0,    0,    field_mode::progressive, 1,     1,    L"invalid",      { 1                            } }
71         };
72
73 video_format_desc::video_format_desc(
74                 video_format format,
75                 int width,
76                 int height,
77                 int square_width,
78                 int square_height,
79                 core::field_mode field_mode,
80                 int time_scale,
81                 int duration,
82                 const std::wstring& name,
83                 const std::vector<int>& audio_cadence)
84         : format(format)
85         , width(width)
86         , height(height)
87         , square_width(square_width)
88         , square_height(square_height)
89         , field_mode(field_mode)
90         , fps(static_cast<double>(time_scale) / static_cast<double>(duration))
91         , framerate(time_scale, duration)
92         , time_scale(time_scale)
93         , duration(duration)
94         , field_count(field_mode == core::field_mode::progressive ? 1 : 2)
95         , size(width*height*4)
96         , name(name)
97         , audio_sample_rate(48000)
98         , audio_cadence(audio_cadence)
99 {
100 }
101
102 video_format_desc::video_format_desc(video_format format)
103         : format(video_format::invalid)
104         , field_mode(core::field_mode::empty)
105 {
106         *this = format_descs.at(static_cast<int>(format));
107 }
108
109 video_format_desc::video_format_desc(const std::wstring& name)
110         : format(video_format::invalid)
111         , field_mode(core::field_mode::empty)
112 {
113         *this = video_format_desc(video_format::invalid);
114         for(auto it = std::begin(format_descs); it != std::end(format_descs)-1; ++it)
115         {
116                 if(boost::iequals(it->name, name))
117                 {
118                         *this = *it;
119                         break;
120                 }
121         }
122 }
123
124 bool operator==(const video_format_desc& lhs, const video_format_desc& rhs)
125 {
126         return lhs.format == rhs.format;
127 }
128
129 bool operator!=(const video_format_desc& lhs, const video_format_desc& rhs)
130 {
131         return !(lhs == rhs);
132 }
133
134 std::wostream& operator<<(std::wostream& out, const video_format_desc& format_desc)
135 {
136         out << format_desc.name.c_str();
137         return out;
138 }
139
140 std::vector<int> find_audio_cadence(const boost::rational<int>& framerate, bool log_quiet)
141 {
142         static std::map<boost::rational<int>, std::vector<int>> CADENCES_BY_FRAMERATE = []
143         {
144                 std::map<boost::rational<int>, std::vector<int>> result;
145
146                 for (core::video_format format : enum_constants<core::video_format>())
147                 {
148                         core::video_format_desc desc(format);
149                         boost::rational<int> format_rate(desc.time_scale, desc.duration);
150
151                         result.insert(std::make_pair(format_rate, desc.audio_cadence));
152                 }
153
154                 return result;
155         }();
156
157         auto exact_match = CADENCES_BY_FRAMERATE.find(framerate);
158
159         if (exact_match != CADENCES_BY_FRAMERATE.end())
160                 return exact_match->second;
161
162         boost::rational<int> closest_framerate_diff     = std::numeric_limits<int>::max();
163         boost::rational<int> closest_framerate          = 0;
164
165         for (auto format_framerate : CADENCES_BY_FRAMERATE | boost::adaptors::map_keys)
166         {
167                 auto diff = boost::abs(framerate - format_framerate);
168
169                 if (diff < closest_framerate_diff)
170                 {
171                         closest_framerate_diff = diff;
172                         closest_framerate = format_framerate;
173                 }
174         }
175
176         if (log_quiet)
177                 CASPAR_LOG(debug) << "No exact audio cadence match found for framerate " << to_string(framerate)
178                         << "\nClosest match is " << to_string(closest_framerate)
179                         << "\nwhich is a " << to_string(closest_framerate_diff) << " difference.";
180         else
181                 CASPAR_LOG(warning) << "No exact audio cadence match found for framerate " << to_string(framerate)
182                         << "\nClosest match is " << to_string(closest_framerate)
183                         << "\nwhich is a " << to_string(closest_framerate_diff) << " difference.";
184
185         return CADENCES_BY_FRAMERATE[closest_framerate];
186 }
187
188
189 }}