Libav 0.7.1
|
00001 /* 00002 * Maxis XA (.xa) File Demuxer 00003 * Copyright (c) 2008 Robert Marston 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 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 int channels, srate, bits_per_sample; 00046 if (p->buf_size < 24) 00047 return 0; 00048 switch(AV_RL32(p->buf)) { 00049 case XA00_TAG: 00050 case XAI0_TAG: 00051 case XAJ0_TAG: 00052 break; 00053 default: 00054 return 0; 00055 } 00056 channels = AV_RL16(p->buf + 10); 00057 srate = AV_RL32(p->buf + 12); 00058 bits_per_sample = AV_RL16(p->buf + 22); 00059 if (!channels || channels > 8 || !srate || srate > 192000 || 00060 bits_per_sample < 4 || bits_per_sample > 32) 00061 return 0; 00062 return AVPROBE_SCORE_MAX/2; 00063 } 00064 00065 static int xa_read_header(AVFormatContext *s, 00066 AVFormatParameters *ap) 00067 { 00068 MaxisXADemuxContext *xa = s->priv_data; 00069 AVIOContext *pb = s->pb; 00070 AVStream *st; 00071 00072 /*Set up the XA Audio Decoder*/ 00073 st = av_new_stream(s, 0); 00074 if (!st) 00075 return AVERROR(ENOMEM); 00076 00077 st->codec->codec_type = AVMEDIA_TYPE_AUDIO; 00078 st->codec->codec_id = CODEC_ID_ADPCM_EA_MAXIS_XA; 00079 avio_skip(pb, 4); /* Skip the XA ID */ 00080 xa->out_size = avio_rl32(pb); 00081 avio_skip(pb, 2); /* Skip the tag */ 00082 st->codec->channels = avio_rl16(pb); 00083 st->codec->sample_rate = avio_rl32(pb); 00084 /* Value in file is average byte rate*/ 00085 st->codec->bit_rate = avio_rl32(pb) * 8; 00086 st->codec->block_align = avio_rl16(pb); 00087 st->codec->bits_per_coded_sample = avio_rl16(pb); 00088 00089 av_set_pts_info(st, 64, 1, st->codec->sample_rate); 00090 00091 return 0; 00092 } 00093 00094 static int xa_read_packet(AVFormatContext *s, 00095 AVPacket *pkt) 00096 { 00097 MaxisXADemuxContext *xa = s->priv_data; 00098 AVStream *st = s->streams[0]; 00099 AVIOContext *pb = s->pb; 00100 unsigned int packet_size; 00101 int ret; 00102 00103 if(xa->sent_bytes > xa->out_size) 00104 return AVERROR(EIO); 00105 /* 1 byte header and 14 bytes worth of samples * number channels per block */ 00106 packet_size = 15*st->codec->channels; 00107 00108 ret = av_get_packet(pb, pkt, packet_size); 00109 if(ret < 0) 00110 return ret; 00111 00112 pkt->stream_index = st->index; 00113 xa->sent_bytes += packet_size; 00114 pkt->pts = xa->audio_frame_counter; 00115 /* 14 bytes Samples per channel with 2 samples per byte */ 00116 xa->audio_frame_counter += 28 * st->codec->channels; 00117 00118 return ret; 00119 } 00120 00121 AVInputFormat ff_xa_demuxer = { 00122 "xa", 00123 NULL_IF_CONFIG_SMALL("Maxis XA File Format"), 00124 sizeof(MaxisXADemuxContext), 00125 xa_probe, 00126 xa_read_header, 00127 xa_read_packet, 00128 };