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.

393 lines
13KB

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