/src/ffmpeg/libavcodec/dstdec.c
Line | Count | 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 | 13.2k | #define DST_MAX_CHANNELS 6 |
46 | 5.14k | #define DST_MAX_ELEMENTS (2 * DST_MAX_CHANNELS) |
47 | | |
48 | 129k | #define DSD_FS44(sample_rate) (sample_rate * 8LL / 44100) |
49 | | |
50 | 129k | #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 | | static av_cold int decode_init(AVCodecContext *avctx) |
91 | 918 | { |
92 | 918 | if (avctx->ch_layout.nb_channels > DST_MAX_CHANNELS) { |
93 | 40 | avpriv_request_sample(avctx, "Channel count %d", avctx->ch_layout.nb_channels); |
94 | 40 | return AVERROR_PATCHWELCOME; |
95 | 40 | } |
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 | 878 | if (avctx->sample_rate > 512 * 44100) |
100 | 10 | return AVERROR_INVALIDDATA; |
101 | | |
102 | | |
103 | 868 | if (DST_SAMPLES_PER_FRAME(avctx->sample_rate) & 7) { |
104 | 10 | return AVERROR_PATCHWELCOME; |
105 | 10 | } |
106 | | |
107 | 858 | avctx->sample_fmt = AV_SAMPLE_FMT_DSD; |
108 | | |
109 | 858 | #if CONFIG_SWRESAMPLE && FF_API_DSD_PCM |
110 | 858 | if (avctx->request_sample_fmt != AV_SAMPLE_FMT_DSD) |
111 | 858 | avctx->sample_fmt = AV_SAMPLE_FMT_FLT; |
112 | 858 | #endif |
113 | | |
114 | 858 | return 0; |
115 | 868 | } |
116 | | |
117 | | static av_cold int decode_close(AVCodecContext *avctx) |
118 | 858 | { |
119 | 858 | #if CONFIG_SWRESAMPLE && FF_API_DSD_PCM |
120 | 858 | DSTContext *s = avctx->priv_data; |
121 | | |
122 | 858 | swr_free(&s->swr); |
123 | 858 | av_freep(&s->scratch); |
124 | 858 | #endif |
125 | 858 | return 0; |
126 | 858 | } |
127 | | |
128 | | static int read_map(GetBitContext *gb, Table *t, unsigned int map[DST_MAX_CHANNELS], int channels) |
129 | 11.6k | { |
130 | 11.6k | int ch; |
131 | 11.6k | t->elements = 1; |
132 | 11.6k | map[0] = 0; |
133 | 11.6k | if (!get_bits1(gb)) { |
134 | 18.8k | for (ch = 1; ch < channels; ch++) { |
135 | 14.8k | int bits = av_log2(t->elements) + 1; |
136 | 14.8k | map[ch] = get_bits(gb, bits); |
137 | 14.8k | if (map[ch] == t->elements) { |
138 | 5.14k | t->elements++; |
139 | 5.14k | if (t->elements >= DST_MAX_ELEMENTS) |
140 | 0 | return AVERROR_INVALIDDATA; |
141 | 9.68k | } else if (map[ch] > t->elements) { |
142 | 445 | return AVERROR_INVALIDDATA; |
143 | 445 | } |
144 | 14.8k | } |
145 | 7.21k | } else { |
146 | 7.21k | memset(map, 0, sizeof(*map) * DST_MAX_CHANNELS); |
147 | 7.21k | } |
148 | 11.1k | return 0; |
149 | 11.6k | } |
150 | | |
151 | | static av_always_inline int get_sr_golomb_dst(GetBitContext *gb, unsigned int k) |
152 | 269k | { |
153 | 269k | int v = get_ur_golomb_jpegls(gb, k, get_bits_left(gb), 0); |
154 | 269k | if (v && get_bits1(gb)) |
155 | 30.7k | v = -v; |
156 | 269k | return v; |
157 | 269k | } |
158 | | |
159 | | static void read_uncoded_coeff(GetBitContext *gb, int *dst, unsigned int elements, |
160 | | int coeff_bits, int is_signed, int offset) |
161 | 21.7k | { |
162 | 21.7k | int i; |
163 | | |
164 | 177k | for (i = 0; i < elements; i++) { |
165 | 155k | dst[i] = (is_signed ? get_sbits(gb, coeff_bits) : get_bits(gb, coeff_bits)) + offset; |
166 | 155k | } |
167 | 21.7k | } |
168 | | |
169 | | 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 | 17.8k | { |
172 | 17.8k | unsigned int i, j, k; |
173 | 38.5k | for (i = 0; i < t->elements; i++) { |
174 | 22.3k | t->length[i] = get_bits(gb, length_bits) + 1; |
175 | 22.3k | if (!get_bits1(gb)) { |
176 | 13.4k | read_uncoded_coeff(gb, t->coeff[i], t->length[i], coeff_bits, is_signed, offset); |
177 | 13.4k | } else { |
178 | 8.94k | int method = get_bits(gb, 2), lsb_size; |
179 | 8.94k | if (method == 3) |
180 | 588 | return AVERROR_INVALIDDATA; |
181 | | |
182 | 8.35k | read_uncoded_coeff(gb, t->coeff[i], method + 1, coeff_bits, is_signed, offset); |
183 | | |
184 | 8.35k | lsb_size = get_bits(gb, 3); |
185 | 276k | for (j = method + 1; j < t->length[i]; j++) { |
186 | 269k | int c, x = 0; |
187 | 756k | for (k = 0; k < method + 1; k++) |
188 | 487k | x += code_pred_coeff[method][k] * (unsigned)t->coeff[i][j - k - 1]; |
189 | 269k | c = get_sr_golomb_dst(gb, lsb_size); |
190 | 269k | if (x >= 0) |
191 | 149k | c -= (x + 4) / 8; |
192 | 119k | else |
193 | 119k | c += (-x + 3) / 8; |
194 | 269k | if (!is_signed) { |
195 | 5.13k | if (c < offset || c >= offset + (1<<coeff_bits)) |
196 | 1.09k | return AVERROR_INVALIDDATA; |
197 | 5.13k | } |
198 | 268k | t->coeff[i][j] = c; |
199 | 268k | } |
200 | 8.35k | } |
201 | 22.3k | } |
202 | 16.1k | return 0; |
203 | 17.8k | } |
204 | | |
205 | | static void ac_init(ArithCoder *ac, GetBitContext *gb) |
206 | 7.09k | { |
207 | 7.09k | ac->a = 4095; |
208 | 7.09k | ac->c = get_bits(gb, 12); |
209 | 7.09k | } |
210 | | |
211 | | static av_always_inline void ac_get(ArithCoder *ac, GetBitContext *gb, int p, int *e) |
212 | 81.7M | { |
213 | 81.7M | unsigned int k = (ac->a >> 8) | ((ac->a >> 7) & 1); |
214 | 81.7M | unsigned int q = k * p; |
215 | 81.7M | unsigned int a_q = ac->a - q; |
216 | | |
217 | 81.7M | *e = ac->c < a_q; |
218 | 81.7M | if (*e) { |
219 | 77.0M | ac->a = a_q; |
220 | 77.0M | } else { |
221 | 4.65M | ac->a = q; |
222 | 4.65M | ac->c -= a_q; |
223 | 4.65M | } |
224 | | |
225 | 81.7M | if (ac->a < 2048) { |
226 | 11.2M | int n = 11 - av_log2(ac->a); |
227 | 11.2M | ac->a <<= n; |
228 | 11.2M | ac->c = (ac->c << n) | get_bits(gb, n); |
229 | 11.2M | } |
230 | 81.7M | } |
231 | | |
232 | | static uint8_t prob_dst_x_bit(int c) |
233 | 6.19k | { |
234 | 6.19k | return (ff_reverse[c & 127] >> 1) + 1; |
235 | 6.19k | } |
236 | | |
237 | | static int build_filter(int16_t table[DST_MAX_ELEMENTS][16][256], const Table *fsets) |
238 | 7.09k | { |
239 | 7.09k | int i, j, k, l; |
240 | | |
241 | 15.2k | for (i = 0; i < fsets->elements; i++) { |
242 | 9.09k | int length = fsets->length[i]; |
243 | | |
244 | 143k | for (j = 0; j < 16; j++) { |
245 | 135k | int total = av_clip(length - j * 8, 0, 8); |
246 | | |
247 | 34.6M | for (k = 0; k < 256; k++) { |
248 | 34.5M | int64_t v = 0; |
249 | | |
250 | 112M | for (l = 0; l < total; l++) |
251 | 78.0M | v += (((k >> l) & 1) * 2 - 1) * fsets->coeff[i][j * 8 + l]; |
252 | 34.5M | if ((int16_t)v != v) |
253 | 897 | return AVERROR_INVALIDDATA; |
254 | 34.5M | table[i][j][k] = v; |
255 | 34.5M | } |
256 | 135k | } |
257 | 9.09k | } |
258 | 6.19k | return 0; |
259 | 7.09k | } |
260 | | |
261 | | static int decode_frame(AVCodecContext *avctx, AVFrame *frame, |
262 | | int *got_frame_ptr, AVPacket *avpkt) |
263 | 128k | { |
264 | 128k | unsigned samples_per_frame = DST_SAMPLES_PER_FRAME(avctx->sample_rate); |
265 | 128k | unsigned map_ch_to_felem[DST_MAX_CHANNELS]; |
266 | 128k | unsigned map_ch_to_pelem[DST_MAX_CHANNELS]; |
267 | 128k | unsigned i, ch, same_map, dst_x_bit; |
268 | 128k | unsigned half_prob[DST_MAX_CHANNELS]; |
269 | 128k | const int channels = avctx->ch_layout.nb_channels; |
270 | 128k | DSTContext *s = avctx->priv_data; |
271 | 128k | GetBitContext *gb = &s->gb; |
272 | 128k | ArithCoder *ac = &s->ac; |
273 | 128k | uint8_t *dsd; |
274 | 128k | int ret; |
275 | | |
276 | 128k | if (avpkt->size <= 1) |
277 | 99.2k | return AVERROR_INVALIDDATA; |
278 | | |
279 | 29.1k | frame->nb_samples = samples_per_frame / 8; |
280 | 29.1k | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
281 | 11.4k | return ret; |
282 | 17.7k | dsd = frame->data[0]; |
283 | | |
284 | 17.7k | #if CONFIG_SWRESAMPLE && FF_API_DSD_PCM |
285 | 17.7k | if (avctx->sample_fmt != AV_SAMPLE_FMT_DSD) { |
286 | 17.7k | if (!s->swr && (ret = ff_dsd_to_pcm_init(avctx, &s->swr)) < 0) |
287 | 0 | return ret; |
288 | | |
289 | 17.7k | av_fast_malloc(&s->scratch, &s->scratch_size, |
290 | 17.7k | frame->nb_samples * channels); |
291 | 17.7k | if (!s->scratch) |
292 | 0 | return AVERROR(ENOMEM); |
293 | 17.7k | dsd = s->scratch; |
294 | 17.7k | } |
295 | 17.7k | #endif |
296 | | |
297 | 17.7k | if ((ret = init_get_bits8(gb, avpkt->data, avpkt->size)) < 0) |
298 | 0 | return ret; |
299 | | |
300 | 17.7k | if (!get_bits1(gb)) { |
301 | 6.92k | unsigned total = frame->nb_samples * channels; |
302 | 6.92k | unsigned n = FFMIN(avpkt->size - 1, total); |
303 | 6.92k | skip_bits1(gb); |
304 | 6.92k | if (get_bits(gb, 6)) |
305 | 6.09k | return AVERROR_INVALIDDATA; |
306 | | // pad short frames with silence |
307 | 825 | memcpy(dsd, avpkt->data + 1, n); |
308 | 825 | memset(dsd + n, 0x69, total - n); |
309 | 825 | goto done; |
310 | 6.92k | } |
311 | | |
312 | | /* Segmentation (10.4, 10.5, 10.6) */ |
313 | | |
314 | 10.8k | if (!get_bits1(gb)) { |
315 | 523 | avpriv_request_sample(avctx, "Not Same Segmentation"); |
316 | 523 | return AVERROR_PATCHWELCOME; |
317 | 523 | } |
318 | | |
319 | 10.3k | if (!get_bits1(gb)) { |
320 | 233 | avpriv_request_sample(avctx, "Not Same Segmentation For All Channels"); |
321 | 233 | return AVERROR_PATCHWELCOME; |
322 | 233 | } |
323 | | |
324 | 10.0k | if (!get_bits1(gb)) { |
325 | 473 | avpriv_request_sample(avctx, "Not End Of Channel Segmentation"); |
326 | 473 | return AVERROR_PATCHWELCOME; |
327 | 473 | } |
328 | | |
329 | | /* Mapping (10.7, 10.8, 10.9) */ |
330 | | |
331 | 9.59k | same_map = get_bits1(gb); |
332 | | |
333 | 9.59k | if ((ret = read_map(gb, &s->fsets, map_ch_to_felem, channels)) < 0) |
334 | 221 | return ret; |
335 | | |
336 | 9.37k | if (same_map) { |
337 | 7.32k | s->probs.elements = s->fsets.elements; |
338 | 7.32k | memcpy(map_ch_to_pelem, map_ch_to_felem, sizeof(map_ch_to_felem)); |
339 | 7.32k | } else { |
340 | 2.04k | avpriv_request_sample(avctx, "Not Same Mapping"); |
341 | 2.04k | if ((ret = read_map(gb, &s->probs, map_ch_to_pelem, channels)) < 0) |
342 | 224 | return ret; |
343 | 2.04k | } |
344 | | |
345 | | /* Half Probability (10.10) */ |
346 | | |
347 | 37.5k | for (ch = 0; ch < channels; ch++) |
348 | 28.3k | half_prob[ch] = get_bits1(gb); |
349 | | |
350 | | /* Filter Coef Sets (10.12) */ |
351 | | |
352 | 9.15k | ret = read_table(gb, &s->fsets, fsets_code_pred_coeff, 7, 9, 1, 0); |
353 | 9.15k | if (ret < 0) |
354 | 448 | return ret; |
355 | | |
356 | | /* Probability Tables (10.13) */ |
357 | | |
358 | 8.70k | ret = read_table(gb, &s->probs, probs_code_pred_coeff, 6, 7, 0, 1); |
359 | 8.70k | if (ret < 0) |
360 | 1.23k | return ret; |
361 | | |
362 | | /* Arithmetic Coded Data (10.11) */ |
363 | | |
364 | 7.47k | if (get_bits1(gb)) |
365 | 379 | return AVERROR_INVALIDDATA; |
366 | 7.09k | ac_init(ac, gb); |
367 | | |
368 | 7.09k | ret = build_filter(s->filter, &s->fsets); |
369 | 7.09k | if (ret < 0) |
370 | 897 | return ret; |
371 | | |
372 | 6.19k | memset(s->status, 0xAA, sizeof(s->status)); |
373 | 6.19k | memset(dsd, 0, frame->nb_samples * channels); |
374 | | |
375 | 6.19k | ac_get(ac, gb, prob_dst_x_bit(s->fsets.coeff[0][0]), &dst_x_bit); |
376 | | |
377 | 39.5M | for (i = 0; i < samples_per_frame; i++) { |
378 | 121M | for (ch = 0; ch < channels; ch++) { |
379 | 81.7M | const unsigned felem = map_ch_to_felem[ch]; |
380 | 81.7M | int16_t (*filter)[256] = s->filter[felem]; |
381 | 81.7M | uint8_t *status = s->status[ch]; |
382 | 81.7M | int prob, residual, v; |
383 | | |
384 | 1.30G | #define F(x) filter[(x)][status[(x)]] |
385 | 81.7M | const int16_t predict = F( 0) + F( 1) + F( 2) + F( 3) + |
386 | 81.7M | F( 4) + F( 5) + F( 6) + F( 7) + |
387 | 81.7M | F( 8) + F( 9) + F(10) + F(11) + |
388 | 81.7M | F(12) + F(13) + F(14) + F(15); |
389 | 81.7M | #undef F |
390 | | |
391 | 81.7M | if (!half_prob[ch] || i >= s->fsets.length[felem]) { |
392 | 81.3M | unsigned pelem = map_ch_to_pelem[ch]; |
393 | 81.3M | unsigned index = FFABS(predict) >> 3; |
394 | 81.3M | prob = s->probs.coeff[pelem][FFMIN(index, s->probs.length[pelem] - 1)]; |
395 | 81.3M | } else { |
396 | 431k | prob = 128; |
397 | 431k | } |
398 | | |
399 | 81.7M | ac_get(ac, gb, prob, &residual); |
400 | 81.7M | v = ((predict >> 15) ^ residual) & 1; |
401 | 81.7M | dsd[(i >> 3) * channels + ch] |= v << (7 - (i & 0x7 )); |
402 | | |
403 | 81.7M | AV_WL64A(status + 8, (AV_RL64A(status + 8) << 1) | ((AV_RL64A(status) >> 63) & 1)); |
404 | 81.7M | AV_WL64A(status, (AV_RL64A(status) << 1) | v); |
405 | 81.7M | } |
406 | 39.5M | } |
407 | | |
408 | 7.02k | done: |
409 | 7.02k | #if CONFIG_SWRESAMPLE && FF_API_DSD_PCM |
410 | 7.02k | if (s->swr) { |
411 | 7.02k | ret = swr_convert(s->swr, &frame->data[0], frame->nb_samples, |
412 | 7.02k | (const uint8_t *const []){ s->scratch }, |
413 | 7.02k | frame->nb_samples); |
414 | 7.02k | if (ret != frame->nb_samples) |
415 | 0 | return ret < 0 ? ret : AVERROR_BUG; |
416 | 7.02k | } |
417 | 7.02k | #endif |
418 | | |
419 | 7.02k | *got_frame_ptr = 1; |
420 | | |
421 | 7.02k | return avpkt->size; |
422 | 7.02k | } |
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 | | }; |