2 * Copyright (c) 2011 Justin Ruggles
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "libavutil/intreadwrite.h"
31 #define BLOCK_SAMPLES 32
33 typedef struct ADXDemuxerContext {
37 static int adx_probe(const AVProbeData *p)
40 if (AV_RB16(p->buf) != 0x8000)
42 offset = AV_RB16(&p->buf[2]);
44 || offset > p->buf_size - 4
45 || memcmp(p->buf + offset - 2, "(c)CRI", 6))
47 return AVPROBE_SCORE_MAX * 3 / 4;
50 static int adx_read_packet(AVFormatContext *s, AVPacket *pkt)
52 ADXDemuxerContext *c = s->priv_data;
53 AVCodecParameters *par = s->streams[0]->codecpar;
56 if (par->channels <= 0) {
57 av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n", par->channels);
58 return AVERROR_INVALIDDATA;
61 size = BLOCK_SIZE * par->channels;
63 pkt->pos = avio_tell(s->pb);
64 pkt->stream_index = 0;
66 ret = av_get_packet(s->pb, pkt, size);
68 return ret < 0 ? ret : AVERROR(EIO);
70 if (AV_RB16(pkt->data) & 0x8000) {
75 pkt->pts = (pkt->pos - c->header_size) / size;
80 static int adx_read_header(AVFormatContext *s)
82 ADXDemuxerContext *c = s->priv_data;
83 AVCodecParameters *par;
85 AVStream *st = avformat_new_stream(s, NULL);
87 return AVERROR(ENOMEM);
88 par = s->streams[0]->codecpar;
90 if (avio_rb16(s->pb) != 0x8000)
91 return AVERROR_INVALIDDATA;
92 c->header_size = avio_rb16(s->pb) + 4;
93 avio_seek(s->pb, -4, SEEK_CUR);
95 if ((ret = ff_get_extradata(s, par, s->pb, c->header_size)) < 0)
98 if (par->extradata_size < 12) {
99 av_log(s, AV_LOG_ERROR, "Invalid extradata size.\n");
100 return AVERROR_INVALIDDATA;
102 par->channels = AV_RB8 (par->extradata + 7);
103 par->sample_rate = AV_RB32(par->extradata + 8);
105 if (par->channels <= 0) {
106 av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n", par->channels);
107 return AVERROR_INVALIDDATA;
110 if (par->sample_rate <= 0) {
111 av_log(s, AV_LOG_ERROR, "Invalid sample rate %d\n", par->sample_rate);
112 return AVERROR_INVALIDDATA;
115 par->codec_type = AVMEDIA_TYPE_AUDIO;
116 par->codec_id = s->iformat->raw_codec_id;
117 par->bit_rate = (int64_t)par->sample_rate * par->channels * BLOCK_SIZE * 8LL / BLOCK_SAMPLES;
119 avpriv_set_pts_info(st, 64, BLOCK_SAMPLES, par->sample_rate);
124 AVInputFormat ff_adx_demuxer = {
126 .long_name = NULL_IF_CONFIG_SMALL("CRI ADX"),
127 .read_probe = adx_probe,
128 .priv_data_size = sizeof(ADXDemuxerContext),
129 .read_header = adx_read_header,
130 .read_packet = adx_read_packet,
132 .raw_codec_id = AV_CODEC_ID_ADPCM_ADX,
133 .flags = AVFMT_GENERIC_INDEX,