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.

312 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. audio_frame_counter = 0;
  155. for (i = 0; i < film->sample_count; i++) {
  156. /* load the next sample record and transfer it to an internal struct */
  157. if (get_buffer(pb, scratch, 16) != 16) {
  158. av_free(film->sample_table);
  159. return -EIO;
  160. }
  161. film->sample_table[i].sample_offset =
  162. data_offset + BE_32(&scratch[0]);
  163. film->sample_table[i].sample_size = BE_32(&scratch[4]);
  164. if (BE_32(&scratch[8]) == 0xFFFFFFFF) {
  165. film->sample_table[i].stream = film->audio_stream_index;
  166. film->sample_table[i].pts = audio_frame_counter;
  167. film->sample_table[i].pts *= film->base_clock;
  168. film->sample_table[i].pts /= film->audio_samplerate;
  169. audio_frame_counter += (film->sample_table[i].sample_size /
  170. (film->audio_channels * film->audio_bits / 8));
  171. } else {
  172. film->sample_table[i].stream = film->video_stream_index;
  173. film->sample_table[i].pts = BE_32(&scratch[8]) & 0x7FFFFFFF;
  174. film->sample_table[i].keyframe = (scratch[8] & 0x80) ? 0 : 1;
  175. }
  176. }
  177. film->current_sample = 0;
  178. /* set the pts reference to match the tick rate of the file */
  179. s->pts_num = 1;
  180. s->pts_den = film->base_clock;
  181. return 0;
  182. }
  183. static int film_read_packet(AVFormatContext *s,
  184. AVPacket *pkt)
  185. {
  186. FilmDemuxContext *film = (FilmDemuxContext *)s->priv_data;
  187. ByteIOContext *pb = &s->pb;
  188. film_sample_t *sample;
  189. int ret = 0;
  190. int i;
  191. int left, right;
  192. if (film->current_sample >= film->sample_count)
  193. return -EIO;
  194. sample = &film->sample_table[film->current_sample];
  195. /* position the stream (will probably be there anyway) */
  196. url_fseek(pb, sample->sample_offset, SEEK_SET);
  197. /* do a special song and dance when loading FILM Cinepak chunks */
  198. if ((sample->stream == film->video_stream_index) &&
  199. (film->video_type == CODEC_ID_CINEPAK)) {
  200. if (av_new_packet(pkt, sample->sample_size - film->cvid_extra_bytes))
  201. return AVERROR_NOMEM;
  202. ret = get_buffer(pb, pkt->data, 10);
  203. /* skip the non-spec CVID bytes */
  204. url_fseek(pb, film->cvid_extra_bytes, SEEK_CUR);
  205. ret += get_buffer(pb, pkt->data + 10,
  206. sample->sample_size - 10 - film->cvid_extra_bytes);
  207. if (ret != sample->sample_size - film->cvid_extra_bytes)
  208. ret = -EIO;
  209. } else if ((sample->stream == film->audio_stream_index) &&
  210. (film->audio_channels == 2)) {
  211. /* stereo PCM needs to be interleaved */
  212. if (av_new_packet(pkt, sample->sample_size))
  213. return AVERROR_NOMEM;
  214. /* make sure the interleave buffer is large enough */
  215. if (sample->sample_size > film->stereo_buffer_size) {
  216. av_free(film->stereo_buffer);
  217. film->stereo_buffer_size = sample->sample_size;
  218. film->stereo_buffer = av_malloc(film->stereo_buffer_size);
  219. }
  220. ret = get_buffer(pb, film->stereo_buffer, sample->sample_size);
  221. if (ret != sample->sample_size)
  222. ret = -EIO;
  223. left = 0;
  224. right = sample->sample_size / 2;
  225. for (i = 0; i < sample->sample_size; ) {
  226. if (film->audio_bits == 8) {
  227. pkt->data[i++] = film->stereo_buffer[left++];
  228. pkt->data[i++] = film->stereo_buffer[right++];
  229. } else {
  230. pkt->data[i++] = film->stereo_buffer[left++];
  231. pkt->data[i++] = film->stereo_buffer[left++];
  232. pkt->data[i++] = film->stereo_buffer[right++];
  233. pkt->data[i++] = film->stereo_buffer[right++];
  234. }
  235. }
  236. } else {
  237. if (av_new_packet(pkt, sample->sample_size))
  238. return AVERROR_NOMEM;
  239. ret = get_buffer(pb, pkt->data, sample->sample_size);
  240. if (ret != sample->sample_size)
  241. ret = -EIO;
  242. }
  243. pkt->stream_index = sample->stream;
  244. pkt->pts = sample->pts;
  245. film->current_sample++;
  246. return ret;
  247. }
  248. static int film_read_close(AVFormatContext *s)
  249. {
  250. FilmDemuxContext *film = (FilmDemuxContext *)s->priv_data;
  251. av_free(film->sample_table);
  252. av_free(film->stereo_buffer);
  253. return 0;
  254. }
  255. static AVInputFormat film_iformat = {
  256. "film_cpk",
  257. "Sega FILM/CPK format",
  258. sizeof(FilmDemuxContext),
  259. film_probe,
  260. film_read_header,
  261. film_read_packet,
  262. film_read_close,
  263. };
  264. int film_init(void)
  265. {
  266. av_register_input_format(&film_iformat);
  267. return 0;
  268. }