libavformat/psxstr.c
Go to the documentation of this file.
00001 /*
00002  * Sony Playstation (PSX) STR File Demuxer
00003  * Copyright (c) 2003 The ffmpeg Project
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 #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     /* video parameters */
00054     int video_stream_index;
00055     AVPacket tmp_pkt;
00056 
00057     /* audio parameters */
00058     int audio_stream_index;
00059 } StrChannel;
00060 
00061 typedef struct StrDemuxContext {
00062 
00063     /* a STR file can contain up to 32 channels of data */
00064     StrChannel channels[32];
00065 } StrDemuxContext;
00066 
00067 static const char 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         /* RIFF header seen; skip 0x2C bytes */
00082         sector += RIFF_HEADER_SIZE;
00083     }
00084 
00085     while (end - sector >= RAW_CD_SECTOR_SIZE) {
00086         /* look for CD sync header (00, 0xFF x 10, 00) */
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(&sector[0x1C]);
00097                 int sector_count   = AV_RL16(&sector[0x1E]);
00098                 int frame_size = AV_RL32(&sector[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                 /*st->codec->width      = AV_RL16(&sector[0x28]);
00107                 st->codec->height     = AV_RL16(&sector[0x2A]);*/
00108 
00109 //                 if (current_sector == sector_count-1) {
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     /* MPEG files (like those ripped from VCDs) can also look like this;
00127      * only return half certainty */
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                            AVFormatParameters *ap)
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     /* skip over any RIFF header */
00143     if (avio_read(pb, sector, RIFF_HEADER_SIZE) != RIFF_HEADER_SIZE)
00144         return AVERROR(EIO);
00145     if (AV_RL32(&sector[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(&sector[0x1C]);
00188                 int sector_count   = AV_RL16(&sector[0x1E]);
00189                 int frame_size = AV_RL32(&sector[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                     /* allocate a new AVStream */
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   = CODEC_ID_MDEC;
00209                     st->codec->codec_tag  = 0;  /* no fourcc */
00210                     st->codec->width      = AV_RL16(&sector[0x28]);
00211                     st->codec->height     = AV_RL16(&sector[0x2A]);
00212                 }
00213 
00214                 /* if this is the first sector of the frame, allocate a pkt */
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                 /* allocate a new AVStream */
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    = CODEC_ID_ADPCM_XA;
00256                 st->codec->codec_tag   = 0;  /* no fourcc */
00257                 st->codec->channels    = (fmt&1)?2:1;
00258                 st->codec->sample_rate = (fmt&4)?18900:37800;
00259             //    st->codec->bit_rate = 0; //FIXME;
00260                 st->codec->block_align = 128;
00261 
00262                 avpriv_set_pts_info(st, 64, 128, st->codec->sample_rate);
00263             }
00264             pkt = ret_pkt;
00265             if (av_new_packet(pkt, 2304))
00266                 return AVERROR(EIO);
00267             memcpy(pkt->data,sector+24,2304);
00268 
00269             pkt->stream_index =
00270                 str->channels[channel].audio_stream_index;
00271             return 0;
00272         default:
00273             av_log(s, AV_LOG_WARNING, "Unknown sector type %02X\n", sector[0x12]);
00274             /* drop the sector and move on */
00275             break;
00276         }
00277 
00278         if (url_feof(pb))
00279             return AVERROR(EIO);
00280     }
00281 }
00282 
00283 static int str_read_close(AVFormatContext *s)
00284 {
00285     StrDemuxContext *str = s->priv_data;
00286     int i;
00287     for(i=0; i<32; i++){
00288         if(str->channels[i].tmp_pkt.data)
00289             av_free_packet(&str->channels[i].tmp_pkt);
00290     }
00291 
00292     return 0;
00293 }
00294 
00295 AVInputFormat ff_str_demuxer = {
00296     .name           = "psxstr",
00297     .long_name      = NULL_IF_CONFIG_SMALL("Sony Playstation STR format"),
00298     .priv_data_size = sizeof(StrDemuxContext),
00299     .read_probe     = str_probe,
00300     .read_header    = str_read_header,
00301     .read_packet    = str_read_packet,
00302     .read_close     = str_read_close,
00303 };