1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

wvdec: split block header parsing into a separate file

It will be reused by other muxers and demuxers.
This commit is contained in:
Anton Khirnov 2013-05-28 09:33:43 +02:00
parent 0a1a94450a
commit 794ca87d2b
4 changed files with 145 additions and 53 deletions

View File

@ -334,7 +334,7 @@ OBJS-$(CONFIG_WSAUD_DEMUXER) += westwood_aud.o
OBJS-$(CONFIG_WSVQA_DEMUXER) += westwood_vqa.o OBJS-$(CONFIG_WSVQA_DEMUXER) += westwood_vqa.o
OBJS-$(CONFIG_WTV_DEMUXER) += wtv.o asfdec.o asf.o asfcrypt.o \ OBJS-$(CONFIG_WTV_DEMUXER) += wtv.o asfdec.o asf.o asfcrypt.o \
avlanguage.o mpegts.o isom.o avlanguage.o mpegts.o isom.o
OBJS-$(CONFIG_WV_DEMUXER) += wvdec.o apetag.o img2.o OBJS-$(CONFIG_WV_DEMUXER) += wvdec.o wv.o apetag.o img2.o
OBJS-$(CONFIG_XA_DEMUXER) += xa.o OBJS-$(CONFIG_XA_DEMUXER) += xa.o
OBJS-$(CONFIG_XMV_DEMUXER) += xmv.o OBJS-$(CONFIG_XMV_DEMUXER) += xmv.o
OBJS-$(CONFIG_XWMA_DEMUXER) += xwma.o OBJS-$(CONFIG_XWMA_DEMUXER) += xwma.o

52
libavformat/wv.c Normal file
View File

@ -0,0 +1,52 @@
/*
* WavPack shared functions
*
* This file is part of Libav.
*
* Libav is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* Libav is distributed in the hope that it will be useful,
* 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
* License along with Libav; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include <string.h>
#include "libavutil/common.h"
#include "libavutil/intreadwrite.h"
#include "wv.h"
int ff_wv_parse_header(WvHeader *wv, const uint8_t *data)
{
memset(wv, 0, sizeof(*wv));
if (AV_RL32(data) != MKTAG('w', 'v', 'p', 'k'))
return AVERROR_INVALIDDATA;
wv->blocksize = AV_RL32(data + 4);
if (wv->blocksize < 24 || wv->blocksize > WV_BLOCK_LIMIT)
return AVERROR_INVALIDDATA;
wv->blocksize -= 24;
wv->version = AV_RL16(data + 8);
wv->total_samples = AV_RL32(data + 12);
wv->block_idx = AV_RL32(data + 16);
wv->samples = AV_RL32(data + 20);
wv->flags = AV_RL32(data + 24);
wv->crc = AV_RL32(data + 28);
wv->initial = !!(wv->flags & WV_FLAG_INITIAL_BLOCK);
wv->final = !!(wv->flags & WV_FLAG_FINAL_BLOCK);
return 0;
}

56
libavformat/wv.h Normal file
View File

@ -0,0 +1,56 @@
/*
* WavPack shared functions
*
* This file is part of Libav.
*
* Libav is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* Libav is distributed in the hope that it will be useful,
* 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
* License along with Libav; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef AVFORMAT_WV_H
#define AVFORMAT_WV_H
#include <stdint.h>
#define WV_HEADER_SIZE 32
#define WV_FLAG_INITIAL_BLOCK (1 << 11)
#define WV_FLAG_FINAL_BLOCK (1 << 12)
// specs say that maximum block size is 1Mb
#define WV_BLOCK_LIMIT 1048576
typedef struct WvHeader {
uint32_t blocksize; //< size of the block data (excluding the header)
uint16_t version; //< bitstream version
uint32_t total_samples; //< total number of samples in the stream
uint32_t block_idx; //< index of the first sample in this block
uint32_t samples; //< number of samples in this block
uint32_t flags;
uint32_t crc;
int initial, final;
} WvHeader;
/**
* Parse a WavPack block header.
*
* @param wv this struct will be filled with parse header information
* @param data header data, must be WV_HEADER_SIZE bytes long
*
* @return 0 on success, a negative AVERROR code on failure
*/
int ff_wv_parse_header(WvHeader *wv, const uint8_t *data);
#endif /* AVFORMAT_WV_H */

View File

