• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2003 - 2017 Sony Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "ldac.h"
18 
19 /***************************************************************************************************
20     Calculate Bits for Band Info
21 ***************************************************************************************************/
encode_band_info_ldac(AB * p_ab)22 static int encode_band_info_ldac(
23 __attribute__((unused)) AB *p_ab)
24 {
25     int	nbits;
26 
27     nbits = LDAC_NBANDBITS + LDAC_FLAGBITS;
28 
29     return nbits;
30 }
31 
32 /***************************************************************************************************
33     Calculate Bits for Gradient Data
34 ***************************************************************************************************/
encode_gradient_ldac(AB * p_ab)35 static int encode_gradient_ldac(
36 AB *p_ab)
37 {
38     int	nbits;
39 
40     if (p_ab->grad_mode == LDAC_MODE_0) {
41         nbits = LDAC_GRADMODEBITS + LDAC_GRADQU0BITS*2 + LDAC_GRADOSBITS*2 + LDAC_NADJQUBITS;
42     }
43     else {
44         nbits = LDAC_GRADMODEBITS + LDAC_GRADQU1BITS + LDAC_GRADOSBITS + LDAC_NADJQUBITS;
45     }
46 
47     return nbits;
48 }
49 
50 /***************************************************************************************************
51     Subfunction: Get Index of Minimum Value
52 ***************************************************************************************************/
get_minimum_id_ldac(int * p_nbits,int n)53 __inline static int get_minimum_id_ldac(
54 int *p_nbits,
55 int n)
56 {
57     int i;
58     int id, nbits;
59 
60     id = 0;
61     nbits = p_nbits[0];
62 
63     for (i = 1; i < n; i++) {
64         if (nbits > p_nbits[i]) {
65             id = i;
66             nbits = p_nbits[i];
67         }
68     }
69 
70     return id;
71 }
72 
73 typedef struct {
74     int bitlen;
75     int offset;
76     int weight;
77 } SFCINF;
78 
79 /***************************************************************************************************
80     Subfunction: Calculate Bits for Scale Factor Data - Mode 0
81 ***************************************************************************************************/
82 static const unsigned char sa_bitlen_maxdif_0_ldac[LDAC_NIDSF] = {
83     3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
84 };
85 
encode_scale_factor_0_ldac(AC * p_ac,SFCINF * p_sfcinf)86 static int encode_scale_factor_0_ldac(
87 AC *p_ac,
88 SFCINF *p_sfcinf)
89 {
90     HCENC *p_hcsf;
91     int iqu, iwt;
92     int nqus = p_ac->p_ab->nqus;
93     int nbits = LDAC_MAXBITNUM;
94     int bitlen, vmin, vmax, val0, val1;
95     int *p_idsf = p_ac->a_idsf;
96     int *p_idsf_dif = p_ac->a_tmp;
97     const unsigned char *p_tbl;
98 
99     for (iwt = 0; iwt < LDAC_NSFCWTBL; iwt++) {
100         p_tbl = gaa_sfcwgt_ldac[iwt];
101         vmin = vmax = val0 = p_idsf[0] + p_tbl[0];
102         for (iqu = 1; iqu < nqus; iqu++) {
103             val1 = p_idsf[iqu] + p_tbl[iqu];
104             if (vmin > val1) {
105                 vmin = val1;
106             }
107             if (vmax < val1) {
108                 vmax = val1;
109             }
110             p_idsf_dif[iqu] = val1 - val0;
111             val0 = val1;
112         }
113 
114         val1 = bitlen = sa_bitlen_maxdif_0_ldac[(vmax-vmin)>>1];
115         p_hcsf = ga_hcenc_sf0_ldac + (bitlen-LDAC_MINSFCBLEN_0);
116         for (iqu = 1; iqu < nqus; iqu++) {
117             val0 = p_idsf_dif[iqu] & p_hcsf->mask;
118             val1 += hc_len_ldac(p_hcsf->p_tbl+val0);
119         }
120 
121         if (nbits > val1) {
122             p_sfcinf->bitlen = bitlen;
123             p_sfcinf->offset = vmin;
124             p_sfcinf->weight = iwt;
125             nbits = val1;
126         }
127     }
128     nbits += LDAC_SFCBLENBITS + LDAC_IDSFBITS + LDAC_SFCWTBLBITS;
129 
130     return nbits;
131 }
132 
133 /***************************************************************************************************
134     Subfunction: Calculate Bits for Scale Factor Data - Mode 1
135 ***************************************************************************************************/
136 static const unsigned char sa_bitlen_maxdif_1_ldac[LDAC_NIDSF] = {
137     2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
138 };
139 
encode_scale_factor_1_ldac(AC * p_ac,SFCINF * p_sfcinf)140 static int encode_scale_factor_1_ldac(
141 AC *p_ac,
142 SFCINF *p_sfcinf)
143 {
144     int iqu, iwt;
145     int nqus = p_ac->p_ab->nqus;
146     int nbits = LDAC_MAXBITNUM;
147     int bitlen, vmin, vmax, val;
148     int *p_idsf = p_ac->a_idsf;
149     const unsigned char *p_tbl;
150 
151     for (iwt = 0; iwt < LDAC_NSFCWTBL; iwt++) {
152         p_tbl = gaa_sfcwgt_ldac[iwt];
153         vmin = vmax = p_idsf[0] + p_tbl[0];
154         for (iqu = 1; iqu < nqus; iqu++) {
155             val = p_idsf[iqu] + p_tbl[iqu];
156             if (vmin > val) {
157                 vmin = val;
158             }
159             if (vmax < val) {
160                 vmax = val;
161             }
162         }
163 
164         bitlen = sa_bitlen_maxdif_1_ldac[(vmax-vmin)>>1];
165         if (bitlen > 4) {
166             val = LDAC_SFCBLENBITS;
167         }
168         else {
169             val = LDAC_SFCBLENBITS + LDAC_IDSFBITS + LDAC_SFCWTBLBITS;
170         }
171         val += bitlen * nqus;
172 
173         if (nbits > val) {
174             p_sfcinf->bitlen = bitlen;
175             p_sfcinf->offset = vmin;
176             p_sfcinf->weight = iwt;
177             nbits = val;
178         }
179     }
180 
181     return nbits;
182 }
183 
184 /***************************************************************************************************
185     Subfunction: Calculate Bits for Scale Factor Data - Mode 2
186 ***************************************************************************************************/
187 static const unsigned char sa_bitlen_absmax_2_ldac[LDAC_NIDSF>>1] = {
188     2, 3, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
189 };
190 
encode_scale_factor_2_ldac(AC * p_ac,SFCINF * p_sfcinf)191 static int encode_scale_factor_2_ldac(
192 AC *p_ac,
193 SFCINF *p_sfcinf)
194 {
195     HCENC *p_hcsf;
196     int iqu;
197     int nqus = p_ac->p_ab->nqus;
198     int nbits, bitlen, vmax, val;
199     int *p_idsf_dif = p_ac->a_tmp;
200 
201     p_idsf_dif[0] = p_ac->a_idsf[0] - p_ac->p_ab->ap_ac[0]->a_idsf[0];
202     vmax = abs(p_idsf_dif[0]);
203     for (iqu = 1; iqu < nqus; iqu++) {
204         p_idsf_dif[iqu] = p_ac->a_idsf[iqu] - p_ac->p_ab->ap_ac[0]->a_idsf[iqu];
205         val = abs(p_idsf_dif[iqu]);
206         if (vmax < val) {
207             vmax = val;
208         }
209     }
210 
211     nbits = LDAC_SFCBLENBITS;
212     bitlen = sa_bitlen_absmax_2_ldac[vmax>>1];
213     p_hcsf = ga_hcenc_sf1_ldac + (bitlen-LDAC_MINSFCBLEN_2);
214     for (iqu = 0; iqu < nqus; iqu++) {
215         val = p_idsf_dif[iqu] & p_hcsf->mask;
216         nbits += hc_len_ldac(p_hcsf->p_tbl+val);
217     }
218 
219     p_sfcinf->bitlen = bitlen;
220     p_sfcinf->offset = 0;
221     p_sfcinf->weight = 0;
222 
223     return nbits;
224 }
225 
226 /***************************************************************************************************
227     Calculate Bits for Scale Factor Data
228 ***************************************************************************************************/
encode_scale_factor_ldac(AC * p_ac)229 static int encode_scale_factor_ldac(
230 AC *p_ac)
231 {
232     SFCINF a_sfcinf[LDAC_NSFCMODE];
233     SFCINF *p_sfcinf;
234     int nbits, sfc_mode;
235     int a_nbits[LDAC_NSFCMODE];
236 
237     if (p_ac->ich == 0) {
238         a_nbits[LDAC_MODE_0] = encode_scale_factor_0_ldac(p_ac, a_sfcinf+LDAC_MODE_0);
239         a_nbits[LDAC_MODE_1] = encode_scale_factor_1_ldac(p_ac, a_sfcinf+LDAC_MODE_1);
240     }
241     else {
242         a_nbits[LDAC_MODE_0] = encode_scale_factor_0_ldac(p_ac, a_sfcinf+LDAC_MODE_0);
243         a_nbits[LDAC_MODE_1] = encode_scale_factor_2_ldac(p_ac, a_sfcinf+LDAC_MODE_1);
244     }
245 
246     p_ac->sfc_mode = sfc_mode = get_minimum_id_ldac(a_nbits, LDAC_MODE_1+1);
247     p_sfcinf = a_sfcinf + sfc_mode;
248     p_ac->sfc_bitlen = p_sfcinf->bitlen;
249     p_ac->sfc_offset = p_sfcinf->offset;
250     p_ac->sfc_weight = p_sfcinf->weight;
251     nbits = a_nbits[sfc_mode] + LDAC_SFCMODEBITS;
252 
253     return nbits;
254 }
255 
256 /***************************************************************************************************
257     Calculate Bits for Side Information (Band Info, Gradient Data & Scale Factor Data)
258 ***************************************************************************************************/
encode_side_info_ldac(AB * p_ab)259 DECLFUNC int encode_side_info_ldac(
260 AB *p_ab)
261 {
262     AC *p_ac;
263     int ich;
264     int nchs = p_ab->blk_nchs;
265     int nbits, nbits_band, nbits_grad, nbits_scfc = 0;
266 
267     p_ab->nbits_band = nbits_band = encode_band_info_ldac(p_ab);
268     p_ab->nbits_grad = nbits_grad = encode_gradient_ldac(p_ab);
269     for (ich = 0; ich < nchs; ich++) {
270         p_ac = p_ab->ap_ac[ich];
271         nbits_scfc += encode_scale_factor_ldac(p_ac);
272         calc_add_word_length_ldac(p_ac);
273     }
274     p_ab->nbits_scfc = nbits_scfc;
275 
276     nbits = nbits_band + nbits_grad + nbits_scfc;
277 
278     return nbits;
279 }
280 
281 /***************************************************************************************************
282     Calculate Additional Word Length Data
283 ***************************************************************************************************/
calc_add_word_length_ldac(AC * p_ac)284 DECLFUNC void calc_add_word_length_ldac(
285 AC *p_ac)
286 {
287     int iqu;
288     int nqus = p_ac->p_ab->nqus;
289     int dif;
290     int *p_idsf = p_ac->a_idsf;
291     int *p_addwl = p_ac->a_addwl;
292 
293     clear_data_ldac(p_addwl, LDAC_MAXNQUS*sizeof(int));
294 
295     if (p_ac->p_ab->grad_mode != LDAC_MODE_0) {
296         for (iqu = 1; iqu < nqus; iqu++) {
297             dif = p_idsf[iqu] - p_idsf[iqu-1];
298 
299             if (dif > 0) {
300                 if (dif > 5) {
301                     p_addwl[iqu] += 5;
302                 }
303                 else if (dif > 4) {
304                     p_addwl[iqu] += 4;
305                 }
306                 else if (dif > 3) {
307                     p_addwl[iqu] += 3;
308                 }
309                 else if (dif > 2) {
310                     p_addwl[iqu] += 2;
311                 }
312                 else if (dif > 1) {
313                     p_addwl[iqu] += 1;
314                 }
315             }
316             else {
317                 if (dif < -5) {
318                     p_addwl[iqu-1] += 5;
319                 }
320                 else if (dif < -4) {
321                     p_addwl[iqu-1] += 4;
322                 }
323                 else if (dif < -3) {
324                     p_addwl[iqu-1] += 3;
325                 }
326                 else if (dif < -2) {
327                     p_addwl[iqu-1] += 2;
328                 }
329                 else if (dif < -1) {
330                     p_addwl[iqu-1] += 1;
331                 }
332             }
333         }
334     }
335 
336     return;
337 }
338 
339 
340