2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
25 #include "multichannel-enumtypes.h"
27 #include <gst/gststructure.h>
31 * @short_description: Support library for audio elements
35 gst_audio_frame_byte_size (GstPad * pad)
37 /* calculate byte size of an audio frame
38 * this should be moved closer to the gstreamer core
39 * and be implemented for every mime type IMO
40 * returns -1 if there's an error (to avoid division by zero),
41 * or the byte size if everything's ok
46 const GstCaps *caps = NULL;
47 GstStructure *structure;
50 caps = GST_PAD_CAPS (pad);
53 /* ERROR: could not get caps of pad */
54 g_warning ("gstaudio: could not get caps of pad %s:%s\n",
55 GST_DEBUG_PAD_NAME (pad));
59 structure = gst_caps_get_structure (caps, 0);
61 gst_structure_get_int (structure, "width", &width);
62 gst_structure_get_int (structure, "channels", &channels);
63 return (width / 8) * channels;
67 gst_audio_frame_length (GstPad * pad, GstBuffer * buf)
68 /* calculate length of buffer in frames
69 * this should be moved closer to the gstreamer core
70 * and be implemented for every mime type IMO
71 * returns 0 if there's an error, or the number of frames if everything's ok
74 int frame_byte_size = 0;
76 frame_byte_size = gst_audio_frame_byte_size (pad);
77 if (frame_byte_size == 0)
80 /* FIXME: this function assumes the buffer size to be a whole multiple
81 * of the frame byte size
83 return GST_BUFFER_SIZE (buf) / frame_byte_size;
87 gst_audio_duration_from_pad_buffer (GstPad * pad, GstBuffer * buf)
89 /* calculate length in nanoseconds
91 * based on capabilities of pad
101 const GstCaps *caps = NULL;
102 GstStructure *structure;
104 g_assert (GST_IS_BUFFER (buf));
105 /* get caps of pad */
106 caps = GST_PAD_CAPS (pad);
108 /* ERROR: could not get caps of pad */
109 g_warning ("gstaudio: could not get caps of pad %s:%s\n",
110 GST_DEBUG_PAD_NAME (pad));
111 length = GST_CLOCK_TIME_NONE;
113 structure = gst_caps_get_structure (caps, 0);
114 bytes = GST_BUFFER_SIZE (buf);
115 gst_structure_get_int (structure, "width", &width);
116 gst_structure_get_int (structure, "channels", &channels);
117 gst_structure_get_int (structure, "rate", &rate);
119 g_assert (bytes != 0);
120 g_assert (width != 0);
121 g_assert (channels != 0);
122 g_assert (rate != 0);
123 length = (bytes * 8 * GST_SECOND) / (rate * channels * width);
129 gst_audio_is_buffer_framed (GstPad * pad, GstBuffer * buf)
130 /* check if the buffer size is a whole multiple of the frame size */
132 if (GST_BUFFER_SIZE (buf) % gst_audio_frame_byte_size (pad) == 0)
138 /* _getcaps helper functions
139 * sets structure fields to default for audio type
140 * flag determines which structure fields to set to default
141 * keep these functions in sync with the templates in audio.h
144 /* private helper function
145 * sets a list on the structure
146 * pass in structure, fieldname for the list, type of the list values,
147 * number of list values, and each of the values, terminating with NULL
150 _gst_audio_structure_set_list (GstStructure * structure,
151 const gchar * fieldname, GType type, int number, ...)
154 GValue value = { 0 };
158 g_return_if_fail (structure != NULL);
160 g_value_init (&value, GST_TYPE_LIST);
161 array = g_value_peek_pointer (&value);
163 va_start (varargs, number);
165 for (j = 0; j < number; ++j) {
169 GValue list_value = { 0 };
173 i = va_arg (varargs, int);
175 g_value_init (&list_value, G_TYPE_INT);
176 g_value_set_int (&list_value, i);
179 b = va_arg (varargs, gboolean);
180 g_value_init (&list_value, G_TYPE_BOOLEAN);
181 g_value_set_boolean (&list_value, b);
185 ("_gst_audio_structure_set_list: LIST of given type not implemented.");
187 g_array_append_val (array, list_value);
190 gst_structure_set_value (structure, fieldname, &value);
195 gst_audio_structure_set_int (GstStructure * structure, GstAudioFieldFlag flag)
197 if (flag & GST_AUDIO_FIELD_RATE)
198 gst_structure_set (structure, "rate", GST_TYPE_INT_RANGE, 1, G_MAXINT,
200 if (flag & GST_AUDIO_FIELD_CHANNELS)
201 gst_structure_set (structure, "channels", GST_TYPE_INT_RANGE, 1, G_MAXINT,
203 if (flag & GST_AUDIO_FIELD_ENDIANNESS)
204 _gst_audio_structure_set_list (structure, "endianness", G_TYPE_INT, 2,
205 G_LITTLE_ENDIAN, G_BIG_ENDIAN, NULL);
206 if (flag & GST_AUDIO_FIELD_WIDTH)
207 _gst_audio_structure_set_list (structure, "width", G_TYPE_INT, 3, 8, 16, 32,
209 if (flag & GST_AUDIO_FIELD_DEPTH)
210 gst_structure_set (structure, "depth", GST_TYPE_INT_RANGE, 1, 32, NULL);
211 if (flag & GST_AUDIO_FIELD_SIGNED)
212 _gst_audio_structure_set_list (structure, "signed", G_TYPE_BOOLEAN, 2, TRUE,