编程规范修改

Signed-off-by: liushuai <cqliushuai@outlook.com>
This commit is contained in:
liushuai 2024-11-04 11:08:00 +08:00
parent ac916e4558
commit 1ff18c834d
5 changed files with 286 additions and 232 deletions

View File

@ -1,5 +1,5 @@
/*
* AV3A Format Common Header
* AV3A Common Header File
*
* This file is part of FFmpeg.
*
@ -22,15 +22,17 @@
#define AVCODEC_AV3A_H
#include "libavutil/samplefmt.h"
#include "libavutil/channel_layout.h"
/* AATF header */
#define AV3A_MAX_NBYTES_HEADER 9
#define AV3A_AUDIO_SYNC_WORD 0xFFF
#define AV3A_AUDIO_FRAME_SIZE 1024
#define AV3A_CHANNEL_LAYOUT_SIZE 15
#define AV3A_SIZE_BITRATE_TABLE 16
#define AV3A_SIZE_FS_TABLE 9
#define AV3A_SIZE_RESOLUTION_TABLE 3
#define AV3A_BITRATE_TABLE_SIZE 16
#define AV3A_FS_TABLE_SIZE 9
#define AV3A_RESOLUTION_TABLE_SIZE 3
#define AV3A_DCA3_BOX_MIN_SIZE 5
/* Channel Layout */
#define AV3A_CH_LAYOUT_MONO (AV_CH_LAYOUT_MONO)
@ -44,6 +46,21 @@
#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)
#define AV3A_CH_AUDIO_OBJECT (AV_CHAN_UNKNOWN)
/* AV3A Codec ID */
typedef enum {
AV3A_LOSSLESS_CODEC_ID = 1,
AV3A_LOSSY_CODEC_ID = 2
} Av3aCodecId;
/* Content Type */
typedef enum {
AV3A_CHANNEL_BASED_TYPE = 0,
AV3A_OBJECT_BASED_TYPE = 1,
AV3A_CHANNEL_OBJECT_TYPE = 2,
AV3A_AMBISONIC_TYPE = 3
} Av3aContentType;
/* AV3A Channel Configuration */
typedef enum {
CHANNEL_CONFIG_MONO = 0, /* Mono = 0 */
CHANNEL_CONFIG_STEREO = 1, /* Stereo = 1 */
@ -60,7 +77,13 @@ typedef enum {
CHANNEL_CONFIG_HOA_ORDER2 = 12, /* HOA2 = 12 */
CHANNEL_CONFIG_HOA_ORDER3 = 13, /* HOA3 = 13 */
CHANNEL_CONFIG_UNKNOWN = 14 /* UNKNOWN = 14 */
} AV3AChannelConfig;
} Av3aChannelConfig;
typedef enum {
AV3A_AMBISONIC_FIRST_ORDER = 1,
AV3A_AMBISONIC_SECOND_ORDER = 2,
AV3A_AMBISONIC_THIRD_ORDER = 3
} Av3aAmbisonicOrder;
typedef struct {
int16_t sync_word; /* sync word */
@ -71,7 +94,7 @@ typedef struct {
int16_t sampling_frequency_index; /* samping rate index */
int16_t channel_number_index; /* channel number index */
int16_t bitrate_index; /* bitrate index */
int16_t soundBedType; /* soundbed type */
int16_t soundbed_type; /* soundbed type */
int16_t object_channel_number; /* object channel number */
int16_t bitrate_index_per_channel; /* bitrate per object */
int16_t order; /* ambisonics order */
@ -85,69 +108,71 @@ typedef struct {
int16_t nb_channels; /* number of channels (channel configuration) */
int16_t nb_objects; /* number of objects (object_channel_number + 1) */
int16_t total_channels; /* total channels */
int16_t hoa_order; /* ambisonics order (order + 1) */
int16_t hoa_order; /* ambisonic order (order + 1) */
int32_t ch_layout_mask; /* channel layout mask */
} AATFHeaderInfo;
// bitrate table for mono
static const int64_t ff_av3a_mono_bitrate_table[AV3A_SIZE_BITRATE_TABLE] = {
/* bitrate table for mono */
static const int64_t ff_av3a_mono_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
16000, 32000, 44000, 56000, 64000, 72000, 80000, 96000, 128000, 144000,
164000, 192000, 0, 0, 0, 0};
// bitrate table for stereo
static const int64_t ff_av3a_stereo_bitrate_table[AV3A_SIZE_BITRATE_TABLE] = {
/* bitrate table for stereo */
static const int64_t ff_av3a_stereo_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
24000, 32000, 48000, 64000, 80000, 96000, 128000, 144000, 192000, 256000,
320000, 0, 0, 0, 0, 0};
// bitrate table for MC 5.1
static const int64_t ff_av3a_mc5p1_bitrate_table[AV3A_SIZE_BITRATE_TABLE] = {
/* bitrate table for MC 5.1 */
static const int64_t ff_av3a_mc5p1_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
192000, 256000, 320000, 384000, 448000, 512000, 640000, 720000, 144000, 96000,
128000, 160000, 0, 0, 0, 0};
// bitrate table for MC 7.1
static const int64_t ff_av3a_mc7p1_bitrate_table[AV3A_SIZE_BITRATE_TABLE] = {
/* bitrate table for MC 7.1 */
static const int64_t ff_av3a_mc7p1_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
192000, 480000, 256000, 384000, 576000, 640000, 128000, 160000, 0, 0,
0, 0, 0, 0, 0, 0};
// bitrate table for MC 4.0
static const int64_t ff_av3a_mc4p0_bitrate_table[AV3A_SIZE_BITRATE_TABLE] = {
/* bitrate table for MC 4.0 */
static const int64_t ff_av3a_mc4p0_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
48000, 96000, 128000, 192000, 256000, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0};
// bitrate table for MC 5.1.2
static const int64_t ff_av3a_mc5p1p2_bitrate_table[AV3A_SIZE_BITRATE_TABLE] = {
/* bitrate table for MC 5.1.2 */
static const int64_t ff_av3a_mc5p1p2_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
152000, 320000, 480000, 576000, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0};
// bitrate table for MC 5.1.4
static const int64_t ff_av3a_mc5p1p4_bitrate_table[AV3A_SIZE_BITRATE_TABLE] = {
/* bitrate table for MC 5.1.4 */
static const int64_t ff_av3a_mc5p1p4_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
176000, 384000, 576000, 704000, 256000, 448000, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0};
// bitrate table for MC 7.1.2
static const int64_t ff_av3a_mc7p1p2_bitrate_table[AV3A_SIZE_BITRATE_TABLE] = {
/* bitrate table for MC 7.1.2 */
static const int64_t ff_av3a_mc7p1p2_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
216000, 480000, 576000, 384000, 768000, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0};
// bitrate table for MC 7.1.4
static const int64_t ff_av3a_mc7p1p4_bitrate_table[AV3A_SIZE_BITRATE_TABLE] = {
/* bitrate table for MC 7.1.4 */
static const int64_t ff_av3a_mc7p1p4_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
240000, 608000, 384000, 512000, 832000, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0};
static const int64_t ff_av3a_foa_bitrate_table[AV3A_SIZE_BITRATE_TABLE] = {
/* bitrate table for FOA */
static const int64_t ff_av3a_foa_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
48000, 96000, 128000, 192000, 256000, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0};
static const int64_t ff_av3a_hoa2_bitrate_table[AV3A_SIZE_BITRATE_TABLE] = {
/* bitrate table for HOA2 */
static const int64_t ff_av3a_hoa2_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
192000, 256000, 320000, 384000, 480000, 512000, 640000, 0, 0, 0,
0, 0, 0, 0, 0, 0};
// bitrate table for HOA order 3
static const int64_t ff_av3a_hoa3_bitrate_table[AV3A_SIZE_BITRATE_TABLE] = {
/* bitrate table for HOA3 */
static const int64_t ff_av3a_hoa3_bitrate_table[AV3A_BITRATE_TABLE_SIZE] = {
256000, 320000, 384000, 512000, 640000, 896000, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0};
static const int32_t ff_av3a_sampling_rate_table[AV3A_SIZE_FS_TABLE] = {
static const int32_t ff_av3a_sampling_rate_table[AV3A_FS_TABLE_SIZE] = {
192000, 96000, 48000, 44100, 32000, 24000, 22050, 16000, 8000
};
@ -156,14 +181,14 @@ typedef struct {
enum AVSampleFormat sample_format;
} Av3aSampleFormatMap;
static const Av3aSampleFormatMap ff_av3a_sample_format_map_table[AV3A_SIZE_RESOLUTION_TABLE] = {
static const Av3aSampleFormatMap ff_av3a_sample_format_map_table[AV3A_RESOLUTION_TABLE_SIZE] = {
{8, AV_SAMPLE_FMT_U8 }, /* 0: 8 bits */
{16, AV_SAMPLE_FMT_S16}, /* 1: 16 bits */
{24, AV_SAMPLE_FMT_S32}, /* 2: 24 bits */
};
typedef struct {
AV3AChannelConfig channel_number_index;
Av3aChannelConfig channel_number_index;
int16_t channels;
const enum AVChannel* channel_layout;
uint64_t mask;
@ -246,7 +271,7 @@ static const Av3aChannelConfigMap ff_av3a_channels_map_table[AV3A_CHANNEL_LAYOUT
};
typedef struct {
AV3AChannelConfig channel_number_index;
Av3aChannelConfig channel_number_index;
const int64_t *bitrate_table;
} Av3aBitrateMap;

View File

@ -20,8 +20,6 @@
#include <stdio.h>
#include <stdint.h>
#include "libavutil/channel_layout.h"
#include "libavutil/samplefmt.h"
#include "libavutil/intreadwrite.h"
#include "parser.h"
#include "get_bits.h"
@ -40,12 +38,13 @@ typedef struct{
int16_t nb_channels;
int16_t nb_objects;
int16_t total_channels;
} AV3AParseContext;
} Av3aParseContext;
static int ff_read_av3a_header_parse(GetBitContext *gb, AATFHeaderInfo *hdf)
{
int64_t soundbed_bitrate, object_bitrate;
int64_t soundbed_bitrate = 0L;
int64_t object_bitrate = 0L;
hdf->nb_channels = 0;
hdf->nb_objects = 0;
@ -56,67 +55,72 @@ static int ff_read_av3a_header_parse(GetBitContext *gb, AATFHeaderInfo* hdf)
}
hdf->audio_codec_id = get_bits(gb, 4);
if (hdf->audio_codec_id != 2) {
if (hdf->audio_codec_id != AV3A_LOSSY_CODEC_ID) {
return AVERROR_INVALIDDATA;
}
hdf->anc_data = get_bits(gb, 1);
if (hdf->anc_data != 0) {
return AVERROR_INVALIDDATA;
}
skip_bits(gb, 1); /* skip anc_data 1 bit */
hdf->nn_type = get_bits(gb, 3);
hdf->coding_profile = get_bits(gb, 3);
hdf->sampling_frequency_index = get_bits(gb, 4);
if (hdf->sampling_frequency_index >= AV3A_SIZE_FS_TABLE) {
if ((hdf->sampling_frequency_index >= AV3A_FS_TABLE_SIZE) || (hdf->sampling_frequency_index < 0)) {
return AVERROR_INVALIDDATA;
}
hdf->sampling_rate = ff_av3a_sampling_rate_table[hdf->sampling_frequency_index];
skip_bits(gb, 8);
skip_bits(gb, 8); /* skip CRC 8 bits */
if (hdf->coding_profile == 0) {
hdf->content_type = 0; /* channel-based */
hdf->content_type = AV3A_CHANNEL_BASED_TYPE;
hdf->channel_number_index = get_bits(gb, 7);
if ((hdf->channel_number_index >= CHANNEL_CONFIG_UNKNOWN) ||
(hdf->channel_number_index == CHANNEL_CONFIG_MC_10_2) ||
(hdf->channel_number_index == CHANNEL_CONFIG_MC_22_2)) {
(hdf->channel_number_index == CHANNEL_CONFIG_MC_22_2) ||
(hdf->channel_number_index < 0)) {
return AVERROR_INVALIDDATA;
}
hdf->nb_channels = ff_av3a_channels_map_table[hdf->channel_number_index].channels;
} else if (hdf->coding_profile == 1) {
hdf->soundBedType = get_bits(gb, 2);
if (hdf->soundBedType == 0) {
hdf->content_type = 1; /* objects-based */
hdf->soundbed_type = get_bits(gb, 2);
if (hdf->soundbed_type == 0) {
hdf->content_type = AV3A_OBJECT_BASED_TYPE;
hdf->object_channel_number = get_bits(gb, 7);
if (hdf->object_channel_number < 0) {
return AVERROR_INVALIDDATA;
}
hdf->nb_objects = hdf->object_channel_number + 1;
hdf->bitrate_index_per_channel = get_bits(gb, 4);
if (hdf->bitrate_index_per_channel < 0 ) {
if ((hdf->bitrate_index_per_channel >= AV3A_BITRATE_TABLE_SIZE) || (hdf->bitrate_index_per_channel < 0)) {
return AVERROR_INVALIDDATA;
}
object_bitrate = ff_av3a_bitrate_map_table[CHANNEL_CONFIG_MONO].bitrate_table[hdf->bitrate_index_per_channel];
hdf->total_bitrate = object_bitrate * hdf->nb_objects;
} else if (hdf->soundBedType == 1) {
hdf->content_type = 2; /* channel-based + objects */
} else if (hdf->soundbed_type == 1) {
hdf->content_type = AV3A_CHANNEL_OBJECT_TYPE;
hdf->channel_number_index = get_bits(gb, 7);
if ((hdf->channel_number_index >= CHANNEL_CONFIG_UNKNOWN) ||
(hdf->channel_number_index == CHANNEL_CONFIG_MC_10_2) ||
(hdf->channel_number_index == CHANNEL_CONFIG_MC_22_2)) {
(hdf->channel_number_index == CHANNEL_CONFIG_MC_22_2) ||
(hdf->channel_number_index < 0)) {
return AVERROR_INVALIDDATA;
}
hdf->bitrate_index = get_bits(gb, 4);
if (hdf->bitrate_index < 0 ) {
if ((hdf->bitrate_index >= AV3A_BITRATE_TABLE_SIZE) || (hdf->bitrate_index < 0)) {
return AVERROR_INVALIDDATA;
}
hdf->nb_channels = ff_av3a_channels_map_table[hdf->channel_number_index].channels;
soundbed_bitrate = ff_av3a_bitrate_map_table[hdf->channel_number_index].bitrate_table[hdf->bitrate_index];
hdf->object_channel_number = get_bits(gb, 7);
if (hdf->object_channel_number < 0) {
return AVERROR_INVALIDDATA;
}
hdf->bitrate_index_per_channel = get_bits(gb, 4);
if (hdf->bitrate_index_per_channel < 0 ) {
if ((hdf->bitrate_index_per_channel >= AV3A_BITRATE_TABLE_SIZE) || (hdf->bitrate_index_per_channel < 0)) {
return AVERROR_INVALIDDATA;
}
hdf->nb_objects = hdf->object_channel_number + 1;
@ -126,28 +130,31 @@ static int ff_read_av3a_header_parse(GetBitContext *gb, AATFHeaderInfo* hdf)
return AVERROR_INVALIDDATA;
}
} else if (hdf->coding_profile == 2) {
hdf->content_type = 3; /* ambisonics */
hdf->content_type = AV3A_AMBISONIC_TYPE;
hdf->order = get_bits(gb, 4);
hdf->hoa_order += 1;
if (hdf->hoa_order == 1) {
switch (hdf->hoa_order) {
case AV3A_AMBISONIC_FIRST_ORDER:
hdf->channel_number_index = CHANNEL_CONFIG_HOA_ORDER1;
} else if (hdf->hoa_order == 2) {
break;
case AV3A_AMBISONIC_SECOND_ORDER:
hdf->channel_number_index = CHANNEL_CONFIG_HOA_ORDER2;
} else if (hdf->hoa_order == 3) {
break;
case AV3A_AMBISONIC_THIRD_ORDER:
hdf->channel_number_index = CHANNEL_CONFIG_HOA_ORDER3;
break;
default:
return AVERROR_INVALIDDATA;
}
hdf->nb_channels = ff_av3a_channels_map_table[hdf->channel_number_index].channels;
} else {
return AVERROR_INVALIDDATA;
}
hdf->nb_channels = (hdf->hoa_order + 1) * (hdf->hoa_order + 1);
} else {
return AVERROR_INVALIDDATA;
}
hdf->total_channels = hdf->nb_channels + hdf->nb_objects;
hdf->resolution_index = get_bits(gb, 2);
if(hdf->resolution_index >= AV3A_SIZE_RESOLUTION_TABLE) {
if((hdf->resolution_index >= AV3A_RESOLUTION_TABLE_SIZE) || (hdf->resolution_index < 0)) {
return AVERROR_INVALIDDATA;
}
hdf->resolution = ff_av3a_sample_format_map_table[hdf->resolution_index].resolution;
@ -155,13 +162,13 @@ static int ff_read_av3a_header_parse(GetBitContext *gb, AATFHeaderInfo* hdf)
if (hdf->coding_profile != 1) {
hdf->bitrate_index = get_bits(gb, 4);
if (hdf->bitrate_index < 0) {
if ((hdf->bitrate_index >= AV3A_BITRATE_TABLE_SIZE) || (hdf->bitrate_index < 0)) {
return AVERROR_INVALIDDATA;
}
hdf->total_bitrate = ff_av3a_bitrate_map_table[hdf->channel_number_index].bitrate_table[hdf->bitrate_index];
}
skip_bits(gb, 8);
skip_bits(gb, 8); /* skip CRC 8 bits */
return 0;
}
@ -173,7 +180,6 @@ static int raw_av3a_parse(AVCodecParserContext *s, AVCodecContext *avctx, const
uint8_t header[AV3A_MAX_NBYTES_HEADER];
AATFHeaderInfo hdf;
GetBitContext gb;
AV3AParseContext *s1 = s->priv_data;
if (buf_size < AV3A_MAX_NBYTES_HEADER) {
return buf_size;
@ -185,18 +191,6 @@ static int raw_av3a_parse(AVCodecParserContext *s, AVCodecContext *avctx, const
return ret;
}
s1->audio_codec_id = hdf.audio_codec_id;
s1->nn_type = hdf.nn_type;
s1->frame_size = AV3A_AUDIO_FRAME_SIZE;
s1->resolution = hdf.resolution;
s1->sample_rate = hdf.sampling_rate;
s1->bit_rate = hdf.total_bitrate;
s1->content_type = hdf.content_type;
s1->nb_channels = hdf.nb_channels;
s1->nb_objects = hdf.nb_objects;
s1->total_channels = hdf.total_channels;
s1->channel_number_index = hdf.channel_number_index;
avctx->codec_id = AV_CODEC_ID_AVS3DA;
avctx->frame_size = AV3A_AUDIO_FRAME_SIZE;
avctx->bits_per_raw_sample = hdf.resolution;
@ -214,6 +208,6 @@ static int raw_av3a_parse(AVCodecParserContext *s, AVCodecContext *avctx, const
const AVCodecParser ff_av3a_parser = {
.codec_ids = { AV_CODEC_ID_AVS3DA },
.priv_data_size = sizeof(AV3AParseContext),
.priv_data_size = sizeof(Av3aParseContext),
.parser_parse = raw_av3a_parse,
};

View File

@ -1,5 +1,5 @@
/*
* Audio Vivid Demuxer
* AV3A Demuxer
*
* This file is part of FFmpeg.
*
@ -58,26 +58,28 @@ static int av3a_read_aatf_frame_header(AATFHeaderInfo *hdf, const uint8_t *buf)
/* codec id */
hdf->audio_codec_id = get_bits(&gb, 4);
if (hdf->audio_codec_id != 2) {
if (hdf->audio_codec_id != AV3A_LOSSY_CODEC_ID) {
return AVERROR_INVALIDDATA;
}
/* anc data */
hdf->anc_data = get_bits(&gb, 1);
if (hdf->audio_codec_id == 2 && hdf->anc_data) {
if (hdf->anc_data) {
return AVERROR_INVALIDDATA;
}
/* neural network type */
hdf->nn_type = get_bits(&gb, 3);
if (hdf->audio_codec_id == 2 && (hdf->nn_type != 0 && hdf->nn_type != 1)) {
if ((hdf->nn_type < 0) || (hdf->nn_type > 2)) {
return AVERROR_INVALIDDATA;
}
/* coding profile */
hdf->coding_profile = get_bits(&gb, 3);
/* sampling rate */
hdf->sampling_frequency_index = get_bits(&gb, 4);
if (hdf->sampling_frequency_index >= AV3A_SIZE_FS_TABLE) {
if ((hdf->sampling_frequency_index >= AV3A_FS_TABLE_SIZE) || (hdf->sampling_frequency_index < 0)) {
return AVERROR_INVALIDDATA;
}
hdf->sampling_rate = ff_av3a_sampling_rate_table[hdf->sampling_frequency_index];
@ -85,43 +87,51 @@ static int av3a_read_aatf_frame_header(AATFHeaderInfo *hdf, const uint8_t *buf)
skip_bits(&gb, 8);
if (hdf->coding_profile == 0) {
hdf->content_type = 0;
hdf->content_type = AV3A_CHANNEL_BASED_TYPE;
hdf->channel_number_index = get_bits(&gb, 7);
if ((hdf->channel_number_index >= CHANNEL_CONFIG_UNKNOWN) ||
(hdf->channel_number_index == CHANNEL_CONFIG_MC_10_2) ||
(hdf->channel_number_index == CHANNEL_CONFIG_MC_22_2)) {
(hdf->channel_number_index == CHANNEL_CONFIG_MC_22_2) ||
(hdf->channel_number_index < 0)) {
return AVERROR_INVALIDDATA;
}
hdf->nb_channels = ff_av3a_channels_map_table[hdf->channel_number_index].channels;
} else if (hdf->coding_profile == 1) {
hdf->soundBedType = get_bits(&gb, 2);
if (hdf->soundBedType == 0) {
hdf->content_type = 1;
hdf->soundbed_type = get_bits(&gb, 2);
if (hdf->soundbed_type == 0) {
hdf->content_type = AV3A_OBJECT_BASED_TYPE;
hdf->object_channel_number = get_bits(&gb, 7);
if (hdf->object_channel_number < 0) {
return AVERROR_INVALIDDATA;
}
hdf->bitrate_index_per_channel = get_bits(&gb, 4);
if (hdf->bitrate_index_per_channel < 0){
if ((hdf->bitrate_index_per_channel >= AV3A_BITRATE_TABLE_SIZE) || (hdf->bitrate_index_per_channel < 0)){
return AVERROR_INVALIDDATA;
}
hdf->nb_objects = hdf->object_channel_number + 1;
hdf->total_bitrate = ff_av3a_bitrate_map_table[CHANNEL_CONFIG_MONO].bitrate_table[hdf->bitrate_index_per_channel] * hdf->nb_objects;
} else if (hdf->soundBedType == 1) {
hdf->content_type = 2;
} else if (hdf->soundbed_type == 1) {
hdf->content_type = AV3A_CHANNEL_OBJECT_TYPE;
hdf->channel_number_index = get_bits(&gb, 7);
if ((hdf->channel_number_index >= CHANNEL_CONFIG_UNKNOWN) ||
(hdf->channel_number_index == CHANNEL_CONFIG_MC_10_2) ||
(hdf->channel_number_index == CHANNEL_CONFIG_MC_22_2)) {
(hdf->channel_number_index == CHANNEL_CONFIG_MC_22_2) ||
(hdf->channel_number_index < 0)) {
return AVERROR_INVALIDDATA;
}
hdf->nb_channels = ff_av3a_channels_map_table[hdf->channel_number_index].channels;
hdf->bitrate_index = get_bits(&gb, 4);
if (hdf->bitrate_index < 0) {
if ((hdf->bitrate_index >= AV3A_BITRATE_TABLE_SIZE) || (hdf->bitrate_index < 0)) {
return AVERROR_INVALIDDATA;
}
hdf->object_channel_number = get_bits(&gb, 7);
if (hdf->object_channel_number < 0) {
return AVERROR_INVALIDDATA;
}
hdf->nb_objects = hdf->object_channel_number + 1;
hdf->bitrate_index_per_channel = get_bits(&gb, 4);
if (hdf->bitrate_index_per_channel < 0) {
if ((hdf->bitrate_index_per_channel >= AV3A_BITRATE_TABLE_SIZE) || (hdf->bitrate_index_per_channel < 0)) {
return AVERROR_INVALIDDATA;
}
@ -131,38 +141,41 @@ static int av3a_read_aatf_frame_header(AATFHeaderInfo *hdf, const uint8_t *buf)
return AVERROR_INVALIDDATA;
}
} else if (hdf->coding_profile == 2) {
/* FOA/HOA */
hdf->content_type = 3;
hdf->content_type = AV3A_AMBISONIC_TYPE;
hdf->order = get_bits(&gb, 4);
hdf->hoa_order = hdf->order + 1;
hdf->nb_channels = (hdf->hoa_order + 1) * (hdf->hoa_order + 1);
if (hdf->hoa_order == 1) {
switch (hdf->hoa_order) {
case AV3A_AMBISONIC_FIRST_ORDER:
hdf->channel_number_index = CHANNEL_CONFIG_HOA_ORDER1;
} else if (hdf->hoa_order == 2) {
break;
case AV3A_AMBISONIC_SECOND_ORDER:
hdf->channel_number_index = CHANNEL_CONFIG_HOA_ORDER2;
} else if (hdf->hoa_order == 3) {
break;
case AV3A_AMBISONIC_THIRD_ORDER:
hdf->channel_number_index = CHANNEL_CONFIG_HOA_ORDER3;
} else {
break;
default:
return AVERROR_INVALIDDATA;
}
hdf->nb_channels = ff_av3a_channels_map_table[hdf->channel_number_index].channels;
} else {
return AVERROR_INVALIDDATA;
}
hdf->total_channels = hdf->nb_channels + hdf->nb_objects;
/* resolution */
hdf->resolution_index = get_bits(&gb, 2);
if(hdf->resolution_index >= AV3A_SIZE_RESOLUTION_TABLE) {
if((hdf->resolution_index >= AV3A_RESOLUTION_TABLE_SIZE) || (hdf->resolution_index < 0)) {
return AVERROR_INVALIDDATA;
}
hdf->resolution = ff_av3a_sample_format_map_table[hdf->resolution_index].resolution;
hdf->sample_format = ff_av3a_sample_format_map_table[hdf->resolution_index].sample_format;
if (hdf->coding_profile != 1) {
hdf->bitrate_index = get_bits(&gb, 4);
if (hdf->bitrate_index < 0){
if ((hdf->bitrate_index >= AV3A_BITRATE_TABLE_SIZE) || (hdf->bitrate_index < 0)){
return AVERROR_INVALIDDATA;
}
hdf->total_bitrate = ff_av3a_bitrate_map_table[hdf->channel_number_index].bitrate_table[hdf->bitrate_index];
@ -175,24 +188,23 @@ static int av3a_read_aatf_frame_header(AATFHeaderInfo *hdf, const uint8_t *buf)
static int av3a_get_packet_size(AVFormatContext *s)
{
int ret = 0;
int read_bytes = 0;
uint16_t sync_word = 0;
int32_t payload_bytes = 0;
int32_t payloud_bits = 0;
uint8_t header[AV3A_MAX_NBYTES_HEADER];
GetBitContext gb;
int32_t sampling_rate;
int16_t coding_profile, sampling_frequency_index, bitrate_index, channel_number_index, object_bitrate_index;
int16_t coding_profile, sampling_frequency_index, channel_number_index;
int16_t bitrate_index, bitrate_index_per_channel;
int16_t objects, hoa_order;
int64_t total_bitrate;
int32_t payload_bytes;
int32_t payloud_bits;
payload_bytes = 0;
payloud_bits = 0;
read_bytes = avio_read(s->pb, header, AV3A_MAX_NBYTES_HEADER);
if (read_bytes != AV3A_MAX_NBYTES_HEADER)
if (read_bytes != AV3A_MAX_NBYTES_HEADER) {
return (read_bytes < 0) ? read_bytes : AVERROR_EOF;
}
init_get_bits8(&gb, header, AV3A_MAX_NBYTES_HEADER);
sync_word = get_bits(&gb, 12);
@ -204,7 +216,7 @@ static int av3a_get_packet_size(AVFormatContext *s)
coding_profile = get_bits(&gb, 3);
sampling_frequency_index = get_bits(&gb, 4);
if (sampling_frequency_index >= AV3A_SIZE_FS_TABLE) {
if ((sampling_frequency_index >= AV3A_FS_TABLE_SIZE) || (sampling_frequency_index < 0)) {
return AVERROR_INVALIDDATA;
}
sampling_rate = ff_av3a_sampling_rate_table[sampling_frequency_index];
@ -215,51 +227,70 @@ static int av3a_get_packet_size(AVFormatContext *s)
channel_number_index = get_bits(&gb, 7);
if ((channel_number_index >= CHANNEL_CONFIG_UNKNOWN) ||
(channel_number_index == CHANNEL_CONFIG_MC_10_2) ||
(channel_number_index == CHANNEL_CONFIG_MC_22_2)) {
(channel_number_index == CHANNEL_CONFIG_MC_22_2) ||
(channel_number_index < 0)) {
return AVERROR_INVALIDDATA;
}
} else if (coding_profile == 1) {
int64_t bitrate_index_per_channel, soundbed_bitrate;
int64_t soundbed_bitrate, objects_bitrate;
int16_t soundbed_type = get_bits(&gb, 2);
if (soundbed_type == 0) {
objects = get_bits(&gb, 7);
objects += 1;
object_bitrate_index = get_bits(&gb, 4);
if (object_bitrate_index < 0) {
if (objects < 0) {
return AVERROR_INVALIDDATA;
}
total_bitrate = ff_av3a_mono_bitrate_table[object_bitrate_index] * objects;
objects += 1;
bitrate_index_per_channel = get_bits(&gb, 4);
if ((bitrate_index_per_channel >= AV3A_BITRATE_TABLE_SIZE) || (bitrate_index_per_channel < 0)) {
return AVERROR_INVALIDDATA;
}
total_bitrate = ff_av3a_bitrate_map_table[CHANNEL_CONFIG_MONO].bitrate_table[bitrate_index_per_channel] * objects;
} else if (soundbed_type == 1) {
channel_number_index = get_bits(&gb, 7);
if ((channel_number_index >= CHANNEL_CONFIG_UNKNOWN) ||
(channel_number_index == CHANNEL_CONFIG_MC_10_2) ||
(channel_number_index == CHANNEL_CONFIG_MC_22_2)) {
return AVERROR_INVALIDDATA;
}
bitrate_index = get_bits(&gb, 4);
objects = get_bits(&gb, 7);
objects += 1;
object_bitrate_index = get_bits(&gb, 4);
if (bitrate_index < 0 || object_bitrate_index < 0){
(channel_number_index == CHANNEL_CONFIG_MC_22_2) ||
(channel_number_index < 0)) {
return AVERROR_INVALIDDATA;
}
bitrate_index = get_bits(&gb, 4);
if ((bitrate_index >= AV3A_BITRATE_TABLE_SIZE) || (bitrate_index < 0)) {
return AVERROR_INVALIDDATA;
}
soundbed_bitrate = ff_av3a_bitrate_map_table[channel_number_index].bitrate_table[bitrate_index];
bitrate_index_per_channel = ff_av3a_bitrate_map_table[CHANNEL_CONFIG_MONO].bitrate_table[object_bitrate_index];
total_bitrate = soundbed_bitrate + (bitrate_index_per_channel * objects);
objects = get_bits(&gb, 7);
if (objects < 0) {
return AVERROR_INVALIDDATA;
}
objects += 1;
bitrate_index_per_channel = get_bits(&gb, 4);
if ((bitrate_index_per_channel >= AV3A_BITRATE_TABLE_SIZE) || (bitrate_index_per_channel < 0)){
return AVERROR_INVALIDDATA;
}
objects_bitrate = ff_av3a_bitrate_map_table[CHANNEL_CONFIG_MONO].bitrate_table[bitrate_index_per_channel];
total_bitrate = soundbed_bitrate + (objects_bitrate * objects);
} else {
return AVERROR_INVALIDDATA;
}
} else if (coding_profile == 2) {
hoa_order = get_bits(&gb, 4);
hoa_order += 1;
if(hoa_order == 1) {
switch (hoa_order) {
case AV3A_AMBISONIC_FIRST_ORDER:
channel_number_index = CHANNEL_CONFIG_HOA_ORDER1;
} else if(hoa_order == 2) {
break;
case AV3A_AMBISONIC_SECOND_ORDER:
channel_number_index = CHANNEL_CONFIG_HOA_ORDER2;
} else if(hoa_order == 3) {
break;
case AV3A_AMBISONIC_THIRD_ORDER:
channel_number_index = CHANNEL_CONFIG_HOA_ORDER3;
} else {
break;
default:
return AVERROR_INVALIDDATA;
}
} else {
@ -269,7 +300,7 @@ static int av3a_get_packet_size(AVFormatContext *s)
skip_bits(&gb, 2);
if (coding_profile != 1) {
bitrate_index = get_bits(&gb, 4);
if (bitrate_index < 0) {
if ((bitrate_index >= AV3A_BITRATE_TABLE_SIZE) || (bitrate_index < 0)) {
return AVERROR_INVALIDDATA;
}
total_bitrate = ff_av3a_bitrate_map_table[channel_number_index].bitrate_table[bitrate_index];
@ -284,7 +315,9 @@ static int av3a_get_packet_size(AVFormatContext *s)
payload_bytes = (int)ceil((((float) (total_bitrate) / sampling_rate) * AV3A_AUDIO_FRAME_SIZE) / 8);
}
avio_seek(s->pb, -read_bytes, SEEK_CUR);
if ((ret = avio_seek(s->pb, -read_bytes, SEEK_CUR)) < 0) {
return ret;
}
return payload_bytes;
}
@ -357,7 +390,9 @@ static int av3a_read_header(AVFormatContext *s)
}
memcpy(stream->codecpar->extradata, &av3afmtctx, sizeof(Av3aFormatContext));
avio_seek(s->pb, -AV3A_MAX_NBYTES_HEADER, SEEK_CUR);
if ((ret = avio_seek(s->pb, -AV3A_MAX_NBYTES_HEADER, SEEK_CUR)) < 0) {
return ret;
}
return 0;
}
@ -386,9 +421,7 @@ static int av3a_read_packet(AVFormatContext *s, AVPacket *pkt) {
return packet_size;
}
ret = av_new_packet(pkt, packet_size);
if (ret < 0) {
av_log(s, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", packet_size);
if ((ret = av_new_packet(pkt, packet_size)) < 0) {
return ret;
}

View File

@ -7927,21 +7927,18 @@ static int mov_read_dca3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
int ret = 0;
int i = 0;
int nb_channels = 0;
int nb_objects = 0;
AVStream *st = NULL;
GetBitContext gb;
uint8_t buffer[7];
uint8_t audio_codec_id, sampling_frequency_index;
uint8_t nn_type, content_type, channel_number_index, number_objects;
uint8_t hoa_order, resolution_index, reserved;
int32_t bitrate_kbps;
int16_t nb_channels, nb_objects;
int audio_codec_id, sampling_frequency_index;
int nn_type, content_type, channel_number_index, number_objects;
int hoa_order, resolution_index, reserved;
int bitrate_kbps;
nb_channels = 0;
nb_objects = 0;
if (atom.size < 5) {
av_log(c->fc, AV_LOG_INFO, "Invalid dca3 box size %ld\n", atom.size);
return 0;
if (atom.size < AV3A_DCA3_BOX_MIN_SIZE) {
return AVERROR_INVALIDDATA;
}
init_get_bits8(&gb, buffer, sizeof(buffer));
@ -7951,20 +7948,19 @@ static int mov_read_dca3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
}
st = c->fc->streams[c->fc->nb_streams - 1];
ret = avio_read(pb, buffer, sizeof(buffer));
if (ret < 0){
if ((ret = avio_read(pb, buffer, sizeof(buffer))) < 0){
return ret;
}
audio_codec_id = get_bits(&gb, 4);
if (audio_codec_id != 2) {
av_log(c->fc, AV_LOG_INFO, "Invalid dca3 box size %ld\n", atom.size);
if (audio_codec_id != AV3A_LOSSY_CODEC_ID) {
return AVERROR_INVALIDDATA;
}
st->codecpar->frame_size = AV3A_AUDIO_FRAME_SIZE;
sampling_frequency_index = get_bits(&gb, 4);
if (sampling_frequency_index >= AV3A_SIZE_FS_TABLE) {
if ((sampling_frequency_index >= AV3A_FS_TABLE_SIZE) || (sampling_frequency_index < 0)) {
return AVERROR_INVALIDDATA;
}
st->codecpar->sample_rate = ff_av3a_sampling_rate_table[sampling_frequency_index];
@ -7973,36 +7969,40 @@ static int mov_read_dca3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
reserved = get_bits(&gb, 1);
content_type = get_bits(&gb, 4);
if (content_type == 0) {
if (content_type == AV3A_CHANNEL_BASED_TYPE) {
channel_number_index = get_bits(&gb, 7);
reserved = get_bits(&gb, 1);
if (channel_number_index >= CHANNEL_CONFIG_UNKNOWN) {
if ((channel_number_index >= CHANNEL_CONFIG_UNKNOWN) ||
(channel_number_index == CHANNEL_CONFIG_MC_10_2) ||
(channel_number_index == CHANNEL_CONFIG_MC_22_2) ||
(channel_number_index < 0)) {
return AVERROR_INVALIDDATA;
}
nb_channels = ff_av3a_channels_map_table[channel_number_index].channels;
} else if (content_type == 1) {
} else if (content_type == AV3A_OBJECT_BASED_TYPE) {
number_objects = get_bits(&gb, 7);
reserved = get_bits(&gb, 1);
nb_objects = number_objects;
} else if (content_type == 2) {
} else if (content_type == AV3A_CHANNEL_OBJECT_TYPE) {
channel_number_index = get_bits(&gb, 7);
reserved = get_bits(&gb, 1);
number_objects = get_bits(&gb, 7);
reserved = get_bits(&gb, 1);
if (channel_number_index >= CHANNEL_CONFIG_UNKNOWN) {
if ((channel_number_index >= CHANNEL_CONFIG_UNKNOWN) ||
(channel_number_index == CHANNEL_CONFIG_MC_10_2) ||
(channel_number_index == CHANNEL_CONFIG_MC_22_2) ||
(channel_number_index < 0)) {
return AVERROR_INVALIDDATA;
}
number_objects = get_bits(&gb, 7);
reserved = get_bits(&gb, 1);
nb_channels = ff_av3a_channels_map_table[channel_number_index].channels;
nb_objects = number_objects;
} else if (content_type == 3) {
} else if (content_type == AV3A_AMBISONIC_TYPE) {
hoa_order = get_bits(&gb , 4);
if (hoa_order > 3) {
if ((hoa_order < AV3A_AMBISONIC_FIRST_ORDER) || (hoa_order > AV3A_AMBISONIC_THIRD_ORDER)) {
return AVERROR_INVALIDDATA;
}
nb_channels = (hoa_order + 1) * (hoa_order + 1);
} else {
av_log(c->fc, AV_LOG_INFO, "Invalid content_type value %ld\n", content_type);
return AVERROR_INVALIDDATA;
}
@ -8010,20 +8010,23 @@ static int mov_read_dca3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
st->codecpar->bit_rate = bitrate_kbps * 1000;
resolution_index = get_bits(&gb, 2);
if (resolution_index >= AV3A_SIZE_RESOLUTION_TABLE) {
if ((resolution_index >= AV3A_RESOLUTION_TABLE_SIZE) || (resolution_index < 0)) {
return AVERROR_INVALIDDATA;
}
st->codecpar->format = ff_av3a_sample_format_map_table[resolution_index].sample_format;
st->codecpar->bits_per_raw_sample = ff_av3a_sample_format_map_table[resolution_index].resolution;
av_channel_layout_uninit(&st->codecpar->channel_layout);
if (content_type != 3) {
if (content_type != AV3A_AMBISONIC_TYPE) {
st->codecpar->ch_layout.order = AV_CHANNEL_ORDER_CUSTOM;
st->codecpar->ch_layout.nb_channels = (nb_channels + nb_objects);
st->codecpar->ch_layout.u.map = av_calloc(st->codecpar->ch_layout.nb_channels,
sizeof(*st->codecpar->ch_layout.u.map));
if (!st->codecpar->ch_layout.u.map) {
return AVERROR(ENOMEM);
}
if (content_type != 1) {
if (content_type != AV3A_OBJECT_BASED_TYPE) {
for(i = 0; i < nb_channels; i ++) {
st->codecpar->ch_layout.u.map[i].id = ff_av3a_channels_map_table[channel_number_index].channel_layout[i];
}

View File

@ -67,6 +67,7 @@
#include "ttmlenc.h"
#include "version.h"
#include "vpcc.h"
#include "libavcodec/av3a.h"
static const AVOption options[] = {
{ "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
@ -904,12 +905,11 @@ static int mov_write_dca3_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *tra
int64_t pos = avio_tell(pb);
uint8_t buffer[7];
PutBitContext pb_dca3;
int16_t audio_codec_id, sampling_frequency_index, nn_type, content_type;
int16_t channel_number_index, number_objects, hoa_order, resolution_index;
int32_t bitrate_kbps;
int audio_codec_id, sampling_frequency_index, nn_type, content_type;
int channel_number_index, number_objects, hoa_order, resolution_index;
int bitrate_kbps;
if (track->par->extradata_size < 10) {
av_log(s, AV_LOG_WARNING, "Can not write dca3 box\n");
return 0;
}
@ -928,8 +928,7 @@ static int mov_write_dca3_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *tra
resolution_index = AV_RB8(track->par->extradata + 7);
bitrate_kbps = AV_RB16(track->par->extradata + 8);
if (audio_codec_id != 2) {
av_log(s, AV_LOG_ERROR, "Not support audio codec id %d\n", audio_codec_id);
if (audio_codec_id != AV3A_LOSSY_CODEC_ID) {
return AVERROR_INVALIDDATA;
}
put_bits(&pb_dca3, 4, audio_codec_id);
@ -939,18 +938,18 @@ static int mov_write_dca3_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *tra
put_bits(&pb_dca3, 1, 0); /* reserved */
put_bits(&pb_dca3, 4, content_type);
if (content_type == 0) {
if (content_type == AV3A_CHANNEL_BASED_TYPE) {
put_bits(&pb_dca3, 7, channel_number_index);
put_bits(&pb_dca3, 1, 0); /* reserved */
} else if (content_type == 1){
} else if (content_type == AV3A_OBJECT_BASED_TYPE){
put_bits(&pb_dca3, 7, number_objects);
put_bits(&pb_dca3, 1, 0); /* reserved */
} else if (content_type == 2){
} else if (content_type == AV3A_CHANNEL_OBJECT_TYPE){
put_bits(&pb_dca3, 7, channel_number_index);
put_bits(&pb_dca3, 1, 0); /* reserved */
put_bits(&pb_dca3, 7, number_objects);
put_bits(&pb_dca3, 1, 0); /* reserved */
} else if (content_type == 3) {
} else if (content_type == AV3A_AMBISONIC_TYPE) {
put_bits(&pb_dca3, 4, hoa_order);
} else {
return AVERROR_INVALIDDATA;
@ -959,7 +958,7 @@ static int mov_write_dca3_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *tra
put_bits(&pb_dca3, 16, bitrate_kbps);
put_bits(&pb_dca3, 2, resolution_index);
if (content_type == 3) {
if (content_type == AV3A_AMBISONIC_TYPE) {
put_bits(&pb_dca3, 2, 0); /* reserved */
} else {
put_bits(&pb_dca3, 6, 0); /* reserved */