]> git.sesse.net Git - casparcg/blob - core/frame/frame.cpp
[general] Abstracted the concept of a key only frame so that readers of const_frame...
[casparcg] / core / frame / frame.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 "frame.h"
25
26 #include <common/except.h>
27 #include <common/array.h>
28 #include <common/future.h>
29 #include <common/timer.h>
30 #include <common/memshfl.h>
31
32 #include <core/frame/frame_visitor.h>
33 #include <core/frame/pixel_format.h>
34 #include <core/frame/geometry.h>
35 #include <core/frame/audio_channel_layout.h>
36
37 #include <cstdint>
38 #include <vector>
39
40 #include <boost/lexical_cast.hpp>
41 #include <boost/thread/future.hpp>
42
43 namespace caspar { namespace core {
44
45 struct mutable_frame::impl : boost::noncopyable
46 {
47         std::vector<array<std::uint8_t>>                        buffers_;
48         core::mutable_audio_buffer                                      audio_data_;
49         const core::pixel_format_desc                           desc_;
50         const core::audio_channel_layout                        channel_layout_;
51         const void*                                                                     tag_;
52         core::frame_geometry                                            geometry_                               = frame_geometry::get_default();
53         caspar::timer                                                           since_created_timer_;
54
55         impl(
56                         std::vector<array<std::uint8_t>> buffers,
57                         mutable_audio_buffer audio_data,
58                         const void* tag,
59                         const core::pixel_format_desc& desc,
60                         const core::audio_channel_layout& channel_layout)
61                 : buffers_(std::move(buffers))
62                 , audio_data_(std::move(audio_data))
63                 , desc_(desc)
64                 , channel_layout_(channel_layout)
65                 , tag_(tag)
66         {
67                 for (auto& buffer : buffers_)
68                         if(!buffer.data())
69                                 CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("mutable_frame: null argument"));
70         }
71 };
72
73 mutable_frame::mutable_frame(
74                 std::vector<array<std::uint8_t>> image_buffers,
75                 mutable_audio_buffer audio_data,
76                 const void* tag,
77                 const core::pixel_format_desc& desc,
78                 const core::audio_channel_layout& channel_layout)
79         : impl_(new impl(std::move(image_buffers), std::move(audio_data), tag, desc, channel_layout)){}
80 mutable_frame::~mutable_frame(){}
81 mutable_frame::mutable_frame(mutable_frame&& other) : impl_(std::move(other.impl_)){}
82 mutable_frame& mutable_frame::operator=(mutable_frame&& other)
83 {
84         impl_ = std::move(other.impl_);
85         return *this;
86 }
87 void mutable_frame::swap(mutable_frame& other){impl_.swap(other.impl_);}
88 const core::pixel_format_desc& mutable_frame::pixel_format_desc() const{return impl_->desc_;}
89 const core::audio_channel_layout& mutable_frame::audio_channel_layout() const { return impl_->channel_layout_; }
90 const array<std::uint8_t>& mutable_frame::image_data(std::size_t index) const{return impl_->buffers_.at(index);}
91 const core::mutable_audio_buffer& mutable_frame::audio_data() const{return impl_->audio_data_;}
92 array<std::uint8_t>& mutable_frame::image_data(std::size_t index){return impl_->buffers_.at(index);}
93 core::mutable_audio_buffer& mutable_frame::audio_data(){return impl_->audio_data_;}
94 std::size_t mutable_frame::width() const{return impl_->desc_.planes.at(0).width;}
95 std::size_t mutable_frame::height() const{return impl_->desc_.planes.at(0).height;}
96 const void* mutable_frame::stream_tag()const{return impl_->tag_;}
97 const frame_geometry& mutable_frame::geometry() const { return impl_->geometry_; }
98 void mutable_frame::set_geometry(const frame_geometry& g) { impl_->geometry_ = g; }
99 caspar::timer mutable_frame::since_created() const { return impl_->since_created_timer_; }
100
101 const const_frame& const_frame::empty()
102 {
103         static int dummy;
104         static const_frame empty(&dummy);
105         return empty;
106 }
107
108 struct const_frame::impl : boost::noncopyable
109 {
110         mutable std::vector<std::shared_future<array<const std::uint8_t>>>      future_buffers_;
111         mutable core::audio_buffer                                                                                      audio_data_;
112         const core::pixel_format_desc                                                                           desc_;
113         const core::audio_channel_layout                                                                        channel_layout_;
114         const void*                                                                                                                     tag_;
115         core::frame_geometry                                                                                            geometry_;
116         caspar::timer                                                                                                           since_created_timer_;
117         bool                                                                                                                            should_record_age_;
118         mutable tbb::atomic<int64_t>                                                                            recorded_age_;
119         std::shared_future<array<const std::uint8_t>>                                           key_only_on_demand_;
120
121         impl(const void* tag)
122                 : audio_data_(0, 0, true, 0)
123                 , desc_(core::pixel_format::invalid)
124                 , channel_layout_(audio_channel_layout::invalid())
125                 , tag_(tag)
126                 , geometry_(frame_geometry::get_default())
127                 , should_record_age_(true)
128         {
129                 recorded_age_ = 0;
130         }
131
132         impl(
133                         std::shared_future<array<const std::uint8_t>> image,
134                         audio_buffer audio_data,
135                         const void* tag,
136                         const core::pixel_format_desc& desc,
137                         const core::audio_channel_layout& channel_layout,
138                         caspar::timer since_created_timer = caspar::timer())
139                 : audio_data_(std::move(audio_data))
140                 , desc_(desc)
141                 , channel_layout_(channel_layout)
142                 , tag_(tag)
143                 , geometry_(frame_geometry::get_default())
144                 , since_created_timer_(std::move(since_created_timer))
145                 , should_record_age_(false)
146         {
147                 if (desc.format != core::pixel_format::bgra)
148                         CASPAR_THROW_EXCEPTION(not_implemented());
149
150                 future_buffers_.push_back(image);
151
152                 key_only_on_demand_ = std::async(std::launch::deferred, [image]
153                 {
154                         auto fill       = image.get();
155                         auto key        = cache_aligned_vector<std::uint8_t>(fill.size());
156
157                         aligned_memshfl(key.data(), fill.data(), fill.size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);
158
159                         return array<const std::uint8_t>(key.data(), key.size(), false, std::move(key));
160                 }).share();
161         }
162
163         impl(mutable_frame&& other)
164                 : audio_data_(0, 0, true, 0) // Complex init done in body instead.
165                 , desc_(other.pixel_format_desc())
166                 , channel_layout_(other.audio_channel_layout())
167                 , tag_(other.stream_tag())
168                 , geometry_(other.geometry())
169                 , since_created_timer_(other.since_created())
170                 , should_record_age_(true)
171         {
172                 spl::shared_ptr<mutable_audio_buffer> shared_audio_data(new mutable_audio_buffer(std::move(other.audio_data())));
173                 // pointer returned by vector::data() should be the same after move, but just to be safe.
174                 audio_data_ = audio_buffer(shared_audio_data->data(), shared_audio_data->size(), true, std::move(shared_audio_data));
175
176                 for (std::size_t n = 0; n < desc_.planes.size(); ++n)
177                 {
178                         future_buffers_.push_back(make_ready_future<array<const std::uint8_t>>(std::move(other.image_data(n))).share());
179                 }
180
181                 recorded_age_ = -1;
182         }
183
184         array<const std::uint8_t> image_data(int index) const
185         {
186                 return tag_ != empty().stream_tag() ? future_buffers_.at(index).get() : array<const std::uint8_t>(nullptr, 0, true, 0);
187         }
188
189         spl::shared_ptr<impl> key_only() const
190         {
191                 return spl::make_shared<impl>(key_only_on_demand_, audio_data_, tag_, desc_, channel_layout_, since_created_timer_);
192         }
193
194         std::size_t width() const
195         {
196                 return tag_ != empty().stream_tag() ? desc_.planes.at(0).width : 0;
197         }
198
199         std::size_t height() const
200         {
201                 return tag_ != empty().stream_tag() ? desc_.planes.at(0).height : 0;
202         }
203
204         std::size_t size() const
205         {
206                 return tag_ != empty().stream_tag() ? desc_.planes.at(0).size : 0;
207         }
208
209         int64_t get_age_millis() const
210         {
211                 if (should_record_age_)
212                 {
213                         if (recorded_age_ == -1)
214                                 recorded_age_ = static_cast<int64_t>(since_created_timer_.elapsed() * 1000.0);
215
216                         return recorded_age_;
217                 }
218                 else
219                         return static_cast<int64_t>(since_created_timer_.elapsed() * 1000.0);
220         }
221 };
222
223 const_frame::const_frame(const void* tag) : impl_(new impl(tag)){}
224 const_frame::const_frame(
225                 std::shared_future<array<const std::uint8_t>> image,
226                 audio_buffer audio_data,
227                 const void* tag,
228                 const core::pixel_format_desc& desc,
229                 const core::audio_channel_layout& channel_layout)
230         : impl_(new impl(std::move(image), std::move(audio_data), tag, desc, channel_layout)){}
231 const_frame::const_frame(mutable_frame&& other) : impl_(new impl(std::move(other))){}
232 const_frame::~const_frame(){}
233 const_frame::const_frame(const_frame&& other) : impl_(std::move(other.impl_)){}
234 const_frame& const_frame::operator=(const_frame&& other)
235 {
236         impl_ = std::move(other.impl_);
237         return *this;
238 }
239 const_frame::const_frame(const const_frame& other) : impl_(other.impl_){}
240 const_frame& const_frame::operator=(const const_frame& other)
241 {
242         impl_ = other.impl_;
243         return *this;
244 }
245 bool const_frame::operator==(const const_frame& other){return impl_ == other.impl_;}
246 bool const_frame::operator!=(const const_frame& other){return !(*this == other);}
247 bool const_frame::operator<(const const_frame& other){return impl_ < other.impl_;}
248 bool const_frame::operator>(const const_frame& other){return impl_ > other.impl_;}
249 const core::pixel_format_desc& const_frame::pixel_format_desc()const{return impl_->desc_;}
250 const core::audio_channel_layout& const_frame::audio_channel_layout()const { return impl_->channel_layout_; }
251 array<const std::uint8_t> const_frame::image_data(int index)const{return impl_->image_data(index);}
252 const core::audio_buffer& const_frame::audio_data()const{return impl_->audio_data_;}
253 std::size_t const_frame::width()const{return impl_->width();}
254 std::size_t const_frame::height()const{return impl_->height();}
255 std::size_t const_frame::size()const{return impl_->size();}
256 const void* const_frame::stream_tag()const{return impl_->tag_;}
257 const frame_geometry& const_frame::geometry() const { return impl_->geometry_; }
258 void const_frame::set_geometry(const frame_geometry& g) { impl_->geometry_ = g; }
259 int64_t const_frame::get_age_millis() const { return impl_->get_age_millis(); }
260 const_frame const_frame::key_only() const
261 {
262         auto result             = const_frame();
263         result.impl_    = impl_->key_only();
264
265         return result;
266 }
267
268 }}