1 /*
2  * xcam_analyzer.h - libxcam analyzer
3  *
4  *  Copyright (c) 2014-2015 Intel Corporation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Author: Wind Yuan <feng.yuan@intel.com>
19  *         Zong Wei <wei.zong@intel.com>
20  */
21 
22 #ifndef XCAM_ANALYZER_H
23 #define XCAM_ANALYZER_H
24 
25 #include <xcam_std.h>
26 #include <handler_interface.h>
27 #include <xcam_thread.h>
28 #include <video_buffer.h>
29 #include <safe_list.h>
30 
31 namespace XCam {
32 
33 class XAnalyzer;
34 
35 class AnalyzerThread
36     : public Thread
37 {
38 public:
39     AnalyzerThread (XAnalyzer *analyzer);
40     ~AnalyzerThread ();
41 
triger_stop()42     void triger_stop() {
43         _stats_queue.pause_pop ();
44     }
45     bool push_stats (const SmartPtr<VideoBuffer> &buffer);
46 
47 protected:
48     virtual bool started ();
stopped()49     virtual void stopped () {
50         _stats_queue.clear ();
51     }
52     virtual bool loop ();
53 
54 private:
55     XAnalyzer              *_analyzer;
56     SafeList<VideoBuffer>   _stats_queue;
57 };
58 
59 class AnalyzerCallback {
60 public:
AnalyzerCallback()61     explicit AnalyzerCallback () {}
~AnalyzerCallback()62     virtual ~AnalyzerCallback () {}
63     virtual void x3a_calculation_done (XAnalyzer *analyzer, X3aResultList &results);
64     virtual void x3a_calculation_failed (XAnalyzer *analyzer, int64_t timestamp, const char *msg);
65 
66 private:
67     XCAM_DEAD_COPY (AnalyzerCallback);
68 };
69 
70 class AnalyzerThread;
71 
72 class XAnalyzer {
73     friend class AnalyzerThread;
74 public:
75     explicit XAnalyzer (const char *name = NULL);
76     virtual ~XAnalyzer ();
77 
78     bool set_results_callback (AnalyzerCallback *callback);
79     XCamReturn prepare_handlers ();
80 
81     // prepare_handlers must called before init
82     XCamReturn init (uint32_t width, uint32_t height, double framerate);
83     XCamReturn deinit ();
84     // set_sync_mode must be called before start
85     XCamReturn set_sync_mode (bool sync);
get_sync_mode()86     bool get_sync_mode () const {
87         return _sync;
88     };
89     XCamReturn start ();
90     XCamReturn stop ();
91     XCamReturn push_buffer (const SmartPtr<VideoBuffer> &buffer);
92 
get_width()93     uint32_t get_width () const {
94         return _width;
95     }
get_height()96     uint32_t get_height () const {
97         return _height;
98     }
99 
get_framerate()100     double get_framerate () const {
101         return _framerate;
102     }
get_name()103     const char * get_name () const {
104         return _name;
105     }
106 
107 protected:
108     /* virtual function list */
109     virtual XCamReturn create_handlers () = 0;
110     virtual XCamReturn release_handlers () = 0;
111     virtual XCamReturn internal_init (uint32_t width, uint32_t height, double framerate) = 0;
112     virtual XCamReturn internal_deinit () = 0;
113 
114     // in analyzer thread
115     virtual XCamReturn configure () = 0;
116     virtual XCamReturn analyze (const SmartPtr<VideoBuffer> &buffer) = 0;
117 
118 protected:
119     void notify_calculation_done (X3aResultList &results);
120     void notify_calculation_failed (AnalyzerHandler *handler, int64_t timestamp, const char *msg);
121     void set_results_timestamp (X3aResultList &results, int64_t timestamp);
122 
123 private:
124 
125     XCAM_DEAD_COPY (XAnalyzer);
126 
127 protected:
128     SmartPtr<AnalyzerThread> _analyzer_thread;
129 
130 private:
131     char                    *_name;
132     bool                     _sync;
133     bool                     _started;
134     uint32_t                 _width;
135     uint32_t                 _height;
136     double                   _framerate;
137     AnalyzerCallback        *_callback;
138 };
139 
140 }
141 #endif //XCAM_ANALYZER_H
142