aarch64: Add assembly support for -fsanitize=hwaddress tagged globals.
[libav.git] / libavcodec / tak.c
blobc90e55ad6ab88cf01725ed93a3be4df9ae2c9c3c
1 /*
2 * TAK common code
3 * Copyright (c) 2012 Paul B Mahol
5 * This file is part of Libav.
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.
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.
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
22 #include "libavutil/bswap.h"
23 #include "libavutil/crc.h"
24 #include "libavutil/intreadwrite.h"
26 #define BITSTREAM_READER_LE
27 #include "bitstream.h"
28 #include "tak.h"
30 static const uint16_t frame_duration_type_quants[] = {
31 3, 4, 6, 8, 4096, 8192, 16384, 512, 1024, 2048,
34 static int tak_get_nb_samples(int sample_rate, enum TAKFrameSizeType type)
36 int nb_samples, max_nb_samples;
38 if (type <= TAK_FST_250ms) {
39 nb_samples = sample_rate * frame_duration_type_quants[type] >>
40 TAK_FRAME_DURATION_QUANT_SHIFT;
41 max_nb_samples = 16384;
42 } else if (type < FF_ARRAY_ELEMS(frame_duration_type_quants)) {
43 nb_samples = frame_duration_type_quants[type];
44 max_nb_samples = sample_rate *
45 frame_duration_type_quants[TAK_FST_250ms] >>
46 TAK_FRAME_DURATION_QUANT_SHIFT;
47 } else {
48 return AVERROR_INVALIDDATA;
51 if (nb_samples <= 0 || nb_samples > max_nb_samples)
52 return AVERROR_INVALIDDATA;
54 return nb_samples;
57 static int crc_init = 0;
58 #if CONFIG_SMALL
59 #define CRC_TABLE_SIZE 257
60 #else
61 #define CRC_TABLE_SIZE 1024
62 #endif
63 static AVCRC crc_24[CRC_TABLE_SIZE];
65 av_cold void ff_tak_init_crc(void)
67 if (!crc_init) {
68 av_crc_init(crc_24, 0, 24, 0x864CFBU, sizeof(crc_24));
69 crc_init = 1;
73 int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size)
75 uint32_t crc, CRC;
77 if (buf_size < 4)
78 return AVERROR_INVALIDDATA;
79 buf_size -= 3;
81 CRC = av_bswap32(AV_RL24(buf + buf_size)) >> 8;
82 crc = av_crc(crc_24, 0xCE04B7U, buf, buf_size);
83 if (CRC != crc)
84 return AVERROR_INVALIDDATA;
86 return 0;
89 void avpriv_tak_parse_streaminfo(BitstreamContext *bc, TAKStreamInfo *s)
91 uint64_t channel_mask = 0;
92 int frame_type, i;
94 s->codec = bitstream_read(bc, TAK_ENCODER_CODEC_BITS);
95 bitstream_skip(bc, TAK_ENCODER_PROFILE_BITS);
97 frame_type = bitstream_read(bc, TAK_SIZE_FRAME_DURATION_BITS);
98 s->samples = bitstream_read_63(bc, TAK_SIZE_SAMPLES_NUM_BITS);
100 s->data_type = bitstream_read(bc, TAK_FORMAT_DATA_TYPE_BITS);
101 s->sample_rate = bitstream_read(bc, TAK_FORMAT_SAMPLE_RATE_BITS) +
102 TAK_SAMPLE_RATE_MIN;
103 s->bps = bitstream_read(bc, TAK_FORMAT_BPS_BITS) +
104 TAK_BPS_MIN;
105 s->channels = bitstream_read(bc, TAK_FORMAT_CHANNEL_BITS) +
106 TAK_CHANNELS_MIN;
108 if (bitstream_read_bit(bc)) {
109 bitstream_skip(bc, TAK_FORMAT_VALID_BITS);
110 if (bitstream_read_bit(bc)) {
111 for (i = 0; i < s->channels; i++) {
112 int value = bitstream_read(bc, TAK_FORMAT_CH_LAYOUT_BITS);
114 if (value > 0 && value <= 18)
115 channel_mask |= 1 << (value - 1);
120 s->ch_layout = channel_mask;
121 s->frame_samples = tak_get_nb_samples(s->sample_rate, frame_type);
124 int ff_tak_decode_frame_header(AVCodecContext *avctx, BitstreamContext *bc,
125 TAKStreamInfo *ti, int log_level_offset)
127 if (bitstream_read(bc, TAK_FRAME_HEADER_SYNC_ID_BITS) != TAK_FRAME_HEADER_SYNC_ID) {
128 av_log(avctx, AV_LOG_ERROR + log_level_offset, "missing sync id\n");
129 return AVERROR_INVALIDDATA;
132 ti->flags = bitstream_read(bc, TAK_FRAME_HEADER_FLAGS_BITS);
133 ti->frame_num = bitstream_read(bc, TAK_FRAME_HEADER_NO_BITS);
135 if (ti->flags & TAK_FRAME_FLAG_IS_LAST) {
136 ti->last_frame_samples = bitstream_read(bc, TAK_FRAME_HEADER_SAMPLE_COUNT_BITS) + 1;
137 bitstream_skip(bc, 2);
138 } else {
139 ti->last_frame_samples = 0;
142 if (ti->flags & TAK_FRAME_FLAG_HAS_INFO) {
143 avpriv_tak_parse_streaminfo(bc, ti);
145 if (bitstream_read(bc, 6))
146 bitstream_skip(bc, 25);
147 bitstream_align(bc);
150 bitstream_skip(bc, 24);
152 return 0;