Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Untitled
Saturday, June 24th, 2006 at 4:36:41pm UTC 

  1. /***************************************************************************
  2.  *             __________               __   ___.
  3.  *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
  4.  *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
  5.  *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
  6.  *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
  7.  *                     \/            \/     \/    \/            \/
  8.  * $Id: speex.c,v 1.00 2006/02/18 20:22:03 lostlogic Exp $
  9.  *
  10.  * Copyright (C) 2006 Frederik M.J. Vestre
  11.  * Based on vorbis.c codec interface:
  12.  * Copyright (C) 2002 Bjørn Stenberg
  13.  *
  14.  * All files in this archive are subject to the GNU General Public License.
  15.  * See the file COPYING in the source tree root for full license agreement.
  16.  *
  17.  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  18.  * KIND, either express or implied.
  19.  *
  20.  ****************************************************************************/
  21.  
  22. #include "codeclib.h"
  23. // #include "Tremor/ivorbisfile.h"
  24. //#include "libspeex/speex/speex_bits.h"
  25. /*#include "libspeex/speex/speex_config_types.h"*/
  26. //#include "libspeex/speex/speex_types.h"
  27. #include "libogg/ogg/ogg.h"
  28. #include "libspeex/speex/speex.h"
  29. #include "libspeex/speex/speex_header.h"
  30. #include "libspeex/speex/speex_stereo.h"
  31. #include "libspeex/speex/speex_callbacks.h"
  32. #include "codeclib.h"
  33. #define MAX_FRAME_SIZE 2000
  34. #define CHUNKSIZE 1000
  35. #define SEEK_CHUNKSIZE 5000
  36. #include <stdio.h>
  37.  
  38. CODEC_HEADER
  39.  
  40. static struct codec_api *rb;
  41.  
  42. /* Some standard functions and variables needed by Tremor */
  43.  
  44. //int errno;
  45.  
  46. size_t read_handler(void *ptr, size_t size, size_t nmemb, void *datasource)
  47. {
  48.     (void)datasource;
  49.     return rb->read_filebuf(ptr, nmemb*size);
  50. }
  51.  
  52. int initial_seek_handler(void *datasource, ogg_int64_t offset, int whence)
  53. {
  54.     (void)datasource;
  55.     (void)offset;
  56.     (void)whence;
  57.     return -1;
  58. }
  59.  
  60. int seek_handler(void *datasource, ogg_int64_t offset, int whence)
  61. {
  62.     (void)datasource;
  63.  
  64.     if (whence == SEEK_CUR) {
  65.         offset += rb->curpos;
  66.     } else if (whence == SEEK_END) {
  67.         offset += rb->filesize;
  68.     }
  69.  
  70.     if (rb->seek_buffer(offset)) {
  71.         return 0;
  72.     }
  73.  
  74.     return -1;
  75. }
  76. //void get_more_data(ogg_sync_state *oy,struct codec_api *rb,char* buffer){
  77. void get_more_data(ogg_sync_state *oy,ogg_int64_t  *curoffset,struct codec_api *rb){
  78.         int bytes;
  79.         char *buffer;
  80.         buffer=(char *)ogg_sync_buffer(oy,1000);//CHUNKSIZE);
  81.         bytes=rb->read_filebuf(buffer, sizeof(char)*1000);
  82.         ogg_sync_wrote(oy,bytes);
  83.         *curoffset+=bytes;
  84. }
  85.  
  86. void get_more_data_2(ogg_sync_state *oy,ogg_int64_t  *curoffset,struct codec_api *rb,ogg_int16_t datalen){
  87.         int bytes;
  88.         char *buffer;
  89.         buffer=(char *)ogg_sync_buffer(oy,datalen);//CHUNKSIZE);
  90.         bytes=rb->read_filebuf(buffer, sizeof(char)*datalen);
  91.         ogg_sync_wrote(oy,bytes);
  92.         *curoffset+=bytes;
  93. }
  94.  
  95. /* The read/seek functions track absolute position within the stream */
  96.  
  97. /* from the head of the stream, get the next page.  boundary specifies
  98.    if the function is allowed to fetch more data from the stream (and
  99.    how much) or only use internally buffered data.
  100.  
  101.    boundary: -1) unbounded search
  102.               0) read no additional data; use cached only
  103.               n) search for a new page beginning for n bytes
  104.  
  105.    return:   <0) did not find a page (OV_FALSE, OV_EOF, OV_EREAD)
  106.               n) found a page at absolute offset n
  107. */
  108.  
  109. static ogg_int64_t get_next_page(ogg_sync_state *oy,ogg_page *og,ogg_int64_t  *curoffset,
  110.         ogg_int64_t boundary,struct codec_api *rb){
  111.   ogg_int64_t localoffset=*curoffset;
  112.   if(boundary>0)boundary+=*curoffset;
  113.   while(1){
  114.     long more;
  115.     //if(boundary>0 && vf->offset>=boundary)return(OV_FALSE);
  116.     more=ogg_sync_pageseek(oy,og);
  117.     if(more<0){
  118.       /* skipped n bytes */
  119.       localoffset-=more;
  120.     }else{
  121.       if(more==0){
  122.         /* send more paramedics */
  123.         if(!boundary)return(-1);
  124.         {
  125.           long ret=1;
  126.           ret=*curoffset;
  127.           get_more_data(oy,curoffset,rb);
  128.           LOGF("Gmbgnp:%d,%d,%d\n",ret,*curoffset,(*curoffset-ret));
  129.           ret=*curoffset-ret;
  130.           LOGF("Gmbgnpp:%d\n",ret);
  131.           if(ret==0)return(-2);
  132.           if(ret<0){
  133.                 LOGF("GmbgnRN:%d\n",ret);
  134.                 return(-3);
  135.         }
  136.         }
  137.       }else{
  138.         /* got a page.  Return the offset at the page beginning,
  139.            advance the internal offset past the page end */
  140.         ogg_int64_t ret=localoffset;
  141.         *curoffset=localoffset+more;
  142.         return(ret);
  143.       }
  144.     }
  145.   }
  146. }
  147.  
  148. /* find the latest page beginning before the current stream cursor
  149.    position. Much dirtier than the above as Ogg doesn't have any
  150.    backward search linkage.  no 'readp' as it will certainly have to
  151.    read. */
  152. /* returns offset or OV_EREAD, OV_FAULT and produces a refcounted page */
  153.  
  154. static ogg_int64_t get_prev_page(ogg_sync_state *oy,ogg_page *og,ogg_int64_t *curoffset,struct codec_api *rb){
  155.   ogg_int64_t begin=*curoffset-1;
  156.   ogg_int64_t end=begin;
  157.   ogg_int64_t ret;
  158.   ogg_int64_t offset=-1;
  159.  
  160.   while(offset==-1){
  161.     begin-=2*CHUNKSIZE;
  162.     if(begin<0){
  163.       return -3;//to early
  164.     }
  165.     rb->seek_buffer(begin);
  166.     ogg_sync_reset(oy);
  167.     get_more_data_2(oy,curoffset,rb,CHUNKSIZE);//read data into the ogg stream
  168.     ret=begin-*curoffset;
  169.     LOGF("Sbb:%d,%d,%d,%d\n",begin,end,begin-*curoffset,*curoffset);
  170.     *curoffset=begin;
  171.     while(*curoffset<end){
  172.       ret=get_next_page(oy,og,curoffset,end-*curoffset,rb);
  173.       if(ret==-3) return(-3);
  174.       if(ret<=0){
  175.         break;
  176.       }else if(*curoffset<end) {
  177.         offset=ret;
  178.       }
  179.     }
  180.   }
  181.   if(end-offset<0){
  182.     /* this shouldn't be possible */
  183.     return(-4);
  184.   }
  185.  
  186.   /* we have the offset.  Actually snork and hold the page now */
  187.   *curoffset=offset;
  188.   rb->seek_buffer(*curoffset);
  189.   ogg_sync_reset(oy);
  190.   get_more_data_2(oy,curoffset,rb,end-*curoffset);//read data into the ogg stream
  191.   ret=get_next_page(oy,og,curoffset,end-*curoffset,rb);
  192.   if(ret<0){
  193.     /* this shouldn't be possible */
  194.     return(-4);
  195.   }
  196.   return(offset);
  197. }
  198. int speex_seek_page_granule(
  199.         ogg_int64_t pos,
  200.         ogg_int64_t curpos,
  201.         ogg_sync_state *oy,
  202.         ogg_int64_t *curbyteoffset,
  203.         ogg_int64_t headerssize,
  204.         ogg_int64_t stream_length,
  205.         struct codec_api *rb
  206. ){
  207.         ogg_int64_t curoffset;
  208.         curoffset=*curbyteoffset;
  209.         ogg_int64_t offset=0;
  210.         ogg_page og={0,0,0,0};
  211.         ogg_packet op={0,0,0,0,0,0};
  212.         //#if 0
  213.         if(abs(pos-curpos)>10000 && headerssize>0 && curoffset-headerssize>10000){
  214.                 //if seeking for more that 10sec,
  215.                 //headersize is known & more than 10kb is played,
  216.                 //try to guess a place to seek from the number of
  217.                 //bytes playe for this position, this works best when
  218.                 //the bitrate is relativly constant.
  219.  
  220.                
  221.                 curoffset=(int)(((float)(*curbyteoffset-headerssize)*(float)pos)/(float)curpos);
  222.                 ogg_int64_t toffset=toffset;
  223.                 rb->seek_buffer(curoffset);
  224.                 ogg_sync_reset(oy);
  225.                 offset=get_next_page(oy,&og,&curoffset,-1,rb);
  226.                 if(offset<0){//could not find new page,use old offset
  227.                         LOGF("Seek/guess/fault:%d->-<-%d,%d:%d,%d\n",curpos,pos,0,offset,toffset,stream_length);
  228.                         curoffset=*curbyteoffset;
  229.                         rb->seek_buffer(curoffset);
  230.                         ogg_sync_reset(oy);
  231.                 }else{
  232.                         if(ogg_page_granulepos(&og)==0&&pos>5000){
  233.                                 LOGF("SEEK/guess/fault:%d->-<-%d,%d:%d,%d\n",curpos,ogg_page_granulepos(&og),pos,offset,toffset,stream_length);
  234.                                 curoffset=*curbyteoffset;
  235.                                 rb->seek_buffer(curoffset);
  236.                                 ogg_sync_reset(oy);
  237.                         }else{
  238.                                 LOGF("Seek/guess:%d->%d<-%d,%d:%d,%d\n",curpos,ogg_page_granulepos(&og),pos,offset,toffset,stream_length);
  239.                                 curoffset=offset;
  240.                                 curpos=ogg_page_granulepos(&og);
  241.                         }
  242.                 }
  243.                
  244.         }
  245.         //#endif
  246.         //which way do we want to seek?
  247.         if(curpos>pos){//backwards
  248.                 LOGF("Sbw\n");
  249.                 while((offset=get_prev_page(oy,&og,&curoffset,rb))>0){
  250.                         LOGF("Gp:%d,%d,%d,%d,%d,%d\n",ogg_page_granulepos(&og),curoffset,offset,pos,pos-ogg_page_granulepos(&og),2);
  251.                         if(ogg_page_granulepos(&og)<pos){
  252.                                 *curbyteoffset=curoffset;
  253.                                 LOGF("GP:%d,%d,%d:%d=%d\n",ogg_page_granulepos(&og),curoffset,curpos-ogg_page_granulepos(&og),curpos-pos);
  254.                                 return 1;
  255.                         }
  256.                         curoffset=offset;
  257.                 }
  258.                 LOGF("Srr:%d\n",offset);
  259.         }else{//forwards
  260.                 LOGF("Sfw\n");
  261.                 while((offset=get_next_page(oy,&og,&curoffset,-1,rb))>0){
  262.                         LOGF("gp:%d,%d,%d,%d,%d,%d\n",ogg_page_granulepos(&og),curoffset,offset,pos,pos-ogg_page_granulepos(&og),2);
  263.                         if(ogg_page_granulepos(&og)>pos){
  264.                                 LOGF("gP:%d,%d,%d:%d=%d\n",ogg_page_granulepos(&og),curoffset,curpos-ogg_page_granulepos(&og),curpos-pos);
  265.                                 offset=get_prev_page(oy,&og,&curoffset,rb);
  266.                                 *curbyteoffset=offset;
  267.                                 return 1;
  268.                         }
  269.                 }
  270.         }
  271.         rb->seek_buffer(*curbyteoffset);
  272.         ogg_sync_reset(oy);
  273.         LOGF("Seek failed:%d\n",offset);
  274.         rb->splash(HZ*2, true, "Seek failed");
  275.         return -1;
  276. }
  277.  
  278.  
  279.  
  280.  
  281.  
  282. int close_handler(void *datasource)
  283. {
  284.     (void)datasource;
  285.     return 0;
  286. }
  287.  
  288. long tell_handler(void *datasource)
  289. {
  290.     (void)datasource;
  291.     return rb->curpos;
  292. }
  293.  
  294. static void *process_header(ogg_packet *op, int enh_enabled, int *frame_size, int *rate, int *nframes, int *channels, SpeexStereoState *stereo, int *extra_headers)
  295. {
  296.    void *st;
  297.    const SpeexMode *mode;
  298.    SpeexHeader *header;
  299.    int modeID;
  300.    SpeexCallback callback;
  301.      
  302.    header = speex_packet_to_header((char*)op->packet, op->bytes);
  303.    if (!header)
  304.    {
  305.            rb->splash(HZ*5, true, "Can't read header");
  306.        DEBUGF ("Cannot read header\n");
  307.       return NULL;
  308.    }
  309.    if (header->mode >= SPEEX_NB_MODES)
  310.    {
  311.            rb->splash(HZ*5, true, "Mode does not (yet/any longer) exist in this version");
  312.         DEBUGF ("Mode does not (yet/any longer) exist in this version\n");
  313.       return NULL;
  314.    }
  315.      
  316.    modeID = header->mode;
  317.  
  318.    mode = speex_lib_get_mode (modeID);
  319.    
  320.    if (header->speex_version_id > 1)
  321.    {
  322.            rb->splash(HZ*5, true, "Undecodeable bitstream");
  323.            DEBUGF("Undecodeable bitstream");
  324.            //fprintf (stderr, "This file was encoded with Speex bit-stream version %d, which I don't know how to decode\n", header->speex_version_id);
  325.       return NULL;
  326.    }
  327.  
  328.    if (mode->bitstream_version < header->mode_bitstream_version)
  329.    {
  330.            rb->splash(HZ*5, true,"Undecodeable bitstream2");
  331.            DEBUGF("Undecodeable bitstream2");
  332.            //fprintf (stderr, "The file was encoded with a newer version of Speex. You need to upgrade in order to play it.\n");
  333.       return NULL;
  334.    }
  335.    if (mode->bitstream_version > header->mode_bitstream_version)
  336.    {
  337.         rb->splash(HZ*5, true,"Too old bitstream");
  338.         DEBUGF("Too old bitstream");
  339.       //fprintf (stderr, "The file was encoded with an older version of Speex. You would need to downgrade the version in order to play it.\n");
  340.       return NULL;
  341.    }
  342.    
  343.    st = speex_decoder_init(mode);
  344.    if (!st)
  345.    {
  346.         rb->splash(HZ*5, true,"Decoder init failed");
  347.       DEBUGF("Decoder init failed");
  348.       return NULL;
  349.    }
  350.    speex_decoder_ctl(st, SPEEX_SET_ENH, &enh_enabled);
  351.    speex_decoder_ctl(st, SPEEX_GET_FRAME_SIZE, frame_size);
  352.  
  353.    if (*channels==-1)
  354.       *channels = header->nb_channels;
  355.  
  356.    if (!(*channels==1))
  357.    {
  358.       callback.callback_id = SPEEX_INBAND_STEREO;
  359.       callback.func = speex_std_stereo_request_handler;
  360.       callback.data = stereo;
  361.       speex_decoder_ctl(st, SPEEX_SET_HANDLER, &callback);
  362.    }
  363.    if (!*rate)
  364.       *rate = header->rate;
  365.  
  366.    speex_decoder_ctl(st, SPEEX_SET_SAMPLING_RATE, rate);
  367.  
  368.    *nframes = header->frames_per_packet;
  369.  
  370.  
  371.    if (*channels == 2) {
  372.       rb->configure(DSP_SET_STEREO_MODE, (int *)STEREO_INTERLEAVED);
  373.    } else if (*channels == 1) {
  374.       rb->configure(DSP_SET_STEREO_MODE, (int *)STEREO_MONO);
  375.    }
  376.  
  377.    *extra_headers = header->extra_headers;
  378.  
  379.    codec_free(header);
  380.    return st;
  381. }
  382.  
  383.  
  384. #ifdef USE_IRAM
  385. extern char iramcopy[];
  386. extern char iramstart[];
  387. extern char iramend[];
  388. extern char iedata[];
  389. extern char iend[];
  390. #endif
  391.  
  392. /* this is the codec entry point */
  393. enum codec_status codec_start(struct codec_api *api)
  394. {
  395.     //printf("Codec start\n");
  396.      //ov_callbacks callbacks;
  397.     //OggVorbis_File vf;
  398.     SpeexBits vf;
  399.     //ogg_int32_t **pcm;
  400.     int error;
  401.     /*long n,bytes*/;
  402.     //int current_section;
  403.     int previous_section = -1;
  404.     int eof;
  405.     ogg_sync_state oy;
  406.     //ogg_page       *og;
  407.     ogg_page og;//={0,0,0,0};
  408.     ogg_packet     op;
  409.     ogg_stream_state os;
  410.    ogg_int64_t page_granule=0, cur_granule=0;
  411.    int enh_enabled;
  412.    int nframes=2;
  413.    //int print_bitrate=0;
  414.    //int close_in=0;
  415.    int eos=0;
  416.    SpeexStereoState stereo = SPEEX_STEREO_STATE_INIT;
  417.    int channels=-1;
  418.    int rate=0,samplerate=0;
  419.    int extra_headers;
  420.    int stream_init=0;
  421.    int /*skip_samples=0,*/ page_nb_packets,frame_size,packet_count=0;
  422.    int lookahead;
  423.    int headerssize=-1;
  424.    //float output[MAX_FRAME_SIZE];
  425.    short output[MAX_FRAME_SIZE];
  426.    enh_enabled = 1;
  427.    void *st;
  428.    ogg_int64_t cur_offset=0;
  429.    //char msgbuf[80];
  430. //     ogg_int64_t vf_offsets[2];
  431. //     ogg_int64_t vf_dataoffsets;
  432. //     ogg_uint32_t vf_serialnos;
  433. //     ogg_int64_t vf_pcmlengths[2];
  434.  
  435.     rb = api;
  436.  
  437.     #ifdef USE_IRAM
  438.     rb->memcpy(iramstart, iramcopy, iramend - iramstart);
  439.     rb->memset(iedata, 0, iend - iedata);
  440.     #endif
  441.  
  442.     //rb->configure(CODEC_DSP_ENABLE, (bool *)true);
  443.    
  444.     rb->configure(DSP_DITHER, (bool *)false);
  445. /*    rb->configure(DSP_SET_SAMPLE_DEPTH, (short *)16);*/
  446.     rb->configure(DSP_SET_SAMPLE_DEPTH, (long *)16);
  447.      //rb->configure(DSP_SET_CLIP_MAX, (long *)((1 << 64)));
  448. //     rb->configure(DSP_SET_CLIP_MIN, (long *)-((1 << 16)));
  449.  
  450. /*    rb->configure(DSP_SET_CLIP_MAX, (long *)());
  451.     rb->configure(DSP_SET_CLIP_MIN, (long *)-(-32768));*/
  452.     /* Note: These are sane defaults for these values.  Perhaps
  453.      * they should be set differently based on quality setting
  454.      */
  455.  
  456.     /* The chunk size below is magic.  If set any lower, resume
  457.      * doesn't work properly (ov_raw_seek() does the wrong thing).
  458.      */
  459.     rb->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (char *)(1000));
  460.     //printf("c1\n");
  461. /* We need to flush reserver memory every track load. */
  462. next_track:
  463.     if (codec_init(rb)) {
  464.         error = CODEC_ERROR;
  465.         //printf("Initr\n");
  466.         goto exit;
  467.     }
  468.     ogg_sync_init(&oy);
  469.     samplerate=rb->id3->frequency; //get samplerate from metadata
  470.                                    //parsing to support start offset
  471.     speex_bits_init(&vf);
  472.    
  473.     eof = 0;
  474.     while (!eof) {
  475.         rb->yield();
  476.         if (rb->stop_codec || rb->new_track)
  477.             break;
  478.  
  479.         if (rb->seek_time) {//seek (seeks to the page before the position
  480.                 if(samplerate!=0){
  481. //                  int speex_seek_page_granule(ogg_int64_t /*granule_*/pos,ogg_sync_state *oy,ogg_int64_t *curoffset,ogg_int64_t stream_length,struct codec_api *rb){
  482.                         LOGF("Seek page:%d,%d,%d,%d\n",
  483.                                 (rb->seek_time*samplerate)/1000,
  484.                                 page_granule,
  485.                                 (rb->id3->length*samplerate)/1000,
  486.                                 rb->seek_time,
  487.                                 (page_granule/samplerate)*1000,
  488.                                 samplerate);
  489.                         speex_seek_page_granule(
  490.                                 (rb->seek_time*samplerate)/1000,
  491.                                 page_granule,
  492.                                 &oy,
  493.                                 &cur_offset,
  494.                                 headerssize,
  495.                                 (rb->id3->length*samplerate)/1000,
  496.                                 rb
  497.                         );
  498.                 }
  499. /*            if (ov_time_seek(&vf, rb->seek_time - 1)) {
  500.                 //rb->logf("ov_time_seek failed");
  501.             }*/
  502.             rb->seek_complete();
  503.         }
  504.  
  505.         //char *data1,*data;
  506.         int /*i,*/ j;
  507. next_page:
  508.         //long nb_read;
  509.        
  510.        /*Get the ogg buffer for writing*/
  511.         //char * buffer;
  512.         get_more_data(&oy,&cur_offset,rb);
  513. //      if(bytes<0){
  514. //           DEBUGF("Decoding, read error\n");
  515. //           error=CODEC_ERROR;
  516. //           goto exit;
  517. //      }
  518.  
  519.         //cur_offset+=bytes;
  520.       //printf("Getbuf\n");
  521.         /*Loop for all complete pages we got (most likely only one)*/
  522.      
  523.       while (ogg_sync_pageout(&oy, &og)==1)
  524.       {
  525.          
  526.          int packet_no;
  527.          if (stream_init == 0) {
  528.             ogg_stream_init(&os, ogg_page_serialno(&og));
  529.             stream_init = 1;
  530.          }
  531.          /*Add page to the bitstream*/
  532.          ogg_stream_pagein(&os, &og);
  533.  
  534.          page_granule = ogg_page_granulepos(&og);
  535.          page_nb_packets = ogg_page_packets(&og);
  536.  
  537.          cur_granule = page_granule;
  538.  
  539.  
  540.          /*Extract all available packets*/
  541.          packet_no=0;
  542.          
  543.         while (!eos && ogg_stream_packetout(&os, &op)==1)
  544.          {
  545.            
  546.                 /*If first packet, process as Speex header*/
  547.             if (packet_count==0)
  548.             {
  549.                st = process_header(&op, enh_enabled, &frame_size, &samplerate, &nframes, &channels, &stereo, &extra_headers);
  550.                speex_decoder_ctl(st, SPEEX_GET_LOOKAHEAD, &lookahead);
  551.                if (!nframes)
  552.                   nframes=1;
  553.                if (!st){
  554.                         error=CODEC_ERROR;
  555.                         goto exit;
  556.                 }
  557.                 rb->id3->vbr = true;
  558.                 //int tmp;
  559.                 rb->id3->frequency = samplerate;
  560.                 rb->configure(DSP_SET_FREQUENCY, (int *)samplerate);//short
  561.                 headerssize+=og.header_len+og.body_len;//Speex header in it's own page, add the whole page  headersize
  562.                 //printf("Freq:%d,%d\n",rate,channels);
  563.                 //rb->configure(DSP_SWITCH_FREQUENCY, (bool *)true);//short
  564.  
  565.             } else if (packet_count<=1+extra_headers)
  566.             {
  567.                headerssize+=op.bytes;//add packet to headersize
  568.                /* Ignore extra headers */
  569.             } else {
  570.                if(packet_count==2+extra_headers){
  571.                  if (rb->id3->offset) {
  572.                                 rb->advance_buffer(rb->id3->offset);
  573.                                 rb->seek_buffer(rb->id3->offset);
  574.                                 cur_offset=rb->id3->offset;
  575.                                 ogg_sync_reset(&oy);
  576.                                 get_more_data(&oy,&cur_offset,rb);
  577.                                 goto next_page;
  578.                         }
  579.                }
  580.                //int lost=0;
  581.                packet_no++;
  582.  
  583.                /*End of stream condition*/
  584.                if (op.e_o_s)
  585.                   eos=1;
  586.  
  587.                /*Copy Ogg packet to Speex bitstream*/
  588.                speex_bits_read_from(&vf, (char*)op.packet, op.bytes);
  589.                //for (i=0;i<op.bytes;i++)
  590.                //    printf ("%d,", (int)op.packet[i]);
  591.  
  592.                for (j=0;j!=nframes;j++)
  593.                {
  594.                   int ret;
  595.                   /*Decode frame*/
  596.                   ret = speex_decode_int(st, &vf, output);
  597. /*             printf("\nDecoded:");
  598.                  for (i=frame_size;i<frame_size*channels;i++)
  599.                      printf ("%d,", (short)output[i]);*/
  600. //            printf("\n");
  601.  
  602.                   if (ret==-1)
  603.                      break;
  604.                   if (ret==-2)
  605.                   {
  606.                      //printf ("Decoding error: corrupted stream?\n");
  607.                      break;
  608.                   }
  609.                   if (speex_bits_remaining(&vf)<0)
  610.                   {
  611.                      //printf ("Decoding overflow: corrupted stream?\n");
  612.                      break;
  613.                   }
  614.                   if (channels==2)
  615.                      speex_decode_stereo_int(output, frame_size, &stereo);//vector rand hack
  616.                   speex_decoder_ctl(st, SPEEX_GET_BITRATE, &rate);
  617.                   rb->id3->bitrate=(rate/1000);//k(bps)
  618.  
  619.  
  620.                   {
  621.                      //int frame_offset = 0;
  622.                      int new_frame_size = frame_size;
  623.  
  624.                      if (new_frame_size>0)
  625.                      { 
  626.                            /*fwrite(out+frame_offset*channels, sizeof(short), new_frame_size*channels, fout);*/
  627.                         //fwrite(output+frame_offset*channels, sizeof(short), new_frame_size*channels, fout);
  628.                         //audio_size+=sizeof(short)*new_frame_size*channels;
  629. //                printf("Inspbuf%d,%d>",sizeof(spx_int16_t) * new_frame_size*channels,new_frame_size,channels/*,frame_offset*/);
  630. //                int i;
  631. //                for (i=0;i<frame_size*channels;i++)
  632. //                     printf ("%d,", (short)output[i]);
  633. //                for (i=0;i<frame_size*channels;i++)
  634. //                     printf ("%d,", (short)output[i]);
  635.  
  636.                         //write((const char*)output,sizeof(short),new_frame_size*channels);
  637.                         //printf("<\n");
  638.                         while (!rb->pcmbuf_insert_split((const char*)output,(const char*)output+new_frame_size,sizeof(short) * new_frame_size*channels)) {
  639.                                 rb->sleep(1);
  640.                         }
  641.                         cur_granule+=new_frame_size/2;/*2 bytes/sample*/
  642.                         rb->set_offset((long)cur_offset);
  643.                         rb->set_elapsed((samplerate==0)?0:cur_granule*1000/samplerate);
  644.                      }
  645.                     //return CODEC_OK;
  646.                   }
  647.                }
  648.             }
  649.             packet_count++;
  650.          }
  651.       }
  652.    }
  653.  
  654.  
  655.     if (rb->request_next_track()) {
  656.         /* Clean things up for the next track */
  657. /*        vf.dataoffsets = NULL;
  658.         vf.offsets = NULL;
  659.         vf.serialnos = NULL;
  660.         vf.pcmlengths = NULL;*/
  661.         speex_bits_reset(&vf);//ov_clear(&vf);
  662.         if (stream_init)
  663.                 ogg_stream_clear(&os);
  664.         ogg_sync_clear(&oy);
  665.         ogg_sync_init(&oy);
  666.         previous_section = -1;
  667. //      stereo = SPEEX_STEREO_STATE_INIT;
  668.         goto next_track;
  669.     }
  670.        
  671.     error = CODEC_OK;
  672. exit:
  673.     speex_bits_destroy(&vf);
  674.     if (stream_init)
  675.        ogg_stream_clear(&os);
  676.     ogg_sync_clear(&oy);
  677.     return error;
  678. }

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

comments powered by Disqus
worth-right
worth-right