libavcodec/tta.c
a24c4a2a
 /*
  * TTA (The Lossless True Audio) decoder
  * Copyright (c) 2006 Alex Beregszaszi
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
a24c4a2a
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
a24c4a2a
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
a24c4a2a
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
e5a389a1
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
a24c4a2a
  */
 
 /**
ba87f080
  * @file
a24c4a2a
  * TTA (The Lossless True Audio) decoder
ad4cd0c2
  * @see http://www.true-audio.com/
  * @see http://tta.corecodec.org/
a24c4a2a
  * @author Alex Beregszaszi
  */
 
aaf47bcd
 #define BITSTREAM_READER_LE
a24c4a2a
 //#define DEBUG
 #include <limits.h>
 #include "avcodec.h"
9106a698
 #include "get_bits.h"
594d4d5d
 #include "internal.h"
2af3dc86
 #include "libavutil/crc.h"
a24c4a2a
 
b16960a8
 #define FORMAT_SIMPLE    1
 #define FORMAT_ENCRYPTED 2
a24c4a2a
 
5113b3bd
 #define MAX_ORDER 16
 typedef struct TTAFilter {
323b9da9
     int32_t shift, round, error;
5113b3bd
     int32_t qm[MAX_ORDER];
     int32_t dx[MAX_ORDER];
     int32_t dl[MAX_ORDER];
 } TTAFilter;
 
 typedef struct TTARice {
     uint32_t k0, k1, sum0, sum1;
 } TTARice;
 
 typedef struct TTAChannel {
     int32_t predictor;
     TTAFilter filter;
     TTARice rice;
 } TTAChannel;
 
a24c4a2a
 typedef struct TTAContext {
     AVCodecContext *avctx;
0eea2129
     AVFrame frame;
a24c4a2a
     GetBitContext gb;
2af3dc86
     const AVCRC *crc_table;
a24c4a2a
 
ac80b812
     int format, channels, bps;
     unsigned data_length;
5778299c
     int frame_length, last_frame_length;
a24c4a2a
 
79a32e3b
     int32_t *decode_buffer;
5113b3bd
 
c058dc22
     TTAChannel *ch_ctx;
a24c4a2a
 } TTAContext;
 
79a32e3b
 static const uint32_t shift_1[] = {
a24c4a2a
     0x00000001, 0x00000002, 0x00000004, 0x00000008,
     0x00000010, 0x00000020, 0x00000040, 0x00000080,
     0x00000100, 0x00000200, 0x00000400, 0x00000800,
     0x00001000, 0x00002000, 0x00004000, 0x00008000,
     0x00010000, 0x00020000, 0x00040000, 0x00080000,
     0x00100000, 0x00200000, 0x00400000, 0x00800000,
     0x01000000, 0x02000000, 0x04000000, 0x08000000,
     0x10000000, 0x20000000, 0x40000000, 0x80000000,
     0x80000000, 0x80000000, 0x80000000, 0x80000000,
     0x80000000, 0x80000000, 0x80000000, 0x80000000
 };
 
cf2baeb3
 static const uint32_t * const shift_16 = shift_1 + 4;
a24c4a2a
 
323b9da9
 static const int32_t ttafilter_configs[4] = {
     10,
     9,
     10,
     12
a24c4a2a
 };
 
323b9da9
 static void ttafilter_init(TTAFilter *c, int32_t shift) {
a24c4a2a
     memset(c, 0, sizeof(TTAFilter));
     c->shift = shift;
    c->round = shift_1[shift-1];
 //    c->round = 1 << (shift - 1);
 }
 
