psymodel.c
Go to the documentation of this file.
1 /*
2  * audio encoder psychoacoustic model
3  * Copyright (C) 2008 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <string.h>
23 
24 #include "avcodec.h"
25 #include "psymodel.h"
26 #include "iirfilter.h"
27 #include "libavutil/mem.h"
28 
29 extern const FFPsyModel ff_aac_psy_model;
30 
31 av_cold int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx, int num_lens,
32  const uint8_t **bands, const int* num_bands,
33  int num_groups, const uint8_t *group_map)
34 {
35  int i, j, k = 0;
36 
37  ctx->avctx = avctx;
38  ctx->ch = av_mallocz(sizeof(ctx->ch[0]) * avctx->channels * 2);
39  ctx->group = av_mallocz(sizeof(ctx->group[0]) * num_groups);
40  ctx->bands = av_malloc (sizeof(ctx->bands[0]) * num_lens);
41  ctx->num_bands = av_malloc (sizeof(ctx->num_bands[0]) * num_lens);
42  memcpy(ctx->bands, bands, sizeof(ctx->bands[0]) * num_lens);
43  memcpy(ctx->num_bands, num_bands, sizeof(ctx->num_bands[0]) * num_lens);
44 
45  /* assign channels to groups (with virtual channels for coupling) */
46  for (i = 0; i < num_groups; i++) {
47  /* NOTE: Add 1 to handle the AAC chan_config without modification.
48  * This has the side effect of allowing an array of 0s to map
49  * to one channel per group.
50  */
51  ctx->group[i].num_ch = group_map[i] + 1;
52  for (j = 0; j < ctx->group[i].num_ch * 2; j++)
53  ctx->group[i].ch[j] = &ctx->ch[k++];
54  }
55 
56  switch (ctx->avctx->codec_id) {
57  case AV_CODEC_ID_AAC:
58  ctx->model = &ff_aac_psy_model;
59  break;
60  }
61  if (ctx->model->init)
62  return ctx->model->init(ctx);
63  return 0;
64 }
65 
67 {
68  int i = 0, ch = 0;
69 
70  while (ch <= channel)
71  ch += ctx->group[i++].num_ch;
72 
73  return &ctx->group[i-1];
74 }
75 
77 {
78  if (ctx->model->end)
79  ctx->model->end(ctx);
80  av_freep(&ctx->bands);
81  av_freep(&ctx->num_bands);
82  av_freep(&ctx->group);
83  av_freep(&ctx->ch);
84 }
85 
86 typedef struct FFPsyPreprocessContext{
88  float stereo_att;
93 
94 #define FILT_ORDER 4
95 
97 {
99  int i;
100  float cutoff_coeff = 0;
101  ctx = av_mallocz(sizeof(FFPsyPreprocessContext));
102  ctx->avctx = avctx;
103 
104  if (avctx->cutoff > 0)
105  cutoff_coeff = 2.0 * avctx->cutoff / avctx->sample_rate;
106 
107  if (!cutoff_coeff && avctx->codec_id == AV_CODEC_ID_AAC)
108  cutoff_coeff = 2.0 * AAC_CUTOFF(avctx) / avctx->sample_rate;
109 
110  if (cutoff_coeff && cutoff_coeff < 0.98)
113  cutoff_coeff, 0.0, 0.0);
114  if (ctx->fcoeffs) {
115  ctx->fstate = av_mallocz(sizeof(ctx->fstate[0]) * avctx->channels);
116  for (i = 0; i < avctx->channels; i++)
118  }
119 
120  ff_iir_filter_init(&ctx->fiir);
121 
122  return ctx;
123 }
124 
125 void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx, float **audio, int channels)
126 {
127  int ch;
128  int frame_size = ctx->avctx->frame_size;
129  FFIIRFilterContext *iir = &ctx->fiir;
130 
131  if (ctx->fstate) {
132  for (ch = 0; ch < channels; ch++)
133  iir->filter_flt(ctx->fcoeffs, ctx->fstate[ch], frame_size,
134  &audio[ch][frame_size], 1, &audio[ch][frame_size], 1);
135  }
136 }
137 
139 {
140  int i;
142  if (ctx->fstate)
143  for (i = 0; i < ctx->avctx->channels; i++)
145  av_freep(&ctx->fstate);
146  av_free(ctx);
147 }
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
void(* end)(FFPsyContext *apc)
Definition: psymodel.h:126
uint8_t ** bands
scalefactor band sizes for possible frame sizes
Definition: psymodel.h:84
FFPsyChannelGroup * group
channel group information
Definition: psymodel.h:81
memory handling functions
av_cold void ff_psy_preprocess_end(struct FFPsyPreprocessContext *ctx)
Cleanup audio preprocessing module.
Definition: psymodel.c:138
psychoacoustic information for an arbitrary group of channels
Definition: psymodel.h:56
av_cold 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)
Initialize psychoacoustic model.
Definition: psymodel.c:31
int * num_bands
number of scalefactor bands for possible frame sizes
Definition: psymodel.h:85
av_cold struct FFIIRFilterState * ff_iir_filter_init_state(int order)
Create new filter state.
Definition: iirfilter.c:201
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
av_cold struct FFIIRFilterCoeffs * ff_iir_filter_init_coeffs(void *avc, enum IIRFilterType filt_type, enum IIRFilterMode filt_mode, int order, float cutoff_ratio, float stopband, float ripple)
Initialize filter coefficients.
Definition: iirfilter.c:159
uint8_t
#define av_cold
Definition: attributes.h:78
struct FFIIRFilterCoeffs * fcoeffs
Definition: psymodel.c:89
context used by psychoacoustic model
Definition: psymodel.h:76
#define AAC_CUTOFF(s)
Definition: psymodel.h:32
const FFPsyModel ff_aac_psy_model
Definition: aacpsy.c:955
static const uint8_t frame_size[4]
Definition: g723_1_data.h:58
AVCodecContext * avctx
Definition: psymodel.c:87
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
FFPsyChannel * ch[PSY_MAX_CHANS]
pointers to the individual channels in the group
Definition: psymodel.h:57
external API header
codec-specific psychoacoustic model implementation
Definition: psymodel.h:99
IIR filter state.
Definition: iirfilter.c:44
int(* init)(FFPsyContext *apc)
Definition: psymodel.h:101
void(* filter_flt)(const struct FFIIRFilterCoeffs *coeffs, struct FFIIRFilterState *state, int size, const float *src, int sstep, float *dst, int dstep)
Perform IIR filtering on floating-point input samples.
Definition: iirfilter.h:62
uint8_t num_ch
number of channels in this group
Definition: psymodel.h:58
av_cold void ff_iir_filter_free_coeffs(struct FFIIRFilterCoeffs *coeffs)
Free filter coefficients.
Definition: iirfilter.c:305
for k
int frame_size
Number of samples per channel in an audio frame.
enum AVCodecID codec_id
int sample_rate
samples per second
FFPsyChannelGroup * ff_psy_find_group(FFPsyContext *ctx, int channel)
Determine what group a channel belongs to.
Definition: psymodel.c:66
main external API structure.
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
synthesis window for stochastic i
const struct FFPsyModel * model
encoder-specific model functions
Definition: psymodel.h:78
IIR filter global parameters.
Definition: iirfilter.c:34
void ff_iir_filter_init(FFIIRFilterContext *f)
Initialize FFIIRFilterContext.
Definition: iirfilter.c:314
struct FFIIRFilterState ** fstate
Definition: psymodel.c:90
av_cold struct FFPsyPreprocessContext * ff_psy_preprocess_init(AVCodecContext *avctx)
psychoacoustic model audio preprocessing initialization
Definition: psymodel.c:96
void ff_psy_preprocess(struct FFPsyPreprocessContext *ctx, float **audio, int channels)
Preprocess several channel in audio frame in order to compress it better.
Definition: psymodel.c:125
#define FILT_ORDER
Definition: psymodel.c:94
struct FFIIRFilterContext fiir
Definition: psymodel.c:91
int cutoff
Audio cutoff bandwidth (0 means "automatic")
IIR filter interface.
int channels
number of audio channels
FFPsyChannel * ch
single channel information
Definition: psymodel.h:80
struct FFPsyPreprocessContext FFPsyPreprocessContext
AVCodecContext * avctx
encoder context
Definition: psymodel.h:77
av_cold void ff_psy_end(FFPsyContext *ctx)
Cleanup model context at the end.
Definition: psymodel.c:76
av_cold void ff_iir_filter_free_state(struct FFIIRFilterState *state)
Free filter state.
Definition: iirfilter.c:300