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;
526 ctx->coded_sample_rate[1] = -1 & 0xf;
537 ctx->wordlength = 16;
543 ctx->wordlength = 24;
549 ctx->coded_sample_fmt[1] = -1 & 0xf;
555 ctx->min_restart_interval =
ctx->cur_restart_interval =
ctx->max_restart_interval;
556 ctx->restart_intervals =
ctx->max_restart_interval /
ctx->min_restart_interval;
558 ctx->num_substreams = 1;
562 static const uint64_t layout_arrangement[] = {
574 !
"Impossible channel layout");
575 if (channels_present == layout_arrangement[
i])
578 ctx->channel_arrangement =
i;
585 switch (channels_present) {
587 ctx->ch2_presentation_mod= 3;
588 ctx->ch6_presentation_mod= 3;
589 ctx->ch8_presentation_mod= 3;
590 ctx->thd_substream_info = 0x14;
593 ctx->ch2_presentation_mod= 1;
594 ctx->ch6_presentation_mod= 1;
595 ctx->ch8_presentation_mod= 1;
596 ctx->thd_substream_info = 0x14;
605 ctx->ch2_presentation_mod= 0;
606 ctx->ch6_presentation_mod= 0;
607 ctx->ch8_presentation_mod= 0;
608 ctx->thd_substream_info = 0x3C;
614 ctx->channel_occupancy = 0;
615 ctx->summary_info = 0;
616 ctx->channel_arrangement =
621 for (
int n = 0; n <
ctx->num_substreams; n++)
622 ctx->s[n].b[
index].seq_size = ((
index + 1) *
ctx->min_restart_interval) + 1;
643 rh = &
ctx->s[1].restart_header;
798 if (!
ctx->noise_type)
801 for (
unsigned int mat = 0; mat <
mp->count; mat++) {
806 for (
int ch = 0; ch <= max_channel; ch++) {
839 for (
int i = 0;
i < fp->
order;
i++) {
853 unsigned int subblock_index)
904 for (
int ch = rh->
min_channel; ch <= rh->max_channel; ch++) {
905 ChannelParams *cp = &
s->b[subblock_index].major_channel_params[ch];
961 for (
int ch = rh->
min_channel; ch <= rh->max_channel; ch++) {
962 ChannelParams *cp = &
s->b[subblock_index].major_channel_params[ch];
966 codebook_index [ch] = cp->
codebook - 1;
972 sign_huff_offset[ch] -= 7 << lsb_bits[ch];
976 sign_huff_offset[ch] -= 1 << sign_shift;
980 for (
unsigned int mat = 0; mat <
mp->count; mat++) {
981 if (
mp->lsb_bypass[mat]) {
982 const int8_t *bypassed_lsbs =
mp->bypassed_lsbs[mat];
988 for (
int ch = rh->
min_channel; ch <= rh->max_channel; ch++) {
991 sample -= sign_huff_offset[ch];
993 if (codebook_index[ch] >= 0) {
994 int vlc =
sample >> lsb_bits[ch];
997 sample &= ((1 << lsb_bits[ch]) - 1);
1008 uint8_t *buf,
int buf_size,
1010 uint16_t *substream_data_len)
1012 int32_t *lossless_check_data = &
s->b[
ctx->frame_index].lossless_check_data;
1013 unsigned int cur_subblock_index =
s->major_cur_subblock_index;
1014 unsigned int num_subblocks =
s->major_filter_state_subblock;
1016 int substr_restart_frame = restart_frame;
1017 uint8_t
parity, checksum;
1021 s->cur_restart_header = rh;
1025 for (
unsigned int subblock = 0; subblock <= num_subblocks; subblock++) {
1026 unsigned int subblock_index = cur_subblock_index++;
1028 params_changed =
s->b[subblock_index].major_params_changed;
1030 if (substr_restart_frame || params_changed) {
1033 if (substr_restart_frame) {
1050 put_bits(&pb, 1, !substr_restart_frame);
1052 substr_restart_frame = 0;
1059 if (
ctx->last_frames == 0 &&
ctx->shorten_by) {
1062 put_bits(&pb, 16, (
ctx->shorten_by & 0x1FFF) | 0xE000);
1082 buf += substream_data_len[0];
1084 s->major_cur_subblock_index +=
s->major_filter_state_subblock + 1;
1085 s->major_filter_state_subblock = 0;
1092 uint8_t *substream_headers,
unsigned int length,
1096 uint16_t access_unit_header = 0;
1097 uint16_t substream_data_end = 0;
1098 uint16_t parity_nibble = 0;
1100 parity_nibble =
ctx->input_timing;
1101 parity_nibble ^= length;
1103 for (
unsigned int substr = 0; substr <
ctx->num_substreams; substr++) {
1104 uint16_t substr_hdr = 0;
1106 substream_data_end += substream_data_len[substr];
1108 substr_hdr |= (0 << 15);
1109 substr_hdr |= (!restart_frame << 14);
1110 substr_hdr |= (1 << 13);
1111 substr_hdr |= (0 << 12);
1112 substr_hdr |= (substream_data_end / 2) & 0x0FFF;
1114 AV_WB16(substream_headers, substr_hdr);
1116 parity_nibble ^= *substream_headers++;
1117 parity_nibble ^= *substream_headers++;
1120 parity_nibble ^= parity_nibble >> 8;
1121 parity_nibble ^= parity_nibble >> 4;
1122 parity_nibble &= 0xF;
1124 access_unit_header |= (parity_nibble ^ 0xF) << 12;
1125 access_unit_header |= length & 0xFFF;
1133 int buf_size,
int restart_frame)
1136 uint8_t *buf1, *buf0 = buf;
1143 if (restart_frame) {
1152 for (
unsigned int substr = 0; substr <
ctx->num_substreams; substr++) {
1157 for (
int substr = 0; substr <
ctx->num_substreams; substr++) {
1159 uint8_t *buf0 = buf;
1161 buf =
write_substr(
ctx,
s, buf, buf_size, restart_frame, &substream_data_len[substr]);
1162 buf_size -= buf - buf0;
1165 total_length = buf - buf0;
1169 return total_length;
1181 uint8_t **
const samples,
1182 int nb_samples,
int is24)
1184 int32_t *lossless_check_data = &
s->b[
ctx->frame_index].lossless_check_data;
1186 int32_t temp_lossless_check_data = 0;
1189 for (
int i = 0;
i < nb_samples;
i++) {
1192 const int16_t *samples_16 = (
const int16_t *)samples[ch];
1193 int32_t *sample_buffer =
s->b[
ctx->frame_index].inout_buffer[ch];
1196 sample = is24 ? samples_32[
i] >> 8 : samples_16[
i] * 256;
1200 temp_lossless_check_data ^= (
sample & 0x00ffffff) << ch;
1206 for (
int i = nb_samples;
i <
ctx->avctx->frame_size;
i++) {
1207 int32_t *sample_buffer =
s->b[
ctx->frame_index].inout_buffer[ch];
1209 sample_buffer[
i] = 0;
1213 s->b[
ctx->frame_index].max_output_bits =
bits;
1215 lossless_check_data[0] = temp_lossless_check_data;
1229 unsigned int cur_index = (
ctx->frame_index +
index + 1) %
ctx->cur_restart_interval;
1233 const int32_t *input_buffer =
s->b[cur_index].inout_buffer[ch];
1241 sample_buffer[
i] = input_buffer[
i];
1246 sample_buffer[
i] = input_buffer[
i + off];
1267 memset(sample_mask, 0,
sizeof(sample_mask));
1269 for (
int j = 0; j <=
ctx->cur_restart_interval; j++) {
1276 sample_mask[ch] |= sample_buffer[
i];
1283 for (
int j = 0; j <=
ctx->cur_restart_interval; j++) {
1291 sample_buffer[
i] >>=
shift;
1305 memset(sample_mask, 0,
sizeof(sample_mask));
1307 for (
int j = 0; j <=
ctx->cur_restart_interval; j++) {
1314 sample_mask[ch] |= sample_buffer[
i];
1328 uint32_t coeff_mask = 0;
1331 for (
int order = 0; order < fp->
order; order++) {
1336 coeff_mask |=
coeff;
1349 int channel,
int retry_filter)
1361 int32_t *lpc_samples =
ctx->lpc_sample_buffer;
1366 for (
unsigned int j = 0; j <=
ctx->cur_restart_interval; j++) {
1371 lpc_samples[
i] = sample_buffer[
i];
1376 lpc_samples -
ctx->lpc_sample_buffer,
1378 ctx->lpc_coeff_precision,
1386 for (
unsigned int i = 0;
i < order;
i++)
1401 for (
int ch = rh->
min_channel; ch <= rh->max_channel; ch++)
1409 int32_t maxl = INT32_MIN, maxr = INT32_MIN, minl = INT32_MAX, minr = INT32_MAX;
1410 int64_t summ = 0, sums = 0, suml = 0, sumr = 0, enl = 0, enr = 0;
1411 const int shift = 14 -
ctx->rematrix_precision;
1416 for (
int j = 0; j <=
ctx->cur_restart_interval; j++) {
1429 summ +=
FFABS(lm + rm);
1430 sums +=
FFABS(lm - rm);
1435 maxl =
FFMAX(maxl, lm);
1436 maxr =
FFMAX(maxr, rm);
1438 minl =
FFMIN(minl, lm);
1439 minr =
FFMIN(minr, rm);
1443 summ -=
FFABS(suml + sumr);
1444 sums -=
FFABS(suml - sumr);
1467 mp->outch[0] = ch0 +
i;
1495 coeff_mask |=
mp->coeff[mat][ch];
1508 if (
ctx->num_channels - 2 != 2)
1514 for (
int mat = 0; mat <
mp->count; mat++)
1524 {-9, 8}, {-8, 7}, {-15, 14},
1544 lsb_bits += !!lsb_bits;
1547 unsign = 1U << (lsb_bits - 1);
1578 unsign = 1 << (lsb_bits - 1);
1587 bo->
min =
max - unsign + 1;
1605 int codebook_offset = 7 + (2 -
codebook);
1607 uint32_t bitcount = 0;
1609 int offset_min = INT_MAX, offset_max = INT_MAX;
1615 while (sample_min < codebook_min || sample_max > codebook_max) {
1621 unsign = 1 << lsb_bits;
1625 unsign_offset -= unsign;
1631 int temp_min, temp_max;
1636 if (temp_min < offset_min)
1637 offset_min = temp_min;
1639 temp_max = unsign - temp_min - 1;
1640 if (temp_max < offset_max)
1641 offset_max = temp_max;
1664 uint32_t previous_count = UINT32_MAX;
1665 int offset_min, offset_max;
1678 if (temp_bo.
bitcount < previous_count) {
1683 }
else if (++is_greater >=
ctx->max_codebook_search)
1705 for (
int ch = rh->
min_channel; ch <= rh->max_channel; ch++) {
1726 if (no_filters_used) {
1734 BestOffset temp_bo = { 0, UINT32_MAX, 0, 0, 0, };
1741 if (no_filters_used) {
1742 offset_max = temp_bo.
max;
1750 s->b[
index].best_offset[ch][
i] = temp_bo;
1760#define SAMPLE_MAX(bitdepth) ((1 << (bitdepth - 1)) - 1)
1761#define SAMPLE_MIN(bitdepth) (~SAMPLE_MAX(bitdepth))
1763#define MSB_MASK(bits) (-(int)(1u << (bits)))
1778 int32_t *sample_buffer =
s->b[0].decoding_params.sample_buffer[
channel];
1779 unsigned int filter_shift = fp[
FIR]->
shift;
1781 ctx->filter_state[
IIR], };
1782 int i, j = 1, k = 0;
1784 for (
i = 0;
i < 8;
i++) {
1785 filter_state[
FIR][
i] = sample_buffer[
i];
1786 filter_state[
IIR][
i] = sample_buffer[
i];
1790 int32_t *sample_buffer =
s->b[j].decoding_params.sample_buffer[
channel];
1791 unsigned int blocksize =
s->b[j].decoding_params.blocksize;
1800 for (
unsigned int order = 0; order < fp[
filter]->
order; order++)
1805 sample = sample_buffer[k];
1806 accum >>= filter_shift;
1815 filter_state[
IIR][
i] = residual;
1819 if (k >= blocksize) {
1822 if (j >
ctx->cur_restart_interval)
1827 for (
int l = 0, j = 0; j <=
ctx->cur_restart_interval; j++) {
1828 int32_t *sample_buffer =
s->b[j].decoding_params.sample_buffer[
channel];
1829 unsigned int blocksize =
s->b[j].decoding_params.blocksize;
1831 for (
int i = 0;
i < blocksize;
i++, l++)
1832 sample_buffer[
i] = filter_state[
IIR][l];
1842 for (
int ch = rh->
min_channel; ch <= rh->max_channel; ch++) {
1856 for (
unsigned int j = 0; j <=
ctx->cur_restart_interval; j++) {
1862 uint16_t seed_shr7 =
seed >> 7;
1864 sample_buffer1[
i] = ((int8_t) seed_shr7) * (1 << rh->
noise_shift);
1866 seed = (
seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
1884 for (
unsigned int j = 0; j <=
ctx->cur_restart_interval; j++) {
1889 for (
int ch = 0; ch <= maxchan; ch++)
1890 orig_samples[ch] = rematrix_samples[ch] = dp->
sample_buffer[ch][
i];
1892 for (
int mat = 0; mat < mp1->
count; mat++) {
1893 unsigned int outch = mp1->
outch[mat];
1896 for (
int ch = 0; ch <= maxchan; ch++) {
1902 rematrix_samples[outch] = accum >> 14;
1905 for (
int ch = 0; ch <= maxchan; ch++)
1908 for (
unsigned int mat = 0; mat < mp1->
count; mat++) {
1909 int8_t *bypassed_lsbs =
mp->bypassed_lsbs[mat];
1910 unsigned int outch = mp1->
outch[mat];
1914 for (
int ch = 0; ch <= maxchan; ch++) {
1920 rematrix_samples[outch] = accum >> 14;
1921 bit = rematrix_samples[outch] != orig_samples[outch];
1923 bypassed_lsbs[
i] =
bit;
1924 lsb_bypass[mat] |=
bit;
1929 for (
unsigned int mat = 0; mat < mp1->
count; mat++)
1943#define CODEBOOK_CHANGE_BITS 21
1947 memset(path_counter, 0, (
NUM_CODEBOOKS + 1) *
sizeof(*path_counter));
1959 int idx =
src->cur_idx;
1961 *prev_bo = idx ?
s->b[idx - 1].best_offset[
channel] :
1963 uint32_t bitcount =
src->bitcount;
1964 int prev_codebook =
src->path[idx];
1966 bitcount += cur_bo[cur_codebook].
bitcount;
1968 if (prev_codebook != cur_codebook ||
1983 unsigned int best_codebook;
1989 uint32_t best_bitcount = UINT32_MAX;
1994 uint32_t prev_best_bitcount = UINT32_MAX;
1996 for (
unsigned int last_best = 0; last_best < 2; last_best++) {
1999 uint32_t temp_bitcount;
2009 src_path = &path_counter[
codebook];
2014 if (temp_bitcount < best_bitcount) {
2015 best_bitcount = temp_bitcount;
2019 if (temp_bitcount < prev_best_bitcount) {
2020 prev_best_bitcount = temp_bitcount;
2021 if (src_path != dst_path)
2025 dst_path->
bitcount = temp_bitcount;
2042 best_codebook = *best_path++;
2059 uint8_t max_huff_lsbs = 0, max_output_bits = 0;
2060 int8_t max_shift = 0;
2063 memcpy(&
s->b[
index].major_decoding_params,
2066 int8_t
shift =
s->b[
index].decoding_params.output_shift[ch];
2070 for (
int ch = rh->
min_channel; ch <= rh->max_channel; ch++) {
2071 uint8_t huff_lsbs =
s->b[
index].channel_params[ch].huff_lsbs;
2073 max_huff_lsbs =
FFMAX(max_huff_lsbs, huff_lsbs);
2075 memcpy(&
s->b[
index].major_channel_params[ch],
2076 &
s->b[
index].channel_params[ch],
2085 if (max_output_bits < s->
b[
index].max_output_bits)
2086 max_output_bits =
s->b[
index].max_output_bits;
2089 s->cur_restart_header = &
s->restart_header;
2094 s->major_filter_state_subblock = 1;
2095 s->major_cur_subblock_index = 0;
2100 s->cur_restart_header = &
s->restart_header;
2113 s->b[0].decoding_params.blocksize = 8;
2114 s->b[1].decoding_params.blocksize -= 8;
2134 ctx->number_of_frames =
ctx->major_number_of_frames;
2136 s->cur_restart_header = &
s->restart_header;
2150 int bytes_written = 0;
2152 int restart_frame, ret;
2153 const uint8_t *
data;
2176 goto input_and_return;
2179 restart_frame = !
ctx->frame_index;
2181 if (restart_frame) {
2183 for (
int n = 0; n <
ctx->num_substreams; n++)
2186 if (
ctx->min_restart_interval !=
ctx->cur_restart_interval)
2200 ctx->next_major_number_of_frames++;
2203 for (
int n = 0; n <
ctx->num_substreams; n++)
2206 restart_frame = (
ctx->frame_index + 1) %
ctx->min_restart_interval;
2208 if (!restart_frame) {
2209 for (
unsigned int seq_index = 0; seq_index <
ctx->restart_intervals; seq_index++) {
2210 unsigned int number_of_samples;
2212 ctx->number_of_frames =
ctx->next_major_number_of_frames;
2213 ctx->number_of_subblocks =
ctx->next_major_number_of_frames + 1;
2215 number_of_samples = avctx->
frame_size *
ctx->number_of_frames;
2217 for (
int n = 0; n <
ctx->num_substreams; n++) {
2220 for (
int i = 0;
i <
s->b[seq_index].seq_size;
i++) {
2226 if (number_of_samples > 0) {
2227 for (
int n = 0; n <
ctx->num_substreams; n++)
2232 if (
ctx->frame_index == (
ctx->cur_restart_interval - 1)) {
2233 ctx->major_frame_size =
ctx->next_major_frame_size;
2234 ctx->next_major_frame_size = 0;
2235 ctx->major_number_of_frames =
ctx->next_major_number_of_frames;
2236 ctx->next_major_number_of_frames = 0;
2240 if (!
frame &&
ctx->last_frames <
ctx->cur_restart_interval - 1)
2243 if (bytes_written > 0) {
2268#define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
2269#define OFFSET(x) offsetof(MLPEncodeContext, x)
2272{
"lpc_coeff_precision",
"LPC coefficient precision",
OFFSET(lpc_coeff_precision),
AV_OPT_TYPE_INT, {.i64 = 15 }, 0, 15,
FLAGS },
2276{
"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 },
2277{
"codebook_search",
"Max number of codebook searches",
OFFSET(max_codebook_search),
AV_OPT_TYPE_INT, {.i64 = 3 }, 1, 100,
FLAGS },
2281{
"rematrix_precision",
"Rematrix coefficient precision",
OFFSET(rematrix_precision),
AV_OPT_TYPE_INT, {.i64 = 1 }, 0, 14,
FLAGS },
2286 .class_name =
"mlpenc",
2292#if CONFIG_MLP_ENCODER
2311#if CONFIG_TRUEHD_ENCODER
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
static double mp(int i, double w0, double r)
const FFCodec ff_mlp_encoder
const FFCodec ff_truehd_encoder
static AVFormatContext * ctx
av_cold void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
int ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, AVPacket *pkt)
Remove frame(s) from the queue.
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Libavcodec external API header.
#define i(width, name, range_min, range_max)
#define bit(string, value)
static const unsigned codebook[256][2]
Public libavutil channel layout APIs header.
#define CODEC_CH_LAYOUTS(...)
#define CODEC_SAMPLERATES(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define CODEC_SAMPLEFMTS(...)
#define CODEC_CH_LAYOUTS_ARRAY(array)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Public header for CRC hash function implementation.
int(* init)(AVBSFContext *ctx)
channel
Use these values when setting the channel map with ebur128_set_channel().
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
static const uint8_t bits[8]
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_INT
Underlying C type is int.
#define AV_CH_LAYOUT_2POINT1
#define AV_CH_LAYOUT_QUAD
#define AV_CH_LAYOUT_4POINT0
#define AV_CH_LAYOUT_5POINT0
#define AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_SURROUND
#define AV_CH_LAYOUT_5POINT0_BACK
#define AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_5POINT1
#define AV_CH_LAYOUT_4POINT1
#define AV_CH_LAYOUT_3POINT1
#define AV_CH_LAYOUT_5POINT1_BACK
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
#define AV_CHANNEL_LAYOUT_4POINT0
#define AV_CHANNEL_LAYOUT_4POINT1
#define AV_CHANNEL_LAYOUT_3POINT1
#define AV_CHANNEL_LAYOUT_5POINT0
#define AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_5POINT1
#define AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_SURROUND
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...
#define AV_CHANNEL_LAYOUT_2POINT1
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
#define LIBAVUTIL_VERSION_INT
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
static int shift(int a, int b)
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
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.
av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order, enum FFLPCType lpc_type)
Initialize LPCContext.
static int ff_thread_once(char *control, void(*routine)(void))
#define ORDER_METHOD_SEARCH
@ FF_LPC_TYPE_CHOLESKY
Cholesky factorization.
@ FF_LPC_TYPE_LEVINSON
Levinson-Durbin recursion.
static const uint16_t mask[17]
#define FFSWAP(type, a, b)
const uint8_t ff_mlp_huffman_tables[3][18][2]
Tables defining the Huffman codes.
uint16_t ff_mlp_checksum16(const uint8_t *buf, unsigned int buf_size)
const AVChannelLayout ff_mlp_ch_layouts[12]
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,...
const ChannelInformation ff_mlp_ch_info[21]
Tables defining channel information.
av_cold void ff_mlp_init_crc(void)
uint8_t ff_mlp_calculate_parity(const uint8_t *buf, unsigned int buf_size)
XOR together all the bytes of a buffer.
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...
#define NUM_FILTERS
number of allowed filters
#define MAX_SUBSTREAMS
Maximum number of substreams that can be decoded.
#define MAX_FIR_ORDER
The maximum number of taps in IIR and FIR filters.
static uint8_t xor_32_to_8(uint32_t value)
XOR four bytes into one.
static int layout_truehd(uint64_t layout)
#define SUBSTREAM_INFO_ALWAYS_SET
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.
static int mlp_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet)
#define MAX_NCHANNELS
MLP encoder Copyright (c) 2008 Ramiro Polla Copyright (c) 2016-2019 Jai Luthra.
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.
#define MAJOR_SYNC_INFO_SIGNATURE
#define MAX_HEADER_INTERVAL
static void write_major_sync(MLPEncodeContext *ctx, uint8_t *buf, int buf_size)
Writes a major sync header to the bitstream.
static void input_to_sample_buffer(MLPEncodeContext *ctx, MLPSubstream *s)
static void write_block_data(MLPEncodeContext *ctx, MLPSubstream *s, PutBitContext *pb, unsigned int subblock_index)
Writes the residuals to the bitstream.
static void generate_2_noise_channels(MLPEncodeContext *ctx, MLPSubstream *s)
Generates two noise channels worth of data.
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 ...
#define SUBSTREAM_INFO_MAX_2_CHAN
static void copy_filter_params(ChannelParams *dst_cp, ChannelParams *src_cp, int filter)
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 PARAM_PRESENCE_FLAGS
static void analyze_sample_buffer(MLPEncodeContext *ctx, MLPSubstream *s)
static void write_matrix_params(MLPEncodeContext *ctx, MLPSubstream *s, DecodingParams *dp, PutBitContext *pb)
Writes matrix params for all primitive matrices to the bitstream.
static av_cold int mlp_encode_close(AVCodecContext *avctx)
static const int8_t codebook_extremes[3][2]
Min and max values that can be encoded with each codebook.
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 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)
#define SAMPLE_MAX(bitdepth)
static void write_decoding_params(MLPEncodeContext *ctx, MLPSubstream *s, PutBitContext *pb, int params_changed, unsigned int subblock_index)
Writes decoding parameters to the bitstream.
static void set_best_codebook(MLPEncodeContext *ctx, MLPSubstream *s)
static void set_major_params(MLPEncodeContext *ctx, MLPSubstream *s)
Analyzes all collected bitcounts and selects the best parameters for each individual access unit.
static void default_decoding_params(MLPEncodeContext *ctx, DecodingParams *dp)
Sets default vales in our encoder for a DecodingParams struct.
static void clear_decoding_params(DecodingParams *decoding_params)
Clears a DecodingParams struct the way it should be after a restart header.
#define MLP_MAX_LPC_ORDER
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 DecodingParams restart_decoding_params[MAX_SUBSTREAMS]
static int mlp_peak_bitrate(int peak_bitrate, int sample_rate)
static uint32_t best_codebook_path_cost(MLPEncodeContext *ctx, MLPSubstream *s, int channel, PathCounter *src, int cur_codebook)
#define CODEBOOK_CHANGE_BITS
static void apply_filters(MLPEncodeContext *ctx, MLPSubstream *s)
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 MIN_HEADER_INTERVAL
static void determine_output_shift(MLPEncodeContext *ctx, MLPSubstream *s)
static int number_trailing_zeroes(int32_t sample, unsigned int max, unsigned int def)
Counts the number of trailing zeroes in a value.
static int compare_best_offset(const BestOffset *prev, const BestOffset *cur)
static void write_restart_header(MLPEncodeContext *ctx, MLPSubstream *s, PutBitContext *pb)
Writes a restart header to the bitstream.
static void lossless_matrix_coeffs(MLPEncodeContext *ctx, MLPSubstream *s)
Determines best coefficients to use for the lossless matrix.
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.
static av_cold int mlp_encode_init(AVCodecContext *avctx)
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 MLP_MIN_LPC_ORDER
static void rematrix_channels(MLPEncodeContext *ctx, MLPSubstream *s)
Rematrixes all channels using chosen coefficients.
static void process_major_frame(MLPEncodeContext *ctx, MLPSubstream *s)
#define SAMPLE_MIN(bitdepth)
static void determine_bits(MLPEncodeContext *ctx, MLPSubstream *s)
Determines the least amount of bits needed to encode the samples using any or no codebook.
static ChannelParams restart_channel_params[MAX_CHANNELS]
#define NUM_CODEBOOKS
Number of possible codebooks (counting "no codebooks")
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...
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.
static const AVClass mlp_class
static int estimate_coeff(MLPEncodeContext *ctx, MLPSubstream *s, MatrixParams *mp, int ch0, int ch1)
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 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...
#define MLP_MAX_LPC_SHIFT
static const BestOffset restart_best_offset[NUM_CODEBOOKS]
#define MLP_MIN_LPC_SHIFT
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 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...
static const AVOption mlp_options[]
static void copy_matrix_params(MatrixParams *dst, MatrixParams *src)
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 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.
static void copy_restart_frame_params(MLPEncodeContext *ctx, MLPSubstream *s)
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.
#define SUBSTREAM_INFO_HIGH_RATE
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.
static av_cold void mlp_encode_init_static(void)
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 ...
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.
static int put_bits_count(PutBitContext *s)
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static int put_bytes_output(const PutBitContext *s)
static av_unused void put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
#define FF_ARRAY_ELEMS(a)
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
main external API structure.
AVChannelLayout ch_layout
Audio channel layout.
enum AVSampleFormat sample_fmt
audio sample format
int64_t frame_num
Frame counter, set by libavcodec.
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
int sample_rate
samples per second
int frame_size
Number of samples per channel in an audio frame.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
int flags
A combination of AV_PKT_FLAG values.
sample data coding information
FilterParams filter_params[NUM_FILTERS]
uint8_t huff_lsbs
Size of residual suffix not encoded using VLC.
int32_t coeff[NUM_FILTERS][MAX_FIR_ORDER]
int16_t huff_offset
Offset to apply to residual values.
uint8_t codebook
Which VLC codebook to use to read residuals.
int32_t sample_buffer[MAX_NCHANNELS][MAX_BLOCKSIZE]
uint8_t quant_step_size[MAX_CHANNELS]
left shift to apply to Huffman-decoded residuals
uint16_t blocksize
number of PCM samples in current audio block
MatrixParams matrix_params
int8_t output_shift[MAX_CHANNELS]
Left shift to apply to decoded PCM values to get final 24-bit output.
uint8_t param_presence_flags
Bitmask of which parameter sets are conveyed in a decoding parameter block.
uint8_t max_order[MAX_CHANNELS]
uint8_t shift
Right shift to apply to output of filter.
uint8_t order
number of taps in filter
BestOffset best_offset[MAX_CHANNELS][NUM_CODEBOOKS]
DecodingParams decoding_params
unsigned int max_output_bits
largest output bit-depth
int32_t lossless_check_data
ChannelParams channel_params[MAX_CHANNELS]
ChannelParams major_channel_params[MAX_CHANNELS]
ChannelParams to be written to bitstream.
int major_params_changed
params_changed to be written to bitstream.
DecodingParams major_decoding_params
DecodingParams to be written to bitstream.
int32_t inout_buffer[MAX_NCHANNELS][MAX_BLOCKSIZE]
uint8_t ch2_presentation_mod
channel modifier for TrueHD stream 0
int coded_sample_rate[2]
sample rate encoded for MLP
int num_substreams
Number of substreams contained within this stream.
unsigned int number_of_frames
int coded_sample_fmt[2]
sample format encoded for MLP
uint16_t input_timing
Decoding timestamp of current access unit.
int32_t filter_state[NUM_FILTERS][MAX_HEADER_INTERVAL *MAX_BLOCKSIZE]
unsigned int frame_index
Index of current frame being encoded.
int max_restart_interval
Max interval of access units in between two major frames.
unsigned int major_frame_size
Number of samples in current major frame being encoded.
int32_t last_frames
Signal last frames.
int32_t lpc_sample_buffer[MAX_HEADER_INTERVAL *MAX_BLOCKSIZE]
uint16_t channel_arrangement8
8 channel arrangement for THD streams
unsigned int next_major_frame_size
Counter of number of samples for next major frame.
int min_restart_interval
Min interval of access units in between two major frames.
uint8_t ch6_presentation_mod
channel modifier for TrueHD stream 1
int coded_peak_bitrate
peak bitrate for this major sync header
uint8_t ch8_presentation_mod
channel modifier for TrueHD stream 2
unsigned int number_of_subblocks
uint16_t output_timing
Timestamp of current access unit.
uint8_t channel_arrangement
channel arrangement for MLP streams
unsigned int next_major_number_of_frames
uint8_t multichannel_type6ch
channel modifier for TrueHD stream 0
uint8_t multichannel_type8ch
channel modifier for TrueHD stream 0
unsigned int major_number_of_frames
int flags
major sync info flags
unsigned int restart_intervals
Number of possible major frame sizes.
int num_channels
Number of channels in major_scratch_buffer.
unsigned int major_filter_state_subblock
RestartHeader restart_header
RestartHeader * cur_restart_header
int32_t coefs[MAX_CHANNELS][MAX_LPC_ORDER][MAX_LPC_ORDER]
MLPBlock b[MAX_HEADER_INTERVAL+1]
unsigned int major_cur_subblock_index
uint8_t outch[MAX_MATRICES]
output channel for each matrix
int32_t coeff[MAX_MATRICES][MAX_NCHANNELS]
decoding coefficients
uint8_t lsb_bypass[MAX_MATRICES]
uint8_t fbits[MAX_MATRICES]
fraction bits
uint8_t count
number of matrices to apply
int8_t noise_shift[MAX_CHANNELS]
int32_t forco[MAX_MATRICES][MAX_NCHANNELS]
forward coefficients
int8_t bypassed_lsbs[MAX_MATRICES][MAX_BLOCKSIZE]
char path[MAX_HEADER_INTERVAL+2]
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
static const double coeff[2][5]