libavformat/wvdec.c
730581f3
 /*
  * WavPack demuxer
8a485dd3
  * Copyright (c) 2006,2011 Konstantin Shishkov
730581f3
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
730581f3
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
730581f3
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
730581f3
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
730581f3
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
a903f8f0
 #include "libavutil/channel_layout.h"
6a5d31ac
 #include "libavutil/intreadwrite.h"
d2d67e42
 #include "libavutil/dict.h"
730581f3
 #include "avformat.h"
c3f9ebf7
 #include "internal.h"
c4d438c2
 #include "apetag.h"
 #include "id3v1.h"
794ca87d
 #include "wv.h"
8a485dd3
 
29d70274
 enum WV_FLAGS {
730581f3
     WV_MONO   = 0x0004,
     WV_HYBRID = 0x0008,
     WV_JOINT  = 0x0010,
     WV_CROSSD = 0x0020,
     WV_HSHAPE = 0x0040,
     WV_FLOAT  = 0x0080,
     WV_INT32  = 0x0100,
     WV_HBR    = 0x0200,
     WV_HBAL   = 0x0400,
     WV_MCINIT = 0x0800,
     WV_MCEND  = 0x1000,
 };
 
 static const int wv_rates[16] = {
29d70274
      6000,  8000,  9600, 11025, 12000, 16000,  22050, 24000,
     32000, 44100, 48000, 64000, 88200, 96000, 192000,    -1
730581f3
 };
 
29d70274
 typedef struct {
5074f454
     uint8_t block_header[WV_HEADER_SIZE];
794ca87d
     WvHeader header;
730581f3
     int rate, chan, bpp;
8a485dd3
     uint32_t chmask;
     int multichannel;
730581f3
     int block_parsed;
49561f99
     int64_t pos;
782e64fb
 
     int64_t apetag_start;
29d70274
 } WVContext;
730581f3
 
 static int wv_probe(AVProbeData *p)
 {
     /* check file header */
     if (p->buf_size <= 32)
         return 0;
fa23f94e
     if (AV_RL32(&p->buf[0]) == MKTAG('w', 'v', 'p', 'k') &&
         AV_RL32(&p->buf[4]) >= 24 &&
         AV_RL32(&p->buf[4]) <= WV_BLOCK_LIMIT &&
         AV_RL16(&p->buf[8]) >= 0x402 &&
         AV_RL16(&p->buf[8]) <= 0x410)
730581f3
         return AVPROBE_SCORE_MAX;
     else
         return 0;
 }
 
5074f454
 static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb)
