ReactOS  r75214
mpg123 position and seeking

Functions

MPG123_EXPORT off_t mpg123_tell (mpg123_handle *mh)
 
MPG123_EXPORT off_t mpg123_tellframe (mpg123_handle *mh)
 
MPG123_EXPORT off_t mpg123_tell_stream (mpg123_handle *mh)
 
MPG123_EXPORT off_t mpg123_seek (mpg123_handle *mh, off_t sampleoff, int whence)
 
MPG123_EXPORT off_t mpg123_feedseek (mpg123_handle *mh, off_t sampleoff, int whence, off_t *input_offset)
 
MPG123_EXPORT off_t mpg123_seek_frame (mpg123_handle *mh, off_t frameoff, int whence)
 
MPG123_EXPORT off_t mpg123_timeframe (mpg123_handle *mh, double sec)
 
MPG123_EXPORT int mpg123_index (mpg123_handle *mh, off_t **offsets, off_t *step, size_t *fill)
 
MPG123_EXPORT int mpg123_set_index (mpg123_handle *mh, off_t *offsets, off_t step, size_t fill)
 
MPG123_EXPORT int mpg123_position (mpg123_handle *mh, off_t frame_offset, off_t buffered_bytes, off_t *current_frame, off_t *frames_left, double *current_seconds, double *seconds_left)
 

Detailed Description

Functions querying and manipulating position in the decoded audio bitstream. The position is measured in decoded audio samples, or MPEG frame offset for the specific functions. If gapless code is in effect, the positions are adjusted to compensate the skipped padding/delay - meaning, you should not care about that at all and just use the position defined for the samples you get out of the decoder;-) The general usage is modelled after stdlib's ftell() and fseek(). Especially, the whence parameter for the seek functions has the same meaning as the one for fseek() and needs the same constants from stdlib.h:

Note that sample-accurate seek only works when gapless support has been enabled at compile time; seek is frame-accurate otherwise. Also, really sample-accurate seeking (meaning that you get the identical sample value after seeking compared to plain decoding up to the position) is only guaranteed when you do not mess with the position code by using MPG123_UPSPEED, MPG123_DOWNSPEED or MPG123_START_FRAME. The first two mainly should cause trouble with NtoM resampling, but in any case with these options in effect, you have to keep in mind that the sample offset is not the same as counting the samples you get from decoding since mpg123 counts the skipped samples, too (or the samples played twice only once)! Short: When you care about the sample position, don't mess with those parameters;-) Also, seeking is not guaranteed to work for all streams (underlying stream may not support it). And yet another caveat: If the stream is concatenated out of differing pieces (Frankenstein stream), seeking may suffer, too.

Function Documentation

MPG123_EXPORT off_t mpg123_feedseek ( mpg123_handle mh,
off_t  sampleoff,
int  whence,
off_t input_offset 
)

Seek to a desired sample offset in data feeding mode. This just prepares things to be right only if you ensure that the next chunk of input data will be from input_offset byte position.

Parameters
mhhandle
sampleoffoffset in PCM samples
whenceone of SEEK_SET, SEEK_CUR or SEEK_END
input_offsetThe position it expects to be at the next time data is fed to mpg123_decode().
Returns
The resulting offset >= 0 or error/message code

Definition at line 291 of file lfs_wrap.c.

292 {
293  long val;
294  off_t largeioff;
295  off_t largeval;
296 
297  largeval = MPG123_LARGENAME(mpg123_feedseek)(mh, sampleoff, whence, &largeioff);
298  /* Error/message codes are small... */
299  if(largeval < 0) return (long)largeval;
300 
301  val = largeval;
302  *input_offset = largeioff;
303  if(val != largeval || *input_offset != largeioff)
304  {
305  mh->err = MPG123_LFS_OVERFLOW;
306  return MPG123_ERR;
307  }
308  return val;
309 }
long attribute_align_arg mpg123_feedseek(mpg123_handle *mh, long sampleoff, int whence, long *input_offset)
Definition: lfs_wrap.c:291
__kernel_off_t off_t
Definition: linux.h:201
GLuint GLfloat * val
Definition: glext.h:7180
MPG123_EXPORT int mpg123_index ( mpg123_handle mh,
off_t **  offsets,
off_t step,
size_t fill 
)