323b9da9
 static inline void ttafilter_process(TTAFilter *c, int32_t *in)
 {
79a32e3b
     register int32_t *dl = c->dl, *qm = c->qm, *dx = c->dx, sum = c->round;
a24c4a2a
 
15e07348
     if (c->error < 0) {
         qm[0] -= dx[0]; qm[1] -= dx[1]; qm[2] -= dx[2]; qm[3] -= dx[3];
         qm[4] -= dx[4]; qm[5] -= dx[5]; qm[6] -= dx[6]; qm[7] -= dx[7];
     } else if (c->error > 0) {
         qm[0] += dx[0]; qm[1] += dx[1]; qm[2] += dx[2]; qm[3] += dx[3];
         qm[4] += dx[4]; qm[5] += dx[5]; qm[6] += dx[6]; qm[7] += dx[7];
a24c4a2a
     }
 
15e07348
     sum += dl[0] * qm[0] + dl[1] * qm[1] + dl[2] * qm[2] + dl[3] * qm[3] +
            dl[4] * qm[4] + dl[5] * qm[5] + dl[6] * qm[6] + dl[7] * qm[7];
 
     dx[0] = dx[1]; dx[1] = dx[2]; dx[2] = dx[3]; dx[3] = dx[4];
     dl[0] = dl[1]; dl[1] = dl[2]; dl[2] = dl[3]; dl[3] = dl[4];
 
     dx[4] = ((dl[4] >> 30) | 1);
     dx[5] = ((dl[5] >> 30) | 2) & ~1;
     dx[6] = ((dl[6] >> 30) | 2) & ~1;
     dx[7] = ((dl[7] >> 30) | 4) & ~3;
a24c4a2a
 
323b9da9
     c->error = *in;
     *in += (sum >> c->shift);
a24c4a2a
 
15e07348
     dl[4] = -dl[5]; dl[5] = -dl[6];
     dl[6] = *in - dl[7]; dl[7] = *in;
     dl[5] += dl[6]; dl[4] += dl[5];
a24c4a2a
 }
 
79a32e3b
 static void rice_init(TTARice *c, uint32_t k0, uint32_t k1)
a24c4a2a
 {
     c->k0 = k0;
     c->k1 = k1;
     c->sum0 = shift_16[k0];
     c->sum1 = shift_16[k1];
 }
 
 static int tta_get_unary(GetBitContext *gb)
 {
     int ret = 0;
 
     // count ones
35f9d8c2
     while (get_bits_left(gb) > 0 && get_bits1(gb))
a24c4a2a
         ret++;
     return ret;
 }
 
d804784d
 static const int64_t tta_channel_layouts[7] = {
     AV_CH_LAYOUT_STEREO,
     AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
     AV_CH_LAYOUT_QUAD,
     0,
     AV_CH_LAYOUT_5POINT1_BACK,
     AV_CH_LAYOUT_5POINT1_BACK|AV_CH_BACK_CENTER,
     AV_CH_LAYOUT_7POINT1_WIDE
 };
 
2af3dc86
 static int tta_check_crc(TTAContext *s, const uint8_t *buf, int buf_size)
 {
     uint32_t crc, CRC;
 
     CRC = AV_RL32(buf + buf_size);
     crc = av_crc(s->crc_table, 0xFFFFFFFFU, buf, buf_size);
     if (CRC != (crc ^ 0xFFFFFFFFU)) {
         av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
         return AVERROR_INVALIDDATA;
     }
 
     return 0;
 }
 
98a6fff9
 static av_cold int tta_decode_init(AVCodecContext * avctx)
a24c4a2a
 {
     TTAContext *s = avctx->priv_data;
5778299c
     int total_frames;
a24c4a2a
 
     s->avctx = avctx;
 
     // 30bytes includes a seektable with one frame
     if (avctx->extradata_size < 30)
1ade37ae
         return AVERROR_INVALIDDATA;
a24c4a2a
 
e5e0580b
     init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8);
2bb6eba2
     if (show_bits_long(&s->gb, 32) == AV_RL32("TTA1"))
