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.

269 lines
8.2KB

  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. uint8_t *sector= p->buf;
  58. if (p->buf_size < RAW_CD_SECTOR_SIZE)
  59. return 0;
  60. if ((AV_RL32(&p->buf[0]) == RIFF_TAG) &&
  61. (AV_RL32(&p->buf[8]) == CDXA_TAG)) {
  62. /* RIFF header seen; skip 0x2C bytes */
  63. sector += RIFF_HEADER_SIZE;
  64. }
  65. /* look for CD sync header (00, 0xFF x 10, 00) */
  66. if (memcmp(sector,sync_header,sizeof(sync_header)))
  67. return 0;
  68. if(sector[0x11] >= 32)
  69. return 0;
  70. if( (sector[0x12] & CDXA_TYPE_MASK) != CDXA_TYPE_VIDEO
  71. && (sector[0x12] & CDXA_TYPE_MASK) != CDXA_TYPE_AUDIO
  72. && (sector[0x12] & CDXA_TYPE_MASK) != CDXA_TYPE_DATA)
  73. return 0;
  74. /* MPEG files (like those ripped from VCDs) can also look like this;
  75. * only return half certainty */
  76. return 50;
  77. }
  78. static int str_read_header(AVFormatContext *s,
  79. AVFormatParameters *ap)
  80. {
  81. ByteIOContext *pb = s->pb;
  82. StrDemuxContext *str = s->priv_data;
  83. unsigned char sector[RAW_CD_SECTOR_SIZE];
  84. int start;
  85. int i;
  86. /* skip over any RIFF header */
  87. if (get_buffer(pb, sector, RIFF_HEADER_SIZE) != RIFF_HEADER_SIZE)
  88. return AVERROR(EIO);
  89. if (AV_RL32(&sector[0]) == RIFF_TAG)
  90. start = RIFF_HEADER_SIZE;
  91. else
  92. start = 0;
  93. url_fseek(pb, start, SEEK_SET);
  94. for(i=0; i<32; i++){
  95. str->channels[i].video_stream_index=
  96. str->channels[i].audio_stream_index= -1;
  97. }
  98. s->ctx_flags |= AVFMTCTX_NOHEADER;
  99. return 0;
  100. }
  101. static int str_read_packet(AVFormatContext *s,
  102. AVPacket *ret_pkt)
  103. {
  104. ByteIOContext *pb = s->pb;
  105. StrDemuxContext *str = s->priv_data;
  106. unsigned char sector[RAW_CD_SECTOR_SIZE];
  107. int channel;
  108. AVPacket *pkt;
  109. AVStream *st;
  110. while (1) {
  111. if (get_buffer(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
  112. return AVERROR(EIO);
  113. channel = sector[0x11];
  114. if (channel >= 32)
  115. return AVERROR_INVALIDDATA;
  116. switch (sector[0x12] & CDXA_TYPE_MASK) {
  117. case CDXA_TYPE_DATA:
  118. case CDXA_TYPE_VIDEO:
  119. {
  120. int current_sector = AV_RL16(&sector[0x1C]);
  121. int sector_count = AV_RL16(&sector[0x1E]);
  122. int frame_size = AV_RL32(&sector[0x24]);
  123. if(!( frame_size>=0
  124. && current_sector < sector_count
  125. && sector_count*VIDEO_DATA_CHUNK_SIZE >=frame_size)){
  126. av_log(s, AV_LOG_ERROR, "Invalid parameters %d %d %d\n", current_sector, sector_count, frame_size);
  127. break;
  128. }
  129. if(str->channels[channel].video_stream_index < 0){
  130. /* allocate a new AVStream */
  131. st = av_new_stream(s, 0);
  132. if (!st)
  133. return AVERROR(ENOMEM);
  134. av_set_pts_info(st, 64, 1, 15);
  135. str->channels[channel].video_stream_index = st->index;
  136. st->codec->codec_type = CODEC_TYPE_VIDEO;
  137. st->codec->codec_id = CODEC_ID_MDEC;
  138. st->codec->codec_tag = 0; /* no fourcc */
  139. st->codec->width = AV_RL16(&sector[0x28]);
  140. st->codec->height = AV_RL16(&sector[0x2A]);
  141. }
  142. /* if this is the first sector of the frame, allocate a pkt */
  143. pkt = &str->channels[channel].tmp_pkt;
  144. if(pkt->size != sector_count*VIDEO_DATA_CHUNK_SIZE){
  145. if(pkt->data)
  146. av_log(s, AV_LOG_ERROR, "missmatching sector_count\n");
  147. av_free_packet(pkt);
  148. if (av_new_packet(pkt, sector_count*VIDEO_DATA_CHUNK_SIZE))
  149. return AVERROR(EIO);
  150. pkt->pos= url_ftell(pb) - RAW_CD_SECTOR_SIZE;
  151. pkt->stream_index =
  152. str->channels[channel].video_stream_index;
  153. }
  154. memcpy(pkt->data + current_sector*VIDEO_DATA_CHUNK_SIZE,
  155. sector + VIDEO_DATA_HEADER_SIZE,
  156. VIDEO_DATA_CHUNK_SIZE);
  157. if (current_sector == sector_count-1) {
  158. pkt->size= frame_size;
  159. *ret_pkt = *pkt;
  160. pkt->data= NULL;
  161. pkt->size= -1;
  162. return 0;
  163. }
  164. }
  165. break;
  166. case CDXA_TYPE_AUDIO:
  167. if(str->channels[channel].audio_stream_index < 0){
  168. int fmt = sector[0x13];
  169. /* allocate a new AVStream */
  170. st = av_new_stream(s, 0);
  171. if (!st)
  172. return AVERROR(ENOMEM);
  173. str->channels[channel].audio_stream_index = st->index;
  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. av_set_pts_info(st, 64, 128, st->codec->sample_rate);
  182. }
  183. pkt = ret_pkt;
  184. if (av_new_packet(pkt, 2304))
  185. return AVERROR(EIO);
  186. memcpy(pkt->data,sector+24,2304);
  187. pkt->stream_index =
  188. str->channels[channel].audio_stream_index;
  189. return 0;
  190. break;
  191. default:
  192. av_log(s, AV_LOG_WARNING, "Unknown sector type %02X\n", sector[0x12]);
  193. /* drop the sector and move on */
  194. break;
  195. }
  196. if (url_feof(pb))
  197. return AVERROR(EIO);
  198. }
  199. }
  200. static int str_read_close(AVFormatContext *s)
  201. {
  202. StrDemuxContext *str = s->priv_data;
  203. int i;
  204. for(i=0; i<32; i++){
  205. if(str->channels[i].tmp_pkt.data)
  206. av_free_packet(&str->channels[i].tmp_pkt);
  207. }
  208. return 0;
  209. }
  210. AVInputFormat str_demuxer = {
  211. "psxstr",
  212. NULL_IF_CONFIG_SMALL("Sony Playstation STR format"),
  213. sizeof(StrDemuxContext),
  214. str_probe,
  215. str_read_header,
  216. str_read_packet,
  217. str_read_close,
  218. };