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.

338 lines
10KB

  1. /*
  2. * Sony Playstation (PSX) STR File 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 psxstr.c
  23. * PSX STR file demuxer
  24. * by Mike Melanson (melanson@pcisys.net)
  25. * This module handles streams that have been ripped from Sony Playstation
  26. * CD games. This demuxer can handle either raw STR files (which are just
  27. * concatenations of raw compact disc sectors) or STR files with 0x2C-byte
  28. * RIFF headers, followed by CD sectors.
  29. */
  30. #include "avformat.h"
  31. //#define PRINTSTUFF
  32. #define RIFF_TAG MKTAG('R', 'I', 'F', 'F')
  33. #define CDXA_TAG MKTAG('C', 'D', 'X', 'A')
  34. #define RAW_CD_SECTOR_SIZE 2352
  35. #define RAW_CD_SECTOR_DATA_SIZE 2304
  36. #define VIDEO_DATA_CHUNK_SIZE 0x7E0
  37. #define VIDEO_DATA_HEADER_SIZE 0x38
  38. #define RIFF_HEADER_SIZE 0x2C
  39. #define CDXA_TYPE_MASK 0x0E
  40. #define CDXA_TYPE_DATA 0x08
  41. #define CDXA_TYPE_AUDIO 0x04
  42. #define CDXA_TYPE_VIDEO 0x02
  43. #define STR_MAGIC (0x80010160)
  44. typedef struct StrChannel {
  45. /* video parameters */
  46. int video_stream_index;
  47. /* audio parameters */
  48. int audio_stream_index;
  49. } StrChannel;
  50. typedef struct StrDemuxContext {
  51. /* a STR file can contain up to 32 channels of data */
  52. StrChannel channels[32];
  53. /* only decode the first audio and video channels encountered */
  54. int video_channel;
  55. int audio_channel;
  56. int64_t pts;
  57. AVPacket tmp_pkt;
  58. } StrDemuxContext;
  59. static const char sync_header[12] = {0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00};
  60. static int str_probe(AVProbeData *p)
  61. {
  62. int start;
  63. /* need at least 0x38 bytes to validate */
  64. if (p->buf_size < 0x38)
  65. return 0;
  66. if ((AV_RL32(&p->buf[0]) == RIFF_TAG) &&
  67. (AV_RL32(&p->buf[8]) == CDXA_TAG)) {
  68. /* RIFF header seen; skip 0x2C bytes */
  69. start = RIFF_HEADER_SIZE;
  70. } else
  71. start = 0;
  72. /* look for CD sync header (00, 0xFF x 10, 00) */
  73. if (memcmp(p->buf+start,sync_header,sizeof(sync_header)))
  74. return 0;
  75. /* MPEG files (like those ripped from VCDs) can also look like this;
  76. * only return half certainty */
  77. return 50;
  78. }
  79. #if 0
  80. static void dump(unsigned char *buf,size_t len)
  81. {
  82. int i;
  83. for(i=0;i<len;i++) {
  84. if ((i&15)==0) av_log(NULL, AV_LOG_DEBUG, "%04x ",i);
  85. av_log(NULL, AV_LOG_DEBUG, "%02x ",buf[i]);
  86. if ((i&15)==15) av_log(NULL, AV_LOG_DEBUG, "\n");
  87. }
  88. av_log(NULL, AV_LOG_DEBUG, "\n");
  89. }
  90. #endif
  91. static int str_read_header(AVFormatContext *s,
  92. AVFormatParameters *ap)
  93. {
  94. ByteIOContext *pb = s->pb;
  95. StrDemuxContext *str = s->priv_data;
  96. AVStream *st;
  97. unsigned char sector[RAW_CD_SECTOR_SIZE];
  98. int start;
  99. int i;
  100. int channel;
  101. /* initialize context members */
  102. str->pts = 0;
  103. str->audio_channel = -1; /* assume to audio or video */
  104. str->video_channel = -1;
  105. /* skip over any RIFF header */
  106. if (get_buffer(pb, sector, RIFF_HEADER_SIZE) != RIFF_HEADER_SIZE)
  107. return AVERROR(EIO);
  108. if (AV_RL32(&sector[0]) == RIFF_TAG)
  109. start = RIFF_HEADER_SIZE;
  110. else
  111. start = 0;
  112. url_fseek(pb, start, SEEK_SET);
  113. /* check through the first 32 sectors for individual channels */
  114. for (i = 0; i < 32; i++) {
  115. if (get_buffer(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
  116. return AVERROR(EIO);
  117. //printf("%02x %02x %02x %02x\n",sector[0x10],sector[0x11],sector[0x12],sector[0x13]);
  118. channel = sector[0x11];
  119. if (channel >= 32)
  120. return AVERROR_INVALIDDATA;
  121. switch (sector[0x12] & CDXA_TYPE_MASK) {
  122. case CDXA_TYPE_DATA:
  123. case CDXA_TYPE_VIDEO:
  124. /* check if this channel gets to be the dominant video channel */
  125. if (str->video_channel == -1) {
  126. /* qualify the magic number */
  127. if (AV_RL32(&sector[0x18]) != STR_MAGIC)
  128. break;
  129. str->video_channel = channel;
  130. /* allocate a new AVStream */
  131. st = av_new_stream(s, 0);
  132. if (!st)
  133. return AVERROR(ENOMEM);
  134. av_set_pts_info(st, 64, 1, 15);
  135. str->channels[channel].video_stream_index = st->index;
  136. st->codec->codec_type = CODEC_TYPE_VIDEO;
  137. st->codec->codec_id = CODEC_ID_MDEC;
  138. st->codec->codec_tag = 0; /* no fourcc */
  139. st->codec->width = AV_RL16(&sector[0x28]);
  140. st->codec->height = AV_RL16(&sector[0x2A]);
  141. }
  142. break;
  143. case CDXA_TYPE_AUDIO:
  144. /* check if this channel gets to be the dominant audio channel */
  145. if (str->audio_channel == -1) {
  146. int fmt;
  147. str->audio_channel = channel;
  148. /* allocate a new AVStream */
  149. st = av_new_stream(s, 0);
  150. if (!st)
  151. return AVERROR(ENOMEM);
  152. str->channels[channel].audio_stream_index = st->index;
  153. fmt = sector[0x13];
  154. st->codec->codec_type = CODEC_TYPE_AUDIO;
  155. st->codec->codec_id = CODEC_ID_ADPCM_XA;
  156. st->codec->codec_tag = 0; /* no fourcc */
  157. st->codec->channels = (fmt&1)?2:1;
  158. st->codec->sample_rate = (fmt&4)?18900:37800;
  159. // st->codec->bit_rate = 0; //FIXME;
  160. st->codec->block_align = 128;
  161. av_set_pts_info(st, 64, 128, st->codec->sample_rate);
  162. }
  163. break;
  164. default:
  165. /* ignore */
  166. break;
  167. }
  168. }
  169. /* back to the start */
  170. url_fseek(pb, start, SEEK_SET);
  171. return 0;
  172. }
  173. static int str_read_packet(AVFormatContext *s,
  174. AVPacket *ret_pkt)
  175. {
  176. ByteIOContext *pb = s->pb;
  177. StrDemuxContext *str = s->priv_data;
  178. unsigned char sector[RAW_CD_SECTOR_SIZE];
  179. int channel;
  180. AVPacket *pkt;
  181. while (1) {
  182. if (get_buffer(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
  183. return AVERROR(EIO);
  184. channel = sector[0x11];
  185. if (channel >= 32)
  186. return AVERROR_INVALIDDATA;
  187. switch (sector[0x12] & CDXA_TYPE_MASK) {
  188. case CDXA_TYPE_DATA:
  189. case CDXA_TYPE_VIDEO:
  190. /* check if this the video channel we care about */
  191. if (channel == str->video_channel) {
  192. int current_sector = AV_RL16(&sector[0x1C]);
  193. int sector_count = AV_RL16(&sector[0x1E]);
  194. int frame_size = AV_RL32(&sector[0x24]);
  195. if(!( frame_size>=0
  196. && current_sector < sector_count
  197. && sector_count*VIDEO_DATA_CHUNK_SIZE >=frame_size)){
  198. av_log(s, AV_LOG_ERROR, "Invalid parameters %d %d %d\n", current_sector, sector_count, frame_size);
  199. return AVERROR_INVALIDDATA;
  200. }
  201. // printf("%d %d %d\n",current_sector,sector_count,frame_size);
  202. /* if this is the first sector of the frame, allocate a pkt */
  203. pkt = &str->tmp_pkt;
  204. if(pkt->size != sector_count*VIDEO_DATA_CHUNK_SIZE){
  205. if(pkt->data)
  206. av_log(s, AV_LOG_ERROR, "missmatching sector_count\n");
  207. av_free_packet(pkt);
  208. if (av_new_packet(pkt, sector_count*VIDEO_DATA_CHUNK_SIZE))
  209. return AVERROR(EIO);
  210. pkt->pos= url_ftell(pb) - RAW_CD_SECTOR_SIZE;
  211. pkt->stream_index =
  212. str->channels[channel].video_stream_index;
  213. // pkt->pts = str->pts;
  214. /* if there is no audio, adjust the pts after every video
  215. * frame; assume 15 fps */
  216. if (str->audio_channel != -1)
  217. str->pts += (90000 / 15);
  218. }
  219. memcpy(pkt->data + current_sector*VIDEO_DATA_CHUNK_SIZE,
  220. sector + VIDEO_DATA_HEADER_SIZE,
  221. VIDEO_DATA_CHUNK_SIZE);
  222. if (current_sector == sector_count-1) {
  223. pkt->size= frame_size;
  224. *ret_pkt = *pkt;
  225. pkt->data= NULL;
  226. pkt->size= -1;
  227. return 0;
  228. }
  229. }
  230. break;
  231. case CDXA_TYPE_AUDIO:
  232. #ifdef PRINTSTUFF
  233. printf (" dropping audio sector\n");
  234. #endif
  235. #if 1
  236. /* check if this the video channel we care about */
  237. if (channel == str->audio_channel) {
  238. pkt = ret_pkt;
  239. if (av_new_packet(pkt, 2304))
  240. return AVERROR(EIO);
  241. memcpy(pkt->data,sector+24,2304);
  242. pkt->stream_index =
  243. str->channels[channel].audio_stream_index;
  244. //pkt->pts = str->pts;
  245. return 0;
  246. }
  247. #endif
  248. break;
  249. default:
  250. /* drop the sector and move on */
  251. #ifdef PRINTSTUFF
  252. printf (" dropping other sector\n");
  253. #endif
  254. break;
  255. }
  256. if (url_feof(pb))
  257. return AVERROR(EIO);
  258. }
  259. }
  260. static int str_read_close(AVFormatContext *s)
  261. {
  262. StrDemuxContext *str = s->priv_data;
  263. return 0;
  264. }
  265. AVInputFormat str_demuxer = {
  266. "psxstr",
  267. NULL_IF_CONFIG_SMALL("Sony Playstation STR format"),
  268. sizeof(StrDemuxContext),
  269. str_probe,
  270. str_read_header,
  271. str_read_packet,
  272. str_read_close,
  273. };