audio.c 5.93 KB
Newer Older
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1
/* GStreamer
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

20
21
22
23
#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

24
#include "audio.h"
Ronald S. Bultje's avatar
Ronald S. Bultje committed
25
#include "multichannel-enumtypes.h"
26

27
28
#include <gst/gststructure.h>

29
int
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
30
gst_audio_frame_byte_size (GstPad * pad)
31
32
33
34
{
/* calculate byte size of an audio frame
 * this should be moved closer to the gstreamer core
 * and be implemented for every mime type IMO
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
35
 * returns -1 if there's an error (to avoid division by zero),
36
 * or the byte size if everything's ok
37
38
39
40
 */

  int width = 0;
  int channels = 0;
Jeremy Simon's avatar
Jeremy Simon committed
41
  const GstCaps *caps = NULL;
David Schleef's avatar
David Schleef committed
42
  GstStructure *structure;
43
44
45
46

  /* get caps of pad */
  caps = GST_PAD_CAPS (pad);

David Schleef's avatar
David Schleef committed
47
  if (caps == NULL) {
48
    /* ERROR: could not get caps of pad */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
49
    g_warning ("gstaudio: could not get caps of pad %s:%s\n",
50
        GST_DEBUG_PAD_NAME (pad));
51
    return 0;
52
  }
53

David Schleef's avatar
David Schleef committed
54
55
  structure = gst_caps_get_structure (caps, 0);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
56
  gst_structure_get_int (structure, "width", &width);
David Schleef's avatar
David Schleef committed
57
  gst_structure_get_int (structure, "channels", &channels);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
58
  return (width / 8) * channels;
59
60
61
}

long
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
62
gst_audio_frame_length (GstPad * pad, GstBuffer * buf)
63
64
65
66
67
68
69
70
71
72
73
74
75
/* calculate length of buffer in frames
 * this should be moved closer to the gstreamer core
 * and be implemented for every mime type IMO
 * returns 0 if there's an error, or the number of frames if everything's ok
 */
{
  int frame_byte_size = 0;

  frame_byte_size = gst_audio_frame_byte_size (pad);
  if (frame_byte_size == 0)
    /* error */
    return 0;
  /* FIXME: this function assumes the buffer size to be a whole multiple
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
76
   *        of the frame byte size
77
78
79
80
   */
  return GST_BUFFER_SIZE (buf) / frame_byte_size;
}

81
GstClockTime
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
gst_audio_duration_from_pad_buffer (GstPad * pad, GstBuffer * buf)
{
/* calculate length in nanoseconds
 * of audio buffer buf
 * based on capabilities of pad
 */

  long bytes = 0;
  int width = 0;
  int channels = 0;
  int rate = 0;

  GstClockTime length;

  const GstCaps *caps = NULL;
  GstStructure *structure;

  g_assert (GST_IS_BUFFER (buf));
  /* get caps of pad */
  caps = GST_PAD_CAPS (pad);
  if (caps == NULL) {
    /* ERROR: could not get caps of pad */
    g_warning ("gstaudio: could not get caps of pad %s:%s\n",
105
        GST_DEBUG_PAD_NAME (pad));
106
107
108
109
110
111
112
113
114
115
116
117
    length = GST_CLOCK_TIME_NONE;
  } else {
    structure = gst_caps_get_structure (caps, 0);
    bytes = GST_BUFFER_SIZE (buf);
    gst_structure_get_int (structure, "width", &width);
    gst_structure_get_int (structure, "channels", &channels);
    gst_structure_get_int (structure, "rate", &rate);

    g_assert (bytes != 0);
    g_assert (width != 0);
    g_assert (channels != 0);
    g_assert (rate != 0);
118
    length = (bytes * 8 * GST_SECOND) / (rate * channels * width);
119
120
121
122
  }
  return length;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
123
124
gboolean
gst_audio_is_buffer_framed (GstPad * pad, GstBuffer * buf)
125
126
127
128
129
130
131
/* check if the buffer size is a whole multiple of the frame size */
{
  if (GST_BUFFER_SIZE (buf) % gst_audio_frame_byte_size (pad) == 0)
    return TRUE;
  else
    return FALSE;
}
132

133
134
135
136
137
138
139
140
141
142
143
144
/* _getcaps helper functions
 * sets structure fields to default for audio type
 * flag determines which structure fields to set to default
 * keep these functions in sync with the templates in audio.h
 */

/* private helper function
 * sets a list on the structure
 * pass in structure, fieldname for the list, type of the list values,
 * number of list values, and each of the values, terminating with NULL
 */
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
145
146
_gst_audio_structure_set_list (GstStructure * structure,
    const gchar * fieldname, GType type, int number, ...)
147
148
149
150
151
152
153
154
155
156
157
158
159
{
  va_list varargs;
  GValue value = { 0 };
  GArray *array;
  int j;

  g_return_if_fail (structure != NULL);

  g_value_init (&value, GST_TYPE_LIST);
  array = g_value_peek_pointer (&value);

  va_start (varargs, number);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
160
  for (j = 0; j < number; ++j) {
161
162
163
164
165
    int i;
    gboolean b;

    GValue list_value = { 0 };

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
166
    switch (type) {
167
      case G_TYPE_INT:
168
        i = va_arg (varargs, int);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
169

170
171
172
        g_value_init (&list_value, G_TYPE_INT);
        g_value_set_int (&list_value, i);
        break;
173
      case G_TYPE_BOOLEAN:
174
175
176
177
        b = va_arg (varargs, gboolean);
        g_value_init (&list_value, G_TYPE_BOOLEAN);
        g_value_set_boolean (&list_value, b);
        break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
178
      default:
179
180
        g_warning
            ("_gst_audio_structure_set_list: LIST of given type not implemented.");
181
182
183
184
185
186
187
188
189
    }
    g_array_append_val (array, list_value);

  }
  gst_structure_set_value (structure, fieldname, &value);
  va_end (varargs);
}

void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
190
gst_audio_structure_set_int (GstStructure * structure, GstAudioFieldFlag flag)
191
192
{
  if (flag & GST_AUDIO_FIELD_RATE)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
193
    gst_structure_set (structure, "rate", GST_TYPE_INT_RANGE, 1, G_MAXINT,
194
        NULL);
195
  if (flag & GST_AUDIO_FIELD_CHANNELS)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
196
    gst_structure_set (structure, "channels", GST_TYPE_INT_RANGE, 1, G_MAXINT,
197
        NULL);
198
  if (flag & GST_AUDIO_FIELD_ENDIANNESS)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
199
    _gst_audio_structure_set_list (structure, "endianness", G_TYPE_INT, 2,
200
        G_LITTLE_ENDIAN, G_BIG_ENDIAN, NULL);
201
  if (flag & GST_AUDIO_FIELD_WIDTH)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
202
    _gst_audio_structure_set_list (structure, "width", G_TYPE_INT, 3, 8, 16, 32,
203
        NULL);
204
205
206
  if (flag & GST_AUDIO_FIELD_DEPTH)
    gst_structure_set (structure, "depth", GST_TYPE_INT_RANGE, 1, 32, NULL);
  if (flag & GST_AUDIO_FIELD_SIGNED)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
207
    _gst_audio_structure_set_list (structure, "signed", G_TYPE_BOOLEAN, 2, TRUE,
208
        FALSE, NULL);
209
}