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.

379 lines
11KB

  1. /*
  2. * Sony Playstation (PSX) STR File Demuxer
  3. * Copyright (c) 2003 The ffmpeg Project
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file psxstr.c
  21. * PSX STR file demuxer
  22. * by Mike Melanson (melanson@pcisys.net)
  23. * This module handles streams that have been ripped from Sony Playstation
  24. * CD games. This demuxer can handle either raw STR files (which are just
  25. * concatenations of raw compact disc sectors) or STR files with 0x2C-byte
  26. * RIFF headers, followed by CD sectors.
  27. */
  28. #include "avformat.h"
  29. //#define PRINTSTUFF
  30. #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
  31. #define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
  32. (((uint8_t*)(x))[2] << 16) | \
  33. (((uint8_t*)(x))[1] << 8) | \
  34. ((uint8_t*)(x))[0])
  35. #define FOURCC_TAG( ch0, ch1, ch2, ch3 ) \
  36. ( (long)(unsigned char)(ch0) | \
  37. ( (long)(unsigned char)(ch1) << 8 ) | \
  38. ( (long)(unsigned char)(ch2) << 16 ) | \
  39. ( (long)(unsigned char)(ch3) << 24 ) )
  40. #define RIFF_TAG FOURCC_TAG('R', 'I', 'F', 'F')
  41. #define CDXA_TAG FOURCC_TAG('C', 'D', 'X', 'A')
  42. #define RAW_CD_SECTOR_SIZE 2352
  43. #define RAW_CD_SECTOR_DATA_SIZE 2304
  44. #define VIDEO_DATA_CHUNK_SIZE 0x7E0
  45. #define VIDEO_DATA_HEADER_SIZE 0x38
  46. #define RIFF_HEADER_SIZE 0x2C
  47. #define CDXA_TYPE_MASK 0x0E
  48. #define CDXA_TYPE_DATA 0x08
  49. #define CDXA_TYPE_AUDIO 0x04
  50. #define CDXA_TYPE_VIDEO 0x02
  51. #define STR_MAGIC (0x80010160)
  52. typedef struct StrChannel {
  53. int type;
  54. #define STR_AUDIO 0
  55. #define STR_VIDEO 1
  56. /* video parameters */
  57. int width;
  58. int height;
  59. int video_stream_index;
  60. /* audio parameters */
  61. int sample_rate;
  62. int channels;
  63. int bits;
  64. int audio_stream_index;
  65. } StrChannel;
  66. typedef struct StrDemuxContext {
  67. /* a STR file can contain up to 32 channels of data */
  68. StrChannel channels[32];
  69. /* only decode the first audio and video channels encountered */
  70. int video_channel;
  71. int audio_channel;
  72. int64_t pts;
  73. unsigned char *video_chunk;
  74. AVPacket tmp_pkt;
  75. } StrDemuxContext;
  76. const static char sync_header[12] = {0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00};
  77. static int str_probe(AVProbeData *p)
  78. {
  79. int start;
  80. /* need at least 0x38 bytes to validate */
  81. if (p->buf_size < 0x38)
  82. return 0;
  83. if ((LE_32(&p->buf[0]) == RIFF_TAG) &&
  84. (LE_32(&p->buf[8]) == CDXA_TAG)) {
  85. /* RIFF header seen; skip 0x2C bytes */
  86. start = RIFF_HEADER_SIZE;
  87. } else
  88. start = 0;
  89. /* look for CD sync header (00, 0xFF x 10, 00) */
  90. if (memcmp(p->buf+start,sync_header,sizeof(sync_header)))
  91. return 0;
  92. /* MPEG files (like those ripped from VCDs) can also look like this;
  93. * only return half certainty */
  94. return 50;
  95. }
  96. static void dump(unsigned char *buf,size_t len)
  97. {
  98. int i;
  99. for(i=0;i<len;i++) {
  100. if ((i&15)==0) printf("%04x ",i);
  101. printf("%02x ",buf[i]);
  102. if ((i&15)==15) printf("\n");
  103. }
  104. printf("\n");
  105. }
  106. static int str_read_header(AVFormatContext *s,
  107. AVFormatParameters *ap)
  108. {
  109. ByteIOContext *pb = &s->pb;
  110. StrDemuxContext *str = (StrDemuxContext *)s->priv_data;
  111. AVStream *st;
  112. unsigned char sector[RAW_CD_SECTOR_SIZE];
  113. int start;
  114. int i;
  115. int channel;
  116. /* initialize context members */
  117. str->pts = 0;
  118. str->audio_channel = -1; /* assume to audio or video */
  119. str->video_channel = -1;
  120. str->video_chunk = NULL;
  121. /* set the pts reference (1 pts = 1/90000) */
  122. s->pts_num = 1;
  123. s->pts_den = 90000;
  124. /* skip over any RIFF header */
  125. if (get_buffer(pb, sector, RIFF_HEADER_SIZE) != RIFF_HEADER_SIZE)
  126. return AVERROR_IO;
  127. if (LE_32(&sector[0]) == RIFF_TAG)
  128. start = RIFF_HEADER_SIZE;
  129. else
  130. start = 0;
  131. url_fseek(pb, start, SEEK_SET);
  132. /* check through the first 32 sectors for individual channels */
  133. for (i = 0; i < 32; i++) {
  134. if (get_buffer(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
  135. return AVERROR_IO;
  136. //printf("%02x %02x %02x %02x\n",sector[0x10],sector[0x11],sector[0x12],sector[0x13]);
  137. channel = sector[0x11];
  138. if (channel >= 32)
  139. return AVERROR_INVALIDDATA;
  140. switch (sector[0x12] & CDXA_TYPE_MASK) {
  141. case CDXA_TYPE_DATA:
  142. case CDXA_TYPE_VIDEO:
  143. /* check if this channel gets to be the dominant video channel */
  144. if (str->video_channel == -1) {
  145. /* qualify the magic number */
  146. if (LE_32(&sector[0x18]) != STR_MAGIC)
  147. break;
  148. str->video_channel = channel;
  149. str->channels[channel].type = STR_VIDEO;
  150. str->channels[channel].width = LE_16(&sector[0x28]);
  151. str->channels[channel].height = LE_16(&sector[0x2A]);
  152. /* allocate a new AVStream */
  153. st = av_new_stream(s, 0);
  154. if (!st)
  155. return AVERROR_NOMEM;
  156. str->channels[channel].video_stream_index = st->index;
  157. st->codec.codec_type = CODEC_TYPE_VIDEO;
  158. st->codec.codec_id = CODEC_ID_MDEC;
  159. st->codec.codec_tag = 0; /* no fourcc */
  160. st->codec.width = str->channels[channel].width;
  161. st->codec.height = str->channels[channel].height;
  162. }
  163. break;
  164. case CDXA_TYPE_AUDIO:
  165. /* check if this channel gets to be the dominant audio channel */
  166. if (str->audio_channel == -1) {
  167. int fmt;
  168. str->audio_channel = channel;
  169. str->channels[channel].type = STR_AUDIO;
  170. str->channels[channel].channels =
  171. (sector[0x13] & 0x01) ? 2 : 1;
  172. str->channels[channel].sample_rate =
  173. (sector[0x13] & 0x04) ? 18900 : 37800;
  174. str->channels[channel].bits =
  175. (sector[0x13] & 0x10) ? 8 : 4;
  176. /* allocate a new AVStream */
  177. st = av_new_stream(s, 0);
  178. if (!st)
  179. return AVERROR_NOMEM;
  180. str->channels[channel].audio_stream_index = st->index;
  181. fmt = sector[0x13];
  182. st->codec.codec_type = CODEC_TYPE_AUDIO;
  183. st->codec.codec_id = CODEC_ID_ADPCM_XA;
  184. st->codec.codec_tag = 0; /* no fourcc */
  185. st->codec.channels = (fmt&1)?2:1;
  186. st->codec.sample_rate = (fmt&4)?18900:37800;
  187. // st->codec.bit_rate = 0; //FIXME;
  188. st->codec.block_align = 128;
  189. }
  190. break;
  191. default:
  192. /* ignore */
  193. break;
  194. }
  195. }
  196. if (str->video_channel != -1)
  197. printf (" video channel = %d, %d x %d %d\n", str->video_channel,
  198. str->channels[str->video_channel].width,
  199. str->channels[str->video_channel].height,str->channels[str->video_channel].video_stream_index);
  200. if (str->audio_channel != -1)
  201. printf (" audio channel = %d, %d Hz, %d channels, %d bits/sample %d\n",
  202. str->audio_channel,
  203. str->channels[str->audio_channel].sample_rate,
  204. str->channels[str->audio_channel].channels,
  205. str->channels[str->audio_channel].bits,str->channels[str->audio_channel].audio_stream_index);
  206. /* back to the start */
  207. url_fseek(pb, start, SEEK_SET);
  208. return 0;
  209. }
  210. static int str_read_packet(AVFormatContext *s,
  211. AVPacket *ret_pkt)
  212. {
  213. ByteIOContext *pb = &s->pb;
  214. StrDemuxContext *str = (StrDemuxContext *)s->priv_data;
  215. unsigned char sector[RAW_CD_SECTOR_SIZE];
  216. int channel;
  217. int packet_read = 0;
  218. int ret = 0;
  219. AVPacket *pkt;
  220. while (!packet_read) {
  221. if (get_buffer(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
  222. return -EIO;
  223. channel = sector[0x11];
  224. if (channel >= 32)
  225. return AVERROR_INVALIDDATA;
  226. switch (sector[0x12] & CDXA_TYPE_MASK) {
  227. case CDXA_TYPE_DATA:
  228. case CDXA_TYPE_VIDEO:
  229. /* check if this the video channel we care about */
  230. if (channel == str->video_channel) {
  231. int current_sector = LE_16(&sector[0x1C]);
  232. int sector_count = LE_16(&sector[0x1E]);
  233. int frame_size = LE_32(&sector[0x24]);
  234. int bytes_to_copy;
  235. // printf("%d %d %d\n",current_sector,sector_count,frame_size);
  236. /* if this is the first sector of the frame, allocate a pkt */
  237. pkt = &str->tmp_pkt;
  238. if (current_sector == 0) {
  239. if (av_new_packet(pkt, frame_size))
  240. return -EIO;
  241. pkt->stream_index =
  242. str->channels[channel].video_stream_index;
  243. // pkt->pts = str->pts;
  244. /* if there is no audio, adjust the pts after every video
  245. * frame; assume 15 fps */
  246. if (str->audio_channel != -1)
  247. str->pts += (90000 / 15);
  248. }
  249. /* load all the constituent chunks in the video packet */
  250. bytes_to_copy = frame_size - current_sector*VIDEO_DATA_CHUNK_SIZE;
  251. if (bytes_to_copy>0) {
  252. if (bytes_to_copy>VIDEO_DATA_CHUNK_SIZE) bytes_to_copy=VIDEO_DATA_CHUNK_SIZE;
  253. memcpy(pkt->data + current_sector*VIDEO_DATA_CHUNK_SIZE,
  254. sector + VIDEO_DATA_HEADER_SIZE, bytes_to_copy);
  255. }
  256. if (current_sector == sector_count-1) {
  257. *ret_pkt = *pkt;
  258. return 0;
  259. }
  260. }
  261. break;
  262. case CDXA_TYPE_AUDIO:
  263. #ifdef PRINTSTUFF
  264. printf (" dropping audio sector\n");
  265. #endif
  266. #if 1
  267. /* check if this the video channel we care about */
  268. if (channel == str->audio_channel) {
  269. pkt = ret_pkt;
  270. if (av_new_packet(pkt, 2304))
  271. return -EIO;
  272. memcpy(pkt->data,sector+24,2304);
  273. pkt->stream_index =
  274. str->channels[channel].audio_stream_index;
  275. //pkt->pts = str->pts;
  276. return 0;
  277. }
  278. #endif
  279. break;
  280. default:
  281. /* drop the sector and move on */
  282. #ifdef PRINTSTUFF
  283. printf (" dropping other sector\n");
  284. #endif
  285. break;
  286. }
  287. if (url_feof(pb))
  288. return -EIO;
  289. }
  290. return ret;
  291. }
  292. static int str_read_close(AVFormatContext *s)
  293. {
  294. StrDemuxContext *str = (StrDemuxContext *)s->priv_data;
  295. av_free(str->video_chunk);
  296. return 0;
  297. }
  298. static AVInputFormat str_iformat = {
  299. "psxstr",
  300. "Sony Playstation STR format",
  301. sizeof(StrDemuxContext),
  302. str_probe,
  303. str_read_header,
  304. str_read_packet,
  305. str_read_close,
  306. };
  307. int str_init(void)
  308. {
  309. av_register_input_format(&str_iformat);
  310. return 0;
  311. }