refactor pcm io routines
[fdkaac.git] / src / pcm_reader.h
1 #ifndef PCM_READER_H
2 #define PCM_READER_H
3
4 #include "lpcm.h"
5
6 typedef struct pcm_reader_t pcm_reader_t;
7
8 typedef struct pcm_reader_vtbl_t {
9 const pcm_sample_description_t *(*get_format)(pcm_reader_t *);
10 int64_t (*get_length)(pcm_reader_t *);
11 int64_t (*get_position)(pcm_reader_t *);
12 int (*read_frames)(pcm_reader_t *, void *, unsigned);
13 void (*teardown)(pcm_reader_t **);
14 } pcm_reader_vtbl_t;
15
16 struct pcm_reader_t {
17 pcm_reader_vtbl_t *vtbl;
18 };
19
20 typedef int (*pcm_read_callback)(void *cookie, void *data, uint32_t count);
21 typedef int (*pcm_seek_callback)(void *cookie, int64_t off, int whence);
22 typedef int64_t (*pcm_tell_callback)(void *cookie);
23
24 typedef struct pcm_io_vtbl_t {
25 pcm_read_callback read;
26 pcm_seek_callback seek;
27 pcm_tell_callback tell;
28 } pcm_io_vtbl_t;
29
30 typedef struct pcm_io_context_t {
31 pcm_io_vtbl_t *vtbl;
32 void *cookie;
33 } pcm_io_context_t;
34
35 static inline
36 const pcm_sample_description_t *pcm_get_format(pcm_reader_t *r)
37 {
38 return r->vtbl->get_format(r);
39 }
40
41 static inline
42 int64_t pcm_get_length(pcm_reader_t *r)
43 {
44 return r->vtbl->get_length(r);
45 }
46
47 static inline
48 int64_t pcm_get_position(pcm_reader_t *r)
49 {
50 return r->vtbl->get_position(r);
51 }
52
53 static inline
54 int64_t pcm_read_frames(pcm_reader_t *r, void *data, unsigned nframes)
55 {
56 return r->vtbl->read_frames(r, data, nframes);
57 }
58
59 static inline
60 void pcm_teardown(pcm_reader_t **r)
61 {
62 (*r)->vtbl->teardown(r);
63 }
64
65 pcm_reader_t *pcm_open_sint16_converter(pcm_reader_t *reader);
66
67 #define TRY_IO(expr) \
68 do { \
69 if ((expr)) goto FAIL; \
70 } while (0)
71
72 int pcm_read(pcm_io_context_t *io, void *buffer, uint32_t size);
73 int pcm_skip(pcm_io_context_t *io, int64_t count);
74
75 static int pcm_seek(pcm_io_context_t *io, int64_t off, int whence)
76 {
77 return io->vtbl->seek ? io->vtbl->seek(io->cookie, off, whence) : -1;
78 }
79
80 static inline int64_t pcm_tell(pcm_io_context_t *io)
81 {
82 return io->vtbl->tell ? io->vtbl->tell(io->cookie) : -1;
83 }
84
85 int pcm_read16le(pcm_io_context_t *io, uint16_t *value);
86 int pcm_read16be(pcm_io_context_t *io, uint16_t *value);
87 int pcm_read32le(pcm_io_context_t *io, uint32_t *value);
88 int pcm_read32be(pcm_io_context_t *io, uint32_t *value);
89 int pcm_read64le(pcm_io_context_t *io, uint64_t *value);
90 int pcm_read64be(pcm_io_context_t *io, uint64_t *value);
91 int pcm_scanl(pcm_io_context_t *io, const char *fmt, ...);
92 int pcm_scanb(pcm_io_context_t *io, const char *fmt, ...);
93
94 #endif
This page took 0.022654 seconds and 4 git commands to generate.