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
28 #include "libavutil/avassert.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/mem_internal.h"
31 #include "internal.h"
32 #include "get_bits.h"
33 #include "avcodec.h"
34 #include "golomb.h"
35 #include "mathops.h"
36 #include "dsd.h"
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
45 static const int8_t fsets_code_pred_coeff[3][3] = {
46 { -8 },
47 { -16, 8 },
48 { -9, -5, 6 },
49 };
50
51 static const int8_t probs_code_pred_coeff[3][3] = {
52 { -8 },
53 { -16, 8 },
54 { -24, 24, -8 },
55 };
56
57 typedef struct ArithCoder {
58 unsigned int a;
59 unsigned int c;
60 } ArithCoder;
61
62 typedef struct Table {
63 unsigned int elements;
64 unsigned int length[DST_MAX_ELEMENTS];
65 int coeff[DST_MAX_ELEMENTS][128];
66 } Table;
67
68 typedef struct DSTContext {
69 AVClass *class;
70
71 GetBitContext gb;
72 ArithCoder ac;
73 Table fsets, probs;
74 DECLARE_ALIGNED(16, uint8_t, status)[DST_MAX_CHANNELS][16];
75 DECLARE_ALIGNED(16, int16_t, filter)[DST_MAX_ELEMENTS][16][256];
76 DSDContext dsdctx[DST_MAX_CHANNELS];
77 } DSTContext;
78
decode_init(AVCodecContext * avctx)79 static av_cold int decode_init(AVCodecContext *avctx)
80 {
81 DSTContext *s = avctx->priv_data;
82 int i;
83
84 if (avctx->channels > DST_MAX_CHANNELS) {
85 avpriv_request_sample(avctx, "Channel count %d", avctx->channels);
86 return AVERROR_PATCHWELCOME;
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
91 if (avctx->sample_rate > 512 * 44100)
92 return AVERROR_INVALIDDATA;
93
94
95 if (DST_SAMPLES_PER_FRAME(avctx->sample_rate) & 7) {
96 return AVERROR_PATCHWELCOME;
97 }
98
99 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
100
101 for (i = 0; i < avctx->channels; i++)
102 memset(s->dsdctx[i].buf, 0x69, sizeof(s->dsdctx[i].buf));
103
104 ff_init_dsd_data();
105
106 return 0;
107 }
108
read_map(GetBitContext * gb,Table * t,unsigned int map[DST_MAX_CHANNELS],int channels)109 static int read_map(GetBitContext *gb, Table *t, unsigned int map[DST_MAX_CHANNELS], int channels)
110 {
111 int ch;
112 t->elements = 1;
113 map[0] = 0;
114 if (!get_bits1(gb)) {
115 for (ch = 1; ch < channels; ch++) {
116 int bits = av_log2(t->elements) + 1;
117 map[ch] = get_bits(gb, bits);
118 if (map[ch] == t->elements) {
119 t->elements++;
120 if (t->elements >= DST_MAX_ELEMENTS)
121 return AVERROR_INVALIDDATA;
122 } else if (map[ch] > t->elements) {
123 return AVERROR_INVALIDDATA;
124 }
125 }
126 } else {
127 memset(map, 0, sizeof(*map) * DST_MAX_CHANNELS);
128 }
129 return 0;
130 }
131
get_sr_golomb_dst(GetBitContext * gb,unsigned int k)132 static av_always_inline int get_sr_golomb_dst(GetBitContext *gb, unsigned int k)
133 {
134 int v = get_ur_golomb_jpegls(gb, k, get_bits_left(gb), 0);
135 if (v && get_bits1(gb))
136 v = -v;
137 return v;
138 }
139
read_uncoded_coeff(GetBitContext * gb,int * dst,unsigned int elements,int coeff_bits,int is_signed,int offset)140 static void read_uncoded_coeff(GetBitContext *gb, int *dst, unsigned int elements,
141 int coeff_bits, int is_signed, int offset)
142 {
143 int i;
144
145 for (i = 0; i < elements; i++) {
146 dst[i] = (is_signed ? get_sbits(gb, coeff_bits) : get_bits(gb, coeff_bits)) + offset;
147 }
148 }
149
read_table(GetBitContext * gb,Table * t,const int8_t code_pred_coeff[3][3],int length_bits,int coeff_bits,int is_signed,int offset)150 static int read_table(GetBitContext *gb, Table *t, const int8_t code_pred_coeff[3][3],
151 int length_bits, int coeff_bits, int is_signed, int offset)
152 {
153 unsigned int i, j, k;
154 for (i = 0; i < t->elements; i++) {
155 t->length[i] = get_bits(gb, length_bits) + 1;
156 if (!get_bits1(gb)) {
157 read_uncoded_coeff(gb, t->coeff[i], t->length[i], coeff_bits, is_signed, offset);
158 } else {
159 int method = get_bits(gb, 2), lsb_size;
160 if (method == 3)
161 return AVERROR_INVALIDDATA;
162
163 read_uncoded_coeff(gb, t->coeff[i], method + 1, coeff_bits, is_signed, offset);
164
165 lsb_size = get_bits(gb, 3);
166 for (j = method + 1; j < t->length[i]; j++) {
167 int c, x = 0;
168 for (k = 0; k < method + 1; k++)
169 x += code_pred_coeff[method][k] * (unsigned)t->coeff[i][j - k - 1];
170 c = get_sr_golomb_dst(gb, lsb_size);
171 if (x >= 0)
172 c -= (x + 4) / 8;
173 else
174 c += (-x + 3) / 8;
175 if (!is_signed) {
176 if (c < offset || c >= offset + (1<<coeff_bits))
177 return AVERROR_INVALIDDATA;
178 }
179 t->coeff[i][j] = c;
180 }
181 }
182 }
183 return 0;
184 }
185
ac_init(ArithCoder * ac,GetBitContext * gb)186 static void ac_init(ArithCoder *ac, GetBitContext *gb)
187 {
188 ac->a = 4095;
189 ac->c = get_bits(gb, 12);
190 }
191
ac_get(ArithCoder * ac,GetBitContext * gb,int p,int * e)192 static av_always_inline void ac_get(ArithCoder *ac, GetBitContext *gb, int p, int *e)
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
198 *e = ac->c < a_q;
199 if (*e) {
200 ac->a = a_q;
201 } else {
202 ac->a = q;
203 ac->c -= a_q;
204 }
205
206 if (ac->a < 2048) {
207 int n = 11 - av_log2(ac->a);
208 ac->a <<= n;
209 ac->c = (ac->c << n) | get_bits(gb, n);
210 }
211 }
212
prob_dst_x_bit(int c)213 static uint8_t prob_dst_x_bit(int c)
214 {
215 return (ff_reverse[c & 127] >> 1) + 1;
216 }
217
build_filter(int16_t table[DST_MAX_ELEMENTS][16][256],const Table * fsets)218 static void build_filter(int16_t table[DST_MAX_ELEMENTS][16][256], const Table *fsets)
219 {
220 int i, j, k, l;
221
222 for (i = 0; i < fsets->elements; i++) {
223 int length = fsets->length[i];
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++) {
229 int v = 0;
230
231 for (l = 0; l < total; l++)
232 v += (((k >> l) & 1) * 2 - 1) * fsets->coeff[i][j * 8 + l];
233 table[i][j][k] = v;
234 }
235 }
236 }
237 }
238
decode_frame(AVCodecContext * avctx,void * data,int * got_frame_ptr,AVPacket * avpkt)239 static int decode_frame(AVCodecContext *avctx, void *data,
240 int *got_frame_ptr, AVPacket *avpkt)
241 {
242 unsigned samples_per_frame = DST_SAMPLES_PER_FRAME(avctx->sample_rate);
243 unsigned map_ch_to_felem[DST_MAX_CHANNELS];
244 unsigned map_ch_to_pelem[DST_MAX_CHANNELS];
245 unsigned i, ch, same_map, dst_x_bit;
246 unsigned half_prob[DST_MAX_CHANNELS];
247 const int channels = avctx->channels;
248 DSTContext *s = avctx->priv_data;
249 GetBitContext *gb = &s->gb;
250 ArithCoder *ac = &s->ac;
251 AVFrame *frame = data;
252 uint8_t *dsd;
253 float *pcm;
254 int ret;
255
256 if (avpkt->size <= 1)
257 return AVERROR_INVALIDDATA;
258
259 frame->nb_samples = samples_per_frame / 8;
260 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
261 return ret;
262 dsd = frame->data[0];
263 pcm = (float *)frame->data[0];
264
265 if ((ret = init_get_bits8(gb, avpkt->data, avpkt->size)) < 0)
266 return ret;
267
268 if (!get_bits1(gb)) {
269 skip_bits1(gb);
270 if (get_bits(gb, 6))
271 return AVERROR_INVALIDDATA;
272 memcpy(frame->data[0], avpkt->data + 1, FFMIN(avpkt->size - 1, frame->nb_samples * channels));
273 goto dsd;
274 }
275
276 /* Segmentation (10.4, 10.5, 10.6) */
277
278 if (!get_bits1(gb)) {
279 avpriv_request_sample(avctx, "Not Same Segmentation");
280 return AVERROR_PATCHWELCOME;
281 }
282
283 if (!get_bits1(gb)) {
284 avpriv_request_sample(avctx, "Not Same Segmentation For All Channels");
285 return AVERROR_PATCHWELCOME;
286 }
287
288 if (!get_bits1(gb)) {
289 avpriv_request_sample(avctx, "Not End Of Channel Segmentation");
290 return AVERROR_PATCHWELCOME;
291 }
292
293 /* Mapping (10.7, 10.8, 10.9) */
294
295 same_map = get_bits1(gb);
296
297 if ((ret = read_map(gb, &s->fsets, map_ch_to_felem, channels)) < 0)
298 return ret;
299
300 if (same_map) {
301 s->probs.elements = s->fsets.elements;
302 memcpy(map_ch_to_pelem, map_ch_to_felem, sizeof(map_ch_to_felem));
303 } else {
304 avpriv_request_sample(avctx, "Not Same Mapping");
305 if ((ret = read_map(gb, &s->probs, map_ch_to_pelem, channels)) < 0)
306 return ret;
307 }
308
309 /* Half Probability (10.10) */
310
311 for (ch = 0; ch < channels; ch++)
312 half_prob[ch] = get_bits1(gb);
313
314 /* Filter Coef Sets (10.12) */
315
316 ret = read_table(gb, &s->fsets, fsets_code_pred_coeff, 7, 9, 1, 0);
317 if (ret < 0)
318 return ret;
319
320 /* Probability Tables (10.13) */
321
322 ret = read_table(gb, &s->probs, probs_code_pred_coeff, 6, 7, 0, 1);
323 if (ret < 0)
324 return ret;
325
326 /* Arithmetic Coded Data (10.11) */
327
328 if (get_bits1(gb))
329 return AVERROR_INVALIDDATA;
330 ac_init(ac, gb);
331
332 build_filter(s->filter, &s->fsets);
333
334 memset(s->status, 0xAA, sizeof(s->status));
335 memset(dsd, 0, frame->nb_samples * 4 * channels);
336
337 ac_get(ac, gb, prob_dst_x_bit(s->fsets.coeff[0][0]), &dst_x_bit);
338
339 for (i = 0; i < samples_per_frame; i++) {
340 for (ch = 0; ch < channels; ch++) {
341 const unsigned felem = map_ch_to_felem[ch];
342 int16_t (*filter)[256] = s->filter[felem];
343 uint8_t *status = s->status[ch];
344 int prob, residual, v;
345
346 #define F(x) filter[(x)][status[(x)]]
347 const int16_t predict = F( 0) + F( 1) + F( 2) + F( 3) +
348 F( 4) + F( 5) + F( 6) + F( 7) +
349 F( 8) + F( 9) + F(10) + F(11) +
350 F(12) + F(13) + F(14) + F(15);
351 #undef F
352
353 if (!half_prob[ch] || i >= s->fsets.length[felem]) {
354 unsigned pelem = map_ch_to_pelem[ch];
355 unsigned index = FFABS(predict) >> 3;
356 prob = s->probs.coeff[pelem][FFMIN(index, s->probs.length[pelem] - 1)];
357 } else {
358 prob = 128;
359 }
360
361 ac_get(ac, gb, prob, &residual);
362 v = ((predict >> 15) ^ residual) & 1;
363 dsd[((i >> 3) * channels + ch) << 2] |= v << (7 - (i & 0x7 ));
364
365 AV_WL64A(status + 8, (AV_RL64A(status + 8) << 1) | ((AV_RL64A(status) >> 63) & 1));
366 AV_WL64A(status, (AV_RL64A(status) << 1) | v);
367 }
368 }
369
370 dsd:
371 for (i = 0; i < channels; i++) {
372 ff_dsd2pcm_translate(&s->dsdctx[i], frame->nb_samples, 0,
373 frame->data[0] + i * 4,
374 channels * 4, pcm + i, channels);
375 }
376
377 *got_frame_ptr = 1;
378
379 return avpkt->size;
380 }
381
382 AVCodec ff_dst_decoder = {
383 .name = "dst",
384 .long_name = NULL_IF_CONFIG_SMALL("DST (Digital Stream Transfer)"),
385 .type = AVMEDIA_TYPE_AUDIO,
386 .id = AV_CODEC_ID_DST,
387 .priv_data_size = sizeof(DSTContext),
388 .init = decode_init,
389 .decode = decode_frame,
390 .capabilities = AV_CODEC_CAP_DR1,
391 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
392 AV_SAMPLE_FMT_NONE },
393 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
394 };
395