Give access to the frame index table that is managed for seeking. You are asked not to modify the values... Use mpg123_set_index to set the seek index

Parameters
mhhandle
offsetspointer to the index array
stepone index byte offset advances this many MPEG frames
fillnumber of recorded index offsets; size of the array
Returns
MPG123_OK on success

Definition at line 349 of file lfs_wrap.c.

350 {
351  int err;
352  size_t i;
353  long smallstep;
354  size_t thefill;
355  off_t largestep;
356  off_t *largeoffsets;
357  struct wrap_data *whd;
358 
359  whd = wrap_get(mh);
360  if(whd == NULL) return MPG123_ERR;
361 
362  err = MPG123_LARGENAME(mpg123_index)(mh, &largeoffsets, &largestep, &thefill);
363  if(err != MPG123_OK) return err;
364 
365  /* For a _very_ large file, even the step could overflow. */
366  smallstep = largestep;
367  if(smallstep != largestep)
368  {
369  mh->err = MPG123_LFS_OVERFLOW;
370  return MPG123_ERR;
371  }
372  if(step != NULL) *step = smallstep;
373 
374  /* When there are no values stored, there is no table content to take care of.
375  Table pointer does not matter. Mission completed. */
376  if(thefill == 0) return MPG123_OK;
377 
378  if(fill != NULL) *fill = thefill;
379 
380  /* Construct a copy of the index to hand over to the small-minded client. */
381  *offsets = safe_realloc(whd->indextable, (*fill)*sizeof(long));
382  if(*offsets == NULL)
383  {
384  mh->err = MPG123_OUT_OF_MEM;
385  return MPG123_ERR;
386  }
387  whd->indextable = *offsets;
388  /* Elaborate conversion of each index value, with overflow check. */
389  for(i=0; i<*fill; ++i)
390  {
391  whd->indextable[i] = largeoffsets[i];
392  if(whd->indextable[i] != largeoffsets[i])
393  {
394  mh->err = MPG123_LFS_OVERFLOW;
395  return MPG123_ERR;
396  }
397  }
398  /* If we came that far... there should be a valid copy of the table now. */
399  return MPG123_OK;
400 }
__kernel_off_t off_t
Definition: linux.h:201
#define safe_realloc
Definition: intsym.h:10
GLenum GLclampf GLint i
Definition: glfuncs.h:14
smooth NULL
Definition: ftsmooth.c:513
_STLP_MOVE_TO_STD_NAMESPACE void fill(_ForwardIter __first, _ForwardIter __last, const _Tp &__val)
Definition: _algobase.h:449
#define err(...)
int attribute_align_arg mpg123_index(mpg123_handle *mh, long **offsets, long *step, size_t *fill)
Definition: lfs_wrap.c:349
long * indextable
Definition: lfs_wrap.c:78
static struct wrap_data * wrap_get(mpg123_handle *mh)
Definition: lfs_wrap.c:126
MPG123_EXPORT int mpg123_position ( mpg123_handle mh,
off_t  frame_offset,
off_t  buffered_bytes,
off_t current_frame,
off_t frames_left,
double current_seconds,
double seconds_left 
)

An old crutch to keep old mpg123 binaries happy. WARNING: This function is there only to avoid runtime linking errors with standalone mpg123 before version 1.23.0 (if you strangely update the library but not the end-user program) and actually is broken for various cases (p.ex. 24 bit output). Do never use. It might eventually be purged from the library.

Definition at line 445 of file lfs_wrap.c.

