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.

296 lines
9.3KB

  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. /* 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 (AV_RB32(&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 = AV_RB32(&scratch[4]);
  82. film->version = AV_RB32(&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 = AV_RB16(&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 (AV_RB32(&scratch[0]) != FDSC_TAG)
  108. return AVERROR_INVALIDDATA;
  109. if (AV_RB32(&scratch[8]) == CVID_TAG) {
  110. film->video_type = CODEC_ID_CINEPAK;
  111. } else
  112. film->video_type = 0;
  113. /* initialize the decoder streams */
  114. if (film->video_type) {
  115. st = av_new_stream(s, 0);
  116. if (!st)
  117. return AVERROR_NOMEM;
  118. film->video_stream_index = st->index;
  119. st->codec->codec_type = CODEC_TYPE_VIDEO;
  120. st->codec->codec_id = film->video_type;
  121. st->codec->codec_tag = 0; /* no fourcc */
  122. st->codec->width = AV_RB32(&scratch[16]);
  123. st->codec->height = AV_RB32(&scratch[12]);
  124. }
  125. if (film->audio_type) {
  126. st = av_new_stream(s, 0);
  127. if (!st)
  128. return AVERROR_NOMEM;
  129. film->audio_stream_index = st->index;
  130. st->codec->codec_type = CODEC_TYPE_AUDIO;
  131. st->codec->codec_id = film->audio_type;
  132. st->codec->codec_tag = 1;
  133. st->codec->channels = film->audio_channels;
  134. st->codec->bits_per_sample = film->audio_bits;
  135. st->codec->sample_rate = film->audio_samplerate;
  136. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  137. st->codec->bits_per_sample;
  138. st->codec->block_align = st->codec->channels *
  139. st->codec->bits_per_sample / 8;
  140. }
  141. /* load the sample table */
  142. if (get_buffer(pb, scratch, 16) != 16)
  143. return AVERROR_IO;
  144. if (AV_RB32(&scratch[0]) != STAB_TAG)
  145. return AVERROR_INVALIDDATA;
  146. film->base_clock = AV_RB32(&scratch[8]);
  147. film->sample_count = AV_RB32(&scratch[12]);
  148. if(film->sample_count >= UINT_MAX / sizeof(film_sample_t))
  149. return -1;
  150. film->sample_table = av_malloc(film->sample_count * sizeof(film_sample_t));
  151. for(i=0; i<s->nb_streams; i++)
  152. av_set_pts_info(s->streams[i], 33, 1, film->base_clock);
  153. audio_frame_counter = 0;
  154. for (i = 0; i < film->sample_count; i++) {
  155. /* load the next sample record and transfer it to an internal struct */
  156. if (get_buffer(pb, scratch, 16) != 16) {
  157. av_free(film->sample_table);
  158. return AVERROR_IO;
  159. }
  160. film->sample_table[i].sample_offset =
  161. data_offset + AV_RB32(&scratch[0]);
  162. film->sample_table[i].sample_size = AV_RB32(&scratch[4]);
  163. if (AV_RB32(&scratch[8]) == 0xFFFFFFFF) {
  164. film->sample_table[i].stream = film->audio_stream_index;
  165. film->sample_table[i].pts = audio_frame_counter;
  166. film->sample_table[i].pts *= film->base_clock;
  167. film->sample_table[i].pts /= film->audio_samplerate;
  168. audio_frame_counter += (film->sample_table[i].sample_size /
  169. (film->audio_channels * film->audio_bits / 8));
  170. } else {
  171. film->sample_table[i].stream = film->video_stream_index;
  172. film->sample_table[i].pts = AV_RB32(&scratch[8]) & 0x7FFFFFFF;
  173. film->sample_table[i].keyframe = (scratch[8] & 0x80) ? 0 : 1;
  174. }
  175. }
  176. film->current_sample = 0;
  177. return 0;
  178. }
  179. static int film_read_packet(AVFormatContext *s,
  180. AVPacket *pkt)
  181. {
  182. FilmDemuxContext *film = (FilmDemuxContext *)s->priv_data;
  183. ByteIOContext *pb = &s->pb;
  184. film_sample_t *sample;
  185. int ret = 0;
  186. int i;
  187. int left, right;
  188. if (film->current_sample >= film->sample_count)
  189. return AVERROR_IO;
  190. sample = &film->sample_table[film->current_sample];
  191. /* position the stream (will probably be there anyway) */
  192. url_fseek(pb, sample->sample_offset, SEEK_SET);
  193. /* do a special song and dance when loading FILM Cinepak chunks */
  194. if ((sample->stream == film->video_stream_index) &&
  195. (film->video_type == CODEC_ID_CINEPAK)) {
  196. pkt->pos= url_ftell(pb);
  197. if (av_new_packet(pkt, sample->sample_size))
  198. return AVERROR_NOMEM;
  199. get_buffer(pb, pkt->data, sample->sample_size);
  200. } else if ((sample->stream == film->audio_stream_index) &&
  201. (film->audio_channels == 2)) {
  202. /* stereo PCM needs to be interleaved */
  203. if (av_new_packet(pkt, sample->sample_size))
  204. return AVERROR_NOMEM;
  205. /* make sure the interleave buffer is large enough */
  206. if (sample->sample_size > film->stereo_buffer_size) {
  207. av_free(film->stereo_buffer);
  208. film->stereo_buffer_size = sample->sample_size;
  209. film->stereo_buffer = av_malloc(film->stereo_buffer_size);
  210. }
  211. pkt->pos= url_ftell(pb);
  212. ret = get_buffer(pb, film->stereo_buffer, sample->sample_size);
  213. if (ret != sample->sample_size)
  214. ret = AVERROR_IO;
  215. left = 0;
  216. right = sample->sample_size / 2;
  217. for (i = 0; i < sample->sample_size; ) {
  218. if (film->audio_bits == 8) {
  219. pkt->data[i++] = film->stereo_buffer[left++];
  220. pkt->data[i++] = film->stereo_buffer[right++];
  221. } else {
  222. pkt->data[i++] = film->stereo_buffer[left++];
  223. pkt->data[i++] = film->stereo_buffer[left++];
  224. pkt->data[i++] = film->stereo_buffer[right++];
  225. pkt->data[i++] = film->stereo_buffer[right++];
  226. }
  227. }
  228. } else {
  229. ret= av_get_packet(pb, pkt, sample->sample_size);
  230. if (ret != sample->sample_size)
  231. ret = AVERROR_IO;
  232. }
  233. pkt->stream_index = sample->stream;
  234. pkt->pts = sample->pts;
  235. film->current_sample++;
  236. return ret;
  237. }
  238. static int film_read_close(AVFormatContext *s)
  239. {
  240. FilmDemuxContext *film = (FilmDemuxContext *)s->priv_data;
  241. av_free(film->sample_table);
  242. av_free(film->stereo_buffer);
  243. return 0;
  244. }
  245. AVInputFormat segafilm_demuxer = {
  246. "film_cpk",
  247. "Sega FILM/CPK format",
  248. sizeof(FilmDemuxContext),
  249. film_probe,
  250. film_read_header,
  251. film_read_packet,
  252. film_read_close,
  253. };