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.

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