Libav
|
00001 /* 00002 * TTA (The Lossless True Audio) decoder 00003 * Copyright (c) 2006 Alex Beregszaszi 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00030 #define ALT_BITSTREAM_READER_LE 00031 //#define DEBUG 00032 #include <limits.h> 00033 #include "avcodec.h" 00034 #include "get_bits.h" 00035 00036 #define FORMAT_INT 1 00037 #define FORMAT_FLOAT 3 00038 00039 typedef struct TTAContext { 00040 AVCodecContext *avctx; 00041 GetBitContext gb; 00042 00043 int flags, channels, bps, is_float, data_length; 00044 int frame_length, last_frame_length, total_frames; 00045 00046 int32_t *decode_buffer; 00047 } TTAContext; 00048 00049 #if 0 00050 static inline int shift_1(int i) 00051 { 00052 if (i < 32) 00053 return 1 << i; 00054 else 00055 return 0x80000000; // 16 << 31 00056 } 00057 00058 static inline int shift_16(int i) 00059 { 00060 if (i < 28) 00061 return 16 << i; 00062 else 00063 return 0x80000000; // 16 << 27 00064 } 00065 #else 00066 static const uint32_t shift_1[] = { 00067 0x00000001, 0x00000002, 0x00000004, 0x00000008, 00068 0x00000010, 0x00000020, 0x00000040, 0x00000080, 00069 0x00000100, 0x00000200, 0x00000400, 0x00000800, 00070 0x00001000, 0x00002000, 0x00004000, 0x00008000, 00071 0x00010000, 0x00020000, 0x00040000, 0x00080000, 00072 0x00100000, 0x00200000, 0x00400000, 0x00800000, 00073 0x01000000, 0x02000000, 0x04000000, 0x08000000, 00074 0x10000000, 0x20000000, 0x40000000, 0x80000000, 00075 0x80000000, 0x80000000, 0x80000000, 0x80000000, 00076 0x80000000, 0x80000000, 0x80000000, 0x80000000 00077 }; 00078 00079 static const uint32_t * const shift_16 = shift_1 + 4; 00080 #endif 00081 00082 #define MAX_ORDER 16 00083 typedef struct TTAFilter { 00084 int32_t shift, round, error, mode; 00085 int32_t qm[MAX_ORDER]; 00086 int32_t dx[MAX_ORDER]; 00087 int32_t dl[MAX_ORDER]; 00088 } TTAFilter; 00089 00090 static const int32_t ttafilter_configs[4][2] = { 00091 {10, 1}, 00092 {9, 1}, 00093 {10, 1}, 00094 {12, 0} 00095 }; 00096 00097 static void ttafilter_init(TTAFilter *c, int32_t shift, int32_t mode) { 00098 memset(c, 0, sizeof(TTAFilter)); 00099 c->shift = shift; 00100 c->round = shift_1[shift-1]; 00101 // c->round = 1 << (shift - 1); 00102 c->mode = mode; 00103 } 00104 00105 // FIXME: copy paste from original 00106 static inline void memshl(register int32_t *a, register int32_t *b) { 00107 *a++ = *b++; 00108 *a++ = *b++; 00109 *a++ = *b++; 00110 *a++ = *b++; 00111 *a++ = *b++; 00112 *a++ = *b++; 00113 *a++ = *b++; 00114 *a = *b; 00115 } 00116 00117 // FIXME: copy paste from original 00118 // mode=1 encoder, mode=0 decoder 00119 static inline void ttafilter_process(TTAFilter *c, int32_t *in, int32_t mode) { 00120 register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round; 00121 00122 if (!c->error) { 00123 sum += *dl++ * *qm, qm++; 00124 sum += *dl++ * *qm, qm++; 00125 sum += *dl++ * *qm, qm++; 00126 sum += *dl++ * *qm, qm++; 00127 sum += *dl++ * *qm, qm++; 00128 sum += *dl++ * *qm, qm++; 00129 sum += *dl++ * *qm, qm++; 00130 sum += *dl++ * *qm, qm++; 00131 dx += 8; 00132 } else if(c->error < 0) { 00133 sum += *dl++ * (*qm -= *dx++), qm++; 00134 sum += *dl++ * (*qm -= *dx++), qm++; 00135 sum += *dl++ * (*qm -= *dx++), qm++; 00136 sum += *dl++ * (*qm -= *dx++), qm++; 00137 sum += *dl++ * (*qm -= *dx++), qm++; 00138 sum += *dl++ * (*qm -= *dx++), qm++; 00139 sum += *dl++ * (*qm -= *dx++), qm++; 00140 sum += *dl++ * (*qm -= *dx++), qm++; 00141 } else { 00142 sum += *dl++ * (*qm += *dx++), qm++; 00143 sum += *dl++ * (*qm += *dx++), qm++; 00144 sum += *dl++ * (*qm += *dx++), qm++; 00145 sum += *dl++ * (*qm += *dx++), qm++; 00146 sum += *dl++ * (*qm += *dx++), qm++; 00147 sum += *dl++ * (*qm += *dx++), qm++; 00148 sum += *dl++ * (*qm += *dx++), qm++; 00149 sum += *dl++ * (*qm += *dx++), qm++; 00150 } 00151 00152 *(dx-0) = ((*(dl-1) >> 30) | 1) << 2; 00153 *(dx-1) = ((*(dl-2) >> 30) | 1) << 1; 00154 *(dx-2) = ((*(dl-3) >> 30) | 1) << 1; 00155 *(dx-3) = ((*(dl-4) >> 30) | 1); 00156 00157 // compress 00158 if (mode) { 00159 *dl = *in; 00160 *in -= (sum >> c->shift); 00161 c->error = *in; 00162 } else { 00163 c->error = *in; 00164 *in += (sum >> c->shift); 00165 *dl = *in; 00166 } 00167 00168 if (c->mode) { 00169 *(dl-1) = *dl - *(dl-1); 00170 *(dl-2) = *(dl-1) - *(dl-2); 00171 *(dl-3) = *(dl-2) - *(dl-3); 00172 } 00173 00174 memshl(c->dl, c->dl + 1); 00175 memshl(c->dx, c->dx + 1); 00176 } 00177 00178 typedef struct TTARice { 00179 uint32_t k0, k1, sum0, sum1; 00180 } TTARice; 00181 00182 static void rice_init(TTARice *c, uint32_t k0, uint32_t k1) 00183 { 00184 c->k0 = k0; 00185 c->k1 = k1; 00186 c->sum0 = shift_16[k0]; 00187 c->sum1 = shift_16[k1]; 00188 } 00189 00190 static int tta_get_unary(GetBitContext *gb) 00191 { 00192 int ret = 0; 00193 00194 // count ones 00195 while(get_bits1(gb)) 00196 ret++; 00197 return ret; 00198 } 00199 00200 static av_cold int tta_decode_init(AVCodecContext * avctx) 00201 { 00202 TTAContext *s = avctx->priv_data; 00203 int i; 00204 00205 s->avctx = avctx; 00206 00207 // 30bytes includes a seektable with one frame 00208 if (avctx->extradata_size < 30) 00209 return -1; 00210 00211 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size); 00212 if (show_bits_long(&s->gb, 32) == AV_RL32("TTA1")) 00213 { 00214 /* signature */ 00215 skip_bits(&s->gb, 32); 00216 // if (get_bits_long(&s->gb, 32) != bswap_32(AV_RL32("TTA1"))) { 00217 // av_log(s->avctx, AV_LOG_ERROR, "Missing magic\n"); 00218 // return -1; 00219 // } 00220 00221 s->flags = get_bits(&s->gb, 16); 00222 if (s->flags != 1 && s->flags != 3) 00223 { 00224 av_log(s->avctx, AV_LOG_ERROR, "Invalid flags\n"); 00225 return -1; 00226 } 00227 s->is_float = (s->flags == FORMAT_FLOAT); 00228 avctx->channels = s->channels = get_bits(&s->gb, 16); 00229 avctx->bits_per_coded_sample = get_bits(&s->gb, 16); 00230 s->bps = (avctx->bits_per_coded_sample + 7) / 8; 00231 avctx->sample_rate = get_bits_long(&s->gb, 32); 00232 if(avctx->sample_rate > 1000000){ //prevent FRAME_TIME * avctx->sample_rate from overflowing and sanity check 00233 av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n"); 00234 return -1; 00235 } 00236 s->data_length = get_bits_long(&s->gb, 32); 00237 skip_bits(&s->gb, 32); // CRC32 of header 00238 00239 if (s->is_float) 00240 { 00241 avctx->sample_fmt = SAMPLE_FMT_FLT; 00242 av_log(s->avctx, AV_LOG_ERROR, "Unsupported sample format. Please contact the developers.\n"); 00243 return -1; 00244 } 00245 else switch(s->bps) { 00246 // case 1: avctx->sample_fmt = SAMPLE_FMT_U8; break; 00247 case 2: avctx->sample_fmt = SAMPLE_FMT_S16; break; 00248 // case 3: avctx->sample_fmt = SAMPLE_FMT_S24; break; 00249 case 4: avctx->sample_fmt = SAMPLE_FMT_S32; break; 00250 default: 00251 av_log(s->avctx, AV_LOG_ERROR, "Invalid/unsupported sample format. Please contact the developers.\n"); 00252 return -1; 00253 } 00254 00255 // FIXME: horribly broken, but directly from reference source 00256 #define FRAME_TIME 1.04489795918367346939 00257 s->frame_length = (int)(FRAME_TIME * avctx->sample_rate); 00258 00259 s->last_frame_length = s->data_length % s->frame_length; 00260 s->total_frames = s->data_length / s->frame_length + 00261 (s->last_frame_length ? 1 : 0); 00262 00263 av_log(s->avctx, AV_LOG_DEBUG, "flags: %x chans: %d bps: %d rate: %d block: %d\n", 00264 s->flags, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate, 00265 avctx->block_align); 00266 av_log(s->avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n", 00267 s->data_length, s->frame_length, s->last_frame_length, s->total_frames); 00268 00269 // FIXME: seek table 00270 for (i = 0; i < s->total_frames; i++) 00271 skip_bits(&s->gb, 32); 00272 skip_bits(&s->gb, 32); // CRC32 of seektable 00273 00274 if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){ 00275 av_log(avctx, AV_LOG_ERROR, "frame_length too large\n"); 00276 return -1; 00277 } 00278 00279 s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels); 00280 } else { 00281 av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n"); 00282 return -1; 00283 } 00284 00285 return 0; 00286 } 00287 00288 static int tta_decode_frame(AVCodecContext *avctx, 00289 void *data, int *data_size, 00290 AVPacket *avpkt) 00291 { 00292 const uint8_t *buf = avpkt->data; 00293 int buf_size = avpkt->size; 00294 TTAContext *s = avctx->priv_data; 00295 int i; 00296 00297 init_get_bits(&s->gb, buf, buf_size*8); 00298 { 00299 int32_t predictors[s->channels]; 00300 TTAFilter filters[s->channels]; 00301 TTARice rices[s->channels]; 00302 int cur_chan = 0, framelen = s->frame_length; 00303 int32_t *p; 00304 00305 if (*data_size < (framelen * s->channels * 2)) { 00306 av_log(avctx, AV_LOG_ERROR, "Output buffer size is too small.\n"); 00307 return -1; 00308 } 00309 // FIXME: seeking 00310 s->total_frames--; 00311 if (!s->total_frames && s->last_frame_length) 00312 framelen = s->last_frame_length; 00313 00314 // init per channel states 00315 for (i = 0; i < s->channels; i++) { 00316 predictors[i] = 0; 00317 ttafilter_init(&(filters[i]), ttafilter_configs[s->bps-1][0], ttafilter_configs[s->bps-1][1]); 00318 rice_init(&(rices[i]), 10, 10); 00319 } 00320 00321 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) { 00322 int32_t *predictor = &(predictors[cur_chan]); 00323 TTAFilter *filter = &(filters[cur_chan]); 00324 TTARice *rice = &(rices[cur_chan]); 00325 uint32_t unary, depth, k; 00326 int32_t value; 00327 00328 unary = tta_get_unary(&s->gb); 00329 00330 if (unary == 0) { 00331 depth = 0; 00332 k = rice->k0; 00333 } else { 00334 depth = 1; 00335 k = rice->k1; 00336 unary--; 00337 } 00338 00339 if (get_bits_left(&s->gb) < k) 00340 return -1; 00341 00342 if (k) { 00343 if (k > MIN_CACHE_BITS) 00344 return -1; 00345 value = (unary << k) + get_bits(&s->gb, k); 00346 } else 00347 value = unary; 00348 00349 // FIXME: copy paste from original 00350 switch (depth) { 00351 case 1: 00352 rice->sum1 += value - (rice->sum1 >> 4); 00353 if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1]) 00354 rice->k1--; 00355 else if(rice->sum1 > shift_16[rice->k1 + 1]) 00356 rice->k1++; 00357 value += shift_1[rice->k0]; 00358 default: 00359 rice->sum0 += value - (rice->sum0 >> 4); 00360 if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0]) 00361 rice->k0--; 00362 else if(rice->sum0 > shift_16[rice->k0 + 1]) 00363 rice->k0++; 00364 } 00365 00366 // extract coded value 00367 #define UNFOLD(x) (((x)&1) ? (++(x)>>1) : (-(x)>>1)) 00368 *p = UNFOLD(value); 00369 00370 // run hybrid filter 00371 ttafilter_process(filter, p, 0); 00372 00373 // fixed order prediction 00374 #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k) 00375 switch (s->bps) { 00376 case 1: *p += PRED(*predictor, 4); break; 00377 case 2: 00378 case 3: *p += PRED(*predictor, 5); break; 00379 case 4: *p += *predictor; break; 00380 } 00381 *predictor = *p; 00382 00383 #if 0 00384 // extract 32bit float from last two int samples 00385 if (s->is_float && ((p - data) & 1)) { 00386 uint32_t neg = *p & 0x80000000; 00387 uint32_t hi = *(p - 1); 00388 uint32_t lo = abs(*p) - 1; 00389 00390 hi += (hi || lo) ? 0x3f80 : 0; 00391 // SWAP16: swap all the 16 bits 00392 *(p - 1) = (hi << 16) | SWAP16(lo) | neg; 00393 } 00394 #endif 00395 00396 /*if ((get_bits_count(&s->gb)+7)/8 > buf_size) 00397 { 00398 av_log(NULL, AV_LOG_INFO, "overread!!\n"); 00399 break; 00400 }*/ 00401 00402 // flip channels 00403 if (cur_chan < (s->channels-1)) 00404 cur_chan++; 00405 else { 00406 // decorrelate in case of stereo integer 00407 if (!s->is_float && (s->channels > 1)) { 00408 int32_t *r = p - 1; 00409 for (*p += *r / 2; r > p - s->channels; r--) 00410 *r = *(r + 1) - *r; 00411 } 00412 cur_chan = 0; 00413 } 00414 } 00415 00416 if (get_bits_left(&s->gb) < 32) 00417 return -1; 00418 skip_bits(&s->gb, 32); // frame crc 00419 00420 // convert to output buffer 00421 switch(s->bps) { 00422 case 2: { 00423 uint16_t *samples = data; 00424 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) { 00425 // *samples++ = (unsigned char)*p; 00426 // *samples++ = (unsigned char)(*p >> 8); 00427 *samples++ = *p; 00428 } 00429 *data_size = (uint8_t *)samples - (uint8_t *)data; 00430 break; 00431 } 00432 default: 00433 av_log(s->avctx, AV_LOG_ERROR, "Error, only 16bit samples supported!\n"); 00434 } 00435 } 00436 00437 // return get_bits_count(&s->gb)+7)/8; 00438 return buf_size; 00439 } 00440 00441 static av_cold int tta_decode_close(AVCodecContext *avctx) { 00442 TTAContext *s = avctx->priv_data; 00443 00444 if (s->decode_buffer) 00445 av_free(s->decode_buffer); 00446 00447 return 0; 00448 } 00449 00450 AVCodec tta_decoder = { 00451 "tta", 00452 AVMEDIA_TYPE_AUDIO, 00453 CODEC_ID_TTA, 00454 sizeof(TTAContext), 00455 tta_decode_init, 00456 NULL, 00457 tta_decode_close, 00458 tta_decode_frame, 00459 .long_name = NULL_IF_CONFIG_SMALL("True Audio (TTA)"), 00460 };