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.

376 lines
11KB

  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. #define VQA_HEADER_SIZE 0x2A
  58. #define VQA_FRAMERATE 15
  59. #define VQA_VIDEO_PTS_INC (90000 / VQA_FRAMERATE)
  60. #define VQA_PREAMBLE_SIZE 8
  61. typedef struct WsAudDemuxContext {
  62. int audio_samplerate;
  63. int audio_channels;
  64. int audio_bits;
  65. int audio_type;
  66. int audio_stream_index;
  67. int64_t audio_frame_counter;
  68. } WsAudDemuxContext;
  69. typedef struct WsVqaDemuxContext {
  70. int audio_samplerate;
  71. int audio_channels;
  72. int audio_bits;
  73. int audio_stream_index;
  74. int video_stream_index;
  75. int64_t audio_frame_counter;
  76. int64_t video_pts;
  77. } WsVqaDemuxContext;
  78. static int wsaud_probe(AVProbeData *p)
  79. {
  80. int field;
  81. /* Probabilistic content detection strategy: There is no file signature
  82. * so perform sanity checks on various header parameters:
  83. * 8000 <= sample rate (16 bits) <= 48000 ==> 40001 acceptable numbers
  84. * compression type (8 bits) = 1 or 99 ==> 2 acceptable numbers
  85. * There is a total of 24 bits. The number space contains 2^24 =
  86. * 16777216 numbers. There are 40001 * 2 = 80002 acceptable combinations
  87. * of numbers. There is a 80002/16777216 = 0.48% chance of a false
  88. * positive.
  89. */
  90. if (p->buf_size < AUD_HEADER_SIZE)
  91. return 0;
  92. /* check sample rate */
  93. field = LE_16(&p->buf[0]);
  94. if ((field < 8000) || (field > 48000))
  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. /* return 1/2 certainty since this file check is a little sketchy */
  101. return AVPROBE_SCORE_MAX / 2;
  102. }
  103. static int wsaud_read_header(AVFormatContext *s,
  104. AVFormatParameters *ap)
  105. {
  106. WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
  107. ByteIOContext *pb = &s->pb;
  108. AVStream *st;
  109. unsigned char header[AUD_HEADER_SIZE];
  110. if (get_buffer(pb, header, AUD_HEADER_SIZE) != AUD_HEADER_SIZE)
  111. return -EIO;
  112. wsaud->audio_samplerate = LE_16(&header[0]);
  113. if (header[11] == 99)
  114. wsaud->audio_type = CODEC_ID_ADPCM_IMA_WS;
  115. else
  116. return AVERROR_INVALIDDATA;
  117. /* flag 0 indicates stereo */
  118. wsaud->audio_channels = (header[10] & 0x1) + 1;
  119. /* flag 1 indicates 16 bit audio */
  120. wsaud->audio_bits = (((header[10] & 0x2) >> 1) + 1) * 8;
  121. /* set the pts reference the same as the sample rate */
  122. s->pts_num = 1;
  123. s->pts_den = wsaud->audio_samplerate;
  124. /* initialize the audio decoder stream */
  125. st = av_new_stream(s, 0);
  126. if (!st)
  127. return AVERROR_NOMEM;
  128. st->codec.codec_type = CODEC_TYPE_AUDIO;
  129. st->codec.codec_id = wsaud->audio_type;
  130. st->codec.codec_tag = 0; /* no tag */
  131. st->codec.channels = wsaud->audio_channels;
  132. st->codec.sample_rate = wsaud->audio_samplerate;
  133. st->codec.bits_per_sample = wsaud->audio_bits;
  134. st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
  135. st->codec.bits_per_sample / 4;
  136. st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
  137. wsaud->audio_stream_index = st->index;
  138. wsaud->audio_frame_counter = 0;
  139. return 0;
  140. }
  141. static int wsaud_read_packet(AVFormatContext *s,
  142. AVPacket *pkt)
  143. {
  144. WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
  145. ByteIOContext *pb = &s->pb;
  146. unsigned char preamble[AUD_CHUNK_PREAMBLE_SIZE];
  147. unsigned int chunk_size;
  148. int ret = 0;
  149. if (get_buffer(pb, preamble, AUD_CHUNK_PREAMBLE_SIZE) !=
  150. AUD_CHUNK_PREAMBLE_SIZE)
  151. return -EIO;
  152. /* validate the chunk */
  153. if (LE_32(&preamble[4]) != AUD_CHUNK_SIGNATURE)
  154. return AVERROR_INVALIDDATA;
  155. chunk_size = LE_16(&preamble[0]);
  156. if (av_new_packet(pkt, chunk_size))
  157. return -EIO;
  158. pkt->stream_index = wsaud->audio_stream_index;
  159. pkt->pts = wsaud->audio_frame_counter;
  160. pkt->pts /= wsaud->audio_samplerate;
  161. if ((ret = get_buffer(pb, pkt->data, chunk_size)) != chunk_size) {
  162. av_free_packet(pkt);
  163. ret = -EIO;
  164. }
  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 wsaud_read_close(AVFormatContext *s)
  170. {
  171. // WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
  172. return 0;
  173. }
  174. static int wsvqa_probe(AVProbeData *p)
  175. {
  176. /* need 12 bytes to qualify */
  177. if (p->buf_size < 12)
  178. return 0;
  179. /* check for the VQA signatures */
  180. if ((BE_32(&p->buf[0]) != FORM_TAG) ||
  181. (BE_32(&p->buf[8]) != WVQA_TAG))
  182. return 0;
  183. return AVPROBE_SCORE_MAX;
  184. }
  185. static int wsvqa_read_header(AVFormatContext *s,
  186. AVFormatParameters *ap)
  187. {
  188. WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
  189. ByteIOContext *pb = &s->pb;
  190. AVStream *st;
  191. unsigned char *header;
  192. unsigned char scratch[VQA_PREAMBLE_SIZE];
  193. /* set the pts reference (1 pts = 1/90000) */
  194. s->pts_num = 1;
  195. s->pts_den = 90000;
  196. /* initialize the video decoder stream */
  197. st = av_new_stream(s, 0);
  198. if (!st)
  199. return AVERROR_NOMEM;
  200. wsvqa->video_stream_index = st->index;
  201. st->codec.codec_type = CODEC_TYPE_VIDEO;
  202. st->codec.codec_id = CODEC_ID_WS_VQA;
  203. st->codec.codec_tag = 0; /* no fourcc */
  204. /* skip to the start of the VQA header */
  205. url_fseek(pb, 20, SEEK_SET);
  206. /* the VQA header needs to go to the decoder */
  207. st->codec.extradata_size = VQA_HEADER_SIZE;
  208. st->codec.extradata = av_malloc(VQA_HEADER_SIZE);
  209. header = (unsigned char *)st->codec.extradata;
  210. if (get_buffer(pb, st->codec.extradata, VQA_HEADER_SIZE) !=
  211. VQA_HEADER_SIZE) {
  212. av_free(st->codec.extradata);
  213. return -EIO;
  214. }
  215. st->codec.width = LE_16(&header[6]);
  216. st->codec.height = LE_16(&header[8]);
  217. /* initialize the audio decoder stream */
  218. st = av_new_stream(s, 0);
  219. if (!st)
  220. return AVERROR_NOMEM;
  221. st->codec.codec_type = CODEC_TYPE_AUDIO;
  222. st->codec.codec_id = CODEC_ID_ADPCM_IMA_WS;
  223. st->codec.codec_tag = 0; /* no tag */
  224. st->codec.sample_rate = LE_16(&header[24]);
  225. st->codec.channels = header[26];
  226. st->codec.bits_per_sample = 16;
  227. st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
  228. st->codec.bits_per_sample / 4;
  229. st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
  230. wsvqa->audio_stream_index = st->index;
  231. wsvqa->audio_samplerate = st->codec.sample_rate;
  232. wsvqa->audio_channels = st->codec.channels;
  233. wsvqa->audio_frame_counter = 0;
  234. /* skip the useless FINF chunk index */
  235. if (get_buffer(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE) {
  236. av_free(st->codec.extradata);
  237. return -EIO;
  238. }
  239. url_fseek(pb, BE_32(&scratch[4]), SEEK_CUR);
  240. wsvqa->video_pts = wsvqa->audio_frame_counter = 0;
  241. return 0;
  242. }
  243. static int wsvqa_read_packet(AVFormatContext *s,
  244. AVPacket *pkt)
  245. {
  246. WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
  247. ByteIOContext *pb = &s->pb;
  248. int ret = 0;
  249. unsigned char preamble[VQA_PREAMBLE_SIZE];
  250. unsigned int chunk_type;
  251. unsigned int chunk_size;
  252. int skip_byte;
  253. if (get_buffer(pb, preamble, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE)
  254. return -EIO;
  255. chunk_type = BE_32(&preamble[0]);
  256. chunk_size = BE_32(&preamble[4]);
  257. skip_byte = chunk_size & 0x01;
  258. if ((chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
  259. if (av_new_packet(pkt, chunk_size))
  260. return -EIO;
  261. ret = get_buffer(pb, pkt->data, chunk_size);
  262. if (ret != chunk_size) {
  263. av_free_packet(pkt);
  264. ret = -EIO;
  265. }
  266. if (chunk_type == SND2_TAG) {
  267. pkt->stream_index = wsvqa->audio_stream_index;
  268. pkt->pts = 90000;
  269. pkt->pts *= wsvqa->audio_frame_counter;
  270. pkt->pts /= wsvqa->audio_samplerate;
  271. /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
  272. wsvqa->audio_frame_counter += (chunk_size * 2) /
  273. wsvqa->audio_channels;
  274. } else {
  275. pkt->stream_index = wsvqa->video_stream_index;
  276. pkt->pts = wsvqa->video_pts;
  277. wsvqa->video_pts += VQA_VIDEO_PTS_INC;
  278. }
  279. } else
  280. return AVERROR_INVALIDDATA;
  281. /* stay on 16-bit alignment */
  282. if (skip_byte)
  283. url_fseek(pb, 1, SEEK_CUR);
  284. return ret;
  285. }
  286. static int wsvqa_read_close(AVFormatContext *s)
  287. {
  288. // WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
  289. return 0;
  290. }
  291. static AVInputFormat wsaud_iformat = {
  292. "wsaud",
  293. "Westwood Studios audio format",
  294. sizeof(WsAudDemuxContext),
  295. wsaud_probe,
  296. wsaud_read_header,
  297. wsaud_read_packet,
  298. wsaud_read_close,
  299. };
  300. static AVInputFormat wsvqa_iformat = {
  301. "wsvqa",
  302. "Westwood Studios VQA format",
  303. sizeof(WsVqaDemuxContext),
  304. wsvqa_probe,
  305. wsvqa_read_header,
  306. wsvqa_read_packet,
  307. wsvqa_read_close,
  308. };
  309. int westwood_init(void)
  310. {
  311. av_register_input_format(&wsaud_iformat);
  312. av_register_input_format(&wsvqa_iformat);
  313. return 0;
  314. }