segafilm.c 9.3 KB

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