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