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.

318 lines
11KB

  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 LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
  69. #define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
  70. (((uint8_t*)(x))[2] << 16) | \
  71. (((uint8_t*)(x))[1] << 8) | \
  72. ((uint8_t*)(x))[0])
  73. #define HUFFMAN_TABLE_SIZE (64 * 1024)
  74. #define FRAME_PTS_INC (90000 / 14)
  75. typedef struct IdcinDemuxContext {
  76. int video_stream_index;
  77. int audio_stream_index;
  78. int audio_chunk_size1;
  79. int audio_chunk_size2;
  80. /* demux state variables */
  81. int current_audio_chunk;
  82. int next_chunk_is_video;
  83. int audio_present;
  84. int64_t pts;
  85. AVPaletteControl palctrl;
  86. } IdcinDemuxContext;
  87. static int idcin_probe(AVProbeData *p)
  88. {
  89. unsigned int number;
  90. /*
  91. * This is what you could call a "probabilistic" file check: Id CIN
  92. * files don't have a definite file signature. In lieu of such a marker,
  93. * perform sanity checks on the 5 32-bit header fields:
  94. * width, height: greater than 0, less than or equal to 1024
  95. * audio sample rate: greater than or equal to 8000, less than or
  96. * equal to 48000, or 0 for no audio
  97. * audio sample width (bytes/sample): 0 for no audio, or 1 or 2
  98. * audio channels: 0 for no audio, or 1 or 2
  99. */
  100. /* cannot proceed without 20 bytes */
  101. if (p->buf_size < 20)
  102. return 0;
  103. /* check the video width */
  104. number = LE_32(&p->buf[0]);
  105. if ((number == 0) || (number > 1024))
  106. return 0;
  107. /* check the video height */
  108. number = LE_32(&p->buf[4]);
  109. if ((number == 0) || (number > 1024))
  110. return 0;
  111. /* check the audio sample rate */
  112. number = LE_32(&p->buf[8]);
  113. if ((number != 0) && ((number < 8000) | (number > 48000)))
  114. return 0;
  115. /* check the audio bytes/sample */
  116. number = LE_32(&p->buf[12]);
  117. if (number > 2)
  118. return 0;
  119. /* check the audio channels */
  120. number = LE_32(&p->buf[16]);
  121. if (number > 2)
  122. return 0;
  123. /* return half certainly since this check is a bit sketchy */
  124. return AVPROBE_SCORE_MAX / 2;
  125. }
  126. static int idcin_read_header(AVFormatContext *s,
  127. AVFormatParameters *ap)
  128. {
  129. ByteIOContext *pb = &s->pb;
  130. IdcinDemuxContext *idcin = (IdcinDemuxContext *)s->priv_data;
  131. AVStream *st;
  132. unsigned int width, height;
  133. unsigned int sample_rate, bytes_per_sample, channels;
  134. /* get the 5 header parameters */
  135. width = get_le32(pb);
  136. height = get_le32(pb);
  137. sample_rate = get_le32(pb);
  138. bytes_per_sample = get_le32(pb);
  139. channels = get_le32(pb);
  140. st = av_new_stream(s, 0);
  141. if (!st)
  142. return AVERROR_NOMEM;
  143. idcin->video_stream_index = st->index;
  144. st->codec.codec_type = CODEC_TYPE_VIDEO;
  145. st->codec.codec_id = CODEC_ID_IDCIN;
  146. st->codec.codec_tag = 0; /* no fourcc */
  147. st->codec.width = width;
  148. st->codec.height = height;
  149. /* load up the Huffman tables into extradata */
  150. st->codec.extradata_size = HUFFMAN_TABLE_SIZE;
  151. st->codec.extradata = av_malloc(HUFFMAN_TABLE_SIZE);
  152. if (get_buffer(pb, st->codec.extradata, HUFFMAN_TABLE_SIZE) !=
  153. HUFFMAN_TABLE_SIZE)
  154. return -EIO;
  155. /* save a reference in order to transport the palette */
  156. st->codec.palctrl = &idcin->palctrl;
  157. /* if sample rate is 0, assume no audio */
  158. if (sample_rate) {
  159. idcin->audio_present = 1;
  160. st = av_new_stream(s, 0);
  161. if (!st)
  162. return AVERROR_NOMEM;
  163. idcin->audio_stream_index = st->index;
  164. st->codec.codec_type = CODEC_TYPE_AUDIO;
  165. st->codec.codec_tag = 1;
  166. st->codec.channels = channels;
  167. st->codec.sample_rate = sample_rate;
  168. st->codec.bits_per_sample = bytes_per_sample * 8;
  169. st->codec.bit_rate = sample_rate * bytes_per_sample * 8 * channels;
  170. st->codec.block_align = bytes_per_sample * channels;
  171. if (bytes_per_sample == 1)
  172. st->codec.codec_id = CODEC_ID_PCM_U8;
  173. else
  174. st->codec.codec_id = CODEC_ID_PCM_S16LE;
  175. if (sample_rate % 14 != 0) {
  176. idcin->audio_chunk_size1 = (sample_rate / 14) *
  177. bytes_per_sample * channels;
  178. idcin->audio_chunk_size2 = (sample_rate / 14 + 1) *
  179. bytes_per_sample * channels;
  180. } else {
  181. idcin->audio_chunk_size1 = idcin->audio_chunk_size2 =
  182. (sample_rate / 14) * bytes_per_sample * channels;
  183. }
  184. idcin->current_audio_chunk = 0;
  185. } else
  186. idcin->audio_present = 1;
  187. idcin->next_chunk_is_video = 1;
  188. idcin->pts = 0;
  189. /* set the pts reference (1 pts = 1/90000) */
  190. s->pts_num = 1;
  191. s->pts_den = 90000;
  192. return 0;
  193. }
  194. static int idcin_read_packet(AVFormatContext *s,
  195. AVPacket *pkt)
  196. {
  197. int ret;
  198. unsigned int command;
  199. unsigned int chunk_size;
  200. IdcinDemuxContext *idcin = (IdcinDemuxContext *)s->priv_data;
  201. ByteIOContext *pb = &s->pb;
  202. int i;
  203. int palette_scale;
  204. unsigned char r, g, b;
  205. unsigned char palette_buffer[768];
  206. if (url_feof(&s->pb))
  207. return -EIO;
  208. if (idcin->next_chunk_is_video) {
  209. command = get_le32(pb);
  210. if (command == 2) {
  211. return -EIO;
  212. } else if (command == 1) {
  213. /* trigger a palette change */
  214. idcin->palctrl.palette_changed = 1;
  215. if (get_buffer(pb, palette_buffer, 768) != 768)
  216. return -EIO;
  217. /* scale the palette as necessary */
  218. palette_scale = 2;
  219. for (i = 0; i < 768; i++)
  220. if (palette_buffer[i] > 63) {
  221. palette_scale = 0;
  222. break;
  223. }
  224. for (i = 0; i < 256; i++) {
  225. r = palette_buffer[i * 3 ] << palette_scale;
  226. g = palette_buffer[i * 3 + 1] << palette_scale;
  227. b = palette_buffer[i * 3 + 2] << palette_scale;
  228. idcin->palctrl.palette[i] = (r << 16) | (g << 8) | (b);
  229. }
  230. }
  231. chunk_size = get_le32(pb);
  232. /* skip the number of decoded bytes (always equal to width * height) */
  233. url_fseek(pb, 4, SEEK_CUR);
  234. chunk_size -= 4;
  235. if (av_new_packet(pkt, chunk_size))
  236. ret = -EIO;
  237. pkt->stream_index = idcin->video_stream_index;
  238. pkt->pts = idcin->pts;
  239. ret = get_buffer(pb, pkt->data, chunk_size);
  240. if (ret != chunk_size)
  241. ret = -EIO;
  242. } else {
  243. /* send out the audio chunk */
  244. if (idcin->current_audio_chunk)
  245. chunk_size = idcin->audio_chunk_size2;
  246. else
  247. chunk_size = idcin->audio_chunk_size1;
  248. if (av_new_packet(pkt, chunk_size))
  249. return -EIO;
  250. pkt->stream_index = idcin->audio_stream_index;
  251. pkt->pts = idcin->pts;
  252. ret = get_buffer(&s->pb, pkt->data, chunk_size);
  253. if (ret != chunk_size)
  254. ret = -EIO;
  255. idcin->current_audio_chunk ^= 1;
  256. idcin->pts += FRAME_PTS_INC;
  257. }
  258. if (idcin->audio_present)
  259. idcin->next_chunk_is_video ^= 1;
  260. return ret;
  261. }
  262. static int idcin_read_close(AVFormatContext *s)
  263. {
  264. return 0;
  265. }
  266. static AVInputFormat idcin_iformat = {
  267. "idcin",
  268. "Id CIN format",
  269. sizeof(IdcinDemuxContext),
  270. idcin_probe,
  271. idcin_read_header,
  272. idcin_read_packet,
  273. idcin_read_close,
  274. };
  275. int idcin_init(void)
  276. {
  277. av_register_input_format(&idcin_iformat);
  278. return 0;
  279. }