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;
|
|
|
|
|
2019-03-21 02:18:37 +02:00
|
|
|
static int adx_probe(const AVProbeData *p)
|
2016-02-01 11:51:46 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-11-21 10:34:18 +03:00
|
|
|
static int adx_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
ADXDemuxerContext *c = s->priv_data;
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
AVCodecParameters *par = s->streams[0]->codecpar;
|
2011-11-21 10:34:18 +03:00
|
|
|
int ret, size;
|
|
|
|
|
2020-09-18 12:08:42 +02:00
|
|
|
if (avio_feof(s->pb))
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
2017-03-31 13:23:50 +02:00
|
|
|
if (par->ch_layout.nb_channels <= 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n", par->ch_layout.nb_channels);
|
2015-02-25 23:55:44 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2017-03-31 13:23:50 +02:00
|
|
|
size = BLOCK_SIZE * par->ch_layout.nb_channels;
|
2011-11-21 10:34:18 +03:00
|
|
|
|
|
|
|
pkt->pos = avio_tell(s->pb);
|
|
|
|
pkt->stream_index = 0;
|
|
|
|
|
2020-09-18 12:08:42 +02:00
|
|
|
ret = av_get_packet(s->pb, pkt, size * 128);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
if ((ret % size) && ret >= size) {
|
|
|
|
size = ret - (ret % size);
|
|
|
|
av_shrink_packet(pkt, size);
|
|
|
|
pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
|
|
|
|
} else if (ret < size) {
|
|
|
|
return AVERROR(EIO);
|
|
|
|
} else {
|
|
|
|
size = ret;
|
2011-11-21 10:34:18 +03:00
|
|
|
}
|
2020-09-18 12:08:42 +02:00
|
|
|
|
2017-03-31 13:23:50 +02:00
|
|
|
pkt->duration = size / (BLOCK_SIZE * par->ch_layout.nb_channels);
|
|
|
|
pkt->pts = (pkt->pos - c->header_size) / (BLOCK_SIZE * par->ch_layout.nb_channels);
|
2011-11-21 10:34:18 +03:00
|
|
|
|
|
|
|
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;
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
AVCodecParameters *par;
|
2019-12-10 23:59:53 +02:00
|
|
|
int ret;
|
2017-03-31 13:23:50 +02:00
|
|
|
int channels;
|
|
|
|
|
2011-11-21 10:34:18 +03:00
|
|
|
AVStream *st = avformat_new_stream(s, NULL);
|
|
|
|
if (!st)
|
|
|
|
return AVERROR(ENOMEM);
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
par = s->streams[0]->codecpar;
|
2011-11-21 10:34:18 +03:00
|
|
|
|
|
|
|
if (avio_rb16(s->pb) != 0x8000)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
c->header_size = avio_rb16(s->pb) + 4;
|
|
|
|
avio_seek(s->pb, -4, SEEK_CUR);
|
|
|
|
|
2019-12-10 23:59:53 +02:00
|
|
|
if ((ret = ff_get_extradata(s, par, s->pb, c->header_size)) < 0)
|
|
|
|
return ret;
|
2011-11-21 10:34:18 +03:00
|
|
|
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
if (par->extradata_size < 12) {
|
2014-07-03 22:01:59 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "Invalid extradata size.\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2017-03-31 13:23:50 +02:00
|
|
|
channels = AV_RB8 (par->extradata + 7);
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
par->sample_rate = AV_RB32(par->extradata + 8);
|
2011-11-21 10:34:18 +03:00
|
|
|
|
2017-03-31 13:23:50 +02:00
|
|
|
if (channels <= 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n", channels);
|
2015-02-26 02:06:57 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2016-10-23 15:47:40 +02:00
|
|
|
if (par->sample_rate <= 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Invalid sample rate %d\n", par->sample_rate);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2017-03-31 13:23:50 +02:00
|
|
|
par->ch_layout.nb_channels = channels;
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
par->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
par->codec_id = s->iformat->raw_codec_id;
|
2017-03-31 13:23:50 +02:00
|
|
|
par->bit_rate = (int64_t)par->sample_rate * par->ch_layout.nb_channels * BLOCK_SIZE * 8LL / BLOCK_SAMPLES;
|
2011-11-21 10:34:18 +03:00
|
|
|
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
avpriv_set_pts_info(st, 64, BLOCK_SAMPLES, par->sample_rate);
|
2011-11-21 10:34:18 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-19 19:45:24 +02:00
|
|
|
const AVInputFormat ff_adx_demuxer = {
|
2011-11-21 10:34:18 +03:00
|
|
|
.name = "adx",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("CRI ADX"),
|
2016-02-01 11:51:46 +02:00
|
|
|
.read_probe = adx_probe,
|
2011-11-21 10:34:18 +03:00
|
|
|
.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
|
|
|
};
|