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.

219 lines
6.7KB

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