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