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.

392 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 AVERROR_IO;
  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. /* initialize the audio decoder stream */
  115. st = av_new_stream(s, 0);
  116. if (!st)
  117. return AVERROR_NOMEM;
  118. av_set_pts_info(st, 33, 1, wsaud->audio_samplerate);
  119. st->codec.codec_type = CODEC_TYPE_AUDIO;
  120. st->codec.codec_id = wsaud->audio_type;
  121. st->codec.codec_tag = 0; /* no tag */
  122. st->codec.channels = wsaud->audio_channels;
  123. st->codec.sample_rate = wsaud->audio_samplerate;
  124. st->codec.bits_per_sample = wsaud->audio_bits;
  125. st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
  126. st->codec.bits_per_sample / 4;
  127. st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
  128. wsaud->audio_stream_index = st->index;
  129. wsaud->audio_frame_counter = 0;
  130. return 0;
  131. }
  132. static int wsaud_read_packet(AVFormatContext *s,
  133. AVPacket *pkt)
  134. {
  135. WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
  136. ByteIOContext *pb = &s->pb;
  137. unsigned char preamble[AUD_CHUNK_PREAMBLE_SIZE];
  138. unsigned int chunk_size;
  139. int ret = 0;
  140. if (get_buffer(pb, preamble, AUD_CHUNK_PREAMBLE_SIZE) !=
  141. AUD_CHUNK_PREAMBLE_SIZE)
  142. return AVERROR_IO;
  143. /* validate the chunk */
  144. if (LE_32(&preamble[4]) != AUD_CHUNK_SIGNATURE)
  145. return AVERROR_INVALIDDATA;
  146. chunk_size = LE_16(&preamble[0]);
  147. if (av_new_packet(pkt, chunk_size))
  148. return AVERROR_IO;
  149. pkt->stream_index = wsaud->audio_stream_index;
  150. pkt->pts = wsaud->audio_frame_counter;
  151. pkt->pts /= wsaud->audio_samplerate;
  152. if ((ret = get_buffer(pb, pkt->data, chunk_size)) != chunk_size) {
  153. av_free_packet(pkt);
  154. ret = AVERROR_IO;
  155. }
  156. /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
  157. wsaud->audio_frame_counter += (chunk_size * 2) / wsaud->audio_channels;
  158. return ret;
  159. }
  160. static int wsaud_read_close(AVFormatContext *s)
  161. {
  162. // WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
  163. return 0;
  164. }
  165. static int wsvqa_probe(AVProbeData *p)
  166. {
  167. /* need 12 bytes to qualify */
  168. if (p->buf_size < 12)
  169. return 0;
  170. /* check for the VQA signatures */
  171. if ((BE_32(&p->buf[0]) != FORM_TAG) ||
  172. (BE_32(&p->buf[8]) != WVQA_TAG))
  173. return 0;
  174. return AVPROBE_SCORE_MAX;
  175. }
  176. static int wsvqa_read_header(AVFormatContext *s,
  177. AVFormatParameters *ap)
  178. {
  179. WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
  180. ByteIOContext *pb = &s->pb;
  181. AVStream *st;
  182. unsigned char *header;
  183. unsigned char scratch[VQA_PREAMBLE_SIZE];
  184. unsigned int chunk_tag;
  185. unsigned int chunk_size;
  186. /* initialize the video decoder stream */
  187. st = av_new_stream(s, 0);
  188. if (!st)
  189. return AVERROR_NOMEM;
  190. av_set_pts_info(st, 33, 1, 90000);
  191. wsvqa->video_stream_index = st->index;
  192. st->codec.codec_type = CODEC_TYPE_VIDEO;
  193. st->codec.codec_id = CODEC_ID_WS_VQA;
  194. st->codec.codec_tag = 0; /* no fourcc */
  195. /* skip to the start of the VQA header */
  196. url_fseek(pb, 20, SEEK_SET);
  197. /* the VQA header needs to go to the decoder */
  198. st->codec.extradata_size = VQA_HEADER_SIZE;
  199. st->codec.extradata = av_malloc(VQA_HEADER_SIZE);
  200. header = (unsigned char *)st->codec.extradata;
  201. if (get_buffer(pb, st->codec.extradata, VQA_HEADER_SIZE) !=
  202. VQA_HEADER_SIZE) {
  203. av_free(st->codec.extradata);
  204. return AVERROR_IO;
  205. }
  206. st->codec.width = LE_16(&header[6]);
  207. st->codec.height = LE_16(&header[8]);
  208. /* initialize the audio decoder stream is sample rate is non-zero */
  209. if (LE_16(&header[24])) {
  210. st = av_new_stream(s, 0);
  211. if (!st)
  212. return AVERROR_NOMEM;
  213. av_set_pts_info(st, 33, 1, 90000);
  214. st->codec.codec_type = CODEC_TYPE_AUDIO;
  215. st->codec.codec_id = CODEC_ID_ADPCM_IMA_WS;
  216. st->codec.codec_tag = 0; /* no tag */
  217. st->codec.sample_rate = LE_16(&header[24]);
  218. st->codec.channels = header[26];
  219. st->codec.bits_per_sample = 16;
  220. st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
  221. st->codec.bits_per_sample / 4;
  222. st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
  223. wsvqa->audio_stream_index = st->index;
  224. wsvqa->audio_samplerate = st->codec.sample_rate;
  225. wsvqa->audio_channels = st->codec.channels;
  226. wsvqa->audio_frame_counter = 0;
  227. }
  228. /* there are 0 or more chunks before the FINF chunk; iterate until
  229. * FINF has been skipped and the file will be ready to be demuxed */
  230. do {
  231. if (get_buffer(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE) {
  232. av_free(st->codec.extradata);
  233. return AVERROR_IO;
  234. }
  235. chunk_tag = BE_32(&scratch[0]);
  236. chunk_size = BE_32(&scratch[4]);
  237. /* catch any unknown header tags, for curiousity */
  238. switch (chunk_tag) {
  239. case CINF_TAG:
  240. case CINH_TAG:
  241. case CIND_TAG:
  242. case PINF_TAG:
  243. case PINH_TAG:
  244. case PIND_TAG:
  245. case FINF_TAG:
  246. break;
  247. default:
  248. av_log (s, AV_LOG_ERROR, " note: unknown chunk seen (%c%c%c%c)\n",
  249. scratch[0], scratch[1],
  250. scratch[2], scratch[3]);
  251. break;
  252. }
  253. url_fseek(pb, chunk_size, SEEK_CUR);
  254. } while (chunk_tag != FINF_TAG);
  255. wsvqa->video_pts = wsvqa->audio_frame_counter = 0;
  256. return 0;
  257. }
  258. static int wsvqa_read_packet(AVFormatContext *s,
  259. AVPacket *pkt)
  260. {
  261. WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
  262. ByteIOContext *pb = &s->pb;
  263. int ret = 0;
  264. unsigned char preamble[VQA_PREAMBLE_SIZE];
  265. unsigned int chunk_type;
  266. unsigned int chunk_size;
  267. int skip_byte;
  268. if (get_buffer(pb, preamble, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE)
  269. return AVERROR_IO;
  270. chunk_type = BE_32(&preamble[0]);
  271. chunk_size = BE_32(&preamble[4]);
  272. skip_byte = chunk_size & 0x01;
  273. if ((chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
  274. if (av_new_packet(pkt, chunk_size))
  275. return AVERROR_IO;
  276. ret = get_buffer(pb, pkt->data, chunk_size);
  277. if (ret != chunk_size) {
  278. av_free_packet(pkt);
  279. ret = AVERROR_IO;
  280. }
  281. if (chunk_type == SND2_TAG) {
  282. pkt->stream_index = wsvqa->audio_stream_index;
  283. pkt->pts = 90000;
  284. pkt->pts *= wsvqa->audio_frame_counter;
  285. pkt->pts /= wsvqa->audio_samplerate;
  286. /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
  287. wsvqa->audio_frame_counter += (chunk_size * 2) /
  288. wsvqa->audio_channels;
  289. } else {
  290. pkt->stream_index = wsvqa->video_stream_index;
  291. pkt->pts = wsvqa->video_pts;
  292. wsvqa->video_pts += VQA_VIDEO_PTS_INC;
  293. }
  294. } else
  295. return AVERROR_INVALIDDATA;
  296. /* stay on 16-bit alignment */
  297. if (skip_byte)
  298. url_fseek(pb, 1, SEEK_CUR);
  299. return ret;
  300. }
  301. static int wsvqa_read_close(AVFormatContext *s)
  302. {
  303. // WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
  304. return 0;
  305. }
  306. static AVInputFormat wsaud_iformat = {
  307. "wsaud",
  308. "Westwood Studios audio format",
  309. sizeof(WsAudDemuxContext),
  310. wsaud_probe,
  311. wsaud_read_header,
  312. wsaud_read_packet,
  313. wsaud_read_close,
  314. };
  315. static AVInputFormat wsvqa_iformat = {
  316. "wsvqa",
  317. "Westwood Studios VQA format",
  318. sizeof(WsVqaDemuxContext),
  319. wsvqa_probe,
  320. wsvqa_read_header,
  321. wsvqa_read_packet,
  322. wsvqa_read_close,
  323. };
  324. int westwood_init(void)
  325. {
  326. av_register_input_format(&wsaud_iformat);
  327. av_register_input_format(&wsvqa_iformat);
  328. return 0;
  329. }