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