29a8f73f |
1 | /* |
2 | * Copyright (C) 2013 nu774 |
3 | * For conditions of distribution and use, see copyright notice in COPYING |
4 | */ |
5 | |
6 | #if HAVE_CONFIG_H |
7 | # include "config.h" |
8 | #endif |
9 | |
10 | #if HAVE_STDINT_H |
11 | # include <stdint.h> |
12 | #endif |
13 | |
14 | #include <stdio.h> |
15 | #include <stdarg.h> |
16 | #include "pcm_reader.h" |
17 | #include "m4af_endian.h" |
18 | |
19 | int pcm_read(pcm_io_context_t *io, void *buffer, uint32_t size) |
20 | { |
21 | int rc; |
22 | uint32_t count = 0; |
23 | |
24 | do { |
25 | rc = io->vtbl->read(io->cookie, buffer, size - count); |
26 | if (rc > 0) |
27 | count += rc; |
28 | } while (rc > 0 && count < size); |
29 | return count > 0 ? count : rc; |
30 | } |
31 | |
32 | int pcm_skip(pcm_io_context_t *io, int64_t count) |
33 | { |
34 | char buff[8192]; |
35 | int rc; |
36 | pcm_io_vtbl_t *vp = io->vtbl; |
37 | |
38 | if (count == 0 || pcm_seek(io, count, SEEK_CUR) >= 0) |
39 | return 0; |
40 | do { |
41 | if ((rc = vp->read(io->cookie, buff, count > 8192 ? 8192 : count)) > 0) |
42 | count -= rc; |
43 | } while (rc > 0 && count > 0); |
44 | |
45 | return count == 0 ? 0 : -1; |
46 | } |
47 | |
48 | int pcm_read16le(pcm_io_context_t *io, uint16_t *value) |
49 | { |
50 | if (pcm_read(io, value, 2) == 2) { |
51 | *value = m4af_ltoh16(*value); |
52 | return 0; |
53 | } |
54 | return -1; |
55 | } |
56 | |
57 | int pcm_read16be(pcm_io_context_t *io, uint16_t *value) |
58 | { |
59 | if (pcm_read(io, value, 2) == 2) { |
60 | *value = m4af_btoh16(*value); |
61 | return 0; |
62 | } |
63 | return -1; |
64 | } |
65 | |
66 | int pcm_read32le(pcm_io_context_t *io, uint32_t *value) |
67 | { |
68 | if (pcm_read(io, value, 4) == 4) { |
69 | *value = m4af_ltoh32(*value); |
70 | return 0; |
71 | } |
72 | return -1; |
73 | } |
74 | |
75 | int pcm_read32be(pcm_io_context_t *io, uint32_t *value) |
76 | { |
77 | if (pcm_read(io, value, 4) == 4) { |
78 | *value = m4af_btoh32(*value); |
79 | return 0; |
80 | } |
81 | return -1; |
82 | } |
83 | |
84 | int pcm_read64le(pcm_io_context_t *io, uint64_t *value) |
85 | { |
86 | if (pcm_read(io, value, 8) == 8) { |
87 | *value = m4af_ltoh64(*value); |
88 | return 0; |
89 | } |
90 | return -1; |
91 | } |
92 | |
93 | int pcm_read64be(pcm_io_context_t *io, uint64_t *value) |
94 | { |
95 | if (pcm_read(io, value, 8) == 8) { |
96 | *value = m4af_btoh64(*value); |
97 | return 0; |
98 | } |
99 | return -1; |
100 | } |
101 | |
102 | int pcm_scanl(pcm_io_context_t *io, const char *fmt, ...) |
103 | { |
104 | int c, count = 0; |
105 | va_list ap; |
106 | |
107 | va_start(ap, fmt); |
108 | while ((c = *fmt++)) { |
109 | switch (c) { |
110 | case 'S': |
111 | TRY_IO(pcm_read16le(io, va_arg(ap, uint16_t*))); |
112 | ++count; |
113 | break; |
114 | case 'L': |
115 | TRY_IO(pcm_read32le(io, va_arg(ap, uint32_t*))); |
116 | ++count; |
117 | break; |
118 | case 'Q': |
119 | TRY_IO(pcm_read64le(io, va_arg(ap, uint64_t*))); |
120 | ++count; |
121 | break; |
122 | } |
123 | } |
124 | FAIL: |
125 | va_end(ap); |
126 | return count; |
127 | } |
128 | |
129 | int pcm_scanb(pcm_io_context_t *io, const char *fmt, ...) |
130 | { |
131 | int c, count = 0; |
132 | va_list ap; |
133 | |
134 | va_start(ap, fmt); |
135 | while ((c = *fmt++)) { |
136 | switch (c) { |
137 | case 'S': |
138 | TRY_IO(pcm_read16be(io, va_arg(ap, uint16_t*))); |
139 | ++count; |
140 | break; |
141 | case 'L': |
142 | TRY_IO(pcm_read32be(io, va_arg(ap, uint32_t*))); |
143 | ++count; |
144 | break; |
145 | case 'Q': |
146 | TRY_IO(pcm_read64be(io, va_arg(ap, uint64_t*))); |
147 | ++count; |
148 | break; |
149 | } |
150 | } |
151 | FAIL: |
152 | va_end(ap); |
153 | return count; |
154 | } |