summaryrefslogtreecommitdiffstats
path: root/ext/flac/gstflacdec.c
blob: a6a07e40cb77f761b31470999b706ac7a3bde22c (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* GStreamer
 * 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.
 */

#include <string.h>
#include <sys/soundcard.h>

/*#define DEBUG_ENABLED */
#include "gstflacdec.h"


extern GstPadTemplate *dec_src_template, *dec_sink_template;

/* elementfactory information */
GstElementDetails flacdec_details = {
  "FLAC decoder",
  "Codec/Audio/Decoder",
  "Decodes FLAC lossless audio streams",
  VERSION,
  "Wim Taymans <wim.taymans@chello.be>",
  "(C) 2001",
};

/* FlacDec signals and args */
enum {
  /* FILL ME */
  LAST_SIGNAL
};

enum {
  ARG_0,
};

static void 		gst_flacdec_class_init		(FlacDecClass *klass);
static void 		gst_flacdec_init		(FlacDec *flacdec);

static void 		gst_flacdec_loop		(GstElement *element);

static void 		gst_flacdec_metadata_callback 	(const FLAC__StreamDecoder *decoder, 
							 const FLAC__StreamMetaData *metadata, 
							 void *client_data);
static void 		gst_flacdec_error_callback 	(const FLAC__StreamDecoder *decoder, 
							 FLAC__StreamDecoderErrorStatus status, 
							 void *client_data);

static FLAC__StreamDecoderReadStatus 	gst_flacdec_read 	(const FLAC__StreamDecoder *decoder, 
								 FLAC__byte buffer[], unsigned *bytes, void *client_data);
static FLAC__StreamDecoderWriteStatus 	gst_flacdec_write 	(const FLAC__StreamDecoder *decoder, 
								 const FLAC__Frame *frame, const FLAC__int32 *buffer[], 
								 void *client_data);

static GstElementClass *parent_class = NULL;
/*static guint gst_flacdec_signals[LAST_SIGNAL] = { 0 }; */

GType
flacdec_get_type(void) {
  static GType flacdec_type = 0;

  if (!flacdec_type) {
    static const GTypeInfo flacdec_info = {
      sizeof(FlacDecClass),
      NULL,
      NULL,
      (GClassInitFunc)gst_flacdec_class_init,
      NULL,
      NULL,
      sizeof(FlacDec),
      0,
      (GInstanceInitFunc)gst_flacdec_init,
    };
    flacdec_type = g_type_register_static (GST_TYPE_ELEMENT, "FlacDec", &flacdec_info, 0);
  }
  return flacdec_type;
}

static void
gst_flacdec_class_init (FlacDecClass *klass) 
{
  GstElementClass *gstelement_class;

  gstelement_class = (GstElementClass*)klass;

  parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
}

static void 
gst_flacdec_init (FlacDec *flacdec) 
{
  flacdec->sinkpad = gst_pad_new_from_template (dec_sink_template, "sink");
  gst_element_add_pad (GST_ELEMENT (flacdec), flacdec->sinkpad);

  gst_element_set_loop_function (GST_ELEMENT (flacdec), gst_flacdec_loop);
  flacdec->srcpad = gst_pad_new_from_template (dec_src_template, "src");
  gst_element_add_pad (GST_ELEMENT (flacdec), flacdec->srcpad);

  flacdec->decoder = FLAC__stream_decoder_new ();
  flacdec->offset_left = 0;
  flacdec->data_left = NULL;

  FLAC__stream_decoder_set_read_callback (flacdec->decoder, gst_flacdec_read);
  FLAC__stream_decoder_set_write_callback (flacdec->decoder, gst_flacdec_write);
  FLAC__stream_decoder_set_metadata_callback (flacdec->decoder, gst_flacdec_metadata_callback);
  FLAC__stream_decoder_set_error_callback (flacdec->decoder, gst_flacdec_error_callback);
  FLAC__stream_decoder_set_client_data (flacdec->decoder, flacdec);

  FLAC__stream_decoder_init (flacdec->decoder);
}

static void 
gst_flacdec_metadata_callback (const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data)
{
}

static void 
gst_flacdec_error_callback (const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
{
}

static FLAC__StreamDecoderReadStatus
gst_flacdec_read (const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
{
  FlacDec *flacdec;
  GstBuffer *inbuf = NULL;
  gint insize;
  guchar *indata;

  flacdec = GST_FLACDEC (client_data);

  if (flacdec->data_left == NULL) {
    inbuf = gst_pad_pull (flacdec->sinkpad);
    insize = GST_BUFFER_SIZE (inbuf);
    indata = GST_BUFFER_DATA (inbuf);
  }
  else {
    inbuf = flacdec->data_left;
    insize = GST_BUFFER_SIZE (inbuf) - flacdec->offset_left;
    indata = GST_BUFFER_DATA (inbuf) + flacdec->offset_left;
  }

  if (*bytes < insize) {
    /* we have more than we can handle */
    flacdec->data_left = inbuf;
    flacdec->offset_left += *bytes;
    inbuf = NULL;
  }
  else {
    flacdec->data_left = NULL;
    flacdec->offset_left = 0;
    *bytes = insize;
  }
  memcpy (buffer, indata, *bytes);

  if (inbuf) 
    gst_buffer_unref (inbuf);

  return FLAC__STREAM_DECODER_READ_CONTINUE;
}

static FLAC__StreamDecoderWriteStatus
gst_flacdec_write (const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data)
{
  FlacDec *flacdec;
  GstBuffer *outbuf;
  guint depth = frame->header.bits_per_sample;
  guint channels = frame->header.channels;
  guint samples = frame->header.blocksize;
  guint j, i;

  flacdec = GST_FLACDEC (client_data);

  if (!GST_PAD_CAPS (flacdec->srcpad)) {
    gst_pad_try_set_caps (flacdec->srcpad,
		    GST_CAPS_NEW (
		      "flac_caps",
		      "audio/raw",
		        "format", 	GST_PROPS_STRING ("int"),
                         "law",         GST_PROPS_INT (0),
                         "endianness",  GST_PROPS_INT (G_BYTE_ORDER),
                         "signed",      GST_PROPS_BOOLEAN (TRUE),
                         "width",       GST_PROPS_INT (depth),
                         "depth",       GST_PROPS_INT (depth),
                         "rate",     	GST_PROPS_INT (frame->header.sample_rate),
                         "channels", 	GST_PROPS_INT (channels)
		    ));
  }

  outbuf = gst_buffer_new ();
  GST_BUFFER_SIZE (outbuf) = samples * channels * ((depth+7)>>3);
  GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));

  if (depth == 8) {
    guint8 *outbuffer = (guint8 *)GST_BUFFER_DATA (outbuf);

    for (i=0; i<samples; i++) {
      for (j=0; j < channels; j++) {
        *outbuffer++ = (guint8) buffer[j][i];
      }
    }
  }
  else if (depth == 16) {
    guint16 *outbuffer = (guint16 *)GST_BUFFER_DATA (outbuf);

    for (i=0; i<samples; i++) {
      for (j=0; j < channels; j++) {
        *outbuffer++ = (guint16) buffer[j][i];
      }
    }
  }
  else {
    g_warning ("flacdec: invalid depth %d found\n", depth);
    return FLAC__STREAM_DECODER_WRITE_ABORT;
  }

  gst_pad_push (flacdec->srcpad, outbuf);

  return FLAC__STREAM_DECODER_WRITE_CONTINUE;
}

static void 
gst_flacdec_loop (GstElement *element) 
{
  FlacDec *flacdec;

  flacdec = GST_FLACDEC (element);
  
  if (FLAC__stream_decoder_get_state (flacdec->decoder) == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA) {
    FLAC__stream_decoder_process_metadata (flacdec->decoder);
  }

  FLAC__stream_decoder_process_one_frame (flacdec->decoder);
}