LCOV - code coverage report
Current view: top level - libavcodec - dvaudiodec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 53 0.0 %
Date: 2017-12-10 21:22:29 Functions: 0 3 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2012 Laurent Aimar
       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/intreadwrite.h"
      22             : #include "avcodec.h"
      23             : #include "internal.h"
      24             : #include "dvaudio.h"
      25             : 
      26             : typedef struct DVAudioContext {
      27             :     int block_size;
      28             :     int is_12bit;
      29             :     int is_pal;
      30             :     int16_t shuffle[2000];
      31             : } DVAudioContext;
      32             : 
      33           0 : static av_cold int decode_init(AVCodecContext *avctx)
      34             : {
      35           0 :     DVAudioContext *s = avctx->priv_data;
      36             :     int i;
      37             : 
      38           0 :     if (avctx->channels != 2) {
      39           0 :         av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
      40           0 :         return AVERROR(EINVAL);
      41             :     }
      42             : 
      43           0 :     if (avctx->codec_tag == 0x0215) {
      44           0 :         s->block_size = 7200;
      45           0 :     } else if (avctx->codec_tag == 0x0216) {
      46           0 :         s->block_size = 8640;
      47           0 :     } else if (avctx->block_align == 7200 ||
      48           0 :                avctx->block_align == 8640) {
      49           0 :         s->block_size = avctx->block_align;
      50             :     } else {
      51           0 :         return AVERROR(EINVAL);
      52             :     }
      53             : 
      54           0 :     s->is_pal = s->block_size == 8640;
      55           0 :     s->is_12bit = avctx->bits_per_coded_sample == 12;
      56           0 :     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
      57           0 :     avctx->channel_layout = AV_CH_LAYOUT_STEREO;
      58             : 
      59           0 :     for (i = 0; i < FF_ARRAY_ELEMS(s->shuffle); i++) {
      60           0 :         const unsigned a = s->is_pal ? 18 : 15;
      61           0 :         const unsigned b = 3 * a;
      62             : 
      63           0 :         s->shuffle[i] = 80 * ((21 * (i % 3) + 9 * (i / 3) + ((i / a) % 3)) % b) +
      64           0 :                          (2 + s->is_12bit) * (i / b) + 8;
      65             :     }
      66             : 
      67           0 :     return 0;
      68             : }
      69             : 
      70           0 : static inline uint16_t dv_audio_12to16(uint16_t sample)
      71             : {
      72             :     uint16_t shift, result;
      73             : 
      74           0 :     sample = (sample < 0x800) ? sample : sample | 0xf000;
      75           0 :     shift  = (sample & 0xf00) >> 8;
      76             : 
      77           0 :     if (shift < 0x2 || shift > 0xd) {
      78           0 :         result = sample;
      79           0 :     } else if (shift < 0x8) {
      80           0 :         shift--;
      81           0 :         result = (sample - (256 * shift)) << shift;
      82             :     } else {
      83           0 :         shift  = 0xe - shift;
      84           0 :         result = ((sample + ((256 * shift) + 1)) << shift) - 1;
      85             :     }
      86             : 
      87           0 :     return result;
      88             : }
      89             : 
      90           0 : static int decode_frame(AVCodecContext *avctx, void *data,
      91             :                         int *got_frame_ptr, AVPacket *pkt)
      92             : {
      93           0 :     DVAudioContext *s = avctx->priv_data;
      94           0 :     AVFrame *frame = data;
      95           0 :     const uint8_t *src = pkt->data;
      96             :     int16_t *dst;
      97             :     int ret, i;
      98             : 
      99           0 :     if (pkt->size < s->block_size)
     100           0 :         return AVERROR_INVALIDDATA;
     101             : 
     102           0 :     frame->nb_samples = dv_get_audio_sample_count(pkt->data + 244, s->is_pal);
     103           0 :     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
     104           0 :         return ret;
     105           0 :     dst = (int16_t *)frame->data[0];
     106             : 
     107           0 :     for (i = 0; i < frame->nb_samples; i++) {
     108           0 :        const uint8_t *v = &src[s->shuffle[i]];
     109             : 
     110           0 :        if (s->is_12bit) {
     111           0 :            *dst++ = dv_audio_12to16((v[0] << 4) | ((v[2] >> 4) & 0x0f));
     112           0 :            *dst++ = dv_audio_12to16((v[1] << 4) | ((v[2] >> 0) & 0x0f));
     113             :        } else {
     114           0 :            *dst++ = AV_RB16(&v[0]);
     115           0 :            *dst++ = AV_RB16(&v[s->is_pal ? 4320 : 3600]);
     116             :        }
     117             :     }
     118             : 
     119           0 :     *got_frame_ptr = 1;
     120             : 
     121           0 :     return s->block_size;
     122             : }
     123             : 
     124             : AVCodec ff_dvaudio_decoder = {
     125             :     .name           = "dvaudio",
     126             :     .long_name      = NULL_IF_CONFIG_SMALL("Ulead DV Audio"),
     127             :     .type           = AVMEDIA_TYPE_AUDIO,
     128             :     .id             = AV_CODEC_ID_DVAUDIO,
     129             :     .init           = decode_init,
     130             :     .decode         = decode_frame,
     131             :     .capabilities   = AV_CODEC_CAP_DR1,
     132             :     .priv_data_size = sizeof(DVAudioContext),
     133             : };

Generated by: LCOV version 1.13