summaryrefslogtreecommitdiff
path: root/swfdec/swfdec_audio_decoder.c
blob: 72be7326f7e39794e995d1a33a04d82773d51119 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* Swfdec
 * Copyright (C) 2006-2008 Benjamin Otte <otte@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "swfdec_audio_decoder.h"
#include "swfdec_debug.h"
#include "swfdec_internal.h"

G_DEFINE_TYPE (SwfdecAudioDecoder, swfdec_audio_decoder, G_TYPE_OBJECT)

static void
swfdec_audio_decoder_class_init (SwfdecAudioDecoderClass *klass)
{
}

static void
swfdec_audio_decoder_init (SwfdecAudioDecoder *audio_decoder)
{
}

static GSList *audio_codecs = NULL;

void
swfdec_audio_decoder_register (GType type)
{
  g_return_if_fail (g_type_is_a (type, SWFDEC_TYPE_AUDIO_DECODER));

  audio_codecs = g_slist_append (audio_codecs, GSIZE_TO_POINTER ((gsize) type));
}

gboolean
swfdec_audio_decoder_prepare (guint codec, SwfdecAudioFormat format, char **missing)
{
  char *detail = NULL, *s = NULL;
  GSList *walk;
  
  for (walk = audio_codecs; walk; walk = walk->next) {
    SwfdecAudioDecoderClass *klass = g_type_class_ref (GPOINTER_TO_SIZE (walk->data));
    if (klass->prepare (codec, format, &s)) {
      g_free (detail);
      g_free (s);
      if (missing)
	*missing = NULL;
      g_type_class_unref (klass);
      return TRUE;
    }
    if (s) {
      if (detail == NULL)
	detail = s;
      else
	g_free (s);
      s = NULL;
    }
    g_type_class_unref (klass);
  }
  if (missing)
    *missing = detail;
  else
    g_free (detail);
  return FALSE;
}

/**
 * swfdec_audio_decoder_new:
 * @codec: codec id
 * @format: #SwfdecAudioCodec to decode
 *
 * Creates a decoder suitable for decoding @format. If no decoder is available
 * for the given for mat, %NULL is returned.
 *
 * Returns: a new decoder or %NULL
 **/
SwfdecAudioDecoder *
swfdec_audio_decoder_new (guint codec, SwfdecAudioFormat format)
{
  SwfdecAudioDecoder *ret = NULL;
  GSList *walk;
  
  g_return_val_if_fail (SWFDEC_IS_AUDIO_FORMAT (format), NULL);

  for (walk = audio_codecs; walk; walk = walk->next) {
    SwfdecAudioDecoderClass *klass = g_type_class_ref (GPOINTER_TO_SIZE (walk->data));
    ret = klass->create (codec, format);
    g_type_class_unref (klass);
    if (ret)
      break;
  }

  if (ret == NULL) {
    ret = g_object_new (SWFDEC_TYPE_AUDIO_DECODER, NULL);
    swfdec_audio_decoder_error (ret, "no suitable decoder for audio codec %u", codec);
  }

  ret->codec = codec;
  ret->format = format;

  return ret;
}

/**
 * swfdec_audio_decoder_push:
 * @decoder: a #SwfdecAudioDecoder
 * @buffer: a #SwfdecBuffer to process or %NULL to flush
 *
 * Pushes a new buffer into the decoding pipeline. After this the results can
 * be queried using swfdec_audio_decoder_pull(). Some decoders may not decode
 * all available data immediately. So when you are done decoding, you may want
 * to flush the decoder. Flushing can be achieved by passing %NULL as the 
 * @buffer argument. Do this when you are finished decoding.
 **/
void
swfdec_audio_decoder_push (SwfdecAudioDecoder *decoder, SwfdecBuffer *buffer)
{
  SwfdecAudioDecoderClass *klass;

  g_return_if_fail (SWFDEC_IS_AUDIO_DECODER (decoder));

  if (decoder->error)
    return;
  klass = SWFDEC_AUDIO_DECODER_GET_CLASS (decoder);
  klass->push (decoder, buffer);
}

/**
 * swfdec_audio_decoder_pull:
 * @decoder: a #SwfdecAudioDecoder
 *
 * Gets the next buffer of decoded audio data. Since some decoders do not
 * produce one output buffer per input buffer, any number of buffers may be
 * available after calling swfdec_audio_decoder_push(), even none. When no more
 * buffers are available, this function returns %NULL. You need to provide more
 * input in then. A simple decoding pipeline would look like this:
 * <informalexample><programlisting>do {
 *   input = next_input_buffer ();
 *   swfdec_audio_decoder_push (decoder, input);
 *   while ((output = swfdec_audio_decoder_pull (decoder))) {
 *     ... process output ...
 *   }
 * } while (input != NULL); </programlisting></informalexample>
 *
 * Returns: the next buffer or %NULL if no more buffers are available.
 **/
SwfdecBuffer *
swfdec_audio_decoder_pull (SwfdecAudioDecoder *decoder)
{
  SwfdecAudioDecoderClass *klass;
  SwfdecBuffer *result;

  g_return_val_if_fail (SWFDEC_IS_AUDIO_DECODER (decoder), NULL);

  if (decoder->error)
    return NULL;
  klass = SWFDEC_AUDIO_DECODER_GET_CLASS (decoder);
  result = klass->pull (decoder);
  /* result must be n samples of 44.1kHz stereo 16bit - and one sample is 4 bytes */
  g_assert (result == NULL || result->length % 4 == 0);
  return result;
}

void
swfdec_audio_decoder_error (SwfdecAudioDecoder *decoder, const char *error, ...)
{
  va_list args;

  g_return_if_fail (SWFDEC_IS_AUDIO_DECODER (decoder));
  g_return_if_fail (error != NULL);

  va_start (args, error);
  swfdec_audio_decoder_errorv (decoder, error, args);
  va_end (args);
}

void
swfdec_audio_decoder_errorv (SwfdecAudioDecoder *decoder, const char *error, va_list args)
{
  char *real;

  g_return_if_fail (SWFDEC_IS_AUDIO_DECODER (decoder));
  g_return_if_fail (error != NULL);

  real = g_strdup_vprintf (error, args);
  SWFDEC_ERROR ("error decoding audio: %s", real);
  g_free (real);
  decoder->error = TRUE;
}

/**
 * swfdec_audio_decoder_uses_format:
 * @decoder: the decoder to check
 * @codec: the codec the decoder should use
 * @format: the format the decoder should use
 *
 * This is a little helper function that checks if the decoder uses the right
 * format.
 *
 * Returns: %TRUE if the @decoder uses the given @codec and @format, %FALSE 
 *          otherwise.
 **/
gboolean
swfdec_audio_decoder_uses_format (SwfdecAudioDecoder *decoder, guint codec,
    SwfdecAudioFormat format)
{
  g_return_val_if_fail (SWFDEC_IS_AUDIO_DECODER (decoder), FALSE);
  g_return_val_if_fail (SWFDEC_IS_AUDIO_FORMAT (format), FALSE);

  return decoder->codec == codec && decoder->format == format;
}