24 #include "libavutil/avassert.h"
25 #include "libavutil/channel_layout.h"
26 #include "libavutil/common.h"
34 #define MAX_CHANNELS 6
35 #define DCA_MAX_FRAME_SIZE 16384
36 #define DCA_HEADER_SIZE 13
37 #define DCA_LFE_SAMPLES 8
39 #define DCA_SUBBANDS 32
41 #define SUBSUBFRAMES 2
42 #define SUBBAND_SAMPLES (SUBFRAMES * SUBSUBFRAMES * 8)
86 static double hom(
double f)
90 return -3.64 * pow(f1, -0.8)
91 + 6.8 * exp(-0.6 * (f1 - 3.4) * (f1 - 3.4))
92 - 6.0 * exp(-0.15 * (f1 - 8.7) * (f1 - 8.7))
93 - 0.0006 * (f1 * f1) * (f1 * f1);
98 double h = (f -
fc[i]) /
erb[i];
102 return 20 * log10(h);
109 int i, min_frame_bits;
120 "encoder will guess the layout, but it "
121 "might be incorrect.\n");
138 for (i = 0; i < 9; i++) {
166 for (i = 0; i < 2048; i++) {
172 for (i = 0; i < 256; i++) {
177 for (i = 0; i < 512; i++) {
182 for (i = 0; i < 9; i++) {
183 for (j = 0; j <
AUBANDS; j++) {
184 for (k = 0; k < 256; k++) {
192 for (i = 0; i < 256; i++) {
193 double add = 1 + pow(10, -0.01 * i);
196 for (j = 0; j < 8; j++) {
198 for (i = 0; i < 512; i++) {
200 accum += reconst * cos(2 *
M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
204 for (j = 0; j < 8; j++) {
206 for (i = 0; i < 512; i++) {
208 accum += reconst * cos(2 *
M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
223 return cos_t(x - 512);
233 int64_t
r = (int64_t)a * b + 0x80000000ULL;
239 int ch, subs, i, k, j;
246 for (i = 0; i < 512; i++)
255 for (i = 0; i < 64; i++)
258 for (k = 0, i = hist_start, j = 0;
259 i < 512; k = (k + 1) & 63, i++, j++)
261 for (i = 0; i < hist_start; k = (k + 1) & 63, i++, j++)
264 for (k = 16; k < 32; k++)
265 accum[k] = accum[k] - accum[31 - k];
266 for (k = 32; k < 48; k++)
267 accum[k] = accum[k] + accum[95 - k];
269 for (band = 0; band < 32; band++) {
271 for (i = 16; i < 48; i++) {
272 int s = (2 * band + 1) * (2 * (i + 16) + 1);
276 c->
subband[subs][
band][ch] = ((band + 1) & 2) ? -resp : resp;
280 for (i = 0; i < 32; i++)
281 hist[i + hist_start] = input[(subs * 32 + i) * c->
channels + ch];
282 hist_start = (hist_start + 32) & 511;
295 for (i = 0; i < 512; i++)
302 for (i = hist_start, j = 0; i < 512; i++, j++)
304 for (i = 0; i < hist_start; i++, j++)
310 for (i = 0; i < 64; i++)
311 hist[i + hist_start] = input[(lfes * 64 + i) * c->
channels + c->
channels - 1];
313 hist_start = (hist_start + 64) & 511;
328 for (i = 0; i < 256; i++) {
330 rin[i].
re =
mul32(in[2 * i], 0x3fffffff - (
cos_t(8 * i + 2) >> 1));
331 rin[i].
im =
mul32(in[2 * i + 1], 0x3fffffff - (
cos_t(8 * i + 6) >> 1));
334 for (i = 0; i < 256; i++) {
341 for (j = 256, l = 1; j != 1; j >>= 1, l <<= 1) {
342 for (k = 0; k < 256; k += j) {
343 for (i = k; i < k + j / 2; i++) {
347 sum.
re = buf[i].
re + buf[i + j / 2].
re;
348 sum.
im = buf[i].
im + buf[i + j / 2].
im;
350 diff.
re = buf[i].
re - buf[i + j / 2].
re;
351 diff.
im = buf[i].
im - buf[i + j / 2].
im;
364 for (i = 0; i < 256; i++) {
371 for (i = 0; i < 256; i++) {
375 o1.
re = rout[i].
re - rout[255 - i].
re;
376 o1.
im = rout[i].
im + rout[255 - i].
im;
378 o2.
re = rout[i].
im - rout[255 - i].
im;
379 o2.
im = -rout[i].
re - rout[255 - i].
re;
396 for (i = 1024; i > 0; i >>= 1) {
426 for (j = 0; j < 256; j++) {
428 out_cb_unnorm[j] = -2047;
431 for (i = 0; i <
AUBANDS; i++) {
433 for (j = 0; j < 256; j++)
434 denom =
add_cb(denom, power[j] +
auf[samplerate_index][i][j]);
435 for (j = 0; j < 256; j++)
436 out_cb_unnorm[j] =
add_cb(out_cb_unnorm[j],
437 -denom +
auf[samplerate_index][i][j]);
440 for (j = 0; j < 256; j++)
441 out_cb[j] =
add_cb(out_cb[j], -out_cb_unnorm[j] - ca_cb - cs_cb);
454 for (f = 0; f < 4; f++)
455 walk(c, 0, 0, f, 0, -2047, channel, arg);
457 for (f = 0; f < 8; f++)
458 walk(c, band, band - 1, 8 * band - 4 + f,
469 for (f = 0; f < 4; f++)
470 walk(c, 31, 31, 256 - 4 + f, 0, -2047, channel, arg);
472 for (f = 0; f < 8; f++)
473 walk(c, band, band + 1, 8 * band + 4 + f,
484 if (value < c->band_masking_cb[band1])
490 int i, k,
band, ch, ssf;
493 for (i = 0; i < 256; i++)
499 for (i = 0, k = 128 + 256 * ssf; k < 512; i++, k++)
501 for (k -= 512; i < 512; i++, k++)
502 data[i] = input[k * c->
channels + ch];
505 for (i = 0; i < 256; i++) {
514 for (band = 0; band < 32; band++) {
525 for (band = 0; band < 32; band++)
550 #define USED_1ABITS 1
551 #define USED_NABITS 2
552 #define USED_26ABITS 4
564 for (band = 0; band < 32; band++) {
567 if (snr_cb >= 1312) {
570 }
else if (snr_cb >= 222) {
573 }
else if (snr_cb >= 0) {
583 for (band = 0; band < 32; band++)
617 for (down =
snr_fudge >> 1; down; down >>= 1) {
633 for (k = 0; k < 512; k++)
634 for (ch = 0; ch < c->
channels; ch++)
643 value = value >> quant.
e;
650 int our_nscale, try_remove;
659 for (try_remove = 64; try_remove > 0; try_remove >>= 1) {
666 our_nscale -= try_remove;
669 if (our_nscale >= 125)
683 for (band = 0; band < 32; band++)
687 &c->
quant[band][ch]);
698 for (band = 0; band < 32; band++)
788 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
789 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
824 for (i = 1; i < 11; i++)
834 if (c->
abits[band][ch] <= 7) {
836 for (i = 0; i < 8; i += 4) {
838 for (j = 3; j >= 0; j--) {
847 for (i = 0; i < 8; i++) {
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and/or allocate data.
int32_t band_masking_cb[32]
static void put_primary_audio_header(DCAContext *c)
#define AVERROR_PATCHWELCOME
This structure describes decoded (raw) audio or video data.
static int32_t cb_to_add[256]
static int32_t auf[9][AUBANDS][256]
int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]
scale factors (2 if transient)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
#define AV_LOG_WARNING
Something somehow does not look correct.
static av_cold int init(AVCodecContext *avctx)
static int32_t band_spectrum[2][8]
int32_t worst_quantization_noise
int32_t quantized[SUBBAND_SAMPLES][DCA_SUBBANDS][MAX_CHANNELS]
static const uint8_t bitstream_sfreq[]
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
int32_t subband[SUBBAND_SAMPLES][DCA_SUBBANDS][MAX_CHANNELS]
#define AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_5POINT0
static const int quant_levels[27]
if((e=av_dict_get(options,"", NULL, AV_DICT_IGNORE_SUFFIX)))
const int32_t * band_spectrum
static const softfloat stepsize_inv[27]
static int32_t lfe_fir_64i[512]
static const uint8_t offset[511][2]
static int32_t cos_table[2048]
static void quantize_all(DCAContext *c)
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
static int calc_one_scale(int32_t peak_cb, int abits, softfloat *quant)
const char * name
Name of the codec implementation.
static int init_quantization_noise(DCAContext *c, int noise)
static void subband_transform(DCAContext *c, const int32_t *input)
static int32_t sin_t(int x)
static const float fir_32bands_nonperfect[]
#define FFSWAP(type, a, b)
static void walk_band_high(DCAContext *c, int band, int channel, walk_band_t walk, int32_t *arg)
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
static void find_peaks(DCAContext *c)
#define AV_CH_LAYOUT_5POINT1
static int32_t add_cb(int32_t a, int32_t b)
static const softfloat scalefactor_inv[128]
static double hom(double f)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);ff_audio_convert_init_arm(ac);ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
static void assign_bits(DCAContext *c)
static const uint16_t mask[17]
static const int snr_fudge
static void lfe_downsample(DCAContext *c, const int32_t *input)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
int32_t masking_curve_cb[SUBSUBFRAMES][256]
Libavcodec external API header.
static int32_t quantize_value(int32_t value, softfloat quant)
static const int sample_rates[]
uint64_t channel_layout
Audio channel layout.
int bit_rate
the average bitrate
static void put_subframe(DCAContext *c, int subframe)
int32_t peak_cb[DCA_SUBBANDS][MAX_CHANNELS]
int32_t eff_masking_curve_cb[256]
#define DCA_MAX_FRAME_SIZE
int AC3_NAME() encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
static const float lfe_fir_64[]
int frame_size
Number of samples per channel in an audio frame.
AVSampleFormat
Audio Sample Formats.
static int noise(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int keyframe)
static int32_t mul32(int32_t a, int32_t b)
int abits[DCA_SUBBANDS][MAX_CHANNELS]
typedef void(RENAME(mix_any_func_type))
static int32_t cb_to_level[2048]
int sample_rate
samples per second
static void walk_band_low(DCAContext *c, int band, int channel, walk_band_t walk, int32_t *arg)
main external API structure.
const int32_t * band_interpolation
int32_t history[512][MAX_CHANNELS]
static void calc_scales(DCAContext *c)
static void adjust_jnd(int samplerate_index, const int32_t in[512], int32_t out_cb[256])
int32_t downsampled_lfe[DCA_LFE_SAMPLES]
static void shift_history(DCAContext *c, const int32_t *input)
static int encode_init(AVCodecContext *avctx)
static void put_subframe_samples(DCAContext *c, int ss, int band, int ch)
static int32_t band_interpolation[2][512]
static int32_t cos_t(int x)
static const float fir_32bands_perfect[]
static void put_frame_header(DCAContext *c)
static void fft(const int32_t in[2 *256], cplx32 out[256])
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
void(* walk_band_t)(DCAContext *c, int band1, int band2, int f, int32_t spectrum1, int32_t spectrum2, int channel, int32_t *arg)
static int32_t get_cb(int32_t in)
#define CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static void update_band_masking(DCAContext *c, int band1, int band2, int f, int32_t spectrum1, int32_t spectrum2, int channel, int32_t *arg)
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);ff_audio_convert_init_arm(ac);ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
static const uint32_t dca_bit_rates[32]
int channels
number of audio channels
static void calc_masking(DCAContext *c, const int32_t *input)
static int32_t half32(int32_t a)
int frame_size
primary frame byte size
static enum AVSampleFormat sample_fmts[]
const uint8_t ff_reverse[256]
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static av_always_inline int64_t ff_samples_to_time_base(AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
softfloat quant[DCA_SUBBANDS][MAX_CHANNELS]
static const AVCodecDefault defaults[]
static const int bit_consumption[27]
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
static double gammafilter(int i, double f)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
int64_t av_get_default_channel_layout(int nb_channels)
Return default channel layout for a given number of channels.