LCOV - code coverage report
Current view: top level - libavcodec - dcadec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 138 199 69.3 %
Date: 2017-12-16 13:57:32 Functions: 6 7 85.7 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2016 foo86
       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             : #include "libavutil/opt.h"
      22             : #include "libavutil/channel_layout.h"
      23             : 
      24             : #include "dcadec.h"
      25             : #include "dcahuff.h"
      26             : #include "dca_syncwords.h"
      27             : #include "profiles.h"
      28             : 
      29             : #define MIN_PACKET_SIZE     16
      30             : #define MAX_PACKET_SIZE     0x104000
      31             : 
      32        2475 : int ff_dca_set_channel_layout(AVCodecContext *avctx, int *ch_remap, int dca_mask)
      33             : {
      34             :     static const uint8_t dca2wav_norm[28] = {
      35             :          2,  0, 1, 9, 10,  3,  8,  4,  5,  9, 10, 6, 7, 12,
      36             :         13, 14, 3, 6,  7, 11, 12, 14, 16, 15, 17, 8, 4,  5,
      37             :     };
      38             : 
      39             :     static const uint8_t dca2wav_wide[28] = {
      40             :          2,  0, 1, 4,  5,  3,  8,  4,  5,  9, 10, 6, 7, 12,
      41             :         13, 14, 3, 9, 10, 11, 12, 14, 16, 15, 17, 8, 4,  5,
      42             :     };
      43             : 
      44        2475 :     int dca_ch, wav_ch, nchannels = 0;
      45             : 
      46        2475 :     if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE) {
      47           0 :         for (dca_ch = 0; dca_ch < DCA_SPEAKER_COUNT; dca_ch++)
      48           0 :             if (dca_mask & (1U << dca_ch))
      49           0 :                 ch_remap[nchannels++] = dca_ch;
      50           0 :         avctx->channel_layout = dca_mask;
      51             :     } else {
      52        2475 :         int wav_mask = 0;
      53             :         int wav_map[18];
      54             :         const uint8_t *dca2wav;
      55        2475 :         if (dca_mask == DCA_SPEAKER_LAYOUT_7POINT0_WIDE ||
      56             :             dca_mask == DCA_SPEAKER_LAYOUT_7POINT1_WIDE)
      57        1173 :             dca2wav = dca2wav_wide;
      58             :         else
      59        1302 :             dca2wav = dca2wav_norm;
      60       71775 :         for (dca_ch = 0; dca_ch < 28; dca_ch++) {
      61       69300 :             if (dca_mask & (1 << dca_ch)) {
      62       15375 :                 wav_ch = dca2wav[dca_ch];
      63       15375 :                 if (!(wav_mask & (1 << wav_ch))) {
      64       15375 :                     wav_map[wav_ch] = dca_ch;
      65       15375 :                     wav_mask |= 1 << wav_ch;
      66             :                 }
      67             :             }
      68             :         }
      69       47025 :         for (wav_ch = 0; wav_ch < 18; wav_ch++)
      70       44550 :             if (wav_mask & (1 << wav_ch))
      71       15375 :                 ch_remap[nchannels++] = wav_map[wav_ch];
      72        2475 :         avctx->channel_layout = wav_mask;
      73             :     }
      74             : 
      75        2475 :     avctx->channels = nchannels;
      76        2475 :     return nchannels;
      77             : }
      78             : 
      79           7 : void ff_dca_downmix_to_stereo_fixed(DCADSPContext *dcadsp, int32_t **samples,
      80             :                                     int *coeff_l, int nsamples, int ch_mask)
      81             : {
      82           7 :     int pos, spkr, max_spkr = av_log2(ch_mask);
      83           7 :     int *coeff_r = coeff_l + av_popcount(ch_mask);
      84             : 
      85           7 :     av_assert0(DCA_HAS_STEREO(ch_mask));
      86             : 
      87             :     // Scale left and right channels
      88           7 :     pos = (ch_mask & DCA_SPEAKER_MASK_C);
      89           7 :     dcadsp->dmix_scale(samples[DCA_SPEAKER_L], coeff_l[pos    ], nsamples);
      90           7 :     dcadsp->dmix_scale(samples[DCA_SPEAKER_R], coeff_r[pos + 1], nsamples);
      91             : 
      92             :     // Downmix remaining channels
      93          49 :     for (spkr = 0; spkr <= max_spkr; spkr++) {
      94          42 :         if (!(ch_mask & (1U << spkr)))
      95           0 :             continue;
      96             : 
      97          42 :         if (*coeff_l && spkr != DCA_SPEAKER_L)
      98           7 :             dcadsp->dmix_add(samples[DCA_SPEAKER_L], samples[spkr],
      99             :                              *coeff_l, nsamples);
     100             : 
     101          42 :         if (*coeff_r && spkr != DCA_SPEAKER_R)
     102           7 :             dcadsp->dmix_add(samples[DCA_SPEAKER_R], samples[spkr],
     103             :                              *coeff_r, nsamples);
     104             : 
     105          42 :         coeff_l++;
     106          42 :         coeff_r++;
     107             :     }
     108           7 : }
     109             : 
     110           7 : void ff_dca_downmix_to_stereo_float(AVFloatDSPContext *fdsp, float **samples,
     111             :                                     int *coeff_l, int nsamples, int ch_mask)
     112             : {
     113           7 :     int pos, spkr, max_spkr = av_log2(ch_mask);
     114           7 :     int *coeff_r = coeff_l + av_popcount(ch_mask);
     115           7 :     const float scale = 1.0f / (1 << 15);
     116             : 
     117           7 :     av_assert0(DCA_HAS_STEREO(ch_mask));
     118             : 
     119             :     // Scale left and right channels
     120           7 :     pos = (ch_mask & DCA_SPEAKER_MASK_C);
     121          14 :     fdsp->vector_fmul_scalar(samples[DCA_SPEAKER_L], samples[DCA_SPEAKER_L],
     122           7 :                              coeff_l[pos    ] * scale, nsamples);
     123          14 :     fdsp->vector_fmul_scalar(samples[DCA_SPEAKER_R], samples[DCA_SPEAKER_R],
     124           7 :                              coeff_r[pos + 1] * scale, nsamples);
     125             : 
     126             :     // Downmix remaining channels
     127          49 :     for (spkr = 0; spkr <= max_spkr; spkr++) {
     128          42 :         if (!(ch_mask & (1U << spkr)))
     129           0 :             continue;
     130             : 
     131          42 :         if (*coeff_l && spkr != DCA_SPEAKER_L)
     132          14 :             fdsp->vector_fmac_scalar(samples[DCA_SPEAKER_L], samples[spkr],
     133           7 :                                      *coeff_l * scale, nsamples);
     134             : 
     135          42 :         if (*coeff_r && spkr != DCA_SPEAKER_R)
     136          14 :             fdsp->vector_fmac_scalar(samples[DCA_SPEAKER_R], samples[spkr],
     137           7 :                                      *coeff_r * scale, nsamples);
     138             : 
     139          42 :         coeff_l++;
     140          42 :         coeff_r++;
     141             :     }
     142           7 : }
     143             : 
     144        2475 : static int dcadec_decode_frame(AVCodecContext *avctx, void *data,
     145             :                                int *got_frame_ptr, AVPacket *avpkt)
     146             : {
     147        2475 :     DCAContext *s = avctx->priv_data;
     148        2475 :     AVFrame *frame = data;
     149        2475 :     uint8_t *input = avpkt->data;
     150        2475 :     int input_size = avpkt->size;
     151        2475 :     int i, ret, prev_packet = s->packet;
     152             :     uint32_t mrk;
     153             : 
     154        2475 :     if (input_size < MIN_PACKET_SIZE || input_size > MAX_PACKET_SIZE) {
     155           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid packet size\n");
     156           0 :         return AVERROR_INVALIDDATA;
     157             :     }
     158             : 
     159             :     // Convert input to BE format
     160        2475 :     mrk = AV_RB32(input);
     161        2475 :     if (mrk != DCA_SYNCWORD_CORE_BE && mrk != DCA_SYNCWORD_SUBSTREAM) {
     162           0 :         av_fast_padded_malloc(&s->buffer, &s->buffer_size, input_size);
     163           0 :         if (!s->buffer)
     164           0 :             return AVERROR(ENOMEM);
     165             : 
     166           0 :         for (i = 0, ret = AVERROR_INVALIDDATA; i < input_size - MIN_PACKET_SIZE + 1 && ret < 0; i++)
     167           0 :             ret = avpriv_dca_convert_bitstream(input + i, input_size - i, s->buffer, s->buffer_size);
     168             : 
     169           0 :         if (ret < 0) {
     170           0 :             av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
     171           0 :             return ret;
     172             :         }
     173             : 
     174           0 :         input      = s->buffer;
     175           0 :         input_size = ret;
     176             :     }
     177             : 
     178        2475 :     s->packet = 0;
     179             : 
     180             :     // Parse backward compatible core sub-stream
     181        2475 :     if (AV_RB32(input) == DCA_SYNCWORD_CORE_BE) {
     182             :         int frame_size;
     183             : 
     184        2463 :         if ((ret = ff_dca_core_parse(&s->core, input, input_size)) < 0)
     185           0 :             return ret;
     186             : 
     187        2463 :         s->packet |= DCA_PACKET_CORE;
     188             : 
     189             :         // EXXS data must be aligned on 4-byte boundary
     190        2463 :         frame_size = FFALIGN(s->core.frame_size, 4);
     191        2463 :         if (input_size - 4 > frame_size) {
     192        1397 :             input      += frame_size;
     193        1397 :             input_size -= frame_size;
     194             :         }
     195             :     }
     196             : 
     197        2475 :     if (!s->core_only) {
     198        2475 :         DCAExssAsset *asset = NULL;
     199             : 
     200             :         // Parse extension sub-stream (EXSS)
     201        2475 :         if (AV_RB32(input) == DCA_SYNCWORD_SUBSTREAM) {
     202        1409 :             if ((ret = ff_dca_exss_parse(&s->exss, input, input_size)) < 0) {
     203           0 :                 if (avctx->err_recognition & AV_EF_EXPLODE)
     204           0 :                     return ret;
     205             :             } else {
     206        1409 :                 s->packet |= DCA_PACKET_EXSS;
     207        1409 :                 asset = &s->exss.assets[0];
     208             :             }
     209             :         }
     210             : 
     211             :         // Parse XLL component in EXSS
     212        2475 :         if (asset && (asset->extension_mask & DCA_EXSS_XLL)) {
     213        1353 :             if ((ret = ff_dca_xll_parse(&s->xll, input, asset)) < 0) {
     214             :                 // Conceal XLL synchronization error
     215           0 :                 if (ret == AVERROR(EAGAIN)
     216           0 :                     && (prev_packet & DCA_PACKET_XLL)
     217           0 :                     && (s->packet & DCA_PACKET_CORE))
     218           0 :                     s->packet |= DCA_PACKET_XLL | DCA_PACKET_RECOVERY;
     219           0 :                 else if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
     220           0 :                     return ret;
     221             :             } else {
     222        1353 :                 s->packet |= DCA_PACKET_XLL;
     223             :             }
     224             :         }
     225             : 
     226             :         // Parse LBR component in EXSS
     227        2475 :         if (asset && (asset->extension_mask & DCA_EXSS_LBR)) {
     228           0 :             if ((ret = ff_dca_lbr_parse(&s->lbr, input, asset)) < 0) {
     229           0 :                 if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
     230           0 :                     return ret;
     231             :             } else {
     232           0 :                 s->packet |= DCA_PACKET_LBR;
     233             :             }
     234             :         }
     235             : 
     236             :         // Parse core extensions in EXSS or backward compatible core sub-stream
     237        2475 :         if ((s->packet & DCA_PACKET_CORE)
     238        2463 :             && (ret = ff_dca_core_parse_exss(&s->core, input, asset)) < 0)
     239           0 :             return ret;
     240             :     }
     241             : 
     242             :     // Filter the frame
     243        2475 :     if (s->packet & DCA_PACKET_LBR) {
     244           0 :         if ((ret = ff_dca_lbr_filter_frame(&s->lbr, frame)) < 0)
     245           0 :             return ret;
     246        2475 :     } else if (s->packet & DCA_PACKET_XLL) {
     247        1353 :         if (s->packet & DCA_PACKET_CORE) {
     248        1341 :             int x96_synth = -1;
     249             : 
     250             :             // Enable X96 synthesis if needed
     251        1341 :             if (s->xll.chset[0].freq == 96000 && s->core.sample_rate == 48000)
     252          84 :                 x96_synth = 1;
     253             : 
     254        1341 :             if ((ret = ff_dca_core_filter_fixed(&s->core, x96_synth)) < 0)
     255           0 :                 return ret;
     256             : 
     257             :             // Force lossy downmixed output on the first core frame filtered.
     258             :             // This prevents audible clicks when seeking and is consistent with
     259             :             // what reference decoder does when there are multiple channel sets.
     260        1341 :             if (!(prev_packet & DCA_PACKET_RESIDUAL) && s->xll.nreschsets > 0
     261          50 :                 && s->xll.nchsets > 1) {
     262          32 :                 av_log(avctx, AV_LOG_VERBOSE, "Forcing XLL recovery mode\n");
     263          32 :                 s->packet |= DCA_PACKET_RECOVERY;
     264             :             }
     265             : 
     266             :             // Set 'residual ok' flag for the next frame
     267        1341 :             s->packet |= DCA_PACKET_RESIDUAL;
     268             :         }
     269             : 
     270        1353 :         if ((ret = ff_dca_xll_filter_frame(&s->xll, frame)) < 0) {
     271             :             // Fall back to core unless hard error
     272           0 :             if (!(s->packet & DCA_PACKET_CORE))
     273           0 :                 return ret;
     274           0 :             if (ret != AVERROR_INVALIDDATA || (avctx->err_recognition & AV_EF_EXPLODE))
     275           0 :                 return ret;
     276           0 :             if ((ret = ff_dca_core_filter_frame(&s->core, frame)) < 0)
     277           0 :                 return ret;
     278             :         }
     279        1122 :     } else if (s->packet & DCA_PACKET_CORE) {
     280        1122 :         if ((ret = ff_dca_core_filter_frame(&s->core, frame)) < 0)
     281           0 :             return ret;
     282        1122 :         if (s->core.filter_mode & DCA_FILTER_MODE_FIXED)
     283         518 :             s->packet |= DCA_PACKET_RESIDUAL;
     284             :     } else {
     285           0 :         av_log(avctx, AV_LOG_ERROR, "No valid DCA sub-stream found\n");
     286           0 :         if (s->core_only)
     287           0 :             av_log(avctx, AV_LOG_WARNING, "Consider disabling 'core_only' option\n");
     288           0 :         return AVERROR_INVALIDDATA;
     289             :     }
     290             : 
     291        2475 :     *got_frame_ptr = 1;
     292             : 
     293        2475 :     return avpkt->size;
     294             : }
     295             : 
     296           0 : static av_cold void dcadec_flush(AVCodecContext *avctx)
     297             : {
     298           0 :     DCAContext *s = avctx->priv_data;
     299             : 
     300           0 :     ff_dca_core_flush(&s->core);
     301           0 :     ff_dca_xll_flush(&s->xll);
     302           0 :     ff_dca_lbr_flush(&s->lbr);
     303             : 
     304           0 :     s->packet &= DCA_PACKET_MASK;
     305           0 : }
     306             : 
     307          91 : static av_cold int dcadec_close(AVCodecContext *avctx)
     308             : {
     309          91 :     DCAContext *s = avctx->priv_data;
     310             : 
     311          91 :     ff_dca_core_close(&s->core);
     312          91 :     ff_dca_xll_close(&s->xll);
     313          91 :     ff_dca_lbr_close(&s->lbr);
     314             : 
     315          91 :     av_freep(&s->buffer);
     316          91 :     s->buffer_size = 0;
     317             : 
     318          91 :     return 0;
     319             : }
     320             : 
     321          91 : static av_cold int dcadec_init(AVCodecContext *avctx)
     322             : {
     323          91 :     DCAContext *s = avctx->priv_data;
     324             : 
     325          91 :     s->avctx = avctx;
     326          91 :     s->core.avctx = avctx;
     327          91 :     s->exss.avctx = avctx;
     328          91 :     s->xll.avctx = avctx;
     329          91 :     s->lbr.avctx = avctx;
     330             : 
     331          91 :     ff_dca_init_vlcs();
     332             : 
     333          91 :     if (ff_dca_core_init(&s->core) < 0)
     334           0 :         return AVERROR(ENOMEM);
     335             : 
     336          91 :     if (ff_dca_lbr_init(&s->lbr) < 0)
     337           0 :         return AVERROR(ENOMEM);
     338             : 
     339          91 :     ff_dcadsp_init(&s->dcadsp);
     340          91 :     s->core.dcadsp = &s->dcadsp;
     341          91 :     s->xll.dcadsp = &s->dcadsp;
     342          91 :     s->lbr.dcadsp = &s->dcadsp;
     343             : 
     344          91 :     s->crctab = av_crc_get_table(AV_CRC_16_CCITT);
     345             : 
     346          91 :     switch (avctx->request_channel_layout & ~AV_CH_LAYOUT_NATIVE) {
     347          47 :     case 0:
     348          47 :         s->request_channel_layout = 0;
     349          47 :         break;
     350          22 :     case AV_CH_LAYOUT_STEREO:
     351             :     case AV_CH_LAYOUT_STEREO_DOWNMIX:
     352          22 :         s->request_channel_layout = DCA_SPEAKER_LAYOUT_STEREO;
     353          22 :         break;
     354           0 :     case AV_CH_LAYOUT_5POINT0:
     355           0 :         s->request_channel_layout = DCA_SPEAKER_LAYOUT_5POINT0;
     356           0 :         break;
     357          22 :     case AV_CH_LAYOUT_5POINT1:
     358          22 :         s->request_channel_layout = DCA_SPEAKER_LAYOUT_5POINT1;
     359          22 :         break;
     360           0 :     default:
     361           0 :         av_log(avctx, AV_LOG_WARNING, "Invalid request_channel_layout\n");
     362           0 :         break;
     363             :     }
     364             : 
     365          91 :     return 0;
     366             : }
     367             : 
     368             : #define OFFSET(x) offsetof(DCAContext, x)
     369             : #define PARAM AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
     370             : 
     371             : static const AVOption dcadec_options[] = {
     372             :     { "core_only", "Decode core only without extensions", OFFSET(core_only), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, PARAM },
     373             :     { NULL }
     374             : };
     375             : 
     376             : static const AVClass dcadec_class = {
     377             :     .class_name = "DCA decoder",
     378             :     .item_name  = av_default_item_name,
     379             :     .option     = dcadec_options,
     380             :     .version    = LIBAVUTIL_VERSION_INT,
     381             :     .category   = AV_CLASS_CATEGORY_DECODER,
     382             : };
     383             : 
     384             : AVCodec ff_dca_decoder = {
     385             :     .name           = "dca",
     386             :     .long_name      = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
     387             :     .type           = AVMEDIA_TYPE_AUDIO,
     388             :     .id             = AV_CODEC_ID_DTS,
     389             :     .priv_data_size = sizeof(DCAContext),
     390             :     .init           = dcadec_init,
     391             :     .decode         = dcadec_decode_frame,
     392             :     .close          = dcadec_close,
     393             :     .flush          = dcadec_flush,
     394             :     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
     395             :     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P,
     396             :                                                       AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE },
     397             :     .priv_class     = &dcadec_class,
     398             :     .profiles       = NULL_IF_CONFIG_SMALL(ff_dca_profiles),
     399             :     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
     400             : };

Generated by: LCOV version 1.13