@ -26,15 +26,7 @@
#include "internal.h" #include "internal.h"
#include "apetag.h" #include "apetag.h"
#include "id3v1.h" #include "id3v1.h"
#include "wv.h"
// specs say that maximum block size is 1Mb
#define WV_BLOCK_LIMIT 1047576
#define WV_HEADER_SIZE 32
#define WV_START_BLOCK 0x0800
#define WV_END_BLOCK 0x1000
#define WV_SINGLE_BLOCK (WV_START_BLOCK | WV_END_BLOCK)
enum WV_FLAGS { enum WV_FLAGS {
WV_MONO = 0x0004, WV_MONO = 0x0004,
@ -57,10 +49,9 @@ static const int wv_rates[16] = {
typedef struct { typedef struct {
uint8_t block_header[WV_HEADER_SIZE]; uint8_t block_header[WV_HEADER_SIZE];
uint32_t blksize, flags; WvHeader header;
int rate, chan, bpp; int rate, chan, bpp;
uint32_t chmask; uint32_t chmask;
uint32_t samples, soff;
int multichannel; int multichannel;
int block_parsed; int block_parsed;
int64_t pos; int64_t pos;
@ -83,10 +74,9 @@ static int wv_probe(AVProbeData *p)
static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb) static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb)
{ {
WVContext *wc = ctx->priv_data; WVContext *wc = ctx->priv_data;
uint32_t ver; int ret;
int size, ret;
int rate, bpp, chan; int rate, bpp, chan;
uint32_t chmask; uint32_t chmask, flags;
wc->pos = avio_tell(pb); wc->pos = avio_tell(pb);
@ -98,39 +88,34 @@ static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb)
if (ret != WV_HEADER_SIZE) if (ret != WV_HEADER_SIZE)
return (ret < 0) ? ret : AVERROR_EOF; return (ret < 0) ? ret : AVERROR_EOF;
if (AV_RL32(wc->block_header) != MKTAG('w', 'v', 'p', 'k')) ret = ff_wv_parse_header(&wc->header, wc->block_header);
return AVERROR_INVALIDDATA; if (ret < 0) {
av_log(ctx, AV_LOG_ERROR, "Invalid block header.\n");
size = AV_RL32(wc->block_header + 4); return ret;
if (size < 24 || size > WV_BLOCK_LIMIT) {
av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size);
return AVERROR_INVALIDDATA;
} }
wc->blksize = size;
ver = AV_RL32(wc->block_header + 8); if (wc->header.version < 0x402 || wc->header.version > 0x410) {
if (ver < 0x402 || ver > 0x410) { av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", wc->header.version);
av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
return AVERROR_PATCHWELCOME; return AVERROR_PATCHWELCOME;
} }
wc->samples = AV_RL32(wc->block_header + 12); // total samples in file
wc->soff = AV_RL32(wc->block_header + 16); // offset in samples of current block
wc->flags = AV_RL32(wc->block_header + 24);
/* Blocks with zero samples don't contain actual audio information /* Blocks with zero samples don't contain actual audio information
* and should be ignored */ * and should be ignored */
if (!AV_RN32(wc->block_header + 20)) if (!wc->header.samples)
return 0; return 0;
// parse flags // parse flags
bpp = ((wc->flags & 3) + 1) << 3; flags = wc->header.flags;
chan = 1 + !(wc->flags & WV_MONO); bpp = ((flags & 3) + 1) << 3;
chmask = wc->flags & WV_MONO ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO; chan = 1 + !(flags & WV_MONO);
rate = wv_rates[(wc->flags >> 23) & 0xF]; chmask = flags & WV_MONO ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
wc->multichannel = !!((wc->flags & WV_SINGLE_BLOCK) != WV_SINGLE_BLOCK); rate = wv_rates[(flags >> 23) & 0xF];
wc->multichannel = !(wc->header.initial && wc->header.final);
if (wc->multichannel) { if (wc->multichannel) {
chan = wc->chan; chan = wc->chan;
chmask = wc->chmask; chmask = wc->chmask;
} }
if ((rate == -1 || !chan) && !wc->block_parsed) { if ((rate == -1 || !chan) && !wc->block_parsed) {
int64_t block_end = avio_tell(pb) + wc->blksize - 24; int64_t block_end = avio_tell(pb) + wc->header.blocksize;
if (!pb->seekable) { if (!pb->seekable) {
av_log(ctx, AV_LOG_ERROR, av_log(ctx, AV_LOG_ERROR,
"Cannot determine additional parameters\n"); "Cannot determine additional parameters\n");
@ -189,7 +174,7 @@ static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb)
"Cannot determine custom sampling rate\n"); "Cannot determine custom sampling rate\n");
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
avio_seek(pb, block_end - wc->blksize + 24, SEEK_SET); avio_seek(pb, block_end - wc->header.blocksize, SEEK_SET);
} }
if (!wc->bpp) if (!wc->bpp)
wc->bpp = bpp; wc->bpp = bpp;
@ -200,25 +185,24 @@ static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb)
if (!wc->rate) if (!wc->rate)
wc->rate = rate; wc->rate = rate;
if (wc->flags && bpp != wc->bpp) { if (flags && bpp != wc->bpp) {
av_log(ctx, AV_LOG_ERROR, av_log(ctx, AV_LOG_ERROR,
"Bits per sample differ, this block: %i, header block: %i\n", "Bits per sample differ, this block: %i, header block: %i\n",
bpp, wc->bpp); bpp, wc->bpp);
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
if (wc->flags && !wc->multichannel && chan != wc->chan) { if (flags && !wc->multichannel && chan != wc->chan) {
av_log(ctx, AV_LOG_ERROR, av_log(ctx, AV_LOG_ERROR,
"Channels differ, this block: %i, header block: %i\n", "Channels differ, this block: %i, header block: %i\n",
chan, wc->chan); chan, wc->chan);
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
if (wc->flags && rate != -1 && rate != wc->rate) { if (flags && rate != -1 && rate != wc->rate) {
av_log(ctx, AV_LOG_ERROR, av_log(ctx, AV_LOG_ERROR,
"Sampling rate differ, this block: %i, header block: %i\n", "Sampling rate differ, this block: %i, header block: %i\n",
rate, wc->rate); rate, wc->rate);
return AVERROR_INVALIDDATA; return AVERROR_INVALIDDATA;
} }
wc->blksize = size - 24;
return 0; return 0;
} }
@ -233,8 +217,8 @@ static int wv_read_header(AVFormatContext *s)
for (;;) { for (;;) {
if ((ret = wv_read_block_header(s, pb)) < 0) if ((ret = wv_read_block_header(s, pb)) < 0)
return ret; return ret;
if (!AV_RL32(wc->block_header + 20)) if (!wc->header.samples)
avio_skip(pb, wc->blksize - 24); avio_skip(pb, wc->header.blocksize);
else else
break; break;
} }
@ -251,7 +235,7 @@ static int wv_read_header(AVFormatContext *s)
st->codec->bits_per_coded_sample = wc->bpp; st->codec->bits_per_coded_sample = wc->bpp;
avpriv_set_pts_info(st, 64, 1, wc->rate); avpriv_set_pts_info(st, 64, 1, wc->rate);
st->start_time = 0; st->start_time = 0;
st->duration = wc->samples; st->duration = wc->header.total_samples;
if (s->pb->seekable) { if (s->pb->seekable) {
int64_t cur = avio_tell(s->pb); int64_t cur = avio_tell(s->pb);
@ -280,37 +264,37 @@ static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
} }
pos = wc->pos; pos = wc->pos;
if (av_new_packet(pkt, wc->blksize + WV_HEADER_SIZE) < 0) if (av_new_packet(pkt, wc->header.blocksize + WV_HEADER_SIZE) < 0)
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
memcpy(pkt->data, wc->block_header, WV_HEADER_SIZE); memcpy(pkt->data, wc->block_header, WV_HEADER_SIZE);
ret = avio_read(s->pb, pkt->data + WV_HEADER_SIZE, wc->blksize); ret = avio_read(s->pb, pkt->data + WV_HEADER_SIZE, wc->header.blocksize);
if (ret != wc->blksize) { if (ret != wc->header.blocksize) {
av_free_packet(pkt); av_free_packet(pkt);
return AVERROR(EIO); return AVERROR(EIO);
} }
while (!(wc->flags & WV_END_BLOCK)) { while (!(wc->header.flags & WV_FLAG_FINAL_BLOCK)) {
if ((ret = wv_read_block_header(s, s->pb)) < 0) { if ((ret = wv_read_block_header(s, s->pb)) < 0) {
av_free_packet(pkt); av_free_packet(pkt);
return ret; return ret;
} }
off = pkt->size; off = pkt->size;
if ((ret = av_grow_packet(pkt, WV_HEADER_SIZE + wc->blksize)) < 0) { if ((ret = av_grow_packet(pkt, WV_HEADER_SIZE + wc->header.blocksize)) < 0) {
av_free_packet(pkt); av_free_packet(pkt);
return ret; return ret;
} }
memcpy(pkt->data + off, wc->block_header, WV_HEADER_SIZE); memcpy(pkt->data + off, wc->block_header, WV_HEADER_SIZE);
ret = avio_read(s->pb, pkt->data + off + WV_HEADER_SIZE, wc->blksize); ret = avio_read(s->pb, pkt->data + off + WV_HEADER_SIZE, wc->header.blocksize);
if (ret != wc->blksize) { if (ret != wc->header.blocksize) {
av_free_packet(pkt); av_free_packet(pkt);
return (ret < 0) ? ret : AVERROR_EOF; return (ret < 0) ? ret : AVERROR_EOF;
} }
} }
pkt->stream_index = 0; pkt->stream_index = 0;
wc->block_parsed = 1; wc->block_parsed = 1;
pkt->pts = wc->soff; pkt->pts = wc->header.block_idx;
block_samples = AV_RL32(wc->block_header + 20); block_samples = wc->header.samples;
if (block_samples > INT32_MAX) if (block_samples > INT32_MAX)
av_log(s, AV_LOG_WARNING, av_log(s, AV_LOG_WARNING,
"Too many samples in block: %"PRIu32"\n", block_samples); "Too many samples in block: %"PRIu32"\n", block_samples);