File: filter_normalize.c

package info (click to toggle)
transcode 3%3A1.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 11,644 kB
  • sloc: ansic: 116,927; sh: 11,468; xml: 2,849; makefile: 1,891; perl: 1,492; pascal: 526; php: 191; python: 144; sed: 43
file content (311 lines) | stat: -rw-r--r-- 7,721 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 *  filter_normalize.c
 *
 *  Copyright (C) pl <p_l@gmx.fr> 2002 and beyond...
 *                Tilmann Bitterberg - June 2002 ported to transcode
 *
 *  Sources: some ideas from volnorm plugin for xmms
 *
 *  This file is part of transcode, a video stream processing tool
 *
 *  transcode is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, version 2.
 *
 *  transcode 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* Values for AVG:
 * 1: uses a 1 value memory and coefficients new=a*old+b*cur (with a+b=1)
 *
 * 2: uses several samples to smooth the variations (standard weighted mean
 *    on past samples)
 *
 * Limitations:
 *  - only AFMT_S16_LE supported
 *
 * */

#define MOD_NAME    "filter_normalize.so"
#define MOD_VERSION "v0.1.1 (2002-06-18)"
#define MOD_CAP     "Volume normalizer"
#define MOD_AUTHOR  "pl, Tilmann Bitterberg"

#include "transcode.h"
#include "filter.h"
#include "libtc/libtc.h"
#include "libtc/optstr.h"

#include <math.h>


// basic parameter
// mul is the value by which the samples are scaled
// and has to be in [MUL_MIN, MUL_MAX]
#define MUL_INIT 1.0
#define MUL_MIN 0.1
#define MUL_MAX 5.0

#define MIN_SAMPLE_SIZE 32000

// Some limits
#define MIN_S16 -32768
#define MAX_S16  32767

// "Ideal" level
#define MID_S16 (MAX_S16 * 0.25)

// Silence level
// FIXME: should be relative to the level of the samples
#define SIL_S16 (MAX_S16 * 0.01)


// Local data

#define NSAMPLES 128

struct mem_t {
    double avg;		// average level of the sample
    int32_t len;	// sample size (weight)
};

typedef struct MyFilterData {
	int format;
	double mul;
	double SMOOTH_MUL;
	double SMOOTH_LASTAVG;
	double lastavg;
	int idx;
	struct mem_t mem[NSAMPLES];
	int AVG;
} MyFilterData;

static MyFilterData *mfd = NULL;

/* should probably honor the other flags too */

/*-------------------------------------------------
 *
 * single function interface
 *
 *-------------------------------------------------*/

static void help_optstr(void)
{
   tc_log_info (MOD_NAME, "(%s) help\n"
"* Overview\n"
"    normalizes audio\n"
"* Options\n"
"     'smooth' double for smoothing ]0.0 1.0[  [0.06]\n"
" 'smoothlast' double for smoothing last sample ]0.0, 1.0[  [0.06]\n"
"       'algo' Which algorithm to use (1 or 2) [1]\n"
"            1: uses a 1 value memory and coefficients new=a*old+b*cur (with a+b=1)\n"
"            2: uses several samples to smooth the variations (standard weighted mean\n"
"            on past samples)\n"
		, MOD_CAP);
}

static void reset(void)
{
  int i;
  mfd->mul = MUL_INIT;
  switch(mfd->format) {
      case(1): /* XXX: bogus */
      mfd->lastavg = MID_S16;
      for(i=0; i < NSAMPLES; ++i) {
	      mfd->mem[i].len = 0;
	      mfd->mem[i].avg = 0;
      }
      mfd->idx = 0;

      break;
    default:
      break;
  }
}

