xref: /aosp_15_r20/external/libopus/dnn/write_lpcnet_weights.c (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1 /* Copyright (c) 2023 Amazon */
2 /*
3    Redistribution and use in source and binary forms, with or without
4    modification, are permitted provided that the following conditions
5    are met:
6 
7    - Redistributions of source code must retain the above copyright
8    notice, this list of conditions and the following disclaimer.
9 
10    - Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 
14    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
18    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26 
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 
31 #include <stdio.h>
32 #include <string.h>
33 #include <stddef.h>
34 #include "nnet.h"
35 #include "os_support.h"
36 #include "arch.h"
37 
38 /* This is a bit of a hack because we need to build nnet_data.c and plc_data.c without USE_WEIGHTS_FILE,
39    but USE_WEIGHTS_FILE is defined in config.h. */
40 #undef HAVE_CONFIG_H
41 #ifdef USE_WEIGHTS_FILE
42 #undef USE_WEIGHTS_FILE
43 #endif
44 #include "pitchdnn_data.c"
45 #include "fargan_data.c"
46 #include "plc_data.c"
47 #include "dred_rdovae_enc_data.c"
48 #include "dred_rdovae_dec_data.c"
49 #ifdef ENABLE_OSCE
50 #include "lace_data.c"
51 #include "nolace_data.c"
52 #endif
53 
write_weights(const WeightArray * list,FILE * fout)54 void write_weights(const WeightArray *list, FILE *fout)
55 {
56   int i=0;
57   unsigned char zeros[WEIGHT_BLOCK_SIZE] = {0};
58   while (list[i].name != NULL) {
59     WeightHead h;
60     if (strlen(list[i].name) >= sizeof(h.name) - 1) {
61       printf("[write_weights] warning: name %s too long\n", list[i].name);
62     }
63     memcpy(h.head, "DNNw", 4);
64     h.version = WEIGHT_BLOB_VERSION;
65     h.type = list[i].type;
66     h.size = list[i].size;
67     h.block_size = (h.size+WEIGHT_BLOCK_SIZE-1)/WEIGHT_BLOCK_SIZE*WEIGHT_BLOCK_SIZE;
68     OPUS_CLEAR(h.name, sizeof(h.name));
69     strncpy(h.name, list[i].name, sizeof(h.name));
70     h.name[sizeof(h.name)-1] = 0;
71     celt_assert(sizeof(h) == WEIGHT_BLOCK_SIZE);
72     fwrite(&h, 1, WEIGHT_BLOCK_SIZE, fout);
73     fwrite(list[i].data, 1, h.size, fout);
74     fwrite(zeros, 1, h.block_size-h.size, fout);
75     i++;
76   }
77 }
78 
main(void)79 int main(void)
80 {
81   FILE *fout = fopen("weights_blob.bin", "w");
82   write_weights(pitchdnn_arrays, fout);
83   write_weights(fargan_arrays, fout);
84   write_weights(plcmodel_arrays, fout);
85   write_weights(rdovaeenc_arrays, fout);
86   write_weights(rdovaedec_arrays, fout);
87 #ifdef ENABLE_OSCE
88 #ifndef DISABLE_LACE
89   write_weights(lacelayers_arrays, fout);
90 #endif
91 #ifndef DISABLE_NOLACE
92   write_weights(nolacelayers_arrays, fout);
93 #endif
94 #endif
95   fclose(fout);
96   return 0;
97 }
98