1 /* This file is part of the Pangolin Project.
2  * http://github.com/stevenlovegrove/Pangolin
3  *
4  * Copyright (c) 2013 Steven Lovegrove
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use,
10  * copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  */
27 
28 #include <pangolin/video/drivers/merge.h>
29 #include <pangolin/factory/factory_registry.h>
30 #include <pangolin/video/iostream_operators.h>
31 #include <pangolin/plot/range.h>
32 #include <assert.h> // assert()
33 
34 #include <assert.h>
35 
36 namespace pangolin
37 {
38 
MergeVideo(std::unique_ptr<VideoInterface> & src_,const std::vector<Point> & stream_pos,size_t w=0,size_t h=0)39 MergeVideo::MergeVideo(std::unique_ptr<VideoInterface>& src_, const std::vector<Point>& stream_pos, size_t w = 0, size_t h = 0 )
40     : src( std::move(src_) ), buffer(new uint8_t[src->SizeBytes()]), stream_pos(stream_pos)
41 {
42     videoin.push_back(src.get());
43 
44     // Must be a stream_pos for each stream
45     // Each stream must have the same format.
46     assert(stream_pos.size() == src->Streams().size());
47     assert(src->Streams().size() > 0);
48     const PixelFormat fmt = src->Streams()[0].PixFormat();
49     for(size_t i=1; i < src->Streams().size(); ++i) {
50         assert(src->Streams()[i].PixFormat().format == fmt.format);
51     }
52 
53     // Compute buffer regions for data copying.
54     XYRange<size_t> r = XYRange<size_t>::Empty();
55     for(size_t i=0; i < src->Streams().size(); ++i) {
56         const StreamInfo& si = src->Streams()[i];
57         const size_t x = stream_pos[i].x;
58         const size_t y = stream_pos[i].y;
59         XYRange<size_t> sr(x, x + si.Width(), y, y + si.Height());
60         r.Insert(sr);
61     }
62 
63     // Use implied min / max based on points
64     if(!w && !h) {
65         w = r.x.max;
66         h = r.y.max;
67     }
68 
69     size_bytes = w*h*fmt.bpp/8;
70     streams.emplace_back(fmt,w,h,w*fmt.bpp/8,(unsigned char*)0);
71 }
72 
~MergeVideo()73 MergeVideo::~MergeVideo()
74 {
75 
76 }
77 
78 //! Implement VideoInput::Start()
Start()79 void MergeVideo::Start()
80 {
81     src->Start();
82 }
83 
84 //! Implement VideoInput::Stop()
Stop()85 void MergeVideo::Stop()
86 {
87     src->Stop();
88 }
89 
90 //! Implement VideoInput::SizeBytes()
SizeBytes() const91 size_t MergeVideo::SizeBytes() const
92 {
93     return size_bytes;
94 }
95 
96 //! Implement VideoInput::Streams()
Streams() const97 const std::vector<StreamInfo>& MergeVideo::Streams() const
98 {
99     return streams;
100 }
101 
CopyBuffer(unsigned char * dst_bytes,unsigned char * src_bytes)102 void MergeVideo::CopyBuffer(unsigned char* dst_bytes, unsigned char* src_bytes)
103 {
104     Image<unsigned char> dst_image = Streams()[0].StreamImage(dst_bytes);
105     const size_t dst_pix_bytes = Streams()[0].PixFormat().bpp / 8;
106 
107     for(size_t i=0; i < stream_pos.size(); ++i) {
108         const StreamInfo& src_stream = src->Streams()[i];
109         const Image<unsigned char> src_image = src_stream.StreamImage(src_bytes);
110         const Point& p = stream_pos[i];
111         for(size_t y=0; y < src_stream.Height(); ++y) {
112             // Copy row from src to dst
113             std::memcpy(
114                 dst_image.RowPtr(y + p.y) + p.x * dst_pix_bytes,
115                 src_image.RowPtr(y), src_stream.RowBytes()
116             );
117         }
118     }
119 }
120 
121 //! Implement VideoInput::GrabNext()
GrabNext(unsigned char * image,bool wait)122 bool MergeVideo::GrabNext( unsigned char* image, bool wait )
123 {
124     const bool success = src->GrabNext(buffer.get(), wait);
125     if(success) CopyBuffer(image, buffer.get());
126     return success;
127 }
128 
129 //! Implement VideoInput::GrabNewest()
GrabNewest(unsigned char * image,bool wait)130 bool MergeVideo::GrabNewest( unsigned char* image, bool wait )
131 {
132     const bool success = src->GrabNewest(buffer.get(), wait);
133     if(success) CopyBuffer(image, buffer.get());
134     return success;
135 }
136 
InputStreams()137 std::vector<VideoInterface*>& MergeVideo::InputStreams()
138 {
139     return videoin;
140 }
141 
PANGOLIN_REGISTER_FACTORY(MergeVideo)142 PANGOLIN_REGISTER_FACTORY(MergeVideo)
143 {
144     struct MergeVideoFactory final : public FactoryInterface<VideoInterface> {
145         std::unique_ptr<VideoInterface> Open(const Uri& uri) override {
146             const ImageDim dim = uri.Get<ImageDim>("size", ImageDim(0,0));
147 
148             std::unique_ptr<VideoInterface> subvid = pangolin::OpenVideo(uri.url);
149             std::vector<Point> points;
150             Point p(0,0);
151             for(size_t s=0; s < subvid->Streams().size(); ++s) {
152                 const StreamInfo& si = subvid->Streams()[s];
153                 p = uri.Get<Point>("pos"+std::to_string(s+1), p);
154                 points.push_back(p);
155                 p.x += si.Width();
156             }
157 
158             return std::unique_ptr<VideoInterface>(new MergeVideo(subvid, points, dim.x, dim.y));
159         }
160     };
161 
162     FactoryRegistry<VideoInterface>::I().RegisterFactory(std::make_shared<MergeVideoFactory>(), 10, "merge");
163 }
164 
165 }
166