| 1 | /* |
| 2 | * Copyright (C) 2013 nu774 |
| 3 | * For conditions of distribution and use, see copyright notice in COPYING |
| 4 | */ |
| 5 | #ifndef PCM_READER_H |
| 6 | #define PCM_READER_H |
| 7 | |
| 8 | #include "lpcm.h" |
| 9 | |
| 10 | typedef struct pcm_reader_t pcm_reader_t; |
| 11 | |
| 12 | typedef struct pcm_reader_vtbl_t { |
| 13 | const pcm_sample_description_t *(*get_format)(pcm_reader_t *); |
| 14 | int64_t (*get_length)(pcm_reader_t *); |
| 15 | int64_t (*get_position)(pcm_reader_t *); |
| 16 | int (*read_frames)(pcm_reader_t *, void *, unsigned); |
| 17 | void (*teardown)(pcm_reader_t **); |
| 18 | } pcm_reader_vtbl_t; |
| 19 | |
| 20 | struct pcm_reader_t { |
| 21 | pcm_reader_vtbl_t *vtbl; |
| 22 | }; |
| 23 | |
| 24 | typedef int (*pcm_read_callback)(void *cookie, void *data, uint32_t count); |
| 25 | typedef int (*pcm_seek_callback)(void *cookie, int64_t off, int whence); |
| 26 | typedef int64_t (*pcm_tell_callback)(void *cookie); |
| 27 | |
| 28 | typedef struct pcm_io_vtbl_t { |
| 29 | pcm_read_callback read; |
| 30 | pcm_seek_callback seek; |
| 31 | pcm_tell_callback tell; |
| 32 | } pcm_io_vtbl_t; |
| 33 | |
| 34 | typedef struct pcm_io_context_t { |
| 35 | pcm_io_vtbl_t *vtbl; |
| 36 | void *cookie; |
| 37 | } pcm_io_context_t; |
| 38 | |
| 39 | static inline |
| 40 | const pcm_sample_description_t *pcm_get_format(pcm_reader_t *r) |
| 41 | { |
| 42 | return r->vtbl->get_format(r); |
| 43 | } |
| 44 | |
| 45 | static inline |
| 46 | int64_t pcm_get_length(pcm_reader_t *r) |
| 47 | { |
| 48 | return r->vtbl->get_length(r); |
| 49 | } |
| 50 | |
| 51 | static inline |
| 52 | int64_t pcm_get_position(pcm_reader_t *r) |
| 53 | { |
| 54 | return r->vtbl->get_position(r); |
| 55 | } |
| 56 | |
| 57 | static inline |
| 58 | int64_t pcm_read_frames(pcm_reader_t *r, void *data, unsigned nframes) |
| 59 | { |
| 60 | return r->vtbl->read_frames(r, data, nframes); |
| 61 | } |
| 62 | |
| 63 | static inline |
| 64 | void pcm_teardown(pcm_reader_t **r) |
| 65 | { |
| 66 | (*r)->vtbl->teardown(r); |
| 67 | } |
| 68 | |
| 69 | static inline |
| 70 | uint32_t bitcount(uint32_t bits) |
| 71 | { |
| 72 | bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555); |
| 73 | bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333); |
| 74 | bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f); |
| 75 | bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff); |
| 76 | return (bits & 0x0000ffff) + (bits >>16 & 0x0000ffff); |
| 77 | } |
| 78 | |
| 79 | #define TRY_IO(expr) \ |
| 80 | do { \ |
| 81 | if ((expr)) goto FAIL; \ |
| 82 | } while (0) |
| 83 | |
| 84 | #define ENSURE(expr) \ |
| 85 | do { \ |
| 86 | if (!(expr)) goto FAIL;\ |
| 87 | } while (0) |
| 88 | |
| 89 | int pcm_read(pcm_io_context_t *io, void *buffer, uint32_t size); |
| 90 | int pcm_skip(pcm_io_context_t *io, int64_t count); |
| 91 | |
| 92 | static inline int pcm_seek(pcm_io_context_t *io, int64_t off, int whence) |
| 93 | { |
| 94 | return io->vtbl->seek ? io->vtbl->seek(io->cookie, off, whence) : -1; |
| 95 | } |
| 96 | |
| 97 | static inline int64_t pcm_tell(pcm_io_context_t *io) |
| 98 | { |
| 99 | return io->vtbl->tell ? io->vtbl->tell(io->cookie) : -1; |
| 100 | } |
| 101 | |
| 102 | int pcm_read16le(pcm_io_context_t *io, uint16_t *value); |
| 103 | int pcm_read16be(pcm_io_context_t *io, uint16_t *value); |
| 104 | int pcm_read32le(pcm_io_context_t *io, uint32_t *value); |
| 105 | int pcm_read32be(pcm_io_context_t *io, uint32_t *value); |
| 106 | int pcm_read64le(pcm_io_context_t *io, uint64_t *value); |
| 107 | int pcm_read64be(pcm_io_context_t *io, uint64_t *value); |
| 108 | int pcm_scanl(pcm_io_context_t *io, const char *fmt, ...); |
| 109 | int pcm_scanb(pcm_io_context_t *io, const char *fmt, ...); |
| 110 | |
| 111 | int apple_chan_chunk(pcm_io_context_t *io, uint32_t chunk_size, |
| 112 | pcm_sample_description_t *fmt, uint8_t *mapping); |
| 113 | |
| 114 | pcm_reader_t *pcm_open_sint16_converter(pcm_reader_t *reader); |
| 115 | |
| 116 | pcm_reader_t *extrapolater_open(pcm_reader_t *reader); |
| 117 | |
| 118 | #endif |