a24c4a2a
     {
2af3dc86
         if (avctx->err_recognition & AV_EF_CRCCHECK) {
             s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
ea1d64ab
             tta_check_crc(s, avctx->extradata, 18);
2af3dc86
         }
 
a24c4a2a
         /* signature */
9aff2d17
         skip_bits_long(&s->gb, 32);
a24c4a2a
 
b16960a8
         s->format = get_bits(&s->gb, 16);
         if (s->format > 2) {
             av_log(s->avctx, AV_LOG_ERROR, "Invalid format\n");
1ade37ae
             return AVERROR_INVALIDDATA;
a24c4a2a
         }
b16960a8
         if (s->format == FORMAT_ENCRYPTED) {
             av_log_missing_feature(s->avctx, "Encrypted TTA", 0);
717addec
             return AVERROR_PATCHWELCOME;
b16960a8
         }
1a174c28
         avctx->channels = s->channels = get_bits(&s->gb, 16);
d804784d
         if (s->channels > 1 && s->channels < 9)
             avctx->channel_layout = tta_channel_layouts[s->channels-2];
4ebf3059
         avctx->bits_per_raw_sample = get_bits(&s->gb, 16);
         s->bps = (avctx->bits_per_raw_sample + 7) / 8;
1a174c28
         avctx->sample_rate = get_bits_long(&s->gb, 32);
         s->data_length = get_bits_long(&s->gb, 32);
9aff2d17
         skip_bits_long(&s->gb, 32); // CRC32 of header
a24c4a2a
 
8bd1f1a4
         if (s->channels == 0) {
             av_log(s->avctx, AV_LOG_ERROR, "Invalid number of channels\n");
             return AVERROR_INVALIDDATA;
7416d610
         } else if (avctx->sample_rate == 0) {
             av_log(s->avctx, AV_LOG_ERROR, "Invalid samplerate\n");
             return AVERROR_INVALIDDATA;
8bd1f1a4
         }
 
b16960a8
         switch(s->bps) {
51bfaa21
         case 1: avctx->sample_fmt = AV_SAMPLE_FMT_U8; break;
c6056d40
         case 2:
             avctx->sample_fmt = AV_SAMPLE_FMT_S16;
             break;
         case 3:
             avctx->sample_fmt = AV_SAMPLE_FMT_S32;
             break;
51bfaa21
         //case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break;
c6056d40
         default:
             av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported sample format.\n");
             return AVERROR_INVALIDDATA;
a24c4a2a
         }
 
b5050539
         // prevent overflow
ac80b812
         if (avctx->sample_rate > 0x7FFFFFu) {
b5050539
             av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
             return AVERROR(EINVAL);
         }
         s->frame_length = 256 * avctx->sample_rate / 245;
a24c4a2a
 
         s->last_frame_length = s->data_length % s->frame_length;
5778299c
         total_frames = s->data_length / s->frame_length +
                        (s->last_frame_length ? 1 : 0);
a24c4a2a
 
b16960a8
         av_log(s->avctx, AV_LOG_DEBUG, "format: %d chans: %d bps: %d rate: %d block: %d\n",
             s->format, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
a24c4a2a
             avctx->block_align);
         av_log(s->avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
5778299c
             s->data_length, s->frame_length, s->last_frame_length, total_frames);
936951ca
 
a24c4a2a
         // FIXME: seek table
5778299c
         if (avctx->extradata_size <= 26 || total_frames > INT_MAX / 4 ||
             avctx->extradata_size - 26 < total_frames * 4)
2af3dc86
             av_log(avctx, AV_LOG_WARNING, "Seek table missing or too small\n");
         else if (avctx->err_recognition & AV_EF_CRCCHECK) {
5778299c
             if (tta_check_crc(s, avctx->extradata + 22, total_frames * 4))
2af3dc86
                 return AVERROR_INVALIDDATA;
         }
5778299c
         skip_bits_long(&s->gb, 32 * total_frames);
9aff2d17
         skip_bits_long(&s->gb, 32); // CRC32 of seektable
a24c4a2a
 
3a1a7e32
         if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){
             av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
1ade37ae
             return AVERROR_INVALIDDATA;
3a1a7e32
         }
 
