westwood.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 SND2_TAG MKBETAG('S', 'N', 'D', '2')
  44. #define VQFR_TAG MKBETAG('V', 'Q', 'F', 'R')
  45. /* don't know what these tags are for, but acknowledge their existence */
  46. #define CINF_TAG MKBETAG('C', 'I', 'N', 'F')
  47. #define CINH_TAG MKBETAG('C', 'I', 'N', 'H')
  48. #define CIND_TAG MKBETAG('C', 'I', 'N', 'D')
  49. #define PINF_TAG MKBETAG('P', 'I', 'N', 'F')
  50. #define PINH_TAG MKBETAG('P', 'I', 'N', 'H')
  51. #define PIND_TAG MKBETAG('P', 'I', 'N', 'D')
  52. #define VQA_HEADER_SIZE 0x2A
  53. #define VQA_FRAMERATE 15
  54. #define VQA_VIDEO_PTS_INC (90000 / VQA_FRAMERATE)
  55. #define VQA_PREAMBLE_SIZE 8
  56. typedef struct WsAudDemuxContext {
  57. int audio_samplerate;
  58. int audio_channels;
  59. int audio_bits;
  60. int audio_type;
  61. int audio_stream_index;
  62. int64_t audio_frame_counter;
  63. } WsAudDemuxContext;
  64. typedef struct WsVqaDemuxContext {
  65. int audio_samplerate;
  66. int audio_channels;
  67. int audio_bits;
  68. int audio_stream_index;
  69. int video_stream_index;
  70. int64_t audio_frame_counter;
  71. int64_t video_pts;
  72. } WsVqaDemuxContext;
  73. static int wsaud_probe(AVProbeData *p)
  74. {
  75. int field;
  76. /* Probabilistic content detection strategy: There is no file signature
  77. * so perform sanity checks on various header parameters:
  78. * 8000 <= sample rate (16 bits) <= 48000 ==> 40001 acceptable numbers
  79. * compression type (8 bits) = 1 or 99 ==> 2 acceptable numbers
  80. * There is a total of 24 bits. The number space contains 2^24 =
  81. * 16777216 numbers. There are 40001 * 2 = 80002 acceptable combinations
  82. * of numbers. There is a 80002/16777216 = 0.48% chance of a false
  83. * positive.
  84. */
  85. if (p->buf_size < AUD_HEADER_SIZE)
  86. return 0;
  87. /* check sample rate */
  88. field = LE_16(&p->buf[0]);
  89. if ((field < 8000) || (field > 48000))
  90. return 0;
  91. /* note: only check for WS IMA (type 99) right now since there is no
  92. * support for type 1 */
  93. if (p->buf[11] != 99)
  94. return 0;
  95. /* return 1/2 certainty since this file check is a little sketchy */
  96. return AVPROBE_SCORE_MAX / 2;
  97. }
  98. static int wsaud_read_header(AVFormatContext *s,
  99. AVFormatParameters *ap)
  100. {
  101. WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
  102. ByteIOContext *pb = &s->pb;
  103. AVStream *st;
  104. unsigned char header[AUD_HEADER_SIZE];
  105. if (get_buffer(pb, header, AUD_HEADER_SIZE) != AUD_HEADER_SIZE)
  106. return AVERROR_IO;
  107. wsaud->audio_samplerate = LE_16(&header[0]);
  108. if (header[11] == 99)
  109. wsaud->audio_type = CODEC_ID_ADPCM_IMA_WS;
  110. else
  111. return AVERROR_INVALIDDATA;
  112. /* flag 0 indicates stereo */
  113. wsaud->audio_channels = (header[10] & 0x1) + 1;
  114. /* flag 1 indicates 16 bit audio */
  115. wsaud->audio_bits = (((header[10] & 0x2) >> 1) + 1) * 8;
  116. /* initialize the audio decoder stream */
  117. st = av_new_stream(s, 0);
  118. if (!st)
  119. return AVERROR_NOMEM;
  120. av_set_pts_info(st, 33, 1, wsaud->audio_samplerate);
  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 AVERROR_IO;
  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. ret= av_get_packet(pb, pkt, chunk_size);
  150. if (ret != chunk_size)
  151. return AVERROR_IO;
  152. pkt->stream_index = wsaud->audio_stream_index;
  153. pkt->pts = wsaud->audio_frame_counter;
  154. pkt->pts /= wsaud->audio_samplerate;
  155. /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
  156. wsaud->audio_frame_counter += (chunk_size * 2) / wsaud->audio_channels;
  157. return ret;
  158. }
  159. static int wsaud_read_close(AVFormatContext *s)
  160. {
  161. // WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
  162. return 0;
  163. }
  164. static int wsvqa_probe(AVProbeData *p)
  165. {
  166. /* need 12 bytes to qualify */
  167. if (p->buf_size < 12)
  168. return 0;
  169. /* check for the VQA signatures */
  170. if ((BE_32(&p->buf[0]) != FORM_TAG) ||
  171. (BE_32(&p->buf[8]) != WVQA_TAG))
  172. return 0;
  173. return AVPROBE_SCORE_MAX;
  174. }
  175. static int wsvqa_read_header(AVFormatContext *s,
  176. AVFormatParameters *ap)
  177. {
  178. WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
  179. ByteIOContext *pb = &s->pb;
  180. AVStream *st;
  181. unsigned char *header;
  182. unsigned char scratch[VQA_PREAMBLE_SIZE];
  183. unsigned int chunk_tag;
  184. unsigned int chunk_size;
  185. /* initialize the video decoder stream */
  186. st = av_new_stream(s, 0);
  187. if (!st)
  188. return AVERROR_NOMEM;
  189. av_set_pts_info(st, 33, 1, 90000);
  190. wsvqa->video_stream_index = st->index;
  191. st->codec->codec_type = CODEC_TYPE_VIDEO;
  192. st->codec->codec_id = CODEC_ID_WS_VQA;
  193. st->codec->codec_tag = 0; /* no fourcc */
  194. /* skip to the start of the VQA header */
  195. url_fseek(pb, 20, SEEK_SET);
  196. /* the VQA header needs to go to the decoder */
  197. st->codec->extradata_size = VQA_HEADER_SIZE;
  198. st->codec->extradata = av_mallocz(VQA_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
  199. header = (unsigned char *)st->codec->extradata;
  200. if (get_buffer(pb, st->codec->extradata, VQA_HEADER_SIZE) !=
  201. VQA_HEADER_SIZE) {
  202. av_free(st->codec->extradata);
  203. return AVERROR_IO;
  204. }
  205. st->codec->width = LE_16(&header[6]);
  206. st->codec->height = LE_16(&header[8]);
  207. /* initialize the audio decoder stream is sample rate is non-zero */
  208. if (LE_16(&header[24])) {
  209. st = av_new_stream(s, 0);
  210. if (!st)
  211. return AVERROR_NOMEM;
  212. av_set_pts_info(st, 33, 1, 90000);
  213. st->codec->codec_type = CODEC_TYPE_AUDIO;
  214. st->codec->codec_id = CODEC_ID_ADPCM_IMA_WS;
  215. st->codec->codec_tag = 0; /* no tag */
  216. st->codec->sample_rate = LE_16(&header[24]);
  217. st->codec->channels = header[26];
  218. st->codec->bits_per_sample = 16;
  219. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  220. st->codec->bits_per_sample / 4;
  221. st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
  222. wsvqa->audio_stream_index = st->index;
  223. wsvqa->audio_samplerate = st->codec->sample_rate;
  224. wsvqa->audio_channels = st->codec->channels;
  225. wsvqa->audio_frame_counter = 0;
  226. }
  227. /* there are 0 or more chunks before the FINF chunk; iterate until
  228. * FINF has been skipped and the file will be ready to be demuxed */
  229. do {
  230. if (get_buffer(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE) {
  231. av_free(st->codec->extradata);
  232. return AVERROR_IO;
  233. }
  234. chunk_tag = BE_32(&scratch[0]);
  235. chunk_size = BE_32(&scratch[4]);
  236. /* catch any unknown header tags, for curiousity */
  237. switch (chunk_tag) {
  238. case CINF_TAG:
  239. case CINH_TAG:
  240. case CIND_TAG:
  241. case PINF_TAG:
  242. case PINH_TAG:
  243. case PIND_TAG:
  244. case FINF_TAG:
  245. break;
  246. default:
  247. av_log (s, AV_LOG_ERROR, " note: unknown chunk seen (%c%c%c%c)\n",
  248. scratch[0], scratch[1],
  249. scratch[2], scratch[3]);
  250. break;
  251. }
  252. url_fseek(pb, chunk_size, SEEK_CUR);
  253. } while (chunk_tag != FINF_TAG);
  254. wsvqa->video_pts = wsvqa->audio_frame_counter = 0;
  255. return 0;
  256. }
  257. static int wsvqa_read_packet(AVFormatContext *s,
  258. AVPacket *pkt)
  259. {
  260. WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
  261. ByteIOContext *pb = &s->pb;
  262. int ret = 0;
  263. unsigned char preamble[VQA_PREAMBLE_SIZE];
  264. unsigned int chunk_type;
  265. unsigned int chunk_size;
  266. int skip_byte;
  267. if (get_buffer(pb, preamble, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE)
  268. return AVERROR_IO;
  269. chunk_type = BE_32(&preamble[0]);
  270. chunk_size = BE_32(&preamble[4]);
  271. skip_byte = chunk_size & 0x01;
  272. if ((chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
  273. av_get_packet(pb, pkt, chunk_size);
  274. if (ret != chunk_size) {
  275. ret = AVERROR_IO;
  276. }
  277. if (chunk_type == SND2_TAG) {
  278. pkt->stream_index = wsvqa->audio_stream_index;
  279. pkt->pts = 90000;
  280. pkt->pts *= wsvqa->audio_frame_counter;
  281. pkt->pts /= wsvqa->audio_samplerate;
  282. /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
  283. wsvqa->audio_frame_counter += (chunk_size * 2) /
  284. wsvqa->audio_channels;
  285. } else {
  286. pkt->stream_index = wsvqa->video_stream_index;
  287. pkt->pts = wsvqa->video_pts;
  288. wsvqa->video_pts += VQA_VIDEO_PTS_INC;
  289. }
  290. } else
  291. return AVERROR_INVALIDDATA;
  292. /* stay on 16-bit alignment */
  293. if (skip_byte)
  294. url_fseek(pb, 1, SEEK_CUR);
  295. return ret;
  296. }
  297. static int wsvqa_read_close(AVFormatContext *s)
  298. {
  299. // WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
  300. return 0;
  301. }
  302. #ifdef CONFIG_WSAUD_DEMUXER
  303. AVInputFormat wsaud_demuxer = {
  304. "wsaud",
  305. "Westwood Studios audio format",
  306. sizeof(WsAudDemuxContext),
  307. wsaud_probe,
  308. wsaud_read_header,
  309. wsaud_read_packet,
  310. wsaud_read_close,
  311. };
  312. #endif
  313. #ifdef CONFIG_WSVQA_DEMUXER
  314. AVInputFormat wsvqa_demuxer = {
  315. "wsvqa",
  316. "Westwood Studios VQA format",
  317. sizeof(WsVqaDemuxContext),
  318. wsvqa_probe,
  319. wsvqa_read_header,
  320. wsvqa_read_packet,
  321. wsvqa_read_close,
  322. };
  323. #endif