LCOV - code coverage report
Current view: top level - libavformat - wvdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 125 171 73.1 %
Date: 2017-12-17 16:07:53 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :  * WavPack demuxer
       3             :  * Copyright (c) 2006,2011 Konstantin Shishkov
       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/channel_layout.h"
      23             : #include "libavutil/intreadwrite.h"
      24             : #include "libavutil/dict.h"
      25             : #include "avformat.h"
      26             : #include "internal.h"
      27             : #include "apetag.h"
      28             : #include "id3v1.h"
      29             : #include "wv.h"
      30             : 
      31             : enum WV_FLAGS {
      32             :     WV_MONO   = 0x0004,
      33             :     WV_HYBRID = 0x0008,
      34             :     WV_JOINT  = 0x0010,
      35             :     WV_CROSSD = 0x0020,
      36             :     WV_HSHAPE = 0x0040,
      37             :     WV_FLOAT  = 0x0080,
      38             :     WV_INT32  = 0x0100,
      39             :     WV_HBR    = 0x0200,
      40             :     WV_HBAL   = 0x0400,
      41             :     WV_MCINIT = 0x0800,
      42             :     WV_MCEND  = 0x1000,
      43             : };
      44             : 
      45             : static const int wv_rates[16] = {
      46             :      6000,  8000,  9600, 11025, 12000, 16000,  22050, 24000,
      47             :     32000, 44100, 48000, 64000, 88200, 96000, 192000,    -1
      48             : };
      49             : 
      50             : typedef struct WVContext {
      51             :     uint8_t block_header[WV_HEADER_SIZE];
      52             :     WvHeader header;
      53             :     int rate, chan, bpp;
      54             :     uint32_t chmask;
      55             :     int multichannel;
      56             :     int block_parsed;
      57             :     int64_t pos;
      58             : 
      59             :     int64_t apetag_start;
      60             : } WVContext;
      61             : 
      62        6130 : static int wv_probe(AVProbeData *p)
      63             : {
      64             :     /* check file header */
      65        6130 :     if (p->buf_size <= 32)
      66           0 :         return 0;
      67        6162 :     if (AV_RL32(&p->buf[0]) == MKTAG('w', 'v', 'p', 'k') &&
      68          64 :         AV_RL32(&p->buf[4]) >= 24 &&
      69          64 :         AV_RL32(&p->buf[4]) <= WV_BLOCK_LIMIT &&
      70          64 :         AV_RL16(&p->buf[8]) >= 0x402 &&
      71          32 :         AV_RL16(&p->buf[8]) <= 0x410)
      72          32 :         return AVPROBE_SCORE_MAX;
      73             :     else
      74        6098 :         return 0;
      75             : }
      76             : 
      77        8241 : static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb)
      78             : {
      79        8241 :     WVContext *wc = ctx->priv_data;
      80             :     int ret;
      81             :     int rate, bpp, chan;
      82             :     uint32_t chmask, flags;
      83             : 
      84        8241 :     wc->pos = avio_tell(pb);
      85             : 
      86             :     /* don't return bogus packets with the ape tag data */
      87        8241 :     if (wc->apetag_start && wc->pos >= wc->apetag_start)
      88           3 :         return AVERROR_EOF;
      89             : 
      90        8238 :     ret = avio_read(pb, wc->block_header, WV_HEADER_SIZE);
      91        8238 :     if (ret != WV_HEADER_SIZE)
      92          11 :         return (ret < 0) ? ret : AVERROR_EOF;
      93             : 
      94        8227 :     ret = ff_wv_parse_header(&wc->header, wc->block_header);
      95        8227 :     if (ret < 0) {
      96           0 :         av_log(ctx, AV_LOG_ERROR, "Invalid block header.\n");
      97           0 :         return ret;
      98             :     }
      99             : 
     100        8227 :     if (wc->header.version < 0x402 || wc->header.version > 0x410) {
     101           0 :         avpriv_report_missing_feature(ctx, "WV version 0x%03X",
     102           0 :                                       wc->header.version);
     103           0 :         return AVERROR_PATCHWELCOME;
     104             :     }
     105             : 
     106             :     /* Blocks with zero samples don't contain actual audio information
     107             :      * and should be ignored */
     108        8227 :     if (!wc->header.samples)
     109           5 :         return 0;
     110             :     // parse flags
     111        8222 :     flags  = wc->header.flags;
     112        8222 :     bpp    = ((flags & 3) + 1) << 3;
     113        8222 :     chan   = 1 + !(flags & WV_MONO);
     114        8222 :     chmask = flags & WV_MONO ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
     115        8222 :     rate   = wv_rates[(flags >> 23) & 0xF];
     116        8222 :     wc->multichannel = !(wc->header.initial && wc->header.final);
     117        8222 :     if (wc->multichannel) {
     118         958 :         chan   = wc->chan;
     119         958 :         chmask = wc->chmask;
     120             :     }
     121        8222 :     if ((rate == -1 || !chan) && !wc->block_parsed) {
     122           9 :         int64_t block_end = avio_tell(pb) + wc->header.blocksize;
     123           9 :         if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
     124           0 :             av_log(ctx, AV_LOG_ERROR,
     125             :                    "Cannot determine additional parameters\n");
     126           0 :             return AVERROR_INVALIDDATA;
     127             :         }
     128          95 :         while (avio_tell(pb) < block_end && !avio_feof(pb)) {
     129             :             int id, size;
     130          77 :             id   = avio_r8(pb);
     131          77 :             size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
     132          77 :             size <<= 1;
     133          77 :             if (id & 0x40)
     134          18 :                 size--;
     135          77 :             switch (id & 0x3F) {
     136           9 :             case 0xD:
     137           9 :                 if (size <= 1) {
     138           0 :                     av_log(ctx, AV_LOG_ERROR,
     139             :                            "Insufficient channel information\n");
     140           0 :                     return AVERROR_INVALIDDATA;
     141             :                 }
     142           9 :                 chan = avio_r8(pb);
     143           9 :                 switch (size - 2) {
     144           0 :                 case 0:
     145           0 :                     chmask = avio_r8(pb);
     146           0 :                     break;
     147           9 :                 case 1:
     148           9 :                     chmask = avio_rl16(pb);
     149           9 :                     break;
     150           0 :                 case 2:
     151           0 :                     chmask = avio_rl24(pb);
     152           0 :                     break;
     153           0 :                 case 3:
     154           0 :                     chmask = avio_rl32(pb);
     155           0 :                     break;
     156           0 :                 case 5:
     157           0 :                     avio_skip(pb, 1);
     158           0 :                     chan  |= (avio_r8(pb) & 0xF) << 8;
     159           0 :                     chmask = avio_rl24(pb);
     160           0 :                     break;
     161           0 :                 default:
     162           0 :                     av_log(ctx, AV_LOG_ERROR,
     163             :                            "Invalid channel info size %d\n", size);
     164           0 :                     return AVERROR_INVALIDDATA;
     165             :                 }
     166           9 :                 break;
     167           0 :             case 0x27:
     168           0 :                 rate = avio_rl24(pb);
     169           0 :                 break;
     170          68 :             default:
     171          68 :                 avio_skip(pb, size);
     172             :             }
     173          77 :             if (id & 0x40)
     174          18 :                 avio_skip(pb, 1);
     175             :         }
     176           9 :         if (rate == -1) {
     177           0 :             av_log(ctx, AV_LOG_ERROR,
     178             :                    "Cannot determine custom sampling rate\n");
     179           0 :             return AVERROR_INVALIDDATA;
     180             :         }
     181           9 :         avio_seek(pb, block_end - wc->header.blocksize, SEEK_SET);
     182             :     }
     183        8222 :     if (!wc->bpp)
     184          32 :         wc->bpp    = bpp;
     185        8222 :     if (!wc->chan)
     186          32 :         wc->chan   = chan;
     187        8222 :     if (!wc->chmask)
     188          32 :         wc->chmask = chmask;
     189        8222 :     if (!wc->rate)
     190          32 :         wc->rate   = rate;
     191             : 
     192        8222 :     if (flags && bpp != wc->bpp) {
     193           0 :         av_log(ctx, AV_LOG_ERROR,
     194             :                "Bits per sample differ, this block: %i, header block: %i\n",
     195             :                bpp, wc->bpp);
     196           0 :         return AVERROR_INVALIDDATA;
     197             :     }
     198        8222 :     if (flags && !wc->multichannel && chan != wc->chan) {
     199           0 :         av_log(ctx, AV_LOG_ERROR,
     200             :                "Channels differ, this block: %i, header block: %i\n",
     201             :                chan, wc->chan);
     202           0 :         return AVERROR_INVALIDDATA;
     203             :     }
     204        8222 :     if (flags && rate != -1 && rate != wc->rate) {
     205           0 :         av_log(ctx, AV_LOG_ERROR,
     206             :                "Sampling rate differ, this block: %i, header block: %i\n",
     207             :                rate, wc->rate);
     208           0 :         return AVERROR_INVALIDDATA;
     209             :     }
     210        8222 :     return 0;
     211             : }
     212             : 
     213          32 : static int wv_read_header(AVFormatContext *s)
     214             : {
     215          32 :     AVIOContext *pb = s->pb;
     216          32 :     WVContext *wc = s->priv_data;
     217             :     AVStream *st;
     218             :     int ret;
     219             : 
     220          32 :     wc->block_parsed = 0;
     221             :     for (;;) {
     222          32 :         if ((ret = wv_read_block_header(s, pb)) < 0)
     223           0 :             return ret;
     224          32 :         if (!wc->header.samples)
     225           0 :             avio_skip(pb, wc->header.blocksize);
     226             :         else
     227          32 :             break;
     228             :     }
     229             : 
     230             :     /* now we are ready: build format streams */
     231          32 :     st = avformat_new_stream(s, NULL);
     232          32 :     if (!st)
     233           0 :         return AVERROR(ENOMEM);
     234          32 :     st->codecpar->codec_type            = AVMEDIA_TYPE_AUDIO;
     235          32 :     st->codecpar->codec_id              = AV_CODEC_ID_WAVPACK;
     236          32 :     st->codecpar->channels              = wc->chan;
     237          32 :     st->codecpar->channel_layout        = wc->chmask;
     238          32 :     st->codecpar->sample_rate           = wc->rate;
     239          32 :     st->codecpar->bits_per_coded_sample = wc->bpp;
     240          32 :     avpriv_set_pts_info(st, 64, 1, wc->rate);
     241          32 :     st->start_time = 0;
     242          32 :     if (wc->header.total_samples != 0xFFFFFFFFu)
     243          32 :         st->duration = wc->header.total_samples;
     244             : 
     245          32 :     if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
     246          32 :         int64_t cur = avio_tell(s->pb);
     247          32 :         wc->apetag_start = ff_ape_parse_tag(s);
     248          32 :         if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
     249          29 :             ff_id3v1_read(s);
     250          32 :         avio_seek(s->pb, cur, SEEK_SET);
     251             :     }
     252             : 
     253          32 :     return 0;
     254             : }
     255             : 
     256        7562 : static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
     257             : {
     258        7562 :     WVContext *wc = s->priv_data;
     259             :     int ret;
     260             :     int off;
     261             :     int64_t pos;
     262             :     uint32_t block_samples;
     263             : 
     264        7562 :     if (avio_feof(s->pb))
     265           0 :         return AVERROR_EOF;
     266        7562 :     if (wc->block_parsed) {
     267        7530 :         if ((ret = wv_read_block_header(s, s->pb)) < 0)
     268           9 :             return ret;
     269             :     }
     270             : 
     271        7553 :     pos = wc->pos;
     272        7553 :     if (av_new_packet(pkt, wc->header.blocksize + WV_HEADER_SIZE) < 0)
     273           0 :         return AVERROR(ENOMEM);
     274        7553 :     memcpy(pkt->data, wc->block_header, WV_HEADER_SIZE);
     275        7553 :     ret = avio_read(s->pb, pkt->data + WV_HEADER_SIZE, wc->header.blocksize);
     276        7553 :     if (ret != wc->header.blocksize) {
     277          17 :         av_packet_unref(pkt);
     278          17 :         return AVERROR(EIO);
     279             :     }
     280       15745 :     while (!(wc->header.flags & WV_FLAG_FINAL_BLOCK)) {
     281         679 :         if ((ret = wv_read_block_header(s, s->pb)) < 0) {
     282           5 :             av_packet_unref(pkt);
     283           5 :             return ret;
     284             :         }
     285             : 
     286         674 :         off = pkt->size;
     287         674 :         if ((ret = av_grow_packet(pkt, WV_HEADER_SIZE + wc->header.blocksize)) < 0) {
     288           0 :             av_packet_unref(pkt);
     289           0 :             return ret;
     290             :         }
     291         674 :         memcpy(pkt->data + off, wc->block_header, WV_HEADER_SIZE);
     292             : 
     293         674 :         ret = avio_read(s->pb, pkt->data + off + WV_HEADER_SIZE, wc->header.blocksize);
     294         674 :         if (ret != wc->header.blocksize) {
     295           1 :             av_packet_unref(pkt);
     296           1 :             return (ret < 0) ? ret : AVERROR_EOF;
     297             :         }
     298             :     }
     299        7530 :     pkt->stream_index = 0;
     300        7530 :     pkt->pos          = pos;
     301        7530 :     wc->block_parsed  = 1;
     302        7530 :     pkt->pts          = wc->header.block_idx;
     303        7530 :     block_samples     = wc->header.samples;
     304        7530 :     if (block_samples > INT32_MAX)
     305           0 :         av_log(s, AV_LOG_WARNING,
     306             :                "Too many samples in block: %"PRIu32"\n", block_samples);
     307             :     else
     308        7530 :         pkt->duration = block_samples;
     309             : 
     310        7530 :     return 0;
     311             : }
     312             : 
     313             : AVInputFormat ff_wv_demuxer = {
     314             :     .name           = "wv",
     315             :     .long_name      = NULL_IF_CONFIG_SMALL("WavPack"),
     316             :     .priv_data_size = sizeof(WVContext),
     317             :     .read_probe     = wv_probe,
     318             :     .read_header    = wv_read_header,
     319             :     .read_packet    = wv_read_packet,
     320             :     .flags          = AVFMT_GENERIC_INDEX,
     321             : };

Generated by: LCOV version 1.13