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