• 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 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 "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 const int64_t tta_channel_layouts[7] = {
00209     AV_CH_LAYOUT_STEREO,
00210     AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
00211     AV_CH_LAYOUT_QUAD,
00212     0,
00213     AV_CH_LAYOUT_5POINT1_BACK,
00214     AV_CH_LAYOUT_5POINT1_BACK|AV_CH_BACK_CENTER,
00215     AV_CH_LAYOUT_7POINT1_WIDE
00216 };
00217 
00218 static av_cold int tta_decode_init(AVCodecContext * avctx)
00219 {
00220     TTAContext *s = avctx->priv_data;
00221     int i;
00222 
00223     s->avctx = avctx;
00224 
00225     // 30bytes includes a seektable with one frame
00226     if (avctx->extradata_size < 30)
00227         return -1;
00228 
00229     init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8);
00230     if (show_bits_long(&s->gb, 32) == AV_RL32("TTA1"))
00231     {
00232         /* signature */
00233         skip_bits(&s->gb, 32);
00234 //        if (get_bits_long(&s->gb, 32) != av_bswap32(AV_RL32("TTA1"))) {
00235 //            av_log(s->avctx, AV_LOG_ERROR, "Missing magic\n");
00236 //            return -1;
00237 //        }
00238 
00239         s->flags = get_bits(&s->gb, 16);
00240         if (s->flags != 1 && s->flags != 3)
00241         {
00242             av_log(s->avctx, AV_LOG_ERROR, "Invalid flags\n");
00243             return -1;
00244         }
00245         s->is_float = (s->flags == FORMAT_FLOAT);
00246         avctx->channels = s->channels = get_bits(&s->gb, 16);
00247         if (s->channels > 1 && s->channels < 9)
00248             avctx->channel_layout = tta_channel_layouts[s->channels-2];
00249         avctx->bits_per_coded_sample = get_bits(&s->gb, 16);
00250         s->bps = (avctx->bits_per_coded_sample + 7) / 8;
00251         avctx->sample_rate = get_bits_long(&s->gb, 32);
00252         if(avctx->sample_rate > 1000000){ //prevent FRAME_TIME * avctx->sample_rate from overflowing and sanity check
00253             av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
00254             return -1;
00255         }
00256         s->data_length = get_bits_long(&s->gb, 32);
00257         skip_bits(&s->gb, 32); // CRC32 of header
00258 
00259         if (s->is_float)
00260         {
00261             avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00262             av_log_ask_for_sample(s->avctx, "Unsupported sample format.\n");
00263             return -1;
00264         }
00265         else switch(s->bps) {
00266             case 1: avctx->sample_fmt = AV_SAMPLE_FMT_U8; break;
00267             case 2: avctx->sample_fmt = AV_SAMPLE_FMT_S16; break;
00268             case 3: avctx->bits_per_coded_sample = 24;
00269             case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break;
00270             default:
00271                 av_log_ask_for_sample(s->avctx,
00272                                       "Invalid/unsupported sample format.\n");
00273                 return -1;
00274         }
00275 
00276         // FIXME: horribly broken, but directly from reference source
00277 #define FRAME_TIME 1.04489795918367346939
00278         s->frame_length = (int)(FRAME_TIME * avctx->sample_rate);
00279 
00280         s->last_frame_length = s->data_length % s->frame_length;
00281         s->total_frames = s->data_length / s->frame_length +
00282                         (s->last_frame_length ? 1 : 0);
00283 
00284         av_log(s->avctx, AV_LOG_DEBUG, "flags: %x chans: %d bps: %d rate: %d block: %d\n",
00285             s->flags, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
00286             avctx->block_align);
00287         av_log(s->avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
00288             s->data_length, s->frame_length, s->last_frame_length, s->total_frames);
00289 
00290         // FIXME: seek table
00291         for (i = 0; i < s->total_frames; i++)
00292             skip_bits(&s->gb, 32);
00293         skip_bits(&s->gb, 32); // CRC32 of seektable
00294 
00295         if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){
00296             av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
00297             return -1;
00298         }
00299 
00300         s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
00301         if (!s->decode_buffer)
00302             return AVERROR(ENOMEM);
00303         s->ch_ctx = av_malloc(avctx->channels * sizeof(*s->ch_ctx));
00304         if (!s->ch_ctx)
00305             return AVERROR(ENOMEM);
00306     } else {
00307         av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
00308         return -1;
00309     }
00310 
00311     return 0;
00312 }
00313 
00314 static int tta_decode_frame(AVCodecContext *avctx,
00315         void *data, int *data_size,
00316         AVPacket *avpkt)
00317 {
00318     const uint8_t *buf = avpkt->data;
00319     int buf_size = avpkt->size;
00320     TTAContext *s = avctx->priv_data;
00321     int i;
00322 
00323     init_get_bits(&s->gb, buf, buf_size*8);
00324     {
00325         int cur_chan = 0, framelen = s->frame_length;
00326         int32_t *p;
00327 
00328         if (*data_size < (framelen * s->channels * av_get_bits_per_sample_fmt(avctx->sample_fmt) / 8)) {
00329             av_log(avctx, AV_LOG_ERROR, "Output buffer size is too small.\n");
00330             return -1;
00331         }
00332         // FIXME: seeking
00333         s->total_frames--;
00334         if (!s->total_frames && s->last_frame_length)
00335             framelen = s->last_frame_length;
00336 
00337         // init per channel states
00338         for (i = 0; i < s->channels; i++) {
00339             s->ch_ctx[i].predictor = 0;
00340             ttafilter_init(&s->ch_ctx[i].filter, ttafilter_configs[s->bps-1][0], ttafilter_configs[s->bps-1][1]);
00341             rice_init(&s->ch_ctx[i].rice, 10, 10);
00342         }
00343 
00344         for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
00345             int32_t *predictor = &s->ch_ctx[cur_chan].predictor;
00346             TTAFilter *filter = &s->ch_ctx[cur_chan].filter;
00347             TTARice *rice = &s->ch_ctx[cur_chan].rice;
00348             uint32_t unary, depth, k;
00349             int32_t value;
00350 
00351             unary = tta_get_unary(&s->gb);
00352 
00353             if (unary == 0) {
00354                 depth = 0;
00355                 k = rice->k0;
00356             } else {
00357                 depth = 1;
00358                 k = rice->k1;
00359                 unary--;
00360             }
00361 
00362             if (get_bits_left(&s->gb) < k)
00363                 return -1;
00364 
00365             if (k) {
00366                 if (k > MIN_CACHE_BITS)
00367                     return -1;
00368                 value = (unary << k) + get_bits(&s->gb, k);
00369             } else
00370                 value = unary;
00371 
00372             // FIXME: copy paste from original
00373             switch (depth) {
00374             case 1:
00375                 rice->sum1 += value - (rice->sum1 >> 4);
00376                 if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1])
00377                     rice->k1--;
00378                 else if(rice->sum1 > shift_16[rice->k1 + 1])
00379                     rice->k1++;
00380                 value += shift_1[rice->k0];
00381             default:
00382                 rice->sum0 += value - (rice->sum0 >> 4);
00383                 if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0])
00384                     rice->k0--;
00385                 else if(rice->sum0 > shift_16[rice->k0 + 1])
00386                     rice->k0++;
00387             }
00388 
00389             // extract coded value
00390 #define UNFOLD(x) (((x)&1) ? (++(x)>>1) : (-(x)>>1))
00391             *p = UNFOLD(value);
00392 
00393             // run hybrid filter
00394             ttafilter_process(filter, p, 0);
00395 
00396             // fixed order prediction
00397 #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
00398             switch (s->bps) {
00399                 case 1: *p += PRED(*predictor, 4); break;
00400                 case 2:
00401                 case 3: *p += PRED(*predictor, 5); break;
00402                 case 4: *p += *predictor; break;
00403             }
00404             *predictor = *p;
00405 
00406 #if 0
00407             // extract 32bit float from last two int samples
00408             if (s->is_float && ((p - data) & 1)) {
00409                 uint32_t neg = *p & 0x80000000;
00410                 uint32_t hi = *(p - 1);
00411                 uint32_t lo = abs(*p) - 1;
00412 
00413                 hi += (hi || lo) ? 0x3f80 : 0;
00414                 // SWAP16: swap all the 16 bits
00415                 *(p - 1) = (hi << 16) | SWAP16(lo) | neg;
00416             }
00417 #endif
00418 
00419             /*if ((get_bits_count(&s->gb)+7)/8 > buf_size)
00420             {
00421                 av_log(NULL, AV_LOG_INFO, "overread!!\n");
00422                 break;
00423             }*/
00424 
00425             // flip channels
00426             if (cur_chan < (s->channels-1))
00427                 cur_chan++;
00428             else {
00429                 // decorrelate in case of stereo integer
00430                 if (!s->is_float && (s->channels > 1)) {
00431                     int32_t *r = p - 1;
00432                     for (*p += *r / 2; r > p - s->channels; r--)
00433                         *r = *(r + 1) - *r;
00434                 }
00435                 cur_chan = 0;
00436             }
00437         }
00438 
00439         if (get_bits_left(&s->gb) < 32)
00440             return -1;
00441         skip_bits(&s->gb, 32); // frame crc
00442 
00443         // convert to output buffer
00444         switch(s->bps) {
00445             case 1: {
00446                 uint8_t *samples = data;
00447                 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
00448                     *samples++ = *p + 0x80;
00449                 *data_size = samples - (uint8_t *)data;
00450                 break;
00451             }
00452             case 2: {
00453                 uint16_t *samples = data;
00454                 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
00455 //                    *samples++ = (unsigned char)*p;
00456 //                    *samples++ = (unsigned char)(*p >> 8);
00457                     *samples++ = *p;
00458                 }
00459                 *data_size = (uint8_t *)samples - (uint8_t *)data;
00460                 break;
00461             }
00462             case 3: {
00463                 int32_t *samples = data;
00464                 for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
00465                     *samples++ = AV_RN32(p) << 8;
00466                 *data_size = (uint8_t *)samples - (uint8_t *)data;
00467                 break;
00468             }
00469             default:
00470                 av_log(s->avctx, AV_LOG_ERROR, "Error, only 16bit samples supported!\n");
00471         }
00472     }
00473 
00474 //    return get_bits_count(&s->gb)+7)/8;
00475     return buf_size;
00476 }
00477 
00478 static av_cold int tta_decode_close(AVCodecContext *avctx) {
00479     TTAContext *s = avctx->priv_data;
00480 
00481     av_free(s->decode_buffer);
00482     av_freep(&s->ch_ctx);
00483 
00484     return 0;
00485 }
00486 
00487 AVCodec ff_tta_decoder = {
00488     "tta",
00489     AVMEDIA_TYPE_AUDIO,
00490     CODEC_ID_TTA,
00491     sizeof(TTAContext),
00492     tta_decode_init,
00493     NULL,
00494     tta_decode_close,
00495     tta_decode_frame,
00496     .long_name = NULL_IF_CONFIG_SMALL("True Audio (TTA)"),
00497 };

Generated on Wed Apr 11 2012 07:31:35 for FFmpeg by  doxygen 1.7.1