730581f3
 {
     WVContext *wc = ctx->priv_data;
794ca87d
     int ret;
730581f3
     int rate, bpp, chan;
794ca87d
     uint32_t chmask, flags;
730581f3
 
384c9c2f
     wc->pos = avio_tell(pb);
782e64fb
 
     /* don't return bogus packets with the ape tag data */
     if (wc->apetag_start && wc->pos >= wc->apetag_start)
         return AVERROR_EOF;
 
5074f454
     ret = avio_read(pb, wc->block_header, WV_HEADER_SIZE);
     if (ret != WV_HEADER_SIZE)
         return (ret < 0) ? ret : AVERROR_EOF;
 
794ca87d
     ret = ff_wv_parse_header(&wc->header, wc->block_header);
     if (ret < 0) {
         av_log(ctx, AV_LOG_ERROR, "Invalid block header.\n");
         return ret;
5074f454
     }
794ca87d
 
     if (wc->header.version < 0x402 || wc->header.version > 0x410) {
         av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", wc->header.version);
5074f454
         return AVERROR_PATCHWELCOME;
8a485dd3
     }
794ca87d
 
29d70274
     /* Blocks with zero samples don't contain actual audio information
      * and should be ignored */
794ca87d
     if (!wc->header.samples)
cb7b55b0
         return 0;
29d70274
     // parse flags
794ca87d
     flags  = wc->header.flags;
     bpp    = ((flags & 3) + 1) << 3;
     chan   = 1 + !(flags & WV_MONO);
     chmask = flags & WV_MONO ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
     rate   = wv_rates[(flags >> 23) & 0xF];
     wc->multichannel = !(wc->header.initial && wc->header.final);
29d70274
     if (wc->multichannel) {
         chan   = wc->chan;
8a485dd3
         chmask = wc->chmask;
614e139a
     }
29d70274
     if ((rate == -1 || !chan) && !wc->block_parsed) {
794ca87d
         int64_t block_end = avio_tell(pb) + wc->header.blocksize;
29d70274
         if (!pb->seekable) {
             av_log(ctx, AV_LOG_ERROR,
                    "Cannot determine additional parameters\n");
c1d865d5
             return AVERROR_INVALIDDATA;
7aa2d42d
         }
29d70274
         while (avio_tell(pb) < block_end) {
7aa2d42d
             int id, size;
29d70274
             id   = avio_r8(pb);
e63a3628
             size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
7aa2d42d
             size <<= 1;
29d70274
             if (id & 0x40)
7aa2d42d
                 size--;
29d70274
             switch (id & 0x3F) {
8a485dd3
             case 0xD:
29d70274
                 if (size <= 1) {
                     av_log(ctx, AV_LOG_ERROR,
                            "Insufficient channel information\n");
c1d865d5
                     return AVERROR_INVALIDDATA;
8a485dd3
                 }
e63a3628
                 chan = avio_r8(pb);
29d70274
                 switch (size - 2) {
8a485dd3
                 case 0:
e63a3628
                     chmask = avio_r8(pb);
8a485dd3
                     break;
                 case 1:
e63a3628
                     chmask = avio_rl16(pb);
8a485dd3
                     break;
                 case 2:
e63a3628
                     chmask = avio_rl24(pb);
8a485dd3
                     break;
                 case 3:
e63a3628
                     chmask = avio_rl32(pb);
8a485dd3
                     break;
                 case 5:
45a8a02a
                     avio_skip(pb, 1);
29d70274
                     chan  |= (avio_r8(pb) & 0xF) << 8;
e63a3628
                     chmask = avio_rl24(pb);
8a485dd3
                     break;
                 default:
29d70274
                     av_log(ctx, AV_LOG_ERROR,
                            "Invalid channel info size %d\n", size);
c1d865d5
                     return AVERROR_INVALIDDATA;
8a485dd3
                 }
                 break;
             case 0x27:
e63a3628
                 rate = avio_rl24(pb);
7aa2d42d
                 break;
8a485dd3
             default:
45a8a02a
                 avio_skip(pb, size);
7aa2d42d
             }
29d70274
             if (id & 0x40)
45a8a02a
                 avio_skip(pb, 1);
7aa2d42d
         }
29d70274
         if (rate == -1) {
             av_log(ctx, AV_LOG_ERROR,
                    "Cannot determine custom sampling rate\n");
c1d865d5
             return AVERROR_INVALIDDATA;
7aa2d42d
         }
794ca87d
         avio_seek(pb, block_end - wc->header.blocksize, SEEK_SET);
730581f3
     }
29d70274
     if (!wc->bpp)
         wc->bpp    = bpp;
     if (!wc->chan)
         wc->chan   = chan;
     if (!wc->chmask)
         wc->chmask = chmask;
     if (!wc->rate)
         wc->rate   = rate;
730581f3
 
794ca87d
     if (flags && bpp != wc->bpp) {
29d70274
         av_log(ctx, AV_LOG_ERROR,
                "Bits per sample differ, this block: %i, header block: %i\n",
                bpp, wc->bpp);
c1d865d5
         return AVERROR_INVALIDDATA;
730581f3
     }
794ca87d
     if (flags && !wc->multichannel && chan != wc->chan) {
29d70274
         av_log(ctx, AV_LOG_ERROR,
                "Channels differ, this block: %i, header block: %i\n",
                chan, wc->chan);
c1d865d5
         return AVERROR_INVALIDDATA;
730581f3
     }
794ca87d
     if (flags && rate != -1 && rate != wc->rate) {
29d70274
         av_log(ctx, AV_LOG_ERROR,
                "Sampling rate differ, this block: %i, header block: %i\n",
                rate, wc->rate);
c1d865d5
         return AVERROR_INVALIDDATA;
730581f3
     }
     return 0;
 }
 
6e9651d1
 static int wv_read_header(AVFormatContext *s)
730581f3
 {
471fe57e
     AVIOContext *pb = s->pb;
730581f3
     WVContext *wc = s->priv_data;
     AVStream *st;
c1d865d5
     int ret;
730581f3
 
aa926a48
     wc->block_parsed = 0;
29d70274
     for (;;) {
5074f454
         if ((ret = wv_read_block_header(s, pb)) < 0)
c1d865d5
             return ret;
794ca87d
         if (!wc->header.samples)
             avio_skip(pb, wc->header.blocksize);
cb7b55b0
         else
             break;
     }
730581f3
 
     /* now we are ready: build format streams */
3b3bbdd3
     st = avformat_new_stream(s, NULL);
730581f3
     if (!st)
c1d865d5
         return AVERROR(ENOMEM);
29d70274
     st->codec->codec_type            = AVMEDIA_TYPE_AUDIO;
36ef5369
     st->codec->codec_id              = AV_CODEC_ID_WAVPACK;
29d70274
     st->codec->channels              = wc->chan;
     st->codec->channel_layout        = wc->chmask;
     st->codec->sample_rate           = wc->rate;
dd1c8f3e
     st->codec->bits_per_coded_sample = wc->bpp;
c3f9ebf7
     avpriv_set_pts_info(st, 64, 1, wc->rate);
0fc07ad9
     st->start_time = 0;
53015bb3
     if (wc->header.total_samples != 0xFFFFFFFFu)
         st->duration = wc->header.total_samples;
c4d438c2
 
29d70274
     if (s->pb->seekable) {
384c9c2f
         int64_t cur = avio_tell(s->pb);
782e64fb
         wc->apetag_start = ff_ape_parse_tag(s);
29d70274
         if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
c4d438c2
             ff_id3v1_read(s);
f59d8ff8
         avio_seek(s->pb, cur, SEEK_SET);
c4d438c2
     }
 
730581f3
     return 0;
 }
 
