LCOV - code coverage report
Current view: top level - libavformat - dsfdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 3 73 4.1 %
Date: 2017-12-17 16:07:53 Functions: 1 4 25.0 %

          Line data    Source code
       1             : /*
       2             :  * DSD Stream File (DSF) demuxer
       3             :  * Copyright (c) 2014 Peter Ross
       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             : #include "libavutil/intreadwrite.h"
      23             : #include "avformat.h"
      24             : #include "internal.h"
      25             : #include "id3v2.h"
      26             : 
      27             : typedef struct {
      28             :     uint64_t data_end;
      29             : } DSFContext;
      30             : 
      31        6130 : static int dsf_probe(AVProbeData *p)
      32             : {
      33        6130 :     if (p->buf_size < 12 || memcmp(p->buf, "DSD ", 4) || AV_RL64(p->buf + 4) != 28)
      34        6130 :         return 0;
      35           0 :     return AVPROBE_SCORE_MAX;
      36             : }
      37             : 
      38             : static const uint64_t dsf_channel_layout[] = {
      39             :     0,
      40             :     AV_CH_LAYOUT_MONO,
      41             :     AV_CH_LAYOUT_STEREO,
      42             :     AV_CH_LAYOUT_SURROUND,
      43             :     AV_CH_LAYOUT_QUAD,
      44             :     AV_CH_LAYOUT_4POINT0,
      45             :     AV_CH_LAYOUT_5POINT0_BACK,
      46             :     AV_CH_LAYOUT_5POINT1_BACK,
      47             : };
      48             : 
      49           0 : static void read_id3(AVFormatContext *s, uint64_t id3pos)
      50             : {
      51           0 :     ID3v2ExtraMeta *id3v2_extra_meta = NULL;
      52           0 :     if (avio_seek(s->pb, id3pos, SEEK_SET) < 0)
      53           0 :         return;
      54             : 
      55           0 :     ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, 0);
      56           0 :     if (id3v2_extra_meta) {
      57           0 :         ff_id3v2_parse_apic(s, &id3v2_extra_meta);
      58           0 :         ff_id3v2_parse_chapters(s, &id3v2_extra_meta);
      59             :     }
      60           0 :     ff_id3v2_free_extra_meta(&id3v2_extra_meta);
      61             : }
      62             : 
      63           0 : static int dsf_read_header(AVFormatContext *s)
      64             : {
      65           0 :     DSFContext *dsf = s->priv_data;
      66           0 :     AVIOContext *pb = s->pb;
      67             :     AVStream *st;
      68             :     uint64_t id3pos;
      69             :     unsigned int channel_type;
      70             : 
      71           0 :     avio_skip(pb, 4);
      72           0 :     if (avio_rl64(pb) != 28)
      73           0 :         return AVERROR_INVALIDDATA;
      74             : 
      75             :     /* create primary stream before any id3 coverart streams */
      76           0 :     st = avformat_new_stream(s, NULL);
      77           0 :     if (!st)
      78           0 :         return AVERROR(ENOMEM);
      79             : 
      80           0 :     avio_skip(pb, 8);
      81           0 :     id3pos = avio_rl64(pb);
      82           0 :     if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
      83           0 :         read_id3(s, id3pos);
      84           0 :         avio_seek(pb, 28, SEEK_SET);
      85             :     }
      86             : 
      87             :     /* fmt chunk */
      88             : 
      89           0 :     if (avio_rl32(pb) != MKTAG('f', 'm', 't', ' ') || avio_rl64(pb) != 52)
      90           0 :         return AVERROR_INVALIDDATA;
      91             : 
      92           0 :     if (avio_rl32(pb) != 1) {
      93           0 :         avpriv_request_sample(s, "unknown format version");
      94           0 :         return AVERROR_INVALIDDATA;
      95             :     }
      96             : 
      97           0 :     if (avio_rl32(pb)) {
      98           0 :         avpriv_request_sample(s, "unknown format id");
      99           0 :         return AVERROR_INVALIDDATA;
     100             :     }
     101             : 
     102           0 :     channel_type = avio_rl32(pb);
     103           0 :     if (channel_type < FF_ARRAY_ELEMS(dsf_channel_layout))
     104           0 :         st->codecpar->channel_layout = dsf_channel_layout[channel_type];
     105           0 :     if (!st->codecpar->channel_layout)
     106           0 :         avpriv_request_sample(s, "channel type %i", channel_type);
     107             : 
     108           0 :     st->codecpar->codec_type   = AVMEDIA_TYPE_AUDIO;
     109           0 :     st->codecpar->channels     = avio_rl32(pb);
     110           0 :     st->codecpar->sample_rate  = avio_rl32(pb) / 8;
     111             : 
     112           0 :     if (st->codecpar->channels <= 0)
     113           0 :         return AVERROR_INVALIDDATA;
     114             : 
     115           0 :     switch(avio_rl32(pb)) {
     116           0 :     case 1: st->codecpar->codec_id = AV_CODEC_ID_DSD_LSBF_PLANAR; break;
     117           0 :     case 8: st->codecpar->codec_id = AV_CODEC_ID_DSD_MSBF_PLANAR; break;
     118           0 :     default:
     119           0 :         avpriv_request_sample(s, "unknown most significant bit");
     120           0 :         return AVERROR_INVALIDDATA;
     121             :     }
     122             : 
     123           0 :     avio_skip(pb, 8);
     124           0 :     st->codecpar->block_align = avio_rl32(pb);
     125           0 :     if (st->codecpar->block_align > INT_MAX / st->codecpar->channels) {
     126           0 :         avpriv_request_sample(s, "block_align overflow");
     127           0 :         return AVERROR_INVALIDDATA;
     128             :     }
     129           0 :     st->codecpar->block_align *= st->codecpar->channels;
     130           0 :     st->codecpar->bit_rate = st->codecpar->channels * st->codecpar->sample_rate * 8LL;
     131           0 :     avio_skip(pb, 4);
     132             : 
     133             :     /* data chunk */
     134             : 
     135           0 :     dsf->data_end = avio_tell(pb);
     136           0 :     if (avio_rl32(pb) != MKTAG('d', 'a', 't', 'a'))
     137           0 :         return AVERROR_INVALIDDATA;
     138           0 :     dsf->data_end += avio_rl64(pb);
     139             : 
     140           0 :     return 0;
     141             : }
     142             : 
     143           0 : static int dsf_read_packet(AVFormatContext *s, AVPacket *pkt)
     144             : {
     145           0 :     DSFContext *dsf = s->priv_data;
     146           0 :     AVIOContext *pb = s->pb;
     147           0 :     AVStream *st = s->streams[0];
     148           0 :     int64_t pos = avio_tell(pb);
     149             : 
     150           0 :     if (pos >= dsf->data_end)
     151           0 :         return AVERROR_EOF;
     152             : 
     153           0 :     pkt->stream_index = 0;
     154           0 :     return av_get_packet(pb, pkt, FFMIN(dsf->data_end - pos, st->codecpar->block_align));
     155             : }
     156             : 
     157             : AVInputFormat ff_dsf_demuxer = {
     158             :     .name           = "dsf",
     159             :     .long_name      = NULL_IF_CONFIG_SMALL("DSD Stream File (DSF)"),
     160             :     .priv_data_size = sizeof(DSFContext),
     161             :     .read_probe     = dsf_probe,
     162             :     .read_header    = dsf_read_header,
     163             :     .read_packet    = dsf_read_packet,
     164             :     .flags          = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK,
     165             : };

Generated by: LCOV version 1.13