2009-07-08 20:01:31 +00:00
|
|
|
/*
|
|
|
|
* audio encoder psychoacoustic model
|
|
|
|
* Copyright (C) 2008 Konstantin Shishkov
|
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* This file is part of Libav.
|
2009-07-08 20:01:31 +00:00
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2009-07-08 20:01:31 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2009-07-08 20:01:31 +00:00
|
|
|
* 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
|
2011-03-18 17:35:10 +00:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2009-07-08 20:01:31 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef AVCODEC_PSYMODEL_H
|
|
|
|
#define AVCODEC_PSYMODEL_H
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
|
|
|
|
|
|
|
/** maximum possible number of bands */
|
|
|
|
#define PSY_MAX_BANDS 128
|
2011-04-05 07:05:22 +00:00
|
|
|
/** maximum number of channels */
|
|
|
|
#define PSY_MAX_CHANS 20
|
2009-07-08 20:01:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* single band psychoacoustic information
|
|
|
|
*/
|
2009-07-08 20:36:45 +00:00
|
|
|
typedef struct FFPsyBand {
|
2009-07-08 20:01:31 +00:00
|
|
|
int bits;
|
|
|
|
float energy;
|
|
|
|
float threshold;
|
|
|
|
float distortion;
|
|
|
|
float perceptual_weight;
|
2009-07-08 20:36:45 +00:00
|
|
|
} FFPsyBand;
|
2009-07-08 20:01:31 +00:00
|
|
|
|
2011-06-15 08:50:25 +00:00
|
|
|
/**
|
|
|
|
* single channel psychoacoustic information
|
|
|
|
*/
|
|
|
|
typedef struct FFPsyChannel {
|
|
|
|
FFPsyBand psy_bands[PSY_MAX_BANDS]; ///< channel bands information
|
|
|
|
float entropy; ///< total PE for this channel
|
|
|
|
} FFPsyChannel;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* psychoacoustic information for an arbitrary group of channels
|
|
|
|
*/
|
|
|
|
typedef struct FFPsyChannelGroup {
|
|
|
|
FFPsyChannel *ch[PSY_MAX_CHANS]; ///< pointers to the individual channels in the group
|
|
|
|
uint8_t num_ch; ///< number of channels in this group
|
|
|
|
uint8_t coupling[PSY_MAX_BANDS]; ///< allow coupling for this band in the group
|
|
|
|
} FFPsyChannelGroup;
|
|
|
|
|
2009-07-08 20:01:31 +00:00
|
|
|
/**
|
|
|
|
* windowing related information
|
|
|
|
*/
|
2009-07-08 20:36:45 +00:00
|
|
|
typedef struct FFPsyWindowInfo {
|
2009-07-08 20:01:31 +00:00
|
|
|
int window_type[3]; ///< window type (short/long/transitional, etc.) - current, previous and next
|
|
|
|
int window_shape; ///< window shape (sine/KBD/whatever)
|
|
|
|
int num_windows; ///< number of windows in a frame
|
|
|
|
int grouping[8]; ///< window grouping (for e.g. AAC)
|
|
|
|
int *window_sizes; ///< sequence of window sizes inside one frame (for eg. WMA)
|
2009-07-08 20:36:45 +00:00
|
|
|
} FFPsyWindowInfo;
|
2009-07-08 20:01:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* context used by psychoacoustic model
|
|
|
|
*/
|
2009-07-08 20:36:45 +00:00
|
|
|
typedef struct FFPsyContext {
|
2009-07-08 20:01:31 +00:00
|
|
|
AVCodecContext *avctx; ///< encoder context
|
|
|
|
const struct FFPsyModel *model; ///< encoder-specific model functions
|
|
|
|
|
2011-06-15 08:50:25 +00:00
|
|
|
FFPsyChannel *ch; ///< single channel information
|
|
|
|
FFPsyChannelGroup *group; ///< channel group information
|
|
|
|
int num_groups; ///< number of channel groups
|
2009-07-08 20:01:31 +00:00
|
|
|
|
|
|
|
uint8_t **bands; ///< scalefactor band sizes for possible frame sizes
|
|
|
|
int *num_bands; ///< number of scalefactor bands for possible frame sizes
|
|
|
|
int num_lens; ///< number of scalefactor band sets
|
|
|
|
|
2011-04-05 07:05:22 +00:00
|
|
|
struct {
|
|
|
|
int size; ///< size of the bitresevoir in bits
|
|
|
|
int bits; ///< number of bits used in the bitresevoir
|
|
|
|
} bitres;
|
|
|
|
|
2009-07-08 20:01:31 +00:00
|
|
|
void* model_priv_data; ///< psychoacoustic model implementation private data
|
2009-07-08 20:36:45 +00:00
|
|
|
} FFPsyContext;
|
2009-07-08 20:01:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* codec-specific psychoacoustic model implementation
|
|
|
|
*/
|
|
|
|
typedef struct FFPsyModel {
|
|
|
|
const char *name;
|
|
|
|
int (*init) (FFPsyContext *apc);
|
2011-05-06 07:19:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Suggest window sequence for channel.
|
|
|
|
*
|
|
|
|
* @param ctx model context
|
|
|
|
* @param audio samples for the current frame
|
|
|
|
* @param la lookahead samples (NULL when unavailable)
|
|
|
|
* @param channel number of channel element to analyze
|
|
|
|
* @param prev_type previous window type
|
|
|
|
*
|
|
|
|
* @return suggested window information in a structure
|
|
|
|
*/
|
2011-12-18 01:45:55 +00:00
|
|
|
FFPsyWindowInfo (*window)(FFPsyContext *ctx, const float *audio, const float *la, int channel, int prev_type);
|
2011-05-06 07:19:52 +00:00
|
|
|
|
2011-06-15 08:50:25 +00:00
|
|
|
/**
|
|
|
|
* Perform psychoacoustic analysis and set band info (threshold, energy) for a group of channels.
|
|
|
|
*
|
|
|
|
* @param ctx model context
|
|
|
|
* @param channel channel number of the first channel in the group to perform analysis on
|
|
|
|
* @param coeffs array of pointers to the transformed coefficients
|
|
|
|
* @param wi window information for the channels in the group
|
|
|
|
*/
|
2011-05-19 05:23:22 +00:00
|
|
|
void (*analyze)(FFPsyContext *ctx, int channel, const float **coeffs, const FFPsyWindowInfo *wi);
|
2011-06-15 08:50:25 +00:00
|
|
|
|
2009-07-08 20:01:31 +00:00
|
|
|
void (*end) (FFPsyContext *apc);
|
2009-07-08 20:36:45 +00:00
|
|
|
} FFPsyModel;
|
2009-07-08 20:01:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize psychoacoustic model.
|
|
|
|
*
|
|
|
|
* @param ctx model context
|
|
|
|
* @param avctx codec context
|
|
|
|
* @param num_lens number of possible frame lengths
|
|
|
|
* @param bands scalefactor band lengths for all frame lengths
|
|
|
|
* @param num_bands number of scalefactor bands for all frame lengths
|
2011-06-15 08:50:25 +00:00
|
|
|
* @param num_groups number of channel groups
|
|
|
|
* @param group_map array with # of channels in group - 1, for each group
|
2009-07-08 20:01:31 +00:00
|
|
|
*
|
|
|
|
* @return zero if successful, a negative value if not
|
|
|
|
*/
|
2013-02-09 15:19:27 +00:00
|
|
|
int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx, int num_lens,
|
|
|
|
const uint8_t **bands, const int *num_bands,
|
|
|
|
int num_groups, const uint8_t *group_map);
|
2011-06-15 08:50:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine what group a channel belongs to.
|
|
|
|
*
|
|
|
|
* @param ctx psymodel context
|
|
|
|
* @param channel channel to locate the group for
|
|
|
|
*
|
|
|
|
* @return pointer to the FFPsyChannelGroup this channel belongs to
|
|
|
|
*/
|
|
|
|
FFPsyChannelGroup *ff_psy_find_group(FFPsyContext *ctx, int channel);
|
2009-07-08 20:01:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleanup model context at the end.
|
|
|
|
*
|
|
|
|
* @param ctx model context
|
|
|
|
*/
|
2013-02-09 15:19:27 +00:00
|
|
|
void ff_psy_end(FFPsyContext *ctx);
|
2009-07-08 20:01:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**************************************************************************
|
|
|
|
* Audio preprocessing stuff. *
|
|
|
|
* This should be moved into some audio filter eventually. *
|
|
|
|
**************************************************************************/
|
|
|
|
struct FFPsyPreprocessContext;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* psychoacoustic model audio preprocessing initialization
|
|
|
|
*/
|
2013-02-09 15:19:27 +00:00
|
|
|
struct FFPsyPreprocessContext *ff_psy_preprocess_init(AVCodecContext *avctx);
|
2009-07-08 20:01:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Preprocess several channel in audio frame in order to compress it better.
|
|
|
|
*
|
|
|
|
* @param ctx preprocessing context
|
2011-12-15 02:43:56 +00:00
|
|
|
* @param audio samples to be filtered (in place)
|
|
|
|
* @param channels number of channel to preprocess
|
2009-07-08 20:01:31 +00:00
|
|
|
*/
|
2011-12-15 02:43:56 +00:00
|
|
|
void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx, float **audio, int channels);
|
2009-07-08 20:01:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleanup audio preprocessing module.
|
|
|
|
*/
|
2013-02-09 15:19:27 +00:00
|
|
|
void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx);
|
2009-07-08 20:01:31 +00:00
|
|
|
|
|
|
|
#endif /* AVCODEC_PSYMODEL_H */
|