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.

268 lines
8.8KB

  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 audio_channels;
  52. int audio_stream_index;
  53. int video_stream_index;
  54. } WsVqaDemuxContext;
  55. static int wsvqa_probe(AVProbeData *p)
  56. {
  57. /* need 12 bytes to qualify */
  58. if (p->buf_size < 12)
  59. return 0;
  60. /* check for the VQA signatures */
  61. if ((AV_RB32(&p->buf[0]) != FORM_TAG) ||
  62. (AV_RB32(&p->buf[8]) != WVQA_TAG))
  63. return 0;
  64. return AVPROBE_SCORE_MAX;
  65. }
  66. static int wsvqa_read_header(AVFormatContext *s,
  67. AVFormatParameters *ap)
  68. {
  69. WsVqaDemuxContext *wsvqa = s->priv_data;
  70. AVIOContext *pb = s->pb;
  71. AVStream *st;
  72. unsigned char *header;
  73. unsigned char scratch[VQA_PREAMBLE_SIZE];
  74. unsigned int chunk_tag;
  75. unsigned int chunk_size;
  76. int fps, version, flags, sample_rate, channels;
  77. /* initialize the video decoder stream */
  78. st = avformat_new_stream(s, NULL);
  79. if (!st)
  80. return AVERROR(ENOMEM);
  81. st->start_time = 0;
  82. wsvqa->video_stream_index = st->index;
  83. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  84. st->codec->codec_id = CODEC_ID_WS_VQA;
  85. st->codec->codec_tag = 0; /* no fourcc */
  86. /* skip to the start of the VQA header */
  87. avio_seek(pb, 20, SEEK_SET);
  88. /* the VQA header needs to go to the decoder */
  89. st->codec->extradata_size = VQA_HEADER_SIZE;
  90. st->codec->extradata = av_mallocz(VQA_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
  91. header = (unsigned char *)st->codec->extradata;
  92. if (avio_read(pb, st->codec->extradata, VQA_HEADER_SIZE) !=
  93. VQA_HEADER_SIZE) {
  94. av_free(st->codec->extradata);
  95. return AVERROR(EIO);
  96. }
  97. st->codec->width = AV_RL16(&header[6]);
  98. st->codec->height = AV_RL16(&header[8]);
  99. fps = header[12];
  100. if (fps < 1 || fps > 30) {
  101. av_log(s, AV_LOG_ERROR, "invalid fps: %d\n", fps);
  102. return AVERROR_INVALIDDATA;
  103. }
  104. avpriv_set_pts_info(st, 64, 1, fps);
  105. /* initialize the audio decoder stream for VQA v1 or nonzero samplerate */
  106. version = AV_RL16(&header[ 0]);
  107. flags = AV_RL16(&header[ 2]);
  108. sample_rate = AV_RL16(&header[24]);
  109. channels = header[26];
  110. if (sample_rate || (version == 1 && flags == 1)) {
  111. st = avformat_new_stream(s, NULL);
  112. if (!st)
  113. return AVERROR(ENOMEM);
  114. st->start_time = 0;
  115. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  116. st->codec->extradata_size = VQA_HEADER_SIZE;
  117. st->codec->extradata = av_mallocz(VQA_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
  118. if (!st->codec->extradata)
  119. return AVERROR(ENOMEM);
  120. memcpy(st->codec->extradata, header, VQA_HEADER_SIZE);
  121. if (!sample_rate)
  122. sample_rate = 22050;
  123. st->codec->sample_rate = sample_rate;
  124. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  125. if (!channels)
  126. channels = 1;
  127. st->codec->channels = channels;
  128. switch (version) {
  129. case 1:
  130. st->codec->codec_id = CODEC_ID_WESTWOOD_SND1;
  131. break;
  132. case 2:
  133. case 3:
  134. st->codec->codec_id = CODEC_ID_ADPCM_IMA_WS;
  135. st->codec->bits_per_coded_sample = 4;
  136. st->codec->bit_rate = channels * sample_rate * 4;
  137. break;
  138. default:
  139. /* NOTE: version 0 is supposedly raw pcm_u8 or pcm_s16le, but we do
  140. not have any samples to validate this */
  141. av_log_ask_for_sample(s, "VQA version %d audio\n", version);
  142. return AVERROR_PATCHWELCOME;
  143. }
  144. wsvqa->audio_stream_index = st->index;
  145. wsvqa->audio_channels = st->codec->channels;
  146. }
  147. /* there are 0 or more chunks before the FINF chunk; iterate until
  148. * FINF has been skipped and the file will be ready to be demuxed */
  149. do {
  150. if (avio_read(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE)
  151. return AVERROR(EIO);
  152. chunk_tag = AV_RB32(&scratch[0]);
  153. chunk_size = AV_RB32(&scratch[4]);
  154. /* catch any unknown header tags, for curiousity */
  155. switch (chunk_tag) {
  156. case CINF_TAG:
  157. case CINH_TAG:
  158. case CIND_TAG:
  159. case PINF_TAG:
  160. case PINH_TAG:
  161. case PIND_TAG:
  162. case FINF_TAG:
  163. case CMDS_TAG:
  164. break;
  165. default:
  166. av_log (s, AV_LOG_ERROR, " note: unknown chunk seen (%c%c%c%c)\n",
  167. scratch[0], scratch[1],
  168. scratch[2], scratch[3]);
  169. break;
  170. }
  171. avio_skip(pb, chunk_size);
  172. } while (chunk_tag != FINF_TAG);
  173. return 0;
  174. }
  175. static int wsvqa_read_packet(AVFormatContext *s,
  176. AVPacket *pkt)
  177. {
  178. WsVqaDemuxContext *wsvqa = s->priv_data;
  179. AVIOContext *pb = s->pb;
  180. int ret = -1;
  181. unsigned char preamble[VQA_PREAMBLE_SIZE];
  182. unsigned int chunk_type;
  183. unsigned int chunk_size;
  184. int skip_byte;
  185. while (avio_read(pb, preamble, VQA_PREAMBLE_SIZE) == VQA_PREAMBLE_SIZE) {
  186. chunk_type = AV_RB32(&preamble[0]);
  187. chunk_size = AV_RB32(&preamble[4]);
  188. skip_byte = chunk_size & 0x01;
  189. if ((chunk_type == SND2_TAG || chunk_type == SND1_TAG) && wsvqa->audio_channels == 0) {
  190. av_log(s, AV_LOG_ERROR, "audio chunk without any audio header information found\n");
  191. return AVERROR_INVALIDDATA;
  192. }
  193. if ((chunk_type == SND1_TAG) || (chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
  194. ret= av_get_packet(pb, pkt, chunk_size);
  195. if (ret<0)
  196. return AVERROR(EIO);
  197. if (chunk_type == SND2_TAG) {
  198. pkt->stream_index = wsvqa->audio_stream_index;
  199. /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
  200. pkt->duration = (chunk_size * 2) / wsvqa->audio_channels;
  201. } else if(chunk_type == SND1_TAG) {
  202. pkt->stream_index = wsvqa->audio_stream_index;
  203. /* unpacked size is stored in header */
  204. pkt->duration = AV_RL16(pkt->data) / wsvqa->audio_channels;
  205. } else {
  206. pkt->stream_index = wsvqa->video_stream_index;
  207. pkt->duration = 1;
  208. }
  209. /* stay on 16-bit alignment */
  210. if (skip_byte)
  211. avio_skip(pb, 1);
  212. return ret;
  213. } else {
  214. switch(chunk_type){
  215. case CMDS_TAG:
  216. case SND0_TAG:
  217. break;
  218. default:
  219. av_log(s, AV_LOG_INFO, "Skipping unknown chunk 0x%08X\n", chunk_type);
  220. }
  221. avio_skip(pb, chunk_size + skip_byte);
  222. }
  223. }
  224. return ret;
  225. }
  226. AVInputFormat ff_wsvqa_demuxer = {
  227. .name = "wsvqa",
  228. .long_name = NULL_IF_CONFIG_SMALL("Westwood Studios VQA format"),
  229. .priv_data_size = sizeof(WsVqaDemuxContext),
  230. .read_probe = wsvqa_probe,
  231. .read_header = wsvqa_read_header,
  232. .read_packet = wsvqa_read_packet,
  233. };