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.

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