libavcodec/tta.c
Go to the documentation of this file.
00001 /*
00002  * TTA (The Lossless True Audio) decoder
00003  * Copyright (c) 2006 Alex Beregszaszi
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00030 #define BITSTREAM_READER_LE
00031 //#define DEBUG
00032 #include <limits.h>
00033 #include "avcodec.h"
00034 #include "internal.h"
00035 #include "get_bits.h"
00036 #include "libavutil/crc.h"
00037 
00038 #define FORMAT_SIMPLE    1
00039 #define FORMAT_ENCRYPTED 2
00040 
00041 #define MAX_ORDER 16
00042 typedef struct TTAFilter {
00043     int32_t shift, round, error, mode;
00044     int32_t qm[MAX_ORDER];
00045     int32_t dx[MAX_ORDER];
00046     int32_t dl[MAX_ORDER];
00047 } TTAFilter;
00048 
00049 typedef struct TTARice {
00050     uint32_t k0, k1, sum0, sum1;
00051 } TTARice;
00052 
00053 typedef struct TTAChannel {
00054     int32_t predictor;
00055     TTAFilter filter;
00056     TTARice rice;
00057 } TTAChannel;
00058 
00059 typedef struct TTAContext {
00060     AVCodecContext *avctx;
00061     AVFrame frame;
00062     GetBitContext gb;
00063     const AVCRC *crc_table;
00064 
00065     int format, channels, bps;
00066     unsigned data_length;
00067     int frame_length, last_frame_length, total_frames;
00068 
00069     int32_t *decode_buffer;
00070 
00071     TTAChannel *ch_ctx;
00072 } TTAContext;
00073 
00074 static const uint32_t shift_1[] = {
00075     0x00000001, 0x00000002, 0x00000004, 0x00000008,
00076     0x00000010, 0x00000020, 0x00000040, 0x00000080,
00077     0x00000100, 0x00000200, 0x00000400, 0x00000800,
00078     0x00001000, 0x00002000, 0x00004000, 0x00008000,
00079     0x00010000, 0x00020000, 0x00040000, 0x00080000,
00080     0x00100000, 0x00200000, 0x00400000, 0x00800000,
00081     0x01000000, 0x02000000, 0x04000000, 0x08000000,
00082     0x10000000, 0x20000000, 0x40000000, 0x80000000,
00083     0x80000000, 0x80000000, 0x80000000, 0x80000000,
00084     0x80000000, 0x80000000, 0x80000000, 0x80000000
00085 };
00086 
00087 static const uint32_t * const shift_16 = shift_1 + 4;
00088 
00089 static const int32_t ttafilter_configs[4][2] = {
00090     {10, 1},
00091     {9, 1},
00092     {10, 1},
00093     {12, 0}
00094 };
00095 
00096 static void ttafilter_init(TTAFilter *c, int32_t shift, int32_t mode) {
00097     memset(c, 0, sizeof(TTAFilter));
00098     c->shift = shift;
00099    c->round = shift_1[shift-1];
00100 //    c->round = 1 << (shift - 1);
00101     c->mode = mode;
00102 }
00103 
00104 // FIXME: copy paste from original
00105 static inline void memshl(register int32_t *a, register int32_t *b) {
00106     *a++ = *b++;
00107     *a++ = *b++;
00108     *a++ = *b++;
00109     *a++ = *b++;
00110     *a++ = *b++;
00111     *a++ = *b++;
00112     *a++ = *b++;
00113     *a = *b;
00114 }
00115 
00116 // FIXME: copy paste from original
00117 // mode=1 encoder, mode=0 decoder
00118 static inline void ttafilter_process(TTAFilter *c, int32_t *in, int32_t mode) {
00119     register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round;
00120 
00121     if (!c->error) {
00122         sum += *dl++ * *qm, qm++;
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         dx += 8;
00131     } else if(c->error < 0) {
00132         sum += *dl++ * (*qm -= *dx++), qm++;
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     } else {
00141         sum += *dl++ * (*qm += *dx++), qm++;
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     }
00150 
00151     *(dx-0) = ((*(dl-1) >> 30) | 1) << 2;
00152     *(dx-1) = ((*(dl-2) >> 30) | 1) << 1;
00153     *(dx-2) = ((*(dl-3) >> 30) | 1) << 1;
00154     *(dx-3) = ((*(dl-4) >> 30) | 1);
00155 
00156     // compress
00157     if (mode) {
00158         *dl = *in;
00159         *in -= (sum >> c->shift);
00160         c->error = *in;
00161     } else {
00162         c->error = *in;
00163         *in += (sum >> c->shift);
00164         *dl = *in;
00165     }
00166 
00167     if (c->mode) {
00168         *(dl-1) = *dl - *(dl-1);
00169         *(dl-2) = *(dl-1) - *(dl-2);
00170         *(dl-3) = *(dl-2) - *(dl-3);
00171     }
00172 
00173     memshl(c->dl, c->dl + 1);
00174     memshl(c->dx, c->dx + 1);
00175 }
00176 
00177 static void rice_init(TTARice *c, uint32_t k0, uint32_t k1)
00178 {
00179     c->k0 = k0;
00180     c->k1 = k1;
00181     c->sum0 = shift_16[k0];
00182     c->sum1 = shift_16[k1];
00183 }
00184 
00185 static int tta_get_unary(GetBitContext *gb)
00186 {
00187     int ret = 0;
00188 
00189     // count ones
00190     while (get_bits_left(gb) > 0 && get_bits1(gb))
00191         ret++;
00192     return ret;
00193 }
00194 
00195 static int tta_check_crc(TTAContext *s, const uint8_t *buf, int buf_size)
00196 {
00197     uint32_t crc, CRC;
00198 
00199     CRC = AV_RL32(buf + buf_size);
00200     crc = av_crc(s->crc_table, 0xFFFFFFFFU, buf, buf_size);
00201     if (CRC != (crc ^ 0xFFFFFFFFU)) {
00202         av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
00203         return AVERROR_INVALIDDATA;
00204     }
00205 
00206     return 0;
00207 }
00208 
00209 static av_cold int tta_decode_init(AVCodecContext * avctx)
00210 {
00211     TTAContext *s = avctx->priv_data;
00212 
00213     s->avctx = avctx;
00214 
00215     // 30bytes includes a seektable with one frame
00216     if (avctx->extradata_size < 30)
00217         return -1;
00218 
00219     init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8);
00220     if (show_bits_long(&s->gb, 32) == AV_RL32("TTA1"))
00221     {
00222         if (avctx->err_recognition & AV_EF_CRCCHECK) {
00223             s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
00224             if (tta_check_crc(s, avctx->extradata, 18))
00225                 return AVERROR_INVALIDDATA;
00226         }
00227 
00228         /* signature */
00229         skip_bits_long(&s->gb, 32);
00230 
00231         s->format = get_bits(&s->gb, 16);
00232         if (s->format > 2) {
00233             av_log(s->avctx, AV_LOG_ERROR, "Invalid format\n");
00234             return -1;
00235         }
00236         if (s->format == FORMAT_ENCRYPTED) {
00237             av_log_missing_feature(s->avctx, "Encrypted TTA", 0);
00238             return AVERROR(EINVAL);
00239         }
00240         avctx->channels = s->channels = get_bits(&s->gb, 16);
00241         avctx->bits_per_coded_sample = get_bits(&s->gb, 16);
00242         s->bps = (avctx->bits_per_coded_sample + 7) / 8;
00243         avctx->sample_rate = get_bits_long(&s->gb, 32);
00244         s->data_length = get_bits_long(&s->gb, 32);
00245         skip_bits_long(&s->gb, 32); // CRC32 of header
00246 
00247         if (s->channels == 0) {
00248             av_log(s->avctx, AV_LOG_ERROR, "Invalid number of channels\n");
00249             return AVERROR_INVALIDDATA;
00250         } else if (avctx->sample_rate == 0) {
00251             av_log(s->avctx, AV_LOG_ERROR, "Invalid samplerate\n");
00252             return AVERROR_INVALIDDATA;
00253         }
00254 
00255         switch(s->bps) {
00256         case 2:
00257             avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00258             avctx->bits_per_raw_sample = 16;
00259             break;
00260         case 3:
00261             avctx->sample_fmt = AV_SAMPLE_FMT_S32;
00262             avctx->bits_per_raw_sample = 24;
00263             break;
00264         default:
00265             av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported sample format.\n");
00266             return AVERROR_INVALIDDATA;
00267         }
00268 
00269         // prevent overflow
00270         if (avctx->sample_rate > 0x7FFFFFu) {
00271             av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
00272             return AVERROR(EINVAL);
00273         }
00274         s->frame_length = 256 * avctx->sample_rate / 245;
00275 
00276         s->last_frame_length = s->data_length % s->frame_length;
00277         s->total_frames = s->data_length / s->frame_length +
00278                         (s->last_frame_length ? 1 : 0);
00279 
00280         av_log(s->avctx, AV_LOG_DEBUG, "format: %d chans: %d bps: %d rate: %d block: %d\n",
00281             s->format, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
00282             avctx->block_align);
00283         av_log(s->avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
00284             s->data_length, s->frame_length, s->last_frame_length, s->total_frames);
00285 
00286         // FIXME: seek table
00287         if (avctx->extradata_size <= 26 || s->total_frames > INT_MAX / 4 ||
00288             avctx->extradata_size - 26 < s->total_frames * 4)
00289             av_log(avctx, AV_LOG_WARNING, "Seek table missing or too small\n");
00290         else if (avctx->err_recognition & AV_EF_CRCCHECK) {
00291             if (tta_check_crc(s, avctx->extradata + 22, s->total_frames * 4))
00292                 return AVERROR_INVALIDDATA;
00293         }
00294         skip_bits_long(&s->gb, 32 * s->total_frames);
00295         skip_bits_long(&s->gb, 32); // CRC32 of seektable
00296 
00297         if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){
00298             av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
00299             return -1;
00300         }
00301 
00302         if (s->bps == 2) {
00303             s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
00304             if (!s->decode_buffer)
00305                 return AVERROR(ENOMEM);
00306         }
00307         s->ch_ctx = av_malloc(avctx->channels * sizeof(*s->ch_ctx));
00308         if (!s->ch_ctx) {
00309             av_freep(&s->decode_buffer);
00310             return AVERROR(ENOMEM);
00311         }
00312     } else {
00313         av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
00314         return -1;
00315     }
00316 
00317     avcodec_get_frame_defaults(&s->frame);
00318     avctx->coded_frame = &s->frame;
00319 
00320     return 0;
00321 }
00322 
00323 static int tta_decode_frame(AVCodecContext *avctx, void *data,
00324                             int *got_frame_ptr, AVPacket *avpkt)
00325 {
00326     const uint8_t *buf = avpkt->data;
00327     int buf_size = avpkt->size;
00328     TTAContext *s = avctx->priv_data;
00329     int i, ret;
00330     int cur_chan = 0, framelen = s->frame_length;
00331     int32_t *p;
00332 
00333     if (avctx->err_recognition & AV_EF_CRCCHECK) {
00334         if (buf_size < 4 || tta_check_crc(s, buf, buf_size - 4))
00335             return AVERROR_INVALIDDATA;
00336     }
00337 
00338     init_get_bits(&s->gb, buf, buf_size*8);
00339 
00340     // FIXME: seeking
00341     s->total_frames--;
00342     if (!s->total_frames && s->last_frame_length)
00343         framelen = s->last_frame_length;
00344 
00345     /* get output buffer */
00346     s->frame.nb_samples = framelen;
00347     if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
00348         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00349         return ret;
00350     }
00351 
00352     // decode directly to output buffer for 24-bit sample format
00353     if (s->bps == 3)
00354         s->decode_buffer = (int32_t *)s->frame.data[0];
00355 
00356     // init per channel states
00357     for (i = 0; i < s->channels; i++) {
00358         s->ch_ctx[i].predictor = 0;
00359         ttafilter_init(&s->ch_ctx[i].filter, ttafilter_configs[s->bps-1][0], ttafilter_configs[s->bps-1][1]);
00360         rice_init(&s->ch_ctx[i].rice, 10, 10);
00361     }
00362 
00363     for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
00364         int32_t *predictor = &s->ch_ctx[cur_chan].predictor;
00365         TTAFilter *filter = &s->ch_ctx[cur_chan].filter;
00366         TTARice *rice = &s->ch_ctx[cur_chan].rice;
00367         uint32_t unary, depth, k;
00368         int32_t value;
00369 
00370         unary = tta_get_unary(&s->gb);
00371 
00372         if (unary == 0) {
00373             depth = 0;
00374             k = rice->k0;
00375         } else {
00376             depth = 1;
00377             k = rice->k1;
00378             unary--;
00379         }
00380 
00381         if (get_bits_left(&s->gb) < k)
00382             return -1;
00383 
00384         if (k) {
00385             if (k > MIN_CACHE_BITS)
00386                 return -1;
00387             value = (unary << k) + get_bits(&s->gb, k);
00388         } else
00389             value = unary;
00390 
00391         // FIXME: copy paste from original
00392         switch (depth) {
00393         case 1:
00394             rice->sum1 += value - (rice->sum1 >> 4);
00395             if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1])
00396                 rice->k1--;
00397             else if(rice->sum1 > shift_16[rice->k1 + 1])
00398                 rice->k1++;
00399             value += shift_1[rice->k0];
00400         default:
00401             rice->sum0 += value - (rice->sum0 >> 4);
00402             if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0])
00403                 rice->k0--;
00404             else if(rice->sum0 > shift_16[rice->k0 + 1])
00405                 rice->k0++;
00406         }
00407 
00408         // extract coded value
00409 #define UNFOLD(x) (((x)&1) ? (++(x)>>1) : (-(x)>>1))
00410         *p = UNFOLD(value);
00411 
00412         // run hybrid filter
00413         ttafilter_process(filter, p, 0);
00414 
00415         // fixed order prediction
00416 #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
00417         switch (s->bps) {
00418             case 1: *p += PRED(*predictor, 4); break;
00419             case 2:
00420             case 3: *p += PRED(*predictor, 5); break;
00421             case 4: *p += *predictor; break;
00422         }
00423         *predictor = *p;
00424 
00425         // flip channels
00426         if (cur_chan < (s->channels-1))
00427             cur_chan++;
00428         else {
00429             // decorrelate in case of stereo integer
00430             if (s->channels > 1) {
00431                 int32_t *r = p - 1;
00432                 for (*p += *r / 2; r > p - s->channels; r--)
00433                     *r = *(r + 1) - *r;
00434             }
00435             cur_chan = 0;
00436         }
00437     }
00438 
00439     if (get_bits_left(&s->gb) < 32)
00440         return -1;
00441     skip_bits_long(&s->gb, 32); // frame crc
00442 
00443     // convert to output buffer
00444     if (s->bps == 2) {
00445         int16_t *samples = (int16_t *)s->frame.data[0];
00446         for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
00447             *samples++ = *p;
00448     } else {
00449         // shift samples for 24-bit sample format
00450         int32_t *samples = (int32_t *)s->frame.data[0];
00451         for (i = 0; i < framelen * s->channels; i++)
00452             *samples++ <<= 8;
00453         // reset decode buffer
00454         s->decode_buffer = NULL;
00455     }
00456 
00457     *got_frame_ptr   = 1;
00458     *(AVFrame *)data = s->frame;
00459 
00460     return buf_size;
00461 }
00462 
00463 static av_cold int tta_decode_close(AVCodecContext *avctx) {
00464     TTAContext *s = avctx->priv_data;
00465 
00466     av_free(s->decode_buffer);
00467     av_freep(&s->ch_ctx);
00468 
00469     return 0;
00470 }
00471 
00472 AVCodec ff_tta_decoder = {
00473     .name           = "tta",
00474     .type           = AVMEDIA_TYPE_AUDIO,
00475     .id             = CODEC_ID_TTA,
00476     .priv_data_size = sizeof(TTAContext),
00477     .init           = tta_decode_init,
00478     .close          = tta_decode_close,
00479     .decode         = tta_decode_frame,
00480     .capabilities   = CODEC_CAP_DR1,
00481     .long_name = NULL_IF_CONFIG_SMALL("True Audio (TTA)"),
00482 };