FFmpeg
Loading...
Searching...
No Matches
dstdec.c
Go to the documentation of this file.
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
31#include "libavutil/mem.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
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
52static const int8_t fsets_code_pred_coeff[3][3] = {
53 { -8 },
54 { -16, 8 },
55 { -9, -5, 6 },
56};
57
58static const int8_t probs_code_pred_coeff[3][3] = {
59 { -8 },
60 { -16, 8 },
61 { -24, 24, -8 },
62};
63
64typedef struct ArithCoder {
65 unsigned int a;
66 unsigned int c;
68
69typedef struct Table {
70 unsigned int elements;
71 unsigned int length[DST_MAX_ELEMENTS];
73} Table;
74
75typedef struct DSTContext {
76 AVClass *class;
77
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
89
91{
93 avpriv_request_sample(avctx, "Channel count %d", avctx->ch_layout.nb_channels);
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 if (avctx->sample_rate > 512 * 44100)
100 return AVERROR_INVALIDDATA;
101
102
103 if (DST_SAMPLES_PER_FRAME(avctx->sample_rate) & 7) {
105 }
106
108
109#if CONFIG_SWRESAMPLE && FF_API_DSD_PCM
112#endif
113
114 return 0;
115}
116
118{
119#if CONFIG_SWRESAMPLE && FF_API_DSD_PCM
120 DSTContext *s = avctx->priv_data;
121
122 swr_free(&s->swr);
123 av_freep(&s->scratch);
124#endif
125 return 0;
126}
127
128static int read_map(GetBitContext *gb, Table *t, unsigned int map[DST_MAX_CHANNELS], int channels)
129{
130 int ch;
131 t->elements = 1;
132 map[0] = 0;
133 if (!get_bits1(gb)) {
134 for (ch = 1; ch < channels; ch++) {
135 int bits = av_log2(t->elements) + 1;
136 map[ch] = get_bits(gb, bits);
137 if (map[ch] == t->elements) {
138 t->elements++;
139 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 return 0;
149}
150
151static 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
159static 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 for (i = 0; i < elements; i++) {
165 dst[i] = (is_signed ? get_sbits(gb, coeff_bits) : get_bits(gb, coeff_bits)) + offset;
166 }
167}
168
169static 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 for (i = 0; i < t->elements; i++) {
174 t->length[i] = get_bits(gb, length_bits) + 1;
175 if (!get_bits1(gb)) {
176 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 return 0;
203}
204
205static void ac_init(ArithCoder *ac, GetBitContext *gb)
206{
207 ac->a = 4095;
208 ac->c = get_bits(gb, 12);
209}
210
211static av_always_inline void ac_get(ArithCoder *ac, GetBitContext *gb, int p, int *e)
212{
213 unsigned int k = (ac->a >> 8) | ((ac->a >> 7) & 1);
214 unsigned int q = k * p;
215 unsigned int a_q = ac->a - q;
216
217 *e = ac->c < a_q;
218 if (*e) {
219 ac->a = a_q;
220 } else {
221 ac->a = q;
222 ac->c -= a_q;
223 }
224
225 if (ac->a < 2048) {
226 int n = 11 - av_log2(ac->a);
227 ac->a <<= n;
228 ac->c = (ac->c << n) | get_bits(gb, n);
229 }
230}
231
232static uint8_t prob_dst_x_bit(int c)
233{
234 return (ff_reverse[c & 127] >> 1) + 1;
235}
236
237static int build_filter(int16_t table[DST_MAX_ELEMENTS][16][256], const Table *fsets)
238{
239 int i, j, k, l;
240
241 for (i = 0; i < fsets->elements; i++) {
242 int length = fsets->length[i];
243
244 for (j = 0; j < 16; j++) {
245 int total = av_clip(length - j * 8, 0, 8);
246
247 for (k = 0; k < 256; k++) {
248 int64_t v = 0;
249
250 for (l = 0; l < total; l++)
251 v += (((k >> l) & 1) * 2 - 1) * fsets->coeff[i][j * 8 + l];
252 if ((int16_t)v != v)
253 return AVERROR_INVALIDDATA;
254 table[i][j][k] = v;
255 }
256 }
257 }
258 return 0;
259}
260
262 int *got_frame_ptr, AVPacket *avpkt)
263{
264 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 const int channels = avctx->ch_layout.nb_channels;
270 DSTContext *s = avctx->priv_data;
271 GetBitContext *gb = &s->gb;
272 ArithCoder *ac = &s->ac;
273 uint8_t *dsd;
274 int ret;
275
276 if (avpkt->size <= 1)
277 return AVERROR_INVALIDDATA;
278
279 frame->nb_samples = samples_per_frame / 8;
280 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
281 return ret;
282 dsd = frame->data[0];
283
284#if CONFIG_SWRESAMPLE && FF_API_DSD_PCM
285 if (avctx->sample_fmt != AV_SAMPLE_FMT_DSD) {
286 if (!s->swr && (ret = ff_dsd_to_pcm_init(avctx, &s->swr)) < 0)
287 return ret;
288
289 av_fast_malloc(&s->scratch, &s->scratch_size,
290 frame->nb_samples * channels);
291 if (!s->scratch)
292 return AVERROR(ENOMEM);
293 dsd = s->scratch;
294 }
295#endif
296
297 if ((ret = init_get_bits8(gb, avpkt->data, avpkt->size)) < 0)
298 return ret;
299
300 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 if (!get_bits1(gb)) {
315 avpriv_request_sample(avctx, "Not Same Segmentation");
317 }
318
319 if (!get_bits1(gb)) {
320 avpriv_request_sample(avctx, "Not Same Segmentation For All Channels");
322 }
323
324 if (!get_bits1(gb)) {
325 avpriv_request_sample(avctx, "Not End Of Channel Segmentation");
327 }
328
329 /* Mapping (10.7, 10.8, 10.9) */
330
331 same_map = get_bits1(gb);
332
333 if ((ret = read_map(gb, &s->fsets, map_ch_to_felem, channels)) < 0)
334 return ret;
335
336 if (same_map) {
337 s->probs.elements = s->fsets.elements;
338 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 for (ch = 0; ch < channels; ch++)
348 half_prob[ch] = get_bits1(gb);
349
350 /* Filter Coef Sets (10.12) */
351
352 ret = read_table(gb, &s->fsets, fsets_code_pred_coeff, 7, 9, 1, 0);
353 if (ret < 0)
354 return ret;
355
356 /* Probability Tables (10.13) */
357
358 ret = read_table(gb, &s->probs, probs_code_pred_coeff, 6, 7, 0, 1);
359 if (ret < 0)
360 return ret;
361
362 /* Arithmetic Coded Data (10.11) */
363
364 if (get_bits1(gb))
365 return AVERROR_INVALIDDATA;
366 ac_init(ac, gb);
367
368 ret = build_filter(s->filter, &s->fsets);
369 if (ret < 0)
370 return ret;
371
372 memset(s->status, 0xAA, sizeof(s->status));
373 memset(dsd, 0, frame->nb_samples * channels);
374
375 ac_get(ac, gb, prob_dst_x_bit(s->fsets.coeff[0][0]), &dst_x_bit);
376
377 for (i = 0; i < samples_per_frame; i++) {
378 for (ch = 0; ch < channels; ch++) {
379 const unsigned felem = map_ch_to_felem[ch];
380 int16_t (*filter)[256] = s->filter[felem];
381 uint8_t *status = s->status[ch];
382 int prob, residual, v;
383
384#define F(x) filter[(x)][status[(x)]]
385 const int16_t predict = F( 0) + F( 1) + F( 2) + F( 3) +
386 F( 4) + F( 5) + F( 6) + F( 7) +
387 F( 8) + F( 9) + F(10) + F(11) +
388 F(12) + F(13) + F(14) + F(15);
389#undef F
390
391 if (!half_prob[ch] || i >= s->fsets.length[felem]) {
392 unsigned pelem = map_ch_to_pelem[ch];
393 unsigned index = FFABS(predict) >> 3;
394 prob = s->probs.coeff[pelem][FFMIN(index, s->probs.length[pelem] - 1)];
395 } else {
396 prob = 128;
397 }
398
399 ac_get(ac, gb, prob, &residual);
400 v = ((predict >> 15) ^ residual) & 1;
401 dsd[(i >> 3) * channels + ch] |= v << (7 - (i & 0x7 ));
402
403 AV_WL64A(status + 8, (AV_RL64A(status + 8) << 1) | ((AV_RL64A(status) >> 63) & 1));
404 AV_WL64A(status, (AV_RL64A(status) << 1) | v);
405 }
406 }
407
408done:
409#if CONFIG_SWRESAMPLE && FF_API_DSD_PCM
410 if (s->swr) {
411 ret = swr_convert(s->swr, &frame->data[0], frame->nb_samples,
412 (const uint8_t *const []){ s->scratch },
413 frame->nb_samples);
414 if (ret != frame->nb_samples)
415 return ret < 0 ? ret : AVERROR_BUG;
416 }
417#endif
418
419 *got_frame_ptr = 1;
420
421 return avpkt->size;
422}
423
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,
433 .p.capabilities = AV_CODEC_CAP_DR1,
434};
static av_always_inline void predict(PredictorState *ps, int *coef, int output_enable)
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
const FFCodec ff_dst_decoder
Definition dstdec.c:424
channels
Definition aptx.h:31
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define prob(name, subs,...)
Definition cbs_vp9.c:252
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define av_clip
Definition common.h:100
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
long long int64_t
Definition coverity.c:34
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVFrame * frame
int ff_dsd_to_pcm_init(struct AVCodecContext *avctx, struct SwrContext **swrp)
(Re)create a libswresample context converting AV_SAMPLE_FMT_DSD to avctx->sample_fmt at the same samp...
static void ac_init(ArithCoder *ac, GetBitContext *gb)
Definition dstdec.c:205
static av_always_inline void ac_get(ArithCoder *ac, GetBitContext *gb, int p, int *e)
Definition dstdec.c:211
#define F(x)
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition dstdec.c:261
static uint8_t prob_dst_x_bit(int c)
Definition dstdec.c:232
static av_cold int decode_close(AVCodecContext *avctx)
Definition dstdec.c:117
static const int8_t fsets_code_pred_coeff[3][3]
Definition dstdec.c:52
static av_cold int decode_init(AVCodecContext *avctx)
Definition dstdec.c:90
#define DST_MAX_ELEMENTS
Definition dstdec.c:46
static int build_filter(int16_t table[DST_MAX_ELEMENTS][16][256], const Table *fsets)
Definition dstdec.c:237
static void read_uncoded_coeff(GetBitContext *gb, int *dst, unsigned int elements, int coeff_bits, int is_signed, int offset)
Definition dstdec.c:159
static av_always_inline int get_sr_golomb_dst(GetBitContext *gb, unsigned int k)
Definition dstdec.c:151
static int 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)
Definition dstdec.c:169
static const int8_t probs_code_pred_coeff[3][3]
Definition dstdec.c:58
#define DST_SAMPLES_PER_FRAME(sample_rate)
Definition dstdec.c:50
#define DST_MAX_CHANNELS
Definition dstdec.c:45
static int read_map(GetBitContext *gb, Table *t, unsigned int map[DST_MAX_CHANNELS], int channels)
Definition dstdec.c:128
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static const uint8_t bits[8]
Definition fastaudio.c:100
bitstream reader API header.
static int get_sbits(GetBitContext *s, int n)
Definition get_bits.h:322
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static void skip_bits1(GetBitContext *s)
Definition get_bits.h:416
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
exp golomb vlc stuff
static int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len)
read unsigned golomb rice code (jpegls).
Definition golomb.h:431
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_DST
Definition codec_id.h:534
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition mem.c:555
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_FLT
float
Definition samplefmt.h:60
@ AV_SAMPLE_FMT_DSD
DSD (Direct Stream Digital) bitstream, interleaved.
Definition samplefmt.h:77
av_cold void swr_free(SwrContext **ss)
Free the given SwrContext and set the pointer to NULL.
Definition swresample.c:137
int attribute_align_arg swr_convert(struct SwrContext *s, uint8_t *const *out_arg, int out_count, const uint8_t *const *in_arg, int in_count)
Convert audio.
Definition swresample.c:743
int index
Definition gxfenc.c:90
const VDPAUPixFmtMap * map
#define av_log2
Definition intmath.h:84
#define AV_WL64A(p, v)
#define AV_RL64A(p)
unsigned offset
Definition libaomenc.c:763
static av_cold int decode_init(AVCodecContext *avctx)
Definition 4xm.c:998
static int decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
Definition 4xm.c:837
static av_cold int decode_close(AVCodecContext *avctx)
Definition aacdec.c:1220
#define av_always_inline
Definition attributes.h:72
#define av_cold
Definition attributes.h:117
const uint8_t ff_reverse[256]
Definition reverse.c:23
#define FFMIN(a, b)
Definition macros.h:49
Memory handling functions.
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
static const uint16_t table[]
Definition prosumer.c:203
static const ElemCat * elements[ELEMENT_COUNT]
Definition signature.h:565
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
enum AVSampleFormat request_sample_fmt
desired sample format
Definition avcodec.h:1097
int sample_rate
samples per second
Definition avcodec.h:1040
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
unsigned int a
Definition dstdec.c:65
unsigned int c
Definition dstdec.c:66
uint8_t status[DST_MAX_CHANNELS][16]
Definition dstdec.c:81
int16_t filter[DST_MAX_ELEMENTS][16][256]
Definition dstdec.c:82
Table fsets
Definition dstdec.c:80
Table probs
Definition dstdec.c:80
GetBitContext gb
Definition dstdec.c:78
ArithCoder ac
Definition dstdec.c:79
The libswresample context.
Definition dstdec.c:69
unsigned int elements
Definition dstdec.c:70
int coeff[DST_MAX_ELEMENTS][128]
Definition dstdec.c:72
unsigned int length[DST_MAX_ELEMENTS]
Definition dstdec.c:71
libswresample public header
#define avpriv_request_sample(...)
#define av_freep(p)
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
Definition h263dsp.c:29
static double c[64]