00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/bswap.h"
00023 #include "libavutil/crc.h"
00024 #include "libavutil/intreadwrite.h"
00025 #include "tak.h"
00026
00027 static const int64_t tak_channel_layouts[] = {
00028 0,
00029 AV_CH_FRONT_LEFT,
00030 AV_CH_FRONT_RIGHT,
00031 AV_CH_FRONT_CENTER,
00032 AV_CH_LOW_FREQUENCY,
00033 AV_CH_BACK_LEFT,
00034 AV_CH_BACK_RIGHT,
00035 AV_CH_FRONT_LEFT_OF_CENTER,
00036 AV_CH_FRONT_RIGHT_OF_CENTER,
00037 AV_CH_BACK_CENTER,
00038 AV_CH_SIDE_LEFT,
00039 AV_CH_SIDE_RIGHT,
00040 AV_CH_TOP_CENTER,
00041 AV_CH_TOP_FRONT_LEFT,
00042 AV_CH_TOP_FRONT_CENTER,
00043 AV_CH_TOP_FRONT_RIGHT,
00044 AV_CH_TOP_BACK_LEFT,
00045 AV_CH_TOP_BACK_CENTER,
00046 AV_CH_TOP_BACK_RIGHT,
00047 };
00048
00049 static const uint16_t frame_duration_type_quants[] = {
00050 3, 4, 6, 8, 4096, 8192, 16384, 512, 1024, 2048,
00051 };
00052
00053 static int tak_get_nb_samples(int sample_rate, enum TAKFrameSizeType type)
00054 {
00055 int nb_samples, max_nb_samples;
00056
00057 if (type <= TAK_FST_250ms) {
00058 nb_samples = sample_rate * frame_duration_type_quants[type] >>
00059 TAK_FRAME_DURATION_QUANT_SHIFT;
00060 max_nb_samples = 16384;
00061 } else if (type < FF_ARRAY_ELEMS(frame_duration_type_quants)) {
00062 nb_samples = frame_duration_type_quants[type];
00063 max_nb_samples = sample_rate *
00064 frame_duration_type_quants[TAK_FST_250ms] >>
00065 TAK_FRAME_DURATION_QUANT_SHIFT;
00066 } else {
00067 return AVERROR_INVALIDDATA;
00068 }
00069
00070 if (nb_samples <= 0 || nb_samples > max_nb_samples)
00071 return AVERROR_INVALIDDATA;
00072
00073 return nb_samples;
00074 }
00075
00076 static int crc_init = 0;
00077 #if CONFIG_SMALL
00078 #define CRC_TABLE_SIZE 257
00079 #else
00080 #define CRC_TABLE_SIZE 1024
00081 #endif
00082 static AVCRC crc_24[CRC_TABLE_SIZE];
00083
00084 av_cold void ff_tak_init_crc(void)
00085 {
00086 if (!crc_init) {
00087 av_crc_init(crc_24, 0, 24, 0x864CFBU, sizeof(crc_24));
00088 crc_init = 1;
00089 }
00090 }
00091
00092 int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size)
00093 {
00094 uint32_t crc, CRC;
00095
00096 if (buf_size < 4)
00097 return AVERROR_INVALIDDATA;
00098 buf_size -= 3;
00099
00100 CRC = av_bswap32(AV_RL24(buf + buf_size)) >> 8;
00101 crc = av_crc(crc_24, 0xCE04B7U, buf, buf_size);
00102 if (CRC != crc)
00103 return AVERROR_INVALIDDATA;
00104
00105 return 0;
00106 }
00107
00108 void avpriv_tak_parse_streaminfo(GetBitContext *gb, TAKStreamInfo *s)
00109 {
00110 uint64_t channel_mask = 0;
00111 int frame_type, i;
00112
00113 s->codec = get_bits(gb, TAK_ENCODER_CODEC_BITS);
00114 skip_bits(gb, TAK_ENCODER_PROFILE_BITS);
00115
00116 frame_type = get_bits(gb, TAK_SIZE_FRAME_DURATION_BITS);
00117 s->samples = get_bits64(gb, TAK_SIZE_SAMPLES_NUM_BITS);
00118
00119 s->data_type = get_bits(gb, TAK_FORMAT_DATA_TYPE_BITS);
00120 s->sample_rate = get_bits(gb, TAK_FORMAT_SAMPLE_RATE_BITS) +
00121 TAK_SAMPLE_RATE_MIN;
00122 s->bps = get_bits(gb, TAK_FORMAT_BPS_BITS) +
00123 TAK_BPS_MIN;
00124 s->channels = get_bits(gb, TAK_FORMAT_CHANNEL_BITS) +
00125 TAK_CHANNELS_MIN;
00126
00127 if (get_bits1(gb)) {
00128 skip_bits(gb, TAK_FORMAT_VALID_BITS);
00129 if (get_bits1(gb)) {
00130 for (i = 0; i < s->channels; i++) {
00131 int value = get_bits(gb, TAK_FORMAT_CH_LAYOUT_BITS);
00132
00133 if (value < FF_ARRAY_ELEMS(tak_channel_layouts))
00134 channel_mask |= tak_channel_layouts[value];
00135 }
00136 }
00137 }
00138
00139 s->ch_layout = channel_mask;
00140 s->frame_samples = tak_get_nb_samples(s->sample_rate, frame_type);
00141 }
00142
00143 int ff_tak_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
00144 TAKStreamInfo *ti, int log_level_offset)
00145 {
00146 if (get_bits(gb, TAK_FRAME_HEADER_SYNC_ID_BITS) != TAK_FRAME_HEADER_SYNC_ID) {
00147 av_log(avctx, AV_LOG_ERROR + log_level_offset, "missing sync id\n");
00148 return AVERROR_INVALIDDATA;
00149 }
00150
00151 ti->flags = get_bits(gb, TAK_FRAME_HEADER_FLAGS_BITS);
00152 ti->frame_num = get_bits(gb, TAK_FRAME_HEADER_NO_BITS);
00153
00154 if (ti->flags & TAK_FRAME_FLAG_IS_LAST) {
00155 ti->last_frame_samples = get_bits(gb, TAK_FRAME_HEADER_SAMPLE_COUNT_BITS) + 1;
00156 skip_bits(gb, 2);
00157 } else {
00158 ti->last_frame_samples = 0;
00159 }
00160
00161 if (ti->flags & TAK_FRAME_FLAG_HAS_INFO) {
00162 avpriv_tak_parse_streaminfo(gb, ti);
00163
00164 if (get_bits(gb, 6))
00165 skip_bits(gb, 25);
00166 align_get_bits(gb);
00167 }
00168
00169 if (ti->flags & TAK_FRAME_FLAG_HAS_METADATA)
00170 return AVERROR_INVALIDDATA;
00171
00172 skip_bits(gb, 24);
00173
00174 return 0;
00175 }