2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\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
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
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
20 #include "../stdafx.h"
\r
22 #include "read_frame.h"
\r
24 #include "gpu/fence.h"
\r
25 #include "gpu/host_buffer.h"
\r
26 #include "gpu/ogl_device.h"
\r
28 #include <tbb/mutex.h>
\r
30 namespace caspar { namespace core {
\r
32 struct read_frame::implementation : boost::noncopyable
\r
36 safe_ptr<host_buffer> image_data_;
\r
38 std::vector<int16_t> audio_data_;
\r
41 implementation(ogl_device& ogl, size_t size, safe_ptr<host_buffer>&& image_data, std::vector<int16_t>&& audio_data)
\r
44 , image_data_(std::move(image_data))
\r
45 , audio_data_(std::move(audio_data)){}
\r
47 const boost::iterator_range<const uint8_t*> image_data()
\r
50 tbb::mutex::scoped_lock lock(mutex_);
\r
52 if(!image_data_->data())
\r
54 image_data_.get()->wait(ogl_);
\r
55 ogl_.invoke([=]{image_data_.get()->map();}, high_priority);
\r
59 auto ptr = static_cast<const uint8_t*>(image_data_->data());
\r
60 return boost::iterator_range<const uint8_t*>(ptr, ptr + image_data_->size());
\r
62 const boost::iterator_range<const int16_t*> audio_data()
\r
64 return boost::iterator_range<const int16_t*>(audio_data_.data(), audio_data_.data() + audio_data_.size());
\r
68 read_frame::read_frame(ogl_device& ogl, size_t size, safe_ptr<host_buffer>&& image_data, std::vector<int16_t>&& audio_data)
\r
69 : impl_(new implementation(ogl, size, std::move(image_data), std::move(audio_data))){}
\r
70 read_frame::read_frame(){}
\r
71 const boost::iterator_range<const uint8_t*> read_frame::image_data()
\r
73 return impl_ ? impl_->image_data() : boost::iterator_range<const uint8_t*>();
\r
76 const boost::iterator_range<const int16_t*> read_frame::audio_data()
\r
78 return impl_ ? impl_->audio_data() : boost::iterator_range<const int16_t*>();
\r
81 size_t read_frame::image_size() const{return impl_ ? impl_->size_ : 0;}
\r
83 //#include <tbb/scalable_allocator.h>
\r
84 //#include <tbb/parallel_for.h>
\r
85 //#include <tbb/enumerable_thread_specific.h>
\r
86 //#define CACHED_BUFFER_SIZE 4096
\r
87 //typedef unsigned int UINT;
\r
89 //struct cache_buffer
\r
91 // cache_buffer() : data(scalable_aligned_malloc(CACHED_BUFFER_SIZE, 64)){}
\r
92 // ~cache_buffer() {scalable_aligned_free(data);}
\r
96 //void CopyFrame( void * pSrc, void * pDest, UINT width, UINT height, UINT pitch );
\r
98 //void* copy_frame(void* dest, const safe_ptr<read_frame>& frame)
\r
100 // auto src = frame->image_data().begin();
\r
101 // auto height = 720;
\r
102 // auto width4 = frame->image_data().size()/height;
\r
104 // CASPAR_ASSERT(frame->image_data().size() % height == 0);
\r
106 // tbb::affinity_partitioner ap;
\r
107 // tbb::parallel_for(tbb::blocked_range<size_t>(0, height), [&](tbb::blocked_range<size_t>& r)
\r
109 // CopyFrame(const_cast<uint8_t*>(src)+r.begin()*width4, reinterpret_cast<uint8_t*>(dest)+r.begin()*width4, width4, r.size(), width4);
\r
117 //// COPIES VIDEO FRAMES FROM USWC MEMORY TO WB SYSTEM MEMORY VIA CACHED BUFFER
\r
118 //// ASSUMES PITCH IS A MULTIPLE OF 64B CACHE LINE SIZE, WIDTH MAY NOT BE
\r
119 //// http://software.intel.com/en-us/articles/copying-accelerated-video-decode-frame-buffers/
\r
120 //void CopyFrame( void * pSrc, void * pDest, UINT width, UINT height, UINT pitch )
\r
122 // tbb::enumerable_thread_specific<cache_buffer> cache_buffers;
\r
124 // void * pCacheBlock = cache_buffers.local().data;
\r
126 // __m128i x0, x1, x2, x3;
\r
128 // __m128i *pStore;
\r
129 // __m128i *pCache;
\r
130 // UINT x, y, yLoad, yStore;
\r
131 // UINT rowsPerBlock;
\r
133 // UINT extraPitch;
\r
135 // rowsPerBlock = CACHED_BUFFER_SIZE / pitch;
\r
136 // width64 = (width + 63) & ~0x03f;
\r
137 // extraPitch = (pitch - width64) / 16;
\r
139 // pLoad = (__m128i *)pSrc;
\r
140 // pStore = (__m128i *)pDest;
\r
142 // // COPY THROUGH 4KB CACHED BUFFER
\r
143 // for( y = 0; y < height; y += rowsPerBlock )
\r
145 // // ROWS LEFT TO COPY AT END
\r
146 // if( y + rowsPerBlock > height )
\r
147 // rowsPerBlock = height - y;
\r
149 // pCache = (__m128i *)pCacheBlock;
\r
153 // // LOAD ROWS OF PITCH WIDTH INTO CACHED BLOCK
\r
154 // for( yLoad = 0; yLoad < rowsPerBlock; yLoad++ )
\r
156 // // COPY A ROW, CACHE LINE AT A TIME
\r
157 // for( x = 0; x < pitch; x +=64 )
\r
159 // x0 = _mm_stream_load_si128( pLoad +0 );
\r
160 // x1 = _mm_stream_load_si128( pLoad +1 );
\r
161 // x2 = _mm_stream_load_si128( pLoad +2 );
\r
162 // x3 = _mm_stream_load_si128( pLoad +3 );
\r
164 // _mm_store_si128( pCache +0, x0 );
\r
165 // _mm_store_si128( pCache +1, x1 );
\r
166 // _mm_store_si128( pCache +2, x2 );
\r
167 // _mm_store_si128( pCache +3, x3 );
\r
176 // pCache = (__m128i *)pCacheBlock;
\r
178 // // STORE ROWS OF FRAME WIDTH FROM CACHED BLOCK
\r
179 // for( yStore = 0; yStore < rowsPerBlock; yStore++ )
\r
181 // // copy a row, cache line at a time
\r
182 // for( x = 0; x < width64; x +=64 )
\r
184 // x0 = _mm_load_si128( pCache );
\r
185 // x1 = _mm_load_si128( pCache +1 );
\r
186 // x2 = _mm_load_si128( pCache +2 );
\r
187 // x3 = _mm_load_si128( pCache +3 );
\r
189 // _mm_stream_si128( pStore, x0 );
\r
190 // _mm_stream_si128( pStore +1, x1 );
\r
191 // _mm_stream_si128( pStore +2, x2 );
\r
192 // _mm_stream_si128( pStore +3, x3 );
\r
198 // pCache += extraPitch;
\r
199 // pStore += extraPitch;
\r