]> git.sesse.net Git - casparcg/blob - core/frame/frame.cpp
2.1.0: Put "array" into its own file, common/memory/array.
[casparcg] / core / frame / frame.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../stdafx.h"\r
23 \r
24 #include "frame.h"\r
25 \r
26 #include <common/except.h>\r
27 #include <common/memory/array.h>\r
28 \r
29 #include <core/frame/frame_visitor.h>\r
30 #include <core/frame/pixel_format.h>\r
31 \r
32 #include <boost/lexical_cast.hpp>\r
33 #include <boost/thread/future.hpp>\r
34 \r
35 namespace caspar { namespace core {\r
36                 \r
37 const const_frame& const_frame::empty()\r
38 {\r
39         static int dummy;\r
40         static const_frame empty(&dummy);\r
41         return empty;\r
42 }\r
43 \r
44 struct const_frame::impl : boost::noncopyable\r
45 {                       \r
46         mutable boost::shared_future<const_array>       future_buffer_;\r
47         int                                                                                     id_;\r
48         core::audio_buffer                                                      audio_data_;\r
49         const core::pixel_format_desc                           desc_;\r
50         const void*                                                                     tag_;\r
51         double                                                                          frame_rate_;\r
52         core::field_mode                                                        field_mode_;\r
53 \r
54         impl(const void* tag)\r
55                 : desc_(core::pixel_format::invalid)\r
56                 , tag_(tag)     \r
57                 , id_(0)\r
58                 , field_mode_(core::field_mode::empty)\r
59         {\r
60         }\r
61         \r
62         impl(boost::shared_future<const_array> image, audio_buffer audio_buffer, const void* tag, const core::pixel_format_desc& desc, double frame_rate, core::field_mode field_mode) \r
63                 : future_buffer_(std::move(image))\r
64                 , audio_data_(std::move(audio_buffer))\r
65                 , desc_(desc)\r
66                 , tag_(tag)\r
67                 , id_(reinterpret_cast<int>(this))\r
68                 , frame_rate_(frame_rate)\r
69                 , field_mode_(field_mode)\r
70         {\r
71                 if(desc.format != core::pixel_format::bgra)\r
72                         BOOST_THROW_EXCEPTION(not_implemented());\r
73         }\r
74 \r
75         const_array image_data() const\r
76         {\r
77                 return tag_ != empty().tag() ? future_buffer_.get() : const_array(nullptr, 0, 0);\r
78         }\r
79 \r
80         std::size_t width() const\r
81         {\r
82                 return tag_ != empty().tag() ? desc_.planes.at(0).width : 0;\r
83         }\r
84 \r
85         std::size_t height() const\r
86         {\r
87                 return tag_ != empty().tag() ? desc_.planes.at(0).height : 0;\r
88         }\r
89 \r
90         std::size_t size() const\r
91         {\r
92                 return tag_ != empty().tag() ? desc_.planes.at(0).size : 0;\r
93         }\r
94 \r
95         bool operator==(const impl& other)\r
96         {\r
97                 return tag_ == other.tag_ && id_ == other.id_;\r
98         }\r
99 };\r
100         \r
101 const_frame::const_frame(const void* tag) : impl_(new impl(tag)){}\r
102 const_frame::const_frame(boost::shared_future<const_array> image, audio_buffer audio_buffer, const void* tag, const core::pixel_format_desc& desc, double frame_rate, core::field_mode field_mode) \r
103         : impl_(new impl(std::move(image), std::move(audio_buffer), tag, desc, frame_rate, field_mode)){}\r
104 const_frame::~const_frame(){}\r
105 const_frame::const_frame(const_frame&& other) : impl_(std::move(other.impl_)){}\r
106 const_frame& const_frame::operator=(const_frame&& other)\r
107 {\r
108         impl_ = std::move(other.impl_);\r
109         return *this;\r
110 }\r
111 const_frame::const_frame(const const_frame& other) : impl_(other.impl_){}\r
112 const_frame& const_frame::operator=(const const_frame& other)\r
113 {\r
114         impl_ = other.impl_;\r
115         return *this;\r
116 }\r
117 bool const_frame::operator==(const const_frame& other){return *impl_ == *other.impl_;}\r
118 bool const_frame::operator!=(const const_frame& other){return !(*this == other);}\r
119 const core::pixel_format_desc& const_frame::pixel_format_desc()const{return impl_->desc_;}\r
120 const_array const_frame::image_data()const{return impl_->image_data();}\r
121 const core::audio_buffer& const_frame::audio_data()const{return impl_->audio_data_;}\r
122 double const_frame::frame_rate()const{return impl_->frame_rate_;}\r
123 core::field_mode const_frame::field_mode()const{return impl_->field_mode_;}\r
124 std::size_t const_frame::width()const{return impl_->width();}\r
125 std::size_t const_frame::height()const{return impl_->height();} \r
126 std::size_t const_frame::size()const{return impl_->size();}                                             \r
127 const void* const_frame::tag()const{return impl_->tag_;}        \r
128 \r
129 struct mutable_frame::impl : boost::noncopyable\r
130 {                       \r
131         std::vector<mutable_array>                                      buffers_;\r
132         core::audio_buffer                                                      audio_data_;\r
133         const core::pixel_format_desc                           desc_;\r
134         const void*                                                                     tag_;\r
135         double                                                                          frame_rate_;\r
136         core::field_mode                                                        field_mode_;\r
137         \r
138         impl(std::vector<mutable_array> buffers, audio_buffer audio_buffer, const void* tag, const core::pixel_format_desc& desc, double frame_rate, core::field_mode field_mode) \r
139                 : buffers_(std::move(buffers))\r
140                 , audio_data_(std::move(audio_buffer))\r
141                 , desc_(desc)\r
142                 , tag_(tag)\r
143                 , frame_rate_(frame_rate)\r
144                 , field_mode_(field_mode)\r
145         {\r
146                 BOOST_FOREACH(auto& buffer, buffers_)\r
147                         if(!buffer.data())\r
148                                 BOOST_THROW_EXCEPTION(invalid_argument() << msg_info("mutable_frame: null argument"));\r
149         }\r
150 };\r
151         \r
152 mutable_frame::mutable_frame(std::vector<mutable_array> image_buffers, audio_buffer audio_buffer, const void* tag, const core::pixel_format_desc& desc, double frame_rate, core::field_mode field_mode) \r
153         : impl_(new impl(std::move(image_buffers), std::move(audio_buffer), tag, desc, frame_rate, field_mode)){}\r
154 mutable_frame::~mutable_frame(){}\r
155 mutable_frame::mutable_frame(mutable_frame&& other) : impl_(std::move(other.impl_)){}\r
156 mutable_frame& mutable_frame::operator=(mutable_frame&& other)\r
157 {\r
158         impl_ = std::move(other.impl_);\r
159         return *this;\r
160 }\r
161 void mutable_frame::swap(mutable_frame& other){impl_.swap(other.impl_);}\r
162 const core::pixel_format_desc& mutable_frame::pixel_format_desc() const{return impl_->desc_;}\r
163 const mutable_array& mutable_frame::image_data(std::size_t index) const{return impl_->buffers_.at(index);}\r
164 const core::audio_buffer& mutable_frame::audio_data() const{return impl_->audio_data_;}\r
165 mutable_array& mutable_frame::image_data(std::size_t index){return impl_->buffers_.at(index);}\r
166 core::audio_buffer& mutable_frame::audio_data(){return impl_->audio_data_;}\r
167 double mutable_frame::frame_rate() const{return impl_->frame_rate_;}\r
168 core::field_mode mutable_frame::field_mode() const{return impl_->field_mode_;}\r
169 std::size_t mutable_frame::width() const{return impl_->desc_.planes.at(0).width;}\r
170 std::size_t mutable_frame::height() const{return impl_->desc_.planes.at(0).height;}                                             \r
171 const void* mutable_frame::tag() const{return impl_->tag_;}     \r
172 \r
173 }}