File: ffsox_frame_convert_sox.c

package info (click to toggle)
bs1770gain 0.4.12-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,832 kB
  • sloc: ansic: 9,380; sh: 1,288; makefile: 212
file content (258 lines) | stat: -rwxr-xr-x 7,090 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
/*
 * ffsox_frame_convert.c
 * Copyright (C) 2014 Peter Belkner <pbelkner@users.sf.net>
 *
 * This program 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; either
 * version 2.0 of the License, or (at your option) any later version.
 *
 * This program 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 this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301  USA
 */
#include <ffsox_priv.h>

///////////////////////////////////////////////////////////////////////////////
// operation
#define INTERCEPT_CHANNEL_INT_OP(i) \
  intercept->channel(intercept->data,ch,scale*(i))
#define INTERCEPT_CHANNEL_FLT_OP(x) \
  intercept->channel(intercept->data,ch,x)
#define INTERCEPT_SAMPLE_OP() \
  intercept->sample(intercept->data)

// no operation
#define INTERCEPT_CHANNEL_INT_NOP(i)
#define INTERCEPT_CHANNEL_FLT_NOP(x)
#define INTERCEPT_SAMPLE_NOP()

/// interleaved ///////////////////////////////////////////////////////////////
#define CONVERT_INT_I(SOX_CONVERT,OP) do { \
  double scale=1.0/MAXOF(*rp); \
  double x; \
 \
  if (1.0==q) { \
    while (wp<mp) { \
      for (ch=0;ch<channels;++ch) { \
        INTERCEPT_CHANNEL_INT_##OP(*rp); \
        *wp++=SOX_CONVERT(*rp++,clips); \
      } \
 \
      INTERCEPT_SAMPLE_##OP(); \
    } \
  } \
  else { \
    while (wp<mp) { \
      for (ch=0;ch<channels;++ch) { \
        x=scale*(*rp++); \
        INTERCEPT_CHANNEL_FLT_##OP(x); \
        x*=q; \
        *wp++=SOX_FLOAT_64BIT_TO_SAMPLE(x,clips); \
      } \
 \
      INTERCEPT_SAMPLE_##OP(); \
    } \
  } \
} while (0)

#define CONVERT_FLT_I(SOX_CONVERT,OP) do { \
  if (1.0==q) { \
    while (wp<mp) { \
      for (ch=0;ch<channels;++ch) { \
        INTERCEPT_CHANNEL_FLT_##OP(*rp); \
        *wp++=SOX_CONVERT(*rp++,clips); \
      } \
 \
      INTERCEPT_SAMPLE_##OP(); \
    } \
  } \
  else { \
    while (wp<mp) { \
      for (ch=0;ch<channels;++ch) { \
        INTERCEPT_CHANNEL_FLT_##OP(*rp); \
        *wp++=SOX_CONVERT(q*(*rp++),clips); \
      } \
 \
      INTERCEPT_SAMPLE_##OP(); \
    } \
  } \
} while (0)

#define CONVERT_I(sfx,T,convert,SOX_CONVERT) \
static void convert_##sfx##i(convert_t *p, sox_uint64_t *clipsp) \
{ \
  sox_uint64_t clips=*clipsp; \
  double q=p->q; \
  intercept_t *intercept=p->intercept; \
  int ch,channels=p->channels; \
  T *rp; \
  sox_sample_t *wp,*mp; \
  SOX_SAMPLE_LOCALS; \
 \
  (void)ch; \
 \
  rp=(void *)p->fr->frame->data[0]; \
  rp+=channels*p->fr->nb_samples.frame; \
 \
  wp=(void *)p->fw->frame->data[0]; \
  wp+=channels*p->fw->nb_samples.frame; \
  mp=wp+channels*p->nb_samples; \
 \
  if (NULL==intercept) \
    convert(SOX_CONVERT,NOP); \
  else \
    convert(SOX_CONVERT,OP); \
 \
  *clipsp=clips; \
}

CONVERT_I(s8,int8_t,CONVERT_INT_I,SOX_UNSIGNED_8BIT_TO_SAMPLE)
CONVERT_I(s16,int16_t,CONVERT_INT_I,SOX_SIGNED_16BIT_TO_SAMPLE)
CONVERT_I(s32,int32_t,CONVERT_INT_I,SOX_SIGNED_32BIT_TO_SAMPLE)
CONVERT_I(flt,float,CONVERT_FLT_I,SOX_FLOAT_32BIT_TO_SAMPLE)
CONVERT_I(dbl,double,CONVERT_FLT_I,SOX_FLOAT_64BIT_TO_SAMPLE)

