Update the changelog
[opencv] / filters / Tracker3dFilter / src / Tracker3dFilter.h
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2002, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistributions of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistributions in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 #include <vector>
43 #include <queue>
44 #include <string>
45 #include "ITracker.h"
46 #include "ITracker3dFilter.h"
47 #include "cv.h"
48 #include "cvaux.h"
49
50 class Tracker3dInputPin;
51 class Tracker3dOutputPin;
52
53 class Tracker3dFilter : public CBaseFilter, ITracker3dFilter, ITracker3dInternal, ISpecifyPropertyPages, IPersistStream
54 {
55     // Properties
56     int m_num_streams;
57     int m_viewing_stream;
58     std::vector<ITracker3dCallback *> m_callbacks;
59     InputSize m_preferred_size;
60     GUID m_tracker_clsid;
61
62     // Camera calibration stuff
63     int m_calibrate_cameras; // 0 - not calibrating;
64                              // 1 - calibrate until successful
65                              // 2 - calibrate continuously
66     Cv3dTrackerCameraIntrinsics *m_camera_intrinsics;
67     CvSize m_etalon_size; // size of the checkerboard used to calibrate cameras
68     float m_square_size;
69
70     // Results of camera calibration
71     Cv3dTrackerCameraInfo *m_pCameraInfo;
72
73     // Results of tracking
74     std::vector<Cv3dTrackerTrackedObject> m_tracked_objects;
75
76     // Filter stuff
77     CCritSec m_cs;
78     CCritSec m_recv_cs;
79     struct Stream
80     {
81         struct Sample
82         {
83             Sample(IMediaSample *, const ITracker::TrackingInfo &);
84             ~Sample();
85             Sample(const Sample &s);
86             void operator=(const Sample &);
87             IMediaSample *sample;
88             REFERENCE_TIME ts, te;
89             unsigned char *data;
90             ITracker::TrackingInfo tracking_info;
91         };
92         Tracker3dInputPin *input_pin;
93         Tracker3dOutputPin *output_pin;
94         CMediaType media_type;
95         ITracker *tracker;
96         IplImage image_header1, image_header2;
97         typedef std::deque<Sample> SampleQueue;
98         SampleQueue queue;
99         unsigned long discarded_frames, total_frames;
100         Stream();
101         ~Stream();
102         void operator =(Stream &);
103     }; 
104     Stream *m_streams;
105     int m_end_of_stream_count; // used to determine when to pass
106                                // end of stream to all output pins
107
108     bool m_camera_configuration_loaded;
109
110     Tracker3dFilter(TCHAR *name, IUnknown *unk);
111     ~Tracker3dFilter();
112
113     void SetNumberOfStreams(int num_streams);
114     HRESULT ReadCameraIntrinsics(const char *filenames[]);
115     bool QueuesReady();
116
117     std::string CalibrationName(int i);
118     void LoadCameraConfiguration();
119     void SaveCameraConfiguration();
120
121 public:
122     static CUnknown * WINAPI CreateInstance(IUnknown *unk, HRESULT *phr);
123     DECLARE_IUNKNOWN;
124     STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void ** ppv);
125
126     // Called by pins
127     HRESULT CheckMediaType(int pin, const CMediaType *mt);
128     HRESULT SetMediaType(int pin, const CMediaType *mt);
129     HRESULT GetMediaType(int pin, int pos, CMediaType *mt);
130     HRESULT Receive(int pin, IMediaSample *);
131     HRESULT EndOfStream(int pin);
132     HRESULT Flush(int pin);
133
134     // Overridden from CBaseFilter
135     CBasePin *GetPin(int n);
136     int GetPinCount();
137     STDMETHODIMP Run(REFERENCE_TIME);
138     STDMETHODIMP Pause();
139     STDMETHODIMP Stop();
140
141     // ISpecifyPropertyPages interface
142     STDMETHODIMP GetPages(CAUUID *pPages);
143
144     // IPersistStream
145     STDMETHODIMP GetClassID(CLSID *clsid);
146     STDMETHODIMP IsDirty();
147     STDMETHODIMP Load(IStream *);
148     STDMETHODIMP Save(IStream *, BOOL clear_dirty);
149     STDMETHODIMP GetSizeMax(ULARGE_INTEGER *size);
150
151     // ITracker3dFilter
152     STDMETHODIMP GetNumberOfCameras(int &num_cameras);
153     STDMETHODIMP LoadCameraConfiguration(const char *filename);
154     STDMETHODIMP SaveCameraConfiguration(const char *filename);
155     STDMETHODIMP GetDefaultTracker(GUID &filename);
156     STDMETHODIMP SetDefaultTracker(const GUID &filename);
157     STDMETHODIMP SetTrackers(int numstreams, ITracker * const *trackers);
158     STDMETHODIMP GetTrackers(std::vector<ITracker *> &trackers);
159     STDMETHODIMP CalibrateCameras(int checkerboard_width, int checkerboard_height,
160                                   const char *camera_intrinsics_filenames[],
161                                   float square_size,
162                                   bool continuous);
163     STDMETHODIMP GetTrackedObjects(std::vector<Cv3dTrackerTrackedObject> &tracked_objects);
164     STDMETHODIMP SetViewingStream(int stream);
165     STDMETHODIMP GetViewingStream(int &stream);
166     STDMETHODIMP GetCameraName(int i, std::string &name);
167     STDMETHODIMP SetPreferredInputSize(InputSize size);
168     STDMETHODIMP GetPreferredInputSize(InputSize &size);
169     STDMETHODIMP AddCallback(ITracker3dCallback *);
170     STDMETHODIMP RemoveCallback(ITracker3dCallback *);
171     STDMETHODIMP IsConnected(bool &any_connected, bool &all_connected);
172
173     // ITracker3dInternal
174     STDMETHODIMP GetCameraInfo(std::vector<Cv3dTrackerCameraInfo> &info);
175 };
176
177 class Tracker3dInputPin : public CBaseInputPin
178 {
179     Tracker3dFilter *m_pFilter;
180     int m_pin_number;
181     long m_refcnt;
182
183 public:
184     Tracker3dInputPin(int pin_number, Tracker3dFilter *filter, CCritSec *cs);
185     ~Tracker3dInputPin();
186
187     STDMETHODIMP_(ULONG) NonDelegatingAddRef();
188     STDMETHODIMP_(ULONG) NonDelegatingRelease();
189
190     HRESULT CheckMediaType(const CMediaType *mt);
191     HRESULT SetMediaType(const CMediaType *mt);
192     HRESULT GetMediaType(int pos, CMediaType *mt);
193     STDMETHODIMP GetAllocator(IMemAllocator **);
194     STDMETHODIMP NotifyAllocator(IMemAllocator *allocator, BOOL read_only);
195     STDMETHODIMP Receive(IMediaSample *sample);
196     STDMETHODIMP Notify(IBaseFilter * pSender, Quality q) { return E_NOTIMPL; };
197     STDMETHODIMP EndOfStream();
198     STDMETHODIMP BeginFlush();
199     STDMETHODIMP EndFlush();
200     STDMETHODIMP NewSegment(REFERENCE_TIME start, REFERENCE_TIME stop, double rate);
201 };
202
203 class Tracker3dOutputPin : public CBaseOutputPin
204 {
205     Tracker3dFilter *m_pFilter;
206     int m_pin_number;
207     long m_refcnt;
208
209 public:
210     Tracker3dOutputPin(int pin_number, Tracker3dFilter *filter, CCritSec *cs);
211     ~Tracker3dOutputPin();
212
213     STDMETHODIMP_(ULONG) NonDelegatingAddRef();
214     STDMETHODIMP_(ULONG) NonDelegatingRelease();
215
216     HRESULT CheckMediaType(const CMediaType *mt);
217     HRESULT SetMediaType(const CMediaType *mt);
218     HRESULT GetMediaType(int pos, CMediaType *mt);
219     HRESULT DecideAllocator(IMemInputPin *, IMemAllocator **);
220     HRESULT DecideBufferSize(IMemAllocator *, ALLOCATOR_PROPERTIES *) { return E_NOTIMPL; };
221     STDMETHODIMP Notify(IBaseFilter * pSender, Quality q) { return E_NOTIMPL; };
222 };