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.

294 lines
9.5KB

  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. } FilmDemuxContext;
  58. static int film_probe(AVProbeData *p)
  59. {
  60. if (AV_RB32(&p->buf[0]) != FILM_TAG)
  61. return 0;
  62. if (AV_RB32(&p->buf[16]) != FDSC_TAG)
  63. return 0;
  64. return AVPROBE_SCORE_MAX;
  65. }
  66. static int film_read_header(AVFormatContext *s)
  67. {
  68. FilmDemuxContext *film = s->priv_data;
  69. AVIOContext *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. /* load the main FILM header */
  77. if (avio_read(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 (avio_read(pb, scratch, 20) != 20)
  85. return AVERROR(EIO);
  86. /* make some assumptions about the audio parameters */
  87. film->audio_type = AV_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 (avio_read(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 (scratch[23] == 2 && film->audio_channels > 0)
  99. film->audio_type = AV_CODEC_ID_ADPCM_ADX;
  100. else if (film->audio_channels > 0) {
  101. if (film->audio_bits == 8)
  102. film->audio_type = AV_CODEC_ID_PCM_S8_PLANAR;
  103. else if (film->audio_bits == 16)
  104. film->audio_type = AV_CODEC_ID_PCM_S16BE_PLANAR;
  105. else
  106. film->audio_type = AV_CODEC_ID_NONE;
  107. } else
  108. film->audio_type = AV_CODEC_ID_NONE;
  109. }
  110. if (AV_RB32(&scratch[0]) != FDSC_TAG)
  111. return AVERROR_INVALIDDATA;
  112. if (AV_RB32(&scratch[8]) == CVID_TAG) {
  113. film->video_type = AV_CODEC_ID_CINEPAK;
  114. } else if (AV_RB32(&scratch[8]) == RAW_TAG) {
  115. film->video_type = AV_CODEC_ID_RAWVIDEO;
  116. } else {
  117. film->video_type = AV_CODEC_ID_NONE;
  118. }
  119. /* initialize the decoder streams */
  120. if (film->video_type) {
  121. st = avformat_new_stream(s, NULL);
  122. if (!st)
  123. return AVERROR(ENOMEM);
  124. film->video_stream_index = st->index;
  125. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  126. st->codec->codec_id = film->video_type;
  127. st->codec->codec_tag = 0; /* no fourcc */
  128. st->codec->width = AV_RB32(&scratch[16]);
  129. st->codec->height = AV_RB32(&scratch[12]);
  130. if (film->video_type == AV_CODEC_ID_RAWVIDEO) {
  131. if (scratch[20] == 24) {
  132. st->codec->pix_fmt = AV_PIX_FMT_RGB24;
  133. } else {
  134. av_log(s, AV_LOG_ERROR, "raw video is using unhandled %dbpp\n", scratch[20]);
  135. return -1;
  136. }
  137. }
  138. }
  139. if (film->audio_type) {
  140. st = avformat_new_stream(s, NULL);
  141. if (!st)
  142. return AVERROR(ENOMEM);
  143. film->audio_stream_index = st->index;
  144. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  145. st->codec->codec_id = film->audio_type;
  146. st->codec->codec_tag = 1;
  147. st->codec->channels = film->audio_channels;
  148. st->codec->sample_rate = film->audio_samplerate;
  149. if (film->audio_type == AV_CODEC_ID_ADPCM_ADX) {
  150. st->codec->bits_per_coded_sample = 18 * 8 / 32;
  151. st->codec->block_align = st->codec->channels * 18;
  152. st->need_parsing = AVSTREAM_PARSE_FULL;
  153. } else {
  154. st->codec->bits_per_coded_sample = film->audio_bits;
  155. st->codec->block_align = st->codec->channels *
  156. st->codec->bits_per_coded_sample / 8;
  157. }
  158. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  159. st->codec->bits_per_coded_sample;
  160. }
  161. /* load the sample table */
  162. if (avio_read(pb, scratch, 16) != 16)
  163. return AVERROR(EIO);
  164. if (AV_RB32(&scratch[0]) != STAB_TAG)
  165. return AVERROR_INVALIDDATA;
  166. film->base_clock = AV_RB32(&scratch[8]);
  167. film->sample_count = AV_RB32(&scratch[12]);
  168. if(film->sample_count >= UINT_MAX / sizeof(film_sample))
  169. return -1;
  170. film->sample_table = av_malloc(film->sample_count * sizeof(film_sample));
  171. if (!film->sample_table)
  172. return AVERROR(ENOMEM);
  173. for (i = 0; i < s->nb_streams; i++) {
  174. st = s->streams[i];
  175. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  176. avpriv_set_pts_info(st, 33, 1, film->base_clock);
  177. else
  178. avpriv_set_pts_info(st, 64, 1, film->audio_samplerate);
  179. }
  180. audio_frame_counter = 0;
  181. for (i = 0; i < film->sample_count; i++) {
  182. /* load the next sample record and transfer it to an internal struct */
  183. if (avio_read(pb, scratch, 16) != 16) {
  184. av_freep(&film->sample_table);
  185. return AVERROR(EIO);
  186. }
  187. film->sample_table[i].sample_offset =
  188. data_offset + AV_RB32(&scratch[0]);
  189. film->sample_table[i].sample_size = AV_RB32(&scratch[4]);
  190. if (film->sample_table[i].sample_size > INT_MAX / 4)
  191. return AVERROR_INVALIDDATA;
  192. if (AV_RB32(&scratch[8]) == 0xFFFFFFFF) {
  193. film->sample_table[i].stream = film->audio_stream_index;
  194. film->sample_table[i].pts = audio_frame_counter;
  195. if (film->audio_type == AV_CODEC_ID_ADPCM_ADX)
  196. audio_frame_counter += (film->sample_table[i].sample_size * 32 /
  197. (18 * film->audio_channels));
  198. else if (film->audio_type != AV_CODEC_ID_NONE)
  199. audio_frame_counter += (film->sample_table[i].sample_size /
  200. (film->audio_channels * film->audio_bits / 8));
  201. } else {
  202. film->sample_table[i].stream = film->video_stream_index;
  203. film->sample_table[i].pts = AV_RB32(&scratch[8]) & 0x7FFFFFFF;
  204. film->sample_table[i].keyframe = (scratch[8] & 0x80) ? 0 : 1;
  205. }
  206. }
  207. film->current_sample = 0;
  208. return 0;
  209. }
  210. static int film_read_packet(AVFormatContext *s,
  211. AVPacket *pkt)
  212. {
  213. FilmDemuxContext *film = s->priv_data;
  214. AVIOContext *pb = s->pb;
  215. film_sample *sample;
  216. int ret = 0;
  217. if (film->current_sample >= film->sample_count)
  218. return AVERROR_EOF;
  219. sample = &film->sample_table[film->current_sample];
  220. /* position the stream (will probably be there anyway) */
  221. avio_seek(pb, sample->sample_offset, SEEK_SET);
  222. /* do a special song and dance when loading FILM Cinepak chunks */
  223. if ((sample->stream == film->video_stream_index) &&
  224. (film->video_type == AV_CODEC_ID_CINEPAK)) {
  225. pkt->pos= avio_tell(pb);
  226. if (av_new_packet(pkt, sample->sample_size))
  227. return AVERROR(ENOMEM);
  228. avio_read(pb, pkt->data, sample->sample_size);
  229. } else {
  230. ret= av_get_packet(pb, pkt, sample->sample_size);
  231. if (ret != sample->sample_size)
  232. ret = AVERROR(EIO);
  233. }
  234. pkt->stream_index = sample->stream;
  235. pkt->pts = sample->pts;
  236. film->current_sample++;
  237. return ret;
  238. }
  239. static int film_read_close(AVFormatContext *s)
  240. {
  241. FilmDemuxContext *film = s->priv_data;
  242. av_freep(&film->sample_table);
  243. return 0;
  244. }
  245. AVInputFormat ff_segafilm_demuxer = {
  246. .name = "film_cpk",
  247. .long_name = NULL_IF_CONFIG_SMALL("Sega FILM / CPK"),
  248. .priv_data_size = sizeof(FilmDemuxContext),
  249. .read_probe = film_probe,
  250. .read_header = film_read_header,
  251. .read_packet = film_read_packet,
  252. .read_close = film_read_close,
  253. };