LCOV - code coverage report
Current view: top level - libavcodec - ttaenc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 87 105 82.9 %
Date: 2017-12-18 06:23:41 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :  * TTA (The Lossless True Audio) encoder
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU Lesser General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2.1 of the License, or (at your option) any later version.
      10             :  *
      11             :  * FFmpeg is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with FFmpeg; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : #define BITSTREAM_WRITER_LE
      22             : #include "ttadata.h"
      23             : #include "ttaencdsp.h"
      24             : #include "avcodec.h"
      25             : #include "put_bits.h"
      26             : #include "internal.h"
      27             : #include "libavutil/crc.h"
      28             : 
      29             : typedef struct TTAEncContext {
      30             :     const AVCRC *crc_table;
      31             :     int bps;
      32             :     TTAChannel *ch_ctx;
      33             :     TTAEncDSPContext dsp;
      34             : } TTAEncContext;
      35             : 
      36           3 : static av_cold int tta_encode_init(AVCodecContext *avctx)
      37             : {
      38           3 :     TTAEncContext *s = avctx->priv_data;
      39             : 
      40           3 :     s->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
      41             : 
      42           3 :     switch (avctx->sample_fmt) {
      43           0 :     case AV_SAMPLE_FMT_U8:
      44           0 :         avctx->bits_per_raw_sample = 8;
      45           0 :         break;
      46           3 :     case AV_SAMPLE_FMT_S16:
      47           3 :         avctx->bits_per_raw_sample = 16;
      48           3 :         break;
      49           0 :     case AV_SAMPLE_FMT_S32:
      50           0 :         if (avctx->bits_per_raw_sample > 24)
      51           0 :             av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
      52           0 :         avctx->bits_per_raw_sample = 24;
      53             :     }
      54             : 
      55           3 :     s->bps = avctx->bits_per_raw_sample >> 3;
      56           3 :     avctx->frame_size = 256 * avctx->sample_rate / 245;
      57             : 
      58           3 :     s->ch_ctx = av_malloc_array(avctx->channels, sizeof(*s->ch_ctx));
      59           3 :     if (!s->ch_ctx)
      60           0 :         return AVERROR(ENOMEM);
      61             : 
      62           3 :     ff_ttaencdsp_init(&s->dsp);
      63             : 
      64           3 :     return 0;
      65             : }
      66             : 
      67      882000 : static int32_t get_sample(const AVFrame *frame, int sample,
      68             :                           enum AVSampleFormat format)
      69             : {
      70             :     int32_t ret;
      71             : 
      72      882000 :     if (format == AV_SAMPLE_FMT_U8) {
      73           0 :         ret = frame->data[0][sample] - 0x80;
      74      882000 :     } else if (format == AV_SAMPLE_FMT_S16) {
      75      882000 :         const int16_t *ptr = (const int16_t *)frame->data[0];
      76      882000 :         ret = ptr[sample];
      77             :     } else {
      78           0 :         const int32_t *ptr = (const int32_t *)frame->data[0];
      79           0 :         ret = ptr[sample] >> 8;
      80             :     }
      81             : 
      82      882000 :     return ret;
      83             : }
      84             : 
      85           8 : static int tta_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
      86             :                             const AVFrame *frame, int *got_packet_ptr)
      87             : {
      88           8 :     TTAEncContext *s = avctx->priv_data;
      89             :     PutBitContext pb;
      90             :     int ret, i, out_bytes, cur_chan, res, samples;
      91           8 :     int64_t pkt_size =  frame->nb_samples * 2LL * avctx->channels * s->bps;
      92             : 
      93           8 : pkt_alloc:
      94           8 :     cur_chan = 0, res = 0, samples = 0;
      95           8 :     if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size, 0)) < 0)
      96           0 :         return ret;
      97           8 :     init_put_bits(&pb, avpkt->data, avpkt->size);
      98             : 
      99             :     // init per channel states
     100          22 :     for (i = 0; i < avctx->channels; i++) {
     101          14 :         s->ch_ctx[i].predictor = 0;
     102          14 :         ff_tta_filter_init(&s->ch_ctx[i].filter, ff_tta_filter_configs[s->bps - 1]);
     103          14 :         ff_tta_rice_init(&s->ch_ctx[i].rice, 10, 10);
     104             :     }
     105             : 
     106      617408 :     for (i = 0; i < frame->nb_samples * avctx->channels; i++) {
     107      617400 :         TTAChannel *c = &s->ch_ctx[cur_chan];
     108      617400 :         TTAFilter *filter = &c->filter;
     109      617400 :         TTARice *rice = &c->rice;
     110             :         uint32_t k, unary, outval;
     111             :         int32_t value, temp;
     112             : 
     113      617400 :         value = get_sample(frame, samples++, avctx->sample_fmt);
     114             : 
     115      617400 :         if (avctx->channels > 1) {
     116      529200 :             if (cur_chan < avctx->channels - 1)
     117      264600 :                 value  = res = get_sample(frame, samples, avctx->sample_fmt) - value;
     118             :             else
     119      264600 :                 value -= res / 2;
     120             :         }
     121             : 
     122      617400 :         temp = value;
     123             : #define PRED(x, k) (int32_t)((((uint64_t)(x) << (k)) - (x)) >> (k))
     124      617400 :         switch (s->bps) {
     125           0 :         case 1: value -= PRED(c->predictor, 4); break;
     126      617400 :         case 2:
     127      617400 :         case 3: value -= PRED(c->predictor, 5); break;
     128             :         }
     129      617400 :         c->predictor = temp;
     130             : 
     131      617400 :         s->dsp.filter_process(filter->qm, filter->dx, filter->dl, &filter->error, &value,
     132             :                               filter->shift, filter->round);
     133      617400 :         outval = (value > 0) ? (value << 1) - 1: -value << 1;
     134             : 
     135      617400 :         k = rice->k0;
     136             : 
     137      617400 :         rice->sum0 += outval - (rice->sum0 >> 4);
     138      617400 :         if (rice->k0 > 0 && rice->sum0 < ff_tta_shift_16[rice->k0])
     139       17694 :             rice->k0--;
     140      599706 :         else if (rice->sum0 > ff_tta_shift_16[rice->k0 + 1])
     141       17600 :             rice->k0++;
     142             : 
     143      617400 :         if (outval >= ff_tta_shift_1[k]) {
     144      257183 :             outval -= ff_tta_shift_1[k];
     145      257183 :             k = rice->k1;
     146             : 
     147      257183 :             rice->sum1 += outval - (rice->sum1 >> 4);
     148      257183 :             if (rice->k1 > 0 && rice->sum1 < ff_tta_shift_16[rice->k1])
     149        7967 :                 rice->k1--;
     150      249216 :             else if (rice->sum1 > ff_tta_shift_16[rice->k1 + 1])
     151        7889 :                 rice->k1++;
     152             : 
     153      257183 :             unary = 1 + (outval >> k);
     154      257183 :             if (unary + 100LL > put_bits_left(&pb)) {
     155           0 :                 if (pkt_size < INT_MAX/2) {
     156           0 :                     pkt_size *= 2;
     157           0 :                     av_packet_unref(avpkt);
     158           0 :                     goto pkt_alloc;
     159             :                 } else
     160           0 :                     return AVERROR(ENOMEM);
     161             :             }
     162             :             do {
     163      257763 :                 if (unary > 31) {
     164         580 :                     put_bits(&pb, 31, 0x7FFFFFFF);
     165         580 :                     unary -= 31;
     166             :                 } else {
     167      257183 :                     put_bits(&pb, unary, (1 << unary) - 1);
     168      257183 :                     unary = 0;
     169             :                 }
     170      257763 :             } while (unary);
     171             :         }
     172             : 
     173      617400 :         put_bits(&pb, 1, 0);
     174             : 
     175      617400 :         if (k)
     176      481630 :             put_bits(&pb, k, outval & (ff_tta_shift_1[k] - 1));
     177             : 
     178      617400 :         if (cur_chan < avctx->channels - 1)
     179      264600 :             cur_chan++;
     180             :         else
     181      352800 :             cur_chan = 0;
     182             :     }
     183             : 
     184           8 :     flush_put_bits(&pb);
     185           8 :     out_bytes = put_bits_count(&pb) >> 3;
     186           8 :     put_bits32(&pb, av_crc(s->crc_table, UINT32_MAX, avpkt->data, out_bytes) ^ UINT32_MAX);
     187           8 :     flush_put_bits(&pb);
     188             : 
     189           8 :     avpkt->pts      = frame->pts;
     190           8 :     avpkt->size     = out_bytes + 4;
     191           8 :     avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
     192           8 :     *got_packet_ptr = 1;
     193           8 :     return 0;
     194             : }
     195             : 
     196           3 : static av_cold int tta_encode_close(AVCodecContext *avctx)
     197             : {
     198           3 :     TTAEncContext *s = avctx->priv_data;
     199           3 :     av_freep(&s->ch_ctx);
     200           3 :     return 0;
     201             : }
     202             : 
     203             : AVCodec ff_tta_encoder = {
     204             :     .name           = "tta",
     205             :     .long_name      = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
     206             :     .type           = AVMEDIA_TYPE_AUDIO,
     207             :     .id             = AV_CODEC_ID_TTA,
     208             :     .priv_data_size = sizeof(TTAEncContext),
     209             :     .init           = tta_encode_init,
     210             :     .close          = tta_encode_close,
     211             :     .encode2        = tta_encode_frame,
     212             :     .capabilities   = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_LOSSLESS,
     213             :     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_U8,
     214             :                                                      AV_SAMPLE_FMT_S16,
     215             :                                                      AV_SAMPLE_FMT_S32,
     216             :                                                      AV_SAMPLE_FMT_NONE },
     217             : };

Generated by: LCOV version 1.13