segafilm.c 11 KB

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