| 1 |
/* |
| 2 |
reader: reading input data |
| 3 |
|
| 4 |
copyright ?-2023 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 |
initially written by Thomas Orgis (after code from Michael Hipp) |
| 7 |
*/ |
| 8 |
|
| 9 |
#ifndef MPG123_READER_H |
| 10 |
#define MPG123_READER_H |
| 11 |
|
| 12 |
#ifndef MPG123_H_INTERN |
| 13 |
#error "include internal mpg123 header first" |
| 14 |
#endif |
| 15 |
|
| 16 |
#ifndef NO_FEEDER |
| 17 |
struct buffy |
| 18 |
{ |
| 19 |
unsigned char *data; |
| 20 |
ptrdiff_t size; |
| 21 |
ptrdiff_t realsize; |
| 22 |
struct buffy *next; |
| 23 |
}; |
| 24 |
|
| 25 |
|
| 26 |
struct bufferchain |
| 27 |
{ |
| 28 |
struct buffy* first; /* The beginning of the chain. */ |
| 29 |
struct buffy* last; /* The end... of the chain. */ |
| 30 |
ptrdiff_t size; /* Aggregated size of all buffies. */ |
| 31 |
/* These positions are relative to buffer chain beginning. */ |
| 32 |
ptrdiff_t pos; /* Position in whole chain. */ |
| 33 |
ptrdiff_t firstpos; /* The point of return on non-forget() */ |
| 34 |
/* The "real" filepos is fileoff + pos. */ |
| 35 |
int64_t fileoff; /* Beginning of chain is at this file offset. */ |
| 36 |
// Unsigned since no direct arithmetic with offsets. Overflow of overall |
| 37 |
// size needs to be checked anyway. |
| 38 |
size_t bufblock; /* Default (minimal) size of buffers. */ |
| 39 |
size_t pool_size; /* Keep that many buffers in storage. */ |
| 40 |
size_t pool_fill; /* That many buffers are there. */ |
| 41 |
/* A pool of buffers to re-use, if activated. It's a linked list that is worked on from the front. */ |
| 42 |
struct buffy *pool; |
| 43 |
}; |
| 44 |
|
| 45 |
/* Call this before any buffer chain use (even bc_init()). */ |
| 46 |
void INT123_bc_prepare(struct bufferchain *, size_t pool_size, size_t bufblock); |
| 47 |
/* Free persistent data in the buffer chain, after bc_reset(). */ |
| 48 |
void INT123_bc_cleanup(struct bufferchain *); |
| 49 |
/* Change pool size. This does not actually allocate/free anything on itself, just instructs later operations to free less / allocate more buffers. */ |
| 50 |
void INT123_bc_poolsize(struct bufferchain *, size_t pool_size, size_t bufblock); |
| 51 |
/* Return available byte count in the buffer. */ |
| 52 |
size_t INT123_bc_fill(struct bufferchain *bc); |
| 53 |
|
| 54 |
#endif |
| 55 |
|
| 56 |
struct reader_data |
| 57 |
{ |
| 58 |
int64_t filelen; /* total file length or total buffer size */ |
| 59 |
int64_t filepos; /* position in file or position in buffer chain */ |
| 60 |
/* Custom opaque I/O handle from the client. */ |
| 61 |
void *iohandle; |
| 62 |
int flags; |
| 63 |
// The one and only lowlevel reader wrapper, wrapping over all others. |
| 64 |
// This is either libmpg123's wrapper or directly the user-supplied functions. |
| 65 |
int (*r_read64) (void *, void *, size_t, size_t *); |
| 66 |
int64_t (*r_lseek64)(void *, int64_t, int); |
| 67 |
void (*cleanup_handle)(void *handle); |
| 68 |
#ifndef NO_FEEDER |
| 69 |
struct bufferchain buffer; /* Not dynamically allocated, these few struct bytes aren't worth the trouble. */ |
| 70 |
#endif |
| 71 |
}; |
| 72 |
|
| 73 |
/* start to use off_t to properly do LFS in future ... used to be long */ |
| 74 |
#ifdef __MORPHOS__ |
| 75 |
#undef tell /* unistd.h defines it as lseek(x, 0L, 1) */ |
| 76 |
#endif |
| 77 |
struct reader |
| 78 |
{ |
| 79 |
int (*init) (mpg123_handle *); |
| 80 |
void (*close) (mpg123_handle *); |
| 81 |
ptrdiff_t (*fullread) (mpg123_handle *, unsigned char *, ptrdiff_t); |
| 82 |
int (*head_read) (mpg123_handle *, unsigned long *newhead); /* succ: TRUE, else <= 0 (FALSE or READER_MORE) */ |
| 83 |
int (*head_shift) (mpg123_handle *, unsigned long *head); /* succ: TRUE, else <= 0 (FALSE or READER_MORE) */ |
| 84 |
int64_t (*skip_bytes) (mpg123_handle *, int64_t len); /* succ: >=0, else error or READER_MORE */ |
| 85 |
int (*read_frame_body)(mpg123_handle *, unsigned char *, int size); |
| 86 |
int (*back_bytes) (mpg123_handle *, int64_t bytes); |
| 87 |
int (*seek_frame) (mpg123_handle *, int64_t num); |
| 88 |
int64_t (*tell) (mpg123_handle *); |
| 89 |
void (*rewind) (mpg123_handle *); |
| 90 |
void (*forget) (mpg123_handle *); |
| 91 |
}; |
| 92 |
|
| 93 |
/* Open an external handle. */ |
| 94 |
int INT123_open_stream_handle(mpg123_handle *, void *iohandle); |
| 95 |
|
| 96 |
/* feed based operation has some specials */ |
| 97 |
int INT123_open_feed(mpg123_handle *); |
| 98 |
/* externally called function, returns 0 on success, -1 on error */ |
| 99 |
int INT123_feed_more(mpg123_handle *fr, const unsigned char *in, size_t count); |
| 100 |
void INT123_feed_forget(mpg123_handle *fr); /* forget the data that has been read (free some buffers) */ |
| 101 |
int64_t INT123_feed_set_pos(mpg123_handle *fr, int64_t pos); /* Set position (inside available data if possible), return wanted byte offset of next feed. */ |
| 102 |
|
| 103 |
void INT123_open_bad(mpg123_handle *); |
| 104 |
|
| 105 |
#define READER_ID3TAG 0x2 |
| 106 |
#define READER_SEEKABLE 0x4 |
| 107 |
#define READER_BUFFERED 0x8 |
| 108 |
#define READER_NOSEEK 0x10 |
| 109 |
#define READER_HANDLEIO 0x40 |
| 110 |
#define READER_STREAM 0 |
| 111 |
#define READER_ICY_STREAM 1 |
| 112 |
#define READER_FEED 2 |
| 113 |
/* These two add a little buffering to enable small seeks for peek ahead. */ |
| 114 |
#define READER_BUF_STREAM 3 |
| 115 |
#define READER_BUF_ICY_STREAM 4 |
| 116 |
|
| 117 |
#define READER_ERROR MPG123_ERR |
| 118 |
#define READER_MORE MPG123_NEED_MORE |
| 119 |
|
| 120 |
#endif |