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 | * |
Zhou Song | ebe8a93 | 2020-02-10 12:18:31 +0800 | [diff] [blame] | 7 | * Copyright (c) 2018-2020, The Linux Foundation. All rights reserved. |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 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; |
Zhou Song | ebe8a93 | 2020-02-10 12:18:31 +0800 | [diff] [blame] | 73 | unsigned int data_before_kw_start; |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 74 | unsigned int data_after_kw_end; |
| 75 | }st_second_stage_info_t; |
| 76 | |
| 77 | typedef struct st_lsm_second_stage_config { |
| 78 | struct st_second_stage_info *ss_info; |
| 79 | struct listnode list_node; |
| 80 | struct st_lsm_ss_params *params; |
| 81 | /* sound model data */ |
| 82 | uint8_t *sm_data; |
| 83 | unsigned int sm_size; |
| 84 | /* confidence levels */ |
| 85 | int32_t confidence_threshold; |
| 86 | }st_lsm_ss_config_t; |
| 87 | |
| 88 | typedef struct st_arm_second_stage { |
| 89 | struct st_second_stage_info *ss_info; |
| 90 | struct listnode list_node; |
| 91 | struct st_arm_ss_session *ss_session; |
Venkatesh Mangalappali | e4f1753 | 2019-06-04 16:30:28 -0700 | [diff] [blame] | 92 | struct sound_trigger_device *stdev; |
Quinn Male | 3d7d9d4 | 2019-05-20 13:35:01 -0700 | [diff] [blame] | 93 | FILE *dump_fp; |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 94 | }st_arm_second_stage_t; |
| 95 | |
| 96 | typedef struct st_arm_ss_session { |
| 97 | /* For controlling the second stage session */ |
| 98 | pthread_t thread; |
| 99 | pthread_mutex_t lock; |
| 100 | pthread_cond_t cond; |
| 101 | bool exit_thread; |
Venkatesh Mangalappali | e4f1753 | 2019-06-04 16:30:28 -0700 | [diff] [blame] | 102 | struct st_proxy_session *st_ses; |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 103 | |
| 104 | /* For CNN to overwrite 1st stage indices */ |
| 105 | uint32_t kw_start_idx; |
| 106 | uint32_t kw_end_idx; |
| 107 | |
| 108 | /* For passing the buffered pcm data to modules */ |
| 109 | uint8_t *hw_rd_ptr; |
| 110 | unsigned int unread_bytes; |
| 111 | bool exit_buffering; |
| 112 | unsigned int buff_sz; |
Dallas Delaney | 03bd0b9 | 2019-07-30 14:38:40 -0700 | [diff] [blame] | 113 | unsigned int lab_buf_sz; |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 114 | unsigned int bytes_processed; |
| 115 | unsigned int buf_start; |
| 116 | unsigned int buf_end; |
| 117 | |
| 118 | /* For handling SUCCESS/REJECT results from modules */ |
| 119 | enum st_ss_detection_status det_status; |
| 120 | |
| 121 | /* For communicating with Capi modules */ |
| 122 | #ifdef ST_MULTI_STAGE_ENABLED |
| 123 | capi_v2_init_f capi_init; |
| 124 | void *capi_lib_handle; |
| 125 | capi_v2_t *capi_handle; |
| 126 | #endif |
| 127 | |
| 128 | /* For registering the sound model with modules */ |
| 129 | uint8_t *sound_model; |
| 130 | unsigned int sm_size; |
| 131 | |
| 132 | /* For getting and setting confidence levels */ |
| 133 | int32_t confidence_threshold; |
| 134 | int32_t confidence_score; |
| 135 | |
| 136 | /* For controlling the second stage process threads */ |
| 137 | bool start_processing; |
| 138 | }st_arm_ss_session_t; |
| 139 | |
| 140 | #ifdef ST_MULTI_STAGE_ENABLED |
| 141 | int st_second_stage_module_init(st_arm_second_stage_t *st_sec_stage, |
| 142 | void *lib_name); |
| 143 | int st_second_stage_module_deinit(st_arm_second_stage_t *st_sec_stage); |
| 144 | int st_second_stage_prepare_session(st_arm_second_stage_t *st_sec_stage); |
| 145 | int st_second_stage_start_session(st_arm_second_stage_t *st_sec_stage); |
| 146 | int st_second_stage_stop_session(st_arm_second_stage_t *st_sec_stage); |
| 147 | #else |
| 148 | #define st_second_stage_module_init(a, b) (0) |
| 149 | #define st_second_stage_module_deinit(a) (0) |
| 150 | #define st_second_stage_prepare_session(a) (0) |
| 151 | #define st_second_stage_start_session(a) (0) |
| 152 | #define st_second_stage_stop_session(a) (0) |
| 153 | #endif |
| 154 | |
| 155 | #endif /* ST_SECOND_STAGE_H */ |