Libav 0.7.1
|
00001 /* 00002 * Sony Playstation (PSX) STR File Demuxer 00003 * Copyright (c) 2003 The ffmpeg Project 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00032 #include "libavutil/intreadwrite.h" 00033 #include "avformat.h" 00034 00035 #define RIFF_TAG MKTAG('R', 'I', 'F', 'F') 00036 #define CDXA_TAG MKTAG('C', 'D', 'X', 'A') 00037 00038 #define RAW_CD_SECTOR_SIZE 2352 00039 #define RAW_CD_SECTOR_DATA_SIZE 2304 00040 #define VIDEO_DATA_CHUNK_SIZE 0x7E0 00041 #define VIDEO_DATA_HEADER_SIZE 0x38 00042 #define RIFF_HEADER_SIZE 0x2C 00043 00044 #define CDXA_TYPE_MASK 0x0E 00045 #define CDXA_TYPE_DATA 0x08 00046 #define CDXA_TYPE_AUDIO 0x04 00047 #define CDXA_TYPE_VIDEO 0x02 00048 00049 #define STR_MAGIC (0x80010160) 00050 00051 typedef struct StrChannel { 00052 /* video parameters */ 00053 int video_stream_index; 00054 AVPacket tmp_pkt; 00055 00056 /* audio parameters */ 00057 int audio_stream_index; 00058 } StrChannel; 00059 00060 typedef struct StrDemuxContext { 00061 00062 /* a STR file can contain up to 32 channels of data */ 00063 StrChannel channels[32]; 00064 } StrDemuxContext; 00065 00066 static const char sync_header[12] = {0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00}; 00067 00068 static int str_probe(AVProbeData *p) 00069 { 00070 uint8_t *sector= p->buf; 00071 00072 if (p->buf_size < RAW_CD_SECTOR_SIZE) 00073 return 0; 00074 00075 if ((AV_RL32(&p->buf[0]) == RIFF_TAG) && 00076 (AV_RL32(&p->buf[8]) == CDXA_TAG)) { 00077 00078 /* RIFF header seen; skip 0x2C bytes */ 00079 sector += RIFF_HEADER_SIZE; 00080 } 00081 00082 /* look for CD sync header (00, 0xFF x 10, 00) */ 00083 if (memcmp(sector,sync_header,sizeof(sync_header))) 00084 return 0; 00085 00086 if(sector[0x11] >= 32) 00087 return 0; 00088 if( (sector[0x12] & CDXA_TYPE_MASK) != CDXA_TYPE_VIDEO 00089 && (sector[0x12] & CDXA_TYPE_MASK) != CDXA_TYPE_AUDIO 00090 && (sector[0x12] & CDXA_TYPE_MASK) != CDXA_TYPE_DATA) 00091 return 0; 00092 00093 /* MPEG files (like those ripped from VCDs) can also look like this; 00094 * only return half certainty */ 00095 return 50; 00096 } 00097 00098 static int str_read_header(AVFormatContext *s, 00099 AVFormatParameters *ap) 00100 { 00101 AVIOContext *pb = s->pb; 00102 StrDemuxContext *str = s->priv_data; 00103 unsigned char sector[RAW_CD_SECTOR_SIZE]; 00104 int start; 00105 int i; 00106 00107 /* skip over any RIFF header */ 00108 if (avio_read(pb, sector, RIFF_HEADER_SIZE) != RIFF_HEADER_SIZE) 00109 return AVERROR(EIO); 00110 if (AV_RL32(§or[0]) == RIFF_TAG) 00111 start = RIFF_HEADER_SIZE; 00112 else 00113 start = 0; 00114 00115 avio_seek(pb, start, SEEK_SET); 00116 00117 for(i=0; i<32; i++){ 00118 str->channels[i].video_stream_index= 00119 str->channels[i].audio_stream_index= -1; 00120 } 00121 00122 s->ctx_flags |= AVFMTCTX_NOHEADER; 00123 00124 return 0; 00125 } 00126 00127 static int str_read_packet(AVFormatContext *s, 00128 AVPacket *ret_pkt) 00129 { 00130 AVIOContext *pb = s->pb; 00131 StrDemuxContext *str = s->priv_data; 00132 unsigned char sector[RAW_CD_SECTOR_SIZE]; 00133 int channel; 00134 AVPacket *pkt; 00135 AVStream *st; 00136 00137 while (1) { 00138 00139 if (avio_read(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE) 00140 return AVERROR(EIO); 00141 00142 channel = sector[0x11]; 00143 if (channel >= 32) 00144 return AVERROR_INVALIDDATA; 00145 00146 switch (sector[0x12] & CDXA_TYPE_MASK) { 00147 00148 case CDXA_TYPE_DATA: 00149 case CDXA_TYPE_VIDEO: 00150 { 00151 00152 int current_sector = AV_RL16(§or[0x1C]); 00153 int sector_count = AV_RL16(§or[0x1E]); 00154 int frame_size = AV_RL32(§or[0x24]); 00155 00156 if(!( frame_size>=0 00157 && current_sector < sector_count 00158 && sector_count*VIDEO_DATA_CHUNK_SIZE >=frame_size)){ 00159 av_log(s, AV_LOG_ERROR, "Invalid parameters %d %d %d\n", current_sector, sector_count, frame_size); 00160 break; 00161 } 00162 00163 if(str->channels[channel].video_stream_index < 0){ 00164 /* allocate a new AVStream */ 00165 st = av_new_stream(s, 0); 00166 if (!st) 00167 return AVERROR(ENOMEM); 00168 av_set_pts_info(st, 64, 1, 15); 00169 00170 str->channels[channel].video_stream_index = st->index; 00171 00172 st->codec->codec_type = AVMEDIA_TYPE_VIDEO; 00173 st->codec->codec_id = CODEC_ID_MDEC; 00174 st->codec->codec_tag = 0; /* no fourcc */ 00175 st->codec->width = AV_RL16(§or[0x28]); 00176 st->codec->height = AV_RL16(§or[0x2A]); 00177 } 00178 00179 /* if this is the first sector of the frame, allocate a pkt */ 00180 pkt = &str->channels[channel].tmp_pkt; 00181 00182 if(pkt->size != sector_count*VIDEO_DATA_CHUNK_SIZE){ 00183 if(pkt->data) 00184 av_log(s, AV_LOG_ERROR, "missmatching sector_count\n"); 00185 av_free_packet(pkt); 00186 if (av_new_packet(pkt, sector_count*VIDEO_DATA_CHUNK_SIZE)) 00187 return AVERROR(EIO); 00188 00189 pkt->pos= avio_tell(pb) - RAW_CD_SECTOR_SIZE; 00190 pkt->stream_index = 00191 str->channels[channel].video_stream_index; 00192 } 00193 00194 memcpy(pkt->data + current_sector*VIDEO_DATA_CHUNK_SIZE, 00195 sector + VIDEO_DATA_HEADER_SIZE, 00196 VIDEO_DATA_CHUNK_SIZE); 00197 00198 if (current_sector == sector_count-1) { 00199 pkt->size= frame_size; 00200 *ret_pkt = *pkt; 00201 pkt->data= NULL; 00202 pkt->size= -1; 00203 return 0; 00204 } 00205 00206 } 00207 break; 00208 00209 case CDXA_TYPE_AUDIO: 00210 if(str->channels[channel].audio_stream_index < 0){ 00211 int fmt = sector[0x13]; 00212 /* allocate a new AVStream */ 00213 st = av_new_stream(s, 0); 00214 if (!st) 00215 return AVERROR(ENOMEM); 00216 00217 str->channels[channel].audio_stream_index = st->index; 00218 00219 st->codec->codec_type = AVMEDIA_TYPE_AUDIO; 00220 st->codec->codec_id = CODEC_ID_ADPCM_XA; 00221 st->codec->codec_tag = 0; /* no fourcc */ 00222 st->codec->channels = (fmt&1)?2:1; 00223 st->codec->sample_rate = (fmt&4)?18900:37800; 00224 // st->codec->bit_rate = 0; //FIXME; 00225 st->codec->block_align = 128; 00226 00227 av_set_pts_info(st, 64, 128, st->codec->sample_rate); 00228 } 00229 pkt = ret_pkt; 00230 if (av_new_packet(pkt, 2304)) 00231 return AVERROR(EIO); 00232 memcpy(pkt->data,sector+24,2304); 00233 00234 pkt->stream_index = 00235 str->channels[channel].audio_stream_index; 00236 return 0; 00237 break; 00238 default: 00239 av_log(s, AV_LOG_WARNING, "Unknown sector type %02X\n", sector[0x12]); 00240 /* drop the sector and move on */ 00241 break; 00242 } 00243 00244 if (pb->eof_reached) 00245 return AVERROR(EIO); 00246 } 00247 } 00248 00249 static int str_read_close(AVFormatContext *s) 00250 { 00251 StrDemuxContext *str = s->priv_data; 00252 int i; 00253 for(i=0; i<32; i++){ 00254 if(str->channels[i].tmp_pkt.data) 00255 av_free_packet(&str->channels[i].tmp_pkt); 00256 } 00257 00258 return 0; 00259 } 00260 00261 AVInputFormat ff_str_demuxer = { 00262 "psxstr", 00263 NULL_IF_CONFIG_SMALL("Sony Playstation STR format"), 00264 sizeof(StrDemuxContext), 00265 str_probe, 00266 str_read_header, 00267 str_read_packet, 00268 str_read_close, 00269 };