comparison src/coder.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
comparison
equal deleted inserted replaced
-1:000000000000 0:56410792419a
1 /***************************************************************************
2 *
3 * FILE NAME: CODER.C
4 *
5 * Main program of the EFR coder at 12.2 kbit/s.
6 *
7 * Usage : coder speech_file bitstream_file
8 *
9 * Format for speech_file:
10 * Speech is read from a binary file of 16 bits data.
11 *
12 * Format for bitstream_file:
13 * 244 words (2-byte) containing 244 bits.
14 * Bit 0 = 0x0000 and Bit 1 = 0x0001
15 * One word (2-byte) for voice activity decision (VAD) flag bit
16 * 0x0000 -> inactive (no detected speech activity);
17 * 0x0001 -> active
18 * One word (2-byte) for speech (SP) flag bit
19 * 0x0000 -> inactive (no transmission of speech frames);
20 * 0x0001 -> active
21 *
22 ***************************************************************************/
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "typedef.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 "n_stack.h"
34 #include "e_homing.h"
35
36 #include "dtx.h"
37
38 Word16 dtx_mode;
39 extern Word16 txdtx_ctrl;
40
41 /* L_FRAME, M, PRM_SIZE, AZ_SIZE, SERIAL_SIZE: defined in "cnst.h" */
42
43 int main (int argc, char *argv[])
44 {
45 FILE *f_speech; /* File of speech data */
46 FILE *f_serial; /* File of serial bits for transmission */
47
48 extern Word16 *new_speech; /* Pointer to new speech data */
49
50 Word16 prm[PRM_SIZE]; /* Analysis parameters. */
51 Word16 serial[SERIAL_SIZE-1];/* Output bitstream buffer */
52 Word16 syn[L_FRAME]; /* Buffer for synthesis speech */
53
54 Word16 frame;
55
56 Word16 vad, sp;
57
58 Word16 reset_flag;
59 Word16 i;
60
61 proc_head ("Encoder");
62
63 /*----------------------------------------------------------------------*
64 * Open speech file and result file (output serial bit stream) *
65 *----------------------------------------------------------------------*/
66
67 if ((argc < 3) || (argc > 4))
68 {
69 fprintf (stderr,
70 " Usage:\n\n coder speech_file bitstream_file <dtx|nodtx>\n");
71 fprintf (stderr, "\n");
72 exit (1);
73 }
74 if ((f_speech = fopen (argv[1], "rb")) == NULL)
75 {
76 fprintf (stderr, "Error opening input file %s !!\n", argv[1]);
77 exit (0);
78 }
79 fprintf (stderr, " Input speech file: %s\n", argv[1]);
80
81 if ((f_serial = fopen (argv[2], "wb")) == NULL)
82 {
83 fprintf (stderr,"Error opening output bitstream file %s !!\n",argv[2]);
84 exit (0);
85 }
86 fprintf (stderr, " Output bitstream file: %s\n", argv[2]);
87
88 dtx_mode = 0; /* DTX disabled by default */
89
90 if (argc == 4)
91 {
92 if (strcmp (argv[3], "nodtx") == 0)
93 {
94 dtx_mode = 0;
95 }
96 else if (strcmp (argv[3], "dtx") == 0)
97 {
98 dtx_mode = 1;
99 }
100 else
101 {
102 fprintf (stderr, "\nWrong DTX switch: %s !!\n", argv[3]);
103 exit (1);
104 }
105 }
106 if (dtx_mode == 1)
107 {
108 fprintf (stderr, " DTX: enabled\n");
109 }
110 else
111 {
112 fprintf (stderr, " DTX: disabled\n");
113 }
114
115 /*-----------------------------------------------------------------------*
116 * Initialisation of the coder. *
117 *-----------------------------------------------------------------------*/
118
119 reset_enc (); /* Bring the encoder, VAD and DTX to the initial state */
120
121 Init_WMOPS_counter ();
122
123 /* Loop for each "L_FRAME" speech data. */
124
125 frame = 0;
126 while (fread (new_speech, sizeof (Word16), L_FRAME, f_speech) == L_FRAME)
127 {
128 #if(WMOPS)
129 fprintf (stderr, "frame=%d ", ++frame);
130 #else
131 fprintf (stderr, "\nframe=%d ", ++frame);
132 #endif
133
134 /* Check whether this frame is an encoder homing frame */
135 reset_flag = encoder_homing_frame_test (new_speech);
136
137 #if (WMOPS)
138 Reset_WMOPS_counter (); /* reset WMOPS counter for the new frame */
139 #endif
140
141 for (i = 0; i < L_FRAME; i++) /* Delete the 3 LSBs (13-bit input) */
142 {
143 new_speech[i] = new_speech[i] & 0xfff8; logic16 (); move16 ();
144 }
145
146 Pre_Process (new_speech, L_FRAME); /* filter + downscaling */
147
148 #if (WMOPS)
149 fwc (); /* function worst case */
150 #endif
151
152 Coder_12k2 (prm, syn); /* Find speech parameters */
153
154 test (); logic16 ();
155 if ((txdtx_ctrl & TX_SP_FLAG) == 0)
156 {
157 /* Write comfort noise parameters into the parameter frame.
158 Use old parameters in case SID frame is not to be updated */
159 CN_encoding (prm, txdtx_ctrl);
160 }
161 Prm2bits_12k2 (prm, &serial[0]); /* Parameters to serial bits */
162
163 #if (WMOPS)
164 fwc (); /* function worst case */
165 #endif
166
167 test (); logic16 ();
168 if ((txdtx_ctrl & TX_SP_FLAG) == 0)
169 {
170 /* Insert SID codeword into the serial parameter frame */
171 sid_codeword_encoding (&serial[0]);
172 }
173
174 #if (WMOPS)
175 fwc (); /* function worst case */
176 #endif
177
178 #if (WMOPS)
179 WMOPS_output (dtx_mode);/* output speech encoder WMOPS values
180 for current frame */
181 #endif
182
183 /* Write the bit stream to file */
184 fwrite (serial, sizeof (Word16), (SERIAL_SIZE-1), f_serial);
185
186 /* Write the VAD- and SP-flags to file after the speech
187 parameter bit stream */
188 vad = 0;
189 sp = 0;
190
191 if ((txdtx_ctrl & TX_VAD_FLAG) != 0)
192 {
193 vad = 1;
194 }
195 if ((txdtx_ctrl & TX_SP_FLAG) != 0)
196 {
197 sp = 1;
198 }
199 fwrite (&vad, sizeof (Word16), 1, f_serial);
200 fwrite (&sp, sizeof (Word16), 1, f_serial);
201
202 if (reset_flag != 0)
203 {
204 reset_enc (); /*Bring the encoder, VAD and DTX to the home state */
205 }
206 }
207
208 return (0);
209 }