2 * Direct Stream Transfer (DST) decoder
3 * Copyright (c) 2014 Peter Ross <pross@xvid.org>
5 * This file is part of FFmpeg.
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.
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.
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
24 * Direct Stream Transfer (DST) decoder
25 * ISO/IEC 14496-3 Part 3 Subpart 10: Technical description of lossless coding of oversampled audio
28 #include "libavutil/avassert.h"
29 #include "libavutil/intreadwrite.h"
37 #define DST_MAX_CHANNELS 6
38 #define DST_MAX_ELEMENTS (2 * DST_MAX_CHANNELS)
40 #define DSD_FS44(sample_rate) (sample_rate * 8LL / 44100)
42 #define DST_SAMPLES_PER_FRAME(sample_rate) (588 * DSD_FS44(sample_rate))
44 static const int8_t fsets_code_pred_coeff[3][3] = {
50 static const int8_t probs_code_pred_coeff[3][3] = {
56 typedef struct ArithCoder {
61 typedef struct Table {
62 unsigned int elements;
63 unsigned int length[DST_MAX_ELEMENTS];
64 int coeff[DST_MAX_ELEMENTS][128];
67 typedef struct DSTContext {
73 DECLARE_ALIGNED(16, uint8_t, status)[DST_MAX_CHANNELS][16];
74 DECLARE_ALIGNED(16, int16_t, filter)[DST_MAX_ELEMENTS][16][256];
75 DSDContext dsdctx[DST_MAX_CHANNELS];
78 static av_cold int decode_init(AVCodecContext *avctx)
80 DSTContext *s = avctx->priv_data;
83 if (avctx->channels > DST_MAX_CHANNELS) {
84 avpriv_request_sample(avctx, "Channel count %d", avctx->channels);
85 return AVERROR_PATCHWELCOME;
88 // the sample rate is only allowed to be 64,128,256 * 44100 by ISO/IEC 14496-3:2005(E)
89 // We are a bit more tolerant here, but this check is needed to bound the size and duration
90 if (avctx->sample_rate > 512 * 44100)
91 return AVERROR_INVALIDDATA;
94 if (DST_SAMPLES_PER_FRAME(avctx->sample_rate) & 7) {
95 return AVERROR_PATCHWELCOME;
98 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
100 for (i = 0; i < avctx->channels; i++)
101 memset(s->dsdctx[i].buf, 0x69, sizeof(s->dsdctx[i].buf));
108 static int read_map(GetBitContext *gb, Table *t, unsigned int map[DST_MAX_CHANNELS], int channels)
113 if (!get_bits1(gb)) {
114 for (ch = 1; ch < channels; ch++) {
115 int bits = av_log2(t->elements) + 1;
116 map[ch] = get_bits(gb, bits);
117 if (map[ch] == t->elements) {
119 if (t->elements >= DST_MAX_ELEMENTS)
120 return AVERROR_INVALIDDATA;
121 } else if (map[ch] > t->elements) {
122 return AVERROR_INVALIDDATA;
126 memset(map, 0, sizeof(*map) * DST_MAX_CHANNELS);
131 static av_always_inline int get_sr_golomb_dst(GetBitContext *gb, unsigned int k)
133 int v = get_ur_golomb_jpegls(gb, k, get_bits_left(gb), 0);
134 if (v && get_bits1(gb))
139 static void read_uncoded_coeff(GetBitContext *gb, int *dst, unsigned int elements,
140 int coeff_bits, int is_signed, int offset)
144 for (i = 0; i < elements; i++) {
145 dst[i] = (is_signed ? get_sbits(gb, coeff_bits) : get_bits(gb, coeff_bits)) + offset;
149 static int read_table(GetBitContext *gb, Table *t, const int8_t code_pred_coeff[3][3],
150 int length_bits, int coeff_bits, int is_signed, int offset)
152 unsigned int i, j, k;
153 for (i = 0; i < t->elements; i++) {
154 t->length[i] = get_bits(gb, length_bits) + 1;
155 if (!get_bits1(gb)) {
156 read_uncoded_coeff(gb, t->coeff[i], t->length[i], coeff_bits, is_signed, offset);
158 int method = get_bits(gb, 2), lsb_size;
160 return AVERROR_INVALIDDATA;
162 read_uncoded_coeff(gb, t->coeff[i], method + 1, coeff_bits, is_signed, offset);
164 lsb_size = get_bits(gb, 3);
165 for (j = method + 1; j < t->length[i]; j++) {
167 for (k = 0; k < method + 1; k++)
168 x += code_pred_coeff[method][k] * (unsigned)t->coeff[i][j - k - 1];
169 c = get_sr_golomb_dst(gb, lsb_size);
175 if (c < offset || c >= offset + (1<<coeff_bits))
176 return AVERROR_INVALIDDATA;
185 static void ac_init(ArithCoder *ac, GetBitContext *gb)
188 ac->c = get_bits(gb, 12);
191 static av_always_inline void ac_get(ArithCoder *ac, GetBitContext *gb, int p, int *e)
193 unsigned int k = (ac->a >> 8) | ((ac->a >> 7) & 1);
194 unsigned int q = k * p;
195 unsigned int a_q = ac->a - q;
206 int n = 11 - av_log2(ac->a);
208 ac->c = (ac->c << n) | get_bits(gb, n);
212 static uint8_t prob_dst_x_bit(int c)
214 return (ff_reverse[c & 127] >> 1) + 1;
217 static void build_filter(int16_t table[DST_MAX_ELEMENTS][16][256], const Table *fsets)
221 for (i = 0; i < fsets->elements; i++) {
222 int length = fsets->length[i];
224 for (j = 0; j < 16; j++) {
225 int total = av_clip(length - j * 8, 0, 8);
227 for (k = 0; k < 256; k++) {
230 for (l = 0; l < total; l++)
231 v += (((k >> l) & 1) * 2 - 1) * fsets->coeff[i][j * 8 + l];
238 static int decode_frame(AVCodecContext *avctx, void *data,
239 int *got_frame_ptr, AVPacket *avpkt)
241 unsigned samples_per_frame = DST_SAMPLES_PER_FRAME(avctx->sample_rate);
242 unsigned map_ch_to_felem[DST_MAX_CHANNELS];
243 unsigned map_ch_to_pelem[DST_MAX_CHANNELS];
244 unsigned i, ch, same_map, dst_x_bit;
245 unsigned half_prob[DST_MAX_CHANNELS];
246 const int channels = avctx->channels;
247 DSTContext *s = avctx->priv_data;
248 GetBitContext *gb = &s->gb;
249 ArithCoder *ac = &s->ac;
250 AVFrame *frame = data;
255 if (avpkt->size <= 1)
256 return AVERROR_INVALIDDATA;
258 frame->nb_samples = samples_per_frame / 8;
259 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
261 dsd = frame->data[0];
262 pcm = (float *)frame->data[0];
264 if ((ret = init_get_bits8(gb, avpkt->data, avpkt->size)) < 0)
267 if (!get_bits1(gb)) {
270 return AVERROR_INVALIDDATA;
271 memcpy(frame->data[0], avpkt->data + 1, FFMIN(avpkt->size - 1, frame->nb_samples * channels));
275 /* Segmentation (10.4, 10.5, 10.6) */
277 if (!get_bits1(gb)) {
278 avpriv_request_sample(avctx, "Not Same Segmentation");
279 return AVERROR_PATCHWELCOME;
282 if (!get_bits1(gb)) {
283 avpriv_request_sample(avctx, "Not Same Segmentation For All Channels");
284 return AVERROR_PATCHWELCOME;
287 if (!get_bits1(gb)) {
288 avpriv_request_sample(avctx, "Not End Of Channel Segmentation");
289 return AVERROR_PATCHWELCOME;
292 /* Mapping (10.7, 10.8, 10.9) */
294 same_map = get_bits1(gb);
296 if ((ret = read_map(gb, &s->fsets, map_ch_to_felem, channels)) < 0)
300 s->probs.elements = s->fsets.elements;
301 memcpy(map_ch_to_pelem, map_ch_to_felem, sizeof(map_ch_to_felem));
303 avpriv_request_sample(avctx, "Not Same Mapping");
304 if ((ret = read_map(gb, &s->probs, map_ch_to_pelem, channels)) < 0)
308 /* Half Probability (10.10) */
310 for (ch = 0; ch < channels; ch++)
311 half_prob[ch] = get_bits1(gb);
313 /* Filter Coef Sets (10.12) */
315 ret = read_table(gb, &s->fsets, fsets_code_pred_coeff, 7, 9, 1, 0);
319 /* Probability Tables (10.13) */
321 ret = read_table(gb, &s->probs, probs_code_pred_coeff, 6, 7, 0, 1);
325 /* Arithmetic Coded Data (10.11) */
328 return AVERROR_INVALIDDATA;
331 build_filter(s->filter, &s->fsets);
333 memset(s->status, 0xAA, sizeof(s->status));
334 memset(dsd, 0, frame->nb_samples * 4 * channels);
336 ac_get(ac, gb, prob_dst_x_bit(s->fsets.coeff[0][0]), &dst_x_bit);
338 for (i = 0; i < samples_per_frame; i++) {
339 for (ch = 0; ch < channels; ch++) {
340 const unsigned felem = map_ch_to_felem[ch];
341 int16_t (*filter)[256] = s->filter[felem];
342 uint8_t *status = s->status[ch];
343 int prob, residual, v;
345 #define F(x) filter[(x)][status[(x)]]
346 const int16_t predict = F( 0) + F( 1) + F( 2) + F( 3) +
347 F( 4) + F( 5) + F( 6) + F( 7) +
348 F( 8) + F( 9) + F(10) + F(11) +
349 F(12) + F(13) + F(14) + F(15);
352 if (!half_prob[ch] || i >= s->fsets.length[felem]) {
353 unsigned pelem = map_ch_to_pelem[ch];
354 unsigned index = FFABS(predict) >> 3;
355 prob = s->probs.coeff[pelem][FFMIN(index, s->probs.length[pelem] - 1)];
360 ac_get(ac, gb, prob, &residual);
361 v = ((predict >> 15) ^ residual) & 1;
362 dsd[((i >> 3) * channels + ch) << 2] |= v << (7 - (i & 0x7 ));
364 AV_WL64A(status + 8, (AV_RL64A(status + 8) << 1) | ((AV_RL64A(status) >> 63) & 1));
365 AV_WL64A(status, (AV_RL64A(status) << 1) | v);
370 for (i = 0; i < channels; i++) {
371 ff_dsd2pcm_translate(&s->dsdctx[i], frame->nb_samples, 0,
372 frame->data[0] + i * 4,
373 channels * 4, pcm + i, channels);
381 AVCodec ff_dst_decoder = {
383 .long_name = NULL_IF_CONFIG_SMALL("DST (Digital Stream Transfer)"),
384 .type = AVMEDIA_TYPE_AUDIO,
385 .id = AV_CODEC_ID_DST,
386 .priv_data_size = sizeof(DSTContext),
388 .decode = decode_frame,
389 .capabilities = AV_CODEC_CAP_DR1,
390 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
391 AV_SAMPLE_FMT_NONE },