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.

252 lines
8.4KB

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