Fix %Sp expansion in wps
[kugel-rb.git] / apps / codecs / speex.c
blob52f4239652a35a0ea57d4b5c5f80005f093266af
1 /**************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2006 Frederik M.J. Vestre
10 * Based on vorbis.c codec interface:
11 * Copyright (C) 2002 Björn Stenberg
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "libspeex/speex/ogg.h"
24 #include "libspeex/speex/speex.h"
25 #include "libspeex/speex/speex_callbacks.h"
26 #include "libspeex/speex/speex_header.h"
27 #include "libspeex/speex/speex_stereo.h"
28 #include "libspeex/speex/speex_config_types.h"
29 #include "codeclib.h"
31 /* Room for one stereo frame of max size, 2*640 */
32 #define MAX_FRAME_SIZE 1280
33 #define CHUNKSIZE 10000 /*2kb*/
34 #define SEEK_CHUNKSIZE 7*CHUNKSIZE
36 CODEC_HEADER
38 spx_int16_t output[MAX_FRAME_SIZE] IBSS_ATTR;
40 static int get_more_data(spx_ogg_sync_state *oy)
42 int bytes;
43 char *buffer;
45 buffer = (char *)spx_ogg_sync_buffer(oy,CHUNKSIZE);
47 bytes = ci->read_filebuf(buffer, sizeof(char)*CHUNKSIZE);
49 spx_ogg_sync_wrote(oy,bytes);
51 return bytes;
54 /* The read/seek functions track absolute position within the stream */
56 static spx_int64_t get_next_page(spx_ogg_sync_state *oy,spx_ogg_page *og,
57 spx_int64_t boundary)
59 spx_int64_t localoffset = ci->curpos;
60 long more;
61 long ret;
63 if (boundary > 0)
64 boundary += ci->curpos;
66 while (1) {
67 more = spx_ogg_sync_pageseek(oy,og);
69 if (more < 0) {
70 /* skipped n bytes */
71 localoffset-=more;
72 } else {
73 if (more == 0) {
74 /* send more paramedics */
75 if(!boundary)return(-1);
77 ret = get_more_data(oy);
78 if (ret == 0)
79 return(-2);
81 if (ret < 0)
82 return(-3);
84 } else {
85 /* got a page. Return the offset at the page beginning,
86 advance the internal offset past the page end */
88 spx_int64_t ret=localoffset;
90 return(ret);
96 static spx_int64_t seek_backwards(spx_ogg_sync_state *oy, spx_ogg_page *og,
97 spx_int64_t wantedpos)
99 spx_int64_t crofs;
100 spx_int64_t *curoffset=&crofs;
101 *curoffset=ci->curpos;
102 spx_int64_t begin=*curoffset;
103 spx_int64_t end=begin;
104 spx_int64_t ret;
105 spx_int64_t offset=-1;
106 spx_int64_t avgpagelen=-1;
107 spx_int64_t lastgranule=-1;
109 short time = -1;
111 while (offset == -1) {
113 begin -= SEEK_CHUNKSIZE;
115 if (begin < 0) {
116 if (time < 0) {
117 begin = 0;
118 time++;
119 } else {
120 LOGF("Can't seek that early:%lld\n",begin);
121 return -3; /* too early */
125 *curoffset = begin;
127 ci->seek_buffer(*curoffset);
129 spx_ogg_sync_reset(oy);
131 lastgranule = -1;
133 while (*curoffset < end) {
134 ret = get_next_page(oy,og,end-*curoffset);
136 if (ret > 0) {
137 if (lastgranule != -1) {
138 if (avgpagelen < 0)
139 avgpagelen = (spx_ogg_page_granulepos(og)-lastgranule);
140 else
141 avgpagelen=((spx_ogg_page_granulepos(og)-lastgranule)
142 + avgpagelen) / 2;
145 lastgranule=spx_ogg_page_granulepos(og);
147 if ((lastgranule - (avgpagelen/4)) < wantedpos &&
148 (lastgranule + avgpagelen + (avgpagelen/4)) > wantedpos) {
150 /*wanted offset found Yeay!*/
152 /*LOGF("GnPagefound:%d,%d,%d,%d\n",ret,
153 lastgranule,wantedpos,avgpagelen);*/
155 return ret;
157 } else if (lastgranule > wantedpos) { /*too late, seek more*/
158 if (offset != -1) {
159 LOGF("Toolate, returnanyway:%lld,%lld,%lld,%lld\n",
160 ret,lastgranule,wantedpos,avgpagelen);
161 return ret;
163 break;
164 } else{ /*if (spx_ogg_page_granulepos(&og)<wantedpos)*/
165 /*too early*/
166 offset = ret;
167 continue;
169 } else if (ret == -3)
170 return(-3);
171 else if (ret<=0)
172 break;
173 else if (*curoffset < end) {
174 /*this should not be possible*/
176 //LOGF("Seek:get_earlier_page:Offset:not_cached by granule:"\"%d,%d,%d,%d,%d\n",*curoffset,end,begin,wantedpos,curpos);
178 offset=ret;
182 return -1;
185 static int speex_seek_page_granule(spx_int64_t pos, spx_int64_t curpos,
186 spx_ogg_sync_state *oy,
187 spx_int64_t headerssize)
189 /* TODO: Someone may want to try to implement seek to packet,
190 instead of just to page (should be more accurate, not be any
191 faster) */
193 spx_int64_t crofs;
194 spx_int64_t *curbyteoffset = &crofs;
195 *curbyteoffset = ci->curpos;
196 spx_int64_t curoffset;
197 curoffset = *curbyteoffset;
198 spx_int64_t offset = 0;
199 spx_ogg_page og = {0,0,0,0};
200 spx_int64_t avgpagelen = -1;
201 spx_int64_t lastgranule = -1;
203 if(abs(pos-curpos)>10000 && headerssize>0 && curoffset-headerssize>10000) {
204 /* if seeking for more that 10sec,
205 headersize is known & more than 10kb is played,
206 try to guess a place to seek from the number of
207 bytes playe for this position, this works best when
208 the bitrate is relativly constant.
211 curoffset = (((*curbyteoffset-headerssize) * pos)/curpos)*98/100;
212 if (curoffset < 0)
213 curoffset=0;
215 //spx_int64_t toffset=curoffset;
217 ci->seek_buffer(curoffset);
219 spx_ogg_sync_reset(oy);
221 offset = get_next_page(oy,&og,-1);
223 if (offset < 0) { /* could not find new page,use old offset */
224 LOGF("Seek/guess/fault:%lld->-<-%d,%lld:%lld,%d,%ld,%d\n",
225 curpos,0,pos,offset,0,
226 ci->curpos,/*stream_length*/0);
228 curoffset = *curbyteoffset;
230 ci->seek_buffer(curoffset);
232 spx_ogg_sync_reset(oy);
233 } else {
234 if (spx_ogg_page_granulepos(&og) == 0 && pos > 5000) {
235 LOGF("SEEK/guess/fault:%lld->-<-%lld,%lld:%lld,%d,%ld,%d\n",
236 curpos,spx_ogg_page_granulepos(&og),pos,
237 offset,0,ci->curpos,/*stream_length*/0);
239 curoffset = *curbyteoffset;
241 ci->seek_buffer(curoffset);
243 spx_ogg_sync_reset(oy);
244 } else {
245 curoffset = offset;
246 curpos = spx_ogg_page_granulepos(&og);
251 /* which way do we want to seek? */
253 if (curpos > pos) { /* backwards */
254 offset = seek_backwards(oy,&og,pos);
256 if (offset > 0) {
257 *curbyteoffset = curoffset;
258 return 1;
260 } else { /* forwards */
262 while ( (offset = get_next_page(oy,&og,-1)) > 0) {
263 if (lastgranule != -1) {
264 if (avgpagelen < 0)
265 avgpagelen = (spx_ogg_page_granulepos(&og) - lastgranule);
266 else
267 avgpagelen = ((spx_ogg_page_granulepos(&og) - lastgranule)
268 + avgpagelen) / 2;
271 lastgranule = spx_ogg_page_granulepos(&og);
273 if ( ((lastgranule - (avgpagelen/4)) < pos && ( lastgranule +
274 avgpagelen + (avgpagelen / 4)) > pos) ||
275 lastgranule > pos) {
277 /*wanted offset found Yeay!*/
279 *curbyteoffset = offset;
281 return offset;
286 ci->seek_buffer(*curbyteoffset);
288 spx_ogg_sync_reset(oy);
290 LOGF("Seek failed:%lld\n", offset);
292 return -1;
295 static void *process_header(spx_ogg_packet *op,
296 int enh_enabled,
297 int *frame_size,
298 int *rate,
299 int *nframes,
300 int *channels,
301 SpeexStereoState *stereo,
302 int *extra_headers
305 void *st;
306 const SpeexMode *mode;
307 SpeexHeader *header;
308 int modeID;
309 SpeexCallback callback;
311 header = speex_packet_to_header((char*)op->packet, op->bytes);
313 if (!header){
314 DEBUGF ("Cannot read header\n");
315 return NULL;
318 if (header->mode >= SPEEX_NB_MODES){
319 DEBUGF ("Mode does not exist\n");
320 return NULL;
323 modeID = header->mode;
325 mode = speex_lib_get_mode(modeID);
327 if (header->speex_version_id > 1) {
328 DEBUGF("Undecodeable bitstream");
329 return NULL;
332 if (mode->bitstream_version < header->mode_bitstream_version){
333 DEBUGF("Undecodeable bitstream, newer bitstream");
334 return NULL;
337 if (mode->bitstream_version > header->mode_bitstream_version){
338 DEBUGF("Too old bitstream");
339 return NULL;
342 st = speex_decoder_init(mode);
343 if (!st){
344 DEBUGF("Decoder init failed");
345 return NULL;
347 speex_decoder_ctl(st, SPEEX_SET_ENH, &enh_enabled);
348 speex_decoder_ctl(st, SPEEX_GET_FRAME_SIZE, frame_size);
350 if (header->nb_channels!=1){
351 callback.callback_id = SPEEX_INBAND_STEREO;
352 callback.func = speex_std_stereo_request_handler;
353 callback.data = stereo;
354 speex_decoder_ctl(st, SPEEX_SET_HANDLER, &callback);
356 *channels = header->nb_channels;
358 if (!*rate)
359 *rate = header->rate;
361 speex_decoder_ctl(st, SPEEX_SET_SAMPLING_RATE, rate);
363 *nframes = header->frames_per_packet;
365 *extra_headers = header->extra_headers;
367 return st;
370 /* this is the codec entry point */
371 enum codec_status codec_main(void)
373 SpeexBits bits;
374 int error = 0;
375 int eof = 0;
376 spx_ogg_sync_state oy;
377 spx_ogg_page og;
378 spx_ogg_packet op;
379 spx_ogg_stream_state os;
380 spx_int64_t page_granule = 0, cur_granule = 0;
381 int enh_enabled = 1;
382 int nframes = 2;
383 int eos = 0;
384 SpeexStereoState *stereo;
385 int channels = -1;
386 int rate = 0, samplerate = 0;
387 int extra_headers = 0;
388 int stream_init = 0;
389 int page_nb_packets, frame_size, packet_count = 0;
390 int lookahead;
391 int headerssize = -1;
392 unsigned long strtoffset = 0;
393 void *st = NULL;
394 int j = 0;
396 /* Ogg handling still uses mallocs, so reset the malloc buffer per track */
397 next_track:
399 if (codec_init()) {
400 error = CODEC_ERROR;
401 goto exit;
403 stereo = speex_stereo_state_init();
404 strtoffset = ci->id3->offset;
406 while (!*ci->taginfo_ready && !ci->stop_codec)
407 ci->sleep(1);
409 spx_ogg_sync_init(&oy);
410 spx_ogg_alloc_buffer(&oy,2*CHUNKSIZE);
412 samplerate = ci->id3->frequency;
413 codec_set_replaygain(ci->id3);
415 eof = 0;
416 while (!eof) {
417 ci->yield();
418 if (ci->stop_codec || ci->new_track)
419 break;
421 /*seek (seeks to the page before the position) */
422 if (ci->seek_time) {
423 if(samplerate!=0&&packet_count>1){
424 LOGF("Speex seek page:%lld,%lld,%ld,%lld,%d\n",
425 ((spx_int64_t)ci->seek_time/1000) *
426 (spx_int64_t)samplerate,
427 page_granule, ci->seek_time,
428 (page_granule/samplerate)*1000, samplerate);
430 speex_seek_page_granule(((spx_int64_t)ci->seek_time/1000) *
431 (spx_int64_t)samplerate,
432 page_granule, &oy, headerssize);
433 ci->seek_complete();
437 next_page:
438 /*Get the ogg buffer for writing*/
439 if(get_more_data(&oy)<1){/*read error*/
440 error=CODEC_ERROR;
441 goto done;
444 /* Loop for all complete pages we got (most likely only one) */
445 while (spx_ogg_sync_pageout(&oy, &og) == 1) {
446 int packet_no;
447 if (stream_init == 0) {
448 spx_ogg_stream_init(&os, spx_ogg_page_serialno(&og));
449 stream_init = 1;
452 /* Add page to the bitstream */
453 spx_ogg_stream_pagein(&os, &og);
455 page_granule = spx_ogg_page_granulepos(&og);
456 page_nb_packets = spx_ogg_page_packets(&og);
458 cur_granule = page_granule;
460 /* Extract all available packets */
461 packet_no=0;
463 while (!eos && spx_ogg_stream_packetout(&os, &op)==1){
464 /* If first packet, process as Speex header */
465 if (packet_count==0){
466 st = process_header(&op, enh_enabled, &frame_size,
467 &samplerate, &nframes, &channels,
468 stereo, &extra_headers);
470 speex_decoder_ctl(st, SPEEX_GET_LOOKAHEAD, &lookahead);
471 if (!nframes)
472 nframes=1;
474 if (!st){
475 error=CODEC_ERROR;
476 goto exit;
479 ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency);
480 ci->configure(DSP_SET_SAMPLE_DEPTH, 16);
481 if (channels == 2) {
482 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
483 } else if (channels == 1) {
484 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
487 /* Speex header in its own page, add the whole page
488 headersize */
489 headerssize += og.header_len+og.body_len;
491 } else if (packet_count<=1+extra_headers){
492 /* add packet to headersize */
493 headerssize += op.bytes;
495 /* Ignore extra headers */
496 } else {
497 if (packet_count <= 2+extra_headers) {
498 if (strtoffset) {
499 ci->seek_buffer(strtoffset);
500 spx_ogg_sync_reset(&oy);
501 packet_count++;
502 goto next_page;
505 packet_no++;
507 if (op.e_o_s) /* End of stream condition */
508 eos=1;
510 /* Set Speex bitstream to point to Ogg packet */
511 speex_bits_set_bit_buffer(&bits, (char *)op.packet,
512 op.bytes);
513 for (j = 0; j != nframes; j++){
514 int ret;
516 /* Decode frame */
517 ret = speex_decode_int(st, &bits, output);
519 if (ret == -1)
520 break;
522 if (ret == -2)
523 break;
525 if (speex_bits_remaining(&bits) < 0)
526 break;
528 if (channels == 2)
529 speex_decode_stereo_int(output, frame_size, stereo);
531 if (frame_size > 0) {
532 spx_int16_t *frame_start = output + lookahead;
534 if (channels == 2)
535 frame_start += lookahead;
536 ci->pcmbuf_insert(frame_start, NULL,
537 frame_size - lookahead);
538 lookahead = 0;
539 /* 2 bytes/sample */
540 cur_granule += frame_size / 2;
542 ci->set_offset((long) ci->curpos);
544 ci->set_elapsed((samplerate == 0) ? 0 :
545 cur_granule * 1000 / samplerate);
549 packet_count++;
554 done:
555 if (ci->request_next_track()) {
557 /* Clean things up for the next track */
559 speex_decoder_destroy(st);
561 if (stream_init == 1)
562 spx_ogg_stream_reset(&os);
564 spx_ogg_sync_reset(&oy);
566 cur_granule = stream_init = rate = samplerate = headerssize
567 = packet_count = eos = 0;
569 goto next_track;
572 error = CODEC_OK;
574 exit:
575 speex_bits_destroy(&bits);
577 if (stream_init)
578 spx_ogg_stream_destroy(&os);
580 spx_ogg_sync_destroy(&oy);
582 return error;