Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame^] | 1 | /* st_second_stage.h |
| 2 | * |
| 3 | * This file contains a sound trigger second stage session abstraction. This |
| 4 | * abstraction represents a single st second stage session from the st session |
| 5 | * perspective. |
| 6 | * |
| 7 | * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions are |
| 11 | * met: |
| 12 | * * Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * * Redistributions in binary form must reproduce the above |
| 15 | * copyright notice, this list of conditions and the following |
| 16 | * disclaimer in the documentation and/or other materials provided |
| 17 | * with the distribution. |
| 18 | * * Neither the name of The Linux Foundation nor the names of its |
| 19 | * contributors may be used to endorse or promote products derived |
| 20 | * from this software without specific prior written permission. |
| 21 | * |
| 22 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 23 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 24 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 26 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 29 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 30 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 31 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 32 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | */ |
| 34 | #ifndef ST_SECOND_STAGE_H |
| 35 | #define ST_SECOND_STAGE_H |
| 36 | |
| 37 | #include <stdint.h> |
| 38 | #include <pthread.h> |
| 39 | #include <cutils/list.h> |
| 40 | #ifdef ST_MULTI_STAGE_ENABLED |
| 41 | #include <capi_v2.h> |
| 42 | #endif |
| 43 | #include "st_common_defs.h" |
| 44 | #include "sml_model_parser.h" |
| 45 | #include "st_buffering.h" |
| 46 | |
| 47 | #define CNN_FRAME_SIZE (320) |
| 48 | |
| 49 | typedef enum st_sound_model_type { |
| 50 | ST_SM_TYPE_NONE, |
| 51 | ST_SM_TYPE_KEYWORD_DETECTION, |
| 52 | ST_SM_TYPE_USER_VERIFICATION, |
| 53 | ST_SM_TYPE_CUSTOM_DETECTION, |
| 54 | ST_SM_TYPE_MAX |
| 55 | }st_sound_model_type_t; |
| 56 | |
| 57 | typedef enum st_ss_detection_status { |
| 58 | KEYWORD_DETECTION_PENDING = 0x1, |
| 59 | KEYWORD_DETECTION_SUCCESS = 0x2, |
| 60 | KEYWORD_DETECTION_REJECT = 0x4, |
| 61 | USER_VERIFICATION_PENDING = 0x8, |
| 62 | USER_VERIFICATION_SUCCESS = 0x10, |
| 63 | USER_VERIFICATION_REJECT = 0x20, |
| 64 | }st_ss_detection_status_t; |
| 65 | |
| 66 | typedef struct st_second_stage_info { |
| 67 | enum st_sound_model_type sm_detection_type; |
| 68 | listen_model_indicator_enum sm_id; |
| 69 | char lib_name[MAX_LIB_HANDLE_SIZE]; |
| 70 | unsigned int sample_rate; |
| 71 | unsigned int bit_width; |
| 72 | unsigned int channel_count; |
| 73 | unsigned int data_after_kw_end; |
| 74 | }st_second_stage_info_t; |
| 75 | |
| 76 | typedef struct st_lsm_second_stage_config { |
| 77 | struct st_second_stage_info *ss_info; |
| 78 | struct listnode list_node; |
| 79 | struct st_lsm_ss_params *params; |
| 80 | /* sound model data */ |
| 81 | uint8_t *sm_data; |
| 82 | unsigned int sm_size; |
| 83 | /* confidence levels */ |
| 84 | int32_t confidence_threshold; |
| 85 | }st_lsm_ss_config_t; |
| 86 | |
| 87 | typedef struct st_arm_second_stage { |
| 88 | struct st_second_stage_info *ss_info; |
| 89 | struct listnode list_node; |
| 90 | struct st_arm_ss_session *ss_session; |
| 91 | }st_arm_second_stage_t; |
| 92 | |
| 93 | typedef struct st_arm_ss_session { |
| 94 | /* For controlling the second stage session */ |
| 95 | pthread_t thread; |
| 96 | pthread_mutex_t lock; |
| 97 | pthread_cond_t cond; |
| 98 | bool exit_thread; |
| 99 | struct st_session *st_ses; |
| 100 | |
| 101 | /* For CNN to overwrite 1st stage indices */ |
| 102 | uint32_t kw_start_idx; |
| 103 | uint32_t kw_end_idx; |
| 104 | |
| 105 | /* For passing the buffered pcm data to modules */ |
| 106 | uint8_t *hw_rd_ptr; |
| 107 | unsigned int unread_bytes; |
| 108 | bool exit_buffering; |
| 109 | unsigned int buff_sz; |
| 110 | unsigned int bytes_processed; |
| 111 | unsigned int buf_start; |
| 112 | unsigned int buf_end; |
| 113 | |
| 114 | /* For handling SUCCESS/REJECT results from modules */ |
| 115 | enum st_ss_detection_status det_status; |
| 116 | |
| 117 | /* For communicating with Capi modules */ |
| 118 | #ifdef ST_MULTI_STAGE_ENABLED |
| 119 | capi_v2_init_f capi_init; |
| 120 | void *capi_lib_handle; |
| 121 | capi_v2_t *capi_handle; |
| 122 | #endif |
| 123 | |
| 124 | /* For registering the sound model with modules */ |
| 125 | uint8_t *sound_model; |
| 126 | unsigned int sm_size; |
| 127 | |
| 128 | /* For getting and setting confidence levels */ |
| 129 | int32_t confidence_threshold; |
| 130 | int32_t confidence_score; |
| 131 | |
| 132 | /* For controlling the second stage process threads */ |
| 133 | bool start_processing; |
| 134 | }st_arm_ss_session_t; |
| 135 | |
| 136 | #ifdef ST_MULTI_STAGE_ENABLED |
| 137 | int st_second_stage_module_init(st_arm_second_stage_t *st_sec_stage, |
| 138 | void *lib_name); |
| 139 | int st_second_stage_module_deinit(st_arm_second_stage_t *st_sec_stage); |
| 140 | int st_second_stage_prepare_session(st_arm_second_stage_t *st_sec_stage); |
| 141 | int st_second_stage_start_session(st_arm_second_stage_t *st_sec_stage); |
| 142 | int st_second_stage_stop_session(st_arm_second_stage_t *st_sec_stage); |
| 143 | #else |
| 144 | #define st_second_stage_module_init(a, b) (0) |
| 145 | #define st_second_stage_module_deinit(a) (0) |
| 146 | #define st_second_stage_prepare_session(a) (0) |
| 147 | #define st_second_stage_start_session(a) (0) |
| 148 | #define st_second_stage_stop_session(a) (0) |
| 149 | #endif |
| 150 | |
| 151 | #endif /* ST_SECOND_STAGE_H */ |