2007-02-09 10:06:53 +00:00
|
|
|
/**************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Frederik M.J. Vestre
|
|
|
|
* Based on vorbis.c codec interface:
|
2009-03-23 17:08:46 +00:00
|
|
|
* Copyright (C) 2002 Björn Stenberg
|
2007-02-09 10:06:53 +00:00
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program 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 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2007-02-09 10:06:53 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
#include "libspeex/speex/ogg.h"
|
|
|
|
#include "libspeex/speex/speex.h"
|
2007-02-10 11:44:26 +00:00
|
|
|
#include "libspeex/speex/speex_callbacks.h"
|
2007-02-09 10:06:53 +00:00
|
|
|
#include "libspeex/speex/speex_header.h"
|
|
|
|
#include "libspeex/speex/speex_stereo.h"
|
2007-02-10 13:33:29 +00:00
|
|
|
#include "libspeex/speex/speex_config_types.h"
|
2007-02-09 10:06:53 +00:00
|
|
|
#include "codeclib.h"
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-11-10 15:43:40 +00:00
|
|
|
/* Room for one stereo frame of max size, 2*640 */
|
2007-11-10 14:57:49 +00:00
|
|
|
#define MAX_FRAME_SIZE 1280
|
2007-02-09 10:06:53 +00:00
|
|
|
#define CHUNKSIZE 10000 /*2kb*/
|
|
|
|
#define SEEK_CHUNKSIZE 7*CHUNKSIZE
|
|
|
|
|
|
|
|
CODEC_HEADER
|
|
|
|
|
2010-07-25 22:24:53 +00:00
|
|
|
static spx_int16_t output[MAX_FRAME_SIZE] IBSS_ATTR;
|
2007-02-09 10:06:53 +00:00
|
|
|
|
2008-08-12 22:01:55 +00:00
|
|
|
static int get_more_data(spx_ogg_sync_state *oy)
|
2007-02-09 18:10:24 +00:00
|
|
|
{
|
2007-02-09 10:06:53 +00:00
|
|
|
int bytes;
|
|
|
|
char *buffer;
|
2007-02-09 18:10:24 +00:00
|
|
|
|
|
|
|
buffer = (char *)spx_ogg_sync_buffer(oy,CHUNKSIZE);
|
|
|
|
|
2007-03-12 11:54:07 +00:00
|
|
|
bytes = ci->read_filebuf(buffer, sizeof(char)*CHUNKSIZE);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
spx_ogg_sync_wrote(oy,bytes);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
return bytes;
|
|
|
|
}
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
/* The read/seek functions track absolute position within the stream */
|
|
|
|
|
|
|
|
static spx_int64_t get_next_page(spx_ogg_sync_state *oy,spx_ogg_page *og,
|
2007-03-12 11:54:07 +00:00
|
|
|
spx_int64_t boundary)
|
2007-02-09 18:10:24 +00:00
|
|
|
{
|
2007-03-12 11:54:07 +00:00
|
|
|
spx_int64_t localoffset = ci->curpos;
|
2007-02-09 18:10:24 +00:00
|
|
|
long more;
|
|
|
|
long ret;
|
|
|
|
|
|
|
|
if (boundary > 0)
|
2007-03-12 11:54:07 +00:00
|
|
|
boundary += ci->curpos;
|
2007-02-09 18:10:24 +00:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
more = spx_ogg_sync_pageseek(oy,og);
|
|
|
|
|
|
|
|
if (more < 0) {
|
2007-02-09 10:06:53 +00:00
|
|
|
/* skipped n bytes */
|
|
|
|
localoffset-=more;
|
2007-02-09 18:10:24 +00:00
|
|
|
} else {
|
|
|
|
if (more == 0) {
|
2007-02-09 10:06:53 +00:00
|
|
|
/* send more paramedics */
|
|
|
|
if(!boundary)return(-1);
|
|
|
|
{
|
2007-03-12 11:54:07 +00:00
|
|
|
ret = get_more_data(oy);
|
2007-02-09 18:10:24 +00:00
|
|
|
if (ret == 0)
|
2007-02-09 10:06:53 +00:00
|
|
|
return(-2);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
|
|
|
if (ret < 0)
|
2007-02-09 10:06:53 +00:00
|
|
|
return(-3);
|
|
|
|
}
|
2007-02-09 18:10:24 +00:00
|
|
|
} else {
|
2007-02-09 10:06:53 +00:00
|
|
|
/* got a page. Return the offset at the page beginning,
|
2007-02-09 18:10:24 +00:00
|
|
|
advance the internal offset past the page end */
|
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
spx_int64_t ret=localoffset;
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-09 18:10:24 +00:00
|
|
|
static spx_int64_t seek_backwards(spx_ogg_sync_state *oy, spx_ogg_page *og,
|
2007-03-12 11:54:07 +00:00
|
|
|
spx_int64_t wantedpos)
|
2007-02-09 18:10:24 +00:00
|
|
|
{
|
2007-02-09 10:06:53 +00:00
|
|
|
spx_int64_t crofs;
|
|
|
|
spx_int64_t *curoffset=&crofs;
|
2007-03-12 11:54:07 +00:00
|
|
|
*curoffset=ci->curpos;
|
2007-02-09 10:06:53 +00:00
|
|
|
spx_int64_t begin=*curoffset;
|
|
|
|
spx_int64_t end=begin;
|
|
|
|
spx_int64_t ret;
|
|
|
|
spx_int64_t offset=-1;
|
|
|
|
spx_int64_t avgpagelen=-1;
|
|
|
|
spx_int64_t lastgranule=-1;
|
2007-02-09 18:10:24 +00:00
|
|
|
|
|
|
|
short time = -1;
|
|
|
|
|
|
|
|
while (offset == -1) {
|
|
|
|
|
|
|
|
begin -= SEEK_CHUNKSIZE;
|
|
|
|
|
|
|
|
if (begin < 0) {
|
|
|
|
if (time < 0) {
|
|
|
|
begin = 0;
|
2007-02-09 10:06:53 +00:00
|
|
|
time++;
|
2007-02-09 18:10:24 +00:00
|
|
|
} else {
|
2020-06-25 13:45:54 +00:00
|
|
|
LOGF("Can't seek that early:%lld\n", (long long int)begin);
|
2007-02-09 18:10:24 +00:00
|
|
|
return -3; /* too early */
|
2007-02-09 10:06:53 +00:00
|
|
|
}
|
|
|
|
}
|
2007-02-09 18:10:24 +00:00
|
|
|
|
|
|
|
*curoffset = begin;
|
|
|
|
|
2007-03-12 11:54:07 +00:00
|
|
|
ci->seek_buffer(*curoffset);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
spx_ogg_sync_reset(oy);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
|
|
|
lastgranule = -1;
|
|
|
|
|
|
|
|
while (*curoffset < end) {
|
2007-03-12 11:54:07 +00:00
|
|
|
ret = get_next_page(oy,og,end-*curoffset);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
|
|
|
if (ret > 0) {
|
|
|
|
if (lastgranule != -1) {
|
|
|
|
if (avgpagelen < 0)
|
|
|
|
avgpagelen = (spx_ogg_page_granulepos(og)-lastgranule);
|
2007-02-09 10:06:53 +00:00
|
|
|
else
|
|
|
|
avgpagelen=((spx_ogg_page_granulepos(og)-lastgranule)
|
2007-02-09 18:10:24 +00:00
|
|
|
+ avgpagelen) / 2;
|
2007-02-09 10:06:53 +00:00
|
|
|
}
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
lastgranule=spx_ogg_page_granulepos(og);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
|
|
|
if ((lastgranule - (avgpagelen/4)) < wantedpos &&
|
|
|
|
(lastgranule + avgpagelen + (avgpagelen/4)) > wantedpos) {
|
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
/*wanted offset found Yeay!*/
|
2007-02-09 18:10:24 +00:00
|
|
|
|
|
|
|
/*LOGF("GnPagefound:%d,%d,%d,%d\n",ret,
|
|
|
|
lastgranule,wantedpos,avgpagelen);*/
|
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
return ret;
|
2007-02-09 18:10:24 +00:00
|
|
|
|
|
|
|
} else if (lastgranule > wantedpos) { /*too late, seek more*/
|
|
|
|
if (offset != -1) {
|
2009-09-30 12:56:33 +00:00
|
|
|
LOGF("Toolate, returnanyway:%lld,%lld,%lld,%lld\n",
|
2020-06-25 13:45:54 +00:00
|
|
|
(long long int)ret, (long long int)lastgranule,
|
|
|
|
(long long int)wantedpos, (long long int)avgpagelen);
|
2007-02-09 10:06:53 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} else{ /*if (spx_ogg_page_granulepos(&og)<wantedpos)*/
|
|
|
|
/*too early*/
|
2007-02-09 18:10:24 +00:00
|
|
|
offset = ret;
|
2007-02-09 10:06:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-02-09 18:10:24 +00:00
|
|
|
} else if (ret == -3)
|
|
|
|
return(-3);
|
|
|
|
else if (ret<=0)
|
2007-02-09 10:06:53 +00:00
|
|
|
break;
|
2007-02-09 18:10:24 +00:00
|
|
|
else if (*curoffset < end) {
|
2007-02-09 10:06:53 +00:00
|
|
|
/*this should not be possible*/
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
//LOGF("Seek:get_earlier_page:Offset:not_cached by granule:"\"%d,%d,%d,%d,%d\n",*curoffset,end,begin,wantedpos,curpos);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
offset=ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-08-12 22:01:55 +00:00
|
|
|
static int speex_seek_page_granule(spx_int64_t pos, spx_int64_t curpos,
|
|
|
|
spx_ogg_sync_state *oy,
|
|
|
|
spx_int64_t headerssize)
|
2007-02-09 18:10:24 +00:00
|
|
|
{
|
2007-02-09 10:06:53 +00:00
|
|
|
/* TODO: Someone may want to try to implement seek to packet,
|
2007-02-09 18:10:24 +00:00
|
|
|
instead of just to page (should be more accurate, not be any
|
|
|
|
faster) */
|
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
spx_int64_t crofs;
|
2007-02-09 18:10:24 +00:00
|
|
|
spx_int64_t *curbyteoffset = &crofs;
|
2007-03-12 11:54:07 +00:00
|
|
|
*curbyteoffset = ci->curpos;
|
2007-02-09 10:06:53 +00:00
|
|
|
spx_int64_t curoffset;
|
2007-02-09 18:10:24 +00:00
|
|
|
curoffset = *curbyteoffset;
|
|
|
|
spx_int64_t offset = 0;
|
|
|
|
spx_ogg_page og = {0,0,0,0};
|
|
|
|
spx_int64_t avgpagelen = -1;
|
|
|
|
spx_int64_t lastgranule = -1;
|
|
|
|
|
|
|
|
if(abs(pos-curpos)>10000 && headerssize>0 && curoffset-headerssize>10000) {
|
|
|
|
/* if seeking for more that 10sec,
|
|
|
|
headersize is known & more than 10kb is played,
|
|
|
|
try to guess a place to seek from the number of
|
|
|
|
bytes playe for this position, this works best when
|
|
|
|
the bitrate is relativly constant.
|
|
|
|
*/
|
|
|
|
|
2007-11-14 00:46:49 +00:00
|
|
|
curoffset = (((*curbyteoffset-headerssize) * pos)/curpos)*98/100;
|
2007-02-09 18:10:24 +00:00
|
|
|
if (curoffset < 0)
|
2007-02-09 10:06:53 +00:00
|
|
|
curoffset=0;
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
//spx_int64_t toffset=curoffset;
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-03-12 11:54:07 +00:00
|
|
|
ci->seek_buffer(curoffset);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
spx_ogg_sync_reset(oy);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-03-12 11:54:07 +00:00
|
|
|
offset = get_next_page(oy,&og,-1);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
|
|
|
if (offset < 0) { /* could not find new page,use old offset */
|
2009-09-30 12:56:33 +00:00
|
|
|
LOGF("Seek/guess/fault:%lld->-<-%d,%lld:%lld,%d,%ld,%d\n",
|
2020-06-25 13:45:54 +00:00
|
|
|
(long long int)curpos,0, (long long int)pos,
|
|
|
|
(long long int)offset,0,ci->curpos,/*stream_length*/0);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
|
|
|
curoffset = *curbyteoffset;
|
|
|
|
|
2007-03-12 11:54:07 +00:00
|
|
|
ci->seek_buffer(curoffset);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
spx_ogg_sync_reset(oy);
|
2007-02-09 18:10:24 +00:00
|
|
|
} else {
|
|
|
|
if (spx_ogg_page_granulepos(&og) == 0 && pos > 5000) {
|
2009-09-30 12:56:33 +00:00
|
|
|
LOGF("SEEK/guess/fault:%lld->-<-%lld,%lld:%lld,%d,%ld,%d\n",
|
2020-06-25 13:45:54 +00:00
|
|
|
(long long int)curpos,(long long int)spx_ogg_page_granulepos(&og),
|
|
|
|
(long long int)pos, (long long int)offset,0,ci->curpos,/*stream_length*/0);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
|
|
|
curoffset = *curbyteoffset;
|
|
|
|
|
2007-03-12 11:54:07 +00:00
|
|
|
ci->seek_buffer(curoffset);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
spx_ogg_sync_reset(oy);
|
2007-02-09 18:10:24 +00:00
|
|
|
} else {
|
|
|
|
curoffset = offset;
|
|
|
|
curpos = spx_ogg_page_granulepos(&og);
|
2007-02-09 10:06:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-02-09 18:10:24 +00:00
|
|
|
|
|
|
|
/* which way do we want to seek? */
|
|
|
|
|
|
|
|
if (curpos > pos) { /* backwards */
|
2007-03-12 11:54:07 +00:00
|
|
|
offset = seek_backwards(oy,&og,pos);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
|
|
|
if (offset > 0) {
|
|
|
|
*curbyteoffset = curoffset;
|
2007-02-09 10:06:53 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2007-02-09 18:10:24 +00:00
|
|
|
} else { /* forwards */
|
|
|
|
|
2007-03-12 11:54:07 +00:00
|
|
|
while ( (offset = get_next_page(oy,&og,-1)) > 0) {
|
2007-02-09 18:10:24 +00:00
|
|
|
if (lastgranule != -1) {
|
|
|
|
if (avgpagelen < 0)
|
|
|
|
avgpagelen = (spx_ogg_page_granulepos(&og) - lastgranule);
|
|
|
|
else
|
|
|
|
avgpagelen = ((spx_ogg_page_granulepos(&og) - lastgranule)
|
|
|
|
+ avgpagelen) / 2;
|
2007-02-09 10:06:53 +00:00
|
|
|
}
|
2007-02-09 18:10:24 +00:00
|
|
|
|
|
|
|
lastgranule = spx_ogg_page_granulepos(&og);
|
|
|
|
|
|
|
|
if ( ((lastgranule - (avgpagelen/4)) < pos && ( lastgranule +
|
|
|
|
avgpagelen + (avgpagelen / 4)) > pos) ||
|
|
|
|
lastgranule > pos) {
|
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
/*wanted offset found Yeay!*/
|
2007-02-09 18:10:24 +00:00
|
|
|
|
|
|
|
*curbyteoffset = offset;
|
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-03-12 11:54:07 +00:00
|
|
|
ci->seek_buffer(*curbyteoffset);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
spx_ogg_sync_reset(oy);
|
|
|
|
|
2020-06-25 13:45:54 +00:00
|
|
|
LOGF("Seek failed:%lld\n", (long long int)offset);
|
2007-02-09 10:06:53 +00:00
|
|
|
|
2007-02-09 18:10:24 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2007-02-09 10:06:53 +00:00
|
|
|
|
2007-02-09 18:10:24 +00:00
|
|
|
static void *process_header(spx_ogg_packet *op,
|
|
|
|
int enh_enabled,
|
|
|
|
int *frame_size,
|
|
|
|
int *rate,
|
|
|
|
int *nframes,
|
|
|
|
int *channels,
|
|
|
|
SpeexStereoState *stereo,
|
|
|
|
int *extra_headers
|
|
|
|
)
|
|
|
|
{
|
2007-02-09 10:06:53 +00:00
|
|
|
void *st;
|
|
|
|
const SpeexMode *mode;
|
|
|
|
SpeexHeader *header;
|
|
|
|
int modeID;
|
|
|
|
SpeexCallback callback;
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
header = speex_packet_to_header((char*)op->packet, op->bytes);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
if (!header){
|
|
|
|
DEBUGF ("Cannot read header\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
if (header->mode >= SPEEX_NB_MODES){
|
|
|
|
DEBUGF ("Mode does not exist\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-02-09 18:10:24 +00:00
|
|
|
modeID = header->mode;
|
|
|
|
|
2007-03-12 11:54:07 +00:00
|
|
|
mode = speex_lib_get_mode(modeID);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
|
|
|
if (header->speex_version_id > 1) {
|
|
|
|
DEBUGF("Undecodeable bitstream");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode->bitstream_version < header->mode_bitstream_version){
|
|
|
|
DEBUGF("Undecodeable bitstream, newer bitstream");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode->bitstream_version > header->mode_bitstream_version){
|
|
|
|
DEBUGF("Too old bitstream");
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-02-09 10:06:53 +00:00
|
|
|
|
2007-02-09 18:10:24 +00:00
|
|
|
st = speex_decoder_init(mode);
|
|
|
|
if (!st){
|
|
|
|
DEBUGF("Decoder init failed");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
speex_decoder_ctl(st, SPEEX_SET_ENH, &enh_enabled);
|
|
|
|
speex_decoder_ctl(st, SPEEX_GET_FRAME_SIZE, frame_size);
|
2007-02-09 10:06:53 +00:00
|
|
|
|
2007-11-14 00:46:49 +00:00
|
|
|
if (header->nb_channels!=1){
|
2007-02-09 18:10:24 +00:00
|
|
|
callback.callback_id = SPEEX_INBAND_STEREO;
|
|
|
|
callback.func = speex_std_stereo_request_handler;
|
|
|
|
callback.data = stereo;
|
|
|
|
speex_decoder_ctl(st, SPEEX_SET_HANDLER, &callback);
|
|
|
|
}
|
2007-11-14 00:46:49 +00:00
|
|
|
*channels = header->nb_channels;
|
|
|
|
|
2007-02-09 18:10:24 +00:00
|
|
|
if (!*rate)
|
|
|
|
*rate = header->rate;
|
2007-02-09 10:06:53 +00:00
|
|
|
|
2007-02-09 18:10:24 +00:00
|
|
|
speex_decoder_ctl(st, SPEEX_SET_SAMPLING_RATE, rate);
|
|
|
|
|
|
|
|
*nframes = header->frames_per_packet;
|
|
|
|
|
|
|
|
*extra_headers = header->extra_headers;
|
|
|
|
|
|
|
|
return st;
|
|
|
|
}
|
2007-02-09 10:06:53 +00:00
|
|
|
|
2011-04-27 12:52:11 +00:00
|
|
|
/* this is the codec entry point */
|
|
|
|
enum codec_status codec_main(enum codec_entry_call_reason reason)
|
|
|
|
{
|
|
|
|
/* Nothing to do */
|
|
|
|
return CODEC_OK;
|
|
|
|
(void)reason;
|
|
|
|
}
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* this is called for each file to process */
|
|
|
|
enum codec_status codec_run(void)
|
2007-02-09 10:06:53 +00:00
|
|
|
{
|
2011-04-27 03:08:23 +00:00
|
|
|
int error = CODEC_ERROR;
|
|
|
|
|
2007-03-12 11:54:07 +00:00
|
|
|
SpeexBits bits;
|
2007-02-09 10:06:53 +00:00
|
|
|
spx_ogg_sync_state oy;
|
|
|
|
spx_ogg_page og;
|
|
|
|
spx_ogg_packet op;
|
|
|
|
spx_ogg_stream_state os;
|
2011-05-01 04:23:30 +00:00
|
|
|
spx_int64_t page_granule = 0;
|
|
|
|
spx_int64_t cur_granule = 0;
|
2007-03-12 11:54:07 +00:00
|
|
|
int enh_enabled = 1;
|
|
|
|
int nframes = 2;
|
|
|
|
int eos = 0;
|
2007-11-14 00:46:49 +00:00
|
|
|
SpeexStereoState *stereo;
|
2007-03-12 11:54:07 +00:00
|
|
|
int channels = -1;
|
2011-05-01 04:23:30 +00:00
|
|
|
int samplerate = ci->id3->frequency;
|
2007-03-12 11:54:07 +00:00
|
|
|
int extra_headers = 0;
|
|
|
|
int stream_init = 0;
|
2011-05-01 11:42:41 +00:00
|
|
|
/* rockbox: comment 'set but unused' variables
|
2011-05-01 04:23:30 +00:00
|
|
|
int page_nb_packets;
|
2011-05-01 11:42:41 +00:00
|
|
|
*/
|
2011-05-01 04:23:30 +00:00
|
|
|
int frame_size;
|
|
|
|
int packet_count = 0;
|
2007-11-14 14:43:32 +00:00
|
|
|
int lookahead;
|
2011-05-01 04:23:30 +00:00
|
|
|
int headerssize = 0;
|
2013-07-14 11:59:39 +00:00
|
|
|
unsigned long strtoffset;
|
2007-03-12 11:54:07 +00:00
|
|
|
void *st = NULL;
|
|
|
|
int j = 0;
|
2017-12-07 18:21:57 +00:00
|
|
|
long action;
|
2011-04-27 03:08:23 +00:00
|
|
|
intptr_t param;
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2011-03-02 03:47:42 +00:00
|
|
|
memset(&bits, 0, sizeof(bits));
|
|
|
|
memset(&oy, 0, sizeof(oy));
|
|
|
|
|
2007-11-14 00:46:49 +00:00
|
|
|
/* Ogg handling still uses mallocs, so reset the malloc buffer per track */
|
2007-02-09 10:06:53 +00:00
|
|
|
if (codec_init()) {
|
|
|
|
goto exit;
|
|
|
|
}
|
2011-03-02 03:47:42 +00:00
|
|
|
|
2013-07-14 11:59:39 +00:00
|
|
|
action = CODEC_ACTION_NULL;
|
|
|
|
param = ci->id3->elapsed;
|
|
|
|
strtoffset = ci->id3->offset;
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
ci->seek_buffer(0);
|
2011-08-28 07:45:35 +00:00
|
|
|
ci->set_elapsed(0);
|
2011-04-27 03:08:23 +00:00
|
|
|
|
2007-11-14 00:46:49 +00:00
|
|
|
stereo = speex_stereo_state_init();
|
2011-03-02 03:47:42 +00:00
|
|
|
spx_ogg_sync_init(&oy);
|
|
|
|
spx_ogg_alloc_buffer(&oy,2*CHUNKSIZE);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-03-12 11:54:07 +00:00
|
|
|
codec_set_replaygain(ci->id3);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2013-07-14 11:59:39 +00:00
|
|
|
if (!strtoffset && param) {
|
|
|
|
action = CODEC_ACTION_SEEK_TIME;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto next_page;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (action == CODEC_ACTION_NULL)
|
|
|
|
action = ci->get_command(¶m);
|
|
|
|
|
|
|
|
if (action != CODEC_ACTION_NULL) {
|
|
|
|
if (action == CODEC_ACTION_HALT)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*seek (seeks to the page before the position) */
|
|
|
|
if (action == CODEC_ACTION_SEEK_TIME) {
|
|
|
|
if(samplerate!=0&&packet_count>1){
|
|
|
|
LOGF("Speex seek page:%lld,%lld,%ld,%lld,%d\n",
|
2020-06-25 13:45:54 +00:00
|
|
|
(long long int)((spx_int64_t)param/1000) *
|
2013-07-14 11:59:39 +00:00
|
|
|
(spx_int64_t)samplerate,
|
2020-06-25 13:45:54 +00:00
|
|
|
(long long int)page_granule, (long)param,
|
|
|
|
(long long int)((page_granule/samplerate)*1000), samplerate);
|
2013-07-14 11:59:39 +00:00
|
|
|
|
|
|
|
speex_seek_page_granule(((spx_int64_t)param/1000) *
|
|
|
|
(spx_int64_t)samplerate,
|
|
|
|
page_granule, &oy, headerssize);
|
|
|
|
}
|
|
|
|
|
|
|
|
ci->set_elapsed(param);
|
|
|
|
ci->seek_complete();
|
2007-02-09 10:06:53 +00:00
|
|
|
}
|
2011-04-27 03:08:23 +00:00
|
|
|
|
2013-07-14 11:59:39 +00:00
|
|
|
action = CODEC_ACTION_NULL;
|
2007-02-09 10:06:53 +00:00
|
|
|
}
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
next_page:
|
|
|
|
/*Get the ogg buffer for writing*/
|
2007-03-12 11:54:07 +00:00
|
|
|
if(get_more_data(&oy)<1){/*read error*/
|
2007-02-09 10:06:53 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2007-02-09 18:10:24 +00:00
|
|
|
|
|
|
|
/* Loop for all complete pages we got (most likely only one) */
|
|
|
|
while (spx_ogg_sync_pageout(&oy, &og) == 1) {
|
2007-02-09 10:06:53 +00:00
|
|
|
int packet_no;
|
|
|
|
if (stream_init == 0) {
|
2007-03-12 11:54:07 +00:00
|
|
|
spx_ogg_stream_init(&os, spx_ogg_page_serialno(&og));
|
2007-02-09 10:06:53 +00:00
|
|
|
stream_init = 1;
|
|
|
|
}
|
2007-02-09 18:10:24 +00:00
|
|
|
|
|
|
|
/* Add page to the bitstream */
|
2007-02-09 10:06:53 +00:00
|
|
|
spx_ogg_stream_pagein(&os, &og);
|
|
|
|
|
|
|
|
page_granule = spx_ogg_page_granulepos(&og);
|
2011-05-01 11:42:41 +00:00
|
|
|
/* page_nb_packets = spx_ogg_page_packets(&og); */
|
2007-02-09 10:06:53 +00:00
|
|
|
|
|
|
|
cur_granule = page_granule;
|
|
|
|
|
2007-02-09 18:10:24 +00:00
|
|
|
/* Extract all available packets */
|
2007-02-09 10:06:53 +00:00
|
|
|
packet_no=0;
|
|
|
|
|
|
|
|
while (!eos && spx_ogg_stream_packetout(&os, &op)==1){
|
2007-02-09 18:10:24 +00:00
|
|
|
/* If first packet, process as Speex header */
|
2007-02-09 10:06:53 +00:00
|
|
|
if (packet_count==0){
|
|
|
|
st = process_header(&op, enh_enabled, &frame_size,
|
2007-03-12 11:54:07 +00:00
|
|
|
&samplerate, &nframes, &channels,
|
2007-11-14 00:46:49 +00:00
|
|
|
stereo, &extra_headers);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-11-14 14:43:32 +00:00
|
|
|
speex_decoder_ctl(st, SPEEX_GET_LOOKAHEAD, &lookahead);
|
2007-02-09 10:06:53 +00:00
|
|
|
if (!nframes)
|
|
|
|
nframes=1;
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
if (!st){
|
2011-04-27 03:08:23 +00:00
|
|
|
goto done;
|
2007-02-09 10:06:53 +00:00
|
|
|
}
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-03-12 11:54:07 +00:00
|
|
|
ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency);
|
|
|
|
ci->configure(DSP_SET_SAMPLE_DEPTH, 16);
|
|
|
|
if (channels == 2) {
|
|
|
|
ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
|
|
|
|
} else if (channels == 1) {
|
|
|
|
ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
|
|
|
|
}
|
2007-02-09 18:10:24 +00:00
|
|
|
|
|
|
|
/* Speex header in its own page, add the whole page
|
|
|
|
headersize */
|
2007-03-12 11:54:07 +00:00
|
|
|
headerssize += og.header_len+og.body_len;
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
} else if (packet_count<=1+extra_headers){
|
2007-02-09 18:10:24 +00:00
|
|
|
/* add packet to headersize */
|
|
|
|
headerssize += op.bytes;
|
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
/* Ignore extra headers */
|
|
|
|
} else {
|
2007-02-09 18:10:24 +00:00
|
|
|
if (packet_count <= 2+extra_headers) {
|
2007-02-09 10:06:53 +00:00
|
|
|
if (strtoffset) {
|
2007-03-12 11:54:07 +00:00
|
|
|
ci->seek_buffer(strtoffset);
|
2007-02-09 10:06:53 +00:00
|
|
|
spx_ogg_sync_reset(&oy);
|
|
|
|
packet_count++;
|
|
|
|
goto next_page;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
packet_no++;
|
2007-02-09 18:10:24 +00:00
|
|
|
|
|
|
|
if (op.e_o_s) /* End of stream condition */
|
2007-02-09 10:06:53 +00:00
|
|
|
eos=1;
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-11-01 21:34:15 +00:00
|
|
|
/* Set Speex bitstream to point to Ogg packet */
|
|
|
|
speex_bits_set_bit_buffer(&bits, (char *)op.packet,
|
|
|
|
op.bytes);
|
2007-02-09 18:10:24 +00:00
|
|
|
for (j = 0; j != nframes; j++){
|
2007-02-09 10:06:53 +00:00
|
|
|
int ret;
|
2007-02-09 18:10:24 +00:00
|
|
|
|
|
|
|
/* Decode frame */
|
2007-03-12 11:54:07 +00:00
|
|
|
ret = speex_decode_int(st, &bits, output);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-03-12 11:54:07 +00:00
|
|
|
if (ret == -1)
|
2007-02-09 10:06:53 +00:00
|
|
|
break;
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-03-12 11:54:07 +00:00
|
|
|
if (ret == -2)
|
2007-02-09 10:06:53 +00:00
|
|
|
break;
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-03-12 11:54:07 +00:00
|
|
|
if (speex_bits_remaining(&bits) < 0)
|
2007-02-09 10:06:53 +00:00
|
|
|
break;
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-02-10 06:11:30 +00:00
|
|
|
if (channels == 2)
|
2007-11-14 00:46:49 +00:00
|
|
|
speex_decode_stereo_int(output, frame_size, stereo);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-11-10 14:57:49 +00:00
|
|
|
if (frame_size > 0) {
|
2007-11-14 14:43:32 +00:00
|
|
|
spx_int16_t *frame_start = output + lookahead;
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-11-14 14:43:32 +00:00
|
|
|
if (channels == 2)
|
|
|
|
frame_start += lookahead;
|
|
|
|
ci->pcmbuf_insert(frame_start, NULL,
|
|
|
|
frame_size - lookahead);
|
|
|
|
lookahead = 0;
|
2007-02-10 06:11:30 +00:00
|
|
|
/* 2 bytes/sample */
|
2007-11-10 14:57:49 +00:00
|
|
|
cur_granule += frame_size / 2;
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-03-12 11:54:07 +00:00
|
|
|
ci->set_offset((long) ci->curpos);
|
2007-02-10 06:11:30 +00:00
|
|
|
|
2007-03-12 11:54:07 +00:00
|
|
|
ci->set_elapsed((samplerate == 0) ? 0 :
|
2018-10-15 21:59:13 +00:00
|
|
|
cur_granule * 1000LL / samplerate);
|
2007-02-10 06:11:30 +00:00
|
|
|
}
|
2007-02-09 10:06:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
packet_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2011-05-01 04:23:30 +00:00
|
|
|
error = CODEC_OK;
|
2007-02-09 10:06:53 +00:00
|
|
|
done:
|
2011-04-27 03:08:23 +00:00
|
|
|
/* Clean things up for the next track */
|
|
|
|
speex_bits_destroy(&bits);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
if (st)
|
2011-05-01 04:23:30 +00:00
|
|
|
speex_decoder_destroy(st);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
if (stream_init)
|
|
|
|
spx_ogg_stream_destroy(&os);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2007-02-09 10:06:53 +00:00
|
|
|
spx_ogg_sync_destroy(&oy);
|
2007-02-09 18:10:24 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
exit:
|
2007-02-09 10:06:53 +00:00
|
|
|
return error;
|
|
|
|
}
|