aboutsummaryrefslogtreecommitdiffstats
path: root/op25/gr-op25_repeater/lib/p25p1_voice_encode.cc
blob: d2fda1315eca7972d7ec9c49f759e4de72c8eb8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* -*- c++ -*- */
/* 
 * GNU Radio interface for Pavel Yazev's Project 25 IMBE Encoder/Decoder
 * 
 * Copyright 2009 Pavel Yazev E-mail: pyazev@gmail.com
 * Copyright 2009, 2010, 2011, 2012, 2013, 2014 KA1RBI
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gnuradio/io_signature.h>
#include "p25p1_voice_encode.h"

#include <vector>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include "imbe_vocoder/imbe_vocoder.h"
#include "p25_frame.h"
#include "op25_imbe_frame.h"

namespace gr {
  namespace op25_repeater {

/* bits to be preset to 1 for all transmitted frames */
static const bool ldu1_preset[] = {
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0-35 */
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1, /* 36-71 */
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 72-107 */
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0, /* 108-143 */
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 144-179 */
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0, /* 180-215 */
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 216-251 */
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0, /* 252-287 */
  0,0,0,0,0,0,0,0,0,0,0,0,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0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 1656-1691 */
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0, /* 1692-1727 */
};

static void clear_bits(bit_vector& v) {
	for (size_t i=0; i<v.size(); i++) {
		v[i]=0;
	}
}

p25p1_voice_encode::p25p1_voice_encode(bool verbose_flag, int stretch_amt, const op25_audio& udp, bool raw_vectors_flag, std::deque<uint8_t> &_output_queue) :
        op25audio(udp),
	frame_cnt(0),
	write_bufp(0),
	peak_amplitude(0),
	peak(0),
	samp_ct(0),
	codeword_ct(0),
	sampbuf_ct(0),
	stretch_count(0),
	output_queue(_output_queue),
	f_body(P25_VOICE_FRAME_SIZE),
	opt_dump_raw_vectors(raw_vectors_flag),
	opt_verbose(verbose_flag)
    {
	opt_stretch_amt = 0;
	if (stretch_amt < 0) {
		opt_stretch_sign = -1;
		opt_stretch_amt = 0 - stretch_amt;
	} else {
		opt_stretch_sign = 1;
		opt_stretch_amt = stretch_amt;
	}

	clear_bits(f_body);
    }

    /*
     * Our virtual destructor.
     */
    p25p1_voice_encode::~p25p1_voice_encode()
    {
    }

static const int STATS_INTERVAL = 20;
static const int SAMP_INTERVAL = 8192;

