]> git.sesse.net Git - casparcg/blob - core/mixer/read_frame.cpp
2.0. Refactoring.
[casparcg] / core / mixer / read_frame.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\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 */\r
20 #include "../stdafx.h"\r
21 \r
22 #include "read_frame.h"\r
23 \r
24 #include "gpu/fence.h"\r
25 #include "gpu/host_buffer.h"    \r
26 #include "gpu/ogl_device.h"\r
27 \r
28 namespace caspar { namespace core {\r
29                                                                                                                                                                                                                                                                                                                         \r
30 struct read_frame::implementation : boost::noncopyable\r
31 {\r
32         ogl_device&                                             ogl_;\r
33         safe_ptr<host_buffer>                   image_data_;\r
34         std::vector<int16_t>                    audio_data_;\r
35 \r
36 public:\r
37         implementation(ogl_device& ogl, safe_ptr<host_buffer>&& image_data, std::vector<int16_t>&& audio_data) \r
38                 : ogl_(ogl)\r
39                 , image_data_(std::move(image_data))\r
40                 , audio_data_(std::move(audio_data)){}  \r
41         \r
42         const boost::iterator_range<const uint8_t*> image_data()\r
43         {\r
44                 if(!image_data_->data())\r
45                 {\r
46                         image_data_->wait(ogl_);\r
47                         ogl_.invoke([=]{image_data_->map();}, high_priority);\r
48                 }\r
49 \r
50                 auto ptr = static_cast<const uint8_t*>(image_data_->data());\r
51                 return boost::iterator_range<const uint8_t*>(ptr, ptr + image_data_->size());\r
52         }\r
53         const boost::iterator_range<const int16_t*> audio_data()\r
54         {\r
55                 return boost::iterator_range<const int16_t*>(audio_data_.data(), audio_data_.data() + audio_data_.size());\r
56         }\r
57 };\r
58 \r
59 read_frame::read_frame(ogl_device& ogl, safe_ptr<host_buffer>&& image_data, std::vector<int16_t>&& audio_data) \r
60         : impl_(new implementation(ogl, std::move(image_data), std::move(audio_data))){}\r
61 read_frame::read_frame(){}\r
62 const boost::iterator_range<const uint8_t*> read_frame::image_data()\r
63 {\r
64         return impl_ ? impl_->image_data() : boost::iterator_range<const uint8_t*>();\r
65 }\r
66 \r
67 const boost::iterator_range<const int16_t*> read_frame::audio_data()\r
68 {\r
69         return impl_ ? impl_->audio_data() : boost::iterator_range<const int16_t*>();\r
70 }\r
71 \r
72 size_t read_frame::image_size() const{return impl_ ? impl_->image_data_->size() : 0;}\r
73 \r
74 //#include <tbb/scalable_allocator.h>\r
75 //#include <tbb/parallel_for.h>\r
76 //#include <tbb/enumerable_thread_specific.h>\r
77 //#define               CACHED_BUFFER_SIZE      4096    \r
78 //typedef               unsigned int            UINT;\r
79 //\r
80 //struct cache_buffer\r
81 //{\r
82 //      cache_buffer() : data(scalable_aligned_malloc(CACHED_BUFFER_SIZE, 64)){}\r
83 //      ~cache_buffer() {scalable_aligned_free(data);}\r
84 //      void* data;\r
85 //};\r
86 //\r
87 //void  CopyFrame( void * pSrc, void * pDest, UINT width, UINT height, UINT pitch );\r
88 //\r
89 //void* copy_frame(void* dest, const safe_ptr<read_frame>& frame)\r
90 //{\r
91 //      auto src                = frame->image_data().begin();\r
92 //      auto height             = 720;\r
93 //      auto width4             = frame->image_data().size()/height;\r
94 //\r
95 //      CASPAR_ASSERT(frame->image_data().size() % height == 0);\r
96 //                      \r
97 //      tbb::affinity_partitioner ap;\r
98 //      tbb::parallel_for(tbb::blocked_range<size_t>(0, height), [&](tbb::blocked_range<size_t>& r)\r
99 //      {\r
100 //              CopyFrame(const_cast<uint8_t*>(src)+r.begin()*width4, reinterpret_cast<uint8_t*>(dest)+r.begin()*width4, width4, r.size(), width4);\r
101 //      }, ap);\r
102 //\r
103 //      return dest;\r
104 //}\r
105 //\r
106 ////  CopyFrame( )\r
107 ////\r
108 ////  COPIES VIDEO FRAMES FROM USWC MEMORY TO WB SYSTEM MEMORY VIA CACHED BUFFER\r
109 ////    ASSUMES PITCH IS A MULTIPLE OF 64B CACHE LINE SIZE, WIDTH MAY NOT BE\r
110 //// http://software.intel.com/en-us/articles/copying-accelerated-video-decode-frame-buffers/\r
111 //void CopyFrame( void * pSrc, void * pDest, UINT width, UINT height, UINT pitch )\r
112 //{\r
113 //      tbb::enumerable_thread_specific<cache_buffer> cache_buffers;\r
114 //\r
115 //      void *          pCacheBlock = cache_buffers.local().data;\r
116 //\r
117 //      __m128i         x0, x1, x2, x3;\r
118 //      __m128i         *pLoad;\r
119 //      __m128i         *pStore;\r
120 //      __m128i         *pCache;\r
121 //      UINT            x, y, yLoad, yStore;\r
122 //      UINT            rowsPerBlock;\r
123 //      UINT            width64;\r
124 //      UINT            extraPitch;     \r
125 //\r
126 //      rowsPerBlock = CACHED_BUFFER_SIZE / pitch;\r
127 //      width64 = (width + 63) & ~0x03f;\r
128 //      extraPitch = (pitch - width64) / 16;\r
129 //\r
130 //      pLoad  = (__m128i *)pSrc;\r
131 //      pStore = (__m128i *)pDest;\r
132 //\r
133 //      //  COPY THROUGH 4KB CACHED BUFFER\r
134 //      for( y = 0; y < height; y += rowsPerBlock  )\r
135 //      {\r
136 //              //  ROWS LEFT TO COPY AT END\r
137 //              if( y + rowsPerBlock > height )\r
138 //                      rowsPerBlock = height - y;\r
139 //\r
140 //              pCache = (__m128i *)pCacheBlock;\r
141 //\r
142 //              _mm_mfence();                           \r
143 //              \r
144 //              // LOAD ROWS OF PITCH WIDTH INTO CACHED BLOCK\r
145 //              for( yLoad = 0; yLoad < rowsPerBlock; yLoad++ )\r
146 //              {\r
147 //                      // COPY A ROW, CACHE LINE AT A TIME\r
148 //                      for( x = 0; x < pitch; x +=64 )\r
149 //                      {\r
150 //                              x0 = _mm_stream_load_si128( pLoad +0 );\r
151 //                              x1 = _mm_stream_load_si128( pLoad +1 );\r
152 //                              x2 = _mm_stream_load_si128( pLoad +2 );\r
153 //                              x3 = _mm_stream_load_si128( pLoad +3 );\r
154 //\r
155 //                              _mm_store_si128( pCache +0,     x0 );\r
156 //                              _mm_store_si128( pCache +1, x1 );\r
157 //                              _mm_store_si128( pCache +2, x2 );\r
158 //                              _mm_store_si128( pCache +3, x3 );\r
159 //\r
160 //                              pCache += 4;\r
161 //                              pLoad += 4;\r
162 //                      }\r
163 //              }\r
164 //\r
165 //              _mm_mfence();\r
166 //\r
167 //              pCache = (__m128i *)pCacheBlock;\r
168 //\r
169 //              // STORE ROWS OF FRAME WIDTH FROM CACHED BLOCK\r
170 //              for( yStore = 0; yStore < rowsPerBlock; yStore++ )\r
171 //              {\r
172 //                      // copy a row, cache line at a time\r
173 //                      for( x = 0; x < width64; x +=64 )\r
174 //                      {\r
175 //                              x0 = _mm_load_si128( pCache );\r
176 //                              x1 = _mm_load_si128( pCache +1 );\r
177 //                              x2 = _mm_load_si128( pCache +2 );\r
178 //                              x3 = _mm_load_si128( pCache +3 );\r
179 //\r
180 //                              _mm_stream_si128( pStore,       x0 );\r
181 //                              _mm_stream_si128( pStore +1, x1 );\r
182 //                              _mm_stream_si128( pStore +2, x2 );\r
183 //                              _mm_stream_si128( pStore +3, x3 );\r
184 //\r
185 //                              pCache += 4;\r
186 //                              pStore += 4;\r
187 //                      }\r
188 //\r
189 //                      pCache += extraPitch;\r
190 //                      pStore += extraPitch;\r
191 //              }\r
192 //      }\r
193 //}\r
194 \r
195 }}