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
10.0KB

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