You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

413 lines
13KB

  1. /*
  2. * Westwood Studios Multimedia Formats Demuxer (VQA, AUD)
  3. * Copyright (c) 2003 The ffmpeg Project
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file westwood.c
  21. * Westwood Studios VQA & AUD file demuxers
  22. * by Mike Melanson (melanson@pcisys.net)
  23. * for more information on the Westwood file formats, visit:
  24. * http://www.pcisys.net/~melanson/codecs/
  25. * http://www.geocities.com/SiliconValley/8682/aud3.txt
  26. *
  27. * Implementation note: There is no definite file signature for AUD files.
  28. * The demuxer uses a probabilistic strategy for content detection. This
  29. * entails performing sanity checks on certain header values in order to
  30. * qualify a file. Refer to wsaud_probe() for the precise parameters.
  31. */
  32. #include "avformat.h"
  33. #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
  34. #define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
  35. (((uint8_t*)(x))[2] << 16) | \
  36. (((uint8_t*)(x))[1] << 8) | \
  37. ((uint8_t*)(x))[0])
  38. #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
  39. (((uint8_t*)(x))[1] << 16) | \
  40. (((uint8_t*)(x))[2] << 8) | \
  41. ((uint8_t*)(x))[3])
  42. #define AUD_HEADER_SIZE 12
  43. #define AUD_CHUNK_PREAMBLE_SIZE 8
  44. #define AUD_CHUNK_SIGNATURE 0x0000DEAF
  45. #define FOURCC_TAG( ch0, ch1, ch2, ch3 ) \
  46. ( (long)(unsigned char)(ch3) | \
  47. ( (long)(unsigned char)(ch2) << 8 ) | \
  48. ( (long)(unsigned char)(ch1) << 16 ) | \
  49. ( (long)(unsigned char)(ch0) << 24 ) )
  50. #define FORM_TAG FOURCC_TAG('F', 'O', 'R', 'M')
  51. #define WVQA_TAG FOURCC_TAG('W', 'V', 'Q', 'A')
  52. #define VQHD_TAG FOURCC_TAG('V', 'Q', 'H', 'D')
  53. #define FINF_TAG FOURCC_TAG('F', 'I', 'N', 'F')
  54. #define SND0_TAG FOURCC_TAG('S', 'N', 'D', '0')
  55. #define SND2_TAG FOURCC_TAG('S', 'N', 'D', '2')
  56. #define VQFR_TAG FOURCC_TAG('V', 'Q', 'F', 'R')
  57. /* don't know what these tags are for, but acknowledge their existence */
  58. #define CINF_TAG FOURCC_TAG('C', 'I', 'N', 'F')
  59. #define CINH_TAG FOURCC_TAG('C', 'I', 'N', 'H')
  60. #define CIND_TAG FOURCC_TAG('C', 'I', 'N', 'D')
  61. #define PINF_TAG FOURCC_TAG('P', 'I', 'N', 'F')
  62. #define PINH_TAG FOURCC_TAG('P', 'I', 'N', 'H')
  63. #define PIND_TAG FOURCC_TAG('P', 'I', 'N', 'D')
  64. #define VQA_HEADER_SIZE 0x2A
  65. #define VQA_FRAMERATE 15
  66. #define VQA_VIDEO_PTS_INC (90000 / VQA_FRAMERATE)
  67. #define VQA_PREAMBLE_SIZE 8
  68. typedef struct WsAudDemuxContext {
  69. int audio_samplerate;
  70. int audio_channels;
  71. int audio_bits;
  72. int audio_type;
  73. int audio_stream_index;
  74. int64_t audio_frame_counter;
  75. } WsAudDemuxContext;
  76. typedef struct WsVqaDemuxContext {
  77. int audio_samplerate;
  78. int audio_channels;
  79. int audio_bits;
  80. int audio_stream_index;
  81. int video_stream_index;
  82. int64_t audio_frame_counter;
  83. int64_t video_pts;
  84. } WsVqaDemuxContext;
  85. static int wsaud_probe(AVProbeData *p)
  86. {
  87. int field;
  88. /* Probabilistic content detection strategy: There is no file signature
  89. * so perform sanity checks on various header parameters:
  90. * 8000 <= sample rate (16 bits) <= 48000 ==> 40001 acceptable numbers
  91. * compression type (8 bits) = 1 or 99 ==> 2 acceptable numbers
  92. * There is a total of 24 bits. The number space contains 2^24 =
  93. * 16777216 numbers. There are 40001 * 2 = 80002 acceptable combinations
  94. * of numbers. There is a 80002/16777216 = 0.48% chance of a false
  95. * positive.
  96. */
  97. if (p->buf_size < AUD_HEADER_SIZE)
  98. return 0;
  99. /* check sample rate */
  100. field = LE_16(&p->buf[0]);
  101. if ((field < 8000) || (field > 48000))
  102. return 0;
  103. /* note: only check for WS IMA (type 99) right now since there is no
  104. * support for type 1 */
  105. if (p->buf[11] != 99)
  106. return 0;
  107. /* return 1/2 certainty since this file check is a little sketchy */
  108. return AVPROBE_SCORE_MAX / 2;
  109. }
  110. static int wsaud_read_header(AVFormatContext *s,
  111. AVFormatParameters *ap)
  112. {
  113. WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
  114. ByteIOContext *pb = &s->pb;
  115. AVStream *st;
  116. unsigned char header[AUD_HEADER_SIZE];
  117. if (get_buffer(pb, header, AUD_HEADER_SIZE) != AUD_HEADER_SIZE)
  118. return -EIO;
  119. wsaud->audio_samplerate = LE_16(&header[0]);
  120. if (header[11] == 99)
  121. wsaud->audio_type = CODEC_ID_ADPCM_IMA_WS;
  122. else
  123. return AVERROR_INVALIDDATA;
  124. /* flag 0 indicates stereo */
  125. wsaud->audio_channels = (header[10] & 0x1) + 1;
  126. /* flag 1 indicates 16 bit audio */
  127. wsaud->audio_bits = (((header[10] & 0x2) >> 1) + 1) * 8;
  128. /* set the pts reference the same as the sample rate */
  129. s->pts_num = 1;
  130. s->pts_den = wsaud->audio_samplerate;
  131. /* initialize the audio decoder stream */
  132. st = av_new_stream(s, 0);
  133. if (!st)
  134. return AVERROR_NOMEM;
  135. st->codec.codec_type = CODEC_TYPE_AUDIO;
  136. st->codec.codec_id = wsaud->audio_type;
  137. st->codec.codec_tag = 0; /* no tag */
  138. st->codec.channels = wsaud->audio_channels;
  139. st->codec.sample_rate = wsaud->audio_samplerate;
  140. st->codec.bits_per_sample = wsaud->audio_bits;
  141. st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
  142. st->codec.bits_per_sample / 4;
  143. st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
  144. wsaud->audio_stream_index = st->index;
  145. wsaud->audio_frame_counter = 0;
  146. return 0;
  147. }
  148. static int wsaud_read_packet(AVFormatContext *s,
  149. AVPacket *pkt)
  150. {
  151. WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
  152. ByteIOContext *pb = &s->pb;
  153. unsigned char preamble[AUD_CHUNK_PREAMBLE_SIZE];
  154. unsigned int chunk_size;
  155. int ret = 0;
  156. if (get_buffer(pb, preamble, AUD_CHUNK_PREAMBLE_SIZE) !=
  157. AUD_CHUNK_PREAMBLE_SIZE)
  158. return -EIO;
  159. /* validate the chunk */
  160. if (LE_32(&preamble[4]) != AUD_CHUNK_SIGNATURE)
  161. return AVERROR_INVALIDDATA;
  162. chunk_size = LE_16(&preamble[0]);
  163. if (av_new_packet(pkt, chunk_size))
  164. return -EIO;
  165. pkt->stream_index = wsaud->audio_stream_index;
  166. pkt->pts = wsaud->audio_frame_counter;
  167. pkt->pts /= wsaud->audio_samplerate;
  168. if ((ret = get_buffer(pb, pkt->data, chunk_size)) != chunk_size) {
  169. av_free_packet(pkt);
  170. ret = -EIO;
  171. }
  172. /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
  173. wsaud->audio_frame_counter += (chunk_size * 2) / wsaud->audio_channels;
  174. return ret;
  175. }
  176. static int wsaud_read_close(AVFormatContext *s)
  177. {
  178. // WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
  179. return 0;
  180. }
  181. static int wsvqa_probe(AVProbeData *p)
  182. {
  183. /* need 12 bytes to qualify */
  184. if (p->buf_size < 12)
  185. return 0;
  186. /* check for the VQA signatures */
  187. if ((BE_32(&p->buf[0]) != FORM_TAG) ||
  188. (BE_32(&p->buf[8]) != WVQA_TAG))
  189. return 0;
  190. return AVPROBE_SCORE_MAX;
  191. }
  192. static int wsvqa_read_header(AVFormatContext *s,
  193. AVFormatParameters *ap)
  194. {
  195. WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
  196. ByteIOContext *pb = &s->pb;
  197. AVStream *st;
  198. unsigned char *header;
  199. unsigned char scratch[VQA_PREAMBLE_SIZE];
  200. unsigned int chunk_tag;
  201. unsigned int chunk_size;
  202. /* set the pts reference (1 pts = 1/90000) */
  203. s->pts_num = 1;
  204. s->pts_den = 90000;
  205. /* initialize the video decoder stream */
  206. st = av_new_stream(s, 0);
  207. if (!st)
  208. return AVERROR_NOMEM;
  209. wsvqa->video_stream_index = st->index;
  210. st->codec.codec_type = CODEC_TYPE_VIDEO;
  211. st->codec.codec_id = CODEC_ID_WS_VQA;
  212. st->codec.codec_tag = 0; /* no fourcc */
  213. /* skip to the start of the VQA header */
  214. url_fseek(pb, 20, SEEK_SET);
  215. /* the VQA header needs to go to the decoder */
  216. st->codec.extradata_size = VQA_HEADER_SIZE;
  217. st->codec.extradata = av_malloc(VQA_HEADER_SIZE);
  218. header = (unsigned char *)st->codec.extradata;
  219. if (get_buffer(pb, st->codec.extradata, VQA_HEADER_SIZE) !=
  220. VQA_HEADER_SIZE) {
  221. av_free(st->codec.extradata);
  222. return -EIO;
  223. }
  224. st->codec.width = LE_16(&header[6]);
  225. st->codec.height = LE_16(&header[8]);
  226. /* initialize the audio decoder stream is sample rate is non-zero */
  227. if (LE_16(&header[24])) {
  228. st = av_new_stream(s, 0);
  229. if (!st)
  230. return AVERROR_NOMEM;
  231. st->codec.codec_type = CODEC_TYPE_AUDIO;
  232. st->codec.codec_id = CODEC_ID_ADPCM_IMA_WS;
  233. st->codec.codec_tag = 0; /* no tag */
  234. st->codec.sample_rate = LE_16(&header[24]);
  235. st->codec.channels = header[26];
  236. st->codec.bits_per_sample = 16;
  237. st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
  238. st->codec.bits_per_sample / 4;
  239. st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
  240. wsvqa->audio_stream_index = st->index;
  241. wsvqa->audio_samplerate = st->codec.sample_rate;
  242. wsvqa->audio_channels = st->codec.channels;
  243. wsvqa->audio_frame_counter = 0;
  244. }
  245. /* there are 0 or more chunks before the FINF chunk; iterate until
  246. * FINF has been skipped and the file will be ready to be demuxed */
  247. do {
  248. if (get_buffer(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE) {
  249. av_free(st->codec.extradata);
  250. return -EIO;
  251. }
  252. chunk_tag = BE_32(&scratch[0]);
  253. chunk_size = BE_32(&scratch[4]);
  254. /* catch any unknown header tags, for curiousity */
  255. switch (chunk_tag) {
  256. case CINF_TAG:
  257. case CINH_TAG:
  258. case CIND_TAG:
  259. case PINF_TAG:
  260. case PINH_TAG:
  261. case PIND_TAG:
  262. case FINF_TAG:
  263. break;
  264. default:
  265. printf (" note: unknown chunk seen (%c%c%c%c)\n",
  266. scratch[0], scratch[1],
  267. scratch[2], scratch[3]);
  268. break;
  269. }
  270. url_fseek(pb, chunk_size, SEEK_CUR);
  271. } while (chunk_tag != FINF_TAG);
  272. wsvqa->video_pts = wsvqa->audio_frame_counter = 0;
  273. return 0;
  274. }
  275. static int wsvqa_read_packet(AVFormatContext *s,
  276. AVPacket *pkt)
  277. {
  278. WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
  279. ByteIOContext *pb = &s->pb;
  280. int ret = 0;
  281. unsigned char preamble[VQA_PREAMBLE_SIZE];
  282. unsigned int chunk_type;
  283. unsigned int chunk_size;
  284. int skip_byte;
  285. if (get_buffer(pb, preamble, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE)
  286. return -EIO;
  287. chunk_type = BE_32(&preamble[0]);
  288. chunk_size = BE_32(&preamble[4]);
  289. skip_byte = chunk_size & 0x01;
  290. if ((chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
  291. if (av_new_packet(pkt, chunk_size))
  292. return -EIO;
  293. ret = get_buffer(pb, pkt->data, chunk_size);
  294. if (ret != chunk_size) {
  295. av_free_packet(pkt);
  296. ret = -EIO;
  297. }
  298. if (chunk_type == SND2_TAG) {
  299. pkt->stream_index = wsvqa->audio_stream_index;
  300. pkt->pts = 90000;
  301. pkt->pts *= wsvqa->audio_frame_counter;
  302. pkt->pts /= wsvqa->audio_samplerate;
  303. /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
  304. wsvqa->audio_frame_counter += (chunk_size * 2) /
  305. wsvqa->audio_channels;
  306. } else {
  307. pkt->stream_index = wsvqa->video_stream_index;
  308. pkt->pts = wsvqa->video_pts;
  309. wsvqa->video_pts += VQA_VIDEO_PTS_INC;
  310. }
  311. } else
  312. return AVERROR_INVALIDDATA;
  313. /* stay on 16-bit alignment */
  314. if (skip_byte)
  315. url_fseek(pb, 1, SEEK_CUR);
  316. return ret;
  317. }
  318. static int wsvqa_read_close(AVFormatContext *s)
  319. {
  320. // WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
  321. return 0;
  322. }
  323. static AVInputFormat wsaud_iformat = {
  324. "wsaud",
  325. "Westwood Studios audio format",
  326. sizeof(WsAudDemuxContext),
  327. wsaud_probe,
  328. wsaud_read_header,
  329. wsaud_read_packet,
  330. wsaud_read_close,
  331. };
  332. static AVInputFormat wsvqa_iformat = {
  333. "wsvqa",
  334. "Westwood Studios VQA format",
  335. sizeof(WsVqaDemuxContext),
  336. wsvqa_probe,
  337. wsvqa_read_header,
  338. wsvqa_read_packet,
  339. wsvqa_read_close,
  340. };
  341. int westwood_init(void)
  342. {
  343. av_register_input_format(&wsaud_iformat);
  344. av_register_input_format(&wsvqa_iformat);
  345. return 0;
  346. }