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 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 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 const int64_t tta_channel_layouts[7] = {
00192     AV_CH_LAYOUT_STEREO,
00193     AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
00194     AV_CH_LAYOUT_QUAD,
00195     0,
00196     AV_CH_LAYOUT_5POINT1_BACK,
00197     AV_CH_LAYOUT_5POINT1_BACK|AV_CH_BACK_CENTER,
00198     AV_CH_LAYOUT_7POINT1_WIDE
00199 };
00200 
00201 static av_cold int tta_decode_init(AVCodecContext * avctx)
00202 {
00203     TTAContext *s = avctx->priv_data;
00204     int i;
00205 
00206     s->avctx = avctx;
00207 
00208     // 30bytes includes a seektable with one frame
00209     if (avctx->extradata_size < 30)
00210         return -1;
00211 
00212     init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8);
00213     if (show_bits_long(&s->gb, 32) == AV_RL32("TTA1"))
00214     {
00215         /* signature */
00216         skip_bits(&s->gb, 32);
00217 
00218         s->format = get_bits(&s->gb, 16);
00219         if (s->format > 2) {
00220             av_log(s->avctx, AV_LOG_ERROR, "Invalid format\n");
00221             return -1;
00222         }
00223         if (s->format == FORMAT_ENCRYPTED) {
00224             av_log_missing_feature(s->avctx, "Encrypted TTA", 0);
00225             return AVERROR(EINVAL);
00226         }
00227         avctx->channels = s->channels = get_bits(&s->gb, 16);
00228         if (s->channels > 1 && s->channels < 9)
00229             avctx->channel_layout = tta_channel_layouts[s->channels-2];
00230         avctx->bits_per_coded_sample = get_bits(&s->gb, 16);
00231         s->bps = (avctx->bits_per_coded_sample + 7) / 8;
00232         avctx->sample_rate = get_bits_long(&s->gb, 32);
00233         s->data_length = get_bits_long(&s->gb, 32);
00234         skip_bits(&s->gb, 32); // CRC32 of header
00235 
00236         if (s->channels == 0) {
00237             av_log(s->avctx, AV_LOG_ERROR, "Invalid number of channels\n");
00238             return AVERROR_INVALIDDATA;
00239         } else if (avctx->sample_rate == 0) {
00240             av_log(s->avctx, AV_LOG_ERROR, "Invalid samplerate\n");
00241             return AVERROR_INVALIDDATA;
00242         }
00243 
00244         switch(s->bps) {
00245         case 1: avctx->sample_fmt = AV_SAMPLE_FMT_U8; break;
00246         case 2:
00247             avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00248             avctx->bits_per_raw_sample = 16;
00249             break;
00250         case 3:
00251             avctx->sample_fmt = AV_SAMPLE_FMT_S32;
00252             avctx->bits_per_raw_sample = 24;
00253             break;
00254         //case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break;
00255         default:
00256             av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported sample format.\n");
00257             return AVERROR_INVALIDDATA;
00258         }
00259 
00260         // prevent overflow
00261         if (avctx->sample_rate > 0x7FFFFF) {
00262             av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
00263             return AVERROR(EINVAL);
00264         }
00265         s->frame_length = 256 * avctx->sample_rate / 245;
00266 
00267         s->last_frame_length = s->data_length % s->frame_length;
00268         s->total_frames = s->data_length / s->frame_length +
00269                         (s->last_frame_length ? 1 : 0);
00270 
00271         av_log(s->avctx, AV_LOG_DEBUG, "format: %d chans: %d bps: %d rate: %d block: %d\n",
00272             s->format, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
00273             avctx->block_align);
00274         av_log(s->avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
00275             s->data_length, s->frame_length, s->last_frame_length, s->total_frames);
00276 
00277         // FIXME: seek table
00278         for (i = 0; i < s->total_frames; i++)
00279             skip_bits(&s->gb, 32);
00280         skip_bits(&s->gb, 32); // CRC32 of seektable
00281 
00282         if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){
00283             av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
00284             return -1;
00285         }
00286 
00287         s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
00288         if (!s->decode_buffer)
00289             return AVERROR(ENOMEM);
00290         s->ch_ctx = av_malloc(avctx->channels * sizeof(*s->ch_ctx));
00291         if (!s->ch_ctx) {
00292             av_freep(&s->decode_buffer);
00293             return AVERROR(ENOMEM);
00294         }
00295     } else {
00296         av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
00297         return -1;
00298     }
00299 
00300     avcodec_get_frame_defaults(&s->frame);
00301     avctx->coded_frame = &s->frame;
00302 
00303     return 0;
00304 }
00305 
00306 static int tta_decode_frame(AVCodecContext *avctx, void *data,
00307                             int *got_frame_ptr, AVPacket *avpkt)
00308 {
00309     const uint8_t *buf = avpkt->data;
00310     int buf_size = avpkt->size;
00311     TTAContext *s = avctx->priv_data;
00312     int i, ret;
00313     int cur_chan = 0, framelen = s->frame_length;
00314     int32_t *p;
00315 
00316     init_get_bits(&s->gb, buf, buf_size*8);
00317 
00318     // FIXME: seeking
00319     s->total_frames--;
00320     if (!s->total_frames && s->last_frame_length)
00321         framelen = s->last_frame_length;
00322 
00323     /* get output buffer */
00324     s->frame.nb_samples = framelen;
00325     if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00326         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00327         return ret;
00328     }
00329 
00330     // decode directly to output buffer for 24-bit sample format
00331     if (s->bps == 3)
00332         s->decode_buffer = (int32_t *)s->frame.data[0];
00333 
00334     // init per channel states
00335     for (i = 0; i < s->channels; i++) {
00336         s->ch_ctx[i].predictor = 0;
00337         ttafilter_init(&s->ch_ctx[i].filter, ttafilter_configs[s->bps-1][0], ttafilter_configs[s->bps-1][1]);
00338         rice_init(&s->ch_ctx[i].rice, 10, 10);
00339     }
00340 
00341     for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
00342         int32_t *predictor = &s->ch_ctx[cur_chan].predictor;
00343         TTAFilter *filter = &s->ch_ctx[cur_chan].filter;
00344         TTARice *rice = &s->ch_ctx[cur_chan].rice;
00345         uint32_t unary, depth, k;
00346         int32_t value;
00347 
00348         unary = tta_get_unary(&s->gb);
00349 
00350         if (unary == 0) {
00351             depth = 0;
00352             k = rice->k0;
00353         } else {
00354             depth = 1;
00355             k = rice->k1;
00356             unary--;
00357         }
00358 
00359         if (get_bits_left(&s->gb) < k)
00360             return -1;
00361 
00362         if (k) {
00363             if (k > MIN_CACHE_BITS)
00364                 return -1;
00365             value = (unary << k) + get_bits(&s->gb, k);
00366         } else
00367             value = unary;
00368 
00369         // FIXME: copy paste from original
00370         switch (depth) {
00371         case 1:
00372             rice->sum1 += value - (rice->sum1 >> 4);
00373             if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1])
00374                 rice->k1--;
00375             else if(rice->sum1 > shift_16[rice->k1 + 1])
00376                 rice->k1++;
00377             value += shift_1[rice->k0];
00378         default:
00379             rice->sum0 += value - (rice->sum0 >> 4);
00380             if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0])
00381                 rice->k0--;
00382             else if(rice->sum0 > shift_16[rice->k0 + 1])
00383                 rice->k0++;
00384         }
00385 
00386         // extract coded value
00387 #define UNFOLD(x) (((x)&1) ? (++(x)>>1) : (-(x)>>1))
00388         *p = UNFOLD(value);
00389 
00390         // run hybrid filter
00391         ttafilter_process(filter, p, 0);
00392 
00393         // fixed order prediction
00394 #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
00395         switch (s->bps) {
00396             case 1: *p += PRED(*predictor, 4); break;
00397             case 2:
00398             case 3: *p += PRED(*predictor, 5); break;
00399             case 4: *p += *predictor; break;
00400         }
00401         *predictor = *p;
00402 
00403         // flip channels
00404         if (cur_chan < (s->channels-1))
00405             cur_chan++;
00406         else {
00407             // decorrelate in case of stereo integer
00408             if (s->channels > 1) {
00409                 int32_t *r = p - 1;
00410                 for (*p += *r / 2; r > p - s->channels; r--)
00411                     *r = *(r + 1) - *r;
00412             }
00413             cur_chan = 0;
00414         }
00415     }
00416 
00417     if (get_bits_left(&s->gb) < 32)
00418         return -1;
00419     skip_bits(&s->gb, 32); // frame crc
00420 
00421         // convert to output buffer
00422         switch(s->bps) {
00423             case 1: {
00424                 uint8_t *samples = (uint8_t *)s->frame.data[0];
00425                 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
00426                     *samples++ = *p + 0x80;
00427                 break;
00428             }
00429             case 2: {
00430                 uint16_t *samples = (int16_t *)s->frame.data[0];
00431                 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
00432                     *samples++ = *p;
00433                 break;
00434             }
00435             case 3: {
00436                 // shift samples for 24-bit sample format
00437                 int32_t *samples = (int32_t *)s->frame.data[0];
00438                 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
00439                     *samples++ <<= 8;
00440                 // reset decode buffer
00441                 s->decode_buffer = NULL;
00442                 break;
00443             }
00444         }
00445 
00446     *got_frame_ptr   = 1;
00447     *(AVFrame *)data = s->frame;
00448 
00449     return buf_size;
00450 }
00451 
00452 static av_cold int tta_decode_close(AVCodecContext *avctx) {
00453     TTAContext *s = avctx->priv_data;
00454 
00455     av_free(s->decode_buffer);
00456     av_freep(&s->ch_ctx);
00457 
00458     return 0;
00459 }
00460 
00461 AVCodec ff_tta_decoder = {
00462     .name           = "tta",
00463     .type           = AVMEDIA_TYPE_AUDIO,
00464     .id             = CODEC_ID_TTA,
00465     .priv_data_size = sizeof(TTAContext),
00466     .init           = tta_decode_init,
00467     .close          = tta_decode_close,
00468     .decode         = tta_decode_frame,
00469     .capabilities   = CODEC_CAP_DR1,
00470     .long_name = NULL_IF_CONFIG_SMALL("True Audio (TTA)"),
00471 };