void p25p1_voice_encode::append_imbe_codeword(bit_vector& frame_body, int16_t frame_vector[], unsigned int& codeword_ct)
{
	voice_codeword cw(voice_codeword_sz);
	uint8_t obuf[P25_VOICE_FRAME_SIZE/2];
	// construct 144-bit codeword from 88 bits of parameters
	imbe_header_encode(cw, frame_vector[0], frame_vector[1], frame_vector[2], frame_vector[3], frame_vector[4], frame_vector[5], frame_vector[6], frame_vector[7]);

	// add codeword to voice data unit
	imbe_interleave(frame_body, cw, codeword_ct);

	// after the ninth and final codeword added, dispose of frame
	if (++codeword_ct >= nof_voice_codewords) {
		static const uint64_t hws[2] = { 0x293555ef2c653437LL, 0x293aba93bec26a2bLL };
                int ldu_type = frame_cnt & 1;	// set ldu_type = 0(LDU1) or 1(LDU2)
		const bool* ldu_preset = (ldu_type == 0) ? ldu1_preset : ldu2_preset;
		
		p25_setup_frame_header(frame_body, hws[ldu_type]);
		for (size_t i = 0; i < frame_body.size(); i++) {
			frame_body[i] = frame_body[i] | ldu_preset[i];
		}
		// finally, output the frame
		if (op25audio.enabled()) {
			// pack the bits into bytes, MSB first
			size_t obuf_ct = 0;
			for (uint32_t i = 0; i < P25_VOICE_FRAME_SIZE; i += 8) {
				uint8_t b = 
					(frame_body[i+0] << 7) +
					(frame_body[i+1] << 6) +
					(frame_body[i+2] << 5) +
					(frame_body[i+3] << 4) +
					(frame_body[i+4] << 3) +
					(frame_body[i+5] << 2) +
					(frame_body[i+6] << 1) +
					(frame_body[i+7]     );
				obuf[obuf_ct++] = b;
			}
			op25audio.send_to(obuf, obuf_ct);
		} else {
			for (uint32_t i = 0; i < P25_VOICE_FRAME_SIZE; i += 2) {
				uint8_t dibit = 
					(frame_body[i+0] << 1) +
					(frame_body[i+1]     );
				output_queue.push_back(dibit);
			}
		}
		codeword_ct = 0;
		frame_cnt++;
		if (opt_verbose && (frame_cnt % STATS_INTERVAL) == 0) {
			gettimeofday(&tv, &tz);
			int s = tv.tv_sec - oldtv.tv_sec;
			int us = tv.tv_usec - oldtv.tv_usec;
			if (us < 0) {
				us = us + 1000000;
				s  = s - 1;
			}
			float f = us;
			f /= 1000000;
			f += s;
			fprintf (stderr, "time %f peak %5d\n", f / STATS_INTERVAL, peak_amplitude);
			oldtv = tv;
		}
		clear_bits(f_body);
	}
}

void p25p1_voice_encode::compress_frame(int16_t snd[])
{
	int16_t frame_vector[8];	

	// encode 160 audio samples into 88 bits (u0-u7)
	vocoder.imbe_encode(frame_vector, snd);

	// if dump option, dump u0-u7 to output
	if (opt_dump_raw_vectors) {
		char s[128];
		sprintf(s, "%03x %03x %03x %03x %03x %03x %03x %03x\n", frame_vector[0], frame_vector[1], frame_vector[2], frame_vector[3], frame_vector[4], frame_vector[5], frame_vector[6], frame_vector[7]);
		memcpy(&write_buf[write_bufp], s, strlen(s));
		write_bufp += strlen(s);
		if (write_bufp >= 288) {
			op25audio.send_to(write_buf, 288);
			write_bufp = 0;
		}
		return;
	}
	append_imbe_codeword(f_body, frame_vector, codeword_ct);
}

void p25p1_voice_encode::add_sample(int16_t samp)
{
	// add one sample to 160-sample frame buffer and process if filled
	sampbuf[sampbuf_ct++] = samp;
	if (sampbuf_ct >= FRAME) {
		compress_frame(sampbuf);
		sampbuf_ct = 0;
	}

	// track signal amplitudes
	int16_t asamp = (samp < 0) ? 0 - samp : samp;
	peak = (asamp > peak) ? asamp : peak;
	if (++samp_ct >= SAMP_INTERVAL) {
		peak_amplitude = peak;
		peak = 0;
		samp_ct = 0;
	}
}

void p25p1_voice_encode::compress_samp(const int16_t * samp, int len)
{
	// Apply sample rate slew to accomodate sound card rate discrepancy -
	// workaround for USRP underrun problem occurring when sound card
	// capture rate is slower than the correct rate

	// FIXME: autodetect proper value for opt_stretch_amt
	// perhaps by steering the LDU output rate to a 180.0 msec. rate

	for (int i = 0; i < len; i++ ) {
		stretch_count++;
		if (opt_stretch_amt != 0 && stretch_count >= opt_stretch_amt) {
			stretch_count = 0;
			if (opt_stretch_sign < 0)
				// spill this samp
				continue;
				// repeat this samp
			add_sample(samp[i]);
		}
		add_sample(samp[i]);
	}
}

void
p25p1_voice_encode::set_gain_adjust(float gain_adjust) {
	vocoder.set_gain_adjust(gain_adjust);
}

  } /* namespace op25_repeater */
} /* namespace gr */