446 {
447  off_t curframe, frameleft;
448  long small_curframe, small_frameleft;
449  int err;
450 
451  err = MPG123_LARGENAME(mpg123_position)(mh, frame_offset, buffered_bytes, &curframe, &frameleft, current_seconds, seconds_left);
452  if(err != MPG123_OK) return err;
453 
454  small_curframe = curframe;
455  small_frameleft = frameleft;
456  if(small_curframe != curframe || small_frameleft != frameleft)
457  {
458  mh->err = MPG123_LFS_OVERFLOW;
459  return MPG123_ERR;
460  }
461 
462  if(current_frame != NULL) *current_frame = small_curframe;
463 
464  if(frames_left != NULL) *frames_left = small_frameleft;
465 
466 
467  return MPG123_OK;
468 }
#define frame_offset
Definition: intsym.h:190
__kernel_off_t off_t
Definition: linux.h:201
int attribute_align_arg mpg123_position(mpg123_handle *mh, long frame_offset, long buffered_bytes, long *current_frame, long *frames_left, double *current_seconds, double *seconds_left)
Definition: lfs_wrap.c:445
smooth NULL
Definition: ftsmooth.c:513
#define err(...)
MPG123_EXPORT off_t mpg123_seek ( mpg123_handle mh,
off_t  sampleoff,
int  whence 
)

Seek to a desired sample offset. Usage is modelled afer the standard lseek().

Parameters
mhhandle
sampleoffoffset in PCM samples
whenceone of SEEK_SET, SEEK_CUR or SEEK_END
Returns
The resulting offset >= 0 or error/message code

Definition at line 274 of file lfs_wrap.c.

Referenced by mpg123_scan().

275 {
276  long val;
277  off_t largeval;
278 
279  largeval = MPG123_LARGENAME(mpg123_seek)(mh, sampleoff, whence);
280  val = largeval;
281  if(val != largeval)
282  {
283  mh->err = MPG123_LFS_OVERFLOW;
284  return MPG123_ERR;
285  }
286  return val;
287 }
__kernel_off_t off_t
Definition: linux.h:201
GLuint GLfloat * val
Definition: glext.h:7180
long attribute_align_arg mpg123_seek(mpg123_handle *mh, long sampleoff, int whence)
Definition: lfs_wrap.c:274
MPG123_EXPORT off_t mpg123_seek_frame ( mpg123_handle mh,
off_t  frameoff,
int  whence 
)

Seek to a desired MPEG frame offset. Usage is modelled afer the standard lseek().

Parameters
mhhandle
frameoffoffset in MPEG frames
whenceone of SEEK_SET, SEEK_CUR or SEEK_END
Returns
The resulting offset >= 0 or error/message code

Definition at line 313 of file lfs_wrap.c.

314 {
315  long val;
316  off_t largeval;
317 
318  largeval = MPG123_LARGENAME(mpg123_seek_frame)(mh, frameoff, whence);
319  val = largeval;
320  if(val != largeval)
321  {
322  mh->err = MPG123_LFS_OVERFLOW;
323  return MPG123_ERR;
324  }
325  return val;
326 }
__kernel_off_t off_t
Definition: linux.h:201
GLuint GLfloat * val
Definition: glext.h:7180
long attribute_align_arg mpg123_seek_frame(mpg123_handle *mh, long frameoff, int whence)
Definition: lfs_wrap.c:313
MPG123_EXPORT int mpg123_set_index ( mpg123_handle mh,
off_t offsets,
off_t  step,
size_t  fill 
)

Set the frame index table Setting offsets to NULL and fill > 0 will allocate fill entries. Setting offsets to NULL and fill to 0 will clear the index and free the allocated memory used by the index.

Parameters
mhhandle
offsetspointer to the index array
stepone index byte offset advances this many MPEG frames
fillnumber of recorded index offsets; size of the array
Returns
MPG123_OK on success

Definition at line 406 of file lfs_wrap.c.

