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.

292 lines
8.6KB

  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 RIFF_TAG MKTAG('R', 'I', 'F', 'F')
  32. #define CDXA_TAG MKTAG('C', 'D', 'X', 'A')
  33. #define RAW_CD_SECTOR_SIZE 2352
  34. #define RAW_CD_SECTOR_DATA_SIZE 2304
  35. #define VIDEO_DATA_CHUNK_SIZE 0x7E0
  36. #define VIDEO_DATA_HEADER_SIZE 0x38
  37. #define RIFF_HEADER_SIZE 0x2C
  38. #define CDXA_TYPE_MASK 0x0E
  39. #define CDXA_TYPE_DATA 0x08
  40. #define CDXA_TYPE_AUDIO 0x04
  41. #define CDXA_TYPE_VIDEO 0x02
  42. #define STR_MAGIC (0x80010160)
  43. typedef struct StrChannel {
  44. /* video parameters */
  45. int video_stream_index;
  46. AVPacket tmp_pkt;
  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. } StrDemuxContext;
  54. static const char sync_header[12] = {0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00};
  55. static int str_probe(AVProbeData *p)
  56. {
  57. int start;
  58. /* need at least 0x38 bytes to validate */
  59. if (p->buf_size < 0x38)
  60. return 0;
  61. if ((AV_RL32(&p->buf[0]) == RIFF_TAG) &&
  62. (AV_RL32(&p->buf[8]) == CDXA_TAG)) {
  63. /* RIFF header seen; skip 0x2C bytes */
  64. start = RIFF_HEADER_SIZE;
  65. } else
  66. start = 0;
  67. /* look for CD sync header (00, 0xFF x 10, 00) */
  68. if (memcmp(p->buf+start,sync_header,sizeof(sync_header)))
  69. return 0;
  70. /* MPEG files (like those ripped from VCDs) can also look like this;
  71. * only return half certainty */
  72. return 50;
  73. }
  74. static int str_read_header(AVFormatContext *s,
  75. AVFormatParameters *ap)
  76. {
  77. ByteIOContext *pb = s->pb;
  78. StrDemuxContext *str = s->priv_data;
  79. AVStream *st;
  80. unsigned char sector[RAW_CD_SECTOR_SIZE];
  81. int start;
  82. int i;
  83. int channel;
  84. /* skip over any RIFF header */
  85. if (get_buffer(pb, sector, RIFF_HEADER_SIZE) != RIFF_HEADER_SIZE)
  86. return AVERROR(EIO);
  87. if (AV_RL32(&sector[0]) == RIFF_TAG)
  88. start = RIFF_HEADER_SIZE;
  89. else
  90. start = 0;
  91. url_fseek(pb, start, SEEK_SET);
  92. for(i=0; i<32; i++){
  93. str->channels[i].video_stream_index=
  94. str->channels[i].audio_stream_index= -1;
  95. }
  96. /* check through the first 32 sectors for individual channels */
  97. for (i = 0; i < 32; i++) {
  98. if (get_buffer(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
  99. return AVERROR(EIO);
  100. channel = sector[0x11];
  101. if (channel >= 32)
  102. return AVERROR_INVALIDDATA;
  103. switch (sector[0x12] & CDXA_TYPE_MASK) {
  104. case CDXA_TYPE_DATA:
  105. case CDXA_TYPE_VIDEO:
  106. /* qualify the magic number */
  107. if (AV_RL32(&sector[0x18]) != STR_MAGIC)
  108. break;
  109. if(str->channels[channel].video_stream_index != -1)
  110. break;
  111. /* allocate a new AVStream */
  112. st = av_new_stream(s, 0);
  113. if (!st)
  114. return AVERROR(ENOMEM);
  115. av_set_pts_info(st, 64, 1, 15);
  116. str->channels[channel].video_stream_index = st->index;
  117. st->codec->codec_type = CODEC_TYPE_VIDEO;
  118. st->codec->codec_id = CODEC_ID_MDEC;
  119. st->codec->codec_tag = 0; /* no fourcc */
  120. st->codec->width = AV_RL16(&sector[0x28]);
  121. st->codec->height = AV_RL16(&sector[0x2A]);
  122. break;
  123. case CDXA_TYPE_AUDIO:
  124. {
  125. int fmt;
  126. if(str->channels[channel].audio_stream_index != -1)
  127. break;
  128. /* allocate a new AVStream */
  129. st = av_new_stream(s, 0);
  130. if (!st)
  131. return AVERROR(ENOMEM);
  132. str->channels[channel].audio_stream_index = st->index;
  133. fmt = sector[0x13];
  134. st->codec->codec_type = CODEC_TYPE_AUDIO;
  135. st->codec->codec_id = CODEC_ID_ADPCM_XA;
  136. st->codec->codec_tag = 0; /* no fourcc */
  137. st->codec->channels = (fmt&1)?2:1;
  138. st->codec->sample_rate = (fmt&4)?18900:37800;
  139. // st->codec->bit_rate = 0; //FIXME;
  140. st->codec->block_align = 128;
  141. av_set_pts_info(st, 64, 128, st->codec->sample_rate);
  142. }
  143. break;
  144. default:
  145. /* ignore */
  146. break;
  147. }
  148. }
  149. /* back to the start */
  150. url_fseek(pb, start, SEEK_SET);
  151. return 0;
  152. }
  153. static int str_read_packet(AVFormatContext *s,
  154. AVPacket *ret_pkt)
  155. {
  156. ByteIOContext *pb = s->pb;
  157. StrDemuxContext *str = s->priv_data;
  158. unsigned char sector[RAW_CD_SECTOR_SIZE];
  159. int channel;
  160. AVPacket *pkt;
  161. while (1) {
  162. if (get_buffer(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
  163. return AVERROR(EIO);
  164. channel = sector[0x11];
  165. if (channel >= 32)
  166. return AVERROR_INVALIDDATA;
  167. switch (sector[0x12] & CDXA_TYPE_MASK) {
  168. case CDXA_TYPE_DATA:
  169. case CDXA_TYPE_VIDEO:
  170. {
  171. int current_sector = AV_RL16(&sector[0x1C]);
  172. int sector_count = AV_RL16(&sector[0x1E]);
  173. int frame_size = AV_RL32(&sector[0x24]);
  174. if(!( frame_size>=0
  175. && current_sector < sector_count
  176. && sector_count*VIDEO_DATA_CHUNK_SIZE >=frame_size)){
  177. av_log(s, AV_LOG_ERROR, "Invalid parameters %d %d %d\n", current_sector, sector_count, frame_size);
  178. return AVERROR_INVALIDDATA;
  179. }
  180. /* if this is the first sector of the frame, allocate a pkt */
  181. pkt = &str->channels[channel].tmp_pkt;
  182. if(pkt->size != sector_count*VIDEO_DATA_CHUNK_SIZE){
  183. if(pkt->data)
  184. av_log(s, AV_LOG_ERROR, "missmatching sector_count\n");
  185. av_free_packet(pkt);
  186. if (av_new_packet(pkt, sector_count*VIDEO_DATA_CHUNK_SIZE))
  187. return AVERROR(EIO);
  188. pkt->pos= url_ftell(pb) - RAW_CD_SECTOR_SIZE;
  189. pkt->stream_index =
  190. str->channels[channel].video_stream_index;
  191. }
  192. memcpy(pkt->data + current_sector*VIDEO_DATA_CHUNK_SIZE,
  193. sector + VIDEO_DATA_HEADER_SIZE,
  194. VIDEO_DATA_CHUNK_SIZE);
  195. if (current_sector == sector_count-1) {
  196. pkt->size= frame_size;
  197. *ret_pkt = *pkt;
  198. pkt->data= NULL;
  199. pkt->size= -1;
  200. return 0;
  201. }
  202. }
  203. break;
  204. case CDXA_TYPE_AUDIO:
  205. pkt = ret_pkt;
  206. if (av_new_packet(pkt, 2304))
  207. return AVERROR(EIO);
  208. memcpy(pkt->data,sector+24,2304);
  209. pkt->stream_index =
  210. str->channels[channel].audio_stream_index;
  211. return 0;
  212. break;
  213. default:
  214. /* drop the sector and move on */
  215. break;
  216. }
  217. if (url_feof(pb))
  218. return AVERROR(EIO);
  219. }
  220. }
  221. static int str_read_close(AVFormatContext *s)
  222. {
  223. StrDemuxContext *str = s->priv_data;
  224. return 0;
  225. }
  226. AVInputFormat str_demuxer = {
  227. "psxstr",
  228. NULL_IF_CONFIG_SMALL("Sony Playstation STR format"),
  229. sizeof(StrDemuxContext),
  230. str_probe,
  231. str_read_header,
  232. str_read_packet,
  233. str_read_close,
  234. };