1 /*
2 * av3a parser
3 *
4 * Copyright (c) 2018 James Almer <jamrial@gmail.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 #include <stdio.h>
24 #include <stdint.h>
25 #include "libavutil/channel_layout.h"
26 #include "libavutil/samplefmt.h"
27 #include "libavutil/intreadwrite.h"
28 #include "parser.h"
29 #include "get_bits.h"
30
31 /* AVS3 header */
32 #define AVS3_AUDIO_HEADER_SIZE 7
33 #define AVS3_SYNC_WORD_SIZE 2
34 #define MAX_NBYTES_FRAME_HEADER 9
35 #define AVS3_AUDIO_SYNC_WORD 0xFFF
36
37 #define AVS3_AUDIO_FRAME_SIZE 1024
38 #define AVS3_SIZE_BITRATE_TABLE 16
39 #define AVS3_SIZE_FS_TABLE 9
40
41 /* AVS3 Audio Format */
42 #define AVS3_MONO_FORMAT 0
43 #define AVS3_STEREO_FORMAT 1
44 #define AVS3_MC_FORMAT 2
45 #define AVS3_HOA_FORMAT 3
46 #define AVS3_MIX_FORMAT 4
47
48 #define AVS3_SIZE_MC_CONFIG_TABLE 10
49
50 #define AVS3P3_CH_LAYOUT_5POINT1 (AV_CH_LAYOUT_SURROUND | AV_CH_LOW_FREQUENCY | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
51
52 typedef struct AVS3AudioParseContext {
53 int32_t frame_size;
54 int32_t bitdepth;
55 int32_t sample_rate;
56 uint64_t bit_rate;
57 uint16_t channels;
58 uint64_t channel_layout;
59 } AVS3AParseContext;
60
61 // AVS3P3 header information
62 typedef struct {
63 // header info
64 uint8_t codec_id;
65 uint8_t sampling_rate_index;
66 int32_t sampling_rate;
67
68 uint16_t bitdepth;
69 uint16_t channels;
70 uint16_t objects;
71 uint16_t hoa_order;
72 uint64_t channel_layout;
73 int64_t total_bitrate;
74
75 // configuration
76 uint8_t content_type;
77 uint16_t channel_num_index;
78 uint16_t total_channels;
79 uint8_t resolution;
80 uint8_t nn_type;
81 uint8_t resolution_index;
82 } AVS3AHeaderInfo;
83
84 typedef enum {
85 CHANNEL_CONFIG_MONO = 0,
86 CHANNEL_CONFIG_STEREO = 1,
87 CHANNEL_CONFIG_MC_5_1,
88 CHANNEL_CONFIG_MC_7_1,
89 CHANNEL_CONFIG_MC_10_2,
90 CHANNEL_CONFIG_MC_22_2,
91 CHANNEL_CONFIG_MC_4_0,
92 CHANNEL_CONFIG_MC_5_1_2,
93 CHANNEL_CONFIG_MC_5_1_4,
94 CHANNEL_CONFIG_MC_7_1_2,
95 CHANNEL_CONFIG_MC_7_1_4,
96 CHANNEL_CONFIG_HOA_ORDER1,
97 CHANNEL_CONFIG_HOA_ORDER2,
98 CHANNEL_CONFIG_HOA_ORDER3,
99 CHANNEL_CONFIG_UNKNOWN
100 } AVS3AChannelConfig;
101
102 /* Codec bitrate config struct */
103 typedef struct CodecBitrateConfigStructure {
104 AVS3AChannelConfig channelNumConfig;
105 const int64_t *bitrateTable;
106 } CodecBitrateConfig;
107
108 typedef struct McChannelConfigStructure {
109 const char mcCmdString[10];
110 AVS3AChannelConfig channelNumConfig;
111 const int16_t numChannels;
112 } McChanelConfig;
113
114 static const McChanelConfig mcChannelConfigTable[AVS3_SIZE_MC_CONFIG_TABLE] = {
115 {"STEREO", CHANNEL_CONFIG_STEREO, 2},
116 {"MC_5_1_0", CHANNEL_CONFIG_MC_5_1, 6},
117 {"MC_7_1_0", CHANNEL_CONFIG_MC_7_1, 8},
118 {"MC_10_2", CHANNEL_CONFIG_MC_10_2, 12},
119 {"MC_22_2", CHANNEL_CONFIG_MC_22_2, 24},
120 {"MC_4_0", CHANNEL_CONFIG_MC_4_0, 4},
121 {"MC_5_1_2", CHANNEL_CONFIG_MC_5_1_2, 8},
122 {"MC_5_1_4", CHANNEL_CONFIG_MC_5_1_4, 10},
123 {"MC_7_1_2", CHANNEL_CONFIG_MC_7_1_2, 10},
124 {"MC_7_1_4", CHANNEL_CONFIG_MC_7_1_4, 12}
125 };
126
127 static const int32_t avs3_samplingrate_table[AVS3_SIZE_FS_TABLE] = {
128 192000, 96000, 48000, 44100, 32000, 24000, 22050, 16000, 8000
129 };
130
131 // bitrate table for mono
132 static const int64_t bitrateTableMono[AVS3_SIZE_BITRATE_TABLE] = {
133 16000, 32000, 44000, 56000, 64000, 72000, 80000, 96000, 128000, 144000, 164000, 192000, 0, 0, 0, 0
134 };
135
136 // bitrate table for stereo
137 static const int64_t bitrateTableStereo[AVS3_SIZE_BITRATE_TABLE] = {
138 24000, 32000, 48000, 64000, 80000, 96000, 128000, 144000, 192000, 256000, 320000, 0, 0, 0, 0, 0
139 };
140
141 // bitrate table for MC 5.1
142 static const int64_t bitrateTableMC5P1[AVS3_SIZE_BITRATE_TABLE] = {
143 192000, 256000, 320000, 384000, 448000, 512000, 640000, 720000, 144000, 96000, 128000, 160000, 0, 0, 0, 0
144 };
145
146 // bitrate table for MC 7.1
147 static const int64_t bitrateTableMC7P1[AVS3_SIZE_BITRATE_TABLE] = {
148 192000, 480000, 256000, 384000, 576000, 640000, 128000, 160000, 0, 0, 0, 0, 0, 0, 0, 0
149 };
150
151 // bitrate table for MC 4.0
152 static const int64_t bitrateTableMC4P0[AVS3_SIZE_BITRATE_TABLE] = {
153 48000, 96000, 128000, 192000, 256000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
154 };
155
156 // bitrate table for MC 5.1.2
157 static const int64_t bitrateTableMC5P1P2[AVS3_SIZE_BITRATE_TABLE] = {
158 152000, 320000, 480000, 576000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
159 };
160
161 // bitrate table for MC 5.1.4
162 static const int64_t bitrateTableMC5P1P4[AVS3_SIZE_BITRATE_TABLE] = {
163 176000, 384000, 576000, 704000, 256000, 448000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
164 };
165
166 // bitrate table for MC 7.1.2
167 static const int64_t bitrateTableMC7P1P2[AVS3_SIZE_BITRATE_TABLE] = {
168 216000, 480000, 576000, 384000, 768000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
169 };
170
171 // bitrate table for MC 7.1.4
172 static const int64_t bitrateTableMC7P1P4[AVS3_SIZE_BITRATE_TABLE] = {
173 240000, 608000, 384000, 512000, 832000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
174 };
175
176 static const int64_t bitrateTableFoa[AVS3_SIZE_BITRATE_TABLE] = {
177 48000, 96000, 128000, 192000, 256000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
178 };
179
180 static const int64_t bitrateTableHoa2[AVS3_SIZE_BITRATE_TABLE] = {
181 192000, 256000, 320000, 384000, 480000, 512000, 640000, 0, 0, 0, 0, 0, 0, 0, 0, 0
182 };
183
184 // bitrate table for HOA order 3
185 static const int64_t bitrateTableHoa3[AVS3_SIZE_BITRATE_TABLE] = {
186 256000, 320000, 384000, 512000, 640000, 896000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
187 };
188
189 // Codec channel number & bitrate config table
190 // format: {channelConfigIdx, numChannels, bitrateTable}
191 static const CodecBitrateConfig codecBitrateConfigTable[CHANNEL_CONFIG_UNKNOWN] = {
192 {CHANNEL_CONFIG_MONO, bitrateTableMono},
193 {CHANNEL_CONFIG_STEREO, bitrateTableStereo},
194 {CHANNEL_CONFIG_MC_5_1, bitrateTableMC5P1},
195 {CHANNEL_CONFIG_MC_7_1, bitrateTableMC7P1},
196 {CHANNEL_CONFIG_MC_10_2, NULL},
197 {CHANNEL_CONFIG_MC_22_2, NULL},
198 {CHANNEL_CONFIG_MC_4_0, bitrateTableMC4P0},
199 {CHANNEL_CONFIG_MC_5_1_2, bitrateTableMC5P1P2},
200 {CHANNEL_CONFIG_MC_5_1_4, bitrateTableMC5P1P4},
201 {CHANNEL_CONFIG_MC_7_1_2, bitrateTableMC7P1P2},
202 {CHANNEL_CONFIG_MC_7_1_4, bitrateTableMC7P1P4},
203 {CHANNEL_CONFIG_HOA_ORDER1, bitrateTableFoa},
204 {CHANNEL_CONFIG_HOA_ORDER2, bitrateTableHoa2},
205 {CHANNEL_CONFIG_HOA_ORDER3, bitrateTableHoa3}
206 };
207
read_av3a_frame_header(AVS3AHeaderInfo * hdf,const uint8_t * buf,const int32_t byte_size)208 static int read_av3a_frame_header(AVS3AHeaderInfo *hdf, const uint8_t *buf, const int32_t byte_size)
209 {
210 GetBitContext gb;
211 AVS3AChannelConfig channel_config;
212
213 uint8_t content_type = 0;
214 uint8_t hoa_order = 0;
215 uint8_t bitdepth = 0;
216 uint8_t resolution = 0;
217
218 int16_t channels = 0;
219 int16_t objects = 0;
220 uint64_t channel_layout = 0;
221
222 int64_t bitrate_per_obj = 0;
223 int64_t bitrate_bed_mc = 0;
224 int64_t total_bitrate = 0;
225
226 uint8_t num_chan_index = 0;
227 uint8_t obj_brt_idx = 0;
228 uint8_t bed_brt_idx = 0;
229 uint8_t brt_idx = 0;
230
231 // Read max header length into bs buffer
232 init_get_bits8(&gb, buf, MAX_NBYTES_FRAME_HEADER);
233
234 // 12 bits for frame sync word
235 if (get_bits(&gb, 12) != AVS3_AUDIO_SYNC_WORD) {
236 return AVERROR_INVALIDDATA;
237 }
238
239 // 4 bits for codec id
240 uint8_t codec_id = get_bits(&gb, 4);
241 if (codec_id != 2) {
242 return AVERROR_INVALIDDATA;
243 }
244
245 // 1 bits for anc data
246 if (get_bits(&gb, 1) != 0) {
247 return AVERROR_INVALIDDATA;
248 }
249
250 // 3 bits nn type
251 uint8_t nn_type = get_bits(&gb, 3);
252
253 // 3 bits for coding profile
254 uint8_t coding_profile = get_bits(&gb, 3);
255
256 // 4 bits for sampling index
257 uint8_t samping_rate_index = get_bits(&gb, 4);
258
259 // skip 8 bits for CRC first part
260 skip_bits(&gb, 8);
261
262 if (coding_profile == 0) {
263 content_type = 0;
264
265 // 7 bits for mono/stereo/MC
266 num_chan_index = get_bits(&gb, 7);
267
268 channel_config = (AVS3AChannelConfig)num_chan_index;
269 switch (channel_config) {
270 case CHANNEL_CONFIG_MONO:
271 channels = 1;
272 channel_layout = AV_CH_LAYOUT_MONO;
273 break;
274 case CHANNEL_CONFIG_STEREO:
275 channels = 2;
276 channel_layout = AV_CH_LAYOUT_STEREO;
277 break;
278 case CHANNEL_CONFIG_MC_4_0:
279 channels = 4;
280 break;
281 case CHANNEL_CONFIG_MC_5_1:
282 channels = 6;
283 channel_layout = AVS3P3_CH_LAYOUT_5POINT1;
284 break;
285 case CHANNEL_CONFIG_MC_7_1:
286 channels = 8;
287 channel_layout = AV_CH_LAYOUT_7POINT1;
288 break;
289 case CHANNEL_CONFIG_MC_5_1_2:
290 channels = 8;
291 break;
292 case CHANNEL_CONFIG_MC_5_1_4:
293 channels = 10;
294 break;
295 case CHANNEL_CONFIG_MC_7_1_2:
296 channels = 10;
297 break;
298 case CHANNEL_CONFIG_MC_7_1_4:
299 channels = 12;
300 break;
301 case CHANNEL_CONFIG_MC_22_2:
302 channels = 24;
303 channel_layout = AV_CH_LAYOUT_22POINT2;
304 break;
305 default:
306 break;
307 }
308 } else if (coding_profile == 1) {
309 // sound bed type, 2bits
310 uint8_t soundBedType = get_bits(&gb, 2);
311
312 if (soundBedType == 0) {
313 content_type = 1;
314
315 // for only objects
316 // object number, 7 bits
317 objects = get_bits(&gb, 7);
318 objects += 1;
319
320 // bitrate index for each obj, 4 bits
321 obj_brt_idx = get_bits(&gb, 4);
322
323 total_bitrate = codecBitrateConfigTable[CHANNEL_CONFIG_MONO].bitrateTable[obj_brt_idx] * objects;
324 } else if (soundBedType == 1) {
325 content_type = 2;
326
327 // for MC + objs
328 // channel number index, 7 bits
329 num_chan_index = get_bits(&gb, 7);
330
331 // bitrate index for sound bed, 4 bits
332 bed_brt_idx = get_bits(&gb, 4);
333
334 // object number, 7 bits
335 objects = get_bits(&gb, 7);
336 objects += 1;
337
338 // bitrate index for each obj, 4 bits
339 obj_brt_idx = get_bits(&gb, 4);
340
341 // channelNumIdx for sound bed
342 channel_config = (AVS3AChannelConfig)num_chan_index;
343
344 // sound bed bitrate
345 bitrate_bed_mc = codecBitrateConfigTable[channel_config].bitrateTable[bed_brt_idx];
346
347 // numChannels for sound bed
348 for (int16_t i = 0; i < AVS3_SIZE_MC_CONFIG_TABLE; i++) {
349 if (channel_config == mcChannelConfigTable[i].channelNumConfig) {
350 channels = mcChannelConfigTable[i].numChannels;
351 }
352 }
353
354 // bitrate per obj
355 bitrate_per_obj = codecBitrateConfigTable[CHANNEL_CONFIG_MONO].bitrateTable[obj_brt_idx];
356
357 // add num chans and num objs to get total chans
358 /* channels += objects; */
359
360 total_bitrate = bitrate_bed_mc + bitrate_per_obj * objects;
361 }
362 } else if (coding_profile == 2) {
363 content_type = 3;
364
365 // 4 bits for HOA order
366 hoa_order = get_bits(&gb, 4);
367 hoa_order += 1;
368
369 switch (hoa_order) {
370 case 1:
371 channels = 4;
372 channel_config = CHANNEL_CONFIG_HOA_ORDER1;
373 break;
374 case 2:
375 channels = 9;
376 channel_config = CHANNEL_CONFIG_HOA_ORDER2;
377 break;
378 case 3:
379 channels = 16;
380 channel_config = CHANNEL_CONFIG_HOA_ORDER3;
381 break;
382 default:
383 break;
384 }
385 } else {
386 return AVERROR_INVALIDDATA;
387 }
388
389 // 2 bits for bit depth
390 uint8_t resolution_index = get_bits(&gb, 2);
391 switch (resolution_index) {
392 case 0:
393 bitdepth = AV_SAMPLE_FMT_U8;
394 resolution = 8;
395 break;
396 case 1:
397 bitdepth = AV_SAMPLE_FMT_S16;
398 resolution = 16;
399 break;
400 case 2:
401 resolution = 24;
402 break;
403 default:
404 return AVERROR_INVALIDDATA;
405 }
406
407 if (coding_profile != 1) {
408 // 4 bits for bitrate index
409 brt_idx = get_bits(&gb, 4);
410 total_bitrate = codecBitrateConfigTable[channel_config].bitrateTable[brt_idx];
411 }
412
413 // 8 bits for CRC second part
414 skip_bits(&gb, 8);
415
416 /* AVS3P6 M6954-v3 */
417 hdf->codec_id = codec_id;
418 hdf->sampling_rate_index = samping_rate_index;
419 hdf->sampling_rate = avs3_samplingrate_table[samping_rate_index];
420 hdf->bitdepth = bitdepth;
421
422 hdf->nn_type = nn_type;
423 hdf->content_type = content_type;
424
425 if (hdf->content_type == 0) {
426 hdf->channel_num_index = num_chan_index;
427 hdf->channels = channels;
428 hdf->objects = 0;
429 hdf->total_channels = channels;
430 hdf->channel_layout = channel_layout;
431 } else if (hdf->content_type == 1) {
432 hdf->objects = objects;
433 hdf->channels = 0;
434 hdf->total_channels = objects;
435 } else if (hdf->content_type == 2) {
436 hdf->channel_num_index = num_chan_index;
437 hdf->channels = channels;
438 hdf->objects = objects;
439 hdf->total_channels = channels + objects;
440 hdf->channel_layout = channel_layout;
441 } else if (hdf->content_type == 3) {
442 hdf->hoa_order = hoa_order;
443 hdf->channels = channels;
444 hdf->total_channels = channels;
445 } else {
446 return AVERROR_INVALIDDATA;
447 }
448
449 hdf->total_bitrate = total_bitrate;
450 hdf->resolution = resolution;
451 hdf->resolution_index = resolution_index;
452
453 return 0;
454 }
455
raw_av3a_parse(AVCodecParserContext * s,AVCodecContext * avctx,const uint8_t ** poutbuf,int32_t * poutbuf_size,const uint8_t * buf,int32_t buf_size)456 static int32_t raw_av3a_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf,
457 int32_t *poutbuf_size, const uint8_t *buf, int32_t buf_size)
458 {
459 uint8_t header[MAX_NBYTES_FRAME_HEADER];
460 AVS3AHeaderInfo hdf;
461
462 if (buf_size < MAX_NBYTES_FRAME_HEADER) {
463 return buf_size;
464 }
465
466 memcpy(header, buf, MAX_NBYTES_FRAME_HEADER);
467
468 // read frame header
469 int32_t ret = read_av3a_frame_header(&hdf, header, MAX_NBYTES_FRAME_HEADER);
470 if (ret != 0) {
471 return ret;
472 }
473
474 avctx->sample_rate = hdf.sampling_rate;
475 avctx->bit_rate = hdf.total_bitrate;
476 avctx->channels = hdf.total_channels;
477 avctx->channel_layout = hdf.channel_layout;
478 avctx->frame_size = AVS3_AUDIO_FRAME_SIZE;
479 s->format = hdf.bitdepth;
480
481 /* return the full packet */
482 *poutbuf = buf;
483 *poutbuf_size = buf_size;
484
485 return buf_size;
486 }
487
488 #ifdef CONFIG_AV3A_PARSER
489 const AVCodecParser ff_av3a_parser = {
490 .codec_ids = { AV_CODEC_ID_AVS3DA },
491 .priv_data_size = sizeof(AVS3AParseContext),
492 .parser_parse = raw_av3a_parse,
493 };
494 #endif