huangcm
2024-12-18 9d29be7f7249789d6ffd0440067187a9f040c2cd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * smart_analyzer.cpp - smart analyzer
 *
 *  Copyright (c) 2015 Intel Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author: Zong Wei <wei.zong@intel.com>
 */
 
#include "smart_analyzer_loader.h"
#include "smart_analyzer.h"
#include "smart_analysis_handler.h"
 
#include "xcam_obj_debug.h"
 
namespace XCam {
 
SmartAnalyzer::SmartAnalyzer (const char *name)
    : XAnalyzer (name)
{
    XCAM_OBJ_PROFILING_INIT;
}
 
SmartAnalyzer::~SmartAnalyzer ()
{
}
 
XCamReturn
SmartAnalyzer::add_handler (SmartPtr<SmartAnalysisHandler> handler)
{
    XCamReturn ret = XCAM_RETURN_NO_ERROR;
    if (!handler.ptr ()) {
        return XCAM_RETURN_ERROR_PARAM;
    }
 
    _handlers.push_back (handler);
    handler->set_analyzer (this);
    return ret;
}
 
XCamReturn
SmartAnalyzer::create_handlers ()
{
    XCamReturn ret = XCAM_RETURN_NO_ERROR;
    if (_handlers.empty ()) {
        ret = XCAM_RETURN_ERROR_PARAM;
    }
    return ret;
}
 
XCamReturn
SmartAnalyzer::release_handlers ()
{
    XCamReturn ret = XCAM_RETURN_NO_ERROR;
    return ret;
}
 
XCamReturn
SmartAnalyzer::internal_init (uint32_t width, uint32_t height, double framerate)
{
    XCAM_UNUSED (width);
    XCAM_UNUSED (height);
    XCAM_UNUSED (framerate);
    SmartHandlerList::iterator i_handler = _handlers.begin ();
    for (; i_handler != _handlers.end ();  ++i_handler)
    {
        SmartPtr<SmartAnalysisHandler> handler = *i_handler;
        XCamReturn ret = handler->create_context (handler);
        if (ret != XCAM_RETURN_NO_ERROR) {
            XCAM_LOG_WARNING ("smart analyzer initialize handler(%s) context failed", XCAM_STR(handler->get_name()));
        }
    }
 
    return XCAM_RETURN_NO_ERROR;
}
 
XCamReturn
SmartAnalyzer::internal_deinit ()
{
    SmartHandlerList::iterator i_handler = _handlers.begin ();
    for (; i_handler != _handlers.end ();  ++i_handler)
    {
        SmartPtr<SmartAnalysisHandler> handler = *i_handler;
        if (handler->is_valid ())
            handler->destroy_context ();
    }
 
    return XCAM_RETURN_NO_ERROR;
}
 
XCamReturn
SmartAnalyzer::configure ()
{
    XCamReturn ret = XCAM_RETURN_NO_ERROR;
    return ret;
}
 
XCamReturn
SmartAnalyzer::update_params (XCamSmartAnalysisParam &params)
{
    XCamReturn ret = XCAM_RETURN_NO_ERROR;
 
    SmartHandlerList::iterator i_handler = _handlers.begin ();
    for (; i_handler != _handlers.end ();  ++i_handler)
    {
        SmartPtr<SmartAnalysisHandler> handler = *i_handler;
        if (!handler->is_valid ())
            continue;
 
        ret = handler->update_params (params);
 
        if (ret != XCAM_RETURN_NO_ERROR) {
            XCAM_LOG_WARNING ("smart analyzer update handler(%s) context failed", XCAM_STR(handler->get_name()));
            handler->destroy_context ();
        }
    }
 
    return XCAM_RETURN_NO_ERROR;
}
 
XCamReturn
SmartAnalyzer::analyze (const SmartPtr<VideoBuffer> &buffer)
{
    XCAM_OBJ_PROFILING_START;
 
    XCamReturn ret = XCAM_RETURN_NO_ERROR;
    X3aResultList results;
 
    if (!buffer.ptr ()) {
        XCAM_LOG_DEBUG ("SmartAnalyzer::analyze got NULL buffer!");
        return XCAM_RETURN_ERROR_PARAM;
    }
 
    SmartHandlerList::iterator i_handler = _handlers.begin ();
    for (; i_handler != _handlers.end ();  ++i_handler)
    {
        SmartPtr<SmartAnalysisHandler> handler = *i_handler;
        if (!handler->is_valid ())
            continue;
 
        ret = handler->analyze (buffer, results);
        if (ret != XCAM_RETURN_NO_ERROR && ret != XCAM_RETURN_BYPASS) {
            XCAM_LOG_WARNING ("smart analyzer analyze handler(%s) context failed", XCAM_STR(handler->get_name()));
            handler->destroy_context ();
        }
    }
 
    if (!results.empty ()) {
        set_results_timestamp (results, buffer->get_timestamp ());
        notify_calculation_done (results);
    }
 
    XCAM_OBJ_PROFILING_END ("smart analysis", XCAM_OBJ_DUR_FRAME_NUM);
 
    return XCAM_RETURN_NO_ERROR;
}
 
void
SmartAnalyzer::post_smart_results (X3aResultList &results, int64_t timestamp)
{
    if (!results.empty ()) {
        set_results_timestamp (results, timestamp);
        notify_calculation_done (results);
    }
}
 
}