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.

322 lines
10KB

  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. /*st->codec->width = AV_RL16(&sector[0x28]);
  89. st->codec->height = AV_RL16(&sector[0x2A]);*/
  90. // if (current_sector == sector_count-1) {
  91. vid++;
  92. // }
  93. }
  94. break;
  95. case CDXA_TYPE_AUDIO:
  96. if(sector[0x13]&0x2A)
  97. return 0;
  98. aud++;
  99. break;
  100. default:
  101. if(sector[0x12] & CDXA_TYPE_MASK)
  102. return 0;
  103. }
  104. sector += RAW_CD_SECTOR_SIZE;
  105. }
  106. /* MPEG files (like those ripped from VCDs) can also look like this;
  107. * only return half certainty */
  108. if(vid+aud > 3) return AVPROBE_SCORE_EXTENSION;
  109. else if(vid+aud) return 1;
  110. else return 0;
  111. }
  112. static int str_read_header(AVFormatContext *s)
  113. {
  114. AVIOContext *pb = s->pb;
  115. StrDemuxContext *str = s->priv_data;
  116. unsigned char sector[RAW_CD_SECTOR_SIZE];
  117. int start;
  118. int i;
  119. /* skip over any RIFF header */
  120. if (avio_read(pb, sector, RIFF_HEADER_SIZE) != RIFF_HEADER_SIZE)
  121. return AVERROR(EIO);
  122. if (AV_RL32(&sector[0]) == RIFF_TAG)
  123. start = RIFF_HEADER_SIZE;
  124. else
  125. start = 0;
  126. avio_seek(pb, start, SEEK_SET);
  127. for(i=0; i<32; i++){
  128. str->channels[i].video_stream_index=
  129. str->channels[i].audio_stream_index= -1;
  130. }
  131. s->ctx_flags |= AVFMTCTX_NOHEADER;
  132. return 0;
  133. }
  134. static int str_read_packet(AVFormatContext *s,
  135. AVPacket *ret_pkt)
  136. {
  137. AVIOContext *pb = s->pb;
  138. StrDemuxContext *str = s->priv_data;
  139. unsigned char sector[RAW_CD_SECTOR_SIZE];
  140. int channel;
  141. AVPacket *pkt;
  142. AVStream *st;
  143. while (1) {
  144. if (avio_read(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
  145. return AVERROR(EIO);
  146. channel = sector[0x11];
  147. if (channel >= 32)
  148. return AVERROR_INVALIDDATA;
  149. switch (sector[0x12] & CDXA_TYPE_MASK) {
  150. case CDXA_TYPE_DATA:
  151. case CDXA_TYPE_VIDEO:
  152. {
  153. int current_sector = AV_RL16(&sector[0x1C]);
  154. int sector_count = AV_RL16(&sector[0x1E]);
  155. int frame_size = AV_RL32(&sector[0x24]);
  156. if(!( frame_size>=0
  157. && current_sector < sector_count
  158. && sector_count*VIDEO_DATA_CHUNK_SIZE >=frame_size)){
  159. av_log(s, AV_LOG_ERROR, "Invalid parameters %d %d %d\n", current_sector, sector_count, frame_size);
  160. break;
  161. }
  162. if(str->channels[channel].video_stream_index < 0){
  163. /* allocate a new AVStream */
  164. st = avformat_new_stream(s, NULL);
  165. if (!st)
  166. return AVERROR(ENOMEM);
  167. avpriv_set_pts_info(st, 64, 1, 15);
  168. str->channels[channel].video_stream_index = st->index;
  169. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  170. st->codec->codec_id = AV_CODEC_ID_MDEC;
  171. st->codec->codec_tag = 0; /* no fourcc */
  172. st->codec->width = AV_RL16(&sector[0x28]);
  173. st->codec->height = AV_RL16(&sector[0x2A]);
  174. }
  175. /* if this is the first sector of the frame, allocate a pkt */
  176. pkt = &str->channels[channel].tmp_pkt;
  177. if(pkt->size != sector_count*VIDEO_DATA_CHUNK_SIZE){
  178. if(pkt->data)
  179. av_log(s, AV_LOG_ERROR, "missmatching sector_count\n");
  180. av_free_packet(pkt);
  181. if (av_new_packet(pkt, sector_count*VIDEO_DATA_CHUNK_SIZE))
  182. return AVERROR(EIO);
  183. memset(pkt->data, 0, sector_count*VIDEO_DATA_CHUNK_SIZE);
  184. pkt->pos= avio_tell(pb) - RAW_CD_SECTOR_SIZE;
  185. pkt->stream_index =
  186. str->channels[channel].video_stream_index;
  187. }
  188. memcpy(pkt->data + current_sector*VIDEO_DATA_CHUNK_SIZE,
  189. sector + VIDEO_DATA_HEADER_SIZE,
  190. VIDEO_DATA_CHUNK_SIZE);
  191. if (current_sector == sector_count-1) {
  192. pkt->size= frame_size;
  193. *ret_pkt = *pkt;
  194. pkt->data= NULL;
  195. pkt->size= -1;
  196. pkt->buf = NULL;
  197. #if FF_API_DESTRUCT_PACKET
  198. FF_DISABLE_DEPRECATION_WARNINGS
  199. pkt->destruct = NULL;
  200. FF_ENABLE_DEPRECATION_WARNINGS
  201. #endif
  202. return 0;
  203. }
  204. }
  205. break;
  206. case CDXA_TYPE_AUDIO:
  207. if(str->channels[channel].audio_stream_index < 0){
  208. int fmt = sector[0x13];
  209. /* allocate a new AVStream */
  210. st = avformat_new_stream(s, NULL);
  211. if (!st)
  212. return AVERROR(ENOMEM);
  213. str->channels[channel].audio_stream_index = st->index;
  214. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  215. st->codec->codec_id = AV_CODEC_ID_ADPCM_XA;
  216. st->codec->codec_tag = 0; /* no fourcc */
  217. if (fmt & 1) {
  218. st->codec->channels = 2;
  219. st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
  220. } else {
  221. st->codec->channels = 1;
  222. st->codec->channel_layout = AV_CH_LAYOUT_MONO;
  223. }
  224. st->codec->sample_rate = (fmt&4)?18900:37800;
  225. // st->codec->bit_rate = 0; //FIXME;
  226. st->codec->block_align = 128;
  227. avpriv_set_pts_info(st, 64, 18 * 224 / st->codec->channels,
  228. st->codec->sample_rate);
  229. st->start_time = 0;
  230. }
  231. pkt = ret_pkt;
  232. if (av_new_packet(pkt, 2304))
  233. return AVERROR(EIO);
  234. memcpy(pkt->data,sector+24,2304);
  235. pkt->stream_index =
  236. str->channels[channel].audio_stream_index;
  237. pkt->duration = 1;
  238. return 0;
  239. default:
  240. av_log(s, AV_LOG_WARNING, "Unknown sector type %02X\n", sector[0x12]);
  241. /* drop the sector and move on */
  242. break;
  243. }
  244. if (url_feof(pb))
  245. return AVERROR(EIO);
  246. }
  247. }
  248. static int str_read_close(AVFormatContext *s)
  249. {
  250. StrDemuxContext *str = s->priv_data;
  251. int i;
  252. for(i=0; i<32; i++){
  253. if(str->channels[i].tmp_pkt.data)
  254. av_free_packet(&str->channels[i].tmp_pkt);
  255. }
  256. return 0;
  257. }
  258. AVInputFormat ff_str_demuxer = {
  259. .name = "psxstr",
  260. .long_name = NULL_IF_CONFIG_SMALL("Sony Playstation STR"),
  261. .priv_data_size = sizeof(StrDemuxContext),
  262. .read_probe = str_probe,
  263. .read_header = str_read_header,
  264. .read_packet = str_read_packet,
  265. .read_close = str_read_close,
  266. .flags = AVFMT_NO_BYTE_SEEK,
  267. };