Go to the documentation of this file.
23 #include "config_components.h"
42 #define MAX_NCHANNELS (MAX_CHANNELS + 2)
44 #define MIN_HEADER_INTERVAL 8
45 #define MAX_HEADER_INTERVAL 128
47 #define MLP_MIN_LPC_ORDER 1
48 #define MLP_MAX_LPC_ORDER 8
49 #define MLP_MIN_LPC_SHIFT 0
50 #define MLP_MAX_LPC_SHIFT 15
82 #define PARAMS_DEFAULT (0xff)
83 #define PARAM_PRESENCE_FLAGS (1 << 8)
105 #define HUFF_OFFSET_MIN (-16384)
106 #define HUFF_OFFSET_MAX ( 16383)
109 #define NUM_CODEBOOKS 4
210 #define SYNC_MAJOR 0xf8726f
211 #define MAJOR_SYNC_INFO_SIGNATURE 0xB752
214 #define FLAGS_DVDA 0x4000
216 #define FLAGS_CONST 0x8000
218 #define SUBSTREAM_INFO_MAX_2_CHAN 0x01
219 #define SUBSTREAM_INFO_HIGH_RATE 0x02
220 #define SUBSTREAM_INFO_ALWAYS_SET 0x04
221 #define SUBSTREAM_INFO_2_SUBSTREAMS 0x08
265 for (
unsigned int mat = 0; mat <
mp->count; mat++) {
266 if (prev->
outch[mat] !=
mp->outch[mat])
269 if (prev->
fbits[mat] !=
mp->fbits[mat])
279 if (prev->
coeff[mat][ch] !=
mp->coeff[mat][ch])
321 for (
int ch = rh->
min_channel; ch <= rh->max_channel; ch++) {
354 dst->coeff_shift =
src->coeff_shift;
355 dst->coeff_bits =
src->coeff_bits;
358 for (
int order = 0; order <
dst->order; order++)
370 dst->outch[count] =
src->outch[count];
371 dst->fbits[count] =
src->fbits[count];
372 dst->noise_shift[count] =
src->noise_shift[count];
373 dst->lsb_bypass[count] =
src->lsb_bypass[count];
390 dp->
output_shift[ch] =
s->b[1].decoding_params.output_shift[ch];
437 uint8_t param_presence_flags = 0;
471 return ((peak_bitrate << 4) - 8) / sample_rate;
485 uint64_t channels_present;
493 ctx->coded_sample_rate[0] = 0x08 + 0;
498 ctx->coded_sample_rate[0] = 0x08 + 1;
504 ctx->coded_sample_rate[0] = 0x08 + 2;
509 ctx->coded_sample_rate[0] = 0x00 + 0;
514 ctx->coded_sample_rate[0] = 0x00 + 1;
520 ctx->coded_sample_rate[0] = 0x00 + 2;
525 "sample rates are 44100, 88200, 176400, 48000, "
529 ctx->coded_sample_rate[1] = -1 & 0xf;
540 ctx->wordlength = 16;
546 ctx->wordlength = 24;
551 "Only 16- and 24-bit samples are supported.\n");
554 ctx->coded_sample_fmt[1] = -1 & 0xf;
560 ctx->min_restart_interval =
ctx->cur_restart_interval =
ctx->max_restart_interval;
561 ctx->restart_intervals =
ctx->max_restart_interval /
ctx->min_restart_interval;
563 ctx->num_substreams = 1;
567 static const uint64_t layout_arrangement[] = {
579 !
"Impossible channel layout");
580 if (channels_present == layout_arrangement[
i])
583 ctx->channel_arrangement =
i;
590 switch (channels_present) {
592 ctx->ch2_presentation_mod= 3;
593 ctx->ch6_presentation_mod= 3;
594 ctx->ch8_presentation_mod= 3;
595 ctx->thd_substream_info = 0x14;
598 ctx->ch2_presentation_mod= 1;
599 ctx->ch6_presentation_mod= 1;
600 ctx->ch8_presentation_mod= 1;
601 ctx->thd_substream_info = 0x14;
610 ctx->ch2_presentation_mod= 0;
611 ctx->ch6_presentation_mod= 0;
612 ctx->ch8_presentation_mod= 0;
613 ctx->thd_substream_info = 0x3C;
616 av_assert1(!
"AVCodec.ch_layouts needs to be updated");
619 ctx->channel_occupancy = 0;
620 ctx->summary_info = 0;
621 ctx->channel_arrangement =
626 for (
int n = 0; n <
ctx->num_substreams; n++)
627 ctx->s[n].b[
index].seq_size = ((
index + 1) *
ctx->min_restart_interval) + 1;
648 rh = &
ctx->s[1].restart_header;
803 if (!
ctx->noise_type)
806 for (
unsigned int mat = 0; mat <
mp->count; mat++) {
811 for (
int ch = 0; ch <= max_channel; ch++) {
844 for (
int i = 0;
i < fp->
order;
i++) {
858 unsigned int subblock_index)
909 for (
int ch = rh->
min_channel; ch <= rh->max_channel; ch++) {
910 ChannelParams *cp = &
s->b[subblock_index].major_channel_params[ch];
966 for (
int ch = rh->
min_channel; ch <= rh->max_channel; ch++) {
967 ChannelParams *cp = &
s->b[subblock_index].major_channel_params[ch];
971 codebook_index [ch] = cp->
codebook - 1;
977 sign_huff_offset[ch] -= 7 << lsb_bits[ch];
981 sign_huff_offset[ch] -= 1 << sign_shift;
985 for (
unsigned int mat = 0; mat <
mp->count; mat++) {
986 if (
mp->lsb_bypass[mat]) {
987 const int8_t *bypassed_lsbs =
mp->bypassed_lsbs[mat];
993 for (
int ch = rh->
min_channel; ch <= rh->max_channel; ch++) {
996 sample -= sign_huff_offset[ch];
998 if (codebook_index[ch] >= 0) {
999 int vlc =
sample >> lsb_bits[ch];
1002 sample &= ((1 << lsb_bits[ch]) - 1);
1013 uint8_t *buf,
int buf_size,
1015 uint16_t *substream_data_len)
1017 int32_t *lossless_check_data = &
s->b[
ctx->frame_index].lossless_check_data;
1018 unsigned int cur_subblock_index =
s->major_cur_subblock_index;
1019 unsigned int num_subblocks =
s->major_filter_state_subblock;
1021 int substr_restart_frame = restart_frame;
1022 uint8_t
parity, checksum;
1026 s->cur_restart_header = rh;
1030 for (
unsigned int subblock = 0; subblock <= num_subblocks; subblock++) {
1031 unsigned int subblock_index = cur_subblock_index++;
1033 params_changed =
s->b[subblock_index].major_params_changed;
1035 if (substr_restart_frame || params_changed) {
1038 if (substr_restart_frame) {
1055 put_bits(&pb, 1, !substr_restart_frame);
1057 substr_restart_frame = 0;
1064 if (
ctx->last_frames == 0 &&
ctx->shorten_by) {
1067 put_bits(&pb, 16, (
ctx->shorten_by & 0x1FFF) | 0xE000);
1087 buf += substream_data_len[0];
1089 s->major_cur_subblock_index +=
s->major_filter_state_subblock + 1;
1090 s->major_filter_state_subblock = 0;
1097 uint8_t *substream_headers,
unsigned int length,
1101 uint16_t access_unit_header = 0;
1102 uint16_t substream_data_end = 0;
1103 uint16_t parity_nibble = 0;
1105 parity_nibble =
ctx->input_timing;
1106 parity_nibble ^= length;
1108 for (
unsigned int substr = 0; substr <
ctx->num_substreams; substr++) {
1109 uint16_t substr_hdr = 0;
1111 substream_data_end += substream_data_len[substr];
1113 substr_hdr |= (0 << 15);
1114 substr_hdr |= (!restart_frame << 14);
1115 substr_hdr |= (1 << 13);
1116 substr_hdr |= (0 << 12);
1117 substr_hdr |= (substream_data_end / 2) & 0x0FFF;
1119 AV_WB16(substream_headers, substr_hdr);
1121 parity_nibble ^= *substream_headers++;
1122 parity_nibble ^= *substream_headers++;
1125 parity_nibble ^= parity_nibble >> 8;
1126 parity_nibble ^= parity_nibble >> 4;
1127 parity_nibble &= 0xF;
1129 access_unit_header |= (parity_nibble ^ 0xF) << 12;
1130 access_unit_header |= length & 0xFFF;
1138 int buf_size,
int restart_frame)
1141 uint8_t *buf1, *buf0 = buf;
1148 if (restart_frame) {
1157 for (
unsigned int substr = 0; substr <
ctx->num_substreams; substr++) {
1162 for (
int substr = 0; substr <
ctx->num_substreams; substr++) {
1164 uint8_t *buf0 = buf;
1166 buf =
write_substr(
ctx,
s, buf, buf_size, restart_frame, &substream_data_len[substr]);
1167 buf_size -= buf - buf0;
1170 total_length = buf - buf0;
1174 return total_length;
1187 int nb_samples,
int is24)
1189 int32_t *lossless_check_data = &
s->b[
ctx->frame_index].lossless_check_data;
1191 int32_t temp_lossless_check_data = 0;
1194 for (
int i = 0;
i < nb_samples;
i++) {
1197 const int16_t *samples_16 = (
const int16_t *)
samples[ch];
1198 int32_t *sample_buffer =
s->b[
ctx->frame_index].inout_buffer[ch];
1201 sample = is24 ? samples_32[
i] >> 8 : samples_16[
i] * 256;
1205 temp_lossless_check_data ^= (
sample & 0x00ffffff) << ch;
1211 for (
int i = nb_samples;
i <
ctx->avctx->frame_size;
i++) {
1212 int32_t *sample_buffer =
s->b[
ctx->frame_index].inout_buffer[ch];
1214 sample_buffer[
i] = 0;
1218 s->b[
ctx->frame_index].max_output_bits =
bits;
1220 lossless_check_data[0] = temp_lossless_check_data;
1234 unsigned int cur_index = (
ctx->frame_index +
index + 1) %
ctx->cur_restart_interval;
1238 const int32_t *input_buffer =
s->b[cur_index].inout_buffer[ch];
1239 int32_t *sample_buffer = dp->sample_buffer[ch];
1242 if (dp->blocksize <
ctx->avctx->frame_size) {
1246 sample_buffer[
i] = input_buffer[
i];
1250 for (
unsigned int i = 0;
i < dp->blocksize;
i++)
1251 sample_buffer[
i] = input_buffer[
i + off];
1272 memset(sample_mask, 0,
sizeof(sample_mask));
1274 for (
int j = 0; j <=
ctx->cur_restart_interval; j++) {
1281 sample_mask[ch] |= sample_buffer[
i];
1288 for (
int j = 0; j <=
ctx->cur_restart_interval; j++) {
1296 sample_buffer[
i] >>=
shift;
1310 memset(sample_mask, 0,
sizeof(sample_mask));
1312 for (
int j = 0; j <=
ctx->cur_restart_interval; j++) {
1319 sample_mask[ch] |= sample_buffer[
i];
1333 uint32_t coeff_mask = 0;
1336 for (
int order = 0; order < fp->
order; order++) {
1341 coeff_mask |=
coeff;
1354 int channel,
int retry_filter)
1366 int32_t *lpc_samples =
ctx->lpc_sample_buffer;
1371 for (
unsigned int j = 0; j <=
ctx->cur_restart_interval; j++) {
1376 lpc_samples[
i] = sample_buffer[
i];
1381 lpc_samples -
ctx->lpc_sample_buffer,
1383 ctx->lpc_coeff_precision,
1391 for (
unsigned int i = 0;
i < order;
i++)
1406 for (
int ch = rh->
min_channel; ch <= rh->max_channel; ch++)
1414 int32_t maxl = INT32_MIN, maxr = INT32_MIN, minl = INT32_MAX, minr = INT32_MAX;
1415 int64_t summ = 0, sums = 0, suml = 0, sumr = 0, enl = 0, enr = 0;
1416 const int shift = 14 -
ctx->rematrix_precision;
1421 for (
int j = 0; j <=
ctx->cur_restart_interval; j++) {
1434 summ +=
FFABS(lm + rm);
1435 sums +=
FFABS(lm - rm);
1440 maxl =
FFMAX(maxl, lm);
1441 maxr =
FFMAX(maxr, rm);
1443 minl =
FFMIN(minl, lm);
1444 minr =
FFMIN(minr, rm);
1448 summ -=
FFABS(suml + sumr);
1449 sums -=
FFABS(suml - sumr);
1472 mp->outch[0] = ch0 +
i;
1500 coeff_mask |=
mp->coeff[mat][ch];
1513 if (
ctx->num_channels - 2 != 2)
1519 for (
int mat = 0; mat <
mp->count; mat++)
1529 {-9, 8}, {-8, 7}, {-15, 14},
1549 lsb_bits += !!lsb_bits;
1552 unsign = 1
U << (lsb_bits - 1);
1583 unsign = 1 << (lsb_bits - 1);
1592 bo->
min =
max - unsign + 1;
1610 int codebook_offset = 7 + (2 -
codebook);
1612 uint32_t bitcount = 0;
1614 int offset_min = INT_MAX, offset_max = INT_MAX;
1620 while (sample_min < codebook_min || sample_max > codebook_max) {
1626 unsign = 1 << lsb_bits;
1630 unsign_offset -= unsign;
1636 int temp_min, temp_max;
1641 if (temp_min < offset_min)
1642 offset_min = temp_min;
1644 temp_max = unsign - temp_min - 1;
1645 if (temp_max < offset_max)
1646 offset_max = temp_max;
1669 uint32_t previous_count = UINT32_MAX;
1670 int offset_min, offset_max;
1676 while (offset <= offset_max && offset >= offset_min) {
1683 if (temp_bo.
bitcount < previous_count) {
1688 }
else if (++is_greater >=
ctx->max_codebook_search)
1710 for (
int ch = rh->
min_channel; ch <= rh->max_channel; ch++) {
1731 if (no_filters_used) {
1739 BestOffset temp_bo = { 0, UINT32_MAX, 0, 0, 0, };
1746 if (no_filters_used) {
1747 offset_max = temp_bo.
max;
1755 s->b[
index].best_offset[ch][
i] = temp_bo;
1765 #define SAMPLE_MAX(bitdepth) ((1 << (bitdepth - 1)) - 1)
1766 #define SAMPLE_MIN(bitdepth) (~SAMPLE_MAX(bitdepth))
1768 #define MSB_MASK(bits) (-(int)(1u << (bits)))
1783 int32_t *sample_buffer =
s->b[0].decoding_params.sample_buffer[
channel];
1784 unsigned int filter_shift = fp[
FIR]->
shift;
1786 ctx->filter_state[
IIR], };
1787 int i, j = 1, k = 0;
1789 for (
i = 0;
i < 8;
i++) {
1790 filter_state[
FIR][
i] = sample_buffer[
i];
1791 filter_state[
IIR][
i] = sample_buffer[
i];
1795 int32_t *sample_buffer =
s->b[j].decoding_params.sample_buffer[
channel];
1796 unsigned int blocksize =
s->b[j].decoding_params.blocksize;
1805 for (
unsigned int order = 0; order < fp[
filter]->
order; order++)
1810 sample = sample_buffer[k];
1811 accum >>= filter_shift;
1820 filter_state[
IIR][
i] = residual;
1824 if (k >= blocksize) {
1827 if (j >
ctx->cur_restart_interval)
1832 for (
int l = 0, j = 0; j <=
ctx->cur_restart_interval; j++) {
1833 int32_t *sample_buffer =
s->b[j].decoding_params.sample_buffer[
channel];
1834 unsigned int blocksize =
s->b[j].decoding_params.blocksize;
1836 for (
int i = 0;
i < blocksize;
i++, l++)
1837 sample_buffer[
i] = filter_state[
IIR][l];
1847 for (
int ch = rh->
min_channel; ch <= rh->max_channel; ch++) {
1861 for (
unsigned int j = 0; j <=
ctx->cur_restart_interval; j++) {
1867 uint16_t seed_shr7 =
seed >> 7;
1869 sample_buffer1[
i] = ((int8_t) seed_shr7) * (1 << rh->
noise_shift);
1871 seed = (
seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
1889 for (
unsigned int j = 0; j <=
ctx->cur_restart_interval; j++) {
1894 for (
int ch = 0; ch <= maxchan; ch++)
1895 orig_samples[ch] = rematrix_samples[ch] = dp->
sample_buffer[ch][
i];
1897 for (
int mat = 0; mat < mp1->
count; mat++) {
1898 unsigned int outch = mp1->
outch[mat];
1901 for (
int ch = 0; ch <= maxchan; ch++) {
1907 rematrix_samples[outch] = accum >> 14;
1910 for (
int ch = 0; ch <= maxchan; ch++)
1913 for (
unsigned int mat = 0; mat < mp1->
count; mat++) {
1914 int8_t *bypassed_lsbs =
mp->bypassed_lsbs[mat];
1915 unsigned int outch = mp1->
outch[mat];
1919 for (
int ch = 0; ch <= maxchan; ch++) {
1925 rematrix_samples[outch] = accum >> 14;
1926 bit = rematrix_samples[outch] != orig_samples[outch];
1928 bypassed_lsbs[
i] =
bit;
1929 lsb_bypass[mat] |=
bit;
1934 for (
unsigned int mat = 0; mat < mp1->
count; mat++)
1948 #define CODEBOOK_CHANGE_BITS 21
1952 memset(path_counter, 0, (
NUM_CODEBOOKS + 1) *
sizeof(*path_counter));
1964 int idx =
src->cur_idx;
1966 *prev_bo = idx ?
s->b[idx - 1].best_offset[
channel] :
1968 uint32_t bitcount =
src->bitcount;
1969 int prev_codebook =
src->path[idx];
1971 bitcount += cur_bo[cur_codebook].
bitcount;
1973 if (prev_codebook != cur_codebook ||
1988 unsigned int best_codebook;
1994 uint32_t best_bitcount = UINT32_MAX;
1999 uint32_t prev_best_bitcount = UINT32_MAX;
2001 for (
unsigned int last_best = 0; last_best < 2; last_best++) {
2004 uint32_t temp_bitcount;
2014 src_path = &path_counter[
codebook];
2019 if (temp_bitcount < best_bitcount) {
2020 best_bitcount = temp_bitcount;
2024 if (temp_bitcount < prev_best_bitcount) {
2025 prev_best_bitcount = temp_bitcount;
2026 if (src_path != dst_path)
2030 dst_path->
bitcount = temp_bitcount;
2047 best_codebook = *best_path++;
2064 uint8_t max_huff_lsbs = 0, max_output_bits = 0;
2065 int8_t max_shift = 0;
2068 memcpy(&
s->b[
index].major_decoding_params,
2071 int8_t
shift =
s->b[
index].decoding_params.output_shift[ch];
2075 for (
int ch = rh->
min_channel; ch <= rh->max_channel; ch++) {
2076 uint8_t huff_lsbs =
s->b[
index].channel_params[ch].huff_lsbs;
2078 max_huff_lsbs =
FFMAX(max_huff_lsbs, huff_lsbs);
2080 memcpy(&
s->b[
index].major_channel_params[ch],
2081 &
s->b[
index].channel_params[ch],
2090 if (max_output_bits < s->
b[
index].max_output_bits)
2091 max_output_bits =
s->b[
index].max_output_bits;
2094 s->cur_restart_header = &
s->restart_header;
2099 s->major_filter_state_subblock = 1;
2100 s->major_cur_subblock_index = 0;
2105 s->cur_restart_header = &
s->restart_header;
2118 s->b[0].decoding_params.blocksize = 8;
2119 s->b[1].decoding_params.blocksize -= 8;
2139 ctx->number_of_frames =
ctx->major_number_of_frames;
2141 s->cur_restart_header = &
s->restart_header;
2155 int bytes_written = 0;
2157 int restart_frame,
ret;
2158 const uint8_t *
data;
2181 goto input_and_return;
2184 restart_frame = !
ctx->frame_index;
2186 if (restart_frame) {
2188 for (
int n = 0; n <
ctx->num_substreams; n++)
2191 if (
ctx->min_restart_interval !=
ctx->cur_restart_interval)
2205 ctx->next_major_number_of_frames++;
2208 for (
int n = 0; n <
ctx->num_substreams; n++)
2211 restart_frame = (
ctx->frame_index + 1) %
ctx->min_restart_interval;
2213 if (!restart_frame) {
2214 for (
unsigned int seq_index = 0; seq_index <
ctx->restart_intervals; seq_index++) {
2215 unsigned int number_of_samples;
2217 ctx->number_of_frames =
ctx->next_major_number_of_frames;
2218 ctx->number_of_subblocks =
ctx->next_major_number_of_frames + 1;
2220 number_of_samples = avctx->
frame_size *
ctx->number_of_frames;
2222 for (
int n = 0; n <
ctx->num_substreams; n++) {
2225 for (
int i = 0;
i <
s->b[seq_index].seq_size;
i++) {
2231 if (number_of_samples > 0) {
2232 for (
int n = 0; n <
ctx->num_substreams; n++)
2237 if (
ctx->frame_index == (
ctx->cur_restart_interval - 1)) {
2238 ctx->major_frame_size =
ctx->next_major_frame_size;
2239 ctx->next_major_frame_size = 0;
2240 ctx->major_number_of_frames =
ctx->next_major_number_of_frames;
2241 ctx->next_major_number_of_frames = 0;
2245 if (!
frame &&
ctx->last_frames <
ctx->cur_restart_interval - 1)
2248 if (bytes_written > 0) {
2274 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
2275 #define OFFSET(x) offsetof(MLPEncodeContext, x)
2278 {
"lpc_coeff_precision",
"LPC coefficient precision",
OFFSET(lpc_coeff_precision),
AV_OPT_TYPE_INT, {.i64 = 15 }, 0, 15,
FLAGS },
2282 {
"lpc_passes",
"Number of passes to use for Cholesky factorization during LPC analysis",
OFFSET(lpc_passes),
AV_OPT_TYPE_INT, {.i64 = 2 }, 1, INT_MAX,
FLAGS },
2283 {
"codebook_search",
"Max number of codebook searches",
OFFSET(max_codebook_search),
AV_OPT_TYPE_INT, {.i64 = 3 }, 1, 100,
FLAGS },
2287 {
"rematrix_precision",
"Rematrix coefficient precision",
OFFSET(rematrix_precision),
AV_OPT_TYPE_INT, {.i64 = 1 }, 0, 14,
FLAGS },
2298 #if CONFIG_MLP_ENCODER
2312 .p.supported_samplerates = (
const int[]) {44100, 48000, 88200, 96000, 176400, 192000, 0},
2317 #if CONFIG_TRUEHD_ENCODER
2332 .p.supported_samplerates = (
const int[]) {44100, 48000, 88200, 96000, 176400, 192000, 0},
unsigned int major_cur_subblock_index
static void clear_decoding_params(DecodingParams *decoding_params)
Clears a DecodingParams struct the way it should be after a restart header.
int frame_size
Number of samples per channel in an audio frame.
static void default_decoding_params(MLPEncodeContext *ctx, DecodingParams *dp)
Sets default vales in our encoder for a DecodingParams struct.
static int compare_matrix_params(MLPEncodeContext *ctx, MLPSubstream *s, const MatrixParams *prev, const MatrixParams *mp)
Compare two primitive matrices and returns 1 if anything has changed.
#define AV_CH_LAYOUT_5POINT0_BACK
int coded_sample_fmt[2]
sample format encoded for MLP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define MLP_MIN_LPC_ORDER
uint8_t max_order[MAX_CHANNELS]
uint8_t codebook
Which VLC codebook to use to read residuals.
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
static av_cold void mlp_encode_init_static(void)
static uint8_t xor_32_to_8(uint32_t value)
XOR four bytes into one.
static ChannelParams restart_channel_params[MAX_CHANNELS]
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int64_t *duration)
Remove frame(s) from the queue.
#define AV_CHANNEL_LAYOUT_STEREO
static int put_bytes_output(const PutBitContext *s)
int sample_rate
samples per second
int major_params_changed
params_changed to be written to bitstream.
#define AV_CHANNEL_LAYOUT_4POINT1
static void generate_2_noise_channels(MLPEncodeContext *ctx, MLPSubstream *s)
Generates two noise channels worth of data.
static int number_sbits(int32_t n)
Calculates the smallest number of bits it takes to encode a given signed value in two's complement.
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
static void write_major_sync(MLPEncodeContext *ctx, uint8_t *buf, int buf_size)
Writes a major sync header to the bitstream.
static av_cold int mlp_encode_close(AVCodecContext *avctx)
static void codebook_bits_offset(MLPEncodeContext *ctx, DecodingParams *dp, int channel, int codebook, int32_t sample_min, int32_t sample_max, int32_t offset, BestOffset *bo)
Determines the least amount of bits needed to encode the samples using a given codebook and a given o...
uint16_t blocksize
number of PCM samples in current audio block
#define SAMPLE_MAX(bitdepth)
int coded_peak_bitrate
peak bitrate for this major sync header
#define AV_CH_LAYOUT_MONO
int ff_lpc_calc_coefs(LPCContext *s, const int32_t *samples, int blocksize, int min_order, int max_order, int precision, int32_t coefs[][MAX_LPC_ORDER], int *shift, enum FFLPCType lpc_type, int lpc_passes, int omethod, int min_shift, int max_shift, int zero_shift)
Calculate LPC coefficients for multiple orders.
static void put_sbits(PutBitContext *pb, int n, int32_t value)
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order, enum FFLPCType lpc_type)
Initialize LPCContext.
static double mp(int i, double w0, double r)
#define SUBSTREAM_INFO_HIGH_RATE
This structure describes decoded (raw) audio or video data.
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
@ FF_LPC_TYPE_CHOLESKY
Cholesky factorization.
static const BestOffset restart_best_offset[NUM_CODEBOOKS]
static void write_frame_headers(MLPEncodeContext *ctx, uint8_t *frame_header, uint8_t *substream_headers, unsigned int length, int restart_frame, uint16_t substream_data_len[MAX_SUBSTREAMS])
Writes the access unit and substream headers to the bitstream.
static void clear_path_counter(PathCounter *path_counter)
unsigned int number_of_subblocks
uint8_t ff_mlp_calculate_parity(const uint8_t *buf, unsigned int buf_size)
XOR together all the bytes of a buffer.
unsigned int number_of_frames
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
static void no_codebook_bits_offset(MLPEncodeContext *ctx, DecodingParams *dp, int channel, int32_t offset, int32_t min, int32_t max, BestOffset *bo)
Determines the amount of bits needed to encode the samples using no codebooks and a specified offset.
void(* filter)(uint8_t *src, int stride, int qscale)
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
RestartHeader restart_header
const ChannelInformation ff_mlp_ch_info[21]
Tables defining channel information.
uint8_t ch8_presentation_mod
channel modifier for TrueHD stream 2
int nb_channels
Number of channels in this layout.
static void write_decoding_params(MLPEncodeContext *ctx, MLPSubstream *s, PutBitContext *pb, int params_changed, unsigned int subblock_index)
Writes decoding parameters to the bitstream.
DecodingParams decoding_params
static void no_codebook_bits(MLPEncodeContext *ctx, DecodingParams *dp, int channel, int32_t min, int32_t max, BestOffset *bo)
Determines the least amount of bits needed to encode the samples using no codebooks.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
int8_t noise_shift[MAX_CHANNELS]
#define PARAM_PRESENCE_FLAGS
static void write_matrix_params(MLPEncodeContext *ctx, MLPSubstream *s, DecodingParams *dp, PutBitContext *pb)
Writes matrix params for all primitive matrices to the bitstream.
#define bit(string, value)
static const int8_t codebook_extremes[3][2]
Min and max values that can be encoded with each codebook.
int32_t lossless_check_data
uint16_t output_timing
Timestamp of current access unit.
uint16_t ff_mlp_checksum16(const uint8_t *buf, unsigned int buf_size)
AVCodec p
The public AVCodec.
FilterParams filter_params[NUM_FILTERS]
uint8_t ff_mlp_checksum8(const uint8_t *buf, unsigned int buf_size)
MLP uses checksums that seem to be based on the standard CRC algorithm, but are not (in implementatio...
int min_restart_interval
Min interval of access units in between two major frames.
AVChannelLayout ch_layout
Audio channel layout.
uint8_t huff_lsbs
Size of residual suffix not encoded using VLC.
static int mlp_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet)
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
unsigned int major_number_of_frames
#define MLP_MAX_LPC_ORDER
#define AV_CHANNEL_LAYOUT_2POINT1
static void apply_filters(MLPEncodeContext *ctx, MLPSubstream *s)
static void clear_channel_params(ChannelParams *channel_params, int nb_channels)
Clears a ChannelParams struct the way it should be after a restart header.
static void determine_output_shift(MLPEncodeContext *ctx, MLPSubstream *s)
static void analyze_sample_buffer(MLPEncodeContext *ctx, MLPSubstream *s)
int32_t inout_buffer[MAX_NCHANNELS][MAX_BLOCKSIZE]
unsigned int next_major_frame_size
Counter of number of samples for next major frame.
#define FF_CODEC_ENCODE_CB(func)
DecodingParams major_decoding_params
DecodingParams to be written to bitstream.
int32_t coeff[MAX_MATRICES][MAX_NCHANNELS]
decoding coefficients
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
#define AV_CH_LAYOUT_STEREO
int8_t output_shift[MAX_CHANNELS]
Left shift to apply to decoded PCM values to get final 24-bit output.
#define AV_CHANNEL_LAYOUT_SURROUND
ChannelParams major_channel_params[MAX_CHANNELS]
ChannelParams to be written to bitstream.
#define AV_CH_LAYOUT_QUAD
static int ff_thread_once(char *control, void(*routine)(void))
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define SUBSTREAM_INFO_MAX_2_CHAN
static uint32_t best_codebook_path_cost(MLPEncodeContext *ctx, MLPSubstream *s, int channel, PathCounter *src, int cur_codebook)
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
#define FF_ARRAY_ELEMS(a)
#define AV_CHANNEL_LAYOUT_4POINT0
static int estimate_coeff(MLPEncodeContext *ctx, MLPSubstream *s, MatrixParams *mp, int ch0, int ch1)
static void lossless_matrix_coeffs(MLPEncodeContext *ctx, MLPSubstream *s)
Determines best coefficients to use for the lossless matrix.
unsigned int max_output_bits
largest output bit-depth
static void copy_matrix_params(MatrixParams *dst, MatrixParams *src)
int max_restart_interval
Max interval of access units in between two major frames.
static int write_access_unit(MLPEncodeContext *ctx, uint8_t *buf, int buf_size, int restart_frame)
Writes an entire access unit to the bitstream.
static void set_best_codebook(MLPEncodeContext *ctx, MLPSubstream *s)
int flags
Flags modifying the (de)muxer behaviour.
int num_channels
Number of channels in major_scratch_buffer.
#define MAX_NCHANNELS
MLP encoder Copyright (c) 2008 Ramiro Polla Copyright (c) 2016-2019 Jai Luthra.
static void codebook_bits(MLPEncodeContext *ctx, DecodingParams *dp, int channel, int codebook, int offset, int32_t min, int32_t max, BestOffset *bo, int direction)
Determines the least amount of bits needed to encode the samples using a given codebook.
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
static void copy_restart_frame_params(MLPEncodeContext *ctx, MLPSubstream *s)
static int mlp_peak_bitrate(int peak_bitrate, int sample_rate)
#define CODEC_LONG_NAME(str)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
uint8_t multichannel_type8ch
channel modifier for TrueHD stream 0
int flags
major sync info flags
#define LIBAVUTIL_VERSION_INT
static void code_filter_coeffs(MLPEncodeContext *ctx, FilterParams *fp, const int32_t *fcoeff)
Determines the smallest number of bits needed to encode the filter coefficients, and if it's possible...
Describe the class of an AVClass context structure.
int32_t last_frames
Signal last frames.
unsigned int major_frame_size
Number of samples in current major frame being encoded.
int num_substreams
Number of substreams contained within this stream.
int32_t sample_buffer[MAX_NCHANNELS][MAX_BLOCKSIZE]
const char * av_default_item_name(void *ptr)
Return the context name.
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
#define AV_CH_LAYOUT_5POINT1
static void copy_filter_params(ChannelParams *dst_cp, ChannelParams *src_cp, int filter)
uint8_t fbits[MAX_MATRICES]
fraction bits
#define ORDER_METHOD_SEARCH
int32_t coeff[NUM_FILTERS][MAX_FIR_ORDER]
static void set_major_params(MLPEncodeContext *ctx, MLPSubstream *s)
Analyzes all collected bitcounts and selects the best parameters for each individual access unit.
MLPBlock b[MAX_HEADER_INTERVAL+1]
int coded_sample_rate[2]
sample rate encoded for MLP
static uint8_t * write_substr(MLPEncodeContext *ctx, MLPSubstream *s, uint8_t *buf, int buf_size, int restart_frame, uint16_t *substream_data_len)
Writes the substream data to the bitstream.
static int layout_truehd(uint64_t layout)
uint8_t channel_arrangement
channel arrangement for MLP streams
uint8_t ff_mlp_restart_checksum(const uint8_t *buf, unsigned int bit_size)
Calculate an 8-bit checksum over a restart header – a non-multiple-of-8 number of bits,...
int(* init)(AVBSFContext *ctx)
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
An AVChannelLayout holds information about the channel layout of audio data.
static int shift(int a, int b)
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
ChannelParams channel_params[MAX_CHANNELS]
enum AVSampleFormat sample_fmt
audio sample format
#define MAX_SUBSTREAMS
Maximum number of substreams that can be decoded.
const FFCodec ff_truehd_encoder
#define NUM_FILTERS
number of allowed filters
uint8_t order
number of taps in filter
uint8_t quant_step_size[MAX_CHANNELS]
left shift to apply to Huffman-decoded residuals
static void rematrix_channels(MLPEncodeContext *ctx, MLPSubstream *s)
Rematrixes all channels using chosen coefficients.
#define AV_CH_LAYOUT_5POINT1_BACK
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
int16_t huff_offset
Offset to apply to residual values.
int flags
A combination of AV_PKT_FLAG values.
#define MAX_FIR_ORDER
The maximum number of taps in IIR and FIR filters.
uint16_t channel_arrangement8
8 channel arrangement for THD streams
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
#define AV_CH_LAYOUT_3POINT1
static int compare_filter_params(const ChannelParams *prev_cp, const ChannelParams *cp, int filter)
Compares two FilterParams structures and returns 1 if anything has changed.
uint8_t lsb_bypass[MAX_MATRICES]
#define AV_CH_LAYOUT_5POINT0
BestOffset best_offset[MAX_CHANNELS][NUM_CODEBOOKS]
static void determine_bits(MLPEncodeContext *ctx, MLPSubstream *s)
Determines the least amount of bits needed to encode the samples using any or no codebook.
unsigned int restart_intervals
Number of possible major frame sizes.
uint8_t outch[MAX_MATRICES]
output channel for each matrix
#define i(width, name, range_min, range_max)
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
static int put_bits_count(PutBitContext *s)
#define AV_CH_LAYOUT_2POINT1
#define SUBSTREAM_INFO_ALWAYS_SET
static int apply_filter(MLPEncodeContext *ctx, MLPSubstream *s, int channel)
Applies the filter to the current samples, and saves the residual back into the samples buffer.
static DecodingParams restart_decoding_params[MAX_SUBSTREAMS]
static int compare_decoding_params(MLPEncodeContext *ctx, MLPSubstream *s, unsigned int index)
Compares two DecodingParams and ChannelParams structures to decide if a new decoding params header ha...
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
AVSampleFormat
Audio sample formats.
#define AV_CH_LAYOUT_4POINT1
static void determine_quant_step_size(MLPEncodeContext *ctx, MLPSubstream *s)
Determines how many bits are zero at the end of all samples so they can be shifted out.
#define AV_CHANNEL_LAYOUT_3POINT1
uint16_t input_timing
Decoding timestamp of current access unit.
const char * name
Name of the codec implementation.
const AVChannelLayout ff_mlp_ch_layouts[12]
sample data coding information
int32_t lpc_sample_buffer[MAX_HEADER_INTERVAL *MAX_BLOCKSIZE]
static void code_matrix_coeffs(MLPEncodeContext *ctx, MLPSubstream *s, DecodingParams *dp, unsigned int mat)
Determines how many fractional bits are needed to encode matrix coefficients.
av_cold void ff_mlp_init_crc(void)
uint8_t ch6_presentation_mod
channel modifier for TrueHD stream 1
#define MAJOR_SYNC_INFO_SIGNATURE
char path[MAX_HEADER_INTERVAL+2]
int64_t frame_num
Frame counter, set by libavcodec.
#define NUM_CODEBOOKS
Number of possible codebooks (counting "no codebooks")
static int number_trailing_zeroes(int32_t sample, unsigned int max, unsigned int def)
Counts the number of trailing zeroes in a value.
#define FFSWAP(type, a, b)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
#define AV_CH_LAYOUT_SURROUND
static void input_to_sample_buffer(MLPEncodeContext *ctx, MLPSubstream *s)
static int compare_best_offset(const BestOffset *prev, const BestOffset *cur)
main external API structure.
uint64_t av_channel_layout_subset(const AVChannelLayout *channel_layout, uint64_t mask)
Find out what channels from a given set are present in a channel layout, without regard for their pos...
const FFCodec ff_mlp_encoder
@ AV_OPT_TYPE_INT
Underlying C type is int.
uint8_t ch2_presentation_mod
channel modifier for TrueHD stream 0
unsigned int next_major_number_of_frames
MatrixParams matrix_params
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Filter the word “frame” indicates either a video frame or a group of audio samples
#define SAMPLE_MIN(bitdepth)
static void set_filter(MLPEncodeContext *ctx, MLPSubstream *s, int channel, int retry_filter)
Determines the best filter parameters for the given data and writes the necessary information to the ...
int8_t bypassed_lsbs[MAX_MATRICES][MAX_BLOCKSIZE]
unsigned int frame_index
Index of current frame being encoded.
static void input_data_internal(MLPEncodeContext *ctx, MLPSubstream *s, uint8_t **const samples, int nb_samples, int is24)
Inputs data from the samples passed by lavc into the context, shifts them appropriately depending on ...
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
int32_t forco[MAX_MATRICES][MAX_NCHANNELS]
forward coefficients
#define AV_CHANNEL_LAYOUT_MONO
#define MIN_HEADER_INTERVAL
const uint8_t ff_mlp_huffman_tables[3][18][2]
Tables defining the Huffman codes.
static av_cold int mlp_encode_init(AVCodecContext *avctx)
This structure stores compressed data.
static const AVOption mlp_options[]
#define AV_CH_LAYOUT_4POINT0
static void write_restart_header(MLPEncodeContext *ctx, MLPSubstream *s, PutBitContext *pb)
Writes a restart header to the bitstream.
#define CODEBOOK_CHANGE_BITS
static const double coeff[2][5]
static void process_major_frame(MLPEncodeContext *ctx, MLPSubstream *s)
int32_t filter_state[NUM_FILTERS][MAX_HEADER_INTERVAL *MAX_BLOCKSIZE]
#define AV_CHANNEL_LAYOUT_5POINT0
#define MLP_MIN_LPC_SHIFT
unsigned int major_filter_state_subblock
uint8_t count
number of matrices to apply
static void determine_filters(MLPEncodeContext *ctx, MLPSubstream *s)
Tries to determine a good prediction filter, and applies it to the samples buffer if the filter is go...
static void input_data(MLPEncodeContext *ctx, MLPSubstream *s, uint8_t **const samples, int nb_samples)
Wrapper function for inputting data in two different bit-depths.
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
#define AV_CHANNEL_LAYOUT_5POINT1
uint8_t shift
Right shift to apply to output of filter.
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
static void write_block_data(MLPEncodeContext *ctx, MLPSubstream *s, PutBitContext *pb, unsigned int subblock_index)
Writes the residuals to the bitstream.
#define MLP_MAX_LPC_SHIFT
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
@ FF_LPC_TYPE_LEVINSON
Levinson-Durbin recursion.
uint8_t param_presence_flags
Bitmask of which parameter sets are conveyed in a decoding parameter block.
static void write_filter_params(MLPEncodeContext *ctx, ChannelParams *cp, PutBitContext *pb, int channel, unsigned int filter)
Writes filter parameters for one filter to the bitstream.
#define MAX_HEADER_INTERVAL
uint8_t multichannel_type6ch
channel modifier for TrueHD stream 0
static const unsigned codebook[256][2]
RestartHeader * cur_restart_header
static const AVClass mlp_class
int32_t coefs[MAX_CHANNELS][MAX_LPC_ORDER][MAX_LPC_ORDER]