2011-11-21 10:34:18 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 Justin Ruggles
|
|
|
|
*
|
2013-08-16 00:12:51 +03:00
|
|
|
* This file is part of FFmpeg.
|
2011-11-21 10:34:18 +03:00
|
|
|
*
|
2013-08-16 00:12:51 +03:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2011-11-21 10:34:18 +03:00
|
|
|
* 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.
|
|
|
|
*
|
2013-08-16 00:12:51 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2011-11-21 10:34:18 +03:00
|
|
|
* 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
|
2013-08-16 00:12:51 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2011-11-21 10:34:18 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* CRI ADX demuxer
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "avformat.h"
|
2011-11-29 21:28:15 +03:00
|
|
|
#include "internal.h"
|
2011-11-21 10:34:18 +03:00
|
|
|
|
|
|
|
#define BLOCK_SIZE 18
|
|
|
|
#define BLOCK_SAMPLES 32
|
|
|
|
|
|
|
|
typedef struct ADXDemuxerContext {
|
|
|
|
int header_size;
|
|
|
|
} ADXDemuxerContext;
|
|
|
|
|
|
|
|
static int adx_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
ADXDemuxerContext *c = s->priv_data;
|
|
|
|
AVCodecContext *avctx = s->streams[0]->codec;
|
|
|
|
int ret, size;
|
|
|
|
|
2015-02-25 23:55:44 +02:00
|
|
|
if (avctx->channels <= 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n", avctx->channels);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2011-11-21 10:34:18 +03:00
|
|
|
size = BLOCK_SIZE * avctx->channels;
|
|
|
|
|
|
|
|
pkt->pos = avio_tell(s->pb);
|
|
|
|
pkt->stream_index = 0;
|
|
|
|
|
|
|
|
ret = av_get_packet(s->pb, pkt, size);
|
|
|
|
if (ret != size) {
|
2015-10-23 11:11:31 +02:00
|
|
|
av_packet_unref(pkt);
|
2011-11-21 10:34:18 +03:00
|
|
|
return ret < 0 ? ret : AVERROR(EIO);
|
|
|
|
}
|
|
|
|
if (AV_RB16(pkt->data) & 0x8000) {
|
2015-10-23 11:11:31 +02:00
|
|
|
av_packet_unref(pkt);
|
2011-11-21 10:34:18 +03:00
|
|
|
return AVERROR_EOF;
|
|
|
|
}
|
|
|
|
pkt->size = size;
|
|
|
|
pkt->duration = 1;
|
|
|
|
pkt->pts = (pkt->pos - c->header_size) / size;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-12 15:20:36 +03:00
|
|
|
static int adx_read_header(AVFormatContext *s)
|
2011-11-21 10:34:18 +03:00
|
|
|
{
|
|
|
|
ADXDemuxerContext *c = s->priv_data;
|
|
|
|
AVCodecContext *avctx;
|
|
|
|
|
|
|
|
AVStream *st = avformat_new_stream(s, NULL);
|
|
|
|
if (!st)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
avctx = s->streams[0]->codec;
|
|
|
|
|
|
|
|
if (avio_rb16(s->pb) != 0x8000)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
c->header_size = avio_rb16(s->pb) + 4;
|
|
|
|
avio_seek(s->pb, -4, SEEK_CUR);
|
|
|
|
|
2013-12-25 18:37:42 +03:00
|
|
|
if (ff_get_extradata(avctx, s->pb, c->header_size) < 0)
|
2011-11-21 10:34:18 +03:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2014-07-03 22:01:59 +03:00
|
|
|
if (avctx->extradata_size < 12) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Invalid extradata size.\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2015-02-26 02:06:57 +02:00
|
|
|
avctx->channels = AV_RB8(avctx->extradata + 7);
|
2014-07-03 22:01:59 +03:00
|
|
|
avctx->sample_rate = AV_RB32(avctx->extradata + 8);
|
2011-11-21 10:34:18 +03:00
|
|
|
|
2015-02-26 02:06:57 +02:00
|
|
|
if (avctx->channels <= 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n", avctx->channels);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2011-11-21 10:34:18 +03:00
|
|
|
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
|
2012-01-31 09:50:31 +03:00
|
|
|
st->codec->codec_id = s->iformat->raw_codec_id;
|
2011-11-21 10:34:18 +03:00
|
|
|
|
2011-11-29 21:28:15 +03:00
|
|
|
avpriv_set_pts_info(st, 64, BLOCK_SAMPLES, avctx->sample_rate);
|
2011-11-21 10:34:18 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVInputFormat ff_adx_demuxer = {
|
|
|
|
.name = "adx",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("CRI ADX"),
|
|
|
|
.priv_data_size = sizeof(ADXDemuxerContext),
|
|
|
|
.read_header = adx_read_header,
|
|
|
|
.read_packet = adx_read_packet,
|
|
|
|
.extensions = "adx",
|
2012-08-05 12:11:04 +03:00
|
|
|
.raw_codec_id = AV_CODEC_ID_ADPCM_ADX,
|
2011-12-19 18:12:29 +03:00
|
|
|
.flags = AVFMT_GENERIC_INDEX,
|
2011-11-21 10:34:18 +03:00
|
|
|
};
|