~ubuntu-branches/ubuntu/lucid/gavl/lucid

« back to all changes in this revision

Viewing changes to gavl/audioformat.c

  • Committer: Bazaar Package Importer
  • Author(s): Free Ekanayaka
  • Date: 2006-05-17 14:24:46 UTC
  • Revision ID: james.westby@ubuntu.com-20060517142446-iqm0jgfbkmy27n5w
Tags: upstream-0.2.3
ImportĀ upstreamĀ versionĀ 0.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************
 
2
 
 
3
  audioformat.c
 
4
 
 
5
  Copyright (c) 2003-2004 by Burkhard Plaum - plaum@ipf.uni-stuttgart.de
 
6
 
 
7
  http://gmerlin.sourceforge.net
 
8
 
 
9
  This program is distributed in the hope that it will be useful,
 
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
  GNU General Public License for more details.
 
13
 
 
14
  You should have received a copy of the GNU General Public License
 
15
  along with this program; if not, write to the Free Software
 
16
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 
17
 
 
18
*****************************************************************/
 
19
 
 
20
 
 
21
#include <stdio.h>
 
22
#include <string.h>
 
23
#include <math.h>
 
24
#include <gavl/gavl.h>
 
25
 
 
26
static struct
 
27
  {
 
28
  gavl_sample_format_t format;
 
29
  char * name;
 
30
  }
 
31
sample_format_names[] =
 
32
  {
 
33
    { GAVL_SAMPLE_NONE,  "Not specified" },
 
34
    { GAVL_SAMPLE_U8,    "Unsigned 8 bit"},
 
35
    { GAVL_SAMPLE_S8,    "Signed 8 bit"},
 
36
    { GAVL_SAMPLE_U16,   "Unsigned 16 bit"},
 
37
    { GAVL_SAMPLE_S16,   "Signed 16 bit"},
 
38
    { GAVL_SAMPLE_S32,   "Signed 32 bit"},
 
39
    { GAVL_SAMPLE_FLOAT, "Floating point"},
 
40
  };
 
41
 
 
42
const char * gavl_sample_format_to_string(gavl_sample_format_t format)
 
43
  {
 
44
  int i;
 
45
  for(i = 0; i < sizeof(sample_format_names)/sizeof(sample_format_names[0]); i++)
 
46
    {
 
47
    if(format == sample_format_names[i].format)
 
48
      return sample_format_names[i].name;
 
49
    }
 
50
  return (char*)0;
 
51
  }
 
52
 
 
53
static struct
 
54
  {
 
55
  gavl_interleave_mode_t mode;
 
56
  char * name;
 
57
  }
 
58
interleave_mode_names[] =
 
59
  {
 
60
    { GAVL_INTERLEAVE_NONE, "Not interleaved" },
 
61
    { GAVL_INTERLEAVE_2,    "Interleaved channel pairs" },
 
62
    { GAVL_INTERLEAVE_ALL,  "All channels interleaved" },
 
63
  };
 
64
 
 
65
 
 
66
const char * gavl_interleave_mode_to_string(gavl_interleave_mode_t mode)
 
67
  {
 
68
  int i;
 
69
  for(i = 0;
 
70
      i < sizeof(interleave_mode_names)/sizeof(interleave_mode_names[0]);
 
71
      i++)
 
72
    {
 
73
    if(mode == interleave_mode_names[i].mode)
 
74
      return interleave_mode_names[i].name;
 
75
    }
 
76
  return (char*)0;
 
77
  }
 
78
 
 
79
static struct
 
80
  {
 
81
  gavl_channel_id_t id;
 
82
  char * name;
 
83
  }
 
84
channel_id_names[] =
 
