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.

401 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. * compression type (8 bits) = 1 or 99 ==> 2 acceptable numbers
  82. * There is a total of 24 bits. The number space contains 2^24 =
  83. * 16777216 numbers. There are 40001 * 2 = 80002 acceptable combinations
  84. * of numbers. There is a 80002/16777216 = 0.48% chance of a false
  85. * positive.
  86. */
  87. if (p->buf_size < AUD_HEADER_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. /* note: only check for WS IMA (type 99) right now since there is no
  94. * support for type 1 */
  95. if (p->buf[11] != 99)
  96. return 0;
  97. /* return 1/2 certainty since this file check is a little sketchy */
  98. return AVPROBE_SCORE_MAX / 2;
  99. }
  100. static int wsaud_read_header(AVFormatContext *s,
  101. AVFormatParameters *ap)
  102. {
  103. WsAudDemuxContext *wsaud = s->priv_data;
  104. ByteIOContext *pb = &s->pb;
  105. AVStream *st;
  106. unsigned char header[AUD_HEADER_SIZE];
  107. if (get_buffer(pb, header, AUD_HEADER_SIZE) != AUD_HEADER_SIZE)
  108. return AVERROR(EIO);
  109. wsaud->audio_samplerate = AV_RL16(&header[0]);
  110. if (header[11] == 99)
  111. wsaud->audio_type = CODEC_ID_ADPCM_IMA_WS;
  112. else
  113. return AVERROR_INVALIDDATA;
  114. /* flag 0 indicates stereo */
  115. wsaud->audio_channels = (header[10] & 0x1) + 1;
  116. /* flag 1 indicates 16 bit audio */
  117. wsaud->audio_bits = (((header[10] & 0x2) >> 1) + 1) * 8;
  118. /* initialize the audio decoder stream */
  119. st = av_new_stream(s, 0);
  120. if (!st)
  121. return AVERROR(ENOMEM);
  122. av_set_pts_info(st, 33, 1, wsaud->audio_samplerate);
  123. st->codec->codec_type = CODEC_TYPE_AUDIO;
  124. st->codec->codec_id = wsaud->audio_type;
  125. st->codec->codec_tag = 0; /* no tag */
  126. st->codec->channels = wsaud->audio_channels;
  127. st->codec->sample_rate = wsaud->audio_samplerate;
  128. st->codec->bits_per_sample = wsaud->audio_bits;
  129. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  130. st->codec->bits_per_sample / 4;
  131. st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
  132. wsaud->audio_stream_index = st->index;
  133. wsaud->audio_frame_counter = 0;
  134. return 0;
  135. }
  136. static int wsaud_read_packet(AVFormatContext *s,
  137. AVPacket *pkt)
  138. {
  139. WsAudDemuxContext *wsaud = s->priv_data;
  140. ByteIOContext *pb = &s->pb;
  141. unsigned char preamble[AUD_CHUNK_PREAMBLE_SIZE];
  142. unsigned int chunk_size;
  143. int ret = 0;
  144. if (get_buffer(pb, preamble, AUD_CHUNK_PREAMBLE_SIZE) !=
  145. AUD_CHUNK_PREAMBLE_SIZE)
  146. return AVERROR(EIO);
  147. /* validate the chunk */
  148. if (AV_RL32(&preamble[4]) != AUD_CHUNK_SIGNATURE)
  149. return AVERROR_INVALIDDATA;
  150. chunk_size = AV_RL16(&preamble[0]);
  151. ret= av_get_packet(pb, pkt, chunk_size);
  152. if (ret != chunk_size)
  153. return AVERROR(EIO);
  154. pkt->stream_index = wsaud->audio_stream_index;
  155. pkt->pts = wsaud->audio_frame_counter;
  156. pkt->pts /= wsaud->audio_samplerate;
  157. /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
  158. wsaud->audio_frame_counter += (chunk_size * 2) / wsaud->audio_channels;
  159. return ret;
  160. }
  161. static int wsaud_read_close(AVFormatContext *s)
  162. {
  163. // WsAudDemuxContext *wsaud = s->priv_data;
  164. return 0;
  165. }
  166. static int wsvqa_probe(AVProbeData *p)
  167. {
  168. /* need 12 bytes to qualify */
  169. if (p->buf_size < 12)
  170. return 0;
  171. /* check for the VQA signatures */
  172. if ((AV_RB32(&p->buf[0]) != FORM_TAG) ||
  173. (AV_RB32(&p->buf[8]) != WVQA_TAG))
  174. return 0;
  175. return AVPROBE_SCORE_MAX;
  176. }
  177. static int wsvqa_read_header(AVFormatContext *s,
  178. AVFormatParameters *ap)
  179. {
  180. WsVqaDemuxContext *wsvqa = s->priv_data;
  181. ByteIOContext *pb = &s->pb;
  182. AVStream *st;
  183. unsigned char *header;
  184. unsigned char scratch[VQA_PREAMBLE_SIZE];
  185. unsigned int chunk_tag;
  186. unsigned int chunk_size;
  187. /* initialize the video decoder stream */
  188. st = av_new_stream(s, 0);
  189. if (!st)
  190. return AVERROR(ENOMEM);
  191. av_set_pts_info(st, 33, 1, VQA_FRAMERATE);
  192. wsvqa->video_stream_index = st->index;
  193. st->codec->codec_type = CODEC_TYPE_VIDEO;
  194. st->codec->codec_id = CODEC_ID_WS_VQA;
  195. st->codec->codec_tag = 0; /* no fourcc */
  196. /* skip to the start of the VQA header */
  197. url_fseek(pb, 20, SEEK_SET);
  198. /* the VQA header needs to go to the decoder */
  199. st->codec->extradata_size = VQA_HEADER_SIZE;
  200. st->codec->extradata = av_mallocz(VQA_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
  201. header = (unsigned char *)st->codec->extradata;
  202. if (get_buffer(pb, st->codec->extradata, VQA_HEADER_SIZE) !=
  203. VQA_HEADER_SIZE) {
  204. av_free(st->codec->extradata);
  205. return AVERROR(EIO);
  206. }
  207. st->codec->width = AV_RL16(&header[6]);
  208. st->codec->height = AV_RL16(&header[8]);
  209. /* initialize the audio decoder stream for VQA v1 or nonzero samplerate */
  210. if (AV_RL16(&header[24]) || (AV_RL16(&header[0]) == 1 && AV_RL16(&header[2]) == 1)) {
  211. st = av_new_stream(s, 0);
  212. if (!st)
  213. return AVERROR(ENOMEM);
  214. av_set_pts_info(st, 33, 1, VQA_FRAMERATE);
  215. st->codec->codec_type = CODEC_TYPE_AUDIO;
  216. if (AV_RL16(&header[0]) == 1)
  217. st->codec->codec_id = CODEC_ID_WESTWOOD_SND1;
  218. else
  219. st->codec->codec_id = CODEC_ID_ADPCM_IMA_WS;
  220. st->codec->codec_tag = 0; /* no tag */
  221. st->codec->sample_rate = AV_RL16(&header[24]);
  222. if (!st->codec->sample_rate)
  223. st->codec->sample_rate = 22050;
  224. st->codec->channels = header[26];
  225. if (!st->codec->channels)
  226. st->codec->channels = 1;
  227. st->codec->bits_per_sample = 16;
  228. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  229. st->codec->bits_per_sample / 4;
  230. st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
  231. wsvqa->audio_stream_index = st->index;
  232. wsvqa->audio_samplerate = st->codec->sample_rate;
  233. wsvqa->audio_channels = st->codec->channels;
  234. wsvqa->audio_frame_counter = 0;
  235. }
  236. /* there are 0 or more chunks before the FINF chunk; iterate until
  237. * FINF has been skipped and the file will be ready to be demuxed */
  238. do {
  239. if (get_buffer(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE) {
  240. av_free(st->codec->extradata);
  241. return AVERROR(EIO);
  242. }
  243. chunk_tag = AV_RB32(&scratch[0]);
  244. chunk_size = AV_RB32(&scratch[4]);
  245. /* catch any unknown header tags, for curiousity */
  246. switch (chunk_tag) {
  247. case CINF_TAG:
  248. case CINH_TAG:
  249. case CIND_TAG:
  250. case PINF_TAG:
  251. case PINH_TAG:
  252. case PIND_TAG:
  253. case FINF_TAG:
  254. case CMDS_TAG:
  255. break;
  256. default:
  257. av_log (s, AV_LOG_ERROR, " note: unknown chunk seen (%c%c%c%c)\n",
  258. scratch[0], scratch[1],
  259. scratch[2], scratch[3]);
  260. break;
  261. }
  262. url_fseek(pb, chunk_size, SEEK_CUR);
  263. } while (chunk_tag != FINF_TAG);
  264. wsvqa->video_pts = wsvqa->audio_frame_counter = 0;
  265. return 0;
  266. }
  267. static int wsvqa_read_packet(AVFormatContext *s,
  268. AVPacket *pkt)
  269. {
  270. WsVqaDemuxContext *wsvqa = s->priv_data;
  271. ByteIOContext *pb = &s->pb;
  272. int ret = -1;
  273. unsigned char preamble[VQA_PREAMBLE_SIZE];
  274. unsigned int chunk_type;
  275. unsigned int chunk_size;
  276. int skip_byte;
  277. while (get_buffer(pb, preamble, VQA_PREAMBLE_SIZE) == VQA_PREAMBLE_SIZE) {
  278. chunk_type = AV_RB32(&preamble[0]);
  279. chunk_size = AV_RB32(&preamble[4]);
  280. skip_byte = chunk_size & 0x01;
  281. if ((chunk_type == SND1_TAG) || (chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
  282. if (av_new_packet(pkt, chunk_size))
  283. return AVERROR(EIO);
  284. ret = get_buffer(pb, pkt->data, chunk_size);
  285. if (ret != chunk_size) {
  286. av_free_packet(pkt);
  287. return AVERROR(EIO);
  288. }
  289. if (chunk_type == SND2_TAG) {
  290. pkt->stream_index = wsvqa->audio_stream_index;
  291. /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
  292. wsvqa->audio_frame_counter += (chunk_size * 2) / wsvqa->audio_channels;
  293. } else if(chunk_type == SND1_TAG) {
  294. pkt->stream_index = wsvqa->audio_stream_index;
  295. /* unpacked size is stored in header */
  296. wsvqa->audio_frame_counter += AV_RL16(pkt->data) / wsvqa->audio_channels;
  297. } else {
  298. pkt->stream_index = wsvqa->video_stream_index;
  299. wsvqa->video_pts += VQA_VIDEO_PTS_INC;
  300. }
  301. /* stay on 16-bit alignment */
  302. if (skip_byte)
  303. url_fseek(pb, 1, SEEK_CUR);
  304. return ret;
  305. } else {
  306. switch(chunk_type){
  307. case CMDS_TAG:
  308. case SND0_TAG:
  309. break;
  310. default:
  311. av_log(s, AV_LOG_INFO, "Skipping unknown chunk 0x%08X\n", chunk_type);
  312. }
  313. url_fseek(pb, chunk_size + skip_byte, SEEK_CUR);
  314. }
  315. }
  316. return ret;
  317. }
  318. static int wsvqa_read_close(AVFormatContext *s)
  319. {
  320. // WsVqaDemuxContext *wsvqa = s->priv_data;
  321. return 0;
  322. }
  323. #ifdef CONFIG_WSAUD_DEMUXER
  324. AVInputFormat wsaud_demuxer = {
  325. "wsaud",
  326. "Westwood Studios audio format",
  327. sizeof(WsAudDemuxContext),
  328. wsaud_probe,
  329. wsaud_read_header,
  330. wsaud_read_packet,
  331. wsaud_read_close,
  332. };
  333. #endif
  334. #ifdef CONFIG_WSVQA_DEMUXER
  335. AVInputFormat wsvqa_demuxer = {
  336. "wsvqa",
  337. "Westwood Studios VQA format",
  338. sizeof(WsVqaDemuxContext),
  339. wsvqa_probe,
  340. wsvqa_read_header,
  341. wsvqa_read_packet,
  342. wsvqa_read_close,
  343. };
  344. #endif