]> git.sesse.net Git - casparcg/blob - core/frame/frame.cpp
Merged INFO DELAY from 2.0
[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
31 #include <core/frame/frame_visitor.h>
32 #include <core/frame/pixel_format.h>
33 #include <core/frame/geometry.h>
34
35 #include <cstdint>
36 #include <vector>
37
38 #include <boost/lexical_cast.hpp>
39 #include <boost/thread/future.hpp>
40
41 namespace caspar { namespace core {
42                 
43 struct mutable_frame::impl : boost::noncopyable
44 {                       
45         std::vector<array<std::uint8_t>>                        buffers_;
46         core::audio_buffer                                                      audio_data_;
47         const core::pixel_format_desc                           desc_;
48         const void*                                                                     tag_;
49         core::frame_geometry                                            geometry_                               = frame_geometry::get_default();
50         caspar::timer                                                           since_created_timer_;
51         
52         impl(std::vector<array<std::uint8_t>> buffers, audio_buffer audio_buffer, const void* tag, const core::pixel_format_desc& desc) 
53                 : buffers_(std::move(buffers))
54                 , audio_data_(std::move(audio_buffer))
55                 , desc_(desc)
56                 , tag_(tag)
57         {
58                 for (auto& buffer : buffers_)
59                         if(!buffer.data())
60                                 CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("mutable_frame: null argument"));
61         }
62 };
63         
64 mutable_frame::mutable_frame(std::vector<array<std::uint8_t>> image_buffers, audio_buffer audio_buffer, const void* tag, const core::pixel_format_desc& desc) 
65         : impl_(new impl(std::move(image_buffers), std::move(audio_buffer), tag, desc)){}
66 mutable_frame::~mutable_frame(){}
67 mutable_frame::mutable_frame(mutable_frame&& other) : impl_(std::move(other.impl_)){}
68 mutable_frame& mutable_frame::operator=(mutable_frame&& other)
69 {
70         impl_ = std::move(other.impl_);
71         return *this;
72 }
73 void mutable_frame::swap(mutable_frame& other){impl_.swap(other.impl_);}
74 const core::pixel_format_desc& mutable_frame::pixel_format_desc() const{return impl_->desc_;}
75 const array<std::uint8_t>& mutable_frame::image_data(std::size_t index) const{return impl_->buffers_.at(index);}
76 const core::audio_buffer& mutable_frame::audio_data() const{return impl_->audio_data_;}
77 array<std::uint8_t>& mutable_frame::image_data(std::size_t index){return impl_->buffers_.at(index);}
78 core::audio_buffer& mutable_frame::audio_data(){return impl_->audio_data_;}
79 std::size_t mutable_frame::width() const{return impl_->desc_.planes.at(0).width;}
80 std::size_t mutable_frame::height() const{return impl_->desc_.planes.at(0).height;}                                             
81 const void* mutable_frame::stream_tag()const{return impl_->tag_;}                               
82 const void* mutable_frame::data_tag()const{return impl_.get();} 
83 const frame_geometry& mutable_frame::geometry() const { return impl_->geometry_; }
84 void mutable_frame::set_geometry(const frame_geometry& g) { impl_->geometry_ = g; }
85 caspar::timer mutable_frame::since_created() const { return impl_->since_created_timer_; }
86
87 const const_frame& const_frame::empty()
88 {
89         static int dummy;
90         static const_frame empty(&dummy);
91         return empty;
92 }
93
94 struct const_frame::impl : boost::noncopyable
95 {                       
96         mutable std::vector<std::shared_future<array<const std::uint8_t>>>      future_buffers_;
97         core::audio_buffer                                                                                                      audio_data_;
98         const core::pixel_format_desc                                                                           desc_;
99         const void*                                                                                                                     tag_;
100         core::frame_geometry                                                                                            geometry_;
101         caspar::timer                                                                                                           since_created_timer_;
102         bool                                                                                                                            should_record_age_;
103         mutable tbb::atomic<int64_t>                                                                            recorded_age_;
104
105         impl(const void* tag)
106                 : desc_(core::pixel_format::invalid)
107                 , tag_(tag)     
108                 , geometry_(frame_geometry::get_default())
109                 , should_record_age_(true)
110         {
111                 recorded_age_ = 0;
112         }
113         
114         impl(std::shared_future<array<const std::uint8_t>> image, audio_buffer audio_buffer, const void* tag, const core::pixel_format_desc& desc) 
115                 : audio_data_(std::move(audio_buffer))
116                 , desc_(desc)
117                 , tag_(tag)
118                 , geometry_(frame_geometry::get_default())
119                 , should_record_age_(false)
120         {
121                 if (desc.format != core::pixel_format::bgra)
122                         CASPAR_THROW_EXCEPTION(not_implemented());
123                 
124                 future_buffers_.push_back(std::move(image));
125         }
126
127         impl(mutable_frame&& other)
128                 : audio_data_(other.audio_data())
129                 , desc_(other.pixel_format_desc())
130                 , tag_(other.stream_tag())
131                 , geometry_(other.geometry())
132                 , since_created_timer_(other.since_created())
133                 , should_record_age_(true)
134         {
135                 for (std::size_t n = 0; n < desc_.planes.size(); ++n)
136                 {
137                         future_buffers_.push_back(make_ready_future<array<const std::uint8_t>>(std::move(other.image_data(n))).share());
138                 }
139
140                 recorded_age_ = -1;
141         }
142
143         array<const std::uint8_t> image_data(int index) const
144         {
145                 return tag_ != empty().stream_tag() ? future_buffers_.at(index).get() : array<const std::uint8_t>(nullptr, 0, true, 0);
146         }
147
148         std::size_t width() const
149         {
150                 return tag_ != empty().stream_tag() ? desc_.planes.at(0).width : 0;
151         }
152
153         std::size_t height() const
154         {
155                 return tag_ != empty().stream_tag() ? desc_.planes.at(0).height : 0;
156         }
157
158         std::size_t size() const
159         {
160                 return tag_ != empty().stream_tag() ? desc_.planes.at(0).size : 0;
161         }
162
163         int64_t get_age_millis() const
164         {
165                 if (should_record_age_)
166                 {
167                         if (recorded_age_ == -1)
168                                 recorded_age_ = static_cast<int64_t>(since_created_timer_.elapsed() * 1000.0);
169
170                         return recorded_age_;
171                 }
172                 else
173                         return static_cast<int64_t>(since_created_timer_.elapsed() * 1000.0);
174         }
175 };
176         
177 const_frame::const_frame(const void* tag) : impl_(new impl(tag)){}
178 const_frame::const_frame(std::shared_future<array<const std::uint8_t>> image, audio_buffer audio_buffer, const void* tag, const core::pixel_format_desc& desc) 
179         : impl_(new impl(std::move(image), std::move(audio_buffer), tag, desc)){}
180 const_frame::const_frame(mutable_frame&& other) : impl_(new impl(std::move(other))){}
181 const_frame::~const_frame(){}
182 const_frame::const_frame(const_frame&& other) : impl_(std::move(other.impl_)){}
183 const_frame& const_frame::operator=(const_frame&& other)
184 {
185         impl_ = std::move(other.impl_);
186         return *this;
187 }
188 const_frame::const_frame(const const_frame& other) : impl_(other.impl_){}
189 const_frame& const_frame::operator=(const const_frame& other)
190 {
191         impl_ = other.impl_;
192         return *this;
193 }
194 bool const_frame::operator==(const const_frame& other){return impl_ == other.impl_;}
195 bool const_frame::operator!=(const const_frame& other){return !(*this == other);}
196 bool const_frame::operator<(const const_frame& other){return impl_ < other.impl_;}
197 bool const_frame::operator>(const const_frame& other){return impl_ > other.impl_;}
198 const core::pixel_format_desc& const_frame::pixel_format_desc()const{return impl_->desc_;}
199 array<const std::uint8_t> const_frame::image_data(int index)const{return impl_->image_data(index);}
200 const core::audio_buffer& const_frame::audio_data()const{return impl_->audio_data_;}
201 std::size_t const_frame::width()const{return impl_->width();}
202 std::size_t const_frame::height()const{return impl_->height();} 
203 std::size_t const_frame::size()const{return impl_->size();}                                             
204 const void* const_frame::stream_tag()const{return impl_->tag_;}                         
205 const void* const_frame::data_tag()const{return impl_.get();}   
206 const frame_geometry& const_frame::geometry() const { return impl_->geometry_; }
207 void const_frame::set_geometry(const frame_geometry& g) { impl_->geometry_ = g; }
208 int64_t const_frame::get_age_millis() const { return impl_->get_age_millis(); }
209
210 }}