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