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.

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