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.

365 lines
11KB

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