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