29d70274
 static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
730581f3
 {
     WVContext *wc = s->priv_data;
827f0561
     int ret;
5074f454
     int off;
5561fe48
     int64_t pos;
0f3d8bae
     uint32_t block_samples;
730581f3
 
899681cd
     if (url_feof(s->pb))
ccc10acb
         return AVERROR_EOF;
29d70274
     if (wc->block_parsed) {
5074f454
         if ((ret = wv_read_block_header(s, s->pb)) < 0)
c1d865d5
             return ret;
730581f3
     }
 
5561fe48
     pos = wc->pos;
794ca87d
     if (av_new_packet(pkt, wc->header.blocksize + WV_HEADER_SIZE) < 0)
769e10f0
         return AVERROR(ENOMEM);
5074f454
     memcpy(pkt->data, wc->block_header, WV_HEADER_SIZE);
794ca87d
     ret = avio_read(s->pb, pkt->data + WV_HEADER_SIZE, wc->header.blocksize);
     if (ret != wc->header.blocksize) {
730581f3
         av_free_packet(pkt);
6f3e0b21
         return AVERROR(EIO);
730581f3
     }
794ca87d
     while (!(wc->header.flags & WV_FLAG_FINAL_BLOCK)) {
5074f454
         if ((ret = wv_read_block_header(s, s->pb)) < 0) {
8a485dd3
             av_free_packet(pkt);
             return ret;
         }
 
5074f454
         off = pkt->size;
794ca87d
         if ((ret = av_grow_packet(pkt, WV_HEADER_SIZE + wc->header.blocksize)) < 0) {
8a485dd3
             av_free_packet(pkt);
c1d865d5
             return ret;
8a485dd3
         }
5074f454
         memcpy(pkt->data + off, wc->block_header, WV_HEADER_SIZE);
 
794ca87d
         ret = avio_read(s->pb, pkt->data + off + WV_HEADER_SIZE, wc->header.blocksize);
         if (ret != wc->header.blocksize) {
8a485dd3
             av_free_packet(pkt);
5074f454
             return (ret < 0) ? ret : AVERROR_EOF;
8a485dd3
         }
     }
730581f3
     pkt->stream_index = 0;
29d70274
     wc->block_parsed  = 1;
794ca87d
     pkt->pts          = wc->header.block_idx;
     block_samples     = wc->header.samples;
0f3d8bae
     if (block_samples > INT32_MAX)
29d70274
         av_log(s, AV_LOG_WARNING,
                "Too many samples in block: %"PRIu32"\n", block_samples);
0f3d8bae
     else
         pkt->duration = block_samples;
 
5561fe48
     av_add_index_entry(s->streams[0], pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
730581f3
     return 0;
 }
 
29d70274
 static int wv_read_seek(AVFormatContext *s, int stream_index,
                         int64_t timestamp, int flags)
49561f99
 {
29d70274
     AVStream  *st = s->streams[stream_index];
49561f99
     WVContext *wc = s->priv_data;
     AVPacket pkt1, *pkt = &pkt1;
     int ret;
     int index = av_index_search_timestamp(st, timestamp, flags);
     int64_t pos, pts;
 
     /* if found, seek there */
ccb919e3
     if (index >= 0 &&
         timestamp <= st->index_entries[st->nb_index_entries - 1].timestamp) {
49561f99
         wc->block_parsed = 1;
f59d8ff8
         avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
49561f99
         return 0;
     }
     /* if timestamp is out of bounds, return error */
29d70274
     if (timestamp < 0 || timestamp >= s->duration)
c1d865d5
         return AVERROR(EINVAL);
49561f99
 
384c9c2f
     pos = avio_tell(s->pb);
29d70274
     do {
49561f99
         ret = av_read_frame(s, pkt);
29d70274
         if (ret < 0) {
f59d8ff8
             avio_seek(s->pb, pos, SEEK_SET);
c1d865d5
             return ret;
49561f99
         }
         pts = pkt->pts;
         av_free_packet(pkt);
29d70274
     } while(pts < timestamp);
49561f99
     return 0;
 }
 
66355be3
 AVInputFormat ff_wv_demuxer = {
dfc2c4d9
     .name           = "wv",
     .long_name      = NULL_IF_CONFIG_SMALL("WavPack"),
     .priv_data_size = sizeof(WVContext),
     .read_probe     = wv_probe,
     .read_header    = wv_read_header,
     .read_packet    = wv_read_packet,
     .read_seek      = wv_read_seek,
730581f3
 };