85
  {
 
86
#if 0
 
87
    GAVL_CHID_NONE         = 0,   /*!< Undefined                                 */
 
88
    GAVL_CHID_FRONT_CENTER,       /*!< For mono                                  */
 
89
    GAVL_CHID_FRONT_LEFT,         /*!< Front left                                */
 
90
    GAVL_CHID_FRONT_RIGHT,        /*!< Front right                               */
 
91
    GAVL_CHID_FRONT_CENTER_LEFT,  /*!< Left of Center                            */
 
92
    GAVL_CHID_FRONT_CENTER_RIGHT, /*!< Right of Center                           */
 
93
    GAVL_CHID_REAR_LEFT,          /*!< Rear left                                 */
 
94
    GAVL_CHID_REAR_RIGHT,         /*!< Rear right                                */
 
95
    GAVL_CHID_REAR_CENTER,        /*!< Rear Center                               */
 
96
    GAVL_CHID_SIDE_LEFT,          /*!< Side left                                 */
 
97
    GAVL_CHID_SIDE_RIGHT,         /*!< Side right                                */
 
98
    GAVL_CHID_LFE,                /*!< Subwoofer                                 */
 
99
    GAVL_CHID_AUX,                /*!< Additional channel (can be more than one) */
 
100
#endif
 
101
    { GAVL_CHID_NONE,                "Unknown channel" },
 
102
    { GAVL_CHID_FRONT_CENTER,        "Front C" },
 
103
    { GAVL_CHID_FRONT_LEFT,          "Front L" },
 
104
    { GAVL_CHID_FRONT_RIGHT,         "Front R" },
 
105
    { GAVL_CHID_FRONT_CENTER_LEFT,   "Front CL" },
 
106
    { GAVL_CHID_FRONT_CENTER_RIGHT,  "Front CR" },
 
107
    { GAVL_CHID_REAR_CENTER,         "Rear C" },
 
108
    { GAVL_CHID_REAR_LEFT,           "Rear L" },
 
109
    { GAVL_CHID_REAR_RIGHT,          "Rear R" },
 
110
    { GAVL_CHID_SIDE_LEFT,           "Side L" },
 
111
    { GAVL_CHID_SIDE_RIGHT,          "Side R" },
 
112
    { GAVL_CHID_LFE,                 "LFE" },
 
113
    { GAVL_CHID_AUX,                 "AUX" },
 
114
  };
 
115
 
 
116
const char * gavl_channel_id_to_string(gavl_channel_id_t id)
 
117
  {
 
118
  int i;
 
119
  for(i = 0;
 
120
      i < sizeof(channel_id_names)/sizeof(channel_id_names[0]);
 
121
      i++)
 
122
    {
 
123
    //    fprintf(stderr, "ID: %d\n", id);
 
124
    if(id == channel_id_names[i].id)
 
125
      return channel_id_names[i].name;
 
126
    }
 
127
  return (char*)0;
 
128
  }
 
129
 
 
130
void gavl_audio_format_dump(const gavl_audio_format_t * f)
 
131
  {
 
132
  int i;
 
133
  fprintf(stderr, "  Channels:          %d\n", f->num_channels);
 
134
 
 
135
  fprintf(stderr, "  Channel order:     ");
 
136
  for(i = 0; i < f->num_channels; i++)
 
137
    {
 
138
    fprintf(stderr, "%s", gavl_channel_id_to_string(f->channel_locations[i]));
 
139
    if(i < f->num_channels - 1)
 
140
      fprintf(stderr, ", ");
 
141
    }
 
142
  fprintf(stderr, "\n");
 
143
 
 
144
  fprintf(stderr, "  Samplerate:        %d\n", f->samplerate);
 
145
  fprintf(stderr, "  Samples per frame: %d\n", f->samples_per_frame);
 
146
  fprintf(stderr, "  Interleave Mode:   %s\n",
 
147
          gavl_interleave_mode_to_string(f->interleave_mode));
 
148
  fprintf(stderr, "  Sample format:     %s\n",
 
149
          gavl_sample_format_to_string(f->sample_format));
 
150
  
 
151
  if(gavl_front_channels(f) == 3)
 
152
    {
 
153
    if(f->center_level > 0.0)
 
154
      fprintf(stderr, "  Center level:      %0.1f dB\n", 20 * log10(f->center_level));
 
155
    else
 
156
      fprintf(stderr, "  Center level:      Zero\n");
 
157
    }
 
158
  if(gavl_rear_channels(f))
 
159
    {
 
160
    if(f->rear_level > 0.0)
 
161
      fprintf(stderr, "  Rear level:        %0.1f dB\n", 20 * log10(f->rear_level));
 
162
    else
 
163
      fprintf(stderr, "  Rear level:        Zero\n");
 
164
    }
 
165
 
 
166
  
 
167
 
 
168
  }
 
