idcin.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. #include "internal.h"
  72. #define HUFFMAN_TABLE_SIZE (64 * 1024)
  73. #define IDCIN_FPS 14
  74. typedef struct IdcinDemuxContext {
  75. int video_stream_index;
  76. int audio_stream_index;
  77. int audio_chunk_size1;
  78. int audio_chunk_size2;
  79. /* demux state variables */
  80. int current_audio_chunk;
  81. int next_chunk_is_video;
  82. int audio_present;
  83. int64_t pts;
  84. } IdcinDemuxContext;
  85. static int idcin_probe(AVProbeData *p)
  86. {
  87. unsigned int number, sample_rate;
  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. sample_rate = AV_RL32(&p->buf[8]);
  112. if (sample_rate && (sample_rate < 8000 || sample_rate > 48000))
  113. return 0;
  114. /* check the audio bytes/sample */
  115. number = AV_RL32(&p->buf[12]);
  116. if (number > 2 || sample_rate && !number)
  117. return 0;
  118. /* check the audio channels */
  119. number = AV_RL32(&p->buf[16]);
  120. if (number > 2 || sample_rate && !number)
  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. {
  127. AVIOContext *pb = s->pb;
  128. IdcinDemuxContext *idcin = s->priv_data;
  129. AVStream *st;
  130. unsigned int width, height;
  131. unsigned int sample_rate, bytes_per_sample, channels;
  132. /* get the 5 header parameters */
  133. width = avio_rl32(pb);
  134. height = avio_rl32(pb);
  135. sample_rate = avio_rl32(pb);
  136. bytes_per_sample = avio_rl32(pb);
  137. channels = avio_rl32(pb);
  138. st = avformat_new_stream(s, NULL);
  139. if (!st)
  140. return AVERROR(ENOMEM);
  141. avpriv_set_pts_info(st, 33, 1, IDCIN_FPS);
  142. idcin->video_stream_index = st->index;
  143. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  144. st->codec->codec_id = AV_CODEC_ID_IDCIN;
  145. st->codec->codec_tag = 0; /* no fourcc */
  146. st->codec->width = width;
  147. st->codec->height = height;
  148. /* load up the Huffman tables into extradata */
  149. st->codec->extradata_size = HUFFMAN_TABLE_SIZE;
  150. st->codec->extradata = av_malloc(HUFFMAN_TABLE_SIZE);
  151. if (avio_read(pb, st->codec->extradata, HUFFMAN_TABLE_SIZE) !=
  152. HUFFMAN_TABLE_SIZE)
  153. return AVERROR(EIO);
  154. /* if sample rate is 0, assume no audio */
  155. if (sample_rate) {
  156. idcin->audio_present = 1;
  157. st = avformat_new_stream(s, NULL);
  158. if (!st)
  159. return AVERROR(ENOMEM);
  160. avpriv_set_pts_info(st, 33, 1, IDCIN_FPS);
  161. idcin->audio_stream_index = st->index;
  162. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  163. st->codec->codec_tag = 1;
  164. st->codec->channels = channels;
  165. st->codec->sample_rate = sample_rate;
  166. st->codec->bits_per_coded_sample = bytes_per_sample * 8;
  167. st->codec->bit_rate = sample_rate * bytes_per_sample * 8 * channels;
  168. st->codec->block_align = bytes_per_sample * channels;
  169. if (bytes_per_sample == 1)
  170. st->codec->codec_id = AV_CODEC_ID_PCM_U8;
  171. else
  172. st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
  173. if (sample_rate % 14 != 0) {
  174. idcin->audio_chunk_size1 = (sample_rate / 14) *
  175. bytes_per_sample * channels;
  176. idcin->audio_chunk_size2 = (sample_rate / 14 + 1) *
  177. bytes_per_sample * channels;
  178. } else {
  179. idcin->audio_chunk_size1 = idcin->audio_chunk_size2 =
  180. (sample_rate / 14) * bytes_per_sample * channels;
  181. }
  182. idcin->current_audio_chunk = 0;
  183. } else
  184. idcin->audio_present = 1;
  185. idcin->next_chunk_is_video = 1;
  186. idcin->pts = 0;
  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 = s->priv_data;
  196. AVIOContext *pb = s->pb;
  197. int i;
  198. int palette_scale;
  199. unsigned char r, g, b;
  200. unsigned char palette_buffer[768];
  201. uint32_t palette[256];
  202. if (url_feof(s->pb))
  203. return AVERROR(EIO);
  204. if (idcin->next_chunk_is_video) {
  205. command = avio_rl32(pb);
  206. if (command == 2) {
  207. return AVERROR(EIO);
  208. } else if (command == 1) {
  209. /* trigger a palette change */
  210. if (avio_read(pb, palette_buffer, 768) != 768)
  211. return AVERROR(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. palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
  224. if (palette_scale == 2)
  225. palette[i] |= palette[i] >> 6 & 0x30303;
  226. }
  227. }
  228. chunk_size = avio_rl32(pb);
  229. /* skip the number of decoded bytes (always equal to width * height) */
  230. avio_skip(pb, 4);
  231. chunk_size -= 4;
  232. ret= av_get_packet(pb, pkt, chunk_size);
  233. if (ret < 0)
  234. return ret;
  235. if (command == 1) {
  236. uint8_t *pal;
  237. pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
  238. AVPALETTE_SIZE);
  239. if (!pal)
  240. return AVERROR(ENOMEM);
  241. memcpy(pal, palette, AVPALETTE_SIZE);
  242. }
  243. pkt->stream_index = idcin->video_stream_index;
  244. pkt->pts = idcin->pts;
  245. } else {
  246. /* send out the audio chunk */
  247. if (idcin->current_audio_chunk)
  248. chunk_size = idcin->audio_chunk_size2;
  249. else
  250. chunk_size = idcin->audio_chunk_size1;
  251. ret= av_get_packet(pb, pkt, chunk_size);
  252. if (ret < 0)
  253. return ret;
  254. pkt->stream_index = idcin->audio_stream_index;
  255. pkt->pts = idcin->pts;
  256. idcin->current_audio_chunk ^= 1;
  257. idcin->pts++;
  258. }
  259. if (idcin->audio_present)
  260. idcin->next_chunk_is_video ^= 1;
  261. return ret;
  262. }
  263. AVInputFormat ff_idcin_demuxer = {
  264. .name = "idcin",
  265. .long_name = NULL_IF_CONFIG_SMALL("id Cinematic"),
  266. .priv_data_size = sizeof(IdcinDemuxContext),
  267. .read_probe = idcin_probe,
  268. .read_header = idcin_read_header,
  269. .read_packet = idcin_read_packet,
  270. };