Home | History | Annotate | Download | only in xcore
      1 /*
      2  * smart_analyzer.cpp - smart analyzer
      3  *
      4  *  Copyright (c) 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: Zong Wei <wei.zong (at) intel.com>
     19  */
     20 
     21 #include "smart_analyzer_loader.h"
     22 #include "smart_analyzer.h"
     23 #include "smart_analysis_handler.h"
     24 
     25 #include "xcam_obj_debug.h"
     26 
     27 namespace XCam {
     28 
     29 SmartAnalyzer::SmartAnalyzer (const char *name)
     30     : XAnalyzer (name)
     31 {
     32     XCAM_OBJ_PROFILING_INIT;
     33 }
     34 
     35 SmartAnalyzer::~SmartAnalyzer ()
     36 {
     37 }
     38 
     39 XCamReturn
     40 SmartAnalyzer::add_handler (SmartPtr<SmartAnalysisHandler> handler)
     41 {
     42     XCamReturn ret = XCAM_RETURN_NO_ERROR;
     43     if (!handler.ptr ()) {
     44         return XCAM_RETURN_ERROR_PARAM;
     45     }
     46 
     47     _handlers.push_back (handler);
     48     handler->set_analyzer (this);
     49     return ret;
     50 }
     51 
     52 XCamReturn
     53 SmartAnalyzer::create_handlers ()
     54 {
     55     XCamReturn ret = XCAM_RETURN_NO_ERROR;
     56     if (_handlers.empty ()) {
     57         ret = XCAM_RETURN_ERROR_PARAM;
     58     }
     59     return ret;
     60 }
     61 
     62 XCamReturn
     63 SmartAnalyzer::release_handlers ()
     64 {
     65     XCamReturn ret = XCAM_RETURN_NO_ERROR;
     66     return ret;
     67 }
     68 
     69 XCamReturn
     70 SmartAnalyzer::internal_init (uint32_t width, uint32_t height, double framerate)
     71 {
     72     XCAM_UNUSED (width);
     73     XCAM_UNUSED (height);
     74     XCAM_UNUSED (framerate);
     75     SmartHandlerList::iterator i_handler = _handlers.begin ();
     76     for (; i_handler != _handlers.end ();  ++i_handler)
     77     {
     78         SmartPtr<SmartAnalysisHandler> handler = *i_handler;
     79         XCamReturn ret = handler->create_context (handler);
     80         if (ret != XCAM_RETURN_NO_ERROR) {
     81             XCAM_LOG_WARNING ("smart analyzer initialize handler(%s) context failed", XCAM_STR(handler->get_name()));
     82         }
     83     }
     84 
     85     return XCAM_RETURN_NO_ERROR;
     86 }
     87 
     88 XCamReturn
     89 SmartAnalyzer::internal_deinit ()
     90 {
     91     SmartHandlerList::iterator i_handler = _handlers.begin ();
     92     for (; i_handler != _handlers.end ();  ++i_handler)
     93     {
     94         SmartPtr<SmartAnalysisHandler> handler = *i_handler;
     95         if (handler->is_valid ())
     96             handler->destroy_context ();
     97     }
     98 
     99     return XCAM_RETURN_NO_ERROR;
    100 }
    101 
    102 XCamReturn
    103 SmartAnalyzer::configure ()
    104 {
    105     XCamReturn ret = XCAM_RETURN_NO_ERROR;
    106     return ret;
    107 }
    108 
    109 XCamReturn
    110 SmartAnalyzer::update_params (XCamSmartAnalysisParam &params)
    111 {
    112     XCamReturn ret = XCAM_RETURN_NO_ERROR;
    113 
    114     SmartHandlerList::iterator i_handler = _handlers.begin ();
    115     for (; i_handler != _handlers.end ();  ++i_handler)
    116     {
    117         SmartPtr<SmartAnalysisHandler> handler = *i_handler;
    118         if (!handler->is_valid ())
    119             continue;
    120 
    121         ret = handler->update_params (params);
    122 
    123         if (ret != XCAM_RETURN_NO_ERROR) {
    124             XCAM_LOG_WARNING ("smart analyzer update handler(%s) context failed", XCAM_STR(handler->get_name()));
    125             handler->destroy_context ();
    126         }
    127     }
    128 
    129     return XCAM_RETURN_NO_ERROR;
    130 }
    131 
    132 XCamReturn
    133 SmartAnalyzer::analyze (const SmartPtr<VideoBuffer> &buffer)
    134 {
    135     XCAM_OBJ_PROFILING_START;
    136 
    137     XCamReturn ret = XCAM_RETURN_NO_ERROR;
    138     X3aResultList results;
    139 
    140     if (!buffer.ptr ()) {
    141         XCAM_LOG_DEBUG ("SmartAnalyzer::analyze got NULL buffer!");
    142         return XCAM_RETURN_ERROR_PARAM;
    143     }
    144 
    145     SmartHandlerList::iterator i_handler = _handlers.begin ();
    146     for (; i_handler != _handlers.end ();  ++i_handler)
    147     {
    148         SmartPtr<SmartAnalysisHandler> handler = *i_handler;
    149         if (!handler->is_valid ())
    150             continue;
    151 
    152         ret = handler->analyze (buffer, results);
    153         if (ret != XCAM_RETURN_NO_ERROR && ret != XCAM_RETURN_BYPASS) {
    154             XCAM_LOG_WARNING ("smart analyzer analyze handler(%s) context failed", XCAM_STR(handler->get_name()));
    155             handler->destroy_context ();
    156         }
    157     }
    158 
    159     if (!results.empty ()) {
    160         set_results_timestamp (results, buffer->get_timestamp ());
    161         notify_calculation_done (results);
    162     }
    163 
    164     XCAM_OBJ_PROFILING_END ("smart analysis", XCAM_OBJ_DUR_FRAME_NUM);
    165 
    166     return XCAM_RETURN_NO_ERROR;
    167 }
    168 
    169 void
    170 SmartAnalyzer::post_smart_results (X3aResultList &results, int64_t timestamp)
    171 {
    172     if (!results.empty ()) {
    173         set_results_timestamp (results, timestamp);
    174         notify_calculation_done (results);
    175     }
    176 }
    177 
    178 }
    179