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.

300 lines
9.6KB

  1. /*
  2. * Sega FILM Format (CPK) Demuxer
  3. * Copyright (c) 2003 The ffmpeg Project
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. #define FILM_TAG MKBETAG('F', 'I', 'L', 'M')
  32. #define FDSC_TAG MKBETAG('F', 'D', 'S', 'C')
  33. #define STAB_TAG MKBETAG('S', 'T', 'A', 'B')
  34. #define CVID_TAG MKBETAG('c', 'v', 'i', 'd')
  35. #define RAW_TAG MKBETAG('r', 'a', 'w', ' ')
  36. typedef struct film_sample {
  37. int stream;
  38. int64_t sample_offset;
  39. unsigned int sample_size;
  40. int64_t pts;
  41. int keyframe;
  42. } film_sample;
  43. typedef struct FilmDemuxContext {
  44. int video_stream_index;
  45. int audio_stream_index;
  46. enum AVCodecID audio_type;
  47. unsigned int audio_samplerate;
  48. unsigned int audio_bits;
  49. unsigned int audio_channels;
  50. enum AVCodecID video_type;
  51. unsigned int sample_count;
  52. film_sample *sample_table;
  53. unsigned int current_sample;
  54. unsigned int base_clock;
  55. unsigned int version;
  56. } FilmDemuxContext;
  57. static int film_probe(AVProbeData *p)
  58. {
  59. if (AV_RB32(&p->buf[0]) != FILM_TAG)
  60. return 0;
  61. return AVPROBE_SCORE_MAX;
  62. }
  63. static int film_read_close(AVFormatContext *s)
  64. {
  65. FilmDemuxContext *film = s->priv_data;
  66. av_freep(&film->sample_table);
  67. return 0;
  68. }
  69. static int film_read_header(AVFormatContext *s)
  70. {
  71. FilmDemuxContext *film = s->priv_data;
  72. AVIOContext *pb = s->pb;
  73. AVStream *st;
  74. unsigned char scratch[256];
  75. int i, ret;
  76. unsigned int data_offset;
  77. unsigned int audio_frame_counter;
  78. film->sample_table = NULL;
  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. if (!film->audio_channels || film->audio_channels > 2) {
  101. av_log(s, AV_LOG_ERROR,
  102. "Invalid number of channels: %d\n", film->audio_channels);
  103. return AVERROR_INVALIDDATA;
  104. }
  105. film->audio_bits = scratch[22];
  106. if (scratch[23] == 2)
  107. film->audio_type = AV_CODEC_ID_ADPCM_ADX;
  108. else if (film->audio_channels > 0) {
  109. if (film->audio_bits == 8)
  110. film->audio_type = AV_CODEC_ID_PCM_S8_PLANAR;
  111. else if (film->audio_bits == 16)
  112. film->audio_type = AV_CODEC_ID_PCM_S16BE_PLANAR;
  113. else
  114. film->audio_type = AV_CODEC_ID_NONE;
  115. } else
  116. film->audio_type = AV_CODEC_ID_NONE;
  117. }
  118. if (AV_RB32(&scratch[0]) != FDSC_TAG)
  119. return AVERROR_INVALIDDATA;
  120. if (AV_RB32(&scratch[8]) == CVID_TAG) {
  121. film->video_type = AV_CODEC_ID_CINEPAK;
  122. } else if (AV_RB32(&scratch[8]) == RAW_TAG) {
  123. film->video_type = AV_CODEC_ID_RAWVIDEO;
  124. } else {
  125. film->video_type = AV_CODEC_ID_NONE;
  126. }
  127. /* initialize the decoder streams */
  128. if (film->video_type) {
  129. st = avformat_new_stream(s, NULL);
  130. if (!st)
  131. return AVERROR(ENOMEM);
  132. film->video_stream_index = st->index;
  133. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  134. st->codec->codec_id = film->video_type;
  135. st->codec->codec_tag = 0; /* no fourcc */
  136. st->codec->width = AV_RB32(&scratch[16]);
  137. st->codec->height = AV_RB32(&scratch[12]);
  138. if (film->video_type == AV_CODEC_ID_RAWVIDEO) {
  139. if (scratch[20] == 24) {
  140. st->codec->pix_fmt = AV_PIX_FMT_RGB24;
  141. } else {
  142. av_log(s, AV_LOG_ERROR, "raw video is using unhandled %dbpp\n", scratch[20]);
  143. return -1;
  144. }
  145. }
  146. }
  147. if (film->audio_type) {
  148. st = avformat_new_stream(s, NULL);
  149. if (!st)
  150. return AVERROR(ENOMEM);
  151. film->audio_stream_index = st->index;
  152. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  153. st->codec->codec_id = film->audio_type;
  154. st->codec->codec_tag = 1;
  155. st->codec->channels = film->audio_channels;
  156. st->codec->sample_rate = film->audio_samplerate;
  157. if (film->audio_type == AV_CODEC_ID_ADPCM_ADX) {
  158. st->codec->bits_per_coded_sample = 18 * 8 / 32;
  159. st->codec->block_align = st->codec->channels * 18;
  160. st->need_parsing = AVSTREAM_PARSE_FULL;
  161. } else {
  162. st->codec->bits_per_coded_sample = film->audio_bits;
  163. st->codec->block_align = st->codec->channels *
  164. st->codec->bits_per_coded_sample / 8;
  165. }
  166. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  167. st->codec->bits_per_coded_sample;
  168. }
  169. /* load the sample table */
  170. if (avio_read(pb, scratch, 16) != 16)
  171. return AVERROR(EIO);
  172. if (AV_RB32(&scratch[0]) != STAB_TAG)
  173. return AVERROR_INVALIDDATA;
  174. film->base_clock = AV_RB32(&scratch[8]);
  175. film->sample_count = AV_RB32(&scratch[12]);
  176. if(film->sample_count >= UINT_MAX / sizeof(film_sample))
  177. return -1;
  178. film->sample_table = av_malloc(film->sample_count * sizeof(film_sample));
  179. if (!film->sample_table)
  180. return AVERROR(ENOMEM);
  181. for (i = 0; i < s->nb_streams; i++) {
  182. st = s->streams[i];
  183. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  184. avpriv_set_pts_info(st, 33, 1, film->base_clock);
  185. else
  186. avpriv_set_pts_info(st, 64, 1, film->audio_samplerate);
  187. }
  188. audio_frame_counter = 0;
  189. for (i = 0; i < film->sample_count; i++) {
  190. /* load the next sample record and transfer it to an internal struct */
  191. if (avio_read(pb, scratch, 16) != 16) {
  192. ret = AVERROR(EIO);
  193. goto fail;
  194. }
  195. film->sample_table[i].sample_offset =
  196. data_offset + AV_RB32(&scratch[0]);
  197. film->sample_table[i].sample_size = AV_RB32(&scratch[4]);
  198. if (film->sample_table[i].sample_size > INT_MAX / 4) {
  199. ret = AVERROR_INVALIDDATA;
  200. goto fail;
  201. }
  202. if (AV_RB32(&scratch[8]) == 0xFFFFFFFF) {
  203. film->sample_table[i].stream = film->audio_stream_index;
  204. film->sample_table[i].pts = audio_frame_counter;
  205. if (film->audio_type == AV_CODEC_ID_ADPCM_ADX)
  206. audio_frame_counter += (film->sample_table[i].sample_size * 32 /
  207. (18 * film->audio_channels));
  208. else if (film->audio_type != AV_CODEC_ID_NONE)
  209. audio_frame_counter += (film->sample_table[i].sample_size /
  210. (film->audio_channels * film->audio_bits / 8));
  211. } else {
  212. film->sample_table[i].stream = film->video_stream_index;
  213. film->sample_table[i].pts = AV_RB32(&scratch[8]) & 0x7FFFFFFF;
  214. film->sample_table[i].keyframe = (scratch[8] & 0x80) ? 0 : 1;
  215. }
  216. }
  217. film->current_sample = 0;
  218. return 0;
  219. fail:
  220. film_read_close(s);
  221. return ret;
  222. }
  223. static int film_read_packet(AVFormatContext *s,
  224. AVPacket *pkt)
  225. {
  226. FilmDemuxContext *film = s->priv_data;
  227. AVIOContext *pb = s->pb;
  228. film_sample *sample;
  229. int ret = 0;
  230. if (film->current_sample >= film->sample_count)
  231. return AVERROR(EIO);
  232. sample = &film->sample_table[film->current_sample];
  233. /* position the stream (will probably be there anyway) */
  234. avio_seek(pb, sample->sample_offset, SEEK_SET);
  235. /* do a special song and dance when loading FILM Cinepak chunks */
  236. if ((sample->stream == film->video_stream_index) &&
  237. (film->video_type == AV_CODEC_ID_CINEPAK)) {
  238. pkt->pos= avio_tell(pb);
  239. if (av_new_packet(pkt, sample->sample_size))
  240. return AVERROR(ENOMEM);
  241. avio_read(pb, pkt->data, sample->sample_size);
  242. } else {
  243. ret= av_get_packet(pb, pkt, sample->sample_size);
  244. if (ret != sample->sample_size)
  245. ret = AVERROR(EIO);
  246. }
  247. pkt->stream_index = sample->stream;
  248. pkt->pts = sample->pts;
  249. film->current_sample++;
  250. return ret;
  251. }
  252. AVInputFormat ff_segafilm_demuxer = {
  253. .name = "film_cpk",
  254. .long_name = NULL_IF_CONFIG_SMALL("Sega FILM / CPK"),
  255. .priv_data_size = sizeof(FilmDemuxContext),
  256. .read_probe = film_probe,
  257. .read_header = film_read_header,
  258. .read_packet = film_read_packet,
  259. .read_close = film_read_close,
  260. };