00001 /* 00002 * Maxis XA (.xa) File Demuxer 00003 * Copyright (c) 2008 Robert Marston 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 00030 #include "libavutil/intreadwrite.h" 00031 #include "avformat.h" 00032 00033 #define XA00_TAG MKTAG('X', 'A', 0, 0) 00034 #define XAI0_TAG MKTAG('X', 'A', 'I', 0) 00035 #define XAJ0_TAG MKTAG('X', 'A', 'J', 0) 00036 00037 typedef struct MaxisXADemuxContext { 00038 uint32_t out_size; 00039 uint32_t sent_bytes; 00040 uint32_t audio_frame_counter; 00041 } MaxisXADemuxContext; 00042 00043 static int xa_probe(AVProbeData *p) 00044 { 00045 switch(AV_RL32(p->buf)) { 00046 case XA00_TAG: 00047 case XAI0_TAG: 00048 case XAJ0_TAG: 00049 return AVPROBE_SCORE_MAX; 00050 } 00051 return 0; 00052 } 00053 00054 static int xa_read_header(AVFormatContext *s, 00055 AVFormatParameters *ap) 00056 { 00057 MaxisXADemuxContext *xa = s->priv_data; 00058 ByteIOContext *pb = s->pb; 00059 AVStream *st; 00060 00061 /*Set up the XA Audio Decoder*/ 00062 st = av_new_stream(s, 0); 00063 if (!st) 00064 return AVERROR(ENOMEM); 00065 00066 st->codec->codec_type = CODEC_TYPE_AUDIO; 00067 st->codec->codec_id = CODEC_ID_ADPCM_EA_MAXIS_XA; 00068 url_fskip(pb, 4); /* Skip the XA ID */ 00069 xa->out_size = get_le32(pb); 00070 url_fskip(pb, 2); /* Skip the tag */ 00071 st->codec->channels = get_le16(pb); 00072 st->codec->sample_rate = get_le32(pb); 00073 /* Value in file is average byte rate*/ 00074 st->codec->bit_rate = get_le32(pb) * 8; 00075 st->codec->block_align = get_le16(pb); 00076 st->codec->bits_per_coded_sample = get_le16(pb); 00077 00078 av_set_pts_info(st, 64, 1, st->codec->sample_rate); 00079 00080 return 0; 00081 } 00082 00083 static int xa_read_packet(AVFormatContext *s, 00084 AVPacket *pkt) 00085 { 00086 MaxisXADemuxContext *xa = s->priv_data; 00087 AVStream *st = s->streams[0]; 00088 ByteIOContext *pb = s->pb; 00089 unsigned int packet_size; 00090 int ret; 00091 00092 if(xa->sent_bytes > xa->out_size) 00093 return AVERROR(EIO); 00094 /* 1 byte header and 14 bytes worth of samples * number channels per block */ 00095 packet_size = 15*st->codec->channels; 00096 00097 ret = av_get_packet(pb, pkt, packet_size); 00098 if(ret != packet_size) 00099 return AVERROR(EIO); 00100 00101 pkt->stream_index = st->index; 00102 xa->sent_bytes += packet_size; 00103 pkt->pts = xa->audio_frame_counter; 00104 /* 14 bytes Samples per channel with 2 samples per byte */ 00105 xa->audio_frame_counter += 28 * st->codec->channels; 00106 00107 return ret; 00108 } 00109 00110 AVInputFormat xa_demuxer = { 00111 "xa", 00112 NULL_IF_CONFIG_SMALL("Maxis XA File Format"), 00113 sizeof(MaxisXADemuxContext), 00114 xa_probe, 00115 xa_read_header, 00116 xa_read_packet, 00117 };