1 /*
2 * Direct Stream Transfer (DST) decoder
3 * Copyright (c) 2014 Peter Ross <pross@xvid.org>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Direct Stream Transfer (DST) decoder
25 * ISO/IEC 14496-3 Part 3 Subpart 10: Technical description of lossless coding of oversampled audio
26 */
27
37
38 #define DST_MAX_CHANNELS 6
39 #define DST_MAX_ELEMENTS (2 * DST_MAX_CHANNELS)
40
41 #define DSD_FS44(sample_rate) (sample_rate * 8LL / 44100)
42
43 #define DST_SAMPLES_PER_FRAME(sample_rate) (588 * DSD_FS44(sample_rate))
44
46 { -8 },
47 { -16, 8 },
48 { -9, -5, 6 },
49 };
50
52 { -8 },
53 { -16, 8 },
54 { -24, 24, -8 },
55 };
56
61
67
70
78
80 {
83
87 }
88
89 // the sample rate is only allowed to be 64,128,256 * 44100 by ISO/IEC 14496-3:2005(E)
90 // We are a bit more tolerant here, but this check is needed to bound the size and duration
93
94
97 }
98
100
102 memset(
s->dsdctx[
i].buf, 0x69,
sizeof(
s->dsdctx[
i].buf));
103
105
106 return 0;
107 }
108
110 {
111 int ch;
124 }
125 }
126 } else {
128 }
129 return 0;
130 }
131
133 {
136 v = -v;
137 return v;
138 }
139
141 int coeff_bits,
int is_signed,
int offset)
142 {
144
147 }
148 }
149
151 int length_bits,
int coeff_bits,
int is_signed,
int offset)
152 {
153 unsigned int i, j, k;
158 } else {
159 int method =
get_bits(gb, 2), lsb_size;
160 if (method == 3)
162
164
166 for (j = method + 1; j < t->
length[
i]; j++) {
168 for (k = 0; k < method + 1; k++)
169 x += code_pred_coeff[method][k] * (
unsigned)t->
coeff[
i][j - k - 1];
171 if (x >= 0)
173 else
175 if (!is_signed) {
176 if (c < offset || c >=
offset + (1<<coeff_bits))
178 }
180 }
181 }
182 }
183 return 0;
184 }
185
187 {
190 }
191
193 {
194 unsigned int k = (ac->
a >> 8) | ((ac->
a >> 7) & 1);
195 unsigned int q = k *
p;
196 unsigned int a_q = ac->
a - q;
197
199 if (*e) {
201 } else {
204 }
205
210 }
211 }
212
214 {
216 }
217
219 {
221
224
225 for (j = 0; j < 16; j++) {
226 int total =
av_clip(length - j * 8, 0, 8);
227
228 for (k = 0; k < 256; k++) {
230
231 for (l = 0; l < total; l++)
232 v += (((k >> l) & 1) * 2 - 1) * fsets->
coeff[
i][j * 8 + l];
233 if ((int16_t)v != v)
236 }
237 }
238 }
239 return 0;
240 }
241
243 int *got_frame_ptr,
AVPacket *avpkt)
244 {
248 unsigned i, ch, same_map, dst_x_bit;
254 uint8_t *dsd;
255 float *pcm;
257
258 if (avpkt->
size <= 1)
260
261 frame->nb_samples = samples_per_frame / 8;
264 dsd =
frame->data[0];
265 pcm = (
float *)
frame->data[0];
266
269
275 goto dsd;
276 }
277
278 /* Segmentation (10.4, 10.5, 10.6) */
279
283 }
284
288 }
289
293 }
294
295 /* Mapping (10.7, 10.8, 10.9) */
296
298
301
302 if (same_map) {
303 s->probs.elements =
s->fsets.elements;
304 memcpy(map_ch_to_pelem, map_ch_to_felem, sizeof(map_ch_to_felem));
305 } else {
309 }
310
311 /* Half Probability (10.10) */
312
315
316 /* Filter Coef Sets (10.12) */
317
321
322 /* Probability Tables (10.13) */
323
327
328 /* Arithmetic Coded Data (10.11) */
329
333
337
338 memset(
s->status, 0xAA,
sizeof(
s->status));
340
342
343 for (
i = 0;
i < samples_per_frame;
i++) {
345 const unsigned felem = map_ch_to_felem[ch];
346 int16_t (*
filter)[256] =
s->filter[felem];
347 uint8_t *
status =
s->status[ch];
348 int prob, residual, v;
349
350 #define F(x) filter[(x)][status[(x)]]
351 const int16_t
predict =
F( 0) +
F( 1) +
F( 2) +
F( 3) +
352 F( 4) +
F( 5) +
F( 6) +
F( 7) +
353 F( 8) +
F( 9) +
F(10) +
F(11) +
354 F(12) +
F(13) +
F(14) +
F(15);
355 #undef F
356
357 if (!half_prob[ch] ||
i >=
s->fsets.length[felem]) {
358 unsigned pelem = map_ch_to_pelem[ch];
361 } else {
363 }
364
366 v = ((
predict >> 15) ^ residual) & 1;
367 dsd[((
i >> 3) *
channels + ch) << 2] |= v << (7 - (
i & 0x7 ));
368
371 }
372 }
373
374 dsd:
379 }
380
381 *got_frame_ptr = 1;
382
384 }
385
396 };