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.

323 lines
10KB

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