FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dstdec.c
Date: 2026-09-17 13:51:07
Exec Total Coverage
Lines: 131 190 68.9%
Functions: 10 11 90.9%
Branches: 61 112 54.5%

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