3 * Copyright (c) 2012 Paul B Mahol
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/crc.h"
24 #include "libavutil/intreadwrite.h"
26 #define CACHED_BITSTREAM_READER !ARCH_X86_32
27 #define BITSTREAM_READER_LE
30 static const int64_t tak_channel_layouts
[] = {
38 AV_CH_FRONT_LEFT_OF_CENTER
,
39 AV_CH_FRONT_RIGHT_OF_CENTER
,
45 AV_CH_TOP_FRONT_CENTER
,
46 AV_CH_TOP_FRONT_RIGHT
,
48 AV_CH_TOP_BACK_CENTER
,
52 static const uint16_t frame_duration_type_quants
[] = {
53 3, 4, 6, 8, 4096, 8192, 16384, 512, 1024, 2048,
56 static int tak_get_nb_samples(int sample_rate
, enum TAKFrameSizeType type
)
58 int nb_samples
, max_nb_samples
;
60 if (type
<= TAK_FST_250ms
) {
61 nb_samples
= sample_rate
* frame_duration_type_quants
[type
] >>
62 TAK_FRAME_DURATION_QUANT_SHIFT
;
63 max_nb_samples
= 16384;
64 } else if (type
< FF_ARRAY_ELEMS(frame_duration_type_quants
)) {
65 nb_samples
= frame_duration_type_quants
[type
];
66 max_nb_samples
= sample_rate
*
67 frame_duration_type_quants
[TAK_FST_250ms
] >>
68 TAK_FRAME_DURATION_QUANT_SHIFT
;
70 return AVERROR_INVALIDDATA
;
73 if (nb_samples
<= 0 || nb_samples
> max_nb_samples
)
74 return AVERROR_INVALIDDATA
;
79 int ff_tak_check_crc(const uint8_t *buf
, unsigned int buf_size
)
84 return AVERROR_INVALIDDATA
;
87 CRC
= AV_RB24(buf
+ buf_size
);
88 crc
= av_crc(av_crc_get_table(AV_CRC_24_IEEE
), 0xCE04B7U
, buf
, buf_size
);
90 return AVERROR_INVALIDDATA
;
95 static int tak_parse_streaminfo(TAKStreamInfo
*s
, GetBitContext
*gb
)
97 uint64_t channel_mask
= 0;
98 int frame_type
, i
, ret
;
100 s
->codec
= get_bits(gb
, TAK_ENCODER_CODEC_BITS
);
101 skip_bits(gb
, TAK_ENCODER_PROFILE_BITS
);
103 frame_type
= get_bits(gb
, TAK_SIZE_FRAME_DURATION_BITS
);
104 s
->samples
= get_bits64(gb
, TAK_SIZE_SAMPLES_NUM_BITS
);
106 s
->data_type
= get_bits(gb
, TAK_FORMAT_DATA_TYPE_BITS
);
107 s
->sample_rate
= get_bits(gb
, TAK_FORMAT_SAMPLE_RATE_BITS
) +
109 s
->bps
= get_bits(gb
, TAK_FORMAT_BPS_BITS
) +
111 s
->channels
= get_bits(gb
, TAK_FORMAT_CHANNEL_BITS
) +
115 skip_bits(gb
, TAK_FORMAT_VALID_BITS
);
117 for (i
= 0; i
< s
->channels
; i
++) {
118 int value
= get_bits(gb
, TAK_FORMAT_CH_LAYOUT_BITS
);
120 if (value
< FF_ARRAY_ELEMS(tak_channel_layouts
))
121 channel_mask
|= tak_channel_layouts
[value
];
126 s
->ch_layout
= channel_mask
;
128 ret
= tak_get_nb_samples(s
->sample_rate
, frame_type
);
131 s
->frame_samples
= ret
;
136 int avpriv_tak_parse_streaminfo(TAKStreamInfo
*s
, const uint8_t *buf
, int size
)
139 int ret
= init_get_bits8(&gb
, buf
, size
);
142 return AVERROR_INVALIDDATA
;
144 return tak_parse_streaminfo(s
, &gb
);
147 int ff_tak_decode_frame_header(void *logctx
, GetBitContext
*gb
,
148 TAKStreamInfo
*ti
, int log_level_offset
)
150 if (get_bits(gb
, TAK_FRAME_HEADER_SYNC_ID_BITS
) != TAK_FRAME_HEADER_SYNC_ID
) {
151 av_log(logctx
, AV_LOG_ERROR
+ log_level_offset
, "missing sync id\n");
152 return AVERROR_INVALIDDATA
;
155 ti
->flags
= get_bits(gb
, TAK_FRAME_HEADER_FLAGS_BITS
);
156 ti
->frame_num
= get_bits(gb
, TAK_FRAME_HEADER_NO_BITS
);
158 if (ti
->flags
& TAK_FRAME_FLAG_IS_LAST
) {
159 ti
->last_frame_samples
= get_bits(gb
, TAK_FRAME_HEADER_SAMPLE_COUNT_BITS
) + 1;
162 ti
->last_frame_samples
= 0;
165 if (ti
->flags
& TAK_FRAME_FLAG_HAS_INFO
) {
166 int ret
= tak_parse_streaminfo(ti
, gb
);
175 if (ti
->flags
& TAK_FRAME_FLAG_HAS_METADATA
)
176 return AVERROR_INVALIDDATA
;
178 if (get_bits_left(gb
) < 24)
179 return AVERROR_INVALIDDATA
;