Libav 0.7.1
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 ALT_BITSTREAM_READER_LE
00031 //#define DEBUG
00032 #include <limits.h>
00033 #include "avcodec.h"
00034 #include "get_bits.h"
00035 
00036 #define FORMAT_INT 1
00037 #define FORMAT_FLOAT 3
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     GetBitContext gb;
00060 
00061     int flags, channels, bps, is_float, data_length;
00062     int frame_length, last_frame_length, total_frames;
00063 
00064     int32_t *decode_buffer;
00065 
00066     TTAChannel *ch_ctx;
00067 } TTAContext;
00068 
00069 #if 0
00070 static inline int shift_1(int i)
00071 {
00072     if (i < 32)
00073         return 1 << i;
00074     else
00075         return 0x80000000; // 16 << 31
00076 }
00077 
00078 static inline int shift_16(int i)
00079 {
00080     if (i < 28)
00081         return 16 << i;
00082     else
00083         return 0x80000000; // 16 << 27
00084 }
00085 #else
00086 static const uint32_t shift_1[] = {
00087     0x00000001, 0x00000002, 0x00000004, 0x00000008,
00088     0x00000010, 0x00000020, 0x00000040, 0x00000080,
00089     0x00000100, 0x00000200, 0x00000400, 0x00000800,
00090     0x00001000, 0x00002000, 0x00004000, 0x00008000,
00091     0x00010000, 0x00020000, 0x00040000, 0x00080000,
00092     0x00100000, 0x00200000, 0x00400000, 0x00800000,
00093     0x01000000, 0x02000000, 0x04000000, 0x08000000,
00094     0x10000000, 0x20000000, 0x40000000, 0x80000000,
00095     0x80000000, 0x80000000, 0x80000000, 0x80000000,
00096     0x80000000, 0x80000000, 0x80000000, 0x80000000
00097 };
00098 
00099 static const uint32_t * const shift_16 = shift_1 + 4;
00100 #endif
00101 
00102 static const int32_t ttafilter_configs[4][2] = {
00103     {10, 1},
00104     {9, 1},
00105     {10, 1},
00106     {12, 0}
00107 };
00108 
00109 static void ttafilter_init(TTAFilter *c, int32_t shift, int32_t mode) {
00110     memset(c, 0, sizeof(TTAFilter));
00111     c->shift = shift;
00112    c->round = shift_1[shift-1];
00113 //    c->round = 1 << (shift - 1);
00114     c->mode = mode;
00115 }
00116 
00117 // FIXME: copy paste from original
00118 static inline void memshl(register int32_t *a, register int32_t *b) {
00119     *a++ = *b++;
00120     *a++ = *b++;
00121     *a++ = *b++;
00122     *a++ = *b++;
00123     *a++ = *b++;
00124     *a++ = *b++;
00125     *a++ = *b++;
00126     *a = *b;
00127 }
00128 
00129 // FIXME: copy paste from original
00130 // mode=1 encoder, mode=0 decoder
00131 static inline void ttafilter_process(TTAFilter *c, int32_t *in, int32_t mode) {
00132     register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round;
00133 
00134     if (!c->error) {
00135         sum += *dl++ * *qm, qm++;
00136         sum += *dl++ * *qm, qm++;
00137         sum += *dl++ * *qm, qm++;
00138         sum += *dl++ * *qm, qm++;
00139         sum += *dl++ * *qm, qm++;
00140         sum += *dl++ * *qm, qm++;
00141         sum += *dl++ * *qm, qm++;
00142         sum += *dl++ * *qm, qm++;
00143         dx += 8;
00144     } else if(c->error < 0) {
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         sum += *dl++ * (*qm -= *dx++), qm++;
00150         sum += *dl++ * (*qm -= *dx++), qm++;
00151         sum += *dl++ * (*qm -= *dx++), qm++;
00152         sum += *dl++ * (*qm -= *dx++), qm++;
00153     } else {
00154         sum += *dl++ * (*qm += *dx++), qm++;
00155         sum += *dl++ * (*qm += *dx++), qm++;
00156         sum += *dl++ * (*qm += *dx++), qm++;
00157         sum += *dl++ * (*qm += *dx++), qm++;
00158         sum += *dl++ * (*qm += *dx++), qm++;
00159         sum += *dl++ * (*qm += *dx++), qm++;
00160         sum += *dl++ * (*qm += *dx++), qm++;
00161         sum += *dl++ * (*qm += *dx++), qm++;
00162     }
00163 
00164     *(dx-0) = ((*(dl-1) >> 30) | 1) << 2;
00165     *(dx-1) = ((*(dl-2) >> 30) | 1) << 1;
00166     *(dx-2) = ((*(dl-3) >> 30) | 1) << 1;
00167     *(dx-3) = ((*(dl-4) >> 30) | 1);
00168 
00169     // compress
00170     if (mode) {
00171         *dl = *in;
00172         *in -= (sum >> c->shift);
00173         c->error = *in;
00174     } else {
00175         c->error = *in;
00176         *in += (sum >> c->shift);
00177         *dl = *in;
00178     }
00179 
00180     if (c->mode) {
00181         *(dl-1) = *dl - *(dl-1);
00182         *(dl-2) = *(dl-1) - *(dl-2);
00183         *(dl-3) = *(dl-2) - *(dl-3);
00184     }
00185 
00186     memshl(c->dl, c->dl + 1);
00187     memshl(c->dx, c->dx + 1);
00188 }
00189 
00190 static void rice_init(TTARice *c, uint32_t k0, uint32_t k1)
00191 {
00192     c->k0 = k0;
00193     c->k1 = k1;
00194     c->sum0 = shift_16[k0];
00195     c->sum1 = shift_16[k1];
00196 }
00197 
00198 static int tta_get_unary(GetBitContext *gb)
00199 {
00200     int ret = 0;
00201 
00202     // count ones
00203     while(get_bits1(gb))
00204         ret++;
00205     return ret;
00206 }
00207 
00208 static av_cold int tta_decode_init(AVCodecContext * avctx)
00209 {
00210     TTAContext *s = avctx->priv_data;
00211     int i;
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         /* signature */
00223         skip_bits(&s->gb, 32);
00224 //        if (get_bits_long(&s->gb, 32) != av_bswap32(AV_RL32("TTA1"))) {
00225 //            av_log(s->avctx, AV_LOG_ERROR, "Missing magic\n");
00226 //            return -1;
00227 //        }
00228 
00229         s->flags = get_bits(&s->gb, 16);
00230         if (s->flags != 1 && s->flags != 3)
00231         {
00232             av_log(s->avctx, AV_LOG_ERROR, "Invalid flags\n");
00233             return -1;
00234         }
00235         s->is_float = (s->flags == FORMAT_FLOAT);
00236         avctx->channels = s->channels = get_bits(&s->gb, 16);
00237         avctx->bits_per_coded_sample = get_bits(&s->gb, 16);
00238         s->bps = (avctx->bits_per_coded_sample + 7) / 8;
00239         avctx->sample_rate = get_bits_long(&s->gb, 32);
00240         if(avctx->sample_rate > 1000000){ //prevent FRAME_TIME * avctx->sample_rate from overflowing and sanity check
00241             av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
00242             return -1;
00243         }
00244         s->data_length = get_bits_long(&s->gb, 32);
00245         skip_bits(&s->gb, 32); // CRC32 of header
00246 
00247         if (s->is_float)
00248         {
00249             avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00250             av_log_ask_for_sample(s->avctx, "Unsupported sample format.\n");
00251             return -1;
00252         }
00253         else switch(s->bps) {
00254 //            case 1: avctx->sample_fmt = AV_SAMPLE_FMT_U8; break;
00255             case 2: avctx->sample_fmt = AV_SAMPLE_FMT_S16; break;
00256 //            case 3: avctx->sample_fmt = AV_SAMPLE_FMT_S24; break;
00257             case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break;
00258             default:
00259                 av_log_ask_for_sample(s->avctx,
00260                                       "Invalid/unsupported sample format.\n");
00261                 return -1;
00262         }
00263 
00264         // FIXME: horribly broken, but directly from reference source
00265 #define FRAME_TIME 1.04489795918367346939
00266         s->frame_length = (int)(FRAME_TIME * avctx->sample_rate);
00267 
00268         s->last_frame_length = s->data_length % s->frame_length;
00269         s->total_frames = s->data_length / s->frame_length +
00270                         (s->last_frame_length ? 1 : 0);
00271 
00272         av_log(s->avctx, AV_LOG_DEBUG, "flags: %x chans: %d bps: %d rate: %d block: %d\n",
00273             s->flags, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
00274             avctx->block_align);
00275         av_log(s->avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
00276             s->data_length, s->frame_length, s->last_frame_length, s->total_frames);
00277 
00278         // FIXME: seek table
00279         for (i = 0; i < s->total_frames; i++)
00280             skip_bits(&s->gb, 32);
00281         skip_bits(&s->gb, 32); // CRC32 of seektable
00282 
00283         if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){
00284             av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
00285             return -1;
00286         }
00287 
00288         s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
00289         s->ch_ctx = av_malloc(avctx->channels * sizeof(*s->ch_ctx));
00290         if (!s->ch_ctx)
00291             return AVERROR(ENOMEM);
00292     } else {
00293         av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
00294         return -1;
00295     }
00296 
00297     return 0;
00298 }
00299 
00300 static int tta_decode_frame(AVCodecContext *avctx,
00301         void *data, int *data_size,
00302         AVPacket *avpkt)
00303 {
00304     const uint8_t *buf = avpkt->data;
00305     int buf_size = avpkt->size;
00306     TTAContext *s = avctx->priv_data;
00307     int i;
00308 
00309     init_get_bits(&s->gb, buf, buf_size*8);
00310     {
00311         int cur_chan = 0, framelen = s->frame_length;
00312         int32_t *p;
00313 
00314         if (*data_size < (framelen * s->channels * 2)) {
00315             av_log(avctx, AV_LOG_ERROR, "Output buffer size is too small.\n");
00316             return -1;
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         // init per channel states
00324         for (i = 0; i < s->channels; i++) {
00325             s->ch_ctx[i].predictor = 0;
00326             ttafilter_init(&s->ch_ctx[i].filter, ttafilter_configs[s->bps-1][0], ttafilter_configs[s->bps-1][1]);
00327             rice_init(&s->ch_ctx[i].rice, 10, 10);
00328         }
00329 
00330         for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
00331             int32_t *predictor = &s->ch_ctx[cur_chan].predictor;
00332             TTAFilter *filter = &s->ch_ctx[cur_chan].filter;
00333             TTARice *rice = &s->ch_ctx[cur_chan].rice;
00334             uint32_t unary, depth, k;
00335             int32_t value;
00336 
00337             unary = tta_get_unary(&s->gb);
00338 
00339             if (unary == 0) {
00340                 depth = 0;
00341                 k = rice->k0;
00342             } else {
00343                 depth = 1;
00344                 k = rice->k1;
00345                 unary--;
00346             }
00347 
00348             if (get_bits_left(&s->gb) < k)
00349                 return -1;
00350 
00351             if (k) {
00352                 if (k > MIN_CACHE_BITS)
00353                     return -1;
00354                 value = (unary << k) + get_bits(&s->gb, k);
00355             } else
00356                 value = unary;
00357 
00358             // FIXME: copy paste from original
00359             switch (depth) {
00360             case 1:
00361                 rice->sum1 += value - (rice->sum1 >> 4);
00362                 if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1])
00363                     rice->k1--;
00364                 else if(rice->sum1 > shift_16[rice->k1 + 1])
00365                     rice->k1++;
00366                 value += shift_1[rice->k0];
00367             default:
00368                 rice->sum0 += value - (rice->sum0 >> 4);
00369                 if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0])
00370                     rice->k0--;
00371                 else if(rice->sum0 > shift_16[rice->k0 + 1])
00372                     rice->k0++;
00373             }
00374 
00375             // extract coded value
00376 #define UNFOLD(x) (((x)&1) ? (++(x)>>1) : (-(x)>>1))
00377             *p = UNFOLD(value);
00378 
00379             // run hybrid filter
00380             ttafilter_process(filter, p, 0);
00381 
00382             // fixed order prediction
00383 #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
00384             switch (s->bps) {
00385                 case 1: *p += PRED(*predictor, 4); break;
00386                 case 2:
00387                 case 3: *p += PRED(*predictor, 5); break;
00388                 case 4: *p += *predictor; break;
00389             }
00390             *predictor = *p;
00391 
00392 #if 0
00393             // extract 32bit float from last two int samples
00394             if (s->is_float && ((p - data) & 1)) {
00395                 uint32_t neg = *p & 0x80000000;
00396                 uint32_t hi = *(p - 1);
00397                 uint32_t lo = abs(*p) - 1;
00398 
00399                 hi += (hi || lo) ? 0x3f80 : 0;
00400                 // SWAP16: swap all the 16 bits
00401                 *(p - 1) = (hi << 16) | SWAP16(lo) | neg;
00402             }
00403 #endif
00404 
00405             /*if ((get_bits_count(&s->gb)+7)/8 > buf_size)
00406             {
00407                 av_log(NULL, AV_LOG_INFO, "overread!!\n");
00408                 break;
00409             }*/
00410 
00411             // flip channels
00412             if (cur_chan < (s->channels-1))
00413                 cur_chan++;
00414             else {
00415                 // decorrelate in case of stereo integer
00416                 if (!s->is_float && (s->channels > 1)) {
00417                     int32_t *r = p - 1;
00418                     for (*p += *r / 2; r > p - s->channels; r--)
00419                         *r = *(r + 1) - *r;
00420                 }
00421                 cur_chan = 0;
00422             }
00423         }
00424 
00425         if (get_bits_left(&s->gb) < 32)
00426             return -1;
00427         skip_bits(&s->gb, 32); // frame crc
00428 
00429         // convert to output buffer
00430         switch(s->bps) {
00431             case 2: {
00432                 uint16_t *samples = data;
00433                 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
00434 //                    *samples++ = (unsigned char)*p;
00435 //                    *samples++ = (unsigned char)(*p >> 8);
00436                     *samples++ = *p;
00437                 }
00438                 *data_size = (uint8_t *)samples - (uint8_t *)data;
00439                 break;
00440             }
00441             default:
00442                 av_log(s->avctx, AV_LOG_ERROR, "Error, only 16bit samples supported!\n");
00443         }
00444     }
00445 
00446 //    return get_bits_count(&s->gb)+7)/8;
00447     return buf_size;
00448 }
00449 
00450 static av_cold int tta_decode_close(AVCodecContext *avctx) {
00451     TTAContext *s = avctx->priv_data;
00452 
00453     av_free(s->decode_buffer);
00454     av_freep(&s->ch_ctx);
00455 
00456     return 0;
00457 }
00458 
00459 AVCodec ff_tta_decoder = {
00460     "tta",
00461     AVMEDIA_TYPE_AUDIO,
00462     CODEC_ID_TTA,
00463     sizeof(TTAContext),
00464     tta_decode_init,
00465     NULL,
00466     tta_decode_close,
00467     tta_decode_frame,
00468     .long_name = NULL_IF_CONFIG_SMALL("True Audio (TTA)"),
00469 };