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.

311 lines
9.9KB

  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
  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 "libavutil/channel_layout.h"
  31. #include "libavutil/internal.h"
  32. #include "libavutil/intreadwrite.h"
  33. #include "avformat.h"
  34. #include "internal.h"
  35. #define RIFF_TAG MKTAG('R', 'I', 'F', 'F')
  36. #define CDXA_TAG MKTAG('C', 'D', 'X', 'A')
  37. #define RAW_CD_SECTOR_SIZE 2352
  38. #define RAW_CD_SECTOR_DATA_SIZE 2304
  39. #define VIDEO_DATA_CHUNK_SIZE 0x7E0
  40. #define VIDEO_DATA_HEADER_SIZE 0x38
  41. #define RIFF_HEADER_SIZE 0x2C
  42. #define CDXA_TYPE_MASK 0x0E
  43. #define CDXA_TYPE_DATA 0x08
  44. #define CDXA_TYPE_AUDIO 0x04
  45. #define CDXA_TYPE_VIDEO 0x02
  46. #define STR_MAGIC (0x80010160)
  47. typedef struct StrChannel {
  48. /* video parameters */
  49. int video_stream_index;
  50. AVPacket tmp_pkt;
  51. /* audio parameters */
  52. int audio_stream_index;
  53. } StrChannel;
  54. typedef struct StrDemuxContext {
  55. /* a STR file can contain up to 32 channels of data */
  56. StrChannel channels[32];
  57. } StrDemuxContext;
  58. static const uint8_t sync_header[12] = {0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00};
  59. static int str_probe(AVProbeData *p)
  60. {
  61. const uint8_t *sector= p->buf;
  62. const uint8_t *end= sector + p->buf_size;
  63. int aud=0, vid=0;
  64. if (p->buf_size < RAW_CD_SECTOR_SIZE)
  65. return 0;
  66. if ((AV_RL32(&p->buf[0]) == RIFF_TAG) &&
  67. (AV_RL32(&p->buf[8]) == CDXA_TAG)) {
  68. /* RIFF header seen; skip 0x2C bytes */
  69. sector += RIFF_HEADER_SIZE;
  70. }
  71. while (end - sector >= RAW_CD_SECTOR_SIZE) {
  72. /* look for CD sync header (00, 0xFF x 10, 00) */
  73. if (memcmp(sector,sync_header,sizeof(sync_header)))
  74. return 0;
  75. if (sector[0x11] >= 32)
  76. return 0;
  77. switch (sector[0x12] & CDXA_TYPE_MASK) {
  78. case CDXA_TYPE_DATA:
  79. case CDXA_TYPE_VIDEO: {
  80. int current_sector = AV_RL16(&sector[0x1C]);
  81. int sector_count = AV_RL16(&sector[0x1E]);
  82. int frame_size = AV_RL32(&sector[0x24]);
  83. if(!( frame_size>=0
  84. && current_sector < sector_count
  85. && sector_count*VIDEO_DATA_CHUNK_SIZE >=frame_size)){
  86. return 0;
  87. }
  88. vid++;
  89. }
  90. break;
  91. case CDXA_TYPE_AUDIO:
  92. if(sector[0x13]&0x2A)
  93. return 0;
  94. aud++;
  95. break;
  96. default:
  97. if(sector[0x12] & CDXA_TYPE_MASK)
  98. return 0;
  99. }
  100. sector += RAW_CD_SECTOR_SIZE;
  101. }
  102. /* MPEG files (like those ripped from VCDs) can also look like this;
  103. * only return half certainty */
  104. if(vid+aud > 3) return AVPROBE_SCORE_EXTENSION;
  105. else if(vid+aud) return 1;
  106. else return 0;
  107. }
  108. static int str_read_header(AVFormatContext *s)
  109. {
  110. AVIOContext *pb = s->pb;
  111. StrDemuxContext *str = s->priv_data;
  112. unsigned char sector[RAW_CD_SECTOR_SIZE];
  113. int start;
  114. int i;
  115. /* skip over any RIFF header */
  116. if (avio_read(pb, sector, RIFF_HEADER_SIZE) != RIFF_HEADER_SIZE)
  117. return AVERROR(EIO);
  118. if (AV_RL32(&sector[0]) == RIFF_TAG)
  119. start = RIFF_HEADER_SIZE;
  120. else
  121. start = 0;
  122. avio_seek(pb, start, SEEK_SET);
  123. for(i=0; i<32; i++){
  124. str->channels[i].video_stream_index=
  125. str->channels[i].audio_stream_index= -1;
  126. }
  127. s->ctx_flags |= AVFMTCTX_NOHEADER;
  128. return 0;
  129. }
  130. static int str_read_packet(AVFormatContext *s,
  131. AVPacket *ret_pkt)
  132. {
  133. AVIOContext *pb = s->pb;
  134. StrDemuxContext *str = s->priv_data;
  135. unsigned char sector[RAW_CD_SECTOR_SIZE];
  136. int channel;
  137. AVPacket *pkt;
  138. AVStream *st;
  139. while (1) {
  140. if (avio_read(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
  141. return AVERROR(EIO);
  142. channel = sector[0x11];
  143. if (channel >= 32)
  144. return AVERROR_INVALIDDATA;
  145. switch (sector[0x12] & CDXA_TYPE_MASK) {
  146. case CDXA_TYPE_DATA:
  147. case CDXA_TYPE_VIDEO:
  148. {
  149. int current_sector = AV_RL16(&sector[0x1C]);
  150. int sector_count = AV_RL16(&sector[0x1E]);
  151. int frame_size = AV_RL32(&sector[0x24]);
  152. if(!( frame_size>=0
  153. && current_sector < sector_count
  154. && sector_count*VIDEO_DATA_CHUNK_SIZE >=frame_size)){
  155. av_log(s, AV_LOG_ERROR, "Invalid parameters %d %d %d\n", current_sector, sector_count, frame_size);
  156. break;
  157. }
  158. if(str->channels[channel].video_stream_index < 0){
  159. /* allocate a new AVStream */
  160. st = avformat_new_stream(s, NULL);
  161. if (!st)
  162. return AVERROR(ENOMEM);
  163. avpriv_set_pts_info(st, 64, 1, 15);
  164. str->channels[channel].video_stream_index = st->index;
  165. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  166. st->codecpar->codec_id = AV_CODEC_ID_MDEC;
  167. st->codecpar->codec_tag = 0; /* no fourcc */
  168. st->codecpar->width = AV_RL16(&sector[0x28]);
  169. st->codecpar->height = AV_RL16(&sector[0x2A]);
  170. }
  171. /* if this is the first sector of the frame, allocate a pkt */
  172. pkt = &str->channels[channel].tmp_pkt;
  173. if(pkt->size != sector_count*VIDEO_DATA_CHUNK_SIZE){
  174. if(pkt->data)
  175. av_log(s, AV_LOG_ERROR, "mismatching sector_count\n");
  176. av_packet_unref(pkt);
  177. if (av_new_packet(pkt, sector_count*VIDEO_DATA_CHUNK_SIZE))
  178. return AVERROR(EIO);
  179. memset(pkt->data, 0, sector_count*VIDEO_DATA_CHUNK_SIZE);
  180. pkt->pos= avio_tell(pb) - RAW_CD_SECTOR_SIZE;
  181. pkt->stream_index =
  182. str->channels[channel].video_stream_index;
  183. }
  184. memcpy(pkt->data + current_sector*VIDEO_DATA_CHUNK_SIZE,
  185. sector + VIDEO_DATA_HEADER_SIZE,
  186. VIDEO_DATA_CHUNK_SIZE);
  187. if (current_sector == sector_count-1) {
  188. pkt->size= frame_size;
  189. *ret_pkt = *pkt;
  190. pkt->data= NULL;
  191. pkt->size= -1;
  192. pkt->buf = NULL;
  193. return 0;
  194. }
  195. }
  196. break;
  197. case CDXA_TYPE_AUDIO:
  198. if(str->channels[channel].audio_stream_index < 0){
  199. int fmt = sector[0x13];
  200. /* allocate a new AVStream */
  201. st = avformat_new_stream(s, NULL);
  202. if (!st)
  203. return AVERROR(ENOMEM);
  204. str->channels[channel].audio_stream_index = st->index;
  205. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  206. st->codecpar->codec_id = AV_CODEC_ID_ADPCM_XA;
  207. st->codecpar->codec_tag = 0; /* no fourcc */
  208. if (fmt & 1) {
  209. st->codecpar->channels = 2;
  210. st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
  211. } else {
  212. st->codecpar->channels = 1;
  213. st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  214. }
  215. st->codecpar->sample_rate = (fmt&4)?18900:37800;
  216. // st->codecpar->bit_rate = 0; //FIXME;
  217. st->codecpar->block_align = 128;
  218. avpriv_set_pts_info(st, 64, 18 * 224 / st->codecpar->channels,
  219. st->codecpar->sample_rate);
  220. st->start_time = 0;
  221. }
  222. pkt = ret_pkt;
  223. if (av_new_packet(pkt, 2304))
  224. return AVERROR(EIO);
  225. memcpy(pkt->data,sector+24,2304);
  226. pkt->stream_index =
  227. str->channels[channel].audio_stream_index;
  228. pkt->duration = 1;
  229. return 0;
  230. default:
  231. av_log(s, AV_LOG_WARNING, "Unknown sector type %02X\n", sector[0x12]);
  232. /* drop the sector and move on */
  233. break;
  234. }
  235. if (avio_feof(pb))
  236. return AVERROR(EIO);
  237. }
  238. }
  239. static int str_read_close(AVFormatContext *s)
  240. {
  241. StrDemuxContext *str = s->priv_data;
  242. int i;
  243. for(i=0; i<32; i++){
  244. if(str->channels[i].tmp_pkt.data)
  245. av_packet_unref(&str->channels[i].tmp_pkt);
  246. }
  247. return 0;
  248. }
  249. AVInputFormat ff_str_demuxer = {
  250. .name = "psxstr",
  251. .long_name = NULL_IF_CONFIG_SMALL("Sony Playstation STR"),
  252. .priv_data_size = sizeof(StrDemuxContext),
  253. .read_probe = str_probe,
  254. .read_header = str_read_header,
  255. .read_packet = str_read_packet,
  256. .read_close = str_read_close,
  257. .flags = AVFMT_NO_BYTE_SEEK,
  258. };