File: audioframe.c

package info (click to toggle)
gavl 1.4.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 14,912 kB
  • sloc: ansic: 437,319; sh: 10,487; makefile: 238
file content (530 lines) | stat: -rw-r--r-- 15,425 bytes parent folder | download | duplicates (3)
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*****************************************************************
 * gavl - a general purpose audio/video processing library
 *
 * Copyright (c) 2001 - 2011 Members of the Gmerlin project
 * gmerlin-general@lists.sourceforge.net
 * http://gmerlin.sourceforge.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 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 program.  If not, see <http://www.gnu.org/licenses/>.
 * *****************************************************************/


#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include <config.h>
#include <gavl.h>
#include <accel.h>
#include <bswap.h>

/* Taken from a52dec (thanks guys) */

#ifdef HAVE_MEMALIGN
/* some systems have memalign() but no declaration for it */
void * memalign (size_t align, size_t size);
#else
/* assume malloc alignment is sufficient */
#define memalign(align,size) malloc (size)
#endif

#define ALIGNMENT_BYTES 16

gavl_audio_frame_t *
gavl_audio_frame_create(const gavl_audio_format_t * format)
  {
  gavl_audio_frame_t * ret;
  int num_samples;
  int i;
  ret = calloc(1, sizeof(*ret));

  if(!format)
    return ret;
  
  num_samples = ALIGNMENT_BYTES *
    ((format->samples_per_frame + ALIGNMENT_BYTES - 1) / ALIGNMENT_BYTES);
  
  switch(format->sample_format)
    {
    case GAVL_SAMPLE_U8:
      ret->channel_stride = num_samples;
      ret->samples.u_8 =
        memalign(ALIGNMENT_BYTES, num_samples * format->num_channels);

      for(i = 0; i < format->num_channels; i++)
        ret->channels.u_8[i] = &ret->samples.u_8[i*num_samples];

      break;
    case GAVL_SAMPLE_S8:
      ret->channel_stride = num_samples;
      ret->samples.s_8 =
        memalign(ALIGNMENT_BYTES, num_samples * format->num_channels);

      for(i = 0; i < format->num_channels; i++)
        ret->channels.s_8[i] = &ret->samples.s_8[i*num_samples];

      break;
    case GAVL_SAMPLE_U16:
      ret->channel_stride = num_samples * 2;
      ret->samples.u_16 =
        memalign(ALIGNMENT_BYTES, 2 * num_samples * format->num_channels);
      for(i = 0; i < format->num_channels; i++)
        ret->channels.u_16[i] = &ret->samples.u_16[i*num_samples];

      break;
    case GAVL_SAMPLE_S16:
      ret->channel_stride = num_samples * 2;
      ret->samples.s_16 =
        memalign(ALIGNMENT_BYTES, 2 * num_samples * format->num_channels);
      for(i = 0; i < format->num_channels; i++)
        ret->channels.s_16[i] = &ret->samples.s_16[i*num_samples];

      break;

    case GAVL_SAMPLE_S32:
      ret->channel_stride = num_samples * 4;
      ret->samples.s_32 =
        memalign(ALIGNMENT_BYTES, 4 * num_samples * format->num_channels);
      for(i = 0; i < format->num_channels; i++)
        ret->channels.s_32[i] = &ret->samples.s_32[i*num_samples];

      break;

    case GAVL_SAMPLE_FLOAT:
      ret->channel_stride = num_samples * sizeof(float);
      ret->samples.f =
        memalign(ALIGNMENT_BYTES, sizeof(float) * num_samples * format->num_channels);

      for(i = 0; i < format->num_channels; i++)
        ret->channels.f[i] = &ret->samples.f[i*num_samples];

      break;
    case GAVL_SAMPLE_DOUBLE:
      ret->channel_stride = num_samples * sizeof(double);
      ret->samples.d =
        memalign(ALIGNMENT_BYTES, sizeof(double) * num_samples * format->num_channels);

      for(i = 0; i < format->num_channels; i++)
        ret->channels.d[i] = &ret->samples.d[i*num_samples];

      break;
    case GAVL_SAMPLE_NONE:
      {
      fprintf(stderr, "Sample format not specified for audio frame\n");
      return ret;
      }
    }
  return ret;
  }

