westwood.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Westwood Studios Multimedia Formats Demuxer (VQA, AUD)
  3. * Copyright (c) 2003 The ffmpeg Project
  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. * @file
  23. * Westwood Studios VQA & AUD file demuxers
  24. * by Mike Melanson (melanson@pcisys.net)
  25. * for more information on the Westwood file formats, visit:
  26. * http://www.pcisys.net/~melanson/codecs/
  27. * http://www.geocities.com/SiliconValley/8682/aud3.txt
  28. *
  29. * Implementation note: There is no definite file signature for AUD files.
  30. * The demuxer uses a probabilistic strategy for content detection. This
  31. * entails performing sanity checks on certain header values in order to
  32. * qualify a file. Refer to wsaud_probe() for the precise parameters.
  33. */
  34. #include "libavutil/intreadwrite.h"
  35. #include "avformat.h"
  36. #define AUD_HEADER_SIZE 12
  37. #define AUD_CHUNK_PREAMBLE_SIZE 8
  38. #define AUD_CHUNK_SIGNATURE 0x0000DEAF
  39. #define FORM_TAG MKBETAG('F', 'O', 'R', 'M')
  40. #define WVQA_TAG MKBETAG('W', 'V', 'Q', 'A')
  41. #define VQHD_TAG MKBETAG('V', 'Q', 'H', 'D')
  42. #define FINF_TAG MKBETAG('F', 'I', 'N', 'F')
  43. #define SND0_TAG MKBETAG('S', 'N', 'D', '0')
  44. #define SND1_TAG MKBETAG('S', 'N', 'D', '1')
  45. #define SND2_TAG MKBETAG('S', 'N', 'D', '2')
  46. #define VQFR_TAG MKBETAG('V', 'Q', 'F', 'R')
  47. /* don't know what these tags are for, but acknowledge their existence */
  48. #define CINF_TAG MKBETAG('C', 'I', 'N', 'F')
  49. #define CINH_TAG MKBETAG('C', 'I', 'N', 'H')
  50. #define CIND_TAG MKBETAG('C', 'I', 'N', 'D')
  51. #define PINF_TAG MKBETAG('P', 'I', 'N', 'F')
  52. #define PINH_TAG MKBETAG('P', 'I', 'N', 'H')
  53. #define PIND_TAG MKBETAG('P', 'I', 'N', 'D')
  54. #define CMDS_TAG MKBETAG('C', 'M', 'D', 'S')
  55. #define VQA_HEADER_SIZE 0x2A
  56. #define VQA_FRAMERATE 15
  57. #define VQA_PREAMBLE_SIZE 8
  58. typedef struct WsAudDemuxContext {
  59. int audio_samplerate;
  60. int audio_channels;
  61. int audio_bits;
  62. enum CodecID audio_type;
  63. int audio_stream_index;
  64. int64_t audio_frame_counter;
  65. } WsAudDemuxContext;
  66. typedef struct WsVqaDemuxContext {
  67. int audio_samplerate;
  68. int audio_channels;
  69. int audio_bits;
  70. int audio_stream_index;
  71. int video_stream_index;
  72. int64_t audio_frame_counter;
  73. } WsVqaDemuxContext;
  74. static int wsaud_probe(AVProbeData *p)
  75. {
  76. int field;
  77. /* Probabilistic content detection strategy: There is no file signature
  78. * so perform sanity checks on various header parameters:
  79. * 8000 <= sample rate (16 bits) <= 48000 ==> 40001 acceptable numbers
  80. * flags <= 0x03 (2 LSBs are used) ==> 4 acceptable numbers
  81. * compression type (8 bits) = 1 or 99 ==> 2 acceptable numbers
  82. * first audio chunk signature (32 bits) ==> 1 acceptable number
  83. * The number space contains 2^64 numbers. There are 40001 * 4 * 2 * 1 =
  84. * 320008 acceptable number combinations.
  85. */
  86. if (p->buf_size < AUD_HEADER_SIZE + AUD_CHUNK_PREAMBLE_SIZE)
  87. return 0;
  88. /* check sample rate */
  89. field = AV_RL16(&p->buf[0]);
  90. if ((field < 8000) || (field > 48000))
  91. return 0;
  92. /* enforce the rule that the top 6 bits of this flags field are reserved (0);
  93. * this might not be true, but enforce it until deemed unnecessary */
  94. if (p->buf[10] & 0xFC)
  95. return 0;
  96. /* note: only check for WS IMA (type 99) right now since there is no
  97. * support for type 1 */
  98. if (p->buf[11] != 99)
  99. return 0;
  100. /* read ahead to the first audio chunk and validate the first header signature */
  101. if (AV_RL32(&p->buf[16]) != AUD_CHUNK_SIGNATURE)
  102. return 0;
  103. /* return 1/2 certainty since this file check is a little sketchy */
  104. return AVPROBE_SCORE_MAX / 2;
  105. }
  106. static int wsaud_read_header(AVFormatContext *s,
  107. AVFormatParameters *ap)
  108. {
  109. WsAudDemuxContext *wsaud = s->priv_data;
  110. AVIOContext *pb = s->pb;
  111. AVStream *st;
  112. unsigned char header[AUD_HEADER_SIZE];
  113. if (avio_read(pb, header, AUD_HEADER_SIZE) != AUD_HEADER_SIZE)
  114. return AVERROR(EIO);
  115. wsaud->audio_samplerate = AV_RL16(&header[0]);
  116. if (header[11] == 99)
  117. wsaud->audio_type = CODEC_ID_ADPCM_IMA_WS;
  118. else
  119. return AVERROR_INVALIDDATA;
  120. /* flag 0 indicates stereo */
  121. wsaud->audio_channels = (header[10] & 0x1) + 1;
  122. /* flag 1 indicates 16 bit audio */
  123. wsaud->audio_bits = (((header[10] & 0x2) >> 1) + 1) * 8;
  124. /* initialize the audio decoder stream */
  125. st = av_new_stream(s, 0);
  126. if (!st)
  127. return AVERROR(ENOMEM);
  128. av_set_pts_info(st, 33, 1, wsaud->audio_samplerate);
  129. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  130. st->codec->codec_id = wsaud->audio_type;
  131. st->codec->codec_tag = 0; /* no tag */
  132. st->codec->channels = wsaud->audio_channels;
  133. st->codec->sample_rate = wsaud->audio_samplerate;
  134. st->codec->bits_per_coded_sample = wsaud->audio_bits;
  135. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  136. st->codec->bits_per_coded_sample / 4;
  137. st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
  138. wsaud->audio_stream_index = st->index;
  139. wsaud->audio_frame_counter = 0;
  140. return 0;
  141. }
  142. static int wsaud_read_packet(AVFormatContext *s,
  143. AVPacket *pkt)
  144. {
  145. WsAudDemuxContext *wsaud = s->priv_data;
  146. AVIOContext *pb = s->pb;
  147. unsigned char preamble[AUD_CHUNK_PREAMBLE_SIZE];
  148. unsigned int chunk_size;
  149. int ret = 0;
  150. if (avio_read(pb, preamble, AUD_CHUNK_PREAMBLE_SIZE) !=
  151. AUD_CHUNK_PREAMBLE_SIZE)
  152. return AVERROR(EIO);
  153. /* validate the chunk */
  154. if (AV_RL32(&preamble[4]) != AUD_CHUNK_SIGNATURE)
  155. return AVERROR_INVALIDDATA;
  156. chunk_size = AV_RL16(&preamble[0]);
  157. ret= av_get_packet(pb, pkt, chunk_size);
  158. if (ret != chunk_size)
  159. return AVERROR(EIO);
  160. pkt->stream_index = wsaud->audio_stream_index;
  161. pkt->pts = wsaud->audio_frame_counter;
  162. pkt->pts /= wsaud->audio_samplerate;
  163. /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
  164. wsaud->audio_frame_counter += (chunk_size * 2) / wsaud->audio_channels;
  165. return ret;
  166. }
  167. static int wsvqa_probe(AVProbeData *p)
  168. {
  169. /* need 12 bytes to qualify */
  170. if (p->buf_size < 12)
  171. return 0;
  172. /* check for the VQA signatures */
  173. if ((AV_RB32(&p->buf[0]) != FORM_TAG) ||
  174. (AV_RB32(&p->buf[8]) != WVQA_TAG))
  175. return 0;
  176. return AVPROBE_SCORE_MAX;
  177. }
  178. static int wsvqa_read_header(AVFormatContext *s,
  179. AVFormatParameters *ap)
  180. {
  181. WsVqaDemuxContext *wsvqa = s->priv_data;
  182. AVIOContext *pb = s->pb;
  183. AVStream *st;
  184. unsigned char *header;
  185. unsigned char scratch[VQA_PREAMBLE_SIZE];
  186. unsigned int chunk_tag;
  187. unsigned int chunk_size;
  188. /* initialize the video decoder stream */
  189. st = av_new_stream(s, 0);
  190. if (!st)
  191. return AVERROR(ENOMEM);
  192. av_set_pts_info(st, 33, 1, VQA_FRAMERATE);
  193. wsvqa->video_stream_index = st->index;
  194. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  195. st->codec->codec_id = CODEC_ID_WS_VQA;
  196. st->codec->codec_tag = 0; /* no fourcc */
  197. /* skip to the start of the VQA header */
  198. avio_seek(pb, 20, SEEK_SET);
  199. /* the VQA header needs to go to the decoder */
  200. st->codec->extradata_size = VQA_HEADER_SIZE;
  201. st->codec->extradata = av_mallocz(VQA_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
  202. header = (unsigned char *)st->codec->extradata;
  203. if (avio_read(pb, st->codec->extradata, VQA_HEADER_SIZE) !=
  204. VQA_HEADER_SIZE) {
  205. av_free(st->codec->extradata);
  206. return AVERROR(EIO);
  207. }
  208. st->codec->width = AV_RL16(&header[6]);
  209. st->codec->height = AV_RL16(&header[8]);
  210. /* initialize the audio decoder stream for VQA v1 or nonzero samplerate */
  211. if (AV_RL16(&header[24]) || (AV_RL16(&header[0]) == 1 && AV_RL16(&header[2]) == 1)) {
  212. st = av_new_stream(s, 0);
  213. if (!st)
  214. return AVERROR(ENOMEM);
  215. av_set_pts_info(st, 33, 1, VQA_FRAMERATE);
  216. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  217. if (AV_RL16(&header[0]) == 1)
  218. st->codec->codec_id = CODEC_ID_WESTWOOD_SND1;
  219. else
  220. st->codec->codec_id = CODEC_ID_ADPCM_IMA_WS;
  221. st->codec->codec_tag = 0; /* no tag */
  222. st->codec->sample_rate = AV_RL16(&header[24]);
  223. if (!st->codec->sample_rate)
  224. st->codec->sample_rate = 22050;
  225. st->codec->channels = header[26];
  226. if (!st->codec->channels)
  227. st->codec->channels = 1;
  228. st->codec->bits_per_coded_sample = 16;
  229. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  230. st->codec->bits_per_coded_sample / 4;
  231. st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
  232. wsvqa->audio_stream_index = st->index;
  233. wsvqa->audio_samplerate = st->codec->sample_rate;
  234. wsvqa->audio_channels = st->codec->channels;
  235. wsvqa->audio_frame_counter = 0;
  236. }
  237. /* there are 0 or more chunks before the FINF chunk; iterate until
  238. * FINF has been skipped and the file will be ready to be demuxed */
  239. do {
  240. if (avio_read(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE)
  241. return AVERROR(EIO);
  242. chunk_tag = AV_RB32(&scratch[0]);
  243. chunk_size = AV_RB32(&scratch[4]);
  244. /* catch any unknown header tags, for curiousity */
  245. switch (chunk_tag) {
  246. case CINF_TAG:
  247. case CINH_TAG:
  248. case CIND_TAG:
  249. case PINF_TAG:
  250. case PINH_TAG:
  251. case PIND_TAG:
  252. case FINF_TAG:
  253. case CMDS_TAG:
  254. break;
  255. default:
  256. av_log (s, AV_LOG_ERROR, " note: unknown chunk seen (%c%c%c%c)\n",
  257. scratch[0], scratch[1],
  258. scratch[2], scratch[3]);
  259. break;
  260. }
  261. avio_skip(pb, chunk_size);
  262. } while (chunk_tag != FINF_TAG);
  263. return 0;
  264. }
  265. static int wsvqa_read_packet(AVFormatContext *s,
  266. AVPacket *pkt)
  267. {
  268. WsVqaDemuxContext *wsvqa = s->priv_data;
  269. AVIOContext *pb = s->pb;
  270. int ret = -1;
  271. unsigned char preamble[VQA_PREAMBLE_SIZE];
  272. unsigned int chunk_type;
  273. unsigned int chunk_size;
  274. int skip_byte;
  275. while (avio_read(pb, preamble, VQA_PREAMBLE_SIZE) == VQA_PREAMBLE_SIZE) {
  276. chunk_type = AV_RB32(&preamble[0]);
  277. chunk_size = AV_RB32(&preamble[4]);
  278. skip_byte = chunk_size & 0x01;
  279. if ((chunk_type == SND1_TAG) || (chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
  280. if (av_new_packet(pkt, chunk_size))
  281. return AVERROR(EIO);
  282. ret = avio_read(pb, pkt->data, chunk_size);
  283. if (ret != chunk_size) {
  284. av_free_packet(pkt);
  285. return AVERROR(EIO);
  286. }
  287. if (chunk_type == SND2_TAG) {
  288. pkt->stream_index = wsvqa->audio_stream_index;
  289. /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
  290. wsvqa->audio_frame_counter += (chunk_size * 2) / wsvqa->audio_channels;
  291. } else if(chunk_type == SND1_TAG) {
  292. pkt->stream_index = wsvqa->audio_stream_index;
  293. /* unpacked size is stored in header */
  294. wsvqa->audio_frame_counter += AV_RL16(pkt->data) / wsvqa->audio_channels;
  295. } else {
  296. pkt->stream_index = wsvqa->video_stream_index;
  297. }
  298. /* stay on 16-bit alignment */
  299. if (skip_byte)
  300. avio_skip(pb, 1);
  301. return ret;
  302. } else {
  303. switch(chunk_type){
  304. case CMDS_TAG:
  305. case SND0_TAG:
  306. break;
  307. default:
  308. av_log(s, AV_LOG_INFO, "Skipping unknown chunk 0x%08X\n", chunk_type);
  309. }
  310. avio_skip(pb, chunk_size + skip_byte);
  311. }
  312. }
  313. return ret;
  314. }
  315. #if CONFIG_WSAUD_DEMUXER
  316. AVInputFormat ff_wsaud_demuxer = {
  317. .name = "wsaud",
  318. .long_name = NULL_IF_CONFIG_SMALL("Westwood Studios audio format"),
  319. .priv_data_size = sizeof(WsAudDemuxContext),
  320. .read_probe = wsaud_probe,
  321. .read_header = wsaud_read_header,
  322. .read_packet = wsaud_read_packet,
  323. };
  324. #endif
  325. #if CONFIG_WSVQA_DEMUXER
  326. AVInputFormat ff_wsvqa_demuxer = {
  327. .name = "wsvqa",
  328. .long_name = NULL_IF_CONFIG_SMALL("Westwood Studios VQA format"),
  329. .priv_data_size = sizeof(WsVqaDemuxContext),
  330. .read_probe = wsvqa_probe,
  331. .read_header = wsvqa_read_header,
  332. .read_packet = wsvqa_read_packet,
  333. };
  334. #endif