169
 
 
170
void gavl_set_channel_setup(gavl_audio_format_t * dst)
 
171
  {
 
172
  int i;
 
173
  if(dst->channel_locations[0] == GAVL_CHID_NONE)
 
174
    {
 
175
    switch(dst->num_channels)
 
176
      {
 
177
      case 1:
 
178
        dst->channel_locations[0] = GAVL_CHID_FRONT_CENTER;
 
179
        break;
 
180
      case 2: /* 2 Front channels (Stereo or Dual channels) */
 
181
        dst->channel_locations[0] = GAVL_CHID_FRONT_LEFT;
 
182
        dst->channel_locations[1] = GAVL_CHID_FRONT_RIGHT;
 
183
        break;
 
184
      case 3:
 
185
        dst->channel_locations[0] = GAVL_CHID_FRONT_LEFT;
 
186
        dst->channel_locations[1] = GAVL_CHID_FRONT_RIGHT;
 
187
        dst->channel_locations[2] = GAVL_CHID_FRONT_CENTER;
 
188
        break;
 
189
      case 4:
 
190
        dst->channel_locations[0] = GAVL_CHID_FRONT_LEFT;
 
191
        dst->channel_locations[1] = GAVL_CHID_FRONT_RIGHT;
 
192
        dst->channel_locations[2] = GAVL_CHID_REAR_LEFT;
 
193
        dst->channel_locations[3] = GAVL_CHID_REAR_RIGHT;
 
194
        break;
 
195
      case 5:
 
196
        dst->channel_locations[0] = GAVL_CHID_FRONT_LEFT;
 
197
        dst->channel_locations[1] = GAVL_CHID_FRONT_RIGHT;
 
198
        dst->channel_locations[2] = GAVL_CHID_REAR_LEFT;
 
199
        dst->channel_locations[3] = GAVL_CHID_REAR_RIGHT;
 
200
        dst->channel_locations[4] = GAVL_CHID_FRONT_CENTER;
 
201
        break;
 
202
      case 6:
 
203
        dst->channel_locations[0] = GAVL_CHID_FRONT_LEFT;
 
204
        dst->channel_locations[1] = GAVL_CHID_FRONT_RIGHT;
 
205
        dst->channel_locations[2] = GAVL_CHID_REAR_LEFT;
 
206
        dst->channel_locations[3] = GAVL_CHID_REAR_RIGHT;
 
207
        dst->channel_locations[4] = GAVL_CHID_FRONT_CENTER;
 
208
        dst->channel_locations[5] = GAVL_CHID_LFE;
 
209
        break;
 
210
      default:
 
211
        for(i = 0; i < dst->num_channels; i++)
 
212
          dst->channel_locations[i] = GAVL_CHID_AUX;
 
213
        break;
 
214
      }
 
215
    }
 
216
  }
 
217
 
 
218
void gavl_audio_format_copy(gavl_audio_format_t * dst,
 
219
                            const gavl_audio_format_t * src)
 
220
  {
 
221
  memcpy(dst, src, sizeof(*dst));
 
222
  }
 
223
 
 
224
int gavl_channel_index(const gavl_audio_format_t * f, gavl_channel_id_t id)
 
