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.

237 lines
6.8KB

  1. /*
  2. * LucasArt VIMA decoder
  3. * Copyright (c) 2012 Paul B Mahol
  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. #include "libavutil/audioconvert.h"
  22. #include "avcodec.h"
  23. #include "get_bits.h"
  24. #include "adpcm_data.h"
  25. typedef struct {
  26. AVFrame frame;
  27. uint16_t predict_table[5786 * 2];
  28. } VimaContext;
  29. static const uint8_t size_table[] =
  30. {
  31. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  32. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  33. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  34. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  35. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  36. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
  37. };
  38. static const int8_t index_table1[] =
  39. {
  40. -1, 4, -1, 4
  41. };
  42. static const int8_t index_table2[] =
  43. {
  44. -1, -1, 2, 6, -1, -1, 2, 6
  45. };
  46. static const int8_t index_table3[] =
  47. {
  48. -1, -1, -1, -1, 1, 2, 4, 6,
  49. -1, -1, -1, -1, 1, 2, 4, 6
  50. };
  51. static const int8_t index_table4[] =
  52. {
  53. -1, -1, -1, -1, -1, -1, -1, -1,
  54. 1, 1, 1, 2, 2, 4, 5, 6,
  55. -1, -1, -1, -1, -1, -1, -1, -1,
  56. 1, 1, 1, 2, 2, 4, 5, 6
  57. };
  58. static const int8_t index_table5[] =
  59. {
  60. -1, -1, -1, -1, -1, -1, -1, -1,
  61. -1, -1, -1, -1, -1, -1, -1, -1,
  62. 1, 1, 1, 1, 1, 2, 2, 2,
  63. 2, 4, 4, 4, 5, 5, 6, 6,
  64. -1, -1, -1, -1, -1, -1, -1, -1,
  65. -1, -1, -1, -1, -1, -1, -1, -1,
  66. 1, 1, 1, 1, 1, 2, 2, 2,
  67. 2, 4, 4, 4, 5, 5, 6, 6
  68. };
  69. static const int8_t index_table6[] =
  70. {
  71. -1, -1, -1, -1, -1, -1, -1, -1,
  72. -1, -1, -1, -1, -1, -1, -1, -1,
  73. -1, -1, -1, -1, -1, -1, -1, -1,
  74. -1, -1, -1, -1, -1, -1, -1, -1,
  75. 1, 1, 1, 1, 1, 1, 1, 1,
  76. 1, 1, 2, 2, 2, 2, 2, 2,
  77. 2, 2, 4, 4, 4, 4, 4, 4,
  78. 5, 5, 5, 5, 6, 6, 6, 6,
  79. -1, -1, -1, -1, -1, -1, -1, -1,
  80. -1, -1, -1, -1, -1, -1, -1, -1,
  81. -1, -1, -1, -1, -1, -1, -1, -1,
  82. -1, -1, -1, -1, -1, -1, -1, -1,
  83. 1, 1, 1, 1, 1, 1, 1, 1,
  84. 1, 1, 2, 2, 2, 2, 2, 2,
  85. 2, 2, 4, 4, 4, 4, 4, 4,
  86. 5, 5, 5, 5, 6, 6, 6, 6
  87. };
  88. static const int8_t* const step_index_tables[] =
  89. {
  90. index_table1, index_table2, index_table3,
  91. index_table4, index_table5, index_table6
  92. };
  93. static av_cold int decode_init(AVCodecContext *avctx)
  94. {
  95. VimaContext *vima = avctx->priv_data;
  96. int start_pos;
  97. for (start_pos = 0; start_pos < 64; start_pos++) {
  98. unsigned int dest_pos, table_pos;
  99. for (table_pos = 0, dest_pos = start_pos;
  100. table_pos < FF_ARRAY_ELEMS(ff_adpcm_step_table);
  101. table_pos++, dest_pos += 64) {
  102. int put = 0, count, table_value;
  103. table_value = ff_adpcm_step_table[table_pos];
  104. for (count = 32; count != 0; count >>= 1) {
  105. if (start_pos & count)
  106. put += table_value;
  107. table_value >>= 1;
  108. }
  109. vima->predict_table[dest_pos] = put;
  110. }
  111. }
  112. avcodec_get_frame_defaults(&vima->frame);
  113. avctx->coded_frame = &vima->frame;
  114. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  115. return 0;
  116. }
  117. static int decode_frame(AVCodecContext *avctx, void *data,
  118. int *got_frame_ptr, AVPacket *pkt)
  119. {
  120. GetBitContext gb;
  121. VimaContext *vima = avctx->priv_data;
  122. int16_t pcm_data[2];
  123. uint32_t samples;
  124. int8_t channel_hint[2];
  125. int ret, chan, channels = 1;
  126. init_get_bits(&gb, pkt->data, pkt->size * 8);
  127. if (pkt->size < 13)
  128. return AVERROR_INVALIDDATA;
  129. samples = get_bits_long(&gb, 32);
  130. if (samples == 0xffffffff) {
  131. skip_bits_long(&gb, 32);
  132. samples = get_bits_long(&gb, 32);
  133. }
  134. if (samples > pkt->size * 2)
  135. return AVERROR_INVALIDDATA;
  136. channel_hint[0] = get_sbits(&gb, 8);
  137. if (channel_hint[0] & 0x80) {
  138. channel_hint[0] = ~channel_hint[0];
  139. channels = 2;
  140. }
  141. avctx->channels = channels;
  142. avctx->channel_layout = (channels == 2) ? AV_CH_LAYOUT_STEREO :
  143. AV_CH_LAYOUT_MONO;
  144. pcm_data[0] = get_sbits(&gb, 16);
  145. if (channels > 1) {
  146. channel_hint[1] = get_sbits(&gb, 8);
  147. pcm_data[1] = get_sbits(&gb, 16);
  148. }
  149. vima->frame.nb_samples = samples;
  150. if ((ret = avctx->get_buffer(avctx, &vima->frame)) < 0) {
  151. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  152. return ret;
  153. }
  154. for (chan = 0; chan < channels; chan++) {
  155. uint16_t *dest = (uint16_t*)vima->frame.data[0] + chan;
  156. int step_index = channel_hint[chan];
  157. int output = pcm_data[chan];
  158. int sample;
  159. for (sample = 0; sample < samples; sample++) {
  160. int lookup_size, lookup, highbit, lowbits;
  161. step_index = av_clip(step_index, 0, 88);
  162. lookup_size = size_table[step_index];
  163. lookup = get_bits(&gb, lookup_size);
  164. highbit = 1 << (lookup_size - 1);
  165. lowbits = highbit - 1;
  166. if (lookup & highbit)
  167. lookup ^= highbit;
  168. else
  169. highbit = 0;
  170. if (lookup == lowbits) {
  171. output = get_sbits(&gb, 16);
  172. } else {
  173. int predict_index, diff;
  174. predict_index = (lookup << (7 - lookup_size)) | (step_index << 6);
  175. predict_index = av_clip(predict_index, 0, 5785);
  176. diff = vima->predict_table[predict_index];
  177. if (lookup)
  178. diff += ff_adpcm_step_table[step_index] >> (lookup_size - 1);
  179. if (highbit)
  180. diff = -diff;
  181. output = av_clip_int16(output + diff);
  182. }
  183. *dest = output;
  184. dest += channels;
  185. step_index += step_index_tables[lookup_size - 2][lookup];
  186. }
  187. }
  188. *got_frame_ptr = 1;
  189. *(AVFrame *)data = vima->frame;
  190. return pkt->size;
  191. }
  192. AVCodec ff_vima_decoder = {
  193. .name = "vima",
  194. .type = AVMEDIA_TYPE_AUDIO,
  195. .id = AV_CODEC_ID_VIMA,
  196. .priv_data_size = sizeof(VimaContext),
  197. .init = decode_init,
  198. .decode = decode_frame,
  199. .capabilities = CODEC_CAP_DR1,
  200. .long_name = NULL_IF_CONFIG_SMALL("LucasArts VIMA audio"),
  201. };