LCOV - code coverage report
Current view: top level - libavcodec - dca.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 48 76 63.2 %
Date: 2017-12-10 21:22:29 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * DCA compatible decoder data
       3             :  * Copyright (C) 2004 Gildas Bazin
       4             :  * Copyright (C) 2004 Benjamin Zores
       5             :  * Copyright (C) 2006 Benjamin Larsson
       6             :  * Copyright (C) 2007 Konstantin Shishkov
       7             :  *
       8             :  * This file is part of FFmpeg.
       9             :  *
      10             :  * FFmpeg is free software; you can redistribute it and/or
      11             :  * modify it under the terms of the GNU Lesser General Public
      12             :  * License as published by the Free Software Foundation; either
      13             :  * version 2.1 of the License, or (at your option) any later version.
      14             :  *
      15             :  * FFmpeg is distributed in the hope that it will be useful,
      16             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      17             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      18             :  * Lesser General Public License for more details.
      19             :  *
      20             :  * You should have received a copy of the GNU Lesser General Public
      21             :  * License along with FFmpeg; if not, write to the Free Software
      22             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      23             :  */
      24             : 
      25             : #include <stdint.h>
      26             : #include <string.h>
      27             : 
      28             : #include "libavutil/error.h"
      29             : 
      30             : #include "dca.h"
      31             : #include "dca_core.h"
      32             : #include "dca_syncwords.h"
      33             : #include "get_bits.h"
      34             : #include "put_bits.h"
      35             : 
      36             : const uint32_t avpriv_dca_sample_rates[16] = {
      37             :     0, 8000, 16000, 32000, 0, 0, 11025, 22050, 44100, 0, 0,
      38             :     12000, 24000, 48000, 96000, 192000
      39             : };
      40             : 
      41             : const uint32_t ff_dca_sampling_freqs[16] = {
      42             :       8000,  16000, 32000, 64000, 128000, 22050,  44100,  88200,
      43             :     176400, 352800, 12000, 24000,  48000, 96000, 192000, 384000,
      44             : };
      45             : 
      46             : const uint8_t ff_dca_freq_ranges[16] = {
      47             :     0, 1, 2, 3, 4, 1, 2, 3, 4, 4, 0, 1, 2, 3, 4, 4
      48             : };
      49             : 
      50             : const uint8_t ff_dca_bits_per_sample[8] = {
      51             :     16, 16, 20, 20, 0, 24, 24, 0
      52             : };
      53             : 
      54        3340 : int avpriv_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
      55             :                              int max_size)
      56             : {
      57             :     uint32_t mrk;
      58             :     int i, tmp;
      59             :     PutBitContext pb;
      60             : 
      61        3340 :     if ((unsigned) src_size > (unsigned) max_size)
      62           0 :         src_size = max_size;
      63             : 
      64        3340 :     mrk = AV_RB32(src);
      65        3340 :     switch (mrk) {
      66        3340 :     case DCA_SYNCWORD_CORE_BE:
      67             :     case DCA_SYNCWORD_SUBSTREAM:
      68        3340 :         memcpy(dst, src, src_size);
      69        3340 :         return src_size;
      70           0 :     case DCA_SYNCWORD_CORE_LE:
      71           0 :         for (i = 0; i < (src_size + 1) >> 1; i++) {
      72           0 :             AV_WB16(dst, AV_RL16(src));
      73           0 :             src += 2;
      74           0 :             dst += 2;
      75             :         }
      76           0 :         return src_size;
      77           0 :     case DCA_SYNCWORD_CORE_14B_BE:
      78             :     case DCA_SYNCWORD_CORE_14B_LE:
      79           0 :         init_put_bits(&pb, dst, max_size);
      80           0 :         for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
      81           0 :             tmp = ((mrk == DCA_SYNCWORD_CORE_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
      82           0 :             put_bits(&pb, 14, tmp);
      83             :         }
      84           0 :         flush_put_bits(&pb);
      85           0 :         return (put_bits_count(&pb) + 7) >> 3;
      86           0 :     default:
      87           0 :         return AVERROR_INVALIDDATA;
      88             :     }
      89             : }
      90             : 
      91        5803 : int ff_dca_parse_core_frame_header(DCACoreFrameHeader *h, GetBitContext *gb)
      92             : {
      93        5803 :     if (get_bits_long(gb, 32) != DCA_SYNCWORD_CORE_BE)
      94           0 :         return DCA_PARSE_ERROR_SYNC_WORD;
      95             : 
      96        5803 :     h->normal_frame = get_bits1(gb);
      97        5803 :     h->deficit_samples = get_bits(gb, 5) + 1;
      98        5803 :     if (h->deficit_samples != DCA_PCMBLOCK_SAMPLES)
      99           0 :         return DCA_PARSE_ERROR_DEFICIT_SAMPLES;
     100             : 
     101        5803 :     h->crc_present = get_bits1(gb);
     102        5803 :     h->npcmblocks = get_bits(gb, 7) + 1;
     103        5803 :     if (h->npcmblocks & (DCA_SUBBAND_SAMPLES - 1))
     104           0 :         return DCA_PARSE_ERROR_PCM_BLOCKS;
     105             : 
     106        5803 :     h->frame_size = get_bits(gb, 14) + 1;
     107        5803 :     if (h->frame_size < 96)
     108           0 :         return DCA_PARSE_ERROR_FRAME_SIZE;
     109             : 
     110        5803 :     h->audio_mode = get_bits(gb, 6);
     111        5803 :     if (h->audio_mode >= DCA_AMODE_COUNT)
     112           0 :         return DCA_PARSE_ERROR_AMODE;
     113             : 
     114        5803 :     h->sr_code = get_bits(gb, 4);
     115        5803 :     if (!avpriv_dca_sample_rates[h->sr_code])
     116           0 :         return DCA_PARSE_ERROR_SAMPLE_RATE;
     117             : 
     118        5803 :     h->br_code = get_bits(gb, 5);
     119        5803 :     if (get_bits1(gb))
     120           0 :         return DCA_PARSE_ERROR_RESERVED_BIT;
     121             : 
     122        5803 :     h->drc_present = get_bits1(gb);
     123        5803 :     h->ts_present = get_bits1(gb);
     124        5803 :     h->aux_present = get_bits1(gb);
     125        5803 :     h->hdcd_master = get_bits1(gb);
     126        5803 :     h->ext_audio_type = get_bits(gb, 3);
     127        5803 :     h->ext_audio_present = get_bits1(gb);
     128        5803 :     h->sync_ssf = get_bits1(gb);
     129        5803 :     h->lfe_present = get_bits(gb, 2);
     130        5803 :     if (h->lfe_present == DCA_LFE_FLAG_INVALID)
     131           0 :         return DCA_PARSE_ERROR_LFE_FLAG;
     132             : 
     133        5803 :     h->predictor_history = get_bits1(gb);
     134        5803 :     if (h->crc_present)
     135           0 :         skip_bits(gb, 16);
     136        5803 :     h->filter_perfect = get_bits1(gb);
     137        5803 :     h->encoder_rev = get_bits(gb, 4);
     138        5803 :     h->copy_hist = get_bits(gb, 2);
     139        5803 :     h->pcmr_code = get_bits(gb, 3);
     140        5803 :     if (!ff_dca_bits_per_sample[h->pcmr_code])
     141           0 :         return DCA_PARSE_ERROR_PCM_RES;
     142             : 
     143        5803 :     h->sumdiff_front = get_bits1(gb);
     144        5803 :     h->sumdiff_surround = get_bits1(gb);
     145        5803 :     h->dn_code = get_bits(gb, 4);
     146        5803 :     return 0;
     147             : }
     148             : 
     149        3340 : int avpriv_dca_parse_core_frame_header(DCACoreFrameHeader *h, const uint8_t *buf, int size)
     150             : {
     151             :     GetBitContext gb;
     152             :     int ret;
     153             : 
     154        3340 :     ret = init_get_bits8(&gb, buf, size);
     155        3340 :     if (ret < 0)
     156           0 :         return ret;
     157             : 
     158        3340 :     if (ff_dca_parse_core_frame_header(h, &gb) < 0)
     159           0 :         return AVERROR_INVALIDDATA;
     160             : 
     161        3340 :     return 0;
     162             : }

Generated by: LCOV version 1.13