c6abf214
         if (s->bps < 3) {
             s->decode_buffer = av_mallocz(sizeof(int32_t)*s->frame_length*s->channels);
             if (!s->decode_buffer)
                 return AVERROR(ENOMEM);
ed83f972
         } else
             s->decode_buffer = NULL;
5113b3bd
         s->ch_ctx = av_malloc(avctx->channels * sizeof(*s->ch_ctx));
2f1d212f
         if (!s->ch_ctx) {
             av_freep(&s->decode_buffer);
5113b3bd
             return AVERROR(ENOMEM);
2f1d212f
         }
a24c4a2a
     } else {
         av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
1ade37ae
         return AVERROR_INVALIDDATA;
a24c4a2a
     }
 
0eea2129
     avcodec_get_frame_defaults(&s->frame);
     avctx->coded_frame = &s->frame;
 
a24c4a2a
     return 0;
 }
 
0eea2129
 static int tta_decode_frame(AVCodecContext *avctx, void *data,
                             int *got_frame_ptr, AVPacket *avpkt)
a24c4a2a
 {
7a00bbad
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
a24c4a2a
     TTAContext *s = avctx->priv_data;
0eea2129
     int i, ret;
7b7a74a1
     int cur_chan = 0, framelen = s->frame_length;
     int32_t *p;
a24c4a2a
 
2af3dc86
     if (avctx->err_recognition & AV_EF_CRCCHECK) {
         if (buf_size < 4 || tta_check_crc(s, buf, buf_size - 4))
             return AVERROR_INVALIDDATA;
     }
 
a24c4a2a
     init_get_bits(&s->gb, buf, buf_size*8);
 
0eea2129
     /* get output buffer */
     s->frame.nb_samples = framelen;
594d4d5d
     if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
0eea2129
         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
         return ret;
8664682d
     }
 
c6056d40
     // decode directly to output buffer for 24-bit sample format
     if (s->bps == 3)
01ed1c39
         s->decode_buffer = (int32_t *)s->frame.data[0];
c6056d40
 
8664682d
     // init per channel states
     for (i = 0; i < s->channels; i++) {
         s->ch_ctx[i].predictor = 0;
323b9da9
         ttafilter_init(&s->ch_ctx[i].filter, ttafilter_configs[s->bps-1]);
8664682d
         rice_init(&s->ch_ctx[i].rice, 10, 10);
     }
e6923f68
 
5778299c
     i = 0;
8664682d
     for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
         int32_t *predictor = &s->ch_ctx[cur_chan].predictor;
         TTAFilter *filter = &s->ch_ctx[cur_chan].filter;
         TTARice *rice = &s->ch_ctx[cur_chan].rice;
         uint32_t unary, depth, k;
         int32_t value;
 
         unary = tta_get_unary(&s->gb);
 
         if (unary == 0) {
             depth = 0;
             k = rice->k0;
         } else {
             depth = 1;
             k = rice->k1;
             unary--;
a24c4a2a
         }
 
6ab681a4
         if (get_bits_left(&s->gb) < k) {
             ret = AVERROR_INVALIDDATA;
             goto error;
         }
a24c4a2a
 
8664682d
         if (k) {
6ab681a4
             if (k > MIN_CACHE_BITS) {
                 ret = AVERROR_INVALIDDATA;
                 goto error;
             }
8664682d
             value = (unary << k) + get_bits(&s->gb, k);
         } else
             value = unary;
 
         // FIXME: copy paste from original
         switch (depth) {
         case 1:
             rice->sum1 += value - (rice->sum1 >> 4);
             if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1])
                 rice->k1--;
             else if(rice->sum1 > shift_16[rice->k1 + 1])
                 rice->k1++;
             value += shift_1[rice->k0];
         default:
             rice->sum0 += value - (rice->sum0 >> 4);
             if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0])
                 rice->k0--;
             else if(rice->sum0 > shift_16[rice->k0 + 1])
                 rice->k0++;
         }
