]> git.sesse.net Git - ffmpeg/blob - libavcodec/tak.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / tak.c
1 /*
2  * TAK common code
3  * Copyright (c) 2012 Paul B Mahol
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "libavutil/bswap.h"
23 #include "libavutil/crc.h"
24 #include "libavutil/intreadwrite.h"
25 #include "tak.h"
26
27 static const int64_t tak_channel_layouts[] = {
28     0,
29     AV_CH_FRONT_LEFT,
30     AV_CH_FRONT_RIGHT,
31     AV_CH_FRONT_CENTER,
32     AV_CH_LOW_FREQUENCY,
33     AV_CH_BACK_LEFT,
34     AV_CH_BACK_RIGHT,
35     AV_CH_FRONT_LEFT_OF_CENTER,
36     AV_CH_FRONT_RIGHT_OF_CENTER,
37     AV_CH_BACK_CENTER,
38     AV_CH_SIDE_LEFT,
39     AV_CH_SIDE_RIGHT,
40     AV_CH_TOP_CENTER,
41     AV_CH_TOP_FRONT_LEFT,
42     AV_CH_TOP_FRONT_CENTER,
43     AV_CH_TOP_FRONT_RIGHT,
44     AV_CH_TOP_BACK_LEFT,
45     AV_CH_TOP_BACK_CENTER,
46     AV_CH_TOP_BACK_RIGHT,
47 };
48
49 static const uint16_t frame_duration_type_quants[] = {
50     3, 4, 6, 8, 4096, 8192, 16384, 512, 1024, 2048,
51 };
52
53 static int tak_get_nb_samples(int sample_rate, enum TAKFrameSizeType type)
54 {
55     int nb_samples, max_nb_samples;
56
57     if (type <= TAK_FST_250ms) {
58         nb_samples     = sample_rate * frame_duration_type_quants[type] >>
59                                        TAK_FRAME_DURATION_QUANT_SHIFT;
60         max_nb_samples = 16384;
61     } else if (type < FF_ARRAY_ELEMS(frame_duration_type_quants)) {
62         nb_samples     = frame_duration_type_quants[type];
63         max_nb_samples = sample_rate * frame_duration_type_quants[TAK_FST_250ms] >>
64                                        TAK_FRAME_DURATION_QUANT_SHIFT;
65     } else {
66         return AVERROR_INVALIDDATA;
67     }
68
69     if (nb_samples <= 0 || nb_samples > max_nb_samples)
70         return AVERROR_INVALIDDATA;
71
72     return nb_samples;
73 }
74
75 static int crc_init = 0;
76 #if CONFIG_SMALL
77 #define CRC_TABLE_SIZE 257
78 #else
79 #define CRC_TABLE_SIZE 1024
80 #endif
81 static AVCRC crc_24[CRC_TABLE_SIZE];
82
83 av_cold void ff_tak_init_crc(void)
84 {
85     if (!crc_init) {
86         av_crc_init(crc_24, 0, 24, 0x864CFBU, sizeof(crc_24));
87         crc_init = 1;
88     }
89 }
90
91 int ff_tak_check_crc(const uint8_t *buf, unsigned int buf_size)
92 {
93     uint32_t crc, CRC;
94
95     if (buf_size < 4)
96         return AVERROR_INVALIDDATA;
97     buf_size -= 3;
98
99     CRC = av_bswap32(AV_RL24(buf + buf_size)) >> 8;
100     crc = av_crc(crc_24, 0xCE04B7U, buf, buf_size);
101     if (CRC != crc)
102         return AVERROR_INVALIDDATA;
103
104     return 0;
105 }
106
107 void avpriv_tak_parse_streaminfo(GetBitContext *gb, TAKStreamInfo *s)
108 {
109     uint64_t channel_mask = 0;
110     int frame_type, i;
111
112     s->codec = get_bits(gb, TAK_ENCODER_CODEC_BITS);
113     skip_bits(gb, TAK_ENCODER_PROFILE_BITS);
114
115     frame_type = get_bits(gb, TAK_SIZE_FRAME_DURATION_BITS);
116     s->samples = get_bits64(gb, TAK_SIZE_SAMPLES_NUM_BITS);
117
118     s->data_type   = get_bits(gb, TAK_FORMAT_DATA_TYPE_BITS);
119     s->sample_rate = get_bits(gb, TAK_FORMAT_SAMPLE_RATE_BITS) + TAK_SAMPLE_RATE_MIN;
120     s->bps         = get_bits(gb, TAK_FORMAT_BPS_BITS) + TAK_BPS_MIN;
121     s->channels    = get_bits(gb, TAK_FORMAT_CHANNEL_BITS) + TAK_CHANNELS_MIN;
122
123     if (get_bits1(gb)) {
124         skip_bits(gb, TAK_FORMAT_VALID_BITS);
125         if (get_bits1(gb)) {
126             for (i = 0; i < s->channels; i++) {
127                 int value = get_bits(gb, TAK_FORMAT_CH_LAYOUT_BITS);
128
129                 if (value < FF_ARRAY_ELEMS(tak_channel_layouts))
130                     channel_mask |= tak_channel_layouts[value];
131             }
132         }
133     }
134
135     s->ch_layout     = channel_mask;
136     s->frame_samples = tak_get_nb_samples(s->sample_rate, frame_type);
137 }
138
139 #define FRAME_IS_LAST       1
140 #define FRAME_HAVE_INFO     2
141 #define FRAME_HAVE_METADATA 4
142
143 int ff_tak_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
144                                TAKStreamInfo *ti, int log_level_offset)
145 {
146     int flags;
147
148     if (get_bits(gb, TAK_FRAME_HEADER_SYNC_ID_BITS) != TAK_FRAME_HEADER_SYNC_ID) {
149         av_log(avctx, AV_LOG_ERROR + log_level_offset, "missing sync id\n");
150         return AVERROR_INVALIDDATA;
151     }
152
153     flags = get_bits(gb, TAK_FRAME_HEADER_FLAGS_BITS);
154     ti->frame_num = get_bits(gb, TAK_FRAME_HEADER_NO_BITS);
155
156     if (flags & FRAME_IS_LAST) {
157         ti->last_frame_samples = get_bits(gb, TAK_FRAME_HEADER_SAMPLE_COUNT_BITS) + 1;
158         skip_bits(gb, 2);
159     } else {
160         ti->last_frame_samples = 0;
161     }
162
163     if (flags & FRAME_HAVE_INFO) {
164         avpriv_tak_parse_streaminfo(gb, ti);
165
166         if (get_bits(gb, 6))
167             skip_bits(gb, 25);
168         align_get_bits(gb);
169     }
170
171     if (flags & FRAME_HAVE_METADATA)
172         return AVERROR_INVALIDDATA;
173
174     skip_bits(gb, 24);
175
176     return 0;
177 }