libavformat/adxdec.c
a17c3c7d
 /*
  * Copyright (c) 2011 Justin Ruggles
  *
d814a839
  * This file is part of FFmpeg.
a17c3c7d
  *
d814a839
  * FFmpeg is free software; you can redistribute it and/or
a17c3c7d
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
d814a839
  * FFmpeg is distributed in the hope that it will be useful,
a17c3c7d
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
d814a839
  * License along with FFmpeg; if not, write to the Free Software
a17c3c7d
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
  * @file
  * CRI ADX demuxer
  */
 
 #include "libavutil/intreadwrite.h"
 #include "avformat.h"
c3f9ebf7
 #include "internal.h"
a17c3c7d
 
 #define BLOCK_SIZE    18
 #define BLOCK_SAMPLES 32
 
 typedef struct ADXDemuxerContext {
     int header_size;
 } ADXDemuxerContext;
 
9ca5b272
 static int adx_probe(AVProbeData *p)
 {
     int offset;
     if (AV_RB16(p->buf) != 0x8000)
         return 0;
     offset = AV_RB16(&p->buf[2]);
     if (   offset < 8
         || offset > p->buf_size - 4
         || memcmp(p->buf + offset - 2, "(c)CRI", 6))
         return 0;
     return AVPROBE_SCORE_MAX * 3 / 4;
 }
 
a17c3c7d
 static int adx_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
     ADXDemuxerContext *c = s->priv_data;
9200514a
     AVCodecParameters *par = s->streams[0]->codecpar;
a17c3c7d
     int ret, size;
 
6f69f7a8
     if (par->channels <= 0) {
         av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n", par->channels);
7faa40af
         return AVERROR_INVALIDDATA;
     }
 
9200514a
     size = BLOCK_SIZE * par->channels;
a17c3c7d
 
     pkt->pos = avio_tell(s->pb);
     pkt->stream_index = 0;
 
     ret = av_get_packet(s->pb, pkt, size);
     if (ret != size) {
ce70f28a
         av_packet_unref(pkt);
a17c3c7d
         return ret < 0 ? ret : AVERROR(EIO);
     }
     if (AV_RB16(pkt->data) & 0x8000) {
ce70f28a
         av_packet_unref(pkt);
a17c3c7d
         return AVERROR_EOF;
     }
     pkt->size     = size;
     pkt->duration = 1;
     pkt->pts      = (pkt->pos - c->header_size) / size;
 
     return 0;
 }
 
6e9651d1
 static int adx_read_header(AVFormatContext *s)
a17c3c7d
 {
     ADXDemuxerContext *c = s->priv_data;
9200514a
     AVCodecParameters *par;
a17c3c7d
 
     AVStream *st = avformat_new_stream(s, NULL);
     if (!st)
         return AVERROR(ENOMEM);
9200514a
     par = s->streams[0]->codecpar;
a17c3c7d
 
     if (avio_rb16(s->pb) != 0x8000)
         return AVERROR_INVALIDDATA;
     c->header_size = avio_rb16(s->pb) + 4;
     avio_seek(s->pb, -4, SEEK_CUR);
 
323b8c95
     if (ff_get_extradata(s, par, s->pb, c->header_size) < 0)
a17c3c7d
         return AVERROR(ENOMEM);
 
9200514a
     if (par->extradata_size < 12) {
d5cf5afa
         av_log(s, AV_LOG_ERROR, "Invalid extradata size.\n");
         return AVERROR_INVALIDDATA;
     }
9200514a
     par->channels    = AV_RB8 (par->extradata + 7);
     par->sample_rate = AV_RB32(par->extradata + 8);
a17c3c7d
 
9200514a
     if (par->channels <= 0) {
         av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n", par->channels);
64ea4a05
         return AVERROR_INVALIDDATA;
     }
 
2eb05eaa
     if (par->sample_rate <= 0) {
         av_log(s, AV_LOG_ERROR, "Invalid sample rate %d\n", par->sample_rate);
         return AVERROR_INVALIDDATA;
     }
 
9200514a
     par->codec_type  = AVMEDIA_TYPE_AUDIO;
     par->codec_id    = s->iformat->raw_codec_id;
ad5807f8
     par->bit_rate    = (int64_t)par->sample_rate * par->channels * BLOCK_SIZE * 8LL / BLOCK_SAMPLES;
a17c3c7d
 
9200514a
     avpriv_set_pts_info(st, 64, BLOCK_SAMPLES, par->sample_rate);
a17c3c7d
 
     return 0;
 }
 
 AVInputFormat ff_adx_demuxer = {
     .name           = "adx",
     .long_name      = NULL_IF_CONFIG_SMALL("CRI ADX"),
9ca5b272
     .read_probe     = adx_probe,
a17c3c7d
     .priv_data_size = sizeof(ADXDemuxerContext),
     .read_header    = adx_read_header,
     .read_packet    = adx_read_packet,
     .extensions     = "adx",
36ef5369
     .raw_codec_id   = AV_CODEC_ID_ADPCM_ADX,
99baf2c7
     .flags          = AVFMT_GENERIC_INDEX,
a17c3c7d
 };