You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

390 lines
11KB

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