2016-08-30 12:23:02 +02:00
/**
* MLP encoder
* Copyright ( c ) 2008 Ramiro Polla
2020-01-24 12:33:32 +02:00
* Copyright ( c ) 2016 - 2019 Jai Luthra
2016-08-30 12:23:02 +02:00
*
* This file is part of FFmpeg .
*
* FFmpeg is free software ; you can redistribute it and / or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation ; either
* version 2.1 of the License , or ( at your option ) any later version .
*
* FFmpeg is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* Lesser General Public License for more details .
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
*/
2022-02-23 14:56:49 +02:00
# include "config_components.h"
2016-08-30 12:23:02 +02:00
# include "avcodec.h"
2022-03-16 19:18:28 +02:00
# include "codec_internal.h"
2021-05-11 15:17:13 +02:00
# include "encode.h"
2016-08-30 12:23:02 +02:00
# include "put_bits.h"
# include "audio_frame_queue.h"
2022-09-17 23:49:57 +02:00
# include "libavutil/avassert.h"
2021-06-12 01:10:58 +02:00
# include "libavutil/channel_layout.h"
2016-08-30 12:23:02 +02:00
# include "libavutil/crc.h"
# include "libavutil/avstring.h"
2021-09-05 00:06:00 +02:00
# include "libavutil/intmath.h"
2023-10-02 20:02:18 +02:00
# include "libavutil/opt.h"
2016-08-30 12:23:02 +02:00
# include "libavutil/samplefmt.h"
2020-12-03 02:06:22 +02:00
# include "libavutil/thread.h"
2023-10-19 22:11:33 +02:00
# include "mlp_parse.h"
2016-08-30 12:23:02 +02:00
# include "mlp.h"
# include "lpc.h"
2023-10-13 12:23:34 +02:00
# define MAX_NCHANNELS (MAX_CHANNELS + 2)
2023-10-09 23:40:23 +02:00
# define MIN_HEADER_INTERVAL 8
# define MAX_HEADER_INTERVAL 128
2016-08-30 12:23:02 +02:00
# define MLP_MIN_LPC_ORDER 1
# define MLP_MAX_LPC_ORDER 8
2023-10-18 14:40:42 +02:00
# define MLP_MIN_LPC_SHIFT 0
2016-08-30 12:23:02 +02:00
# define MLP_MAX_LPC_SHIFT 15
2023-10-03 12:43:14 +02:00
typedef struct RestartHeader {
2016-08-30 12:23:02 +02:00
uint8_t min_channel ; ///< The index of the first channel coded in this substream.
uint8_t max_channel ; ///< The index of the last channel coded in this substream.
uint8_t max_matrix_channel ; ///< The number of channels input into the rematrix stage.
2023-10-20 16:28:08 +02:00
int8_t max_shift ;
2016-08-30 12:23:02 +02:00
uint8_t noise_shift ; ///< The left shift applied to random noise in 0x31ea substreams.
uint32_t noisegen_seed ; ///< The current seed value for the pseudorandom noise generator(s).
2023-10-03 12:43:14 +02:00
uint8_t data_check_present ; ///< Set if the substream contains extra info to check the size of VLC blocks.
2016-08-30 12:23:02 +02:00
int32_t lossless_check_data ; ///< XOR of all output samples
uint8_t max_huff_lsbs ; ///< largest huff_lsbs
uint8_t max_output_bits ; ///< largest output bit-depth
} RestartHeader ;
2023-10-03 12:43:14 +02:00
typedef struct MatrixParams {
2016-08-30 12:23:02 +02:00
uint8_t count ; ///< number of matrices to apply
uint8_t outch [ MAX_MATRICES ] ; ///< output channel for each matrix
2023-10-13 12:23:34 +02:00
int32_t forco [ MAX_MATRICES ] [ MAX_NCHANNELS ] ; ///< forward coefficients
int32_t coeff [ MAX_MATRICES ] [ MAX_NCHANNELS ] ; ///< decoding coefficients
2023-10-19 23:36:19 +02:00
uint8_t fbits [ MAX_MATRICES ] ; ///< fraction bits
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
int8_t noise_shift [ MAX_CHANNELS ] ;
2023-10-13 12:23:34 +02:00
uint8_t lsb_bypass [ MAX_MATRICES ] ;
int8_t bypassed_lsbs [ MAX_MATRICES ] [ MAX_BLOCKSIZE ] ;
2016-08-30 12:23:02 +02:00
} MatrixParams ;
2023-10-18 22:56:52 +02:00
# define PARAMS_DEFAULT (0xff)
# define PARAM_PRESENCE_FLAGS (1 << 8)
2016-08-30 12:23:02 +02:00
2023-10-03 12:43:14 +02:00
typedef struct DecodingParams {
2016-08-30 12:23:02 +02:00
uint16_t blocksize ; ///< number of PCM samples in current audio block
uint8_t quant_step_size [ MAX_CHANNELS ] ; ///< left shift to apply to Huffman-decoded residuals
2023-10-20 16:28:08 +02:00
int8_t output_shift [ MAX_CHANNELS ] ; ///< Left shift to apply to decoded PCM values to get final 24-bit output.
2023-10-25 15:32:53 +02:00
uint8_t max_order [ MAX_CHANNELS ] ;
2016-08-30 12:23:02 +02:00
MatrixParams matrix_params ;
uint8_t param_presence_flags ; ///< Bitmask of which parameter sets are conveyed in a decoding parameter block.
2023-10-13 12:23:34 +02:00
int32_t sample_buffer [ MAX_NCHANNELS ] [ MAX_BLOCKSIZE ] ;
2016-08-30 12:23:02 +02:00
} DecodingParams ;
typedef struct BestOffset {
2020-02-02 21:02:59 +02:00
int32_t offset ;
2023-10-03 12:43:14 +02:00
uint32_t bitcount ;
uint8_t lsb_bits ;
2020-02-02 21:02:59 +02:00
int32_t min ;
int32_t max ;
2016-08-30 12:23:02 +02:00
} BestOffset ;
2020-01-24 12:33:34 +02:00
# define HUFF_OFFSET_MIN (-16384)
# define HUFF_OFFSET_MAX ( 16383)
2016-08-30 12:23:02 +02:00
/** Number of possible codebooks (counting "no codebooks") */
# define NUM_CODEBOOKS 4
2023-10-03 12:43:14 +02:00
typedef struct MLPBlock {
unsigned int seq_size ;
2023-10-13 12:23:34 +02:00
ChannelParams channel_params [ MAX_CHANNELS ] ;
2023-10-03 12:43:14 +02:00
DecodingParams decoding_params ;
2023-10-19 23:36:19 +02:00
int32_t lossless_check_data ;
2023-10-03 12:43:14 +02:00
unsigned int max_output_bits ; ///< largest output bit-depth
BestOffset best_offset [ MAX_CHANNELS ] [ NUM_CODEBOOKS ] ;
ChannelParams major_channel_params [ MAX_CHANNELS ] ; ///< ChannelParams to be written to bitstream.
DecodingParams major_decoding_params ; ///< DecodingParams to be written to bitstream.
int major_params_changed ; ///< params_changed to be written to bitstream.
2023-10-13 12:23:34 +02:00
int32_t inout_buffer [ MAX_NCHANNELS ] [ MAX_BLOCKSIZE ] ;
2023-10-03 12:43:14 +02:00
} MLPBlock ;
2023-10-19 23:36:19 +02:00
typedef struct MLPSubstream {
RestartHeader restart_header ;
RestartHeader * cur_restart_header ;
MLPBlock b [ MAX_HEADER_INTERVAL + 1 ] ;
unsigned int major_cur_subblock_index ;
unsigned int major_filter_state_subblock ;
2023-10-25 15:32:53 +02:00
int32_t coefs [ MAX_CHANNELS ] [ MAX_LPC_ORDER ] [ MAX_LPC_ORDER ] ;
2023-10-19 23:36:19 +02:00
} MLPSubstream ;
2021-08-30 22:27:33 +02:00
typedef struct MLPEncodeContext {
2023-10-02 20:02:18 +02:00
AVClass * class ;
2016-08-30 12:23:02 +02:00
AVCodecContext * avctx ;
2023-10-09 23:40:23 +02:00
int max_restart_interval ; ///< Max interval of access units in between two major frames.
int min_restart_interval ; ///< Min interval of access units in between two major frames.
2023-10-13 12:23:34 +02:00
int cur_restart_interval ;
2023-10-10 13:44:03 +02:00
int lpc_coeff_precision ;
2023-10-16 12:42:30 +02:00
int rematrix_precision ;
2023-10-02 20:02:18 +02:00
int lpc_type ;
int lpc_passes ;
int prediction_order ;
2023-10-09 23:13:45 +02:00
int max_codebook_search ;
2023-10-02 20:02:18 +02:00
2016-08-30 12:23:02 +02:00
int num_substreams ; ///< Number of substreams contained within this stream.
int num_channels ; /**< Number of channels in major_scratch_buffer.
* Normal channels + noise channels . */
int coded_sample_fmt [ 2 ] ; ///< sample format encoded for MLP
int coded_sample_rate [ 2 ] ; ///< sample rate encoded for MLP
int coded_peak_bitrate ; ///< peak bitrate for this major sync header
int flags ; ///< major sync info flags
/* channel_meaning */
int substream_info ;
2022-09-18 14:44:27 +02:00
int thd_substream_info ;
2016-08-30 12:23:02 +02:00
int fs ;
int wordlength ;
int channel_occupancy ;
int summary_info ;
2021-09-05 11:38:01 +02:00
int32_t last_frames ; ///< Signal last frames.
2016-08-30 12:23:02 +02:00
unsigned int major_number_of_frames ;
unsigned int next_major_number_of_frames ;
unsigned int major_frame_size ; ///< Number of samples in current major frame being encoded.
unsigned int next_major_frame_size ; ///< Counter of number of samples for next major frame.
unsigned int frame_index ; ///< Index of current frame being encoded.
unsigned int restart_intervals ; ///< Number of possible major frame sizes.
2022-09-17 16:28:05 +02:00
uint16_t output_timing ; ///< Timestamp of current access unit.
uint16_t input_timing ; ///< Decoding timestamp of current access unit.
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
uint8_t noise_type ;
2016-08-30 12:23:02 +02:00
uint8_t channel_arrangement ; ///< channel arrangement for MLP streams
2023-10-25 12:46:24 +02:00
uint16_t channel_arrangement8 ; ///< 8 channel arrangement for THD streams
2016-08-30 12:23:02 +02:00
2023-10-06 22:15:33 +02:00
uint8_t multichannel_type6ch ; ///< channel modifier for TrueHD stream 0
uint8_t multichannel_type8ch ; ///< channel modifier for TrueHD stream 0
uint8_t ch2_presentation_mod ; ///< channel modifier for TrueHD stream 0
uint8_t ch6_presentation_mod ; ///< channel modifier for TrueHD stream 1
uint8_t ch8_presentation_mod ; ///< channel modifier for TrueHD stream 2
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
MLPSubstream s [ 2 ] ;
2023-10-13 12:23:34 +02:00
int32_t filter_state [ NUM_FILTERS ] [ MAX_HEADER_INTERVAL * MAX_BLOCKSIZE ] ;
2023-10-09 23:40:23 +02:00
int32_t lpc_sample_buffer [ MAX_HEADER_INTERVAL * MAX_BLOCKSIZE ] ;
2016-08-30 12:23:02 +02:00
AudioFrameQueue afq ;
/* Analysis stage. */
unsigned int number_of_frames ;
unsigned int number_of_subblocks ;
2021-08-30 22:27:33 +02:00
int shorten_by ;
2016-08-30 12:23:02 +02:00
LPCContext lpc_ctx ;
} MLPEncodeContext ;
static ChannelParams restart_channel_params [ MAX_CHANNELS ] ;
static DecodingParams restart_decoding_params [ MAX_SUBSTREAMS ] ;
2020-12-03 01:33:27 +02:00
static const BestOffset restart_best_offset [ NUM_CODEBOOKS ] = { { 0 } } ;
2016-08-30 12:23:02 +02:00
# define SYNC_MAJOR 0xf8726f
# define MAJOR_SYNC_INFO_SIGNATURE 0xB752
/* must be set for DVD-A */
# define FLAGS_DVDA 0x4000
/* FIFO delay must be constant */
# define FLAGS_CONST 0x8000
# define SUBSTREAM_INFO_MAX_2_CHAN 0x01
# define SUBSTREAM_INFO_HIGH_RATE 0x02
# define SUBSTREAM_INFO_ALWAYS_SET 0x04
# define SUBSTREAM_INFO_2_SUBSTREAMS 0x08
/****************************************************************************
* * * * * * * * * * * * Functions that copy , clear , or compare parameters * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/** Compares two FilterParams structures and returns 1 if anything has
* changed . Returns 0 if they are both equal .
*/
static int compare_filter_params ( const ChannelParams * prev_cp , const ChannelParams * cp , int filter )
{
const FilterParams * prev = & prev_cp - > filter_params [ filter ] ;
const FilterParams * fp = & cp - > filter_params [ filter ] ;
if ( prev - > order ! = fp - > order )
return 1 ;
2023-10-10 13:44:03 +02:00
if ( ! fp - > order )
2016-08-30 12:23:02 +02:00
return 0 ;
if ( prev - > shift ! = fp - > shift )
return 1 ;
2021-09-06 23:12:00 +02:00
for ( int i = 0 ; i < fp - > order ; i + + )
2016-08-30 12:23:02 +02:00
if ( prev_cp - > coeff [ filter ] [ i ] ! = cp - > coeff [ filter ] [ i ] )
return 1 ;
return 0 ;
}
/** Compare two primitive matrices and returns 1 if anything has changed.
* Returns 0 if they are both equal .
*/
2023-10-19 23:36:19 +02:00
static int compare_matrix_params ( MLPEncodeContext * ctx , MLPSubstream * s ,
const MatrixParams * prev , const MatrixParams * mp )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
RestartHeader * rh = s - > cur_restart_header ;
2016-08-30 12:23:02 +02:00
if ( prev - > count ! = mp - > count )
return 1 ;
2023-10-10 13:44:03 +02:00
if ( ! mp - > count )
2016-08-30 12:23:02 +02:00
return 0 ;
2021-09-06 23:12:00 +02:00
for ( unsigned int mat = 0 ; mat < mp - > count ; mat + + ) {
2016-08-30 12:23:02 +02:00
if ( prev - > outch [ mat ] ! = mp - > outch [ mat ] )
return 1 ;
2023-10-19 23:36:19 +02:00
if ( prev - > fbits [ mat ] ! = mp - > fbits [ mat ] )
return 1 ;
if ( prev - > noise_shift [ mat ] ! = mp - > noise_shift [ mat ] )
return 1 ;
2023-10-13 12:23:34 +02:00
if ( prev - > lsb_bypass [ mat ] ! = mp - > lsb_bypass [ mat ] )
return 1 ;
2023-10-19 23:36:19 +02:00
for ( int ch = 0 ; ch < = rh - > max_matrix_channel ; ch + + )
2023-10-16 12:42:30 +02:00
if ( prev - > coeff [ mat ] [ ch ] ! = mp - > coeff [ mat ] [ ch ] )
2016-08-30 12:23:02 +02:00
return 1 ;
}
return 0 ;
}
/** Compares two DecodingParams and ChannelParams structures to decide if a
* new decoding params header has to be written .
*/
2023-10-13 12:23:34 +02:00
static int compare_decoding_params ( MLPEncodeContext * ctx ,
2023-10-19 23:36:19 +02:00
MLPSubstream * s ,
2023-10-13 12:23:34 +02:00
unsigned int index )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
const DecodingParams * prev = index ? & s - > b [ index - 1 ] . major_decoding_params : restart_decoding_params ;
DecodingParams * dp = & s - > b [ index ] . major_decoding_params ;
2020-12-03 01:33:27 +02:00
const MatrixParams * prev_mp = & prev - > matrix_params ;
2016-08-30 12:23:02 +02:00
MatrixParams * mp = & dp - > matrix_params ;
2023-10-19 23:36:19 +02:00
RestartHeader * rh = s - > cur_restart_header ;
2016-08-30 12:23:02 +02:00
int retval = 0 ;
if ( prev - > param_presence_flags ! = dp - > param_presence_flags )
retval | = PARAM_PRESENCE_FLAGS ;
if ( prev - > blocksize ! = dp - > blocksize )
retval | = PARAM_BLOCKSIZE ;
2023-10-19 23:36:19 +02:00
if ( compare_matrix_params ( ctx , s , prev_mp , mp ) )
2016-08-30 12:23:02 +02:00
retval | = PARAM_MATRIX ;
2023-10-19 23:36:19 +02:00
for ( int ch = 0 ; ch < = rh - > max_matrix_channel ; ch + + )
if ( prev - > output_shift [ ch ] ! = dp - > output_shift [ ch ] ) {
2016-08-30 12:23:02 +02:00
retval | = PARAM_OUTSHIFT ;
break ;
}
2023-10-19 23:36:19 +02:00
for ( int ch = 0 ; ch < = rh - > max_channel ; ch + + )
2016-08-30 12:23:02 +02:00
if ( prev - > quant_step_size [ ch ] ! = dp - > quant_step_size [ ch ] ) {
retval | = PARAM_QUANTSTEP ;
break ;
}
2023-10-19 23:36:19 +02:00
for ( int ch = rh - > min_channel ; ch < = rh - > max_channel ; ch + + ) {
const ChannelParams * prev_cp = index ? & s - > b [ index - 1 ] . major_channel_params [ ch ] : & restart_channel_params [ ch ] ;
ChannelParams * cp = & s - > b [ index ] . major_channel_params [ ch ] ;
2016-08-30 12:23:02 +02:00
if ( ! ( retval & PARAM_FIR ) & &
compare_filter_params ( prev_cp , cp , FIR ) )
retval | = PARAM_FIR ;
if ( ! ( retval & PARAM_IIR ) & &
compare_filter_params ( prev_cp , cp , IIR ) )
retval | = PARAM_IIR ;
if ( prev_cp - > huff_offset ! = cp - > huff_offset )
retval | = PARAM_HUFFOFFSET ;
if ( prev_cp - > codebook ! = cp - > codebook | |
prev_cp - > huff_lsbs ! = cp - > huff_lsbs )
2023-10-18 22:56:52 +02:00
retval | = PARAM_PRESENCE ;
2016-08-30 12:23:02 +02:00
}
return retval ;
}
static void copy_filter_params ( ChannelParams * dst_cp , ChannelParams * src_cp , int filter )
{
FilterParams * dst = & dst_cp - > filter_params [ filter ] ;
FilterParams * src = & src_cp - > filter_params [ filter ] ;
dst - > order = src - > order ;
if ( dst - > order ) {
dst - > shift = src - > shift ;
dst - > coeff_shift = src - > coeff_shift ;
dst - > coeff_bits = src - > coeff_bits ;
}
2023-10-19 23:36:19 +02:00
for ( int order = 0 ; order < dst - > order ; order + + )
2016-08-30 12:23:02 +02:00
dst_cp - > coeff [ filter ] [ order ] = src_cp - > coeff [ filter ] [ order ] ;
}
static void copy_matrix_params ( MatrixParams * dst , MatrixParams * src )
{
dst - > count = src - > count ;
2023-10-19 23:36:19 +02:00
if ( ! dst - > count )
return ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
for ( int count = 0 ; count < MAX_MATRICES ; count + + ) {
dst - > outch [ count ] = src - > outch [ count ] ;
dst - > fbits [ count ] = src - > fbits [ count ] ;
dst - > noise_shift [ count ] = src - > noise_shift [ count ] ;
dst - > lsb_bypass [ count ] = src - > lsb_bypass [ count ] ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
for ( int channel = 0 ; channel < MAX_NCHANNELS ; channel + + )
dst - > coeff [ count ] [ channel ] = src - > coeff [ count ] [ channel ] ;
2016-08-30 12:23:02 +02:00
}
}
2023-10-19 23:36:19 +02:00
static void copy_restart_frame_params ( MLPEncodeContext * ctx , MLPSubstream * s )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
RestartHeader * rh = s - > cur_restart_header ;
2021-09-06 23:12:00 +02:00
for ( unsigned int index = 0 ; index < ctx - > number_of_subblocks ; index + + ) {
2023-10-19 23:36:19 +02:00
DecodingParams * dp = & s - > b [ index ] . decoding_params ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
copy_matrix_params ( & dp - > matrix_params , & s - > b [ 1 ] . decoding_params . matrix_params ) ;
2016-08-30 12:23:02 +02:00
2023-10-20 16:28:08 +02:00
for ( int ch = 0 ; ch < = rh - > max_matrix_channel ; ch + + )
dp - > output_shift [ ch ] = s - > b [ 1 ] . decoding_params . output_shift [ ch ] ;
2023-10-19 23:36:19 +02:00
for ( int ch = 0 ; ch < = rh - > max_channel ; ch + + ) {
ChannelParams * cp = & s - > b [ index ] . channel_params [ ch ] ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
dp - > quant_step_size [ ch ] = s - > b [ 1 ] . decoding_params . quant_step_size [ ch ] ;
2016-08-30 12:23:02 +02:00
if ( index )
2021-09-06 23:12:00 +02:00
for ( unsigned int filter = 0 ; filter < NUM_FILTERS ; filter + + )
2023-10-19 23:36:19 +02:00
copy_filter_params ( cp , & s - > b [ 1 ] . channel_params [ ch ] , filter ) ;
2016-08-30 12:23:02 +02:00
}
}
}
/** Clears a DecodingParams struct the way it should be after a restart header. */
2021-09-06 23:46:23 +02:00
static void clear_decoding_params ( DecodingParams * decoding_params )
2016-08-30 12:23:02 +02:00
{
2021-09-06 23:46:23 +02:00
DecodingParams * dp = decoding_params ;
2016-08-30 12:23:02 +02:00
2021-09-06 23:46:23 +02:00
dp - > param_presence_flags = 0xff ;
2023-10-13 12:23:34 +02:00
dp - > blocksize = 0 ;
2016-08-30 12:23:02 +02:00
2023-10-13 12:23:34 +02:00
memset ( & dp - > matrix_params , 0 , sizeof ( dp - > matrix_params ) ) ;
2021-09-06 23:46:23 +02:00
memset ( dp - > quant_step_size , 0 , sizeof ( dp - > quant_step_size ) ) ;
2023-10-13 12:23:34 +02:00
memset ( dp - > sample_buffer , 0 , sizeof ( dp - > sample_buffer ) ) ;
2023-10-19 23:36:19 +02:00
memset ( dp - > output_shift , 0 , sizeof ( dp - > output_shift ) ) ;
2023-10-25 15:32:53 +02:00
memset ( dp - > max_order , MAX_FIR_ORDER , sizeof ( dp - > max_order ) ) ;
2016-08-30 12:23:02 +02:00
}
/** Clears a ChannelParams struct the way it should be after a restart header. */
2023-10-03 12:43:14 +02:00
static void clear_channel_params ( ChannelParams * channel_params , int nb_channels )
2016-08-30 12:23:02 +02:00
{
2020-12-03 02:06:22 +02:00
for ( unsigned channel = 0 ; channel < nb_channels ; channel + + ) {
2016-08-30 12:23:02 +02:00
ChannelParams * cp = & channel_params [ channel ] ;
memset ( & cp - > filter_params , 0 , sizeof ( cp - > filter_params ) ) ;
/* Default audio coding is 24-bit raw PCM. */
cp - > huff_offset = 0 ;
cp - > codebook = 0 ;
cp - > huff_lsbs = 24 ;
}
}
/** Sets default vales in our encoder for a DecodingParams struct. */
2023-10-13 12:23:34 +02:00
static void default_decoding_params ( MLPEncodeContext * ctx , DecodingParams * dp )
2016-08-30 12:23:02 +02:00
{
2021-09-06 23:46:23 +02:00
uint8_t param_presence_flags = 0 ;
2016-08-30 12:23:02 +02:00
2023-10-13 12:23:34 +02:00
clear_decoding_params ( dp ) ;
2021-09-06 23:46:23 +02:00
param_presence_flags | = PARAM_BLOCKSIZE ;
param_presence_flags | = PARAM_MATRIX ;
param_presence_flags | = PARAM_OUTSHIFT ;
param_presence_flags | = PARAM_QUANTSTEP ;
param_presence_flags | = PARAM_FIR ;
2023-10-18 20:28:04 +02:00
param_presence_flags | = PARAM_IIR ;
2021-09-06 23:46:23 +02:00
param_presence_flags | = PARAM_HUFFOFFSET ;
2023-10-18 22:56:52 +02:00
param_presence_flags | = PARAM_PRESENCE ;
2021-09-06 23:46:23 +02:00
dp - > param_presence_flags = param_presence_flags ;
2016-08-30 12:23:02 +02:00
}
/****************************************************************************/
/** Calculates the smallest number of bits it takes to encode a given signed
* value in two ' s complement .
*/
2023-10-03 12:43:14 +02:00
static int inline number_sbits ( int32_t n )
2016-08-30 12:23:02 +02:00
{
2023-10-10 10:23:06 +02:00
return 33 - ff_clz ( FFABS ( n ) | 1 ) - ! n ;
2016-08-30 12:23:02 +02:00
}
enum InputBitDepth {
BITS_16 ,
BITS_20 ,
BITS_24 ,
} ;
static int mlp_peak_bitrate ( int peak_bitrate , int sample_rate )
{
return ( ( peak_bitrate < < 4 ) - 8 ) / sample_rate ;
}
2020-12-03 02:06:22 +02:00
static av_cold void mlp_encode_init_static ( void )
{
clear_channel_params ( restart_channel_params , MAX_CHANNELS ) ;
2021-09-06 23:46:23 +02:00
clear_decoding_params ( restart_decoding_params ) ;
2020-12-03 02:06:22 +02:00
ff_mlp_init_crc ( ) ;
}
2016-08-30 12:23:02 +02:00
static av_cold int mlp_encode_init ( AVCodecContext * avctx )
{
2020-12-03 02:06:22 +02:00
static AVOnce init_static_once = AV_ONCE_INIT ;
2016-08-30 12:23:02 +02:00
MLPEncodeContext * ctx = avctx - > priv_data ;
2022-09-17 23:13:05 +02:00
uint64_t channels_present ;
2016-08-30 12:23:02 +02:00
int ret ;
ctx - > avctx = avctx ;
switch ( avctx - > sample_rate ) {
case 44100 < < 0 :
avctx - > frame_size = 40 < < 0 ;
ctx - > coded_sample_rate [ 0 ] = 0x08 + 0 ;
ctx - > fs = 0x08 + 1 ;
break ;
case 44100 < < 1 :
avctx - > frame_size = 40 < < 1 ;
ctx - > coded_sample_rate [ 0 ] = 0x08 + 1 ;
ctx - > fs = 0x0C + 1 ;
break ;
case 44100 < < 2 :
ctx - > substream_info | = SUBSTREAM_INFO_HIGH_RATE ;
avctx - > frame_size = 40 < < 2 ;
ctx - > coded_sample_rate [ 0 ] = 0x08 + 2 ;
ctx - > fs = 0x10 + 1 ;
break ;
case 48000 < < 0 :
avctx - > frame_size = 40 < < 0 ;
ctx - > coded_sample_rate [ 0 ] = 0x00 + 0 ;
ctx - > fs = 0x08 + 2 ;
break ;
case 48000 < < 1 :
avctx - > frame_size = 40 < < 1 ;
ctx - > coded_sample_rate [ 0 ] = 0x00 + 1 ;
ctx - > fs = 0x0C + 2 ;
break ;
case 48000 < < 2 :
ctx - > substream_info | = SUBSTREAM_INFO_HIGH_RATE ;
avctx - > frame_size = 40 < < 2 ;
ctx - > coded_sample_rate [ 0 ] = 0x00 + 2 ;
ctx - > fs = 0x10 + 2 ;
break ;
default :
av_log ( avctx , AV_LOG_ERROR , " Unsupported sample rate %d. Supported "
" sample rates are 44100, 88200, 176400, 48000, "
" 96000, and 192000. \n " , avctx - > sample_rate ) ;
2020-06-24 05:34:20 +02:00
return AVERROR ( EINVAL ) ;
2016-08-30 12:23:02 +02:00
}
ctx - > coded_sample_rate [ 1 ] = - 1 & 0xf ;
ctx - > coded_peak_bitrate = mlp_peak_bitrate ( 9600000 , avctx - > sample_rate ) ;
ctx - > substream_info | = SUBSTREAM_INFO_ALWAYS_SET ;
2022-09-18 14:44:27 +02:00
if ( avctx - > ch_layout . nb_channels < = 2 )
2016-08-30 12:23:02 +02:00
ctx - > substream_info | = SUBSTREAM_INFO_MAX_2_CHAN ;
switch ( avctx - > sample_fmt ) {
2023-10-19 23:36:19 +02:00
case AV_SAMPLE_FMT_S16P :
2016-08-30 12:23:02 +02:00
ctx - > coded_sample_fmt [ 0 ] = BITS_16 ;
ctx - > wordlength = 16 ;
avctx - > bits_per_raw_sample = 16 ;
break ;
/* TODO 20 bits: */
2023-10-19 23:36:19 +02:00
case AV_SAMPLE_FMT_S32P :
2016-08-30 12:23:02 +02:00
ctx - > coded_sample_fmt [ 0 ] = BITS_24 ;
ctx - > wordlength = 24 ;
avctx - > bits_per_raw_sample = 24 ;
break ;
default :
av_log ( avctx , AV_LOG_ERROR , " Sample format not supported. "
" Only 16- and 24-bit samples are supported. \n " ) ;
2020-06-24 05:34:20 +02:00
return AVERROR ( EINVAL ) ;
2016-08-30 12:23:02 +02:00
}
ctx - > coded_sample_fmt [ 1 ] = - 1 & 0xf ;
2022-09-17 16:28:05 +02:00
ctx - > input_timing = - avctx - > frame_size ;
2016-08-30 12:23:02 +02:00
2019-05-29 09:36:27 +02:00
ctx - > num_channels = avctx - > ch_layout . nb_channels + 2 ; /* +2 noise channels */
2016-08-30 12:23:02 +02:00
2023-10-13 12:23:34 +02:00
ctx - > min_restart_interval = ctx - > cur_restart_interval = ctx - > max_restart_interval ;
2016-08-30 12:23:02 +02:00
ctx - > restart_intervals = ctx - > max_restart_interval / ctx - > min_restart_interval ;
2023-10-19 23:36:19 +02:00
ctx - > num_substreams = 1 ;
2016-08-30 12:23:02 +02:00
2022-09-17 23:13:05 +02:00
channels_present = av_channel_layout_subset ( & avctx - > ch_layout , ~ ( uint64_t ) 0 ) ;
2016-08-30 12:23:02 +02:00
if ( ctx - > avctx - > codec_id = = AV_CODEC_ID_MLP ) {
2022-09-17 23:13:05 +02:00
static const uint64_t layout_arrangement [ ] = {
AV_CH_LAYOUT_MONO , AV_CH_LAYOUT_STEREO ,
AV_CH_LAYOUT_2_1 , AV_CH_LAYOUT_QUAD ,
AV_CH_LAYOUT_2POINT1 , 0 , 0 ,
AV_CH_LAYOUT_SURROUND , AV_CH_LAYOUT_4POINT0 ,
AV_CH_LAYOUT_5POINT0_BACK , AV_CH_LAYOUT_3POINT1 ,
AV_CH_LAYOUT_4POINT1 , AV_CH_LAYOUT_5POINT1_BACK ,
2019-05-29 09:36:27 +02:00
} ;
int i ;
2022-09-17 23:49:57 +02:00
for ( i = 0 ; ; i + + ) {
av_assert1 ( i < FF_ARRAY_ELEMS ( layout_arrangement ) | |
! " Impossible channel layout " ) ;
2022-09-17 23:13:05 +02:00
if ( channels_present = = layout_arrangement [ i ] )
2019-05-29 09:36:27 +02:00
break ;
2016-08-30 12:23:02 +02:00
}
2019-05-29 09:36:27 +02:00
ctx - > channel_arrangement = i ;
2016-08-30 12:23:02 +02:00
ctx - > flags = FLAGS_DVDA ;
ctx - > channel_occupancy = ff_mlp_ch_info [ ctx - > channel_arrangement ] . channel_occupancy ;
ctx - > summary_info = ff_mlp_ch_info [ ctx - > channel_arrangement ] . summary_info ;
} else {
/* TrueHD */
2023-10-19 23:36:19 +02:00
ctx - > num_substreams = 1 + ( avctx - > ch_layout . nb_channels > 2 ) ;
switch ( channels_present ) {
case AV_CH_LAYOUT_MONO :
2023-10-06 22:15:33 +02:00
ctx - > ch2_presentation_mod = 3 ;
ctx - > ch6_presentation_mod = 3 ;
ctx - > ch8_presentation_mod = 3 ;
2022-09-18 14:44:27 +02:00
ctx - > thd_substream_info = 0x14 ;
2023-10-19 23:36:19 +02:00
break ;
case AV_CH_LAYOUT_STEREO :
2023-10-06 22:15:33 +02:00
ctx - > ch2_presentation_mod = 1 ;
ctx - > ch6_presentation_mod = 1 ;
ctx - > ch8_presentation_mod = 1 ;
2022-09-18 14:44:27 +02:00
ctx - > thd_substream_info = 0x14 ;
2023-10-19 23:36:19 +02:00
break ;
2023-10-20 23:27:06 +02:00
case AV_CH_LAYOUT_2POINT1 :
2023-10-19 23:36:19 +02:00
case AV_CH_LAYOUT_SURROUND :
2023-10-25 11:05:35 +02:00
case AV_CH_LAYOUT_3POINT1 :
2023-10-25 12:46:24 +02:00
case AV_CH_LAYOUT_4POINT0 :
case AV_CH_LAYOUT_4POINT1 :
2023-10-19 23:36:19 +02:00
case AV_CH_LAYOUT_5POINT0 :
case AV_CH_LAYOUT_5POINT1 :
ctx - > ch2_presentation_mod = 0 ;
ctx - > ch6_presentation_mod = 0 ;
ctx - > ch8_presentation_mod = 0 ;
ctx - > thd_substream_info = 0x3C ;
break ;
default :
2022-09-17 23:49:57 +02:00
av_assert1 ( ! " AVCodec.ch_layouts needs to be updated " ) ;
2016-08-30 12:23:02 +02:00
}
ctx - > flags = 0 ;
ctx - > channel_occupancy = 0 ;
ctx - > summary_info = 0 ;
2023-10-19 22:11:33 +02:00
ctx - > channel_arrangement =
ctx - > channel_arrangement8 = layout_truehd ( channels_present ) ;
2016-08-30 12:23:02 +02:00
}
2023-10-19 23:36:19 +02:00
for ( unsigned int index = 0 ; index < ctx - > restart_intervals ; index + + ) {
for ( int n = 0 ; n < ctx - > num_substreams ; n + + )
ctx - > s [ n ] . b [ index ] . seq_size = ( ( index + 1 ) * ctx - > min_restart_interval ) + 1 ;
}
2016-08-30 12:23:02 +02:00
2021-09-06 23:46:23 +02:00
/* TODO see if noisegen_seed is really worth it. */
2023-10-19 23:36:19 +02:00
if ( ctx - > avctx - > codec_id = = AV_CODEC_ID_MLP ) {
RestartHeader * const rh = & ctx - > s [ 0 ] . restart_header ;
rh - > noisegen_seed = 0 ;
rh - > min_channel = 0 ;
rh - > max_channel = avctx - > ch_layout . nb_channels - 1 ;
rh - > max_matrix_channel = rh - > max_channel ;
} else {
RestartHeader * rh = & ctx - > s [ 0 ] . restart_header ;
rh - > noisegen_seed = 0 ;
rh - > min_channel = 0 ;
rh - > max_channel = FFMIN ( avctx - > ch_layout . nb_channels , 2 ) - 1 ;
rh - > max_matrix_channel = rh - > max_channel ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
if ( avctx - > ch_layout . nb_channels > 2 ) {
rh = & ctx - > s [ 1 ] . restart_header ;
rh - > noisegen_seed = 0 ;
rh - > min_channel = 2 ;
rh - > max_channel = avctx - > ch_layout . nb_channels - 1 ;
rh - > max_matrix_channel = rh - > max_channel ;
}
}
2016-08-30 12:23:02 +02:00
2023-10-13 12:23:34 +02:00
if ( ( ret = ff_lpc_init ( & ctx - > lpc_ctx , ctx - > avctx - > frame_size ,
2023-10-02 20:02:18 +02:00
MLP_MAX_LPC_ORDER , ctx - > lpc_type ) ) < 0 )
2016-08-30 12:23:02 +02:00
return ret ;
ff_af_queue_init ( avctx , & ctx - > afq ) ;
2020-12-03 02:06:22 +02:00
ff_thread_once ( & init_static_once , mlp_encode_init_static ) ;
2016-08-30 12:23:02 +02:00
return 0 ;
}
/****************************************************************************
* * * * * * * * * * * * * * * * * * Functions that write to the bitstream * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/** Writes a major sync header to the bitstream. */
static void write_major_sync ( MLPEncodeContext * ctx , uint8_t * buf , int buf_size )
{
PutBitContext pb ;
init_put_bits ( & pb , buf , buf_size ) ;
put_bits ( & pb , 24 , SYNC_MAJOR ) ;
if ( ctx - > avctx - > codec_id = = AV_CODEC_ID_MLP ) {
put_bits ( & pb , 8 , SYNC_MLP ) ;
put_bits ( & pb , 4 , ctx - > coded_sample_fmt [ 0 ] ) ;
put_bits ( & pb , 4 , ctx - > coded_sample_fmt [ 1 ] ) ;
put_bits ( & pb , 4 , ctx - > coded_sample_rate [ 0 ] ) ;
put_bits ( & pb , 4 , ctx - > coded_sample_rate [ 1 ] ) ;
put_bits ( & pb , 4 , 0 ) ; /* ignored */
put_bits ( & pb , 4 , 0 ) ; /* multi_channel_type */
put_bits ( & pb , 3 , 0 ) ; /* ignored */
put_bits ( & pb , 5 , ctx - > channel_arrangement ) ;
} else if ( ctx - > avctx - > codec_id = = AV_CODEC_ID_TRUEHD ) {
put_bits ( & pb , 8 , SYNC_TRUEHD ) ;
put_bits ( & pb , 4 , ctx - > coded_sample_rate [ 0 ] ) ;
2023-10-06 22:15:33 +02:00
put_bits ( & pb , 1 , ctx - > multichannel_type6ch ) ;
put_bits ( & pb , 1 , ctx - > multichannel_type8ch ) ;
2022-09-18 14:44:27 +02:00
put_bits ( & pb , 2 , 0 ) ; /* ignored */
2023-10-06 22:15:33 +02:00
put_bits ( & pb , 2 , ctx - > ch2_presentation_mod ) ;
put_bits ( & pb , 2 , ctx - > ch6_presentation_mod ) ;
2016-08-30 12:23:02 +02:00
put_bits ( & pb , 5 , ctx - > channel_arrangement ) ;
2023-10-06 22:15:33 +02:00
put_bits ( & pb , 2 , ctx - > ch8_presentation_mod ) ;
put_bits ( & pb , 13 , ctx - > channel_arrangement8 ) ;
2016-08-30 12:23:02 +02:00
}
put_bits ( & pb , 16 , MAJOR_SYNC_INFO_SIGNATURE ) ;
put_bits ( & pb , 16 , ctx - > flags ) ;
put_bits ( & pb , 16 , 0 ) ; /* ignored */
put_bits ( & pb , 1 , 1 ) ; /* is_vbr */
put_bits ( & pb , 15 , ctx - > coded_peak_bitrate ) ;
2023-10-19 22:13:51 +02:00
put_bits ( & pb , 4 , ctx - > num_substreams ) ;
2022-09-18 14:44:27 +02:00
put_bits ( & pb , 2 , 0 ) ; /* ignored */
put_bits ( & pb , 2 , 0 ) ; /* extended substream info */
2016-08-30 12:23:02 +02:00
/* channel_meaning */
2022-09-18 14:44:27 +02:00
if ( ctx - > avctx - > codec_id = = AV_CODEC_ID_MLP ) {
put_bits ( & pb , 8 , ctx - > substream_info ) ;
put_bits ( & pb , 5 , ctx - > fs ) ;
put_bits ( & pb , 5 , ctx - > wordlength ) ;
put_bits ( & pb , 6 , ctx - > channel_occupancy ) ;
put_bits ( & pb , 3 , 0 ) ; /* ignored */
put_bits ( & pb , 10 , 0 ) ; /* speaker_layout */
put_bits ( & pb , 3 , 0 ) ; /* copy_protection */
put_bits ( & pb , 16 , 0x8080 ) ; /* ignored */
put_bits ( & pb , 7 , 0 ) ; /* ignored */
put_bits ( & pb , 4 , 0 ) ; /* source_format */
put_bits ( & pb , 5 , ctx - > summary_info ) ;
} else if ( ctx - > avctx - > codec_id = = AV_CODEC_ID_TRUEHD ) {
put_bits ( & pb , 8 , ctx - > thd_substream_info ) ;
put_bits ( & pb , 6 , 0 ) ; /* reserved */
put_bits ( & pb , 1 , 0 ) ; /* 2ch control enabled */
put_bits ( & pb , 1 , 0 ) ; /* 6ch control enabled */
put_bits ( & pb , 1 , 0 ) ; /* 8ch control enabled */
put_bits ( & pb , 1 , 0 ) ; /* reserved */
put_bits ( & pb , 7 , 0 ) ; /* drc start up gain */
put_bits ( & pb , 6 , 0 ) ; /* 2ch dialogue norm */
put_bits ( & pb , 6 , 0 ) ; /* 2ch mix level */
put_bits ( & pb , 5 , 0 ) ; /* 6ch dialogue norm */
put_bits ( & pb , 6 , 0 ) ; /* 6ch mix level */
put_bits ( & pb , 5 , 0 ) ; /* 6ch source format */
put_bits ( & pb , 5 , 0 ) ; /* 8ch dialogue norm */
put_bits ( & pb , 6 , 0 ) ; /* 8ch mix level */
put_bits ( & pb , 6 , 0 ) ; /* 8ch source format */
put_bits ( & pb , 1 , 0 ) ; /* reserved */
put_bits ( & pb , 1 , 0 ) ; /* extra channel meaning present */
}
2016-08-30 12:23:02 +02:00
flush_put_bits ( & pb ) ;
AV_WL16 ( buf + 26 , ff_mlp_checksum16 ( buf , 26 ) ) ;
}
/** Writes a restart header to the bitstream. Damaged streams can start being
* decoded losslessly again after such a header and the subsequent decoding
* params header .
*/
2023-10-19 23:36:19 +02:00
static void write_restart_header ( MLPEncodeContext * ctx , MLPSubstream * s ,
PutBitContext * pb )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
RestartHeader * rh = s - > cur_restart_header ;
2020-01-24 12:33:34 +02:00
uint8_t lossless_check = xor_32_to_8 ( rh - > lossless_check_data ) ;
2016-08-30 12:23:02 +02:00
unsigned int start_count = put_bits_count ( pb ) ;
PutBitContext tmpb ;
uint8_t checksum ;
put_bits ( pb , 14 , 0x31ea ) ; /* TODO 0x31eb */
2022-09-17 16:28:05 +02:00
put_bits ( pb , 16 , ctx - > output_timing ) ;
2016-08-30 12:23:02 +02:00
put_bits ( pb , 4 , rh - > min_channel ) ;
put_bits ( pb , 4 , rh - > max_channel ) ;
put_bits ( pb , 4 , rh - > max_matrix_channel ) ;
put_bits ( pb , 4 , rh - > noise_shift ) ;
put_bits ( pb , 23 , rh - > noisegen_seed ) ;
2023-10-20 16:28:08 +02:00
put_bits ( pb , 4 , rh - > max_shift ) ;
2016-08-30 12:23:02 +02:00
put_bits ( pb , 5 , rh - > max_huff_lsbs ) ;
put_bits ( pb , 5 , rh - > max_output_bits ) ;
put_bits ( pb , 5 , rh - > max_output_bits ) ;
put_bits ( pb , 1 , rh - > data_check_present ) ;
put_bits ( pb , 8 , lossless_check ) ;
put_bits ( pb , 16 , 0 ) ; /* ignored */
2023-10-19 23:36:19 +02:00
for ( int ch = 0 ; ch < = rh - > max_matrix_channel ; ch + + )
2016-08-30 12:23:02 +02:00
put_bits ( pb , 6 , ch ) ;
/* Data must be flushed for the checksum to be correct. */
tmpb = * pb ;
flush_put_bits ( & tmpb ) ;
checksum = ff_mlp_restart_checksum ( pb - > buf , put_bits_count ( pb ) - start_count ) ;
put_bits ( pb , 8 , checksum ) ;
}
/** Writes matrix params for all primitive matrices to the bitstream. */
2023-10-13 12:23:34 +02:00
static void write_matrix_params ( MLPEncodeContext * ctx ,
2023-10-19 23:36:19 +02:00
MLPSubstream * s ,
2023-10-13 12:23:34 +02:00
DecodingParams * dp ,
PutBitContext * pb )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
RestartHeader * rh = s - > cur_restart_header ;
2016-08-30 12:23:02 +02:00
MatrixParams * mp = & dp - > matrix_params ;
2023-10-19 23:36:19 +02:00
int max_channel = rh - > max_matrix_channel ;
2016-08-30 12:23:02 +02:00
put_bits ( pb , 4 , mp - > count ) ;
2023-10-19 23:36:19 +02:00
if ( ! ctx - > noise_type )
max_channel + = 2 ;
2021-09-06 23:12:00 +02:00
for ( unsigned int mat = 0 ; mat < mp - > count ; mat + + ) {
2016-08-30 12:23:02 +02:00
put_bits ( pb , 4 , mp - > outch [ mat ] ) ; /* matrix_out_ch */
put_bits ( pb , 4 , mp - > fbits [ mat ] ) ;
2023-10-13 12:23:34 +02:00
put_bits ( pb , 1 , mp - > lsb_bypass [ mat ] ) ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
for ( int ch = 0 ; ch < = max_channel ; ch + + ) {
int32_t coeff = mp - > coeff [ mat ] [ ch ] ;
2016-08-30 12:23:02 +02:00
if ( coeff ) {
put_bits ( pb , 1 , 1 ) ;
coeff > > = 14 - mp - > fbits [ mat ] ;
put_sbits ( pb , mp - > fbits [ mat ] + 2 , coeff ) ;
} else {
put_bits ( pb , 1 , 0 ) ;
}
}
}
}
/** Writes filter parameters for one filter to the bitstream. */
2023-10-13 12:23:34 +02:00
static void write_filter_params ( MLPEncodeContext * ctx ,
ChannelParams * cp ,
PutBitContext * pb ,
2023-10-19 23:36:19 +02:00
int channel , unsigned int filter )
2016-08-30 12:23:02 +02:00
{
2023-10-13 12:23:34 +02:00
FilterParams * fp = & cp - > filter_params [ filter ] ;
2016-08-30 12:23:02 +02:00
put_bits ( pb , 4 , fp - > order ) ;
if ( fp - > order > 0 ) {
2023-10-13 12:23:34 +02:00
int32_t * fcoeff = cp - > coeff [ filter ] ;
2016-08-30 12:23:02 +02:00
put_bits ( pb , 4 , fp - > shift ) ;
put_bits ( pb , 5 , fp - > coeff_bits ) ;
put_bits ( pb , 3 , fp - > coeff_shift ) ;
2021-09-06 23:12:00 +02:00
for ( int i = 0 ; i < fp - > order ; i + + ) {
2016-08-30 12:23:02 +02:00
put_sbits ( pb , fp - > coeff_bits , fcoeff [ i ] > > fp - > coeff_shift ) ;
}
/* TODO state data for IIR filter. */
put_bits ( pb , 1 , 0 ) ;
}
}
/** Writes decoding parameters to the bitstream. These change very often,
* usually at almost every frame .
*/
2023-10-19 23:36:19 +02:00
static void write_decoding_params ( MLPEncodeContext * ctx , MLPSubstream * s ,
PutBitContext * pb , int params_changed ,
2023-10-13 12:23:34 +02:00
unsigned int subblock_index )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
DecodingParams * dp = & s - > b [ subblock_index ] . major_decoding_params ;
RestartHeader * rh = s - > cur_restart_header ;
2016-08-30 12:23:02 +02:00
if ( dp - > param_presence_flags ! = PARAMS_DEFAULT & &
params_changed & PARAM_PRESENCE_FLAGS ) {
put_bits ( pb , 1 , 1 ) ;
put_bits ( pb , 8 , dp - > param_presence_flags ) ;
} else {
put_bits ( pb , 1 , 0 ) ;
}
if ( dp - > param_presence_flags & PARAM_BLOCKSIZE ) {
if ( params_changed & PARAM_BLOCKSIZE ) {
put_bits ( pb , 1 , 1 ) ;
put_bits ( pb , 9 , dp - > blocksize ) ;
} else {
put_bits ( pb , 1 , 0 ) ;
}
}
if ( dp - > param_presence_flags & PARAM_MATRIX ) {
if ( params_changed & PARAM_MATRIX ) {
put_bits ( pb , 1 , 1 ) ;
2023-10-19 23:36:19 +02:00
write_matrix_params ( ctx , s , dp , pb ) ;
2016-08-30 12:23:02 +02:00
} else {
put_bits ( pb , 1 , 0 ) ;
}
}
if ( dp - > param_presence_flags & PARAM_OUTSHIFT ) {
if ( params_changed & PARAM_OUTSHIFT ) {
put_bits ( pb , 1 , 1 ) ;
2023-10-19 23:36:19 +02:00
for ( int ch = 0 ; ch < = rh - > max_matrix_channel ; ch + + )
put_sbits ( pb , 4 , dp - > output_shift [ ch ] ) ;
2016-08-30 12:23:02 +02:00
} else {
put_bits ( pb , 1 , 0 ) ;
}
}
if ( dp - > param_presence_flags & PARAM_QUANTSTEP ) {
if ( params_changed & PARAM_QUANTSTEP ) {
put_bits ( pb , 1 , 1 ) ;
2023-10-19 23:36:19 +02:00
for ( int ch = 0 ; ch < = rh - > max_channel ; ch + + )
2016-08-30 12:23:02 +02:00
put_bits ( pb , 4 , dp - > quant_step_size [ ch ] ) ;
} else {
put_bits ( pb , 1 , 0 ) ;
}
}
2023-10-19 23:36:19 +02:00
for ( int ch = rh - > min_channel ; ch < = rh - > max_channel ; ch + + ) {
ChannelParams * cp = & s - > b [ subblock_index ] . major_channel_params [ ch ] ;
2016-08-30 12:23:02 +02:00
if ( dp - > param_presence_flags & 0xF ) {
put_bits ( pb , 1 , 1 ) ;
if ( dp - > param_presence_flags & PARAM_FIR ) {
if ( params_changed & PARAM_FIR ) {
put_bits ( pb , 1 , 1 ) ;
2023-10-13 12:23:34 +02:00
write_filter_params ( ctx , cp , pb , ch , FIR ) ;
2016-08-30 12:23:02 +02:00
} else {
put_bits ( pb , 1 , 0 ) ;
}
}
if ( dp - > param_presence_flags & PARAM_IIR ) {
if ( params_changed & PARAM_IIR ) {
put_bits ( pb , 1 , 1 ) ;
2023-10-13 12:23:34 +02:00
write_filter_params ( ctx , cp , pb , ch , IIR ) ;
2016-08-30 12:23:02 +02:00
} else {
put_bits ( pb , 1 , 0 ) ;
}
}
if ( dp - > param_presence_flags & PARAM_HUFFOFFSET ) {
if ( params_changed & PARAM_HUFFOFFSET ) {
put_bits ( pb , 1 , 1 ) ;
put_sbits ( pb , 15 , cp - > huff_offset ) ;
} else {
put_bits ( pb , 1 , 0 ) ;
}
}
2020-02-02 21:03:00 +02:00
if ( cp - > codebook > 0 & & cp - > huff_lsbs > 24 ) {
2023-10-03 12:43:14 +02:00
av_log ( ctx - > avctx , AV_LOG_ERROR , " Invalid Huff LSBs %d \n " , cp - > huff_lsbs ) ;
2020-02-02 21:03:00 +02:00
}
2016-08-30 12:23:02 +02:00
put_bits ( pb , 2 , cp - > codebook ) ;
put_bits ( pb , 5 , cp - > huff_lsbs ) ;
} else {
put_bits ( pb , 1 , 0 ) ;
}
}
}
/** Writes the residuals to the bitstream. That is, the VLC codes from the
* codebooks ( if any is used ) , and then the residual .
*/
2023-10-19 23:36:19 +02:00
static void write_block_data ( MLPEncodeContext * ctx , MLPSubstream * s ,
PutBitContext * pb , unsigned int subblock_index )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
RestartHeader * rh = s - > cur_restart_header ;
DecodingParams * dp = & s - > b [ subblock_index ] . major_decoding_params ;
2023-10-13 12:23:34 +02:00
MatrixParams * mp = & dp - > matrix_params ;
2016-08-30 12:23:02 +02:00
int32_t sign_huff_offset [ MAX_CHANNELS ] ;
int codebook_index [ MAX_CHANNELS ] ;
int lsb_bits [ MAX_CHANNELS ] ;
2023-10-19 23:36:19 +02:00
for ( int ch = rh - > min_channel ; ch < = rh - > max_channel ; ch + + ) {
ChannelParams * cp = & s - > b [ subblock_index ] . major_channel_params [ ch ] ;
2016-08-30 12:23:02 +02:00
int sign_shift ;
lsb_bits [ ch ] = cp - > huff_lsbs - dp - > quant_step_size [ ch ] ;
codebook_index [ ch ] = cp - > codebook - 1 ;
sign_huff_offset [ ch ] = cp - > huff_offset ;
2020-01-24 12:33:34 +02:00
sign_shift = lsb_bits [ ch ] + ( cp - > codebook ? 2 - cp - > codebook : - 1 ) ;
2016-08-30 12:23:02 +02:00
2020-01-24 12:33:34 +02:00
if ( cp - > codebook > 0 )
2016-08-30 12:23:02 +02:00
sign_huff_offset [ ch ] - = 7 < < lsb_bits [ ch ] ;
/* Unsign if needed. */
if ( sign_shift > = 0 )
sign_huff_offset [ ch ] - = 1 < < sign_shift ;
}
2021-09-06 23:12:00 +02:00
for ( unsigned int i = 0 ; i < dp - > blocksize ; i + + ) {
2023-10-13 12:23:34 +02:00
for ( unsigned int mat = 0 ; mat < mp - > count ; mat + + ) {
if ( mp - > lsb_bypass [ mat ] ) {
const int8_t * bypassed_lsbs = mp - > bypassed_lsbs [ mat ] ;
put_bits ( pb , 1 , bypassed_lsbs [ i ] ) ;
}
}
2023-10-19 23:36:19 +02:00
for ( int ch = rh - > min_channel ; ch < = rh - > max_channel ; ch + + ) {
2023-10-13 12:23:34 +02:00
int32_t * sample_buffer = dp - > sample_buffer [ ch ] ;
int32_t sample = sample_buffer [ i ] > > dp - > quant_step_size [ ch ] ;
2016-08-30 12:23:02 +02:00
sample - = sign_huff_offset [ ch ] ;
if ( codebook_index [ ch ] > = 0 ) {
int vlc = sample > > lsb_bits [ ch ] ;
put_bits ( pb , ff_mlp_huffman_tables [ codebook_index [ ch ] ] [ vlc ] [ 1 ] ,
ff_mlp_huffman_tables [ codebook_index [ ch ] ] [ vlc ] [ 0 ] ) ;
2023-10-03 12:43:14 +02:00
sample & = ( ( 1 < < lsb_bits [ ch ] ) - 1 ) ;
2016-08-30 12:23:02 +02:00
}
2023-10-03 12:43:14 +02:00
put_bits ( pb , lsb_bits [ ch ] , sample ) ;
2016-08-30 12:23:02 +02:00
}
}
}
2021-09-06 23:46:23 +02:00
/** Writes the substream data to the bitstream. */
2023-10-19 23:36:19 +02:00
static uint8_t * write_substr ( MLPEncodeContext * ctx ,
MLPSubstream * s ,
uint8_t * buf , int buf_size ,
2021-09-06 23:46:23 +02:00
int restart_frame ,
2023-10-19 23:36:19 +02:00
uint16_t * substream_data_len )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
int32_t * lossless_check_data = & s - > b [ ctx - > frame_index ] . lossless_check_data ;
unsigned int cur_subblock_index = s - > major_cur_subblock_index ;
unsigned int num_subblocks = s - > major_filter_state_subblock ;
RestartHeader * rh = & s - > restart_header ;
2021-09-06 23:46:23 +02:00
int substr_restart_frame = restart_frame ;
uint8_t parity , checksum ;
PutBitContext pb ;
int params_changed ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
s - > cur_restart_header = rh ;
2016-08-30 12:23:02 +02:00
2021-09-06 23:46:23 +02:00
init_put_bits ( & pb , buf , buf_size ) ;
2016-08-30 12:23:02 +02:00
2021-09-06 23:46:23 +02:00
for ( unsigned int subblock = 0 ; subblock < = num_subblocks ; subblock + + ) {
2023-10-13 12:23:34 +02:00
unsigned int subblock_index = cur_subblock_index + + ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
params_changed = s - > b [ subblock_index ] . major_params_changed ;
2016-08-30 12:23:02 +02:00
2021-09-06 23:46:23 +02:00
if ( substr_restart_frame | | params_changed ) {
put_bits ( & pb , 1 , 1 ) ;
2016-08-30 12:23:02 +02:00
2021-09-06 23:46:23 +02:00
if ( substr_restart_frame ) {
2016-08-30 12:23:02 +02:00
put_bits ( & pb , 1 , 1 ) ;
2023-10-19 23:36:19 +02:00
write_restart_header ( ctx , s , & pb ) ;
2021-09-06 23:46:23 +02:00
rh - > lossless_check_data = 0 ;
2016-08-30 12:23:02 +02:00
} else {
put_bits ( & pb , 1 , 0 ) ;
}
2023-10-19 23:36:19 +02:00
write_decoding_params ( ctx , s , & pb , params_changed ,
2023-10-13 12:23:34 +02:00
subblock_index ) ;
2021-09-06 23:46:23 +02:00
} else {
put_bits ( & pb , 1 , 0 ) ;
}
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
write_block_data ( ctx , s , & pb , subblock_index ) ;
2016-08-30 12:23:02 +02:00
2021-09-06 23:46:23 +02:00
put_bits ( & pb , 1 , ! substr_restart_frame ) ;
2016-08-30 12:23:02 +02:00
2021-09-06 23:46:23 +02:00
substr_restart_frame = 0 ;
}
2016-08-30 12:23:02 +02:00
2021-09-06 23:46:23 +02:00
put_bits ( & pb , ( - put_bits_count ( & pb ) ) & 15 , 0 ) ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
rh - > lossless_check_data ^ = lossless_check_data [ 0 ] ;
2021-09-06 23:46:23 +02:00
if ( ctx - > last_frames = = 0 & & ctx - > shorten_by ) {
if ( ctx - > avctx - > codec_id = = AV_CODEC_ID_TRUEHD ) {
put_bits ( & pb , 16 , END_OF_STREAM & 0xFFFF ) ;
2023-10-09 18:24:49 +02:00
put_bits ( & pb , 16 , ( ctx - > shorten_by & 0x1FFF ) | 0xE000 ) ;
2021-09-06 23:46:23 +02:00
} else {
2023-10-01 22:59:34 +02:00
put_bits32 ( & pb , END_OF_STREAM ) ;
2016-08-30 12:23:02 +02:00
}
2021-09-06 23:46:23 +02:00
}
2016-08-30 12:23:02 +02:00
2021-09-06 23:46:23 +02:00
/* Data must be flushed for the checksum and parity to be correct;
* notice that we already are word - aligned here . */
flush_put_bits ( & pb ) ;
2016-08-30 12:23:02 +02:00
2021-09-06 23:46:23 +02:00
parity = ff_mlp_calculate_parity ( buf , put_bytes_output ( & pb ) ) ^ 0xa9 ;
checksum = ff_mlp_checksum8 ( buf , put_bytes_output ( & pb ) ) ;
2016-08-30 12:23:02 +02:00
2021-09-06 23:46:23 +02:00
put_bits ( & pb , 8 , parity ) ;
put_bits ( & pb , 8 , checksum ) ;
2016-08-30 12:23:02 +02:00
2021-09-06 23:46:23 +02:00
flush_put_bits ( & pb ) ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
substream_data_len [ 0 ] = put_bytes_output ( & pb ) ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
buf + = substream_data_len [ 0 ] ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
s - > major_cur_subblock_index + = s - > major_filter_state_subblock + 1 ;
s - > major_filter_state_subblock = 0 ;
2016-08-30 12:23:02 +02:00
return buf ;
}
/** Writes the access unit and substream headers to the bitstream. */
static void write_frame_headers ( MLPEncodeContext * ctx , uint8_t * frame_header ,
uint8_t * substream_headers , unsigned int length ,
int restart_frame ,
2023-10-19 23:36:19 +02:00
uint16_t substream_data_len [ MAX_SUBSTREAMS ] )
2016-08-30 12:23:02 +02:00
{
uint16_t access_unit_header = 0 ;
2023-10-19 23:36:19 +02:00
uint16_t substream_data_end = 0 ;
2016-08-30 12:23:02 +02:00
uint16_t parity_nibble = 0 ;
2022-09-17 16:28:05 +02:00
parity_nibble = ctx - > input_timing ;
2016-08-30 12:23:02 +02:00
parity_nibble ^ = length ;
2021-09-06 23:12:00 +02:00
for ( unsigned int substr = 0 ; substr < ctx - > num_substreams ; substr + + ) {
2016-08-30 12:23:02 +02:00
uint16_t substr_hdr = 0 ;
2023-10-19 23:36:19 +02:00
substream_data_end + = substream_data_len [ substr ] ;
2016-08-30 12:23:02 +02:00
substr_hdr | = ( 0 < < 15 ) ; /* extraword */
substr_hdr | = ( ! restart_frame < < 14 ) ; /* !restart_frame */
substr_hdr | = ( 1 < < 13 ) ; /* checkdata */
substr_hdr | = ( 0 < < 12 ) ; /* ??? */
2023-10-19 23:36:19 +02:00
substr_hdr | = ( substream_data_end / 2 ) & 0x0FFF ;
2016-08-30 12:23:02 +02:00
AV_WB16 ( substream_headers , substr_hdr ) ;
parity_nibble ^ = * substream_headers + + ;
parity_nibble ^ = * substream_headers + + ;
}
parity_nibble ^ = parity_nibble > > 8 ;
parity_nibble ^ = parity_nibble > > 4 ;
parity_nibble & = 0xF ;
access_unit_header | = ( parity_nibble ^ 0xF ) < < 12 ;
access_unit_header | = length & 0xFFF ;
AV_WB16 ( frame_header , access_unit_header ) ;
2022-09-17 16:28:05 +02:00
AV_WB16 ( frame_header + 2 , ctx - > input_timing ) ;
2016-08-30 12:23:02 +02:00
}
/** Writes an entire access unit to the bitstream. */
2021-09-05 11:36:50 +02:00
static int write_access_unit ( MLPEncodeContext * ctx , uint8_t * buf ,
int buf_size , int restart_frame )
2016-08-30 12:23:02 +02:00
{
uint16_t substream_data_len [ MAX_SUBSTREAMS ] ;
uint8_t * buf1 , * buf0 = buf ;
int total_length ;
/* Frame header will be written at the end. */
buf + = 4 ;
buf_size - = 4 ;
if ( restart_frame ) {
write_major_sync ( ctx , buf , buf_size ) ;
buf + = 28 ;
buf_size - = 28 ;
}
buf1 = buf ;
/* Substream headers will be written at the end. */
2021-09-06 23:12:00 +02:00
for ( unsigned int substr = 0 ; substr < ctx - > num_substreams ; substr + + ) {
2016-08-30 12:23:02 +02:00
buf + = 2 ;
buf_size - = 2 ;
}
2023-10-19 23:36:19 +02:00
for ( int substr = 0 ; substr < ctx - > num_substreams ; substr + + ) {
MLPSubstream * s = & ctx - > s [ substr ] ;
uint8_t * buf0 = buf ;
buf = write_substr ( ctx , s , buf , buf_size , restart_frame , & substream_data_len [ substr ] ) ;
buf_size - = buf - buf0 ;
}
2016-08-30 12:23:02 +02:00
total_length = buf - buf0 ;
write_frame_headers ( ctx , buf0 , buf1 , total_length / 2 , restart_frame , substream_data_len ) ;
return total_length ;
}
/****************************************************************************
* * * * * * * * * * * * * * * * * * Functions that input data to context * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/** Inputs data from the samples passed by lavc into the context, shifts them
* appropriately depending on the bit - depth , and calculates the
* lossless_check_data that will be written to the restart header .
*/
2023-10-19 23:36:19 +02:00
static void input_data_internal ( MLPEncodeContext * ctx , MLPSubstream * s ,
uint8_t * * const samples ,
2023-10-16 12:42:30 +02:00
int nb_samples , int is24 )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
int32_t * lossless_check_data = & s - > b [ ctx - > frame_index ] . lossless_check_data ;
RestartHeader * rh = & s - > restart_header ;
2021-09-06 23:46:23 +02:00
int32_t temp_lossless_check_data = 0 ;
2023-10-03 12:43:14 +02:00
uint32_t bits = 0 ;
2016-08-30 12:23:02 +02:00
2022-04-12 17:57:58 +02:00
for ( int i = 0 ; i < nb_samples ; i + + ) {
2023-10-19 23:36:19 +02:00
for ( int ch = 0 ; ch < = rh - > max_channel ; ch + + ) {
const int32_t * samples_32 = ( const int32_t * ) samples [ ch ] ;
const int16_t * samples_16 = ( const int16_t * ) samples [ ch ] ;
int32_t * sample_buffer = s - > b [ ctx - > frame_index ] . inout_buffer [ ch ] ;
2021-09-06 23:46:23 +02:00
int32_t sample ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
sample = is24 ? samples_32 [ i ] > > 8 : samples_16 [ i ] * 256 ;
2016-08-30 12:23:02 +02:00
2023-10-03 12:43:14 +02:00
bits = FFMAX ( number_sbits ( sample ) , bits ) ;
2016-08-30 12:23:02 +02:00
2023-10-13 12:23:34 +02:00
temp_lossless_check_data ^ = ( sample & 0x00ffffff ) < < ch ;
sample_buffer [ i ] = sample ;
2016-08-30 12:23:02 +02:00
}
}
2021-09-06 23:46:23 +02:00
2023-10-19 23:36:19 +02:00
for ( int ch = 0 ; ch < = rh - > max_channel ; ch + + ) {
for ( int i = nb_samples ; i < ctx - > avctx - > frame_size ; i + + ) {
int32_t * sample_buffer = s - > b [ ctx - > frame_index ] . inout_buffer [ ch ] ;
2023-10-16 12:42:30 +02:00
sample_buffer [ i ] = 0 ;
}
}
2023-10-19 23:36:19 +02:00
s - > b [ ctx - > frame_index ] . max_output_bits = bits ;
2021-09-06 23:46:23 +02:00
2023-10-19 23:36:19 +02:00
lossless_check_data [ 0 ] = temp_lossless_check_data ;
2016-08-30 12:23:02 +02:00
}
/** Wrapper function for inputting data in two different bit-depths. */
2023-10-19 23:36:19 +02:00
static void input_data ( MLPEncodeContext * ctx , MLPSubstream * s , uint8_t * * const samples , int nb_samples )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
input_data_internal ( ctx , s , samples , nb_samples , ctx - > avctx - > sample_fmt = = AV_SAMPLE_FMT_S32P ) ;
2016-08-30 12:23:02 +02:00
}
2023-10-19 23:36:19 +02:00
static void input_to_sample_buffer ( MLPEncodeContext * ctx , MLPSubstream * s )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
RestartHeader * rh = & s - > restart_header ;
2021-09-06 23:12:00 +02:00
for ( unsigned int index = 0 ; index < ctx - > number_of_frames ; index + + ) {
2023-10-13 12:23:34 +02:00
unsigned int cur_index = ( ctx - > frame_index + index + 1 ) % ctx - > cur_restart_interval ;
2023-10-19 23:36:19 +02:00
DecodingParams * dp = & s - > b [ index + 1 ] . decoding_params ;
2023-10-13 12:23:34 +02:00
2023-10-19 23:36:19 +02:00
for ( int ch = 0 ; ch < = rh - > max_channel ; ch + + ) {
const int32_t * input_buffer = s - > b [ cur_index ] . inout_buffer [ ch ] ;
2023-10-13 12:23:34 +02:00
int32_t * sample_buffer = dp - > sample_buffer [ ch ] ;
int off = 0 ;
if ( dp - > blocksize < ctx - > avctx - > frame_size ) {
2023-10-19 23:36:19 +02:00
DecodingParams * dp = & s - > b [ index ] . decoding_params ;
2023-10-13 12:23:34 +02:00
int32_t * sample_buffer = dp - > sample_buffer [ ch ] ;
for ( unsigned int i = 0 ; i < dp - > blocksize ; i + + )
sample_buffer [ i ] = input_buffer [ i ] ;
off = dp - > blocksize ;
}
for ( unsigned int i = 0 ; i < dp - > blocksize ; i + + )
sample_buffer [ i ] = input_buffer [ i + off ] ;
2016-08-30 12:23:02 +02:00
}
}
}
/****************************************************************************
* * * * * * * * * Functions that analyze the data and set the parameters * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/** Counts the number of trailing zeroes in a value */
2023-10-09 16:36:10 +02:00
static int number_trailing_zeroes ( int32_t sample , unsigned int max , unsigned int def )
2016-08-30 12:23:02 +02:00
{
2023-10-09 16:36:10 +02:00
return sample ? FFMIN ( max , ff_ctz ( sample ) ) : def ;
2016-08-30 12:23:02 +02:00
}
2023-10-20 16:28:08 +02:00
static void determine_output_shift ( MLPEncodeContext * ctx , MLPSubstream * s )
{
RestartHeader * rh = s - > cur_restart_header ;
DecodingParams * dp1 = & s - > b [ 1 ] . decoding_params ;
int32_t sample_mask [ MAX_CHANNELS ] ;
memset ( sample_mask , 0 , sizeof ( sample_mask ) ) ;
for ( int j = 0 ; j < = ctx - > cur_restart_interval ; j + + ) {
DecodingParams * dp = & s - > b [ j ] . decoding_params ;
for ( int ch = 0 ; ch < = rh - > max_matrix_channel ; ch + + ) {
int32_t * sample_buffer = dp - > sample_buffer [ ch ] ;
for ( int i = 0 ; i < dp - > blocksize ; i + + )
sample_mask [ ch ] | = sample_buffer [ i ] ;
}
}
for ( int ch = 0 ; ch < = rh - > max_matrix_channel ; ch + + )
dp1 - > output_shift [ ch ] = number_trailing_zeroes ( sample_mask [ ch ] , 7 , 0 ) ;
for ( int j = 0 ; j < = ctx - > cur_restart_interval ; j + + ) {
DecodingParams * dp = & s - > b [ j ] . decoding_params ;
for ( int ch = 0 ; ch < = rh - > max_matrix_channel ; ch + + ) {
int32_t * sample_buffer = dp - > sample_buffer [ ch ] ;
const int shift = dp1 - > output_shift [ ch ] ;
for ( int i = 0 ; i < dp - > blocksize ; i + + )
sample_buffer [ i ] > > = shift ;
}
}
}
2016-08-30 12:23:02 +02:00
/** Determines how many bits are zero at the end of all samples so they can be
* shifted out .
*/
2023-10-19 23:36:19 +02:00
static void determine_quant_step_size ( MLPEncodeContext * ctx , MLPSubstream * s )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
RestartHeader * rh = s - > cur_restart_header ;
DecodingParams * dp1 = & s - > b [ 1 ] . decoding_params ;
2016-08-30 12:23:02 +02:00
int32_t sample_mask [ MAX_CHANNELS ] ;
2023-10-13 12:23:34 +02:00
memset ( sample_mask , 0 , sizeof ( sample_mask ) ) ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
for ( int j = 0 ; j < = ctx - > cur_restart_interval ; j + + ) {
DecodingParams * dp = & s - > b [ j ] . decoding_params ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
for ( int ch = 0 ; ch < = rh - > max_channel ; ch + + ) {
2023-10-13 12:23:34 +02:00
int32_t * sample_buffer = dp - > sample_buffer [ ch ] ;
2023-10-19 23:36:19 +02:00
for ( int i = 0 ; i < dp - > blocksize ; i + + )
2023-10-13 12:23:34 +02:00
sample_mask [ ch ] | = sample_buffer [ i ] ;
}
2016-08-30 12:23:02 +02:00
}
2023-10-19 23:36:19 +02:00
for ( int ch = 0 ; ch < = rh - > max_channel ; ch + + )
dp1 - > quant_step_size [ ch ] = number_trailing_zeroes ( sample_mask [ ch ] , 15 , 0 ) ;
2016-08-30 12:23:02 +02:00
}
/** Determines the smallest number of bits needed to encode the filter
* coefficients , and if it ' s possible to right - shift their values without
* losing any precision .
*/
2023-10-18 20:28:04 +02:00
static void code_filter_coeffs ( MLPEncodeContext * ctx , FilterParams * fp , const int32_t * fcoeff )
2016-08-30 12:23:02 +02:00
{
2023-10-03 12:43:14 +02:00
uint32_t coeff_mask = 0 ;
int bits = 0 , shift ;
2016-08-30 12:23:02 +02:00
2021-09-06 23:12:00 +02:00
for ( int order = 0 ; order < fp - > order ; order + + ) {
2023-10-03 12:43:14 +02:00
int32_t coeff = fcoeff [ order ] ;
2016-08-30 12:23:02 +02:00
2023-10-03 12:43:14 +02:00
bits = FFMAX ( number_sbits ( coeff ) , bits ) ;
2016-08-30 12:23:02 +02:00
coeff_mask | = coeff ;
}
2023-10-03 12:43:14 +02:00
shift = FFMIN ( 7 , coeff_mask ? ff_ctz ( coeff_mask ) : 0 ) ;
2016-08-30 12:23:02 +02:00
2023-10-03 12:43:14 +02:00
fp - > coeff_bits = FFMAX ( 1 , bits - shift ) ;
fp - > coeff_shift = FFMIN ( shift , 16 - fp - > coeff_bits ) ;
2016-08-30 12:23:02 +02:00
}
/** Determines the best filter parameters for the given data and writes the
* necessary information to the context .
*/
2023-10-19 23:36:19 +02:00
static void set_filter ( MLPEncodeContext * ctx , MLPSubstream * s ,
2023-10-25 15:32:53 +02:00
int channel , int retry_filter )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
ChannelParams * cp = & s - > b [ 1 ] . channel_params [ channel ] ;
2023-10-25 15:32:53 +02:00
DecodingParams * dp1 = & s - > b [ 1 ] . decoding_params ;
2023-10-18 20:28:04 +02:00
FilterParams * fp = & cp - > filter_params [ FIR ] ;
2016-08-30 12:23:02 +02:00
2023-10-25 15:32:53 +02:00
if ( retry_filter )
dp1 - > max_order [ channel ] - - ;
if ( dp1 - > max_order [ channel ] = = 0 ) {
2016-08-30 12:23:02 +02:00
fp - > order = 0 ;
2023-10-18 20:28:04 +02:00
} else {
2016-08-30 12:23:02 +02:00
int32_t * lpc_samples = ctx - > lpc_sample_buffer ;
2023-10-18 20:28:04 +02:00
int32_t * fcoeff = cp - > coeff [ FIR ] ;
2023-10-02 19:29:26 +02:00
int shift [ MAX_LPC_ORDER ] ;
2016-08-30 12:23:02 +02:00
int order ;
2023-10-13 12:23:34 +02:00
for ( unsigned int j = 0 ; j < = ctx - > cur_restart_interval ; j + + ) {
2023-10-19 23:36:19 +02:00
DecodingParams * dp = & s - > b [ j ] . decoding_params ;
2023-10-13 12:23:34 +02:00
int32_t * sample_buffer = dp - > sample_buffer [ channel ] ;
for ( unsigned int i = 0 ; i < dp - > blocksize ; i + + )
lpc_samples [ i ] = sample_buffer [ i ] ;
lpc_samples + = dp - > blocksize ;
2016-08-30 12:23:02 +02:00
}
order = ff_lpc_calc_coefs ( & ctx - > lpc_ctx , ctx - > lpc_sample_buffer ,
2023-10-13 12:23:34 +02:00
lpc_samples - ctx - > lpc_sample_buffer ,
2023-10-25 15:32:53 +02:00
MLP_MIN_LPC_ORDER , dp1 - > max_order [ channel ] ,
2023-10-13 12:23:34 +02:00
ctx - > lpc_coeff_precision ,
2023-10-25 15:32:53 +02:00
s - > coefs [ channel ] , shift , ctx - > lpc_type , ctx - > lpc_passes ,
2023-10-02 20:02:18 +02:00
ctx - > prediction_order , MLP_MIN_LPC_SHIFT ,
2023-10-18 14:40:42 +02:00
MLP_MAX_LPC_SHIFT , 0 ) ;
2016-08-30 12:23:02 +02:00
fp - > order = order ;
2023-10-19 23:36:19 +02:00
fp - > shift = order ? shift [ order - 1 ] : 0 ;
2016-08-30 12:23:02 +02:00
2021-09-06 23:12:00 +02:00
for ( unsigned int i = 0 ; i < order ; i + + )
2023-10-25 15:32:53 +02:00
fcoeff [ i ] = s - > coefs [ channel ] [ order - 1 ] [ i ] ;
2016-08-30 12:23:02 +02:00
code_filter_coeffs ( ctx , fp , fcoeff ) ;
}
}
/** Tries to determine a good prediction filter, and applies it to the samples
* buffer if the filter is good enough . Sets the filter data to be cleared if
* no good filter was found .
*/
2023-10-19 23:36:19 +02:00
static void determine_filters ( MLPEncodeContext * ctx , MLPSubstream * s )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
RestartHeader * rh = s - > cur_restart_header ;
2016-08-30 12:23:02 +02:00
2023-10-18 20:28:04 +02:00
for ( int ch = rh - > min_channel ; ch < = rh - > max_channel ; ch + + )
2023-10-19 23:36:19 +02:00
set_filter ( ctx , s , ch , 0 ) ;
2016-08-30 12:23:02 +02:00
}
2023-10-19 23:36:19 +02:00
static int estimate_coeff ( MLPEncodeContext * ctx , MLPSubstream * s ,
2023-10-09 16:36:10 +02:00
MatrixParams * mp ,
2023-10-19 23:36:19 +02:00
int ch0 , int ch1 )
2016-08-30 12:23:02 +02:00
{
2023-10-16 12:42:30 +02:00
int32_t maxl = INT32_MIN , maxr = INT32_MIN , minl = INT32_MAX , minr = INT32_MAX ;
int64_t summ = 0 , sums = 0 , suml = 0 , sumr = 0 , enl = 0 , enr = 0 ;
const int shift = 14 - ctx - > rematrix_precision ;
int32_t cf0 , cf1 , e [ 4 ] , d [ 4 ] , ml , mr ;
int i , count = 0 ;
2023-10-09 16:36:10 +02:00
2023-10-13 12:23:34 +02:00
for ( int j = 0 ; j < = ctx - > cur_restart_interval ; j + + ) {
2023-10-19 23:36:19 +02:00
DecodingParams * dp = & s - > b [ j ] . decoding_params ;
2023-10-13 12:23:34 +02:00
const int32_t * ch [ 2 ] ;
2023-10-09 16:36:10 +02:00
2023-10-13 12:23:34 +02:00
ch [ 0 ] = dp - > sample_buffer [ ch0 ] ;
ch [ 1 ] = dp - > sample_buffer [ ch1 ] ;
2023-10-09 16:36:10 +02:00
2023-10-13 12:23:34 +02:00
for ( int i = 0 ; i < dp - > blocksize ; i + + ) {
2023-10-16 12:42:30 +02:00
int32_t lm = ch [ 0 ] [ i ] , rm = ch [ 1 ] [ i ] ;
2023-10-09 16:36:10 +02:00
2023-10-16 12:42:30 +02:00
enl + = FFABS ( lm ) ;
enr + = FFABS ( rm ) ;
2023-10-09 16:36:10 +02:00
2023-10-16 12:42:30 +02:00
summ + = FFABS ( lm + rm ) ;
sums + = FFABS ( lm - rm ) ;
2023-10-09 16:36:10 +02:00
2023-10-16 12:42:30 +02:00
suml + = lm ;
sumr + = rm ;
2023-10-09 16:36:10 +02:00
2023-10-16 12:42:30 +02:00
maxl = FFMAX ( maxl , lm ) ;
maxr = FFMAX ( maxr , rm ) ;
2023-10-09 16:36:10 +02:00
2023-10-16 12:42:30 +02:00
minl = FFMIN ( minl , lm ) ;
minr = FFMIN ( minr , rm ) ;
2023-10-13 12:23:34 +02:00
}
2016-08-30 12:23:02 +02:00
}
2023-10-16 12:42:30 +02:00
summ - = FFABS ( suml + sumr ) ;
sums - = FFABS ( suml - sumr ) ;
2023-10-09 16:36:10 +02:00
2023-10-16 12:42:30 +02:00
ml = maxl - minl ;
mr = maxr - minr ;
2023-10-09 16:36:10 +02:00
2023-10-16 12:42:30 +02:00
if ( ! summ & & ! sums )
return 0 ;
2023-10-09 16:36:10 +02:00
2023-10-16 12:42:30 +02:00
if ( ! ml | | ! mr )
return 0 ;
2016-08-30 12:23:02 +02:00
2023-10-16 12:42:30 +02:00
if ( ( FFABS ( ml ) + FFABS ( mr ) ) > = ( 1 < < 24 ) )
return 0 ;
cf0 = ( FFMIN ( FFABS ( mr ) , FFABS ( ml ) ) * ( 1LL < < 14 ) ) / FFMAX ( FFABS ( ml ) , FFABS ( mr ) ) ;
cf0 = ( cf0 > > shift ) < < shift ;
cf1 = - cf0 ;
if ( sums > summ )
FFSWAP ( int32_t , cf0 , cf1 ) ;
count = 1 ;
i = enl < enr ;
2023-10-19 23:36:19 +02:00
mp - > outch [ 0 ] = ch0 + i ;
2023-10-16 12:42:30 +02:00
d [ ! i ] = cf0 ;
d [ i ] = 1 < < 14 ;
e [ ! i ] = cf1 ;
e [ i ] = 1 < < 14 ;
mp - > coeff [ 0 ] [ ch0 ] = av_clip_intp2 ( d [ 0 ] , 15 ) ;
mp - > coeff [ 0 ] [ ch1 ] = av_clip_intp2 ( d [ 1 ] , 15 ) ;
mp - > forco [ 0 ] [ ch0 ] = av_clip_intp2 ( e [ 0 ] , 15 ) ;
mp - > forco [ 0 ] [ ch1 ] = av_clip_intp2 ( e [ 1 ] , 15 ) ;
return count ;
2016-08-30 12:23:02 +02:00
}
/** Determines how many fractional bits are needed to encode matrix
* coefficients . Also shifts the coefficients to fit within 2.14 bits .
*/
2023-10-19 23:36:19 +02:00
static void code_matrix_coeffs ( MLPEncodeContext * ctx , MLPSubstream * s ,
2023-10-13 12:23:34 +02:00
DecodingParams * dp ,
unsigned int mat )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
RestartHeader * rh = s - > cur_restart_header ;
2016-08-30 12:23:02 +02:00
MatrixParams * mp = & dp - > matrix_params ;
int32_t coeff_mask = 0 ;
2023-10-19 23:36:19 +02:00
for ( int ch = 0 ; ch < = rh - > max_matrix_channel ; ch + + )
coeff_mask | = mp - > coeff [ mat ] [ ch ] ;
2016-08-30 12:23:02 +02:00
2023-10-09 16:36:10 +02:00
mp - > fbits [ mat ] = 14 - number_trailing_zeroes ( coeff_mask , 14 , 14 ) ;
2016-08-30 12:23:02 +02:00
}
/** Determines best coefficients to use for the lossless matrix. */
2023-10-19 23:36:19 +02:00
static void lossless_matrix_coeffs ( MLPEncodeContext * ctx , MLPSubstream * s )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
RestartHeader * rh = s - > cur_restart_header ;
DecodingParams * dp = & s - > b [ 1 ] . decoding_params ;
2016-08-30 12:23:02 +02:00
MatrixParams * mp = & dp - > matrix_params ;
2023-10-09 16:36:10 +02:00
mp - > count = 0 ;
if ( ctx - > num_channels - 2 ! = 2 )
2016-08-30 12:23:02 +02:00
return ;
2023-10-19 23:36:19 +02:00
mp - > count = estimate_coeff ( ctx , s , mp ,
rh - > min_channel , rh - > max_channel ) ;
2016-08-30 12:23:02 +02:00
2021-09-06 23:12:00 +02:00
for ( int mat = 0 ; mat < mp - > count ; mat + + )
2023-10-19 23:36:19 +02:00
code_matrix_coeffs ( ctx , s , dp , mat ) ;
2016-08-30 12:23:02 +02:00
}
/** Min and max values that can be encoded with each codebook. The values for
* the third codebook take into account the fact that the sign shift for this
* codebook is outside the coded value , so it has one more bit of precision .
* It should actually be - 7 - > 7 , shifted down by 0.5 .
*/
2023-10-03 12:43:14 +02:00
static const int8_t codebook_extremes [ 3 ] [ 2 ] = {
2016-08-30 12:23:02 +02:00
{ - 9 , 8 } , { - 8 , 7 } , { - 15 , 14 } ,
} ;
/** Determines the amount of bits needed to encode the samples using no
* codebooks and a specified offset .
*/
static void no_codebook_bits_offset ( MLPEncodeContext * ctx ,
2023-10-13 12:23:34 +02:00
DecodingParams * dp ,
2023-10-19 23:36:19 +02:00
int channel , int32_t offset ,
2016-08-30 12:23:02 +02:00
int32_t min , int32_t max ,
BestOffset * bo )
{
2020-01-24 12:33:32 +02:00
int32_t unsign = 0 ;
2016-08-30 12:23:02 +02:00
int lsb_bits ;
min - = offset ;
max - = offset ;
lsb_bits = FFMAX ( number_sbits ( min ) , number_sbits ( max ) ) - 1 ;
lsb_bits + = ! ! lsb_bits ;
2020-01-24 12:33:32 +02:00
if ( lsb_bits > 0 )
2023-10-03 12:43:14 +02:00
unsign = 1U < < ( lsb_bits - 1 ) ;
2016-08-30 12:23:02 +02:00
bo - > offset = offset ;
bo - > lsb_bits = lsb_bits ;
bo - > bitcount = lsb_bits * dp - > blocksize ;
bo - > min = offset - unsign + 1 ;
bo - > max = offset + unsign ;
}
/** Determines the least amount of bits needed to encode the samples using no
* codebooks .
*/
static void no_codebook_bits ( MLPEncodeContext * ctx ,
2023-10-13 12:23:34 +02:00
DecodingParams * dp ,
2023-10-19 23:36:19 +02:00
int channel ,
2016-08-30 12:23:02 +02:00
int32_t min , int32_t max ,
BestOffset * bo )
{
2023-10-03 12:43:14 +02:00
int32_t offset , unsign = 0 ;
uint8_t lsb_bits ;
2016-08-30 12:23:02 +02:00
/* Set offset inside huffoffset's boundaries by adjusting extremes
* so that more bits are used , thus shifting the offset . */
if ( min < HUFF_OFFSET_MIN )
max = FFMAX ( max , 2 * HUFF_OFFSET_MIN - min + 1 ) ;
if ( max > HUFF_OFFSET_MAX )
min = FFMIN ( min , 2 * HUFF_OFFSET_MAX - max - 1 ) ;
2023-10-03 12:43:14 +02:00
lsb_bits = FFMAX ( number_sbits ( min ) , number_sbits ( max ) ) ;
2016-08-30 12:23:02 +02:00
2020-01-24 12:33:32 +02:00
if ( lsb_bits > 0 )
unsign = 1 < < ( lsb_bits - 1 ) ;
2016-08-30 12:23:02 +02:00
/* If all samples are the same (lsb_bits == 0), offset must be
* adjusted because of sign_shift . */
2023-10-03 12:43:14 +02:00
offset = min + ( max - min ) / 2 + ! ! lsb_bits ;
2016-08-30 12:23:02 +02:00
bo - > offset = offset ;
bo - > lsb_bits = lsb_bits ;
bo - > bitcount = lsb_bits * dp - > blocksize ;
bo - > min = max - unsign + 1 ;
bo - > max = min + unsign ;
2023-10-03 12:43:14 +02:00
bo - > min = FFMAX ( bo - > min , HUFF_OFFSET_MIN ) ;
bo - > max = FFMIN ( bo - > max , HUFF_OFFSET_MAX ) ;
2016-08-30 12:23:02 +02:00
}
/** Determines the least amount of bits needed to encode the samples using a
* given codebook and a given offset .
*/
static inline void codebook_bits_offset ( MLPEncodeContext * ctx ,
2023-10-13 12:23:34 +02:00
DecodingParams * dp ,
2023-10-19 23:36:19 +02:00
int channel , int codebook ,
2016-08-30 12:23:02 +02:00
int32_t sample_min , int32_t sample_max ,
2023-10-03 12:43:14 +02:00
int32_t offset , BestOffset * bo )
2016-08-30 12:23:02 +02:00
{
int32_t codebook_min = codebook_extremes [ codebook ] [ 0 ] ;
int32_t codebook_max = codebook_extremes [ codebook ] [ 1 ] ;
2023-10-13 12:23:34 +02:00
int32_t * sample_buffer = dp - > sample_buffer [ channel ] ;
2016-08-30 12:23:02 +02:00
int codebook_offset = 7 + ( 2 - codebook ) ;
int32_t unsign_offset = offset ;
2023-10-03 12:43:14 +02:00
uint32_t bitcount = 0 ;
int lsb_bits = 0 ;
2016-08-30 12:23:02 +02:00
int offset_min = INT_MAX , offset_max = INT_MAX ;
int unsign , mask ;
sample_min - = offset ;
sample_max - = offset ;
while ( sample_min < codebook_min | | sample_max > codebook_max ) {
lsb_bits + + ;
sample_min > > = 1 ;
sample_max > > = 1 ;
}
unsign = 1 < < lsb_bits ;
mask = unsign - 1 ;
if ( codebook = = 2 ) {
unsign_offset - = unsign ;
lsb_bits + + ;
}
2021-09-06 23:12:00 +02:00
for ( int i = 0 ; i < dp - > blocksize ; i + + ) {
2023-10-13 12:23:34 +02:00
int32_t sample = sample_buffer [ i ] > > dp - > quant_step_size [ channel ] ;
2016-08-30 12:23:02 +02:00
int temp_min , temp_max ;
sample - = unsign_offset ;
temp_min = sample & mask ;
if ( temp_min < offset_min )
offset_min = temp_min ;
temp_max = unsign - temp_min - 1 ;
if ( temp_max < offset_max )
offset_max = temp_max ;
sample > > = lsb_bits ;
bitcount + = ff_mlp_huffman_tables [ codebook ] [ sample + codebook_offset ] [ 1 ] ;
}
bo - > offset = offset ;
bo - > lsb_bits = lsb_bits ;
bo - > bitcount = lsb_bits * dp - > blocksize + bitcount ;
bo - > min = FFMAX ( offset - offset_min , HUFF_OFFSET_MIN ) ;
bo - > max = FFMIN ( offset + offset_max , HUFF_OFFSET_MAX ) ;
}
/** Determines the least amount of bits needed to encode the samples using a
* given codebook . Searches for the best offset to minimize the bits .
*/
static inline void codebook_bits ( MLPEncodeContext * ctx ,
2023-10-13 12:23:34 +02:00
DecodingParams * dp ,
2023-10-19 23:36:19 +02:00
int channel , int codebook ,
2016-08-30 12:23:02 +02:00
int offset , int32_t min , int32_t max ,
BestOffset * bo , int direction )
{
2023-10-03 12:43:14 +02:00
uint32_t previous_count = UINT32_MAX ;
2016-08-30 12:23:02 +02:00
int offset_min , offset_max ;
int is_greater = 0 ;
offset_min = FFMAX ( min , HUFF_OFFSET_MIN ) ;
offset_max = FFMIN ( max , HUFF_OFFSET_MAX ) ;
2020-01-24 12:33:31 +02:00
while ( offset < = offset_max & & offset > = offset_min ) {
2016-08-30 12:23:02 +02:00
BestOffset temp_bo ;
2023-10-13 12:23:34 +02:00
codebook_bits_offset ( ctx , dp , channel , codebook ,
2016-08-30 12:23:02 +02:00
min , max , offset ,
& temp_bo ) ;
if ( temp_bo . bitcount < previous_count ) {
if ( temp_bo . bitcount < bo - > bitcount )
* bo = temp_bo ;
is_greater = 0 ;
} else if ( + + is_greater > = ctx - > max_codebook_search )
break ;
previous_count = temp_bo . bitcount ;
if ( direction ) {
offset = temp_bo . max + 1 ;
} else {
offset = temp_bo . min - 1 ;
}
}
}
/** Determines the least amount of bits needed to encode the samples using
* any or no codebook .
*/
2023-10-19 23:36:19 +02:00
static void determine_bits ( MLPEncodeContext * ctx , MLPSubstream * s )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
RestartHeader * rh = s - > cur_restart_header ;
2023-10-13 12:23:34 +02:00
for ( unsigned int index = 0 ; index < ctx - > number_of_subblocks ; index + + ) {
2023-10-19 23:36:19 +02:00
DecodingParams * dp = & s - > b [ index ] . decoding_params ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
for ( int ch = rh - > min_channel ; ch < = rh - > max_channel ; ch + + ) {
ChannelParams * cp = & s - > b [ index ] . channel_params [ ch ] ;
int32_t * sample_buffer = dp - > sample_buffer [ ch ] ;
2023-10-13 12:23:34 +02:00
int32_t min = INT32_MAX , max = INT32_MIN ;
int no_filters_used = ! cp - > filter_params [ FIR ] . order ;
int average = 0 ;
int offset = 0 ;
/* Determine extremes and average. */
for ( int i = 0 ; i < dp - > blocksize ; i + + ) {
2023-10-19 23:36:19 +02:00
int32_t sample = sample_buffer [ i ] > > dp - > quant_step_size [ ch ] ;
2023-10-13 12:23:34 +02:00
if ( sample < min )
min = sample ;
if ( sample > max )
max = sample ;
average + = sample ;
}
average / = dp - > blocksize ;
/* If filtering is used, we always set the offset to zero, otherwise
* we search for the offset that minimizes the bitcount . */
if ( no_filters_used ) {
2023-10-19 23:36:19 +02:00
no_codebook_bits ( ctx , dp , ch , min , max , & s - > b [ index ] . best_offset [ ch ] [ 0 ] ) ;
2023-10-13 12:23:34 +02:00
offset = av_clip ( average , HUFF_OFFSET_MIN , HUFF_OFFSET_MAX ) ;
} else {
2023-10-19 23:36:19 +02:00
no_codebook_bits_offset ( ctx , dp , ch , offset , min , max , & s - > b [ index ] . best_offset [ ch ] [ 0 ] ) ;
2023-10-13 12:23:34 +02:00
}
2016-08-30 12:23:02 +02:00
2023-10-13 12:23:34 +02:00
for ( int i = 1 ; i < NUM_CODEBOOKS ; i + + ) {
BestOffset temp_bo = { 0 , UINT32_MAX , 0 , 0 , 0 , } ;
int32_t offset_max ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
codebook_bits_offset ( ctx , dp , ch , i - 1 ,
2023-10-13 12:23:34 +02:00
min , max , offset ,
& temp_bo ) ;
2016-08-30 12:23:02 +02:00
2023-10-13 12:23:34 +02:00
if ( no_filters_used ) {
offset_max = temp_bo . max ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
codebook_bits ( ctx , dp , ch , i - 1 , temp_bo . min - 1 ,
2023-10-13 12:23:34 +02:00
min , max , & temp_bo , 0 ) ;
2023-10-19 23:36:19 +02:00
codebook_bits ( ctx , dp , ch , i - 1 , offset_max + 1 ,
2023-10-13 12:23:34 +02:00
min , max , & temp_bo , 1 ) ;
}
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
s - > b [ index ] . best_offset [ ch ] [ i ] = temp_bo ;
2023-10-13 12:23:34 +02:00
}
2016-08-30 12:23:02 +02:00
}
}
}
/****************************************************************************
* * * * * * * * * * * * * * * Functions that process the data in some way * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# define SAMPLE_MAX(bitdepth) ((1 << (bitdepth - 1)) - 1)
# define SAMPLE_MIN(bitdepth) (~SAMPLE_MAX(bitdepth))
2020-02-02 21:02:59 +02:00
# define MSB_MASK(bits) (-(int)(1u << (bits)))
2016-08-30 12:23:02 +02:00
/** Applies the filter to the current samples, and saves the residual back
* into the samples buffer . If the filter is too bad and overflows the
2020-01-24 12:33:33 +02:00
* maximum amount of bits allowed ( 24 ) , the samples buffer is left as is and
2016-08-30 12:23:02 +02:00
* the function returns - 1.
*/
2023-10-19 23:36:19 +02:00
static int apply_filter ( MLPEncodeContext * ctx , MLPSubstream * s , int channel )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
DecodingParams * dp = & s - > b [ 1 ] . decoding_params ;
ChannelParams * cp = & s - > b [ 1 ] . channel_params [ channel ] ;
2023-10-03 12:43:14 +02:00
FilterParams * fp [ NUM_FILTERS ] = { & cp - > filter_params [ FIR ] ,
& cp - > filter_params [ IIR ] , } ;
const uint8_t codebook = cp - > codebook ;
2023-10-13 12:23:34 +02:00
int32_t mask = MSB_MASK ( dp - > quant_step_size [ channel ] ) ;
2023-10-19 23:36:19 +02:00
int32_t * sample_buffer = s - > b [ 0 ] . decoding_params . sample_buffer [ channel ] ;
2016-08-30 12:23:02 +02:00
unsigned int filter_shift = fp [ FIR ] - > shift ;
2023-10-13 12:23:34 +02:00
int32_t * filter_state [ NUM_FILTERS ] = { ctx - > filter_state [ FIR ] ,
ctx - > filter_state [ IIR ] , } ;
int i , j = 1 , k = 0 ;
2016-08-30 12:23:02 +02:00
2023-10-13 12:23:34 +02:00
for ( i = 0 ; i < 8 ; i + + ) {
filter_state [ FIR ] [ i ] = sample_buffer [ i ] ;
filter_state [ IIR ] [ i ] = sample_buffer [ i ] ;
2016-08-30 12:23:02 +02:00
}
2023-10-13 12:23:34 +02:00
while ( 1 ) {
2023-10-19 23:36:19 +02:00
int32_t * sample_buffer = s - > b [ j ] . decoding_params . sample_buffer [ channel ] ;
unsigned int blocksize = s - > b [ j ] . decoding_params . blocksize ;
2023-10-13 12:23:34 +02:00
int32_t sample , residual ;
2016-08-30 12:23:02 +02:00
int64_t accum = 0 ;
2023-10-13 12:23:34 +02:00
if ( ! blocksize )
break ;
2016-08-30 12:23:02 +02:00
2021-09-06 23:12:00 +02:00
for ( int filter = 0 ; filter < NUM_FILTERS ; filter + + ) {
2023-10-03 12:43:14 +02:00
int32_t * fcoeff = cp - > coeff [ filter ] ;
2021-09-06 23:12:00 +02:00
for ( unsigned int order = 0 ; order < fp [ filter ] - > order ; order + + )
2023-10-13 12:23:34 +02:00
accum + = ( int64_t ) filter_state [ filter ] [ i - 1 - order ] *
fcoeff [ order ] ;
2016-08-30 12:23:02 +02:00
}
2023-10-13 12:23:34 +02:00
sample = sample_buffer [ k ] ;
2016-08-30 12:23:02 +02:00
accum > > = filter_shift ;
residual = sample - ( accum & mask ) ;
2023-10-03 12:43:14 +02:00
if ( ( codebook > 0 ) & &
( residual < SAMPLE_MIN ( 24 ) | |
residual > SAMPLE_MAX ( 24 ) ) )
return - 1 ;
2016-08-30 12:23:02 +02:00
2023-10-13 12:23:34 +02:00
filter_state [ FIR ] [ i ] = sample ;
filter_state [ IIR ] [ i ] = residual ;
2016-08-30 12:23:02 +02:00
2023-10-13 12:23:34 +02:00
i + + ;
k + + ;
if ( k > = blocksize ) {
k = 0 ;
j + + ;
if ( j > ctx - > cur_restart_interval )
break ;
}
2016-08-30 12:23:02 +02:00
}
2023-10-13 12:23:34 +02:00
for ( int l = 0 , j = 0 ; j < = ctx - > cur_restart_interval ; j + + ) {
2023-10-19 23:36:19 +02:00
int32_t * sample_buffer = s - > b [ j ] . decoding_params . sample_buffer [ channel ] ;
unsigned int blocksize = s - > b [ j ] . decoding_params . blocksize ;
2016-08-30 12:23:02 +02:00
2023-10-13 12:23:34 +02:00
for ( int i = 0 ; i < blocksize ; i + + , l + + )
sample_buffer [ i ] = filter_state [ IIR ] [ l ] ;
2016-08-30 12:23:02 +02:00
}
2023-10-03 12:43:14 +02:00
return 0 ;
2016-08-30 12:23:02 +02:00
}
2023-10-19 23:36:19 +02:00
static void apply_filters ( MLPEncodeContext * ctx , MLPSubstream * s )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
RestartHeader * rh = s - > cur_restart_header ;
2016-08-30 12:23:02 +02:00
2023-10-18 20:28:04 +02:00
for ( int ch = rh - > min_channel ; ch < = rh - > max_channel ; ch + + ) {
2023-10-25 15:32:53 +02:00
while ( apply_filter ( ctx , s , ch ) < 0 ) {
/* Filter is horribly wrong. Retry. */
2023-10-19 23:36:19 +02:00
set_filter ( ctx , s , ch , 1 ) ;
2016-08-30 12:23:02 +02:00
}
}
}
/** Generates two noise channels worth of data. */
2023-10-19 23:36:19 +02:00
static void generate_2_noise_channels ( MLPEncodeContext * ctx , MLPSubstream * s )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
RestartHeader * rh = s - > cur_restart_header ;
2016-08-30 12:23:02 +02:00
uint32_t seed = rh - > noisegen_seed ;
2023-10-13 12:23:34 +02:00
for ( unsigned int j = 0 ; j < = ctx - > cur_restart_interval ; j + + ) {
2023-10-19 23:36:19 +02:00
DecodingParams * dp = & s - > b [ j ] . decoding_params ;
2023-10-13 12:23:34 +02:00
int32_t * sample_buffer2 = dp - > sample_buffer [ ctx - > num_channels - 2 ] ;
int32_t * sample_buffer1 = dp - > sample_buffer [ ctx - > num_channels - 1 ] ;
2016-08-30 12:23:02 +02:00
2023-10-13 12:23:34 +02:00
for ( unsigned int i = 0 ; i < dp - > blocksize ; i + + ) {
uint16_t seed_shr7 = seed > > 7 ;
sample_buffer2 [ i ] = ( ( int8_t ) ( seed > > 15 ) ) * ( 1 < < rh - > noise_shift ) ;
sample_buffer1 [ i ] = ( ( int8_t ) seed_shr7 ) * ( 1 < < rh - > noise_shift ) ;
2016-08-30 12:23:02 +02:00
2023-10-13 12:23:34 +02:00
seed = ( seed < < 16 ) ^ seed_shr7 ^ ( seed_shr7 < < 5 ) ;
}
2016-08-30 12:23:02 +02:00
}
rh - > noisegen_seed = seed & ( ( 1 < < 24 ) - 1 ) ;
}
/** Rematrixes all channels using chosen coefficients. */
2023-10-19 23:36:19 +02:00
static void rematrix_channels ( MLPEncodeContext * ctx , MLPSubstream * s )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
RestartHeader * rh = s - > cur_restart_header ;
DecodingParams * dp1 = & s - > b [ 1 ] . decoding_params ;
2023-10-13 12:23:34 +02:00
MatrixParams * mp1 = & dp1 - > matrix_params ;
2023-10-19 23:36:19 +02:00
const int maxchan = rh - > max_matrix_channel ;
2023-10-16 12:42:30 +02:00
int32_t orig_samples [ MAX_NCHANNELS ] ;
int32_t rematrix_samples [ MAX_NCHANNELS ] ;
2023-10-13 12:23:34 +02:00
uint8_t lsb_bypass [ MAX_MATRICES ] = { 0 } ;
2016-08-30 12:23:02 +02:00
2023-10-13 12:23:34 +02:00
for ( unsigned int j = 0 ; j < = ctx - > cur_restart_interval ; j + + ) {
2023-10-19 23:36:19 +02:00
DecodingParams * dp = & s - > b [ j ] . decoding_params ;
2023-10-13 12:23:34 +02:00
MatrixParams * mp = & dp - > matrix_params ;
2016-08-30 12:23:02 +02:00
2023-10-13 12:23:34 +02:00
for ( unsigned int i = 0 ; i < dp - > blocksize ; i + + ) {
2023-10-19 23:36:19 +02:00
for ( int ch = 0 ; ch < = maxchan ; ch + + )
2023-10-16 12:42:30 +02:00
orig_samples [ ch ] = rematrix_samples [ ch ] = dp - > sample_buffer [ ch ] [ i ] ;
2016-08-30 12:23:02 +02:00
2023-10-16 12:42:30 +02:00
for ( int mat = 0 ; mat < mp1 - > count ; mat + + ) {
2023-10-13 12:23:34 +02:00
unsigned int outch = mp1 - > outch [ mat ] ;
int64_t accum = 0 ;
2023-10-16 12:42:30 +02:00
for ( int ch = 0 ; ch < = maxchan ; ch + + ) {
int32_t sample = rematrix_samples [ ch ] ;
2023-10-13 12:23:34 +02:00
accum + = ( int64_t ) sample * mp1 - > forco [ mat ] [ ch ] ;
}
2023-10-16 12:42:30 +02:00
rematrix_samples [ outch ] = accum > > 14 ;
2016-08-30 12:23:02 +02:00
}
2023-10-19 23:36:19 +02:00
for ( int ch = 0 ; ch < = maxchan ; ch + + )
2023-10-16 12:42:30 +02:00
dp - > sample_buffer [ ch ] [ i ] = rematrix_samples [ ch ] ;
2023-10-13 12:23:34 +02:00
for ( unsigned int mat = 0 ; mat < mp1 - > count ; mat + + ) {
int8_t * bypassed_lsbs = mp - > bypassed_lsbs [ mat ] ;
unsigned int outch = mp1 - > outch [ mat ] ;
int64_t accum = 0 ;
int8_t bit ;
2023-10-19 23:36:19 +02:00
for ( int ch = 0 ; ch < = maxchan ; ch + + ) {
2023-10-16 12:42:30 +02:00
int32_t sample = rematrix_samples [ ch ] ;
2023-10-13 12:23:34 +02:00
accum + = ( int64_t ) sample * mp1 - > coeff [ mat ] [ ch ] ;
}
2023-10-16 12:42:30 +02:00
rematrix_samples [ outch ] = accum > > 14 ;
bit = rematrix_samples [ outch ] ! = orig_samples [ outch ] ;
2023-10-13 12:23:34 +02:00
bypassed_lsbs [ i ] = bit ;
lsb_bypass [ mat ] | = bit ;
}
2016-08-30 12:23:02 +02:00
}
}
2023-10-13 12:23:34 +02:00
for ( unsigned int mat = 0 ; mat < mp1 - > count ; mat + + )
mp1 - > lsb_bypass [ mat ] = lsb_bypass [ mat ] ;
2016-08-30 12:23:02 +02:00
}
/****************************************************************************
* * * * Functions that deal with determining the best parameters and output * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2023-10-03 12:43:14 +02:00
typedef struct PathCounter {
2023-10-09 23:40:23 +02:00
char path [ MAX_HEADER_INTERVAL + 2 ] ;
2020-12-26 19:16:21 +02:00
int cur_idx ;
2023-10-03 12:43:14 +02:00
uint32_t bitcount ;
2016-08-30 12:23:02 +02:00
} PathCounter ;
# define CODEBOOK_CHANGE_BITS 21
static void clear_path_counter ( PathCounter * path_counter )
{
2020-12-26 19:16:21 +02:00
memset ( path_counter , 0 , ( NUM_CODEBOOKS + 1 ) * sizeof ( * path_counter ) ) ;
2016-08-30 12:23:02 +02:00
}
2020-12-03 01:33:27 +02:00
static int compare_best_offset ( const BestOffset * prev , const BestOffset * cur )
2016-08-30 12:23:02 +02:00
{
2021-09-05 00:27:27 +02:00
return prev - > lsb_bits ! = cur - > lsb_bits ;
2016-08-30 12:23:02 +02:00
}
2023-10-19 23:36:19 +02:00
static uint32_t best_codebook_path_cost ( MLPEncodeContext * ctx , MLPSubstream * s ,
int channel ,
PathCounter * src , int cur_codebook )
2016-08-30 12:23:02 +02:00
{
2020-12-26 19:16:21 +02:00
int idx = src - > cur_idx ;
2023-10-19 23:36:19 +02:00
const BestOffset * cur_bo = s - > b [ idx ] . best_offset [ channel ] ,
* prev_bo = idx ? s - > b [ idx - 1 ] . best_offset [ channel ] :
2020-12-03 01:33:27 +02:00
restart_best_offset ;
2023-10-03 12:43:14 +02:00
uint32_t bitcount = src - > bitcount ;
2020-12-26 19:16:21 +02:00
int prev_codebook = src - > path [ idx ] ;
2016-08-30 12:23:02 +02:00
bitcount + = cur_bo [ cur_codebook ] . bitcount ;
if ( prev_codebook ! = cur_codebook | |
compare_best_offset ( & prev_bo [ prev_codebook ] , & cur_bo [ cur_codebook ] ) )
bitcount + = CODEBOOK_CHANGE_BITS ;
return bitcount ;
}
2023-10-19 23:36:19 +02:00
static void set_best_codebook ( MLPEncodeContext * ctx , MLPSubstream * s )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
RestartHeader * rh = s - > cur_restart_header ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
for ( int channel = rh - > min_channel ; channel < = rh - > max_channel ; channel + + ) {
2020-12-03 01:33:27 +02:00
const BestOffset * prev_bo = restart_best_offset ;
BestOffset * cur_bo ;
2016-08-30 12:23:02 +02:00
PathCounter path_counter [ NUM_CODEBOOKS + 1 ] ;
unsigned int best_codebook ;
char * best_path ;
clear_path_counter ( path_counter ) ;
2021-09-06 23:12:00 +02:00
for ( unsigned int index = 0 ; index < ctx - > number_of_subblocks ; index + + ) {
2023-10-03 12:43:14 +02:00
uint32_t best_bitcount = UINT32_MAX ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
cur_bo = s - > b [ index ] . best_offset [ channel ] ;
2016-08-30 12:23:02 +02:00
2021-09-06 23:12:00 +02:00
for ( unsigned int codebook = 0 ; codebook < NUM_CODEBOOKS ; codebook + + ) {
2023-10-03 12:43:14 +02:00
uint32_t prev_best_bitcount = UINT32_MAX ;
2016-08-30 12:23:02 +02:00
2021-09-06 23:12:00 +02:00
for ( unsigned int last_best = 0 ; last_best < 2 ; last_best + + ) {
2016-08-30 12:23:02 +02:00
PathCounter * dst_path = & path_counter [ codebook ] ;
PathCounter * src_path ;
2023-10-03 12:43:14 +02:00
uint32_t temp_bitcount ;
2016-08-30 12:23:02 +02:00
/* First test last path with same headers,
* then with last best . */
if ( last_best ) {
src_path = & path_counter [ NUM_CODEBOOKS ] ;
} else {
if ( compare_best_offset ( & prev_bo [ codebook ] , & cur_bo [ codebook ] ) )
continue ;
else
src_path = & path_counter [ codebook ] ;
}
2023-10-19 23:36:19 +02:00
temp_bitcount = best_codebook_path_cost ( ctx , s , channel , src_path , codebook ) ;
2016-08-30 12:23:02 +02:00
if ( temp_bitcount < best_bitcount ) {
best_bitcount = temp_bitcount ;
best_codebook = codebook ;
}
if ( temp_bitcount < prev_best_bitcount ) {
prev_best_bitcount = temp_bitcount ;
if ( src_path ! = dst_path )
memcpy ( dst_path , src_path , sizeof ( PathCounter ) ) ;
2020-12-26 19:16:21 +02:00
if ( dst_path - > cur_idx < FF_ARRAY_ELEMS ( dst_path - > path ) - 1 )
dst_path - > path [ + + dst_path - > cur_idx ] = codebook ;
2016-08-30 12:23:02 +02:00
dst_path - > bitcount = temp_bitcount ;
}
}
}
prev_bo = cur_bo ;
memcpy ( & path_counter [ NUM_CODEBOOKS ] , & path_counter [ best_codebook ] , sizeof ( PathCounter ) ) ;
}
best_path = path_counter [ NUM_CODEBOOKS ] . path + 1 ;
/* Update context. */
2021-09-06 23:12:00 +02:00
for ( unsigned int index = 0 ; index < ctx - > number_of_subblocks ; index + + ) {
2023-10-19 23:36:19 +02:00
ChannelParams * cp = & s - > b [ index ] . channel_params [ channel ] ;
DecodingParams * dp = & s - > b [ index ] . decoding_params ;
2016-08-30 12:23:02 +02:00
2020-12-26 19:16:21 +02:00
best_codebook = * best_path + + ;
2023-10-19 23:36:19 +02:00
cur_bo = & s - > b [ index ] . best_offset [ channel ] [ best_codebook ] ;
2016-08-30 12:23:02 +02:00
2020-01-24 12:33:34 +02:00
cp - > huff_offset = cur_bo - > offset ;
cp - > huff_lsbs = cur_bo - > lsb_bits + dp - > quant_step_size [ channel ] ;
cp - > codebook = best_codebook ;
2016-08-30 12:23:02 +02:00
}
}
}
/** Analyzes all collected bitcounts and selects the best parameters for each
* individual access unit .
* TODO This is just a stub !
*/
2023-10-19 23:36:19 +02:00
static void set_major_params ( MLPEncodeContext * ctx , MLPSubstream * s )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
RestartHeader * rh = s - > cur_restart_header ;
2023-10-03 12:43:14 +02:00
uint8_t max_huff_lsbs = 0 , max_output_bits = 0 ;
2023-10-20 16:28:08 +02:00
int8_t max_shift = 0 ;
2021-09-06 23:46:23 +02:00
2023-10-19 23:36:19 +02:00
for ( int index = 0 ; index < s - > b [ ctx - > restart_intervals - 1 ] . seq_size ; index + + ) {
memcpy ( & s - > b [ index ] . major_decoding_params ,
& s - > b [ index ] . decoding_params , sizeof ( DecodingParams ) ) ;
2023-10-20 16:28:08 +02:00
for ( int ch = 0 ; ch < = rh - > max_matrix_channel ; ch + + ) {
int8_t shift = s - > b [ index ] . decoding_params . output_shift [ ch ] ;
max_shift = FFMAX ( max_shift , shift ) ;
}
2023-10-19 23:36:19 +02:00
for ( int ch = rh - > min_channel ; ch < = rh - > max_channel ; ch + + ) {
uint8_t huff_lsbs = s - > b [ index ] . channel_params [ ch ] . huff_lsbs ;
2023-10-20 16:28:08 +02:00
max_huff_lsbs = FFMAX ( max_huff_lsbs , huff_lsbs ) ;
2023-10-19 23:36:19 +02:00
memcpy ( & s - > b [ index ] . major_channel_params [ ch ] ,
& s - > b [ index ] . channel_params [ ch ] ,
2021-09-06 23:46:23 +02:00
sizeof ( ChannelParams ) ) ;
2016-08-30 12:23:02 +02:00
}
}
rh - > max_huff_lsbs = max_huff_lsbs ;
2023-10-20 16:28:08 +02:00
rh - > max_shift = max_shift ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
for ( int index = 0 ; index < ctx - > number_of_frames ; index + + )
if ( max_output_bits < s - > b [ index ] . max_output_bits )
max_output_bits = s - > b [ index ] . max_output_bits ;
2016-08-30 12:23:02 +02:00
rh - > max_output_bits = max_output_bits ;
2023-10-19 23:36:19 +02:00
s - > cur_restart_header = & s - > restart_header ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
for ( int index = 0 ; index < = ctx - > cur_restart_interval ; index + + )
s - > b [ index ] . major_params_changed = compare_decoding_params ( ctx , s , index ) ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
s - > major_filter_state_subblock = 1 ;
s - > major_cur_subblock_index = 0 ;
2016-08-30 12:23:02 +02:00
}
2023-10-19 23:36:19 +02:00
static void analyze_sample_buffer ( MLPEncodeContext * ctx , MLPSubstream * s )
2016-08-30 12:23:02 +02:00
{
2023-10-19 23:36:19 +02:00
s - > cur_restart_header = & s - > restart_header ;
2016-08-30 12:23:02 +02:00
2021-09-06 23:46:23 +02:00
/* Copy frame_size from frames 0...max to decoding_params 1...max + 1
* decoding_params [ 0 ] is for the filter state subblock .
*/
for ( unsigned int index = 0 ; index < ctx - > number_of_frames ; index + + ) {
2023-10-19 23:36:19 +02:00
DecodingParams * dp = & s - > b [ index + 1 ] . decoding_params ;
2021-09-06 23:46:23 +02:00
dp - > blocksize = ctx - > avctx - > frame_size ;
2016-08-30 12:23:02 +02:00
}
2021-09-06 23:46:23 +02:00
/* The official encoder seems to always encode a filter state subblock
* even if there are no filters . TODO check if it is possible to skip
* the filter state subblock for no filters .
*/
2023-10-19 23:36:19 +02:00
s - > b [ 0 ] . decoding_params . blocksize = 8 ;
s - > b [ 1 ] . decoding_params . blocksize - = 8 ;
2021-09-06 23:46:23 +02:00
2023-10-19 23:36:19 +02:00
input_to_sample_buffer ( ctx , s ) ;
2023-10-20 16:28:08 +02:00
determine_output_shift ( ctx , s ) ;
2023-10-19 23:36:19 +02:00
generate_2_noise_channels ( ctx , s ) ;
lossless_matrix_coeffs ( ctx , s ) ;
rematrix_channels ( ctx , s ) ;
determine_quant_step_size ( ctx , s ) ;
determine_filters ( ctx , s ) ;
apply_filters ( ctx , s ) ;
2023-10-13 12:23:34 +02:00
2023-10-19 23:36:19 +02:00
copy_restart_frame_params ( ctx , s ) ;
2023-10-13 12:23:34 +02:00
2023-10-19 23:36:19 +02:00
determine_bits ( ctx , s ) ;
2021-09-06 23:46:23 +02:00
2023-10-19 23:36:19 +02:00
set_best_codebook ( ctx , s ) ;
2016-08-30 12:23:02 +02:00
}
2023-10-19 23:36:19 +02:00
static void process_major_frame ( MLPEncodeContext * ctx , MLPSubstream * s )
2016-08-30 12:23:02 +02:00
{
ctx - > number_of_frames = ctx - > major_number_of_frames ;
2023-10-19 23:36:19 +02:00
s - > cur_restart_header = & s - > restart_header ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
generate_2_noise_channels ( ctx , s ) ;
rematrix_channels ( ctx , s ) ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
apply_filters ( ctx , s ) ;
2016-08-30 12:23:02 +02:00
}
/****************************************************************************/
static int mlp_encode_frame ( AVCodecContext * avctx , AVPacket * avpkt ,
const AVFrame * frame , int * got_packet )
{
MLPEncodeContext * ctx = avctx - > priv_data ;
2021-09-05 11:36:50 +02:00
int bytes_written = 0 ;
2019-05-29 09:36:27 +02:00
int channels = avctx - > ch_layout . nb_channels ;
2016-08-30 12:23:02 +02:00
int restart_frame , ret ;
2022-07-26 09:09:44 +02:00
const uint8_t * data ;
2016-08-30 12:23:02 +02:00
2022-04-12 17:57:58 +02:00
if ( ! frame & & ! ctx - > last_frames )
ctx - > last_frames = ( ctx - > afq . remaining_samples + avctx - > frame_size - 1 ) / avctx - > frame_size ;
2021-08-30 22:27:33 +02:00
if ( ! frame & & ! ctx - > last_frames - - )
return 0 ;
2019-05-29 09:36:27 +02:00
if ( ( ret = ff_alloc_packet ( avctx , avpkt , 87500 * channels ) ) < 0 )
2016-08-30 12:23:02 +02:00
return ret ;
2021-08-30 22:27:33 +02:00
if ( frame ) {
/* add current frame to queue */
if ( ( ret = ff_af_queue_add ( & ctx - > afq , frame ) ) < 0 )
return ret ;
}
2016-08-30 12:23:02 +02:00
2021-08-30 22:27:33 +02:00
data = frame ? frame - > data [ 0 ] : NULL ;
2016-08-30 12:23:02 +02:00
2023-10-13 12:23:34 +02:00
ctx - > frame_index = avctx - > frame_num % ctx - > cur_restart_interval ;
2016-08-30 12:23:02 +02:00
2023-10-13 12:23:34 +02:00
if ( avctx - > frame_num < ctx - > cur_restart_interval ) {
2021-08-30 22:27:33 +02:00
if ( data )
2016-08-30 12:23:02 +02:00
goto input_and_return ;
}
restart_frame = ! ctx - > frame_index ;
if ( restart_frame ) {
2021-09-21 21:34:32 +02:00
avpkt - > flags | = AV_PKT_FLAG_KEY ;
2023-10-19 23:36:19 +02:00
for ( int n = 0 ; n < ctx - > num_substreams ; n + + )
set_major_params ( ctx , & ctx - > s [ n ] ) ;
2023-10-13 12:23:34 +02:00
if ( ctx - > min_restart_interval ! = ctx - > cur_restart_interval )
2023-10-19 23:36:19 +02:00
process_major_frame ( ctx , & ctx - > s [ 0 ] ) ;
2016-08-30 12:23:02 +02:00
}
bytes_written = write_access_unit ( ctx , avpkt - > data , avpkt - > size , restart_frame ) ;
2022-09-17 16:28:05 +02:00
ctx - > output_timing + = avctx - > frame_size ;
ctx - > input_timing + = avctx - > frame_size ;
2016-08-30 12:23:02 +02:00
input_and_return :
2022-04-13 14:07:48 +02:00
if ( frame ) {
2021-08-30 22:27:33 +02:00
ctx - > shorten_by = avctx - > frame_size - frame - > nb_samples ;
2022-04-13 14:07:48 +02:00
ctx - > next_major_frame_size + = avctx - > frame_size ;
ctx - > next_major_number_of_frames + + ;
}
2021-08-30 22:27:33 +02:00
if ( data )
2023-10-19 23:36:19 +02:00
for ( int n = 0 ; n < ctx - > num_substreams ; n + + )
input_data ( ctx , & ctx - > s [ n ] , frame - > extended_data , frame - > nb_samples ) ;
2016-08-30 12:23:02 +02:00
restart_frame = ( ctx - > frame_index + 1 ) % ctx - > min_restart_interval ;
if ( ! restart_frame ) {
2021-09-07 18:11:45 +02:00
for ( unsigned int seq_index = 0 ; seq_index < ctx - > restart_intervals ; seq_index + + ) {
2021-09-05 15:23:37 +02:00
unsigned int number_of_samples ;
2016-08-30 12:23:02 +02:00
ctx - > number_of_frames = ctx - > next_major_number_of_frames ;
ctx - > number_of_subblocks = ctx - > next_major_number_of_frames + 1 ;
2021-09-05 15:23:37 +02:00
number_of_samples = avctx - > frame_size * ctx - > number_of_frames ;
2016-08-30 12:23:02 +02:00
2023-10-19 23:36:19 +02:00
for ( int n = 0 ; n < ctx - > num_substreams ; n + + ) {
MLPSubstream * s = & ctx - > s [ n ] ;
for ( int i = 0 ; i < s - > b [ seq_index ] . seq_size ; i + + ) {
clear_channel_params ( s - > b [ i ] . channel_params , channels ) ;
default_decoding_params ( ctx , & s - > b [ i ] . decoding_params ) ;
}
2016-08-30 12:23:02 +02:00
}
2023-10-19 23:36:19 +02:00
if ( number_of_samples > 0 ) {
for ( int n = 0 ; n < ctx - > num_substreams ; n + + )
analyze_sample_buffer ( ctx , & ctx - > s [ n ] ) ;
}
2016-08-30 12:23:02 +02:00
}
2023-10-13 12:23:34 +02:00
if ( ctx - > frame_index = = ( ctx - > cur_restart_interval - 1 ) ) {
2016-08-30 12:23:02 +02:00
ctx - > major_frame_size = ctx - > next_major_frame_size ;
ctx - > next_major_frame_size = 0 ;
ctx - > major_number_of_frames = ctx - > next_major_number_of_frames ;
ctx - > next_major_number_of_frames = 0 ;
}
}
2023-10-13 12:23:34 +02:00
if ( ! frame & & ctx - > last_frames < ctx - > cur_restart_interval - 1 )
2023-01-24 01:35:54 +02:00
avctx - > frame_num + + ;
2021-09-05 11:36:50 +02:00
if ( bytes_written > 0 ) {
2022-04-12 17:57:58 +02:00
ff_af_queue_remove ( & ctx - > afq ,
FFMIN ( avctx - > frame_size , ctx - > afq . remaining_samples ) ,
& avpkt - > pts ,
2021-09-05 12:10:11 +02:00
& avpkt - > duration ) ;
2021-09-05 14:08:08 +02:00
av_shrink_packet ( avpkt , bytes_written ) ;
2021-09-05 11:36:50 +02:00
* got_packet = 1 ;
} else {
* got_packet = 0 ;
}
2016-08-30 12:23:02 +02:00
return 0 ;
}
static av_cold int mlp_encode_close ( AVCodecContext * avctx )
{
MLPEncodeContext * ctx = avctx - > priv_data ;
ff_lpc_end ( & ctx - > lpc_ctx ) ;
ff_af_queue_close ( & ctx - > afq ) ;
return 0 ;
}
2023-10-02 20:02:18 +02:00
# define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
# define OFFSET(x) offsetof(MLPEncodeContext, x)
static const AVOption mlp_options [ ] = {
2023-10-09 23:40:23 +02:00
{ " max_interval " , " Max number of frames between each new header " , OFFSET ( max_restart_interval ) , AV_OPT_TYPE_INT , { . i64 = 16 } , MIN_HEADER_INTERVAL , MAX_HEADER_INTERVAL , FLAGS } ,
2023-10-10 13:44:03 +02:00
{ " lpc_coeff_precision " , " LPC coefficient precision " , OFFSET ( lpc_coeff_precision ) , AV_OPT_TYPE_INT , { . i64 = 15 } , 0 , 15 , FLAGS } ,
2024-02-11 16:41:05 +02:00
{ " lpc_type " , " LPC algorithm " , OFFSET ( lpc_type ) , AV_OPT_TYPE_INT , { . i64 = FF_LPC_TYPE_LEVINSON } , FF_LPC_TYPE_LEVINSON , FF_LPC_TYPE_CHOLESKY , FLAGS , . unit = " lpc_type " } ,
{ " levinson " , NULL , 0 , AV_OPT_TYPE_CONST , { . i64 = FF_LPC_TYPE_LEVINSON } , 0 , 0 , FLAGS , . unit = " lpc_type " } ,
{ " cholesky " , NULL , 0 , AV_OPT_TYPE_CONST , { . i64 = FF_LPC_TYPE_CHOLESKY } , 0 , 0 , FLAGS , . unit = " lpc_type " } ,
2023-10-02 20:02:18 +02:00
{ " 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 } ,
2023-10-09 23:13:45 +02:00
{ " codebook_search " , " Max number of codebook searches " , OFFSET ( max_codebook_search ) , AV_OPT_TYPE_INT , { . i64 = 3 } , 1 , 100 , FLAGS } ,
2024-02-11 16:41:05 +02:00
{ " prediction_order " , " Search method for selecting prediction order " , OFFSET ( prediction_order ) , AV_OPT_TYPE_INT , { . i64 = ORDER_METHOD_EST } , ORDER_METHOD_EST , ORDER_METHOD_SEARCH , FLAGS , . unit = " predm " } ,
{ " estimation " , NULL , 0 , AV_OPT_TYPE_CONST , { . i64 = ORDER_METHOD_EST } , 0 , 0 , FLAGS , . unit = " predm " } ,
{ " search " , NULL , 0 , AV_OPT_TYPE_CONST , { . i64 = ORDER_METHOD_SEARCH } , 0 , 0 , FLAGS , . unit = " predm " } ,
2023-10-16 12:42:30 +02:00
{ " rematrix_precision " , " Rematrix coefficient precision " , OFFSET ( rematrix_precision ) , AV_OPT_TYPE_INT , { . i64 = 1 } , 0 , 14 , FLAGS } ,
2023-10-02 20:02:18 +02:00
{ NULL } ,
} ;
static const AVClass mlp_class = {
. class_name = " mlpenc " ,
2024-01-19 14:33:28 +02:00
. item_name = av_default_item_name ,
2023-10-02 20:02:18 +02:00
. option = mlp_options ,
. version = LIBAVUTIL_VERSION_INT ,
} ;
2016-08-30 12:23:02 +02:00
# if CONFIG_MLP_ENCODER
2022-03-16 22:09:54 +02:00
const FFCodec ff_mlp_encoder = {
. p . name = " mlp " ,
2022-08-29 13:38:02 +02:00
CODEC_LONG_NAME ( " MLP (Meridian Lossless Packing) " ) ,
2022-03-16 22:09:54 +02:00
. p . type = AVMEDIA_TYPE_AUDIO ,
. p . id = AV_CODEC_ID_MLP ,
2021-05-11 20:52:13 +02:00
. p . capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
AV_CODEC_CAP_EXPERIMENTAL ,
2016-08-30 12:23:02 +02:00
. priv_data_size = sizeof ( MLPEncodeContext ) ,
. init = mlp_encode_init ,
2022-03-30 23:28:24 +02:00
FF_CODEC_ENCODE_CB ( mlp_encode_frame ) ,
2016-08-30 12:23:02 +02:00
. close = mlp_encode_close ,
2023-10-02 20:02:18 +02:00
. p . priv_class = & mlp_class ,
2023-10-19 23:36:19 +02:00
. p . sample_fmts = ( const enum AVSampleFormat [ ] ) { AV_SAMPLE_FMT_S16P , AV_SAMPLE_FMT_S32P , AV_SAMPLE_FMT_NONE } ,
2022-03-16 22:09:54 +02:00
. p . supported_samplerates = ( const int [ ] ) { 44100 , 48000 , 88200 , 96000 , 176400 , 192000 , 0 } ,
2022-09-26 01:10:51 +02:00
CODEC_OLD_CHANNEL_LAYOUTS_ARRAY ( ff_mlp_channel_layouts )
2022-03-16 22:09:54 +02:00
. p . ch_layouts = ff_mlp_ch_layouts ,
2022-07-10 00:05:45 +02:00
. caps_internal = FF_CODEC_CAP_INIT_CLEANUP ,
2016-08-30 12:23:02 +02:00
} ;
# endif
# if CONFIG_TRUEHD_ENCODER
2022-03-16 22:09:54 +02:00
const FFCodec ff_truehd_encoder = {
. p . name = " truehd " ,
2022-08-29 13:38:02 +02:00
CODEC_LONG_NAME ( " TrueHD " ) ,
2022-03-16 22:09:54 +02:00
. p . type = AVMEDIA_TYPE_AUDIO ,
. p . id = AV_CODEC_ID_TRUEHD ,
2021-05-11 20:52:13 +02:00
. p . capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
AV_CODEC_CAP_SMALL_LAST_FRAME |
AV_CODEC_CAP_EXPERIMENTAL ,
2016-08-30 12:23:02 +02:00
. priv_data_size = sizeof ( MLPEncodeContext ) ,
. init = mlp_encode_init ,
2022-03-30 23:28:24 +02:00
FF_CODEC_ENCODE_CB ( mlp_encode_frame ) ,
2016-08-30 12:23:02 +02:00
. close = mlp_encode_close ,
2023-10-02 20:02:18 +02:00
. p . priv_class = & mlp_class ,
2023-10-19 23:36:19 +02:00
. p . sample_fmts = ( const enum AVSampleFormat [ ] ) { AV_SAMPLE_FMT_S16P , AV_SAMPLE_FMT_S32P , AV_SAMPLE_FMT_NONE } ,
2022-03-16 22:09:54 +02:00
. p . supported_samplerates = ( const int [ ] ) { 44100 , 48000 , 88200 , 96000 , 176400 , 192000 , 0 } ,
2023-10-25 12:46:24 +02:00
CODEC_OLD_CHANNEL_LAYOUTS ( AV_CH_LAYOUT_MONO , AV_CH_LAYOUT_STEREO , AV_CH_LAYOUT_2POINT1 , AV_CH_LAYOUT_SURROUND , AV_CH_LAYOUT_3POINT1 , AV_CH_LAYOUT_4POINT0 , AV_CH_LAYOUT_4POINT1 , AV_CH_LAYOUT_5POINT0 , AV_CH_LAYOUT_5POINT1 )
2022-03-16 22:09:54 +02:00
. p . ch_layouts = ( const AVChannelLayout [ ] ) {
2022-09-18 14:44:27 +02:00
AV_CHANNEL_LAYOUT_MONO ,
2019-05-29 09:36:27 +02:00
AV_CHANNEL_LAYOUT_STEREO ,
2023-10-20 23:27:06 +02:00
AV_CHANNEL_LAYOUT_2POINT1 ,
2023-10-19 23:36:19 +02:00
AV_CHANNEL_LAYOUT_SURROUND ,
2023-10-25 11:05:35 +02:00
AV_CHANNEL_LAYOUT_3POINT1 ,
2023-10-25 12:46:24 +02:00
AV_CHANNEL_LAYOUT_4POINT0 ,
AV_CHANNEL_LAYOUT_4POINT1 ,
2022-09-17 22:51:59 +02:00
AV_CHANNEL_LAYOUT_5POINT0 ,
AV_CHANNEL_LAYOUT_5POINT1 ,
2019-05-29 09:36:27 +02:00
{ 0 }
} ,
2022-07-10 00:05:45 +02:00
. caps_internal = FF_CODEC_CAP_INIT_CLEANUP ,
2016-08-30 12:23:02 +02:00
} ;
# endif