Ranjani Sridharan | 75bb4a6 | 2018-05-31 19:29:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018, Intel Corporation |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * * Neither the name of the Intel Corporation nor the |
| 13 | * names of its contributors may be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | * POSSIBILITY OF SUCH DAMAGE. |
| 27 | * |
| 28 | * Author: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com> |
| 29 | * Ranjani Sridharan <ranjani.sridharan@linux.intel.com> |
| 30 | */ |
| 31 | |
| 32 | #include <sof/ipc.h> |
| 33 | #include <sof/list.h> |
| 34 | #include <getopt.h> |
| 35 | #include <dlfcn.h> |
| 36 | #include "host/common_test.h" |
| 37 | #include "host/topology.h" |
| 38 | #include "host/trace.h" |
| 39 | #include "host/file.h" |
| 40 | |
| 41 | #define TESTBENCH_NCH 2 /* Stereo */ |
| 42 | |
Ranjani Sridharan | 03067c6 | 2018-06-27 15:09:25 -0700 | [diff] [blame] | 43 | /* shared library look up table */ |
| 44 | struct shared_lib_table lib_table[NUM_WIDGETS_SUPPORTED] = { |
| 45 | {"file", "", SND_SOC_TPLG_DAPM_AIF_IN, "", 0, NULL}, |
| 46 | {"vol", "libsof_volume.so", SND_SOC_TPLG_DAPM_PGA, "sys_comp_volume_init", 0, |
| 47 | NULL}, |
| 48 | {"src", "libsof_src.so", SND_SOC_TPLG_DAPM_SRC, "sys_comp_src_init", 0, NULL}, |
| 49 | }; |
| 50 | |
Ranjani Sridharan | 75bb4a6 | 2018-05-31 19:29:07 -0700 | [diff] [blame] | 51 | /* main firmware context */ |
| 52 | static struct sof sof; |
| 53 | static int fr_id; /* comp id for fileread */ |
| 54 | static int fw_id; /* comp id for filewrite */ |
| 55 | static int sched_id; /* comp id for scheduling comp */ |
| 56 | |
| 57 | int debug; |
| 58 | |
| 59 | /* |
| 60 | * Parse shared library from user input |
Ranjani Sridharan | 03067c6 | 2018-06-27 15:09:25 -0700 | [diff] [blame] | 61 | * Currently only handles volume and src comp |
| 62 | * This function takes in the libraries to be used as an input in the format: |
| 63 | * "vol=libsof_volume.so,src=libsof_src.so,..." |
| 64 | * The function parses the above string to identify the following: |
| 65 | * component type and the library name and sets up the library handle |
| 66 | * for the component and stores it in the shared library table |
Ranjani Sridharan | 75bb4a6 | 2018-05-31 19:29:07 -0700 | [diff] [blame] | 67 | */ |
Ranjani Sridharan | 03067c6 | 2018-06-27 15:09:25 -0700 | [diff] [blame] | 68 | static void parse_libraries(char *libs) |
Ranjani Sridharan | 75bb4a6 | 2018-05-31 19:29:07 -0700 | [diff] [blame] | 69 | { |
| 70 | char *lib_token, *comp_token; |
| 71 | char *token = strtok_r(libs, ",", &lib_token); |
| 72 | char message[DEBUG_MSG_LEN]; |
Ranjani Sridharan | 03067c6 | 2018-06-27 15:09:25 -0700 | [diff] [blame] | 73 | int index; |
Ranjani Sridharan | 75bb4a6 | 2018-05-31 19:29:07 -0700 | [diff] [blame] | 74 | |
| 75 | while (token) { |
Ranjani Sridharan | 03067c6 | 2018-06-27 15:09:25 -0700 | [diff] [blame] | 76 | |
| 77 | /* get component type */ |
Ranjani Sridharan | 75bb4a6 | 2018-05-31 19:29:07 -0700 | [diff] [blame] | 78 | char *token1 = strtok_r(token, "=", &comp_token); |
| 79 | |
Ranjani Sridharan | 03067c6 | 2018-06-27 15:09:25 -0700 | [diff] [blame] | 80 | /* get shared library index from library table */ |
| 81 | index = get_index_by_name(token1, lib_table); |
Ranjani Sridharan | 75bb4a6 | 2018-05-31 19:29:07 -0700 | [diff] [blame] | 82 | |
Ranjani Sridharan | 03067c6 | 2018-06-27 15:09:25 -0700 | [diff] [blame] | 83 | if (index < 0) { |
| 84 | fprintf(stderr, "error: unsupported comp type\n"); |
| 85 | break; |
Ranjani Sridharan | 75bb4a6 | 2018-05-31 19:29:07 -0700 | [diff] [blame] | 86 | } |
Ranjani Sridharan | 03067c6 | 2018-06-27 15:09:25 -0700 | [diff] [blame] | 87 | |
| 88 | /* get shared library name */ |
| 89 | token1 = strtok_r(NULL, "=", &comp_token); |
| 90 | if (!token1) |
| 91 | break; |
| 92 | |
| 93 | /* close default shared library object */ |
| 94 | if (lib_table[index].handle) |
| 95 | dlclose(lib_table[index].handle); |
| 96 | |
| 97 | /* open volume shared library object */ |
| 98 | lib_table[index].handle = dlopen(token1, RTLD_LAZY); |
| 99 | if (!lib_table[index].handle) { |
| 100 | fprintf(stderr, "error: %s\n", dlerror()); |
| 101 | exit(EXIT_FAILURE); |
| 102 | } |
| 103 | |
| 104 | sprintf(message, "opening shared lib %s\n", token1); |
| 105 | debug_print(message); |
| 106 | |
| 107 | /* next library */ |
Ranjani Sridharan | 75bb4a6 | 2018-05-31 19:29:07 -0700 | [diff] [blame] | 108 | token = strtok_r(NULL, ",", &lib_token); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /* print usage for testbench */ |
| 113 | static void print_usage(char *executable) |
| 114 | { |
| 115 | printf("Usage: %s -i <input_file> -o <output_file> ", executable); |
| 116 | printf("-t <tplg_file> -b <input_format> "); |
| 117 | printf("-a <comp1=comp1_library,comp2=comp2_library>\n"); |
| 118 | printf("input_format should be S16_LE, S32_LE, S24_LE or FLOAT_LE\n"); |
| 119 | printf("Example Usage:\n"); |
| 120 | printf("%s -i in.txt -o out.txt -t test.tplg ", executable); |
Ranjani Sridharan | e822cfa | 2018-06-27 20:40:18 -0700 | [diff] [blame] | 121 | printf("-r 48000 -R 96000 "); |
Ranjani Sridharan | 75bb4a6 | 2018-05-31 19:29:07 -0700 | [diff] [blame] | 122 | printf("-b S16_LE -a vol=libsof_volume.so\n"); |
| 123 | } |
| 124 | |
| 125 | /* free components */ |
| 126 | static void free_comps(void) |
| 127 | { |
| 128 | struct list_item *clist; |
| 129 | struct list_item *temp; |
| 130 | struct ipc_comp_dev *icd = NULL; |
| 131 | |
| 132 | list_for_item_safe(clist, temp, &sof.ipc->comp_list) { |
| 133 | icd = container_of(clist, struct ipc_comp_dev, list); |
| 134 | switch (icd->type) { |
| 135 | case COMP_TYPE_COMPONENT: |
| 136 | comp_free(icd->cd); |
| 137 | list_item_del(&icd->list); |
| 138 | rfree(icd); |
| 139 | break; |
| 140 | case COMP_TYPE_BUFFER: |
| 141 | rfree(icd->cb->addr); |
| 142 | rfree(icd->cb); |
| 143 | list_item_del(&icd->list); |
| 144 | rfree(icd); |
| 145 | break; |
| 146 | default: |
| 147 | rfree(icd->pipeline); |
| 148 | list_item_del(&icd->list); |
| 149 | rfree(icd); |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
Ranjani Sridharan | 03067c6 | 2018-06-27 15:09:25 -0700 | [diff] [blame] | 155 | static int set_up_library_table(void) |
| 156 | { |
| 157 | int i; |
| 158 | |
| 159 | /* set up default shared libraries */ |
| 160 | for (i = 1; i < NUM_WIDGETS_SUPPORTED; i++) { |
| 161 | |
| 162 | /* open default shared library */ |
| 163 | lib_table[i].handle = |
| 164 | dlopen(lib_table[i].library_name, RTLD_LAZY); |
| 165 | if (!lib_table[i].handle) { |
| 166 | fprintf(stderr, "error: %s\n", dlerror()); |
| 167 | return -EINVAL; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
Ranjani Sridharan | 528ec8f | 2018-06-27 20:42:02 -0700 | [diff] [blame] | 174 | static void parse_input_args(int argc, char **argv) |
Ranjani Sridharan | 75bb4a6 | 2018-05-31 19:29:07 -0700 | [diff] [blame] | 175 | { |
Ranjani Sridharan | 528ec8f | 2018-06-27 20:42:02 -0700 | [diff] [blame] | 176 | int option = 0; |
Ranjani Sridharan | 75bb4a6 | 2018-05-31 19:29:07 -0700 | [diff] [blame] | 177 | |
Ranjani Sridharan | e822cfa | 2018-06-27 20:40:18 -0700 | [diff] [blame] | 178 | while ((option = getopt(argc, argv, "hdi:o:t:b:a:r:R:")) != -1) { |
Ranjani Sridharan | 75bb4a6 | 2018-05-31 19:29:07 -0700 | [diff] [blame] | 179 | switch (option) { |
| 180 | /* input sample file */ |
| 181 | case 'i': |
| 182 | input_file = strdup(optarg); |
| 183 | break; |
| 184 | |
| 185 | /* output sample file */ |
| 186 | case 'o': |
| 187 | output_file = strdup(optarg); |
| 188 | break; |
| 189 | |
| 190 | /* topology file */ |
| 191 | case 't': |
| 192 | tplg_file = strdup(optarg); |
| 193 | break; |
| 194 | |
| 195 | /* input samples bit format */ |
| 196 | case 'b': |
| 197 | bits_in = strdup(optarg); |
| 198 | break; |
| 199 | |
| 200 | /* override default libraries */ |
| 201 | case 'a': |
Ranjani Sridharan | 03067c6 | 2018-06-27 15:09:25 -0700 | [diff] [blame] | 202 | parse_libraries(optarg); |
Ranjani Sridharan | 75bb4a6 | 2018-05-31 19:29:07 -0700 | [diff] [blame] | 203 | break; |
| 204 | |
Ranjani Sridharan | e822cfa | 2018-06-27 20:40:18 -0700 | [diff] [blame] | 205 | /* input sample rate */ |
| 206 | case 'r': |
| 207 | fs_in = atoi(optarg); |
| 208 | break; |
| 209 | |
| 210 | /* output sample rate */ |
| 211 | case 'R': |
| 212 | fs_out = atoi(optarg); |
| 213 | break; |
| 214 | |
Ranjani Sridharan | 75bb4a6 | 2018-05-31 19:29:07 -0700 | [diff] [blame] | 215 | /* enable debug prints */ |
| 216 | case 'd': |
| 217 | debug = 1; |
| 218 | break; |
| 219 | |
| 220 | /* print usage */ |
| 221 | case 'h': |
| 222 | default: |
| 223 | print_usage(argv[0]); |
| 224 | exit(EXIT_FAILURE); |
| 225 | } |
| 226 | } |
Ranjani Sridharan | 528ec8f | 2018-06-27 20:42:02 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | int main(int argc, char **argv) |
| 230 | { |
| 231 | struct ipc_comp_dev *pcm_dev; |
| 232 | struct pipeline *p; |
| 233 | struct sof_ipc_pipe_new *ipc_pipe; |
| 234 | struct comp_dev *cd; |
| 235 | struct file_comp_data *frcd, *fwcd; |
| 236 | char pipeline[DEBUG_MSG_LEN]; |
| 237 | clock_t tic, toc; |
| 238 | double c_realtime, t_exec; |
| 239 | int n_in, n_out, ret; |
| 240 | int i; |
| 241 | |
| 242 | /* initialize input and output sample rates */ |
| 243 | fs_in = 0; |
| 244 | fs_out = 0; |
| 245 | |
| 246 | /* set up shared library look up table */ |
| 247 | ret = set_up_library_table(); |
| 248 | if (ret < 0) { |
| 249 | fprintf(stderr, "error: setting up shared libraried\n"); |
| 250 | exit(EXIT_FAILURE); |
| 251 | } |
| 252 | |
| 253 | /* set up trace class definition table from trace header */ |
Ranjani Sridharan | 7166305 | 2018-07-02 21:51:13 -0700 | [diff] [blame] | 254 | ret = setup_trace_table(); |
| 255 | if (ret < 0) { |
| 256 | fprintf(stderr, "error: setting up trace header table\n"); |
| 257 | exit(EXIT_FAILURE); |
| 258 | } |
Ranjani Sridharan | 528ec8f | 2018-06-27 20:42:02 -0700 | [diff] [blame] | 259 | /* command line arguments*/ |
| 260 | parse_input_args(argc, argv); |
Ranjani Sridharan | 75bb4a6 | 2018-05-31 19:29:07 -0700 | [diff] [blame] | 261 | |
| 262 | /* check args */ |
Ranjani Sridharan | f15512e | 2018-06-27 20:14:57 -0700 | [diff] [blame] | 263 | if (!tplg_file || !input_file || !output_file || !bits_in) { |
Ranjani Sridharan | 75bb4a6 | 2018-05-31 19:29:07 -0700 | [diff] [blame] | 264 | print_usage(argv[0]); |
| 265 | exit(EXIT_FAILURE); |
| 266 | } |
| 267 | |
| 268 | /* initialize ipc and scheduler */ |
| 269 | if (tb_pipeline_setup(&sof) < 0) { |
| 270 | fprintf(stderr, "error: pipeline init\n"); |
| 271 | exit(EXIT_FAILURE); |
| 272 | } |
| 273 | |
| 274 | /* parse topology file and create pipeline */ |
| 275 | if (parse_topology(tplg_file, &sof, &fr_id, &fw_id, &sched_id, bits_in, |
Ranjani Sridharan | 03067c6 | 2018-06-27 15:09:25 -0700 | [diff] [blame] | 276 | input_file, output_file, lib_table, pipeline) < 0) { |
Ranjani Sridharan | 75bb4a6 | 2018-05-31 19:29:07 -0700 | [diff] [blame] | 277 | fprintf(stderr, "error: parsing topology\n"); |
| 278 | exit(EXIT_FAILURE); |
| 279 | } |
| 280 | |
| 281 | /* Get pointers to fileread and filewrite */ |
| 282 | pcm_dev = ipc_get_comp(sof.ipc, fw_id); |
| 283 | fwcd = comp_get_drvdata(pcm_dev->cd); |
| 284 | pcm_dev = ipc_get_comp(sof.ipc, fr_id); |
| 285 | frcd = comp_get_drvdata(pcm_dev->cd); |
| 286 | |
| 287 | /* Run pipeline until EOF from fileread */ |
| 288 | pcm_dev = ipc_get_comp(sof.ipc, sched_id); |
| 289 | p = pcm_dev->cd->pipeline; |
| 290 | ipc_pipe = &p->ipc_pipe; |
| 291 | |
Ranjani Sridharan | e822cfa | 2018-06-27 20:40:18 -0700 | [diff] [blame] | 292 | /* input and output sample rate */ |
| 293 | if (!fs_in) |
| 294 | fs_in = ipc_pipe->deadline * ipc_pipe->frames_per_sched; |
| 295 | |
| 296 | if (!fs_out) |
| 297 | fs_out = ipc_pipe->deadline * ipc_pipe->frames_per_sched; |
Ranjani Sridharan | 75bb4a6 | 2018-05-31 19:29:07 -0700 | [diff] [blame] | 298 | |
| 299 | /* set pipeline params and trigger start */ |
| 300 | if (tb_pipeline_start(sof.ipc, TESTBENCH_NCH, bits_in, ipc_pipe) < 0) { |
| 301 | fprintf(stderr, "error: pipeline params\n"); |
| 302 | exit(EXIT_FAILURE); |
| 303 | } |
| 304 | |
| 305 | cd = pcm_dev->cd; |
| 306 | tb_enable_trace(false); /* reduce trace output */ |
| 307 | tic = clock(); |
| 308 | |
| 309 | while (frcd->fs.reached_eof == 0) |
| 310 | pipeline_schedule_copy(p, 0); |
| 311 | |
| 312 | if (!frcd->fs.reached_eof) |
| 313 | printf("warning: possible pipeline xrun\n"); |
| 314 | |
| 315 | /* reset and free pipeline */ |
| 316 | toc = clock(); |
| 317 | tb_enable_trace(true); |
| 318 | ret = pipeline_reset(p, cd); |
| 319 | if (ret < 0) { |
| 320 | fprintf(stderr, "error: pipeline reset\n"); |
| 321 | exit(EXIT_FAILURE); |
| 322 | } |
| 323 | |
| 324 | n_in = frcd->fs.n; |
| 325 | n_out = fwcd->fs.n; |
| 326 | t_exec = (double)(toc - tic) / CLOCKS_PER_SEC; |
Ranjani Sridharan | e822cfa | 2018-06-27 20:40:18 -0700 | [diff] [blame] | 327 | c_realtime = (double)n_out / TESTBENCH_NCH / fs_out / t_exec; |
Ranjani Sridharan | 75bb4a6 | 2018-05-31 19:29:07 -0700 | [diff] [blame] | 328 | |
| 329 | /* free all components/buffers in pipeline */ |
| 330 | free_comps(); |
| 331 | |
| 332 | /* free trace class defs */ |
| 333 | free_trace_table(); |
| 334 | |
| 335 | /* print test summary */ |
| 336 | printf("==========================================================\n"); |
| 337 | printf(" Test Summary\n"); |
| 338 | printf("==========================================================\n"); |
| 339 | printf("Test Pipeline:\n"); |
| 340 | printf("%s\n", pipeline); |
| 341 | printf("Input bit format: %s\n", bits_in); |
Ranjani Sridharan | e822cfa | 2018-06-27 20:40:18 -0700 | [diff] [blame] | 342 | printf("Input sample rate: %d\n", fs_in); |
| 343 | printf("Output sample rate: %d\n", fs_out); |
Ranjani Sridharan | 75bb4a6 | 2018-05-31 19:29:07 -0700 | [diff] [blame] | 344 | printf("Output written to file: \"%s\"\n", output_file); |
| 345 | printf("Input sample count: %d\n", n_in); |
| 346 | printf("Output sample count: %d\n", n_out); |
| 347 | printf("Total execution time: %.2f us, %.2f x realtime\n", |
| 348 | 1e3 * t_exec, c_realtime); |
| 349 | |
| 350 | /* free all other data */ |
| 351 | free(bits_in); |
| 352 | free(input_file); |
| 353 | free(tplg_file); |
| 354 | free(output_file); |
| 355 | |
Ranjani Sridharan | 03067c6 | 2018-06-27 15:09:25 -0700 | [diff] [blame] | 356 | /* close shared library objects */ |
| 357 | for (i = 0; i < NUM_WIDGETS_SUPPORTED; i++) { |
| 358 | if (lib_table[i].handle) |
| 359 | dlclose(lib_table[i].handle); |
| 360 | } |
Ranjani Sridharan | 75bb4a6 | 2018-05-31 19:29:07 -0700 | [diff] [blame] | 361 | |
| 362 | return EXIT_SUCCESS; |
| 363 | } |