00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00026 #include "libavutil/intreadwrite.h"
00027 #include "libavcodec/adx.h"
00028 #include "avformat.h"
00029 #include "internal.h"
00030 
00031 #define BLOCK_SIZE    18
00032 #define BLOCK_SAMPLES 32
00033 
00034 typedef struct ADXDemuxerContext {
00035     int header_size;
00036 } ADXDemuxerContext;
00037 
00038 static int adx_read_packet(AVFormatContext *s, AVPacket *pkt)
00039 {
00040     ADXDemuxerContext *c = s->priv_data;
00041     AVCodecContext *avctx = s->streams[0]->codec;
00042     int ret, size;
00043 
00044     size = BLOCK_SIZE * avctx->channels;
00045 
00046     pkt->pos = avio_tell(s->pb);
00047     pkt->stream_index = 0;
00048 
00049     ret = av_get_packet(s->pb, pkt, size);
00050     if (ret != size) {
00051         av_free_packet(pkt);
00052         return ret < 0 ? ret : AVERROR(EIO);
00053     }
00054     if (AV_RB16(pkt->data) & 0x8000) {
00055         av_free_packet(pkt);
00056         return AVERROR_EOF;
00057     }
00058     pkt->size     = size;
00059     pkt->duration = 1;
00060     pkt->pts      = (pkt->pos - c->header_size) / size;
00061 
00062     return 0;
00063 }
00064 
00065 static int adx_read_header(AVFormatContext *s)
00066 {
00067     ADXDemuxerContext *c = s->priv_data;
00068     AVCodecContext *avctx;
00069     int ret;
00070 
00071     AVStream *st = avformat_new_stream(s, NULL);
00072     if (!st)
00073         return AVERROR(ENOMEM);
00074     avctx = s->streams[0]->codec;
00075 
00076     if (avio_rb16(s->pb) != 0x8000)
00077         return AVERROR_INVALIDDATA;
00078     c->header_size = avio_rb16(s->pb) + 4;
00079     avio_seek(s->pb, -4, SEEK_CUR);
00080 
00081     avctx->extradata = av_mallocz(c->header_size + FF_INPUT_BUFFER_PADDING_SIZE);
00082     if (!avctx->extradata)
00083         return AVERROR(ENOMEM);
00084     if (avio_read(s->pb, avctx->extradata, c->header_size) < c->header_size) {
00085         av_freep(&avctx->extradata);
00086         return AVERROR(EIO);
00087     }
00088     avctx->extradata_size = c->header_size;
00089 
00090     ret = avpriv_adx_decode_header(avctx, avctx->extradata,
00091                                    avctx->extradata_size, &c->header_size,
00092                                    NULL);
00093     if (ret)
00094         return ret;
00095 
00096     st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
00097     st->codec->codec_id    = s->iformat->raw_codec_id;
00098 
00099     avpriv_set_pts_info(st, 64, BLOCK_SAMPLES, avctx->sample_rate);
00100 
00101     return 0;
00102 }
00103 
00104 AVInputFormat ff_adx_demuxer = {
00105     .name           = "adx",
00106     .long_name      = NULL_IF_CONFIG_SMALL("CRI ADX"),
00107     .priv_data_size = sizeof(ADXDemuxerContext),
00108     .read_header    = adx_read_header,
00109     .read_packet    = adx_read_packet,
00110     .extensions     = "adx",
00111     .raw_codec_id   = AV_CODEC_ID_ADPCM_ADX,
00112     .flags          = AVFMT_GENERIC_INDEX,
00113 };