void gavl_audio_frame_copy_ptrs(const gavl_audio_format_t * format,
                                gavl_audio_frame_t * dst,
                                const gavl_audio_frame_t * src)
  {
  int i;
  dst->samples.s_8 = src->samples.s_8;
  for(i = 0; i < format->num_channels; i++)
    dst->channels.s_8[i] = src->channels.s_8[i];
  dst->timestamp = src->timestamp;
  dst->valid_samples = src->valid_samples;
  }


void gavl_audio_frame_destroy(gavl_audio_frame_t * frame)
  {
  if(frame->samples.s_8)
    free(frame->samples.s_8);
  free(frame);
  }

void gavl_audio_frame_mute_samples(gavl_audio_frame_t * frame,
                                   const gavl_audio_format_t * format,
                                   int num_samples)
  {
  int i;
  int imax;
  imax = format->num_channels * num_samples;
  
  switch(format->sample_format)
    {
    case GAVL_SAMPLE_NONE:
      break;
    case GAVL_SAMPLE_U8:
      for(i = 0; i < imax; i++)
        frame->samples.u_8[i] = 0x80;
      break;
    case GAVL_SAMPLE_S8:
      for(i = 0; i < imax; i++)
        frame->samples.u_8[i] = 0x00;
      break;
    case GAVL_SAMPLE_U16:
      for(i = 0; i < imax; i++)
        frame->samples.u_16[i] = 0x8000;
      break;
    case GAVL_SAMPLE_S16:
      for(i = 0; i < imax; i++)
        frame->samples.s_16[i] = 0x0000;
      break;
    case GAVL_SAMPLE_S32:
      for(i = 0; i < imax; i++)
        frame->samples.s_32[i] = 0x00000000;
      break;
    case GAVL_SAMPLE_FLOAT:
      for(i = 0; i < imax; i++)
        frame->samples.f[i] = 0.0;
      break;
    case GAVL_SAMPLE_DOUBLE:
      for(i = 0; i < imax; i++)
        frame->samples.d[i] = 0.0;
      break;
    }
  frame->valid_samples = num_samples;
  }

void gavl_audio_frame_mute(gavl_audio_frame_t * frame,
                           const gavl_audio_format_t * format)
  {
  gavl_audio_frame_mute_samples(frame,
                                format,
                                format->samples_per_frame);
  }

void gavl_audio_frame_mute_channel(gavl_audio_frame_t * frame,
                                   const gavl_audio_format_t * format,
                                   int channel)
  {
  int i;
  int imax;
  int offset = 0;
  int advance = 0;

  switch(format->interleave_mode)
    {
    case GAVL_INTERLEAVE_ALL:
      offset = channel;
      advance = format->num_channels;
      break;
    case GAVL_INTERLEAVE_NONE:
      offset = channel * format->samples_per_frame;
      advance = 1;
      break;
    case GAVL_INTERLEAVE_2:
      if(channel & 1)
        offset = (channel-1) * format->samples_per_frame + 1;
      else
        offset = channel * format->samples_per_frame;

      if((channel == format->num_channels - 1) &&
         (format->num_channels & 1))
        advance = 1;
      else
        advance = 2;
      
      break;
    }
  
  imax = format->samples_per_frame;
  
  switch(format->sample_format)
    {
    case GAVL_SAMPLE_NONE:
      break;
    case GAVL_SAMPLE_U8:
      for(i = 0; i < imax; i++)
        frame->samples.u_8[offset + advance * i] = 0x80;
      break;
    case GAVL_SAMPLE_S8:
      for(i = 0; i < imax; i++)
        frame->samples.u_8[offset + advance * i] = 0x00;
      break;
    case GAVL_SAMPLE_U16:
      for(i = 0; i < imax; i++)
        frame->samples.u_16[offset + advance * i] = 0x8000;
      break;
    case GAVL_SAMPLE_S16:
      for(i = 0; i < imax; i++)
        frame->samples.s_16[offset + advance * i] = 0x0000;
      break;
    case GAVL_SAMPLE_S32:
      for(i = 0; i < imax; i++)
        frame->samples.s_32[offset + advance * i] = 0x00000000;
      break;
    case GAVL_SAMPLE_FLOAT:
      for(i = 0; i < imax; i++)
        frame->samples.f[offset + advance * i] = 0.0;
      break;
    case GAVL_SAMPLE_DOUBLE:
      for(i = 0; i < imax; i++)
        frame->samples.d[offset + advance * i] = 0.0;
      break;
    }
  }


