LCOV - code coverage report
Current view: top level - libavcodec - dstdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 162 0.0 %
Date: 2017-12-15 11:05:35 Functions: 0 10 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Direct Stream Transfer (DST) decoder
       3             :  * Copyright (c) 2014 Peter Ross <pross@xvid.org>
       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             :  * Direct Stream Transfer (DST) decoder
      25             :  * ISO/IEC 14496-3 Part 3 Subpart 10: Technical description of lossless coding of oversampled audio
      26             :  */
      27             : 
      28             : #include "libavutil/avassert.h"
      29             : #include "libavutil/intreadwrite.h"
      30             : #include "internal.h"
      31             : #include "get_bits.h"
      32             : #include "avcodec.h"
      33             : #include "golomb.h"
      34             : #include "mathops.h"
      35             : #include "dsd.h"
      36             : 
      37             : #define DST_MAX_CHANNELS 6
      38             : #define DST_MAX_ELEMENTS (2 * DST_MAX_CHANNELS)
      39             : 
      40             : #define DSD_FS44(sample_rate) (sample_rate * 8 / 44100)
      41             : 
      42             : #define DST_SAMPLES_PER_FRAME(sample_rate) (588 * DSD_FS44(sample_rate))
      43             : 
      44             : static const int8_t fsets_code_pred_coeff[3][3] = {
      45             :     {  -8 },
      46             :     { -16,  8 },
      47             :     {  -9, -5, 6 },
      48             : };
      49             : 
      50             : static const int8_t probs_code_pred_coeff[3][3] = {
      51             :     {  -8 },
      52             :     { -16,  8 },
      53             :     { -24, 24, -8 },
      54             : };
      55             : 
      56             : typedef struct ArithCoder {
      57             :     unsigned int a;
      58             :     unsigned int c;
      59             : } ArithCoder;
      60             : 
      61             : typedef struct Table {
      62             :     unsigned int elements;
      63             :     unsigned int length[DST_MAX_ELEMENTS];
      64             :     int coeff[DST_MAX_ELEMENTS][128];
      65             : } Table;
      66             : 
      67             : typedef struct DSTContext {
      68             :     AVClass *class;
      69             : 
      70             :     GetBitContext gb;
      71             :     ArithCoder ac;
      72             :     Table fsets, probs;
      73             :     DECLARE_ALIGNED(64, uint8_t, status)[DST_MAX_CHANNELS][16];
      74             :     DECLARE_ALIGNED(16, int16_t, filter)[DST_MAX_ELEMENTS][16][256];
      75             :     DSDContext dsdctx[DST_MAX_CHANNELS];
      76             : } DSTContext;
      77             : 
      78           0 : static av_cold int decode_init(AVCodecContext *avctx)
      79             : {
      80           0 :     DSTContext *s = avctx->priv_data;
      81             :     int i;
      82             : 
      83           0 :     if (avctx->channels > DST_MAX_CHANNELS) {
      84           0 :         avpriv_request_sample(avctx, "Channel count %d", avctx->channels);
      85           0 :         return AVERROR_PATCHWELCOME;
      86             :     }
      87             : 
      88           0 :     avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
      89             : 
      90           0 :     for (i = 0; i < avctx->channels; i++)
      91           0 :         memset(s->dsdctx[i].buf, 0x69, sizeof(s->dsdctx[i].buf));
      92             : 
      93           0 :     ff_init_dsd_data();
      94             : 
      95           0 :     return 0;
      96             : }
      97             : 
      98           0 : static int read_map(GetBitContext *gb, Table *t, unsigned int map[DST_MAX_CHANNELS], int channels)
      99             : {
     100             :     int ch;
     101           0 :     t->elements = 1;
     102           0 :     map[0] = 0;
     103           0 :     if (!get_bits1(gb)) {
     104           0 :         for (ch = 1; ch < channels; ch++) {
     105           0 :             int bits = av_log2(t->elements) + 1;
     106           0 :             map[ch] = get_bits(gb, bits);
     107           0 :             if (map[ch] == t->elements) {
     108           0 :                 t->elements++;
     109           0 :                 if (t->elements >= DST_MAX_ELEMENTS)
     110           0 :                     return AVERROR_INVALIDDATA;
     111           0 :             } else if (map[ch] > t->elements) {
     112           0 :                 return AVERROR_INVALIDDATA;
     113             :             }
     114             :         }
     115             :     } else {
     116           0 :         memset(map, 0, sizeof(*map) * DST_MAX_CHANNELS);
     117             :     }
     118           0 :     return 0;
     119             : }
     120             : 
     121           0 : static av_always_inline int get_sr_golomb_dst(GetBitContext *gb, unsigned int k)
     122             : {
     123           0 :     int v = get_ur_golomb(gb, k, get_bits_left(gb), 0);
     124           0 :     if (v && get_bits1(gb))
     125           0 :         v = -v;
     126           0 :     return v;
     127             : }
     128             : 
     129           0 : static void read_uncoded_coeff(GetBitContext *gb, int *dst, unsigned int elements,
     130             :                                int coeff_bits, int is_signed, int offset)
     131             : {
     132             :     int i;
     133             : 
     134           0 :     for (i = 0; i < elements; i++) {
     135           0 :         dst[i] = (is_signed ? get_sbits(gb, coeff_bits) : get_bits(gb, coeff_bits)) + offset;
     136             :     }
     137           0 : }
     138             : 
     139           0 : static int read_table(GetBitContext *gb, Table *t, const int8_t code_pred_coeff[3][3],
     140             :                       int length_bits, int coeff_bits, int is_signed, int offset)
     141             : {
     142             :     unsigned int i, j, k;
     143           0 :     for (i = 0; i < t->elements; i++) {
     144           0 :         t->length[i] = get_bits(gb, length_bits) + 1;
     145           0 :         if (!get_bits1(gb)) {
     146           0 :             read_uncoded_coeff(gb, t->coeff[i], t->length[i], coeff_bits, is_signed, offset);
     147             :         } else {
     148           0 :             int method = get_bits(gb, 2), lsb_size;
     149           0 :             if (method == 3)
     150           0 :                 return AVERROR_INVALIDDATA;
     151             : 
     152           0 :             read_uncoded_coeff(gb, t->coeff[i], method + 1, coeff_bits, is_signed, offset);
     153             : 
     154           0 :             lsb_size  = get_bits(gb, 3);
     155           0 :             for (j = method + 1; j < t->length[i]; j++) {
     156           0 :                 int c, x = 0;
     157           0 :                 for (k = 0; k < method + 1; k++)
     158           0 :                     x += code_pred_coeff[method][k] * t->coeff[i][j - k - 1];
     159           0 :                 c = get_sr_golomb_dst(gb, lsb_size);
     160           0 :                 if (x >= 0)
     161           0 :                     c -= (x + 4) / 8;
     162             :                 else
     163           0 :                     c += (-x + 3) / 8;
     164           0 :                 t->coeff[i][j] = c;
     165             :             }
     166             :         }
     167             :     }
     168           0 :     return 0;
     169             : }
     170             : 
     171           0 : static void ac_init(ArithCoder *ac, GetBitContext *gb)
     172             : {
     173           0 :     ac->a = 4095;
     174           0 :     ac->c = get_bits(gb, 12);
     175           0 : }
     176             : 
     177           0 : static av_always_inline void ac_get(ArithCoder *ac, GetBitContext *gb, int p, int *e)
     178             : {
     179           0 :     unsigned int k = (ac->a >> 8) | ((ac->a >> 7) & 1);
     180           0 :     unsigned int q = k * p;
     181           0 :     unsigned int a_q = ac->a - q;
     182             : 
     183           0 :     *e = ac->c < a_q;
     184           0 :     if (*e) {
     185           0 :         ac->a  = a_q;
     186             :     } else {
     187           0 :         ac->a  = q;
     188           0 :         ac->c -= a_q;
     189             :     }
     190             : 
     191           0 :     if (ac->a < 2048) {
     192           0 :         int n = 11 - av_log2(ac->a);
     193           0 :         ac->a <<= n;
     194           0 :         ac->c = (ac->c << n) | get_bits(gb, n);
     195             :     }
     196           0 : }
     197             : 
     198           0 : static uint8_t prob_dst_x_bit(int c)
     199             : {
     200           0 :     return (ff_reverse[c & 127] >> 1) + 1;
     201             : }
     202             : 
     203           0 : static void build_filter(int16_t table[DST_MAX_ELEMENTS][16][256], const Table *fsets)
     204             : {
     205             :     int i, j, k, l;
     206             : 
     207           0 :     for (i = 0; i < fsets->elements; i++) {
     208           0 :         int length = fsets->length[i];
     209             : 
     210           0 :         for (j = 0; j < 16; j++) {
     211           0 :             int total = av_clip(length - j * 8, 0, 8);
     212             : 
     213           0 :             for (k = 0; k < 256; k++) {
     214           0 :                 int v = 0;
     215             : 
     216           0 :                 for (l = 0; l < total; l++)
     217           0 :                     v += (((k >> l) & 1) * 2 - 1) * fsets->coeff[i][j * 8 + l];
     218           0 :                 table[i][j][k] = v;
     219             :             }
     220             :         }
     221             :     }
     222           0 : }
     223             : 
     224           0 : static int decode_frame(AVCodecContext *avctx, void *data,
     225             :                         int *got_frame_ptr, AVPacket *avpkt)
     226             : {
     227           0 :     unsigned samples_per_frame = DST_SAMPLES_PER_FRAME(avctx->sample_rate);
     228             :     unsigned map_ch_to_felem[DST_MAX_CHANNELS];
     229             :     unsigned map_ch_to_pelem[DST_MAX_CHANNELS];
     230             :     unsigned i, ch, same_map, dst_x_bit;
     231             :     unsigned half_prob[DST_MAX_CHANNELS];
     232           0 :     const int channels = avctx->channels;
     233           0 :     DSTContext *s = avctx->priv_data;
     234           0 :     GetBitContext *gb = &s->gb;
     235           0 :     ArithCoder *ac = &s->ac;
     236           0 :     AVFrame *frame = data;
     237             :     uint8_t *dsd;
     238             :     float *pcm;
     239             :     int ret;
     240             : 
     241           0 :     if (avpkt->size <= 1)
     242           0 :         return AVERROR_INVALIDDATA;
     243             : 
     244           0 :     frame->nb_samples = samples_per_frame / 8;
     245           0 :     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
     246           0 :         return ret;
     247           0 :     dsd = frame->data[0];
     248           0 :     pcm = (float *)frame->data[0];
     249             : 
     250           0 :     if ((ret = init_get_bits8(gb, avpkt->data, avpkt->size)) < 0)
     251           0 :         return ret;
     252             : 
     253           0 :     if (!get_bits1(gb)) {
     254           0 :         skip_bits1(gb);
     255           0 :         if (get_bits(gb, 6))
     256           0 :             return AVERROR_INVALIDDATA;
     257           0 :         memcpy(frame->data[0], avpkt->data + 1, FFMIN(avpkt->size - 1, frame->nb_samples * avctx->channels));
     258           0 :         goto dsd;
     259             :     }
     260             : 
     261             :     /* Segmentation (10.4, 10.5, 10.6) */
     262             : 
     263           0 :     if (!get_bits1(gb)) {
     264           0 :         avpriv_request_sample(avctx, "Not Same Segmentation");
     265           0 :         return AVERROR_PATCHWELCOME;
     266             :     }
     267             : 
     268           0 :     if (!get_bits1(gb)) {
     269           0 :         avpriv_request_sample(avctx, "Not Same Segmentation For All Channels");
     270           0 :         return AVERROR_PATCHWELCOME;
     271             :     }
     272             : 
     273           0 :     if (!get_bits1(gb)) {
     274           0 :         avpriv_request_sample(avctx, "Not End Of Channel Segmentation");
     275           0 :         return AVERROR_PATCHWELCOME;
     276             :     }
     277             : 
     278             :     /* Mapping (10.7, 10.8, 10.9) */
     279             : 
     280           0 :     same_map = get_bits1(gb);
     281             : 
     282           0 :     if ((ret = read_map(gb, &s->fsets, map_ch_to_felem, avctx->channels)) < 0)
     283           0 :         return ret;
     284             : 
     285           0 :     if (same_map) {
     286           0 :         s->probs.elements = s->fsets.elements;
     287           0 :         memcpy(map_ch_to_pelem, map_ch_to_felem, sizeof(map_ch_to_felem));
     288             :     } else {
     289           0 :         avpriv_request_sample(avctx, "Not Same Mapping");
     290           0 :         if ((ret = read_map(gb, &s->probs, map_ch_to_pelem, avctx->channels)) < 0)
     291           0 :             return ret;
     292             :     }
     293             : 
     294             :     /* Half Probability (10.10) */
     295             : 
     296           0 :     for (ch = 0; ch < avctx->channels; ch++)
     297           0 :         half_prob[ch] = get_bits1(gb);
     298             : 
     299             :     /* Filter Coef Sets (10.12) */
     300             : 
     301           0 :     read_table(gb, &s->fsets, fsets_code_pred_coeff, 7, 9, 1, 0);
     302             : 
     303             :     /* Probability Tables (10.13) */
     304             : 
     305           0 :     read_table(gb, &s->probs, probs_code_pred_coeff, 6, 7, 0, 1);
     306             : 
     307             :     /* Arithmetic Coded Data (10.11) */
     308             : 
     309           0 :     if (get_bits1(gb))
     310           0 :         return AVERROR_INVALIDDATA;
     311           0 :     ac_init(ac, gb);
     312             : 
     313           0 :     build_filter(s->filter, &s->fsets);
     314             : 
     315           0 :     memset(s->status, 0xAA, sizeof(s->status));
     316           0 :     memset(dsd, 0, frame->nb_samples * 4 * avctx->channels);
     317             : 
     318           0 :     ac_get(ac, gb, prob_dst_x_bit(s->fsets.coeff[0][0]), &dst_x_bit);
     319             : 
     320           0 :     for (i = 0; i < samples_per_frame; i++) {
     321           0 :         for (ch = 0; ch < channels; ch++) {
     322           0 :             const unsigned felem = map_ch_to_felem[ch];
     323           0 :             int16_t (*filter)[256] = s->filter[felem];
     324           0 :             uint8_t *status = s->status[ch];
     325             :             int prob, residual, v;
     326             : 
     327             : #define F(x) filter[(x)][status[(x)]]
     328           0 :             const int16_t predict = F( 0) + F( 1) + F( 2) + F( 3) +
     329           0 :                                     F( 4) + F( 5) + F( 6) + F( 7) +
     330           0 :                                     F( 8) + F( 9) + F(10) + F(11) +
     331           0 :                                     F(12) + F(13) + F(14) + F(15);
     332             : #undef F
     333             : 
     334           0 :             if (!half_prob[ch] || i >= s->fsets.length[felem]) {
     335           0 :                 unsigned pelem = map_ch_to_pelem[ch];
     336           0 :                 unsigned index = FFABS(predict) >> 3;
     337           0 :                 prob = s->probs.coeff[pelem][FFMIN(index, s->probs.length[pelem] - 1)];
     338             :             } else {
     339           0 :                 prob = 128;
     340             :             }
     341             : 
     342           0 :             ac_get(ac, gb, prob, &residual);
     343           0 :             v = ((predict >> 15) ^ residual) & 1;
     344           0 :             dsd[((i >> 3) * channels + ch) << 2] |= v << (7 - (i & 0x7 ));
     345             : 
     346           0 :             AV_WN64A(status + 8, (AV_RN64A(status + 8) << 1) | ((AV_RN64A(status) >> 63) & 1));
     347           0 :             AV_WN64A(status, (AV_RN64A(status) << 1) | v);
     348             :         }
     349             :     }
     350             : 
     351           0 : dsd:
     352           0 :     for (i = 0; i < avctx->channels; i++) {
     353           0 :         ff_dsd2pcm_translate(&s->dsdctx[i], frame->nb_samples, 0,
     354           0 :                              frame->data[0] + i * 4,
     355           0 :                              avctx->channels * 4, pcm + i, avctx->channels);
     356             :     }
     357             : 
     358           0 :     *got_frame_ptr = 1;
     359             : 
     360           0 :     return avpkt->size;
     361             : }
     362             : 
     363             : AVCodec ff_dst_decoder = {
     364             :     .name           = "dst",
     365             :     .long_name      = NULL_IF_CONFIG_SMALL("DST (Digital Stream Transfer)"),
     366             :     .type           = AVMEDIA_TYPE_AUDIO,
     367             :     .id             = AV_CODEC_ID_DST,
     368             :     .priv_data_size = sizeof(DSTContext),
     369             :     .init           = decode_init,
     370             :     .decode         = decode_frame,
     371             :     .capabilities   = AV_CODEC_CAP_DR1,
     372             :     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
     373             :                                                       AV_SAMPLE_FMT_NONE },
     374             : };

Generated by: LCOV version 1.13