LCOV - code coverage report
Current view: top level - libavformat - dsfdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 3 93 3.2 %
Date: 2018-05-20 11:54:08 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             :     uint64_t audio_size;
      30             :     uint64_t data_size;
      31             : } DSFContext;
      32             : 
      33        6217 : static int dsf_probe(AVProbeData *p)
      34             : {
      35        6217 :     if (p->buf_size < 12 || memcmp(p->buf, "DSD ", 4) || AV_RL64(p->buf + 4) != 28)
      36        6217 :         return 0;
      37           0 :     return AVPROBE_SCORE_MAX;
      38             : }
      39             : 
      40             : static const uint64_t dsf_channel_layout[] = {
      41             :     0,
      42             :     AV_CH_LAYOUT_MONO,
      43             :     AV_CH_LAYOUT_STEREO,
      44             :     AV_CH_LAYOUT_SURROUND,
      45             :     AV_CH_LAYOUT_QUAD,
      46             :     AV_CH_LAYOUT_4POINT0,
      47             :     AV_CH_LAYOUT_5POINT0_BACK,
      48             :     AV_CH_LAYOUT_5POINT1_BACK,
      49             : };
      50             : 
      51           0 : static void read_id3(AVFormatContext *s, uint64_t id3pos)
      52             : {
      53           0 :     ID3v2ExtraMeta *id3v2_extra_meta = NULL;
      54           0 :     if (avio_seek(s->pb, id3pos, SEEK_SET) < 0)
      55           0 :         return;
      56             : 
      57           0 :     ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, 0);
      58           0 :     if (id3v2_extra_meta) {
      59           0 :         ff_id3v2_parse_apic(s, &id3v2_extra_meta);
      60           0 :         ff_id3v2_parse_chapters(s, &id3v2_extra_meta);
      61             :     }
      62           0 :     ff_id3v2_free_extra_meta(&id3v2_extra_meta);
      63             : }
      64             : 
      65           0 : static int dsf_read_header(AVFormatContext *s)
      66             : {
      67           0 :     DSFContext *dsf = s->priv_data;
      68           0 :     AVIOContext *pb = s->pb;
      69             :     AVStream *st;
      70             :     uint64_t id3pos;
      71             :     unsigned int channel_type;
      72             : 
      73           0 :     avio_skip(pb, 4);
      74           0 :     if (avio_rl64(pb) != 28)
      75           0 :         return AVERROR_INVALIDDATA;
      76             : 
      77             :     /* create primary stream before any id3 coverart streams */
      78           0 :     st = avformat_new_stream(s, NULL);
      79           0 :     if (!st)
      80           0 :         return AVERROR(ENOMEM);
      81             : 
      82           0 :     avio_skip(pb, 8);
      83           0 :     id3pos = avio_rl64(pb);
      84           0 :     if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
      85           0 :         read_id3(s, id3pos);
      86           0 :         avio_seek(pb, 28, SEEK_SET);
      87             :     }
      88             : 
      89             :     /* fmt chunk */
      90             : 
      91           0 :     if (avio_rl32(pb) != MKTAG('f', 'm', 't', ' ') || avio_rl64(pb) != 52)
      92           0 :         return AVERROR_INVALIDDATA;
      93             : 
      94           0 :     if (avio_rl32(pb) != 1) {
      95           0 :         avpriv_request_sample(s, "unknown format version");
      96           0 :         return AVERROR_INVALIDDATA;
      97             :     }
      98             : 
      99           0 :     if (avio_rl32(pb)) {
     100           0 :         avpriv_request_sample(s, "unknown format id");
     101           0 :         return AVERROR_INVALIDDATA;
     102             :     }
     103             : 
     104           0 :     channel_type = avio_rl32(pb);
     105           0 :     if (channel_type < FF_ARRAY_ELEMS(dsf_channel_layout))
     106           0 :         st->codecpar->channel_layout = dsf_channel_layout[channel_type];
     107           0 :     if (!st->codecpar->channel_layout)
     108           0 :         avpriv_request_sample(s, "channel type %i", channel_type);
     109             : 
     110           0 :     st->codecpar->codec_type   = AVMEDIA_TYPE_AUDIO;
     111           0 :     st->codecpar->channels     = avio_rl32(pb);
     112           0 :     st->codecpar->sample_rate  = avio_rl32(pb) / 8;
     113             : 
     114           0 :     if (st->codecpar->channels <= 0)
     115           0 :         return AVERROR_INVALIDDATA;
     116             : 
     117           0 :     switch(avio_rl32(pb)) {
     118           0 :     case 1: st->codecpar->codec_id = AV_CODEC_ID_DSD_LSBF_PLANAR; break;
     119           0 :     case 8: st->codecpar->codec_id = AV_CODEC_ID_DSD_MSBF_PLANAR; break;
     120           0 :     default:
     121           0 :         avpriv_request_sample(s, "unknown most significant bit");
     122           0 :         return AVERROR_INVALIDDATA;
     123             :     }
     124             : 
     125           0 :     dsf->audio_size = avio_rl64(pb) / 8 * st->codecpar->channels;
     126           0 :     st->codecpar->block_align = avio_rl32(pb);
     127           0 :     if (st->codecpar->block_align > INT_MAX / st->codecpar->channels) {
     128           0 :         avpriv_request_sample(s, "block_align overflow");
     129           0 :         return AVERROR_INVALIDDATA;
     130             :     }
     131           0 :     st->codecpar->block_align *= st->codecpar->channels;
     132           0 :     st->codecpar->bit_rate = st->codecpar->channels * st->codecpar->sample_rate * 8LL;
     133           0 :     avio_skip(pb, 4);
     134             : 
     135             :     /* data chunk */
     136             : 
     137           0 :     dsf->data_end = avio_tell(pb);
     138           0 :     if (avio_rl32(pb) != MKTAG('d', 'a', 't', 'a'))
     139           0 :         return AVERROR_INVALIDDATA;
     140           0 :     dsf->data_size = avio_rl64(pb) - 12;
     141           0 :     dsf->data_end += dsf->data_size + 12;
     142           0 :     s->internal->data_offset = avio_tell(pb);
     143             : 
     144           0 :     return 0;
     145             : }
     146             : 
     147           0 : static int dsf_read_packet(AVFormatContext *s, AVPacket *pkt)
     148             : {
     149           0 :     DSFContext *dsf = s->priv_data;
     150           0 :     AVIOContext *pb = s->pb;
     151           0 :     AVStream *st = s->streams[0];
     152           0 :     int64_t pos = avio_tell(pb);
     153             : 
     154           0 :     if (pos >= dsf->data_end)
     155           0 :         return AVERROR_EOF;
     156             : 
     157           0 :     pkt->stream_index = 0;
     158           0 :     if (dsf->data_size > dsf->audio_size) {
     159           0 :         int last_packet = pos == (dsf->data_end - st->codecpar->block_align);
     160             : 
     161           0 :         if (last_packet) {
     162           0 :             int64_t data_pos = pos - s->internal->data_offset;
     163           0 :             int64_t packet_size = dsf->audio_size - data_pos;
     164           0 :             int64_t skip_size = dsf->data_size - data_pos - packet_size;
     165             :             uint8_t *dst;
     166             :             int ch, ret;
     167             : 
     168           0 :             if (packet_size <= 0 || skip_size <= 0)
     169           0 :                 return AVERROR_INVALIDDATA;
     170             : 
     171           0 :             if (av_new_packet(pkt, packet_size) < 0)
     172           0 :                 return AVERROR(ENOMEM);
     173           0 :             dst = pkt->data;
     174           0 :             for (ch = 0; ch < st->codecpar->channels; ch++) {
     175           0 :                 ret = avio_read(pb, dst,  packet_size / st->codecpar->channels);
     176           0 :                 if (ret < packet_size / st->codecpar->channels)
     177           0 :                     return AVERROR_EOF;
     178             : 
     179           0 :                 dst += ret;
     180           0 :                 avio_skip(pb, skip_size / st->codecpar->channels);
     181             :             }
     182             : 
     183           0 :             return 0;
     184             :         }
     185             :     }
     186           0 :     return av_get_packet(pb, pkt, FFMIN(dsf->data_end - pos, st->codecpar->block_align));
     187             : }
     188             : 
     189             : AVInputFormat ff_dsf_demuxer = {
     190             :     .name           = "dsf",
     191             :     .long_name      = NULL_IF_CONFIG_SMALL("DSD Stream File (DSF)"),
     192             :     .priv_data_size = sizeof(DSFContext),
     193             :     .read_probe     = dsf_probe,
     194             :     .read_header    = dsf_read_header,
     195             :     .read_packet    = dsf_read_packet,
     196             :     .flags          = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK,
     197             : };

Generated by: LCOV version 1.13