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.

282 lines
8.9KB

  1. /*
  2. * Sony Playstation (PSX) STR File Demuxer
  3. * Copyright (c) 2003 The FFmpeg project
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 char sync_header[12] = {0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00};
  59. static int str_probe(AVProbeData *p)
  60. {
  61. uint8_t *sector= p->buf;
  62. if (p->buf_size < RAW_CD_SECTOR_SIZE)
  63. return 0;
  64. if ((AV_RL32(&p->buf[0]) == RIFF_TAG) &&
  65. (AV_RL32(&p->buf[8]) == CDXA_TAG)) {
  66. /* RIFF header seen; skip 0x2C bytes */
  67. sector += RIFF_HEADER_SIZE;
  68. }
  69. /* look for CD sync header (00, 0xFF x 10, 00) */
  70. if (memcmp(sector,sync_header,sizeof(sync_header)))
  71. return 0;
  72. if(sector[0x11] >= 32)
  73. return 0;
  74. if( (sector[0x12] & CDXA_TYPE_MASK) != CDXA_TYPE_VIDEO
  75. && (sector[0x12] & CDXA_TYPE_MASK) != CDXA_TYPE_AUDIO
  76. && (sector[0x12] & CDXA_TYPE_MASK) != CDXA_TYPE_DATA)
  77. return 0;
  78. /* MPEG files (like those ripped from VCDs) can also look like this;
  79. * only return half certainty */
  80. return AVPROBE_SCORE_EXTENSION;
  81. }
  82. static int str_read_header(AVFormatContext *s)
  83. {
  84. AVIOContext *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 (avio_read(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. avio_seek(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. AVIOContext *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 (avio_read(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 = avformat_new_stream(s, NULL);
  135. if (!st)
  136. return AVERROR(ENOMEM);
  137. avpriv_set_pts_info(st, 64, 1, 15);
  138. str->channels[channel].video_stream_index = st->index;
  139. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  140. st->codecpar->codec_id = AV_CODEC_ID_MDEC;
  141. st->codecpar->codec_tag = 0; /* no fourcc */
  142. st->codecpar->width = AV_RL16(&sector[0x28]);
  143. st->codecpar->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_packet_unref(pkt);
  151. if (av_new_packet(pkt, sector_count*VIDEO_DATA_CHUNK_SIZE))
  152. return AVERROR(EIO);
  153. pkt->pos= avio_tell(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. pkt->buf = NULL;
  166. return 0;
  167. }
  168. }
  169. break;
  170. case CDXA_TYPE_AUDIO:
  171. if(str->channels[channel].audio_stream_index < 0){
  172. int fmt = sector[0x13];
  173. /* allocate a new AVStream */
  174. st = avformat_new_stream(s, NULL);
  175. if (!st)
  176. return AVERROR(ENOMEM);
  177. str->channels[channel].audio_stream_index = st->index;
  178. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  179. st->codecpar->codec_id = AV_CODEC_ID_ADPCM_XA;
  180. st->codecpar->codec_tag = 0; /* no fourcc */
  181. if (fmt & 1) {
  182. st->codecpar->channels = 2;
  183. st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
  184. } else {
  185. st->codecpar->channels = 1;
  186. st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  187. }
  188. st->codecpar->sample_rate = (fmt&4)?18900:37800;
  189. // st->codecpar->bit_rate = 0; //FIXME;
  190. st->codecpar->block_align = 128;
  191. avpriv_set_pts_info(st, 64, 18 * 224 / st->codecpar->channels,
  192. st->codecpar->sample_rate);
  193. st->start_time = 0;
  194. }
  195. pkt = ret_pkt;
  196. if (av_new_packet(pkt, 2304))
  197. return AVERROR(EIO);
  198. memcpy(pkt->data,sector+24,2304);
  199. pkt->stream_index =
  200. str->channels[channel].audio_stream_index;
  201. pkt->duration = 1;
  202. return 0;
  203. default:
  204. av_log(s, AV_LOG_WARNING, "Unknown sector type %02X\n", sector[0x12]);
  205. /* drop the sector and move on */
  206. break;
  207. }
  208. if (pb->eof_reached)
  209. return AVERROR(EIO);
  210. }
  211. }
  212. static int str_read_close(AVFormatContext *s)
  213. {
  214. StrDemuxContext *str = s->priv_data;
  215. int i;
  216. for(i=0; i<32; i++){
  217. if(str->channels[i].tmp_pkt.data)
  218. av_packet_unref(&str->channels[i].tmp_pkt);
  219. }
  220. return 0;
  221. }
  222. AVInputFormat ff_str_demuxer = {
  223. .name = "psxstr",
  224. .long_name = NULL_IF_CONFIG_SMALL("Sony Playstation STR"),
  225. .priv_data_size = sizeof(StrDemuxContext),
  226. .read_probe = str_probe,
  227. .read_header = str_read_header,
  228. .read_packet = str_read_packet,
  229. .read_close = str_read_close,
  230. .flags = AVFMT_NO_BYTE_SEEK,
  231. };