LCOV - code coverage report
Current view: top level - src/libavformat - adxdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 43 57 75.4 %
Date: 2017-04-28 19:02:26 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2011 Justin Ruggles
       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             : /**
      22             :  * @file
      23             :  * CRI ADX demuxer
      24             :  */
      25             : 
      26             : #include "libavutil/intreadwrite.h"
      27             : #include "avformat.h"
      28             : #include "internal.h"
      29             : 
      30             : #define BLOCK_SIZE    18
      31             : #define BLOCK_SAMPLES 32
      32             : 
      33             : typedef struct ADXDemuxerContext {
      34             :     int header_size;
      35             : } ADXDemuxerContext;
      36             : 
      37        5949 : static int adx_probe(AVProbeData *p)
      38             : {
      39             :     int offset;
      40        5949 :     if (AV_RB16(p->buf) != 0x8000)
      41        5947 :         return 0;
      42           2 :     offset = AV_RB16(&p->buf[2]);
      43           2 :     if (   offset < 8
      44           2 :         || offset > p->buf_size - 4
      45           2 :         || memcmp(p->buf + offset - 2, "(c)CRI", 6))
      46           0 :         return 0;
      47           2 :     return AVPROBE_SCORE_MAX * 3 / 4;
      48             : }
      49             : 
      50       16540 : static int adx_read_packet(AVFormatContext *s, AVPacket *pkt)
      51             : {
      52       16540 :     ADXDemuxerContext *c = s->priv_data;
      53       16540 :     AVCodecParameters *par = s->streams[0]->codecpar;
      54             :     int ret, size;
      55             : 
      56       16540 :     if (par->channels <= 0) {
      57           0 :         av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n", par->channels);
      58           0 :         return AVERROR_INVALIDDATA;
      59             :     }
      60             : 
      61       16540 :     size = BLOCK_SIZE * par->channels;
      62             : 
      63       16540 :     pkt->pos = avio_tell(s->pb);
      64       16540 :     pkt->stream_index = 0;
      65             : 
      66       16540 :     ret = av_get_packet(s->pb, pkt, size);
      67       16540 :     if (ret != size) {
      68           2 :         av_packet_unref(pkt);
      69           2 :         return ret < 0 ? ret : AVERROR(EIO);
      70             :     }
      71       16538 :     if (AV_RB16(pkt->data) & 0x8000) {
      72           0 :         av_packet_unref(pkt);
      73           0 :         return AVERROR_EOF;
      74             :     }
      75       16538 :     pkt->size     = size;
      76       16538 :     pkt->duration = 1;
      77       16538 :     pkt->pts      = (pkt->pos - c->header_size) / size;
      78             : 
      79       16538 :     return 0;
      80             : }
      81             : 
      82           2 : static int adx_read_header(AVFormatContext *s)
      83             : {
      84           2 :     ADXDemuxerContext *c = s->priv_data;
      85             :     AVCodecParameters *par;
      86             : 
      87           2 :     AVStream *st = avformat_new_stream(s, NULL);
      88           2 :     if (!st)
      89           0 :         return AVERROR(ENOMEM);
      90           2 :     par = s->streams[0]->codecpar;
      91             : 
      92           2 :     if (avio_rb16(s->pb) != 0x8000)
      93           0 :         return AVERROR_INVALIDDATA;
      94           2 :     c->header_size = avio_rb16(s->pb) + 4;
      95           2 :     avio_seek(s->pb, -4, SEEK_CUR);
      96             : 
      97           2 :     if (ff_get_extradata(s, par, s->pb, c->header_size) < 0)
      98           0 :         return AVERROR(ENOMEM);
      99             : 
     100           2 :     if (par->extradata_size < 12) {
     101           0 :         av_log(s, AV_LOG_ERROR, "Invalid extradata size.\n");
     102           0 :         return AVERROR_INVALIDDATA;
     103             :     }
     104           2 :     par->channels    = AV_RB8 (par->extradata + 7);
     105           2 :     par->sample_rate = AV_RB32(par->extradata + 8);
     106             : 
     107           2 :     if (par->channels <= 0) {
     108           0 :         av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n", par->channels);
     109           0 :         return AVERROR_INVALIDDATA;
     110             :     }
     111             : 
     112           2 :     if (par->sample_rate <= 0) {
     113           0 :         av_log(s, AV_LOG_ERROR, "Invalid sample rate %d\n", par->sample_rate);
     114           0 :         return AVERROR_INVALIDDATA;
     115             :     }
     116             : 
     117           2 :     par->codec_type  = AVMEDIA_TYPE_AUDIO;
     118           2 :     par->codec_id    = s->iformat->raw_codec_id;
     119           2 :     par->bit_rate    = (int64_t)par->sample_rate * par->channels * BLOCK_SIZE * 8LL / BLOCK_SAMPLES;
     120             : 
     121           2 :     avpriv_set_pts_info(st, 64, BLOCK_SAMPLES, par->sample_rate);
     122             : 
     123           2 :     return 0;
     124             : }
     125             : 
     126             : AVInputFormat ff_adx_demuxer = {
     127             :     .name           = "adx",
     128             :     .long_name      = NULL_IF_CONFIG_SMALL("CRI ADX"),
     129             :     .read_probe     = adx_probe,
     130             :     .priv_data_size = sizeof(ADXDemuxerContext),
     131             :     .read_header    = adx_read_header,
     132             :     .read_packet    = adx_read_packet,
     133             :     .extensions     = "adx",
     134             :     .raw_codec_id   = AV_CODEC_ID_ADPCM_ADX,
     135             :     .flags          = AVFMT_GENERIC_INDEX,
     136             : };

Generated by: LCOV version 1.13