a9837b58
 
8664682d
         // extract coded value
f2a4559c
         *p = 1 + ((value >> 1) ^ ((value & 1) - 1));
a24c4a2a
 
8664682d
         // run hybrid filter
323b9da9
         ttafilter_process(filter, p);
a24c4a2a
 
8664682d
         // fixed order prediction
79a32e3b
 #define PRED(x, k) (int32_t)((((uint64_t)x << k) - x) >> k)
8664682d
         switch (s->bps) {
e04ca1d4
         case 1: *p += PRED(*predictor, 4); break;
         case 2:
         case 3: *p += PRED(*predictor, 5); break;
         case 4: *p +=      *predictor;     break;
8664682d
         }
         *predictor = *p;
 
         // flip channels
         if (cur_chan < (s->channels-1))
             cur_chan++;
         else {
46ea4635
             // decorrelate in case of multiple channels
8664682d
             if (s->channels > 1) {
                 int32_t *r = p - 1;
                 for (*p += *r / 2; r > p - s->channels; r--)
                     *r = *(r + 1) - *r;
a24c4a2a
             }
8664682d
             cur_chan = 0;
5778299c
             i++;
             // check for last frame
             if (i == s->last_frame_length && get_bits_left(&s->gb) / 8 == 4) {
                 s->frame.nb_samples = framelen = s->last_frame_length;
                 break;
             }
a24c4a2a
         }
8664682d
     }
a24c4a2a
 
5778299c
     align_get_bits(&s->gb);
6ab681a4
     if (get_bits_left(&s->gb) < 32) {
         ret = AVERROR_INVALIDDATA;
         goto error;
     }
9aff2d17
     skip_bits_long(&s->gb, 32); // frame crc
a24c4a2a
 
07dbea70
     // convert to output buffer
     switch (s->bps) {
     case 1: {
         uint8_t *samples = (uint8_t *)s->frame.data[0];
         for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
             *samples++ = *p + 0x80;
         break;
a24c4a2a
         }
07dbea70
     case 2: {
01c9ffa9
         int16_t *samples = (int16_t *)s->frame.data[0];
07dbea70
         for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
             *samples++ = *p;
         break;
         }
     case 3: {
         // shift samples for 24-bit sample format
         int32_t *samples = (int32_t *)s->frame.data[0];
         for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
             *samples++ <<= 8;
         // reset decode buffer
         s->decode_buffer = NULL;
         break;
         }
     }
a24c4a2a
 
0eea2129
     *got_frame_ptr   = 1;
     *(AVFrame *)data = s->frame;
c6056d40
 
a24c4a2a
     return buf_size;
6ab681a4
 error:
     // reset decode buffer
     if (s->bps == 3)
         s->decode_buffer = NULL;
     return ret;
a24c4a2a
 }
 
98a6fff9
 static av_cold int tta_decode_close(AVCodecContext *avctx) {
a24c4a2a
     TTAContext *s = avctx->priv_data;
 
ed83f972
     if (s->bps < 3)
         av_free(s->decode_buffer);
     s->decode_buffer = NULL;
5113b3bd
     av_freep(&s->ch_ctx);
a24c4a2a
 
     return 0;
 }
 
e7e2df27
 AVCodec ff_tta_decoder = {
ec6402b7
     .name           = "tta",
     .type           = AVMEDIA_TYPE_AUDIO,
36ef5369
     .id             = AV_CODEC_ID_TTA,
ec6402b7
     .priv_data_size = sizeof(TTAContext),
     .init           = tta_decode_init,
     .close          = tta_decode_close,
     .decode         = tta_decode_frame,
0eea2129
     .capabilities   = CODEC_CAP_DR1,
0177b7d2
     .long_name      = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
a24c4a2a
 };