225
  {
 
226
  int i;
 
227
  for(i = 0; i < f->num_channels; i++)
 
228
    {
 
229
    if(f->channel_locations[i] == id)
 
230
      return i;
 
231
    }
 
232
  //  fprintf(stderr, "Channel %s not present!!! Format was\n",
 
233
  //          gavl_channel_id_to_string(id));
 
234
  gavl_audio_format_dump(f);
 
235
  return -1;
 
236
  }
 
237
 
 
238
int gavl_front_channels(const gavl_audio_format_t * f)
 
239
  {
 
240
  int i;
 
241
  int result = 0;
 
242
  for(i = 0; i < f->num_channels; i++)
 
243
    {
 
244
    switch(f->channel_locations[i])
 
245
      {
 
246
      case GAVL_CHID_FRONT_CENTER:
 
247
      case GAVL_CHID_FRONT_LEFT:
 
248
      case GAVL_CHID_FRONT_RIGHT:
 
249
      case GAVL_CHID_FRONT_CENTER_LEFT:
 
250
      case GAVL_CHID_FRONT_CENTER_RIGHT:
 
251
        result++;
 
252
        break;
 
253
      case GAVL_CHID_NONE:
 
254
      case GAVL_CHID_REAR_LEFT:
 
255
      case GAVL_CHID_REAR_RIGHT:
 
256
      case GAVL_CHID_REAR_CENTER:
 
257
      case GAVL_CHID_SIDE_LEFT:
 
258
      case GAVL_CHID_SIDE_RIGHT:
 
259
      case GAVL_CHID_LFE:
 
260
      case GAVL_CHID_AUX:
 
261
        break;
 
262
      }
 
263
    }
 
264
  return result;
 
265
  }
 
266
 
 
267
int gavl_rear_channels(const gavl_audio_format_t * f)
 
268
  {
 
269
  int i;
 
270
  int result = 0;
 
271
  for(i = 0; i < f->num_channels; i++)
 
272
    {
 
273
    switch(f->channel_locations[i])
 
274
      {
 
275
      case GAVL_CHID_REAR_LEFT:
 
276
      case GAVL_CHID_REAR_RIGHT:
 
277
      case GAVL_CHID_REAR_CENTER:
 
278
        result++;
 
279
        break;
 
280
      case GAVL_CHID_FRONT_CENTER:
 
281
      case GAVL_CHID_FRONT_LEFT:
 
282
      case GAVL_CHID_FRONT_RIGHT:
 
283
      case GAVL_CHID_FRONT_CENTER_LEFT:
 
284
      case GAVL_CHID_FRONT_CENTER_RIGHT:
 
285
      case GAVL_CHID_NONE:
 
286
      case GAVL_CHID_SIDE_LEFT:
 
287
      case GAVL_CHID_SIDE_RIGHT:
 
288
      case GAVL_CHID_LFE:
 
289
      case GAVL_CHID_AUX:
 
290
        break;
 
291
      }
 
292
    }
 
293
  return result;
 
294
  }
 
295
 
 
296
int gavl_side_channels(const gavl_audio_format_t * f)
 
297
  {
 
298
  int i;
 
299
  int result = 0;
 
300
  for(i = 0; i < f->num_channels; i++)
 
301
    {
 
302
    switch(f->channel_locations[i])
 
303
      {
 
304
      case GAVL_CHID_SIDE_LEFT:
 
305
      case GAVL_CHID_SIDE_RIGHT:
 
306
        result++;
 
307
        break;
 
308
      case GAVL_CHID_REAR_LEFT:
 
309
      case GAVL_CHID_REAR_RIGHT:
 
310
      case GAVL_CHID_REAR_CENTER:
 
311
      case GAVL_CHID_FRONT_CENTER:
 
312
      case GAVL_CHID_FRONT_LEFT:
 
313
      case GAVL_CHID_FRONT_RIGHT:
 
314
      case GAVL_CHID_FRONT_CENTER_LEFT:
 
315
      case GAVL_CHID_FRONT_CENTER_RIGHT:
 
316
      case GAVL_CHID_NONE:
 
317
      case GAVL_CHID_LFE:
 
318
      case GAVL_CHID_AUX:
 
319
        break;
 
320
      }
 
321
    }
 
322
  return result;
 
323
  }
 
