• 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 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 #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 
00073     if (p->buf_size < RAW_CD_SECTOR_SIZE)
00074         return 0;
00075 
00076     if ((AV_RL32(&p->buf[0]) == RIFF_TAG) &&
00077         (AV_RL32(&p->buf[8]) == CDXA_TAG)) {
00078 
00079         /* RIFF header seen; skip 0x2C bytes */
00080         sector += RIFF_HEADER_SIZE;
00081     }
00082 
00083     /* look for CD sync header (00, 0xFF x 10, 00) */
00084     if (memcmp(sector,sync_header,sizeof(sync_header)))
00085         return 0;
00086 
00087     if(sector[0x11] >= 32)
00088         return 0;
00089     if(   (sector[0x12] & CDXA_TYPE_MASK) != CDXA_TYPE_VIDEO
00090        && (sector[0x12] & CDXA_TYPE_MASK) != CDXA_TYPE_AUDIO
00091        && (sector[0x12] & CDXA_TYPE_MASK) != CDXA_TYPE_DATA)
00092         return 0;
00093 
00094     /* MPEG files (like those ripped from VCDs) can also look like this;
00095      * only return half certainty */
00096     return 50;
00097 }
00098 
00099 static int str_read_header(AVFormatContext *s,
00100                            AVFormatParameters *ap)
00101 {
00102     AVIOContext *pb = s->pb;
00103     StrDemuxContext *str = s->priv_data;
00104     unsigned char sector[RAW_CD_SECTOR_SIZE];
00105     int start;
00106     int i;
00107 
00108     /* skip over any RIFF header */
00109     if (avio_read(pb, sector, RIFF_HEADER_SIZE) != RIFF_HEADER_SIZE)
00110         return AVERROR(EIO);
00111     if (AV_RL32(&sector[0]) == RIFF_TAG)
00112         start = RIFF_HEADER_SIZE;
00113     else
00114         start = 0;
00115 
00116     avio_seek(pb, start, SEEK_SET);
00117 
00118     for(i=0; i<32; i++){
00119         str->channels[i].video_stream_index=
00120         str->channels[i].audio_stream_index= -1;
00121     }
00122 
00123     s->ctx_flags |= AVFMTCTX_NOHEADER;
00124 
00125     return 0;
00126 }
00127 
00128 static int str_read_packet(AVFormatContext *s,
00129                            AVPacket *ret_pkt)
00130 {
00131     AVIOContext *pb = s->pb;
00132     StrDemuxContext *str = s->priv_data;
00133     unsigned char sector[RAW_CD_SECTOR_SIZE];
00134     int channel;
00135     AVPacket *pkt;
00136     AVStream *st;
00137 
00138     while (1) {
00139 
00140         if (avio_read(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
00141             return AVERROR(EIO);
00142 
00143         channel = sector[0x11];
00144         if (channel >= 32)
00145             return AVERROR_INVALIDDATA;
00146 
00147         switch (sector[0x12] & CDXA_TYPE_MASK) {
00148 
00149         case CDXA_TYPE_DATA:
00150         case CDXA_TYPE_VIDEO:
00151             {
00152 
00153                 int current_sector = AV_RL16(&sector[0x1C]);
00154                 int sector_count   = AV_RL16(&sector[0x1E]);
00155                 int frame_size = AV_RL32(&sector[0x24]);
00156 
00157                 if(!(   frame_size>=0
00158                      && current_sector < sector_count
00159                      && sector_count*VIDEO_DATA_CHUNK_SIZE >=frame_size)){
00160                     av_log(s, AV_LOG_ERROR, "Invalid parameters %d %d %d\n", current_sector, sector_count, frame_size);
00161                     break;
00162                 }
00163 
00164                 if(str->channels[channel].video_stream_index < 0){
00165                     /* allocate a new AVStream */
00166                     st = avformat_new_stream(s, NULL);
00167                     if (!st)
00168                         return AVERROR(ENOMEM);
00169                     avpriv_set_pts_info(st, 64, 1, 15);
00170 
00171                     str->channels[channel].video_stream_index = st->index;
00172 
00173                     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00174                     st->codec->codec_id   = CODEC_ID_MDEC;
00175                     st->codec->codec_tag  = 0;  /* no fourcc */
00176                     st->codec->width      = AV_RL16(&sector[0x28]);
00177                     st->codec->height     = AV_RL16(&sector[0x2A]);
00178                 }
00179 
00180                 /* if this is the first sector of the frame, allocate a pkt */
00181                 pkt = &str->channels[channel].tmp_pkt;
00182 
00183                 if(pkt->size != sector_count*VIDEO_DATA_CHUNK_SIZE){
00184                     if(pkt->data)
00185                         av_log(s, AV_LOG_ERROR, "missmatching sector_count\n");
00186                     av_free_packet(pkt);
00187                     if (av_new_packet(pkt, sector_count*VIDEO_DATA_CHUNK_SIZE))
00188                         return AVERROR(EIO);
00189 
00190                     pkt->pos= avio_tell(pb) - RAW_CD_SECTOR_SIZE;
00191                     pkt->stream_index =
00192                         str->channels[channel].video_stream_index;
00193                 }
00194 
00195                 memcpy(pkt->data + current_sector*VIDEO_DATA_CHUNK_SIZE,
00196                        sector + VIDEO_DATA_HEADER_SIZE,
00197                        VIDEO_DATA_CHUNK_SIZE);
00198 
00199                 if (current_sector == sector_count-1) {
00200                     pkt->size= frame_size;
00201                     *ret_pkt = *pkt;
00202                     pkt->data= NULL;
00203                     pkt->size= -1;
00204                     return 0;
00205                 }
00206 
00207             }
00208             break;
00209 
00210         case CDXA_TYPE_AUDIO:
00211             if(str->channels[channel].audio_stream_index < 0){
00212                 int fmt = sector[0x13];
00213                 /* allocate a new AVStream */
00214                 st = avformat_new_stream(s, NULL);
00215                 if (!st)
00216                     return AVERROR(ENOMEM);
00217 
00218                 str->channels[channel].audio_stream_index = st->index;
00219 
00220                 st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
00221                 st->codec->codec_id    = CODEC_ID_ADPCM_XA;
00222                 st->codec->codec_tag   = 0;  /* no fourcc */
00223                 st->codec->channels    = (fmt&1)?2:1;
00224                 st->codec->sample_rate = (fmt&4)?18900:37800;
00225             //    st->codec->bit_rate = 0; //FIXME;
00226                 st->codec->block_align = 128;
00227 
00228                 avpriv_set_pts_info(st, 64, 128, st->codec->sample_rate);
00229             }
00230             pkt = ret_pkt;
00231             if (av_new_packet(pkt, 2304))
00232                 return AVERROR(EIO);
00233             memcpy(pkt->data,sector+24,2304);
00234 
00235             pkt->stream_index =
00236                 str->channels[channel].audio_stream_index;
00237             return 0;
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     .name           = "psxstr",
00263     .long_name      = NULL_IF_CONFIG_SMALL("Sony Playstation STR format"),
00264     .priv_data_size = sizeof(StrDemuxContext),
00265     .read_probe     = str_probe,
00266     .read_header    = str_read_header,
00267     .read_packet    = str_read_packet,
00268     .read_close     = str_read_close,
00269 };
Generated on Sun Apr 22 2012 21:54:08 for Libav by doxygen 1.7.1