• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * AV3A Common Header File
3  *
4  * Copyright (c) 2024 Shuai Liu <cqliushuai@outlook.com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #ifndef AVCODEC_AV3A_H
24 #define AVCODEC_AV3A_H
25 
26 #include "libavutil/samplefmt.h"
27 #include "libavutil/channel_layout.h"
28 
29 /* AATF header */
30 #define AV3A_MAX_NBYTES_HEADER          9
31 #define AV3A_AUDIO_SYNC_WORD            0xFFF
32 #define AV3A_AUDIO_FRAME_SIZE           1024
33 #define AV3A_CHANNEL_LAYOUT_SIZE        15
34 #define AV3A_BITRATE_TABLE_SIZE         16
35 #define AV3A_FS_TABLE_SIZE              9
36 #define AV3A_RESOLUTION_TABLE_SIZE      3
37 #define AV3A_DCA3_BOX_MIN_SIZE          5
38 #define AV3A_DCA3_BOX_MAX_SIZE          7
39 
40 /* Channel Layout */
41 #define AV3A_CH_LAYOUT_MONO             (AV_CH_LAYOUT_MONO)
42 #define AV3A_CH_LAYOUT_STEREO           (AV_CH_LAYOUT_STEREO)
43 #define AV3A_CH_LAYOUT_4POINT0          (AV3A_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER|AV_CH_BACK_CENTER)
44 #define AV3A_CH_LAYOUT_5POINT1          (AV_CH_LAYOUT_5POINT1)
45 #define AV3A_CH_LAYOUT_7POINT1          (AV_CH_LAYOUT_5POINT1|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT)
46 #define AV3A_CH_LAYOUT_5POINT1POINT2    (AV_CH_LAYOUT_5POINT1|AV_CH_TOP_SIDE_LEFT|AV_CH_TOP_SIDE_RIGHT)
47 #define AV3A_CH_LAYOUT_7POINT1POINT2    (AV3A_CH_LAYOUT_7POINT1|AV_CH_TOP_SIDE_LEFT|AV_CH_TOP_SIDE_RIGHT)
48 #define AV3A_CH_LAYOUT_5POINT1POINT4    (AV_CH_LAYOUT_5POINT1|AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_FRONT_RIGHT|AV_CH_TOP_BACK_LEFT|AV_CH_TOP_BACK_RIGHT)
49 #define AV3A_CH_LAYOUT_7POINT1POINT4    (AV3A_CH_LAYOUT_7POINT1|AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_FRONT_RIGHT|AV_CH_TOP_BACK_LEFT|AV_CH_TOP_BACK_RIGHT)
50 #define AV3A_CH_AUDIO_OBJECT            (AV_CHAN_UNKNOWN)
51 
52 /* AV3A Codec ID */
53 typedef enum {
54     AV3A_LOSSLESS_CODEC_ID = 1,
55     AV3A_LOSSY_CODEC_ID    = 2
56 } Av3aCodecId;
57 
58 /* Content Type */
59 typedef enum {
60     AV3A_CHANNEL_BASED_TYPE  = 0,
61     AV3A_OBJECT_BASED_TYPE   = 1,
62     AV3A_CHANNEL_OBJECT_TYPE = 2,
63     AV3A_AMBISONIC_TYPE      = 3
64 } Av3aContentType;
65 
66 /* Internal Coding Profile */
67 typedef enum {
68     AV3A_BASE_PROFILE            = 0,
69     AV3A_OBJECT_METADATA_PROFILE = 1,
70     AV3A_AMBISONIC_PROFILE       = 2
71 } Av3aCodingProfile;
72 
73 /* NN Type */
74 typedef enum {
75     AV3A_BASELINE_NN_TYPE = 0,
76     AV3A_LC_NN_TYPE       = 1
77 } Av3aNeuralNetworkType;
78 
79 /* AV3A Channel Configuration */
80 typedef enum {
81     CHANNEL_CONFIG_MONO       = 0,  /* Mono    = 0  */
82     CHANNEL_CONFIG_STEREO     = 1,  /* Stereo  = 1  */
83     CHANNEL_CONFIG_MC_5_1     = 2,  /* 5.1     = 2  */
84     CHANNEL_CONFIG_MC_7_1     = 3,  /* 7.1     = 3  */
85     CHANNEL_CONFIG_MC_10_2    = 4,  /* 10.2    = 4  */
86     CHANNEL_CONFIG_MC_22_2    = 5,  /* 22.2    = 5  */
87     CHANNEL_CONFIG_MC_4_0     = 6,  /* 4.0     = 6  */
88     CHANNEL_CONFIG_MC_5_1_2   = 7,  /* 5.1.2   = 7  */
89     CHANNEL_CONFIG_MC_5_1_4   = 8,  /* 5.1.4   = 8  */
90     CHANNEL_CONFIG_MC_7_1_2   = 9,  /* 7.1.2   = 9  */
91     CHANNEL_CONFIG_MC_7_1_4   = 10, /* 7.1.4   = 10 */
92     CHANNEL_CONFIG_HOA_ORDER1 = 11, /* HOA1    = 11 */
93     CHANNEL_CONFIG_HOA_ORDER2 = 12, /* HOA2    = 12 */
94     CHANNEL_CONFIG_HOA_ORDER3 = 13, /* HOA3    = 13 */
95     CHANNEL_CONFIG_UNKNOWN    = 14  /* UNKNOWN = 14 */
96 } Av3aChannelConfig;
97 
98 typedef enum {
99     AV3A_AMBISONIC_FIRST_ORDER  = 1,
100     AV3A_AMBISONIC_SECOND_ORDER = 2,
101     AV3A_AMBISONIC_THIRD_ORDER  = 3
102 } Av3aAmbisonicOrder;
103 
104 typedef struct {
105     int16_t sync_word;                 /* sync word */
106     int16_t audio_codec_id;            /* audio codec id */
107     int16_t anc_data;                  /* anc data */
108     int16_t nn_type;                   /* neural network type */
109     int16_t coding_profile;            /* coding profile */
110     int16_t sampling_frequency_index;  /* sampling frequency index */
111     int16_t channel_number_index;      /* channel number index */
112     int16_t bitrate_index;             /* bitrate index */
113     int16_t soundbed_type;             /* soundbed type  */
114     int16_t object_channel_number;     /* object channel number */
115     int16_t bitrate_index_per_channel; /* bitrate per object */
116     int16_t order;                     /* ambisonics order */
117     int16_t resolution_index;          /* resolution index */
118 
119     int32_t sampling_rate;             /* sampling rate */
120     int64_t total_bitrate;             /* total bitrate */
121     int16_t sample_format;             /* sample format */
122     int16_t resolution;                /* resolution */
123     int16_t content_type;              /* internal content type */
124     int16_t nb_channels;               /* number of channels (channel configuration) */
125     int16_t nb_objects;                /* number of objects (object_channel_number + 1) */
126     int16_t total_channels;            /* total channels */
127     int16_t hoa_order;                 /* ambisonic order (order + 1) */
128     int32_t ch_layout_mask;            /* channel layout mask */
129 } AATFHeaderInfo;
130 
131 /* bitrate table for mono */
132 static const int64_t ff_av3a_mono_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
133     16000, 32000, 44000, 56000, 64000, 72000, 80000, 96000, 128000, 144000,
134     164000, 192000, 0, 0, 0, 0
135 };
136 
137 /* bitrate table for stereo */
138 static const int64_t ff_av3a_stereo_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
139     24000, 32000, 48000, 64000, 80000, 96000, 128000, 144000, 192000, 256000,
140     320000, 0, 0, 0, 0, 0
141 };
142 
143 /* bitrate table for MC 5.1 */
144 static const int64_t ff_av3a_mc5p1_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
145     192000, 256000, 320000, 384000, 448000, 512000, 640000, 720000, 144000, 96000,
146     128000, 160000, 0, 0, 0, 0
147 };
148 
149 /* bitrate table for MC 7.1 */
150 static const int64_t ff_av3a_mc7p1_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
151     192000, 480000, 256000, 384000, 576000, 640000, 128000, 160000, 0, 0,
152     0, 0, 0, 0, 0, 0
153 };
154 
155 /* bitrate table for MC 4.0 */
156 static const int64_t ff_av3a_mc4p0_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
157     48000, 96000, 128000, 192000, 256000, 0, 0, 0, 0, 0,
158     0, 0, 0, 0, 0, 0
159 };
160 
161 /* bitrate table for MC 5.1.2 */
162 static const int64_t ff_av3a_mc5p1p2_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
163     152000, 320000, 480000, 576000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
164 };
165 
166 /* bitrate table for MC 5.1.4 */
167 static const int64_t ff_av3a_mc5p1p4_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
168     176000, 384000, 576000, 704000, 256000, 448000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
169 };
170 
171 /* bitrate table for MC 7.1.2 */
172 static const int64_t ff_av3a_mc7p1p2_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
173     216000, 480000, 576000, 384000, 768000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
174 };
175 
176 /* bitrate table for MC 7.1.4 */
177 static const int64_t ff_av3a_mc7p1p4_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
178     240000, 608000, 384000, 512000, 832000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
179 };
180 
181 /* bitrate table for FOA */
182 static const int64_t ff_av3a_foa_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
183     48000, 96000, 128000, 192000, 256000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
184 };
185 
186 /* bitrate table for HOA2 */
187 static const int64_t ff_av3a_hoa2_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
188     192000, 256000, 320000, 384000, 480000, 512000, 640000, 0, 0, 0, 0, 0, 0, 0, 0, 0
189 };
190 
191 /* bitrate table for HOA3 */
192 static const int64_t ff_av3a_hoa3_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
193     256000, 320000, 384000, 512000, 640000, 896000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
194 
195 static const int32_t ff_av3a_sampling_rate_table[AV3A_FS_TABLE_SIZE] = {
196     192000, 96000, 48000, 44100, 32000, 24000, 22050, 16000, 8000
197 };
198 
199 typedef struct {
200     int16_t resolution;
201     enum AVSampleFormat sample_format;
202 } Av3aSampleFormatMap;
203 
204 static const Av3aSampleFormatMap ff_av3a_sample_format_map_table[AV3A_RESOLUTION_TABLE_SIZE] = {
205     {8,  AV_SAMPLE_FMT_U8 }, /* 0: 8 bits  */
206     {16, AV_SAMPLE_FMT_S16}, /* 1: 16 bits */
207     {24, AV_SAMPLE_FMT_S32}, /* 2: 24 bits */
208 };
209 
210 typedef struct {
211     Av3aChannelConfig channel_number_index;
212     int16_t channels;
213     const enum AVChannel* channel_layout;
214     uint64_t mask;
215 } Av3aChannelConfigMap;
216 
217 static const enum AVChannel ff_av3a_default_channel_layout_mono[1] = {
218     AV_CHAN_FRONT_CENTER
219 };
220 
221 static const enum AVChannel ff_av3a_default_channel_layout_stereo[2] = {
222     AV_CHAN_FRONT_LEFT, AV_CHAN_FRONT_RIGHT
223 };
224 
225 static const enum AVChannel ff_av3a_channel_layout_mc_4_0[4] = {
226     AV_CHAN_FRONT_LEFT, AV_CHAN_FRONT_RIGHT,
227     AV_CHAN_FRONT_CENTER, AV_CHAN_BACK_CENTER
228 };
229 
230 static const enum AVChannel ff_av3a_default_channel_layout_mc_5_1[6] = {
231     AV_CHAN_FRONT_LEFT, AV_CHAN_FRONT_RIGHT, AV_CHAN_FRONT_CENTER,
232     AV_CHAN_LOW_FREQUENCY,
233     AV_CHAN_SIDE_LEFT, AV_CHAN_SIDE_RIGHT
234 };
235 
236 static const enum AVChannel ff_av3a_default_channel_layout_mc_5_1_2[8] = {
237     AV_CHAN_FRONT_LEFT, AV_CHAN_FRONT_RIGHT, AV_CHAN_FRONT_CENTER,
238     AV_CHAN_LOW_FREQUENCY,
239     AV_CHAN_SIDE_LEFT, AV_CHAN_SIDE_RIGHT,
240     AV_CHAN_TOP_SIDE_LEFT, AV_CHAN_TOP_SIDE_RIGHT
241 };
242 
243 static const enum AVChannel ff_av3a_default_channel_layout_mc_7_1[8] = {
244     AV_CHAN_FRONT_LEFT, AV_CHAN_FRONT_RIGHT, AV_CHAN_FRONT_CENTER,
245     AV_CHAN_LOW_FREQUENCY,
246     AV_CHAN_SIDE_LEFT, AV_CHAN_SIDE_RIGHT,
247     AV_CHAN_BACK_LEFT, AV_CHAN_BACK_RIGHT
248 };
249 
250 static const enum AVChannel ff_av3a_default_channel_layout_mc_5_1_4[10] = {
251     AV_CHAN_FRONT_LEFT, AV_CHAN_FRONT_RIGHT, AV_CHAN_FRONT_CENTER,
252     AV_CHAN_LOW_FREQUENCY,
253     AV_CHAN_SIDE_LEFT, AV_CHAN_SIDE_RIGHT,
254     AV_CHAN_TOP_FRONT_LEFT, AV_CHAN_TOP_FRONT_RIGHT,
255     AV_CHAN_TOP_BACK_LEFT, AV_CHAN_TOP_BACK_RIGHT
256 };
257 
258 static const enum AVChannel ff_av3a_default_channel_layout_mc_7_1_2[10] = {
259     AV_CHAN_FRONT_LEFT, AV_CHAN_FRONT_RIGHT, AV_CHAN_FRONT_CENTER,
260     AV_CHAN_LOW_FREQUENCY,
261     AV_CHAN_SIDE_LEFT, AV_CHAN_SIDE_RIGHT,
262     AV_CHAN_BACK_LEFT, AV_CHAN_BACK_RIGHT,
263     AV_CHAN_TOP_SIDE_LEFT, AV_CHAN_TOP_SIDE_RIGHT
264 };
265 
266 static const enum AVChannel ff_av3a_default_channel_layout_mc_7_1_4[12] = {
267     AV_CHAN_FRONT_LEFT, AV_CHAN_FRONT_RIGHT, AV_CHAN_FRONT_CENTER,
268     AV_CHAN_LOW_FREQUENCY,
269     AV_CHAN_SIDE_LEFT, AV_CHAN_SIDE_RIGHT,
270     AV_CHAN_BACK_LEFT, AV_CHAN_BACK_RIGHT,
271     AV_CHAN_TOP_FRONT_LEFT, AV_CHAN_TOP_FRONT_RIGHT,
272     AV_CHAN_TOP_BACK_LEFT, AV_CHAN_TOP_BACK_RIGHT
273 };
274 
275 static const Av3aChannelConfigMap ff_av3a_channels_map_table[AV3A_CHANNEL_LAYOUT_SIZE] = {
276     { CHANNEL_CONFIG_MONO,       1,  ff_av3a_default_channel_layout_mono,     AV3A_CH_LAYOUT_MONO          },
277     { CHANNEL_CONFIG_STEREO,     2,  ff_av3a_default_channel_layout_stereo,   AV3A_CH_LAYOUT_STEREO        },
278     { CHANNEL_CONFIG_MC_5_1,     6,  ff_av3a_default_channel_layout_mc_5_1,   AV3A_CH_LAYOUT_5POINT1       },
279     { CHANNEL_CONFIG_MC_7_1,     8,  ff_av3a_default_channel_layout_mc_7_1,   AV3A_CH_LAYOUT_7POINT1       },
280     { CHANNEL_CONFIG_MC_10_2,    12, NULL,                                    0L                           }, /* reserved */
281     { CHANNEL_CONFIG_MC_22_2,    24, NULL,                                    0L                           }, /* reserved */
282     { CHANNEL_CONFIG_MC_4_0,     4,  ff_av3a_channel_layout_mc_4_0,           AV3A_CH_LAYOUT_4POINT0       },
283     { CHANNEL_CONFIG_MC_5_1_2,   8,  ff_av3a_default_channel_layout_mc_5_1_2, AV3A_CH_LAYOUT_5POINT1POINT2 },
284     { CHANNEL_CONFIG_MC_5_1_4,   10, ff_av3a_default_channel_layout_mc_5_1_4, AV3A_CH_LAYOUT_5POINT1POINT4 },
285     { CHANNEL_CONFIG_MC_7_1_2,   10, ff_av3a_default_channel_layout_mc_7_1_2, AV3A_CH_LAYOUT_7POINT1POINT2 },
286     { CHANNEL_CONFIG_MC_7_1_4,   12, ff_av3a_default_channel_layout_mc_7_1_4, AV3A_CH_LAYOUT_7POINT1POINT4 },
287     { CHANNEL_CONFIG_HOA_ORDER1, 4,  NULL,                                    0L                           },
288     { CHANNEL_CONFIG_HOA_ORDER2, 9,  NULL,                                    0L                           },
289     { CHANNEL_CONFIG_HOA_ORDER3, 16, NULL,                                    0L                           },
290     { CHANNEL_CONFIG_UNKNOWN,    0,  NULL,                                    0L                           },
291 };
292 
293 typedef struct {
294     Av3aChannelConfig channel_number_index;
295     const int64_t *bitrate_table;
296 } Av3aBitrateMap;
297 
298 static const Av3aBitrateMap ff_av3a_bitrate_map_table[15] = {
299     {CHANNEL_CONFIG_MONO,       ff_av3a_mono_bitrate_table    },
300     {CHANNEL_CONFIG_STEREO,     ff_av3a_stereo_bitrate_table  },
301     {CHANNEL_CONFIG_MC_5_1,     ff_av3a_mc5p1_bitrate_table   },
302     {CHANNEL_CONFIG_MC_7_1,     ff_av3a_mc7p1_bitrate_table   },
303     {CHANNEL_CONFIG_MC_10_2,    NULL                          }, /* reserved */
304     {CHANNEL_CONFIG_MC_22_2,    NULL                          }, /* reserved */
305     {CHANNEL_CONFIG_MC_4_0,     ff_av3a_mc4p0_bitrate_table   },
306     {CHANNEL_CONFIG_MC_5_1_2,   ff_av3a_mc5p1p2_bitrate_table },
307     {CHANNEL_CONFIG_MC_5_1_4,   ff_av3a_mc5p1p4_bitrate_table },
308     {CHANNEL_CONFIG_MC_7_1_2,   ff_av3a_mc7p1p2_bitrate_table },
309     {CHANNEL_CONFIG_MC_7_1_4,   ff_av3a_mc7p1p4_bitrate_table },
310     {CHANNEL_CONFIG_HOA_ORDER1, ff_av3a_foa_bitrate_table     },
311     {CHANNEL_CONFIG_HOA_ORDER2, ff_av3a_hoa2_bitrate_table    },
312     {CHANNEL_CONFIG_HOA_ORDER3, ff_av3a_hoa3_bitrate_table    },
313     {CHANNEL_CONFIG_UNKNOWN,    NULL                          },
314 };
315 #endif /* AVCODEC_AV3A_H */
316