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.
21 * @short_description: Support library for audio elements
23 * This library contains some helper functions for audio elements.
31 #include "audio-enumtypes.h"
33 #include <gst/gststructure.h>
36 * gst_audio_frame_byte_size:
37 * @pad: the #GstPad to get the caps from
39 * Calculate byte size of an audio frame.
41 * Returns: the byte size, or 0 if there was an error
44 gst_audio_frame_byte_size (GstPad * pad)
46 /* FIXME: this should be moved closer to the gstreamer core
47 * and be implemented for every mime type IMO
52 const GstCaps *caps = NULL;
53 GstStructure *structure;
56 caps = GST_PAD_CAPS (pad);
59 /* ERROR: could not get caps of pad */
60 g_warning ("gstaudio: could not get caps of pad %s:%s\n",
61 GST_DEBUG_PAD_NAME (pad));
65 structure = gst_caps_get_structure (caps, 0);
67 gst_structure_get_int (structure, "width", &width);
68 gst_structure_get_int (structure, "channels", &channels);
69 return (width / 8) * channels;
73 * gst_audio_frame_length:
74 * @pad: the #GstPad to get the caps from
75 * @buf: the #GstBuffer
77 * Calculate length of buffer in frames.
79 * Returns: 0 if there's an error, or the number of frames if everything's ok
82 gst_audio_frame_length (GstPad * pad, GstBuffer * buf)
84 /* FIXME: this should be moved closer to the gstreamer core
85 * and be implemented for every mime type IMO
87 int frame_byte_size = 0;
89 frame_byte_size = gst_audio_frame_byte_size (pad);
90 if (frame_byte_size == 0)
93 /* FIXME: this function assumes the buffer size to be a whole multiple
94 * of the frame byte size
96 return GST_BUFFER_SIZE (buf) / frame_byte_size;
100 * gst_audio_duration_from_pad_buffer:
101 * @pad: the #GstPad to get the caps from
102 * @buf: the #GstBuffer
104 * Calculate length in nanoseconds of audio buffer @buf based on capabilities of
107 * Returns: the length.
110 gst_audio_duration_from_pad_buffer (GstPad * pad, GstBuffer * buf)
119 const GstCaps *caps = NULL;
120 GstStructure *structure;
122 g_assert (GST_IS_BUFFER (buf));
123 /* get caps of pad */
124 caps = GST_PAD_CAPS (pad);
126 /* ERROR: could not get caps of pad */
127 g_warning ("gstaudio: could not get caps of pad %s:%s\n",
128 GST_DEBUG_PAD_NAME (pad));
129 length = GST_CLOCK_TIME_NONE;
131 structure = gst_caps_get_structure (caps, 0);
132 bytes = GST_BUFFER_SIZE (buf);
133 gst_structure_get_int (structure, "width", &width);
134 gst_structure_get_int (structure, "channels", &channels);
135 gst_structure_get_int (structure, "rate", &rate);
137 g_assert (bytes != 0);
138 g_assert (width != 0);
139 g_assert (channels != 0);
140 g_assert (rate != 0);
141 length = (bytes * 8 * GST_SECOND) / (rate * channels * width);
147 * gst_audio_is_buffer_framed:
148 * @pad: the #GstPad to get the caps from
149 * @buf: the #GstBuffer
151 * Check if the buffer size is a whole multiple of the frame size.
153 * Returns: %TRUE if buffer size is multiple.
156 gst_audio_is_buffer_framed (GstPad * pad, GstBuffer * buf)
158 if (GST_BUFFER_SIZE (buf) % gst_audio_frame_byte_size (pad) == 0)
164 /* _getcaps helper functions
165 * sets structure fields to default for audio type
166 * flag determines which structure fields to set to default
167 * keep these functions in sync with the templates in audio.h
170 /* private helper function
171 * sets a list on the structure
172 * pass in structure, fieldname for the list, type of the list values,
173 * number of list values, and each of the values, terminating with NULL
176 _gst_audio_structure_set_list (GstStructure * structure,
177 const gchar * fieldname, GType type, int number, ...)
180 GValue value = { 0 };
184 g_return_if_fail (structure != NULL);
186 g_value_init (&value, GST_TYPE_LIST);
187 array = g_value_peek_pointer (&value);
189 va_start (varargs, number);
191 for (j = 0; j < number; ++j) {
195 GValue list_value = { 0 };
199 i = va_arg (varargs, int);
201 g_value_init (&list_value, G_TYPE_INT);
202 g_value_set_int (&list_value, i);
205 b = va_arg (varargs, gboolean);
206 g_value_init (&list_value, G_TYPE_BOOLEAN);
207 g_value_set_boolean (&list_value, b);
211 ("_gst_audio_structure_set_list: LIST of given type not implemented.");
213 g_array_append_val (array, list_value);
216 gst_structure_set_value (structure, fieldname, &value);
221 * gst_audio_structure_set_int:
222 * @structure: a #GstStructure
223 * @flag: a set of #GstAudioFieldFlag
225 * Do not use anymore.
227 * Deprecated: use gst_structure_set()
229 #ifndef GST_REMOVE_DEPRECATED
230 #ifdef GST_DISABLE_DEPRECATED
233 GST_AUDIO_FIELD_RATE = (1 << 0),
234 GST_AUDIO_FIELD_CHANNELS = (1 << 1),
235 GST_AUDIO_FIELD_ENDIANNESS = (1 << 2),
236 GST_AUDIO_FIELD_WIDTH = (1 << 3),
237 GST_AUDIO_FIELD_DEPTH = (1 << 4),
238 GST_AUDIO_FIELD_SIGNED = (1 << 5),
241 gst_audio_structure_set_int (GstStructure * structure, GstAudioFieldFlag flag);
242 #endif /* GST_DISABLE_DEPRECATED */
245 gst_audio_structure_set_int (GstStructure * structure, GstAudioFieldFlag flag)
248 * http://webcvs.freedesktop.org/gstreamer/gst-plugins-base/gst-libs/gst/audio/audio.c?r1=1.16&r2=1.17
251 if (flag & GST_AUDIO_FIELD_RATE)
252 gst_structure_set (structure, "rate", GST_TYPE_INT_RANGE, 1, G_MAXINT,
254 if (flag & GST_AUDIO_FIELD_CHANNELS)
255 gst_structure_set (structure, "channels", GST_TYPE_INT_RANGE, 1, G_MAXINT,
257 if (flag & GST_AUDIO_FIELD_ENDIANNESS)
258 _gst_audio_structure_set_list (structure, "endianness", G_TYPE_INT, 2,
259 G_LITTLE_ENDIAN, G_BIG_ENDIAN, NULL);
260 if (flag & GST_AUDIO_FIELD_WIDTH)
261 _gst_audio_structure_set_list (structure, "width", G_TYPE_INT, 3, 8, 16, 32,
263 if (flag & GST_AUDIO_FIELD_DEPTH)
264 gst_structure_set (structure, "depth", GST_TYPE_INT_RANGE, 1, 32, NULL);
265 if (flag & GST_AUDIO_FIELD_SIGNED)
266 _gst_audio_structure_set_list (structure, "signed", G_TYPE_BOOLEAN, 2, TRUE,
269 #endif /* GST_REMOVE_DEPRECATED */
272 * gst_audio_buffer_clip:
273 * @buffer: The buffer to clip.
274 * @segment: Segment in %GST_FORMAT_TIME or %GST_FORMAT_DEFAULT to which the buffer should be clipped.
275 * @rate: sample rate.
276 * @frame_size: size of one audio frame in bytes.
278 * Clip the the buffer to the given %GstSegment.
280 * After calling this function the caller does not own a reference to
283 * Returns: %NULL if the buffer is completely outside the configured segment,
284 * otherwise the clipped buffer is returned.
286 * If the buffer has no timestamp, it is assumed to be inside the segment and
292 gst_audio_buffer_clip (GstBuffer * buffer, GstSegment * segment, gint rate,
296 GstClockTime timestamp = GST_CLOCK_TIME_NONE, duration = GST_CLOCK_TIME_NONE;
297 guint64 offset = GST_BUFFER_OFFSET_NONE, offset_end = GST_BUFFER_OFFSET_NONE;
301 gboolean change_duration = TRUE, change_offset = TRUE, change_offset_end =
304 g_return_val_if_fail (segment->format == GST_FORMAT_TIME ||
305 segment->format == GST_FORMAT_DEFAULT, buffer);
306 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
308 if (!GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
309 /* No timestamp - assume the buffer is completely in the segment */
312 /* Get copies of the buffer metadata to change later.
313 * Calculate the missing values for the calculations,
314 * they won't be changed later though. */
316 data = GST_BUFFER_DATA (buffer);
317 size = GST_BUFFER_SIZE (buffer);
319 timestamp = GST_BUFFER_TIMESTAMP (buffer);
320 if (GST_BUFFER_DURATION_IS_VALID (buffer)) {
321 duration = GST_BUFFER_DURATION (buffer);
323 change_duration = FALSE;
324 duration = gst_util_uint64_scale (size / frame_size, GST_SECOND, rate);
327 if (GST_BUFFER_OFFSET_IS_VALID (buffer)) {
328 offset = GST_BUFFER_OFFSET (buffer);
330 change_offset = FALSE;
334 if (GST_BUFFER_OFFSET_END_IS_VALID (buffer)) {
335 offset_end = GST_BUFFER_OFFSET_END (buffer);
337 change_offset_end = FALSE;
338 offset_end = offset + size / frame_size;
341 if (segment->format == GST_FORMAT_TIME) {
342 /* Handle clipping for GST_FORMAT_TIME */
344 gint64 start, stop, cstart, cstop, diff;
347 stop = timestamp + duration;
349 if (gst_segment_clip (segment, GST_FORMAT_TIME,
350 start, stop, &cstart, &cstop)) {
352 diff = cstart - start;
359 diff = gst_util_uint64_scale (diff, rate, GST_SECOND);
362 data += diff * frame_size;
363 size -= diff * frame_size;
368 /* duration is always valid if stop is valid */
371 diff = gst_util_uint64_scale (diff, rate, GST_SECOND);
372 if (change_offset_end)
374 size -= diff * frame_size;
377 gst_buffer_unref (buffer);
381 /* Handle clipping for GST_FORMAT_DEFAULT */
382 gint64 start, stop, cstart, cstop, diff;
384 g_return_val_if_fail (GST_BUFFER_OFFSET_IS_VALID (buffer), buffer);
389 if (gst_segment_clip (segment, GST_FORMAT_DEFAULT,
390 start, stop, &cstart, &cstop)) {
392 diff = cstart - start;
396 timestamp = gst_util_uint64_scale (cstart, GST_SECOND, rate);
399 duration -= gst_util_uint64_scale (diff, GST_SECOND, rate);
401 data += diff * frame_size;
402 size -= diff * frame_size;
410 duration -= gst_util_uint64_scale (diff, GST_SECOND, rate);
412 size -= diff * frame_size;
415 gst_buffer_unref (buffer);
420 /* Get a metadata writable buffer and apply all changes */
421 ret = gst_buffer_make_metadata_writable (buffer);
423 GST_BUFFER_TIMESTAMP (ret) = timestamp;
424 GST_BUFFER_SIZE (ret) = size;
425 GST_BUFFER_DATA (ret) = data;
428 GST_BUFFER_DURATION (ret) = duration;
430 GST_BUFFER_OFFSET (ret) = offset;
431 if (change_offset_end)
432 GST_BUFFER_OFFSET_END (ret) = offset_end;