File: ffsox_frame_convert.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 (424 lines) | stat: -rwxr-xr-x 10,970 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 * 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>

///////////////////////////////////////////////////////////////////////////////
void ffsox_convert_int_int(uint8_t *rp, size_t rsize, uint8_t *wp,
    uint8_t *mp, size_t wsize, double q)
{
  int bits=8*(wsize-rsize);
  int32_t i;

//fprintf(stderr,"1.1 ...\n");
  if (0<bits)
    q*=1<<bits;
  else if (bits<0)
    q/=1<<-bits;

  while (wp<mp) {
    //i=(1<<7)&*rp?~0:0;
    i=0;
//fprintf(stderr,"%d, %d: %d -> ",rsize,wsize,i);
    memcpy(&i,rp,rsize);
    rp+=rsize;

    if (rp[-1]&(1<<7)) {
      rp[-1]&=~(1<<7);
      i=-i;
    }

//fprintf(stderr,"%d, %d: %d -> ",rsize,wsize,i);
    i=q*i+0.5;
//fprintf(stderr,"%d\n",i);
    memcpy(wp,&i,wsize);
    wp+=wsize;
  }
}

///////////////////////////////////////////////////////////////////////////////
// interleaved int -> interleaved int.
#if 1 // {
#define CONVERT_INT_INT_II() do { \
 int bits=sizeof *wp; \
 bits-=sizeof *rp; \
 bits*=8; \
 \
  if (1.0==q) { \
    if (0<bits) { \
      while (wp<mp) { \
        *wp=*rp++; \
        *wp++<<=bits; \
      } \
    } \
    else if (bits<0) { \
      while (wp<mp) { \
        *wp=*rp++; \
        *wp++>>=-bits; \
      } \
    } \
    else { \
      while (wp<mp) \
        *wp++=*rp++; \
    } \
  } \
  else { \
    if (0<bits) \
      q*=1<<bits; \
    else if (bits<0) \
      q/=1<<-bits; \
 \
    while (wp<mp) \
      *wp++=floor(q*(*rp++)+0.5); \
  } \
} while (0)
#else // } {
#define CONVERT_INT_INT_II() \
  ffsox_convert_int_int((uint8_t *)rp,sizeof *rp,(uint8_t *)wp, \
      (uint8_t *)mp,sizeof *wp,q)
#endif // }

// interleaved float -> interleaved int.
#define CONVERT_FLOAT_INT_II() do { \
 int bits=sizeof *wp; \
 bits*=8; \
 bits-=1; \
 \
  q=1.0==q?~(~0ull<<bits):q*~(~0ull<<bits); \
 \
  while (wp<mp) \
    *wp++=floor(q*(*rp++)+0.5); \
} while (0)

// interleaved int -> interleaved float.
#define CONVERT_INT_FLOAT_II() do { \
 int bits=sizeof *rp; \
 bits*=8; \
 bits-=1; \
 \
  q/=~(~0ull<<bits); \
 \
  while (wp<mp) \
    *wp++=q*(*rp++); \
} while (0)

// interleaved float -> interleaved float.
#define CONVERT_FLOAT_FLOAT_II() do { \
 \
  if (1.0==q) { \
    while (wp<mp) \
      *wp++=*rp++; \
  } \
  else { \
    while (wp<mp) \
      *wp++=q*(*rp++); \
  } \
} while (0)

// interleaved -> interleaved int.
#define CONVERT_II(r,w,R,W,convert) \
static int convert_##r##i##_##w##i(convert_t *p) \
{ \
  double q=p->q; \
  int channels=p->channels; \
  R *rp; \
  W *wp,*mp; \
 \
  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; \
 \
  convert(); \
 \
  return 0; \
}

////
CONVERT_II(s8,s8,int8_t,int8_t,CONVERT_INT_INT_II)
CONVERT_II(s16,s8,int16_t,int8_t,CONVERT_INT_INT_II)
CONVERT_II(s32,s8,int32_t,int8_t,CONVERT_INT_INT_II)
CONVERT_II(flt,s8,float,int8_t,CONVERT_FLOAT_INT_II)
CONVERT_II(dbl,s8,double,int8_t,CONVERT_FLOAT_INT_II)