int gavl_audio_frame_copy(const gavl_audio_format_t * format,
                          gavl_audio_frame_t * dst,
                          const gavl_audio_frame_t * src,
                          int out_pos,
                          int in_pos,
                          int out_size,
                          int in_size)
  {
  int i;
  int bytes_per_sample;
  int samples_to_copy;
  gavl_init_memcpy();
  samples_to_copy = (in_size < out_size) ? in_size : out_size;

  if(!dst)
    return samples_to_copy;

  bytes_per_sample = gavl_bytes_per_sample(format->sample_format);
  
  switch(format->interleave_mode)
    {
    case GAVL_INTERLEAVE_NONE:
      for(i = 0; i < format->num_channels; i++)
        {
        gavl_memcpy(&dst->channels.s_8[i][out_pos * bytes_per_sample],
                    &src->channels.s_8[i][in_pos * bytes_per_sample],
                    samples_to_copy * bytes_per_sample);
        }
      break;
    case GAVL_INTERLEAVE_2:
      for(i = 0; i < format->num_channels/2; i++)
        {
        gavl_memcpy(&dst->channels.s_8[i*2][2 * out_pos * bytes_per_sample],
                    &src->channels.s_8[i*2][2 * in_pos * bytes_per_sample],
                    2*samples_to_copy * bytes_per_sample);
        }
      /* Last channel is not interleaved */
      if(format->num_channels & 1)
        {
        gavl_memcpy(&dst->channels.s_8[format->num_channels-1][2 * out_pos * bytes_per_sample],
                    &src->channels.s_8[format->num_channels-1][2 * in_pos * bytes_per_sample],
                    2*samples_to_copy * bytes_per_sample);
        }
      break;
    case GAVL_INTERLEAVE_ALL:
      gavl_memcpy(&dst->samples.s_8[format->num_channels * out_pos * bytes_per_sample],
                  &src->samples.s_8[format->num_channels * in_pos * bytes_per_sample],
                  format->num_channels * samples_to_copy * bytes_per_sample);
      break;
    }
  return samples_to_copy;
  }

void gavl_audio_frame_null(gavl_audio_frame_t * f)
  {
  memset(f, 0, sizeof(*f));
  }

GAVL_PUBLIC
void gavl_audio_frame_get_subframe(const gavl_audio_format_t * format,
                                   gavl_audio_frame_t * src,
                                   gavl_audio_frame_t * dst,
                                   int start, int len)
  {
  int i;
  int bytes_per_sample = gavl_bytes_per_sample(format->sample_format);

  switch(format->interleave_mode)
    {
    case GAVL_INTERLEAVE_ALL:
      dst->samples.s_8 = src->samples.s_8 +
        bytes_per_sample * start * format->num_channels;
      break;
    case GAVL_INTERLEAVE_NONE:
      for(i = 0; i < format->num_channels; i++)
        dst->channels.s_8[i] =
          src->channels.s_8[i] + bytes_per_sample * start;
      break;
    case GAVL_INTERLEAVE_2:
      for(i = 0; i < format->num_channels/2; i++)
        dst->channels.s_8[i*2] =
          src->channels.s_8[i*2] + bytes_per_sample * start * 2;
      if(format->num_channels & 1)
        {
        dst->channels.s_8[format->num_channels-1] =
          src->channels.s_8[format->num_channels-1] +
          bytes_per_sample * start;
        }
      break;
    }
  dst->valid_samples = len;
  }

