westwood_vqa.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Westwood Studios VQA Format Demuxer
  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
  23. * Westwood Studios VQA file demuxer
  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. #include "libavutil/intreadwrite.h"
  30. #include "avformat.h"
  31. #include "internal.h"
  32. #define FORM_TAG MKBETAG('F', 'O', 'R', 'M')
  33. #define WVQA_TAG MKBETAG('W', 'V', 'Q', 'A')
  34. #define VQHD_TAG MKBETAG('V', 'Q', 'H', 'D')
  35. #define FINF_TAG MKBETAG('F', 'I', 'N', 'F')
  36. #define SND0_TAG MKBETAG('S', 'N', 'D', '0')
  37. #define SND1_TAG MKBETAG('S', 'N', 'D', '1')
  38. #define SND2_TAG MKBETAG('S', 'N', 'D', '2')
  39. #define VQFR_TAG MKBETAG('V', 'Q', 'F', 'R')
  40. /* don't know what these tags are for, but acknowledge their existence */
  41. #define CINF_TAG MKBETAG('C', 'I', 'N', 'F')
  42. #define CINH_TAG MKBETAG('C', 'I', 'N', 'H')
  43. #define CIND_TAG MKBETAG('C', 'I', 'N', 'D')
  44. #define PINF_TAG MKBETAG('P', 'I', 'N', 'F')
  45. #define PINH_TAG MKBETAG('P', 'I', 'N', 'H')
  46. #define PIND_TAG MKBETAG('P', 'I', 'N', 'D')
  47. #define CMDS_TAG MKBETAG('C', 'M', 'D', 'S')
  48. #define VQA_HEADER_SIZE 0x2A
  49. #define VQA_PREAMBLE_SIZE 8
  50. typedef struct WsVqaDemuxContext {
  51. int version;
  52. int bps;
  53. int channels;
  54. int sample_rate;
  55. int audio_stream_index;
  56. int video_stream_index;
  57. } WsVqaDemuxContext;
  58. static int wsvqa_probe(AVProbeData *p)
  59. {
  60. /* need 12 bytes to qualify */
  61. if (p->buf_size < 12)
  62. return 0;
  63. /* check for the VQA signatures */
  64. if ((AV_RB32(&p->buf[0]) != FORM_TAG) ||
  65. (AV_RB32(&p->buf[8]) != WVQA_TAG))
  66. return 0;
  67. return AVPROBE_SCORE_MAX;
  68. }
  69. static int wsvqa_read_header(AVFormatContext *s)
  70. {
  71. WsVqaDemuxContext *wsvqa = s->priv_data;
  72. AVIOContext *pb = s->pb;
  73. AVStream *st;
  74. unsigned char *header;
  75. unsigned char scratch[VQA_PREAMBLE_SIZE];
  76. unsigned int chunk_tag;
  77. unsigned int chunk_size;
  78. int fps;
  79. /* initialize the video decoder stream */
  80. st = avformat_new_stream(s, NULL);
  81. if (!st)
  82. return AVERROR(ENOMEM);
  83. st->start_time = 0;
  84. wsvqa->video_stream_index = st->index;
  85. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  86. st->codec->codec_id = AV_CODEC_ID_WS_VQA;
  87. st->codec->codec_tag = 0; /* no fourcc */
  88. /* skip to the start of the VQA header */
  89. avio_seek(pb, 20, SEEK_SET);
  90. /* the VQA header needs to go to the decoder */
  91. st->codec->extradata_size = VQA_HEADER_SIZE;
  92. st->codec->extradata = av_mallocz(VQA_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
  93. header = (unsigned char *)st->codec->extradata;
  94. if (avio_read(pb, st->codec->extradata, VQA_HEADER_SIZE) !=
  95. VQA_HEADER_SIZE) {
  96. av_free(st->codec->extradata);
  97. return AVERROR(EIO);
  98. }
  99. st->codec->width = AV_RL16(&header[6]);
  100. st->codec->height = AV_RL16(&header[8]);
  101. fps = header[12];
  102. st->nb_frames =
  103. st->duration = AV_RL16(&header[4]);
  104. if (fps < 1 || fps > 30) {
  105. av_log(s, AV_LOG_ERROR, "invalid fps: %d\n", fps);
  106. return AVERROR_INVALIDDATA;
  107. }
  108. avpriv_set_pts_info(st, 64, 1, fps);
  109. wsvqa->version = AV_RL16(&header[ 0]);
  110. wsvqa->sample_rate = AV_RL16(&header[24]);
  111. wsvqa->channels = header[26];
  112. wsvqa->bps = header[27];
  113. wsvqa->audio_stream_index = -1;
  114. s->ctx_flags |= AVFMTCTX_NOHEADER;
  115. /* there are 0 or more chunks before the FINF chunk; iterate until
  116. * FINF has been skipped and the file will be ready to be demuxed */
  117. do {
  118. if (avio_read(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE)
  119. return AVERROR(EIO);
  120. chunk_tag = AV_RB32(&scratch[0]);
  121. chunk_size = AV_RB32(&scratch[4]);
  122. /* catch any unknown header tags, for curiousity */
  123. switch (chunk_tag) {
  124. case CINF_TAG:
  125. case CINH_TAG:
  126. case CIND_TAG:
  127. case PINF_TAG:
  128. case PINH_TAG:
  129. case PIND_TAG:
  130. case FINF_TAG:
  131. case CMDS_TAG:
  132. break;
  133. default:
  134. av_log (s, AV_LOG_ERROR, " note: unknown chunk seen (%c%c%c%c)\n",
  135. scratch[0], scratch[1],
  136. scratch[2], scratch[3]);
  137. break;
  138. }
  139. avio_skip(pb, chunk_size);
  140. } while (chunk_tag != FINF_TAG);
  141. return 0;
  142. }
  143. static int wsvqa_read_packet(AVFormatContext *s,
  144. AVPacket *pkt)
  145. {
  146. WsVqaDemuxContext *wsvqa = s->priv_data;
  147. AVIOContext *pb = s->pb;
  148. int ret = -1;
  149. unsigned char preamble[VQA_PREAMBLE_SIZE];
  150. unsigned int chunk_type;
  151. unsigned int chunk_size;
  152. int skip_byte;
  153. while (avio_read(pb, preamble, VQA_PREAMBLE_SIZE) == VQA_PREAMBLE_SIZE) {
  154. chunk_type = AV_RB32(&preamble[0]);
  155. chunk_size = AV_RB32(&preamble[4]);
  156. skip_byte = chunk_size & 0x01;
  157. if ((chunk_type == SND0_TAG) || (chunk_type == SND1_TAG) ||
  158. (chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
  159. ret= av_get_packet(pb, pkt, chunk_size);
  160. if (ret<0)
  161. return AVERROR(EIO);
  162. switch (chunk_type) {
  163. case SND0_TAG:
  164. case SND1_TAG:
  165. case SND2_TAG:
  166. if (wsvqa->audio_stream_index == -1) {
  167. AVStream *st = avformat_new_stream(s, NULL);
  168. if (!st)
  169. return AVERROR(ENOMEM);
  170. wsvqa->audio_stream_index = st->index;
  171. if (!wsvqa->sample_rate)
  172. wsvqa->sample_rate = 22050;
  173. if (!wsvqa->channels)
  174. wsvqa->channels = 1;
  175. if (!wsvqa->bps)
  176. wsvqa->bps = 8;
  177. st->codec->sample_rate = wsvqa->sample_rate;
  178. st->codec->bits_per_coded_sample = wsvqa->bps;
  179. st->codec->channels = wsvqa->channels;
  180. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  181. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  182. switch (chunk_type) {
  183. case SND0_TAG:
  184. if (wsvqa->bps == 16)
  185. st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
  186. else
  187. st->codec->codec_id = AV_CODEC_ID_PCM_U8;
  188. break;
  189. case SND1_TAG:
  190. st->codec->codec_id = AV_CODEC_ID_WESTWOOD_SND1;
  191. break;
  192. case SND2_TAG:
  193. st->codec->codec_id = AV_CODEC_ID_ADPCM_IMA_WS;
  194. st->codec->extradata_size = 2;
  195. st->codec->extradata = av_mallocz(2 + FF_INPUT_BUFFER_PADDING_SIZE);
  196. if (!st->codec->extradata)
  197. return AVERROR(ENOMEM);
  198. AV_WL16(st->codec->extradata, wsvqa->version);
  199. break;
  200. }
  201. }
  202. pkt->stream_index = wsvqa->audio_stream_index;
  203. switch (chunk_type) {
  204. case SND1_TAG:
  205. /* unpacked size is stored in header */
  206. pkt->duration = AV_RL16(pkt->data) / wsvqa->channels;
  207. break;
  208. case SND2_TAG:
  209. /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
  210. pkt->duration = (chunk_size * 2) / wsvqa->channels;
  211. break;
  212. }
  213. break;
  214. case VQFR_TAG:
  215. pkt->stream_index = wsvqa->video_stream_index;
  216. pkt->duration = 1;
  217. break;
  218. }
  219. /* stay on 16-bit alignment */
  220. if (skip_byte)
  221. avio_skip(pb, 1);
  222. return ret;
  223. } else {
  224. switch(chunk_type){
  225. case CMDS_TAG:
  226. break;
  227. default:
  228. av_log(s, AV_LOG_INFO, "Skipping unknown chunk 0x%08X\n", chunk_type);
  229. }
  230. avio_skip(pb, chunk_size + skip_byte);
  231. }
  232. }
  233. return ret;
  234. }
  235. AVInputFormat ff_wsvqa_demuxer = {
  236. .name = "wsvqa",
  237. .long_name = NULL_IF_CONFIG_SMALL("Westwood Studios VQA"),
  238. .priv_data_size = sizeof(WsVqaDemuxContext),
  239. .read_probe = wsvqa_probe,
  240. .read_header = wsvqa_read_header,
  241. .read_packet = wsvqa_read_packet,
  242. };