comparison libtwamr/dec_main.c @ 427:357d1faad55d

libtwamr: implement decoder top level
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 07 May 2024 21:45:47 +0000
parents libtwamr/enc_main.c@2a094af3d384
children
comparison
equal deleted inserted replaced
426:7bef001cd8b8 427:357d1faad55d
1 /*
2 * This C module is the top level entity for our stateful decoder engine.
3 */
4
5 #include <stdint.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include "tw_amr.h"
9 #include "namespace.h"
10 #include "typedef.h"
11 #include "cnst.h"
12 #include "dec_amr.h"
13 #include "pstfilt.h"
14 #include "post_pro.h"
15 #include "bitno.h"
16
17 struct amr_decoder_state {
18 Decoder_amrState dec;
19 Post_FilterState pstfilt;
20 Post_ProcessState posthp;
21 enum Mode prev_mode;
22 Flag is_homed;
23 };
24
25 struct amr_decoder_state *amr_decoder_create(void)
26 {
27 struct amr_decoder_state *st;
28
29 st = malloc(sizeof(struct amr_decoder_state));
30 if (st)
31 amr_decoder_reset(st);
32 return st;
33 }
34
35 void amr_decoder_reset(struct amr_decoder_state *st)
36 {
37 Decoder_amr_reset(&st->dec, 0);
38 Post_Filter_reset(&st->pstfilt);
39 Post_Process_reset(&st->posthp);
40 st->prev_mode = (enum Mode) 0;
41 st->is_homed = 1;
42 }
43
44 void amr_decode_frame(struct amr_decoder_state *st,
45 const struct amr_param_frame *frame, int16_t *pcm)
46 {
47 enum Mode mode;
48 Word16 parm[MAX_PRM_SIZE];
49 Word16 Az_dec[AZ_SIZE];
50 Word16 i;
51
52 /* fast home state handling needs to be first */
53 if (st->is_homed && amr_check_dhf(frame, 1)) {
54 for (i = 0; i < L_FRAME; i++)
55 pcm[i] = EHF_MASK;
56 return;
57 }
58 /* "unpack" into internal form */
59 if (frame->type == RX_NO_DATA)
60 mode = st->prev_mode;
61 else {
62 mode = frame->mode & 7;
63 st->prev_mode = mode;
64 }
65 memcpy(parm, frame->param, prmno[mode] * sizeof(int16_t));
66 /* now we can call the guts of the decoder */
67 Decoder_amr(&st->dec, mode, parm, frame->type, pcm, Az_dec);
68 Post_Filter(&st->pstfilt, mode, pcm, Az_dec);
69 Post_Process(&st->posthp, pcm, L_FRAME);
70 /*
71 * The 3 lsbs of each speech sample typically won't be all 0
72 * out of Post_Process(), hence we have to clear them explicitly
73 * to maintain overall bit-exact operation.
74 */
75 for (i = 0; i < L_FRAME; i++)
76 pcm[i] &= 0xFFF8;
77 /* final check for full DHF */
78 if (amr_check_dhf(frame, 0))
79 amr_decoder_reset(st);
80 else
81 st->is_homed = 0;
82 }