CONVERT_II(s8,s16,int8_t,int16_t,CONVERT_INT_INT_II)
CONVERT_II(s16,s16,int16_t,int16_t,CONVERT_INT_INT_II)
CONVERT_II(s32,s16,int32_t,int16_t,CONVERT_INT_INT_II)
CONVERT_II(flt,s16,float,int16_t,CONVERT_FLOAT_INT_II)
CONVERT_II(dbl,s16,double,int16_t,CONVERT_FLOAT_INT_II)

CONVERT_II(s8,s32,int8_t,int32_t,CONVERT_INT_INT_II)
CONVERT_II(s16,s32,int16_t,int32_t,CONVERT_INT_INT_II)
CONVERT_II(s32,s32,int32_t,int32_t,CONVERT_INT_INT_II)
CONVERT_II(flt,s32,float,int32_t,CONVERT_FLOAT_INT_II)
CONVERT_II(dbl,s32,double,int32_t,CONVERT_FLOAT_INT_II)

////
CONVERT_II(s8,flt,int8_t,float,CONVERT_INT_FLOAT_II)
CONVERT_II(s16,flt,int16_t,float,CONVERT_INT_FLOAT_II)
CONVERT_II(s32,flt,int32_t,float,CONVERT_INT_FLOAT_II)
CONVERT_II(flt,flt,float,float,CONVERT_FLOAT_FLOAT_II)
CONVERT_II(dbl,flt,double,float,CONVERT_FLOAT_FLOAT_II)

CONVERT_II(s8,dbl,int8_t,double,CONVERT_INT_FLOAT_II)
CONVERT_II(s16,dbl,int16_t,double,CONVERT_INT_FLOAT_II)
CONVERT_II(s32,dbl,int32_t,double,CONVERT_INT_FLOAT_II)
CONVERT_II(flt,dbl,float,double,CONVERT_FLOAT_FLOAT_II)
CONVERT_II(dbl,dbl,double,double,CONVERT_FLOAT_FLOAT_II)

///////////////////////////////////////////////////////////////////////////////
// planar int -> interleaved int.
#define CONVERT_INT_INT_PI() do { \
 int bits=sizeof *wp; \
 bits-=sizeof *rp[0]; \
 bits*=8; \
 \
  if (1.0==q) { \
    if (0<bits) { \
      while (wp<mp) { \
        for (ch=0;ch<channels;++ch) { \
          *wp=*rp[ch]++; \
          *wp++<<=bits; \
        } \
      } \
    } \
    else if (bits<0) { \
      while (wp<mp) { \
        for (ch=0;ch<channels;++ch) { \
          *wp=*rp[ch]++; \
          *wp++>>=-bits; \
        } \
      } \
    } \
    else { \
      while (wp<mp) { \
        for (ch=0;ch<channels;++ch) \
          *wp++=*rp[ch]++; \
      } \
    } \
  } \
  else { \
    if (0<bits) \
      q*=1<<bits; \
    else if (bits<0) \
      q/=1<<-bits; \
 \
    while (wp<mp) \
      for (ch=0;ch<channels;++ch) \
        *wp++=floor(q*(*rp[ch]++)+0.5); \
  } \
} while (0)

// planar float -> interleaved int.
#define CONVERT_FLOAT_INT_PI() do { \
 int bits=sizeof *wp; \
 bits*=8; \
 bits-=1; \
 \
  q=1.0==q?~(~0ull<<bits):q*~(~0ull<<bits); \
 \
  while (wp<mp) { \
    for (ch=0;ch<channels;++ch) \
      *wp++=floor(q*(*rp[ch]++)+0.5); \
  } \
} while (0)

// planar int -> interleaved float.
#define CONVERT_INT_FLOAT_PI() do { \
 int bits=sizeof *rp[0]; \
 bits*=8; \
 bits-=1; \
 \
  q/=~(~0ull<<bits); \
 \
  while (wp<mp) { \
    for (ch=0;ch<channels;++ch) \
      *wp++=q*(*rp[ch]++); \
  } \
} while (0)

// planar float -> interleaved float.
#define CONVERT_FLOAT_FLOAT_PI() do { \
  if (1.0==q) { \
    while (wp<mp) { \
      for (ch=0;ch<channels;++ch) \
        *wp++=*rp[ch]++; \
    } \
  } \
  else { \
    while (wp<mp) { \
      for (ch=0;ch<channels;++ch) \
        *wp++=q*(*rp[ch]++); \
    } \
  } \
} while (0)

