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.

397 lines
12KB

  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 AUD_HEADER_SIZE 12
  34. #define AUD_CHUNK_PREAMBLE_SIZE 8
  35. #define AUD_CHUNK_SIGNATURE 0x0000DEAF
  36. #define FORM_TAG MKBETAG('F', 'O', 'R', 'M')
  37. #define WVQA_TAG MKBETAG('W', 'V', 'Q', 'A')
  38. #define VQHD_TAG MKBETAG('V', 'Q', 'H', 'D')
  39. #define FINF_TAG MKBETAG('F', 'I', 'N', 'F')
  40. #define SND0_TAG MKBETAG('S', 'N', 'D', '0')
  41. #define SND2_TAG MKBETAG('S', 'N', 'D', '2')
  42. #define VQFR_TAG MKBETAG('V', 'Q', 'F', 'R')
  43. /* don't know what these tags are for, but acknowledge their existence */
  44. #define CINF_TAG MKBETAG('C', 'I', 'N', 'F')
  45. #define CINH_TAG MKBETAG('C', 'I', 'N', 'H')
  46. #define CIND_TAG MKBETAG('C', 'I', 'N', 'D')
  47. #define PINF_TAG MKBETAG('P', 'I', 'N', 'F')
  48. #define PINH_TAG MKBETAG('P', 'I', 'N', 'H')
  49. #define PIND_TAG MKBETAG('P', 'I', 'N', 'D')
  50. #define VQA_HEADER_SIZE 0x2A
  51. #define VQA_FRAMERATE 15
  52. #define VQA_VIDEO_PTS_INC (90000 / VQA_FRAMERATE)
  53. #define VQA_PREAMBLE_SIZE 8
  54. typedef struct WsAudDemuxContext {
  55. int audio_samplerate;
  56. int audio_channels;
  57. int audio_bits;
  58. int audio_type;
  59. int audio_stream_index;
  60. int64_t audio_frame_counter;
  61. } WsAudDemuxContext;
  62. typedef struct WsVqaDemuxContext {
  63. int audio_samplerate;
  64. int audio_channels;
  65. int audio_bits;
  66. int audio_stream_index;
  67. int video_stream_index;
  68. int64_t audio_frame_counter;
  69. int64_t video_pts;
  70. } WsVqaDemuxContext;
  71. static int wsaud_probe(AVProbeData *p)
  72. {
  73. int field;
  74. /* Probabilistic content detection strategy: There is no file signature
  75. * so perform sanity checks on various header parameters:
  76. * 8000 <= sample rate (16 bits) <= 48000 ==> 40001 acceptable numbers
  77. * compression type (8 bits) = 1 or 99 ==> 2 acceptable numbers
  78. * There is a total of 24 bits. The number space contains 2^24 =
  79. * 16777216 numbers. There are 40001 * 2 = 80002 acceptable combinations
  80. * of numbers. There is a 80002/16777216 = 0.48% chance of a false
  81. * positive.
  82. */
  83. if (p->buf_size < AUD_HEADER_SIZE)
  84. return 0;
  85. /* check sample rate */
  86. field = LE_16(&p->buf[0]);
  87. if ((field < 8000) || (field > 48000))
  88. return 0;
  89. /* note: only check for WS IMA (type 99) right now since there is no
  90. * support for type 1 */
  91. if (p->buf[11] != 99)
  92. return 0;
  93. /* return 1/2 certainty since this file check is a little sketchy */
  94. return AVPROBE_SCORE_MAX / 2;
  95. }
  96. static int wsaud_read_header(AVFormatContext *s,
  97. AVFormatParameters *ap)
  98. {
  99. WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
  100. ByteIOContext *pb = &s->pb;
  101. AVStream *st;
  102. unsigned char header[AUD_HEADER_SIZE];
  103. if (get_buffer(pb, header, AUD_HEADER_SIZE) != AUD_HEADER_SIZE)
  104. return -EIO;
  105. wsaud->audio_samplerate = LE_16(&header[0]);
  106. if (header[11] == 99)
  107. wsaud->audio_type = CODEC_ID_ADPCM_IMA_WS;
  108. else
  109. return AVERROR_INVALIDDATA;
  110. /* flag 0 indicates stereo */
  111. wsaud->audio_channels = (header[10] & 0x1) + 1;
  112. /* flag 1 indicates 16 bit audio */
  113. wsaud->audio_bits = (((header[10] & 0x2) >> 1) + 1) * 8;
  114. /* set the pts reference the same as the sample rate */
  115. s->pts_num = 1;
  116. s->pts_den = wsaud->audio_samplerate;
  117. /* initialize the audio decoder stream */
  118. st = av_new_stream(s, 0);
  119. if (!st)
  120. return AVERROR_NOMEM;
  121. st->codec.codec_type = CODEC_TYPE_AUDIO;
  122. st->codec.codec_id = wsaud->audio_type;
  123. st->codec.codec_tag = 0; /* no tag */
  124. st->codec.channels = wsaud->audio_channels;
  125. st->codec.sample_rate = wsaud->audio_samplerate;
  126. st->codec.bits_per_sample = wsaud->audio_bits;
  127. st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
  128. st->codec.bits_per_sample / 4;
  129. st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
  130. wsaud->audio_stream_index = st->index;
  131. wsaud->audio_frame_counter = 0;
  132. return 0;
  133. }
  134. static int wsaud_read_packet(AVFormatContext *s,
  135. AVPacket *pkt)
  136. {
  137. WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
  138. ByteIOContext *pb = &s->pb;
  139. unsigned char preamble[AUD_CHUNK_PREAMBLE_SIZE];
  140. unsigned int chunk_size;
  141. int ret = 0;
  142. if (get_buffer(pb, preamble, AUD_CHUNK_PREAMBLE_SIZE) !=
  143. AUD_CHUNK_PREAMBLE_SIZE)
  144. return -EIO;
  145. /* validate the chunk */
  146. if (LE_32(&preamble[4]) != AUD_CHUNK_SIGNATURE)
  147. return AVERROR_INVALIDDATA;
  148. chunk_size = LE_16(&preamble[0]);
  149. if (av_new_packet(pkt, chunk_size))
  150. return -EIO;
  151. pkt->stream_index = wsaud->audio_stream_index;
  152. pkt->pts = wsaud->audio_frame_counter;
  153. pkt->pts /= wsaud->audio_samplerate;
  154. if ((ret = get_buffer(pb, pkt->data, chunk_size)) != chunk_size) {
  155. av_free_packet(pkt);
  156. ret = -EIO;
  157. }
  158. /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
  159. wsaud->audio_frame_counter += (chunk_size * 2) / wsaud->audio_channels;
  160. return ret;
  161. }
  162. static int wsaud_read_close(AVFormatContext *s)
  163. {
  164. // WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
  165. return 0;
  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 ((BE_32(&p->buf[0]) != FORM_TAG) ||
  174. (BE_32(&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 = (WsVqaDemuxContext *)s->priv_data;
  182. ByteIOContext *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. /* set the pts reference (1 pts = 1/90000) */
  189. s->pts_num = 1;
  190. s->pts_den = 90000;
  191. /* initialize the video decoder stream */
  192. st = av_new_stream(s, 0);
  193. if (!st)
  194. return AVERROR_NOMEM;
  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_malloc(VQA_HEADER_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 -EIO;
  209. }
  210. st->codec.width = LE_16(&header[6]);
  211. st->codec.height = LE_16(&header[8]);
  212. /* initialize the audio decoder stream is sample rate is non-zero */
  213. if (LE_16(&header[24])) {
  214. st = av_new_stream(s, 0);
  215. if (!st)
  216. return AVERROR_NOMEM;
  217. st->codec.codec_type = CODEC_TYPE_AUDIO;
  218. st->codec.codec_id = CODEC_ID_ADPCM_IMA_WS;
  219. st->codec.codec_tag = 0; /* no tag */
  220. st->codec.sample_rate = LE_16(&header[24]);
  221. st->codec.channels = header[26];
  222. st->codec.bits_per_sample = 16;
  223. st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
  224. st->codec.bits_per_sample / 4;
  225. st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
  226. wsvqa->audio_stream_index = st->index;
  227. wsvqa->audio_samplerate = st->codec.sample_rate;
  228. wsvqa->audio_channels = st->codec.channels;
  229. wsvqa->audio_frame_counter = 0;
  230. }
  231. /* there are 0 or more chunks before the FINF chunk; iterate until
  232. * FINF has been skipped and the file will be ready to be demuxed */
  233. do {
  234. if (get_buffer(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE) {
  235. av_free(st->codec.extradata);
  236. return -EIO;
  237. }
  238. chunk_tag = BE_32(&scratch[0]);
  239. chunk_size = BE_32(&scratch[4]);
  240. /* catch any unknown header tags, for curiousity */
  241. switch (chunk_tag) {
  242. case CINF_TAG:
  243. case CINH_TAG:
  244. case CIND_TAG:
  245. case PINF_TAG:
  246. case PINH_TAG:
  247. case PIND_TAG:
  248. case FINF_TAG:
  249. break;
  250. default:
  251. av_log (s, AV_LOG_ERROR, " note: unknown chunk seen (%c%c%c%c)\n",
  252. scratch[0], scratch[1],
  253. scratch[2], scratch[3]);
  254. break;
  255. }
  256. url_fseek(pb, chunk_size, SEEK_CUR);
  257. } while (chunk_tag != FINF_TAG);
  258. wsvqa->video_pts = wsvqa->audio_frame_counter = 0;
  259. return 0;
  260. }
  261. static int wsvqa_read_packet(AVFormatContext *s,
  262. AVPacket *pkt)
  263. {
  264. WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
  265. ByteIOContext *pb = &s->pb;
  266. int ret = 0;
  267. unsigned char preamble[VQA_PREAMBLE_SIZE];
  268. unsigned int chunk_type;
  269. unsigned int chunk_size;
  270. int skip_byte;
  271. if (get_buffer(pb, preamble, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE)
  272. return -EIO;
  273. chunk_type = BE_32(&preamble[0]);
  274. chunk_size = BE_32(&preamble[4]);
  275. skip_byte = chunk_size & 0x01;
  276. if ((chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
  277. if (av_new_packet(pkt, chunk_size))
  278. return -EIO;
  279. ret = get_buffer(pb, pkt->data, chunk_size);
  280. if (ret != chunk_size) {
  281. av_free_packet(pkt);
  282. ret = -EIO;
  283. }
  284. if (chunk_type == SND2_TAG) {
  285. pkt->stream_index = wsvqa->audio_stream_index;
  286. pkt->pts = 90000;
  287. pkt->pts *= wsvqa->audio_frame_counter;
  288. pkt->pts /= wsvqa->audio_samplerate;
  289. /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
  290. wsvqa->audio_frame_counter += (chunk_size * 2) /
  291. wsvqa->audio_channels;
  292. } else {
  293. pkt->stream_index = wsvqa->video_stream_index;
  294. pkt->pts = wsvqa->video_pts;
  295. wsvqa->video_pts += VQA_VIDEO_PTS_INC;
  296. }
  297. } else
  298. return AVERROR_INVALIDDATA;
  299. /* stay on 16-bit alignment */
  300. if (skip_byte)
  301. url_fseek(pb, 1, SEEK_CUR);
  302. return ret;
  303. }
  304. static int wsvqa_read_close(AVFormatContext *s)
  305. {
  306. // WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
  307. return 0;
  308. }
  309. static AVInputFormat wsaud_iformat = {
  310. "wsaud",
  311. "Westwood Studios audio format",
  312. sizeof(WsAudDemuxContext),
  313. wsaud_probe,
  314. wsaud_read_header,
  315. wsaud_read_packet,
  316. wsaud_read_close,
  317. };
  318. static AVInputFormat wsvqa_iformat = {
  319. "wsvqa",
  320. "Westwood Studios VQA format",
  321. sizeof(WsVqaDemuxContext),
  322. wsvqa_probe,
  323. wsvqa_read_header,
  324. wsvqa_read_packet,
  325. wsvqa_read_close,
  326. };
  327. int westwood_init(void)
  328. {
  329. av_register_input_format(&wsaud_iformat);
  330. av_register_input_format(&wsvqa_iformat);
  331. return 0;
  332. }