int gavl_audio_frames_equal(const gavl_audio_format_t * format,
                            const gavl_audio_frame_t * f1,
                            const gavl_audio_frame_t * f2)
  {
  int i;
  int bytes;

  if(f1->valid_samples != f2->valid_samples)
    return 0;
  
  switch(format->interleave_mode)
    {
    case GAVL_INTERLEAVE_ALL:
      bytes = f1->valid_samples * format->num_channels *
        gavl_bytes_per_sample(format->sample_format);
      if(memcmp(f1->samples.s_8, f2->samples.s_8, bytes))
        return 0;
      break;
    case GAVL_INTERLEAVE_2:
      bytes = f1->valid_samples * 2 *
        gavl_bytes_per_sample(format->sample_format);

      for(i = 0; i < format->num_channels/2; i++)
        {
        if(memcmp(f1->channels.s_8[i*2], f2->channels.s_8[i*2], bytes))
          return 0;
        }
      if(format->num_channels % 2)
        {
        if(memcmp(f1->channels.s_8[format->num_channels-1],
                  f2->channels.s_8[format->num_channels-1], bytes/2))
          return 0;
        }
      break;
    case GAVL_INTERLEAVE_NONE:
      bytes = f1->valid_samples *
        gavl_bytes_per_sample(format->sample_format);
      for(i = 0; i < format->num_channels/2; i++)
        {
        if(memcmp(f1->channels.s_8[i], f2->channels.s_8[i], bytes))
          return 0;
        }
      break;
    }
  return 1;
  }

static void do_plot(const gavl_audio_format_t * format,
                    const gavl_audio_frame_t * frame,
                    FILE * out)
  {
  int i, j;
  for(j = 0; j < frame->valid_samples; j++)
    {
    fprintf(out, "%d", j);
    switch(format->sample_format)
      {
      case GAVL_SAMPLE_U8:
        for(i = 0; i < format->num_channels; i++)
          fprintf(out, " %d", frame->channels.u_8[i][j]);
        break;
      case GAVL_SAMPLE_S8:
        for(i = 0; i < format->num_channels; i++)
          fprintf(out, " %d", frame->channels.s_8[i][j]);

        break;
      case GAVL_SAMPLE_U16:
        for(i = 0; i < format->num_channels; i++)
          fprintf(out, " %d", frame->channels.u_16[i][j]);

        break;
      case GAVL_SAMPLE_S16:
        for(i = 0; i < format->num_channels; i++)
          fprintf(out, " %d", frame->channels.s_16[i][j]);

        break;

      case GAVL_SAMPLE_S32:
        for(i = 0; i < format->num_channels; i++)
          fprintf(out, " %d", frame->channels.s_32[i][j]);

        break;

      case GAVL_SAMPLE_FLOAT:
        for(i = 0; i < format->num_channels; i++)
          fprintf(out, " %f", frame->channels.f[i][j]);

        break;
      case GAVL_SAMPLE_DOUBLE:
        for(i = 0; i < format->num_channels; i++)
          fprintf(out, " %f", frame->channels.d[i][j]);

        break;
      case GAVL_SAMPLE_NONE:
        break;
      }
    fprintf(out, "\n");
    }
  }


int gavl_audio_frame_plot(const gavl_audio_format_t * format,
                          const gavl_audio_frame_t * frame,
                          const char * name_base)
  {
  int i;
  gavl_audio_converter_t * cnv;
  gavl_audio_format_t plot_format;
  int do_convert;
  FILE * out;
  char * filename = malloc(strlen(name_base) + 5);

  /* Write data file */
  strcpy(filename, name_base);
  strcat(filename, ".dat");
  out = fopen(filename, "w");
  if(!out)
    return 0;

  cnv = gavl_audio_converter_create();
  gavl_audio_format_copy(&plot_format, 
                         format);
  plot_format.interleave_mode = GAVL_INTERLEAVE_NONE;

  plot_format.samples_per_frame = frame->valid_samples;
  
  do_convert =
    gavl_audio_converter_init(cnv, format, &plot_format);
  
  if(do_convert)
    {
    gavl_audio_frame_t * plot_frame = gavl_audio_frame_create(&plot_format);
    gavl_audio_convert(cnv, frame, plot_frame);
    
    do_plot(&plot_format, plot_frame, out);
    gavl_audio_frame_destroy(plot_frame);
    }
  else
    do_plot(format, frame, out);

  fclose(out);

  /* Write gnuplot file */
  strcpy(filename, name_base);
  strcat(filename, ".gnu");
  out = fopen(filename, "w");
  if(!out)
    return 0;
  
  fprintf(out, "plot ");

  for(i = 0; i < format->num_channels; i++)
    {
    if(i)
      fprintf(out, ", ");
    fprintf(out, "\"%s.dat\" using 1:%d title \"%s\"",
            name_base, i+2,
            gavl_channel_id_to_string(format->channel_locations[i]));
    }
  fprintf(out, "\n");
  fclose(out);
  return 1;
  }