]> iEval git - fdkaac.git/blame_incremental - src/extrapolater.c
add m4 macros
[fdkaac.git] / src / extrapolater.c
... / ...
CommitLineData
1/*
2 * Copyright (C) 2013 nu774
3 * For conditions of distribution and use, see copyright notice in COPYING
4 */
5#if HAVE_CONFIG_H
6# include "config.h"
7#endif
8#if HAVE_STDINT_H
9# include <stdint.h>
10#endif
11#include <stdlib.h>
12#include <string.h>
13#include <assert.h>
14#include "pcm_reader.h"
15#include "lpc.h"
16
17typedef int16_t sample_t;
18
19typedef struct buffer_t {
20 sample_t *data;
21 unsigned count; /* count in frames */
22 unsigned capacity; /* size in bytes */
23} buffer_t;
24
25typedef struct extrapolater_t {
26 pcm_reader_vtbl_t *vtbl;
27 pcm_reader_t *src;
28 pcm_sample_description_t format;
29 buffer_t buffer[2];
30 unsigned nbuffer;
31 int (*process)(struct extrapolater_t *, void *, unsigned);
32} extrapolater_t;
33
34#define LPC_ORDER 32
35
36static inline pcm_reader_t *get_source(pcm_reader_t *reader)
37{
38 return ((extrapolater_t *)reader)->src;
39}
40
41static const
42pcm_sample_description_t *get_format(pcm_reader_t *reader)
43{
44 return pcm_get_format(get_source(reader));
45}
46
47static int64_t get_length(pcm_reader_t *reader)
48{
49 return pcm_get_length(get_source(reader));
50}
51
52static int64_t get_position(pcm_reader_t *reader)
53{
54 return pcm_get_position(get_source(reader));
55}
56
57static int realloc_buffer(buffer_t *bp, size_t size)
58{
59 if (bp->capacity < size) {
60 void *p = realloc(bp->data, size);
61 if (!p) return -1;
62 bp->data = p;
63 bp->capacity = size;
64 }
65 return 0;
66}
67
68static void reverse_buffer(sample_t *data, unsigned nframes, unsigned nchannels)
69{
70 unsigned i = 0, j = nchannels * (nframes - 1), n;
71
72 for (; i < j; i += nchannels, j -= nchannels) {
73 for (n = 0; n < nchannels; ++n) {
74 sample_t tmp = data[i + n];
75 data[i + n] = data[j + n];
76 data[j + n] = tmp;
77 }
78 }
79}
80
81static int fetch(extrapolater_t *self, unsigned nframes)
82{
83 const pcm_sample_description_t *sfmt = pcm_get_format(self->src);
84 buffer_t *bp = &self->buffer[self->nbuffer];
85 int rc = 0;
86
87 if (realloc_buffer(bp, nframes * sfmt->bytes_per_frame) == 0) {
88 rc = pcm_read_frames(self->src, bp->data, nframes);
89 if (rc > 0) bp->count = rc;
90 }
91 if (rc > 0)
92 self->nbuffer ^= 1;
93 return rc <= 0 ? 0 : bp->count;
94}
95
96static int extrapolate(extrapolater_t *self, const buffer_t *bp,
97 void *dst, unsigned nframes)
98{
99 const pcm_sample_description_t *sfmt = pcm_get_format(self->src);
100 unsigned i, n = sfmt->channels_per_frame;
101 float lpc[LPC_ORDER];
102
103 for (i = 0; i < n; ++i) {
104 vorbis_lpc_from_data(bp->data + i, lpc, bp->count, LPC_ORDER, n);
105 vorbis_lpc_predict(lpc, &bp->data[i + n * (bp->count - LPC_ORDER)],
106 LPC_ORDER, (sample_t*)dst + i, nframes, n);
107 }
108 return nframes;
109}
110
111static int process1(extrapolater_t *self, void *buffer, unsigned nframes);
112static int process2(extrapolater_t *self, void *buffer, unsigned nframes);
113static int process3(extrapolater_t *self, void *buffer, unsigned nframes);
114
115static int process0(extrapolater_t *self, void *buffer, unsigned nframes)
116{
117 const pcm_sample_description_t *sfmt = pcm_get_format(self->src);
118 unsigned nchannels = sfmt->channels_per_frame;
119 buffer_t *bp = &self->buffer[self->nbuffer];
120
121 if (fetch(self, nframes) < 2 * LPC_ORDER)
122 memset(buffer, 0, nframes * sfmt->bytes_per_frame);
123 else {
124 reverse_buffer(bp->data, bp->count, nchannels);
125 extrapolate(self, bp, buffer, nframes);
126 reverse_buffer(buffer, nframes, nchannels);
127 reverse_buffer(bp->data, bp->count, nchannels);
128 }
129 if (bp->count)
130 self->process = process1;
131 else {
132 memset(bp->data, 0, nframes * sfmt->bytes_per_frame);
133 bp->count = nframes;
134 self->process = process2;
135 }
136 return nframes;
137}
138
139static int process1(extrapolater_t *self, void *buffer, unsigned nframes)
140{
141 const pcm_sample_description_t *sfmt = pcm_get_format(self->src);
142 buffer_t *bp = &self->buffer[self->nbuffer ^ 1];
143
144 assert(bp->count <= nframes);
145 memcpy(buffer, bp->data, bp->count * sfmt->bytes_per_frame);
146 if (!fetch(self, nframes)) {
147 buffer_t *bbp = &self->buffer[self->nbuffer];
148 if (bp->count < 2 * LPC_ORDER) {
149 size_t total = bp->count + bbp->count;
150 if (bbp->count &&
151 realloc_buffer(bbp, total * sfmt->bytes_per_frame) == 0 &&
152 realloc_buffer(bp, total * sfmt->bytes_per_frame) == 0)
153 {
154 memcpy(bbp->data + bbp->count * sfmt->channels_per_frame,
155 bp->data, bp->count * sfmt->bytes_per_frame);
156 memcpy(bp->data, bbp->data, total * sfmt->bytes_per_frame);
157 bp->count = total;
158 }
159 }
160 if (bp->count >= 2 * LPC_ORDER)
161 extrapolate(self, bp, bbp->data, nframes);
162 else
163 memset(bbp->data, 0, nframes * sfmt->bytes_per_frame);
164 bbp->count = nframes;
165 self->process = process2;
166 }
167 return bp->count;
168}
169
170static int process2(extrapolater_t *self, void *buffer, unsigned nframes)
171{
172 const pcm_sample_description_t *sfmt = pcm_get_format(self->src);
173 buffer_t *bp = &self->buffer[self->nbuffer];
174 if (bp->count < nframes)
175 nframes = bp->count;
176 memcpy(buffer, bp->data, nframes * sfmt->bytes_per_frame);
177 if (bp->count > nframes)
178 memmove(bp->data, bp->data + nframes * sfmt->channels_per_frame,
179 (bp->count - nframes) * sfmt->bytes_per_frame);
180 bp->count -= nframes;
181 if (bp->count == 0)
182 self->process = process3;
183 return nframes;
184}
185
186static int process3(extrapolater_t *self, void *buffer, unsigned nframes)
187{
188 return 0;
189}
190
191static int read_frames(pcm_reader_t *reader, void *buffer, unsigned nframes)
192{
193 extrapolater_t *self = (extrapolater_t *)reader;
194 return self->process(self, buffer, nframes);
195}
196
197static void teardown(pcm_reader_t **reader)
198{
199 extrapolater_t *self = (extrapolater_t *)*reader;
200 pcm_teardown(&self->src);
201 free(self->buffer[0].data);
202 free(self->buffer[1].data);
203 free(self);
204 *reader = 0;
205}
206
207static pcm_reader_vtbl_t my_vtable = {
208 get_format, get_length, get_position, read_frames, teardown
209};
210
211pcm_reader_t *extrapolater_open(pcm_reader_t *reader)
212{
213 extrapolater_t *self = 0;
214
215 if ((self = calloc(1, sizeof(extrapolater_t))) == 0)
216 return 0;
217 self->src = reader;
218 self->vtbl = &my_vtable;
219 self->process = process0;
220 return (pcm_reader_t *)self;
221}
This page took 0.021264 seconds and 4 git commands to generate.