int tc_filter(frame_list_t *ptr_, char *options)
{
  aframe_list_t *ptr = (aframe_list_t *)ptr_;
  static vob_t *vob=NULL;

  if(ptr->tag & TC_FILTER_GET_CONFIG) {
      optstr_filter_desc (options, MOD_NAME, MOD_CAP, MOD_VERSION, "pl, Tilmann Bitterberg", "AE", "1");
      optstr_param (options, "smooth", "Value for smoothing ]0.0 1.0[", "%f", "0.06", "0.0", "1.0");
      optstr_param (options, "smoothlast", "Value for smoothing last sample ]0.0, 1.0[", "%f", "0.06", "0.0", "1.0");
      optstr_param (options, "algo", "Algorithm to use (1 or 2). 1=uses a 1 value memory and coefficients new=a*old+b*cur (with a+b=1).   2=uses several samples to smooth the variations (standard weighted mean on past samples)", "%d", "1", "1", "2");
      return 0;
  }


  //----------------------------------
  //
  // filter init
  //
  //----------------------------------


  if(ptr->tag & TC_FILTER_INIT) {

    if((vob = tc_get_vob())==NULL) return(-1);

    if (vob->a_bits != 16) {
	tc_log_error(MOD_NAME, "This filter only works for 16 bit samples");
	return (-1);

    }
    if((mfd = tc_malloc (sizeof(MyFilterData))) == NULL) return (-1);

    mfd->format  = 1; /* XXX bogus */
    mfd->mul     = MUL_INIT;
    mfd->lastavg = MID_S16;
    mfd->idx     = 0;
    mfd->SMOOTH_MUL     = 0.06;
    mfd->SMOOTH_LASTAVG = 0.06;
    mfd->AVG     = 1;

    reset();

    if (options != NULL) {

	if(verbose) tc_log_info(MOD_NAME, "options=%s", options);

	optstr_get(options, "smooth", "%lf", &mfd->SMOOTH_MUL);
	optstr_get(options, "smoothlast", "%lf", &mfd->SMOOTH_LASTAVG);
	optstr_get(options, "algo", "%d", &mfd->AVG);

	if (mfd->AVG > 2) mfd->AVG = 2;
	if (mfd->AVG < 1) mfd->AVG = 1;

    }

#if 0
    if (verbose > 1) {
	tc_log_info (MOD_NAME, " Normalize Filter Settings:");
    }
#endif

    if (options)
	if (optstr_lookup (options, "help")) {
	    help_optstr();
	}

    // filter init ok.
    if (verbose) tc_log_info(MOD_NAME, "%s %s", MOD_VERSION, MOD_CAP);


    return(0);
  }

  //----------------------------------
  //
  // filter close
  //
  //----------------------------------


  if(ptr->tag & TC_FILTER_CLOSE) {

    if (mfd) {
	free(mfd);
    }

    return(0);

  } /* filter close */

  //----------------------------------
  //
  // filter frame routine
  //
  //----------------------------------


  // tag variable indicates, if we are called before
  // transcodes internal video/audo frame processing routines
  // or after and determines video/audio context

  if((ptr->tag & TC_PRE_M_PROCESS) && (ptr->tag & TC_AUDIO) && !(ptr->attributes & TC_FRAME_IS_SKIPPED))  {


#define CLAMP(x,m,M) do { if ((x)<(m)) (x) = (m); else if ((x)>(M)) (x) = (M); } while(0)

    int16_t* data=(int16_t *)ptr->audio_buf;
    int len=ptr->audio_size / 2; // 16 bits samples

    int32_t i, tmp;
    double curavg, newavg;

    double neededmul;

    double avg;
    int32_t totallen;

    // Evaluate current samples average level
    curavg = 0.0;
    for (i = 0; i < len ; ++i) {
      tmp = data[i];
      curavg += tmp * tmp;
    }
    curavg = sqrt(curavg / (double) len);

    // Evaluate an adequate 'mul' coefficient based on previous state, current
    // samples level, etc
    if (mfd->AVG == 1) {
	if (curavg > SIL_S16) {
	    neededmul = MID_S16 / ( curavg * mfd->mul);
	    mfd->mul = (1.0 - mfd->SMOOTH_MUL) * mfd->mul + mfd->SMOOTH_MUL * neededmul;

	    // Clamp the mul coefficient
	    CLAMP(mfd->mul, MUL_MIN, MUL_MAX);
	}
    } else if (mfd->AVG == 2) {
	avg = 0.0;
	totallen = 0;

	for (i = 0; i < NSAMPLES; ++i) {
	    avg += mfd->mem[i].avg * (double) mfd->mem[i].len;
	    totallen += mfd->mem[i].len;
	}

	if (totallen > MIN_SAMPLE_SIZE) {
	    avg /= (double) totallen;
	    if (avg >= SIL_S16) {
		mfd->mul = (double) MID_S16 / avg;
		CLAMP(mfd->mul, MUL_MIN, MUL_MAX);
	    }
	}
    }

    // Scale & clamp the samples
    for (i = 0; i < len ; ++i) {
      tmp = mfd->mul * data[i];
      CLAMP(tmp, MIN_S16, MAX_S16);
      data[i] = tmp;
    }

    // Evaluation of newavg (not 100% accurate because of values clamping)
    newavg = mfd->mul * curavg;

    // Stores computed values for future smoothing
    if (mfd->AVG == 1) {
	mfd->lastavg = (1.0-mfd->SMOOTH_LASTAVG)*mfd->lastavg + mfd->SMOOTH_LASTAVG*newavg;
    } else if (mfd->AVG == 2) {
	mfd->mem[mfd->idx].len = len;
	mfd->mem[mfd->idx].avg = newavg;
	mfd->idx = (mfd->idx + 1) % NSAMPLES;
    }


  }

  return(0);
}