Libav
tak.c
Go to the documentation of this file.
1 /*
2  * TAK common code
3  * Copyright (c) 2012 Paul B Mahol
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/bswap.h"
23 #include "libavutil/crc.h"
24 #include "libavutil/intreadwrite.h"
25 
26 #define BITSTREAM_READER_LE
27 #include "tak.h"
28 
29 static const uint16_t frame_duration_type_quants[] = {
30  3, 4, 6, 8, 4096, 8192, 16384, 512, 1024, 2048,
31 };
32 
33 static int tak_get_nb_samples(int sample_rate, enum TAKFrameSizeType type)
34 {
35  int nb_samples, max_nb_samples;
36 
37  if (type <= TAK_FST_250ms) {
38  nb_samples = sample_rate * frame_duration_type_quants[type] >>
40  max_nb_samples = 16384;
41  } else if (type < FF_ARRAY_ELEMS(frame_duration_type_quants)) {
42  nb_samples = frame_duration_type_quants[type];
43  max_nb_samples = sample_rate *
46  } else {
47  return AVERROR_INVALIDDATA;
48  }
49 
50  if (nb_samples <= 0 || nb_samples > max_nb_samples)
51  return AVERROR_INVALIDDATA;
52 
53  return nb_samples;
54 }
55 
56 static int crc_init = 0;
57 #if CONFIG_SMALL
58 #define CRC_TABLE_SIZE 257
59 #else
60 #define CRC_TABLE_SIZE 1024
61 #endif
63 
65 {
66  if (!crc_init) {
67  av_crc_init(crc_24, 0, 24, 0x864CFBU, sizeof(crc_24));
68  crc_init = 1;
69  }
70 }
71 
72 int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size)
73 {
74  uint32_t crc, CRC;
75 
76  if (buf_size < 4)
77  return AVERROR_INVALIDDATA;
78  buf_size -= 3;
79 
80  CRC = av_bswap32(AV_RL24(buf + buf_size)) >> 8;
81  crc = av_crc(crc_24, 0xCE04B7U, buf, buf_size);
82  if (CRC != crc)
83  return AVERROR_INVALIDDATA;
84 
85  return 0;
86 }
87 
89 {
90  uint64_t channel_mask = 0;
91  int frame_type, i;
92 
95 
96  frame_type = get_bits(gb, TAK_SIZE_FRAME_DURATION_BITS);
98 
102  s->bps = get_bits(gb, TAK_FORMAT_BPS_BITS) +
103  TAK_BPS_MIN;
106 
107  if (get_bits1(gb)) {
109  if (get_bits1(gb)) {
110  for (i = 0; i < s->channels; i++) {
111  int value = get_bits(gb, TAK_FORMAT_CH_LAYOUT_BITS);
112 
113  if (value > 0 && value <= 18)
114  channel_mask |= 1 << (value - 1);
115  }
116  }
117  }
118 
119  s->ch_layout = channel_mask;
120  s->frame_samples = tak_get_nb_samples(s->sample_rate, frame_type);
121 }
122 
124  TAKStreamInfo *ti, int log_level_offset)
125 {
127  av_log(avctx, AV_LOG_ERROR + log_level_offset, "missing sync id\n");
128  return AVERROR_INVALIDDATA;
129  }
130 
133 
134  if (ti->flags & TAK_FRAME_FLAG_IS_LAST) {
136  skip_bits(gb, 2);
137  } else {
138  ti->last_frame_samples = 0;
139  }
140 
141  if (ti->flags & TAK_FRAME_FLAG_HAS_INFO) {
143 
144  if (get_bits(gb, 6))
145  skip_bits(gb, 25);
146  align_get_bits(gb);
147  }
148 
149  skip_bits(gb, 24);
150 
151  return 0;
152 }
static const uint16_t frame_duration_type_quants[]
Definition: tak.c:29
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int ff_tak_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb, TAKStreamInfo *ti, int log_level_offset)
Validate and decode a frame header.
Definition: tak.c:123
int channels
Definition: tak.h:133
#define TAK_FORMAT_BPS_BITS
Definition: tak.h:37
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:312
#define TAK_BPS_MIN
Definition: tak.h:50
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:228
#define TAK_FRAME_HEADER_NO_BITS
Definition: tak.h:55
#define TAK_FRAME_FLAG_IS_LAST
Definition: tak.h:60
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
uint64_t ch_layout
Definition: tak.h:138
#define FF_ARRAY_ELEMS(a)
#define TAK_SIZE_FRAME_DURATION_BITS
Definition: tak.h:41
int av_crc_init(AVCRC *ctx, int le, int bits, uint32_t poly, int ctx_size)
Initialize a CRC table.
Definition: crc.c:265
#define TAK_FORMAT_DATA_TYPE_BITS
Definition: tak.h:35
uint8_t
#define av_cold
Definition: attributes.h:66
int flags
Definition: tak.h:129
int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size)
Definition: tak.c:72
static int crc_init
Definition: tak.c:56
#define TAK_FRAME_HEADER_FLAGS_BITS
Definition: tak.h:51
#define TAK_FRAME_FLAG_HAS_INFO
Definition: tak.h:61
int data_type
Definition: tak.h:131
#define TAK_FRAME_HEADER_SYNC_ID
Definition: tak.h:52
static uint64_t get_bits64(GetBitContext *s, int n)
Definition: get_bits.h:318
#define CRC_TABLE_SIZE
Definition: tak.c:60
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
av_cold void ff_tak_init_crc(void)
Definition: tak.c:64
int last_frame_samples
Definition: tak.h:137
#define TAK_SIZE_SAMPLES_NUM_BITS
Definition: tak.h:42
int bps
Definition: tak.h:134
int64_t samples
Definition: tak.h:139
#define TAK_CHANNELS_MIN
Definition: tak.h:49
#define av_bswap32
Definition: bswap.h:33
void avpriv_tak_parse_streaminfo(GetBitContext *gb, TAKStreamInfo *s)
Parse the Streaminfo metadata block.
Definition: tak.c:88
main external API structure.
Definition: avcodec.h:1409
enum TAKCodecType codec
Definition: tak.h:130
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:267
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:259
#define TAK_SAMPLE_RATE_MIN
Definition: tak.h:48
int frame_samples
Definition: tak.h:136
TAK (Tom&#39;s lossless Audio Kompressor) decoder/demuxer common functions.
byte swapping routines
int sample_rate
Definition: tak.h:132
#define TAK_ENCODER_CODEC_BITS
Definition: tak.h:45
int frame_num
Definition: tak.h:135
#define AV_RL24
Definition: intreadwrite.h:78
TAKFrameSizeType
Definition: tak.h:115
#define TAK_ENCODER_PROFILE_BITS
Definition: tak.h:46
#define TAK_FORMAT_CH_LAYOUT_BITS
Definition: tak.h:40
#define TAK_FRAME_HEADER_SAMPLE_COUNT_BITS
Definition: tak.h:54
#define TAK_FRAME_HEADER_SYNC_ID_BITS
Definition: tak.h:53
#define TAK_FRAME_DURATION_QUANT_SHIFT
Definition: tak.h:56
uint32_t AVCRC
Definition: crc.h:28
#define TAK_FORMAT_VALID_BITS
Definition: tak.h:39
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:403
static AVCRC crc_24[CRC_TABLE_SIZE]
Definition: tak.c:62
#define TAK_FORMAT_CHANNEL_BITS
Definition: tak.h:38
static int tak_get_nb_samples(int sample_rate, enum TAKFrameSizeType type)
Definition: tak.c:33
#define TAK_FORMAT_SAMPLE_RATE_BITS
Definition: tak.h:36