• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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