Skip to content

Commit

Permalink
TAK demuxer, decoder and parser
Browse files Browse the repository at this point in the history
Signed-off-by: Paul B Mahol <[email protected]>
  • Loading branch information
richardpl committed Oct 8, 2012
1 parent 208a5d1 commit d7a4739
Show file tree
Hide file tree
Showing 18 changed files with 1,635 additions and 3 deletions.
1 change: 1 addition & 0 deletions Changelog
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ version next:
- Opus encoder using libopus
- ffprobe -select_streams option
- Pinnacle TARGA CineWave YUV16 decoder
- TAK demuxer, decoder and parser


version 1.0:
Expand Down
1 change: 1 addition & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -1824,6 +1824,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"
Expand Down
2 changes: 2 additions & 0 deletions doc/general.texi
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,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
Expand Down Expand Up @@ -863,6 +864,7 @@ following image formats are supported:
@tab experimental codec
@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.
Expand Down
2 changes: 2 additions & 0 deletions libavcodec/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,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_TARGA_Y216_DECODER) += targa_y216dec.o
Expand Down Expand Up @@ -713,6 +714,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
Expand Down
2 changes: 2 additions & 0 deletions libavcodec/allcodecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ void avcodec_register_all(void)
REGISTER_DECODER (SMACKAUD, smackaud);
REGISTER_ENCDEC (SONIC, sonic);
REGISTER_ENCODER (SONIC_LS, sonic_ls);
REGISTER_DECODER (TAK, tak);
REGISTER_DECODER (TRUEHD, truehd);
REGISTER_DECODER (TRUESPEECH, truespeech);
REGISTER_DECODER (TTA, tta);
Expand Down Expand Up @@ -486,6 +487,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);
Expand Down
1 change: 1 addition & 0 deletions libavcodec/avcodec.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ enum AVCodecID {
AV_CODEC_ID_SONIC_LS = MKBETAG('S','O','N','L'),
AV_CODEC_ID_PAF_AUDIO = MKBETAG('P','A','F','A'),
AV_CODEC_ID_OPUS = MKBETAG('O','P','U','S'),
AV_CODEC_ID_TAK = MKBETAG('t','B','a','K'),

/* subtitle codecs */
AV_CODEC_ID_FIRST_SUBTITLE = 0x17000, ///< A dummy ID pointing at the start of subtitle codecs.
Expand Down
7 changes: 7 additions & 0 deletions libavcodec/codec_desc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2252,6 +2252,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
.long_name = NULL_IF_CONFIG_SMALL("Opus (Opus Interactive Audio Codec)"),
.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 */
{
Expand Down
177 changes: 177 additions & 0 deletions libavcodec/tak.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
* TAK common code
* 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
*/

#include "tak.h"
#include "libavutil/crc.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/bswap.h"

static const int64_t tak_channel_layouts[] = {
0,
AV_CH_FRONT_LEFT,
AV_CH_FRONT_RIGHT,
AV_CH_FRONT_CENTER,
AV_CH_LOW_FREQUENCY,
AV_CH_BACK_LEFT,
AV_CH_BACK_RIGHT,
AV_CH_FRONT_LEFT_OF_CENTER,
AV_CH_FRONT_RIGHT_OF_CENTER,
AV_CH_BACK_CENTER,
AV_CH_SIDE_LEFT,
AV_CH_SIDE_RIGHT,
AV_CH_TOP_CENTER,
AV_CH_TOP_FRONT_LEFT,
AV_CH_TOP_FRONT_CENTER,
AV_CH_TOP_FRONT_RIGHT,
AV_CH_TOP_BACK_LEFT,
AV_CH_TOP_BACK_CENTER,
AV_CH_TOP_BACK_RIGHT,
};

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_bits_longlong(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 < FF_ARRAY_ELEMS(tak_channel_layouts))
channel_mask |= tak_channel_layouts[value];
}
}
}

s->ch_layout = channel_mask;
s->frame_samples = tak_get_nb_samples(s->sample_rate, frame_type);
}

#define FRAME_IS_LAST 1
#define FRAME_HAVE_INFO 2
#define FRAME_HAVE_METADATA 4

int ff_tak_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
TAKStreamInfo *ti, int log_level_offset)
{
int flags;

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;
}

flags = get_bits(gb, TAK_FRAME_HEADER_FLAGS_BITS);
ti->frame_num = get_bits(gb, TAK_FRAME_HEADER_NO_BITS);

if (flags & FRAME_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 (flags & FRAME_HAVE_INFO) {
avpriv_tak_parse_streaminfo(gb, ti);

if (get_bits(gb, 6))
skip_bits(gb, 25);
align_get_bits(gb);
}

if (flags & FRAME_HAVE_METADATA)
return AVERROR_INVALIDDATA;

skip_bits(gb, 24);

return 0;
}
Loading

0 comments on commit d7a4739

Please sign in to comment.