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.

311 lines
9.9KB

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