407 {
408  int err;
409  size_t i;
410  struct wrap_data *whd;
411  off_t *indextmp;
412 
413  whd = wrap_get(mh);
414  if(whd == NULL) return MPG123_ERR;
415 
416  /* Expensive temporary storage... for staying outside at the API layer. */
417  indextmp = malloc(fill*sizeof(off_t));
418  if(indextmp == NULL)
419  {
420  mh->err = MPG123_OUT_OF_MEM;
421  return MPG123_ERR;
422  }
423 
424  if(fill > 0 && offsets == NULL)
425  {
427  err = MPG123_ERR;
428  }
429  else
430  {
431  /* Fill the large-file copy of the provided index, then feed it to mpg123. */
432  for(i=0; i<fill; ++i)
433  indextmp[i] = offsets[i];
434 
435  err = MPG123_LARGENAME(mpg123_set_index)(mh, indextmp, step, fill);
436  }
437  free(indextmp);
438 
439  return err;
440 }
#define free
Definition: debug_ros.c:5
__kernel_off_t off_t
Definition: linux.h:201
GLenum GLclampf GLint i
Definition: glfuncs.h:14
smooth NULL
Definition: ftsmooth.c:513
_STLP_MOVE_TO_STD_NAMESPACE void fill(_ForwardIter __first, _ForwardIter __last, const _Tp &__val)
Definition: _algobase.h:449
int attribute_align_arg mpg123_set_index(mpg123_handle *mh, long *offsets, long step, size_t fill)
Definition: lfs_wrap.c:406
#define err(...)
#define malloc
Definition: debug_ros.c:4
static struct wrap_data * wrap_get(mpg123_handle *mh)
Definition: lfs_wrap.c:126
MPG123_EXPORT off_t mpg123_tell ( mpg123_handle mh)

Returns the current position in samples. On the next successful read, you'd get that sample.

Parameters
mhhandle
Returns
sample offset or MPG123_ERR (null handle)

Definition at line 140 of file lfs_alias.c.

Referenced by get_next_frame(), mpg123_feedseek(), mpg123_length(), mpg123_scan(), and mpg123_seek().

141 {
142  return NATIVE_NAME(mpg123_tell)(mh);
143 }
lfs_alias_t NATIVE_NAME() mpg123_tell(mpg123_handle *mh)
Definition: lfs_alias.c:140
#define NATIVE_NAME(func)
Definition: lfs_alias.c:52
MPG123_EXPORT off_t mpg123_tell_stream ( mpg123_handle mh)

Returns the current byte offset in the input stream.

Parameters
mhhandle
Returns
byte offset or MPG123_ERR (null handle)

Definition at line 152 of file lfs_alias.c.

153 {
154  return NATIVE_NAME(mpg123_tell_stream)(mh);
155 }
lfs_alias_t NATIVE_NAME() mpg123_tell_stream(mpg123_handle *mh)
Definition: lfs_alias.c:152
#define NATIVE_NAME(func)
Definition: lfs_alias.c:52
MPG123_EXPORT off_t mpg123_tellframe ( mpg123_handle mh)

Returns the frame number that the next read will give you data from.

Parameters
mhhandle
Returns
frame offset or MPG123_ERR (null handle)

Definition at line 146 of file lfs_alias.c.

Referenced by mpg123_seek_frame().

147 {
148  return NATIVE_NAME(mpg123_tellframe)(mh);
149 }
lfs_alias_t NATIVE_NAME() mpg123_tellframe(mpg123_handle *mh)
Definition: lfs_alias.c:146
#define NATIVE_NAME(func)
Definition: lfs_alias.c:52
MPG123_EXPORT off_t mpg123_timeframe ( mpg123_handle mh,
double  sec 
)

Return a MPEG frame offset corresponding to an offset in seconds. This assumes that the samples per frame do not change in the file/stream, which is a good assumption for any sane file/stream only.

Returns
frame offset >= 0 or error/message code

Definition at line 176 of file lfs_alias.c.

177 {
178  return NATIVE_NAME(mpg123_timeframe)(mh, sec);
179 }
lfs_alias_t NATIVE_NAME() mpg123_timeframe(mpg123_handle *mh, double sec)
Definition: lfs_alias.c:176
#define NATIVE_NAME(func)
Definition: lfs_alias.c:52