2012-10-02 13:43:19 +00:00
|
|
|
/*
|
|
|
|
* Raw TAK demuxer
|
|
|
|
* Copyright (c) 2012 Paul B Mahol
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2013-06-04 19:24:48 +00:00
|
|
|
#include "libavutil/crc.h"
|
2016-06-29 09:27:33 +00:00
|
|
|
|
2016-06-04 13:07:30 +00:00
|
|
|
#define BITSTREAM_READER_LE
|
2012-10-02 13:43:19 +00:00
|
|
|
#include "libavcodec/tak.h"
|
2016-06-04 13:07:30 +00:00
|
|
|
|
|
|
|
#include "apetag.h"
|
2012-10-02 13:43:19 +00:00
|
|
|
#include "avformat.h"
|
2013-06-04 19:24:48 +00:00
|
|
|
#include "avio_internal.h"
|
2012-10-02 13:43:19 +00:00
|
|
|
#include "internal.h"
|
|
|
|
#include "rawdec.h"
|
|
|
|
|
|
|
|
typedef struct TAKDemuxContext {
|
2021-06-07 17:49:20 +00:00
|
|
|
FFRawDemuxerContext rawctx;
|
2012-12-08 12:18:23 +00:00
|
|
|
int mlast_frame;
|
|
|
|
int64_t data_end;
|
2012-10-02 13:43:19 +00:00
|
|
|
} TAKDemuxContext;
|
|
|
|
|
2019-03-21 00:18:37 +00:00
|
|
|
static int tak_probe(const AVProbeData *p)
|
2012-10-02 13:43:19 +00:00
|
|
|
{
|
|
|
|
if (!memcmp(p->buf, "tBaK", 4))
|
2013-03-25 15:12:51 +00:00
|
|
|
return AVPROBE_SCORE_EXTENSION;
|
2012-10-02 13:43:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-04 19:24:48 +00:00
|
|
|
static unsigned long tak_check_crc(unsigned long checksum, const uint8_t *buf,
|
|
|
|
unsigned int len)
|
|
|
|
{
|
|
|
|
return av_crc(av_crc_get_table(AV_CRC_24_IEEE), checksum, buf, len);
|
|
|
|
}
|
|
|
|
|
2012-10-02 13:43:19 +00:00
|
|
|
static int tak_read_header(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
TAKDemuxContext *tc = s->priv_data;
|
2012-12-08 12:18:23 +00:00
|
|
|
AVIOContext *pb = s->pb;
|
2012-10-02 13:43:19 +00:00
|
|
|
GetBitContext gb;
|
|
|
|
AVStream *st;
|
|
|
|
uint8_t *buffer = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
st = avformat_new_stream(s, 0);
|
|
|
|
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 18:42:52 +00:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_TAK;
|
2021-08-24 17:41:16 +00:00
|
|
|
ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
|
2012-10-02 13:43:19 +00:00
|
|
|
|
|
|
|
tc->mlast_frame = 0;
|
|
|
|
if (avio_rl32(pb) != MKTAG('t', 'B', 'a', 'K')) {
|
|
|
|
avio_seek(pb, -4, SEEK_CUR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-07 20:12:41 +00:00
|
|
|
while (!avio_feof(pb)) {
|
2012-10-02 13:43:19 +00:00
|
|
|
enum TAKMetaDataType type;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
type = avio_r8(pb) & 0x7f;
|
|
|
|
size = avio_rl24(pb);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case TAK_METADATA_STREAMINFO:
|
2019-07-02 19:11:57 +00:00
|
|
|
if (st->codecpar->extradata)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2012-10-02 13:43:19 +00:00
|
|
|
case TAK_METADATA_LAST_FRAME:
|
|
|
|
case TAK_METADATA_ENCODER:
|
2013-06-04 19:24:48 +00:00
|
|
|
if (size <= 3)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2015-07-27 20:53:16 +00:00
|
|
|
buffer = av_malloc(size - 3 + AV_INPUT_BUFFER_PADDING_SIZE);
|
2012-10-02 13:43:19 +00:00
|
|
|
if (!buffer)
|
|
|
|
return AVERROR(ENOMEM);
|
2015-07-27 20:53:16 +00:00
|
|
|
memset(buffer + size - 3, 0, AV_INPUT_BUFFER_PADDING_SIZE);
|
2012-10-02 13:43:19 +00:00
|
|
|
|
2013-06-04 19:24:48 +00:00
|
|
|
ffio_init_checksum(pb, tak_check_crc, 0xCE04B7U);
|
|
|
|
if (avio_read(pb, buffer, size - 3) != size - 3) {
|
2012-10-02 13:43:19 +00:00
|
|
|
av_freep(&buffer);
|
|
|
|
return AVERROR(EIO);
|
|
|
|
}
|
2013-06-04 19:24:48 +00:00
|
|
|
if (ffio_get_checksum(s->pb) != avio_rb24(pb)) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "%d metadata block CRC error.\n", type);
|
|
|
|
if (s->error_recognition & AV_EF_EXPLODE) {
|
|
|
|
av_freep(&buffer);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
}
|
2012-10-02 13:43:19 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
case TAK_METADATA_MD5: {
|
|
|
|
uint8_t md5[16];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (size != 19)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2013-06-04 19:24:48 +00:00
|
|
|
ffio_init_checksum(pb, tak_check_crc, 0xCE04B7U);
|
2012-10-02 13:43:19 +00:00
|
|
|
avio_read(pb, md5, 16);
|
2013-06-04 19:24:48 +00:00
|
|
|
if (ffio_get_checksum(s->pb) != avio_rb24(pb)) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "MD5 metadata block CRC error.\n");
|
|
|
|
if (s->error_recognition & AV_EF_EXPLODE)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2012-10-02 13:43:19 +00:00
|
|
|
av_log(s, AV_LOG_VERBOSE, "MD5=");
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
av_log(s, AV_LOG_VERBOSE, "%02x", md5[i]);
|
|
|
|
av_log(s, AV_LOG_VERBOSE, "\n");
|
|
|
|
break;
|
2012-12-08 12:18:23 +00:00
|
|
|
}
|
2012-10-10 12:50:22 +00:00
|
|
|
case TAK_METADATA_END: {
|
|
|
|
int64_t curpos = avio_tell(pb);
|
2012-10-02 13:43:19 +00:00
|
|
|
|
2016-09-27 14:26:37 +00:00
|
|
|
if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
|
2012-10-02 13:43:19 +00:00
|
|
|
ff_ape_parse_tag(s);
|
|
|
|
avio_seek(pb, curpos, SEEK_SET);
|
|
|
|
}
|
2012-10-10 12:50:22 +00:00
|
|
|
|
|
|
|
tc->data_end += curpos;
|
2012-10-02 13:43:19 +00:00
|
|
|
return 0;
|
2012-12-08 12:18:23 +00:00
|
|
|
}
|
2012-10-02 13:43:19 +00:00
|
|
|
default:
|
|
|
|
ret = avio_skip(pb, size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == TAK_METADATA_STREAMINFO) {
|
|
|
|
TAKStreamInfo ti;
|
|
|
|
|
2017-10-22 23:29:53 +00:00
|
|
|
ret = avpriv_tak_parse_streaminfo(&ti, buffer, size -3);
|
|
|
|
if (ret < 0)
|
2019-07-02 18:35:30 +00:00
|
|
|
goto end;
|
2012-10-02 13:43:19 +00:00
|
|
|
if (ti.samples > 0)
|
|
|
|
st->duration = ti.samples;
|
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->bits_per_coded_sample = ti.bps;
|
2012-10-02 13:43:19 +00:00
|
|
|
if (ti.ch_layout)
|
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->channel_layout = ti.ch_layout;
|
|
|
|
st->codecpar->sample_rate = ti.sample_rate;
|
|
|
|
st->codecpar->channels = ti.channels;
|
2012-12-08 12:18:23 +00:00
|
|
|
st->start_time = 0;
|
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
|
|
|
avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
|
|
|
|
st->codecpar->extradata = buffer;
|
2016-04-10 19:58:15 +00:00
|
|
|
st->codecpar->extradata_size = size - 3;
|
2012-12-08 12:18:23 +00:00
|
|
|
buffer = NULL;
|
2012-10-02 13:43:19 +00:00
|
|
|
} else if (type == TAK_METADATA_LAST_FRAME) {
|
2019-07-02 18:35:30 +00:00
|
|
|
if (size != 11) {
|
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
goto end;
|
|
|
|
}
|
2017-10-22 23:29:53 +00:00
|
|
|
init_get_bits8(&gb, buffer, size - 3);
|
2012-10-02 13:43:19 +00:00
|
|
|
tc->mlast_frame = 1;
|
2012-12-08 12:18:23 +00:00
|
|
|
tc->data_end = get_bits64(&gb, TAK_LAST_FRAME_POS_BITS) +
|
|
|
|
get_bits(&gb, TAK_LAST_FRAME_SIZE_BITS);
|
2012-10-02 13:43:19 +00:00
|
|
|
av_freep(&buffer);
|
|
|
|
} else if (type == TAK_METADATA_ENCODER) {
|
2017-10-22 23:29:53 +00:00
|
|
|
init_get_bits8(&gb, buffer, size - 3);
|
2012-10-02 13:43:19 +00:00
|
|
|
av_log(s, AV_LOG_VERBOSE, "encoder version: %0X\n",
|
|
|
|
get_bits_long(&gb, TAK_ENCODER_VERSION_BITS));
|
|
|
|
av_freep(&buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return AVERROR_EOF;
|
2019-07-02 18:35:30 +00:00
|
|
|
end:
|
|
|
|
av_freep(&buffer);
|
|
|
|
return ret;
|
2012-10-02 13:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
TAKDemuxContext *tc = s->priv_data;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (tc->mlast_frame) {
|
|
|
|
AVIOContext *pb = s->pb;
|
2012-10-10 12:50:22 +00:00
|
|
|
int64_t size, left;
|
2012-10-02 13:43:19 +00:00
|
|
|
|
2013-06-04 19:24:48 +00:00
|
|
|
left = tc->data_end - avio_tell(pb);
|
2012-10-10 12:50:22 +00:00
|
|
|
size = FFMIN(left, 1024);
|
|
|
|
if (size <= 0)
|
2012-10-02 13:43:19 +00:00
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
|
|
ret = av_get_packet(pb, pkt, size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
pkt->stream_index = 0;
|
|
|
|
} else {
|
|
|
|
ret = ff_raw_read_partial_packet(s, pkt);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-04-19 17:45:24 +00:00
|
|
|
const AVInputFormat ff_tak_demuxer = {
|
2012-10-02 13:43:19 +00:00
|
|
|
.name = "tak",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("raw TAK"),
|
|
|
|
.priv_data_size = sizeof(TAKDemuxContext),
|
|
|
|
.read_probe = tak_probe,
|
|
|
|
.read_header = tak_read_header,
|
|
|
|
.read_packet = raw_read_packet,
|
|
|
|
.flags = AVFMT_GENERIC_INDEX,
|
|
|
|
.extensions = "tak",
|
|
|
|
.raw_codec_id = AV_CODEC_ID_TAK,
|
2021-06-07 14:13:33 +00:00
|
|
|
.priv_class = &ff_raw_demuxer_class,
|
2012-10-02 13:43:19 +00:00
|
|
|
};
|