annotate ffmpeg/libavformat/westwood_aud.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * Westwood Studios AUD Format Demuxer
yading@11 3 * Copyright (c) 2003 The ffmpeg Project
yading@11 4 *
yading@11 5 * This file is part of FFmpeg.
yading@11 6 *
yading@11 7 * FFmpeg is free software; you can redistribute it and/or
yading@11 8 * modify it under the terms of the GNU Lesser General Public
yading@11 9 * License as published by the Free Software Foundation; either
yading@11 10 * version 2.1 of the License, or (at your option) any later version.
yading@11 11 *
yading@11 12 * FFmpeg is distributed in the hope that it will be useful,
yading@11 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 15 * Lesser General Public License for more details.
yading@11 16 *
yading@11 17 * You should have received a copy of the GNU Lesser General Public
yading@11 18 * License along with FFmpeg; if not, write to the Free Software
yading@11 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 20 */
yading@11 21
yading@11 22 /**
yading@11 23 * @file
yading@11 24 * Westwood Studios AUD file demuxer
yading@11 25 * by Mike Melanson (melanson@pcisys.net)
yading@11 26 * for more information on the Westwood file formats, visit:
yading@11 27 * http://www.pcisys.net/~melanson/codecs/
yading@11 28 * http://www.geocities.com/SiliconValley/8682/aud3.txt
yading@11 29 *
yading@11 30 * Implementation note: There is no definite file signature for AUD files.
yading@11 31 * The demuxer uses a probabilistic strategy for content detection. This
yading@11 32 * entails performing sanity checks on certain header values in order to
yading@11 33 * qualify a file. Refer to wsaud_probe() for the precise parameters.
yading@11 34 */
yading@11 35
yading@11 36 #include "libavutil/channel_layout.h"
yading@11 37 #include "libavutil/intreadwrite.h"
yading@11 38 #include "avformat.h"
yading@11 39 #include "internal.h"
yading@11 40
yading@11 41 #define AUD_HEADER_SIZE 12
yading@11 42 #define AUD_CHUNK_PREAMBLE_SIZE 8
yading@11 43 #define AUD_CHUNK_SIGNATURE 0x0000DEAF
yading@11 44
yading@11 45 static int wsaud_probe(AVProbeData *p)
yading@11 46 {
yading@11 47 int field;
yading@11 48
yading@11 49 /* Probabilistic content detection strategy: There is no file signature
yading@11 50 * so perform sanity checks on various header parameters:
yading@11 51 * 8000 <= sample rate (16 bits) <= 48000 ==> 40001 acceptable numbers
yading@11 52 * flags <= 0x03 (2 LSBs are used) ==> 4 acceptable numbers
yading@11 53 * compression type (8 bits) = 1 or 99 ==> 2 acceptable numbers
yading@11 54 * first audio chunk signature (32 bits) ==> 1 acceptable number
yading@11 55 * The number space contains 2^64 numbers. There are 40001 * 4 * 2 * 1 =
yading@11 56 * 320008 acceptable number combinations.
yading@11 57 */
yading@11 58
yading@11 59 if (p->buf_size < AUD_HEADER_SIZE + AUD_CHUNK_PREAMBLE_SIZE)
yading@11 60 return 0;
yading@11 61
yading@11 62 /* check sample rate */
yading@11 63 field = AV_RL16(&p->buf[0]);
yading@11 64 if ((field < 8000) || (field > 48000))
yading@11 65 return 0;
yading@11 66
yading@11 67 /* enforce the rule that the top 6 bits of this flags field are reserved (0);
yading@11 68 * this might not be true, but enforce it until deemed unnecessary */
yading@11 69 if (p->buf[10] & 0xFC)
yading@11 70 return 0;
yading@11 71
yading@11 72 if (p->buf[11] != 99 && p->buf[11] != 1)
yading@11 73 return 0;
yading@11 74
yading@11 75 /* read ahead to the first audio chunk and validate the first header signature */
yading@11 76 if (AV_RL32(&p->buf[16]) != AUD_CHUNK_SIGNATURE)
yading@11 77 return 0;
yading@11 78
yading@11 79 /* return 1/2 certainty since this file check is a little sketchy */
yading@11 80 return AVPROBE_SCORE_MAX / 2;
yading@11 81 }
yading@11 82
yading@11 83 static int wsaud_read_header(AVFormatContext *s)
yading@11 84 {
yading@11 85 AVIOContext *pb = s->pb;
yading@11 86 AVStream *st;
yading@11 87 unsigned char header[AUD_HEADER_SIZE];
yading@11 88 int sample_rate, channels, codec;
yading@11 89
yading@11 90 if (avio_read(pb, header, AUD_HEADER_SIZE) != AUD_HEADER_SIZE)
yading@11 91 return AVERROR(EIO);
yading@11 92
yading@11 93 sample_rate = AV_RL16(&header[0]);
yading@11 94 channels = (header[10] & 0x1) + 1;
yading@11 95 codec = header[11];
yading@11 96
yading@11 97 /* initialize the audio decoder stream */
yading@11 98 st = avformat_new_stream(s, NULL);
yading@11 99 if (!st)
yading@11 100 return AVERROR(ENOMEM);
yading@11 101
yading@11 102 switch (codec) {
yading@11 103 case 1:
yading@11 104 if (channels != 1) {
yading@11 105 avpriv_request_sample(s, "Stereo WS-SND1");
yading@11 106 return AVERROR_PATCHWELCOME;
yading@11 107 }
yading@11 108 st->codec->codec_id = AV_CODEC_ID_WESTWOOD_SND1;
yading@11 109 break;
yading@11 110 case 99:
yading@11 111 st->codec->codec_id = AV_CODEC_ID_ADPCM_IMA_WS;
yading@11 112 st->codec->bits_per_coded_sample = 4;
yading@11 113 st->codec->bit_rate = channels * sample_rate * 4;
yading@11 114 break;
yading@11 115 default:
yading@11 116 avpriv_request_sample(s, "Unknown codec: %d", codec);
yading@11 117 return AVERROR_PATCHWELCOME;
yading@11 118 }
yading@11 119 avpriv_set_pts_info(st, 64, 1, sample_rate);
yading@11 120 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
yading@11 121 st->codec->channels = channels;
yading@11 122 st->codec->channel_layout = channels == 1 ? AV_CH_LAYOUT_MONO :
yading@11 123 AV_CH_LAYOUT_STEREO;
yading@11 124 st->codec->sample_rate = sample_rate;
yading@11 125
yading@11 126 return 0;
yading@11 127 }
yading@11 128
yading@11 129 static int wsaud_read_packet(AVFormatContext *s,
yading@11 130 AVPacket *pkt)
yading@11 131 {
yading@11 132 AVIOContext *pb = s->pb;
yading@11 133 unsigned char preamble[AUD_CHUNK_PREAMBLE_SIZE];
yading@11 134 unsigned int chunk_size;
yading@11 135 int ret = 0;
yading@11 136 AVStream *st = s->streams[0];
yading@11 137
yading@11 138 if (avio_read(pb, preamble, AUD_CHUNK_PREAMBLE_SIZE) !=
yading@11 139 AUD_CHUNK_PREAMBLE_SIZE)
yading@11 140 return AVERROR(EIO);
yading@11 141
yading@11 142 /* validate the chunk */
yading@11 143 if (AV_RL32(&preamble[4]) != AUD_CHUNK_SIGNATURE)
yading@11 144 return AVERROR_INVALIDDATA;
yading@11 145
yading@11 146 chunk_size = AV_RL16(&preamble[0]);
yading@11 147
yading@11 148 if (st->codec->codec_id == AV_CODEC_ID_WESTWOOD_SND1) {
yading@11 149 /* For Westwood SND1 audio we need to add the output size and input
yading@11 150 size to the start of the packet to match what is in VQA.
yading@11 151 Specifically, this is needed to signal when a packet should be
yading@11 152 decoding as raw 8-bit pcm or variable-size ADPCM. */
yading@11 153 int out_size = AV_RL16(&preamble[2]);
yading@11 154 if ((ret = av_new_packet(pkt, chunk_size + 4)))
yading@11 155 return ret;
yading@11 156 if ((ret = avio_read(pb, &pkt->data[4], chunk_size)) != chunk_size)
yading@11 157 return ret < 0 ? ret : AVERROR(EIO);
yading@11 158 AV_WL16(&pkt->data[0], out_size);
yading@11 159 AV_WL16(&pkt->data[2], chunk_size);
yading@11 160
yading@11 161 pkt->duration = out_size;
yading@11 162 } else {
yading@11 163 ret = av_get_packet(pb, pkt, chunk_size);
yading@11 164 if (ret != chunk_size)
yading@11 165 return AVERROR(EIO);
yading@11 166
yading@11 167 /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
yading@11 168 pkt->duration = (chunk_size * 2) / st->codec->channels;
yading@11 169 }
yading@11 170 pkt->stream_index = st->index;
yading@11 171
yading@11 172 return ret;
yading@11 173 }
yading@11 174
yading@11 175 AVInputFormat ff_wsaud_demuxer = {
yading@11 176 .name = "wsaud",
yading@11 177 .long_name = NULL_IF_CONFIG_SMALL("Westwood Studios audio"),
yading@11 178 .read_probe = wsaud_probe,
yading@11 179 .read_header = wsaud_read_header,
yading@11 180 .read_packet = wsaud_read_packet,
yading@11 181 };