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.

273 lines
8.3KB

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