LCOV - code coverage report
Current view: top level - libavcodec - tta.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 140 207 67.6 %
Date: 2017-12-16 13:57:32 Functions: 5 7 71.4 %

          Line data    Source code
       1             : /*
       2             :  * TTA (The Lossless True Audio) decoder
       3             :  * Copyright (c) 2006 Alex Beregszaszi
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : /**
      23             :  * @file
      24             :  * TTA (The Lossless True Audio) decoder
      25             :  * @see http://www.true-audio.com/
      26             :  * @see http://tta.corecodec.org/
      27             :  * @author Alex Beregszaszi
      28             :  */
      29             : 
      30             : #include <limits.h>
      31             : 
      32             : #include "libavutil/crc.h"
      33             : #include "libavutil/intreadwrite.h"
      34             : #include "libavutil/opt.h"
      35             : 
      36             : #define BITSTREAM_READER_LE
      37             : #include "ttadata.h"
      38             : #include "ttadsp.h"
      39             : #include "avcodec.h"
      40             : #include "get_bits.h"
      41             : #include "thread.h"
      42             : #include "unary.h"
      43             : #include "internal.h"
      44             : 
      45             : #define FORMAT_SIMPLE    1
      46             : #define FORMAT_ENCRYPTED 2
      47             : 
      48             : typedef struct TTAContext {
      49             :     AVClass *class;
      50             :     AVCodecContext *avctx;
      51             :     const AVCRC *crc_table;
      52             : 
      53             :     int format, channels, bps;
      54             :     unsigned data_length;
      55             :     int frame_length, last_frame_length;
      56             : 
      57             :     int32_t *decode_buffer;
      58             : 
      59             :     uint8_t crc_pass[8];
      60             :     uint8_t *pass;
      61             :     TTAChannel *ch_ctx;
      62             :     TTADSPContext dsp;
      63             : } TTAContext;
      64             : 
      65             : static const int64_t tta_channel_layouts[7] = {
      66             :     AV_CH_LAYOUT_STEREO,
      67             :     AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY,
      68             :     AV_CH_LAYOUT_QUAD,
      69             :     0,
      70             :     AV_CH_LAYOUT_5POINT1_BACK,
      71             :     AV_CH_LAYOUT_5POINT1_BACK|AV_CH_BACK_CENTER,
      72             :     AV_CH_LAYOUT_7POINT1_WIDE
      73             : };
      74             : 
      75           0 : static int tta_check_crc(TTAContext *s, const uint8_t *buf, int buf_size)
      76             : {
      77             :     uint32_t crc, CRC;
      78             : 
      79           0 :     CRC = AV_RL32(buf + buf_size);
      80           0 :     crc = av_crc(s->crc_table, 0xFFFFFFFFU, buf, buf_size);
      81           0 :     if (CRC != (crc ^ 0xFFFFFFFFU)) {
      82           0 :         av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
      83           0 :         return AVERROR_INVALIDDATA;
      84             :     }
      85             : 
      86           0 :     return 0;
      87             : }
      88             : 
      89           2 : static uint64_t tta_check_crc64(uint8_t *pass)
      90             : {
      91           2 :     uint64_t crc = UINT64_MAX, poly = 0x42F0E1EBA9EA3693U;
      92           2 :     uint8_t *end = pass + strlen(pass);
      93             :     int i;
      94             : 
      95          16 :     while (pass < end) {
      96          12 :         crc ^= (uint64_t)*pass++ << 56;
      97         108 :         for (i = 0; i < 8; i++)
      98          96 :             crc = (crc << 1) ^ (poly & (((int64_t) crc) >> 63));
      99             :     }
     100             : 
     101           2 :     return crc ^ UINT64_MAX;
     102             : }
     103             : 
     104          10 : static int allocate_buffers(AVCodecContext *avctx)
     105             : {
     106          10 :     TTAContext *s = avctx->priv_data;
     107             : 
     108          10 :     if (s->bps < 3) {
     109          10 :         s->decode_buffer = av_mallocz_array(sizeof(int32_t)*s->frame_length, s->channels);
     110          10 :         if (!s->decode_buffer)
     111           0 :             return AVERROR(ENOMEM);
     112             :     } else
     113           0 :         s->decode_buffer = NULL;
     114          10 :     s->ch_ctx = av_malloc_array(avctx->channels, sizeof(*s->ch_ctx));
     115          10 :     if (!s->ch_ctx) {
     116           0 :         av_freep(&s->decode_buffer);
     117           0 :         return AVERROR(ENOMEM);
     118             :     }
     119             : 
     120          10 :     return 0;
     121             : }
     122             : 
     123          10 : static av_cold int tta_decode_init(AVCodecContext * avctx)
     124             : {
     125          10 :     TTAContext *s = avctx->priv_data;
     126             :     GetBitContext gb;
     127             :     int total_frames;
     128             :     int ret;
     129             : 
     130          10 :     s->avctx = avctx;
     131             : 
     132             :     // 30bytes includes TTA1 header
     133          10 :     if (avctx->extradata_size < 22)
     134           0 :         return AVERROR_INVALIDDATA;
     135             : 
     136          10 :     s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
     137          10 :     ret = init_get_bits8(&gb, avctx->extradata, avctx->extradata_size);
     138          10 :     if (ret < 0)
     139           0 :         return ret;
     140             : 
     141          10 :     if (show_bits_long(&gb, 32) == AV_RL32("TTA1")) {
     142             :         /* signature */
     143          10 :         skip_bits_long(&gb, 32);
     144             : 
     145          10 :         s->format = get_bits(&gb, 16);
     146          10 :         if (s->format > 2) {
     147           0 :             av_log(avctx, AV_LOG_ERROR, "Invalid format\n");
     148           0 :             return AVERROR_INVALIDDATA;
     149             :         }
     150          10 :         if (s->format == FORMAT_ENCRYPTED) {
     151           2 :             if (!s->pass) {
     152           0 :                 av_log(avctx, AV_LOG_ERROR, "Missing password for encrypted stream. Please use the -password option\n");
     153           0 :                 return AVERROR(EINVAL);
     154             :             }
     155           2 :             AV_WL64(s->crc_pass, tta_check_crc64(s->pass));
     156             :         }
     157          10 :         avctx->channels = s->channels = get_bits(&gb, 16);
     158          10 :         if (s->channels > 1 && s->channels < 9)
     159           6 :             avctx->channel_layout = tta_channel_layouts[s->channels-2];
     160          10 :         avctx->bits_per_raw_sample = get_bits(&gb, 16);
     161          10 :         s->bps = (avctx->bits_per_raw_sample + 7) / 8;
     162          10 :         avctx->sample_rate = get_bits_long(&gb, 32);
     163          10 :         s->data_length = get_bits_long(&gb, 32);
     164          10 :         skip_bits_long(&gb, 32); // CRC32 of header
     165             : 
     166          10 :         if (s->channels == 0) {
     167           0 :             av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
     168           0 :             return AVERROR_INVALIDDATA;
     169          10 :         } else if (avctx->sample_rate == 0) {
     170           0 :             av_log(avctx, AV_LOG_ERROR, "Invalid samplerate\n");
     171           0 :             return AVERROR_INVALIDDATA;
     172             :         }
     173             : 
     174          10 :         switch(s->bps) {
     175           0 :         case 1: avctx->sample_fmt = AV_SAMPLE_FMT_U8; break;
     176          10 :         case 2:
     177          10 :             avctx->sample_fmt = AV_SAMPLE_FMT_S16;
     178          10 :             break;
     179           0 :         case 3:
     180           0 :             avctx->sample_fmt = AV_SAMPLE_FMT_S32;
     181           0 :             break;
     182             :         //case 4: avctx->sample_fmt = AV_SAMPLE_FMT_S32; break;
     183           0 :         default:
     184           0 :             av_log(avctx, AV_LOG_ERROR, "Invalid/unsupported sample format.\n");
     185           0 :             return AVERROR_INVALIDDATA;
     186             :         }
     187             : 
     188             :         // prevent overflow
     189          10 :         if (avctx->sample_rate > 0x7FFFFFu) {
     190           0 :             av_log(avctx, AV_LOG_ERROR, "sample_rate too large\n");
     191           0 :             return AVERROR(EINVAL);
     192             :         }
     193          10 :         s->frame_length = 256 * avctx->sample_rate / 245;
     194             : 
     195          10 :         s->last_frame_length = s->data_length % s->frame_length;
     196          20 :         total_frames = s->data_length / s->frame_length +
     197          10 :                        (s->last_frame_length ? 1 : 0);
     198             : 
     199          10 :         av_log(avctx, AV_LOG_DEBUG, "format: %d chans: %d bps: %d rate: %d block: %d\n",
     200             :             s->format, avctx->channels, avctx->bits_per_coded_sample, avctx->sample_rate,
     201             :             avctx->block_align);
     202          10 :         av_log(avctx, AV_LOG_DEBUG, "data_length: %d frame_length: %d last: %d total: %d\n",
     203             :             s->data_length, s->frame_length, s->last_frame_length, total_frames);
     204             : 
     205          10 :         if(s->frame_length >= UINT_MAX / (s->channels * sizeof(int32_t))){
     206           0 :             av_log(avctx, AV_LOG_ERROR, "frame_length too large\n");
     207           0 :             return AVERROR_INVALIDDATA;
     208             :         }
     209             :     } else {
     210           0 :         av_log(avctx, AV_LOG_ERROR, "Wrong extradata present\n");
     211           0 :         return AVERROR_INVALIDDATA;
     212             :     }
     213             : 
     214          10 :     ff_ttadsp_init(&s->dsp);
     215             : 
     216          10 :     return allocate_buffers(avctx);
     217             : }
     218             : 
     219          28 : static int tta_decode_frame(AVCodecContext *avctx, void *data,
     220             :                             int *got_frame_ptr, AVPacket *avpkt)
     221             : {
     222          28 :     AVFrame *frame     = data;
     223          28 :     ThreadFrame tframe = { .f = data };
     224          28 :     const uint8_t *buf = avpkt->data;
     225          28 :     int buf_size = avpkt->size;
     226          28 :     TTAContext *s = avctx->priv_data;
     227             :     GetBitContext gb;
     228             :     int i, ret;
     229          28 :     int cur_chan = 0, framelen = s->frame_length;
     230             :     int32_t *p;
     231             : 
     232          28 :     if (avctx->err_recognition & AV_EF_CRCCHECK) {
     233           0 :         if (buf_size < 4 ||
     234           0 :             (tta_check_crc(s, buf, buf_size - 4) && avctx->err_recognition & AV_EF_EXPLODE))
     235           0 :             return AVERROR_INVALIDDATA;
     236             :     }
     237             : 
     238          28 :     if ((ret = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
     239           0 :         return ret;
     240             : 
     241             :     /* get output buffer */
     242          28 :     frame->nb_samples = framelen;
     243          28 :     if ((ret = ff_thread_get_buffer(avctx, &tframe, 0)) < 0)
     244           0 :         return ret;
     245             : 
     246             :     // decode directly to output buffer for 24-bit sample format
     247          28 :     if (s->bps == 3)
     248           0 :         s->decode_buffer = (int32_t *)frame->data[0];
     249             : 
     250             :     // init per channel states
     251          82 :     for (i = 0; i < s->channels; i++) {
     252          54 :         TTAFilter *filter = &s->ch_ctx[i].filter;
     253          54 :         s->ch_ctx[i].predictor = 0;
     254          54 :         ff_tta_filter_init(filter, ff_tta_filter_configs[s->bps-1]);
     255          54 :         if (s->format == FORMAT_ENCRYPTED) {
     256             :             int i;
     257         144 :             for (i = 0; i < 8; i++)
     258         128 :                 filter->qm[i] = sign_extend(s->crc_pass[i], 8);
     259             :         }
     260          54 :         ff_tta_rice_init(&s->ch_ctx[i].rice, 10, 10);
     261             :     }
     262             : 
     263          28 :     i = 0;
     264     2318737 :     for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++) {
     265     2318714 :         int32_t *predictor = &s->ch_ctx[cur_chan].predictor;
     266     2318714 :         TTAFilter *filter = &s->ch_ctx[cur_chan].filter;
     267     2318714 :         TTARice *rice = &s->ch_ctx[cur_chan].rice;
     268             :         uint32_t unary, depth, k;
     269             :         int32_t value;
     270             : 
     271     2318714 :         unary = get_unary(&gb, 0, get_bits_left(&gb));
     272             : 
     273     2318714 :         if (unary == 0) {
     274     1098815 :             depth = 0;
     275     1098815 :             k = rice->k0;
     276             :         } else {
     277     1219899 :             depth = 1;
     278     1219899 :             k = rice->k1;
     279     1219899 :             unary--;
     280             :         }
     281             : 
     282     2318714 :         if (get_bits_left(&gb) < k) {
     283           0 :             ret = AVERROR_INVALIDDATA;
     284           0 :             goto error;
     285             :         }
     286             : 
     287     2318714 :         if (k) {
     288     2172994 :             if (k > MIN_CACHE_BITS || unary > INT32_MAX >> k) {
     289           0 :                 ret = AVERROR_INVALIDDATA;
     290           0 :                 goto error;
     291             :             }
     292     2172994 :             value = (unary << k) + get_bits(&gb, k);
     293             :         } else
     294      145720 :             value = unary;
     295             : 
     296             :         // FIXME: copy paste from original
     297     2318714 :         switch (depth) {
     298     1219899 :         case 1:
     299     1219899 :             rice->sum1 += value - (rice->sum1 >> 4);
     300     1219899 :             if (rice->k1 > 0 && rice->sum1 < ff_tta_shift_16[rice->k1])
     301       33717 :                 rice->k1--;
     302     1186182 :             else if(rice->sum1 > ff_tta_shift_16[rice->k1 + 1])
     303       33488 :                 rice->k1++;
     304     1219899 :             value += ff_tta_shift_1[rice->k0];
     305     2318714 :         default:
     306     2318714 :             rice->sum0 += value - (rice->sum0 >> 4);
     307     2318714 :             if (rice->k0 > 0 && rice->sum0 < ff_tta_shift_16[rice->k0])
     308       64196 :                 rice->k0--;
     309     2254518 :             else if(rice->sum0 > ff_tta_shift_16[rice->k0 + 1])
     310       63963 :                 rice->k0++;
     311             :         }
     312             : 
     313             :         // extract coded value
     314     2318714 :         *p = 1 + ((value >> 1) ^ ((value & 1) - 1));
     315             : 
     316             :         // run hybrid filter
     317     2318714 :         s->dsp.filter_process(filter->qm, filter->dx, filter->dl, &filter->error, p,
     318             :                               filter->shift, filter->round);
     319             : 
     320             :         // fixed order prediction
     321             : #define PRED(x, k) (int32_t)((((uint64_t)(x) << (k)) - (x)) >> (k))
     322     2318714 :         switch (s->bps) {
     323           0 :         case 1: *p += PRED(*predictor, 4); break;
     324     2318714 :         case 2:
     325     2318714 :         case 3: *p += PRED(*predictor, 5); break;
     326           0 :         case 4: *p +=      *predictor;     break;
     327             :         }
     328     2318714 :         *predictor = *p;
     329             : 
     330             :         // flip channels
     331     2318714 :         if (cur_chan < (s->channels-1))
     332     1115257 :             cur_chan++;
     333             :         else {
     334             :             // decorrelate in case of multiple channels
     335     1203457 :             if (s->channels > 1) {
     336     1115257 :                 int32_t *r = p - 1;
     337     2230514 :                 for (*p += *r / 2; r > p - s->channels; r--)
     338     1115257 :                     *r = *(r + 1) - *r;
     339             :             }
     340     1203457 :             cur_chan = 0;
     341     1203457 :             i++;
     342             :             // check for last frame
     343     1203457 :             if (i == s->last_frame_length && get_bits_left(&gb) / 8 == 4) {
     344           5 :                 frame->nb_samples = framelen = s->last_frame_length;
     345           5 :                 break;
     346             :             }
     347             :         }
     348             :     }
     349             : 
     350          28 :     align_get_bits(&gb);
     351          28 :     if (get_bits_left(&gb) < 32) {
     352           0 :         ret = AVERROR_INVALIDDATA;
     353           0 :         goto error;
     354             :     }
     355          28 :     skip_bits_long(&gb, 32); // frame crc
     356             : 
     357             :     // convert to output buffer
     358          28 :     switch (s->bps) {
     359           0 :     case 1: {
     360           0 :         uint8_t *samples = (uint8_t *)frame->data[0];
     361           0 :         for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
     362           0 :             *samples++ = *p + 0x80;
     363           0 :         break;
     364             :         }
     365          28 :     case 2: {
     366          28 :         int16_t *samples = (int16_t *)frame->data[0];
     367     2318742 :         for (p = s->decode_buffer; p < s->decode_buffer + (framelen * s->channels); p++)
     368     2318714 :             *samples++ = *p;
     369          28 :         break;
     370             :         }
     371           0 :     case 3: {
     372             :         // shift samples for 24-bit sample format
     373           0 :         int32_t *samples = (int32_t *)frame->data[0];
     374           0 :         for (i = 0; i < framelen * s->channels; i++)
     375           0 :             *samples++ <<= 8;
     376             :         // reset decode buffer
     377           0 :         s->decode_buffer = NULL;
     378           0 :         break;
     379             :         }
     380             :     }
     381             : 
     382          28 :     *got_frame_ptr = 1;
     383             : 
     384          28 :     return buf_size;
     385           0 : error:
     386             :     // reset decode buffer
     387           0 :     if (s->bps == 3)
     388           0 :         s->decode_buffer = NULL;
     389           0 :     return ret;
     390             : }
     391             : 
     392           0 : static int init_thread_copy(AVCodecContext *avctx)
     393             : {
     394           0 :     TTAContext *s = avctx->priv_data;
     395           0 :     s->avctx = avctx;
     396           0 :     return allocate_buffers(avctx);
     397             : }
     398             : 
     399          10 : static av_cold int tta_decode_close(AVCodecContext *avctx) {
     400          10 :     TTAContext *s = avctx->priv_data;
     401             : 
     402          10 :     if (s->bps < 3)
     403          10 :         av_freep(&s->decode_buffer);
     404          10 :     s->decode_buffer = NULL;
     405          10 :     av_freep(&s->ch_ctx);
     406             : 
     407          10 :     return 0;
     408             : }
     409             : 
     410             : #define OFFSET(x) offsetof(TTAContext, x)
     411             : #define DEC (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
     412             : static const AVOption options[] = {
     413             :     { "password", "Set decoding password", OFFSET(pass), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, DEC },
     414             :     { NULL },
     415             : };
     416             : 
     417             : static const AVClass tta_decoder_class = {
     418             :     .class_name = "TTA Decoder",
     419             :     .item_name  = av_default_item_name,
     420             :     .option     = options,
     421             :     .version    = LIBAVUTIL_VERSION_INT,
     422             : };
     423             : 
     424             : AVCodec ff_tta_decoder = {
     425             :     .name           = "tta",
     426             :     .long_name      = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
     427             :     .type           = AVMEDIA_TYPE_AUDIO,
     428             :     .id             = AV_CODEC_ID_TTA,
     429             :     .priv_data_size = sizeof(TTAContext),
     430             :     .init           = tta_decode_init,
     431             :     .close          = tta_decode_close,
     432             :     .decode         = tta_decode_frame,
     433             :     .init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy),
     434             :     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
     435             :     .priv_class     = &tta_decoder_class,
     436             : };

Generated by: LCOV version 1.13