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.

264 lines
8.1KB

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