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.

341 lines
11KB

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