westwood.c 13 KB

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