[371] | 1 | //codecfilter_speex.c: |
---|
| 2 | |
---|
| 3 | #include "roard.h" |
---|
| 4 | #ifdef ROAR_HAVE_LIBSPEEX |
---|
| 5 | |
---|
[373] | 6 | int cf_speex_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
| 7 | struct roar_stream_server * info, |
---|
| 8 | struct roar_codecfilter * filter) { |
---|
[391] | 9 | struct codecfilter_speex_inst * self = malloc(sizeof(struct codecfilter_speex_inst)); |
---|
[395] | 10 | struct roar_stream * s = (struct roar_stream *) info; |
---|
[391] | 11 | |
---|
[373] | 12 | *inst = NULL; |
---|
[391] | 13 | |
---|
| 14 | if (!self) |
---|
| 15 | return -1; |
---|
| 16 | |
---|
[392] | 17 | self->encoder = NULL; |
---|
| 18 | self->decoder = NULL; |
---|
| 19 | |
---|
[393] | 20 | self->stream = info; |
---|
| 21 | |
---|
[395] | 22 | self->cd = NULL; |
---|
| 23 | |
---|
| 24 | self->i_rest = NULL; |
---|
| 25 | self->fi_rest = 0; |
---|
| 26 | |
---|
[392] | 27 | speex_bits_init(&(self->bits)); |
---|
| 28 | |
---|
[395] | 29 | s->info.codec = ROAR_CODEC_DEFAULT; |
---|
| 30 | s->info.bits = 16; // speex hardcoded |
---|
| 31 | s->info.channels = 1; // only mono support at the moment |
---|
| 32 | |
---|
[391] | 33 | *inst = (void*) self; |
---|
| 34 | |
---|
| 35 | return 0; |
---|
[373] | 36 | } |
---|
| 37 | |
---|
| 38 | int cf_speex_close(CODECFILTER_USERDATA_T inst) { |
---|
[391] | 39 | struct codecfilter_speex_inst * self = (struct codecfilter_speex_inst *) inst; |
---|
| 40 | |
---|
| 41 | if (!self) |
---|
| 42 | return -1; |
---|
| 43 | |
---|
[392] | 44 | if ( self->encoder ) |
---|
| 45 | speex_encoder_destroy(self->decoder); |
---|
| 46 | |
---|
| 47 | self->encoder = NULL; |
---|
| 48 | |
---|
| 49 | if ( self->decoder ) |
---|
| 50 | speex_decoder_destroy(self->decoder); |
---|
| 51 | |
---|
| 52 | self->decoder = NULL; |
---|
| 53 | |
---|
| 54 | speex_bits_destroy(&(self->bits)); |
---|
| 55 | |
---|
[395] | 56 | if ( self->cd ) |
---|
| 57 | free(self->cd); |
---|
| 58 | |
---|
| 59 | if ( self->i_rest ) |
---|
| 60 | free(self->i_rest); |
---|
| 61 | |
---|
[391] | 62 | free((void*)self); |
---|
| 63 | |
---|
| 64 | return 0; |
---|
[373] | 65 | } |
---|
| 66 | |
---|
| 67 | int cf_speex_read(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
[391] | 68 | struct codecfilter_speex_inst * self = (struct codecfilter_speex_inst *) inst; |
---|
[395] | 69 | struct roar_stream * s = (struct roar_stream *) self->stream; |
---|
[393] | 70 | int mode; |
---|
| 71 | uint16_t ui; |
---|
[395] | 72 | int tmp; |
---|
| 73 | int still_todo = len / 2 /* 16 bit */; |
---|
| 74 | int ret = 0; |
---|
[393] | 75 | |
---|
[396] | 76 | ROAR_WARN("cf_speex_read(inst=%p, buf=%p, len=%i) = ?", inst, buf, len); |
---|
| 77 | |
---|
[393] | 78 | if ( ! self->decoder ) { |
---|
[395] | 79 | ROAR_DBG("cf_speex_read(*): no decoder, starting one!"); |
---|
[393] | 80 | if ( read(s->fh, &ui, 2) != 2 ) |
---|
| 81 | return 0; |
---|
| 82 | |
---|
| 83 | mode = ntohs(ui); |
---|
| 84 | |
---|
| 85 | if ( mode == ROAR_SPEEX_MODE_NB ) { |
---|
| 86 | self->decoder = speex_decoder_init(&speex_nb_mode); |
---|
| 87 | } else if ( mode == ROAR_SPEEX_MODE_WB ) { |
---|
| 88 | self->decoder = speex_decoder_init(&speex_wb_mode); |
---|
| 89 | } else if ( mode == ROAR_SPEEX_MODE_UWB ) { |
---|
| 90 | self->decoder = speex_decoder_init(&speex_uwb_mode); |
---|
| 91 | } else { |
---|
| 92 | return 0; |
---|
| 93 | } |
---|
[395] | 94 | |
---|
| 95 | tmp=1; |
---|
| 96 | speex_decoder_ctl(self->decoder, SPEEX_SET_ENH, &tmp); |
---|
| 97 | |
---|
| 98 | speex_decoder_ctl(self->decoder, SPEEX_GET_FRAME_SIZE, &(self->frame_size)); |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | ROAR_WARN("cf_speex_read(*): frame_size=%i (%i bytes)", self->frame_size, self->frame_size*2); |
---|
| 103 | |
---|
| 104 | if ( !self->cd ) { |
---|
| 105 | self->cd = malloc((self->frame_size)*2); |
---|
| 106 | if ( !self->cd ) |
---|
| 107 | return 0; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | if ( !self->i_rest ) { |
---|
| 111 | self->i_rest = malloc((self->frame_size)*2); |
---|
| 112 | if ( !self->i_rest ) |
---|
| 113 | return 0; |
---|
| 114 | } |
---|
[393] | 115 | } |
---|
[391] | 116 | |
---|
[396] | 117 | ROAR_DBG("cf_speex_read(*): Have a working decoder!"); |
---|
[395] | 118 | |
---|
[396] | 119 | ROAR_DBG("cf_speex_read(*): frame_size=%i (%i bytes)", self->frame_size, self->frame_size*2); |
---|
| 120 | ROAR_DBG("cf_speex_read(*): i_rest is %i bytes after cd", ((void*)self->i_rest - (void*)self->cd)); |
---|
[395] | 121 | |
---|
| 122 | |
---|
| 123 | if ( self->fi_rest ) { |
---|
[397] | 124 | if ( self->fi_rest > (still_todo*2) ) { |
---|
[395] | 125 | ROAR_WARN("cf_speex_read(*): discarding input rest data: buffer too long!"); |
---|
| 126 | self->fi_rest = 0; |
---|
| 127 | } else { |
---|
| 128 | ROAR_WARN("cf_speex_read(*): using data from input rest buffer: len=%i", self->fi_rest); |
---|
| 129 | memcpy(buf, self->i_rest, self->fi_rest); |
---|
| 130 | buf += self->fi_rest; |
---|
[396] | 131 | still_todo -= self->fi_rest/2; |
---|
| 132 | ret += self->fi_rest; |
---|
[395] | 133 | self->fi_rest = 0; |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | while (still_todo) { |
---|
| 138 | ROAR_WARN("cf_speex_read(*): we sill need %i frames", still_todo); |
---|
| 139 | if ( read(s->fh, &ui, 2) != 2 ) |
---|
| 140 | return -1; |
---|
| 141 | |
---|
| 142 | ui = ntohs(ui); |
---|
| 143 | |
---|
| 144 | if ( ui > ROAR_SPEEX_MAX_CC ) |
---|
| 145 | return 0; |
---|
| 146 | |
---|
| 147 | if ( read(s->fh, self->cc, ui) != ui ) |
---|
| 148 | break; |
---|
| 149 | |
---|
| 150 | speex_bits_read_from(&(self->bits), self->cc, ui); |
---|
| 151 | |
---|
| 152 | speex_decode_int(self->decoder, &(self->bits), self->cd); |
---|
| 153 | |
---|
| 154 | if ( self->frame_size > still_todo ) { |
---|
| 155 | memcpy(buf, self->cd, still_todo*2); |
---|
| 156 | ret += still_todo*2; |
---|
| 157 | self->fi_rest = (self->frame_size - still_todo)*2; |
---|
| 158 | ROAR_WARN("cf_speex_read(*): self->fi_rest=%i, off=%i", self->fi_rest, still_todo*2); |
---|
| 159 | memcpy(self->i_rest, (self->cd)+(still_todo*2), self->fi_rest); |
---|
| 160 | still_todo = 0; |
---|
| 161 | } else { |
---|
| 162 | memcpy(buf, self->cd, self->frame_size*2); |
---|
| 163 | buf += self->frame_size*2; |
---|
| 164 | ret += self->frame_size*2; |
---|
| 165 | still_todo -= self->frame_size; |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | |
---|
[396] | 169 | if ( still_todo ) { |
---|
| 170 | ROAR_WARN("cf_speex_read(*): could not read all reqquested data, returning %i byte less", still_todo*2); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | ROAR_WARN("cf_speex_read(*) = %i", ret); |
---|
| 174 | |
---|
[395] | 175 | return ret; |
---|
[373] | 176 | } |
---|
[393] | 177 | |
---|
[373] | 178 | int cf_speex_write(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
[391] | 179 | struct codecfilter_speex_inst * self = (struct codecfilter_speex_inst *) inst; |
---|
| 180 | |
---|
[373] | 181 | return -1; |
---|
| 182 | } |
---|
| 183 | |
---|
[371] | 184 | #endif |
---|
| 185 | |
---|
| 186 | //ll |
---|