From 23940f1405d4c19df69b1fa77c319e9f114c8ef7 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sun, 11 Jul 2010 16:56:20 +0000 Subject: [PATCH] Add AVCodecContext.lpc_type and Add AVCodecContext.lpc_passes fields. Add AVLPCType enum. Deprecate AVCodecContext.use_lpc. Originally committed as revision 24199 to svn://svn.ffmpeg.org/ffmpeg/trunk --- doc/APIchanges | 5 ++ libavcodec/alacenc.c | 3 +- libavcodec/avcodec.h | 31 ++++++++++++- libavcodec/flacenc.c | 103 ++++++++++++++++++++++++++++++------------ libavcodec/lpc.c | 12 +++-- libavcodec/lpc.h | 3 +- libavcodec/options.c | 8 ++++ libavcodec/ra144enc.c | 4 +- 8 files changed, 130 insertions(+), 39 deletions(-) diff --git a/doc/APIchanges b/doc/APIchanges index 55d8b691b8..5c56b84501 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -12,6 +12,11 @@ libavutil: 2009-03-08 API changes, most recent first: +2010-07-11 - r????? - lavc 52.83.0 + Add AVCodecContext.lpc_type and Add AVCodecContext.lpc_passes fields. + Add AVLPCType enum. + Deprecate AVCodecContext.use_lpc. + 2010-07-11 - r24185 - lavc 52.82.0 - avsubtitle_free() Add a function for free the contents of a AVSubtitle generated by avcodec_decode_subtitle. diff --git a/libavcodec/alacenc.c b/libavcodec/alacenc.c index 0876633cbd..ee6acc0715 100644 --- a/libavcodec/alacenc.c +++ b/libavcodec/alacenc.c @@ -145,7 +145,8 @@ static void calc_predictor_params(AlacEncodeContext *s, int ch) s->avctx->frame_size, s->min_prediction_order, s->max_prediction_order, - ALAC_MAX_LPC_PRECISION, coefs, shift, 1, + ALAC_MAX_LPC_PRECISION, coefs, shift, + AV_LPC_TYPE_LEVINSON, 0, ORDER_METHOD_EST, ALAC_MAX_LPC_SHIFT, 1); s->lpc[ch].lpc_order = opt_order; diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 475d9b5f03..48666f9970 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -30,7 +30,7 @@ #include "libavutil/avutil.h" #define LIBAVCODEC_VERSION_MAJOR 52 -#define LIBAVCODEC_VERSION_MINOR 82 +#define LIBAVCODEC_VERSION_MINOR 83 #define LIBAVCODEC_VERSION_MICRO 0 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ @@ -526,6 +526,18 @@ enum AVChromaLocation{ AVCHROMA_LOC_NB , ///< Not part of ABI }; +/** + * LPC analysis type + */ +enum AVLPCType { + AV_LPC_TYPE_DEFAULT = -1, ///< use the codec default LPC type + AV_LPC_TYPE_NONE = 0, ///< do not use LPC prediction or use all zero coefficients + AV_LPC_TYPE_FIXED = 1, ///< fixed LPC coefficients + AV_LPC_TYPE_LEVINSON = 2, ///< Levinson-Durbin recursion + AV_LPC_TYPE_CHOLESKY = 3, ///< Cholesky factorization + AV_LPC_TYPE_NB , ///< Not part of ABI +}; + typedef struct RcOverride{ int start_frame; int end_frame; @@ -2413,12 +2425,15 @@ typedef struct AVCodecContext { int compression_level; #define FF_COMPRESSION_DEFAULT -1 +#if LIBAVCODEC_VERSION_MAJOR < 53 /** * Sets whether to use LPC mode - used by FLAC encoder. * - encoding: Set by user. * - decoding: unused + * @deprecated Deprecated in favor of lpc_type and lpc_passes. */ int use_lpc; +#endif /** * LPC coefficient precision - used by FLAC encoder @@ -2672,6 +2687,20 @@ typedef struct AVCodecContext { float crf_max; int log_level_offset; + + /** + * Determines which LPC analysis algorithm to use. + * - encoding: Set by user + * - decoding: unused + */ + enum AVLPCType lpc_type; + + /** + * Number of passes to use for Cholesky factorization during LPC analysis + * - encoding: Set by user + * - decoding: unused + */ + int lpc_passes; } AVCodecContext; /** diff --git a/libavcodec/flacenc.c b/libavcodec/flacenc.c index fa05b7d4ee..d87d5d7c21 100644 --- a/libavcodec/flacenc.c +++ b/libavcodec/flacenc.c @@ -44,7 +44,8 @@ typedef struct CompressionOptions { int compression_level; int block_time_ms; - int use_lpc; + enum AVLPCType lpc_type; + int lpc_passes; int lpc_coeff_precision; int min_prediction_order; int max_prediction_order; @@ -208,7 +209,11 @@ static av_cold int flac_encode_init(AVCodecContext *avctx) } s->options.block_time_ms = ((int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level]; - s->options.use_lpc = ((int[]){ 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level]; + s->options.lpc_type = ((int[]){ AV_LPC_TYPE_FIXED, AV_LPC_TYPE_FIXED, AV_LPC_TYPE_FIXED, + AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON, + AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON, + AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON, + AV_LPC_TYPE_LEVINSON})[level]; s->options.min_prediction_order= ((int[]){ 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level]; s->options.max_prediction_order= ((int[]){ 3, 4, 4, 6, 8, 8, 8, 8, 12, 12, 12, 32, 32})[level]; s->options.prediction_order_method = ((int[]){ ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST, @@ -220,45 +225,83 @@ static av_cold int flac_encode_init(AVCodecContext *avctx) s->options.max_partition_order = ((int[]){ 2, 2, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8})[level]; /* set compression option overrides from AVCodecContext */ - if(avctx->use_lpc >= 0) { - s->options.use_lpc = av_clip(avctx->use_lpc, 0, 11); - } - if(s->options.use_lpc == 1) - av_log(avctx, AV_LOG_DEBUG, " use lpc: Levinson-Durbin recursion with Welch window\n"); - else if(s->options.use_lpc > 1) - av_log(avctx, AV_LOG_DEBUG, " use lpc: Cholesky factorization\n"); - - if(avctx->min_prediction_order >= 0) { - if(s->options.use_lpc) { - if(avctx->min_prediction_order < MIN_LPC_ORDER || - avctx->min_prediction_order > MAX_LPC_ORDER) { - av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n", - avctx->min_prediction_order); +#if LIBAVCODEC_VERSION_MAJOR < 53 + /* for compatibility with deprecated AVCodecContext.use_lpc */ + if (avctx->use_lpc == 0) { + s->options.lpc_type = AV_LPC_TYPE_FIXED; + } else if (avctx->use_lpc == 1) { + s->options.lpc_type = AV_LPC_TYPE_LEVINSON; + } else if (avctx->use_lpc > 1) { + s->options.lpc_type = AV_LPC_TYPE_CHOLESKY; + s->options.lpc_passes = avctx->use_lpc - 1; + } +#endif + if (avctx->lpc_type > AV_LPC_TYPE_DEFAULT) { + if (avctx->lpc_type > AV_LPC_TYPE_CHOLESKY) { + av_log(avctx, AV_LOG_ERROR, "unknown lpc type: %d\n", avctx->lpc_type); + return -1; + } + s->options.lpc_type = avctx->lpc_type; + if (s->options.lpc_type == AV_LPC_TYPE_CHOLESKY) { + if (avctx->lpc_passes < 0) { + // default number of passes for Cholesky + s->options.lpc_passes = 2; + } else if (avctx->lpc_passes == 0) { + av_log(avctx, AV_LOG_ERROR, "invalid number of lpc passes: %d\n", + avctx->lpc_passes); return -1; + } else { + s->options.lpc_passes = avctx->lpc_passes; } - } else { + } + } + switch (s->options.lpc_type) { + case AV_LPC_TYPE_NONE: + av_log(avctx, AV_LOG_DEBUG, " lpc type: None\n"); + break; + case AV_LPC_TYPE_FIXED: + av_log(avctx, AV_LOG_DEBUG, " lpc type: Fixed pre-defined coefficients\n"); + break; + case AV_LPC_TYPE_LEVINSON: + av_log(avctx, AV_LOG_DEBUG, " lpc type: Levinson-Durbin recursion with Welch window\n"); + break; + case AV_LPC_TYPE_CHOLESKY: + av_log(avctx, AV_LOG_DEBUG, " lpc type: Cholesky factorization, %d pass%s\n", + s->options.lpc_passes, s->options.lpc_passes==1?"":"es"); + break; + } + + if (s->options.lpc_type == AV_LPC_TYPE_NONE) { + s->options.min_prediction_order = 0; + } else if (avctx->min_prediction_order >= 0) { + if (s->options.lpc_type == AV_LPC_TYPE_FIXED) { if(avctx->min_prediction_order > MAX_FIXED_ORDER) { av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n", avctx->min_prediction_order); return -1; } + } else if(avctx->min_prediction_order < MIN_LPC_ORDER || + avctx->min_prediction_order > MAX_LPC_ORDER) { + av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n", + avctx->min_prediction_order); + return -1; } s->options.min_prediction_order = avctx->min_prediction_order; } - if(avctx->max_prediction_order >= 0) { - if(s->options.use_lpc) { - if(avctx->max_prediction_order < MIN_LPC_ORDER || - avctx->max_prediction_order > MAX_LPC_ORDER) { - av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n", - avctx->max_prediction_order); - return -1; - } - } else { + if (s->options.lpc_type == AV_LPC_TYPE_NONE) { + s->options.max_prediction_order = 0; + } else if (avctx->max_prediction_order >= 0) { + if (s->options.lpc_type == AV_LPC_TYPE_FIXED) { if(avctx->max_prediction_order > MAX_FIXED_ORDER) { av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n", avctx->max_prediction_order); return -1; } + } else if (avctx->max_prediction_order < MIN_LPC_ORDER || + avctx->max_prediction_order > MAX_LPC_ORDER) { + av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n", + avctx->max_prediction_order); + return -1; } s->options.max_prediction_order = avctx->max_prediction_order; } @@ -754,7 +797,8 @@ static int encode_residual(FlacEncodeContext *ctx, int ch) omethod = ctx->options.prediction_order_method; /* FIXED */ - if(!ctx->options.use_lpc || max_order == 0 || (n <= max_order)) { + if (ctx->options.lpc_type == AV_LPC_TYPE_NONE || + ctx->options.lpc_type == AV_LPC_TYPE_FIXED || n <= max_order) { uint32_t bits[MAX_FIXED_ORDER+1]; if(max_order > MAX_FIXED_ORDER) max_order = MAX_FIXED_ORDER; opt_order = 0; @@ -780,8 +824,9 @@ static int encode_residual(FlacEncodeContext *ctx, int ch) /* LPC */ opt_order = ff_lpc_calc_coefs(&ctx->dsp, smp, n, min_order, max_order, - precision, coefs, shift, ctx->options.use_lpc, - omethod, MAX_LPC_SHIFT, 0); + precision, coefs, shift, ctx->options.lpc_type, + ctx->options.lpc_passes, omethod, + MAX_LPC_SHIFT, 0); if(omethod == ORDER_METHOD_2LEVEL || omethod == ORDER_METHOD_4LEVEL || diff --git a/libavcodec/lpc.c b/libavcodec/lpc.c index 41d5e19255..112a78d4b9 100644 --- a/libavcodec/lpc.c +++ b/libavcodec/lpc.c @@ -165,7 +165,8 @@ static int estimate_best_order(double *ref, int min_order, int max_order) int ff_lpc_calc_coefs(DSPContext *s, const int32_t *samples, int blocksize, int min_order, int max_order, int precision, - int32_t coefs[][MAX_LPC_ORDER], int *shift, int use_lpc, + int32_t coefs[][MAX_LPC_ORDER], int *shift, + enum AVLPCType lpc_type, int lpc_passes, int omethod, int max_shift, int zero_shift) { double autoc[MAX_LPC_ORDER+1]; @@ -174,20 +175,21 @@ int ff_lpc_calc_coefs(DSPContext *s, int i, j, pass; int opt_order; - assert(max_order >= MIN_LPC_ORDER && max_order <= MAX_LPC_ORDER && use_lpc > 0); + assert(max_order >= MIN_LPC_ORDER && max_order <= MAX_LPC_ORDER && + lpc_type > AV_LPC_TYPE_FIXED); - if(use_lpc == 1){ + if (lpc_type == AV_LPC_TYPE_LEVINSON) { s->lpc_compute_autocorr(samples, blocksize, max_order, autoc); compute_lpc_coefs(autoc, max_order, &lpc[0][0], MAX_LPC_ORDER, 0, 1); for(i=0; idsp, lpc_data, NBLOCKS * BLOCKSIZE, LPC_ORDER, - LPC_ORDER, 16, lpc_coefs, shift, 1, ORDER_METHOD_EST, 12, - 0); + LPC_ORDER, 16, lpc_coefs, shift, AV_LPC_TYPE_LEVINSON, + 0, ORDER_METHOD_EST, 12, 0); for (i = 0; i < LPC_ORDER; i++) block_coefs[NBLOCKS - 1][i] = -(lpc_coefs[LPC_ORDER - 1][i] << (12 - shift[LPC_ORDER - 1]));