Subversion Repositories Kolibri OS

Rev

Rev 1905 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.         libmpg123: MPEG Audio Decoder library
  3.  
  4.         copyright 1995-2012 by the mpg123 project - free software under the terms of the LGPL 2.1
  5.         see COPYING and AUTHORS files in distribution or http://mpg123.org
  6.  
  7. */
  8.  
  9. #include "mpg123lib_intern.h"
  10. #include "icy2utf8.h"
  11. #include "debug.h"
  12.  
  13. #include "gapless.h"
  14.  
  15. #define SEEKFRAME(mh) ((mh)->ignoreframe < 0 ? 0 : (mh)->ignoreframe)
  16.  
  17. static int initialized = 0;
  18.  
  19. int attribute_align_arg mpg123_init(void)
  20. {
  21.         if((sizeof(short) != 2) || (sizeof(long) < 4)) return MPG123_BAD_TYPES;
  22.  
  23.         if(initialized) return MPG123_OK; /* no need to initialize twice */
  24.  
  25. #ifndef NO_LAYER12
  26.         init_layer12(); /* inits also shared tables with layer1 */
  27. #endif
  28. #ifndef NO_LAYER3
  29.         init_layer3();
  30. #endif
  31.         prepare_decode_tables();
  32.         check_decoders();
  33.         initialized = 1;
  34.         return MPG123_OK;
  35. }
  36.  
  37. void attribute_align_arg mpg123_exit(void)
  38. {
  39.         /* nothing yet, but something later perhaps */
  40. }
  41.  
  42. /* create a new handle with specified decoder, decoder can be "", "auto" or NULL for auto-detection */
  43. mpg123_handle attribute_align_arg *mpg123_new(const char* decoder, int *error)
  44. {
  45.         return mpg123_parnew(NULL, decoder, error);
  46. }
  47.  
  48. /* ...the full routine with optional initial parameters to override defaults. */
  49. mpg123_handle attribute_align_arg *mpg123_parnew(mpg123_pars *mp, const char* decoder, int *error)
  50. {
  51.         mpg123_handle *fr = NULL;
  52.         int err = MPG123_OK;
  53.  
  54.         if(initialized) fr = (mpg123_handle*) malloc(sizeof(mpg123_handle));
  55.         else err = MPG123_NOT_INITIALIZED;
  56.         if(fr != NULL)
  57.         {
  58.                 frame_init_par(fr, mp);
  59.                 debug("cpu opt setting");
  60.                 if(frame_cpu_opt(fr, decoder) != 1)
  61.                 {
  62.                         err = MPG123_BAD_DECODER;
  63.                         frame_exit(fr);
  64.                         free(fr);
  65.                         fr = NULL;
  66.                 }
  67.         }
  68.         if(fr != NULL)
  69.         {
  70.                 fr->decoder_change = 1;
  71.         }
  72.         else if(err == MPG123_OK) err = MPG123_OUT_OF_MEM;
  73.  
  74.         if(error != NULL) *error = err;
  75.         return fr;
  76. }
  77.  
  78. int attribute_align_arg mpg123_decoder(mpg123_handle *mh, const char* decoder)
  79. {
  80.         enum optdec dt = dectype(decoder);
  81.  
  82.         if(mh == NULL) return MPG123_ERR;
  83.  
  84.         if(dt == nodec)
  85.         {
  86.                 mh->err = MPG123_BAD_DECODER;
  87.                 return MPG123_ERR;
  88.         }
  89.         if(dt == mh->cpu_opts.type) return MPG123_OK;
  90.  
  91.         /* Now really change. */
  92.         /* frame_exit(mh);
  93.         frame_init(mh); */
  94.         debug("cpu opt setting");
  95.         if(frame_cpu_opt(mh, decoder) != 1)
  96.         {
  97.                 mh->err = MPG123_BAD_DECODER;
  98.                 frame_exit(mh);
  99.                 return MPG123_ERR;
  100.         }
  101.         /* New buffers for decoder are created in frame_buffers() */
  102.         if((frame_outbuffer(mh) != 0))
  103.         {
  104.                 mh->err = MPG123_NO_BUFFERS;
  105.                 frame_exit(mh);
  106.                 return MPG123_ERR;
  107.         }
  108.         /* Do _not_ call decode_update here! That is only allowed after a first MPEG frame has been met. */
  109.         mh->decoder_change = 1;
  110.         return MPG123_OK;
  111. }
  112.  
  113. int attribute_align_arg mpg123_param(mpg123_handle *mh, enum mpg123_parms key, long val, double fval)
  114. {
  115.         int r;
  116.  
  117.         if(mh == NULL) return MPG123_ERR;
  118.         r = mpg123_par(&mh->p, key, val, fval);
  119.         if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; }
  120.         else
  121.         { /* Special treatment for some settings. */
  122. #ifdef FRAME_INDEX
  123.                 if(key == MPG123_INDEX_SIZE)
  124.                 { /* Apply frame index size and grow property on the fly. */
  125.                         r = frame_index_setup(mh);
  126.                         if(r != MPG123_OK) mh->err = MPG123_INDEX_FAIL;
  127.                 }
  128. #endif
  129. #ifndef NO_FEEDER
  130.                 /* Feeder pool size is applied right away, reader will react to that. */
  131.                 if(key == MPG123_FEEDPOOL || key == MPG123_FEEDBUFFER)
  132.                 bc_poolsize(&mh->rdat.buffer, mh->p.feedpool, mh->p.feedbuffer);
  133. #endif
  134.         }
  135.         return r;
  136. }
  137.  
  138. int attribute_align_arg mpg123_par(mpg123_pars *mp, enum mpg123_parms key, long val, double fval)
  139. {
  140.         int ret = MPG123_OK;
  141.  
  142.         if(mp == NULL) return MPG123_BAD_PARS;
  143.         switch(key)
  144.         {
  145.                 case MPG123_VERBOSE:
  146.                         mp->verbose = val;
  147.                 break;
  148.                 case MPG123_FLAGS:
  149. #ifndef GAPLESS
  150.                         if(val & MPG123_GAPLESS) ret = MPG123_NO_GAPLESS;
  151. #endif
  152.                         if(ret == MPG123_OK) mp->flags = val;
  153.                         debug1("set flags to 0x%lx", (unsigned long) mp->flags);
  154.                 break;
  155.                 case MPG123_ADD_FLAGS:
  156. #ifndef GAPLESS
  157.                         /* Enabling of gapless mode doesn't work when it's not there, but disabling (below) is no problem. */
  158.                         if(val & MPG123_GAPLESS) ret = MPG123_NO_GAPLESS;
  159.                         else
  160. #endif
  161.                         mp->flags |= val;
  162.                         debug1("set flags to 0x%lx", (unsigned long) mp->flags);
  163.                 break;
  164.                 case MPG123_REMOVE_FLAGS:
  165.                         mp->flags &= ~val;
  166.                         debug1("set flags to 0x%lx", (unsigned long) mp->flags);
  167.                 break;
  168.                 case MPG123_FORCE_RATE: /* should this trigger something? */
  169. #ifdef NO_NTOM
  170.                         if(val > 0)
  171.                         ret = MPG123_BAD_RATE;
  172. #else
  173.                         if(val > 96000) ret = MPG123_BAD_RATE;
  174.                         else mp->force_rate = val < 0 ? 0 : val; /* >0 means enable, 0 disable */
  175. #endif
  176.                 break;
  177.                 case MPG123_DOWN_SAMPLE:
  178. #ifdef NO_DOWNSAMPLE
  179.                         if(val != 0) ret = MPG123_BAD_RATE;
  180. #else
  181.                         if(val < 0 || val > 2) ret = MPG123_BAD_RATE;
  182.                         else mp->down_sample = (int)val;
  183. #endif
  184.                 break;
  185.                 case MPG123_RVA:
  186.                         if(val < 0 || val > MPG123_RVA_MAX) ret = MPG123_BAD_RVA;
  187.                         else mp->rva = (int)val;
  188.                 break;
  189.                 case MPG123_DOWNSPEED:
  190.                         mp->halfspeed = val < 0 ? 0 : val;
  191.                 break;
  192.                 case MPG123_UPSPEED:
  193.                         mp->doublespeed = val < 0 ? 0 : val;
  194.                 break;
  195.                 case MPG123_ICY_INTERVAL:
  196. #ifndef NO_ICY
  197.                         mp->icy_interval = val > 0 ? val : 0;
  198. #else
  199.                         if(val > 0) ret = MPG123_BAD_PARAM;
  200. #endif
  201.                 break;
  202.                 case MPG123_OUTSCALE:
  203.                         /* Choose the value that is non-zero, if any.
  204.                            Downscaling integers to 1.0 . */
  205.                         mp->outscale = val == 0 ? fval : (double)val/SHORT_SCALE;
  206.                 break;
  207.                 case MPG123_TIMEOUT:
  208. #ifdef TIMEOUT_READ
  209.                         mp->timeout = val >= 0 ? val : 0;
  210. #else
  211.                         if(val > 0) ret = MPG123_NO_TIMEOUT;
  212. #endif
  213.                 break;
  214.                 case MPG123_RESYNC_LIMIT:
  215.                         mp->resync_limit = val;
  216.                 break;
  217.                 case MPG123_INDEX_SIZE:
  218. #ifdef FRAME_INDEX
  219.                         mp->index_size = val;
  220. #else
  221.                         ret = MPG123_NO_INDEX;
  222. #endif
  223.                 break;
  224.                 case MPG123_PREFRAMES:
  225.                         if(val >= 0) mp->preframes = val;
  226.                         else ret = MPG123_BAD_VALUE;
  227.                 break;
  228.                 case MPG123_FEEDPOOL:
  229. #ifndef NO_FEEDER
  230.                         if(val >= 0) mp->feedpool = val;
  231.                         else ret = MPG123_BAD_VALUE;
  232. #else
  233.                         ret = MPG123_MISSING_FEATURE;
  234. #endif
  235.                 break;
  236.                 case MPG123_FEEDBUFFER:
  237. #ifndef NO_FEEDER
  238.                         if(val > 0) mp->feedbuffer = val;
  239.                         else ret = MPG123_BAD_VALUE;
  240. #else
  241.                         ret = MPG123_MISSING_FEATURE;
  242. #endif
  243.                 break;
  244.                 default:
  245.                         ret = MPG123_BAD_PARAM;
  246.         }
  247.         return ret;
  248. }
  249.  
  250. int attribute_align_arg mpg123_getparam(mpg123_handle *mh, enum mpg123_parms key, long *val, double *fval)
  251. {
  252.         int r;
  253.  
  254.         if(mh == NULL) return MPG123_ERR;
  255.         r = mpg123_getpar(&mh->p, key, val, fval);
  256.         if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; }
  257.         return r;
  258. }
  259.  
  260. int attribute_align_arg mpg123_getpar(mpg123_pars *mp, enum mpg123_parms key, long *val, double *fval)
  261. {
  262.         int ret = 0;
  263.  
  264.         if(mp == NULL) return MPG123_BAD_PARS;
  265.         switch(key)
  266.         {
  267.                 case MPG123_VERBOSE:
  268.                         if(val) *val = mp->verbose;
  269.                 break;
  270.                 case MPG123_FLAGS:
  271.                 case MPG123_ADD_FLAGS:
  272.                         if(val) *val = mp->flags;
  273.                 break;
  274.                 case MPG123_FORCE_RATE:
  275.                         if(val)
  276. #ifdef NO_NTOM
  277.                         *val = 0;
  278. #else
  279.                         *val = mp->force_rate;
  280. #endif
  281.                 break;
  282.                 case MPG123_DOWN_SAMPLE:
  283.                         if(val) *val = mp->down_sample;
  284.                 break;
  285.                 case MPG123_RVA:
  286.                         if(val) *val = mp->rva;
  287.                 break;
  288.                 case MPG123_DOWNSPEED:
  289.                         if(val) *val = mp->halfspeed;
  290.                 break;
  291.                 case MPG123_UPSPEED:
  292.                         if(val) *val = mp->doublespeed;
  293.                 break;
  294.                 case MPG123_ICY_INTERVAL:
  295. #ifndef NO_ICY
  296.                         if(val) *val = (long)mp->icy_interval;
  297. #else
  298.                         if(val) *val = 0;
  299. #endif
  300.                 break;
  301.                 case MPG123_OUTSCALE:
  302.                         if(fval) *fval = mp->outscale;
  303.                         if(val) *val = (long)(mp->outscale*SHORT_SCALE);
  304.                 break;
  305.                 case MPG123_RESYNC_LIMIT:
  306.                         if(val) *val = mp->resync_limit;
  307.                 break;
  308.                 case MPG123_INDEX_SIZE:
  309.                         if(val)
  310. #ifdef FRAME_INDEX
  311.                         *val = mp->index_size;
  312. #else
  313.                         *val = 0; /* graceful fallback: no index is index of zero size */
  314. #endif
  315.                 break;
  316.                 case MPG123_PREFRAMES:
  317.                         *val = mp->preframes;
  318.                 break;
  319.                 case MPG123_FEEDPOOL:
  320. #ifndef NO_FEEDER
  321.                         *val = mp->feedpool;
  322. #else
  323.                         ret = MPG123_MISSING_FEATURE;
  324. #endif
  325.                 break;
  326.                 case MPG123_FEEDBUFFER:
  327. #ifndef NO_FEEDER
  328.                         *val = mp->feedbuffer;
  329. #else
  330.                         ret = MPG123_MISSING_FEATURE;
  331. #endif
  332.                 break;
  333.                 default:
  334.                         ret = MPG123_BAD_PARAM;
  335.         }
  336.         return ret;
  337. }
  338.  
  339. int attribute_align_arg mpg123_getstate(mpg123_handle *mh, enum mpg123_state key, long *val, double *fval)
  340. {
  341.         int ret = MPG123_OK;
  342.         long theval = 0;
  343.         double thefval = 0.;
  344.  
  345.         if(mh == NULL) return MPG123_ERR;
  346.  
  347.         switch(key)
  348.         {
  349.                 case MPG123_ACCURATE:
  350.                         theval = mh->state_flags & FRAME_ACCURATE;
  351.                 break;
  352.                 case MPG123_FRANKENSTEIN:
  353.                         theval = mh->state_flags & FRAME_FRANKENSTEIN;
  354.                 break;
  355.                 case MPG123_BUFFERFILL:
  356. #ifndef NO_FEEDER
  357.                 {
  358.                         size_t sval = bc_fill(&mh->rdat.buffer);
  359.                         theval = (long)sval;
  360.                         if((size_t)theval != sval)
  361.                         {
  362.                                 mh->err = MPG123_INT_OVERFLOW;
  363.                                 ret = MPG123_ERR;
  364.                         }
  365.                 }
  366. #else
  367.                         mh->err = MPG123_MISSING_FEATURE;
  368.                         ret = MPG123_ERR;
  369. #endif
  370.                 break;
  371.                 default:
  372.                         mh->err = MPG123_BAD_KEY;
  373.                         ret = MPG123_ERR;
  374.         }
  375.  
  376.         if(val  != NULL) *val  = theval;
  377.         if(fval != NULL) *fval = thefval;
  378.  
  379.         return ret;
  380. }
  381.  
  382. int attribute_align_arg mpg123_eq(mpg123_handle *mh, enum mpg123_channels channel, int band, double val)
  383. {
  384.         if(mh == NULL) return MPG123_ERR;
  385.         if(band < 0 || band > 31){ mh->err = MPG123_BAD_BAND; return MPG123_ERR; }
  386.         switch(channel)
  387.         {
  388.                 case MPG123_LEFT|MPG123_RIGHT:
  389.                         mh->equalizer[0][band] = mh->equalizer[1][band] = DOUBLE_TO_REAL(val);
  390.                 break;
  391.                 case MPG123_LEFT:  mh->equalizer[0][band] = DOUBLE_TO_REAL(val); break;
  392.                 case MPG123_RIGHT: mh->equalizer[1][band] = DOUBLE_TO_REAL(val); break;
  393.                 default:
  394.                         mh->err=MPG123_BAD_CHANNEL;
  395.                         return MPG123_ERR;
  396.         }
  397.         mh->have_eq_settings = TRUE;
  398.         return MPG123_OK;
  399. }
  400.  
  401. double attribute_align_arg mpg123_geteq(mpg123_handle *mh, enum mpg123_channels channel, int band)
  402. {
  403.         double ret = 0.;
  404.  
  405.         if(mh == NULL) return MPG123_ERR;
  406.  
  407.         /* Handle this gracefully. When there is no band, it has no volume. */
  408.         if(band > -1 && band < 32)
  409.         switch(channel)
  410.         {
  411.                 case MPG123_LEFT|MPG123_RIGHT:
  412.                         ret = 0.5*(REAL_TO_DOUBLE(mh->equalizer[0][band])+REAL_TO_DOUBLE(mh->equalizer[1][band]));
  413.                 break;
  414.                 case MPG123_LEFT:  ret = REAL_TO_DOUBLE(mh->equalizer[0][band]); break;
  415.                 case MPG123_RIGHT: ret = REAL_TO_DOUBLE(mh->equalizer[1][band]); break;
  416.                 /* Default case is already handled: ret = 0 */
  417.         }
  418.  
  419.         return ret;
  420. }
  421.  
  422.  
  423. /* plain file access, no http! */
  424. int attribute_align_arg mpg123_open(mpg123_handle *mh, const char *path)
  425. {
  426.         if(mh == NULL) return MPG123_ERR;
  427.  
  428.         mpg123_close(mh);
  429.         return open_stream(mh, path, -1);
  430. }
  431.  
  432. int attribute_align_arg mpg123_open_fd(mpg123_handle *mh, int fd)
  433. {
  434.         if(mh == NULL) return MPG123_ERR;
  435.  
  436.         mpg123_close(mh);
  437.         return open_stream(mh, NULL, fd);
  438. }
  439.  
  440. int attribute_align_arg mpg123_open_handle(mpg123_handle *mh, void *iohandle)
  441. {
  442.         if(mh == NULL) return MPG123_ERR;
  443.  
  444.         mpg123_close(mh);
  445.         if(mh->rdat.r_read_handle == NULL)
  446.         {
  447.                 mh->err = MPG123_BAD_CUSTOM_IO;
  448.                 return MPG123_ERR;
  449.         }
  450.         return open_stream_handle(mh, iohandle);
  451. }
  452.  
  453. int attribute_align_arg mpg123_open_feed(mpg123_handle *mh)
  454. {
  455.         if(mh == NULL) return MPG123_ERR;
  456.  
  457.         mpg123_close(mh);
  458.         return open_feed(mh);
  459. }
  460.  
  461. int attribute_align_arg mpg123_replace_reader( mpg123_handle *mh,
  462.                            ssize_t (*r_read) (int, void *, size_t),
  463.                            off_t   (*r_lseek)(int, off_t, int) )
  464. {
  465.         if(mh == NULL) return MPG123_ERR;
  466.  
  467.         mpg123_close(mh);
  468.         mh->rdat.r_read = r_read;
  469.         mh->rdat.r_lseek = r_lseek;
  470.         return MPG123_OK;
  471. }
  472.  
  473. int attribute_align_arg mpg123_replace_reader_handle( mpg123_handle *mh,
  474.                            ssize_t (*r_read) (void*, void *, size_t),
  475.                            off_t   (*r_lseek)(void*, off_t, int),
  476.                            void    (*cleanup)(void*)  )
  477. {
  478.         if(mh == NULL) return MPG123_ERR;
  479.  
  480.         mpg123_close(mh);
  481.         mh->rdat.r_read_handle = r_read;
  482.         mh->rdat.r_lseek_handle = r_lseek;
  483.         mh->rdat.cleanup_handle = cleanup;
  484.         return MPG123_OK;
  485. }
  486.  
  487. /* Update decoding engine for
  488.    a) a new choice of decoder
  489.    b) a changed native format of the MPEG stream
  490.    ... calls are only valid after parsing some MPEG frame! */
  491. int decode_update(mpg123_handle *mh)
  492. {
  493.         long native_rate;
  494.         int b;
  495.  
  496.         if(mh->num < 0)
  497.         {
  498.                 if(!(mh->p.flags & MPG123_QUIET)) error("decode_update() has been called before reading the first MPEG frame! Internal programming error.");
  499.  
  500.                 mh->err = MPG123_BAD_DECODER_SETUP;
  501.                 return MPG123_ERR;
  502.         }
  503.  
  504.         native_rate = frame_freq(mh);
  505.  
  506.         b = frame_output_format(mh); /* Select the new output format based on given constraints. */
  507.         if(b < 0) return MPG123_ERR;
  508.  
  509.         if(b == 1) mh->new_format = 1; /* Store for later... */
  510.  
  511.         debug3("updating decoder structure with native rate %li and af.rate %li (new format: %i)", native_rate, mh->af.rate, mh->new_format);
  512.         if(mh->af.rate == native_rate) mh->down_sample = 0;
  513.         else if(mh->af.rate == native_rate>>1) mh->down_sample = 1;
  514.         else if(mh->af.rate == native_rate>>2) mh->down_sample = 2;
  515.         else mh->down_sample = 3; /* flexible (fixed) rate */
  516.         switch(mh->down_sample)
  517.         {
  518.                 case 0:
  519.                 case 1:
  520.                 case 2:
  521.                         mh->down_sample_sblimit = SBLIMIT>>(mh->down_sample);
  522.                         /* With downsampling I get less samples per frame */
  523.                         mh->outblock = samples_to_storage(mh, (spf(mh)>>mh->down_sample));
  524.                 break;
  525. #ifndef NO_NTOM
  526.                 case 3:
  527.                 {
  528.                         if(synth_ntom_set_step(mh) != 0) return -1;
  529.                         if(frame_freq(mh) > mh->af.rate)
  530.                         {
  531.                                 mh->down_sample_sblimit = SBLIMIT * mh->af.rate;
  532.                                 mh->down_sample_sblimit /= frame_freq(mh);
  533.                         }
  534.                         else mh->down_sample_sblimit = SBLIMIT;
  535.                         mh->outblock = samples_to_storage(mh,
  536.                                          ( ( NTOM_MUL-1+spf(mh)
  537.                                            * (((size_t)NTOM_MUL*mh->af.rate)/frame_freq(mh))
  538.                                          )/NTOM_MUL ));
  539.                 }
  540.                 break;
  541. #endif
  542.         }
  543.  
  544.         if(!(mh->p.flags & MPG123_FORCE_MONO))
  545.         {
  546.                 if(mh->af.channels == 1) mh->single = SINGLE_MIX;
  547.                 else mh->single = SINGLE_STEREO;
  548.         }
  549.         else mh->single = (mh->p.flags & MPG123_FORCE_MONO)-1;
  550.         if(set_synth_functions(mh) != 0) return -1;;
  551.  
  552.         /* The needed size of output buffer may have changed. */
  553.         if(frame_outbuffer(mh) != MPG123_OK) return -1;
  554.  
  555.         do_rva(mh);
  556.         debug3("done updating decoder structure with native rate %li and af.rate %li and down_sample %i", frame_freq(mh), mh->af.rate, mh->down_sample);
  557.  
  558.         return 0;
  559. }
  560.  
  561. size_t attribute_align_arg mpg123_safe_buffer(void)
  562. {
  563.         /* real is the largest possible output (it's 32bit float, 32bit int or 64bit double). */
  564.         return sizeof(real)*2*1152*NTOM_MAX;
  565. }
  566.  
  567. size_t attribute_align_arg mpg123_outblock(mpg123_handle *mh)
  568. {
  569.         /* Try to be helpful and never return zero output block size. */
  570.         if(mh != NULL && mh->outblock > 0) return mh->outblock;
  571.         else return mpg123_safe_buffer();
  572. }
  573.  
  574. /* Read in the next frame we actually want for decoding.
  575.    This includes skipping/ignoring frames, in additon to skipping junk in the parser. */
  576. static int get_next_frame(mpg123_handle *mh)
  577. {
  578.         /* We have some decoder ready, if the desired decoder has changed,
  579.            it is OK to use the old one for ignoring frames and activating
  580.            the new one for real (decode_update()) after getting the frame. */
  581.         int change = mh->decoder_change;
  582.         do
  583.         {
  584.                 int b;
  585.                 /* Decode & discard some frame(s) before beginning. */
  586.                 if(mh->to_ignore && mh->num < mh->firstframe && mh->num >= mh->ignoreframe)
  587.                 {
  588.                         debug1("ignoring frame %li", (long)mh->num);
  589.                         /* Decoder structure must be current! decode_update has been called before... */
  590.                         (mh->do_layer)(mh); mh->buffer.fill = 0;
  591. #ifndef NO_NTOM
  592.                         /* The ignored decoding may have failed. Make sure ntom stays consistent. */
  593.                         if(mh->down_sample == 3) ntom_set_ntom(mh, mh->num+1);
  594. #endif
  595.                         mh->to_ignore = mh->to_decode = FALSE;
  596.                 }
  597.                 /* Read new frame data; possibly breaking out here for MPG123_NEED_MORE. */
  598.                 debug("read frame");
  599.                 mh->to_decode = FALSE;
  600.                 b = read_frame(mh); /* That sets to_decode only if a full frame was read. */
  601.                 debug4("read of frame %li returned %i (to_decode=%i) at sample %li", (long)mh->num, b, mh->to_decode, (long)mpg123_tell(mh));
  602.                 if(b == MPG123_NEED_MORE) return MPG123_NEED_MORE; /* need another call with data */
  603.                 else if(b <= 0)
  604.                 {
  605.                         /* More sophisticated error control? */
  606.                         if(b==0 || (mh->rdat.filelen >= 0 && mh->rdat.filepos == mh->rdat.filelen))
  607.                         { /* We simply reached the end. */
  608.                                 mh->track_frames = mh->num + 1;
  609.                                 debug("What about updating/checking gapless sample count here?");
  610.                                 return MPG123_DONE;
  611.                         }
  612.                         else return MPG123_ERR; /* Some real error. */
  613.                 }
  614.                 /* Now, there should be new data to decode ... and also possibly new stream properties */
  615.                 if(mh->header_change > 1)
  616.                 {
  617.                         debug("big header change");
  618.                         change = 1;
  619.                 }
  620.                 /* Now some accounting: Look at the numbers and decide if we want this frame. */
  621.                 ++mh->playnum;
  622.                 /* Plain skipping without decoding, only when frame is not ignored on next cycle. */
  623.                 if(mh->num < mh->firstframe || (mh->p.doublespeed && (mh->playnum % mh->p.doublespeed)))
  624.                 {
  625.                         if(!(mh->to_ignore && mh->num < mh->firstframe && mh->num >= mh->ignoreframe))
  626.                         {
  627.                                 frame_skip(mh);
  628.                                 /* Should one fix NtoM here or not?
  629.                                    It is not work the trouble for doublespeed, but what with leading frames? */
  630.                         }
  631.                 }
  632.                 /* Or, we are finally done and have a new frame. */
  633.                 else break;
  634.         } while(1);
  635.  
  636.         /* If we reach this point, we got a new frame ready to be decoded.
  637.            All other situations resulted in returns from the loop. */
  638.         if(change)
  639.         {
  640.                 if(decode_update(mh) < 0)  /* dito... */
  641.                 return MPG123_ERR;
  642.  
  643. debug1("new format: %i", mh->new_format);
  644.  
  645.                 mh->decoder_change = 0;
  646.                 if(mh->fresh)
  647.                 {
  648. #ifdef GAPLESS
  649.                         int b=0;
  650.                         /* Prepare offsets for gapless decoding. */
  651.                         debug1("preparing gapless stuff with native rate %li", frame_freq(mh));
  652.                         frame_gapless_realinit(mh);
  653.                         frame_set_frameseek(mh, mh->num);
  654. #endif
  655.                         mh->fresh = 0;
  656. #ifdef GAPLESS
  657.                         /* Could this possibly happen? With a real big gapless offset... */
  658.                         if(mh->num < mh->firstframe) b = get_next_frame(mh);
  659.                         if(b < 0) return b; /* Could be error, need for more, new format... */
  660. #endif
  661.                 }
  662.         }
  663.         return MPG123_OK;
  664. }
  665.  
  666. /* Assumption: A buffer full of zero samples can be constructed by repetition of this byte.
  667.    Oh, and it handles some format conversion.
  668.    Only to be used by decode_the_frame() ... */
  669. static int zero_byte(mpg123_handle *fr)
  670. {
  671. #ifndef NO_8BIT
  672.         return fr->af.encoding & MPG123_ENC_8 ? fr->conv16to8[0] : 0;
  673. #else
  674.         return 0; /* All normal signed formats have the zero here (even in byte form -- that may be an assumption for your funny machine...). */
  675. #endif
  676. }
  677.  
  678. /*
  679.         Not part of the api. This just decodes the frame and fills missing bits with zeroes.
  680.         There can be frames that are broken and thus make do_layer() fail.
  681. */
  682. static void decode_the_frame(mpg123_handle *fr)
  683. {
  684.         size_t needed_bytes = samples_to_storage(fr, frame_expect_outsamples(fr));
  685.         fr->clip += (fr->do_layer)(fr);
  686.         /*fprintf(stderr, "frame %"OFF_P": got %"SIZE_P" / %"SIZE_P"\n", fr->num,(size_p)fr->buffer.fill, (size_p)needed_bytes);*/
  687.         /* There could be less data than promised.
  688.            Also, then debugging, we look out for coding errors that could result in _more_ data than expected. */
  689. #ifdef DEBUG
  690.         if(fr->buffer.fill != needed_bytes)
  691.         {
  692. #endif
  693.                 if(fr->buffer.fill < needed_bytes)
  694.                 {
  695.                         if(VERBOSE2)
  696.                         fprintf(stderr, "Note: broken frame %li, filling up with %"SIZE_P" zeroes, from %"SIZE_P"\n", (long)fr->num, (size_p)(needed_bytes-fr->buffer.fill), (size_p)fr->buffer.fill);
  697.  
  698.                         /*
  699.                                 One could do a loop with individual samples instead... but zero is zero
  700.                                 Actually, that is wrong: zero is mostly a series of null bytes,
  701.                                 but we have funny 8bit formats that have a different opinion on zero...
  702.                                 Unsigned 16 or 32 bit formats are handled later.
  703.                         */
  704.                         memset( fr->buffer.data + fr->buffer.fill, zero_byte(fr), needed_bytes - fr->buffer.fill );
  705.  
  706.                         fr->buffer.fill = needed_bytes;
  707. #ifndef NO_NTOM
  708.                         /* ntom_val will be wrong when the decoding wasn't carried out completely */
  709.                         ntom_set_ntom(fr, fr->num+1);
  710. #endif
  711.                 }
  712. #ifdef DEBUG
  713.                 else
  714.                 {
  715.                         if(NOQUIET)
  716.                         error2("I got _more_ bytes than expected (%"SIZE_P" / %"SIZE_P"), that should not be possible!", (size_p)fr->buffer.fill, (size_p)needed_bytes);
  717.                 }
  718.         }
  719. #endif
  720.         postprocess_buffer(fr);
  721. }
  722.  
  723. /*
  724.         Decode the current frame into the frame structure's buffer, accessible at the location stored in <audio>, with <bytes> bytes available.
  725.         <num> will contain the last decoded frame number. This function should be called after mpg123_framebyframe_next positioned the stream at a
  726.         valid mp3 frame. The buffer contents will get lost on the next call to mpg123_framebyframe_next or mpg123_framebyframe_decode.
  727.         returns
  728.         MPG123_OK -- successfully decoded or ignored the frame, you get your output data or in case of ignored frames 0 bytes
  729.         MPG123_DONE -- decoding finished, should not happen
  730.         MPG123_ERR -- some error occured.
  731.         MPG123_ERR_NULL -- audio or bytes are not pointing to valid storage addresses
  732.         MPG123_BAD_HANDLE -- mh has not been initialized
  733.         MPG123_NO_SPACE -- not enough space in buffer for safe decoding, should not happen
  734. */
  735. int attribute_align_arg mpg123_framebyframe_decode(mpg123_handle *mh, off_t *num, unsigned char **audio, size_t *bytes)
  736. {
  737.         if(bytes == NULL) return MPG123_ERR_NULL;
  738.         if(audio == NULL) return MPG123_ERR_NULL;
  739.         if(mh == NULL) return MPG123_BAD_HANDLE;
  740.         if(mh->buffer.size < mh->outblock) return MPG123_NO_SPACE;
  741.  
  742.         *bytes = 0;
  743.         mh->buffer.fill = 0; /* always start fresh */
  744.         if(!mh->to_decode) return MPG123_OK;
  745.  
  746.         if(num != NULL) *num = mh->num;
  747.         debug("decoding");
  748.         decode_the_frame(mh);
  749.         mh->to_decode = mh->to_ignore = FALSE;
  750.         mh->buffer.p = mh->buffer.data;
  751.         FRAME_BUFFERCHECK(mh);
  752.         *audio = mh->buffer.p;
  753.         *bytes = mh->buffer.fill;
  754.         return MPG123_OK;
  755. }
  756.  
  757. /*
  758.         Find, read and parse the next mp3 frame while skipping junk and parsing id3 tags, lame headers, etc.
  759.         Prepares everything for decoding using mpg123_framebyframe_decode.
  760.         returns
  761.         MPG123_OK -- new frame was read and parsed, call mpg123_framebyframe_decode to actually decode
  762.         MPG123_NEW_FORMAT -- new frame was read, it results in changed output format, call mpg123_framebyframe_decode to actually decode
  763.         MPG123_BAD_HANDLE -- mh has not been initialized
  764.         MPG123_NEED_MORE  -- more input data is needed to advance to the next frame. supply more input data using mpg123_feed
  765. */
  766. int attribute_align_arg mpg123_framebyframe_next(mpg123_handle *mh)
  767. {
  768.         int b;
  769.         if(mh == NULL) return MPG123_BAD_HANDLE;
  770.  
  771.         mh->to_decode = mh->to_ignore = FALSE;
  772.         mh->buffer.fill = 0;
  773.  
  774.         b = get_next_frame(mh);
  775.         if(b < 0) return b;
  776.         debug1("got next frame, %i", mh->to_decode);
  777.  
  778.         /* mpg123_framebyframe_decode will return MPG123_OK with 0 bytes decoded if mh->to_decode is 0 */
  779.         if(!mh->to_decode)
  780.                 return MPG123_OK;
  781.  
  782.         if(mh->new_format)
  783.         {
  784.                 debug("notifiying new format");
  785.                 mh->new_format = 0;
  786.                 return MPG123_NEW_FORMAT;
  787.         }
  788.  
  789.         return MPG123_OK;
  790. }
  791.  
  792. /*
  793.         Put _one_ decoded frame into the frame structure's buffer, accessible at the location stored in <audio>, with <bytes> bytes available.
  794.         The buffer contents will be lost on next call to mpg123_decode_frame.
  795.         MPG123_OK -- successfully decoded the frame, you get your output data
  796.         MPg123_DONE -- This is it. End.
  797.         MPG123_ERR -- some error occured...
  798.         MPG123_NEW_FORMAT -- new frame was read, it results in changed output format -> will be decoded on next call
  799.         MPG123_NEED_MORE  -- that should not happen as this function is intended for in-library stream reader but if you force it...
  800.         MPG123_NO_SPACE   -- not enough space in buffer for safe decoding, also should not happen
  801.  
  802.         num will be updated to the last decoded frame number (may possibly _not_ increase, p.ex. when format changed).
  803. */
  804. int attribute_align_arg mpg123_decode_frame(mpg123_handle *mh, off_t *num, unsigned char **audio, size_t *bytes)
  805. {
  806.         if(bytes != NULL) *bytes = 0;
  807.         if(mh == NULL) return MPG123_ERR;
  808.         if(mh->buffer.size < mh->outblock) return MPG123_NO_SPACE;
  809.         mh->buffer.fill = 0; /* always start fresh */
  810.         while(TRUE)
  811.         {
  812.                 /* decode if possible */
  813.                 if(mh->to_decode)
  814.                 {
  815.                         if(mh->new_format)
  816.                         {
  817.                                 debug("notifiying new format");
  818.                                 mh->new_format = 0;
  819.                                 return MPG123_NEW_FORMAT;
  820.                         }
  821.                         if(num != NULL) *num = mh->num;
  822.                         debug("decoding");
  823.  
  824.                         decode_the_frame(mh);
  825.  
  826.                         mh->to_decode = mh->to_ignore = FALSE;
  827.                         mh->buffer.p = mh->buffer.data;
  828.                         FRAME_BUFFERCHECK(mh);
  829.                         if(audio != NULL) *audio = mh->buffer.p;
  830.                         if(bytes != NULL) *bytes = mh->buffer.fill;
  831.  
  832.                         return MPG123_OK;
  833.                 }
  834.                 else
  835.                 {
  836.                         int b = get_next_frame(mh);
  837.                         if(b < 0) return b;
  838.                         debug1("got next frame, %i", mh->to_decode);
  839.                 }
  840.         }
  841. }
  842.  
  843. int attribute_align_arg mpg123_read(mpg123_handle *mh, unsigned char *out, size_t size, size_t *done)
  844. {
  845.         return mpg123_decode(mh, NULL, 0, out, size, done);
  846. }
  847.  
  848. int attribute_align_arg mpg123_feed(mpg123_handle *mh, const unsigned char *in, size_t size)
  849. {
  850.         if(mh == NULL) return MPG123_ERR;
  851. #ifndef NO_FEEDER
  852.         if(size > 0)
  853.         {
  854.                 if(in != NULL)
  855.                 {
  856.                         if(feed_more(mh, in, size) != 0) return MPG123_ERR;
  857.                         else
  858.                         {
  859.                                 /* The need for more data might have triggered an error.
  860.                                    This one is outdated now with the new data. */
  861.                                 if(mh->err == MPG123_ERR_READER) mh->err = MPG123_OK;
  862.  
  863.                                 return MPG123_OK;
  864.                         }
  865.                 }
  866.                 else
  867.                 {
  868.                         mh->err = MPG123_NULL_BUFFER;
  869.                         return MPG123_ERR;
  870.                 }
  871.         }
  872.         return MPG123_OK;
  873. #else
  874.         mh->err = MPG123_MISSING_FEATURE;
  875.         return MPG123_ERR;
  876. #endif
  877. }
  878.  
  879. /*
  880.         The old picture:
  881.         while(1) {
  882.                 len = read(0,buf,16384);
  883.                 if(len <= 0)
  884.                         break;
  885.                 ret = decodeMP3(&mp,buf,len,out,8192,&size);
  886.                 while(ret == MP3_OK) {
  887.                         write(1,out,size);
  888.                         ret = decodeMP3(&mp,NULL,0,out,8192,&size);
  889.                 }
  890.         }
  891. */
  892.  
  893. int attribute_align_arg mpg123_decode(mpg123_handle *mh, const unsigned char *inmemory, size_t inmemsize, unsigned char *outmemory, size_t outmemsize, size_t *done)
  894. {
  895.         int ret = MPG123_OK;
  896.         size_t mdone = 0;
  897.  
  898.         if(done != NULL) *done = 0;
  899.         if(mh == NULL) return MPG123_ERR;
  900. #ifndef NO_FEEDER
  901.         if(inmemsize > 0 && mpg123_feed(mh, inmemory, inmemsize) != MPG123_OK)
  902.         {
  903.                 ret = MPG123_ERR;
  904.                 goto decodeend;
  905.         }
  906.         if(outmemory == NULL) outmemsize = 0; /* Not just give error, give chance to get a status message. */
  907.  
  908.         while(ret == MPG123_OK)
  909.         {
  910.                 debug4("decode loop, fill %i (%li vs. %li); to_decode: %i", (int)mh->buffer.fill, (long)mh->num, (long)mh->firstframe, mh->to_decode);
  911.                 /* Decode a frame that has been read before.
  912.                    This only happens when buffer is empty! */
  913.                 if(mh->to_decode)
  914.                 {
  915.                         if(mh->new_format)
  916.                         {
  917.                                 debug("notifiying new format");
  918.                                 mh->new_format = 0;
  919.                                 ret = MPG123_NEW_FORMAT;
  920.                                 goto decodeend;
  921.                         }
  922.                         if(mh->buffer.size - mh->buffer.fill < mh->outblock)
  923.                         {
  924.                                 ret = MPG123_NO_SPACE;
  925.                                 goto decodeend;
  926.                         }
  927.                         decode_the_frame(mh);
  928.                         mh->to_decode = mh->to_ignore = FALSE;
  929.                         mh->buffer.p = mh->buffer.data;
  930.                         debug2("decoded frame %li, got %li samples in buffer", (long)mh->num, (long)(mh->buffer.fill / (samples_to_bytes(mh, 1))));
  931.                         FRAME_BUFFERCHECK(mh);
  932.                 }
  933.                 if(mh->buffer.fill) /* Copy (part of) the decoded data to the caller's buffer. */
  934.                 {
  935.                         /* get what is needed - or just what is there */
  936.                         int a = mh->buffer.fill > (outmemsize - mdone) ? outmemsize - mdone : mh->buffer.fill;
  937.                         debug4("buffer fill: %i; copying %i (%i - %li)", (int)mh->buffer.fill, a, (int)outmemsize, (long)mdone);
  938.                         memcpy(outmemory, mh->buffer.p, a);
  939.                         /* less data in frame buffer, less needed, output pointer increase, more data given... */
  940.                         mh->buffer.fill -= a;
  941.                         outmemory  += a;
  942.                         mdone += a;
  943.                         mh->buffer.p += a;
  944.                         if(!(outmemsize > mdone)) goto decodeend;
  945.                 }
  946.                 else /* If we didn't have data, get a new frame. */
  947.                 {
  948.                         int b = get_next_frame(mh);
  949.                         if(b < 0){ ret = b; goto decodeend; }
  950.                 }
  951.         }
  952. decodeend:
  953.         if(done != NULL) *done = mdone;
  954.         return ret;
  955. #else
  956.         mh->err = MPG123_MISSING_FEATURE;
  957.         return MPG123_ERR;
  958. #endif
  959. }
  960.  
  961. long attribute_align_arg mpg123_clip(mpg123_handle *mh)
  962. {
  963.         long ret = 0;
  964.  
  965.         if(mh != NULL)
  966.         {
  967.                 ret = mh->clip;
  968.                 mh->clip = 0;
  969.         }
  970.         return ret;
  971. }
  972.  
  973. /* Simples: Track needs initializtion if no initial frame has been read yet. */
  974. #define track_need_init(mh) ((mh)->num < 0)
  975.  
  976. static int init_track(mpg123_handle *mh)
  977. {
  978.         if(track_need_init(mh))
  979.         {
  980.                 /* Fresh track, need first frame for basic info. */
  981.                 int b = get_next_frame(mh);
  982.                 if(b < 0) return b;
  983.         }
  984.         return 0;
  985. }
  986.  
  987. int attribute_align_arg mpg123_getformat(mpg123_handle *mh, long *rate, int *channels, int *encoding)
  988. {
  989.         int b;
  990.  
  991.         if(mh == NULL) return MPG123_ERR;
  992.         b = init_track(mh);
  993.         if(b < 0) return b;
  994.  
  995.         if(rate != NULL) *rate = mh->af.rate;
  996.         if(channels != NULL) *channels = mh->af.channels;
  997.         if(encoding != NULL) *encoding = mh->af.encoding;
  998.         mh->new_format = 0;
  999.         return MPG123_OK;
  1000. }
  1001.  
  1002. off_t attribute_align_arg mpg123_timeframe(mpg123_handle *mh, double seconds)
  1003. {
  1004.         off_t b;
  1005.  
  1006.         if(mh == NULL) return MPG123_ERR;
  1007.         b = init_track(mh);
  1008.         if(b<0) return b;
  1009.         return (off_t)(seconds/mpg123_tpf(mh));
  1010. }
  1011.  
  1012. /*
  1013.         Now, where are we? We need to know the last decoded frame... and what's left of it in buffer.
  1014.         The current frame number can mean the last decoded frame or the to-be-decoded frame.
  1015.         If mh->to_decode, then mh->num frames have been decoded, the frame mh->num now coming next.
  1016.         If not, we have the possibility of mh->num+1 frames being decoded or nothing at all.
  1017.         Then, there is firstframe...when we didn't reach it yet, then the next data will come from there.
  1018.         mh->num starts with -1
  1019. */
  1020. off_t attribute_align_arg mpg123_tell(mpg123_handle *mh)
  1021. {
  1022.         if(mh == NULL) return MPG123_ERR;
  1023.         if(track_need_init(mh)) return 0;
  1024.         /* Now we have all the info at hand. */
  1025.         debug5("tell: %li/%i first %li buffer %lu; frame_outs=%li", (long)mh->num, mh->to_decode, (long)mh->firstframe, (unsigned long)mh->buffer.fill, (long)frame_outs(mh, mh->num));
  1026.  
  1027.         { /* Funny block to keep C89 happy. */
  1028.                 off_t pos = 0;
  1029.                 if((mh->num < mh->firstframe) || (mh->num == mh->firstframe && mh->to_decode))
  1030.                 { /* We are at the beginning, expect output from firstframe on. */
  1031.                         pos = frame_outs(mh, mh->firstframe);
  1032. #ifdef GAPLESS
  1033.                         pos += mh->firstoff;
  1034. #endif
  1035.                 }
  1036.                 else if(mh->to_decode)
  1037.                 { /* We start fresh with this frame. Buffer should be empty, but we make sure to count it in.  */
  1038.                         pos = frame_outs(mh, mh->num) - bytes_to_samples(mh, mh->buffer.fill);
  1039.                 }
  1040.                 else
  1041.                 { /* We serve what we have in buffer and then the beginning of next frame... */
  1042.                         pos = frame_outs(mh, mh->num+1) - bytes_to_samples(mh, mh->buffer.fill);
  1043.                 }
  1044.                 /* Substract padding and delay from the beginning. */
  1045.                 pos = SAMPLE_ADJUST(mh,pos);
  1046.                 /* Negative sample offsets are not right, less than nothing is still nothing. */
  1047.                 return pos>0 ? pos : 0;
  1048.         }
  1049. }
  1050.  
  1051. off_t attribute_align_arg mpg123_tellframe(mpg123_handle *mh)
  1052. {
  1053.         if(mh == NULL) return MPG123_ERR;
  1054.         if(mh->num < mh->firstframe) return mh->firstframe;
  1055.         if(mh->to_decode) return mh->num;
  1056.         /* Consider firstoff? */
  1057.         return mh->buffer.fill ? mh->num : mh->num + 1;
  1058. }
  1059.  
  1060. off_t attribute_align_arg mpg123_tell_stream(mpg123_handle *mh)
  1061. {
  1062.         if(mh == NULL) return MPG123_ERR;
  1063.         /* mh->rd is at least a bad_reader, so no worry. */
  1064.         return mh->rd->tell(mh);
  1065. }
  1066.  
  1067. static int do_the_seek(mpg123_handle *mh)
  1068. {
  1069.         int b;
  1070.         off_t fnum = SEEKFRAME(mh);
  1071.         mh->buffer.fill = 0;
  1072.  
  1073.         /* If we are inside the ignoreframe - firstframe window, we may get away without actual seeking. */
  1074.         if(mh->num < mh->firstframe)
  1075.         {
  1076.                 mh->to_decode = FALSE; /* In any case, don't decode the current frame, perhaps ignore instead. */
  1077.                 if(mh->num > fnum) return MPG123_OK;
  1078.         }
  1079.  
  1080.         /* If we are already there, we are fine either for decoding or for ignoring. */
  1081.         if(mh->num == fnum && (mh->to_decode || fnum < mh->firstframe)) return MPG123_OK;
  1082.         /* We have the frame before... just go ahead as normal. */
  1083.         if(mh->num == fnum-1)
  1084.         {
  1085.                 mh->to_decode = FALSE;
  1086.                 return MPG123_OK;
  1087.         }
  1088.  
  1089.         /* OK, real seeking follows... clear buffers and go for it. */
  1090.         frame_buffers_reset(mh);
  1091. #ifndef NO_NTOM
  1092.         if(mh->down_sample == 3)
  1093.         {
  1094.                 ntom_set_ntom(mh, fnum);
  1095.                 debug3("fixed ntom for frame %"OFF_P" to %lu, num=%"OFF_P, (off_p)fnum, mh->ntom_val[0], (off_p)mh->num);
  1096.         }
  1097. #endif
  1098.         b = mh->rd->seek_frame(mh, fnum);
  1099.         debug1("seek_frame returned: %i", b);
  1100.         if(b<0) return b;
  1101.         /* Only mh->to_ignore is TRUE. */
  1102.         if(mh->num < mh->firstframe) mh->to_decode = FALSE;
  1103.  
  1104.         mh->playnum = mh->num;
  1105.         return 0;
  1106. }
  1107.  
  1108. off_t attribute_align_arg mpg123_seek(mpg123_handle *mh, off_t sampleoff, int whence)
  1109. {
  1110.         int b;
  1111.         off_t pos;
  1112.  
  1113.         pos = mpg123_tell(mh); /* adjusted samples */
  1114.         /* pos < 0 also can mean that simply a former seek failed at the lower levels.
  1115.           In that case, we only allow absolute seeks. */
  1116.         if(pos < 0 && whence != SEEK_SET)
  1117.         { /* Unless we got the obvious error of NULL handle, this is a special seek failure. */
  1118.                 if(mh != NULL) mh->err = MPG123_NO_RELSEEK;
  1119.                 return MPG123_ERR;
  1120.         }
  1121.         if((b=init_track(mh)) < 0) return b;
  1122.         switch(whence)
  1123.         {
  1124.                 case SEEK_CUR: pos += sampleoff; break;
  1125.                 case SEEK_SET: pos  = sampleoff; break;
  1126.                 case SEEK_END:
  1127.                         /* When we do not know the end already, we can try to find it. */
  1128.                         if(mh->track_frames < 1 && (mh->rdat.flags & READER_SEEKABLE))
  1129.                         mpg123_scan(mh);
  1130.                         if(mh->track_frames > 0) pos = SAMPLE_ADJUST(mh,frame_outs(mh, mh->track_frames)) - sampleoff;
  1131. #ifdef GAPLESS
  1132.                         else if(mh->end_os > 0) pos = SAMPLE_ADJUST(mh,mh->end_os) - sampleoff;
  1133. #endif
  1134.                         else
  1135.                         {
  1136.                                 mh->err = MPG123_NO_SEEK_FROM_END;
  1137.                                 return MPG123_ERR;
  1138.                         }
  1139.                 break;
  1140.                 default: mh->err = MPG123_BAD_WHENCE; return MPG123_ERR;
  1141.         }
  1142.         if(pos < 0) pos = 0;
  1143.         /* pos now holds the wanted sample offset in adjusted samples */
  1144.         frame_set_seek(mh, SAMPLE_UNADJUST(mh,pos));
  1145.         pos = do_the_seek(mh);
  1146.         if(pos < 0) return pos;
  1147.  
  1148.         return mpg123_tell(mh);
  1149. }
  1150.  
  1151. /*
  1152.         A bit more tricky... libmpg123 does not do the seeking itself.
  1153.         All it can do is to ignore frames until the wanted one is there.
  1154.         The caller doesn't know where a specific frame starts and mpg123 also only knows the general region after it scanned the file.
  1155.         Well, it is tricky...
  1156. */
  1157. off_t attribute_align_arg mpg123_feedseek(mpg123_handle *mh, off_t sampleoff, int whence, off_t *input_offset)
  1158. {
  1159.         int b;
  1160.         off_t pos;
  1161.  
  1162.         pos = mpg123_tell(mh); /* adjusted samples */
  1163.         debug3("seek from %li to %li (whence=%i)", (long)pos, (long)sampleoff, whence);
  1164.         /* The special seek error handling does not apply here... there is no lowlevel I/O. */
  1165.         if(pos < 0) return pos; /* mh == NULL is covered in mpg123_tell() */
  1166. #ifndef NO_FEEDER
  1167.         if(input_offset == NULL)
  1168.         {
  1169.                 mh->err = MPG123_NULL_POINTER;
  1170.                 return MPG123_ERR;
  1171.         }
  1172.  
  1173.         if((b=init_track(mh)) < 0) return b; /* May need more to do anything at all. */
  1174.  
  1175.         switch(whence)
  1176.         {
  1177.                 case SEEK_CUR: pos += sampleoff; break;
  1178.                 case SEEK_SET: pos  = sampleoff; break;
  1179.                 case SEEK_END:
  1180.                         if(mh->track_frames > 0) pos = SAMPLE_ADJUST(mh,frame_outs(mh, mh->track_frames)) - sampleoff;
  1181. #ifdef GAPLESS
  1182.                         else if(mh->end_os >= 0) pos = SAMPLE_ADJUST(mh,mh->end_os) - sampleoff;
  1183. #endif
  1184.                         else
  1185.                         {
  1186.                                 mh->err = MPG123_NO_SEEK_FROM_END;
  1187.                                 return MPG123_ERR;
  1188.                         }
  1189.                 break;
  1190.                 default: mh->err = MPG123_BAD_WHENCE; return MPG123_ERR;
  1191.         }
  1192.         if(pos < 0) pos = 0;
  1193.         frame_set_seek(mh, SAMPLE_UNADJUST(mh,pos));
  1194.         pos = SEEKFRAME(mh);
  1195.         mh->buffer.fill = 0;
  1196.  
  1197.         /* Shortcuts without modifying input stream. */
  1198.         *input_offset = mh->rdat.buffer.fileoff + mh->rdat.buffer.size;
  1199.         if(mh->num < mh->firstframe) mh->to_decode = FALSE;
  1200.         if(mh->num == pos && mh->to_decode) goto feedseekend;
  1201.         if(mh->num == pos-1) goto feedseekend;
  1202.         /* Whole way. */
  1203.         *input_offset = feed_set_pos(mh, frame_index_find(mh, SEEKFRAME(mh), &pos));
  1204.         mh->num = pos-1; /* The next read frame will have num = pos. */
  1205.         if(*input_offset < 0) return MPG123_ERR;
  1206.  
  1207. feedseekend:
  1208.         return mpg123_tell(mh);
  1209. #else
  1210.         mh->err = MPG123_MISSING_FEATURE;
  1211.         return MPG123_ERR;
  1212. #endif
  1213. }
  1214.  
  1215. off_t attribute_align_arg mpg123_seek_frame(mpg123_handle *mh, off_t offset, int whence)
  1216. {
  1217.         int b;
  1218.         off_t pos = 0;
  1219.  
  1220.         if(mh == NULL) return MPG123_ERR;
  1221.         if((b=init_track(mh)) < 0) return b;
  1222.  
  1223.         /* Could play games here with to_decode... */
  1224.         pos = mh->num;
  1225.         switch(whence)
  1226.         {
  1227.                 case SEEK_CUR: pos += offset; break;
  1228.                 case SEEK_SET: pos  = offset; break;
  1229.                 case SEEK_END:
  1230.                         if(mh->track_frames > 0) pos = mh->track_frames - offset;
  1231.                         else
  1232.                         {
  1233.                                 mh->err = MPG123_NO_SEEK_FROM_END;
  1234.                                 return MPG123_ERR;
  1235.                         }
  1236.                 break;
  1237.                 default:
  1238.                         mh->err = MPG123_BAD_WHENCE;
  1239.                         return MPG123_ERR;
  1240.         }
  1241.         if(pos < 0) pos = 0;
  1242.         /* Not limiting the possible position on end for the chance that there might be more to the stream than announced via track_frames. */
  1243.  
  1244.         frame_set_frameseek(mh, pos);
  1245.         pos = do_the_seek(mh);
  1246.         if(pos < 0) return pos;
  1247.  
  1248.         return mpg123_tellframe(mh);
  1249. }
  1250.  
  1251. int attribute_align_arg mpg123_set_filesize(mpg123_handle *mh, off_t size)
  1252. {
  1253.         if(mh == NULL) return MPG123_ERR;
  1254.  
  1255.         mh->rdat.filelen = size;
  1256.         return MPG123_OK;
  1257. }
  1258.  
  1259. off_t attribute_align_arg mpg123_length(mpg123_handle *mh)
  1260. {
  1261.         int b;
  1262.         off_t length;
  1263.  
  1264.         if(mh == NULL) return MPG123_ERR;
  1265.         b = init_track(mh);
  1266.         if(b<0) return b;
  1267.         if(mh->track_samples > -1) length = mh->track_samples;
  1268.         else if(mh->track_frames > 0) length = mh->track_frames*spf(mh);
  1269.         else if(mh->rdat.filelen > 0) /* Let the case of 0 length just fall through. */
  1270.         {
  1271.                 /* A bad estimate. Ignoring tags 'n stuff. */
  1272.                 double bpf = mh->mean_framesize ? mh->mean_framesize : compute_bpf(mh);
  1273.                 length = (off_t)((double)(mh->rdat.filelen)/bpf*spf(mh));
  1274.         }
  1275.         else if(mh->rdat.filelen == 0) return mpg123_tell(mh); /* we could be in feeder mode */
  1276.         else return MPG123_ERR; /* No length info there! */
  1277.  
  1278.         debug1("mpg123_length: internal sample length: %"OFF_P, (off_p)length);
  1279.  
  1280.         length = frame_ins2outs(mh, length);
  1281.         debug1("mpg123_length: external sample length: %"OFF_P, (off_p)length);
  1282.         length = SAMPLE_ADJUST(mh,length);
  1283.         return length;
  1284. }
  1285.  
  1286. int attribute_align_arg mpg123_scan(mpg123_handle *mh)
  1287. {
  1288.         int b;
  1289.         off_t oldpos;
  1290.         off_t track_frames = 0;
  1291.         off_t track_samples = 0;
  1292.  
  1293.         if(mh == NULL) return MPG123_ERR;
  1294.         if(!(mh->rdat.flags & READER_SEEKABLE)){ mh->err = MPG123_NO_SEEK; return MPG123_ERR; }
  1295.         /* Scan through the _whole_ file, since the current position is no count but computed assuming constant samples per frame. */
  1296.         /* Also, we can just keep the current buffer and seek settings. Just operate on input frames here. */
  1297.         debug("issuing scan");
  1298.         b = init_track(mh); /* mh->num >= 0 !! */
  1299.         if(b<0)
  1300.         {
  1301.                 if(b == MPG123_DONE) return MPG123_OK;
  1302.                 else return MPG123_ERR; /* Must be error here, NEED_MORE is not for seekable streams. */
  1303.         }
  1304.         oldpos = mpg123_tell(mh);
  1305.         b = mh->rd->seek_frame(mh, 0);
  1306.         if(b<0 || mh->num != 0) return MPG123_ERR;
  1307.         /* One frame must be there now. */
  1308.         track_frames = 1;
  1309.         track_samples = spf(mh); /* Internal samples. */
  1310.         debug("TODO: We should disable gapless code when encountering inconsistent spf(mh)!");
  1311.         /* Do not increment mh->track_frames in the loop as tha would confuse Frankenstein detection. */
  1312.         while(read_frame(mh) == 1)
  1313.         {
  1314.                 ++track_frames;
  1315.                 track_samples += spf(mh);
  1316.         }
  1317.         mh->track_frames = track_frames;
  1318.         mh->track_samples = track_samples;
  1319.         mpg123_seek_frame(mh, SEEK_SET, mh->track_frames);
  1320.         debug2("Scanning yielded %"OFF_P" track samples, %"OFF_P" frames.", (off_p)mh->track_samples, (off_p)mh->track_frames);
  1321. #ifdef GAPLESS
  1322.         /* Also, think about usefulness of that extra value track_samples ... it could be used for consistency checking. */
  1323.         frame_gapless_update(mh, mh->track_samples);
  1324. #endif
  1325.         return mpg123_seek(mh, oldpos, SEEK_SET) >= 0 ? MPG123_OK : MPG123_ERR;
  1326. }
  1327.  
  1328. int attribute_align_arg mpg123_meta_check(mpg123_handle *mh)
  1329. {
  1330.         if(mh != NULL) return mh->metaflags;
  1331.         else return 0;
  1332. }
  1333.  
  1334. void attribute_align_arg mpg123_meta_free(mpg123_handle *mh)
  1335. {
  1336.         if(mh == NULL) return;
  1337.  
  1338.         reset_id3(mh);
  1339.         reset_icy(&mh->icy);
  1340. }
  1341.  
  1342. int attribute_align_arg mpg123_id3(mpg123_handle *mh, mpg123_id3v1 **v1, mpg123_id3v2 **v2)
  1343. {
  1344.         if(v1 != NULL) *v1 = NULL;
  1345.         if(v2 != NULL) *v2 = NULL;
  1346.         if(mh == NULL) return MPG123_ERR;
  1347.  
  1348.         if(mh->metaflags & MPG123_ID3)
  1349.         {
  1350.                 id3_link(mh);
  1351.                 if(v1 != NULL && mh->rdat.flags & READER_ID3TAG) *v1 = (mpg123_id3v1*) mh->id3buf;
  1352.                 if(v2 != NULL)
  1353. #ifdef NO_ID3V2
  1354.                 *v2 = NULL;
  1355. #else
  1356.                 *v2 = &mh->id3v2;
  1357. #endif
  1358.  
  1359.                 mh->metaflags |= MPG123_ID3;
  1360.                 mh->metaflags &= ~MPG123_NEW_ID3;
  1361.         }
  1362.         return MPG123_OK;
  1363. }
  1364.  
  1365. int attribute_align_arg mpg123_icy(mpg123_handle *mh, char **icy_meta)
  1366. {
  1367.         if(mh == NULL) return MPG123_ERR;
  1368. #ifndef NO_ICY
  1369.         if(icy_meta == NULL)
  1370.         {
  1371.                 mh->err = MPG123_NULL_POINTER;
  1372.                 return MPG123_ERR;
  1373.         }
  1374.         *icy_meta = NULL;
  1375.  
  1376.         if(mh->metaflags & MPG123_ICY)
  1377.         {
  1378.                 *icy_meta = mh->icy.data;
  1379.                 mh->metaflags |= MPG123_ICY;
  1380.                 mh->metaflags &= ~MPG123_NEW_ICY;
  1381.         }
  1382.         return MPG123_OK;
  1383. #else
  1384.         mh->err = MPG123_MISSING_FEATURE;
  1385.         return MPG123_ERR;
  1386. #endif
  1387. }
  1388.  
  1389. char* attribute_align_arg mpg123_icy2utf8(const char* icy_text)
  1390. {
  1391. #ifndef NO_ICY
  1392.         return icy2utf8(icy_text, 0);
  1393. #else
  1394.         return NULL;
  1395. #endif
  1396. }
  1397.  
  1398. /* That one is always defined... it's not worth it to remove it for NO_ID3V2. */
  1399. enum mpg123_text_encoding attribute_align_arg mpg123_enc_from_id3(unsigned char id3_enc_byte)
  1400. {
  1401.         switch(id3_enc_byte)
  1402.         {
  1403.                 case mpg123_id3_latin1:   return mpg123_text_latin1;
  1404.                 case mpg123_id3_utf16bom: return mpg123_text_utf16bom; /* ID3v2.3 has UCS-2 with BOM here. */
  1405.                 case mpg123_id3_utf16be:  return mpg123_text_utf16be;
  1406.                 case mpg123_id3_utf8:     return mpg123_text_utf8;
  1407.                 default: return mpg123_text_unknown;
  1408.         }
  1409. }
  1410.  
  1411. #ifndef NO_STRING
  1412. int mpg123_store_utf8(mpg123_string *sb, enum mpg123_text_encoding enc, const unsigned char *source, size_t source_size)
  1413. {
  1414.         switch(enc)
  1415.         {
  1416. #ifndef NO_ID3V2
  1417.                 /* The encodings we get from ID3v2 tags. */
  1418.                 case mpg123_text_utf8:
  1419.                         id3_to_utf8(sb, mpg123_id3_utf8, source, source_size, 0);
  1420.                 break;
  1421.                 case mpg123_text_latin1:
  1422.                         id3_to_utf8(sb, mpg123_id3_latin1, source, source_size, 0);
  1423.                 break;
  1424.                 case mpg123_text_utf16bom:
  1425.                 case mpg123_text_utf16:
  1426.                         id3_to_utf8(sb, mpg123_id3_utf16bom, source, source_size, 0);
  1427.                 break;
  1428.                 /* Special because one cannot skip zero bytes here. */
  1429.                 case mpg123_text_utf16be:
  1430.                         id3_to_utf8(sb, mpg123_id3_utf16be, source, source_size, 0);
  1431.                 break;
  1432. #endif
  1433. #ifndef NO_ICY
  1434.                 /* ICY encoding... */
  1435.                 case mpg123_text_icy:
  1436.                 case mpg123_text_cp1252:
  1437.                 {
  1438.                         mpg123_free_string(sb);
  1439.                         /* Paranoia: Make sure that the string ends inside the buffer... */
  1440.                         if(source[source_size-1] == 0)
  1441.                         {
  1442.                                 /* Convert from ICY encoding... with force applied or not. */
  1443.                                 char *tmpstring = icy2utf8((const char*)source, enc == mpg123_text_cp1252 ? 1 : 0);
  1444.                                 if(tmpstring != NULL)
  1445.                                 {
  1446.                                         mpg123_set_string(sb, tmpstring);
  1447.                                         free(tmpstring);
  1448.                                 }
  1449.                         }
  1450.                 }
  1451.                 break;
  1452. #endif
  1453.                 default:
  1454.                         mpg123_free_string(sb);
  1455.         }
  1456.         /* At least a trailing null of some form should be there... */
  1457.         return (sb->fill > 0) ? 1 : 0;
  1458. }
  1459. #endif
  1460.  
  1461. int attribute_align_arg mpg123_index(mpg123_handle *mh, off_t **offsets, off_t *step, size_t *fill)
  1462. {
  1463.         if(mh == NULL) return MPG123_ERR;
  1464.         if(offsets == NULL || step == NULL || fill == NULL)
  1465.         {
  1466.                 mh->err = MPG123_BAD_INDEX_PAR;
  1467.                 return MPG123_ERR;
  1468.         }
  1469. #ifdef FRAME_INDEX
  1470.         *offsets = mh->index.data;
  1471.         *step    = mh->index.step;
  1472.         *fill    = mh->index.fill;
  1473. #else
  1474.         *offsets = NULL;
  1475.         *step    = 0;
  1476.         *fill    = 0;
  1477. #endif
  1478.         return MPG123_OK;
  1479. }
  1480.  
  1481. int attribute_align_arg mpg123_set_index(mpg123_handle *mh, off_t *offsets, off_t step, size_t fill)
  1482. {
  1483.         if(mh == NULL) return MPG123_ERR;
  1484. #ifdef FRAME_INDEX
  1485.         if(step == 0)
  1486.         {
  1487.                 mh->err = MPG123_BAD_INDEX_PAR;
  1488.                 return MPG123_ERR;
  1489.         }
  1490.         if(fi_set(&mh->index, offsets, step, fill) == -1)
  1491.         {
  1492.                 mh->err = MPG123_OUT_OF_MEM;
  1493.                 return MPG123_ERR;
  1494.         }
  1495.         return MPG123_OK;
  1496. #else
  1497.         mh->err = MPG123_MISSING_FEATURE;
  1498.         return MPG123_ERR;
  1499. #endif
  1500. }
  1501.  
  1502. int attribute_align_arg mpg123_close(mpg123_handle *mh)
  1503. {
  1504.         if(mh == NULL) return MPG123_ERR;
  1505.  
  1506.         /* mh->rd is never NULL! */
  1507.         if(mh->rd->close != NULL) mh->rd->close(mh);
  1508.  
  1509.         if(mh->new_format)
  1510.         {
  1511.                 debug("Hey, we are closing a track before the new format has been queried...");
  1512.                 invalidate_format(&mh->af);
  1513.                 mh->new_format = 0;
  1514.         }
  1515.         /* Always reset the frame buffers on close, so we cannot forget it in funky opening routines (wrappers, even). */
  1516.         frame_reset(mh);
  1517.         return MPG123_OK;
  1518. }
  1519.  
  1520. void attribute_align_arg mpg123_delete(mpg123_handle *mh)
  1521. {
  1522.         if(mh != NULL)
  1523.         {
  1524.                 mpg123_close(mh);
  1525.                 frame_exit(mh); /* free buffers in frame */
  1526.                 free(mh); /* free struct; cast? */
  1527.         }
  1528. }
  1529.  
  1530. static const char *mpg123_error[] =
  1531. {
  1532.         "No error... (code 0)",
  1533.         "Unable to set up output format! (code 1)",
  1534.         "Invalid channel number specified. (code 2)",
  1535.         "Invalid sample rate specified. (code 3)",
  1536.         "Unable to allocate memory for 16 to 8 converter table! (code 4)",
  1537.         "Bad parameter id! (code 5)",
  1538.         "Bad buffer given -- invalid pointer or too small size. (code 6)",
  1539.         "Out of memory -- some malloc() failed. (code 7)",
  1540.         "You didn't initialize the library! (code 8)",
  1541.         "Invalid decoder choice. (code 9)",
  1542.         "Invalid mpg123 handle. (code 10)",
  1543.         "Unable to initialize frame buffers (out of memory?)! (code 11)",
  1544.         "Invalid RVA mode. (code 12)",
  1545.         "This build doesn't support gapless decoding. (code 13)",
  1546.         "Not enough buffer space. (code 14)",
  1547.         "Incompatible numeric data types. (code 15)",
  1548.         "Bad equalizer band. (code 16)",
  1549.         "Null pointer given where valid storage address needed. (code 17)",
  1550.         "Error reading the stream. (code 18)",
  1551.         "Cannot seek from end (end is not known). (code 19)",
  1552.         "Invalid 'whence' for seek function. (code 20)",
  1553.         "Build does not support stream timeouts. (code 21)",
  1554.         "File access error. (code 22)",
  1555.         "Seek not supported by stream. (code 23)",
  1556.         "No stream opened. (code 24)",
  1557.         "Bad parameter handle. (code 25)",
  1558.         "Invalid parameter addresses for index retrieval. (code 26)",
  1559.         "Lost track in the bytestream and did not attempt resync. (code 27)",
  1560.         "Failed to find valid MPEG data within limit on resync. (code 28)",
  1561.         "No 8bit encoding possible. (code 29)",
  1562.         "Stack alignment is not good. (code 30)",
  1563.         "You gave me a NULL buffer? (code 31)",
  1564.         "File position is screwed up, please do an absolute seek (code 32)",
  1565.         "Inappropriate NULL-pointer provided.",
  1566.         "Bad key value given.",
  1567.         "There is no frame index (disabled in this build).",
  1568.         "Frame index operation failed.",
  1569.         "Decoder setup failed (invalid combination of settings?)",
  1570.         "Feature not in this build."
  1571.         ,"Some bad value has been provided."
  1572.         ,"Low-level seeking has failed (call to lseek(), usually)."
  1573.         ,"Custom I/O obviously not prepared."
  1574.         ,"Overflow in LFS (large file support) conversion."
  1575.         ,"Overflow in integer conversion."
  1576. };
  1577.  
  1578. const char* attribute_align_arg mpg123_plain_strerror(int errcode)
  1579. {
  1580.         if(errcode >= 0 && errcode < sizeof(mpg123_error)/sizeof(char*))
  1581.         return mpg123_error[errcode];
  1582.         else switch(errcode)
  1583.         {
  1584.                 case MPG123_ERR:
  1585.                         return "A generic mpg123 error.";
  1586.                 case MPG123_DONE:
  1587.                         return "Message: I am done with this track.";
  1588.                 case MPG123_NEED_MORE:
  1589.                         return "Message: Feed me more input data!";
  1590.                 case MPG123_NEW_FORMAT:
  1591.                         return "Message: Prepare for a changed audio format (query the new one)!";
  1592.                 default:
  1593.                         return "I have no idea - an unknown error code!";
  1594.         }
  1595. }
  1596.  
  1597. int attribute_align_arg mpg123_errcode(mpg123_handle *mh)
  1598. {
  1599.         if(mh != NULL) return mh->err;
  1600.         return MPG123_BAD_HANDLE;
  1601. }
  1602.  
  1603. const char* attribute_align_arg mpg123_strerror(mpg123_handle *mh)
  1604. {
  1605.         return mpg123_plain_strerror(mpg123_errcode(mh));
  1606. }
  1607.