• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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 
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     uint8_t *end= sector + p->buf_size;
00072     int aud=0, vid=0;
00073 
00074     if (p->buf_size < RAW_CD_SECTOR_SIZE)
00075         return 0;
00076 
00077     if ((AV_RL32(&p->buf[0]) == RIFF_TAG) &&
00078         (AV_RL32(&p->buf[8]) == CDXA_TAG)) {
00079 
00080         /* RIFF header seen; skip 0x2C bytes */
00081         sector += RIFF_HEADER_SIZE;
00082     }
00083 
00084     while (end - sector >= RAW_CD_SECTOR_SIZE) {
00085         /* look for CD sync header (00, 0xFF x 10, 00) */
00086         if (memcmp(sector,sync_header,sizeof(sync_header)))
00087             return 0;
00088 
00089         if (sector[0x11] >= 32)
00090             return 0;
00091 
00092         switch (sector[0x12] & CDXA_TYPE_MASK) {
00093         case CDXA_TYPE_DATA:
00094         case CDXA_TYPE_VIDEO: {
00095                 int current_sector = AV_RL16(&sector[0x1C]);
00096                 int sector_count   = AV_RL16(&sector[0x1E]);
00097                 int frame_size = AV_RL32(&sector[0x24]);
00098 
00099                 if(!(   frame_size>=0
00100                      && current_sector < sector_count
00101                      && sector_count*VIDEO_DATA_CHUNK_SIZE >=frame_size)){
00102                     return 0;
00103                 }
00104 
00105                 /*st->codec->width      = AV_RL16(&sector[0x28]);
00106                 st->codec->height     = AV_RL16(&sector[0x2A]);*/
00107 
00108 //                 if (current_sector == sector_count-1) {
00109                     vid++;
00110 //                 }
00111 
00112             }
00113             break;
00114         case CDXA_TYPE_AUDIO:
00115             if(sector[0x13]&0x2A)
00116                 return 0;
00117             aud++;
00118             break;
00119         default:
00120             if(sector[0x12] & CDXA_TYPE_MASK)
00121                 return 0;
00122         }
00123         sector += RAW_CD_SECTOR_SIZE;
00124     }
00125     /* MPEG files (like those ripped from VCDs) can also look like this;
00126      * only return half certainty */
00127     if(vid+aud > 3)  return 50;
00128     else if(vid+aud) return 1;
00129     else             return 0;
00130 }
00131 
00132 static int str_read_header(AVFormatContext *s,
00133                            AVFormatParameters *ap)
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     /* skip over any RIFF header */
00142     if (avio_read(pb, sector, RIFF_HEADER_SIZE) != RIFF_HEADER_SIZE)
00143         return AVERROR(EIO);
00144     if (AV_RL32(&sector[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(&sector[0x1C]);
00187                 int sector_count   = AV_RL16(&sector[0x1E]);
00188                 int frame_size = AV_RL32(&sector[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                     /* allocate a new AVStream */
00199                     st = av_new_stream(s, 0);
00200                     if (!st)
00201                         return AVERROR(ENOMEM);
00202                     av_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   = CODEC_ID_MDEC;
00208                     st->codec->codec_tag  = 0;  /* no fourcc */
00209                     st->codec->width      = AV_RL16(&sector[0x28]);
00210                     st->codec->height     = AV_RL16(&sector[0x2A]);
00211                 }
00212 
00213                 /* if this is the first sector of the frame, allocate a pkt */
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                 /* allocate a new AVStream */
00247                 st = av_new_stream(s, 0);
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    = CODEC_ID_ADPCM_XA;
00255                 st->codec->codec_tag   = 0;  /* no fourcc */
00256                 st->codec->channels    = (fmt&1)?2:1;
00257                 st->codec->sample_rate = (fmt&4)?18900:37800;
00258             //    st->codec->bit_rate = 0; //FIXME;
00259                 st->codec->block_align = 128;
00260 
00261                 av_set_pts_info(st, 64, 128, st->codec->sample_rate);
00262             }
00263             pkt = ret_pkt;
00264             if (av_new_packet(pkt, 2304))
00265                 return AVERROR(EIO);
00266             memcpy(pkt->data,sector+24,2304);
00267 
00268             pkt->stream_index =
00269                 str->channels[channel].audio_stream_index;
00270             return 0;
00271             break;
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     "psxstr",
00297     NULL_IF_CONFIG_SMALL("Sony Playstation STR format"),
00298     sizeof(StrDemuxContext),
00299     str_probe,
00300     str_read_header,
00301     str_read_packet,
00302     str_read_close,
00303 };

Generated on Wed Apr 11 2012 07:31:36 for FFmpeg by  doxygen 1.7.1