]> git.sesse.net Git - casparcg/blob - core/mixer/write_frame.cpp
Added support for more than 2 audio channels
[casparcg] / core / mixer / write_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 "write_frame.h"\r
25 \r
26 #include "gpu/ogl_device.h"\r
27 #include "gpu/host_buffer.h"\r
28 #include "gpu/device_buffer.h"\r
29 \r
30 #include <core/producer/frame/frame_visitor.h>\r
31 #include <core/producer/frame/pixel_format.h>\r
32 #include <core/mixer/audio/audio_util.h>\r
33 \r
34 #include <boost/lexical_cast.hpp>\r
35 \r
36 namespace caspar { namespace core {\r
37                                                                                                                                                                                                                                                                                                                         \r
38 struct write_frame::implementation\r
39 {                               \r
40         std::shared_ptr<ogl_device>                                     ogl_;\r
41         std::vector<std::shared_ptr<host_buffer>>       buffers_;\r
42         std::vector<safe_ptr<device_buffer>>            textures_;\r
43         audio_buffer                                                            audio_data_;\r
44         const core::pixel_format_desc                           desc_;\r
45         const channel_layout                                            channel_layout_;\r
46         const void*                                                                     tag_;\r
47         core::field_mode::type                                          mode_;\r
48 \r
49         implementation(const void* tag, const channel_layout& channel_layout)\r
50                 : channel_layout_(channel_layout)\r
51                 , tag_(tag)\r
52         {\r
53         }\r
54 \r
55         implementation(const safe_ptr<ogl_device>& ogl, const void* tag, const core::pixel_format_desc& desc, const channel_layout& channel_layout) \r
56                 : ogl_(ogl)\r
57                 , desc_(desc)\r
58                 , channel_layout_(channel_layout)\r
59                 , tag_(tag)\r
60                 , mode_(core::field_mode::progressive)\r
61         {\r
62                 std::transform(desc.planes.begin(), desc.planes.end(), std::back_inserter(buffers_), [&](const core::pixel_format_desc::plane& plane)\r
63                 {\r
64                         return ogl_->create_host_buffer(plane.size, host_buffer::write_only);\r
65                 });\r
66                 std::transform(desc.planes.begin(), desc.planes.end(), std::back_inserter(textures_), [&](const core::pixel_format_desc::plane& plane)\r
67                 {\r
68                         return ogl_->create_device_buffer(plane.width, plane.height, plane.channels);   \r
69                 });\r
70         }\r
71                         \r
72         void accept(write_frame& self, core::frame_visitor& visitor)\r
73         {\r
74                 visitor.begin(self);\r
75                 visitor.visit(self);\r
76                 visitor.end();\r
77         }\r
78 \r
79         boost::iterator_range<uint8_t*> image_data(size_t index)\r
80         {\r
81                 if(index >= buffers_.size() || !buffers_[index]->data())\r
82                         return boost::iterator_range<uint8_t*>();\r
83                 auto ptr = static_cast<uint8_t*>(buffers_[index]->data());\r
84                 return boost::iterator_range<uint8_t*>(ptr, ptr+buffers_[index]->size());\r
85         }\r
86         \r
87         void commit()\r
88         {\r
89                 for(size_t n = 0; n < buffers_.size(); ++n)\r
90                         commit(n);\r
91         }\r
92 \r
93         void commit(size_t plane_index)\r
94         {\r
95                 if(plane_index >= buffers_.size())\r
96                         return;\r
97                                 \r
98                 auto buffer = std::move(buffers_[plane_index]); // Release buffer once done.\r
99 \r
100                 if(!buffer)\r
101                         return;\r
102 \r
103                 auto texture = textures_.at(plane_index);\r
104                 \r
105                 ogl_->begin_invoke([=]\r
106                 {                       \r
107                         buffer->unmap();\r
108                         buffer->bind();\r
109                         texture->begin_read();\r
110                         buffer->unbind();\r
111                 }, high_priority);\r
112         }\r
113 };\r
114         \r
115 write_frame::write_frame(const void* tag, const channel_layout& channel_layout)\r
116         : impl_(new implementation(tag, channel_layout))\r
117 {\r
118 }\r
119 write_frame::write_frame(\r
120                 const safe_ptr<ogl_device>& ogl,\r
121                 const void* tag,\r
122                 const core::pixel_format_desc& desc,\r
123                 const channel_layout& channel_layout)\r
124         : impl_(new implementation(ogl, tag, desc, channel_layout))\r
125 {\r
126 }\r
127 write_frame::write_frame(const write_frame& other) : impl_(new implementation(*other.impl_)){}\r
128 write_frame::write_frame(write_frame&& other) : impl_(std::move(other.impl_)){}\r
129 write_frame& write_frame::operator=(const write_frame& other)\r
130 {\r
131         basic_frame temp(other);\r
132         temp.swap(*this);\r
133         return *this;\r
134 }\r
135 write_frame& write_frame::operator=(write_frame&& other)\r
136 {\r
137         write_frame temp(std::move(other));\r
138         temp.swap(*this);\r
139         return *this;\r
140 }\r
141 void write_frame::swap(write_frame& other){impl_.swap(other.impl_);}\r
142 \r
143 boost::iterator_range<uint8_t*> write_frame::image_data(size_t index){return impl_->image_data(index);}\r
144 audio_buffer& write_frame::audio_data() { return impl_->audio_data_; }\r
145 const void* write_frame::tag() const {return impl_->tag_;}\r
146 const core::pixel_format_desc& write_frame::get_pixel_format_desc() const{return impl_->desc_;}\r
147 const channel_layout& write_frame::get_channel_layout() const{return impl_->channel_layout_;}\r
148 multichannel_view<int32_t, audio_buffer::iterator> write_frame::get_multichannel_view()\r
149 {\r
150         return make_multichannel_view<int32_t>(impl_->audio_data_.begin(), impl_->audio_data_.end(), impl_->channel_layout_);\r
151 }\r
152 const std::vector<safe_ptr<device_buffer>>& write_frame::get_textures() const{return impl_->textures_;}\r
153 void write_frame::commit(size_t plane_index){impl_->commit(plane_index);}\r
154 void write_frame::commit(){impl_->commit();}\r
155 void write_frame::set_type(const field_mode::type& mode){impl_->mode_ = mode;}\r
156 core::field_mode::type write_frame::get_type() const{return impl_->mode_;}\r
157 void write_frame::accept(core::frame_visitor& visitor){impl_->accept(*this, visitor);}\r
158 \r
159 }}