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.

324 lines
10KB

  1. /*
  2. * Sega FILM Format (CPK) 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 segafilm.c
  21. * Sega FILM (.cpk) file demuxer
  22. * by Mike Melanson (melanson@pcisys.net)
  23. * For more information regarding the Sega FILM file format, visit:
  24. * http://www.pcisys.net/~melanson/codecs/
  25. */
  26. #include "avformat.h"
  27. #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
  28. #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
  29. (((uint8_t*)(x))[1] << 16) | \
  30. (((uint8_t*)(x))[2] << 8) | \
  31. ((uint8_t*)(x))[3])
  32. #define FOURCC_TAG( ch0, ch1, ch2, ch3 ) \
  33. ( (long)(unsigned char)(ch3) | \
  34. ( (long)(unsigned char)(ch2) << 8 ) | \
  35. ( (long)(unsigned char)(ch1) << 16 ) | \
  36. ( (long)(unsigned char)(ch0) << 24 ) )
  37. #define FILM_TAG FOURCC_TAG('F', 'I', 'L', 'M')
  38. #define FDSC_TAG FOURCC_TAG('F', 'D', 'S', 'C')
  39. #define STAB_TAG FOURCC_TAG('S', 'T', 'A', 'B')
  40. #define CVID_TAG FOURCC_TAG('c', 'v', 'i', 'd')
  41. typedef struct {
  42. int stream;
  43. offset_t sample_offset;
  44. unsigned int sample_size;
  45. int64_t pts;
  46. int keyframe;
  47. } film_sample_t;
  48. typedef struct FilmDemuxContext {
  49. int video_stream_index;
  50. int audio_stream_index;
  51. unsigned int audio_type;
  52. unsigned int audio_samplerate;
  53. unsigned int audio_bits;
  54. unsigned int audio_channels;
  55. unsigned int video_type;
  56. unsigned int sample_count;
  57. film_sample_t *sample_table;
  58. unsigned int current_sample;
  59. unsigned int base_clock;
  60. unsigned int version;
  61. int cvid_extra_bytes; /* the number of bytes thrown into the Cinepak
  62. * chunk header to throw off decoders */
  63. /* buffer used for interleaving stereo PCM data */
  64. unsigned char *stereo_buffer;
  65. int stereo_buffer_size;
  66. } FilmDemuxContext;
  67. static int film_probe(AVProbeData *p)
  68. {
  69. if (p->buf_size < 4)
  70. return 0;
  71. if (BE_32(&p->buf[0]) != FILM_TAG)
  72. return 0;
  73. return AVPROBE_SCORE_MAX;
  74. }
  75. static int film_read_header(AVFormatContext *s,
  76. AVFormatParameters *ap)
  77. {
  78. FilmDemuxContext *film = (FilmDemuxContext *)s->priv_data;
  79. ByteIOContext *pb = &s->pb;
  80. AVStream *st;
  81. unsigned char scratch[256];
  82. int i;
  83. unsigned int data_offset;
  84. unsigned int audio_frame_counter;
  85. film->sample_table = NULL;
  86. film->stereo_buffer = NULL;
  87. film->stereo_buffer_size = 0;
  88. /* load the main FILM header */
  89. if (get_buffer(pb, scratch, 16) != 16)
  90. return -EIO;
  91. data_offset = BE_32(&scratch[4]);
  92. film->version = BE_32(&scratch[8]);
  93. /* load the FDSC chunk */
  94. if (film->version == 0) {
  95. /* special case for Lemmings .film files; 20-byte header */
  96. if (get_buffer(pb, scratch, 20) != 20)
  97. return -EIO;
  98. /* make some assumptions about the audio parameters */
  99. film->audio_type = CODEC_ID_PCM_S8;
  100. film->audio_samplerate = 22050;
  101. film->audio_channels = 1;
  102. film->audio_bits = 8;
  103. } else {
  104. /* normal Saturn .cpk files; 32-byte header */
  105. if (get_buffer(pb, scratch, 32) != 32)
  106. return -EIO;
  107. film->audio_samplerate = BE_16(&scratch[24]);;
  108. film->audio_channels = scratch[21];
  109. film->audio_bits = scratch[22];
  110. if (film->audio_bits == 8)
  111. film->audio_type = CODEC_ID_PCM_S8;
  112. else if (film->audio_bits == 16)
  113. film->audio_type = CODEC_ID_PCM_S16BE;
  114. else
  115. film->audio_type = 0;
  116. }
  117. if (BE_32(&scratch[0]) != FDSC_TAG)
  118. return AVERROR_INVALIDDATA;
  119. film->cvid_extra_bytes = 0;
  120. if (BE_32(&scratch[8]) == CVID_TAG) {
  121. film->video_type = CODEC_ID_CINEPAK;
  122. if (film->version)
  123. film->cvid_extra_bytes = 2;
  124. else
  125. film->cvid_extra_bytes = 6; /* Lemmings 3DO case */
  126. } else
  127. film->video_type = 0;
  128. /* initialize the decoder streams */
  129. if (film->video_type) {
  130. st = av_new_stream(s, 0);
  131. if (!st)
  132. return AVERROR_NOMEM;
  133. film->video_stream_index = st->index;
  134. st->codec.codec_type = CODEC_TYPE_VIDEO;
  135. st->codec.codec_id = film->video_type;
  136. st->codec.codec_tag = 0; /* no fourcc */
  137. st->codec.width = BE_32(&scratch[16]);
  138. st->codec.height = BE_32(&scratch[12]);
  139. }
  140. if (film->audio_type) {
  141. st = av_new_stream(s, 0);
  142. if (!st)
  143. return AVERROR_NOMEM;
  144. film->audio_stream_index = st->index;
  145. st->codec.codec_type = CODEC_TYPE_AUDIO;
  146. st->codec.codec_id = film->audio_type;
  147. st->codec.codec_tag = 1;
  148. st->codec.channels = film->audio_channels;
  149. st->codec.bits_per_sample = film->audio_bits;
  150. st->codec.sample_rate = film->audio_samplerate;
  151. st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
  152. st->codec.bits_per_sample;
  153. st->codec.block_align = st->codec.channels *
  154. st->codec.bits_per_sample / 8;
  155. }
  156. /* load the sample table */
  157. if (get_buffer(pb, scratch, 16) != 16)
  158. return -EIO;
  159. if (BE_32(&scratch[0]) != STAB_TAG)
  160. return AVERROR_INVALIDDATA;
  161. film->base_clock = BE_32(&scratch[8]);
  162. film->sample_count = BE_32(&scratch[12]);
  163. film->sample_table = av_malloc(film->sample_count * sizeof(film_sample_t));
  164. audio_frame_counter = 0;
  165. for (i = 0; i < film->sample_count; i++) {
  166. /* load the next sample record and transfer it to an internal struct */
  167. if (get_buffer(pb, scratch, 16) != 16) {
  168. av_free(film->sample_table);
  169. return -EIO;
  170. }
  171. film->sample_table[i].sample_offset =
  172. data_offset + BE_32(&scratch[0]);
  173. film->sample_table[i].sample_size = BE_32(&scratch[4]);
  174. if (BE_32(&scratch[8]) == 0xFFFFFFFF) {
  175. film->sample_table[i].stream = film->audio_stream_index;
  176. film->sample_table[i].pts = audio_frame_counter;
  177. film->sample_table[i].pts *= film->base_clock;
  178. film->sample_table[i].pts /= film->audio_samplerate;
  179. audio_frame_counter += (film->sample_table[i].sample_size /
  180. (film->audio_channels * film->audio_bits / 8));
  181. } else {
  182. film->sample_table[i].stream = film->video_stream_index;
  183. film->sample_table[i].pts = BE_32(&scratch[8]) & 0x7FFFFFFF;
  184. film->sample_table[i].keyframe = (scratch[8] & 0x80) ? 0 : 1;
  185. }
  186. }
  187. film->current_sample = 0;
  188. /* set the pts reference to match the tick rate of the file */
  189. s->pts_num = 1;
  190. s->pts_den = film->base_clock;
  191. return 0;
  192. }
  193. static int film_read_packet(AVFormatContext *s,
  194. AVPacket *pkt)
  195. {
  196. FilmDemuxContext *film = (FilmDemuxContext *)s->priv_data;
  197. ByteIOContext *pb = &s->pb;
  198. film_sample_t *sample;
  199. int ret = 0;
  200. int i;
  201. int left, right;
  202. if (film->current_sample >= film->sample_count)
  203. return -EIO;
  204. sample = &film->sample_table[film->current_sample];
  205. /* position the stream (will probably be there anyway) */
  206. url_fseek(pb, sample->sample_offset, SEEK_SET);
  207. /* do a special song and dance when loading FILM Cinepak chunks */
  208. if ((sample->stream == film->video_stream_index) &&
  209. (film->video_type == CODEC_ID_CINEPAK)) {
  210. if (av_new_packet(pkt, sample->sample_size - film->cvid_extra_bytes))
  211. return AVERROR_NOMEM;
  212. ret = get_buffer(pb, pkt->data, 10);
  213. /* skip the non-spec CVID bytes */
  214. url_fseek(pb, film->cvid_extra_bytes, SEEK_CUR);
  215. ret += get_buffer(pb, pkt->data + 10,
  216. sample->sample_size - 10 - film->cvid_extra_bytes);
  217. if (ret != sample->sample_size - film->cvid_extra_bytes)
  218. ret = -EIO;
  219. } else if ((sample->stream == film->audio_stream_index) &&
  220. (film->audio_channels == 2)) {
  221. /* stereo PCM needs to be interleaved */
  222. if (av_new_packet(pkt, sample->sample_size))
  223. return AVERROR_NOMEM;
  224. /* make sure the interleave buffer is large enough */
  225. if (sample->sample_size > film->stereo_buffer_size) {
  226. av_free(film->stereo_buffer);
  227. film->stereo_buffer_size = sample->sample_size;
  228. film->stereo_buffer = av_malloc(film->stereo_buffer_size);
  229. }
  230. ret = get_buffer(pb, film->stereo_buffer, sample->sample_size);
  231. if (ret != sample->sample_size)
  232. ret = -EIO;
  233. left = 0;
  234. right = sample->sample_size / 2;
  235. for (i = 0; i < sample->sample_size; ) {
  236. if (film->audio_bits == 8) {
  237. pkt->data[i++] = film->stereo_buffer[left++];
  238. pkt->data[i++] = film->stereo_buffer[right++];
  239. } else {
  240. pkt->data[i++] = film->stereo_buffer[left++];
  241. pkt->data[i++] = film->stereo_buffer[left++];
  242. pkt->data[i++] = film->stereo_buffer[right++];
  243. pkt->data[i++] = film->stereo_buffer[right++];
  244. }
  245. }
  246. } else {
  247. if (av_new_packet(pkt, sample->sample_size))
  248. return AVERROR_NOMEM;
  249. ret = get_buffer(pb, pkt->data, sample->sample_size);
  250. if (ret != sample->sample_size)
  251. ret = -EIO;
  252. }
  253. pkt->stream_index = sample->stream;
  254. pkt->pts = sample->pts;
  255. film->current_sample++;
  256. return ret;
  257. }
  258. static int film_read_close(AVFormatContext *s)
  259. {
  260. FilmDemuxContext *film = (FilmDemuxContext *)s->priv_data;
  261. av_free(film->sample_table);
  262. av_free(film->stereo_buffer);
  263. return 0;
  264. }
  265. static AVInputFormat film_iformat = {
  266. "film_cpk",
  267. "Sega FILM/CPK format",
  268. sizeof(FilmDemuxContext),
  269. film_probe,
  270. film_read_header,
  271. film_read_packet,
  272. film_read_close,
  273. };
  274. int film_init(void)
  275. {
  276. av_register_input_format(&film_iformat);
  277. return 0;
  278. }