-
Notifications
You must be signed in to change notification settings - Fork 1
/
decoder.c
149 lines (121 loc) · 3.13 KB
/
decoder.c
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
#include "decoder.h"
#include "celp/celp.h"
#include "gsm/gsm.h"
#include "sound.h"
#include "ringbuffer.h"
#define FRAME_SIZE 160
extern ringbuffer_t * speakers;
static int16_t * decode_speex(void * input, uint8_t nbframes, int datasize) {
int16_t * out = (int16_t *)calloc(FRAME_SIZE * nbframes, sizeof(int16_t));
int16_t * outptr = out;
SpeexBits bits;
void * state;
int i;
state = speex_decoder_init(&speex_nb_mode);
speex_bits_init(&bits);
speex_bits_read_from(&bits, (char *)input, datasize*nbframes/8);
for(i=0 ; i<nbframes ; i++) {
speex_decode_int(state, &bits, outptr);
outptr += FRAME_SIZE;
}
speex_decoder_destroy(state);
speex_bits_destroy(&bits);
return out;
}
static int16_t * decode_gsm(void * input, int nbframes) {
int16_t * out = (int16_t *)calloc(FRAME_SIZE * nbframes, sizeof(int16_t));
int16_t * outptr = out;
unsigned char * ptr = (unsigned char *)input;
int i;
gsm handle;
ptr+=6;
handle = gsm_create();
for(i=0 ; i<nbframes ; i++) {
if(gsm_decode(handle, ptr, outptr)) {
printf("GSM ERROR...\n");
}
ptr+=33;
outptr += FRAME_SIZE;
}
gsm_destroy(handle);
return out;
}
static int16_t * decode_celp(void * input, int nbframes) {
int16_t * out = (int16_t *)calloc(240 * nbframes, sizeof(int16_t));
int16_t * outptr = out;
unsigned char * ptr = (unsigned char *)input;
int i;
celp_decoder_state * state;
ptr+=6;
state = create_celp_decoder_state();
init_celp_decoder_state(state, CELP_4_5_FRAMESIZE);
for(i=0 ; i<nbframes ; i++) {
celp_decode(ptr, outptr, state);
ptr+=17;
outptr += 240;
}
destroy_celp_decoder_state(state);
return out;
}
void decode_audio_packet(void * input) {
uint8_t nbframes;
int16_t * out;
char * ptr = (char *)input;
uint8_t codec;
int i;
ptr+=3;
codec = *ptr;
ptr+=19;
nbframes = *ptr;
ptr++;
/* printf(">>>> CODEC : 0x%x\n", *((char *)(&codec)));*/
switch(codec) {
case CODEC_SPEEX_25_9:
out = decode_speex(ptr, nbframes, 492);
break;
case CODEC_SPEEX_19_6:
out = decode_speex(ptr, nbframes, 364);
break;
case CODEC_SPEEX_16_3:
out = decode_speex(ptr, nbframes, 300);
break;
case CODEC_SPEEX_12_3:
out = decode_speex(ptr, nbframes, 220);
break;
case CODEC_SPEEX_9_3:
out = decode_speex(ptr, nbframes, 160);
break;
case CODEC_SPEEX_7_2:
out = decode_speex(ptr, nbframes, 119);
break;
case CODEC_SPEEX_5_2:
out = decode_speex(ptr, nbframes, 79);
break;
case CODEC_SPEEX_3_4:
out = decode_speex(ptr, nbframes, 43);
break;
case CODEC_GSM_14_8:
nbframes = 5;
out = decode_gsm(ptr,nbframes);
break;
case CODEC_GSM_16_4:
nbframes = 3;
out = decode_gsm(ptr,nbframes);
break;
case CODEC_CELP_6_3:
nbframes = 3;
out = decode_celp(ptr, nbframes);
break;
case CODEC_CELP_5_1:
nbframes = 9;
out = decode_celp(ptr, nbframes);
break;
default:
nbframes = 0;
out = NULL;
}
for(i=0;i<nbframes;i++) {
ringbuffer_write(speakers, out+(FRAME_SIZE * i));
}
free(out);
}