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.

203 lines
5.9KB

  1. /*
  2. * MOFLEX Fast Audio decoder
  3. * Copyright (c) 2015-2016 Florian Nouwt
  4. * Copyright (c) 2017 Adib Surani
  5. * Copyright (c) 2020 Paul B Mahol
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "libavutil/intreadwrite.h"
  24. #include "avcodec.h"
  25. #include "bytestream.h"
  26. #include "internal.h"
  27. #include "mathops.h"
  28. typedef struct ChannelItems {
  29. float f[8];
  30. float last;
  31. } ChannelItems;
  32. typedef struct FastAudioContext {
  33. float table[8][64];
  34. ChannelItems *ch;
  35. } FastAudioContext;
  36. static av_cold int fastaudio_init(AVCodecContext *avctx)
  37. {
  38. FastAudioContext *s = avctx->priv_data;
  39. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  40. for (int i = 0; i < 8; i++)
  41. s->table[0][i] = (i - 159.5f) / 160.f;
  42. for (int i = 0; i < 11; i++)
  43. s->table[0][i + 8] = (i - 37.5f) / 40.f;
  44. for (int i = 0; i < 27; i++)
  45. s->table[0][i + 8 + 11] = (i - 13.f) / 20.f;
  46. for (int i = 0; i < 11; i++)
  47. s->table[0][i + 8 + 11 + 27] = (i + 27.5f) / 40.f;
  48. for (int i = 0; i < 7; i++)
  49. s->table[0][i + 8 + 11 + 27 + 11] = (i + 152.5f) / 160.f;
  50. memcpy(s->table[1], s->table[0], sizeof(s->table[0]));
  51. for (int i = 0; i < 7; i++)
  52. s->table[2][i] = (i - 33.5f) / 40.f;
  53. for (int i = 0; i < 25; i++)
  54. s->table[2][i + 7] = (i - 13.f) / 20.f;
  55. for (int i = 0; i < 32; i++)
  56. s->table[3][i] = -s->table[2][31 - i];
  57. for (int i = 0; i < 16; i++)
  58. s->table[4][i] = i * 0.22f / 3.f - 0.6f;
  59. for (int i = 0; i < 16; i++)
  60. s->table[5][i] = i * 0.20f / 3.f - 0.3f;
  61. for (int i = 0; i < 8; i++)
  62. s->table[6][i] = i * 0.36f / 3.f - 0.4f;
  63. for (int i = 0; i < 8; i++)
  64. s->table[7][i] = i * 0.34f / 3.f - 0.2f;
  65. s->ch = av_calloc(avctx->channels, sizeof(*s->ch));
  66. if (!s->ch)
  67. return AVERROR(ENOMEM);
  68. return 0;
  69. }
  70. static int read_bits(int bits, int *ppos, unsigned *src)
  71. {
  72. int r, pos;
  73. pos = *ppos;
  74. pos += bits;
  75. r = src[(pos - 1) / 32] >> ((-pos) & 31);
  76. *ppos = pos;
  77. return r & ((1 << bits) - 1);
  78. }
  79. static const uint8_t bits[8] = { 6, 6, 5, 5, 4, 0, 3, 3, };
  80. static void set_sample(int i, int j, int v, float *result, int *pads, float value)
  81. {
  82. result[i * 64 + pads[i] + j * 3] = value * (2 * v - 7);
  83. }
  84. static int fastaudio_decode(AVCodecContext *avctx, void *data,
  85. int *got_frame, AVPacket *pkt)
  86. {
  87. FastAudioContext *s = avctx->priv_data;
  88. GetByteContext gb;
  89. AVFrame *frame = data;
  90. int subframes;
  91. int ret;
  92. subframes = pkt->size / (40 * avctx->channels);
  93. frame->nb_samples = subframes * 256;
  94. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  95. return ret;
  96. bytestream2_init(&gb, pkt->data, pkt->size);
  97. for (int subframe = 0; subframe < subframes; subframe++) {
  98. for (int channel = 0; channel < avctx->channels; channel++) {
  99. ChannelItems *ch = &s->ch[channel];
  100. float result[256] = { 0 };
  101. unsigned src[10];
  102. int inds[4], pads[4];
  103. float m[8];
  104. int pos = 0;
  105. for (int i = 0; i < 10; i++)
  106. src[i] = bytestream2_get_le32(&gb);
  107. for (int i = 0; i < 8; i++)
  108. m[7 - i] = s->table[i][read_bits(bits[i], &pos, src)];
  109. for (int i = 0; i < 4; i++)
  110. inds[3 - i] = read_bits(6, &pos, src);
  111. for (int i = 0; i < 4; i++)
  112. pads[3 - i] = read_bits(2, &pos, src);
  113. for (int i = 0, index5 = 0; i < 4; i++) {
  114. float value = av_int2float((inds[i] + 1) << 20) * powf(2.f, 116.f);
  115. for (int j = 0, tmp = 0; j < 21; j++) {
  116. set_sample(i, j, j == 20 ? tmp / 2 : read_bits(3, &pos, src), result, pads, value);
  117. if (j % 10 == 9)
  118. tmp = 4 * tmp + read_bits(2, &pos, src);
  119. if (j == 20)
  120. index5 = FFMIN(2 * index5 + tmp % 2, 63);
  121. }
  122. m[2] = s->table[5][index5];
  123. }
  124. for (int i = 0; i < 256; i++) {
  125. float x = result[i];
  126. for (int j = 0; j < 8; j++) {
  127. x -= m[j] * ch->f[j];
  128. ch->f[j] += m[j] * x;
  129. }
  130. memmove(&ch->f[0], &ch->f[1], sizeof(float) * 7);
  131. ch->f[7] = x;
  132. ch->last = x + ch->last * 0.86f;
  133. result[i] = ch->last * 2.f;
  134. }
  135. memcpy(frame->extended_data[channel] + 1024 * subframe, result, 256 * sizeof(float));
  136. }
  137. }
  138. *got_frame = 1;
  139. return pkt->size;
  140. }
  141. static av_cold int fastaudio_close(AVCodecContext *avctx)
  142. {
  143. FastAudioContext *s = avctx->priv_data;
  144. av_freep(&s->ch);
  145. return 0;
  146. }
  147. AVCodec ff_fastaudio_decoder = {
  148. .name = "fastaudio",
  149. .long_name = NULL_IF_CONFIG_SMALL("MobiClip FastAudio"),
  150. .type = AVMEDIA_TYPE_AUDIO,
  151. .id = AV_CODEC_ID_FASTAUDIO,
  152. .priv_data_size = sizeof(FastAudioContext),
  153. .init = fastaudio_init,
  154. .decode = fastaudio_decode,
  155. .close = fastaudio_close,
  156. .capabilities = AV_CODEC_CAP_DR1,
  157. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
  158. AV_SAMPLE_FMT_NONE },
  159. };