324
 
 
325
int gavl_lfe_channels(const gavl_audio_format_t * f)
 
326
  {
 
327
  int i;
 
328
  int result = 0;
 
329
  for(i = 0; i < f->num_channels; i++)
 
330
    {
 
331
    switch(f->channel_locations[i])
 
332
      {
 
333
      case GAVL_CHID_LFE:
 
334
        result++;
 
335
        break;
 
336
      case GAVL_CHID_SIDE_LEFT:
 
337
      case GAVL_CHID_SIDE_RIGHT:
 
338
      case GAVL_CHID_REAR_LEFT:
 
339
      case GAVL_CHID_REAR_RIGHT:
 
340
      case GAVL_CHID_REAR_CENTER:
 
341
      case GAVL_CHID_FRONT_CENTER:
 
342
      case GAVL_CHID_FRONT_LEFT:
 
343
      case GAVL_CHID_FRONT_RIGHT:
 
344
      case GAVL_CHID_FRONT_CENTER_LEFT:
 
345
      case GAVL_CHID_FRONT_CENTER_RIGHT:
 
346
      case GAVL_CHID_NONE:
 
347
      case GAVL_CHID_AUX:
 
348
        break;
 
349
      }
 
350
    }
 
351
  return result;
 
352
  }
 
353
 
 
354
int gavl_aux_channels(const gavl_audio_format_t * f)
 
355
  {
 
356
  int i;
 
357
  int result = 0;
 
358
  for(i = 0; i < f->num_channels; i++)
 
359
    {
 
360
    switch(f->channel_locations[i])
 
361
      {
 
362
      case GAVL_CHID_AUX:
 
363
        result++;
 
364
        break;
 
365
      case GAVL_CHID_SIDE_LEFT:
 
366
      case GAVL_CHID_SIDE_RIGHT:
 
367
      case GAVL_CHID_REAR_LEFT:
 
368
      case GAVL_CHID_REAR_RIGHT:
 
369
      case GAVL_CHID_REAR_CENTER:
 
370
      case GAVL_CHID_FRONT_CENTER:
 
371
      case GAVL_CHID_FRONT_LEFT:
 
372
      case GAVL_CHID_FRONT_RIGHT:
 
373
      case GAVL_CHID_FRONT_CENTER_LEFT:
 
374
      case GAVL_CHID_FRONT_CENTER_RIGHT:
 
375
      case GAVL_CHID_NONE:
 
376
      case GAVL_CHID_LFE:
 
377
        break;
 
378
      }
 
379
    }
 
380
  return result;
 
381
  }
 
382
 
 
383
 
 
384
 
 
385
int gavl_bytes_per_sample(gavl_sample_format_t format)
 
386
  {
 
387
  switch(format)
 
388
    {
 
389
    case     GAVL_SAMPLE_U8:
 
390
    case     GAVL_SAMPLE_S8:
 
391
      return 1;
 
392
      break;
 
393
    case     GAVL_SAMPLE_U16:
 
394
    case     GAVL_SAMPLE_S16:
 
395
      return 2;
 
396
      break;
 
397
    case     GAVL_SAMPLE_S32:
 
398
      return 4;
 
399
      break;
 
400
    case     GAVL_SAMPLE_FLOAT:
 
401
      return sizeof(float);
 
402
      break;
 
403
    case     GAVL_SAMPLE_NONE:
 
404
      return 0;
 
405
    }
 
406
  return 0;
 
407
  }
 
408
 
 
409