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.

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