269
271
#endif /* GST_REMOVE_DEPRECATED */
273
#define SINT (GST_AUDIO_FORMAT_FLAG_INTEGER | GST_AUDIO_FORMAT_FLAG_SIGNED)
274
#define UINT (GST_AUDIO_FORMAT_FLAG_INTEGER)
276
#define MAKE_FORMAT(str,flags,end,width,depth,silent) \
277
{ GST_AUDIO_FORMAT_ ##str, G_STRINGIFY(str), flags, end, width, depth, silent }
279
#define SILENT_0 { 0, 0, 0, 0, 0, 0, 0, 0 }
280
#define SILENT_U8 { 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 }
281
#define SILENT_U16LE { 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80 }
282
#define SILENT_U16BE { 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00 }
283
#define SILENT_U24_32LE { 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00 }
284
#define SILENT_U24_32BE { 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00 }
285
#define SILENT_U32LE { 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80 }
286
#define SILENT_U32BE { 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 }
287
#define SILENT_U24LE { 0x00, 0x00, 0x80, 0x00, 0x00, 0x80 }
288
#define SILENT_U24BE { 0x80, 0x00, 0x00, 0x80, 0x00, 0x00 }
289
#define SILENT_U20LE { 0x00, 0x00, 0x08, 0x00, 0x00, 0x08 }
290
#define SILENT_U20BE { 0x08, 0x00, 0x00, 0x08, 0x00, 0x00 }
291
#define SILENT_U18LE { 0x00, 0x00, 0x02, 0x00, 0x00, 0x02 }
292
#define SILENT_U18BE { 0x02, 0x00, 0x00, 0x02, 0x00, 0x00 }
294
static GstAudioFormatInfo formats[] = {
295
{GST_AUDIO_FORMAT_UNKNOWN, "UNKNOWN", 0, 0, 0, 0},
297
MAKE_FORMAT (S8, SINT, 0, 8, 8, SILENT_0),
298
MAKE_FORMAT (U8, UINT, 0, 8, 8, SILENT_U8),
300
MAKE_FORMAT (S16LE, SINT, G_LITTLE_ENDIAN, 16, 16, SILENT_0),
301
MAKE_FORMAT (S16BE, SINT, G_BIG_ENDIAN, 16, 16, SILENT_0),
302
MAKE_FORMAT (U16LE, UINT, G_LITTLE_ENDIAN, 16, 16, SILENT_U16LE),
303
MAKE_FORMAT (U16BE, UINT, G_BIG_ENDIAN, 16, 16, SILENT_U16BE),
304
/* 24 bit in low 3 bytes of 32 bits */
305
MAKE_FORMAT (S24_32LE, SINT, G_LITTLE_ENDIAN, 32, 24, SILENT_0),
306
MAKE_FORMAT (S24_32BE, SINT, G_BIG_ENDIAN, 32, 24, SILENT_0),
307
MAKE_FORMAT (U24_32LE, UINT, G_LITTLE_ENDIAN, 32, 24, SILENT_U24_32LE),
308
MAKE_FORMAT (U24_32BE, UINT, G_BIG_ENDIAN, 32, 24, SILENT_U24_32BE),
310
MAKE_FORMAT (S32LE, SINT, G_LITTLE_ENDIAN, 32, 32, SILENT_0),
311
MAKE_FORMAT (S32BE, SINT, G_BIG_ENDIAN, 32, 32, SILENT_0),
312
MAKE_FORMAT (U32LE, UINT, G_LITTLE_ENDIAN, 32, 32, SILENT_U32LE),
313
MAKE_FORMAT (U32BE, UINT, G_BIG_ENDIAN, 32, 32, SILENT_U32BE),
314
/* 24 bit in 3 bytes */
315
MAKE_FORMAT (S24LE, SINT, G_LITTLE_ENDIAN, 24, 24, SILENT_0),
316
MAKE_FORMAT (S24BE, SINT, G_BIG_ENDIAN, 24, 24, SILENT_0),
317
MAKE_FORMAT (U24LE, UINT, G_LITTLE_ENDIAN, 24, 24, SILENT_U24LE),
318
MAKE_FORMAT (U24BE, UINT, G_BIG_ENDIAN, 24, 24, SILENT_U24BE),
319
/* 20 bit in 3 bytes */
320
MAKE_FORMAT (S20LE, SINT, G_LITTLE_ENDIAN, 24, 20, SILENT_0),
321
MAKE_FORMAT (S20BE, SINT, G_BIG_ENDIAN, 24, 20, SILENT_0),
322
MAKE_FORMAT (U20LE, UINT, G_LITTLE_ENDIAN, 24, 20, SILENT_U20LE),
323
MAKE_FORMAT (U20BE, UINT, G_BIG_ENDIAN, 24, 20, SILENT_U20BE),
324
/* 18 bit in 3 bytes */
325
MAKE_FORMAT (S18LE, SINT, G_LITTLE_ENDIAN, 24, 18, SILENT_0),
326
MAKE_FORMAT (S18BE, SINT, G_BIG_ENDIAN, 24, 18, SILENT_0),
327
MAKE_FORMAT (U18LE, UINT, G_LITTLE_ENDIAN, 24, 18, SILENT_U18LE),
328
MAKE_FORMAT (U18BE, UINT, G_BIG_ENDIAN, 24, 18, SILENT_U18BE),
330
MAKE_FORMAT (F32LE, GST_AUDIO_FORMAT_FLAG_FLOAT, G_LITTLE_ENDIAN, 32, 32,
332
MAKE_FORMAT (F32BE, GST_AUDIO_FORMAT_FLAG_FLOAT, G_BIG_ENDIAN, 32, 32,
334
MAKE_FORMAT (F64LE, GST_AUDIO_FORMAT_FLAG_FLOAT, G_LITTLE_ENDIAN, 64, 64,
336
MAKE_FORMAT (F64BE, GST_AUDIO_FORMAT_FLAG_FLOAT, G_BIG_ENDIAN, 64, 64,
340
static GstAudioFormat
341
gst_audio_format_from_caps_structure (const GstStructure * s)
343
gint endianness, width, depth;
346
if (gst_structure_has_name (s, "audio/x-raw-int")) {
349
if (!gst_structure_get_boolean (s, "signed", &sign))
350
goto missing_field_signed;
352
if (!gst_structure_get_int (s, "endianness", &endianness))
353
goto missing_field_endianness;
355
if (!gst_structure_get_int (s, "width", &width))
356
goto missing_field_width;
358
if (!gst_structure_get_int (s, "depth", &depth))
359
goto missing_field_depth;
361
for (i = 0; i < G_N_ELEMENTS (formats); i++) {
362
if (GST_AUDIO_FORMAT_INFO_IS_INTEGER (&formats[i]) &&
363
sign == GST_AUDIO_FORMAT_INFO_IS_SIGNED (&formats[i]) &&
364
GST_AUDIO_FORMAT_INFO_ENDIANNESS (&formats[i]) == endianness &&
365
GST_AUDIO_FORMAT_INFO_WIDTH (&formats[i]) == width &&
366
GST_AUDIO_FORMAT_INFO_DEPTH (&formats[i]) == depth) {
367
return GST_AUDIO_FORMAT_INFO_FORMAT (&formats[i]);
370
} else if (gst_structure_has_name (s, "audio/x-raw-float")) {
371
/* fallbacks are for backwards compatibility (is this needed at all?) */
372
if (!gst_structure_get_int (s, "endianness", &endianness)) {
373
GST_WARNING ("float audio caps without endianness %" GST_PTR_FORMAT, s);
374
endianness = G_BYTE_ORDER;
377
if (!gst_structure_get_int (s, "width", &width)) {
378
GST_WARNING ("float audio caps without width %" GST_PTR_FORMAT, s);
382
for (i = 0; i < G_N_ELEMENTS (formats); i++) {
383
if (GST_AUDIO_FORMAT_INFO_IS_FLOAT (&formats[i]) &&
384
GST_AUDIO_FORMAT_INFO_ENDIANNESS (&formats[i]) == endianness &&
385
GST_AUDIO_FORMAT_INFO_WIDTH (&formats[i]) == width) {
386
return GST_AUDIO_FORMAT_INFO_FORMAT (&formats[i]);
392
return GST_AUDIO_FORMAT_UNKNOWN;
394
missing_field_signed:
396
GST_ERROR ("missing 'signed' field in audio caps %" GST_PTR_FORMAT, s);
397
return GST_AUDIO_FORMAT_UNKNOWN;
399
missing_field_endianness:
401
GST_ERROR ("missing 'endianness' field in audio caps %" GST_PTR_FORMAT, s);
402
return GST_AUDIO_FORMAT_UNKNOWN;
406
GST_ERROR ("missing 'depth' field in audio caps %" GST_PTR_FORMAT, s);
407
return GST_AUDIO_FORMAT_UNKNOWN;
411
GST_ERROR ("missing 'width' field in audio caps %" GST_PTR_FORMAT, s);
412
return GST_AUDIO_FORMAT_UNKNOWN;
416
/* FIXME: remove these if we don't actually go for deep alloc positions */
418
gst_audio_info_init (GstAudioInfo * info)
420
memset (info, 0, sizeof (GstAudioInfo));
424
gst_audio_info_clear (GstAudioInfo * info)
426
memset (info, 0, sizeof (GstAudioInfo));
430
gst_audio_info_copy (GstAudioInfo * info)
432
return (GstAudioInfo *) g_slice_copy (sizeof (GstAudioInfo), info);
436
gst_audio_info_free (GstAudioInfo * info)
438
g_slice_free (GstAudioInfo, info);
442
gst_audio_info_set_format (GstAudioInfo * info, GstAudioFormat format,
443
gint rate, gint channels)
445
const GstAudioFormatInfo *finfo;
447
g_return_if_fail (info != NULL);
448
g_return_if_fail (format != GST_AUDIO_FORMAT_UNKNOWN);
450
finfo = &formats[format];
455
info->channels = channels;
456
info->bpf = (finfo->width * channels) / 8;
459
/* from multichannel.c */
460
void priv_gst_audio_info_fill_default_channel_positions (GstAudioInfo * info);
463
* gst_audio_info_from_caps:
464
* @info: a #GstAudioInfo
467
* Parse @caps and update @info.
469
* Returns: TRUE if @caps could be parsed
474
gst_audio_info_from_caps (GstAudioInfo * info, const GstCaps * caps)
477
GstAudioFormat format;
479
const GValue *pos_val_arr, *pos_val_entry;
482
g_return_val_if_fail (info != NULL, FALSE);
483
g_return_val_if_fail (caps != NULL, FALSE);
484
g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
486
GST_DEBUG ("parsing caps %" GST_PTR_FORMAT, caps);
488
str = gst_caps_get_structure (caps, 0);
490
format = gst_audio_format_from_caps_structure (str);
491
if (format == GST_AUDIO_FORMAT_UNKNOWN)
494
if (!gst_structure_get_int (str, "rate", &rate))
496
if (!gst_structure_get_int (str, "channels", &channels))
499
gst_audio_info_set_format (info, format, rate, channels);
501
pos_val_arr = gst_structure_get_value (str, "channel-positions");
503
if (channels <= G_N_ELEMENTS (info->position)) {
504
for (i = 0; i < channels; i++) {
505
pos_val_entry = gst_value_array_get_value (pos_val_arr, i);
506
info->position[i] = g_value_get_enum (pos_val_entry);
509
/* for that many channels, the positions are always NONE */
510
for (i = 0; i < G_N_ELEMENTS (info->position); i++)
511
info->position[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
512
info->flags |= GST_AUDIO_FLAG_DEFAULT_POSITIONS;
515
info->flags |= GST_AUDIO_FLAG_DEFAULT_POSITIONS;
516
priv_gst_audio_info_fill_default_channel_positions (info);
524
GST_ERROR ("unknown format given");
529
GST_ERROR ("no rate property given");
534
GST_ERROR ("no channels property given");
540
* gst_audio_info_to_caps:
541
* @info: a #GstAudioInfo
543
* Convert the values of @info into a #GstCaps.
545
* Returns: (transfer full): the new #GstCaps containing the
551
gst_audio_info_to_caps (GstAudioInfo * info)
555
g_return_val_if_fail (info != NULL, NULL);
556
g_return_val_if_fail (info->finfo != NULL, NULL);
557
g_return_val_if_fail (info->finfo->format != GST_AUDIO_FORMAT_UNKNOWN, NULL);
559
if (GST_AUDIO_FORMAT_INFO_IS_INTEGER (info->finfo)) {
560
caps = gst_caps_new_simple ("audio/x-raw-int",
561
"width", G_TYPE_INT, GST_AUDIO_INFO_WIDTH (info),
562
"depth", G_TYPE_INT, GST_AUDIO_INFO_DEPTH (info),
563
"endianness", G_TYPE_INT,
564
GST_AUDIO_FORMAT_INFO_ENDIANNESS (info->finfo), "signed",
565
G_TYPE_BOOLEAN, GST_AUDIO_FORMAT_INFO_IS_SIGNED (info->finfo), "rate",
566
G_TYPE_INT, GST_AUDIO_INFO_RATE (info), "channels", G_TYPE_INT,
567
GST_AUDIO_INFO_CHANNELS (info), NULL);
568
} else if (GST_AUDIO_FORMAT_INFO_IS_FLOAT (info->finfo)) {
569
caps = gst_caps_new_simple ("audio/x-raw-float",
570
"width", G_TYPE_INT, GST_AUDIO_INFO_WIDTH (info),
571
"endianness", G_TYPE_INT,
572
GST_AUDIO_FORMAT_INFO_ENDIANNESS (info->finfo), "rate", G_TYPE_INT,
573
GST_AUDIO_INFO_RATE (info), "channels", G_TYPE_INT,
574
GST_AUDIO_INFO_CHANNELS (info), NULL);
576
GST_ERROR ("unknown audio format, neither integer nor float");
580
if (info->channels > 2) {
581
GValue pos_val_arr = { 0 }
587
/* build gvaluearray from positions */
588
g_value_init (&pos_val_arr, GST_TYPE_ARRAY);
589
g_value_init (&pos_val_entry, GST_TYPE_AUDIO_CHANNEL_POSITION);
590
for (i = 0; i < info->channels; i++) {
591
/* if we have many many channels, all positions are NONE */
592
if (info->channels <= 64)
593
g_value_set_enum (&pos_val_entry, info->position[i]);
595
g_value_set_enum (&pos_val_entry, GST_AUDIO_CHANNEL_POSITION_NONE);
597
gst_value_array_append_value (&pos_val_arr, &pos_val_entry);
599
g_value_unset (&pos_val_entry);
601
/* add to structure */
602
str = gst_caps_get_structure (caps, 0);
603
gst_structure_set_value (str, "channel-positions", &pos_val_arr);
604
g_value_unset (&pos_val_arr);
611
* gst_audio_format_convert:
612
* @info: a #GstAudioInfo
613
* @src_format: #GstFormat of the @src_value
614
* @src_value: value to convert
615
* @dest_format: #GstFormat of the @dest_value
616
* @dest_value: pointer to destination value
618
* Converts among various #GstFormat types. This function handles
619
* GST_FORMAT_BYTES, GST_FORMAT_TIME, and GST_FORMAT_DEFAULT. For
620
* raw audio, GST_FORMAT_DEFAULT corresponds to audio frames. This
621
* function can be used to handle pad queries of the type GST_QUERY_CONVERT.
623
* Returns: TRUE if the conversion was successful.
628
gst_audio_info_convert (GstAudioInfo * info,
629
GstFormat src_fmt, gint64 src_val, GstFormat dest_fmt, gint64 * dest_val)
634
GST_DEBUG ("converting value %" G_GINT64_FORMAT " from %s (%d) to %s (%d)",
635
src_val, gst_format_get_name (src_fmt), src_fmt,
636
gst_format_get_name (dest_fmt), dest_fmt);
638
if (src_fmt == dest_fmt || src_val == -1) {
643
/* get important info */
644
bpf = GST_AUDIO_INFO_BPF (info);
645
rate = GST_AUDIO_INFO_RATE (info);
647
if (bpf == 0 || rate == 0) {
648
GST_DEBUG ("no rate or bpf configured");
654
case GST_FORMAT_BYTES:
656
case GST_FORMAT_TIME:
657
*dest_val = GST_FRAMES_TO_CLOCK_TIME (src_val / bpf, rate);
659
case GST_FORMAT_DEFAULT:
660
*dest_val = src_val / bpf;
667
case GST_FORMAT_DEFAULT:
669
case GST_FORMAT_TIME:
670
*dest_val = GST_FRAMES_TO_CLOCK_TIME (src_val, rate);
672
case GST_FORMAT_BYTES:
673
*dest_val = src_val * bpf;
680
case GST_FORMAT_TIME:
682
case GST_FORMAT_DEFAULT:
683
*dest_val = GST_CLOCK_TIME_TO_FRAMES (src_val, rate);
685
case GST_FORMAT_BYTES:
686
*dest_val = GST_CLOCK_TIME_TO_FRAMES (src_val, rate);
699
GST_DEBUG ("ret=%d result %" G_GINT64_FORMAT, res, *dest_val);
272
705
* gst_audio_buffer_clip:
273
706
* @buffer: The buffer to clip.