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.

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