summaryrefslogtreecommitdiff
path: root/swfdec/swfdec_audio_decoder_uncompressed.c
blob: e299f728eccbbf8fd295f0b4562c8623d16394a1 (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
/* 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_uncompressed.h"
#include "swfdec_debug.h"
#include "swfdec_internal.h"

G_DEFINE_TYPE (SwfdecAudioDecoderUncompressed, swfdec_audio_decoder_uncompressed, SWFDEC_TYPE_AUDIO_DECODER)

static gboolean
swfdec_audio_decoder_uncompressed_prepare (guint codec, SwfdecAudioFormat format, char **missing)
{
  return codec == SWFDEC_AUDIO_CODEC_UNDEFINED ||
      codec == SWFDEC_AUDIO_CODEC_UNCOMPRESSED;
}

static SwfdecAudioDecoder *
swfdec_audio_decoder_uncompressed_create (guint codec, SwfdecAudioFormat format, SwfdecBuffer *data)
{
  if (codec != SWFDEC_AUDIO_CODEC_UNDEFINED &&
      codec != SWFDEC_AUDIO_CODEC_UNCOMPRESSED)
    return NULL;

  return g_object_new (SWFDEC_TYPE_AUDIO_DECODER_UNCOMPRESSED, NULL);
}

static SwfdecBuffer *
swfdec_audio_decoder_uncompressed_upscale (SwfdecBuffer *buffer, SwfdecAudioFormat format)
{
  guint channels = swfdec_audio_format_get_channels (format);
  guint granularity = swfdec_audio_format_get_granularity (format);
  SwfdecBuffer *ret;
  guint i, j, n_samples;
  gint16 *src, *dest;

  n_samples = buffer->length / 2 / channels;
  if (n_samples * 2 * channels != buffer->length) {
    SWFDEC_ERROR ("incorrect buffer size, %"G_GSIZE_FORMAT" bytes overhead",
	buffer->length - n_samples * 2 * channels);
  }
  ret = swfdec_buffer_new (n_samples * 4 * granularity);
  /* can be upscaled, because the input buffer and the newly allocated buffer 
   * are aligned properly */
  src = (gint16 *) (gpointer) buffer->data;
  dest = (gint16 *) (gpointer) ret->data;
  for (i = 0; i < n_samples; i++) {
    for (j = 0; j < granularity; j++) {
      *dest++ = src[0];
      *dest++ = src[channels - 1];
    }
    src += channels;
  }

  swfdec_buffer_unref (buffer);
  return ret;
}

static SwfdecBuffer *
swfdec_audio_decoder_uncompressed_decode_8bit (SwfdecBuffer *buffer)
{
  SwfdecBuffer *ret;
  gint16 *out;
  guint8 *in;
  guint i;

  ret = swfdec_buffer_new (buffer->length * 2);
  out = (gint16 *) (void *) ret->data;
  in = buffer->data;
  for (i = 0; i < buffer->length; i++) {
    *out = ((gint16) *in << 8) ^ (-1);
    out++;
    in++;
  }
  return ret;
}

static SwfdecBuffer *
swfdec_audio_decoder_uncompressed_decode_16bit (SwfdecBuffer *buffer)
{
  SwfdecBuffer *ret;
  SwfdecBits bits;
  gint16 *dest;
  guint i;

  if (buffer->length & 2) {
    SWFDEC_ERROR ("buffer length not a multiple of 16bit");
  }
  ret = swfdec_buffer_new (buffer->length & ~1);
  swfdec_bits_init (&bits, buffer);
  dest = (gint16 *) (gpointer) ret->data;
  for (i = 0; i < ret->length; i += 2) {
    *dest = swfdec_bits_get_u16 (&bits);
    dest++;
  }

  return ret;
}

static void
swfdec_audio_decoder_uncompressed_push (SwfdecAudioDecoder *decoder, 
    SwfdecBuffer *buffer)
{
  SwfdecBuffer *tmp;

  if (buffer == NULL)
    return;

  if (swfdec_audio_format_is_16bit (decoder->format))
    tmp = swfdec_audio_decoder_uncompressed_decode_16bit (buffer);
  else
    tmp = swfdec_audio_decoder_uncompressed_decode_8bit (buffer);

  tmp = swfdec_audio_decoder_uncompressed_upscale (tmp, decoder->format);
  swfdec_buffer_queue_push (SWFDEC_AUDIO_DECODER_UNCOMPRESSED (decoder)->queue,
      tmp);
}

static SwfdecBuffer *
swfdec_audio_decoder_uncompressed_pull (SwfdecAudioDecoder *decoder)
{
  SwfdecAudioDecoderUncompressed *dec = (SwfdecAudioDecoderUncompressed *) decoder;
  
  return swfdec_buffer_queue_pull_buffer (dec->queue);
}

static void
swfdec_audio_decoder_uncompressed_dispose (GObject *object)
{
  SwfdecAudioDecoderUncompressed *dec = (SwfdecAudioDecoderUncompressed *) object;

  swfdec_buffer_queue_unref (dec->queue);

  G_OBJECT_CLASS (swfdec_audio_decoder_uncompressed_parent_class)->dispose (object);
}

static void
swfdec_audio_decoder_uncompressed_class_init (SwfdecAudioDecoderUncompressedClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  SwfdecAudioDecoderClass *decoder_class = SWFDEC_AUDIO_DECODER_CLASS (klass);

  object_class->dispose = swfdec_audio_decoder_uncompressed_dispose;

  decoder_class->prepare = swfdec_audio_decoder_uncompressed_prepare;
  decoder_class->create = swfdec_audio_decoder_uncompressed_create;
  decoder_class->pull = swfdec_audio_decoder_uncompressed_pull;
  decoder_class->push = swfdec_audio_decoder_uncompressed_push;
}

static void
swfdec_audio_decoder_uncompressed_init (SwfdecAudioDecoderUncompressed *dec)
{
  dec->queue = swfdec_buffer_queue_new ();
}