• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • 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 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 ALT_BITSTREAM_READER_LE
00031 //#define DEBUG
00032 #include <limits.h>
00033 #include "avcodec.h"
00034 #include "bitstream.h"
00035 
00036 #define FORMAT_INT 1
00037 #define FORMAT_FLOAT 3
00038 
00039 typedef struct TTAContext {
00040     AVCodecContext *avctx;
00041     GetBitContext gb;
00042 
00043     int flags, channels, bps, is_float, data_length;
00044     int frame_length, last_frame_length, total_frames;
00045 
00046     int32_t *decode_buffer;
00047 } TTAContext;
00048 
00049 #if 0
00050 static inline int shift_1(int i)
00051 {
00052     if (i < 32)
00053         return 1 << i;
00054     else
00055         return 0x80000000; // 16 << 31
00056 }
00057 
00058 static inline int shift_16(int i)
00059 {
00060     if (i < 28)
00061         return 16 << i;
00062     else
00063         return 0x80000000; // 16 << 27
00064 }
00065 #else
00066 static const uint32_t shift_1[] = {
00067     0x00000001, 0x00000002, 0x00000004, 0x00000008,
00068     0x00000010, 0x00000020, 0x00000040, 0x00000080,
00069     0x00000100, 0x00000200, 0x00000400, 0x00000800,
00070     0x00001000, 0x00002000, 0x00004000, 0x00008000,
00071     0x00010000, 0x00020000, 0x00040000, 0x00080000,
00072     0x00100000, 0x00200000, 0x00400000, 0x00800000,
00073     0x01000000, 0x02000000, 0x04000000, 0x08000000,
00074     0x10000000, 0x20000000, 0x40000000, 0x80000000,
00075     0x80000000, 0x80000000, 0x80000000, 0x80000000,
00076     0x80000000, 0x80000000, 0x80000000, 0x80000000
00077 };
00078 
00079 static const uint32_t * const shift_16 = shift_1 + 4;
00080 #endif
00081 
00082 #define MAX_ORDER 16
00083 typedef struct TTAFilter {
00084     int32_t shift, round, error, mode;
00085     int32_t qm[MAX_ORDER];
00086     int32_t dx[MAX_ORDER];
00087     int32_t dl[MAX_ORDER];
00088 } TTAFilter;
00089 
00090 static const int32_t ttafilter_configs[4][2] = {
00091     {10, 1},
00092     {9, 1},
00093     {10, 1},
00094     {12, 0}
00095 };
00096 
00097 static void ttafilter_init(TTAFilter *c, int32_t shift, int32_t mode) {
00098     memset(c, 0, sizeof(TTAFilter));
00099     c->shift = shift;
00100    c->round = shift_1[shift-1];
00101 //    c->round = 1 << (shift - 1);
00102     c->mode = mode;
00103 }
00104 
00105 // FIXME: copy paste from original
00106 static inline void memshl(register int32_t *a, register int32_t *b) {
00107     *a++ = *b++;
00108     *a++ = *b++;
00109     *a++ = *b++;
00110     *a++ = *b++;
00111     *a++ = *b++;
00112     *a++ = *b++;
00113     *a++ = *b++;
00114     *a = *b;
00115 }
00116 
00117 // FIXME: copy paste from original
00118 // mode=1 encoder, mode=0 decoder
00119 static inline void ttafilter_process(TTAFilter *c, int32_t *in, int32_t mode) {
00120     register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round;
00121 
00122     if (!c->error) {
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         sum += *dl++ * *qm, qm++;
00130         sum += *dl++ * *qm, qm++;
00131         dx += 8;
00132     } else if(c->error < 0) {
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         sum += *dl++ * (*qm -= *dx++), qm++;
00140         sum += *dl++ * (*qm -= *dx++), qm++;
00141     } else {
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         sum += *dl++ * (*qm += *dx++), qm++;
00149         sum += *dl++ * (*qm += *dx++), qm++;
00150     }
00151 
00152     *(dx-0) = ((*(dl-1) >> 30) | 1) << 2;
00153     *(dx-1) = ((*(dl-2) >> 30) | 1) << 1;
00154     *(dx-2) = ((*(dl-3) >> 30) | 1) << 1;
00155     *(dx-3) = ((*(dl-4) >> 30) | 1);
00156 
00157     // compress
00158     if (mode) {
00159         *dl = *in;
00160         *in -= (sum >> c->shift);
00161         c->error = *in;
00162     } else {
00163         c->error = *in;
00164         *in += (sum >> c->shift);
00165         *dl = *in;
00166     }
00167 
00168     if (c->mode) {
00169         *(dl-1) = *dl - *(dl-1);
00170         *(dl-2) = *(dl-1) - *(dl-2);
00171         *(dl-3) = *(dl-2) - *(dl-3);
00172     }
00173 
00174     memshl(c->dl, c->dl + 1);
00175     memshl(c->dx, c->dx + 1);
00176 }
00177 
00178 typedef struct TTARice {
00179     uint32_t k0, k1, sum0, sum1;
00180 } TTARice;
00181 
00182 static void rice_init(TTARice *c, uint32_t k0, uint32_t k1)
00183 {
00184     c->k0 = k0;
00185     c->k1 = k1;
00186     c->sum0 = shift_16[k0];
00187     c->sum1 = shift_16[k1];
00188 }
00189 
00190 static int tta_get_unary(GetBitContext *gb)
00191 {
00192     int ret = 0;
00193 
00194     // count ones
00195     while(get_bits1(gb))
00196         ret++;
00197     return ret;
00198 }
00199 
00200 static av_cold int tta_decode_init(AVCodecContext * avctx)
00201 {
00202     TTAContext *s = avctx->priv_data;
00203     int i;
00204 
00205     s->avctx = avctx;
00206 
00207     // 30bytes includes a seektable with one frame
00208     if (avctx->extradata_size < 30)
00209         return -1;
00210 
00211     init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size);
00212     if (show_bits_long(&s->gb, 32) == AV_RL32("TTA1"))
00213     {
00214         /* signature */
00215         skip_bits(&s->gb, 32);
00216 //        if (get_bits_long(&s->gb, 32) != bswap_32(AV_RL32("TTA1"))) {
00217 //            av_log(s->avctx, AV_LOG_ERROR, "Missing magic\n");
00218 //            return -1;
00219 //        }
00220 
00221         s->flags = get_bits(&s->gb, 16);
00222         if (s->flags != 1 && s->flags != 3)
00223         {
00224             av_log(s->avctx, AV_LOG_ERROR, "Invalid flags\n");
00225             return -1;
00226         }
00227         s->is_float = (s->flags == FORMAT_FLOAT);
00228         avctx->channels = s->channels = get_bits(&s->gb, 16);
00229         avctx->bits_per_coded_sample = get_bits(&s->gb, 16);
00230         s->bps = (avctx->bits_per_coded_sample + 7) / 8;
00231         avctx->sample_rate = get_bits_long(&s->gb, 32);
00232         if(avctx->sample_rate > 1000000){ //prevent FRAME_TIME * avctx->sample_rate from overflowing and sanity check
00233             av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
00234             return -1;
00235         }
00236         s->data_length = get_bits_long(&s->gb, 32);
00237         skip_bits(&s->gb, 32); // CRC32 of header
00238 
00239         if (s->is_float)
00240         {
00241             avctx->sample_fmt = SAMPLE_FMT_FLT;
00242             av_log(s->avctx, AV_LOG_ERROR, "Unsupported sample format. Please contact the developers.\n");
00243             return -1;
00244         }
00245         else switch(s->bps) {
00246 //            case 1: avctx->sample_fmt = SAMPLE_FMT_U8; break;
00247             case 2: avctx->sample_fmt = SAMPLE_FMT_S16; break;
00248 //            case 3: avctx->sample_fmt = SAMPLE_FMT_S24; break;
00249             case 4: avctx->sample_fmt = SAMPLE_FMT_S32; break;
00250             default:
00251                 av_log(s->avctx, AV_LOG_ERROR, "Invalid/unsupported sample format. Please contact the developers.\n");
00252                 return -1;
00253         }
00254 
00255         // FIXME: horribly broken, but directly from reference source
00256 #define FRAME_TIME 1.04489795918367346939
00257         s->frame_length = (int)(FRAME_TIME * avctx->sample_rate);
00258 
00259         s->last_frame_length = s->data_length % s->frame_length;
00260         s->total_frames = s->data_length / s->frame_length +
00261                         (s->last_frame_length ? 1 : 0);
00262 
00263         av_log(s->avctx, AV_LOG_DEBUG, "flags: %x chans: %d bps: %d rate: %d block: %d\n",
00264             s->flags, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
00265             avctx->block_align);
00266         av_log(s->avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
00267             s->data_length, s->frame_length, s->last_frame_length, s->total_frames);
00268 
00269         // FIXME: seek table
00270         for (i = 0; i < s->total_frames; i++)
00271             skip_bits(&s->gb, 32);
00272         skip_bits(&s->gb, 32); // CRC32 of seektable
00273 
00274         if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){
00275             av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
00276             return -1;
00277         }
00278 
00279         s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
00280     } else {
00281         av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
00282         return -1;
00283     }
00284 
00285     return 0;
00286 }
00287 
00288 static int tta_decode_frame(AVCodecContext *avctx,
00289         void *data, int *data_size,
00290         const uint8_t *buf, int buf_size)
00291 {
00292     TTAContext *s = avctx->priv_data;
00293     int i;
00294 
00295     init_get_bits(&s->gb, buf, buf_size*8);
00296     {
00297         int32_t predictors[s->channels];
00298         TTAFilter filters[s->channels];
00299         TTARice rices[s->channels];
00300         int cur_chan = 0, framelen = s->frame_length;
00301         int32_t *p;
00302 
00303         // FIXME: seeking
00304         s->total_frames--;
00305         if (!s->total_frames && s->last_frame_length)
00306             framelen = s->last_frame_length;
00307 
00308         // init per channel states
00309         for (i = 0; i < s->channels; i++) {
00310             predictors[i] = 0;
00311             ttafilter_init(&(filters[i]), ttafilter_configs[s->bps-1][0], ttafilter_configs[s->bps-1][1]);
00312             rice_init(&(rices[i]), 10, 10);
00313         }
00314 
00315         for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
00316             int32_t *predictor = &(predictors[cur_chan]);
00317             TTAFilter *filter = &(filters[cur_chan]);
00318             TTARice *rice = &(rices[cur_chan]);
00319             uint32_t unary, depth, k;
00320             int32_t value;
00321 
00322             unary = tta_get_unary(&s->gb);
00323 
00324             if (unary == 0) {
00325                 depth = 0;
00326                 k = rice->k0;
00327             } else {
00328                 depth = 1;
00329                 k = rice->k1;
00330                 unary--;
00331             }
00332 
00333             if (k)
00334                 value = (unary << k) + get_bits(&s->gb, k);
00335             else
00336                 value = unary;
00337 
00338             // FIXME: copy paste from original
00339             switch (depth) {
00340             case 1:
00341                 rice->sum1 += value - (rice->sum1 >> 4);
00342                 if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1])
00343                     rice->k1--;
00344                 else if(rice->sum1 > shift_16[rice->k1 + 1])
00345                     rice->k1++;
00346                 value += shift_1[rice->k0];
00347             default:
00348                 rice->sum0 += value - (rice->sum0 >> 4);
00349                 if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0])
00350                     rice->k0--;
00351                 else if(rice->sum0 > shift_16[rice->k0 + 1])
00352                     rice->k0++;
00353             }
00354 
00355             // extract coded value
00356 #define UNFOLD(x) (((x)&1) ? (++(x)>>1) : (-(x)>>1))
00357             *p = UNFOLD(value);
00358 
00359             // run hybrid filter
00360             ttafilter_process(filter, p, 0);
00361 
00362             // fixed order prediction
00363 #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
00364             switch (s->bps) {
00365                 case 1: *p += PRED(*predictor, 4); break;
00366                 case 2:
00367                 case 3: *p += PRED(*predictor, 5); break;
00368                 case 4: *p += *predictor; break;
00369             }
00370             *predictor = *p;
00371 
00372 #if 0
00373             // extract 32bit float from last two int samples
00374             if (s->is_float && ((p - data) & 1)) {
00375                 uint32_t neg = *p & 0x80000000;
00376                 uint32_t hi = *(p - 1);
00377                 uint32_t lo = abs(*p) - 1;
00378 
00379                 hi += (hi || lo) ? 0x3f80 : 0;
00380                 // SWAP16: swap all the 16 bits
00381                 *(p - 1) = (hi << 16) | SWAP16(lo) | neg;
00382             }
00383 #endif
00384 
00385             /*if ((get_bits_count(&s->gb)+7)/8 > buf_size)
00386             {
00387                 av_log(NULL, AV_LOG_INFO, "overread!!\n");
00388                 break;
00389             }*/
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->is_float && (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         skip_bits(&s->gb, 32); // frame crc
00406 
00407         // convert to output buffer
00408         switch(s->bps) {
00409             case 2: {
00410                 uint16_t *samples = data;
00411                 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
00412 //                    *samples++ = (unsigned char)*p;
00413 //                    *samples++ = (unsigned char)(*p >> 8);
00414                     *samples++ = *p;
00415                 }
00416                 *data_size = (uint8_t *)samples - (uint8_t *)data;
00417                 break;
00418             }
00419             default:
00420                 av_log(s->avctx, AV_LOG_ERROR, "Error, only 16bit samples supported!\n");
00421         }
00422     }
00423 
00424 //    return get_bits_count(&s->gb)+7)/8;
00425     return buf_size;
00426 }
00427 
00428 static av_cold int tta_decode_close(AVCodecContext *avctx) {
00429     TTAContext *s = avctx->priv_data;
00430 
00431     if (s->decode_buffer)
00432         av_free(s->decode_buffer);
00433 
00434     return 0;
00435 }
00436 
00437 AVCodec tta_decoder = {
00438     "tta",
00439     CODEC_TYPE_AUDIO,
00440     CODEC_ID_TTA,
00441     sizeof(TTAContext),
00442     tta_decode_init,
00443     NULL,
00444     tta_decode_close,
00445     tta_decode_frame,
00446     .long_name = NULL_IF_CONFIG_SMALL("True Audio (TTA)"),
00447 };

Generated on Tue Nov 4 2014 12:59:23 for ffmpeg by  doxygen 1.7.1