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.

353 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. } StrDemuxContext;
  75. static int str_probe(AVProbeData *p)
  76. {
  77. int start;
  78. /* need at least 0x38 bytes to validate */
  79. if (p->buf_size < 0x38)
  80. return 0;
  81. if ((LE_32(&p->buf[0]) == RIFF_TAG) &&
  82. (LE_32(&p->buf[8]) == CDXA_TAG)) {
  83. /* RIFF header seen; skip 0x2C bytes */
  84. start = RIFF_HEADER_SIZE;
  85. } else
  86. start = 0;
  87. /* look for CD sync header (00, 0xFF x 10, 00) */
  88. if ((p->buf[start + 0] != 0x00) || (p->buf[start + 1] != 0xFF) ||
  89. (p->buf[start + 2] != 0xFF) || (p->buf[start + 3] != 0xFF) ||
  90. (p->buf[start + 4] != 0xFF) || (p->buf[start + 5] != 0xFF) ||
  91. (p->buf[start + 6] != 0xFF) || (p->buf[start + 7] != 0xFF) ||
  92. (p->buf[start + 8] != 0xFF) || (p->buf[start + 9] != 0xFF) ||
  93. (p->buf[start + 10] != 0xFF) || (p->buf[start + 11] != 0x00))
  94. return 0;
  95. /* MPEG files (like those ripped from VCDs) can also look like this;
  96. * only return half certainty */
  97. return 50;
  98. }
  99. static int str_read_header(AVFormatContext *s,
  100. AVFormatParameters *ap)
  101. {
  102. ByteIOContext *pb = &s->pb;
  103. StrDemuxContext *str = (StrDemuxContext *)s->priv_data;
  104. AVStream *st;
  105. unsigned char sector[RAW_CD_SECTOR_SIZE];
  106. int start;
  107. int i;
  108. int channel;
  109. /* initialize context members */
  110. str->pts = 0;
  111. str->audio_channel = -1; /* assume to audio or video */
  112. str->video_channel = -1;
  113. str->video_chunk = NULL;
  114. /* set the pts reference (1 pts = 1/90000) */
  115. s->pts_num = 1;
  116. s->pts_den = 90000;
  117. /* skip over any RIFF header */
  118. if (get_buffer(pb, sector, RIFF_HEADER_SIZE) != RIFF_HEADER_SIZE)
  119. return AVERROR_IO;
  120. if (LE_32(&sector[0]) == RIFF_TAG)
  121. start = RIFF_HEADER_SIZE;
  122. else
  123. start = 0;
  124. url_fseek(pb, start, SEEK_SET);
  125. /* check through the first 32 sectors for individual channels */
  126. for (i = 0; i < 32; i++) {
  127. if (get_buffer(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
  128. return AVERROR_IO;
  129. channel = sector[0x11];
  130. if (channel >= 32)
  131. return AVERROR_INVALIDDATA;
  132. switch (sector[0x12] & CDXA_TYPE_MASK) {
  133. case CDXA_TYPE_DATA:
  134. case CDXA_TYPE_VIDEO:
  135. /* check if this channel gets to be the dominant video channel */
  136. if (str->video_channel == -1) {
  137. /* qualify the magic number */
  138. if (LE_32(&sector[0x18]) != STR_MAGIC)
  139. break;
  140. str->video_channel = channel;
  141. str->channels[channel].type = STR_VIDEO;
  142. str->channels[channel].width = LE_16(&sector[0x28]);
  143. str->channels[channel].height = LE_16(&sector[0x2A]);
  144. /* allocate a new AVStream */
  145. st = av_new_stream(s, 0);
  146. if (!st)
  147. return AVERROR_NOMEM;
  148. str->channels[channel].video_stream_index = st->index;
  149. st->codec.codec_type = CODEC_TYPE_VIDEO;
  150. st->codec.codec_id = CODEC_ID_MDEC;
  151. st->codec.codec_tag = 0; /* no fourcc */
  152. st->codec.width = str->channels[channel].width;
  153. st->codec.height = str->channels[channel].height;
  154. }
  155. break;
  156. case CDXA_TYPE_AUDIO:
  157. /* check if this channel gets to be the dominant audio channel */
  158. if (str->audio_channel == -1) {
  159. str->audio_channel = channel;
  160. str->channels[channel].type = STR_AUDIO;
  161. str->channels[channel].channels =
  162. (sector[0x13] & 0x01) ? 2 : 1;
  163. str->channels[channel].sample_rate =
  164. (sector[0x13] & 0x04) ? 18900 : 37800;
  165. str->channels[channel].bits =
  166. (sector[0x13] & 0x10) ? 8 : 4;
  167. }
  168. break;
  169. default:
  170. /* ignore */
  171. break;
  172. }
  173. }
  174. if (str->video_channel != -1)
  175. printf (" video channel = %d, %d x %d\n", str->video_channel,
  176. str->channels[str->video_channel].width,
  177. str->channels[str->video_channel].height);
  178. if (str->audio_channel != -1)
  179. printf (" audio channel = %d, %d Hz, %d channels, %d bits/sample\n",
  180. str->video_channel,
  181. str->channels[str->video_channel].sample_rate,
  182. str->channels[str->video_channel].channels,
  183. str->channels[str->video_channel].bits);
  184. /* back to the start */
  185. url_fseek(pb, start, SEEK_SET);
  186. return 0;
  187. }
  188. static int str_read_packet(AVFormatContext *s,
  189. AVPacket *pkt)
  190. {
  191. ByteIOContext *pb = &s->pb;
  192. StrDemuxContext *str = (StrDemuxContext *)s->priv_data;
  193. unsigned char sector[RAW_CD_SECTOR_SIZE];
  194. int channel;
  195. int packet_read = 0;
  196. int video_sector_count = 0;
  197. int current_video_sector = 0;
  198. int video_frame_size = 0;
  199. int video_frame_index = 0;
  200. int bytes_to_copy;
  201. int ret = 0;
  202. while (!packet_read) {
  203. if (get_buffer(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
  204. return -EIO;
  205. channel = sector[0x11];
  206. if (channel >= 32)
  207. return AVERROR_INVALIDDATA;
  208. switch (sector[0x12] & CDXA_TYPE_MASK) {
  209. case CDXA_TYPE_DATA:
  210. case CDXA_TYPE_VIDEO:
  211. /* check if this the video channel we care about */
  212. if (channel == str->video_channel) {
  213. /* if this is the first sector of the frame, allocate a pkt */
  214. if (current_video_sector == 0) {
  215. video_frame_size = LE_32(&sector[0x24]);
  216. video_sector_count = LE_16(&sector[0x1E]);
  217. if (av_new_packet(pkt, video_frame_size))
  218. return -EIO;
  219. pkt->stream_index =
  220. str->channels[channel].video_stream_index;
  221. pkt->pts = str->pts;
  222. /* if there is no audio, adjust the pts after every video
  223. * frame; assume 15 fps */
  224. if (str->audio_channel != -1)
  225. str->pts += (90000 / 15);
  226. }
  227. /* load all the constituent chunks in the video packet */
  228. if (video_frame_size - video_frame_index < VIDEO_DATA_CHUNK_SIZE)
  229. bytes_to_copy = video_frame_size - video_frame_index;
  230. else
  231. bytes_to_copy = VIDEO_DATA_CHUNK_SIZE;
  232. if (video_frame_index < video_frame_size)
  233. memcpy(&pkt->data[video_frame_index],
  234. &sector[VIDEO_DATA_HEADER_SIZE], bytes_to_copy);
  235. #ifdef PRINTSTUFF
  236. printf (" chunk %d/%d (bytes %d/%d), first 6 bytes = %02X %02X %02X %02X %02X %02X\n",
  237. current_video_sector, video_sector_count,
  238. video_frame_index, video_frame_size,
  239. pkt->data[current_video_sector * VIDEO_DATA_CHUNK_SIZE + 0],
  240. pkt->data[current_video_sector * VIDEO_DATA_CHUNK_SIZE + 1],
  241. pkt->data[current_video_sector * VIDEO_DATA_CHUNK_SIZE + 2],
  242. pkt->data[current_video_sector * VIDEO_DATA_CHUNK_SIZE + 3],
  243. pkt->data[current_video_sector * VIDEO_DATA_CHUNK_SIZE + 4],
  244. pkt->data[current_video_sector * VIDEO_DATA_CHUNK_SIZE + 5]);
  245. #endif
  246. video_frame_index += bytes_to_copy;
  247. /* must keep reading sectors until all current video sectors
  248. * are consumed */
  249. current_video_sector++;
  250. if (current_video_sector >= video_sector_count)
  251. packet_read = 1;
  252. }
  253. break;
  254. case CDXA_TYPE_AUDIO:
  255. #ifdef PRINTSTUFF
  256. printf (" dropping audio sector\n");
  257. #endif
  258. break;
  259. default:
  260. /* drop the sector and move on */
  261. #ifdef PRINTSTUFF
  262. printf (" dropping other sector\n");
  263. #endif
  264. break;
  265. }
  266. if (url_feof(pb))
  267. return -EIO;
  268. }
  269. return ret;
  270. }
  271. static int str_read_close(AVFormatContext *s)
  272. {
  273. StrDemuxContext *str = (StrDemuxContext *)s->priv_data;
  274. av_free(str->video_chunk);
  275. return 0;
  276. }
  277. static AVInputFormat str_iformat = {
  278. "psxstr",
  279. "Sony Playstation STR format",
  280. sizeof(StrDemuxContext),
  281. str_probe,
  282. str_read_header,
  283. str_read_packet,
  284. str_read_close,
  285. };
  286. int str_init(void)
  287. {
  288. av_register_input_format(&str_iformat);
  289. return 0;
  290. }