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.

291 lines
9.1KB

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