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.

293 lines
9.2KB

  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 (AV_RB32(&p->buf[0]) != FILM_TAG)
  60. return 0;
  61. return AVPROBE_SCORE_MAX;
  62. }
  63. static int film_read_header(AVFormatContext *s,
  64. AVFormatParameters *ap)
  65. {
  66. FilmDemuxContext *film = s->priv_data;
  67. ByteIOContext *pb = s->pb;
  68. AVStream *st;
  69. unsigned char scratch[256];
  70. int i;
  71. unsigned int data_offset;
  72. unsigned int audio_frame_counter;
  73. film->sample_table = NULL;
  74. film->stereo_buffer = NULL;
  75. film->stereo_buffer_size = 0;
  76. /* load the main FILM header */
  77. if (get_buffer(pb, scratch, 16) != 16)
  78. return AVERROR(EIO);
  79. data_offset = AV_RB32(&scratch[4]);
  80. film->version = AV_RB32(&scratch[8]);
  81. /* load the FDSC chunk */
  82. if (film->version == 0) {
  83. /* special case for Lemmings .film files; 20-byte header */
  84. if (get_buffer(pb, scratch, 20) != 20)
  85. return AVERROR(EIO);
  86. /* make some assumptions about the audio parameters */
  87. film->audio_type = CODEC_ID_PCM_S8;
  88. film->audio_samplerate = 22050;
  89. film->audio_channels = 1;
  90. film->audio_bits = 8;
  91. } else {
  92. /* normal Saturn .cpk files; 32-byte header */
  93. if (get_buffer(pb, scratch, 32) != 32)
  94. return AVERROR(EIO);
  95. film->audio_samplerate = AV_RB16(&scratch[24]);
  96. film->audio_channels = scratch[21];
  97. film->audio_bits = scratch[22];
  98. if (film->audio_bits == 8)
  99. film->audio_type = CODEC_ID_PCM_S8;
  100. else if (film->audio_bits == 16)
  101. film->audio_type = CODEC_ID_PCM_S16BE;
  102. else
  103. film->audio_type = 0;
  104. }
  105. if (AV_RB32(&scratch[0]) != FDSC_TAG)
  106. return AVERROR_INVALIDDATA;
  107. if (AV_RB32(&scratch[8]) == CVID_TAG) {
  108. film->video_type = CODEC_ID_CINEPAK;
  109. } else
  110. film->video_type = 0;
  111. /* initialize the decoder streams */
  112. if (film->video_type) {
  113. st = av_new_stream(s, 0);
  114. if (!st)
  115. return AVERROR(ENOMEM);
  116. film->video_stream_index = st->index;
  117. st->codec->codec_type = CODEC_TYPE_VIDEO;
  118. st->codec->codec_id = film->video_type;
  119. st->codec->codec_tag = 0; /* no fourcc */
  120. st->codec->width = AV_RB32(&scratch[16]);
  121. st->codec->height = AV_RB32(&scratch[12]);
  122. }
  123. if (film->audio_type) {
  124. st = av_new_stream(s, 0);
  125. if (!st)
  126. return AVERROR(ENOMEM);
  127. film->audio_stream_index = st->index;
  128. st->codec->codec_type = CODEC_TYPE_AUDIO;
  129. st->codec->codec_id = film->audio_type;
  130. st->codec->codec_tag = 1;
  131. st->codec->channels = film->audio_channels;
  132. st->codec->bits_per_sample = film->audio_bits;
  133. st->codec->sample_rate = film->audio_samplerate;
  134. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  135. st->codec->bits_per_sample;
  136. st->codec->block_align = st->codec->channels *
  137. st->codec->bits_per_sample / 8;
  138. }
  139. /* load the sample table */
  140. if (get_buffer(pb, scratch, 16) != 16)
  141. return AVERROR(EIO);
  142. if (AV_RB32(&scratch[0]) != STAB_TAG)
  143. return AVERROR_INVALIDDATA;
  144. film->base_clock = AV_RB32(&scratch[8]);
  145. film->sample_count = AV_RB32(&scratch[12]);
  146. if(film->sample_count >= UINT_MAX / sizeof(film_sample_t))
  147. return -1;
  148. film->sample_table = av_malloc(film->sample_count * sizeof(film_sample_t));
  149. for(i=0; i<s->nb_streams; i++)
  150. av_set_pts_info(s->streams[i], 33, 1, film->base_clock);
  151. audio_frame_counter = 0;
  152. for (i = 0; i < film->sample_count; i++) {
  153. /* load the next sample record and transfer it to an internal struct */
  154. if (get_buffer(pb, scratch, 16) != 16) {
  155. av_free(film->sample_table);
  156. return AVERROR(EIO);
  157. }
  158. film->sample_table[i].sample_offset =
  159. data_offset + AV_RB32(&scratch[0]);
  160. film->sample_table[i].sample_size = AV_RB32(&scratch[4]);
  161. if (AV_RB32(&scratch[8]) == 0xFFFFFFFF) {
  162. film->sample_table[i].stream = film->audio_stream_index;
  163. film->sample_table[i].pts = audio_frame_counter;
  164. film->sample_table[i].pts *= film->base_clock;
  165. film->sample_table[i].pts /= film->audio_samplerate;
  166. audio_frame_counter += (film->sample_table[i].sample_size /
  167. (film->audio_channels * film->audio_bits / 8));
  168. } else {
  169. film->sample_table[i].stream = film->video_stream_index;
  170. film->sample_table[i].pts = AV_RB32(&scratch[8]) & 0x7FFFFFFF;
  171. film->sample_table[i].keyframe = (scratch[8] & 0x80) ? 0 : 1;
  172. }
  173. }
  174. film->current_sample = 0;
  175. return 0;
  176. }
  177. static int film_read_packet(AVFormatContext *s,
  178. AVPacket *pkt)
  179. {
  180. FilmDemuxContext *film = s->priv_data;
  181. ByteIOContext *pb = s->pb;
  182. film_sample_t *sample;
  183. int ret = 0;
  184. int i;
  185. int left, right;
  186. if (film->current_sample >= film->sample_count)
  187. return AVERROR(EIO);
  188. sample = &film->sample_table[film->current_sample];
  189. /* position the stream (will probably be there anyway) */
  190. url_fseek(pb, sample->sample_offset, SEEK_SET);
  191. /* do a special song and dance when loading FILM Cinepak chunks */
  192. if ((sample->stream == film->video_stream_index) &&
  193. (film->video_type == CODEC_ID_CINEPAK)) {
  194. pkt->pos= url_ftell(pb);
  195. if (av_new_packet(pkt, sample->sample_size))
  196. return AVERROR(ENOMEM);
  197. get_buffer(pb, pkt->data, sample->sample_size);
  198. } else if ((sample->stream == film->audio_stream_index) &&
  199. (film->audio_channels == 2)) {
  200. /* stereo PCM needs to be interleaved */
  201. if (av_new_packet(pkt, sample->sample_size))
  202. return AVERROR(ENOMEM);
  203. /* make sure the interleave buffer is large enough */
  204. if (sample->sample_size > film->stereo_buffer_size) {
  205. av_free(film->stereo_buffer);
  206. film->stereo_buffer_size = sample->sample_size;
  207. film->stereo_buffer = av_malloc(film->stereo_buffer_size);
  208. }
  209. pkt->pos= url_ftell(pb);
  210. ret = get_buffer(pb, film->stereo_buffer, sample->sample_size);
  211. if (ret != sample->sample_size)
  212. ret = AVERROR(EIO);
  213. left = 0;
  214. right = sample->sample_size / 2;
  215. for (i = 0; i < sample->sample_size; ) {
  216. if (film->audio_bits == 8) {
  217. pkt->data[i++] = film->stereo_buffer[left++];
  218. pkt->data[i++] = film->stereo_buffer[right++];
  219. } else {
  220. pkt->data[i++] = film->stereo_buffer[left++];
  221. pkt->data[i++] = film->stereo_buffer[left++];
  222. pkt->data[i++] = film->stereo_buffer[right++];
  223. pkt->data[i++] = film->stereo_buffer[right++];
  224. }
  225. }
  226. } else {
  227. ret= av_get_packet(pb, pkt, sample->sample_size);
  228. if (ret != sample->sample_size)
  229. ret = AVERROR(EIO);
  230. }
  231. pkt->stream_index = sample->stream;
  232. pkt->pts = sample->pts;
  233. film->current_sample++;
  234. return ret;
  235. }
  236. static int film_read_close(AVFormatContext *s)
  237. {
  238. FilmDemuxContext *film = s->priv_data;
  239. av_free(film->sample_table);
  240. av_free(film->stereo_buffer);
  241. return 0;
  242. }
  243. AVInputFormat segafilm_demuxer = {
  244. "film_cpk",
  245. "Sega FILM/CPK format",
  246. sizeof(FilmDemuxContext),
  247. film_probe,
  248. film_read_header,
  249. film_read_packet,
  250. film_read_close,
  251. };