2009-05-20 17:19:39 +00:00
|
|
|
/*
|
|
|
|
* QCP format (.qcp) demuxer
|
|
|
|
* Copyright (c) 2009 Kenan Gillet
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 14:45:34 +00:00
|
|
|
* @file
|
2009-05-20 17:19:39 +00:00
|
|
|
* QCP format (.qcp) demuxer
|
|
|
|
* @author Kenan Gillet
|
2011-07-14 22:45:23 +00:00
|
|
|
* @see RFC 3625: "The QCP File Format and Media Types for Speech Data"
|
2009-05-20 17:19:39 +00:00
|
|
|
* http://tools.ietf.org/html/rfc3625
|
|
|
|
*/
|
|
|
|
|
2012-04-07 21:57:25 +00:00
|
|
|
#include "libavutil/channel_layout.h"
|
2009-05-20 17:19:39 +00:00
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "avformat.h"
|
2014-12-02 10:40:01 +00:00
|
|
|
#include "riff.h"
|
2009-05-20 17:19:39 +00:00
|
|
|
|
2014-09-22 07:19:33 +00:00
|
|
|
typedef struct QCPContext {
|
2009-05-20 17:19:39 +00:00
|
|
|
uint32_t data_size; ///< size of data chunk
|
|
|
|
|
|
|
|
#define QCP_MAX_MODE 4
|
|
|
|
int16_t rates_per_mode[QCP_MAX_MODE+1]; ///< contains the packet size corresponding
|
|
|
|
///< to each mode, -1 if no size.
|
|
|
|
} QCPContext;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Last 15 out of 16 bytes of QCELP-13K GUID, as stored in the file;
|
|
|
|
* the first byte of the GUID can be either 0x41 or 0x42.
|
|
|
|
*/
|
|
|
|
static const uint8_t guid_qcelp_13k_part[15] = {
|
|
|
|
0x6d, 0x7f, 0x5e, 0x15, 0xb1, 0xd0, 0x11, 0xba,
|
|
|
|
0x91, 0x00, 0x80, 0x5f, 0xb4, 0xb9, 0x7e
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EVRC GUID as stored in the file
|
|
|
|
*/
|
|
|
|
static const uint8_t guid_evrc[16] = {
|
|
|
|
0x8d, 0xd4, 0x89, 0xe6, 0x76, 0x90, 0xb5, 0x46,
|
|
|
|
0x91, 0xef, 0x73, 0x6a, 0x51, 0x00, 0xce, 0xb4
|
|
|
|
};
|
|
|
|
|
2015-06-12 15:49:50 +00:00
|
|
|
static const uint8_t guid_4gv[16] = {
|
|
|
|
0xca, 0x29, 0xfd, 0x3c, 0x53, 0xf6, 0xf5, 0x4e,
|
|
|
|
0x90, 0xe9, 0xf4, 0x23, 0x6d, 0x59, 0x9b, 0x61
|
|
|
|
};
|
|
|
|
|
2009-05-20 17:19:39 +00:00
|
|
|
/**
|
|
|
|
* SMV GUID as stored in the file
|
|
|
|
*/
|
|
|
|
static const uint8_t guid_smv[16] = {
|
|
|
|
0x75, 0x2b, 0x7c, 0x8d, 0x97, 0xa7, 0x49, 0xed,
|
|
|
|
0x98, 0x5e, 0xd5, 0x3c, 0x8c, 0xc7, 0x5f, 0x84
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param guid contains at least 16 bytes
|
|
|
|
* @return 1 if the guid is a qcelp_13k guid, 0 otherwise
|
|
|
|
*/
|
|
|
|
static int is_qcelp_13k_guid(const uint8_t *guid) {
|
|
|
|
return (guid[0] == 0x41 || guid[0] == 0x42)
|
|
|
|
&& !memcmp(guid+1, guid_qcelp_13k_part, sizeof(guid_qcelp_13k_part));
|
|
|
|
}
|
|
|
|
|
2019-03-21 00:18:37 +00:00
|
|
|
static int qcp_probe(const AVProbeData *pd)
|
2009-05-20 17:19:39 +00:00
|
|
|
{
|
|
|
|
if (AV_RL32(pd->buf ) == AV_RL32("RIFF") &&
|
|
|
|
AV_RL64(pd->buf+8) == AV_RL64("QLCMfmt "))
|
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-12 12:20:36 +00:00
|
|
|
static int qcp_read_header(AVFormatContext *s)
|
2009-05-20 17:19:39 +00:00
|
|
|
{
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *pb = s->pb;
|
2009-05-20 17:19:39 +00:00
|
|
|
QCPContext *c = s->priv_data;
|
2011-06-18 09:43:24 +00:00
|
|
|
AVStream *st = avformat_new_stream(s, NULL);
|
2009-05-20 17:19:39 +00:00
|
|
|
uint8_t buf[16];
|
|
|
|
int i, nb_rates;
|
|
|
|
|
|
|
|
if (!st)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2011-02-21 15:43:01 +00:00
|
|
|
avio_rb32(pb); // "RIFF"
|
2011-10-09 12:12:14 +00:00
|
|
|
avio_skip(pb, 4 + 8 + 4 + 1 + 1); // filesize + "QLCMfmt " + chunk-size + major-version + minor-version
|
2009-05-20 17:19:39 +00: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 18:42:52 +00:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
st->codecpar->channels = 1;
|
|
|
|
st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
|
2011-02-21 15:43:01 +00:00
|
|
|
avio_read(pb, buf, 16);
|
2009-05-20 17:19:39 +00:00
|
|
|
if (is_qcelp_13k_guid(buf)) {
|
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 18:42:52 +00:00
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_QCELP;
|
2009-05-20 17:19:39 +00:00
|
|
|
} else if (!memcmp(buf, guid_evrc, 16)) {
|
2016-04-10 19:58:15 +00:00
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_EVRC;
|
2009-05-20 17:19:39 +00:00
|
|
|
} else if (!memcmp(buf, guid_smv, 16)) {
|
2016-04-10 19:58:15 +00:00
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_SMV;
|
2015-06-12 15:49:50 +00:00
|
|
|
} else if (!memcmp(buf, guid_4gv, 16)) {
|
2016-04-10 19:58:15 +00:00
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_4GV;
|
2009-05-20 17:19:39 +00:00
|
|
|
} else {
|
2014-12-02 10:40:01 +00:00
|
|
|
av_log(s, AV_LOG_ERROR, "Unknown codec GUID "FF_PRI_GUID".\n",
|
|
|
|
FF_ARG_GUID(buf));
|
2009-05-20 17:19:39 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 2 + 80); // codec-version + codec-name
|
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 18:42:52 +00:00
|
|
|
st->codecpar->bit_rate = avio_rl16(pb);
|
2009-05-20 17:19:39 +00:00
|
|
|
|
2011-02-21 15:43:01 +00:00
|
|
|
s->packet_size = avio_rl16(pb);
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 2); // block-size
|
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 18:42:52 +00:00
|
|
|
st->codecpar->sample_rate = avio_rl16(pb);
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 2); // sample-size
|
2009-05-20 17:19:39 +00:00
|
|
|
|
|
|
|
memset(c->rates_per_mode, -1, sizeof(c->rates_per_mode));
|
2011-02-21 15:43:01 +00:00
|
|
|
nb_rates = avio_rl32(pb);
|
2009-05-20 17:19:39 +00:00
|
|
|
nb_rates = FFMIN(nb_rates, 8);
|
|
|
|
for (i=0; i<nb_rates; i++) {
|
2011-02-21 15:43:01 +00:00
|
|
|
int size = avio_r8(pb);
|
|
|
|
int mode = avio_r8(pb);
|
2009-05-20 17:19:39 +00:00
|
|
|
if (mode > QCP_MAX_MODE) {
|
|
|
|
av_log(s, AV_LOG_WARNING, "Unknown entry %d=>%d in rate-map-table.\n ", mode, size);
|
|
|
|
} else
|
|
|
|
c->rates_per_mode[mode] = size;
|
|
|
|
}
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 16 - 2*nb_rates + 20); // empty entries of rate-map-table + reserved
|
2009-05-20 17:19:39 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qcp_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *pb = s->pb;
|
2009-05-20 17:19:39 +00:00
|
|
|
QCPContext *c = s->priv_data;
|
|
|
|
unsigned int chunk_size, tag;
|
|
|
|
|
2014-08-07 20:12:41 +00:00
|
|
|
while(!avio_feof(pb)) {
|
2009-05-20 17:19:39 +00:00
|
|
|
if (c->data_size) {
|
2011-02-21 15:43:01 +00:00
|
|
|
int pkt_size, ret, mode = avio_r8(pb);
|
2009-05-20 17:19:39 +00:00
|
|
|
|
|
|
|
if (s->packet_size) {
|
|
|
|
pkt_size = s->packet_size - 1;
|
|
|
|
} else if (mode > QCP_MAX_MODE || (pkt_size = c->rates_per_mode[mode]) < 0) {
|
|
|
|
c->data_size--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c->data_size <= pkt_size) {
|
|
|
|
av_log(s, AV_LOG_WARNING, "Data chunk is too small.\n");
|
|
|
|
pkt_size = c->data_size - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = av_get_packet(pb, pkt, pkt_size)) >= 0) {
|
|
|
|
if (pkt_size != ret)
|
|
|
|
av_log(s, AV_LOG_ERROR, "Packet size is too small.\n");
|
|
|
|
|
|
|
|
c->data_size -= pkt_size + 1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-03-03 19:11:45 +00:00
|
|
|
if (avio_tell(pb) & 1 && avio_r8(pb))
|
2009-05-20 17:19:39 +00:00
|
|
|
av_log(s, AV_LOG_WARNING, "Padding should be 0.\n");
|
|
|
|
|
2011-02-21 15:43:01 +00:00
|
|
|
tag = avio_rl32(pb);
|
|
|
|
chunk_size = avio_rl32(pb);
|
2009-05-20 17:19:39 +00:00
|
|
|
switch (tag) {
|
|
|
|
case MKTAG('v', 'r', 'a', 't'):
|
2011-02-21 15:43:01 +00:00
|
|
|
if (avio_rl32(pb)) // var-rate-flag
|
2009-05-20 17:19:39 +00:00
|
|
|
s->packet_size = 0;
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 4); // size-in-packets
|
2009-05-20 17:19:39 +00:00
|
|
|
break;
|
|
|
|
case MKTAG('d', 'a', 't', 'a'):
|
|
|
|
c->data_size = chunk_size;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, chunk_size);
|
2009-05-20 17:19:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return AVERROR_EOF;
|
|
|
|
}
|
|
|
|
|
2011-01-25 22:03:28 +00:00
|
|
|
AVInputFormat ff_qcp_demuxer = {
|
2009-05-20 17:19:39 +00:00
|
|
|
.name = "qcp",
|
2012-07-24 21:51:41 +00:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("QCP"),
|
2009-05-20 17:19:39 +00:00
|
|
|
.priv_data_size = sizeof(QCPContext),
|
|
|
|
.read_probe = qcp_probe,
|
|
|
|
.read_header = qcp_read_header,
|
|
|
|
.read_packet = qcp_read_packet,
|
|
|
|
};
|