/// planar ////////////////////////////////////////////////////////////////////
#define CONVERT_INT_P(SOX_CONVERT) do { \
  double scale=1.0/MAXOF(*rp[0]); \
  double x; \
 \
  if (1.0==q) { \
    while (wp<mp) { \
      for (ch=0;ch<channels;++ch) { \
        INTERCEPT_CHANNEL_INT_##OP(*rp[ch]); \
        *wp++=SOX_CONVERT(*rp[ch]++,clips); \
      } \
 \
      INTERCEPT_SAMPLE_##OP(); \
    } \
  } \
  else { \
    while (wp<mp) { \
      for (ch=0;ch<channels;++ch) { \
        x=scale*(*rp[ch]++); \
        INTERCEPT_CHANNEL_FLT_##OP(x); \
        x*=q; \
        *wp++=SOX_FLOAT_64BIT_TO_SAMPLE(x,clips); \
      } \
 \
      INTERCEPT_SAMPLE_##OP(); \
    } \
  } \
} while (0)

#define CONVERT_FLOAT_P(SOX_CONVERT) do { \
  if (1.0==q) { \
    while (wp<mp) { \
      for (ch=0;ch<channels;++ch) { \
        INTERCEPT_CHANNEL_FLT_##OP(*rp[ch]); \
        *wp++=SOX_CONVERT(*rp[ch]++,clips); \
      } \
 \
      INTERCEPT_SAMPLE_##OP(); \
    } \
  } \
  else { \
    while (wp<mp) { \
      for (ch=0;ch<channels;++ch) { \
        INTERCEPT_CHANNEL_FLT_##OP(*rp[ch]); \
        *wp++=SOX_CONVERT(q*(*rp[ch]++),clips); \
      } \
 \
      INTERCEPT_SAMPLE_##OP(); \
    } \
  } \
} while (0)

#define CONVERT_P(sfx,T,convert,SOX_CONVERT) \
static void convert_##sfx##p(convert_t *p, sox_uint64_t *clipsp) \
{ \
  sox_uint64_t clips=*clipsp; \
  double q=p->q; \
  intercept_t *intercept=p->intercept; \
  int channels=p->channels; \
  T *rp[AV_NUM_DATA_POINTERS]; \
  sox_sample_t *wp,*mp; \
  int ch; \
  SOX_SAMPLE_LOCALS; \
 \
  for (ch=0;ch<channels;++ch) { \
    rp[ch]=(void *)p->fr->frame->data[ch]; \
    rp[ch]+=p->fr->nb_samples.frame; \
  } \
 \
  wp=(void *)p->fw->frame->data[0]; \
  wp+=channels*p->fw->nb_samples.frame; \
  mp=wp+channels*p->nb_samples; \
 \
  convert(SOX_CONVERT); \
 \
  *clipsp=clips; \
}

CONVERT_P(s8,int8_t,CONVERT_INT_P,SOX_UNSIGNED_8BIT_TO_SAMPLE)
CONVERT_P(s16,int32_t,CONVERT_INT_P,SOX_SIGNED_16BIT_TO_SAMPLE)
CONVERT_P(s32,int32_t,CONVERT_INT_P,SOX_SIGNED_32BIT_TO_SAMPLE)
CONVERT_P(flt,float,CONVERT_FLOAT_P,SOX_FLOAT_32BIT_TO_SAMPLE)
CONVERT_P(dbl,double,CONVERT_FLOAT_P,SOX_FLOAT_64BIT_TO_SAMPLE)

///////////////////////////////////////////////////////////////////////////////
int ffsox_frame_convert_sox(frame_t *fr, frame_t *fw, double q,
    intercept_t *intercept, sox_uint64_t *clipsp)
{
  convert_t convert;

  ffsox_convert_setup(&convert,fr,fw,q,intercept);

  switch (fr->frame->format) {
  /// interleaved /////////////////////////////////////////////////////////////
  case AV_SAMPLE_FMT_U8:
    convert_s8i(&convert,clipsp);
    break;
  case AV_SAMPLE_FMT_S16:
    convert_s16i(&convert,clipsp);
    break;
  case AV_SAMPLE_FMT_S32:
    convert_s32i(&convert,clipsp);
    break;
  case AV_SAMPLE_FMT_FLT:
    convert_flti(&convert,clipsp);
    break;
  case AV_SAMPLE_FMT_DBL:
    convert_dbli(&convert,clipsp);
    break;
  /// planar //////////////////////////////////////////////////////////////////
  case AV_SAMPLE_FMT_U8P:
    convert_s8p(&convert,clipsp);
    break;
  case AV_SAMPLE_FMT_S16P:
    convert_s16p(&convert,clipsp);
    break;
  case AV_SAMPLE_FMT_S32P:
    convert_s32p(&convert,clipsp);
    break;
  case AV_SAMPLE_FMT_FLTP:
    convert_fltp(&convert,clipsp);
    break;
  case AV_SAMPLE_FMT_DBLP:
    convert_dblp(&convert,clipsp);
    break;
  /////////////////////////////////////////////////////////////////////////////
  default:
    DMESSAGE("unsupported sample format");
    return -1;
  }

  // advance the read and write offsets
  fr->nb_samples.frame+=convert.nb_samples;
  fw->nb_samples.frame+=convert.nb_samples;

  return convert.nb_samples;
}