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