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.

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