1function blob8 = eq_fir_blob_pack(bs, ipc_ver, endian)
2
3%% Pack equalizer struct to bytes
4%
5% blob8 = eq_fir_blob_pack(bs, ipc_ver, endian)
6% bs - blob struct
7% ipc_ver - optional, use 3 or 4. Default is 3.
8% endian - optional, use 'little' or 'big'. Defaults to little.
9%
10
11% SPDX-License-Identifier: BSD-3-Clause
12%
13% Copyright(c) 2016 Intel Corporation. All rights reserved.
14%
15% Author: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
16
17if nargin < 2
18	ipc_ver = 3;
19end
20
21if nargin < 3
22	endian = 'little';
23end
24
25%% Endianness of blob
26switch lower(endian)
27        case 'little'
28                sh16 = [0 -8];
29                sh32 = [0 -8 -16 -24];
30        case 'big'
31                sh16 = [-8 0];
32                sh32 = [-24 -16 -8 0];
33        otherwise
34                error('Unknown endianness');
35end
36
37%% Channels count must be even
38if mod(bs.channels_in_config, 2) > 0
39	error("Channels # must be even");
40end
41
42%% Channels count and assign vector length must be the same
43if bs.channels_in_config ~= length( bs.assign_response)
44	error("Channels # and response assign length must match");
45end
46
47%% Coefficients vector length must be multiple of 4
48len = length(bs.all_coefficients);
49len_no_header = len - 10 * bs.number_of_responses_defined;
50if mod(len_no_header, 4) > 0
51	error("Coefficient data vector length must be multiple of 4");
52end
53
54%% Header format is
55%	uint32_t size;
56%	uint16_t channels_in_config;
57%	uint16_t number_of_responses;
58%	uint32_t reserved[4];
59%	int16_t data[];
60
61%% Pack as 16 bits
62nh16 = 12+bs.channels_in_config;
63h16 = zeros(1, nh16, 'int16');
64nc16 = length(bs.all_coefficients);
65nb16 = ceil((nh16+nc16)/2)*2;
66h16(1) = 2 * nb16;
67h16(2) = 0;
68h16(3) = bs.channels_in_config;
69h16(4) = bs.number_of_responses_defined;
70h16(5) = 0;
71h16(6) = 0;
72h16(7) = 0;
73h16(8) = 0;
74h16(9) = 0;
75h16(10) = 0;
76h16(11) = 0;
77h16(12) = 0;
78for i=1:bs.channels_in_config
79        h16(12+i) = bs.assign_response(i);
80end
81
82%% Merge header and coefficients, make even number of int16 to make it
83%  multiple of int32
84blob16 = zeros(1,nb16, 'int16');
85blob16(1:nh16) = h16;
86blob16(nh16+1:nh16+nc16) = int16(bs.all_coefficients);
87
88%% Pack as 8 bits
89nbytes_data = nb16 * 2;
90
91%% Get ABI information
92[abi_bytes, nbytes_abi] = eq_get_abi(nbytes_data, ipc_ver);
93
94%% Initialize uint8 array with correct size
95nbytes = nbytes_abi + nbytes_data;
96blob8 = zeros(1, nbytes, 'uint8');
97
98%% Inset ABI header
99blob8(1:nbytes_abi) = abi_bytes;
100j = nbytes_abi + 1;
101
102%% Component data
103for i = 1:length(blob16)
104        blob8(j:j+1) = w16b(blob16(i), sh16);
105        j = j+2;
106end
107
108%% Done
109fprintf('Blob size is %d bytes.\n', nbytes);
110
111end
112
113function bytes = w16b(word, sh)
114bytes = uint8(zeros(1,2));
115bytes(1) = bitand(bitshift(word, sh(1)), 255);
116bytes(2) = bitand(bitshift(word, sh(2)), 255);
117end
118
119function bytes = w32b(word, sh)
120bytes = uint8(zeros(1,4));
121bytes(1) = bitand(bitshift(word, sh(1)), 255);
122bytes(2) = bitand(bitshift(word, sh(2)), 255);
123bytes(3) = bitand(bitshift(word, sh(3)), 255);
124bytes(4) = bitand(bitshift(word, sh(4)), 255);
125end
126