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.4KB

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