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.

312 lines
10KB

  1. /*
  2. * Id Quake II CIN File Demuxer
  3. * Copyright (c) 2003 The ffmpeg Project
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file idcin.c
  21. * Id Quake II CIN file demuxer by Mike Melanson (melanson@pcisys.net)
  22. * For more information about the Id CIN format, visit:
  23. * http://www.csse.monash.edu.au/~timf/
  24. *
  25. * CIN is a somewhat quirky and ill-defined format. Here are some notes
  26. * for anyone trying to understand the technical details of this format:
  27. *
  28. * The format has no definite file signature. This is problematic for a
  29. * general-purpose media player that wants to automatically detect file
  30. * types. However, a CIN file does start with 5 32-bit numbers that
  31. * specify audio and video parameters. This demuxer gets around the lack
  32. * of file signature by performing sanity checks on those parameters.
  33. * Probabalistically, this is a reasonable solution since the number of
  34. * valid combinations of the 5 parameters is a very small subset of the
  35. * total 160-bit number space.
  36. *
  37. * Refer to the function idcin_probe() for the precise A/V parameters
  38. * that this demuxer allows.
  39. *
  40. * Next, each audio and video frame has a duration of 1/14 sec. If the
  41. * audio sample rate is a multiple of the common frequency 22050 Hz it will
  42. * divide evenly by 14. However, if the sample rate is 11025 Hz:
  43. * 11025 (samples/sec) / 14 (frames/sec) = 787.5 (samples/frame)
  44. * The way the CIN stores audio in this case is by storing 787 sample
  45. * frames in the first audio frame and 788 sample frames in the second
  46. * audio frame. Therefore, the total number of bytes in an audio frame
  47. * is given as:
  48. * audio frame #0: 787 * (bytes/sample) * (# channels) bytes in frame
  49. * audio frame #1: 788 * (bytes/sample) * (# channels) bytes in frame
  50. * audio frame #2: 787 * (bytes/sample) * (# channels) bytes in frame
  51. * audio frame #3: 788 * (bytes/sample) * (# channels) bytes in frame
  52. *
  53. * Finally, not all Id CIN creation tools agree on the resolution of the
  54. * color palette, apparently. Some creation tools specify red, green, and
  55. * blue palette components in terms of 6-bit VGA color DAC values which
  56. * range from 0..63. Other tools specify the RGB components as full 8-bit
  57. * values that range from 0..255. Since there are no markers in the file to
  58. * differentiate between the two variants, this demuxer uses the following
  59. * heuristic:
  60. * - load the 768 palette bytes from disk
  61. * - assume that they will need to be shifted left by 2 bits to
  62. * transform them from 6-bit values to 8-bit values
  63. * - scan through all 768 palette bytes
  64. * - if any bytes exceed 63, do not shift the bytes at all before
  65. * transmitting them to the video decoder
  66. */
  67. #include "avformat.h"
  68. #define HUFFMAN_TABLE_SIZE (64 * 1024)
  69. #define FRAME_PTS_INC (90000 / 14)
  70. typedef struct IdcinDemuxContext {
  71. int video_stream_index;
  72. int audio_stream_index;
  73. int audio_chunk_size1;
  74. int audio_chunk_size2;
  75. /* demux state variables */
  76. int current_audio_chunk;
  77. int next_chunk_is_video;
  78. int audio_present;
  79. int64_t pts;
  80. AVPaletteControl palctrl;
  81. } IdcinDemuxContext;
  82. static int idcin_probe(AVProbeData *p)
  83. {
  84. unsigned int number;
  85. /*
  86. * This is what you could call a "probabilistic" file check: Id CIN
  87. * files don't have a definite file signature. In lieu of such a marker,
  88. * perform sanity checks on the 5 32-bit header fields:
  89. * width, height: greater than 0, less than or equal to 1024
  90. * audio sample rate: greater than or equal to 8000, less than or
  91. * equal to 48000, or 0 for no audio
  92. * audio sample width (bytes/sample): 0 for no audio, or 1 or 2
  93. * audio channels: 0 for no audio, or 1 or 2
  94. */
  95. /* cannot proceed without 20 bytes */
  96. if (p->buf_size < 20)
  97. return 0;
  98. /* check the video width */
  99. number = LE_32(&p->buf[0]);
  100. if ((number == 0) || (number > 1024))
  101. return 0;
  102. /* check the video height */
  103. number = LE_32(&p->buf[4]);
  104. if ((number == 0) || (number > 1024))
  105. return 0;
  106. /* check the audio sample rate */
  107. number = LE_32(&p->buf[8]);
  108. if ((number != 0) && ((number < 8000) | (number > 48000)))
  109. return 0;
  110. /* check the audio bytes/sample */
  111. number = LE_32(&p->buf[12]);
  112. if (number > 2)
  113. return 0;
  114. /* check the audio channels */
  115. number = LE_32(&p->buf[16]);
  116. if (number > 2)
  117. return 0;
  118. /* return half certainly since this check is a bit sketchy */
  119. return AVPROBE_SCORE_MAX / 2;
  120. }
  121. static int idcin_read_header(AVFormatContext *s,
  122. AVFormatParameters *ap)
  123. {
  124. ByteIOContext *pb = &s->pb;
  125. IdcinDemuxContext *idcin = (IdcinDemuxContext *)s->priv_data;
  126. AVStream *st;
  127. unsigned int width, height;
  128. unsigned int sample_rate, bytes_per_sample, channels;
  129. /* get the 5 header parameters */
  130. width = get_le32(pb);
  131. height = get_le32(pb);
  132. sample_rate = get_le32(pb);
  133. bytes_per_sample = get_le32(pb);
  134. channels = get_le32(pb);
  135. st = av_new_stream(s, 0);
  136. if (!st)
  137. return AVERROR_NOMEM;
  138. idcin->video_stream_index = st->index;
  139. st->codec.codec_type = CODEC_TYPE_VIDEO;
  140. st->codec.codec_id = CODEC_ID_IDCIN;
  141. st->codec.codec_tag = 0; /* no fourcc */
  142. st->codec.width = width;
  143. st->codec.height = height;
  144. /* load up the Huffman tables into extradata */
  145. st->codec.extradata_size = HUFFMAN_TABLE_SIZE;
  146. st->codec.extradata = av_malloc(HUFFMAN_TABLE_SIZE);
  147. if (get_buffer(pb, st->codec.extradata, HUFFMAN_TABLE_SIZE) !=
  148. HUFFMAN_TABLE_SIZE)
  149. return -EIO;
  150. /* save a reference in order to transport the palette */
  151. st->codec.palctrl = &idcin->palctrl;
  152. /* if sample rate is 0, assume no audio */
  153. if (sample_rate) {
  154. idcin->audio_present = 1;
  155. st = av_new_stream(s, 0);
  156. if (!st)
  157. return AVERROR_NOMEM;
  158. idcin->audio_stream_index = st->index;
  159. st->codec.codec_type = CODEC_TYPE_AUDIO;
  160. st->codec.codec_tag = 1;
  161. st->codec.channels = channels;
  162. st->codec.sample_rate = sample_rate;
  163. st->codec.bits_per_sample = bytes_per_sample * 8;
  164. st->codec.bit_rate = sample_rate * bytes_per_sample * 8 * channels;
  165. st->codec.block_align = bytes_per_sample * channels;
  166. if (bytes_per_sample == 1)
  167. st->codec.codec_id = CODEC_ID_PCM_U8;
  168. else
  169. st->codec.codec_id = CODEC_ID_PCM_S16LE;
  170. if (sample_rate % 14 != 0) {
  171. idcin->audio_chunk_size1 = (sample_rate / 14) *
  172. bytes_per_sample * channels;
  173. idcin->audio_chunk_size2 = (sample_rate / 14 + 1) *
  174. bytes_per_sample * channels;
  175. } else {
  176. idcin->audio_chunk_size1 = idcin->audio_chunk_size2 =
  177. (sample_rate / 14) * bytes_per_sample * channels;
  178. }
  179. idcin->current_audio_chunk = 0;
  180. } else
  181. idcin->audio_present = 1;
  182. idcin->next_chunk_is_video = 1;
  183. idcin->pts = 0;
  184. /* set the pts reference (1 pts = 1/90000) */
  185. s->pts_num = 1;
  186. s->pts_den = 90000;
  187. return 0;
  188. }
  189. static int idcin_read_packet(AVFormatContext *s,
  190. AVPacket *pkt)
  191. {
  192. int ret;
  193. unsigned int command;
  194. unsigned int chunk_size;
  195. IdcinDemuxContext *idcin = (IdcinDemuxContext *)s->priv_data;
  196. ByteIOContext *pb = &s->pb;
  197. int i;
  198. int palette_scale;
  199. unsigned char r, g, b;
  200. unsigned char palette_buffer[768];
  201. if (url_feof(&s->pb))
  202. return -EIO;
  203. if (idcin->next_chunk_is_video) {
  204. command = get_le32(pb);
  205. if (command == 2) {
  206. return -EIO;
  207. } else if (command == 1) {
  208. /* trigger a palette change */
  209. idcin->palctrl.palette_changed = 1;
  210. if (get_buffer(pb, palette_buffer, 768) != 768)
  211. return -EIO;
  212. /* scale the palette as necessary */
  213. palette_scale = 2;
  214. for (i = 0; i < 768; i++)
  215. if (palette_buffer[i] > 63) {
  216. palette_scale = 0;
  217. break;
  218. }
  219. for (i = 0; i < 256; i++) {
  220. r = palette_buffer[i * 3 ] << palette_scale;
  221. g = palette_buffer[i * 3 + 1] << palette_scale;
  222. b = palette_buffer[i * 3 + 2] << palette_scale;
  223. idcin->palctrl.palette[i] = (r << 16) | (g << 8) | (b);
  224. }
  225. }
  226. chunk_size = get_le32(pb);
  227. /* skip the number of decoded bytes (always equal to width * height) */
  228. url_fseek(pb, 4, SEEK_CUR);
  229. chunk_size -= 4;
  230. if (av_new_packet(pkt, chunk_size))
  231. ret = -EIO;
  232. pkt->stream_index = idcin->video_stream_index;
  233. pkt->pts = idcin->pts;
  234. ret = get_buffer(pb, pkt->data, chunk_size);
  235. if (ret != chunk_size)
  236. ret = -EIO;
  237. } else {
  238. /* send out the audio chunk */
  239. if (idcin->current_audio_chunk)
  240. chunk_size = idcin->audio_chunk_size2;
  241. else
  242. chunk_size = idcin->audio_chunk_size1;
  243. if (av_new_packet(pkt, chunk_size))
  244. return -EIO;
  245. pkt->stream_index = idcin->audio_stream_index;
  246. pkt->pts = idcin->pts;
  247. ret = get_buffer(&s->pb, pkt->data, chunk_size);
  248. if (ret != chunk_size)
  249. ret = -EIO;
  250. idcin->current_audio_chunk ^= 1;
  251. idcin->pts += FRAME_PTS_INC;
  252. }
  253. if (idcin->audio_present)
  254. idcin->next_chunk_is_video ^= 1;
  255. return ret;
  256. }
  257. static int idcin_read_close(AVFormatContext *s)
  258. {
  259. return 0;
  260. }
  261. static AVInputFormat idcin_iformat = {
  262. "idcin",
  263. "Id CIN format",
  264. sizeof(IdcinDemuxContext),
  265. idcin_probe,
  266. idcin_read_header,
  267. idcin_read_packet,
  268. idcin_read_close,
  269. };
  270. int idcin_init(void)
  271. {
  272. av_register_input_format(&idcin_iformat);
  273. return 0;
  274. }