comparison src/decoder.c @ 0:56410792419a

src: original EFR source from ETSI
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 03 Apr 2024 05:31:37 +0000
parents
children 799b56cbccb6
comparison
equal deleted inserted replaced
-1:000000000000 0:56410792419a
1 /***************************************************************************
2 *
3 * FILE NAME: decoder.c
4 *
5 * Usage : decoder bitstream_file synth_file
6 *
7 * Format for bitstream_file:
8 * One word (2-byte) for bad frame indication (BFI) flag bit
9 * 0x0000 -> good frame; 0x0001 -> bad frame
10 * 244 words (2-byte) containing 244 bits.
11 * Bit 0 = 0x0000 and Bit 1 = 0x0001
12 * One word (2-byte) for ternary Silence Descriptor (SID) flag
13 * 0x0000 -> inactive (no detected speech activity);
14 * 0x0001 -> active
15 * One word (2-byte) for Time Alignment Flag (TAF) bit
16 * 0x0000 -> inactive (no transmission of speech frames);
17 * 0x0001 -> active
18 *
19 * Format for synth_file:
20 * Synthesis is written to a binary file of 16 bits data.
21 *
22 ***************************************************************************/
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include "typedef.h"
27 #include "n_stack.h"
28 #include "basic_op.h"
29 #include "sig_proc.h"
30 #include "count.h"
31 #include "codec.h"
32 #include "cnst.h"
33 #include "d_homing.h"
34
35
36 /* These constants define the number of consecutive parameters
37 that function decoder_homing_frame_test() checks */
38
39 #define WHOLE_FRAME 57
40 #define TO_FIRST_SUBFRAME 18
41
42
43 Word16 synth_buf[L_FRAME + M];
44
45 /* L_FRAME, M, PRM_SIZE, AZ_SIZE, SERIAL_SIZE: defined in "cnst.h" */
46
47 /*-----------------------------------------------------------------*
48 * Global variables *
49 *-----------------------------------------------------------------*/
50
51 #if (WMOPS)
52 Word16 dtx_mode = 0;
53
54 #endif
55
56 /*-----------------------------------------------------------------*
57 * Main decoder routine *
58 *-----------------------------------------------------------------*/
59
60 int
61 main (int argc, char *argv[])
62 {
63 Word16 *synth; /* Synthesis */
64 Word16 parm[PRM_SIZE + 1]; /* Synthesis parameters */
65 Word16 serial[SERIAL_SIZE+2];/* Serial stream */
66 Word16 Az_dec[AZ_SIZE]; /* Decoded Az for post-filter */
67 /* in 4 subframes, length= 44 */
68 Word16 i, frame, temp;
69 FILE *f_syn, *f_serial;
70
71 Word16 TAF, SID_flag;
72
73 Word16 reset_flag;
74 static Word16 reset_flag_old = 1;
75
76 proc_head ("Decoder");
77
78 /*-----------------------------------------------------------------*
79 * Read passed arguments and open in/out files *
80 *-----------------------------------------------------------------*/
81
82 if (argc != 3)
83 {
84 fprintf (stderr,
85 " Usage:\n\n decoder bitstream_file synth_file\n");
86 fprintf (stderr, "\n");
87 exit (1);
88 }
89 /* Open file for synthesis and packed serial stream */
90
91 if ((f_serial = fopen (argv[1], "rb")) == NULL)
92 {
93 fprintf (stderr, "Input file '%s' does not exist !!\n", argv[1]);
94 exit (0);
95 }
96 else
97 fprintf (stderr, "Input bitstream file: %s\n", argv[1]);
98
99 if ((f_syn = fopen (argv[2], "wb")) == NULL)
100 {
101 fprintf (stderr, "Cannot open file '%s' !!\n", argv[2]);
102 exit (0);
103 }
104 else
105 fprintf (stderr, "Synthesis speech file: %s\n", argv[2]);
106
107 /*-----------------------------------------------------------------*
108 * Initialization of decoder *
109 *-----------------------------------------------------------------*/
110
111 synth = synth_buf + M;
112
113 reset_dec (); /* Bring the decoder and receive DTX to the initial state */
114
115 #if (WMOPS)
116 Init_WMOPS_counter ();
117 #endif
118
119 /*-----------------------------------------------------------------*
120 * Loop for each "L_FRAME" speech data *
121 *-----------------------------------------------------------------*/
122
123 frame = 0;
124
125 while (fread (serial, sizeof (Word16), 247, f_serial) == 247)
126 {
127 #if (WMOPS)
128 fprintf (stderr, "frame=%d ", ++frame);
129 #else
130 fprintf (stderr, "\nframe=%d ", ++frame);
131 #endif
132
133 #if (WMOPS)
134 Reset_WMOPS_counter (); /* reset WMOPS counter for the new frame */
135 #endif
136
137 SID_flag = serial[245]; /* Receive SID flag */
138 TAF = serial[246]; /* Receive TAF flag */
139
140 Bits2prm_12k2 (serial, parm); /* serial to parameters */
141
142 #if (WMOPS)
143 fwc (); /* function worst case */
144 #endif
145
146 if (parm[0] == 0) /* BFI == 0, perform DHF check */
147 {
148 if (reset_flag_old == 1) /* Check for second and further
149 successive DHF (to first subfr.) */
150 {
151 reset_flag = decoder_homing_frame_test (&parm[1],
152 TO_FIRST_SUBFRAME);
153 }
154 else
155 {
156 reset_flag = 0;
157 }
158 }
159 else /* BFI==1, bypass DHF check (frame
160 is taken as not being a DHF) */
161 {
162 reset_flag = 0;
163 }
164
165 if ((reset_flag != 0) && (reset_flag_old != 0))
166 {
167 /* Force the output to be the encoder homing frame pattern */
168
169 for (i = 0; i < L_FRAME; i++)
170 {
171 synth[i] = EHF_MASK;
172 }
173 }
174 else
175 {
176 Decoder_12k2 (parm, synth, Az_dec, TAF, SID_flag);/* Synthesis */
177
178 Post_Filter (synth, Az_dec); /* Post-filter */
179 #if (WMOPS)
180 fwc (); /* function worst case */
181 #endif
182
183 for (i = 0; i < L_FRAME; i++)
184 /* Upscale the 15 bit linear PCM to 16 bits,
185 then truncate to 13 bits */
186 {
187 temp = shl (synth[i], 1);
188 synth[i] = temp & 0xfff8; logic16 (); move16 ();
189 }
190
191 #if (WMOPS)
192 fwc (); /* function worst case */
193 #endif
194
195 #if (WMOPS)
196 WMOPS_output (dtx_mode);/* output WMOPS values for current frame */
197 #endif
198 } /* else */
199
200 fwrite (synth, sizeof (Word16), L_FRAME, f_syn);
201
202 /* BFI == 0, perform check for first DHF (whole frame) */
203 if ((parm[0] == 0) && (reset_flag_old == 0))
204 {
205 reset_flag = decoder_homing_frame_test (&parm[1], WHOLE_FRAME);
206 }
207
208 if (reset_flag != 0)
209 {
210 /* Bring the decoder and receive DTX to the home state */
211 reset_dec ();
212 }
213 reset_flag_old = reset_flag;
214
215 } /* while */
216
217 return 0;
218 }