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.

239 lines
6.9KB

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