xref: /aosp_15_r20/external/libopus/dnn/dred_decoder.c (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1 /* Copyright (c) 2022 Amazon
2    Written by Jan Buethe */
3 /*
4    Redistribution and use in source and binary forms, with or without
5    modification, are permitted provided that the following conditions
6    are met:
7 
8    - Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 
11    - Redistributions in binary form must reproduce the above copyright
12    notice, this list of conditions and the following disclaimer in the
13    documentation and/or other materials provided with the distribution.
14 
15    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #include <string.h>
29 
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 
34 #include "os_support.h"
35 #include "dred_decoder.h"
36 #include "dred_coding.h"
37 #include "celt/entdec.h"
38 #include "celt/laplace.h"
39 #include "dred_rdovae_stats_data.h"
40 #include "dred_rdovae_constants.h"
41 
dred_decode_latents(ec_dec * dec,float * x,const opus_uint8 * scale,const opus_uint8 * r,const opus_uint8 * p0,int dim)42 static void dred_decode_latents(ec_dec *dec, float *x, const opus_uint8 *scale, const opus_uint8 *r, const opus_uint8 *p0, int dim) {
43     int i;
44     for (i=0;i<dim;i++) {
45         int q;
46         if (r[i] == 0 || p0[i] == 255) q = 0;
47         else q = ec_laplace_decode_p0(dec, p0[i]<<7, r[i]<<7);
48         x[i] = q*256.f/(scale[i] == 0 ? 1 : scale[i]);
49     }
50 }
51 
dred_ec_decode(OpusDRED * dec,const opus_uint8 * bytes,int num_bytes,int min_feature_frames,int dred_frame_offset)52 int dred_ec_decode(OpusDRED *dec, const opus_uint8 *bytes, int num_bytes, int min_feature_frames, int dred_frame_offset)
53 {
54   ec_dec ec;
55   int q_level;
56   int i;
57   int offset;
58   int q0;
59   int dQ;
60   int qmax;
61   int state_qoffset;
62   int extra_offset;
63 
64   /* since features are decoded in quadruples, it makes no sense to go with an uneven number of redundancy frames */
65   celt_assert(DRED_NUM_REDUNDANCY_FRAMES % 2 == 0);
66 
67   /* decode initial state and initialize RDOVAE decoder */
68   ec_dec_init(&ec, (unsigned char*)bytes, num_bytes);
69   q0 = ec_dec_uint(&ec, 16);
70   dQ = ec_dec_uint(&ec, 8);
71   if (ec_dec_uint(&ec, 2)) extra_offset = 32*ec_dec_uint(&ec, 256);
72   else extra_offset = 0;
73   /* Compute total offset, including DRED position in a multiframe packet. */
74   dec->dred_offset = 16 - ec_dec_uint(&ec, 32) - extra_offset + dred_frame_offset;
75   /*printf("%d %d %d\n", dred_offset, q0, dQ);*/
76   qmax = 15;
77   if (q0 < 14 && dQ > 0) {
78     int nvals;
79     int ft;
80     int s;
81     /* The distribution for the dQmax symbol is split evenly between zero
82         (which implies qmax == 15) and larger values, with the probability of
83         all larger values being uniform.
84        This is equivalent to coding 1 bit to decide if the maximum is less than
85         15 followed by a uint to decide the actual value if it is less than
86         15, but combined into a single symbol. */
87     nvals = 15 - (q0 + 1);
88     ft = 2*nvals;
89     s = ec_decode(&ec, ft);
90     if (s >= nvals) {
91       qmax = q0 + (s - nvals) + 1;
92       ec_dec_update(&ec, s, s + 1, ft);
93     }
94     else {
95       ec_dec_update(&ec, 0, nvals, ft);
96     }
97   }
98   state_qoffset = q0*DRED_STATE_DIM;
99   dred_decode_latents(
100       &ec,
101       dec->state,
102       dred_state_quant_scales_q8 + state_qoffset,
103       dred_state_r_q8 + state_qoffset,
104       dred_state_p0_q8 + state_qoffset,
105       DRED_STATE_DIM);
106 
107   /* decode newest to oldest and store oldest to newest */
108   for (i = 0; i < IMIN(DRED_NUM_REDUNDANCY_FRAMES, (min_feature_frames+1)/2); i += 2)
109   {
110       /* FIXME: Figure out how to avoid missing a last frame that would take up < 8 bits. */
111       if (8*num_bytes - ec_tell(&ec) <= 7)
112          break;
113       q_level = compute_quantizer(q0, dQ, qmax, i/2);
114       offset = q_level*DRED_LATENT_DIM;
115       dred_decode_latents(
116           &ec,
117           &dec->latents[(i/2)*DRED_LATENT_DIM],
118           dred_latent_quant_scales_q8 + offset,
119           dred_latent_r_q8 + offset,
120           dred_latent_p0_q8 + offset,
121           DRED_LATENT_DIM
122           );
123 
124       offset = 2 * i * DRED_NUM_FEATURES;
125   }
126   dec->process_stage = 1;
127   dec->nb_latents = i/2;
128   return i/2;
129 }
130