2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
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.
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.
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/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../StdAfx.h"
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>
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>
40 #include <boost/lexical_cast.hpp>
41 #include <boost/thread/future.hpp>
43 namespace caspar { namespace core {
45 struct mutable_frame::impl : boost::noncopyable
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_;
52 core::frame_geometry geometry_ = frame_geometry::get_default();
53 caspar::timer since_created_timer_;
56 std::vector<array<std::uint8_t>> buffers,
57 mutable_audio_buffer audio_data,
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))
64 , channel_layout_(channel_layout)
67 for (auto& buffer : buffers_)
69 CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("mutable_frame: null argument"));
73 mutable_frame::mutable_frame(
74 std::vector<array<std::uint8_t>> image_buffers,
75 mutable_audio_buffer audio_data,
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)
84 impl_ = std::move(other.impl_);
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_; }
101 const const_frame& const_frame::empty()
104 static const_frame empty(&dummy);
108 struct const_frame::impl : boost::noncopyable
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_;
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_;
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())
126 , geometry_(frame_geometry::get_default())
127 , should_record_age_(true)
133 std::shared_future<array<const std::uint8_t>> image,
134 audio_buffer audio_data,
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))
141 , channel_layout_(channel_layout)
143 , geometry_(frame_geometry::get_default())
144 , since_created_timer_(std::move(since_created_timer))
145 , should_record_age_(false)
147 if (desc.format != core::pixel_format::bgra)
148 CASPAR_THROW_EXCEPTION(not_implemented());
150 future_buffers_.push_back(image);
152 key_only_on_demand_ = std::async(std::launch::deferred, [image]
154 auto fill = image.get();
155 auto key = cache_aligned_vector<std::uint8_t>(fill.size());
157 aligned_memshfl(key.data(), fill.data(), fill.size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);
159 return array<const std::uint8_t>(key.data(), key.size(), false, std::move(key));
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)
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));
176 for (std::size_t n = 0; n < desc_.planes.size(); ++n)
178 future_buffers_.push_back(make_ready_future<array<const std::uint8_t>>(std::move(other.image_data(n))).share());
184 array<const std::uint8_t> image_data(int index) const
186 return tag_ != empty().stream_tag() ? future_buffers_.at(index).get() : array<const std::uint8_t>(nullptr, 0, true, 0);
189 spl::shared_ptr<impl> key_only() const
191 return spl::make_shared<impl>(key_only_on_demand_, audio_data_, tag_, desc_, channel_layout_, since_created_timer_);
194 std::size_t width() const
196 return tag_ != empty().stream_tag() ? desc_.planes.at(0).width : 0;
199 std::size_t height() const
201 return tag_ != empty().stream_tag() ? desc_.planes.at(0).height : 0;
204 std::size_t size() const
206 return tag_ != empty().stream_tag() ? desc_.planes.at(0).size : 0;
209 int64_t get_age_millis() const
211 if (should_record_age_)
213 if (recorded_age_ == -1)
214 recorded_age_ = static_cast<int64_t>(since_created_timer_.elapsed() * 1000.0);
216 return recorded_age_;
219 return static_cast<int64_t>(since_created_timer_.elapsed() * 1000.0);
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,
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)
236 impl_ = std::move(other.impl_);
239 const_frame::const_frame(const const_frame& other) : impl_(other.impl_){}
240 const_frame& const_frame::operator=(const const_frame& other)
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
262 auto result = const_frame();
263 result.impl_ = impl_->key_only();