00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00032 #include "libavutil/intreadwrite.h"
00033 #include "avformat.h"
00034 #include "internal.h"
00035
00036 #define RIFF_TAG MKTAG('R', 'I', 'F', 'F')
00037 #define CDXA_TAG MKTAG('C', 'D', 'X', 'A')
00038
00039 #define RAW_CD_SECTOR_SIZE 2352
00040 #define RAW_CD_SECTOR_DATA_SIZE 2304
00041 #define VIDEO_DATA_CHUNK_SIZE 0x7E0
00042 #define VIDEO_DATA_HEADER_SIZE 0x38
00043 #define RIFF_HEADER_SIZE 0x2C
00044
00045 #define CDXA_TYPE_MASK 0x0E
00046 #define CDXA_TYPE_DATA 0x08
00047 #define CDXA_TYPE_AUDIO 0x04
00048 #define CDXA_TYPE_VIDEO 0x02
00049
00050 #define STR_MAGIC (0x80010160)
00051
00052 typedef struct StrChannel {
00053
00054 int video_stream_index;
00055 AVPacket tmp_pkt;
00056
00057
00058 int audio_stream_index;
00059 } StrChannel;
00060
00061 typedef struct StrDemuxContext {
00062
00063
00064 StrChannel channels[32];
00065 } StrDemuxContext;
00066
00067 static const uint8_t sync_header[12] = {0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00};
00068
00069 static int str_probe(AVProbeData *p)
00070 {
00071 uint8_t *sector= p->buf;
00072 uint8_t *end= sector + p->buf_size;
00073 int aud=0, vid=0;
00074
00075 if (p->buf_size < RAW_CD_SECTOR_SIZE)
00076 return 0;
00077
00078 if ((AV_RL32(&p->buf[0]) == RIFF_TAG) &&
00079 (AV_RL32(&p->buf[8]) == CDXA_TAG)) {
00080
00081
00082 sector += RIFF_HEADER_SIZE;
00083 }
00084
00085 while (end - sector >= RAW_CD_SECTOR_SIZE) {
00086
00087 if (memcmp(sector,sync_header,sizeof(sync_header)))
00088 return 0;
00089
00090 if (sector[0x11] >= 32)
00091 return 0;
00092
00093 switch (sector[0x12] & CDXA_TYPE_MASK) {
00094 case CDXA_TYPE_DATA:
00095 case CDXA_TYPE_VIDEO: {
00096 int current_sector = AV_RL16(§or[0x1C]);
00097 int sector_count = AV_RL16(§or[0x1E]);
00098 int frame_size = AV_RL32(§or[0x24]);
00099
00100 if(!( frame_size>=0
00101 && current_sector < sector_count
00102 && sector_count*VIDEO_DATA_CHUNK_SIZE >=frame_size)){
00103 return 0;
00104 }
00105
00106
00107
00108
00109
00110 vid++;
00111
00112
00113 }
00114 break;
00115 case CDXA_TYPE_AUDIO:
00116 if(sector[0x13]&0x2A)
00117 return 0;
00118 aud++;
00119 break;
00120 default:
00121 if(sector[0x12] & CDXA_TYPE_MASK)
00122 return 0;
00123 }
00124 sector += RAW_CD_SECTOR_SIZE;
00125 }
00126
00127
00128 if(vid+aud > 3) return 50;
00129 else if(vid+aud) return 1;
00130 else return 0;
00131 }
00132
00133 static int str_read_header(AVFormatContext *s)
00134 {
00135 AVIOContext *pb = s->pb;
00136 StrDemuxContext *str = s->priv_data;
00137 unsigned char sector[RAW_CD_SECTOR_SIZE];
00138 int start;
00139 int i;
00140
00141
00142 if (avio_read(pb, sector, RIFF_HEADER_SIZE) != RIFF_HEADER_SIZE)
00143 return AVERROR(EIO);
00144 if (AV_RL32(§or[0]) == RIFF_TAG)
00145 start = RIFF_HEADER_SIZE;
00146 else
00147 start = 0;
00148
00149 avio_seek(pb, start, SEEK_SET);
00150
00151 for(i=0; i<32; i++){
00152 str->channels[i].video_stream_index=
00153 str->channels[i].audio_stream_index= -1;
00154 }
00155
00156 s->ctx_flags |= AVFMTCTX_NOHEADER;
00157
00158 return 0;
00159 }
00160
00161 static int str_read_packet(AVFormatContext *s,
00162 AVPacket *ret_pkt)
00163 {
00164 AVIOContext *pb = s->pb;
00165 StrDemuxContext *str = s->priv_data;
00166 unsigned char sector[RAW_CD_SECTOR_SIZE];
00167 int channel;
00168 AVPacket *pkt;
00169 AVStream *st;
00170
00171 while (1) {
00172
00173 if (avio_read(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
00174 return AVERROR(EIO);
00175
00176 channel = sector[0x11];
00177 if (channel >= 32)
00178 return AVERROR_INVALIDDATA;
00179
00180 switch (sector[0x12] & CDXA_TYPE_MASK) {
00181
00182 case CDXA_TYPE_DATA:
00183 case CDXA_TYPE_VIDEO:
00184 {
00185
00186 int current_sector = AV_RL16(§or[0x1C]);
00187 int sector_count = AV_RL16(§or[0x1E]);
00188 int frame_size = AV_RL32(§or[0x24]);
00189
00190 if(!( frame_size>=0
00191 && current_sector < sector_count
00192 && sector_count*VIDEO_DATA_CHUNK_SIZE >=frame_size)){
00193 av_log(s, AV_LOG_ERROR, "Invalid parameters %d %d %d\n", current_sector, sector_count, frame_size);
00194 break;
00195 }
00196
00197 if(str->channels[channel].video_stream_index < 0){
00198
00199 st = avformat_new_stream(s, NULL);
00200 if (!st)
00201 return AVERROR(ENOMEM);
00202 avpriv_set_pts_info(st, 64, 1, 15);
00203
00204 str->channels[channel].video_stream_index = st->index;
00205
00206 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00207 st->codec->codec_id = AV_CODEC_ID_MDEC;
00208 st->codec->codec_tag = 0;
00209 st->codec->width = AV_RL16(§or[0x28]);
00210 st->codec->height = AV_RL16(§or[0x2A]);
00211 }
00212
00213
00214 pkt = &str->channels[channel].tmp_pkt;
00215
00216 if(pkt->size != sector_count*VIDEO_DATA_CHUNK_SIZE){
00217 if(pkt->data)
00218 av_log(s, AV_LOG_ERROR, "missmatching sector_count\n");
00219 av_free_packet(pkt);
00220 if (av_new_packet(pkt, sector_count*VIDEO_DATA_CHUNK_SIZE))
00221 return AVERROR(EIO);
00222
00223 pkt->pos= avio_tell(pb) - RAW_CD_SECTOR_SIZE;
00224 pkt->stream_index =
00225 str->channels[channel].video_stream_index;
00226 }
00227
00228 memcpy(pkt->data + current_sector*VIDEO_DATA_CHUNK_SIZE,
00229 sector + VIDEO_DATA_HEADER_SIZE,
00230 VIDEO_DATA_CHUNK_SIZE);
00231
00232 if (current_sector == sector_count-1) {
00233 pkt->size= frame_size;
00234 *ret_pkt = *pkt;
00235 pkt->data= NULL;
00236 pkt->size= -1;
00237 return 0;
00238 }
00239
00240 }
00241 break;
00242
00243 case CDXA_TYPE_AUDIO:
00244 if(str->channels[channel].audio_stream_index < 0){
00245 int fmt = sector[0x13];
00246
00247 st = avformat_new_stream(s, NULL);
00248 if (!st)
00249 return AVERROR(ENOMEM);
00250
00251 str->channels[channel].audio_stream_index = st->index;
00252
00253 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00254 st->codec->codec_id = AV_CODEC_ID_ADPCM_XA;
00255 st->codec->codec_tag = 0;
00256 st->codec->channels = (fmt&1)?2:1;
00257 st->codec->sample_rate = (fmt&4)?18900:37800;
00258
00259 st->codec->block_align = 128;
00260
00261 avpriv_set_pts_info(st, 64, 18 * 224 / st->codec->channels,
00262 st->codec->sample_rate);
00263 st->start_time = 0;
00264 }
00265 pkt = ret_pkt;
00266 if (av_new_packet(pkt, 2304))
00267 return AVERROR(EIO);
00268 memcpy(pkt->data,sector+24,2304);
00269
00270 pkt->stream_index =
00271 str->channels[channel].audio_stream_index;
00272 pkt->duration = 1;
00273 return 0;
00274 default:
00275 av_log(s, AV_LOG_WARNING, "Unknown sector type %02X\n", sector[0x12]);
00276
00277 break;
00278 }
00279
00280 if (url_feof(pb))
00281 return AVERROR(EIO);
00282 }
00283 }
00284
00285 static int str_read_close(AVFormatContext *s)
00286 {
00287 StrDemuxContext *str = s->priv_data;
00288 int i;
00289 for(i=0; i<32; i++){
00290 if(str->channels[i].tmp_pkt.data)
00291 av_free_packet(&str->channels[i].tmp_pkt);
00292 }
00293
00294 return 0;
00295 }
00296
00297 AVInputFormat ff_str_demuxer = {
00298 .name = "psxstr",
00299 .long_name = NULL_IF_CONFIG_SMALL("Sony Playstation STR"),
00300 .priv_data_size = sizeof(StrDemuxContext),
00301 .read_probe = str_probe,
00302 .read_header = str_read_header,
00303 .read_packet = str_read_packet,
00304 .read_close = str_read_close,
00305 .flags = AVFMT_NO_BYTE_SEEK,
00306 };