// planar -> interleaved int.
#define CONVERT_PI(r,w,R,W,convert) \
static int convert_##r##p##_##w##i(convert_t *p) \
{ \
  double q=p->q; \
  int ch,channels=p->channels; \
  R *rp[AV_NUM_DATA_POINTERS]; \
  W *wp,*mp; \
 \
  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(); \
 \
  return 0; \
}

//////
CONVERT_PI(s8,s8,int8_t,int8_t,CONVERT_INT_INT_PI)
CONVERT_PI(s16,s8,int16_t,int8_t,CONVERT_INT_INT_PI)
CONVERT_PI(s32,s8,int32_t,int8_t,CONVERT_INT_INT_PI)
CONVERT_PI(flt,s8,float,int8_t,CONVERT_FLOAT_INT_PI)
CONVERT_PI(dbl,s8,double,int8_t,CONVERT_FLOAT_INT_PI)

CONVERT_PI(s8,s16,int8_t,int16_t,CONVERT_INT_INT_PI)
CONVERT_PI(s16,s16,int16_t,int16_t,CONVERT_INT_INT_PI)
CONVERT_PI(s32,s16,int32_t,int16_t,CONVERT_INT_INT_PI)
CONVERT_PI(flt,s16,float,int16_t,CONVERT_FLOAT_INT_PI)
CONVERT_PI(dbl,s16,double,int16_t,CONVERT_FLOAT_INT_PI)

CONVERT_PI(s8,s32,int8_t,int32_t,CONVERT_INT_INT_PI)
CONVERT_PI(s16,s32,int16_t,int32_t,CONVERT_INT_INT_PI)
CONVERT_PI(s32,s32,int32_t,int32_t,CONVERT_INT_INT_PI)
CONVERT_PI(flt,s32,float,int32_t,CONVERT_FLOAT_INT_PI)
CONVERT_PI(dbl,s32,double,int32_t,CONVERT_FLOAT_INT_PI)

//////
CONVERT_PI(s8,flt,int8_t,float,CONVERT_INT_FLOAT_PI)
CONVERT_PI(s16,flt,int16_t,float,CONVERT_INT_FLOAT_PI)
CONVERT_PI(s32,flt,int32_t,float,CONVERT_INT_FLOAT_PI)
CONVERT_PI(flt,flt,float,float,CONVERT_FLOAT_FLOAT_PI)
CONVERT_PI(dbl,flt,double,float,CONVERT_FLOAT_FLOAT_PI)

CONVERT_PI(s8,dbl,int8_t,double,CONVERT_INT_FLOAT_PI)
CONVERT_PI(s16,dbl,int16_t,double,CONVERT_INT_FLOAT_PI)
CONVERT_PI(s32,dbl,int32_t,double,CONVERT_INT_FLOAT_PI)
CONVERT_PI(flt,dbl,float,double,CONVERT_FLOAT_FLOAT_PI)
CONVERT_PI(dbl,dbl,double,double,CONVERT_FLOAT_FLOAT_PI)

///////////////////////////////////////////////////////////////////////////////
// interleaved/planar int/float -> interleaved int/float.
#define CONVERT(sfx) \
static int convert_##sfx(convert_t *convert) \
{ \
  switch (convert->fw->frame->format) { \
  /* interleaved */ \
  case AV_SAMPLE_FMT_U8: \
    return convert_##sfx##_s8i(convert); \
  case AV_SAMPLE_FMT_S16: \
    return convert_##sfx##_s16i(convert); \
  case AV_SAMPLE_FMT_S32: \
    return convert_##sfx##_s32i(convert); \
  case AV_SAMPLE_FMT_FLT: \
    return convert_##sfx##_flti(convert); \
  case AV_SAMPLE_FMT_DBL: \
    return convert_##sfx##_dbli(convert); \
  /* default */ \
  default: \
    DMESSAGE("output sample format not supported yet"); \
    break; \
  } \
 \
  return -1; \
}

CONVERT(s8i)
CONVERT(s16i)
CONVERT(s32i)
CONVERT(flti)
CONVERT(dbli)

CONVERT(s8p)
CONVERT(s16p)
CONVERT(s32p)
CONVERT(fltp)
CONVERT(dblp)

///////////////////////////////////////////////////////////////////////////////
int ffsox_frame_convert(frame_t *fr, frame_t *fw, double q)
{
  convert_t convert;
  int code;

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

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

  if (code<0) {
    DMESSAGE("converion not supported");
    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;
}