idcin.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * id Quake II CIN File 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 libavformat/idcin.c
  23. * id Quake II CIN file demuxer by Mike Melanson (melanson@pcisys.net)
  24. * For more information about the id CIN format, visit:
  25. * http://www.csse.monash.edu.au/~timf/
  26. *
  27. * CIN is a somewhat quirky and ill-defined format. Here are some notes
  28. * for anyone trying to understand the technical details of this format:
  29. *
  30. * The format has no definite file signature. This is problematic for a
  31. * general-purpose media player that wants to automatically detect file
  32. * types. However, a CIN file does start with 5 32-bit numbers that
  33. * specify audio and video parameters. This demuxer gets around the lack
  34. * of file signature by performing sanity checks on those parameters.
  35. * Probabalistically, this is a reasonable solution since the number of
  36. * valid combinations of the 5 parameters is a very small subset of the
  37. * total 160-bit number space.
  38. *
  39. * Refer to the function idcin_probe() for the precise A/V parameters
  40. * that this demuxer allows.
  41. *
  42. * Next, each audio and video frame has a duration of 1/14 sec. If the
  43. * audio sample rate is a multiple of the common frequency 22050 Hz it will
  44. * divide evenly by 14. However, if the sample rate is 11025 Hz:
  45. * 11025 (samples/sec) / 14 (frames/sec) = 787.5 (samples/frame)
  46. * The way the CIN stores audio in this case is by storing 787 sample
  47. * frames in the first audio frame and 788 sample frames in the second
  48. * audio frame. Therefore, the total number of bytes in an audio frame
  49. * is given as:
  50. * audio frame #0: 787 * (bytes/sample) * (# channels) bytes in frame
  51. * audio frame #1: 788 * (bytes/sample) * (# channels) bytes in frame
  52. * audio frame #2: 787 * (bytes/sample) * (# channels) bytes in frame
  53. * audio frame #3: 788 * (bytes/sample) * (# channels) bytes in frame
  54. *
  55. * Finally, not all id CIN creation tools agree on the resolution of the
  56. * color palette, apparently. Some creation tools specify red, green, and
  57. * blue palette components in terms of 6-bit VGA color DAC values which
  58. * range from 0..63. Other tools specify the RGB components as full 8-bit
  59. * values that range from 0..255. Since there are no markers in the file to
  60. * differentiate between the two variants, this demuxer uses the following
  61. * heuristic:
  62. * - load the 768 palette bytes from disk
  63. * - assume that they will need to be shifted left by 2 bits to
  64. * transform them from 6-bit values to 8-bit values
  65. * - scan through all 768 palette bytes
  66. * - if any bytes exceed 63, do not shift the bytes at all before
  67. * transmitting them to the video decoder
  68. */
  69. #include "libavutil/intreadwrite.h"
  70. #include "avformat.h"
  71. #define HUFFMAN_TABLE_SIZE (64 * 1024)
  72. #define IDCIN_FPS 14
  73. typedef struct IdcinDemuxContext {
  74. int video_stream_index;
  75. int audio_stream_index;
  76. int audio_chunk_size1;
  77. int audio_chunk_size2;
  78. /* demux state variables */
  79. int current_audio_chunk;
  80. int next_chunk_is_video;
  81. int audio_present;
  82. int64_t pts;
  83. AVPaletteControl palctrl;
  84. } IdcinDemuxContext;
  85. static int idcin_probe(AVProbeData *p)
  86. {
  87. unsigned int number;
  88. /*
  89. * This is what you could call a "probabilistic" file check: id CIN
  90. * files don't have a definite file signature. In lieu of such a marker,
  91. * perform sanity checks on the 5 32-bit header fields:
  92. * width, height: greater than 0, less than or equal to 1024
  93. * audio sample rate: greater than or equal to 8000, less than or
  94. * equal to 48000, or 0 for no audio
  95. * audio sample width (bytes/sample): 0 for no audio, or 1 or 2
  96. * audio channels: 0 for no audio, or 1 or 2
  97. */
  98. /* check the video width */
  99. number = AV_RL32(&p->buf[0]);
  100. if ((number == 0) || (number > 1024))
  101. return 0;
  102. /* check the video height */
  103. number = AV_RL32(&p->buf[4]);
  104. if ((number == 0) || (number > 1024))
  105. return 0;
  106. /* check the audio sample rate */
  107. number = AV_RL32(&p->buf[8]);
  108. if ((number != 0) && ((number < 8000) | (number > 48000)))
  109. return 0;
  110. /* check the audio bytes/sample */
  111. number = AV_RL32(&p->buf[12]);
  112. if (number > 2)
  113. return 0;
  114. /* check the audio channels */
  115. number = AV_RL32(&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 = 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(ENOMEM);
  138. av_set_pts_info(st, 33, 1, IDCIN_FPS);
  139. idcin->video_stream_index = st->index;
  140. st->codec->codec_type = CODEC_TYPE_VIDEO;
  141. st->codec->codec_id = CODEC_ID_IDCIN;
  142. st->codec->codec_tag = 0; /* no fourcc */
  143. st->codec->width = width;
  144. st->codec->height = height;
  145. /* load up the Huffman tables into extradata */
  146. st->codec->extradata_size = HUFFMAN_TABLE_SIZE;
  147. st->codec->extradata = av_malloc(HUFFMAN_TABLE_SIZE);
  148. if (get_buffer(pb, st->codec->extradata, HUFFMAN_TABLE_SIZE) !=
  149. HUFFMAN_TABLE_SIZE)
  150. return AVERROR(EIO);
  151. /* save a reference in order to transport the palette */
  152. st->codec->palctrl = &idcin->palctrl;
  153. /* if sample rate is 0, assume no audio */
  154. if (sample_rate) {
  155. idcin->audio_present = 1;
  156. st = av_new_stream(s, 0);
  157. if (!st)
  158. return AVERROR(ENOMEM);
  159. av_set_pts_info(st, 33, 1, IDCIN_FPS);
  160. idcin->audio_stream_index = st->index;
  161. st->codec->codec_type = CODEC_TYPE_AUDIO;
  162. st->codec->codec_tag = 1;
  163. st->codec->channels = channels;
  164. st->codec->sample_rate = sample_rate;
  165. st->codec->bits_per_coded_sample = bytes_per_sample * 8;
  166. st->codec->bit_rate = sample_rate * bytes_per_sample * 8 * channels;
  167. st->codec->block_align = bytes_per_sample * channels;
  168. if (bytes_per_sample == 1)
  169. st->codec->codec_id = CODEC_ID_PCM_U8;
  170. else
  171. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  172. if (sample_rate % 14 != 0) {
  173. idcin->audio_chunk_size1 = (sample_rate / 14) *
  174. bytes_per_sample * channels;
  175. idcin->audio_chunk_size2 = (sample_rate / 14 + 1) *
  176. bytes_per_sample * channels;
  177. } else {
  178. idcin->audio_chunk_size1 = idcin->audio_chunk_size2 =
  179. (sample_rate / 14) * bytes_per_sample * channels;
  180. }
  181. idcin->current_audio_chunk = 0;
  182. } else
  183. idcin->audio_present = 1;
  184. idcin->next_chunk_is_video = 1;
  185. idcin->pts = 0;
  186. return 0;
  187. }
  188. static int idcin_read_packet(AVFormatContext *s,
  189. AVPacket *pkt)
  190. {
  191. int ret;
  192. unsigned int command;
  193. unsigned int chunk_size;
  194. IdcinDemuxContext *idcin = s->priv_data;
  195. ByteIOContext *pb = s->pb;
  196. int i;
  197. int palette_scale;
  198. unsigned char r, g, b;
  199. unsigned char palette_buffer[768];
  200. if (url_feof(s->pb))
  201. return AVERROR(EIO);
  202. if (idcin->next_chunk_is_video) {
  203. command = get_le32(pb);
  204. if (command == 2) {
  205. return AVERROR(EIO);
  206. } else if (command == 1) {
  207. /* trigger a palette change */
  208. idcin->palctrl.palette_changed = 1;
  209. if (get_buffer(pb, palette_buffer, 768) != 768)
  210. return AVERROR(EIO);
  211. /* scale the palette as necessary */
  212. palette_scale = 2;
  213. for (i = 0; i < 768; i++)
  214. if (palette_buffer[i] > 63) {
  215. palette_scale = 0;
  216. break;
  217. }
  218. for (i = 0; i < 256; i++) {
  219. r = palette_buffer[i * 3 ] << palette_scale;
  220. g = palette_buffer[i * 3 + 1] << palette_scale;
  221. b = palette_buffer[i * 3 + 2] << palette_scale;
  222. idcin->palctrl.palette[i] = (r << 16) | (g << 8) | (b);
  223. }
  224. }
  225. chunk_size = get_le32(pb);
  226. /* skip the number of decoded bytes (always equal to width * height) */
  227. url_fseek(pb, 4, SEEK_CUR);
  228. chunk_size -= 4;
  229. ret= av_get_packet(pb, pkt, chunk_size);
  230. if (ret != chunk_size)
  231. return AVERROR(EIO);
  232. pkt->stream_index = idcin->video_stream_index;
  233. pkt->pts = idcin->pts;
  234. } else {
  235. /* send out the audio chunk */
  236. if (idcin->current_audio_chunk)
  237. chunk_size = idcin->audio_chunk_size2;
  238. else
  239. chunk_size = idcin->audio_chunk_size1;
  240. ret= av_get_packet(pb, pkt, chunk_size);
  241. if (ret != chunk_size)
  242. return AVERROR(EIO);
  243. pkt->stream_index = idcin->audio_stream_index;
  244. pkt->pts = idcin->pts;
  245. idcin->current_audio_chunk ^= 1;
  246. idcin->pts++;
  247. }
  248. if (idcin->audio_present)
  249. idcin->next_chunk_is_video ^= 1;
  250. return ret;
  251. }
  252. AVInputFormat idcin_demuxer = {
  253. "idcin",
  254. NULL_IF_CONFIG_SMALL("id Cinematic format"),
  255. sizeof(IdcinDemuxContext),
  256. idcin_probe,
  257. idcin_read_header,
  258. idcin_read_packet,
  259. };