idcin.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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
  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 we have enough data to do all checks, otherwise the
  99. 0-padding may cause a wrong recognition */
  100. if (p->buf_size < 20)
  101. return 0;
  102. /* check the video width */
  103. number = AV_RL32(&p->buf[0]);
  104. if ((number == 0) || (number > 1024))
  105. return 0;
  106. /* check the video height */
  107. number = AV_RL32(&p->buf[4]);
  108. if ((number == 0) || (number > 1024))
  109. return 0;
  110. /* check the audio sample rate */
  111. number = AV_RL32(&p->buf[8]);
  112. if ((number != 0) && ((number < 8000) | (number > 48000)))
  113. return 0;
  114. /* check the audio bytes/sample */
  115. number = AV_RL32(&p->buf[12]);
  116. if (number > 2)
  117. return 0;
  118. /* check the audio channels */
  119. number = AV_RL32(&p->buf[16]);
  120. if (number > 2)
  121. return 0;
  122. /* return half certainly since this check is a bit sketchy */
  123. return AVPROBE_SCORE_MAX / 2;
  124. }
  125. static int idcin_read_header(AVFormatContext *s,
  126. AVFormatParameters *ap)
  127. {
  128. AVIOContext *pb = s->pb;
  129. IdcinDemuxContext *idcin = s->priv_data;
  130. AVStream *st;
  131. unsigned int width, height;
  132. unsigned int sample_rate, bytes_per_sample, channels;
  133. /* get the 5 header parameters */
  134. width = avio_rl32(pb);
  135. height = avio_rl32(pb);
  136. sample_rate = avio_rl32(pb);
  137. bytes_per_sample = avio_rl32(pb);
  138. channels = avio_rl32(pb);
  139. st = av_new_stream(s, 0);
  140. if (!st)
  141. return AVERROR(ENOMEM);
  142. av_set_pts_info(st, 33, 1, IDCIN_FPS);
  143. idcin->video_stream_index = st->index;
  144. st->codec->codec_type = AVMEDIA_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 (avio_read(pb, st->codec->extradata, HUFFMAN_TABLE_SIZE) !=
  153. HUFFMAN_TABLE_SIZE)
  154. return AVERROR(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(ENOMEM);
  163. av_set_pts_info(st, 33, 1, IDCIN_FPS);
  164. idcin->audio_stream_index = st->index;
  165. st->codec->codec_type = AVMEDIA_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_coded_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. return 0;
  191. }
  192. static int idcin_read_packet(AVFormatContext *s,
  193. AVPacket *pkt)
  194. {
  195. int ret;
  196. unsigned int command;
  197. unsigned int chunk_size;
  198. IdcinDemuxContext *idcin = s->priv_data;
  199. AVIOContext *pb = s->pb;
  200. int i;
  201. int palette_scale;
  202. unsigned char r, g, b;
  203. unsigned char palette_buffer[768];
  204. if (url_feof(s->pb))
  205. return AVERROR(EIO);
  206. if (idcin->next_chunk_is_video) {
  207. command = avio_rl32(pb);
  208. if (command == 2) {
  209. return AVERROR(EIO);
  210. } else if (command == 1) {
  211. /* trigger a palette change */
  212. idcin->palctrl.palette_changed = 1;
  213. if (avio_read(pb, palette_buffer, 768) != 768)
  214. return AVERROR(EIO);
  215. /* scale the palette as necessary */
  216. palette_scale = 2;
  217. for (i = 0; i < 768; i++)
  218. if (palette_buffer[i] > 63) {
  219. palette_scale = 0;
  220. break;
  221. }
  222. for (i = 0; i < 256; i++) {
  223. r = palette_buffer[i * 3 ] << palette_scale;
  224. g = palette_buffer[i * 3 + 1] << palette_scale;
  225. b = palette_buffer[i * 3 + 2] << palette_scale;
  226. idcin->palctrl.palette[i] = (r << 16) | (g << 8) | (b);
  227. }
  228. }
  229. chunk_size = avio_rl32(pb);
  230. /* skip the number of decoded bytes (always equal to width * height) */
  231. avio_skip(pb, 4);
  232. chunk_size -= 4;
  233. ret= av_get_packet(pb, pkt, chunk_size);
  234. if (ret < 0)
  235. return ret;
  236. pkt->stream_index = idcin->video_stream_index;
  237. pkt->pts = idcin->pts;
  238. } else {
  239. /* send out the audio chunk */
  240. if (idcin->current_audio_chunk)
  241. chunk_size = idcin->audio_chunk_size2;
  242. else
  243. chunk_size = idcin->audio_chunk_size1;
  244. ret= av_get_packet(pb, pkt, chunk_size);
  245. if (ret < 0)
  246. return ret;
  247. pkt->stream_index = idcin->audio_stream_index;
  248. pkt->pts = idcin->pts;
  249. idcin->current_audio_chunk ^= 1;
  250. idcin->pts++;
  251. }
  252. if (idcin->audio_present)
  253. idcin->next_chunk_is_video ^= 1;
  254. return ret;
  255. }
  256. AVInputFormat ff_idcin_demuxer = {
  257. .name = "idcin",
  258. .long_name = NULL_IF_CONFIG_SMALL("id Cinematic format"),
  259. .priv_data_size = sizeof(IdcinDemuxContext),
  260. .read_probe = idcin_probe,
  261. .read_header = idcin_read_header,
  262. .read_packet = idcin_read_packet,
  263. };