Commit 57231e4d authored by Paul B Mahol's avatar Paul B Mahol Committed by Justin Ruggles

tak: demuxer, parser, and decoder

Signed-off-by: 's avatarPaul B Mahol <onemda@gmail.com>
Signed-off-by: 's avatarJustin Ruggles <justin.ruggles@gmail.com>
parent 096abfa1
......@@ -7,6 +7,7 @@ version <next>:
- audio volume filter
- deprecated the avconv -vol option. the volume filter is to be used instead.
- multi-channel ALAC encoding up to 7.1
- TAK demuxer, parser, and decoder
version 9_beta2:
......
......@@ -1675,6 +1675,7 @@ sap_muxer_select="rtp_muxer rtp_protocol"
sdp_demuxer_select="rtpdec"
smoothstreaming_muxer_select="ismv_muxer"
spdif_muxer_select="aac_parser"
tak_demuxer_select="tak_parser"
tg2_muxer_select="mov_muxer"
tgp_muxer_select="mov_muxer"
w64_demuxer_deps="wav_demuxer"
......
......@@ -273,6 +273,7 @@ library:
@item raw video @tab X @tab X
@item raw id RoQ @tab X @tab
@item raw Shorten @tab @tab X
@item raw TAK @tab @tab X
@item raw TrueHD @tab X @tab X
@item raw VC-1 @tab @tab X
@item raw PCM A-law @tab X @tab X
......@@ -800,6 +801,7 @@ following image formats are supported:
@item SMPTE 302M AES3 audio @tab @tab X
@item Speex @tab E @tab E
@tab supported through external library libspeex
@item TAK (Tom's lossless Audio Kompressor) @tab @tab X
@item True Audio (TTA) @tab @tab X
@item TrueHD @tab @tab X
@tab Used in HD-DVD and Blu-Ray discs.
......
......@@ -349,6 +349,7 @@ OBJS-$(CONFIG_SVQ3_DECODER) += svq3.o svq13.o h263.o h264.o \
h264_loopfilter.o h264_direct.o \
h264_sei.o h264_ps.o h264_refs.o \
h264_cavlc.o h264_cabac.o cabac.o
OBJS-$(CONFIG_TAK_DECODER) += takdec.o tak.o
OBJS-$(CONFIG_TARGA_DECODER) += targa.o
OBJS-$(CONFIG_TARGA_ENCODER) += targaenc.o rle.o
OBJS-$(CONFIG_THEORA_DECODER) += xiph.o
......@@ -555,6 +556,7 @@ OBJS-$(CONFIG_RTP_MUXER) += mpeg4audio.o xiph.o
OBJS-$(CONFIG_RTPDEC) += mjpeg.o
OBJS-$(CONFIG_SPDIF_DEMUXER) += aacadtsdec.o mpeg4audio.o
OBJS-$(CONFIG_SPDIF_MUXER) += dca.o
OBJS-$(CONFIG_TAK_DEMUXER) += tak.o
OBJS-$(CONFIG_WEBM_MUXER) += mpeg4audio.o mpegaudiodata.o \
xiph.o flac.o flacdata.o
OBJS-$(CONFIG_WTV_DEMUXER) += mpeg4audio.o mpegaudiodata.o
......@@ -631,6 +633,7 @@ OBJS-$(CONFIG_MPEGVIDEO_PARSER) += mpegvideo_parser.o \
OBJS-$(CONFIG_PNM_PARSER) += pnm_parser.o pnm.o
OBJS-$(CONFIG_RV30_PARSER) += rv34_parser.o
OBJS-$(CONFIG_RV40_PARSER) += rv34_parser.o
OBJS-$(CONFIG_TAK_PARSER) += tak_parser.o tak.o
OBJS-$(CONFIG_VC1_PARSER) += vc1_parser.o vc1.o vc1data.o \
msmpeg4.o msmpeg4data.o mpeg4video.o \
h263.o
......
......@@ -295,6 +295,7 @@ void avcodec_register_all(void)
REGISTER_DECODER (SHORTEN, shorten);
REGISTER_DECODER (SIPR, sipr);
REGISTER_DECODER (SMACKAUD, smackaud);
REGISTER_DECODER (TAK, tak);
REGISTER_DECODER (TRUEHD, truehd);
REGISTER_DECODER (TRUESPEECH, truespeech);
REGISTER_DECODER (TTA, tta);
......@@ -431,6 +432,7 @@ void avcodec_register_all(void)
REGISTER_PARSER (PNM, pnm);
REGISTER_PARSER (RV30, rv30);
REGISTER_PARSER (RV40, rv40);
REGISTER_PARSER (TAK, tak);
REGISTER_PARSER (VC1, vc1);
REGISTER_PARSER (VORBIS, vorbis);
REGISTER_PARSER (VP3, vp3);
......
......@@ -407,6 +407,7 @@ enum AVCodecID {
AV_CODEC_ID_ILBC,
AV_CODEC_ID_OPUS,
AV_CODEC_ID_COMFORT_NOISE,
AV_CODEC_ID_TAK,
/* subtitle codecs */
AV_CODEC_ID_FIRST_SUBTITLE = 0x17000, ///< A dummy ID pointing at the start of subtitle codecs.
......
......@@ -2119,6 +2119,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
.long_name = NULL_IF_CONFIG_SMALL("RFC 3389 Comfort Noise"),
.props = AV_CODEC_PROP_LOSSY,
},
{
.id = AV_CODEC_ID_TAK,
.type = AVMEDIA_TYPE_AUDIO,
.name = "tak",
.long_name = NULL_IF_CONFIG_SMALL("TAK (Tom's lossless Audio Kompressor)"),
.props = AV_CODEC_PROP_LOSSLESS,
},
/* subtitle codecs */
{
......
/*
* TAK common code
* Copyright (c) 2012 Paul B Mahol
*
* This file is part of Libav.
*
* Libav 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.
*
* Libav 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 Libav; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavutil/bswap.h"
#include "libavutil/crc.h"
#include "libavutil/intreadwrite.h"
#include "tak.h"
static const uint16_t frame_duration_type_quants[] = {
3, 4, 6, 8, 4096, 8192, 16384, 512, 1024, 2048,
};
static int tak_get_nb_samples(int sample_rate, enum TAKFrameSizeType type)
{
int nb_samples, max_nb_samples;
if (type <= TAK_FST_250ms) {
nb_samples = sample_rate * frame_duration_type_quants[type] >>
TAK_FRAME_DURATION_QUANT_SHIFT;
max_nb_samples = 16384;
} else if (type < FF_ARRAY_ELEMS(frame_duration_type_quants)) {
nb_samples = frame_duration_type_quants[type];
max_nb_samples = sample_rate *
frame_duration_type_quants[TAK_FST_250ms] >>
TAK_FRAME_DURATION_QUANT_SHIFT;
} else {
return AVERROR_INVALIDDATA;
}
if (nb_samples <= 0 || nb_samples > max_nb_samples)
return AVERROR_INVALIDDATA;
return nb_samples;
}
static int crc_init = 0;
#if CONFIG_SMALL
#define CRC_TABLE_SIZE 257
#else
#define CRC_TABLE_SIZE 1024
#endif
static AVCRC crc_24[CRC_TABLE_SIZE];
av_cold void ff_tak_init_crc(void)
{
if (!crc_init) {
av_crc_init(crc_24, 0, 24, 0x864CFBU, sizeof(crc_24));
crc_init = 1;
}
}
int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size)
{
uint32_t crc, CRC;
if (buf_size < 4)
return AVERROR_INVALIDDATA;
buf_size -= 3;
CRC = av_bswap32(AV_RL24(buf + buf_size)) >> 8;
crc = av_crc(crc_24, 0xCE04B7U, buf, buf_size);
if (CRC != crc)
return AVERROR_INVALIDDATA;
return 0;
}
void avpriv_tak_parse_streaminfo(GetBitContext *gb, TAKStreamInfo *s)
{
uint64_t channel_mask = 0;
int frame_type, i;
s->codec = get_bits(gb, TAK_ENCODER_CODEC_BITS);
skip_bits(gb, TAK_ENCODER_PROFILE_BITS);
frame_type = get_bits(gb, TAK_SIZE_FRAME_DURATION_BITS);
s->samples = get_bits64(gb, TAK_SIZE_SAMPLES_NUM_BITS);
s->data_type = get_bits(gb, TAK_FORMAT_DATA_TYPE_BITS);
s->sample_rate = get_bits(gb, TAK_FORMAT_SAMPLE_RATE_BITS) +
TAK_SAMPLE_RATE_MIN;
s->bps = get_bits(gb, TAK_FORMAT_BPS_BITS) +
TAK_BPS_MIN;
s->channels = get_bits(gb, TAK_FORMAT_CHANNEL_BITS) +
TAK_CHANNELS_MIN;
if (get_bits1(gb)) {
skip_bits(gb, TAK_FORMAT_VALID_BITS);
if (get_bits1(gb)) {
for (i = 0; i < s->channels; i++) {
int value = get_bits(gb, TAK_FORMAT_CH_LAYOUT_BITS);
if (value > 0 && value <= 18)
channel_mask |= 1 << (value - 1);
}
}
}
s->ch_layout = channel_mask;
s->frame_samples = tak_get_nb_samples(s->sample_rate, frame_type);
}
int ff_tak_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
TAKStreamInfo *ti, int log_level_offset)
{
if (get_bits(gb, TAK_FRAME_HEADER_SYNC_ID_BITS) != TAK_FRAME_HEADER_SYNC_ID) {
av_log(avctx, AV_LOG_ERROR + log_level_offset, "missing sync id\n");
return AVERROR_INVALIDDATA;
}
ti->flags = get_bits(gb, TAK_FRAME_HEADER_FLAGS_BITS);
ti->frame_num = get_bits(gb, TAK_FRAME_HEADER_NO_BITS);
if (ti->flags & TAK_FRAME_FLAG_IS_LAST) {
ti->last_frame_samples = get_bits(gb, TAK_FRAME_HEADER_SAMPLE_COUNT_BITS) + 1;
skip_bits(gb, 2);
} else {
ti->last_frame_samples = 0;
}
if (ti->flags & TAK_FRAME_FLAG_HAS_INFO) {
avpriv_tak_parse_streaminfo(gb, ti);
if (get_bits(gb, 6))
skip_bits(gb, 25);
align_get_bits(gb);
}
skip_bits(gb, 24);
return 0;
}
/*
* TAK decoder/demuxer common code
* Copyright (c) 2012 Paul B Mahol
*
* This file is part of Libav.
*
* Libav 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.
*
* Libav 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 Libav; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* TAK (Tom's lossless Audio Kompressor) decoder/demuxer common functions
*/
#ifndef AVCODEC_TAK_H
#define AVCODEC_TAK_H
#include <stdint.h>
#define BITSTREAM_READER_LE
#include "get_bits.h"
#include "avcodec.h"
#define TAK_FORMAT_DATA_TYPE_BITS 3
#define TAK_FORMAT_SAMPLE_RATE_BITS 18
#define TAK_FORMAT_BPS_BITS 5
#define TAK_FORMAT_CHANNEL_BITS 4
#define TAK_FORMAT_VALID_BITS 5
#define TAK_FORMAT_CH_LAYOUT_BITS 6
#define TAK_SIZE_FRAME_DURATION_BITS 4
#define TAK_SIZE_SAMPLES_NUM_BITS 35
#define TAK_LAST_FRAME_POS_BITS 40
#define TAK_LAST_FRAME_SIZE_BITS 24
#define TAK_ENCODER_CODEC_BITS 6
#define TAK_ENCODER_PROFILE_BITS 4
#define TAK_ENCODER_VERSION_BITS 24
#define TAK_SAMPLE_RATE_MIN 6000
#define TAK_CHANNELS_MIN 1
#define TAK_BPS_MIN 8
#define TAK_FRAME_HEADER_FLAGS_BITS 3
#define TAK_FRAME_HEADER_SYNC_ID 0xA0FF
#define TAK_FRAME_HEADER_SYNC_ID_BITS 16
#define TAK_FRAME_HEADER_SAMPLE_COUNT_BITS 14
#define TAK_FRAME_HEADER_NO_BITS 21
#define TAK_FRAME_DURATION_QUANT_SHIFT 5
#define TAK_CRC24_BITS 24
#define TAK_FRAME_FLAG_IS_LAST 0x1
#define TAK_FRAME_FLAG_HAS_INFO 0x2
#define TAK_FRAME_FLAG_HAS_METADATA 0x4
#define TAK_MAX_CHANNELS (1 << TAK_FORMAT_CHANNEL_BITS)
#define TAK_MIN_FRAME_HEADER_BITS (TAK_FRAME_HEADER_SYNC_ID_BITS + \
TAK_FRAME_HEADER_FLAGS_BITS + \
TAK_FRAME_HEADER_NO_BITS + \
TAK_CRC24_BITS)
#define TAK_MIN_FRAME_HEADER_LAST_BITS (TAK_MIN_FRAME_HEADER_BITS + 2 + \
TAK_FRAME_HEADER_SAMPLE_COUNT_BITS)
#define TAK_ENCODER_BITS (TAK_ENCODER_CODEC_BITS + \
TAK_ENCODER_PROFILE_BITS)
#define TAK_SIZE_BITS (TAK_SIZE_SAMPLES_NUM_BITS + \
TAK_SIZE_FRAME_DURATION_BITS)
#define TAK_FORMAT_BITS (TAK_FORMAT_DATA_TYPE_BITS + \
TAK_FORMAT_SAMPLE_RATE_BITS + \
TAK_FORMAT_BPS_BITS + \
TAK_FORMAT_CHANNEL_BITS + 1 + \
TAK_FORMAT_VALID_BITS + 1 + \
TAK_FORMAT_CH_LAYOUT_BITS * \
TAK_MAX_CHANNELS)
#define TAK_STREAMINFO_BITS (TAK_ENCODER_BITS + \
TAK_SIZE_BITS + \
TAK_FORMAT_BITS)
#define TAK_MAX_FRAME_HEADER_BITS (TAK_MIN_FRAME_HEADER_LAST_BITS + \
TAK_STREAMINFO_BITS + 31)
#define TAK_STREAMINFO_BYTES ((TAK_STREAMINFO_BITS + 7) / 8)
#define TAK_MAX_FRAME_HEADER_BYTES ((TAK_MAX_FRAME_HEADER_BITS + 7) / 8)
#define TAK_MIN_FRAME_HEADER_BYTES ((TAK_MIN_FRAME_HEADER_BITS + 7) / 8)
enum TAKCodecType {
TAK_CODEC_MONO_STEREO = 2,
TAK_CODEC_MULTICHANNEL = 4
};
enum TAKMetaDataType {
TAK_METADATA_END = 0,
TAK_METADATA_STREAMINFO,
TAK_METADATA_SEEKTABLE,
TAK_METADATA_SIMPLE_WAVE_DATA,
TAK_METADATA_ENCODER,
TAK_METADATA_PADDING,
TAK_METADATA_MD5,
TAK_METADATA_LAST_FRAME,
};
enum TAKFrameSizeType {
TAK_FST_94ms = 0,
TAK_FST_125ms,
TAK_FST_188ms,
TAK_FST_250ms,
TAK_FST_4096,
TAK_FST_8192,
TAK_FST_16384,
TAK_FST_512,
TAK_FST_1024,
TAK_FST_2048,
};
typedef struct TAKStreamInfo {
int flags;
enum TAKCodecType codec;
int data_type;
int sample_rate;
int channels;
int bps;
int frame_num;
int frame_samples;
int last_frame_samples;
uint64_t ch_layout;
int64_t samples;
} TAKStreamInfo;
void ff_tak_init_crc(void);
int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size);
/**
* Parse the Streaminfo metadata block.
* @param[in] gb pointer to GetBitContext
* @param[out] s storage for parsed information
*/
void avpriv_tak_parse_streaminfo(GetBitContext *gb, TAKStreamInfo *s);
/**
* Validate and decode a frame header.
* @param avctx AVCodecContext to use as av_log() context
* @param[in] gb GetBitContext from which to read frame header
* @param[out] s frame information
* @param log_level_offset log level offset, can be used to silence
* error messages.
* @return non-zero on error, 0 if OK
*/
int ff_tak_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
TAKStreamInfo *s, int log_level_offset);
#endif /* AVCODEC_TAK_H */
/*
* TAK parser
* Copyright (c) 2012 Michael Niedermayer
*
* This file is part of Libav.
*
* Libav 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.
*
* Libav 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 Libav; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* TAK parser
**/
#include "tak.h"
#include "parser.h"
typedef struct TAKParseContext {
ParseContext pc;
TAKStreamInfo ti;
int index;
} TAKParseContext;
static av_cold int tak_init(AVCodecParserContext *s)
{
ff_tak_init_crc();
return 0;
}
static int tak_parse(AVCodecParserContext *s, AVCodecContext *avctx,
const uint8_t **poutbuf, int *poutbuf_size,
const uint8_t *buf, int buf_size)
{
TAKParseContext *t = s->priv_data;
ParseContext *pc = &t->pc;
int next = END_NOT_FOUND;
GetBitContext gb;
int consumed = 0;
int needed = buf_size ? TAK_MAX_FRAME_HEADER_BYTES : 8;
if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
TAKStreamInfo ti;
init_get_bits(&gb, buf, buf_size);
if (!ff_tak_decode_frame_header(avctx, &gb, &ti, 127))
s->duration = t->ti.last_frame_samples ? t->ti.last_frame_samples
: t->ti.frame_samples;
*poutbuf = buf;
*poutbuf_size = buf_size;
return buf_size;
}
while (buf_size || t->index + needed <= pc->index) {
if (buf_size && t->index + TAK_MAX_FRAME_HEADER_BYTES > pc->index) {
int tmp_buf_size = FFMIN(2 * TAK_MAX_FRAME_HEADER_BYTES,
buf_size);
const uint8_t *tmp_buf = buf;
ff_combine_frame(pc, END_NOT_FOUND, &tmp_buf, &tmp_buf_size);
consumed += tmp_buf_size;
buf += tmp_buf_size;
buf_size -= tmp_buf_size;
}
for (; t->index + needed <= pc->index; t->index++)
if (pc->buffer[t->index] == 0xFF &&
pc->buffer[t->index + 1] == 0xA0) {
TAKStreamInfo ti;
init_get_bits(&gb, pc->buffer + t->index,
8 * (pc->index - t->index));
if (!ff_tak_decode_frame_header(avctx, &gb,
pc->frame_start_found ? &ti
: &t->ti,
127) &&
!ff_tak_check_crc(pc->buffer + t->index,
get_bits_count(&gb) / 8)) {
if (!pc->frame_start_found) {
pc->frame_start_found = 1;
s->duration = t->ti.last_frame_samples ?
t->ti.last_frame_samples :
t->ti.frame_samples;
} else {
pc->frame_start_found = 0;
next = t->index - pc->index;
t->index = 0;
goto found;
}
}
}
}
found:
if (consumed && !buf_size && next == END_NOT_FOUND ||
ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
*poutbuf = NULL;
*poutbuf_size = 0;
return buf_size + consumed;
}
if (next != END_NOT_FOUND) {
next += consumed;
pc->overread = FFMAX(0, -next);
}
*poutbuf = buf;
*poutbuf_size = buf_size;
return next;
}
AVCodecParser ff_tak_parser = {
.codec_ids = { AV_CODEC_ID_TAK },
.priv_data_size = sizeof(TAKParseContext),
.parser_init = tak_init,
.parser_parse = tak_parse,
.parser_close = ff_parse_close,
};
This diff is collapsed.
......@@ -27,7 +27,7 @@
*/
#define LIBAVCODEC_VERSION_MAJOR 54
#define LIBAVCODEC_VERSION_MINOR 33
#define LIBAVCODEC_VERSION_MINOR 34
#define LIBAVCODEC_VERSION_MICRO 0
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
......
......@@ -304,6 +304,7 @@ OBJS-$(CONFIG_SRT_MUXER) += rawenc.o
OBJS-$(CONFIG_STR_DEMUXER) += psxstr.o
OBJS-$(CONFIG_SWF_DEMUXER) += swfdec.o swf.o
OBJS-$(CONFIG_SWF_MUXER) += swfenc.o swf.o
OBJS-$(CONFIG_TAK_DEMUXER) += takdec.o apetag.o img2.o rawdec.o
OBJS-$(CONFIG_THP_DEMUXER) += thp.o
OBJS-$(CONFIG_TIERTEXSEQ_DEMUXER) += tiertexseq.o
OBJS-$(CONFIG_TMV_DEMUXER) += tmv.o
......
......@@ -214,6 +214,7 @@ void av_register_all(void)
REGISTER_MUXDEMUX (SRT, srt);
REGISTER_DEMUXER (STR, str);
REGISTER_MUXDEMUX (SWF, swf);
REGISTER_DEMUXER (TAK, tak);
REGISTER_MUXER (TG2, tg2);
REGISTER_MUXER (TGP, tgp);
REGISTER_DEMUXER (THP, thp);
......
/*
* Raw TAK demuxer
* Copyright (c) 2012 Paul B Mahol
*
* This file is part of Libav.
*
* Libav 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.
*
* Libav 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 Libav; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavcodec/tak.h"
#include "avformat.h"
#include "internal.h"
#include "rawdec.h"
#include "apetag.h"
typedef struct TAKDemuxContext {
int mlast_frame;
int64_t data_end;
} TAKDemuxContext;
static int tak_probe(AVProbeData *p)
{
if (!memcmp(p->buf, "tBaK", 4))
return AVPROBE_SCORE_MAX / 2;
return 0;
}
static int tak_read_header(AVFormatContext *s)
{
TAKDemuxContext *tc = s->priv_data;
AVIOContext *pb = s->pb;
GetBitContext gb;
AVStream *st;
uint8_t *buffer = NULL;
int ret;
st = avformat_new_stream(s, 0);
if (!st)
return AVERROR(ENOMEM);
st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
st->codec->codec_id = AV_CODEC_ID_TAK;
st->need_parsing = AVSTREAM_PARSE_FULL;
tc->mlast_frame = 0;
if (avio_rl32(pb) != MKTAG('t', 'B', 'a', 'K')) {
avio_seek(pb, -4, SEEK_CUR);
return 0;
}
while (!pb->eof_reached) {
enum TAKMetaDataType type;
int size;
type = avio_r8(pb) & 0x7f;
size = avio_rl24(pb);
switch (type) {
case TAK_METADATA_STREAMINFO:
case TAK_METADATA_LAST_FRAME:
case TAK_METADATA_ENCODER:
buffer = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
if (!buffer)
return AVERROR(ENOMEM);
if (avio_read(pb, buffer, size) != size) {
av_freep(&buffer);
return AVERROR(EIO);
}
init_get_bits(&gb, buffer, size * 8);
break;
case TAK_METADATA_MD5: {
uint8_t md5[16];
int i;
if (size != 19)
return AVERROR_INVALIDDATA;
avio_read(pb, md5, 16);
avio_skip(pb, 3);
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;
}
case TAK_METADATA_END: {
int64_t curpos = avio_tell(pb);
if (pb->seekable) {
ff_ape_parse_tag(s);
avio_seek(pb, curpos, SEEK_SET);
}
tc->data_end += curpos;
return 0;
}
default:
ret = avio_skip(pb, size);
if (ret < 0)
return ret;
}
if (type == TAK_METADATA_STREAMINFO) {
TAKStreamInfo ti;
avpriv_tak_parse_streaminfo(&gb, &ti);
if (ti.samples > 0)
st->duration = ti.samples;
st->codec->bits_per_coded_sample = ti.bps;
if (ti.ch_layout)
st->codec->channel_layout = ti.ch_layout;
st->codec->sample_rate = ti.sample_rate;
st->codec->channels = ti.channels;
st->start_time = 0;
avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
st->codec->extradata = buffer;
st->codec->extradata_size = size;
buffer = NULL;
} else if (type == TAK_METADATA_LAST_FRAME) {
if (size != 11)
return AVERROR_INVALIDDATA;
tc->mlast_frame = 1;
tc->data_end = get_bits64(&gb, TAK_LAST_FRAME_POS_BITS) +
get_bits(&gb, TAK_LAST_FRAME_SIZE_BITS);
av_freep(&buffer);
} else if (type == TAK_METADATA_ENCODER) {
av_log(s, AV_LOG_VERBOSE, "encoder version: %0X\n",
get_bits_long(&gb, TAK_ENCODER_VERSION_BITS));
av_freep(&buffer);
}
}
return AVERROR_EOF;
}
static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
{
TAKDemuxContext *tc = s->priv_data;
int ret;
if (tc->mlast_frame) {
AVIOContext *pb = s->pb;
int64_t size, left;
left = tc->data_end - avio_tell(s->pb);
size = FFMIN(left, 1024);
if (size <= 0)
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;